Chinaunix首页 | 论坛 | 博客
  • 博客访问: 506434
  • 博文数量: 92
  • 博客积分: 3146
  • 博客等级: 中校
  • 技术积分: 2314
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-27 10:20
文章分类

全部博文(92)

文章存档

2014年(3)

2013年(17)

2012年(16)

2011年(22)

2010年(34)

分类: 嵌入式

2011-08-23 12:15:20

 1、  哪些适用于plarform驱动?
platform机制将设备本身的资源注册进内核,由内核统一管理,在驱动程序中使用这些资源时通过platform device提供的标准接口进行申请并使用。这样提高了驱动和资源管理的独立性,这样拥有更好的可移植性。platform机制的本身使用并不复杂,由两部分组成:platform_device和platfrom_driver。Platform driver通过platform bus获取platform_device。

通常情况下只要和内核本身运行依赖性不大的外围设备,相对独立的,拥有各自独立的资源(地址总线和IRQs),都可以用 platform_driver来管理,而timer,irq等小系统之内的设备则最好不用platfrom_driver机制。

platform_device最大的特定是CPU直接寻址设备的寄存器空间,即使对于其他总线设备,设备本身的寄存器无法通过CPU总线访问,但总线的controller仍然需要通过platform bus来管理。

总之,platfrom_driver的根本目的是为了统一管理系统的外设资源,为驱动程序提供统一的接口来访问系统资源,将驱动和资源分离,提高程序的可移植性。

2、    基于platform总线的驱动开发流程
基于Platform总线的驱动开发流程如下:
?    定义初始化platform bus  //kernel 起来就会注册
?    定义各种platform devices //定义一般是在CPU芯片厂商目录下arch/arm/**  将platform_device归纳为一个数组,最终通过platform_add_devices()函数统一注册

点击(此处)折叠或打开

  1. struct platform_device sirfsoc_gps_device = {
  2.     .name = "sirfsoc-gps",
  3.     .id = -1,
  4.     .num_resources = ARRAY_SIZE(sirfsoc_gps_resources),
  5.     .resource = sirfsoc_gps_resources,
  6.     .dev = {
  7.         .platform_data = &gps_pdata,
  8.     },
  9. };


?    注册各种platform devices

点击(此处)折叠或打开

  1. static struct platform_device *atlas6cb_devices[] __initdata = {

  2.     &sirfsoc_gps_device,


  3. };

  4. void __init atlas6_add_devices(void)
  5. {
  6.     
  7.     platform_add_devices(atlas6cb_devices, ARRAY_SIZE(atlas6cb_devices));
  8. }

?    定义相关platform driver  //在驱动文件中定义如下结构体

点击(此处)折叠或打开

  1. static struct platform_driver gps_platform_driver = {
  2.     .probe =    gps_platform_probe,
  3.     .remove =    gps_platform_remove,
  4.     .driver    =    {
  5.     .name = "sirfsoc-gps",
  6.     },
  7. }; 
?    注册相关platform driver

点击(此处)折叠或打开

  1. static int __init gps_platform_init(void)
  2. {
  3.     
  4.     return platform_driver_register(&gps_platform_driver);
  5. }
?    操作相关设备
3、   何谓platform bus?
Linux系统中许多部分对设备是如何链接的并不感兴趣,但是他们需要知道哪些类型的设备是可以使用的。设备模型提供了一种机制来对设备进行分类,在更高的功能层面上描述这些设备,并使得这些设备对用户空间可见。因此从2.6内核开始引入了设备模型。

总线是处理器和一个或多个设备之间的通道,在设备模型中, 所有的设备都通过总线相连。总线可以相互插入。设备模型展示了总线和它们所控制的设备之间的实际连接。

Platform总线是2.6 kernel中最近引入的一种虚拟总线,主要用来管理CPU的片上资源,具有更好的移植性,因此在2.6 kernel中,很多驱动都用platform改写了。
platform_bus_type的定义如下:
  1. 609struct bus_type platform_bus_type = {
  2. 610 .name = "platform",
  3. 611 .dev_attrs = platform_dev_attrs,
  4. 612 .match = platform_match,
  5. 613 .uevent = platform_uevent,
  6. 614 .suspend = platform_suspend,
  7. 615 .suspend_late = platform_suspend_late,
  8. 616 .resume_early = platform_resume_early,
  9. 617 .resume = platform_resume,
  10. 618};
  11. 619EXPORT_SYMBOL_GPL(platform_bus_type);
  12. http://lxr.linux.no/#linux+v2.6.25/include/linux/device.h#L55
  13. 55struct bus_type {
  14. 56 const char *name;
  15. 57 struct bus_attribute *bus_attrs;
  16. 58 struct device_attribute *dev_attrs;
  17. 59 struct driver_attribute *drv_attrs;
  18. 60
  19. 61 int (*match)(struct device *dev, struct device_driver *drv);
  20. 62 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  21. 63 int (*probe)(struct device *dev);
  22. 64 int (*remove)(struct device *dev);
  23. 65 void (*shutdown)(struct device *dev);
  24. 66
  25. 67 int (*suspend)(struct device *dev, pm_message_t state);
  26. 68 int (*suspend_late)(struct device *dev, pm_message_t state);
  27. 69 int (*resume_early)(struct device *dev);
  28. 70 int (*resume)(struct device *dev);
  29. 71
  30. 72 struct bus_type_private *p;
  31. 73};
总线名称是"platform",其只是bus_type的一种,定义了总线的属性,同时platform_bus_type还有相关操作方法,如挂起、中止、匹配及hotplug事件等。
总线bus是联系driver和device的中间枢纽。Device通过所属的bus找到driver,由match操作方法进行匹配。
 
4    bus、device及driver三者之间的关系
在数据结构设计上,总线、设备及驱动三者相互关联。

platform device包含device,根据device可以获得相应的bus及driver。

设备添加到总线上后形成一个双向循环链表,根据总线可以获得其上挂接的所有device,进而获得了 platform device。根据device也可以获得驱动该总线上所有设备的相关driver。

platform driver包含driver,根据driver可以获得相应的bus,进而获得bus上所有的device,进一步获得platform device,根据name对driver与platform device进行匹配,匹配成功后将device与相应的driver关联起来,即实现了platform device和platform driver的关联。

匹配成功后调用driver的probe进而调用platform driver的probe,在probe里实现驱动特定的功能。
5、    device和platform_device
Plarform device会有一个名字用于driver binding(在注册driver的时候会查找driver的目标设备的bus位置,这个过程称为driver binding),另外IRQ以及地址空间等资源也要给出 。

platform_device结构体用来描述设备的名称、资源信息等。该结构被定义在http://lxr.linux.no/#linux+v2.6.25/include/linux/platform_device.h#L16中,定义原型如下:
  1. struct platform_device {
  2.           const char * name; //定义平台设备的名称,此处设备的命名应和相应驱动程序命名一致

  3.         int id;
  4.         struct device dev;
  5.          u32 num_resources;
  6.           struct resource * resource; //定义平台设备的资源
  7.   };
在这个结构里封装了struct device及struct resource。可知:platform_device由device派生而来,是一种特殊的device。

下面来看一下platform_device结构体中最重要的一个成员struct resource * resource。struct resource被定义在中,定义原型如下:
  1. struct resource {
  2.           resource_size_t start; //定义资源的起始地址
  3.           resource_size_t end; //定义资源的结束地址
  4.         const char *name; //定义资源的名称
  5.           unsigned long flags; 定义资源的类型,比如MEM,IO,IRQ,DMA类型
  6.           struct resource *parent, *sibling, *child;
  7.   };
这个结构表示设备所拥有的资源,即I/O端口、I/O映射内存、中断及DMA等。这里的地址指的是物理地址。
6、device_register和platform_device_register

 
  1. int device_register(struct device *dev)
  2.  {
  3.          device_initialize(dev);
  4.          return device_add(dev);
  5.  }
  6. //初始化一个设备,然后加入到系统中

  7. int platform_device_register(struct platform_device *pdev)
  8.  {
  9.          device_initialize(&pdev->dev);
  10.          return platform_device_add(pdev);
  11. }
  12.  EXPORT_SYMBOL_GPL(platform_device_register);
我们看到注册一个platform device分为了两部分,初始化这个platform_device,然后将此platform_device添加到platform总线中。输入参数platform_device可以是静态的全局设备。

另外一种机制就是动态申请platform_device_alloc一个platform_device设备,然后通过platform_device_add_resources及platform_device_add_data等添加相关资源和属性。

无论哪一种platform_device,最终都将通过platform_device_add这册到platform总线上。
device_register()和platform_device_register()都会首先初始化设备
区别在于第二步:其实platform_device_add()包括device_add(),不过要先注册resources,然后将设备挂接到特定的platform总线。
 
7    device_driver和platform driver
Platform device是一种device自己是不会做事情的,要有人为它做事情,那就是platform driver。platform driver遵循linux系统的driver model。对于device的discovery/enumerate都不是driver自己完成的而是由由系统的driver注册机制完成。driver编写人员只要将注册必须的数据结构初始化并调用注册driver的kernel API就可以了。

接下来来看platform_driver结构体的原型定义,在http://lxr.linux.no/#linux+v2.6.25/include/linux/platform_device.h#L48中,代码如下:
  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 (*suspend_late)(struct platform_device *, pm_message_t state);
  7.           int (*resume_early)(struct platform_device *);
  8.           int (*resume)(struct platform_device *);
  9.           struct device_driver driver;
  10.   };

可见,它包含了设备操作的几个功能函数,同时包含了一个device_driver结构,说明device_driver是platform_driver的基类。驱动程序中需要初始化这个变量。下面看一下这个变量的定义,位于http://lxr.linux.no/#linux+v2.6.25/include/linux/device.h#L121中:
  1. struct device_driver {
  2.          const char *name;
  3.         struct bus_type *bus;
  4.           struct module *owner;
  5.          const char *mod_name; /* used for built-in modules */
  6.           int (*probe) (struct device *dev);
  7.          int (*remove) (struct device *dev);
  8.          void (*shutdown) (struct device *dev);
  9.          int (*suspend) (struct device *dev, pm_message_t state);
  10.          int (*resume) (struct device *dev);
  11.          struct attribute_group **groups;
  12.           struct driver_private *p;
  13.  };
device_driver提供了一些操作接口,但其并没有实现,相当于一些虚函数,由派生类platform_driver进行重载,无论何种类型的driver都是基于device_driver派生而来的,具体的各种操作都是基于统一的基类接口的,这样就实现了面向对象的设计。

需要注意这两个变量:name和owner。其作用主要是为了和相关的platform_device关联起来,owner的作用是说明模块的所有者,驱动程序中一般初始化为THIS_MODULE。

device_driver结构中也有一个name变量。platform_driver从字面上来看就知道是设备驱动。设备驱动是为谁服务的呢?当然是设备了。内核正是通过这个一致性来为驱动程序找到资源,即 platform_device中的resource。
8    driver_register 和platform_driver_register

内核提供的platform_driver结构体的注册函数为platform_driver_register(),其原型定义在文件中,具体实现代码如下:

  1. int platform_driver_register(struct platform_driver *drv)
  2.  {
  3.          drv->driver.bus = &platform_bus_type;
  4.    /*设置成platform_bus_type这个很重要,因为driver和device是通过bus联系在一起的,具体在本例中是通过 platform_bus_type中注册的回调例程和属性来是实现的, driver与device的匹配就是通过 platform_bus_type注册的回调例程platform_match ()来完成的。*/

  5.          if (drv->probe)
  6.                  drv->driver.probe = platform_drv_probe;
  7.  //在really_probe函数中,回调了platform_drv_probe函数

  8.         if (drv->remove)
  9.                  drv->driver.remove = platform_drv_remove;
  10.          if (drv->shutdown)
  11.                  drv->driver.shutdown = platform_drv_shutdown;
  12.          if (drv->suspend)
  13.                  drv->driver.suspend = platform_drv_suspend;
  14.          if (drv->resume)
  15.                  drv->driver.resume = platform_drv_resume;
  16.          return driver_register(&drv->driver);
  17.  }
  18.  EXPORT_SYMBOL_GPL(platform_driver_register);

不要被上面的platform_drv_XXX吓倒了,它们其实很简单,就是将struct device转换为struct platform_device和struct platform_driver,然后调用platform_driver中的相应接口函数。那为什么不直接调用platform_drv_XXX等接口呢?这就是Linux内核中面向对象的设计思想。

device_driver提供了一些操作接口,但其并没有实现,相当于一些虚函数,由派生类platform_driver进行重载,无论何种类型的driver都是基于device_driver派生而来的,device_driver中具体的各种操作都是基于统一的基类接口的,这样就实现了面向对象的设计。

更详细的请看:http://blog.csdn.net/sailor_8318/article/details/5267698

                    http://blog.chinaunix.net/uid-27041925-id-3884962.html



 
阅读(2328) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

kingsley_z2013-12-05 21:54:55

写的很清晰,转走了,谢谢分享。