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