platform驱动平台下,关于probe函数中,形参dev的“dev->dev.of_node;”的理解

        驱动函数里通过int platform_driver_register (struct platform_driver *driver)函数注册的驱动, 

183 static int __init miscbeep_init(void)
184 {
185 return platform_driver_register(&beep_driver);
186 }

198 module_init(miscbeep_init);

当platform总线检测到设备树有和该驱动匹配的设备节点后,就执行probe函数(  int (*probe) (struct device *dev); ),此时probe函数的形参dev指针指向的变量是platform_device类型,platform_device结构体保存的是platform平台下的设备信息。

22 struct platform_device {
23 const char *name; 
24 int id; 
25 bool id_auto;
26 struct device dev;
27 u32 num_resources; 
28 struct resource *resource;
29
30 const struct platform_device_id *id_entry;
31 char *driver_override; /* Driver name to force a match */
32
33 /* MFD cell pointer */
34 struct mfd_cell *mfd_cell;
35
36 /* arch specific additions */
37 struct pdev_archdata archdata;
38 };

        其 26行的“struct device dev” 成员(类似于C++的父类,platform_device继承了struct device )已经获取到了设备树中的节点信息,可以通过此成员获取节点信息,即“ miscbeep.nd = dev->dev.of_node;”

static int miscbeep_probe(struct platform_device *dev)
 {
 int ret = 0;

 printk("beep driver and device was matched!\r\n");
 /* 设置 BEEP 所使用的 GPIO */
 /* 1、获取设备节点:beep */
 //miscbeep.nd = of_find_node_by_path("/beep");
 
 miscbeep.nd = dev->dev.of_node;//用这种方式替代上一行“miscbeep.nd = of_find_node_by_path("/beep");”的方式来获取设备节点

device结构体:设备驱动模型中的基础结构体之一。结构体定义如下:

struct device {

  /*设备所依附的父设备,大多数情况下,这样的设备是某种总线或主控制器,如果该成员变量的值为NULL,表示当前设备是一个最顶端设备,通常这样的设备都不是你想得到的那个*/

  struct device *parent;

  struct device_private *p;

  struct kobject kobj;
  const char *init_name; /* initial name of the device */
  const struct device_type *type;

  struct mutex mutex; /* mutex 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;
  struct dev_pm_domain *pm_domain;

#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;

  struct device_node *of_node; /* associated device tree node */

  dev_t devt; /* dev_t, creates the sysfs "dev" */
  u32 id; /* device instance */

  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);
};

你可能感兴趣的:(c++,c语言,linux)