Home | History | Annotate | Line # | Download | only in ultrix
ultrix_ioctl.c revision 1.18
      1 /*	$NetBSD: ultrix_ioctl.c,v 1.18 2001/11/13 02:09:33 lukem Exp $ */
      2 /*	from : NetBSD: sunos_ioctl.c,v 1.21 1995/10/07 06:27:31 mycroft Exp */
      3 
      4 /*
      5  * Copyright (c) 1993 Markus Wild.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. The name of the author may not be used to endorse or promote products
     14  *    derived from this software without specific prior written permission
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  * loosely from: Header: sunos_ioctl.c,v 1.7 93/05/28 04:40:43 torek Exp
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: ultrix_ioctl.c,v 1.18 2001/11/13 02:09:33 lukem Exp $");
     32 
     33 #if defined(_KERNEL_OPT)
     34 #include "opt_compat_ultrix.h"
     35 #include "opt_compat_sunos.h"
     36 #endif
     37 
     38 #include <sys/param.h>
     39 #include <sys/proc.h>
     40 #include <sys/systm.h>
     41 #include <sys/file.h>
     42 #include <sys/filedesc.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/termios.h>
     45 #include <sys/tty.h>
     46 #include <sys/socket.h>
     47 #include <sys/audioio.h>
     48 #include <net/if.h>
     49 
     50 #include <sys/mount.h>
     51 
     52 #include <compat/ultrix/ultrix_syscallargs.h>
     53 #include <sys/syscallargs.h>
     54 
     55 #include <compat/sunos/sunos.h>
     56 
     57 #include <compat/ultrix/ultrix_tty.h>
     58 
     59 #define emul_termio	ultrix_termio
     60 #define emul_termios	ultrix_termios
     61 
     62 /*
     63  * SunOS ioctl calls.
     64  * This file is something of a hodge-podge.
     65  * Support gets added as things turn up....
     66  */
     67 
     68 static struct speedtab sptab[] = {
     69 	{ 0, 0 },
     70 	{ 50, 1 },
     71 	{ 75, 2 },
     72 	{ 110, 3 },
     73 	{ 134, 4 },
     74 	{ 135, 4 },
     75 	{ 150, 5 },
     76 	{ 200, 6 },
     77 	{ 300, 7 },
     78 	{ 600, 8 },
     79 	{ 1200, 9 },
     80 	{ 1800, 10 },
     81 	{ 2400, 11 },
     82 	{ 4800, 12 },
     83 	{ 9600, 13 },
     84 	{ 19200, 14 },
     85 	{ 38400, 15 },
     86 	{ -1, -1 }
     87 };
     88 
     89 static u_long s2btab[] = {
     90 	0,
     91 	50,
     92 	75,
     93 	110,
     94 	134,
     95 	150,
     96 	200,
     97 	300,
     98 	600,
     99 	1200,
    100 	1800,
    101 	2400,
    102 	4800,
    103 	9600,
    104 	19200,
    105 	38400,
    106 };
    107 
    108 
    109 /*
    110  * Translate a single tty control char from the emulation value
    111  * to native termios, and vice-versa. Special-case
    112  * the value of POSIX_VDISABLE, mapping it to and from 0.
    113  */
    114 #define NATIVE_TO_EMUL_CC(bsd_cc) \
    115  (((bsd_cc)   != _POSIX_VDISABLE) ? (bsd_cc) : 0)
    116 
    117 #define EMUL_TO_NATIVE_CC(emul_cc) \
    118  (emul_cc) ? (emul_cc) : _POSIX_VDISABLE;
    119 
    120 
    121 static void stios2btios __P((struct emul_termios *, struct termios *));
    122 static void btios2stios __P((struct termios *, struct emul_termios *));
    123 static void stios2stio __P((struct emul_termios *, struct emul_termio *));
    124 static void stio2stios __P((struct emul_termio *, struct emul_termios *));
    125 
    126 /*
    127  * these two conversion functions have mostly been done
    128  * with some perl cut&paste, then handedited to comment
    129  * out what doesn't exist under NetBSD.
    130  * A note from Markus's code:
    131  *	(l & BITMASK1) / BITMASK1 * BITMASK2  is translated
    132  *	optimally by gcc m68k, much better than any ?: stuff.
    133  *	Code may vary with different architectures of course.
    134  *
    135  * I don't know what optimizer you used, but seeing divu's and
    136  * bfextu's in the m68k assembly output did not encourage me...
    137  * as well, gcc on the sparc definately generates much better
    138  * code with ?:.
    139  */
    140 
    141 
    142 static void
    143 stios2btios(st, bt)
    144 	struct emul_termios *st;
    145 	struct termios *bt;
    146 {
    147 	u_long l, r;
    148 
    149 	l = st->c_iflag;
    150 	r = 	((l & 0x00000001) ? IGNBRK	: 0);
    151 	r |=	((l & 0x00000002) ? BRKINT	: 0);
    152 	r |=	((l & 0x00000004) ? IGNPAR	: 0);
    153 	r |=	((l & 0x00000008) ? PARMRK	: 0);
    154 	r |=	((l & 0x00000010) ? INPCK	: 0);
    155 	r |=	((l & 0x00000020) ? ISTRIP	: 0);
    156 	r |= 	((l & 0x00000040) ? INLCR	: 0);
    157 	r |=	((l & 0x00000080) ? IGNCR	: 0);
    158 	r |=	((l & 0x00000100) ? ICRNL	: 0);
    159 	/*	((l & 0x00000200) ? IUCLC	: 0) */
    160 	r |=	((l & 0x00000400) ? IXON	: 0);
    161 	r |=	((l & 0x00000800) ? IXANY	: 0);
    162 	r |=	((l & 0x00001000) ? IXOFF	: 0);
    163 	r |=	((l & 0x00002000) ? IMAXBEL	: 0);
    164 	bt->c_iflag = r;
    165 
    166 	l = st->c_oflag;
    167 	r = 	((l & 0x00000001) ? OPOST	: 0);
    168 	/*	((l & 0x00000002) ? OLCUC	: 0) */
    169 	r |=	((l & 0x00000004) ? ONLCR	: 0);
    170 	/*	((l & 0x00000008) ? OCRNL	: 0) */
    171 	/*	((l & 0x00000010) ? ONOCR	: 0) */
    172 	/*	((l & 0x00000020) ? ONLRET	: 0) */
    173 	/*	((l & 0x00000040) ? OFILL	: 0) */
    174 	/*	((l & 0x00000080) ? OFDEL	: 0) */
    175 	/*	((l & 0x00000100) ? NLDLY	: 0) */
    176 	/*	((l & 0x00000100) ? NL1		: 0) */
    177 	/*	((l & 0x00000600) ? CRDLY	: 0) */
    178 	/*	((l & 0x00000200) ? CR1		: 0) */
    179 	/*	((l & 0x00000400) ? CR2		: 0) */
    180 	/*	((l & 0x00000600) ? CR3		: 0) */
    181 	/*	((l & 0x00001800) ? TABDLY	: 0) */
    182 	/*	((l & 0x00000800) ? TAB1	: 0) */
    183 	/*	((l & 0x00001000) ? TAB2	: 0) */
    184 	r |=	((l & 0x00001800) ? OXTABS	: 0);
    185 	/*	((l & 0x00002000) ? BSDLY	: 0) */
    186 	/*	((l & 0x00002000) ? BS1		: 0) */
    187 	/*	((l & 0x00004000) ? VTDLY	: 0) */
    188 	/*	((l & 0x00004000) ? VT1		: 0) */
    189 	/*	((l & 0x00008000) ? FFDLY	: 0) */
    190 	/*	((l & 0x00008000) ? FF1		: 0) */
    191 	/*	((l & 0x00010000) ? PAGEOUT	: 0) */
    192 	/*	((l & 0x00020000) ? WRAP	: 0) */
    193 	bt->c_oflag = r;
    194 
    195 	l = st->c_cflag;
    196 	switch (l & 0x00000030) {
    197 	case 0:
    198 		r = CS5;
    199 		break;
    200 	case 0x00000010:
    201 		r = CS6;
    202 		break;
    203 	case 0x00000020:
    204 		r = CS7;
    205 		break;
    206 	case 0x00000030:
    207 		r = CS8;
    208 		break;
    209 	}
    210 	r |=	((l & 0x00000040) ? CSTOPB	: 0);
    211 	r |=	((l & 0x00000080) ? CREAD	: 0);
    212 	r |= 	((l & 0x00000100) ? PARENB	: 0);
    213 	r |=	((l & 0x00000200) ? PARODD	: 0);
    214 	r |=	((l & 0x00000400) ? HUPCL	: 0);
    215 	r |=	((l & 0x00000800) ? CLOCAL	: 0);
    216 	/*	((l & 0x00001000) ? LOBLK	: 0) */
    217 	r |=	((l & 0x80000000) ? (CRTS_IFLOW|CCTS_OFLOW) : 0);
    218 	bt->c_cflag = r;
    219 
    220 	bt->c_ispeed = bt->c_ospeed = s2btab[l & 0x0000000f];
    221 
    222 	l = st->c_lflag;
    223 	r = 	((l & 0x00000001) ? ISIG	: 0);
    224 	r |=	((l & 0x00000002) ? ICANON	: 0);
    225 	/*	((l & 0x00000004) ? XCASE	: 0) */
    226 	r |=	((l & 0x00000008) ? ECHO	: 0);
    227 	r |=	((l & 0x00000010) ? ECHOE	: 0);
    228 	r |=	((l & 0x00000020) ? ECHOK	: 0);
    229 	r |=	((l & 0x00000040) ? ECHONL	: 0);
    230 	r |= 	((l & 0x00000080) ? NOFLSH	: 0);
    231 	r |=	((l & 0x00000100) ? TOSTOP	: 0);
    232 	r |=	((l & 0x00000200) ? ECHOCTL	: 0);
    233 	r |=	((l & 0x00000400) ? ECHOPRT	: 0);
    234 	r |=	((l & 0x00000800) ? ECHOKE	: 0);
    235 	/*	((l & 0x00001000) ? DEFECHO	: 0) */
    236 	r |=	((l & 0x00002000) ? FLUSHO	: 0);
    237 	r |=	((l & 0x00004000) ? PENDIN	: 0);
    238 	bt->c_lflag = r;
    239 
    240 	bt->c_cc[VINTR]    = EMUL_TO_NATIVE_CC(st->c_cc[0]);
    241 	bt->c_cc[VQUIT]    = EMUL_TO_NATIVE_CC(st->c_cc[1]);
    242 	bt->c_cc[VERASE]   = EMUL_TO_NATIVE_CC(st->c_cc[2]);
    243 	bt->c_cc[VKILL]    = EMUL_TO_NATIVE_CC(st->c_cc[3]);
    244 	bt->c_cc[VEOF]     = EMUL_TO_NATIVE_CC(st->c_cc[4]);
    245 	bt->c_cc[VEOL]     = EMUL_TO_NATIVE_CC(st->c_cc[5]);
    246 	bt->c_cc[VEOL2]    = EMUL_TO_NATIVE_CC(st->c_cc[6]);
    247 	/* not present on NetBSD */
    248 	/* bt->c_cc[VSWTCH]   = EMUL_TO_NATIVE_CC(st->c_cc[7]); */
    249 	bt->c_cc[VSTART]   = EMUL_TO_NATIVE_CC(st->c_cc[10]);
    250 	bt->c_cc[VSTOP]    = EMUL_TO_NATIVE_CC(st->c_cc[11]);
    251 	bt->c_cc[VSUSP]    = EMUL_TO_NATIVE_CC(st->c_cc[12]);
    252 	bt->c_cc[VDSUSP]   = EMUL_TO_NATIVE_CC(st->c_cc[13]);
    253 	bt->c_cc[VREPRINT] = EMUL_TO_NATIVE_CC(st->c_cc[14]);
    254 	bt->c_cc[VDISCARD] = EMUL_TO_NATIVE_CC(st->c_cc[15]);
    255 	bt->c_cc[VWERASE]  = EMUL_TO_NATIVE_CC(st->c_cc[16]);
    256 	bt->c_cc[VLNEXT]   = EMUL_TO_NATIVE_CC(st->c_cc[17]);
    257 	bt->c_cc[VSTATUS]  = EMUL_TO_NATIVE_CC(st->c_cc[18]);
    258 
    259 #ifdef COMPAT_ULTRIX
    260 	/* Ultrix termio/termios has real vmin/vtime */
    261 	bt->c_cc[VMIN]	   = EMUL_TO_NATIVE_CC(st->c_cc[8]);
    262 	bt->c_cc[VTIME]	   = EMUL_TO_NATIVE_CC(st->c_cc[9]);
    263 #else
    264 	/* if `raw mode', create native VMIN/VTIME from SunOS VEOF/VEOL */
    265 	bt->c_cc[VMIN]	   = (bt->c_lflag & ICANON) ? 1 : bt->c_cc[VEOF];
    266 	bt->c_cc[VTIME]	   = (bt->c_lflag & ICANON) ? 1 : bt->c_cc[VEOL];
    267 #endif
    268 
    269 }
    270 
    271 /*
    272  * Convert bsd termios to "sunos" emulated termios
    273  */
    274 static void
    275 btios2stios(bt, st)
    276 	struct termios *bt;
    277 	struct emul_termios *st;
    278 {
    279 	u_long l, r;
    280 
    281 	l = bt->c_iflag;
    282 	r = 	((l &  IGNBRK) ? 0x00000001	: 0);
    283 	r |=	((l &  BRKINT) ? 0x00000002	: 0);
    284 	r |=	((l &  IGNPAR) ? 0x00000004	: 0);
    285 	r |=	((l &  PARMRK) ? 0x00000008	: 0);
    286 	r |=	((l &   INPCK) ? 0x00000010	: 0);
    287 	r |=	((l &  ISTRIP) ? 0x00000020	: 0);
    288 	r |=	((l &   INLCR) ? 0x00000040	: 0);
    289 	r |=	((l &   IGNCR) ? 0x00000080	: 0);
    290 	r |=	((l &   ICRNL) ? 0x00000100	: 0);
    291 	/*	((l &   IUCLC) ? 0x00000200	: 0) */
    292 	r |=	((l &    IXON) ? 0x00000400	: 0);
    293 	r |=	((l &   IXANY) ? 0x00000800	: 0);
    294 	r |=	((l &   IXOFF) ? 0x00001000	: 0);
    295 	r |=	((l & IMAXBEL) ? 0x00002000	: 0);
    296 	st->c_iflag = r;
    297 
    298 	l = bt->c_oflag;
    299 	r =	((l &   OPOST) ? 0x00000001	: 0);
    300 	/*	((l &   OLCUC) ? 0x00000002	: 0) */
    301 	r |=	((l &   ONLCR) ? 0x00000004	: 0);
    302 	/*	((l &   OCRNL) ? 0x00000008	: 0) */
    303 	/*	((l &   ONOCR) ? 0x00000010	: 0) */
    304 	/*	((l &  ONLRET) ? 0x00000020	: 0) */
    305 	/*	((l &   OFILL) ? 0x00000040	: 0) */
    306 	/*	((l &   OFDEL) ? 0x00000080	: 0) */
    307 	/*	((l &   NLDLY) ? 0x00000100	: 0) */
    308 	/*	((l &     NL1) ? 0x00000100	: 0) */
    309 	/*	((l &   CRDLY) ? 0x00000600	: 0) */
    310 	/*	((l &     CR1) ? 0x00000200	: 0) */
    311 	/*	((l &     CR2) ? 0x00000400	: 0) */
    312 	/*	((l &     CR3) ? 0x00000600	: 0) */
    313 	/*	((l &  TABDLY) ? 0x00001800	: 0) */
    314 	/*	((l &    TAB1) ? 0x00000800	: 0) */
    315 	/*	((l &    TAB2) ? 0x00001000	: 0) */
    316 	r |=	((l &  OXTABS) ? 0x00001800	: 0);
    317 	/*	((l &   BSDLY) ? 0x00002000	: 0) */
    318 	/*	((l &     BS1) ? 0x00002000	: 0) */
    319 	/*	((l &   VTDLY) ? 0x00004000	: 0) */
    320 	/*	((l &     VT1) ? 0x00004000	: 0) */
    321 	/*	((l &   FFDLY) ? 0x00008000	: 0) */
    322 	/*	((l &     FF1) ? 0x00008000	: 0) */
    323 	/*	((l & PAGEOUT) ? 0x00010000	: 0) */
    324 	/*	((l &    WRAP) ? 0x00020000	: 0) */
    325 	st->c_oflag = r;
    326 
    327 	l = bt->c_cflag;
    328 	switch (l & CSIZE) {
    329 	case CS5:
    330 		r = 0;
    331 		break;
    332 	case CS6:
    333 		r = 0x00000010;
    334 		break;
    335 	case CS7:
    336 		r = 0x00000020;
    337 		break;
    338 	case CS8:
    339 		r = 0x00000030;
    340 		break;
    341 	}
    342 	r |=	((l &  CSTOPB) ? 0x00000040	: 0);
    343 	r |=	((l &   CREAD) ? 0x00000080	: 0);
    344 	r |=	((l &  PARENB) ? 0x00000100	: 0);
    345 	r |=	((l &  PARODD) ? 0x00000200	: 0);
    346 	r |=	((l &   HUPCL) ? 0x00000400	: 0);
    347 	r |=	((l &  CLOCAL) ? 0x00000800	: 0);
    348 	/*	((l &   LOBLK) ? 0x00001000	: 0) */
    349 	r |=	((l & (CRTS_IFLOW|CCTS_OFLOW)) ? 0x80000000 : 0);
    350 	st->c_cflag = r;
    351 
    352 	l = bt->c_lflag;
    353 	r =	((l &    ISIG) ? 0x00000001	: 0);
    354 	r |=	((l &  ICANON) ? 0x00000002	: 0);
    355 	/*	((l &   XCASE) ? 0x00000004	: 0) */
    356 	r |=	((l &    ECHO) ? 0x00000008	: 0);
    357 	r |=	((l &   ECHOE) ? 0x00000010	: 0);
    358 	r |=	((l &   ECHOK) ? 0x00000020	: 0);
    359 	r |=	((l &  ECHONL) ? 0x00000040	: 0);
    360 	r |=	((l &  NOFLSH) ? 0x00000080	: 0);
    361 	r |=	((l &  TOSTOP) ? 0x00000100	: 0);
    362 	r |=	((l & ECHOCTL) ? 0x00000200	: 0);
    363 	r |=	((l & ECHOPRT) ? 0x00000400	: 0);
    364 	r |=	((l &  ECHOKE) ? 0x00000800	: 0);
    365 	/*	((l & DEFECHO) ? 0x00001000	: 0) */
    366 	r |=	((l &  FLUSHO) ? 0x00002000	: 0);
    367 	r |=	((l &  PENDIN) ? 0x00004000	: 0);
    368 	st->c_lflag = r;
    369 
    370 	l = ttspeedtab(bt->c_ospeed, sptab);
    371 	if (l >= 0)
    372 		st->c_cflag |= l;
    373 
    374 	st->c_cc[0] = NATIVE_TO_EMUL_CC(bt->c_cc[VINTR]);
    375 	st->c_cc[1] = NATIVE_TO_EMUL_CC(bt->c_cc[VQUIT]);
    376 	st->c_cc[2] = NATIVE_TO_EMUL_CC(bt->c_cc[VERASE]);
    377 	st->c_cc[3] = NATIVE_TO_EMUL_CC(bt->c_cc[VKILL]);
    378 	st->c_cc[4] = NATIVE_TO_EMUL_CC(bt->c_cc[VEOF]);
    379 	st->c_cc[5] = NATIVE_TO_EMUL_CC(bt->c_cc[VEOL]);
    380 	st->c_cc[6] = NATIVE_TO_EMUL_CC(bt->c_cc[VEOL2]);
    381 
    382 	/* XXX ultrix has a VSWTCH.  NetBSD does not. */
    383 #ifdef notdef
    384 	st->c_cc[7] = NATIVE_TO_EMUL_CC(bt->c_cc[VSWTCH]);
    385 #else
    386 	st->c_cc[7] = 0;
    387 #endif
    388 	st->c_cc[10] = NATIVE_TO_EMUL_CC(bt->c_cc[VSTART]);
    389 	st->c_cc[11] = NATIVE_TO_EMUL_CC(bt->c_cc[VSTOP]);
    390 	st->c_cc[12]= NATIVE_TO_EMUL_CC(bt->c_cc[VSUSP]);
    391 	st->c_cc[13]= NATIVE_TO_EMUL_CC(bt->c_cc[VDSUSP]);
    392 	st->c_cc[14]= NATIVE_TO_EMUL_CC(bt->c_cc[VREPRINT]);
    393 	st->c_cc[15]= NATIVE_TO_EMUL_CC(bt->c_cc[VDISCARD]);
    394 	st->c_cc[16]= NATIVE_TO_EMUL_CC(bt->c_cc[VWERASE]);
    395 	st->c_cc[17]= NATIVE_TO_EMUL_CC(bt->c_cc[VLNEXT]);
    396 	st->c_cc[18]= NATIVE_TO_EMUL_CC(bt->c_cc[VSTATUS]);
    397 
    398 #ifdef COMPAT_ULTRIX
    399 	st->c_cc[8]= NATIVE_TO_EMUL_CC(bt->c_cc[VMIN]);
    400 	st->c_cc[9]= NATIVE_TO_EMUL_CC(bt->c_cc[VTIME]);
    401 #else
    402 	if (!(bt->c_lflag & ICANON)) {
    403 		/* SunOS stores VMIN/VTIME in VEOF/VEOL (if ICANON is off) */
    404 		st->c_cc[4] = bt->c_cc[VMIN];
    405 		st->c_cc[5] = bt->c_cc[VTIME];
    406 	}
    407 #endif
    408 
    409 #ifdef COMPAT_SUNOS
    410 	st->c_line = 0;	/* 4.3bsd "old" line discipline */
    411 #else
    412 	st->c_line = 2;	/* 4.3bsd "new" line discipline, Ultrix default. */
    413 #endif
    414 }
    415 
    416 #define TERMIO_NCC 10	/* ultrix termio NCC is 10 */
    417 
    418 /*
    419  * Convert emulated struct termios to termio(?)
    420  */
    421 static void
    422 stios2stio(ts, t)
    423 	struct emul_termios *ts;
    424 	struct emul_termio *t;
    425 {
    426 	t->c_iflag = ts->c_iflag;
    427 	t->c_oflag = ts->c_oflag;
    428 	t->c_cflag = ts->c_cflag;
    429 	t->c_lflag = ts->c_lflag;
    430 	t->c_line  = ts->c_line;
    431 	memcpy(t->c_cc, ts->c_cc, TERMIO_NCC);
    432 }
    433 
    434 /*
    435  * Convert the other way
    436  */
    437 static void
    438 stio2stios(t, ts)
    439 	struct emul_termio *t;
    440 	struct emul_termios *ts;
    441 {
    442 	ts->c_iflag = t->c_iflag;
    443 	ts->c_oflag = t->c_oflag;
    444 	ts->c_cflag = t->c_cflag;
    445 	ts->c_lflag = t->c_lflag;
    446 	ts->c_line  = t->c_line;
    447 	memcpy(ts->c_cc, t->c_cc, TERMIO_NCC); /* don't touch the upper fields! */
    448 }
    449 
    450 int
    451 ultrix_sys_ioctl(p, v, retval)
    452 	struct proc *p;
    453 	void *v;
    454 	register_t *retval;
    455 {
    456 	struct ultrix_sys_ioctl_args *uap = v;
    457 	struct filedesc *fdp = p->p_fd;
    458 	struct file *fp;
    459 	int (*ctl) __P((struct file *, u_long, caddr_t, struct proc *));
    460 	int error;
    461 
    462 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
    463 		return EBADF;
    464 
    465 	if ((fp->f_flag & (FREAD|FWRITE)) == 0)
    466 		return EBADF;
    467 
    468 	ctl = fp->f_ops->fo_ioctl;
    469 
    470 	switch (SCARG(uap, com)) {
    471 	case _IOR('t', 0, int):
    472 		SCARG(uap, com) = TIOCGETD;
    473 		break;
    474 	case _IOW('t', 1, int):
    475 	    {
    476 		int disc;
    477 
    478 		if ((error = copyin(SCARG(uap, data), (caddr_t)&disc,
    479 		    sizeof disc)) != 0)
    480 			return error;
    481 
    482 		/* map SunOS NTTYDISC into our termios discipline */
    483 		if (disc == 2)
    484 			disc = 0;
    485 		/* all other disciplines are not supported by NetBSD */
    486 		if (disc)
    487 			return ENXIO;
    488 
    489 		return (*ctl)(fp, TIOCSETD, (caddr_t)&disc, p);
    490 	    }
    491 	case _IOW('t', 101, int):	/* sun SUNOS_TIOCSSOFTCAR */
    492 	    {
    493 		int x;	/* unused */
    494 
    495 		return copyin((caddr_t)&x, SCARG(uap, data), sizeof x);
    496 	    }
    497 	case _IOR('t', 100, int):	/* sun SUNOS_TIOCSSOFTCAR */
    498 	    {
    499 		int x = 0;
    500 
    501 		return copyout((caddr_t)&x, SCARG(uap, data), sizeof x);
    502 	    }
    503 	case _IO('t', 36): 		/* sun TIOCCONS, no parameters */
    504 	    {
    505 		int on = 1;
    506 		return (*ctl)(fp, TIOCCONS, (caddr_t)&on, p);
    507 	    }
    508 	case _IOW('t', 37, struct sunos_ttysize):
    509 	    {
    510 		struct winsize ws;
    511 		struct sunos_ttysize ss;
    512 
    513 		if ((error = (*ctl)(fp, TIOCGWINSZ, (caddr_t)&ws, p)) != 0)
    514 			return (error);
    515 
    516 		if ((error = copyin (SCARG(uap, data), &ss, sizeof (ss))) != 0)
    517 			return error;
    518 
    519 		ws.ws_row = ss.ts_row;
    520 		ws.ws_col = ss.ts_col;
    521 
    522 		return ((*ctl)(fp, TIOCSWINSZ, (caddr_t)&ws, p));
    523 	    }
    524 	case _IOW('t', 38, struct sunos_ttysize):
    525 	    {
    526 		struct winsize ws;
    527 		struct sunos_ttysize ss;
    528 
    529 		if ((error = (*ctl)(fp, TIOCGWINSZ, (caddr_t)&ws, p)) != 0)
    530 			return (error);
    531 
    532 		ss.ts_row = ws.ws_row;
    533 		ss.ts_col = ws.ws_col;
    534 
    535 		return copyout ((caddr_t)&ss, SCARG(uap, data), sizeof (ss));
    536 	    }
    537 	case _IOW('t', 118, int):
    538 		SCARG(uap, com) = TIOCSPGRP;
    539 		break;
    540 	case _IOR('t', 119, int):
    541 		SCARG(uap, com) = TIOCGPGRP;
    542 		break;
    543 
    544 	/* Emulate termio or termios tcget() */
    545 	case ULTRIX_TCGETA:
    546 	case ULTRIX_TCGETS:
    547 	    {
    548 		struct termios bts;
    549 		struct ultrix_termios sts;
    550 		struct ultrix_termio st;
    551 
    552 		if ((error = (*ctl)(fp, TIOCGETA, (caddr_t)&bts, p)) != 0)
    553 			return error;
    554 
    555 		btios2stios (&bts, &sts);
    556 		if (SCARG(uap, com) == ULTRIX_TCGETA) {
    557 			stios2stio (&sts, &st);
    558 			return copyout((caddr_t)&st, SCARG(uap, data),
    559 			    sizeof (st));
    560 		} else
    561 			return copyout((caddr_t)&sts, SCARG(uap, data),
    562 			    sizeof (sts));
    563 		/*NOTREACHED*/
    564 	    }
    565 	/* Emulate termio tcset() */
    566 	case ULTRIX_TCSETA:
    567 	case ULTRIX_TCSETAW:
    568 	case ULTRIX_TCSETAF:
    569 	    {
    570 		struct termios bts;
    571 		struct ultrix_termios sts;
    572 		struct ultrix_termio st;
    573 		int result;
    574 
    575 		if ((error = copyin(SCARG(uap, data), (caddr_t)&st,
    576 		    sizeof (st))) != 0)
    577 			return error;
    578 
    579 		/* get full BSD termios so we don't lose information */
    580 		if ((error = (*ctl)(fp, TIOCGETA, (caddr_t)&bts, p)) != 0)
    581 			return error;
    582 
    583 		/*
    584 		 * convert to sun termios, copy in information from
    585 		 * termio, and convert back, then set new values.
    586 		 */
    587 		btios2stios(&bts, &sts);
    588 		stio2stios(&st, &sts);
    589 		stios2btios(&sts, &bts);
    590 
    591 		/*
    592 		 * map ioctl code: ultrix tcsets are numbered in reverse order
    593 		 */
    594 #ifdef notyet
    595 		return (*ctl)(fp, ULTRIX_TCSETA - SCARG(uap, com) + TIOCSETA,
    596 		    (caddr_t)&bts, p);
    597 #else
    598 		result= (*ctl)(fp, ULTRIX_TCSETA -  SCARG(uap, com) + TIOCSETA,
    599 		    (caddr_t)&bts, p);
    600 		printf("ultrix TCSETA %lx returns %d\n",
    601 		    ULTRIX_TCSETA - SCARG(uap, com), result);
    602 		return result;
    603 #endif
    604 
    605 	    }
    606 	/* Emulate termios tcset() */
    607 	case ULTRIX_TCSETS:
    608 	case ULTRIX_TCSETSW:
    609 	case ULTRIX_TCSETSF:
    610 	    {
    611 		struct termios bts;
    612 		struct ultrix_termios sts;
    613 
    614 		if ((error = copyin (SCARG(uap, data), (caddr_t)&sts,
    615 		    sizeof (sts))) != 0)
    616 			return error;
    617 		stios2btios (&sts, &bts);
    618 		return (*ctl)(fp, ULTRIX_TCSETS - SCARG(uap, com) + TIOCSETA,
    619 		    (caddr_t)&bts, p);
    620 	    }
    621 /*
    622  * Pseudo-tty ioctl translations.
    623  */
    624 	case _IOW('t', 32, int): {	/* TIOCTCNTL */
    625 		int error, on;
    626 
    627 		error = copyin (SCARG(uap, data), (caddr_t)&on, sizeof (on));
    628 		if (error != 0)
    629 			return error;
    630 		return (*ctl)(fp, TIOCUCNTL, (caddr_t)&on, p);
    631 	}
    632 	case _IOW('t', 33, int): {	/* TIOCSIGNAL */
    633 		int error, sig;
    634 
    635 		error = copyin (SCARG(uap, data), (caddr_t)&sig, sizeof (sig));
    636 		if (error != 0)
    637 			return error;
    638 		return (*ctl)(fp, TIOCSIG, (caddr_t)&sig, p);
    639 	}
    640 
    641 /*
    642  * Socket ioctl translations.
    643  */
    644 #define IN_TYPE(a, type_t) { \
    645 	type_t localbuf; \
    646 	if ((error = copyin (SCARG(uap, data), \
    647 				(caddr_t)&localbuf, sizeof (type_t))) != 0) \
    648 		return error; \
    649 	return (*ctl)(fp, a, (caddr_t)&localbuf, p); \
    650 }
    651 
    652 #define INOUT_TYPE(a, type_t) { \
    653 	type_t localbuf; \
    654 	if ((error = copyin (SCARG(uap, data), (caddr_t)&localbuf,	\
    655 			     sizeof (type_t))) != 0) \
    656 		return error; \
    657 	if ((error = (*ctl)(fp, a, (caddr_t)&localbuf, p)) != 0) \
    658 		return error; \
    659 	return copyout ((caddr_t)&localbuf, SCARG(uap, data), sizeof (type_t)); \
    660 }
    661 
    662 
    663 #define IFREQ_IN(a) { \
    664 	struct ifreq ifreq; \
    665 	if ((error = copyin (SCARG(uap, data), (caddr_t)&ifreq, sizeof (ifreq))) != 0) \
    666 		return error; \
    667 	return (*ctl)(fp, a, (caddr_t)&ifreq, p); \
    668 }
    669 
    670 #define IFREQ_INOUT(a) { \
    671 	struct ifreq ifreq; \
    672 	if ((error = copyin (SCARG(uap, data), (caddr_t)&ifreq, sizeof (ifreq))) != 0) \
    673 		return error; \
    674 	if ((error = (*ctl)(fp, a, (caddr_t)&ifreq, p)) != 0) \
    675 		return error; \
    676 	return copyout ((caddr_t)&ifreq, SCARG(uap, data), sizeof (ifreq)); \
    677 }
    678 
    679 	case _IOW('i', 12, struct ifreq):
    680 		/* SIOCSIFADDR */
    681 		break;
    682 
    683 	case _IOWR('i', 13, struct ifreq):
    684 		IFREQ_INOUT(OSIOCGIFADDR);
    685 
    686 	case _IOW('i', 14, struct ifreq):
    687 		/* SIOCSIFDSTADDR */
    688 		break;
    689 
    690 	case _IOWR('i', 15, struct ifreq):
    691 		IFREQ_INOUT(OSIOCGIFDSTADDR);
    692 
    693 	case _IOW('i', 16, struct ifreq):
    694 		/* SIOCSIFFLAGS */
    695 		break;
    696 
    697 	case _IOWR('i', 17, struct ifreq):
    698 		/* SIOCGIFFLAGS */
    699 		break;
    700 
    701 	case _IOWR('i', 18, struct ifreq):
    702 		IFREQ_INOUT(SIOCGIFBRDADDR);
    703 
    704 	case _IOWR('i', 19, struct ifreq):
    705 		IFREQ_INOUT(SIOCSIFBRDADDR);
    706 
    707 	case _IOWR('i', 21, struct ifreq):
    708 		IFREQ_INOUT(OSIOCGIFNETMASK);
    709 
    710 	case _IOW('i', 22, struct ifreq):
    711 		IFREQ_IN(SIOCSIFNETMASK);
    712 
    713 	/* 18: _IOWR('i', 18, struct ifreq):  Ultrix SIOCGIFBRDADDR */
    714 	/* 19: _IOW('i',  19, struct ifreq):  Ultrix SIOCSIFBRDADDR */
    715 	/* 20: _IOWR('i', 20, struct ifreq):  Ultrix SIOCSIFCONF */
    716 	/* 21: _IOWR('i', 21, struct ifreq):  Ultrix SIOCGIFNETMASK */
    717 	/* 22: _IOW('i',  22, struct ifreq):  Ultrix SIOCSIFNETMASK */
    718 	/* 23: _IOWR('i', 23, struct ifreq):  Ultrix SIOCSPHYADDR */
    719 	/* 24: _IOWR('i', 24, struct ifreq):  Ultrix SIOCSADDMULTI */
    720 	/* 25: _IOWR('i', 25, struct ifreq):  Ultrix SIOCSDELMULTI */
    721 
    722 	/* 30: _IOWR('i', 30, struct arpreq):  Ultrix SIOCSARP */
    723 	/* 31: _IOWR('i', 31, struct arpreq):  Ultrix SIOCGARP */
    724 	/* 32: _IOWR('i', 32, struct arpreq):  Ultrix SIOCDARP */
    725 
    726 	case _IOWR('i', 41, struct ifreq):
    727 		IFREQ_INOUT(SIOCGIFMETRIC);
    728 
    729 	case _IOWR('i', 42, struct ifreq):
    730 		IFREQ_IN(SIOCSIFMETRIC);
    731 
    732 	case _IOW('i', 30, struct arpreq):
    733 		/* SIOCSARP */
    734 		break;
    735 
    736 	case _IOWR('i', 31, struct arpreq):
    737 		/* SIOCGARP */
    738 		break;
    739 
    740 	case _IOW('i', 32, struct arpreq):
    741 		/* SIOCDARP */
    742 		break;
    743 
    744 	case _IOW('i',  26, struct ifreq):	/* SIOCSIFRDCTRS? */
    745 	case _IOWR('i', 27, struct ifreq):	/* SIOCGIFZCTRS? */
    746 	case _IOWR('i', 28, struct ifreq):	/* read physaddr ? */
    747 
    748 	case _IOW('i', 40, struct ifreq):	/* SIOCARPREQ */
    749 	case _IOW('i', 44, struct ifreq):	/* SIOCSETSYNC */
    750 	case _IOWR('i', 45, struct ifreq):	/* SIOCGETSYNC */
    751 	case _IOWR('i', 46, struct ifreq):	/* SIOCSDSTATS */
    752 	case _IOWR('i', 47, struct ifreq):	/* SIOCSESTATS */
    753 	case _IOW('i', 48, int):		/* SIOCSPROMISC */
    754 		return EOPNOTSUPP;
    755 
    756 	/* emulate for vat, vic tools */
    757 	case _IOW('i', 49, struct ifreq):	/* SIOCADDMULTI */
    758 	case _IOW('i', 50, struct ifreq):	/* SIOCDELMULTI */
    759 		return EOPNOTSUPP;
    760 
    761 	case _IOWR('i', 20, struct ifconf):	/* SIOCGIFCONF */
    762 	    {
    763 		struct ifconf ifconf;
    764 
    765 		/*
    766 		 * XXX: two more problems
    767 		 * 1. our sockaddr's are variable length, not always sizeof(sockaddr)
    768 		 * 2. this returns a name per protocol, ie. it returns two "lo0"'s
    769 		 */
    770 		error = copyin (SCARG(uap, data), (caddr_t)&ifconf,
    771 		    sizeof (ifconf));
    772 		if (error)
    773 			return error;
    774 		error = (*ctl)(fp, OSIOCGIFCONF, (caddr_t)&ifconf, p);
    775 		if (error)
    776 			return error;
    777 		return copyout ((caddr_t)&ifconf, SCARG(uap, data),
    778 		    sizeof (ifconf));
    779 	    }
    780 
    781 	}
    782 	return (sys_ioctl(p, uap, retval));
    783 }
    784