device.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 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 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 kset subsys;
  150. struct list_head devices;
  151. struct list_head interfaces;
  152. struct kset class_dirs;
  153. struct semaphore sem; /* locks children, devices, interfaces */
  154. struct class_attribute *class_attrs;
  155. struct device_attribute *dev_attrs;
  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. };
  162. extern int __must_check class_register(struct class *class);
  163. extern void class_unregister(struct class *class);
  164. extern int class_for_each_device(struct class *class, void *data,
  165. int (*fn)(struct device *dev, void *data));
  166. extern struct device *class_find_device(struct class *class, void *data,
  167. int (*match)(struct device *, void *));
  168. struct class_attribute {
  169. struct attribute attr;
  170. ssize_t (*show)(struct class *class, char *buf);
  171. ssize_t (*store)(struct class *class, const char *buf, size_t count);
  172. };
  173. #define CLASS_ATTR(_name, _mode, _show, _store) \
  174. struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
  175. extern int __must_check class_create_file(struct class *class,
  176. const struct class_attribute *attr);
  177. extern void class_remove_file(struct class *class,
  178. const struct class_attribute *attr);
  179. struct class_interface {
  180. struct list_head node;
  181. struct class *class;
  182. int (*add_dev) (struct device *, struct class_interface *);
  183. void (*remove_dev) (struct device *, struct class_interface *);
  184. };
  185. extern int __must_check class_interface_register(struct class_interface *);
  186. extern void class_interface_unregister(struct class_interface *);
  187. extern struct class *class_create(struct module *owner, const char *name);
  188. extern void class_destroy(struct class *cls);
  189. /*
  190. * The type of device, "struct device" is embedded in. A class
  191. * or bus can contain devices of different types
  192. * like "partitions" and "disks", "mouse" and "event".
  193. * This identifies the device type and carries type-specific
  194. * information, equivalent to the kobj_type of a kobject.
  195. * If "name" is specified, the uevent will contain it in
  196. * the DEVTYPE variable.
  197. */
  198. struct device_type {
  199. const char *name;
  200. struct attribute_group **groups;
  201. int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  202. void (*release)(struct device *dev);
  203. int (*suspend)(struct device *dev, pm_message_t state);
  204. int (*resume)(struct device *dev);
  205. };
  206. /* interface for exporting device attributes */
  207. struct device_attribute {
  208. struct attribute attr;
  209. ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  210. char *buf);
  211. ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  212. const char *buf, size_t count);
  213. };
  214. #define DEVICE_ATTR(_name, _mode, _show, _store) \
  215. struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
  216. extern int __must_check device_create_file(struct device *device,
  217. struct device_attribute *entry);
  218. extern void device_remove_file(struct device *dev,
  219. struct device_attribute *attr);
  220. extern int __must_check device_create_bin_file(struct device *dev,
  221. struct bin_attribute *attr);
  222. extern void device_remove_bin_file(struct device *dev,
  223. struct bin_attribute *attr);
  224. extern int device_schedule_callback_owner(struct device *dev,
  225. void (*func)(struct device *dev), struct module *owner);
  226. /* This is a macro to avoid include problems with THIS_MODULE */
  227. #define device_schedule_callback(dev, func) \
  228. device_schedule_callback_owner(dev, func, THIS_MODULE)
  229. /* device resource management */
  230. typedef void (*dr_release_t)(struct device *dev, void *res);
  231. typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
  232. #ifdef CONFIG_DEBUG_DEVRES
  233. extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
  234. const char *name);
  235. #define devres_alloc(release, size, gfp) \
  236. __devres_alloc(release, size, gfp, #release)
  237. #else
  238. extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
  239. #endif
  240. extern void devres_free(void *res);
  241. extern void devres_add(struct device *dev, void *res);
  242. extern void *devres_find(struct device *dev, dr_release_t release,
  243. dr_match_t match, void *match_data);
  244. extern void *devres_get(struct device *dev, void *new_res,
  245. dr_match_t match, void *match_data);
  246. extern void *devres_remove(struct device *dev, dr_release_t release,
  247. dr_match_t match, void *match_data);
  248. extern int devres_destroy(struct device *dev, dr_release_t release,
  249. dr_match_t match, void *match_data);
  250. /* devres group */
  251. extern void * __must_check devres_open_group(struct device *dev, void *id,
  252. gfp_t gfp);
  253. extern void devres_close_group(struct device *dev, void *id);
  254. extern void devres_remove_group(struct device *dev, void *id);
  255. extern int devres_release_group(struct device *dev, void *id);
  256. /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
  257. extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
  258. extern void devm_kfree(struct device *dev, void *p);
  259. struct device_dma_parameters {
  260. /*
  261. * a low level driver may set these to teach IOMMU code about
  262. * sg limitations.
  263. */
  264. unsigned int max_segment_size;
  265. unsigned long segment_boundary_mask;
  266. };
  267. struct device {
  268. struct klist klist_children;
  269. struct klist_node knode_parent; /* node in sibling list */
  270. struct klist_node knode_driver;
  271. struct klist_node knode_bus;
  272. struct device *parent;
  273. struct kobject kobj;
  274. char bus_id[BUS_ID_SIZE]; /* position on parent bus */
  275. struct device_type *type;
  276. unsigned uevent_suppress:1;
  277. struct semaphore sem; /* semaphore to synchronize calls to
  278. * its driver.
  279. */
  280. struct bus_type *bus; /* type of bus device is on */
  281. struct device_driver *driver; /* which driver has allocated this
  282. device */
  283. void *driver_data; /* data private to the driver */
  284. void *platform_data; /* Platform specific data, device
  285. core doesn't touch it */
  286. struct dev_pm_info power;
  287. #ifdef CONFIG_NUMA
  288. int numa_node; /* NUMA node this device is close to */
  289. #endif
  290. u64 *dma_mask; /* dma mask (if dma'able device) */
  291. u64 coherent_dma_mask;/* Like dma_mask, but for
  292. alloc_coherent mappings as
  293. not all hardware supports
  294. 64 bit addresses for consistent
  295. allocations such descriptors. */
  296. struct device_dma_parameters *dma_parms;
  297. struct list_head dma_pools; /* dma pools (if dma'ble) */
  298. struct dma_coherent_mem *dma_mem; /* internal for coherent mem
  299. override */
  300. /* arch specific additions */
  301. struct dev_archdata archdata;
  302. spinlock_t devres_lock;
  303. struct list_head devres_head;
  304. struct list_head node;
  305. struct class *class;
  306. dev_t devt; /* dev_t, creates the sysfs "dev" */
  307. struct attribute_group **groups; /* optional groups */
  308. void (*release)(struct device *dev);
  309. };
  310. /* Get the wakeup routines, which depend on struct device */
  311. #include <linux/pm_wakeup.h>
  312. static inline const char *dev_name(struct device *dev)
  313. {
  314. /* will be changed into kobject_name(&dev->kobj) in the near future */
  315. return dev->bus_id;
  316. }
  317. #ifdef CONFIG_NUMA
  318. static inline int dev_to_node(struct device *dev)
  319. {
  320. return dev->numa_node;
  321. }
  322. static inline void set_dev_node(struct device *dev, int node)
  323. {
  324. dev->numa_node = node;
  325. }
  326. #else
  327. static inline int dev_to_node(struct device *dev)
  328. {
  329. return -1;
  330. }
  331. static inline void set_dev_node(struct device *dev, int node)
  332. {
  333. }
  334. #endif
  335. static inline void *dev_get_drvdata(struct device *dev)
  336. {
  337. return dev->driver_data;
  338. }
  339. static inline void dev_set_drvdata(struct device *dev, void *data)
  340. {
  341. dev->driver_data = data;
  342. }
  343. static inline int device_is_registered(struct device *dev)
  344. {
  345. return dev->kobj.state_in_sysfs;
  346. }
  347. void driver_init(void);
  348. /*
  349. * High level routines for use by the bus drivers
  350. */
  351. extern int __must_check device_register(struct device *dev);
  352. extern void device_unregister(struct device *dev);
  353. extern void device_initialize(struct device *dev);
  354. extern int __must_check device_add(struct device *dev);
  355. extern void device_del(struct device *dev);
  356. extern int device_for_each_child(struct device *dev, void *data,
  357. int (*fn)(struct device *dev, void *data));
  358. extern struct device *device_find_child(struct device *dev, void *data,
  359. int (*match)(struct device *dev, void *data));
  360. extern int device_rename(struct device *dev, char *new_name);
  361. extern int device_move(struct device *dev, struct device *new_parent);
  362. /*
  363. * Manual binding of a device to driver. See drivers/base/bus.c
  364. * for information on use.
  365. */
  366. extern int __must_check device_bind_driver(struct device *dev);
  367. extern void device_release_driver(struct device *dev);
  368. extern int __must_check device_attach(struct device *dev);
  369. extern int __must_check driver_attach(struct device_driver *drv);
  370. extern int __must_check device_reprobe(struct device *dev);
  371. /*
  372. * Easy functions for dynamically creating devices on the fly
  373. */
  374. extern struct device *device_create_vargs(struct class *cls,
  375. struct device *parent,
  376. dev_t devt,
  377. void *drvdata,
  378. const char *fmt,
  379. va_list vargs);
  380. extern struct device *device_create(struct class *cls, struct device *parent,
  381. dev_t devt, const char *fmt, ...)
  382. __attribute__((format(printf, 4, 5)));
  383. extern struct device *device_create_drvdata(struct class *cls,
  384. struct device *parent,
  385. dev_t devt,
  386. void *drvdata,
  387. const char *fmt, ...)
  388. __attribute__((format(printf, 5, 6)));
  389. extern void device_destroy(struct class *cls, dev_t devt);
  390. /*
  391. * Platform "fixup" functions - allow the platform to have their say
  392. * about devices and actions that the general device layer doesn't
  393. * know about.
  394. */
  395. /* Notify platform of device discovery */
  396. extern int (*platform_notify)(struct device *dev);
  397. extern int (*platform_notify_remove)(struct device *dev);
  398. /**
  399. * get_device - atomically increment the reference count for the device.
  400. *
  401. */
  402. extern struct device *get_device(struct device *dev);
  403. extern void put_device(struct device *dev);
  404. /* drivers/base/power/shutdown.c */
  405. extern void device_shutdown(void);
  406. /* drivers/base/sys.c */
  407. extern void sysdev_shutdown(void);
  408. /* debugging and troubleshooting/diagnostic helpers. */
  409. extern const char *dev_driver_string(struct device *dev);
  410. #define dev_printk(level, dev, format, arg...) \
  411. printk(level "%s %s: " format , dev_driver_string(dev) , \
  412. dev_name(dev) , ## arg)
  413. #define dev_emerg(dev, format, arg...) \
  414. dev_printk(KERN_EMERG , dev , format , ## arg)
  415. #define dev_alert(dev, format, arg...) \
  416. dev_printk(KERN_ALERT , dev , format , ## arg)
  417. #define dev_crit(dev, format, arg...) \
  418. dev_printk(KERN_CRIT , dev , format , ## arg)
  419. #define dev_err(dev, format, arg...) \
  420. dev_printk(KERN_ERR , dev , format , ## arg)
  421. #define dev_warn(dev, format, arg...) \
  422. dev_printk(KERN_WARNING , dev , format , ## arg)
  423. #define dev_notice(dev, format, arg...) \
  424. dev_printk(KERN_NOTICE , dev , format , ## arg)
  425. #define dev_info(dev, format, arg...) \
  426. dev_printk(KERN_INFO , dev , format , ## arg)
  427. #ifdef DEBUG
  428. #define dev_dbg(dev, format, arg...) \
  429. dev_printk(KERN_DEBUG , dev , format , ## arg)
  430. #else
  431. #define dev_dbg(dev, format, arg...) \
  432. ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
  433. #endif
  434. #ifdef VERBOSE_DEBUG
  435. #define dev_vdbg dev_dbg
  436. #else
  437. #define dev_vdbg(dev, format, arg...) \
  438. ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
  439. #endif
  440. /* Create alias, so I can be autoloaded. */
  441. #define MODULE_ALIAS_CHARDEV(major,minor) \
  442. MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
  443. #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
  444. MODULE_ALIAS("char-major-" __stringify(major) "-*")
  445. #endif /* _DEVICE_H_ */