jgm
jgm

Reputation: 1270

Getting cat: /dev/mydevice1: Invalid argument as output when trying to communicate in driver

I am trying to develop a FIFO virtual character device driver in Linux kernel. I am trying to copy data from one process executing on one device to another process executing on another device of the same type.

I was trying to read from mydevice1

cat /dev/mydevice1

and writing to mydevice

echo a > /dev/mydevice

but I keep getting an error:

cat: /dev/mydevice1: Invalid argument

in the second device output(that is the reading device).

This is my code to read from device

static ssize_t device_read( struct file *file,char *bufStoreData, size_t count,
loff_t *ppos )                       
{

    ssize_t val = 1;
    if(access_mode == WRITE_MY)
        return -EINVAL;
    else
          res = wait_event_interruptible (read_queue, ready_to_read);

    printk(KERN_ALERT "going to copy\n");
    res = copy_from_user(bufStoreData,virtual_device.data,count);           
    printk(KERN_ALERT "copied data %c \n",virtual_device.data[0]);
    ready_to_read = 0;  
    ready_to_write = 1;  
    wake_up_interruptible (&write_queue);
        return val;  
}

When I check the output using dmesg, I get the data is being copied, but I do not get any output on the device.Getting invalid argument in the output in the reading device.

Upvotes: 3

Views: 2990

Answers (1)

Kevan Ahlquist
Kevan Ahlquist

Reputation: 5533

Agreeing with n.m and Safayet Ahmed, try changing:

res = copy_from_user(bufStoreData,virtual_device.data,count);           

To:

res = copy_to_user(bufStoreData,virtual_device.data,count);           

The nomenclature can be a bit confusing, I think about it as the user-space process reading data from or writing data to the kernel memory.

On a slightly different topic, the function always returns 1, regardless of the number of bytes read. Traditionally read functions return the number of bytes read, or -1 in combination with setting errno to indicate an error. Are you intentionally breaking with that convention?

Also, the result of wait_event_interruptible in the res variable is overwritten every time the function runs.

Upvotes: 1

Related Questions