本文共 2751 字,大约阅读时间需要 9 分钟。
来自:http://blog.csdn.net/smilefyx/article/details/40539885
一、简述
本文实例演示如何在驱动编程中使驱动自动在/proc目录下穿件文件,用户可以直接操作/proc目录下的文件对设备进行操作。
二、编码
2.1、测试驱动源码
使用编辑工具编写如下驱动代码,如文件procbuffer.c内容如下:
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/init.h>
- #include <linux/delay.h>
- #include <asm/uaccess.h>
- #include <asm/irq.h>
- #include <asm/io.h>
- #include <mach/gpio.h>
- #include <mach/hardware.h>
- #include <linux/device.h>
- #include <linux/proc_fs.h>
-
-
- #define PROC_DIR_NAME "ArvinFei"
- #define PROC_FILE_NAME "wrbuffer"
-
-
- struct proc_dir_entry *proc_wrbuff_dir;
-
- static int proc_wrbuff_open(struct inode *inode,struct file *file);
- static ssize_t proc_wrbuff_read(struct file *file, char __user *buf, size_t count, loff_t *offset);
- static ssize_t proc_wrbuff_write(struct file *file, const char __user *buf, size_t count, loff_t *offset);
-
-
- static struct file_operations fops_proc_wrbuffer = {
- .owner = THIS_MODULE,
- .open = proc_wrbuff_open,
- .read = proc_wrbuff_read,
- .write = proc_wrbuff_write,
- };
-
- static int proc_wrbuff_open(struct inode *inode,struct file *file) {
- printk("open proc wrbuffer device!");
- return 0;
- }
-
- static ssize_t proc_wrbuff_read(struct file *file, char __user *buf, size_t count, loff_t *offset) {
- printk("read proc wrbuffer device!");
- return 0;
- }
-
- static ssize_t proc_wrbuff_write(struct file *file, const char __user *buf, size_t count, loff_t *offset) {
- printk("write proc wrbuffer device!");
- return 0;
- }
-
- static int __init proc_wrbuff_init(void) {
- int ret = 0;
- struct proc_dir_entry *proc_file;
-
-
- proc_wrbuff_dir = proc_mkdir(PROC_DIR_NAME, NULL);
- if(!proc_wrbuff_dir){
- printk("create proc dir error!");
- return -ENOMEM;
- }
-
-
- proc_file = create_proc_entry(PROC_FILE_NAME, 0666, proc_wrbuff_dir);
- if (!proc_file) {
- printk("create proc file error!");
- ret = -ENOMEM;
- goto no_proc_file;
- }
-
-
- proc_file->proc_fops = &fops_proc_wrbuffer;
-
- return 0;
-
- no_proc_file:
- remove_proc_entry(PROC_FILE_NAME,proc_wrbuff_dir);
-
- return ret;
- }
-
- static void __exit proc_wrbuff_exit(void) {
-
- remove_proc_entry(PROC_FILE_NAME,proc_wrbuff_dir);
-
- remove_proc_entry(PROC_DIR_NAME,NULL);
- }
-
-
- late_initcall(proc_wrbuff_init);
- module_exit(proc_wrbuff_exit);
-
- MODULE_AUTHOR("yxtouch520@yeah.net");
- MODULE_DESCRIPTION("allwinner buffer read and write test driver,you can control this device use the interface in /proc dir.");
- MODULE_LICENSE("GPL");
2.2、Makefile
Makefile中的内核源代码请按照自己源码存放的位置进行修改。
- obj-m := proc_wrbuffer.o
- proc_wrbuffer-objs := procbuffer.o
- KERNELDIR := /home/feiyinxian/workspace/A20/landsem/A20_Android4.4/lichee/linux-3.4
- PWD := $(shell pwd)
-
- modules:
- $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
- .PHONY:clean
- clean:
- rm *.ko *.o