at v6.6 11 kB view raw
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 21struct device_driver; 22struct 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 * 52 * @online: Called to put the device back online (after offlining it). 53 * @offline: Called to put the device offline for hot-removal. May fail. 54 * 55 * @suspend: Called when a device on this bus wants to go to sleep mode. 56 * @resume: Called to bring a device on this bus out of sleep mode. 57 * @num_vf: Called to find out how many virtual functions a device on this 58 * bus supports. 59 * @dma_configure: Called to setup DMA configuration on a device on 60 * this bus. 61 * @dma_cleanup: Called to cleanup DMA configuration on a device on 62 * this bus. 63 * @pm: Power management operations of this bus, callback the specific 64 * device driver's pm-ops. 65 * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU 66 * driver implementations to a bus and allow the driver to do 67 * bus-specific setup 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 */ 80struct bus_type { 81 const char *name; 82 const char *dev_name; 83 const struct attribute_group **bus_groups; 84 const struct attribute_group **dev_groups; 85 const struct attribute_group **drv_groups; 86 87 int (*match)(struct device *dev, struct device_driver *drv); 88 int (*uevent)(const 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 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 const struct iommu_ops *iommu_ops; 108 109 bool need_parent_lock; 110}; 111 112int __must_check bus_register(const struct bus_type *bus); 113 114void bus_unregister(const struct bus_type *bus); 115 116int __must_check bus_rescan_devices(const struct bus_type *bus); 117 118struct bus_attribute { 119 struct attribute attr; 120 ssize_t (*show)(const struct bus_type *bus, char *buf); 121 ssize_t (*store)(const struct bus_type *bus, const char *buf, size_t count); 122}; 123 124#define BUS_ATTR_RW(_name) \ 125 struct bus_attribute bus_attr_##_name = __ATTR_RW(_name) 126#define BUS_ATTR_RO(_name) \ 127 struct bus_attribute bus_attr_##_name = __ATTR_RO(_name) 128#define BUS_ATTR_WO(_name) \ 129 struct bus_attribute bus_attr_##_name = __ATTR_WO(_name) 130 131int __must_check bus_create_file(const struct bus_type *bus, struct bus_attribute *attr); 132void bus_remove_file(const struct bus_type *bus, struct bus_attribute *attr); 133 134/* Generic device matching functions that all busses can use to match with */ 135int device_match_name(struct device *dev, const void *name); 136int device_match_of_node(struct device *dev, const void *np); 137int device_match_fwnode(struct device *dev, const void *fwnode); 138int device_match_devt(struct device *dev, const void *pdevt); 139int device_match_acpi_dev(struct device *dev, const void *adev); 140int device_match_acpi_handle(struct device *dev, const void *handle); 141int device_match_any(struct device *dev, const void *unused); 142 143/* iterator helpers for buses */ 144int bus_for_each_dev(const struct bus_type *bus, struct device *start, void *data, 145 int (*fn)(struct device *dev, void *data)); 146struct device *bus_find_device(const struct bus_type *bus, struct device *start, 147 const void *data, 148 int (*match)(struct device *dev, const void *data)); 149/** 150 * bus_find_device_by_name - device iterator for locating a particular device 151 * of a specific name. 152 * @bus: bus type 153 * @start: Device to begin with 154 * @name: name of the device to match 155 */ 156static inline struct device *bus_find_device_by_name(const struct bus_type *bus, 157 struct device *start, 158 const char *name) 159{ 160 return bus_find_device(bus, start, name, device_match_name); 161} 162 163/** 164 * bus_find_device_by_of_node : device iterator for locating a particular device 165 * matching the of_node. 166 * @bus: bus type 167 * @np: of_node of the device to match. 168 */ 169static inline struct device * 170bus_find_device_by_of_node(const struct bus_type *bus, const struct device_node *np) 171{ 172 return bus_find_device(bus, NULL, np, device_match_of_node); 173} 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 */ 181static inline struct device * 182bus_find_device_by_fwnode(const struct bus_type *bus, const struct fwnode_handle *fwnode) 183{ 184 return bus_find_device(bus, NULL, fwnode, device_match_fwnode); 185} 186 187/** 188 * bus_find_device_by_devt : device iterator for locating a particular device 189 * matching the device type. 190 * @bus: bus type 191 * @devt: device type of the device to match. 192 */ 193static inline struct device *bus_find_device_by_devt(const struct bus_type *bus, 194 dev_t devt) 195{ 196 return bus_find_device(bus, NULL, &devt, device_match_devt); 197} 198 199/** 200 * bus_find_next_device - Find the next device after a given device in a 201 * given bus. 202 * @bus: bus type 203 * @cur: device to begin the search with. 204 */ 205static inline struct device * 206bus_find_next_device(const struct bus_type *bus,struct device *cur) 207{ 208 return bus_find_device(bus, cur, NULL, device_match_any); 209} 210 211#ifdef CONFIG_ACPI 212struct acpi_device; 213 214/** 215 * bus_find_device_by_acpi_dev : device iterator for locating a particular device 216 * matching the ACPI COMPANION device. 217 * @bus: bus type 218 * @adev: ACPI COMPANION device to match. 219 */ 220static inline struct device * 221bus_find_device_by_acpi_dev(const struct bus_type *bus, const struct acpi_device *adev) 222{ 223 return bus_find_device(bus, NULL, adev, device_match_acpi_dev); 224} 225#else 226static inline struct device * 227bus_find_device_by_acpi_dev(const struct bus_type *bus, const void *adev) 228{ 229 return NULL; 230} 231#endif 232 233int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start, 234 void *data, int (*fn)(struct device_driver *, void *)); 235void bus_sort_breadthfirst(struct bus_type *bus, 236 int (*compare)(const struct device *a, 237 const struct device *b)); 238/* 239 * Bus notifiers: Get notified of addition/removal of devices 240 * and binding/unbinding of drivers to devices. 241 * In the long run, it should be a replacement for the platform 242 * notify hooks. 243 */ 244struct notifier_block; 245 246int bus_register_notifier(const struct bus_type *bus, struct notifier_block *nb); 247int bus_unregister_notifier(const struct bus_type *bus, struct notifier_block *nb); 248 249/** 250 * enum bus_notifier_event - Bus Notifier events that have happened 251 * @BUS_NOTIFY_ADD_DEVICE: device is added to this bus 252 * @BUS_NOTIFY_DEL_DEVICE: device is about to be removed from this bus 253 * @BUS_NOTIFY_REMOVED_DEVICE: device is successfully removed from this bus 254 * @BUS_NOTIFY_BIND_DRIVER: a driver is about to be bound to this device on this bus 255 * @BUS_NOTIFY_BOUND_DRIVER: a driver is successfully bound to this device on this bus 256 * @BUS_NOTIFY_UNBIND_DRIVER: a driver is about to be unbound from this device on this bus 257 * @BUS_NOTIFY_UNBOUND_DRIVER: a driver is successfully unbound from this device on this bus 258 * @BUS_NOTIFY_DRIVER_NOT_BOUND: a driver failed to be bound to this device on this bus 259 * 260 * These are the value passed to a bus notifier when a specific event happens. 261 * 262 * Note that bus notifiers are likely to be called with the device lock already 263 * held by the driver core, so be careful in any notifier callback as to what 264 * you do with the device structure. 265 * 266 * All bus notifiers are called with the target struct device * as an argument. 267 */ 268enum bus_notifier_event { 269 BUS_NOTIFY_ADD_DEVICE, 270 BUS_NOTIFY_DEL_DEVICE, 271 BUS_NOTIFY_REMOVED_DEVICE, 272 BUS_NOTIFY_BIND_DRIVER, 273 BUS_NOTIFY_BOUND_DRIVER, 274 BUS_NOTIFY_UNBIND_DRIVER, 275 BUS_NOTIFY_UNBOUND_DRIVER, 276 BUS_NOTIFY_DRIVER_NOT_BOUND, 277}; 278 279struct kset *bus_get_kset(const struct bus_type *bus); 280struct device *bus_get_dev_root(const struct bus_type *bus); 281 282#endif