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. [SDOI2013] 直径 - 树形dp

    对于给定的一棵树,其直径的长度是多少,以及有多少条边满足所有的直径都经过该边. Solution 有点意思 先随便求一条直径(两次DFS即可),不妨设为 \(s,t\),我们知道要求的这些边一定都在这 ...

  2. Spring Boot2 快速入门教程-到上手

    Spring Boot2 教程合集 入门 纯 Java 代码搭建 SSM 环境 创建一个 Spring Boot 项目的三种方法 理解 Spring Boot 项目中的 parent 基础配置 配置文 ...

  3. js和jq跳转到另一个页面或者在另一个窗口打开页面

    $("#pic").click(function(){ location.href='newpage.html'; }); 上面的相当于<a href="newpa ...

  4. Go源码文件与命令

    Go源码文件 文件类型 命令源码文件 : 声明自己属于main包且包含main函数的源码文件,一个包里边不要有多个命令源码文件,虽然用go install ,go run单独执行命令源码文件没有问题, ...

  5. SQLServer导出查询结果带表头(标题行)

    SQLServer导出查询结果带表头(标题行) 平时我们经常会需要将SQLSERVER查询的结果复制到EXCEL文档中进行分析处理,但是有一件事很头痛,就是复制结果网格的数据到EXCEL之后,都是没有 ...

  6. 题解【洛谷P1967】[NOIP2013]货车运输

    题面 题解 注意到有一些限重很低的边不会被走到. 于是考虑建一棵最大生成树,在生成树上寻找答案. 设\(f[i][j]\)表示\(i\)的\(2^j\)级祖先,\(w[i][j]\)表示\(i\)到\ ...

  7. [HDU4609] 3-idiots - 多项式乘法,FFT

    题意:有\(n\)个正整数,求随机选取一个3组合,能构成三角形的概率. Solution: 很容易想到构造权值序列,对其卷积得到任取两条边(可重复)总长度为某数时的方案数序列,我们希望将它转化为两条边 ...

  8. (转)java垃圾回收一

    转自:http://jianfulove.iteye.com/blog/1833768 一切都进入了自动化了,但是对于各种内存溢出,内存泄漏问题的出现,我们还是很有必要学习GC的.地球人都知道,Jav ...

  9. codeforces 1284D. New Year and Conference(线段树)

    链接:https://codeforces.com/problemset/problem/1284/D 题意:有n场讲座,有两个场地a和b,如果在a场地开讲座则需要占用[sai,eai],在b场地开讲 ...

  10. OmniGraffle原型案例 | 某APP产品原型PDF文件分享之二

    1.从 App 首页进入商城 App底部 Tab有社区.商城,我们点击「商城」进入「乐宠商城」,下面简称商城.商城采用的是经典的宫格式导航设计(还有普通列表式.和瀑布流列表式.或两者皆有等),主要有搜 ...