[Linux服务器] 一张图掌握 Linux platform 平台设备驱动框架!【建议收藏】

144 0
Honkers 2025-5-21 23:06:34 来自手机 | 显示全部楼层 |阅读模式


所有的热爱都要不遗余力,真正喜欢它便给它更高的优先级,和更多的时间吧!

Linux系统&驱动其它文章:     Linux


嵌入式系统中有很多的物理总线:I2c、SPI、USB、uart、PCIE、APB、AHB。

像这样的总线很多,而 Linux 系统要考虑到驱动的可重用性,因此提出了驱动的分离与分层这样的软件思路,于是 Linux从2.6起就加入了 platform 设备驱动,也叫做平台设备驱动。它是一条虚拟的总线, 并不是一个物理的总线。如下图所示:(本文不考虑设备树的情况):


一、总线驱动模型简介

总线是处理器与一个或者多个设备之间的通道,在设备模型中,所有的设备都是通过总线相连,当然也包括虚拟的 platform 平台总线。

总线驱动模型中有三要素:

1. 总线

● struct bus_type 结构体:

  1. struct bus_type {
  2. const char *name;
  3. const char *dev_name;
  4. struct device *dev_root;
  5. ...
  6. };
复制代码

name 成员是总线的名字,比如 platform:


● 总线注册:

  1. bus_register //注册一条总线
  2. bus_unregister //注销总线
复制代码

平台总线的注册如下:

2. 总线设备(硬件)

● struct device 结构:

在最底层,Linux 系统中每一个设备都用 device 结构的一个实例来表示:

  1. struct device //device.h
  2. {
  3. struct bus_type *bus;//代表该设备挂在哪条总线上
  4. void (*release)(struct device *dev); //release 是必须实现的
  5. ...
  6. }
复制代码

● 设备注册:

  1. device_register(struct device *dev)
  2. device_unregister(struct device *dev)
复制代码

3. 设备驱动(软件)

设备模型跟踪所有系统所知道的设备,进行跟踪的主要原因是让驱动程序核心协调驱动程序与新设备之间的关系。

设备驱动程序可以导出信息和配置变量,这些是独立于任何特定设备的。

● device_driver 结构:

  1. struct device_driver //device.h
  2. {
  3. //用于和硬件进行匹配
  4. const char *name;
  5. struct bus_type *bus;
  6. int (*probe) (struct device *dev);
  7. int (*remove) (struct device *dev);
  8. ...
  9. }
复制代码

● 注册函数:

  1. driver_register(struct device_driver *drv)
  2. driver_unregister(struct device_driver *drv)
复制代码

二、platform 平台总线

为了达到所有硬件都可以按照总线设备驱动模型来实现驱动,内核中建立一条虚拟的总线platform,它可以将那些没有真正挂在具体总线上的硬件, 虚拟的认为挂在了platform总线上,达到统一。

而其中用户最需要做的就是填充 platform_driver 驱动 和 platform_device设备。

1. platform_device

“继承” 于 deviceplatform 设备,用来描述硬件,存储硬件使用的资源信息和容易变化的信息

  1. struct platform_device
  2. {
  3. //设备的名字,用于和驱动进行匹配的
  4. const char *name;
  5. //内核中维护的所有的设备必须包含该成员
  6. struct device dev;
  7. //资源个数
  8. u32 num_resources;
  9. //描述资源
  10. struct resource * resource;
  11. ...
  12. }
复制代码

其中 struct resource

  1. struct resource {
  2. resource_size_t start; //表示资源的起始值,
  3. resource_size_t end; //表示资源的最后一个字节的地址, 如果是中断,end和satrt相同
  4. const char *name; // 可不写
  5. unsigned long flags; //资源的类型
  6. struct resource *parent, *sibling, *child;
  7. };
  8. // [flags] 类型,通常将该硬件使用的物理地址、中断号视为资源:
  9. #define IORESOURCE_MEM 0x00000200 //内存: 物理地址资源
  10. #define IORESOURCE_IRQ 0x00000400 //中断: 中断号资源
复制代码

● 注册设备 platform_driver_register :

  1. // #define platform_driver_register(drv) \
  2. // __platform_driver_register(drv, THIS_MODULE)
  3. platform_device_register(struct platform_device *);
复制代码

2. platform_driver

继承于 device_driverplatform_driver 设备驱动:

  1. struct platform_driver
  2. {
  3. //当驱动和硬件信息匹配成功之后,就会调用probe函数,驱动所有的资源的注册和初始化全部放在probe函数中
  4. int (*probe)(struct platform_device *);
  5. //硬件信息被移除了,或者驱动被卸载了,全部要释放,释放资源的操作就放在该函数中
  6. int (*remove)(struct platform_device *);
  7. void (*shutdown)(struct platform_device *);
  8. int (*suspend)(struct platform_device *, pm_message_t state);
  9. int (*resume)(struct platform_device *);
  10. //内核维护的所有的驱动必须包含该成员,通常driver->name用于和设备进行匹配
  11. struct device_driver driver;
  12. //往往一个驱动可能能同时支持多个硬件,这些硬件的名字都放在该结构体数组中
  13. const struct platform_device_id *id_table;
  14. bool prevent_deferred_probe;
  15. }
复制代码

● 驱动注册和卸载

  1. platform_driver_register(struct platform_driver *);
  2. platform_driver_unregister(struct platform_driver *);
复制代码

三、实例

1. 设备 platform_device

1)填充 struct platform_device 的各个成员,主要为:

  • 赋值设备名;
  • 填充 resource 结构变量;
  • 填充 struct device dev;

2)向系统注册设备:platform_device_register

3)注销设备:platform_device_unregister

  1. //btn_dev.c
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. #include <linux/input.h>
  5. #include <linux/platform_device.h>
  6. #include <mach/platform.h>
  7. #include "btn_desc.h"
  8. MODULE_LICENSE("GPL");
  9. //1) 填充 struct platform_device 的各个成员:初始化 resource 结构变量。
  10. struct resource btn_resource[]=
  11. {
  12. {
  13. .start = IRQ_GPIO_A_START + 28,
  14. .end = IRQ_GPIO_A_START + 28, //可以不赋值
  15. .flags = IORESOURCE_IRQ, //中断类型资源
  16. },
  17. {
  18. .start = IRQ_GPIO_B_START + 30,
  19. .end = IRQ_GPIO_B_START + 30, //可以不赋值
  20. .flags = IORESOURCE_IRQ,
  21. },
  22. };
  23. //1) 填充 struct platform_device 的各个成员:初始化 私有资源 结构变量。
  24. btn_desc_t buttons[] = //私有资源,描述信息
  25. {
  26. {"up", PAD_GPIO_A+28, KEY_UP},
  27. {"down", PAD_GPIO_B+30, KEY_DOWN},
  28. };
  29. //1) 填充 struct platform_device 的各个成员: 注销设备时,调用
  30. void btn_release(struct device *dev)
  31. {
  32. printk("call %s\n", __func__);
  33. }
  34. //1) 填充 struct platform_device 的各个成员:
  35. struct platform_device btn_dev =
  36. {
  37. .name = "mybuttons",
  38. .dev =
  39. {
  40. .release = btn_release,
  41. .platform_data = (void *)buttons, //私有,平台设备总线
  42. },
  43. .resource = btn_resource,
  44. .num_resources = ARRAY_SIZE(btn_resource), //资源个数
  45. };
  46. // 2)向系统注册设备:platform_device_register
  47. int __init btn_dev_init(void)
  48. {
  49. platform_device_register(&btn_dev); //1.向dev中加入一个节点, 2.与driver设备匹配
  50. return 0;
  51. }
  52. //3)注销设备:platform_device_unregister
  53. void __exit btn_dev_exit(void)
  54. {
  55. platform_device_unregister(&btn_dev);
  56. }
  57. module_init(btn_dev_init);
  58. module_exit(btn_dev_exit);
复制代码

以上必须在设备驱动加载前完成,即执行platform_driver_register()之前,原因是驱动注册时需要匹配内核中所有已注册的设备名。

结合上面框架流程图,在 platform_device_register 注册的时候, 赋值了成员变量的总线为 platform 总线,然后插入用户资源。device_add 中向 kobject 核心注册设备的 kobject(热插拔事件),然后将改设备添加到设备列表中,包含改节点的父节点所拥有,之后所有的设备都可以通过正确的顺序访问,并知道每个设备都挂在层次结构的哪一点上。

添加设备到设备链表中、再遍历这个链表(),并为每个驱动程序调用该总线的 match 函数 (platform_match)

如果匹配成功,则调用 device_driver 结构中指定的 probe。

2. 驱动 platform_driver

platform_driver 的填充

1)填充 struct platform_driver 的各个成员,主要为:

  • 赋值驱动名;
  • 填充 probe 成员;
  • 填充 remove 成员

2)向系统注册驱动:platform_driver_register

3)注销驱动:platform_driver_unregister

  1. //btn_drv.c
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. #include <linux/platform_device.h>
  5. #include "btn_desc.h"
  6. MODULE_LICENSE("GPL");
  7. //1)填充 struct platform_driver 的各个成员: .probe = btn_probe
  8. int btn_probe(struct platform_device *dev)
  9. {
  10. printk("call %s\n", __func__);
  11. return 0;
  12. }
  13. //1)填充 struct platform_driver 的各个成员: .remove = btn_remove
  14. int btn_remove(struct platform_device *dev)
  15. {
  16. printk("call %s\n", __func__);
  17. return 0;
  18. }
  19. //1)填充 struct platform_driver 的各个成员: 绑定
  20. //继承于device_driver的 platform_driver 设备驱动
  21. struct platform_driver btn_drv =
  22. {
  23. //struct device_driver driver
  24. .driver =
  25. {
  26. .name = "mybuttons",
  27. },
  28. //int (*probe)(struct platform_device *)
  29. .probe = btn_probe,
  30. //int (*remove)(struct platform_device *)
  31. .remove = btn_remove,
  32. };
  33. //2)向系统注册驱动:platform_driver_register
  34. int __init btn_drv_init(void)
  35. {
  36. platform_driver_register(&btn_drv); //1.向dev中加入一个节点, 2.与device设备匹配
  37. return 0;
  38. }
  39. //3)注销驱动:platform_driver_unregister
  40. void __exit btn_drv_exit(void)
  41. {
  42. platform_driver_unregister(&btn_drv);
  43. }
  44. module_init(btn_drv_init);
  45. module_exit(btn_drv_exit);
复制代码

结合上面框架流程图,在 platform_driver_register 注册的时候,一个 platform 驱动程序添加到 platform 核心中。

与添加设备相似, diver_register 函数初始化了几个 device_driver 中的锁,然后调用了 bus_add_driver 函数:

  • 查找与驱动程序相关的总线。如果没有找到该总线,函数返回
  • 根据驱动程序的名字以及相关总线,创建驱动程序 sysfs 目录
  • 获取总线内部的锁,接着遍历所有注册的设备,然后调用这些设备的 match 函数。如果 match 函数成功,如 device 一样,开始绑定过程的剩余步骤。

四、测试

1. 驱动和设备的 Makefile

  1. KERNELDIR := /home/chunn/linux/IMX6ULL/linux/temp/linux-imx-rel_imx_4.1.15_2.1.0_ga_alientek
  2. CURRENT_PATH := $(shell pwd)
  3. obj-m += btn_dev.o
  4. obj-m += btn_drv.o
  5. build: kernel_modules
  6. kernel_modules::
  7. $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
  8. clean:
  9. $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
复制代码

2. 运行

● 加载模块:

将编译出来 btn_dev.ko、btn_drv.ko 拷贝到 rootfs/lib/modules/4.1.15 目录中,然后输入如下


结果一致:

● 查看在系统中的驱动:

  1. ls /sys/bus/platform/devices
复制代码

● 查看在系统中的驱动:

  1. ls /sys/bus/platform/drivers/
复制代码

● 卸载模块:

【参考】

● 公众号:一口Linux ,作者土豆居士

● 《Linux设备驱动程序(第3版)》

● 《【正点原子】I.MX6U嵌入式Linux驱动开发指南V1.5.1》

● Linux内核源码 4.1.15


Linux系统&驱动其它文章:     Linux

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行