1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * The driver-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_DRIVER_H_
15*4882a593Smuzhiyun #define _DEVICE_DRIVER_H_
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <linux/kobject.h>
18*4882a593Smuzhiyun #include <linux/klist.h>
19*4882a593Smuzhiyun #include <linux/pm.h>
20*4882a593Smuzhiyun #include <linux/device/bus.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /**
23*4882a593Smuzhiyun * enum probe_type - device driver probe type to try
24*4882a593Smuzhiyun * Device drivers may opt in for special handling of their
25*4882a593Smuzhiyun * respective probe routines. This tells the core what to
26*4882a593Smuzhiyun * expect and prefer.
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * @PROBE_DEFAULT_STRATEGY: Used by drivers that work equally well
29*4882a593Smuzhiyun * whether probed synchronously or asynchronously.
30*4882a593Smuzhiyun * @PROBE_PREFER_ASYNCHRONOUS: Drivers for "slow" devices which
31*4882a593Smuzhiyun * probing order is not essential for booting the system may
32*4882a593Smuzhiyun * opt into executing their probes asynchronously.
33*4882a593Smuzhiyun * @PROBE_FORCE_SYNCHRONOUS: Use this to annotate drivers that need
34*4882a593Smuzhiyun * their probe routines to run synchronously with driver and
35*4882a593Smuzhiyun * device registration (with the exception of -EPROBE_DEFER
36*4882a593Smuzhiyun * handling - re-probing always ends up being done asynchronously).
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * Note that the end goal is to switch the kernel to use asynchronous
39*4882a593Smuzhiyun * probing by default, so annotating drivers with
40*4882a593Smuzhiyun * %PROBE_PREFER_ASYNCHRONOUS is a temporary measure that allows us
41*4882a593Smuzhiyun * to speed up boot process while we are validating the rest of the
42*4882a593Smuzhiyun * drivers.
43*4882a593Smuzhiyun */
44*4882a593Smuzhiyun enum probe_type {
45*4882a593Smuzhiyun PROBE_DEFAULT_STRATEGY,
46*4882a593Smuzhiyun PROBE_PREFER_ASYNCHRONOUS,
47*4882a593Smuzhiyun PROBE_FORCE_SYNCHRONOUS,
48*4882a593Smuzhiyun };
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /**
51*4882a593Smuzhiyun * struct device_driver - The basic device driver structure
52*4882a593Smuzhiyun * @name: Name of the device driver.
53*4882a593Smuzhiyun * @bus: The bus which the device of this driver belongs to.
54*4882a593Smuzhiyun * @owner: The module owner.
55*4882a593Smuzhiyun * @mod_name: Used for built-in modules.
56*4882a593Smuzhiyun * @suppress_bind_attrs: Disables bind/unbind via sysfs.
57*4882a593Smuzhiyun * @probe_type: Type of the probe (synchronous or asynchronous) to use.
58*4882a593Smuzhiyun * @of_match_table: The open firmware table.
59*4882a593Smuzhiyun * @acpi_match_table: The ACPI match table.
60*4882a593Smuzhiyun * @probe: Called to query the existence of a specific device,
61*4882a593Smuzhiyun * whether this driver can work with it, and bind the driver
62*4882a593Smuzhiyun * to a specific device.
63*4882a593Smuzhiyun * @sync_state: Called to sync device state to software state after all the
64*4882a593Smuzhiyun * state tracking consumers linked to this device (present at
65*4882a593Smuzhiyun * the time of late_initcall) have successfully bound to a
66*4882a593Smuzhiyun * driver. If the device has no consumers, this function will
67*4882a593Smuzhiyun * be called at late_initcall_sync level. If the device has
68*4882a593Smuzhiyun * consumers that are never bound to a driver, this function
69*4882a593Smuzhiyun * will never get called until they do.
70*4882a593Smuzhiyun * @remove: Called when the device is removed from the system to
71*4882a593Smuzhiyun * unbind a device from this driver.
72*4882a593Smuzhiyun * @shutdown: Called at shut-down time to quiesce the device.
73*4882a593Smuzhiyun * @suspend: Called to put the device to sleep mode. Usually to a
74*4882a593Smuzhiyun * low power state.
75*4882a593Smuzhiyun * @resume: Called to bring a device from sleep mode.
76*4882a593Smuzhiyun * @groups: Default attributes that get created by the driver core
77*4882a593Smuzhiyun * automatically.
78*4882a593Smuzhiyun * @dev_groups: Additional attributes attached to device instance once the
79*4882a593Smuzhiyun * it is bound to the driver.
80*4882a593Smuzhiyun * @pm: Power management operations of the device which matched
81*4882a593Smuzhiyun * this driver.
82*4882a593Smuzhiyun * @coredump: Called when sysfs entry is written to. The device driver
83*4882a593Smuzhiyun * is expected to call the dev_coredump API resulting in a
84*4882a593Smuzhiyun * uevent.
85*4882a593Smuzhiyun * @p: Driver core's private data, no one other than the driver
86*4882a593Smuzhiyun * core can touch this.
87*4882a593Smuzhiyun *
88*4882a593Smuzhiyun * The device driver-model tracks all of the drivers known to the system.
89*4882a593Smuzhiyun * The main reason for this tracking is to enable the driver core to match
90*4882a593Smuzhiyun * up drivers with new devices. Once drivers are known objects within the
91*4882a593Smuzhiyun * system, however, a number of other things become possible. Device drivers
92*4882a593Smuzhiyun * can export information and configuration variables that are independent
93*4882a593Smuzhiyun * of any specific device.
94*4882a593Smuzhiyun */
95*4882a593Smuzhiyun struct device_driver {
96*4882a593Smuzhiyun const char *name;
97*4882a593Smuzhiyun struct bus_type *bus;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun struct module *owner;
100*4882a593Smuzhiyun const char *mod_name; /* used for built-in modules */
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
103*4882a593Smuzhiyun enum probe_type probe_type;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun const struct of_device_id *of_match_table;
106*4882a593Smuzhiyun const struct acpi_device_id *acpi_match_table;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun int (*probe) (struct device *dev);
109*4882a593Smuzhiyun void (*sync_state)(struct device *dev);
110*4882a593Smuzhiyun int (*remove) (struct device *dev);
111*4882a593Smuzhiyun void (*shutdown) (struct device *dev);
112*4882a593Smuzhiyun int (*suspend) (struct device *dev, pm_message_t state);
113*4882a593Smuzhiyun int (*resume) (struct device *dev);
114*4882a593Smuzhiyun const struct attribute_group **groups;
115*4882a593Smuzhiyun const struct attribute_group **dev_groups;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun const struct dev_pm_ops *pm;
118*4882a593Smuzhiyun void (*coredump) (struct device *dev);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun struct driver_private *p;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun ANDROID_KABI_RESERVE(1);
123*4882a593Smuzhiyun ANDROID_KABI_RESERVE(2);
124*4882a593Smuzhiyun ANDROID_KABI_RESERVE(3);
125*4882a593Smuzhiyun ANDROID_KABI_RESERVE(4);
126*4882a593Smuzhiyun };
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun extern int __must_check driver_register(struct device_driver *drv);
130*4882a593Smuzhiyun extern void driver_unregister(struct device_driver *drv);
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun extern struct device_driver *driver_find(const char *name,
133*4882a593Smuzhiyun struct bus_type *bus);
134*4882a593Smuzhiyun extern int driver_probe_done(void);
135*4882a593Smuzhiyun extern void wait_for_device_probe(void);
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /* sysfs interface for exporting driver attributes */
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun struct driver_attribute {
140*4882a593Smuzhiyun struct attribute attr;
141*4882a593Smuzhiyun ssize_t (*show)(struct device_driver *driver, char *buf);
142*4882a593Smuzhiyun ssize_t (*store)(struct device_driver *driver, const char *buf,
143*4882a593Smuzhiyun size_t count);
144*4882a593Smuzhiyun };
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun #define DRIVER_ATTR_RW(_name) \
147*4882a593Smuzhiyun struct driver_attribute driver_attr_##_name = __ATTR_RW(_name)
148*4882a593Smuzhiyun #define DRIVER_ATTR_RO(_name) \
149*4882a593Smuzhiyun struct driver_attribute driver_attr_##_name = __ATTR_RO(_name)
150*4882a593Smuzhiyun #define DRIVER_ATTR_WO(_name) \
151*4882a593Smuzhiyun struct driver_attribute driver_attr_##_name = __ATTR_WO(_name)
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun extern int __must_check driver_create_file(struct device_driver *driver,
154*4882a593Smuzhiyun const struct driver_attribute *attr);
155*4882a593Smuzhiyun extern void driver_remove_file(struct device_driver *driver,
156*4882a593Smuzhiyun const struct driver_attribute *attr);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun extern int __must_check driver_for_each_device(struct device_driver *drv,
159*4882a593Smuzhiyun struct device *start,
160*4882a593Smuzhiyun void *data,
161*4882a593Smuzhiyun int (*fn)(struct device *dev,
162*4882a593Smuzhiyun void *));
163*4882a593Smuzhiyun struct device *driver_find_device(struct device_driver *drv,
164*4882a593Smuzhiyun struct device *start, const void *data,
165*4882a593Smuzhiyun int (*match)(struct device *dev, const void *data));
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /**
168*4882a593Smuzhiyun * driver_find_device_by_name - device iterator for locating a particular device
169*4882a593Smuzhiyun * of a specific name.
170*4882a593Smuzhiyun * @drv: the driver we're iterating
171*4882a593Smuzhiyun * @name: name of the device to match
172*4882a593Smuzhiyun */
driver_find_device_by_name(struct device_driver * drv,const char * name)173*4882a593Smuzhiyun static inline struct device *driver_find_device_by_name(struct device_driver *drv,
174*4882a593Smuzhiyun const char *name)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun return driver_find_device(drv, NULL, name, device_match_name);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun /**
180*4882a593Smuzhiyun * driver_find_device_by_of_node- device iterator for locating a particular device
181*4882a593Smuzhiyun * by of_node pointer.
182*4882a593Smuzhiyun * @drv: the driver we're iterating
183*4882a593Smuzhiyun * @np: of_node pointer to match.
184*4882a593Smuzhiyun */
185*4882a593Smuzhiyun static inline struct device *
driver_find_device_by_of_node(struct device_driver * drv,const struct device_node * np)186*4882a593Smuzhiyun driver_find_device_by_of_node(struct device_driver *drv,
187*4882a593Smuzhiyun const struct device_node *np)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun return driver_find_device(drv, NULL, np, device_match_of_node);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun /**
193*4882a593Smuzhiyun * driver_find_device_by_fwnode- device iterator for locating a particular device
194*4882a593Smuzhiyun * by fwnode pointer.
195*4882a593Smuzhiyun * @drv: the driver we're iterating
196*4882a593Smuzhiyun * @fwnode: fwnode pointer to match.
197*4882a593Smuzhiyun */
198*4882a593Smuzhiyun static inline struct device *
driver_find_device_by_fwnode(struct device_driver * drv,const struct fwnode_handle * fwnode)199*4882a593Smuzhiyun driver_find_device_by_fwnode(struct device_driver *drv,
200*4882a593Smuzhiyun const struct fwnode_handle *fwnode)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun return driver_find_device(drv, NULL, fwnode, device_match_fwnode);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun /**
206*4882a593Smuzhiyun * driver_find_device_by_devt- device iterator for locating a particular device
207*4882a593Smuzhiyun * by devt.
208*4882a593Smuzhiyun * @drv: the driver we're iterating
209*4882a593Smuzhiyun * @devt: devt pointer to match.
210*4882a593Smuzhiyun */
driver_find_device_by_devt(struct device_driver * drv,dev_t devt)211*4882a593Smuzhiyun static inline struct device *driver_find_device_by_devt(struct device_driver *drv,
212*4882a593Smuzhiyun dev_t devt)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun return driver_find_device(drv, NULL, &devt, device_match_devt);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
driver_find_next_device(struct device_driver * drv,struct device * start)217*4882a593Smuzhiyun static inline struct device *driver_find_next_device(struct device_driver *drv,
218*4882a593Smuzhiyun struct device *start)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun return driver_find_device(drv, start, NULL, device_match_any);
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun #ifdef CONFIG_ACPI
224*4882a593Smuzhiyun /**
225*4882a593Smuzhiyun * driver_find_device_by_acpi_dev : device iterator for locating a particular
226*4882a593Smuzhiyun * device matching the ACPI_COMPANION device.
227*4882a593Smuzhiyun * @drv: the driver we're iterating
228*4882a593Smuzhiyun * @adev: ACPI_COMPANION device to match.
229*4882a593Smuzhiyun */
230*4882a593Smuzhiyun static inline struct device *
driver_find_device_by_acpi_dev(struct device_driver * drv,const struct acpi_device * adev)231*4882a593Smuzhiyun driver_find_device_by_acpi_dev(struct device_driver *drv,
232*4882a593Smuzhiyun const struct acpi_device *adev)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun return driver_find_device(drv, NULL, adev, device_match_acpi_dev);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun #else
237*4882a593Smuzhiyun static inline struct device *
driver_find_device_by_acpi_dev(struct device_driver * drv,const void * adev)238*4882a593Smuzhiyun driver_find_device_by_acpi_dev(struct device_driver *drv, const void *adev)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun return NULL;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun #endif
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun extern int driver_deferred_probe_timeout;
245*4882a593Smuzhiyun void driver_deferred_probe_add(struct device *dev);
246*4882a593Smuzhiyun int driver_deferred_probe_check_state(struct device *dev);
247*4882a593Smuzhiyun void driver_init(void);
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun /**
250*4882a593Smuzhiyun * module_driver() - Helper macro for drivers that don't do anything
251*4882a593Smuzhiyun * special in module init/exit. This eliminates a lot of boilerplate.
252*4882a593Smuzhiyun * Each module may only use this macro once, and calling it replaces
253*4882a593Smuzhiyun * module_init() and module_exit().
254*4882a593Smuzhiyun *
255*4882a593Smuzhiyun * @__driver: driver name
256*4882a593Smuzhiyun * @__register: register function for this driver type
257*4882a593Smuzhiyun * @__unregister: unregister function for this driver type
258*4882a593Smuzhiyun * @...: Additional arguments to be passed to __register and __unregister.
259*4882a593Smuzhiyun *
260*4882a593Smuzhiyun * Use this macro to construct bus specific macros for registering
261*4882a593Smuzhiyun * drivers, and do not use it on its own.
262*4882a593Smuzhiyun */
263*4882a593Smuzhiyun #define module_driver(__driver, __register, __unregister, ...) \
264*4882a593Smuzhiyun static int __init __driver##_init(void) \
265*4882a593Smuzhiyun { \
266*4882a593Smuzhiyun return __register(&(__driver) , ##__VA_ARGS__); \
267*4882a593Smuzhiyun } \
268*4882a593Smuzhiyun module_init(__driver##_init); \
269*4882a593Smuzhiyun static void __exit __driver##_exit(void) \
270*4882a593Smuzhiyun { \
271*4882a593Smuzhiyun __unregister(&(__driver) , ##__VA_ARGS__); \
272*4882a593Smuzhiyun } \
273*4882a593Smuzhiyun module_exit(__driver##_exit);
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun /**
276*4882a593Smuzhiyun * builtin_driver() - Helper macro for drivers that don't do anything
277*4882a593Smuzhiyun * special in init and have no exit. This eliminates some boilerplate.
278*4882a593Smuzhiyun * Each driver may only use this macro once, and calling it replaces
279*4882a593Smuzhiyun * device_initcall (or in some cases, the legacy __initcall). This is
280*4882a593Smuzhiyun * meant to be a direct parallel of module_driver() above but without
281*4882a593Smuzhiyun * the __exit stuff that is not used for builtin cases.
282*4882a593Smuzhiyun *
283*4882a593Smuzhiyun * @__driver: driver name
284*4882a593Smuzhiyun * @__register: register function for this driver type
285*4882a593Smuzhiyun * @...: Additional arguments to be passed to __register
286*4882a593Smuzhiyun *
287*4882a593Smuzhiyun * Use this macro to construct bus specific macros for registering
288*4882a593Smuzhiyun * drivers, and do not use it on its own.
289*4882a593Smuzhiyun */
290*4882a593Smuzhiyun #define builtin_driver(__driver, __register, ...) \
291*4882a593Smuzhiyun static int __init __driver##_init(void) \
292*4882a593Smuzhiyun { \
293*4882a593Smuzhiyun return __register(&(__driver) , ##__VA_ARGS__); \
294*4882a593Smuzhiyun } \
295*4882a593Smuzhiyun device_initcall(__driver##_init);
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun #endif /* _DEVICE_DRIVER_H_ */
298