device.h 19 KB

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