2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)tty_compat.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/kern/tty_compat.c,v 1.29 1999/08/28 00:46:20 peter Exp $
38 * mapping routines for old line discipline (yuck)
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/ioctl_compat.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
48 static int ttcompatgetflags (struct tty *tp);
49 static void ttcompatsetflags (struct tty *tp, struct termios *t);
50 static void ttcompatsetlflags (struct tty *tp, struct termios *t);
51 static int ttcompatspeedtab (int speed, struct speedtab *table);
53 static int ttydebug = 0;
54 SYSCTL_INT(_debug, OID_AUTO, ttydebug, CTLFLAG_RW, &ttydebug, 0,
57 static struct speedtab compatspeeds[] = {
79 static int compatspcodes[] = {
80 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
81 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200,
85 ttcompatspeedtab(int speed, struct speedtab *table)
88 return (0); /* hangup */
89 for ( ; table->sp_speed > 0; table++)
90 if (table->sp_speed <= speed) /* nearest one, rounded down */
91 return (table->sp_code);
92 return (1); /* 50, min and not hangup */
96 ttsetcompat(struct tty *tp, u_long *com, caddr_t data, struct termios *term)
101 struct sgttyb *sg = (struct sgttyb *)data;
104 if ((speed = sg->sg_ispeed) > MAX_SPEED || speed < 0)
106 else if (speed != ttcompatspeedtab(tp->t_ispeed, compatspeeds))
107 term->c_ispeed = compatspcodes[speed];
109 term->c_ispeed = tp->t_ispeed;
110 if ((speed = sg->sg_ospeed) > MAX_SPEED || speed < 0)
112 else if (speed != ttcompatspeedtab(tp->t_ospeed, compatspeeds))
113 term->c_ospeed = compatspcodes[speed];
115 term->c_ospeed = tp->t_ospeed;
116 term->c_cc[VERASE] = sg->sg_erase;
117 term->c_cc[VKILL] = sg->sg_kill;
118 tp->t_flags = (tp->t_flags&0xffff0000) | (sg->sg_flags&0xffff);
119 ttcompatsetflags(tp, term);
120 *com = (*com == TIOCSETP) ? TIOCSETAF : TIOCSETA;
124 struct tchars *tc = (struct tchars *)data;
128 cc[VINTR] = tc->t_intrc;
129 cc[VQUIT] = tc->t_quitc;
130 cc[VSTART] = tc->t_startc;
131 cc[VSTOP] = tc->t_stopc;
132 cc[VEOF] = tc->t_eofc;
133 cc[VEOL] = tc->t_brkc;
134 if (tc->t_brkc == -1)
135 cc[VEOL2] = _POSIX_VDISABLE;
140 struct ltchars *ltc = (struct ltchars *)data;
144 cc[VSUSP] = ltc->t_suspc;
145 cc[VDSUSP] = ltc->t_dsuspc;
146 cc[VREPRINT] = ltc->t_rprntc;
147 cc[VDISCARD] = ltc->t_flushc;
148 cc[VWERASE] = ltc->t_werasc;
149 cc[VLNEXT] = ltc->t_lnextc;
156 if (*com == TIOCLSET)
157 tp->t_flags = (tp->t_flags&0xffff) | *(int *)data<<16;
160 (ttcompatgetflags(tp)&0xffff0000)|(tp->t_flags&0xffff);
161 if (*com == TIOCLBIS)
162 tp->t_flags |= *(int *)data<<16;
164 tp->t_flags &= ~(*(int *)data<<16);
166 ttcompatsetlflags(tp, term);
175 ttcompat(struct tty *tp, u_long com, caddr_t data, int flag)
188 term = tp->t_termios;
189 if ((error = ttsetcompat(tp, &com, data, &term)) != 0)
191 return ttioctl(tp, com, &term, flag);
194 struct sgttyb *sg = (struct sgttyb *)data;
197 sg->sg_ospeed = ttcompatspeedtab(tp->t_ospeed, compatspeeds);
198 if (tp->t_ispeed == 0)
199 sg->sg_ispeed = sg->sg_ospeed;
201 sg->sg_ispeed = ttcompatspeedtab(tp->t_ispeed, compatspeeds);
202 sg->sg_erase = cc[VERASE];
203 sg->sg_kill = cc[VKILL];
204 sg->sg_flags = tp->t_flags = ttcompatgetflags(tp);
208 struct tchars *tc = (struct tchars *)data;
211 tc->t_intrc = cc[VINTR];
212 tc->t_quitc = cc[VQUIT];
213 tc->t_startc = cc[VSTART];
214 tc->t_stopc = cc[VSTOP];
215 tc->t_eofc = cc[VEOF];
216 tc->t_brkc = cc[VEOL];
220 struct ltchars *ltc = (struct ltchars *)data;
223 ltc->t_suspc = cc[VSUSP];
224 ltc->t_dsuspc = cc[VDSUSP];
225 ltc->t_rprntc = cc[VREPRINT];
226 ltc->t_flushc = cc[VDISCARD];
227 ltc->t_werasc = cc[VWERASE];
228 ltc->t_lnextc = cc[VLNEXT];
233 (ttcompatgetflags(tp) & 0xffff0000UL)
234 | (tp->t_flags & 0xffff);
235 *(int *)data = tp->t_flags>>16;
237 kprintf("CLGET: returning %x\n", *(int *)data);
241 *(int *)data = tp->t_line ? tp->t_line : 2;
247 return (ttioctl(tp, TIOCSETD,
248 *(int *)data == 2 ? (caddr_t)&ldisczero : data, flag));
253 return (ttioctl(tp, TIOCCONS, data, flag));
262 ttcompatgetflags(struct tty *tp)
264 tcflag_t iflag = tp->t_iflag;
265 tcflag_t lflag = tp->t_lflag;
266 tcflag_t oflag = tp->t_oflag;
267 tcflag_t cflag = tp->t_cflag;
272 if (iflag&ICRNL || oflag&ONLCR)
274 if ((cflag&CSIZE) == CS8) {
279 else if (cflag&PARENB) {
286 flags |= EVENP | ODDP;
289 if ((lflag&ICANON) == 0) {
291 if (iflag&(INPCK|ISTRIP|IXON) || lflag&(IEXTEN|ISIG)
292 || (cflag&(CSIZE|PARENB)) != CS8)
297 if (!(flags&RAW) && !(oflag&OPOST) && (cflag&(CSIZE|PARENB)) == CS8)
301 if ((cflag&HUPCL) == 0)
306 flags |= CRTERA|CRTBS;
308 flags |= CRTKIL|CRTBS;
313 if ((iflag&IXANY) == 0)
315 flags |= lflag&(ECHO|TOSTOP|FLUSHO|PENDIN|NOFLSH);
317 kprintf("getflags: %x\n", flags);
322 ttcompatsetflags(struct tty *tp, struct termios *t)
324 int flags = tp->t_flags;
325 tcflag_t iflag = t->c_iflag;
326 tcflag_t oflag = t->c_oflag;
327 tcflag_t lflag = t->c_lflag;
328 tcflag_t cflag = t->c_cflag;
332 lflag &= ~(ECHOCTL|ISIG|ICANON|IEXTEN);
334 iflag &= ~(PARMRK|IGNPAR|IGNCR|INLCR);
335 iflag |= BRKINT|IXON|IMAXBEL;
336 lflag |= ISIG|IEXTEN|ECHOCTL; /* XXX was echoctl on ? */
358 cflag &= ~(CSIZE|PARENB);
359 if (flags&(RAW|LITOUT|PASS8)) {
361 if (!(flags&(RAW|PASS8))
362 || (flags&(RAW|PASS8|ANYP)) == (PASS8|ANYP))
366 if (flags&(RAW|LITOUT))
375 /* XXX don't set INPCK if RAW or PASS8? */
376 if ((flags&(EVENP|ODDP)) == EVENP) {
379 } else if ((flags&(EVENP|ODDP)) == ODDP) {
388 if ((flags&DECCTQ) == 0)
399 ttcompatsetlflags(struct tty *tp, struct termios *t)
401 int flags = tp->t_flags;
402 tcflag_t iflag = t->c_iflag;
403 tcflag_t oflag = t->c_oflag;
404 tcflag_t lflag = t->c_lflag;
405 tcflag_t cflag = t->c_cflag;
407 iflag &= ~(PARMRK|IGNPAR|IGNCR|INLCR);
428 if ((flags&DECCTQ) == 0)
440 lflag &= ~(TOSTOP|FLUSHO|PENDIN|NOFLSH);
441 lflag |= flags&(TOSTOP|FLUSHO|PENDIN|NOFLSH);
444 * The next if-else statement is copied from above so don't bother
445 * checking it separately. We could avoid fiddlling with the
446 * character size if the mode is already RAW or if neither the
447 * LITOUT bit or the PASS8 bit is being changed, but the delta of
448 * the change is not available here and skipping the RAW case would
449 * make the code different from above.
451 cflag &= ~(CSIZE|PARENB);
452 if (flags&(RAW|LITOUT|PASS8)) {
454 if (!(flags&(RAW|PASS8))
455 || (flags&(RAW|PASS8|ANYP)) == (PASS8|ANYP))
459 if (flags&(RAW|LITOUT))