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