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