device.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 DEVICE_ID_SIZE 32
  26. #define BUS_ID_SIZE 20
  27. struct device;
  28. struct device_driver;
  29. struct driver_private;
  30. struct class;
  31. struct class_private;
  32. struct bus_type;
  33. struct bus_type_private;
  34. struct bus_attribute {
  35. struct attribute attr;
  36. ssize_t (*show)(struct bus_type *bus, char *buf);
  37. ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
  38. };
  39. #define BUS_ATTR(_name, _mode, _show, _store) \
  40. struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
  41. extern int __must_check bus_create_file(struct bus_type *,
  42. struct bus_attribute *);
  43. extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
  44. struct bus_type {
  45. const char *name;
  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 (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  51. int (*probe)(struct device *dev);
  52. int (*remove)(struct device *dev);
  53. void (*shutdown)(struct device *dev);
  54. int (*suspend)(struct device *dev, pm_message_t state);
  55. int (*suspend_late)(struct device *dev, pm_message_t state);
  56. int (*resume_early)(struct device *dev);
  57. int (*resume)(struct device *dev);
  58. struct pm_ext_ops *pm;
  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 pm_ops *pm;
  110. struct driver_private *p;
  111. };
  112. extern int __must_check driver_register(struct device_driver *drv);
  113. extern void driver_unregister(struct device_driver *drv);
  114. extern struct device_driver *get_driver(struct device_driver *drv);
  115. extern void put_driver(struct device_driver *drv);
  116. extern struct device_driver *driver_find(const char *name,
  117. struct bus_type *bus);
  118. extern int driver_probe_done(void);
  119. /* sysfs interface for exporting driver attributes */
  120. struct driver_attribute {
  121. struct attribute attr;
  122. ssize_t (*show)(struct device_driver *driver, char *buf);
  123. ssize_t (*store)(struct device_driver *driver, const char *buf,
  124. size_t count);
  125. };
  126. #define DRIVER_ATTR(_name, _mode, _show, _store) \
  127. struct driver_attribute driver_attr_##_name = \
  128. __ATTR(_name, _mode, _show, _store)
  129. extern int __must_check driver_create_file(struct device_driver *driver,
  130. struct driver_attribute *attr);
  131. extern void driver_remove_file(struct device_driver *driver,
  132. struct driver_attribute *attr);
  133. extern int __must_check driver_add_kobj(struct device_driver *drv,
  134. struct kobject *kobj,
  135. const char *fmt, ...);
  136. extern int __must_check driver_for_each_device(struct device_driver *drv,
  137. struct device *start,
  138. void *data,
  139. int (*fn)(struct device *dev,
  140. void *));
  141. struct device *driver_find_device(struct device_driver *drv,
  142. struct device *start, void *data,
  143. int (*match)(struct device *dev, void *data));
  144. /*
  145. * device classes
  146. */
  147. struct class {
  148. const char *name;
  149. struct module *owner;
  150. struct class_attribute *class_attrs;
  151. struct device_attribute *dev_attrs;
  152. struct kobject *dev_kobj;
  153. int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
  154. void (*class_release)(struct class *class);
  155. void (*dev_release)(struct device *dev);
  156. int (*suspend)(struct device *dev, pm_message_t state);
  157. int (*resume)(struct device *dev);
  158. struct pm_ops *pm;
  159. struct class_private *p;
  160. };
  161. extern struct kobject *sysfs_dev_block_kobj;
  162. extern struct kobject *sysfs_dev_char_kobj;
  163. extern int __must_check __class_register(struct class *class,
  164. struct lock_class_key *key);
  165. extern void class_unregister(struct class *class);
  166. /* This is a #define to keep the compiler from merging different
  167. * instances of the __key variable */
  168. #define class_register(class) \
  169. ({ \
  170. static struct lock_class_key __key; \
  171. __class_register(class, &__key); \
  172. })
  173. extern int class_for_each_device(struct class *class, struct device *start,
  174. void *data,
  175. int (*fn)(struct device *dev, void *data));
  176. extern struct device *class_find_device(struct class *class,
  177. struct device *start, void *data,
  178. int (*match)(struct device *, void *));
  179. struct class_attribute {
  180. struct attribute attr;
  181. ssize_t (*show)(struct class *class, char *buf);
  182. ssize_t (*store)(struct class *class, const char *buf, size_t count);
  183. };
  184. #define CLASS_ATTR(_name, _mode, _show, _store) \
  185. struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
  186. extern int __must_check class_create_file(struct class *class,
  187. const struct class_attribute *attr);
  188. extern void class_remove_file(struct class *class,
  189. const struct class_attribute *attr);
  190. struct class_interface {
  191. struct list_head node;
  192. struct class *class;
  193. int (*add_dev) (struct device *, struct class_interface *);
  194. void (*remove_dev) (struct device *, struct class_interface *);
  195. };
  196. extern int __must_check class_interface_register(struct class_interface *);
  197. extern void class_interface_unregister(struct class_interface *);
  198. extern struct class * __must_check __class_create(struct module *owner,
  199. const char *name,
  200. struct lock_class_key *key);
  201. extern void class_destroy(struct class *cls);
  202. /* This is a #define to keep the compiler from merging different
  203. * instances of the __key variable */
  204. #define class_create(owner, name) \
  205. ({ \
  206. static struct lock_class_key __key; \
  207. __class_create(owner, name, &__key); \
  208. })
  209. /*
  210. * The type of device, "struct device" is embedded in. A class
  211. * or bus can contain devices of different types
  212. * like "partitions" and "disks", "mouse" and "event".
  213. * This identifies the device type and carries type-specific
  214. * information, equivalent to the kobj_type of a kobject.
  215. * If "name" is specified, the uevent will contain it in
  216. * the DEVTYPE variable.
  217. */
  218. struct device_type {
  219. const char *name;
  220. struct attribute_group **groups;
  221. int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  222. void (*release)(struct device *dev);
  223. int (*suspend)(struct device *dev, pm_message_t state);
  224. int (*resume)(struct device *dev);
  225. struct pm_ops *pm;
  226. };
  227. /* interface for exporting device attributes */
  228. struct device_attribute {
  229. struct attribute attr;
  230. ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  231. char *buf);
  232. ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  233. const char *buf, size_t count);
  234. };
  235. #define DEVICE_ATTR(_name, _mode, _show, _store) \
  236. struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
  237. extern int __must_check device_create_file(struct device *device,
  238. struct device_attribute *entry);
  239. extern void device_remove_file(struct device *dev,
  240. struct device_attribute *attr);
  241. extern int __must_check device_create_bin_file(struct device *dev,
  242. struct bin_attribute *attr);
  243. extern void device_remove_bin_file(struct device *dev,
  244. struct bin_attribute *attr);
  245. extern int device_schedule_callback_owner(struct device *dev,
  246. void (*func)(struct device *dev), struct module *owner);
  247. /* This is a macro to avoid include problems with THIS_MODULE */
  248. #define device_schedule_callback(dev, func) \
  249. device_schedule_callback_owner(dev, func, THIS_MODULE)
  250. /* device resource management */
  251. typedef void (*dr_release_t)(struct device *dev, void *res);
  252. typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
  253. #ifdef CONFIG_DEBUG_DEVRES
  254. extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
  255. const char *name);
  256. #define devres_alloc(release, size, gfp) \
  257. __devres_alloc(release, size, gfp, #release)
  258. #else
  259. extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
  260. #endif
  261. extern void devres_free(void *res);
  262. extern void devres_add(struct device *dev, void *res);
  263. extern void *devres_find(struct device *dev, dr_release_t release,
  264. dr_match_t match, void *match_data);
  265. extern void *devres_get(struct device *dev, void *new_res,
  266. dr_match_t match, void *match_data);
  267. extern void *devres_remove(struct device *dev, dr_release_t release,
  268. dr_match_t match, void *match_data);
  269. extern int devres_destroy(struct device *dev, dr_release_t release,
  270. dr_match_t match, void *match_data);
  271. /* devres group */
  272. extern void * __must_check devres_open_group(struct device *dev, void *id,
  273. gfp_t gfp);
  274. extern void devres_close_group(struct device *dev, void *id);
  275. extern void devres_remove_group(struct device *dev, void *id);
  276. extern int devres_release_group(struct device *dev, void *id);
  277. /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
  278. extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
  279. extern void devm_kfree(struct device *dev, void *p);
  280. struct device_dma_parameters {
  281. /*
  282. * a low level driver may set these to teach IOMMU code about
  283. * sg limitations.
  284. */
  285. unsigned int max_segment_size;
  286. unsigned long segment_boundary_mask;
  287. };
  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 uevent_suppress:1;
  298. struct semaphore sem; /* semaphore to synchronize calls to
  299. * its driver.
  300. */
  301. struct bus_type *bus; /* type of bus device is on */
  302. struct device_driver *driver; /* which driver has allocated this
  303. device */
  304. void *driver_data; /* data private to the driver */
  305. void *platform_data; /* Platform specific data, device
  306. core doesn't touch it */
  307. struct dev_pm_info power;
  308. #ifdef CONFIG_NUMA
  309. int numa_node; /* NUMA node this device is close to */
  310. #endif
  311. u64 *dma_mask; /* dma mask (if dma'able device) */
  312. u64 coherent_dma_mask;/* Like dma_mask, but for
  313. alloc_coherent mappings as
  314. not all hardware supports
  315. 64 bit addresses for consistent
  316. allocations such descriptors. */
  317. struct device_dma_parameters *dma_parms;
  318. struct list_head dma_pools; /* dma pools (if dma'ble) */
  319. struct dma_coherent_mem *dma_mem; /* internal for coherent mem
  320. override */
  321. /* arch specific additions */
  322. struct dev_archdata archdata;
  323. spinlock_t devres_lock;
  324. struct list_head devres_head;
  325. struct list_head node;
  326. struct class *class;
  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. /* Get the wakeup routines, which depend on struct device */
  332. #include <linux/pm_wakeup.h>
  333. static inline const char *dev_name(struct device *dev)
  334. {
  335. /* will be changed into kobject_name(&dev->kobj) in the near future */
  336. return dev->bus_id;
  337. }
  338. extern int dev_set_name(struct device *dev, const char *name, ...)
  339. __attribute__((format(printf, 2, 3)));
  340. #ifdef CONFIG_NUMA
  341. static inline int dev_to_node(struct device *dev)
  342. {
  343. return dev->numa_node;
  344. }
  345. static inline void set_dev_node(struct device *dev, int node)
  346. {
  347. dev->numa_node = node;
  348. }
  349. #else
  350. static inline int dev_to_node(struct device *dev)
  351. {
  352. return -1;
  353. }
  354. static inline void set_dev_node(struct device *dev, int node)
  355. {
  356. }
  357. #endif
  358. static inline void *dev_get_drvdata(struct device *dev)
  359. {
  360. return dev->driver_data;
  361. }
  362. static inline void dev_set_drvdata(struct device *dev, void *data)
  363. {
  364. dev->driver_data = data;
  365. }
  366. static inline int device_is_registered(struct device *dev)
  367. {
  368. return dev->kobj.state_in_sysfs;
  369. }
  370. void driver_init(void);
  371. /*
  372. * High level routines for use by the bus drivers
  373. */
  374. extern int __must_check device_register(struct device *dev);
  375. extern void device_unregister(struct device *dev);
  376. extern void device_initialize(struct device *dev);
  377. extern int __must_check device_add(struct device *dev);
  378. extern void device_del(struct device *dev);
  379. extern int device_for_each_child(struct device *dev, void *data,
  380. int (*fn)(struct device *dev, void *data));
  381. extern struct device *device_find_child(struct device *dev, void *data,
  382. int (*match)(struct device *dev, void *data));
  383. extern int device_rename(struct device *dev, char *new_name);
  384. extern int device_move(struct device *dev, struct device *new_parent);
  385. /*
  386. * Manual binding of a device to driver. See drivers/base/bus.c
  387. * for information on use.
  388. */
  389. extern int __must_check device_bind_driver(struct device *dev);
  390. extern void device_release_driver(struct device *dev);
  391. extern int __must_check device_attach(struct device *dev);
  392. extern int __must_check driver_attach(struct device_driver *drv);
  393. extern int __must_check device_reprobe(struct device *dev);
  394. /*
  395. * Easy functions for dynamically creating devices on the fly
  396. */
  397. extern struct device *device_create_vargs(struct class *cls,
  398. struct device *parent,
  399. dev_t devt,
  400. void *drvdata,
  401. const char *fmt,
  402. va_list vargs);
  403. extern struct device *device_create(struct class *cls, struct device *parent,
  404. dev_t devt, void *drvdata,
  405. const char *fmt, ...)
  406. __attribute__((format(printf, 5, 6)));
  407. #define device_create_drvdata device_create
  408. extern void device_destroy(struct class *cls, dev_t devt);
  409. /*
  410. * Platform "fixup" functions - allow the platform to have their say
  411. * about devices and actions that the general device layer doesn't
  412. * know about.
  413. */
  414. /* Notify platform of device discovery */
  415. extern int (*platform_notify)(struct device *dev);
  416. extern int (*platform_notify_remove)(struct device *dev);
  417. /**
  418. * get_device - atomically increment the reference count for the device.
  419. *
  420. */
  421. extern struct device *get_device(struct device *dev);
  422. extern void put_device(struct device *dev);
  423. /* drivers/base/power/shutdown.c */
  424. extern void device_shutdown(void);
  425. /* drivers/base/sys.c */
  426. extern void sysdev_shutdown(void);
  427. /* debugging and troubleshooting/diagnostic helpers. */
  428. extern const char *dev_driver_string(struct device *dev);
  429. #define dev_printk(level, dev, format, arg...) \
  430. printk(level "%s %s: " format , dev_driver_string(dev) , \
  431. dev_name(dev) , ## arg)
  432. #define dev_emerg(dev, format, arg...) \
  433. dev_printk(KERN_EMERG , dev , format , ## arg)
  434. #define dev_alert(dev, format, arg...) \
  435. dev_printk(KERN_ALERT , dev , format , ## arg)
  436. #define dev_crit(dev, format, arg...) \
  437. dev_printk(KERN_CRIT , dev , format , ## arg)
  438. #define dev_err(dev, format, arg...) \
  439. dev_printk(KERN_ERR , dev , format , ## arg)
  440. #define dev_warn(dev, format, arg...) \
  441. dev_printk(KERN_WARNING , dev , format , ## arg)
  442. #define dev_notice(dev, format, arg...) \
  443. dev_printk(KERN_NOTICE , dev , format , ## arg)
  444. #define dev_info(dev, format, arg...) \
  445. dev_printk(KERN_INFO , dev , format , ## arg)
  446. #ifdef DEBUG
  447. #define dev_dbg(dev, format, arg...) \
  448. dev_printk(KERN_DEBUG , dev , format , ## arg)
  449. #else
  450. #define dev_dbg(dev, format, arg...) \
  451. ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
  452. #endif
  453. #ifdef VERBOSE_DEBUG
  454. #define dev_vdbg dev_dbg
  455. #else
  456. #define dev_vdbg(dev, format, arg...) \
  457. ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
  458. #endif
  459. /* Create alias, so I can be autoloaded. */
  460. #define MODULE_ALIAS_CHARDEV(major,minor) \
  461. MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
  462. #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
  463. MODULE_ALIAS("char-major-" __stringify(major) "-*")
  464. #endif /* _DEVICE_H_ */