IOCTL

发布时间:2026/8/11 5:21:14
IOCTL 一、什么是 ioctlioctl是 input/output control 的缩写是 Linux 内核提供的一个系统调用用于实现用户空间与内核空间设备驱动之间的自定义控制通道。当标准的read/write操作无法满足设备的控制和配置需求时如设置设备模式、查询状态、执行特定操作就需要使用ioctl。核心作用设备控制启动/停止/模式切换参数配置设置频率、颜色、亮度等状态查询获取设备忙/闲状态特殊数据传输非普通数据流二、工作原理调用链路用户空间程序调用 ioctl() │ ▼ 系统调用陷入内核 VFS 根据文件描述符 fd 找到对应的设备驱动 │ ▼ 驱动中的 unlocked_ioctl 回调函数被调用 │ ▼ 驱动执行 cmd 对应的操作返回结果三、ioctl 命令号的定义命令号是一个32 位的整数通过标准宏来生成包含以下字段字段说明幻数 (Magic Number)一个 8 位的字符或数字用于区分不同驱动避免命令号冲突命令序号 (Number)在驱动内部唯一标识一个命令数据传输方向用户→驱动 / 驱动→用户 / 无传输参数大小用于内核检查参数传递的合法性常用宏宏含义数据流向_IO(type, nr)无参数 ioctl无数据传输_IOW(type, nr, datatype)带写入参数用户空间 → 内核空间_IOR(type, nr, datatype)带读取参数内核空间 → 用户空间_IOWR(type, nr, datatype)带读写参数双向传输示例#define WR_VALUE _IOW(a, a, int32_t*) /* 写入 int32_t 值 */ #define RD_VALUE _IOR(a, b, int32_t*) /* 读取 int32_t 值 */a幻数用于区分设备a/b命令序号int32_t*传递参数的类型四、驱动中的 ioctl 实现1. 函数原型static long my_ioctl(struct file *file, unsigned int cmd, unsigned long arg)参数含义struct file *file打开的设备文件指针unsigned int cmd用户空间传入的 ioctl 命令号unsigned long arg用户空间传入的参数指针或整数值2. 数据安全拷贝由于用户空间和内核空间内存隔离需要使用内核提供的安全函数/* 从用户空间拷贝数据到内核空间 */ copy_from_user(kernel_buf, (void __user *)arg, size); /* 从内核空间拷贝数据到用户空间 */ copy_to_user((void __user *)arg, kernel_buf, size);失败时返回非零值驱动应返回-EFAULT。3. 标准处理模板static long my_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { int32_t value; switch (cmd) { case WR_VALUE: if (copy_from_user(value, (int32_t __user *)arg, sizeof(value))) { pr_err(copy_from_user failed\n); return -EFAULT; } pr_info(Received value: %d\n, value); break; case RD_VALUE: if (copy_to_user((int32_t __user *)arg, value, sizeof(value))) { pr_err(copy_to_user failed\n); return -EFAULT; } break; default: return -ENOTTY; /* 不支持的命令 */ } return 0; }4. 注册到文件操作结构体static const struct file_operations fops { .owner THIS_MODULE, .open my_open, .release my_release, .read my_read, .write my_write, .unlocked_ioctl my_ioctl, /* ← 绑定 ioctl 处理函数 */ };五、用户空间程序如何使用 ioctl1. 包含头文件#include sys/ioctl.h #include fcntl.h #include unistd.h2. 定义命令号必须与驱动一致#define WR_VALUE _IOW(a, a, int32_t*) #define RD_VALUE _IOR(a, b, int32_t*)3. 调用 ioctlint fd; int32_t val 100; /* 打开设备 */ fd open(/dev/etx_device, O_RDWR); /* 写入值 */ ioctl(fd, WR_VALUE, val); /* 读取值 */ ioctl(fd, RD_VALUE, val); printf(Read value: %d\n, val); /* 关闭设备 */ close(fd);六、完整驱动示例代码结构1.标准字符设备驱动register_chrdev#include linux/module.h #include linux/fs.h #include linux/cdev.h #include linux/device.h #include linux/uaccess.h #include linux/ioctl.h /* 1. 定义 ioctl 命令 */ #define WR_VALUE _IOW(a, a, int32_t*) #define RD_VALUE _IOR(a, b, int32_t*) /* 2. 全局变量 */ int32_t value 0; dev_t dev; static struct class *dev_class; static struct cdev etx_cdev; /* 3. ioctl 处理函数 */ static long my_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { /* 处理 cmd... */ return 0; } /* 4. 文件操作结构体 */ static const struct file_operations fops { .owner THIS_MODULE, .open my_open, .release my_release, .unlocked_ioctl my_ioctl, }; /* 5. 模块初始化 */ static int __init my_init(void) { alloc_chrdev_region(dev, 0, 1, my_dev); //动态分配主设备号和次设备号 cdev_init(etx_cdev, fops);//初始化字符设备结构体 cdev cdev_add(etx_cdev, dev, 1);//将字符设备添加到内核系统 dev_class class_create(THIS_MODULE, my_class);//创建一个设备类 device_create(dev_class, NULL, dev, NULL, my_device);//创建设备节点 return 0; } /* 6. 模块退出 */ static void __exit my_exit(void) { device_destroy(dev_class, dev); class_destroy(dev_class); cdev_del(etx_cdev); unregister_chrdev_region(dev, 1); } module_init(my_init); module_exit(my_exit); MODULE_LICENSE(GPL);2.MISC 设备驱动(misc_register)#include linux/module.h #include linux/miscdevice.h #include linux/fs.h #include linux/uaccess.h #include linux/ioctl.h /* * 1. 定义 ioctl 命令 * */ #define WR_VALUE _IOW(a, a, int32_t*) #define RD_VALUE _IOR(a, b, int32_t*) static int32_t value 0; /* * 2. 文件操作函数 * */ static long my_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { switch (cmd) { case WR_VALUE: if (copy_from_user(value, (int32_t __user *)arg, sizeof(value))) return -EFAULT; pr_info(Write value: %d\n, value); break; case RD_VALUE: if (copy_to_user((int32_t __user *)arg, value, sizeof(value))) return -EFAULT; pr_info(Read value: %d\n, value); break; default: return -ENOTTY; } return 0; } static int my_open(struct inode *inode, struct file *file) { pr_info(Device opened\n); return 0; } static int my_release(struct inode *inode, struct file *file) { pr_info(Device closed\n); return 0; } /* * 3. 文件操作结构体 * */ static const struct file_operations my_fops { .owner THIS_MODULE, .open my_open, .release my_release, .unlocked_ioctl my_ioctl, }; /* * 4. MISC 设备结构体 * */ static struct miscdevice my_misc { .minor MISC_DYNAMIC_MINOR, // 内核自动分配次设备号 .name my_misc_device, // → /dev/my_misc_device .fops my_fops, }; /* * 5. 模块初始化一步完成 * */ static int __init my_init(void) { /* 一步完成驱动注册 设备节点创建 */ if (misc_register(my_misc) 0) { pr_err(misc_register failed\n); return -1; } pr_info(MISC device registered: /dev/%s\n, my_misc.name); return 0; } /* * 6. 模块退出 * */ static void __exit my_exit(void) { misc_deregister(my_misc); pr_info(MISC device removed\n); } module_init(my_init); module_exit(my_exit); MODULE_LICENSE(GPL);七、常见错误码错误码含义使用场景-EFAULT地址无效copy_from_user/copy_to_user失败-ENOTTY不支持的 ioctl 命令cmd不在switch分支中-EINVAL参数无效传入的参数值超出有效范围