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
- Use
register_chrdev
to get the base number for your kernel file. Givefops
as parameter. - You will get the returned main number (you can also find it in
/proc/devices
) using that number with the commandmknod
to create/dev/yourdevice
. - In your user code, open
/dev/yourdevice
and use the functionioctl
with a file descriptor.
+1
source to share
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 to share