xref: /linux-6.15/include/linux/device/bus.h (revision e0766ea4)
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 
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  * @dev_root:	Default device to use as the parent.
30  * @bus_groups:	Default attributes of the bus.
31  * @dev_groups:	Default attributes of the devices on the bus.
32  * @drv_groups: Default attributes of the device drivers on the bus.
33  * @match:	Called, perhaps multiple times, whenever a new device or driver
34  *		is added for this bus. It should return a positive value if the
35  *		given device can be handled by the given driver and zero
36  *		otherwise. It may also return error code if determining that
37  *		the driver supports the device is not possible. In case of
38  *		-EPROBE_DEFER it will queue the device for deferred probing.
39  * @uevent:	Called when a device is added, removed, or a few other things
40  *		that generate uevents to add the environment variables.
41  * @probe:	Called when a new device or driver add to this bus, and callback
42  *		the specific driver's probe to initial the matched device.
43  * @sync_state:	Called to sync device state to software state after all the
44  *		state tracking consumers linked to this device (present at
45  *		the time of late_initcall) have successfully bound to a
46  *		driver. If the device has no consumers, this function will
47  *		be called at late_initcall_sync level. If the device has
48  *		consumers that are never bound to a driver, this function
49  *		will never get called until they do.
50  * @remove:	Called when a device removed from this bus.
51  * @shutdown:	Called at shut-down time to quiesce the device.
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  * @iommu_ops:  IOMMU specific operations for this bus, used to attach IOMMU
67  *              driver implementations to a bus and allow the driver to do
68  *              bus-specific setup
69  * @p:		The private data of the driver core, only the driver core can
70  *		touch this.
71  * @lock_key:	Lock class key for use by the lock validator
72  * @need_parent_lock:	When probing or removing a device on this bus, the
73  *			device core should lock the device's parent.
74  *
75  * A bus is a channel between the processor and one or more devices. For the
76  * purposes of the device model, all devices are connected via a bus, even if
77  * it is an internal, virtual, "platform" bus. Buses can plug into each other.
78  * A USB controller is usually a PCI device, for example. The device model
79  * represents the actual connections between buses and the devices they control.
80  * A bus is represented by the bus_type structure. It contains the name, the
81  * default attributes, the bus' methods, PM operations, and the driver core's
82  * private data.
83  */
84 struct bus_type {
85 	const char		*name;
86 	const char		*dev_name;
87 	struct device		*dev_root;
88 	const struct attribute_group **bus_groups;
89 	const struct attribute_group **dev_groups;
90 	const struct attribute_group **drv_groups;
91 
92 	int (*match)(struct device *dev, struct device_driver *drv);
93 	int (*uevent)(const struct device *dev, struct kobj_uevent_env *env);
94 	int (*probe)(struct device *dev);
95 	void (*sync_state)(struct device *dev);
96 	void (*remove)(struct device *dev);
97 	void (*shutdown)(struct device *dev);
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 	const struct iommu_ops *iommu_ops;
113 
114 	struct subsys_private *p;
115 
116 	bool need_parent_lock;
117 };
118 
119 extern int __must_check bus_register(struct bus_type *bus);
120 
121 extern void bus_unregister(struct bus_type *bus);
122 
123 extern int __must_check bus_rescan_devices(struct bus_type *bus);
124 
125 struct bus_attribute {
126 	struct attribute	attr;
127 	ssize_t (*show)(struct bus_type *bus, char *buf);
128 	ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
129 };
130 
131 #define BUS_ATTR_RW(_name) \
132 	struct bus_attribute bus_attr_##_name = __ATTR_RW(_name)
133 #define BUS_ATTR_RO(_name) \
134 	struct bus_attribute bus_attr_##_name = __ATTR_RO(_name)
135 #define BUS_ATTR_WO(_name) \
136 	struct bus_attribute bus_attr_##_name = __ATTR_WO(_name)
137 
138 extern int __must_check bus_create_file(struct bus_type *,
139 					struct bus_attribute *);
140 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
141 
142 /* Generic device matching functions that all busses can use to match with */
143 int device_match_name(struct device *dev, const void *name);
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 /* iterator helpers for buses */
152 int bus_for_each_dev(const struct bus_type *bus, struct device *start, void *data,
153 		     int (*fn)(struct device *dev, void *data));
154 struct device *bus_find_device(const struct bus_type *bus, struct device *start,
155 			       const void *data,
156 			       int (*match)(struct device *dev, const void *data));
157 /**
158  * bus_find_device_by_name - device iterator for locating a particular device
159  * of a specific name.
160  * @bus: bus type
161  * @start: Device to begin with
162  * @name: name of the device to match
163  */
164 static inline struct device *bus_find_device_by_name(const struct bus_type *bus,
165 						     struct device *start,
166 						     const char *name)
167 {
168 	return bus_find_device(bus, start, name, device_match_name);
169 }
170 
171 /**
172  * bus_find_device_by_of_node : device iterator for locating a particular device
173  * matching the of_node.
174  * @bus: bus type
175  * @np: of_node of the device to match.
176  */
177 static inline struct device *
178 bus_find_device_by_of_node(const struct bus_type *bus, const struct device_node *np)
179 {
180 	return bus_find_device(bus, NULL, np, device_match_of_node);
181 }
182 
183 /**
184  * bus_find_device_by_fwnode : device iterator for locating a particular device
185  * matching the fwnode.
186  * @bus: bus type
187  * @fwnode: fwnode of the device to match.
188  */
189 static inline struct device *
190 bus_find_device_by_fwnode(const struct bus_type *bus, const struct fwnode_handle *fwnode)
191 {
192 	return bus_find_device(bus, NULL, fwnode, device_match_fwnode);
193 }
194 
195 /**
196  * bus_find_device_by_devt : device iterator for locating a particular device
197  * matching the device type.
198  * @bus: bus type
199  * @devt: device type of the device to match.
200  */
201 static inline struct device *bus_find_device_by_devt(const struct bus_type *bus,
202 						     dev_t devt)
203 {
204 	return bus_find_device(bus, NULL, &devt, device_match_devt);
205 }
206 
207 /**
208  * bus_find_next_device - Find the next device after a given device in a
209  * given bus.
210  * @bus: bus type
211  * @cur: device to begin the search with.
212  */
213 static inline struct device *
214 bus_find_next_device(const struct bus_type *bus,struct device *cur)
215 {
216 	return bus_find_device(bus, cur, NULL, device_match_any);
217 }
218 
219 #ifdef CONFIG_ACPI
220 struct acpi_device;
221 
222 /**
223  * bus_find_device_by_acpi_dev : device iterator for locating a particular device
224  * matching the ACPI COMPANION device.
225  * @bus: bus type
226  * @adev: ACPI COMPANION device to match.
227  */
228 static inline struct device *
229 bus_find_device_by_acpi_dev(const struct bus_type *bus, const struct acpi_device *adev)
230 {
231 	return bus_find_device(bus, NULL, adev, device_match_acpi_dev);
232 }
233 #else
234 static inline struct device *
235 bus_find_device_by_acpi_dev(const struct bus_type *bus, const void *adev)
236 {
237 	return NULL;
238 }
239 #endif
240 
241 int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start,
242 		     void *data, int (*fn)(struct device_driver *, void *));
243 void bus_sort_breadthfirst(struct bus_type *bus,
244 			   int (*compare)(const struct device *a,
245 					  const struct device *b));
246 /*
247  * Bus notifiers: Get notified of addition/removal of devices
248  * and binding/unbinding of drivers to devices.
249  * In the long run, it should be a replacement for the platform
250  * notify hooks.
251  */
252 struct notifier_block;
253 
254 extern int bus_register_notifier(struct bus_type *bus,
255 				 struct notifier_block *nb);
256 extern int bus_unregister_notifier(struct bus_type *bus,
257 				   struct notifier_block *nb);
258 
259 /**
260  * enum bus_notifier_event - Bus Notifier events that have happened
261  * @BUS_NOTIFY_ADD_DEVICE: device is added to this bus
262  * @BUS_NOTIFY_DEL_DEVICE: device is about to be removed from this bus
263  * @BUS_NOTIFY_REMOVED_DEVICE: device is successfully removed from this bus
264  * @BUS_NOTIFY_BIND_DRIVER: a driver is about to be bound to this device on this bus
265  * @BUS_NOTIFY_BOUND_DRIVER: a driver is successfully bound to this device on this bus
266  * @BUS_NOTIFY_UNBIND_DRIVER: a driver is about to be unbound from this device on this bus
267  * @BUS_NOTIFY_UNBOUND_DRIVER: a driver is successfully unbound from this device on this bus
268  * @BUS_NOTIFY_DRIVER_NOT_BOUND: a driver failed to be bound to this device on this bus
269  *
270  * These are the value passed to a bus notifier when a specific event happens.
271  *
272  * Note that bus notifiers are likely to be called with the device lock already
273  * held by the driver core, so be careful in any notifier callback as to what
274  * you do with the device structure.
275  *
276  * All bus notifiers are called with the target struct device * as an argument.
277  */
278 enum bus_notifier_event {
279 	BUS_NOTIFY_ADD_DEVICE,
280 	BUS_NOTIFY_DEL_DEVICE,
281 	BUS_NOTIFY_REMOVED_DEVICE,
282 	BUS_NOTIFY_BIND_DRIVER,
283 	BUS_NOTIFY_BOUND_DRIVER,
284 	BUS_NOTIFY_UNBIND_DRIVER,
285 	BUS_NOTIFY_UNBOUND_DRIVER,
286 	BUS_NOTIFY_DRIVER_NOT_BOUND,
287 };
288 
289 extern struct kset *bus_get_kset(struct bus_type *bus);
290 
291 #endif
292