1
0

vinput.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. return -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. if (!vinput) {
  141. pr_err("vinput: Cannot allocate vinput input device\n");
  142. return ERR_PTR(-ENOMEM);
  143. }
  144. try_module_get(THIS_MODULE);
  145. spin_lock_init(&vinput->lock);
  146. spin_lock(&vinput_lock);
  147. vinput->id = find_first_zero_bit(vinput_ids, VINPUT_MINORS);
  148. if (vinput->id >= VINPUT_MINORS) {
  149. err = -ENOBUFS;
  150. goto fail_id;
  151. }
  152. set_bit(vinput->id, vinput_ids);
  153. list_add(&vinput->list, &vinput_vdevices);
  154. spin_unlock(&vinput_lock);
  155. /* allocate the input device */
  156. vinput->input = input_allocate_device();
  157. if (vinput->input == NULL) {
  158. pr_err("vinput: Cannot allocate vinput input device\n");
  159. err = -ENOMEM;
  160. goto fail_input_dev;
  161. }
  162. /* initialize device */
  163. vinput->dev.class = &vinput_class;
  164. vinput->dev.release = vinput_release_dev;
  165. vinput->dev.devt = MKDEV(vinput_dev, vinput->id);
  166. dev_set_name(&vinput->dev, DRIVER_NAME "%lu", vinput->id);
  167. return vinput;
  168. fail_input_dev:
  169. spin_lock(&vinput_lock);
  170. list_del(&vinput->list);
  171. fail_id:
  172. spin_unlock(&vinput_lock);
  173. module_put(THIS_MODULE);
  174. kfree(vinput);
  175. return ERR_PTR(err);
  176. }
  177. static int vinput_register_vdevice(struct vinput *vinput)
  178. {
  179. int err = 0;
  180. /* register the input device */
  181. vinput->input->name = vinput->type->name;
  182. vinput->input->phys = "vinput";
  183. vinput->input->dev.parent = &vinput->dev;
  184. vinput->input->id.bustype = BUS_VIRTUAL;
  185. vinput->input->id.product = 0x0000;
  186. vinput->input->id.vendor = 0x0000;
  187. vinput->input->id.version = 0x0000;
  188. err = vinput->type->ops->init(vinput);
  189. if (err == 0)
  190. dev_info(&vinput->dev, "Registered virtual input %s %ld\n",
  191. vinput->type->name, vinput->id);
  192. return err;
  193. }
  194. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
  195. static ssize_t export_store(const struct class *class,
  196. const struct class_attribute *attr,
  197. #else
  198. static ssize_t export_store(struct class *class, struct class_attribute *attr,
  199. #endif
  200. const char *buf, size_t len)
  201. {
  202. int err;
  203. struct vinput *vinput;
  204. struct vinput_device *device;
  205. device = vinput_get_device_by_type(buf);
  206. if (IS_ERR(device)) {
  207. pr_info("vinput: This virtual device isn't registered\n");
  208. err = PTR_ERR(device);
  209. goto fail;
  210. }
  211. vinput = vinput_alloc_vdevice();
  212. if (IS_ERR(vinput)) {
  213. err = PTR_ERR(vinput);
  214. goto fail;
  215. }
  216. vinput->type = device;
  217. err = device_register(&vinput->dev);
  218. if (err < 0)
  219. goto fail_register;
  220. err = vinput_register_vdevice(vinput);
  221. if (err < 0)
  222. goto fail_register_vinput;
  223. return len;
  224. fail_register_vinput:
  225. input_free_device(vinput->input);
  226. device_unregister(&vinput->dev);
  227. /* avoid calling vinput_destroy_vdevice() twice */
  228. return err;
  229. fail_register:
  230. input_free_device(vinput->input);
  231. vinput_destroy_vdevice(vinput);
  232. fail:
  233. return err;
  234. }
  235. /* This macro generates class_attr_export structure and export_store() */
  236. static CLASS_ATTR_WO(export);
  237. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
  238. static ssize_t unexport_store(const struct class *class,
  239. const struct class_attribute *attr,
  240. #else
  241. static ssize_t unexport_store(struct class *class, struct class_attribute *attr,
  242. #endif
  243. const char *buf, size_t len)
  244. {
  245. int err;
  246. unsigned long id;
  247. struct vinput *vinput;
  248. err = kstrtol(buf, 10, &id);
  249. if (err) {
  250. err = -EINVAL;
  251. goto failed;
  252. }
  253. vinput = vinput_get_vdevice_by_id(id);
  254. if (IS_ERR(vinput)) {
  255. pr_err("vinput: No such vinput device %ld\n", id);
  256. err = PTR_ERR(vinput);
  257. goto failed;
  258. }
  259. vinput_unregister_vdevice(vinput);
  260. device_unregister(&vinput->dev);
  261. return len;
  262. failed:
  263. return err;
  264. }
  265. /* This macro generates class_attr_unexport structure and unexport_store() */
  266. static CLASS_ATTR_WO(unexport);
  267. static struct attribute *vinput_class_attrs[] = {
  268. &class_attr_export.attr,
  269. &class_attr_unexport.attr,
  270. NULL,
  271. };
  272. /* This macro generates vinput_class_groups structure */
  273. ATTRIBUTE_GROUPS(vinput_class);
  274. static struct class vinput_class = {
  275. .name = "vinput",
  276. #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
  277. .owner = THIS_MODULE,
  278. #endif
  279. .class_groups = vinput_class_groups,
  280. };
  281. int vinput_register(struct vinput_device *dev)
  282. {
  283. spin_lock(&vinput_lock);
  284. list_add(&dev->list, &vinput_devices);
  285. spin_unlock(&vinput_lock);
  286. pr_info("vinput: registered new virtual input device '%s'\n", dev->name);
  287. return 0;
  288. }
  289. EXPORT_SYMBOL(vinput_register);
  290. void vinput_unregister(struct vinput_device *dev)
  291. {
  292. struct list_head *curr, *next;
  293. /* Remove from the list first */
  294. spin_lock(&vinput_lock);
  295. list_del(&dev->list);
  296. spin_unlock(&vinput_lock);
  297. /* unregister all devices of this type */
  298. list_for_each_safe (curr, next, &vinput_vdevices) {
  299. struct vinput *vinput = list_entry(curr, struct vinput, list);
  300. if (vinput && vinput->type == dev) {
  301. vinput_unregister_vdevice(vinput);
  302. device_unregister(&vinput->dev);
  303. }
  304. }
  305. pr_info("vinput: unregistered virtual input device '%s'\n", dev->name);
  306. }
  307. EXPORT_SYMBOL(vinput_unregister);
  308. static int __init vinput_init(void)
  309. {
  310. int err = 0;
  311. pr_info("vinput: Loading virtual input driver\n");
  312. vinput_dev = register_chrdev(0, DRIVER_NAME, &vinput_fops);
  313. if (vinput_dev < 0) {
  314. pr_err("vinput: Unable to allocate char dev region\n");
  315. err = vinput_dev;
  316. goto failed_alloc;
  317. }
  318. spin_lock_init(&vinput_lock);
  319. err = class_register(&vinput_class);
  320. if (err < 0) {
  321. pr_err("vinput: Unable to register vinput class\n");
  322. goto failed_class;
  323. }
  324. return 0;
  325. failed_class:
  326. unregister_chrdev(vinput_dev, DRIVER_NAME);
  327. failed_alloc:
  328. return err;
  329. }
  330. static void __exit vinput_end(void)
  331. {
  332. pr_info("vinput: Unloading virtual input driver\n");
  333. unregister_chrdev(vinput_dev, DRIVER_NAME);
  334. class_unregister(&vinput_class);
  335. }
  336. module_init(vinput_init);
  337. module_exit(vinput_end);
  338. MODULE_LICENSE("GPL");
  339. MODULE_DESCRIPTION("Emulate input events");