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