博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux驱动中在/proc虚拟文件系统目录下自动创建设备【转】
阅读量:2194 次
发布时间:2019-05-02

本文共 2751 字,大约阅读时间需要 9 分钟。

来自:http://blog.csdn.net/smilefyx/article/details/40539885

一、简述

本文实例演示如何在驱动编程中使驱动自动在/proc目录下穿件文件,用户可以直接操作/proc目录下的文件对设备进行操作。

二、编码

2.1、测试驱动源码

使用编辑工具编写如下驱动代码,如文件procbuffer.c内容如下:

[cpp]   
 
  1. #include <linux/module.h>  
  2. #include <linux/kernel.h>  
  3. #include <linux/fs.h>  
  4. #include <linux/init.h>  
  5. #include <linux/delay.h>  
  6. #include <asm/uaccess.h>  
  7. #include <asm/irq.h>  
  8. #include <asm/io.h>  
  9. #include <mach/gpio.h>  
  10. #include <mach/hardware.h>  
  11. #include <linux/device.h>  
  12. #include <linux/proc_fs.h>  
  13.   
  14.   
  15. #define PROC_DIR_NAME      "ArvinFei"  
  16. #define PROC_FILE_NAME     "wrbuffer"  
  17.   
  18.   
  19. struct proc_dir_entry *proc_wrbuff_dir;  
  20.   
  21. static int proc_wrbuff_open(struct inode *inode,struct file *file);  
  22. static ssize_t proc_wrbuff_read(struct file *file, char __user *buf, size_t count, loff_t *offset);  
  23. static ssize_t proc_wrbuff_write(struct file *file, const char __user *buf, size_t count, loff_t *offset);  
  24.   
  25.   
  26. static struct file_operations fops_proc_wrbuffer = {  
  27.    .owner = THIS_MODULE,  
  28.    .open = proc_wrbuff_open,  
  29.    .read = proc_wrbuff_read,  
  30.    .write = proc_wrbuff_write,  
  31. };  
  32.   
  33. static int proc_wrbuff_open(struct inode *inode,struct file *file)  {  
  34.     printk("open proc wrbuffer device!");  
  35.     return 0;  
  36. }  
  37.   
  38. static ssize_t proc_wrbuff_read(struct file *file, char __user *buf, size_t count, loff_t *offset)  {  
  39.     printk("read proc wrbuffer device!");  
  40.     return 0;  
  41. }  
  42.   
  43. static ssize_t proc_wrbuff_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)  {  
  44.     printk("write proc wrbuffer device!");  
  45.     return 0;  
  46. }  
  47.   
  48. static int __init proc_wrbuff_init(void)  {   
  49.     int ret = 0;  
  50.     struct proc_dir_entry *proc_file;  
  51.   
  52.     /*1  create parent dir in /porc */  
  53.     proc_wrbuff_dir = proc_mkdir(PROC_DIR_NAME, NULL);  
  54.     if(!proc_wrbuff_dir){  
  55.         printk("create proc dir error!");  
  56.         return -ENOMEM;  
  57.     }  
  58.   
  59.     /*2  creata device file in /proc/parent dir*/  
  60.     proc_file = create_proc_entry(PROC_FILE_NAME, 0666, proc_wrbuff_dir);  
  61.     if (!proc_file) {  
  62.         printk("create proc file error!");  
  63.         ret =  -ENOMEM;  
  64.         goto no_proc_file;  
  65.     }  
  66.   
  67.     /*3  set file operation point*/  
  68.     proc_file->proc_fops = &fops_proc_wrbuffer;  
  69.   
  70.     return 0;  
  71.   
  72. no_proc_file:  
  73.     remove_proc_entry(PROC_FILE_NAME,proc_wrbuff_dir);  
  74.   
  75.     return ret;  
  76. }  
  77.   
  78. static void __exit proc_wrbuff_exit(void)  {  
  79.     //  
  80.     remove_proc_entry(PROC_FILE_NAME,proc_wrbuff_dir);  
  81.     //  
  82.     remove_proc_entry(PROC_DIR_NAME,NULL);  
  83. }  
  84.   
  85.   
  86. late_initcall(proc_wrbuff_init);  
  87. module_exit(proc_wrbuff_exit);  
  88.   
  89. MODULE_AUTHOR("yxtouch520@yeah.net");  
  90. MODULE_DESCRIPTION("allwinner buffer read and write test driver,you can control this device use the interface in /proc dir.");  
  91. MODULE_LICENSE("GPL");  

 

2.2、Makefile

Makefile中的内核源代码请按照自己源码存放的位置进行修改。

[cpp]   
 
  1. obj-m := proc_wrbuffer.o  
  2. proc_wrbuffer-objs := procbuffer.o  
  3. KERNELDIR := /home/feiyinxian/workspace/A20/landsem/A20_Android4.4/lichee/linux-3.4  
  4. PWD := $(shell pwd)  
  5.   
  6. modules:  
  7.     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules  
  8. .PHONY:clean  
  9. clean:  
  10.     rm *.ko *.o  

你可能感兴趣的文章
阿里云《云原生》公开课笔记 第八章 应用配置管理
查看>>
阿里云《云原生》公开课笔记 第九章 应用存储和持久化数据卷:核心知识
查看>>
linux系统 阿里云源
查看>>
国内外helm源记录
查看>>
牛客网题目1:最大数
查看>>
散落人间知识点记录one
查看>>
Leetcode C++ 随手刷 547.朋友圈
查看>>
手抄笔记:深入理解linux内核-1
查看>>
内存堆与栈
查看>>
Leetcode C++《每日一题》20200621 124.二叉树的最大路径和
查看>>
Leetcode C++《每日一题》20200622 面试题 16.18. 模式匹配
查看>>
Leetcode C++《每日一题》20200625 139. 单词拆分
查看>>
Leetcode C++《每日一题》20200626 338. 比特位计数
查看>>
Leetcode C++ 《拓扑排序-1》20200626 207.课程表
查看>>
Go语言学习Part1:包、变量和函数
查看>>
Go语言学习Part2:流程控制语句:for、if、else、switch 和 defer
查看>>
Go语言学习Part3:struct、slice和映射
查看>>
Go语言学习Part4-1:方法和接口
查看>>
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
leetcode 130. Surrounded Regions
查看>>