无法从 linux 字符设备读取 [英] Can't read from linux character device
问题描述
我正在为加速度计实现 SPI 驱动程序.SPI 部分已完成,但我无法从用户空间读取值.
static char charDevMessage[CD_BUFFER_SIZE] = { 0 };...静态 ssize_t char_dev_read(struct file *filep, char *buffer, size_t len, loff_t *position){int error_count = 0;结构 xyz_values xyz;size_t size_requested;xyz = adxl345_get_xyz();memset(charDevMessage, 0, CD_BUFFER_SIZE);sprintf(charDevMessage, "%d,%d,%d", xyz.x, xyz.y, xyz.z);printk(KERN_INFO "MOB: %s, 请求大小: %d
", charDevMessage, len);如果 (len >= CD_BUFFER_SIZE){size_requested = CD_BUFFER_SIZE;}别的{size_requested = len;}error_count = copy_to_user(buffer, charDevMessage, size_requested);如果(错误计数== 0){printk(KERN_INFO "MOB: 向用户发送了 %d 个字符
", size_requested);返回 (size_requested = 0);}别的{printk(KERN_INFO "MOB: 无法向用户发送 %d 个字符
", error_count);返回 -EFAULT;}}
我安装驱动时已经创建了节点.但是当我尝试 cat
或尝试通过 python 读取时,它返回一个空字符串.
dmesg
说它已成功发送到用户空间:
[ 3094.495972] MOB: SPI 字符设备已打开 1 次[3094.506075] MOB:-349,-512,511,请求大小:49[3094.514487] MOB:向用户发送了 256 个字符[3094.522646]MOB:字符设备成功关闭[3120.658568] MOB:SPI 字符设备已打开 2 次[3120.668609] MOB:0,0,0,请求大小:48[3120.676392] MOB:向用户发送了 256 个字符[3120.684740]MOB:字符设备成功关闭
我做错了什么?
你误解了 .read
函数的概念:
阅读器(用户空间)只看到.read
返回的值并将其解释为已读取的字节数.
至于.read
的最后一个参数(在您的情况下为position
),它的解释完全取决于驱动程序作者.position
指向的值在打开文件时被内核初始化为 0.之后,内核本身不再修改它.
如果你想总是从头开始读,你可以忽略position
参数:
返回 size_requested;
或者,在语义上更好,您可以递增由 position
指向的值,因此它将反映读取的total 字节数.但否则忽略它:
*position += size_requested;返回 size_requested;
I am implementing an SPI driver for an accelerometer. The SPI part is done but I can't read values from userspace.
static char charDevMessage[CD_BUFFER_SIZE] = { 0 };
...
static ssize_t char_dev_read(struct file *filep, char *buffer, size_t len, loff_t *position)
{
int error_count = 0;
struct xyz_values xyz;
size_t size_requested;
xyz = adxl345_get_xyz();
memset(charDevMessage, 0, CD_BUFFER_SIZE);
sprintf(charDevMessage, "%d,%d,%d", xyz.x, xyz.y, xyz.z);
printk(KERN_INFO "MOB: %s, requested size: %d
", charDevMessage, len);
if (len >= CD_BUFFER_SIZE)
{
size_requested = CD_BUFFER_SIZE;
}
else
{
size_requested = len;
}
error_count = copy_to_user(buffer, charDevMessage, size_requested);
if (error_count == 0)
{
printk(KERN_INFO "MOB: Sent %d characters to the user
", size_requested);
return (size_requested = 0);
}
else
{
printk(KERN_INFO "MOB: Failed to send %d characters to the user
", error_count);
return -EFAULT;
}
}
The node has been created when I installed the driver. But when I tried cat
or tried to read by python it returns an empty string.
dmesg
says it has been sent to userspace successfully:
[ 3094.495972] MOB: SPI Character device has been opened 1 time(s) [ 3094.506075] MOB: -349,-512,511 , requested size:49 [ 3094.514487] MOB: Sent 256 characters to the user [ 3094.522646] MOB: Character device successfully closed [ 3120.658568] MOB: SPI Character device has been opened 2 time(s) [ 3120.668609] MOB: 0,0,0 , requested size:48 [ 3120.676392] MOB: Sent 256 characters to the user [ 3120.684740] MOB: Character device successfully closed
What am I doing wrong?
You misaunderstood the concept of the .read
function:
The reader (user space) sees only value returned by the .read
and interpret it as a number of bytes which has been read.
As for the last parameter to the .read
(position
in your case), its interpretation is fully up to the driver's author. Value, pointed by position
, is initialized to 0 by the kernel core when the file is opened. After that, kernel itself never modifies it.
If you want to always read from the beginning, you may just ignore position
parameter:
return size_requested;
Or, semantically better, you may increment value, pointed by the position
, so it will reflect total number of bytes read. But otherwise ignore it:
*position += size_requested;
return size_requested;
这篇关于无法从 linux 字符设备读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!