HDU网安操作系统课程设计 实验四 Linux设备驱动

一 题目介绍

Linux驱动程序占内核代码一半以上,开发设计驱动程序是Linux内核编程的一项很重要的工作。过本次实验,应了解Linux的设备管理机制和驱动程序的框架结构:掌握Linux设备驱动程序的编写流程和加载方法,为从事具体的硬件设备驱动程序开发打下基础。

内容要求:

编写一个字符设备驱动程序。实现对该字符设备的打开、读、写、I/O控制和关闭五个基本操作。为了避免牵涉到汇编语言,字符设备并非一个真实的字符设备,而是用一段内核空间来模拟的。以模块的方式加载该驱动程序。

二 实验思路

图1:字符设备驱动程序框架图

三 遇到问题及解决方法

1.对ioctl函数中的int cmd参数的构建方法不清楚,通过学习_IO(IOC_MAGIC, 0)宏构建了三个简单的cmd。

2.在华为云上编译的时候一直在报错,查阅资料之后发现是关于设备驱动的函数代码随着版本更新已经改变了,课本上的代码有一些错误,比如unlocked_ioctl的返回值类型变成了long,定义file_operations时要加 = 。

3.根据书本提到的设备初始化、注销的一系列API,在网上查阅资料,熟悉参数之后,都成功实现了功能。

四 核心代码及实验结果展示

  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/fs.h>
  4. #include <linux/kernel.h>
  5. #include <linux/slab.h>
  6. #include <linux/types.h>
  7. #include <linux/errno.h>
  8. #include <linux/cdev.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/ioctl.h>
  11. #define MAX_BUF_SIZE 1024
  12. #define IOC_MAGIC  'c'
  13. #define IOCTEST0    _IO(IOC_MAGIC, 0)
  14. #define IOCTEST1    _IO(IOC_MAGIC, 1)
  15. #define IOCTEST2    _IO(IOC_MAGIC, 2)
  16. //#define _IO(type,nr)             _IOC(_IOC_NONE,(type),(nr),0)
  17. //构造无参数的命令cmd编号
  18. /*
  19. |   设备类型   | 序列号  | 方向 |  数据尺寸   |
  20. |-------------|----------|-------|------------|
  21. |     8 bit    |   8 bit  | 2 bit | 8~14 bit|
  22. |-------------|----------|-------|------------|
  23. */
  24. static struct cdev my_cdev;
  25. int major = 0;
  26. static char *data = NULL;//指向为设备开辟的数据区的指针 
  27. static int my_open(struct inode *inode, struct file *file)
  28. {
  29.     data = (char*)kmalloc(sizeof(char) * MAX_BUF_SIZE, GFP_KERNEL);//GFP_KERNEL内核内存的通常分配方法 
  30.     if (!data)
  31.         return -ENOMEM;//Out of memory
  32.     memset(data, 0, MAX_BUF_SIZE);
  33.     printk("device open.\n");
  34.         return 0;
  35. }
  36. static int my_release(struct inode *inode, struct file *file)
  37. {
  38.     if (data)
  39.     {
  40.         kfree(data);
  41.         data = NULL;
  42.     }
  43.     printk("device release.\n");
  44.         return 0;
  45. }
  46. static ssize_t my_read(struct file *file, char __user*buf, size_t count, loff_t *offp)
  47. {
  48.         if (offp > 10)
  49.                 return 0;
  50.         if (count < 0)
  51.                 return -EINVAL;//参数错误 
  52.         if (count > MAX_BUF_SIZE)
  53.                 count = MAX_BUF_SIZE;
  54.         if (copy_to_user(buf, data, count) == EFAULT)
  55.                 return -EFAULT;
  56.         printk("my_write\n");
  57.         return count;
  58. }
  59. static long my_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  60. {
  61.         printk("my_ioctl\n");
  62.         switch(cmd)
  63.         {
  64.                 case IOCTEST0:
  65.                         printk("ioctl_test0\n");
  66.                         break;
  67.                 case IOCTEST1:
  68.                         printk("ioctl_test1\n");
  69.                         break;
  70.                 case IOCTEST2:
  71.                         printk("ioctl_test2\n");
  72.                         break;
  73.                 default:
  74.                         return -ENOTTY;
  75.         }
  76.         return 0;
  77. }
  78. static struct file_operations my_cdev_fops =
  79. {
  80.         .owner = THIS_MODULE,
  81.         .read = my_read,
  82.         .write= my_write,
  83.         .open = my_open,
  84.         .release = my_release,
  85.         .unlocked_ioctl = my_ioctl,
  86. };
  87. static int init_cdev(void)
  88. {
  89.         dev_t dev = MKDEV(major, 0); //MKDEV(int major, int minor);
  90.         int ret = alloc_chrdev_region(&dev, 01, (char*)"my_cdev");//动态分配设备号
  91.         if(ret < 0)
  92.         {
  93.                 printk(KERN_WARNING"分配设备号失败\n");
  94.                 return ret;
  95.         }
  96.         major = MAJOR(dev);
  97.         cdev_init(&my_cdev, &my_cdev_fops);
  98.         my_cdev.owner = THIS_MODULE;
  99.         int errno = cdev_add(&my_cdev, dev, 1); //注册cdev结构,将设备号和cdev结构体加入cdev_map散列表 
  100.         if(errno < 0)
  101.         {
  102.                 printk(KERN_WARNING"cdev注册失败\n");
  103.                 return errno;
  104.         }
  105.         printk("my_cdev init\n");
  106.         return 0;
  107. }
  108. static void exit_cdev(void)
  109. {
  110.         cdev_del(&my_cdev);
  111.         unregister_chrdev_region(MKDEV(major, 0), 1);//释放设备号 
  112.         printk("my_cdev exit\n");
  113. }
  114. module_init(init_cdev);
  115. module_exit(exit_cdev);
  116. MODULE_LICENSE("GPL");

驱动模块代码

图1:通过insmod加载驱动模块后,cat /proc/devices可以看到my_cdev的major号为243

图2:手动建立设备节点,并给设备设置777的权限

图3:在/dev目录下就可以看到我们的设备了,权限也是我们刚刚设置的777

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <sys/ioctl.h>
  9. #define DEV_NAME "/dev/my_cdev"
  10. #define BUF_SIZE 1024
  11. #define IOC_MAGIC  'c'
  12. #define IOCTEST0    _IO(IOC_MAGIC, 0)
  13. #define IOCTEST1    _IO(IOC_MAGIC, 1)
  14. #define IOCTEST2    _IO(IOC_MAGIC, 2)
  15. int main(int argc, char const *argv[])
  16. {
  17.         int fd;
  18.         char buffer[BUF_SIZE];
  19.         fd = open(DEV_NAME, O_RDWR);
  20.         if (fd < 0)
  21.         {
  22.                 perror("open dev fail!\n");
  23.                 return -1;
  24.         }
  25.         open(NULL, fd);
  26.         read(fd, buffer, 1);
  27.         write(fd, buffer, 1);
  28.         ioctl(fd, IOCTEST0);
  29.         ioctl(fd, IOCTEST1);
  30.         ioctl(fd, IOCTEST2);
  31.         return 0;
  32. }

 编写一个简单的测试代码,主要通过printk验证功能

 图4:设备可以正常运行

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值