1 // SPDX-License-Identifier: GPL-2.0 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> 11 12 13 /* 14 * Routine which returns the baud rate of the tty 15 * 16 * Note that the baud_table needs to be kept in sync with the 17 * include/asm/termbits.h file. 18 */ 19 static const speed_t baud_table[] = { 20 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 21 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 22 #ifdef __sparc__ 23 76800, 153600, 307200, 614400, 921600 24 #else 25 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000, 26 2500000, 3000000, 3500000, 4000000 27 #endif 28 }; 29 30 static const tcflag_t baud_bits[] = { 31 B0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400, 32 B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, 33 #ifdef __sparc__ 34 B76800, B153600, B307200, B614400, B921600 35 #else 36 B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, 37 B2500000, B3000000, B3500000, B4000000 38 #endif 39 }; 40 41 static int n_baud_table = ARRAY_SIZE(baud_table); 42 43 /** 44 * tty_termios_baud_rate 45 * @termios: termios structure 46 * 47 * Convert termios baud rate data into a speed. This should be called 48 * with the termios lock held if this termios is a terminal termios 49 * structure. May change the termios data. Device drivers can call this 50 * function but should use ->c_[io]speed directly as they are updated. 51 * 52 * Locking: none 53 */ 54 55 speed_t tty_termios_baud_rate(struct ktermios *termios) 56 { 57 unsigned int cbaud; 58 59 cbaud = termios->c_cflag & CBAUD; 60 61 #ifdef BOTHER 62 /* Magic token for arbitrary speed via c_ispeed/c_ospeed */ 63 if (cbaud == BOTHER) 64 return termios->c_ospeed; 65 #endif 66 if (cbaud & CBAUDEX) { 67 cbaud &= ~CBAUDEX; 68 69 if (cbaud < 1 || cbaud + 15 > n_baud_table) 70 termios->c_cflag &= ~CBAUDEX; 71 else 72 cbaud += 15; 73 } 74 return cbaud >= n_baud_table ? 0 : baud_table[cbaud]; 75 } 76 EXPORT_SYMBOL(tty_termios_baud_rate); 77 78 /** 79 * tty_termios_input_baud_rate 80 * @termios: termios structure 81 * 82 * Convert termios baud rate data into a speed. This should be called 83 * with the termios lock held if this termios is a terminal termios 84 * structure. May change the termios data. Device drivers can call this 85 * function but should use ->c_[io]speed directly as they are updated. 86 * 87 * Locking: none 88 */ 89 90 speed_t tty_termios_input_baud_rate(struct ktermios *termios) 91 { 92 #ifdef IBSHIFT 93 unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD; 94 95 if (cbaud == B0) 96 return tty_termios_baud_rate(termios); 97 #ifdef BOTHER 98 /* Magic token for arbitrary speed via c_ispeed*/ 99 if (cbaud == BOTHER) 100 return termios->c_ispeed; 101 #endif 102 if (cbaud & CBAUDEX) { 103 cbaud &= ~CBAUDEX; 104 105 if (cbaud < 1 || cbaud + 15 > n_baud_table) 106 termios->c_cflag &= ~(CBAUDEX << IBSHIFT); 107 else 108 cbaud += 15; 109 } 110 return cbaud >= n_baud_table ? 0 : baud_table[cbaud]; 111 #else /* IBSHIFT */ 112 return tty_termios_baud_rate(termios); 113 #endif /* IBSHIFT */ 114 } 115 EXPORT_SYMBOL(tty_termios_input_baud_rate); 116 117 /** 118 * tty_termios_encode_baud_rate 119 * @termios: ktermios structure holding user requested state 120 * @ispeed: input speed 121 * @ospeed: output speed 122 * 123 * Encode the speeds set into the passed termios structure. This is 124 * used as a library helper for drivers so that they can report back 125 * the actual speed selected when it differs from the speed requested 126 * 127 * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour 128 * we need to carefully set the bits when the user does not get the 129 * desired speed. We allow small margins and preserve as much of possible 130 * of the input intent to keep compatibility. 131 * 132 * Locking: Caller should hold termios lock. This is already held 133 * when calling this function from the driver termios handler. 134 * 135 * The ifdefs deal with platforms whose owners have yet to update them 136 * and will all go away once this is done. 137 */ 138 139 void tty_termios_encode_baud_rate(struct ktermios *termios, 140 speed_t ibaud, speed_t obaud) 141 { 142 int i = 0; 143 int ifound = -1, ofound = -1; 144 int iclose = ibaud/50, oclose = obaud/50; 145 int ibinput = 0; 146 147 if (obaud == 0) /* CD dropped */ 148 ibaud = 0; /* Clear ibaud to be sure */ 149 150 termios->c_ispeed = ibaud; 151 termios->c_ospeed = obaud; 152 153 #ifdef IBSHIFT 154 if ((termios->c_cflag >> IBSHIFT) & CBAUD) 155 ibinput = 1; /* An input speed was specified */ 156 #endif 157 #ifdef BOTHER 158 /* If the user asked for a precise weird speed give a precise weird 159 answer. If they asked for a Bfoo speed they may have problems 160 digesting non-exact replies so fuzz a bit */ 161 162 if ((termios->c_cflag & CBAUD) == BOTHER) { 163 oclose = 0; 164 if (!ibinput) 165 iclose = 0; 166 } 167 if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER) 168 iclose = 0; 169 #endif 170 termios->c_cflag &= ~CBAUD; 171 #ifdef IBSHIFT 172 termios->c_cflag &= ~(CBAUD << IBSHIFT); 173 #endif 174 175 /* 176 * Our goal is to find a close match to the standard baud rate 177 * returned. Walk the baud rate table and if we get a very close 178 * match then report back the speed as a POSIX Bxxxx value by 179 * preference 180 */ 181 182 do { 183 if (obaud - oclose <= baud_table[i] && 184 obaud + oclose >= baud_table[i]) { 185 termios->c_cflag |= baud_bits[i]; 186 ofound = i; 187 } 188 if (ibaud - iclose <= baud_table[i] && 189 ibaud + iclose >= baud_table[i]) { 190 /* For the case input == output don't set IBAUD bits 191 if the user didn't do so */ 192 if (ofound == i && !ibinput) 193 ifound = i; 194 #ifdef IBSHIFT 195 else { 196 ifound = i; 197 termios->c_cflag |= (baud_bits[i] << IBSHIFT); 198 } 199 #endif 200 } 201 } while (++i < n_baud_table); 202 203 /* 204 * If we found no match then use BOTHER if provided or warn 205 * the user their platform maintainer needs to wake up if not. 206 */ 207 #ifdef BOTHER 208 if (ofound == -1) 209 termios->c_cflag |= BOTHER; 210 /* Set exact input bits only if the input and output differ or the 211 user already did */ 212 if (ifound == -1 && (ibaud != obaud || ibinput)) 213 termios->c_cflag |= (BOTHER << IBSHIFT); 214 #else 215 if (ifound == -1 || ofound == -1) 216 pr_warn_once("tty: Unable to return correct speed data as your architecture needs updating.\n"); 217 #endif 218 } 219 EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate); 220 221 /** 222 * tty_encode_baud_rate - set baud rate of the tty 223 * @ibaud: input baud rate 224 * @obad: output baud rate 225 * 226 * Update the current termios data for the tty with the new speed 227 * settings. The caller must hold the termios_rwsem for the tty in 228 * question. 229 */ 230 231 void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud) 232 { 233 tty_termios_encode_baud_rate(&tty->termios, ibaud, obaud); 234 } 235 EXPORT_SYMBOL_GPL(tty_encode_baud_rate); 236