当我尝试使用以下方法读取字符设备时:
cat /dev/fifodev
我从终端收到下一条消息
cat: /dev/fifodev: Invalid argument.
我已经创建了文件并赋予了它这样的权限
sudo mknod /dev/fifodev c 251 0
sudo chmod 666 /dev/fifodev
我的驱动代码是:
/*
* Called when a process, which already opened the dev file, attempts to
* read from it.
*/
static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
char *buffer, /* buffer to fill with data */
size_t length, /* length of the buffer */
loff_t * offset)
{
char aux[BUF_LEN];
printk(KERN_ALERT "Entering into device_read");
if (size_cbuffer_t(buf)<length){
return -EINVAL;
}
remove_items_cbuffer_t (buf,aux, length);
copy_to_user(buffer, aux, length);
printk(KERN_ALERT "Getting out from device_read");
return length;
}
我这里有什么问题?为什么我不能对/dev/fifodev 文件使用 cat?
最佳答案
根据您的评论,您遇到的问题似乎是您的应用程序请求将数据读入缓冲区,该缓冲区大于您要填充的数据。
您需要计算要复制的适当数据量(例如,length
和 size_cbuffer_t(buf)
中较小的值),并在length
的位置。
关于c - 使用 cat 读取字符设备驱动程序时参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21006513/