Reputation: 435
In my kernel module I have the following read function:
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 );
}
Here I copy to user space the value of c (that is the value of gpio) by buffer.
In case where for example I need to copy to user space more data using the copy_to_user function?
For example if I would like to copy to user space also a value int x = gpio_get_value( BTN_2 )?
Upvotes: 0
Views: 1020
Reputation: 5351
Firstly, in your code for sample_read()
change
copy_to_user( buffer, &c, 1 );
to
copy_to_user( buffer, &c, sizeof(c));
Because the third argument should be number of bytes
to copy, which should be size of data which you intend to copy to user space.
Next, to copy more data
: use a struct. For example
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));
Upvotes: 2