我有这样的功能
static ssize_t read_mydevice(struct file *filp, char* buf, size_t count, loff_t* ppos) {
char *text = "Device is empty\n";
int len = strlen(text);
if (*ppos != 0)
return 0;
if(count>bytesindev) count=bytesindev;
if(bytesindev==0) {
if (copy_to_user(buf, text, len))
return -EINVAL;
} else {
while(count>0) {
if (copy_to_user(buf++, msg_Ptr, (unsigned long) 1)) {
return -EINVAL;
} else {
strcpy(msg_Ptr, (msg_Ptr+1));
bytesindev-=1;
*(msg_Ptr+bytesindev) = '\0';
}
count-=1;
printk(KERN_INFO "%d\n", count);
}
printk(KERN_INFO "%s\n", buf);
}
/*
* Tell the user how much data we wrote.
*/
*ppos = len;
return len;
}
问题是用户没有得到完整的消息,它始终是它的前 13-16 个标志,一个循环被制作了很多次,它应该是。起初传递给 copy_to_user 的第三个参数是整个消息的长度,但这个解决方案也不能正常工作。有任何想法吗?这个函数应该给用户他需要的一部分缓冲区(首先写入缓冲区的字节数,有点像 FIFO)。数据必须早先发送到设备。这是编写函数:
static ssize_t
write_mydevice(struct file *filp, const char *buff, size_t len, loff_t * off)
{
if(bytesindev+len>limit) {
if(limit-bytesindev<=0) {
printk(KERN_ALERT "Device is full.\n");
return -EINVAL;
} else {
printk(KERN_INFO "Device almost full.\n");
strncat(msg_Ptr, buff, limit-bytesindev);
bytesindev+=limit-bytesindev;
return limit-bytesindev;
}
} else {
//printk(KERN_INFO "Device working\n");
strncat(msg_Ptr, buff, len);
bytesindev+=len;
return len;
}
}