copy_to_user没有复制数据?

时间:2016-03-01 19:14:39

标签: linux-kernel linux-device-driver embedded-linux

对于我们正在SoC系统上从嵌入式FPGA读取和写入数据的项目。写作有效(现在只有1个字节,但很好)。读取功能正确访问FPGA(并获取正确的值)但由于某种原因,copy_to_user不会将任何内容复制到用户。在我的设备上运行猫不会返回任何东西。我希望有人可以告诉我,我做错了什么。

其他信息:我们的目标是采用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!

非常感谢!

1 个答案:

答案 0 :(得分:4)

您确定return 0;吗?我认为这个函数应该返回复制的字节数,在你的情况下应该是return sizeof(read_value32);