Skip to content

ioctl

Purpose

ioctl provides command-based control operations for a device file.

Driver Callback

static long mydev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    switch (cmd) {
    case MYDEV_IOCTL_GET_STATUS:
        return 0;
    default:
        return -ENOTTY;
    }
}

Command Design

Linux drivers commonly define command numbers with macros such as:

#define MYDEV_IOCTL_GET_STATUS _IOR('M', 1, struct mydev_status)
#define MYDEV_IOCTL_CLEAR      _IO('M', 2)

Common Pitfalls

Warning

Validate command numbers and user pointers carefully.

Warning

Use copy_to_user() and copy_from_user() when transferring data between kernel and user space.