uhid.c 4.2 KB

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