device.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 int 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. int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
  67. void * data, int (*fn)(struct device_driver *, void *));
  68. /* driverfs interface for exporting bus attributes */
  69. struct bus_attribute {
  70. struct attribute attr;
  71. ssize_t (*show)(struct bus_type *, char * buf);
  72. ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
  73. };
  74. #define BUS_ATTR(_name,_mode,_show,_store) \
  75. struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store)
  76. extern int bus_create_file(struct bus_type *, struct bus_attribute *);
  77. extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
  78. struct device_driver {
  79. const char * name;
  80. struct bus_type * bus;
  81. struct completion unloaded;
  82. struct kobject kobj;
  83. struct klist klist_devices;
  84. struct klist_node knode_bus;
  85. struct module * owner;
  86. int (*probe) (struct device * dev);
  87. int (*remove) (struct device * dev);
  88. void (*shutdown) (struct device * dev);
  89. int (*suspend) (struct device * dev, pm_message_t state, u32 level);
  90. int (*resume) (struct device * dev, u32 level);
  91. };
  92. extern int driver_register(struct device_driver * drv);
  93. extern void driver_unregister(struct device_driver * drv);
  94. extern struct device_driver * get_driver(struct device_driver * drv);
  95. extern void put_driver(struct device_driver * drv);
  96. extern struct device_driver *driver_find(const char *name, struct bus_type *bus);
  97. /* driverfs interface for exporting driver attributes */
  98. struct driver_attribute {
  99. struct attribute attr;
  100. ssize_t (*show)(struct device_driver *, char * buf);
  101. ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
  102. };
  103. #define DRIVER_ATTR(_name,_mode,_show,_store) \
  104. struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
  105. extern int driver_create_file(struct device_driver *, struct driver_attribute *);
  106. extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
  107. extern int driver_for_each_device(struct device_driver * drv, struct device * start,
  108. void * data, int (*fn)(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 interfaces;
  118. struct semaphore sem; /* locks both the children and interfaces lists */
  119. struct class_attribute * class_attrs;
  120. struct class_device_attribute * class_dev_attrs;
  121. int (*hotplug)(struct class_device *dev, char **envp,
  122. int num_envp, char *buffer, int buffer_size);
  123. void (*release)(struct class_device *dev);
  124. void (*class_release)(struct class *class);
  125. };
  126. extern int class_register(struct class *);
  127. extern void class_unregister(struct class *);
  128. extern struct class * class_get(struct class *);
  129. extern void class_put(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 {
  140. struct list_head node;
  141. struct kobject kobj;
  142. struct class * class; /* required */
  143. dev_t devt; /* dev_t, creates the sysfs "dev" */
  144. struct class_device_attribute *devt_attr;
  145. struct device * dev; /* not necessary, but nice to have */
  146. void * class_data; /* class-specific data */
  147. char class_id[BUS_ID_SIZE]; /* unique to this class */
  148. };
  149. static inline void *
  150. class_get_devdata (struct class_device *dev)
  151. {
  152. return dev->class_data;
  153. }
  154. static inline void
  155. class_set_devdata (struct class_device *dev, void *data)
  156. {
  157. dev->class_data = data;
  158. }
  159. extern int class_device_register(struct class_device *);
  160. extern void class_device_unregister(struct class_device *);
  161. extern void class_device_initialize(struct class_device *);
  162. extern int class_device_add(struct class_device *);
  163. extern void class_device_del(struct class_device *);
  164. extern int class_device_rename(struct class_device *, char *);
  165. extern struct class_device * class_device_get(struct class_device *);
  166. extern void class_device_put(struct class_device *);
  167. struct class_device_attribute {
  168. struct attribute attr;
  169. ssize_t (*show)(struct class_device *, char * buf);
  170. ssize_t (*store)(struct class_device *, const char * buf, size_t count);
  171. };
  172. #define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
  173. struct class_device_attribute class_device_attr_##_name = \
  174. __ATTR(_name,_mode,_show,_store)
  175. extern int class_device_create_file(struct class_device *,
  176. const struct class_device_attribute *);
  177. extern void class_device_remove_file(struct class_device *,
  178. const struct class_device_attribute *);
  179. extern int class_device_create_bin_file(struct class_device *,
  180. struct bin_attribute *);
  181. extern void class_device_remove_bin_file(struct class_device *,
  182. struct bin_attribute *);
  183. struct class_interface {
  184. struct list_head node;
  185. struct class *class;
  186. int (*add) (struct class_device *);
  187. void (*remove) (struct class_device *);
  188. };
  189. extern int class_interface_register(struct class_interface *);
  190. extern void class_interface_unregister(struct class_interface *);
  191. extern struct class *class_create(struct module *owner, char *name);
  192. extern void class_destroy(struct class *cls);
  193. extern struct class_device *class_device_create(struct class *cls, dev_t devt,
  194. struct device *device, char *fmt, ...)
  195. __attribute__((format(printf,4,5)));
  196. extern void class_device_destroy(struct class *cls, dev_t devt);
  197. struct device {
  198. struct klist klist_children;
  199. struct klist_node knode_parent; /* node in sibling list */
  200. struct klist_node knode_driver;
  201. struct klist_node knode_bus;
  202. struct device * parent;
  203. struct kobject kobj;
  204. char bus_id[BUS_ID_SIZE]; /* position on parent bus */
  205. struct semaphore sem; /* semaphore to synchronize calls to
  206. * its driver.
  207. */
  208. struct bus_type * bus; /* type of bus device is on */
  209. struct device_driver *driver; /* which driver has allocated this
  210. device */
  211. void *driver_data; /* data private to the driver */
  212. void *platform_data; /* Platform specific data (e.g. ACPI,
  213. BIOS data relevant to device) */
  214. struct dev_pm_info power;
  215. u64 *dma_mask; /* dma mask (if dma'able device) */
  216. u64 coherent_dma_mask;/* Like dma_mask, but for
  217. alloc_coherent mappings as
  218. not all hardware supports
  219. 64 bit addresses for consistent
  220. allocations such descriptors. */
  221. struct list_head dma_pools; /* dma pools (if dma'ble) */
  222. struct dma_coherent_mem *dma_mem; /* internal for coherent mem
  223. override */
  224. void (*release)(struct device * dev);
  225. };
  226. static inline void *
  227. dev_get_drvdata (struct device *dev)
  228. {
  229. return dev->driver_data;
  230. }
  231. static inline void
  232. dev_set_drvdata (struct device *dev, void *data)
  233. {
  234. dev->driver_data = data;
  235. }
  236. /*
  237. * High level routines for use by the bus drivers
  238. */
  239. extern int device_register(struct device * dev);
  240. extern void device_unregister(struct device * dev);
  241. extern void device_initialize(struct device * dev);
  242. extern int device_add(struct device * dev);
  243. extern void device_del(struct device * dev);
  244. extern int device_for_each_child(struct device *, void *,
  245. int (*fn)(struct device *, void *));
  246. /*
  247. * Manual binding of a device to driver. See drivers/base/bus.c
  248. * for information on use.
  249. */
  250. extern void device_bind_driver(struct device * dev);
  251. extern void device_release_driver(struct device * dev);
  252. extern int device_attach(struct device * dev);
  253. extern void driver_attach(struct device_driver * drv);
  254. /* driverfs interface for exporting device attributes */
  255. struct device_attribute {
  256. struct attribute attr;
  257. ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  258. char *buf);
  259. ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  260. const char *buf, size_t count);
  261. };
  262. #define DEVICE_ATTR(_name,_mode,_show,_store) \
  263. struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
  264. extern int device_create_file(struct device *device, struct device_attribute * entry);
  265. extern void device_remove_file(struct device * dev, struct device_attribute * attr);
  266. /*
  267. * Platform "fixup" functions - allow the platform to have their say
  268. * about devices and actions that the general device layer doesn't
  269. * know about.
  270. */
  271. /* Notify platform of device discovery */
  272. extern int (*platform_notify)(struct device * dev);
  273. extern int (*platform_notify_remove)(struct device * dev);
  274. /**
  275. * get_device - atomically increment the reference count for the device.
  276. *
  277. */
  278. extern struct device * get_device(struct device * dev);
  279. extern void put_device(struct device * dev);
  280. /* drivers/base/platform.c */
  281. struct platform_device {
  282. const char * name;
  283. u32 id;
  284. struct device dev;
  285. u32 num_resources;
  286. struct resource * resource;
  287. };
  288. #define to_platform_device(x) container_of((x), struct platform_device, dev)
  289. extern int platform_device_register(struct platform_device *);
  290. extern void platform_device_unregister(struct platform_device *);
  291. extern struct bus_type platform_bus_type;
  292. extern struct device platform_bus;
  293. extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
  294. extern int platform_get_irq(struct platform_device *, unsigned int);
  295. extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, char *);
  296. extern int platform_get_irq_byname(struct platform_device *, char *);
  297. extern int platform_add_devices(struct platform_device **, int);
  298. extern struct platform_device *platform_device_register_simple(char *, unsigned int, struct resource *, unsigned int);
  299. /* drivers/base/power.c */
  300. extern void device_shutdown(void);
  301. /* drivers/base/firmware.c */
  302. extern int firmware_register(struct subsystem *);
  303. extern void firmware_unregister(struct subsystem *);
  304. /* debugging and troubleshooting/diagnostic helpers. */
  305. #define dev_printk(level, dev, format, arg...) \
  306. printk(level "%s %s: " format , (dev)->driver ? (dev)->driver->name : "" , (dev)->bus_id , ## arg)
  307. #ifdef DEBUG
  308. #define dev_dbg(dev, format, arg...) \
  309. dev_printk(KERN_DEBUG , dev , format , ## arg)
  310. #else
  311. #define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
  312. #endif
  313. #define dev_err(dev, format, arg...) \
  314. dev_printk(KERN_ERR , dev , format , ## arg)
  315. #define dev_info(dev, format, arg...) \
  316. dev_printk(KERN_INFO , dev , format , ## arg)
  317. #define dev_warn(dev, format, arg...) \
  318. dev_printk(KERN_WARNING , dev , format , ## arg)
  319. /* Create alias, so I can be autoloaded. */
  320. #define MODULE_ALIAS_CHARDEV(major,minor) \
  321. MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
  322. #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
  323. MODULE_ALIAS("char-major-" __stringify(major) "-*")
  324. #endif /* _DEVICE_H_ */