Fix some typos.
[dragonfly.git] / sys / kern / tty_compat.c
blobcc48bfe8bc3f7c4c088f97525f3f06e25c0e74db
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, "");
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 },
82 static int compatspcodes[] = {
83 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
84 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200,
87 static int
88 ttcompatspeedtab(int speed, struct speedtab *table)
90 if (speed == 0)
91 return (0); /* hangup */
92 for ( ; table->sp_speed > 0; table++)
93 if (table->sp_speed <= speed) /* nearest one, rounded down */
94 return (table->sp_code);
95 return (1); /* 50, min and not hangup */
98 int
99 ttsetcompat(struct tty *tp, u_long *com, caddr_t data, struct termios *term)
101 switch (*com) {
102 case TIOCSETP:
103 case TIOCSETN: {
104 struct sgttyb *sg = (struct sgttyb *)data;
105 int speed;
107 if ((speed = sg->sg_ispeed) > MAX_SPEED || speed < 0)
108 return(EINVAL);
109 else if (speed != ttcompatspeedtab(tp->t_ispeed, compatspeeds))
110 term->c_ispeed = compatspcodes[speed];
111 else
112 term->c_ispeed = tp->t_ispeed;
113 if ((speed = sg->sg_ospeed) > MAX_SPEED || speed < 0)
114 return(EINVAL);
115 else if (speed != ttcompatspeedtab(tp->t_ospeed, compatspeeds))
116 term->c_ospeed = compatspcodes[speed];
117 else
118 term->c_ospeed = tp->t_ospeed;
119 term->c_cc[VERASE] = sg->sg_erase;
120 term->c_cc[VKILL] = sg->sg_kill;
121 tp->t_flags = (tp->t_flags&0xffff0000) | (sg->sg_flags&0xffff);
122 ttcompatsetflags(tp, term);
123 *com = (*com == TIOCSETP) ? TIOCSETAF : TIOCSETA;
124 break;
126 case TIOCSETC: {
127 struct tchars *tc = (struct tchars *)data;
128 cc_t *cc;
130 cc = term->c_cc;
131 cc[VINTR] = tc->t_intrc;
132 cc[VQUIT] = tc->t_quitc;
133 cc[VSTART] = tc->t_startc;
134 cc[VSTOP] = tc->t_stopc;
135 cc[VEOF] = tc->t_eofc;
136 cc[VEOL] = tc->t_brkc;
137 if (tc->t_brkc == -1)
138 cc[VEOL2] = _POSIX_VDISABLE;
139 *com = TIOCSETA;
140 break;
142 case TIOCSLTC: {
143 struct ltchars *ltc = (struct ltchars *)data;
144 cc_t *cc;
146 cc = term->c_cc;
147 cc[VSUSP] = ltc->t_suspc;
148 cc[VDSUSP] = ltc->t_dsuspc;
149 cc[VREPRINT] = ltc->t_rprntc;
150 cc[VDISCARD] = ltc->t_flushc;
151 cc[VWERASE] = ltc->t_werasc;
152 cc[VLNEXT] = ltc->t_lnextc;
153 *com = TIOCSETA;
154 break;
156 case TIOCLBIS:
157 case TIOCLBIC:
158 case TIOCLSET:
159 if (*com == TIOCLSET)
160 tp->t_flags = (tp->t_flags&0xffff) | *(int *)data<<16;
161 else {
162 tp->t_flags =
163 (ttcompatgetflags(tp)&0xffff0000)|(tp->t_flags&0xffff);
164 if (*com == TIOCLBIS)
165 tp->t_flags |= *(int *)data<<16;
166 else
167 tp->t_flags &= ~(*(int *)data<<16);
169 ttcompatsetlflags(tp, term);
170 *com = TIOCSETA;
171 break;
173 return 0;
176 /*ARGSUSED*/
178 ttcompat(struct tty *tp, u_long com, caddr_t data, int flag)
180 switch (com) {
181 case TIOCSETP:
182 case TIOCSETN:
183 case TIOCSETC:
184 case TIOCSLTC:
185 case TIOCLBIS:
186 case TIOCLBIC:
187 case TIOCLSET: {
188 struct termios term;
189 int error;
191 term = tp->t_termios;
192 if ((error = ttsetcompat(tp, &com, data, &term)) != 0)
193 return error;
194 return ttioctl(tp, com, &term, flag);
196 case TIOCGETP: {
197 struct sgttyb *sg = (struct sgttyb *)data;
198 cc_t *cc = tp->t_cc;
200 sg->sg_ospeed = ttcompatspeedtab(tp->t_ospeed, compatspeeds);
201 if (tp->t_ispeed == 0)
202 sg->sg_ispeed = sg->sg_ospeed;
203 else
204 sg->sg_ispeed = ttcompatspeedtab(tp->t_ispeed, compatspeeds);
205 sg->sg_erase = cc[VERASE];
206 sg->sg_kill = cc[VKILL];
207 sg->sg_flags = tp->t_flags = ttcompatgetflags(tp);
208 break;
210 case TIOCGETC: {
211 struct tchars *tc = (struct tchars *)data;
212 cc_t *cc = tp->t_cc;
214 tc->t_intrc = cc[VINTR];
215 tc->t_quitc = cc[VQUIT];
216 tc->t_startc = cc[VSTART];
217 tc->t_stopc = cc[VSTOP];
218 tc->t_eofc = cc[VEOF];
219 tc->t_brkc = cc[VEOL];
220 break;
222 case TIOCGLTC: {
223 struct ltchars *ltc = (struct ltchars *)data;
224 cc_t *cc = tp->t_cc;
226 ltc->t_suspc = cc[VSUSP];
227 ltc->t_dsuspc = cc[VDSUSP];
228 ltc->t_rprntc = cc[VREPRINT];
229 ltc->t_flushc = cc[VDISCARD];
230 ltc->t_werasc = cc[VWERASE];
231 ltc->t_lnextc = cc[VLNEXT];
232 break;
234 case TIOCLGET:
235 tp->t_flags =
236 (ttcompatgetflags(tp) & 0xffff0000UL)
237 | (tp->t_flags & 0xffff);
238 *(int *)data = tp->t_flags>>16;
239 if (ttydebug)
240 kprintf("CLGET: returning %x\n", *(int *)data);
241 break;
243 case OTIOCGETD:
244 *(int *)data = tp->t_line ? tp->t_line : 2;
245 break;
247 case OTIOCSETD: {
248 int ldisczero = 0;
250 return (ttioctl(tp, TIOCSETD,
251 *(int *)data == 2 ? (caddr_t)&ldisczero : data, flag));
254 case OTIOCCONS:
255 *(int *)data = 1;
256 return (ttioctl(tp, TIOCCONS, data, flag));
258 default:
259 return (ENOIOCTL);
261 return (0);
264 static int
265 ttcompatgetflags(struct tty *tp)
267 tcflag_t iflag = tp->t_iflag;
268 tcflag_t lflag = tp->t_lflag;
269 tcflag_t oflag = tp->t_oflag;
270 tcflag_t cflag = tp->t_cflag;
271 int flags = 0;
273 if (iflag&IXOFF)
274 flags |= TANDEM;
275 if (iflag&ICRNL || oflag&ONLCR)
276 flags |= CRMOD;
277 if ((cflag&CSIZE) == CS8) {
278 flags |= PASS8;
279 if (iflag&ISTRIP)
280 flags |= ANYP;
282 else if (cflag&PARENB) {
283 if (iflag&INPCK) {
284 if (cflag&PARODD)
285 flags |= ODDP;
286 else
287 flags |= EVENP;
288 } else
289 flags |= EVENP | ODDP;
292 if ((lflag&ICANON) == 0) {
293 /* fudge */
294 if (iflag&(INPCK|ISTRIP|IXON) || lflag&(IEXTEN|ISIG)
295 || (cflag&(CSIZE|PARENB)) != CS8)
296 flags |= CBREAK;
297 else
298 flags |= RAW;
300 if (!(flags&RAW) && !(oflag&OPOST) && (cflag&(CSIZE|PARENB)) == CS8)
301 flags |= LITOUT;
302 if (cflag&MDMBUF)
303 flags |= MDMBUF;
304 if ((cflag&HUPCL) == 0)
305 flags |= NOHANG;
306 if (oflag&OXTABS)
307 flags |= XTABS;
308 if (lflag&ECHOE)
309 flags |= CRTERA|CRTBS;
310 if (lflag&ECHOKE)
311 flags |= CRTKIL|CRTBS;
312 if (lflag&ECHOPRT)
313 flags |= PRTERA;
314 if (lflag&ECHOCTL)
315 flags |= CTLECH;
316 if ((iflag&IXANY) == 0)
317 flags |= DECCTQ;
318 flags |= lflag&(ECHO|TOSTOP|FLUSHO|PENDIN|NOFLSH);
319 if (ttydebug)
320 kprintf("getflags: %x\n", flags);
321 return (flags);
324 static void
325 ttcompatsetflags(struct tty *tp, struct termios *t)
327 int flags = tp->t_flags;
328 tcflag_t iflag = t->c_iflag;
329 tcflag_t oflag = t->c_oflag;
330 tcflag_t lflag = t->c_lflag;
331 tcflag_t cflag = t->c_cflag;
333 if (flags & RAW) {
334 iflag = IGNBRK;
335 lflag &= ~(ECHOCTL|ISIG|ICANON|IEXTEN);
336 } else {
337 iflag &= ~(PARMRK|IGNPAR|IGNCR|INLCR);
338 iflag |= BRKINT|IXON|IMAXBEL;
339 lflag |= ISIG|IEXTEN|ECHOCTL; /* XXX was echoctl on ? */
340 if (flags & XTABS)
341 oflag |= OXTABS;
342 else
343 oflag &= ~OXTABS;
344 if (flags & CBREAK)
345 lflag &= ~ICANON;
346 else
347 lflag |= ICANON;
348 if (flags&CRMOD) {
349 iflag |= ICRNL;
350 oflag |= ONLCR;
351 } else {
352 iflag &= ~ICRNL;
353 oflag &= ~ONLCR;
356 if (flags&ECHO)
357 lflag |= ECHO;
358 else
359 lflag &= ~ECHO;
361 cflag &= ~(CSIZE|PARENB);
362 if (flags&(RAW|LITOUT|PASS8)) {
363 cflag |= CS8;
364 if (!(flags&(RAW|PASS8))
365 || (flags&(RAW|PASS8|ANYP)) == (PASS8|ANYP))
366 iflag |= ISTRIP;
367 else
368 iflag &= ~ISTRIP;
369 if (flags&(RAW|LITOUT))
370 oflag &= ~OPOST;
371 else
372 oflag |= OPOST;
373 } else {
374 cflag |= CS7|PARENB;
375 iflag |= ISTRIP;
376 oflag |= OPOST;
378 /* XXX don't set INPCK if RAW or PASS8? */
379 if ((flags&(EVENP|ODDP)) == EVENP) {
380 iflag |= INPCK;
381 cflag &= ~PARODD;
382 } else if ((flags&(EVENP|ODDP)) == ODDP) {
383 iflag |= INPCK;
384 cflag |= PARODD;
385 } else
386 iflag &= ~INPCK;
387 if (flags&TANDEM)
388 iflag |= IXOFF;
389 else
390 iflag &= ~IXOFF;
391 if ((flags&DECCTQ) == 0)
392 iflag |= IXANY;
393 else
394 iflag &= ~IXANY;
395 t->c_iflag = iflag;
396 t->c_oflag = oflag;
397 t->c_lflag = lflag;
398 t->c_cflag = cflag;
401 static void
402 ttcompatsetlflags(struct tty *tp, struct termios *t)
404 int flags = tp->t_flags;
405 tcflag_t iflag = t->c_iflag;
406 tcflag_t oflag = t->c_oflag;
407 tcflag_t lflag = t->c_lflag;
408 tcflag_t cflag = t->c_cflag;
410 iflag &= ~(PARMRK|IGNPAR|IGNCR|INLCR);
411 if (flags&CRTERA)
412 lflag |= ECHOE;
413 else
414 lflag &= ~ECHOE;
415 if (flags&CRTKIL)
416 lflag |= ECHOKE;
417 else
418 lflag &= ~ECHOKE;
419 if (flags&PRTERA)
420 lflag |= ECHOPRT;
421 else
422 lflag &= ~ECHOPRT;
423 if (flags&CTLECH)
424 lflag |= ECHOCTL;
425 else
426 lflag &= ~ECHOCTL;
427 if (flags&TANDEM)
428 iflag |= IXOFF;
429 else
430 iflag &= ~IXOFF;
431 if ((flags&DECCTQ) == 0)
432 iflag |= IXANY;
433 else
434 iflag &= ~IXANY;
435 if (flags & MDMBUF)
436 cflag |= MDMBUF;
437 else
438 cflag &= ~MDMBUF;
439 if (flags&NOHANG)
440 cflag &= ~HUPCL;
441 else
442 cflag |= HUPCL;
443 lflag &= ~(TOSTOP|FLUSHO|PENDIN|NOFLSH);
444 lflag |= flags&(TOSTOP|FLUSHO|PENDIN|NOFLSH);
447 * The next if-else statement is copied from above so don't bother
448 * checking it separately. We could avoid fiddlling with the
449 * character size if the mode is already RAW or if neither the
450 * LITOUT bit or the PASS8 bit is being changed, but the delta of
451 * the change is not available here and skipping the RAW case would
452 * make the code different from above.
454 cflag &= ~(CSIZE|PARENB);
455 if (flags&(RAW|LITOUT|PASS8)) {
456 cflag |= CS8;
457 if (!(flags&(RAW|PASS8))
458 || (flags&(RAW|PASS8|ANYP)) == (PASS8|ANYP))
459 iflag |= ISTRIP;
460 else
461 iflag &= ~ISTRIP;
462 if (flags&(RAW|LITOUT))
463 oflag &= ~OPOST;
464 else
465 oflag |= OPOST;
466 } else {
467 cflag |= CS7|PARENB;
468 iflag |= ISTRIP;
469 oflag |= OPOST;
471 t->c_iflag = iflag;
472 t->c_oflag = oflag;
473 t->c_lflag = lflag;
474 t->c_cflag = cflag;
476 #endif /* COMPAT_43 || COMPAT_SUNOS */