XenevaOS
Loading...
Searching...
No Matches
termios.h
Go to the documentation of this file.
1#ifndef __LINUX_TERMIOS_H__
2#define __LINUX_TERMIOS_H__
3
4/*
5 * DCL <linux/termios.h> -- the termios structures and the flag vocabulary
6 * every tty file speaks.
7 *
8 * The structures are mainline's (uapi/asm-generic/termbits.h, which is what
9 * arm64 includes): struct termios/termios2/ktermios and struct winsize, with
10 * the full c_cc index list and all four flag sets copied verbatim. There is no
11 * room for interpretation in a flag table -- `CS8` is 0x30 because the UART
12 * divides c_cflag by 16 to program the line control register, and 8250_port.c
13 * does exactly that at serial8250_do_set_termios(). Inventing a "close enough"
14 * value here would silently produce a port that transmits but never receives.
15 *
16 * The kernel-side helpers (tty_termios_baud_rate and friends) are the reason
17 * this header is load-bearing rather than just a table: serial_core.c:504
18 * reads the line rate through tty_termios_baud_rate() to decide whether the
19 * port can be started, and 8250_port.c:2817 re-encodes a rate back into
20 * c_cflag so the divisor can be recomputed. Both directions have to agree, so
21 * they are written as one table read one way and one table scanned the other:
22 *
23 * tty_termios_baud_rate() c_cflag & CBAUD -> bits per second
24 * tty_termios_encode_baud_rate() bits per second -> c_cflag & CBAUD
25 *
26 * BOTHER (0x1000) means "not a standard rate": the real number lives in
27 * c_ospeed/c_ispeed, which is how a caller requests 4 MHz from a UART that has
28 * no named constant for it.
29 *
30 * Mainline keeps these as non-inline functions in drivers/tty/tty_termios.c.
31 * They are static inline here because stage 2 has no tty_termios.c to link
32 * against and a declaration nothing defines is a link error waiting for its
33 * first caller. The bodies are self-contained (a switch and a table), so the
34 * cost of inlining is nothing next to the port I/O it guards.
35 */
36
37
38/* --- uapi/asm-generic/termios.h: modem-control line states (TIOCM_*) ------
39 * mainline keeps these next to termios rather than in serial_core.h, which is
40 * why vendored 8250.h reaches TIOCM_CAR through <linux/serial.h> and gets
41 * them via termios.h. The values are the modem lines, not flags: DCL's flat
42 * BaseHdr has no uapi/ tree to import them from, so they are copied verbatim
43 * from asm-generic/termios.h -- every one of them is an ABI constant that
44 * userspace's TIOCMGET/TIOCMSET sees.
45 */
46#define TIOCM_LE 0x001
47#define TIOCM_DTR 0x002
48#define TIOCM_RTS 0x004
49#define TIOCM_ST 0x008
50#define TIOCM_SR 0x010
51#define TIOCM_CTS 0x020
52#define TIOCM_CAR 0x040
53#define TIOCM_RNG 0x080
54#define TIOCM_DSR 0x100
55#define TIOCM_CD TIOCM_CAR
56#define TIOCM_RI TIOCM_RNG
57#define TIOCM_OUT1 0x2000
58#define TIOCM_OUT2 0x4000
59#define TIOCM_LOOP 0x8000
60
61#include <linux/kernel.h> /* tcflag_t users get u8/u32 here, and bool */
62/*
63 * The TC and TIOC request numbers (TCGETS, TCSETS, TCXONC, TIOCGSOFTCAR,
64 * TIOCGLCKTRMIOS ...). 14 of them are spelled by tty_ioctl.c and all 14 came
65 * back undeclared until this include existed; TIOCM_* already in this file
66 * are the *modem line* bits from uapi/asm-generic/termios.h, a different
67 * file from the request numbers in uapi/asm-generic/ioctls.h, which is why
68 * one existed without the other.
69 *
70 * The #ifdef _IO guard on the _IOR()-computed entries inside it (TCGETS2 and
71 * friends) is why this works: those are function-like macro *definitions*,
72 * never expanded unless a file uses them, so a DCL with no struct termios2
73 * still reads the header cleanly.
74 */
75#include <asm-generic/ioctls.h>
76
77/* --- uapi/asm-generic/termbits-common.h -------------------------------- */
78typedef unsigned char cc_t;
79typedef unsigned int speed_t;
80
81/* c_iflag bits */
82#define IGNBRK 0x001 /* Ignore break condition */
83#define BRKINT 0x002 /* Signal interrupt on break */
84#define IGNPAR 0x004 /* Ignore characters with parity errors */
85#define PARMRK 0x008 /* Mark parity and framing errors */
86#define INPCK 0x010 /* Enable input parity check */
87#define ISTRIP 0x020 /* Strip 8th bit off characters */
88#define INLCR 0x040 /* Map NL to CR on input */
89#define IGNCR 0x080 /* Ignore CR */
90#define ICRNL 0x100 /* Map CR to NL on input */
91#define IXANY 0x800 /* Any character will restart after stop */
92
93/* c_iflag bits (from termbits.h) */
94#define IUCLC 0x0200
95#define IXON 0x0400
96#define IXOFF 0x1000
97#define IMAXBEL 0x2000
98#define IUTF8 0x4000
99
100/* c_oflag bits */
101#define OPOST 0x01 /* Perform output processing */
102#define OLCUC 0x00002
103#define ONLCR 0x00004
104#define OCRNL 0x08
105#define ONOCR 0x10
106#define ONLRET 0x20
107#define OFILL 0x40
108#define OFDEL 0x80
109
110#define NLDLY 0x00100
111#define NL0 0x00000
112#define NL1 0x00100
113#define CRDLY 0x00600
114#define CR0 0x00000
115#define CR1 0x00200
116#define CR2 0x00400
117#define CR3 0x00600
118#define TABDLY 0x01800
119#define TAB0 0x00000
120#define TAB1 0x00800
121#define TAB2 0x01000
122#define TAB3 0x01800
123#define XTABS 0x01800
124#define BSDLY 0x02000
125#define BS0 0x00000
126#define BS1 0x02000
127#define VTDLY 0x04000
128#define VT0 0x00000
129#define VT1 0x04000
130#define FFDLY 0x08000
131#define FF0 0x00000
132#define FF1 0x08000
133
134/* c_cflag bit meaning */
135#define CBAUD 0x0000100f
136#define CSIZE 0x00000030
137#define CS5 0x00000000
138#define CS6 0x00000010
139#define CS7 0x00000020
140#define CS8 0x00000030
141#define CSTOPB 0x00000040
142#define CREAD 0x00000080
143#define PARENB 0x00000100
144#define PARODD 0x00000200
145#define HUPCL 0x00000400
146#define CLOCAL 0x00000800
147#define CBAUDEX 0x00001000
148#define BOTHER 0x00001000
149#define B57600 0x00001001
150#define B115200 0x00001002
151#define B230400 0x00001003
152#define B460800 0x00001004
153#define B500000 0x00001005
154#define B576000 0x00001006
155#define B921600 0x00001007
156#define B1000000 0x00001008
157#define B1152000 0x00001009
158#define B1500000 0x0000100a
159#define B2000000 0x0000100b
160#define B2500000 0x0000100c
161#define B3000000 0x0000100d
162#define B3500000 0x0000100e
163#define B4000000 0x0000100f
164#define CIBAUD 0x100f0000 /* input baud rate */
165#define ADDRB 0x20000000
166#define CMSPAR 0x40000000 /* mark or space (stick) parity */
167#define CRTSCTS 0x80000000 /* flow control */
168#define IBSHIFT 16 /* Shift from CBAUD to CIBAUD */
169
170/* The classic rates. Values are the CBAUD encodings, not the bit rates --
171 * that is what `c_cflag & CBAUD` compares against. */
172#define B0 0x00000000
173#define B50 0x00000001
174#define B75 0x00000002
175#define B110 0x00000003
176#define B134 0x00000004
177#define B150 0x00000005
178#define B200 0x00000006
179#define B300 0x00000007
180#define B600 0x00000008
181#define B1200 0x00000009
182#define B1800 0x0000000a
183#define B2400 0x0000000b
184#define B4800 0x0000000c
185#define B9600 0x0000000d
186#define B19200 0x0000000e
187#define B38400 0x0000000f
188#define EXTA B19200
189#define EXTB B38400
190
191/* c_lflag bits */
192#define ISIG 0x00001
193#define ICANON 0x00002
194#define XCASE 0x00004
195#define ECHO 0x00008
196#define ECHOE 0x00010
197#define ECHOK 0x00020
198#define ECHONL 0x00040
199#define NOFLSH 0x00080
200#define TOSTOP 0x00100
201#define ECHOCTL 0x00200
202#define ECHOPRT 0x00400
203#define ECHOKE 0x00800
204#define FLUSHO 0x01000
205#define PENDIN 0x04000
206#define IEXTEN 0x08000
207#define EXTPROC 0x10000
208
209/* tcflow() ACTION argument and TCXONC use these */
210#define TCOOFF 0
211#define TCOON 1
212#define TCIOFF 2
213#define TCION 3
214
215/* tcflush() QUEUE_SELECTOR argument and TCFLSH use these */
216#define TCIFLUSH 0
217#define TCOFLUSH 1
218#define TCIOFLUSH 2
219
220/* tcsetattr uses these */
221#define TCSANOW 0
222#define TCSADRAIN 1
223#define TCSAFLUSH 2
224
225/* --- uapi/asm-generic/termbits.h --------------------------------------- */
226typedef unsigned int tcflag_t;
227
228#define NCCS 19
229
230struct termios {
231 tcflag_t c_iflag; /* input mode flags */
232 tcflag_t c_oflag; /* output mode flags */
233 tcflag_t c_cflag; /* control mode flags */
234 tcflag_t c_lflag; /* local mode flags */
235 cc_t c_line; /* line discipline */
236 cc_t c_cc[NCCS]; /* control characters */
237};
238
249
260
261/* c_cc character indices */
262#define VINTR 0
263#define VQUIT 1
264#define VERASE 2
265#define VKILL 3
266#define VEOF 4
267#define VTIME 5
268#define VMIN 6
269#define VSWTC 7
270#define VSTART 8
271#define VSTOP 9
272#define VSUSP 10
273#define VEOL 11
274#define VREPRINT 12
275#define VDISCARD 13
276#define VWERASE 14
277#define VLNEXT 15
278#define VEOL2 16
279
280/* uapi/asm-generic/termios.h -- the winsize block used by TIOCGWINSZ */
281struct winsize {
282 unsigned short ws_row;
283 unsigned short ws_col;
284 unsigned short ws_xpixel;
285 unsigned short ws_ypixel;
286};
287
288#define NCC 8
289struct termio {
290 unsigned short c_iflag;
291 unsigned short c_oflag;
292 unsigned short c_cflag;
293 unsigned short c_lflag;
294 unsigned char c_line;
295 unsigned char c_cc[NCC];
296};
297
298/*
299 * --- kernel-side rate helpers ------------------------------------------
300 *
301 * One table, read in both directions. The order matters: the index *is* the
302 * CBAUD encoding for 1..15 and 0x1001..0x100f, so baud_table[1] is 50 and
303 * baud_table[0x1001 - 1] would be wrong -- hence the two explicit ranges
304 * below rather than one array with holes in it.
305 */
306static inline speed_t __dcl_cbaud_to_rate(unsigned int cbaud)
307{
308 static const speed_t std_rate[16] = {
309 0, 50, 75, 110, 134, 150, 200, 300,
310 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400
311 };
312 static const speed_t high_rate[16] = {
313 /* index 0 == BOTHER: the rate is not encoded in c_cflag */
314 0, 57600, 115200, 230400, 460800, 500000, 576000, 921600,
315 1000000, 1152000, 1500000, 2000000, 2500000, 3000000,
316 3500000, 4000000
317 };
318
319 if (cbaud & CBAUDEX)
320 return high_rate[cbaud & 0x0f];
321 return std_rate[cbaud & 0x0f];
322}
323
324static inline speed_t tty_termios_baud_rate(const struct ktermios* termios)
325{
326 unsigned int cbaud = termios->c_cflag & CBAUD;
327
328 if (cbaud == BOTHER)
329 return termios->c_ospeed;
330 return __dcl_cbaud_to_rate(cbaud);
331}
332
333/*
334 * Input rate: CIBAUD is the output encoding shifted left by IBSHIFT, so the
335 * same table answers for it. A c_cflag that never mentions CIBAUD (every
336 * port before someone has set an input rate) falls back to the output rate --
337 * the common case, since tcsetattr() writes both together.
338 */
339static inline speed_t tty_termios_input_baud_rate(const struct ktermios* termios)
340{
341 unsigned int ibaud = (termios->c_cflag & CIBAUD) >> IBSHIFT;
342
343 if (ibaud == 0)
344 return tty_termios_baud_rate(termios);
345 if (ibaud == BOTHER)
346 return termios->c_ispeed;
347 return __dcl_cbaud_to_rate(ibaud);
348}
349
350static inline unsigned int __dcl_rate_to_cbaud(speed_t rate)
351{
352 switch (rate) {
353 case 0: return B0;
354 case 50: return B50;
355 case 75: return B75;
356 case 110: return B110;
357 case 134: return B134;
358 case 150: return B150;
359 case 200: return B200;
360 case 300: return B300;
361 case 600: return B600;
362 case 1200: return B1200;
363 case 1800: return B1800;
364 case 2400: return B2400;
365 case 4800: return B4800;
366 case 9600: return B9600;
367 case 19200: return B19200;
368 case 38400: return B38400;
369 case 57600: return B57600;
370 case 115200: return B115200;
371 case 230400: return B230400;
372 case 460800: return B460800;
373 case 500000: return B500000;
374 case 576000: return B576000;
375 case 921600: return B921600;
376 case 1000000: return B1000000;
377 case 1152000: return B1152000;
378 case 1500000: return B1500000;
379 case 2000000: return B2000000;
380 case 2500000: return B2500000;
381 case 3000000: return B3000000;
382 case 3500000: return B3500000;
383 case 4000000: return B4000000;
384 default: return BOTHER;
385 }
386}
387
388/*
389 * Encode a pair of rates into c_cflag. Note the shift: CIBAUD is CBAUD in the
390 * high half, so the input encoding is written one IBSHIFT to the left. Getting
391 * this backwards yields a port whose *output* runs at the *input* rate, which
392 * is invisible until someone opens a modem at 9600 and sees garbage.
393 */
394static inline void tty_termios_encode_baud_rate(struct ktermios* termios,
395 speed_t ibaud, speed_t obaud)
396{
397 termios->c_cflag &= ~(CBAUD | CIBAUD);
398 termios->c_cflag |= __dcl_rate_to_cbaud(obaud) |
399 ((unsigned int)__dcl_rate_to_cbaud(ibaud) << IBSHIFT);
400 termios->c_ispeed = ibaud;
401 termios->c_ospeed = obaud;
402}
403
404/*
405 * tty_termios_copy_hw -- declaration only, because drivers/tty/tty_ioctl.c:231
406 * has mainline's definition and is now compiled from source.
407 *
408 * It used to be a static inline here, and that was wrong twice over. A
409 * non-static declaration next to a static definition is a compile error, so
410 * vendoring tty_ioctl.c made the two collide outright. And the body it
411 * displaced was not mainline's: this file had
412 *
413 * new_t->c_cflag = old_t->c_cflag;
414 *
415 * where mainline keeps only the hardware half (HUPCL | CREAD | CLOCAL are the
416 * ones the caller may not change) and drops the processing flags. The
417 * comment that used to sit above it said "Mainline defines this in
418 * tty_termios.c" -- it does not; tty_ioctl.c:231 does, alongside nothing of
419 * the sort, and the rate helpers above really are separate. The declaration
420 * matches mainline's spelling exactly, which is what lets the call at
421 * tty_ioctl.c:343 take it.
422 *
423 * The other two of <linux/tty.h>'s note-2 trio, tty_termios_baud_rate() and
424 * tty_termios_encode_baud_rate(), stay static inline here: their definition
425 * is drivers/tty/tty_baudrate.c, which is not staged, and an inline beside an
426 * uncalled extern would only create a link error.
427 */
428void tty_termios_copy_hw(struct ktermios* new, const struct ktermios* old);
429
430/*
431 * tty_std_termios -- what a driver gets before anyone has configured it.
432 * 9600 8N1 with ICANON/ECHO on: the settings a text console wants on first
433 * open.
434 *
435 * Declared here, defined by drivers/tty/tty_io.c:124 now that tty_io.c is
436 * compiled from source -- which is where mainline puts it and why the
437 * comment that used to name DCL/tty_shim.c was wrong: that file never had a
438 * definition, it only ever had this declaration, and the symbol stayed
439 * undefined until tty_io.c landed.
440 */
441extern struct ktermios tty_std_termios;
442
443#endif /* __LINUX_TERMIOS_H__ */
#define CBAUDEX
Definition termios.h:147
#define B4000000
Definition termios.h:163
unsigned char cc_t
Definition termios.h:78
#define B1800
Definition termios.h:182
#define B3500000
Definition termios.h:162
#define B2500000
Definition termios.h:160
unsigned int speed_t
Definition termios.h:79
#define NCCS
Definition termios.h:228
#define B576000
Definition termios.h:154
#define BOTHER
Definition termios.h:148
#define B1000000
Definition termios.h:156
#define B1200
Definition termios.h:181
#define B4800
Definition termios.h:184
#define IBSHIFT
Definition termios.h:168
#define B600
Definition termios.h:180
#define B57600
Definition termios.h:149
#define B300
Definition termios.h:179
#define CBAUD
Definition termios.h:135
#define B500000
Definition termios.h:153
#define B460800
Definition termios.h:152
#define NCC
Definition termios.h:288
#define B75
Definition termios.h:174
#define B150
Definition termios.h:177
#define B1152000
Definition termios.h:157
#define B134
Definition termios.h:176
void tty_termios_copy_hw(struct ktermios *new, const struct ktermios *old)
#define B0
Definition termios.h:172
#define B200
Definition termios.h:178
#define B1500000
Definition termios.h:158
#define B230400
Definition termios.h:151
#define B38400
Definition termios.h:187
#define CIBAUD
Definition termios.h:164
#define B50
Definition termios.h:173
struct ktermios tty_std_termios
#define B110
Definition termios.h:175
#define B2000000
Definition termios.h:159
unsigned int tcflag_t
Definition termios.h:226
#define B3000000
Definition termios.h:161
#define B9600
Definition termios.h:185
#define B921600
Definition termios.h:155
#define B19200
Definition termios.h:186
#define B115200
Definition termios.h:150
#define B2400
Definition termios.h:183
Definition termios.h:250
speed_t c_ospeed
Definition termios.h:258
tcflag_t c_iflag
Definition termios.h:251
speed_t c_ispeed
Definition termios.h:257
tcflag_t c_cflag
Definition termios.h:253
tcflag_t c_oflag
Definition termios.h:252
cc_t c_line
Definition termios.h:255
tcflag_t c_lflag
Definition termios.h:254
cc_t c_cc[NCCS]
Definition termios.h:256
Definition termios.h:289
unsigned short c_cflag
Definition termios.h:292
unsigned short c_oflag
Definition termios.h:291
unsigned char c_line
Definition termios.h:294
unsigned short c_iflag
Definition termios.h:290
unsigned short c_lflag
Definition termios.h:293
unsigned char c_cc[NCC]
Definition termios.h:295
Definition termios.h:239
tcflag_t c_oflag
Definition termios.h:241
cc_t c_cc[NCCS]
Definition termios.h:245
speed_t c_ispeed
Definition termios.h:246
tcflag_t c_cflag
Definition termios.h:242
speed_t c_ospeed
Definition termios.h:247
tcflag_t c_lflag
Definition termios.h:243
cc_t c_line
Definition termios.h:244
tcflag_t c_iflag
Definition termios.h:240
Definition termios.h:230
tcflag_t c_cflag
Definition termios.h:233
cc_t c_cc[NCCS]
Definition termios.h:236
tcflag_t c_iflag
Definition termios.h:231
tcflag_t c_lflag
Definition termios.h:234
tcflag_t c_oflag
Definition termios.h:232
cc_t c_line
Definition termios.h:235
Definition termios.h:281
unsigned short ws_row
Definition termios.h:282
unsigned short ws_col
Definition termios.h:283
unsigned short ws_xpixel
Definition termios.h:284
unsigned short ws_ypixel
Definition termios.h:285