xref: /freebsd/sys/kern/tty_compat.c (revision 48991a368427cadb9cdac39581d1676c29619c52)
1 /*-
2  * Copyright (c) 1982, 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)tty_compat.c	8.1 (Berkeley) 6/10/93
34  * $Id: tty_compat.c,v 1.17 1995/08/02 12:53:14 ache Exp $
35  */
36 
37 /*
38  * mapping routines for old line discipline (yuck)
39  */
40 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/ioctl.h>
45 #include <sys/proc.h>
46 #include <sys/tty.h>
47 #include <sys/termios.h>
48 #include <sys/file.h>
49 #include <sys/conf.h>
50 #include <sys/kernel.h>
51 #include <sys/syslog.h>
52 
53 static int ttcompatgetflags	__P((struct tty	*tp));
54 static void ttcompatsetflags	__P((struct tty	*tp, struct termios *t));
55 static void ttcompatsetlflags	__P((struct tty	*tp, struct termios *t));
56 static int ttcompatspeedtab	__P((int speed, struct speedtab *table));
57 
58 int ttydebug = 0;
59 
60 static struct speedtab compatspeeds[] = {
61 #define MAX_SPEED	17
62 	{ 115200, 17 },
63 	{ 57600, 16 },
64 	{ 38400, 15 },
65 	{ 19200, 14 },
66 	{ 9600,	13 },
67 	{ 4800,	12 },
68 	{ 2400,	11 },
69 	{ 1800,	10 },
70 	{ 1200,	9 },
71 	{ 600,	8 },
72 	{ 300,	7 },
73 	{ 200,	6 },
74 	{ 150,	5 },
75 	{ 134,	4 },
76 	{ 110,	3 },
77 	{ 75,	2 },
78 	{ 50,	1 },
79 	{ 0,	0 },
80 	{ -1,	-1 },
81 };
82 static int compatspcodes[] = {
83 	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
84 	1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200,
85 };
86 
87 static int
88 ttcompatspeedtab(speed, table)
89 	int speed;
90 	register struct speedtab *table;
91 {
92 	if (speed == 0)
93 		return (0); /* hangup */
94 	for ( ; table->sp_speed > 0; table++)
95 		if (table->sp_speed <= speed) /* nearest one, rounded down */
96 			return (table->sp_code);
97 	return (1); /* 50, min and not hangup */
98 }
99 
100 int
101 ttsetcompat(tp, com, data, term)
102 	register struct tty *tp;
103 	int *com;
104 	caddr_t data;
105 	struct termios *term;
106 {
107 	switch (*com) {
108 	case TIOCSETP:
109 	case TIOCSETN: {
110 		register struct sgttyb *sg = (struct sgttyb *)data;
111 		int speed;
112 
113 		if ((speed = sg->sg_ispeed) > MAX_SPEED || speed < 0)
114 			return(EINVAL);
115 		else if (speed != ttcompatspeedtab(tp->t_ispeed, compatspeeds))
116 			term->c_ispeed = compatspcodes[speed];
117 		else
118 			term->c_ispeed = tp->t_ispeed;
119 		if ((speed = sg->sg_ospeed) > MAX_SPEED || speed < 0)
120 			return(EINVAL);
121 		else if (speed != ttcompatspeedtab(tp->t_ospeed, compatspeeds))
122 			term->c_ospeed = compatspcodes[speed];
123 		else
124 			term->c_ospeed = tp->t_ospeed;
125 		term->c_cc[VERASE] = sg->sg_erase;
126 		term->c_cc[VKILL] = sg->sg_kill;
127 		tp->t_flags = (tp->t_flags&0xffff0000) | (sg->sg_flags&0xffff);
128 		ttcompatsetflags(tp, term);
129 		*com = (*com == TIOCSETP) ? TIOCSETAF : TIOCSETA;
130 		break;
131 	}
132 	case TIOCSETC: {
133 		struct tchars *tc = (struct tchars *)data;
134 		register cc_t *cc;
135 
136 		cc = term->c_cc;
137 		cc[VINTR] = tc->t_intrc;
138 		cc[VQUIT] = tc->t_quitc;
139 		cc[VSTART] = tc->t_startc;
140 		cc[VSTOP] = tc->t_stopc;
141 		cc[VEOF] = tc->t_eofc;
142 		cc[VEOL] = tc->t_brkc;
143 		if (tc->t_brkc == -1)
144 			cc[VEOL2] = _POSIX_VDISABLE;
145 		*com = TIOCSETA;
146 		break;
147 	}
148 	case TIOCSLTC: {
149 		struct ltchars *ltc = (struct ltchars *)data;
150 		register cc_t *cc;
151 
152 		cc = term->c_cc;
153 		cc[VSUSP] = ltc->t_suspc;
154 		cc[VDSUSP] = ltc->t_dsuspc;
155 		cc[VREPRINT] = ltc->t_rprntc;
156 		cc[VDISCARD] = ltc->t_flushc;
157 		cc[VWERASE] = ltc->t_werasc;
158 		cc[VLNEXT] = ltc->t_lnextc;
159 		*com = TIOCSETA;
160 		break;
161 	}
162 	case TIOCLBIS:
163 	case TIOCLBIC:
164 	case TIOCLSET:
165 		if (*com == TIOCLSET)
166 			tp->t_flags = (tp->t_flags&0xffff) | *(int *)data<<16;
167 		else {
168 			tp->t_flags =
169 			 (ttcompatgetflags(tp)&0xffff0000)|(tp->t_flags&0xffff);
170 			if (*com == TIOCLBIS)
171 				tp->t_flags |= *(int *)data<<16;
172 			else
173 				tp->t_flags &= ~(*(int *)data<<16);
174 		}
175 		ttcompatsetlflags(tp, term);
176 		*com = TIOCSETA;
177 		break;
178 	}
179 	return 0;
180 }
181 
182 /*ARGSUSED*/
183 int
184 ttcompat(tp, com, data, flag)
185 	register struct tty *tp;
186 	int com;
187 	caddr_t data;
188 	int flag;
189 {
190 	switch (com) {
191 	case TIOCSETP:
192 	case TIOCSETN:
193 	case TIOCSETC:
194 	case TIOCSLTC:
195 	case TIOCLBIS:
196 	case TIOCLBIC:
197 	case TIOCLSET: {
198 		struct termios term;
199 		int error;
200 
201 		term = tp->t_termios;
202 		if ((error = ttsetcompat(tp, &com, data, &term)) != 0)
203 			return error;
204 		return ttioctl(tp, com, &term, flag);
205 	}
206 	case TIOCGETP: {
207 		register struct sgttyb *sg = (struct sgttyb *)data;
208 		register cc_t *cc = tp->t_cc;
209 
210 		sg->sg_ospeed = ttcompatspeedtab(tp->t_ospeed, compatspeeds);
211 		if (tp->t_ispeed == 0)
212 			sg->sg_ispeed = sg->sg_ospeed;
213 		else
214 			sg->sg_ispeed = ttcompatspeedtab(tp->t_ispeed, compatspeeds);
215 		sg->sg_erase = cc[VERASE];
216 		sg->sg_kill = cc[VKILL];
217 		sg->sg_flags = tp->t_flags = ttcompatgetflags(tp);
218 		break;
219 	}
220 	case TIOCGETC: {
221 		struct tchars *tc = (struct tchars *)data;
222 		register cc_t *cc = tp->t_cc;
223 
224 		tc->t_intrc = cc[VINTR];
225 		tc->t_quitc = cc[VQUIT];
226 		tc->t_startc = cc[VSTART];
227 		tc->t_stopc = cc[VSTOP];
228 		tc->t_eofc = cc[VEOF];
229 		tc->t_brkc = cc[VEOL];
230 		break;
231 	}
232 	case TIOCGLTC: {
233 		struct ltchars *ltc = (struct ltchars *)data;
234 		register cc_t *cc = tp->t_cc;
235 
236 		ltc->t_suspc = cc[VSUSP];
237 		ltc->t_dsuspc = cc[VDSUSP];
238 		ltc->t_rprntc = cc[VREPRINT];
239 		ltc->t_flushc = cc[VDISCARD];
240 		ltc->t_werasc = cc[VWERASE];
241 		ltc->t_lnextc = cc[VLNEXT];
242 		break;
243 	}
244 	case TIOCLGET:
245 		tp->t_flags =
246 		 (ttcompatgetflags(tp) & 0xffff0000UL)
247 		   | (tp->t_flags & 0xffff);
248 		*(int *)data = tp->t_flags>>16;
249 		if (ttydebug)
250 			printf("CLGET: returning %x\n", *(int *)data);
251 		break;
252 
253 	case OTIOCGETD:
254 		*(int *)data = tp->t_line ? tp->t_line : 2;
255 		break;
256 
257 	case OTIOCSETD: {
258 		int ldisczero = 0;
259 
260 		return (ttioctl(tp, TIOCSETD,
261 			*(int *)data == 2 ? (caddr_t)&ldisczero : data, flag));
262 	    }
263 
264 	case OTIOCCONS:
265 		*(int *)data = 1;
266 		return (ttioctl(tp, TIOCCONS, data, flag));
267 
268 	default:
269 		return (-1);
270 	}
271 	return (0);
272 }
273 
274 static int
275 ttcompatgetflags(tp)
276 	register struct tty *tp;
277 {
278 	register tcflag_t iflag	= tp->t_iflag;
279 	register tcflag_t lflag	= tp->t_lflag;
280 	register tcflag_t oflag	= tp->t_oflag;
281 	register tcflag_t cflag	= tp->t_cflag;
282 	register flags = 0;
283 
284 	if (iflag&IXOFF)
285 		flags |= TANDEM;
286 	if (iflag&ICRNL || oflag&ONLCR)
287 		flags |= CRMOD;
288 	if ((cflag&CSIZE) == CS8) {
289 		flags |= PASS8;
290 		if (iflag&ISTRIP)
291 			flags |= ANYP;
292 	}
293 	else if (cflag&PARENB) {
294 		if (iflag&INPCK) {
295 			if (cflag&PARODD)
296 				flags |= ODDP;
297 			else
298 				flags |= EVENP;
299 		} else
300 			flags |= EVENP | ODDP;
301 	}
302 
303 	if ((lflag&ICANON) == 0) {
304 		/* fudge */
305 		if (iflag&(INPCK|ISTRIP|IXON) || lflag&(IEXTEN|ISIG)
306 		    || cflag&(CSIZE|PARENB) != CS8)
307 			flags |= CBREAK;
308 		else
309 			flags |= RAW;
310 	}
311 	if (!(flags&RAW) && !(oflag&OPOST) && cflag&(CSIZE|PARENB) == CS8)
312 		flags |= LITOUT;
313 	if (cflag&MDMBUF)
314 		flags |= MDMBUF;
315 	if ((cflag&HUPCL) == 0)
316 		flags |= NOHANG;
317 	if (oflag&OXTABS)
318 		flags |= XTABS;
319 	if (lflag&ECHOE)
320 		flags |= CRTERA|CRTBS;
321 	if (lflag&ECHOKE)
322 		flags |= CRTKIL|CRTBS;
323 	if (lflag&ECHOPRT)
324 		flags |= PRTERA;
325 	if (lflag&ECHOCTL)
326 		flags |= CTLECH;
327 	if ((iflag&IXANY) == 0)
328 		flags |= DECCTQ;
329 	flags |= lflag&(ECHO|TOSTOP|FLUSHO|PENDIN|NOFLSH);
330 	if (ttydebug)
331 		printf("getflags: %x\n", flags);
332 	return (flags);
333 }
334 
335 static void
336 ttcompatsetflags(tp, t)
337 	register struct tty *tp;
338 	register struct termios *t;
339 {
340 	register flags = tp->t_flags;
341 	register tcflag_t iflag	= t->c_iflag;
342 	register tcflag_t oflag	= t->c_oflag;
343 	register tcflag_t lflag	= t->c_lflag;
344 	register tcflag_t cflag	= t->c_cflag;
345 
346 	if (flags & RAW) {
347 		iflag = IGNBRK;
348 		lflag &= ~(ECHOCTL|ISIG|ICANON|IEXTEN);
349 	} else {
350 		iflag &= ~(PARMRK|IGNPAR|IGNCR|INLCR);
351 		iflag |= BRKINT|IXON|IMAXBEL;
352 		lflag |= ISIG|IEXTEN|ECHOCTL;	/* XXX was echoctl on ? */
353 		if (flags & XTABS)
354 			oflag |= OXTABS;
355 		else
356 			oflag &= ~OXTABS;
357 		if (flags & CBREAK)
358 			lflag &= ~ICANON;
359 		else
360 			lflag |= ICANON;
361 		if (flags&CRMOD) {
362 			iflag |= ICRNL;
363 			oflag |= ONLCR;
364 		} else {
365 			iflag &= ~ICRNL;
366 			oflag &= ~ONLCR;
367 		}
368 	}
369 	if (flags&ECHO)
370 		lflag |= ECHO;
371 	else
372 		lflag &= ~ECHO;
373 
374 	cflag &= ~(CSIZE|PARENB);
375 	if (flags&(RAW|LITOUT|PASS8)) {
376 		cflag |= CS8;
377 		if (!(flags&(RAW|PASS8))
378 		    || (flags&(RAW|PASS8|ANYP)) == (PASS8|ANYP))
379 			iflag |= ISTRIP;
380 		else
381 			iflag &= ~ISTRIP;
382 		if (flags&(RAW|LITOUT))
383 			oflag &= ~OPOST;
384 		else
385 			oflag |= OPOST;
386 	} else {
387 		cflag |= CS7|PARENB;
388 		iflag |= ISTRIP;
389 		oflag |= OPOST;
390 	}
391 	/* XXX don't set INPCK if RAW or PASS8? */
392 	if ((flags&(EVENP|ODDP)) == EVENP) {
393 		iflag |= INPCK;
394 		cflag &= ~PARODD;
395 	} else if ((flags&(EVENP|ODDP)) == ODDP) {
396 		iflag |= INPCK;
397 		cflag |= PARODD;
398 	} else
399 		iflag &= ~INPCK;
400 	if (flags&TANDEM)
401 		iflag |= IXOFF;
402 	else
403 		iflag &= ~IXOFF;
404 	if ((flags&DECCTQ) == 0)
405 		iflag |= IXANY;
406 	else
407 		iflag &= ~IXANY;
408 	t->c_iflag = iflag;
409 	t->c_oflag = oflag;
410 	t->c_lflag = lflag;
411 	t->c_cflag = cflag;
412 }
413 
414 static void
415 ttcompatsetlflags(tp, t)
416 	register struct tty *tp;
417 	register struct termios *t;
418 {
419 	register flags = tp->t_flags;
420 	register tcflag_t iflag	= t->c_iflag;
421 	register tcflag_t oflag	= t->c_oflag;
422 	register tcflag_t lflag	= t->c_lflag;
423 	register tcflag_t cflag	= t->c_cflag;
424 
425 	iflag &= ~(PARMRK|IGNPAR|IGNCR|INLCR);
426 	if (flags&CRTERA)
427 		lflag |= ECHOE;
428 	else
429 		lflag &= ~ECHOE;
430 	if (flags&CRTKIL)
431 		lflag |= ECHOKE;
432 	else
433 		lflag &= ~ECHOKE;
434 	if (flags&PRTERA)
435 		lflag |= ECHOPRT;
436 	else
437 		lflag &= ~ECHOPRT;
438 	if (flags&CTLECH)
439 		lflag |= ECHOCTL;
440 	else
441 		lflag &= ~ECHOCTL;
442 	if (flags&TANDEM)
443 		iflag |= IXOFF;
444 	else
445 		iflag &= ~IXOFF;
446 	if ((flags&DECCTQ) == 0)
447 		iflag |= IXANY;
448 	else
449 		iflag &= ~IXANY;
450 	if (flags & MDMBUF)
451 		cflag |= MDMBUF;
452 	else
453 		cflag &= ~MDMBUF;
454 	if (flags&NOHANG)
455 		cflag &= ~HUPCL;
456 	else
457 		cflag |= HUPCL;
458 	lflag &= ~(TOSTOP|FLUSHO|PENDIN|NOFLSH);
459 	lflag |= flags&(TOSTOP|FLUSHO|PENDIN|NOFLSH);
460 
461 	/*
462 	 * The next if-else statement is copied from above so don't bother
463 	 * checking it separately.  We could avoid fiddlling with the
464 	 * character size if the mode is already RAW or if neither the
465 	 * LITOUT bit or the PASS8 bit is being changed, but the delta of
466 	 * the change is not available here and skipping the RAW case would
467 	 * make the code different from above.
468 	 */
469 	cflag &= ~(CSIZE|PARENB);
470 	if (flags&(RAW|LITOUT|PASS8)) {
471 		cflag |= CS8;
472 		if (!(flags&(RAW|PASS8))
473 		    || (flags&(RAW|PASS8|ANYP)) == (PASS8|ANYP))
474 			iflag |= ISTRIP;
475 		else
476 			iflag &= ~ISTRIP;
477 		if (flags&(RAW|LITOUT))
478 			oflag &= ~OPOST;
479 		else
480 			oflag |= OPOST;
481 	} else {
482 		cflag |= CS7|PARENB;
483 		iflag |= ISTRIP;
484 		oflag |= OPOST;
485 	}
486 	t->c_iflag = iflag;
487 	t->c_oflag = oflag;
488 	t->c_lflag = lflag;
489 	t->c_cflag = cflag;
490 }
491 #endif	/* COMPAT_43 || COMPAT_SUNOS */
492