device.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 dev_pm_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. void bus_sort_breadthfirst(struct bus_type *bus,
  76. int (*compare)(const struct device *a,
  77. const struct device *b));
  78. /*
  79. * Bus notifiers: Get notified of addition/removal of devices
  80. * and binding/unbinding of drivers to devices.
  81. * In the long run, it should be a replacement for the platform
  82. * notify hooks.
  83. */
  84. struct notifier_block;
  85. extern int bus_register_notifier(struct bus_type *bus,
  86. struct notifier_block *nb);
  87. extern int bus_unregister_notifier(struct bus_type *bus,
  88. struct notifier_block *nb);
  89. /* All 4 notifers below get called with the target struct device *
  90. * as an argument. Note that those functions are likely to be called
  91. * with the device semaphore held in the core, so be careful.
  92. */
  93. #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
  94. #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
  95. #define BUS_NOTIFY_BOUND_DRIVER 0x00000003 /* driver bound to device */
  96. #define BUS_NOTIFY_UNBIND_DRIVER 0x00000004 /* driver about to be
  97. unbound */
  98. extern struct kset *bus_get_kset(struct bus_type *bus);
  99. extern struct klist *bus_get_device_klist(struct bus_type *bus);
  100. struct device_driver {
  101. const char *name;
  102. struct bus_type *bus;
  103. struct module *owner;
  104. const char *mod_name; /* used for built-in modules */
  105. int (*probe) (struct device *dev);
  106. int (*remove) (struct device *dev);
  107. void (*shutdown) (struct device *dev);
  108. int (*suspend) (struct device *dev, pm_message_t state);
  109. int (*resume) (struct device *dev);
  110. struct attribute_group **groups;
  111. struct dev_pm_ops *pm;
  112. struct driver_private *p;
  113. };
  114. extern int __must_check driver_register(struct device_driver *drv);
  115. extern void driver_unregister(struct device_driver *drv);
  116. extern struct device_driver *get_driver(struct device_driver *drv);
  117. extern void put_driver(struct device_driver *drv);
  118. extern struct device_driver *driver_find(const char *name,
  119. struct bus_type *bus);
  120. extern int driver_probe_done(void);
  121. extern int wait_for_device_probe(void);
  122. /* sysfs interface for exporting driver attributes */
  123. struct driver_attribute {
  124. struct attribute attr;
  125. ssize_t (*show)(struct device_driver *driver, char *buf);
  126. ssize_t (*store)(struct device_driver *driver, const char *buf,
  127. size_t count);
  128. };
  129. #define DRIVER_ATTR(_name, _mode, _show, _store) \
  130. struct driver_attribute driver_attr_##_name = \
  131. __ATTR(_name, _mode, _show, _store)
  132. extern int __must_check driver_create_file(struct device_driver *driver,
  133. struct driver_attribute *attr);
  134. extern void driver_remove_file(struct device_driver *driver,
  135. struct driver_attribute *attr);
  136. extern int __must_check driver_add_kobj(struct device_driver *drv,
  137. struct kobject *kobj,
  138. const char *fmt, ...);
  139. extern int __must_check driver_for_each_device(struct device_driver *drv,
  140. struct device *start,
  141. void *data,
  142. int (*fn)(struct device *dev,
  143. void *));
  144. struct device *driver_find_device(struct device_driver *drv,
  145. struct device *start, void *data,
  146. int (*match)(struct device *dev, void *data));
  147. /*
  148. * device classes
  149. */
  150. struct class {
  151. const char *name;
  152. struct module *owner;
  153. struct class_attribute *class_attrs;
  154. struct device_attribute *dev_attrs;
  155. struct kobject *dev_kobj;
  156. int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
  157. void (*class_release)(struct class *class);
  158. void (*dev_release)(struct device *dev);
  159. int (*suspend)(struct device *dev, pm_message_t state);
  160. int (*resume)(struct device *dev);
  161. struct dev_pm_ops *pm;
  162. struct class_private *p;
  163. };
  164. struct class_dev_iter {
  165. struct klist_iter ki;
  166. const struct device_type *type;
  167. };
  168. extern struct kobject *sysfs_dev_block_kobj;
  169. extern struct kobject *sysfs_dev_char_kobj;
  170. extern int __must_check __class_register(struct class *class,
  171. struct lock_class_key *key);
  172. extern void class_unregister(struct class *class);
  173. /* This is a #define to keep the compiler from merging different
  174. * instances of the __key variable */
  175. #define class_register(class) \
  176. ({ \
  177. static struct lock_class_key __key; \
  178. __class_register(class, &__key); \
  179. })
  180. extern void class_dev_iter_init(struct class_dev_iter *iter,
  181. struct class *class,
  182. struct device *start,
  183. const struct device_type *type);
  184. extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
  185. extern void class_dev_iter_exit(struct class_dev_iter *iter);
  186. extern int class_for_each_device(struct class *class, struct device *start,
  187. void *data,
  188. int (*fn)(struct device *dev, void *data));
  189. extern struct device *class_find_device(struct class *class,
  190. struct device *start, void *data,
  191. int (*match)(struct device *, void *));
  192. struct class_attribute {
  193. struct attribute attr;
  194. ssize_t (*show)(struct class *class, char *buf);
  195. ssize_t (*store)(struct class *class, const char *buf, size_t count);
  196. };
  197. #define CLASS_ATTR(_name, _mode, _show, _store) \
  198. struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
  199. extern int __must_check class_create_file(struct class *class,
  200. const struct class_attribute *attr);
  201. extern void class_remove_file(struct class *class,
  202. const struct class_attribute *attr);
  203. struct class_interface {
  204. struct list_head node;
  205. struct class *class;
  206. int (*add_dev) (struct device *, struct class_interface *);
  207. void (*remove_dev) (struct device *, struct class_interface *);
  208. };
  209. extern int __must_check class_interface_register(struct class_interface *);
  210. extern void class_interface_unregister(struct class_interface *);
  211. extern struct class * __must_check __class_create(struct module *owner,
  212. const char *name,
  213. struct lock_class_key *key);
  214. extern void class_destroy(struct class *cls);
  215. /* This is a #define to keep the compiler from merging different
  216. * instances of the __key variable */
  217. #define class_create(owner, name) \
  218. ({ \
  219. static struct lock_class_key __key; \
  220. __class_create(owner, name, &__key); \
  221. })
  222. /*
  223. * The type of device, "struct device" is embedded in. A class
  224. * or bus can contain devices of different types
  225. * like "partitions" and "disks", "mouse" and "event".
  226. * This identifies the device type and carries type-specific
  227. * information, equivalent to the kobj_type of a kobject.
  228. * If "name" is specified, the uevent will contain it in
  229. * the DEVTYPE variable.
  230. */
  231. struct device_type {
  232. const char *name;
  233. struct attribute_group **groups;
  234. int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  235. void (*release)(struct device *dev);
  236. int (*suspend)(struct device *dev, pm_message_t state);
  237. int (*resume)(struct device *dev);
  238. struct dev_pm_ops *pm;
  239. };
  240. /* interface for exporting device attributes */
  241. struct device_attribute {
  242. struct attribute attr;
  243. ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  244. char *buf);
  245. ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  246. const char *buf, size_t count);
  247. };
  248. #define DEVICE_ATTR(_name, _mode, _show, _store) \
  249. struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
  250. extern int __must_check device_create_file(struct device *device,
  251. struct device_attribute *entry);
  252. extern void device_remove_file(struct device *dev,
  253. struct device_attribute *attr);
  254. extern int __must_check device_create_bin_file(struct device *dev,
  255. struct bin_attribute *attr);
  256. extern void device_remove_bin_file(struct device *dev,
  257. struct bin_attribute *attr);
  258. extern int device_schedule_callback_owner(struct device *dev,
  259. void (*func)(struct device *dev), struct module *owner);
  260. /* This is a macro to avoid include problems with THIS_MODULE */
  261. #define device_schedule_callback(dev, func) \
  262. device_schedule_callback_owner(dev, func, THIS_MODULE)
  263. /* device resource management */
  264. typedef void (*dr_release_t)(struct device *dev, void *res);
  265. typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
  266. #ifdef CONFIG_DEBUG_DEVRES
  267. extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
  268. const char *name);
  269. #define devres_alloc(release, size, gfp) \
  270. __devres_alloc(release, size, gfp, #release)
  271. #else
  272. extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
  273. #endif
  274. extern void devres_free(void *res);
  275. extern void devres_add(struct device *dev, void *res);
  276. extern void *devres_find(struct device *dev, dr_release_t release,
  277. dr_match_t match, void *match_data);
  278. extern void *devres_get(struct device *dev, void *new_res,
  279. dr_match_t match, void *match_data);
  280. extern void *devres_remove(struct device *dev, dr_release_t release,
  281. dr_match_t match, void *match_data);
  282. extern int devres_destroy(struct device *dev, dr_release_t release,
  283. dr_match_t match, void *match_data);
  284. /* devres group */
  285. extern void * __must_check devres_open_group(struct device *dev, void *id,
  286. gfp_t gfp);
  287. extern void devres_close_group(struct device *dev, void *id);
  288. extern void devres_remove_group(struct device *dev, void *id);
  289. extern int devres_release_group(struct device *dev, void *id);
  290. /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
  291. extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
  292. extern void devm_kfree(struct device *dev, void *p);
  293. struct device_dma_parameters {
  294. /*
  295. * a low level driver may set these to teach IOMMU code about
  296. * sg limitations.
  297. */
  298. unsigned int max_segment_size;
  299. unsigned long segment_boundary_mask;
  300. };
  301. struct device {
  302. struct klist klist_children;
  303. struct klist_node knode_parent; /* node in sibling list */
  304. struct klist_node knode_driver;
  305. struct klist_node knode_bus;
  306. struct device *parent;
  307. struct kobject kobj;
  308. char bus_id[BUS_ID_SIZE]; /* position on parent bus */
  309. unsigned uevent_suppress:1;
  310. const char *init_name; /* initial name of the device */
  311. struct device_type *type;
  312. struct semaphore sem; /* semaphore to synchronize calls to
  313. * its driver.
  314. */
  315. struct bus_type *bus; /* type of bus device is on */
  316. struct device_driver *driver; /* which driver has allocated this
  317. device */
  318. void *driver_data; /* data private to the driver */
  319. void *platform_data; /* Platform specific data, device
  320. core doesn't touch it */
  321. struct dev_pm_info power;
  322. #ifdef CONFIG_NUMA
  323. int numa_node; /* NUMA node this device is close to */
  324. #endif
  325. u64 *dma_mask; /* dma mask (if dma'able device) */
  326. u64 coherent_dma_mask;/* Like dma_mask, but for
  327. alloc_coherent mappings as
  328. not all hardware supports
  329. 64 bit addresses for consistent
  330. allocations such descriptors. */
  331. struct device_dma_parameters *dma_parms;
  332. struct list_head dma_pools; /* dma pools (if dma'ble) */
  333. struct dma_coherent_mem *dma_mem; /* internal for coherent mem
  334. override */
  335. /* arch specific additions */
  336. struct dev_archdata archdata;
  337. dev_t devt; /* dev_t, creates the sysfs "dev" */
  338. spinlock_t devres_lock;
  339. struct list_head devres_head;
  340. struct klist_node knode_class;
  341. struct class *class;
  342. struct attribute_group **groups; /* optional groups */
  343. void (*release)(struct device *dev);
  344. };
  345. /* Get the wakeup routines, which depend on struct device */
  346. #include <linux/pm_wakeup.h>
  347. static inline const char *dev_name(const struct device *dev)
  348. {
  349. /* will be changed into kobject_name(&dev->kobj) in the near future */
  350. return dev->bus_id;
  351. }
  352. extern int dev_set_name(struct device *dev, const char *name, ...)
  353. __attribute__((format(printf, 2, 3)));
  354. #ifdef CONFIG_NUMA
  355. static inline int dev_to_node(struct device *dev)
  356. {
  357. return dev->numa_node;
  358. }
  359. static inline void set_dev_node(struct device *dev, int node)
  360. {
  361. dev->numa_node = node;
  362. }
  363. #else
  364. static inline int dev_to_node(struct device *dev)
  365. {
  366. return -1;
  367. }
  368. static inline void set_dev_node(struct device *dev, int node)
  369. {
  370. }
  371. #endif
  372. static inline void *dev_get_drvdata(const struct device *dev)
  373. {
  374. return dev->driver_data;
  375. }
  376. static inline void dev_set_drvdata(struct device *dev, void *data)
  377. {
  378. dev->driver_data = data;
  379. }
  380. static inline int device_is_registered(struct device *dev)
  381. {
  382. return dev->kobj.state_in_sysfs;
  383. }
  384. void driver_init(void);
  385. /*
  386. * High level routines for use by the bus drivers
  387. */
  388. extern int __must_check device_register(struct device *dev);
  389. extern void device_unregister(struct device *dev);
  390. extern void device_initialize(struct device *dev);
  391. extern int __must_check device_add(struct device *dev);
  392. extern void device_del(struct device *dev);
  393. extern int device_for_each_child(struct device *dev, void *data,
  394. int (*fn)(struct device *dev, void *data));
  395. extern struct device *device_find_child(struct device *dev, void *data,
  396. int (*match)(struct device *dev, void *data));
  397. extern int device_rename(struct device *dev, char *new_name);
  398. extern int device_move(struct device *dev, struct device *new_parent);
  399. /*
  400. * Root device objects for grouping under /sys/devices
  401. */
  402. extern struct device *__root_device_register(const char *name,
  403. struct module *owner);
  404. static inline struct device *root_device_register(const char *name)
  405. {
  406. return __root_device_register(name, THIS_MODULE);
  407. }
  408. extern void root_device_unregister(struct device *root);
  409. /*
  410. * Manual binding of a device to driver. See drivers/base/bus.c
  411. * for information on use.
  412. */
  413. extern int __must_check device_bind_driver(struct device *dev);
  414. extern void device_release_driver(struct device *dev);
  415. extern int __must_check device_attach(struct device *dev);
  416. extern int __must_check driver_attach(struct device_driver *drv);
  417. extern int __must_check device_reprobe(struct device *dev);
  418. /*
  419. * Easy functions for dynamically creating devices on the fly
  420. */
  421. extern struct device *device_create_vargs(struct class *cls,
  422. struct device *parent,
  423. dev_t devt,
  424. void *drvdata,
  425. const char *fmt,
  426. va_list vargs);
  427. extern struct device *device_create(struct class *cls, struct device *parent,
  428. dev_t devt, void *drvdata,
  429. const char *fmt, ...)
  430. __attribute__((format(printf, 5, 6)));
  431. extern void device_destroy(struct class *cls, dev_t devt);
  432. /*
  433. * Platform "fixup" functions - allow the platform to have their say
  434. * about devices and actions that the general device layer doesn't
  435. * know about.
  436. */
  437. /* Notify platform of device discovery */
  438. extern int (*platform_notify)(struct device *dev);
  439. extern int (*platform_notify_remove)(struct device *dev);
  440. /**
  441. * get_device - atomically increment the reference count for the device.
  442. *
  443. */
  444. extern struct device *get_device(struct device *dev);
  445. extern void put_device(struct device *dev);
  446. /* drivers/base/power/shutdown.c */
  447. extern void device_shutdown(void);
  448. /* drivers/base/sys.c */
  449. extern void sysdev_shutdown(void);
  450. /* debugging and troubleshooting/diagnostic helpers. */
  451. extern const char *dev_driver_string(const struct device *dev);
  452. #define dev_printk(level, dev, format, arg...) \
  453. printk(level "%s %s: " format , dev_driver_string(dev) , \
  454. dev_name(dev) , ## arg)
  455. #define dev_emerg(dev, format, arg...) \
  456. dev_printk(KERN_EMERG , dev , format , ## arg)
  457. #define dev_alert(dev, format, arg...) \
  458. dev_printk(KERN_ALERT , dev , format , ## arg)
  459. #define dev_crit(dev, format, arg...) \
  460. dev_printk(KERN_CRIT , dev , format , ## arg)
  461. #define dev_err(dev, format, arg...) \
  462. dev_printk(KERN_ERR , dev , format , ## arg)
  463. #define dev_warn(dev, format, arg...) \
  464. dev_printk(KERN_WARNING , dev , format , ## arg)
  465. #define dev_notice(dev, format, arg...) \
  466. dev_printk(KERN_NOTICE , dev , format , ## arg)
  467. #define dev_info(dev, format, arg...) \
  468. dev_printk(KERN_INFO , dev , format , ## arg)
  469. #if defined(DEBUG)
  470. #define dev_dbg(dev, format, arg...) \
  471. dev_printk(KERN_DEBUG , dev , format , ## arg)
  472. #elif defined(CONFIG_DYNAMIC_PRINTK_DEBUG)
  473. #define dev_dbg(dev, format, ...) do { \
  474. dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
  475. } while (0)
  476. #else
  477. #define dev_dbg(dev, format, arg...) \
  478. ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
  479. #endif
  480. #ifdef VERBOSE_DEBUG
  481. #define dev_vdbg dev_dbg
  482. #else
  483. #define dev_vdbg(dev, format, arg...) \
  484. ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
  485. #endif
  486. /*
  487. * dev_WARN() acts like dev_printk(), but with the key difference
  488. * of using a WARN/WARN_ON to get the message out, including the
  489. * file/line information and a backtrace.
  490. */
  491. #define dev_WARN(dev, format, arg...) \
  492. WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg);
  493. /* Create alias, so I can be autoloaded. */
  494. #define MODULE_ALIAS_CHARDEV(major,minor) \
  495. MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
  496. #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
  497. MODULE_ALIAS("char-major-" __stringify(major) "-*")
  498. #endif /* _DEVICE_H_ */