device.h 20 KB

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