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