uhid.c 7.9 KB

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