加了一段代码:
#include
#include
#include /* everything... */
#define DEVICE_NAME "demo"
#define demo_MAJOR 249
#define demo_MINOR 0
#define MAX_BUF_LEN 20
static char drv_buf[20];
unsigned long copy_to_user(void __user *to, const void *from, unsigned long n)
{
if (access_ok(VERIFY_WRITE, to, n))
n = __copy_to_user(to, from, n);
return n;
}
unsigned long copy_from_user(void *to, const void __user *from, unsigned long n)
{
if (access_ok(VERIFY_READ, from, n))
n = __copy_from_user(to, from, n);
else
memset(to, 0, n);
return n;
}
static int demo_open(struct inode *inode, struct file *file) {
//MOD_INC_USE_COUNT;
sprintf(drv_buf,"device open sucess!\n");
printk("device open sucess!\n");
return 0;
}
static int demo_release(struct inode *inode, struct file *filp) {
//MOD_DEC_USE_COUNT;
printk("device release\n");
return 0;
}
static ssize_t demo_read(struct file *filp, char *buffer, size_t count, loff_t *ppos) {
if(count > MAX_BUF_LEN)
count=MAX_BUF_LEN;
copy_to_user(buffer, drv_buf,count);
printk("user read data from driver\n");
return count;
}
static ssize_t demo_write(struct file *filp,const char *buffer, size_t count) {
copy_from_user(drv_buf , buffer, count);
printk("user write data to driver\n");
//your code here
return count;
}
static struct file_operations demo_fops = {
write: demo_write,
read: demo_read,
open: demo_open,
release: demo_release,
};
static int __init demo_init(void) {
int result;
SET_MODULE_OWNER(&demo_fops);
result = register_chrdev(demo_MAJOR, "demo", &demo_fops);
if (result < 0) return result;
printk(DEVICE_NAME " initialized\n");
return 0;
}
static void __exit demo_exit(void) {
unregister_chrdev(demo_MAJOR, "demo");
printk(DEVICE_NAME " unloaded\n");
}
module_init(demo_init);
module_exit(demo_exit);
make 还是过不去
[root@localhost ~]# cd /home/cc/demo
bash: cd: /home/cc/demo: 没有那个文件或目录
[root@localhost ~]# cd /home/cc/code/demo
[root@localhost demo]# ls
demo.c demo.c~ Makefile Makefile~ test_demo.c test_demo.c~
[root@localhost demo]# make
make -C /lib/modules/2.6.18-1.2798.fc6/build M=/home/cc/code/demo modules
make[1]: Entering directory `/usr/src/kernels/2.6.18-1.2798.fc6-i586'
CC [M] /home/cc/code/demo/demo.o
/home/cc/code/demo/demo.c: 在函数 ‘copy_to_user’ 中:
/home/cc/code/demo/demo.c:13: 错误:隐式声明函数‘access_ok’
/home/cc/code/demo/demo.c:13: 错误:‘VERIFY_WRITE’ 未声明 (在此函数内第一次使用)
/home/cc/code/demo/demo.c:13: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
/home/cc/code/demo/demo.c:13: 错误:所在的函数内只报告一次。)
/home/cc/code/demo/demo.c:14: 错误:隐式声明函数 ‘__copy_to_user’
/home/cc/code/demo/demo.c: 在函数 ‘copy_from_user’ 中:
/home/cc/code/demo/demo.c:20: 错误:‘VERIFY_READ’ 未声明 (在此函数内第一次使用)
/home/cc/code/demo/demo.c:21: 错误:隐式声明函数 ‘__copy_from_user’
/home/cc/code/demo/demo.c: 在顶层:
/home/cc/code/demo/demo.c:54: 警告:从不兼容的指针类型初始化
/home/cc/code/demo/demo.c: 在函数 ‘demo_init’ 中:
/home/cc/code/demo/demo.c:62: 错误:隐式声明函数 ‘SET_MODULE_OWNER’
make[2]: *** [/home/cc/code/demo/demo.o] 错误 1
make[1]: *** [_module_/home/cc/code/demo] 错误 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-1.2798.fc6-i586'
make: *** [default] 错误 2
[root@localhost demo]#