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/module.h>
24#include <linux/pm.h>
25#include <asm/atomic.h>
26#include <asm/device.h>
27
28struct device;
29struct device_private;
30struct device_driver;
31struct driver_private;
32struct class;
33struct subsys_private;
34struct bus_type;
35struct device_node;
36
37struct bus_attribute {
38 struct attribute attr;
39 ssize_t (*show)(struct bus_type *bus, char *buf);
40 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
41};
42
43#define BUS_ATTR(_name, _mode, _show, _store) \
44struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
45
46extern int __must_check bus_create_file(struct bus_type *,
47 struct bus_attribute *);
48extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82struct bus_type {
83 const char *name;
84 struct bus_attribute *bus_attrs;
85 struct device_attribute *dev_attrs;
86 struct driver_attribute *drv_attrs;
87
88 int (*match)(struct device *dev, struct device_driver *drv);
89 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
90 int (*probe)(struct device *dev);
91 int (*remove)(struct device *dev);
92 void (*shutdown)(struct device *dev);
93
94 int (*suspend)(struct device *dev, pm_message_t state);
95 int (*resume)(struct device *dev);
96
97 const struct dev_pm_ops *pm;
98
99 struct subsys_private *p;
100};
101
102extern int __must_check bus_register(struct bus_type *bus);
103extern void bus_unregister(struct bus_type *bus);
104
105extern int __must_check bus_rescan_devices(struct bus_type *bus);
106
107
108
109int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
110 int (*fn)(struct device *dev, void *data));
111struct device *bus_find_device(struct bus_type *bus, struct device *start,
112 void *data,
113 int (*match)(struct device *dev, void *data));
114struct device *bus_find_device_by_name(struct bus_type *bus,
115 struct device *start,
116 const char *name);
117
118int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
119 void *data, int (*fn)(struct device_driver *, void *));
120
121void bus_sort_breadthfirst(struct bus_type *bus,
122 int (*compare)(const struct device *a,
123 const struct device *b));
124
125
126
127
128
129
130struct notifier_block;
131
132extern int bus_register_notifier(struct bus_type *bus,
133 struct notifier_block *nb);
134extern int bus_unregister_notifier(struct bus_type *bus,
135 struct notifier_block *nb);
136
137
138
139
140
141#define BUS_NOTIFY_ADD_DEVICE 0x00000001
142#define BUS_NOTIFY_DEL_DEVICE 0x00000002
143#define BUS_NOTIFY_BIND_DRIVER 0x00000003
144
145#define BUS_NOTIFY_BOUND_DRIVER 0x00000004
146#define BUS_NOTIFY_UNBIND_DRIVER 0x00000005
147
148#define BUS_NOTIFY_UNBOUND_DRIVER 0x00000006
149
150
151extern struct kset *bus_get_kset(struct bus_type *bus);
152extern struct klist *bus_get_device_klist(struct bus_type *bus);
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185struct device_driver {
186 const char *name;
187 struct bus_type *bus;
188
189 struct module *owner;
190 const char *mod_name;
191
192 bool suppress_bind_attrs;
193
194 const struct of_device_id *of_match_table;
195
196 int (*probe) (struct device *dev);
197 int (*remove) (struct device *dev);
198 void (*shutdown) (struct device *dev);
199 int (*suspend) (struct device *dev, pm_message_t state);
200 int (*resume) (struct device *dev);
201 const struct attribute_group **groups;
202
203 const struct dev_pm_ops *pm;
204
205 struct driver_private *p;
206};
207
208
209extern int __must_check driver_register(struct device_driver *drv);
210extern void driver_unregister(struct device_driver *drv);
211
212extern struct device_driver *get_driver(struct device_driver *drv);
213extern void put_driver(struct device_driver *drv);
214extern struct device_driver *driver_find(const char *name,
215 struct bus_type *bus);
216extern int driver_probe_done(void);
217extern void wait_for_device_probe(void);
218
219
220
221
222struct driver_attribute {
223 struct attribute attr;
224 ssize_t (*show)(struct device_driver *driver, char *buf);
225 ssize_t (*store)(struct device_driver *driver, const char *buf,
226 size_t count);
227};
228
229#define DRIVER_ATTR(_name, _mode, _show, _store) \
230struct driver_attribute driver_attr_##_name = \
231 __ATTR(_name, _mode, _show, _store)
232
233extern int __must_check driver_create_file(struct device_driver *driver,
234 const struct driver_attribute *attr);
235extern void driver_remove_file(struct device_driver *driver,
236 const struct driver_attribute *attr);
237
238extern int __must_check driver_add_kobj(struct device_driver *drv,
239 struct kobject *kobj,
240 const char *fmt, ...);
241
242extern int __must_check driver_for_each_device(struct device_driver *drv,
243 struct device *start,
244 void *data,
245 int (*fn)(struct device *dev,
246 void *));
247struct device *driver_find_device(struct device_driver *drv,
248 struct device *start, void *data,
249 int (*match)(struct device *dev, void *data));
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280struct class {
281 const char *name;
282 struct module *owner;
283
284 struct class_attribute *class_attrs;
285 struct device_attribute *dev_attrs;
286 struct bin_attribute *dev_bin_attrs;
287 struct kobject *dev_kobj;
288
289 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
290 char *(*devnode)(struct device *dev, mode_t *mode);
291
292 void (*class_release)(struct class *class);
293 void (*dev_release)(struct device *dev);
294
295 int (*suspend)(struct device *dev, pm_message_t state);
296 int (*resume)(struct device *dev);
297
298 const struct kobj_ns_type_operations *ns_type;
299 const void *(*namespace)(struct device *dev);
300
301 const struct dev_pm_ops *pm;
302
303 struct subsys_private *p;
304};
305
306struct class_dev_iter {
307 struct klist_iter ki;
308 const struct device_type *type;
309};
310
311extern struct kobject *sysfs_dev_block_kobj;
312extern struct kobject *sysfs_dev_char_kobj;
313extern int __must_check __class_register(struct class *class,
314 struct lock_class_key *key);
315extern void class_unregister(struct class *class);
316
317
318
319#define class_register(class) \
320({ \
321 static struct lock_class_key __key; \
322 __class_register(class, &__key); \
323})
324
325struct class_compat;
326struct class_compat *class_compat_register(const char *name);
327void class_compat_unregister(struct class_compat *cls);
328int class_compat_create_link(struct class_compat *cls, struct device *dev,
329 struct device *device_link);
330void class_compat_remove_link(struct class_compat *cls, struct device *dev,
331 struct device *device_link);
332
333extern void class_dev_iter_init(struct class_dev_iter *iter,
334 struct class *class,
335 struct device *start,
336 const struct device_type *type);
337extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
338extern void class_dev_iter_exit(struct class_dev_iter *iter);
339
340extern int class_for_each_device(struct class *class, struct device *start,
341 void *data,
342 int (*fn)(struct device *dev, void *data));
343extern struct device *class_find_device(struct class *class,
344 struct device *start, void *data,
345 int (*match)(struct device *, void *));
346
347struct class_attribute {
348 struct attribute attr;
349 ssize_t (*show)(struct class *class, struct class_attribute *attr,
350 char *buf);
351 ssize_t (*store)(struct class *class, struct class_attribute *attr,
352 const char *buf, size_t count);
353};
354
355#define CLASS_ATTR(_name, _mode, _show, _store) \
356struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
357
358extern int __must_check class_create_file(struct class *class,
359 const struct class_attribute *attr);
360extern void class_remove_file(struct class *class,
361 const struct class_attribute *attr);
362
363
364
365struct class_attribute_string {
366 struct class_attribute attr;
367 char *str;
368};
369
370
371#define _CLASS_ATTR_STRING(_name, _mode, _str) \
372 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
373#define CLASS_ATTR_STRING(_name, _mode, _str) \
374 struct class_attribute_string class_attr_##_name = \
375 _CLASS_ATTR_STRING(_name, _mode, _str)
376
377extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
378 char *buf);
379
380struct class_interface {
381 struct list_head node;
382 struct class *class;
383
384 int (*add_dev) (struct device *, struct class_interface *);
385 void (*remove_dev) (struct device *, struct class_interface *);
386};
387
388extern int __must_check class_interface_register(struct class_interface *);
389extern void class_interface_unregister(struct class_interface *);
390
391extern struct class * __must_check __class_create(struct module *owner,
392 const char *name,
393 struct lock_class_key *key);
394extern void class_destroy(struct class *cls);
395
396
397
398#define class_create(owner, name) \
399({ \
400 static struct lock_class_key __key; \
401 __class_create(owner, name, &__key); \
402})
403
404
405
406
407
408
409
410
411
412
413struct device_type {
414 const char *name;
415 const struct attribute_group **groups;
416 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
417 char *(*devnode)(struct device *dev, mode_t *mode);
418 void (*release)(struct device *dev);
419
420 const struct dev_pm_ops *pm;
421};
422
423
424struct device_attribute {
425 struct attribute attr;
426 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
427 char *buf);
428 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
429 const char *buf, size_t count);
430};
431
432#define DEVICE_ATTR(_name, _mode, _show, _store) \
433struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
434
435extern int __must_check device_create_file(struct device *device,
436 const struct device_attribute *entry);
437extern void device_remove_file(struct device *dev,
438 const struct device_attribute *attr);
439extern int __must_check device_create_bin_file(struct device *dev,
440 const struct bin_attribute *attr);
441extern void device_remove_bin_file(struct device *dev,
442 const struct bin_attribute *attr);
443extern int device_schedule_callback_owner(struct device *dev,
444 void (*func)(struct device *dev), struct module *owner);
445
446
447#define device_schedule_callback(dev, func) \
448 device_schedule_callback_owner(dev, func, THIS_MODULE)
449
450
451typedef void (*dr_release_t)(struct device *dev, void *res);
452typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
453
454#ifdef CONFIG_DEBUG_DEVRES
455extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
456 const char *name);
457#define devres_alloc(release, size, gfp) \
458 __devres_alloc(release, size, gfp, #release)
459#else
460extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
461#endif
462extern void devres_free(void *res);
463extern void devres_add(struct device *dev, void *res);
464extern void *devres_find(struct device *dev, dr_release_t release,
465 dr_match_t match, void *match_data);
466extern void *devres_get(struct device *dev, void *new_res,
467 dr_match_t match, void *match_data);
468extern void *devres_remove(struct device *dev, dr_release_t release,
469 dr_match_t match, void *match_data);
470extern int devres_destroy(struct device *dev, dr_release_t release,
471 dr_match_t match, void *match_data);
472
473
474extern void * __must_check devres_open_group(struct device *dev, void *id,
475 gfp_t gfp);
476extern void devres_close_group(struct device *dev, void *id);
477extern void devres_remove_group(struct device *dev, void *id);
478extern int devres_release_group(struct device *dev, void *id);
479
480
481extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
482extern void devm_kfree(struct device *dev, void *p);
483
484struct device_dma_parameters {
485
486
487
488
489 unsigned int max_segment_size;
490 unsigned long segment_boundary_mask;
491};
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551struct device {
552 struct device *parent;
553
554 struct device_private *p;
555
556 struct kobject kobj;
557 const char *init_name;
558 const struct device_type *type;
559
560 struct mutex mutex;
561
562
563
564 struct bus_type *bus;
565 struct device_driver *driver;
566
567 void *platform_data;
568
569 struct dev_pm_info power;
570 struct dev_power_domain *pwr_domain;
571
572#ifdef CONFIG_NUMA
573 int numa_node;
574#endif
575 u64 *dma_mask;
576 u64 coherent_dma_mask;
577
578
579
580
581
582 struct device_dma_parameters *dma_parms;
583
584 struct list_head dma_pools;
585
586 struct dma_coherent_mem *dma_mem;
587
588
589 struct dev_archdata archdata;
590
591 struct device_node *of_node;
592
593 dev_t devt;
594
595 spinlock_t devres_lock;
596 struct list_head devres_head;
597
598 struct klist_node knode_class;
599 struct class *class;
600 const struct attribute_group **groups;
601
602 void (*release)(struct device *dev);
603};
604
605
606#include <linux/pm_wakeup.h>
607
608static inline const char *dev_name(const struct device *dev)
609{
610
611 if (dev->init_name)
612 return dev->init_name;
613
614 return kobject_name(&dev->kobj);
615}
616
617extern int dev_set_name(struct device *dev, const char *name, ...)
618 __attribute__((format(printf, 2, 3)));
619
620#ifdef CONFIG_NUMA
621static inline int dev_to_node(struct device *dev)
622{
623 return dev->numa_node;
624}
625static inline void set_dev_node(struct device *dev, int node)
626{
627 dev->numa_node = node;
628}
629#else
630static inline int dev_to_node(struct device *dev)
631{
632 return -1;
633}
634static inline void set_dev_node(struct device *dev, int node)
635{
636}
637#endif
638
639static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
640{
641 return dev->kobj.uevent_suppress;
642}
643
644static inline void dev_set_uevent_suppress(struct device *dev, int val)
645{
646 dev->kobj.uevent_suppress = val;
647}
648
649static inline int device_is_registered(struct device *dev)
650{
651 return dev->kobj.state_in_sysfs;
652}
653
654static inline void device_enable_async_suspend(struct device *dev)
655{
656 if (!dev->power.is_prepared)
657 dev->power.async_suspend = true;
658}
659
660static inline void device_disable_async_suspend(struct device *dev)
661{
662 if (!dev->power.is_prepared)
663 dev->power.async_suspend = false;
664}
665
666static inline bool device_async_suspend_enabled(struct device *dev)
667{
668 return !!dev->power.async_suspend;
669}
670
671static inline void device_lock(struct device *dev)
672{
673 mutex_lock(&dev->mutex);
674}
675
676static inline int device_trylock(struct device *dev)
677{
678 return mutex_trylock(&dev->mutex);
679}
680
681static inline void device_unlock(struct device *dev)
682{
683 mutex_unlock(&dev->mutex);
684}
685
686void driver_init(void);
687
688
689
690
691extern int __must_check device_register(struct device *dev);
692extern void device_unregister(struct device *dev);
693extern void device_initialize(struct device *dev);
694extern int __must_check device_add(struct device *dev);
695extern void device_del(struct device *dev);
696extern int device_for_each_child(struct device *dev, void *data,
697 int (*fn)(struct device *dev, void *data));
698extern struct device *device_find_child(struct device *dev, void *data,
699 int (*match)(struct device *dev, void *data));
700extern int device_rename(struct device *dev, const char *new_name);
701extern int device_move(struct device *dev, struct device *new_parent,
702 enum dpm_order dpm_order);
703extern const char *device_get_devnode(struct device *dev,
704 mode_t *mode, const char **tmp);
705extern void *dev_get_drvdata(const struct device *dev);
706extern int dev_set_drvdata(struct device *dev, void *data);
707
708
709
710
711extern struct device *__root_device_register(const char *name,
712 struct module *owner);
713static inline struct device *root_device_register(const char *name)
714{
715 return __root_device_register(name, THIS_MODULE);
716}
717extern void root_device_unregister(struct device *root);
718
719static inline void *dev_get_platdata(const struct device *dev)
720{
721 return dev->platform_data;
722}
723
724
725
726
727
728extern int __must_check device_bind_driver(struct device *dev);
729extern void device_release_driver(struct device *dev);
730extern int __must_check device_attach(struct device *dev);
731extern int __must_check driver_attach(struct device_driver *drv);
732extern int __must_check device_reprobe(struct device *dev);
733
734
735
736
737extern struct device *device_create_vargs(struct class *cls,
738 struct device *parent,
739 dev_t devt,
740 void *drvdata,
741 const char *fmt,
742 va_list vargs);
743extern struct device *device_create(struct class *cls, struct device *parent,
744 dev_t devt, void *drvdata,
745 const char *fmt, ...)
746 __attribute__((format(printf, 5, 6)));
747extern void device_destroy(struct class *cls, dev_t devt);
748
749
750
751
752
753
754
755extern int (*platform_notify)(struct device *dev);
756
757extern int (*platform_notify_remove)(struct device *dev);
758
759
760
761
762
763
764extern struct device *get_device(struct device *dev);
765extern void put_device(struct device *dev);
766
767extern void wait_for_device_probe(void);
768
769#ifdef CONFIG_DEVTMPFS
770extern int devtmpfs_create_node(struct device *dev);
771extern int devtmpfs_delete_node(struct device *dev);
772extern int devtmpfs_mount(const char *mntdir);
773#else
774static inline int devtmpfs_create_node(struct device *dev) { return 0; }
775static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
776static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
777#endif
778
779
780extern void device_shutdown(void);
781
782
783extern const char *dev_driver_string(const struct device *dev);
784
785
786#ifdef CONFIG_PRINTK
787
788extern int dev_printk(const char *level, const struct device *dev,
789 const char *fmt, ...)
790 __attribute__ ((format (printf, 3, 4)));
791extern int dev_emerg(const struct device *dev, const char *fmt, ...)
792 __attribute__ ((format (printf, 2, 3)));
793extern int dev_alert(const struct device *dev, const char *fmt, ...)
794 __attribute__ ((format (printf, 2, 3)));
795extern int dev_crit(const struct device *dev, const char *fmt, ...)
796 __attribute__ ((format (printf, 2, 3)));
797extern int dev_err(const struct device *dev, const char *fmt, ...)
798 __attribute__ ((format (printf, 2, 3)));
799extern int dev_warn(const struct device *dev, const char *fmt, ...)
800 __attribute__ ((format (printf, 2, 3)));
801extern int dev_notice(const struct device *dev, const char *fmt, ...)
802 __attribute__ ((format (printf, 2, 3)));
803extern int _dev_info(const struct device *dev, const char *fmt, ...)
804 __attribute__ ((format (printf, 2, 3)));
805
806#else
807
808static inline int dev_printk(const char *level, const struct device *dev,
809 const char *fmt, ...)
810 __attribute__ ((format (printf, 3, 4)));
811static inline int dev_printk(const char *level, const struct device *dev,
812 const char *fmt, ...)
813 { return 0; }
814
815static inline int dev_emerg(const struct device *dev, const char *fmt, ...)
816 __attribute__ ((format (printf, 2, 3)));
817static inline int dev_emerg(const struct device *dev, const char *fmt, ...)
818 { return 0; }
819static inline int dev_crit(const struct device *dev, const char *fmt, ...)
820 __attribute__ ((format (printf, 2, 3)));
821static inline int dev_crit(const struct device *dev, const char *fmt, ...)
822 { return 0; }
823static inline int dev_alert(const struct device *dev, const char *fmt, ...)
824 __attribute__ ((format (printf, 2, 3)));
825static inline int dev_alert(const struct device *dev, const char *fmt, ...)
826 { return 0; }
827static inline int dev_err(const struct device *dev, const char *fmt, ...)
828 __attribute__ ((format (printf, 2, 3)));
829static inline int dev_err(const struct device *dev, const char *fmt, ...)
830 { return 0; }
831static inline int dev_warn(const struct device *dev, const char *fmt, ...)
832 __attribute__ ((format (printf, 2, 3)));
833static inline int dev_warn(const struct device *dev, const char *fmt, ...)
834 { return 0; }
835static inline int dev_notice(const struct device *dev, const char *fmt, ...)
836 __attribute__ ((format (printf, 2, 3)));
837static inline int dev_notice(const struct device *dev, const char *fmt, ...)
838 { return 0; }
839static inline int _dev_info(const struct device *dev, const char *fmt, ...)
840 __attribute__ ((format (printf, 2, 3)));
841static inline int _dev_info(const struct device *dev, const char *fmt, ...)
842 { return 0; }
843
844#endif
845
846
847
848
849
850
851
852
853#define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
854
855#if defined(DEBUG)
856#define dev_dbg(dev, format, arg...) \
857 dev_printk(KERN_DEBUG, dev, format, ##arg)
858#elif defined(CONFIG_DYNAMIC_DEBUG)
859#define dev_dbg(dev, format, ...) \
860do { \
861 dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
862} while (0)
863#else
864#define dev_dbg(dev, format, arg...) \
865({ \
866 if (0) \
867 dev_printk(KERN_DEBUG, dev, format, ##arg); \
868 0; \
869})
870#endif
871
872#ifdef VERBOSE_DEBUG
873#define dev_vdbg dev_dbg
874#else
875#define dev_vdbg(dev, format, arg...) \
876({ \
877 if (0) \
878 dev_printk(KERN_DEBUG, dev, format, ##arg); \
879 0; \
880})
881#endif
882
883
884
885
886
887
888#define dev_WARN(dev, format, arg...) \
889 WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg);
890
891#define dev_WARN_ONCE(dev, condition, format, arg...) \
892 WARN_ONCE(condition, "Device %s\n" format, \
893 dev_driver_string(dev), ## arg)
894
895
896#define MODULE_ALIAS_CHARDEV(major,minor) \
897 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
898#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
899 MODULE_ALIAS("char-major-" __stringify(major) "-*")
900
901#ifdef CONFIG_SYSFS_DEPRECATED
902extern long sysfs_deprecated;
903#else
904#define sysfs_deprecated 0
905#endif
906
907#endif
908