Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds |
| 4 | */ |
| 5 | |
| 6 | #include <linux/types.h> |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/termios.h> |
| 9 | #include <linux/tty.h> |
| 10 | #include <linux/export.h> |
Greg Kroah-Hartman | 5ffa6e3 | 2021-04-08 14:51:34 +0200 | [diff] [blame] | 11 | #include "tty.h" |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 12 | |
| 13 | |
| 14 | /* |
| 15 | * Routine which returns the baud rate of the tty |
| 16 | * |
| 17 | * Note that the baud_table needs to be kept in sync with the |
| 18 | * include/asm/termbits.h file. |
| 19 | */ |
| 20 | static const speed_t baud_table[] = { |
Andy Shevchenko | 6ada606 | 2020-01-16 00:41:23 +0200 | [diff] [blame] | 21 | 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, |
| 22 | 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 23 | #ifdef __sparc__ |
Andy Shevchenko | 1ddeb5a | 2020-01-16 00:41:24 +0200 | [diff] [blame] | 24 | 76800, 153600, 307200, 614400, 921600, 500000, 576000, |
| 25 | 1000000, 1152000, 1500000, 2000000 |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 26 | #else |
| 27 | 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000, |
| 28 | 2500000, 3000000, 3500000, 4000000 |
| 29 | #endif |
| 30 | }; |
| 31 | |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 32 | static const tcflag_t baud_bits[] = { |
Andy Shevchenko | 6ada606 | 2020-01-16 00:41:23 +0200 | [diff] [blame] | 33 | B0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400, |
| 34 | B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, |
| 35 | #ifdef __sparc__ |
Andy Shevchenko | 1ddeb5a | 2020-01-16 00:41:24 +0200 | [diff] [blame] | 36 | B76800, B153600, B307200, B614400, B921600, B500000, B576000, |
| 37 | B1000000, B1152000, B1500000, B2000000 |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 38 | #else |
Andy Shevchenko | 6ada606 | 2020-01-16 00:41:23 +0200 | [diff] [blame] | 39 | B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, |
| 40 | B2500000, B3000000, B3500000, B4000000 |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 41 | #endif |
Andy Shevchenko | 6ada606 | 2020-01-16 00:41:23 +0200 | [diff] [blame] | 42 | }; |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 43 | |
| 44 | static int n_baud_table = ARRAY_SIZE(baud_table); |
| 45 | |
| 46 | /** |
| 47 | * tty_termios_baud_rate |
| 48 | * @termios: termios structure |
| 49 | * |
| 50 | * Convert termios baud rate data into a speed. This should be called |
| 51 | * with the termios lock held if this termios is a terminal termios |
Ilpo Järvinen | 87888fb | 2022-08-16 14:57:32 +0300 | [diff] [blame] | 52 | * structure. Device drivers can call this function but should use |
| 53 | * ->c_[io]speed directly as they are updated. |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 54 | * |
| 55 | * Locking: none |
| 56 | */ |
| 57 | |
Ilpo Järvinen | 87888fb | 2022-08-16 14:57:32 +0300 | [diff] [blame] | 58 | speed_t tty_termios_baud_rate(const struct ktermios *termios) |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 59 | { |
| 60 | unsigned int cbaud; |
| 61 | |
| 62 | cbaud = termios->c_cflag & CBAUD; |
| 63 | |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 64 | /* Magic token for arbitrary speed via c_ispeed/c_ospeed */ |
| 65 | if (cbaud == BOTHER) |
| 66 | return termios->c_ospeed; |
Ilpo Järvinen | 69648d7 | 2022-05-13 11:29:03 +0300 | [diff] [blame] | 67 | |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 68 | if (cbaud & CBAUDEX) { |
| 69 | cbaud &= ~CBAUDEX; |
Ilpo Järvinen | 87888fb | 2022-08-16 14:57:32 +0300 | [diff] [blame] | 70 | cbaud += 15; |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 71 | } |
H. Peter Anvin | 991a251 | 2018-10-22 09:19:04 -0700 | [diff] [blame] | 72 | return cbaud >= n_baud_table ? 0 : baud_table[cbaud]; |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 73 | } |
| 74 | EXPORT_SYMBOL(tty_termios_baud_rate); |
| 75 | |
| 76 | /** |
| 77 | * tty_termios_input_baud_rate |
| 78 | * @termios: termios structure |
| 79 | * |
| 80 | * Convert termios baud rate data into a speed. This should be called |
| 81 | * with the termios lock held if this termios is a terminal termios |
Ilpo Järvinen | 87888fb | 2022-08-16 14:57:32 +0300 | [diff] [blame] | 82 | * structure. Device drivers can call this function but should use |
| 83 | * ->c_[io]speed directly as they are updated. |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 84 | * |
| 85 | * Locking: none |
| 86 | */ |
| 87 | |
Ilpo Järvinen | 87888fb | 2022-08-16 14:57:32 +0300 | [diff] [blame] | 88 | speed_t tty_termios_input_baud_rate(const struct ktermios *termios) |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 89 | { |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 90 | unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD; |
| 91 | |
| 92 | if (cbaud == B0) |
| 93 | return tty_termios_baud_rate(termios); |
Ilpo Järvinen | 69648d7 | 2022-05-13 11:29:03 +0300 | [diff] [blame] | 94 | |
Ilpo Järvinen | 292e2e7 | 2022-08-16 14:57:33 +0300 | [diff] [blame] | 95 | /* Magic token for arbitrary speed via c_ispeed */ |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 96 | if (cbaud == BOTHER) |
| 97 | return termios->c_ispeed; |
Ilpo Järvinen | 69648d7 | 2022-05-13 11:29:03 +0300 | [diff] [blame] | 98 | |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 99 | if (cbaud & CBAUDEX) { |
| 100 | cbaud &= ~CBAUDEX; |
Ilpo Järvinen | 87888fb | 2022-08-16 14:57:32 +0300 | [diff] [blame] | 101 | cbaud += 15; |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 102 | } |
H. Peter Anvin | 991a251 | 2018-10-22 09:19:04 -0700 | [diff] [blame] | 103 | return cbaud >= n_baud_table ? 0 : baud_table[cbaud]; |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 104 | } |
| 105 | EXPORT_SYMBOL(tty_termios_input_baud_rate); |
| 106 | |
| 107 | /** |
| 108 | * tty_termios_encode_baud_rate |
| 109 | * @termios: ktermios structure holding user requested state |
Jiri Slaby | fa44195 | 2020-08-18 10:56:51 +0200 | [diff] [blame] | 110 | * @ibaud: input speed |
| 111 | * @obaud: output speed |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 112 | * |
| 113 | * Encode the speeds set into the passed termios structure. This is |
| 114 | * used as a library helper for drivers so that they can report back |
| 115 | * the actual speed selected when it differs from the speed requested |
| 116 | * |
| 117 | * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour |
| 118 | * we need to carefully set the bits when the user does not get the |
| 119 | * desired speed. We allow small margins and preserve as much of possible |
| 120 | * of the input intent to keep compatibility. |
| 121 | * |
| 122 | * Locking: Caller should hold termios lock. This is already held |
| 123 | * when calling this function from the driver termios handler. |
| 124 | * |
| 125 | * The ifdefs deal with platforms whose owners have yet to update them |
| 126 | * and will all go away once this is done. |
| 127 | */ |
| 128 | |
| 129 | void tty_termios_encode_baud_rate(struct ktermios *termios, |
| 130 | speed_t ibaud, speed_t obaud) |
| 131 | { |
| 132 | int i = 0; |
| 133 | int ifound = -1, ofound = -1; |
| 134 | int iclose = ibaud/50, oclose = obaud/50; |
| 135 | int ibinput = 0; |
| 136 | |
Xiaofei Tan | eb460ed | 2021-05-12 17:26:09 +0800 | [diff] [blame] | 137 | if (obaud == 0) /* CD dropped */ |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 138 | ibaud = 0; /* Clear ibaud to be sure */ |
| 139 | |
| 140 | termios->c_ispeed = ibaud; |
| 141 | termios->c_ospeed = obaud; |
| 142 | |
Pali Rohár | 4545b06 | 2021-09-27 15:35:16 +0200 | [diff] [blame] | 143 | if (((termios->c_cflag >> IBSHIFT) & CBAUD) != B0) |
Johan Hovold | 1cee38f | 2018-07-15 15:39:34 +0200 | [diff] [blame] | 144 | ibinput = 1; /* An input speed was specified */ |
Ilpo Järvinen | 9cca25e | 2022-05-13 11:29:04 +0300 | [diff] [blame] | 145 | |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 146 | /* If the user asked for a precise weird speed give a precise weird |
Xiaofei Tan | ad48749 | 2021-05-12 17:26:10 +0800 | [diff] [blame] | 147 | * answer. If they asked for a Bfoo speed they may have problems |
| 148 | * digesting non-exact replies so fuzz a bit. |
| 149 | */ |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 150 | |
Johan Hovold | 1cee38f | 2018-07-15 15:39:34 +0200 | [diff] [blame] | 151 | if ((termios->c_cflag & CBAUD) == BOTHER) { |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 152 | oclose = 0; |
Johan Hovold | 1cee38f | 2018-07-15 15:39:34 +0200 | [diff] [blame] | 153 | if (!ibinput) |
| 154 | iclose = 0; |
| 155 | } |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 156 | if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER) |
| 157 | iclose = 0; |
Ilpo Järvinen | 69648d7 | 2022-05-13 11:29:03 +0300 | [diff] [blame] | 158 | |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 159 | termios->c_cflag &= ~CBAUD; |
Johan Hovold | fada18c | 2018-07-15 15:39:33 +0200 | [diff] [blame] | 160 | termios->c_cflag &= ~(CBAUD << IBSHIFT); |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | * Our goal is to find a close match to the standard baud rate |
| 164 | * returned. Walk the baud rate table and if we get a very close |
| 165 | * match then report back the speed as a POSIX Bxxxx value by |
| 166 | * preference |
| 167 | */ |
| 168 | |
| 169 | do { |
| 170 | if (obaud - oclose <= baud_table[i] && |
| 171 | obaud + oclose >= baud_table[i]) { |
| 172 | termios->c_cflag |= baud_bits[i]; |
| 173 | ofound = i; |
| 174 | } |
| 175 | if (ibaud - iclose <= baud_table[i] && |
| 176 | ibaud + iclose >= baud_table[i]) { |
| 177 | /* For the case input == output don't set IBAUD bits |
Xiaofei Tan | ad48749 | 2021-05-12 17:26:10 +0800 | [diff] [blame] | 178 | * if the user didn't do so. |
| 179 | */ |
Ilpo Järvinen | 9cca25e | 2022-05-13 11:29:04 +0300 | [diff] [blame] | 180 | if (ofound == i && !ibinput) { |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 181 | ifound = i; |
Ilpo Järvinen | 9cca25e | 2022-05-13 11:29:04 +0300 | [diff] [blame] | 182 | } else { |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 183 | ifound = i; |
| 184 | termios->c_cflag |= (baud_bits[i] << IBSHIFT); |
| 185 | } |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 186 | } |
| 187 | } while (++i < n_baud_table); |
| 188 | |
Ilpo Järvinen | 69648d7 | 2022-05-13 11:29:03 +0300 | [diff] [blame] | 189 | /* If we found no match then use BOTHER. */ |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 190 | if (ofound == -1) |
| 191 | termios->c_cflag |= BOTHER; |
| 192 | /* Set exact input bits only if the input and output differ or the |
Xiaofei Tan | ad48749 | 2021-05-12 17:26:10 +0800 | [diff] [blame] | 193 | * user already did. |
| 194 | */ |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 195 | if (ifound == -1 && (ibaud != obaud || ibinput)) |
| 196 | termios->c_cflag |= (BOTHER << IBSHIFT); |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 197 | } |
| 198 | EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate); |
| 199 | |
| 200 | /** |
| 201 | * tty_encode_baud_rate - set baud rate of the tty |
Lee Jones | 6e30f28 | 2020-11-04 19:35:16 +0000 | [diff] [blame] | 202 | * @tty: terminal device |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 203 | * @ibaud: input baud rate |
Jiri Slaby | fa44195 | 2020-08-18 10:56:51 +0200 | [diff] [blame] | 204 | * @obaud: output baud rate |
Nicolas Pitre | fff0a2c | 2017-04-12 18:37:15 -0400 | [diff] [blame] | 205 | * |
| 206 | * Update the current termios data for the tty with the new speed |
| 207 | * settings. The caller must hold the termios_rwsem for the tty in |
| 208 | * question. |
| 209 | */ |
| 210 | |
| 211 | void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud) |
| 212 | { |
| 213 | tty_termios_encode_baud_rate(&tty->termios, ibaud, obaud); |
| 214 | } |
| 215 | EXPORT_SYMBOL_GPL(tty_encode_baud_rate); |