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. int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
  159. void (*class_release)(struct class *class);
  160. void (*dev_release)(struct device *dev);
  161. int (*suspend)(struct device *dev, pm_message_t state);
  162. int (*resume)(struct device *dev);
  163. struct pm_ops *pm;
  164. };
  165. extern int __must_check class_register(struct class *class);
  166. extern void class_unregister(struct class *class);
  167. extern int class_for_each_device(struct class *class, void *data,
  168. int (*fn)(struct device *dev, void *data));
  169. extern struct device *class_find_device(struct class *class, void *data,
  170. int (*match)(struct device *, void *));
  171. struct class_attribute {
  172. struct attribute attr;
  173. ssize_t (*show)(struct class *class, char *buf);
  174. ssize_t (*store)(struct class *class, const char *buf, size_t count);
  175. };
  176. #define CLASS_ATTR(_name, _mode, _show, _store) \
  177. struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
  178. extern int __must_check class_create_file(struct class *class,
  179. const struct class_attribute *attr);
  180. extern void class_remove_file(struct class *class,
  181. const struct class_attribute *attr);
  182. struct class_interface {
  183. struct list_head node;
  184. struct class *class;
  185. int (*add_dev) (struct device *, struct class_interface *);
  186. void (*remove_dev) (struct device *, struct class_interface *);
  187. };
  188. extern int __must_check class_interface_register(struct class_interface *);
  189. extern void class_interface_unregister(struct class_interface *);
  190. extern struct class *class_create(struct module *owner, const char *name);
  191. extern void class_destroy(struct class *cls);
  192. /*
  193. * The type of device, "struct device" is embedded in. A class
  194. * or bus can contain devices of different types
  195. * like "partitions" and "disks", "mouse" and "event".
  196. * This identifies the device type and carries type-specific
  197. * information, equivalent to the kobj_type of a kobject.
  198. * If "name" is specified, the uevent will contain it in
  199. * the DEVTYPE variable.
  200. */
  201. struct device_type {
  202. const char *name;
  203. struct attribute_group **groups;
  204. int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  205. void (*release)(struct device *dev);
  206. int (*suspend)(struct device *dev, pm_message_t state);
  207. int (*resume)(struct device *dev);
  208. struct pm_ops *pm;
  209. };
  210. /* interface for exporting device attributes */
  211. struct device_attribute {
  212. struct attribute attr;
  213. ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  214. char *buf);
  215. ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  216. const char *buf, size_t count);
  217. };
  218. #define DEVICE_ATTR(_name, _mode, _show, _store) \
  219. struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
  220. extern int __must_check device_create_file(struct device *device,
  221. struct device_attribute *entry);
  222. extern void device_remove_file(struct device *dev,
  223. struct device_attribute *attr);
  224. extern int __must_check device_create_bin_file(struct device *dev,
  225. struct bin_attribute *attr);
  226. extern void device_remove_bin_file(struct device *dev,
  227. struct bin_attribute *attr);
  228. extern int device_schedule_callback_owner(struct device *dev,
  229. void (*func)(struct device *dev), struct module *owner);
  230. /* This is a macro to avoid include problems with THIS_MODULE */
  231. #define device_schedule_callback(dev, func) \
  232. device_schedule_callback_owner(dev, func, THIS_MODULE)
  233. /* device resource management */
  234. typedef void (*dr_release_t)(struct device *dev, void *res);
  235. typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
  236. #ifdef CONFIG_DEBUG_DEVRES
  237. extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
  238. const char *name);
  239. #define devres_alloc(release, size, gfp) \
  240. __devres_alloc(release, size, gfp, #release)
  241. #else
  242. extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
  243. #endif
  244. extern void devres_free(void *res);
  245. extern void devres_add(struct device *dev, void *res);
  246. extern void *devres_find(struct device *dev, dr_release_t release,
  247. dr_match_t match, void *match_data);
  248. extern void *devres_get(struct device *dev, void *new_res,
  249. dr_match_t match, void *match_data);
  250. extern void *devres_remove(struct device *dev, dr_release_t release,
  251. dr_match_t match, void *match_data);
  252. extern int devres_destroy(struct device *dev, dr_release_t release,
  253. dr_match_t match, void *match_data);
  254. /* devres group */
  255. extern void * __must_check devres_open_group(struct device *dev, void *id,
  256. gfp_t gfp);
  257. extern void devres_close_group(struct device *dev, void *id);
  258. extern void devres_remove_group(struct device *dev, void *id);
  259. extern int devres_release_group(struct device *dev, void *id);
  260. /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
  261. extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
  262. extern void devm_kfree(struct device *dev, void *p);
  263. struct device_dma_parameters {
  264. /*
  265. * a low level driver may set these to teach IOMMU code about
  266. * sg limitations.
  267. */
  268. unsigned int max_segment_size;
  269. unsigned long segment_boundary_mask;
  270. };
  271. struct device {
  272. struct klist klist_children;
  273. struct klist_node knode_parent; /* node in sibling list */
  274. struct klist_node knode_driver;
  275. struct klist_node knode_bus;
  276. struct device *parent;
  277. struct kobject kobj;
  278. char bus_id[BUS_ID_SIZE]; /* position on parent bus */
  279. struct device_type *type;
  280. unsigned uevent_suppress:1;
  281. struct semaphore sem; /* semaphore to synchronize calls to
  282. * its driver.
  283. */
  284. struct bus_type *bus; /* type of bus device is on */
  285. struct device_driver *driver; /* which driver has allocated this
  286. device */
  287. void *driver_data; /* data private to the driver */
  288. void *platform_data; /* Platform specific data, device
  289. core doesn't touch it */
  290. struct dev_pm_info power;
  291. #ifdef CONFIG_NUMA
  292. int numa_node; /* NUMA node this device is close to */
  293. #endif
  294. u64 *dma_mask; /* dma mask (if dma'able device) */
  295. u64 coherent_dma_mask;/* Like dma_mask, but for
  296. alloc_coherent mappings as
  297. not all hardware supports
  298. 64 bit addresses for consistent
  299. allocations such descriptors. */
  300. struct device_dma_parameters *dma_parms;
  301. struct list_head dma_pools; /* dma pools (if dma'ble) */
  302. struct dma_coherent_mem *dma_mem; /* internal for coherent mem
  303. override */
  304. /* arch specific additions */
  305. struct dev_archdata archdata;
  306. spinlock_t devres_lock;
  307. struct list_head devres_head;
  308. struct list_head node;
  309. struct class *class;
  310. dev_t devt; /* dev_t, creates the sysfs "dev" */
  311. struct attribute_group **groups; /* optional groups */
  312. void (*release)(struct device *dev);
  313. };
  314. /* Get the wakeup routines, which depend on struct device */
  315. #include <linux/pm_wakeup.h>
  316. static inline const char *dev_name(struct device *dev)
  317. {
  318. /* will be changed into kobject_name(&dev->kobj) in the near future */
  319. return dev->bus_id;
  320. }
  321. extern int dev_set_name(struct device *dev, const char *name, ...)
  322. __attribute__((format(printf, 2, 3)));
  323. #ifdef CONFIG_NUMA
  324. static inline int dev_to_node(struct device *dev)
  325. {
  326. return dev->numa_node;
  327. }
  328. static inline void set_dev_node(struct device *dev, int node)
  329. {
  330. dev->numa_node = node;
  331. }
  332. #else
  333. static inline int dev_to_node(struct device *dev)
  334. {
  335. return -1;
  336. }
  337. static inline void set_dev_node(struct device *dev, int node)
  338. {
  339. }
  340. #endif
  341. static inline void *dev_get_drvdata(struct device *dev)
  342. {
  343. return dev->driver_data;
  344. }
  345. static inline void dev_set_drvdata(struct device *dev, void *data)
  346. {
  347. dev->driver_data = data;
  348. }
  349. static inline int device_is_registered(struct device *dev)
  350. {
  351. return dev->kobj.state_in_sysfs;
  352. }
  353. void driver_init(void);
  354. /*
  355. * High level routines for use by the bus drivers
  356. */
  357. extern int __must_check device_register(struct device *dev);
  358. extern void device_unregister(struct device *dev);
  359. extern void device_initialize(struct device *dev);
  360. extern int __must_check device_add(struct device *dev);
  361. extern void device_del(struct device *dev);
  362. extern int device_for_each_child(struct device *dev, void *data,
  363. int (*fn)(struct device *dev, void *data));
  364. extern struct device *device_find_child(struct device *dev, void *data,
  365. int (*match)(struct device *dev, void *data));
  366. extern int device_rename(struct device *dev, char *new_name);
  367. extern int device_move(struct device *dev, struct device *new_parent);
  368. /*
  369. * Manual binding of a device to driver. See drivers/base/bus.c
  370. * for information on use.
  371. */
  372. extern int __must_check device_bind_driver(struct device *dev);
  373. extern void device_release_driver(struct device *dev);
  374. extern int __must_check device_attach(struct device *dev);
  375. extern int __must_check driver_attach(struct device_driver *drv);
  376. extern int __must_check device_reprobe(struct device *dev);
  377. /*
  378. * Easy functions for dynamically creating devices on the fly
  379. */
  380. extern struct device *device_create_vargs(struct class *cls,
  381. struct device *parent,
  382. dev_t devt,
  383. void *drvdata,
  384. const char *fmt,
  385. va_list vargs);
  386. extern struct device *device_create(struct class *cls, struct device *parent,
  387. dev_t devt, const char *fmt, ...)
  388. __attribute__((format(printf, 4, 5)));
  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_ */