device.h 14 KB

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