Linux设备驱动之platform

来源:
导读 大家好,我是本期栏目编辑小友,现在为大家讲解Linux设备驱动之platform问题。 根据Linux设备模型,一个逼真的

大家好,我是本期栏目编辑小友,现在为大家讲解Linux设备驱动之platform问题。

根据Linux设备模型,一个逼真的Linux设备和驱动通常需要连接到一条总线上,对于连接到PCI、USB等的设备来说,这自然不是问题。但是在嵌入式系统中,集成在SoC系统中的独立外设控制器和连接到SoC存储空间的外设不连接到这种总线。基于这样的背景,Linux设计了一个名为platform Bus的虚拟总线,对应的设备称为platform_device,驱动程序称为platform_driver。

设计目标

兼容设备型号

因此符合Linux 2.6的设备模型。因此,匹配sysfs节点和设备电源管理是可能的。

BSP和驱动器的隔离。

在BSP中定义平台设备、设备使用的资源以及设备的具体配置信息。在驱动中,只需要通过通用API获取资源和数据,使得板卡相关代码与驱动代码分离,使得驱动更具可扩展性和跨平台性。

软件体系结构

内核中与平台设备相关的实现位于include/Linux/Platform _ device . h和drivers/base/platform.c中,其软件架构如下:

从图片中可以看出,平台设备在内核中的实现主要包括三个部分:

平台总线,基于底层总线模块,抽象出虚拟平台总线,用于挂载平台设备;

平台设备,从底层设备模块抽象而来,用于表示平台设备;

平台驱动程序是从底层设备驱动程序模块中抽象出来的,用于驱动平台设备。

平台_设备

注意,所谓platform_device不是与字符设备、块设备、网络设备并列的概念,而是Linux系统提供的附加手段。比如在S3C2440处理器中,内部集成的控制器如I2C、RTC、SPI、LCD、看门狗等都归类为platform_device,本身就是字符设备。

/*在*/struct platform _ device { const char * name;/*设备名称*/u32 id;/*用于标识设备的id */struct设备dev/*真实设备(Platform设备只是一个特殊的设备,所以它的核心逻辑是由底层模块实现的)*/u32 num _ resources;/*设备使用的各种资源数量*/结构资源*资源;/*资源*/};/*在*/struct资源{ resource _ size _ t start/*资源开始*/resource_size_t结束;/* end */const char *名称;无符号长标志;/* type */struct resource *父级,*同级,*子级;};/*设备驱动获取BSP定义的资源*/struct resource * platform _ get _ resource(struct platform _ device *,无符号int标志,无符号int num);#包含int platform _ device _ register(struct platform _ device *);void platform _ device _ unregister(struct platform _ device *);

与板级密切相关的Tip:的资源描述放在dev.paltform_data中。

paltform_driver

Platform_driver包含probe(),remove(),shutdown(),suspend(),resume()函数,这些函数通常需要由驱动程序实现。

struct platform _ driver { int(* probe)(struct platform _ device *);int(* remove)(struct platform _ device *);void(*关机)(struct platform _ device *);int(* suspend)(struct platform _ device *,pm_message_t状态);int(* suspend _ late)(struct platform _ device *,pm_message_t状态);int(* resume _ early)(struct platform _ device *);int(* resume)(struct platform _ device *);struct device_driver驱动程序;};# include int platform _ driver _ register(struct platform _ driver *);void platform _ driver _ unregister(struct platform _ driver *);

平台总线

系统中为平台总线平台总线类型定义了一个总线类型实例:

struct bus _ type platform _ bus _ type={。名称='platform ',dev_attrs=platform_dev_attrs,match=platform_match,uevent=platform_uevent,pm=PLATFORM_PM_OPS_PTR,};EXPORT_SYMBOL_GPL(平台_总线_类型);

这里我们应该关注它的match()成员函数,它决定了platform_device和platform_driver之间如何匹配:

staTIc int platform _ match(struct device * dev,struct device _ driver * drv){ struct platform _ device * pdev;pdev=container_of(dev,struct platform_device,dev);return (strncmp(pdev-name,drv-name,BUS _ ID _ SIZE)=0);}

标签:

版权声明:转载此文是出于传递更多信息之目的。若有来源标注错误或侵犯了您的合法权益,请作者持权属证明与本网联系,我们将及时更正、删除,谢谢您的支持与理解。