ioctl.c 4.4 KB

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