1
0

ioctl.c 4.8 KB

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