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