userio (originally ps2emu) - virtual serio device module
Subsystems (5)
v3 cover userio (originally ps2emu) - virtual serio device module
Here's the latest revision of the userio patch with all of the suggested edits made, along with a couple of extras I picked up on the way.
Cheers,
Stephen Chandler Paul
Stephen Chandler Paul (1):
Input: Add userio module
Documentation/input/userio.txt | 70 +++++++++++ MAINTAINERS | 6 + drivers/input/serio/Kconfig | 11 ++ drivers/input/serio/Makefile | 1 + drivers/input/serio/userio.c | 261 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/userio.h | 42 +++++++ 6 files changed, 391 insertions(+) create mode 100644 Documentation/input/userio.txt create mode 100644 drivers/input/serio/userio.c create mode 100644 include/uapi/linux/userio.h
Signature · 1 line
2.4.3
RFC v3 1/1 Input: Add userio module
Debugging input devices, specifically laptop touchpads, can be tricky without having the physical device handy. Here we try to remedy that with userio. This module allows an application to connect to a character device provided by the kernel, and emulate any serio device. In combination with userspace programs that can record PS/2 devices and replay them through the /dev/userio device, this allows developers to debug driver issues on the PS/2 level with devices simply by requesting a recording from the user experiencing the issue without having to have the physical hardware in front of them.
--- Documentation/input/userio.txt | 70 +++++++++++ MAINTAINERS | 6 + drivers/input/serio/Kconfig | 11 ++ drivers/input/serio/Makefile | 1 + drivers/input/serio/userio.c | 261 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/userio.h | 42 +++++++ 6 files changed, 391 insertions(+) create mode 100644 Documentation/input/userio.txt create mode 100644 drivers/input/serio/userio.c create mode 100644 include/uapi/linux/userio.h
Documentation/input/userio.txt new file +70 -0
@@ -0,0 +1,70 @@ + The userio Protocol + (c) 2015 Stephen Chandler Paul <[email protected]> + Sponsored by Red Hat +-------------------------------------------------------------------------------- + +1. Introduction +~~~~~~~~~~~~~~~ + This module is intended to try to make the lives of input driver developers +easier by allowing them to test various serio devices (mainly the various +touchpads found on laptops) without having to have the physical device in front +of them. userio accomplishes this by allowing any privileged userspace program +to directly interact with the kernel's serio driver and control a virtual serio +port from there. + +2. Usage overview +~~~~~~~~~~~~~~~~~ + In order to interact with the userio kernel module, one simply opens the +/dev/userio character device in their applications. Commands are sent to the +kernel module by writing to the device, and any data received from the serio +driver is read as-is from the /dev/userio device. All of the structures and +macros you need to interact with the device are defined in <linux/userio.h> and +<linux/serio.h>. + +3. Command Structure +~~~~~~~~~~~~~~~~~~~~ + The struct used for sending commands to /dev/userio is as follows: + + struct userio_cmd { + __u8 type; + __u8 data; + }; + + "type" describes the type of command that is being sent. This can be any one +of the USERIO_CMD macros defined in <linux/userio.h>. "data" is the argument +that goes along with the command. In the event that the command doesn't have an +argument, this field can be left untouched and will be ignored by the kernel. +Each command should be sent by writing the struct directly to the character +device. In the event that the command you send is invalid, an error will be +returned by the character device and a more descriptive error will be printed +to the kernel log. Only one command can be sent at a time, any additional data +written to the character device after the initial command will be ignored. + To close the virtual serio port, just close /dev/userio. + +4. Commands +~~~~~~~~~~~ + +4.1 USERIO_CMD_REGISTER +~~~~~~~~~~~~~~~~~~~~~~~ + Registers the port with the serio driver and begins transmitting data back and +forth. Registration can only be performed once a port type is set with +USERIO_CMD_SET_PORT_TYPE. Has no argument. + +4.2 USERIO_CMD_SET_PORT_TYPE +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + Sets the type of port we're emulating, where "data" is the port type being +set. Can be any of the macros from <linux/serio.h>. For example: SERIO_8042 +would set the port type to be a normal PS/2 port. + +4.3 USERIO_CMD_SEND_INTERRUPT +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + Sends an interrupt through the virtual serio port to the serio driver, where +"data" is the interrupt data being sent. + +5. Userspace tools +~~~~~~~~~~~~~~~~~~ + The userio userspace tools are able to record PS/2 devices using some of the +debugging information from i8042, and play back the devices on /dev/userio. The +latest version of these tools can be found at: + + https://github.com/Lyude/ps2emu
MAINTAINERS +6 -0
@@ -10877,6 +10877,12 @@ S: Maintained F: drivers/media/v4l2-core/videobuf2-* F: include/media/videobuf2-* +VIRTUAL PS/2 DEVICE DRIVER +M: Stephen Chandler Paul <[email protected]> +S: Maintained +F: drivers/input/serio/ps2emu.c +F: include/uapi/linux/ps2emu.h + VIRTIO CONSOLE DRIVER M: Amit Shah <[email protected]> L: [email protected]
drivers/input/serio/Kconfig +11 -0
@@ -292,4 +292,15 @@ config SERIO_SUN4I_PS2 To compile this driver as a module, choose M here: the module will be called sun4i-ps2. +config USERIO + tristate "Virtual serio device support" + help + Say Y here if you want to emulate serio devices using the userio + kernel module. + + To compile this driver as a module, choose M here: the module will be + called userio. + + If you are unsure, say N. + endif
drivers/input/serio/Makefile +1 -0
@@ -30,3 +30,4 @@ obj-$(CONFIG_SERIO_APBPS2) += apbps2.o obj-$(CONFIG_SERIO_OLPC_APSP) += olpc_apsp.o obj-$(CONFIG_HYPERV_KEYBOARD) += hyperv-keyboard.o obj-$(CONFIG_SERIO_SUN4I_PS2) += sun4i-ps2.o +obj-$(CONFIG_USERIO) += userio.o
drivers/input/serio/userio.c new file +261 -0
261 lines not on this page · raw message
include/uapi/linux/userio.h new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
2.4.3
Re: [RFC v3 1/1] Input: Add userio module
Forgot to add the changes I made onto here, whoops!
* Allocate the serio struct separately, we don't have control over when it's freed
* Use locking when working with the head or when working with the tail. Use two seperate locks so that we only risk making the userspace threads wait, not the kernel threads
* Use modulus operations when incrementing the head and the tail
* Check for read()s with length 0, return immediately if we find one
* When doing ps2emu_device_write(), check to make sure that the ps2emu struct hasn't been set for destruction (being set to NULL), if it has just abort the write
* Don't assign ps2emu to NULL, kzalloc will set it to NULL if allocation fails
* Get rid of some ugly whitespace
* Remove an unused include header
* Allow any port types to be used
* Rename the module from ps2emu to userio
* Change documentation in Kconfig to say "using the userio kernel module" as opposed to "using the userio tools.", since it's possible there may be tools other then the ps2emu tools used with userio in the future.
* Mention the requirement for <linux/serio.h>
Quoted 532 lines · Stephen Chandler Paul
Debugging input devices, specifically laptop touchpads, can be tricky without having the physical device handy. Here we try to remedy that with userio. This module allows an application to connect to a character
device provided by the kernel, and emulate any serio device. In combination with userspace programs that can record PS/2 devices and replay them through the/dev/useriodevice, this allows developers to debug driver issues on the PS/2 level with devices simply by requesting
a recording from the user experiencing the issue without having to have
the physical hardware in front of them.Signed-off-by Stephen Chandler Paul <[email protected]>--- Documentation/input/userio.txt | 70 +++++++++++ MAINTAINERS | 6 + drivers/input/serio/Kconfig | 11 ++ drivers/input/serio/Makefile | 1 + drivers/input/serio/userio.c | 261 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/userio.h | 42 +++++++ 6 files changed, 391 insertions(+) create mode 100644 Documentation/input/userio.txt create mode 100644 drivers/input/serio/userio.c create mode 100644 include/uapi/linux/userio.hDocumentation/input/userio.txt new file +8
-1@@ -0,0 +1,70 @@ + The userio Protocol + (c) 2015 Stephen Chandler Paul <[email protected]> + Sponsored by Red Hat +-------------------------------------------------------------------- ------------ + +1. Introduction +~~~~~~~~~~~~~~~ + This module is intended to try to make the lives of input driverdevelopers
+easier by allowing them to test various serio devices (mainly the various
+touchpads found on laptops) without having to have the physical device in front
+of them. userio accomplishes this by allowing any privileged userspace program
+to directly interact with the kernel's serio driver and control a virtual serioDocumentation/input/userio.txt new file +5
-0+port from there. + +2. Usage overview +~~~~~~~~~~~~~~~~~ + In order to interact with the userio kernel module, one simplyopens the
+/dev/userio character device in their applications. Commands are sent to the
+kernel module by writing to the device, and any data received from the serio
+driver is read as-is from the/dev/useriodevice. All of the structures and
+macros you need to interact with the device are defined in <linux/userio.h> and
+<linux/serio.h>.Documentation/input/userio.txt new file +11
-0+ +3. Command Structure +~~~~~~~~~~~~~~~~~~~~ + The struct used for sending commands to /dev/userio is as follows: + + struct userio_cmd { + __u8 type; + __u8 data; + }; + + "type" describes the type of command that is being sent. This canbe any one
+of theUSERIO_CMDmacros defined in <linux/userio.h>. "data" is the argument
+that goes along with the command. In the event that the command doesn't have an
+argument, this field can be left untouched and will be ignored by the kernel.
+Each command should be sent by writing the struct directly to the character
+device. In the event that the command you send is invalid, an error will be
+returned by the character device and a more descriptive error will be printed
+to the kernel log. Only one command can be sent at a time, any additional data
+written to the character device after the initial command will be ignored.Documentation/input/userio.txt new file +8
-0+ To close the virtual serio port, just close /dev/userio. + +4. Commands +~~~~~~~~~~~ + +4.1 USERIO_CMD_REGISTER +~~~~~~~~~~~~~~~~~~~~~~~ + Registers the port with the serio driver and begins transmittingdata back and
+forth. Registration can only be performed once a port type is set withDocumentation/input/userio.txt new file +5
-0+USERIO_CMD_SET_PORT_TYPE. Has no argument. + +4.2 USERIO_CMD_SET_PORT_TYPE +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + Sets the type of port we're emulating, where "data" is the porttype being
+set. Can be any of the macros from <linux/serio.h>. For example:SERIO_8042Documentation/input/userio.txt new file +5
-0+would set the port type to be a normal PS/2 port. + +4.3 USERIO_CMD_SEND_INTERRUPT +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + Sends an interrupt through the virtual serio port to the seriodriver, where
+"data" is the interrupt data being sent.Documentation/input/userio.txt new file +4
-0+ +5. Userspace tools +~~~~~~~~~~~~~~~~~~ + The userio userspace tools are able to record PS/2 devices usingsome of the
+debugging information from i8042, and play back the devices on/dev/userio. TheDocumentation/input/userio.txt new file +3
-0+latest version of these tools can be found at: + + https://github.com/Lyude/ps2emuMAINTAINERS +6
-0@@ -10877,6 +10877,12 @@ S: Maintained F: drivers/media/v4l2-core/videobuf2-* F: include/media/videobuf2-* +VIRTUAL PS/2 DEVICE DRIVER +M: Stephen Chandler Paul <[email protected]> +S: Maintained +F: drivers/input/serio/ps2emu.c +F: include/uapi/linux/ps2emu.h + VIRTIO CONSOLE DRIVER M: Amit Shah <[email protected]> L: [email protected]drivers/input/serio/Kconfig +4
-0@@ -292,4 +292,15 @@ config SERIO_SUN4I_PS2 To compile this driver as a module, choose M here: the module will be called sun4i-ps2. +config USERIO + tristate "Virtual serio device support" + help + Say Y here if you want to emulate serio devices using theuserio
drivers/input/serio/Kconfig +3
-0+ kernel module. + + To compile this driver as a module, choose M here: themodule will be
drivers/input/serio/Kconfig +4
-0+ called userio. + + If you are unsure, say N. + endifdrivers/input/serio/Makefile +1
-0@@ -30,3 +30,4 @@ obj-$(CONFIG_SERIO_APBPS2) += apbps2.o obj-$(CONFIG_SERIO_OLPC_APSP) += olpc_apsp.o obj-$(CONFIG_HYPERV_KEYBOARD) += hyperv-keyboard.o obj-$(CONFIG_SERIO_SUN4I_PS2) += sun4i-ps2.o +obj-$(CONFIG_USERIO) += userio.odrivers/input/serio/userio.c new file +6
-0@@ -0,0 +1,261 @@ +/* +* userio kernel serio device emulation module +* Copyright (C) 2015 Red Hat +* Copyright (C) 2015 Stephen Chandler Paul <[email protected]> +* +* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ */drivers/input/serio/userio.c new file +30
-0+#include <linux/circ_buf.h> +#include <linux/mutex.h> +#include <linux/module.h> +#include <linux/init.h> +#include <linux/kernel.h> +#include <linux/serio.h> +#include <linux/slab.h> +#include <linux/fs.h> +#include <linux/miscdevice.h> +#include <linux/sched.h> +#include <linux/poll.h> +#include <linux/spinlock.h> +#include <uapi/linux/userio.h> + +#define USERIO_NAME "userio" +#define USERIO_BUFSIZE 16 + +static const struct file_operations userio_fops; +static struct miscdevice userio_misc; + +struct userio_device { +struct serio *serio; + +bool running; + +u8 head; +u8 tail; + +/* +* Since we need to copy from userspace when modifying the tail and we
drivers/input/serio/userio.c new file +1
-0+* don't want to lock up the serio driver during these operations, we
drivers/input/serio/userio.c new file +12
-0+* use two different locks +*/ +spinlock_t head_lock; +struct mutex tail_lock; + +unsigned char buf[USERIO_BUFSIZE ]; + +wait_queue_head_t waitq; +}; + +/** +* userio_device_write - Write data from serio to a userio device in userspace
+ * @id: The serio port for the userio device
+ * @val: The data to write to the device
+ */drivers/input/serio/userio.c new file +18
-0+static int userio_device_write (struct serio *id,unsigned char val) +{ +struct userio_device *userio = id->port_data; +unsigned long flags; + +if (!userio) +return -1 ; + +spin_lock_irqsave (&userio->head_lock, flags); + + userio->buf[userio->head] = val; + userio->head = (userio->head +1 ) %USERIO_BUFSIZE ; + +spin_unlock_irqrestore (&userio->head_lock, flags); + +if (userio->head == userio->tail) + dev_warn(userio_misc.this_device, + "Buffer overflowed, userio client isn'tkeeping up");
drivers/input/serio/userio.c new file +12
-0+ +wake_up_interruptible (&userio->waitq); + +return 0 ; +} + +static int userio_char_open (struct inode *inode,struct file *file) +{ +struct userio_device *userio; + + userio = devm_kzalloc(userio_misc.this_device, + sizeof(struct userio_device),
GFP_KERNEL);drivers/input/serio/userio.c new file +22
-0+if (!userio) +return -ENOMEM ; + +spin_lock_init (&userio->head_lock); +mutex_init (&userio->tail_lock); +init_waitqueue_head (&userio->waitq); + + userio->serio =kzalloc (sizeof (struct serio ),GFP_KERNEL ); +if (!userio->serio) { +devm_kfree (userio_misc.this_device, userio); +return -ENOMEM ; + } + + userio->serio->write = userio_device_write; + userio->serio->port_data = userio; + + file->private_data = userio; + +return 0 ; +} + +static int userio_char_release(struct inode *inode, struct file*file)
drivers/input/serio/userio.c new file +18
-0+{ +struct userio_device *userio = file->private_data; + +if (userio->serio) { + userio->serio->port_data =NULL ; + +if (userio->running) +serio_unregister_port (userio->serio); +else +kfree (userio->serio); + } + +devm_kfree (userio_misc.this_device, userio); + +return 0 ; +} + +static ssize_t userio_char_read(struct file *file, char __user*buffer,
drivers/input/serio/userio.c new file +14
-1+ size_t count, loff_t *ppos) +{ + struct userio_device *userio = file->private_data; +int ret; +size_t nonwrap_len, copylen; + +if (!count) +return 0 ; + + if (file->f_flags & O_NONBLOCK && userio->head == userio ->tail) +return -EAGAIN ; + else { + ret = wait_event_interruptible(userio->waitq, + userio->head !=
userio->tail);drivers/input/serio/userio.c new file +12
-0+ +if (ret) +return ret; + } + +mutex_lock (&userio->tail_lock); + + nonwrap_len =CIRC_CNT_TO_END (userio->head, userio->tail, +USERIO_BUFSIZE ); + copylen =min (nonwrap_len, count); + + if (copy_to_user(buffer, &userio->buf[userio->tail],copylen)) {
drivers/input/serio/userio.c new file +12
-0+mutex_unlock (&userio->tail_lock); +return -EFAULT ; + } + + userio->tail = (userio->tail + copylen) %USERIO_BUFSIZE ; + +mutex_unlock (&userio->tail_lock); + +return copylen; +} + +static ssize_t userio_char_write(struct file *file, const char
__user*buffer,drivers/input/serio/userio.c new file +16
-0+ size_t count, loff_t *ppos) +{ + struct userio_device *userio = file->private_data; +struct userio_cmd cmd; + +if (count <sizeof (cmd)) +return -EINVAL ; + +if (copy_from_user (&cmd, buffer,sizeof (cmd))) +return -EFAULT ; + +switch (cmd.type) { +case USERIO_CMD_REGISTER : +if (!userio->serio->id.type) { + dev_warn(userio_misc.this_device, + "No port type given on/dev/userio\n");
drivers/input/serio/userio.c new file +6
-0+ +return -EINVAL ; + } +if (userio->running) { + dev_warn(userio_misc.this_device, + "Begin command sent, but we'realready running\n");
drivers/input/serio/userio.c new file +12
-0+ +return -EINVAL ; + } + + userio->running = true; +serio_register_port (userio->serio); +break ; + +case USERIO_CMD_SET_PORT_TYPE : +if (userio->running) { + dev_warn(userio_misc.this_device, + "Can't change port type on analready running userio instance\n");
drivers/input/serio/userio.c new file +11
-0+ +return -EINVAL ; + } + + userio->serio->id.type = cmd.data; +break ; + +case USERIO_CMD_SEND_INTERRUPT : +if (!userio->running) { + dev_warn(userio_misc.this_device, + "The device must be registeredbefore sending interrupts\n");
drivers/input/serio/userio.c new file +15
-0+ +return -EINVAL ; + } + +serio_interrupt (userio->serio, cmd.data,0 ); +break ; + +default : +return -EINVAL ; + } + +return sizeof (cmd); +} + +static unsigned int userio_char_poll(struct file *file, poll_table*wait)
drivers/input/serio/userio.c new file +32
-0+{ +struct userio_device *userio = file->private_data; + +poll_wait (file, &userio->waitq, wait); + +if (userio->head != userio->tail) +return POLLIN |POLLRDNORM ; + +return 0 ; +} + +static const struct file_operations userio_fops = { + .owner =THIS_MODULE , + .open = userio_char_open, + .release = userio_char_release, + .read = userio_char_read, + .write = userio_char_write, + .poll = userio_char_poll, + .llseek = no_llseek, +}; + +static struct miscdevice userio_misc = { + .fops = &userio_fops, + .minor =MISC_DYNAMIC_MINOR , + .name =USERIO_NAME , +}; + +MODULE_AUTHOR ("Stephen Chandler Paul <[email protected]>" ); +MODULE_DESCRIPTION ("userio" ); +MODULE_LICENSE ("GPL" ); + +module_driver (userio_misc, misc_register, misc_deregister);include/uapi/linux/userio.h new file +4
-0@@ -0,0 +1,42 @@ +/* +* userio.h +* Copyright (C) 2015 Red Hat +* Copyright (C) 2015 Lyude (Stephen Chandler Paul) < [email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ *
+ * This is the public header used for user-space communication with the userio
+ * driver. __attribute__((__packed__)) is used for all structs to keep ABI
+ * compatibility between all architectures.
+ */include/uapi/linux/userio.h new file +13
-0+ +#ifndef _USERIO_H +#define _USERIO_H + +#include <linux/types.h> + +#define USERIO_CMD_REGISTER 0 +#define USERIO_CMD_SET_PORT_TYPE 1 +#define USERIO_CMD_SEND_INTERRUPT 2 + +/* +* userio Commands +* All commands sent to /dev/userio are encoded using this structure. The type
+ * field should contain a USERIO_CMD* value that indicates what kind of command
+ * is being sent to userio. The data field should contain the accompanying
+ * argument for the command, if there is one.
+ */include/uapi/linux/userio.h new file +6
-0+struct userio_cmd { +__u8 type; +__u8 data; +} __attribute__((__packed__ )); + +#endif /* !_USERIO_H */
Signature · 2 lines
Cheers, Stephen Chandler Paul
Re: [RFC v3 1/1] Input: Add userio module
In reply to Stephen Chandler PaulQuoted 10 lines · Stephen Chandler Paul
Debugging input devices, specifically laptop touchpads, can be tricky without having the physical device handy. Here we try to remedy that with userio. This module allows an application to connect to a character device provided by the kernel, and emulate any serio device. In combination with userspace programs that can record PS/2 devices and replay them through the
/dev/useriodevice, this allows developers to debug driver issues on the PS/2 level with devices simply by requesting a recording from the user experiencing the issue without having to have the physical hardware in front of them.
Quoted 2 lines
Signed-off-by Stephen Chandler Paul <[email protected]>---
Thanks Chandler for the new version. I think we still need to enhance the concurrency barriers here. See inline.
Quoted 177 lines
Documentation/input/userio.txt | 70 +++++++++++ MAINTAINERS | 6 + drivers/input/serio/Kconfig | 11 ++ drivers/input/serio/Makefile | 1 + drivers/input/serio/userio.c | 261 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/userio.h | 42 +++++++ 6 files changed, 391 insertions(+) create mode 100644 Documentation/input/userio.txt create mode 100644 drivers/input/serio/userio.c create mode 100644 include/uapi/linux/userio.hDocumentation/input/userio.txt new file +70
-0@@ -0,0 +1,70 @@ + The userio Protocol + (c) 2015 Stephen Chandler Paul <[email protected]> + Sponsored by Red Hat +-------------------------------------------------------------------------------- + +1. Introduction +~~~~~~~~~~~~~~~ + This module is intended to try to make the lives of input driver developers +easier by allowing them to test various serio devices (mainly the various +touchpads found on laptops) without having to have the physical device in front +of them. userio accomplishes this by allowing any privileged userspace program +to directly interact with the kernel's serio driver and control a virtual serio +port from there. + +2. Usage overview +~~~~~~~~~~~~~~~~~ + In order to interact with the userio kernel module, one simply opens the +/dev/userio character device in their applications. Commands are sent to the +kernel module by writing to the device, and any data received from the serio +driver is read as-is from the /dev/userio device. All of the structures and +macros you need to interact with the device are defined in <linux/userio.h> and +<linux/serio.h>. + +3. Command Structure +~~~~~~~~~~~~~~~~~~~~ + The struct used for sending commands to /dev/userio is as follows: + + struct userio_cmd { + __u8 type; + __u8 data; + }; + + "type" describes the type of command that is being sent. This can be any one +of the USERIO_CMD macros defined in <linux/userio.h>. "data" is the argument +that goes along with the command. In the event that the command doesn't have an +argument, this field can be left untouched and will be ignored by the kernel. +Each command should be sent by writing the struct directly to the character +device. In the event that the command you send is invalid, an error will be +returned by the character device and a more descriptive error will be printed +to the kernel log. Only one command can be sent at a time, any additional data +written to the character device after the initial command will be ignored. + To close the virtual serio port, just close /dev/userio. + +4. Commands +~~~~~~~~~~~ + +4.1 USERIO_CMD_REGISTER +~~~~~~~~~~~~~~~~~~~~~~~ + Registers the port with the serio driver and begins transmitting data back and +forth. Registration can only be performed once a port type is set with +USERIO_CMD_SET_PORT_TYPE. Has no argument. + +4.2 USERIO_CMD_SET_PORT_TYPE +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + Sets the type of port we're emulating, where "data" is the port type being +set. Can be any of the macros from <linux/serio.h>. For example: SERIO_8042 +would set the port type to be a normal PS/2 port. + +4.3 USERIO_CMD_SEND_INTERRUPT +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + Sends an interrupt through the virtual serio port to the serio driver, where +"data" is the interrupt data being sent. + +5. Userspace tools +~~~~~~~~~~~~~~~~~~ + The userio userspace tools are able to record PS/2 devices using some of the +debugging information from i8042, and play back the devices on /dev/userio. The +latest version of these tools can be found at: + + https://github.com/Lyude/ps2emuMAINTAINERS +6
-0@@ -10877,6 +10877,12 @@ S: Maintained F: drivers/media/v4l2-core/videobuf2-* F: include/media/videobuf2-* +VIRTUAL PS/2 DEVICE DRIVER +M: Stephen Chandler Paul <[email protected]> +S: Maintained +F: drivers/input/serio/ps2emu.c +F: include/uapi/linux/ps2emu.h + VIRTIO CONSOLE DRIVER M: Amit Shah <[email protected]> L: virtualization-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.orgdrivers/input/serio/Kconfig +11
-0@@ -292,4 +292,15 @@ config SERIO_SUN4I_PS2 To compile this driver as a module, choose M here: the module will be called sun4i-ps2. +config USERIO + tristate "Virtual serio device support" + help + Say Y here if you want to emulate serio devices using the userio + kernel module. + + To compile this driver as a module, choose M here: the module will be + called userio. + + If you are unsure, say N. + endifdrivers/input/serio/Makefile +1
-0@@ -30,3 +30,4 @@ obj-$(CONFIG_SERIO_APBPS2) += apbps2.o obj-$(CONFIG_SERIO_OLPC_APSP) += olpc_apsp.o obj-$(CONFIG_HYPERV_KEYBOARD) += hyperv-keyboard.o obj-$(CONFIG_SERIO_SUN4I_PS2) += sun4i-ps2.o +obj-$(CONFIG_USERIO) += userio.odrivers/input/serio/userio.c new file +38
-0@@ -0,0 +1,261 @@ +/* +* userio kernel serio device emulation module +* Copyright (C) 2015 Red Hat +* Copyright (C) 2015 Stephen Chandler Paul <[email protected]> +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU Lesser General Public License as published by the +* Free Software Foundation; either version 2 of the License, or (at your +* option) any later version. +* +* This program is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +* details. +*/ +#include <linux/circ_buf.h> +#include <linux/mutex.h> +#include <linux/module.h> +#include <linux/init.h> +#include <linux/kernel.h> +#include <linux/serio.h> +#include <linux/slab.h> +#include <linux/fs.h> +#include <linux/miscdevice.h> +#include <linux/sched.h> +#include <linux/poll.h> +#include <linux/spinlock.h> +#include <uapi/linux/userio.h> + +#define USERIO_NAME "userio" +#define USERIO_BUFSIZE 16 + +static const struct file_operations userio_fops; +static struct miscdevice userio_misc; + +struct userio_device { +struct serio *serio; +
drivers/input/serio/userio.c line 48
Quoted 10 lines
drivers/input/serio/userio.c new file +10
-0+bool running; + +u8 head; +u8 tail; + +/* +* Since we need to copy from userspace when modifying the tail and we +* don't want to lock up the serio driver during these operations, we +* use two different locks +*/
My guts tell me that this is racy. You generally want to have a single mutex/spinlock to protect your buffer. Having 2 might introduce interesting complex problems...
Quoted 13 lines
drivers/input/serio/userio.c new file +13
-0+spinlock_t head_lock; +struct mutex tail_lock; + +unsigned char buf[USERIO_BUFSIZE ]; + +wait_queue_head_t waitq; +}; + +/** +* userio_device_write - Write data from serio to a userio device in userspace +* @id: The serio port for the userio device +* @val: The data to write to the device +*/
drivers/input/serio/userio.c line 71
Quoted 10 lines
drivers/input/serio/userio.c new file +10
-0+static int userio_device_write (struct serio *id,unsigned char val) +{ +struct userio_device *userio = id->port_data; +unsigned long flags; + +if (!userio) +return -1 ; + +spin_lock_irqsave (&userio->head_lock, flags); +
I know Dmitry said that this could be called by several threads, but I am not 100% sure.
This function is called by the serio driver, and I don't think we will have more than one thread at a time in the serio driver (this would raise interesting hardware behavior for a serial protocol).
Though protecting the head and the value here could be a good thing if you protect also when you read them.
Quoted 45 lines
drivers/input/serio/userio.c new file +45
-0+ userio->buf[userio->head] = val; + userio->head = (userio->head +1 ) %USERIO_BUFSIZE ; + +spin_unlock_irqrestore (&userio->head_lock, flags); + +if (userio->head == userio->tail) +dev_warn (userio_misc.this_device, +"Buffer overflowed, userio client isn't keeping up" ); + +wake_up_interruptible (&userio->waitq); + +return 0 ; +} + +static int userio_char_open (struct inode *inode,struct file *file) +{ +struct userio_device *userio; + + userio =devm_kzalloc (userio_misc.this_device, +sizeof (struct userio_device ),GFP_KERNEL ); +if (!userio) +return -ENOMEM ; + +spin_lock_init (&userio->head_lock); +mutex_init (&userio->tail_lock); +init_waitqueue_head (&userio->waitq); + + userio->serio =kzalloc (sizeof (struct serio ),GFP_KERNEL ); +if (!userio->serio) { +devm_kfree (userio_misc.this_device, userio); +return -ENOMEM ; + } + + userio->serio->write = userio_device_write; + userio->serio->port_data = userio; + + file->private_data = userio; + +return 0 ; +} + +static int userio_char_release (struct inode *inode,struct file *file) +{ +struct userio_device *userio = file->private_data; +
drivers/input/serio/userio.c line 126
Quoted 10 lines
drivers/input/serio/userio.c new file +10
-0+if (userio->serio) { + userio->serio->port_data =NULL ; + +if (userio->running) +serio_unregister_port (userio->serio); +else +kfree (userio->serio); + } + +devm_kfree (userio_misc.this_device, userio);
Calling devm_kfree directly should always raise a big red warning. The point of having managed memory is to avoid calling kfree. If you have to, then you probably should not call devm_kzalloc() in the first place.
Quoted 5 lines
drivers/input/serio/userio.c new file +5
-0+ +return 0 ; +} + +static ssize_t userio_char_read(struct file *file, char __user *buffer,
drivers/input/serio/userio.c line 141
Quoted 10 lines
drivers/input/serio/userio.c new file +10
-0+ size_t count, loff_t *ppos) +{ + struct userio_device *userio = file->private_data; +int ret; +size_t nonwrap_len, copylen; + +if (!count) +return 0 ; + +if (file->f_flags &O_NONBLOCK && userio->head == userio->tail)
This section is not protected while you are accessing userio->tail. I think it is still safe here, but I am not 100% sure.
drivers/input/serio/userio.c line 145
Quoted 4 lines
drivers/input/serio/userio.c new file +4
-0+return -EAGAIN ; + else { + ret =wait_event_interruptible (userio->waitq, + userio->head != userio->tail);
I would protect this under the mutex, but OTOH, it might lead also to some interesting lockups.
Quoted 25 lines
drivers/input/serio/userio.c new file +25
-0+ +if (ret) +return ret; + } + +mutex_lock (&userio->tail_lock); + + nonwrap_len =CIRC_CNT_TO_END (userio->head, userio->tail, +USERIO_BUFSIZE ); + copylen =min (nonwrap_len, count); + +if (copy_to_user (buffer, &userio->buf[userio->tail], copylen)) { +mutex_unlock (&userio->tail_lock); +return -EFAULT ; + } + + userio->tail = (userio->tail + copylen) %USERIO_BUFSIZE ; + +mutex_unlock (&userio->tail_lock); + +return copylen; +} + +static ssize_t userio_char_write (struct file *file,const char __user *buffer, +size_t count,loff_t *ppos)
drivers/input/serio/userio.c line 180
Quoted 10 lines
drivers/input/serio/userio.c new file +10
-0+{ +struct userio_device *userio = file->private_data; +struct userio_cmd cmd; + +if (count <sizeof (cmd)) +return -EINVAL ; + +if (copy_from_user (&cmd, buffer,sizeof (cmd))) +return -EFAULT ; +
Starting from here
Quoted 34 lines
drivers/input/serio/userio.c new file +34
-0+switch (cmd.type) { +case USERIO_CMD_REGISTER : +if (!userio->serio->id.type) { +dev_warn (userio_misc.this_device, +"No port type given on /dev/userio\n" ); + +return -EINVAL ; + } +if (userio->running) { +dev_warn (userio_misc.this_device, +"Begin command sent, but we're already running\n" ); + +return -EINVAL ; + } + + userio->running = true; +serio_register_port (userio->serio); +break ; + +case USERIO_CMD_SET_PORT_TYPE : +if (userio->running) { +dev_warn (userio_misc.this_device, +"Can't change port type on an already running userio instance\n" ); + +return -EINVAL ; + } + + userio->serio->id.type = cmd.data; +break ; + +case USERIO_CMD_SEND_INTERRUPT : +if (!userio->running) { +dev_warn (userio_misc.this_device, +"The device must be registered before sending interrupts\n" );
drivers/input/serio/userio.c line 224
Quoted 10 lines
drivers/input/serio/userio.c new file +10
-0+ +return -EINVAL ; + } + +serio_interrupt (userio->serio, cmd.data,0 ); +break ; + +default : +return -EINVAL ; + }
... to here, you should protect this against several threads. You don't want to register twice the serio port because 2 threads read userio->running as false when issuing USERIO_CMD_REGISTER. Likewise, there could be races between USERIO_CMD_SEND_INTERRUPT and USERIO_CMD_REGISTER.
I don't think this user-space interface (both read and write) is time critical, so I would simply put a lock before each interaction with the internal state and release it when done.
Quoted 88 lines
drivers/input/serio/userio.c new file +37
-0+ +return sizeof (cmd); +} + +static unsigned int userio_char_poll (struct file *file,poll_table *wait) +{ +struct userio_device *userio = file->private_data; + +poll_wait (file, &userio->waitq, wait); + +if (userio->head != userio->tail) +return POLLIN |POLLRDNORM ; + +return 0 ; +} + +static const struct file_operations userio_fops = { + .owner =THIS_MODULE , + .open = userio_char_open, + .release = userio_char_release, + .read = userio_char_read, + .write = userio_char_write, + .poll = userio_char_poll, + .llseek = no_llseek, +}; + +static struct miscdevice userio_misc = { + .fops = &userio_fops, + .minor =MISC_DYNAMIC_MINOR , + .name =USERIO_NAME , +}; + +MODULE_AUTHOR ("Stephen Chandler Paul <[email protected]>" ); +MODULE_DESCRIPTION ("userio" ); +MODULE_LICENSE ("GPL" ); + +module_driver (userio_misc, misc_register, misc_deregister);include/uapi/linux/userio.h new file +42
-1@@ -0,0 +1,42 @@ +/* +* userio.h +* Copyright (C) 2015 Red Hat +* Copyright (C) 2015 Lyude (Stephen Chandler Paul) <[email protected]> +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU Lesser General Public License as published by the +* Free Software Foundation; either version 2 of the License, or (at your +* option) any later version. +* +* This program is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +* details. +* +* This is the public header used for user-space communication with the userio +* driver. __attribute__((__packed__)) is used for all structs to keep ABI +* compatibility between all architectures. +*/ + +#ifndef _USERIO_H +#define _USERIO_H + +#include <linux/types.h> + +#define USERIO_CMD_REGISTER 0 +#define USERIO_CMD_SET_PORT_TYPE 1 +#define USERIO_CMD_SEND_INTERRUPT 2 + +/* +* userio Commands +* All commands sent to /dev/userio are encoded using this structure. The type +* field should contain a USERIO_CMD* value that indicates what kind of command +* is being sent to userio. The data field should contain the accompanying +* argument for the command, if there is one. +*/ +struct userio_cmd { +__u8 type; +__u8 data; +} __attribute__((__packed__ )); + +#endif /* !_USERIO_H */ --2.4.3
Quoted 4 lines
--
To unsubscribe from this list: send the line "unsubscribe linux-input" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Cheers,
Benjamin
Dmitry Torokhov
maintainerRe: [RFC v3 1/1] Input: Add userio module
In reply to Stephen Chandler PaulHi Stephen,
drivers/input/serio/userio.c line 93
Quoted 8 lines · Stephen Chandler Paul
drivers/input/serio/userio.c new file +8
-0+static int userio_char_open (struct inode *inode,struct file *file) +{ +struct userio_device *userio; + + userio =devm_kzalloc (userio_misc.this_device, +sizeof (struct userio_device ),GFP_KERNEL ); +if (!userio) +return -ENOMEM ;
Never ever use devm_ API outside of device probe() calls because devm-managed resources only freed after failed probe() or after remove() callback on driver model device and not any other device (block, char, etc) in the system.
drivers/input/serio/userio.c line 101
Quoted 8 lines
drivers/input/serio/userio.c new file +8
-0+ +spin_lock_init (&userio->head_lock); +mutex_init (&userio->tail_lock); +init_waitqueue_head (&userio->waitq); + + userio->serio =kzalloc (sizeof (struct serio ),GFP_KERNEL ); +if (!userio->serio) { +devm_kfree (userio_misc.this_device, userio);
And indeed here you have ot manually release the acquired resource. So the only benefit you got form devm* is wasted resousrces.
Quoted 7 lines
drivers/input/serio/userio.c new file +7
-0+return -ENOMEM ; + } + + userio->serio->write = userio_device_write; + userio->serio->port_data = userio; + + file->private_data = userio;
drivers/input/serio/userio.c line 118
Quoted 10 lines
drivers/input/serio/userio.c new file +10
-0+ +return 0 ; +} + +static int userio_char_release (struct inode *inode,struct file *file) +{ +struct userio_device *userio = file->private_data; + +if (userio->serio) { + userio->serio->port_data =NULL ;
No need to reset, it is going away.
Quoted 15 lines
drivers/input/serio/userio.c new file +15
-0+ +if (userio->running) +serio_unregister_port (userio->serio); +else +kfree (userio->serio); + } + +devm_kfree (userio_misc.this_device, userio); + +return 0 ; +} + +static ssize_t userio_char_read (struct file *file,char __user *buffer, +size_t count,loff_t *ppos) +{
drivers/input/serio/userio.c line 143
Quoted 10 lines
drivers/input/serio/userio.c new file +10
-0+struct userio_device *userio = file->private_data; +int ret; +size_t nonwrap_len, copylen; + +if (!count) +return 0 ; + +if (file->f_flags &O_NONBLOCK && userio->head == userio->tail) +return -EAGAIN ; +else {
This is racy. If there was data and other thread "stole" it here then your O_NONBLOCK will block. See evdev_read() how you supposed to handle this.
Thanks.
Signature · 1 line
Dmitry