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"
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>
49 int sp_speed
; /* Speed. */
50 int sp_code
; /* Code. */
53 static int ttcompatgetflags(struct tty
*tp
);
54 static void ttcompatsetflags(struct tty
*tp
, struct termios
*t
);
55 static void ttcompatsetlflags(struct tty
*tp
, struct termios
*t
);
56 static int ttcompatspeedtab(int speed
, struct speedtab
*table
);
58 static int ttydebug
= 0;
59 SYSCTL_INT(_debug
, OID_AUTO
, ttydebug
, CTLFLAG_RW
, &ttydebug
, 0, "");
61 static struct speedtab compatspeeds
[] = {
83 static int compatspcodes
[] = {
84 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
85 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200,
89 ttcompatspeedtab(int speed
, struct speedtab
*table
)
92 return (0); /* hangup */
93 for ( ; table
->sp_speed
> 0; table
++)
94 if (table
->sp_speed
<= speed
) /* nearest one, rounded down */
95 return (table
->sp_code
);
96 return (1); /* 50, min and not hangup */
100 ttsetcompat(struct tty
*tp
, u_long
*com
, caddr_t data
, struct termios
*term
)
105 struct sgttyb
*sg
= (struct sgttyb
*)data
;
108 if ((speed
= sg
->sg_ispeed
) > MAX_SPEED
|| speed
< 0)
110 else if (speed
!= ttcompatspeedtab(tp
->t_termios
.c_ispeed
,
112 term
->c_ispeed
= compatspcodes
[speed
];
114 term
->c_ispeed
= tp
->t_termios
.c_ispeed
;
115 if ((speed
= sg
->sg_ospeed
) > MAX_SPEED
|| speed
< 0)
117 else if (speed
!= ttcompatspeedtab(tp
->t_termios
.c_ospeed
,
119 term
->c_ospeed
= compatspcodes
[speed
];
121 term
->c_ospeed
= tp
->t_termios
.c_ospeed
;
122 term
->c_cc
[VERASE
] = sg
->sg_erase
;
123 term
->c_cc
[VKILL
] = sg
->sg_kill
;
124 tp
->t_compatflags
= (tp
->t_compatflags
&0xffff0000) |
125 (sg
->sg_flags
&0xffff);
126 ttcompatsetflags(tp
, term
);
127 *com
= (*com
== TIOCSETP
) ? TIOCSETAF
: TIOCSETA
;
131 struct tchars
*tc
= (struct tchars
*)data
;
135 cc
[VINTR
] = tc
->t_intrc
;
136 cc
[VQUIT
] = tc
->t_quitc
;
137 cc
[VSTART
] = tc
->t_startc
;
138 cc
[VSTOP
] = tc
->t_stopc
;
139 cc
[VEOF
] = tc
->t_eofc
;
140 cc
[VEOL
] = tc
->t_brkc
;
141 if (tc
->t_brkc
== (char)_POSIX_VDISABLE
)
142 cc
[VEOL2
] = _POSIX_VDISABLE
;
147 struct ltchars
*ltc
= (struct ltchars
*)data
;
151 cc
[VSUSP
] = ltc
->t_suspc
;
152 cc
[VDSUSP
] = ltc
->t_dsuspc
;
153 cc
[VREPRINT
] = ltc
->t_rprntc
;
154 cc
[VDISCARD
] = ltc
->t_flushc
;
155 cc
[VWERASE
] = ltc
->t_werasc
;
156 cc
[VLNEXT
] = ltc
->t_lnextc
;
163 if (*com
== TIOCLSET
)
164 tp
->t_compatflags
= (tp
->t_compatflags
&0xffff) |
167 tp
->t_compatflags
= (ttcompatgetflags(tp
)&0xffff0000) |
168 (tp
->t_compatflags
&0xffff);
169 if (*com
== TIOCLBIS
)
170 tp
->t_compatflags
|= *(int *)data
<<16;
172 tp
->t_compatflags
&= ~(*(int *)data
<<16);
174 ttcompatsetlflags(tp
, term
);
183 tty_ioctl_compat(struct tty
*tp
, u_long com
, caddr_t data
, struct thread
*td
)
196 term
= tp
->t_termios
;
197 if ((error
= ttsetcompat(tp
, &com
, data
, &term
)) != 0)
199 return tty_ioctl(tp
, com
, &term
, td
);
202 struct sgttyb
*sg
= (struct sgttyb
*)data
;
203 cc_t
*cc
= tp
->t_termios
.c_cc
;
205 sg
->sg_ospeed
= ttcompatspeedtab(tp
->t_termios
.c_ospeed
,
207 if (tp
->t_termios
.c_ispeed
== 0)
208 sg
->sg_ispeed
= sg
->sg_ospeed
;
210 sg
->sg_ispeed
= ttcompatspeedtab(tp
->t_termios
.c_ispeed
,
212 sg
->sg_erase
= cc
[VERASE
];
213 sg
->sg_kill
= cc
[VKILL
];
214 sg
->sg_flags
= tp
->t_compatflags
= ttcompatgetflags(tp
);
218 struct tchars
*tc
= (struct tchars
*)data
;
219 cc_t
*cc
= tp
->t_termios
.c_cc
;
221 tc
->t_intrc
= cc
[VINTR
];
222 tc
->t_quitc
= cc
[VQUIT
];
223 tc
->t_startc
= cc
[VSTART
];
224 tc
->t_stopc
= cc
[VSTOP
];
225 tc
->t_eofc
= cc
[VEOF
];
226 tc
->t_brkc
= cc
[VEOL
];
230 struct ltchars
*ltc
= (struct ltchars
*)data
;
231 cc_t
*cc
= tp
->t_termios
.c_cc
;
233 ltc
->t_suspc
= cc
[VSUSP
];
234 ltc
->t_dsuspc
= cc
[VDSUSP
];
235 ltc
->t_rprntc
= cc
[VREPRINT
];
236 ltc
->t_flushc
= cc
[VDISCARD
];
237 ltc
->t_werasc
= cc
[VWERASE
];
238 ltc
->t_lnextc
= cc
[VLNEXT
];
243 (ttcompatgetflags(tp
) & 0xffff0000UL
)
244 | (tp
->t_compatflags
& 0xffff);
245 *(int *)data
= tp
->t_compatflags
>>16;
247 printf("CLGET: returning %x\n", *(int *)data
);
257 return (tty_ioctl(tp
, TIOCSETD
,
258 *(int *)data
== 2 ? (caddr_t
)&ldisczero
: data
, td
));
263 return (tty_ioctl(tp
, TIOCCONS
, data
, td
));
272 ttcompatgetflags(struct tty
*tp
)
274 tcflag_t iflag
= tp
->t_termios
.c_iflag
;
275 tcflag_t lflag
= tp
->t_termios
.c_lflag
;
276 tcflag_t oflag
= tp
->t_termios
.c_oflag
;
277 tcflag_t cflag
= tp
->t_termios
.c_cflag
;
282 if (iflag
&ICRNL
|| oflag
&ONLCR
)
284 if ((cflag
&CSIZE
) == CS8
) {
289 else if (cflag
&PARENB
) {
296 flags
|= EVENP
| ODDP
;
299 if ((lflag
&ICANON
) == 0) {
301 if (iflag
&(INPCK
|ISTRIP
|IXON
) || lflag
&(IEXTEN
|ISIG
)
302 || (cflag
&(CSIZE
|PARENB
)) != CS8
)
307 if (!(flags
&RAW
) && !(oflag
&OPOST
) && (cflag
&(CSIZE
|PARENB
)) == CS8
)
311 if ((cflag
&HUPCL
) == 0)
316 flags
|= CRTERA
|CRTBS
;
318 flags
|= CRTKIL
|CRTBS
;
323 if ((iflag
&IXANY
) == 0)
325 flags
|= lflag
&(ECHO
|TOSTOP
|FLUSHO
|PENDIN
|NOFLSH
);
327 printf("getflags: %x\n", flags
);
332 ttcompatsetflags(struct tty
*tp
, struct termios
*t
)
334 int flags
= tp
->t_compatflags
;
335 tcflag_t iflag
= t
->c_iflag
;
336 tcflag_t oflag
= t
->c_oflag
;
337 tcflag_t lflag
= t
->c_lflag
;
338 tcflag_t cflag
= t
->c_cflag
;
342 lflag
&= ~(ECHOCTL
|ISIG
|ICANON
|IEXTEN
);
344 iflag
&= ~(PARMRK
|IGNPAR
|IGNCR
|INLCR
);
345 iflag
|= BRKINT
|IXON
|IMAXBEL
;
346 lflag
|= ISIG
|IEXTEN
|ECHOCTL
; /* XXX was echoctl on ? */
368 cflag
&= ~(CSIZE
|PARENB
);
369 if (flags
&(RAW
|LITOUT
|PASS8
)) {
371 if (!(flags
&(RAW
|PASS8
))
372 || (flags
&(RAW
|PASS8
|ANYP
)) == (PASS8
|ANYP
))
376 if (flags
&(RAW
|LITOUT
))
385 /* XXX don't set INPCK if RAW or PASS8? */
386 if ((flags
&(EVENP
|ODDP
)) == EVENP
) {
389 } else if ((flags
&(EVENP
|ODDP
)) == ODDP
) {
398 if ((flags
&DECCTQ
) == 0)
409 ttcompatsetlflags(struct tty
*tp
, struct termios
*t
)
411 int flags
= tp
->t_compatflags
;
412 tcflag_t iflag
= t
->c_iflag
;
413 tcflag_t oflag
= t
->c_oflag
;
414 tcflag_t lflag
= t
->c_lflag
;
415 tcflag_t cflag
= t
->c_cflag
;
417 iflag
&= ~(PARMRK
|IGNPAR
|IGNCR
|INLCR
);
438 if ((flags
&DECCTQ
) == 0)
450 lflag
&= ~(TOSTOP
|FLUSHO
|PENDIN
|NOFLSH
);
451 lflag
|= flags
&(TOSTOP
|FLUSHO
|PENDIN
|NOFLSH
);
454 * The next if-else statement is copied from above so don't bother
455 * checking it separately. We could avoid fiddlling with the
456 * character size if the mode is already RAW or if neither the
457 * LITOUT bit or the PASS8 bit is being changed, but the delta of
458 * the change is not available here and skipping the RAW case would
459 * make the code different from above.
461 cflag
&= ~(CSIZE
|PARENB
);
462 if (flags
&(RAW
|LITOUT
|PASS8
)) {
464 if (!(flags
&(RAW
|PASS8
))
465 || (flags
&(RAW
|PASS8
|ANYP
)) == (PASS8
|ANYP
))
469 if (flags
&(RAW
|LITOUT
))