2012年3月25日 Sunday
1、内核版本号2.6.33.20
2、proc文件系统简单示例
3、驱动模块源文件proc_dev.c
- #include <linux/init.h>
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/fs.h>
- #include <linux/errno.h>
- #include <linux/types.h>
- #include <linux/fcntl.h>
- #include <asm/uaccess.h>
- #include <asm/io.h>
- #include <linux/stat.h>
- #include <linux/proc_fs.h>
- struct proc_dir_entry *sumproc_root_fp =NULL;
- struct proc_dir_entry *sumproc_val1_fp =NULL;
- struct proc_dir_entry *sumproc_val2_fp =NULL;
- struct proc_dir_entry *sumproc_result_fp =NULL;
- char sumproc_str1[PAGE_SIZE-80]={0,};
- char sumproc_str2[PAGE_SIZE-80]={0,};
- int read_sumproc_val(char *page,char **start,
- off_t off,int count,
- int *eof,void *data_unused)
- {
- char *buf;
- char *realdata;
-
- realdata=(char*)data_unused;
- buf=page;
- buf+=sprintf(buf,"Value=[%s]\n",realdata);
-
- *eof=1;
- return buf-page;
- }
- int write_sumproc_val(struct file *filp,const char __user *buffer,
- unsigned long count,void *data)
- {
- int len;
- char *realdata;
-
- realdata=(char*)data;
-
- if(copy_from_user(realdata,buffer,count)){
- return -EFAULT;
- }
-
- realdata[count]='\0';
- len=strlen(realdata);
- if(realdata[len-1]=='\n'){
- realdata[--len]=0;
- }
- return count;
- }
- int read_sumproc_result(char *page,char **start,
- off_t off,int count,
- int *eof,void *data_unused)
- {
- char *buf;
- int a,b,sum;
- buf=page;
-
- a=simple_strtoul(sumproc_str1,NULL,10);
- b=simple_strtoul(sumproc_str2,NULL,10);
-
- sum=a+b;
- buf+=sprintf(buf,"Result[%d+%d=%d]\n",a,b,sum);
-
- *eof=1;
- return buf-page;
- }
- int sumproc_init(void)
- {
- sumproc_root_fp =proc_mkdir("sumproc",0);
-
- sumproc_val1_fp=create_proc_entry("val1",S_IFREG|S_IRWXU,sumproc_root_fp);
-
- if(sumproc_val1_fp){
- sumproc_val1_fp->data=sumproc_str1;
- sumproc_val1_fp->read_proc=read_sumproc_val;
- sumproc_val1_fp->write_proc=write_sumproc_val;
- }
-
- sumproc_val2_fp=create_proc_entry("val2",S_IFREG|S_IRWXU,sumproc_root_fp);
-
- if(sumproc_val2_fp){
- sumproc_val2_fp->data=sumproc_str2;
- sumproc_val2_fp->read_proc=read_sumproc_val;
- sumproc_val2_fp->write_proc=write_sumproc_val;
- }
-
- sumproc_result_fp=create_proc_entry("result",S_IFREG|S_IRWXU,sumproc_root_fp);
-
- if(sumproc_root_fp){
- sumproc_result_fp->read_proc=read_sumproc_result;
- }
-
- return 0;
- }
- void sumproc_exit(void)
- {
- remove_proc_entry("result",sumproc_root_fp);
- remove_proc_entry("val1",sumproc_root_fp);
- remove_proc_entry("val2",sumproc_root_fp);
- remove_proc_entry("sumproc",0);
- }
- module_init(sumproc_init);
- module_exit(sumproc_exit);
- MODULE_LICENSE("Gual BSD/GPL");
4、Makefile
- obj-m += proc_dev.o
- CURRENT_PATH := $(shell pwd)
- LINUX_KERNEL := $(shell uname -r)
- LINUX_KERNEL_PATH := /usr/src/linux-$(LINUX_KERNEL)
- all:
- make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
- clean:
- #make -C $(LINUX_KERNEL_PAHT) M=$(CURRENT_PATH) clean
- rm *.mod.*
- rm *.order
- rm *.o
- rm *.symvers
5、执行方法及结果
- ~/proc_test# make //编译驱动模块
- ~/proc_test# make clean //清理不用到的生成文件
- ~/proc_test# insmod proc_dev.ko //插入驱动模块
- ~/proc_test# lsmod |grep proc_dev //查看驱动模块是否插入成功
- proc_dev 9562 0
- ~/proc_test# echo "100" > /proc/sumproc/val1 //用echo向val1文件输入值
- ~/proc_test# cat /proc/sumproc/val1 //查看是否输入成功
- Value=[100]
- ~/proc_test# echo "10" > /proc/sumproc/val2 //用echo向val2文件输入值
- ~/proc_test# cat /proc/sumproc/result //查看result文件中的运算结果
- Result[100+10=110]
6、移除模块
- ~/proc_test# rmmod proc_dev //移除驱动模块
阅读(1015) | 评论(0) | 转发(0) |