- 把公共的部分抽出来,写成leddrv.c
- 这些公共部分对任何一个开发板来说都是一样的
- 起承上启下的作用
- 应用程序通过它来访问驱动程序,它向下调用各个单板的代码(board_a.c …)
- 框架
- 每一个驱动程序都会对应一个file_operations结构体,内核中有那么多的结构体,怎么才能找到具体哪个结构体呢?
- 应用程序要访问驱动程序,需要打开设备节点,设备节点里有主设备号;
- 根据设备节点的主设备号在内核中找到file_operations结构体
- 所以在注册file_operations时就需要提供主设备号
- 对于LED驱动,需要怎样接口?
- 通过设备节点的次设备号,测试程序可以决定去操作哪一盏灯
- 在LED的open函数中配置LED引脚为输出(为什么不在入口函数中配置? 因为安装了驱动程序并不表示会用到驱动程序)
- LED 驱动要怎么写,才能支持多个板子?
- 分层
- 直接调用单板提供的初始化、操作函数
- 每一个单板都实现同名的初始化、操作函数
- 给boardA用时,只编译leddrv.c和boardA.c两个;同理,给boardB使用时依然
- 以面向对象的思想,抽象出一个结构体
- 上层leddrv.c代码会调用底层board提供的结构体,以及调用结构体里面的函数
- leddrv.c ```c
/ 1. 确定主设备号 / static int major = 0; static struct class led_class; struct led_operations p_led_opr; / 底层board的LED结构体 /
define MIN(a, b) (a < b ? a : b)
/ 3. 实现对应的open/read/write等函数,填入file_operations结构体 / static ssizet leddrvread (struct file file, char __user buf, sizet size, lofft *offset) { printk(“%s %s line %d\n”, _FILE, __FUNCTION, __LINE); 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); /* 由file得到node */
int minor = iminor(inode); /* 再由node得到次设备号 */
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
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); / 根据node得到次设备号 /
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
/* 根据次设备号初始化LED */
p_led_opr->init(minor);
return 0;
}
static int leddrvclose (struct inode node, struct file file) { printk(“%s %s line %d\n”, FILE, FUNCTION, __LINE); 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; int i;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
major = register_chrdev(0, "100ask_led", &led_drv); /* /dev/led */
led_class = class_create(THIS_MODULE, "100ask_led_class");
err = PTR_ERR(led_class);
if (IS_ERR(led_class)) {
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
unregister_chrdev(major, "100ask_led");
return -1;
}
/* 创建多个LED,通过次设备号区分 */
for (i = 0; i < LED_NUM; i++)
device_create(led_class, NULL, MKDEV(major, i), NULL, "100ask_led%d", i); /* /dev/100ask_led0,1,... */
/* device_create 可变参数; "100ask_led%d", i */
p_led_opr = get_board_led_opr(); /* 获取board的led操作结构体 */
return 0;
}
/ 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数 / static void exit led_exit(void) { int i; printk(“%s %s line %d\n”, FILE, FUNCTION, LINE__);
for (i = 0; i < LED_NUM; i++)
device_destroy(led_class, MKDEV(major, i)); /* /dev/100ask_led0,1,... */
device_destroy(led_class, MKDEV(major, 0));
class_destroy(led_class);
unregister_chrdev(major, "100ask_led");
}
/ 7. 其他完善:提供设备信息,自动创建设备节点 / module_init(led_init); module_exit(led_exit);
MODULE_LICENSE(“GPL”);
- 底层board向上提供led_operations结构体
```c
struct led_operations {
int (*init) (int which); /* 初始化LED, which-哪个LED */
int (*ctl) (int which, char status); /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
};
/* 在board中提供函数给上层获取自身实现的结构体 */
struct led_operations *get_board_led_opr(void);
- 将两个.c文件编译成一个.ko文件
```makefile
参考内核源码drivers/char/ipmi/Makefile
要想把a.c, b.c编译成ab.ko, 可以这样指定:
ab-y := a.o b.o
obj-m += ab.o
leddrv.c board_demo.c 编译成 100ask.ko
100ask_led-y := leddrv.o board_demo.o obj-m += 100ask_led.o ```