Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * bus.h - the bus-specific portions of the driver model
0004  *
0005  * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
0006  * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
0007  * Copyright (c) 2008-2009 Novell Inc.
0008  * Copyright (c) 2012-2019 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
0009  * Copyright (c) 2012-2019 Linux Foundation
0010  *
0011  * See Documentation/driver-api/driver-model/ for more information.
0012  */
0013 
0014 #ifndef _DEVICE_BUS_H_
0015 #define _DEVICE_BUS_H_
0016 
0017 #include <linux/kobject.h>
0018 #include <linux/klist.h>
0019 #include <linux/pm.h>
0020 
0021 struct device_driver;
0022 struct fwnode_handle;
0023 
0024 /**
0025  * struct bus_type - The bus type of the device
0026  *
0027  * @name:   The name of the bus.
0028  * @dev_name:   Used for subsystems to enumerate devices like ("foo%u", dev->id).
0029  * @dev_root:   Default device to use as the parent.
0030  * @bus_groups: Default attributes of the bus.
0031  * @dev_groups: Default attributes of the devices on the bus.
0032  * @drv_groups: Default attributes of the device drivers on the bus.
0033  * @match:  Called, perhaps multiple times, whenever a new device or driver
0034  *      is added for this bus. It should return a positive value if the
0035  *      given device can be handled by the given driver and zero
0036  *      otherwise. It may also return error code if determining that
0037  *      the driver supports the device is not possible. In case of
0038  *      -EPROBE_DEFER it will queue the device for deferred probing.
0039  * @uevent: Called when a device is added, removed, or a few other things
0040  *      that generate uevents to add the environment variables.
0041  * @probe:  Called when a new device or driver add to this bus, and callback
0042  *      the specific driver's probe to initial the matched device.
0043  * @sync_state: Called to sync device state to software state after all the
0044  *      state tracking consumers linked to this device (present at
0045  *      the time of late_initcall) have successfully bound to a
0046  *      driver. If the device has no consumers, this function will
0047  *      be called at late_initcall_sync level. If the device has
0048  *      consumers that are never bound to a driver, this function
0049  *      will never get called until they do.
0050  * @remove: Called when a device removed from this bus.
0051  * @shutdown:   Called at shut-down time to quiesce the device.
0052  *
0053  * @online: Called to put the device back online (after offlining it).
0054  * @offline:    Called to put the device offline for hot-removal. May fail.
0055  *
0056  * @suspend:    Called when a device on this bus wants to go to sleep mode.
0057  * @resume: Called to bring a device on this bus out of sleep mode.
0058  * @num_vf: Called to find out how many virtual functions a device on this
0059  *      bus supports.
0060  * @dma_configure:  Called to setup DMA configuration on a device on
0061  *          this bus.
0062  * @dma_cleanup:    Called to cleanup DMA configuration on a device on
0063  *          this bus.
0064  * @pm:     Power management operations of this bus, callback the specific
0065  *      device driver's pm-ops.
0066  * @iommu_ops:  IOMMU specific operations for this bus, used to attach IOMMU
0067  *              driver implementations to a bus and allow the driver to do
0068  *              bus-specific setup
0069  * @p:      The private data of the driver core, only the driver core can
0070  *      touch this.
0071  * @lock_key:   Lock class key for use by the lock validator
0072  * @need_parent_lock:   When probing or removing a device on this bus, the
0073  *          device core should lock the device's parent.
0074  *
0075  * A bus is a channel between the processor and one or more devices. For the
0076  * purposes of the device model, all devices are connected via a bus, even if
0077  * it is an internal, virtual, "platform" bus. Buses can plug into each other.
0078  * A USB controller is usually a PCI device, for example. The device model
0079  * represents the actual connections between buses and the devices they control.
0080  * A bus is represented by the bus_type structure. It contains the name, the
0081  * default attributes, the bus' methods, PM operations, and the driver core's
0082  * private data.
0083  */
0084 struct bus_type {
0085     const char      *name;
0086     const char      *dev_name;
0087     struct device       *dev_root;
0088     const struct attribute_group **bus_groups;
0089     const struct attribute_group **dev_groups;
0090     const struct attribute_group **drv_groups;
0091 
0092     int (*match)(struct device *dev, struct device_driver *drv);
0093     int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
0094     int (*probe)(struct device *dev);
0095     void (*sync_state)(struct device *dev);
0096     void (*remove)(struct device *dev);
0097     void (*shutdown)(struct device *dev);
0098 
0099     int (*online)(struct device *dev);
0100     int (*offline)(struct device *dev);
0101 
0102     int (*suspend)(struct device *dev, pm_message_t state);
0103     int (*resume)(struct device *dev);
0104 
0105     int (*num_vf)(struct device *dev);
0106 
0107     int (*dma_configure)(struct device *dev);
0108     void (*dma_cleanup)(struct device *dev);
0109 
0110     const struct dev_pm_ops *pm;
0111 
0112     const struct iommu_ops *iommu_ops;
0113 
0114     struct subsys_private *p;
0115     struct lock_class_key lock_key;
0116 
0117     bool need_parent_lock;
0118 };
0119 
0120 extern int __must_check bus_register(struct bus_type *bus);
0121 
0122 extern void bus_unregister(struct bus_type *bus);
0123 
0124 extern int __must_check bus_rescan_devices(struct bus_type *bus);
0125 
0126 struct bus_attribute {
0127     struct attribute    attr;
0128     ssize_t (*show)(struct bus_type *bus, char *buf);
0129     ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
0130 };
0131 
0132 #define BUS_ATTR_RW(_name) \
0133     struct bus_attribute bus_attr_##_name = __ATTR_RW(_name)
0134 #define BUS_ATTR_RO(_name) \
0135     struct bus_attribute bus_attr_##_name = __ATTR_RO(_name)
0136 #define BUS_ATTR_WO(_name) \
0137     struct bus_attribute bus_attr_##_name = __ATTR_WO(_name)
0138 
0139 extern int __must_check bus_create_file(struct bus_type *,
0140                     struct bus_attribute *);
0141 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
0142 
0143 /* Generic device matching functions that all busses can use to match with */
0144 int device_match_name(struct device *dev, const void *name);
0145 int device_match_of_node(struct device *dev, const void *np);
0146 int device_match_fwnode(struct device *dev, const void *fwnode);
0147 int device_match_devt(struct device *dev, const void *pdevt);
0148 int device_match_acpi_dev(struct device *dev, const void *adev);
0149 int device_match_acpi_handle(struct device *dev, const void *handle);
0150 int device_match_any(struct device *dev, const void *unused);
0151 
0152 /* iterator helpers for buses */
0153 struct subsys_dev_iter {
0154     struct klist_iter       ki;
0155     const struct device_type    *type;
0156 };
0157 void subsys_dev_iter_init(struct subsys_dev_iter *iter,
0158              struct bus_type *subsys,
0159              struct device *start,
0160              const struct device_type *type);
0161 struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter);
0162 void subsys_dev_iter_exit(struct subsys_dev_iter *iter);
0163 
0164 int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
0165              int (*fn)(struct device *dev, void *data));
0166 struct device *bus_find_device(struct bus_type *bus, struct device *start,
0167                    const void *data,
0168                    int (*match)(struct device *dev, const void *data));
0169 /**
0170  * bus_find_device_by_name - device iterator for locating a particular device
0171  * of a specific name.
0172  * @bus: bus type
0173  * @start: Device to begin with
0174  * @name: name of the device to match
0175  */
0176 static inline struct device *bus_find_device_by_name(struct bus_type *bus,
0177                              struct device *start,
0178                              const char *name)
0179 {
0180     return bus_find_device(bus, start, name, device_match_name);
0181 }
0182 
0183 /**
0184  * bus_find_device_by_of_node : device iterator for locating a particular device
0185  * matching the of_node.
0186  * @bus: bus type
0187  * @np: of_node of the device to match.
0188  */
0189 static inline struct device *
0190 bus_find_device_by_of_node(struct bus_type *bus, const struct device_node *np)
0191 {
0192     return bus_find_device(bus, NULL, np, device_match_of_node);
0193 }
0194 
0195 /**
0196  * bus_find_device_by_fwnode : device iterator for locating a particular device
0197  * matching the fwnode.
0198  * @bus: bus type
0199  * @fwnode: fwnode of the device to match.
0200  */
0201 static inline struct device *
0202 bus_find_device_by_fwnode(struct bus_type *bus, const struct fwnode_handle *fwnode)
0203 {
0204     return bus_find_device(bus, NULL, fwnode, device_match_fwnode);
0205 }
0206 
0207 /**
0208  * bus_find_device_by_devt : device iterator for locating a particular device
0209  * matching the device type.
0210  * @bus: bus type
0211  * @devt: device type of the device to match.
0212  */
0213 static inline struct device *bus_find_device_by_devt(struct bus_type *bus,
0214                              dev_t devt)
0215 {
0216     return bus_find_device(bus, NULL, &devt, device_match_devt);
0217 }
0218 
0219 /**
0220  * bus_find_next_device - Find the next device after a given device in a
0221  * given bus.
0222  * @bus: bus type
0223  * @cur: device to begin the search with.
0224  */
0225 static inline struct device *
0226 bus_find_next_device(struct bus_type *bus,struct device *cur)
0227 {
0228     return bus_find_device(bus, cur, NULL, device_match_any);
0229 }
0230 
0231 #ifdef CONFIG_ACPI
0232 struct acpi_device;
0233 
0234 /**
0235  * bus_find_device_by_acpi_dev : device iterator for locating a particular device
0236  * matching the ACPI COMPANION device.
0237  * @bus: bus type
0238  * @adev: ACPI COMPANION device to match.
0239  */
0240 static inline struct device *
0241 bus_find_device_by_acpi_dev(struct bus_type *bus, const struct acpi_device *adev)
0242 {
0243     return bus_find_device(bus, NULL, adev, device_match_acpi_dev);
0244 }
0245 #else
0246 static inline struct device *
0247 bus_find_device_by_acpi_dev(struct bus_type *bus, const void *adev)
0248 {
0249     return NULL;
0250 }
0251 #endif
0252 
0253 struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id,
0254                     struct device *hint);
0255 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
0256              void *data, int (*fn)(struct device_driver *, void *));
0257 void bus_sort_breadthfirst(struct bus_type *bus,
0258                int (*compare)(const struct device *a,
0259                       const struct device *b));
0260 /*
0261  * Bus notifiers: Get notified of addition/removal of devices
0262  * and binding/unbinding of drivers to devices.
0263  * In the long run, it should be a replacement for the platform
0264  * notify hooks.
0265  */
0266 struct notifier_block;
0267 
0268 extern int bus_register_notifier(struct bus_type *bus,
0269                  struct notifier_block *nb);
0270 extern int bus_unregister_notifier(struct bus_type *bus,
0271                    struct notifier_block *nb);
0272 
0273 /* All 4 notifers below get called with the target struct device *
0274  * as an argument. Note that those functions are likely to be called
0275  * with the device lock held in the core, so be careful.
0276  */
0277 #define BUS_NOTIFY_ADD_DEVICE       0x00000001 /* device added */
0278 #define BUS_NOTIFY_DEL_DEVICE       0x00000002 /* device to be removed */
0279 #define BUS_NOTIFY_REMOVED_DEVICE   0x00000003 /* device removed */
0280 #define BUS_NOTIFY_BIND_DRIVER      0x00000004 /* driver about to be
0281                               bound */
0282 #define BUS_NOTIFY_BOUND_DRIVER     0x00000005 /* driver bound to device */
0283 #define BUS_NOTIFY_UNBIND_DRIVER    0x00000006 /* driver about to be
0284                               unbound */
0285 #define BUS_NOTIFY_UNBOUND_DRIVER   0x00000007 /* driver is unbound
0286                               from the device */
0287 #define BUS_NOTIFY_DRIVER_NOT_BOUND 0x00000008 /* driver fails to be bound */
0288 
0289 extern struct kset *bus_get_kset(struct bus_type *bus);
0290 extern struct klist *bus_get_device_klist(struct bus_type *bus);
0291 
0292 #endif