/sys/modules/*/holders
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / base / core.c
CommitLineData
1da177e4
LT
1/*
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
64bb5d2c
GKH
6 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
1da177e4
LT
8 *
9 * This file is released under the GPLv2
10 *
11 */
12
1da177e4
LT
13#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>
23681e47 19#include <linux/kdev_t.h>
116af378 20#include <linux/notifier.h>
1da177e4
LT
21
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
3e95637a
AS
34/**
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}
310a922d 48EXPORT_SYMBOL(dev_driver_string);
3e95637a 49
1da177e4
LT
50#define to_dev(obj) container_of(obj, struct device, kobj)
51#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
52
1da177e4
LT
53static 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);
4a0c20bf 58 ssize_t ret = -EIO;
1da177e4
LT
59
60 if (dev_attr->show)
54b6f35c 61 ret = dev_attr->show(dev, dev_attr, buf);
1da177e4
LT
62 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);
4a0c20bf 71 ssize_t ret = -EIO;
1da177e4
LT
72
73 if (dev_attr->store)
54b6f35c 74 ret = dev_attr->store(dev, dev_attr, buf, count);
1da177e4
LT
75 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);
2620efef
GKH
98 else if (dev->class && dev->class->dev_release)
99 dev->class->dev_release(dev);
1da177e4
LT
100 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,
1da177e4
LT
111};
112
113
312c004d 114static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1da177e4
LT
115{
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;
23681e47
GKH
122 if (dev->class)
123 return 1;
1da177e4
LT
124 }
125 return 0;
126}
127
312c004d 128static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1da177e4
LT
129{
130 struct device *dev = to_dev(kobj);
131
23681e47
GKH
132 if (dev->bus)
133 return dev->bus->name;
134 if (dev->class)
135 return dev->class->name;
136 return NULL;
1da177e4
LT
137}
138
312c004d 139static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
1da177e4
LT
140 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
23681e47
GKH
147 /* 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
a87cb2ac 157#ifdef CONFIG_SYSFS_DEPRECATED
d81d9d6b 158 /* add bus name (same as SUBSYSTEM, deprecated) */
1da177e4 159 if (dev->bus)
312c004d
KS
160 add_uevent_var(envp, num_envp, &i,
161 buffer, buffer_size, &length,
162 "PHYSDEVBUS=%s", dev->bus->name);
a87cb2ac 163#endif
1da177e4 164
d81d9d6b
KS
165 /* 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);
a87cb2ac 170#ifdef CONFIG_SYSFS_DEPRECATED
312c004d
KS
171 add_uevent_var(envp, num_envp, &i,
172 buffer, buffer_size, &length,
173 "PHYSDEVDRIVER=%s", dev->driver->name);
a87cb2ac 174#endif
d81d9d6b 175 }
1da177e4
LT
176
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
312c004d 184 if (dev->bus && dev->bus->uevent) {
1da177e4 185 /* have the bus specific function add its stuff */
312c004d 186 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
1da177e4 187 if (retval) {
312c004d 188 pr_debug ("%s - uevent() returned %d\n",
1da177e4
LT
189 __FUNCTION__, retval);
190 }
191 }
192
2620efef
GKH
193 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
1da177e4
LT
202 return retval;
203}
204
312c004d
KS
205static struct kset_uevent_ops device_uevent_ops = {
206 .filter = dev_uevent_filter,
207 .name = dev_uevent_name,
208 .uevent = dev_uevent,
1da177e4
LT
209};
210
a7fd6706
KS
211static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
212 const char *buf, size_t count)
213{
312c004d 214 kobject_uevent(&dev->kobj, KOBJ_ADD);
a7fd6706
KS
215 return count;
216}
217
de0ff00d
GKH
218static 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
2620efef
GKH
247static 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
23681e47
GKH
284static 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
0863afb3
MW
290/*
291 * devices_subsys - structure to be registered with kobject core.
1da177e4
LT
292 */
293
312c004d 294decl_subsys(devices, &ktype_device, &device_uevent_ops);
1da177e4
LT
295
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
2589f188
GKH
327/**
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
34bb61f9
JB
353static 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
1da177e4
LT
367
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);
34bb61f9
JB
383 klist_init(&dev->klist_children, klist_children_get,
384 klist_children_put);
1da177e4 385 INIT_LIST_HEAD(&dev->dma_pools);
23681e47 386 INIT_LIST_HEAD(&dev->node);
af70316a 387 init_MUTEX(&dev->sem);
0ac85241 388 device_init_wakeup(dev, 0);
87348136 389 set_dev_node(dev, -1);
1da177e4
LT
390}
391
40fa5422 392#ifdef CONFIG_SYSFS_DEPRECATED
c744aeae
CH
393static struct kobject * get_device_parent(struct device *dev,
394 struct device *parent)
40fa5422
GKH
395{
396 /* Set the parent to the class, not the parent device */
397 /* this keeps sysfs from having a symlink to make old udevs happy */
398 if (dev->class)
c744aeae 399 return &dev->class->subsys.kset.kobj;
40fa5422 400 else if (parent)
c744aeae 401 return &parent->kobj;
40fa5422 402
c744aeae 403 return NULL;
40fa5422
GKH
404}
405#else
c744aeae 406static struct kobject * virtual_device_parent(struct device *dev)
f0ee61a6
GKH
407{
408 if (!dev->class)
c744aeae 409 return ERR_PTR(-ENODEV);
f0ee61a6
GKH
410
411 if (!dev->class->virtual_dir) {
412 static struct kobject *virtual_dir = NULL;
413
414 if (!virtual_dir)
415 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
416 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
417 }
418
c744aeae 419 return dev->class->virtual_dir;
f0ee61a6
GKH
420}
421
c744aeae
CH
422static struct kobject * get_device_parent(struct device *dev,
423 struct device *parent)
40fa5422 424{
40fa5422
GKH
425 /* if this is a class device, and has no parent, create one */
426 if ((dev->class) && (parent == NULL)) {
c744aeae 427 return virtual_device_parent(dev);
40fa5422 428 } else if (parent)
c744aeae
CH
429 return &parent->kobj;
430 return NULL;
431}
40fa5422 432
c744aeae
CH
433#endif
434static int setup_parent(struct device *dev, struct device *parent)
435{
436 struct kobject *kobj;
437 kobj = get_device_parent(dev, parent);
438 if (IS_ERR(kobj))
439 return PTR_ERR(kobj);
440 if (kobj)
441 dev->kobj.parent = kobj;
40fa5422
GKH
442 return 0;
443}
40fa5422 444
1da177e4
LT
445/**
446 * device_add - add device to device hierarchy.
447 * @dev: device.
448 *
449 * This is part 2 of device_register(), though may be called
450 * separately _iff_ device_initialize() has been called separately.
451 *
452 * This adds it to the kobject hierarchy via kobject_add(), adds it
453 * to the global and sibling lists for the device, then
454 * adds it to the other relevant subsystems of the driver model.
455 */
456int device_add(struct device *dev)
457{
458 struct device *parent = NULL;
e9a7d305 459 char *class_name = NULL;
c47ed219 460 struct class_interface *class_intf;
1da177e4
LT
461 int error = -EINVAL;
462
463 dev = get_device(dev);
464 if (!dev || !strlen(dev->bus_id))
465 goto Error;
466
40fa5422 467 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
c205ef48 468
1da177e4
LT
469 parent = get_device(dev->parent);
470
40fa5422
GKH
471 error = setup_parent(dev, parent);
472 if (error)
473 goto Error;
1da177e4
LT
474
475 /* first, register with generic layer. */
476 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
40fa5422
GKH
477 error = kobject_add(&dev->kobj);
478 if (error)
1da177e4 479 goto Error;
a7fd6706 480
37022644
BW
481 /* notify platform of device entry */
482 if (platform_notify)
483 platform_notify(dev);
484
116af378
BH
485 /* notify clients of device entry (new way) */
486 if (dev->bus)
487 blocking_notifier_call_chain(&dev->bus->bus_notifier,
488 BUS_NOTIFY_ADD_DEVICE, dev);
489
a7fd6706
KS
490 dev->uevent_attr.attr.name = "uevent";
491 dev->uevent_attr.attr.mode = S_IWUSR;
492 if (dev->driver)
493 dev->uevent_attr.attr.owner = dev->driver->owner;
494 dev->uevent_attr.store = store_uevent;
a306eea4
CH
495 error = device_create_file(dev, &dev->uevent_attr);
496 if (error)
497 goto attrError;
a7fd6706 498
23681e47
GKH
499 if (MAJOR(dev->devt)) {
500 struct device_attribute *attr;
501 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
502 if (!attr) {
503 error = -ENOMEM;
a306eea4 504 goto ueventattrError;
23681e47
GKH
505 }
506 attr->attr.name = "dev";
507 attr->attr.mode = S_IRUGO;
508 if (dev->driver)
509 attr->attr.owner = dev->driver->owner;
510 attr->show = show_dev;
511 error = device_create_file(dev, attr);
512 if (error) {
513 kfree(attr);
a306eea4 514 goto ueventattrError;
23681e47
GKH
515 }
516
517 dev->devt_attr = attr;
518 }
519
b9d9c82b
KS
520 if (dev->class) {
521 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
522 "subsystem");
40fa5422
GKH
523 /* If this is not a "fake" compatible device, then create the
524 * symlink from the class to the device. */
525 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
526 sysfs_create_link(&dev->class->subsys.kset.kobj,
527 &dev->kobj, dev->bus_id);
99ef3ef8 528#ifdef CONFIG_SYSFS_DEPRECATED
64bb5d2c
GKH
529 if (parent) {
530 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
531 class_name = make_class_name(dev->class->name, &dev->kobj);
532 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
533 }
99ef3ef8 534#endif
b9d9c82b 535 }
23681e47 536
2620efef
GKH
537 if ((error = device_add_attrs(dev)))
538 goto AttrsError;
de0ff00d
GKH
539 if ((error = device_add_groups(dev)))
540 goto GroupError;
1da177e4
LT
541 if ((error = device_pm_add(dev)))
542 goto PMError;
543 if ((error = bus_add_device(dev)))
544 goto BusError;
53877d06 545 kobject_uevent(&dev->kobj, KOBJ_ADD);
f70fa629
AS
546 if ((error = bus_attach_device(dev)))
547 goto AttachError;
1da177e4 548 if (parent)
d856f1e3 549 klist_add_tail(&dev->knode_parent, &parent->klist_children);
1da177e4 550
5d9fd169 551 if (dev->class) {
5d9fd169 552 down(&dev->class->sem);
c47ed219 553 /* tie the class to the device */
5d9fd169 554 list_add_tail(&dev->node, &dev->class->devices);
c47ed219
GKH
555
556 /* notify any interfaces that the device is here */
557 list_for_each_entry(class_intf, &dev->class->interfaces, node)
558 if (class_intf->add_dev)
559 class_intf->add_dev(dev, class_intf);
5d9fd169
GKH
560 up(&dev->class->sem);
561 }
1da177e4 562 Done:
e9a7d305 563 kfree(class_name);
1da177e4
LT
564 put_device(dev);
565 return error;
f70fa629
AS
566 AttachError:
567 bus_remove_device(dev);
1da177e4
LT
568 BusError:
569 device_pm_remove(dev);
570 PMError:
116af378
BH
571 if (dev->bus)
572 blocking_notifier_call_chain(&dev->bus->bus_notifier,
573 BUS_NOTIFY_DEL_DEVICE, dev);
de0ff00d
GKH
574 device_remove_groups(dev);
575 GroupError:
2620efef
GKH
576 device_remove_attrs(dev);
577 AttrsError:
23681e47
GKH
578 if (dev->devt_attr) {
579 device_remove_file(dev, dev->devt_attr);
580 kfree(dev->devt_attr);
581 }
a306eea4
CH
582 ueventattrError:
583 device_remove_file(dev, &dev->uevent_attr);
23681e47 584 attrError:
312c004d 585 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1da177e4
LT
586 kobject_del(&dev->kobj);
587 Error:
588 if (parent)
589 put_device(parent);
590 goto Done;
591}
592
593
594/**
595 * device_register - register a device with the system.
596 * @dev: pointer to the device structure
597 *
598 * This happens in two clean steps - initialize the device
599 * and add it to the system. The two steps can be called
600 * separately, but this is the easiest and most common.
601 * I.e. you should only call the two helpers separately if
602 * have a clearly defined need to use and refcount the device
603 * before it is added to the hierarchy.
604 */
605
606int device_register(struct device *dev)
607{
608 device_initialize(dev);
609 return device_add(dev);
610}
611
612
613/**
614 * get_device - increment reference count for device.
615 * @dev: device.
616 *
617 * This simply forwards the call to kobject_get(), though
618 * we do take care to provide for the case that we get a NULL
619 * pointer passed in.
620 */
621
622struct device * get_device(struct device * dev)
623{
624 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
625}
626
627
628/**
629 * put_device - decrement reference count.
630 * @dev: device in question.
631 */
632void put_device(struct device * dev)
633{
634 if (dev)
635 kobject_put(&dev->kobj);
636}
637
638
639/**
640 * device_del - delete device from system.
641 * @dev: device.
642 *
643 * This is the first part of the device unregistration
644 * sequence. This removes the device from the lists we control
645 * from here, has it removed from the other driver model
646 * subsystems it was added to in device_add(), and removes it
647 * from the kobject hierarchy.
648 *
649 * NOTE: this should be called manually _iff_ device_add() was
650 * also called manually.
651 */
652
653void device_del(struct device * dev)
654{
655 struct device * parent = dev->parent;
c47ed219 656 struct class_interface *class_intf;
1da177e4 657
1da177e4 658 if (parent)
d62c0f9f 659 klist_del(&dev->knode_parent);
82189b98 660 if (dev->devt_attr) {
23681e47 661 device_remove_file(dev, dev->devt_attr);
82189b98
CM
662 kfree(dev->devt_attr);
663 }
b9d9c82b
KS
664 if (dev->class) {
665 sysfs_remove_link(&dev->kobj, "subsystem");
40fa5422
GKH
666 /* If this is not a "fake" compatible device, remove the
667 * symlink from the class to the device. */
668 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
669 sysfs_remove_link(&dev->class->subsys.kset.kobj,
670 dev->bus_id);
99ef3ef8 671#ifdef CONFIG_SYSFS_DEPRECATED
64bb5d2c 672 if (parent) {
99ef3ef8
KS
673 char *class_name = make_class_name(dev->class->name,
674 &dev->kobj);
64bb5d2c 675 sysfs_remove_link(&dev->parent->kobj, class_name);
99ef3ef8
KS
676 kfree(class_name);
677 sysfs_remove_link(&dev->kobj, "device");
64bb5d2c 678 }
99ef3ef8
KS
679#endif
680
5d9fd169 681 down(&dev->class->sem);
c47ed219
GKH
682 /* notify any interfaces that the device is now gone */
683 list_for_each_entry(class_intf, &dev->class->interfaces, node)
684 if (class_intf->remove_dev)
685 class_intf->remove_dev(dev, class_intf);
686 /* remove the device from the class list */
5d9fd169
GKH
687 list_del_init(&dev->node);
688 up(&dev->class->sem);
b9d9c82b 689 }
a7fd6706 690 device_remove_file(dev, &dev->uevent_attr);
de0ff00d 691 device_remove_groups(dev);
2620efef 692 device_remove_attrs(dev);
28953533 693 bus_remove_device(dev);
1da177e4
LT
694
695 /* Notify the platform of the removal, in case they
696 * need to do anything...
697 */
698 if (platform_notify_remove)
699 platform_notify_remove(dev);
116af378
BH
700 if (dev->bus)
701 blocking_notifier_call_chain(&dev->bus->bus_notifier,
702 BUS_NOTIFY_DEL_DEVICE, dev);
1da177e4 703 device_pm_remove(dev);
312c004d 704 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1da177e4
LT
705 kobject_del(&dev->kobj);
706 if (parent)
707 put_device(parent);
708}
709
710/**
711 * device_unregister - unregister device from system.
712 * @dev: device going away.
713 *
714 * We do this in two parts, like we do device_register(). First,
715 * we remove it from all the subsystems with device_del(), then
716 * we decrement the reference count via put_device(). If that
717 * is the final reference count, the device will be cleaned up
718 * via device_release() above. Otherwise, the structure will
719 * stick around until the final reference to the device is dropped.
720 */
721void device_unregister(struct device * dev)
722{
723 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
724 device_del(dev);
725 put_device(dev);
726}
727
728
36239577
PM
729static struct device * next_device(struct klist_iter * i)
730{
731 struct klist_node * n = klist_next(i);
732 return n ? container_of(n, struct device, knode_parent) : NULL;
733}
734
1da177e4
LT
735/**
736 * device_for_each_child - device child iterator.
c41455fb 737 * @parent: parent struct device.
1da177e4
LT
738 * @data: data for the callback.
739 * @fn: function to be called for each device.
740 *
c41455fb 741 * Iterate over @parent's child devices, and call @fn for each,
1da177e4
LT
742 * passing it @data.
743 *
744 * We check the return of @fn each time. If it returns anything
745 * other than 0, we break out and return that value.
746 */
36239577 747int device_for_each_child(struct device * parent, void * data,
1da177e4
LT
748 int (*fn)(struct device *, void *))
749{
36239577 750 struct klist_iter i;
1da177e4
LT
751 struct device * child;
752 int error = 0;
753
36239577
PM
754 klist_iter_init(&parent->klist_children, &i);
755 while ((child = next_device(&i)) && !error)
756 error = fn(child, data);
757 klist_iter_exit(&i);
1da177e4
LT
758 return error;
759}
760
5ab69981
CH
761/**
762 * device_find_child - device iterator for locating a particular device.
763 * @parent: parent struct device
764 * @data: Data to pass to match function
765 * @match: Callback function to check device
766 *
767 * This is similar to the device_for_each_child() function above, but it
768 * returns a reference to a device that is 'found' for later use, as
769 * determined by the @match callback.
770 *
771 * The callback should return 0 if the device doesn't match and non-zero
772 * if it does. If the callback returns non-zero and a reference to the
773 * current device can be obtained, this function will return to the caller
774 * and not iterate over any more devices.
775 */
776struct device * device_find_child(struct device *parent, void *data,
777 int (*match)(struct device *, void *))
778{
779 struct klist_iter i;
780 struct device *child;
781
782 if (!parent)
783 return NULL;
784
785 klist_iter_init(&parent->klist_children, &i);
786 while ((child = next_device(&i)))
787 if (match(child, data) && get_device(child))
788 break;
789 klist_iter_exit(&i);
790 return child;
791}
792
1da177e4
LT
793int __init devices_init(void)
794{
795 return subsystem_register(&devices_subsys);
796}
797
798EXPORT_SYMBOL_GPL(device_for_each_child);
5ab69981 799EXPORT_SYMBOL_GPL(device_find_child);
1da177e4
LT
800
801EXPORT_SYMBOL_GPL(device_initialize);
802EXPORT_SYMBOL_GPL(device_add);
803EXPORT_SYMBOL_GPL(device_register);
804
805EXPORT_SYMBOL_GPL(device_del);
806EXPORT_SYMBOL_GPL(device_unregister);
807EXPORT_SYMBOL_GPL(get_device);
808EXPORT_SYMBOL_GPL(put_device);
1da177e4
LT
809
810EXPORT_SYMBOL_GPL(device_create_file);
811EXPORT_SYMBOL_GPL(device_remove_file);
23681e47
GKH
812
813
814static void device_create_release(struct device *dev)
815{
816 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
817 kfree(dev);
818}
819
820/**
821 * device_create - creates a device and registers it with sysfs
42734daf
HK
822 * @class: pointer to the struct class that this device should be registered to
823 * @parent: pointer to the parent struct device of this new device, if any
824 * @devt: the dev_t for the char device to be added
825 * @fmt: string for the device's name
826 *
827 * This function can be used by char device classes. A struct device
828 * will be created in sysfs, registered to the specified class.
23681e47 829 *
23681e47
GKH
830 * A "dev" file will be created, showing the dev_t for the device, if
831 * the dev_t is not 0,0.
42734daf
HK
832 * If a pointer to a parent struct device is passed in, the newly created
833 * struct device will be a child of that device in sysfs.
834 * The pointer to the struct device will be returned from the call.
835 * Any further sysfs files that might be required can be created using this
23681e47
GKH
836 * pointer.
837 *
838 * Note: the struct class passed to this function must have previously
839 * been created with a call to class_create().
840 */
841struct device *device_create(struct class *class, struct device *parent,
5cbe5f8a 842 dev_t devt, const char *fmt, ...)
23681e47
GKH
843{
844 va_list args;
845 struct device *dev = NULL;
846 int retval = -ENODEV;
847
848 if (class == NULL || IS_ERR(class))
849 goto error;
23681e47
GKH
850
851 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
852 if (!dev) {
853 retval = -ENOMEM;
854 goto error;
855 }
856
857 dev->devt = devt;
858 dev->class = class;
859 dev->parent = parent;
860 dev->release = device_create_release;
861
862 va_start(args, fmt);
863 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
864 va_end(args);
865 retval = device_register(dev);
866 if (retval)
867 goto error;
868
23681e47
GKH
869 return dev;
870
871error:
872 kfree(dev);
873 return ERR_PTR(retval);
874}
875EXPORT_SYMBOL_GPL(device_create);
876
877/**
878 * device_destroy - removes a device that was created with device_create()
42734daf
HK
879 * @class: pointer to the struct class that this device was registered with
880 * @devt: the dev_t of the device that was previously registered
23681e47 881 *
42734daf
HK
882 * This call unregisters and cleans up a device that was created with a
883 * call to device_create().
23681e47
GKH
884 */
885void device_destroy(struct class *class, dev_t devt)
886{
887 struct device *dev = NULL;
888 struct device *dev_tmp;
889
890 down(&class->sem);
891 list_for_each_entry(dev_tmp, &class->devices, node) {
892 if (dev_tmp->devt == devt) {
893 dev = dev_tmp;
894 break;
895 }
896 }
897 up(&class->sem);
898
5d9fd169 899 if (dev)
23681e47 900 device_unregister(dev);
23681e47
GKH
901}
902EXPORT_SYMBOL_GPL(device_destroy);
a2de48ca
GKH
903
904/**
905 * device_rename - renames a device
906 * @dev: the pointer to the struct device to be renamed
907 * @new_name: the new name of the device
908 */
909int device_rename(struct device *dev, char *new_name)
910{
911 char *old_class_name = NULL;
912 char *new_class_name = NULL;
913 char *old_symlink_name = NULL;
914 int error;
915
916 dev = get_device(dev);
917 if (!dev)
918 return -EINVAL;
919
920 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
921
99ef3ef8 922#ifdef CONFIG_SYSFS_DEPRECATED
a2de48ca
GKH
923 if ((dev->class) && (dev->parent))
924 old_class_name = make_class_name(dev->class->name, &dev->kobj);
99ef3ef8 925#endif
a2de48ca
GKH
926
927 if (dev->class) {
928 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
952ab431
JJ
929 if (!old_symlink_name) {
930 error = -ENOMEM;
931 goto out_free_old_class;
932 }
a2de48ca
GKH
933 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
934 }
935
936 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
937
938 error = kobject_rename(&dev->kobj, new_name);
939
99ef3ef8 940#ifdef CONFIG_SYSFS_DEPRECATED
a2de48ca
GKH
941 if (old_class_name) {
942 new_class_name = make_class_name(dev->class->name, &dev->kobj);
943 if (new_class_name) {
944 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
945 new_class_name);
946 sysfs_remove_link(&dev->parent->kobj, old_class_name);
947 }
948 }
99ef3ef8
KS
949#endif
950
a2de48ca
GKH
951 if (dev->class) {
952 sysfs_remove_link(&dev->class->subsys.kset.kobj,
953 old_symlink_name);
954 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
955 dev->bus_id);
956 }
957 put_device(dev);
958
a2de48ca
GKH
959 kfree(new_class_name);
960 kfree(old_symlink_name);
952ab431
JJ
961 out_free_old_class:
962 kfree(old_class_name);
a2de48ca
GKH
963
964 return error;
965}
8a82472f
CH
966
967
968static int device_move_class_links(struct device *dev,
969 struct device *old_parent,
970 struct device *new_parent)
971{
972#ifdef CONFIG_SYSFS_DEPRECATED
973 int error;
974 char *class_name;
975
976 class_name = make_class_name(dev->class->name, &dev->kobj);
977 if (!class_name) {
978 error = PTR_ERR(class_name);
979 class_name = NULL;
980 goto out;
981 }
982 if (old_parent) {
983 sysfs_remove_link(&dev->kobj, "device");
984 sysfs_remove_link(&old_parent->kobj, class_name);
985 }
c744aeae
CH
986 if (new_parent) {
987 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
988 "device");
989 if (error)
990 goto out;
991 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
992 class_name);
993 if (error)
994 sysfs_remove_link(&dev->kobj, "device");
995 }
996 else
997 error = 0;
8a82472f
CH
998out:
999 kfree(class_name);
1000 return error;
1001#else
1002 return 0;
1003#endif
1004}
1005
1006/**
1007 * device_move - moves a device to a new parent
1008 * @dev: the pointer to the struct device to be moved
c744aeae 1009 * @new_parent: the new parent of the device (can by NULL)
8a82472f
CH
1010 */
1011int device_move(struct device *dev, struct device *new_parent)
1012{
1013 int error;
1014 struct device *old_parent;
c744aeae 1015 struct kobject *new_parent_kobj;
8a82472f
CH
1016
1017 dev = get_device(dev);
1018 if (!dev)
1019 return -EINVAL;
1020
8a82472f 1021 new_parent = get_device(new_parent);
c744aeae
CH
1022 new_parent_kobj = get_device_parent (dev, new_parent);
1023 if (IS_ERR(new_parent_kobj)) {
1024 error = PTR_ERR(new_parent_kobj);
1025 put_device(new_parent);
8a82472f
CH
1026 goto out;
1027 }
1028 pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
c744aeae
CH
1029 new_parent ? new_parent->bus_id : "<NULL>");
1030 error = kobject_move(&dev->kobj, new_parent_kobj);
8a82472f
CH
1031 if (error) {
1032 put_device(new_parent);
1033 goto out;
1034 }
1035 old_parent = dev->parent;
1036 dev->parent = new_parent;
1037 if (old_parent)
acf02d23 1038 klist_remove(&dev->knode_parent);
c744aeae
CH
1039 if (new_parent)
1040 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
8a82472f
CH
1041 if (!dev->class)
1042 goto out_put;
1043 error = device_move_class_links(dev, old_parent, new_parent);
1044 if (error) {
1045 /* We ignore errors on cleanup since we're hosed anyway... */
1046 device_move_class_links(dev, new_parent, old_parent);
1047 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
c744aeae
CH
1048 if (new_parent)
1049 klist_remove(&dev->knode_parent);
8a82472f
CH
1050 if (old_parent)
1051 klist_add_tail(&dev->knode_parent,
1052 &old_parent->klist_children);
1053 }
1054 put_device(new_parent);
1055 goto out;
1056 }
1057out_put:
1058 put_device(old_parent);
1059out:
1060 put_device(dev);
1061 return error;
1062}
1063
1064EXPORT_SYMBOL_GPL(device_move);