[platform]linux platform device/driver(一)--Driver是如何找到对应的device
1.platform device是怎么"自动"关联到platform driver上的?
转向linux driver有些时间了,前段时间碰到个问题,在Linux kernel 3.10的drivers/tty/serial/imx.c中,注册driver的时候调用platform_driver_register(&serial_imx_driver),serial_imx_driver类型为platform_driver, serial_imx_driver中有个成员变量probe,在driver注册时会调用这个函数,但是probe这个函数的参数是platform_device,而在imx.c中没有出现platform_device类型的任何变量,问题就此产生了,platform_device这个参数到底是从什么地方传进来的,内容又是什么,什么时候赋值的?
2.设备、总线、驱动三者如何关联起来?
后来查阅了相关资料,大体上的意思是,在linux2.6内核以后,设备和驱动的结构划分为设备、总线、驱动,设备和驱动分别挂在总线上,而他们可以通过name来进行匹配(还有其他的方式),二者绑定有两种方式:一种是先安装driver,后挂载设备;另一种则是先挂载设备,然后安装driver。但二者都是后面动作的会在总线上搜索与自己name项相同的设备或者driver进行匹配,也即完成绑定。
3.代码中的实现
在imx.c中,由于平台是3.10,所以使用DTS,设备都是预先挂载了如串口0/1/2/3..., 后面安装驱动时,会调用上述说到的platform_driver_register(&serial_imx_driver),在驱动注册过程中会对进行匹配设备,一旦匹配到某个device,就会调用probe进行初始化,这个probe的参量platform_device也就有来源了。具体可以参考下面的调用过程:
do_basic_setup()->driver_init()->platform_bus_init()->...初始化platform bus(虚拟总线)
设备向内核注册的时候platform_device_register()->platform_device_add()->...内核把设备挂在虚拟的platform bus下
驱动注册的时候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),如果相符就调用platform_drv_probe()->driver->probe(),如果probe成功则设备与驱动成功绑定.
代码如下:在执行到driver_attach的时候platform_device还没出现:
int driver_attach(struct device_driver *drv)
{
return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
}
EXPORT_SYMBOL_GPL(driver_attach);
这一步开始出现了,县初始化device list,然后通过next_device来遍历节点的属性,调用fn也即__driver_attach,dev也是从next_device中获取device指针。
int bus_for_each_dev(struct bus_type *bus, struct device *start,
void *data, int (*fn)(struct device *, void *))
{
struct klist_iter i;
struct device *dev;
int error = ; if (!bus || !bus->p)
return -EINVAL; klist_iter_init_node(&bus->p->klist_devices, &i,
(start ? &start->p->knode_bus : NULL));
while ((dev = next_device(&i)) && !error)
error = fn(dev, data);
klist_iter_exit(&i);
return error;
}
EXPORT_SYMBOL_GPL(bus_for_each_dev);
以上只是简单理解,还是没有深入进去,所以很多原理说不清楚,希望能有时间静下心好好研究研究,如:
1. 设备、驱动、总线三者设计理念,结构模式
2. 设备、驱动先后挂到总线上的机制
3. linux2.6之前、linux2.6、linux2.6之后,驱动、设备在这些版本中的变迁,如platform的出现,dts的出现等
[platform]linux platform device/driver(一)--Driver是如何找到对应的device的更多相关文章
- [platform]linux platform device/driver(三)--Platform Device和Platform_driver注册过程之代码对比
转自:http://blog.csdn.net/thl789/article/details/6723350 Linux 2.6的设备驱动模型中,所有的device都是通过Bus相连.device_r ...
- [platform]linux platform device/driver(二)--Platform Device和Platform_driver注册过程之详细代码
转自:http://www.cnblogs.com/haimeng2010/p/3582403.html 目录: 1.platform_device注册过程 2.platform_driver注册过程 ...
- Linux Platform Device and Driver
从 Linux 2.6 起引入了一套新的驱动管理和注册机制 :Platform_device 和 Platform_driver . Linux 中大部分的设备驱动,都可以使用这套机制 , 设备用 P ...
- Linux kernel驱动相关抽象概念及其实现 之“bus,device,driver”
bus,device,driver三个很重要的概念贯穿Linux内核驱动架构,特转载一篇博文: (转载自http://blog.csdn.net/gdt_a20/article/details/642 ...
- Linux platform设备简介
Technorati 标签: Linux platform Linux在2.6内核中,针对一系列设备驱动,提供新的管理框架,成为platform机制,推出的目的,在于隔离驱动的资源和实现,使得 ...
- Linux Platform驱动模型(二) _驱动方法
在Linux设备树语法详解和Linux Platform驱动模型(一) _设备信息中我们讨论了设备信息的写法,本文主要讨论平台总线中另外一部分-驱动方法,将试图回答下面几个问题: 如何填充platfo ...
- Linux Regulator Framework(2)_regulator driver
转自蜗窝科技:http://www.wowotech.net/pm_subsystem/regulator_driver.html 说实话,这篇好难懂啊... 1. 前言 本文从regulator d ...
- [中英对照]Device Drivers in User Space: A Case for Network Device Driver | 用户态设备驱动: 以网卡驱动为例
前文初步介绍了Linux用户态设备驱动,本文将介绍一个典型的案例.Again, 如对Linux用户态设备驱动程序开发感兴趣,请阅读本文,否则请飘过. Device Drivers in User Sp ...
- Linux Platform驱动模型(二) _驱动方法【转】
转自:http://www.cnblogs.com/xiaojiang1025/archive/2017/02/06/6367910.html 在Linux设备树语法详解和Linux Platform ...
随机推荐
- nginx+tomcat集群配置(1)---根目录设定和多后端分发配置
前言: 对于javaer而言, nginx+tomcat集群配置, 已然成了web应用部署的主流. 大公司如此, 小公司亦然. 对于个人开发者而言, 资源有限, 往往多个web应用混部于一台服务器(云 ...
- 345. Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- 12. Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- Codeforces Round #190 (Div. 2) B. Ciel and Flowers
链接:http://codeforces.com/contest/322/problem/B 这题做错了.没考虑周全. #include <cstdio> #include <cst ...
- 转ORA-28002: the password will expire within 7 days 解决方法
最后一步要改密码,否则还会报错. 1. 查看用户的profile设置: SELECT username,profile FROM dba_users; 一般用户的profile设置都为DEFAULT. ...
- java parseint()
static int parseInt(String s) static int parseInt(String s, int radix) parseInt(String s)表示将 “数字” 转换 ...
- 多线程问题(JVM重排序)
public class Test3 { private static boolean ready; private static int Number; private static class R ...
- JavaScript判断IE各版本最完美解决方案
https://github.com/nioteam/jquery-plugins/issues/12 jQuery在1.9版本之前,提供了一个浏览器对象检测的属性$.browser,使用率极高.但是 ...
- IE 下加载jQuery
转:http://www.iitshare.com/ie8-not-use-native-json.html 解决在IE8中无法使用原生JSON的问题 起因 在项目中要将页面上的js对象传给后台, ...
- Python爬虫学习笔记——豆瓣登陆(二)
昨天能够登陆成功,但是不能使用cookies,今天试了一下requests库的Session(),发现可以保持会话了,代码只是稍作改动. #-*- coding:utf-8 -*- import re ...