Chinaunix首页 | 论坛 | 博客
  • 博客访问: 362005
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: 嵌入式

2014-01-25 20:36:13

read.h

点击(此处)折叠或打开

  1. #if defined TEST
  2. int hello();
  3. #endif
read.c

点击(此处)折叠或打开

  1. //#include "/work/coding/driver/proc/read.h"
  2. #include "read.h"
  3. #include <linux/kernel.h>

  4. int hello()
  5. {
  6.     printk("this is hello in read.c\n");
  7. }
new_test.c

点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/stat.h>
  3. #include <linux/kernel.h>
  4. #include <linux/proc_fs.h>
  5. #include <asm/uaccess.h>
  6. #include "/work/coding/driver/proc/read.h"

  7. MODULE_LICENSE("GPL");

  8. static struct proc_dir_entry * myproc_entry = NULL;
  9. static char msg[512]= {0};
  10. static int my_file_read(struct file * file,char *data,size_t len,loff_t *off)
  11. {
  12.         if(*off > 0)
  13.                 return 0;
  14.         if(copy_to_user(data,msg,strlen(msg)))
  15.                 return -EFAULT;
  16.         *off += strlen(msg);
  17.         return strlen(msg);
  18. }

  19. static int my_file_write(struct file *file, const char *data,size_t len,loff_t *off)
  20. {

  21.     printk(KERN_ERR "write len %d\n",len);
  22.         if(copy_from_user(msg,(void*)data,len))
  23.                 return -EFAULT;
  24.         return len;
  25. }

  26. static struct file_operations my_file_test_ops =
  27. {
  28.         .read = my_file_read,
  29.         .write = my_file_write,
  30. };

  31. static int __init procTest_init(void)
  32. {
  33.     hello();
  34.         myproc_entry = create_proc_entry("new_test",0666,NULL);
  35.         if(!myproc_entry)
  36.         {
  37.                 printk(KERN_ERR "can't create /proc/mytest \n");
  38.                 return -EFAULT;
  39.         }

  40.         myproc_entry->proc_fops = & my_file_test_ops;

  41.         return 0;
  42. }

  43. static void __exit procTest_exit(void)
  44. {
  45.         remove_proc_entry("new_test",NULL);
  46. }

  47. module_init(procTest_init);
  48. module_exit(procTest_exit)
Makefile

点击(此处)折叠或打开

  1. obj-m := testmodule.o

  2. testmodule-y := read.o new_test.o

  3. EXTRA_CFLAGS += \
  4.         -I/work/coding/driver/proc \
  5.         -DTEST

  6. KDIR := /lib/modules/$(shell uname -r)/build

  7. PWD := $(shell pwd)

  8. default:
  9.     $(MAKE) -C $(KDIR) M=$(PWD) modules
  10. test-y : \
  11.     read.o \
  12.     new_test.o

  13. clean:
  14.     rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions *.order *symvers *Module.markers



阅读(2085) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~