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