简体   繁体   中英

Read and write in ioctl linux device driver

I am implementing a Linux USB Driver for x86 customized board. I have implemented ioctl for communicating with user space.

I have a buffer in the hardware which the user needs to read..

struct piclet_port_data
{
    unsigned int  num_bytes;
    unsigned char *data;
}__attribute__((packed));

The user fills the num_bytes value requesting number of bytes to read and kernel driver puts the data in the buffer passed in the structure.

Kernel IOCTL Code:

case GEN_IOCTL_PORT_READ:
        {
            struct piclet_port_data port_data;
            retval = copy_from_user(&port_data.num_bytes,
                        &((struct piclet_port_data *)arg)->num_bytes,
                        sizeof(port_data.num_bytes));
            if (!retval) {
                dev_info(&dev->interface->dev, "%s: Requested:%d"
                        "bytes\n", __func__, port_data.num_bytes);
                port_data.data =
                        kzalloc(port_data.num_bytes, GFP_KERNEL);
                if (!port_data.data) {
                    dev_err(&dev->interface->dev, "%s: Failed to allocate memory\n",
                            __func__);
                    retval = -ENOMEM;
                }
                else {
                    retval = read_port(dev, &port_data);
                    dev_info(&dev->interface->dev, "%s: read_port ret:%d\n",
                                 __func__, retval);
                    if (!retval) {
                        retval = copy_to_user(((struct piclet_port_data *)arg)->data,
                                                port_data.data,
                                               (port_data.num_bytes));
                        dev_info(&dev->interface->dev, "%s: data[0]:%c\n",
                                    __func__, ((struct piclet_port_data *)arg)->data[0]);

                    }
                }
                kfree(port_data.data);
            }
            else {
                retval = -EFAULT;
            }
        }

The code is failing on the following line with Unable to handle paging request.

 retval = copy_to_user(((struct piclet_port_data *)arg)->data,
                                                port_data.data,
                                               (port_data.num_bytes));

Can you guys please help me whether it is possible to access both read and write in ioctl in such a way.. Thanks for your time.

how you created ioctl number -- like what direction parameters have you set while creating that ioctl number.

If you have created that with _IOWR() then reading and writing should work.

 #define GEN_IOCTL_PORT_READ _IOW(type,nr,size)

I guess you have created it with IOR(), so only read is working for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM