4d59975c24a850c4d134a178211ae9ab520730f7
[safe/jmp/linux-2.6] / drivers / base / core.c
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
6  * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7  * Copyright (c) 2006 Novell, Inc.
8  *
9  * This file is released under the GPLv2
10  *
11  */
12
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>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
21 #include <linux/genhd.h>
22 #include <linux/kallsyms.h>
23 #include <linux/semaphore.h>
24 #include <linux/mutex.h>
25 #include <linux/async.h>
26
27 #include "base.h"
28 #include "power/power.h"
29
30 int (*platform_notify)(struct device *dev) = NULL;
31 int (*platform_notify_remove)(struct device *dev) = NULL;
32 static struct kobject *dev_kobj;
33 struct kobject *sysfs_dev_char_kobj;
34 struct kobject *sysfs_dev_block_kobj;
35
36 #ifdef CONFIG_BLOCK
37 static inline int device_is_not_partition(struct device *dev)
38 {
39         return !(dev->type == &part_type);
40 }
41 #else
42 static inline int device_is_not_partition(struct device *dev)
43 {
44         return 1;
45 }
46 #endif
47
48 /**
49  * dev_driver_string - Return a device's driver name, if at all possible
50  * @dev: struct device to get the name of
51  *
52  * Will return the device's driver's name if it is bound to a device.  If
53  * the device is not bound to a device, it will return the name of the bus
54  * it is attached to.  If it is not attached to a bus either, an empty
55  * string will be returned.
56  */
57 const char *dev_driver_string(const struct device *dev)
58 {
59         return dev->driver ? dev->driver->name :
60                         (dev->bus ? dev->bus->name :
61                         (dev->class ? dev->class->name : ""));
62 }
63 EXPORT_SYMBOL(dev_driver_string);
64
65 #define to_dev(obj) container_of(obj, struct device, kobj)
66 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
67
68 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
69                              char *buf)
70 {
71         struct device_attribute *dev_attr = to_dev_attr(attr);
72         struct device *dev = to_dev(kobj);
73         ssize_t ret = -EIO;
74
75         if (dev_attr->show)
76                 ret = dev_attr->show(dev, dev_attr, buf);
77         if (ret >= (ssize_t)PAGE_SIZE) {
78                 print_symbol("dev_attr_show: %s returned bad count\n",
79                                 (unsigned long)dev_attr->show);
80         }
81         return ret;
82 }
83
84 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
85                               const char *buf, size_t count)
86 {
87         struct device_attribute *dev_attr = to_dev_attr(attr);
88         struct device *dev = to_dev(kobj);
89         ssize_t ret = -EIO;
90
91         if (dev_attr->store)
92                 ret = dev_attr->store(dev, dev_attr, buf, count);
93         return ret;
94 }
95
96 static struct sysfs_ops dev_sysfs_ops = {
97         .show   = dev_attr_show,
98         .store  = dev_attr_store,
99 };
100
101
102 /**
103  *      device_release - free device structure.
104  *      @kobj:  device's kobject.
105  *
106  *      This is called once the reference count for the object
107  *      reaches 0. We forward the call to the device's release
108  *      method, which should handle actually freeing the structure.
109  */
110 static void device_release(struct kobject *kobj)
111 {
112         struct device *dev = to_dev(kobj);
113         struct device_private *p = dev->p;
114
115         if (dev->release)
116                 dev->release(dev);
117         else if (dev->type && dev->type->release)
118                 dev->type->release(dev);
119         else if (dev->class && dev->class->dev_release)
120                 dev->class->dev_release(dev);
121         else
122                 WARN(1, KERN_ERR "Device '%s' does not have a release() "
123                         "function, it is broken and must be fixed.\n",
124                         dev_name(dev));
125         kfree(p);
126 }
127
128 static struct kobj_type device_ktype = {
129         .release        = device_release,
130         .sysfs_ops      = &dev_sysfs_ops,
131 };
132
133
134 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
135 {
136         struct kobj_type *ktype = get_ktype(kobj);
137
138         if (ktype == &device_ktype) {
139                 struct device *dev = to_dev(kobj);
140                 if (dev->bus)
141                         return 1;
142                 if (dev->class)
143                         return 1;
144         }
145         return 0;
146 }
147
148 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
149 {
150         struct device *dev = to_dev(kobj);
151
152         if (dev->bus)
153                 return dev->bus->name;
154         if (dev->class)
155                 return dev->class->name;
156         return NULL;
157 }
158
159 static int dev_uevent(struct kset *kset, struct kobject *kobj,
160                       struct kobj_uevent_env *env)
161 {
162         struct device *dev = to_dev(kobj);
163         int retval = 0;
164
165         /* add the major/minor if present */
166         if (MAJOR(dev->devt)) {
167                 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
168                 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
169         }
170
171         if (dev->type && dev->type->name)
172                 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
173
174         if (dev->driver)
175                 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
176
177 #ifdef CONFIG_SYSFS_DEPRECATED
178         if (dev->class) {
179                 struct device *parent = dev->parent;
180
181                 /* find first bus device in parent chain */
182                 while (parent && !parent->bus)
183                         parent = parent->parent;
184                 if (parent && parent->bus) {
185                         const char *path;
186
187                         path = kobject_get_path(&parent->kobj, GFP_KERNEL);
188                         if (path) {
189                                 add_uevent_var(env, "PHYSDEVPATH=%s", path);
190                                 kfree(path);
191                         }
192
193                         add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
194
195                         if (parent->driver)
196                                 add_uevent_var(env, "PHYSDEVDRIVER=%s",
197                                                parent->driver->name);
198                 }
199         } else if (dev->bus) {
200                 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
201
202                 if (dev->driver)
203                         add_uevent_var(env, "PHYSDEVDRIVER=%s",
204                                        dev->driver->name);
205         }
206 #endif
207
208         /* have the bus specific function add its stuff */
209         if (dev->bus && dev->bus->uevent) {
210                 retval = dev->bus->uevent(dev, env);
211                 if (retval)
212                         pr_debug("device: '%s': %s: bus uevent() returned %d\n",
213                                  dev_name(dev), __func__, retval);
214         }
215
216         /* have the class specific function add its stuff */
217         if (dev->class && dev->class->dev_uevent) {
218                 retval = dev->class->dev_uevent(dev, env);
219                 if (retval)
220                         pr_debug("device: '%s': %s: class uevent() "
221                                  "returned %d\n", dev_name(dev),
222                                  __func__, retval);
223         }
224
225         /* have the device type specific fuction add its stuff */
226         if (dev->type && dev->type->uevent) {
227                 retval = dev->type->uevent(dev, env);
228                 if (retval)
229                         pr_debug("device: '%s': %s: dev_type uevent() "
230                                  "returned %d\n", dev_name(dev),
231                                  __func__, retval);
232         }
233
234         return retval;
235 }
236
237 static struct kset_uevent_ops device_uevent_ops = {
238         .filter =       dev_uevent_filter,
239         .name =         dev_uevent_name,
240         .uevent =       dev_uevent,
241 };
242
243 static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
244                            char *buf)
245 {
246         struct kobject *top_kobj;
247         struct kset *kset;
248         struct kobj_uevent_env *env = NULL;
249         int i;
250         size_t count = 0;
251         int retval;
252
253         /* search the kset, the device belongs to */
254         top_kobj = &dev->kobj;
255         while (!top_kobj->kset && top_kobj->parent)
256                 top_kobj = top_kobj->parent;
257         if (!top_kobj->kset)
258                 goto out;
259
260         kset = top_kobj->kset;
261         if (!kset->uevent_ops || !kset->uevent_ops->uevent)
262                 goto out;
263
264         /* respect filter */
265         if (kset->uevent_ops && kset->uevent_ops->filter)
266                 if (!kset->uevent_ops->filter(kset, &dev->kobj))
267                         goto out;
268
269         env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
270         if (!env)
271                 return -ENOMEM;
272
273         /* let the kset specific function add its keys */
274         retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
275         if (retval)
276                 goto out;
277
278         /* copy keys to file */
279         for (i = 0; i < env->envp_idx; i++)
280                 count += sprintf(&buf[count], "%s\n", env->envp[i]);
281 out:
282         kfree(env);
283         return count;
284 }
285
286 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
287                             const char *buf, size_t count)
288 {
289         enum kobject_action action;
290
291         if (kobject_action_type(buf, count, &action) == 0) {
292                 kobject_uevent(&dev->kobj, action);
293                 goto out;
294         }
295
296         dev_err(dev, "uevent: unsupported action-string; this will "
297                      "be ignored in a future kernel version\n");
298         kobject_uevent(&dev->kobj, KOBJ_ADD);
299 out:
300         return count;
301 }
302
303 static struct device_attribute uevent_attr =
304         __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
305
306 static int device_add_attributes(struct device *dev,
307                                  struct device_attribute *attrs)
308 {
309         int error = 0;
310         int i;
311
312         if (attrs) {
313                 for (i = 0; attr_name(attrs[i]); i++) {
314                         error = device_create_file(dev, &attrs[i]);
315                         if (error)
316                                 break;
317                 }
318                 if (error)
319                         while (--i >= 0)
320                                 device_remove_file(dev, &attrs[i]);
321         }
322         return error;
323 }
324
325 static void device_remove_attributes(struct device *dev,
326                                      struct device_attribute *attrs)
327 {
328         int i;
329
330         if (attrs)
331                 for (i = 0; attr_name(attrs[i]); i++)
332                         device_remove_file(dev, &attrs[i]);
333 }
334
335 static int device_add_groups(struct device *dev,
336                              struct attribute_group **groups)
337 {
338         int error = 0;
339         int i;
340
341         if (groups) {
342                 for (i = 0; groups[i]; i++) {
343                         error = sysfs_create_group(&dev->kobj, groups[i]);
344                         if (error) {
345                                 while (--i >= 0)
346                                         sysfs_remove_group(&dev->kobj,
347                                                            groups[i]);
348                                 break;
349                         }
350                 }
351         }
352         return error;
353 }
354
355 static void device_remove_groups(struct device *dev,
356                                  struct attribute_group **groups)
357 {
358         int i;
359
360         if (groups)
361                 for (i = 0; groups[i]; i++)
362                         sysfs_remove_group(&dev->kobj, groups[i]);
363 }
364
365 static int device_add_attrs(struct device *dev)
366 {
367         struct class *class = dev->class;
368         struct device_type *type = dev->type;
369         int error;
370
371         if (class) {
372                 error = device_add_attributes(dev, class->dev_attrs);
373                 if (error)
374                         return error;
375         }
376
377         if (type) {
378                 error = device_add_groups(dev, type->groups);
379                 if (error)
380                         goto err_remove_class_attrs;
381         }
382
383         error = device_add_groups(dev, dev->groups);
384         if (error)
385                 goto err_remove_type_groups;
386
387         return 0;
388
389  err_remove_type_groups:
390         if (type)
391                 device_remove_groups(dev, type->groups);
392  err_remove_class_attrs:
393         if (class)
394                 device_remove_attributes(dev, class->dev_attrs);
395
396         return error;
397 }
398
399 static void device_remove_attrs(struct device *dev)
400 {
401         struct class *class = dev->class;
402         struct device_type *type = dev->type;
403
404         device_remove_groups(dev, dev->groups);
405
406         if (type)
407                 device_remove_groups(dev, type->groups);
408
409         if (class)
410                 device_remove_attributes(dev, class->dev_attrs);
411 }
412
413
414 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
415                         char *buf)
416 {
417         return print_dev_t(buf, dev->devt);
418 }
419
420 static struct device_attribute devt_attr =
421         __ATTR(dev, S_IRUGO, show_dev, NULL);
422
423 /* kset to create /sys/devices/  */
424 struct kset *devices_kset;
425
426 /**
427  * device_create_file - create sysfs attribute file for device.
428  * @dev: device.
429  * @attr: device attribute descriptor.
430  */
431 int device_create_file(struct device *dev, struct device_attribute *attr)
432 {
433         int error = 0;
434         if (dev)
435                 error = sysfs_create_file(&dev->kobj, &attr->attr);
436         return error;
437 }
438
439 /**
440  * device_remove_file - remove sysfs attribute file.
441  * @dev: device.
442  * @attr: device attribute descriptor.
443  */
444 void device_remove_file(struct device *dev, struct device_attribute *attr)
445 {
446         if (dev)
447                 sysfs_remove_file(&dev->kobj, &attr->attr);
448 }
449
450 /**
451  * device_create_bin_file - create sysfs binary attribute file for device.
452  * @dev: device.
453  * @attr: device binary attribute descriptor.
454  */
455 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
456 {
457         int error = -EINVAL;
458         if (dev)
459                 error = sysfs_create_bin_file(&dev->kobj, attr);
460         return error;
461 }
462 EXPORT_SYMBOL_GPL(device_create_bin_file);
463
464 /**
465  * device_remove_bin_file - remove sysfs binary attribute file
466  * @dev: device.
467  * @attr: device binary attribute descriptor.
468  */
469 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
470 {
471         if (dev)
472                 sysfs_remove_bin_file(&dev->kobj, attr);
473 }
474 EXPORT_SYMBOL_GPL(device_remove_bin_file);
475
476 /**
477  * device_schedule_callback_owner - helper to schedule a callback for a device
478  * @dev: device.
479  * @func: callback function to invoke later.
480  * @owner: module owning the callback routine
481  *
482  * Attribute methods must not unregister themselves or their parent device
483  * (which would amount to the same thing).  Attempts to do so will deadlock,
484  * since unregistration is mutually exclusive with driver callbacks.
485  *
486  * Instead methods can call this routine, which will attempt to allocate
487  * and schedule a workqueue request to call back @func with @dev as its
488  * argument in the workqueue's process context.  @dev will be pinned until
489  * @func returns.
490  *
491  * This routine is usually called via the inline device_schedule_callback(),
492  * which automatically sets @owner to THIS_MODULE.
493  *
494  * Returns 0 if the request was submitted, -ENOMEM if storage could not
495  * be allocated, -ENODEV if a reference to @owner isn't available.
496  *
497  * NOTE: This routine won't work if CONFIG_SYSFS isn't set!  It uses an
498  * underlying sysfs routine (since it is intended for use by attribute
499  * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
500  */
501 int device_schedule_callback_owner(struct device *dev,
502                 void (*func)(struct device *), struct module *owner)
503 {
504         return sysfs_schedule_callback(&dev->kobj,
505                         (void (*)(void *)) func, dev, owner);
506 }
507 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
508
509 static void klist_children_get(struct klist_node *n)
510 {
511         struct device_private *p = to_device_private_parent(n);
512         struct device *dev = p->device;
513
514         get_device(dev);
515 }
516
517 static void klist_children_put(struct klist_node *n)
518 {
519         struct device_private *p = to_device_private_parent(n);
520         struct device *dev = p->device;
521
522         put_device(dev);
523 }
524
525 /**
526  * device_initialize - init device structure.
527  * @dev: device.
528  *
529  * This prepares the device for use by other layers by initializing
530  * its fields.
531  * It is the first half of device_register(), if called by
532  * that function, though it can also be called separately, so one
533  * may use @dev's fields. In particular, get_device()/put_device()
534  * may be used for reference counting of @dev after calling this
535  * function.
536  *
537  * NOTE: Use put_device() to give up your reference instead of freeing
538  * @dev directly once you have called this function.
539  */
540 void device_initialize(struct device *dev)
541 {
542         dev->kobj.kset = devices_kset;
543         kobject_init(&dev->kobj, &device_ktype);
544         INIT_LIST_HEAD(&dev->dma_pools);
545         init_MUTEX(&dev->sem);
546         spin_lock_init(&dev->devres_lock);
547         INIT_LIST_HEAD(&dev->devres_head);
548         device_init_wakeup(dev, 0);
549         device_pm_init(dev);
550         set_dev_node(dev, -1);
551 }
552
553 #ifdef CONFIG_SYSFS_DEPRECATED
554 static struct kobject *get_device_parent(struct device *dev,
555                                          struct device *parent)
556 {
557         /* class devices without a parent live in /sys/class/<classname>/ */
558         if (dev->class && (!parent || parent->class != dev->class))
559                 return &dev->class->p->class_subsys.kobj;
560         /* all other devices keep their parent */
561         else if (parent)
562                 return &parent->kobj;
563
564         return NULL;
565 }
566
567 static inline void cleanup_device_parent(struct device *dev) {}
568 static inline void cleanup_glue_dir(struct device *dev,
569                                     struct kobject *glue_dir) {}
570 #else
571 static struct kobject *virtual_device_parent(struct device *dev)
572 {
573         static struct kobject *virtual_dir = NULL;
574
575         if (!virtual_dir)
576                 virtual_dir = kobject_create_and_add("virtual",
577                                                      &devices_kset->kobj);
578
579         return virtual_dir;
580 }
581
582 static struct kobject *get_device_parent(struct device *dev,
583                                          struct device *parent)
584 {
585         int retval;
586
587         if (dev->class) {
588                 struct kobject *kobj = NULL;
589                 struct kobject *parent_kobj;
590                 struct kobject *k;
591
592                 /*
593                  * If we have no parent, we live in "virtual".
594                  * Class-devices with a non class-device as parent, live
595                  * in a "glue" directory to prevent namespace collisions.
596                  */
597                 if (parent == NULL)
598                         parent_kobj = virtual_device_parent(dev);
599                 else if (parent->class)
600                         return &parent->kobj;
601                 else
602                         parent_kobj = &parent->kobj;
603
604                 /* find our class-directory at the parent and reference it */
605                 spin_lock(&dev->class->p->class_dirs.list_lock);
606                 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
607                         if (k->parent == parent_kobj) {
608                                 kobj = kobject_get(k);
609                                 break;
610                         }
611                 spin_unlock(&dev->class->p->class_dirs.list_lock);
612                 if (kobj)
613                         return kobj;
614
615                 /* or create a new class-directory at the parent device */
616                 k = kobject_create();
617                 if (!k)
618                         return NULL;
619                 k->kset = &dev->class->p->class_dirs;
620                 retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
621                 if (retval < 0) {
622                         kobject_put(k);
623                         return NULL;
624                 }
625                 /* do not emit an uevent for this simple "glue" directory */
626                 return k;
627         }
628
629         if (parent)
630                 return &parent->kobj;
631         return NULL;
632 }
633
634 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
635 {
636         /* see if we live in a "glue" directory */
637         if (!glue_dir || !dev->class ||
638             glue_dir->kset != &dev->class->p->class_dirs)
639                 return;
640
641         kobject_put(glue_dir);
642 }
643
644 static void cleanup_device_parent(struct device *dev)
645 {
646         cleanup_glue_dir(dev, dev->kobj.parent);
647 }
648 #endif
649
650 static void setup_parent(struct device *dev, struct device *parent)
651 {
652         struct kobject *kobj;
653         kobj = get_device_parent(dev, parent);
654         if (kobj)
655                 dev->kobj.parent = kobj;
656 }
657
658 static int device_add_class_symlinks(struct device *dev)
659 {
660         int error;
661
662         if (!dev->class)
663                 return 0;
664
665         error = sysfs_create_link(&dev->kobj,
666                                   &dev->class->p->class_subsys.kobj,
667                                   "subsystem");
668         if (error)
669                 goto out;
670
671 #ifdef CONFIG_SYSFS_DEPRECATED
672         /* stacked class devices need a symlink in the class directory */
673         if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
674             device_is_not_partition(dev)) {
675                 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
676                                           &dev->kobj, dev_name(dev));
677                 if (error)
678                         goto out_subsys;
679         }
680
681         if (dev->parent && device_is_not_partition(dev)) {
682                 struct device *parent = dev->parent;
683                 char *class_name;
684
685                 /*
686                  * stacked class devices have the 'device' link
687                  * pointing to the bus device instead of the parent
688                  */
689                 while (parent->class && !parent->bus && parent->parent)
690                         parent = parent->parent;
691
692                 error = sysfs_create_link(&dev->kobj,
693                                           &parent->kobj,
694                                           "device");
695                 if (error)
696                         goto out_busid;
697
698                 class_name = make_class_name(dev->class->name,
699                                                 &dev->kobj);
700                 if (class_name)
701                         error = sysfs_create_link(&dev->parent->kobj,
702                                                 &dev->kobj, class_name);
703                 kfree(class_name);
704                 if (error)
705                         goto out_device;
706         }
707         return 0;
708
709 out_device:
710         if (dev->parent && device_is_not_partition(dev))
711                 sysfs_remove_link(&dev->kobj, "device");
712 out_busid:
713         if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
714             device_is_not_partition(dev))
715                 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
716                                   dev_name(dev));
717 #else
718         /* link in the class directory pointing to the device */
719         error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
720                                   &dev->kobj, dev_name(dev));
721         if (error)
722                 goto out_subsys;
723
724         if (dev->parent && device_is_not_partition(dev)) {
725                 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
726                                           "device");
727                 if (error)
728                         goto out_busid;
729         }
730         return 0;
731
732 out_busid:
733         sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
734 #endif
735
736 out_subsys:
737         sysfs_remove_link(&dev->kobj, "subsystem");
738 out:
739         return error;
740 }
741
742 static void device_remove_class_symlinks(struct device *dev)
743 {
744         if (!dev->class)
745                 return;
746
747 #ifdef CONFIG_SYSFS_DEPRECATED
748         if (dev->parent && device_is_not_partition(dev)) {
749                 char *class_name;
750
751                 class_name = make_class_name(dev->class->name, &dev->kobj);
752                 if (class_name) {
753                         sysfs_remove_link(&dev->parent->kobj, class_name);
754                         kfree(class_name);
755                 }
756                 sysfs_remove_link(&dev->kobj, "device");
757         }
758
759         if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
760             device_is_not_partition(dev))
761                 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
762                                   dev_name(dev));
763 #else
764         if (dev->parent && device_is_not_partition(dev))
765                 sysfs_remove_link(&dev->kobj, "device");
766
767         sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
768 #endif
769
770         sysfs_remove_link(&dev->kobj, "subsystem");
771 }
772
773 /**
774  * dev_set_name - set a device name
775  * @dev: device
776  * @fmt: format string for the device's name
777  */
778 int dev_set_name(struct device *dev, const char *fmt, ...)
779 {
780         va_list vargs;
781         int err;
782
783         va_start(vargs, fmt);
784         err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
785         va_end(vargs);
786         return err;
787 }
788 EXPORT_SYMBOL_GPL(dev_set_name);
789
790 /**
791  * device_to_dev_kobj - select a /sys/dev/ directory for the device
792  * @dev: device
793  *
794  * By default we select char/ for new entries.  Setting class->dev_obj
795  * to NULL prevents an entry from being created.  class->dev_kobj must
796  * be set (or cleared) before any devices are registered to the class
797  * otherwise device_create_sys_dev_entry() and
798  * device_remove_sys_dev_entry() will disagree about the the presence
799  * of the link.
800  */
801 static struct kobject *device_to_dev_kobj(struct device *dev)
802 {
803         struct kobject *kobj;
804
805         if (dev->class)
806                 kobj = dev->class->dev_kobj;
807         else
808                 kobj = sysfs_dev_char_kobj;
809
810         return kobj;
811 }
812
813 static int device_create_sys_dev_entry(struct device *dev)
814 {
815         struct kobject *kobj = device_to_dev_kobj(dev);
816         int error = 0;
817         char devt_str[15];
818
819         if (kobj) {
820                 format_dev_t(devt_str, dev->devt);
821                 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
822         }
823
824         return error;
825 }
826
827 static void device_remove_sys_dev_entry(struct device *dev)
828 {
829         struct kobject *kobj = device_to_dev_kobj(dev);
830         char devt_str[15];
831
832         if (kobj) {
833                 format_dev_t(devt_str, dev->devt);
834                 sysfs_remove_link(kobj, devt_str);
835         }
836 }
837
838 /**
839  * device_add - add device to device hierarchy.
840  * @dev: device.
841  *
842  * This is part 2 of device_register(), though may be called
843  * separately _iff_ device_initialize() has been called separately.
844  *
845  * This adds @dev to the kobject hierarchy via kobject_add(), adds it
846  * to the global and sibling lists for the device, then
847  * adds it to the other relevant subsystems of the driver model.
848  *
849  * NOTE: _Never_ directly free @dev after calling this function, even
850  * if it returned an error! Always use put_device() to give up your
851  * reference instead.
852  */
853 int device_add(struct device *dev)
854 {
855         struct device *parent = NULL;
856         struct class_interface *class_intf;
857         int error = -EINVAL;
858
859         dev = get_device(dev);
860         if (!dev)
861                 goto done;
862
863         dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
864         if (!dev->p) {
865                 error = -ENOMEM;
866                 goto done;
867         }
868         dev->p->device = dev;
869         klist_init(&dev->p->klist_children, klist_children_get,
870                    klist_children_put);
871
872         /*
873          * for statically allocated devices, which should all be converted
874          * some day, we need to initialize the name. We prevent reading back
875          * the name, and force the use of dev_name()
876          */
877         if (dev->init_name) {
878                 dev_set_name(dev, "%s", dev->init_name);
879                 dev->init_name = NULL;
880         }
881
882         if (!dev_name(dev))
883                 goto name_error;
884
885         pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
886
887         parent = get_device(dev->parent);
888         setup_parent(dev, parent);
889
890         /* use parent numa_node */
891         if (parent)
892                 set_dev_node(dev, dev_to_node(parent));
893
894         /* first, register with generic layer. */
895         /* we require the name to be set before, and pass NULL */
896         error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
897         if (error)
898                 goto Error;
899
900         /* notify platform of device entry */
901         if (platform_notify)
902                 platform_notify(dev);
903
904         error = device_create_file(dev, &uevent_attr);
905         if (error)
906                 goto attrError;
907
908         if (MAJOR(dev->devt)) {
909                 error = device_create_file(dev, &devt_attr);
910                 if (error)
911                         goto ueventattrError;
912
913                 error = device_create_sys_dev_entry(dev);
914                 if (error)
915                         goto devtattrError;
916         }
917
918         error = device_add_class_symlinks(dev);
919         if (error)
920                 goto SymlinkError;
921         error = device_add_attrs(dev);
922         if (error)
923                 goto AttrsError;
924         error = bus_add_device(dev);
925         if (error)
926                 goto BusError;
927         error = dpm_sysfs_add(dev);
928         if (error)
929                 goto DPMError;
930         device_pm_add(dev);
931
932         /* Notify clients of device addition.  This call must come
933          * after dpm_sysf_add() and before kobject_uevent().
934          */
935         if (dev->bus)
936                 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
937                                              BUS_NOTIFY_ADD_DEVICE, dev);
938
939         kobject_uevent(&dev->kobj, KOBJ_ADD);
940         bus_attach_device(dev);
941         if (parent)
942                 klist_add_tail(&dev->p->knode_parent,
943                                &parent->p->klist_children);
944
945         if (dev->class) {
946                 mutex_lock(&dev->class->p->class_mutex);
947                 /* tie the class to the device */
948                 klist_add_tail(&dev->knode_class,
949                                &dev->class->p->class_devices);
950
951                 /* notify any interfaces that the device is here */
952                 list_for_each_entry(class_intf,
953                                     &dev->class->p->class_interfaces, node)
954                         if (class_intf->add_dev)
955                                 class_intf->add_dev(dev, class_intf);
956                 mutex_unlock(&dev->class->p->class_mutex);
957         }
958 done:
959         put_device(dev);
960         return error;
961  DPMError:
962         bus_remove_device(dev);
963  BusError:
964         device_remove_attrs(dev);
965  AttrsError:
966         device_remove_class_symlinks(dev);
967  SymlinkError:
968         if (MAJOR(dev->devt))
969                 device_remove_sys_dev_entry(dev);
970  devtattrError:
971         if (MAJOR(dev->devt))
972                 device_remove_file(dev, &devt_attr);
973  ueventattrError:
974         device_remove_file(dev, &uevent_attr);
975  attrError:
976         kobject_uevent(&dev->kobj, KOBJ_REMOVE);
977         kobject_del(&dev->kobj);
978  Error:
979         cleanup_device_parent(dev);
980         if (parent)
981                 put_device(parent);
982 name_error:
983         kfree(dev->p);
984         dev->p = NULL;
985         goto done;
986 }
987
988 /**
989  * device_register - register a device with the system.
990  * @dev: pointer to the device structure
991  *
992  * This happens in two clean steps - initialize the device
993  * and add it to the system. The two steps can be called
994  * separately, but this is the easiest and most common.
995  * I.e. you should only call the two helpers separately if
996  * have a clearly defined need to use and refcount the device
997  * before it is added to the hierarchy.
998  *
999  * NOTE: _Never_ directly free @dev after calling this function, even
1000  * if it returned an error! Always use put_device() to give up the
1001  * reference initialized in this function instead.
1002  */
1003 int device_register(struct device *dev)
1004 {
1005         device_initialize(dev);
1006         return device_add(dev);
1007 }
1008
1009 /**
1010  * get_device - increment reference count for device.
1011  * @dev: device.
1012  *
1013  * This simply forwards the call to kobject_get(), though
1014  * we do take care to provide for the case that we get a NULL
1015  * pointer passed in.
1016  */
1017 struct device *get_device(struct device *dev)
1018 {
1019         return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1020 }
1021
1022 /**
1023  * put_device - decrement reference count.
1024  * @dev: device in question.
1025  */
1026 void put_device(struct device *dev)
1027 {
1028         /* might_sleep(); */
1029         if (dev)
1030                 kobject_put(&dev->kobj);
1031 }
1032
1033 /**
1034  * device_del - delete device from system.
1035  * @dev: device.
1036  *
1037  * This is the first part of the device unregistration
1038  * sequence. This removes the device from the lists we control
1039  * from here, has it removed from the other driver model
1040  * subsystems it was added to in device_add(), and removes it
1041  * from the kobject hierarchy.
1042  *
1043  * NOTE: this should be called manually _iff_ device_add() was
1044  * also called manually.
1045  */
1046 void device_del(struct device *dev)
1047 {
1048         struct device *parent = dev->parent;
1049         struct class_interface *class_intf;
1050
1051         /* Notify clients of device removal.  This call must come
1052          * before dpm_sysfs_remove().
1053          */
1054         if (dev->bus)
1055                 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1056                                              BUS_NOTIFY_DEL_DEVICE, dev);
1057         device_pm_remove(dev);
1058         dpm_sysfs_remove(dev);
1059         if (parent)
1060                 klist_del(&dev->p->knode_parent);
1061         if (MAJOR(dev->devt)) {
1062                 device_remove_sys_dev_entry(dev);
1063                 device_remove_file(dev, &devt_attr);
1064         }
1065         if (dev->class) {
1066                 device_remove_class_symlinks(dev);
1067
1068                 mutex_lock(&dev->class->p->class_mutex);
1069                 /* notify any interfaces that the device is now gone */
1070                 list_for_each_entry(class_intf,
1071                                     &dev->class->p->class_interfaces, node)
1072                         if (class_intf->remove_dev)
1073                                 class_intf->remove_dev(dev, class_intf);
1074                 /* remove the device from the class list */
1075                 klist_del(&dev->knode_class);
1076                 mutex_unlock(&dev->class->p->class_mutex);
1077         }
1078         device_remove_file(dev, &uevent_attr);
1079         device_remove_attrs(dev);
1080         bus_remove_device(dev);
1081
1082         /*
1083          * Some platform devices are driven without driver attached
1084          * and managed resources may have been acquired.  Make sure
1085          * all resources are released.
1086          */
1087         devres_release_all(dev);
1088
1089         /* Notify the platform of the removal, in case they
1090          * need to do anything...
1091          */
1092         if (platform_notify_remove)
1093                 platform_notify_remove(dev);
1094         kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1095         cleanup_device_parent(dev);
1096         kobject_del(&dev->kobj);
1097         put_device(parent);
1098 }
1099
1100 /**
1101  * device_unregister - unregister device from system.
1102  * @dev: device going away.
1103  *
1104  * We do this in two parts, like we do device_register(). First,
1105  * we remove it from all the subsystems with device_del(), then
1106  * we decrement the reference count via put_device(). If that
1107  * is the final reference count, the device will be cleaned up
1108  * via device_release() above. Otherwise, the structure will
1109  * stick around until the final reference to the device is dropped.
1110  */
1111 void device_unregister(struct device *dev)
1112 {
1113         pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1114         device_del(dev);
1115         put_device(dev);
1116 }
1117
1118 static struct device *next_device(struct klist_iter *i)
1119 {
1120         struct klist_node *n = klist_next(i);
1121         struct device *dev = NULL;
1122         struct device_private *p;
1123
1124         if (n) {
1125                 p = to_device_private_parent(n);
1126                 dev = p->device;
1127         }
1128         return dev;
1129 }
1130
1131 /**
1132  * device_for_each_child - device child iterator.
1133  * @parent: parent struct device.
1134  * @data: data for the callback.
1135  * @fn: function to be called for each device.
1136  *
1137  * Iterate over @parent's child devices, and call @fn for each,
1138  * passing it @data.
1139  *
1140  * We check the return of @fn each time. If it returns anything
1141  * other than 0, we break out and return that value.
1142  */
1143 int device_for_each_child(struct device *parent, void *data,
1144                           int (*fn)(struct device *dev, void *data))
1145 {
1146         struct klist_iter i;
1147         struct device *child;
1148         int error = 0;
1149
1150         if (!parent->p)
1151                 return 0;
1152
1153         klist_iter_init(&parent->p->klist_children, &i);
1154         while ((child = next_device(&i)) && !error)
1155                 error = fn(child, data);
1156         klist_iter_exit(&i);
1157         return error;
1158 }
1159
1160 /**
1161  * device_find_child - device iterator for locating a particular device.
1162  * @parent: parent struct device
1163  * @data: Data to pass to match function
1164  * @match: Callback function to check device
1165  *
1166  * This is similar to the device_for_each_child() function above, but it
1167  * returns a reference to a device that is 'found' for later use, as
1168  * determined by the @match callback.
1169  *
1170  * The callback should return 0 if the device doesn't match and non-zero
1171  * if it does.  If the callback returns non-zero and a reference to the
1172  * current device can be obtained, this function will return to the caller
1173  * and not iterate over any more devices.
1174  */
1175 struct device *device_find_child(struct device *parent, void *data,
1176                                  int (*match)(struct device *dev, void *data))
1177 {
1178         struct klist_iter i;
1179         struct device *child;
1180
1181         if (!parent)
1182                 return NULL;
1183
1184         klist_iter_init(&parent->p->klist_children, &i);
1185         while ((child = next_device(&i)))
1186                 if (match(child, data) && get_device(child))
1187                         break;
1188         klist_iter_exit(&i);
1189         return child;
1190 }
1191
1192 int __init devices_init(void)
1193 {
1194         devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1195         if (!devices_kset)
1196                 return -ENOMEM;
1197         dev_kobj = kobject_create_and_add("dev", NULL);
1198         if (!dev_kobj)
1199                 goto dev_kobj_err;
1200         sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1201         if (!sysfs_dev_block_kobj)
1202                 goto block_kobj_err;
1203         sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1204         if (!sysfs_dev_char_kobj)
1205                 goto char_kobj_err;
1206
1207         return 0;
1208
1209  char_kobj_err:
1210         kobject_put(sysfs_dev_block_kobj);
1211  block_kobj_err:
1212         kobject_put(dev_kobj);
1213  dev_kobj_err:
1214         kset_unregister(devices_kset);
1215         return -ENOMEM;
1216 }
1217
1218 EXPORT_SYMBOL_GPL(device_for_each_child);
1219 EXPORT_SYMBOL_GPL(device_find_child);
1220
1221 EXPORT_SYMBOL_GPL(device_initialize);
1222 EXPORT_SYMBOL_GPL(device_add);
1223 EXPORT_SYMBOL_GPL(device_register);
1224
1225 EXPORT_SYMBOL_GPL(device_del);
1226 EXPORT_SYMBOL_GPL(device_unregister);
1227 EXPORT_SYMBOL_GPL(get_device);
1228 EXPORT_SYMBOL_GPL(put_device);
1229
1230 EXPORT_SYMBOL_GPL(device_create_file);
1231 EXPORT_SYMBOL_GPL(device_remove_file);
1232
1233 struct root_device
1234 {
1235         struct device dev;
1236         struct module *owner;
1237 };
1238
1239 #define to_root_device(dev) container_of(dev, struct root_device, dev)
1240
1241 static void root_device_release(struct device *dev)
1242 {
1243         kfree(to_root_device(dev));
1244 }
1245
1246 /**
1247  * __root_device_register - allocate and register a root device
1248  * @name: root device name
1249  * @owner: owner module of the root device, usually THIS_MODULE
1250  *
1251  * This function allocates a root device and registers it
1252  * using device_register(). In order to free the returned
1253  * device, use root_device_unregister().
1254  *
1255  * Root devices are dummy devices which allow other devices
1256  * to be grouped under /sys/devices. Use this function to
1257  * allocate a root device and then use it as the parent of
1258  * any device which should appear under /sys/devices/{name}
1259  *
1260  * The /sys/devices/{name} directory will also contain a
1261  * 'module' symlink which points to the @owner directory
1262  * in sysfs.
1263  *
1264  * Note: You probably want to use root_device_register().
1265  */
1266 struct device *__root_device_register(const char *name, struct module *owner)
1267 {
1268         struct root_device *root;
1269         int err = -ENOMEM;
1270
1271         root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1272         if (!root)
1273                 return ERR_PTR(err);
1274
1275         err = dev_set_name(&root->dev, "%s", name);
1276         if (err) {
1277                 kfree(root);
1278                 return ERR_PTR(err);
1279         }
1280
1281         root->dev.release = root_device_release;
1282
1283         err = device_register(&root->dev);
1284         if (err) {
1285                 put_device(&root->dev);
1286                 return ERR_PTR(err);
1287         }
1288
1289 #ifdef CONFIG_MODULE    /* gotta find a "cleaner" way to do this */
1290         if (owner) {
1291                 struct module_kobject *mk = &owner->mkobj;
1292
1293                 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1294                 if (err) {
1295                         device_unregister(&root->dev);
1296                         return ERR_PTR(err);
1297                 }
1298                 root->owner = owner;
1299         }
1300 #endif
1301
1302         return &root->dev;
1303 }
1304 EXPORT_SYMBOL_GPL(__root_device_register);
1305
1306 /**
1307  * root_device_unregister - unregister and free a root device
1308  * @dev: device going away
1309  *
1310  * This function unregisters and cleans up a device that was created by
1311  * root_device_register().
1312  */
1313 void root_device_unregister(struct device *dev)
1314 {
1315         struct root_device *root = to_root_device(dev);
1316
1317         if (root->owner)
1318                 sysfs_remove_link(&root->dev.kobj, "module");
1319
1320         device_unregister(dev);
1321 }
1322 EXPORT_SYMBOL_GPL(root_device_unregister);
1323
1324
1325 static void device_create_release(struct device *dev)
1326 {
1327         pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1328         kfree(dev);
1329 }
1330
1331 /**
1332  * device_create_vargs - creates a device and registers it with sysfs
1333  * @class: pointer to the struct class that this device should be registered to
1334  * @parent: pointer to the parent struct device of this new device, if any
1335  * @devt: the dev_t for the char device to be added
1336  * @drvdata: the data to be added to the device for callbacks
1337  * @fmt: string for the device's name
1338  * @args: va_list for the device's name
1339  *
1340  * This function can be used by char device classes.  A struct device
1341  * will be created in sysfs, registered to the specified class.
1342  *
1343  * A "dev" file will be created, showing the dev_t for the device, if
1344  * the dev_t is not 0,0.
1345  * If a pointer to a parent struct device is passed in, the newly created
1346  * struct device will be a child of that device in sysfs.
1347  * The pointer to the struct device will be returned from the call.
1348  * Any further sysfs files that might be required can be created using this
1349  * pointer.
1350  *
1351  * Note: the struct class passed to this function must have previously
1352  * been created with a call to class_create().
1353  */
1354 struct device *device_create_vargs(struct class *class, struct device *parent,
1355                                    dev_t devt, void *drvdata, const char *fmt,
1356                                    va_list args)
1357 {
1358         struct device *dev = NULL;
1359         int retval = -ENODEV;
1360
1361         if (class == NULL || IS_ERR(class))
1362                 goto error;
1363
1364         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1365         if (!dev) {
1366                 retval = -ENOMEM;
1367                 goto error;
1368         }
1369
1370         dev->devt = devt;
1371         dev->class = class;
1372         dev->parent = parent;
1373         dev->release = device_create_release;
1374         dev_set_drvdata(dev, drvdata);
1375
1376         retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1377         if (retval)
1378                 goto error;
1379
1380         retval = device_register(dev);
1381         if (retval)
1382                 goto error;
1383
1384         return dev;
1385
1386 error:
1387         put_device(dev);
1388         return ERR_PTR(retval);
1389 }
1390 EXPORT_SYMBOL_GPL(device_create_vargs);
1391
1392 /**
1393  * device_create - creates a device and registers it with sysfs
1394  * @class: pointer to the struct class that this device should be registered to
1395  * @parent: pointer to the parent struct device of this new device, if any
1396  * @devt: the dev_t for the char device to be added
1397  * @drvdata: the data to be added to the device for callbacks
1398  * @fmt: string for the device's name
1399  *
1400  * This function can be used by char device classes.  A struct device
1401  * will be created in sysfs, registered to the specified class.
1402  *
1403  * A "dev" file will be created, showing the dev_t for the device, if
1404  * the dev_t is not 0,0.
1405  * If a pointer to a parent struct device is passed in, the newly created
1406  * struct device will be a child of that device in sysfs.
1407  * The pointer to the struct device will be returned from the call.
1408  * Any further sysfs files that might be required can be created using this
1409  * pointer.
1410  *
1411  * Note: the struct class passed to this function must have previously
1412  * been created with a call to class_create().
1413  */
1414 struct device *device_create(struct class *class, struct device *parent,
1415                              dev_t devt, void *drvdata, const char *fmt, ...)
1416 {
1417         va_list vargs;
1418         struct device *dev;
1419
1420         va_start(vargs, fmt);
1421         dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1422         va_end(vargs);
1423         return dev;
1424 }
1425 EXPORT_SYMBOL_GPL(device_create);
1426
1427 static int __match_devt(struct device *dev, void *data)
1428 {
1429         dev_t *devt = data;
1430
1431         return dev->devt == *devt;
1432 }
1433
1434 /**
1435  * device_destroy - removes a device that was created with device_create()
1436  * @class: pointer to the struct class that this device was registered with
1437  * @devt: the dev_t of the device that was previously registered
1438  *
1439  * This call unregisters and cleans up a device that was created with a
1440  * call to device_create().
1441  */
1442 void device_destroy(struct class *class, dev_t devt)
1443 {
1444         struct device *dev;
1445
1446         dev = class_find_device(class, NULL, &devt, __match_devt);
1447         if (dev) {
1448                 put_device(dev);
1449                 device_unregister(dev);
1450         }
1451 }
1452 EXPORT_SYMBOL_GPL(device_destroy);
1453
1454 /**
1455  * device_rename - renames a device
1456  * @dev: the pointer to the struct device to be renamed
1457  * @new_name: the new name of the device
1458  *
1459  * It is the responsibility of the caller to provide mutual
1460  * exclusion between two different calls of device_rename
1461  * on the same device to ensure that new_name is valid and
1462  * won't conflict with other devices.
1463  */
1464 int device_rename(struct device *dev, char *new_name)
1465 {
1466         char *old_class_name = NULL;
1467         char *new_class_name = NULL;
1468         char *old_device_name = NULL;
1469         int error;
1470
1471         dev = get_device(dev);
1472         if (!dev)
1473                 return -EINVAL;
1474
1475         pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1476                  __func__, new_name);
1477
1478 #ifdef CONFIG_SYSFS_DEPRECATED
1479         if ((dev->class) && (dev->parent))
1480                 old_class_name = make_class_name(dev->class->name, &dev->kobj);
1481 #endif
1482
1483         old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1484         if (!old_device_name) {
1485                 error = -ENOMEM;
1486                 goto out;
1487         }
1488
1489         error = kobject_rename(&dev->kobj, new_name);
1490         if (error)
1491                 goto out;
1492
1493 #ifdef CONFIG_SYSFS_DEPRECATED
1494         if (old_class_name) {
1495                 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1496                 if (new_class_name) {
1497                         error = sysfs_create_link_nowarn(&dev->parent->kobj,
1498                                                          &dev->kobj,
1499                                                          new_class_name);
1500                         if (error)
1501                                 goto out;
1502                         sysfs_remove_link(&dev->parent->kobj, old_class_name);
1503                 }
1504         }
1505 #else
1506         if (dev->class) {
1507                 error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
1508                                                  &dev->kobj, dev_name(dev));
1509                 if (error)
1510                         goto out;
1511                 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
1512                                   old_device_name);
1513         }
1514 #endif
1515
1516 out:
1517         put_device(dev);
1518
1519         kfree(new_class_name);
1520         kfree(old_class_name);
1521         kfree(old_device_name);
1522
1523         return error;
1524 }
1525 EXPORT_SYMBOL_GPL(device_rename);
1526
1527 static int device_move_class_links(struct device *dev,
1528                                    struct device *old_parent,
1529                                    struct device *new_parent)
1530 {
1531         int error = 0;
1532 #ifdef CONFIG_SYSFS_DEPRECATED
1533         char *class_name;
1534
1535         class_name = make_class_name(dev->class->name, &dev->kobj);
1536         if (!class_name) {
1537                 error = -ENOMEM;
1538                 goto out;
1539         }
1540         if (old_parent) {
1541                 sysfs_remove_link(&dev->kobj, "device");
1542                 sysfs_remove_link(&old_parent->kobj, class_name);
1543         }
1544         if (new_parent) {
1545                 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1546                                           "device");
1547                 if (error)
1548                         goto out;
1549                 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1550                                           class_name);
1551                 if (error)
1552                         sysfs_remove_link(&dev->kobj, "device");
1553         } else
1554                 error = 0;
1555 out:
1556         kfree(class_name);
1557         return error;
1558 #else
1559         if (old_parent)
1560                 sysfs_remove_link(&dev->kobj, "device");
1561         if (new_parent)
1562                 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1563                                           "device");
1564         return error;
1565 #endif
1566 }
1567
1568 /**
1569  * device_move - moves a device to a new parent
1570  * @dev: the pointer to the struct device to be moved
1571  * @new_parent: the new parent of the device (can by NULL)
1572  * @dpm_order: how to reorder the dpm_list
1573  */
1574 int device_move(struct device *dev, struct device *new_parent,
1575                 enum dpm_order dpm_order)
1576 {
1577         int error;
1578         struct device *old_parent;
1579         struct kobject *new_parent_kobj;
1580
1581         dev = get_device(dev);
1582         if (!dev)
1583                 return -EINVAL;
1584
1585         device_pm_lock();
1586         new_parent = get_device(new_parent);
1587         new_parent_kobj = get_device_parent(dev, new_parent);
1588
1589         pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1590                  __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1591         error = kobject_move(&dev->kobj, new_parent_kobj);
1592         if (error) {
1593                 cleanup_glue_dir(dev, new_parent_kobj);
1594                 put_device(new_parent);
1595                 goto out;
1596         }
1597         old_parent = dev->parent;
1598         dev->parent = new_parent;
1599         if (old_parent)
1600                 klist_remove(&dev->p->knode_parent);
1601         if (new_parent) {
1602                 klist_add_tail(&dev->p->knode_parent,
1603                                &new_parent->p->klist_children);
1604                 set_dev_node(dev, dev_to_node(new_parent));
1605         }
1606
1607         if (!dev->class)
1608                 goto out_put;
1609         error = device_move_class_links(dev, old_parent, new_parent);
1610         if (error) {
1611                 /* We ignore errors on cleanup since we're hosed anyway... */
1612                 device_move_class_links(dev, new_parent, old_parent);
1613                 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1614                         if (new_parent)
1615                                 klist_remove(&dev->p->knode_parent);
1616                         dev->parent = old_parent;
1617                         if (old_parent) {
1618                                 klist_add_tail(&dev->p->knode_parent,
1619                                                &old_parent->p->klist_children);
1620                                 set_dev_node(dev, dev_to_node(old_parent));
1621                         }
1622                 }
1623                 cleanup_glue_dir(dev, new_parent_kobj);
1624                 put_device(new_parent);
1625                 goto out;
1626         }
1627         switch (dpm_order) {
1628         case DPM_ORDER_NONE:
1629                 break;
1630         case DPM_ORDER_DEV_AFTER_PARENT:
1631                 device_pm_move_after(dev, new_parent);
1632                 break;
1633         case DPM_ORDER_PARENT_BEFORE_DEV:
1634                 device_pm_move_before(new_parent, dev);
1635                 break;
1636         case DPM_ORDER_DEV_LAST:
1637                 device_pm_move_last(dev);
1638                 break;
1639         }
1640 out_put:
1641         put_device(old_parent);
1642 out:
1643         device_pm_unlock();
1644         put_device(dev);
1645         return error;
1646 }
1647 EXPORT_SYMBOL_GPL(device_move);
1648
1649 /**
1650  * device_shutdown - call ->shutdown() on each device to shutdown.
1651  */
1652 void device_shutdown(void)
1653 {
1654         struct device *dev, *devn;
1655
1656         list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
1657                                 kobj.entry) {
1658                 if (dev->bus && dev->bus->shutdown) {
1659                         dev_dbg(dev, "shutdown\n");
1660                         dev->bus->shutdown(dev);
1661                 } else if (dev->driver && dev->driver->shutdown) {
1662                         dev_dbg(dev, "shutdown\n");
1663                         dev->driver->shutdown(dev);
1664                 }
1665         }
1666         kobject_put(sysfs_dev_char_kobj);
1667         kobject_put(sysfs_dev_block_kobj);
1668         kobject_put(dev_kobj);
1669         async_synchronize_full();
1670 }