userio.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * userio kernel serio device emulation module
  3. * Copyright (C) 2015 Red Hat
  4. * Copyright (C) 2015 Stephen Chandler Paul <thatslyude@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  14. * General Public License for more details.
  15. */
  16. #include <linux/circ_buf.h>
  17. #include <linux/mutex.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/serio.h>
  22. #include <linux/slab.h>
  23. #include <linux/fs.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/sched.h>
  26. #include <linux/poll.h>
  27. #include <uapi/linux/userio.h>
  28. #define USERIO_NAME "userio"
  29. #define USERIO_BUFSIZE 16
  30. static struct miscdevice userio_misc;
  31. struct userio_device {
  32. struct serio *serio;
  33. struct mutex mutex;
  34. bool running;
  35. u8 head;
  36. u8 tail;
  37. spinlock_t buf_lock;
  38. unsigned char buf[USERIO_BUFSIZE];
  39. wait_queue_head_t waitq;
  40. };
  41. /**
  42. * userio_device_write - Write data from serio to a userio device in userspace
  43. * @id: The serio port for the userio device
  44. * @val: The data to write to the device
  45. */
  46. static int userio_device_write(struct serio *id, unsigned char val)
  47. {
  48. struct userio_device *userio = id->port_data;
  49. scoped_guard(spinlock_irqsave, &userio->buf_lock) {
  50. userio->buf[userio->head] = val;
  51. userio->head = (userio->head + 1) % USERIO_BUFSIZE;
  52. if (userio->head == userio->tail)
  53. dev_warn(userio_misc.this_device,
  54. "Buffer overflowed, userio client isn't keeping up");
  55. }
  56. wake_up_interruptible(&userio->waitq);
  57. return 0;
  58. }
  59. static int userio_char_open(struct inode *inode, struct file *file)
  60. {
  61. struct userio_device *userio __free(kfree) =
  62. kzalloc_obj(*userio);
  63. if (!userio)
  64. return -ENOMEM;
  65. mutex_init(&userio->mutex);
  66. spin_lock_init(&userio->buf_lock);
  67. init_waitqueue_head(&userio->waitq);
  68. userio->serio = kzalloc_obj(*userio->serio);
  69. if (!userio->serio)
  70. return -ENOMEM;
  71. userio->serio->write = userio_device_write;
  72. userio->serio->port_data = userio;
  73. file->private_data = no_free_ptr(userio);
  74. return 0;
  75. }
  76. static int userio_char_release(struct inode *inode, struct file *file)
  77. {
  78. struct userio_device *userio = file->private_data;
  79. if (userio->running) {
  80. /*
  81. * Don't free the serio port here, serio_unregister_port()
  82. * does it for us.
  83. */
  84. serio_unregister_port(userio->serio);
  85. } else {
  86. kfree(userio->serio);
  87. }
  88. kfree(userio);
  89. return 0;
  90. }
  91. static size_t userio_fetch_data(struct userio_device *userio, u8 *buf,
  92. size_t count, size_t *copylen)
  93. {
  94. size_t available, len;
  95. guard(spinlock_irqsave)(&userio->buf_lock);
  96. available = CIRC_CNT_TO_END(userio->head, userio->tail,
  97. USERIO_BUFSIZE);
  98. len = min(available, count);
  99. if (len) {
  100. memcpy(buf, &userio->buf[userio->tail], len);
  101. userio->tail = (userio->tail + len) % USERIO_BUFSIZE;
  102. }
  103. *copylen = len;
  104. return available;
  105. }
  106. static ssize_t userio_char_read(struct file *file, char __user *user_buffer,
  107. size_t count, loff_t *ppos)
  108. {
  109. struct userio_device *userio = file->private_data;
  110. int error;
  111. size_t available, copylen;
  112. u8 buf[USERIO_BUFSIZE];
  113. /*
  114. * By the time we get here, the data that was waiting might have
  115. * been taken by another thread. Grab the buffer lock and check if
  116. * there's still any data waiting, otherwise repeat this process
  117. * until we have data (unless the file descriptor is non-blocking
  118. * of course).
  119. */
  120. for (;;) {
  121. available = userio_fetch_data(userio, buf, count, &copylen);
  122. if (available)
  123. break;
  124. /* buffer was/is empty */
  125. if (file->f_flags & O_NONBLOCK)
  126. return -EAGAIN;
  127. /*
  128. * count == 0 is special - no IO is done but we check
  129. * for error conditions (see above).
  130. */
  131. if (count == 0)
  132. return 0;
  133. error = wait_event_interruptible(userio->waitq,
  134. userio->head != userio->tail);
  135. if (error)
  136. return error;
  137. }
  138. if (copylen)
  139. if (copy_to_user(user_buffer, buf, copylen))
  140. return -EFAULT;
  141. return copylen;
  142. }
  143. static int userio_execute_cmd(struct userio_device *userio,
  144. const struct userio_cmd *cmd)
  145. {
  146. switch (cmd->type) {
  147. case USERIO_CMD_REGISTER:
  148. if (!userio->serio->id.type) {
  149. dev_warn(userio_misc.this_device,
  150. "No port type given on /dev/userio\n");
  151. return -EINVAL;
  152. }
  153. if (userio->running) {
  154. dev_warn(userio_misc.this_device,
  155. "Begin command sent, but we're already running\n");
  156. return -EBUSY;
  157. }
  158. userio->running = true;
  159. serio_register_port(userio->serio);
  160. break;
  161. case USERIO_CMD_SET_PORT_TYPE:
  162. if (userio->running) {
  163. dev_warn(userio_misc.this_device,
  164. "Can't change port type on an already running userio instance\n");
  165. return -EBUSY;
  166. }
  167. userio->serio->id.type = cmd->data;
  168. break;
  169. case USERIO_CMD_SEND_INTERRUPT:
  170. if (!userio->running) {
  171. dev_warn(userio_misc.this_device,
  172. "The device must be registered before sending interrupts\n");
  173. return -ENODEV;
  174. }
  175. serio_interrupt(userio->serio, cmd->data, 0);
  176. break;
  177. default:
  178. return -EOPNOTSUPP;
  179. }
  180. return 0;
  181. }
  182. static ssize_t userio_char_write(struct file *file, const char __user *buffer,
  183. size_t count, loff_t *ppos)
  184. {
  185. struct userio_device *userio = file->private_data;
  186. struct userio_cmd cmd;
  187. int error;
  188. if (count != sizeof(cmd)) {
  189. dev_warn(userio_misc.this_device, "Invalid payload size\n");
  190. return -EINVAL;
  191. }
  192. if (copy_from_user(&cmd, buffer, sizeof(cmd)))
  193. return -EFAULT;
  194. scoped_cond_guard(mutex_intr, return -EINTR, &userio->mutex) {
  195. error = userio_execute_cmd(userio, &cmd);
  196. if (error)
  197. return error;
  198. }
  199. return count;
  200. }
  201. static __poll_t userio_char_poll(struct file *file, poll_table *wait)
  202. {
  203. struct userio_device *userio = file->private_data;
  204. poll_wait(file, &userio->waitq, wait);
  205. if (userio->head != userio->tail)
  206. return EPOLLIN | EPOLLRDNORM;
  207. return 0;
  208. }
  209. static const struct file_operations userio_fops = {
  210. .owner = THIS_MODULE,
  211. .open = userio_char_open,
  212. .release = userio_char_release,
  213. .read = userio_char_read,
  214. .write = userio_char_write,
  215. .poll = userio_char_poll,
  216. };
  217. static struct miscdevice userio_misc = {
  218. .fops = &userio_fops,
  219. .minor = USERIO_MINOR,
  220. .name = USERIO_NAME,
  221. };
  222. module_driver(userio_misc, misc_register, misc_deregister);
  223. MODULE_ALIAS_MISCDEV(USERIO_MINOR);
  224. MODULE_ALIAS("devname:" USERIO_NAME);
  225. MODULE_AUTHOR("Stephen Chandler Paul <thatslyude@gmail.com>");
  226. MODULE_DESCRIPTION("Virtual Serio Device Support");
  227. MODULE_LICENSE("GPL");