Platform是一种虚拟总线,Platform机制将设备本身的资源注册进内核,有内核统一管理,在驱动程序使用这些资源时使用统一的接口,这样提高了程序的可移植性。

Linux的大部分设备驱动都可以使用platform 机制,用platform device 表示设备,用platform driver 表示驱动。

Platform总线的定义如下:

struct bus_type platform_bus_type = {
  .name = "platform",//总线名字,总线注册后新建目录sys/bus/platform
  .dev_groups = platform_dev_groups,
  .match = platform_match,//当有总线或者设备注册到platform总线时,内核自动调用match函数,判断设备和驱动的name是否一致。
  .uevent = platform_uevent,
  .pm = &platform_dev_pm_ops,
};

Platform device定义如下:

struct platform_device {
  const char *name;//device名字,应该与platform driver对应。注册后,会在/sys/device/目录下创建一个以name命名的目录,并且创建软连接到/sys/bus/platform/device下。
  int id;
  bool id_auto;
  struct device dev;//基本的device结构
  u32 num_resources;//资源数
  struct resource *resource;//资源

  const struct platform_device_id *id_entry;
  char *driver_override; /* Driver name to force a match */

  /* MFD cell pointer */
  struct mfd_cell *mfd_cell;

  /* arch specific additions */
  struct pdev_archdata archdata;
};

Platform driver定义如下:

struct platform_driver {
  int (*probe)(struct platform_device *);//platform driver注册时,如果总线上已有匹配的device,将调用probe函数。
  int (*remove)(struct platform_device *);
  void (*shutdown)(struct platform_device *);
  int (*suspend)(struct platform_device *, pm_message_t state);
  int (*resume)(struct platform_device *);
  struct device_driver driver;//基本的device driver结构.platform driver注册成功后,/sys/bus/platform/driver/目录下创建一个名字为driver->name的目录
  const struct platform_device_id *id_table;
  bool prevent_deferred_probe;
};

 Platform device 注册过程:

int platform_device_register(struct platform_device *pdev)
{
  device_initialize(&pdev->dev);//初始化platform device结构体中的dev结构
  arch_setup_pdev_archdata(pdev);
  return platform_device_add(pdev);//add platform到platform bus.
}

int platform_device_add(struct platform_device *pdev)
{
  int i, ret;

  if (!pdev)
    return -EINVAL;

  if (!pdev->dev.parent)
    pdev->dev.parent = &platform_bus;//dev的parent device为platform_bus

  pdev->dev.bus = &platform_bus_type;//dev的bus 类型

  switch (pdev->id) {//设置dev 名字。
  default:
    dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
  break;
  case PLATFORM_DEVID_NONE:
    dev_set_name(&pdev->dev, "%s", pdev->name);
  break;
  case PLATFORM_DEVID_AUTO:
/*
* Automatically allocated device ID. We mark it as such so
* that we remember it must be freed, and we append a suffix
* to avoid namespace collision with explicit IDs.
*/
    ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
    if (ret < 0)
      goto err_out;
    pdev->id = ret;
    pdev->id_auto = true;
    dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
    break;
  }

  for (i = 0; i < pdev->num_resources; i++) {//添加resource
    struct resource *p, *r = &pdev->resource[i];

    if (r->name == NULL)
    r->name = dev_name(&pdev->dev);

    p = r->parent;
    if (!p) {
      if (resource_type(r) == IORESOURCE_MEM)
      p = &iomem_resource;
      else if (resource_type(r) == IORESOURCE_IO)
      p = &ioport_resource;
    }

    if (p && insert_resource(p, r)) {
      dev_err(&pdev->dev, "failed to claim resource %d\n", i);
      ret = -EBUSY;
      goto failed;
    }
  }

  pr_debug("Registering platform device '%s'. Parent at %s\n",
  dev_name(&pdev->dev), dev_name(pdev->dev.parent));

  ret = device_add(&pdev->dev);//add dev到bus
  if (ret == 0)
  return ret;

failed:
  if (pdev->id_auto) {
    ida_simple_remove(&platform_devid_ida, pdev->id);
    pdev->id = PLATFORM_DEVID_AUTO;
  }

  while (--i >= 0) {
    struct resource *r = &pdev->resource[i];
    if (r->parent)
    release_resource(r);
  }

err_out:
  return ret;
}

在device_add函数中调用bus_add_device函数将device加到bus,并调用bus_probe_device来调用总线上与之匹配的driver的probe函数。

在bus_probe_device函数中主要调用device_initial_probe,

void device_initial_probe(struct device *dev)
{
  __device_attach(dev, true);
}

在__device_attach中主要的代码如下:

ret = bus_for_each_drv(dev->bus, NULL, &data,
__device_attach_driver);//遍历总线上的driver,并调用__device_attach_driver

在__device_attach_driver中主要调用driver_match_device判断driver和device是否match,如果match则调用driver_probe_device来probe driver

static inline int driver_match_device(struct device_driver *drv,
struct device *dev)
{
  return drv->bus->match ? drv->bus->match(dev, drv) : 1;//drv所属bus的match函数。
}

bus->match即platform bus的match 函数:

static int platform_match(struct device *dev, struct device_driver *drv)
{
  struct platform_device *pdev = to_platform_device(dev);
  struct platform_driver *pdrv = to_platform_driver(drv);

  /* When driver_override is set, only bind to the matching driver */
  if (pdev->driver_override)
    return !strcmp(pdev->driver_override, drv->name);

  /* Attempt an OF style match first */
  if (of_driver_match_device(dev, drv))
  return 1;

  /* Then try ACPI style match */
  if (acpi_driver_match_device(dev, drv))
  return 1;

  /* Then try to match against the id table */
  if (pdrv->id_table)
    return platform_match_id(pdrv->id_table, pdev) != NULL;

  /* fall-back to driver name match */
  return (strcmp(pdev->name, drv->name) == 0);//比较driver和device的名字。
}

在driver_probe_device中主要调用really_probe,really_probe的主要代码如下。

if (dev->bus->probe) {//如果bus定义了probe,就调用bus的probe
  ret = dev->bus->probe(dev);
  if (ret)
  goto probe_failed;
} else if (drv->probe) {//调用bus所属driver的probe函数。
  ret = drv->probe(dev);
  if (ret)
  goto probe_failed;
}

 Platform driver的注册过程:

int __platform_driver_register(struct platform_driver *drv,
struct module *owner)
{
  drv->driver.owner = owner;
  drv->driver.bus = &platform_bus_type;//driver的bus type为Platform bus
  drv->driver.probe = platform_drv_probe;//driver的probe函数,如果platform driver定义了probe函数,里面调用的platform定义的probe
  drv->driver.remove = platform_drv_remove;
  drv->driver.shutdown = platform_drv_shutdown;

  return driver_register(&drv->driver);//主要注册函数。
}

在driver_register函数中,通过driver_find查找driver是否已经注册,如果没有注册,则调用bus_add_driver将driver add到bus。

在bus_add_driver最主要的操作是driver 和匹配的device进行绑定。

int driver_attach(struct device_driver *drv)
{
  return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);//遍历bus上所有的device,并调用__driver_attach
}

在__driver_attach函数中与上面__device_attach_driver过程类似,调用driver_match_device判断driver和device是否match,如果match则调用driver_probe_device来probe driver。如果bus上没有注册过与platform driver匹配的platform device,那platform driver的probe函数将不会调用到。

从上述注册过程可以看出platform device和platform driver注册并不需要区分先后。

设备挂接到总线上时,与总线上的所有驱动进行匹配(用bus_type.match进行匹配)。如果匹配成功,则调用bus_type.probe或者driver.probe初始化该设备;挂接到总线上如果匹配失败,则只是将该设备挂接到总线上。

驱动挂接到总线上时,与总线上的所有设备进行匹配(用bus_type.match进行匹配)。如果匹配成功,则调用bus_type.probe或者driver.probe初始化该设备;挂接到总线上如果匹配失败,则只是将该驱动挂接到总线上。

Platform device/driver注册过程的更多相关文章

  1. [platform]linux platform device/driver(二)--Platform Device和Platform_driver注册过程之详细代码

    转自:http://www.cnblogs.com/haimeng2010/p/3582403.html 目录: 1.platform_device注册过程 2.platform_driver注册过程 ...

  2. [platform]linux platform device/driver(一)--Driver是如何找到对应的device

    1.platform device是怎么"自动"关联到platform driver上的? 转向linux driver有些时间了,前段时间碰到个问题,在Linux kernel ...

  3. [platform]linux platform device/driver(三)--Platform Device和Platform_driver注册过程之代码对比

    转自:http://blog.csdn.net/thl789/article/details/6723350 Linux 2.6的设备驱动模型中,所有的device都是通过Bus相连.device_r ...

  4. [platform]Device和Driver注册顺序

    1. 设备和驱动注册,无论谁先谁后,都可以通过查询总线进行匹配 设备挂接到总线上时,与总线上的所有驱动进行匹配(用bus_type.match进行匹配),如果匹配成功,则调用bus_type.prob ...

  5. linux 内核驱动--Platform Device和Platform_driver注册过程

    linux 内核驱动--Platform Device和Platform_driver注册过程 从 Linux 2.6 起引入了一套新的驱动管理和注册机制 :Platform_device 和 Pla ...

  6. Linux Platform Device and Driver

    从 Linux 2.6 起引入了一套新的驱动管理和注册机制 :Platform_device 和 Platform_driver . Linux 中大部分的设备驱动,都可以使用这套机制 , 设备用 P ...

  7. Samsung_tiny4412(驱动笔记10)----mdev,bus,device,driver,platform

    /*********************************************************************************** * * mdev,bus,de ...

  8. 从串口驱动的移植看linux2.6内核中的驱动模型 platform device & platform driver【转】

    转自:http://blog.csdn.net/bonnshore/article/details/7979705 写在前面的话: 博主新开了个人站点:你也可以在这里看到这篇文章,点击打开链接 本文是 ...

  9. platform device和platform driver简述

    首先我们在module_init中使用platform_driver_register来注册我们的驱动.一般来说platform_driver_register放在module_init的最后调用,因 ...

随机推荐

  1. 全栈高级web前端工程师的必经之路

    这是最近整理的一个进阶高级web工程师的技术栈 同时也是激励自己每天来看一下离真正的王者还有多少距离! 过段时间再来反思一下自己进步了多少? ------------------------20190 ...

  2. C# Timer 控件的用法

    一.主要的属性 在 Windows 窗体应用程序中,定时器控件(Timer)与其他的控件略有不同,它并不直接显示在窗体上,而是与其他控件连用. Enabled 属性: 用于设置该Timer控件是否可用 ...

  3. 浅谈C#委托的用法-delegate

    2018年11月7日     小雨 一.委托的概念 委托和类一样是一种用户自定义类型,它存储的就是一系列具有相同签名和返回类型的方法的地址,调用委托的时候,它所包含的所有方法都会被执行. 借用百度上的 ...

  4. Servlet与idea

    1.idea会为每一个Tomcat部署的项目,独立建一份配置文件. 配置文件所在位置 怎么部署的?使用的第三种部署方式(查看虚拟目录) docBase部署项目存放的路径 项目目录和Tomcat部署目录 ...

  5. python面试的100题(16)

    Python高级 元类 42.Python中类方法.类实例方法.静态方法有何区别? 类方法: 是类对象的方法,在定义时需要在上方使用 @classmethod 进行装饰,形参为cls,表示类对象,类对 ...

  6. 导入org.apache.poi.xssf 读取excel

    POI 操作 excel  用XSSF 方式时,如果不能自动导入 org.apache.poi.xssf 对应jar 包,则可以Apache 官网进行下载,自行导入. step1: 访问 http:/ ...

  7. 剑指offer 62. 二叉搜索树的第 k 个结点

    62. 二叉搜索树的第 k 个结点 题目描述 给定一棵二叉搜索树,请找出其中的第k小的结点.例如, (5,3,7,2,4,6,8)    中,按结点数值大小顺序第三小结点的值为4. 法一: 非递归中序 ...

  8. PVE上安装黑裙辉6.2

    参考文章:https://post.smzdm.com/p/a25r8mo2/ http://www.myxzy.com/post-488.html 环境介绍 1.Proxmox VE(以下简称PVE ...

  9. centos7 tar.gz zip 解压命令

    tar负责打包,gzip负责压缩 tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个, ...

  10. 2、Spring-RootApplicationContext-refresh

    上一篇文中提到父容器root applicationContext最后是调用XmlWebApplicationContext去实现的, 但是什么时候开始解析标签(默认标签.自定义标签).注册bean以 ...