另一种简单的读写-simple_read_from_buffer

之前在《简单的读和写》中提到过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;
}

猜你喜欢

转载自blog.csdn.net/weixin_39821531/article/details/88556480