uhid.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * User-space I/O driver support for HID subsystem
  3. * Copyright (c) 2012 David Herrmann
  4. */
  5. /*
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. */
  11. #include <linux/atomic.h>
  12. #include <linux/device.h>
  13. #include <linux/fs.h>
  14. #include <linux/hid.h>
  15. #include <linux/input.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/poll.h>
  20. #include <linux/sched.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/uhid.h>
  23. #include <linux/wait.h>
  24. #define UHID_NAME "uhid"
  25. #define UHID_BUFSIZE 32
  26. struct uhid_device {
  27. struct mutex devlock;
  28. bool running;
  29. __u8 *rd_data;
  30. uint rd_size;
  31. struct hid_device *hid;
  32. struct uhid_event input_buf;
  33. wait_queue_head_t waitq;
  34. spinlock_t qlock;
  35. __u8 head;
  36. __u8 tail;
  37. struct uhid_event *outq[UHID_BUFSIZE];
  38. };
  39. static struct miscdevice uhid_misc;
  40. static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
  41. {
  42. __u8 newhead;
  43. newhead = (uhid->head + 1) % UHID_BUFSIZE;
  44. if (newhead != uhid->tail) {
  45. uhid->outq[uhid->head] = ev;
  46. uhid->head = newhead;
  47. wake_up_interruptible(&uhid->waitq);
  48. } else {
  49. hid_warn(uhid->hid, "Output queue is full\n");
  50. kfree(ev);
  51. }
  52. }
  53. static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
  54. {
  55. unsigned long flags;
  56. struct uhid_event *ev;
  57. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  58. if (!ev)
  59. return -ENOMEM;
  60. ev->type = event;
  61. spin_lock_irqsave(&uhid->qlock, flags);
  62. uhid_queue(uhid, ev);
  63. spin_unlock_irqrestore(&uhid->qlock, flags);
  64. return 0;
  65. }
  66. static int uhid_hid_start(struct hid_device *hid)
  67. {
  68. struct uhid_device *uhid = hid->driver_data;
  69. return uhid_queue_event(uhid, UHID_START);
  70. }
  71. static void uhid_hid_stop(struct hid_device *hid)
  72. {
  73. struct uhid_device *uhid = hid->driver_data;
  74. hid->claimed = 0;
  75. uhid_queue_event(uhid, UHID_STOP);
  76. }
  77. static int uhid_hid_open(struct hid_device *hid)
  78. {
  79. struct uhid_device *uhid = hid->driver_data;
  80. return uhid_queue_event(uhid, UHID_OPEN);
  81. }
  82. static void uhid_hid_close(struct hid_device *hid)
  83. {
  84. struct uhid_device *uhid = hid->driver_data;
  85. uhid_queue_event(uhid, UHID_CLOSE);
  86. }
  87. static int uhid_hid_input(struct input_dev *input, unsigned int type,
  88. unsigned int code, int value)
  89. {
  90. return 0;
  91. }
  92. static int uhid_hid_parse(struct hid_device *hid)
  93. {
  94. struct uhid_device *uhid = hid->driver_data;
  95. return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
  96. }
  97. static int uhid_hid_get_raw(struct hid_device *hid, unsigned char rnum,
  98. __u8 *buf, size_t count, unsigned char rtype)
  99. {
  100. return 0;
  101. }
  102. static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
  103. unsigned char report_type)
  104. {
  105. return 0;
  106. }
  107. static struct hid_ll_driver uhid_hid_driver = {
  108. .start = uhid_hid_start,
  109. .stop = uhid_hid_stop,
  110. .open = uhid_hid_open,
  111. .close = uhid_hid_close,
  112. .hidinput_input_event = uhid_hid_input,
  113. .parse = uhid_hid_parse,
  114. };
  115. static int uhid_dev_create(struct uhid_device *uhid,
  116. const struct uhid_event *ev)
  117. {
  118. struct hid_device *hid;
  119. int ret;
  120. if (uhid->running)
  121. return -EALREADY;
  122. uhid->rd_size = ev->u.create.rd_size;
  123. if (uhid->rd_size <= 0 || uhid->rd_size > HID_MAX_DESCRIPTOR_SIZE)
  124. return -EINVAL;
  125. uhid->rd_data = kmalloc(uhid->rd_size, GFP_KERNEL);
  126. if (!uhid->rd_data)
  127. return -ENOMEM;
  128. if (copy_from_user(uhid->rd_data, ev->u.create.rd_data,
  129. uhid->rd_size)) {
  130. ret = -EFAULT;
  131. goto err_free;
  132. }
  133. hid = hid_allocate_device();
  134. if (IS_ERR(hid)) {
  135. ret = PTR_ERR(hid);
  136. goto err_free;
  137. }
  138. strncpy(hid->name, ev->u.create.name, 127);
  139. hid->name[127] = 0;
  140. strncpy(hid->phys, ev->u.create.phys, 63);
  141. hid->phys[63] = 0;
  142. strncpy(hid->uniq, ev->u.create.uniq, 63);
  143. hid->uniq[63] = 0;
  144. hid->ll_driver = &uhid_hid_driver;
  145. hid->hid_get_raw_report = uhid_hid_get_raw;
  146. hid->hid_output_raw_report = uhid_hid_output_raw;
  147. hid->bus = ev->u.create.bus;
  148. hid->vendor = ev->u.create.vendor;
  149. hid->product = ev->u.create.product;
  150. hid->version = ev->u.create.version;
  151. hid->country = ev->u.create.country;
  152. hid->driver_data = uhid;
  153. hid->dev.parent = uhid_misc.this_device;
  154. uhid->hid = hid;
  155. uhid->running = true;
  156. ret = hid_add_device(hid);
  157. if (ret) {
  158. hid_err(hid, "Cannot register HID device\n");
  159. goto err_hid;
  160. }
  161. return 0;
  162. err_hid:
  163. hid_destroy_device(hid);
  164. uhid->hid = NULL;
  165. uhid->running = false;
  166. err_free:
  167. kfree(uhid->rd_data);
  168. return ret;
  169. }
  170. static int uhid_dev_destroy(struct uhid_device *uhid)
  171. {
  172. if (!uhid->running)
  173. return -EINVAL;
  174. uhid->running = false;
  175. hid_destroy_device(uhid->hid);
  176. kfree(uhid->rd_data);
  177. return 0;
  178. }
  179. static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
  180. {
  181. if (!uhid->running)
  182. return -EINVAL;
  183. hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
  184. min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
  185. return 0;
  186. }
  187. static int uhid_char_open(struct inode *inode, struct file *file)
  188. {
  189. struct uhid_device *uhid;
  190. uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
  191. if (!uhid)
  192. return -ENOMEM;
  193. mutex_init(&uhid->devlock);
  194. spin_lock_init(&uhid->qlock);
  195. init_waitqueue_head(&uhid->waitq);
  196. uhid->running = false;
  197. file->private_data = uhid;
  198. nonseekable_open(inode, file);
  199. return 0;
  200. }
  201. static int uhid_char_release(struct inode *inode, struct file *file)
  202. {
  203. struct uhid_device *uhid = file->private_data;
  204. unsigned int i;
  205. uhid_dev_destroy(uhid);
  206. for (i = 0; i < UHID_BUFSIZE; ++i)
  207. kfree(uhid->outq[i]);
  208. kfree(uhid);
  209. return 0;
  210. }
  211. static ssize_t uhid_char_read(struct file *file, char __user *buffer,
  212. size_t count, loff_t *ppos)
  213. {
  214. struct uhid_device *uhid = file->private_data;
  215. int ret;
  216. unsigned long flags;
  217. size_t len;
  218. /* they need at least the "type" member of uhid_event */
  219. if (count < sizeof(__u32))
  220. return -EINVAL;
  221. try_again:
  222. if (file->f_flags & O_NONBLOCK) {
  223. if (uhid->head == uhid->tail)
  224. return -EAGAIN;
  225. } else {
  226. ret = wait_event_interruptible(uhid->waitq,
  227. uhid->head != uhid->tail);
  228. if (ret)
  229. return ret;
  230. }
  231. ret = mutex_lock_interruptible(&uhid->devlock);
  232. if (ret)
  233. return ret;
  234. if (uhid->head == uhid->tail) {
  235. mutex_unlock(&uhid->devlock);
  236. goto try_again;
  237. } else {
  238. len = min(count, sizeof(**uhid->outq));
  239. if (copy_to_user(buffer, &uhid->outq[uhid->tail], len)) {
  240. ret = -EFAULT;
  241. } else {
  242. kfree(uhid->outq[uhid->tail]);
  243. uhid->outq[uhid->tail] = NULL;
  244. spin_lock_irqsave(&uhid->qlock, flags);
  245. uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
  246. spin_unlock_irqrestore(&uhid->qlock, flags);
  247. }
  248. }
  249. mutex_unlock(&uhid->devlock);
  250. return ret ? ret : len;
  251. }
  252. static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
  253. size_t count, loff_t *ppos)
  254. {
  255. struct uhid_device *uhid = file->private_data;
  256. int ret;
  257. size_t len;
  258. /* we need at least the "type" member of uhid_event */
  259. if (count < sizeof(__u32))
  260. return -EINVAL;
  261. ret = mutex_lock_interruptible(&uhid->devlock);
  262. if (ret)
  263. return ret;
  264. memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
  265. len = min(count, sizeof(uhid->input_buf));
  266. if (copy_from_user(&uhid->input_buf, buffer, len)) {
  267. ret = -EFAULT;
  268. goto unlock;
  269. }
  270. switch (uhid->input_buf.type) {
  271. case UHID_CREATE:
  272. ret = uhid_dev_create(uhid, &uhid->input_buf);
  273. break;
  274. case UHID_DESTROY:
  275. ret = uhid_dev_destroy(uhid);
  276. break;
  277. case UHID_INPUT:
  278. ret = uhid_dev_input(uhid, &uhid->input_buf);
  279. break;
  280. default:
  281. ret = -EOPNOTSUPP;
  282. }
  283. unlock:
  284. mutex_unlock(&uhid->devlock);
  285. /* return "count" not "len" to not confuse the caller */
  286. return ret ? ret : count;
  287. }
  288. static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
  289. {
  290. struct uhid_device *uhid = file->private_data;
  291. poll_wait(file, &uhid->waitq, wait);
  292. if (uhid->head != uhid->tail)
  293. return POLLIN | POLLRDNORM;
  294. return 0;
  295. }
  296. static const struct file_operations uhid_fops = {
  297. .owner = THIS_MODULE,
  298. .open = uhid_char_open,
  299. .release = uhid_char_release,
  300. .read = uhid_char_read,
  301. .write = uhid_char_write,
  302. .poll = uhid_char_poll,
  303. .llseek = no_llseek,
  304. };
  305. static struct miscdevice uhid_misc = {
  306. .fops = &uhid_fops,
  307. .minor = MISC_DYNAMIC_MINOR,
  308. .name = UHID_NAME,
  309. };
  310. static int __init uhid_init(void)
  311. {
  312. return misc_register(&uhid_misc);
  313. }
  314. static void __exit uhid_exit(void)
  315. {
  316. misc_deregister(&uhid_misc);
  317. }
  318. module_init(uhid_init);
  319. module_exit(uhid_exit);
  320. MODULE_LICENSE("GPL");
  321. MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
  322. MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");