Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * The driver-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_DRIVER_H_
0015 #define _DEVICE_DRIVER_H_
0016 
0017 #include <linux/kobject.h>
0018 #include <linux/klist.h>
0019 #include <linux/pm.h>
0020 #include <linux/device/bus.h>
0021 #include <linux/module.h>
0022 
0023 /**
0024  * enum probe_type - device driver probe type to try
0025  *  Device drivers may opt in for special handling of their
0026  *  respective probe routines. This tells the core what to
0027  *  expect and prefer.
0028  *
0029  * @PROBE_DEFAULT_STRATEGY: Used by drivers that work equally well
0030  *  whether probed synchronously or asynchronously.
0031  * @PROBE_PREFER_ASYNCHRONOUS: Drivers for "slow" devices which
0032  *  probing order is not essential for booting the system may
0033  *  opt into executing their probes asynchronously.
0034  * @PROBE_FORCE_SYNCHRONOUS: Use this to annotate drivers that need
0035  *  their probe routines to run synchronously with driver and
0036  *  device registration (with the exception of -EPROBE_DEFER
0037  *  handling - re-probing always ends up being done asynchronously).
0038  *
0039  * Note that the end goal is to switch the kernel to use asynchronous
0040  * probing by default, so annotating drivers with
0041  * %PROBE_PREFER_ASYNCHRONOUS is a temporary measure that allows us
0042  * to speed up boot process while we are validating the rest of the
0043  * drivers.
0044  */
0045 enum probe_type {
0046     PROBE_DEFAULT_STRATEGY,
0047     PROBE_PREFER_ASYNCHRONOUS,
0048     PROBE_FORCE_SYNCHRONOUS,
0049 };
0050 
0051 /**
0052  * struct device_driver - The basic device driver structure
0053  * @name:   Name of the device driver.
0054  * @bus:    The bus which the device of this driver belongs to.
0055  * @owner:  The module owner.
0056  * @mod_name:   Used for built-in modules.
0057  * @suppress_bind_attrs: Disables bind/unbind via sysfs.
0058  * @probe_type: Type of the probe (synchronous or asynchronous) to use.
0059  * @of_match_table: The open firmware table.
0060  * @acpi_match_table: The ACPI match table.
0061  * @probe:  Called to query the existence of a specific device,
0062  *      whether this driver can work with it, and bind the driver
0063  *      to a specific device.
0064  * @sync_state: Called to sync device state to software state after all the
0065  *      state tracking consumers linked to this device (present at
0066  *      the time of late_initcall) have successfully bound to a
0067  *      driver. If the device has no consumers, this function will
0068  *      be called at late_initcall_sync level. If the device has
0069  *      consumers that are never bound to a driver, this function
0070  *      will never get called until they do.
0071  * @remove: Called when the device is removed from the system to
0072  *      unbind a device from this driver.
0073  * @shutdown:   Called at shut-down time to quiesce the device.
0074  * @suspend:    Called to put the device to sleep mode. Usually to a
0075  *      low power state.
0076  * @resume: Called to bring a device from sleep mode.
0077  * @groups: Default attributes that get created by the driver core
0078  *      automatically.
0079  * @dev_groups: Additional attributes attached to device instance once
0080  *      it is bound to the driver.
0081  * @pm:     Power management operations of the device which matched
0082  *      this driver.
0083  * @coredump:   Called when sysfs entry is written to. The device driver
0084  *      is expected to call the dev_coredump API resulting in a
0085  *      uevent.
0086  * @p:      Driver core's private data, no one other than the driver
0087  *      core can touch this.
0088  *
0089  * The device driver-model tracks all of the drivers known to the system.
0090  * The main reason for this tracking is to enable the driver core to match
0091  * up drivers with new devices. Once drivers are known objects within the
0092  * system, however, a number of other things become possible. Device drivers
0093  * can export information and configuration variables that are independent
0094  * of any specific device.
0095  */
0096 struct device_driver {
0097     const char      *name;
0098     struct bus_type     *bus;
0099 
0100     struct module       *owner;
0101     const char      *mod_name;  /* used for built-in modules */
0102 
0103     bool suppress_bind_attrs;   /* disables bind/unbind via sysfs */
0104     enum probe_type probe_type;
0105 
0106     const struct of_device_id   *of_match_table;
0107     const struct acpi_device_id *acpi_match_table;
0108 
0109     int (*probe) (struct device *dev);
0110     void (*sync_state)(struct device *dev);
0111     int (*remove) (struct device *dev);
0112     void (*shutdown) (struct device *dev);
0113     int (*suspend) (struct device *dev, pm_message_t state);
0114     int (*resume) (struct device *dev);
0115     const struct attribute_group **groups;
0116     const struct attribute_group **dev_groups;
0117 
0118     const struct dev_pm_ops *pm;
0119     void (*coredump) (struct device *dev);
0120 
0121     struct driver_private *p;
0122 };
0123 
0124 
0125 extern int __must_check driver_register(struct device_driver *drv);
0126 extern void driver_unregister(struct device_driver *drv);
0127 
0128 extern struct device_driver *driver_find(const char *name,
0129                      struct bus_type *bus);
0130 extern int driver_probe_done(void);
0131 extern void wait_for_device_probe(void);
0132 void __init wait_for_init_devices_probe(void);
0133 
0134 /* sysfs interface for exporting driver attributes */
0135 
0136 struct driver_attribute {
0137     struct attribute attr;
0138     ssize_t (*show)(struct device_driver *driver, char *buf);
0139     ssize_t (*store)(struct device_driver *driver, const char *buf,
0140              size_t count);
0141 };
0142 
0143 #define DRIVER_ATTR_RW(_name) \
0144     struct driver_attribute driver_attr_##_name = __ATTR_RW(_name)
0145 #define DRIVER_ATTR_RO(_name) \
0146     struct driver_attribute driver_attr_##_name = __ATTR_RO(_name)
0147 #define DRIVER_ATTR_WO(_name) \
0148     struct driver_attribute driver_attr_##_name = __ATTR_WO(_name)
0149 
0150 extern int __must_check driver_create_file(struct device_driver *driver,
0151                     const struct driver_attribute *attr);
0152 extern void driver_remove_file(struct device_driver *driver,
0153                    const struct driver_attribute *attr);
0154 
0155 int driver_set_override(struct device *dev, const char **override,
0156             const char *s, size_t len);
0157 extern int __must_check driver_for_each_device(struct device_driver *drv,
0158                            struct device *start,
0159                            void *data,
0160                            int (*fn)(struct device *dev,
0161                              void *));
0162 struct device *driver_find_device(struct device_driver *drv,
0163                   struct device *start, const void *data,
0164                   int (*match)(struct device *dev, const void *data));
0165 
0166 /**
0167  * driver_find_device_by_name - device iterator for locating a particular device
0168  * of a specific name.
0169  * @drv: the driver we're iterating
0170  * @name: name of the device to match
0171  */
0172 static inline struct device *driver_find_device_by_name(struct device_driver *drv,
0173                             const char *name)
0174 {
0175     return driver_find_device(drv, NULL, name, device_match_name);
0176 }
0177 
0178 /**
0179  * driver_find_device_by_of_node- device iterator for locating a particular device
0180  * by of_node pointer.
0181  * @drv: the driver we're iterating
0182  * @np: of_node pointer to match.
0183  */
0184 static inline struct device *
0185 driver_find_device_by_of_node(struct device_driver *drv,
0186                   const struct device_node *np)
0187 {
0188     return driver_find_device(drv, NULL, np, device_match_of_node);
0189 }
0190 
0191 /**
0192  * driver_find_device_by_fwnode- device iterator for locating a particular device
0193  * by fwnode pointer.
0194  * @drv: the driver we're iterating
0195  * @fwnode: fwnode pointer to match.
0196  */
0197 static inline struct device *
0198 driver_find_device_by_fwnode(struct device_driver *drv,
0199                  const struct fwnode_handle *fwnode)
0200 {
0201     return driver_find_device(drv, NULL, fwnode, device_match_fwnode);
0202 }
0203 
0204 /**
0205  * driver_find_device_by_devt- device iterator for locating a particular device
0206  * by devt.
0207  * @drv: the driver we're iterating
0208  * @devt: devt pointer to match.
0209  */
0210 static inline struct device *driver_find_device_by_devt(struct device_driver *drv,
0211                             dev_t devt)
0212 {
0213     return driver_find_device(drv, NULL, &devt, device_match_devt);
0214 }
0215 
0216 static inline struct device *driver_find_next_device(struct device_driver *drv,
0217                              struct device *start)
0218 {
0219     return driver_find_device(drv, start, NULL, device_match_any);
0220 }
0221 
0222 #ifdef CONFIG_ACPI
0223 /**
0224  * driver_find_device_by_acpi_dev : device iterator for locating a particular
0225  * device matching the ACPI_COMPANION device.
0226  * @drv: the driver we're iterating
0227  * @adev: ACPI_COMPANION device to match.
0228  */
0229 static inline struct device *
0230 driver_find_device_by_acpi_dev(struct device_driver *drv,
0231                    const struct acpi_device *adev)
0232 {
0233     return driver_find_device(drv, NULL, adev, device_match_acpi_dev);
0234 }
0235 #else
0236 static inline struct device *
0237 driver_find_device_by_acpi_dev(struct device_driver *drv, const void *adev)
0238 {
0239     return NULL;
0240 }
0241 #endif
0242 
0243 extern int driver_deferred_probe_timeout;
0244 void driver_deferred_probe_add(struct device *dev);
0245 int driver_deferred_probe_check_state(struct device *dev);
0246 void driver_init(void);
0247 
0248 /**
0249  * module_driver() - Helper macro for drivers that don't do anything
0250  * special in module init/exit. This eliminates a lot of boilerplate.
0251  * Each module may only use this macro once, and calling it replaces
0252  * module_init() and module_exit().
0253  *
0254  * @__driver: driver name
0255  * @__register: register function for this driver type
0256  * @__unregister: unregister function for this driver type
0257  * @...: Additional arguments to be passed to __register and __unregister.
0258  *
0259  * Use this macro to construct bus specific macros for registering
0260  * drivers, and do not use it on its own.
0261  */
0262 #define module_driver(__driver, __register, __unregister, ...) \
0263 static int __init __driver##_init(void) \
0264 { \
0265     return __register(&(__driver) , ##__VA_ARGS__); \
0266 } \
0267 module_init(__driver##_init); \
0268 static void __exit __driver##_exit(void) \
0269 { \
0270     __unregister(&(__driver) , ##__VA_ARGS__); \
0271 } \
0272 module_exit(__driver##_exit);
0273 
0274 /**
0275  * builtin_driver() - Helper macro for drivers that don't do anything
0276  * special in init and have no exit. This eliminates some boilerplate.
0277  * Each driver may only use this macro once, and calling it replaces
0278  * device_initcall (or in some cases, the legacy __initcall).  This is
0279  * meant to be a direct parallel of module_driver() above but without
0280  * the __exit stuff that is not used for builtin cases.
0281  *
0282  * @__driver: driver name
0283  * @__register: register function for this driver type
0284  * @...: Additional arguments to be passed to __register
0285  *
0286  * Use this macro to construct bus specific macros for registering
0287  * drivers, and do not use it on its own.
0288  */
0289 #define builtin_driver(__driver, __register, ...) \
0290 static int __init __driver##_init(void) \
0291 { \
0292     return __register(&(__driver) , ##__VA_ARGS__); \
0293 } \
0294 device_initcall(__driver##_init);
0295 
0296 #endif  /* _DEVICE_DRIVER_H_ */