device.h 15 KB

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