device.h 16 KB

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