device.h 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /*
  2. * device.h - generic, centralized driver model
  3. *
  4. * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
  5. * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
  6. * Copyright (c) 2008-2009 Novell Inc.
  7. *
  8. * This file is released under the GPLv2
  9. *
  10. * See Documentation/driver-model/ for more information.
  11. */
  12. #ifndef _DEVICE_H_
  13. #define _DEVICE_H_
  14. #include <linux/ioport.h>
  15. #include <linux/kobject.h>
  16. #include <linux/klist.h>
  17. #include <linux/list.h>
  18. #include <linux/lockdep.h>
  19. #include <linux/compiler.h>
  20. #include <linux/types.h>
  21. #include <linux/mutex.h>
  22. #include <linux/pm.h>
  23. #include <linux/atomic.h>
  24. #include <asm/device.h>
  25. struct device;
  26. struct device_private;
  27. struct device_driver;
  28. struct driver_private;
  29. struct module;
  30. struct class;
  31. struct subsys_private;
  32. struct bus_type;
  33. struct device_node;
  34. struct iommu_ops;
  35. struct bus_attribute {
  36. struct attribute attr;
  37. ssize_t (*show)(struct bus_type *bus, char *buf);
  38. ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
  39. };
  40. #define BUS_ATTR(_name, _mode, _show, _store) \
  41. struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
  42. extern int __must_check bus_create_file(struct bus_type *,
  43. struct bus_attribute *);
  44. extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
  45. /**
  46. * struct bus_type - The bus type of the device
  47. *
  48. * @name: The name of the bus.
  49. * @dev_name: Used for subsystems to enumerate devices like ("foo%u", dev->id).
  50. * @dev_root: Default device to use as the parent.
  51. * @bus_attrs: Default attributes of the bus.
  52. * @dev_attrs: Default attributes of the devices on the bus.
  53. * @drv_attrs: Default attributes of the device drivers on the bus.
  54. * @match: Called, perhaps multiple times, whenever a new device or driver
  55. * is added for this bus. It should return a nonzero value if the
  56. * given device can be handled by the given driver.
  57. * @uevent: Called when a device is added, removed, or a few other things
  58. * that generate uevents to add the environment variables.
  59. * @probe: Called when a new device or driver add to this bus, and callback
  60. * the specific driver's probe to initial the matched device.
  61. * @remove: Called when a device removed from this bus.
  62. * @shutdown: Called at shut-down time to quiesce the device.
  63. * @suspend: Called when a device on this bus wants to go to sleep mode.
  64. * @resume: Called to bring a device on this bus out of sleep mode.
  65. * @pm: Power management operations of this bus, callback the specific
  66. * device driver's pm-ops.
  67. * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU
  68. * driver implementations to a bus and allow the driver to do
  69. * bus-specific setup
  70. * @p: The private data of the driver core, only the driver core can
  71. * touch this.
  72. *
  73. * A bus is a channel between the processor and one or more devices. For the
  74. * purposes of the device model, all devices are connected via a bus, even if
  75. * it is an internal, virtual, "platform" bus. Buses can plug into each other.
  76. * A USB controller is usually a PCI device, for example. The device model
  77. * represents the actual connections between buses and the devices they control.
  78. * A bus is represented by the bus_type structure. It contains the name, the
  79. * default attributes, the bus' methods, PM operations, and the driver core's
  80. * private data.
  81. */
  82. struct bus_type {
  83. const char *name;
  84. const char *dev_name;
  85. struct device *dev_root;
  86. struct bus_attribute *bus_attrs;
  87. struct device_attribute *dev_attrs;
  88. struct driver_attribute *drv_attrs;
  89. int (*match)(struct device *dev, struct device_driver *drv);
  90. int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  91. int (*probe)(struct device *dev);
  92. int (*remove)(struct device *dev);
  93. void (*shutdown)(struct device *dev);
  94. int (*suspend)(struct device *dev, pm_message_t state);
  95. int (*resume)(struct device *dev);
  96. const struct dev_pm_ops *pm;
  97. struct iommu_ops *iommu_ops;
  98. struct subsys_private *p;
  99. };
  100. /* This is a #define to keep the compiler from merging different
  101. * instances of the __key variable */
  102. #define bus_register(subsys) \
  103. ({ \
  104. static struct lock_class_key __key; \
  105. __bus_register(subsys, &__key); \
  106. })
  107. extern int __must_check __bus_register(struct bus_type *bus,
  108. struct lock_class_key *key);
  109. extern void bus_unregister(struct bus_type *bus);
  110. extern int __must_check bus_rescan_devices(struct bus_type *bus);
  111. /* iterator helpers for buses */
  112. struct subsys_dev_iter {
  113. struct klist_iter ki;
  114. const struct device_type *type;
  115. };
  116. void subsys_dev_iter_init(struct subsys_dev_iter *iter,
  117. struct bus_type *subsys,
  118. struct device *start,
  119. const struct device_type *type);
  120. struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter);
  121. void subsys_dev_iter_exit(struct subsys_dev_iter *iter);
  122. int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
  123. int (*fn)(struct device *dev, void *data));
  124. struct device *bus_find_device(struct bus_type *bus, struct device *start,
  125. void *data,
  126. int (*match)(struct device *dev, void *data));
  127. struct device *bus_find_device_by_name(struct bus_type *bus,
  128. struct device *start,
  129. const char *name);
  130. struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id,
  131. struct device *hint);
  132. int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
  133. void *data, int (*fn)(struct device_driver *, void *));
  134. void bus_sort_breadthfirst(struct bus_type *bus,
  135. int (*compare)(const struct device *a,
  136. const struct device *b));
  137. /*
  138. * Bus notifiers: Get notified of addition/removal of devices
  139. * and binding/unbinding of drivers to devices.
  140. * In the long run, it should be a replacement for the platform
  141. * notify hooks.
  142. */
  143. struct notifier_block;
  144. extern int bus_register_notifier(struct bus_type *bus,
  145. struct notifier_block *nb);
  146. extern int bus_unregister_notifier(struct bus_type *bus,
  147. struct notifier_block *nb);
  148. /* All 4 notifers below get called with the target struct device *
  149. * as an argument. Note that those functions are likely to be called
  150. * with the device lock held in the core, so be careful.
  151. */
  152. #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
  153. #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
  154. #define BUS_NOTIFY_BIND_DRIVER 0x00000003 /* driver about to be
  155. bound */
  156. #define BUS_NOTIFY_BOUND_DRIVER 0x00000004 /* driver bound to device */
  157. #define BUS_NOTIFY_UNBIND_DRIVER 0x00000005 /* driver about to be
  158. unbound */
  159. #define BUS_NOTIFY_UNBOUND_DRIVER 0x00000006 /* driver is unbound
  160. from the device */
  161. extern struct kset *bus_get_kset(struct bus_type *bus);
  162. extern struct klist *bus_get_device_klist(struct bus_type *bus);
  163. /**
  164. * struct device_driver - The basic device driver structure
  165. * @name: Name of the device driver.
  166. * @bus: The bus which the device of this driver belongs to.
  167. * @owner: The module owner.
  168. * @mod_name: Used for built-in modules.
  169. * @suppress_bind_attrs: Disables bind/unbind via sysfs.
  170. * @of_match_table: The open firmware table.
  171. * @probe: Called to query the existence of a specific device,
  172. * whether this driver can work with it, and bind the driver
  173. * to a specific device.
  174. * @remove: Called when the device is removed from the system to
  175. * unbind a device from this driver.
  176. * @shutdown: Called at shut-down time to quiesce the device.
  177. * @suspend: Called to put the device to sleep mode. Usually to a
  178. * low power state.
  179. * @resume: Called to bring a device from sleep mode.
  180. * @groups: Default attributes that get created by the driver core
  181. * automatically.
  182. * @pm: Power management operations of the device which matched
  183. * this driver.
  184. * @p: Driver core's private data, no one other than the driver
  185. * core can touch this.
  186. *
  187. * The device driver-model tracks all of the drivers known to the system.
  188. * The main reason for this tracking is to enable the driver core to match
  189. * up drivers with new devices. Once drivers are known objects within the
  190. * system, however, a number of other things become possible. Device drivers
  191. * can export information and configuration variables that are independent
  192. * of any specific device.
  193. */
  194. struct device_driver {
  195. const char *name;
  196. struct bus_type *bus;
  197. struct module *owner;
  198. const char *mod_name; /* used for built-in modules */
  199. bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
  200. const struct of_device_id *of_match_table;
  201. int (*probe) (struct device *dev);
  202. int (*remove) (struct device *dev);
  203. void (*shutdown) (struct device *dev);
  204. int (*suspend) (struct device *dev, pm_message_t state);
  205. int (*resume) (struct device *dev);
  206. const struct attribute_group **groups;
  207. const struct dev_pm_ops *pm;
  208. struct driver_private *p;
  209. };
  210. extern int __must_check driver_register(struct device_driver *drv);
  211. extern void driver_unregister(struct device_driver *drv);
  212. extern struct device_driver *driver_find(const char *name,
  213. struct bus_type *bus);
  214. extern int driver_probe_done(void);
  215. extern void wait_for_device_probe(void);
  216. /* sysfs interface for exporting driver attributes */
  217. struct driver_attribute {
  218. struct attribute attr;
  219. ssize_t (*show)(struct device_driver *driver, char *buf);
  220. ssize_t (*store)(struct device_driver *driver, const char *buf,
  221. size_t count);
  222. };
  223. #define DRIVER_ATTR(_name, _mode, _show, _store) \
  224. struct driver_attribute driver_attr_##_name = \
  225. __ATTR(_name, _mode, _show, _store)
  226. extern int __must_check driver_create_file(struct device_driver *driver,
  227. const struct driver_attribute *attr);
  228. extern void driver_remove_file(struct device_driver *driver,
  229. const struct driver_attribute *attr);
  230. extern int __must_check driver_add_kobj(struct device_driver *drv,
  231. struct kobject *kobj,
  232. const char *fmt, ...);
  233. extern int __must_check driver_for_each_device(struct device_driver *drv,
  234. struct device *start,
  235. void *data,
  236. int (*fn)(struct device *dev,
  237. void *));
  238. struct device *driver_find_device(struct device_driver *drv,
  239. struct device *start, void *data,
  240. int (*match)(struct device *dev, void *data));
  241. /**
  242. * struct subsys_interface - interfaces to device functions
  243. * @name: name of the device function
  244. * @subsys: subsytem of the devices to attach to
  245. * @node: the list of functions registered at the subsystem
  246. * @add_dev: device hookup to device function handler
  247. * @remove_dev: device hookup to device function handler
  248. *
  249. * Simple interfaces attached to a subsystem. Multiple interfaces can
  250. * attach to a subsystem and its devices. Unlike drivers, they do not
  251. * exclusively claim or control devices. Interfaces usually represent
  252. * a specific functionality of a subsystem/class of devices.
  253. */
  254. struct subsys_interface {
  255. const char *name;
  256. struct bus_type *subsys;
  257. struct list_head node;
  258. int (*add_dev)(struct device *dev, struct subsys_interface *sif);
  259. int (*remove_dev)(struct device *dev, struct subsys_interface *sif);
  260. };
  261. int subsys_interface_register(struct subsys_interface *sif);
  262. void subsys_interface_unregister(struct subsys_interface *sif);
  263. int subsys_system_register(struct bus_type *subsys,
  264. const struct attribute_group **groups);
  265. /**
  266. * struct class - device classes
  267. * @name: Name of the class.
  268. * @owner: The module owner.
  269. * @class_attrs: Default attributes of this class.
  270. * @dev_attrs: Default attributes of the devices belong to the class.
  271. * @dev_bin_attrs: Default binary attributes of the devices belong to the class.
  272. * @dev_kobj: The kobject that represents this class and links it into the hierarchy.
  273. * @dev_uevent: Called when a device is added, removed from this class, or a
  274. * few other things that generate uevents to add the environment
  275. * variables.
  276. * @devnode: Callback to provide the devtmpfs.
  277. * @class_release: Called to release this class.
  278. * @dev_release: Called to release the device.
  279. * @suspend: Used to put the device to sleep mode, usually to a low power
  280. * state.
  281. * @resume: Used to bring the device from the sleep mode.
  282. * @ns_type: Callbacks so sysfs can detemine namespaces.
  283. * @namespace: Namespace of the device belongs to this class.
  284. * @pm: The default device power management operations of this class.
  285. * @p: The private data of the driver core, no one other than the
  286. * driver core can touch this.
  287. *
  288. * A class is a higher-level view of a device that abstracts out low-level
  289. * implementation details. Drivers may see a SCSI disk or an ATA disk, but,
  290. * at the class level, they are all simply disks. Classes allow user space
  291. * to work with devices based on what they do, rather than how they are
  292. * connected or how they work.
  293. */
  294. struct class {
  295. const char *name;
  296. struct module *owner;
  297. struct class_attribute *class_attrs;
  298. struct device_attribute *dev_attrs;
  299. struct bin_attribute *dev_bin_attrs;
  300. struct kobject *dev_kobj;
  301. int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
  302. char *(*devnode)(struct device *dev, umode_t *mode);
  303. void (*class_release)(struct class *class);
  304. void (*dev_release)(struct device *dev);
  305. int (*suspend)(struct device *dev, pm_message_t state);
  306. int (*resume)(struct device *dev);
  307. const struct kobj_ns_type_operations *ns_type;
  308. const void *(*namespace)(struct device *dev);
  309. const struct dev_pm_ops *pm;
  310. struct subsys_private *p;
  311. };
  312. struct class_dev_iter {
  313. struct klist_iter ki;
  314. const struct device_type *type;
  315. };
  316. extern struct kobject *sysfs_dev_block_kobj;
  317. extern struct kobject *sysfs_dev_char_kobj;
  318. extern int __must_check __class_register(struct class *class,
  319. struct lock_class_key *key);
  320. extern void class_unregister(struct class *class);
  321. /* This is a #define to keep the compiler from merging different
  322. * instances of the __key variable */
  323. #define class_register(class) \
  324. ({ \
  325. static struct lock_class_key __key; \
  326. __class_register(class, &__key); \
  327. })
  328. struct class_compat;
  329. struct class_compat *class_compat_register(const char *name);
  330. void class_compat_unregister(struct class_compat *cls);
  331. int class_compat_create_link(struct class_compat *cls, struct device *dev,
  332. struct device *device_link);
  333. void class_compat_remove_link(struct class_compat *cls, struct device *dev,
  334. struct device *device_link);
  335. extern void class_dev_iter_init(struct class_dev_iter *iter,
  336. struct class *class,
  337. struct device *start,
  338. const struct device_type *type);
  339. extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
  340. extern void class_dev_iter_exit(struct class_dev_iter *iter);
  341. extern int class_for_each_device(struct class *class, struct device *start,
  342. void *data,
  343. int (*fn)(struct device *dev, void *data));
  344. extern struct device *class_find_device(struct class *class,
  345. struct device *start, void *data,
  346. int (*match)(struct device *, void *));
  347. struct class_attribute {
  348. struct attribute attr;
  349. ssize_t (*show)(struct class *class, struct class_attribute *attr,
  350. char *buf);
  351. ssize_t (*store)(struct class *class, struct class_attribute *attr,
  352. const char *buf, size_t count);
  353. const void *(*namespace)(struct class *class,
  354. const struct class_attribute *attr);
  355. };
  356. #define CLASS_ATTR(_name, _mode, _show, _store) \
  357. struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
  358. extern int __must_check class_create_file(struct class *class,
  359. const struct class_attribute *attr);
  360. extern void class_remove_file(struct class *class,
  361. const struct class_attribute *attr);
  362. /* Simple class attribute that is just a static string */
  363. struct class_attribute_string {
  364. struct class_attribute attr;
  365. char *str;
  366. };
  367. /* Currently read-only only */
  368. #define _CLASS_ATTR_STRING(_name, _mode, _str) \
  369. { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
  370. #define CLASS_ATTR_STRING(_name, _mode, _str) \
  371. struct class_attribute_string class_attr_##_name = \
  372. _CLASS_ATTR_STRING(_name, _mode, _str)
  373. extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
  374. char *buf);
  375. struct class_interface {
  376. struct list_head node;
  377. struct class *class;
  378. int (*add_dev) (struct device *, struct class_interface *);
  379. void (*remove_dev) (struct device *, struct class_interface *);
  380. };
  381. extern int __must_check class_interface_register(struct class_interface *);
  382. extern void class_interface_unregister(struct class_interface *);
  383. extern struct class * __must_check __class_create(struct module *owner,
  384. const char *name,
  385. struct lock_class_key *key);
  386. extern void class_destroy(struct class *cls);
  387. /* This is a #define to keep the compiler from merging different
  388. * instances of the __key variable */
  389. #define class_create(owner, name) \
  390. ({ \
  391. static struct lock_class_key __key; \
  392. __class_create(owner, name, &__key); \
  393. })
  394. /*
  395. * The type of device, "struct device" is embedded in. A class
  396. * or bus can contain devices of different types
  397. * like "partitions" and "disks", "mouse" and "event".
  398. * This identifies the device type and carries type-specific
  399. * information, equivalent to the kobj_type of a kobject.
  400. * If "name" is specified, the uevent will contain it in
  401. * the DEVTYPE variable.
  402. */
  403. struct device_type {
  404. const char *name;
  405. const struct attribute_group **groups;
  406. int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  407. char *(*devnode)(struct device *dev, umode_t *mode);
  408. void (*release)(struct device *dev);
  409. const struct dev_pm_ops *pm;
  410. };
  411. /* interface for exporting device attributes */
  412. struct device_attribute {
  413. struct attribute attr;
  414. ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  415. char *buf);
  416. ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  417. const char *buf, size_t count);
  418. };
  419. struct dev_ext_attribute {
  420. struct device_attribute attr;
  421. void *var;
  422. };
  423. ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
  424. char *buf);
  425. ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
  426. const char *buf, size_t count);
  427. ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
  428. char *buf);
  429. ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
  430. const char *buf, size_t count);
  431. #define DEVICE_ATTR(_name, _mode, _show, _store) \
  432. struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
  433. #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
  434. struct dev_ext_attribute dev_attr_##_name = \
  435. { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
  436. #define DEVICE_INT_ATTR(_name, _mode, _var) \
  437. struct dev_ext_attribute dev_attr_##_name = \
  438. { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
  439. extern int device_create_file(struct device *device,
  440. const struct device_attribute *entry);
  441. extern void device_remove_file(struct device *dev,
  442. const struct device_attribute *attr);
  443. extern int __must_check device_create_bin_file(struct device *dev,
  444. const struct bin_attribute *attr);
  445. extern void device_remove_bin_file(struct device *dev,
  446. const struct bin_attribute *attr);
  447. extern int device_schedule_callback_owner(struct device *dev,
  448. void (*func)(struct device *dev), struct module *owner);
  449. /* This is a macro to avoid include problems with THIS_MODULE */
  450. #define device_schedule_callback(dev, func) \
  451. device_schedule_callback_owner(dev, func, THIS_MODULE)
  452. /* device resource management */
  453. typedef void (*dr_release_t)(struct device *dev, void *res);
  454. typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
  455. #ifdef CONFIG_DEBUG_DEVRES
  456. extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
  457. const char *name);
  458. #define devres_alloc(release, size, gfp) \
  459. __devres_alloc(release, size, gfp, #release)
  460. #else
  461. extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
  462. #endif
  463. extern void devres_free(void *res);
  464. extern void devres_add(struct device *dev, void *res);
  465. extern void *devres_find(struct device *dev, dr_release_t release,
  466. dr_match_t match, void *match_data);
  467. extern void *devres_get(struct device *dev, void *new_res,
  468. dr_match_t match, void *match_data);
  469. extern void *devres_remove(struct device *dev, dr_release_t release,
  470. dr_match_t match, void *match_data);
  471. extern int devres_destroy(struct device *dev, dr_release_t release,
  472. dr_match_t match, void *match_data);
  473. /* devres group */
  474. extern void * __must_check devres_open_group(struct device *dev, void *id,
  475. gfp_t gfp);
  476. extern void devres_close_group(struct device *dev, void *id);
  477. extern void devres_remove_group(struct device *dev, void *id);
  478. extern int devres_release_group(struct device *dev, void *id);
  479. /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
  480. extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
  481. extern void devm_kfree(struct device *dev, void *p);
  482. void __iomem *devm_request_and_ioremap(struct device *dev,
  483. struct resource *res);
  484. struct device_dma_parameters {
  485. /*
  486. * a low level driver may set these to teach IOMMU code about
  487. * sg limitations.
  488. */
  489. unsigned int max_segment_size;
  490. unsigned long segment_boundary_mask;
  491. };
  492. /**
  493. * struct device - The basic device structure
  494. * @parent: The device's "parent" device, the device to which it is attached.
  495. * In most cases, a parent device is some sort of bus or host
  496. * controller. If parent is NULL, the device, is a top-level device,
  497. * which is not usually what you want.
  498. * @p: Holds the private data of the driver core portions of the device.
  499. * See the comment of the struct device_private for detail.
  500. * @kobj: A top-level, abstract class from which other classes are derived.
  501. * @init_name: Initial name of the device.
  502. * @type: The type of device.
  503. * This identifies the device type and carries type-specific
  504. * information.
  505. * @mutex: Mutex to synchronize calls to its driver.
  506. * @bus: Type of bus device is on.
  507. * @driver: Which driver has allocated this
  508. * @platform_data: Platform data specific to the device.
  509. * Example: For devices on custom boards, as typical of embedded
  510. * and SOC based hardware, Linux often uses platform_data to point
  511. * to board-specific structures describing devices and how they
  512. * are wired. That can include what ports are available, chip
  513. * variants, which GPIO pins act in what additional roles, and so
  514. * on. This shrinks the "Board Support Packages" (BSPs) and
  515. * minimizes board-specific #ifdefs in drivers.
  516. * @power: For device power management.
  517. * See Documentation/power/devices.txt for details.
  518. * @pm_domain: Provide callbacks that are executed during system suspend,
  519. * hibernation, system resume and during runtime PM transitions
  520. * along with subsystem-level and driver-level callbacks.
  521. * @numa_node: NUMA node this device is close to.
  522. * @dma_mask: Dma mask (if dma'ble device).
  523. * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
  524. * hardware supports 64-bit addresses for consistent allocations
  525. * such descriptors.
  526. * @dma_parms: A low level driver may set these to teach IOMMU code about
  527. * segment limitations.
  528. * @dma_pools: Dma pools (if dma'ble device).
  529. * @dma_mem: Internal for coherent mem override.
  530. * @archdata: For arch-specific additions.
  531. * @of_node: Associated device tree node.
  532. * @devt: For creating the sysfs "dev".
  533. * @id: device instance
  534. * @devres_lock: Spinlock to protect the resource of the device.
  535. * @devres_head: The resources list of the device.
  536. * @knode_class: The node used to add the device to the class list.
  537. * @class: The class of the device.
  538. * @groups: Optional attribute groups.
  539. * @release: Callback to free the device after all references have
  540. * gone away. This should be set by the allocator of the
  541. * device (i.e. the bus driver that discovered the device).
  542. *
  543. * At the lowest level, every device in a Linux system is represented by an
  544. * instance of struct device. The device structure contains the information
  545. * that the device model core needs to model the system. Most subsystems,
  546. * however, track additional information about the devices they host. As a
  547. * result, it is rare for devices to be represented by bare device structures;
  548. * instead, that structure, like kobject structures, is usually embedded within
  549. * a higher-level representation of the device.
  550. */
  551. struct device {
  552. struct device *parent;
  553. struct device_private *p;
  554. struct kobject kobj;
  555. const char *init_name; /* initial name of the device */
  556. const struct device_type *type;
  557. struct mutex mutex; /* mutex to synchronize calls to
  558. * its driver.
  559. */
  560. struct bus_type *bus; /* type of bus device is on */
  561. struct device_driver *driver; /* which driver has allocated this
  562. device */
  563. void *platform_data; /* Platform specific data, device
  564. core doesn't touch it */
  565. struct dev_pm_info power;
  566. struct dev_pm_domain *pm_domain;
  567. #ifdef CONFIG_NUMA
  568. int numa_node; /* NUMA node this device is close to */
  569. #endif
  570. u64 *dma_mask; /* dma mask (if dma'able device) */
  571. u64 coherent_dma_mask;/* Like dma_mask, but for
  572. alloc_coherent mappings as
  573. not all hardware supports
  574. 64 bit addresses for consistent
  575. allocations such descriptors. */
  576. struct device_dma_parameters *dma_parms;
  577. struct list_head dma_pools; /* dma pools (if dma'ble) */
  578. struct dma_coherent_mem *dma_mem; /* internal for coherent mem
  579. override */
  580. /* arch specific additions */
  581. struct dev_archdata archdata;
  582. struct device_node *of_node; /* associated device tree node */
  583. dev_t devt; /* dev_t, creates the sysfs "dev" */
  584. u32 id; /* device instance */
  585. spinlock_t devres_lock;
  586. struct list_head devres_head;
  587. struct klist_node knode_class;
  588. struct class *class;
  589. const struct attribute_group **groups; /* optional groups */
  590. void (*release)(struct device *dev);
  591. };
  592. /* Get the wakeup routines, which depend on struct device */
  593. #include <linux/pm_wakeup.h>
  594. static inline const char *dev_name(const struct device *dev)
  595. {
  596. /* Use the init name until the kobject becomes available */
  597. if (dev->init_name)
  598. return dev->init_name;
  599. return kobject_name(&dev->kobj);
  600. }
  601. extern __printf(2, 3)
  602. int dev_set_name(struct device *dev, const char *name, ...);
  603. #ifdef CONFIG_NUMA
  604. static inline int dev_to_node(struct device *dev)
  605. {
  606. return dev->numa_node;
  607. }
  608. static inline void set_dev_node(struct device *dev, int node)
  609. {
  610. dev->numa_node = node;
  611. }
  612. #else
  613. static inline int dev_to_node(struct device *dev)
  614. {
  615. return -1;
  616. }
  617. static inline void set_dev_node(struct device *dev, int node)
  618. {
  619. }
  620. #endif
  621. static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
  622. {
  623. return dev ? dev->power.subsys_data : NULL;
  624. }
  625. static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
  626. {
  627. return dev->kobj.uevent_suppress;
  628. }
  629. static inline void dev_set_uevent_suppress(struct device *dev, int val)
  630. {
  631. dev->kobj.uevent_suppress = val;
  632. }
  633. static inline int device_is_registered(struct device *dev)
  634. {
  635. return dev->kobj.state_in_sysfs;
  636. }
  637. static inline void device_enable_async_suspend(struct device *dev)
  638. {
  639. if (!dev->power.is_prepared)
  640. dev->power.async_suspend = true;
  641. }
  642. static inline void device_disable_async_suspend(struct device *dev)
  643. {
  644. if (!dev->power.is_prepared)
  645. dev->power.async_suspend = false;
  646. }
  647. static inline bool device_async_suspend_enabled(struct device *dev)
  648. {
  649. return !!dev->power.async_suspend;
  650. }
  651. static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
  652. {
  653. dev->power.ignore_children = enable;
  654. }
  655. static inline void device_lock(struct device *dev)
  656. {
  657. mutex_lock(&dev->mutex);
  658. }
  659. static inline int device_trylock(struct device *dev)
  660. {
  661. return mutex_trylock(&dev->mutex);
  662. }
  663. static inline void device_unlock(struct device *dev)
  664. {
  665. mutex_unlock(&dev->mutex);
  666. }
  667. void driver_init(void);
  668. /*
  669. * High level routines for use by the bus drivers
  670. */
  671. extern int __must_check device_register(struct device *dev);
  672. extern void device_unregister(struct device *dev);
  673. extern void device_initialize(struct device *dev);
  674. extern int __must_check device_add(struct device *dev);
  675. extern void device_del(struct device *dev);
  676. extern int device_for_each_child(struct device *dev, void *data,
  677. int (*fn)(struct device *dev, void *data));
  678. extern struct device *device_find_child(struct device *dev, void *data,
  679. int (*match)(struct device *dev, void *data));
  680. extern int device_rename(struct device *dev, const char *new_name);
  681. extern int device_move(struct device *dev, struct device *new_parent,
  682. enum dpm_order dpm_order);
  683. extern const char *device_get_devnode(struct device *dev,
  684. umode_t *mode, const char **tmp);
  685. extern void *dev_get_drvdata(const struct device *dev);
  686. extern int dev_set_drvdata(struct device *dev, void *data);
  687. /*
  688. * Root device objects for grouping under /sys/devices
  689. */
  690. extern struct device *__root_device_register(const char *name,
  691. struct module *owner);
  692. /*
  693. * This is a macro to avoid include problems with THIS_MODULE,
  694. * just as per what is done for device_schedule_callback() above.
  695. */
  696. #define root_device_register(name) \
  697. __root_device_register(name, THIS_MODULE)
  698. extern void root_device_unregister(struct device *root);
  699. static inline void *dev_get_platdata(const struct device *dev)
  700. {
  701. return dev->platform_data;
  702. }
  703. /*
  704. * Manual binding of a device to driver. See drivers/base/bus.c
  705. * for information on use.
  706. */
  707. extern int __must_check device_bind_driver(struct device *dev);
  708. extern void device_release_driver(struct device *dev);
  709. extern int __must_check device_attach(struct device *dev);
  710. extern int __must_check driver_attach(struct device_driver *drv);
  711. extern int __must_check device_reprobe(struct device *dev);
  712. /*
  713. * Easy functions for dynamically creating devices on the fly
  714. */
  715. extern struct device *device_create_vargs(struct class *cls,
  716. struct device *parent,
  717. dev_t devt,
  718. void *drvdata,
  719. const char *fmt,
  720. va_list vargs);
  721. extern __printf(5, 6)
  722. struct device *device_create(struct class *cls, struct device *parent,
  723. dev_t devt, void *drvdata,
  724. const char *fmt, ...);
  725. extern void device_destroy(struct class *cls, dev_t devt);
  726. /*
  727. * Platform "fixup" functions - allow the platform to have their say
  728. * about devices and actions that the general device layer doesn't
  729. * know about.
  730. */
  731. /* Notify platform of device discovery */
  732. extern int (*platform_notify)(struct device *dev);
  733. extern int (*platform_notify_remove)(struct device *dev);
  734. /*
  735. * get_device - atomically increment the reference count for the device.
  736. *
  737. */
  738. extern struct device *get_device(struct device *dev);
  739. extern void put_device(struct device *dev);
  740. extern void wait_for_device_probe(void);
  741. #ifdef CONFIG_DEVTMPFS
  742. extern int devtmpfs_create_node(struct device *dev);
  743. extern int devtmpfs_delete_node(struct device *dev);
  744. extern int devtmpfs_mount(const char *mntdir);
  745. #else
  746. static inline int devtmpfs_create_node(struct device *dev) { return 0; }
  747. static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
  748. static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
  749. #endif
  750. /* drivers/base/power/shutdown.c */
  751. extern void device_shutdown(void);
  752. /* debugging and troubleshooting/diagnostic helpers. */
  753. extern const char *dev_driver_string(const struct device *dev);
  754. #ifdef CONFIG_PRINTK
  755. extern int __dev_printk(const char *level, const struct device *dev,
  756. struct va_format *vaf);
  757. extern __printf(3, 4)
  758. int dev_printk(const char *level, const struct device *dev,
  759. const char *fmt, ...)
  760. ;
  761. extern __printf(2, 3)
  762. int dev_emerg(const struct device *dev, const char *fmt, ...);
  763. extern __printf(2, 3)
  764. int dev_alert(const struct device *dev, const char *fmt, ...);
  765. extern __printf(2, 3)
  766. int dev_crit(const struct device *dev, const char *fmt, ...);
  767. extern __printf(2, 3)
  768. int dev_err(const struct device *dev, const char *fmt, ...);
  769. extern __printf(2, 3)
  770. int dev_warn(const struct device *dev, const char *fmt, ...);
  771. extern __printf(2, 3)
  772. int dev_notice(const struct device *dev, const char *fmt, ...);
  773. extern __printf(2, 3)
  774. int _dev_info(const struct device *dev, const char *fmt, ...);
  775. #else
  776. static inline int __dev_printk(const char *level, const struct device *dev,
  777. struct va_format *vaf)
  778. { return 0; }
  779. static inline __printf(3, 4)
  780. int dev_printk(const char *level, const struct device *dev,
  781. const char *fmt, ...)
  782. { return 0; }
  783. static inline __printf(2, 3)
  784. int dev_emerg(const struct device *dev, const char *fmt, ...)
  785. { return 0; }
  786. static inline __printf(2, 3)
  787. int dev_crit(const struct device *dev, const char *fmt, ...)
  788. { return 0; }
  789. static inline __printf(2, 3)
  790. int dev_alert(const struct device *dev, const char *fmt, ...)
  791. { return 0; }
  792. static inline __printf(2, 3)
  793. int dev_err(const struct device *dev, const char *fmt, ...)
  794. { return 0; }
  795. static inline __printf(2, 3)
  796. int dev_warn(const struct device *dev, const char *fmt, ...)
  797. { return 0; }
  798. static inline __printf(2, 3)
  799. int dev_notice(const struct device *dev, const char *fmt, ...)
  800. { return 0; }
  801. static inline __printf(2, 3)
  802. int _dev_info(const struct device *dev, const char *fmt, ...)
  803. { return 0; }
  804. #endif
  805. /*
  806. * Stupid hackaround for existing uses of non-printk uses dev_info
  807. *
  808. * Note that the definition of dev_info below is actually _dev_info
  809. * and a macro is used to avoid redefining dev_info
  810. */
  811. #define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
  812. #if defined(CONFIG_DYNAMIC_DEBUG)
  813. #define dev_dbg(dev, format, ...) \
  814. do { \
  815. dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
  816. } while (0)
  817. #elif defined(DEBUG)
  818. #define dev_dbg(dev, format, arg...) \
  819. dev_printk(KERN_DEBUG, dev, format, ##arg)
  820. #else
  821. #define dev_dbg(dev, format, arg...) \
  822. ({ \
  823. if (0) \
  824. dev_printk(KERN_DEBUG, dev, format, ##arg); \
  825. 0; \
  826. })
  827. #endif
  828. #ifdef VERBOSE_DEBUG
  829. #define dev_vdbg dev_dbg
  830. #else
  831. #define dev_vdbg(dev, format, arg...) \
  832. ({ \
  833. if (0) \
  834. dev_printk(KERN_DEBUG, dev, format, ##arg); \
  835. 0; \
  836. })
  837. #endif
  838. /*
  839. * dev_WARN*() acts like dev_printk(), but with the key difference
  840. * of using a WARN/WARN_ON to get the message out, including the
  841. * file/line information and a backtrace.
  842. */
  843. #define dev_WARN(dev, format, arg...) \
  844. WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg);
  845. #define dev_WARN_ONCE(dev, condition, format, arg...) \
  846. WARN_ONCE(condition, "Device %s\n" format, \
  847. dev_driver_string(dev), ## arg)
  848. /* Create alias, so I can be autoloaded. */
  849. #define MODULE_ALIAS_CHARDEV(major,minor) \
  850. MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
  851. #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
  852. MODULE_ALIAS("char-major-" __stringify(major) "-*")
  853. #ifdef CONFIG_SYSFS_DEPRECATED
  854. extern long sysfs_deprecated;
  855. #else
  856. #define sysfs_deprecated 0
  857. #endif
  858. /**
  859. * module_driver() - Helper macro for drivers that don't do anything
  860. * special in module init/exit. This eliminates a lot of boilerplate.
  861. * Each module may only use this macro once, and calling it replaces
  862. * module_init() and module_exit().
  863. *
  864. * @__driver: driver name
  865. * @__register: register function for this driver type
  866. * @__unregister: unregister function for this driver type
  867. *
  868. * Use this macro to construct bus specific macros for registering
  869. * drivers, and do not use it on its own.
  870. */
  871. #define module_driver(__driver, __register, __unregister) \
  872. static int __init __driver##_init(void) \
  873. { \
  874. return __register(&(__driver)); \
  875. } \
  876. module_init(__driver##_init); \
  877. static void __exit __driver##_exit(void) \
  878. { \
  879. __unregister(&(__driver)); \
  880. } \
  881. module_exit(__driver##_exit);
  882. #endif /* _DEVICE_H_ */