From a38c3c0384cbcec0e3bc2592916a4a03c0bb9bb0 Mon Sep 17 00:00:00 2001 From: PulkoMandy Date: Thu, 26 Oct 2023 11:18:28 +0200 Subject: [PATCH] serial/tty: fix handling of custom baudrates Fixes the code I introduced in hrev50114 for custom serial port baudrates. The idea there was based on FreeBSD implementation, but I missed a key detail: speed_t in BeOS (and Haiku) is only an 8 bit value. Note that BeOS does not have c_ispeed and c_ospeed fields, instead they are named c_ixxxxx and c_oxxxxx with a comment in termios.h saying that they are not used. So the renaming and moving of these fields isn't a problem. This means the previous code worked only for speed between 20 and 255 baud, quite the opposite of what I wanted to do, which is to enable access to fast baudrates. This new implementation exploits the fact that tcflag_t is 32 bit, but we never actually use more than 16 bits. Therefore, the high bits of each value were unused, and can be reclaimed to store the speed, by changing tcflag_t to 16 bits. The speed is then inserted as two 16 bit values that can be combined as a 32 bit one. The flag bits are not moved (on little endian systems), and the extra values are guaranteed to be set to 0 by any previous code that was compiled with 32 bit tcflag_t. Support for different speeds for input and output is now also possible (POSIX specifies separate functions for setting the input and output speeds, which is useful for some old terminals and modems, where it was useful to have a high baudrate for data to display on the screen, but things typed on the keyboard aren't quite as fast). If desired, we could now properly implement this in our serial drivers, but it isn't done here yet. Additional changes: - speed_t is now a 32bit type, allowing to pass large values to cfset(i,o)speed - fix some places where a baudrate enum value was incorrectly put in the c_ispeed and c_ospeed fields, this is not how they were meant to be used (it meant the default was to use a speed of 0, that means "hangup" the line, which I think no serial driver really implemented). - do not put baudrate enumeration values in c_iflag and c_oflag, they are meant to be used in c_cflag only, and conflict with other bits. Separate speeds for input and output can be done by setting the c_cflag value to CBAUD (indicating custom baudrates) and then setting the values in c_ispeed and c_ospeed. Fixes #18483 Change-Id: If63a24b5ced5edf6d051d921197db194def0c614 Reviewed-on: https://review.haiku-os.org/c/haiku/+/7068 Reviewed-by: waddlesplash Tested-by: Commit checker robot Reviewed-by: Adrien Destugues --- headers/posix/termios.h | 24 +++++++++++-------- .../drivers/ports/usb_serial/SerialDevice.cpp | 9 +++---- .../drivers/ports/usb_serial/Tracing.cpp | 17 ++++++------- src/add-ons/kernel/generic/tty/module.cpp | 20 +++++++++------- src/add-ons/kernel/generic/tty/tty.cpp | 6 +++-- src/libs/bsd/termios.c | 8 ++++--- src/system/libroot/posix/termios.c | 10 ++++---- 7 files changed, 54 insertions(+), 40 deletions(-) diff --git a/headers/posix/termios.h b/headers/posix/termios.h index 2f843111aa..3d590e67ed 100644 --- a/headers/posix/termios.h +++ b/headers/posix/termios.h @@ -9,21 +9,25 @@ #include -typedef __haiku_uint32 tcflag_t; -typedef unsigned char speed_t; +typedef __haiku_uint16 tcflag_t; +typedef __haiku_uint32 speed_t; typedef unsigned char cc_t; #define NCCS 11 /* number of control characters */ struct termios { - tcflag_t c_iflag; /* input modes */ - tcflag_t c_oflag; /* output modes */ - tcflag_t c_cflag; /* control modes */ - tcflag_t c_lflag; /* local modes */ - char c_line; /* line discipline */ - speed_t c_ispeed; /* custom input baudrate */ - speed_t c_ospeed; /* custom output baudrate */ - cc_t c_cc[NCCS]; /* control characters */ + tcflag_t c_iflag; /* input modes */ + tcflag_t c_ispeed; /* input baudrate */ + tcflag_t c_oflag; /* output modes */ + tcflag_t c_ospeed; /* output baudrate */ + tcflag_t c_cflag; /* control modes */ + tcflag_t c_ispeed_high; /* high word of input baudrate */ + tcflag_t c_lflag; /* local modes */ + tcflag_t c_ospeed_high; /* high word of output baudrate */ + char c_line; /* line discipline */ + unsigned char _padding; /* unused */ + unsigned char _padding2; /* unused */ + cc_t c_cc[NCCS]; /* control characters */ }; /* control characters */ diff --git a/src/add-ons/kernel/drivers/ports/usb_serial/SerialDevice.cpp b/src/add-ons/kernel/drivers/ports/usb_serial/SerialDevice.cpp index 8056983060..6000e0c99d 100644 --- a/src/add-ons/kernel/drivers/ports/usb_serial/SerialDevice.cpp +++ b/src/add-ons/kernel/drivers/ports/usb_serial/SerialDevice.cpp @@ -160,10 +160,11 @@ SerialDevice::SetModes(struct termios *tios) TRACE_FUNCRES(trace_termios, tios); uint8 baud = tios->c_cflag & CBAUD; - int32 speed = baud_index_to_speed(baud); - if (speed < 0) { - baud = CBAUD; - speed = tios->c_ospeed; + int32 speed; + if (baud == CBAUD) { + speed = tios->c_ospeed + (tios->c_ospeed_high << 16); + } else { + speed = baud_index_to_speed(baud); } // update our master config in full diff --git a/src/add-ons/kernel/drivers/ports/usb_serial/Tracing.cpp b/src/add-ons/kernel/drivers/ports/usb_serial/Tracing.cpp index be3b3684fb..dfee837ae9 100644 --- a/src/add-ons/kernel/drivers/ports/usb_serial/Tracing.cpp +++ b/src/add-ons/kernel/drivers/ports/usb_serial/Tracing.cpp @@ -106,17 +106,18 @@ void trace_termios(struct termios *tios) { TRACE("struct termios:\n" - "\tc_iflag: 0x%08x\n" - "\tc_oflag: 0x%08x\n" - "\tc_cflag: 0x%08x\n" - "\tc_lflag: 0x%08x\n" - "\tc_line: 0x%08x\n" - "\tc_ispeed: 0x%08x\n" - "\tc_ospeed: 0x%08x\n" + "\tc_iflag: 0x%04x\n" + "\tc_oflag: 0x%04x\n" + "\tc_cflag: 0x%04x\n" + "\tc_lflag: 0x%04x\n" + "\tc_line: 0x%04x\n" + "\tc_ispeed: %u\n" + "\tc_ospeed: %u\n" "\tc_cc[0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x]\n", tios->c_iflag, tios->c_oflag, tios->c_cflag, tios->c_lflag, tios->c_line, - tios->c_ispeed, tios->c_ospeed, + tios->c_ispeed + ((uint32_t)tios->c_ispeed_high << 16), + tios->c_ospeed + (tios->c_ospeed_high << 16), tios->c_cc[0], tios->c_cc[1], tios->c_cc[2], tios->c_cc[3], tios->c_cc[4], tios->c_cc[5], tios->c_cc[6], tios->c_cc[7], tios->c_cc[8], tios->c_cc[9], tios->c_cc[10]); diff --git a/src/add-ons/kernel/generic/tty/module.cpp b/src/add-ons/kernel/generic/tty/module.cpp index d9f501251c..830400e86e 100644 --- a/src/add-ons/kernel/generic/tty/module.cpp +++ b/src/add-ons/kernel/generic/tty/module.cpp @@ -28,17 +28,19 @@ dump_tty_settings(struct tty_settings& settings) kprintf(" session_id: %" B_PRId32 "\n", settings.session_id); kprintf(" termios:\n"); - kprintf(" c_iflag: 0x%08" B_PRIx32 "\n", settings.termios.c_iflag); - kprintf(" c_oflag: 0x%08" B_PRIx32 "\n", settings.termios.c_oflag); - kprintf(" c_cflag: 0x%08" B_PRIx32 "\n", settings.termios.c_cflag); - kprintf(" c_lflag: 0x%08" B_PRIx32 "\n", settings.termios.c_lflag); - kprintf(" c_line: %d\n", settings.termios.c_line); - kprintf(" c_ispeed: %u\n", settings.termios.c_ispeed); - kprintf(" c_ospeed: %u\n", settings.termios.c_ospeed); + kprintf(" c_iflag: 0x%08" B_PRIx32 "\n", settings.termios.c_iflag); + kprintf(" c_oflag: 0x%08" B_PRIx32 "\n", settings.termios.c_oflag); + kprintf(" c_cflag: 0x%08" B_PRIx32 "\n", settings.termios.c_cflag); + kprintf(" c_lflag: 0x%08" B_PRIx32 "\n", settings.termios.c_lflag); + kprintf(" c_line: %d\n", settings.termios.c_line); + kprintf(" c_ispeed: %u\n", settings.termios.c_ispeed + + ((uint32_t)settings.termios.c_ispeed_high << 16)); + kprintf(" c_ospeed: %u\n", settings.termios.c_ospeed + + ((uint32_t)settings.termios.c_ospeed_high << 16)); for (int i = 0; i < NCCS; i++) - kprintf(" c_cc[%02d]: %d\n", i, settings.termios.c_cc[i]); + kprintf(" c_cc[%02d]: %d\n", i, settings.termios.c_cc[i]); - kprintf(" wsize: %u x %u c, %u x %u pxl\n", + kprintf(" wsize: %u x %u c, %u x %u pxl\n", settings.window_size.ws_row, settings.window_size.ws_col, settings.window_size.ws_xpixel, settings.window_size.ws_ypixel); } diff --git a/src/add-ons/kernel/generic/tty/tty.cpp b/src/add-ons/kernel/generic/tty/tty.cpp index ddae54a01c..dea650b6e8 100644 --- a/src/add-ons/kernel/generic/tty/tty.cpp +++ b/src/add-ons/kernel/generic/tty/tty.cpp @@ -760,8 +760,10 @@ reset_termios(struct termios& termios) termios.c_cflag = B19200 | CS8 | CREAD | HUPCL; // enable receiver, hang up on last close termios.c_lflag = ECHO | ISIG | ICANON; - termios.c_ispeed = B19200; - termios.c_ospeed = B19200; + termios.c_ispeed = 0; + termios.c_ospeed = 0; + termios.c_ispeed_high = 0; + termios.c_ospeed_high = 0; // control characters termios.c_cc[VINTR] = CTRL('C'); diff --git a/src/libs/bsd/termios.c b/src/libs/bsd/termios.c index 1049aeea6a..0513b33f25 100644 --- a/src/libs/bsd/termios.c +++ b/src/libs/bsd/termios.c @@ -11,12 +11,14 @@ int cfsetspeed(struct termios *termios, speed_t speed) { - /* Custom speed values are stored in c_ispeed and c_ospeed. + /* Custom values are stored in two parts for ABI compatibility reasons. * Standard values are inlined in c_cflag. */ if (speed > B31250) { termios->c_cflag |= CBAUD; - termios->c_ispeed = speed; - termios->c_ospeed = speed; + termios->c_ospeed = speed & 0xFFFF; + termios->c_ospeed_high = speed >> 16; + termios->c_ispeed = termios->c_ospeed; + termios->c_ispeed_high = termios->c_ospeed_high; return 0; } diff --git a/src/system/libroot/posix/termios.c b/src/system/libroot/posix/termios.c index ce0ebed5f4..8b2e949fea 100644 --- a/src/system/libroot/posix/termios.c +++ b/src/system/libroot/posix/termios.c @@ -106,7 +106,7 @@ speed_t cfgetispeed(const struct termios *termios) { if ((termios->c_cflag & CBAUD) == CBAUD) - return termios->c_ispeed; + return termios->c_ispeed + ((uint32_t)termios->c_ispeed_high << 16); return termios->c_cflag & CBAUD; } @@ -121,7 +121,8 @@ cfsetispeed(struct termios *termios, speed_t speed) detected only when the tcsetattr() function is called */ if (speed > B31250) { termios->c_cflag |= CBAUD; - termios->c_ispeed = speed; + termios->c_ispeed = speed & 0xFFFF; + termios->c_ispeed_high = speed >> 16; return 0; } @@ -135,7 +136,7 @@ speed_t cfgetospeed(const struct termios *termios) { if ((termios->c_cflag & CBAUD) == CBAUD) - return termios->c_ospeed; + return termios->c_ospeed + ((uint32_t)termios->c_ospeed_high << 16); return termios->c_cflag & CBAUD; } @@ -147,7 +148,8 @@ cfsetospeed(struct termios *termios, speed_t speed) /* Check for custom speed values (see above) */ if (speed > B31250) { termios->c_cflag |= CBAUD; - termios->c_ospeed = speed; + termios->c_ospeed = speed & 0xFFFF; + termios->c_ospeed_high = speed >> 16; return 0; }