linux驱动中在/proc虚拟文件系统目录下自动创建设备【转】

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

一、简述

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


二、编码

2.1、测试驱动源码

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

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  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]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  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  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值