xref: /netbsd/sys/compat/linux/common/linux_termios.h (revision f036aca9)
1 /*	$NetBSD: linux_termios.h,v 1.23 2021/09/23 06:56:27 ryo Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Eric Haszlakiewicz.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _LINUX_TERMIOS_H
33 #define _LINUX_TERMIOS_H
34 
35 #if defined(__i386__)
36 #include <compat/linux/arch/i386/linux_termios.h>
37 #elif defined(__m68k__)
38 #include <compat/linux/arch/m68k/linux_termios.h>
39 #elif defined(__alpha__)
40 #include <compat/linux/arch/alpha/linux_termios.h>
41 #elif defined(__powerpc__)
42 #include <compat/linux/arch/powerpc/linux_termios.h>
43 #elif defined(__mips__)
44 #include <compat/linux/arch/mips/linux_termios.h>
45 #elif defined(__aarch64__)
46 #include <compat/linux/arch/aarch64/linux_termios.h>
47 #elif defined(__arm__)
48 #include <compat/linux/arch/arm/linux_termios.h>
49 #elif defined(__amd64__)
50 #include <compat/linux/arch/amd64/linux_termios.h>
51 #else
52 #error Undefined linux_termios.h machine type.
53 #endif
54 
55 struct linux_winsize {
56 	unsigned short ws_row;
57 	unsigned short ws_col;
58 	unsigned short ws_xpixel;
59 	unsigned short ws_ypixel;
60 };
61 
62 /*
63  * LINUX_NCC is architecture dependent. It is now
64  * defined in sys/compat/linux/<arch>/linux_termios.h
65  */
66 struct linux_termio {
67 	unsigned short c_iflag;
68 	unsigned short c_oflag;
69 	unsigned short c_cflag;
70 	unsigned short c_lflag;
71 	unsigned char c_line;
72 	unsigned char c_cc[LINUX_NCC];
73 };
74 
75 struct linux_termios {
76 	linux_tcflag_t	c_iflag;
77 	linux_tcflag_t	c_oflag;
78 	linux_tcflag_t	c_cflag;
79 	linux_tcflag_t	c_lflag;
80 	linux_cc_t	c_line;
81 	linux_cc_t	c_cc[LINUX_NCCS];
82 #ifdef LINUX_LARGE_STRUCT_TERMIOS
83 	/*
84     * Present on some linux ports but unused:
85 	 * However we must enable it, else it breaks ioctl
86 	 * definitions (the size does not match anymore)
87     */
88 	linux_speed_t	c_ispeed;
89 	linux_speed_t	c_ospeed;
90 #endif
91 };
92 
93 /* Linux modem line defines.. not sure if they'll be used */
94 #define LINUX_TIOCM_LE		0x0001
95 #define LINUX_TIOCM_DTR		0x0002
96 #define LINUX_TIOCM_RTS		0x0004
97 #define LINUX_TIOCM_ST		0x0008
98 #define LINUX_TIOCM_SR		0x0010
99 #define LINUX_TIOCM_CTS		0x0020
100 #define LINUX_TIOCM_CAR		0x0040
101 #define LINUX_TIOCM_RNG		0x0080
102 #define LINUX_TIOCM_DSR		0x0100
103 #define LINUX_TIOCM_CD		LINUX_TIOCM_CAR
104 #define LINUX_TIOCM_RI 		LINUX_TIOCM_RNG
105 
106 #define	LINUX_TCIFLUSH		0
107 #define	LINUX_TCOFLUSH		1
108 #define	LINUX_TCIOFLUSH		2
109 
110 #define	LINUX_TCOOFF		0
111 #define	LINUX_TCOON		1
112 #define	LINUX_TCIOFF		2
113 #define	LINUX_TCION		3
114 
115 #define	LINUX_TCSANOW		0
116 #define	LINUX_TCSADRAIN		1
117 #define	LINUX_TCSAFLUSH		2
118 
119 /* Linux line disciplines */
120 #define LINUX_N_TTY		0
121 #define LINUX_N_SLIP		1
122 #define LINUX_N_MOUSE		2
123 #define LINUX_N_PPP		3
124 #define	LINUX_N_STRIP		4
125 
126 /* currently unused: */
127 #define	LINUX_N_AX25		5
128 #define	LINUX_N_X25		6
129 #define	LINUX_N_6PACK		7
130 
131 /* values passed to TIOCLINUX ioctl */
132 #define LINUX_TIOCLINUX_COPY		 2
133 #define LINUX_TIOCLINUX_PASTE		 3
134 #define LINUX_TIOCLINUX_UNBLANK		 4
135 #define LINUX_TIOCLINUX_LOADLUT		 5
136 #define LINUX_TIOCLINUX_READSHIFT	 6
137 #define LINUX_TIOCLINUX_READMOUSE	 7
138 #define LINUX_TIOCLINUX_VESABLANK	10
139 #define LINUX_TIOCLINUX_KERNMSG		11
140 #define LINUX_TIOCLINUX_CURCONS		12
141 
142 static linux_speed_t linux_speeds[] = {
143 	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
144 	9600, 19200, 38400, 57600, 115200, 230400
145 };
146 
147 static const int linux_spmasks[] = {
148 	LINUX_B0, LINUX_B50, LINUX_B75, LINUX_B110, LINUX_B134, LINUX_B150,
149 	LINUX_B200, LINUX_B300, LINUX_B600, LINUX_B1200, LINUX_B1800,
150 	LINUX_B2400, LINUX_B4800, LINUX_B9600, LINUX_B19200, LINUX_B38400,
151 	LINUX_B57600, LINUX_B115200, LINUX_B230400
152 };
153 
154 #ifdef COMPAT_LINUX32
155 struct linux32_termio;
156 struct linux32_termios;
157 static void linux32_termio_to_bsd_termios(struct linux32_termio *,
158 					     struct termios *);
159 static void bsd_termios_to_linux32_termio(struct termios *,
160 					     struct linux32_termio *);
161 static void linux32_termios_to_bsd_termios(struct linux32_termios *,
162 					      struct termios *);
163 static void bsd_termios_to_linux32_termios(struct termios *,
164 					      struct linux32_termios *);
165 #else
166 struct linux_termio;
167 struct linux_termios;
168 static void linux_termio_to_bsd_termios(struct linux_termio *,
169 					     struct termios *);
170 static void bsd_termios_to_linux_termio(struct termios *,
171 					     struct linux_termio *);
172 static void linux_termios_to_bsd_termios(struct linux_termios *,
173 					      struct termios *);
174 static void bsd_termios_to_linux_termios(struct termios *,
175 					      struct linux_termios *);
176 #endif
177 
178 /*
179  * Deal with termio ioctl cruft. This doesn't look very good..
180  * XXX too much code duplication, obviously..
181  *
182  * The conversion routines between Linux and BSD structures assume
183  * that the fields are already filled with the current values,
184  * so that fields present in BSD but not in Linux keep their current
185  * values.
186  */
187 
188 static void
189 #ifdef COMPAT_LINUX32
linux32_termio_to_bsd_termios(struct linux32_termio * lt,struct termios * bts)190 linux32_termio_to_bsd_termios(struct linux32_termio *lt, struct termios *bts)
191 #else
192 linux_termio_to_bsd_termios(struct linux_termio *lt, struct termios *bts)
193 #endif
194 {
195 	int index;
196 
197 	bts->c_iflag = 0;
198 	bts->c_iflag |= cvtto_bsd_mask(lt->c_iflag, LINUX_IGNBRK, IGNBRK);
199 	bts->c_iflag |= cvtto_bsd_mask(lt->c_iflag, LINUX_BRKINT, BRKINT);
200 	bts->c_iflag |= cvtto_bsd_mask(lt->c_iflag, LINUX_IGNPAR, IGNPAR);
201 	bts->c_iflag |= cvtto_bsd_mask(lt->c_iflag, LINUX_INPCK, INPCK);
202 	bts->c_iflag |= cvtto_bsd_mask(lt->c_iflag, LINUX_ISTRIP, ISTRIP);
203 	bts->c_iflag |= cvtto_bsd_mask(lt->c_iflag, LINUX_INLCR, INLCR);
204 	bts->c_iflag |= cvtto_bsd_mask(lt->c_iflag, LINUX_IGNCR, IGNCR);
205 	bts->c_iflag |= cvtto_bsd_mask(lt->c_iflag, LINUX_ICRNL, ICRNL);
206 	bts->c_iflag |= cvtto_bsd_mask(lt->c_iflag, LINUX_IXON, IXON);
207 	bts->c_iflag |= cvtto_bsd_mask(lt->c_iflag, LINUX_IXANY, IXANY);
208 	bts->c_iflag |= cvtto_bsd_mask(lt->c_iflag, LINUX_IXOFF, IXOFF);
209 	bts->c_iflag |= cvtto_bsd_mask(lt->c_iflag, LINUX_IMAXBEL, IMAXBEL);
210 
211 	bts->c_oflag = 0;
212 	bts->c_oflag |= cvtto_bsd_mask(lt->c_oflag, LINUX_OPOST, OPOST);
213 	bts->c_oflag |= cvtto_bsd_mask(lt->c_oflag, LINUX_ONLCR, ONLCR);
214 	bts->c_oflag |= cvtto_bsd_mask(lt->c_oflag, LINUX_XTABS, OXTABS);
215 
216 	/*
217 	 * This could have been:
218 	 * bts->c_cflag = (lt->c_flag & LINUX_CSIZE) << 4
219 	 * But who knows, those values might perhaps change one day.
220 	 */
221 	switch (lt->c_cflag & LINUX_CSIZE) {
222 	case LINUX_CS5:
223 		bts->c_cflag = CS5;
224 		break;
225 	case LINUX_CS6:
226 		bts->c_cflag = CS6;
227 		break;
228 	case LINUX_CS7:
229 		bts->c_cflag = CS7;
230 		break;
231 	case LINUX_CS8:
232 		bts->c_cflag = CS8;
233 		break;
234 	}
235 	bts->c_cflag |= cvtto_bsd_mask(lt->c_cflag, LINUX_CSTOPB, CSTOPB);
236 	bts->c_cflag |= cvtto_bsd_mask(lt->c_cflag, LINUX_CREAD, CREAD);
237 	bts->c_cflag |= cvtto_bsd_mask(lt->c_cflag, LINUX_PARENB, PARENB);
238 	bts->c_cflag |= cvtto_bsd_mask(lt->c_cflag, LINUX_PARODD, PARODD);
239 	bts->c_cflag |= cvtto_bsd_mask(lt->c_cflag, LINUX_HUPCL, HUPCL);
240 	bts->c_cflag |= cvtto_bsd_mask(lt->c_cflag, LINUX_CLOCAL, CLOCAL);
241 	bts->c_cflag |= cvtto_bsd_mask(lt->c_cflag, LINUX_CRTSCTS, CRTSCTS);
242 
243 	bts->c_lflag = 0;
244 	bts->c_lflag |= cvtto_bsd_mask(lt->c_lflag, LINUX_ISIG, ISIG);
245 	bts->c_lflag |= cvtto_bsd_mask(lt->c_lflag, LINUX_ICANON, ICANON);
246 	bts->c_lflag |= cvtto_bsd_mask(lt->c_lflag, LINUX_ECHO, ECHO);
247 	bts->c_lflag |= cvtto_bsd_mask(lt->c_lflag, LINUX_ECHOE, ECHOE);
248 	bts->c_lflag |= cvtto_bsd_mask(lt->c_lflag, LINUX_ECHOK, ECHOK);
249 	bts->c_lflag |= cvtto_bsd_mask(lt->c_lflag, LINUX_ECHONL, ECHONL);
250 	bts->c_lflag |= cvtto_bsd_mask(lt->c_lflag, LINUX_NOFLSH, NOFLSH);
251 	bts->c_lflag |= cvtto_bsd_mask(lt->c_lflag, LINUX_TOSTOP, TOSTOP);
252 	bts->c_lflag |= cvtto_bsd_mask(lt->c_lflag, LINUX_ECHOCTL, ECHOCTL);
253 	bts->c_lflag |= cvtto_bsd_mask(lt->c_lflag, LINUX_ECHOPRT, ECHOPRT);
254 	bts->c_lflag |= cvtto_bsd_mask(lt->c_lflag, LINUX_ECHOKE, ECHOKE);
255 	bts->c_lflag |= cvtto_bsd_mask(lt->c_lflag, LINUX_FLUSHO, FLUSHO);
256 	bts->c_lflag |= cvtto_bsd_mask(lt->c_lflag, LINUX_PENDIN, PENDIN);
257 	bts->c_lflag |= cvtto_bsd_mask(lt->c_lflag, LINUX_IEXTEN, IEXTEN);
258 
259 	index = lt->c_cflag & LINUX_CBAUD;
260 	if (index & LINUX_CBAUDEX)
261 		index = (index & ~LINUX_CBAUDEX) + LINUX_NSPEEDS - 1;
262 	bts->c_ispeed = bts->c_ospeed = linux_speeds[index];
263 
264 	bts->c_cc[VINTR] = lt->c_cc[LINUX_OLD_VINTR];
265 	bts->c_cc[VQUIT] = lt->c_cc[LINUX_OLD_VQUIT];
266 	bts->c_cc[VERASE] = lt->c_cc[LINUX_OLD_VERASE];
267 	bts->c_cc[VKILL] = lt->c_cc[LINUX_OLD_VKILL];
268 #if LINUX_VEOF < LINUX_NCC
269 	bts->c_cc[VEOF] = lt->c_cc[LINUX_OLD_VEOF];
270 #endif
271 	bts->c_cc[VTIME] = lt->c_cc[LINUX_OLD_VTIME];
272 	bts->c_cc[VMIN] = lt->c_cc[LINUX_OLD_VMIN];
273 }
274 
275 static void
276 #ifdef COMPAT_LINUX32
bsd_termios_to_linux32_termio(struct termios * bts,struct linux32_termio * lt)277 bsd_termios_to_linux32_termio(struct termios *bts, struct linux32_termio *lt)
278 #else
279 bsd_termios_to_linux_termio(struct termios *bts, struct linux_termio *lt)
280 #endif
281 {
282 	int i, mask;
283 
284 	memset(lt, 0, sizeof(*lt));
285 	lt->c_iflag = 0;
286 	lt->c_iflag |= cvtto_linux_mask(bts->c_iflag, IGNBRK, LINUX_IGNBRK);
287 	lt->c_iflag |= cvtto_linux_mask(bts->c_iflag, BRKINT, LINUX_BRKINT);
288 	lt->c_iflag |= cvtto_linux_mask(bts->c_iflag, IGNPAR, LINUX_IGNPAR);
289 	lt->c_iflag |= cvtto_linux_mask(bts->c_iflag, INPCK, LINUX_INPCK);
290 	lt->c_iflag |= cvtto_linux_mask(bts->c_iflag, ISTRIP, LINUX_ISTRIP);
291 	lt->c_iflag |= cvtto_linux_mask(bts->c_iflag, INLCR, LINUX_INLCR);
292 	lt->c_iflag |= cvtto_linux_mask(bts->c_iflag, IGNCR, LINUX_IGNCR);
293 	lt->c_iflag |= cvtto_linux_mask(bts->c_iflag, ICRNL, LINUX_ICRNL);
294 	lt->c_iflag |= cvtto_linux_mask(bts->c_iflag, IXON, LINUX_IXON);
295 	lt->c_iflag |= cvtto_linux_mask(bts->c_iflag, IXANY, LINUX_IXANY);
296 	lt->c_iflag |= cvtto_linux_mask(bts->c_iflag, IXOFF, LINUX_IXOFF);
297 	lt->c_iflag |= cvtto_linux_mask(bts->c_iflag, IMAXBEL, LINUX_IMAXBEL);
298 
299 	lt->c_oflag = 0;
300 	lt->c_oflag |= cvtto_linux_mask(bts->c_oflag, OPOST, LINUX_OPOST);
301 	lt->c_oflag |= cvtto_linux_mask(bts->c_oflag, ONLCR, LINUX_ONLCR);
302 	lt->c_oflag |= cvtto_linux_mask(bts->c_oflag, OXTABS, LINUX_XTABS);
303 
304 	switch (bts->c_cflag & CSIZE) {
305 	case CS5:
306 		lt->c_cflag = LINUX_CS5;
307 		break;
308 	case CS6:
309 		lt->c_cflag = LINUX_CS6;
310 		break;
311 	case CS7:
312 		lt->c_cflag = LINUX_CS7;
313 		break;
314 	case CS8:
315 		lt->c_cflag = LINUX_CS8;
316 		break;
317 	}
318 	lt->c_cflag |= cvtto_linux_mask(bts->c_cflag, CSTOPB, LINUX_CSTOPB);
319 	lt->c_cflag |= cvtto_linux_mask(bts->c_cflag, CREAD, LINUX_CREAD);
320 	lt->c_cflag |= cvtto_linux_mask(bts->c_cflag, PARENB, LINUX_PARENB);
321 	lt->c_cflag |= cvtto_linux_mask(bts->c_cflag, PARODD, LINUX_PARODD);
322 	lt->c_cflag |= cvtto_linux_mask(bts->c_cflag, HUPCL, LINUX_HUPCL);
323 	lt->c_cflag |= cvtto_linux_mask(bts->c_cflag, CLOCAL, LINUX_CLOCAL);
324 	lt->c_cflag |= cvtto_linux_mask(bts->c_cflag, CRTSCTS, LINUX_CRTSCTS);
325 
326 	lt->c_lflag = 0;
327 	lt->c_lflag |= cvtto_linux_mask(bts->c_lflag, ISIG, LINUX_ISIG);
328 	lt->c_lflag |= cvtto_linux_mask(bts->c_lflag, ICANON, LINUX_ICANON);
329 	lt->c_lflag |= cvtto_linux_mask(bts->c_lflag, ECHO, LINUX_ECHO);
330 	lt->c_lflag |= cvtto_linux_mask(bts->c_lflag, ECHOE, LINUX_ECHOE);
331 	lt->c_lflag |= cvtto_linux_mask(bts->c_lflag, ECHOK, LINUX_ECHOK);
332 	lt->c_lflag |= cvtto_linux_mask(bts->c_lflag, ECHONL, LINUX_ECHONL);
333 	lt->c_lflag |= cvtto_linux_mask(bts->c_lflag, NOFLSH, LINUX_NOFLSH);
334 	lt->c_lflag |= cvtto_linux_mask(bts->c_lflag, TOSTOP, LINUX_TOSTOP);
335 	lt->c_lflag |= cvtto_linux_mask(bts->c_lflag, ECHOCTL, LINUX_ECHOCTL);
336 	lt->c_lflag |= cvtto_linux_mask(bts->c_lflag, ECHOPRT, LINUX_ECHOPRT);
337 	lt->c_lflag |= cvtto_linux_mask(bts->c_lflag, ECHOKE, LINUX_ECHOKE);
338 	lt->c_lflag |= cvtto_linux_mask(bts->c_lflag, FLUSHO, LINUX_FLUSHO);
339 	lt->c_lflag |= cvtto_linux_mask(bts->c_lflag, PENDIN, LINUX_PENDIN);
340 	lt->c_lflag |= cvtto_linux_mask(bts->c_lflag, IEXTEN, LINUX_IEXTEN);
341 
342 	mask = LINUX_B9600;	/* XXX default value should this be 0? */
343 	for (i = 0; i < sizeof (linux_speeds) / sizeof (linux_speed_t); i++) {
344 		if (bts->c_ospeed == linux_speeds[i]) {
345 			mask = linux_spmasks[i];
346 			break;
347 		}
348 	}
349 	lt->c_cflag |= mask;
350 
351 	lt->c_cc[LINUX_OLD_VINTR] = bts->c_cc[VINTR];
352 	lt->c_cc[LINUX_OLD_VQUIT] = bts->c_cc[VQUIT];
353 	lt->c_cc[LINUX_OLD_VERASE] = bts->c_cc[VERASE];
354 	lt->c_cc[LINUX_OLD_VKILL] = bts->c_cc[VKILL];
355 #if LINUX_OLD_VEOF < LINUX_NCC
356 	lt->c_cc[LINUX_OLD_VEOF] = bts->c_cc[VEOF];
357 #endif
358 	lt->c_cc[LINUX_OLD_VTIME] = bts->c_cc[VTIME];
359 	lt->c_cc[LINUX_OLD_VMIN] = bts->c_cc[VMIN];
360 	lt->c_cc[LINUX_OLD_VSWTC] = 0;
361 
362 	/* XXX should be fixed someday */
363 	lt->c_line = 0;
364 }
365 
366 static void
367 #ifdef COMPAT_LINUX32
linux32_termios_to_bsd_termios(struct linux32_termios * lts,struct termios * bts)368 linux32_termios_to_bsd_termios(struct linux32_termios *lts, struct termios *bts)
369 #else
370 linux_termios_to_bsd_termios(struct linux_termios *lts, struct termios *bts)
371 #endif
372 {
373 	int index;
374 
375 	bts->c_iflag = 0;
376 	bts->c_iflag |= cvtto_bsd_mask(lts->c_iflag, LINUX_IGNBRK, IGNBRK);
377 	bts->c_iflag |= cvtto_bsd_mask(lts->c_iflag, LINUX_BRKINT, BRKINT);
378 	bts->c_iflag |= cvtto_bsd_mask(lts->c_iflag, LINUX_IGNPAR, IGNPAR);
379 	bts->c_iflag |= cvtto_bsd_mask(lts->c_iflag, LINUX_INPCK, INPCK);
380 	bts->c_iflag |= cvtto_bsd_mask(lts->c_iflag, LINUX_ISTRIP, ISTRIP);
381 	bts->c_iflag |= cvtto_bsd_mask(lts->c_iflag, LINUX_INLCR, INLCR);
382 	bts->c_iflag |= cvtto_bsd_mask(lts->c_iflag, LINUX_IGNCR, IGNCR);
383 	bts->c_iflag |= cvtto_bsd_mask(lts->c_iflag, LINUX_ICRNL, ICRNL);
384 	bts->c_iflag |= cvtto_bsd_mask(lts->c_iflag, LINUX_IXON, IXON);
385 	bts->c_iflag |= cvtto_bsd_mask(lts->c_iflag, LINUX_IXANY, IXANY);
386 	bts->c_iflag |= cvtto_bsd_mask(lts->c_iflag, LINUX_IXOFF, IXOFF);
387 	bts->c_iflag |= cvtto_bsd_mask(lts->c_iflag, LINUX_IMAXBEL, IMAXBEL);
388 
389 	bts->c_oflag = 0;
390 	bts->c_oflag |= cvtto_bsd_mask(lts->c_oflag, LINUX_OPOST, OPOST);
391 	bts->c_oflag |= cvtto_bsd_mask(lts->c_oflag, LINUX_ONLCR, ONLCR);
392 	bts->c_oflag |= cvtto_bsd_mask(lts->c_oflag, LINUX_XTABS, OXTABS);
393 
394 	bts->c_cflag = 0;
395 	switch (lts->c_cflag & LINUX_CSIZE) {
396 	case LINUX_CS5:
397 		bts->c_cflag = CS5;
398 		break;
399 	case LINUX_CS6:
400 		bts->c_cflag = CS6;
401 		break;
402 	case LINUX_CS7:
403 		bts->c_cflag = CS7;
404 		break;
405 	case LINUX_CS8:
406 		bts->c_cflag = CS8;
407 		break;
408 	}
409 	bts->c_cflag |= cvtto_bsd_mask(lts->c_cflag, LINUX_CSTOPB, CSTOPB);
410 	bts->c_cflag |= cvtto_bsd_mask(lts->c_cflag, LINUX_CREAD, CREAD);
411 	bts->c_cflag |= cvtto_bsd_mask(lts->c_cflag, LINUX_PARENB, PARENB);
412 	bts->c_cflag |= cvtto_bsd_mask(lts->c_cflag, LINUX_PARODD, PARODD);
413 	bts->c_cflag |= cvtto_bsd_mask(lts->c_cflag, LINUX_HUPCL, HUPCL);
414 	bts->c_cflag |= cvtto_bsd_mask(lts->c_cflag, LINUX_CLOCAL, CLOCAL);
415 	bts->c_cflag |= cvtto_bsd_mask(lts->c_cflag, LINUX_CRTSCTS, CRTSCTS);
416 
417 	bts->c_lflag = 0;
418 	bts->c_lflag |= cvtto_bsd_mask(lts->c_lflag, LINUX_ISIG, ISIG);
419 	bts->c_lflag |= cvtto_bsd_mask(lts->c_lflag, LINUX_ICANON, ICANON);
420 	bts->c_lflag |= cvtto_bsd_mask(lts->c_lflag, LINUX_ECHO, ECHO);
421 	bts->c_lflag |= cvtto_bsd_mask(lts->c_lflag, LINUX_ECHOE, ECHOE);
422 	bts->c_lflag |= cvtto_bsd_mask(lts->c_lflag, LINUX_ECHOK, ECHOK);
423 	bts->c_lflag |= cvtto_bsd_mask(lts->c_lflag, LINUX_ECHONL, ECHONL);
424 	bts->c_lflag |= cvtto_bsd_mask(lts->c_lflag, LINUX_NOFLSH, NOFLSH);
425 	bts->c_lflag |= cvtto_bsd_mask(lts->c_lflag, LINUX_TOSTOP, TOSTOP);
426 	bts->c_lflag |= cvtto_bsd_mask(lts->c_lflag, LINUX_ECHOCTL, ECHOCTL);
427 	bts->c_lflag |= cvtto_bsd_mask(lts->c_lflag, LINUX_ECHOPRT, ECHOPRT);
428 	bts->c_lflag |= cvtto_bsd_mask(lts->c_lflag, LINUX_ECHOKE, ECHOKE);
429 	bts->c_lflag |= cvtto_bsd_mask(lts->c_lflag, LINUX_FLUSHO, FLUSHO);
430 	bts->c_lflag |= cvtto_bsd_mask(lts->c_lflag, LINUX_PENDIN, PENDIN);
431 	bts->c_lflag |= cvtto_bsd_mask(lts->c_lflag, LINUX_IEXTEN, IEXTEN);
432 
433 	index = lts->c_cflag & LINUX_CBAUD;
434 	if (index & LINUX_CBAUDEX)
435 		index = (index & ~LINUX_CBAUDEX) + LINUX_NSPEEDS - 1;
436 	bts->c_ispeed = bts->c_ospeed = linux_speeds[index];
437 	/*
438 	 * A null c_ospeed causes NetBSD to hangup the terminal.
439 	 * Linux does not do this, and it sets c_ospeed to zero
440 	 * sometimes. If it is null, we store -1 in the kernel
441 	 */
442 	if (bts->c_ospeed == 0)
443 		bts->c_ospeed = -1;
444 
445 	bts->c_cc[VINTR] = lts->c_cc[LINUX_VINTR];
446 	bts->c_cc[VQUIT] = lts->c_cc[LINUX_VQUIT];
447 	bts->c_cc[VERASE] = lts->c_cc[LINUX_VERASE];
448 	bts->c_cc[VKILL] = lts->c_cc[LINUX_VKILL];
449 	bts->c_cc[VEOF] = lts->c_cc[LINUX_VEOF];
450 	bts->c_cc[VTIME] = lts->c_cc[LINUX_VTIME];
451 	bts->c_cc[VMIN] = lts->c_cc[LINUX_VMIN];
452 	bts->c_cc[VEOL] = lts->c_cc[LINUX_VEOL];
453 	bts->c_cc[VEOL2] = lts->c_cc[LINUX_VEOL2];
454 	bts->c_cc[VWERASE] = lts->c_cc[LINUX_VWERASE];
455 	bts->c_cc[VSUSP] = lts->c_cc[LINUX_VSUSP];
456 	bts->c_cc[VSTART] = lts->c_cc[LINUX_VSTART];
457 	bts->c_cc[VSTOP] = lts->c_cc[LINUX_VSTOP];
458 	bts->c_cc[VLNEXT] = lts->c_cc[LINUX_VLNEXT];
459 	bts->c_cc[VDISCARD] = lts->c_cc[LINUX_VDISCARD];
460 	bts->c_cc[VREPRINT] = lts->c_cc[LINUX_VREPRINT];
461 }
462 
463 static void
464 #ifdef COMPAT_LINUX32
bsd_termios_to_linux32_termios(struct termios * bts,struct linux32_termios * lts)465 bsd_termios_to_linux32_termios(struct termios *bts, struct linux32_termios *lts)
466 #else
467 bsd_termios_to_linux_termios(struct termios *bts, struct linux_termios *lts)
468 #endif
469 {
470 	int i, mask;
471 
472 	memset(lts, 0, sizeof(*lts));
473 	lts->c_iflag = 0;
474 	lts->c_iflag |= cvtto_linux_mask(bts->c_iflag, IGNBRK, LINUX_IGNBRK);
475 	lts->c_iflag |= cvtto_linux_mask(bts->c_iflag, BRKINT, LINUX_BRKINT);
476 	lts->c_iflag |= cvtto_linux_mask(bts->c_iflag, IGNPAR, LINUX_IGNPAR);
477 	lts->c_iflag |= cvtto_linux_mask(bts->c_iflag, INPCK, LINUX_INPCK);
478 	lts->c_iflag |= cvtto_linux_mask(bts->c_iflag, ISTRIP, LINUX_ISTRIP);
479 	lts->c_iflag |= cvtto_linux_mask(bts->c_iflag, INLCR, LINUX_INLCR);
480 	lts->c_iflag |= cvtto_linux_mask(bts->c_iflag, IGNCR, LINUX_IGNCR);
481 	lts->c_iflag |= cvtto_linux_mask(bts->c_iflag, ICRNL, LINUX_ICRNL);
482 	lts->c_iflag |= cvtto_linux_mask(bts->c_iflag, IXON, LINUX_IXON);
483 	lts->c_iflag |= cvtto_linux_mask(bts->c_iflag, IXANY, LINUX_IXANY);
484 	lts->c_iflag |= cvtto_linux_mask(bts->c_iflag, IXOFF, LINUX_IXOFF);
485 	lts->c_iflag |= cvtto_linux_mask(bts->c_iflag, IMAXBEL, LINUX_IMAXBEL);
486 
487 	lts->c_oflag = 0;
488 	lts->c_oflag |= cvtto_linux_mask(bts->c_oflag, OPOST, LINUX_OPOST);
489 	lts->c_oflag |= cvtto_linux_mask(bts->c_oflag, ONLCR, LINUX_ONLCR);
490 	lts->c_oflag |= cvtto_linux_mask(bts->c_oflag, OXTABS, LINUX_XTABS);
491 
492 	switch (bts->c_cflag & CSIZE) {
493 	case CS5:
494 		lts->c_cflag = LINUX_CS5;
495 		break;
496 	case CS6:
497 		lts->c_cflag = LINUX_CS6;
498 		break;
499 	case CS7:
500 		lts->c_cflag = LINUX_CS7;
501 		break;
502 	case CS8:
503 		lts->c_cflag = LINUX_CS8;
504 		break;
505 	}
506 	lts->c_cflag |= cvtto_linux_mask(bts->c_cflag, CS5, LINUX_CS5);
507 	lts->c_cflag |= cvtto_linux_mask(bts->c_cflag, CS6, LINUX_CS6);
508 	lts->c_cflag |= cvtto_linux_mask(bts->c_cflag, CS7, LINUX_CS7);
509 	lts->c_cflag |= cvtto_linux_mask(bts->c_cflag, CS8, LINUX_CS8);
510 	lts->c_cflag |= cvtto_linux_mask(bts->c_cflag, CSTOPB, LINUX_CSTOPB);
511 	lts->c_cflag |= cvtto_linux_mask(bts->c_cflag, CREAD, LINUX_CREAD);
512 	lts->c_cflag |= cvtto_linux_mask(bts->c_cflag, PARENB, LINUX_PARENB);
513 	lts->c_cflag |= cvtto_linux_mask(bts->c_cflag, PARODD, LINUX_PARODD);
514 	lts->c_cflag |= cvtto_linux_mask(bts->c_cflag, HUPCL, LINUX_HUPCL);
515 	lts->c_cflag |= cvtto_linux_mask(bts->c_cflag, CLOCAL, LINUX_CLOCAL);
516 	lts->c_cflag |= cvtto_linux_mask(bts->c_cflag, CRTSCTS, LINUX_CRTSCTS);
517 
518 	lts->c_lflag = 0;
519 	lts->c_lflag |= cvtto_linux_mask(bts->c_lflag, ISIG, LINUX_ISIG);
520 	lts->c_lflag |= cvtto_linux_mask(bts->c_lflag, ICANON, LINUX_ICANON);
521 	lts->c_lflag |= cvtto_linux_mask(bts->c_lflag, ECHO, LINUX_ECHO);
522 	lts->c_lflag |= cvtto_linux_mask(bts->c_lflag, ECHOE, LINUX_ECHOE);
523 	lts->c_lflag |= cvtto_linux_mask(bts->c_lflag, ECHOK, LINUX_ECHOK);
524 	lts->c_lflag |= cvtto_linux_mask(bts->c_lflag, ECHONL, LINUX_ECHONL);
525 	lts->c_lflag |= cvtto_linux_mask(bts->c_lflag, NOFLSH, LINUX_NOFLSH);
526 	lts->c_lflag |= cvtto_linux_mask(bts->c_lflag, TOSTOP, LINUX_TOSTOP);
527 	lts->c_lflag |= cvtto_linux_mask(bts->c_lflag, ECHOCTL, LINUX_ECHOCTL);
528 	lts->c_lflag |= cvtto_linux_mask(bts->c_lflag, ECHOPRT, LINUX_ECHOPRT);
529 	lts->c_lflag |= cvtto_linux_mask(bts->c_lflag, ECHOKE, LINUX_ECHOKE);
530 	lts->c_lflag |= cvtto_linux_mask(bts->c_lflag, FLUSHO, LINUX_FLUSHO);
531 	lts->c_lflag |= cvtto_linux_mask(bts->c_lflag, PENDIN, LINUX_PENDIN);
532 	lts->c_lflag |= cvtto_linux_mask(bts->c_lflag, IEXTEN, LINUX_IEXTEN);
533 
534 	mask = LINUX_B9600;	/* XXX default value */
535 	for (i = 0; i < sizeof (linux_speeds) / sizeof (linux_speed_t); i++) {
536 		if (bts->c_ospeed == linux_speeds[i]) {
537 			mask = linux_spmasks[i];
538 			break;
539 		}
540 	}
541 	/*
542 	 * A null c_ospeed causes NetBSD to hangup the terminal.
543 	 * Linux does not do this, and it sets c_ospeed to zero
544 	 * sometimes. If it is null, we store -1 in the kernel
545 	 */
546 	if (bts->c_ospeed == -1)
547 		bts->c_ospeed = 0;
548 	lts->c_cflag |= mask;
549 
550 	lts->c_cc[LINUX_VINTR] = bts->c_cc[VINTR];
551 	lts->c_cc[LINUX_VQUIT] = bts->c_cc[VQUIT];
552 	lts->c_cc[LINUX_VERASE] = bts->c_cc[VERASE];
553 	lts->c_cc[LINUX_VKILL] = bts->c_cc[VKILL];
554 	lts->c_cc[LINUX_VEOF] = bts->c_cc[VEOF];
555 	lts->c_cc[LINUX_VTIME] = bts->c_cc[VTIME];
556 	lts->c_cc[LINUX_VMIN] = bts->c_cc[VMIN];
557 	lts->c_cc[LINUX_VEOL] = bts->c_cc[VEOL];
558 	lts->c_cc[LINUX_VEOL2] = bts->c_cc[VEOL2];
559 	lts->c_cc[LINUX_VWERASE] = bts->c_cc[VWERASE];
560 	lts->c_cc[LINUX_VSUSP] = bts->c_cc[VSUSP];
561 	lts->c_cc[LINUX_VSTART] = bts->c_cc[VSTART];
562 	lts->c_cc[LINUX_VSTOP] = bts->c_cc[VSTOP];
563 	lts->c_cc[LINUX_VLNEXT] = bts->c_cc[VLNEXT];
564 	lts->c_cc[LINUX_VDISCARD] = bts->c_cc[VDISCARD];
565 	lts->c_cc[LINUX_VREPRINT] = bts->c_cc[VREPRINT];
566 	lts->c_cc[LINUX_VSWTC] = 0;
567 
568 	/* XXX should be fixed someday */
569 	lts->c_line = 0;
570 }
571 #endif /* !_LINUX_TERMIOS_H */
572