Linux字符设备驱动实例

闲话少说,理论不讲,直接拷贝源码即可运行。

首先是device文件:mycdev.c

  1. #include <linux/init.h>  
  2. #include <linux/module.h>  
  3. #include <linux/types.h>  
  4. #include <linux/fs.h>  
  5. #include <asm/uaccess.h>  
  6. #include <linux/cdev.h>  
  7.   
  8. MODULE_AUTHOR("Boatman Yang");  
  9. MODULE_LICENSE("GPL");  
  10.   
  11. #define MYCDEV_MAJOR 231 /*the predefined mycdev's major devno*/  
  12. #define MYCDEV_SIZE 100  
  13.   
  14. static int mycdev_open(struct inode *inode, struct file *fp)  
  15. {  
  16.     return 0;  
  17. }  
  18.   
  19. static int mycdev_release(struct inode *inode, struct file *fp)  
  20. {  
  21.     return 0;  
  22. }  
  23.   
  24. static ssize_t mycdev_read(struct file *fp, char __user *buf, size_t size, loff_t *pos)  
  25. {  
  26.     unsigned long p = *pos;  
  27.     unsigned int count = size;  
  28.     int i;  
  29.     char kernel_buf[MYCDEV_SIZE] = "This is mycdev from Boatman!";  
  30.   
  31.     if(p >= MYCDEV_SIZE)  
  32.         return -1;  
  33.     if(count > MYCDEV_SIZE)  
  34.         count = MYCDEV_SIZE - p;  
  35.   
  36.     if (copy_to_user(buf, kernel_buf, count) != 0) {  
  37.         printk("read error!\n");  
  38.         return -1;  
  39.     }  
  40.   
  41.     /* 
  42.     for (i = 0; i < count; i++) { 
  43.         __put_user(i, buf);//write 'i' from kernel space to user space's buf; 
  44.         buf++; 
  45.     } 
  46.     */  
  47.   
  48.     printk("boatman's reader: %d bytes was read...\n", count);  
  49.     return count;  
  50.   
  51. }  
  52.   
  53. static ssize_t mycdev_write(struct file *fp, const char __user *buf, size_t size, loff_t *pos)  
  54. {  
  55.     return size;  
  56. }  
  57.   
  58. /*filling the mycdev's file operation interface in the struct file_operations*/  
  59. static const struct file_operations mycdev_fops =  
  60. {  
  61.     .owner = THIS_MODULE,  
  62.     .read = mycdev_read,  
  63.     .write = mycdev_write,  
  64.     .open = mycdev_open,  
  65.     .release = mycdev_release,  
  66. };  
  67.   
  68. /*module loading function*/  
  69. static int __init mycdev_init(void)  
  70. {  
  71.     int ret;  
  72.   
  73.     printk("mycdev module is staring..\n");  
  74.   
  75.     ret=register_chrdev(MYCDEV_MAJOR,"boatman_cdev",&mycdev_fops);  
  76.     if(ret<0)  
  77.     {  
  78.         printk("register failed..\n");  
  79.         return 0;  
  80.     }  
  81.     else  
  82.     {  
  83.         printk("register success..\n");  
  84.     }  
  85.   
  86.     return 0;  
  87. }  
  88.   
  89. /*module unloading function*/  
  90. static void __exit mycdev_exit(void)  
  91. {  
  92.     printk("mycdev module is leaving..\n");  
  93.     unregister_chrdev(MYCDEV_MAJOR,"boatman_cdev");  
  94. }  
  95.   
  96. module_init(mycdev_init);  
  97. module_exit(mycdev_exit);  


然后是它的Makefile文件,如下:

  1. obj-m:=mycdev.o  
  2. PWD:=$(shell pwd)  
  3. CUR_PATH:=$(shell uname -r)  
  4. KERNEL_PATH:=/usr/src/linux-headers-$(CUR_PATH)  
  5.   
  6. all:  
  7.     make -C $(KERNEL_PATH) M=$(PWD) modules  
  8. clean:  
  9.     make -C $(KERNEL_PATH) M=$(PWD) clean  


第三个测试程序,mycdev_test.c

  1. #include <stdio.h>  
  2. #include <sys/types.h>  
  3. #include <sys/stat.h>  
  4. #include <fcntl.h>  
  5. #include <stdlib.h>  
  6.   
  7. int main()  
  8. {  
  9.     int testdev;  
  10.     int i, ret;  
  11.     char buf[15];  
  12.   
  13.     testdev = open("/dev/mycdev", O_RDWR);  
  14.   
  15.     if (-1 == testdev) {  
  16.         printf("cannot open file.\n");  
  17.         exit(1);  
  18.     }  
  19.   
  20.     if (ret = read(testdev, buf, 15) < 15) {  
  21.         printf("read error!\n");  
  22.         exit(1);  
  23.     }  
  24.   
  25.     printf("%s\n", buf);  
  26.   
  27.     close(testdev);  
  28.   
  29.     return 0;  
  30. }  


 

 

测试步骤:

1.       运行“make”来编译mycdev.c,会生成mycdev.ko

2.       将生成的驱动文件插入到内核:sudo insmod mycdev.ko

3.       通过cat /proc/devices 查看系统中未使用的字符设备主设备号,比如当前231未使用;

4.       创建设备文件结点:sudo mknod /dev/mycdev c 231 0;具体使用方法通过man mknod命令查看;

5.       修改设备文件权限:sudo chmod 777 /dev/mycdev;

以上成功完成后,编译本用户态测试程序;运行该程序查看结果(如有必要可以通过dmesg查看日志信息);


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值