另一种简单的读写-simple_read_from_buffer

这篇博客介绍了内核编程中,除了使用copy_to_user和copy_from_user之外,如何利用simple_read_from_buffer和simple_write_to_buffer进行简单读写操作。文章以GPIO读取为例,阐述了这两个函数在内核与用户空间交互过程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前在《简单的读和写》中提到过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 availabl
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/init.h> #include <linux/module.h> #include <linux/platform_device.h> #include <linux/kernel.h> #include <linux/proc_fs.h> static int ab_system_status_value = 0; static int ab_sync_status_value = 0; static int ota_status_value = 0; // ab_system_status节点读写 static ssize_t ab_system_status_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { char buffer[32]; int len = snprintf(buffer, sizeof(buffer), “%d\n”, ab_system_status_value); return simple_read_from_buffer(buf, count, ppos, buffer, len); } static ssize_t ab_system_status_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { char input[32]; int value; if (count > sizeof(input) - 1) return -EINVAL; if (copy_from_user(input, buf, count)) return -EFAULT; input[count] = '\0'; if (sscanf(input, "%d", &value) == 1) { ab_system_status_value = value; return count; } return -EINVAL; } // ab_sync_status节点读写 static ssize_t ab_sync_status_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { char buffer[32]; int len = snprintf(buffer, sizeof(buffer), “%d\n”, ab_sync_status_value); return simple_read_from_buffer(buf, count, ppos, buffer, len); } static ssize_t ab_sync_status_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { char input[32]; int value; if (count > sizeof(input) - 1) return -EINVAL; if (copy_from_user(input, buf, count)) return -EFAULT; input[count] = '\0'; if (sscanf(input, "%d", &value) == 1) { ab_sync_status_value = value; return count; } return -EINVAL; } // ota_status节点读写 static ssize_t ota_status_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { char buffer[32]; int len = snprintf(buffer, sizeof(buffer), “%d\n”, ota_status_value); return simple_read_from_buffer(buf, count, ppos, buffer, len); } static ssize_t ota_status_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { char input[32]; int value; if (count > sizeof(input) - 1) return -EINVAL; if (copy_from_user(input, buf, count)) return -EFAULT; input[count] = '\0'; if (sscanf(input, "%d", &value) == 1) { ota_status_value = value; return count; } return -EINVAL; } // proc节点操作结构体 static const struct proc_ops ab_system_status_ops = { .proc_read = ab_system_status_read, .proc_write = ab_system_status_write, }; static const struct proc_ops ab_sync_status_ops = { .proc_read = ab_sync_status_read, .proc_write = ab_sync_status_write, }; static const struct proc_ops ota_status_ops = { .proc_read = ota_status_read, .proc_write = ota_status_write, }; // 设备树匹配表 static const struct of_device_id fibo_ab_ota_of_match[] = { { .compatible = “fibocom,fibo_ab_ota”, }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, fibo_ab_ota_of_match); // probe函数 static int fibo_ab_ota_probe(struct platform_device *pdev) { int ret; pr_info("fibo_ab_ota_probe called\n"); if (!proc_create("ab_system_status", 0664, NULL, &ab_system_status_ops)) { pr_err("Failed to create ab_system_status proc entry\n"); return -ENOMEM; } if (!proc_create("ab_sync_status", 0664, NULL, &ab_sync_status_ops)) { pr_err("Failed to create ab_sync_status proc entry\n"); remove_proc_entry("ab_system_status", NULL); return -ENOMEM; } if (!proc_create("ota_status", 0664, NULL, &ota_status_ops)) { pr_err("Failed to create ota_status proc entry\n"); remove_proc_entry("ab_system_status", NULL); remove_proc_entry("ab_sync_status", NULL); return -ENOMEM; } return 0; } // remove函数 static int fibo_ab_ota_remove(struct platform_device *pdev) { pr_info(“fibo_ab_ota_remove called\n”); remove_proc_entry(“ab_system_status”, NULL); remove_proc_entry(“ab_sync_status”, NULL); remove_proc_entry(“ota_status”, NULL); return 0; } // 平台驱动结构体 static struct platform_driver fibo_ab_ota_driver = { .probe = fibo_ab_ota_probe, .remove = fibo_ab_ota_remove, .driver = { .name = “fibo_ab_ota”, .owner = THIS_MODULE, .of_match_table = fibo_ab_ota_of_match, }, }; static int __init fibo_ab_ota_init(void) { return platform_driver_register(&fibo_ab_ota_driver); } static void __exit fibo_ab_ota_exit(void) { platform_driver_unregister(&fibo_ab_ota_driver); } module_init(fibo_ab_ota_init); module_exit(fibo_ab_ota_exit); MODULE_DESCRIPTION(“fibo ab ota Module”); MODULE_AUTHOR(“qujunyang”); MODULE_LICENSE(“GPL”); 检查驱动代码是否存在问题,不要gpio功能
最新发布
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值