probe函數中定義的局部變量,如果我想在其他地方使用它怎麼辦呢? 這就需要把它保存起來。內核提供了這個方法,使用函數platform_set_drvdata()可以將ndev保存成平臺總線設備的私有數據。以後再要使用它時只需調用platform_get_drvdata()就可以了。
#define platform_set_drvdata(_dev,data) dev_set_drvdata(&(_dev)->dev, (data))
/* /drivers/base/dd.c */
- 565 int dev_set_drvdata(struct device *dev, void *data)
- 566 {
- 567 int error;
- 568
- 569 if (!dev->p) {
- 570 error = device_private_init(dev);
- 571 if (error)
- 572 return error;
- 573 }
- 574 dev->p->driver_data = data;
- 575 return 0;
- 576 }
- struct device {
- struct device *parent;
- struct device_private *p;
- struct kobject kobj;
- const char *init_name; /* initial name of the device */
- struct device_type *type;
- struct semaphore sem; /* semaphore to synchronize calls to
- * its driver.
- */
- struct bus_type *bus; /* type of bus device is on */
- struct device_driver *driver; /* which driver has allocated this
- device */
- void *platform_data; /* Platform specific data, device
- core doesn't touch it */
- struct dev_pm_info power;
- #ifdef CONFIG_NUMA
- int numa_node; /* NUMA node this device is close to */
- #endif
- u64 *dma_mask; /* dma mask (if dma'able device) */
- u64 coherent_dma_mask;/* Like dma_mask, but for
- alloc_coherent mappings as
- not all hardware supports
- 64 bit addresses for consistent
- allocations such descriptors. */
- struct device_dma_parameters *dma_parms;
- struct list_head dma_pools; /* dma pools (if dma'ble) */
- struct dma_coherent_mem *dma_mem; /* internal for coherent mem
- override */
- /* arch specific additions */
- struct dev_archdata archdata;
- dev_t devt; /* dev_t, creates the sysfs "dev" */
- spinlock_t devres_lock;
- struct list_head devres_head;
- struct klist_node knode_class;
- struct class *class;
- const struct attribute_group **groups; /* optional groups */
- void (*release)(struct device *dev);
- };
- 73 struct device_private {
- 74 struct klist klist_children;
- 75 struct klist_node knode_parent;
- 76 struct klist_node knode_driver;
- 77 struct klist_node knode_bus;
- 78 struct list_head deferred_probe;
- 79 void *driver_data;
- 80 struct device *device;
- 81 };