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. Yii2 JWT

    Yii2 JWT 这个扩展为Yii framework 2.0提供了JWT集成(需要PHP 5.6+).它包括基本的HTTP身份验证支持. 目录 安装 依赖关系 基本用法 创建 从字符串分析 验证 令 ...

  2. [转]触发fullgc的条件

    良好的状态是:minor gc比较多 full gc 比较少 因为fullgc时间比较慢,而且会占用CPU的时间片. 不好的状态是:minor gc比较少 full gc 比较多 这样程序就一直卡在f ...

  3. PP: Overviewing evolution patterns of egocentric networks by interactive construction of spatial layouts

    Problem: get an overall picture of how ego-networks evolve is a common challenging task. Existing te ...

  4. ORA-00917: missing comma

    问题描述 ORA-00917: missing comma 问题原因 逗号,引号什么的多了或者少了,或者换行引起的

  5. 分析https网页加载http资源导致的页面报错原因及其解决方案

    https网页加载http资源导致的页面报错及解决方案 https是当下的网站的主流趋势,甚至像苹果这样的大公司,则完全要求用户必须使用https地址. 然而对于以前http链接来说,我们往往就存在一 ...

  6. 关于RTP时间戳及多媒体通信同步的问题(转)

    文章转载自:罗索实验室 [http://www.rosoo.net/a/201101/10776.html]

  7. java基础之 数据类型

    数据类型表示要存储在变量中的不同类型的值. 一.Java语言提供了八种基本数据类型.六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型. 1. byte byte 数据类型是8位.有 ...

  8. 堆(Heap)和栈(Stack)

    详细可以查看这篇文章:https://www.cnblogs.com/qingtianMo/p/5255121.html 栈保存代码执行(调用)的路径,堆负责保存对象(数据) 栈相当于摞盒子,进入一个 ...

  9. jQuery的主要使用方法

    一.在html中添加jquery,可以使用cdn加载jquery 1.网址:https://www.bootcdn.cn/jquery/ 2.推荐使用3.4左右版本的,建议使用min.js后缀的,mi ...

  10. OpenCV之XML和YAML文件读写

    FileStorage类 该类有两个构造函数 FileStorage::FileStorage() FileStorage::FileStorage(const string& source, ...