tijin
tijin

Reputation: 757

How i can use ioctl for the communication between the user program and driver?

i am writing a driver in linux. How i can use ioctl for the communication between the user program and driver ? In my driver, the structure is as follows:

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

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

But i am not sure about the way of using ioctl. Also totally confused and eager to know that how i can write device_ioctl ?

Can anyone help me?

Thanks in advance.

Upvotes: 3

Views: 2958

Answers (3)

Ilya Matveychikov
Ilya Matveychikov

Reputation: 4024

You can use any proc file instead of the device in a such way:

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);
[...]

Upvotes: 1

Wayne
Wayne

Reputation: 651

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

Upvotes: 3

ugoren
ugoren

Reputation: 16441

  1. Use register_chrdev to get a major number for your kernel file. Give fops as a parameter.
  2. You get a major number returned (you can also find it in /proc/devices) use this number with the mknod command to create /dev/yourdevice.
  3. In your user space code, open /dev/yourdevice and use the ioctl function with the file descriptor.

Upvotes: 2

Related Questions