Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / kern / tty_compat.c
blob52f4e692330054361c655d6b66984bd93b60419f
1 /*-
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
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.
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.
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 $
35 * $DragonFly: src/sys/kern/tty_compat.c,v 1.6 2006/12/23 23:47:54 swildner Exp $
38 #include "opt_compat.h"
41 * mapping routines for old line discipline (yuck)
43 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/ioctl_compat.h>
48 #include <sys/tty.h>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
52 static int ttcompatgetflags (struct tty *tp);
53 static void ttcompatsetflags (struct tty *tp, struct termios *t);
54 static void ttcompatsetlflags (struct tty *tp, struct termios *t);
55 static int ttcompatspeedtab (int speed, struct speedtab *table);
57 static int ttydebug = 0;
58 SYSCTL_INT(_debug, OID_AUTO, ttydebug, CTLFLAG_RW, &ttydebug, 0,
59 "tty debugging");
61 static struct speedtab compatspeeds[] = {
62 #define MAX_SPEED 17
63 { 115200, 17 },
64 { 57600, 16 },
65 { 38400, 15 },
66 { 19200, 14 },
67 { 9600, 13 },
68 { 4800, 12 },
69 { 2400, 11 },
70 { 1800, 10 },
71 { 1200, 9 },
72 { 600, 8 },
73 { 300, 7 },
74 { 200, 6 },
75 { 150, 5 },
76 { 134, 4 },
77 { 110, 3 },
78 { 75, 2 },
79 { 50, 1 },
80 { 0, 0 },
81 { -1, -1 },
83 static int compatspcodes[] = {
84 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
85 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200,
88 static int
89 ttcompatspeedtab(int speed, struct speedtab *table)
91 if (speed == 0)
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 */
99 int
100 ttsetcompat(struct tty *tp, u_long *com, caddr_t data, struct termios *term)
102 switch (*com) {
103 case TIOCSETP:
104 case TIOCSETN: {
105 struct sgttyb *sg = (struct sgttyb *)data;
106 int speed;
108 if ((speed = sg->sg_ispeed) > MAX_SPEED || speed < 0)
109 return(EINVAL);
110 else if (speed != ttcompatspeedtab(tp->t_ispeed, compatspeeds))
111 term->c_ispeed = compatspcodes[speed];
112 else
113 term->c_ispeed = tp->t_ispeed;
114 if ((speed = sg->sg_ospeed) > MAX_SPEED || speed < 0)
115 return(EINVAL);
116 else if (speed != ttcompatspeedtab(tp->t_ospeed, compatspeeds))
117 term->c_ospeed = compatspcodes[speed];
118 else
119 term->c_ospeed = tp->t_ospeed;
120 term->c_cc[VERASE] = sg->sg_erase;
121 term->c_cc[VKILL] = sg->sg_kill;
122 tp->t_flags = (tp->t_flags&0xffff0000) | (sg->sg_flags&0xffff);
123 ttcompatsetflags(tp, term);
124 *com = (*com == TIOCSETP) ? TIOCSETAF : TIOCSETA;
125 break;
127 case TIOCSETC: {
128 struct tchars *tc = (struct tchars *)data;
129 cc_t *cc;
131 cc = term->c_cc;
132 cc[VINTR] = tc->t_intrc;
133 cc[VQUIT] = tc->t_quitc;
134 cc[VSTART] = tc->t_startc;
135 cc[VSTOP] = tc->t_stopc;
136 cc[VEOF] = tc->t_eofc;
137 cc[VEOL] = tc->t_brkc;
138 if (tc->t_brkc == -1)
139 cc[VEOL2] = _POSIX_VDISABLE;
140 *com = TIOCSETA;
141 break;
143 case TIOCSLTC: {
144 struct ltchars *ltc = (struct ltchars *)data;
145 cc_t *cc;
147 cc = term->c_cc;
148 cc[VSUSP] = ltc->t_suspc;
149 cc[VDSUSP] = ltc->t_dsuspc;
150 cc[VREPRINT] = ltc->t_rprntc;
151 cc[VDISCARD] = ltc->t_flushc;
152 cc[VWERASE] = ltc->t_werasc;
153 cc[VLNEXT] = ltc->t_lnextc;
154 *com = TIOCSETA;
155 break;
157 case TIOCLBIS:
158 case TIOCLBIC:
159 case TIOCLSET:
160 if (*com == TIOCLSET)
161 tp->t_flags = (tp->t_flags&0xffff) | *(int *)data<<16;
162 else {
163 tp->t_flags =
164 (ttcompatgetflags(tp)&0xffff0000)|(tp->t_flags&0xffff);
165 if (*com == TIOCLBIS)
166 tp->t_flags |= *(int *)data<<16;
167 else
168 tp->t_flags &= ~(*(int *)data<<16);
170 ttcompatsetlflags(tp, term);
171 *com = TIOCSETA;
172 break;
174 return 0;
177 /*ARGSUSED*/
179 ttcompat(struct tty *tp, u_long com, caddr_t data, int flag)
181 switch (com) {
182 case TIOCSETP:
183 case TIOCSETN:
184 case TIOCSETC:
185 case TIOCSLTC:
186 case TIOCLBIS:
187 case TIOCLBIC:
188 case TIOCLSET: {
189 struct termios term;
190 int error;
192 term = tp->t_termios;
193 if ((error = ttsetcompat(tp, &com, data, &term)) != 0)
194 return error;
195 return ttioctl(tp, com, &term, flag);
197 case TIOCGETP: {
198 struct sgttyb *sg = (struct sgttyb *)data;
199 cc_t *cc = tp->t_cc;
201 sg->sg_ospeed = ttcompatspeedtab(tp->t_ospeed, compatspeeds);
202 if (tp->t_ispeed == 0)
203 sg->sg_ispeed = sg->sg_ospeed;
204 else
205 sg->sg_ispeed = ttcompatspeedtab(tp->t_ispeed, compatspeeds);
206 sg->sg_erase = cc[VERASE];
207 sg->sg_kill = cc[VKILL];
208 sg->sg_flags = tp->t_flags = ttcompatgetflags(tp);
209 break;
211 case TIOCGETC: {
212 struct tchars *tc = (struct tchars *)data;
213 cc_t *cc = tp->t_cc;
215 tc->t_intrc = cc[VINTR];
216 tc->t_quitc = cc[VQUIT];
217 tc->t_startc = cc[VSTART];
218 tc->t_stopc = cc[VSTOP];
219 tc->t_eofc = cc[VEOF];
220 tc->t_brkc = cc[VEOL];
221 break;
223 case TIOCGLTC: {
224 struct ltchars *ltc = (struct ltchars *)data;
225 cc_t *cc = tp->t_cc;
227 ltc->t_suspc = cc[VSUSP];
228 ltc->t_dsuspc = cc[VDSUSP];
229 ltc->t_rprntc = cc[VREPRINT];
230 ltc->t_flushc = cc[VDISCARD];
231 ltc->t_werasc = cc[VWERASE];
232 ltc->t_lnextc = cc[VLNEXT];
233 break;
235 case TIOCLGET:
236 tp->t_flags =
237 (ttcompatgetflags(tp) & 0xffff0000UL)
238 | (tp->t_flags & 0xffff);
239 *(int *)data = tp->t_flags>>16;
240 if (ttydebug)
241 kprintf("CLGET: returning %x\n", *(int *)data);
242 break;
244 case OTIOCGETD:
245 *(int *)data = tp->t_line ? tp->t_line : 2;
246 break;
248 case OTIOCSETD: {
249 int ldisczero = 0;
251 return (ttioctl(tp, TIOCSETD,
252 *(int *)data == 2 ? (caddr_t)&ldisczero : data, flag));
255 case OTIOCCONS:
256 *(int *)data = 1;
257 return (ttioctl(tp, TIOCCONS, data, flag));
259 default:
260 return (ENOIOCTL);
262 return (0);
265 static int
266 ttcompatgetflags(struct tty *tp)
268 tcflag_t iflag = tp->t_iflag;
269 tcflag_t lflag = tp->t_lflag;
270 tcflag_t oflag = tp->t_oflag;
271 tcflag_t cflag = tp->t_cflag;
272 int flags = 0;
274 if (iflag&IXOFF)
275 flags |= TANDEM;
276 if (iflag&ICRNL || oflag&ONLCR)
277 flags |= CRMOD;
278 if ((cflag&CSIZE) == CS8) {
279 flags |= PASS8;
280 if (iflag&ISTRIP)
281 flags |= ANYP;
283 else if (cflag&PARENB) {
284 if (iflag&INPCK) {
285 if (cflag&PARODD)
286 flags |= ODDP;
287 else
288 flags |= EVENP;
289 } else
290 flags |= EVENP | ODDP;
293 if ((lflag&ICANON) == 0) {
294 /* fudge */
295 if (iflag&(INPCK|ISTRIP|IXON) || lflag&(IEXTEN|ISIG)
296 || (cflag&(CSIZE|PARENB)) != CS8)
297 flags |= CBREAK;
298 else
299 flags |= RAW;
301 if (!(flags&RAW) && !(oflag&OPOST) && (cflag&(CSIZE|PARENB)) == CS8)
302 flags |= LITOUT;
303 if (cflag&MDMBUF)
304 flags |= MDMBUF;
305 if ((cflag&HUPCL) == 0)
306 flags |= NOHANG;
307 if (oflag&OXTABS)
308 flags |= XTABS;
309 if (lflag&ECHOE)
310 flags |= CRTERA|CRTBS;
311 if (lflag&ECHOKE)
312 flags |= CRTKIL|CRTBS;
313 if (lflag&ECHOPRT)
314 flags |= PRTERA;
315 if (lflag&ECHOCTL)
316 flags |= CTLECH;
317 if ((iflag&IXANY) == 0)
318 flags |= DECCTQ;
319 flags |= lflag&(ECHO|TOSTOP|FLUSHO|PENDIN|NOFLSH);
320 if (ttydebug)
321 kprintf("getflags: %x\n", flags);
322 return (flags);
325 static void
326 ttcompatsetflags(struct tty *tp, struct termios *t)
328 int flags = tp->t_flags;
329 tcflag_t iflag = t->c_iflag;
330 tcflag_t oflag = t->c_oflag;
331 tcflag_t lflag = t->c_lflag;
332 tcflag_t cflag = t->c_cflag;
334 if (flags & RAW) {
335 iflag = IGNBRK;
336 lflag &= ~(ECHOCTL|ISIG|ICANON|IEXTEN);
337 } else {
338 iflag &= ~(PARMRK|IGNPAR|IGNCR|INLCR);
339 iflag |= BRKINT|IXON|IMAXBEL;
340 lflag |= ISIG|IEXTEN|ECHOCTL; /* XXX was echoctl on ? */
341 if (flags & XTABS)
342 oflag |= OXTABS;
343 else
344 oflag &= ~OXTABS;
345 if (flags & CBREAK)
346 lflag &= ~ICANON;
347 else
348 lflag |= ICANON;
349 if (flags&CRMOD) {
350 iflag |= ICRNL;
351 oflag |= ONLCR;
352 } else {
353 iflag &= ~ICRNL;
354 oflag &= ~ONLCR;
357 if (flags&ECHO)
358 lflag |= ECHO;
359 else
360 lflag &= ~ECHO;
362 cflag &= ~(CSIZE|PARENB);
363 if (flags&(RAW|LITOUT|PASS8)) {
364 cflag |= CS8;
365 if (!(flags&(RAW|PASS8))
366 || (flags&(RAW|PASS8|ANYP)) == (PASS8|ANYP))
367 iflag |= ISTRIP;
368 else
369 iflag &= ~ISTRIP;
370 if (flags&(RAW|LITOUT))
371 oflag &= ~OPOST;
372 else
373 oflag |= OPOST;
374 } else {
375 cflag |= CS7|PARENB;
376 iflag |= ISTRIP;
377 oflag |= OPOST;
379 /* XXX don't set INPCK if RAW or PASS8? */
380 if ((flags&(EVENP|ODDP)) == EVENP) {
381 iflag |= INPCK;
382 cflag &= ~PARODD;
383 } else if ((flags&(EVENP|ODDP)) == ODDP) {
384 iflag |= INPCK;
385 cflag |= PARODD;
386 } else
387 iflag &= ~INPCK;
388 if (flags&TANDEM)
389 iflag |= IXOFF;
390 else
391 iflag &= ~IXOFF;
392 if ((flags&DECCTQ) == 0)
393 iflag |= IXANY;
394 else
395 iflag &= ~IXANY;
396 t->c_iflag = iflag;
397 t->c_oflag = oflag;
398 t->c_lflag = lflag;
399 t->c_cflag = cflag;
402 static void
403 ttcompatsetlflags(struct tty *tp, struct termios *t)
405 int flags = tp->t_flags;
406 tcflag_t iflag = t->c_iflag;
407 tcflag_t oflag = t->c_oflag;
408 tcflag_t lflag = t->c_lflag;
409 tcflag_t cflag = t->c_cflag;
411 iflag &= ~(PARMRK|IGNPAR|IGNCR|INLCR);
412 if (flags&CRTERA)
413 lflag |= ECHOE;
414 else
415 lflag &= ~ECHOE;
416 if (flags&CRTKIL)
417 lflag |= ECHOKE;
418 else
419 lflag &= ~ECHOKE;
420 if (flags&PRTERA)
421 lflag |= ECHOPRT;
422 else
423 lflag &= ~ECHOPRT;
424 if (flags&CTLECH)
425 lflag |= ECHOCTL;
426 else
427 lflag &= ~ECHOCTL;
428 if (flags&TANDEM)
429 iflag |= IXOFF;
430 else
431 iflag &= ~IXOFF;
432 if ((flags&DECCTQ) == 0)
433 iflag |= IXANY;
434 else
435 iflag &= ~IXANY;
436 if (flags & MDMBUF)
437 cflag |= MDMBUF;
438 else
439 cflag &= ~MDMBUF;
440 if (flags&NOHANG)
441 cflag &= ~HUPCL;
442 else
443 cflag |= HUPCL;
444 lflag &= ~(TOSTOP|FLUSHO|PENDIN|NOFLSH);
445 lflag |= flags&(TOSTOP|FLUSHO|PENDIN|NOFLSH);
448 * The next if-else statement is copied from above so don't bother
449 * checking it separately. We could avoid fiddlling with the
450 * character size if the mode is already RAW or if neither the
451 * LITOUT bit or the PASS8 bit is being changed, but the delta of
452 * the change is not available here and skipping the RAW case would
453 * make the code different from above.
455 cflag &= ~(CSIZE|PARENB);
456 if (flags&(RAW|LITOUT|PASS8)) {
457 cflag |= CS8;
458 if (!(flags&(RAW|PASS8))
459 || (flags&(RAW|PASS8|ANYP)) == (PASS8|ANYP))
460 iflag |= ISTRIP;
461 else
462 iflag &= ~ISTRIP;
463 if (flags&(RAW|LITOUT))
464 oflag &= ~OPOST;
465 else
466 oflag |= OPOST;
467 } else {
468 cflag |= CS7|PARENB;
469 iflag |= ISTRIP;
470 oflag |= OPOST;
472 t->c_iflag = iflag;
473 t->c_oflag = oflag;
474 t->c_lflag = lflag;
475 t->c_cflag = cflag;
477 #endif /* COMPAT_43 || COMPAT_SUNOS */