创建一个简单的字符设备驱动程序。
本文命令的运行基本上都需要root权限,使用root账号,或者在命令前面加上sudo。
如果你使用ssh远程连接的服务器进行代码编写。那么不要在root用户下创建文件或者文件夹。这会导致你ssh连接vscode编写代码的权限问题。可以在普通用户创建好所有的文件,然后编写。
(资料图片仅供参考)
hello_driver.c
#include #include #include #include #include #include #include #include #include dev_t hello_devid;struct cdev hello_cdev;int hello_major = 0;int hello_minor; uint8_t kernel_buffer[1024] = {0};static struct class *hello_class; static int hello_world_open(struct inode * inode, struct file * file){printk("hello_world_open\r\n");return 0;} static int hello_world_release (struct inode * inode, struct file * file){printk("hello_world_release\r\n");return 0;} static ssize_t hello_world_read (struct file * file, char __user * buffer, size_t size, loff_t * ppos){printk("hello_world_read size:%ld\r\n",size);copy_to_user(buffer,kernel_buffer,size);return size;} static ssize_t hello_world_write(struct file * file, const char __user * buffer, size_t size, loff_t *ppos){printk("hello_world_write size:%ld\r\n",size);copy_from_user(kernel_buffer,buffer,size);return size;} static const struct file_operations hello_world_fops = {.owner= THIS_MODULE,.open= hello_world_open,.release = hello_world_release,.read= hello_world_read,.write= hello_world_write,}; static int __init hello_driver_init(void){int ret;printk("hello_driver_init\r\n"); alloc_chrdev_region(&hello_devid, 0, 1, "hello");hello_major = MAJOR(hello_devid);hello_minor = MINOR(hello_devid);printk("hello driver major=%d,minor=%d\r\n",hello_major, hello_minor); hello_cdev.owner = THIS_MODULE;cdev_init(&hello_cdev, &hello_world_fops);cdev_add(&hello_cdev, hello_devid, 1);hello_class = class_create(THIS_MODULE,"hello_class"); device_create(hello_class,NULL,hello_devid,NULL,"hello"); /* /dev/hello */ return 0;} static void __exit hello_driver_cleanup(void){printk("hello_driver_cleanup\r\n");cdev_del(&hello_cdev);unregister_chrdev_region(hello_devid, 1);device_destroy(hello_class,hello_devid);class_destroy(hello_class);} module_init(hello_driver_init);module_exit(hello_driver_cleanup);MODULE_LICENSE("GPL");
KERNELDIR := /lib/modules/$(shell uname -r)/buildCURRENT_PATH := $(shell pwd)obj-m := hello_driver.oKBUILD_CFLAGS += -Wno-unused-result -Wno-unused-variablebuild: kernel_modules kernel_modules:$(MAKE) ${CFLAGS} -C $(KERNELDIR) M=$(CURRENT_PATH) modules$(CROSS_COMPILE)gcc -o test_app test_app.cclean:$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) cleanrm -rf test_app
test_app.c
#include #include #include #include #include #include #include #include uint8_t buffer[512] = {0}; int main(int argc, char *argv[]){int fd;int ret;fd = open(argv[1], O_RDWR); if(!fd) { printf("everthing is error\n"); } if(!strcmp("read",argv[2])){printf("read data from kernel\r\n");ret = read(fd,buffer,sizeof(buffer));printf("ret len:%d data:%s\r\n",ret,buffer);} if(!strcmp("write",argv[2])){printf("write data to kernel %s len:%ld\r\n",argv[3],strlen(argv[3]));ret = write(fd,argv[3],strlen(argv[3]));printf("ret len:%d\r\n",ret);} close(fd); }
执行make命令编译
root@ubuntu:/home/dong/workspace/drivercode# makemake -C /lib/modules/5.19.0-38-generic/build M=/home/dong/workspace/drivercode modulesmake[1]: Entering directory "/usr/src/linux-headers-5.19.0-38-generic"warning: the compiler differs from the one used to build the kernel The kernel was built by: x86_64-linux-gnu-gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 You are using: gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 CC [M] /home/dong/workspace/drivercode/hello_driver.o MODPOST /home/dong/workspace/drivercode/Module.symvers CC [M] /home/dong/workspace/drivercode/hello_driver.mod.o LD [M] /home/dong/workspace/drivercode/hello_driver.ko BTF [M] /home/dong/workspace/drivercode/hello_driver.koSkipping BTF generation for /home/dong/workspace/drivercode/hello_driver.ko due to unavailability of vmlinuxmake[1]: Leaving directory "/usr/src/linux-headers-5.19.0-38-generic"gcc -o test_app test_app.c
会生成hello_driver.ko文件和test_app
insmod hello_driver.ko
rmmod hello_driver
lsmod |grep hello_driver
这个文件不是随便创造的,会分配设备号,如果不使用分配的设备号创建会出现读写错误。
每一次加载或者卸载模块都会有dmesg信息。
使用dmesg -c清除信息。加载模块,使用dmesg查看模块信息。
root@ubuntu:/home/dong/workspace/drivercode# dmesg[164284.337396] hello_driver_init[164284.337399] hello driver major=238,minor=0
我的设备主设备号就是238,从设备号是0
创建设备文件命令
mknod /dev/hello c 238 0
读 : ./test_app /dev/hello read
写: ./test_app /dev/hello write 需要写入的内容
关键词:
ubuntu编译字符设备-当前速递
千城胜景丨春晨薄雾映山绿 云南开远花迎春
全球微头条丨GDP同比增长3.1%!一季度北京经济回升向好
舱驾一体成趋势,博泰车联网和纵目科技联手把握新风口_环球快报
医保电子凭证和社保卡的区别 是一样的吗
致密气体行业市场分析 2023年致密气行业现状及发展前景分析
栽了!为境外电信诈骗团伙提供通信帮助,福州2男子被抓|世界快消息
保险百万医疗有必要买吗?有哪些坑? 新要闻
简讯:中方呼吁国际社会支持大湖地区国家打造共同未来
当前热门:当日快讯:上海警方保护企业权益维护营商环境,严厉打击企业人员职务犯罪
portion什么意思_portion_环球焦点
世界焦点!生态问题解决了?苹果头显又有新爆料:可直接下载iPad应用
强降雨中出行,公交企业多举措保障_天天快看点
俩组词组什么词-俩组词有哪些词语
推动经济运行持续整体好转(经济新方位·首季数据观察)
今日黄金期货价格实时行情(2023年4月20日)
华设集团主板再融资审核状态更新为“通过”_全球观速讯
孤雨下午(04.19)主要商品操作建议|热点聚焦
wegame无法定位程序输入点_wegame无法定位程序输入点_天天资讯
焦点播报:恶心大巴黎!曝皇马已联系穆里尼奥:1年后来执教,不让对头开心
车辆落锁婴儿被困 民警60秒紧急营救
1kg=多少毫升_1kg
普京谈归还中国领土震撼全球中国人会回来的_普京归还300万领土-全球快资讯
消息!春光里,看万象“耕”新沃野织锦
农业主体连成链 产业融合促振兴
彩虹六号围攻闪盾攻略 闪盾Blitz武器技能介绍
1000单板机-屋顶板设备 世界快看
今日播报!保险退保本人可以不去吗?退保怎么退比较划算?
今日黄金td开盘价格(2023年4月19日)
闯出一条教育振兴乡村新路|头条