OSDN Git Service

cnss2: Add support for genoa sdio
[sagit-ice-cold/kernel_xiaomi_msm8998.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/fwnode.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/kdev_t.h>
21 #include <linux/notifier.h>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 #include <linux/genhd.h>
25 #include <linux/kallsyms.h>
26 #include <linux/mutex.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/netdevice.h>
29 #include <linux/sysfs.h>
30
31 #include "base.h"
32 #include "power/power.h"
33
34 #ifdef CONFIG_SYSFS_DEPRECATED
35 #ifdef CONFIG_SYSFS_DEPRECATED_V2
36 long sysfs_deprecated = 1;
37 #else
38 long sysfs_deprecated = 0;
39 #endif
40 static int __init sysfs_deprecated_setup(char *arg)
41 {
42         return kstrtol(arg, 10, &sysfs_deprecated);
43 }
44 early_param("sysfs.deprecated", sysfs_deprecated_setup);
45 #endif
46
47 int (*platform_notify)(struct device *dev) = NULL;
48 int (*platform_notify_remove)(struct device *dev) = NULL;
49 static struct kobject *dev_kobj;
50 struct kobject *sysfs_dev_char_kobj;
51 struct kobject *sysfs_dev_block_kobj;
52
53 static DEFINE_MUTEX(device_hotplug_lock);
54
55 void lock_device_hotplug(void)
56 {
57         mutex_lock(&device_hotplug_lock);
58 }
59
60 void unlock_device_hotplug(void)
61 {
62         mutex_unlock(&device_hotplug_lock);
63 }
64
65 int lock_device_hotplug_sysfs(void)
66 {
67         if (mutex_trylock(&device_hotplug_lock))
68                 return 0;
69
70         /* Avoid busy looping (5 ms of sleep should do). */
71         msleep(5);
72         return restart_syscall();
73 }
74
75 void lock_device_hotplug_assert(void)
76 {
77         lockdep_assert_held(&device_hotplug_lock);
78 }
79
80 #ifdef CONFIG_BLOCK
81 static inline int device_is_not_partition(struct device *dev)
82 {
83         return !(dev->type == &part_type);
84 }
85 #else
86 static inline int device_is_not_partition(struct device *dev)
87 {
88         return 1;
89 }
90 #endif
91
92 /**
93  * dev_driver_string - Return a device's driver name, if at all possible
94  * @dev: struct device to get the name of
95  *
96  * Will return the device's driver's name if it is bound to a device.  If
97  * the device is not bound to a driver, it will return the name of the bus
98  * it is attached to.  If it is not attached to a bus either, an empty
99  * string will be returned.
100  */
101 const char *dev_driver_string(const struct device *dev)
102 {
103         struct device_driver *drv;
104
105         /* dev->driver can change to NULL underneath us because of unbinding,
106          * so be careful about accessing it.  dev->bus and dev->class should
107          * never change once they are set, so they don't need special care.
108          */
109         drv = ACCESS_ONCE(dev->driver);
110         return drv ? drv->name :
111                         (dev->bus ? dev->bus->name :
112                         (dev->class ? dev->class->name : ""));
113 }
114 EXPORT_SYMBOL(dev_driver_string);
115
116 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
117
118 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
119                              char *buf)
120 {
121         struct device_attribute *dev_attr = to_dev_attr(attr);
122         struct device *dev = kobj_to_dev(kobj);
123         ssize_t ret = -EIO;
124
125         if (dev_attr->show)
126                 ret = dev_attr->show(dev, dev_attr, buf);
127         if (ret >= (ssize_t)PAGE_SIZE) {
128                 print_symbol("dev_attr_show: %s returned bad count\n",
129                                 (unsigned long)dev_attr->show);
130         }
131         return ret;
132 }
133
134 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
135                               const char *buf, size_t count)
136 {
137         struct device_attribute *dev_attr = to_dev_attr(attr);
138         struct device *dev = kobj_to_dev(kobj);
139         ssize_t ret = -EIO;
140
141         if (dev_attr->store)
142                 ret = dev_attr->store(dev, dev_attr, buf, count);
143         return ret;
144 }
145
146 static const struct sysfs_ops dev_sysfs_ops = {
147         .show   = dev_attr_show,
148         .store  = dev_attr_store,
149 };
150
151 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
152
153 ssize_t device_store_ulong(struct device *dev,
154                            struct device_attribute *attr,
155                            const char *buf, size_t size)
156 {
157         struct dev_ext_attribute *ea = to_ext_attr(attr);
158         char *end;
159         unsigned long new = simple_strtoul(buf, &end, 0);
160         if (end == buf)
161                 return -EINVAL;
162         *(unsigned long *)(ea->var) = new;
163         /* Always return full write size even if we didn't consume all */
164         return size;
165 }
166 EXPORT_SYMBOL_GPL(device_store_ulong);
167
168 ssize_t device_show_ulong(struct device *dev,
169                           struct device_attribute *attr,
170                           char *buf)
171 {
172         struct dev_ext_attribute *ea = to_ext_attr(attr);
173         return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
174 }
175 EXPORT_SYMBOL_GPL(device_show_ulong);
176
177 ssize_t device_store_int(struct device *dev,
178                          struct device_attribute *attr,
179                          const char *buf, size_t size)
180 {
181         struct dev_ext_attribute *ea = to_ext_attr(attr);
182         char *end;
183         long new = simple_strtol(buf, &end, 0);
184         if (end == buf || new > INT_MAX || new < INT_MIN)
185                 return -EINVAL;
186         *(int *)(ea->var) = new;
187         /* Always return full write size even if we didn't consume all */
188         return size;
189 }
190 EXPORT_SYMBOL_GPL(device_store_int);
191
192 ssize_t device_show_int(struct device *dev,
193                         struct device_attribute *attr,
194                         char *buf)
195 {
196         struct dev_ext_attribute *ea = to_ext_attr(attr);
197
198         return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
199 }
200 EXPORT_SYMBOL_GPL(device_show_int);
201
202 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
203                           const char *buf, size_t size)
204 {
205         struct dev_ext_attribute *ea = to_ext_attr(attr);
206
207         if (strtobool(buf, ea->var) < 0)
208                 return -EINVAL;
209
210         return size;
211 }
212 EXPORT_SYMBOL_GPL(device_store_bool);
213
214 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
215                          char *buf)
216 {
217         struct dev_ext_attribute *ea = to_ext_attr(attr);
218
219         return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
220 }
221 EXPORT_SYMBOL_GPL(device_show_bool);
222
223 /**
224  * device_release - free device structure.
225  * @kobj: device's kobject.
226  *
227  * This is called once the reference count for the object
228  * reaches 0. We forward the call to the device's release
229  * method, which should handle actually freeing the structure.
230  */
231 static void device_release(struct kobject *kobj)
232 {
233         struct device *dev = kobj_to_dev(kobj);
234         struct device_private *p = dev->p;
235
236         /*
237          * Some platform devices are driven without driver attached
238          * and managed resources may have been acquired.  Make sure
239          * all resources are released.
240          *
241          * Drivers still can add resources into device after device
242          * is deleted but alive, so release devres here to avoid
243          * possible memory leak.
244          */
245         devres_release_all(dev);
246
247         if (dev->release)
248                 dev->release(dev);
249         else if (dev->type && dev->type->release)
250                 dev->type->release(dev);
251         else if (dev->class && dev->class->dev_release)
252                 dev->class->dev_release(dev);
253         else
254                 WARN(1, KERN_ERR "Device '%s' does not have a release() "
255                         "function, it is broken and must be fixed.\n",
256                         dev_name(dev));
257         kfree(p);
258 }
259
260 static const void *device_namespace(struct kobject *kobj)
261 {
262         struct device *dev = kobj_to_dev(kobj);
263         const void *ns = NULL;
264
265         if (dev->class && dev->class->ns_type)
266                 ns = dev->class->namespace(dev);
267
268         return ns;
269 }
270
271 static struct kobj_type device_ktype = {
272         .release        = device_release,
273         .sysfs_ops      = &dev_sysfs_ops,
274         .namespace      = device_namespace,
275 };
276
277
278 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
279 {
280         struct kobj_type *ktype = get_ktype(kobj);
281
282         if (ktype == &device_ktype) {
283                 struct device *dev = kobj_to_dev(kobj);
284                 if (dev->bus)
285                         return 1;
286                 if (dev->class)
287                         return 1;
288         }
289         return 0;
290 }
291
292 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
293 {
294         struct device *dev = kobj_to_dev(kobj);
295
296         if (dev->bus)
297                 return dev->bus->name;
298         if (dev->class)
299                 return dev->class->name;
300         return NULL;
301 }
302
303 static int dev_uevent(struct kset *kset, struct kobject *kobj,
304                       struct kobj_uevent_env *env)
305 {
306         struct device *dev = kobj_to_dev(kobj);
307         int retval = 0;
308
309         /* add device node properties if present */
310         if (MAJOR(dev->devt)) {
311                 const char *tmp;
312                 const char *name;
313                 umode_t mode = 0;
314                 kuid_t uid = GLOBAL_ROOT_UID;
315                 kgid_t gid = GLOBAL_ROOT_GID;
316
317                 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
318                 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
319                 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
320                 if (name) {
321                         add_uevent_var(env, "DEVNAME=%s", name);
322                         if (mode)
323                                 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
324                         if (!uid_eq(uid, GLOBAL_ROOT_UID))
325                                 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
326                         if (!gid_eq(gid, GLOBAL_ROOT_GID))
327                                 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
328                         kfree(tmp);
329                 }
330         }
331
332         if (dev->type && dev->type->name)
333                 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
334
335         if (dev->driver)
336                 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
337
338         /* Add common DT information about the device */
339         of_device_uevent(dev, env);
340
341         /* have the bus specific function add its stuff */
342         if (dev->bus && dev->bus->uevent) {
343                 retval = dev->bus->uevent(dev, env);
344                 if (retval)
345                         pr_debug("device: '%s': %s: bus uevent() returned %d\n",
346                                  dev_name(dev), __func__, retval);
347         }
348
349         /* have the class specific function add its stuff */
350         if (dev->class && dev->class->dev_uevent) {
351                 retval = dev->class->dev_uevent(dev, env);
352                 if (retval)
353                         pr_debug("device: '%s': %s: class uevent() "
354                                  "returned %d\n", dev_name(dev),
355                                  __func__, retval);
356         }
357
358         /* have the device type specific function add its stuff */
359         if (dev->type && dev->type->uevent) {
360                 retval = dev->type->uevent(dev, env);
361                 if (retval)
362                         pr_debug("device: '%s': %s: dev_type uevent() "
363                                  "returned %d\n", dev_name(dev),
364                                  __func__, retval);
365         }
366
367         return retval;
368 }
369
370 static const struct kset_uevent_ops device_uevent_ops = {
371         .filter =       dev_uevent_filter,
372         .name =         dev_uevent_name,
373         .uevent =       dev_uevent,
374 };
375
376 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
377                            char *buf)
378 {
379         struct kobject *top_kobj;
380         struct kset *kset;
381         struct kobj_uevent_env *env = NULL;
382         int i;
383         size_t count = 0;
384         int retval;
385
386         /* search the kset, the device belongs to */
387         top_kobj = &dev->kobj;
388         while (!top_kobj->kset && top_kobj->parent)
389                 top_kobj = top_kobj->parent;
390         if (!top_kobj->kset)
391                 goto out;
392
393         kset = top_kobj->kset;
394         if (!kset->uevent_ops || !kset->uevent_ops->uevent)
395                 goto out;
396
397         /* respect filter */
398         if (kset->uevent_ops && kset->uevent_ops->filter)
399                 if (!kset->uevent_ops->filter(kset, &dev->kobj))
400                         goto out;
401
402         env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
403         if (!env)
404                 return -ENOMEM;
405
406         /* let the kset specific function add its keys */
407         retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
408         if (retval)
409                 goto out;
410
411         /* copy keys to file */
412         for (i = 0; i < env->envp_idx; i++)
413                 count += sprintf(&buf[count], "%s\n", env->envp[i]);
414 out:
415         kfree(env);
416         return count;
417 }
418
419 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
420                             const char *buf, size_t count)
421 {
422         enum kobject_action action;
423
424         if (kobject_action_type(buf, count, &action) == 0)
425                 kobject_uevent(&dev->kobj, action);
426         else
427                 dev_err(dev, "uevent: unknown action-string\n");
428         return count;
429 }
430 static DEVICE_ATTR_RW(uevent);
431
432 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
433                            char *buf)
434 {
435         bool val;
436
437         device_lock(dev);
438         val = !dev->offline;
439         device_unlock(dev);
440         return sprintf(buf, "%u\n", val);
441 }
442
443 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
444                             const char *buf, size_t count)
445 {
446         bool val;
447         int ret;
448
449         ret = strtobool(buf, &val);
450         if (ret < 0)
451                 return ret;
452
453         ret = lock_device_hotplug_sysfs();
454         if (ret)
455                 return ret;
456
457         ret = val ? device_online(dev) : device_offline(dev);
458         unlock_device_hotplug();
459         return ret < 0 ? ret : count;
460 }
461 static DEVICE_ATTR_RW(online);
462
463 int device_add_groups(struct device *dev, const struct attribute_group **groups)
464 {
465         return sysfs_create_groups(&dev->kobj, groups);
466 }
467
468 void device_remove_groups(struct device *dev,
469                           const struct attribute_group **groups)
470 {
471         sysfs_remove_groups(&dev->kobj, groups);
472 }
473
474 static int device_add_attrs(struct device *dev)
475 {
476         struct class *class = dev->class;
477         const struct device_type *type = dev->type;
478         int error;
479
480         if (class) {
481                 error = device_add_groups(dev, class->dev_groups);
482                 if (error)
483                         return error;
484         }
485
486         if (type) {
487                 error = device_add_groups(dev, type->groups);
488                 if (error)
489                         goto err_remove_class_groups;
490         }
491
492         error = device_add_groups(dev, dev->groups);
493         if (error)
494                 goto err_remove_type_groups;
495
496         if (device_supports_offline(dev) && !dev->offline_disabled) {
497                 error = device_create_file(dev, &dev_attr_online);
498                 if (error)
499                         goto err_remove_dev_groups;
500         }
501
502         return 0;
503
504  err_remove_dev_groups:
505         device_remove_groups(dev, dev->groups);
506  err_remove_type_groups:
507         if (type)
508                 device_remove_groups(dev, type->groups);
509  err_remove_class_groups:
510         if (class)
511                 device_remove_groups(dev, class->dev_groups);
512
513         return error;
514 }
515
516 static void device_remove_attrs(struct device *dev)
517 {
518         struct class *class = dev->class;
519         const struct device_type *type = dev->type;
520
521         device_remove_file(dev, &dev_attr_online);
522         device_remove_groups(dev, dev->groups);
523
524         if (type)
525                 device_remove_groups(dev, type->groups);
526
527         if (class)
528                 device_remove_groups(dev, class->dev_groups);
529 }
530
531 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
532                         char *buf)
533 {
534         return print_dev_t(buf, dev->devt);
535 }
536 static DEVICE_ATTR_RO(dev);
537
538 /* /sys/devices/ */
539 struct kset *devices_kset;
540
541 /**
542  * devices_kset_move_before - Move device in the devices_kset's list.
543  * @deva: Device to move.
544  * @devb: Device @deva should come before.
545  */
546 static void devices_kset_move_before(struct device *deva, struct device *devb)
547 {
548         if (!devices_kset)
549                 return;
550         pr_debug("devices_kset: Moving %s before %s\n",
551                  dev_name(deva), dev_name(devb));
552         spin_lock(&devices_kset->list_lock);
553         list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
554         spin_unlock(&devices_kset->list_lock);
555 }
556
557 /**
558  * devices_kset_move_after - Move device in the devices_kset's list.
559  * @deva: Device to move
560  * @devb: Device @deva should come after.
561  */
562 static void devices_kset_move_after(struct device *deva, struct device *devb)
563 {
564         if (!devices_kset)
565                 return;
566         pr_debug("devices_kset: Moving %s after %s\n",
567                  dev_name(deva), dev_name(devb));
568         spin_lock(&devices_kset->list_lock);
569         list_move(&deva->kobj.entry, &devb->kobj.entry);
570         spin_unlock(&devices_kset->list_lock);
571 }
572
573 /**
574  * devices_kset_move_last - move the device to the end of devices_kset's list.
575  * @dev: device to move
576  */
577 void devices_kset_move_last(struct device *dev)
578 {
579         if (!devices_kset)
580                 return;
581         pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
582         spin_lock(&devices_kset->list_lock);
583         list_move_tail(&dev->kobj.entry, &devices_kset->list);
584         spin_unlock(&devices_kset->list_lock);
585 }
586
587 /**
588  * device_create_file - create sysfs attribute file for device.
589  * @dev: device.
590  * @attr: device attribute descriptor.
591  */
592 int device_create_file(struct device *dev,
593                        const struct device_attribute *attr)
594 {
595         int error = 0;
596
597         if (dev) {
598                 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
599                         "Attribute %s: write permission without 'store'\n",
600                         attr->attr.name);
601                 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
602                         "Attribute %s: read permission without 'show'\n",
603                         attr->attr.name);
604                 error = sysfs_create_file(&dev->kobj, &attr->attr);
605         }
606
607         return error;
608 }
609 EXPORT_SYMBOL_GPL(device_create_file);
610
611 /**
612  * device_remove_file - remove sysfs attribute file.
613  * @dev: device.
614  * @attr: device attribute descriptor.
615  */
616 void device_remove_file(struct device *dev,
617                         const struct device_attribute *attr)
618 {
619         if (dev)
620                 sysfs_remove_file(&dev->kobj, &attr->attr);
621 }
622 EXPORT_SYMBOL_GPL(device_remove_file);
623
624 /**
625  * device_remove_file_self - remove sysfs attribute file from its own method.
626  * @dev: device.
627  * @attr: device attribute descriptor.
628  *
629  * See kernfs_remove_self() for details.
630  */
631 bool device_remove_file_self(struct device *dev,
632                              const struct device_attribute *attr)
633 {
634         if (dev)
635                 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
636         else
637                 return false;
638 }
639 EXPORT_SYMBOL_GPL(device_remove_file_self);
640
641 /**
642  * device_create_bin_file - create sysfs binary attribute file for device.
643  * @dev: device.
644  * @attr: device binary attribute descriptor.
645  */
646 int device_create_bin_file(struct device *dev,
647                            const struct bin_attribute *attr)
648 {
649         int error = -EINVAL;
650         if (dev)
651                 error = sysfs_create_bin_file(&dev->kobj, attr);
652         return error;
653 }
654 EXPORT_SYMBOL_GPL(device_create_bin_file);
655
656 /**
657  * device_remove_bin_file - remove sysfs binary attribute file
658  * @dev: device.
659  * @attr: device binary attribute descriptor.
660  */
661 void device_remove_bin_file(struct device *dev,
662                             const struct bin_attribute *attr)
663 {
664         if (dev)
665                 sysfs_remove_bin_file(&dev->kobj, attr);
666 }
667 EXPORT_SYMBOL_GPL(device_remove_bin_file);
668
669 static void klist_children_get(struct klist_node *n)
670 {
671         struct device_private *p = to_device_private_parent(n);
672         struct device *dev = p->device;
673
674         get_device(dev);
675 }
676
677 static void klist_children_put(struct klist_node *n)
678 {
679         struct device_private *p = to_device_private_parent(n);
680         struct device *dev = p->device;
681
682         put_device(dev);
683 }
684
685 /**
686  * device_initialize - init device structure.
687  * @dev: device.
688  *
689  * This prepares the device for use by other layers by initializing
690  * its fields.
691  * It is the first half of device_register(), if called by
692  * that function, though it can also be called separately, so one
693  * may use @dev's fields. In particular, get_device()/put_device()
694  * may be used for reference counting of @dev after calling this
695  * function.
696  *
697  * All fields in @dev must be initialized by the caller to 0, except
698  * for those explicitly set to some other value.  The simplest
699  * approach is to use kzalloc() to allocate the structure containing
700  * @dev.
701  *
702  * NOTE: Use put_device() to give up your reference instead of freeing
703  * @dev directly once you have called this function.
704  */
705 void device_initialize(struct device *dev)
706 {
707         dev->kobj.kset = devices_kset;
708         kobject_init(&dev->kobj, &device_ktype);
709         INIT_LIST_HEAD(&dev->dma_pools);
710         mutex_init(&dev->mutex);
711         lockdep_set_novalidate_class(&dev->mutex);
712         spin_lock_init(&dev->devres_lock);
713         INIT_LIST_HEAD(&dev->devres_head);
714         device_pm_init(dev);
715         set_dev_node(dev, -1);
716 #ifdef CONFIG_GENERIC_MSI_IRQ
717         INIT_LIST_HEAD(&dev->msi_list);
718 #endif
719 }
720 EXPORT_SYMBOL_GPL(device_initialize);
721
722 struct kobject *virtual_device_parent(struct device *dev)
723 {
724         static struct kobject *virtual_dir = NULL;
725
726         if (!virtual_dir)
727                 virtual_dir = kobject_create_and_add("virtual",
728                                                      &devices_kset->kobj);
729
730         return virtual_dir;
731 }
732
733 struct class_dir {
734         struct kobject kobj;
735         struct class *class;
736 };
737
738 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
739
740 static void class_dir_release(struct kobject *kobj)
741 {
742         struct class_dir *dir = to_class_dir(kobj);
743         kfree(dir);
744 }
745
746 static const
747 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
748 {
749         struct class_dir *dir = to_class_dir(kobj);
750         return dir->class->ns_type;
751 }
752
753 static struct kobj_type class_dir_ktype = {
754         .release        = class_dir_release,
755         .sysfs_ops      = &kobj_sysfs_ops,
756         .child_ns_type  = class_dir_child_ns_type
757 };
758
759 static struct kobject *
760 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
761 {
762         struct class_dir *dir;
763         int retval;
764
765         dir = kzalloc(sizeof(*dir), GFP_KERNEL);
766         if (!dir)
767                 return ERR_PTR(-ENOMEM);
768
769         dir->class = class;
770         kobject_init(&dir->kobj, &class_dir_ktype);
771
772         dir->kobj.kset = &class->p->glue_dirs;
773
774         retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
775         if (retval < 0) {
776                 kobject_put(&dir->kobj);
777                 return ERR_PTR(retval);
778         }
779         return &dir->kobj;
780 }
781
782 static DEFINE_MUTEX(gdp_mutex);
783
784 static struct kobject *get_device_parent(struct device *dev,
785                                          struct device *parent)
786 {
787         if (dev->class) {
788                 struct kobject *kobj = NULL;
789                 struct kobject *parent_kobj;
790                 struct kobject *k;
791
792 #ifdef CONFIG_BLOCK
793                 /* block disks show up in /sys/block */
794                 if (sysfs_deprecated && dev->class == &block_class) {
795                         if (parent && parent->class == &block_class)
796                                 return &parent->kobj;
797                         return &block_class.p->subsys.kobj;
798                 }
799 #endif
800
801                 /*
802                  * If we have no parent, we live in "virtual".
803                  * Class-devices with a non class-device as parent, live
804                  * in a "glue" directory to prevent namespace collisions.
805                  */
806                 if (parent == NULL)
807                         parent_kobj = virtual_device_parent(dev);
808                 else if (parent->class && !dev->class->ns_type)
809                         return &parent->kobj;
810                 else
811                         parent_kobj = &parent->kobj;
812
813                 mutex_lock(&gdp_mutex);
814
815                 /* find our class-directory at the parent and reference it */
816                 spin_lock(&dev->class->p->glue_dirs.list_lock);
817                 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
818                         if (k->parent == parent_kobj) {
819                                 kobj = kobject_get(k);
820                                 break;
821                         }
822                 spin_unlock(&dev->class->p->glue_dirs.list_lock);
823                 if (kobj) {
824                         mutex_unlock(&gdp_mutex);
825                         return kobj;
826                 }
827
828                 /* or create a new class-directory at the parent device */
829                 k = class_dir_create_and_add(dev->class, parent_kobj);
830                 /* do not emit an uevent for this simple "glue" directory */
831                 mutex_unlock(&gdp_mutex);
832                 return k;
833         }
834
835         /* subsystems can specify a default root directory for their devices */
836         if (!parent && dev->bus && dev->bus->dev_root)
837                 return &dev->bus->dev_root->kobj;
838
839         if (parent)
840                 return &parent->kobj;
841         return NULL;
842 }
843
844 static inline bool live_in_glue_dir(struct kobject *kobj,
845                                     struct device *dev)
846 {
847         if (!kobj || !dev->class ||
848             kobj->kset != &dev->class->p->glue_dirs)
849                 return false;
850         return true;
851 }
852
853 static inline struct kobject *get_glue_dir(struct device *dev)
854 {
855         return dev->kobj.parent;
856 }
857
858 /*
859  * make sure cleaning up dir as the last step, we need to make
860  * sure .release handler of kobject is run with holding the
861  * global lock
862  */
863 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
864 {
865         /* see if we live in a "glue" directory */
866         if (!live_in_glue_dir(glue_dir, dev))
867                 return;
868
869         mutex_lock(&gdp_mutex);
870         if (!kobject_has_children(glue_dir))
871                 kobject_del(glue_dir);
872         kobject_put(glue_dir);
873         mutex_unlock(&gdp_mutex);
874 }
875
876 static int device_add_class_symlinks(struct device *dev)
877 {
878         struct device_node *of_node = dev_of_node(dev);
879         int error;
880
881         if (of_node) {
882                 error = sysfs_create_link(&dev->kobj, &of_node->kobj,"of_node");
883                 if (error)
884                         dev_warn(dev, "Error %d creating of_node link\n",error);
885                 /* An error here doesn't warrant bringing down the device */
886         }
887
888         if (!dev->class)
889                 return 0;
890
891         error = sysfs_create_link(&dev->kobj,
892                                   &dev->class->p->subsys.kobj,
893                                   "subsystem");
894         if (error)
895                 goto out_devnode;
896
897         if (dev->parent && device_is_not_partition(dev)) {
898                 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
899                                           "device");
900                 if (error)
901                         goto out_subsys;
902         }
903
904 #ifdef CONFIG_BLOCK
905         /* /sys/block has directories and does not need symlinks */
906         if (sysfs_deprecated && dev->class == &block_class)
907                 return 0;
908 #endif
909
910         /* link in the class directory pointing to the device */
911         error = sysfs_create_link(&dev->class->p->subsys.kobj,
912                                   &dev->kobj, dev_name(dev));
913         if (error)
914                 goto out_device;
915
916         return 0;
917
918 out_device:
919         sysfs_remove_link(&dev->kobj, "device");
920
921 out_subsys:
922         sysfs_remove_link(&dev->kobj, "subsystem");
923 out_devnode:
924         sysfs_remove_link(&dev->kobj, "of_node");
925         return error;
926 }
927
928 static void device_remove_class_symlinks(struct device *dev)
929 {
930         if (dev_of_node(dev))
931                 sysfs_remove_link(&dev->kobj, "of_node");
932
933         if (!dev->class)
934                 return;
935
936         if (dev->parent && device_is_not_partition(dev))
937                 sysfs_remove_link(&dev->kobj, "device");
938         sysfs_remove_link(&dev->kobj, "subsystem");
939 #ifdef CONFIG_BLOCK
940         if (sysfs_deprecated && dev->class == &block_class)
941                 return;
942 #endif
943         sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
944 }
945
946 /**
947  * dev_set_name - set a device name
948  * @dev: device
949  * @fmt: format string for the device's name
950  */
951 int dev_set_name(struct device *dev, const char *fmt, ...)
952 {
953         va_list vargs;
954         int err;
955
956         va_start(vargs, fmt);
957         err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
958         va_end(vargs);
959         return err;
960 }
961 EXPORT_SYMBOL_GPL(dev_set_name);
962
963 /**
964  * device_to_dev_kobj - select a /sys/dev/ directory for the device
965  * @dev: device
966  *
967  * By default we select char/ for new entries.  Setting class->dev_obj
968  * to NULL prevents an entry from being created.  class->dev_kobj must
969  * be set (or cleared) before any devices are registered to the class
970  * otherwise device_create_sys_dev_entry() and
971  * device_remove_sys_dev_entry() will disagree about the presence of
972  * the link.
973  */
974 static struct kobject *device_to_dev_kobj(struct device *dev)
975 {
976         struct kobject *kobj;
977
978         if (dev->class)
979                 kobj = dev->class->dev_kobj;
980         else
981                 kobj = sysfs_dev_char_kobj;
982
983         return kobj;
984 }
985
986 static int device_create_sys_dev_entry(struct device *dev)
987 {
988         struct kobject *kobj = device_to_dev_kobj(dev);
989         int error = 0;
990         char devt_str[15];
991
992         if (kobj) {
993                 format_dev_t(devt_str, dev->devt);
994                 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
995         }
996
997         return error;
998 }
999
1000 static void device_remove_sys_dev_entry(struct device *dev)
1001 {
1002         struct kobject *kobj = device_to_dev_kobj(dev);
1003         char devt_str[15];
1004
1005         if (kobj) {
1006                 format_dev_t(devt_str, dev->devt);
1007                 sysfs_remove_link(kobj, devt_str);
1008         }
1009 }
1010
1011 int device_private_init(struct device *dev)
1012 {
1013         dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1014         if (!dev->p)
1015                 return -ENOMEM;
1016         dev->p->device = dev;
1017         klist_init(&dev->p->klist_children, klist_children_get,
1018                    klist_children_put);
1019         INIT_LIST_HEAD(&dev->p->deferred_probe);
1020         return 0;
1021 }
1022
1023 /**
1024  * device_add - add device to device hierarchy.
1025  * @dev: device.
1026  *
1027  * This is part 2 of device_register(), though may be called
1028  * separately _iff_ device_initialize() has been called separately.
1029  *
1030  * This adds @dev to the kobject hierarchy via kobject_add(), adds it
1031  * to the global and sibling lists for the device, then
1032  * adds it to the other relevant subsystems of the driver model.
1033  *
1034  * Do not call this routine or device_register() more than once for
1035  * any device structure.  The driver model core is not designed to work
1036  * with devices that get unregistered and then spring back to life.
1037  * (Among other things, it's very hard to guarantee that all references
1038  * to the previous incarnation of @dev have been dropped.)  Allocate
1039  * and register a fresh new struct device instead.
1040  *
1041  * NOTE: _Never_ directly free @dev after calling this function, even
1042  * if it returned an error! Always use put_device() to give up your
1043  * reference instead.
1044  */
1045 int device_add(struct device *dev)
1046 {
1047         struct device *parent = NULL;
1048         struct kobject *kobj;
1049         struct class_interface *class_intf;
1050         int error = -EINVAL;
1051         struct kobject *glue_dir = NULL;
1052
1053         dev = get_device(dev);
1054         if (!dev)
1055                 goto done;
1056
1057         if (!dev->p) {
1058                 error = device_private_init(dev);
1059                 if (error)
1060                         goto done;
1061         }
1062
1063         /*
1064          * for statically allocated devices, which should all be converted
1065          * some day, we need to initialize the name. We prevent reading back
1066          * the name, and force the use of dev_name()
1067          */
1068         if (dev->init_name) {
1069                 dev_set_name(dev, "%s", dev->init_name);
1070                 dev->init_name = NULL;
1071         }
1072
1073         /* subsystems can specify simple device enumeration */
1074         if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1075                 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1076
1077         if (!dev_name(dev)) {
1078                 error = -EINVAL;
1079                 goto name_error;
1080         }
1081
1082         pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1083
1084         parent = get_device(dev->parent);
1085         kobj = get_device_parent(dev, parent);
1086         if (IS_ERR(kobj)) {
1087                 error = PTR_ERR(kobj);
1088                 goto parent_error;
1089         }
1090         if (kobj)
1091                 dev->kobj.parent = kobj;
1092
1093         /* use parent numa_node */
1094         if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
1095                 set_dev_node(dev, dev_to_node(parent));
1096
1097         /* first, register with generic layer. */
1098         /* we require the name to be set before, and pass NULL */
1099         error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
1100         if (error) {
1101                 glue_dir = get_glue_dir(dev);
1102                 goto Error;
1103         }
1104
1105         /* notify platform of device entry */
1106         if (platform_notify)
1107                 platform_notify(dev);
1108
1109         error = device_create_file(dev, &dev_attr_uevent);
1110         if (error)
1111                 goto attrError;
1112
1113         error = device_add_class_symlinks(dev);
1114         if (error)
1115                 goto SymlinkError;
1116         error = device_add_attrs(dev);
1117         if (error)
1118                 goto AttrsError;
1119         error = bus_add_device(dev);
1120         if (error)
1121                 goto BusError;
1122         error = dpm_sysfs_add(dev);
1123         if (error)
1124                 goto DPMError;
1125         device_pm_add(dev);
1126
1127         if (MAJOR(dev->devt)) {
1128                 error = device_create_file(dev, &dev_attr_dev);
1129                 if (error)
1130                         goto DevAttrError;
1131
1132                 error = device_create_sys_dev_entry(dev);
1133                 if (error)
1134                         goto SysEntryError;
1135
1136                 devtmpfs_create_node(dev);
1137         }
1138
1139         /* Notify clients of device addition.  This call must come
1140          * after dpm_sysfs_add() and before kobject_uevent().
1141          */
1142         if (dev->bus)
1143                 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1144                                              BUS_NOTIFY_ADD_DEVICE, dev);
1145
1146         kobject_uevent(&dev->kobj, KOBJ_ADD);
1147         bus_probe_device(dev);
1148         if (parent)
1149                 klist_add_tail(&dev->p->knode_parent,
1150                                &parent->p->klist_children);
1151
1152         if (dev->class) {
1153                 mutex_lock(&dev->class->p->mutex);
1154                 /* tie the class to the device */
1155                 klist_add_tail(&dev->knode_class,
1156                                &dev->class->p->klist_devices);
1157
1158                 /* notify any interfaces that the device is here */
1159                 list_for_each_entry(class_intf,
1160                                     &dev->class->p->interfaces, node)
1161                         if (class_intf->add_dev)
1162                                 class_intf->add_dev(dev, class_intf);
1163                 mutex_unlock(&dev->class->p->mutex);
1164         }
1165 done:
1166         put_device(dev);
1167         return error;
1168  SysEntryError:
1169         if (MAJOR(dev->devt))
1170                 device_remove_file(dev, &dev_attr_dev);
1171  DevAttrError:
1172         device_pm_remove(dev);
1173         dpm_sysfs_remove(dev);
1174  DPMError:
1175         bus_remove_device(dev);
1176  BusError:
1177         device_remove_attrs(dev);
1178  AttrsError:
1179         device_remove_class_symlinks(dev);
1180  SymlinkError:
1181         device_remove_file(dev, &dev_attr_uevent);
1182  attrError:
1183         kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1184         glue_dir = get_glue_dir(dev);
1185         kobject_del(&dev->kobj);
1186  Error:
1187         cleanup_glue_dir(dev, glue_dir);
1188 parent_error:
1189         put_device(parent);
1190 name_error:
1191         kfree(dev->p);
1192         dev->p = NULL;
1193         goto done;
1194 }
1195 EXPORT_SYMBOL_GPL(device_add);
1196
1197 /**
1198  * device_register - register a device with the system.
1199  * @dev: pointer to the device structure
1200  *
1201  * This happens in two clean steps - initialize the device
1202  * and add it to the system. The two steps can be called
1203  * separately, but this is the easiest and most common.
1204  * I.e. you should only call the two helpers separately if
1205  * have a clearly defined need to use and refcount the device
1206  * before it is added to the hierarchy.
1207  *
1208  * For more information, see the kerneldoc for device_initialize()
1209  * and device_add().
1210  *
1211  * NOTE: _Never_ directly free @dev after calling this function, even
1212  * if it returned an error! Always use put_device() to give up the
1213  * reference initialized in this function instead.
1214  */
1215 int device_register(struct device *dev)
1216 {
1217         device_initialize(dev);
1218         return device_add(dev);
1219 }
1220 EXPORT_SYMBOL_GPL(device_register);
1221
1222 /**
1223  * get_device - increment reference count for device.
1224  * @dev: device.
1225  *
1226  * This simply forwards the call to kobject_get(), though
1227  * we do take care to provide for the case that we get a NULL
1228  * pointer passed in.
1229  */
1230 struct device *get_device(struct device *dev)
1231 {
1232         return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1233 }
1234 EXPORT_SYMBOL_GPL(get_device);
1235
1236 /**
1237  * put_device - decrement reference count.
1238  * @dev: device in question.
1239  */
1240 void put_device(struct device *dev)
1241 {
1242         /* might_sleep(); */
1243         if (dev)
1244                 kobject_put(&dev->kobj);
1245 }
1246 EXPORT_SYMBOL_GPL(put_device);
1247
1248 /**
1249  * device_del - delete device from system.
1250  * @dev: device.
1251  *
1252  * This is the first part of the device unregistration
1253  * sequence. This removes the device from the lists we control
1254  * from here, has it removed from the other driver model
1255  * subsystems it was added to in device_add(), and removes it
1256  * from the kobject hierarchy.
1257  *
1258  * NOTE: this should be called manually _iff_ device_add() was
1259  * also called manually.
1260  */
1261 void device_del(struct device *dev)
1262 {
1263         struct device *parent = dev->parent;
1264         struct kobject *glue_dir = NULL;
1265         struct class_interface *class_intf;
1266
1267         /* Notify clients of device removal.  This call must come
1268          * before dpm_sysfs_remove().
1269          */
1270         if (dev->bus)
1271                 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1272                                              BUS_NOTIFY_DEL_DEVICE, dev);
1273         dpm_sysfs_remove(dev);
1274         if (parent)
1275                 klist_del(&dev->p->knode_parent);
1276         if (MAJOR(dev->devt)) {
1277                 devtmpfs_delete_node(dev);
1278                 device_remove_sys_dev_entry(dev);
1279                 device_remove_file(dev, &dev_attr_dev);
1280         }
1281         if (dev->class) {
1282                 device_remove_class_symlinks(dev);
1283
1284                 mutex_lock(&dev->class->p->mutex);
1285                 /* notify any interfaces that the device is now gone */
1286                 list_for_each_entry(class_intf,
1287                                     &dev->class->p->interfaces, node)
1288                         if (class_intf->remove_dev)
1289                                 class_intf->remove_dev(dev, class_intf);
1290                 /* remove the device from the class list */
1291                 klist_del(&dev->knode_class);
1292                 mutex_unlock(&dev->class->p->mutex);
1293         }
1294         device_remove_file(dev, &dev_attr_uevent);
1295         device_remove_attrs(dev);
1296         bus_remove_device(dev);
1297         device_pm_remove(dev);
1298         driver_deferred_probe_del(dev);
1299
1300         /* Notify the platform of the removal, in case they
1301          * need to do anything...
1302          */
1303         if (platform_notify_remove)
1304                 platform_notify_remove(dev);
1305         if (dev->bus)
1306                 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1307                                              BUS_NOTIFY_REMOVED_DEVICE, dev);
1308         kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1309         glue_dir = get_glue_dir(dev);
1310         kobject_del(&dev->kobj);
1311         cleanup_glue_dir(dev, glue_dir);
1312         put_device(parent);
1313 }
1314 EXPORT_SYMBOL_GPL(device_del);
1315
1316 /**
1317  * device_unregister - unregister device from system.
1318  * @dev: device going away.
1319  *
1320  * We do this in two parts, like we do device_register(). First,
1321  * we remove it from all the subsystems with device_del(), then
1322  * we decrement the reference count via put_device(). If that
1323  * is the final reference count, the device will be cleaned up
1324  * via device_release() above. Otherwise, the structure will
1325  * stick around until the final reference to the device is dropped.
1326  */
1327 void device_unregister(struct device *dev)
1328 {
1329         pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1330         device_del(dev);
1331         put_device(dev);
1332 }
1333 EXPORT_SYMBOL_GPL(device_unregister);
1334
1335 static struct device *prev_device(struct klist_iter *i)
1336 {
1337         struct klist_node *n = klist_prev(i);
1338         struct device *dev = NULL;
1339         struct device_private *p;
1340
1341         if (n) {
1342                 p = to_device_private_parent(n);
1343                 dev = p->device;
1344         }
1345         return dev;
1346 }
1347
1348 static struct device *next_device(struct klist_iter *i)
1349 {
1350         struct klist_node *n = klist_next(i);
1351         struct device *dev = NULL;
1352         struct device_private *p;
1353
1354         if (n) {
1355                 p = to_device_private_parent(n);
1356                 dev = p->device;
1357         }
1358         return dev;
1359 }
1360
1361 /**
1362  * device_get_devnode - path of device node file
1363  * @dev: device
1364  * @mode: returned file access mode
1365  * @uid: returned file owner
1366  * @gid: returned file group
1367  * @tmp: possibly allocated string
1368  *
1369  * Return the relative path of a possible device node.
1370  * Non-default names may need to allocate a memory to compose
1371  * a name. This memory is returned in tmp and needs to be
1372  * freed by the caller.
1373  */
1374 const char *device_get_devnode(struct device *dev,
1375                                umode_t *mode, kuid_t *uid, kgid_t *gid,
1376                                const char **tmp)
1377 {
1378         char *s;
1379
1380         *tmp = NULL;
1381
1382         /* the device type may provide a specific name */
1383         if (dev->type && dev->type->devnode)
1384                 *tmp = dev->type->devnode(dev, mode, uid, gid);
1385         if (*tmp)
1386                 return *tmp;
1387
1388         /* the class may provide a specific name */
1389         if (dev->class && dev->class->devnode)
1390                 *tmp = dev->class->devnode(dev, mode);
1391         if (*tmp)
1392                 return *tmp;
1393
1394         /* return name without allocation, tmp == NULL */
1395         if (strchr(dev_name(dev), '!') == NULL)
1396                 return dev_name(dev);
1397
1398         /* replace '!' in the name with '/' */
1399         s = kstrdup(dev_name(dev), GFP_KERNEL);
1400         if (!s)
1401                 return NULL;
1402         strreplace(s, '!', '/');
1403         return *tmp = s;
1404 }
1405
1406 /**
1407  * device_for_each_child - device child iterator.
1408  * @parent: parent struct device.
1409  * @fn: function to be called for each device.
1410  * @data: data for the callback.
1411  *
1412  * Iterate over @parent's child devices, and call @fn for each,
1413  * passing it @data.
1414  *
1415  * We check the return of @fn each time. If it returns anything
1416  * other than 0, we break out and return that value.
1417  */
1418 int device_for_each_child(struct device *parent, void *data,
1419                           int (*fn)(struct device *dev, void *data))
1420 {
1421         struct klist_iter i;
1422         struct device *child;
1423         int error = 0;
1424
1425         if (!parent->p)
1426                 return 0;
1427
1428         klist_iter_init(&parent->p->klist_children, &i);
1429         while ((child = next_device(&i)) && !error)
1430                 error = fn(child, data);
1431         klist_iter_exit(&i);
1432         return error;
1433 }
1434 EXPORT_SYMBOL_GPL(device_for_each_child);
1435
1436 /**
1437  * device_for_each_child_reverse - device child iterator in reversed order.
1438  * @parent: parent struct device.
1439  * @fn: function to be called for each device.
1440  * @data: data for the callback.
1441  *
1442  * Iterate over @parent's child devices, and call @fn for each,
1443  * passing it @data.
1444  *
1445  * We check the return of @fn each time. If it returns anything
1446  * other than 0, we break out and return that value.
1447  */
1448 int device_for_each_child_reverse(struct device *parent, void *data,
1449                                   int (*fn)(struct device *dev, void *data))
1450 {
1451         struct klist_iter i;
1452         struct device *child;
1453         int error = 0;
1454
1455         if (!parent->p)
1456                 return 0;
1457
1458         klist_iter_init(&parent->p->klist_children, &i);
1459         while ((child = prev_device(&i)) && !error)
1460                 error = fn(child, data);
1461         klist_iter_exit(&i);
1462         return error;
1463 }
1464 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
1465
1466 /**
1467  * device_find_child - device iterator for locating a particular device.
1468  * @parent: parent struct device
1469  * @match: Callback function to check device
1470  * @data: Data to pass to match function
1471  *
1472  * This is similar to the device_for_each_child() function above, but it
1473  * returns a reference to a device that is 'found' for later use, as
1474  * determined by the @match callback.
1475  *
1476  * The callback should return 0 if the device doesn't match and non-zero
1477  * if it does.  If the callback returns non-zero and a reference to the
1478  * current device can be obtained, this function will return to the caller
1479  * and not iterate over any more devices.
1480  *
1481  * NOTE: you will need to drop the reference with put_device() after use.
1482  */
1483 struct device *device_find_child(struct device *parent, void *data,
1484                                  int (*match)(struct device *dev, void *data))
1485 {
1486         struct klist_iter i;
1487         struct device *child;
1488
1489         if (!parent)
1490                 return NULL;
1491
1492         klist_iter_init(&parent->p->klist_children, &i);
1493         while ((child = next_device(&i)))
1494                 if (match(child, data) && get_device(child))
1495                         break;
1496         klist_iter_exit(&i);
1497         return child;
1498 }
1499 EXPORT_SYMBOL_GPL(device_find_child);
1500
1501 int __init devices_init(void)
1502 {
1503         devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1504         if (!devices_kset)
1505                 return -ENOMEM;
1506         dev_kobj = kobject_create_and_add("dev", NULL);
1507         if (!dev_kobj)
1508                 goto dev_kobj_err;
1509         sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1510         if (!sysfs_dev_block_kobj)
1511                 goto block_kobj_err;
1512         sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1513         if (!sysfs_dev_char_kobj)
1514                 goto char_kobj_err;
1515
1516         return 0;
1517
1518  char_kobj_err:
1519         kobject_put(sysfs_dev_block_kobj);
1520  block_kobj_err:
1521         kobject_put(dev_kobj);
1522  dev_kobj_err:
1523         kset_unregister(devices_kset);
1524         return -ENOMEM;
1525 }
1526
1527 static int device_check_offline(struct device *dev, void *not_used)
1528 {
1529         int ret;
1530
1531         ret = device_for_each_child(dev, NULL, device_check_offline);
1532         if (ret)
1533                 return ret;
1534
1535         return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
1536 }
1537
1538 /**
1539  * device_offline - Prepare the device for hot-removal.
1540  * @dev: Device to be put offline.
1541  *
1542  * Execute the device bus type's .offline() callback, if present, to prepare
1543  * the device for a subsequent hot-removal.  If that succeeds, the device must
1544  * not be used until either it is removed or its bus type's .online() callback
1545  * is executed.
1546  *
1547  * Call under device_hotplug_lock.
1548  */
1549 int device_offline(struct device *dev)
1550 {
1551         int ret;
1552
1553         if (dev->offline_disabled)
1554                 return -EPERM;
1555
1556         ret = device_for_each_child(dev, NULL, device_check_offline);
1557         if (ret)
1558                 return ret;
1559
1560         device_lock(dev);
1561         if (device_supports_offline(dev)) {
1562                 if (dev->offline) {
1563                         ret = 1;
1564                 } else {
1565                         ret = dev->bus->offline(dev);
1566                         if (!ret) {
1567                                 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1568                                 dev->offline = true;
1569                         }
1570                 }
1571         }
1572         device_unlock(dev);
1573
1574         return ret;
1575 }
1576
1577 /**
1578  * device_online - Put the device back online after successful device_offline().
1579  * @dev: Device to be put back online.
1580  *
1581  * If device_offline() has been successfully executed for @dev, but the device
1582  * has not been removed subsequently, execute its bus type's .online() callback
1583  * to indicate that the device can be used again.
1584  *
1585  * Call under device_hotplug_lock.
1586  */
1587 int device_online(struct device *dev)
1588 {
1589         int ret = 0;
1590
1591         device_lock(dev);
1592         if (device_supports_offline(dev)) {
1593                 if (dev->offline) {
1594                         ret = dev->bus->online(dev);
1595                         if (!ret) {
1596                                 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
1597                                 dev->offline = false;
1598                         }
1599                 } else {
1600                         ret = 1;
1601                 }
1602         }
1603         device_unlock(dev);
1604
1605         return ret;
1606 }
1607
1608 struct root_device {
1609         struct device dev;
1610         struct module *owner;
1611 };
1612
1613 static inline struct root_device *to_root_device(struct device *d)
1614 {
1615         return container_of(d, struct root_device, dev);
1616 }
1617
1618 static void root_device_release(struct device *dev)
1619 {
1620         kfree(to_root_device(dev));
1621 }
1622
1623 /**
1624  * __root_device_register - allocate and register a root device
1625  * @name: root device name
1626  * @owner: owner module of the root device, usually THIS_MODULE
1627  *
1628  * This function allocates a root device and registers it
1629  * using device_register(). In order to free the returned
1630  * device, use root_device_unregister().
1631  *
1632  * Root devices are dummy devices which allow other devices
1633  * to be grouped under /sys/devices. Use this function to
1634  * allocate a root device and then use it as the parent of
1635  * any device which should appear under /sys/devices/{name}
1636  *
1637  * The /sys/devices/{name} directory will also contain a
1638  * 'module' symlink which points to the @owner directory
1639  * in sysfs.
1640  *
1641  * Returns &struct device pointer on success, or ERR_PTR() on error.
1642  *
1643  * Note: You probably want to use root_device_register().
1644  */
1645 struct device *__root_device_register(const char *name, struct module *owner)
1646 {
1647         struct root_device *root;
1648         int err = -ENOMEM;
1649
1650         root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1651         if (!root)
1652                 return ERR_PTR(err);
1653
1654         err = dev_set_name(&root->dev, "%s", name);
1655         if (err) {
1656                 kfree(root);
1657                 return ERR_PTR(err);
1658         }
1659
1660         root->dev.release = root_device_release;
1661
1662         err = device_register(&root->dev);
1663         if (err) {
1664                 put_device(&root->dev);
1665                 return ERR_PTR(err);
1666         }
1667
1668 #ifdef CONFIG_MODULES   /* gotta find a "cleaner" way to do this */
1669         if (owner) {
1670                 struct module_kobject *mk = &owner->mkobj;
1671
1672                 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1673                 if (err) {
1674                         device_unregister(&root->dev);
1675                         return ERR_PTR(err);
1676                 }
1677                 root->owner = owner;
1678         }
1679 #endif
1680
1681         return &root->dev;
1682 }
1683 EXPORT_SYMBOL_GPL(__root_device_register);
1684
1685 /**
1686  * root_device_unregister - unregister and free a root device
1687  * @dev: device going away
1688  *
1689  * This function unregisters and cleans up a device that was created by
1690  * root_device_register().
1691  */
1692 void root_device_unregister(struct device *dev)
1693 {
1694         struct root_device *root = to_root_device(dev);
1695
1696         if (root->owner)
1697                 sysfs_remove_link(&root->dev.kobj, "module");
1698
1699         device_unregister(dev);
1700 }
1701 EXPORT_SYMBOL_GPL(root_device_unregister);
1702
1703
1704 static void device_create_release(struct device *dev)
1705 {
1706         pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1707         kfree(dev);
1708 }
1709
1710 static struct device *
1711 device_create_groups_vargs(struct class *class, struct device *parent,
1712                            dev_t devt, void *drvdata,
1713                            const struct attribute_group **groups,
1714                            const char *fmt, va_list args)
1715 {
1716         struct device *dev = NULL;
1717         int retval = -ENODEV;
1718
1719         if (class == NULL || IS_ERR(class))
1720                 goto error;
1721
1722         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1723         if (!dev) {
1724                 retval = -ENOMEM;
1725                 goto error;
1726         }
1727
1728         device_initialize(dev);
1729         dev->devt = devt;
1730         dev->class = class;
1731         dev->parent = parent;
1732         dev->groups = groups;
1733         dev->release = device_create_release;
1734         dev_set_drvdata(dev, drvdata);
1735
1736         retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1737         if (retval)
1738                 goto error;
1739
1740         retval = device_add(dev);
1741         if (retval)
1742                 goto error;
1743
1744         return dev;
1745
1746 error:
1747         put_device(dev);
1748         return ERR_PTR(retval);
1749 }
1750
1751 /**
1752  * device_create_vargs - creates a device and registers it with sysfs
1753  * @class: pointer to the struct class that this device should be registered to
1754  * @parent: pointer to the parent struct device of this new device, if any
1755  * @devt: the dev_t for the char device to be added
1756  * @drvdata: the data to be added to the device for callbacks
1757  * @fmt: string for the device's name
1758  * @args: va_list for the device's name
1759  *
1760  * This function can be used by char device classes.  A struct device
1761  * will be created in sysfs, registered to the specified class.
1762  *
1763  * A "dev" file will be created, showing the dev_t for the device, if
1764  * the dev_t is not 0,0.
1765  * If a pointer to a parent struct device is passed in, the newly created
1766  * struct device will be a child of that device in sysfs.
1767  * The pointer to the struct device will be returned from the call.
1768  * Any further sysfs files that might be required can be created using this
1769  * pointer.
1770  *
1771  * Returns &struct device pointer on success, or ERR_PTR() on error.
1772  *
1773  * Note: the struct class passed to this function must have previously
1774  * been created with a call to class_create().
1775  */
1776 struct device *device_create_vargs(struct class *class, struct device *parent,
1777                                    dev_t devt, void *drvdata, const char *fmt,
1778                                    va_list args)
1779 {
1780         return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
1781                                           fmt, args);
1782 }
1783 EXPORT_SYMBOL_GPL(device_create_vargs);
1784
1785 /**
1786  * device_create - creates a device and registers it with sysfs
1787  * @class: pointer to the struct class that this device should be registered to
1788  * @parent: pointer to the parent struct device of this new device, if any
1789  * @devt: the dev_t for the char device to be added
1790  * @drvdata: the data to be added to the device for callbacks
1791  * @fmt: string for the device's name
1792  *
1793  * This function can be used by char device classes.  A struct device
1794  * will be created in sysfs, registered to the specified class.
1795  *
1796  * A "dev" file will be created, showing the dev_t for the device, if
1797  * the dev_t is not 0,0.
1798  * If a pointer to a parent struct device is passed in, the newly created
1799  * struct device will be a child of that device in sysfs.
1800  * The pointer to the struct device will be returned from the call.
1801  * Any further sysfs files that might be required can be created using this
1802  * pointer.
1803  *
1804  * Returns &struct device pointer on success, or ERR_PTR() on error.
1805  *
1806  * Note: the struct class passed to this function must have previously
1807  * been created with a call to class_create().
1808  */
1809 struct device *device_create(struct class *class, struct device *parent,
1810                              dev_t devt, void *drvdata, const char *fmt, ...)
1811 {
1812         va_list vargs;
1813         struct device *dev;
1814
1815         va_start(vargs, fmt);
1816         dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1817         va_end(vargs);
1818         return dev;
1819 }
1820 EXPORT_SYMBOL_GPL(device_create);
1821
1822 /**
1823  * device_create_with_groups - creates a device and registers it with sysfs
1824  * @class: pointer to the struct class that this device should be registered to
1825  * @parent: pointer to the parent struct device of this new device, if any
1826  * @devt: the dev_t for the char device to be added
1827  * @drvdata: the data to be added to the device for callbacks
1828  * @groups: NULL-terminated list of attribute groups to be created
1829  * @fmt: string for the device's name
1830  *
1831  * This function can be used by char device classes.  A struct device
1832  * will be created in sysfs, registered to the specified class.
1833  * Additional attributes specified in the groups parameter will also
1834  * be created automatically.
1835  *
1836  * A "dev" file will be created, showing the dev_t for the device, if
1837  * the dev_t is not 0,0.
1838  * If a pointer to a parent struct device is passed in, the newly created
1839  * struct device will be a child of that device in sysfs.
1840  * The pointer to the struct device will be returned from the call.
1841  * Any further sysfs files that might be required can be created using this
1842  * pointer.
1843  *
1844  * Returns &struct device pointer on success, or ERR_PTR() on error.
1845  *
1846  * Note: the struct class passed to this function must have previously
1847  * been created with a call to class_create().
1848  */
1849 struct device *device_create_with_groups(struct class *class,
1850                                          struct device *parent, dev_t devt,
1851                                          void *drvdata,
1852                                          const struct attribute_group **groups,
1853                                          const char *fmt, ...)
1854 {
1855         va_list vargs;
1856         struct device *dev;
1857
1858         va_start(vargs, fmt);
1859         dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
1860                                          fmt, vargs);
1861         va_end(vargs);
1862         return dev;
1863 }
1864 EXPORT_SYMBOL_GPL(device_create_with_groups);
1865
1866 static int __match_devt(struct device *dev, const void *data)
1867 {
1868         const dev_t *devt = data;
1869
1870         return dev->devt == *devt;
1871 }
1872
1873 /**
1874  * device_destroy - removes a device that was created with device_create()
1875  * @class: pointer to the struct class that this device was registered with
1876  * @devt: the dev_t of the device that was previously registered
1877  *
1878  * This call unregisters and cleans up a device that was created with a
1879  * call to device_create().
1880  */
1881 void device_destroy(struct class *class, dev_t devt)
1882 {
1883         struct device *dev;
1884
1885         dev = class_find_device(class, NULL, &devt, __match_devt);
1886         if (dev) {
1887                 put_device(dev);
1888                 device_unregister(dev);
1889         }
1890 }
1891 EXPORT_SYMBOL_GPL(device_destroy);
1892
1893 /**
1894  * device_rename - renames a device
1895  * @dev: the pointer to the struct device to be renamed
1896  * @new_name: the new name of the device
1897  *
1898  * It is the responsibility of the caller to provide mutual
1899  * exclusion between two different calls of device_rename
1900  * on the same device to ensure that new_name is valid and
1901  * won't conflict with other devices.
1902  *
1903  * Note: Don't call this function.  Currently, the networking layer calls this
1904  * function, but that will change.  The following text from Kay Sievers offers
1905  * some insight:
1906  *
1907  * Renaming devices is racy at many levels, symlinks and other stuff are not
1908  * replaced atomically, and you get a "move" uevent, but it's not easy to
1909  * connect the event to the old and new device. Device nodes are not renamed at
1910  * all, there isn't even support for that in the kernel now.
1911  *
1912  * In the meantime, during renaming, your target name might be taken by another
1913  * driver, creating conflicts. Or the old name is taken directly after you
1914  * renamed it -- then you get events for the same DEVPATH, before you even see
1915  * the "move" event. It's just a mess, and nothing new should ever rely on
1916  * kernel device renaming. Besides that, it's not even implemented now for
1917  * other things than (driver-core wise very simple) network devices.
1918  *
1919  * We are currently about to change network renaming in udev to completely
1920  * disallow renaming of devices in the same namespace as the kernel uses,
1921  * because we can't solve the problems properly, that arise with swapping names
1922  * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1923  * be allowed to some other name than eth[0-9]*, for the aforementioned
1924  * reasons.
1925  *
1926  * Make up a "real" name in the driver before you register anything, or add
1927  * some other attributes for userspace to find the device, or use udev to add
1928  * symlinks -- but never rename kernel devices later, it's a complete mess. We
1929  * don't even want to get into that and try to implement the missing pieces in
1930  * the core. We really have other pieces to fix in the driver core mess. :)
1931  */
1932 int device_rename(struct device *dev, const char *new_name)
1933 {
1934         struct kobject *kobj = &dev->kobj;
1935         char *old_device_name = NULL;
1936         int error;
1937
1938         dev = get_device(dev);
1939         if (!dev)
1940                 return -EINVAL;
1941
1942         dev_dbg(dev, "renaming to %s\n", new_name);
1943
1944         old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1945         if (!old_device_name) {
1946                 error = -ENOMEM;
1947                 goto out;
1948         }
1949
1950         if (dev->class) {
1951                 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
1952                                              kobj, old_device_name,
1953                                              new_name, kobject_namespace(kobj));
1954                 if (error)
1955                         goto out;
1956         }
1957
1958         error = kobject_rename(kobj, new_name);
1959         if (error)
1960                 goto out;
1961
1962 out:
1963         put_device(dev);
1964
1965         kfree(old_device_name);
1966
1967         return error;
1968 }
1969 EXPORT_SYMBOL_GPL(device_rename);
1970
1971 static int device_move_class_links(struct device *dev,
1972                                    struct device *old_parent,
1973                                    struct device *new_parent)
1974 {
1975         int error = 0;
1976
1977         if (old_parent)
1978                 sysfs_remove_link(&dev->kobj, "device");
1979         if (new_parent)
1980                 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1981                                           "device");
1982         return error;
1983 }
1984
1985 /**
1986  * device_move - moves a device to a new parent
1987  * @dev: the pointer to the struct device to be moved
1988  * @new_parent: the new parent of the device (can by NULL)
1989  * @dpm_order: how to reorder the dpm_list
1990  */
1991 int device_move(struct device *dev, struct device *new_parent,
1992                 enum dpm_order dpm_order)
1993 {
1994         int error;
1995         struct device *old_parent;
1996         struct kobject *new_parent_kobj;
1997
1998         dev = get_device(dev);
1999         if (!dev)
2000                 return -EINVAL;
2001
2002         device_pm_lock();
2003         new_parent = get_device(new_parent);
2004         new_parent_kobj = get_device_parent(dev, new_parent);
2005         if (IS_ERR(new_parent_kobj)) {
2006                 error = PTR_ERR(new_parent_kobj);
2007                 put_device(new_parent);
2008                 goto out;
2009         }
2010
2011         pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2012                  __func__, new_parent ? dev_name(new_parent) : "<NULL>");
2013         error = kobject_move(&dev->kobj, new_parent_kobj);
2014         if (error) {
2015                 cleanup_glue_dir(dev, new_parent_kobj);
2016                 put_device(new_parent);
2017                 goto out;
2018         }
2019         old_parent = dev->parent;
2020         dev->parent = new_parent;
2021         if (old_parent)
2022                 klist_remove(&dev->p->knode_parent);
2023         if (new_parent) {
2024                 klist_add_tail(&dev->p->knode_parent,
2025                                &new_parent->p->klist_children);
2026                 set_dev_node(dev, dev_to_node(new_parent));
2027         }
2028
2029         if (dev->class) {
2030                 error = device_move_class_links(dev, old_parent, new_parent);
2031                 if (error) {
2032                         /* We ignore errors on cleanup since we're hosed anyway... */
2033                         device_move_class_links(dev, new_parent, old_parent);
2034                         if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2035                                 if (new_parent)
2036                                         klist_remove(&dev->p->knode_parent);
2037                                 dev->parent = old_parent;
2038                                 if (old_parent) {
2039                                         klist_add_tail(&dev->p->knode_parent,
2040                                                        &old_parent->p->klist_children);
2041                                         set_dev_node(dev, dev_to_node(old_parent));
2042                                 }
2043                         }
2044                         cleanup_glue_dir(dev, new_parent_kobj);
2045                         put_device(new_parent);
2046                         goto out;
2047                 }
2048         }
2049         switch (dpm_order) {
2050         case DPM_ORDER_NONE:
2051                 break;
2052         case DPM_ORDER_DEV_AFTER_PARENT:
2053                 device_pm_move_after(dev, new_parent);
2054                 devices_kset_move_after(dev, new_parent);
2055                 break;
2056         case DPM_ORDER_PARENT_BEFORE_DEV:
2057                 device_pm_move_before(new_parent, dev);
2058                 devices_kset_move_before(new_parent, dev);
2059                 break;
2060         case DPM_ORDER_DEV_LAST:
2061                 device_pm_move_last(dev);
2062                 devices_kset_move_last(dev);
2063                 break;
2064         }
2065
2066         put_device(old_parent);
2067 out:
2068         device_pm_unlock();
2069         put_device(dev);
2070         return error;
2071 }
2072 EXPORT_SYMBOL_GPL(device_move);
2073
2074 /**
2075  * device_shutdown - call ->shutdown() on each device to shutdown.
2076  */
2077 void device_shutdown(void)
2078 {
2079         struct device *dev, *parent;
2080
2081         spin_lock(&devices_kset->list_lock);
2082         /*
2083          * Walk the devices list backward, shutting down each in turn.
2084          * Beware that device unplug events may also start pulling
2085          * devices offline, even as the system is shutting down.
2086          */
2087         while (!list_empty(&devices_kset->list)) {
2088                 dev = list_entry(devices_kset->list.prev, struct device,
2089                                 kobj.entry);
2090
2091                 /*
2092                  * hold reference count of device's parent to
2093                  * prevent it from being freed because parent's
2094                  * lock is to be held
2095                  */
2096                 parent = get_device(dev->parent);
2097                 get_device(dev);
2098                 /*
2099                  * Make sure the device is off the kset list, in the
2100                  * event that dev->*->shutdown() doesn't remove it.
2101                  */
2102                 list_del_init(&dev->kobj.entry);
2103                 spin_unlock(&devices_kset->list_lock);
2104
2105                 /* hold lock to avoid race with probe/release */
2106                 if (parent)
2107                         device_lock(parent);
2108                 device_lock(dev);
2109
2110                 /* Don't allow any more runtime suspends */
2111                 pm_runtime_get_noresume(dev);
2112                 pm_runtime_barrier(dev);
2113
2114                 if (dev->class && dev->class->shutdown) {
2115                         if (initcall_debug)
2116                                 dev_info(dev, "shutdown\n");
2117                         dev->class->shutdown(dev);
2118                 } else if (dev->bus && dev->bus->shutdown) {
2119                         if (initcall_debug)
2120                                 dev_info(dev, "shutdown\n");
2121                         dev->bus->shutdown(dev);
2122                 } else if (dev->driver && dev->driver->shutdown) {
2123                         if (initcall_debug)
2124                                 dev_info(dev, "shutdown\n");
2125                         dev->driver->shutdown(dev);
2126                 }
2127
2128                 device_unlock(dev);
2129                 if (parent)
2130                         device_unlock(parent);
2131
2132                 put_device(dev);
2133                 put_device(parent);
2134
2135                 spin_lock(&devices_kset->list_lock);
2136         }
2137         spin_unlock(&devices_kset->list_lock);
2138 }
2139
2140 /*
2141  * Device logging functions
2142  */
2143
2144 #ifdef CONFIG_PRINTK
2145 static int
2146 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
2147 {
2148         const char *subsys;
2149         size_t pos = 0;
2150
2151         if (dev->class)
2152                 subsys = dev->class->name;
2153         else if (dev->bus)
2154                 subsys = dev->bus->name;
2155         else
2156                 return 0;
2157
2158         pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
2159         if (pos >= hdrlen)
2160                 goto overflow;
2161
2162         /*
2163          * Add device identifier DEVICE=:
2164          *   b12:8         block dev_t
2165          *   c127:3        char dev_t
2166          *   n8            netdev ifindex
2167          *   +sound:card0  subsystem:devname
2168          */
2169         if (MAJOR(dev->devt)) {
2170                 char c;
2171
2172                 if (strcmp(subsys, "block") == 0)
2173                         c = 'b';
2174                 else
2175                         c = 'c';
2176                 pos++;
2177                 pos += snprintf(hdr + pos, hdrlen - pos,
2178                                 "DEVICE=%c%u:%u",
2179                                 c, MAJOR(dev->devt), MINOR(dev->devt));
2180         } else if (strcmp(subsys, "net") == 0) {
2181                 struct net_device *net = to_net_dev(dev);
2182
2183                 pos++;
2184                 pos += snprintf(hdr + pos, hdrlen - pos,
2185                                 "DEVICE=n%u", net->ifindex);
2186         } else {
2187                 pos++;
2188                 pos += snprintf(hdr + pos, hdrlen - pos,
2189                                 "DEVICE=+%s:%s", subsys, dev_name(dev));
2190         }
2191
2192         if (pos >= hdrlen)
2193                 goto overflow;
2194
2195         return pos;
2196
2197 overflow:
2198         dev_WARN(dev, "device/subsystem name too long");
2199         return 0;
2200 }
2201
2202 int dev_vprintk_emit(int level, const struct device *dev,
2203                      const char *fmt, va_list args)
2204 {
2205         char hdr[128];
2206         size_t hdrlen;
2207
2208         hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2209
2210         return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2211 }
2212 EXPORT_SYMBOL(dev_vprintk_emit);
2213
2214 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2215 {
2216         va_list args;
2217         int r;
2218
2219         va_start(args, fmt);
2220
2221         r = dev_vprintk_emit(level, dev, fmt, args);
2222
2223         va_end(args);
2224
2225         return r;
2226 }
2227 EXPORT_SYMBOL(dev_printk_emit);
2228
2229 static void __dev_printk(const char *level, const struct device *dev,
2230                         struct va_format *vaf)
2231 {
2232         if (dev)
2233                 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2234                                 dev_driver_string(dev), dev_name(dev), vaf);
2235         else
2236                 printk("%s(NULL device *): %pV", level, vaf);
2237 }
2238
2239 void dev_printk(const char *level, const struct device *dev,
2240                 const char *fmt, ...)
2241 {
2242         struct va_format vaf;
2243         va_list args;
2244
2245         va_start(args, fmt);
2246
2247         vaf.fmt = fmt;
2248         vaf.va = &args;
2249
2250         __dev_printk(level, dev, &vaf);
2251
2252         va_end(args);
2253 }
2254 EXPORT_SYMBOL(dev_printk);
2255
2256 #define define_dev_printk_level(func, kern_level)               \
2257 void func(const struct device *dev, const char *fmt, ...)       \
2258 {                                                               \
2259         struct va_format vaf;                                   \
2260         va_list args;                                           \
2261                                                                 \
2262         va_start(args, fmt);                                    \
2263                                                                 \
2264         vaf.fmt = fmt;                                          \
2265         vaf.va = &args;                                         \
2266                                                                 \
2267         __dev_printk(kern_level, dev, &vaf);                    \
2268                                                                 \
2269         va_end(args);                                           \
2270 }                                                               \
2271 EXPORT_SYMBOL(func);
2272
2273 define_dev_printk_level(dev_emerg, KERN_EMERG);
2274 define_dev_printk_level(dev_alert, KERN_ALERT);
2275 define_dev_printk_level(dev_crit, KERN_CRIT);
2276 define_dev_printk_level(dev_err, KERN_ERR);
2277 define_dev_printk_level(dev_warn, KERN_WARNING);
2278 define_dev_printk_level(dev_notice, KERN_NOTICE);
2279 define_dev_printk_level(_dev_info, KERN_INFO);
2280
2281 #endif
2282
2283 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
2284 {
2285         return fwnode && !IS_ERR(fwnode->secondary);
2286 }
2287
2288 /**
2289  * set_primary_fwnode - Change the primary firmware node of a given device.
2290  * @dev: Device to handle.
2291  * @fwnode: New primary firmware node of the device.
2292  *
2293  * Set the device's firmware node pointer to @fwnode, but if a secondary
2294  * firmware node of the device is present, preserve it.
2295  */
2296 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2297 {
2298         if (fwnode) {
2299                 struct fwnode_handle *fn = dev->fwnode;
2300
2301                 if (fwnode_is_primary(fn))
2302                         fn = fn->secondary;
2303
2304                 fwnode->secondary = fn;
2305                 dev->fwnode = fwnode;
2306         } else {
2307                 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
2308                         dev->fwnode->secondary : NULL;
2309         }
2310 }
2311 EXPORT_SYMBOL_GPL(set_primary_fwnode);
2312
2313 /**
2314  * set_secondary_fwnode - Change the secondary firmware node of a given device.
2315  * @dev: Device to handle.
2316  * @fwnode: New secondary firmware node of the device.
2317  *
2318  * If a primary firmware node of the device is present, set its secondary
2319  * pointer to @fwnode.  Otherwise, set the device's firmware node pointer to
2320  * @fwnode.
2321  */
2322 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2323 {
2324         if (fwnode)
2325                 fwnode->secondary = ERR_PTR(-ENODEV);
2326
2327         if (fwnode_is_primary(dev->fwnode))
2328                 dev->fwnode->secondary = fwnode;
2329         else
2330                 dev->fwnode = fwnode;
2331 }