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