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