device.h 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. #define DEVICE_ATTR(_name, _mode, _show, _store) \
  433. struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
  434. #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
  435. struct dev_ext_attribute dev_attr_##_name = \
  436. { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
  437. #define DEVICE_INT_ATTR(_name, _mode, _var) \
  438. struct dev_ext_attribute dev_attr_##_name = \
  439. { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
  440. #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
  441. struct device_attribute dev_attr_##_name = \
  442. __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
  443. extern int device_create_file(struct device *device,
  444. const struct device_attribute *entry);
  445. extern void device_remove_file(struct device *dev,
  446. const struct device_attribute *attr);
  447. extern int __must_check device_create_bin_file(struct device *dev,
  448. const struct bin_attribute *attr);
  449. extern void device_remove_bin_file(struct device *dev,
  450. const struct bin_attribute *attr);
  451. extern int device_schedule_callback_owner(struct device *dev,
  452. void (*func)(struct device *dev), struct module *owner);
  453. /* This is a macro to avoid include problems with THIS_MODULE */
  454. #define device_schedule_callback(dev, func) \
  455. device_schedule_callback_owner(dev, func, THIS_MODULE)
  456. /* device resource management */
  457. typedef void (*dr_release_t)(struct device *dev, void *res);
  458. typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
  459. #ifdef CONFIG_DEBUG_DEVRES
  460. extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
  461. const char *name);
  462. #define devres_alloc(release, size, gfp) \
  463. __devres_alloc(release, size, gfp, #release)
  464. #else
  465. extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
  466. #endif
  467. extern void devres_for_each_res(struct device *dev, dr_release_t release,
  468. dr_match_t match, void *match_data,
  469. void (*fn)(struct device *, void *, void *),
  470. void *data);
  471. extern void devres_free(void *res);
  472. extern void devres_add(struct device *dev, void *res);
  473. extern void *devres_find(struct device *dev, dr_release_t release,
  474. dr_match_t match, void *match_data);
  475. extern void *devres_get(struct device *dev, void *new_res,
  476. dr_match_t match, void *match_data);
  477. extern void *devres_remove(struct device *dev, dr_release_t release,
  478. dr_match_t match, void *match_data);
  479. extern int devres_destroy(struct device *dev, dr_release_t release,
  480. dr_match_t match, void *match_data);
  481. extern int devres_release(struct device *dev, dr_release_t release,
  482. dr_match_t match, void *match_data);
  483. /* devres group */
  484. extern void * __must_check devres_open_group(struct device *dev, void *id,
  485. gfp_t gfp);
  486. extern void devres_close_group(struct device *dev, void *id);
  487. extern void devres_remove_group(struct device *dev, void *id);
  488. extern int devres_release_group(struct device *dev, void *id);
  489. /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
  490. extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
  491. extern void devm_kfree(struct device *dev, void *p);
  492. void __iomem *devm_request_and_ioremap(struct device *dev,
  493. struct resource *res);
  494. struct device_dma_parameters {
  495. /*
  496. * a low level driver may set these to teach IOMMU code about
  497. * sg limitations.
  498. */
  499. unsigned int max_segment_size;
  500. unsigned long segment_boundary_mask;
  501. };
  502. /**
  503. * struct device - The basic device structure
  504. * @parent: The device's "parent" device, the device to which it is attached.
  505. * In most cases, a parent device is some sort of bus or host
  506. * controller. If parent is NULL, the device, is a top-level device,
  507. * which is not usually what you want.
  508. * @p: Holds the private data of the driver core portions of the device.
  509. * See the comment of the struct device_private for detail.
  510. * @kobj: A top-level, abstract class from which other classes are derived.
  511. * @init_name: Initial name of the device.
  512. * @type: The type of device.
  513. * This identifies the device type and carries type-specific
  514. * information.
  515. * @mutex: Mutex to synchronize calls to its driver.
  516. * @bus: Type of bus device is on.
  517. * @driver: Which driver has allocated this
  518. * @platform_data: Platform data specific to the device.
  519. * Example: For devices on custom boards, as typical of embedded
  520. * and SOC based hardware, Linux often uses platform_data to point
  521. * to board-specific structures describing devices and how they
  522. * are wired. That can include what ports are available, chip
  523. * variants, which GPIO pins act in what additional roles, and so
  524. * on. This shrinks the "Board Support Packages" (BSPs) and
  525. * minimizes board-specific #ifdefs in drivers.
  526. * @power: For device power management.
  527. * See Documentation/power/devices.txt for details.
  528. * @pm_domain: Provide callbacks that are executed during system suspend,
  529. * hibernation, system resume and during runtime PM transitions
  530. * along with subsystem-level and driver-level callbacks.
  531. * @numa_node: NUMA node this device is close to.
  532. * @dma_mask: Dma mask (if dma'ble device).
  533. * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
  534. * hardware supports 64-bit addresses for consistent allocations
  535. * such descriptors.
  536. * @dma_parms: A low level driver may set these to teach IOMMU code about
  537. * segment limitations.
  538. * @dma_pools: Dma pools (if dma'ble device).
  539. * @dma_mem: Internal for coherent mem override.
  540. * @archdata: For arch-specific additions.
  541. * @of_node: Associated device tree node.
  542. * @acpi_handle: Associated ACPI device node's namespace handle.
  543. * @devt: For creating the sysfs "dev".
  544. * @id: device instance
  545. * @devres_lock: Spinlock to protect the resource of the device.
  546. * @devres_head: The resources list of the device.
  547. * @knode_class: The node used to add the device to the class list.
  548. * @class: The class of the device.
  549. * @groups: Optional attribute groups.
  550. * @release: Callback to free the device after all references have
  551. * gone away. This should be set by the allocator of the
  552. * device (i.e. the bus driver that discovered the device).
  553. *
  554. * At the lowest level, every device in a Linux system is represented by an
  555. * instance of struct device. The device structure contains the information
  556. * that the device model core needs to model the system. Most subsystems,
  557. * however, track additional information about the devices they host. As a
  558. * result, it is rare for devices to be represented by bare device structures;
  559. * instead, that structure, like kobject structures, is usually embedded within
  560. * a higher-level representation of the device.
  561. */
  562. struct device {
  563. struct device *parent;
  564. struct device_private *p;
  565. struct kobject kobj;
  566. const char *init_name; /* initial name of the device */
  567. const struct device_type *type;
  568. struct mutex mutex; /* mutex to synchronize calls to
  569. * its driver.
  570. */
  571. struct bus_type *bus; /* type of bus device is on */
  572. struct device_driver *driver; /* which driver has allocated this
  573. device */
  574. void *platform_data; /* Platform specific data, device
  575. core doesn't touch it */
  576. struct dev_pm_info power;
  577. struct dev_pm_domain *pm_domain;
  578. #ifdef CONFIG_NUMA
  579. int numa_node; /* NUMA node this device is close to */
  580. #endif
  581. u64 *dma_mask; /* dma mask (if dma'able device) */
  582. u64 coherent_dma_mask;/* Like dma_mask, but for
  583. alloc_coherent mappings as
  584. not all hardware supports
  585. 64 bit addresses for consistent
  586. allocations such descriptors. */
  587. struct device_dma_parameters *dma_parms;
  588. struct list_head dma_pools; /* dma pools (if dma'ble) */
  589. struct dma_coherent_mem *dma_mem; /* internal for coherent mem
  590. override */
  591. #ifdef CONFIG_CMA
  592. struct cma *cma_area; /* contiguous memory area for dma
  593. allocations */
  594. #endif
  595. /* arch specific additions */
  596. struct dev_archdata archdata;
  597. struct device_node *of_node; /* associated device tree node */
  598. void *acpi_handle; /* associated ACPI device node */
  599. dev_t devt; /* dev_t, creates the sysfs "dev" */
  600. u32 id; /* device instance */
  601. spinlock_t devres_lock;
  602. struct list_head devres_head;
  603. struct klist_node knode_class;
  604. struct class *class;
  605. const struct attribute_group **groups; /* optional groups */
  606. void (*release)(struct device *dev);
  607. struct iommu_group *iommu_group;
  608. };
  609. static inline struct device *kobj_to_dev(struct kobject *kobj)
  610. {
  611. return container_of(kobj, struct device, kobj);
  612. }
  613. /* Get the wakeup routines, which depend on struct device */
  614. #include <linux/pm_wakeup.h>
  615. static inline const char *dev_name(const struct device *dev)
  616. {
  617. /* Use the init name until the kobject becomes available */
  618. if (dev->init_name)
  619. return dev->init_name;
  620. return kobject_name(&dev->kobj);
  621. }
  622. extern __printf(2, 3)
  623. int dev_set_name(struct device *dev, const char *name, ...);
  624. #ifdef CONFIG_NUMA
  625. static inline int dev_to_node(struct device *dev)
  626. {
  627. return dev->numa_node;
  628. }
  629. static inline void set_dev_node(struct device *dev, int node)
  630. {
  631. dev->numa_node = node;
  632. }
  633. #else
  634. static inline int dev_to_node(struct device *dev)
  635. {
  636. return -1;
  637. }
  638. static inline void set_dev_node(struct device *dev, int node)
  639. {
  640. }
  641. #endif
  642. static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
  643. {
  644. return dev ? dev->power.subsys_data : NULL;
  645. }
  646. static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
  647. {
  648. return dev->kobj.uevent_suppress;
  649. }
  650. static inline void dev_set_uevent_suppress(struct device *dev, int val)
  651. {
  652. dev->kobj.uevent_suppress = val;
  653. }
  654. static inline int device_is_registered(struct device *dev)
  655. {
  656. return dev->kobj.state_in_sysfs;
  657. }
  658. static inline void device_enable_async_suspend(struct device *dev)
  659. {
  660. if (!dev->power.is_prepared)
  661. dev->power.async_suspend = true;
  662. }
  663. static inline void device_disable_async_suspend(struct device *dev)
  664. {
  665. if (!dev->power.is_prepared)
  666. dev->power.async_suspend = false;
  667. }
  668. static inline bool device_async_suspend_enabled(struct device *dev)
  669. {
  670. return !!dev->power.async_suspend;
  671. }
  672. static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
  673. {
  674. dev->power.ignore_children = enable;
  675. }
  676. static inline void dev_pm_syscore_device(struct device *dev, bool val)
  677. {
  678. #ifdef CONFIG_PM_SLEEP
  679. dev->power.syscore = val;
  680. #endif
  681. }
  682. static inline void device_lock(struct device *dev)
  683. {
  684. mutex_lock(&dev->mutex);
  685. }
  686. static inline int device_trylock(struct device *dev)
  687. {
  688. return mutex_trylock(&dev->mutex);
  689. }
  690. static inline void device_unlock(struct device *dev)
  691. {
  692. mutex_unlock(&dev->mutex);
  693. }
  694. void driver_init(void);
  695. /*
  696. * High level routines for use by the bus drivers
  697. */
  698. extern int __must_check device_register(struct device *dev);
  699. extern void device_unregister(struct device *dev);
  700. extern void device_initialize(struct device *dev);
  701. extern int __must_check device_add(struct device *dev);
  702. extern void device_del(struct device *dev);
  703. extern int device_for_each_child(struct device *dev, void *data,
  704. int (*fn)(struct device *dev, void *data));
  705. extern struct device *device_find_child(struct device *dev, void *data,
  706. int (*match)(struct device *dev, void *data));
  707. extern int device_rename(struct device *dev, const char *new_name);
  708. extern int device_move(struct device *dev, struct device *new_parent,
  709. enum dpm_order dpm_order);
  710. extern const char *device_get_devnode(struct device *dev,
  711. umode_t *mode, const char **tmp);
  712. extern void *dev_get_drvdata(const struct device *dev);
  713. extern int dev_set_drvdata(struct device *dev, void *data);
  714. /*
  715. * Root device objects for grouping under /sys/devices
  716. */
  717. extern struct device *__root_device_register(const char *name,
  718. struct module *owner);
  719. /*
  720. * This is a macro to avoid include problems with THIS_MODULE,
  721. * just as per what is done for device_schedule_callback() above.
  722. */
  723. #define root_device_register(name) \
  724. __root_device_register(name, THIS_MODULE)
  725. extern void root_device_unregister(struct device *root);
  726. static inline void *dev_get_platdata(const struct device *dev)
  727. {
  728. return dev->platform_data;
  729. }
  730. /*
  731. * Manual binding of a device to driver. See drivers/base/bus.c
  732. * for information on use.
  733. */
  734. extern int __must_check device_bind_driver(struct device *dev);
  735. extern void device_release_driver(struct device *dev);
  736. extern int __must_check device_attach(struct device *dev);
  737. extern int __must_check driver_attach(struct device_driver *drv);
  738. extern int __must_check device_reprobe(struct device *dev);
  739. /*
  740. * Easy functions for dynamically creating devices on the fly
  741. */
  742. extern struct device *device_create_vargs(struct class *cls,
  743. struct device *parent,
  744. dev_t devt,
  745. void *drvdata,
  746. const char *fmt,
  747. va_list vargs);
  748. extern __printf(5, 6)
  749. struct device *device_create(struct class *cls, struct device *parent,
  750. dev_t devt, void *drvdata,
  751. const char *fmt, ...);
  752. extern void device_destroy(struct class *cls, dev_t devt);
  753. /*
  754. * Platform "fixup" functions - allow the platform to have their say
  755. * about devices and actions that the general device layer doesn't
  756. * know about.
  757. */
  758. /* Notify platform of device discovery */
  759. extern int (*platform_notify)(struct device *dev);
  760. extern int (*platform_notify_remove)(struct device *dev);
  761. /*
  762. * get_device - atomically increment the reference count for the device.
  763. *
  764. */
  765. extern struct device *get_device(struct device *dev);
  766. extern void put_device(struct device *dev);
  767. #ifdef CONFIG_DEVTMPFS
  768. extern int devtmpfs_create_node(struct device *dev);
  769. extern int devtmpfs_delete_node(struct device *dev);
  770. extern int devtmpfs_mount(const char *mntdir);
  771. #else
  772. static inline int devtmpfs_create_node(struct device *dev) { return 0; }
  773. static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
  774. static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
  775. #endif
  776. /* drivers/base/power/shutdown.c */
  777. extern void device_shutdown(void);
  778. /* debugging and troubleshooting/diagnostic helpers. */
  779. extern const char *dev_driver_string(const struct device *dev);
  780. #ifdef CONFIG_PRINTK
  781. extern __printf(3, 0)
  782. int dev_vprintk_emit(int level, const struct device *dev,
  783. const char *fmt, va_list args);
  784. extern __printf(3, 4)
  785. int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...);
  786. extern __printf(3, 4)
  787. int dev_printk(const char *level, const struct device *dev,
  788. const char *fmt, ...);
  789. extern __printf(2, 3)
  790. int dev_emerg(const struct device *dev, const char *fmt, ...);
  791. extern __printf(2, 3)
  792. int dev_alert(const struct device *dev, const char *fmt, ...);
  793. extern __printf(2, 3)
  794. int dev_crit(const struct device *dev, const char *fmt, ...);
  795. extern __printf(2, 3)
  796. int dev_err(const struct device *dev, const char *fmt, ...);
  797. extern __printf(2, 3)
  798. int dev_warn(const struct device *dev, const char *fmt, ...);
  799. extern __printf(2, 3)
  800. int dev_notice(const struct device *dev, const char *fmt, ...);
  801. extern __printf(2, 3)
  802. int _dev_info(const struct device *dev, const char *fmt, ...);
  803. #else
  804. static inline __printf(3, 0)
  805. int dev_vprintk_emit(int level, const struct device *dev,
  806. const char *fmt, va_list args)
  807. { return 0; }
  808. static inline __printf(3, 4)
  809. int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
  810. { return 0; }
  811. static inline int __dev_printk(const char *level, const struct device *dev,
  812. struct va_format *vaf)
  813. { return 0; }
  814. static inline __printf(3, 4)
  815. int dev_printk(const char *level, const struct device *dev,
  816. const char *fmt, ...)
  817. { return 0; }
  818. static inline __printf(2, 3)
  819. int dev_emerg(const struct device *dev, const char *fmt, ...)
  820. { return 0; }
  821. static inline __printf(2, 3)
  822. int dev_crit(const struct device *dev, const char *fmt, ...)
  823. { return 0; }
  824. static inline __printf(2, 3)
  825. int dev_alert(const struct device *dev, const char *fmt, ...)
  826. { return 0; }
  827. static inline __printf(2, 3)
  828. int dev_err(const struct device *dev, const char *fmt, ...)
  829. { return 0; }
  830. static inline __printf(2, 3)
  831. int dev_warn(const struct device *dev, const char *fmt, ...)
  832. { return 0; }
  833. static inline __printf(2, 3)
  834. int dev_notice(const struct device *dev, const char *fmt, ...)
  835. { return 0; }
  836. static inline __printf(2, 3)
  837. int _dev_info(const struct device *dev, const char *fmt, ...)
  838. { return 0; }
  839. #endif
  840. /*
  841. * Stupid hackaround for existing uses of non-printk uses dev_info
  842. *
  843. * Note that the definition of dev_info below is actually _dev_info
  844. * and a macro is used to avoid redefining dev_info
  845. */
  846. #define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
  847. #if defined(CONFIG_DYNAMIC_DEBUG)
  848. #define dev_dbg(dev, format, ...) \
  849. do { \
  850. dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
  851. } while (0)
  852. #elif defined(DEBUG)
  853. #define dev_dbg(dev, format, arg...) \
  854. dev_printk(KERN_DEBUG, dev, format, ##arg)
  855. #else
  856. #define dev_dbg(dev, format, arg...) \
  857. ({ \
  858. if (0) \
  859. dev_printk(KERN_DEBUG, dev, format, ##arg); \
  860. 0; \
  861. })
  862. #endif
  863. #define dev_level_ratelimited(dev_level, dev, fmt, ...) \
  864. do { \
  865. static DEFINE_RATELIMIT_STATE(_rs, \
  866. DEFAULT_RATELIMIT_INTERVAL, \
  867. DEFAULT_RATELIMIT_BURST); \
  868. if (__ratelimit(&_rs)) \
  869. dev_level(dev, fmt, ##__VA_ARGS__); \
  870. } while (0)
  871. #define dev_emerg_ratelimited(dev, fmt, ...) \
  872. dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
  873. #define dev_alert_ratelimited(dev, fmt, ...) \
  874. dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
  875. #define dev_crit_ratelimited(dev, fmt, ...) \
  876. dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
  877. #define dev_err_ratelimited(dev, fmt, ...) \
  878. dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
  879. #define dev_warn_ratelimited(dev, fmt, ...) \
  880. dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
  881. #define dev_notice_ratelimited(dev, fmt, ...) \
  882. dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
  883. #define dev_info_ratelimited(dev, fmt, ...) \
  884. dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
  885. #if defined(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
  886. #define dev_dbg_ratelimited(dev, fmt, ...) \
  887. do { \
  888. static DEFINE_RATELIMIT_STATE(_rs, \
  889. DEFAULT_RATELIMIT_INTERVAL, \
  890. DEFAULT_RATELIMIT_BURST); \
  891. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  892. if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \
  893. __ratelimit(&_rs)) \
  894. __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
  895. ##__VA_ARGS__); \
  896. } while (0)
  897. #else
  898. #define dev_dbg_ratelimited(dev, fmt, ...) \
  899. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  900. #endif
  901. #ifdef VERBOSE_DEBUG
  902. #define dev_vdbg dev_dbg
  903. #else
  904. #define dev_vdbg(dev, format, arg...) \
  905. ({ \
  906. if (0) \
  907. dev_printk(KERN_DEBUG, dev, format, ##arg); \
  908. 0; \
  909. })
  910. #endif
  911. /*
  912. * dev_WARN*() acts like dev_printk(), but with the key difference
  913. * of using a WARN/WARN_ON to get the message out, including the
  914. * file/line information and a backtrace.
  915. */
  916. #define dev_WARN(dev, format, arg...) \
  917. WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg);
  918. #define dev_WARN_ONCE(dev, condition, format, arg...) \
  919. WARN_ONCE(condition, "Device %s\n" format, \
  920. dev_driver_string(dev), ## arg)
  921. /* Create alias, so I can be autoloaded. */
  922. #define MODULE_ALIAS_CHARDEV(major,minor) \
  923. MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
  924. #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
  925. MODULE_ALIAS("char-major-" __stringify(major) "-*")
  926. #ifdef CONFIG_SYSFS_DEPRECATED
  927. extern long sysfs_deprecated;
  928. #else
  929. #define sysfs_deprecated 0
  930. #endif
  931. /**
  932. * module_driver() - Helper macro for drivers that don't do anything
  933. * special in module init/exit. This eliminates a lot of boilerplate.
  934. * Each module may only use this macro once, and calling it replaces
  935. * module_init() and module_exit().
  936. *
  937. * @__driver: driver name
  938. * @__register: register function for this driver type
  939. * @__unregister: unregister function for this driver type
  940. * @...: Additional arguments to be passed to __register and __unregister.
  941. *
  942. * Use this macro to construct bus specific macros for registering
  943. * drivers, and do not use it on its own.
  944. */
  945. #define module_driver(__driver, __register, __unregister, ...) \
  946. static int __init __driver##_init(void) \
  947. { \
  948. return __register(&(__driver) , ##__VA_ARGS__); \
  949. } \
  950. module_init(__driver##_init); \
  951. static void __exit __driver##_exit(void) \
  952. { \
  953. __unregister(&(__driver) , ##__VA_ARGS__); \
  954. } \
  955. module_exit(__driver##_exit);
  956. #endif /* _DEVICE_H_ */