How can I use ioctl to communicate between user program and driver?

I am writing a driver in Linux. How can I use ioctl to communicate between user program and driver? In my driver, the structure looks like this:

struct file_operations fops = {.read = device_read,.write = device_write,.unlocked_ioctl = device_ioctl,.open = device_open,.release=device_release };

      

In my understanding, device_ioctl here is a function that handles the ioctl call from the user program. And the call is possible with a variable number of parameters.

But I'm not sure about the way the ioctl is used. Also completely confused and wants to know how can I write device_ioctl?

Can anyone help me?

Thanks in advance.

+3


source to share


3 answers


I believe LDD3 chapter 6.1 can answer your question with good examples.



+2


source


  • Use register_chrdev

    to get the base number for your kernel file. Give fops

    as parameter.
  • You will get the returned main number (you can also find it in /proc/devices

    ) using that number with the command mknod

    to create /dev/yourdevice

    .
  • In your user code, open /dev/yourdevice

    and use the function ioctl

    with a file descriptor.


+1


source


You can use any file proc

instead of a device like this:

static long my_proc_ioctl(struct file * file, unsigned int cmd, unsigned long arg)
{
    printk("%s() cmd=%.08x arg=%pK\n", __func__, cmd, (void *)arg);
    return 0;
}

static const struct file_operations my_ioctl_fops = {
    .owner = THIS_MODULE,
    .unlocked_ioctl = my_proc_ioctl,
};

[...]
proc_create("my_ioctl_file", 0600, NULL, &my_ioctl_fops);
[...]

      

0


source







All Articles