Skip to content

Commit 47462c5

Browse files
paligregkh
authored andcommitted
serial: core: Fix initializing and restoring termios speed
commit 027b571 upstream. Since commit edc6afc ("tty: switch to ktermios and new framework") termios speed is no longer stored only in c_cflag member but also in new additional c_ispeed and c_ospeed members. If BOTHER flag is set in c_cflag then termios speed is stored only in these new members. Therefore to correctly restore termios speed it is required to store also ispeed and ospeed members, not only cflag member. In case only cflag member with BOTHER flag is restored then functions tty_termios_baud_rate() and tty_termios_input_baud_rate() returns baudrate stored in c_ospeed / c_ispeed member, which is zero as it was not restored too. If reported baudrate is invalid (e.g. zero) then serial core functions report fallback baudrate value 9600. So it means that in this case original baudrate is lost and kernel changes it to value 9600. Simple reproducer of this issue is to boot kernel with following command line argument: "console=ttyXXX,86400" (where ttyXXX is the device name). For speed 86400 there is no Bnnn constant and therefore kernel has to represent this speed via BOTHER c_cflag. Which means that speed is stored only in c_ospeed and c_ispeed members, not in c_cflag anymore. If bootloader correctly configures serial device to speed 86400 then kernel prints boot log to early console at speed speed 86400 without any issue. But after kernel starts initializing real console device ttyXXX then speed is changed to fallback value 9600 because information about speed was lost. This patch fixes above issue by storing and restoring also ispeed and ospeed members, which are required for BOTHER flag. Fixes: edc6afc ("[PATCH] tty: switch to ktermios and new framework") Cc: stable@vger.kernel.org Signed-off-by: Pali Rohár <pali@kernel.org> Link: https://lore.kernel.org/r/20211002130900.9518-1-pali@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent c1e6e42 commit 47462c5

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎drivers/tty/serial/serial_core.c‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,11 @@ static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
222222
if (retval == 0) {
223223
if (uart_console(uport) && uport->cons->cflag) {
224224
tty->termios.c_cflag = uport->cons->cflag;
225+
tty->termios.c_ispeed = uport->cons->ispeed;
226+
tty->termios.c_ospeed = uport->cons->ospeed;
225227
uport->cons->cflag = 0;
228+
uport->cons->ispeed = 0;
229+
uport->cons->ospeed = 0;
226230
}
227231
/*
228232
* Initialise the hardware port settings.
@@ -290,8 +294,11 @@ static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
290294
/*
291295
* Turn off DTR and RTS early.
292296
*/
293-
if (uport && uart_console(uport) && tty)
297+
if (uport && uart_console(uport) && tty) {
294298
uport->cons->cflag = tty->termios.c_cflag;
299+
uport->cons->ispeed = tty->termios.c_ispeed;
300+
uport->cons->ospeed = tty->termios.c_ospeed;
301+
}
295302

296303
if (!tty || C_HUPCL(tty))
297304
uart_port_dtr_rts(uport, 0);
@@ -2123,8 +2130,11 @@ uart_set_options(struct uart_port *port, struct console *co,
21232130
* Allow the setting of the UART parameters with a NULL console
21242131
* too:
21252132
*/
2126-
if (co)
2133+
if (co) {
21272134
co->cflag = termios.c_cflag;
2135+
co->ispeed = termios.c_ispeed;
2136+
co->ospeed = termios.c_ospeed;
2137+
}
21282138

21292139
return 0;
21302140
}
@@ -2258,6 +2268,8 @@ int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
22582268
*/
22592269
memset(&termios, 0, sizeof(struct ktermios));
22602270
termios.c_cflag = uport->cons->cflag;
2271+
termios.c_ispeed = uport->cons->ispeed;
2272+
termios.c_ospeed = uport->cons->ospeed;
22612273

22622274
/*
22632275
* If that's unset, use the tty termios setting.

‎include/linux/console.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ struct console {
150150
short flags;
151151
short index;
152152
int cflag;
153+
uint ispeed;
154+
uint ospeed;
153155
void *data;
154156
struct console *next;
155157
};

0 commit comments

Comments
 (0)