1
2
3
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#include "tty.h"
12
13
14
15
16
17
18
19
20static 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,
23#ifdef __sparc__
24 76800, 153600, 307200, 614400, 921600, 500000, 576000,
25 1000000, 1152000, 1500000, 2000000
26#else
27 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
28 2500000, 3000000, 3500000, 4000000
29#endif
30};
31
32static 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,
35#ifdef __sparc__
36 B76800, B153600, B307200, B614400, B921600, B500000, B576000,
37 B1000000, B1152000, B1500000, B2000000
38#else
39 B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000,
40 B2500000, B3000000, B3500000, B4000000
41#endif
42};
43
44static int n_baud_table = ARRAY_SIZE(baud_table);
45
46
47
48
49
50
51
52
53
54
55
56
57
58speed_t tty_termios_baud_rate(struct ktermios *termios)
59{
60 unsigned int cbaud;
61
62 cbaud = termios->c_cflag & CBAUD;
63
64#ifdef BOTHER
65
66 if (cbaud == BOTHER)
67 return termios->c_ospeed;
68#endif
69 if (cbaud & CBAUDEX) {
70 cbaud &= ~CBAUDEX;
71
72 if (cbaud < 1 || cbaud + 15 > n_baud_table)
73 termios->c_cflag &= ~CBAUDEX;
74 else
75 cbaud += 15;
76 }
77 return cbaud >= n_baud_table ? 0 : baud_table[cbaud];
78}
79EXPORT_SYMBOL(tty_termios_baud_rate);
80
81
82
83
84
85
86
87
88
89
90
91
92
93speed_t tty_termios_input_baud_rate(struct ktermios *termios)
94{
95#ifdef IBSHIFT
96 unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD;
97
98 if (cbaud == B0)
99 return tty_termios_baud_rate(termios);
100#ifdef BOTHER
101
102 if (cbaud == BOTHER)
103 return termios->c_ispeed;
104#endif
105 if (cbaud & CBAUDEX) {
106 cbaud &= ~CBAUDEX;
107
108 if (cbaud < 1 || cbaud + 15 > n_baud_table)
109 termios->c_cflag &= ~(CBAUDEX << IBSHIFT);
110 else
111 cbaud += 15;
112 }
113 return cbaud >= n_baud_table ? 0 : baud_table[cbaud];
114#else
115 return tty_termios_baud_rate(termios);
116#endif
117}
118EXPORT_SYMBOL(tty_termios_input_baud_rate);
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142void tty_termios_encode_baud_rate(struct ktermios *termios,
143 speed_t ibaud, speed_t obaud)
144{
145 int i = 0;
146 int ifound = -1, ofound = -1;
147 int iclose = ibaud/50, oclose = obaud/50;
148 int ibinput = 0;
149
150 if (obaud == 0)
151 ibaud = 0;
152
153 termios->c_ispeed = ibaud;
154 termios->c_ospeed = obaud;
155
156#ifdef IBSHIFT
157 if ((termios->c_cflag >> IBSHIFT) & CBAUD)
158 ibinput = 1;
159#endif
160#ifdef BOTHER
161
162
163
164
165
166 if ((termios->c_cflag & CBAUD) == BOTHER) {
167 oclose = 0;
168 if (!ibinput)
169 iclose = 0;
170 }
171 if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
172 iclose = 0;
173#endif
174 termios->c_cflag &= ~CBAUD;
175#ifdef IBSHIFT
176 termios->c_cflag &= ~(CBAUD << IBSHIFT);
177#endif
178
179
180
181
182
183
184
185
186 do {
187 if (obaud - oclose <= baud_table[i] &&
188 obaud + oclose >= baud_table[i]) {
189 termios->c_cflag |= baud_bits[i];
190 ofound = i;
191 }
192 if (ibaud - iclose <= baud_table[i] &&
193 ibaud + iclose >= baud_table[i]) {
194
195
196
197 if (ofound == i && !ibinput)
198 ifound = i;
199#ifdef IBSHIFT
200 else {
201 ifound = i;
202 termios->c_cflag |= (baud_bits[i] << IBSHIFT);
203 }
204#endif
205 }
206 } while (++i < n_baud_table);
207
208
209
210
211
212#ifdef BOTHER
213 if (ofound == -1)
214 termios->c_cflag |= BOTHER;
215
216
217
218 if (ifound == -1 && (ibaud != obaud || ibinput))
219 termios->c_cflag |= (BOTHER << IBSHIFT);
220#else
221 if (ifound == -1 || ofound == -1)
222 pr_warn_once("tty: Unable to return correct speed data as your architecture needs updating.\n");
223#endif
224}
225EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
226
227
228
229
230
231
232
233
234
235
236
237
238void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
239{
240 tty_termios_encode_baud_rate(&tty->termios, ibaud, obaud);
241}
242EXPORT_SYMBOL_GPL(tty_encode_baud_rate);
243