vinput.c 9.5 KB

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