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