device.h 17 KB

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