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