1
2
3
4
5
6
7
8
9
10
11#include <linux/device.h>
12#include <linux/err.h>
13#include <linux/fwnode.h>
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/string.h>
18#include <linux/kdev_t.h>
19#include <linux/notifier.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
22#include <linux/genhd.h>
23#include <linux/mutex.h>
24#include <linux/pm_runtime.h>
25#include <linux/netdevice.h>
26#include <linux/sched/signal.h>
27#include <linux/sysfs.h>
28
29#include "base.h"
30#include "power/power.h"
31
32#ifdef CONFIG_SYSFS_DEPRECATED
33#ifdef CONFIG_SYSFS_DEPRECATED_V2
34long sysfs_deprecated = 1;
35#else
36long sysfs_deprecated = 0;
37#endif
38static int __init sysfs_deprecated_setup(char *arg)
39{
40 return kstrtol(arg, 10, &sysfs_deprecated);
41}
42early_param("sysfs.deprecated", sysfs_deprecated_setup);
43#endif
44
45
46
47#ifdef CONFIG_SRCU
48static DEFINE_MUTEX(device_links_lock);
49DEFINE_STATIC_SRCU(device_links_srcu);
50
51static inline void device_links_write_lock(void)
52{
53 mutex_lock(&device_links_lock);
54}
55
56static inline void device_links_write_unlock(void)
57{
58 mutex_unlock(&device_links_lock);
59}
60
61int device_links_read_lock(void)
62{
63 return srcu_read_lock(&device_links_srcu);
64}
65
66void device_links_read_unlock(int idx)
67{
68 srcu_read_unlock(&device_links_srcu, idx);
69}
70#else
71static DECLARE_RWSEM(device_links_lock);
72
73static inline void device_links_write_lock(void)
74{
75 down_write(&device_links_lock);
76}
77
78static inline void device_links_write_unlock(void)
79{
80 up_write(&device_links_lock);
81}
82
83int device_links_read_lock(void)
84{
85 down_read(&device_links_lock);
86 return 0;
87}
88
89void device_links_read_unlock(int not_used)
90{
91 up_read(&device_links_lock);
92}
93#endif
94
95
96
97
98
99
100
101
102
103static int device_is_dependent(struct device *dev, void *target)
104{
105 struct device_link *link;
106 int ret;
107
108 if (WARN_ON(dev == target))
109 return 1;
110
111 ret = device_for_each_child(dev, target, device_is_dependent);
112 if (ret)
113 return ret;
114
115 list_for_each_entry(link, &dev->links.consumers, s_node) {
116 if (WARN_ON(link->consumer == target))
117 return 1;
118
119 ret = device_is_dependent(link->consumer, target);
120 if (ret)
121 break;
122 }
123 return ret;
124}
125
126static int device_reorder_to_tail(struct device *dev, void *not_used)
127{
128 struct device_link *link;
129
130
131
132
133
134 if (device_is_registered(dev))
135 devices_kset_move_last(dev);
136
137 if (device_pm_initialized(dev))
138 device_pm_move_last(dev);
139
140 device_for_each_child(dev, NULL, device_reorder_to_tail);
141 list_for_each_entry(link, &dev->links.consumers, s_node)
142 device_reorder_to_tail(link->consumer, NULL);
143
144 return 0;
145}
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175struct device_link *device_link_add(struct device *consumer,
176 struct device *supplier, u32 flags)
177{
178 struct device_link *link;
179
180 if (!consumer || !supplier ||
181 ((flags & DL_FLAG_STATELESS) && (flags & DL_FLAG_AUTOREMOVE)))
182 return NULL;
183
184 device_links_write_lock();
185 device_pm_lock();
186
187
188
189
190
191
192 if (!device_pm_initialized(supplier)
193 || device_is_dependent(consumer, supplier)) {
194 link = NULL;
195 goto out;
196 }
197
198 list_for_each_entry(link, &supplier->links.consumers, s_node)
199 if (link->consumer == consumer)
200 goto out;
201
202 link = kzalloc(sizeof(*link), GFP_KERNEL);
203 if (!link)
204 goto out;
205
206 if (flags & DL_FLAG_PM_RUNTIME) {
207 if (flags & DL_FLAG_RPM_ACTIVE) {
208 if (pm_runtime_get_sync(supplier) < 0) {
209 pm_runtime_put_noidle(supplier);
210 kfree(link);
211 link = NULL;
212 goto out;
213 }
214 link->rpm_active = true;
215 }
216 pm_runtime_new_link(consumer);
217 }
218 get_device(supplier);
219 link->supplier = supplier;
220 INIT_LIST_HEAD(&link->s_node);
221 get_device(consumer);
222 link->consumer = consumer;
223 INIT_LIST_HEAD(&link->c_node);
224 link->flags = flags;
225
226
227 if (flags & DL_FLAG_STATELESS) {
228 link->status = DL_STATE_NONE;
229 } else {
230 switch (supplier->links.status) {
231 case DL_DEV_DRIVER_BOUND:
232 switch (consumer->links.status) {
233 case DL_DEV_PROBING:
234
235
236
237
238
239 if (flags & DL_FLAG_PM_RUNTIME)
240 pm_runtime_get_sync(supplier);
241
242 link->status = DL_STATE_CONSUMER_PROBE;
243 break;
244 case DL_DEV_DRIVER_BOUND:
245 link->status = DL_STATE_ACTIVE;
246 break;
247 default:
248 link->status = DL_STATE_AVAILABLE;
249 break;
250 }
251 break;
252 case DL_DEV_UNBINDING:
253 link->status = DL_STATE_SUPPLIER_UNBIND;
254 break;
255 default:
256 link->status = DL_STATE_DORMANT;
257 break;
258 }
259 }
260
261
262
263
264
265
266
267
268 device_reorder_to_tail(consumer, NULL);
269
270 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
271 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
272
273 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
274
275 out:
276 device_pm_unlock();
277 device_links_write_unlock();
278 return link;
279}
280EXPORT_SYMBOL_GPL(device_link_add);
281
282static void device_link_free(struct device_link *link)
283{
284 put_device(link->consumer);
285 put_device(link->supplier);
286 kfree(link);
287}
288
289#ifdef CONFIG_SRCU
290static void __device_link_free_srcu(struct rcu_head *rhead)
291{
292 device_link_free(container_of(rhead, struct device_link, rcu_head));
293}
294
295static void __device_link_del(struct device_link *link)
296{
297 dev_info(link->consumer, "Dropping the link to %s\n",
298 dev_name(link->supplier));
299
300 if (link->flags & DL_FLAG_PM_RUNTIME)
301 pm_runtime_drop_link(link->consumer);
302
303 list_del_rcu(&link->s_node);
304 list_del_rcu(&link->c_node);
305 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
306}
307#else
308static void __device_link_del(struct device_link *link)
309{
310 dev_info(link->consumer, "Dropping the link to %s\n",
311 dev_name(link->supplier));
312
313 if (link->flags & DL_FLAG_PM_RUNTIME)
314 pm_runtime_drop_link(link->consumer);
315
316 list_del(&link->s_node);
317 list_del(&link->c_node);
318 device_link_free(link);
319}
320#endif
321
322
323
324
325
326
327
328
329void device_link_del(struct device_link *link)
330{
331 device_links_write_lock();
332 device_pm_lock();
333 __device_link_del(link);
334 device_pm_unlock();
335 device_links_write_unlock();
336}
337EXPORT_SYMBOL_GPL(device_link_del);
338
339static void device_links_missing_supplier(struct device *dev)
340{
341 struct device_link *link;
342
343 list_for_each_entry(link, &dev->links.suppliers, c_node)
344 if (link->status == DL_STATE_CONSUMER_PROBE)
345 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
346}
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364int device_links_check_suppliers(struct device *dev)
365{
366 struct device_link *link;
367 int ret = 0;
368
369 device_links_write_lock();
370
371 list_for_each_entry(link, &dev->links.suppliers, c_node) {
372 if (link->flags & DL_FLAG_STATELESS)
373 continue;
374
375 if (link->status != DL_STATE_AVAILABLE) {
376 device_links_missing_supplier(dev);
377 ret = -EPROBE_DEFER;
378 break;
379 }
380 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
381 }
382 dev->links.status = DL_DEV_PROBING;
383
384 device_links_write_unlock();
385 return ret;
386}
387
388
389
390
391
392
393
394
395
396
397
398
399void device_links_driver_bound(struct device *dev)
400{
401 struct device_link *link;
402
403 device_links_write_lock();
404
405 list_for_each_entry(link, &dev->links.consumers, s_node) {
406 if (link->flags & DL_FLAG_STATELESS)
407 continue;
408
409 WARN_ON(link->status != DL_STATE_DORMANT);
410 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
411 }
412
413 list_for_each_entry(link, &dev->links.suppliers, c_node) {
414 if (link->flags & DL_FLAG_STATELESS)
415 continue;
416
417 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
418 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
419 }
420
421 dev->links.status = DL_DEV_DRIVER_BOUND;
422
423 device_links_write_unlock();
424}
425
426
427
428
429
430
431
432
433
434
435
436
437
438static void __device_links_no_driver(struct device *dev)
439{
440 struct device_link *link, *ln;
441
442 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
443 if (link->flags & DL_FLAG_STATELESS)
444 continue;
445
446 if (link->flags & DL_FLAG_AUTOREMOVE)
447 __device_link_del(link);
448 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
449 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
450 }
451
452 dev->links.status = DL_DEV_NO_DRIVER;
453}
454
455void device_links_no_driver(struct device *dev)
456{
457 device_links_write_lock();
458 __device_links_no_driver(dev);
459 device_links_write_unlock();
460}
461
462
463
464
465
466
467
468
469
470
471
472void device_links_driver_cleanup(struct device *dev)
473{
474 struct device_link *link;
475
476 device_links_write_lock();
477
478 list_for_each_entry(link, &dev->links.consumers, s_node) {
479 if (link->flags & DL_FLAG_STATELESS)
480 continue;
481
482 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE);
483 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
484 WRITE_ONCE(link->status, DL_STATE_DORMANT);
485 }
486
487 __device_links_no_driver(dev);
488
489 device_links_write_unlock();
490}
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506bool device_links_busy(struct device *dev)
507{
508 struct device_link *link;
509 bool ret = false;
510
511 device_links_write_lock();
512
513 list_for_each_entry(link, &dev->links.consumers, s_node) {
514 if (link->flags & DL_FLAG_STATELESS)
515 continue;
516
517 if (link->status == DL_STATE_CONSUMER_PROBE
518 || link->status == DL_STATE_ACTIVE) {
519 ret = true;
520 break;
521 }
522 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
523 }
524
525 dev->links.status = DL_DEV_UNBINDING;
526
527 device_links_write_unlock();
528 return ret;
529}
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546void device_links_unbind_consumers(struct device *dev)
547{
548 struct device_link *link;
549
550 start:
551 device_links_write_lock();
552
553 list_for_each_entry(link, &dev->links.consumers, s_node) {
554 enum device_link_state status;
555
556 if (link->flags & DL_FLAG_STATELESS)
557 continue;
558
559 status = link->status;
560 if (status == DL_STATE_CONSUMER_PROBE) {
561 device_links_write_unlock();
562
563 wait_for_device_probe();
564 goto start;
565 }
566 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
567 if (status == DL_STATE_ACTIVE) {
568 struct device *consumer = link->consumer;
569
570 get_device(consumer);
571
572 device_links_write_unlock();
573
574 device_release_driver_internal(consumer, NULL,
575 consumer->parent);
576 put_device(consumer);
577 goto start;
578 }
579 }
580
581 device_links_write_unlock();
582}
583
584
585
586
587
588static void device_links_purge(struct device *dev)
589{
590 struct device_link *link, *ln;
591
592
593
594
595
596 device_links_write_lock();
597
598 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
599 WARN_ON(link->status == DL_STATE_ACTIVE);
600 __device_link_del(link);
601 }
602
603 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
604 WARN_ON(link->status != DL_STATE_DORMANT &&
605 link->status != DL_STATE_NONE);
606 __device_link_del(link);
607 }
608
609 device_links_write_unlock();
610}
611
612
613
614int (*platform_notify)(struct device *dev) = NULL;
615int (*platform_notify_remove)(struct device *dev) = NULL;
616static struct kobject *dev_kobj;
617struct kobject *sysfs_dev_char_kobj;
618struct kobject *sysfs_dev_block_kobj;
619
620static DEFINE_MUTEX(device_hotplug_lock);
621
622void lock_device_hotplug(void)
623{
624 mutex_lock(&device_hotplug_lock);
625}
626
627void unlock_device_hotplug(void)
628{
629 mutex_unlock(&device_hotplug_lock);
630}
631
632int lock_device_hotplug_sysfs(void)
633{
634 if (mutex_trylock(&device_hotplug_lock))
635 return 0;
636
637
638 msleep(5);
639 return restart_syscall();
640}
641
642#ifdef CONFIG_BLOCK
643static inline int device_is_not_partition(struct device *dev)
644{
645 return !(dev->type == &part_type);
646}
647#else
648static inline int device_is_not_partition(struct device *dev)
649{
650 return 1;
651}
652#endif
653
654
655
656
657
658
659
660
661
662
663const char *dev_driver_string(const struct device *dev)
664{
665 struct device_driver *drv;
666
667
668
669
670
671 drv = READ_ONCE(dev->driver);
672 return drv ? drv->name :
673 (dev->bus ? dev->bus->name :
674 (dev->class ? dev->class->name : ""));
675}
676EXPORT_SYMBOL(dev_driver_string);
677
678#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
679
680static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
681 char *buf)
682{
683 struct device_attribute *dev_attr = to_dev_attr(attr);
684 struct device *dev = kobj_to_dev(kobj);
685 ssize_t ret = -EIO;
686
687 if (dev_attr->show)
688 ret = dev_attr->show(dev, dev_attr, buf);
689 if (ret >= (ssize_t)PAGE_SIZE) {
690 printk("dev_attr_show: %pS returned bad count\n",
691 dev_attr->show);
692 }
693 return ret;
694}
695
696static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
697 const char *buf, size_t count)
698{
699 struct device_attribute *dev_attr = to_dev_attr(attr);
700 struct device *dev = kobj_to_dev(kobj);
701 ssize_t ret = -EIO;
702
703 if (dev_attr->store)
704 ret = dev_attr->store(dev, dev_attr, buf, count);
705 return ret;
706}
707
708static const struct sysfs_ops dev_sysfs_ops = {
709 .show = dev_attr_show,
710 .store = dev_attr_store,
711};
712
713#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
714
715ssize_t device_store_ulong(struct device *dev,
716 struct device_attribute *attr,
717 const char *buf, size_t size)
718{
719 struct dev_ext_attribute *ea = to_ext_attr(attr);
720 char *end;
721 unsigned long new = simple_strtoul(buf, &end, 0);
722 if (end == buf)
723 return -EINVAL;
724 *(unsigned long *)(ea->var) = new;
725
726 return size;
727}
728EXPORT_SYMBOL_GPL(device_store_ulong);
729
730ssize_t device_show_ulong(struct device *dev,
731 struct device_attribute *attr,
732 char *buf)
733{
734 struct dev_ext_attribute *ea = to_ext_attr(attr);
735 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
736}
737EXPORT_SYMBOL_GPL(device_show_ulong);
738
739ssize_t device_store_int(struct device *dev,
740 struct device_attribute *attr,
741 const char *buf, size_t size)
742{
743 struct dev_ext_attribute *ea = to_ext_attr(attr);
744 char *end;
745 long new = simple_strtol(buf, &end, 0);
746 if (end == buf || new > INT_MAX || new < INT_MIN)
747 return -EINVAL;
748 *(int *)(ea->var) = new;
749
750 return size;
751}
752EXPORT_SYMBOL_GPL(device_store_int);
753
754ssize_t device_show_int(struct device *dev,
755 struct device_attribute *attr,
756 char *buf)
757{
758 struct dev_ext_attribute *ea = to_ext_attr(attr);
759
760 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
761}
762EXPORT_SYMBOL_GPL(device_show_int);
763
764ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
765 const char *buf, size_t size)
766{
767 struct dev_ext_attribute *ea = to_ext_attr(attr);
768
769 if (strtobool(buf, ea->var) < 0)
770 return -EINVAL;
771
772 return size;
773}
774EXPORT_SYMBOL_GPL(device_store_bool);
775
776ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
777 char *buf)
778{
779 struct dev_ext_attribute *ea = to_ext_attr(attr);
780
781 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
782}
783EXPORT_SYMBOL_GPL(device_show_bool);
784
785
786
787
788
789
790
791
792
793static void device_release(struct kobject *kobj)
794{
795 struct device *dev = kobj_to_dev(kobj);
796 struct device_private *p = dev->p;
797
798
799
800
801
802
803
804
805
806
807 devres_release_all(dev);
808
809 if (dev->release)
810 dev->release(dev);
811 else if (dev->type && dev->type->release)
812 dev->type->release(dev);
813 else if (dev->class && dev->class->dev_release)
814 dev->class->dev_release(dev);
815 else
816 WARN(1, KERN_ERR "Device '%s' does not have a release() "
817 "function, it is broken and must be fixed.\n",
818 dev_name(dev));
819 kfree(p);
820}
821
822static const void *device_namespace(struct kobject *kobj)
823{
824 struct device *dev = kobj_to_dev(kobj);
825 const void *ns = NULL;
826
827 if (dev->class && dev->class->ns_type)
828 ns = dev->class->namespace(dev);
829
830 return ns;
831}
832
833static struct kobj_type device_ktype = {
834 .release = device_release,
835 .sysfs_ops = &dev_sysfs_ops,
836 .namespace = device_namespace,
837};
838
839
840static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
841{
842 struct kobj_type *ktype = get_ktype(kobj);
843
844 if (ktype == &device_ktype) {
845 struct device *dev = kobj_to_dev(kobj);
846 if (dev->bus)
847 return 1;
848 if (dev->class)
849 return 1;
850 }
851 return 0;
852}
853
854static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
855{
856 struct device *dev = kobj_to_dev(kobj);
857
858 if (dev->bus)
859 return dev->bus->name;
860 if (dev->class)
861 return dev->class->name;
862 return NULL;
863}
864
865static int dev_uevent(struct kset *kset, struct kobject *kobj,
866 struct kobj_uevent_env *env)
867{
868 struct device *dev = kobj_to_dev(kobj);
869 int retval = 0;
870
871
872 if (MAJOR(dev->devt)) {
873 const char *tmp;
874 const char *name;
875 umode_t mode = 0;
876 kuid_t uid = GLOBAL_ROOT_UID;
877 kgid_t gid = GLOBAL_ROOT_GID;
878
879 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
880 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
881 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
882 if (name) {
883 add_uevent_var(env, "DEVNAME=%s", name);
884 if (mode)
885 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
886 if (!uid_eq(uid, GLOBAL_ROOT_UID))
887 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
888 if (!gid_eq(gid, GLOBAL_ROOT_GID))
889 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
890 kfree(tmp);
891 }
892 }
893
894 if (dev->type && dev->type->name)
895 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
896
897 if (dev->driver)
898 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
899
900
901 of_device_uevent(dev, env);
902
903
904 if (dev->bus && dev->bus->uevent) {
905 retval = dev->bus->uevent(dev, env);
906 if (retval)
907 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
908 dev_name(dev), __func__, retval);
909 }
910
911
912 if (dev->class && dev->class->dev_uevent) {
913 retval = dev->class->dev_uevent(dev, env);
914 if (retval)
915 pr_debug("device: '%s': %s: class uevent() "
916 "returned %d\n", dev_name(dev),
917 __func__, retval);
918 }
919
920
921 if (dev->type && dev->type->uevent) {
922 retval = dev->type->uevent(dev, env);
923 if (retval)
924 pr_debug("device: '%s': %s: dev_type uevent() "
925 "returned %d\n", dev_name(dev),
926 __func__, retval);
927 }
928
929 return retval;
930}
931
932static const struct kset_uevent_ops device_uevent_ops = {
933 .filter = dev_uevent_filter,
934 .name = dev_uevent_name,
935 .uevent = dev_uevent,
936};
937
938static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
939 char *buf)
940{
941 struct kobject *top_kobj;
942 struct kset *kset;
943 struct kobj_uevent_env *env = NULL;
944 int i;
945 size_t count = 0;
946 int retval;
947
948
949 top_kobj = &dev->kobj;
950 while (!top_kobj->kset && top_kobj->parent)
951 top_kobj = top_kobj->parent;
952 if (!top_kobj->kset)
953 goto out;
954
955 kset = top_kobj->kset;
956 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
957 goto out;
958
959
960 if (kset->uevent_ops && kset->uevent_ops->filter)
961 if (!kset->uevent_ops->filter(kset, &dev->kobj))
962 goto out;
963
964 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
965 if (!env)
966 return -ENOMEM;
967
968
969 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
970 if (retval)
971 goto out;
972
973
974 for (i = 0; i < env->envp_idx; i++)
975 count += sprintf(&buf[count], "%s\n", env->envp[i]);
976out:
977 kfree(env);
978 return count;
979}
980
981static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
982 const char *buf, size_t count)
983{
984 if (kobject_synth_uevent(&dev->kobj, buf, count))
985 dev_err(dev, "uevent: failed to send synthetic uevent\n");
986
987 return count;
988}
989static DEVICE_ATTR_RW(uevent);
990
991static ssize_t online_show(struct device *dev, struct device_attribute *attr,
992 char *buf)
993{
994 bool val;
995
996 device_lock(dev);
997 val = !dev->offline;
998 device_unlock(dev);
999 return sprintf(buf, "%u\n", val);
1000}
1001
1002static ssize_t online_store(struct device *dev, struct device_attribute *attr,
1003 const char *buf, size_t count)
1004{
1005 bool val;
1006 int ret;
1007
1008 ret = strtobool(buf, &val);
1009 if (ret < 0)
1010 return ret;
1011
1012 ret = lock_device_hotplug_sysfs();
1013 if (ret)
1014 return ret;
1015
1016 ret = val ? device_online(dev) : device_offline(dev);
1017 unlock_device_hotplug();
1018 return ret < 0 ? ret : count;
1019}
1020static DEVICE_ATTR_RW(online);
1021
1022int device_add_groups(struct device *dev, const struct attribute_group **groups)
1023{
1024 return sysfs_create_groups(&dev->kobj, groups);
1025}
1026EXPORT_SYMBOL_GPL(device_add_groups);
1027
1028void device_remove_groups(struct device *dev,
1029 const struct attribute_group **groups)
1030{
1031 sysfs_remove_groups(&dev->kobj, groups);
1032}
1033EXPORT_SYMBOL_GPL(device_remove_groups);
1034
1035union device_attr_group_devres {
1036 const struct attribute_group *group;
1037 const struct attribute_group **groups;
1038};
1039
1040static int devm_attr_group_match(struct device *dev, void *res, void *data)
1041{
1042 return ((union device_attr_group_devres *)res)->group == data;
1043}
1044
1045static void devm_attr_group_remove(struct device *dev, void *res)
1046{
1047 union device_attr_group_devres *devres = res;
1048 const struct attribute_group *group = devres->group;
1049
1050 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1051 sysfs_remove_group(&dev->kobj, group);
1052}
1053
1054static void devm_attr_groups_remove(struct device *dev, void *res)
1055{
1056 union device_attr_group_devres *devres = res;
1057 const struct attribute_group **groups = devres->groups;
1058
1059 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1060 sysfs_remove_groups(&dev->kobj, groups);
1061}
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1074{
1075 union device_attr_group_devres *devres;
1076 int error;
1077
1078 devres = devres_alloc(devm_attr_group_remove,
1079 sizeof(*devres), GFP_KERNEL);
1080 if (!devres)
1081 return -ENOMEM;
1082
1083 error = sysfs_create_group(&dev->kobj, grp);
1084 if (error) {
1085 devres_free(devres);
1086 return error;
1087 }
1088
1089 devres->group = grp;
1090 devres_add(dev, devres);
1091 return 0;
1092}
1093EXPORT_SYMBOL_GPL(devm_device_add_group);
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103void devm_device_remove_group(struct device *dev,
1104 const struct attribute_group *grp)
1105{
1106 WARN_ON(devres_release(dev, devm_attr_group_remove,
1107 devm_attr_group_match,
1108 (void *)grp));
1109}
1110EXPORT_SYMBOL_GPL(devm_device_remove_group);
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125int devm_device_add_groups(struct device *dev,
1126 const struct attribute_group **groups)
1127{
1128 union device_attr_group_devres *devres;
1129 int error;
1130
1131 devres = devres_alloc(devm_attr_groups_remove,
1132 sizeof(*devres), GFP_KERNEL);
1133 if (!devres)
1134 return -ENOMEM;
1135
1136 error = sysfs_create_groups(&dev->kobj, groups);
1137 if (error) {
1138 devres_free(devres);
1139 return error;
1140 }
1141
1142 devres->groups = groups;
1143 devres_add(dev, devres);
1144 return 0;
1145}
1146EXPORT_SYMBOL_GPL(devm_device_add_groups);
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156void devm_device_remove_groups(struct device *dev,
1157 const struct attribute_group **groups)
1158{
1159 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1160 devm_attr_group_match,
1161 (void *)groups));
1162}
1163EXPORT_SYMBOL_GPL(devm_device_remove_groups);
1164
1165static int device_add_attrs(struct device *dev)
1166{
1167 struct class *class = dev->class;
1168 const struct device_type *type = dev->type;
1169 int error;
1170
1171 if (class) {
1172 error = device_add_groups(dev, class->dev_groups);
1173 if (error)
1174 return error;
1175 }
1176
1177 if (type) {
1178 error = device_add_groups(dev, type->groups);
1179 if (error)
1180 goto err_remove_class_groups;
1181 }
1182
1183 error = device_add_groups(dev, dev->groups);
1184 if (error)
1185 goto err_remove_type_groups;
1186
1187 if (device_supports_offline(dev) && !dev->offline_disabled) {
1188 error = device_create_file(dev, &dev_attr_online);
1189 if (error)
1190 goto err_remove_dev_groups;
1191 }
1192
1193 return 0;
1194
1195 err_remove_dev_groups:
1196 device_remove_groups(dev, dev->groups);
1197 err_remove_type_groups:
1198 if (type)
1199 device_remove_groups(dev, type->groups);
1200 err_remove_class_groups:
1201 if (class)
1202 device_remove_groups(dev, class->dev_groups);
1203
1204 return error;
1205}
1206
1207static void device_remove_attrs(struct device *dev)
1208{
1209 struct class *class = dev->class;
1210 const struct device_type *type = dev->type;
1211
1212 device_remove_file(dev, &dev_attr_online);
1213 device_remove_groups(dev, dev->groups);
1214
1215 if (type)
1216 device_remove_groups(dev, type->groups);
1217
1218 if (class)
1219 device_remove_groups(dev, class->dev_groups);
1220}
1221
1222static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
1223 char *buf)
1224{
1225 return print_dev_t(buf, dev->devt);
1226}
1227static DEVICE_ATTR_RO(dev);
1228
1229
1230struct kset *devices_kset;
1231
1232
1233
1234
1235
1236
1237static void devices_kset_move_before(struct device *deva, struct device *devb)
1238{
1239 if (!devices_kset)
1240 return;
1241 pr_debug("devices_kset: Moving %s before %s\n",
1242 dev_name(deva), dev_name(devb));
1243 spin_lock(&devices_kset->list_lock);
1244 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1245 spin_unlock(&devices_kset->list_lock);
1246}
1247
1248
1249
1250
1251
1252
1253static void devices_kset_move_after(struct device *deva, struct device *devb)
1254{
1255 if (!devices_kset)
1256 return;
1257 pr_debug("devices_kset: Moving %s after %s\n",
1258 dev_name(deva), dev_name(devb));
1259 spin_lock(&devices_kset->list_lock);
1260 list_move(&deva->kobj.entry, &devb->kobj.entry);
1261 spin_unlock(&devices_kset->list_lock);
1262}
1263
1264
1265
1266
1267
1268void devices_kset_move_last(struct device *dev)
1269{
1270 if (!devices_kset)
1271 return;
1272 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1273 spin_lock(&devices_kset->list_lock);
1274 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1275 spin_unlock(&devices_kset->list_lock);
1276}
1277
1278
1279
1280
1281
1282
1283int device_create_file(struct device *dev,
1284 const struct device_attribute *attr)
1285{
1286 int error = 0;
1287
1288 if (dev) {
1289 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
1290 "Attribute %s: write permission without 'store'\n",
1291 attr->attr.name);
1292 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
1293 "Attribute %s: read permission without 'show'\n",
1294 attr->attr.name);
1295 error = sysfs_create_file(&dev->kobj, &attr->attr);
1296 }
1297
1298 return error;
1299}
1300EXPORT_SYMBOL_GPL(device_create_file);
1301
1302
1303
1304
1305
1306
1307void device_remove_file(struct device *dev,
1308 const struct device_attribute *attr)
1309{
1310 if (dev)
1311 sysfs_remove_file(&dev->kobj, &attr->attr);
1312}
1313EXPORT_SYMBOL_GPL(device_remove_file);
1314
1315
1316
1317
1318
1319
1320
1321
1322bool device_remove_file_self(struct device *dev,
1323 const struct device_attribute *attr)
1324{
1325 if (dev)
1326 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1327 else
1328 return false;
1329}
1330EXPORT_SYMBOL_GPL(device_remove_file_self);
1331
1332
1333
1334
1335
1336
1337int device_create_bin_file(struct device *dev,
1338 const struct bin_attribute *attr)
1339{
1340 int error = -EINVAL;
1341 if (dev)
1342 error = sysfs_create_bin_file(&dev->kobj, attr);
1343 return error;
1344}
1345EXPORT_SYMBOL_GPL(device_create_bin_file);
1346
1347
1348
1349
1350
1351
1352void device_remove_bin_file(struct device *dev,
1353 const struct bin_attribute *attr)
1354{
1355 if (dev)
1356 sysfs_remove_bin_file(&dev->kobj, attr);
1357}
1358EXPORT_SYMBOL_GPL(device_remove_bin_file);
1359
1360static void klist_children_get(struct klist_node *n)
1361{
1362 struct device_private *p = to_device_private_parent(n);
1363 struct device *dev = p->device;
1364
1365 get_device(dev);
1366}
1367
1368static void klist_children_put(struct klist_node *n)
1369{
1370 struct device_private *p = to_device_private_parent(n);
1371 struct device *dev = p->device;
1372
1373 put_device(dev);
1374}
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396void device_initialize(struct device *dev)
1397{
1398 dev->kobj.kset = devices_kset;
1399 kobject_init(&dev->kobj, &device_ktype);
1400 INIT_LIST_HEAD(&dev->dma_pools);
1401 mutex_init(&dev->mutex);
1402 lockdep_set_novalidate_class(&dev->mutex);
1403 spin_lock_init(&dev->devres_lock);
1404 INIT_LIST_HEAD(&dev->devres_head);
1405 device_pm_init(dev);
1406 set_dev_node(dev, -1);
1407#ifdef CONFIG_GENERIC_MSI_IRQ
1408 INIT_LIST_HEAD(&dev->msi_list);
1409#endif
1410 INIT_LIST_HEAD(&dev->links.consumers);
1411 INIT_LIST_HEAD(&dev->links.suppliers);
1412 dev->links.status = DL_DEV_NO_DRIVER;
1413}
1414EXPORT_SYMBOL_GPL(device_initialize);
1415
1416struct kobject *virtual_device_parent(struct device *dev)
1417{
1418 static struct kobject *virtual_dir = NULL;
1419
1420 if (!virtual_dir)
1421 virtual_dir = kobject_create_and_add("virtual",
1422 &devices_kset->kobj);
1423
1424 return virtual_dir;
1425}
1426
1427struct class_dir {
1428 struct kobject kobj;
1429 struct class *class;
1430};
1431
1432#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1433
1434static void class_dir_release(struct kobject *kobj)
1435{
1436 struct class_dir *dir = to_class_dir(kobj);
1437 kfree(dir);
1438}
1439
1440static const
1441struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1442{
1443 struct class_dir *dir = to_class_dir(kobj);
1444 return dir->class->ns_type;
1445}
1446
1447static struct kobj_type class_dir_ktype = {
1448 .release = class_dir_release,
1449 .sysfs_ops = &kobj_sysfs_ops,
1450 .child_ns_type = class_dir_child_ns_type
1451};
1452
1453static struct kobject *
1454class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1455{
1456 struct class_dir *dir;
1457 int retval;
1458
1459 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1460 if (!dir)
1461 return NULL;
1462
1463 dir->class = class;
1464 kobject_init(&dir->kobj, &class_dir_ktype);
1465
1466 dir->kobj.kset = &class->p->glue_dirs;
1467
1468 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1469 if (retval < 0) {
1470 kobject_put(&dir->kobj);
1471 return NULL;
1472 }
1473 return &dir->kobj;
1474}
1475
1476static DEFINE_MUTEX(gdp_mutex);
1477
1478static struct kobject *get_device_parent(struct device *dev,
1479 struct device *parent)
1480{
1481 if (dev->class) {
1482 struct kobject *kobj = NULL;
1483 struct kobject *parent_kobj;
1484 struct kobject *k;
1485
1486#ifdef CONFIG_BLOCK
1487
1488 if (sysfs_deprecated && dev->class == &block_class) {
1489 if (parent && parent->class == &block_class)
1490 return &parent->kobj;
1491 return &block_class.p->subsys.kobj;
1492 }
1493#endif
1494
1495
1496
1497
1498
1499
1500 if (parent == NULL)
1501 parent_kobj = virtual_device_parent(dev);
1502 else if (parent->class && !dev->class->ns_type)
1503 return &parent->kobj;
1504 else
1505 parent_kobj = &parent->kobj;
1506
1507 mutex_lock(&gdp_mutex);
1508
1509
1510 spin_lock(&dev->class->p->glue_dirs.list_lock);
1511 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
1512 if (k->parent == parent_kobj) {
1513 kobj = kobject_get(k);
1514 break;
1515 }
1516 spin_unlock(&dev->class->p->glue_dirs.list_lock);
1517 if (kobj) {
1518 mutex_unlock(&gdp_mutex);
1519 return kobj;
1520 }
1521
1522
1523 k = class_dir_create_and_add(dev->class, parent_kobj);
1524
1525 mutex_unlock(&gdp_mutex);
1526 return k;
1527 }
1528
1529
1530 if (!parent && dev->bus && dev->bus->dev_root)
1531 return &dev->bus->dev_root->kobj;
1532
1533 if (parent)
1534 return &parent->kobj;
1535 return NULL;
1536}
1537
1538static inline bool live_in_glue_dir(struct kobject *kobj,
1539 struct device *dev)
1540{
1541 if (!kobj || !dev->class ||
1542 kobj->kset != &dev->class->p->glue_dirs)
1543 return false;
1544 return true;
1545}
1546
1547static inline struct kobject *get_glue_dir(struct device *dev)
1548{
1549 return dev->kobj.parent;
1550}
1551
1552
1553
1554
1555
1556
1557static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
1558{
1559
1560 if (!live_in_glue_dir(glue_dir, dev))
1561 return;
1562
1563 mutex_lock(&gdp_mutex);
1564 kobject_put(glue_dir);
1565 mutex_unlock(&gdp_mutex);
1566}
1567
1568static int device_add_class_symlinks(struct device *dev)
1569{
1570 struct device_node *of_node = dev_of_node(dev);
1571 int error;
1572
1573 if (of_node) {
1574 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
1575 if (error)
1576 dev_warn(dev, "Error %d creating of_node link\n",error);
1577
1578 }
1579
1580 if (!dev->class)
1581 return 0;
1582
1583 error = sysfs_create_link(&dev->kobj,
1584 &dev->class->p->subsys.kobj,
1585 "subsystem");
1586 if (error)
1587 goto out_devnode;
1588
1589 if (dev->parent && device_is_not_partition(dev)) {
1590 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1591 "device");
1592 if (error)
1593 goto out_subsys;
1594 }
1595
1596#ifdef CONFIG_BLOCK
1597
1598 if (sysfs_deprecated && dev->class == &block_class)
1599 return 0;
1600#endif
1601
1602
1603 error = sysfs_create_link(&dev->class->p->subsys.kobj,
1604 &dev->kobj, dev_name(dev));
1605 if (error)
1606 goto out_device;
1607
1608 return 0;
1609
1610out_device:
1611 sysfs_remove_link(&dev->kobj, "device");
1612
1613out_subsys:
1614 sysfs_remove_link(&dev->kobj, "subsystem");
1615out_devnode:
1616 sysfs_remove_link(&dev->kobj, "of_node");
1617 return error;
1618}
1619
1620static void device_remove_class_symlinks(struct device *dev)
1621{
1622 if (dev_of_node(dev))
1623 sysfs_remove_link(&dev->kobj, "of_node");
1624
1625 if (!dev->class)
1626 return;
1627
1628 if (dev->parent && device_is_not_partition(dev))
1629 sysfs_remove_link(&dev->kobj, "device");
1630 sysfs_remove_link(&dev->kobj, "subsystem");
1631#ifdef CONFIG_BLOCK
1632 if (sysfs_deprecated && dev->class == &block_class)
1633 return;
1634#endif
1635 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
1636}
1637
1638
1639
1640
1641
1642
1643int dev_set_name(struct device *dev, const char *fmt, ...)
1644{
1645 va_list vargs;
1646 int err;
1647
1648 va_start(vargs, fmt);
1649 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
1650 va_end(vargs);
1651 return err;
1652}
1653EXPORT_SYMBOL_GPL(dev_set_name);
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666static struct kobject *device_to_dev_kobj(struct device *dev)
1667{
1668 struct kobject *kobj;
1669
1670 if (dev->class)
1671 kobj = dev->class->dev_kobj;
1672 else
1673 kobj = sysfs_dev_char_kobj;
1674
1675 return kobj;
1676}
1677
1678static int device_create_sys_dev_entry(struct device *dev)
1679{
1680 struct kobject *kobj = device_to_dev_kobj(dev);
1681 int error = 0;
1682 char devt_str[15];
1683
1684 if (kobj) {
1685 format_dev_t(devt_str, dev->devt);
1686 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1687 }
1688
1689 return error;
1690}
1691
1692static void device_remove_sys_dev_entry(struct device *dev)
1693{
1694 struct kobject *kobj = device_to_dev_kobj(dev);
1695 char devt_str[15];
1696
1697 if (kobj) {
1698 format_dev_t(devt_str, dev->devt);
1699 sysfs_remove_link(kobj, devt_str);
1700 }
1701}
1702
1703int device_private_init(struct device *dev)
1704{
1705 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1706 if (!dev->p)
1707 return -ENOMEM;
1708 dev->p->device = dev;
1709 klist_init(&dev->p->klist_children, klist_children_get,
1710 klist_children_put);
1711 INIT_LIST_HEAD(&dev->p->deferred_probe);
1712 return 0;
1713}
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737int device_add(struct device *dev)
1738{
1739 struct device *parent;
1740 struct kobject *kobj;
1741 struct class_interface *class_intf;
1742 int error = -EINVAL;
1743 struct kobject *glue_dir = NULL;
1744
1745 dev = get_device(dev);
1746 if (!dev)
1747 goto done;
1748
1749 if (!dev->p) {
1750 error = device_private_init(dev);
1751 if (error)
1752 goto done;
1753 }
1754
1755
1756
1757
1758
1759
1760 if (dev->init_name) {
1761 dev_set_name(dev, "%s", dev->init_name);
1762 dev->init_name = NULL;
1763 }
1764
1765
1766 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1767 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1768
1769 if (!dev_name(dev)) {
1770 error = -EINVAL;
1771 goto name_error;
1772 }
1773
1774 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1775
1776 parent = get_device(dev->parent);
1777 kobj = get_device_parent(dev, parent);
1778 if (kobj)
1779 dev->kobj.parent = kobj;
1780
1781
1782 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
1783 set_dev_node(dev, dev_to_node(parent));
1784
1785
1786
1787 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
1788 if (error) {
1789 glue_dir = get_glue_dir(dev);
1790 goto Error;
1791 }
1792
1793
1794 if (platform_notify)
1795 platform_notify(dev);
1796
1797 error = device_create_file(dev, &dev_attr_uevent);
1798 if (error)
1799 goto attrError;
1800
1801 error = device_add_class_symlinks(dev);
1802 if (error)
1803 goto SymlinkError;
1804 error = device_add_attrs(dev);
1805 if (error)
1806 goto AttrsError;
1807 error = bus_add_device(dev);
1808 if (error)
1809 goto BusError;
1810 error = dpm_sysfs_add(dev);
1811 if (error)
1812 goto DPMError;
1813 device_pm_add(dev);
1814
1815 if (MAJOR(dev->devt)) {
1816 error = device_create_file(dev, &dev_attr_dev);
1817 if (error)
1818 goto DevAttrError;
1819
1820 error = device_create_sys_dev_entry(dev);
1821 if (error)
1822 goto SysEntryError;
1823
1824 devtmpfs_create_node(dev);
1825 }
1826
1827
1828
1829
1830 if (dev->bus)
1831 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1832 BUS_NOTIFY_ADD_DEVICE, dev);
1833
1834 kobject_uevent(&dev->kobj, KOBJ_ADD);
1835 bus_probe_device(dev);
1836 if (parent)
1837 klist_add_tail(&dev->p->knode_parent,
1838 &parent->p->klist_children);
1839
1840 if (dev->class) {
1841 mutex_lock(&dev->class->p->mutex);
1842
1843 klist_add_tail(&dev->knode_class,
1844 &dev->class->p->klist_devices);
1845
1846
1847 list_for_each_entry(class_intf,
1848 &dev->class->p->interfaces, node)
1849 if (class_intf->add_dev)
1850 class_intf->add_dev(dev, class_intf);
1851 mutex_unlock(&dev->class->p->mutex);
1852 }
1853done:
1854 put_device(dev);
1855 return error;
1856 SysEntryError:
1857 if (MAJOR(dev->devt))
1858 device_remove_file(dev, &dev_attr_dev);
1859 DevAttrError:
1860 device_pm_remove(dev);
1861 dpm_sysfs_remove(dev);
1862 DPMError:
1863 bus_remove_device(dev);
1864 BusError:
1865 device_remove_attrs(dev);
1866 AttrsError:
1867 device_remove_class_symlinks(dev);
1868 SymlinkError:
1869 device_remove_file(dev, &dev_attr_uevent);
1870 attrError:
1871 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1872 glue_dir = get_glue_dir(dev);
1873 kobject_del(&dev->kobj);
1874 Error:
1875 cleanup_glue_dir(dev, glue_dir);
1876 put_device(parent);
1877name_error:
1878 kfree(dev->p);
1879 dev->p = NULL;
1880 goto done;
1881}
1882EXPORT_SYMBOL_GPL(device_add);
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902int device_register(struct device *dev)
1903{
1904 device_initialize(dev);
1905 return device_add(dev);
1906}
1907EXPORT_SYMBOL_GPL(device_register);
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917struct device *get_device(struct device *dev)
1918{
1919 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1920}
1921EXPORT_SYMBOL_GPL(get_device);
1922
1923
1924
1925
1926
1927void put_device(struct device *dev)
1928{
1929
1930 if (dev)
1931 kobject_put(&dev->kobj);
1932}
1933EXPORT_SYMBOL_GPL(put_device);
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948void device_del(struct device *dev)
1949{
1950 struct device *parent = dev->parent;
1951 struct kobject *glue_dir = NULL;
1952 struct class_interface *class_intf;
1953
1954
1955
1956
1957 if (dev->bus)
1958 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1959 BUS_NOTIFY_DEL_DEVICE, dev);
1960
1961 dpm_sysfs_remove(dev);
1962 if (parent)
1963 klist_del(&dev->p->knode_parent);
1964 if (MAJOR(dev->devt)) {
1965 devtmpfs_delete_node(dev);
1966 device_remove_sys_dev_entry(dev);
1967 device_remove_file(dev, &dev_attr_dev);
1968 }
1969 if (dev->class) {
1970 device_remove_class_symlinks(dev);
1971
1972 mutex_lock(&dev->class->p->mutex);
1973
1974 list_for_each_entry(class_intf,
1975 &dev->class->p->interfaces, node)
1976 if (class_intf->remove_dev)
1977 class_intf->remove_dev(dev, class_intf);
1978
1979 klist_del(&dev->knode_class);
1980 mutex_unlock(&dev->class->p->mutex);
1981 }
1982 device_remove_file(dev, &dev_attr_uevent);
1983 device_remove_attrs(dev);
1984 bus_remove_device(dev);
1985 device_pm_remove(dev);
1986 driver_deferred_probe_del(dev);
1987 device_remove_properties(dev);
1988 device_links_purge(dev);
1989
1990
1991
1992
1993 if (platform_notify_remove)
1994 platform_notify_remove(dev);
1995 if (dev->bus)
1996 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1997 BUS_NOTIFY_REMOVED_DEVICE, dev);
1998 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1999 glue_dir = get_glue_dir(dev);
2000 kobject_del(&dev->kobj);
2001 cleanup_glue_dir(dev, glue_dir);
2002 put_device(parent);
2003}
2004EXPORT_SYMBOL_GPL(device_del);
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017void device_unregister(struct device *dev)
2018{
2019 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2020 device_del(dev);
2021 put_device(dev);
2022}
2023EXPORT_SYMBOL_GPL(device_unregister);
2024
2025static struct device *prev_device(struct klist_iter *i)
2026{
2027 struct klist_node *n = klist_prev(i);
2028 struct device *dev = NULL;
2029 struct device_private *p;
2030
2031 if (n) {
2032 p = to_device_private_parent(n);
2033 dev = p->device;
2034 }
2035 return dev;
2036}
2037
2038static struct device *next_device(struct klist_iter *i)
2039{
2040 struct klist_node *n = klist_next(i);
2041 struct device *dev = NULL;
2042 struct device_private *p;
2043
2044 if (n) {
2045 p = to_device_private_parent(n);
2046 dev = p->device;
2047 }
2048 return dev;
2049}
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064const char *device_get_devnode(struct device *dev,
2065 umode_t *mode, kuid_t *uid, kgid_t *gid,
2066 const char **tmp)
2067{
2068 char *s;
2069
2070 *tmp = NULL;
2071
2072
2073 if (dev->type && dev->type->devnode)
2074 *tmp = dev->type->devnode(dev, mode, uid, gid);
2075 if (*tmp)
2076 return *tmp;
2077
2078
2079 if (dev->class && dev->class->devnode)
2080 *tmp = dev->class->devnode(dev, mode);
2081 if (*tmp)
2082 return *tmp;
2083
2084
2085 if (strchr(dev_name(dev), '!') == NULL)
2086 return dev_name(dev);
2087
2088
2089 s = kstrdup(dev_name(dev), GFP_KERNEL);
2090 if (!s)
2091 return NULL;
2092 strreplace(s, '!', '/');
2093 return *tmp = s;
2094}
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108int device_for_each_child(struct device *parent, void *data,
2109 int (*fn)(struct device *dev, void *data))
2110{
2111 struct klist_iter i;
2112 struct device *child;
2113 int error = 0;
2114
2115 if (!parent->p)
2116 return 0;
2117
2118 klist_iter_init(&parent->p->klist_children, &i);
2119 while (!error && (child = next_device(&i)))
2120 error = fn(child, data);
2121 klist_iter_exit(&i);
2122 return error;
2123}
2124EXPORT_SYMBOL_GPL(device_for_each_child);
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138int device_for_each_child_reverse(struct device *parent, void *data,
2139 int (*fn)(struct device *dev, void *data))
2140{
2141 struct klist_iter i;
2142 struct device *child;
2143 int error = 0;
2144
2145 if (!parent->p)
2146 return 0;
2147
2148 klist_iter_init(&parent->p->klist_children, &i);
2149 while ((child = prev_device(&i)) && !error)
2150 error = fn(child, data);
2151 klist_iter_exit(&i);
2152 return error;
2153}
2154EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173struct device *device_find_child(struct device *parent, void *data,
2174 int (*match)(struct device *dev, void *data))
2175{
2176 struct klist_iter i;
2177 struct device *child;
2178
2179 if (!parent)
2180 return NULL;
2181
2182 klist_iter_init(&parent->p->klist_children, &i);
2183 while ((child = next_device(&i)))
2184 if (match(child, data) && get_device(child))
2185 break;
2186 klist_iter_exit(&i);
2187 return child;
2188}
2189EXPORT_SYMBOL_GPL(device_find_child);
2190
2191int __init devices_init(void)
2192{
2193 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2194 if (!devices_kset)
2195 return -ENOMEM;
2196 dev_kobj = kobject_create_and_add("dev", NULL);
2197 if (!dev_kobj)
2198 goto dev_kobj_err;
2199 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2200 if (!sysfs_dev_block_kobj)
2201 goto block_kobj_err;
2202 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2203 if (!sysfs_dev_char_kobj)
2204 goto char_kobj_err;
2205
2206 return 0;
2207
2208 char_kobj_err:
2209 kobject_put(sysfs_dev_block_kobj);
2210 block_kobj_err:
2211 kobject_put(dev_kobj);
2212 dev_kobj_err:
2213 kset_unregister(devices_kset);
2214 return -ENOMEM;
2215}
2216
2217static int device_check_offline(struct device *dev, void *not_used)
2218{
2219 int ret;
2220
2221 ret = device_for_each_child(dev, NULL, device_check_offline);
2222 if (ret)
2223 return ret;
2224
2225 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2226}
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239int device_offline(struct device *dev)
2240{
2241 int ret;
2242
2243 if (dev->offline_disabled)
2244 return -EPERM;
2245
2246 ret = device_for_each_child(dev, NULL, device_check_offline);
2247 if (ret)
2248 return ret;
2249
2250 device_lock(dev);
2251 if (device_supports_offline(dev)) {
2252 if (dev->offline) {
2253 ret = 1;
2254 } else {
2255 ret = dev->bus->offline(dev);
2256 if (!ret) {
2257 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2258 dev->offline = true;
2259 }
2260 }
2261 }
2262 device_unlock(dev);
2263
2264 return ret;
2265}
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277int device_online(struct device *dev)
2278{
2279 int ret = 0;
2280
2281 device_lock(dev);
2282 if (device_supports_offline(dev)) {
2283 if (dev->offline) {
2284 ret = dev->bus->online(dev);
2285 if (!ret) {
2286 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2287 dev->offline = false;
2288 }
2289 } else {
2290 ret = 1;
2291 }
2292 }
2293 device_unlock(dev);
2294
2295 return ret;
2296}
2297
2298struct root_device {
2299 struct device dev;
2300 struct module *owner;
2301};
2302
2303static inline struct root_device *to_root_device(struct device *d)
2304{
2305 return container_of(d, struct root_device, dev);
2306}
2307
2308static void root_device_release(struct device *dev)
2309{
2310 kfree(to_root_device(dev));
2311}
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335struct device *__root_device_register(const char *name, struct module *owner)
2336{
2337 struct root_device *root;
2338 int err = -ENOMEM;
2339
2340 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2341 if (!root)
2342 return ERR_PTR(err);
2343
2344 err = dev_set_name(&root->dev, "%s", name);
2345 if (err) {
2346 kfree(root);
2347 return ERR_PTR(err);
2348 }
2349
2350 root->dev.release = root_device_release;
2351
2352 err = device_register(&root->dev);
2353 if (err) {
2354 put_device(&root->dev);
2355 return ERR_PTR(err);
2356 }
2357
2358#ifdef CONFIG_MODULES
2359 if (owner) {
2360 struct module_kobject *mk = &owner->mkobj;
2361
2362 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2363 if (err) {
2364 device_unregister(&root->dev);
2365 return ERR_PTR(err);
2366 }
2367 root->owner = owner;
2368 }
2369#endif
2370
2371 return &root->dev;
2372}
2373EXPORT_SYMBOL_GPL(__root_device_register);
2374
2375
2376
2377
2378
2379
2380
2381
2382void root_device_unregister(struct device *dev)
2383{
2384 struct root_device *root = to_root_device(dev);
2385
2386 if (root->owner)
2387 sysfs_remove_link(&root->dev.kobj, "module");
2388
2389 device_unregister(dev);
2390}
2391EXPORT_SYMBOL_GPL(root_device_unregister);
2392
2393
2394static void device_create_release(struct device *dev)
2395{
2396 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2397 kfree(dev);
2398}
2399
2400static struct device *
2401device_create_groups_vargs(struct class *class, struct device *parent,
2402 dev_t devt, void *drvdata,
2403 const struct attribute_group **groups,
2404 const char *fmt, va_list args)
2405{
2406 struct device *dev = NULL;
2407 int retval = -ENODEV;
2408
2409 if (class == NULL || IS_ERR(class))
2410 goto error;
2411
2412 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2413 if (!dev) {
2414 retval = -ENOMEM;
2415 goto error;
2416 }
2417
2418 device_initialize(dev);
2419 dev->devt = devt;
2420 dev->class = class;
2421 dev->parent = parent;
2422 dev->groups = groups;
2423 dev->release = device_create_release;
2424 dev_set_drvdata(dev, drvdata);
2425
2426 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2427 if (retval)
2428 goto error;
2429
2430 retval = device_add(dev);
2431 if (retval)
2432 goto error;
2433
2434 return dev;
2435
2436error:
2437 put_device(dev);
2438 return ERR_PTR(retval);
2439}
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466struct device *device_create_vargs(struct class *class, struct device *parent,
2467 dev_t devt, void *drvdata, const char *fmt,
2468 va_list args)
2469{
2470 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2471 fmt, args);
2472}
2473EXPORT_SYMBOL_GPL(device_create_vargs);
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499struct device *device_create(struct class *class, struct device *parent,
2500 dev_t devt, void *drvdata, const char *fmt, ...)
2501{
2502 va_list vargs;
2503 struct device *dev;
2504
2505 va_start(vargs, fmt);
2506 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2507 va_end(vargs);
2508 return dev;
2509}
2510EXPORT_SYMBOL_GPL(device_create);
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539struct device *device_create_with_groups(struct class *class,
2540 struct device *parent, dev_t devt,
2541 void *drvdata,
2542 const struct attribute_group **groups,
2543 const char *fmt, ...)
2544{
2545 va_list vargs;
2546 struct device *dev;
2547
2548 va_start(vargs, fmt);
2549 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2550 fmt, vargs);
2551 va_end(vargs);
2552 return dev;
2553}
2554EXPORT_SYMBOL_GPL(device_create_with_groups);
2555
2556static int __match_devt(struct device *dev, const void *data)
2557{
2558 const dev_t *devt = data;
2559
2560 return dev->devt == *devt;
2561}
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571void device_destroy(struct class *class, dev_t devt)
2572{
2573 struct device *dev;
2574
2575 dev = class_find_device(class, NULL, &devt, __match_devt);
2576 if (dev) {
2577 put_device(dev);
2578 device_unregister(dev);
2579 }
2580}
2581EXPORT_SYMBOL_GPL(device_destroy);
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622int device_rename(struct device *dev, const char *new_name)
2623{
2624 struct kobject *kobj = &dev->kobj;
2625 char *old_device_name = NULL;
2626 int error;
2627
2628 dev = get_device(dev);
2629 if (!dev)
2630 return -EINVAL;
2631
2632 dev_dbg(dev, "renaming to %s\n", new_name);
2633
2634 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
2635 if (!old_device_name) {
2636 error = -ENOMEM;
2637 goto out;
2638 }
2639
2640 if (dev->class) {
2641 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2642 kobj, old_device_name,
2643 new_name, kobject_namespace(kobj));
2644 if (error)
2645 goto out;
2646 }
2647
2648 error = kobject_rename(kobj, new_name);
2649 if (error)
2650 goto out;
2651
2652out:
2653 put_device(dev);
2654
2655 kfree(old_device_name);
2656
2657 return error;
2658}
2659EXPORT_SYMBOL_GPL(device_rename);
2660
2661static int device_move_class_links(struct device *dev,
2662 struct device *old_parent,
2663 struct device *new_parent)
2664{
2665 int error = 0;
2666
2667 if (old_parent)
2668 sysfs_remove_link(&dev->kobj, "device");
2669 if (new_parent)
2670 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2671 "device");
2672 return error;
2673}
2674
2675
2676
2677
2678
2679
2680
2681int device_move(struct device *dev, struct device *new_parent,
2682 enum dpm_order dpm_order)
2683{
2684 int error;
2685 struct device *old_parent;
2686 struct kobject *new_parent_kobj;
2687
2688 dev = get_device(dev);
2689 if (!dev)
2690 return -EINVAL;
2691
2692 device_pm_lock();
2693 new_parent = get_device(new_parent);
2694 new_parent_kobj = get_device_parent(dev, new_parent);
2695
2696 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2697 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
2698 error = kobject_move(&dev->kobj, new_parent_kobj);
2699 if (error) {
2700 cleanup_glue_dir(dev, new_parent_kobj);
2701 put_device(new_parent);
2702 goto out;
2703 }
2704 old_parent = dev->parent;
2705 dev->parent = new_parent;
2706 if (old_parent)
2707 klist_remove(&dev->p->knode_parent);
2708 if (new_parent) {
2709 klist_add_tail(&dev->p->knode_parent,
2710 &new_parent->p->klist_children);
2711 set_dev_node(dev, dev_to_node(new_parent));
2712 }
2713
2714 if (dev->class) {
2715 error = device_move_class_links(dev, old_parent, new_parent);
2716 if (error) {
2717
2718 device_move_class_links(dev, new_parent, old_parent);
2719 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2720 if (new_parent)
2721 klist_remove(&dev->p->knode_parent);
2722 dev->parent = old_parent;
2723 if (old_parent) {
2724 klist_add_tail(&dev->p->knode_parent,
2725 &old_parent->p->klist_children);
2726 set_dev_node(dev, dev_to_node(old_parent));
2727 }
2728 }
2729 cleanup_glue_dir(dev, new_parent_kobj);
2730 put_device(new_parent);
2731 goto out;
2732 }
2733 }
2734 switch (dpm_order) {
2735 case DPM_ORDER_NONE:
2736 break;
2737 case DPM_ORDER_DEV_AFTER_PARENT:
2738 device_pm_move_after(dev, new_parent);
2739 devices_kset_move_after(dev, new_parent);
2740 break;
2741 case DPM_ORDER_PARENT_BEFORE_DEV:
2742 device_pm_move_before(new_parent, dev);
2743 devices_kset_move_before(new_parent, dev);
2744 break;
2745 case DPM_ORDER_DEV_LAST:
2746 device_pm_move_last(dev);
2747 devices_kset_move_last(dev);
2748 break;
2749 }
2750
2751 put_device(old_parent);
2752out:
2753 device_pm_unlock();
2754 put_device(dev);
2755 return error;
2756}
2757EXPORT_SYMBOL_GPL(device_move);
2758
2759
2760
2761
2762void device_shutdown(void)
2763{
2764 struct device *dev, *parent;
2765
2766 spin_lock(&devices_kset->list_lock);
2767
2768
2769
2770
2771
2772 while (!list_empty(&devices_kset->list)) {
2773 dev = list_entry(devices_kset->list.prev, struct device,
2774 kobj.entry);
2775
2776
2777
2778
2779
2780
2781 parent = get_device(dev->parent);
2782 get_device(dev);
2783
2784
2785
2786
2787 list_del_init(&dev->kobj.entry);
2788 spin_unlock(&devices_kset->list_lock);
2789
2790
2791 if (parent)
2792 device_lock(parent);
2793 device_lock(dev);
2794
2795
2796 pm_runtime_get_noresume(dev);
2797 pm_runtime_barrier(dev);
2798
2799 if (dev->class && dev->class->shutdown_pre) {
2800 if (initcall_debug)
2801 dev_info(dev, "shutdown_pre\n");
2802 dev->class->shutdown_pre(dev);
2803 }
2804 if (dev->bus && dev->bus->shutdown) {
2805 if (initcall_debug)
2806 dev_info(dev, "shutdown\n");
2807 dev->bus->shutdown(dev);
2808 } else if (dev->driver && dev->driver->shutdown) {
2809 if (initcall_debug)
2810 dev_info(dev, "shutdown\n");
2811 dev->driver->shutdown(dev);
2812 }
2813
2814 device_unlock(dev);
2815 if (parent)
2816 device_unlock(parent);
2817
2818 put_device(dev);
2819 put_device(parent);
2820
2821 spin_lock(&devices_kset->list_lock);
2822 }
2823 spin_unlock(&devices_kset->list_lock);
2824}
2825
2826
2827
2828
2829
2830#ifdef CONFIG_PRINTK
2831static int
2832create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
2833{
2834 const char *subsys;
2835 size_t pos = 0;
2836
2837 if (dev->class)
2838 subsys = dev->class->name;
2839 else if (dev->bus)
2840 subsys = dev->bus->name;
2841 else
2842 return 0;
2843
2844 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
2845 if (pos >= hdrlen)
2846 goto overflow;
2847
2848
2849
2850
2851
2852
2853
2854
2855 if (MAJOR(dev->devt)) {
2856 char c;
2857
2858 if (strcmp(subsys, "block") == 0)
2859 c = 'b';
2860 else
2861 c = 'c';
2862 pos++;
2863 pos += snprintf(hdr + pos, hdrlen - pos,
2864 "DEVICE=%c%u:%u",
2865 c, MAJOR(dev->devt), MINOR(dev->devt));
2866 } else if (strcmp(subsys, "net") == 0) {
2867 struct net_device *net = to_net_dev(dev);
2868
2869 pos++;
2870 pos += snprintf(hdr + pos, hdrlen - pos,
2871 "DEVICE=n%u", net->ifindex);
2872 } else {
2873 pos++;
2874 pos += snprintf(hdr + pos, hdrlen - pos,
2875 "DEVICE=+%s:%s", subsys, dev_name(dev));
2876 }
2877
2878 if (pos >= hdrlen)
2879 goto overflow;
2880
2881 return pos;
2882
2883overflow:
2884 dev_WARN(dev, "device/subsystem name too long");
2885 return 0;
2886}
2887
2888int dev_vprintk_emit(int level, const struct device *dev,
2889 const char *fmt, va_list args)
2890{
2891 char hdr[128];
2892 size_t hdrlen;
2893
2894 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2895
2896 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2897}
2898EXPORT_SYMBOL(dev_vprintk_emit);
2899
2900int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2901{
2902 va_list args;
2903 int r;
2904
2905 va_start(args, fmt);
2906
2907 r = dev_vprintk_emit(level, dev, fmt, args);
2908
2909 va_end(args);
2910
2911 return r;
2912}
2913EXPORT_SYMBOL(dev_printk_emit);
2914
2915static void __dev_printk(const char *level, const struct device *dev,
2916 struct va_format *vaf)
2917{
2918 if (dev)
2919 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2920 dev_driver_string(dev), dev_name(dev), vaf);
2921 else
2922 printk("%s(NULL device *): %pV", level, vaf);
2923}
2924
2925void dev_printk(const char *level, const struct device *dev,
2926 const char *fmt, ...)
2927{
2928 struct va_format vaf;
2929 va_list args;
2930
2931 va_start(args, fmt);
2932
2933 vaf.fmt = fmt;
2934 vaf.va = &args;
2935
2936 __dev_printk(level, dev, &vaf);
2937
2938 va_end(args);
2939}
2940EXPORT_SYMBOL(dev_printk);
2941
2942#define define_dev_printk_level(func, kern_level) \
2943void func(const struct device *dev, const char *fmt, ...) \
2944{ \
2945 struct va_format vaf; \
2946 va_list args; \
2947 \
2948 va_start(args, fmt); \
2949 \
2950 vaf.fmt = fmt; \
2951 vaf.va = &args; \
2952 \
2953 __dev_printk(kern_level, dev, &vaf); \
2954 \
2955 va_end(args); \
2956} \
2957EXPORT_SYMBOL(func);
2958
2959define_dev_printk_level(dev_emerg, KERN_EMERG);
2960define_dev_printk_level(dev_alert, KERN_ALERT);
2961define_dev_printk_level(dev_crit, KERN_CRIT);
2962define_dev_printk_level(dev_err, KERN_ERR);
2963define_dev_printk_level(dev_warn, KERN_WARNING);
2964define_dev_printk_level(dev_notice, KERN_NOTICE);
2965define_dev_printk_level(_dev_info, KERN_INFO);
2966
2967#endif
2968
2969static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
2970{
2971 return fwnode && !IS_ERR(fwnode->secondary);
2972}
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2983{
2984 if (fwnode) {
2985 struct fwnode_handle *fn = dev->fwnode;
2986
2987 if (fwnode_is_primary(fn))
2988 fn = fn->secondary;
2989
2990 if (fn) {
2991 WARN_ON(fwnode->secondary);
2992 fwnode->secondary = fn;
2993 }
2994 dev->fwnode = fwnode;
2995 } else {
2996 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
2997 dev->fwnode->secondary : NULL;
2998 }
2999}
3000EXPORT_SYMBOL_GPL(set_primary_fwnode);
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3012{
3013 if (fwnode)
3014 fwnode->secondary = ERR_PTR(-ENODEV);
3015
3016 if (fwnode_is_primary(dev->fwnode))
3017 dev->fwnode->secondary = fwnode;
3018 else
3019 dev->fwnode = fwnode;
3020}
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3031{
3032 of_node_put(dev->of_node);
3033 dev->of_node = of_node_get(dev2->of_node);
3034 dev->of_node_reused = true;
3035}
3036EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
3037