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