uhid.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. return 0;
  80. }
  81. static void uhid_hid_close(struct hid_device *hid)
  82. {
  83. }
  84. static int uhid_hid_input(struct input_dev *input, unsigned int type,
  85. unsigned int code, int value)
  86. {
  87. return 0;
  88. }
  89. static int uhid_hid_parse(struct hid_device *hid)
  90. {
  91. struct uhid_device *uhid = hid->driver_data;
  92. return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
  93. }
  94. static int uhid_hid_get_raw(struct hid_device *hid, unsigned char rnum,
  95. __u8 *buf, size_t count, unsigned char rtype)
  96. {
  97. return 0;
  98. }
  99. static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
  100. unsigned char report_type)
  101. {
  102. return 0;
  103. }
  104. static struct hid_ll_driver uhid_hid_driver = {
  105. .start = uhid_hid_start,
  106. .stop = uhid_hid_stop,
  107. .open = uhid_hid_open,
  108. .close = uhid_hid_close,
  109. .hidinput_input_event = uhid_hid_input,
  110. .parse = uhid_hid_parse,
  111. };
  112. static int uhid_dev_create(struct uhid_device *uhid,
  113. const struct uhid_event *ev)
  114. {
  115. struct hid_device *hid;
  116. int ret;
  117. if (uhid->running)
  118. return -EALREADY;
  119. uhid->rd_size = ev->u.create.rd_size;
  120. if (uhid->rd_size <= 0 || uhid->rd_size > HID_MAX_DESCRIPTOR_SIZE)
  121. return -EINVAL;
  122. uhid->rd_data = kmalloc(uhid->rd_size, GFP_KERNEL);
  123. if (!uhid->rd_data)
  124. return -ENOMEM;
  125. if (copy_from_user(uhid->rd_data, ev->u.create.rd_data,
  126. uhid->rd_size)) {
  127. ret = -EFAULT;
  128. goto err_free;
  129. }
  130. hid = hid_allocate_device();
  131. if (IS_ERR(hid)) {
  132. ret = PTR_ERR(hid);
  133. goto err_free;
  134. }
  135. strncpy(hid->name, ev->u.create.name, 127);
  136. hid->name[127] = 0;
  137. strncpy(hid->phys, ev->u.create.phys, 63);
  138. hid->phys[63] = 0;
  139. strncpy(hid->uniq, ev->u.create.uniq, 63);
  140. hid->uniq[63] = 0;
  141. hid->ll_driver = &uhid_hid_driver;
  142. hid->hid_get_raw_report = uhid_hid_get_raw;
  143. hid->hid_output_raw_report = uhid_hid_output_raw;
  144. hid->bus = ev->u.create.bus;
  145. hid->vendor = ev->u.create.vendor;
  146. hid->product = ev->u.create.product;
  147. hid->version = ev->u.create.version;
  148. hid->country = ev->u.create.country;
  149. hid->driver_data = uhid;
  150. hid->dev.parent = uhid_misc.this_device;
  151. uhid->hid = hid;
  152. uhid->running = true;
  153. ret = hid_add_device(hid);
  154. if (ret) {
  155. hid_err(hid, "Cannot register HID device\n");
  156. goto err_hid;
  157. }
  158. return 0;
  159. err_hid:
  160. hid_destroy_device(hid);
  161. uhid->hid = NULL;
  162. uhid->running = false;
  163. err_free:
  164. kfree(uhid->rd_data);
  165. return ret;
  166. }
  167. static int uhid_dev_destroy(struct uhid_device *uhid)
  168. {
  169. if (!uhid->running)
  170. return -EINVAL;
  171. uhid->running = false;
  172. hid_destroy_device(uhid->hid);
  173. kfree(uhid->rd_data);
  174. return 0;
  175. }
  176. static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
  177. {
  178. if (!uhid->running)
  179. return -EINVAL;
  180. hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
  181. min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
  182. return 0;
  183. }
  184. static int uhid_char_open(struct inode *inode, struct file *file)
  185. {
  186. struct uhid_device *uhid;
  187. uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
  188. if (!uhid)
  189. return -ENOMEM;
  190. mutex_init(&uhid->devlock);
  191. spin_lock_init(&uhid->qlock);
  192. init_waitqueue_head(&uhid->waitq);
  193. uhid->running = false;
  194. file->private_data = uhid;
  195. nonseekable_open(inode, file);
  196. return 0;
  197. }
  198. static int uhid_char_release(struct inode *inode, struct file *file)
  199. {
  200. struct uhid_device *uhid = file->private_data;
  201. unsigned int i;
  202. uhid_dev_destroy(uhid);
  203. for (i = 0; i < UHID_BUFSIZE; ++i)
  204. kfree(uhid->outq[i]);
  205. kfree(uhid);
  206. return 0;
  207. }
  208. static ssize_t uhid_char_read(struct file *file, char __user *buffer,
  209. size_t count, loff_t *ppos)
  210. {
  211. struct uhid_device *uhid = file->private_data;
  212. int ret;
  213. unsigned long flags;
  214. size_t len;
  215. /* they need at least the "type" member of uhid_event */
  216. if (count < sizeof(__u32))
  217. return -EINVAL;
  218. try_again:
  219. if (file->f_flags & O_NONBLOCK) {
  220. if (uhid->head == uhid->tail)
  221. return -EAGAIN;
  222. } else {
  223. ret = wait_event_interruptible(uhid->waitq,
  224. uhid->head != uhid->tail);
  225. if (ret)
  226. return ret;
  227. }
  228. ret = mutex_lock_interruptible(&uhid->devlock);
  229. if (ret)
  230. return ret;
  231. if (uhid->head == uhid->tail) {
  232. mutex_unlock(&uhid->devlock);
  233. goto try_again;
  234. } else {
  235. len = min(count, sizeof(**uhid->outq));
  236. if (copy_to_user(buffer, &uhid->outq[uhid->tail], len)) {
  237. ret = -EFAULT;
  238. } else {
  239. kfree(uhid->outq[uhid->tail]);
  240. uhid->outq[uhid->tail] = NULL;
  241. spin_lock_irqsave(&uhid->qlock, flags);
  242. uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
  243. spin_unlock_irqrestore(&uhid->qlock, flags);
  244. }
  245. }
  246. mutex_unlock(&uhid->devlock);
  247. return ret ? ret : len;
  248. }
  249. static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
  250. size_t count, loff_t *ppos)
  251. {
  252. struct uhid_device *uhid = file->private_data;
  253. int ret;
  254. size_t len;
  255. /* we need at least the "type" member of uhid_event */
  256. if (count < sizeof(__u32))
  257. return -EINVAL;
  258. ret = mutex_lock_interruptible(&uhid->devlock);
  259. if (ret)
  260. return ret;
  261. memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
  262. len = min(count, sizeof(uhid->input_buf));
  263. if (copy_from_user(&uhid->input_buf, buffer, len)) {
  264. ret = -EFAULT;
  265. goto unlock;
  266. }
  267. switch (uhid->input_buf.type) {
  268. case UHID_CREATE:
  269. ret = uhid_dev_create(uhid, &uhid->input_buf);
  270. break;
  271. case UHID_DESTROY:
  272. ret = uhid_dev_destroy(uhid);
  273. break;
  274. case UHID_INPUT:
  275. ret = uhid_dev_input(uhid, &uhid->input_buf);
  276. break;
  277. default:
  278. ret = -EOPNOTSUPP;
  279. }
  280. unlock:
  281. mutex_unlock(&uhid->devlock);
  282. /* return "count" not "len" to not confuse the caller */
  283. return ret ? ret : count;
  284. }
  285. static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
  286. {
  287. struct uhid_device *uhid = file->private_data;
  288. poll_wait(file, &uhid->waitq, wait);
  289. if (uhid->head != uhid->tail)
  290. return POLLIN | POLLRDNORM;
  291. return 0;
  292. }
  293. static const struct file_operations uhid_fops = {
  294. .owner = THIS_MODULE,
  295. .open = uhid_char_open,
  296. .release = uhid_char_release,
  297. .read = uhid_char_read,
  298. .write = uhid_char_write,
  299. .poll = uhid_char_poll,
  300. .llseek = no_llseek,
  301. };
  302. static struct miscdevice uhid_misc = {
  303. .fops = &uhid_fops,
  304. .minor = MISC_DYNAMIC_MINOR,
  305. .name = UHID_NAME,
  306. };
  307. static int __init uhid_init(void)
  308. {
  309. return misc_register(&uhid_misc);
  310. }
  311. static void __exit uhid_exit(void)
  312. {
  313. misc_deregister(&uhid_misc);
  314. }
  315. module_init(uhid_init);
  316. module_exit(uhid_exit);
  317. MODULE_LICENSE("GPL");
  318. MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
  319. MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");