Reputation: 757
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
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
Reputation: 651
I believe LDD3 chapter 6.1 can answer your question with good examples.
Upvotes: 3
Reputation: 16441
register_chrdev
to get a major number for your kernel file. Give fops
as a parameter./proc/devices
) use this number with the mknod
command to create /dev/yourdevice
./dev/yourdevice
and use the ioctl
function with the file descriptor.Upvotes: 2