1 // SPDX-License-Identifier: GPL-2.0
3 * device.h - generic, centralized driver model
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.
9 * See Documentation/driver-api/driver-model/ for more information.
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>
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>
32 struct device_private;
34 struct driver_private;
37 struct subsys_private;
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);
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)
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 *);
65 * struct bus_type - The bus type of the device
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.
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.
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
93 * @dma_configure: Called to setup DMA configuration on a device on
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
100 * @p: The private data of the driver core, only the driver core can
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.
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
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;
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);
129 int (*online)(struct device *dev);
130 int (*offline)(struct device *dev);
132 int (*suspend)(struct device *dev, pm_message_t state);
133 int (*resume)(struct device *dev);
135 int (*num_vf)(struct device *dev);
137 int (*dma_configure)(struct device *dev);
139 const struct dev_pm_ops *pm;
141 const struct iommu_ops *iommu_ops;
143 struct subsys_private *p;
144 struct lock_class_key lock_key;
146 bool need_parent_lock;
149 extern int __must_check bus_register(struct bus_type *bus);
151 extern void bus_unregister(struct bus_type *bus);
153 extern int __must_check bus_rescan_devices(struct bus_type *bus);
155 /* iterator helpers for buses */
156 struct subsys_dev_iter {
157 struct klist_iter ki;
158 const struct device_type *type;
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);
167 int device_match_name(struct device *dev, const void *name);
168 int device_match_of_node(struct device *dev, const void *np);
169 int device_match_fwnode(struct device *dev, const void *fwnode);
170 int device_match_devt(struct device *dev, const void *pdevt);
172 int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
173 int (*fn)(struct device *dev, void *data));
174 struct device *bus_find_device(struct bus_type *bus, struct device *start,
176 int (*match)(struct device *dev, const void *data));
178 * bus_find_device_by_name - device iterator for locating a particular device
179 * of a specific name.
181 * @start: Device to begin with
182 * @name: name of the device to match
184 static inline struct device *bus_find_device_by_name(struct bus_type *bus,
185 struct device *start,
188 return bus_find_device(bus, start, name, device_match_name);
192 * bus_find_device_by_of_node : device iterator for locating a particular device
193 * matching the of_node.
195 * @np: of_node of the device to match.
197 static inline struct device *
198 bus_find_device_by_of_node(struct bus_type *bus, const struct device_node *np)
200 return bus_find_device(bus, NULL, np, device_match_of_node);
204 * bus_find_device_by_fwnode : device iterator for locating a particular device
205 * matching the fwnode.
207 * @fwnode: fwnode of the device to match.
209 static inline struct device *
210 bus_find_device_by_fwnode(struct bus_type *bus, const struct fwnode_handle *fwnode)
212 return bus_find_device(bus, NULL, fwnode, device_match_fwnode);
216 * bus_find_device_by_devt : device iterator for locating a particular device
217 * matching the device type.
219 * @devt: device type of the device to match.
221 static inline struct device *bus_find_device_by_devt(struct bus_type *bus,
224 return bus_find_device(bus, NULL, &devt, device_match_devt);
227 struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id,
228 struct device *hint);
229 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
230 void *data, int (*fn)(struct device_driver *, void *));
231 void bus_sort_breadthfirst(struct bus_type *bus,
232 int (*compare)(const struct device *a,
233 const struct device *b));
235 * Bus notifiers: Get notified of addition/removal of devices
236 * and binding/unbinding of drivers to devices.
237 * In the long run, it should be a replacement for the platform
240 struct notifier_block;
242 extern int bus_register_notifier(struct bus_type *bus,
243 struct notifier_block *nb);
244 extern int bus_unregister_notifier(struct bus_type *bus,
245 struct notifier_block *nb);
247 /* All 4 notifers below get called with the target struct device *
248 * as an argument. Note that those functions are likely to be called
249 * with the device lock held in the core, so be careful.
251 #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
252 #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device to be removed */
253 #define BUS_NOTIFY_REMOVED_DEVICE 0x00000003 /* device removed */
254 #define BUS_NOTIFY_BIND_DRIVER 0x00000004 /* driver about to be
256 #define BUS_NOTIFY_BOUND_DRIVER 0x00000005 /* driver bound to device */
257 #define BUS_NOTIFY_UNBIND_DRIVER 0x00000006 /* driver about to be
259 #define BUS_NOTIFY_UNBOUND_DRIVER 0x00000007 /* driver is unbound
261 #define BUS_NOTIFY_DRIVER_NOT_BOUND 0x00000008 /* driver fails to be bound */
263 extern struct kset *bus_get_kset(struct bus_type *bus);
264 extern struct klist *bus_get_device_klist(struct bus_type *bus);
267 * enum probe_type - device driver probe type to try
268 * Device drivers may opt in for special handling of their
269 * respective probe routines. This tells the core what to
272 * @PROBE_DEFAULT_STRATEGY: Used by drivers that work equally well
273 * whether probed synchronously or asynchronously.
274 * @PROBE_PREFER_ASYNCHRONOUS: Drivers for "slow" devices which
275 * probing order is not essential for booting the system may
276 * opt into executing their probes asynchronously.
277 * @PROBE_FORCE_SYNCHRONOUS: Use this to annotate drivers that need
278 * their probe routines to run synchronously with driver and
279 * device registration (with the exception of -EPROBE_DEFER
280 * handling - re-probing always ends up being done asynchronously).
282 * Note that the end goal is to switch the kernel to use asynchronous
283 * probing by default, so annotating drivers with
284 * %PROBE_PREFER_ASYNCHRONOUS is a temporary measure that allows us
285 * to speed up boot process while we are validating the rest of the
289 PROBE_DEFAULT_STRATEGY,
290 PROBE_PREFER_ASYNCHRONOUS,
291 PROBE_FORCE_SYNCHRONOUS,
295 * struct device_driver - The basic device driver structure
296 * @name: Name of the device driver.
297 * @bus: The bus which the device of this driver belongs to.
298 * @owner: The module owner.
299 * @mod_name: Used for built-in modules.
300 * @suppress_bind_attrs: Disables bind/unbind via sysfs.
301 * @probe_type: Type of the probe (synchronous or asynchronous) to use.
302 * @of_match_table: The open firmware table.
303 * @acpi_match_table: The ACPI match table.
304 * @probe: Called to query the existence of a specific device,
305 * whether this driver can work with it, and bind the driver
306 * to a specific device.
307 * @remove: Called when the device is removed from the system to
308 * unbind a device from this driver.
309 * @shutdown: Called at shut-down time to quiesce the device.
310 * @suspend: Called to put the device to sleep mode. Usually to a
312 * @resume: Called to bring a device from sleep mode.
313 * @groups: Default attributes that get created by the driver core
315 * @pm: Power management operations of the device which matched
317 * @coredump: Called when sysfs entry is written to. The device driver
318 * is expected to call the dev_coredump API resulting in a
320 * @p: Driver core's private data, no one other than the driver
321 * core can touch this.
323 * The device driver-model tracks all of the drivers known to the system.
324 * The main reason for this tracking is to enable the driver core to match
325 * up drivers with new devices. Once drivers are known objects within the
326 * system, however, a number of other things become possible. Device drivers
327 * can export information and configuration variables that are independent
328 * of any specific device.
330 struct device_driver {
332 struct bus_type *bus;
334 struct module *owner;
335 const char *mod_name; /* used for built-in modules */
337 bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
338 enum probe_type probe_type;
340 const struct of_device_id *of_match_table;
341 const struct acpi_device_id *acpi_match_table;
343 int (*probe) (struct device *dev);
344 int (*remove) (struct device *dev);
345 void (*shutdown) (struct device *dev);
346 int (*suspend) (struct device *dev, pm_message_t state);
347 int (*resume) (struct device *dev);
348 const struct attribute_group **groups;
350 const struct dev_pm_ops *pm;
351 void (*coredump) (struct device *dev);
353 struct driver_private *p;
357 extern int __must_check driver_register(struct device_driver *drv);
358 extern void driver_unregister(struct device_driver *drv);
360 extern struct device_driver *driver_find(const char *name,
361 struct bus_type *bus);
362 extern int driver_probe_done(void);
363 extern void wait_for_device_probe(void);
365 /* sysfs interface for exporting driver attributes */
367 struct driver_attribute {
368 struct attribute attr;
369 ssize_t (*show)(struct device_driver *driver, char *buf);
370 ssize_t (*store)(struct device_driver *driver, const char *buf,
374 #define DRIVER_ATTR_RW(_name) \
375 struct driver_attribute driver_attr_##_name = __ATTR_RW(_name)
376 #define DRIVER_ATTR_RO(_name) \
377 struct driver_attribute driver_attr_##_name = __ATTR_RO(_name)
378 #define DRIVER_ATTR_WO(_name) \
379 struct driver_attribute driver_attr_##_name = __ATTR_WO(_name)
381 extern int __must_check driver_create_file(struct device_driver *driver,
382 const struct driver_attribute *attr);
383 extern void driver_remove_file(struct device_driver *driver,
384 const struct driver_attribute *attr);
386 extern int __must_check driver_for_each_device(struct device_driver *drv,
387 struct device *start,
389 int (*fn)(struct device *dev,
391 struct device *driver_find_device(struct device_driver *drv,
392 struct device *start, const void *data,
393 int (*match)(struct device *dev, const void *data));
396 * driver_find_device_by_name - device iterator for locating a particular device
397 * of a specific name.
398 * @driver: the driver we're iterating
399 * @name: name of the device to match
401 static inline struct device *driver_find_device_by_name(struct device_driver *drv,
404 return driver_find_device(drv, NULL, name, device_match_name);
408 * driver_find_device_by_of_node- device iterator for locating a particular device
409 * by of_node pointer.
410 * @driver: the driver we're iterating
411 * @np: of_node pointer to match.
413 static inline struct device *
414 driver_find_device_by_of_node(struct device_driver *drv,
415 const struct device_node *np)
417 return driver_find_device(drv, NULL, np, device_match_of_node);
421 * driver_find_device_by_fwnode- device iterator for locating a particular device
423 * @driver: the driver we're iterating
424 * @fwnode: fwnode pointer to match.
426 static inline struct device *
427 driver_find_device_by_fwnode(struct device_driver *drv,
428 const struct fwnode_handle *fwnode)
430 return driver_find_device(drv, NULL, fwnode, device_match_fwnode);
434 * driver_find_device_by_devt- device iterator for locating a particular device
436 * @driver: the driver we're iterating
437 * @devt: devt pointer to match.
439 static inline struct device *driver_find_device_by_devt(struct device_driver *drv,
442 return driver_find_device(drv, NULL, &devt, device_match_devt);
445 void driver_deferred_probe_add(struct device *dev);
446 int driver_deferred_probe_check_state(struct device *dev);
447 int driver_deferred_probe_check_state_continue(struct device *dev);
450 * struct subsys_interface - interfaces to device functions
451 * @name: name of the device function
452 * @subsys: subsytem of the devices to attach to
453 * @node: the list of functions registered at the subsystem
454 * @add_dev: device hookup to device function handler
455 * @remove_dev: device hookup to device function handler
457 * Simple interfaces attached to a subsystem. Multiple interfaces can
458 * attach to a subsystem and its devices. Unlike drivers, they do not
459 * exclusively claim or control devices. Interfaces usually represent
460 * a specific functionality of a subsystem/class of devices.
462 struct subsys_interface {
464 struct bus_type *subsys;
465 struct list_head node;
466 int (*add_dev)(struct device *dev, struct subsys_interface *sif);
467 void (*remove_dev)(struct device *dev, struct subsys_interface *sif);
470 int subsys_interface_register(struct subsys_interface *sif);
471 void subsys_interface_unregister(struct subsys_interface *sif);
473 int subsys_system_register(struct bus_type *subsys,
474 const struct attribute_group **groups);
475 int subsys_virtual_register(struct bus_type *subsys,
476 const struct attribute_group **groups);
479 * struct class - device classes
480 * @name: Name of the class.
481 * @owner: The module owner.
482 * @class_groups: Default attributes of this class.
483 * @dev_groups: Default attributes of the devices that belong to the class.
484 * @dev_kobj: The kobject that represents this class and links it into the hierarchy.
485 * @dev_uevent: Called when a device is added, removed from this class, or a
486 * few other things that generate uevents to add the environment
488 * @devnode: Callback to provide the devtmpfs.
489 * @class_release: Called to release this class.
490 * @dev_release: Called to release the device.
491 * @shutdown_pre: Called at shut-down time before driver shutdown.
492 * @ns_type: Callbacks so sysfs can detemine namespaces.
493 * @namespace: Namespace of the device belongs to this class.
494 * @get_ownership: Allows class to specify uid/gid of the sysfs directories
495 * for the devices belonging to the class. Usually tied to
496 * device's namespace.
497 * @pm: The default device power management operations of this class.
498 * @p: The private data of the driver core, no one other than the
499 * driver core can touch this.
501 * A class is a higher-level view of a device that abstracts out low-level
502 * implementation details. Drivers may see a SCSI disk or an ATA disk, but,
503 * at the class level, they are all simply disks. Classes allow user space
504 * to work with devices based on what they do, rather than how they are
505 * connected or how they work.
509 struct module *owner;
511 const struct attribute_group **class_groups;
512 const struct attribute_group **dev_groups;
513 struct kobject *dev_kobj;
515 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
516 char *(*devnode)(struct device *dev, umode_t *mode);
518 void (*class_release)(struct class *class);
519 void (*dev_release)(struct device *dev);
521 int (*shutdown_pre)(struct device *dev);
523 const struct kobj_ns_type_operations *ns_type;
524 const void *(*namespace)(struct device *dev);
526 void (*get_ownership)(struct device *dev, kuid_t *uid, kgid_t *gid);
528 const struct dev_pm_ops *pm;
530 struct subsys_private *p;
533 struct class_dev_iter {
534 struct klist_iter ki;
535 const struct device_type *type;
538 extern struct kobject *sysfs_dev_block_kobj;
539 extern struct kobject *sysfs_dev_char_kobj;
540 extern int __must_check __class_register(struct class *class,
541 struct lock_class_key *key);
542 extern void class_unregister(struct class *class);
544 /* This is a #define to keep the compiler from merging different
545 * instances of the __key variable */
546 #define class_register(class) \
548 static struct lock_class_key __key; \
549 __class_register(class, &__key); \
553 struct class_compat *class_compat_register(const char *name);
554 void class_compat_unregister(struct class_compat *cls);
555 int class_compat_create_link(struct class_compat *cls, struct device *dev,
556 struct device *device_link);
557 void class_compat_remove_link(struct class_compat *cls, struct device *dev,
558 struct device *device_link);
560 extern void class_dev_iter_init(struct class_dev_iter *iter,
562 struct device *start,
563 const struct device_type *type);
564 extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
565 extern void class_dev_iter_exit(struct class_dev_iter *iter);
567 extern int class_for_each_device(struct class *class, struct device *start,
569 int (*fn)(struct device *dev, void *data));
570 extern struct device *class_find_device(struct class *class,
571 struct device *start, const void *data,
572 int (*match)(struct device *, const void *));
575 * class_find_device_by_name - device iterator for locating a particular device
576 * of a specific name.
578 * @name: name of the device to match
580 static inline struct device *class_find_device_by_name(struct class *class,
583 return class_find_device(class, NULL, name, device_match_name);
587 * class_find_device_by_of_node : device iterator for locating a particular device
588 * matching the of_node.
590 * @np: of_node of the device to match.
592 static inline struct device *
593 class_find_device_by_of_node(struct class *class, const struct device_node *np)
595 return class_find_device(class, NULL, np, device_match_of_node);
599 * class_find_device_by_fwnode : device iterator for locating a particular device
600 * matching the fwnode.
602 * @fwnode: fwnode of the device to match.
604 static inline struct device *
605 class_find_device_by_fwnode(struct class *class,
606 const struct fwnode_handle *fwnode)
608 return class_find_device(class, NULL, fwnode, device_match_fwnode);
612 * class_find_device_by_devt : device iterator for locating a particular device
613 * matching the device type.
615 * @devt: device type of the device to match.
617 static inline struct device *class_find_device_by_devt(struct class *class,
620 return class_find_device(class, NULL, &devt, device_match_devt);
623 struct class_attribute {
624 struct attribute attr;
625 ssize_t (*show)(struct class *class, struct class_attribute *attr,
627 ssize_t (*store)(struct class *class, struct class_attribute *attr,
628 const char *buf, size_t count);
631 #define CLASS_ATTR_RW(_name) \
632 struct class_attribute class_attr_##_name = __ATTR_RW(_name)
633 #define CLASS_ATTR_RO(_name) \
634 struct class_attribute class_attr_##_name = __ATTR_RO(_name)
635 #define CLASS_ATTR_WO(_name) \
636 struct class_attribute class_attr_##_name = __ATTR_WO(_name)
638 extern int __must_check class_create_file_ns(struct class *class,
639 const struct class_attribute *attr,
641 extern void class_remove_file_ns(struct class *class,
642 const struct class_attribute *attr,
645 static inline int __must_check class_create_file(struct class *class,
646 const struct class_attribute *attr)
648 return class_create_file_ns(class, attr, NULL);
651 static inline void class_remove_file(struct class *class,
652 const struct class_attribute *attr)
654 return class_remove_file_ns(class, attr, NULL);
657 /* Simple class attribute that is just a static string */
658 struct class_attribute_string {
659 struct class_attribute attr;
663 /* Currently read-only only */
664 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
665 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
666 #define CLASS_ATTR_STRING(_name, _mode, _str) \
667 struct class_attribute_string class_attr_##_name = \
668 _CLASS_ATTR_STRING(_name, _mode, _str)
670 extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
673 struct class_interface {
674 struct list_head node;
677 int (*add_dev) (struct device *, struct class_interface *);
678 void (*remove_dev) (struct device *, struct class_interface *);
681 extern int __must_check class_interface_register(struct class_interface *);
682 extern void class_interface_unregister(struct class_interface *);
684 extern struct class * __must_check __class_create(struct module *owner,
686 struct lock_class_key *key);
687 extern void class_destroy(struct class *cls);
689 /* This is a #define to keep the compiler from merging different
690 * instances of the __key variable */
691 #define class_create(owner, name) \
693 static struct lock_class_key __key; \
694 __class_create(owner, name, &__key); \
698 * The type of device, "struct device" is embedded in. A class
699 * or bus can contain devices of different types
700 * like "partitions" and "disks", "mouse" and "event".
701 * This identifies the device type and carries type-specific
702 * information, equivalent to the kobj_type of a kobject.
703 * If "name" is specified, the uevent will contain it in
704 * the DEVTYPE variable.
708 const struct attribute_group **groups;
709 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
710 char *(*devnode)(struct device *dev, umode_t *mode,
711 kuid_t *uid, kgid_t *gid);
712 void (*release)(struct device *dev);
714 const struct dev_pm_ops *pm;
717 /* interface for exporting device attributes */
718 struct device_attribute {
719 struct attribute attr;
720 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
722 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
723 const char *buf, size_t count);
726 struct dev_ext_attribute {
727 struct device_attribute attr;
731 ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
733 ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
734 const char *buf, size_t count);
735 ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
737 ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
738 const char *buf, size_t count);
739 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
741 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
742 const char *buf, size_t count);
744 #define DEVICE_ATTR(_name, _mode, _show, _store) \
745 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
746 #define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \
747 struct device_attribute dev_attr_##_name = \
748 __ATTR_PREALLOC(_name, _mode, _show, _store)
749 #define DEVICE_ATTR_RW(_name) \
750 struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
751 #define DEVICE_ATTR_RO(_name) \
752 struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
753 #define DEVICE_ATTR_WO(_name) \
754 struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
755 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
756 struct dev_ext_attribute dev_attr_##_name = \
757 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
758 #define DEVICE_INT_ATTR(_name, _mode, _var) \
759 struct dev_ext_attribute dev_attr_##_name = \
760 { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
761 #define DEVICE_BOOL_ATTR(_name, _mode, _var) \
762 struct dev_ext_attribute dev_attr_##_name = \
763 { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
764 #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
765 struct device_attribute dev_attr_##_name = \
766 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
768 extern int device_create_file(struct device *device,
769 const struct device_attribute *entry);
770 extern void device_remove_file(struct device *dev,
771 const struct device_attribute *attr);
772 extern bool device_remove_file_self(struct device *dev,
773 const struct device_attribute *attr);
774 extern int __must_check device_create_bin_file(struct device *dev,
775 const struct bin_attribute *attr);
776 extern void device_remove_bin_file(struct device *dev,
777 const struct bin_attribute *attr);
779 /* device resource management */
780 typedef void (*dr_release_t)(struct device *dev, void *res);
781 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
783 #ifdef CONFIG_DEBUG_DEVRES
784 extern void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
785 int nid, const char *name) __malloc;
786 #define devres_alloc(release, size, gfp) \
787 __devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)
788 #define devres_alloc_node(release, size, gfp, nid) \
789 __devres_alloc_node(release, size, gfp, nid, #release)
791 extern void *devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
793 static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
795 return devres_alloc_node(release, size, gfp, NUMA_NO_NODE);
799 extern void devres_for_each_res(struct device *dev, dr_release_t release,
800 dr_match_t match, void *match_data,
801 void (*fn)(struct device *, void *, void *),
803 extern void devres_free(void *res);
804 extern void devres_add(struct device *dev, void *res);
805 extern void *devres_find(struct device *dev, dr_release_t release,
806 dr_match_t match, void *match_data);
807 extern void *devres_get(struct device *dev, void *new_res,
808 dr_match_t match, void *match_data);
809 extern void *devres_remove(struct device *dev, dr_release_t release,
810 dr_match_t match, void *match_data);
811 extern int devres_destroy(struct device *dev, dr_release_t release,
812 dr_match_t match, void *match_data);
813 extern int devres_release(struct device *dev, dr_release_t release,
814 dr_match_t match, void *match_data);
817 extern void * __must_check devres_open_group(struct device *dev, void *id,
819 extern void devres_close_group(struct device *dev, void *id);
820 extern void devres_remove_group(struct device *dev, void *id);
821 extern int devres_release_group(struct device *dev, void *id);
823 /* managed devm_k.alloc/kfree for device drivers */
824 extern void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __malloc;
825 extern __printf(3, 0)
826 char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
827 va_list ap) __malloc;
828 extern __printf(3, 4)
829 char *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...) __malloc;
830 static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
832 return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
834 static inline void *devm_kmalloc_array(struct device *dev,
835 size_t n, size_t size, gfp_t flags)
839 if (unlikely(check_mul_overflow(n, size, &bytes)))
842 return devm_kmalloc(dev, bytes, flags);
844 static inline void *devm_kcalloc(struct device *dev,
845 size_t n, size_t size, gfp_t flags)
847 return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
849 extern void devm_kfree(struct device *dev, const void *p);
850 extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
851 extern const char *devm_kstrdup_const(struct device *dev,
852 const char *s, gfp_t gfp);
853 extern void *devm_kmemdup(struct device *dev, const void *src, size_t len,
856 extern unsigned long devm_get_free_pages(struct device *dev,
857 gfp_t gfp_mask, unsigned int order);
858 extern void devm_free_pages(struct device *dev, unsigned long addr);
860 void __iomem *devm_ioremap_resource(struct device *dev,
861 const struct resource *res);
863 void __iomem *devm_of_iomap(struct device *dev,
864 struct device_node *node, int index,
865 resource_size_t *size);
867 /* allows to add/remove a custom action to devres stack */
868 int devm_add_action(struct device *dev, void (*action)(void *), void *data);
869 void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
870 void devm_release_action(struct device *dev, void (*action)(void *), void *data);
872 static inline int devm_add_action_or_reset(struct device *dev,
873 void (*action)(void *), void *data)
877 ret = devm_add_action(dev, action, data);
885 * devm_alloc_percpu - Resource-managed alloc_percpu
886 * @dev: Device to allocate per-cpu memory for
887 * @type: Type to allocate per-cpu memory for
889 * Managed alloc_percpu. Per-cpu memory allocated with this function is
890 * automatically freed on driver detach.
893 * Pointer to allocated memory on success, NULL on failure.
895 #define devm_alloc_percpu(dev, type) \
896 ((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
899 void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
901 void devm_free_percpu(struct device *dev, void __percpu *pdata);
903 struct device_dma_parameters {
905 * a low level driver may set these to teach IOMMU code about
908 unsigned int max_segment_size;
909 unsigned long segment_boundary_mask;
913 * struct device_connection - Device Connection Descriptor
914 * @fwnode: The device node of the connected device
915 * @endpoint: The names of the two devices connected together
916 * @id: Unique identifier for the connection
917 * @list: List head, private, for internal use only
919 * NOTE: @fwnode is not used together with @endpoint. @fwnode is used when
920 * platform firmware defines the connection. When the connection is registered
921 * with device_connection_add() @endpoint is used instead.
923 struct device_connection {
924 struct fwnode_handle *fwnode;
925 const char *endpoint[2];
927 struct list_head list;
930 void *device_connection_find_match(struct device *dev, const char *con_id,
932 void *(*match)(struct device_connection *con,
933 int ep, void *data));
935 struct device *device_connection_find(struct device *dev, const char *con_id);
937 void device_connection_add(struct device_connection *con);
938 void device_connection_remove(struct device_connection *con);
941 * device_connections_add - Add multiple device connections at once
942 * @cons: Zero terminated array of device connection descriptors
944 static inline void device_connections_add(struct device_connection *cons)
946 struct device_connection *c;
948 for (c = cons; c->endpoint[0]; c++)
949 device_connection_add(c);
953 * device_connections_remove - Remove multiple device connections at once
954 * @cons: Zero terminated array of device connection descriptors
956 static inline void device_connections_remove(struct device_connection *cons)
958 struct device_connection *c;
960 for (c = cons; c->endpoint[0]; c++)
961 device_connection_remove(c);
965 * enum device_link_state - Device link states.
966 * @DL_STATE_NONE: The presence of the drivers is not being tracked.
967 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
968 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.
969 * @DL_STATE_CONSUMER_PROBE: The consumer is probing (supplier driver present).
970 * @DL_STATE_ACTIVE: Both the supplier and consumer drivers are present.
971 * @DL_STATE_SUPPLIER_UNBIND: The supplier driver is unbinding.
973 enum device_link_state {
975 DL_STATE_DORMANT = 0,
977 DL_STATE_CONSUMER_PROBE,
979 DL_STATE_SUPPLIER_UNBIND,
985 * STATELESS: The core won't track the presence of supplier/consumer drivers.
986 * AUTOREMOVE_CONSUMER: Remove the link automatically on consumer driver unbind.
987 * PM_RUNTIME: If set, the runtime PM framework will use this link.
988 * RPM_ACTIVE: Run pm_runtime_get_sync() on the supplier during link creation.
989 * AUTOREMOVE_SUPPLIER: Remove the link automatically on supplier driver unbind.
990 * AUTOPROBE_CONSUMER: Probe consumer driver automatically after supplier binds.
992 #define DL_FLAG_STATELESS BIT(0)
993 #define DL_FLAG_AUTOREMOVE_CONSUMER BIT(1)
994 #define DL_FLAG_PM_RUNTIME BIT(2)
995 #define DL_FLAG_RPM_ACTIVE BIT(3)
996 #define DL_FLAG_AUTOREMOVE_SUPPLIER BIT(4)
997 #define DL_FLAG_AUTOPROBE_CONSUMER BIT(5)
1000 * struct device_link - Device link representation.
1001 * @supplier: The device on the supplier end of the link.
1002 * @s_node: Hook to the supplier device's list of links to consumers.
1003 * @consumer: The device on the consumer end of the link.
1004 * @c_node: Hook to the consumer device's list of links to suppliers.
1005 * @status: The state of the link (with respect to the presence of drivers).
1006 * @flags: Link flags.
1007 * @rpm_active: Whether or not the consumer device is runtime-PM-active.
1008 * @kref: Count repeated addition of the same link.
1009 * @rcu_head: An RCU head to use for deferred execution of SRCU callbacks.
1010 * @supplier_preactivated: Supplier has been made active before consumer probe.
1012 struct device_link {
1013 struct device *supplier;
1014 struct list_head s_node;
1015 struct device *consumer;
1016 struct list_head c_node;
1017 enum device_link_state status;
1019 refcount_t rpm_active;
1022 struct rcu_head rcu_head;
1024 bool supplier_preactivated; /* Owned by consumer probe. */
1028 * enum dl_dev_state - Device driver presence tracking information.
1029 * @DL_DEV_NO_DRIVER: There is no driver attached to the device.
1030 * @DL_DEV_PROBING: A driver is probing.
1031 * @DL_DEV_DRIVER_BOUND: The driver has been bound to the device.
1032 * @DL_DEV_UNBINDING: The driver is unbinding from the device.
1035 DL_DEV_NO_DRIVER = 0,
1037 DL_DEV_DRIVER_BOUND,
1042 * struct dev_links_info - Device data related to device links.
1043 * @suppliers: List of links to supplier devices.
1044 * @consumers: List of links to consumer devices.
1045 * @status: Driver status information.
1047 struct dev_links_info {
1048 struct list_head suppliers;
1049 struct list_head consumers;
1050 enum dl_dev_state status;
1054 * struct device - The basic device structure
1055 * @parent: The device's "parent" device, the device to which it is attached.
1056 * In most cases, a parent device is some sort of bus or host
1057 * controller. If parent is NULL, the device, is a top-level device,
1058 * which is not usually what you want.
1059 * @p: Holds the private data of the driver core portions of the device.
1060 * See the comment of the struct device_private for detail.
1061 * @kobj: A top-level, abstract class from which other classes are derived.
1062 * @init_name: Initial name of the device.
1063 * @type: The type of device.
1064 * This identifies the device type and carries type-specific
1066 * @mutex: Mutex to synchronize calls to its driver.
1067 * @bus: Type of bus device is on.
1068 * @driver: Which driver has allocated this
1069 * @platform_data: Platform data specific to the device.
1070 * Example: For devices on custom boards, as typical of embedded
1071 * and SOC based hardware, Linux often uses platform_data to point
1072 * to board-specific structures describing devices and how they
1073 * are wired. That can include what ports are available, chip
1074 * variants, which GPIO pins act in what additional roles, and so
1075 * on. This shrinks the "Board Support Packages" (BSPs) and
1076 * minimizes board-specific #ifdefs in drivers.
1077 * @driver_data: Private pointer for driver specific info.
1078 * @links: Links to suppliers and consumers of this device.
1079 * @power: For device power management.
1080 * See Documentation/driver-api/pm/devices.rst for details.
1081 * @pm_domain: Provide callbacks that are executed during system suspend,
1082 * hibernation, system resume and during runtime PM transitions
1083 * along with subsystem-level and driver-level callbacks.
1084 * @pins: For device pin management.
1085 * See Documentation/driver-api/pinctl.rst for details.
1086 * @msi_list: Hosts MSI descriptors
1087 * @msi_domain: The generic MSI domain this device is using.
1088 * @numa_node: NUMA node this device is close to.
1089 * @dma_ops: DMA mapping operations for this device.
1090 * @dma_mask: Dma mask (if dma'ble device).
1091 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
1092 * hardware supports 64-bit addresses for consistent allocations
1094 * @bus_dma_mask: Mask of an upstream bridge or bus which imposes a smaller DMA
1095 * limit than the device itself supports.
1096 * @dma_pfn_offset: offset of DMA memory range relatively of RAM
1097 * @dma_parms: A low level driver may set these to teach IOMMU code about
1098 * segment limitations.
1099 * @dma_pools: Dma pools (if dma'ble device).
1100 * @dma_mem: Internal for coherent mem override.
1101 * @cma_area: Contiguous memory area for dma allocations
1102 * @archdata: For arch-specific additions.
1103 * @of_node: Associated device tree node.
1104 * @fwnode: Associated device node supplied by platform firmware.
1105 * @devt: For creating the sysfs "dev".
1106 * @id: device instance
1107 * @devres_lock: Spinlock to protect the resource of the device.
1108 * @devres_head: The resources list of the device.
1109 * @knode_class: The node used to add the device to the class list.
1110 * @class: The class of the device.
1111 * @groups: Optional attribute groups.
1112 * @release: Callback to free the device after all references have
1113 * gone away. This should be set by the allocator of the
1114 * device (i.e. the bus driver that discovered the device).
1115 * @iommu_group: IOMMU group the device belongs to.
1116 * @iommu_fwspec: IOMMU-specific properties supplied by firmware.
1117 * @iommu_param: Per device generic IOMMU runtime data
1119 * @offline_disabled: If set, the device is permanently online.
1120 * @offline: Set after successful invocation of bus type's .offline().
1121 * @of_node_reused: Set if the device-tree node is shared with an ancestor
1123 * @dma_coherent: this particular device is dma coherent, even if the
1124 * architecture supports non-coherent devices.
1126 * At the lowest level, every device in a Linux system is represented by an
1127 * instance of struct device. The device structure contains the information
1128 * that the device model core needs to model the system. Most subsystems,
1129 * however, track additional information about the devices they host. As a
1130 * result, it is rare for devices to be represented by bare device structures;
1131 * instead, that structure, like kobject structures, is usually embedded within
1132 * a higher-level representation of the device.
1135 struct kobject kobj;
1136 struct device *parent;
1138 struct device_private *p;
1140 const char *init_name; /* initial name of the device */
1141 const struct device_type *type;
1143 struct bus_type *bus; /* type of bus device is on */
1144 struct device_driver *driver; /* which driver has allocated this
1146 void *platform_data; /* Platform specific data, device
1147 core doesn't touch it */
1148 void *driver_data; /* Driver data, set and get with
1149 dev_set_drvdata/dev_get_drvdata */
1150 struct mutex mutex; /* mutex to synchronize calls to
1154 struct dev_links_info links;
1155 struct dev_pm_info power;
1156 struct dev_pm_domain *pm_domain;
1158 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
1159 struct irq_domain *msi_domain;
1161 #ifdef CONFIG_PINCTRL
1162 struct dev_pin_info *pins;
1164 #ifdef CONFIG_GENERIC_MSI_IRQ
1165 struct list_head msi_list;
1168 const struct dma_map_ops *dma_ops;
1169 u64 *dma_mask; /* dma mask (if dma'able device) */
1170 u64 coherent_dma_mask;/* Like dma_mask, but for
1171 alloc_coherent mappings as
1172 not all hardware supports
1173 64 bit addresses for consistent
1174 allocations such descriptors. */
1175 u64 bus_dma_mask; /* upstream dma_mask constraint */
1176 unsigned long dma_pfn_offset;
1178 struct device_dma_parameters *dma_parms;
1180 struct list_head dma_pools; /* dma pools (if dma'ble) */
1182 #ifdef CONFIG_DMA_DECLARE_COHERENT
1183 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
1186 #ifdef CONFIG_DMA_CMA
1187 struct cma *cma_area; /* contiguous memory area for dma
1190 /* arch specific additions */
1191 struct dev_archdata archdata;
1193 struct device_node *of_node; /* associated device tree node */
1194 struct fwnode_handle *fwnode; /* firmware device node */
1197 int numa_node; /* NUMA node this device is close to */
1199 dev_t devt; /* dev_t, creates the sysfs "dev" */
1200 u32 id; /* device instance */
1202 spinlock_t devres_lock;
1203 struct list_head devres_head;
1205 struct class *class;
1206 const struct attribute_group **groups; /* optional groups */
1208 void (*release)(struct device *dev);
1209 struct iommu_group *iommu_group;
1210 struct iommu_fwspec *iommu_fwspec;
1211 struct iommu_param *iommu_param;
1213 bool offline_disabled:1;
1215 bool of_node_reused:1;
1216 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
1217 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
1218 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
1219 bool dma_coherent:1;
1223 static inline struct device *kobj_to_dev(struct kobject *kobj)
1225 return container_of(kobj, struct device, kobj);
1229 * device_iommu_mapped - Returns true when the device DMA is translated
1231 * @dev: Device to perform the check on
1233 static inline bool device_iommu_mapped(struct device *dev)
1235 return (dev->iommu_group != NULL);
1238 /* Get the wakeup routines, which depend on struct device */
1239 #include <linux/pm_wakeup.h>
1241 static inline const char *dev_name(const struct device *dev)
1243 /* Use the init name until the kobject becomes available */
1245 return dev->init_name;
1247 return kobject_name(&dev->kobj);
1250 extern __printf(2, 3)
1251 int dev_set_name(struct device *dev, const char *name, ...);
1254 static inline int dev_to_node(struct device *dev)
1256 return dev->numa_node;
1258 static inline void set_dev_node(struct device *dev, int node)
1260 dev->numa_node = node;
1263 static inline int dev_to_node(struct device *dev)
1265 return NUMA_NO_NODE;
1267 static inline void set_dev_node(struct device *dev, int node)
1272 static inline struct irq_domain *dev_get_msi_domain(const struct device *dev)
1274 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
1275 return dev->msi_domain;
1281 static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d)
1283 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
1284 dev->msi_domain = d;
1288 static inline void *dev_get_drvdata(const struct device *dev)
1290 return dev->driver_data;
1293 static inline void dev_set_drvdata(struct device *dev, void *data)
1295 dev->driver_data = data;
1298 static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
1300 return dev ? dev->power.subsys_data : NULL;
1303 static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
1305 return dev->kobj.uevent_suppress;
1308 static inline void dev_set_uevent_suppress(struct device *dev, int val)
1310 dev->kobj.uevent_suppress = val;
1313 static inline int device_is_registered(struct device *dev)
1315 return dev->kobj.state_in_sysfs;
1318 static inline void device_enable_async_suspend(struct device *dev)
1320 if (!dev->power.is_prepared)
1321 dev->power.async_suspend = true;
1324 static inline void device_disable_async_suspend(struct device *dev)
1326 if (!dev->power.is_prepared)
1327 dev->power.async_suspend = false;
1330 static inline bool device_async_suspend_enabled(struct device *dev)
1332 return !!dev->power.async_suspend;
1335 static inline bool device_pm_not_required(struct device *dev)
1337 return dev->power.no_pm;
1340 static inline void device_set_pm_not_required(struct device *dev)
1342 dev->power.no_pm = true;
1345 static inline void dev_pm_syscore_device(struct device *dev, bool val)
1347 #ifdef CONFIG_PM_SLEEP
1348 dev->power.syscore = val;
1352 static inline void dev_pm_set_driver_flags(struct device *dev, u32 flags)
1354 dev->power.driver_flags = flags;
1357 static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags)
1359 return !!(dev->power.driver_flags & flags);
1362 static inline void device_lock(struct device *dev)
1364 mutex_lock(&dev->mutex);
1367 static inline int device_lock_interruptible(struct device *dev)
1369 return mutex_lock_interruptible(&dev->mutex);
1372 static inline int device_trylock(struct device *dev)
1374 return mutex_trylock(&dev->mutex);
1377 static inline void device_unlock(struct device *dev)
1379 mutex_unlock(&dev->mutex);
1382 static inline void device_lock_assert(struct device *dev)
1384 lockdep_assert_held(&dev->mutex);
1387 static inline struct device_node *dev_of_node(struct device *dev)
1389 if (!IS_ENABLED(CONFIG_OF) || !dev)
1391 return dev->of_node;
1394 void driver_init(void);
1397 * High level routines for use by the bus drivers
1399 extern int __must_check device_register(struct device *dev);
1400 extern void device_unregister(struct device *dev);
1401 extern void device_initialize(struct device *dev);
1402 extern int __must_check device_add(struct device *dev);
1403 extern void device_del(struct device *dev);
1404 extern int device_for_each_child(struct device *dev, void *data,
1405 int (*fn)(struct device *dev, void *data));
1406 extern int device_for_each_child_reverse(struct device *dev, void *data,
1407 int (*fn)(struct device *dev, void *data));
1408 extern struct device *device_find_child(struct device *dev, void *data,
1409 int (*match)(struct device *dev, void *data));
1410 extern struct device *device_find_child_by_name(struct device *parent,
1412 extern int device_rename(struct device *dev, const char *new_name);
1413 extern int device_move(struct device *dev, struct device *new_parent,
1414 enum dpm_order dpm_order);
1415 extern const char *device_get_devnode(struct device *dev,
1416 umode_t *mode, kuid_t *uid, kgid_t *gid,
1419 static inline bool device_supports_offline(struct device *dev)
1421 return dev->bus && dev->bus->offline && dev->bus->online;
1424 extern void lock_device_hotplug(void);
1425 extern void unlock_device_hotplug(void);
1426 extern int lock_device_hotplug_sysfs(void);
1427 extern int device_offline(struct device *dev);
1428 extern int device_online(struct device *dev);
1429 extern void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1430 extern void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1431 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);
1433 static inline int dev_num_vf(struct device *dev)
1435 if (dev->bus && dev->bus->num_vf)
1436 return dev->bus->num_vf(dev);
1441 * Root device objects for grouping under /sys/devices
1443 extern struct device *__root_device_register(const char *name,
1444 struct module *owner);
1446 /* This is a macro to avoid include problems with THIS_MODULE */
1447 #define root_device_register(name) \
1448 __root_device_register(name, THIS_MODULE)
1450 extern void root_device_unregister(struct device *root);
1452 static inline void *dev_get_platdata(const struct device *dev)
1454 return dev->platform_data;
1458 * Manual binding of a device to driver. See drivers/base/bus.c
1459 * for information on use.
1461 extern int __must_check device_bind_driver(struct device *dev);
1462 extern void device_release_driver(struct device *dev);
1463 extern int __must_check device_attach(struct device *dev);
1464 extern int __must_check driver_attach(struct device_driver *drv);
1465 extern void device_initial_probe(struct device *dev);
1466 extern int __must_check device_reprobe(struct device *dev);
1468 extern bool device_is_bound(struct device *dev);
1471 * Easy functions for dynamically creating devices on the fly
1473 extern __printf(5, 0)
1474 struct device *device_create_vargs(struct class *cls, struct device *parent,
1475 dev_t devt, void *drvdata,
1476 const char *fmt, va_list vargs);
1477 extern __printf(5, 6)
1478 struct device *device_create(struct class *cls, struct device *parent,
1479 dev_t devt, void *drvdata,
1480 const char *fmt, ...);
1481 extern __printf(6, 7)
1482 struct device *device_create_with_groups(struct class *cls,
1483 struct device *parent, dev_t devt, void *drvdata,
1484 const struct attribute_group **groups,
1485 const char *fmt, ...);
1486 extern void device_destroy(struct class *cls, dev_t devt);
1488 extern int __must_check device_add_groups(struct device *dev,
1489 const struct attribute_group **groups);
1490 extern void device_remove_groups(struct device *dev,
1491 const struct attribute_group **groups);
1493 static inline int __must_check device_add_group(struct device *dev,
1494 const struct attribute_group *grp)
1496 const struct attribute_group *groups[] = { grp, NULL };
1498 return device_add_groups(dev, groups);
1501 static inline void device_remove_group(struct device *dev,
1502 const struct attribute_group *grp)
1504 const struct attribute_group *groups[] = { grp, NULL };
1506 return device_remove_groups(dev, groups);
1509 extern int __must_check devm_device_add_groups(struct device *dev,
1510 const struct attribute_group **groups);
1511 extern void devm_device_remove_groups(struct device *dev,
1512 const struct attribute_group **groups);
1513 extern int __must_check devm_device_add_group(struct device *dev,
1514 const struct attribute_group *grp);
1515 extern void devm_device_remove_group(struct device *dev,
1516 const struct attribute_group *grp);
1519 * Platform "fixup" functions - allow the platform to have their say
1520 * about devices and actions that the general device layer doesn't
1523 /* Notify platform of device discovery */
1524 extern int (*platform_notify)(struct device *dev);
1526 extern int (*platform_notify_remove)(struct device *dev);
1530 * get_device - atomically increment the reference count for the device.
1533 extern struct device *get_device(struct device *dev);
1534 extern void put_device(struct device *dev);
1536 #ifdef CONFIG_DEVTMPFS
1537 extern int devtmpfs_create_node(struct device *dev);
1538 extern int devtmpfs_delete_node(struct device *dev);
1539 extern int devtmpfs_mount(const char *mntdir);
1541 static inline int devtmpfs_create_node(struct device *dev) { return 0; }
1542 static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
1543 static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
1546 /* drivers/base/power/shutdown.c */
1547 extern void device_shutdown(void);
1549 /* debugging and troubleshooting/diagnostic helpers. */
1550 extern const char *dev_driver_string(const struct device *dev);
1552 /* Device links interface. */
1553 struct device_link *device_link_add(struct device *consumer,
1554 struct device *supplier, u32 flags);
1555 void device_link_del(struct device_link *link);
1556 void device_link_remove(void *consumer, struct device *supplier);
1559 #define dev_fmt(fmt) fmt
1562 #ifdef CONFIG_PRINTK
1564 __printf(3, 0) __cold
1565 int dev_vprintk_emit(int level, const struct device *dev,
1566 const char *fmt, va_list args);
1567 __printf(3, 4) __cold
1568 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...);
1570 __printf(3, 4) __cold
1571 void dev_printk(const char *level, const struct device *dev,
1572 const char *fmt, ...);
1573 __printf(2, 3) __cold
1574 void _dev_emerg(const struct device *dev, const char *fmt, ...);
1575 __printf(2, 3) __cold
1576 void _dev_alert(const struct device *dev, const char *fmt, ...);
1577 __printf(2, 3) __cold
1578 void _dev_crit(const struct device *dev, const char *fmt, ...);
1579 __printf(2, 3) __cold
1580 void _dev_err(const struct device *dev, const char *fmt, ...);
1581 __printf(2, 3) __cold
1582 void _dev_warn(const struct device *dev, const char *fmt, ...);
1583 __printf(2, 3) __cold
1584 void _dev_notice(const struct device *dev, const char *fmt, ...);
1585 __printf(2, 3) __cold
1586 void _dev_info(const struct device *dev, const char *fmt, ...);
1590 static inline __printf(3, 0)
1591 int dev_vprintk_emit(int level, const struct device *dev,
1592 const char *fmt, va_list args)
1594 static inline __printf(3, 4)
1595 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
1598 static inline void __dev_printk(const char *level, const struct device *dev,
1599 struct va_format *vaf)
1601 static inline __printf(3, 4)
1602 void dev_printk(const char *level, const struct device *dev,
1603 const char *fmt, ...)
1606 static inline __printf(2, 3)
1607 void _dev_emerg(const struct device *dev, const char *fmt, ...)
1609 static inline __printf(2, 3)
1610 void _dev_crit(const struct device *dev, const char *fmt, ...)
1612 static inline __printf(2, 3)
1613 void _dev_alert(const struct device *dev, const char *fmt, ...)
1615 static inline __printf(2, 3)
1616 void _dev_err(const struct device *dev, const char *fmt, ...)
1618 static inline __printf(2, 3)
1619 void _dev_warn(const struct device *dev, const char *fmt, ...)
1621 static inline __printf(2, 3)
1622 void _dev_notice(const struct device *dev, const char *fmt, ...)
1624 static inline __printf(2, 3)
1625 void _dev_info(const struct device *dev, const char *fmt, ...)
1631 * #defines for all the dev_<level> macros to prefix with whatever
1632 * possible use of #define dev_fmt(fmt) ...
1635 #define dev_emerg(dev, fmt, ...) \
1636 _dev_emerg(dev, dev_fmt(fmt), ##__VA_ARGS__)
1637 #define dev_crit(dev, fmt, ...) \
1638 _dev_crit(dev, dev_fmt(fmt), ##__VA_ARGS__)
1639 #define dev_alert(dev, fmt, ...) \
1640 _dev_alert(dev, dev_fmt(fmt), ##__VA_ARGS__)
1641 #define dev_err(dev, fmt, ...) \
1642 _dev_err(dev, dev_fmt(fmt), ##__VA_ARGS__)
1643 #define dev_warn(dev, fmt, ...) \
1644 _dev_warn(dev, dev_fmt(fmt), ##__VA_ARGS__)
1645 #define dev_notice(dev, fmt, ...) \
1646 _dev_notice(dev, dev_fmt(fmt), ##__VA_ARGS__)
1647 #define dev_info(dev, fmt, ...) \
1648 _dev_info(dev, dev_fmt(fmt), ##__VA_ARGS__)
1650 #if defined(CONFIG_DYNAMIC_DEBUG)
1651 #define dev_dbg(dev, fmt, ...) \
1652 dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__)
1653 #elif defined(DEBUG)
1654 #define dev_dbg(dev, fmt, ...) \
1655 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
1657 #define dev_dbg(dev, fmt, ...) \
1660 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
1664 #ifdef CONFIG_PRINTK
1665 #define dev_level_once(dev_level, dev, fmt, ...) \
1667 static bool __print_once __read_mostly; \
1669 if (!__print_once) { \
1670 __print_once = true; \
1671 dev_level(dev, fmt, ##__VA_ARGS__); \
1675 #define dev_level_once(dev_level, dev, fmt, ...) \
1678 dev_level(dev, fmt, ##__VA_ARGS__); \
1682 #define dev_emerg_once(dev, fmt, ...) \
1683 dev_level_once(dev_emerg, dev, fmt, ##__VA_ARGS__)
1684 #define dev_alert_once(dev, fmt, ...) \
1685 dev_level_once(dev_alert, dev, fmt, ##__VA_ARGS__)
1686 #define dev_crit_once(dev, fmt, ...) \
1687 dev_level_once(dev_crit, dev, fmt, ##__VA_ARGS__)
1688 #define dev_err_once(dev, fmt, ...) \
1689 dev_level_once(dev_err, dev, fmt, ##__VA_ARGS__)
1690 #define dev_warn_once(dev, fmt, ...) \
1691 dev_level_once(dev_warn, dev, fmt, ##__VA_ARGS__)
1692 #define dev_notice_once(dev, fmt, ...) \
1693 dev_level_once(dev_notice, dev, fmt, ##__VA_ARGS__)
1694 #define dev_info_once(dev, fmt, ...) \
1695 dev_level_once(dev_info, dev, fmt, ##__VA_ARGS__)
1696 #define dev_dbg_once(dev, fmt, ...) \
1697 dev_level_once(dev_dbg, dev, fmt, ##__VA_ARGS__)
1699 #define dev_level_ratelimited(dev_level, dev, fmt, ...) \
1701 static DEFINE_RATELIMIT_STATE(_rs, \
1702 DEFAULT_RATELIMIT_INTERVAL, \
1703 DEFAULT_RATELIMIT_BURST); \
1704 if (__ratelimit(&_rs)) \
1705 dev_level(dev, fmt, ##__VA_ARGS__); \
1708 #define dev_emerg_ratelimited(dev, fmt, ...) \
1709 dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
1710 #define dev_alert_ratelimited(dev, fmt, ...) \
1711 dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
1712 #define dev_crit_ratelimited(dev, fmt, ...) \
1713 dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
1714 #define dev_err_ratelimited(dev, fmt, ...) \
1715 dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
1716 #define dev_warn_ratelimited(dev, fmt, ...) \
1717 dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
1718 #define dev_notice_ratelimited(dev, fmt, ...) \
1719 dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
1720 #define dev_info_ratelimited(dev, fmt, ...) \
1721 dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
1722 #if defined(CONFIG_DYNAMIC_DEBUG)
1723 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
1724 #define dev_dbg_ratelimited(dev, fmt, ...) \
1726 static DEFINE_RATELIMIT_STATE(_rs, \
1727 DEFAULT_RATELIMIT_INTERVAL, \
1728 DEFAULT_RATELIMIT_BURST); \
1729 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
1730 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
1731 __ratelimit(&_rs)) \
1732 __dynamic_dev_dbg(&descriptor, dev, dev_fmt(fmt), \
1735 #elif defined(DEBUG)
1736 #define dev_dbg_ratelimited(dev, fmt, ...) \
1738 static DEFINE_RATELIMIT_STATE(_rs, \
1739 DEFAULT_RATELIMIT_INTERVAL, \
1740 DEFAULT_RATELIMIT_BURST); \
1741 if (__ratelimit(&_rs)) \
1742 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
1745 #define dev_dbg_ratelimited(dev, fmt, ...) \
1748 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
1752 #ifdef VERBOSE_DEBUG
1753 #define dev_vdbg dev_dbg
1755 #define dev_vdbg(dev, fmt, ...) \
1758 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
1763 * dev_WARN*() acts like dev_printk(), but with the key difference of
1764 * using WARN/WARN_ONCE to include file/line information and a backtrace.
1766 #define dev_WARN(dev, format, arg...) \
1767 WARN(1, "%s %s: " format, dev_driver_string(dev), dev_name(dev), ## arg);
1769 #define dev_WARN_ONCE(dev, condition, format, arg...) \
1770 WARN_ONCE(condition, "%s %s: " format, \
1771 dev_driver_string(dev), dev_name(dev), ## arg)
1773 /* Create alias, so I can be autoloaded. */
1774 #define MODULE_ALIAS_CHARDEV(major,minor) \
1775 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1776 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1777 MODULE_ALIAS("char-major-" __stringify(major) "-*")
1779 #ifdef CONFIG_SYSFS_DEPRECATED
1780 extern long sysfs_deprecated;
1782 #define sysfs_deprecated 0
1786 * module_driver() - Helper macro for drivers that don't do anything
1787 * special in module init/exit. This eliminates a lot of boilerplate.
1788 * Each module may only use this macro once, and calling it replaces
1789 * module_init() and module_exit().
1791 * @__driver: driver name
1792 * @__register: register function for this driver type
1793 * @__unregister: unregister function for this driver type
1794 * @...: Additional arguments to be passed to __register and __unregister.
1796 * Use this macro to construct bus specific macros for registering
1797 * drivers, and do not use it on its own.
1799 #define module_driver(__driver, __register, __unregister, ...) \
1800 static int __init __driver##_init(void) \
1802 return __register(&(__driver) , ##__VA_ARGS__); \
1804 module_init(__driver##_init); \
1805 static void __exit __driver##_exit(void) \
1807 __unregister(&(__driver) , ##__VA_ARGS__); \
1809 module_exit(__driver##_exit);
1812 * builtin_driver() - Helper macro for drivers that don't do anything
1813 * special in init and have no exit. This eliminates some boilerplate.
1814 * Each driver may only use this macro once, and calling it replaces
1815 * device_initcall (or in some cases, the legacy __initcall). This is
1816 * meant to be a direct parallel of module_driver() above but without
1817 * the __exit stuff that is not used for builtin cases.
1819 * @__driver: driver name
1820 * @__register: register function for this driver type
1821 * @...: Additional arguments to be passed to __register
1823 * Use this macro to construct bus specific macros for registering
1824 * drivers, and do not use it on its own.
1826 #define builtin_driver(__driver, __register, ...) \
1827 static int __init __driver##_init(void) \
1829 return __register(&(__driver) , ##__VA_ARGS__); \
1831 device_initcall(__driver##_init);
1833 #endif /* _DEVICE_H_ */