uhid.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. struct hid_device *hid;
  29. struct uhid_event input_buf;
  30. wait_queue_head_t waitq;
  31. spinlock_t qlock;
  32. __u8 head;
  33. __u8 tail;
  34. struct uhid_event *outq[UHID_BUFSIZE];
  35. };
  36. static struct miscdevice uhid_misc;
  37. static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
  38. {
  39. __u8 newhead;
  40. newhead = (uhid->head + 1) % UHID_BUFSIZE;
  41. if (newhead != uhid->tail) {
  42. uhid->outq[uhid->head] = ev;
  43. uhid->head = newhead;
  44. wake_up_interruptible(&uhid->waitq);
  45. } else {
  46. hid_warn(uhid->hid, "Output queue is full\n");
  47. kfree(ev);
  48. }
  49. }
  50. static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
  51. {
  52. unsigned long flags;
  53. struct uhid_event *ev;
  54. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  55. if (!ev)
  56. return -ENOMEM;
  57. ev->type = event;
  58. spin_lock_irqsave(&uhid->qlock, flags);
  59. uhid_queue(uhid, ev);
  60. spin_unlock_irqrestore(&uhid->qlock, flags);
  61. return 0;
  62. }
  63. static int uhid_char_open(struct inode *inode, struct file *file)
  64. {
  65. struct uhid_device *uhid;
  66. uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
  67. if (!uhid)
  68. return -ENOMEM;
  69. mutex_init(&uhid->devlock);
  70. spin_lock_init(&uhid->qlock);
  71. init_waitqueue_head(&uhid->waitq);
  72. file->private_data = uhid;
  73. nonseekable_open(inode, file);
  74. return 0;
  75. }
  76. static int uhid_char_release(struct inode *inode, struct file *file)
  77. {
  78. struct uhid_device *uhid = file->private_data;
  79. unsigned int i;
  80. for (i = 0; i < UHID_BUFSIZE; ++i)
  81. kfree(uhid->outq[i]);
  82. kfree(uhid);
  83. return 0;
  84. }
  85. static ssize_t uhid_char_read(struct file *file, char __user *buffer,
  86. size_t count, loff_t *ppos)
  87. {
  88. struct uhid_device *uhid = file->private_data;
  89. int ret;
  90. unsigned long flags;
  91. size_t len;
  92. /* they need at least the "type" member of uhid_event */
  93. if (count < sizeof(__u32))
  94. return -EINVAL;
  95. try_again:
  96. if (file->f_flags & O_NONBLOCK) {
  97. if (uhid->head == uhid->tail)
  98. return -EAGAIN;
  99. } else {
  100. ret = wait_event_interruptible(uhid->waitq,
  101. uhid->head != uhid->tail);
  102. if (ret)
  103. return ret;
  104. }
  105. ret = mutex_lock_interruptible(&uhid->devlock);
  106. if (ret)
  107. return ret;
  108. if (uhid->head == uhid->tail) {
  109. mutex_unlock(&uhid->devlock);
  110. goto try_again;
  111. } else {
  112. len = min(count, sizeof(**uhid->outq));
  113. if (copy_to_user(buffer, &uhid->outq[uhid->tail], len)) {
  114. ret = -EFAULT;
  115. } else {
  116. kfree(uhid->outq[uhid->tail]);
  117. uhid->outq[uhid->tail] = NULL;
  118. spin_lock_irqsave(&uhid->qlock, flags);
  119. uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
  120. spin_unlock_irqrestore(&uhid->qlock, flags);
  121. }
  122. }
  123. mutex_unlock(&uhid->devlock);
  124. return ret ? ret : len;
  125. }
  126. static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
  127. size_t count, loff_t *ppos)
  128. {
  129. struct uhid_device *uhid = file->private_data;
  130. int ret;
  131. size_t len;
  132. /* we need at least the "type" member of uhid_event */
  133. if (count < sizeof(__u32))
  134. return -EINVAL;
  135. ret = mutex_lock_interruptible(&uhid->devlock);
  136. if (ret)
  137. return ret;
  138. memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
  139. len = min(count, sizeof(uhid->input_buf));
  140. if (copy_from_user(&uhid->input_buf, buffer, len)) {
  141. ret = -EFAULT;
  142. goto unlock;
  143. }
  144. switch (uhid->input_buf.type) {
  145. default:
  146. ret = -EOPNOTSUPP;
  147. }
  148. unlock:
  149. mutex_unlock(&uhid->devlock);
  150. /* return "count" not "len" to not confuse the caller */
  151. return ret ? ret : count;
  152. }
  153. static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
  154. {
  155. struct uhid_device *uhid = file->private_data;
  156. poll_wait(file, &uhid->waitq, wait);
  157. if (uhid->head != uhid->tail)
  158. return POLLIN | POLLRDNORM;
  159. return 0;
  160. }
  161. static const struct file_operations uhid_fops = {
  162. .owner = THIS_MODULE,
  163. .open = uhid_char_open,
  164. .release = uhid_char_release,
  165. .read = uhid_char_read,
  166. .write = uhid_char_write,
  167. .poll = uhid_char_poll,
  168. .llseek = no_llseek,
  169. };
  170. static struct miscdevice uhid_misc = {
  171. .fops = &uhid_fops,
  172. .minor = MISC_DYNAMIC_MINOR,
  173. .name = UHID_NAME,
  174. };
  175. static int __init uhid_init(void)
  176. {
  177. return misc_register(&uhid_misc);
  178. }
  179. static void __exit uhid_exit(void)
  180. {
  181. misc_deregister(&uhid_misc);
  182. }
  183. module_init(uhid_init);
  184. module_exit(uhid_exit);
  185. MODULE_LICENSE("GPL");
  186. MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
  187. MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");