1
2
3
4
5
6
7
8
9
10
11
12
13#ifndef _DEVICE_H_
14#define _DEVICE_H_
15
16#include <linux/ioport.h>
17#include <linux/kobject.h>
18#include <linux/klist.h>
19#include <linux/list.h>
20#include <linux/lockdep.h>
21#include <linux/compiler.h>
22#include <linux/types.h>
23#include <linux/mutex.h>
24#include <linux/pinctrl/devinfo.h>
25#include <linux/pm.h>
26#include <linux/atomic.h>
27#include <linux/ratelimit.h>
28#include <linux/uidgid.h>
29#include <linux/gfp.h>
30#include <asm/device.h>
31
32struct device;
33struct device_private;
34struct device_driver;
35struct driver_private;
36struct module;
37struct class;
38struct subsys_private;
39struct bus_type;
40struct device_node;
41struct fwnode_handle;
42struct iommu_ops;
43struct iommu_group;
44
45struct bus_attribute {
46 struct attribute attr;
47 ssize_t (*show)(struct bus_type *bus, char *buf);
48 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
49};
50
51#define BUS_ATTR(_name, _mode, _show, _store) \
52 struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
53#define BUS_ATTR_RW(_name) \
54 struct bus_attribute bus_attr_##_name = __ATTR_RW(_name)
55#define BUS_ATTR_RO(_name) \
56 struct bus_attribute bus_attr_##_name = __ATTR_RO(_name)
57
58extern int __must_check bus_create_file(struct bus_type *,
59 struct bus_attribute *);
60extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108struct bus_type {
109 const char *name;
110 const char *dev_name;
111 struct device *dev_root;
112 struct device_attribute *dev_attrs;
113 const struct attribute_group **bus_groups;
114 const struct attribute_group **dev_groups;
115 const struct attribute_group **drv_groups;
116
117 int (*match)(struct device *dev, struct device_driver *drv);
118 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
119 int (*probe)(struct device *dev);
120 int (*remove)(struct device *dev);
121 void (*shutdown)(struct device *dev);
122
123 int (*online)(struct device *dev);
124 int (*offline)(struct device *dev);
125
126 int (*suspend)(struct device *dev, pm_message_t state);
127 int (*resume)(struct device *dev);
128
129 const struct dev_pm_ops *pm;
130
131 const struct iommu_ops *iommu_ops;
132
133 struct subsys_private *p;
134 struct lock_class_key lock_key;
135};
136
137extern int __must_check bus_register(struct bus_type *bus);
138
139extern void bus_unregister(struct bus_type *bus);
140
141extern int __must_check bus_rescan_devices(struct bus_type *bus);
142
143
144struct subsys_dev_iter {
145 struct klist_iter ki;
146 const struct device_type *type;
147};
148void subsys_dev_iter_init(struct subsys_dev_iter *iter,
149 struct bus_type *subsys,
150 struct device *start,
151 const struct device_type *type);
152struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter);
153void subsys_dev_iter_exit(struct subsys_dev_iter *iter);
154
155int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
156 int (*fn)(struct device *dev, void *data));
157struct device *bus_find_device(struct bus_type *bus, struct device *start,
158 void *data,
159 int (*match)(struct device *dev, void *data));
160struct device *bus_find_device_by_name(struct bus_type *bus,
161 struct device *start,
162 const char *name);
163struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id,
164 struct device *hint);
165int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
166 void *data, int (*fn)(struct device_driver *, void *));
167void bus_sort_breadthfirst(struct bus_type *bus,
168 int (*compare)(const struct device *a,
169 const struct device *b));
170
171
172
173
174
175
176struct notifier_block;
177
178extern int bus_register_notifier(struct bus_type *bus,
179 struct notifier_block *nb);
180extern int bus_unregister_notifier(struct bus_type *bus,
181 struct notifier_block *nb);
182
183
184
185
186
187#define BUS_NOTIFY_ADD_DEVICE 0x00000001
188#define BUS_NOTIFY_DEL_DEVICE 0x00000002
189#define BUS_NOTIFY_REMOVED_DEVICE 0x00000003
190#define BUS_NOTIFY_BIND_DRIVER 0x00000004
191
192#define BUS_NOTIFY_BOUND_DRIVER 0x00000005
193#define BUS_NOTIFY_UNBIND_DRIVER 0x00000006
194
195#define BUS_NOTIFY_UNBOUND_DRIVER 0x00000007
196
197#define BUS_NOTIFY_DRIVER_NOT_BOUND 0x00000008
198
199extern struct kset *bus_get_kset(struct bus_type *bus);
200extern struct klist *bus_get_device_klist(struct bus_type *bus);
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224enum probe_type {
225 PROBE_DEFAULT_STRATEGY,
226 PROBE_PREFER_ASYNCHRONOUS,
227 PROBE_FORCE_SYNCHRONOUS,
228};
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263struct device_driver {
264 const char *name;
265 struct bus_type *bus;
266
267 struct module *owner;
268 const char *mod_name;
269
270 bool suppress_bind_attrs;
271 enum probe_type probe_type;
272
273 const struct of_device_id *of_match_table;
274 const struct acpi_device_id *acpi_match_table;
275
276 int (*probe) (struct device *dev);
277 int (*remove) (struct device *dev);
278 void (*shutdown) (struct device *dev);
279 int (*suspend) (struct device *dev, pm_message_t state);
280 int (*resume) (struct device *dev);
281 const struct attribute_group **groups;
282
283 const struct dev_pm_ops *pm;
284
285 struct driver_private *p;
286};
287
288
289extern int __must_check driver_register(struct device_driver *drv);
290extern void driver_unregister(struct device_driver *drv);
291
292extern struct device_driver *driver_find(const char *name,
293 struct bus_type *bus);
294extern int driver_probe_done(void);
295extern void wait_for_device_probe(void);
296
297
298
299
300struct driver_attribute {
301 struct attribute attr;
302 ssize_t (*show)(struct device_driver *driver, char *buf);
303 ssize_t (*store)(struct device_driver *driver, const char *buf,
304 size_t count);
305};
306
307#define DRIVER_ATTR(_name, _mode, _show, _store) \
308 struct driver_attribute driver_attr_##_name = __ATTR(_name, _mode, _show, _store)
309#define DRIVER_ATTR_RW(_name) \
310 struct driver_attribute driver_attr_##_name = __ATTR_RW(_name)
311#define DRIVER_ATTR_RO(_name) \
312 struct driver_attribute driver_attr_##_name = __ATTR_RO(_name)
313#define DRIVER_ATTR_WO(_name) \
314 struct driver_attribute driver_attr_##_name = __ATTR_WO(_name)
315
316extern int __must_check driver_create_file(struct device_driver *driver,
317 const struct driver_attribute *attr);
318extern void driver_remove_file(struct device_driver *driver,
319 const struct driver_attribute *attr);
320
321extern int __must_check driver_for_each_device(struct device_driver *drv,
322 struct device *start,
323 void *data,
324 int (*fn)(struct device *dev,
325 void *));
326struct device *driver_find_device(struct device_driver *drv,
327 struct device *start, void *data,
328 int (*match)(struct device *dev, void *data));
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343struct subsys_interface {
344 const char *name;
345 struct bus_type *subsys;
346 struct list_head node;
347 int (*add_dev)(struct device *dev, struct subsys_interface *sif);
348 void (*remove_dev)(struct device *dev, struct subsys_interface *sif);
349};
350
351int subsys_interface_register(struct subsys_interface *sif);
352void subsys_interface_unregister(struct subsys_interface *sif);
353
354int subsys_system_register(struct bus_type *subsys,
355 const struct attribute_group **groups);
356int subsys_virtual_register(struct bus_type *subsys,
357 const struct attribute_group **groups);
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387struct class {
388 const char *name;
389 struct module *owner;
390
391 struct class_attribute *class_attrs;
392 const struct attribute_group **dev_groups;
393 struct kobject *dev_kobj;
394
395 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
396 char *(*devnode)(struct device *dev, umode_t *mode);
397
398 void (*class_release)(struct class *class);
399 void (*dev_release)(struct device *dev);
400
401 int (*suspend)(struct device *dev, pm_message_t state);
402 int (*resume)(struct device *dev);
403
404 const struct kobj_ns_type_operations *ns_type;
405 const void *(*namespace)(struct device *dev);
406
407 const struct dev_pm_ops *pm;
408
409 struct subsys_private *p;
410};
411
412struct class_dev_iter {
413 struct klist_iter ki;
414 const struct device_type *type;
415};
416
417extern struct kobject *sysfs_dev_block_kobj;
418extern struct kobject *sysfs_dev_char_kobj;
419extern int __must_check __class_register(struct class *class,
420 struct lock_class_key *key);
421extern void class_unregister(struct class *class);
422
423
424
425#define class_register(class) \
426({ \
427 static struct lock_class_key __key; \
428 __class_register(class, &__key); \
429})
430
431struct class_compat;
432struct class_compat *class_compat_register(const char *name);
433void class_compat_unregister(struct class_compat *cls);
434int class_compat_create_link(struct class_compat *cls, struct device *dev,
435 struct device *device_link);
436void class_compat_remove_link(struct class_compat *cls, struct device *dev,
437 struct device *device_link);
438
439extern void class_dev_iter_init(struct class_dev_iter *iter,
440 struct class *class,
441 struct device *start,
442 const struct device_type *type);
443extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
444extern void class_dev_iter_exit(struct class_dev_iter *iter);
445
446extern int class_for_each_device(struct class *class, struct device *start,
447 void *data,
448 int (*fn)(struct device *dev, void *data));
449extern struct device *class_find_device(struct class *class,
450 struct device *start, const void *data,
451 int (*match)(struct device *, const void *));
452
453struct class_attribute {
454 struct attribute attr;
455 ssize_t (*show)(struct class *class, struct class_attribute *attr,
456 char *buf);
457 ssize_t (*store)(struct class *class, struct class_attribute *attr,
458 const char *buf, size_t count);
459};
460
461#define CLASS_ATTR(_name, _mode, _show, _store) \
462 struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
463#define CLASS_ATTR_RW(_name) \
464 struct class_attribute class_attr_##_name = __ATTR_RW(_name)
465#define CLASS_ATTR_RO(_name) \
466 struct class_attribute class_attr_##_name = __ATTR_RO(_name)
467
468extern int __must_check class_create_file_ns(struct class *class,
469 const struct class_attribute *attr,
470 const void *ns);
471extern void class_remove_file_ns(struct class *class,
472 const struct class_attribute *attr,
473 const void *ns);
474
475static inline int __must_check class_create_file(struct class *class,
476 const struct class_attribute *attr)
477{
478 return class_create_file_ns(class, attr, NULL);
479}
480
481static inline void class_remove_file(struct class *class,
482 const struct class_attribute *attr)
483{
484 return class_remove_file_ns(class, attr, NULL);
485}
486
487
488struct class_attribute_string {
489 struct class_attribute attr;
490 char *str;
491};
492
493
494#define _CLASS_ATTR_STRING(_name, _mode, _str) \
495 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
496#define CLASS_ATTR_STRING(_name, _mode, _str) \
497 struct class_attribute_string class_attr_##_name = \
498 _CLASS_ATTR_STRING(_name, _mode, _str)
499
500extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
501 char *buf);
502
503struct class_interface {
504 struct list_head node;
505 struct class *class;
506
507 int (*add_dev) (struct device *, struct class_interface *);
508 void (*remove_dev) (struct device *, struct class_interface *);
509};
510
511extern int __must_check class_interface_register(struct class_interface *);
512extern void class_interface_unregister(struct class_interface *);
513
514extern struct class * __must_check __class_create(struct module *owner,
515 const char *name,
516 struct lock_class_key *key);
517extern void class_destroy(struct class *cls);
518
519
520
521#define class_create(owner, name) \
522({ \
523 static struct lock_class_key __key; \
524 __class_create(owner, name, &__key); \
525})
526
527
528
529
530
531
532
533
534
535
536struct device_type {
537 const char *name;
538 const struct attribute_group **groups;
539 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
540 char *(*devnode)(struct device *dev, umode_t *mode,
541 kuid_t *uid, kgid_t *gid);
542 void (*release)(struct device *dev);
543
544 const struct dev_pm_ops *pm;
545};
546
547
548struct device_attribute {
549 struct attribute attr;
550 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
551 char *buf);
552 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
553 const char *buf, size_t count);
554};
555
556struct dev_ext_attribute {
557 struct device_attribute attr;
558 void *var;
559};
560
561ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
562 char *buf);
563ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
564 const char *buf, size_t count);
565ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
566 char *buf);
567ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
568 const char *buf, size_t count);
569ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
570 char *buf);
571ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
572 const char *buf, size_t count);
573
574#define DEVICE_ATTR(_name, _mode, _show, _store) \
575 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
576#define DEVICE_ATTR_RW(_name) \
577 struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
578#define DEVICE_ATTR_RO(_name) \
579 struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
580#define DEVICE_ATTR_WO(_name) \
581 struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
582#define DEVICE_ULONG_ATTR(_name, _mode, _var) \
583 struct dev_ext_attribute dev_attr_##_name = \
584 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
585#define DEVICE_INT_ATTR(_name, _mode, _var) \
586 struct dev_ext_attribute dev_attr_##_name = \
587 { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
588#define DEVICE_BOOL_ATTR(_name, _mode, _var) \
589 struct dev_ext_attribute dev_attr_##_name = \
590 { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
591#define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
592 struct device_attribute dev_attr_##_name = \
593 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
594
595extern int device_create_file(struct device *device,
596 const struct device_attribute *entry);
597extern void device_remove_file(struct device *dev,
598 const struct device_attribute *attr);
599extern bool device_remove_file_self(struct device *dev,
600 const struct device_attribute *attr);
601extern int __must_check device_create_bin_file(struct device *dev,
602 const struct bin_attribute *attr);
603extern void device_remove_bin_file(struct device *dev,
604 const struct bin_attribute *attr);
605
606
607typedef void (*dr_release_t)(struct device *dev, void *res);
608typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
609
610#ifdef CONFIG_DEBUG_DEVRES
611extern void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
612 int nid, const char *name);
613#define devres_alloc(release, size, gfp) \
614 __devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)
615#define devres_alloc_node(release, size, gfp, nid) \
616 __devres_alloc_node(release, size, gfp, nid, #release)
617#else
618extern void *devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
619 int nid);
620static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
621{
622 return devres_alloc_node(release, size, gfp, NUMA_NO_NODE);
623}
624#endif
625
626extern void devres_for_each_res(struct device *dev, dr_release_t release,
627 dr_match_t match, void *match_data,
628 void (*fn)(struct device *, void *, void *),
629 void *data);
630extern void devres_free(void *res);
631extern void devres_add(struct device *dev, void *res);
632extern void *devres_find(struct device *dev, dr_release_t release,
633 dr_match_t match, void *match_data);
634extern void *devres_get(struct device *dev, void *new_res,
635 dr_match_t match, void *match_data);
636extern void *devres_remove(struct device *dev, dr_release_t release,
637 dr_match_t match, void *match_data);
638extern int devres_destroy(struct device *dev, dr_release_t release,
639 dr_match_t match, void *match_data);
640extern int devres_release(struct device *dev, dr_release_t release,
641 dr_match_t match, void *match_data);
642
643
644extern void * __must_check devres_open_group(struct device *dev, void *id,
645 gfp_t gfp);
646extern void devres_close_group(struct device *dev, void *id);
647extern void devres_remove_group(struct device *dev, void *id);
648extern int devres_release_group(struct device *dev, void *id);
649
650
651extern void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp);
652extern __printf(3, 0)
653char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
654 va_list ap);
655extern __printf(3, 4)
656char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...);
657static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
658{
659 return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
660}
661static inline void *devm_kmalloc_array(struct device *dev,
662 size_t n, size_t size, gfp_t flags)
663{
664 if (size != 0 && n > SIZE_MAX / size)
665 return NULL;
666 return devm_kmalloc(dev, n * size, flags);
667}
668static inline void *devm_kcalloc(struct device *dev,
669 size_t n, size_t size, gfp_t flags)
670{
671 return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
672}
673extern void devm_kfree(struct device *dev, void *p);
674extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp);
675extern void *devm_kmemdup(struct device *dev, const void *src, size_t len,
676 gfp_t gfp);
677
678extern unsigned long devm_get_free_pages(struct device *dev,
679 gfp_t gfp_mask, unsigned int order);
680extern void devm_free_pages(struct device *dev, unsigned long addr);
681
682void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res);
683
684
685int devm_add_action(struct device *dev, void (*action)(void *), void *data);
686void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
687
688static inline int devm_add_action_or_reset(struct device *dev,
689 void (*action)(void *), void *data)
690{
691 int ret;
692
693 ret = devm_add_action(dev, action, data);
694 if (ret)
695 action(data);
696
697 return ret;
698}
699
700struct device_dma_parameters {
701
702
703
704
705 unsigned int max_segment_size;
706 unsigned long segment_boundary_mask;
707};
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780struct device {
781 struct device *parent;
782
783 struct device_private *p;
784
785 struct kobject kobj;
786 const char *init_name;
787 const struct device_type *type;
788
789 struct mutex mutex;
790
791
792
793 struct bus_type *bus;
794 struct device_driver *driver;
795
796 void *platform_data;
797
798 void *driver_data;
799
800 struct dev_pm_info power;
801 struct dev_pm_domain *pm_domain;
802
803#ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
804 struct irq_domain *msi_domain;
805#endif
806#ifdef CONFIG_PINCTRL
807 struct dev_pin_info *pins;
808#endif
809#ifdef CONFIG_GENERIC_MSI_IRQ
810 struct list_head msi_list;
811#endif
812
813#ifdef CONFIG_NUMA
814 int numa_node;
815#endif
816 u64 *dma_mask;
817 u64 coherent_dma_mask;
818
819
820
821
822 unsigned long dma_pfn_offset;
823
824 struct device_dma_parameters *dma_parms;
825
826 struct list_head dma_pools;
827
828 struct dma_coherent_mem *dma_mem;
829
830#ifdef CONFIG_DMA_CMA
831 struct cma *cma_area;
832
833#endif
834
835 struct dev_archdata archdata;
836
837 struct device_node *of_node;
838 struct fwnode_handle *fwnode;
839
840 dev_t devt;
841 u32 id;
842
843 spinlock_t devres_lock;
844 struct list_head devres_head;
845
846 struct klist_node knode_class;
847 struct class *class;
848 const struct attribute_group **groups;
849
850 void (*release)(struct device *dev);
851 struct iommu_group *iommu_group;
852
853 bool offline_disabled:1;
854 bool offline:1;
855};
856
857static inline struct device *kobj_to_dev(struct kobject *kobj)
858{
859 return container_of(kobj, struct device, kobj);
860}
861
862
863#include <linux/pm_wakeup.h>
864
865static inline const char *dev_name(const struct device *dev)
866{
867
868 if (dev->init_name)
869 return dev->init_name;
870
871 return kobject_name(&dev->kobj);
872}
873
874extern __printf(2, 3)
875int dev_set_name(struct device *dev, const char *name, ...);
876
877#ifdef CONFIG_NUMA
878static inline int dev_to_node(struct device *dev)
879{
880 return dev->numa_node;
881}
882static inline void set_dev_node(struct device *dev, int node)
883{
884 dev->numa_node = node;
885}
886#else
887static inline int dev_to_node(struct device *dev)
888{
889 return -1;
890}
891static inline void set_dev_node(struct device *dev, int node)
892{
893}
894#endif
895
896static inline struct irq_domain *dev_get_msi_domain(const struct device *dev)
897{
898#ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
899 return dev->msi_domain;
900#else
901 return NULL;
902#endif
903}
904
905static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d)
906{
907#ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
908 dev->msi_domain = d;
909#endif
910}
911
912static inline void *dev_get_drvdata(const struct device *dev)
913{
914 return dev->driver_data;
915}
916
917static inline void dev_set_drvdata(struct device *dev, void *data)
918{
919 dev->driver_data = data;
920}
921
922static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
923{
924 return dev ? dev->power.subsys_data : NULL;
925}
926
927static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
928{
929 return dev->kobj.uevent_suppress;
930}
931
932static inline void dev_set_uevent_suppress(struct device *dev, int val)
933{
934 dev->kobj.uevent_suppress = val;
935}
936
937static inline int device_is_registered(struct device *dev)
938{
939 return dev->kobj.state_in_sysfs;
940}
941
942static inline void device_enable_async_suspend(struct device *dev)
943{
944 if (!dev->power.is_prepared)
945 dev->power.async_suspend = true;
946}
947
948static inline void device_disable_async_suspend(struct device *dev)
949{
950 if (!dev->power.is_prepared)
951 dev->power.async_suspend = false;
952}
953
954static inline bool device_async_suspend_enabled(struct device *dev)
955{
956 return !!dev->power.async_suspend;
957}
958
959static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
960{
961 dev->power.ignore_children = enable;
962}
963
964static inline void dev_pm_syscore_device(struct device *dev, bool val)
965{
966#ifdef CONFIG_PM_SLEEP
967 dev->power.syscore = val;
968#endif
969}
970
971static inline void device_lock(struct device *dev)
972{
973 mutex_lock(&dev->mutex);
974}
975
976static inline int device_lock_interruptible(struct device *dev)
977{
978 return mutex_lock_interruptible(&dev->mutex);
979}
980
981static inline int device_trylock(struct device *dev)
982{
983 return mutex_trylock(&dev->mutex);
984}
985
986static inline void device_unlock(struct device *dev)
987{
988 mutex_unlock(&dev->mutex);
989}
990
991static inline void device_lock_assert(struct device *dev)
992{
993 lockdep_assert_held(&dev->mutex);
994}
995
996static inline struct device_node *dev_of_node(struct device *dev)
997{
998 if (!IS_ENABLED(CONFIG_OF))
999 return NULL;
1000 return dev->of_node;
1001}
1002
1003void driver_init(void);
1004
1005
1006
1007
1008extern int __must_check device_register(struct device *dev);
1009extern void device_unregister(struct device *dev);
1010extern void device_initialize(struct device *dev);
1011extern int __must_check device_add(struct device *dev);
1012extern void device_del(struct device *dev);
1013extern int device_for_each_child(struct device *dev, void *data,
1014 int (*fn)(struct device *dev, void *data));
1015extern int device_for_each_child_reverse(struct device *dev, void *data,
1016 int (*fn)(struct device *dev, void *data));
1017extern struct device *device_find_child(struct device *dev, void *data,
1018 int (*match)(struct device *dev, void *data));
1019extern int device_rename(struct device *dev, const char *new_name);
1020extern int device_move(struct device *dev, struct device *new_parent,
1021 enum dpm_order dpm_order);
1022extern const char *device_get_devnode(struct device *dev,
1023 umode_t *mode, kuid_t *uid, kgid_t *gid,
1024 const char **tmp);
1025
1026static inline bool device_supports_offline(struct device *dev)
1027{
1028 return dev->bus && dev->bus->offline && dev->bus->online;
1029}
1030
1031extern void lock_device_hotplug(void);
1032extern void unlock_device_hotplug(void);
1033extern int lock_device_hotplug_sysfs(void);
1034extern int device_offline(struct device *dev);
1035extern int device_online(struct device *dev);
1036extern void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1037extern void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1038
1039
1040
1041
1042extern struct device *__root_device_register(const char *name,
1043 struct module *owner);
1044
1045
1046#define root_device_register(name) \
1047 __root_device_register(name, THIS_MODULE)
1048
1049extern void root_device_unregister(struct device *root);
1050
1051static inline void *dev_get_platdata(const struct device *dev)
1052{
1053 return dev->platform_data;
1054}
1055
1056
1057
1058
1059
1060extern int __must_check device_bind_driver(struct device *dev);
1061extern void device_release_driver(struct device *dev);
1062extern int __must_check device_attach(struct device *dev);
1063extern int __must_check driver_attach(struct device_driver *drv);
1064extern void device_initial_probe(struct device *dev);
1065extern int __must_check device_reprobe(struct device *dev);
1066
1067extern bool device_is_bound(struct device *dev);
1068
1069
1070
1071
1072extern __printf(5, 0)
1073struct device *device_create_vargs(struct class *cls, struct device *parent,
1074 dev_t devt, void *drvdata,
1075 const char *fmt, va_list vargs);
1076extern __printf(5, 6)
1077struct device *device_create(struct class *cls, struct device *parent,
1078 dev_t devt, void *drvdata,
1079 const char *fmt, ...);
1080extern __printf(6, 7)
1081struct device *device_create_with_groups(struct class *cls,
1082 struct device *parent, dev_t devt, void *drvdata,
1083 const struct attribute_group **groups,
1084 const char *fmt, ...);
1085extern void device_destroy(struct class *cls, dev_t devt);
1086
1087
1088
1089
1090
1091
1092
1093extern int (*platform_notify)(struct device *dev);
1094
1095extern int (*platform_notify_remove)(struct device *dev);
1096
1097
1098
1099
1100
1101
1102extern struct device *get_device(struct device *dev);
1103extern void put_device(struct device *dev);
1104
1105#ifdef CONFIG_DEVTMPFS
1106extern int devtmpfs_create_node(struct device *dev);
1107extern int devtmpfs_delete_node(struct device *dev);
1108extern int devtmpfs_mount(const char *mntdir);
1109#else
1110static inline int devtmpfs_create_node(struct device *dev) { return 0; }
1111static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
1112static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
1113#endif
1114
1115
1116extern void device_shutdown(void);
1117
1118
1119extern const char *dev_driver_string(const struct device *dev);
1120
1121
1122#ifdef CONFIG_PRINTK
1123
1124extern __printf(3, 0)
1125int dev_vprintk_emit(int level, const struct device *dev,
1126 const char *fmt, va_list args);
1127extern __printf(3, 4)
1128int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...);
1129
1130extern __printf(3, 4)
1131void dev_printk(const char *level, const struct device *dev,
1132 const char *fmt, ...);
1133extern __printf(2, 3)
1134void dev_emerg(const struct device *dev, const char *fmt, ...);
1135extern __printf(2, 3)
1136void dev_alert(const struct device *dev, const char *fmt, ...);
1137extern __printf(2, 3)
1138void dev_crit(const struct device *dev, const char *fmt, ...);
1139extern __printf(2, 3)
1140void dev_err(const struct device *dev, const char *fmt, ...);
1141extern __printf(2, 3)
1142void dev_warn(const struct device *dev, const char *fmt, ...);
1143extern __printf(2, 3)
1144void dev_notice(const struct device *dev, const char *fmt, ...);
1145extern __printf(2, 3)
1146void _dev_info(const struct device *dev, const char *fmt, ...);
1147
1148#else
1149
1150static inline __printf(3, 0)
1151int dev_vprintk_emit(int level, const struct device *dev,
1152 const char *fmt, va_list args)
1153{ return 0; }
1154static inline __printf(3, 4)
1155int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
1156{ return 0; }
1157
1158static inline void __dev_printk(const char *level, const struct device *dev,
1159 struct va_format *vaf)
1160{}
1161static inline __printf(3, 4)
1162void dev_printk(const char *level, const struct device *dev,
1163 const char *fmt, ...)
1164{}
1165
1166static inline __printf(2, 3)
1167void dev_emerg(const struct device *dev, const char *fmt, ...)
1168{}
1169static inline __printf(2, 3)
1170void dev_crit(const struct device *dev, const char *fmt, ...)
1171{}
1172static inline __printf(2, 3)
1173void dev_alert(const struct device *dev, const char *fmt, ...)
1174{}
1175static inline __printf(2, 3)
1176void dev_err(const struct device *dev, const char *fmt, ...)
1177{}
1178static inline __printf(2, 3)
1179void dev_warn(const struct device *dev, const char *fmt, ...)
1180{}
1181static inline __printf(2, 3)
1182void dev_notice(const struct device *dev, const char *fmt, ...)
1183{}
1184static inline __printf(2, 3)
1185void _dev_info(const struct device *dev, const char *fmt, ...)
1186{}
1187
1188#endif
1189
1190
1191
1192
1193
1194
1195
1196
1197#define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
1198
1199#if defined(CONFIG_DYNAMIC_DEBUG)
1200#define dev_dbg(dev, format, ...) \
1201do { \
1202 dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
1203} while (0)
1204#elif defined(DEBUG)
1205#define dev_dbg(dev, format, arg...) \
1206 dev_printk(KERN_DEBUG, dev, format, ##arg)
1207#else
1208#define dev_dbg(dev, format, arg...) \
1209({ \
1210 if (0) \
1211 dev_printk(KERN_DEBUG, dev, format, ##arg); \
1212})
1213#endif
1214
1215#ifdef CONFIG_PRINTK
1216#define dev_level_once(dev_level, dev, fmt, ...) \
1217do { \
1218 static bool __print_once __read_mostly; \
1219 \
1220 if (!__print_once) { \
1221 __print_once = true; \
1222 dev_level(dev, fmt, ##__VA_ARGS__); \
1223 } \
1224} while (0)
1225#else
1226#define dev_level_once(dev_level, dev, fmt, ...) \
1227do { \
1228 if (0) \
1229 dev_level(dev, fmt, ##__VA_ARGS__); \
1230} while (0)
1231#endif
1232
1233#define dev_emerg_once(dev, fmt, ...) \
1234 dev_level_once(dev_emerg, dev, fmt, ##__VA_ARGS__)
1235#define dev_alert_once(dev, fmt, ...) \
1236 dev_level_once(dev_alert, dev, fmt, ##__VA_ARGS__)
1237#define dev_crit_once(dev, fmt, ...) \
1238 dev_level_once(dev_crit, dev, fmt, ##__VA_ARGS__)
1239#define dev_err_once(dev, fmt, ...) \
1240 dev_level_once(dev_err, dev, fmt, ##__VA_ARGS__)
1241#define dev_warn_once(dev, fmt, ...) \
1242 dev_level_once(dev_warn, dev, fmt, ##__VA_ARGS__)
1243#define dev_notice_once(dev, fmt, ...) \
1244 dev_level_once(dev_notice, dev, fmt, ##__VA_ARGS__)
1245#define dev_info_once(dev, fmt, ...) \
1246 dev_level_once(dev_info, dev, fmt, ##__VA_ARGS__)
1247#define dev_dbg_once(dev, fmt, ...) \
1248 dev_level_once(dev_dbg, dev, fmt, ##__VA_ARGS__)
1249
1250#define dev_level_ratelimited(dev_level, dev, fmt, ...) \
1251do { \
1252 static DEFINE_RATELIMIT_STATE(_rs, \
1253 DEFAULT_RATELIMIT_INTERVAL, \
1254 DEFAULT_RATELIMIT_BURST); \
1255 if (__ratelimit(&_rs)) \
1256 dev_level(dev, fmt, ##__VA_ARGS__); \
1257} while (0)
1258
1259#define dev_emerg_ratelimited(dev, fmt, ...) \
1260 dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
1261#define dev_alert_ratelimited(dev, fmt, ...) \
1262 dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
1263#define dev_crit_ratelimited(dev, fmt, ...) \
1264 dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
1265#define dev_err_ratelimited(dev, fmt, ...) \
1266 dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
1267#define dev_warn_ratelimited(dev, fmt, ...) \
1268 dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
1269#define dev_notice_ratelimited(dev, fmt, ...) \
1270 dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
1271#define dev_info_ratelimited(dev, fmt, ...) \
1272 dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
1273#if defined(CONFIG_DYNAMIC_DEBUG)
1274
1275#define dev_dbg_ratelimited(dev, fmt, ...) \
1276do { \
1277 static DEFINE_RATELIMIT_STATE(_rs, \
1278 DEFAULT_RATELIMIT_INTERVAL, \
1279 DEFAULT_RATELIMIT_BURST); \
1280 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
1281 if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \
1282 __ratelimit(&_rs)) \
1283 __dynamic_dev_dbg(&descriptor, dev, fmt, \
1284 ##__VA_ARGS__); \
1285} while (0)
1286#elif defined(DEBUG)
1287#define dev_dbg_ratelimited(dev, fmt, ...) \
1288do { \
1289 static DEFINE_RATELIMIT_STATE(_rs, \
1290 DEFAULT_RATELIMIT_INTERVAL, \
1291 DEFAULT_RATELIMIT_BURST); \
1292 if (__ratelimit(&_rs)) \
1293 dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); \
1294} while (0)
1295#else
1296#define dev_dbg_ratelimited(dev, fmt, ...) \
1297 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
1298#endif
1299
1300#ifdef VERBOSE_DEBUG
1301#define dev_vdbg dev_dbg
1302#else
1303#define dev_vdbg(dev, format, arg...) \
1304({ \
1305 if (0) \
1306 dev_printk(KERN_DEBUG, dev, format, ##arg); \
1307})
1308#endif
1309
1310
1311
1312
1313
1314#define dev_WARN(dev, format, arg...) \
1315 WARN(1, "%s %s: " format, dev_driver_string(dev), dev_name(dev), ## arg);
1316
1317#define dev_WARN_ONCE(dev, condition, format, arg...) \
1318 WARN_ONCE(condition, "%s %s: " format, \
1319 dev_driver_string(dev), dev_name(dev), ## arg)
1320
1321
1322#define MODULE_ALIAS_CHARDEV(major,minor) \
1323 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1324#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1325 MODULE_ALIAS("char-major-" __stringify(major) "-*")
1326
1327#ifdef CONFIG_SYSFS_DEPRECATED
1328extern long sysfs_deprecated;
1329#else
1330#define sysfs_deprecated 0
1331#endif
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347#define module_driver(__driver, __register, __unregister, ...) \
1348static int __init __driver##_init(void) \
1349{ \
1350 return __register(&(__driver) , ##__VA_ARGS__); \
1351} \
1352module_init(__driver##_init); \
1353static void __exit __driver##_exit(void) \
1354{ \
1355 __unregister(&(__driver) , ##__VA_ARGS__); \
1356} \
1357module_exit(__driver##_exit);
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374#define builtin_driver(__driver, __register, ...) \
1375static int __init __driver##_init(void) \
1376{ \
1377 return __register(&(__driver) , ##__VA_ARGS__); \
1378} \
1379device_initcall(__driver##_init);
1380
1381#endif
1382