LED虚拟杂项字符设备驱动
2025-07-24
14
0
该驱动是在嵌入式LINUX驱动的基础上修改而来,没有进行真正的寄存器操作,只是为了验证LINUX驱动的开发。
vled.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <asm/io.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/ioport.h>
#include <linux/of.h>
#include <linux/uaccess.h>
static void __iomem *led_base; //led 寄存器基地址
//打开设备触发的服务函数
static int led_open(struct inode * inode, struct file * filp)
{
printk("dev is open!");
return 0;
}
//写设备触发的服务函数 file:设备 buf:数据缓存区 count:数据个数单位字节
static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
int val,ret;
//把 buff 缓存数据拷贝到 val 地址空间
ret = copy_from_user(&val, buf, count);
//把 val 的值写进 led_base 寄存器
//iowrite32(val, led_base);
printk("led : Write 0x%x to 0x%p\n", val, led_base);
return 0;
}
//LED 函数接口结构体
static const struct file_operations led_fops =
{
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
};
//LED 设备结构体
static struct miscdevice led_dev=
{
.minor = MISC_DYNAMIC_MINOR,
.name = "led_dev", // /dev/目录下的设备节点名
.fops = &led_fops,
};
//LED 设备初始化
static int led_init(void)
{
int ret;
//地址映射:把物理地址转换为虚拟地址
led_base = 0;//ioremap(0x41200000, 4);
printk("LED: Access address to device is:0x%x\n", (unsigned int)led_base);
//注册设备到/dev/目录
ret = misc_register(&led_dev);
if(ret)
{
printk("led:[ERROR] Misc device register failed.\n");
return ret;
}
printk("Module init complete!\n");
return 0;
}
//LED 设备退出
static void led_exit(void)
{
printk("Module exit!\n");
//iounmap(led_base); //取消物理地址的映射
misc_deregister(&led_dev); //删除/dev/目录下的设备节点
}
module_init(led_init); //模块初始化接口
module_exit(led_exit); //模块推出接口
MODULE_AUTHOR("osrc@ ");
MODULE_DESCRIPTION("ledDriver");
MODULE_ALIAS("It's only a test");
MODULE_LICENSE("Dual BSD/GPL");
Makefile:
obj-m += vled.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
应用测试程序:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int i;
int fd;
int val=1;
fd = open("/dev/led_dev", O_RDWR); //打开设备
if (fd <= 0)
{
printf("open error!\n" );
return 0;
}
//LED 流水灯
while(1)
{
for(i=0;i<4;i++)
{
val = (1<<i);
write(fd,&val,4); //把 val 的值写到 fd 设备中,大小为 4 字节
sleep(1);
}
}
return 0;
}
编译与安装
编译驱动vled
zzmt@zzmt-virtual-machine:~/Desktop/vled$ make
make -C /lib/modules/6.8.0-64-generic/build M=/home/zzmt/Desktop/vled modules
make[1]: Entering directory '/usr/src/linux-headers-6.8.0-64-generic'
warning: the compiler differs from the one used to build the kernel
The kernel was built by: x86_64-linux-gnu-gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
You are using: gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
make[1]: Leaving directory '/usr/src/linux-headers-6.8.0-64-generic'
编译应用程序
zzmt@zzmt-virtual-machine:~/Desktop/vled$ gcc ledapp.c -o ledapp.exe
安装驱动程序
zzmt@zzmt-virtual-machine:~/Desktop/vled$ sudo insmod vled.ko
[sudo] password for zzmt:
zzmt@zzmt-virtual-machine:~/Desktop/vled$ lsmod | grep vled
vled 12288 0
运行应用程序
zzmt@zzmt-virtual-machine:~/Desktop/vled$ sudo ./ledapp.exe
查看内核消息
zzmt@zzmt-virtual-machine:~/Desktop/vled$ sudo dmesg | tail -n 30
...
[ 542.741524] vled: loading out-of-tree module taints kernel.
[ 542.741530] vled: module verification failed: signature and/or required key missing - tainting kernel
[ 542.742050] LED: Access address to device is:0x0
[ 542.742280] Module init complete!
[ 582.939465] dev is open!
[ 582.939471] led : Write 0x1 to 0x0000000000000000
[ 583.939963] led : Write 0x2 to 0x0000000000000000
[ 584.940281] led : Write 0x4 to 0x0000000000000000