ioctl.c 4.2 KB

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