device.h 17 KB

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