TheBrillowable
TheBrillowable

Reputation: 319

copy_to_user not copying data?

for a project we are reading and writing data from an embedded FPGA on a SoC system. Writing works (for now just 1 byte, but oh well). The read function correctly accesses the FPGA (and gets the correct value) but for some reason the copy_to_user does not copy anything to the user. Running cat on my device doesn't not return anything. I hope somebody can tell me where I'm doing something wrong.

Additional information: We're targeting a Altrera Cyclone V SoC system with a ARMv7 Processor. We're using a buildroot system with kernel 4.3.0 as recommended by Altera.

Code:

// 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;
}

Output (including kernel messages and debug prints):

# 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!

Thanks a bunch!

Upvotes: 1

Views: 2566

Answers (1)

pelya
pelya

Reputation: 4494

Are you sure about return 0;? I think this function should return the amount of bytes copied, in your case this should be return sizeof(read_value32);

Upvotes: 4

Related Questions