device.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 <asm/semaphore.h>
  22. #include <asm/atomic.h>
  23. #include <asm/device.h>
  24. #define DEVICE_NAME_SIZE 50
  25. #define DEVICE_NAME_HALF __stringify(20) /* Less than half to accommodate slop */
  26. #define DEVICE_ID_SIZE 32
  27. #define BUS_ID_SIZE KOBJ_NAME_LEN
  28. struct device;
  29. struct device_driver;
  30. struct class;
  31. struct class_device;
  32. struct bus_type;
  33. struct bus_attribute {
  34. struct attribute attr;
  35. ssize_t (*show)(struct bus_type *, char * buf);
  36. ssize_t (*store)(struct bus_type *, 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 module * owner;
  46. struct kset subsys;
  47. struct kset *drivers_kset;
  48. struct kset *devices_kset;
  49. struct klist klist_devices;
  50. struct klist klist_drivers;
  51. struct blocking_notifier_head bus_notifier;
  52. struct bus_attribute * bus_attrs;
  53. struct device_attribute * dev_attrs;
  54. struct driver_attribute * drv_attrs;
  55. int (*match)(struct device * dev, struct device_driver * drv);
  56. int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  57. int (*probe)(struct device * dev);
  58. int (*remove)(struct device * dev);
  59. void (*shutdown)(struct device * dev);
  60. int (*suspend)(struct device * dev, pm_message_t state);
  61. int (*suspend_late)(struct device * dev, pm_message_t state);
  62. int (*resume_early)(struct device * dev);
  63. int (*resume)(struct device * dev);
  64. unsigned int drivers_autoprobe:1;
  65. };
  66. extern int __must_check bus_register(struct bus_type * bus);
  67. extern void bus_unregister(struct bus_type * bus);
  68. extern int __must_check bus_rescan_devices(struct bus_type * bus);
  69. /* iterator helpers for buses */
  70. int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
  71. int (*fn)(struct device *, void *));
  72. struct device * bus_find_device(struct bus_type *bus, struct device *start,
  73. void *data, int (*match)(struct device *, void *));
  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. struct device_driver {
  98. const char * name;
  99. struct bus_type * bus;
  100. struct kobject kobj;
  101. struct klist klist_devices;
  102. struct klist_node knode_bus;
  103. struct module * owner;
  104. const char * mod_name; /* used for built-in modules */
  105. struct module_kobject * mkobj;
  106. int (*probe) (struct device * dev);
  107. int (*remove) (struct device * dev);
  108. void (*shutdown) (struct device * dev);
  109. int (*suspend) (struct device * dev, pm_message_t state);
  110. int (*resume) (struct device * dev);
  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, 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 *, char * buf);
  122. ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
  123. };
  124. #define DRIVER_ATTR(_name,_mode,_show,_store) \
  125. struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
  126. extern int __must_check driver_create_file(struct device_driver *,
  127. struct driver_attribute *);
  128. extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
  129. extern int __must_check driver_for_each_device(struct device_driver * drv,
  130. struct device *start, void *data,
  131. int (*fn)(struct device *, void *));
  132. struct device * driver_find_device(struct device_driver *drv,
  133. struct device *start, void *data,
  134. int (*match)(struct device *, void *));
  135. /*
  136. * device classes
  137. */
  138. struct class {
  139. const char * name;
  140. struct module * owner;
  141. struct kset subsys;
  142. struct list_head children;
  143. struct list_head devices;
  144. struct list_head interfaces;
  145. struct kset class_dirs;
  146. struct semaphore sem; /* locks both the children and interfaces lists */
  147. struct class_attribute * class_attrs;
  148. struct class_device_attribute * class_dev_attrs;
  149. struct device_attribute * dev_attrs;
  150. int (*uevent)(struct class_device *dev, struct kobj_uevent_env *env);
  151. int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
  152. void (*release)(struct class_device *dev);
  153. void (*class_release)(struct class *class);
  154. void (*dev_release)(struct device *dev);
  155. int (*suspend)(struct device *, pm_message_t state);
  156. int (*resume)(struct device *);
  157. };
  158. extern int __must_check class_register(struct class *);
  159. extern void class_unregister(struct class *);
  160. struct class_attribute {
  161. struct attribute attr;
  162. ssize_t (*show)(struct class *, char * buf);
  163. ssize_t (*store)(struct class *, const char * buf, size_t count);
  164. };
  165. #define CLASS_ATTR(_name,_mode,_show,_store) \
  166. struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store)
  167. extern int __must_check class_create_file(struct class *,
  168. const struct class_attribute *);
  169. extern void class_remove_file(struct class *, const struct class_attribute *);
  170. struct class_device_attribute {
  171. struct attribute attr;
  172. ssize_t (*show)(struct class_device *, char * buf);
  173. ssize_t (*store)(struct class_device *, const char * buf, size_t count);
  174. };
  175. #define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
  176. struct class_device_attribute class_device_attr_##_name = \
  177. __ATTR(_name,_mode,_show,_store)
  178. extern int __must_check class_device_create_file(struct class_device *,
  179. const struct class_device_attribute *);
  180. /**
  181. * struct class_device - class devices
  182. * @class: pointer to the parent class for this class device. This is required.
  183. * @devt: for internal use by the driver core only.
  184. * @node: for internal use by the driver core only.
  185. * @kobj: for internal use by the driver core only.
  186. * @groups: optional additional groups to be created
  187. * @dev: if set, a symlink to the struct device is created in the sysfs
  188. * directory for this struct class device.
  189. * @class_data: pointer to whatever you want to store here for this struct
  190. * class_device. Use class_get_devdata() and class_set_devdata() to get and
  191. * set this pointer.
  192. * @parent: pointer to a struct class_device that is the parent of this struct
  193. * class_device. If NULL, this class_device will show up at the root of the
  194. * struct class in sysfs (which is probably what you want to have happen.)
  195. * @release: pointer to a release function for this struct class_device. If
  196. * set, this will be called instead of the class specific release function.
  197. * Only use this if you want to override the default release function, like
  198. * when you are nesting class_device structures.
  199. * @uevent: pointer to a uevent function for this struct class_device. If
  200. * set, this will be called instead of the class specific uevent function.
  201. * Only use this if you want to override the default uevent function, like
  202. * when you are nesting class_device structures.
  203. */
  204. struct class_device {
  205. struct list_head node;
  206. struct kobject kobj;
  207. struct class * class; /* required */
  208. dev_t devt; /* dev_t, creates the sysfs "dev" */
  209. struct device * dev; /* not necessary, but nice to have */
  210. void * class_data; /* class-specific data */
  211. struct class_device *parent; /* parent of this child device, if there is one */
  212. struct attribute_group ** groups; /* optional groups */
  213. void (*release)(struct class_device *dev);
  214. int (*uevent)(struct class_device *dev, struct kobj_uevent_env *env);
  215. char class_id[BUS_ID_SIZE]; /* unique to this class */
  216. };
  217. static inline void *
  218. class_get_devdata (struct class_device *dev)
  219. {
  220. return dev->class_data;
  221. }
  222. static inline void
  223. class_set_devdata (struct class_device *dev, void *data)
  224. {
  225. dev->class_data = data;
  226. }
  227. extern int __must_check class_device_register(struct class_device *);
  228. extern void class_device_unregister(struct class_device *);
  229. extern void class_device_initialize(struct class_device *);
  230. extern int __must_check class_device_add(struct class_device *);
  231. extern void class_device_del(struct class_device *);
  232. extern struct class_device * class_device_get(struct class_device *);
  233. extern void class_device_put(struct class_device *);
  234. extern void class_device_remove_file(struct class_device *,
  235. const struct class_device_attribute *);
  236. extern int __must_check class_device_create_bin_file(struct class_device *,
  237. struct bin_attribute *);
  238. extern void class_device_remove_bin_file(struct class_device *,
  239. struct bin_attribute *);
  240. struct class_interface {
  241. struct list_head node;
  242. struct class *class;
  243. int (*add) (struct class_device *, struct class_interface *);
  244. void (*remove) (struct class_device *, struct class_interface *);
  245. int (*add_dev) (struct device *, struct class_interface *);
  246. void (*remove_dev) (struct device *, struct class_interface *);
  247. };
  248. extern int __must_check class_interface_register(struct class_interface *);
  249. extern void class_interface_unregister(struct class_interface *);
  250. extern struct class *class_create(struct module *owner, const char *name);
  251. extern void class_destroy(struct class *cls);
  252. extern struct class_device *class_device_create(struct class *cls,
  253. struct class_device *parent,
  254. dev_t devt,
  255. struct device *device,
  256. const char *fmt, ...)
  257. __attribute__((format(printf,5,6)));
  258. extern void class_device_destroy(struct class *cls, dev_t devt);
  259. /*
  260. * The type of device, "struct device" is embedded in. A class
  261. * or bus can contain devices of different types
  262. * like "partitions" and "disks", "mouse" and "event".
  263. * This identifies the device type and carries type-specific
  264. * information, equivalent to the kobj_type of a kobject.
  265. * If "name" is specified, the uevent will contain it in
  266. * the DEVTYPE variable.
  267. */
  268. struct device_type {
  269. const char *name;
  270. struct attribute_group **groups;
  271. int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  272. void (*release)(struct device *dev);
  273. int (*suspend)(struct device * dev, pm_message_t state);
  274. int (*resume)(struct device * dev);
  275. };
  276. /* interface for exporting device attributes */
  277. struct device_attribute {
  278. struct attribute attr;
  279. ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  280. char *buf);
  281. ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  282. const char *buf, size_t count);
  283. };
  284. #define DEVICE_ATTR(_name,_mode,_show,_store) \
  285. struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
  286. extern int __must_check device_create_file(struct device *device,
  287. struct device_attribute * entry);
  288. extern void device_remove_file(struct device * dev, struct device_attribute * attr);
  289. extern int __must_check device_create_bin_file(struct device *dev,
  290. struct bin_attribute *attr);
  291. extern void device_remove_bin_file(struct device *dev,
  292. struct bin_attribute *attr);
  293. extern int device_schedule_callback_owner(struct device *dev,
  294. void (*func)(struct device *), struct module *owner);
  295. /* This is a macro to avoid include problems with THIS_MODULE */
  296. #define device_schedule_callback(dev, func) \
  297. device_schedule_callback_owner(dev, func, THIS_MODULE)
  298. /* device resource management */
  299. typedef void (*dr_release_t)(struct device *dev, void *res);
  300. typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
  301. #ifdef CONFIG_DEBUG_DEVRES
  302. extern void * __devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
  303. const char *name);
  304. #define devres_alloc(release, size, gfp) \
  305. __devres_alloc(release, size, gfp, #release)
  306. #else
  307. extern void * devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
  308. #endif
  309. extern void devres_free(void *res);
  310. extern void devres_add(struct device *dev, void *res);
  311. extern void * devres_find(struct device *dev, dr_release_t release,
  312. dr_match_t match, void *match_data);
  313. extern void * devres_get(struct device *dev, void *new_res,
  314. dr_match_t match, void *match_data);
  315. extern void * devres_remove(struct device *dev, dr_release_t release,
  316. dr_match_t match, void *match_data);
  317. extern int devres_destroy(struct device *dev, dr_release_t release,
  318. dr_match_t match, void *match_data);
  319. /* devres group */
  320. extern void * __must_check devres_open_group(struct device *dev, void *id,
  321. gfp_t gfp);
  322. extern void devres_close_group(struct device *dev, void *id);
  323. extern void devres_remove_group(struct device *dev, void *id);
  324. extern int devres_release_group(struct device *dev, void *id);
  325. /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
  326. extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
  327. extern void devm_kfree(struct device *dev, void *p);
  328. struct device {
  329. struct klist klist_children;
  330. struct klist_node knode_parent; /* node in sibling list */
  331. struct klist_node knode_driver;
  332. struct klist_node knode_bus;
  333. struct device *parent;
  334. struct kobject kobj;
  335. char bus_id[BUS_ID_SIZE]; /* position on parent bus */
  336. struct device_type *type;
  337. unsigned is_registered:1;
  338. unsigned uevent_suppress:1;
  339. struct semaphore sem; /* semaphore to synchronize calls to
  340. * its driver.
  341. */
  342. struct bus_type * bus; /* type of bus device is on */
  343. struct device_driver *driver; /* which driver has allocated this
  344. device */
  345. void *driver_data; /* data private to the driver */
  346. void *platform_data; /* Platform specific data, device
  347. core doesn't touch it */
  348. struct dev_pm_info power;
  349. #ifdef CONFIG_NUMA
  350. int numa_node; /* NUMA node this device is close to */
  351. #endif
  352. u64 *dma_mask; /* dma mask (if dma'able device) */
  353. u64 coherent_dma_mask;/* Like dma_mask, but for
  354. alloc_coherent mappings as
  355. not all hardware supports
  356. 64 bit addresses for consistent
  357. allocations such descriptors. */
  358. struct list_head dma_pools; /* dma pools (if dma'ble) */
  359. struct dma_coherent_mem *dma_mem; /* internal for coherent mem
  360. override */
  361. /* arch specific additions */
  362. struct dev_archdata archdata;
  363. spinlock_t devres_lock;
  364. struct list_head devres_head;
  365. /* class_device migration path */
  366. struct list_head node;
  367. struct class *class;
  368. dev_t devt; /* dev_t, creates the sysfs "dev" */
  369. struct attribute_group **groups; /* optional groups */
  370. void (*release)(struct device * dev);
  371. };
  372. #ifdef CONFIG_NUMA
  373. static inline int dev_to_node(struct device *dev)
  374. {
  375. return dev->numa_node;
  376. }
  377. static inline void set_dev_node(struct device *dev, int node)
  378. {
  379. dev->numa_node = node;
  380. }
  381. #else
  382. static inline int dev_to_node(struct device *dev)
  383. {
  384. return -1;
  385. }
  386. static inline void set_dev_node(struct device *dev, int node)
  387. {
  388. }
  389. #endif
  390. static inline void *
  391. dev_get_drvdata (struct device *dev)
  392. {
  393. return dev->driver_data;
  394. }
  395. static inline void
  396. dev_set_drvdata (struct device *dev, void *data)
  397. {
  398. dev->driver_data = data;
  399. }
  400. static inline int device_is_registered(struct device *dev)
  401. {
  402. return dev->is_registered;
  403. }
  404. void driver_init(void);
  405. /*
  406. * High level routines for use by the bus drivers
  407. */
  408. extern int __must_check device_register(struct device * dev);
  409. extern void device_unregister(struct device * dev);
  410. extern void device_initialize(struct device * dev);
  411. extern int __must_check device_add(struct device * dev);
  412. extern void device_del(struct device * dev);
  413. extern int device_for_each_child(struct device *, void *,
  414. int (*fn)(struct device *, void *));
  415. extern struct device *device_find_child(struct device *, void *data,
  416. int (*match)(struct device *, void *));
  417. extern int device_rename(struct device *dev, char *new_name);
  418. extern int device_move(struct device *dev, struct device *new_parent);
  419. /*
  420. * Manual binding of a device to driver. See drivers/base/bus.c
  421. * for information on use.
  422. */
  423. extern int __must_check device_bind_driver(struct device *dev);
  424. extern void device_release_driver(struct device * dev);
  425. extern int __must_check device_attach(struct device * dev);
  426. extern int __must_check driver_attach(struct device_driver *drv);
  427. extern int __must_check device_reprobe(struct device *dev);
  428. /*
  429. * Easy functions for dynamically creating devices on the fly
  430. */
  431. extern struct device *device_create(struct class *cls, struct device *parent,
  432. dev_t devt, const char *fmt, ...)
  433. __attribute__((format(printf,4,5)));
  434. extern void device_destroy(struct class *cls, dev_t devt);
  435. #ifdef CONFIG_PM_SLEEP
  436. extern void destroy_suspended_device(struct class *cls, dev_t devt);
  437. #else /* !CONFIG_PM_SLEEP */
  438. static inline void destroy_suspended_device(struct class *cls, dev_t devt)
  439. {
  440. device_destroy(cls, devt);
  441. }
  442. #endif /* !CONFIG_PM_SLEEP */
  443. /*
  444. * Platform "fixup" functions - allow the platform to have their say
  445. * about devices and actions that the general device layer doesn't
  446. * know about.
  447. */
  448. /* Notify platform of device discovery */
  449. extern int (*platform_notify)(struct device * dev);
  450. extern int (*platform_notify_remove)(struct device * dev);
  451. /**
  452. * get_device - atomically increment the reference count for the device.
  453. *
  454. */
  455. extern struct device * get_device(struct device * dev);
  456. extern void put_device(struct device * dev);
  457. /* drivers/base/power/shutdown.c */
  458. extern void device_shutdown(void);
  459. /* drivers/base/sys.c */
  460. extern void sysdev_shutdown(void);
  461. /* debugging and troubleshooting/diagnostic helpers. */
  462. extern const char *dev_driver_string(struct device *dev);
  463. #define dev_printk(level, dev, format, arg...) \
  464. printk(level "%s %s: " format , dev_driver_string(dev) , (dev)->bus_id , ## arg)
  465. #define dev_emerg(dev, format, arg...) \
  466. dev_printk(KERN_EMERG , dev , format , ## arg)
  467. #define dev_alert(dev, format, arg...) \
  468. dev_printk(KERN_ALERT , dev , format , ## arg)
  469. #define dev_crit(dev, format, arg...) \
  470. dev_printk(KERN_CRIT , dev , format , ## arg)
  471. #define dev_err(dev, format, arg...) \
  472. dev_printk(KERN_ERR , dev , format , ## arg)
  473. #define dev_warn(dev, format, arg...) \
  474. dev_printk(KERN_WARNING , dev , format , ## arg)
  475. #define dev_notice(dev, format, arg...) \
  476. dev_printk(KERN_NOTICE , dev , format , ## arg)
  477. #define dev_info(dev, format, arg...) \
  478. dev_printk(KERN_INFO , dev , format , ## arg)
  479. #ifdef DEBUG
  480. #define dev_dbg(dev, format, arg...) \
  481. dev_printk(KERN_DEBUG , dev , format , ## arg)
  482. #else
  483. static inline int __attribute__ ((format (printf, 2, 3)))
  484. dev_dbg(struct device * dev, const char * fmt, ...)
  485. {
  486. return 0;
  487. }
  488. #endif
  489. #ifdef VERBOSE_DEBUG
  490. #define dev_vdbg dev_dbg
  491. #else
  492. static inline int __attribute__ ((format (printf, 2, 3)))
  493. dev_vdbg(struct device * dev, const char * fmt, ...)
  494. {
  495. return 0;
  496. }
  497. #endif
  498. /* Create alias, so I can be autoloaded. */
  499. #define MODULE_ALIAS_CHARDEV(major,minor) \
  500. MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
  501. #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
  502. MODULE_ALIAS("char-major-" __stringify(major) "-*")
  503. #endif /* _DEVICE_H_ */