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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)tty_compat.c 8.1 (Berkeley) 6/10/93
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
39 * mapping routines for old line discipline (yuck)
41 #if defined(COMPAT_43)
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/ioctl_compat.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
50 static int ttcompatgetflags(struct tty *tp);
51 static void ttcompatsetflags(struct tty *tp, struct termios *t);
52 static void ttcompatsetlflags(struct tty *tp, struct termios *t);
53 static int ttcompatspeedtab(int speed, struct speedtab *table);
55 static int ttydebug = 0;
56 SYSCTL_INT(_debug, OID_AUTO, ttydebug, CTLFLAG_RW, &ttydebug, 0, "");
58 static struct speedtab compatspeeds[] = {
80 static int compatspcodes[] = {
81 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
82 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200,
86 ttcompatspeedtab(int speed, struct speedtab *table)
89 return (0); /* hangup */
90 for ( ; table->sp_speed > 0; table++)
91 if (table->sp_speed <= speed) /* nearest one, rounded down */
92 return (table->sp_code);
93 return (1); /* 50, min and not hangup */
97 ttsetcompat(struct tty *tp, u_long *com, caddr_t data, struct termios *term)
102 struct sgttyb *sg = (struct sgttyb *)data;
105 if ((speed = sg->sg_ispeed) > MAX_SPEED || speed < 0)
107 else if (speed != ttcompatspeedtab(tp->t_ispeed, compatspeeds))
108 term->c_ispeed = compatspcodes[speed];
110 term->c_ispeed = tp->t_ispeed;
111 if ((speed = sg->sg_ospeed) > MAX_SPEED || speed < 0)
113 else if (speed != ttcompatspeedtab(tp->t_ospeed, compatspeeds))
114 term->c_ospeed = compatspcodes[speed];
116 term->c_ospeed = tp->t_ospeed;
117 term->c_cc[VERASE] = sg->sg_erase;
118 term->c_cc[VKILL] = sg->sg_kill;
119 tp->t_flags = (tp->t_flags&0xffff0000) | (sg->sg_flags&0xffff);
120 ttcompatsetflags(tp, term);
121 *com = (*com == TIOCSETP) ? TIOCSETAF : TIOCSETA;
125 struct tchars *tc = (struct tchars *)data;
129 cc[VINTR] = tc->t_intrc;
130 cc[VQUIT] = tc->t_quitc;
131 cc[VSTART] = tc->t_startc;
132 cc[VSTOP] = tc->t_stopc;
133 cc[VEOF] = tc->t_eofc;
134 cc[VEOL] = tc->t_brkc;
135 if (tc->t_brkc == (char)_POSIX_VDISABLE)
136 cc[VEOL2] = _POSIX_VDISABLE;
141 struct ltchars *ltc = (struct ltchars *)data;
145 cc[VSUSP] = ltc->t_suspc;
146 cc[VDSUSP] = ltc->t_dsuspc;
147 cc[VREPRINT] = ltc->t_rprntc;
148 cc[VDISCARD] = ltc->t_flushc;
149 cc[VWERASE] = ltc->t_werasc;
150 cc[VLNEXT] = ltc->t_lnextc;
157 if (*com == TIOCLSET)
158 tp->t_flags = (tp->t_flags&0xffff) | *(int *)data<<16;
161 (ttcompatgetflags(tp)&0xffff0000)|(tp->t_flags&0xffff);
162 if (*com == TIOCLBIS)
163 tp->t_flags |= *(int *)data<<16;
165 tp->t_flags &= ~(*(int *)data<<16);
167 ttcompatsetlflags(tp, term);
176 ttcompat(struct tty *tp, u_long com, caddr_t data, int flag)
189 term = tp->t_termios;
190 if ((error = ttsetcompat(tp, &com, data, &term)) != 0)
192 return ttioctl(tp, com, &term, flag);
195 struct sgttyb *sg = (struct sgttyb *)data;
198 sg->sg_ospeed = ttcompatspeedtab(tp->t_ospeed, compatspeeds);
199 if (tp->t_ispeed == 0)
200 sg->sg_ispeed = sg->sg_ospeed;
202 sg->sg_ispeed = ttcompatspeedtab(tp->t_ispeed, compatspeeds);
203 sg->sg_erase = cc[VERASE];
204 sg->sg_kill = cc[VKILL];
205 sg->sg_flags = tp->t_flags = ttcompatgetflags(tp);
209 struct tchars *tc = (struct tchars *)data;
212 tc->t_intrc = cc[VINTR];
213 tc->t_quitc = cc[VQUIT];
214 tc->t_startc = cc[VSTART];
215 tc->t_stopc = cc[VSTOP];
216 tc->t_eofc = cc[VEOF];
217 tc->t_brkc = cc[VEOL];
221 struct ltchars *ltc = (struct ltchars *)data;
224 ltc->t_suspc = cc[VSUSP];
225 ltc->t_dsuspc = cc[VDSUSP];
226 ltc->t_rprntc = cc[VREPRINT];
227 ltc->t_flushc = cc[VDISCARD];
228 ltc->t_werasc = cc[VWERASE];
229 ltc->t_lnextc = cc[VLNEXT];
234 (ttcompatgetflags(tp) & 0xffff0000UL)
235 | (tp->t_flags & 0xffff);
236 *(int *)data = tp->t_flags>>16;
238 printf("CLGET: returning %x\n", *(int *)data);
242 *(int *)data = tp->t_line ? tp->t_line : 2;
248 return (ttioctl(tp, TIOCSETD,
249 *(int *)data == 2 ? (caddr_t)&ldisczero : data, flag));
254 return (ttioctl(tp, TIOCCONS, data, flag));
263 ttcompatgetflags(struct tty *tp)
265 tcflag_t iflag = tp->t_iflag;
266 tcflag_t lflag = tp->t_lflag;
267 tcflag_t oflag = tp->t_oflag;
268 tcflag_t cflag = tp->t_cflag;
273 if (iflag&ICRNL || oflag&ONLCR)
275 if ((cflag&CSIZE) == CS8) {
280 else if (cflag&PARENB) {
287 flags |= EVENP | ODDP;
290 if ((lflag&ICANON) == 0) {
292 if (iflag&(INPCK|ISTRIP|IXON) || lflag&(IEXTEN|ISIG)
293 || (cflag&(CSIZE|PARENB)) != CS8)
298 if (!(flags&RAW) && !(oflag&OPOST) && (cflag&(CSIZE|PARENB)) == CS8)
302 if ((cflag&HUPCL) == 0)
307 flags |= CRTERA|CRTBS;
309 flags |= CRTKIL|CRTBS;
314 if ((iflag&IXANY) == 0)
316 flags |= lflag&(ECHO|TOSTOP|FLUSHO|PENDIN|NOFLSH);
318 printf("getflags: %x\n", flags);
323 ttcompatsetflags(struct tty *tp, struct termios *t)
325 int flags = tp->t_flags;
326 tcflag_t iflag = t->c_iflag;
327 tcflag_t oflag = t->c_oflag;
328 tcflag_t lflag = t->c_lflag;
329 tcflag_t cflag = t->c_cflag;
333 lflag &= ~(ECHOCTL|ISIG|ICANON|IEXTEN);
335 iflag &= ~(PARMRK|IGNPAR|IGNCR|INLCR);
336 iflag |= BRKINT|IXON|IMAXBEL;
337 lflag |= ISIG|IEXTEN|ECHOCTL; /* XXX was echoctl on ? */
359 cflag &= ~(CSIZE|PARENB);
360 if (flags&(RAW|LITOUT|PASS8)) {
362 if (!(flags&(RAW|PASS8))
363 || (flags&(RAW|PASS8|ANYP)) == (PASS8|ANYP))
367 if (flags&(RAW|LITOUT))
376 /* XXX don't set INPCK if RAW or PASS8? */
377 if ((flags&(EVENP|ODDP)) == EVENP) {
380 } else if ((flags&(EVENP|ODDP)) == ODDP) {
389 if ((flags&DECCTQ) == 0)
400 ttcompatsetlflags(struct tty *tp, struct termios *t)
402 int flags = tp->t_flags;
403 tcflag_t iflag = t->c_iflag;
404 tcflag_t oflag = t->c_oflag;
405 tcflag_t lflag = t->c_lflag;
406 tcflag_t cflag = t->c_cflag;
408 iflag &= ~(PARMRK|IGNPAR|IGNCR|INLCR);
429 if ((flags&DECCTQ) == 0)
441 lflag &= ~(TOSTOP|FLUSHO|PENDIN|NOFLSH);
442 lflag |= flags&(TOSTOP|FLUSHO|PENDIN|NOFLSH);
445 * The next if-else statement is copied from above so don't bother
446 * checking it separately. We could avoid fiddlling with the
447 * character size if the mode is already RAW or if neither the
448 * LITOUT bit or the PASS8 bit is being changed, but the delta of
449 * the change is not available here and skipping the RAW case would
450 * make the code different from above.
452 cflag &= ~(CSIZE|PARENB);
453 if (flags&(RAW|LITOUT|PASS8)) {
455 if (!(flags&(RAW|PASS8))
456 || (flags&(RAW|PASS8|ANYP)) == (PASS8|ANYP))
460 if (flags&(RAW|LITOUT))
474 #endif /* COMPAT_43 */
476 #endif /* BURN_BRIDGES */