bus.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * bus.h - the bus-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_BUS_H_
  14. #define _DEVICE_BUS_H_
  15. #include <linux/kobject.h>
  16. #include <linux/klist.h>
  17. #include <linux/pm.h>
  18. struct device_driver;
  19. struct fwnode_handle;
  20. /**
  21. * struct bus_type - The bus type of the device
  22. *
  23. * @name: The name of the bus.
  24. * @dev_name: Used for subsystems to enumerate devices like ("foo%u", dev->id).
  25. * @dev_root: Default device to use as the parent.
  26. * @bus_groups: Default attributes of the bus.
  27. * @dev_groups: Default attributes of the devices on the bus.
  28. * @drv_groups: Default attributes of the device drivers on the bus.
  29. * @match: Called, perhaps multiple times, whenever a new device or driver
  30. * is added for this bus. It should return a positive value if the
  31. * given device can be handled by the given driver and zero
  32. * otherwise. It may also return error code if determining that
  33. * the driver supports the device is not possible. In case of
  34. * -EPROBE_DEFER it will queue the device for deferred probing.
  35. * @uevent: Called when a device is added, removed, or a few other things
  36. * that generate uevents to add the environment variables.
  37. * @probe: Called when a new device or driver add to this bus, and callback
  38. * the specific driver's probe to initial the matched device.
  39. * @sync_state: Called to sync device state to software state after all the
  40. * state tracking consumers linked to this device (present at
  41. * the time of late_initcall) have successfully bound to a
  42. * driver. If the device has no consumers, this function will
  43. * be called at late_initcall_sync level. If the device has
  44. * consumers that are never bound to a driver, this function
  45. * will never get called until they do.
  46. * @remove: Called when a device removed from this bus.
  47. * @shutdown: Called at shut-down time to quiesce the device.
  48. *
  49. * @online: Called to put the device back online (after offlining it).
  50. * @offline: Called to put the device offline for hot-removal. May fail.
  51. *
  52. * @suspend: Called when a device on this bus wants to go to sleep mode.
  53. * @resume: Called to bring a device on this bus out of sleep mode.
  54. * @num_vf: Called to find out how many virtual functions a device on this
  55. * bus supports.
  56. * @dma_configure: Called to setup DMA configuration on a device on
  57. * this bus.
  58. * @dma_cleanup: Called to cleanup DMA configuration on a device on
  59. * this bus.
  60. * @pm: Power management operations of this bus, callback the specific
  61. * device driver's pm-ops.
  62. * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU
  63. * driver implementations to a bus and allow the driver to do
  64. * bus-specific setup
  65. * @p: The private data of the driver core, only the driver core can
  66. * touch this.
  67. * @lock_key: Lock class key for use by the lock validator
  68. * @need_parent_lock: When probing or removing a device on this bus, the
  69. * device core should lock the device's parent.
  70. *
  71. * A bus is a channel between the processor and one or more devices. For the
  72. * purposes of the device model, all devices are connected via a bus, even if
  73. * it is an internal, virtual, "platform" bus. Buses can plug into each other.
  74. * A USB controller is usually a PCI device, for example. The device model
  75. * represents the actual connections between buses and the devices they control.
  76. * A bus is represented by the bus_type structure. It contains the name, the
  77. * default attributes, the bus' methods, PM operations, and the driver core's
  78. * private data.
  79. */
  80. struct bus_type {
  81. const char *name;
  82. const char *dev_name;
  83. struct device *dev_root;
  84. const struct attribute_group **bus_groups;
  85. const struct attribute_group **dev_groups;
  86. const struct attribute_group **drv_groups;
  87. int (*match)(struct device *dev, struct device_driver *drv);
  88. int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  89. int (*probe)(struct device *dev);
  90. void (*sync_state)(struct device *dev);
  91. void (*remove)(struct device *dev);
  92. void (*shutdown)(struct device *dev);
  93. int (*online)(struct device *dev);
  94. int (*offline)(struct device *dev);
  95. int (*suspend)(struct device *dev, pm_message_t state);
  96. int (*resume)(struct device *dev);
  97. int (*num_vf)(struct device *dev);
  98. int (*dma_configure)(struct device *dev);
  99. void (*dma_cleanup)(struct device *dev);
  100. const struct dev_pm_ops *pm;
  101. const struct iommu_ops *iommu_ops;
  102. struct subsys_private *p;
  103. struct lock_class_key lock_key;
  104. bool need_parent_lock;
  105. ANDROID_KABI_RESERVE(1);
  106. ANDROID_KABI_RESERVE(2);
  107. ANDROID_KABI_RESERVE(3);
  108. ANDROID_KABI_RESERVE(4);
  109. };
  110. extern int __must_check bus_register(struct bus_type *bus);
  111. extern void bus_unregister(struct bus_type *bus);
  112. extern int __must_check bus_rescan_devices(struct bus_type *bus);
  113. struct bus_attribute {
  114. struct attribute attr;
  115. ssize_t (*show)(struct bus_type *bus, char *buf);
  116. ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
  117. };
  118. #define BUS_ATTR_RW(_name) \
  119. struct bus_attribute bus_attr_##_name = __ATTR_RW(_name)
  120. #define BUS_ATTR_RO(_name) \
  121. struct bus_attribute bus_attr_##_name = __ATTR_RO(_name)
  122. #define BUS_ATTR_WO(_name) \
  123. struct bus_attribute bus_attr_##_name = __ATTR_WO(_name)
  124. extern int __must_check bus_create_file(struct bus_type *,
  125. struct bus_attribute *);
  126. extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
  127. /* Generic device matching functions that all busses can use to match with */
  128. int device_match_name(struct device *dev, const void *name);
  129. int device_match_of_node(struct device *dev, const void *np);
  130. int device_match_fwnode(struct device *dev, const void *fwnode);
  131. int device_match_devt(struct device *dev, const void *pdevt);
  132. int device_match_acpi_dev(struct device *dev, const void *adev);
  133. int device_match_acpi_handle(struct device *dev, const void *handle);
  134. int device_match_any(struct device *dev, const void *unused);
  135. /* iterator helpers for buses */
  136. struct subsys_dev_iter {
  137. struct klist_iter ki;
  138. const struct device_type *type;
  139. };
  140. void subsys_dev_iter_init(struct subsys_dev_iter *iter,
  141. struct bus_type *subsys,
  142. struct device *start,
  143. const struct device_type *type);
  144. struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter);
  145. void subsys_dev_iter_exit(struct subsys_dev_iter *iter);
  146. int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
  147. int (*fn)(struct device *dev, void *data));
  148. struct device *bus_find_device(struct bus_type *bus, struct device *start,
  149. const void *data,
  150. int (*match)(struct device *dev, const void *data));
  151. /**
  152. * bus_find_device_by_name - device iterator for locating a particular device
  153. * of a specific name.
  154. * @bus: bus type
  155. * @start: Device to begin with
  156. * @name: name of the device to match
  157. */
  158. static inline struct device *bus_find_device_by_name(struct bus_type *bus,
  159. struct device *start,
  160. const char *name)
  161. {
  162. return bus_find_device(bus, start, name, device_match_name);
  163. }
  164. /**
  165. * bus_find_device_by_of_node : device iterator for locating a particular device
  166. * matching the of_node.
  167. * @bus: bus type
  168. * @np: of_node of the device to match.
  169. */
  170. static inline struct device *
  171. bus_find_device_by_of_node(struct bus_type *bus, const struct device_node *np)
  172. {
  173. return bus_find_device(bus, NULL, np, device_match_of_node);
  174. }
  175. /**
  176. * bus_find_device_by_fwnode : device iterator for locating a particular device
  177. * matching the fwnode.
  178. * @bus: bus type
  179. * @fwnode: fwnode of the device to match.
  180. */
  181. static inline struct device *
  182. bus_find_device_by_fwnode(struct bus_type *bus, const struct fwnode_handle *fwnode)
  183. {
  184. return bus_find_device(bus, NULL, fwnode, device_match_fwnode);
  185. }
  186. /**
  187. * bus_find_device_by_devt : device iterator for locating a particular device
  188. * matching the device type.
  189. * @bus: bus type
  190. * @devt: device type of the device to match.
  191. */
  192. static inline struct device *bus_find_device_by_devt(struct bus_type *bus,
  193. dev_t devt)
  194. {
  195. return bus_find_device(bus, NULL, &devt, device_match_devt);
  196. }
  197. /**
  198. * bus_find_next_device - Find the next device after a given device in a
  199. * given bus.
  200. * @bus: bus type
  201. * @cur: device to begin the search with.
  202. */
  203. static inline struct device *
  204. bus_find_next_device(struct bus_type *bus,struct device *cur)
  205. {
  206. return bus_find_device(bus, cur, NULL, device_match_any);
  207. }
  208. #ifdef CONFIG_ACPI
  209. struct acpi_device;
  210. /**
  211. * bus_find_device_by_acpi_dev : device iterator for locating a particular device
  212. * matching the ACPI COMPANION device.
  213. * @bus: bus type
  214. * @adev: ACPI COMPANION device to match.
  215. */
  216. static inline struct device *
  217. bus_find_device_by_acpi_dev(struct bus_type *bus, const struct acpi_device *adev)
  218. {
  219. return bus_find_device(bus, NULL, adev, device_match_acpi_dev);
  220. }
  221. #else
  222. static inline struct device *
  223. bus_find_device_by_acpi_dev(struct bus_type *bus, const void *adev)
  224. {
  225. return NULL;
  226. }
  227. #endif
  228. struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id,
  229. struct device *hint);
  230. int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
  231. void *data, int (*fn)(struct device_driver *, void *));
  232. void bus_sort_breadthfirst(struct bus_type *bus,
  233. int (*compare)(const struct device *a,
  234. const struct device *b));
  235. /*
  236. * Bus notifiers: Get notified of addition/removal of devices
  237. * and binding/unbinding of drivers to devices.
  238. * In the long run, it should be a replacement for the platform
  239. * notify hooks.
  240. */
  241. struct notifier_block;
  242. extern int bus_register_notifier(struct bus_type *bus,
  243. struct notifier_block *nb);
  244. extern int bus_unregister_notifier(struct bus_type *bus,
  245. struct notifier_block *nb);
  246. /* All 4 notifers below get called with the target struct device *
  247. * as an argument. Note that those functions are likely to be called
  248. * with the device lock held in the core, so be careful.
  249. */
  250. #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
  251. #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device to be removed */
  252. #define BUS_NOTIFY_REMOVED_DEVICE 0x00000003 /* device removed */
  253. #define BUS_NOTIFY_BIND_DRIVER 0x00000004 /* driver about to be
  254. bound */
  255. #define BUS_NOTIFY_BOUND_DRIVER 0x00000005 /* driver bound to device */
  256. #define BUS_NOTIFY_UNBIND_DRIVER 0x00000006 /* driver about to be
  257. unbound */
  258. #define BUS_NOTIFY_UNBOUND_DRIVER 0x00000007 /* driver is unbound
  259. from the device */
  260. #define BUS_NOTIFY_DRIVER_NOT_BOUND 0x00000008 /* driver fails to be bound */
  261. extern struct kset *bus_get_kset(struct bus_type *bus);
  262. extern struct klist *bus_get_device_klist(struct bus_type *bus);
  263. #endif