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

相关文章

Python 迭代器和生成器概念及场景分析

《Python迭代器和生成器概念及场景分析》yield是Python中实现惰性计算和协程的核心工具,结合send()、throw()、close()等方法,能够构建高效、灵活的数据流和控制流模型,这... 目录迭代器的介绍自定义迭代器省略的迭代器生产器的介绍yield的普通用法yield的高级用法yidle

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

找不到Anaconda prompt终端的原因分析及解决方案

《找不到Anacondaprompt终端的原因分析及解决方案》因为anaconda还没有初始化,在安装anaconda的过程中,有一行是否要添加anaconda到菜单目录中,由于没有勾选,导致没有菜... 目录问题原因问http://www.chinasem.cn题解决安装了 Anaconda 却找不到 An