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