tty/serial: Add a serial port simulator
Subsystems (2)
PATCH 1/1 tty/serial: Add a serial port simulator
From: Corey Minyard <[email protected]>
This creates simulated serial ports, both as echo devices and pipe devices. The driver reasonably approximates the serial port speed and simulates some modem control lines. It allows error injection and dirct control of modem control lines.
Signed-off-by Corey Minyard <[email protected]>
---
I wrote this for doing automated tests on ser2net and the gensio library, but it's general purpose and other people wanting to test serial port programs might find it handy. It has been invaluable for me.
Documentation/serial/serialsim.rst | 149 ++++ MAINTAINERS | 7 + drivers/tty/serial/Kconfig | 10 + drivers/tty/serial/Makefile | 1 + drivers/tty/serial/serialsim.c | 1086 ++++++++++++++++++++++++++++ include/uapi/linux/serialsim.h | 28 + 6 files changed, 1281 insertions(+) create mode 100644 Documentation/serial/serialsim.rst create mode 100644 drivers/tty/serial/serialsim.c create mode 100644 include/uapi/linux/serialsim.h
Documentation/serial/serialsim.rst new file +149 -0
@@ -0,0 +1,149 @@ +.. SPDX-License-Identifier: GPL-2.0+ +===================================== +serialsim - A kernel serial simualtor +===================================== + +:Author: Corey Minyard <[email protected]> / <[email protected]> + +The serialsim device is a serial simulator with echo and pipe devices. +It is quite useful for testing programs that use serial ports. + +This attempts to emulate a basic serial device. It uses the baud rate +and sends the bytes through the loopback or pipe at approximately the +speed it would on a normal serial device. + +There is a python interface to the special ioctls for controlling the +remote end of the termios in addition to the standard ioctl interface +documented below. See https://github.com/cminyard/serialsim + +===== +Using +===== + +The serialsim.ko module creates two types of devices. Echo devices +simply echo back the data to the same device. These devices will +appear as /dev/ttyEcho<n>. + +Pipe devices will transfer the data between two devices. The +devices will appear as /dev/ttyPipeA<n> and /dev/ttyPipeB<n>. And +data written to PipeA reads from PipeB, and vice-versa. + +You may create an arbitrary number of devices by setting the +nr_echo ports and nr_pipe_ports module parameters. The default is +four for both. + +This driver supports modifying the modem control lines and +injecting various serial errors. It also supports a simulated null +modem between the two pipes, or in a loopback on the echo device. + +By default a pipe or echo comes up in null modem configuration, +meaning that the DTR line is hooked to the DSR and CD lines on the +other side and the RTS line on one side is hooked to the CTS line +on the other side. + +The RTS and CTS lines don't currently do anything for flow control. + +You can modify null modem and control the lines individually +through an interface in /sys/class/tty/ttyECHO<n>/ctrl, +/sys/class/tty/ttyPipeA<n>/ctrl, and +/sys/class/tty/ttyPipeB<n>/ctrl. The following may be written to +those files: + +[+-]nullmodem + enable/disable null modem + +[+-]cd + enable/disable Carrier Detect (no effect if +nullmodem) + +[+-]dsr + enable/disable Data Set Ready (no effect if +nullmodem) + +[+-]cts + enable/disable Clear To Send (no effect if +nullmodem) + +[+-]ring + enable/disable Ring + +frame + inject a frame error on the next byte + +parity + inject a parity error on the next byte + +overrun + inject an overrun error on the next byte + +The contents of the above files has the following format: + +tty[Echo|PipeA|PipeB]<n> + <mctrl values> + +where <mctrl values> is the modem control values above (not frame, +parity, or overrun) with the following added: + +[+-]dtr + value of the Data Terminal Ready + +[+-]rts + value of the Request To Send + +The above values are not settable through this interface, they are +set through the serial port interface itself. + +So, for instance, ttyEcho0 comes up in the following state:: + + # cat /sys/class/tty/ttyEcho0/ctrl + ttyEcho0: +nullmodem -cd -dsr -cts -ring -dtr -rts + +If something connects, it will become:: + + ttyEcho0: +nullmodem +cd +dsr +cts -ring +dtr +rts + +To enable ring:: + + # echo "+ring" >/sys/class/tty/ttyEcho0/ctrl + # cat /sys/class/tty/ttyEcho0/ctrl + ttyEcho0: +nullmodem +cd +dsr +cts +ring +dtr +rts + +Now disable NULL modem and the CD line:: + + # echo "-nullmodem -cd" >/sys/class/tty/ttyEcho0/ctrl + # cat /sys/class/tty/ttyEcho0/ctrl + ttyEcho0: -nullmodem -cd -dsr -cts +ring -dtr -rts + +Note that these settings are for the side you are modifying. So if +you set nullmodem on ttyPipeA0, that controls whether the DTR/RTS +lines from ttyPipeB0 affect ttyPipeA0. It doesn't affect ttyPipeB's +modem control lines. + +The PIPEA and PIPEB devices also have the ability to set these +values for the other end via an ioctl. The following ioctls are +available: + +TIOCSERSNULLMODEM + Set the null modem value, the arg is a boolean. + +TIOCSERSREMMCTRL + Set the modem control lines, bits 16-31 of the arg is + a 16-bit mask telling which values to set, bits 0-15 are the + actual values. Settable values are TIOCM_CAR, TIOCM_CTS, + TIOCM_DSR, and TIOC_RNG. If NULLMODEM is set to true, then only + TIOC_RNG is settable. The DTR and RTS lines are not here, you can + set them through the normal interface. + +TIOCSERSREMERR + Send an error or errors on the next sent byte. arg is + a bitwise OR of (1 << TTY_xxx). Allowed errors are TTY_BREAK, + TTY_FRAME, TTY_PARITY, and TTY_OVERRUN. + +TIOCSERGREMTERMIOS + Return the termios structure for the other side of the pipe. + arg is a pointer to a standard termios struct. + +TIOCSERGREMRS485 + Return the remote RS485 settings, arg is a pointer to a struct + serial_rs485. + +Note that unlike the sysfs interface, these ioctls affect the other +end. So setting nullmodem on the ttyPipeB0 interface sets whether +the DTR/RTS lines on ttyPipeB0 affect ttyPipeA0.
MAINTAINERS +7 -0
@@ -13702,6 +13702,13 @@ L: [email protected] S: Maintained F: drivers/media/rc/serial_ir.c +SERIAL PORT SIMULATOR +M: Corey Minyard <[email protected]> +S: Maintained +F: Documentation/serial/serialsim.rst +F: drivers/tty/serial/serialsim.c +F: include/uapi/linux/serialsim.h + SFC NETWORK DRIVER M: Solarflare linux maintainers <[email protected]> M: Edward Cree <[email protected]>
drivers/tty/serial/Kconfig +10 -0
@@ -1565,4 +1565,14 @@ endmenu config SERIAL_MCTRL_GPIO tristate +config SERIAL_SIMULATOR + tristate "Serial port simulator" + default n + help + Support for a simulated device that behaves like a normal + serial port. It has echo devices and pipe devices. This + is useful for testing programs that use serial ports. + + If unsure, say N. + endif # TTY
drivers/tty/serial/Makefile +1 -0
@@ -91,6 +91,7 @@ obj-$(CONFIG_SERIAL_PIC32) += pic32_uart.o obj-$(CONFIG_SERIAL_MPS2_UART) += mps2-uart.o obj-$(CONFIG_SERIAL_OWL) += owl-uart.o obj-$(CONFIG_SERIAL_RDA) += rda-uart.o +obj-$(CONFIG_SERIAL_SIMULATOR) += serialsim.o # GPIOLIB helpers for modem control lines obj-$(CONFIG_SERIAL_MCTRL_GPIO) += serial_mctrl_gpio.o
drivers/tty/serial/serialsim.c new file +1086 -0
1,086 lines not on this page · raw message
include/uapi/linux/serialsim.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */ + +/* +* serialsim - Emulate a serial device in a loopback and/or pipe +*/ + +/* +* TTY IOCTLs for controlling the modem control and for error injection. +* See drivers/tty/serial/serialsim.c and Documentation/serial/serialsim.rst +* for details. +*/ + +#ifndef LINUX_SERIALSIM_H +#define LINUX_SERIALSIM_H + +#include <linux/ioctl.h> +#include <asm/termbits.h> + +#define TIOCSERSREMNULLMODEM 0x54e4 +#define TIOCSERSREMMCTRL 0x54e5 +#define TIOCSERSREMERR 0x54e6 +#define TIOCSERGREMTERMIOS _IOR('T', 0xe7, struct termios2) +#define TIOCSERGREMNULLMODEM _IOR('T', 0xe8, int) +#define TIOCSERGREMMCTRL _IOR('T', 0xe9, unsigned int) +#define TIOCSERGREMERR _IOR('T', 0xea, unsigned int) +#define TIOCSERGREMRS485 _IOR('T', 0xeb, struct serial_rs485) + +#endif
Signature · 1 line
2.17.1