1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/device.h>
14#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/string.h>
19#include <linux/kdev_t.h>
20#include <linux/notifier.h>
21#include <linux/of.h>
22#include <linux/of_device.h>
23#include <linux/genhd.h>
24#include <linux/kallsyms.h>
25#include <linux/mutex.h>
26#include <linux/async.h>
27#include <linux/pm_runtime.h>
28#include <linux/netdevice.h>
29
30#include "base.h"
31#include "power/power.h"
32
33#ifdef CONFIG_SYSFS_DEPRECATED
34#ifdef CONFIG_SYSFS_DEPRECATED_V2
35long sysfs_deprecated = 1;
36#else
37long sysfs_deprecated = 0;
38#endif
39static __init int sysfs_deprecated_setup(char *arg)
40{
41 return strict_strtol(arg, 10, &sysfs_deprecated);
42}
43early_param("sysfs.deprecated", sysfs_deprecated_setup);
44#endif
45
46int (*platform_notify)(struct device *dev) = NULL;
47int (*platform_notify_remove)(struct device *dev) = NULL;
48static struct kobject *dev_kobj;
49struct kobject *sysfs_dev_char_kobj;
50struct kobject *sysfs_dev_block_kobj;
51
52#ifdef CONFIG_BLOCK
53static inline int device_is_not_partition(struct device *dev)
54{
55 return !(dev->type == &part_type);
56}
57#else
58static inline int device_is_not_partition(struct device *dev)
59{
60 return 1;
61}
62#endif
63
64
65
66
67
68
69
70
71
72
73const char *dev_driver_string(const struct device *dev)
74{
75 struct device_driver *drv;
76
77
78
79
80
81 drv = ACCESS_ONCE(dev->driver);
82 return drv ? drv->name :
83 (dev->bus ? dev->bus->name :
84 (dev->class ? dev->class->name : ""));
85}
86EXPORT_SYMBOL(dev_driver_string);
87
88#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
89
90static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
91 char *buf)
92{
93 struct device_attribute *dev_attr = to_dev_attr(attr);
94 struct device *dev = kobj_to_dev(kobj);
95 ssize_t ret = -EIO;
96
97 if (dev_attr->show)
98 ret = dev_attr->show(dev, dev_attr, buf);
99 if (ret >= (ssize_t)PAGE_SIZE) {
100 print_symbol("dev_attr_show: %s returned bad count\n",
101 (unsigned long)dev_attr->show);
102 }
103 return ret;
104}
105
106static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
107 const char *buf, size_t count)
108{
109 struct device_attribute *dev_attr = to_dev_attr(attr);
110 struct device *dev = kobj_to_dev(kobj);
111 ssize_t ret = -EIO;
112
113 if (dev_attr->store)
114 ret = dev_attr->store(dev, dev_attr, buf, count);
115 return ret;
116}
117
118static const struct sysfs_ops dev_sysfs_ops = {
119 .show = dev_attr_show,
120 .store = dev_attr_store,
121};
122
123#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
124
125ssize_t device_store_ulong(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t size)
128{
129 struct dev_ext_attribute *ea = to_ext_attr(attr);
130 char *end;
131 unsigned long new = simple_strtoul(buf, &end, 0);
132 if (end == buf)
133 return -EINVAL;
134 *(unsigned long *)(ea->var) = new;
135
136 return size;
137}
138EXPORT_SYMBOL_GPL(device_store_ulong);
139
140ssize_t device_show_ulong(struct device *dev,
141 struct device_attribute *attr,
142 char *buf)
143{
144 struct dev_ext_attribute *ea = to_ext_attr(attr);
145 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
146}
147EXPORT_SYMBOL_GPL(device_show_ulong);
148
149ssize_t device_store_int(struct device *dev,
150 struct device_attribute *attr,
151 const char *buf, size_t size)
152{
153 struct dev_ext_attribute *ea = to_ext_attr(attr);
154 char *end;
155 long new = simple_strtol(buf, &end, 0);
156 if (end == buf || new > INT_MAX || new < INT_MIN)
157 return -EINVAL;
158 *(int *)(ea->var) = new;
159
160 return size;
161}
162EXPORT_SYMBOL_GPL(device_store_int);
163
164ssize_t device_show_int(struct device *dev,
165 struct device_attribute *attr,
166 char *buf)
167{
168 struct dev_ext_attribute *ea = to_ext_attr(attr);
169
170 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
171}
172EXPORT_SYMBOL_GPL(device_show_int);
173
174
175
176
177
178
179
180
181
182static void device_release(struct kobject *kobj)
183{
184 struct device *dev = kobj_to_dev(kobj);
185 struct device_private *p = dev->p;
186
187 if (dev->release)
188 dev->release(dev);
189 else if (dev->type && dev->type->release)
190 dev->type->release(dev);
191 else if (dev->class && dev->class->dev_release)
192 dev->class->dev_release(dev);
193 else
194 WARN(1, KERN_ERR "Device '%s' does not have a release() "
195 "function, it is broken and must be fixed.\n",
196 dev_name(dev));
197 kfree(p);
198}
199
200static const void *device_namespace(struct kobject *kobj)
201{
202 struct device *dev = kobj_to_dev(kobj);
203 const void *ns = NULL;
204
205 if (dev->class && dev->class->ns_type)
206 ns = dev->class->namespace(dev);
207
208 return ns;
209}
210
211static struct kobj_type device_ktype = {
212 .release = device_release,
213 .sysfs_ops = &dev_sysfs_ops,
214 .namespace = device_namespace,
215};
216
217
218static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
219{
220 struct kobj_type *ktype = get_ktype(kobj);
221
222 if (ktype == &device_ktype) {
223 struct device *dev = kobj_to_dev(kobj);
224 if (dev->bus)
225 return 1;
226 if (dev->class)
227 return 1;
228 }
229 return 0;
230}
231
232static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
233{
234 struct device *dev = kobj_to_dev(kobj);
235
236 if (dev->bus)
237 return dev->bus->name;
238 if (dev->class)
239 return dev->class->name;
240 return NULL;
241}
242
243static int dev_uevent(struct kset *kset, struct kobject *kobj,
244 struct kobj_uevent_env *env)
245{
246 struct device *dev = kobj_to_dev(kobj);
247 int retval = 0;
248
249
250 if (MAJOR(dev->devt)) {
251 const char *tmp;
252 const char *name;
253 umode_t mode = 0;
254
255 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
256 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
257 name = device_get_devnode(dev, &mode, &tmp);
258 if (name) {
259 add_uevent_var(env, "DEVNAME=%s", name);
260 kfree(tmp);
261 if (mode)
262 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
263 }
264 }
265
266 if (dev->type && dev->type->name)
267 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
268
269 if (dev->driver)
270 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
271
272
273 of_device_uevent(dev, env);
274
275
276 if (dev->bus && dev->bus->uevent) {
277 retval = dev->bus->uevent(dev, env);
278 if (retval)
279 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
280 dev_name(dev), __func__, retval);
281 }
282
283
284 if (dev->class && dev->class->dev_uevent) {
285 retval = dev->class->dev_uevent(dev, env);
286 if (retval)
287 pr_debug("device: '%s': %s: class uevent() "
288 "returned %d\n", dev_name(dev),
289 __func__, retval);
290 }
291
292
293 if (dev->type && dev->type->uevent) {
294 retval = dev->type->uevent(dev, env);
295 if (retval)
296 pr_debug("device: '%s': %s: dev_type uevent() "
297 "returned %d\n", dev_name(dev),
298 __func__, retval);
299 }
300
301 return retval;
302}
303
304static const struct kset_uevent_ops device_uevent_ops = {
305 .filter = dev_uevent_filter,
306 .name = dev_uevent_name,
307 .uevent = dev_uevent,
308};
309
310static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
311 char *buf)
312{
313 struct kobject *top_kobj;
314 struct kset *kset;
315 struct kobj_uevent_env *env = NULL;
316 int i;
317 size_t count = 0;
318 int retval;
319
320
321 top_kobj = &dev->kobj;
322 while (!top_kobj->kset && top_kobj->parent)
323 top_kobj = top_kobj->parent;
324 if (!top_kobj->kset)
325 goto out;
326
327 kset = top_kobj->kset;
328 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
329 goto out;
330
331
332 if (kset->uevent_ops && kset->uevent_ops->filter)
333 if (!kset->uevent_ops->filter(kset, &dev->kobj))
334 goto out;
335
336 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
337 if (!env)
338 return -ENOMEM;
339
340
341 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
342 if (retval)
343 goto out;
344
345
346 for (i = 0; i < env->envp_idx; i++)
347 count += sprintf(&buf[count], "%s\n", env->envp[i]);
348out:
349 kfree(env);
350 return count;
351}
352
353static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
354 const char *buf, size_t count)
355{
356 enum kobject_action action;
357
358 if (kobject_action_type(buf, count, &action) == 0)
359 kobject_uevent(&dev->kobj, action);
360 else
361 dev_err(dev, "uevent: unknown action-string\n");
362 return count;
363}
364
365static struct device_attribute uevent_attr =
366 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
367
368static int device_add_attributes(struct device *dev,
369 struct device_attribute *attrs)
370{
371 int error = 0;
372 int i;
373
374 if (attrs) {
375 for (i = 0; attr_name(attrs[i]); i++) {
376 error = device_create_file(dev, &attrs[i]);
377 if (error)
378 break;
379 }
380 if (error)
381 while (--i >= 0)
382 device_remove_file(dev, &attrs[i]);
383 }
384 return error;
385}
386
387static void device_remove_attributes(struct device *dev,
388 struct device_attribute *attrs)
389{
390 int i;
391
392 if (attrs)
393 for (i = 0; attr_name(attrs[i]); i++)
394 device_remove_file(dev, &attrs[i]);
395}
396
397static int device_add_bin_attributes(struct device *dev,
398 struct bin_attribute *attrs)
399{
400 int error = 0;
401 int i;
402
403 if (attrs) {
404 for (i = 0; attr_name(attrs[i]); i++) {
405 error = device_create_bin_file(dev, &attrs[i]);
406 if (error)
407 break;
408 }
409 if (error)
410 while (--i >= 0)
411 device_remove_bin_file(dev, &attrs[i]);
412 }
413 return error;
414}
415
416static void device_remove_bin_attributes(struct device *dev,
417 struct bin_attribute *attrs)
418{
419 int i;
420
421 if (attrs)
422 for (i = 0; attr_name(attrs[i]); i++)
423 device_remove_bin_file(dev, &attrs[i]);
424}
425
426static int device_add_groups(struct device *dev,
427 const struct attribute_group **groups)
428{
429 int error = 0;
430 int i;
431
432 if (groups) {
433 for (i = 0; groups[i]; i++) {
434 error = sysfs_create_group(&dev->kobj, groups[i]);
435 if (error) {
436 while (--i >= 0)
437 sysfs_remove_group(&dev->kobj,
438 groups[i]);
439 break;
440 }
441 }
442 }
443 return error;
444}
445
446static void device_remove_groups(struct device *dev,
447 const struct attribute_group **groups)
448{
449 int i;
450
451 if (groups)
452 for (i = 0; groups[i]; i++)
453 sysfs_remove_group(&dev->kobj, groups[i]);
454}
455
456static int device_add_attrs(struct device *dev)
457{
458 struct class *class = dev->class;
459 const struct device_type *type = dev->type;
460 int error;
461
462 if (class) {
463 error = device_add_attributes(dev, class->dev_attrs);
464 if (error)
465 return error;
466 error = device_add_bin_attributes(dev, class->dev_bin_attrs);
467 if (error)
468 goto err_remove_class_attrs;
469 }
470
471 if (type) {
472 error = device_add_groups(dev, type->groups);
473 if (error)
474 goto err_remove_class_bin_attrs;
475 }
476
477 error = device_add_groups(dev, dev->groups);
478 if (error)
479 goto err_remove_type_groups;
480
481 return 0;
482
483 err_remove_type_groups:
484 if (type)
485 device_remove_groups(dev, type->groups);
486 err_remove_class_bin_attrs:
487 if (class)
488 device_remove_bin_attributes(dev, class->dev_bin_attrs);
489 err_remove_class_attrs:
490 if (class)
491 device_remove_attributes(dev, class->dev_attrs);
492
493 return error;
494}
495
496static void device_remove_attrs(struct device *dev)
497{
498 struct class *class = dev->class;
499 const struct device_type *type = dev->type;
500
501 device_remove_groups(dev, dev->groups);
502
503 if (type)
504 device_remove_groups(dev, type->groups);
505
506 if (class) {
507 device_remove_attributes(dev, class->dev_attrs);
508 device_remove_bin_attributes(dev, class->dev_bin_attrs);
509 }
510}
511
512
513static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
514 char *buf)
515{
516 return print_dev_t(buf, dev->devt);
517}
518
519static struct device_attribute devt_attr =
520 __ATTR(dev, S_IRUGO, show_dev, NULL);
521
522
523struct kset *devices_kset;
524
525
526
527
528
529
530int device_create_file(struct device *dev,
531 const struct device_attribute *attr)
532{
533 int error = 0;
534 if (dev)
535 error = sysfs_create_file(&dev->kobj, &attr->attr);
536 return error;
537}
538
539
540
541
542
543
544void device_remove_file(struct device *dev,
545 const struct device_attribute *attr)
546{
547 if (dev)
548 sysfs_remove_file(&dev->kobj, &attr->attr);
549}
550
551
552
553
554
555
556int device_create_bin_file(struct device *dev,
557 const struct bin_attribute *attr)
558{
559 int error = -EINVAL;
560 if (dev)
561 error = sysfs_create_bin_file(&dev->kobj, attr);
562 return error;
563}
564EXPORT_SYMBOL_GPL(device_create_bin_file);
565
566
567
568
569
570
571void device_remove_bin_file(struct device *dev,
572 const struct bin_attribute *attr)
573{
574 if (dev)
575 sysfs_remove_bin_file(&dev->kobj, attr);
576}
577EXPORT_SYMBOL_GPL(device_remove_bin_file);
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604int device_schedule_callback_owner(struct device *dev,
605 void (*func)(struct device *), struct module *owner)
606{
607 return sysfs_schedule_callback(&dev->kobj,
608 (void (*)(void *)) func, dev, owner);
609}
610EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
611
612static void klist_children_get(struct klist_node *n)
613{
614 struct device_private *p = to_device_private_parent(n);
615 struct device *dev = p->device;
616
617 get_device(dev);
618}
619
620static void klist_children_put(struct klist_node *n)
621{
622 struct device_private *p = to_device_private_parent(n);
623 struct device *dev = p->device;
624
625 put_device(dev);
626}
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648void device_initialize(struct device *dev)
649{
650 dev->kobj.kset = devices_kset;
651 kobject_init(&dev->kobj, &device_ktype);
652 INIT_LIST_HEAD(&dev->dma_pools);
653 mutex_init(&dev->mutex);
654 lockdep_set_novalidate_class(&dev->mutex);
655 spin_lock_init(&dev->devres_lock);
656 INIT_LIST_HEAD(&dev->devres_head);
657 device_pm_init(dev);
658 set_dev_node(dev, -1);
659}
660
661static struct kobject *virtual_device_parent(struct device *dev)
662{
663 static struct kobject *virtual_dir = NULL;
664
665 if (!virtual_dir)
666 virtual_dir = kobject_create_and_add("virtual",
667 &devices_kset->kobj);
668
669 return virtual_dir;
670}
671
672struct class_dir {
673 struct kobject kobj;
674 struct class *class;
675};
676
677#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
678
679static void class_dir_release(struct kobject *kobj)
680{
681 struct class_dir *dir = to_class_dir(kobj);
682 kfree(dir);
683}
684
685static const
686struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
687{
688 struct class_dir *dir = to_class_dir(kobj);
689 return dir->class->ns_type;
690}
691
692static struct kobj_type class_dir_ktype = {
693 .release = class_dir_release,
694 .sysfs_ops = &kobj_sysfs_ops,
695 .child_ns_type = class_dir_child_ns_type
696};
697
698static struct kobject *
699class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
700{
701 struct class_dir *dir;
702 int retval;
703
704 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
705 if (!dir)
706 return NULL;
707
708 dir->class = class;
709 kobject_init(&dir->kobj, &class_dir_ktype);
710
711 dir->kobj.kset = &class->p->glue_dirs;
712
713 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
714 if (retval < 0) {
715 kobject_put(&dir->kobj);
716 return NULL;
717 }
718 return &dir->kobj;
719}
720
721
722static struct kobject *get_device_parent(struct device *dev,
723 struct device *parent)
724{
725 if (dev->class) {
726 static DEFINE_MUTEX(gdp_mutex);
727 struct kobject *kobj = NULL;
728 struct kobject *parent_kobj;
729 struct kobject *k;
730
731#ifdef CONFIG_BLOCK
732
733 if (sysfs_deprecated && dev->class == &block_class) {
734 if (parent && parent->class == &block_class)
735 return &parent->kobj;
736 return &block_class.p->subsys.kobj;
737 }
738#endif
739
740
741
742
743
744
745 if (parent == NULL)
746 parent_kobj = virtual_device_parent(dev);
747 else if (parent->class && !dev->class->ns_type)
748 return &parent->kobj;
749 else
750 parent_kobj = &parent->kobj;
751
752 mutex_lock(&gdp_mutex);
753
754
755 spin_lock(&dev->class->p->glue_dirs.list_lock);
756 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
757 if (k->parent == parent_kobj) {
758 kobj = kobject_get(k);
759 break;
760 }
761 spin_unlock(&dev->class->p->glue_dirs.list_lock);
762 if (kobj) {
763 mutex_unlock(&gdp_mutex);
764 return kobj;
765 }
766
767
768 k = class_dir_create_and_add(dev->class, parent_kobj);
769
770 mutex_unlock(&gdp_mutex);
771 return k;
772 }
773
774
775 if (!parent && dev->bus && dev->bus->dev_root)
776 return &dev->bus->dev_root->kobj;
777
778 if (parent)
779 return &parent->kobj;
780 return NULL;
781}
782
783static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
784{
785
786 if (!glue_dir || !dev->class ||
787 glue_dir->kset != &dev->class->p->glue_dirs)
788 return;
789
790 kobject_put(glue_dir);
791}
792
793static void cleanup_device_parent(struct device *dev)
794{
795 cleanup_glue_dir(dev, dev->kobj.parent);
796}
797
798static int device_add_class_symlinks(struct device *dev)
799{
800 int error;
801
802 if (!dev->class)
803 return 0;
804
805 error = sysfs_create_link(&dev->kobj,
806 &dev->class->p->subsys.kobj,
807 "subsystem");
808 if (error)
809 goto out;
810
811 if (dev->parent && device_is_not_partition(dev)) {
812 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
813 "device");
814 if (error)
815 goto out_subsys;
816 }
817
818#ifdef CONFIG_BLOCK
819
820 if (sysfs_deprecated && dev->class == &block_class)
821 return 0;
822#endif
823
824
825 error = sysfs_create_link(&dev->class->p->subsys.kobj,
826 &dev->kobj, dev_name(dev));
827 if (error)
828 goto out_device;
829
830 return 0;
831
832out_device:
833 sysfs_remove_link(&dev->kobj, "device");
834
835out_subsys:
836 sysfs_remove_link(&dev->kobj, "subsystem");
837out:
838 return error;
839}
840
841static void device_remove_class_symlinks(struct device *dev)
842{
843 if (!dev->class)
844 return;
845
846 if (dev->parent && device_is_not_partition(dev))
847 sysfs_remove_link(&dev->kobj, "device");
848 sysfs_remove_link(&dev->kobj, "subsystem");
849#ifdef CONFIG_BLOCK
850 if (sysfs_deprecated && dev->class == &block_class)
851 return;
852#endif
853 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
854}
855
856
857
858
859
860
861int dev_set_name(struct device *dev, const char *fmt, ...)
862{
863 va_list vargs;
864 int err;
865
866 va_start(vargs, fmt);
867 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
868 va_end(vargs);
869 return err;
870}
871EXPORT_SYMBOL_GPL(dev_set_name);
872
873
874
875
876
877
878
879
880
881
882
883
884static struct kobject *device_to_dev_kobj(struct device *dev)
885{
886 struct kobject *kobj;
887
888 if (dev->class)
889 kobj = dev->class->dev_kobj;
890 else
891 kobj = sysfs_dev_char_kobj;
892
893 return kobj;
894}
895
896static int device_create_sys_dev_entry(struct device *dev)
897{
898 struct kobject *kobj = device_to_dev_kobj(dev);
899 int error = 0;
900 char devt_str[15];
901
902 if (kobj) {
903 format_dev_t(devt_str, dev->devt);
904 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
905 }
906
907 return error;
908}
909
910static void device_remove_sys_dev_entry(struct device *dev)
911{
912 struct kobject *kobj = device_to_dev_kobj(dev);
913 char devt_str[15];
914
915 if (kobj) {
916 format_dev_t(devt_str, dev->devt);
917 sysfs_remove_link(kobj, devt_str);
918 }
919}
920
921int device_private_init(struct device *dev)
922{
923 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
924 if (!dev->p)
925 return -ENOMEM;
926 dev->p->device = dev;
927 klist_init(&dev->p->klist_children, klist_children_get,
928 klist_children_put);
929 INIT_LIST_HEAD(&dev->p->deferred_probe);
930 return 0;
931}
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955int device_add(struct device *dev)
956{
957 struct device *parent = NULL;
958 struct kobject *kobj;
959 struct class_interface *class_intf;
960 int error = -EINVAL;
961
962 dev = get_device(dev);
963 if (!dev)
964 goto done;
965
966 if (!dev->p) {
967 error = device_private_init(dev);
968 if (error)
969 goto done;
970 }
971
972
973
974
975
976
977 if (dev->init_name) {
978 dev_set_name(dev, "%s", dev->init_name);
979 dev->init_name = NULL;
980 }
981
982
983 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
984 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
985
986 if (!dev_name(dev)) {
987 error = -EINVAL;
988 goto name_error;
989 }
990
991 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
992
993 parent = get_device(dev->parent);
994 kobj = get_device_parent(dev, parent);
995 if (kobj)
996 dev->kobj.parent = kobj;
997
998
999 if (parent)
1000 set_dev_node(dev, dev_to_node(parent));
1001
1002
1003
1004 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
1005 if (error)
1006 goto Error;
1007
1008
1009 if (platform_notify)
1010 platform_notify(dev);
1011
1012 error = device_create_file(dev, &uevent_attr);
1013 if (error)
1014 goto attrError;
1015
1016 if (MAJOR(dev->devt)) {
1017 error = device_create_file(dev, &devt_attr);
1018 if (error)
1019 goto ueventattrError;
1020
1021 error = device_create_sys_dev_entry(dev);
1022 if (error)
1023 goto devtattrError;
1024
1025 devtmpfs_create_node(dev);
1026 }
1027
1028 error = device_add_class_symlinks(dev);
1029 if (error)
1030 goto SymlinkError;
1031 error = device_add_attrs(dev);
1032 if (error)
1033 goto AttrsError;
1034 error = bus_add_device(dev);
1035 if (error)
1036 goto BusError;
1037 error = dpm_sysfs_add(dev);
1038 if (error)
1039 goto DPMError;
1040 device_pm_add(dev);
1041
1042
1043
1044
1045 if (dev->bus)
1046 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1047 BUS_NOTIFY_ADD_DEVICE, dev);
1048
1049 kobject_uevent(&dev->kobj, KOBJ_ADD);
1050 bus_probe_device(dev);
1051 if (parent)
1052 klist_add_tail(&dev->p->knode_parent,
1053 &parent->p->klist_children);
1054
1055 if (dev->class) {
1056 mutex_lock(&dev->class->p->mutex);
1057
1058 klist_add_tail(&dev->knode_class,
1059 &dev->class->p->klist_devices);
1060
1061
1062 list_for_each_entry(class_intf,
1063 &dev->class->p->interfaces, node)
1064 if (class_intf->add_dev)
1065 class_intf->add_dev(dev, class_intf);
1066 mutex_unlock(&dev->class->p->mutex);
1067 }
1068done:
1069 put_device(dev);
1070 return error;
1071 DPMError:
1072 bus_remove_device(dev);
1073 BusError:
1074 device_remove_attrs(dev);
1075 AttrsError:
1076 device_remove_class_symlinks(dev);
1077 SymlinkError:
1078 if (MAJOR(dev->devt))
1079 devtmpfs_delete_node(dev);
1080 if (MAJOR(dev->devt))
1081 device_remove_sys_dev_entry(dev);
1082 devtattrError:
1083 if (MAJOR(dev->devt))
1084 device_remove_file(dev, &devt_attr);
1085 ueventattrError:
1086 device_remove_file(dev, &uevent_attr);
1087 attrError:
1088 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1089 kobject_del(&dev->kobj);
1090 Error:
1091 cleanup_device_parent(dev);
1092 if (parent)
1093 put_device(parent);
1094name_error:
1095 kfree(dev->p);
1096 dev->p = NULL;
1097 goto done;
1098}
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118int device_register(struct device *dev)
1119{
1120 device_initialize(dev);
1121 return device_add(dev);
1122}
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132struct device *get_device(struct device *dev)
1133{
1134 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1135}
1136
1137
1138
1139
1140
1141void put_device(struct device *dev)
1142{
1143
1144 if (dev)
1145 kobject_put(&dev->kobj);
1146}
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161void device_del(struct device *dev)
1162{
1163 struct device *parent = dev->parent;
1164 struct class_interface *class_intf;
1165
1166
1167
1168
1169 if (dev->bus)
1170 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1171 BUS_NOTIFY_DEL_DEVICE, dev);
1172 device_pm_remove(dev);
1173 dpm_sysfs_remove(dev);
1174 if (parent)
1175 klist_del(&dev->p->knode_parent);
1176 if (MAJOR(dev->devt)) {
1177 devtmpfs_delete_node(dev);
1178 device_remove_sys_dev_entry(dev);
1179 device_remove_file(dev, &devt_attr);
1180 }
1181 if (dev->class) {
1182 device_remove_class_symlinks(dev);
1183
1184 mutex_lock(&dev->class->p->mutex);
1185
1186 list_for_each_entry(class_intf,
1187 &dev->class->p->interfaces, node)
1188 if (class_intf->remove_dev)
1189 class_intf->remove_dev(dev, class_intf);
1190
1191 klist_del(&dev->knode_class);
1192 mutex_unlock(&dev->class->p->mutex);
1193 }
1194 device_remove_file(dev, &uevent_attr);
1195 device_remove_attrs(dev);
1196 bus_remove_device(dev);
1197 driver_deferred_probe_del(dev);
1198
1199
1200
1201
1202
1203
1204 devres_release_all(dev);
1205
1206
1207
1208
1209 if (platform_notify_remove)
1210 platform_notify_remove(dev);
1211 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1212 cleanup_device_parent(dev);
1213 kobject_del(&dev->kobj);
1214 put_device(parent);
1215}
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228void device_unregister(struct device *dev)
1229{
1230 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1231 device_del(dev);
1232 put_device(dev);
1233}
1234
1235static struct device *next_device(struct klist_iter *i)
1236{
1237 struct klist_node *n = klist_next(i);
1238 struct device *dev = NULL;
1239 struct device_private *p;
1240
1241 if (n) {
1242 p = to_device_private_parent(n);
1243 dev = p->device;
1244 }
1245 return dev;
1246}
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259const char *device_get_devnode(struct device *dev,
1260 umode_t *mode, const char **tmp)
1261{
1262 char *s;
1263
1264 *tmp = NULL;
1265
1266
1267 if (dev->type && dev->type->devnode)
1268 *tmp = dev->type->devnode(dev, mode);
1269 if (*tmp)
1270 return *tmp;
1271
1272
1273 if (dev->class && dev->class->devnode)
1274 *tmp = dev->class->devnode(dev, mode);
1275 if (*tmp)
1276 return *tmp;
1277
1278
1279 if (strchr(dev_name(dev), '!') == NULL)
1280 return dev_name(dev);
1281
1282
1283 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1284 if (!*tmp)
1285 return NULL;
1286 while ((s = strchr(*tmp, '!')))
1287 s[0] = '/';
1288 return *tmp;
1289}
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303int device_for_each_child(struct device *parent, void *data,
1304 int (*fn)(struct device *dev, void *data))
1305{
1306 struct klist_iter i;
1307 struct device *child;
1308 int error = 0;
1309
1310 if (!parent->p)
1311 return 0;
1312
1313 klist_iter_init(&parent->p->klist_children, &i);
1314 while ((child = next_device(&i)) && !error)
1315 error = fn(child, data);
1316 klist_iter_exit(&i);
1317 return error;
1318}
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335struct device *device_find_child(struct device *parent, void *data,
1336 int (*match)(struct device *dev, void *data))
1337{
1338 struct klist_iter i;
1339 struct device *child;
1340
1341 if (!parent)
1342 return NULL;
1343
1344 klist_iter_init(&parent->p->klist_children, &i);
1345 while ((child = next_device(&i)))
1346 if (match(child, data) && get_device(child))
1347 break;
1348 klist_iter_exit(&i);
1349 return child;
1350}
1351
1352int __init devices_init(void)
1353{
1354 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1355 if (!devices_kset)
1356 return -ENOMEM;
1357 dev_kobj = kobject_create_and_add("dev", NULL);
1358 if (!dev_kobj)
1359 goto dev_kobj_err;
1360 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1361 if (!sysfs_dev_block_kobj)
1362 goto block_kobj_err;
1363 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1364 if (!sysfs_dev_char_kobj)
1365 goto char_kobj_err;
1366
1367 return 0;
1368
1369 char_kobj_err:
1370 kobject_put(sysfs_dev_block_kobj);
1371 block_kobj_err:
1372 kobject_put(dev_kobj);
1373 dev_kobj_err:
1374 kset_unregister(devices_kset);
1375 return -ENOMEM;
1376}
1377
1378EXPORT_SYMBOL_GPL(device_for_each_child);
1379EXPORT_SYMBOL_GPL(device_find_child);
1380
1381EXPORT_SYMBOL_GPL(device_initialize);
1382EXPORT_SYMBOL_GPL(device_add);
1383EXPORT_SYMBOL_GPL(device_register);
1384
1385EXPORT_SYMBOL_GPL(device_del);
1386EXPORT_SYMBOL_GPL(device_unregister);
1387EXPORT_SYMBOL_GPL(get_device);
1388EXPORT_SYMBOL_GPL(put_device);
1389
1390EXPORT_SYMBOL_GPL(device_create_file);
1391EXPORT_SYMBOL_GPL(device_remove_file);
1392
1393struct root_device {
1394 struct device dev;
1395 struct module *owner;
1396};
1397
1398inline struct root_device *to_root_device(struct device *d)
1399{
1400 return container_of(d, struct root_device, dev);
1401}
1402
1403static void root_device_release(struct device *dev)
1404{
1405 kfree(to_root_device(dev));
1406}
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430struct device *__root_device_register(const char *name, struct module *owner)
1431{
1432 struct root_device *root;
1433 int err = -ENOMEM;
1434
1435 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1436 if (!root)
1437 return ERR_PTR(err);
1438
1439 err = dev_set_name(&root->dev, "%s", name);
1440 if (err) {
1441 kfree(root);
1442 return ERR_PTR(err);
1443 }
1444
1445 root->dev.release = root_device_release;
1446
1447 err = device_register(&root->dev);
1448 if (err) {
1449 put_device(&root->dev);
1450 return ERR_PTR(err);
1451 }
1452
1453#ifdef CONFIG_MODULES
1454 if (owner) {
1455 struct module_kobject *mk = &owner->mkobj;
1456
1457 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1458 if (err) {
1459 device_unregister(&root->dev);
1460 return ERR_PTR(err);
1461 }
1462 root->owner = owner;
1463 }
1464#endif
1465
1466 return &root->dev;
1467}
1468EXPORT_SYMBOL_GPL(__root_device_register);
1469
1470
1471
1472
1473
1474
1475
1476
1477void root_device_unregister(struct device *dev)
1478{
1479 struct root_device *root = to_root_device(dev);
1480
1481 if (root->owner)
1482 sysfs_remove_link(&root->dev.kobj, "module");
1483
1484 device_unregister(dev);
1485}
1486EXPORT_SYMBOL_GPL(root_device_unregister);
1487
1488
1489static void device_create_release(struct device *dev)
1490{
1491 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1492 kfree(dev);
1493}
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520struct device *device_create_vargs(struct class *class, struct device *parent,
1521 dev_t devt, void *drvdata, const char *fmt,
1522 va_list args)
1523{
1524 struct device *dev = NULL;
1525 int retval = -ENODEV;
1526
1527 if (class == NULL || IS_ERR(class))
1528 goto error;
1529
1530 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1531 if (!dev) {
1532 retval = -ENOMEM;
1533 goto error;
1534 }
1535
1536 dev->devt = devt;
1537 dev->class = class;
1538 dev->parent = parent;
1539 dev->release = device_create_release;
1540 dev_set_drvdata(dev, drvdata);
1541
1542 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1543 if (retval)
1544 goto error;
1545
1546 retval = device_register(dev);
1547 if (retval)
1548 goto error;
1549
1550 return dev;
1551
1552error:
1553 put_device(dev);
1554 return ERR_PTR(retval);
1555}
1556EXPORT_SYMBOL_GPL(device_create_vargs);
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582struct device *device_create(struct class *class, struct device *parent,
1583 dev_t devt, void *drvdata, const char *fmt, ...)
1584{
1585 va_list vargs;
1586 struct device *dev;
1587
1588 va_start(vargs, fmt);
1589 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1590 va_end(vargs);
1591 return dev;
1592}
1593EXPORT_SYMBOL_GPL(device_create);
1594
1595static int __match_devt(struct device *dev, void *data)
1596{
1597 dev_t *devt = data;
1598
1599 return dev->devt == *devt;
1600}
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610void device_destroy(struct class *class, dev_t devt)
1611{
1612 struct device *dev;
1613
1614 dev = class_find_device(class, NULL, &devt, __match_devt);
1615 if (dev) {
1616 put_device(dev);
1617 device_unregister(dev);
1618 }
1619}
1620EXPORT_SYMBOL_GPL(device_destroy);
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661int device_rename(struct device *dev, const char *new_name)
1662{
1663 char *old_class_name = NULL;
1664 char *new_class_name = NULL;
1665 char *old_device_name = NULL;
1666 int error;
1667
1668 dev = get_device(dev);
1669 if (!dev)
1670 return -EINVAL;
1671
1672 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1673 __func__, new_name);
1674
1675 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1676 if (!old_device_name) {
1677 error = -ENOMEM;
1678 goto out;
1679 }
1680
1681 if (dev->class) {
1682 error = sysfs_rename_link(&dev->class->p->subsys.kobj,
1683 &dev->kobj, old_device_name, new_name);
1684 if (error)
1685 goto out;
1686 }
1687
1688 error = kobject_rename(&dev->kobj, new_name);
1689 if (error)
1690 goto out;
1691
1692out:
1693 put_device(dev);
1694
1695 kfree(new_class_name);
1696 kfree(old_class_name);
1697 kfree(old_device_name);
1698
1699 return error;
1700}
1701EXPORT_SYMBOL_GPL(device_rename);
1702
1703static int device_move_class_links(struct device *dev,
1704 struct device *old_parent,
1705 struct device *new_parent)
1706{
1707 int error = 0;
1708
1709 if (old_parent)
1710 sysfs_remove_link(&dev->kobj, "device");
1711 if (new_parent)
1712 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1713 "device");
1714 return error;
1715}
1716
1717
1718
1719
1720
1721
1722
1723int device_move(struct device *dev, struct device *new_parent,
1724 enum dpm_order dpm_order)
1725{
1726 int error;
1727 struct device *old_parent;
1728 struct kobject *new_parent_kobj;
1729
1730 dev = get_device(dev);
1731 if (!dev)
1732 return -EINVAL;
1733
1734 device_pm_lock();
1735 new_parent = get_device(new_parent);
1736 new_parent_kobj = get_device_parent(dev, new_parent);
1737
1738 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1739 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1740 error = kobject_move(&dev->kobj, new_parent_kobj);
1741 if (error) {
1742 cleanup_glue_dir(dev, new_parent_kobj);
1743 put_device(new_parent);
1744 goto out;
1745 }
1746 old_parent = dev->parent;
1747 dev->parent = new_parent;
1748 if (old_parent)
1749 klist_remove(&dev->p->knode_parent);
1750 if (new_parent) {
1751 klist_add_tail(&dev->p->knode_parent,
1752 &new_parent->p->klist_children);
1753 set_dev_node(dev, dev_to_node(new_parent));
1754 }
1755
1756 if (dev->class) {
1757 error = device_move_class_links(dev, old_parent, new_parent);
1758 if (error) {
1759
1760 device_move_class_links(dev, new_parent, old_parent);
1761 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1762 if (new_parent)
1763 klist_remove(&dev->p->knode_parent);
1764 dev->parent = old_parent;
1765 if (old_parent) {
1766 klist_add_tail(&dev->p->knode_parent,
1767 &old_parent->p->klist_children);
1768 set_dev_node(dev, dev_to_node(old_parent));
1769 }
1770 }
1771 cleanup_glue_dir(dev, new_parent_kobj);
1772 put_device(new_parent);
1773 goto out;
1774 }
1775 }
1776 switch (dpm_order) {
1777 case DPM_ORDER_NONE:
1778 break;
1779 case DPM_ORDER_DEV_AFTER_PARENT:
1780 device_pm_move_after(dev, new_parent);
1781 break;
1782 case DPM_ORDER_PARENT_BEFORE_DEV:
1783 device_pm_move_before(new_parent, dev);
1784 break;
1785 case DPM_ORDER_DEV_LAST:
1786 device_pm_move_last(dev);
1787 break;
1788 }
1789
1790 put_device(old_parent);
1791out:
1792 device_pm_unlock();
1793 put_device(dev);
1794 return error;
1795}
1796EXPORT_SYMBOL_GPL(device_move);
1797
1798
1799
1800
1801void device_shutdown(void)
1802{
1803 struct device *dev;
1804
1805 spin_lock(&devices_kset->list_lock);
1806
1807
1808
1809
1810
1811 while (!list_empty(&devices_kset->list)) {
1812 dev = list_entry(devices_kset->list.prev, struct device,
1813 kobj.entry);
1814
1815
1816
1817
1818
1819
1820 get_device(dev->parent);
1821 get_device(dev);
1822
1823
1824
1825
1826 list_del_init(&dev->kobj.entry);
1827 spin_unlock(&devices_kset->list_lock);
1828
1829
1830 if (dev->parent)
1831 device_lock(dev->parent);
1832 device_lock(dev);
1833
1834
1835 pm_runtime_get_noresume(dev);
1836 pm_runtime_barrier(dev);
1837
1838 if (dev->bus && dev->bus->shutdown) {
1839 dev_dbg(dev, "shutdown\n");
1840 dev->bus->shutdown(dev);
1841 } else if (dev->driver && dev->driver->shutdown) {
1842 dev_dbg(dev, "shutdown\n");
1843 dev->driver->shutdown(dev);
1844 }
1845
1846 device_unlock(dev);
1847 if (dev->parent)
1848 device_unlock(dev->parent);
1849
1850 put_device(dev);
1851 put_device(dev->parent);
1852
1853 spin_lock(&devices_kset->list_lock);
1854 }
1855 spin_unlock(&devices_kset->list_lock);
1856 async_synchronize_full();
1857}
1858
1859
1860
1861
1862
1863#ifdef CONFIG_PRINTK
1864int __dev_printk(const char *level, const struct device *dev,
1865 struct va_format *vaf)
1866{
1867 char dict[128];
1868 const char *level_extra = "";
1869 size_t dictlen = 0;
1870 const char *subsys;
1871
1872 if (!dev)
1873 return printk("%s(NULL device *): %pV", level, vaf);
1874
1875 if (dev->class)
1876 subsys = dev->class->name;
1877 else if (dev->bus)
1878 subsys = dev->bus->name;
1879 else
1880 goto skip;
1881
1882 dictlen += snprintf(dict + dictlen, sizeof(dict) - dictlen,
1883 "SUBSYSTEM=%s", subsys);
1884
1885
1886
1887
1888
1889
1890
1891
1892 if (MAJOR(dev->devt)) {
1893 char c;
1894
1895 if (strcmp(subsys, "block") == 0)
1896 c = 'b';
1897 else
1898 c = 'c';
1899 dictlen++;
1900 dictlen += snprintf(dict + dictlen, sizeof(dict) - dictlen,
1901 "DEVICE=%c%u:%u",
1902 c, MAJOR(dev->devt), MINOR(dev->devt));
1903 } else if (strcmp(subsys, "net") == 0) {
1904 struct net_device *net = to_net_dev(dev);
1905
1906 dictlen++;
1907 dictlen += snprintf(dict + dictlen, sizeof(dict) - dictlen,
1908 "DEVICE=n%u", net->ifindex);
1909 } else {
1910 dictlen++;
1911 dictlen += snprintf(dict + dictlen, sizeof(dict) - dictlen,
1912 "DEVICE=+%s:%s", subsys, dev_name(dev));
1913 }
1914skip:
1915 if (level[2])
1916 level_extra = &level[2];
1917
1918 return printk_emit(0, level[1] - '0',
1919 dictlen ? dict : NULL, dictlen,
1920 "%s %s: %s%pV",
1921 dev_driver_string(dev), dev_name(dev),
1922 level_extra, vaf);
1923}
1924EXPORT_SYMBOL(__dev_printk);
1925
1926int dev_printk(const char *level, const struct device *dev,
1927 const char *fmt, ...)
1928{
1929 struct va_format vaf;
1930 va_list args;
1931 int r;
1932
1933 va_start(args, fmt);
1934
1935 vaf.fmt = fmt;
1936 vaf.va = &args;
1937
1938 r = __dev_printk(level, dev, &vaf);
1939 va_end(args);
1940
1941 return r;
1942}
1943EXPORT_SYMBOL(dev_printk);
1944
1945#define define_dev_printk_level(func, kern_level) \
1946int func(const struct device *dev, const char *fmt, ...) \
1947{ \
1948 struct va_format vaf; \
1949 va_list args; \
1950 int r; \
1951 \
1952 va_start(args, fmt); \
1953 \
1954 vaf.fmt = fmt; \
1955 vaf.va = &args; \
1956 \
1957 r = __dev_printk(kern_level, dev, &vaf); \
1958 va_end(args); \
1959 \
1960 return r; \
1961} \
1962EXPORT_SYMBOL(func);
1963
1964define_dev_printk_level(dev_emerg, KERN_EMERG);
1965define_dev_printk_level(dev_alert, KERN_ALERT);
1966define_dev_printk_level(dev_crit, KERN_CRIT);
1967define_dev_printk_level(dev_err, KERN_ERR);
1968define_dev_printk_level(dev_warn, KERN_WARNING);
1969define_dev_printk_level(dev_notice, KERN_NOTICE);
1970define_dev_printk_level(_dev_info, KERN_INFO);
1971
1972#endif
1973