【发布时间】:2016-03-01 19:14:39
【问题描述】:
对于一个项目,我们正在从 SoC 系统上的嵌入式 FPGA 读取和写入数据。写作作品(现在只有 1 个字节,但很好)。读取函数正确访问 FPGA(并获得正确的值),但由于某种原因,copy_to_user 不会将任何内容复制给用户。在我的设备上运行 cat 不会返回任何内容。我希望有人能告诉我哪里做错了。
附加信息:我们的目标是配备 ARMv7 处理器的 Altrera Cyclone V SoC 系统。我们使用的是 Altera 推荐的带有内核 4.3.0 的 buildroot 系统。
代码:
// Read function is called whenever a read in performed on one of the /dev devices
static ssize_t mydevice_read(struct file *file, char *buffer, size_t len, loff_t *offset) {
int success = 0;
u32 read_value32 = 0;
// Get the device struct out of the miscdev struct
struct mydevice_dev *dev = container_of(file->private_data, struct mydevice_dev, miscdev);
// Read data from FPGA
read_value32 = ioread32(dev->regs);
pr_info("Data received from FPGA: %d", read_value32);
success = copy_to_user(buffer, &read_value32, sizeof(read_value32));
pr_info("%d: %d bytes copied to userspace pointer 0x%p, value: %d!\n", success, sizeof(read_value32), buffer, dev->data_value8);
// If copy_to_user failed
if (success != 0) {
pr_info("Failed to copy current value to userspace!\n");
return -EFAULT;
}
return 0;
}
输出(包括内核消息和调试打印):
# insmod mymodule.ko
[ 701.922707] Initializing mymodule module
[ 701.926681] Probing for fpga devices...
[ 701.931382] Probing successful!
[ 701.935429] FPGA successfully initialized!
# echo -n -e \\x81 > /dev/mydevice
# cat /dev/mydevice
[ 721.555795] Data received from FPGA: 129
[ 721.559539] 0: 4 bytes copied to userspace pointer 0xbec67c78, value: 129!
非常感谢!
【问题讨论】:
-
认为@pelya 是在正确的轨道上。还要注意,您的调试打印正在打印名为
dev->data_value8的东西,而不是在您的代码 sn-p 中使用(而不是read_value32)。 -
是的,这是我之前写的值(本地存储在结构中)所以我有一个比较点,第一次调试打印应该打印正确读取的值。
标签: linux-kernel linux-device-driver embedded-linux