platform设备驱动框架搭建分析

2024-06-11 16:08

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

之前接触到的字符设备驱动是非常单纯的Linux字符设备驱动,他不具备工程中Linux驱动中的设备与驱动分离思想设备驱动的分层思想,不具备“总线-设备-驱动”模型的概念。接下来通过分析platform设备驱动模型的搭建过程来看看Linux的设备驱动模型究竟是怎样的?

platform驱动模型搭建:

(1)platform核心层:为设备层和驱动层提供注册接口、为设备层和驱动层的匹配提供标准

①搭建总线框架:
总线类结构体:

struct bus_type {const char		*name;struct bus_attribute	*bus_attrs;struct device_attribute	*dev_attrs;struct driver_attribute	*drv_attrs;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 (*suspend)(struct device *dev, pm_message_t state);int (*suspend_late)(struct device *dev, pm_message_t state);int (*resume_early)(struct device *dev);int (*resume)(struct device *dev);struct dev_pm_ops *pm;struct bus_type_private *p; //看到这个private就有点C++类中的限定域关键字的意思,这个类的私有成员
};
总线类实例化:platform总线
struct bus_type platform_bus_type = {.name		= "platform",.dev_attrs	= platform_dev_attrs,.match		= platform_match, //关键成员.uevent		= platform_uevent,.pm		= PLATFORM_PM_OPS_PTR,
};
注册platform总线过程:
platform_bus_init()
{.....error =  bus_register(&platform_bus_type);//注册platform总线的核心工作.....
}
bus_register(struct bus_type *bus)
{//创建bus的属性文件   retval = bus_create_file(bus, &bus_attr_uevent);......//在/sys/bus/bus->name目录下创建devices目录priv->devices_kset = kset_create_and_add("devices", NULL,&priv->subsys.kobj);....//在/sys/bus/bus->name目录下创建drivers目录priv->drivers_kset = kset_create_and_add("drivers", NULL,&priv->subsys.kobj);//初始化总线设备\总线驱动链表klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);klist_init(&priv->klist_drivers, NULL, NULL);
}
核心层的功绩:初始化了klist_devices和klist_drivers两个链表,没有谈到如何判断设备和驱动匹配?“.match=platform_match”有初始化,但是什么时候被调用?
当一个驱动挂接到该总线的时候,该总线的match方法被调用。同样的,当一个设备挂接到该总线时,platform_match也会被调用。也就是说核心层只提供匹配的方法!不会帮他们去匹配,这人生大事要他们自己去完成!
这就好办了,都是挂接到总线上的时候,往后分析时肯定会遇到,先暂时放着,先看看他的实现:

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);/* match against the id table first */if (pdrv->id_table) //看看drv的id_table中是否有现成匹配的设备记录return platform_match_id(pdrv->id_table, pdev) != NULL;/* fall-back to driver name match */return (strcmp(pdev->name, drv->name) == 0); /* match成功,strcmp返回0,语句逻辑返回1 */
}
②为设备层提供注册API、提供自动匹配接口函数
设备基类:

struct device {struct device			*parent;struct device_private	*p;struct kobject 			kobj;const char				*init_name; 	/* initial name of the device 这个就是传统的bus_id,具体到每一个设备之后当做默认值 */struct device_type		*type;......struct bus_type			*bus;			/* type of bus device is on */struct device_driver 	*driver;		/* which driver has allocated this device */void					*driver_data;	/* data private to the driver */void					*platform_data;	/* Platform specific data, device core doesn't touch it */......void					(*release)(struct device *dev);
};
派生类:platform设备
struct platform_device {const char		*name;int				id;  // 硬件设备的象征/代表struct device	dev; // 由此继承基类u32				num_resources;struct resource	* resource;//这个驱动使用的资源struct platform_device_id	*id_entry;
};
注册platform设备函数调用关系:
platform_device_register(struct platform_device *pdev)
platform_device_add(struct platform_device *pdev)
pdev->dev.bus = &platform_bus_type;
device_add(&pdev->dev);
bus_attach_device(struct device *dev)
device_attach(dev);
bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
bus_for_each_drv()函数的实现:
bus_for_each_drv(struct bus_type *bus, struct device_driver *start,void *data, int (*fn)(struct device_driver *, void *))
{......while ((drv = next_driver(&i)) && !error)error = fn(drv, data);......
}
分析:
首先关心他的最后一个形参(*fn),他在注册platform_device时最终被重定向到__device_attach()函数,回调函数的使用在内核源码里边屡见不鲜!因为它可以减少很多重复的代码。
现在分析的焦点转移到__device_attach函数:

__device_attach(struct device_driver *drv, void *data)
{struct device *dev = data;if (!driver_match_device(drv, dev))return 0;return driver_probe_device(drv, dev); //match成功就执行这个函数,他最终调用really_probe()函数
}
driver_match_device(struct device_driver *drv,struct device *dev)
{return drv->bus->match ? drv->bus->match(dev, drv) : 1; //看到这一句,上面留下的疑问就解决了:原来核心层留下的匹配判断标准match接口就是在这里被调用的!!!好爽!^_^
}
really_probe(struct device *dev, struct device_driver *drv)
{
......if (dev->bus->probe) //如果bus_type结构里边的probe成员有定义就优先调用他的{ret = dev->bus->probe(dev);if (ret)goto probe_failed;} else if (drv->probe) //没有就调用匹配到的drv结构里边的probe成员函数{ret = drv->probe(dev);if (ret)goto probe_failed;}driver_bound(dev);//bound是绑定的意思,即将match成功的设备加入驱动的设备链表......
}
③为驱动层提供API、提供自动匹配接口函数
驱动基类:

struct device_driver {const char		*name;struct bus_type		*bus;struct module		*owner;const char 		*mod_name;	/* used for built-in modules */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);struct attribute_group **groups;struct dev_pm_ops *pm;struct driver_private *p;
};
驱动派生类:
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 (*suspend_late)(struct platform_device *, pm_message_t state);int (*resume_early)(struct platform_device *);int (*resume)(struct platform_device *);struct device_driver driver;  //继承基类struct platform_device_id *id_table;
};
注册platform_driver驱动结构体函数执行流程:
platform_driver_register(struct platform_driver *drv)
{/*下面进行一系列的判断,如果派生的platform_driver中没有对特有成员进行初始化,设置成默认的 */drv->driver.bus = &platform_bus_type;   //指向这个驱动所属的bus类型:platformif (drv->probe)  //有重定向drv->driver.probe = platform_drv_probe;if (drv->remove) //有重定向drv->driver.remove = platform_drv_remove;......return driver_register(&drv->driver); 【进入分析】//注册的关键材料是platform_driver->driver->bus:关键是为了注册总线的类型platform_bus_type
}
driver_register(struct device_driver *drv)
{......struct device_driver *other;......other = driver_find(drv->name, drv->bus); //在该总线上查找是否有该设备驱动名对应的驱动if (other) { //如果设备已经存在对应的驱动就:出错,驱动已经存在put_driver(other);printk(KERN_ERR "Error: Driver '%s' is already registered, ""aborting...\n", drv->name);return -EEXIST;}bus_add_driver(drv);  /* 在总线上添加这个驱动,成功的话最终结果:在bus/platform/drivers目录下面生成“name”对应的目录 ,并且会生成 bind  module  uevent  unbind 四个文件*/......
}
继续深入分析:
bus_add_driver(struct device_driver *drv)
driver_attach(drv);  /* 试图将驱动和设备绑定起来 */
bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);//到这里可以非常明显的发现和设备层做的事情非常相似,几乎是对称出现
/* 对总线上的每一个设备都会拿来执行__driver_attach,他在这里被用作回调函数,看看是否匹配,这个函数和__device_attach函数做的事情基本一样这里就不再累述了*/

(2)设备层:主要工作就是把核心层提供的API用起来
1.设置好platform_device结构体成员:主要是name、resource、num_resources、id、dev->release、

2.通过platform_device_register()把这个结构体链入核心层的klist_devices链表


(3)驱动层:同样是把核心层提供的接口函数用起来
1.设置好platform_driver结构体成员:probe、remove、driver->name
2.通过platform_driver_register()函数把这个结构体链入核心层的klist_drivers链表
3.实现probe成员函数
4.通常最后才去完成probe函数用到的材料,一般是file_operation结构体成员,这样应用层就可以通过这个接口来操作设备

这篇关于platform设备驱动框架搭建分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

python中Hash使用场景分析

《python中Hash使用场景分析》Python的hash()函数用于获取对象哈希值,常用于字典和集合,不可变类型可哈希,可变类型不可,常见算法包括除法、乘法、平方取中和随机数哈希,各有优缺点,需根... 目录python中的 Hash除法哈希算法乘法哈希算法平方取中法随机数哈希算法小结在Python中,

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

如何使用Haporxy搭建Web群集

《如何使用Haporxy搭建Web群集》Haproxy是目前比较流行的一种群集调度工具,同类群集调度工具有很多如LVS和Nginx,本案例介绍使用Haproxy及Nginx搭建一套Web群集,感兴趣的... 目录一、案例分析1.案例概述2.案例前置知识点2.1 HTTP请求2.2 负载均衡常用调度算法 2.

Python的端到端测试框架SeleniumBase使用解读

《Python的端到端测试框架SeleniumBase使用解读》:本文主要介绍Python的端到端测试框架SeleniumBase使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录SeleniumBase详细介绍及用法指南什么是 SeleniumBase?SeleniumBase

关于MyISAM和InnoDB对比分析

《关于MyISAM和InnoDB对比分析》:本文主要介绍关于MyISAM和InnoDB对比分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录开篇:从交通规则看存储引擎选择理解存储引擎的基本概念技术原理对比1. 事务支持:ACID的守护者2. 锁机制:并发控制的艺

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis