platform_set_drvdata

           probe函數中定義的局部變量,如果我想在其他地方使用它怎麼辦呢? 這就需要把它保存起來。內核提供了這個方法,使用函數platform_set_drvdata()可以將ndev保存成平臺總線設備的私有數據。以後再要使用它時只需調用platform_get_drvdata()就可以了。


#define platform_set_drvdata(_dev,data) dev_set_drvdata(&(_dev)->dev, (data))

/*   /drivers/base/dd.c  */

  1. 565 int dev_set_drvdata(struct device *dev, void *data)  
  2. 566 {  
  3. 567         int error;  
  4. 568   
  5. 569         if (!dev->p) {  
  6. 570                 error = device_private_init(dev);  
  7. 571                 if (error)  
  8. 572                         return error;  
  9. 573         }  
  10. 574         dev->p->driver_data = data;  
  11. 575         return 0;  
  12. 576 }
/* /linux/device.h */
  1. struct device {  
  2.     struct device       *parent;  
  3.   
  4.     struct device_private   *p;  
  5.   
  6.     struct kobject kobj;  
  7.     const char      *init_name; /* initial name of the device */  
  8.     struct device_type  *type;  
  9.   
  10.     struct semaphore    sem;    /* semaphore to synchronize calls to 
  11.                      * its driver. 
  12.                      */  
  13.   
  14.     struct bus_type *bus;       /* type of bus device is on */  
  15.     struct device_driver *driver;   /* which driver has allocated this 
  16.                        device */  
  17.     void        *platform_data; /* Platform specific data, device 
  18.                        core doesn't touch it */  
  19.     struct dev_pm_info  power;  
  20.   
  21. #ifdef CONFIG_NUMA  
  22.     int     numa_node;  /* NUMA node this device is close to */  
  23. #endif  
  24.     u64     *dma_mask;  /* dma mask (if dma'able device) */  
  25.     u64     coherent_dma_mask;/* Like dma_mask, but for 
  26.                          alloc_coherent mappings as 
  27.                          not all hardware supports 
  28.                          64 bit addresses for consistent 
  29.                          allocations such descriptors. */  
  30.   
  31.     struct device_dma_parameters *dma_parms;  
  32.   
  33.     struct list_head    dma_pools;  /* dma pools (if dma'ble) */  
  34.   
  35.     struct dma_coherent_mem *dma_mem; /* internal for coherent mem 
  36.                          override */  
  37.     /* arch specific additions */  
  38.     struct dev_archdata archdata;  
  39.   
  40.     dev_t           devt;   /* dev_t, creates the sysfs "dev" */  
  41.   
  42.     spinlock_t      devres_lock;  
  43.     struct list_head    devres_head;  
  44.   
  45.     struct klist_node   knode_class;  
  46.     struct class        *class;  
  47.     const struct attribute_group **groups;  /* optional groups */  
  48.   
  49.     void    (*release)(struct device *dev);  
  50. };  
/* /drivers/base/base.h */
  1. 73 struct device_private {  
  2. 74         struct klist klist_children;  
  3. 75         struct klist_node knode_parent;  
  4. 76         struct klist_node knode_driver;  
  5. 77         struct klist_node knode_bus;  
  6. 78         struct list_head deferred_probe;  
  7. 79         void *driver_data;  
  8. 80         struct device *device;  
  9. 81 };  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章