14c002c97SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
24c002c97SGreg Kroah-Hartman /*
34c002c97SGreg Kroah-Hartman * The driver-specific portions of the driver model
44c002c97SGreg Kroah-Hartman *
54c002c97SGreg Kroah-Hartman * Copyright (c) 2001-2003 Patrick Mochel <[email protected]>
64c002c97SGreg Kroah-Hartman * Copyright (c) 2004-2009 Greg Kroah-Hartman <[email protected]>
74c002c97SGreg Kroah-Hartman * Copyright (c) 2008-2009 Novell Inc.
84c002c97SGreg Kroah-Hartman * Copyright (c) 2012-2019 Greg Kroah-Hartman <[email protected]>
94c002c97SGreg Kroah-Hartman * Copyright (c) 2012-2019 Linux Foundation
104c002c97SGreg Kroah-Hartman *
114c002c97SGreg Kroah-Hartman * See Documentation/driver-api/driver-model/ for more information.
124c002c97SGreg Kroah-Hartman */
134c002c97SGreg Kroah-Hartman
144c002c97SGreg Kroah-Hartman #ifndef _DEVICE_DRIVER_H_
154c002c97SGreg Kroah-Hartman #define _DEVICE_DRIVER_H_
164c002c97SGreg Kroah-Hartman
174c002c97SGreg Kroah-Hartman #include <linux/kobject.h>
184c002c97SGreg Kroah-Hartman #include <linux/klist.h>
194c002c97SGreg Kroah-Hartman #include <linux/pm.h>
204c002c97SGreg Kroah-Hartman #include <linux/device/bus.h>
218581fd40SJakub Kicinski #include <linux/module.h>
224c002c97SGreg Kroah-Hartman
234c002c97SGreg Kroah-Hartman /**
244c002c97SGreg Kroah-Hartman * enum probe_type - device driver probe type to try
254c002c97SGreg Kroah-Hartman * Device drivers may opt in for special handling of their
264c002c97SGreg Kroah-Hartman * respective probe routines. This tells the core what to
274c002c97SGreg Kroah-Hartman * expect and prefer.
284c002c97SGreg Kroah-Hartman *
294c002c97SGreg Kroah-Hartman * @PROBE_DEFAULT_STRATEGY: Used by drivers that work equally well
304c002c97SGreg Kroah-Hartman * whether probed synchronously or asynchronously.
314c002c97SGreg Kroah-Hartman * @PROBE_PREFER_ASYNCHRONOUS: Drivers for "slow" devices which
324c002c97SGreg Kroah-Hartman * probing order is not essential for booting the system may
334c002c97SGreg Kroah-Hartman * opt into executing their probes asynchronously.
344c002c97SGreg Kroah-Hartman * @PROBE_FORCE_SYNCHRONOUS: Use this to annotate drivers that need
354c002c97SGreg Kroah-Hartman * their probe routines to run synchronously with driver and
364c002c97SGreg Kroah-Hartman * device registration (with the exception of -EPROBE_DEFER
374c002c97SGreg Kroah-Hartman * handling - re-probing always ends up being done asynchronously).
384c002c97SGreg Kroah-Hartman *
394c002c97SGreg Kroah-Hartman * Note that the end goal is to switch the kernel to use asynchronous
404c002c97SGreg Kroah-Hartman * probing by default, so annotating drivers with
414c002c97SGreg Kroah-Hartman * %PROBE_PREFER_ASYNCHRONOUS is a temporary measure that allows us
424c002c97SGreg Kroah-Hartman * to speed up boot process while we are validating the rest of the
434c002c97SGreg Kroah-Hartman * drivers.
444c002c97SGreg Kroah-Hartman */
454c002c97SGreg Kroah-Hartman enum probe_type {
464c002c97SGreg Kroah-Hartman PROBE_DEFAULT_STRATEGY,
474c002c97SGreg Kroah-Hartman PROBE_PREFER_ASYNCHRONOUS,
484c002c97SGreg Kroah-Hartman PROBE_FORCE_SYNCHRONOUS,
494c002c97SGreg Kroah-Hartman };
504c002c97SGreg Kroah-Hartman
514c002c97SGreg Kroah-Hartman /**
524c002c97SGreg Kroah-Hartman * struct device_driver - The basic device driver structure
534c002c97SGreg Kroah-Hartman * @name: Name of the device driver.
544c002c97SGreg Kroah-Hartman * @bus: The bus which the device of this driver belongs to.
554c002c97SGreg Kroah-Hartman * @owner: The module owner.
564c002c97SGreg Kroah-Hartman * @mod_name: Used for built-in modules.
574c002c97SGreg Kroah-Hartman * @suppress_bind_attrs: Disables bind/unbind via sysfs.
584c002c97SGreg Kroah-Hartman * @probe_type: Type of the probe (synchronous or asynchronous) to use.
594c002c97SGreg Kroah-Hartman * @of_match_table: The open firmware table.
604c002c97SGreg Kroah-Hartman * @acpi_match_table: The ACPI match table.
614c002c97SGreg Kroah-Hartman * @probe: Called to query the existence of a specific device,
624c002c97SGreg Kroah-Hartman * whether this driver can work with it, and bind the driver
634c002c97SGreg Kroah-Hartman * to a specific device.
644c002c97SGreg Kroah-Hartman * @sync_state: Called to sync device state to software state after all the
654c002c97SGreg Kroah-Hartman * state tracking consumers linked to this device (present at
664c002c97SGreg Kroah-Hartman * the time of late_initcall) have successfully bound to a
674c002c97SGreg Kroah-Hartman * driver. If the device has no consumers, this function will
684c002c97SGreg Kroah-Hartman * be called at late_initcall_sync level. If the device has
694c002c97SGreg Kroah-Hartman * consumers that are never bound to a driver, this function
704c002c97SGreg Kroah-Hartman * will never get called until they do.
714c002c97SGreg Kroah-Hartman * @remove: Called when the device is removed from the system to
724c002c97SGreg Kroah-Hartman * unbind a device from this driver.
734c002c97SGreg Kroah-Hartman * @shutdown: Called at shut-down time to quiesce the device.
744c002c97SGreg Kroah-Hartman * @suspend: Called to put the device to sleep mode. Usually to a
754c002c97SGreg Kroah-Hartman * low power state.
764c002c97SGreg Kroah-Hartman * @resume: Called to bring a device from sleep mode.
774c002c97SGreg Kroah-Hartman * @groups: Default attributes that get created by the driver core
784c002c97SGreg Kroah-Hartman * automatically.
795c3db63aSGeert Uytterhoeven * @dev_groups: Additional attributes attached to device instance once
804c002c97SGreg Kroah-Hartman * it is bound to the driver.
814c002c97SGreg Kroah-Hartman * @pm: Power management operations of the device which matched
824c002c97SGreg Kroah-Hartman * this driver.
834c002c97SGreg Kroah-Hartman * @coredump: Called when sysfs entry is written to. The device driver
844c002c97SGreg Kroah-Hartman * is expected to call the dev_coredump API resulting in a
854c002c97SGreg Kroah-Hartman * uevent.
864c002c97SGreg Kroah-Hartman * @p: Driver core's private data, no one other than the driver
874c002c97SGreg Kroah-Hartman * core can touch this.
884c002c97SGreg Kroah-Hartman *
894c002c97SGreg Kroah-Hartman * The device driver-model tracks all of the drivers known to the system.
904c002c97SGreg Kroah-Hartman * The main reason for this tracking is to enable the driver core to match
914c002c97SGreg Kroah-Hartman * up drivers with new devices. Once drivers are known objects within the
924c002c97SGreg Kroah-Hartman * system, however, a number of other things become possible. Device drivers
934c002c97SGreg Kroah-Hartman * can export information and configuration variables that are independent
944c002c97SGreg Kroah-Hartman * of any specific device.
954c002c97SGreg Kroah-Hartman */
964c002c97SGreg Kroah-Hartman struct device_driver {
974c002c97SGreg Kroah-Hartman const char *name;
98c28dd08eSGreg Kroah-Hartman const struct bus_type *bus;
994c002c97SGreg Kroah-Hartman
1004c002c97SGreg Kroah-Hartman struct module *owner;
1014c002c97SGreg Kroah-Hartman const char *mod_name; /* used for built-in modules */
1024c002c97SGreg Kroah-Hartman
1034c002c97SGreg Kroah-Hartman bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
1044c002c97SGreg Kroah-Hartman enum probe_type probe_type;
1054c002c97SGreg Kroah-Hartman
1064c002c97SGreg Kroah-Hartman const struct of_device_id *of_match_table;
1074c002c97SGreg Kroah-Hartman const struct acpi_device_id *acpi_match_table;
1084c002c97SGreg Kroah-Hartman
1094c002c97SGreg Kroah-Hartman int (*probe) (struct device *dev);
1104c002c97SGreg Kroah-Hartman void (*sync_state)(struct device *dev);
1114c002c97SGreg Kroah-Hartman int (*remove) (struct device *dev);
1124c002c97SGreg Kroah-Hartman void (*shutdown) (struct device *dev);
1134c002c97SGreg Kroah-Hartman int (*suspend) (struct device *dev, pm_message_t state);
1144c002c97SGreg Kroah-Hartman int (*resume) (struct device *dev);
1154c002c97SGreg Kroah-Hartman const struct attribute_group **groups;
1164c002c97SGreg Kroah-Hartman const struct attribute_group **dev_groups;
1174c002c97SGreg Kroah-Hartman
1184c002c97SGreg Kroah-Hartman const struct dev_pm_ops *pm;
1194c002c97SGreg Kroah-Hartman void (*coredump) (struct device *dev);
1204c002c97SGreg Kroah-Hartman
1214c002c97SGreg Kroah-Hartman struct driver_private *p;
1224c002c97SGreg Kroah-Hartman };
1234c002c97SGreg Kroah-Hartman
1244c002c97SGreg Kroah-Hartman
1258a2b9c84SGreg Kroah-Hartman int __must_check driver_register(struct device_driver *drv);
1268a2b9c84SGreg Kroah-Hartman void driver_unregister(struct device_driver *drv);
1274c002c97SGreg Kroah-Hartman
1288a2b9c84SGreg Kroah-Hartman struct device_driver *driver_find(const char *name, const struct bus_type *bus);
129aa5f6ed8SChristoph Hellwig bool __init driver_probe_done(void);
1308a2b9c84SGreg Kroah-Hartman void wait_for_device_probe(void);
1312f8c3ae8SSaravana Kannan void __init wait_for_init_devices_probe(void);
1324c002c97SGreg Kroah-Hartman
1334c002c97SGreg Kroah-Hartman /* sysfs interface for exporting driver attributes */
1344c002c97SGreg Kroah-Hartman
1354c002c97SGreg Kroah-Hartman struct driver_attribute {
1364c002c97SGreg Kroah-Hartman struct attribute attr;
1374c002c97SGreg Kroah-Hartman ssize_t (*show)(struct device_driver *driver, char *buf);
1384c002c97SGreg Kroah-Hartman ssize_t (*store)(struct device_driver *driver, const char *buf,
1394c002c97SGreg Kroah-Hartman size_t count);
1404c002c97SGreg Kroah-Hartman };
1414c002c97SGreg Kroah-Hartman
1424c002c97SGreg Kroah-Hartman #define DRIVER_ATTR_RW(_name) \
1434c002c97SGreg Kroah-Hartman struct driver_attribute driver_attr_##_name = __ATTR_RW(_name)
1444c002c97SGreg Kroah-Hartman #define DRIVER_ATTR_RO(_name) \
1454c002c97SGreg Kroah-Hartman struct driver_attribute driver_attr_##_name = __ATTR_RO(_name)
1464c002c97SGreg Kroah-Hartman #define DRIVER_ATTR_WO(_name) \
1474c002c97SGreg Kroah-Hartman struct driver_attribute driver_attr_##_name = __ATTR_WO(_name)
1484c002c97SGreg Kroah-Hartman
149ab7a8802SGreg Kroah-Hartman int __must_check driver_create_file(const struct device_driver *driver,
1504c002c97SGreg Kroah-Hartman const struct driver_attribute *attr);
151ab7a8802SGreg Kroah-Hartman void driver_remove_file(const struct device_driver *driver,
1524c002c97SGreg Kroah-Hartman const struct driver_attribute *attr);
1534c002c97SGreg Kroah-Hartman
1546c2f4211SKrzysztof Kozlowski int driver_set_override(struct device *dev, const char **override,
1556c2f4211SKrzysztof Kozlowski const char *s, size_t len);
1568a2b9c84SGreg Kroah-Hartman int __must_check driver_for_each_device(struct device_driver *drv, struct device *start,
157*767b74e0SZijun Hu void *data, device_iter_t fn);
158f8fb4691SGreg Kroah-Hartman struct device *driver_find_device(const struct device_driver *drv,
1594c002c97SGreg Kroah-Hartman struct device *start, const void *data,
160b45ed06fSZijun Hu device_match_t match);
1614c002c97SGreg Kroah-Hartman
1624c002c97SGreg Kroah-Hartman /**
1634c002c97SGreg Kroah-Hartman * driver_find_device_by_name - device iterator for locating a particular device
1644c002c97SGreg Kroah-Hartman * of a specific name.
1654c002c97SGreg Kroah-Hartman * @drv: the driver we're iterating
1664c002c97SGreg Kroah-Hartman * @name: name of the device to match
1674c002c97SGreg Kroah-Hartman */
driver_find_device_by_name(const struct device_driver * drv,const char * name)168f8fb4691SGreg Kroah-Hartman static inline struct device *driver_find_device_by_name(const struct device_driver *drv,
1694c002c97SGreg Kroah-Hartman const char *name)
1704c002c97SGreg Kroah-Hartman {
1714c002c97SGreg Kroah-Hartman return driver_find_device(drv, NULL, name, device_match_name);
1724c002c97SGreg Kroah-Hartman }
1734c002c97SGreg Kroah-Hartman
1744c002c97SGreg Kroah-Hartman /**
1754c002c97SGreg Kroah-Hartman * driver_find_device_by_of_node- device iterator for locating a particular device
1764c002c97SGreg Kroah-Hartman * by of_node pointer.
1774c002c97SGreg Kroah-Hartman * @drv: the driver we're iterating
1784c002c97SGreg Kroah-Hartman * @np: of_node pointer to match.
1794c002c97SGreg Kroah-Hartman */
1804c002c97SGreg Kroah-Hartman static inline struct device *
driver_find_device_by_of_node(const struct device_driver * drv,const struct device_node * np)181f8fb4691SGreg Kroah-Hartman driver_find_device_by_of_node(const struct device_driver *drv,
1824c002c97SGreg Kroah-Hartman const struct device_node *np)
1834c002c97SGreg Kroah-Hartman {
1844c002c97SGreg Kroah-Hartman return driver_find_device(drv, NULL, np, device_match_of_node);
1854c002c97SGreg Kroah-Hartman }
1864c002c97SGreg Kroah-Hartman
1874c002c97SGreg Kroah-Hartman /**
1884c002c97SGreg Kroah-Hartman * driver_find_device_by_fwnode- device iterator for locating a particular device
1894c002c97SGreg Kroah-Hartman * by fwnode pointer.
1904c002c97SGreg Kroah-Hartman * @drv: the driver we're iterating
1914c002c97SGreg Kroah-Hartman * @fwnode: fwnode pointer to match.
1924c002c97SGreg Kroah-Hartman */
1934c002c97SGreg Kroah-Hartman static inline struct device *
driver_find_device_by_fwnode(struct device_driver * drv,const struct fwnode_handle * fwnode)1944c002c97SGreg Kroah-Hartman driver_find_device_by_fwnode(struct device_driver *drv,
1954c002c97SGreg Kroah-Hartman const struct fwnode_handle *fwnode)
1964c002c97SGreg Kroah-Hartman {
1974c002c97SGreg Kroah-Hartman return driver_find_device(drv, NULL, fwnode, device_match_fwnode);
1984c002c97SGreg Kroah-Hartman }
1994c002c97SGreg Kroah-Hartman
2004c002c97SGreg Kroah-Hartman /**
2014c002c97SGreg Kroah-Hartman * driver_find_device_by_devt- device iterator for locating a particular device
2024c002c97SGreg Kroah-Hartman * by devt.
2034c002c97SGreg Kroah-Hartman * @drv: the driver we're iterating
2044c002c97SGreg Kroah-Hartman * @devt: devt pointer to match.
2054c002c97SGreg Kroah-Hartman */
driver_find_device_by_devt(const struct device_driver * drv,dev_t devt)206f8fb4691SGreg Kroah-Hartman static inline struct device *driver_find_device_by_devt(const struct device_driver *drv,
2074c002c97SGreg Kroah-Hartman dev_t devt)
2084c002c97SGreg Kroah-Hartman {
2094c002c97SGreg Kroah-Hartman return driver_find_device(drv, NULL, &devt, device_match_devt);
2104c002c97SGreg Kroah-Hartman }
2114c002c97SGreg Kroah-Hartman
driver_find_next_device(const struct device_driver * drv,struct device * start)212f8fb4691SGreg Kroah-Hartman static inline struct device *driver_find_next_device(const struct device_driver *drv,
2134c002c97SGreg Kroah-Hartman struct device *start)
2144c002c97SGreg Kroah-Hartman {
2154c002c97SGreg Kroah-Hartman return driver_find_device(drv, start, NULL, device_match_any);
2164c002c97SGreg Kroah-Hartman }
2174c002c97SGreg Kroah-Hartman
2184c002c97SGreg Kroah-Hartman #ifdef CONFIG_ACPI
2194c002c97SGreg Kroah-Hartman /**
2204c002c97SGreg Kroah-Hartman * driver_find_device_by_acpi_dev : device iterator for locating a particular
2214c002c97SGreg Kroah-Hartman * device matching the ACPI_COMPANION device.
2224c002c97SGreg Kroah-Hartman * @drv: the driver we're iterating
2234c002c97SGreg Kroah-Hartman * @adev: ACPI_COMPANION device to match.
2244c002c97SGreg Kroah-Hartman */
2254c002c97SGreg Kroah-Hartman static inline struct device *
driver_find_device_by_acpi_dev(const struct device_driver * drv,const struct acpi_device * adev)226f8fb4691SGreg Kroah-Hartman driver_find_device_by_acpi_dev(const struct device_driver *drv,
2274c002c97SGreg Kroah-Hartman const struct acpi_device *adev)
2284c002c97SGreg Kroah-Hartman {
2294c002c97SGreg Kroah-Hartman return driver_find_device(drv, NULL, adev, device_match_acpi_dev);
2304c002c97SGreg Kroah-Hartman }
2314c002c97SGreg Kroah-Hartman #else
2324c002c97SGreg Kroah-Hartman static inline struct device *
driver_find_device_by_acpi_dev(const struct device_driver * drv,const void * adev)233f8fb4691SGreg Kroah-Hartman driver_find_device_by_acpi_dev(const struct device_driver *drv, const void *adev)
2344c002c97SGreg Kroah-Hartman {
2354c002c97SGreg Kroah-Hartman return NULL;
2364c002c97SGreg Kroah-Hartman }
2374c002c97SGreg Kroah-Hartman #endif
2384c002c97SGreg Kroah-Hartman
2394c002c97SGreg Kroah-Hartman void driver_deferred_probe_add(struct device *dev);
24013a8e0f6SSaravana Kannan int driver_deferred_probe_check_state(struct device *dev);
2414c002c97SGreg Kroah-Hartman void driver_init(void);
2424c002c97SGreg Kroah-Hartman
2434c002c97SGreg Kroah-Hartman /**
2444c002c97SGreg Kroah-Hartman * module_driver() - Helper macro for drivers that don't do anything
2454c002c97SGreg Kroah-Hartman * special in module init/exit. This eliminates a lot of boilerplate.
2464c002c97SGreg Kroah-Hartman * Each module may only use this macro once, and calling it replaces
2474c002c97SGreg Kroah-Hartman * module_init() and module_exit().
2484c002c97SGreg Kroah-Hartman *
2494c002c97SGreg Kroah-Hartman * @__driver: driver name
2504c002c97SGreg Kroah-Hartman * @__register: register function for this driver type
2514c002c97SGreg Kroah-Hartman * @__unregister: unregister function for this driver type
2524c002c97SGreg Kroah-Hartman * @...: Additional arguments to be passed to __register and __unregister.
2534c002c97SGreg Kroah-Hartman *
2544c002c97SGreg Kroah-Hartman * Use this macro to construct bus specific macros for registering
2554c002c97SGreg Kroah-Hartman * drivers, and do not use it on its own.
2564c002c97SGreg Kroah-Hartman */
2574c002c97SGreg Kroah-Hartman #define module_driver(__driver, __register, __unregister, ...) \
2584c002c97SGreg Kroah-Hartman static int __init __driver##_init(void) \
2594c002c97SGreg Kroah-Hartman { \
2604c002c97SGreg Kroah-Hartman return __register(&(__driver) , ##__VA_ARGS__); \
2614c002c97SGreg Kroah-Hartman } \
2624c002c97SGreg Kroah-Hartman module_init(__driver##_init); \
2634c002c97SGreg Kroah-Hartman static void __exit __driver##_exit(void) \
2644c002c97SGreg Kroah-Hartman { \
2654c002c97SGreg Kroah-Hartman __unregister(&(__driver) , ##__VA_ARGS__); \
2664c002c97SGreg Kroah-Hartman } \
2674c002c97SGreg Kroah-Hartman module_exit(__driver##_exit);
2684c002c97SGreg Kroah-Hartman
2694c002c97SGreg Kroah-Hartman /**
2704c002c97SGreg Kroah-Hartman * builtin_driver() - Helper macro for drivers that don't do anything
2714c002c97SGreg Kroah-Hartman * special in init and have no exit. This eliminates some boilerplate.
2724c002c97SGreg Kroah-Hartman * Each driver may only use this macro once, and calling it replaces
2734c002c97SGreg Kroah-Hartman * device_initcall (or in some cases, the legacy __initcall). This is
2744c002c97SGreg Kroah-Hartman * meant to be a direct parallel of module_driver() above but without
2754c002c97SGreg Kroah-Hartman * the __exit stuff that is not used for builtin cases.
2764c002c97SGreg Kroah-Hartman *
2774c002c97SGreg Kroah-Hartman * @__driver: driver name
2784c002c97SGreg Kroah-Hartman * @__register: register function for this driver type
2794c002c97SGreg Kroah-Hartman * @...: Additional arguments to be passed to __register
2804c002c97SGreg Kroah-Hartman *
2814c002c97SGreg Kroah-Hartman * Use this macro to construct bus specific macros for registering
2824c002c97SGreg Kroah-Hartman * drivers, and do not use it on its own.
2834c002c97SGreg Kroah-Hartman */
2844c002c97SGreg Kroah-Hartman #define builtin_driver(__driver, __register, ...) \
2854c002c97SGreg Kroah-Hartman static int __init __driver##_init(void) \
2864c002c97SGreg Kroah-Hartman { \
2874c002c97SGreg Kroah-Hartman return __register(&(__driver) , ##__VA_ARGS__); \
2884c002c97SGreg Kroah-Hartman } \
2894c002c97SGreg Kroah-Hartman device_initcall(__driver##_init);
2904c002c97SGreg Kroah-Hartman
2914c002c97SGreg Kroah-Hartman #endif /* _DEVICE_DRIVER_H_ */
292