ioctl.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. struct ioctl_arg {
  12. unsigned int reg;
  13. unsigned int val;
  14. };
  15. /* Documentation/ioctl/ioctl-number.txt */
  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,
  32. unsigned int cmd,
  33. unsigned long arg)
  34. {
  35. struct test_ioctl_data *ioctl_data = filp->private_data;
  36. int retval = 0;
  37. unsigned char val;
  38. struct ioctl_arg data;
  39. memset(&data, 0, sizeof(data));
  40. switch (cmd) {
  41. case IOCTL_VALSET:
  42. /*
  43. if (!capable(CAP_SYS_ADMIN)) {
  44. retval = -EPERM;
  45. goto done;
  46. }
  47. if (!access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd))) {
  48. retval = -EFAULT;
  49. goto done;
  50. }
  51. */
  52. if (copy_from_user(&data, (int __user *) arg, sizeof(data))) {
  53. retval = -EFAULT;
  54. goto done;
  55. }
  56. pr_alert("IOCTL set val:%x .\n", data.val);
  57. write_lock(&ioctl_data->lock);
  58. ioctl_data->val = data.val;
  59. write_unlock(&ioctl_data->lock);
  60. break;
  61. case IOCTL_VALGET:
  62. /*
  63. if (!access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd))) {
  64. retval = -EFAULT;
  65. goto done;
  66. }
  67. */
  68. read_lock(&ioctl_data->lock);
  69. val = ioctl_data->val;
  70. read_unlock(&ioctl_data->lock);
  71. data.val = val;
  72. if (copy_to_user((int __user *) arg, &data, sizeof(data))) {
  73. retval = -EFAULT;
  74. goto done;
  75. }
  76. break;
  77. case IOCTL_VALGET_NUM:
  78. retval = __put_user(ioctl_num, (int __user *) arg);
  79. break;
  80. case IOCTL_VALSET_NUM:
  81. /*
  82. if (!capable(CAP_SYS_ADMIN))
  83. return -EPERM;
  84. */
  85. ioctl_num = arg;
  86. break;
  87. default:
  88. retval = -ENOTTY;
  89. }
  90. done:
  91. return retval;
  92. }
  93. ssize_t test_ioctl_read(struct file *filp,
  94. char __user *buf,
  95. size_t count,
  96. loff_t *f_pos)
  97. {
  98. struct test_ioctl_data *ioctl_data = filp->private_data;
  99. unsigned char val;
  100. int retval;
  101. int i = 0;
  102. read_lock(&ioctl_data->lock);
  103. val = ioctl_data->val;
  104. read_unlock(&ioctl_data->lock);
  105. for (; i < count; i++) {
  106. if (copy_to_user(&buf[i], &val, 1)) {
  107. retval = -EFAULT;
  108. goto out;
  109. }
  110. }
  111. retval = count;
  112. out:
  113. return retval;
  114. }
  115. static int test_ioctl_close(struct inode *inode, struct file *filp)
  116. {
  117. pr_alert("%s call.\n", __func__);
  118. if (filp->private_data) {
  119. kfree(filp->private_data);
  120. filp->private_data = NULL;
  121. }
  122. return 0;
  123. }
  124. static int test_ioctl_open(struct inode *inode, struct file *filp)
  125. {
  126. struct test_ioctl_data *ioctl_data;
  127. pr_alert("%s call.\n", __func__);
  128. ioctl_data = kmalloc(sizeof(struct test_ioctl_data), GFP_KERNEL);
  129. if (ioctl_data == NULL) {
  130. return -ENOMEM;
  131. }
  132. rwlock_init(&ioctl_data->lock);
  133. ioctl_data->val = 0xFF;
  134. filp->private_data = ioctl_data;
  135. return 0;
  136. }
  137. struct file_operations fops = {
  138. .owner = THIS_MODULE,
  139. .open = test_ioctl_open,
  140. .release = test_ioctl_close,
  141. .read = test_ioctl_read,
  142. .unlocked_ioctl = test_ioctl_ioctl,
  143. };
  144. static int ioctl_init(void)
  145. {
  146. dev_t dev = MKDEV(test_ioctl_major, 0);
  147. int alloc_ret = 0;
  148. int cdev_ret = 0;
  149. alloc_ret = alloc_chrdev_region(&dev, 0, num_of_dev, DRIVER_NAME);
  150. if (alloc_ret) {
  151. goto error;
  152. }
  153. test_ioctl_major = MAJOR(dev);
  154. cdev_init(&test_ioctl_cdev, &fops);
  155. cdev_ret = cdev_add(&test_ioctl_cdev, dev, num_of_dev);
  156. if (cdev_ret) {
  157. goto error;
  158. }
  159. pr_alert("%s driver(major: %d) installed.\n", DRIVER_NAME,
  160. test_ioctl_major);
  161. return 0;
  162. error:
  163. if (cdev_ret == 0) {
  164. cdev_del(&test_ioctl_cdev);
  165. }
  166. if (alloc_ret == 0) {
  167. unregister_chrdev_region(dev, num_of_dev);
  168. }
  169. return -1;
  170. }
  171. static void ioctl_exit(void)
  172. {
  173. dev_t dev = MKDEV(test_ioctl_major, 0);
  174. cdev_del(&test_ioctl_cdev);
  175. unregister_chrdev_region(dev, num_of_dev);
  176. pr_alert("%s driver removed.\n", DRIVER_NAME);
  177. }
  178. module_init(ioctl_init);
  179. module_exit(ioctl_exit);
  180. MODULE_LICENSE("GPL");
  181. MODULE_DESCRIPTION("This is test_ioctl module");