2 * userio kernel serio device emulation module
3 * Copyright (C) 2015 Red Hat
4 * Copyright (C) 2015 Stephen Chandler Paul <thatslyude@gmail.com>
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.
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.
17 #include <linux/circ_buf.h>
18 #include <linux/mutex.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/serio.h>
23 #include <linux/slab.h>
25 #include <linux/miscdevice.h>
26 #include <linux/sched.h>
27 #include <linux/poll.h>
28 #include <uapi/linux/userio.h>
30 #define USERIO_NAME "userio"
31 #define USERIO_BUFSIZE 16
33 static struct miscdevice userio_misc;
35 struct userio_device {
45 unsigned char buf[USERIO_BUFSIZE];
47 wait_queue_head_t waitq;
51 * userio_device_write - Write data from serio to a userio device in userspace
52 * @id: The serio port for the userio device
53 * @val: The data to write to the device
55 static int userio_device_write(struct serio *id, unsigned char val)
57 struct userio_device *userio = id->port_data;
60 spin_lock_irqsave(&userio->buf_lock, flags);
62 userio->buf[userio->head] = val;
63 userio->head = (userio->head + 1) % USERIO_BUFSIZE;
65 if (userio->head == userio->tail)
66 dev_warn(userio_misc.this_device,
67 "Buffer overflowed, userio client isn't keeping up");
69 spin_unlock_irqrestore(&userio->buf_lock, flags);
71 wake_up_interruptible(&userio->waitq);
76 static int userio_char_open(struct inode *inode, struct file *file)
78 struct userio_device *userio;
80 userio = kzalloc(sizeof(struct userio_device), GFP_KERNEL);
84 mutex_init(&userio->mutex);
85 spin_lock_init(&userio->buf_lock);
86 init_waitqueue_head(&userio->waitq);
88 userio->serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
94 userio->serio->write = userio_device_write;
95 userio->serio->port_data = userio;
97 file->private_data = userio;
102 static int userio_char_release(struct inode *inode, struct file *file)
104 struct userio_device *userio = file->private_data;
106 if (userio->running) {
108 * Don't free the serio port here, serio_unregister_port()
111 serio_unregister_port(userio->serio);
113 kfree(userio->serio);
121 static ssize_t userio_char_read(struct file *file, char __user *user_buffer,
122 size_t count, loff_t *ppos)
124 struct userio_device *userio = file->private_data;
126 size_t nonwrap_len, copylen;
127 unsigned char buf[USERIO_BUFSIZE];
131 * By the time we get here, the data that was waiting might have
132 * been taken by another thread. Grab the buffer lock and check if
133 * there's still any data waiting, otherwise repeat this process
134 * until we have data (unless the file descriptor is non-blocking
138 spin_lock_irqsave(&userio->buf_lock, flags);
140 nonwrap_len = CIRC_CNT_TO_END(userio->head,
143 copylen = min(nonwrap_len, count);
145 memcpy(buf, &userio->buf[userio->tail], copylen);
146 userio->tail = (userio->tail + copylen) %
150 spin_unlock_irqrestore(&userio->buf_lock, flags);
155 /* buffer was/is empty */
156 if (file->f_flags & O_NONBLOCK)
160 * count == 0 is special - no IO is done but we check
161 * for error conditions (see above).
166 error = wait_event_interruptible(userio->waitq,
167 userio->head != userio->tail);
173 if (copy_to_user(user_buffer, buf, copylen))
179 static ssize_t userio_char_write(struct file *file, const char __user *buffer,
180 size_t count, loff_t *ppos)
182 struct userio_device *userio = file->private_data;
183 struct userio_cmd cmd;
186 if (count != sizeof(cmd)) {
187 dev_warn(userio_misc.this_device, "Invalid payload size\n");
191 if (copy_from_user(&cmd, buffer, sizeof(cmd)))
194 error = mutex_lock_interruptible(&userio->mutex);
199 case USERIO_CMD_REGISTER:
200 if (!userio->serio->id.type) {
201 dev_warn(userio_misc.this_device,
202 "No port type given on /dev/userio\n");
208 if (userio->running) {
209 dev_warn(userio_misc.this_device,
210 "Begin command sent, but we're already running\n");
215 userio->running = true;
216 serio_register_port(userio->serio);
219 case USERIO_CMD_SET_PORT_TYPE:
220 if (userio->running) {
221 dev_warn(userio_misc.this_device,
222 "Can't change port type on an already running userio instance\n");
227 userio->serio->id.type = cmd.data;
230 case USERIO_CMD_SEND_INTERRUPT:
231 if (!userio->running) {
232 dev_warn(userio_misc.this_device,
233 "The device must be registered before sending interrupts\n");
238 serio_interrupt(userio->serio, cmd.data, 0);
247 mutex_unlock(&userio->mutex);
248 return error ?: count;
251 static unsigned int userio_char_poll(struct file *file, poll_table *wait)
253 struct userio_device *userio = file->private_data;
255 poll_wait(file, &userio->waitq, wait);
257 if (userio->head != userio->tail)
258 return POLLIN | POLLRDNORM;
263 static const struct file_operations userio_fops = {
264 .owner = THIS_MODULE,
265 .open = userio_char_open,
266 .release = userio_char_release,
267 .read = userio_char_read,
268 .write = userio_char_write,
269 .poll = userio_char_poll,
273 static struct miscdevice userio_misc = {
274 .fops = &userio_fops,
275 .minor = USERIO_MINOR,
278 module_driver(userio_misc, misc_register, misc_deregister);
280 MODULE_ALIAS_MISCDEV(USERIO_MINOR);
281 MODULE_ALIAS("devname:" USERIO_NAME);
283 MODULE_AUTHOR("Stephen Chandler Paul <thatslyude@gmail.com>");
284 MODULE_DESCRIPTION("Virtual Serio Device Support");
285 MODULE_LICENSE("GPL");