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

一、简述

本文实例演示如何在linux驱动编程中使驱动自动在/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;

    /*1  create parent dir in /porc */
	proc_wrbuff_dir = proc_mkdir(PROC_DIR_NAME, NULL);
	if(!proc_wrbuff_dir){
        printk("create proc dir error!");
		return -ENOMEM;
	}

    /*2  creata device file in /proc/parent dir*/
	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;
	}

	/*3  set file operation point*/
	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("yxtouch@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/xxx/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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值