简介

IOCTL被称为输入和输出控制,用于与设备驱动程序进行通信。该系统调用在大多数驱动程序类别中可用。此功能的主要用途是处理默认情况下内核没有系统调用的设备的某些特定操作。这个函数在设备驱动里面用的很多。
IOCTL的一些实时应用程序是从“ cd”驱动器中弹出媒体,以更改串行端口的波特率,调整音量,读取或写入设备寄存器等。我们已经在设备中具有写入和读取功能驱动。但这还不足以适用于所有情况。

IOCTL涉及到的一步骤

  1. 在驱动程序创建IOCTL命令
  2. 在驱动程序编写IOCTL函数
  3. 应用程序创建IOCTL命令
  4. 应用程序使用IOCTL调用

    在驱动程序创建IOCTL命令

    ```c

    define “ioctl name” __IOX(“magic number”,”command number”,”argument type”)

  • IOX 可以是下面几种值 “IO“: an ioctl with no parameters “IOW“: an ioctl with write parameters (copy_from_user) “IOR“: an ioctl with read parameters (copy_to_user) “IOWR“: an ioctl with both write and read parameters

“Magic Number” 是唯一的数字或字符,将区分我们从其他ioctl调用ioctl()调用的集合。有时在这里使用设备的主要号码。 “command number” 是分配给ioctl的编号。这用于区分命令 “argument type” 是数据类型

  1. - 下面是使用例子
  2. ```c
  3. #include <linux/ioctl.h>
  4. #define WR_VALUE _IOW('a','a',int32_t*)
  5. #define RD_VALUE _IOR('a','b',int32_t*)

Write IOCTL function in the driver

上面我们创建了IOCTL命令。下面我们来实现ioctl驱动函数

  1. inioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
  2. <inode> : is the inode number of the file being worked on.
  3. <file> : is the file pointer to the file that was passed by the application.
  4. <cmd> : is the ioctl command that was called from the userspace.
  5. <arg> : are the arguments passed from the userspace.

在函数“ ioctl”中,我们需要实现上面(WR_VALUERD_VALUE)定义的所有命令。
然后,我们需要通知内核ioctl调用是在函数“ etx_ioctl” 中实现的。

  1. static long etx_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2. {
  3. switch(cmd) {
  4. case WR_VALUE:
  5. copy_from_user(&value ,(int32_t*) arg, sizeof(value));
  6. printk(KERN_INFO "Value = %d\n", value);
  7. break;
  8. case RD_VALUE:
  9. copy_to_user((int32_t*) arg, &value, sizeof(value));
  10. break;
  11. }
  12. return 0;
  13. }
  14. static struct file_operations fops =
  15. {
  16. .owner = THIS_MODULE,
  17. .read = etx_read,
  18. .write = etx_write,
  19. .open = etx_open,
  20. .unlocked_ioctl = etx_ioctl,
  21. .release = etx_release,
  22. };


代码

驱动

  1. #include <linux/kernel.h>
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. #include <linux/kdev_t.h>
  5. #include <linux/fs.h>
  6. #include <linux/cdev.h>
  7. #include <linux/device.h>
  8. #include <linux/slab.h> //kmalloc()
  9. #include <linux/uaccess.h> //copy_to/from_user()
  10. #include <linux/ioctl.h>
  11. #define WR_VALUE _IOW('a', 'a', int32_t *)
  12. #define RD_VALUE _IOR('a', 'b', int32_t *)
  13. #define NONE_Op _IO('a', 'c')
  14. #define READWRIE_VALUE _IOWR('a', 'd', int32_t *)
  15. int32_t value = 0;
  16. dev_t dev = 0;
  17. static struct class *dev_class;
  18. static struct cdev etx_cdev;
  19. static int __init etx_driver_init(void);
  20. static void __exit etx_driver_exit(void);
  21. static int etx_open(struct inode *inode, struct file *file);
  22. static int etx_release(struct inode *inode, struct file *file);
  23. static ssize_t etx_read(struct file *filp, char __user *buf, size_t len, loff_t *off);
  24. static ssize_t etx_write(struct file *filp, const char *buf, size_t len, loff_t *off);
  25. static long etx_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  26. static struct file_operations fops =
  27. {
  28. .owner = THIS_MODULE,
  29. .read = etx_read,
  30. .write = etx_write,
  31. .open = etx_open,
  32. .unlocked_ioctl = etx_ioctl,
  33. .release = etx_release,
  34. };
  35. static int etx_open(struct inode *inode, struct file *file)
  36. {
  37. printk(KERN_INFO "Device File Opened...!!!\n");
  38. return 0;
  39. }
  40. static int etx_release(struct inode *inode, struct file *file)
  41. {
  42. printk(KERN_INFO "Device File Closed...!!!\n");
  43. return 0;
  44. }
  45. static ssize_t etx_read(struct file *filp, char __user *buf, size_t len, loff_t *off)
  46. {
  47. printk(KERN_INFO "Read Function\n");
  48. return 0;
  49. }
  50. static ssize_t etx_write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
  51. {
  52. printk(KERN_INFO "Write function\n");
  53. return 0;
  54. }
  55. static long etx_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  56. {
  57. int ret = 0x00;
  58. switch (cmd)
  59. {
  60. case WR_VALUE:
  61. ret = copy_from_user(&value, (int32_t *)arg, sizeof(value));
  62. printk(KERN_INFO "Value = %d\n", value);
  63. break;
  64. case RD_VALUE:
  65. ret = copy_to_user((int32_t *)arg, &value, sizeof(value));
  66. break;
  67. case NONE_Op:
  68. printk("Nonr Argc\n");
  69. break;
  70. case READWRIE_VALUE:
  71. ret = copy_from_user(&value, (int32_t *)arg, sizeof(value));
  72. value = value + 1;
  73. ret = copy_to_user((int32_t *)arg, &value, sizeof(value));
  74. break;
  75. }
  76. (void)ret;
  77. return 0;
  78. }
  79. static int __init etx_driver_init(void)
  80. {
  81. /*Allocating Major number*/
  82. if ((alloc_chrdev_region(&dev, 0, 1, "etx_Dev")) < 0)
  83. {
  84. printk(KERN_INFO "Cannot allocate major number\n");
  85. return -1;
  86. }
  87. printk(KERN_INFO "Major = %d Minor = %d \n", MAJOR(dev), MINOR(dev));
  88. /*Creating cdev structure*/
  89. cdev_init(&etx_cdev, &fops);
  90. /*Adding character device to the system*/
  91. if ((cdev_add(&etx_cdev, dev, 1)) < 0)
  92. {
  93. printk(KERN_INFO "Cannot add the device to the system\n");
  94. goto r_class;
  95. }
  96. /*Creating struct class*/
  97. if ((dev_class = class_create(THIS_MODULE, "etx_class")) == NULL)
  98. {
  99. printk(KERN_INFO "Cannot create the struct class\n");
  100. goto r_class;
  101. }
  102. /*Creating device*/
  103. if ((device_create(dev_class, NULL, dev, NULL, "etx_device")) == NULL)
  104. {
  105. printk(KERN_INFO "Cannot create the Device 1\n");
  106. goto r_device;
  107. }
  108. printk(KERN_INFO "Device Driver Insert...Done!!!\n");
  109. return 0;
  110. r_device:
  111. class_destroy(dev_class);
  112. r_class:
  113. unregister_chrdev_region(dev, 1);
  114. return -1;
  115. }
  116. void __exit etx_driver_exit(void)
  117. {
  118. device_destroy(dev_class, dev);
  119. class_destroy(dev_class);
  120. cdev_del(&etx_cdev);
  121. unregister_chrdev_region(dev, 1);
  122. printk(KERN_INFO "Device Driver Remove...Done!!!\n");
  123. }
  124. module_init(etx_driver_init);
  125. module_exit(etx_driver_exit);
  126. MODULE_LICENSE("GPL");
  127. MODULE_AUTHOR("zhouchengzhu <1073355312@qq.com>");
  128. MODULE_DESCRIPTION("A Sample ioctl module demo");
  129. MODULE_VERSION("2:1.0");

应用

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <sys/ioctl.h>
  9. #define WR_VALUE _IOW('a', 'a', int32_t *)
  10. #define RD_VALUE _IOR('a', 'b', int32_t *)
  11. #define NONE_Op _IO('a', 'c')
  12. #define READWRIE_VALUE _IOWR('a', 'd', int32_t *)
  13. int main()
  14. {
  15. int fd;
  16. int32_t value, number;
  17. printf("*********************************\n");
  18. printf("*******WWW.EmbeTronicX.com*******\n");
  19. printf("\nOpening Driver\n");
  20. fd = open("/dev/etx_device", O_RDWR);
  21. if (fd < 0)
  22. {
  23. printf("Cannot open device file...\n");
  24. return 0;
  25. }
  26. printf("Enter the Value to send\n");
  27. scanf("%d", &number);
  28. printf("Writing Value to Driver\n");
  29. ioctl(fd, WR_VALUE, (int32_t *)&number);
  30. printf("Reading Value from Driver\n");
  31. ioctl(fd, RD_VALUE, (int32_t *)&value);
  32. printf("Value is %d\n", value);
  33. printf("None Value from Driver\n");
  34. ioctl(fd, NONE_Op);
  35. scanf("%d", &number);
  36. printf("Write Value is 0x%x\n", number);
  37. ioctl(fd, READWRIE_VALUE, (int32_t *)&number);
  38. printf("Write Bakc Value is 0x%x\n", number);
  39. printf("Closing Driver\n");
  40. close(fd);
  41. }

参考资料

IOCTL Tutorial in Linux