【问题标题】:copy_to_user: treating more datacopy_to_user:处理更多数据
【发布时间】:2014-12-30 03:32:39
【问题描述】:

在我的内核模块中,我有以下读取功能:

static ssize_t sample_read(struct file *filp, char *buffer, size_t length, loff_t * offset) //read function here means to manage the communication with the button
{
    int ret = 1;
    int c;

    c = gpio_get_value( BTN );

    copy_to_user( buffer, &c, 1 ); //Buffer is the stack where to place the data read by function. copy_to_user copies the buffer on the user space. Here the reading is very simple. But if I would like to transfer more data?

    printk( KERN_INFO "%s: %s read %d from BTN\n", module_name, __func__, c );

    return( ret );
}

这里我通过缓冲区将c的值(也就是gpio的值)复制到用户空间。

如果我需要使用 copy_to_user 函数将更多数据复制到用户空间?

例如,如果我还想将值复制到用户空间 int x = gpio_get_value( BTN_2 )?

【问题讨论】:

  • copy to user space more datacopy to user space also a value int x = gpio_get_value( BTN_2 )? 是什么意思?
  • 您可以从 sysfs 导出设备信息/值,而不是使用结构。
  • 嗨 askb... 我不知道如何使用 sysfs。你能举个例子或小解释一下吗?

标签: c linux-kernel kernel kernel-module


【解决方案1】:

首先,在sample_read() 更改代码中

copy_to_user( buffer, &c, 1 );

copy_to_user( buffer, &c, sizeof(c));

因为要复制的第三个参数应该是number of bytes,它应该是您打算复制到用户空间的数据的大小。

接下来,to copy more data:使用结构。例如

typedef struct data {
    int x;
    int c;
} data_t;

data_t val;
val.x = gpio_get_value(BTN_2);
val.c = gpio_get_value(BTN);

copy_to_user( buffer, &val, sizeof(data_t));

【讨论】:

  • 对不起。我知道缓冲区是一个 char*,所以当我在用户空间时,如何获取用户空间中的两个值?同样在用户空间中,我应该通过 typedef 创建一个 data_t 类型来获取这两个值?
  • 在用户空间,typecast char*data_t然后读取值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-06
  • 2019-07-13
相关资源
最近更新 更多