userio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. unsigned long flags;
  50. spin_lock_irqsave(&userio->buf_lock, flags);
  51. userio->buf[userio->head] = val;
  52. userio->head = (userio->head + 1) % USERIO_BUFSIZE;
  53. if (userio->head == userio->tail)
  54. dev_warn(userio_misc.this_device,
  55. "Buffer overflowed, userio client isn't keeping up");
  56. spin_unlock_irqrestore(&userio->buf_lock, flags);
  57. wake_up_interruptible(&userio->waitq);
  58. return 0;
  59. }
  60. static int userio_char_open(struct inode *inode, struct file *file)
  61. {
  62. struct userio_device *userio;
  63. userio = kzalloc(sizeof(struct userio_device), GFP_KERNEL);
  64. if (!userio)
  65. return -ENOMEM;
  66. mutex_init(&userio->mutex);
  67. spin_lock_init(&userio->buf_lock);
  68. init_waitqueue_head(&userio->waitq);
  69. userio->serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  70. if (!userio->serio) {
  71. kfree(userio);
  72. return -ENOMEM;
  73. }
  74. userio->serio->write = userio_device_write;
  75. userio->serio->port_data = userio;
  76. file->private_data = userio;
  77. return 0;
  78. }
  79. static int userio_char_release(struct inode *inode, struct file *file)
  80. {
  81. struct userio_device *userio = file->private_data;
  82. if (userio->running) {
  83. /*
  84. * Don't free the serio port here, serio_unregister_port()
  85. * does it for us.
  86. */
  87. serio_unregister_port(userio->serio);
  88. } else {
  89. kfree(userio->serio);
  90. }
  91. kfree(userio);
  92. return 0;
  93. }
  94. static ssize_t userio_char_read(struct file *file, char __user *user_buffer,
  95. size_t count, loff_t *ppos)
  96. {
  97. struct userio_device *userio = file->private_data;
  98. int error;
  99. size_t nonwrap_len, copylen;
  100. unsigned char buf[USERIO_BUFSIZE];
  101. unsigned long flags;
  102. /*
  103. * By the time we get here, the data that was waiting might have
  104. * been taken by another thread. Grab the buffer lock and check if
  105. * there's still any data waiting, otherwise repeat this process
  106. * until we have data (unless the file descriptor is non-blocking
  107. * of course).
  108. */
  109. for (;;) {
  110. spin_lock_irqsave(&userio->buf_lock, flags);
  111. nonwrap_len = CIRC_CNT_TO_END(userio->head,
  112. userio->tail,
  113. USERIO_BUFSIZE);
  114. copylen = min(nonwrap_len, count);
  115. if (copylen) {
  116. memcpy(buf, &userio->buf[userio->tail], copylen);
  117. userio->tail = (userio->tail + copylen) %
  118. USERIO_BUFSIZE;
  119. }
  120. spin_unlock_irqrestore(&userio->buf_lock, flags);
  121. if (nonwrap_len)
  122. break;
  123. /* buffer was/is empty */
  124. if (file->f_flags & O_NONBLOCK)
  125. return -EAGAIN;
  126. /*
  127. * count == 0 is special - no IO is done but we check
  128. * for error conditions (see above).
  129. */
  130. if (count == 0)
  131. return 0;
  132. error = wait_event_interruptible(userio->waitq,
  133. userio->head != userio->tail);
  134. if (error)
  135. return error;
  136. }
  137. if (copylen)
  138. if (copy_to_user(user_buffer, buf, copylen))
  139. return -EFAULT;
  140. return copylen;
  141. }
  142. static ssize_t userio_char_write(struct file *file, const char __user *buffer,
  143. size_t count, loff_t *ppos)
  144. {
  145. struct userio_device *userio = file->private_data;
  146. struct userio_cmd cmd;
  147. int error;
  148. if (count != sizeof(cmd)) {
  149. dev_warn(userio_misc.this_device, "Invalid payload size\n");
  150. return -EINVAL;
  151. }
  152. if (copy_from_user(&cmd, buffer, sizeof(cmd)))
  153. return -EFAULT;
  154. error = mutex_lock_interruptible(&userio->mutex);
  155. if (error)
  156. return error;
  157. switch (cmd.type) {
  158. case USERIO_CMD_REGISTER:
  159. if (!userio->serio->id.type) {
  160. dev_warn(userio_misc.this_device,
  161. "No port type given on /dev/userio\n");
  162. error = -EINVAL;
  163. goto out;
  164. }
  165. if (userio->running) {
  166. dev_warn(userio_misc.this_device,
  167. "Begin command sent, but we're already running\n");
  168. error = -EBUSY;
  169. goto out;
  170. }
  171. userio->running = true;
  172. serio_register_port(userio->serio);
  173. break;
  174. case USERIO_CMD_SET_PORT_TYPE:
  175. if (userio->running) {
  176. dev_warn(userio_misc.this_device,
  177. "Can't change port type on an already running userio instance\n");
  178. error = -EBUSY;
  179. goto out;
  180. }
  181. userio->serio->id.type = cmd.data;
  182. break;
  183. case USERIO_CMD_SEND_INTERRUPT:
  184. if (!userio->running) {
  185. dev_warn(userio_misc.this_device,
  186. "The device must be registered before sending interrupts\n");
  187. error = -ENODEV;
  188. goto out;
  189. }
  190. serio_interrupt(userio->serio, cmd.data, 0);
  191. break;
  192. default:
  193. error = -EOPNOTSUPP;
  194. goto out;
  195. }
  196. out:
  197. mutex_unlock(&userio->mutex);
  198. return error ?: count;
  199. }
  200. static __poll_t userio_char_poll(struct file *file, poll_table *wait)
  201. {
  202. struct userio_device *userio = file->private_data;
  203. poll_wait(file, &userio->waitq, wait);
  204. if (userio->head != userio->tail)
  205. return EPOLLIN | EPOLLRDNORM;
  206. return 0;
  207. }
  208. static const struct file_operations userio_fops = {
  209. .owner = THIS_MODULE,
  210. .open = userio_char_open,
  211. .release = userio_char_release,
  212. .read = userio_char_read,
  213. .write = userio_char_write,
  214. .poll = userio_char_poll,
  215. .llseek = no_llseek,
  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");