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