无法从Linux字符设备读取 [英] Can't read from linux character device

查看:368
本文介绍了无法从Linux字符设备读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为加速度计实现SPI驱动程序. SPI部分已完成,但我无法从用户空间读取值.

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\n", 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\n", size_requested);
        return (size_requested = 0);      

    }
    else
    {
        printk(KERN_INFO "MOB: Failed to send %d characters to the user\n", error_count);
        return -EFAULT;              

    }
}

在安装驱动程序时已创建该节点.但是当我尝试cat或尝试通过python读取时,它返回一个空字符串.

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说它已经成功发送到用户空间:

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

我在做什么错了?

推荐答案

您误解了.read函数的概念:

读取器(用户空间)仅看到返回的,并将其解释为已读取的字节数.

The reader (user space) sees only value returned by the .read and interpret it as a number of bytes which has been read.

关于 .read的最后一个参数(在您的情况下为position),其解释完全取决于驱动程序的作者.当文件打开时,由position指向的值由内核内核初始化为0.在那之后,内核本身再也不会对其进行修改.

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.

如果您想始终从头开始阅读,则可以忽略position参数:

If you want to always read from the beginning, you may just ignore position parameter:

return size_requested;

或者,从语义上讲,更好的是,您可以增加position所指向的值,这样它将反映 total 读取的字节数.否则请忽略它:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆