ioctl.c 4.9 KB

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