device.h 20 KB

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