platform总线和普通总线,驱动模型间的一些关系和理解

【摘要】本文以Linux 2.6.25 内核为例,分析了基于platform总线的驱动模型。首先介绍了Platform总线的基本概念,接着介绍了platform device和platform driver的定义和加载过程,分析了其与基类device 和driver的派生关系及在此过程中面向对象的设计思想。最后以ARM S3C2440中I2C控制器为例介绍了基于platform总线的驱动开发流程。

【关键字】platform_bus, platform_device, resource , platform_driver, file_operations

目录

1    何谓platform bus?    2
2    device和platform_device    3
3    device_register和platform_device_register    5
4    device_driver和platform driver    8
5    driver_register 和platform_driver_register    10
6    bus、device及driver三者之间的关系    17
7    哪些适用于plarform驱动?    18
8    基于platform总线的驱动开发流程    18
8.1    初始化platform_bus    19
8.2    定义platform_device    22
8.3    注册platform_device    22
8.4    定义platform_driver    28
8.5    注册platform_driver    29
8.6    操作设备    32

1    何谓platform bus?
Linux系统中许多部分对设备是如何链接的并不感兴趣,但是他们需要知道哪些类型的设备是可以使用的。设备模型提供了一种机制来对设备进行分类,在更高的功能层面上描述这些设备,并使得这些设备对用户空间可见。因此从2.6内核开始引入了设备模型。

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

Platform总线是2.6 kernel中最近引入的一种虚拟总线,主要用来管理CPU的片上资源,具有更好的移植性,因此在2.6 kernel中,很多驱动都用platform改写了。

platform_bus_type的定义如下:

 
  1. http://lxr.linux.no/#linux+v2.6.25/drivers/base/platform.c#L609

  2. 609struct bus_type platform_bus_type = {

  3. 610 .name = "platform",

  4. 611 .dev_attrs = platform_dev_attrs,

  5. 612 .match = platform_match,

  6. 613 .uevent = platform_uevent,

  7. 614 .suspend = platform_suspend,

  8. 615 .suspend_late = platform_suspend_late,

  9. 616 .resume_early = platform_resume_early,

  10. 617 .resume = platform_resume,

  11. 618};

  12. 619EXPORT_SYMBOL_GPL(platform_bus_type);

  13.  
  14. http://lxr.linux.no/#linux+v2.6.25/include/linux/device.h#L55

  15. 55struct bus_type {

  16. 56 const char *name;

  17. 57 struct bus_attribute *bus_attrs;

  18. 58 struct device_attribute *dev_attrs;

  19. 59 struct driver_attribute *drv_attrs;

  20. 60

  21. 61 int (*match)(struct device *dev, struct device_driver *drv);

  22. 62 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);

  23. 63 int (*probe)(struct device *dev);

  24. 64 int (*remove)(struct device *dev);

  25. 65 void (*shutdown)(struct device *dev);

  26. 66

  27. 67 int (*suspend)(struct device *dev, pm_message_t state);

  28. 68 int (*suspend_late)(struct device *dev, pm_message_t state);

  29. 69 int (*resume_early)(struct device *dev);

  30. 70 int (*resume)(struct device *dev);

  31. 71

  32. 72 struct bus_type_private *p;

  33. 73};



总线名称是"platform",其只是bus_type的一种,定义了总线的属性,同时platform_bus_type还有相关操作方法,如挂起、中止、匹配及hotplug事件等。

总线bus是联系driver和device的中间枢纽。Device通过所属的bus找到driver,由match操作方法进行匹配。

 
Bus、driver及devices的连接关系

2    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. 16struct platform_device {

  2. 17 const char * name; //定义平台设备的名称,此处设备的命名应和相应驱动程序命名一致

  3.  
  4. 18 int id;

  5. 19 struct device dev;

  6. 20 u32 num_resources;

  7. 21 struct resource * resource; //定义平台设备的资源

  8. 22};



在这个结构里封装了struct device及struct resource。可知:platform_device由device派生而来,是一种特殊的device。

下面来看一下platform_device结构体中最重要的一个成员struct resource * resource。struct resource被定义在

 
  1. http://lxr.linux.no/#linux+v2.6.25/include/linux/ioport.h#L18中,定义原型如下:

  2. 14/*

  3. 15 * Resources are tree-like, allowing

  4. 16 * nesting etc..

  5. 17 */

  6. 18struct resource {

  7. 19 resource_size_t start; //定义资源的起始地址

  8. 20 resource_size_t end; //定义资源的结束地址

  9. 21 const char *name; //定义资源的名称

  10. 22 unsigned long flags; 定义资源的类型,比如MEM,IO,IRQ,DMA类型

  11. 23 struct resource *parent, *sibling, *child;

  12. 24};



这个结构表示设备所拥有的资源,即I/O端口、I/O映射内存、中断及DMA等。这里的地址指的是物理地址。

另外还需要注意platform_device中的device结构,它详细描述了设备的情况,其为所有设备的基类,定义如下:

 
  1. http://lxr.linux.no/#linux+v2.6.25/include/linux/device.h#L422

  2. 422struct device {

  3. 423 struct klist klist_children;

  4. 424 struct klist_node knode_parent; /* node in sibling list */

  5. 425 struct klist_node knode_driver;

  6. 426 struct klist_node knode_bus;

  7. 427 struct device *parent;

  8. 428

  9. 429 struct kobject kobj;

  10. 430 char bus_id[BUS_ID_SIZE]; /* position on parent bus */

  11. 431 struct device_type *type;

  12. 432 unsigned is_registered:1;

  13. 433 unsigned uevent_suppress:1;

  14. 434

  15. 435 struct semaphore sem; /* semaphore to synchronize calls to

  16. 436 * its driver.

  17. 437 */

  18. 438

  19. 439 struct bus_type *bus; /* type of bus device is on */

  20. 440 struct device_driver *driver; /* which driver has allocated this

  21. 441 device */

  22. 442 void *driver_data; /* data private to the driver */

  23. 443 void *platform_data; /* Platform specific data, device

  24. 444 core doesn't touch it */

  25. 445 struct dev_pm_info power;

  26. 446

  27. 447#ifdef CONFIG_NUMA

  28. 448 int numa_node; /* NUMA node this device is close to */

  29. 449#endif

  30. 450 u64 *dma_mask; /* dma mask (if dma'able device) */

  31. 451 u64 coherent_dma_mask;/* Like dma_mask, but for

  32. 452 alloc_coherent mappings as

  33. 453 not all hardware supports

  34. 454 64 bit addresses for consistent

  35. 455 allocations such descriptors. */

  36. 456

  37. 457 struct device_dma_parameters *dma_parms;

  38. 458

  39. 459 struct list_head dma_pools; /* dma pools (if dma'ble) */

  40. 460

  41. 461 struct dma_coherent_mem *dma_mem; /* internal for coherent mem

  42. 462 override */

  43. 463 /* arch specific additions */

  44. 464 struct dev_archdata archdata;

  45. 465

  46. 466 spinlock_t devres_lock;

  47. 467 struct list_head devres_head;

  48. 468

  49. 469 /* class_device migration path */

  50. 470 struct list_head node;

  51. 471 struct class *class;

  52. 472 dev_t devt; /* dev_t, creates the sysfs "dev" */

  53. 473 struct attribute_group **groups; /* optional groups */

  54. 474

  55. 475 void (*release)(struct device *dev);

  56. 476};

  57. 477




3    device_register和platform_device_register
 

 
  1. http://lxr.linux.no/#linux+v2.6.25/drivers/base/core.c#L881

  2. 870/**

  3. 871 * device_register - register a device with the system.

  4. 872 * @dev: pointer to the device structure

  5. 873 *

  6. 874 * This happens in two clean steps - initialize the device

  7. 875 * and add it to the system. The two steps can be called

  8. 876 * separately, but this is the easiest and most common.

  9. 877 * I.e. you should only call the two helpers separately if

  10. 878 * have a clearly defined need to use and refcount the device

  11. 879 * before it is added to the hierarchy.

  12. 880 */

  13. 881int device_register(struct device *dev)

  14. 882{

  15. 883 device_initialize(dev);

  16. 884 return device_add(dev);

  17. 885}

  18. 初始化一个设备,然后加入到系统中。

  19.  
  20. http://lxr.linux.no/#linux+v2.6.25/drivers/base/platform.c#L325

  21. 316/**

  22. 317 * platform_device_register - add a platform-level device

  23. 318 * @pdev: platform device we're adding

  24. 319 */

  25. 320int platform_device_register(struct platform_device *pdev)

  26. 321{

  27. 322 device_initialize(&pdev->dev);

  28. 323 return platform_device_add(pdev);

  29. 324}

  30. 325EXPORT_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总线上。
 

 
  1. 229/**

  2. 230 * platform_device_add - add a platform device to device hierarchy

  3. 231 * @pdev: platform device we're adding

  4. 232 *

  5. 233 * This is part 2 of platform_device_register(), though may be called

  6. 234 * separately _iff_ pdev was allocated by platform_device_alloc().

  7. 235 */

  8. 236int platform_device_add(struct platform_device *pdev)

  9. 237{

  10. 238 int i, ret = 0;

  11. 239

  12. 240 if (!pdev)

  13. 241 return -EINVAL;

  14. 242

  15. 初始化设备的parent为platform_bus,初始化驱备的总线为platform_bus_type。

  16. 243 if (!pdev->dev.parent)

  17. 244 pdev->dev.parent = &platform_bus;

  18. 245

  19. 246 pdev->dev.bus = &platform_bus_type;

  20. 247

  21. /*++++++++++++++

  22. The platform_device.dev.bus_id is the canonical name for the devices.

  23. It's built from two components:

  24.  
  25. * platform_device.name ... which is also used to for driver matching.

  26. * platform_device.id ... the device instance number, or else "-1"

  27. to indicate there's only one.

  28.  
  29. These are concatenated, so name/id "serial"/0 indicates bus_id "serial.0", and

  30. "serial/3" indicates bus_id "serial.3"; both would use the platform_driver

  31. named "serial". While "my_rtc"/-1 would be bus_id "my_rtc" (no instance id)

  32. and use the platform_driver called "my_rtc".

  33. ++++++++++++++*/

  34. 248 if (pdev->id != -1)

  35. 249 snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%d", pdev->name,

  36. 250 pdev->id);

  37. 251 else

  38. 252 strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);

  39. 253

  40. 设置设备struct device 的bus_id成员,留心这个地方,在以后还需要用到这个的。

  41. 254 for (i = 0; i < pdev->num_resources; i++) {

  42. 255 struct resource *p, *r = &pdev->resource[i];

  43. 256

  44. 257 if (r->name == NULL)

  45. 258 r->name = pdev->dev.bus_id;

  46. 259

  47. 260 p = r->parent;

  48. 261 if (!p) {

  49. 262 if (r->flags & IORESOURCE_MEM)

  50. 263 p = &iomem_resource;

  51. 264 else if (r->flags & IORESOURCE_IO)

  52. 265 p = &ioport_resource;

  53. 266 }

  54. //resources分为两种IORESOURCE_MEM和IORESOURCE_IO

  55. //CPU对外设IO端口物理地址的编址方式有两种:I/O映射方式和内存映射方式

  56. 267

  57. 268 if (p && insert_resource(p, r)) {

  58. 269 printk(KERN_ERR

  59. 270 "%s: failed to claim resource %d/n",

  60. 271 pdev->dev.bus_id, i);

  61. 272 ret = -EBUSY;

  62. 273 goto failed;

  63. 274 }

  64. 275 }

  65. 276

  66. 277 pr_debug("Registering platform device '%s'. Parent at %s/n",

  67. 278 pdev->dev.bus_id, pdev->dev.parent->bus_id);

  68. 279

  69. 280 ret = device_add(&pdev->dev);

  70. 281 if (ret == 0)

  71. 282 return ret;

  72. 283

  73. 284 failed:

  74. 285 while (--i >= 0)

  75. 286 if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))

  76. 287 release_resource(&pdev->resource[i]);

  77. 288 return ret;

  78. 289}

  79. 290EXPORT_SYMBOL_GPL(platform_device_add);



由platform_device_register和platform_device_add的实现可知,device_register()和platform_device_register()都会首先初始化设备
区别在于第二步:其实platform_device_add()包括device_add(),不过要先注册resources,然后将设备挂接到特定的platform总线。

4    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. 48 struct platform_driver {

  2. 49 int (*probe)(struct platform_device *);

  3. 50 int (*remove)(struct platform_device *);

  4. 51 void (*shutdown)(struct platform_device *);

  5. 52 int (*suspend)(struct platform_device *, pm_message_t state);

  6. 53 int (*suspend_late)(struct platform_device *, pm_message_t state);

  7. 54 int (*resume_early)(struct platform_device *);

  8. 55 int (*resume)(struct platform_device *);

  9. 56 struct device_driver driver;

  10. 57};



可见,它包含了设备操作的几个功能函数,同时包含了一个device_driver结构,说明device_driver是platform_driver的基类。驱动程序中需要初始化这个变量。下面看一下这个变量的定义,位于http://lxr.linux.no/#linux+v2.6.25/include/linux/device.h#L121中:
 

 
  1. 121struct device_driver {

  2. 122 const char *name;

  3. 123 struct bus_type *bus;

  4. 124

  5. 125 struct module *owner;

  6. 126 const char *mod_name; /* used for built-in modules */

  7. 127

  8. 128 int (*probe) (struct device *dev);

  9. 129 int (*remove) (struct device *dev);

  10. 130 void (*shutdown) (struct device *dev);

  11. 131 int (*suspend) (struct device *dev, pm_message_t state);

  12. 132 int (*resume) (struct device *dev);

  13. 133 struct attribute_group **groups;

  14. 134

  15. 135 struct driver_private *p;

  16. 136};



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

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

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

5    driver_register 和platform_driver_register

内核提供的platform_driver结构体的注册函数为platform_driver_register(),其原型定义在http://lxr.linux.no/#linux+v2.6.25/drivers/base/platform.c#L458文件中,具体实现代码如下:

 
  1. 439/**

  2. 440 * platform_driver_register

  3. 441 * @drv: platform driver structure

  4. 442 */

  5. 443int platform_driver_register(struct platform_driver *drv)

  6. 444{

  7. 445 drv->driver.bus = &platform_bus_type;

  8. /*设置成platform_bus_type这个很重要,因为driver和device是通过bus联系在一起的,具体在本例中是通过 platform_bus_type中注册的回调例程和属性来是实现的, driver与device的匹配就是通过 platform_bus_type注册的回调例程platform_match ()来完成的。*/

  9.  
  10. 446 if (drv->probe)

  11. 447 drv->driver.probe = platform_drv_probe;

  12. //在really_probe函数中,回调了platform_drv_probe函数

  13.  
  14. 448 if (drv->remove)

  15. 449 drv->driver.remove = platform_drv_remove;

  16. 450 if (drv->shutdown)

  17. 451 drv->driver.shutdown = platform_drv_shutdown;

  18. 452 if (drv->suspend)

  19. 453 drv->driver.suspend = platform_drv_suspend;

  20. 454 if (drv->resume)

  21. 455 drv->driver.resume = platform_drv_resume;

  22. 456 return driver_register(&drv->driver);

  23. 457}

  24. 458EXPORT_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://lxr.linux.no/#linux+v2.6.25/drivers/base/driver.c#L234中,实现了driver_register()函数。
 

 
  1. 209/**

  2. 210 * driver_register - register driver with bus

  3. 211 * @drv: driver to register

  4. 212 *

  5. 213 * We pass off most of the work to the bus_add_driver() call,

  6. 214 * since most of the things we have to do deal with the bus

  7. 215 * structures.

  8. 216 */

  9. 217int driver_register(struct device_driver *drv)

  10. 218{

  11. 219 int ret;

  12. 220

  13. //如果总线的方法和设备自己的方法同时存在,将打印告警信息,对于platform bus,其没有probe等接口

  14. 221 if ((drv->bus->probe && drv->probe) ||

  15. 222 (drv->bus->remove && drv->remove) ||

  16. 223 (drv->bus->shutdown && drv->shutdown))

  17. 224 printk(KERN_WARNING "Driver '%s' needs updating - please use "

  18. 225 "bus_type methods/n", drv->name);

  19. 226 ret = bus_add_driver(drv);

  20. 227 if (ret)

  21. 228 return ret;

  22. 229 ret = driver_add_groups(drv, drv->groups);

  23. 230 if (ret)

  24. 231 bus_remove_driver(drv);

  25. 232 return ret;

  26. 233}

  27. 234EXPORT_SYMBOL_GPL(driver_register);



226        其主要将驱动挂接到总线上,通过总线来驱动设备。
 

 
  1. 644/**

  2. 645 * bus_add_driver - Add a driver to the bus.

  3. 646 * @drv: driver.

  4. 647 */

  5. 648int bus_add_driver(struct device_driver *drv)

  6. 649{

  7. 650 struct bus_type *bus;

  8. 651 struct driver_private *priv;

  9. 652 int error = 0;

  10. 653

  11. 654 bus = bus_get(drv->bus);

  12. 655 if (!bus)

  13. 656 return -EINVAL;

  14. 657

  15. 658 pr_debug("bus: '%s': add driver %s/n", bus->name, drv->name);

  16. 659

  17. 660 priv = kzalloc(sizeof(*priv), GFP_KERNEL);

  18. 661 if (!priv) {

  19. 662 error = -ENOMEM;

  20. 663 goto out_put_bus;

  21. 664 }

  22. 665 klist_init(&priv->klist_devices, NULL, NULL);

  23. 666 priv->driver = drv;

  24. 667 drv->p = priv;

  25. 668 priv->kobj.kset = bus->p->drivers_kset;

  26. 669 error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,

  27. 670 "%s", drv->name);

  28. 671 if (error)

  29. 672 goto out_unregister;

  30. 673

  31. 674 if (drv->bus->p->drivers_autoprobe) {

  32. 675 error = driver_attach(drv);

  33. 676 if (error)

  34. 677 goto out_unregister;

  35. 678 }

  36. 679 klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);

  37. 680 module_add_driver(drv->owner, drv);

  38. 681

  39. 682 error = driver_create_file(drv, &driver_attr_uevent);

  40. 683 if (error) {

  41. 684 printk(KERN_ERR "%s: uevent attr (%s) failed/n",

  42. 685 __FUNCTION__, drv->name);

  43. 686 }

  44. 687 error = driver_add_attrs(bus, drv);

  45. 688 if (error) {

  46. 689 /* How the hell do we get out of this pickle? Give up */

  47. 690 printk(KERN_ERR "%s: driver_add_attrs(%s) failed/n",

  48. 691 __FUNCTION__, drv->name);

  49. 692 }

  50. 693 error = add_bind_files(drv);

  51. 694 if (error) {

  52. 695 /* Ditto */

  53. 696 printk(KERN_ERR "%s: add_bind_files(%s) failed/n",

  54. 697 __FUNCTION__, drv->name);

  55. 698 }

  56. 699

  57. 700 kobject_uevent(&priv->kobj, KOBJ_ADD);

  58. 701 return error;

  59. 702out_unregister:

  60. 703 kobject_put(&priv->kobj);

  61. 704out_put_bus:

  62. 705 bus_put(bus);

  63. 706 return error;

  64. 707}



如果总线上的driver是自动probe的话,则将该总线上的driver和device绑定起来。
 

 
  1. http://lxr.linux.no/#linux+v2.6.25/drivers/base/dd.c#L285

  2. 272/**

  3. 273 * driver_attach - try to bind driver to devices.

  4. 274 * @drv: driver.

  5. 275 *

  6. 276 * Walk the list of devices that the bus has on it and try to

  7. 277 * match the driver with each one. If driver_probe_device()

  8. 278 * returns 0 and the @dev->driver is set, we've found a

  9. 279 * compatible pair.

  10. 280 */

  11. 281int driver_attach(struct device_driver *drv)

  12. 282{

  13. 283 return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);

  14. 284}

  15. 285EXPORT_SYMBOL_GPL(driver_attach);




扫描该总线上的每一个设备,将当前driver和总线上的设备进行match,如果匹配成功,则将设备和driver绑定起来。
 

 
  1. 246static int __driver_attach(struct device *dev, void *data)

  2. 247{

  3. 248 struct device_driver *drv = data;

  4. 249

  5. 250 /*

  6. 251 * Lock device and try to bind to it. We drop the error

  7. 252 * here and always return 0, because we need to keep trying

  8. 253 * to bind to devices and some drivers will return an error

  9. 254 * simply if it didn't support the device.

  10. 255 *

  11. 256 * driver_probe_device() will spit a warning if there

  12. 257 * is an error.

  13. 258 */

  14. 259

  15. 260 if (dev->parent) /* Needed for USB */

  16. 261 down(&dev->parent->sem);

  17. 262 down(&dev->sem);

  18. 263 if (!dev->driver)

  19. 264 driver_probe_device(drv, dev);

  20. 265 up(&dev->sem);

  21. 266 if (dev->parent)

  22. 267 up(&dev->parent->sem);

  23. 268

  24. 269 return 0;

  25. 270}



263,如果该设备尚没有匹配的driver,则尝试匹配。
 

 
  1. http://lxr.linux.no/#linux+v2.6.25/drivers/base/dd.c#L187

  2. 170/**

  3. 171 * driver_probe_device - attempt to bind device & driver together

  4. 172 * @drv: driver to bind a device to

  5. 173 * @dev: device to try to bind to the driver

  6. 174 *

  7. 175 * First, we call the bus's match function, if one present, which should

  8. 176 * compare the device IDs the driver supports with the device IDs of the

  9. 177 * device. Note we don't do this ourselves because we don't know the

  10. 178 * format of the ID structures, nor what is to be considered a match and

  11. 179 * what is not.

  12. 180 *

  13. 181 * This function returns 1 if a match is found, -ENODEV if the device is

  14. 182 * not registered, and 0 otherwise.

  15. 183 *

  16. 184 * This function must be called with @dev->sem held. When called for a

  17. 185 * USB interface, @dev->parent->sem must be held as well.

  18. 186 */

  19. 187int driver_probe_device(struct device_driver *drv, struct device *dev)

  20. 188{

  21. 189 int ret = 0;

  22. 190

  23. 191 if (!device_is_registered(dev))

  24. 192 return -ENODEV;

  25. 193 if (drv->bus->match && !drv->bus->match(dev, drv))

  26. 194 goto done;

  27. 195

  28. 196 pr_debug("bus: '%s': %s: matched device %s with driver %s/n",

  29. 197 drv->bus->name, __FUNCTION__, dev->bus_id, drv->name);

  30. 198

  31. 199 ret = really_probe(dev, drv);

  32. 200

  33. 201done:

  34. 202 return ret;

  35. 203}




193,如果该总线上的设备需要进行匹配,则验证是否匹配。对于platform总线,其匹配过程如下:

 
  1. http://lxr.linux.no/#linux+v2.6.25/drivers/base/platform.c#L555

  2. 542/**

  3. 543 * platform_match - bind platform device to platform driver.

  4. 544 * @dev: device.

  5. 545 * @drv: driver.

  6. 546 *

  7. 547 * Platform device IDs are assumed to be encoded like this:

  8. 548 * "<name><instance>", where <name> is a short description of the type of

  9. 549 * device, like "pci" or "floppy", and <instance> is the enumerated

  10. 550 * instance of the device, like '0' or '42'. Driver IDs are simply

  11. 551 * "<name>". So, extract the <name> from the platform_device structure,

  12. 552 * and compare it against the name of the driver. Return whether they match

  13. 553 * or not.

  14. 554 */

  15. 555static int platform_match(struct device *dev, struct device_driver *drv)

  16. 556{

  17. 557 struct platform_device *pdev;

  18. 558

  19. 559 pdev = container_of(dev, struct platform_device, dev);

  20. 560 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);

  21. 561}




560,简单的进行字符串匹配,这也是我们强调platform_device和platform_driver中的name属性需要一致的原因。

匹配成功后,则调用probe接口。

 
  1. http://lxr.linux.no/#linux+v2.6.25/drivers/base/dd.c#L101

  2. 98static atomic_t probe_count = ATOMIC_INIT(0);

  3. 99static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);

  4. 100

  5. 101static int really_probe(struct device *dev, struct device_driver *drv)

  6. 102{

  7. 103 int ret = 0;

  8. 104

  9. 105 atomic_inc(&probe_count);

  10. 106 pr_debug("bus: '%s': %s: probing driver %s with device %s/n",

  11. 107 drv->bus->name, __FUNCTION__, drv->name, dev->bus_id);

  12. 108 WARN_ON(!list_empty(&dev->devres_head));

  13. 109

  14. 110 dev->driver = drv;

  15. 111 if (driver_sysfs_add(dev)) {

  16. 112 printk(KERN_ERR "%s: driver_sysfs_add(%s) failed/n",

  17. 113 __FUNCTION__, dev->bus_id);

  18. 114 goto probe_failed;

  19. 115 }

  20. 116

  21. 117 if (dev->bus->probe) {

  22. 118 ret = dev->bus->probe(dev);

  23. 119 if (ret)

  24. 120 goto probe_failed;

  25. 121 } else if (drv->probe) {

  26. 122 ret = drv->probe(dev);

  27. 123 if (ret)

  28. 124 goto probe_failed;

  29. 125 }

  30. 126

  31. 127 driver_bound(dev);

  32. 128 ret = 1;

  33. 129 pr_debug("bus: '%s': %s: bound device %s to driver %s/n",

  34. 130 drv->bus->name, __FUNCTION__, dev->bus_id, drv->name);

  35. 131 goto done;

  36. 132

  37. 133probe_failed:

  38. 134 devres_release_all(dev);

  39. 135 driver_sysfs_remove(dev);

  40. 136 dev->driver = NULL;

  41. 137

  42. 138 if (ret != -ENODEV && ret != -ENXIO) {

  43. 139 /* driver matched but the probe failed */

  44. 140 printk(KERN_WARNING

  45. 141 "%s: probe of %s failed with error %d/n",

  46. 142 drv->name, dev->bus_id, ret);

  47. 143 }

  48. 144 /*

  49. 145 * Ignore errors returned by ->probe so that the next driver can try

  50. 146 * its luck.

  51. 147 */

  52. 148 ret = 0;

  53. 149done:

  54. 150 atomic_dec(&probe_count);

  55. 151 wake_up(&probe_waitqueue);

  56. 152 return ret;

  57. 153}

  58. 154




如果bus和driver同时具备probe方法,则优先调用总线的probe函数。否则调用device_driver的probe函数,此probe函数是经过各种类型的driver重载的函数,这就实现了利用基类的统一方法来实现不同的功能。对于platform_driver来说,其就是:

 
  1. http://lxr.linux.no/#linux+v2.6.25/drivers/base/platform.c#L394

  2. 394static int platform_drv_probe(struct device *_dev)

  3. 395{

  4. 396 struct platform_driver *drv = to_platform_driver(_dev->driver);

  5. 397 struct platform_device *dev = to_platform_device(_dev);

  6. 398

  7. 399 return drv->probe(dev);

  8. 400}



然后调用特定platform_driver所定义的操作方法,这个是在定义某个platform_driver时静态指定的操作接口。

至此,platform_driver成功挂接到platform bus上了,并与特定的设备实现了绑定,并对设备进行了probe处理。

6    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里实现驱动特定的功能。
 

7    哪些适用于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的根本目的是为了统一管理系统的外设资源,为驱动程序提供统一的接口来访问系统资源,将驱动和资源分离,提高程序的可移植性。

8    基于platform总线的驱动开发流程
基于Platform总线的驱动开发流程如下:
•    定义初始化platform bus
•    定义各种platform devices
•    注册各种platform devices
•    定义相关platform driver
•    注册相关platform driver
•    操作相关设备

 
图 platform机制开发驱动流程

以S3C24xx平台为例,来简单讲述下platform驱动的实现流程。
8.1    初始化platform_bus
Platform总线的初始化是在platform_bus_init()完成的,代码如下:

 
  1. http://lxr.linux.no/#linux+v2.6.25/drivers/base/platform.c#L621

  2. 26struct device platform_bus = {

  3. 27 .bus_id = "platform",

  4. 28};

  5. 29EXPORT_SYMBOL_GPL(platform_bus);

  6.  
  7. 621int __init platform_bus_init(void)

  8. 622{

  9. 623 int error;

  10. 624

  11. 625 error = device_register(&platform_bus);

  12. 626 if (error)

  13. 627 return error;

  14. 628 error = bus_register(&platform_bus_type);

  15. 629 if (error)

  16. 630 device_unregister(&platform_bus);

  17. 631 return error;

  18. 632}




该函数创建了一个名为 “platform”的设备,后续platform的设备都会以此为parent。在sysfs中表示为:所有platform类型的设备都会添加在 platform_bus所代表的目录下,即 /sys/devices/platform下面。
-sh-3.1# ls /sys/devices/platform/   
Fixed MDIO bus.0     fsl-i2c.0            serial8250
fsl-ehci.0           fsl-i2c.1            serial8250.0
fsl-gianfar.0        mpc83xx_spi.0        uevent
fsl-gianfar.1        mpc83xx_wdt.0
fsl-gianfar_mdio.-5  power

-sh-3.1# ls /sys/
block/    class/    firmware/ kernel/   power/    
bus/      devices/  fs/       module/   
-sh-3.1# ls /sys/bus/
i2c/         of_platform/ pci_express/ scsi/        usb/         
mdio_bus/    pci/         platform/    spi/         
-sh-3.1# ls /sys/bus/i2c/
devices/           drivers_autoprobe  uevent             
drivers/           drivers_probe    

-sh-3.1# ls /sys/bus/platform/devices/
Fixed MDIO bus.0/    fsl-gianfar_mdio.-5/ mpc83xx_wdt.0/
fsl-ehci.0/          fsl-i2c.0/           serial8250/
fsl-gianfar.0/       fsl-i2c.1/           serial8250.0/
fsl-gianfar.1/       mpc83xx_spi.0/       
-sh-3.1# ls /sys/bus/platform/drivers 
drivers/           drivers_autoprobe  drivers_probe      
-sh-3.1# ls /sys/bus/platform/drivers/
fsl-ehci/         fsl-gianfar_mdio/ mpc83xx_spi/      serial8250/
fsl-gianfar/      fsl-i2c/          mpc83xx_wdt/     

platform_bus必须在系统注册任何platform driver和platform device之前初始化,那么这是如何实现的呢?

http://lxr.linux.no/#linux+v2.6.25/drivers/base/init.c
 

 
  1. 14/**

  2. 15 * driver_init - initialize driver model.

  3. 16 *

  4. 17 * Call the driver model init functions to initialize their

  5. 18 * subsystems. Called early from init/main.c.

  6. 19 */

  7. 20void __init driver_init(void)

  8. 21{

  9. 22 /* These are the core pieces */

  10. 23 devices_init();

  11. 24 buses_init();

  12. 25 classes_init();

  13. 26 firmware_init();

  14. 27 hypervisor_init();

  15. 28

  16. 29 /* These are also core pieces, but must come after the

  17. 30 * core core pieces.

  18. 31 */

  19. 32 platform_bus_init();

  20. 33 system_bus_init();

  21. 34 cpu_dev_init();

  22. 35 memory_dev_init();

  23. 36}



init/main.c
start_kernel  》 rest_init  》 kernel_init  》 do_basic_setup》driver_init 》platform_bus_init

http://lxr.linux.no/#linux+v2.6.25/drivers/base/init.c#L32

 
  1. 724/*

  2. 725 * Ok, the machine is now initialized. None of the devices

  3. 726 * have been touched yet, but the CPU subsystem is up and

  4. 727 * running, and memory and process management works.

  5. 728 *

  6. 729 * Now we can finally start doing some real work..

  7. 730 */

  8. 731static void __init do_basic_setup(void)

  9. 732{

  10. 733 /* drivers will send hotplug events */

  11. 734 init_workqueues();

  12. 735 usermodehelper_init();

  13. 736 driver_init();

  14. 737 init_irq_proc();

  15. 738 do_initcalls();

  16. 739}



platform driver和platform device的初始化是在do_initcalls中进行的。

8.2    定义platform_device
http://lxr.linux.no/#linux+v2.6.25/arch/arm/plat-s3c24xx/devs.c#L276中定义了系统的资源,是一个高度可移植的文件,大部分板级资源都在这里集中定义。
 

 
  1. 274/* I2C */

  2. 275

  3. 276static struct resource s3c_i2c_resource[] = {

  4. 277 [0] = {

  5. 278 .start = S3C24XX_PA_IIC,

  6. 279 .end = S3C24XX_PA_IIC + S3C24XX_SZ_IIC - 1,

  7. 280 .flags = IORESOURCE_MEM,

  8. 281 },

  9. 282 [1] = {

  10. 283 .start = IRQ_IIC,

  11. 284 .end = IRQ_IIC,

  12. 285 .flags = IORESOURCE_IRQ,

  13. 286 }

  14. 287

  15. 288};

  16. 289

  17. 290struct platform_device s3c_device_i2c = {

  18. 291 .name = "s3c2410-i2c",

  19. 292 .id = -1,

  20. 293 .num_resources = ARRAY_SIZE(s3c_i2c_resource),

  21. 294 .resource = s3c_i2c_resource,

  22. 295};

  23. 296

  24. 297EXPORT_SYMBOL(s3c_device_i2c);




设备名称为s3c2410-i2c,“-1”只有一个i2c设备,两个资源s3c_i2c_resource,分别为i2c控制器的寄存器空间和中断信息。

8.3    注册platform_device

定义了platform_device后,需要添加到系统中,就可以调用函数platform_add_devices。
http://lxr.linux.no/#linux+v2.6.25/arch/arm/mach-s3c2440/mach-smdk2440.c

smdk2440_devices将系统资源组织起来,统一注册进内核。
 

 
  1. 151static struct platform_device *smdk2440_devices[] __initdata = {

  2. 152 &s3c_device_usb,

  3. 153 &s3c_device_lcd,

  4. 154 &s3c_device_wdt,

  5. 155 &s3c_device_i2c,

  6. 156 &s3c_device_iis,

  7. 157};

  8.  
  9. 166static void __init smdk2440_machine_init(void)

  10. 167{

  11. 168 s3c24xx_fb_set_platdata(&smdk2440_fb_info);

  12. 169

  13. 170 platform_add_devices(smdk2440_devices, ARRAY_SIZE(smdk2440_devices));

  14. 171 smdk_machine_init();

  15. 172}

  16. 173

  17. 174MACHINE_START(S3C2440, "SMDK2440")

  18. 175 /* Maintainer: Ben Dooks <ben@fluff.org> */

  19. 176 .phys_io = S3C2410_PA_UART,

  20. 177 .io_pg_offst = (((u32)S3C24XX_VA_UART) >> 18) & 0xfffc,

  21. 178 .boot_params = S3C2410_SDRAM_PA + 0x100,

  22. 179

  23. 180 .init_irq = s3c24xx_init_irq,

  24. 181 .map_io = smdk2440_map_io,

  25. 182 .init_machine = smdk2440_machine_init,

  26. 183 .timer = &s3c24xx_timer,

  27. 184MACHINE_END




170        platform_add_devices(smdk2440_devices, ARRAY_SIZE(smdk2440_devices));
将系统所有资源注册进系统,在此之前platform bus需要初始化成功,否则无法将platform devices挂接到platform bus上。为了保证platform drive初始化时,相关platform资源已经注册进系统,smdk2440_machine_init需要很早执行,而其作为平台初始化init_machine 时,将优先于系统所有驱动的初始化。

其调用顺序如下:
start_kernel》setup_arch》init_machine》arch_initcall(customize_machine)
http://lxr.linux.no/#linux+v2.6.25/arch/arm/kernel/setup.c#L788

 
  1. 786arch_initcall(customize_machine);

  2. 787

  3. 788void __init setup_arch(char **cmdline_p)

  4. 789{

  5. 790 struct tag *tags = (struct tag *)&init_tags;

  6. 791 struct machine_desc *mdesc;

  7. 792 char *from = default_command_line;

  8. 793

  9. 794 setup_processor();

  10. 795 mdesc = setup_machine(machine_arch_type);

  11. //根据machine id获得移植时定义的machine desc结构

  12. 796 machine_name = mdesc->name;

  13. 797

  14. 798 if (mdesc->soft_reboot)

  15. 799 reboot_setup("s");

  16. 800

  17. 801 if (__atags_pointer)

  18. 802 tags = phys_to_virt(__atags_pointer);

  19. 803 else if (mdesc->boot_params)

  20. 804 tags = phys_to_virt(mdesc->boot_params);

  21. 805

  22. 806 /*

  23. 807 * If we have the old style parameters, convert them to

  24. 808 * a tag list.

  25. 809 */

  26. 810 if (tags->hdr.tag != ATAG_CORE)

  27. 811 convert_to_tag_list(tags);

  28. 812 if (tags->hdr.tag != ATAG_CORE)

  29. 813 tags = (struct tag *)&init_tags;

  30. 814

  31. 815 if (mdesc->fixup)

  32. 816 mdesc->fixup(mdesc, tags, &from, &meminfo);

  33. 817

  34. 818 if (tags->hdr.tag == ATAG_CORE) {

  35. 819 if (meminfo.nr_banks != 0)

  36. 820 squash_mem_tags(tags);

  37. 821 save_atags(tags);

  38. 822 parse_tags(tags);

  39. 823 }

  40. 824

  41. 825 init_mm.start_code = (unsigned long) &_text;

  42. 826 init_mm.end_code = (unsigned long) &_etext;

  43. 827 init_mm.end_data = (unsigned long) &_edata;

  44. 828 init_mm.brk = (unsigned long) &_end;

  45. 829

  46. 830 memcpy(boot_command_line, from, COMMAND_LINE_SIZE);

  47. 831 boot_command_line[COMMAND_LINE_SIZE-1] = '/0';

  48. 832 parse_cmdline(cmdline_p, from);

  49. 833 paging_init(&meminfo, mdesc);

  50. 834 request_standard_resources(&meminfo, mdesc);

  51. 835

  52. 836#ifdef CONFIG_SMP

  53. 837 smp_init_cpus();

  54. 838#endif

  55. 839

  56. 840 cpu_init();

  57. 841

  58. 842 /*

  59. 843 * Set up various architecture-specific pointers

  60. 844 */

  61. 845 init_arch_irq = mdesc->init_irq;

  62. 846 system_timer = mdesc->timer;

  63. 847 init_machine = mdesc->init_machine;

  64. //对init_machine指针赋值

  65. 848

  66. 849#ifdef CONFIG_VT

  67. 850#if defined(CONFIG_VGA_CONSOLE)

  68. 851 conswitchp = &vga_con;

  69. 852#elif defined(CONFIG_DUMMY_CONSOLE)

  70. 853 conswitchp = &dummy_con;

  71. 854#endif

  72. 855#endif

  73. 856}

  74.  
  75. 777static void (*init_machine)(void) __initdata;

  76. 778

  77. 779static int __init customize_machine(void)

  78. 780{

  79. 781 /* customizes platform devices, or adds new ones */

  80. 782 if (init_machine)

  81. 783 init_machine();

  82. 784 return 0;

  83. 785}

  84. 786arch_initcall(customize_machine);



arch_initcall将customize_machine放在特定的段中,系统将在某个地方运行所有的arch_initcall修饰的函数。

http://lxr.linux.no/#linux+v2.6.25/include/linux/init.h#L182

 
  1. 152#ifndef MODULE //非可加载模块,即编译链接进内核的代码

  2. 153

  3. 154#ifndef __ASSEMBLY__

  4. 155

  5. 156/* initcalls are now grouped by functionality into separate

  6. 157 * subsections. Ordering inside the subsections is determined

  7. 158 * by link order.

  8. 159 * For backwards compatibility, initcall() puts the call in

  9. 160 * the device init subsection.

  10. 161 *

  11. 162 * The `id' arg to __define_initcall() is needed so that multiple initcalls

  12. 163 * can point at the same handler without causing duplicate-symbol build errors.

  13. 164 */

  14. 165

  15. 166#define __define_initcall(level,fn,id) /

  16. 167 static initcall_t __initcall_##fn##id __used /

  17. 168 __attribute__((__section__(".initcall" level ".init"))) = fn

  18. 169

  19. 170/*

  20. 171 * A "pure" initcall has no dependencies on anything else, and purely

  21. 172 * initializes variables that couldn't be statically initialized.

  22. 173 *

  23. 174 * This only exists for built-in code, not for modules.

  24. 175 */

  25. 176#define pure_initcall(fn) __define_initcall("0",fn,0)

  26. 177

  27. 178#define core_initcall(fn) __define_initcall("1",fn,1)

  28. 179#define core_initcall_sync(fn) __define_initcall("1s",fn,1s)

  29. 180#define postcore_initcall(fn) __define_initcall("2",fn,2)

  30. 181#define postcore_initcall_sync(fn) __define_initcall("2s",fn,2s)

  31. 182#define arch_initcall(fn) __define_initcall("3",fn,3)

  32. 183#define arch_initcall_sync(fn) __define_initcall("3s",fn,3s)

  33. 184#define subsys_initcall(fn) __define_initcall("4",fn,4)

  34. 185#define subsys_initcall_sync(fn) __define_initcall("4s",fn,4s)

  35. 186#define fs_initcall(fn) __define_initcall("5",fn,5)

  36. 187#define fs_initcall_sync(fn) __define_initcall("5s",fn,5s)

  37. 188#define rootfs_initcall(fn) __define_initcall("rootfs",fn,rootfs)

  38. 189#define device_initcall(fn) __define_initcall("6",fn,6)

  39. 190#define device_initcall_sync(fn) __define_initcall("6s",fn,6s)

  40. 191#define late_initcall(fn) __define_initcall("7",fn,7)

  41. 192#define late_initcall_sync(fn) __define_initcall("7s",fn,7s)

  42. 193

  43. 194#define __initcall(fn) device_initcall(fn)

  44. 195

  45. 196#define __exitcall(fn) /

  46. 197 static exitcall_t __exitcall_##fn __exit_call = fn

  47. 198

  48. 。。。。。。。。。

  49. 239#endif /* __ASSEMBLY__ */

  50. 240

  51. 241/**

  52. 242 * module_init() - driver initialization entry point

  53. 243 * @x: function to be run at kernel boot time or module insertion

  54. 244 *

  55. 245 * module_init() will either be called during do_initcalls() (if

  56. 246 * builtin) or at module insertion time (if a module). There can only

  57. 247 * be one per module.

  58. 248 */

  59. 249#define module_init(x) __initcall(x);

  60. 250

  61. 251/**

  62. 252 * module_exit() - driver exit entry point

  63. 253 * @x: function to be run when driver is removed

  64. 254 *

  65. 255 * module_exit() will wrap the driver clean-up code

  66. 256 * with cleanup_module() when used with rmmod when

  67. 257 * the driver is a module. If the driver is statically

  68. 258 * compiled into the kernel, module_exit() has no effect.

  69. 259 * There can only be one per module.

  70. 260 */

  71. 261#define module_exit(x) __exitcall(x);

  72. 262

  73. 263#else /* MODULE */



各种xx_core_initcall被定义到了不同的分级的段中
所以arch_initcall == __initcall_fn3 它将被链接器放于section  .initcall3.init. 中

module_init()==__initcall(fn)==device_initcall(fn)== __initcall_fn6

各个段的优先级由链接脚本定义
http://lxr.linux.no/#linux+v2.6.25/include/asm-generic/vmlinux.lds.h#L328

 
  1. #define INITCALLS /

  2. *(.initcall0.init) /

  3. *(.initcall0s.init) /

  4. *(.initcall1.init) /

  5. *(.initcall1s.init) /

  6. *(.initcall2.init) /

  7. *(.initcall2s.init) /

  8. *(.initcall3.init) /

  9. *(.initcall3s.init) /

  10. *(.initcall4.init) /

  11. *(.initcall4s.init) /

  12. *(.initcall5.init) /

  13. *(.initcall5s.init) /

  14. *(.initcallrootfs.init) /

  15. *(.initcall6.init) /

  16. *(.initcall6s.init) /

  17. *(.initcall7.init) /

  18. *(.initcall7s.init)

  19.  
  20. 这个__initcall_start是在文件arch/xxx/kernel/vmlinux.lds.S定义的:

  21. __initcall_start = .;

  22. INITCALLS

  23. __initcall_end = .;

  24.  
  25. http://lxr.linux.no/#linux+v2.6.25/init/main.c#L664

  26. 664static void __init do_initcalls(void)

  27. 665{

  28. 666 initcall_t *call;

  29. 667 int count = preempt_count();

  30. 668

  31. 669 for (call = __initcall_start; call < __initcall_end; call++) {

  32. .。。。。

  33. 682

  34. 683 result = (*call)();

  35. 684

  36. 。。。 }

  37. 720 /* Make sure there is no pending stuff from the initcall sequence */

  38. 721 flush_scheduled_work();

  39. 722}



因此__initcall_fnx,数字越小,越先被调用,故arch_initcall优先于module_init所修饰的函数。

arch_initcall修饰的函数的调用顺序如下:
start_kernel  》 rest_init(在setup_arch之后)  》 kernel_init  》 do_basic_setup》do_initcalls(在driver_init()之后) ,因为platform_bus_init在此之前已经初始化完毕了,便可将设备挂接到总线上了。

8.4    定义platform_driver
Platform bus和设备都定义好了后,需要定义一个platform driver用来驱动此设备。

对于设备来说:

 
  1. 290struct platform_device s3c_device_i2c = {

  2. 291 .name = "s3c2410-i2c",

  3. 292 .id = -1,

  4. 293 .num_resources = ARRAY_SIZE(s3c_i2c_resource),

  5. 294 .resource = s3c_i2c_resource,

  6. 295};

  7. 296

  8. 297EXPORT_SYMBOL(s3c_device_i2c);




根据platform总线上device和driver的匹配规则可知,I2C 的platform driver的名字是s3c2410-i2c。

http://lxr.linux.no/#linux+v2.6.25/drivers/i2c/busses/i2c-s3c2410.c#L1

 
  1. 903/* device driver for platform bus bits */

  2. 904

  3. 905static struct platform_driver s3c2410_i2c_driver = {

  4. 906 .probe = s3c24xx_i2c_probe,

  5. 907 .remove = s3c24xx_i2c_remove,

  6. 908 .resume = s3c24xx_i2c_resume,

  7. 909 .driver = {

  8. 910 .owner = THIS_MODULE,

  9. 911 .name = "s3c2410-i2c",

  10. 912 },

  11. 913};



8.5    注册platform_driver
http://lxr.linux.no/#linux+v2.6.25/drivers/i2c/busses/i2c-s3c2410.c#L1
 

 
  1. 925static int __init i2c_adap_s3c_init(void)

  2. 926{

  3. 927 int ret;

  4. 928

  5. 929 ret = platform_driver_register(&s3c2410_i2c_driver);

  6. 930 if (ret == 0) {

  7. 931 ret = platform_driver_register(&s3c2440_i2c_driver);

  8. 932 if (ret)

  9. 933 platform_driver_unregister(&s3c2410_i2c_driver);

  10. 934 }

  11. 935

  12. 936 return ret;

  13. 937}

  14. 938

  15.  
  16. 945module_init(i2c_adap_s3c_init);

  17. 946module_exit(i2c_adap_s3c_exit);



在i2c_adap_s3c_init中注册s3c2410_i2c_driver,那么i2c_adap_s3c_init何时执行的呢?module_init(i2c_adap_s3c_init)表明其存放在initcall段,调用顺序如下:
init/main.c
start_kernel  》 rest_init  》 kernel_init  》 do_basic_setup》do_initcalls,因为platform_bus_init在此之前已经初始化完毕了,且设备已经注册到内核中了,驱动将和内核绑定,并最终调用s3c24xx_i2c_probe。
 

 
  1. 748/* s3c24xx_i2c_probe

  2. 749 *

  3. 750 * called by the bus driver when a suitable device is found

  4. 751*/

  5. 752

  6. 753static int s3c24xx_i2c_probe(struct platform_device *pdev)

  7. 754{

  8. 755 struct s3c24xx_i2c *i2c = &s3c24xx_i2c;

  9. 756 struct resource *res;

  10. 757 int ret;

  11. 758

  12. 759 /* find the clock and enable it */

  13. 760

  14. 761 i2c->dev = &pdev->dev;

  15. 762 i2c->clk = clk_get(&pdev->dev, "i2c");

  16. 763 if (IS_ERR(i2c->clk)) {

  17. 764 dev_err(&pdev->dev, "cannot get clock/n");

  18. 765 ret = -ENOENT;

  19. 766 goto err_noclk;

  20. 767 }

  21. 768

  22. 769 dev_dbg(&pdev->dev, "clock source %p/n", i2c->clk);

  23. 770

  24. 771 clk_enable(i2c->clk);

  25. 772

  26. 773 /* map the registers */

  27. 774

  28. 775 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

  29. 776 if (res == NULL) {

  30. 777 dev_err(&pdev->dev, "cannot find IO resource/n");

  31. 778 ret = -ENOENT;

  32. 779 goto err_clk;

  33. 780 }

  34. 781

  35. 782 i2c->ioarea = request_mem_region(res->start, (res->end-res->start)+1,

  36. 783 pdev->name);

  37. 784

  38. 785 if (i2c->ioarea == NULL) {

  39. 786 dev_err(&pdev->dev, "cannot request IO/n");

  40. 787 ret = -ENXIO;

  41. 788 goto err_clk;

  42. 789 }

  43. 790

  44. 791 i2c->regs = ioremap(res->start, (res->end-res->start)+1);

  45. 792

  46. 793 if (i2c->regs == NULL) {

  47. 794 dev_err(&pdev->dev, "cannot map IO/n");

  48. 795 ret = -ENXIO;

  49. 796 goto err_ioarea;

  50. 797 }

  51. 798

  52. 799 dev_dbg(&pdev->dev, "registers %p (%p, %p)/n", i2c->regs, i2c->ioarea, res);

  53. 800

  54. 801 /* setup info block for the i2c core */

  55. 802

  56. 803 i2c->adap.algo_data = i2c;

  57. 804 i2c->adap.dev.parent = &pdev->dev;

  58. 805

  59. 806 /* initialise the i2c controller */

  60. 807

  61. 808 ret = s3c24xx_i2c_init(i2c);

  62. 809 if (ret != 0)

  63. 810 goto err_iomap;

  64. 811

  65. 812 /* find the IRQ for this unit (note, this relies on the init call to

  66. 813 * ensure no current IRQs pending

  67. 814 */

  68. 815

  69. 816 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);

  70. 817 if (res == NULL) {

  71. 818 dev_err(&pdev->dev, "cannot find IRQ/n");

  72. 819 ret = -ENOENT;

  73. 820 goto err_iomap;

  74. 821 }

  75. 822

  76. 823 ret = request_irq(res->start, s3c24xx_i2c_irq, IRQF_DISABLED,

  77. 824 pdev->name, i2c);

  78. 825

  79. 826 if (ret != 0) {

  80. 827 dev_err(&pdev->dev, "cannot claim IRQ/n");

  81. 828 goto err_iomap;

  82. 829 }

  83. 830

  84. 831 i2c->irq = res;

  85. 832

  86. 833 dev_dbg(&pdev->dev, "irq resource %p (%lu)/n", res,

  87. 834 (unsigned long)res->start);

  88. 835

  89. 836 ret = i2c_add_adapter(&i2c->adap);

  90. 837 if (ret < 0) {

  91. 838 dev_err(&pdev->dev, "failed to add bus to i2c core/n");

  92. 839 goto err_irq;

  93. 840 }

  94. 841

  95. 842 platform_set_drvdata(pdev, i2c);

  96. 843

  97. 844 dev_info(&pdev->dev, "%s: S3C I2C adapter/n", i2c->adap.dev.bus_id);

  98. 845 return 0;

  99. 846

  100. 847 err_irq:

  101. 848 free_irq(i2c->irq->start, i2c);

  102. 849

  103. 850 err_iomap:

  104. 851 iounmap(i2c->regs);

  105. 852

  106. 853 err_ioarea:

  107. 854 release_resource(i2c->ioarea);

  108. 855 kfree(i2c->ioarea);

  109. 856

  110. 857 err_clk:

  111. 858 clk_disable(i2c->clk);

  112. 859 clk_put(i2c->clk);

  113. 860

  114. 861 err_noclk:

  115. 862 return ret;

  116. 863}




当进入probe函数后,需要获取设备的资源信息,常用获取资源的函数主要是:
struct resource * platform_get_resource(struct platform_device *dev, unsigned int type, unsigned int num);
根据参数type所指定类型,例如IORESOURCE_MEM,来获取指定的资源。
struct int platform_get_irq(struct platform_device *dev, unsigned int num);
获取资源中的中断号。
struct resource * platform_get_resource_byname(struct platform_device *dev, unsigned int type, char *name);
根据参数name所指定的名称,来获取指定的资源。
int platform_get_irq_byname(struct platform_device *dev, char *name);
根据参数name所指定的名称,来获取资源中的中断号。

此probe函数获取物理IO空间,通过request_mem_region和ioremap等操作物理地址转换成内核中的虚拟地址,初始化I2C控制器,通过platform_get_irq或platform_get_resource得到设备的中断号以后,就可以调用request_irq函数来向系统注册中断,并将此I2C控制器添加到系统中。

8.6    操作设备
进行了platform_device_register 和platform_driver_register后,驱动的相应信息就出现在sys目录的相应文件夹下,然后,我们该如何调用设备呢??怎么对设备进行打开读写等操作呢???

Platform总线只是为了方便管理挂接在CPU总线上的设备,与用户空间的交互,如读写还是需要利用file_operations。当然如果此platform设备无需和用户空间交互,则无需file_operations实例。

对于I2C总线来说,其file_operations如下:
http://lxr.linux.no/#linux+v2.6.25/drivers/i2c/i2c-core.c#L461
 

 
  1. 478static const struct file_operations i2cdev_fops = {

  2. 479 .owner = THIS_MODULE,

  3. 480 .llseek = no_llseek,

  4. 481 .read = i2cdev_read,

  5. 482 .write = i2cdev_write,

  6. 483 .ioctl = i2cdev_ioctl,

  7. 484 .open = i2cdev_open,

  8. 485 .release = i2cdev_release,

  9. 486};



其和platform bus的区别在于,platform bus提供机制访问I2C 控制器本身的资源,而I2C总线提供访问I2C 控制器上挂接的I2C设备

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值