1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * device.h - generic, centralized driver model 4 * 5 * Copyright (c) 2001-2003 Patrick Mochel <[email protected]> 6 * Copyright (c) 2004-2009 Greg Kroah-Hartman <[email protected]> 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/ioport.h> 16 #include <linux/kobject.h> 17 #include <linux/klist.h> 18 #include <linux/list.h> 19 #include <linux/lockdep.h> 20 #include <linux/compiler.h> 21 #include <linux/types.h> 22 #include <linux/mutex.h> 23 #include <linux/pm.h> 24 #include <linux/atomic.h> 25 #include <linux/ratelimit.h> 26 #include <linux/uidgid.h> 27 #include <linux/gfp.h> 28 #include <linux/overflow.h> 29 #include <asm/device.h> 30 31 struct device; 32 struct device_private; 33 struct device_driver; 34 struct driver_private; 35 struct module; 36 struct class; 37 struct subsys_private; 38 struct bus_type; 39 struct device_node; 40 struct fwnode_handle; 41 struct iommu_ops; 42 struct iommu_group; 43 struct iommu_fwspec; 44 struct dev_pin_info; 45 struct iommu_param; 46 47 struct bus_attribute { 48 struct attribute attr; 49 ssize_t (*show)(struct bus_type *bus, char *buf); 50 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count); 51 }; 52 53 #define BUS_ATTR_RW(_name) \ 54 struct bus_attribute bus_attr_##_name = __ATTR_RW(_name) 55 #define BUS_ATTR_RO(_name) \ 56 struct bus_attribute bus_attr_##_name = __ATTR_RO(_name) 57 #define BUS_ATTR_WO(_name) \ 58 struct bus_attribute bus_attr_##_name = __ATTR_WO(_name) 59 60 extern int __must_check bus_create_file(struct bus_type *, 61 struct bus_attribute *); 62 extern void bus_remove_file(struct bus_type *, struct bus_attribute *); 63 64 /** 65 * struct bus_type - The bus type of the device 66 * 67 * @name: The name of the bus. 68 * @dev_name: Used for subsystems to enumerate devices like ("foo%u", dev->id). 69 * @dev_root: Default device to use as the parent. 70 * @bus_groups: Default attributes of the bus. 71 * @dev_groups: Default attributes of the devices on the bus. 72 * @drv_groups: Default attributes of the device drivers on the bus. 73 * @match: Called, perhaps multiple times, whenever a new device or driver 74 * is added for this bus. It should return a positive value if the 75 * given device can be handled by the given driver and zero 76 * otherwise. It may also return error code if determining that 77 * the driver supports the device is not possible. In case of 78 * -EPROBE_DEFER it will queue the device for deferred probing. 79 * @uevent: Called when a device is added, removed, or a few other things 80 * that generate uevents to add the environment variables. 81 * @probe: Called when a new device or driver add to this bus, and callback 82 * the specific driver's probe to initial the matched device. 83 * @remove: Called when a device removed from this bus. 84 * @shutdown: Called at shut-down time to quiesce the device. 85 * 86 * @online: Called to put the device back online (after offlining it). 87 * @offline: Called to put the device offline for hot-removal. May fail. 88 * 89 * @suspend: Called when a device on this bus wants to go to sleep mode. 90 * @resume: Called to bring a device on this bus out of sleep mode. 91 * @num_vf: Called to find out how many virtual functions a device on this 92 * bus supports. 93 * @dma_configure: Called to setup DMA configuration on a device on 94 * this bus. 95 * @pm: Power management operations of this bus, callback the specific 96 * device driver's pm-ops. 97 * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU 98 * driver implementations to a bus and allow the driver to do 99 * bus-specific setup 100 * @p: The private data of the driver core, only the driver core can 101 * touch this. 102 * @lock_key: Lock class key for use by the lock validator 103 * @need_parent_lock: When probing or removing a device on this bus, the 104 * device core should lock the device's parent. 105 * 106 * A bus is a channel between the processor and one or more devices. For the 107 * purposes of the device model, all devices are connected via a bus, even if 108 * it is an internal, virtual, "platform" bus. Buses can plug into each other. 109 * A USB controller is usually a PCI device, for example. The device model 110 * represents the actual connections between buses and the devices they control. 111 * A bus is represented by the bus_type structure. It contains the name, the 112 * default attributes, the bus' methods, PM operations, and the driver core's 113 * private data. 114 */ 115 struct bus_type { 116 const char *name; 117 const char *dev_name; 118 struct device *dev_root; 119 const struct attribute_group **bus_groups; 120 const struct attribute_group **dev_groups; 121 const struct attribute_group **drv_groups; 122 123 int (*match)(struct device *dev, struct device_driver *drv); 124 int (*uevent)(struct device *dev, struct kobj_uevent_env *env); 125 int (*probe)(struct device *dev); 126 int (*remove)(struct device *dev); 127 void (*shutdown)(struct device *dev); 128 129 int (*online)(struct device *dev); 130 int (*offline)(struct device *dev); 131 132 int (*suspend)(struct device *dev, pm_message_t state); 133 int (*resume)(struct device *dev); 134 135 int (*num_vf)(struct device *dev); 136 137 int (*dma_configure)(struct device *dev); 138 139 const struct dev_pm_ops *pm; 140 141 const struct iommu_ops *iommu_ops; 142 143 struct subsys_private *p; 144 struct lock_class_key lock_key; 145 146 bool need_parent_lock; 147 }; 148 149 extern int __must_check bus_register(struct bus_type *bus); 150 151 extern void bus_unregister(struct bus_type *bus); 152 153 extern int __must_check bus_rescan_devices(struct bus_type *bus); 154 155 /* iterator helpers for buses */ 156 struct subsys_dev_iter { 157 struct klist_iter ki; 158 const struct device_type *type; 159 }; 160 void subsys_dev_iter_init(struct subsys_dev_iter *iter, 161 struct bus_type *subsys, 162 struct device *start, 163 const struct device_type *type); 164 struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter); 165 void subsys_dev_iter_exit(struct subsys_dev_iter *iter); 166 167 int device_match_name(struct device *dev, const void *name); 168 int device_match_of_node(struct device *dev, const void *np); 169 170 int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data, 171 int (*fn)(struct device *dev, void *data)); 172 struct device *bus_find_device(struct bus_type *bus, struct device *start, 173 const void *data, 174 int (*match)(struct device *dev, const void *data)); 175 /** 176 * bus_find_device_by_name - device iterator for locating a particular device 177 * of a specific name. 178 * @bus: bus type 179 * @start: Device to begin with 180 * @name: name of the device to match 181 */ 182 static inline struct device *bus_find_device_by_name(struct bus_type *bus, 183 struct device *start, 184 const char *name) 185 { 186 return bus_find_device(bus, start, name, device_match_name); 187 } 188 189 /** 190 * bus_find_device_by_of_node : device iterator for locating a particular device 191 * matching the of_node. 192 * @bus: bus type 193 * @np: of_node of the device to match. 194 */ 195 static inline struct device * 196 bus_find_device_by_of_node(struct bus_type *bus, const struct device_node *np) 197 { 198 return bus_find_device(bus, NULL, np, device_match_of_node); 199 } 200 201 struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id, 202 struct device *hint); 203 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, 204 void *data, int (*fn)(struct device_driver *, void *)); 205 void bus_sort_breadthfirst(struct bus_type *bus, 206 int (*compare)(const struct device *a, 207 const struct device *b)); 208 /* 209 * Bus notifiers: Get notified of addition/removal of devices 210 * and binding/unbinding of drivers to devices. 211 * In the long run, it should be a replacement for the platform 212 * notify hooks. 213 */ 214 struct notifier_block; 215 216 extern int bus_register_notifier(struct bus_type *bus, 217 struct notifier_block *nb); 218 extern int bus_unregister_notifier(struct bus_type *bus, 219 struct notifier_block *nb); 220 221 /* All 4 notifers below get called with the target struct device * 222 * as an argument. Note that those functions are likely to be called 223 * with the device lock held in the core, so be careful. 224 */ 225 #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */ 226 #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device to be removed */ 227 #define BUS_NOTIFY_REMOVED_DEVICE 0x00000003 /* device removed */ 228 #define BUS_NOTIFY_BIND_DRIVER 0x00000004 /* driver about to be 229 bound */ 230 #define BUS_NOTIFY_BOUND_DRIVER 0x00000005 /* driver bound to device */ 231 #define BUS_NOTIFY_UNBIND_DRIVER 0x00000006 /* driver about to be 232 unbound */ 233 #define BUS_NOTIFY_UNBOUND_DRIVER 0x00000007 /* driver is unbound 234 from the device */ 235 #define BUS_NOTIFY_DRIVER_NOT_BOUND 0x00000008 /* driver fails to be bound */ 236 237 extern struct kset *bus_get_kset(struct bus_type *bus); 238 extern struct klist *bus_get_device_klist(struct bus_type *bus); 239 240 /** 241 * enum probe_type - device driver probe type to try 242 * Device drivers may opt in for special handling of their 243 * respective probe routines. This tells the core what to 244 * expect and prefer. 245 * 246 * @PROBE_DEFAULT_STRATEGY: Used by drivers that work equally well 247 * whether probed synchronously or asynchronously. 248 * @PROBE_PREFER_ASYNCHRONOUS: Drivers for "slow" devices which 249 * probing order is not essential for booting the system may 250 * opt into executing their probes asynchronously. 251 * @PROBE_FORCE_SYNCHRONOUS: Use this to annotate drivers that need 252 * their probe routines to run synchronously with driver and 253 * device registration (with the exception of -EPROBE_DEFER 254 * handling - re-probing always ends up being done asynchronously). 255 * 256 * Note that the end goal is to switch the kernel to use asynchronous 257 * probing by default, so annotating drivers with 258 * %PROBE_PREFER_ASYNCHRONOUS is a temporary measure that allows us 259 * to speed up boot process while we are validating the rest of the 260 * drivers. 261 */ 262 enum probe_type { 263 PROBE_DEFAULT_STRATEGY, 264 PROBE_PREFER_ASYNCHRONOUS, 265 PROBE_FORCE_SYNCHRONOUS, 266 }; 267 268 /** 269 * struct device_driver - The basic device driver structure 270 * @name: Name of the device driver. 271 * @bus: The bus which the device of this driver belongs to. 272 * @owner: The module owner. 273 * @mod_name: Used for built-in modules. 274 * @suppress_bind_attrs: Disables bind/unbind via sysfs. 275 * @probe_type: Type of the probe (synchronous or asynchronous) to use. 276 * @of_match_table: The open firmware table. 277 * @acpi_match_table: The ACPI match table. 278 * @probe: Called to query the existence of a specific device, 279 * whether this driver can work with it, and bind the driver 280 * to a specific device. 281 * @remove: Called when the device is removed from the system to 282 * unbind a device from this driver. 283 * @shutdown: Called at shut-down time to quiesce the device. 284 * @suspend: Called to put the device to sleep mode. Usually to a 285 * low power state. 286 * @resume: Called to bring a device from sleep mode. 287 * @groups: Default attributes that get created by the driver core 288 * automatically. 289 * @pm: Power management operations of the device which matched 290 * this driver. 291 * @coredump: Called when sysfs entry is written to. The device driver 292 * is expected to call the dev_coredump API resulting in a 293 * uevent. 294 * @p: Driver core's private data, no one other than the driver 295 * core can touch this. 296 * 297 * The device driver-model tracks all of the drivers known to the system. 298 * The main reason for this tracking is to enable the driver core to match 299 * up drivers with new devices. Once drivers are known objects within the 300 * system, however, a number of other things become possible. Device drivers 301 * can export information and configuration variables that are independent 302 * of any specific device. 303 */ 304 struct device_driver { 305 const char *name; 306 struct bus_type *bus; 307 308 struct module *owner; 309 const char *mod_name; /* used for built-in modules */ 310 311 bool suppress_bind_attrs; /* disables bind/unbind via sysfs */ 312 enum probe_type probe_type; 313 314 const struct of_device_id *of_match_table; 315 const struct acpi_device_id *acpi_match_table; 316 317 int (*probe) (struct device *dev); 318 int (*remove) (struct device *dev); 319 void (*shutdown) (struct device *dev); 320 int (*suspend) (struct device *dev, pm_message_t state); 321 int (*resume) (struct device *dev); 322 const struct attribute_group **groups; 323 324 const struct dev_pm_ops *pm; 325 void (*coredump) (struct device *dev); 326 327 struct driver_private *p; 328 }; 329 330 331 extern int __must_check driver_register(struct device_driver *drv); 332 extern void driver_unregister(struct device_driver *drv); 333 334 extern struct device_driver *driver_find(const char *name, 335 struct bus_type *bus); 336 extern int driver_probe_done(void); 337 extern void wait_for_device_probe(void); 338 339 /* sysfs interface for exporting driver attributes */ 340 341 struct driver_attribute { 342 struct attribute attr; 343 ssize_t (*show)(struct device_driver *driver, char *buf); 344 ssize_t (*store)(struct device_driver *driver, const char *buf, 345 size_t count); 346 }; 347 348 #define DRIVER_ATTR_RW(_name) \ 349 struct driver_attribute driver_attr_##_name = __ATTR_RW(_name) 350 #define DRIVER_ATTR_RO(_name) \ 351 struct driver_attribute driver_attr_##_name = __ATTR_RO(_name) 352 #define DRIVER_ATTR_WO(_name) \ 353 struct driver_attribute driver_attr_##_name = __ATTR_WO(_name) 354 355 extern int __must_check driver_create_file(struct device_driver *driver, 356 const struct driver_attribute *attr); 357 extern void driver_remove_file(struct device_driver *driver, 358 const struct driver_attribute *attr); 359 360 extern int __must_check driver_for_each_device(struct device_driver *drv, 361 struct device *start, 362 void *data, 363 int (*fn)(struct device *dev, 364 void *)); 365 struct device *driver_find_device(struct device_driver *drv, 366 struct device *start, const void *data, 367 int (*match)(struct device *dev, const void *data)); 368 369 /** 370 * driver_find_device_by_name - device iterator for locating a particular device 371 * of a specific name. 372 * @driver: the driver we're iterating 373 * @name: name of the device to match 374 */ 375 static inline struct device *driver_find_device_by_name(struct device_driver *drv, 376 const char *name) 377 { 378 return driver_find_device(drv, NULL, name, device_match_name); 379 } 380 381 /** 382 * driver_find_device_by_of_node- device iterator for locating a particular device 383 * by of_node pointer. 384 * @driver: the driver we're iterating 385 * @np: of_node pointer to match. 386 */ 387 static inline struct device * 388 driver_find_device_by_of_node(struct device_driver *drv, 389 const struct device_node *np) 390 { 391 return driver_find_device(drv, NULL, np, device_match_of_node); 392 } 393 394 void driver_deferred_probe_add(struct device *dev); 395 int driver_deferred_probe_check_state(struct device *dev); 396 int driver_deferred_probe_check_state_continue(struct device *dev); 397 398 /** 399 * struct subsys_interface - interfaces to device functions 400 * @name: name of the device function 401 * @subsys: subsytem of the devices to attach to 402 * @node: the list of functions registered at the subsystem 403 * @add_dev: device hookup to device function handler 404 * @remove_dev: device hookup to device function handler 405 * 406 * Simple interfaces attached to a subsystem. Multiple interfaces can 407 * attach to a subsystem and its devices. Unlike drivers, they do not 408 * exclusively claim or control devices. Interfaces usually represent 409 * a specific functionality of a subsystem/class of devices. 410 */ 411 struct subsys_interface { 412 const char *name; 413 struct bus_type *subsys; 414 struct list_head node; 415 int (*add_dev)(struct device *dev, struct subsys_interface *sif); 416 void (*remove_dev)(struct device *dev, struct subsys_interface *sif); 417 }; 418 419 int subsys_interface_register(struct subsys_interface *sif); 420 void subsys_interface_unregister(struct subsys_interface *sif); 421 422 int subsys_system_register(struct bus_type *subsys, 423 const struct attribute_group **groups); 424 int subsys_virtual_register(struct bus_type *subsys, 425 const struct attribute_group **groups); 426 427 /** 428 * struct class - device classes 429 * @name: Name of the class. 430 * @owner: The module owner. 431 * @class_groups: Default attributes of this class. 432 * @dev_groups: Default attributes of the devices that belong to the class. 433 * @dev_kobj: The kobject that represents this class and links it into the hierarchy. 434 * @dev_uevent: Called when a device is added, removed from this class, or a 435 * few other things that generate uevents to add the environment 436 * variables. 437 * @devnode: Callback to provide the devtmpfs. 438 * @class_release: Called to release this class. 439 * @dev_release: Called to release the device. 440 * @shutdown_pre: Called at shut-down time before driver shutdown. 441 * @ns_type: Callbacks so sysfs can detemine namespaces. 442 * @namespace: Namespace of the device belongs to this class. 443 * @get_ownership: Allows class to specify uid/gid of the sysfs directories 444 * for the devices belonging to the class. Usually tied to 445 * device's namespace. 446 * @pm: The default device power management operations of this class. 447 * @p: The private data of the driver core, no one other than the 448 * driver core can touch this. 449 * 450 * A class is a higher-level view of a device that abstracts out low-level 451 * implementation details. Drivers may see a SCSI disk or an ATA disk, but, 452 * at the class level, they are all simply disks. Classes allow user space 453 * to work with devices based on what they do, rather than how they are 454 * connected or how they work. 455 */ 456 struct class { 457 const char *name; 458 struct module *owner; 459 460 const struct attribute_group **class_groups; 461 const struct attribute_group **dev_groups; 462 struct kobject *dev_kobj; 463 464 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env); 465 char *(*devnode)(struct device *dev, umode_t *mode); 466 467 void (*class_release)(struct class *class); 468 void (*dev_release)(struct device *dev); 469 470 int (*shutdown_pre)(struct device *dev); 471 472 const struct kobj_ns_type_operations *ns_type; 473 const void *(*namespace)(struct device *dev); 474 475 void (*get_ownership)(struct device *dev, kuid_t *uid, kgid_t *gid); 476 477 const struct dev_pm_ops *pm; 478 479 struct subsys_private *p; 480 }; 481 482 struct class_dev_iter { 483 struct klist_iter ki; 484 const struct device_type *type; 485 }; 486 487 extern struct kobject *sysfs_dev_block_kobj; 488 extern struct kobject *sysfs_dev_char_kobj; 489 extern int __must_check __class_register(struct class *class, 490 struct lock_class_key *key); 491 extern void class_unregister(struct class *class); 492 493 /* This is a #define to keep the compiler from merging different 494 * instances of the __key variable */ 495 #define class_register(class) \ 496 ({ \ 497 static struct lock_class_key __key; \ 498 __class_register(class, &__key); \ 499 }) 500 501 struct class_compat; 502 struct class_compat *class_compat_register(const char *name); 503 void class_compat_unregister(struct class_compat *cls); 504 int class_compat_create_link(struct class_compat *cls, struct device *dev, 505 struct device *device_link); 506 void class_compat_remove_link(struct class_compat *cls, struct device *dev, 507 struct device *device_link); 508 509 extern void class_dev_iter_init(struct class_dev_iter *iter, 510 struct class *class, 511 struct device *start, 512 const struct device_type *type); 513 extern struct device *class_dev_iter_next(struct class_dev_iter *iter); 514 extern void class_dev_iter_exit(struct class_dev_iter *iter); 515 516 extern int class_for_each_device(struct class *class, struct device *start, 517 void *data, 518 int (*fn)(struct device *dev, void *data)); 519 extern struct device *class_find_device(struct class *class, 520 struct device *start, const void *data, 521 int (*match)(struct device *, const void *)); 522 523 /** 524 * class_find_device_by_name - device iterator for locating a particular device 525 * of a specific name. 526 * @class: class type 527 * @name: name of the device to match 528 */ 529 static inline struct device *class_find_device_by_name(struct class *class, 530 const char *name) 531 { 532 return class_find_device(class, NULL, name, device_match_name); 533 } 534 535 /** 536 * class_find_device_by_of_node : device iterator for locating a particular device 537 * matching the of_node. 538 * @class: class type 539 * @np: of_node of the device to match. 540 */ 541 static inline struct device * 542 class_find_device_by_of_node(struct class *class, const struct device_node *np) 543 { 544 return class_find_device(class, NULL, np, device_match_of_node); 545 } 546 547 struct class_attribute { 548 struct attribute attr; 549 ssize_t (*show)(struct class *class, struct class_attribute *attr, 550 char *buf); 551 ssize_t (*store)(struct class *class, struct class_attribute *attr, 552 const char *buf, size_t count); 553 }; 554 555 #define CLASS_ATTR_RW(_name) \ 556 struct class_attribute class_attr_##_name = __ATTR_RW(_name) 557 #define CLASS_ATTR_RO(_name) \ 558 struct class_attribute class_attr_##_name = __ATTR_RO(_name) 559 #define CLASS_ATTR_WO(_name) \ 560 struct class_attribute class_attr_##_name = __ATTR_WO(_name) 561 562 extern int __must_check class_create_file_ns(struct class *class, 563 const struct class_attribute *attr, 564 const void *ns); 565 extern void class_remove_file_ns(struct class *class, 566 const struct class_attribute *attr, 567 const void *ns); 568 569 static inline int __must_check class_create_file(struct class *class, 570 const struct class_attribute *attr) 571 { 572 return class_create_file_ns(class, attr, NULL); 573 } 574 575 static inline void class_remove_file(struct class *class, 576 const struct class_attribute *attr) 577 { 578 return class_remove_file_ns(class, attr, NULL); 579 } 580 581 /* Simple class attribute that is just a static string */ 582 struct class_attribute_string { 583 struct class_attribute attr; 584 char *str; 585 }; 586 587 /* Currently read-only only */ 588 #define _CLASS_ATTR_STRING(_name, _mode, _str) \ 589 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str } 590 #define CLASS_ATTR_STRING(_name, _mode, _str) \ 591 struct class_attribute_string class_attr_##_name = \ 592 _CLASS_ATTR_STRING(_name, _mode, _str) 593 594 extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr, 595 char *buf); 596 597 struct class_interface { 598 struct list_head node; 599 struct class *class; 600 601 int (*add_dev) (struct device *, struct class_interface *); 602 void (*remove_dev) (struct device *, struct class_interface *); 603 }; 604 605 extern int __must_check class_interface_register(struct class_interface *); 606 extern void class_interface_unregister(struct class_interface *); 607 608 extern struct class * __must_check __class_create(struct module *owner, 609 const char *name, 610 struct lock_class_key *key); 611 extern void class_destroy(struct class *cls); 612 613 /* This is a #define to keep the compiler from merging different 614 * instances of the __key variable */ 615 #define class_create(owner, name) \ 616 ({ \ 617 static struct lock_class_key __key; \ 618 __class_create(owner, name, &__key); \ 619 }) 620 621 /* 622 * The type of device, "struct device" is embedded in. A class 623 * or bus can contain devices of different types 624 * like "partitions" and "disks", "mouse" and "event". 625 * This identifies the device type and carries type-specific 626 * information, equivalent to the kobj_type of a kobject. 627 * If "name" is specified, the uevent will contain it in 628 * the DEVTYPE variable. 629 */ 630 struct device_type { 631 const char *name; 632 const struct attribute_group **groups; 633 int (*uevent)(struct device *dev, struct kobj_uevent_env *env); 634 char *(*devnode)(struct device *dev, umode_t *mode, 635 kuid_t *uid, kgid_t *gid); 636 void (*release)(struct device *dev); 637 638 const struct dev_pm_ops *pm; 639 }; 640 641 /* interface for exporting device attributes */ 642 struct device_attribute { 643 struct attribute attr; 644 ssize_t (*show)(struct device *dev, struct device_attribute *attr, 645 char *buf); 646 ssize_t (*store)(struct device *dev, struct device_attribute *attr, 647 const char *buf, size_t count); 648 }; 649 650 struct dev_ext_attribute { 651 struct device_attribute attr; 652 void *var; 653 }; 654 655 ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr, 656 char *buf); 657 ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr, 658 const char *buf, size_t count); 659 ssize_t device_show_int(struct device *dev, struct device_attribute *attr, 660 char *buf); 661 ssize_t device_store_int(struct device *dev, struct device_attribute *attr, 662 const char *buf, size_t count); 663 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr, 664 char *buf); 665 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr, 666 const char *buf, size_t count); 667 668 #define DEVICE_ATTR(_name, _mode, _show, _store) \ 669 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store) 670 #define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \ 671 struct device_attribute dev_attr_##_name = \ 672 __ATTR_PREALLOC(_name, _mode, _show, _store) 673 #define DEVICE_ATTR_RW(_name) \ 674 struct device_attribute dev_attr_##_name = __ATTR_RW(_name) 675 #define DEVICE_ATTR_RO(_name) \ 676 struct device_attribute dev_attr_##_name = __ATTR_RO(_name) 677 #define DEVICE_ATTR_WO(_name) \ 678 struct device_attribute dev_attr_##_name = __ATTR_WO(_name) 679 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \ 680 struct dev_ext_attribute dev_attr_##_name = \ 681 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) } 682 #define DEVICE_INT_ATTR(_name, _mode, _var) \ 683 struct dev_ext_attribute dev_attr_##_name = \ 684 { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) } 685 #define DEVICE_BOOL_ATTR(_name, _mode, _var) \ 686 struct dev_ext_attribute dev_attr_##_name = \ 687 { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) } 688 #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \ 689 struct device_attribute dev_attr_##_name = \ 690 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) 691 692 extern int device_create_file(struct device *device, 693 const struct device_attribute *entry); 694 extern void device_remove_file(struct device *dev, 695 const struct device_attribute *attr); 696 extern bool device_remove_file_self(struct device *dev, 697 const struct device_attribute *attr); 698 extern int __must_check device_create_bin_file(struct device *dev, 699 const struct bin_attribute *attr); 700 extern void device_remove_bin_file(struct device *dev, 701 const struct bin_attribute *attr); 702 703 /* device resource management */ 704 typedef void (*dr_release_t)(struct device *dev, void *res); 705 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data); 706 707 #ifdef CONFIG_DEBUG_DEVRES 708 extern void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, 709 int nid, const char *name) __malloc; 710 #define devres_alloc(release, size, gfp) \ 711 __devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release) 712 #define devres_alloc_node(release, size, gfp, nid) \ 713 __devres_alloc_node(release, size, gfp, nid, #release) 714 #else 715 extern void *devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, 716 int nid) __malloc; 717 static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp) 718 { 719 return devres_alloc_node(release, size, gfp, NUMA_NO_NODE); 720 } 721 #endif 722 723 extern void devres_for_each_res(struct device *dev, dr_release_t release, 724 dr_match_t match, void *match_data, 725 void (*fn)(struct device *, void *, void *), 726 void *data); 727 extern void devres_free(void *res); 728 extern void devres_add(struct device *dev, void *res); 729 extern void *devres_find(struct device *dev, dr_release_t release, 730 dr_match_t match, void *match_data); 731 extern void *devres_get(struct device *dev, void *new_res, 732 dr_match_t match, void *match_data); 733 extern void *devres_remove(struct device *dev, dr_release_t release, 734 dr_match_t match, void *match_data); 735 extern int devres_destroy(struct device *dev, dr_release_t release, 736 dr_match_t match, void *match_data); 737 extern int devres_release(struct device *dev, dr_release_t release, 738 dr_match_t match, void *match_data); 739 740 /* devres group */ 741 extern void * __must_check devres_open_group(struct device *dev, void *id, 742 gfp_t gfp); 743 extern void devres_close_group(struct device *dev, void *id); 744 extern void devres_remove_group(struct device *dev, void *id); 745 extern int devres_release_group(struct device *dev, void *id); 746 747 /* managed devm_k.alloc/kfree for device drivers */ 748 extern void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __malloc; 749 extern __printf(3, 0) 750 char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, 751 va_list ap) __malloc; 752 extern __printf(3, 4) 753 char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...) __malloc; 754 static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp) 755 { 756 return devm_kmalloc(dev, size, gfp | __GFP_ZERO); 757 } 758 static inline void *devm_kmalloc_array(struct device *dev, 759 size_t n, size_t size, gfp_t flags) 760 { 761 size_t bytes; 762 763 if (unlikely(check_mul_overflow(n, size, &bytes))) 764 return NULL; 765 766 return devm_kmalloc(dev, bytes, flags); 767 } 768 static inline void *devm_kcalloc(struct device *dev, 769 size_t n, size_t size, gfp_t flags) 770 { 771 return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO); 772 } 773 extern void devm_kfree(struct device *dev, const void *p); 774 extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc; 775 extern const char *devm_kstrdup_const(struct device *dev, 776 const char *s, gfp_t gfp); 777 extern void *devm_kmemdup(struct device *dev, const void *src, size_t len, 778 gfp_t gfp); 779 780 extern unsigned long devm_get_free_pages(struct device *dev, 781 gfp_t gfp_mask, unsigned int order); 782 extern void devm_free_pages(struct device *dev, unsigned long addr); 783 784 void __iomem *devm_ioremap_resource(struct device *dev, 785 const struct resource *res); 786 787 void __iomem *devm_of_iomap(struct device *dev, 788 struct device_node *node, int index, 789 resource_size_t *size); 790 791 /* allows to add/remove a custom action to devres stack */ 792 int devm_add_action(struct device *dev, void (*action)(void *), void *data); 793 void devm_remove_action(struct device *dev, void (*action)(void *), void *data); 794 void devm_release_action(struct device *dev, void (*action)(void *), void *data); 795 796 static inline int devm_add_action_or_reset(struct device *dev, 797 void (*action)(void *), void *data) 798 { 799 int ret; 800 801 ret = devm_add_action(dev, action, data); 802 if (ret) 803 action(data); 804 805 return ret; 806 } 807 808 /** 809 * devm_alloc_percpu - Resource-managed alloc_percpu 810 * @dev: Device to allocate per-cpu memory for 811 * @type: Type to allocate per-cpu memory for 812 * 813 * Managed alloc_percpu. Per-cpu memory allocated with this function is 814 * automatically freed on driver detach. 815 * 816 * RETURNS: 817 * Pointer to allocated memory on success, NULL on failure. 818 */ 819 #define devm_alloc_percpu(dev, type) \ 820 ((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \ 821 __alignof__(type))) 822 823 void __percpu *__devm_alloc_percpu(struct device *dev, size_t size, 824 size_t align); 825 void devm_free_percpu(struct device *dev, void __percpu *pdata); 826 827 struct device_dma_parameters { 828 /* 829 * a low level driver may set these to teach IOMMU code about 830 * sg limitations. 831 */ 832 unsigned int max_segment_size; 833 unsigned long segment_boundary_mask; 834 }; 835 836 /** 837 * struct device_connection - Device Connection Descriptor 838 * @fwnode: The device node of the connected device 839 * @endpoint: The names of the two devices connected together 840 * @id: Unique identifier for the connection 841 * @list: List head, private, for internal use only 842 * 843 * NOTE: @fwnode is not used together with @endpoint. @fwnode is used when 844 * platform firmware defines the connection. When the connection is registered 845 * with device_connection_add() @endpoint is used instead. 846 */ 847 struct device_connection { 848 struct fwnode_handle *fwnode; 849 const char *endpoint[2]; 850 const char *id; 851 struct list_head list; 852 }; 853 854 void *device_connection_find_match(struct device *dev, const char *con_id, 855 void *data, 856 void *(*match)(struct device_connection *con, 857 int ep, void *data)); 858 859 struct device *device_connection_find(struct device *dev, const char *con_id); 860 861 void device_connection_add(struct device_connection *con); 862 void device_connection_remove(struct device_connection *con); 863 864 /** 865 * device_connections_add - Add multiple device connections at once 866 * @cons: Zero terminated array of device connection descriptors 867 */ 868 static inline void device_connections_add(struct device_connection *cons) 869 { 870 struct device_connection *c; 871 872 for (c = cons; c->endpoint[0]; c++) 873 device_connection_add(c); 874 } 875 876 /** 877 * device_connections_remove - Remove multiple device connections at once 878 * @cons: Zero terminated array of device connection descriptors 879 */ 880 static inline void device_connections_remove(struct device_connection *cons) 881 { 882 struct device_connection *c; 883 884 for (c = cons; c->endpoint[0]; c++) 885 device_connection_remove(c); 886 } 887 888 /** 889 * enum device_link_state - Device link states. 890 * @DL_STATE_NONE: The presence of the drivers is not being tracked. 891 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present. 892 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not. 893 * @DL_STATE_CONSUMER_PROBE: The consumer is probing (supplier driver present). 894 * @DL_STATE_ACTIVE: Both the supplier and consumer drivers are present. 895 * @DL_STATE_SUPPLIER_UNBIND: The supplier driver is unbinding. 896 */ 897 enum device_link_state { 898 DL_STATE_NONE = -1, 899 DL_STATE_DORMANT = 0, 900 DL_STATE_AVAILABLE, 901 DL_STATE_CONSUMER_PROBE, 902 DL_STATE_ACTIVE, 903 DL_STATE_SUPPLIER_UNBIND, 904 }; 905 906 /* 907 * Device link flags. 908 * 909 * STATELESS: The core won't track the presence of supplier/consumer drivers. 910 * AUTOREMOVE_CONSUMER: Remove the link automatically on consumer driver unbind. 911 * PM_RUNTIME: If set, the runtime PM framework will use this link. 912 * RPM_ACTIVE: Run pm_runtime_get_sync() on the supplier during link creation. 913 * AUTOREMOVE_SUPPLIER: Remove the link automatically on supplier driver unbind. 914 * AUTOPROBE_CONSUMER: Probe consumer driver automatically after supplier binds. 915 */ 916 #define DL_FLAG_STATELESS BIT(0) 917 #define DL_FLAG_AUTOREMOVE_CONSUMER BIT(1) 918 #define DL_FLAG_PM_RUNTIME BIT(2) 919 #define DL_FLAG_RPM_ACTIVE BIT(3) 920 #define DL_FLAG_AUTOREMOVE_SUPPLIER BIT(4) 921 #define DL_FLAG_AUTOPROBE_CONSUMER BIT(5) 922 923 /** 924 * struct device_link - Device link representation. 925 * @supplier: The device on the supplier end of the link. 926 * @s_node: Hook to the supplier device's list of links to consumers. 927 * @consumer: The device on the consumer end of the link. 928 * @c_node: Hook to the consumer device's list of links to suppliers. 929 * @status: The state of the link (with respect to the presence of drivers). 930 * @flags: Link flags. 931 * @rpm_active: Whether or not the consumer device is runtime-PM-active. 932 * @kref: Count repeated addition of the same link. 933 * @rcu_head: An RCU head to use for deferred execution of SRCU callbacks. 934 * @supplier_preactivated: Supplier has been made active before consumer probe. 935 */ 936 struct device_link { 937 struct device *supplier; 938 struct list_head s_node; 939 struct device *consumer; 940 struct list_head c_node; 941 enum device_link_state status; 942 u32 flags; 943 refcount_t rpm_active; 944 struct kref kref; 945 #ifdef CONFIG_SRCU 946 struct rcu_head rcu_head; 947 #endif 948 bool supplier_preactivated; /* Owned by consumer probe. */ 949 }; 950 951 /** 952 * enum dl_dev_state - Device driver presence tracking information. 953 * @DL_DEV_NO_DRIVER: There is no driver attached to the device. 954 * @DL_DEV_PROBING: A driver is probing. 955 * @DL_DEV_DRIVER_BOUND: The driver has been bound to the device. 956 * @DL_DEV_UNBINDING: The driver is unbinding from the device. 957 */ 958 enum dl_dev_state { 959 DL_DEV_NO_DRIVER = 0, 960 DL_DEV_PROBING, 961 DL_DEV_DRIVER_BOUND, 962 DL_DEV_UNBINDING, 963 }; 964 965 /** 966 * struct dev_links_info - Device data related to device links. 967 * @suppliers: List of links to supplier devices. 968 * @consumers: List of links to consumer devices. 969 * @status: Driver status information. 970 */ 971 struct dev_links_info { 972 struct list_head suppliers; 973 struct list_head consumers; 974 enum dl_dev_state status; 975 }; 976 977 /** 978 * struct device - The basic device structure 979 * @parent: The device's "parent" device, the device to which it is attached. 980 * In most cases, a parent device is some sort of bus or host 981 * controller. If parent is NULL, the device, is a top-level device, 982 * which is not usually what you want. 983 * @p: Holds the private data of the driver core portions of the device. 984 * See the comment of the struct device_private for detail. 985 * @kobj: A top-level, abstract class from which other classes are derived. 986 * @init_name: Initial name of the device. 987 * @type: The type of device. 988 * This identifies the device type and carries type-specific 989 * information. 990 * @mutex: Mutex to synchronize calls to its driver. 991 * @bus: Type of bus device is on. 992 * @driver: Which driver has allocated this 993 * @platform_data: Platform data specific to the device. 994 * Example: For devices on custom boards, as typical of embedded 995 * and SOC based hardware, Linux often uses platform_data to point 996 * to board-specific structures describing devices and how they 997 * are wired. That can include what ports are available, chip 998 * variants, which GPIO pins act in what additional roles, and so 999 * on. This shrinks the "Board Support Packages" (BSPs) and 1000 * minimizes board-specific #ifdefs in drivers. 1001 * @driver_data: Private pointer for driver specific info. 1002 * @links: Links to suppliers and consumers of this device. 1003 * @power: For device power management. 1004 * See Documentation/driver-api/pm/devices.rst for details. 1005 * @pm_domain: Provide callbacks that are executed during system suspend, 1006 * hibernation, system resume and during runtime PM transitions 1007 * along with subsystem-level and driver-level callbacks. 1008 * @pins: For device pin management. 1009 * See Documentation/driver-api/pinctl.rst for details. 1010 * @msi_list: Hosts MSI descriptors 1011 * @msi_domain: The generic MSI domain this device is using. 1012 * @numa_node: NUMA node this device is close to. 1013 * @dma_ops: DMA mapping operations for this device. 1014 * @dma_mask: Dma mask (if dma'ble device). 1015 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all 1016 * hardware supports 64-bit addresses for consistent allocations 1017 * such descriptors. 1018 * @bus_dma_mask: Mask of an upstream bridge or bus which imposes a smaller DMA 1019 * limit than the device itself supports. 1020 * @dma_pfn_offset: offset of DMA memory range relatively of RAM 1021 * @dma_parms: A low level driver may set these to teach IOMMU code about 1022 * segment limitations. 1023 * @dma_pools: Dma pools (if dma'ble device). 1024 * @dma_mem: Internal for coherent mem override. 1025 * @cma_area: Contiguous memory area for dma allocations 1026 * @archdata: For arch-specific additions. 1027 * @of_node: Associated device tree node. 1028 * @fwnode: Associated device node supplied by platform firmware. 1029 * @devt: For creating the sysfs "dev". 1030 * @id: device instance 1031 * @devres_lock: Spinlock to protect the resource of the device. 1032 * @devres_head: The resources list of the device. 1033 * @knode_class: The node used to add the device to the class list. 1034 * @class: The class of the device. 1035 * @groups: Optional attribute groups. 1036 * @release: Callback to free the device after all references have 1037 * gone away. This should be set by the allocator of the 1038 * device (i.e. the bus driver that discovered the device). 1039 * @iommu_group: IOMMU group the device belongs to. 1040 * @iommu_fwspec: IOMMU-specific properties supplied by firmware. 1041 * @iommu_param: Per device generic IOMMU runtime data 1042 * 1043 * @offline_disabled: If set, the device is permanently online. 1044 * @offline: Set after successful invocation of bus type's .offline(). 1045 * @of_node_reused: Set if the device-tree node is shared with an ancestor 1046 * device. 1047 * @dma_coherent: this particular device is dma coherent, even if the 1048 * architecture supports non-coherent devices. 1049 * 1050 * At the lowest level, every device in a Linux system is represented by an 1051 * instance of struct device. The device structure contains the information 1052 * that the device model core needs to model the system. Most subsystems, 1053 * however, track additional information about the devices they host. As a 1054 * result, it is rare for devices to be represented by bare device structures; 1055 * instead, that structure, like kobject structures, is usually embedded within 1056 * a higher-level representation of the device. 1057 */ 1058 struct device { 1059 struct kobject kobj; 1060 struct device *parent; 1061 1062 struct device_private *p; 1063 1064 const char *init_name; /* initial name of the device */ 1065 const struct device_type *type; 1066 1067 struct bus_type *bus; /* type of bus device is on */ 1068 struct device_driver *driver; /* which driver has allocated this 1069 device */ 1070 void *platform_data; /* Platform specific data, device 1071 core doesn't touch it */ 1072 void *driver_data; /* Driver data, set and get with 1073 dev_set_drvdata/dev_get_drvdata */ 1074 struct mutex mutex; /* mutex to synchronize calls to 1075 * its driver. 1076 */ 1077 1078 struct dev_links_info links; 1079 struct dev_pm_info power; 1080 struct dev_pm_domain *pm_domain; 1081 1082 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN 1083 struct irq_domain *msi_domain; 1084 #endif 1085 #ifdef CONFIG_PINCTRL 1086 struct dev_pin_info *pins; 1087 #endif 1088 #ifdef CONFIG_GENERIC_MSI_IRQ 1089 struct list_head msi_list; 1090 #endif 1091 1092 const struct dma_map_ops *dma_ops; 1093 u64 *dma_mask; /* dma mask (if dma'able device) */ 1094 u64 coherent_dma_mask;/* Like dma_mask, but for 1095 alloc_coherent mappings as 1096 not all hardware supports 1097 64 bit addresses for consistent 1098 allocations such descriptors. */ 1099 u64 bus_dma_mask; /* upstream dma_mask constraint */ 1100 unsigned long dma_pfn_offset; 1101 1102 struct device_dma_parameters *dma_parms; 1103 1104 struct list_head dma_pools; /* dma pools (if dma'ble) */ 1105 1106 #ifdef CONFIG_DMA_DECLARE_COHERENT 1107 struct dma_coherent_mem *dma_mem; /* internal for coherent mem 1108 override */ 1109 #endif 1110 #ifdef CONFIG_DMA_CMA 1111 struct cma *cma_area; /* contiguous memory area for dma 1112 allocations */ 1113 #endif 1114 /* arch specific additions */ 1115 struct dev_archdata archdata; 1116 1117 struct device_node *of_node; /* associated device tree node */ 1118 struct fwnode_handle *fwnode; /* firmware device node */ 1119 1120 #ifdef CONFIG_NUMA 1121 int numa_node; /* NUMA node this device is close to */ 1122 #endif 1123 dev_t devt; /* dev_t, creates the sysfs "dev" */ 1124 u32 id; /* device instance */ 1125 1126 spinlock_t devres_lock; 1127 struct list_head devres_head; 1128 1129 struct class *class; 1130 const struct attribute_group **groups; /* optional groups */ 1131 1132 void (*release)(struct device *dev); 1133 struct iommu_group *iommu_group; 1134 struct iommu_fwspec *iommu_fwspec; 1135 struct iommu_param *iommu_param; 1136 1137 bool offline_disabled:1; 1138 bool offline:1; 1139 bool of_node_reused:1; 1140 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ 1141 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ 1142 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) 1143 bool dma_coherent:1; 1144 #endif 1145 }; 1146 1147 static inline struct device *kobj_to_dev(struct kobject *kobj) 1148 { 1149 return container_of(kobj, struct device, kobj); 1150 } 1151 1152 /** 1153 * device_iommu_mapped - Returns true when the device DMA is translated 1154 * by an IOMMU 1155 * @dev: Device to perform the check on 1156 */ 1157 static inline bool device_iommu_mapped(struct device *dev) 1158 { 1159 return (dev->iommu_group != NULL); 1160 } 1161 1162 /* Get the wakeup routines, which depend on struct device */ 1163 #include <linux/pm_wakeup.h> 1164 1165 static inline const char *dev_name(const struct device *dev) 1166 { 1167 /* Use the init name until the kobject becomes available */ 1168 if (dev->init_name) 1169 return dev->init_name; 1170 1171 return kobject_name(&dev->kobj); 1172 } 1173 1174 extern __printf(2, 3) 1175 int dev_set_name(struct device *dev, const char *name, ...); 1176 1177 #ifdef CONFIG_NUMA 1178 static inline int dev_to_node(struct device *dev) 1179 { 1180 return dev->numa_node; 1181 } 1182 static inline void set_dev_node(struct device *dev, int node) 1183 { 1184 dev->numa_node = node; 1185 } 1186 #else 1187 static inline int dev_to_node(struct device *dev) 1188 { 1189 return NUMA_NO_NODE; 1190 } 1191 static inline void set_dev_node(struct device *dev, int node) 1192 { 1193 } 1194 #endif 1195 1196 static inline struct irq_domain *dev_get_msi_domain(const struct device *dev) 1197 { 1198 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN 1199 return dev->msi_domain; 1200 #else 1201 return NULL; 1202 #endif 1203 } 1204 1205 static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d) 1206 { 1207 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN 1208 dev->msi_domain = d; 1209 #endif 1210 } 1211 1212 static inline void *dev_get_drvdata(const struct device *dev) 1213 { 1214 return dev->driver_data; 1215 } 1216 1217 static inline void dev_set_drvdata(struct device *dev, void *data) 1218 { 1219 dev->driver_data = data; 1220 } 1221 1222 static inline struct pm_subsys_data *dev_to_psd(struct device *dev) 1223 { 1224 return dev ? dev->power.subsys_data : NULL; 1225 } 1226 1227 static inline unsigned int dev_get_uevent_suppress(const struct device *dev) 1228 { 1229 return dev->kobj.uevent_suppress; 1230 } 1231 1232 static inline void dev_set_uevent_suppress(struct device *dev, int val) 1233 { 1234 dev->kobj.uevent_suppress = val; 1235 } 1236 1237 static inline int device_is_registered(struct device *dev) 1238 { 1239 return dev->kobj.state_in_sysfs; 1240 } 1241 1242 static inline void device_enable_async_suspend(struct device *dev) 1243 { 1244 if (!dev->power.is_prepared) 1245 dev->power.async_suspend = true; 1246 } 1247 1248 static inline void device_disable_async_suspend(struct device *dev) 1249 { 1250 if (!dev->power.is_prepared) 1251 dev->power.async_suspend = false; 1252 } 1253 1254 static inline bool device_async_suspend_enabled(struct device *dev) 1255 { 1256 return !!dev->power.async_suspend; 1257 } 1258 1259 static inline bool device_pm_not_required(struct device *dev) 1260 { 1261 return dev->power.no_pm; 1262 } 1263 1264 static inline void device_set_pm_not_required(struct device *dev) 1265 { 1266 dev->power.no_pm = true; 1267 } 1268 1269 static inline void dev_pm_syscore_device(struct device *dev, bool val) 1270 { 1271 #ifdef CONFIG_PM_SLEEP 1272 dev->power.syscore = val; 1273 #endif 1274 } 1275 1276 static inline void dev_pm_set_driver_flags(struct device *dev, u32 flags) 1277 { 1278 dev->power.driver_flags = flags; 1279 } 1280 1281 static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags) 1282 { 1283 return !!(dev->power.driver_flags & flags); 1284 } 1285 1286 static inline void device_lock(struct device *dev) 1287 { 1288 mutex_lock(&dev->mutex); 1289 } 1290 1291 static inline int device_lock_interruptible(struct device *dev) 1292 { 1293 return mutex_lock_interruptible(&dev->mutex); 1294 } 1295 1296 static inline int device_trylock(struct device *dev) 1297 { 1298 return mutex_trylock(&dev->mutex); 1299 } 1300 1301 static inline void device_unlock(struct device *dev) 1302 { 1303 mutex_unlock(&dev->mutex); 1304 } 1305 1306 static inline void device_lock_assert(struct device *dev) 1307 { 1308 lockdep_assert_held(&dev->mutex); 1309 } 1310 1311 static inline struct device_node *dev_of_node(struct device *dev) 1312 { 1313 if (!IS_ENABLED(CONFIG_OF) || !dev) 1314 return NULL; 1315 return dev->of_node; 1316 } 1317 1318 void driver_init(void); 1319 1320 /* 1321 * High level routines for use by the bus drivers 1322 */ 1323 extern int __must_check device_register(struct device *dev); 1324 extern void device_unregister(struct device *dev); 1325 extern void device_initialize(struct device *dev); 1326 extern int __must_check device_add(struct device *dev); 1327 extern void device_del(struct device *dev); 1328 extern int device_for_each_child(struct device *dev, void *data, 1329 int (*fn)(struct device *dev, void *data)); 1330 extern int device_for_each_child_reverse(struct device *dev, void *data, 1331 int (*fn)(struct device *dev, void *data)); 1332 extern struct device *device_find_child(struct device *dev, void *data, 1333 int (*match)(struct device *dev, void *data)); 1334 extern struct device *device_find_child_by_name(struct device *parent, 1335 const char *name); 1336 extern int device_rename(struct device *dev, const char *new_name); 1337 extern int device_move(struct device *dev, struct device *new_parent, 1338 enum dpm_order dpm_order); 1339 extern const char *device_get_devnode(struct device *dev, 1340 umode_t *mode, kuid_t *uid, kgid_t *gid, 1341 const char **tmp); 1342 1343 static inline bool device_supports_offline(struct device *dev) 1344 { 1345 return dev->bus && dev->bus->offline && dev->bus->online; 1346 } 1347 1348 extern void lock_device_hotplug(void); 1349 extern void unlock_device_hotplug(void); 1350 extern int lock_device_hotplug_sysfs(void); 1351 extern int device_offline(struct device *dev); 1352 extern int device_online(struct device *dev); 1353 extern void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode); 1354 extern void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode); 1355 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2); 1356 1357 static inline int dev_num_vf(struct device *dev) 1358 { 1359 if (dev->bus && dev->bus->num_vf) 1360 return dev->bus->num_vf(dev); 1361 return 0; 1362 } 1363 1364 /* 1365 * Root device objects for grouping under /sys/devices 1366 */ 1367 extern struct device *__root_device_register(const char *name, 1368 struct module *owner); 1369 1370 /* This is a macro to avoid include problems with THIS_MODULE */ 1371 #define root_device_register(name) \ 1372 __root_device_register(name, THIS_MODULE) 1373 1374 extern void root_device_unregister(struct device *root); 1375 1376 static inline void *dev_get_platdata(const struct device *dev) 1377 { 1378 return dev->platform_data; 1379 } 1380 1381 /* 1382 * Manual binding of a device to driver. See drivers/base/bus.c 1383 * for information on use. 1384 */ 1385 extern int __must_check device_bind_driver(struct device *dev); 1386 extern void device_release_driver(struct device *dev); 1387 extern int __must_check device_attach(struct device *dev); 1388 extern int __must_check driver_attach(struct device_driver *drv); 1389 extern void device_initial_probe(struct device *dev); 1390 extern int __must_check device_reprobe(struct device *dev); 1391 1392 extern bool device_is_bound(struct device *dev); 1393 1394 /* 1395 * Easy functions for dynamically creating devices on the fly 1396 */ 1397 extern __printf(5, 0) 1398 struct device *device_create_vargs(struct class *cls, struct device *parent, 1399 dev_t devt, void *drvdata, 1400 const char *fmt, va_list vargs); 1401 extern __printf(5, 6) 1402 struct device *device_create(struct class *cls, struct device *parent, 1403 dev_t devt, void *drvdata, 1404 const char *fmt, ...); 1405 extern __printf(6, 7) 1406 struct device *device_create_with_groups(struct class *cls, 1407 struct device *parent, dev_t devt, void *drvdata, 1408 const struct attribute_group **groups, 1409 const char *fmt, ...); 1410 extern void device_destroy(struct class *cls, dev_t devt); 1411 1412 extern int __must_check device_add_groups(struct device *dev, 1413 const struct attribute_group **groups); 1414 extern void device_remove_groups(struct device *dev, 1415 const struct attribute_group **groups); 1416 1417 static inline int __must_check device_add_group(struct device *dev, 1418 const struct attribute_group *grp) 1419 { 1420 const struct attribute_group *groups[] = { grp, NULL }; 1421 1422 return device_add_groups(dev, groups); 1423 } 1424 1425 static inline void device_remove_group(struct device *dev, 1426 const struct attribute_group *grp) 1427 { 1428 const struct attribute_group *groups[] = { grp, NULL }; 1429 1430 return device_remove_groups(dev, groups); 1431 } 1432 1433 extern int __must_check devm_device_add_groups(struct device *dev, 1434 const struct attribute_group **groups); 1435 extern void devm_device_remove_groups(struct device *dev, 1436 const struct attribute_group **groups); 1437 extern int __must_check devm_device_add_group(struct device *dev, 1438 const struct attribute_group *grp); 1439 extern void devm_device_remove_group(struct device *dev, 1440 const struct attribute_group *grp); 1441 1442 /* 1443 * Platform "fixup" functions - allow the platform to have their say 1444 * about devices and actions that the general device layer doesn't 1445 * know about. 1446 */ 1447 /* Notify platform of device discovery */ 1448 extern int (*platform_notify)(struct device *dev); 1449 1450 extern int (*platform_notify_remove)(struct device *dev); 1451 1452 1453 /* 1454 * get_device - atomically increment the reference count for the device. 1455 * 1456 */ 1457 extern struct device *get_device(struct device *dev); 1458 extern void put_device(struct device *dev); 1459 1460 #ifdef CONFIG_DEVTMPFS 1461 extern int devtmpfs_create_node(struct device *dev); 1462 extern int devtmpfs_delete_node(struct device *dev); 1463 extern int devtmpfs_mount(const char *mntdir); 1464 #else 1465 static inline int devtmpfs_create_node(struct device *dev) { return 0; } 1466 static inline int devtmpfs_delete_node(struct device *dev) { return 0; } 1467 static inline int devtmpfs_mount(const char *mountpoint) { return 0; } 1468 #endif 1469 1470 /* drivers/base/power/shutdown.c */ 1471 extern void device_shutdown(void); 1472 1473 /* debugging and troubleshooting/diagnostic helpers. */ 1474 extern const char *dev_driver_string(const struct device *dev); 1475 1476 /* Device links interface. */ 1477 struct device_link *device_link_add(struct device *consumer, 1478 struct device *supplier, u32 flags); 1479 void device_link_del(struct device_link *link); 1480 void device_link_remove(void *consumer, struct device *supplier); 1481 1482 #ifndef dev_fmt 1483 #define dev_fmt(fmt) fmt 1484 #endif 1485 1486 #ifdef CONFIG_PRINTK 1487 1488 __printf(3, 0) __cold 1489 int dev_vprintk_emit(int level, const struct device *dev, 1490 const char *fmt, va_list args); 1491 __printf(3, 4) __cold 1492 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...); 1493 1494 __printf(3, 4) __cold 1495 void dev_printk(const char *level, const struct device *dev, 1496 const char *fmt, ...); 1497 __printf(2, 3) __cold 1498 void _dev_emerg(const struct device *dev, const char *fmt, ...); 1499 __printf(2, 3) __cold 1500 void _dev_alert(const struct device *dev, const char *fmt, ...); 1501 __printf(2, 3) __cold 1502 void _dev_crit(const struct device *dev, const char *fmt, ...); 1503 __printf(2, 3) __cold 1504 void _dev_err(const struct device *dev, const char *fmt, ...); 1505 __printf(2, 3) __cold 1506 void _dev_warn(const struct device *dev, const char *fmt, ...); 1507 __printf(2, 3) __cold 1508 void _dev_notice(const struct device *dev, const char *fmt, ...); 1509 __printf(2, 3) __cold 1510 void _dev_info(const struct device *dev, const char *fmt, ...); 1511 1512 #else 1513 1514 static inline __printf(3, 0) 1515 int dev_vprintk_emit(int level, const struct device *dev, 1516 const char *fmt, va_list args) 1517 { return 0; } 1518 static inline __printf(3, 4) 1519 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...) 1520 { return 0; } 1521 1522 static inline void __dev_printk(const char *level, const struct device *dev, 1523 struct va_format *vaf) 1524 {} 1525 static inline __printf(3, 4) 1526 void dev_printk(const char *level, const struct device *dev, 1527 const char *fmt, ...) 1528 {} 1529 1530 static inline __printf(2, 3) 1531 void _dev_emerg(const struct device *dev, const char *fmt, ...) 1532 {} 1533 static inline __printf(2, 3) 1534 void _dev_crit(const struct device *dev, const char *fmt, ...) 1535 {} 1536 static inline __printf(2, 3) 1537 void _dev_alert(const struct device *dev, const char *fmt, ...) 1538 {} 1539 static inline __printf(2, 3) 1540 void _dev_err(const struct device *dev, const char *fmt, ...) 1541 {} 1542 static inline __printf(2, 3) 1543 void _dev_warn(const struct device *dev, const char *fmt, ...) 1544 {} 1545 static inline __printf(2, 3) 1546 void _dev_notice(const struct device *dev, const char *fmt, ...) 1547 {} 1548 static inline __printf(2, 3) 1549 void _dev_info(const struct device *dev, const char *fmt, ...) 1550 {} 1551 1552 #endif 1553 1554 /* 1555 * #defines for all the dev_<level> macros to prefix with whatever 1556 * possible use of #define dev_fmt(fmt) ... 1557 */ 1558 1559 #define dev_emerg(dev, fmt, ...) \ 1560 _dev_emerg(dev, dev_fmt(fmt), ##__VA_ARGS__) 1561 #define dev_crit(dev, fmt, ...) \ 1562 _dev_crit(dev, dev_fmt(fmt), ##__VA_ARGS__) 1563 #define dev_alert(dev, fmt, ...) \ 1564 _dev_alert(dev, dev_fmt(fmt), ##__VA_ARGS__) 1565 #define dev_err(dev, fmt, ...) \ 1566 _dev_err(dev, dev_fmt(fmt), ##__VA_ARGS__) 1567 #define dev_warn(dev, fmt, ...) \ 1568 _dev_warn(dev, dev_fmt(fmt), ##__VA_ARGS__) 1569 #define dev_notice(dev, fmt, ...) \ 1570 _dev_notice(dev, dev_fmt(fmt), ##__VA_ARGS__) 1571 #define dev_info(dev, fmt, ...) \ 1572 _dev_info(dev, dev_fmt(fmt), ##__VA_ARGS__) 1573 1574 #if defined(CONFIG_DYNAMIC_DEBUG) 1575 #define dev_dbg(dev, fmt, ...) \ 1576 dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__) 1577 #elif defined(DEBUG) 1578 #define dev_dbg(dev, fmt, ...) \ 1579 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__) 1580 #else 1581 #define dev_dbg(dev, fmt, ...) \ 1582 ({ \ 1583 if (0) \ 1584 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \ 1585 }) 1586 #endif 1587 1588 #ifdef CONFIG_PRINTK 1589 #define dev_level_once(dev_level, dev, fmt, ...) \ 1590 do { \ 1591 static bool __print_once __read_mostly; \ 1592 \ 1593 if (!__print_once) { \ 1594 __print_once = true; \ 1595 dev_level(dev, fmt, ##__VA_ARGS__); \ 1596 } \ 1597 } while (0) 1598 #else 1599 #define dev_level_once(dev_level, dev, fmt, ...) \ 1600 do { \ 1601 if (0) \ 1602 dev_level(dev, fmt, ##__VA_ARGS__); \ 1603 } while (0) 1604 #endif 1605 1606 #define dev_emerg_once(dev, fmt, ...) \ 1607 dev_level_once(dev_emerg, dev, fmt, ##__VA_ARGS__) 1608 #define dev_alert_once(dev, fmt, ...) \ 1609 dev_level_once(dev_alert, dev, fmt, ##__VA_ARGS__) 1610 #define dev_crit_once(dev, fmt, ...) \ 1611 dev_level_once(dev_crit, dev, fmt, ##__VA_ARGS__) 1612 #define dev_err_once(dev, fmt, ...) \ 1613 dev_level_once(dev_err, dev, fmt, ##__VA_ARGS__) 1614 #define dev_warn_once(dev, fmt, ...) \ 1615 dev_level_once(dev_warn, dev, fmt, ##__VA_ARGS__) 1616 #define dev_notice_once(dev, fmt, ...) \ 1617 dev_level_once(dev_notice, dev, fmt, ##__VA_ARGS__) 1618 #define dev_info_once(dev, fmt, ...) \ 1619 dev_level_once(dev_info, dev, fmt, ##__VA_ARGS__) 1620 #define dev_dbg_once(dev, fmt, ...) \ 1621 dev_level_once(dev_dbg, dev, fmt, ##__VA_ARGS__) 1622 1623 #define dev_level_ratelimited(dev_level, dev, fmt, ...) \ 1624 do { \ 1625 static DEFINE_RATELIMIT_STATE(_rs, \ 1626 DEFAULT_RATELIMIT_INTERVAL, \ 1627 DEFAULT_RATELIMIT_BURST); \ 1628 if (__ratelimit(&_rs)) \ 1629 dev_level(dev, fmt, ##__VA_ARGS__); \ 1630 } while (0) 1631 1632 #define dev_emerg_ratelimited(dev, fmt, ...) \ 1633 dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__) 1634 #define dev_alert_ratelimited(dev, fmt, ...) \ 1635 dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__) 1636 #define dev_crit_ratelimited(dev, fmt, ...) \ 1637 dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__) 1638 #define dev_err_ratelimited(dev, fmt, ...) \ 1639 dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__) 1640 #define dev_warn_ratelimited(dev, fmt, ...) \ 1641 dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__) 1642 #define dev_notice_ratelimited(dev, fmt, ...) \ 1643 dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__) 1644 #define dev_info_ratelimited(dev, fmt, ...) \ 1645 dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__) 1646 #if defined(CONFIG_DYNAMIC_DEBUG) 1647 /* descriptor check is first to prevent flooding with "callbacks suppressed" */ 1648 #define dev_dbg_ratelimited(dev, fmt, ...) \ 1649 do { \ 1650 static DEFINE_RATELIMIT_STATE(_rs, \ 1651 DEFAULT_RATELIMIT_INTERVAL, \ 1652 DEFAULT_RATELIMIT_BURST); \ 1653 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \ 1654 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \ 1655 __ratelimit(&_rs)) \ 1656 __dynamic_dev_dbg(&descriptor, dev, dev_fmt(fmt), \ 1657 ##__VA_ARGS__); \ 1658 } while (0) 1659 #elif defined(DEBUG) 1660 #define dev_dbg_ratelimited(dev, fmt, ...) \ 1661 do { \ 1662 static DEFINE_RATELIMIT_STATE(_rs, \ 1663 DEFAULT_RATELIMIT_INTERVAL, \ 1664 DEFAULT_RATELIMIT_BURST); \ 1665 if (__ratelimit(&_rs)) \ 1666 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \ 1667 } while (0) 1668 #else 1669 #define dev_dbg_ratelimited(dev, fmt, ...) \ 1670 do { \ 1671 if (0) \ 1672 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \ 1673 } while (0) 1674 #endif 1675 1676 #ifdef VERBOSE_DEBUG 1677 #define dev_vdbg dev_dbg 1678 #else 1679 #define dev_vdbg(dev, fmt, ...) \ 1680 ({ \ 1681 if (0) \ 1682 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \ 1683 }) 1684 #endif 1685 1686 /* 1687 * dev_WARN*() acts like dev_printk(), but with the key difference of 1688 * using WARN/WARN_ONCE to include file/line information and a backtrace. 1689 */ 1690 #define dev_WARN(dev, format, arg...) \ 1691 WARN(1, "%s %s: " format, dev_driver_string(dev), dev_name(dev), ## arg); 1692 1693 #define dev_WARN_ONCE(dev, condition, format, arg...) \ 1694 WARN_ONCE(condition, "%s %s: " format, \ 1695 dev_driver_string(dev), dev_name(dev), ## arg) 1696 1697 /* Create alias, so I can be autoloaded. */ 1698 #define MODULE_ALIAS_CHARDEV(major,minor) \ 1699 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor)) 1700 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \ 1701 MODULE_ALIAS("char-major-" __stringify(major) "-*") 1702 1703 #ifdef CONFIG_SYSFS_DEPRECATED 1704 extern long sysfs_deprecated; 1705 #else 1706 #define sysfs_deprecated 0 1707 #endif 1708 1709 /** 1710 * module_driver() - Helper macro for drivers that don't do anything 1711 * special in module init/exit. This eliminates a lot of boilerplate. 1712 * Each module may only use this macro once, and calling it replaces 1713 * module_init() and module_exit(). 1714 * 1715 * @__driver: driver name 1716 * @__register: register function for this driver type 1717 * @__unregister: unregister function for this driver type 1718 * @...: Additional arguments to be passed to __register and __unregister. 1719 * 1720 * Use this macro to construct bus specific macros for registering 1721 * drivers, and do not use it on its own. 1722 */ 1723 #define module_driver(__driver, __register, __unregister, ...) \ 1724 static int __init __driver##_init(void) \ 1725 { \ 1726 return __register(&(__driver) , ##__VA_ARGS__); \ 1727 } \ 1728 module_init(__driver##_init); \ 1729 static void __exit __driver##_exit(void) \ 1730 { \ 1731 __unregister(&(__driver) , ##__VA_ARGS__); \ 1732 } \ 1733 module_exit(__driver##_exit); 1734 1735 /** 1736 * builtin_driver() - Helper macro for drivers that don't do anything 1737 * special in init and have no exit. This eliminates some boilerplate. 1738 * Each driver may only use this macro once, and calling it replaces 1739 * device_initcall (or in some cases, the legacy __initcall). This is 1740 * meant to be a direct parallel of module_driver() above but without 1741 * the __exit stuff that is not used for builtin cases. 1742 * 1743 * @__driver: driver name 1744 * @__register: register function for this driver type 1745 * @...: Additional arguments to be passed to __register 1746 * 1747 * Use this macro to construct bus specific macros for registering 1748 * drivers, and do not use it on its own. 1749 */ 1750 #define builtin_driver(__driver, __register, ...) \ 1751 static int __init __driver##_init(void) \ 1752 { \ 1753 return __register(&(__driver) , ##__VA_ARGS__); \ 1754 } \ 1755 device_initcall(__driver##_init); 1756 1757 #endif /* _DEVICE_H_ */ 1758