vinput.c 9.5 KB

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