device.h 18 KB

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