1
0

vinput.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * vinput.c
  3. */
  4. #include <linux/cdev.h>
  5. #include <linux/input.h>
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/version.h>
  10. #include <asm/uaccess.h>
  11. #include "vinput.h"
  12. #define DRIVER_NAME "vinput"
  13. #define dev_to_vinput(dev) container_of(dev, struct vinput, dev)
  14. static DECLARE_BITMAP(vinput_ids, VINPUT_MINORS);
  15. static LIST_HEAD(vinput_devices);
  16. static LIST_HEAD(vinput_vdevices);
  17. static int vinput_dev;
  18. static struct spinlock vinput_lock;
  19. static struct class vinput_class;
  20. /* Search the name of vinput device in the vinput_devices linked list,
  21. * which added at vinput_register().
  22. */
  23. static struct vinput_device *vinput_get_device_by_type(const char *type)
  24. {
  25. int found = 0;
  26. struct vinput_device *vinput;
  27. struct list_head *curr;
  28. spin_lock(&vinput_lock);
  29. list_for_each (curr, &vinput_devices) {
  30. vinput = list_entry(curr, struct vinput_device, list);
  31. if (vinput && strncmp(type, vinput->name, strlen(vinput->name)) == 0) {
  32. found = 1;
  33. break;
  34. }
  35. }
  36. spin_unlock(&vinput_lock);
  37. if (found)
  38. return vinput;
  39. return ERR_PTR(-ENODEV);
  40. }
  41. /* Search the id of virtual device in the vinput_vdevices linked list,
  42. * which added at vinput_alloc_vdevice().
  43. */
  44. static struct vinput *vinput_get_vdevice_by_id(long id)
  45. {
  46. struct vinput *vinput = NULL;
  47. struct list_head *curr;
  48. spin_lock(&vinput_lock);
  49. list_for_each (curr, &vinput_vdevices) {
  50. vinput = list_entry(curr, struct vinput, list);
  51. if (vinput && vinput->id == id)
  52. break;
  53. }
  54. spin_unlock(&vinput_lock);
  55. if (vinput && vinput->id == id)
  56. return vinput;
  57. return ERR_PTR(-ENODEV);
  58. }
  59. static int vinput_open(struct inode *inode, struct file *file)
  60. {
  61. int err = 0;
  62. struct vinput *vinput = NULL;
  63. vinput = vinput_get_vdevice_by_id(iminor(inode));
  64. if (IS_ERR(vinput))
  65. err = PTR_ERR(vinput);
  66. else
  67. file->private_data = vinput;
  68. return err;
  69. }
  70. static int vinput_release(struct inode *inode, struct file *file)
  71. {
  72. return 0;
  73. }
  74. static ssize_t vinput_read(struct file *file, char __user *buffer, size_t count,
  75. loff_t *offset)
  76. {
  77. int len;
  78. char buff[VINPUT_MAX_LEN + 1];
  79. struct vinput *vinput = file->private_data;
  80. len = vinput->type->ops->read(vinput, buff, count);
  81. if (*offset > len)
  82. count = 0;
  83. else if (count + *offset > VINPUT_MAX_LEN)
  84. count = len - *offset;
  85. if (raw_copy_to_user(buffer, buff + *offset, count))
  86. count = -EFAULT;
  87. *offset += count;
  88. return count;
  89. }
  90. static ssize_t vinput_write(struct file *file, const char __user *buffer,
  91. size_t count, loff_t *offset)
  92. {
  93. char buff[VINPUT_MAX_LEN + 1];
  94. struct vinput *vinput = file->private_data;
  95. memset(buff, 0, sizeof(char) * (VINPUT_MAX_LEN + 1));
  96. if (count > VINPUT_MAX_LEN) {
  97. dev_warn(&vinput->dev, "Too long. %d bytes allowed\n", VINPUT_MAX_LEN);
  98. return -EINVAL;
  99. }
  100. if (raw_copy_from_user(buff, buffer, count))
  101. return -EFAULT;
  102. return vinput->type->ops->send(vinput, buff, count);
  103. }
  104. static const struct file_operations vinput_fops = {
  105. #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
  106. .owner = THIS_MODULE,
  107. #endif
  108. .open = vinput_open,
  109. .release = vinput_release,
  110. .read = vinput_read,
  111. .write = vinput_write,
  112. };
  113. static void vinput_unregister_vdevice(struct vinput *vinput)
  114. {
  115. input_unregister_device(vinput->input);
  116. if (vinput->type->ops->kill)
  117. vinput->type->ops->kill(vinput);
  118. }
  119. static void vinput_destroy_vdevice(struct vinput *vinput)
  120. {
  121. /* Remove from the list first */
  122. spin_lock(&vinput_lock);
  123. list_del(&vinput->list);
  124. clear_bit(vinput->id, vinput_ids);
  125. spin_unlock(&vinput_lock);
  126. module_put(THIS_MODULE);
  127. kfree(vinput);
  128. }
  129. static void vinput_release_dev(struct device *dev)
  130. {
  131. struct vinput *vinput = dev_to_vinput(dev);
  132. int id = vinput->id;
  133. vinput_destroy_vdevice(vinput);
  134. pr_debug("released vinput%d.\n", id);
  135. }
  136. static struct vinput *vinput_alloc_vdevice(void)
  137. {
  138. int err;
  139. struct vinput *vinput = kzalloc(sizeof(struct vinput), GFP_KERNEL);
  140. try_module_get(THIS_MODULE);
  141. memset(vinput, 0, sizeof(struct vinput));
  142. spin_lock_init(&vinput->lock);
  143. spin_lock(&vinput_lock);
  144. vinput->id = find_first_zero_bit(vinput_ids, VINPUT_MINORS);
  145. if (vinput->id >= VINPUT_MINORS) {
  146. err = -ENOBUFS;
  147. goto fail_id;
  148. }
  149. set_bit(vinput->id, vinput_ids);
  150. list_add(&vinput->list, &vinput_vdevices);
  151. spin_unlock(&vinput_lock);
  152. /* allocate the input device */
  153. vinput->input = input_allocate_device();
  154. if (vinput->input == NULL) {
  155. pr_err("vinput: Cannot allocate vinput input device\n");
  156. err = -ENOMEM;
  157. goto fail_input_dev;
  158. }
  159. /* initialize device */
  160. vinput->dev.class = &vinput_class;
  161. vinput->dev.release = vinput_release_dev;
  162. vinput->dev.devt = MKDEV(vinput_dev, vinput->id);
  163. dev_set_name(&vinput->dev, DRIVER_NAME "%lu", vinput->id);
  164. return vinput;
  165. fail_input_dev:
  166. spin_lock(&vinput_lock);
  167. list_del(&vinput->list);
  168. fail_id:
  169. spin_unlock(&vinput_lock);
  170. module_put(THIS_MODULE);
  171. kfree(vinput);
  172. return ERR_PTR(err);
  173. }
  174. static int vinput_register_vdevice(struct vinput *vinput)
  175. {
  176. int err = 0;
  177. /* register the input device */
  178. vinput->input->name = vinput->type->name;
  179. vinput->input->phys = "vinput";
  180. vinput->input->dev.parent = &vinput->dev;
  181. vinput->input->id.bustype = BUS_VIRTUAL;
  182. vinput->input->id.product = 0x0000;
  183. vinput->input->id.vendor = 0x0000;
  184. vinput->input->id.version = 0x0000;
  185. err = vinput->type->ops->init(vinput);
  186. if (err == 0)
  187. dev_info(&vinput->dev, "Registered virtual input %s %ld\n",
  188. vinput->type->name, vinput->id);
  189. return err;
  190. }
  191. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
  192. static ssize_t export_store(const struct class *class,
  193. const struct class_attribute *attr,
  194. #else
  195. static ssize_t export_store(struct class *class, struct class_attribute *attr,
  196. #endif
  197. const char *buf, size_t len)
  198. {
  199. int err;
  200. struct vinput *vinput;
  201. struct vinput_device *device;
  202. device = vinput_get_device_by_type(buf);
  203. if (IS_ERR(device)) {
  204. pr_info("vinput: This virtual device isn't registered\n");
  205. err = PTR_ERR(device);
  206. goto fail;
  207. }
  208. vinput = vinput_alloc_vdevice();
  209. if (IS_ERR(vinput)) {
  210. err = PTR_ERR(vinput);
  211. goto fail;
  212. }
  213. vinput->type = device;
  214. err = device_register(&vinput->dev);
  215. if (err < 0)
  216. goto fail_register;
  217. err = vinput_register_vdevice(vinput);
  218. if (err < 0)
  219. goto fail_register_vinput;
  220. return len;
  221. fail_register_vinput:
  222. device_unregister(&vinput->dev);
  223. fail_register:
  224. vinput_destroy_vdevice(vinput);
  225. fail:
  226. return err;
  227. }
  228. /* This macro generates class_attr_export structure and export_store() */
  229. static CLASS_ATTR_WO(export);
  230. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
  231. static ssize_t unexport_store(const struct class *class,
  232. const struct class_attribute *attr,
  233. #else
  234. static ssize_t unexport_store(struct class *class, struct class_attribute *attr,
  235. #endif
  236. const char *buf, size_t len)
  237. {
  238. int err;
  239. unsigned long id;
  240. struct vinput *vinput;
  241. err = kstrtol(buf, 10, &id);
  242. if (err) {
  243. err = -EINVAL;
  244. goto failed;
  245. }
  246. vinput = vinput_get_vdevice_by_id(id);
  247. if (IS_ERR(vinput)) {
  248. pr_err("vinput: No such vinput device %ld\n", id);
  249. err = PTR_ERR(vinput);
  250. goto failed;
  251. }
  252. vinput_unregister_vdevice(vinput);
  253. device_unregister(&vinput->dev);
  254. return len;
  255. failed:
  256. return err;
  257. }
  258. /* This macro generates class_attr_unexport structure and unexport_store() */
  259. static CLASS_ATTR_WO(unexport);
  260. static struct attribute *vinput_class_attrs[] = {
  261. &class_attr_export.attr,
  262. &class_attr_unexport.attr,
  263. NULL,
  264. };
  265. /* This macro generates vinput_class_groups structure */
  266. ATTRIBUTE_GROUPS(vinput_class);
  267. static struct class vinput_class = {
  268. .name = "vinput",
  269. #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
  270. .owner = THIS_MODULE,
  271. #endif
  272. .class_groups = vinput_class_groups,
  273. };
  274. int vinput_register(struct vinput_device *dev)
  275. {
  276. spin_lock(&vinput_lock);
  277. list_add(&dev->list, &vinput_devices);
  278. spin_unlock(&vinput_lock);
  279. pr_info("vinput: registered new virtual input device '%s'\n", dev->name);
  280. return 0;
  281. }
  282. EXPORT_SYMBOL(vinput_register);
  283. void vinput_unregister(struct vinput_device *dev)
  284. {
  285. struct list_head *curr, *next;
  286. /* Remove from the list first */
  287. spin_lock(&vinput_lock);
  288. list_del(&dev->list);
  289. spin_unlock(&vinput_lock);
  290. /* unregister all devices of this type */
  291. list_for_each_safe (curr, next, &vinput_vdevices) {
  292. struct vinput *vinput = list_entry(curr, struct vinput, list);
  293. if (vinput && vinput->type == dev) {
  294. vinput_unregister_vdevice(vinput);
  295. device_unregister(&vinput->dev);
  296. }
  297. }
  298. pr_info("vinput: unregistered virtual input device '%s'\n", dev->name);
  299. }
  300. EXPORT_SYMBOL(vinput_unregister);
  301. static int __init vinput_init(void)
  302. {
  303. int err = 0;
  304. pr_info("vinput: Loading virtual input driver\n");
  305. vinput_dev = register_chrdev(0, DRIVER_NAME, &vinput_fops);
  306. if (vinput_dev < 0) {
  307. pr_err("vinput: Unable to allocate char dev region\n");
  308. err = vinput_dev;
  309. goto failed_alloc;
  310. }
  311. spin_lock_init(&vinput_lock);
  312. err = class_register(&vinput_class);
  313. if (err < 0) {
  314. pr_err("vinput: Unable to register vinput class\n");
  315. goto failed_class;
  316. }
  317. return 0;
  318. failed_class:
  319. class_unregister(&vinput_class);
  320. failed_alloc:
  321. return err;
  322. }
  323. static void __exit vinput_end(void)
  324. {
  325. pr_info("vinput: Unloading virtual input driver\n");
  326. unregister_chrdev(vinput_dev, DRIVER_NAME);
  327. class_unregister(&vinput_class);
  328. }
  329. module_init(vinput_init);
  330. module_exit(vinput_end);
  331. MODULE_LICENSE("GPL");
  332. MODULE_DESCRIPTION("Emulate input events");