ioctl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * ioctl.c
  3. */
  4. #include <linux/cdev.h>
  5. #include <linux/fs.h>
  6. #include <linux/init.h>
  7. #include <linux/ioctl.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/version.h>
  12. struct ioctl_arg {
  13. unsigned int val;
  14. };
  15. /* Documentation/userspace-api/ioctl/ioctl-number.rst */
  16. #define IOC_MAGIC '\x66'
  17. #define IOCTL_VALSET _IOW(IOC_MAGIC, 0, struct ioctl_arg)
  18. #define IOCTL_VALGET _IOR(IOC_MAGIC, 1, struct ioctl_arg)
  19. #define IOCTL_VALGET_NUM _IOR(IOC_MAGIC, 2, int)
  20. #define IOCTL_VALSET_NUM _IOW(IOC_MAGIC, 3, int)
  21. #define IOCTL_VAL_MAXNR 3
  22. #define DRIVER_NAME "ioctltest"
  23. static unsigned int test_ioctl_major = 0;
  24. static unsigned int num_of_dev = 1;
  25. static struct cdev test_ioctl_cdev;
  26. static int ioctl_num = 0;
  27. struct test_ioctl_data {
  28. unsigned char val;
  29. rwlock_t lock;
  30. };
  31. static long test_ioctl_ioctl(struct file *filp, unsigned int cmd,
  32. unsigned long arg)
  33. {
  34. struct test_ioctl_data *ioctl_data = filp->private_data;
  35. int retval = 0;
  36. unsigned char val;
  37. struct ioctl_arg data;
  38. memset(&data, 0, sizeof(data));
  39. switch (cmd) {
  40. case IOCTL_VALSET:
  41. if (copy_from_user(&data, (int __user *)arg, sizeof(data))) {
  42. retval = -EFAULT;
  43. goto done;
  44. }
  45. pr_alert("IOCTL set val:%x .\n", data.val);
  46. write_lock(&ioctl_data->lock);
  47. ioctl_data->val = data.val;
  48. write_unlock(&ioctl_data->lock);
  49. break;
  50. case IOCTL_VALGET:
  51. read_lock(&ioctl_data->lock);
  52. val = ioctl_data->val;
  53. read_unlock(&ioctl_data->lock);
  54. data.val = val;
  55. if (copy_to_user((int __user *)arg, &data, sizeof(data))) {
  56. retval = -EFAULT;
  57. goto done;
  58. }
  59. break;
  60. case IOCTL_VALGET_NUM:
  61. retval = __put_user(ioctl_num, (int __user *)arg);
  62. break;
  63. case IOCTL_VALSET_NUM:
  64. ioctl_num = arg;
  65. break;
  66. default:
  67. retval = -ENOTTY;
  68. }
  69. done:
  70. return retval;
  71. }
  72. static ssize_t test_ioctl_read(struct file *filp, char __user *buf,
  73. size_t count, loff_t *f_pos)
  74. {
  75. struct test_ioctl_data *ioctl_data = filp->private_data;
  76. unsigned char val;
  77. int retval;
  78. int i = 0;
  79. read_lock(&ioctl_data->lock);
  80. val = ioctl_data->val;
  81. read_unlock(&ioctl_data->lock);
  82. for (; i < count; i++) {
  83. if (copy_to_user(&buf[i], &val, 1)) {
  84. retval = -EFAULT;
  85. goto out;
  86. }
  87. }
  88. retval = count;
  89. out:
  90. return retval;
  91. }
  92. static int test_ioctl_close(struct inode *inode, struct file *filp)
  93. {
  94. pr_alert("%s call.\n", __func__);
  95. if (filp->private_data) {
  96. kfree(filp->private_data);
  97. filp->private_data = NULL;
  98. }
  99. return 0;
  100. }
  101. static int test_ioctl_open(struct inode *inode, struct file *filp)
  102. {
  103. struct test_ioctl_data *ioctl_data;
  104. pr_alert("%s call.\n", __func__);
  105. ioctl_data = kmalloc(sizeof(struct test_ioctl_data), GFP_KERNEL);
  106. if (ioctl_data == NULL)
  107. return -ENOMEM;
  108. rwlock_init(&ioctl_data->lock);
  109. ioctl_data->val = 0xFF;
  110. filp->private_data = ioctl_data;
  111. return 0;
  112. }
  113. static struct file_operations fops = {
  114. #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
  115. .owner = THIS_MODULE,
  116. #endif
  117. .open = test_ioctl_open,
  118. .release = test_ioctl_close,
  119. .read = test_ioctl_read,
  120. .unlocked_ioctl = test_ioctl_ioctl,
  121. };
  122. static int __init ioctl_init(void)
  123. {
  124. dev_t dev;
  125. int alloc_ret = -1;
  126. int cdev_ret = -1;
  127. alloc_ret = alloc_chrdev_region(&dev, 0, num_of_dev, DRIVER_NAME);
  128. if (alloc_ret)
  129. goto error;
  130. test_ioctl_major = MAJOR(dev);
  131. cdev_init(&test_ioctl_cdev, &fops);
  132. cdev_ret = cdev_add(&test_ioctl_cdev, dev, num_of_dev);
  133. if (cdev_ret)
  134. goto error;
  135. pr_alert("%s driver(major: %d) installed.\n", DRIVER_NAME,
  136. test_ioctl_major);
  137. return 0;
  138. error:
  139. if (cdev_ret == 0)
  140. cdev_del(&test_ioctl_cdev);
  141. if (alloc_ret == 0)
  142. unregister_chrdev_region(dev, num_of_dev);
  143. return -1;
  144. }
  145. static void __exit ioctl_exit(void)
  146. {
  147. dev_t dev = MKDEV(test_ioctl_major, 0);
  148. cdev_del(&test_ioctl_cdev);
  149. unregister_chrdev_region(dev, num_of_dev);
  150. pr_alert("%s driver removed.\n", DRIVER_NAME);
  151. }
  152. module_init(ioctl_init);
  153. module_exit(ioctl_exit);
  154. MODULE_LICENSE("GPL");
  155. MODULE_DESCRIPTION("This is test_ioctl module");