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 <[email protected]>
Tested-by: Commit checker robot <[email protected]>
Reviewed-by: Adrien Destugues <[email protected]>
This commit is contained in:
PulkoMandy
2024-12-14 12:29:05 +00:00
committed by Adrien Destugues
parent da701bc547
commit a38c3c0384
7 changed files with 54 additions and 40 deletions
+8 -4
View File
@@ -9,20 +9,24 @@
#include <sys/types.h> #include <sys/types.h>
typedef __haiku_uint32 tcflag_t; typedef __haiku_uint16 tcflag_t;
typedef unsigned char speed_t; typedef __haiku_uint32 speed_t;
typedef unsigned char cc_t; typedef unsigned char cc_t;
#define NCCS 11 /* number of control characters */ #define NCCS 11 /* number of control characters */
struct termios { struct termios {
tcflag_t c_iflag; /* input modes */ tcflag_t c_iflag; /* input modes */
tcflag_t c_ispeed; /* input baudrate */
tcflag_t c_oflag; /* output modes */ tcflag_t c_oflag; /* output modes */
tcflag_t c_ospeed; /* output baudrate */
tcflag_t c_cflag; /* control modes */ 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_lflag; /* local modes */
tcflag_t c_ospeed_high; /* high word of output baudrate */
char c_line; /* line discipline */ char c_line; /* line discipline */
speed_t c_ispeed; /* custom input baudrate */ unsigned char _padding; /* unused */
speed_t c_ospeed; /* custom output baudrate */ unsigned char _padding2; /* unused */
cc_t c_cc[NCCS]; /* control characters */ cc_t c_cc[NCCS]; /* control characters */
}; };
@@ -160,10 +160,11 @@ SerialDevice::SetModes(struct termios *tios)
TRACE_FUNCRES(trace_termios, tios); TRACE_FUNCRES(trace_termios, tios);
uint8 baud = tios->c_cflag & CBAUD; uint8 baud = tios->c_cflag & CBAUD;
int32 speed = baud_index_to_speed(baud); int32 speed;
if (speed < 0) { if (baud == CBAUD) {
baud = CBAUD; speed = tios->c_ospeed + (tios->c_ospeed_high << 16);
speed = tios->c_ospeed; } else {
speed = baud_index_to_speed(baud);
} }
// update our master config in full // update our master config in full
@@ -106,17 +106,18 @@ void
trace_termios(struct termios *tios) trace_termios(struct termios *tios)
{ {
TRACE("struct termios:\n" TRACE("struct termios:\n"
"\tc_iflag: 0x%08x\n" "\tc_iflag: 0x%04x\n"
"\tc_oflag: 0x%08x\n" "\tc_oflag: 0x%04x\n"
"\tc_cflag: 0x%08x\n" "\tc_cflag: 0x%04x\n"
"\tc_lflag: 0x%08x\n" "\tc_lflag: 0x%04x\n"
"\tc_line: 0x%08x\n" "\tc_line: 0x%04x\n"
"\tc_ispeed: 0x%08x\n" "\tc_ispeed: %u\n"
"\tc_ospeed: 0x%08x\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", "\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_iflag, tios->c_oflag, tios->c_cflag, tios->c_lflag,
tios->c_line, 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[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[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]); tios->c_cc[8], tios->c_cc[9], tios->c_cc[10]);
+4 -2
View File
@@ -33,8 +33,10 @@ dump_tty_settings(struct tty_settings& settings)
kprintf(" c_cflag: 0x%08" B_PRIx32 "\n", settings.termios.c_cflag); 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_lflag: 0x%08" B_PRIx32 "\n", settings.termios.c_lflag);
kprintf(" c_line: %d\n", settings.termios.c_line); kprintf(" c_line: %d\n", settings.termios.c_line);
kprintf(" c_ispeed: %u\n", settings.termios.c_ispeed); kprintf(" c_ispeed: %u\n", settings.termios.c_ispeed
kprintf(" c_ospeed: %u\n", settings.termios.c_ospeed); + ((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++) 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]);
+4 -2
View File
@@ -760,8 +760,10 @@ reset_termios(struct termios& termios)
termios.c_cflag = B19200 | CS8 | CREAD | HUPCL; termios.c_cflag = B19200 | CS8 | CREAD | HUPCL;
// enable receiver, hang up on last close // enable receiver, hang up on last close
termios.c_lflag = ECHO | ISIG | ICANON; termios.c_lflag = ECHO | ISIG | ICANON;
termios.c_ispeed = B19200; termios.c_ispeed = 0;
termios.c_ospeed = B19200; termios.c_ospeed = 0;
termios.c_ispeed_high = 0;
termios.c_ospeed_high = 0;
// control characters // control characters
termios.c_cc[VINTR] = CTRL('C'); termios.c_cc[VINTR] = CTRL('C');
+5 -3
View File
@@ -11,12 +11,14 @@
int int
cfsetspeed(struct termios *termios, speed_t speed) 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. */ * Standard values are inlined in c_cflag. */
if (speed > B31250) { if (speed > B31250) {
termios->c_cflag |= CBAUD; termios->c_cflag |= CBAUD;
termios->c_ispeed = speed; termios->c_ospeed = speed & 0xFFFF;
termios->c_ospeed = speed; termios->c_ospeed_high = speed >> 16;
termios->c_ispeed = termios->c_ospeed;
termios->c_ispeed_high = termios->c_ospeed_high;
return 0; return 0;
} }
+6 -4
View File
@@ -106,7 +106,7 @@ speed_t
cfgetispeed(const struct termios *termios) cfgetispeed(const struct termios *termios)
{ {
if ((termios->c_cflag & CBAUD) == CBAUD) 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; return termios->c_cflag & CBAUD;
} }
@@ -121,7 +121,8 @@ cfsetispeed(struct termios *termios, speed_t speed)
detected only when the tcsetattr() function is called */ detected only when the tcsetattr() function is called */
if (speed > B31250) { if (speed > B31250) {
termios->c_cflag |= CBAUD; termios->c_cflag |= CBAUD;
termios->c_ispeed = speed; termios->c_ispeed = speed & 0xFFFF;
termios->c_ispeed_high = speed >> 16;
return 0; return 0;
} }
@@ -135,7 +136,7 @@ speed_t
cfgetospeed(const struct termios *termios) cfgetospeed(const struct termios *termios)
{ {
if ((termios->c_cflag & CBAUD) == CBAUD) 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; return termios->c_cflag & CBAUD;
} }
@@ -147,7 +148,8 @@ cfsetospeed(struct termios *termios, speed_t speed)
/* Check for custom speed values (see above) */ /* Check for custom speed values (see above) */
if (speed > B31250) { if (speed > B31250) {
termios->c_cflag |= CBAUD; termios->c_cflag |= CBAUD;
termios->c_ospeed = speed; termios->c_ospeed = speed & 0xFFFF;
termios->c_ospeed_high = speed >> 16;
return 0; return 0;
} }