1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * device.h - generic, centralized 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 * 9 * See Documentation/driver-api/driver-model/ for more information. 10 */ 11 12 #ifndef _DEVICE_H_ 13 #define _DEVICE_H_ 14 15 #include <linux/dev_printk.h> 16 #include <linux/energy_model.h> 17 #include <linux/ioport.h> 18 #include <linux/kobject.h> 19 #include <linux/klist.h> 20 #include <linux/list.h> 21 #include <linux/lockdep.h> 22 #include <linux/compiler.h> 23 #include <linux/types.h> 24 #include <linux/mutex.h> 25 #include <linux/pm.h> 26 #include <linux/atomic.h> 27 #include <linux/uidgid.h> 28 #include <linux/gfp.h> 29 #include <linux/device/bus.h> 30 #include <linux/device/class.h> 31 #include <linux/device/devres.h> 32 #include <linux/device/driver.h> 33 #include <linux/cleanup.h> 34 #include <asm/device.h> 35 36 struct device; 37 struct device_private; 38 struct device_driver; 39 struct driver_private; 40 struct module; 41 struct subsys_private; 42 struct device_node; 43 struct fwnode_handle; 44 struct iommu_group; 45 struct dev_pin_info; 46 struct dev_iommu; 47 struct msi_device_data; 48 49 /** 50 * struct subsys_interface - interfaces to device functions 51 * @name: name of the device function 52 * @subsys: subsystem of the devices to attach to 53 * @node: the list of functions registered at the subsystem 54 * @add_dev: device hookup to device function handler 55 * @remove_dev: device hookup to device function handler 56 * 57 * Simple interfaces attached to a subsystem. Multiple interfaces can 58 * attach to a subsystem and its devices. Unlike drivers, they do not 59 * exclusively claim or control devices. Interfaces usually represent 60 * a specific functionality of a subsystem/class of devices. 61 */ 62 struct subsys_interface { 63 const char *name; 64 const struct bus_type *subsys; 65 struct list_head node; 66 int (*add_dev)(struct device *dev, struct subsys_interface *sif); 67 void (*remove_dev)(struct device *dev, struct subsys_interface *sif); 68 }; 69 70 int subsys_interface_register(struct subsys_interface *sif); 71 void subsys_interface_unregister(struct subsys_interface *sif); 72 73 int subsys_system_register(const struct bus_type *subsys, 74 const struct attribute_group **groups); 75 int subsys_virtual_register(const struct bus_type *subsys, 76 const struct attribute_group **groups); 77 78 /* 79 * The type of device, "struct device" is embedded in. A class 80 * or bus can contain devices of different types 81 * like "partitions" and "disks", "mouse" and "event". 82 * This identifies the device type and carries type-specific 83 * information, equivalent to the kobj_type of a kobject. 84 * If "name" is specified, the uevent will contain it in 85 * the DEVTYPE variable. 86 * 87 * @need_parent_lock: When probing or removing a device of this type, 88 * driver core should lock the device's parent. Note that 89 * the device must hold a reference to its parent. 90 */ 91 struct device_type { 92 const char *name; 93 const struct attribute_group **groups; 94 int (*uevent)(const struct device *dev, struct kobj_uevent_env *env); 95 char *(*devnode)(const struct device *dev, umode_t *mode, 96 kuid_t *uid, kgid_t *gid); 97 void (*release)(struct device *dev); 98 99 const struct dev_pm_ops *pm; 100 bool need_parent_lock; 101 }; 102 103 /** 104 * struct device_attribute - Interface for exporting device attributes. 105 * @attr: sysfs attribute definition. 106 * @show: Show handler. 107 * @show_const: Show handler (read-only). 108 * @store: Store handler. 109 * @store_const: Store handler (read-only). 110 */ 111 struct device_attribute { 112 struct attribute attr; 113 __SYSFS_FUNCTION_ALTERNATIVE( 114 ssize_t (*show)(struct device *dev, struct device_attribute *attr, 115 char *buf); 116 ssize_t (*show_const)(struct device *dev, const struct device_attribute *attr, 117 char *buf); 118 ); 119 __SYSFS_FUNCTION_ALTERNATIVE( 120 ssize_t (*store)(struct device *dev, struct device_attribute *attr, 121 const char *buf, size_t count); 122 ssize_t (*store_const)(struct device *dev, const struct device_attribute *attr, 123 const char *buf, size_t count); 124 ); 125 }; 126 127 /** 128 * struct dev_ext_attribute - Exported device attribute with extra context. 129 * @attr: Exported device attribute. 130 * @var: Pointer to context. 131 */ 132 struct dev_ext_attribute { 133 struct device_attribute attr; 134 void *var; 135 }; 136 137 ssize_t device_show_ulong(struct device *dev, const struct device_attribute *attr, 138 char *buf); 139 ssize_t device_store_ulong(struct device *dev, const struct device_attribute *attr, 140 const char *buf, size_t count); 141 ssize_t device_show_int(struct device *dev, const struct device_attribute *attr, 142 char *buf); 143 ssize_t device_store_int(struct device *dev, const struct device_attribute *attr, 144 const char *buf, size_t count); 145 ssize_t device_show_bool(struct device *dev, const struct device_attribute *attr, 146 char *buf); 147 ssize_t device_store_bool(struct device *dev, const struct device_attribute *attr, 148 const char *buf, size_t count); 149 ssize_t device_show_string(struct device *dev, const struct device_attribute *attr, 150 char *buf); 151 152 typedef ssize_t __device_show_handler_const(struct device *dev, const struct device_attribute *attr, 153 char *buf); 154 typedef ssize_t __device_store_handler_const(struct device *dev, const struct device_attribute *attr, 155 const char *buf, size_t count); 156 157 #ifdef CONFIG_CFI 158 159 #define __DEVICE_ATTR_SHOW_STORE(_show, _store) \ 160 .show = _Generic(_show, \ 161 __device_show_handler_const * : NULL, \ 162 default : _show \ 163 ), \ 164 .show_const = _Generic(_show, \ 165 __device_show_handler_const * : _show, \ 166 default : NULL \ 167 ), \ 168 .store = _Generic(_store, \ 169 __device_store_handler_const * : NULL, \ 170 default : _store \ 171 ), \ 172 .store_const = _Generic(_store, \ 173 __device_store_handler_const * : _store, \ 174 default : NULL \ 175 ), 176 177 #else 178 179 #define __DEVICE_ATTR_SHOW_STORE(_show, _store) \ 180 .show = _Generic(_show, \ 181 __device_show_handler_const * : (void *)_show, \ 182 default : _show \ 183 ), \ 184 .store = _Generic(_store, \ 185 __device_store_handler_const * : (void *)_store, \ 186 default : _store \ 187 ), \ 188 189 #endif 190 191 192 #define __DEVICE_ATTR(_name, _mode, _show, _store) { \ 193 .attr = {.name = __stringify(_name), \ 194 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \ 195 __DEVICE_ATTR_SHOW_STORE(_show, _store) \ 196 } 197 198 #define __DEVICE_ATTR_RO_MODE(_name, _mode) \ 199 __DEVICE_ATTR(_name, _mode, _name##_show, NULL) 200 201 #define __DEVICE_ATTR_RO(_name) \ 202 __DEVICE_ATTR_RO_MODE(_name, 0444) 203 204 #define __DEVICE_ATTR_WO(_name) \ 205 __DEVICE_ATTR(_name, 0200, NULL, _name##_store) 206 207 #define __DEVICE_ATTR_RW_MODE(_name, _mode) \ 208 __DEVICE_ATTR(_name, _mode, _name##_show, _name##_store) 209 210 #define __DEVICE_ATTR_RW(_name) \ 211 __DEVICE_ATTR_RW_MODE(_name, 0644) 212 213 #ifdef CONFIG_DEBUG_LOCK_ALLOC 214 #define __DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) { \ 215 .attr = {.name = __stringify(_name), .mode = _mode, \ 216 .ignore_lockdep = true }, \ 217 __DEVICE_ATTR_SHOW_STORE(_show, _store) \ 218 } 219 #else 220 #define __DEVICE_ATTR_IGNORE_LOCKDEP __DEVICE_ATTR 221 #endif 222 223 /** 224 * DEVICE_ATTR - Define a device attribute. 225 * @_name: Attribute name. 226 * @_mode: File mode. 227 * @_show: Show handler. Optional, but mandatory if attribute is readable. 228 * @_store: Store handler. Optional, but mandatory if attribute is writable. 229 * 230 * Convenience macro for defining a struct device_attribute. 231 * 232 * For example, ``DEVICE_ATTR(foo, 0644, foo_show, foo_store);`` expands to: 233 * 234 * .. code-block:: c 235 * 236 * struct device_attribute dev_attr_foo = { 237 * .attr = { .name = "foo", .mode = 0644 }, 238 * .show = foo_show, 239 * .store = foo_store, 240 * }; 241 */ 242 #define DEVICE_ATTR(_name, _mode, _show, _store) \ 243 struct device_attribute dev_attr_##_name = __DEVICE_ATTR(_name, _mode, _show, _store) 244 245 /** 246 * DEVICE_ATTR_RW - Define a read-write device attribute. 247 * @_name: Attribute name. 248 * 249 * Like DEVICE_ATTR(), but @_mode is 0644, @_show is <_name>_show, 250 * and @_store is <_name>_store. 251 */ 252 #define DEVICE_ATTR_RW(_name) \ 253 struct device_attribute dev_attr_##_name = __DEVICE_ATTR_RW(_name) 254 255 /** 256 * DEVICE_ATTR_ADMIN_RW - Define an admin-only read-write device attribute. 257 * @_name: Attribute name. 258 * 259 * Like DEVICE_ATTR_RW(), but @_mode is 0600. 260 */ 261 #define DEVICE_ATTR_ADMIN_RW(_name) \ 262 struct device_attribute dev_attr_##_name = __DEVICE_ATTR_RW_MODE(_name, 0600) 263 264 /** 265 * DEVICE_ATTR_RW_NAMED - Define a read-write device attribute with a sysfs name 266 * that differs from the function name. 267 * @_name: Attribute function preface 268 * @_attrname: Attribute name as it wil be exposed in the sysfs. 269 * 270 * Like DEVICE_ATTR_RW(), but allows for reusing names under separate paths in 271 * the same driver. 272 */ 273 #define DEVICE_ATTR_RW_NAMED(_name, _attrname) \ 274 struct device_attribute dev_attr_##_name = { \ 275 .attr = { .name = _attrname, .mode = 0644 }, \ 276 __DEVICE_ATTR_SHOW_STORE(_name##_show, _name##_store) \ 277 } 278 279 /** 280 * DEVICE_ATTR_RO - Define a readable device attribute. 281 * @_name: Attribute name. 282 * 283 * Like DEVICE_ATTR(), but @_mode is 0444 and @_show is <_name>_show. 284 */ 285 #define DEVICE_ATTR_RO(_name) \ 286 struct device_attribute dev_attr_##_name = __DEVICE_ATTR_RO(_name) 287 288 /** 289 * DEVICE_ATTR_ADMIN_RO - Define an admin-only readable device attribute. 290 * @_name: Attribute name. 291 * 292 * Like DEVICE_ATTR_RO(), but @_mode is 0400. 293 */ 294 #define DEVICE_ATTR_ADMIN_RO(_name) \ 295 struct device_attribute dev_attr_##_name = __DEVICE_ATTR_RO_MODE(_name, 0400) 296 297 /** 298 * DEVICE_ATTR_RO_NAMED - Define a read-only device attribute with a sysfs name 299 * that differs from the function name. 300 * @_name: Attribute function preface 301 * @_attrname: Attribute name as it wil be exposed in the sysfs. 302 * 303 * Like DEVICE_ATTR_RO(), but allows for reusing names under separate paths in 304 * the same driver. 305 */ 306 #define DEVICE_ATTR_RO_NAMED(_name, _attrname) \ 307 struct device_attribute dev_attr_##_name = { \ 308 .attr = { .name = _attrname, .mode = 0444 }, \ 309 __DEVICE_ATTR_SHOW_STORE(_name##_show, NULL) \ 310 } 311 312 /** 313 * DEVICE_ATTR_WO - Define an admin-only writable device attribute. 314 * @_name: Attribute name. 315 * 316 * Like DEVICE_ATTR(), but @_mode is 0200 and @_store is <_name>_store. 317 */ 318 #define DEVICE_ATTR_WO(_name) \ 319 struct device_attribute dev_attr_##_name = __DEVICE_ATTR_WO(_name) 320 321 /** 322 * DEVICE_ATTR_WO_NAMED - Define a read-only device attribute with a sysfs name 323 * that differs from the function name. 324 * @_name: Attribute function preface 325 * @_attrname: Attribute name as it wil be exposed in the sysfs. 326 * 327 * Like DEVICE_ATTR_WO(), but allows for reusing names under separate paths in 328 * the same driver. 329 */ 330 #define DEVICE_ATTR_WO_NAMED(_name, _attrname) \ 331 struct device_attribute dev_attr_##_name = { \ 332 .attr = { .name = _attrname, .mode = 0200 }, \ 333 __DEVICE_ATTR_SHOW_STORE(NULL, _name##_store) \ 334 } 335 336 /** 337 * DEVICE_ULONG_ATTR - Define a device attribute backed by an unsigned long. 338 * @_name: Attribute name. 339 * @_mode: File mode. 340 * @_var: Identifier of unsigned long. 341 * 342 * Like DEVICE_ATTR(), but @_show and @_store are automatically provided 343 * such that reads and writes to the attribute from userspace affect @_var. 344 */ 345 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \ 346 struct dev_ext_attribute dev_attr_##_name = \ 347 { __DEVICE_ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) } 348 349 /** 350 * DEVICE_INT_ATTR - Define a device attribute backed by an int. 351 * @_name: Attribute name. 352 * @_mode: File mode. 353 * @_var: Identifier of int. 354 * 355 * Like DEVICE_ULONG_ATTR(), but @_var is an int. 356 */ 357 #define DEVICE_INT_ATTR(_name, _mode, _var) \ 358 struct dev_ext_attribute dev_attr_##_name = \ 359 { __DEVICE_ATTR(_name, _mode, device_show_int, device_store_int), &(_var) } 360 361 /** 362 * DEVICE_BOOL_ATTR - Define a device attribute backed by a bool. 363 * @_name: Attribute name. 364 * @_mode: File mode. 365 * @_var: Identifier of bool. 366 * 367 * Like DEVICE_ULONG_ATTR(), but @_var is a bool. 368 */ 369 #define DEVICE_BOOL_ATTR(_name, _mode, _var) \ 370 struct dev_ext_attribute dev_attr_##_name = \ 371 { __DEVICE_ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) } 372 373 /** 374 * DEVICE_STRING_ATTR_RO - Define a device attribute backed by a r/o string. 375 * @_name: Attribute name. 376 * @_mode: File mode. 377 * @_var: Identifier of string. 378 * 379 * Like DEVICE_ULONG_ATTR(), but @_var is a string. Because the length of the 380 * string allocation is unknown, the attribute must be read-only. 381 */ 382 #define DEVICE_STRING_ATTR_RO(_name, _mode, _var) \ 383 struct dev_ext_attribute dev_attr_##_name = \ 384 { __DEVICE_ATTR(_name, (_mode) & ~0222, device_show_string, NULL), (_var) } 385 386 #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \ 387 struct device_attribute dev_attr_##_name = \ 388 __DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) 389 390 int device_create_file(struct device *device, 391 const struct device_attribute *entry); 392 void device_remove_file(struct device *dev, 393 const struct device_attribute *attr); 394 bool device_remove_file_self(struct device *dev, 395 const struct device_attribute *attr); 396 int __must_check device_create_bin_file(struct device *dev, 397 const struct bin_attribute *attr); 398 void device_remove_bin_file(struct device *dev, 399 const struct bin_attribute *attr); 400 401 struct device_dma_parameters { 402 /* 403 * a low level driver may set these to teach IOMMU code about 404 * sg limitations. 405 */ 406 unsigned int max_segment_size; 407 unsigned int min_align_mask; 408 unsigned long segment_boundary_mask; 409 }; 410 411 /** 412 * enum device_link_state - Device link states. 413 * @DL_STATE_NONE: The presence of the drivers is not being tracked. 414 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present. 415 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not. 416 * @DL_STATE_CONSUMER_PROBE: The consumer is probing (supplier driver present). 417 * @DL_STATE_ACTIVE: Both the supplier and consumer drivers are present. 418 * @DL_STATE_SUPPLIER_UNBIND: The supplier driver is unbinding. 419 */ 420 enum device_link_state { 421 DL_STATE_NONE = -1, 422 DL_STATE_DORMANT = 0, 423 DL_STATE_AVAILABLE, 424 DL_STATE_CONSUMER_PROBE, 425 DL_STATE_ACTIVE, 426 DL_STATE_SUPPLIER_UNBIND, 427 }; 428 429 /* 430 * Device link flags. 431 * 432 * STATELESS: The core will not remove this link automatically. 433 * AUTOREMOVE_CONSUMER: Remove the link automatically on consumer driver unbind. 434 * PM_RUNTIME: If set, the runtime PM framework will use this link. 435 * RPM_ACTIVE: Run pm_runtime_get_sync() on the supplier during link creation. 436 * AUTOREMOVE_SUPPLIER: Remove the link automatically on supplier driver unbind. 437 * AUTOPROBE_CONSUMER: Probe consumer driver automatically after supplier binds. 438 * MANAGED: The core tracks presence of supplier/consumer drivers (internal). 439 * SYNC_STATE_ONLY: Link only affects sync_state() behavior. 440 * INFERRED: Inferred from data (eg: firmware) and not from driver actions. 441 */ 442 #define DL_FLAG_STATELESS BIT(0) 443 #define DL_FLAG_AUTOREMOVE_CONSUMER BIT(1) 444 #define DL_FLAG_PM_RUNTIME BIT(2) 445 #define DL_FLAG_RPM_ACTIVE BIT(3) 446 #define DL_FLAG_AUTOREMOVE_SUPPLIER BIT(4) 447 #define DL_FLAG_AUTOPROBE_CONSUMER BIT(5) 448 #define DL_FLAG_MANAGED BIT(6) 449 #define DL_FLAG_SYNC_STATE_ONLY BIT(7) 450 #define DL_FLAG_INFERRED BIT(8) 451 #define DL_FLAG_CYCLE BIT(9) 452 453 /** 454 * enum dl_dev_state - Device driver presence tracking information. 455 * @DL_DEV_NO_DRIVER: There is no driver attached to the device. 456 * @DL_DEV_PROBING: A driver is probing. 457 * @DL_DEV_DRIVER_BOUND: The driver has been bound to the device. 458 * @DL_DEV_UNBINDING: The driver is unbinding from the device. 459 */ 460 enum dl_dev_state { 461 DL_DEV_NO_DRIVER = 0, 462 DL_DEV_PROBING, 463 DL_DEV_DRIVER_BOUND, 464 DL_DEV_UNBINDING, 465 }; 466 467 /** 468 * enum device_removable - Whether the device is removable. The criteria for a 469 * device to be classified as removable is determined by its subsystem or bus. 470 * @DEVICE_REMOVABLE_NOT_SUPPORTED: This attribute is not supported for this 471 * device (default). 472 * @DEVICE_REMOVABLE_UNKNOWN: Device location is Unknown. 473 * @DEVICE_FIXED: Device is not removable by the user. 474 * @DEVICE_REMOVABLE: Device is removable by the user. 475 */ 476 enum device_removable { 477 DEVICE_REMOVABLE_NOT_SUPPORTED = 0, /* must be 0 */ 478 DEVICE_REMOVABLE_UNKNOWN, 479 DEVICE_FIXED, 480 DEVICE_REMOVABLE, 481 }; 482 483 /** 484 * struct dev_links_info - Device data related to device links. 485 * @suppliers: List of links to supplier devices. 486 * @consumers: List of links to consumer devices. 487 * @defer_sync: Hook to global list of devices that have deferred sync_state. 488 * @status: Driver status information. 489 */ 490 struct dev_links_info { 491 struct list_head suppliers; 492 struct list_head consumers; 493 struct list_head defer_sync; 494 enum dl_dev_state status; 495 }; 496 497 /** 498 * struct dev_msi_info - Device data related to MSI 499 * @domain: The MSI interrupt domain associated to the device 500 * @data: Pointer to MSI device data 501 */ 502 struct dev_msi_info { 503 #ifdef CONFIG_GENERIC_MSI_IRQ 504 struct irq_domain *domain; 505 struct msi_device_data *data; 506 #endif 507 }; 508 509 /** 510 * enum device_physical_location_panel - Describes which panel surface of the 511 * system's housing the device connection point resides on. 512 * @DEVICE_PANEL_TOP: Device connection point is on the top panel. 513 * @DEVICE_PANEL_BOTTOM: Device connection point is on the bottom panel. 514 * @DEVICE_PANEL_LEFT: Device connection point is on the left panel. 515 * @DEVICE_PANEL_RIGHT: Device connection point is on the right panel. 516 * @DEVICE_PANEL_FRONT: Device connection point is on the front panel. 517 * @DEVICE_PANEL_BACK: Device connection point is on the back panel. 518 * @DEVICE_PANEL_UNKNOWN: The panel with device connection point is unknown. 519 */ 520 enum device_physical_location_panel { 521 DEVICE_PANEL_TOP, 522 DEVICE_PANEL_BOTTOM, 523 DEVICE_PANEL_LEFT, 524 DEVICE_PANEL_RIGHT, 525 DEVICE_PANEL_FRONT, 526 DEVICE_PANEL_BACK, 527 DEVICE_PANEL_UNKNOWN, 528 }; 529 530 /** 531 * enum device_physical_location_vertical_position - Describes vertical 532 * position of the device connection point on the panel surface. 533 * @DEVICE_VERT_POS_UPPER: Device connection point is at upper part of panel. 534 * @DEVICE_VERT_POS_CENTER: Device connection point is at center part of panel. 535 * @DEVICE_VERT_POS_LOWER: Device connection point is at lower part of panel. 536 */ 537 enum device_physical_location_vertical_position { 538 DEVICE_VERT_POS_UPPER, 539 DEVICE_VERT_POS_CENTER, 540 DEVICE_VERT_POS_LOWER, 541 }; 542 543 /** 544 * enum device_physical_location_horizontal_position - Describes horizontal 545 * position of the device connection point on the panel surface. 546 * @DEVICE_HORI_POS_LEFT: Device connection point is at left part of panel. 547 * @DEVICE_HORI_POS_CENTER: Device connection point is at center part of panel. 548 * @DEVICE_HORI_POS_RIGHT: Device connection point is at right part of panel. 549 */ 550 enum device_physical_location_horizontal_position { 551 DEVICE_HORI_POS_LEFT, 552 DEVICE_HORI_POS_CENTER, 553 DEVICE_HORI_POS_RIGHT, 554 }; 555 556 /** 557 * struct device_physical_location - Device data related to physical location 558 * of the device connection point. 559 * @panel: Panel surface of the system's housing that the device connection 560 * point resides on. 561 * @vertical_position: Vertical position of the device connection point within 562 * the panel. 563 * @horizontal_position: Horizontal position of the device connection point 564 * within the panel. 565 * @dock: Set if the device connection point resides in a docking station or 566 * port replicator. 567 * @lid: Set if this device connection point resides on the lid of laptop 568 * system. 569 */ 570 struct device_physical_location { 571 enum device_physical_location_panel panel; 572 enum device_physical_location_vertical_position vertical_position; 573 enum device_physical_location_horizontal_position horizontal_position; 574 bool dock; 575 bool lid; 576 }; 577 578 /** 579 * enum struct_device_flags - Flags in struct device 580 * 581 * Each flag should have a set of accessor functions created via 582 * __create_dev_flag_accessors() for each access. 583 * 584 * @DEV_FLAG_READY_TO_PROBE: If set then device_add() has finished enough 585 * initialization that probe could be called. 586 * @DEV_FLAG_CAN_MATCH: The device has matched with a driver at least once or it 587 * is in a bus (like AMBA) which can't check for matching drivers 588 * until other devices probe successfully. 589 * @DEV_FLAG_DMA_IOMMU: Device is using default IOMMU implementation for DMA and 590 * doesn't rely on dma_ops structure. 591 * @DEV_FLAG_DMA_SKIP_SYNC: DMA sync operations can be skipped for coherent 592 * buffers. 593 * @DEV_FLAG_DMA_OPS_BYPASS: If set then the dma_ops are bypassed for the 594 * streaming DMA operations (->map_* / ->unmap_* / ->sync_*), and 595 * optional (if the coherent mask is large enough) also for dma 596 * allocations. This flag is managed by the dma ops instance from 597 * ->dma_supported. 598 * @DEV_FLAG_STATE_SYNCED: The hardware state of this device has been synced to 599 * match the software state of this device by calling the 600 * driver/bus sync_state() callback. 601 * @DEV_FLAG_DMA_COHERENT: This particular device is dma coherent, even if the 602 * architecture supports non-coherent devices. 603 * @DEV_FLAG_OF_NODE_REUSED: Set if the device-tree node is shared with an 604 * ancestor device. 605 * @DEV_FLAG_OFFLINE_DISABLED: If set, the device is permanently online. 606 * @DEV_FLAG_OFFLINE: Set after successful invocation of bus type's .offline(). 607 * @DEV_FLAG_COUNT: Number of defined struct_device_flags. 608 */ 609 enum struct_device_flags { 610 DEV_FLAG_READY_TO_PROBE = 0, 611 DEV_FLAG_CAN_MATCH = 1, 612 DEV_FLAG_DMA_IOMMU = 2, 613 DEV_FLAG_DMA_SKIP_SYNC = 3, 614 DEV_FLAG_DMA_OPS_BYPASS = 4, 615 DEV_FLAG_STATE_SYNCED = 5, 616 DEV_FLAG_DMA_COHERENT = 6, 617 DEV_FLAG_OF_NODE_REUSED = 7, 618 DEV_FLAG_OFFLINE_DISABLED = 8, 619 DEV_FLAG_OFFLINE = 9, 620 621 DEV_FLAG_COUNT 622 }; 623 624 /** 625 * struct device - The basic device structure 626 * @parent: The device's "parent" device, the device to which it is attached. 627 * In most cases, a parent device is some sort of bus or host 628 * controller. If parent is NULL, the device, is a top-level device, 629 * which is not usually what you want. 630 * @p: Holds the private data of the driver core portions of the device. 631 * See the comment of the struct device_private for detail. 632 * @kobj: A top-level, abstract class from which other classes are derived. 633 * @init_name: Initial name of the device. 634 * @type: The type of device. 635 * This identifies the device type and carries type-specific 636 * information. 637 * @mutex: Mutex to synchronize calls to its driver. 638 * @bus: Type of bus device is on. 639 * @driver: Which driver has allocated this 640 * @platform_data: Platform data specific to the device. 641 * Example: For devices on custom boards, as typical of embedded 642 * and SOC based hardware, Linux often uses platform_data to point 643 * to board-specific structures describing devices and how they 644 * are wired. That can include what ports are available, chip 645 * variants, which GPIO pins act in what additional roles, and so 646 * on. This shrinks the "Board Support Packages" (BSPs) and 647 * minimizes board-specific #ifdefs in drivers. 648 * @driver_data: Private pointer for driver specific info. 649 * @driver_override: Driver name to force a match. Do not touch directly; use 650 * device_set_driver_override() instead. 651 * @links: Links to suppliers and consumers of this device. 652 * @power: For device power management. 653 * See Documentation/driver-api/pm/devices.rst for details. 654 * @pm_domain: Provide callbacks that are executed during system suspend, 655 * hibernation, system resume and during runtime PM transitions 656 * along with subsystem-level and driver-level callbacks. 657 * @em_pd: device's energy model performance domain 658 * @pins: For device pin management. 659 * See Documentation/driver-api/pin-control.rst for details. 660 * @msi: MSI related data 661 * @numa_node: NUMA node this device is close to. 662 * @dma_ops: DMA mapping operations for this device. 663 * @dma_mask: Dma mask (if dma'ble device). 664 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all 665 * hardware supports 64-bit addresses for consistent allocations 666 * such descriptors. 667 * @bus_dma_limit: Limit of an upstream bridge or bus which imposes a smaller 668 * DMA limit than the device itself supports. 669 * @dma_range_map: map for DMA memory ranges relative to that of RAM 670 * @dma_parms: A low level driver may set these to teach IOMMU code about 671 * segment limitations. 672 * @dma_pools: Dma pools (if dma'ble device). 673 * @dma_mem: Internal for coherent mem override. 674 * @cma_area: Contiguous memory area for dma allocations 675 * @dma_io_tlb_mem: Software IO TLB allocator. Not for driver use. 676 * @dma_io_tlb_pools: List of transient swiotlb memory pools. 677 * @dma_io_tlb_lock: Protects changes to the list of active pools. 678 * @dma_uses_io_tlb: %true if device has used the software IO TLB. 679 * @archdata: For arch-specific additions. 680 * @of_node: Associated device tree node. 681 * @fwnode: Associated device node supplied by platform firmware. 682 * @devt: For creating the sysfs "dev". 683 * @id: device instance 684 * @devres_lock: Spinlock to protect the resource of the device. 685 * @devres_head: The resources list of the device. 686 * @class: The class of the device. 687 * @groups: Optional attribute groups. 688 * @release: Callback to free the device after all references have 689 * gone away. This should be set by the allocator of the 690 * device (i.e. the bus driver that discovered the device). 691 * @iommu_group: IOMMU group the device belongs to. 692 * @iommu: Per device generic IOMMU runtime data 693 * @physical_location: Describes physical location of the device connection 694 * point in the system housing. 695 * @removable: Whether the device can be removed from the system. This 696 * should be set by the subsystem / bus driver that discovered 697 * the device. 698 * @flags: DEV_FLAG_XXX flags. Use atomic bitfield operations to modify. 699 * 700 * At the lowest level, every device in a Linux system is represented by an 701 * instance of struct device. The device structure contains the information 702 * that the device model core needs to model the system. Most subsystems, 703 * however, track additional information about the devices they host. As a 704 * result, it is rare for devices to be represented by bare device structures; 705 * instead, that structure, like kobject structures, is usually embedded within 706 * a higher-level representation of the device. 707 */ 708 struct device { 709 struct kobject kobj; 710 struct device *parent; 711 712 struct device_private *p; 713 714 const char *init_name; /* initial name of the device */ 715 const struct device_type *type; 716 717 const struct bus_type *bus; /* type of bus device is on */ 718 struct device_driver *driver; /* which driver has allocated this 719 device */ 720 void *platform_data; /* Platform specific data, device 721 core doesn't touch it */ 722 void *driver_data; /* Driver data, set and get with 723 dev_set_drvdata/dev_get_drvdata */ 724 struct { 725 const char *name; 726 spinlock_t lock; 727 } driver_override; 728 struct mutex mutex; /* mutex to synchronize calls to 729 * its driver. 730 */ 731 732 struct dev_links_info links; 733 struct dev_pm_info power; 734 struct dev_pm_domain *pm_domain; 735 736 #ifdef CONFIG_ENERGY_MODEL 737 struct em_perf_domain *em_pd; 738 #endif 739 740 #ifdef CONFIG_PINCTRL 741 struct dev_pin_info *pins; 742 #endif 743 struct dev_msi_info msi; 744 #ifdef CONFIG_ARCH_HAS_DMA_OPS 745 const struct dma_map_ops *dma_ops; 746 #endif 747 u64 *dma_mask; /* dma mask (if dma'able device) */ 748 u64 coherent_dma_mask;/* Like dma_mask, but for 749 alloc_coherent mappings as 750 not all hardware supports 751 64 bit addresses for consistent 752 allocations such descriptors. */ 753 u64 bus_dma_limit; /* upstream dma constraint */ 754 const struct bus_dma_region *dma_range_map; 755 756 struct device_dma_parameters *dma_parms; 757 758 struct list_head dma_pools; /* dma pools (if dma'ble) */ 759 760 #ifdef CONFIG_DMA_DECLARE_COHERENT 761 struct dma_coherent_mem *dma_mem; /* internal for coherent mem 762 override */ 763 #endif 764 #ifdef CONFIG_DMA_CMA 765 struct cma *cma_area; /* contiguous memory area for dma 766 allocations */ 767 #endif 768 #ifdef CONFIG_SWIOTLB 769 struct io_tlb_mem *dma_io_tlb_mem; 770 #endif 771 #ifdef CONFIG_SWIOTLB_DYNAMIC 772 struct list_head dma_io_tlb_pools; 773 spinlock_t dma_io_tlb_lock; 774 bool dma_uses_io_tlb; 775 #endif 776 /* arch specific additions */ 777 struct dev_archdata archdata; 778 779 struct device_node *of_node; /* associated device tree node */ 780 struct fwnode_handle *fwnode; /* firmware device node */ 781 782 #ifdef CONFIG_NUMA 783 int numa_node; /* NUMA node this device is close to */ 784 #endif 785 dev_t devt; /* dev_t, creates the sysfs "dev" */ 786 u32 id; /* device instance */ 787 788 spinlock_t devres_lock; 789 struct list_head devres_head; 790 791 const struct class *class; 792 const struct attribute_group **groups; /* optional groups */ 793 794 void (*release)(struct device *dev); 795 struct iommu_group *iommu_group; 796 struct dev_iommu *iommu; 797 798 struct device_physical_location *physical_location; 799 800 enum device_removable removable; 801 802 DECLARE_BITMAP(flags, DEV_FLAG_COUNT); 803 }; 804 805 #define __create_dev_flag_accessors(accessor_name, flag_name) \ 806 static inline bool dev_##accessor_name(const struct device *dev) \ 807 { \ 808 return test_bit(flag_name, dev->flags); \ 809 } \ 810 static inline void dev_set_##accessor_name(struct device *dev) \ 811 { \ 812 set_bit(flag_name, dev->flags); \ 813 } \ 814 static inline void dev_clear_##accessor_name(struct device *dev) \ 815 { \ 816 clear_bit(flag_name, dev->flags); \ 817 } \ 818 static inline void dev_assign_##accessor_name(struct device *dev, bool value) \ 819 { \ 820 assign_bit(flag_name, dev->flags, value); \ 821 } \ 822 static inline bool dev_test_and_set_##accessor_name(struct device *dev) \ 823 { \ 824 return test_and_set_bit(flag_name, dev->flags); \ 825 } 826 827 __create_dev_flag_accessors(ready_to_probe, DEV_FLAG_READY_TO_PROBE); 828 __create_dev_flag_accessors(can_match, DEV_FLAG_CAN_MATCH); 829 __create_dev_flag_accessors(dma_iommu, DEV_FLAG_DMA_IOMMU); 830 __create_dev_flag_accessors(dma_skip_sync, DEV_FLAG_DMA_SKIP_SYNC); 831 __create_dev_flag_accessors(dma_ops_bypass, DEV_FLAG_DMA_OPS_BYPASS); 832 __create_dev_flag_accessors(state_synced, DEV_FLAG_STATE_SYNCED); 833 __create_dev_flag_accessors(dma_coherent, DEV_FLAG_DMA_COHERENT); 834 __create_dev_flag_accessors(of_node_reused, DEV_FLAG_OF_NODE_REUSED); 835 __create_dev_flag_accessors(offline_disabled, DEV_FLAG_OFFLINE_DISABLED); 836 __create_dev_flag_accessors(offline, DEV_FLAG_OFFLINE); 837 838 #undef __create_dev_flag_accessors 839 840 /** 841 * struct device_link - Device link representation. 842 * @supplier: The device on the supplier end of the link. 843 * @s_node: Hook to the supplier device's list of links to consumers. 844 * @consumer: The device on the consumer end of the link. 845 * @c_node: Hook to the consumer device's list of links to suppliers. 846 * @link_dev: device used to expose link details in sysfs 847 * @status: The state of the link (with respect to the presence of drivers). 848 * @flags: Link flags. 849 * @rpm_active: Whether or not the consumer device is runtime-PM-active. 850 * @kref: Count repeated addition of the same link. 851 * @rm_work: Work structure used for removing the link. 852 * @supplier_preactivated: Supplier has been made active before consumer probe. 853 */ 854 struct device_link { 855 struct device *supplier; 856 struct list_head s_node; 857 struct device *consumer; 858 struct list_head c_node; 859 struct device link_dev; 860 enum device_link_state status; 861 u32 flags; 862 refcount_t rpm_active; 863 struct kref kref; 864 struct work_struct rm_work; 865 bool supplier_preactivated; /* Owned by consumer probe. */ 866 }; 867 868 #define kobj_to_dev(__kobj) container_of_const(__kobj, struct device, kobj) 869 870 int __device_set_driver_override(struct device *dev, const char *s, size_t len); 871 872 /** 873 * device_set_driver_override() - Helper to set or clear driver override. 874 * @dev: Device to change 875 * @s: NUL-terminated string, new driver name to force a match, pass empty 876 * string to clear it ("" or "\n", where the latter is only for sysfs 877 * interface). 878 * 879 * Helper to set or clear driver override of a device. 880 * 881 * Returns: 0 on success or a negative error code on failure. 882 */ 883 static inline int device_set_driver_override(struct device *dev, const char *s) 884 { 885 return __device_set_driver_override(dev, s, s ? strlen(s) : 0); 886 } 887 888 /** 889 * device_has_driver_override() - Check if a driver override has been set. 890 * @dev: device to check 891 * 892 * Returns true if a driver override has been set for this device. 893 */ 894 static inline bool device_has_driver_override(struct device *dev) 895 { 896 guard(spinlock)(&dev->driver_override.lock); 897 return !!dev->driver_override.name; 898 } 899 900 /** 901 * device_match_driver_override() - Match a driver against the device's driver_override. 902 * @dev: device to check 903 * @drv: driver to match against 904 * 905 * Returns > 0 if a driver override is set and matches the given driver, 0 if a 906 * driver override is set but does not match, or < 0 if a driver override is not 907 * set at all. 908 */ 909 static inline int device_match_driver_override(struct device *dev, 910 const struct device_driver *drv) 911 { 912 guard(spinlock)(&dev->driver_override.lock); 913 if (dev->driver_override.name) 914 return !strcmp(dev->driver_override.name, drv->name); 915 return -1; 916 } 917 918 /** 919 * device_iommu_mapped - Returns true when the device DMA is translated 920 * by an IOMMU 921 * @dev: Device to perform the check on 922 */ 923 static inline bool device_iommu_mapped(struct device *dev) 924 { 925 return (dev->iommu_group != NULL); 926 } 927 928 /* Get the wakeup routines, which depend on struct device */ 929 #include <linux/pm_wakeup.h> 930 931 /** 932 * dev_name - Return a device's name. 933 * @dev: Device with name to get. 934 * Return: The kobject name of the device, or its initial name if unavailable. 935 */ 936 static inline const char *dev_name(const struct device *dev) 937 { 938 /* Use the init name until the kobject becomes available */ 939 if (dev->init_name) 940 return dev->init_name; 941 942 return kobject_name(&dev->kobj); 943 } 944 945 /** 946 * dev_bus_name - Return a device's bus/class name, if at all possible 947 * @dev: struct device to get the bus/class name of 948 * 949 * Will return the name of the bus/class the device is attached to. If it is 950 * not attached to a bus/class, an empty string will be returned. 951 */ 952 static inline const char *dev_bus_name(const struct device *dev) 953 { 954 return dev->bus ? dev->bus->name : (dev->class ? dev->class->name : ""); 955 } 956 957 __printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...); 958 959 #ifdef CONFIG_NUMA 960 static inline int dev_to_node(struct device *dev) 961 { 962 return dev->numa_node; 963 } 964 static inline void set_dev_node(struct device *dev, int node) 965 { 966 dev->numa_node = node; 967 } 968 #else 969 static inline int dev_to_node(struct device *dev) 970 { 971 return NUMA_NO_NODE; 972 } 973 static inline void set_dev_node(struct device *dev, int node) 974 { 975 } 976 #endif 977 978 static inline struct irq_domain *dev_get_msi_domain(const struct device *dev) 979 { 980 #ifdef CONFIG_GENERIC_MSI_IRQ 981 return dev->msi.domain; 982 #else 983 return NULL; 984 #endif 985 } 986 987 static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d) 988 { 989 #ifdef CONFIG_GENERIC_MSI_IRQ 990 dev->msi.domain = d; 991 #endif 992 } 993 994 static inline void *dev_get_drvdata(const struct device *dev) 995 { 996 return dev->driver_data; 997 } 998 999 static inline void dev_set_drvdata(struct device *dev, void *data) 1000 { 1001 dev->driver_data = data; 1002 } 1003 1004 static inline struct pm_subsys_data *dev_to_psd(struct device *dev) 1005 { 1006 return dev ? dev->power.subsys_data : NULL; 1007 } 1008 1009 static inline unsigned int dev_get_uevent_suppress(const struct device *dev) 1010 { 1011 return dev->kobj.uevent_suppress; 1012 } 1013 1014 static inline void dev_set_uevent_suppress(struct device *dev, int val) 1015 { 1016 dev->kobj.uevent_suppress = val; 1017 } 1018 1019 static inline int device_is_registered(struct device *dev) 1020 { 1021 return dev->kobj.state_in_sysfs; 1022 } 1023 1024 static inline void device_enable_async_suspend(struct device *dev) 1025 { 1026 if (!dev->power.is_prepared) 1027 dev->power.async_suspend = true; 1028 } 1029 1030 static inline void device_disable_async_suspend(struct device *dev) 1031 { 1032 if (!dev->power.is_prepared) 1033 dev->power.async_suspend = false; 1034 } 1035 1036 static inline bool device_async_suspend_enabled(struct device *dev) 1037 { 1038 return !!dev->power.async_suspend; 1039 } 1040 1041 static inline bool device_pm_not_required(struct device *dev) 1042 { 1043 return dev->power.no_pm; 1044 } 1045 1046 static inline void device_set_pm_not_required(struct device *dev) 1047 { 1048 dev->power.no_pm = true; 1049 #ifdef CONFIG_PM 1050 dev->power.no_callbacks = true; 1051 #endif 1052 } 1053 1054 static inline void dev_pm_syscore_device(struct device *dev, bool val) 1055 { 1056 #ifdef CONFIG_PM_SLEEP 1057 dev->power.syscore = val; 1058 #endif 1059 } 1060 1061 static inline void dev_pm_set_driver_flags(struct device *dev, u32 flags) 1062 { 1063 dev->power.driver_flags = flags; 1064 } 1065 1066 static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags) 1067 { 1068 return !!(dev->power.driver_flags & flags); 1069 } 1070 1071 static inline bool dev_pm_smart_suspend(struct device *dev) 1072 { 1073 #ifdef CONFIG_PM_SLEEP 1074 return dev->power.smart_suspend; 1075 #else 1076 return false; 1077 #endif 1078 } 1079 1080 /* 1081 * dev_pm_set_strict_midlayer - Update the device's power.strict_midlayer flag 1082 * @dev: Target device. 1083 * @val: New flag value. 1084 * 1085 * When set, power.strict_midlayer means that the middle layer power management 1086 * code (typically, a bus type or a PM domain) does not expect its runtime PM 1087 * suspend callback to be invoked at all during system-wide PM transitions and 1088 * it does not expect its runtime PM resume callback to be invoked at any point 1089 * when runtime PM is disabled for the device during system-wide PM transitions. 1090 */ 1091 static inline void dev_pm_set_strict_midlayer(struct device *dev, bool val) 1092 { 1093 #ifdef CONFIG_PM_SLEEP 1094 dev->power.strict_midlayer = val; 1095 #endif 1096 } 1097 1098 static inline bool dev_pm_strict_midlayer_is_set(struct device *dev) 1099 { 1100 #ifdef CONFIG_PM_SLEEP 1101 return dev->power.strict_midlayer; 1102 #else 1103 return false; 1104 #endif 1105 } 1106 1107 static inline void device_lock(struct device *dev) 1108 { 1109 mutex_lock(&dev->mutex); 1110 } 1111 1112 static inline int device_lock_interruptible(struct device *dev) 1113 { 1114 return mutex_lock_interruptible(&dev->mutex); 1115 } 1116 1117 static inline int device_trylock(struct device *dev) 1118 { 1119 return mutex_trylock(&dev->mutex); 1120 } 1121 1122 static inline void device_unlock(struct device *dev) 1123 { 1124 mutex_unlock(&dev->mutex); 1125 } 1126 1127 DEFINE_GUARD(device, struct device *, device_lock(_T), device_unlock(_T)) 1128 DEFINE_GUARD_COND(device, _intr, device_lock_interruptible(_T), _RET == 0) 1129 DEFINE_GUARD_COND(device, _try, device_trylock(_T)) 1130 1131 static inline void device_lock_assert(struct device *dev) 1132 { 1133 lockdep_assert_held(&dev->mutex); 1134 } 1135 1136 static inline int dev_set_drv_sync_state(struct device *dev, 1137 void (*fn)(struct device *dev)) 1138 { 1139 if (!dev || !dev->driver) 1140 return 0; 1141 if (dev->driver->sync_state && dev->driver->sync_state != fn) 1142 return -EBUSY; 1143 if (!dev->driver->sync_state) 1144 dev->driver->sync_state = fn; 1145 return 0; 1146 } 1147 1148 static inline void dev_set_removable(struct device *dev, 1149 enum device_removable removable) 1150 { 1151 dev->removable = removable; 1152 } 1153 1154 static inline bool dev_is_removable(struct device *dev) 1155 { 1156 return dev->removable == DEVICE_REMOVABLE; 1157 } 1158 1159 static inline bool dev_removable_is_valid(struct device *dev) 1160 { 1161 return dev->removable != DEVICE_REMOVABLE_NOT_SUPPORTED; 1162 } 1163 1164 /* 1165 * High level routines for use by the bus drivers 1166 */ 1167 int __must_check device_register(struct device *dev); 1168 void device_unregister(struct device *dev); 1169 void device_initialize(struct device *dev); 1170 int __must_check device_add(struct device *dev); 1171 void device_del(struct device *dev); 1172 1173 DEFINE_FREE(device_del, struct device *, if (_T) device_del(_T)) 1174 1175 int device_for_each_child(struct device *parent, void *data, 1176 device_iter_t fn); 1177 int device_for_each_child_reverse(struct device *parent, void *data, 1178 device_iter_t fn); 1179 int device_for_each_child_reverse_from(struct device *parent, 1180 struct device *from, void *data, 1181 device_iter_t fn); 1182 struct device *device_find_child(struct device *parent, const void *data, 1183 device_match_t match); 1184 /** 1185 * device_find_child_by_name - device iterator for locating a child device. 1186 * @parent: parent struct device 1187 * @name: name of the child device 1188 * 1189 * This is similar to the device_find_child() function above, but it 1190 * returns a reference to a device that has the name @name. 1191 * 1192 * NOTE: you will need to drop the reference with put_device() after use. 1193 */ 1194 static inline struct device *device_find_child_by_name(struct device *parent, 1195 const char *name) 1196 { 1197 return device_find_child(parent, name, device_match_name); 1198 } 1199 1200 /** 1201 * device_find_any_child - device iterator for locating a child device, if any. 1202 * @parent: parent struct device 1203 * 1204 * This is similar to the device_find_child() function above, but it 1205 * returns a reference to a child device, if any. 1206 * 1207 * NOTE: you will need to drop the reference with put_device() after use. 1208 */ 1209 static inline struct device *device_find_any_child(struct device *parent) 1210 { 1211 return device_find_child(parent, NULL, device_match_any); 1212 } 1213 1214 int device_rename(struct device *dev, const char *new_name); 1215 int device_move(struct device *dev, struct device *new_parent, 1216 enum dpm_order dpm_order); 1217 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid); 1218 1219 static inline bool device_supports_offline(struct device *dev) 1220 { 1221 return dev->bus && dev->bus->offline && dev->bus->online; 1222 } 1223 1224 #define __device_lock_set_class(dev, name, key) \ 1225 do { \ 1226 struct device *__d2 __maybe_unused = dev; \ 1227 lock_set_class(&__d2->mutex.dep_map, name, key, 0, _THIS_IP_); \ 1228 } while (0) 1229 1230 /** 1231 * device_lock_set_class - Specify a temporary lock class while a device 1232 * is attached to a driver 1233 * @dev: device to modify 1234 * @key: lock class key data 1235 * 1236 * This must be called with the device_lock() already held, for example 1237 * from driver ->probe(). Take care to only override the default 1238 * lockdep_no_validate class. 1239 */ 1240 #ifdef CONFIG_LOCKDEP 1241 #define device_lock_set_class(dev, key) \ 1242 do { \ 1243 struct device *__d = dev; \ 1244 dev_WARN_ONCE(__d, !lockdep_match_class(&__d->mutex, \ 1245 &__lockdep_no_validate__), \ 1246 "overriding existing custom lock class\n"); \ 1247 __device_lock_set_class(__d, #key, key); \ 1248 } while (0) 1249 #else 1250 #define device_lock_set_class(dev, key) __device_lock_set_class(dev, #key, key) 1251 #endif 1252 1253 /** 1254 * device_lock_reset_class - Return a device to the default lockdep novalidate state 1255 * @dev: device to modify 1256 * 1257 * This must be called with the device_lock() already held, for example 1258 * from driver ->remove(). 1259 */ 1260 #define device_lock_reset_class(dev) \ 1261 do { \ 1262 struct device *__d __maybe_unused = dev; \ 1263 lock_set_novalidate_class(&__d->mutex.dep_map, "&dev->mutex", \ 1264 _THIS_IP_); \ 1265 } while (0) 1266 1267 void lock_device_hotplug(void); 1268 void unlock_device_hotplug(void); 1269 int lock_device_hotplug_sysfs(void); 1270 int device_offline(struct device *dev); 1271 int device_online(struct device *dev); 1272 1273 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode); 1274 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode); 1275 void device_set_node(struct device *dev, struct fwnode_handle *fwnode); 1276 int device_add_of_node(struct device *dev, struct device_node *of_node); 1277 void device_remove_of_node(struct device *dev); 1278 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2); 1279 struct device *get_dev_from_fwnode(struct fwnode_handle *fwnode); 1280 1281 static inline struct device_node *dev_of_node(struct device *dev) 1282 { 1283 if (!IS_ENABLED(CONFIG_OF) || !dev) 1284 return NULL; 1285 return dev->of_node; 1286 } 1287 1288 static inline int dev_num_vf(struct device *dev) 1289 { 1290 if (dev->bus && dev->bus->num_vf) 1291 return dev->bus->num_vf(dev); 1292 return 0; 1293 } 1294 1295 /* 1296 * Root device objects for grouping under /sys/devices 1297 */ 1298 struct device *__root_device_register(const char *name, struct module *owner); 1299 1300 /* This is a macro to avoid include problems with THIS_MODULE */ 1301 #define root_device_register(name) \ 1302 __root_device_register(name, THIS_MODULE) 1303 1304 void root_device_unregister(struct device *root); 1305 1306 static inline void *dev_get_platdata(const struct device *dev) 1307 { 1308 return dev->platform_data; 1309 } 1310 1311 /* 1312 * Manual binding of a device to driver. See drivers/base/bus.c 1313 * for information on use. 1314 */ 1315 int __must_check device_driver_attach(const struct device_driver *drv, 1316 struct device *dev); 1317 int __must_check device_bind_driver(struct device *dev); 1318 void device_release_driver(struct device *dev); 1319 int __must_check device_attach(struct device *dev); 1320 int __must_check driver_attach(const struct device_driver *drv); 1321 void device_initial_probe(struct device *dev); 1322 int __must_check device_reprobe(struct device *dev); 1323 1324 bool device_is_bound(struct device *dev); 1325 1326 /* 1327 * Easy functions for dynamically creating devices on the fly 1328 */ 1329 __printf(5, 6) struct device * 1330 device_create(const struct class *cls, struct device *parent, dev_t devt, 1331 void *drvdata, const char *fmt, ...); 1332 __printf(6, 7) struct device * 1333 device_create_with_groups(const struct class *cls, struct device *parent, dev_t devt, 1334 void *drvdata, const struct attribute_group **groups, 1335 const char *fmt, ...); 1336 void device_destroy(const struct class *cls, dev_t devt); 1337 1338 int __must_check device_add_groups(struct device *dev, 1339 const struct attribute_group *const *groups); 1340 void device_remove_groups(struct device *dev, 1341 const struct attribute_group *const *groups); 1342 1343 static inline int __must_check device_add_group(struct device *dev, 1344 const struct attribute_group *grp) 1345 { 1346 const struct attribute_group *groups[] = { grp, NULL }; 1347 1348 return device_add_groups(dev, groups); 1349 } 1350 1351 static inline void device_remove_group(struct device *dev, 1352 const struct attribute_group *grp) 1353 { 1354 const struct attribute_group *groups[] = { grp, NULL }; 1355 1356 device_remove_groups(dev, groups); 1357 } 1358 1359 int __must_check devm_device_add_group(struct device *dev, 1360 const struct attribute_group *grp); 1361 1362 /* 1363 * get_device - atomically increment the reference count for the device. 1364 * 1365 */ 1366 struct device *get_device(struct device *dev); 1367 void put_device(struct device *dev); 1368 1369 DEFINE_FREE(put_device, struct device *, if (_T) put_device(_T)) 1370 1371 bool kill_device(struct device *dev); 1372 1373 #ifdef CONFIG_DEVTMPFS 1374 int devtmpfs_mount(void); 1375 #else 1376 static inline int devtmpfs_mount(void) { return 0; } 1377 #endif 1378 1379 /* drivers/base/power/shutdown.c */ 1380 void device_shutdown(void); 1381 1382 /* debugging and troubleshooting/diagnostic helpers. */ 1383 const char *dev_driver_string(const struct device *dev); 1384 1385 /* Device links interface. */ 1386 struct device_link *device_link_add(struct device *consumer, 1387 struct device *supplier, u32 flags); 1388 void device_link_del(struct device_link *link); 1389 void device_link_remove(void *consumer, struct device *supplier); 1390 void device_links_supplier_sync_state_pause(void); 1391 void device_links_supplier_sync_state_resume(void); 1392 void device_link_wait_removal(void); 1393 1394 static inline bool device_link_test(const struct device_link *link, u32 flags) 1395 { 1396 return !!(link->flags & flags); 1397 } 1398 1399 /* Create alias, so I can be autoloaded. */ 1400 #define MODULE_ALIAS_CHARDEV(major,minor) \ 1401 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor)) 1402 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \ 1403 MODULE_ALIAS("char-major-" __stringify(major) "-*") 1404 1405 #endif /* _DEVICE_H_ */ 1406