tty_43.c revision 1.38 1 /* $NetBSD: tty_43.c,v 1.38 2020/10/09 10:41:53 nia Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*-
30 * Copyright (c) 1982, 1986, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 * @(#)tty_compat.c 8.2 (Berkeley) 1/9/95
58 */
59
60 /*
61 * mapping routines for old line discipline (yuck)
62 */
63
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: tty_43.c,v 1.38 2020/10/09 10:41:53 nia Exp $");
66
67 #if defined(_KERNEL_OPT)
68 #include "opt_compat_netbsd.h"
69 #endif
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/ioctl.h>
74 #include <sys/proc.h>
75 #include <sys/conf.h>
76 #include <sys/tty.h>
77 #include <sys/termios.h>
78 #include <sys/file.h>
79 #include <sys/kernel.h>
80 #include <sys/syslog.h>
81 #include <sys/compat_stub.h>
82 #include <sys/module_hook.h>
83 #include <sys/ioctl_compat.h>
84
85 #include <compat/common/compat_mod.h>
86 #include <compat/sys/ttycom.h>
87
88 int ttydebug = 0;
89
90 static const struct speedtab compatspeeds[] = {
91 #define MAX_SPEED 17
92 { 115200, 17 },
93 { 57600, 16 },
94 { 38400, 15 },
95 { 19200, 14 },
96 { 9600, 13 },
97 { 4800, 12 },
98 { 2400, 11 },
99 { 1800, 10 },
100 { 1200, 9 },
101 { 600, 8 },
102 { 300, 7 },
103 { 200, 6 },
104 { 150, 5 },
105 { 134, 4 },
106 { 110, 3 },
107 { 75, 2 },
108 { 50, 1 },
109 { 0, 0 },
110 { -1, -1 },
111 };
112 static const int compatspcodes[] = {
113 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
114 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200
115 };
116
117 static int ttcompatgetflags(struct tty *);
118 static void ttcompatsetflags(struct tty *, struct termios *);
119 static void ttcompatsetlflags(struct tty *, struct termios *);
120
121 /*ARGSUSED*/
122 int
123 compat_43_ttioctl(struct tty *tp, u_long com, void *data, int flag,
124 struct lwp *l)
125 {
126
127 switch (com) {
128 case TIOCGETP: {
129 struct sgttyb *sg = (struct sgttyb *)data;
130 int speed;
131
132 mutex_spin_enter(&tty_lock);
133 speed = ttspeedtab(tp->t_ospeed, compatspeeds);
134 sg->sg_ospeed = (speed == -1) ? MAX_SPEED : speed;
135 if (tp->t_ispeed == 0)
136 sg->sg_ispeed = sg->sg_ospeed;
137 else {
138 speed = ttspeedtab(tp->t_ispeed, compatspeeds);
139 sg->sg_ispeed = (speed == -1) ? MAX_SPEED : speed;
140 }
141 sg->sg_erase = tty_getctrlchar(tp, VERASE);
142 sg->sg_kill = tty_getctrlchar(tp, VKILL);
143 sg->sg_flags = ttcompatgetflags(tp);
144 mutex_spin_exit(&tty_lock);
145 break;
146 }
147
148 case TIOCSETP:
149 case TIOCSETN: {
150 struct sgttyb *sg = (struct sgttyb *)data;
151 struct termios term;
152 int speed;
153
154 mutex_spin_enter(&tty_lock);
155 term = tp->t_termios;
156 if ((speed = sg->sg_ispeed) > MAX_SPEED || speed < 0)
157 term.c_ispeed = speed;
158 else
159 term.c_ispeed = compatspcodes[speed];
160 if ((speed = sg->sg_ospeed) > MAX_SPEED || speed < 0)
161 term.c_ospeed = speed;
162 else
163 term.c_ospeed = compatspcodes[speed];
164 term.c_cc[VERASE] = sg->sg_erase;
165 term.c_cc[VKILL] = sg->sg_kill;
166 tp->t_flags = (ttcompatgetflags(tp)&0xffff0000) | (sg->sg_flags&0xffff);
167 ttcompatsetflags(tp, &term);
168 mutex_spin_exit(&tty_lock);
169 return (ttioctl(tp, com == TIOCSETP ? TIOCSETAF : TIOCSETA,
170 (void *)&term, flag, l));
171 }
172
173 case TIOCGETC: {
174 struct tchars *tc = (struct tchars *)data;
175
176 tc->t_intrc = tty_getctrlchar(tp, VINTR);
177 tc->t_quitc = tty_getctrlchar(tp, VQUIT);
178 tc->t_startc = tty_getctrlchar(tp, VSTART);
179 tc->t_stopc = tty_getctrlchar(tp, VSTOP);
180 tc->t_eofc = tty_getctrlchar(tp, VEOF);
181 tc->t_brkc = tty_getctrlchar(tp, VEOL);
182 break;
183 }
184 case TIOCSETC: {
185 struct tchars *tc = (struct tchars *)data;
186
187 tty_setctrlchar(tp, VINTR, tc->t_intrc);
188 tty_setctrlchar(tp, VQUIT, tc->t_quitc);
189 tty_setctrlchar(tp, VSTART, tc->t_startc);
190 tty_setctrlchar(tp, VSTOP, tc->t_stopc);
191 tty_setctrlchar(tp, VEOF, tc->t_eofc);
192 tty_setctrlchar(tp, VEOL, tc->t_brkc);
193 if (tc->t_brkc == (char)-1)
194 tty_setctrlchar(tp, VEOL2, _POSIX_VDISABLE);
195 break;
196 }
197 case TIOCSLTC: {
198 struct ltchars *ltc = (struct ltchars *)data;
199
200 tty_setctrlchar(tp, VSUSP, ltc->t_suspc);
201 tty_setctrlchar(tp, VDSUSP, ltc->t_dsuspc);
202 tty_setctrlchar(tp, VREPRINT, ltc->t_rprntc);
203 tty_setctrlchar(tp, VDISCARD, ltc->t_flushc);
204 tty_setctrlchar(tp, VWERASE, ltc->t_werasc);
205 tty_setctrlchar(tp, VLNEXT, ltc->t_lnextc);
206 break;
207 }
208 case TIOCGLTC: {
209 struct ltchars *ltc = (struct ltchars *)data;
210
211 ltc->t_suspc = tty_getctrlchar(tp, VSUSP);
212 ltc->t_dsuspc = tty_getctrlchar(tp, VDSUSP);
213 ltc->t_rprntc = tty_getctrlchar(tp, VREPRINT);
214 ltc->t_flushc = tty_getctrlchar(tp, VDISCARD);
215 ltc->t_werasc = tty_getctrlchar(tp, VWERASE);
216 ltc->t_lnextc = tty_getctrlchar(tp, VLNEXT);
217 break;
218 }
219 case TIOCLBIS:
220 case TIOCLBIC:
221 case TIOCLSET: {
222 struct termios term;
223 int argbits, flags;
224
225 argbits = *(int *)data;
226 if (argbits < 0)
227 return EINVAL;
228
229 mutex_spin_enter(&tty_lock);
230 term = tp->t_termios;
231 flags = ttcompatgetflags(tp);
232 switch (com) {
233 case TIOCLSET:
234 tp->t_flags = (flags & 0xffff) | (argbits << 16);
235 break;
236 case TIOCLBIS:
237 tp->t_flags = flags | (argbits << 16);
238 break;
239 case TIOCLBIC:
240 tp->t_flags = flags & ~(argbits << 16);
241 break;
242 }
243 ttcompatsetlflags(tp, &term);
244 mutex_spin_exit(&tty_lock);
245 return (ttioctl(tp, TIOCSETA, (void *)&term, flag, l));
246 }
247 case TIOCLGET:
248 mutex_spin_enter(&tty_lock);
249 *(int *)data = ttcompatgetflags(tp)>>16;
250 mutex_spin_exit(&tty_lock);
251 if (ttydebug)
252 printf("CLGET: returning %x\n", *(int *)data);
253 break;
254
255 case OTIOCGETD:
256 mutex_spin_enter(&tty_lock);
257 *(int *)data = (tp->t_linesw == NULL || tp->t_linesw->l_no == 0)
258 ? 2 /* XXX old NTTYDISC */ : tp->t_linesw->l_no;
259 mutex_spin_exit(&tty_lock);
260 break;
261
262 case OTIOCSETD: {
263 int ldisczero = 0;
264
265 return (ttioctl(tp, TIOCSETD,
266 *(int *)data == 2 ? (void *)&ldisczero : data, flag,
267 l));
268 }
269
270 case OTIOCCONS:
271 *(int *)data = 1;
272 return (ttioctl(tp, TIOCCONS, data, flag, l));
273
274 case TIOCHPCL:
275 mutex_spin_enter(&tty_lock);
276 SET(tp->t_cflag, HUPCL);
277 mutex_spin_exit(&tty_lock);
278 break;
279
280 case TIOCGSID:
281 mutex_enter(&proc_lock);
282 if (tp->t_session == NULL) {
283 mutex_exit(&proc_lock);
284 return ENOTTY;
285 }
286 if (tp->t_session->s_leader == NULL) {
287 mutex_exit(&proc_lock);
288 return ENOTTY;
289 }
290 *(int *) data = tp->t_session->s_leader->p_pid;
291 mutex_exit(&proc_lock);
292 break;
293
294 default:
295 return (EPASSTHROUGH);
296 }
297 return (0);
298 }
299
300 static int
301 ttcompatgetflags(struct tty *tp)
302 {
303 tcflag_t iflag = tp->t_iflag;
304 tcflag_t lflag = tp->t_lflag;
305 tcflag_t oflag = tp->t_oflag;
306 tcflag_t cflag = tp->t_cflag;
307 int flags = 0;
308
309 KASSERT(mutex_owned(&tty_lock));
310
311 if (ISSET(iflag, IXOFF))
312 SET(flags, TANDEM);
313 if (ISSET(iflag, ICRNL) || ISSET(oflag, ONLCR))
314 SET(flags, CRMOD);
315 if (ISSET(cflag, PARENB)) {
316 if (ISSET(iflag, INPCK)) {
317 if (ISSET(cflag, PARODD))
318 SET(flags, ODDP);
319 else
320 SET(flags, EVENP);
321 } else
322 SET(flags, ANYP);
323 }
324
325 if (!ISSET(lflag, ICANON)) {
326 /* fudge */
327 if (ISSET(iflag, IXON) || ISSET(lflag, ISIG|IEXTEN) ||
328 ISSET(cflag, PARENB))
329 SET(flags, CBREAK);
330 else
331 SET(flags, RAW);
332 }
333
334 if (ISSET(flags, RAW))
335 SET(flags, ISSET(tp->t_flags, LITOUT|PASS8));
336 else if (ISSET(cflag, CSIZE) == CS8) {
337 if (!ISSET(oflag, OPOST))
338 SET(flags, LITOUT);
339 if (!ISSET(iflag, ISTRIP))
340 SET(flags, PASS8);
341 }
342
343 if (ISSET(cflag, MDMBUF))
344 SET(flags, MDMBUF);
345 if (!ISSET(cflag, HUPCL))
346 SET(flags, NOHANG);
347 if (ISSET(oflag, OXTABS))
348 SET(flags, XTABS);
349 if (ISSET(lflag, ECHOE))
350 SET(flags, CRTERA|CRTBS);
351 if (ISSET(lflag, ECHOKE))
352 SET(flags, CRTKIL|CRTBS);
353 if (ISSET(lflag, ECHOPRT))
354 SET(flags, PRTERA);
355 if (ISSET(lflag, ECHOCTL))
356 SET(flags, CTLECH);
357 if (!ISSET(iflag, IXANY))
358 SET(flags, DECCTQ);
359 SET(flags, ISSET(lflag, ECHO|TOSTOP|FLUSHO|PENDIN|NOFLSH));
360 if (ttydebug)
361 printf("getflags: %x\n", flags);
362 return (flags);
363 }
364
365 static void
366 ttcompatsetflags(struct tty *tp, struct termios *t)
367 {
368 int flags = tp->t_flags;
369
370 KASSERT(mutex_owned(&tty_lock));
371
372 tcflag_t iflag = t->c_iflag;
373 tcflag_t oflag = t->c_oflag;
374 tcflag_t lflag = t->c_lflag;
375 tcflag_t cflag = t->c_cflag;
376
377 if (ISSET(flags, TANDEM))
378 SET(iflag, IXOFF);
379 else
380 CLR(iflag, IXOFF);
381 if (ISSET(flags, ECHO))
382 SET(lflag, ECHO);
383 else
384 CLR(lflag, ECHO);
385 if (ISSET(flags, CRMOD)) {
386 SET(iflag, ICRNL);
387 SET(oflag, ONLCR);
388 } else {
389 CLR(iflag, ICRNL);
390 CLR(oflag, ONLCR);
391 }
392 if (ISSET(flags, XTABS))
393 SET(oflag, OXTABS);
394 else
395 CLR(oflag, OXTABS);
396
397
398 if (ISSET(flags, RAW)) {
399 iflag &= IXOFF;
400 CLR(lflag, ISIG|ICANON|IEXTEN);
401 CLR(cflag, PARENB);
402 } else {
403 SET(iflag, BRKINT|IXON|IMAXBEL);
404 SET(lflag, ISIG|IEXTEN);
405 if (ISSET(flags, CBREAK))
406 CLR(lflag, ICANON);
407 else
408 SET(lflag, ICANON);
409 switch (ISSET(flags, ANYP)) {
410 case 0:
411 CLR(cflag, PARENB);
412 break;
413 case ANYP:
414 SET(cflag, PARENB);
415 CLR(iflag, INPCK);
416 break;
417 case EVENP:
418 SET(cflag, PARENB);
419 SET(iflag, INPCK);
420 CLR(cflag, PARODD);
421 break;
422 case ODDP:
423 SET(cflag, PARENB);
424 SET(iflag, INPCK);
425 SET(cflag, PARODD);
426 break;
427 }
428 }
429
430 if (ISSET(flags, RAW|LITOUT|PASS8)) {
431 CLR(cflag, CSIZE);
432 SET(cflag, CS8);
433 if (!ISSET(flags, RAW|PASS8))
434 SET(iflag, ISTRIP);
435 else
436 CLR(iflag, ISTRIP);
437 if (!ISSET(flags, RAW|LITOUT))
438 SET(oflag, OPOST);
439 else
440 CLR(oflag, OPOST);
441 } else {
442 CLR(cflag, CSIZE);
443 SET(cflag, CS7);
444 SET(iflag, ISTRIP);
445 SET(oflag, OPOST);
446 }
447
448 t->c_iflag = iflag;
449 t->c_oflag = oflag;
450 t->c_lflag = lflag;
451 t->c_cflag = cflag;
452 }
453
454 static void
455 ttcompatsetlflags(struct tty *tp, struct termios *t)
456 {
457 int flags = tp->t_flags;
458 tcflag_t iflag = t->c_iflag;
459 tcflag_t oflag = t->c_oflag;
460 tcflag_t lflag = t->c_lflag;
461 tcflag_t cflag = t->c_cflag;
462
463 KASSERT(mutex_owned(&tty_lock));
464
465 /* Nothing we can do with CRTBS. */
466 if (ISSET(flags, PRTERA))
467 SET(lflag, ECHOPRT);
468 else
469 CLR(lflag, ECHOPRT);
470 if (ISSET(flags, CRTERA))
471 SET(lflag, ECHOE);
472 else
473 CLR(lflag, ECHOE);
474 /* Nothing we can do with TILDE. */
475 if (ISSET(flags, MDMBUF))
476 SET(cflag, MDMBUF);
477 else
478 CLR(cflag, MDMBUF);
479 if (ISSET(flags, NOHANG))
480 CLR(cflag, HUPCL);
481 else
482 SET(cflag, HUPCL);
483 if (ISSET(flags, CRTKIL))
484 SET(lflag, ECHOKE);
485 else
486 CLR(lflag, ECHOKE);
487 if (ISSET(flags, CTLECH))
488 SET(lflag, ECHOCTL);
489 else
490 CLR(lflag, ECHOCTL);
491 if (!ISSET(flags, DECCTQ))
492 SET(iflag, IXANY);
493 else
494 CLR(iflag, IXANY);
495 CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH);
496 SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
497
498 if (ISSET(flags, RAW|LITOUT|PASS8)) {
499 CLR(cflag, CSIZE);
500 SET(cflag, CS8);
501 if (!ISSET(flags, RAW|PASS8))
502 SET(iflag, ISTRIP);
503 else
504 CLR(iflag, ISTRIP);
505 if (!ISSET(flags, RAW|LITOUT))
506 SET(oflag, OPOST);
507 else
508 CLR(oflag, OPOST);
509 } else {
510 CLR(cflag, CSIZE);
511 SET(cflag, CS7);
512 SET(iflag, ISTRIP);
513 SET(oflag, OPOST);
514 }
515
516 t->c_iflag = iflag;
517 t->c_oflag = oflag;
518 t->c_lflag = lflag;
519 t->c_cflag = cflag;
520 }
521
522 int
523 kern_tty_43_init(void)
524 {
525 MODULE_HOOK_SET(tty_ttioctl_43_hook, compat_43_ttioctl);
526 return 0;
527 }
528
529 int
530 kern_tty_43_fini(void)
531 {
532 MODULE_HOOK_UNSET(tty_ttioctl_43_hook);
533 return 0;
534 }
535