Linux平台总线驱动设备模型

来源:互联网 发布:sql合并部分相同行 编辑:程序博客网 时间:2024/05/16 00:58

原文地址:http://blog.csdn.net/lwj103862095/article/details/17957637

platform总线是一种虚拟的总线,相应的设备则为platform_device,而驱动则为platform_driver。Linux 2.6的设备驱动模型中,把I2C、RTC、LCD等都归纳为platform_device。

总线将设备和驱动绑定,在系统每注册一个设备的时候,会寻找与之匹配的驱动;相反的,在系统每注册一个驱动的时候,会寻找与之匹配的设备,而匹配由总线完成。

Linux2.6系统中定义了一个bus_type的实例platform_bus_type

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct bus_type platform_bus_type = {  
  2.     .name       = "platform",  
  3.     .dev_attrs  = platform_dev_attrs,  
  4.     .match      = platform_match,       //设备和驱动使用match函数来判断是否匹配  
  5.     .uevent     = platform_uevent,  
  6.     .pm     = PLATFORM_PM_OPS_PTR,  
  7. };  

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /* platform_match函数用于匹配总线中的驱动和设备 */  
  2. static int platform_match(struct device *dev, struct device_driver *drv)  
  3. {  
  4.     struct platform_device *pdev = to_platform_device(dev);  
  5.     struct platform_driver *pdrv = to_platform_driver(drv);  
  6.   
  7.     /* match against the id table first */  
  8.     if (pdrv->id_table)  
  9.         return platform_match_id(pdrv->id_table, pdev) != NULL;  
  10.   
  11.     /* fall-back to driver name match */  
  12.     return (strcmp(pdev->name, drv->name) == 0);  
  13. }  

platform_match函数首先判断是否由id_table,如果有则使用id_table来进行匹配,否则,判断platform_device和platform_driver成员里的name,如果二者的name字段相同则匹配,如果匹配则调用platform_driver的probe函数。

platform_device结构体的定义

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct platform_device {  
  2.     const char  * name;         /* 名字 */  
  3.     int     id;  
  4.     struct device   dev;  
  5.     u32     num_resources;      /* 资源总数 */  
  6.     struct resource * resource; /* 资源 */  
  7.   
  8.     struct platform_device_id   *id_entry;  
  9. };  

其中有个重要的成员是resource,是设备的资源信息,如IO地址,中断号等。

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct resource {  
  2.     resource_size_t start;      //资源的起始值  
  3.     resource_size_t end;        //资源的结束值  
  4.     const char *name;  
  5.     unsigned long flags;        //资源的类型,如IORESOURCE_IO,IORESOURCE_MEM,IORESOURCE_IRQ,IORESOURCE_DMA  
  6.     struct resource *parent, *sibling, *child;  
  7. };  

有的设备可能有多个资源,通常使用platform_get_resource函数来获取资源

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * platform_get_resource - get a resource for a device 
  3.  * @dev: platform device 
  4.  * @type: resource type 
  5.  * @num: resource index 
  6.  */  
  7. struct resource *platform_get_resource(struct platform_device *dev,  
  8.                        unsigned int type, unsigned int num)  
  9. {  
  10.     int i;  
  11.   
  12.     for (i = 0; i < dev->num_resources; i++) {  
  13.         struct resource *r = &dev->resource[i];  
  14.   
  15.         if (type == resource_type(r) && num-- == 0)  
  16.             return r;  
  17.     }  
  18.     return NULL;  
  19. }  

平台设备的注册,使用platform_device_register函数

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int platform_device_register(struct platform_device *pdev)  
  2. {  
  3.     device_initialize(&pdev->dev);  
  4.     return platform_device_add(pdev);  
  5. }  

platform_device_register函数先通过device_initialize函数初始化platform_device的device成员,然后调用platform_device_add向内核添加一个平台设备。

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int platform_device_add(struct platform_device *pdev)  
  2. {  
  3.     int i, ret = 0;  
  4.   
  5.     if (!pdev)  /* 如果pdev为空则返回EINVAL */  
  6.         return -EINVAL;  
  7.   
  8.     /* 如果pdev->dev.parent为空则将pdev->dev.parent设置为platform_bus */  
  9.     if (!pdev->dev.parent)  
  10.         pdev->dev.parent = &platform_bus;  
  11.   
  12.     pdev->dev.bus = &platform_bus_type;  /* 设置总线类型 */  
  13.   
  14.     if (pdev->id != -1)      /* 如果id = -1则表示自动分配name */  
  15.         dev_set_name(&pdev->dev, "%s.%d", pdev->name,  pdev->id);  
  16.     else  
  17.         dev_set_name(&pdev->dev, pdev->name);  
  18.   
  19.     for (i = 0; i < pdev->num_resources; i++) {  
  20.         struct resource *p, *r = &pdev->resource[i]; /* 获取资源 */  
  21.   
  22.         if (r->name == NULL)  
  23.             r->name = dev_name(&pdev->dev);  
  24.   
  25.         p = r->parent;  
  26.         if (!p) {  
  27.             if (resource_type(r) == IORESOURCE_MEM) /* 设置资源类型 */  
  28.                 p = &iomem_resource;  
  29.             else if (resource_type(r) == IORESOURCE_IO)  
  30.                 p = &ioport_resource;  
  31.         }  
  32.   
  33.         if (p && insert_resource(p, r)) {  
  34.             printk(KERN_ERR  
  35.                    "%s: failed to claim resource %d\n",  
  36.                    dev_name(&pdev->dev), i);  
  37.             ret = -EBUSY;  
  38.             goto failed;  
  39.         }  
  40.     }  
  41.   
  42.     pr_debug("Registering platform device '%s'. Parent at %s\n",  
  43.          dev_name(&pdev->dev), dev_name(pdev->dev.parent));  
  44.   
  45.     /* 向内核添加一个device */  
  46.     ret = device_add(&pdev->dev);  
  47.     if (ret == 0)  
  48.         return ret;  
  49.   
  50.  failed:  
  51.     while (--i >= 0) {  
  52.         struct resource *r = &pdev->resource[i];  
  53.         unsigned long type = resource_type(r);  
  54.   
  55.         if (type == IORESOURCE_MEM || type == IORESOURCE_IO)  
  56.             release_resource(r);  
  57.     }  
  58.   
  59.     return ret;  
  60. }  

platform_device_add最终调用device_add来完成平台设备的注册。


 相反地,如果要注销平台设备则使用platform_device_unregister函数

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. void platform_device_unregister(struct platform_device *pdev)  
  2. {  
  3.     platform_device_del(pdev);  
  4.     platform_device_put(pdev);  
  5. }  

platform_device_unregister函数调用platform_device_del函数来注销平台设备

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. void platform_device_del(struct platform_device *pdev)  
  2. {  
  3.     int i;  
  4.   
  5.     if (pdev) {  
  6.         device_del(&pdev->dev);  
  7.   
  8.         for (i = 0; i < pdev->num_resources; i++) {  
  9.             struct resource *r = &pdev->resource[i];  
  10.             unsigned long type = resource_type(r);  
  11.   
  12.             if (type == IORESOURCE_MEM || type == IORESOURCE_IO)  
  13.                 release_resource(r);  
  14.         }  
  15.     }  
  16. }  

platform_device_del函数调用device_del函数来删除平台设备,相应地,要释放资源应调用release_resource函数,前提是资源的类型必须为IORESOURCE_MEM或者IORESOURCE_IO

platform_driver的定义:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct platform_driver {  
  2.     int (*probe)(struct platform_device *);  
  3.     int (*remove)(struct platform_device *);  
  4.     void (*shutdown)(struct platform_device *);  
  5.     int (*suspend)(struct platform_device *, pm_message_t state);  
  6.     int (*resume)(struct platform_device *);  
  7.     struct device_driver driver;  
  8.     const struct platform_device_id *id_table;  
  9. };  


device_driver的定义:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct device_driver {  
  2.     const char      *name;  
  3.     struct bus_type     *bus;  
  4.   
  5.     struct module       *owner;  
  6.     const char      *mod_name;  /* used for built-in modules */  
  7.   
  8.     bool suppress_bind_attrs;   /* disables bind/unbind via sysfs */  
  9.   
  10.     const struct of_device_id   *of_match_table;  
  11.     const struct acpi_device_id *acpi_match_table;  
  12.   
  13.     int (*probe) (struct device *dev);  
  14.     int (*remove) (struct device *dev);  
  15.     void (*shutdown) (struct device *dev);  
  16.     int (*suspend) (struct device *dev, pm_message_t state);  
  17.     int (*resume) (struct device *dev);  
  18.     const struct attribute_group **groups;  
  19.   
  20.     const struct dev_pm_ops *pm;  
  21.   
  22.     struct driver_private *p;  
  23. };  

platform_driver结构体有device_driver成员,该成员的各自字段如上所示,device_driver也有probe、remove、shutdown等函数,在平台驱动注册的时候被初始化。

前面说过,当系统中存在有平台设备和平台驱动通过总线的match函数匹配后则会调用platform_driver的probe函数,参数为platform_device,有时候也通过id_table来判断是否匹配。

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct platform_device_id {  
  2.     char name[PLATFORM_NAME_SIZE];  
  3.     kernel_ulong_t driver_data  
  4.             __attribute__((aligned(sizeof(kernel_ulong_t))));  
  5. };  

平台驱动的注册使用platform_driver_register函数

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int platform_driver_register(struct platform_driver *drv)  
  2. {  
  3.     drv->driver.bus = &platform_bus_type;  
  4.     if (drv->probe)  
  5.         drv->driver.probe = platform_drv_probe;  
  6.     if (drv->remove)  
  7.         drv->driver.remove = platform_drv_remove;  
  8.     if (drv->shutdown)  
  9.         drv->driver.shutdown = platform_drv_shutdown;  
  10.     if (drv->suspend)  
  11.         drv->driver.suspend = platform_drv_suspend;  
  12.     if (drv->resume)  
  13.         drv->driver.resume = platform_drv_resume;  
  14.     return driver_register(&drv->driver);  
  15. }  

先初始化platform_driver里的driver,该driver的类型为device_driver,设置driver的bus为platform_bus_type;设置driver的probe为platform_drv_probe;设置driver的remove为platform_drv_remove;设置driver的shutdown为platform_drv_shutdown;设置driver的suspend为platform_drv_suspend;设置driver的resume为platform_drv_resume,最后调用driver_register函数来注册平台驱动。

相反地,要注销平台驱动的话,使用platform_driver_unregister函数

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. void platform_driver_unregister(struct platform_driver *drv)  
  2. {  
  3.     driver_unregister(&drv->driver);  
  4. }  

附韦老师总结的bus_drv_dev模型的框架图



0 0
原创粉丝点击