device.h 38 KB

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