1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
6 #include <linux/types.h>
7 #include <linux/kernel.h>
8 #include <linux/termios.h>
10 #include <linux/export.h>
15 * Routine which returns the baud rate of the tty
17 * Note that the baud_table needs to be kept in sync with the
18 * include/asm/termbits.h file.
20 static const speed_t baud_table[] = {
21 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
22 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800,
24 76800, 153600, 307200, 614400, 921600, 500000, 576000,
25 1000000, 1152000, 1500000, 2000000
27 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
28 2500000, 3000000, 3500000, 4000000
32 static const tcflag_t baud_bits[] = {
33 B0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400,
34 B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800,
36 B76800, B153600, B307200, B614400, B921600, B500000, B576000,
37 B1000000, B1152000, B1500000, B2000000
39 B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000,
40 B2500000, B3000000, B3500000, B4000000
44 static int n_baud_table = ARRAY_SIZE(baud_table);
47 * tty_termios_baud_rate
48 * @termios: termios structure
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
52 * structure. May change the termios data. Device drivers can call this
53 * function but should use ->c_[io]speed directly as they are updated.
58 speed_t tty_termios_baud_rate(struct ktermios *termios)
62 cbaud = termios->c_cflag & CBAUD;
65 /* Magic token for arbitrary speed via c_ispeed/c_ospeed */
67 return termios->c_ospeed;
69 if (cbaud & CBAUDEX) {
72 if (cbaud < 1 || cbaud + 15 > n_baud_table)
73 termios->c_cflag &= ~CBAUDEX;
77 return cbaud >= n_baud_table ? 0 : baud_table[cbaud];
79 EXPORT_SYMBOL(tty_termios_baud_rate);
82 * tty_termios_input_baud_rate
83 * @termios: termios structure
85 * Convert termios baud rate data into a speed. This should be called
86 * with the termios lock held if this termios is a terminal termios
87 * structure. May change the termios data. Device drivers can call this
88 * function but should use ->c_[io]speed directly as they are updated.
93 speed_t tty_termios_input_baud_rate(struct ktermios *termios)
96 unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD;
99 return tty_termios_baud_rate(termios);
101 /* Magic token for arbitrary speed via c_ispeed*/
103 return termios->c_ispeed;
105 if (cbaud & CBAUDEX) {
108 if (cbaud < 1 || cbaud + 15 > n_baud_table)
109 termios->c_cflag &= ~(CBAUDEX << IBSHIFT);
113 return cbaud >= n_baud_table ? 0 : baud_table[cbaud];
115 return tty_termios_baud_rate(termios);
118 EXPORT_SYMBOL(tty_termios_input_baud_rate);
121 * tty_termios_encode_baud_rate
122 * @termios: ktermios structure holding user requested state
123 * @ibaud: input speed
124 * @obaud: output speed
126 * Encode the speeds set into the passed termios structure. This is
127 * used as a library helper for drivers so that they can report back
128 * the actual speed selected when it differs from the speed requested
130 * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
131 * we need to carefully set the bits when the user does not get the
132 * desired speed. We allow small margins and preserve as much of possible
133 * of the input intent to keep compatibility.
135 * Locking: Caller should hold termios lock. This is already held
136 * when calling this function from the driver termios handler.
138 * The ifdefs deal with platforms whose owners have yet to update them
139 * and will all go away once this is done.
142 void tty_termios_encode_baud_rate(struct ktermios *termios,
143 speed_t ibaud, speed_t obaud)
146 int ifound = -1, ofound = -1;
147 int iclose = ibaud/50, oclose = obaud/50;
150 if (obaud == 0) /* CD dropped */
151 ibaud = 0; /* Clear ibaud to be sure */
153 termios->c_ispeed = ibaud;
154 termios->c_ospeed = obaud;
157 if ((termios->c_cflag >> IBSHIFT) & CBAUD)
158 ibinput = 1; /* An input speed was specified */
161 /* If the user asked for a precise weird speed give a precise weird
162 * answer. If they asked for a Bfoo speed they may have problems
163 * digesting non-exact replies so fuzz a bit.
166 if ((termios->c_cflag & CBAUD) == BOTHER) {
171 if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
174 termios->c_cflag &= ~CBAUD;
176 termios->c_cflag &= ~(CBAUD << IBSHIFT);
180 * Our goal is to find a close match to the standard baud rate
181 * returned. Walk the baud rate table and if we get a very close
182 * match then report back the speed as a POSIX Bxxxx value by
187 if (obaud - oclose <= baud_table[i] &&
188 obaud + oclose >= baud_table[i]) {
189 termios->c_cflag |= baud_bits[i];
192 if (ibaud - iclose <= baud_table[i] &&
193 ibaud + iclose >= baud_table[i]) {
194 /* For the case input == output don't set IBAUD bits
195 * if the user didn't do so.
197 if (ofound == i && !ibinput)
202 termios->c_cflag |= (baud_bits[i] << IBSHIFT);
206 } while (++i < n_baud_table);
209 * If we found no match then use BOTHER if provided or warn
210 * the user their platform maintainer needs to wake up if not.
214 termios->c_cflag |= BOTHER;
215 /* Set exact input bits only if the input and output differ or the
218 if (ifound == -1 && (ibaud != obaud || ibinput))
219 termios->c_cflag |= (BOTHER << IBSHIFT);
221 if (ifound == -1 || ofound == -1)
222 pr_warn_once("tty: Unable to return correct speed data as your architecture needs updating.\n");
225 EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
228 * tty_encode_baud_rate - set baud rate of the tty
229 * @tty: terminal device
230 * @ibaud: input baud rate
231 * @obaud: output baud rate
233 * Update the current termios data for the tty with the new speed
234 * settings. The caller must hold the termios_rwsem for the tty in
238 void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
240 tty_termios_encode_baud_rate(&tty->termios, ibaud, obaud);
242 EXPORT_SYMBOL_GPL(tty_encode_baud_rate);