驱动开发之platform总线

2024-06-02 18:44
文章标签 开发 驱动 总线 platform

本文主要是介绍驱动开发之platform总线,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.前言

在前面的实验以及提出的各种问题中,为了提高移植性,降低模块耦合度,提让模块高内聚,分离device与driver是一个必然的趋势了。为了解决这个问题,心心念念的platform总线出来。

linux从2.6起就加入了一套新的驱动管理和注册的机制platform平台总线,是一条虚拟的总线,并不是一个物理的总线。

platform平台总线连接了平台设备(Platform Devices)和平台驱动(Platform Drivers)。平台设备是通过结构体platform_device来表示的,包含了设备的名称、资源(如内存地址、中断号等)和设备特定的属性。而平台驱动则是通过结构体platform_driver来实现,定义了与设备交互的函数指针,如探测、移除、打开、关闭、读写等操作。

2.platform总线工作原理

device与driver的注册无先后顺序,不管是device注册还是driver注册,都会触发platrom总线的检测函数,检测另一端是否已经注册,完成校验,匹配。其他匹配过程,可以看一下下面这个图,一个大佬画得,很清晰明了(链接:参考第一个)

 2.1.platform 总线介绍

2.1.1.总线结构体

此结构体定义在文件 include/linux/device.h

struct bus_type {const char *name; /* 总线名字 */const char *dev_name; struct device *dev_root;struct device_attribute *dev_attrs;const struct attribute_group **bus_groups; /* 总线属性 */const struct attribute_group **dev_groups; /* 设备属性 */const struct attribute_group **drv_groups; /* 驱动属性 */int (*match)(struct device *dev, struct device_driver *drv);int (*uevent)(struct device *dev, struct kobj_uevent_env *env);int (*probe)(struct device *dev);int (*remove)(struct device *dev);void (*shutdown)(struct device *dev);int (*online)(struct device *dev);int (*offline)(struct device *dev);int (*suspend)(struct device *dev, pm_message_t state);int (*resume)(struct device *dev);const struct dev_pm_ops *pm;const struct iommu_ops *iommu_ops;struct subsys_private *p;struct lock_class_key lock_key;};

 match 函数,就是完成设备和驱动之间匹配的,总线就是使用 match 函数来根据注册的设备来查找对应的驱 动,或者根据注册的驱动来查找相应的设备,因此每一条总线都必须实现此函数match 函数有 两个参数:dev 和 drv,这两个参数分别为 device 和 device_driver 类型,也就是设备和驱动。

2.1.2.bus实例platform 总线

platform 总线是 bus_type 的一个具体实例,定义在文件 drivers/base/platform.c,platform 总 线定义如下:

struct bus_type platform_bus_type = {
.name = "platform",
.dev_groups = platform_dev_groups,
.match = platform_match,
.uevent = platform_uevent,
.pm = &platform_dev_pm_ops,
};

 这些函数实现都在drivers/base/platform.c内部,不需要我们去实现,但是作为学习可以看看。

2.1.3.platform 总线的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. 当device设置driver_override时,只绑定到匹配的驱动程序 */if (pdev->driver_override)return !strcmp(pdev->driver_override, drv->name);/* Attempt an OF style match first 第一种匹配方式, OF 类型的匹配,也就是设备树采用的匹配方式 */if (of_driver_match_device(dev, drv))return 1;/* Then try ACPI style match 第二种匹配方式,ACPI 匹配方式 */if (acpi_driver_match_device(dev, drv))return 1;/* Then try to match against the id table . 第三种匹配方式id_table 匹配,每个 platform_driver 结构体有一个 id_table成员变量,保存了很多 id 信息。这些 id 信息存放着这个 platformd 驱动所支持的驱动类型*/if (pdrv->id_table)return platform_match_id(pdrv->id_table, pdev) != NULL;/* fall-back to driver name match 第四种匹配方式,如果第三种匹配方式的 id_table 不存在的话就直接比较驱动和设备的 name 字段,看看是不是相等,如果相等的话就匹配成功*/return (strcmp(pdev->name, drv->name) == 0);}

 3.platform 总线的使用

使用platform总线是很简单的,只需要实现的结构体的函数指针定义的函数,然后通过相应的接口注册进去即可。(使用很简单,如果使用很难,这个东西就不会那么出名了,懂的都懂

 3.1.platform 驱动

struct platform_driver {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 (*resume)(struct platform_device *);struct device_driver driver;const struct platform_device_id *id_table;bool prevent_deferred_probe;
};

 probe 函数,当驱动与设备匹配成功以后 probe 函数就会执行。

remove函数,当platform_driver_unregister(&led_driver);函数调用时,会调用bus_remove_driver函数,该函数里面会再调用driver_detach函数,最后一层一层下去调用remove函数,完成资源释放, 或者在dev释放的时候,也会触发remove函数进行资源释放。测试实验见第4节。

probe ,remove函数一般驱动的提供者会编写,如果自己要编写一个全新的驱动,那么 probe 就需要自行实现。

driver 成员,为 device_driver 结构体变量,Linux 内核里面大量使用到了面向对象 的思维,device_driver 相当于基类,提供了最基础的驱动框架。plaform_driver 继承了这个基类, 然后在此基础上又添加了一些特有的成员变量。

id_table 表,也就是我们上一小节讲解 platform 总线匹配驱动和设备的时候采用的 第四种方法,id_table 是个表( 也就是数组) ,每个元素的类型为 platform_device_id:

struct platform_device_id {char name[PLATFORM_NAME_SIZE];kernel_ulong_t driver_data;
};

3.1.1.struct device_driver结构体


struct device_driver {
const char *name;
struct bus_type *bus;struct module *owner;
const char *mod_name; /* used for built-in modules */bool suppress_bind_attrs; /* disables bind/unbind via sysfs */const struct of_device_id *of_match_table;const struct acpi_device_id *acpi_match_table;int (*probe) (struct device *dev);int (*remove) (struct device *dev);void (*shutdown) (struct device *dev);int (*suspend) (struct device *dev, pm_message_t state);int (*resume) (struct device *dev);const struct attribute_group **groups;const struct dev_pm_ops *pm;struct driver_private *p;
};

of_match_table 就是采用设备树的时候驱动使用的匹配表,同样是数组,每个匹 配项都为 of_device_id 结构体类型,此结构体定义在文件 include/linux/mod_devicetable.h 中,内容如下:

struct of_device_id {char name[32];char type[32];char compatible[128];const void *data;
};

compatible 非常重要,因为对于设备树而言,就是通过设备节点的 compatible 属 性值和 of_match_table 中每个项目的 compatible 成员变量进行比较,如果有相等的就表示设备 和此驱动匹配成功。这是设备树匹配方法

struct device_driver结构体里面的name就是最后一种匹配方法了。

虽然有四种匹配方法,但是只要有一种方法存在,而且匹配失败了,剩下的匹配方法就不能进行,就认为无法匹配了,请注意!!!!

3.2.platform 设备

struct platform_device {
const char *name; 
int id; 
bool id_auto;
struct device dev;
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;
};
name 表示设备名字,要和所使用的 platform 驱动的 name 字段相同,否则的话设
备就无法匹配到对应的驱动
driver_override成员: 就是match里面的第一种强制匹配了
num_resources 表示资源数量
resource 表示资源,也就是设备信息,resource 结构体内容如下:
struct resource {
resource_size_t start;
resource_size_t end;
const char *name;
unsigned long flags;
struct resource *parent, *sibling, *child;
};
start end 分别表示资源的起始和终止信息,对于内存类的资源,就表示内存起始和终止
地址, name 表示资源名字, flags 表示资源类型,可选的资源类型都定义在了文件
include/linux/ioport.h 里面,如:
#define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */#define IORESOURCE_TYPE_BITS 0x00001f00 /* Resource type */
#define IORESOURCE_IO 0x00000100 /* PCI/ISA I/O ports */
#define IORESOURCE_MEM 0x00000200
#define IORESOURCE_REG 0x00000300 /* Register offsets */
#define IORESOURCE_IRQ 0x00000400
#define IORESOURCE_DMA 0x00000800
#define IORESOURCE_BUS 0x00001000

一般是IORESOURCE_MEM与IORESOURCE_IRQ使用的比较多。

3.3.驱动注册接口

int platform_driver_register (struct platform_driver *driver)
driver :要注册的 platform 驱动。
返回值: 负数,失败; 0 ,成功。
void platform_driver_unregister(struct platform_driver *drv)
drv :要卸载的 platform 驱动。
返回值: 无。

 3.4.设备注册和注销接口

int platform_device_register(struct platform_device *pdev)
pdev :要注册的 platform 设备。
返回值: 负数,失败; 0 ,成功。
void platform_device_unregister(struct platform_device *pdev)
pdev :要注销的 platform 设备。
返回值: 无。

3.5.小结

从接口看,就两组接口(4个接口),两个结构体,就可以完成一个platform总线设备开发,是不是感觉很简单。下面进行一些实验验证。

4.测试实验

例子来源:韦东山老师的例子:

4.1.driver部分

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/platform_device.h>static int hello_probe(struct platform_device *pdev)
{int i = 0;printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);struct resource *source[2];for (i = 0; i < 2; i++) 
{source[i] = platform_get_resource(pdev, IORESOURCE_MEM, i);if (!source[i]) {dev_err(&pdev->dev, "No MEM resource for always on\n");continue;}printk(" MEM resource i:%d, start:%x, end:%x \r\n", i, source[i]->start, source[i]->end);
}for (i = 0; i < 2; i++) 
{source[i] = platform_get_resource(pdev, IORESOURCE_IRQ, i);if (!source[i]) {dev_err(&pdev->dev, "No IRQ resource for always on\n");continue;}printk(" IRQ resource i:%d, start:%x, end:%x \r\n", i, source[i]->start, source[i]->end);
}return 0;
}static int hello_remove(struct platform_device *pdev)
{printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);return 0;
}static struct platform_driver hello_driver = 
{.probe      = hello_probe,.remove     = hello_remove,.driver     = {.name   = "100ask_led",},
};static int __init hello_drv_init(void)
{int err;printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);err = platform_driver_register(&hello_driver); return err;
}static void __exit hello_drv_exit(void)
{printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);platform_driver_unregister(&hello_driver);
}module_init(hello_drv_init);
module_exit(hello_drv_exit);MODULE_LICENSE("GPL");

4.2.deivce部分

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/platform_device.h>static struct resource resources[] = {[0] ={.start = (3<<8)|(1),.flags = IORESOURCE_IRQ,},[1] = {.start = 0x1000,.end = 0x1010,.flags = IORESOURCE_MEM,}, 
};static void hello_dev_release(struct device *dev)
{printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
}static struct platform_device hello_dev = 
{.name = "100ask_led",.dev = {.release = hello_dev_release,},.num_resources = ARRAY_SIZE(resources),.resource = resources,};static int __init hello_dev_init(void)
{int err;printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);err = platform_device_register(&hello_dev);   return err;
}static void __exit hello_dev_exit(void)
{printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);platform_device_unregister(&hello_dev);
}module_init(hello_dev_init);
module_exit(hello_dev_exit);MODULE_LICENSE("GPL");

 4.3.测试结果

(1)先加载drv,再加载dev.然后卸载dev,再卸载drv.结果如下:

可以发现,dev与drv匹配上之后,会触发drv的probe函数。当dev卸载的时候,也会触发drv的remove函数(前提drv没有被释放)。dev里面的release函数是在dev的platform_device_unregister函数调用之后触发的,与drv没关系

数据获取也正常,当然数据是随便造的,这是普通设备的数据获取,设备树的数据是不一样的,等设备树章节,再介绍,获取数据,以及设备树下的匹配规则。

(2)先加载dev,再加载drv,然后卸载drv,再卸载dev,结果如下:

 可以发现,dev与drv匹配上之后,会触发drv的probe函数。当drv卸载的时候,platform_driver_unregister函数也会触发drv的remove函数,原理可见3.1小节。

4.4.通过id_table进行匹配

static const struct platform_device_id led_id_table[] = {{"hellodevice1",   1},{"hellodevice2", 2},{"100ask_led", 3},{ },
};static struct platform_driver hello_driver = 
{.probe      = hello_probe,.remove     = hello_remove,.driver     = {.name   = "100ask_led1",},.id_table = led_id_table,
};
id_table匹配只需要改一下drv的参数即可,dev不需要改动。测试结果和上面测试一样。

4.5.driver_override强制匹配

static struct platform_device hello_dev =
{.name = "100ask_led1",.dev ={.release = hello_dev_release,},.num_resources = ARRAY_SIZE(resources),.resource = resources,.driver_override = "100ask_led1",
};

强制匹配,只需要修改dev里面的参数,这个名字必须得与platform_driver结构体下面的.driver成员里面的name匹配上,才可以触发probe函数。

ok,目前platform总线就这样了,与设备树的使用,将在下一章进行介绍。

参考:

一张图掌握 Linux platform 平台设备驱动框架!【建议收藏】-CSDN博客

手把手教Linux驱动10-platform总线详解_linux驱动中platform总线驱动的ppt-CSDN博客

Linux驱动开发(二)---驱动与设备的分离设计_linux driver device 分离-CSDN博客

这篇关于驱动开发之platform总线的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1024783

相关文章

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

基于Python开发批量提取Excel图片的小工具

《基于Python开发批量提取Excel图片的小工具》这篇文章主要为大家详细介绍了如何使用Python中的openpyxl库开发一个小工具,可以实现批量提取Excel图片,有需要的小伙伴可以参考一下... 目前有一个需求,就是批量读取当前目录下所有文件夹里的Excel文件,去获取出Excel文件中的图片,并

基于Python开发PDF转PNG的可视化工具

《基于Python开发PDF转PNG的可视化工具》在数字文档处理领域,PDF到图像格式的转换是常见需求,本文介绍如何利用Python的PyMuPDF库和Tkinter框架开发一个带图形界面的PDF转P... 目录一、引言二、功能特性三、技术架构1. 技术栈组成2. 系统架构javascript设计3.效果图