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