uhid.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 hid_device *hid;
  28. wait_queue_head_t waitq;
  29. spinlock_t qlock;
  30. __u8 head;
  31. __u8 tail;
  32. struct uhid_event *outq[UHID_BUFSIZE];
  33. };
  34. static struct miscdevice uhid_misc;
  35. static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
  36. {
  37. __u8 newhead;
  38. newhead = (uhid->head + 1) % UHID_BUFSIZE;
  39. if (newhead != uhid->tail) {
  40. uhid->outq[uhid->head] = ev;
  41. uhid->head = newhead;
  42. wake_up_interruptible(&uhid->waitq);
  43. } else {
  44. hid_warn(uhid->hid, "Output queue is full\n");
  45. kfree(ev);
  46. }
  47. }
  48. static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
  49. {
  50. unsigned long flags;
  51. struct uhid_event *ev;
  52. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  53. if (!ev)
  54. return -ENOMEM;
  55. ev->type = event;
  56. spin_lock_irqsave(&uhid->qlock, flags);
  57. uhid_queue(uhid, ev);
  58. spin_unlock_irqrestore(&uhid->qlock, flags);
  59. return 0;
  60. }
  61. static int uhid_char_open(struct inode *inode, struct file *file)
  62. {
  63. struct uhid_device *uhid;
  64. uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
  65. if (!uhid)
  66. return -ENOMEM;
  67. spin_lock_init(&uhid->qlock);
  68. init_waitqueue_head(&uhid->waitq);
  69. file->private_data = uhid;
  70. nonseekable_open(inode, file);
  71. return 0;
  72. }
  73. static int uhid_char_release(struct inode *inode, struct file *file)
  74. {
  75. struct uhid_device *uhid = file->private_data;
  76. unsigned int i;
  77. for (i = 0; i < UHID_BUFSIZE; ++i)
  78. kfree(uhid->outq[i]);
  79. kfree(uhid);
  80. return 0;
  81. }
  82. static ssize_t uhid_char_read(struct file *file, char __user *buffer,
  83. size_t count, loff_t *ppos)
  84. {
  85. return 0;
  86. }
  87. static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
  88. size_t count, loff_t *ppos)
  89. {
  90. return 0;
  91. }
  92. static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
  93. {
  94. struct uhid_device *uhid = file->private_data;
  95. poll_wait(file, &uhid->waitq, wait);
  96. if (uhid->head != uhid->tail)
  97. return POLLIN | POLLRDNORM;
  98. return 0;
  99. }
  100. static const struct file_operations uhid_fops = {
  101. .owner = THIS_MODULE,
  102. .open = uhid_char_open,
  103. .release = uhid_char_release,
  104. .read = uhid_char_read,
  105. .write = uhid_char_write,
  106. .poll = uhid_char_poll,
  107. .llseek = no_llseek,
  108. };
  109. static struct miscdevice uhid_misc = {
  110. .fops = &uhid_fops,
  111. .minor = MISC_DYNAMIC_MINOR,
  112. .name = UHID_NAME,
  113. };
  114. static int __init uhid_init(void)
  115. {
  116. return misc_register(&uhid_misc);
  117. }
  118. static void __exit uhid_exit(void)
  119. {
  120. misc_deregister(&uhid_misc);
  121. }
  122. module_init(uhid_init);
  123. module_exit(uhid_exit);
  124. MODULE_LICENSE("GPL");
  125. MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
  126. MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");