device.h 13 KB

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