driver.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * The driver-specific portions of the driver model
  4. *
  5. * Copyright (c) 2001-2003 Patrick Mochel <[email protected]>
  6. * Copyright (c) 2004-2009 Greg Kroah-Hartman <[email protected]>
  7. * Copyright (c) 2008-2009 Novell Inc.
  8. * Copyright (c) 2012-2019 Greg Kroah-Hartman <[email protected]>
  9. * Copyright (c) 2012-2019 Linux Foundation
  10. *
  11. * See Documentation/driver-api/driver-model/ for more information.
  12. */
  13. #ifndef _DEVICE_DRIVER_H_
  14. #define _DEVICE_DRIVER_H_
  15. #include <linux/kobject.h>
  16. #include <linux/klist.h>
  17. #include <linux/pm.h>
  18. #include <linux/device/bus.h>
  19. #include <linux/module.h>
  20. /**
  21. * enum probe_type - device driver probe type to try
  22. * Device drivers may opt in for special handling of their
  23. * respective probe routines. This tells the core what to
  24. * expect and prefer.
  25. *
  26. * @PROBE_DEFAULT_STRATEGY: Used by drivers that work equally well
  27. * whether probed synchronously or asynchronously.
  28. * @PROBE_PREFER_ASYNCHRONOUS: Drivers for "slow" devices which
  29. * probing order is not essential for booting the system may
  30. * opt into executing their probes asynchronously.
  31. * @PROBE_FORCE_SYNCHRONOUS: Use this to annotate drivers that need
  32. * their probe routines to run synchronously with driver and
  33. * device registration (with the exception of -EPROBE_DEFER
  34. * handling - re-probing always ends up being done asynchronously).
  35. *
  36. * Note that the end goal is to switch the kernel to use asynchronous
  37. * probing by default, so annotating drivers with
  38. * %PROBE_PREFER_ASYNCHRONOUS is a temporary measure that allows us
  39. * to speed up boot process while we are validating the rest of the
  40. * drivers.
  41. */
  42. enum probe_type {
  43. PROBE_DEFAULT_STRATEGY,
  44. PROBE_PREFER_ASYNCHRONOUS,
  45. PROBE_FORCE_SYNCHRONOUS,
  46. };
  47. /**
  48. * struct device_driver - The basic device driver structure
  49. * @name: Name of the device driver.
  50. * @bus: The bus which the device of this driver belongs to.
  51. * @owner: The module owner.
  52. * @mod_name: Used for built-in modules.
  53. * @suppress_bind_attrs: Disables bind/unbind via sysfs.
  54. * @probe_type: Type of the probe (synchronous or asynchronous) to use.
  55. * @of_match_table: The open firmware table.
  56. * @acpi_match_table: The ACPI match table.
  57. * @probe: Called to query the existence of a specific device,
  58. * whether this driver can work with it, and bind the driver
  59. * to a specific device.
  60. * @sync_state: Called to sync device state to software state after all the
  61. * state tracking consumers linked to this device (present at
  62. * the time of late_initcall) have successfully bound to a
  63. * driver. If the device has no consumers, this function will
  64. * be called at late_initcall_sync level. If the device has
  65. * consumers that are never bound to a driver, this function
  66. * will never get called until they do.
  67. * @remove: Called when the device is removed from the system to
  68. * unbind a device from this driver.
  69. * @shutdown: Called at shut-down time to quiesce the device.
  70. * @suspend: Called to put the device to sleep mode. Usually to a
  71. * low power state.
  72. * @resume: Called to bring a device from sleep mode.
  73. * @groups: Default attributes that get created by the driver core
  74. * automatically.
  75. * @dev_groups: Additional attributes attached to device instance once
  76. * it is bound to the driver.
  77. * @pm: Power management operations of the device which matched
  78. * this driver.
  79. * @coredump: Called when sysfs entry is written to. The device driver
  80. * is expected to call the dev_coredump API resulting in a
  81. * uevent.
  82. * @p: Driver core's private data, no one other than the driver
  83. * core can touch this.
  84. *
  85. * The device driver-model tracks all of the drivers known to the system.
  86. * The main reason for this tracking is to enable the driver core to match
  87. * up drivers with new devices. Once drivers are known objects within the
  88. * system, however, a number of other things become possible. Device drivers
  89. * can export information and configuration variables that are independent
  90. * of any specific device.
  91. */
  92. struct device_driver {
  93. const char *name;
  94. struct bus_type *bus;
  95. struct module *owner;
  96. const char *mod_name; /* used for built-in modules */
  97. bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
  98. enum probe_type probe_type;
  99. const struct of_device_id *of_match_table;
  100. const struct acpi_device_id *acpi_match_table;
  101. int (*probe) (struct device *dev);
  102. void (*sync_state)(struct device *dev);
  103. int (*remove) (struct device *dev);
  104. void (*shutdown) (struct device *dev);
  105. int (*suspend) (struct device *dev, pm_message_t state);
  106. int (*resume) (struct device *dev);
  107. const struct attribute_group **groups;
  108. const struct attribute_group **dev_groups;
  109. const struct dev_pm_ops *pm;
  110. void (*coredump) (struct device *dev);
  111. struct driver_private *p;
  112. ANDROID_KABI_RESERVE(1);
  113. ANDROID_KABI_RESERVE(2);
  114. ANDROID_KABI_RESERVE(3);
  115. ANDROID_KABI_RESERVE(4);
  116. };
  117. extern int __must_check driver_register(struct device_driver *drv);
  118. extern void driver_unregister(struct device_driver *drv);
  119. extern struct device_driver *driver_find(const char *name,
  120. struct bus_type *bus);
  121. extern int driver_probe_done(void);
  122. extern void wait_for_device_probe(void);
  123. extern void flush_deferred_probe_now(void);
  124. void __init wait_for_init_devices_probe(void);
  125. /* sysfs interface for exporting driver attributes */
  126. struct driver_attribute {
  127. struct attribute attr;
  128. ssize_t (*show)(struct device_driver *driver, char *buf);
  129. ssize_t (*store)(struct device_driver *driver, const char *buf,
  130. size_t count);
  131. };
  132. #define DRIVER_ATTR_RW(_name) \
  133. struct driver_attribute driver_attr_##_name = __ATTR_RW(_name)
  134. #define DRIVER_ATTR_RO(_name) \
  135. struct driver_attribute driver_attr_##_name = __ATTR_RO(_name)
  136. #define DRIVER_ATTR_WO(_name) \
  137. struct driver_attribute driver_attr_##_name = __ATTR_WO(_name)
  138. extern int __must_check driver_create_file(struct device_driver *driver,
  139. const struct driver_attribute *attr);
  140. extern void driver_remove_file(struct device_driver *driver,
  141. const struct driver_attribute *attr);
  142. int driver_set_override(struct device *dev, const char **override,
  143. const char *s, size_t len);
  144. extern int __must_check driver_for_each_device(struct device_driver *drv,
  145. struct device *start,
  146. void *data,
  147. int (*fn)(struct device *dev,
  148. void *));
  149. struct device *driver_find_device(struct device_driver *drv,
  150. struct device *start, const void *data,
  151. int (*match)(struct device *dev, const void *data));
  152. /**
  153. * driver_find_device_by_name - device iterator for locating a particular device
  154. * of a specific name.
  155. * @drv: the driver we're iterating
  156. * @name: name of the device to match
  157. */
  158. static inline struct device *driver_find_device_by_name(struct device_driver *drv,
  159. const char *name)
  160. {
  161. return driver_find_device(drv, NULL, name, device_match_name);
  162. }
  163. /**
  164. * driver_find_device_by_of_node- device iterator for locating a particular device
  165. * by of_node pointer.
  166. * @drv: the driver we're iterating
  167. * @np: of_node pointer to match.
  168. */
  169. static inline struct device *
  170. driver_find_device_by_of_node(struct device_driver *drv,
  171. const struct device_node *np)
  172. {
  173. return driver_find_device(drv, NULL, np, device_match_of_node);
  174. }
  175. /**
  176. * driver_find_device_by_fwnode- device iterator for locating a particular device
  177. * by fwnode pointer.
  178. * @drv: the driver we're iterating
  179. * @fwnode: fwnode pointer to match.
  180. */
  181. static inline struct device *
  182. driver_find_device_by_fwnode(struct device_driver *drv,
  183. const struct fwnode_handle *fwnode)
  184. {
  185. return driver_find_device(drv, NULL, fwnode, device_match_fwnode);
  186. }
  187. /**
  188. * driver_find_device_by_devt- device iterator for locating a particular device
  189. * by devt.
  190. * @drv: the driver we're iterating
  191. * @devt: devt pointer to match.
  192. */
  193. static inline struct device *driver_find_device_by_devt(struct device_driver *drv,
  194. dev_t devt)
  195. {
  196. return driver_find_device(drv, NULL, &devt, device_match_devt);
  197. }
  198. static inline struct device *driver_find_next_device(struct device_driver *drv,
  199. struct device *start)
  200. {
  201. return driver_find_device(drv, start, NULL, device_match_any);
  202. }
  203. #ifdef CONFIG_ACPI
  204. /**
  205. * driver_find_device_by_acpi_dev : device iterator for locating a particular
  206. * device matching the ACPI_COMPANION device.
  207. * @drv: the driver we're iterating
  208. * @adev: ACPI_COMPANION device to match.
  209. */
  210. static inline struct device *
  211. driver_find_device_by_acpi_dev(struct device_driver *drv,
  212. const struct acpi_device *adev)
  213. {
  214. return driver_find_device(drv, NULL, adev, device_match_acpi_dev);
  215. }
  216. #else
  217. static inline struct device *
  218. driver_find_device_by_acpi_dev(struct device_driver *drv, const void *adev)
  219. {
  220. return NULL;
  221. }
  222. #endif
  223. extern int driver_deferred_probe_timeout;
  224. void driver_deferred_probe_add(struct device *dev);
  225. int driver_deferred_probe_check_state(struct device *dev);
  226. void driver_init(void);
  227. /**
  228. * module_driver() - Helper macro for drivers that don't do anything
  229. * special in module init/exit. This eliminates a lot of boilerplate.
  230. * Each module may only use this macro once, and calling it replaces
  231. * module_init() and module_exit().
  232. *
  233. * @__driver: driver name
  234. * @__register: register function for this driver type
  235. * @__unregister: unregister function for this driver type
  236. * @...: Additional arguments to be passed to __register and __unregister.
  237. *
  238. * Use this macro to construct bus specific macros for registering
  239. * drivers, and do not use it on its own.
  240. */
  241. #define module_driver(__driver, __register, __unregister, ...) \
  242. static int __init __driver##_init(void) \
  243. { \
  244. return __register(&(__driver) , ##__VA_ARGS__); \
  245. } \
  246. module_init(__driver##_init); \
  247. static void __exit __driver##_exit(void) \
  248. { \
  249. __unregister(&(__driver) , ##__VA_ARGS__); \
  250. } \
  251. module_exit(__driver##_exit);
  252. /**
  253. * builtin_driver() - Helper macro for drivers that don't do anything
  254. * special in init and have no exit. This eliminates some boilerplate.
  255. * Each driver may only use this macro once, and calling it replaces
  256. * device_initcall (or in some cases, the legacy __initcall). This is
  257. * meant to be a direct parallel of module_driver() above but without
  258. * the __exit stuff that is not used for builtin cases.
  259. *
  260. * @__driver: driver name
  261. * @__register: register function for this driver type
  262. * @...: Additional arguments to be passed to __register
  263. *
  264. * Use this macro to construct bus specific macros for registering
  265. * drivers, and do not use it on its own.
  266. */
  267. #define builtin_driver(__driver, __register, ...) \
  268. static int __init __driver##_init(void) \
  269. { \
  270. return __register(&(__driver) , ##__VA_ARGS__); \
  271. } \
  272. device_initcall(__driver##_init);
  273. #endif /* _DEVICE_DRIVER_H_ */