problems with copy_to_user()
Hi all,
I'm trying to use the function copy_to_user() to transfer an integer into user space but it transfers only 1 byte. For example, if I try to transfer the integer 300, it transfers only the first byte from 300: 300=12Ch in this case only 2Ch will be transfered, so in the user space, with the function fread(), I get always 44 (44 = 2Ch). I have tried to change the parameters from fread(), to take more bytes, but it didn't work. I'm still getting only 1 byte per transfer. Any Idea what could be this problem? Thank you, Leonardo |
Quote:
|
"The function copy_to_user takes the number of bytes as its third parameter."
I know, I have used also sizeof() in the third parameter but it does not work |
Would you show us some sample code?
|
ssize_t read(struct file *filp, int __user *buff,
size_t count, loff_t *offp) { int value; copy_to_user(buf, &value,4); return 1; } This is a simplfied version of this function's implementation. The rest is the same as in the "linux Device Drivers" book. thank you for your help Leonardo |
Quote:
Btw, you if you enclose sourcecode like this: [code]Paste code here[/code], it is easier to read on the forum. |
I seem to have stumbled on same kind of problem.
No matter how many bytes I try to send only one byte goes through. In kernel space I do like this: Code:
copy_to_user(buf, &data_buf, 32); Code:
bytes_read = fread(buf, size, count, MYDEVICE); As I said only first data byte gets through and I have hard time figuring that out... Fixed it now. I had a couple of mishaps there. One of them was to somehow mix size and count in fread. Silly me! |
u may mistake the copy to user()
copy_to _user(__user *buf,__ker *buf,count) is transmit the address,and it has the bits limit (8 bit at most), the value count is to point out how many values that will transmit at the address
example : int the test program int distance[3]; ... read(fd, R_buf, 3); int the read function of device driver static ElemType dsw248_read( struct file *file, char __user *buf, size_t count , loff_t *f_pos) { char ker_buf[3]; ...... copy_to_user(buf, ker_buf, count); return count; } that will trans 3 numbers at most and each number such as ker_buf[0] is 8 bit, that way 258 will return 2 (256+2) the 256 lost; you can use __put_user() or __copy_to_user () to trans int numbers 使用 __put_user() 来实现数据复制。说明一下,put_user 和 copy_to_user 都是有内核地址检查的。 put_user() 是有 access_ok() 代码的 __put_user(), 其执行小数据(1、2、4、8字节)的复制,效率比 copy_to_user 稍高。 |
Hey... the Linux kernel is stuffed with "working" examples of this function. Simply grep one, and copy it exactly!
"Use the Source, Luke!!" |
All times are GMT -5. The time now is 01:40 AM. |