Linux2.6的内核中引入了一种新的设备驱动模型-平台(platform)设备驱动,平台设备驱动分为平台设备(platform_device)和平台驱动(platform_driver),平台设备的引入使得Linux设备驱动更加便于移植。
一、平台设备
平台设备结构体:
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为设备资源数组,类型有IORESOURCE_IO、IORESOURCE_MEM、IORESOURCE_IRQ、IORESOURCE_DMA、IORESOURCE_DMA。下面是一个网卡芯片DM9000的外设资源:
1 static struct resource dm9000_resources[] = {
2 [0] = {
3 .start = S3C64XX_PA_DM9000,
4 .end = S3C64XX_PA_DM9000 + 3,
5 .flags = IORESOURCE_MEM,
6 },
7 [1] = {
8 .start = S3C64XX_PA_DM9000 + 4,
9 .end = S3C64XX_PA_DM9000 + S3C64XX_SZ_DM9000 - 1,
10 .flags = IORESOURCE_MEM,
11 },
12 [2] = {
13 .start = IRQ_EINT(7),
14 .end = IRQ_EINT(7),
15 .flags = IORESOURCE_IRQ | IRQF_TRIGGER_HIGH,
16 },
17 };
dm9000_resources里面有三个设备资源,第一个为IORESOURCE_MEM类型,指明了第一个资源内存的起始地址为S3C64XX_PA_DM9000结束地址为S3C64XX_PA_DM9000 + 3,第二个同样为IORESOURCE_MEM类型,指明了第二个资源内存的起始地址为S3C64XX_PA_DM9000 + 4结束地址为S3C64XX_PA_DM9000 + S3C64XX_SZ_DM9000 - 1,第三个为IORESOURCE_IRQ类型,指明了中断号为IRQ_EINT(7)。
1 struct device {
2 struct device *parent;
3
4 struct device_private *p;
5
6 struct kobject kobj;
7 const char *init_name; /* initial name of the device */
8 struct device_type *type;
9
10 struct mutex mutex; /* mutex to synchronize calls to
11 * its driver.
12 */
13
14 struct bus_type *bus; /* type of bus device is on */
15 struct device_driver *driver; /* which driver has allocated this
16 device */
17 void *platform_data; /* Platform specific data, device
18 core doesn't touch it */
19 ...
20 };
struct device结构体里面有一个重要成员platform_data,它是平台设备和平台驱动进行数据传递的重要成员。
平台设备注册:
1 int platform_device_register(struct platform_device *pdev);
platform_device_register()会对平台设备进行相应的初始化之后调用platform_device_register()函数把它添加到子系统中。
平台设备注销:
1 void platform_device_unregister(struct platform_device *pdev);
platform_device_unregister()函数释放设备资源之后从子系统中将其移除。
平台设备模板:
1 static struct resource xxx_resource =
2 {
3 [0] =
4 {
5 .start = ...,
6 .end = ...,
7 .flags = ...,
8 },
9 [1] =
10 {
11 ...
12 }
13 ...
14 };
15
16 static struct xxx_plat_data xxx_data =
17 {
18 ...
19 };
20
21 static struct platform_device xxx_platform_device =
22 {
23 .name = NAME,
24 .num_resources = ARRAY_SIZE(xxx_resource),
25 .resource = xxx_resource,
26 .dev =
27 {
28 .platform_data = &xxx_data,
29 }
30 };
31
32 static int __init xxx_device_init(void)
33 {
34 ...
35 /* 注册平台设备 */
36 platform_device_register(&xxx_platform_device);
37 ...
38 }
39
40 static void __exit xxx_device_exit(void)
41 {
42 ...
43 /* 注销平台设备 */
44 platform_device_unregister(&xxx_platform_device);
45 ...
46 }
二、平台驱动
平台驱动结构体:
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 };
平台驱动结构体driver成员里面的name必须与平台设备结构体里面的name成员一致,在系统注册一个设备的时候,会通过设备结构体里面的name成员和平台驱动driver里面的name成员匹配,当匹配成功则调用平台驱动的probe函数,
通常在probe函数中获取平台设备的资源和私有数据并进行设备的初始化。
获取设备资源:
1 struct resource *platform_get_resource(struct platform_device *dev, unsigned int type, unsigned int num);
platform_get_resource()函数用于获取平台设备的资源,dev为要平台设备,type为平台设备资源类型,num为平台资源号(比如同一个资源有两个则资源号为0,1)。
平台驱动注册:
1 int platform_driver_register(struct platform_driver *drv);
platform_driver_register()函数完成平台驱动的注册,在驱动模块加载时调用。
平台驱动注销:
1 void platform_driver_unregister(struct platform_driver *drv);
platform_driver_unregister()函数完成平台驱动的注销,在驱动模块卸载时调用。
平台驱动模板:
1 static int __devinit xxx_probe(struct platform_device *pdev)
2 {
3 struct xxx_plat_data *pdata = pdev->dev.platform_data; /* 获取私有数据 */
4 platform_get_resource(pdev,xxx,x); /* 获取设备资源 */
5 ...
6 }
7
8 static struct platform_driver xxx_platform_driver =
9 {
10 .probe = xxx_probe,
11 .remove = __devexit_p(xxx_remove),
12 .driver =
13 {
14 .name = NAME, /* 跟平台设备名一致 */
15 ...
16 },
17 ...
18 };
19
20 static int __init xxx_driver_init(void)
21 {
22 ...
23 /* 驱动注册 */
24 platform_driver_register(&xxx_platform_driver);
25 ...
26 }
27
28 static void __exit xxx_driver_exit(void)
29 {
30 ...
31 /* 驱动注销 */
32 platform_driver_unregister(&xxx_platform_driver);
33 ...
34 }
上一篇:Linux混杂设备驱动 - 按键设备驱动
下一篇:Linux设备驱动开发 - 混杂设备驱动
推荐阅读最新更新时间:2026-03-07 03:15
- ADP1829 PWM 降压控制器使用 9V 至 12V 作为输入电压
- 采用MC9RS08KA2单片机的单相电机调速应用电路
- LT3435 的典型应用 - 具有 100 uA 静态电流的高压 3A、500kHz 降压型开关稳压器
- DC854D-E、LTC2217IUP 演示板、CMOS 输出、105 Msps、16 位 ADC 1MHz
- LT6654BMPS6-2.048 扩展电源范围电压基准的典型应用
- LTC3110IUF 3.6V 自主备份和充电应用的典型应用电路
- 使用 Analog Devices 的 LTC1562-2 的参考设计
- XCEL-NOIX0SE045KBG-GEVK:X-Celerator XGS 45000 彩色显影剂套件
- OP497GPZ精密运算放大器正峰值检波器典型应用电路
- LDK120C28R 2.8V低压降稳压器典型应用(D版)电路

Linux技术手册
【2025 DigiKey创意大赛】 学伴智盒 - 代码
智能机械臂
非常经典的关于LLC的杨波博士论文
XC6406PP60DL






京公网安备 11010802033920号