前面了解了定时器、驱动传参、地址传参相关知识,这里进行一个总结实验,几个知识点结合在一起来进一步理解和运用相关知识点。
目标
- 通过 ioctl 对定时器进行控制, 分别实现打开定时器、 关闭定时器和设置定时时
间的功能。 - 对实验一的应用程序进行封装, 从而让应用编程人员更好的对设备进行编程。
相关资料参考
高级字符设备进阶-第36章 封装驱动API接口实验
驱动-Linux定时-timer_list
驱动-传参实验-ioctl
上面是相关知识点的介绍,这里就是需要把定时器和驱动传参知识点串联起来,综合运用。
实验
控制程序ioctl.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#define TIME_OPEN _IO('L', 0)
#define TIME_CLOSE _IO('L', 1)
#define TIME_SET _IOW('L', 2, int)
int main(int argc, char *argv[])
{
int fd;
fd = open("/dev/test", O_RDWR, 0777); // 打开test节点
if (fd < 0)
{
printf("file open error \n");
}
ioctl(fd, TIME_SET, 1000);
ioctl(fd, TIME_OPEN);
sleep(3);
ioctl(fd, TIME_SET, 3000);
sleep(7);
ioctl(fd, TIME_CLOSE);
close(fd);
}
代码解读
传递TIME_SET 、TIME_OPEN、TIME_CLOSE 参数值需要对照实际驱动控制程序一起看,这里可以理解为定时器修改、设置、关闭
驱动程序ioctl_timer.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>
#include <linux/uaccess.h>
#include <linux/timer.h>
#define TIMER_OPEN _IO('L', 0)
#define TIMER_CLOSE _IO('L', 1)
#define TIMER_SET _IOW('L', 2, int)
struct device_test
{
dev_t dev_num; // 设备号
int major; // 主设备号
int minor; // 次设备号
struct cdev cdev_test; // cdev
struct class *class; // 类
struct device *device; // 设备
int counter;
};
static struct device_test dev1;
static void fnction_test(struct timer_list *t); // 定义function_test定时功能函数
DEFINE_TIMER(timer_test, fnction_test); // 定义一个定时器
void fnction_test(struct timer_list *t)
{
printk("this is fnction_test===============\n");
mod_timer(&timer_test, jiffies_64 + msecs_to_jiffies(dev1.counter)); // 使用mod_timer函数重新设置定时时间
}
static int cdev_test_open(struct inode *inode, struct file *file)
{
file->private_data = &dev1; // 设置私有数据
return 0;
}
static int cdev_test_release(struct inode *inode, struct file *file)
{
file->private_data = &dev1; // 设置私有数据
return 0;
}
static long cdev_test_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct device_test *test_dev = (struct device_test *)file->private_data; // 设置私有数据
switch (cmd)
{
case TIMER_OPEN:
add_timer(&timer_test); // 添加一个定时器
break;
case TIMER_CLOSE:
del_timer(&timer_test); // 删除一个定时器
break;
case TIMER_SET:
test_dev->counter = arg;
timer_test.expires = jiffies_64 + msecs_to_jiffies(test_dev->counter); // 设置定时时间
break;
default:
break;
}
return 0;
}
/*设备操作函数*/
struct file_operations cdev_test_fops = {
.owner = THIS_MODULE, // 将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块
.open = cdev_test_open,
.release = cdev_test_release,
.unlocked_ioctl = cdev_test_ioctl,
};
static int __init timer_dev_init(void) // 驱动入口函数
{
/*注册字符设备驱动*/
int ret;
/*1 创建设备号*/
ret = alloc_chrdev_region(&dev1.dev_num, 0, 1, "alloc_name"); // 动态分配设备号
if (ret < 0)
{
goto err_chrdev;
}
printk("alloc_chrdev_region is ok\n");
dev1.major = MAJOR(dev1.dev_num); // 获取主设备号
dev1.minor = MINOR(dev1.dev_num); // 获取次设备号
printk("major is %d \r\n", dev1.major); // 打印主设备号
printk("minor is %d \r\n", dev1.minor); // 打印次设备号
/*2 初始化cdev*/
dev1.cdev_test.owner = THIS_MODULE;
cdev_init(&dev1.cdev_test, &cdev_test_fops);
/*3 添加一个cdev,完成字符设备注册到内核*/
ret = cdev_add(&dev1.cdev_test, dev1.dev_num, 1);
if (ret < 0)
{
goto err_chr_add;
}
/*4 创建类*/
dev1.class = class_create(THIS_MODULE, "test");
if (IS_ERR(dev1.class))
{
ret = PTR_ERR(dev1.class);
goto err_class_create;
}
/*5 创建设备*/
dev1.device = device_create(dev1.class, NULL, dev1.dev_num, NULL, "test");
if (IS_ERR(dev1.device))
{
ret = PTR_ERR(dev1.device);
goto err_device_create;
}
return 0;
err_device_create:
class_destroy(dev1.class); // 删除类
err_class_create:
cdev_del(&dev1.cdev_test); // 删除cdev
err_chr_add:
unregister_chrdev_region(dev1.dev_num, 1); // 注销设备号
err_chrdev:
return ret;
}
static void __exit timer_dev_exit(void) // 驱动出口函数
{
/*注销字符设备*/
unregister_chrdev_region(dev1.dev_num, 1); // 注销设备号
cdev_del(&dev1.cdev_test); // 删除cdev
device_destroy(dev1.class, dev1.dev_num); // 删除设备
class_destroy(dev1.class); // 删除类
}
module_init(timer_dev_init);
module_exit(timer_dev_exit);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("topeet");
代码解读
驱动程序从字符设备到现在,基本思路、必备技能知识点:
- 结构体封装设备号、主设备号、次设备号、字符设备、类、设备
- 私有数据结构体
- 创建设备号
- 初始化字符设备
- 添加字符设备到内核
- 创建类
- 创建设备
- 定时器的创建、修改、删除
这些基础知识点通过各个案例、实验 不断深化基础,这里不再讲解
Makefile 编译文件
#!/bin/bash
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
obj-m += ioctl_timer.o
KDIR :=/home/wfc123/Linux/rk356x_linux/kernel
PWD ?= $(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
编译脚本
- make 脚本直接编译驱动C程序代码生成 驱动文件:make
- 针对控制程序,编译生成控制脚本
aarch64-linux-gnu-gcc -o ioctl ioctl.c -static
实际测试验证
驱动程序和控制程序copy 到主板系统目录下面,如下
- 先执行驱动程序,让驱动生成字符设备、节点、设备
- 执行控制程序,查看测试结果
总结
- 这里针对前面的知识点:定时器、驱动传参、api 封装 来对知识点的进一步掌握。
- 核心需要了解的还是驱动传参的逻辑和业务,后面有相关功能都可以自己定制了。