之前在《簡單的讀和寫》中提到過file_operations結構中的讀/寫方法中的buff參數是指向用戶空間的緩沖區。而用戶空間的指針,內核代碼不能直接引用其中的內容。
所以用了copy_to_user和copy_from_user來完成最后的讀寫操作。
這里介紹另一對可以完成讀寫的函數:simple_read_from_buffer和simple_write_to_buffer。
這兩個方法定義在:kernel-4.9/fs/libfs.c
/**
* simple_read_from_buffer - copy data from the buffer to user space
* @to: the user space buffer to read to
* @count: the maximum number of bytes to read
* @ppos: the current position in the buffer
* @from: the buffer to read from
* @available: the size of the buffer
*
* The simple_read_from_buffer() function reads up to @count bytes from the
* buffer @from at offset @ppos into the user space address starting at @to.
*
* On success, the number of bytes read is returned and the offset @ppos is
* advanced by this number, or negative value is returned on error.
**/
ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
const void *from, size_t available)
{
loff_t pos = *ppos;
size_t ret;
if (pos < 0)
return -EINVAL;
if (pos >= available || !count)
return 0;
if (count > available - pos)
count = available - pos;
ret = copy_to_user(to, from + pos, count);
if (ret == count)
return -EFAULT;
count -= ret;
*ppos = pos + count;
return count;
}
EXPORT_SYMBOL(simple_read_from_buffer);
/**
* simple_write_to_buffer - copy data from user space to the buffer
* @to: the buffer to write to
* @available: the size of the buffer
* @ppos: the current position in the buffer
* @from: the user space buffer to read from
* @count: the maximum number of bytes to read
*
* The simple_write_to_buffer() function reads up to @count bytes from the user
* space address starting at @from into the buffer @to at offset @ppos.
*
* On success, the number of bytes written is returned and the offset @ppos is
* advanced by this number, or negative value is returned on error.
**/
ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
const void __user *from, size_t count)
{
loff_t pos = *ppos;
size_t res;
if (pos < 0)
return -EINVAL;
if (pos >= available || !count)
return 0;
if (count > available - pos)
count = available - pos;
res = copy_from_user(to + pos, from, count);
if (res == count)
return -EFAULT;
count -= res;
*ppos = pos + count;
return count;
}
EXPORT_SYMBOL(simple_write_to_buffer);
舉個讀取GPIO的例子:
static ssize_t amy_proc_read(struct file *file, char __user *buffer, size_t count, loff_t *data) {
int ret;
char kbuf[32];
int value = 0;
if (*data)
return 0;
value = gpio_get_value(my_dev->gpio_pin);
sprintf(kbuf, "%d\n", value);
if (clear_user(buffer, count)) {
printk(KERN_ERR "clear error\n");
return -EIO;
}
ret = simple_read_from_buffer(buffer,count,data,kbuf,strlen(kbuf));
return ret;
}
————————————————
版權聲明:本文為CSDN博主「三三不盡」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_39821531/java/article/details/88556480