</>EmbeddedInterview

Explain ioctl in brief with complete flow.

MediumLinux Kernel & Device Drivers

ioctl() (Input/Output Control) is a system call used by userspace applications to perform device-specific operations that cannot be conveniently performed using normal read() and write() calls.

Examples: configuring UART baud rate, setting display resolution, controlling GPIO and configuring camera parameters.

1. ioctl syntax

int ioctl(int fd, unsigned long request, ...);
Parameter Purpose
fd Device file descriptor
request Device-specific command
arg Optional value or userspace pointer

2. Complete ioctl flow

Userspace Applicationioctl(fd, cmd, &data)glibcioctl() system call wrapperARM64: SVC #0CPU switches from EL0 to EL1Kernel Exception Handlerel0t_64_sync_handler()System Call Handlerel0_svc() → __arm64_sys_ioctl()VFS LayerResolve fd → struct file → do_vfs_ioctl()Device Driverfile->f_op->unlocked_ioctl()Driver ioctl HandlerValidate command and copy user dataHardwareConfigure peripheral using readl() / writel() if neededReturn to UserspaceReturn result through X0; ERET to EL0

Exact internal function names may vary depending on the Linux kernel version.

3. Define ioctl commands

A common header shared between userspace and the driver:

#define MAGIC 'A'

#define SET_VALUE _IOW(MAGIC, 1, int)
#define GET_VALUE _IOR(MAGIC, 2, int)
Macro Purpose
_IO() No data transfer
_IOW() Userspace to kernel
_IOR() Kernel to userspace
_IOWR() Bidirectional transfer

The command encodes direction, magic number, command number and data size. These macros do not automatically copy data.

4. Userspace application

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>

#define SET_VALUE _IOW('A', 1, int)
#define GET_VALUE _IOR('A', 2, int)

int main(void)
{
    int fd = open("/dev/mydevice", O_RDWR);
    int value = 100;

    if (fd < 0)
        return 1;

    ioctl(fd, SET_VALUE, &value);

    value = 0;

    ioctl(fd, GET_VALUE, &value);

    printf("%d\n", value);

    close(fd);
    return 0;
}

Expected output after successful operations: 100.

Production applications should also check the return value of each ioctl().

5. Kernel device driver

#include <linux/fs.h>
#include <linux/ioctl.h>
#include <linux/uaccess.h>
#include <linux/module.h>

#define SET_VALUE _IOW('A', 1, int)
#define GET_VALUE _IOR('A', 2, int)

static int value;

static long my_ioctl(struct file *file,
                     unsigned int cmd,
                     unsigned long arg)
{
    switch (cmd) {

    case SET_VALUE:
        if (copy_from_user(&value,
                           (void __user *)arg,
                           sizeof(value)))
            return -EFAULT;
        break;

    case GET_VALUE:
        if (copy_to_user((void __user *)arg,
                         &value,
                         sizeof(value)))
            return -EFAULT;
        break;

    default:
        return -ENOTTY;
    }

    return 0;
}

static const struct file_operations fops = {
    .owner = THIS_MODULE,
    .unlocked_ioctl = my_ioctl,
};

This is the ioctl portion of a driver; device registration and synchronization for concurrent callers are omitted for brevity.

6. What happens at ARM64 register level?

Register Value
X0 File descriptor
X1 ioctl command
X2 Argument (value or pointer)
X8 System call number (29 for AArch64 ioctl)

The CPU executes SVC #0, enters EL1, executes the kernel syscall handler and eventually returns to EL0 using ERET.

7. Important functions

Function Purpose
unlocked_ioctl() Device driver's ioctl callback
copy_from_user() Copies userspace data into kernel memory
copy_to_user() Copies kernel data into userspace memory
compat_ioctl() Supports compatible 32-bit applications on a 64-bit kernel
readl() / writel() Accesses hardware registers, when required

Interview answer: ioctl() provides a device-specific control interface between userspace and kernel drivers. The application invokes a system call, the VFS identifies the device using its file descriptor, and calls the driver's unlocked_ioctl() callback. The driver processes the command, safely transfers data using copy_from_user() or copy_to_user(), optionally configures hardware and returns the result.