您的位置:首页 > 运维架构 > Linux

Linux Platform Device and Driver

2010-11-03 10:18 603 查看
参考资料:

linux-2.6.24/Documentation/driver-model/platform.txt
http://blog.chinaunix.net/u2/60011/showart_1018502.html
《platform _device 和 platform_driver 注册过程》

http://www.eetop.cn/blog/html/45/11145-676.html http://blog.csdn.net/unbutun/archive/2009/03/25/4023313.aspx
从 Linux 2.6 起引入了一套新的驱动管理和注册机制 :platform_device 和 platform_driver 。 Linux 中大部分的设备驱动,都可以使用这套机制 , 设备用 platform_device 表示,驱动用 platform_driver 进行注册。
Linux platform driver 机制和传统的 device driver 机制 ( 通过 driver_register 函数进行注册 ) 相比,一个十分明显的优势在于 platform 机制将设备本身的资源注册进内核,由内核统一管理,在驱动程序中使用这些资源时通过 platform device 提供的标准接口进行申请并使用 。这样提高了驱动和资源管理的独立性,并且拥有较好的可移植性和安全性 ( 这些标准接口是安全的 ) 。
platform 机制的本身并不复杂,由两部分组成: platform _ device 和 platform _ driver
通过 Platform 机制开发发底层驱动的大致流程为 : 定义 platform_device -> 注册 platform_device -> 定义 platform_driver -> 注册 platform_driver 。

数据结构

在 2.6 内核中 platform 设备用结构体 platform_device 来描述,
// 该结构体定义在kernel/include/linux/platform_device.h
struct platform_device
{
const char * name;
u32 id;
struct device dev;
u32 num_resources;
struct resource * resource;
};

该结构一个重要的元素是 resource ,该元素存入了最为重要的设备资源信息,
// 定义在kernel/include/linux/ioport.h
struct resource
{
const char      *name;
unsigned long   start, end;
unsigned long   flags;
struct resource *parent, *sibling, *child;
};

实际上是对地址范围及其属性的一个描述。最后几个用于树型结构的指针是内核用于管理所有资源的。

定义resource

下面举 s3c2410 平台的 i2c 驱动作为例子来说明:
/* arch/arm/mach-s3c2410/devs.c */
/* I2C */
static struct resource s3c_i2c_resource[] = {
[0] = {
.start = S3C24XX_PA_IIC,
.end = S3C24XX_PA_IIC + S3C24XX_SZ_IIC - 1,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = IRQ_IIC, //S3C2410_IRQ(27)
.end = IRQ_IIC,
.flags = IORESOURCE_IRQ,
}
};

这里定义了两组resource,它描述了一个I2C设备的资源,第1组描述了这个I2C设备所占用的总线地址范围,IORESOURCE_MEM表示第 1组描述的是内存类型的资源信息,第2组描述了这个I2C设备的中断号,IORESOURCE_IRQ表示第2组描述的是中断资源信息。设备驱动会根据 flags来获取相应的资源。

platform_device

struct platform_device s3c_device_i2c =
{
.name = "s3c2410-i2c",
.id = -1,
.num_resources = ARRAY_SIZE(s3c_i2c_resource),
.resource = s3c_i2c_resource,
};
。。。。
platform_add_devices(...); // 向内核注册设备


定义好了platform_device结构体后就可以调用函数platform_add_devices向系统中添加该设备 了,之后可以调用 platform_driver_register()进行设备注册 。要注意的是,这里的platform_device设备的注册过程必须在相应设备驱动加载之前被调用,即执行platform_driver_register之前,原因是因为驱动注册时需要匹配内核中所以已注册的设备名 。

platform_driver

// include/linux/platform_device.h
/* device driver for platform bus bits */
static struct platform_driver s3c2410_i2c_driver = {
.probe = s3c24xx_i2c_probe,
.remove = s3c24xx_i2c_remove,
.resume = s3c24xx_i2c_resume,
.driver = {
.owner = THIS_MODULE,
.name = "s3c2410-i2c",
},
};
...
platform_driver_register(...); // 向内核注册驱动

在驱动初始化函数中调用函数platform_driver_register()注册platform_driver,需要注意的是 s3c_device_i2c结构中name元素和s3c2410_i2c_driver结构中driver.name必须是相同的,这样在 platform_driver_register()注册时会对所有已注册的所有platform_device中的name和当前注册的 platform_driver的driver.name进行比较,只有找到相同的名称的platfomr_device才能注册成功,当注册成功时会调用platform_driver结构元素probe函数指针,这里就是s3c24xx_i2c_probe,当进入probe函数后,需要获取设备的资源信息,常用获取资源的函数主要是:

struct resource * platform_get_resource(struct platform_device *dev, unsigned int type, unsigned int num);
根据参数type所指定类型,例如IORESOURCE_MEM,来获取指定的资源。

那什么情况使用 platform driver 机制编写驱动

我的理解是只要和内核本身运行依赖性不大的外围设备 ( 换句话说只要不在内核运行所需的一个最小系统之内的设备 ), 相对独立的 , 拥有各自独自的资源 (addresses and IRQs) , 都可以用 platform_driver 实现。如: lcd,usb,uart 等,都可以用 platfrom_driver 写,而 timer,irq 等最小系统之内的设备则最好不用 platfrom_driver 机制,实际上内核实现也是这样的。

/* 以下为本人注解:
1、更多的概况了解,可参考文档 Documentation/driver-model/platform.txt
2、在 Understanding the Linux Kernel一书的第13.1.1.1节中有这样一段话
“... 尽管访问I/O端口很容易, 检测哪个I/O端口已被分配给I/O设备却并非易事. 对基于ISA总线(译注:原始IBM PC采用的总线)的系统来说尤其如此. 通常设备驱动必须盲目地写数据到一些I/O端口来探测设备的存在, 然而,如果该端口已经被另外一种设备占用, 这样的操作就可能导致系统崩溃... ”
以下为文档platform.txt中的话:
“... 不提倡这种驱动方式. 假如你在升级这样一个驱动, 请尽力把设备列举从驱动中转移到更合适的地方. 这样做很赚, 因为驱动程序一开始就处在"正常模式", 可以直接使用由即插即用(PNP)设置或平台设备设置代码所创建的设备..

struct platform_ device *platform_device_alloc(const char *name, int id);

你可以用 platform_device_alloc _alloc( )来动态地给设备分配空间, 然后在用platform_device_register()加上一些资源来初始化该设备.

经常也用另外一个更好的解决办法:

struct platform_device *platform_device_register_simple(const char *name, int id,struct resource *res, unsigned int nres);

你可以用platform_device_register_simple()一次完成分配空间和注册设备的任务, 设备命名和驱动绑定
所以,比较老的现成的较多的驱动中,我们可以看到用
request_mem_region()和
request_irq()等申请device资源,而看到上面的话后,在最新的驱动中,应尽量的采用platform_device_alloc等platform机制的函数实现。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: