【linux设备模型】之platform设备驱动
1 struct platform_device {
2 const char * name;
3 int id;
4 struct device dev;
5 u32 num_resources;
6 struct resource * resource;
7
8 const struct platform_device_id *id_entry;
9
10 /* arch specific additions */
11 struct pdev_archdata archdata;
12 };
当中的resource是platform驱动用到的资源,它是定义在<include/linux/ioport.h>中:
1 struct resource {
2 resource_size_t start;
3 resource_size_t end;
4 const char *name;
5 unsigned long flags;
6 struct resource *parent, *sibling, *child;
7 };
platform_driver定义在<linux/platform_device.h>中:
1 struct platform_driver {
2 int (*probe)(struct platform_device *);
3 int (*remove)(struct platform_device *);
4 void (*shutdown)(struct platform_device *);
5 int (*suspend)(struct platform_device *, pm_message_t state);
6 int (*resume)(struct platform_device *);
7 struct device_driver driver;
8 const struct platform_device_id *id_table;
9 };系统中为platform总线定义了一个实例platform_bus_type,在<linux/platform_device.h>进行了声明:extern struct bus_type platform_bus_type;定义在drivers/base/platform.c中:1 struct bus_type platform_bus_type = {
2 .name = "platform",
3 .dev_attrs = platform_dev_attrs,
4 .match = platform_match,
5 .uevent = platform_uevent,
6 .pm = &platform_dev_pm_ops,
7 };
8 EXPORT_SYMBOL_GPL(platform_bus_type);二、详细实现1、platform_match函数的实现(定义在drivers/base/platform.c中):1 static const struct platform_device_id *platform_match_id(
2 const struct platform_device_id *id,
3 struct platform_device *pdev)
4 {
5 while (id->name[0]) {
6 if (strcmp(pdev->name, id->name) == 0) {
7 pdev->id_entry = id;
8 return id;
9 }
10 id++;
11 }
12 return NULL;
13 }
14
15 /**
16 * platform_match - bind platform device to platform driver.
17 * @dev: device.
18 * @drv: driver.
19 *
20 * Platform device IDs are assumed to be encoded like this:
21 * "<name><instance>", where <name> is a short description of the type of
22 * device, like "pci" or "floppy", and <instance> is the enumerated
23 * instance of the device, like '0' or '42'. Driver IDs are simply
24 * "<name>". So, extract the <name> from the platform_device structure,
25 * and compare it against the name of the driver. Return whether they match
26 * or not.
27 */
28 static int platform_match(struct device *dev, struct device_driver *drv)
29 {
30 struct platform_device *pdev = to_platform_device(dev);
31 struct platform_driver *pdrv = to_platform_driver(drv);
32
33 /* Attempt an OF style match first */
34 if (of_driver_match_device(dev, drv))
35 return 1;
36
37 /* Then try to match against the id table */
38 if (pdrv->id_table)
39 return platform_match_id(pdrv->id_table, pdev) != NULL;
40
41 /* fall-back to driver name match */
42 return (strcmp(pdev->name, drv->name) == 0);
43 }是通过device的name和driver的name进行匹配的。
2、platform_device的定义和注冊
(platform_add_devices定义在drivers/base/platform.c中)1 /**
2 * platform_add_devices - add a numbers of platform devices
3 * @devs: array of platform devices to add
4 * @num: number of platform devices in array
5 */
6 int platform_add_devices(struct platform_device **devs, int num)
7 {
8 int i, ret = 0;
9
10 for (i = 0; i < num; i++) {
11 ret = platform_device_register(devs[i]);
12 if (ret) {
13 while (--i >= 0)
14 platform_device_unregister(devs[i]);
15 break;
16 }
17 }
18
19 return ret;
20 }
21 EXPORT_SYMBOL_GPL(platform_add_devices);对于一个指定的板子来说,比方对于ldd6410的globalfifo驱动来说,要在mach-ldd6410.c中定义platform_device globalfifo_device,并将globalfifo_device放入ldd6410_devices数组中,代码例如以下所看到的。![]()
3、platform_driver的操作
platform_driver的定义和注冊在对应驱动文件里实现,以下是《linux驱动开发具体解释》一书中globalfifo的样例的部分代码:
4、platform设备资源和数据
(1)、resource的定义(<linux/ioport.h>)
1 struct resource {
2 resource_size_t start;
3 resource_size_t end;
4 const char *name;
5 unsigned long flags;
6 struct resource *parent, *sibling, *child;
7 };
当flags是IORESOURCE_MEM时,start和end各自是资源的起始地址和结束地址;
当flags是IORESOURCE_IRQ时,start和end各自是资源的起始中断号和结束中断号;
#define IORESOURCE_TYPE_BITS 0x00001f00 /* Resource type */
#define IORESOURCE_IO 0x00000100
#define IORESOURCE_MEM 0x00000200
#define IORESOURCE_IRQ 0x00000400
#define IORESOURCE_DMA 0x00000800
#define IORESOURCE_BUS 0x00001000对于同种类型的资源能够有两份或多份。
(2)、获得资源
对resource的定义通常在板文件里,而驱动通过platform_get_resource获得资源(driver/base/platform.c):
1 /**
2 * platform_get_resource - get a resource for a device
3 * @dev: platform device
4 * @type: resource type
5 * @num: resource index
6 */
7 struct resource *platform_get_resource(struct platform_device *dev,
8 unsigned int type, unsigned int num)
9 {
10 int i;
11
12 for (i = 0; i < dev->num_resources; i++) {
13 struct resource *r = &dev->resource[i];
14
15 if (type == resource_type(r) && num-- == 0)
16 return r;
17 }
18 return NULL;
19 }
20 EXPORT_SYMBOL_GPL(platform_get_resource);三、platform驱动的一个样例以linux-2.6.38内核的dm9000驱动为例说明平台驱动的定义和资源获取。1、platform_device和resource的定义(mach-mini6410.c)1 /* DM9000AEP 10/100 ethernet controller */
2 /*dm9000的资源*/
3 static struct resource mini6410_dm9k_resource[] = {
4 [0] = {
5 .start = S3C64XX_PA_XM0CSN1,
6 .end = S3C64XX_PA_XM0CSN1 + 1,
7 .flags = IORESOURCE_MEM
8 },
9 [1] = {
10 .start = S3C64XX_PA_XM0CSN1 + 4,
11 .end = S3C64XX_PA_XM0CSN1 + 5,
12 .flags = IORESOURCE_MEM
13 },
14 [2] = {
15 .start = S3C_EINT(7),
16 .end = S3C_EINT(7),
17 .flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL
18 }
19 };
20 /*dm9000_plat_data定义在<linux/dm9000.h>*/
21 static struct dm9000_plat_data mini6410_dm9k_pdata = {
22 .flags = (DM9000_PLATF_16BITONLY | DM9000_PLATF_NO_EEPROM),
23 };
24
25 static struct platform_device mini6410_device_eth = {
26 .name = "dm9000",
27 .id = -1,
28 .num_resources = ARRAY_SIZE(mini6410_dm9k_resource),
29 .resource = mini6410_dm9k_resource,
30 .dev = {
31 .platform_data = &mini6410_dm9k_pdata,
32 },
33 };
34
35 /*全部的设备存放在这个数组里,最后通过platform_add_devices进行注冊*/
36 static struct platform_device *mini6410_devices[] __initdata = {
37 &mini6410_device_eth,
38 &s3c_device_hsmmc0,
39 &s3c_device_hsmmc1,
40 &s3c_device_ohci,
41 &s3c_device_nand,
42 &s3c_device_fb,
43 &mini6410_lcd_powerdev,
44 &s3c_device_adc,
45 &s3c_device_ts,
46 };2、platform_driver的定义和注冊(dm9000.c)1 static struct platform_driver dm9000_driver = {
2 .driver = {
3 .name = "dm9000",
4 .owner = THIS_MODULE,
5 .pm = &dm9000_drv_pm_ops,
6 },
7 .probe = dm9000_probe,
8 .remove = __devexit_p(dm9000_drv_remove),
9 };
10
11 static int __init
12 dm9000_init(void)
13 {
14 printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
15
16 return platform_driver_register(&dm9000_driver);
17 }
18
19 static void __exit
20 dm9000_cleanup(void)
21 {
22 platform_driver_unregister(&dm9000_driver);
23 }dm9000驱动获得定义在板文件里的platform_data的方法:struct platform_device *pdevstruct dm9000_plat_data *pdata = pdev->dev.platform_data;db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
platform_get_resource函数定义在drivers/base/platform.c中:1 /**
2 * platform_get_resource - get a resource for a device
3 * @dev: platform device
4 * @type: resource type
5 * @num: resource index
6 */
7 struct resource *platform_get_resource(struct platform_device *dev,
8 unsigned int type, unsigned int num)
9 {
10 int i;
11
12 for (i = 0; i < dev->num_resources; i++) {
13 struct resource *r = &dev->resource[i];
14
15 if (type == resource_type(r) && num-- == 0)
16 return r;
17 }
18 return NULL;
19 }
20 EXPORT_SYMBOL_GPL(platform_get_resource);
參考:《linux驱动开发具体解释》
【linux设备模型】之platform设备驱动的更多相关文章
- 【RT-Thread笔记】IO设备模型及GPIO设备
RTT内核对象--设备 RT-Thread有多种内核对象,其中设备device就是其中一种. 内核继承关系图如下: 设备继承关系图如下: device对象对应的结构体如下: 其中,设备类型type有如 ...
- linux驱动之设备模型
linux 设备驱动模型 inux2.6提供了新的设备模型:总线.驱动.设备.基本关系简要的概括如下: 驱动核心可以注册多种类型的总线. 每种总线下面可以挂载许多设备.(通过kset devices) ...
- Linux 设备驱动开发 —— platform设备驱动应用实例解析
前面我们已经学习了platform设备的理论知识Linux 设备驱动开发 —— platform 设备驱动 ,下面将通过一个实例来深入我们的学习. 一.platform 驱动的工作过程 platfor ...
- Linux 设备模型浅析之 uevent 篇(2)
Linux 设备模型浅析之 uevent 篇 本文属本人原创,欢迎转载,转载请注明出处.由于个人的见识和能力有限,不可能面 面俱到,也可能存在谬误,敬请网友指出,本人的邮箱是 yzq.seen@gma ...
- linux设备模型:扩展篇
Linux设备模型组件:总线 一.定义:总线是不同IC器件之间相互通讯的通道;在计算机中,一个总线就是处理器与一个或多个不同外设之间的通讯通道;为了设备模型的目的,所有的设备都通过总线相互连接,甚至 ...
- Linux内核(7) - 设备模型(上)
对于驱动开发来说,设备模型的理解是根本,毫不夸张得说,理解了设备模型,再去看那些五花八门的驱动程序,你会发现自己站在了另一个高度,从而有了一种俯视的感觉,就像凤姐俯视知音和故事会,韩峰同志俯视女下属. ...
- 【原创】linux设备模型之kset/kobj/ktype分析
背 景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本 ...
- Linux设备模型(3)_Uevent
转自:http://www.wowotech.net/linux_kenrel/uevent.html 1. Uevent的功能 Uevent是Kobject的一部分,用于在Kobject状态发生改变 ...
- Linux设备模型(3)_Uevent【转】
转自:http://www.wowotech.net/device_model/uevent.html 1. Uevent的功能 Uevent是Kobject的一部分,用于在Kobject状态发生改变 ...
- Linux 设备模型
在 2.5 开发循环中一个声明的目标是为内核创建一个统一的设备模型. 之前的内核没有单一的数据结 构, 使它们可以来获取关于系统如何整合的信息. 尽管缺乏信息, 有时事情也进行的不错. 新系统, 带 ...
随机推荐
- HDU 5749 Colmerauer 单调队列+暴力贡献
BestCoder Round #84 1003 分析:(先奉上zimpha巨官方题解) 感悟:看到题解单调队列,秒懂如何处理每个点的范围,但是题解的一句算贡献让我纠结半天 已知一个点的up,do ...
- XTUOJ1247 Pair-Pair 预处理+暴力
分析:开个1000*1000的数组,预处理矩阵和,然后分类讨论就好 时间复杂度:O(n) #include <cstdio> #include <iostream> #incl ...
- 如何避免JavaScript的内存泄露及内存管理技巧
发表于谷歌WebPerf(伦敦WebPerf集团),2014年8月26日. 高效的JavaScript Web应用必须流畅,快速.与用户交互的任何应用程序,都需要考虑如何确保内存有效使用,因为如果 ...
- Python学习之eventlet.greenpool
该模块提供对 greenthread 池的支持. greenthread 池提供了一定数量的备用 greenthread ,有效限制了孵化 greenthread 过多导致的内存不足,当池子中没有足够 ...
- CSAPP(2):程序的汇编表示(Linux版)
程序员学习汇编代码的需求随着时间的推移发生了变化,开始时只要求程序员能直接用汇编语言编写程序,现在则要求他们能够阅读和理解编译器产生的代码. 下面是针对32位机器 数据格式 Intel用术语“字”(w ...
- MATLAB / Simulink on BeagleBone Black
转自:beagleboard@googlegroups.com邮件组 作者:kevind I have MATLAB / Simulink working with BeagleBone Black. ...
- leetcode@ [355] Design Twitter (Object Oriented Programming)
https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...
- USACO 2013 November Contest Gold 简要题解
Problem 1. Empty Stalls 扫两遍即可. Problem 2. Line of Sight 我们发现能互相看见的一对点一定能同时看见粮仓的某一段.于是转换成有n段线段,问有多少对线 ...
- 依赖包bcrypt安装Issues
说明:本文在个人博客地址为edwardesire.com,欢迎前来品尝. 在决策树项目中,使用到了bcrypt依赖包来加密文件.在wini8(win7)部署安装这个依赖的时候容易出现出现了问题. 解决 ...
- WebBrowser实现编辑网页
//1.显示网页 procedure TForm2.FormCreate(Sender: TObject); begin Panel1.Align := alTop; CheckBox1.Anchor ...