这是第五篇第5章的课后作业,尝试实现
实现读 LED 状态的功能:涉及 APP 和驱动。
1 LED 驱动能支持多个板子的基础: 分层思想
参考分层思想
①把驱动拆分为通用的框架(leddrv.c)、具体的硬件操作(board_X.c):
②以面向对象的思想,改进代码, 抽象出一个结构体:后面需要修改
struct led_operations {
int (*init) (int which); // 初始化LED,which是哪一个LED
int (*ctl) (int which, int status); // 控制LED,which-哪一个LED,status-1亮,0灭
};
有初始化函数、有控制函数,每个单板相关的 board_X.c 实现自己的 led_operations 结构体,供上层的 leddrv.c 调用
2 写代码
驱动程序分为上下两层: led_drv.c、 board_demo.c。
led_drv.c 负责注册file_operations 结构体,它的 open/write 成员会调用 board_demo.c 中提供的硬件 led_opr 中的对应函数。
1 把 LED 的操作抽象出一个 led_operations 结构体
首先看看 led_opr.h,它定义了一个 led_operations 结构体,把LED的操作抽象为这个结构体:
分别是写操作和读操作
#ifndef LED_OPERATIONS_H
#define LED_OPERATIONS_H
struct led_operations {
int (*init) (int which); // 初始化LED,which是哪一个LED
int (*ctl_write) (int which, char status); // 控制LED,which-哪一个LED,status-1亮,0-灭
int (*ctl_read) (int which); // 控制LED,which-哪一个LED,返回值1-亮,0-灭
};
// 返回结构体指针
struct led_operations* get_board_led_operations(void);
#endif
2 驱动程序的上层: file_operations 结构体
上层是 leddrv.c,它的核心是 file_operations 结构体,首先看看入口函数:
// 4把 file_operations 结构体告诉内核: register_chrdev
// 5谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数
static int __init led_init(void)
{
int err, i;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
// 注册led_drv,返回主设备号
major = register_chrdev(0, "winter_led", &led_drv); /* /dev/led */
// 创建class
led_class = class_create(THIS_MODULE, "led_class");
err = PTR_ERR(led_class);
if (IS_ERR(led_class)) {
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
unregister_chrdev(major, "led_class");
return -1;
}
// 创建device
// 根据次设备号访问多个LED
// device_create(led_class, NULL, MKDEV(major, 0), NULL, "winter_led0"); /* /dev/winter_led0 */
// device_create(led_class, NULL, MKDEV(major, 1), NULL, "winter_led1"); /* /dev/winter_led1 */
for (i = 0; i < LED_NUM; i++)
{
device_create(led_class, NULL, MKDEV(major, i), NULL, "winter_led%d", i);
}
// 入口函数获得结构体指针
p_led_operations = get_board_led_operations();
return 0;
}
(1)通过register_chrdev函数向内核注册一个 file_operations 结构体
(2)通过get_board_led_operations函数从底层硬件相关的代码 board_demo.c 中获得 led_operaions结构体
和之前的一样
再来看看 leddrv.c 中 file_operations 结构体的成员函数:
因为是读操作,所以要在read函数中做文章,write函数、open函数不变
// 3 实现对应的 drv_open/drv_read/drv_write 等函数,填入 file_operations 结构体
// res = read(fd, val, 1)
static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
int err;
struct inode* node;
int minor;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
// 将kernel_buf区的数据拷贝到用户区数据status中,即从内核kernel_buf中读数据
err = copy_to_user(buf, status, MIN(1024, size));
// 根据次设备号和status控制LED
node = file_inode(file);
minor = iminor(node);
p_led_operations->ctl_read(minor);
return MIN(1024, size);
}
// 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;
struct inode* node;
int minor;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
// 把用户区的数据buf拷贝到内核区status,即向写到内核status中写数据
err = copy_from_user(status, buf, MIN(1024, size));
// 根据次设备号和status控制LED
node = file_inode(file);
minor = iminor(node);
p_led_operations->ctl_write(minor, status);
return MIN(1024, size);
}
static int led_drv_open (struct inode *node, struct file *file)
{
int minor;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
// 得到次设备号
minor = iminor(node);
// 根据次设备号初始化LED
p_led_operations->init(minor);
return 0;
}
static int led_drv_close (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,
};
说白了以前是一个drv.c文件,在里面定义了file_operation结构体、并实现了对应的read/open等函数,再在init函数中注册file_operaions结构体。
现在是将drv.c抽象抽象成两层:
(1)底层抽象为led_operations结构体,这里面主要有init初始化属性(函数指针)和ctl控制属性(函数指针),然后针对不同的板子,去实现对应的初始化函数和控制函数;
(2)上层中依旧保留原来大的框架,在初始化函数(open函数)中利用led_operations结构体指针调用init初始化属性,在控制函数(read/write函数)中利用led_operations结构体指针调用ctl控制属性。
从而实现具体板子的功能业务和框架的分离。
led.drv.c
/*************************************************************************
> File Name: led.drv.c
> Author: Winter
> Created Time: Sun 07 Jul 2024 12:35:19 AM EDT
************************************************************************/
#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_operations.h"
#define LED_NUM 2
// 1确定主设备号,也可以让内核分配
static int major = 0; // 让内核分配
static struct class *led_class;
struct led_operations* p_led_operations;
char status[1024];
#define MIN(a, b) (a < b ? a : b)
// 3 实现对应的 drv_open/drv_read/drv_write 等函数,填入 file_operations 结构体
// res = read(fd, val, 1)
static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
int err;
struct inode* node;
int minor;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
// 将kernel_buf区的数据拷贝到用户区数据status中,即从内核kernel_buf中读数据
err = copy_to_user(buf, status, MIN(1024, size));
// 根据次设备号和status控制LED
node = file_inode(file);
minor = iminor(node);
p_led_operations->ctl_read(minor);
return MIN(1024, size);
}
// 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;
struct inode* node;
int minor;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
// 把用户区的数据buf拷贝到内核区status,即向写到内核status中写数据
err = copy_from_user(status, buf, MIN(1024, size));
// 根据次设备号和status控制LED
node = file_inode(file);
minor = iminor(node);
p_led_operations->ctl_write(minor, status);
return MIN(1024, size);
}
static int led_drv_open (struct inode *node, struct file *file)
{
int minor;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
// 得到次设备号
minor = iminor(node);
// 根据次设备号初始化LED
p_led_operations->init(minor);
return 0;
}
static int led_drv_close (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 结构体告诉内核: register_chrdev
// 5谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数
static int __init led_init(void)
{
int err, i;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
// 注册led_drv,返回主设备号
major = register_chrdev(0, "winter_led", &led_drv); /* /dev/led */
// 创建class
led_class = class_create(THIS_MODULE, "led_class");
err = PTR_ERR(led_class);
if (IS_ERR(led_class)) {
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
unregister_chrdev(major, "led_class");
return -1;
}
// 创建device
// 根据次设备号访问多个LED
// device_create(led_class, NULL, MKDEV(major, 0), NULL, "winter_led0"); /* /dev/winter_led0 */
// device_create(led_class, NULL, MKDEV(major, 1), NULL, "winter_led1"); /* /dev/winter_led1 */
for (i = 0; i < LED_NUM; i++)
{
device_create(led_class, NULL, MKDEV(major, i), NULL, "winter_led%d", i);
}
// 入口函数获得结构体指针
p_led_operations = get_board_led_operations();
return 0;
}
// 6有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev
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));
}
class_destroy(led_class);
// 卸载
unregister_chrdev(major, "winter_led");
}
// 7其他完善:提供设备信息,自动创建设备节点: class_create,device_create
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
board_demo.c
#include <linux/gfp.h>
#include "led_operations.h"
// init函数
static int board_demo_led_init(int which)
{
printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
return 0;
}
// ctl_write函数
static int board_demo_led_ctl_write(int which, char status[1024])
{
char* str = status;
printk("%s %s line %d, led %d, %s\n",
__FILE__, __FUNCTION__, __LINE__, which, strcmp(str, "on") == 0 ? "on" : "off");
return 0;
}
// ctl_write函数
static int board_demo_led_ctl_read(int which)
{
printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
return 0;
}
static struct led_operations board_demo_led_operations = {
.init = board_demo_led_init,
.ctl_write = board_demo_led_ctl_write,
.ctl_read = board_demo_led_ctl_read,
};
// 返回结构体
struct led_operations* get_board_led_operations(void)
{
return &board_demo_led_operations;
}
led_operations.h
#ifndef LED_OPERATIONS_H
#define LED_OPERATIONS_H
struct led_operations {
int (*init) (int which); // 初始化LED,which是哪一个LED
int (*ctl_write) (int which, char status); // 控制LED,which-哪一个LED,status-1亮,0-灭
int (*ctl_read) (int which); // 控制LED,which-哪一个LED,返回值1-亮,0-灭
};
// 返回结构体指针
struct led_operations* get_board_led_operations(void);
#endif
led_drv_test.c
/*************************************************************************
> File Name: hello_test.c
> Author: Winter
> Created Time: Sun 07 Jul 2024 01:39:39 AM EDT
************************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
/*
* ./led_drv /dev/winter_led0 -w on
* ./led_drv /dev/winter_led0 -w off
* ./led_drv /dev/winter_led0 -r
*/
int main(int argc, char **argv)
{
int fd;
int len;
char buf[1024];
/* 1. 判断参数 */
if (argc < 3)
{
printf("Usage: %s <dev> -w <on | off>\n", argv[0]);
printf(" %s <dev> -r\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], "-w")) && (0 == strcmp(argv[3], "on")))
{
write(fd, argv[3], strlen(argv[3]) + 1);
}
else if ((0 == strcmp(argv[2], "-w")) && (0 == strcmp(argv[3], "off")))
{
write(fd, argv[3], strlen(argv[3]) + 1);
}
else
{
len = read(fd, buf, 1024);
buf[1023] = '\0';
printf("APP read : %s\n", buf);
}
close(fd);
return 0;
}
Makefile
# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH, 比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH, 比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
# 请参考各开发板的高级用户使用手册
KERN_DIR = /home/book/100ask_stm32mp157_pro-sdk/Linux-5.4
all:
make -C $(KERN_DIR) M=`pwd` modules
$(CROSS_COMPILE)gcc -o led_drv_test led_drv_test.c
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
rm -f led_drv_test
# 参考内核源码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
winter_led-y := led_drv.o board_demo.o
obj-m += winter_led.o
执行
make
3 测试
在开发板挂载 Ubuntu 的NFS目录
mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs/ /mnt
安装驱动
insmod winter_led.ko
执行打开灯/读
./led_drv_test /dev/winter_led0 -w on
./led_drv_test /dev/winter_led0 -r
./led_drv_test /dev/winter_led0 -w off
./led_drv_test /dev/winter_led0 -r
./led_drv_test /dev/winter_led1 -w on
./led_drv_test /dev/winter_led1 -r
./led_drv_test /dev/winter_led1 -w off
./led_drv_test /dev/winter_led1 -r
结果