Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * device.h - generic, centralized driver model
0004  *
0005  * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
0006  * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
0007  * Copyright (c) 2008-2009 Novell Inc.
0008  *
0009  * See Documentation/driver-api/driver-model/ for more information.
0010  */
0011 
0012 #ifndef _DEVICE_H_
0013 #define _DEVICE_H_
0014 
0015 #include <linux/dev_printk.h>
0016 #include <linux/energy_model.h>
0017 #include <linux/ioport.h>
0018 #include <linux/kobject.h>
0019 #include <linux/klist.h>
0020 #include <linux/list.h>
0021 #include <linux/lockdep.h>
0022 #include <linux/compiler.h>
0023 #include <linux/types.h>
0024 #include <linux/mutex.h>
0025 #include <linux/pm.h>
0026 #include <linux/atomic.h>
0027 #include <linux/uidgid.h>
0028 #include <linux/gfp.h>
0029 #include <linux/overflow.h>
0030 #include <linux/device/bus.h>
0031 #include <linux/device/class.h>
0032 #include <linux/device/driver.h>
0033 #include <asm/device.h>
0034 
0035 struct device;
0036 struct device_private;
0037 struct device_driver;
0038 struct driver_private;
0039 struct module;
0040 struct class;
0041 struct subsys_private;
0042 struct device_node;
0043 struct fwnode_handle;
0044 struct iommu_ops;
0045 struct iommu_group;
0046 struct dev_pin_info;
0047 struct dev_iommu;
0048 struct msi_device_data;
0049 
0050 /**
0051  * struct subsys_interface - interfaces to device functions
0052  * @name:       name of the device function
0053  * @subsys:     subsystem of the devices to attach to
0054  * @node:       the list of functions registered at the subsystem
0055  * @add_dev:    device hookup to device function handler
0056  * @remove_dev: device hookup to device function handler
0057  *
0058  * Simple interfaces attached to a subsystem. Multiple interfaces can
0059  * attach to a subsystem and its devices. Unlike drivers, they do not
0060  * exclusively claim or control devices. Interfaces usually represent
0061  * a specific functionality of a subsystem/class of devices.
0062  */
0063 struct subsys_interface {
0064     const char *name;
0065     struct bus_type *subsys;
0066     struct list_head node;
0067     int (*add_dev)(struct device *dev, struct subsys_interface *sif);
0068     void (*remove_dev)(struct device *dev, struct subsys_interface *sif);
0069 };
0070 
0071 int subsys_interface_register(struct subsys_interface *sif);
0072 void subsys_interface_unregister(struct subsys_interface *sif);
0073 
0074 int subsys_system_register(struct bus_type *subsys,
0075                const struct attribute_group **groups);
0076 int subsys_virtual_register(struct bus_type *subsys,
0077                 const struct attribute_group **groups);
0078 
0079 /*
0080  * The type of device, "struct device" is embedded in. A class
0081  * or bus can contain devices of different types
0082  * like "partitions" and "disks", "mouse" and "event".
0083  * This identifies the device type and carries type-specific
0084  * information, equivalent to the kobj_type of a kobject.
0085  * If "name" is specified, the uevent will contain it in
0086  * the DEVTYPE variable.
0087  */
0088 struct device_type {
0089     const char *name;
0090     const struct attribute_group **groups;
0091     int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
0092     char *(*devnode)(struct device *dev, umode_t *mode,
0093              kuid_t *uid, kgid_t *gid);
0094     void (*release)(struct device *dev);
0095 
0096     const struct dev_pm_ops *pm;
0097 };
0098 
0099 /* interface for exporting device attributes */
0100 struct device_attribute {
0101     struct attribute    attr;
0102     ssize_t (*show)(struct device *dev, struct device_attribute *attr,
0103             char *buf);
0104     ssize_t (*store)(struct device *dev, struct device_attribute *attr,
0105              const char *buf, size_t count);
0106 };
0107 
0108 struct dev_ext_attribute {
0109     struct device_attribute attr;
0110     void *var;
0111 };
0112 
0113 ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
0114               char *buf);
0115 ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
0116                const char *buf, size_t count);
0117 ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
0118             char *buf);
0119 ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
0120              const char *buf, size_t count);
0121 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
0122             char *buf);
0123 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
0124              const char *buf, size_t count);
0125 
0126 #define DEVICE_ATTR(_name, _mode, _show, _store) \
0127     struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
0128 #define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \
0129     struct device_attribute dev_attr_##_name = \
0130         __ATTR_PREALLOC(_name, _mode, _show, _store)
0131 #define DEVICE_ATTR_RW(_name) \
0132     struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
0133 #define DEVICE_ATTR_ADMIN_RW(_name) \
0134     struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)
0135 #define DEVICE_ATTR_RO(_name) \
0136     struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
0137 #define DEVICE_ATTR_ADMIN_RO(_name) \
0138     struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)
0139 #define DEVICE_ATTR_WO(_name) \
0140     struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
0141 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
0142     struct dev_ext_attribute dev_attr_##_name = \
0143         { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
0144 #define DEVICE_INT_ATTR(_name, _mode, _var) \
0145     struct dev_ext_attribute dev_attr_##_name = \
0146         { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
0147 #define DEVICE_BOOL_ATTR(_name, _mode, _var) \
0148     struct dev_ext_attribute dev_attr_##_name = \
0149         { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
0150 #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
0151     struct device_attribute dev_attr_##_name =      \
0152         __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
0153 
0154 int device_create_file(struct device *device,
0155                const struct device_attribute *entry);
0156 void device_remove_file(struct device *dev,
0157             const struct device_attribute *attr);
0158 bool device_remove_file_self(struct device *dev,
0159                  const struct device_attribute *attr);
0160 int __must_check device_create_bin_file(struct device *dev,
0161                     const struct bin_attribute *attr);
0162 void device_remove_bin_file(struct device *dev,
0163                 const struct bin_attribute *attr);
0164 
0165 /* device resource management */
0166 typedef void (*dr_release_t)(struct device *dev, void *res);
0167 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
0168 
0169 void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
0170               int nid, const char *name) __malloc;
0171 #define devres_alloc(release, size, gfp) \
0172     __devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)
0173 #define devres_alloc_node(release, size, gfp, nid) \
0174     __devres_alloc_node(release, size, gfp, nid, #release)
0175 
0176 void devres_for_each_res(struct device *dev, dr_release_t release,
0177              dr_match_t match, void *match_data,
0178              void (*fn)(struct device *, void *, void *),
0179              void *data);
0180 void devres_free(void *res);
0181 void devres_add(struct device *dev, void *res);
0182 void *devres_find(struct device *dev, dr_release_t release,
0183           dr_match_t match, void *match_data);
0184 void *devres_get(struct device *dev, void *new_res,
0185          dr_match_t match, void *match_data);
0186 void *devres_remove(struct device *dev, dr_release_t release,
0187             dr_match_t match, void *match_data);
0188 int devres_destroy(struct device *dev, dr_release_t release,
0189            dr_match_t match, void *match_data);
0190 int devres_release(struct device *dev, dr_release_t release,
0191            dr_match_t match, void *match_data);
0192 
0193 /* devres group */
0194 void * __must_check devres_open_group(struct device *dev, void *id, gfp_t gfp);
0195 void devres_close_group(struct device *dev, void *id);
0196 void devres_remove_group(struct device *dev, void *id);
0197 int devres_release_group(struct device *dev, void *id);
0198 
0199 /* managed devm_k.alloc/kfree for device drivers */
0200 void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __malloc;
0201 void *devm_krealloc(struct device *dev, void *ptr, size_t size,
0202             gfp_t gfp) __must_check;
0203 __printf(3, 0) char *devm_kvasprintf(struct device *dev, gfp_t gfp,
0204                      const char *fmt, va_list ap) __malloc;
0205 __printf(3, 4) char *devm_kasprintf(struct device *dev, gfp_t gfp,
0206                     const char *fmt, ...) __malloc;
0207 static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
0208 {
0209     return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
0210 }
0211 static inline void *devm_kmalloc_array(struct device *dev,
0212                        size_t n, size_t size, gfp_t flags)
0213 {
0214     size_t bytes;
0215 
0216     if (unlikely(check_mul_overflow(n, size, &bytes)))
0217         return NULL;
0218 
0219     return devm_kmalloc(dev, bytes, flags);
0220 }
0221 static inline void *devm_kcalloc(struct device *dev,
0222                  size_t n, size_t size, gfp_t flags)
0223 {
0224     return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
0225 }
0226 void devm_kfree(struct device *dev, const void *p);
0227 char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
0228 const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
0229 void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp);
0230 
0231 unsigned long devm_get_free_pages(struct device *dev,
0232                   gfp_t gfp_mask, unsigned int order);
0233 void devm_free_pages(struct device *dev, unsigned long addr);
0234 
0235 void __iomem *devm_ioremap_resource(struct device *dev,
0236                     const struct resource *res);
0237 void __iomem *devm_ioremap_resource_wc(struct device *dev,
0238                        const struct resource *res);
0239 
0240 void __iomem *devm_of_iomap(struct device *dev,
0241                 struct device_node *node, int index,
0242                 resource_size_t *size);
0243 
0244 /* allows to add/remove a custom action to devres stack */
0245 int devm_add_action(struct device *dev, void (*action)(void *), void *data);
0246 void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
0247 void devm_release_action(struct device *dev, void (*action)(void *), void *data);
0248 
0249 static inline int devm_add_action_or_reset(struct device *dev,
0250                        void (*action)(void *), void *data)
0251 {
0252     int ret;
0253 
0254     ret = devm_add_action(dev, action, data);
0255     if (ret)
0256         action(data);
0257 
0258     return ret;
0259 }
0260 
0261 /**
0262  * devm_alloc_percpu - Resource-managed alloc_percpu
0263  * @dev: Device to allocate per-cpu memory for
0264  * @type: Type to allocate per-cpu memory for
0265  *
0266  * Managed alloc_percpu. Per-cpu memory allocated with this function is
0267  * automatically freed on driver detach.
0268  *
0269  * RETURNS:
0270  * Pointer to allocated memory on success, NULL on failure.
0271  */
0272 #define devm_alloc_percpu(dev, type)      \
0273     ((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
0274                               __alignof__(type)))
0275 
0276 void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
0277                    size_t align);
0278 void devm_free_percpu(struct device *dev, void __percpu *pdata);
0279 
0280 struct device_dma_parameters {
0281     /*
0282      * a low level driver may set these to teach IOMMU code about
0283      * sg limitations.
0284      */
0285     unsigned int max_segment_size;
0286     unsigned int min_align_mask;
0287     unsigned long segment_boundary_mask;
0288 };
0289 
0290 /**
0291  * enum device_link_state - Device link states.
0292  * @DL_STATE_NONE: The presence of the drivers is not being tracked.
0293  * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
0294  * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.
0295  * @DL_STATE_CONSUMER_PROBE: The consumer is probing (supplier driver present).
0296  * @DL_STATE_ACTIVE: Both the supplier and consumer drivers are present.
0297  * @DL_STATE_SUPPLIER_UNBIND: The supplier driver is unbinding.
0298  */
0299 enum device_link_state {
0300     DL_STATE_NONE = -1,
0301     DL_STATE_DORMANT = 0,
0302     DL_STATE_AVAILABLE,
0303     DL_STATE_CONSUMER_PROBE,
0304     DL_STATE_ACTIVE,
0305     DL_STATE_SUPPLIER_UNBIND,
0306 };
0307 
0308 /*
0309  * Device link flags.
0310  *
0311  * STATELESS: The core will not remove this link automatically.
0312  * AUTOREMOVE_CONSUMER: Remove the link automatically on consumer driver unbind.
0313  * PM_RUNTIME: If set, the runtime PM framework will use this link.
0314  * RPM_ACTIVE: Run pm_runtime_get_sync() on the supplier during link creation.
0315  * AUTOREMOVE_SUPPLIER: Remove the link automatically on supplier driver unbind.
0316  * AUTOPROBE_CONSUMER: Probe consumer driver automatically after supplier binds.
0317  * MANAGED: The core tracks presence of supplier/consumer drivers (internal).
0318  * SYNC_STATE_ONLY: Link only affects sync_state() behavior.
0319  * INFERRED: Inferred from data (eg: firmware) and not from driver actions.
0320  */
0321 #define DL_FLAG_STATELESS       BIT(0)
0322 #define DL_FLAG_AUTOREMOVE_CONSUMER BIT(1)
0323 #define DL_FLAG_PM_RUNTIME      BIT(2)
0324 #define DL_FLAG_RPM_ACTIVE      BIT(3)
0325 #define DL_FLAG_AUTOREMOVE_SUPPLIER BIT(4)
0326 #define DL_FLAG_AUTOPROBE_CONSUMER  BIT(5)
0327 #define DL_FLAG_MANAGED         BIT(6)
0328 #define DL_FLAG_SYNC_STATE_ONLY     BIT(7)
0329 #define DL_FLAG_INFERRED        BIT(8)
0330 
0331 /**
0332  * enum dl_dev_state - Device driver presence tracking information.
0333  * @DL_DEV_NO_DRIVER: There is no driver attached to the device.
0334  * @DL_DEV_PROBING: A driver is probing.
0335  * @DL_DEV_DRIVER_BOUND: The driver has been bound to the device.
0336  * @DL_DEV_UNBINDING: The driver is unbinding from the device.
0337  */
0338 enum dl_dev_state {
0339     DL_DEV_NO_DRIVER = 0,
0340     DL_DEV_PROBING,
0341     DL_DEV_DRIVER_BOUND,
0342     DL_DEV_UNBINDING,
0343 };
0344 
0345 /**
0346  * enum device_removable - Whether the device is removable. The criteria for a
0347  * device to be classified as removable is determined by its subsystem or bus.
0348  * @DEVICE_REMOVABLE_NOT_SUPPORTED: This attribute is not supported for this
0349  *                  device (default).
0350  * @DEVICE_REMOVABLE_UNKNOWN:  Device location is Unknown.
0351  * @DEVICE_FIXED: Device is not removable by the user.
0352  * @DEVICE_REMOVABLE: Device is removable by the user.
0353  */
0354 enum device_removable {
0355     DEVICE_REMOVABLE_NOT_SUPPORTED = 0, /* must be 0 */
0356     DEVICE_REMOVABLE_UNKNOWN,
0357     DEVICE_FIXED,
0358     DEVICE_REMOVABLE,
0359 };
0360 
0361 /**
0362  * struct dev_links_info - Device data related to device links.
0363  * @suppliers: List of links to supplier devices.
0364  * @consumers: List of links to consumer devices.
0365  * @defer_sync: Hook to global list of devices that have deferred sync_state.
0366  * @status: Driver status information.
0367  */
0368 struct dev_links_info {
0369     struct list_head suppliers;
0370     struct list_head consumers;
0371     struct list_head defer_sync;
0372     enum dl_dev_state status;
0373 };
0374 
0375 /**
0376  * struct dev_msi_info - Device data related to MSI
0377  * @domain: The MSI interrupt domain associated to the device
0378  * @data:   Pointer to MSI device data
0379  */
0380 struct dev_msi_info {
0381 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
0382     struct irq_domain   *domain;
0383 #endif
0384 #ifdef CONFIG_GENERIC_MSI_IRQ
0385     struct msi_device_data  *data;
0386 #endif
0387 };
0388 
0389 /**
0390  * enum device_physical_location_panel - Describes which panel surface of the
0391  * system's housing the device connection point resides on.
0392  * @DEVICE_PANEL_TOP: Device connection point is on the top panel.
0393  * @DEVICE_PANEL_BOTTOM: Device connection point is on the bottom panel.
0394  * @DEVICE_PANEL_LEFT: Device connection point is on the left panel.
0395  * @DEVICE_PANEL_RIGHT: Device connection point is on the right panel.
0396  * @DEVICE_PANEL_FRONT: Device connection point is on the front panel.
0397  * @DEVICE_PANEL_BACK: Device connection point is on the back panel.
0398  * @DEVICE_PANEL_UNKNOWN: The panel with device connection point is unknown.
0399  */
0400 enum device_physical_location_panel {
0401     DEVICE_PANEL_TOP,
0402     DEVICE_PANEL_BOTTOM,
0403     DEVICE_PANEL_LEFT,
0404     DEVICE_PANEL_RIGHT,
0405     DEVICE_PANEL_FRONT,
0406     DEVICE_PANEL_BACK,
0407     DEVICE_PANEL_UNKNOWN,
0408 };
0409 
0410 /**
0411  * enum device_physical_location_vertical_position - Describes vertical
0412  * position of the device connection point on the panel surface.
0413  * @DEVICE_VERT_POS_UPPER: Device connection point is at upper part of panel.
0414  * @DEVICE_VERT_POS_CENTER: Device connection point is at center part of panel.
0415  * @DEVICE_VERT_POS_LOWER: Device connection point is at lower part of panel.
0416  */
0417 enum device_physical_location_vertical_position {
0418     DEVICE_VERT_POS_UPPER,
0419     DEVICE_VERT_POS_CENTER,
0420     DEVICE_VERT_POS_LOWER,
0421 };
0422 
0423 /**
0424  * enum device_physical_location_horizontal_position - Describes horizontal
0425  * position of the device connection point on the panel surface.
0426  * @DEVICE_HORI_POS_LEFT: Device connection point is at left part of panel.
0427  * @DEVICE_HORI_POS_CENTER: Device connection point is at center part of panel.
0428  * @DEVICE_HORI_POS_RIGHT: Device connection point is at right part of panel.
0429  */
0430 enum device_physical_location_horizontal_position {
0431     DEVICE_HORI_POS_LEFT,
0432     DEVICE_HORI_POS_CENTER,
0433     DEVICE_HORI_POS_RIGHT,
0434 };
0435 
0436 /**
0437  * struct device_physical_location - Device data related to physical location
0438  * of the device connection point.
0439  * @panel: Panel surface of the system's housing that the device connection
0440  *         point resides on.
0441  * @vertical_position: Vertical position of the device connection point within
0442  *                     the panel.
0443  * @horizontal_position: Horizontal position of the device connection point
0444  *                       within the panel.
0445  * @dock: Set if the device connection point resides in a docking station or
0446  *        port replicator.
0447  * @lid: Set if this device connection point resides on the lid of laptop
0448  *       system.
0449  */
0450 struct device_physical_location {
0451     enum device_physical_location_panel panel;
0452     enum device_physical_location_vertical_position vertical_position;
0453     enum device_physical_location_horizontal_position horizontal_position;
0454     bool dock;
0455     bool lid;
0456 };
0457 
0458 /**
0459  * struct device - The basic device structure
0460  * @parent: The device's "parent" device, the device to which it is attached.
0461  *      In most cases, a parent device is some sort of bus or host
0462  *      controller. If parent is NULL, the device, is a top-level device,
0463  *      which is not usually what you want.
0464  * @p:      Holds the private data of the driver core portions of the device.
0465  *      See the comment of the struct device_private for detail.
0466  * @kobj:   A top-level, abstract class from which other classes are derived.
0467  * @init_name:  Initial name of the device.
0468  * @type:   The type of device.
0469  *      This identifies the device type and carries type-specific
0470  *      information.
0471  * @mutex:  Mutex to synchronize calls to its driver.
0472  * @bus:    Type of bus device is on.
0473  * @driver: Which driver has allocated this
0474  * @platform_data: Platform data specific to the device.
0475  *      Example: For devices on custom boards, as typical of embedded
0476  *      and SOC based hardware, Linux often uses platform_data to point
0477  *      to board-specific structures describing devices and how they
0478  *      are wired.  That can include what ports are available, chip
0479  *      variants, which GPIO pins act in what additional roles, and so
0480  *      on.  This shrinks the "Board Support Packages" (BSPs) and
0481  *      minimizes board-specific #ifdefs in drivers.
0482  * @driver_data: Private pointer for driver specific info.
0483  * @links:  Links to suppliers and consumers of this device.
0484  * @power:  For device power management.
0485  *      See Documentation/driver-api/pm/devices.rst for details.
0486  * @pm_domain:  Provide callbacks that are executed during system suspend,
0487  *      hibernation, system resume and during runtime PM transitions
0488  *      along with subsystem-level and driver-level callbacks.
0489  * @em_pd:  device's energy model performance domain
0490  * @pins:   For device pin management.
0491  *      See Documentation/driver-api/pin-control.rst for details.
0492  * @msi:    MSI related data
0493  * @numa_node:  NUMA node this device is close to.
0494  * @dma_ops:    DMA mapping operations for this device.
0495  * @dma_mask:   Dma mask (if dma'ble device).
0496  * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
0497  *      hardware supports 64-bit addresses for consistent allocations
0498  *      such descriptors.
0499  * @bus_dma_limit: Limit of an upstream bridge or bus which imposes a smaller
0500  *      DMA limit than the device itself supports.
0501  * @dma_range_map: map for DMA memory ranges relative to that of RAM
0502  * @dma_parms:  A low level driver may set these to teach IOMMU code about
0503  *      segment limitations.
0504  * @dma_pools:  Dma pools (if dma'ble device).
0505  * @dma_mem:    Internal for coherent mem override.
0506  * @cma_area:   Contiguous memory area for dma allocations
0507  * @dma_io_tlb_mem: Pointer to the swiotlb pool used.  Not for driver use.
0508  * @archdata:   For arch-specific additions.
0509  * @of_node:    Associated device tree node.
0510  * @fwnode: Associated device node supplied by platform firmware.
0511  * @devt:   For creating the sysfs "dev".
0512  * @id:     device instance
0513  * @devres_lock: Spinlock to protect the resource of the device.
0514  * @devres_head: The resources list of the device.
0515  * @knode_class: The node used to add the device to the class list.
0516  * @class:  The class of the device.
0517  * @groups: Optional attribute groups.
0518  * @release:    Callback to free the device after all references have
0519  *      gone away. This should be set by the allocator of the
0520  *      device (i.e. the bus driver that discovered the device).
0521  * @iommu_group: IOMMU group the device belongs to.
0522  * @iommu:  Per device generic IOMMU runtime data
0523  * @physical_location: Describes physical location of the device connection
0524  *      point in the system housing.
0525  * @removable:  Whether the device can be removed from the system. This
0526  *              should be set by the subsystem / bus driver that discovered
0527  *              the device.
0528  *
0529  * @offline_disabled: If set, the device is permanently online.
0530  * @offline:    Set after successful invocation of bus type's .offline().
0531  * @of_node_reused: Set if the device-tree node is shared with an ancestor
0532  *              device.
0533  * @state_synced: The hardware state of this device has been synced to match
0534  *        the software state of this device by calling the driver/bus
0535  *        sync_state() callback.
0536  * @can_match:  The device has matched with a driver at least once or it is in
0537  *      a bus (like AMBA) which can't check for matching drivers until
0538  *      other devices probe successfully.
0539  * @dma_coherent: this particular device is dma coherent, even if the
0540  *      architecture supports non-coherent devices.
0541  * @dma_ops_bypass: If set to %true then the dma_ops are bypassed for the
0542  *      streaming DMA operations (->map_* / ->unmap_* / ->sync_*),
0543  *      and optionall (if the coherent mask is large enough) also
0544  *      for dma allocations.  This flag is managed by the dma ops
0545  *      instance from ->dma_supported.
0546  *
0547  * At the lowest level, every device in a Linux system is represented by an
0548  * instance of struct device. The device structure contains the information
0549  * that the device model core needs to model the system. Most subsystems,
0550  * however, track additional information about the devices they host. As a
0551  * result, it is rare for devices to be represented by bare device structures;
0552  * instead, that structure, like kobject structures, is usually embedded within
0553  * a higher-level representation of the device.
0554  */
0555 struct device {
0556     struct kobject kobj;
0557     struct device       *parent;
0558 
0559     struct device_private   *p;
0560 
0561     const char      *init_name; /* initial name of the device */
0562     const struct device_type *type;
0563 
0564     struct bus_type *bus;       /* type of bus device is on */
0565     struct device_driver *driver;   /* which driver has allocated this
0566                        device */
0567     void        *platform_data; /* Platform specific data, device
0568                        core doesn't touch it */
0569     void        *driver_data;   /* Driver data, set and get with
0570                        dev_set_drvdata/dev_get_drvdata */
0571     struct mutex        mutex;  /* mutex to synchronize calls to
0572                      * its driver.
0573                      */
0574 
0575     struct dev_links_info   links;
0576     struct dev_pm_info  power;
0577     struct dev_pm_domain    *pm_domain;
0578 
0579 #ifdef CONFIG_ENERGY_MODEL
0580     struct em_perf_domain   *em_pd;
0581 #endif
0582 
0583 #ifdef CONFIG_PINCTRL
0584     struct dev_pin_info *pins;
0585 #endif
0586     struct dev_msi_info msi;
0587 #ifdef CONFIG_DMA_OPS
0588     const struct dma_map_ops *dma_ops;
0589 #endif
0590     u64     *dma_mask;  /* dma mask (if dma'able device) */
0591     u64     coherent_dma_mask;/* Like dma_mask, but for
0592                          alloc_coherent mappings as
0593                          not all hardware supports
0594                          64 bit addresses for consistent
0595                          allocations such descriptors. */
0596     u64     bus_dma_limit;  /* upstream dma constraint */
0597     const struct bus_dma_region *dma_range_map;
0598 
0599     struct device_dma_parameters *dma_parms;
0600 
0601     struct list_head    dma_pools;  /* dma pools (if dma'ble) */
0602 
0603 #ifdef CONFIG_DMA_DECLARE_COHERENT
0604     struct dma_coherent_mem *dma_mem; /* internal for coherent mem
0605                          override */
0606 #endif
0607 #ifdef CONFIG_DMA_CMA
0608     struct cma *cma_area;       /* contiguous memory area for dma
0609                        allocations */
0610 #endif
0611 #ifdef CONFIG_SWIOTLB
0612     struct io_tlb_mem *dma_io_tlb_mem;
0613 #endif
0614     /* arch specific additions */
0615     struct dev_archdata archdata;
0616 
0617     struct device_node  *of_node; /* associated device tree node */
0618     struct fwnode_handle    *fwnode; /* firmware device node */
0619 
0620 #ifdef CONFIG_NUMA
0621     int     numa_node;  /* NUMA node this device is close to */
0622 #endif
0623     dev_t           devt;   /* dev_t, creates the sysfs "dev" */
0624     u32         id; /* device instance */
0625 
0626     spinlock_t      devres_lock;
0627     struct list_head    devres_head;
0628 
0629     struct class        *class;
0630     const struct attribute_group **groups;  /* optional groups */
0631 
0632     void    (*release)(struct device *dev);
0633     struct iommu_group  *iommu_group;
0634     struct dev_iommu    *iommu;
0635 
0636     struct device_physical_location *physical_location;
0637 
0638     enum device_removable   removable;
0639 
0640     bool            offline_disabled:1;
0641     bool            offline:1;
0642     bool            of_node_reused:1;
0643     bool            state_synced:1;
0644     bool            can_match:1;
0645 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
0646     defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
0647     defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
0648     bool            dma_coherent:1;
0649 #endif
0650 #ifdef CONFIG_DMA_OPS_BYPASS
0651     bool            dma_ops_bypass : 1;
0652 #endif
0653 };
0654 
0655 /**
0656  * struct device_link - Device link representation.
0657  * @supplier: The device on the supplier end of the link.
0658  * @s_node: Hook to the supplier device's list of links to consumers.
0659  * @consumer: The device on the consumer end of the link.
0660  * @c_node: Hook to the consumer device's list of links to suppliers.
0661  * @link_dev: device used to expose link details in sysfs
0662  * @status: The state of the link (with respect to the presence of drivers).
0663  * @flags: Link flags.
0664  * @rpm_active: Whether or not the consumer device is runtime-PM-active.
0665  * @kref: Count repeated addition of the same link.
0666  * @rm_work: Work structure used for removing the link.
0667  * @supplier_preactivated: Supplier has been made active before consumer probe.
0668  */
0669 struct device_link {
0670     struct device *supplier;
0671     struct list_head s_node;
0672     struct device *consumer;
0673     struct list_head c_node;
0674     struct device link_dev;
0675     enum device_link_state status;
0676     u32 flags;
0677     refcount_t rpm_active;
0678     struct kref kref;
0679     struct work_struct rm_work;
0680     bool supplier_preactivated; /* Owned by consumer probe. */
0681 };
0682 
0683 static inline struct device *kobj_to_dev(struct kobject *kobj)
0684 {
0685     return container_of(kobj, struct device, kobj);
0686 }
0687 
0688 /**
0689  * device_iommu_mapped - Returns true when the device DMA is translated
0690  *           by an IOMMU
0691  * @dev: Device to perform the check on
0692  */
0693 static inline bool device_iommu_mapped(struct device *dev)
0694 {
0695     return (dev->iommu_group != NULL);
0696 }
0697 
0698 /* Get the wakeup routines, which depend on struct device */
0699 #include <linux/pm_wakeup.h>
0700 
0701 static inline const char *dev_name(const struct device *dev)
0702 {
0703     /* Use the init name until the kobject becomes available */
0704     if (dev->init_name)
0705         return dev->init_name;
0706 
0707     return kobject_name(&dev->kobj);
0708 }
0709 
0710 /**
0711  * dev_bus_name - Return a device's bus/class name, if at all possible
0712  * @dev: struct device to get the bus/class name of
0713  *
0714  * Will return the name of the bus/class the device is attached to.  If it is
0715  * not attached to a bus/class, an empty string will be returned.
0716  */
0717 static inline const char *dev_bus_name(const struct device *dev)
0718 {
0719     return dev->bus ? dev->bus->name : (dev->class ? dev->class->name : "");
0720 }
0721 
0722 __printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...);
0723 
0724 #ifdef CONFIG_NUMA
0725 static inline int dev_to_node(struct device *dev)
0726 {
0727     return dev->numa_node;
0728 }
0729 static inline void set_dev_node(struct device *dev, int node)
0730 {
0731     dev->numa_node = node;
0732 }
0733 #else
0734 static inline int dev_to_node(struct device *dev)
0735 {
0736     return NUMA_NO_NODE;
0737 }
0738 static inline void set_dev_node(struct device *dev, int node)
0739 {
0740 }
0741 #endif
0742 
0743 static inline struct irq_domain *dev_get_msi_domain(const struct device *dev)
0744 {
0745 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
0746     return dev->msi.domain;
0747 #else
0748     return NULL;
0749 #endif
0750 }
0751 
0752 static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d)
0753 {
0754 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
0755     dev->msi.domain = d;
0756 #endif
0757 }
0758 
0759 static inline void *dev_get_drvdata(const struct device *dev)
0760 {
0761     return dev->driver_data;
0762 }
0763 
0764 static inline void dev_set_drvdata(struct device *dev, void *data)
0765 {
0766     dev->driver_data = data;
0767 }
0768 
0769 static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
0770 {
0771     return dev ? dev->power.subsys_data : NULL;
0772 }
0773 
0774 static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
0775 {
0776     return dev->kobj.uevent_suppress;
0777 }
0778 
0779 static inline void dev_set_uevent_suppress(struct device *dev, int val)
0780 {
0781     dev->kobj.uevent_suppress = val;
0782 }
0783 
0784 static inline int device_is_registered(struct device *dev)
0785 {
0786     return dev->kobj.state_in_sysfs;
0787 }
0788 
0789 static inline void device_enable_async_suspend(struct device *dev)
0790 {
0791     if (!dev->power.is_prepared)
0792         dev->power.async_suspend = true;
0793 }
0794 
0795 static inline void device_disable_async_suspend(struct device *dev)
0796 {
0797     if (!dev->power.is_prepared)
0798         dev->power.async_suspend = false;
0799 }
0800 
0801 static inline bool device_async_suspend_enabled(struct device *dev)
0802 {
0803     return !!dev->power.async_suspend;
0804 }
0805 
0806 static inline bool device_pm_not_required(struct device *dev)
0807 {
0808     return dev->power.no_pm;
0809 }
0810 
0811 static inline void device_set_pm_not_required(struct device *dev)
0812 {
0813     dev->power.no_pm = true;
0814 }
0815 
0816 static inline void dev_pm_syscore_device(struct device *dev, bool val)
0817 {
0818 #ifdef CONFIG_PM_SLEEP
0819     dev->power.syscore = val;
0820 #endif
0821 }
0822 
0823 static inline void dev_pm_set_driver_flags(struct device *dev, u32 flags)
0824 {
0825     dev->power.driver_flags = flags;
0826 }
0827 
0828 static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags)
0829 {
0830     return !!(dev->power.driver_flags & flags);
0831 }
0832 
0833 static inline void device_lock(struct device *dev)
0834 {
0835     mutex_lock(&dev->mutex);
0836 }
0837 
0838 static inline int device_lock_interruptible(struct device *dev)
0839 {
0840     return mutex_lock_interruptible(&dev->mutex);
0841 }
0842 
0843 static inline int device_trylock(struct device *dev)
0844 {
0845     return mutex_trylock(&dev->mutex);
0846 }
0847 
0848 static inline void device_unlock(struct device *dev)
0849 {
0850     mutex_unlock(&dev->mutex);
0851 }
0852 
0853 static inline void device_lock_assert(struct device *dev)
0854 {
0855     lockdep_assert_held(&dev->mutex);
0856 }
0857 
0858 static inline struct device_node *dev_of_node(struct device *dev)
0859 {
0860     if (!IS_ENABLED(CONFIG_OF) || !dev)
0861         return NULL;
0862     return dev->of_node;
0863 }
0864 
0865 static inline bool dev_has_sync_state(struct device *dev)
0866 {
0867     if (!dev)
0868         return false;
0869     if (dev->driver && dev->driver->sync_state)
0870         return true;
0871     if (dev->bus && dev->bus->sync_state)
0872         return true;
0873     return false;
0874 }
0875 
0876 static inline void dev_set_removable(struct device *dev,
0877                      enum device_removable removable)
0878 {
0879     dev->removable = removable;
0880 }
0881 
0882 static inline bool dev_is_removable(struct device *dev)
0883 {
0884     return dev->removable == DEVICE_REMOVABLE;
0885 }
0886 
0887 static inline bool dev_removable_is_valid(struct device *dev)
0888 {
0889     return dev->removable != DEVICE_REMOVABLE_NOT_SUPPORTED;
0890 }
0891 
0892 /*
0893  * High level routines for use by the bus drivers
0894  */
0895 int __must_check device_register(struct device *dev);
0896 void device_unregister(struct device *dev);
0897 void device_initialize(struct device *dev);
0898 int __must_check device_add(struct device *dev);
0899 void device_del(struct device *dev);
0900 int device_for_each_child(struct device *dev, void *data,
0901               int (*fn)(struct device *dev, void *data));
0902 int device_for_each_child_reverse(struct device *dev, void *data,
0903                   int (*fn)(struct device *dev, void *data));
0904 struct device *device_find_child(struct device *dev, void *data,
0905                  int (*match)(struct device *dev, void *data));
0906 struct device *device_find_child_by_name(struct device *parent,
0907                      const char *name);
0908 struct device *device_find_any_child(struct device *parent);
0909 
0910 int device_rename(struct device *dev, const char *new_name);
0911 int device_move(struct device *dev, struct device *new_parent,
0912         enum dpm_order dpm_order);
0913 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid);
0914 const char *device_get_devnode(struct device *dev, umode_t *mode, kuid_t *uid,
0915                    kgid_t *gid, const char **tmp);
0916 int device_is_dependent(struct device *dev, void *target);
0917 
0918 static inline bool device_supports_offline(struct device *dev)
0919 {
0920     return dev->bus && dev->bus->offline && dev->bus->online;
0921 }
0922 
0923 #define __device_lock_set_class(dev, name, key)                        \
0924 do {                                                                   \
0925     struct device *__d2 __maybe_unused = dev;                      \
0926     lock_set_class(&__d2->mutex.dep_map, name, key, 0, _THIS_IP_); \
0927 } while (0)
0928 
0929 /**
0930  * device_lock_set_class - Specify a temporary lock class while a device
0931  *             is attached to a driver
0932  * @dev: device to modify
0933  * @key: lock class key data
0934  *
0935  * This must be called with the device_lock() already held, for example
0936  * from driver ->probe(). Take care to only override the default
0937  * lockdep_no_validate class.
0938  */
0939 #ifdef CONFIG_LOCKDEP
0940 #define device_lock_set_class(dev, key)                                    \
0941 do {                                                                       \
0942     struct device *__d = dev;                                          \
0943     dev_WARN_ONCE(__d, !lockdep_match_class(&__d->mutex,               \
0944                         &__lockdep_no_validate__), \
0945          "overriding existing custom lock class\n");               \
0946     __device_lock_set_class(__d, #key, key);                           \
0947 } while (0)
0948 #else
0949 #define device_lock_set_class(dev, key) __device_lock_set_class(dev, #key, key)
0950 #endif
0951 
0952 /**
0953  * device_lock_reset_class - Return a device to the default lockdep novalidate state
0954  * @dev: device to modify
0955  *
0956  * This must be called with the device_lock() already held, for example
0957  * from driver ->remove().
0958  */
0959 #define device_lock_reset_class(dev) \
0960 do { \
0961     struct device *__d __maybe_unused = dev;                       \
0962     lock_set_novalidate_class(&__d->mutex.dep_map, "&dev->mutex",  \
0963                   _THIS_IP_);                          \
0964 } while (0)
0965 
0966 void lock_device_hotplug(void);
0967 void unlock_device_hotplug(void);
0968 int lock_device_hotplug_sysfs(void);
0969 int device_offline(struct device *dev);
0970 int device_online(struct device *dev);
0971 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
0972 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
0973 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);
0974 void device_set_node(struct device *dev, struct fwnode_handle *fwnode);
0975 
0976 static inline int dev_num_vf(struct device *dev)
0977 {
0978     if (dev->bus && dev->bus->num_vf)
0979         return dev->bus->num_vf(dev);
0980     return 0;
0981 }
0982 
0983 /*
0984  * Root device objects for grouping under /sys/devices
0985  */
0986 struct device *__root_device_register(const char *name, struct module *owner);
0987 
0988 /* This is a macro to avoid include problems with THIS_MODULE */
0989 #define root_device_register(name) \
0990     __root_device_register(name, THIS_MODULE)
0991 
0992 void root_device_unregister(struct device *root);
0993 
0994 static inline void *dev_get_platdata(const struct device *dev)
0995 {
0996     return dev->platform_data;
0997 }
0998 
0999 /*
1000  * Manual binding of a device to driver. See drivers/base/bus.c
1001  * for information on use.
1002  */
1003 int __must_check device_driver_attach(struct device_driver *drv,
1004                       struct device *dev);
1005 int __must_check device_bind_driver(struct device *dev);
1006 void device_release_driver(struct device *dev);
1007 int  __must_check device_attach(struct device *dev);
1008 int __must_check driver_attach(struct device_driver *drv);
1009 void device_initial_probe(struct device *dev);
1010 int __must_check device_reprobe(struct device *dev);
1011 
1012 bool device_is_bound(struct device *dev);
1013 
1014 /*
1015  * Easy functions for dynamically creating devices on the fly
1016  */
1017 __printf(5, 6) struct device *
1018 device_create(struct class *cls, struct device *parent, dev_t devt,
1019           void *drvdata, const char *fmt, ...);
1020 __printf(6, 7) struct device *
1021 device_create_with_groups(struct class *cls, struct device *parent, dev_t devt,
1022               void *drvdata, const struct attribute_group **groups,
1023               const char *fmt, ...);
1024 void device_destroy(struct class *cls, dev_t devt);
1025 
1026 int __must_check device_add_groups(struct device *dev,
1027                    const struct attribute_group **groups);
1028 void device_remove_groups(struct device *dev,
1029               const struct attribute_group **groups);
1030 
1031 static inline int __must_check device_add_group(struct device *dev,
1032                     const struct attribute_group *grp)
1033 {
1034     const struct attribute_group *groups[] = { grp, NULL };
1035 
1036     return device_add_groups(dev, groups);
1037 }
1038 
1039 static inline void device_remove_group(struct device *dev,
1040                        const struct attribute_group *grp)
1041 {
1042     const struct attribute_group *groups[] = { grp, NULL };
1043 
1044     return device_remove_groups(dev, groups);
1045 }
1046 
1047 int __must_check devm_device_add_groups(struct device *dev,
1048                     const struct attribute_group **groups);
1049 void devm_device_remove_groups(struct device *dev,
1050                    const struct attribute_group **groups);
1051 int __must_check devm_device_add_group(struct device *dev,
1052                        const struct attribute_group *grp);
1053 void devm_device_remove_group(struct device *dev,
1054                   const struct attribute_group *grp);
1055 
1056 /*
1057  * Platform "fixup" functions - allow the platform to have their say
1058  * about devices and actions that the general device layer doesn't
1059  * know about.
1060  */
1061 /* Notify platform of device discovery */
1062 extern int (*platform_notify)(struct device *dev);
1063 
1064 extern int (*platform_notify_remove)(struct device *dev);
1065 
1066 
1067 /*
1068  * get_device - atomically increment the reference count for the device.
1069  *
1070  */
1071 struct device *get_device(struct device *dev);
1072 void put_device(struct device *dev);
1073 bool kill_device(struct device *dev);
1074 
1075 #ifdef CONFIG_DEVTMPFS
1076 int devtmpfs_mount(void);
1077 #else
1078 static inline int devtmpfs_mount(void) { return 0; }
1079 #endif
1080 
1081 /* drivers/base/power/shutdown.c */
1082 void device_shutdown(void);
1083 
1084 /* debugging and troubleshooting/diagnostic helpers. */
1085 const char *dev_driver_string(const struct device *dev);
1086 
1087 /* Device links interface. */
1088 struct device_link *device_link_add(struct device *consumer,
1089                     struct device *supplier, u32 flags);
1090 void device_link_del(struct device_link *link);
1091 void device_link_remove(void *consumer, struct device *supplier);
1092 void device_links_supplier_sync_state_pause(void);
1093 void device_links_supplier_sync_state_resume(void);
1094 
1095 extern __printf(3, 4)
1096 int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
1097 
1098 /* Create alias, so I can be autoloaded. */
1099 #define MODULE_ALIAS_CHARDEV(major,minor) \
1100     MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1101 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1102     MODULE_ALIAS("char-major-" __stringify(major) "-*")
1103 
1104 #ifdef CONFIG_SYSFS_DEPRECATED
1105 extern long sysfs_deprecated;
1106 #else
1107 #define sysfs_deprecated 0
1108 #endif
1109 
1110 #endif /* _DEVICE_H_ */