How do you implement ioctl in a character driver, with a complete example?
Short answer: Define command numbers in a header shared with user space using _IO, _IOR, _IOW and _IOWR (they encode direction, a magic type, a number and the argument size), implement .unlocked_ioctl with a switch on the command, copy arguments with copy_from_user/copy_to_user, return -ENOTTY for unknown commands, and add .compat_ioctl = compat_ptr_ioctl so 32-bit programs work on a 64-bit kernel (use fixed-size types so layouts match).
Shared header (mydev_ioctl.h)
#include <linux/ioctl.h>
#include <linux/types.h>
struct mydev_cfg {
__u32 sample_rate; /* fixed-size types: same layout for 32- and 64-bit user space */
__u32 gain;
__u64 reserved; /* room for future fields; must be zero */
};
#define MYDEV_MAGIC 'M'
#define MYDEV_RESET _IO(MYDEV_MAGIC, 0)
#define MYDEV_GET_CFG _IOR(MYDEV_MAGIC, 1, struct mydev_cfg)
#define MYDEV_SET_CFG _IOW(MYDEV_MAGIC, 2, struct mydev_cfg)
#define MYDEV_GET_CNT _IOR(MYDEV_MAGIC, 3, __u32)
Driver side
static long mydev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{
struct mydev *d = f->private_data;
void __user *uarg = (void __user *)arg;
struct mydev_cfg cfg;
long ret = 0;
if (_IOC_TYPE(cmd) != MYDEV_MAGIC) return -ENOTTY;
mutex_lock(&d->lock);
switch (cmd) {
case MYDEV_RESET:
mydev_hw_reset(d);
break;
case MYDEV_GET_CFG:
if (copy_to_user(uarg, &d->cfg, sizeof d->cfg)) ret = -EFAULT;
break;
case MYDEV_SET_CFG:
if (copy_from_user(&cfg, uarg, sizeof cfg)) { ret = -EFAULT; break; }
if (cfg.reserved || cfg.sample_rate == 0 || cfg.sample_rate > 100000 || cfg.gain > 7) {
ret = -EINVAL; break; /* validate everything from user space */
}
d->cfg = cfg;
mydev_hw_apply(d);
break;
case MYDEV_GET_CNT:
ret = put_user(d->irq_count, (__u32 __user *)uarg);
break;
default:
ret = -ENOTTY; /* conventional "unknown ioctl" */
}
mutex_unlock(&d->lock);
return ret;
}
static const struct file_operations mydev_fops = {
.owner = THIS_MODULE,
.open = mydev_open,
.release = mydev_release,
.unlocked_ioctl = mydev_ioctl,
.compat_ioctl = compat_ptr_ioctl, /* fine because the structs are layout-identical */
};
User space
int fd = open("/dev/mydev0", O_RDWR);
struct mydev_cfg c = { .sample_rate = 1000, .gain = 2 };
if (ioctl(fd, MYDEV_SET_CFG, &c) < 0) perror("SET_CFG");
Interview points
unlocked_ioctlmeans no Big Kernel Lock: you provide your own locking.- Never trust user input: check ranges, reserved fields and sizes, and never pass a user pointer to hardware.
_IORmeans user space reads (kernel writes to user memory).- Check
capable(CAP_SYS_ADMIN)for privileged operations. - Alternatives for new interfaces: sysfs attributes (single values), configfs, netlink, or a subsystem's standard API, which reviewers often prefer over custom ioctls.