您的问题是关于如何实现 test_measurements_file_ops,我可以提供以下演示代码供您参考:
static const struct file_operations test_measurements_file_ops = {
.write = test_write,
.read = test_read,
};
static ssize_t test_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
char *data;
int error;
if (!count || count >= MAXIMUM_SIZE)
return -ENOMEM;
data = kzalloc(count + 1, GFP_NOFS);
if (!data)
return -ENOMEM;
if (copy_from_user(data, buf, count)) {
error = -EFAULT;
goto out;
}
/* handling kaddr */
out:
kfree(data);
return error ? error : count;
}
static ssize_t test_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
void *kaddr;
loff_t pos = *ppos;
loff_t len = /* strlen(kernel strings need to be copied) */;
if (pos >= len || !count)
return 0;
len -= pos;
if (count < len)
len = count;
/* handling */
if (copy_to_user(buf, kaddr, len))
return -EFAULT;
*ppos += len;
return len;
}