翼度科技»论坛 云主机 LINUX 查看内容

ubuntu编译字符设备

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
前言

创建一个简单的字符设备驱动程序。
​        本文命令的运行基本上都需要root权限,使用root账号,或者在命令前面加上sudo。
​        如果你使用ssh远程连接的服务器进行代码编写。那么不要在root用户下创建文件或者文件夹。这会导致你ssh连接vscode编写代码的权限问题。可以在普通用户创建好所有的文件,然后编写。
代码

驱动程序

hello_driver.c
  1. #include <linux/types.h>
  2. #include <linux/init.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/mm.h>
  5. #include <linux/slab.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/module.h>
  8. #include <linux/device.h>
  9. #include <linux/cdev.h>
  10. dev_t hello_devid;
  11. struct cdev hello_cdev;
  12. int hello_major = 0;
  13. int hello_minor;
  14. uint8_t kernel_buffer[1024] = {0};
  15. static struct class *hello_class;
  16. static int hello_world_open(struct inode * inode, struct file * file)
  17. {
  18.         printk("hello_world_open\r\n");
  19.         return 0;
  20. }
  21. static int hello_world_release (struct inode * inode, struct file * file)
  22. {
  23.         printk("hello_world_release\r\n");
  24.         return 0;
  25. }
  26. static ssize_t hello_world_read (struct file * file, char __user * buffer, size_t size, loff_t * ppos)
  27. {
  28.         printk("hello_world_read size:%ld\r\n",size);
  29.         copy_to_user(buffer,kernel_buffer,size);
  30.         return size;
  31. }
  32. static ssize_t hello_world_write(struct file * file, const char __user * buffer, size_t size, loff_t *ppos)
  33. {
  34.         printk("hello_world_write size:%ld\r\n",size);
  35.         copy_from_user(kernel_buffer,buffer,size);
  36.         return size;
  37. }
  38. static const struct file_operations hello_world_fops = {
  39.         .owner                = THIS_MODULE,
  40.         .open                = hello_world_open,
  41.         .release = hello_world_release,
  42.         .read                = hello_world_read,
  43.         .write        = hello_world_write,
  44. };
  45. static int __init hello_driver_init(void)
  46. {
  47.         int ret;
  48.         printk("hello_driver_init\r\n");
  49.         alloc_chrdev_region(&hello_devid, 0, 1, "hello");
  50.         hello_major = MAJOR(hello_devid);
  51.         hello_minor = MINOR(hello_devid);
  52.         printk("hello driver major=%d,minor=%d\r\n",hello_major, hello_minor);       
  53.         hello_cdev.owner = THIS_MODULE;
  54.         cdev_init(&hello_cdev, &hello_world_fops);
  55.         cdev_add(&hello_cdev, hello_devid, 1);
  56.        
  57.         hello_class = class_create(THIS_MODULE,"hello_class");
  58.         device_create(hello_class,NULL,hello_devid,NULL,"hello"); /* /dev/hello */
  59.         return 0;
  60. }
  61. static void __exit hello_driver_cleanup(void)
  62. {
  63.         printk("hello_driver_cleanup\r\n");
  64.         cdev_del(&hello_cdev);
  65.         unregister_chrdev_region(hello_devid, 1);
  66.        
  67.         device_destroy(hello_class,hello_devid);
  68.         class_destroy(hello_class);
  69.        
  70. }
  71. module_init(hello_driver_init);
  72. module_exit(hello_driver_cleanup);
  73. MODULE_LICENSE("GPL");
复制代码
Makefile文件
  1. KERNELDIR := /lib/modules/$(shell uname -r)/build
  2. CURRENT_PATH := $(shell pwd)
  3. obj-m := hello_driver.o
  4. KBUILD_CFLAGS   += -Wno-unused-result -Wno-unused-variable
  5. build: kernel_modules
  6. kernel_modules:
  7.         $(MAKE) ${CFLAGS} -C $(KERNELDIR) M=$(CURRENT_PATH) modules
  8.         $(CROSS_COMPILE)gcc -o test_app test_app.c
  9. clean:
  10.         $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
  11.         rm -rf test_app
复制代码
测试程序

test_app.c
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdint.h>
  9. uint8_t buffer[512] = {0};
  10. int main(int argc, char *argv[])
  11. {
  12.         int fd;
  13.         int ret;
  14.        
  15.         fd  = open(argv[1], O_RDWR);
  16.     if(!fd)
  17.     {
  18.         printf("everthing is error\n");
  19.     }
  20.         if(!strcmp("read",argv[2]))
  21.         {
  22.                 printf("read data from kernel\r\n");
  23.                 ret = read(fd,buffer,sizeof(buffer));
  24.                 printf("ret len:%d data:%s\r\n",ret,buffer);
  25.         }
  26.         if(!strcmp("write",argv[2]))
  27.         {
  28.                 printf("write data to kernel %s len:%ld\r\n",argv[3],strlen(argv[3]));
  29.                 ret = write(fd,argv[3],strlen(argv[3]));
  30.                 printf("ret len:%d\r\n",ret);
  31.         }
  32.        
  33.         close(fd);
  34.        
  35. }
复制代码
编译

执行make命令编译
  1. root@ubuntu:/home/dong/workspace/drivercode# make
  2. make  -C /lib/modules/5.19.0-38-generic/build M=/home/dong/workspace/drivercode modules
  3. make[1]: Entering directory '/usr/src/linux-headers-5.19.0-38-generic'
  4. warning: the compiler differs from the one used to build the kernel
  5.   The kernel was built by: x86_64-linux-gnu-gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
  6.   You are using:           gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
  7.   CC [M]  /home/dong/workspace/drivercode/hello_driver.o
  8.   MODPOST /home/dong/workspace/drivercode/Module.symvers
  9.   CC [M]  /home/dong/workspace/drivercode/hello_driver.mod.o
  10.   LD [M]  /home/dong/workspace/drivercode/hello_driver.ko
  11.   BTF [M] /home/dong/workspace/drivercode/hello_driver.ko
  12. Skipping BTF generation for /home/dong/workspace/drivercode/hello_driver.ko due to unavailability of vmlinux
  13. make[1]: Leaving directory '/usr/src/linux-headers-5.19.0-38-generic'
  14. 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查看模块信息。
  1. root@ubuntu:/home/dong/workspace/drivercode# dmesg
  2. [164284.337396] hello_driver_init
  3. [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 需要写入的内容

来源:https://www.cnblogs.com/wdgray/p/17337041.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具