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