最近研究了一下Linux驱动的platform机制。

从Linux2.6起,引入了一套新的驱动管理和注册机制:Platform_device和Platform_driver。Linux中大部分的设备驱动,都可以使用这套机制,设备用platform_device表示,驱动用platform_driver进行注册。

Linux platform driver机制和传统的device_driver机制相比,一个十分明显的优势在于platform机制将本身的资源注册进内核,由内核统一管理,在驱动程序中使用这些资源时通过platform_device提供的标准接口进行申请并使用。这样提高了驱动和资源管理的独立性,并且拥有较好的可移植性和安全性。

和传统的驱动一样,platform机制也分为三个步骤:

1 总线注册阶段:

内核启动初始化时的main.c文件中的kernel_init() à do_basic_setup() à driver_init() à platform_bus_init() à bus_register(&platform_bus_type),注册了一条platform总线(虚拟总线)。

2 添加设备时:

设备注册的时候Platform_device_register() à platform_device_add() à (pdev à dev.bus = &platform_bus_type) à device_add(),就这样把设备给挂到虚拟的总线上。

3 驱动注册时:

Platform_driver_register() à driver_register() à bus_add_driver() à driver_attach() à bus_for_each_dev(), 对在每个挂在虚拟的platform bus的设备作__driver_attach() à driver_probe_device(),判断drvàbusàmatch()是否执行成功,此时通过指针执行platform_match à strncmp(pdev à name , drvàname , BUS_ID_SIZE),如果相符就调用really_probe(实际就是执行相应设备的platform_driver àprobe(platform_device)。)开始真正的探测,如果probe成功,则绑定设备到该驱动。

从上面可以看出,platform机制最后还是调用了bus_register() , device_add() , driver_register()这三个关键的函数。

下面看几个结构体:

struct platform_device           (/include/linux/Platform_device.h)

{

const char   * name;

int          id;

struct device     dev;

u32        num_resources;

struct resource * resource;

};

Platform_device结构体描述了一个platform结构的设备,在其中包含了一般设备的结构体struct device       dev;设备的资源结构体struct resource    * resource;还有设备的名字const char     * name。(注意,这个名字一定要和后面platform_driver.driver àname相同,原因会在后面说明。)

该结构体中最重要的就是resource结构,这也是之所以引入platform机制的原因。

struct resource                            ( /include/linux/ioport.h)

{

resource_size_t start;

resource_size_t end;

const char *name;

unsigned long flags;

struct resource *parent, *sibling, *child;

};

其中 flags位表示该资源的类型,start和end分别表示该资源的起始地址和结束地址

struct platform_driver                  (/include/linux/Platform_device.h)

{

int (*probe)(struct platform_device *);

int (*remove)(struct platform_device *);

void (*shutdown)(struct platform_device *);

int (*suspend)(struct platform_device *, pm_message_t state);

int (*suspend_late)(struct platform_device *, pm_message_t state);

int (*resume_early)(struct platform_device *);

int (*resume)(struct platform_device *);

struct device_driver driver;

};

Platform_driver结构体描述了一个platform结构的驱动。其中除了一些函数指针外,还有一个一般驱动的device_driver结构。

名字要一致的原因:

上面说的驱动在注册的时候会调用函数bus_for_each_dev(), 对在每个挂在虚拟的platform bus的设备作__driver_attach() à driver_probe_device(),在此函数中会对dev和drv做初步的匹配,调用的是drv->bus->match所指向的函数。platform_driver_register函数中drv->driver.bus = &platform_bus_type,所以drv->bus->match就为platform_bus_typeàmatch,为platform_match函数,该函数如下:

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

{

struct platform_device *pdev = container_of(dev, struct platform_device, dev);

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

}

是比较dev和drv的name,相同则会进入really_probe()函数,从而进入自己写的probe函数做进一步的匹配。所以devàname和driveràdrvàname在初始化时一定要填一样的。

不同类型的驱动,其match函数是不一样的,这个platform的驱动,比较的是dev和drv的名字,还记得usb类驱动里的match吗?它比较的是Product ID和Vendor ID。

个人总结Platform机制的好处:

1 提供platform_bus_type类型的总线,把那些不是总线型的soc设备都添加到这条虚拟总线上。使得,总线——设备——驱动 的模式可以得到普及。

2 提供platform_device和platform_driver类型的数据结构,将传统的device和driver数据结构嵌入其中,并且加入resource成员,以便于和Open Firmware这种动态传递设备资源的新型bootloader和kernel 接轨。

转载于:https://www.cnblogs.com/armlinux/archive/2010/11/26/2396887.html

Linux驱动的platform机制相关推荐

  1. Linux驱动之platform设备驱动

    当我们在一块开发板上写好了驱动,但换一块不同芯片的开发板,我们就需要重新写一个驱动.其中主要是硬件连接也就是接口发生了改变,而软件框架几乎不用通用的.所以为了更加方便地移植,能够仅修改很小的内容就达到 ...

  2. 详解Linux2.6内核中基于platform机制的驱动模型 (经典)

    [摘要]本文以Linux 2.6.25 内核为例,分析了基于platform总线的驱动模型.首先介绍了Platform总线的基本概念,接着介绍了platform device和platform dri ...

  3. 详解Linux2.6内核中基于platform机制的驱动模型

    原文地址:详解Linux2.6内核中基于platform机制的驱动模型 作者:nacichan [摘要]本文以Linux 2.6.25 内核为例,分析了基于platform总线的驱动模型.首先介绍了P ...

  4. 十八、Linux驱动之nor flash驱动

    1. 基本概念 NOR FLASH是很常见的一种存储芯片,数据掉电不会丢失.NOR FLASH支持Execute On Chip,即程序可以直接在FLASH片内执行(这意味着存储在NOR FLASH上 ...

  5. linux驱动模型开发——linux platform总线机制讲解与实例开发

    1.概述: 通常在Linux中,把SoC系统中集成的独立外设单元(如:I2C.IIS.RTC.看门狗等)都被当作平台设备来处理. 从Linux2.6起,引入了一套新的驱动管理和注册机制:Platfor ...

  6. linux platform匹配机制,Linux驱动中的platform总线详解

    platform总线是学习linux驱动必须要掌握的一个知识点. 一.概念 嵌入式系统中有很多的物理总线:I2c.SPI.USB.uart.PCIE.APB.AHB linux从2.6起就加入了一套新 ...

  7. linux 内核驱动的名字,Linux内核驱动的的platform机制

    接下来来看platform_driver结构体的原型定义,在include/linux/platform_device.h中,代码如下: struct platform_driver { int (* ...

  8. Linux内核中的platform机制

    Linux内核中的platform机制 从Linux 2.6起引入了一套新的驱动管理和注册机制:platform_device和platform_driver.Linux中大部分的设备驱动,都可以使用 ...

  9. Linux驱动下的platform总线架构(转)

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

  10. linux驱动-platform详解

    platform详解 作者:程姚根,华清远见嵌入式学院讲师. 一.platform设备模型 从Linux 2.6起引入了一套新的驱动管理和注册机制,platform_device和platform_d ...

最新文章

  1. 微软技术透明中心落地北京,允许相关机构查看源代码
  2. xp计算机属性打不开,xp系统我的电脑右键属性打不开怎么办
  3. android仿ios录音动画,仿IOS录音机
  4. 机器视觉技术的应用领域
  5. QT学习第一步 QT下载及安装详细步骤
  6. 修改html字体为微软雅黑,通过自定义CSS全局更换微软雅黑字体
  7. (18)全民小视频引流脚本模块化开发12-任务总数与时间间隔By飞云脚本学院
  8. 【2018黑龙江省赛】UPC-7222 Overflow(模拟物理水体积)
  9. 去除URL中带有的jsessionid
  10. 购物足迹功能php,wordpress实现访客足迹功能
  11. 【第十篇】- Git 远程仓库(Github)之Spring Cloud直播商城 b2b2c电子商务技术总结
  12. 魅蓝note2解锁bootloader教程
  13. Kali2020版安装 macof安装使用
  14. 信息熵、相对熵和交叉熵
  15. unity3D用图片当做字体显示————点阵字体概念
  16. 爬取链家北京租房数据并做简单分析
  17. android 横竖屏坐标转换,Android 屏幕横竖切换详解
  18. IOS启动动画(广告)
  19. Dreamweaver怎么用css设置基本的文字属性
  20. Oracle特殊字符转义:和'

热门文章

  1. MongoDB 安全与认证
  2. 织梦系统MySQL安装_如何更改已安装的织梦dedecms系统数据库表前缀?
  3. 阶段3 2.Spring_08.面向切面编程 AOP_1 AOP的概念
  4. jQuery基础--选择器
  5. 期望dp+高斯消元——bzoj3143
  6. 移位和位运算相关算法题学习积累
  7. 【深度学习系列】PaddlePaddle可视化之VisualDL
  8. java中String、StringBuffer和StringBuilder的区别(简单介绍)
  9. 红帽学习笔记[RHCSA] 第一周
  10. 小程序 长按复制文本