blob: 67b79a7592a9d94c92d0e737d82638fdc5e7f74d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/core.c - core driver model code (device registration, etc)
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07006 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070019#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100020#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/semaphore.h>
23
24#include "base.h"
25#include "power/power.h"
26
27int (*platform_notify)(struct device * dev) = NULL;
28int (*platform_notify_remove)(struct device * dev) = NULL;
29
30/*
31 * sysfs bindings for devices.
32 */
33
Alan Stern3e956372006-06-16 17:10:48 -040034/**
35 * dev_driver_string - Return a device's driver name, if at all possible
36 * @dev: struct device to get the name of
37 *
38 * Will return the device's driver's name if it is bound to a device. If
39 * the device is not bound to a device, it will return the name of the bus
40 * it is attached to. If it is not attached to a bus either, an empty
41 * string will be returned.
42 */
43const char *dev_driver_string(struct device *dev)
44{
45 return dev->driver ? dev->driver->name :
46 (dev->bus ? dev->bus->name : "");
47}
Matthew Wilcox310a9222006-09-23 23:35:04 -060048EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define to_dev(obj) container_of(obj, struct device, kobj)
51#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static ssize_t
54dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
55{
56 struct device_attribute * dev_attr = to_dev_attr(attr);
57 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050058 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040061 ret = dev_attr->show(dev, dev_attr, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return ret;
63}
64
65static ssize_t
66dev_attr_store(struct kobject * kobj, struct attribute * attr,
67 const char * buf, size_t count)
68{
69 struct device_attribute * dev_attr = to_dev_attr(attr);
70 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050071 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040074 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return ret;
76}
77
78static struct sysfs_ops dev_sysfs_ops = {
79 .show = dev_attr_show,
80 .store = dev_attr_store,
81};
82
83
84/**
85 * device_release - free device structure.
86 * @kobj: device's kobject.
87 *
88 * This is called once the reference count for the object
89 * reaches 0. We forward the call to the device's release
90 * method, which should handle actually freeing the structure.
91 */
92static void device_release(struct kobject * kobj)
93{
94 struct device * dev = to_dev(kobj);
95
96 if (dev->release)
97 dev->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -070098 else if (dev->class && dev->class->dev_release)
99 dev->class->dev_release(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 else {
101 printk(KERN_ERR "Device '%s' does not have a release() function, "
102 "it is broken and must be fixed.\n",
103 dev->bus_id);
104 WARN_ON(1);
105 }
106}
107
108static struct kobj_type ktype_device = {
109 .release = device_release,
110 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113
Kay Sievers312c0042005-11-16 09:00:00 +0100114static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct kobj_type *ktype = get_ktype(kobj);
117
118 if (ktype == &ktype_device) {
119 struct device *dev = to_dev(kobj);
120 if (dev->bus)
121 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700122 if (dev->class)
123 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125 return 0;
126}
127
Kay Sievers312c0042005-11-16 09:00:00 +0100128static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct device *dev = to_dev(kobj);
131
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700132 if (dev->bus)
133 return dev->bus->name;
134 if (dev->class)
135 return dev->class->name;
136 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Kay Sievers312c0042005-11-16 09:00:00 +0100139static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int num_envp, char *buffer, int buffer_size)
141{
142 struct device *dev = to_dev(kobj);
143 int i = 0;
144 int length = 0;
145 int retval = 0;
146
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700147 /* add the major/minor if present */
148 if (MAJOR(dev->devt)) {
149 add_uevent_var(envp, num_envp, &i,
150 buffer, buffer_size, &length,
151 "MAJOR=%u", MAJOR(dev->devt));
152 add_uevent_var(envp, num_envp, &i,
153 buffer, buffer_size, &length,
154 "MINOR=%u", MINOR(dev->devt));
155 }
156
Kay Sieversa87cb2a2006-09-14 11:23:28 +0200157#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sieversd81d9d62006-08-13 06:17:09 +0200158 /* add bus name (same as SUBSYSTEM, deprecated) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (dev->bus)
Kay Sievers312c0042005-11-16 09:00:00 +0100160 add_uevent_var(envp, num_envp, &i,
161 buffer, buffer_size, &length,
162 "PHYSDEVBUS=%s", dev->bus->name);
Kay Sieversa87cb2a2006-09-14 11:23:28 +0200163#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Kay Sieversd81d9d62006-08-13 06:17:09 +0200165 /* add driver name (PHYSDEV* values are deprecated)*/
166 if (dev->driver) {
167 add_uevent_var(envp, num_envp, &i,
168 buffer, buffer_size, &length,
169 "DRIVER=%s", dev->driver->name);
Kay Sieversa87cb2a2006-09-14 11:23:28 +0200170#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sievers312c0042005-11-16 09:00:00 +0100171 add_uevent_var(envp, num_envp, &i,
172 buffer, buffer_size, &length,
173 "PHYSDEVDRIVER=%s", dev->driver->name);
Kay Sieversa87cb2a2006-09-14 11:23:28 +0200174#endif
Kay Sieversd81d9d62006-08-13 06:17:09 +0200175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /* terminate, set to next free slot, shrink available space */
178 envp[i] = NULL;
179 envp = &envp[i];
180 num_envp -= i;
181 buffer = &buffer[length];
182 buffer_size -= length;
183
Kay Sievers312c0042005-11-16 09:00:00 +0100184 if (dev->bus && dev->bus->uevent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100186 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (retval) {
Kay Sievers312c0042005-11-16 09:00:00 +0100188 pr_debug ("%s - uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 __FUNCTION__, retval);
190 }
191 }
192
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700193 if (dev->class && dev->class->dev_uevent) {
194 /* have the class specific function add its stuff */
195 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
196 if (retval) {
197 pr_debug("%s - dev_uevent() returned %d\n",
198 __FUNCTION__, retval);
199 }
200 }
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return retval;
203}
204
Kay Sievers312c0042005-11-16 09:00:00 +0100205static struct kset_uevent_ops device_uevent_ops = {
206 .filter = dev_uevent_filter,
207 .name = dev_uevent_name,
208 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210
Kay Sieversa7fd6702005-10-01 14:49:43 +0200211static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
212 const char *buf, size_t count)
213{
Kay Sievers312c0042005-11-16 09:00:00 +0100214 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200215 return count;
216}
217
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700218static int device_add_groups(struct device *dev)
219{
220 int i;
221 int error = 0;
222
223 if (dev->groups) {
224 for (i = 0; dev->groups[i]; i++) {
225 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
226 if (error) {
227 while (--i >= 0)
228 sysfs_remove_group(&dev->kobj, dev->groups[i]);
229 goto out;
230 }
231 }
232 }
233out:
234 return error;
235}
236
237static void device_remove_groups(struct device *dev)
238{
239 int i;
240 if (dev->groups) {
241 for (i = 0; dev->groups[i]; i++) {
242 sysfs_remove_group(&dev->kobj, dev->groups[i]);
243 }
244 }
245}
246
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700247static int device_add_attrs(struct device *dev)
248{
249 struct class *class = dev->class;
250 int error = 0;
251 int i;
252
253 if (!class)
254 return 0;
255
256 if (class->dev_attrs) {
257 for (i = 0; attr_name(class->dev_attrs[i]); i++) {
258 error = device_create_file(dev, &class->dev_attrs[i]);
259 if (error)
260 break;
261 }
262 }
263 if (error)
264 while (--i >= 0)
265 device_remove_file(dev, &class->dev_attrs[i]);
266 return error;
267}
268
269static void device_remove_attrs(struct device *dev)
270{
271 struct class *class = dev->class;
272 int i;
273
274 if (!class)
275 return;
276
277 if (class->dev_attrs) {
278 for (i = 0; attr_name(class->dev_attrs[i]); i++)
279 device_remove_file(dev, &class->dev_attrs[i]);
280 }
281}
282
283
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700284static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
285 char *buf)
286{
287 return print_dev_t(buf, dev->devt);
288}
289
Martin Waitz0863afb2006-01-09 20:53:55 -0800290/*
291 * devices_subsys - structure to be registered with kobject core.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
293
Kay Sievers312c0042005-11-16 09:00:00 +0100294decl_subsys(devices, &ktype_device, &device_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296
297/**
298 * device_create_file - create sysfs attribute file for device.
299 * @dev: device.
300 * @attr: device attribute descriptor.
301 */
302
303int device_create_file(struct device * dev, struct device_attribute * attr)
304{
305 int error = 0;
306 if (get_device(dev)) {
307 error = sysfs_create_file(&dev->kobj, &attr->attr);
308 put_device(dev);
309 }
310 return error;
311}
312
313/**
314 * device_remove_file - remove sysfs attribute file.
315 * @dev: device.
316 * @attr: device attribute descriptor.
317 */
318
319void device_remove_file(struct device * dev, struct device_attribute * attr)
320{
321 if (get_device(dev)) {
322 sysfs_remove_file(&dev->kobj, &attr->attr);
323 put_device(dev);
324 }
325}
326
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700327/**
328 * device_create_bin_file - create sysfs binary attribute file for device.
329 * @dev: device.
330 * @attr: device binary attribute descriptor.
331 */
332int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
333{
334 int error = -EINVAL;
335 if (dev)
336 error = sysfs_create_bin_file(&dev->kobj, attr);
337 return error;
338}
339EXPORT_SYMBOL_GPL(device_create_bin_file);
340
341/**
342 * device_remove_bin_file - remove sysfs binary attribute file
343 * @dev: device.
344 * @attr: device binary attribute descriptor.
345 */
346void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
347{
348 if (dev)
349 sysfs_remove_bin_file(&dev->kobj, attr);
350}
351EXPORT_SYMBOL_GPL(device_remove_bin_file);
352
James Bottomley34bb61f2005-09-06 16:56:51 -0700353static void klist_children_get(struct klist_node *n)
354{
355 struct device *dev = container_of(n, struct device, knode_parent);
356
357 get_device(dev);
358}
359
360static void klist_children_put(struct klist_node *n)
361{
362 struct device *dev = container_of(n, struct device, knode_parent);
363
364 put_device(dev);
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368/**
369 * device_initialize - init device structure.
370 * @dev: device.
371 *
372 * This prepares the device for use by other layers,
373 * including adding it to the device hierarchy.
374 * It is the first half of device_register(), if called by
375 * that, though it can also be called separately, so one
376 * may use @dev's fields (e.g. the refcount).
377 */
378
379void device_initialize(struct device *dev)
380{
381 kobj_set_kset_s(dev, devices_subsys);
382 kobject_init(&dev->kobj);
James Bottomley34bb61f2005-09-06 16:56:51 -0700383 klist_init(&dev->klist_children, klist_children_get,
384 klist_children_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 INIT_LIST_HEAD(&dev->dma_pools);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700386 INIT_LIST_HEAD(&dev->node);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800387 init_MUTEX(&dev->sem);
David Brownell0ac85242005-09-12 19:39:34 -0700388 device_init_wakeup(dev, 0);
Christoph Hellwig87348132006-12-06 20:32:33 -0800389 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100392#ifdef CONFIG_SYSFS_DEPRECATED
Adrian Bunkaf9e0762006-11-17 02:19:44 +0100393static int setup_parent(struct device *dev, struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100394{
395 /* Set the parent to the class, not the parent device */
396 /* this keeps sysfs from having a symlink to make old udevs happy */
397 if (dev->class)
398 dev->kobj.parent = &dev->class->subsys.kset.kobj;
399 else if (parent)
400 dev->kobj.parent = &parent->kobj;
401
402 return 0;
403}
404#else
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700405static int virtual_device_parent(struct device *dev)
406{
407 if (!dev->class)
408 return -ENODEV;
409
410 if (!dev->class->virtual_dir) {
411 static struct kobject *virtual_dir = NULL;
412
413 if (!virtual_dir)
414 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
415 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
416 }
417
418 dev->kobj.parent = dev->class->virtual_dir;
419 return 0;
420}
421
Adrian Bunkaf9e0762006-11-17 02:19:44 +0100422static int setup_parent(struct device *dev, struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100423{
424 int error;
425
426 /* if this is a class device, and has no parent, create one */
427 if ((dev->class) && (parent == NULL)) {
428 error = virtual_device_parent(dev);
429 if (error)
430 return error;
431 } else if (parent)
432 dev->kobj.parent = &parent->kobj;
433
434 return 0;
435}
436#endif
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/**
439 * device_add - add device to device hierarchy.
440 * @dev: device.
441 *
442 * This is part 2 of device_register(), though may be called
443 * separately _iff_ device_initialize() has been called separately.
444 *
445 * This adds it to the kobject hierarchy via kobject_add(), adds it
446 * to the global and sibling lists for the device, then
447 * adds it to the other relevant subsystems of the driver model.
448 */
449int device_add(struct device *dev)
450{
451 struct device *parent = NULL;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700452 char *class_name = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200453 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int error = -EINVAL;
455
456 dev = get_device(dev);
457 if (!dev || !strlen(dev->bus_id))
458 goto Error;
459
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100460 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 parent = get_device(dev->parent);
463
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100464 error = setup_parent(dev, parent);
465 if (error)
466 goto Error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 /* first, register with generic layer. */
469 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100470 error = kobject_add(&dev->kobj);
471 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200473
Brian Walsh37022642006-08-14 22:43:19 -0700474 /* notify platform of device entry */
475 if (platform_notify)
476 platform_notify(dev);
477
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000478 /* notify clients of device entry (new way) */
479 if (dev->bus)
480 blocking_notifier_call_chain(&dev->bus->bus_notifier,
481 BUS_NOTIFY_ADD_DEVICE, dev);
482
Kay Sieversa7fd6702005-10-01 14:49:43 +0200483 dev->uevent_attr.attr.name = "uevent";
484 dev->uevent_attr.attr.mode = S_IWUSR;
485 if (dev->driver)
486 dev->uevent_attr.attr.owner = dev->driver->owner;
487 dev->uevent_attr.store = store_uevent;
Cornelia Hucka306eea2006-09-22 11:37:13 +0200488 error = device_create_file(dev, &dev->uevent_attr);
489 if (error)
490 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200491
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700492 if (MAJOR(dev->devt)) {
493 struct device_attribute *attr;
494 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
495 if (!attr) {
496 error = -ENOMEM;
Cornelia Hucka306eea2006-09-22 11:37:13 +0200497 goto ueventattrError;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700498 }
499 attr->attr.name = "dev";
500 attr->attr.mode = S_IRUGO;
501 if (dev->driver)
502 attr->attr.owner = dev->driver->owner;
503 attr->show = show_dev;
504 error = device_create_file(dev, attr);
505 if (error) {
506 kfree(attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200507 goto ueventattrError;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700508 }
509
510 dev->devt_attr = attr;
511 }
512
Kay Sieversb9d9c822006-06-15 15:31:56 +0200513 if (dev->class) {
514 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
515 "subsystem");
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100516 /* If this is not a "fake" compatible device, then create the
517 * symlink from the class to the device. */
518 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
519 sysfs_create_link(&dev->class->subsys.kset.kobj,
520 &dev->kobj, dev->bus_id);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200521#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700522 if (parent) {
523 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
524 class_name = make_class_name(dev->class->name, &dev->kobj);
525 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
526 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200527#endif
Kay Sieversb9d9c822006-06-15 15:31:56 +0200528 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700529
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700530 if ((error = device_add_attrs(dev)))
531 goto AttrsError;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700532 if ((error = device_add_groups(dev)))
533 goto GroupError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if ((error = device_pm_add(dev)))
535 goto PMError;
536 if ((error = bus_add_device(dev)))
537 goto BusError;
Kay Sievers53877d02006-04-04 20:42:26 +0200538 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Sternf70fa622006-10-05 17:03:24 -0400539 if ((error = bus_attach_device(dev)))
540 goto AttachError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (parent)
James Bottomleyd856f1e2005-08-19 09:14:01 -0400542 klist_add_tail(&dev->knode_parent, &parent->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700544 if (dev->class) {
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700545 down(&dev->class->sem);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200546 /* tie the class to the device */
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700547 list_add_tail(&dev->node, &dev->class->devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200548
549 /* notify any interfaces that the device is here */
550 list_for_each_entry(class_intf, &dev->class->interfaces, node)
551 if (class_intf->add_dev)
552 class_intf->add_dev(dev, class_intf);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700553 up(&dev->class->sem);
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 Done:
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700556 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 put_device(dev);
558 return error;
Alan Sternf70fa622006-10-05 17:03:24 -0400559 AttachError:
560 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 BusError:
562 device_pm_remove(dev);
563 PMError:
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000564 if (dev->bus)
565 blocking_notifier_call_chain(&dev->bus->bus_notifier,
566 BUS_NOTIFY_DEL_DEVICE, dev);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700567 device_remove_groups(dev);
568 GroupError:
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700569 device_remove_attrs(dev);
570 AttrsError:
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700571 if (dev->devt_attr) {
572 device_remove_file(dev, dev->devt_attr);
573 kfree(dev->devt_attr);
574 }
Cornelia Hucka306eea2006-09-22 11:37:13 +0200575 ueventattrError:
576 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700577 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100578 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 kobject_del(&dev->kobj);
580 Error:
581 if (parent)
582 put_device(parent);
583 goto Done;
584}
585
586
587/**
588 * device_register - register a device with the system.
589 * @dev: pointer to the device structure
590 *
591 * This happens in two clean steps - initialize the device
592 * and add it to the system. The two steps can be called
593 * separately, but this is the easiest and most common.
594 * I.e. you should only call the two helpers separately if
595 * have a clearly defined need to use and refcount the device
596 * before it is added to the hierarchy.
597 */
598
599int device_register(struct device *dev)
600{
601 device_initialize(dev);
602 return device_add(dev);
603}
604
605
606/**
607 * get_device - increment reference count for device.
608 * @dev: device.
609 *
610 * This simply forwards the call to kobject_get(), though
611 * we do take care to provide for the case that we get a NULL
612 * pointer passed in.
613 */
614
615struct device * get_device(struct device * dev)
616{
617 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
618}
619
620
621/**
622 * put_device - decrement reference count.
623 * @dev: device in question.
624 */
625void put_device(struct device * dev)
626{
627 if (dev)
628 kobject_put(&dev->kobj);
629}
630
631
632/**
633 * device_del - delete device from system.
634 * @dev: device.
635 *
636 * This is the first part of the device unregistration
637 * sequence. This removes the device from the lists we control
638 * from here, has it removed from the other driver model
639 * subsystems it was added to in device_add(), and removes it
640 * from the kobject hierarchy.
641 *
642 * NOTE: this should be called manually _iff_ device_add() was
643 * also called manually.
644 */
645
646void device_del(struct device * dev)
647{
648 struct device * parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200649 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (parent)
Patrick Mocheld62c0f92005-06-24 08:39:33 -0700652 klist_del(&dev->knode_parent);
Catalin Marinas82189b92006-11-25 11:09:30 -0800653 if (dev->devt_attr) {
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700654 device_remove_file(dev, dev->devt_attr);
Catalin Marinas82189b92006-11-25 11:09:30 -0800655 kfree(dev->devt_attr);
656 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200657 if (dev->class) {
658 sysfs_remove_link(&dev->kobj, "subsystem");
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100659 /* If this is not a "fake" compatible device, remove the
660 * symlink from the class to the device. */
661 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
662 sysfs_remove_link(&dev->class->subsys.kset.kobj,
663 dev->bus_id);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200664#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700665 if (parent) {
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200666 char *class_name = make_class_name(dev->class->name,
667 &dev->kobj);
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700668 sysfs_remove_link(&dev->parent->kobj, class_name);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200669 kfree(class_name);
670 sysfs_remove_link(&dev->kobj, "device");
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700671 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200672#endif
673
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700674 down(&dev->class->sem);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200675 /* notify any interfaces that the device is now gone */
676 list_for_each_entry(class_intf, &dev->class->interfaces, node)
677 if (class_intf->remove_dev)
678 class_intf->remove_dev(dev, class_intf);
679 /* remove the device from the class list */
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700680 list_del_init(&dev->node);
681 up(&dev->class->sem);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200682 }
Kay Sieversa7fd6702005-10-01 14:49:43 +0200683 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700684 device_remove_groups(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700685 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -0800686 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 /* Notify the platform of the removal, in case they
689 * need to do anything...
690 */
691 if (platform_notify_remove)
692 platform_notify_remove(dev);
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000693 if (dev->bus)
694 blocking_notifier_call_chain(&dev->bus->bus_notifier,
695 BUS_NOTIFY_DEL_DEVICE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 device_pm_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +0100697 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 kobject_del(&dev->kobj);
699 if (parent)
700 put_device(parent);
701}
702
703/**
704 * device_unregister - unregister device from system.
705 * @dev: device going away.
706 *
707 * We do this in two parts, like we do device_register(). First,
708 * we remove it from all the subsystems with device_del(), then
709 * we decrement the reference count via put_device(). If that
710 * is the final reference count, the device will be cleaned up
711 * via device_release() above. Otherwise, the structure will
712 * stick around until the final reference to the device is dropped.
713 */
714void device_unregister(struct device * dev)
715{
716 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
717 device_del(dev);
718 put_device(dev);
719}
720
721
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800722static struct device * next_device(struct klist_iter * i)
723{
724 struct klist_node * n = klist_next(i);
725 return n ? container_of(n, struct device, knode_parent) : NULL;
726}
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728/**
729 * device_for_each_child - device child iterator.
Randy Dunlapc41455f2005-10-23 11:59:14 -0700730 * @parent: parent struct device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * @data: data for the callback.
732 * @fn: function to be called for each device.
733 *
Randy Dunlapc41455f2005-10-23 11:59:14 -0700734 * Iterate over @parent's child devices, and call @fn for each,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * passing it @data.
736 *
737 * We check the return of @fn each time. If it returns anything
738 * other than 0, we break out and return that value.
739 */
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800740int device_for_each_child(struct device * parent, void * data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 int (*fn)(struct device *, void *))
742{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800743 struct klist_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 struct device * child;
745 int error = 0;
746
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800747 klist_iter_init(&parent->klist_children, &i);
748 while ((child = next_device(&i)) && !error)
749 error = fn(child, data);
750 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return error;
752}
753
Cornelia Huck5ab69982006-11-16 15:42:07 +0100754/**
755 * device_find_child - device iterator for locating a particular device.
756 * @parent: parent struct device
757 * @data: Data to pass to match function
758 * @match: Callback function to check device
759 *
760 * This is similar to the device_for_each_child() function above, but it
761 * returns a reference to a device that is 'found' for later use, as
762 * determined by the @match callback.
763 *
764 * The callback should return 0 if the device doesn't match and non-zero
765 * if it does. If the callback returns non-zero and a reference to the
766 * current device can be obtained, this function will return to the caller
767 * and not iterate over any more devices.
768 */
769struct device * device_find_child(struct device *parent, void *data,
770 int (*match)(struct device *, void *))
771{
772 struct klist_iter i;
773 struct device *child;
774
775 if (!parent)
776 return NULL;
777
778 klist_iter_init(&parent->klist_children, &i);
779 while ((child = next_device(&i)))
780 if (match(child, data) && get_device(child))
781 break;
782 klist_iter_exit(&i);
783 return child;
784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786int __init devices_init(void)
787{
788 return subsystem_register(&devices_subsys);
789}
790
791EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +0100792EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794EXPORT_SYMBOL_GPL(device_initialize);
795EXPORT_SYMBOL_GPL(device_add);
796EXPORT_SYMBOL_GPL(device_register);
797
798EXPORT_SYMBOL_GPL(device_del);
799EXPORT_SYMBOL_GPL(device_unregister);
800EXPORT_SYMBOL_GPL(get_device);
801EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803EXPORT_SYMBOL_GPL(device_create_file);
804EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700805
806
807static void device_create_release(struct device *dev)
808{
809 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
810 kfree(dev);
811}
812
813/**
814 * device_create - creates a device and registers it with sysfs
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200815 * @class: pointer to the struct class that this device should be registered to
816 * @parent: pointer to the parent struct device of this new device, if any
817 * @devt: the dev_t for the char device to be added
818 * @fmt: string for the device's name
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700819 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200820 * This function can be used by char device classes. A struct device
821 * will be created in sysfs, registered to the specified class.
822 *
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700823 * A "dev" file will be created, showing the dev_t for the device, if
824 * the dev_t is not 0,0.
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200825 * If a pointer to a parent struct device is passed in, the newly created
826 * struct device will be a child of that device in sysfs.
827 * The pointer to the struct device will be returned from the call.
828 * Any further sysfs files that might be required can be created using this
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700829 * pointer.
830 *
831 * Note: the struct class passed to this function must have previously
832 * been created with a call to class_create().
833 */
834struct device *device_create(struct class *class, struct device *parent,
Greg Kroah-Hartman5cbe5f82006-08-10 01:19:19 -0400835 dev_t devt, const char *fmt, ...)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700836{
837 va_list args;
838 struct device *dev = NULL;
839 int retval = -ENODEV;
840
841 if (class == NULL || IS_ERR(class))
842 goto error;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700843
844 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
845 if (!dev) {
846 retval = -ENOMEM;
847 goto error;
848 }
849
850 dev->devt = devt;
851 dev->class = class;
852 dev->parent = parent;
853 dev->release = device_create_release;
854
855 va_start(args, fmt);
856 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
857 va_end(args);
858 retval = device_register(dev);
859 if (retval)
860 goto error;
861
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700862 return dev;
863
864error:
865 kfree(dev);
866 return ERR_PTR(retval);
867}
868EXPORT_SYMBOL_GPL(device_create);
869
870/**
871 * device_destroy - removes a device that was created with device_create()
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200872 * @class: pointer to the struct class that this device was registered with
873 * @devt: the dev_t of the device that was previously registered
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700874 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +0200875 * This call unregisters and cleans up a device that was created with a
876 * call to device_create().
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700877 */
878void device_destroy(struct class *class, dev_t devt)
879{
880 struct device *dev = NULL;
881 struct device *dev_tmp;
882
883 down(&class->sem);
884 list_for_each_entry(dev_tmp, &class->devices, node) {
885 if (dev_tmp->devt == devt) {
886 dev = dev_tmp;
887 break;
888 }
889 }
890 up(&class->sem);
891
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700892 if (dev)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700893 device_unregister(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700894}
895EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700896
897/**
898 * device_rename - renames a device
899 * @dev: the pointer to the struct device to be renamed
900 * @new_name: the new name of the device
901 */
902int device_rename(struct device *dev, char *new_name)
903{
904 char *old_class_name = NULL;
905 char *new_class_name = NULL;
906 char *old_symlink_name = NULL;
907 int error;
908
909 dev = get_device(dev);
910 if (!dev)
911 return -EINVAL;
912
913 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
914
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200915#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700916 if ((dev->class) && (dev->parent))
917 old_class_name = make_class_name(dev->class->name, &dev->kobj);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200918#endif
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700919
920 if (dev->class) {
921 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
Jesper Juhl952ab432006-09-28 23:56:01 +0200922 if (!old_symlink_name) {
923 error = -ENOMEM;
924 goto out_free_old_class;
925 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700926 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
927 }
928
929 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
930
931 error = kobject_rename(&dev->kobj, new_name);
932
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200933#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700934 if (old_class_name) {
935 new_class_name = make_class_name(dev->class->name, &dev->kobj);
936 if (new_class_name) {
937 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
938 new_class_name);
939 sysfs_remove_link(&dev->parent->kobj, old_class_name);
940 }
941 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200942#endif
943
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700944 if (dev->class) {
945 sysfs_remove_link(&dev->class->subsys.kset.kobj,
946 old_symlink_name);
947 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
948 dev->bus_id);
949 }
950 put_device(dev);
951
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700952 kfree(new_class_name);
953 kfree(old_symlink_name);
Jesper Juhl952ab432006-09-28 23:56:01 +0200954 out_free_old_class:
955 kfree(old_class_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -0700956
957 return error;
958}
Cornelia Huck8a824722006-11-20 17:07:51 +0100959
960
961static int device_move_class_links(struct device *dev,
962 struct device *old_parent,
963 struct device *new_parent)
964{
965#ifdef CONFIG_SYSFS_DEPRECATED
966 int error;
967 char *class_name;
968
969 class_name = make_class_name(dev->class->name, &dev->kobj);
970 if (!class_name) {
971 error = PTR_ERR(class_name);
972 class_name = NULL;
973 goto out;
974 }
975 if (old_parent) {
976 sysfs_remove_link(&dev->kobj, "device");
977 sysfs_remove_link(&old_parent->kobj, class_name);
978 }
979 error = sysfs_create_link(&dev->kobj, &new_parent->kobj, "device");
980 if (error)
981 goto out;
982 error = sysfs_create_link(&new_parent->kobj, &dev->kobj, class_name);
983 if (error)
984 sysfs_remove_link(&dev->kobj, "device");
985out:
986 kfree(class_name);
987 return error;
988#else
989 return 0;
990#endif
991}
992
993/**
994 * device_move - moves a device to a new parent
995 * @dev: the pointer to the struct device to be moved
996 * @new_parent: the new parent of the device
997 */
998int device_move(struct device *dev, struct device *new_parent)
999{
1000 int error;
1001 struct device *old_parent;
1002
1003 dev = get_device(dev);
1004 if (!dev)
1005 return -EINVAL;
1006
1007 if (!device_is_registered(dev)) {
1008 error = -EINVAL;
1009 goto out;
1010 }
1011 new_parent = get_device(new_parent);
1012 if (!new_parent) {
1013 error = -EINVAL;
1014 goto out;
1015 }
1016 pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
1017 new_parent->bus_id);
1018 error = kobject_move(&dev->kobj, &new_parent->kobj);
1019 if (error) {
1020 put_device(new_parent);
1021 goto out;
1022 }
1023 old_parent = dev->parent;
1024 dev->parent = new_parent;
1025 if (old_parent)
Cornelia Huckacf02d22006-11-22 17:49:39 +01001026 klist_remove(&dev->knode_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +01001027 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
1028 if (!dev->class)
1029 goto out_put;
1030 error = device_move_class_links(dev, old_parent, new_parent);
1031 if (error) {
1032 /* We ignore errors on cleanup since we're hosed anyway... */
1033 device_move_class_links(dev, new_parent, old_parent);
1034 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
Cornelia Huckacf02d22006-11-22 17:49:39 +01001035 klist_remove(&dev->knode_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +01001036 if (old_parent)
1037 klist_add_tail(&dev->knode_parent,
1038 &old_parent->klist_children);
1039 }
1040 put_device(new_parent);
1041 goto out;
1042 }
1043out_put:
1044 put_device(old_parent);
1045out:
1046 put_device(dev);
1047 return error;
1048}
1049
1050EXPORT_SYMBOL_GPL(device_move);