device.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * device.h - generic, centralized driver model
  3. *
  4. * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
  5. * Copyright (c) 2004-2007 Greg Kroah-Hartman <gregkh@suse.de>
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. * See Documentation/driver-model/ for more information.
  10. */
  11. #ifndef _DEVICE_H_
  12. #define _DEVICE_H_
  13. #include <linux/ioport.h>
  14. #include <linux/kobject.h>
  15. #include <linux/klist.h>
  16. #include <linux/list.h>
  17. #include <linux/compiler.h>
  18. #include <linux/types.h>
  19. #include <linux/module.h>
  20. #include <linux/pm.h>
  21. #include <linux/semaphore.h>
  22. #include <asm/atomic.h>
  23. #include <asm/device.h>
  24. #define DEVICE_NAME_SIZE 50
  25. /* DEVICE_NAME_HALF is really less than half to accommodate slop */
  26. #define DEVICE_NAME_HALF __stringify(20)
  27. #define DEVICE_ID_SIZE 32
  28. #define BUS_ID_SIZE KOBJ_NAME_LEN
  29. struct device;
  30. struct device_driver;
  31. struct driver_private;
  32. struct class;
  33. struct bus_type;
  34. struct bus_type_private;
  35. struct bus_attribute {
  36. struct attribute attr;
  37. ssize_t (*show)(struct bus_type *bus, char *buf);
  38. ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
  39. };
  40. #define BUS_ATTR(_name, _mode, _show, _store) \
  41. struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
  42. extern int __must_check bus_create_file(struct bus_type *,
  43. struct bus_attribute *);
  44. extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
  45. struct bus_type {
  46. const char *name;
  47. struct bus_attribute *bus_attrs;
  48. struct device_attribute *dev_attrs;
  49. struct driver_attribute *drv_attrs;
  50. int (*match)(struct device *dev, struct device_driver *drv);
  51. int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  52. int (*probe)(struct device *dev);
  53. int (*remove)(struct device *dev);
  54. void (*shutdown)(struct device *dev);
  55. int (*suspend)(struct device *dev, pm_message_t state);
  56. int (*suspend_late)(struct device *dev, pm_message_t state);
  57. int (*resume_early)(struct device *dev);
  58. int (*resume)(struct device *dev);
  59. struct bus_type_private *p;
  60. };
  61. extern int __must_check bus_register(struct bus_type *bus);
  62. extern void bus_unregister(struct bus_type *bus);
  63. extern int __must_check bus_rescan_devices(struct bus_type *bus);
  64. /* iterator helpers for buses */
  65. int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
  66. int (*fn)(struct device *dev, void *data));
  67. struct device *bus_find_device(struct bus_type *bus, struct device *start,
  68. void *data,
  69. int (*match)(struct device *dev, void *data));
  70. struct device *bus_find_device_by_name(struct bus_type *bus,
  71. struct device *start,
  72. const char *name);
  73. int __must_check bus_for_each_drv(struct bus_type *bus,
  74. struct device_driver *start, void *data,
  75. int (*fn)(struct device_driver *, void *));
  76. /*
  77. * Bus notifiers: Get notified of addition/removal of devices
  78. * and binding/unbinding of drivers to devices.
  79. * In the long run, it should be a replacement for the platform
  80. * notify hooks.
  81. */
  82. struct notifier_block;
  83. extern int bus_register_notifier(struct bus_type *bus,
  84. struct notifier_block *nb);
  85. extern int bus_unregister_notifier(struct bus_type *bus,
  86. struct notifier_block *nb);
  87. /* All 4 notifers below get called with the target struct device *
  88. * as an argument. Note that those functions are likely to be called
  89. * with the device semaphore held in the core, so be careful.
  90. */
  91. #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
  92. #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
  93. #define BUS_NOTIFY_BOUND_DRIVER 0x00000003 /* driver bound to device */
  94. #define BUS_NOTIFY_UNBIND_DRIVER 0x00000004 /* driver about to be
  95. unbound */
  96. extern struct kset *bus_get_kset(struct bus_type *bus);
  97. extern struct klist *bus_get_device_klist(struct bus_type *bus);
  98. struct device_driver {
  99. const char *name;
  100. struct bus_type *bus;
  101. struct module *owner;
  102. const char *mod_name; /* used for built-in modules */
  103. int (*probe) (struct device *dev);
  104. int (*remove) (struct device *dev);
  105. void (*shutdown) (struct device *dev);
  106. int (*suspend) (struct device *dev, pm_message_t state);
  107. int (*resume) (struct device *dev);
  108. struct attribute_group **groups;
  109. struct driver_private *p;
  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,
  116. struct bus_type *bus);
  117. extern int driver_probe_done(void);
  118. /* sysfs interface for exporting driver attributes */
  119. struct driver_attribute {
  120. struct attribute attr;
  121. ssize_t (*show)(struct device_driver *driver, char *buf);
  122. ssize_t (*store)(struct device_driver *driver, const char *buf,
  123. size_t count);
  124. };
  125. #define DRIVER_ATTR(_name, _mode, _show, _store) \
  126. struct driver_attribute driver_attr_##_name = \
  127. __ATTR(_name, _mode, _show, _store)
  128. extern int __must_check driver_create_file(struct device_driver *driver,
  129. struct driver_attribute *attr);
  130. extern void driver_remove_file(struct device_driver *driver,
  131. struct driver_attribute *attr);
  132. extern int __must_check driver_add_kobj(struct device_driver *drv,
  133. struct kobject *kobj,
  134. const char *fmt, ...);
  135. extern int __must_check driver_for_each_device(struct device_driver *drv,
  136. struct device *start,
  137. void *data,
  138. int (*fn)(struct device *dev,
  139. void *));
  140. struct device *driver_find_device(struct device_driver *drv,
  141. struct device *start, void *data,
  142. int (*match)(struct device *dev, void *data));
  143. /*
  144. * device classes
  145. */
  146. struct class {
  147. const char *name;
  148. struct module *owner;
  149. struct kset subsys;
  150. struct list_head children;
  151. struct list_head devices;
  152. struct list_head interfaces;
  153. struct kset class_dirs;
  154. struct semaphore sem; /* locks children, devices, interfaces */
  155. struct class_attribute *class_attrs;
  156. struct device_attribute *dev_attrs;
  157. int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
  158. void (*class_release)(struct class *class);
  159. void (*dev_release)(struct device *dev);
  160. int (*suspend)(struct device *dev, pm_message_t state);
  161. int (*resume)(struct device *dev);
  162. };
  163. extern int __must_check class_register(struct class *class);
  164. extern void class_unregister(struct class *class);
  165. extern int class_for_each_device(struct class *class, void *data,
  166. int (*fn)(struct device *dev, void *data));
  167. extern struct device *class_find_device(struct class *class, void *data,
  168. int (*match)(struct device *, void *));
  169. struct class_attribute {
  170. struct attribute attr;
  171. ssize_t (*show)(struct class *class, char *buf);
  172. ssize_t (*store)(struct class *class, const char *buf, size_t count);
  173. };
  174. #define CLASS_ATTR(_name, _mode, _show, _store) \
  175. struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
  176. extern int __must_check class_create_file(struct class *class,
  177. const struct class_attribute *attr);
  178. extern void class_remove_file(struct class *class,
  179. const struct class_attribute *attr);
  180. struct class_interface {
  181. struct list_head node;
  182. struct class *class;
  183. int (*add_dev) (struct device *, struct class_interface *);
  184. void (*remove_dev) (struct device *, struct class_interface *);
  185. };
  186. extern int __must_check class_interface_register(struct class_interface *);
  187. extern void class_interface_unregister(struct class_interface *);
  188. extern struct class *class_create(struct module *owner, const char *name);
  189. extern void class_destroy(struct class *cls);
  190. /*
  191. * The type of device, "struct device" is embedded in. A class
  192. * or bus can contain devices of different types
  193. * like "partitions" and "disks", "mouse" and "event".
  194. * This identifies the device type and carries type-specific
  195. * information, equivalent to the kobj_type of a kobject.
  196. * If "name" is specified, the uevent will contain it in
  197. * the DEVTYPE variable.
  198. */
  199. struct device_type {
  200. const char *name;
  201. struct attribute_group **groups;
  202. int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  203. void (*release)(struct device *dev);
  204. int (*suspend)(struct device *dev, pm_message_t state);
  205. int (*resume)(struct device *dev);
  206. };
  207. /* interface for exporting device attributes */
  208. struct device_attribute {
  209. struct attribute attr;
  210. ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  211. char *buf);
  212. ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  213. const char *buf, size_t count);
  214. };
  215. #define DEVICE_ATTR(_name, _mode, _show, _store) \
  216. struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
  217. extern int __must_check device_create_file(struct device *device,
  218. struct device_attribute *entry);
  219. extern void device_remove_file(struct device *dev,
  220. struct device_attribute *attr);
  221. extern int __must_check device_create_bin_file(struct device *dev,
  222. struct bin_attribute *attr);
  223. extern void device_remove_bin_file(struct device *dev,
  224. struct bin_attribute *attr);
  225. extern int device_schedule_callback_owner(struct device *dev,
  226. void (*func)(struct device *dev), struct module *owner);
  227. /* This is a macro to avoid include problems with THIS_MODULE */
  228. #define device_schedule_callback(dev, func) \
  229. device_schedule_callback_owner(dev, func, THIS_MODULE)
  230. /* device resource management */
  231. typedef void (*dr_release_t)(struct device *dev, void *res);
  232. typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
  233. #ifdef CONFIG_DEBUG_DEVRES
  234. extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
  235. const char *name);
  236. #define devres_alloc(release, size, gfp) \
  237. __devres_alloc(release, size, gfp, #release)
  238. #else
  239. extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
  240. #endif
  241. extern void devres_free(void *res);
  242. extern void devres_add(struct device *dev, void *res);
  243. extern void *devres_find(struct device *dev, dr_release_t release,
  244. dr_match_t match, void *match_data);
  245. extern void *devres_get(struct device *dev, void *new_res,
  246. dr_match_t match, void *match_data);
  247. extern void *devres_remove(struct device *dev, dr_release_t release,
  248. dr_match_t match, void *match_data);
  249. extern int devres_destroy(struct device *dev, dr_release_t release,
  250. dr_match_t match, void *match_data);
  251. /* devres group */
  252. extern void * __must_check devres_open_group(struct device *dev, void *id,
  253. gfp_t gfp);
  254. extern void devres_close_group(struct device *dev, void *id);
  255. extern void devres_remove_group(struct device *dev, void *id);
  256. extern int devres_release_group(struct device *dev, void *id);
  257. /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
  258. extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
  259. extern void devm_kfree(struct device *dev, void *p);
  260. struct device_dma_parameters {
  261. /*
  262. * a low level driver may set these to teach IOMMU code about
  263. * sg limitations.
  264. */
  265. unsigned int max_segment_size;
  266. unsigned long segment_boundary_mask;
  267. };
  268. struct device {
  269. struct klist klist_children;
  270. struct klist_node knode_parent; /* node in sibling list */
  271. struct klist_node knode_driver;
  272. struct klist_node knode_bus;
  273. struct device *parent;
  274. struct kobject kobj;
  275. char bus_id[BUS_ID_SIZE]; /* position on parent bus */
  276. struct device_type *type;
  277. unsigned uevent_suppress:1;
  278. struct semaphore sem; /* semaphore to synchronize calls to
  279. * its driver.
  280. */
  281. struct bus_type *bus; /* type of bus device is on */
  282. struct device_driver *driver; /* which driver has allocated this
  283. device */
  284. void *driver_data; /* data private to the driver */
  285. void *platform_data; /* Platform specific data, device
  286. core doesn't touch it */
  287. struct dev_pm_info power;
  288. #ifdef CONFIG_NUMA
  289. int numa_node; /* NUMA node this device is close to */
  290. #endif
  291. u64 *dma_mask; /* dma mask (if dma'able device) */
  292. u64 coherent_dma_mask;/* Like dma_mask, but for
  293. alloc_coherent mappings as
  294. not all hardware supports
  295. 64 bit addresses for consistent
  296. allocations such descriptors. */
  297. struct device_dma_parameters *dma_parms;
  298. struct list_head dma_pools; /* dma pools (if dma'ble) */
  299. struct dma_coherent_mem *dma_mem; /* internal for coherent mem
  300. override */
  301. /* arch specific additions */
  302. struct dev_archdata archdata;
  303. spinlock_t devres_lock;
  304. struct list_head devres_head;
  305. struct list_head node;
  306. struct class *class;
  307. dev_t devt; /* dev_t, creates the sysfs "dev" */
  308. struct attribute_group **groups; /* optional groups */
  309. void (*release)(struct device *dev);
  310. };
  311. /* Get the wakeup routines, which depend on struct device */
  312. #include <linux/pm_wakeup.h>
  313. #ifdef CONFIG_NUMA
  314. static inline int dev_to_node(struct device *dev)
  315. {
  316. return dev->numa_node;
  317. }
  318. static inline void set_dev_node(struct device *dev, int node)
  319. {
  320. dev->numa_node = node;
  321. }
  322. #else
  323. static inline int dev_to_node(struct device *dev)
  324. {
  325. return -1;
  326. }
  327. static inline void set_dev_node(struct device *dev, int node)
  328. {
  329. }
  330. #endif
  331. static inline void *dev_get_drvdata(struct device *dev)
  332. {
  333. return dev->driver_data;
  334. }
  335. static inline void dev_set_drvdata(struct device *dev, void *data)
  336. {
  337. dev->driver_data = data;
  338. }
  339. static inline int device_is_registered(struct device *dev)
  340. {
  341. return dev->kobj.state_in_sysfs;
  342. }
  343. void driver_init(void);
  344. /*
  345. * High level routines for use by the bus drivers
  346. */
  347. extern int __must_check device_register(struct device *dev);
  348. extern void device_unregister(struct device *dev);
  349. extern void device_initialize(struct device *dev);
  350. extern int __must_check device_add(struct device *dev);
  351. extern void device_del(struct device *dev);
  352. extern int device_for_each_child(struct device *dev, void *data,
  353. int (*fn)(struct device *dev, void *data));
  354. extern struct device *device_find_child(struct device *dev, void *data,
  355. int (*match)(struct device *dev, void *data));
  356. extern int device_rename(struct device *dev, char *new_name);
  357. extern int device_move(struct device *dev, struct device *new_parent);
  358. /*
  359. * Manual binding of a device to driver. See drivers/base/bus.c
  360. * for information on use.
  361. */
  362. extern int __must_check device_bind_driver(struct device *dev);
  363. extern void device_release_driver(struct device *dev);
  364. extern int __must_check device_attach(struct device *dev);
  365. extern int __must_check driver_attach(struct device_driver *drv);
  366. extern int __must_check device_reprobe(struct device *dev);
  367. /*
  368. * Easy functions for dynamically creating devices on the fly
  369. */
  370. extern struct device *device_create(struct class *cls, struct device *parent,
  371. dev_t devt, const char *fmt, ...)
  372. __attribute__((format(printf, 4, 5)));
  373. extern void device_destroy(struct class *cls, dev_t devt);
  374. /*
  375. * Platform "fixup" functions - allow the platform to have their say
  376. * about devices and actions that the general device layer doesn't
  377. * know about.
  378. */
  379. /* Notify platform of device discovery */
  380. extern int (*platform_notify)(struct device *dev);
  381. extern int (*platform_notify_remove)(struct device *dev);
  382. /**
  383. * get_device - atomically increment the reference count for the device.
  384. *
  385. */
  386. extern struct device *get_device(struct device *dev);
  387. extern void put_device(struct device *dev);
  388. /* drivers/base/power/shutdown.c */
  389. extern void device_shutdown(void);
  390. /* drivers/base/sys.c */
  391. extern void sysdev_shutdown(void);
  392. /* debugging and troubleshooting/diagnostic helpers. */
  393. extern const char *dev_driver_string(struct device *dev);
  394. #define dev_printk(level, dev, format, arg...) \
  395. printk(level "%s %s: " format , dev_driver_string(dev) , \
  396. (dev)->bus_id , ## arg)
  397. #define dev_emerg(dev, format, arg...) \
  398. dev_printk(KERN_EMERG , dev , format , ## arg)
  399. #define dev_alert(dev, format, arg...) \
  400. dev_printk(KERN_ALERT , dev , format , ## arg)
  401. #define dev_crit(dev, format, arg...) \
  402. dev_printk(KERN_CRIT , dev , format , ## arg)
  403. #define dev_err(dev, format, arg...) \
  404. dev_printk(KERN_ERR , dev , format , ## arg)
  405. #define dev_warn(dev, format, arg...) \
  406. dev_printk(KERN_WARNING , dev , format , ## arg)
  407. #define dev_notice(dev, format, arg...) \
  408. dev_printk(KERN_NOTICE , dev , format , ## arg)
  409. #define dev_info(dev, format, arg...) \
  410. dev_printk(KERN_INFO , dev , format , ## arg)
  411. #ifdef DEBUG
  412. #define dev_dbg(dev, format, arg...) \
  413. dev_printk(KERN_DEBUG , dev , format , ## arg)
  414. #else
  415. #define dev_dbg(dev, format, arg...) \
  416. ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
  417. #endif
  418. #ifdef VERBOSE_DEBUG
  419. #define dev_vdbg dev_dbg
  420. #else
  421. #define dev_vdbg(dev, format, arg...) \
  422. ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
  423. #endif
  424. /* Create alias, so I can be autoloaded. */
  425. #define MODULE_ALIAS_CHARDEV(major,minor) \
  426. MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
  427. #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
  428. MODULE_ALIAS("char-major-" __stringify(major) "-*")
  429. #endif /* _DEVICE_H_ */