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