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