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