15aee2bf2SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
25aee2bf2SGreg Kroah-Hartman /*
35aee2bf2SGreg Kroah-Hartman * bus.h - the bus-specific portions of the driver model
45aee2bf2SGreg Kroah-Hartman *
55aee2bf2SGreg Kroah-Hartman * Copyright (c) 2001-2003 Patrick Mochel <[email protected]>
65aee2bf2SGreg Kroah-Hartman * Copyright (c) 2004-2009 Greg Kroah-Hartman <[email protected]>
75aee2bf2SGreg Kroah-Hartman * Copyright (c) 2008-2009 Novell Inc.
85aee2bf2SGreg Kroah-Hartman * Copyright (c) 2012-2019 Greg Kroah-Hartman <[email protected]>
95aee2bf2SGreg Kroah-Hartman * Copyright (c) 2012-2019 Linux Foundation
105aee2bf2SGreg Kroah-Hartman *
115aee2bf2SGreg Kroah-Hartman * See Documentation/driver-api/driver-model/ for more information.
125aee2bf2SGreg Kroah-Hartman */
135aee2bf2SGreg Kroah-Hartman
145aee2bf2SGreg Kroah-Hartman #ifndef _DEVICE_BUS_H_
155aee2bf2SGreg Kroah-Hartman #define _DEVICE_BUS_H_
165aee2bf2SGreg Kroah-Hartman
175aee2bf2SGreg Kroah-Hartman #include <linux/kobject.h>
185aee2bf2SGreg Kroah-Hartman #include <linux/klist.h>
195aee2bf2SGreg Kroah-Hartman #include <linux/pm.h>
205aee2bf2SGreg Kroah-Hartman
215aee2bf2SGreg Kroah-Hartman struct device_driver;
225aee2bf2SGreg Kroah-Hartman struct fwnode_handle;
235aee2bf2SGreg Kroah-Hartman
245aee2bf2SGreg Kroah-Hartman /**
255aee2bf2SGreg Kroah-Hartman * struct bus_type - The bus type of the device
265aee2bf2SGreg Kroah-Hartman *
275aee2bf2SGreg Kroah-Hartman * @name: The name of the bus.
285aee2bf2SGreg Kroah-Hartman * @dev_name: Used for subsystems to enumerate devices like ("foo%u", dev->id).
295aee2bf2SGreg Kroah-Hartman * @bus_groups: Default attributes of the bus.
305aee2bf2SGreg Kroah-Hartman * @dev_groups: Default attributes of the devices on the bus.
315aee2bf2SGreg Kroah-Hartman * @drv_groups: Default attributes of the device drivers on the bus.
325aee2bf2SGreg Kroah-Hartman * @match: Called, perhaps multiple times, whenever a new device or driver
335aee2bf2SGreg Kroah-Hartman * is added for this bus. It should return a positive value if the
345aee2bf2SGreg Kroah-Hartman * given device can be handled by the given driver and zero
355aee2bf2SGreg Kroah-Hartman * otherwise. It may also return error code if determining that
365aee2bf2SGreg Kroah-Hartman * the driver supports the device is not possible. In case of
375aee2bf2SGreg Kroah-Hartman * -EPROBE_DEFER it will queue the device for deferred probing.
385aee2bf2SGreg Kroah-Hartman * @uevent: Called when a device is added, removed, or a few other things
395aee2bf2SGreg Kroah-Hartman * that generate uevents to add the environment variables.
405aee2bf2SGreg Kroah-Hartman * @probe: Called when a new device or driver add to this bus, and callback
415aee2bf2SGreg Kroah-Hartman * the specific driver's probe to initial the matched device.
425aee2bf2SGreg Kroah-Hartman * @sync_state: Called to sync device state to software state after all the
435aee2bf2SGreg Kroah-Hartman * state tracking consumers linked to this device (present at
445aee2bf2SGreg Kroah-Hartman * the time of late_initcall) have successfully bound to a
455aee2bf2SGreg Kroah-Hartman * driver. If the device has no consumers, this function will
465aee2bf2SGreg Kroah-Hartman * be called at late_initcall_sync level. If the device has
475aee2bf2SGreg Kroah-Hartman * consumers that are never bound to a driver, this function
485aee2bf2SGreg Kroah-Hartman * will never get called until they do.
495aee2bf2SGreg Kroah-Hartman * @remove: Called when a device removed from this bus.
505aee2bf2SGreg Kroah-Hartman * @shutdown: Called at shut-down time to quiesce the device.
515aee2bf2SGreg Kroah-Hartman * @irq_get_affinity: Get IRQ affinity mask for the device on this bus.
525aee2bf2SGreg Kroah-Hartman *
535aee2bf2SGreg Kroah-Hartman * @online: Called to put the device back online (after offlining it).
545aee2bf2SGreg Kroah-Hartman * @offline: Called to put the device offline for hot-removal. May fail.
555aee2bf2SGreg Kroah-Hartman *
565aee2bf2SGreg Kroah-Hartman * @suspend: Called when a device on this bus wants to go to sleep mode.
575aee2bf2SGreg Kroah-Hartman * @resume: Called to bring a device on this bus out of sleep mode.
585aee2bf2SGreg Kroah-Hartman * @num_vf: Called to find out how many virtual functions a device on this
595aee2bf2SGreg Kroah-Hartman * bus supports.
605aee2bf2SGreg Kroah-Hartman * @dma_configure: Called to setup DMA configuration on a device on
6125f3bcfcSLu Baolu * this bus.
6225f3bcfcSLu Baolu * @dma_cleanup: Called to cleanup DMA configuration on a device on
635aee2bf2SGreg Kroah-Hartman * this bus.
645aee2bf2SGreg Kroah-Hartman * @pm: Power management operations of this bus, callback the specific
655aee2bf2SGreg Kroah-Hartman * device driver's pm-ops.
665aee2bf2SGreg Kroah-Hartman * @need_parent_lock: When probing or removing a device on this bus, the
675aee2bf2SGreg Kroah-Hartman * device core should lock the device's parent.
685aee2bf2SGreg Kroah-Hartman *
695aee2bf2SGreg Kroah-Hartman * A bus is a channel between the processor and one or more devices. For the
705aee2bf2SGreg Kroah-Hartman * purposes of the device model, all devices are connected via a bus, even if
715aee2bf2SGreg Kroah-Hartman * it is an internal, virtual, "platform" bus. Buses can plug into each other.
725aee2bf2SGreg Kroah-Hartman * A USB controller is usually a PCI device, for example. The device model
735aee2bf2SGreg Kroah-Hartman * represents the actual connections between buses and the devices they control.
745aee2bf2SGreg Kroah-Hartman * A bus is represented by the bus_type structure. It contains the name, the
755aee2bf2SGreg Kroah-Hartman * default attributes, the bus' methods, PM operations, and the driver core's
765aee2bf2SGreg Kroah-Hartman * private data.
775aee2bf2SGreg Kroah-Hartman */
785aee2bf2SGreg Kroah-Hartman struct bus_type {
795aee2bf2SGreg Kroah-Hartman const char *name;
805aee2bf2SGreg Kroah-Hartman const char *dev_name;
815aee2bf2SGreg Kroah-Hartman const struct attribute_group **bus_groups;
825aee2bf2SGreg Kroah-Hartman const struct attribute_group **dev_groups;
835aee2bf2SGreg Kroah-Hartman const struct attribute_group **drv_groups;
84d69d8048SGreg Kroah-Hartman
852a81ada3SGreg Kroah-Hartman int (*match)(struct device *dev, const struct device_driver *drv);
865aee2bf2SGreg Kroah-Hartman int (*uevent)(const struct device *dev, struct kobj_uevent_env *env);
875aee2bf2SGreg Kroah-Hartman int (*probe)(struct device *dev);
88fc7a6209SUwe Kleine-König void (*sync_state)(struct device *dev);
895aee2bf2SGreg Kroah-Hartman void (*remove)(struct device *dev);
905aee2bf2SGreg Kroah-Hartman void (*shutdown)(struct device *dev);
915aee2bf2SGreg Kroah-Hartman const struct cpumask *(*irq_get_affinity)(struct device *dev,
925aee2bf2SGreg Kroah-Hartman unsigned int irq_vec);
935aee2bf2SGreg Kroah-Hartman
945aee2bf2SGreg Kroah-Hartman int (*online)(struct device *dev);
955aee2bf2SGreg Kroah-Hartman int (*offline)(struct device *dev);
965aee2bf2SGreg Kroah-Hartman
975aee2bf2SGreg Kroah-Hartman int (*suspend)(struct device *dev, pm_message_t state);
985aee2bf2SGreg Kroah-Hartman int (*resume)(struct device *dev);
995aee2bf2SGreg Kroah-Hartman
10025f3bcfcSLu Baolu int (*num_vf)(struct device *dev);
1015aee2bf2SGreg Kroah-Hartman
1025aee2bf2SGreg Kroah-Hartman int (*dma_configure)(struct device *dev);
1035aee2bf2SGreg Kroah-Hartman void (*dma_cleanup)(struct device *dev);
1045aee2bf2SGreg Kroah-Hartman
1055aee2bf2SGreg Kroah-Hartman const struct dev_pm_ops *pm;
1065aee2bf2SGreg Kroah-Hartman
1070d62b79fSGreg Kroah-Hartman bool need_parent_lock;
1085aee2bf2SGreg Kroah-Hartman };
1090d62b79fSGreg Kroah-Hartman
1105aee2bf2SGreg Kroah-Hartman int __must_check bus_register(const struct bus_type *bus);
1110d62b79fSGreg Kroah-Hartman
1125aee2bf2SGreg Kroah-Hartman void bus_unregister(const struct bus_type *bus);
1135aee2bf2SGreg Kroah-Hartman
1145aee2bf2SGreg Kroah-Hartman int __must_check bus_rescan_devices(const struct bus_type *bus);
11575cff725SGreg Kroah-Hartman
11675cff725SGreg Kroah-Hartman struct bus_attribute {
1175aee2bf2SGreg Kroah-Hartman struct attribute attr;
1185aee2bf2SGreg Kroah-Hartman ssize_t (*show)(const struct bus_type *bus, char *buf);
1195aee2bf2SGreg Kroah-Hartman ssize_t (*store)(const struct bus_type *bus, const char *buf, size_t count);
1205aee2bf2SGreg Kroah-Hartman };
1215aee2bf2SGreg Kroah-Hartman
1225aee2bf2SGreg Kroah-Hartman #define BUS_ATTR_RW(_name) \
1235aee2bf2SGreg Kroah-Hartman struct bus_attribute bus_attr_##_name = __ATTR_RW(_name)
1245aee2bf2SGreg Kroah-Hartman #define BUS_ATTR_RO(_name) \
1255aee2bf2SGreg Kroah-Hartman struct bus_attribute bus_attr_##_name = __ATTR_RO(_name)
1260396f286SGreg Kroah-Hartman #define BUS_ATTR_WO(_name) \
1270396f286SGreg Kroah-Hartman struct bus_attribute bus_attr_##_name = __ATTR_WO(_name)
1285aee2bf2SGreg Kroah-Hartman
129b45ed06fSZijun Hu int __must_check bus_create_file(const struct bus_type *bus, struct bus_attribute *attr);
130b45ed06fSZijun Hu void bus_remove_file(const struct bus_type *bus, struct bus_attribute *attr);
131b45ed06fSZijun Hu
1325aee2bf2SGreg Kroah-Hartman /* Matching function type for drivers/base APIs to find a specific device */
1335aee2bf2SGreg Kroah-Hartman typedef int (*device_match_t)(struct device *dev, const void *data);
134adf908c9SZijun Hu
1355aee2bf2SGreg Kroah-Hartman /* Generic device matching functions that all busses can use to match with */
1365aee2bf2SGreg Kroah-Hartman int device_match_name(struct device *dev, const void *name);
1375aee2bf2SGreg Kroah-Hartman int device_match_type(struct device *dev, const void *type);
1385aee2bf2SGreg Kroah-Hartman int device_match_of_node(struct device *dev, const void *np);
139a164ff53SAndy Shevchenko int device_match_fwnode(struct device *dev, const void *fwnode);
1405aee2bf2SGreg Kroah-Hartman int device_match_devt(struct device *dev, const void *pdevt);
1415aee2bf2SGreg Kroah-Hartman int device_match_acpi_dev(struct device *dev, const void *adev);
142*767b74e0SZijun Hu int device_match_acpi_handle(struct device *dev, const void *handle);
143*767b74e0SZijun Hu int device_match_any(struct device *dev, const void *unused);
144*767b74e0SZijun Hu
1455aee2bf2SGreg Kroah-Hartman /* Device iterating function type for various driver core for_each APIs */
146*767b74e0SZijun Hu typedef int (*device_iter_t)(struct device *dev, void *data);
147*767b74e0SZijun Hu
148e0766ea4SGreg Kroah-Hartman /* iterator helpers for buses */
149b45ed06fSZijun Hu int bus_for_each_dev(const struct bus_type *bus, struct device *start,
1505aee2bf2SGreg Kroah-Hartman void *data, device_iter_t fn);
1515aee2bf2SGreg Kroah-Hartman struct device *bus_find_device(const struct bus_type *bus, struct device *start,
1525aee2bf2SGreg Kroah-Hartman const void *data, device_match_t match);
1535aee2bf2SGreg Kroah-Hartman /**
1545aee2bf2SGreg Kroah-Hartman * bus_find_device_by_name - device iterator for locating a particular device
1555aee2bf2SGreg Kroah-Hartman * of a specific name.
1565aee2bf2SGreg Kroah-Hartman * @bus: bus type
157e0766ea4SGreg Kroah-Hartman * @start: Device to begin with
1585aee2bf2SGreg Kroah-Hartman * @name: name of the device to match
1595aee2bf2SGreg Kroah-Hartman */
bus_find_device_by_name(const struct bus_type * bus,struct device * start,const char * name)1605aee2bf2SGreg Kroah-Hartman static inline struct device *bus_find_device_by_name(const struct bus_type *bus,
1615aee2bf2SGreg Kroah-Hartman struct device *start,
1625aee2bf2SGreg Kroah-Hartman const char *name)
1635aee2bf2SGreg Kroah-Hartman {
1645aee2bf2SGreg Kroah-Hartman return bus_find_device(bus, start, name, device_match_name);
1655aee2bf2SGreg Kroah-Hartman }
1665aee2bf2SGreg Kroah-Hartman
1675aee2bf2SGreg Kroah-Hartman /**
1685aee2bf2SGreg Kroah-Hartman * bus_find_device_by_of_node : device iterator for locating a particular device
1695aee2bf2SGreg Kroah-Hartman * matching the of_node.
1705aee2bf2SGreg Kroah-Hartman * @bus: bus type
171e0766ea4SGreg Kroah-Hartman * @np: of_node of the device to match.
1725aee2bf2SGreg Kroah-Hartman */
1735aee2bf2SGreg Kroah-Hartman static inline struct device *
bus_find_device_by_of_node(const struct bus_type * bus,const struct device_node * np)1745aee2bf2SGreg Kroah-Hartman bus_find_device_by_of_node(const struct bus_type *bus, const struct device_node *np)
1755aee2bf2SGreg Kroah-Hartman {
1765aee2bf2SGreg Kroah-Hartman return bus_find_device(bus, NULL, np, device_match_of_node);
1775aee2bf2SGreg Kroah-Hartman }
1785aee2bf2SGreg Kroah-Hartman
1795aee2bf2SGreg Kroah-Hartman /**
1805aee2bf2SGreg Kroah-Hartman * bus_find_device_by_fwnode : device iterator for locating a particular device
1815aee2bf2SGreg Kroah-Hartman * matching the fwnode.
1825aee2bf2SGreg Kroah-Hartman * @bus: bus type
183e0766ea4SGreg Kroah-Hartman * @fwnode: fwnode of the device to match.
1845aee2bf2SGreg Kroah-Hartman */
1855aee2bf2SGreg Kroah-Hartman static inline struct device *
bus_find_device_by_fwnode(const struct bus_type * bus,const struct fwnode_handle * fwnode)1865aee2bf2SGreg Kroah-Hartman bus_find_device_by_fwnode(const struct bus_type *bus, const struct fwnode_handle *fwnode)
1875aee2bf2SGreg Kroah-Hartman {
1885aee2bf2SGreg Kroah-Hartman return bus_find_device(bus, NULL, fwnode, device_match_fwnode);
1895aee2bf2SGreg Kroah-Hartman }
1905aee2bf2SGreg Kroah-Hartman
1915aee2bf2SGreg Kroah-Hartman /**
1925aee2bf2SGreg Kroah-Hartman * bus_find_device_by_devt : device iterator for locating a particular device
1935aee2bf2SGreg Kroah-Hartman * matching the device type.
194e0766ea4SGreg Kroah-Hartman * @bus: bus type
1955aee2bf2SGreg Kroah-Hartman * @devt: device type of the device to match.
1965aee2bf2SGreg Kroah-Hartman */
bus_find_device_by_devt(const struct bus_type * bus,dev_t devt)1975aee2bf2SGreg Kroah-Hartman static inline struct device *bus_find_device_by_devt(const struct bus_type *bus,
1985aee2bf2SGreg Kroah-Hartman dev_t devt)
1995aee2bf2SGreg Kroah-Hartman {
2005aee2bf2SGreg Kroah-Hartman return bus_find_device(bus, NULL, &devt, device_match_devt);
2015aee2bf2SGreg Kroah-Hartman }
2025aee2bf2SGreg Kroah-Hartman
2035aee2bf2SGreg Kroah-Hartman /**
2045aee2bf2SGreg Kroah-Hartman * bus_find_next_device - Find the next device after a given device in a
2055aee2bf2SGreg Kroah-Hartman * given bus.
2065aee2bf2SGreg Kroah-Hartman * @bus: bus type
207e0766ea4SGreg Kroah-Hartman * @cur: device to begin the search with.
2085aee2bf2SGreg Kroah-Hartman */
2095aee2bf2SGreg Kroah-Hartman static inline struct device *
bus_find_next_device(const struct bus_type * bus,struct device * cur)2105aee2bf2SGreg Kroah-Hartman bus_find_next_device(const struct bus_type *bus,struct device *cur)
2115aee2bf2SGreg Kroah-Hartman {
2125aee2bf2SGreg Kroah-Hartman return bus_find_device(bus, cur, NULL, device_match_any);
2135aee2bf2SGreg Kroah-Hartman }
2145aee2bf2SGreg Kroah-Hartman
2155aee2bf2SGreg Kroah-Hartman #ifdef CONFIG_ACPI
2165aee2bf2SGreg Kroah-Hartman struct acpi_device;
2175aee2bf2SGreg Kroah-Hartman
2185aee2bf2SGreg Kroah-Hartman /**
2195aee2bf2SGreg Kroah-Hartman * bus_find_device_by_acpi_dev : device iterator for locating a particular device
2205aee2bf2SGreg Kroah-Hartman * matching the ACPI COMPANION device.
2215aee2bf2SGreg Kroah-Hartman * @bus: bus type
222e0766ea4SGreg Kroah-Hartman * @adev: ACPI COMPANION device to match.
2235aee2bf2SGreg Kroah-Hartman */
2245aee2bf2SGreg Kroah-Hartman static inline struct device *
bus_find_device_by_acpi_dev(const struct bus_type * bus,const struct acpi_device * adev)2255aee2bf2SGreg Kroah-Hartman bus_find_device_by_acpi_dev(const struct bus_type *bus, const struct acpi_device *adev)
2265aee2bf2SGreg Kroah-Hartman {
2275aee2bf2SGreg Kroah-Hartman return bus_find_device(bus, NULL, adev, device_match_acpi_dev);
228e0766ea4SGreg Kroah-Hartman }
2295aee2bf2SGreg Kroah-Hartman #else
2305aee2bf2SGreg Kroah-Hartman static inline struct device *
bus_find_device_by_acpi_dev(const struct bus_type * bus,const void * adev)2315aee2bf2SGreg Kroah-Hartman bus_find_device_by_acpi_dev(const struct bus_type *bus, const void *adev)
2325aee2bf2SGreg Kroah-Hartman {
2335aee2bf2SGreg Kroah-Hartman return NULL;
234e0766ea4SGreg Kroah-Hartman }
2355aee2bf2SGreg Kroah-Hartman #endif
2365ae81209SGreg Kroah-Hartman
2375aee2bf2SGreg Kroah-Hartman int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start,
2385aee2bf2SGreg Kroah-Hartman void *data, int (*fn)(struct device_driver *, void *));
2395aee2bf2SGreg Kroah-Hartman void bus_sort_breadthfirst(const struct bus_type *bus,
2405aee2bf2SGreg Kroah-Hartman int (*compare)(const struct device *a,
2415aee2bf2SGreg Kroah-Hartman const struct device *b));
2425aee2bf2SGreg Kroah-Hartman /*
2435aee2bf2SGreg Kroah-Hartman * Bus notifiers: Get notified of addition/removal of devices
2445aee2bf2SGreg Kroah-Hartman * and binding/unbinding of drivers to devices.
2455aee2bf2SGreg Kroah-Hartman * In the long run, it should be a replacement for the platform
2465aee2bf2SGreg Kroah-Hartman * notify hooks.
2470d62b79fSGreg Kroah-Hartman */
2480d62b79fSGreg Kroah-Hartman struct notifier_block;
2495aee2bf2SGreg Kroah-Hartman
250504450d0SGreg Kroah-Hartman int bus_register_notifier(const struct bus_type *bus, struct notifier_block *nb);
251504450d0SGreg Kroah-Hartman int bus_unregister_notifier(const struct bus_type *bus, struct notifier_block *nb);
252504450d0SGreg Kroah-Hartman
253504450d0SGreg Kroah-Hartman /**
254504450d0SGreg Kroah-Hartman * enum bus_notifier_event - Bus Notifier events that have happened
255504450d0SGreg Kroah-Hartman * @BUS_NOTIFY_ADD_DEVICE: device is added to this bus
256504450d0SGreg Kroah-Hartman * @BUS_NOTIFY_DEL_DEVICE: device is about to be removed from this bus
257504450d0SGreg Kroah-Hartman * @BUS_NOTIFY_REMOVED_DEVICE: device is successfully removed from this bus
258504450d0SGreg Kroah-Hartman * @BUS_NOTIFY_BIND_DRIVER: a driver is about to be bound to this device on this bus
259504450d0SGreg Kroah-Hartman * @BUS_NOTIFY_BOUND_DRIVER: a driver is successfully bound to this device on this bus
260504450d0SGreg Kroah-Hartman * @BUS_NOTIFY_UNBIND_DRIVER: a driver is about to be unbound from this device on this bus
261504450d0SGreg Kroah-Hartman * @BUS_NOTIFY_UNBOUND_DRIVER: a driver is successfully unbound from this device on this bus
262504450d0SGreg Kroah-Hartman * @BUS_NOTIFY_DRIVER_NOT_BOUND: a driver failed to be bound to this device on this bus
263504450d0SGreg Kroah-Hartman *
264504450d0SGreg Kroah-Hartman * These are the value passed to a bus notifier when a specific event happens.
265504450d0SGreg Kroah-Hartman *
266504450d0SGreg Kroah-Hartman * Note that bus notifiers are likely to be called with the device lock already
267504450d0SGreg Kroah-Hartman * held by the driver core, so be careful in any notifier callback as to what
2685aee2bf2SGreg Kroah-Hartman * you do with the device structure.
269504450d0SGreg Kroah-Hartman *
270504450d0SGreg Kroah-Hartman * All bus notifiers are called with the target struct device * as an argument.
271504450d0SGreg Kroah-Hartman */
272504450d0SGreg Kroah-Hartman enum bus_notifier_event {
273504450d0SGreg Kroah-Hartman BUS_NOTIFY_ADD_DEVICE,
274504450d0SGreg Kroah-Hartman BUS_NOTIFY_DEL_DEVICE,
275504450d0SGreg Kroah-Hartman BUS_NOTIFY_REMOVED_DEVICE,
276504450d0SGreg Kroah-Hartman BUS_NOTIFY_BIND_DRIVER,
277504450d0SGreg Kroah-Hartman BUS_NOTIFY_BOUND_DRIVER,
278504450d0SGreg Kroah-Hartman BUS_NOTIFY_UNBIND_DRIVER,
2795aee2bf2SGreg Kroah-Hartman BUS_NOTIFY_UNBOUND_DRIVER,
2800d62b79fSGreg Kroah-Hartman BUS_NOTIFY_DRIVER_NOT_BOUND,
2818c99377eSGreg Kroah-Hartman };
2825aee2bf2SGreg Kroah-Hartman
2835aee2bf2SGreg Kroah-Hartman 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