device.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * device.h - generic, centralized driver model
  3. *
  4. * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
  5. *
  6. * This file is released under the GPLv2
  7. *
  8. * See Documentation/driver-model/ for more information.
  9. */
  10. #ifndef _DEVICE_H_
  11. #define _DEVICE_H_
  12. #include <linux/ioport.h>
  13. #include <linux/kobject.h>
  14. #include <linux/klist.h>
  15. #include <linux/list.h>
  16. #include <linux/compiler.h>
  17. #include <linux/types.h>
  18. #include <linux/module.h>
  19. #include <linux/pm.h>
  20. #include <asm/semaphore.h>
  21. #include <asm/atomic.h>
  22. #define DEVICE_NAME_SIZE 50
  23. #define DEVICE_NAME_HALF __stringify(20) /* Less than half to accommodate slop */
  24. #define DEVICE_ID_SIZE 32
  25. #define BUS_ID_SIZE KOBJ_NAME_LEN
  26. struct device;
  27. struct device_driver;
  28. struct class;
  29. struct class_device;
  30. struct bus_type {
  31. const char * name;
  32. struct subsystem subsys;
  33. struct kset drivers;
  34. struct kset devices;
  35. struct klist klist_devices;
  36. struct klist klist_drivers;
  37. struct blocking_notifier_head bus_notifier;
  38. struct bus_attribute * bus_attrs;
  39. struct device_attribute * dev_attrs;
  40. struct driver_attribute * drv_attrs;
  41. int (*match)(struct device * dev, struct device_driver * drv);
  42. int (*uevent)(struct device *dev, char **envp,
  43. int num_envp, char *buffer, int buffer_size);
  44. int (*probe)(struct device * dev);
  45. int (*remove)(struct device * dev);
  46. void (*shutdown)(struct device * dev);
  47. int (*suspend)(struct device * dev, pm_message_t state);
  48. int (*suspend_late)(struct device * dev, pm_message_t state);
  49. int (*resume_early)(struct device * dev);
  50. int (*resume)(struct device * dev);
  51. };
  52. extern int __must_check bus_register(struct bus_type * bus);
  53. extern void bus_unregister(struct bus_type * bus);
  54. extern int __must_check bus_rescan_devices(struct bus_type * bus);
  55. /* iterator helpers for buses */
  56. int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
  57. int (*fn)(struct device *, void *));
  58. struct device * bus_find_device(struct bus_type *bus, struct device *start,
  59. void *data, int (*match)(struct device *, void *));
  60. int __must_check bus_for_each_drv(struct bus_type *bus,
  61. struct device_driver *start, void *data,
  62. int (*fn)(struct device_driver *, void *));
  63. /*
  64. * Bus notifiers: Get notified of addition/removal of devices
  65. * and binding/unbinding of drivers to devices.
  66. * In the long run, it should be a replacement for the platform
  67. * notify hooks.
  68. */
  69. struct notifier_block;
  70. extern int bus_register_notifier(struct bus_type *bus,
  71. struct notifier_block *nb);
  72. extern int bus_unregister_notifier(struct bus_type *bus,
  73. struct notifier_block *nb);
  74. /* All 4 notifers below get called with the target struct device *
  75. * as an argument. Note that those functions are likely to be called
  76. * with the device semaphore held in the core, so be careful.
  77. */
  78. #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
  79. #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
  80. #define BUS_NOTIFY_BOUND_DRIVER 0x00000003 /* driver bound to device */
  81. #define BUS_NOTIFY_UNBIND_DRIVER 0x00000004 /* driver about to be
  82. unbound */
  83. /* driverfs interface for exporting bus attributes */
  84. struct bus_attribute {
  85. struct attribute attr;
  86. ssize_t (*show)(struct bus_type *, char * buf);
  87. ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
  88. };
  89. #define BUS_ATTR(_name,_mode,_show,_store) \
  90. struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store)
  91. extern int __must_check bus_create_file(struct bus_type *,
  92. struct bus_attribute *);
  93. extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
  94. struct device_driver {
  95. const char * name;
  96. struct bus_type * bus;
  97. struct completion unloaded;
  98. struct kobject kobj;
  99. struct klist klist_devices;
  100. struct klist_node knode_bus;
  101. struct module * owner;
  102. int (*probe) (struct device * dev);
  103. int (*remove) (struct device * dev);
  104. void (*shutdown) (struct device * dev);
  105. int (*suspend) (struct device * dev, pm_message_t state);
  106. int (*resume) (struct device * dev);
  107. unsigned int multithread_probe:1;
  108. };
  109. extern int __must_check driver_register(struct device_driver * drv);
  110. extern void driver_unregister(struct device_driver * drv);
  111. extern struct device_driver * get_driver(struct device_driver * drv);
  112. extern void put_driver(struct device_driver * drv);
  113. extern struct device_driver *driver_find(const char *name, struct bus_type *bus);
  114. extern int driver_probe_done(void);
  115. /* driverfs interface for exporting driver attributes */
  116. struct driver_attribute {
  117. struct attribute attr;
  118. ssize_t (*show)(struct device_driver *, char * buf);
  119. ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
  120. };
  121. #define DRIVER_ATTR(_name,_mode,_show,_store) \
  122. struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
  123. extern int __must_check driver_create_file(struct device_driver *,
  124. struct driver_attribute *);
  125. extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
  126. extern int __must_check driver_for_each_device(struct device_driver * drv,
  127. struct device *start, void *data,
  128. int (*fn)(struct device *, void *));
  129. struct device * driver_find_device(struct device_driver *drv,
  130. struct device *start, void *data,
  131. int (*match)(struct device *, void *));
  132. /*
  133. * device classes
  134. */
  135. struct class {
  136. const char * name;
  137. struct module * owner;
  138. struct subsystem subsys;
  139. struct list_head children;
  140. struct list_head devices;
  141. struct list_head interfaces;
  142. struct semaphore sem; /* locks both the children and interfaces lists */
  143. struct kobject *virtual_dir;
  144. struct class_attribute * class_attrs;
  145. struct class_device_attribute * class_dev_attrs;
  146. struct device_attribute * dev_attrs;
  147. int (*uevent)(struct class_device *dev, char **envp,
  148. int num_envp, char *buffer, int buffer_size);
  149. int (*dev_uevent)(struct device *dev, char **envp, int num_envp,
  150. char *buffer, int buffer_size);
  151. void (*release)(struct class_device *dev);
  152. void (*class_release)(struct class *class);
  153. void (*dev_release)(struct device *dev);
  154. int (*suspend)(struct device *, pm_message_t state);
  155. int (*resume)(struct device *);
  156. };
  157. extern int __must_check class_register(struct class *);
  158. extern void class_unregister(struct class *);
  159. struct class_attribute {
  160. struct attribute attr;
  161. ssize_t (*show)(struct class *, char * buf);
  162. ssize_t (*store)(struct class *, const char * buf, size_t count);
  163. };
  164. #define CLASS_ATTR(_name,_mode,_show,_store) \
  165. struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store)
  166. extern int __must_check class_create_file(struct class *,
  167. const struct class_attribute *);
  168. extern void class_remove_file(struct class *, const struct class_attribute *);
  169. struct class_device_attribute {
  170. struct attribute attr;
  171. ssize_t (*show)(struct class_device *, char * buf);
  172. ssize_t (*store)(struct class_device *, const char * buf, size_t count);
  173. };
  174. #define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
  175. struct class_device_attribute class_device_attr_##_name = \
  176. __ATTR(_name,_mode,_show,_store)
  177. extern int __must_check class_device_create_file(struct class_device *,
  178. const struct class_device_attribute *);
  179. /**
  180. * struct class_device - class devices
  181. * @class: pointer to the parent class for this class device. This is required.
  182. * @devt: for internal use by the driver core only.
  183. * @node: for internal use by the driver core only.
  184. * @kobj: for internal use by the driver core only.
  185. * @devt_attr: for internal use by the driver core only.
  186. * @groups: optional additional groups to be created
  187. * @dev: if set, a symlink to the struct device is created in the sysfs
  188. * directory for this struct class device.
  189. * @class_data: pointer to whatever you want to store here for this struct
  190. * class_device. Use class_get_devdata() and class_set_devdata() to get and
  191. * set this pointer.
  192. * @parent: pointer to a struct class_device that is the parent of this struct
  193. * class_device. If NULL, this class_device will show up at the root of the
  194. * struct class in sysfs (which is probably what you want to have happen.)
  195. * @release: pointer to a release function for this struct class_device. If
  196. * set, this will be called instead of the class specific release function.
  197. * Only use this if you want to override the default release function, like
  198. * when you are nesting class_device structures.
  199. * @uevent: pointer to a uevent function for this struct class_device. If
  200. * set, this will be called instead of the class specific uevent function.
  201. * Only use this if you want to override the default uevent function, like
  202. * when you are nesting class_device structures.
  203. */
  204. struct class_device {
  205. struct list_head node;
  206. struct kobject kobj;
  207. struct class * class; /* required */
  208. dev_t devt; /* dev_t, creates the sysfs "dev" */
  209. struct class_device_attribute *devt_attr;
  210. struct class_device_attribute uevent_attr;
  211. struct device * dev; /* not necessary, but nice to have */
  212. void * class_data; /* class-specific data */
  213. struct class_device *parent; /* parent of this child device, if there is one */
  214. struct attribute_group ** groups; /* optional groups */
  215. void (*release)(struct class_device *dev);
  216. int (*uevent)(struct class_device *dev, char **envp,
  217. int num_envp, char *buffer, int buffer_size);
  218. char class_id[BUS_ID_SIZE]; /* unique to this class */
  219. };
  220. static inline void *
  221. class_get_devdata (struct class_device *dev)
  222. {
  223. return dev->class_data;
  224. }
  225. static inline void
  226. class_set_devdata (struct class_device *dev, void *data)
  227. {
  228. dev->class_data = data;
  229. }
  230. extern int __must_check class_device_register(struct class_device *);
  231. extern void class_device_unregister(struct class_device *);
  232. extern void class_device_initialize(struct class_device *);
  233. extern int __must_check class_device_add(struct class_device *);
  234. extern void class_device_del(struct class_device *);
  235. extern int class_device_rename(struct class_device *, char *);
  236. extern struct class_device * class_device_get(struct class_device *);
  237. extern void class_device_put(struct class_device *);
  238. extern void class_device_remove_file(struct class_device *,
  239. const struct class_device_attribute *);
  240. extern int __must_check class_device_create_bin_file(struct class_device *,
  241. struct bin_attribute *);
  242. extern void class_device_remove_bin_file(struct class_device *,
  243. struct bin_attribute *);
  244. struct class_interface {
  245. struct list_head node;
  246. struct class *class;
  247. int (*add) (struct class_device *, struct class_interface *);
  248. void (*remove) (struct class_device *, struct class_interface *);
  249. int (*add_dev) (struct device *, struct class_interface *);
  250. void (*remove_dev) (struct device *, struct class_interface *);
  251. };
  252. extern int __must_check class_interface_register(struct class_interface *);
  253. extern void class_interface_unregister(struct class_interface *);
  254. extern struct class *class_create(struct module *owner, const char *name);
  255. extern void class_destroy(struct class *cls);
  256. extern struct class_device *class_device_create(struct class *cls,
  257. struct class_device *parent,
  258. dev_t devt,
  259. struct device *device,
  260. const char *fmt, ...)
  261. __attribute__((format(printf,5,6)));
  262. extern void class_device_destroy(struct class *cls, dev_t devt);
  263. /* interface for exporting device attributes */
  264. struct device_attribute {
  265. struct attribute attr;
  266. ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  267. char *buf);
  268. ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  269. const char *buf, size_t count);
  270. };
  271. #define DEVICE_ATTR(_name,_mode,_show,_store) \
  272. struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
  273. extern int __must_check device_create_file(struct device *device,
  274. struct device_attribute * entry);
  275. extern void device_remove_file(struct device * dev, struct device_attribute * attr);
  276. extern int __must_check device_create_bin_file(struct device *dev,
  277. struct bin_attribute *attr);
  278. extern void device_remove_bin_file(struct device *dev,
  279. struct bin_attribute *attr);
  280. struct device {
  281. struct klist klist_children;
  282. struct klist_node knode_parent; /* node in sibling list */
  283. struct klist_node knode_driver;
  284. struct klist_node knode_bus;
  285. struct device * parent;
  286. struct kobject kobj;
  287. char bus_id[BUS_ID_SIZE]; /* position on parent bus */
  288. unsigned is_registered:1;
  289. struct device_attribute uevent_attr;
  290. struct device_attribute *devt_attr;
  291. struct semaphore sem; /* semaphore to synchronize calls to
  292. * its driver.
  293. */
  294. struct bus_type * bus; /* type of bus device is on */
  295. struct device_driver *driver; /* which driver has allocated this
  296. device */
  297. void *driver_data; /* data private to the driver */
  298. void *platform_data; /* Platform specific data, device
  299. core doesn't touch it */
  300. void *firmware_data; /* Firmware specific data (e.g. ACPI,
  301. BIOS data),reserved for device core*/
  302. struct dev_pm_info power;
  303. u64 *dma_mask; /* dma mask (if dma'able device) */
  304. u64 coherent_dma_mask;/* Like dma_mask, but for
  305. alloc_coherent mappings as
  306. not all hardware supports
  307. 64 bit addresses for consistent
  308. allocations such descriptors. */
  309. struct list_head dma_pools; /* dma pools (if dma'ble) */
  310. struct dma_coherent_mem *dma_mem; /* internal for coherent mem
  311. override */
  312. /* class_device migration path */
  313. struct list_head node;
  314. struct class *class; /* optional*/
  315. dev_t devt; /* dev_t, creates the sysfs "dev" */
  316. struct attribute_group **groups; /* optional groups */
  317. void (*release)(struct device * dev);
  318. };
  319. static inline void *
  320. dev_get_drvdata (struct device *dev)
  321. {
  322. return dev->driver_data;
  323. }
  324. static inline void
  325. dev_set_drvdata (struct device *dev, void *data)
  326. {
  327. dev->driver_data = data;
  328. }
  329. static inline int device_is_registered(struct device *dev)
  330. {
  331. return dev->is_registered;
  332. }
  333. /*
  334. * High level routines for use by the bus drivers
  335. */
  336. extern int __must_check device_register(struct device * dev);
  337. extern void device_unregister(struct device * dev);
  338. extern void device_initialize(struct device * dev);
  339. extern int __must_check device_add(struct device * dev);
  340. extern void device_del(struct device * dev);
  341. extern int device_for_each_child(struct device *, void *,
  342. int (*fn)(struct device *, void *));
  343. extern int device_rename(struct device *dev, char *new_name);
  344. /*
  345. * Manual binding of a device to driver. See drivers/base/bus.c
  346. * for information on use.
  347. */
  348. extern int __must_check device_bind_driver(struct device *dev);
  349. extern void device_release_driver(struct device * dev);
  350. extern int __must_check device_attach(struct device * dev);
  351. extern int __must_check driver_attach(struct device_driver *drv);
  352. extern int __must_check device_reprobe(struct device *dev);
  353. /*
  354. * Easy functions for dynamically creating devices on the fly
  355. */
  356. extern struct device *device_create(struct class *cls, struct device *parent,
  357. dev_t devt, const char *fmt, ...)
  358. __attribute__((format(printf,4,5)));
  359. extern void device_destroy(struct class *cls, dev_t devt);
  360. /*
  361. * Platform "fixup" functions - allow the platform to have their say
  362. * about devices and actions that the general device layer doesn't
  363. * know about.
  364. */
  365. /* Notify platform of device discovery */
  366. extern int (*platform_notify)(struct device * dev);
  367. extern int (*platform_notify_remove)(struct device * dev);
  368. /**
  369. * get_device - atomically increment the reference count for the device.
  370. *
  371. */
  372. extern struct device * get_device(struct device * dev);
  373. extern void put_device(struct device * dev);
  374. /* drivers/base/power/shutdown.c */
  375. extern void device_shutdown(void);
  376. /* drivers/base/firmware.c */
  377. extern int __must_check firmware_register(struct subsystem *);
  378. extern void firmware_unregister(struct subsystem *);
  379. /* debugging and troubleshooting/diagnostic helpers. */
  380. extern const char *dev_driver_string(struct device *dev);
  381. #define dev_printk(level, dev, format, arg...) \
  382. printk(level "%s %s: " format , dev_driver_string(dev) , (dev)->bus_id , ## arg)
  383. #ifdef DEBUG
  384. #define dev_dbg(dev, format, arg...) \
  385. dev_printk(KERN_DEBUG , dev , format , ## arg)
  386. #else
  387. #define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
  388. #endif
  389. #define dev_err(dev, format, arg...) \
  390. dev_printk(KERN_ERR , dev , format , ## arg)
  391. #define dev_info(dev, format, arg...) \
  392. dev_printk(KERN_INFO , dev , format , ## arg)
  393. #define dev_warn(dev, format, arg...) \
  394. dev_printk(KERN_WARNING , dev , format , ## arg)
  395. #define dev_notice(dev, format, arg...) \
  396. dev_printk(KERN_NOTICE , dev , format , ## arg)
  397. /* Create alias, so I can be autoloaded. */
  398. #define MODULE_ALIAS_CHARDEV(major,minor) \
  399. MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
  400. #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
  401. MODULE_ALIAS("char-major-" __stringify(major) "-*")
  402. #endif /* _DEVICE_H_ */