函数dev_get_drvdata()

来源:互联网 发布:招聘网络女主播 编辑:程序博客网 时间:2024/06/05 16:18

函数dev_get_drvdata(), 是用来返回driver的私有数据的,其函数实现如下。

另外,与之相对应的保存driver的私有数据的函数是dev_set_drvdata()。

void *dev_get_drvdata(const struct device *dev){if (dev && dev->p)return dev->p->driver_data;return NULL;}


device结构体如下,从device->p的定义中看到,这个地址是用来保存driver的私有数据的

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * struct device - The basic device structure 
  3.  * @parent: The device's "parent" device, the device to which it is attached. 
  4.  *      In most cases, a parent device is some sort of bus or host 
  5.  *      controller. If parent is NULL, the device, is a top-level device, 
  6.  *      which is not usually what you want. 
  7. <span style="background-color: rgb(255, 255, 153);"> * @p:        Holds the <span style="color:#ff0000;">private data </span>of the driver core portions of the device. 
  8.  *      See the comment of the struct device_private for detail.</span> 
  9.  * @kobj:   A top-level, abstract class from which other classes are derived. 
  10.  * @init_name:  Initial name of the device. 
  11.  * @type:   The type of device. 
  12.  *      This identifies the device type and carries type-specific 
  13.  *      information. 
  14.  * @mutex:  Mutex to synchronize calls to its driver. 
  15.  * @bus:    Type of bus device is on. 
  16.  * @driver: Which driver has allocated this 
  17.  * @platform_data: Platform data specific to the device. 
  18.  *      Example: For devices on custom boards, as typical of embedded 
  19.  *      and SOC based hardware, Linux often uses platform_data to point 
  20.  *      to board-specific structures describing devices and how they 
  21.  *      are wired.  That can include what ports are available, chip 
  22.  *      variants, which GPIO pins act in what additional roles, and so 
  23.  *      on.  This shrinks the "Board Support Packages" (BSPs) and 
  24.  *      minimizes board-specific #ifdefs in drivers. 
  25.  * @power:  For device power management. 
  26.  *      See Documentation/power/devices.txt for details. 
  27.  * @pm_domain:  Provide callbacks that are executed during system suspend, 
  28.  *      hibernation, system resume and during runtime PM transitions 
  29.  *      along with subsystem-level and driver-level callbacks. 
  30.  * @pins:   For device pin management. 
  31.  *      See Documentation/pinctrl.txt for details. 
  32.  * @numa_node:  NUMA node this device is close to. 
  33.  * @dma_mask:   Dma mask (if dma'ble device). 
  34.  * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all 
  35.  *      hardware supports 64-bit addresses for consistent allocations 
  36.  *      such descriptors. 
  37.  * @dma_parms:  A low level driver may set these to teach IOMMU code about 
  38.  *      segment limitations. 
  39.  * @dma_pools:  Dma pools (if dma'ble device). 
  40.  * @dma_mem:    Internal for coherent mem override. 
  41.  * @archdata:   For arch-specific additions. 
  42.  * @of_node:    Associated device tree node. 
  43.  * @acpi_node:  Associated ACPI device node. 
  44.  * @devt:   For creating the sysfs "dev". 
  45.  * @id:     device instance 
  46.  * @devres_lock: Spinlock to protect the resource of the device. 
  47.  * @devres_head: The resources list of the device. 
  48.  * @knode_class: The node used to add the device to the class list. 
  49.  * @class:  The class of the device. 
  50.  * @groups: Optional attribute groups. 
  51.  * @release:    Callback to free the device after all references have 
  52.  *      gone away. This should be set by the allocator of the 
  53.  *      device (i.e. the bus driver that discovered the device). 
  54.  * 
  55.  * At the lowest level, every device in a Linux system is represented by an 
  56.  * instance of struct device. The device structure contains the information 
  57.  * that the device model core needs to model the system. Most subsystems, 
  58.  * however, track additional information about the devices they host. As a 
  59.  * result, it is rare for devices to be represented by bare device structures; 
  60.  * instead, that structure, like kobject structures, is usually embedded within 
  61.  * a higher-level representation of the device. 
  62.  */  
  63. struct device {  
  64.     struct device       *parent;  
  65.   
  66.     struct device_private   *p;  
  67.   
  68.     struct kobject kobj;  
  69.     const char      *init_name; /* initial name of the device */  
  70.     const struct device_type *type;  
  71.   
  72.     struct mutex        mutex;  /* mutex to synchronize calls to 
  73.                      * its driver. 
  74.                      */  
  75.   
  76.     struct bus_type *bus;       /* type of bus device is on */  
  77.     struct device_driver *driver;   /* which driver has allocated this 
  78.                        device */  
  79.     void        *platform_data; /* Platform specific data, device 
  80.                        core doesn't touch it */  
  81.     struct dev_pm_info  power;  
  82.     struct dev_pm_domain    *pm_domain;  
  83.   
  84. #ifdef CONFIG_PINCTRL  
  85.     struct dev_pin_info *pins;  
  86. #endif  
  87.   
  88. #ifdef CONFIG_NUMA  
  89.     int     numa_node;  /* NUMA node this device is close to */  
  90. #endif  
  91.     u64     *dma_mask;  /* dma mask (if dma'able device) */  
  92.     u64     coherent_dma_mask;/* Like dma_mask, but for 
  93.                          alloc_coherent mappings as 
  94.                          not all hardware supports 
  95.                          64 bit addresses for consistent 
  96.                          allocations such descriptors. */  
  97.   
  98.     struct device_dma_parameters *dma_parms;  
  99.   
  100.     struct list_head    dma_pools;  /* dma pools (if dma'ble) */  
  101.   
  102.     struct dma_coherent_mem *dma_mem; /* internal for coherent mem 
  103.                          override */  
  104. #ifdef CONFIG_CMA  
  105.     struct cma *cma_area;       /* contiguous memory area for dma 
  106.                        allocations */  
  107. #endif  
  108.     /* arch specific additions */  
  109.     struct dev_archdata archdata;  
  110.   
  111.     struct device_node  *of_node; /* associated device tree node */  
  112.     struct acpi_dev_node    acpi_node; /* associated ACPI device node */  
  113.   
  114.     dev_t           devt;   /* dev_t, creates the sysfs "dev" */  
  115.     u32         id; /* device instance */  
  116.   
  117.     spinlock_t      devres_lock;  
  118.     struct list_head    devres_head;  
  119.   
  120.     struct klist_node   knode_class;  
  121.     struct class        *class;  
  122.     const struct attribute_group **groups;  /* optional groups */  
  123.   
  124.     void    (*release)(struct device *dev);  
  125.     struct iommu_group  *iommu_group;  
  126. };  

device_private的结构体如下,指针driver_data是用来保存driver的私有数据的。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * struct device_private - structure to hold the private to the driver core portions of the device structure. 
  3.  * 
  4.  * @klist_children - klist containing all children of this device 
  5.  * @knode_parent - node in sibling list 
  6.  * @knode_driver - node in driver list 
  7.  * @knode_bus - node in bus list 
  8.  * @deferred_probe - entry in deferred_probe_list which is used to retry the 
  9.  *  binding of drivers which were unable to get all the resources needed by 
  10.  *  the device; typically because it depends on another driver getting 
  11.  *  probed first. 
  12. <span style="background-color: rgb(255, 255, 102);"> * @driver_data - private pointer for driver specific info.  Will turn into a 
  13.  * list soon.</span> 
  14.  * @device - pointer back to the struct class that this structure is 
  15.  * associated with. 
  16.  * 
  17.  * Nothing outside of the driver core should ever touch these fields. 
  18.  */  
  19. struct device_private {  
  20.     struct klist klist_children;  
  21.     struct klist_node knode_parent;  
  22.     struct klist_node knode_driver;  
  23.     struct klist_node knode_bus;  
  24.     struct list_head deferred_probe;  
  25.     void *driver_data;  
  26.     struct device *device;  
  27. };  



0 0
原创粉丝点击