菜鸟问题:隐式声明函数 copy_to_user。大家帮忙啊

21lyw 2009-04-11 09:22:51
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h> /* everything... */

#define DEVICE_NAME "demo"
#define demo_MAJOR 249
#define demo_MINOR 0
#define MAX_BUF_LEN 20
static char drv_buf[20];


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);


makefile

obj-m := demo.o
#KERNEL_DIR :=/lib/modules/$(shell uname -r)/build
KERNEL_DIR :=/lib/modules/2.6.18-1.2798.fc6/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules
...全文
2063 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
z08053520 2010-03-20
  • 打赏
  • 举报
回复
使用copy_to_user()和copy_from_user()需包含头文件<asm/uaccess.h>
morris88 2009-04-11
  • 打赏
  • 举报
回复
不必在代码中实现 copy_to_user 和 copy_from_user,系统核心已经实现了。如果需要这个,只需要包含

#include <asm/uaccess.h>
21lyw 2009-04-11
  • 打赏
  • 举报
回复
加了一段代码:

#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h> /* 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]#


morris88 2009-04-11
  • 打赏
  • 举报
回复
你要问什么?

你就贴了一个

菜鸟问题:隐式声明函数 copy_to_user。大家帮忙啊


加一段代码啦....
21lyw 2009-04-11
  • 打赏
  • 举报
回复
哥们,我还是不知道错在哪儿?
morris88 2009-04-11
  • 打赏
  • 举报
回复
copy_from_user()和copy_to_user()函数的作用:由于内核空间与用户空间的内存不能直接互访,因此借助
函数copy_to_user()完成用户空间到内核空间的复制,函数copy_from_user()完成内核空间到用户空间的复制。

//源码:linux/arch/i386/lib/usercopy.c

copy_to_user: - Copy a block of data into user space.
@to: Destination address, in user space.
@from: Source address, in kernel space.
@n: Number of bytes to copy.
849unsigned long copy_to_user(void __user *to, const void *from, unsigned long n)

850{

851 if (access_ok(VERIFY_WRITE, to, n))
852 n = __copy_to_user(to, from, n);
853 return n;
854}

copy_from_user: - Copy a block of data from user space.
@to: Destination address, in kernel space.
@from: Source address, in user space.
@n: Number of bytes to copy.

874unsigned long copy_from_user(void *to, const void __user *from, unsigned long n)
875{
876 if (access_ok(VERIFY_READ, from, n))
877 n = __copy_from_user(to, from, n);
878 else
879 memset(to, 0, n);
880 return n;
881}

21,595

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 驱动开发/核心开发
社区管理员
  • 驱动开发/核心开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧