Elegance’s the only beauty that never fades.
文章目录
一、Platform平台初探
1.1、platform平台与platform_device/platform_driver关系
在上一节通过面向对象的思想,实现了上下分层和左右分离。
- 上下分层 - 上层驱动与下层硬件分开
- 左右分离 - 把下层的硬件再分成硬件资源(指定引脚)与硬件操作(引脚配置)
其实这个时候已经有platform的味道了,只是没有说明而已。我们把上层驱动抽象成一个platform_driver结构体,硬件资源和硬件操作抽象成platform_device结构体,这样就构成了Platform平台。
即,
- platform_device来指定资源
- platform_driver来指定驱动
引入 platform_device和platform_driver, 你可能会说这似乎看起来跟上一节也差不多嘛,不过是加了一层platform的外衣罢了。它的作用无非也是将“资源”与“驱动”分离开来,当修改引脚只需要修改资源,而驱动不需要动。莫慌,莫慌,循序渐进嘛,做这一切都是为了给后面的主角做铺垫 - 设备树。而设备树跟platform思想及调用手段是那么的相像,我们怎么能有理由先去了解一下Platform呢?
1.2、platform平台虚拟总线
为了更好的管理Platform这个平台,Linux引入了总线(bus)、驱动(driver)和设备(device)模型。 可以类比I2C、 SPI、 USB 等总线,编写相应的驱动文件,挂载相应的设备。
但是在 SOC 中有些外设是没有总线这个概念的,但是又要使用总线、驱动和设备模型该怎么办呢?为了解决此问题, Linux 提出了 platform 这个虚拟总线的概念。
1.3、platform平台匹配规则
总线有了,我们可以把设备和驱动程序都挂上去(注册进内核),但是我们的设备可不止一个呀,同样驱动也不止一个呀。那么问题也就来了, platform_device和platform_driver该如何对应呢,或者说该如何进行匹配呢?我们看图学习。
1.3.1 匹配规则
- 最先比较: platform_device. driver_override 和 platform_driver.driver.name
- 可以设置 platform_device 的 driver_override,强制选择某个 platform_driver。
- 然后比较: platform_device. name 和 platform_driver.id_table[i].name
- Platform_driver.id_table 是“platform_device_id”指针,表示该 drv 支持若干个 device,它里面列出了各个 device 的{.name, .driver_data}, 其中的“name”表示该 drv 支持的设备的名字, driver_data 是些提供给该 device 的私有数据。
- 最后比较: platform_device.name 和 platform_driver.driver.name
- platform_driver.id_table 可能为空,这时可以根据 platform_driver.driver.name 来寻找同名的 platform_device。
1.3.2 上面只是系统规定的匹配顺序,但是实际使用中匹配的常用顺序是
- ①、直接比较驱动和设备的 name 字段,毕竟这种方式最简单了;
- ②、然后是id_table的名字;
- ③、最后才是driver_override。
当设备和驱动匹配成功以后,platform_driver里面的probe 成员函数就会执行,probe成员函数执行的内容便是我们熟悉的驱动程序,这样便把驱动和设备对应了起来!
1.4、platform函数调用关系记录(无需彻底深入,大概搞懂即可)
platform_device_register
platform_device_add
device_add
bus_add_device // 放入链表
bus_probe_device // probe 枚举设备,即找到匹配的(dev, drv)
device_initial_probe
__device_attach
bus_for_each_drv(...,__device_attach_driver,...)
__device_attach_driver
driver_match_device(drv, dev) // 是否匹配
driver_probe_device // 调用 drv 的 probe
platform_driver_register
__platform_driver_register
driver_register
bus_add_driver // 放入链表
driver_attach(drv)
bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
__driver_attach
driver_match_device(drv, dev) // 是否匹配
driver_probe_device // 调用 drv 的 probe
二、编写程序
编程步骤如下:
- 分配/设置/注册 platform_device 结构体
- 在里面定义所用资源,指定设备名字。
- 分配/设置/注册 platform_driver 结构体
- 在其中的 probe 函数里,分配/设置/注册 file_operations 结构体,
- 并从 platform_device 中确实所用硬件资源。
- 指定 platform_driver 的名字。
程序框架如下:
平台设备platform_deviceboard_A_led.c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/platform_device.h>
#include "led_resource.h"
static void led_dev_release(struct device *dev)
{
}
static struct resource resources[] = {
{
.start = GROUP_PIN(3,1),
.flags = IORESOURCE_IRQ,
.name = "led_pin",
},
{
.start = GROUP_PIN(5,8),
.flags = IORESOURCE_IRQ,
.name = "led_pin",
},
};
static struct platform_device board_A_led_dev = {
.name = "led",
.num_resources = ARRAY_SIZE(resources), //得到资源个数
.resource = resources, //具体资源信息定义在了结构体里
.dev = {
.release = led_dev_release,//必须加上release不然unregister的时候会有警告!
},
};
static int __init led_dev_init(void)
{
int err;
err = platform_device_register(&board_A_led_dev);
return 0;
}
static void __exit led_dev_exit(void)
{
platform_device_unregister(&board_A_led_dev);
}
module_init(led_dev_init);
module_exit(led_dev_exit);
MODULE_LICENSE("GPL");
平台设备文件需要说明的是:
- 该文件单独编译成.ko文件,故需要入口函数、出口函数
- 在该文件中指定资源,所以资源是动态的。(操作几个引脚,以及操作哪几个引脚事先都已经不知道了)
- 通过platform_device结构体board_A_led_dev中的name成员决定与哪个驱动配对!
平台设备文件需要说明的是:
- 该文件单独编译成.ko文件,故需要入口函数、出口函数
- 在该文件中指定资源,所以资源是动态的。(操作几个引脚,以及操作哪几个引脚事先都已经不知道了)
平台驱动文件chipY_gpio.c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/platform_device.h>
#include "led_opr.h"
#include "leddrv.h"
#include "led_resource.h"
static int g_ledpins[100];
static int g_ledcnt = 0;
static int board_demo_led_init (int which) /* 初始化LED, which-哪个LED */
{
//printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
printk("init gpio: group %d, pin %d\n", GROUP(g_ledpins[which]), PIN(g_ledpins[which]));
switch(GROUP(g_ledpins[which]))
{
case 0:
{
printk("init pin of group 0 ...\n");
break;
}
case 1:
{
printk("init pin of group 1 ...\n");
break;
}
case 2:
{
printk("init pin of group 2 ...\n");
break;
}
case 3:
{
printk("init pin of group 3 ...\n");
break;
}
}
return 0;
}
static int board_demo_led_ctl (int which, char status) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
{
//printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, which, status ? "on" : "off");
printk("set led %s: group %d, pin %d\n", status ? "on" : "off", GROUP(g_ledpins[which]), PIN(g_ledpins[which]));
switch(GROUP(g_ledpins[which]))
{
case 0:
{
printk("set pin of group 0 ...\n");
break;
}
case 1:
{
printk("set pin of group 1 ...\n");
break;
}
case 2:
{
printk("set pin of group 2 ...\n");
break;
}
case 3:
{
printk("set pin of group 3 ...\n");
break;
}
}
return 0;
}
static struct led_operations board_demo_led_opr = {
.init = board_demo_led_init,
.ctl = board_demo_led_ctl,
};
static int chip_demo_gpio_probe(struct platform_device *pdev)
{
struct resource *res;
int i = 0;
while (1)
{
/* 获取第i个资源 */
res = platform_get_resource(pdev, IORESOURCE_IRQ, i++);
if (!res)
break;
/* 记录引脚 */
g_ledpins[g_ledcnt] = res->start;
/* 调用device_create,注意不能直接调用device_create,因为需要用到device_class,所以封装了led_class_create_device函数*/
led_class_create_device(g_ledcnt);
g_ledcnt++;
}
return 0;
}
static int chip_demo_gpio_remove(struct platform_device *pdev)
{
struct resource *res;
int i = 0;
while (1)
{
res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
if (!res)
break;
/* 调用device_destroy*/
led_class_destroy_device(i);
i++;
g_ledcnt--;
}
return 0;
}
static struct platform_driver chip_demo_gpio_driver = {
.probe = chip_demo_gpio_probe,
.remove = chip_demo_gpio_remove,
.driver = {
.name = "led",
},
};
static int __init chip_demo_gpio_drv_init(void)
{
int err;
err = platform_driver_register(&chip_demo_gpio_driver);
register_led_operations(&board_demo_led_opr);
return 0;
}
static void __exit chip_demo_gpio_drv_exit(void)
{
platform_driver_unregister(&chip_demo_gpio_driver);
}
module_init(chip_demo_gpio_drv_init);
module_exit(chip_demo_gpio_drv_exit);
MODULE_LICENSE("GPL");
平台驱动文件需要说明的
- 该文件单独编译成.ko文件,故需要入口函数、出口函数
- 通过platform_driver结构体chip_demo_gpio_driver中的driver.name成员决定与哪个设备配对!
- probe函数获取资源,记录引脚,同时创建设备。
- 注意不能直接调用device_create,因为需要用到device_class,所以在驱动文件
leddrv.c
中封装了led_class_create_device函数。销毁led_class_destroy_device也是同样的道理! - 这样也就意味着,
chipY_gpio.c
依赖leddrv.c
文件,故需要先加载leddrv.c
文件
- 注意不能直接调用device_create,因为需要用到device_class,所以在驱动文件
- 还需要注意的一点,在平台驱动加载的入口函数里,获取了led_operations结构体board_demo_led_opr,这一次不像上一节在驱动文件
leddrv.c
中获得。led_operations结构体定义平台驱动chipY_gpio.c
中,如果仍在leddrv.c
中获得的话,也就意味着leddrv.c
依赖chipY_gpio.c
,这两个互相依赖,那岂不是会出错了!!!- 由于不再需要get_board_led_opr函数,所以本例程直接删除了!
- 由于驱动文件需要led_operations结构体,所以最好的办法就是在驱动文件
leddrv.c
中创建一个类似上面设备创建和销毁的注册函数,于是就有了register_led_operations函数!
这里再补充一下关于Platform_device中指定资源的两种方式
- ①、构造resource结构体(必须构造 start flags name 等成员)
- start 表示起始地址
- flags表示是哪一类资源
- ②、在私有数据中存放(可以存放任何类型的数据,自由度比较大)
驱动文件leddrc.c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include "led_opr.h"
/* 1. 确定主设备号 */
static int major = 0;
static struct class *led_class;
struct led_operations *p_led_opr;
#define MIN(a, b) (a < b ? a : b)
void led_class_create_device(int minor)
{
device_create(led_class, NULL, MKDEV(major, minor), NULL, "led%d", minor); /* /dev/led0,1,... */
}
void led_class_destroy_device(int minor)
{
device_destroy(led_class, MKDEV(major, minor));
}
void register_led_operations(struct led_operations *opr)
{
p_led_opr = opr;
}
/*该程序是个ko文件,别的ko需要用到该程序里的函数时,需要使用EXPORT_SYMBOL导出,也就意味着产生了依赖关系!*/
EXPORT_SYMBOL(led_class_create_device);
EXPORT_SYMBOL(led_class_destroy_device);
EXPORT_SYMBOL(register_led_operations);
/* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */
static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
return 0;
}
/* write(fd, &val, 1); */
static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
int err;
char status;
struct inode *inode = file_inode(file);
int minor = iminor(inode);
err = copy_from_user(&status, buf, 1);
/* 根据次设备号和status控制LED */
p_led_opr->ctl(minor, status);
return 1;
}
static int led_drv_open (struct inode *node, struct file *file)
{
int minor = iminor(node);
/* 根据次设备号初始化LED */
p_led_opr->init(minor);
return 0;
}
static int led_drv_close (struct inode *node, struct file *file)
{
return 0;
}
/* 2. 定义自己的file_operations结构体 */
static struct file_operations led_drv = {
.owner = THIS_MODULE,
.open = led_drv_open,
.read = led_drv_read,
.write = led_drv_write,
.release = led_drv_close,
};
/* 4. 把file_operations结构体告诉内核:注册驱动程序 */
/* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
static int __init led_init(void)
{
int err;
major = register_chrdev(0, "led", &led_drv); /* /dev/led */
led_class = class_create(THIS_MODULE, "led_class");
err = PTR_ERR(led_class);
if (IS_ERR(led_class)) {
unregister_chrdev(major, "led");
return -1;
}
return 0;
}
/* 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数 */
static void __exit led_exit(void)
{
class_destroy(led_class);
unregister_chrdev(major, "led");
}
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
驱动程序leddrv.c
需要说明的
- 该程序是个ko文件,别的ko需要用到该程序里的函数时,需要使用EXPORT_SYMBOL导出!同时需要在对应.h文件中声明!
Makefile
KERN_DIR = /home/clay/linux/qemu/kernel/100ask_imx6ull-qemu/linux-4.9.88
all:
make -C $(KERN_DIR) M=`pwd` modules
$(CROSS_COMPILE)gcc -o ledtest ledtest.c
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
rm -f ledtest
# 编译生成3个.ko文件
obj-m += leddrv.o chipY_gpio.o board_A_led.o
Makefile需要提醒的
- 最后一行,因为这一次是编译成3个ko文件!
关于应用程序和相应的.h文件,没有什么需要说的,一并附在下面
leddrv.h
#ifndef _LEDDRV_H
#define _LEDDRV_H
#include "led_opr.h"
void led_class_create_device(int minor);
void led_class_destroy_device(int minor);
void register_led_operations(struct led_operations *opr);
#endif /* _LEDDRV_H */
led_opr.h
#ifndef _LED_OPR_H
#define _LED_OPR_H
struct led_operations {
int (*init) (int which); /* 初始化LED, which-哪个LED */
int (*ctl) (int which, char status); /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
};
#endif
ledtest.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
/*
* ./ledtest /dev/led0 on
* ./ledtest /dev/led0 off
*/
int main(int argc, char **argv)
{
int fd;
char status;
/* 1. 判断参数 */
if (argc != 3)
{
printf("Usage: %s <dev> <on | off>\n", argv[0]);
return -1;
}
/* 2. 打开文件 */
fd = open(argv[1], O_RDWR);
if (fd == -1)
{
printf("can not open file %s\n", argv[1]);
return -1;
}
/* 3. 写文件 */
if (0 == strcmp(argv[2], "on"))
{
status = 1;
write(fd, &status, 1);
}
else
{
status = 0;
write(fd, &status, 1);
}
close(fd);
return 0;
}
三、运行程序
编译程序没有问题后,运行qemu虚拟开发板,并做好准备工作!
- 拷贝所有的.ko和ledtest到NFS中
cp *.ko ledtest ~/linux/qemu/NFS/
- 在qemu终端,加载ko文件,需要注意的是,因为有依赖关系,所以顺序需要确定!board_A_led.ko文件没有依赖,所以在前在后无所谓!而chipY_gpio.ko依赖leddrv.ko,故需要先加载leddrv.ko!
insmod board_A_led.ko
insmod leddrv.ko
insmod chipY_gpio.ko
在qemu中加载最后一个模块时,会出现下面的提示信息,但是ctrl+c之后,似乎测试还是可以用的,不知道是怎么回事。知道的朋友,可以在下面留言一起探讨!
- 在qemu终端,运行应用程序打开LED0
./ledtest /dev/led0 on
led0对应的就是GPIO3_IO1引脚。
- 在qemu终端,运行应用程序打开LED1
./ledtest /dev/led1 on
整体程序的框架,第一次接触确实蛮难的,但是没关系,静下心来慢慢理,还是可以理通的,嘻嘻,继续加油咯,下一篇就到了设备树!