- 论坛徽章:
- 0
|
本帖最后由 shaohua312 于 2013-08-13 10:25 编辑
最近在写驱动,遇到个问题,请各位有空帮帮忙:
我写了一个字符驱动程序,其实就是抄宋宝华那本书的一个fifo驱动。无论我应用层fread函数要求读多少个字节,下面的驱动对应的read函数都会读取4096,也是就是说static ssize_t lshgpio_read(struct file * file, char __user * buf, size_t size, loff_t * ppos)该函数中的size永远都是4096。
举个例子 num_read = fread(Buf,1,sizeof(Buf),fp0);这里我如果定义buf为512个大小,当到内核的static ssize_t lshgpio_read(struct file * file, char __user * buf, size_t size, loff_t * ppos)时候,函数中的size是4096,在lshgpio_read我可以把真实的数据个数传到上面来,但是如果传递的数据小于512,则自动再次进入驱动的read函数,直到总共读取到512个自己为止;如果传到应用层的数据大于512个自己,则只读取512个,num_read就显示512。
很奇怪,我现在搞不清楚是linux就是这种机制还是我的程序什么地方出了问题!!!请大家帮忙,着急中!!!
static ssize_t lshgpio_read(struct file * file, char __user * buf, size_t size, loff_t * ppos)
{
unsigned int count = size;
pr_info("the count value is :%d,----- %s,line:%d\n",count, __func__, __LINE__);
int ret = 0;
struct gpio_timer_device *dev = file->private_data;
DECLARE_WAITQUEUE(wait, current);
down(&dev->sem);
add_wait_queue(&dev->r_wait, &wait);
pr_info("%s,line:%d\n", __func__, __LINE__);
while(dev->current_len == 0) {
if (file->f_flags &O_NONBLOCK){
ret = - EAGAIN;
goto out;
}
__set_current_state(TASK_INTERRUPTIBLE);
up(&dev->sem);
schedule();
if(signal_pending(current)){
ret = - ERESTARTSYS;
goto out2;
}
down(&dev->sem);
}
if (count > dev->current_len)
count= dev->current_len;
if (copy_to_user(buf, dev->mem, count)){
ret = -EFAULT;
goto out;
} else {
memcpy(dev->mem, dev->mem + count, dev->current_len - count);
dev->current_len -= count;
ret = count;
pr_info("Read %d bytes--%s,line:%d\n", count , __func__, __LINE__);
wake_up_interruptible(&dev->w_wait);
}
out:
up(&dev->sem);
out2:
remove_wait_queue(&dev->r_wait, &wait);
set_current_state(TASK_RUNNING);
pr_info("%s,line:%d\n", __func__, __LINE__);
return ret;
}
附上这段程序!无论我怎么写应用层,当进入read函数后都会显示the count value is :4096。 |
|