Home | History | Annotate | Line # | Download | only in sunos
sunos_ioctl.c revision 1.67
      1 /*	$NetBSD: sunos_ioctl.c,v 1.66 2015/12/11 08:19:07 mlelstv Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993 Markus Wild.
      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. The name of the author may not be used to endorse or promote products
     13  *    derived from this software without specific prior written permission
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  *
     26  * loosely from: Header: sunos_ioctl.c,v 1.7 93/05/28 04:40:43 torek Exp
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: sunos_ioctl.c,v 1.66 2015/12/11 08:19:07 mlelstv Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/proc.h>
     34 #include <sys/systm.h>
     35 #include <sys/file.h>
     36 #include <sys/filedesc.h>
     37 #include <sys/ioctl.h>
     38 #include <sys/termios.h>
     39 #include <sys/tty.h>
     40 #include <sys/socket.h>
     41 #include <sys/audioio.h>
     42 #include <sys/vnode.h>
     43 #include <sys/mount.h>
     44 #include <sys/disklabel.h>
     45 #include <sys/syscallargs.h>
     46 
     47 #include <miscfs/specfs/specdev.h>
     48 
     49 #include <net/if.h>
     50 
     51 #include <compat/sys/sockio.h>
     52 
     53 #include <dev/sun/disklabel.h>
     54 
     55 #include <compat/sunos/sunos.h>
     56 #include <compat/sunos/sunos_syscallargs.h>
     57 #include <compat/common/compat_util.h>
     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 const 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 const 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 static void stios2btios(struct sunos_termios *, struct termios *);
    106 static void btios2stios(struct termios *, struct sunos_termios *);
    107 static void stios2stio(struct sunos_termios *, struct sunos_termio *);
    108 static void stio2stios(struct sunos_termio *, struct sunos_termios *);
    109 
    110 /*
    111  * These two conversion functions have mostly been done
    112  * with some perl cut&paste, then hand-edited to comment
    113  * out what doesn't exist under NetBSD.
    114  * A note from Markus's code:
    115  *	(l & BITMASK1) / BITMASK1 * BITMASK2  is translated
    116  *	optimally by gcc m68k, much better than any ?: stuff.
    117  *	Code may vary with different architectures of course.
    118  *
    119  * I don't know what optimizer you used, but seeing divu's and
    120  * bfextu's in the m68k assembly output did not encourage me...
    121  * as well, gcc on the sparc definitely generates much better
    122  * code with `?:'.
    123  */
    124 
    125 static void
    126 stios2btios(struct sunos_termios *st, struct termios *bt)
    127 {
    128 	u_long l, r;
    129 
    130 	l = st->c_iflag;
    131 	r = 	((l & 0x00000001) ? IGNBRK	: 0);
    132 	r |=	((l & 0x00000002) ? BRKINT	: 0);
    133 	r |=	((l & 0x00000004) ? IGNPAR	: 0);
    134 	r |=	((l & 0x00000008) ? PARMRK	: 0);
    135 	r |=	((l & 0x00000010) ? INPCK	: 0);
    136 	r |=	((l & 0x00000020) ? ISTRIP	: 0);
    137 	r |= 	((l & 0x00000040) ? INLCR	: 0);
    138 	r |=	((l & 0x00000080) ? IGNCR	: 0);
    139 	r |=	((l & 0x00000100) ? ICRNL	: 0);
    140 	/*	((l & 0x00000200) ? IUCLC	: 0) */
    141 	r |=	((l & 0x00000400) ? IXON	: 0);
    142 	r |=	((l & 0x00000800) ? IXANY	: 0);
    143 	r |=	((l & 0x00001000) ? IXOFF	: 0);
    144 	r |=	((l & 0x00002000) ? IMAXBEL	: 0);
    145 	bt->c_iflag = r;
    146 
    147 	l = st->c_oflag;
    148 	r = 	((l & 0x00000001) ? OPOST	: 0);
    149 	/*	((l & 0x00000002) ? OLCUC	: 0) */
    150 	r |=	((l & 0x00000004) ? ONLCR	: 0);
    151 	/*	((l & 0x00000008) ? OCRNL	: 0) */
    152 	/*	((l & 0x00000010) ? ONOCR	: 0) */
    153 	/*	((l & 0x00000020) ? ONLRET	: 0) */
    154 	/*	((l & 0x00000040) ? OFILL	: 0) */
    155 	/*	((l & 0x00000080) ? OFDEL	: 0) */
    156 	/*	((l & 0x00000100) ? NLDLY	: 0) */
    157 	/*	((l & 0x00000100) ? NL1		: 0) */
    158 	/*	((l & 0x00000600) ? CRDLY	: 0) */
    159 	/*	((l & 0x00000200) ? CR1		: 0) */
    160 	/*	((l & 0x00000400) ? CR2		: 0) */
    161 	/*	((l & 0x00000600) ? CR3		: 0) */
    162 	/*	((l & 0x00001800) ? TABDLY	: 0) */
    163 	/*	((l & 0x00000800) ? TAB1	: 0) */
    164 	/*	((l & 0x00001000) ? TAB2	: 0) */
    165 	r |=	((l & 0x00001800) ? OXTABS	: 0);
    166 	/*	((l & 0x00002000) ? BSDLY	: 0) */
    167 	/*	((l & 0x00002000) ? BS1		: 0) */
    168 	/*	((l & 0x00004000) ? VTDLY	: 0) */
    169 	/*	((l & 0x00004000) ? VT1		: 0) */
    170 	/*	((l & 0x00008000) ? FFDLY	: 0) */
    171 	/*	((l & 0x00008000) ? FF1		: 0) */
    172 	/*	((l & 0x00010000) ? PAGEOUT	: 0) */
    173 	/*	((l & 0x00020000) ? WRAP	: 0) */
    174 	bt->c_oflag = r;
    175 
    176 	l = st->c_cflag;
    177 	switch (l & 0x00000030) {
    178 	case 0:
    179 		r = CS5;
    180 		break;
    181 	case 0x00000010:
    182 		r = CS6;
    183 		break;
    184 	case 0x00000020:
    185 		r = CS7;
    186 		break;
    187 	case 0x00000030:
    188 		r = CS8;
    189 		break;
    190 	}
    191 	r |=	((l & 0x00000040) ? CSTOPB	: 0);
    192 	r |=	((l & 0x00000080) ? CREAD	: 0);
    193 	r |= 	((l & 0x00000100) ? PARENB	: 0);
    194 	r |=	((l & 0x00000200) ? PARODD	: 0);
    195 	r |=	((l & 0x00000400) ? HUPCL	: 0);
    196 	r |=	((l & 0x00000800) ? CLOCAL	: 0);
    197 	/*	((l & 0x00001000) ? LOBLK	: 0) */
    198 	r |=	((l & 0x80000000) ? (CRTS_IFLOW|CCTS_OFLOW) : 0);
    199 	bt->c_cflag = r;
    200 
    201 	bt->c_ispeed = bt->c_ospeed = s2btab[l & 0x0000000f];
    202 
    203 	l = st->c_lflag;
    204 	r = 	((l & 0x00000001) ? ISIG	: 0);
    205 	r |=	((l & 0x00000002) ? ICANON	: 0);
    206 	/*	((l & 0x00000004) ? XCASE	: 0) */
    207 	r |=	((l & 0x00000008) ? ECHO	: 0);
    208 	r |=	((l & 0x00000010) ? ECHOE	: 0);
    209 	r |=	((l & 0x00000020) ? ECHOK	: 0);
    210 	r |=	((l & 0x00000040) ? ECHONL	: 0);
    211 	r |= 	((l & 0x00000080) ? NOFLSH	: 0);
    212 	r |=	((l & 0x00000100) ? TOSTOP	: 0);
    213 	r |=	((l & 0x00000200) ? ECHOCTL	: 0);
    214 	r |=	((l & 0x00000400) ? ECHOPRT	: 0);
    215 	r |=	((l & 0x00000800) ? ECHOKE	: 0);
    216 	/*	((l & 0x00001000) ? DEFECHO	: 0) */
    217 	r |=	((l & 0x00002000) ? FLUSHO	: 0);
    218 	r |=	((l & 0x00004000) ? PENDIN	: 0);
    219 	bt->c_lflag = r;
    220 
    221 	bt->c_cc[VINTR]    = st->c_cc[0]  ? st->c_cc[0]  : _POSIX_VDISABLE;
    222 	bt->c_cc[VQUIT]    = st->c_cc[1]  ? st->c_cc[1]  : _POSIX_VDISABLE;
    223 	bt->c_cc[VERASE]   = st->c_cc[2]  ? st->c_cc[2]  : _POSIX_VDISABLE;
    224 	bt->c_cc[VKILL]    = st->c_cc[3]  ? st->c_cc[3]  : _POSIX_VDISABLE;
    225 	bt->c_cc[VEOF]     = st->c_cc[4]  ? st->c_cc[4]  : _POSIX_VDISABLE;
    226 	bt->c_cc[VEOL]     = st->c_cc[5]  ? st->c_cc[5]  : _POSIX_VDISABLE;
    227 	bt->c_cc[VEOL2]    = st->c_cc[6]  ? st->c_cc[6]  : _POSIX_VDISABLE;
    228     /*	bt->c_cc[VSWTCH]   = st->c_cc[7]  ? st->c_cc[7]  : _POSIX_VDISABLE; */
    229 	bt->c_cc[VSTART]   = st->c_cc[8]  ? st->c_cc[8]  : _POSIX_VDISABLE;
    230 	bt->c_cc[VSTOP]    = st->c_cc[9]  ? st->c_cc[9]  : _POSIX_VDISABLE;
    231 	bt->c_cc[VSUSP]    = st->c_cc[10] ? st->c_cc[10] : _POSIX_VDISABLE;
    232 	bt->c_cc[VDSUSP]   = st->c_cc[11] ? st->c_cc[11] : _POSIX_VDISABLE;
    233 	bt->c_cc[VREPRINT] = st->c_cc[12] ? st->c_cc[12] : _POSIX_VDISABLE;
    234 	bt->c_cc[VDISCARD] = st->c_cc[13] ? st->c_cc[13] : _POSIX_VDISABLE;
    235 	bt->c_cc[VWERASE]  = st->c_cc[14] ? st->c_cc[14] : _POSIX_VDISABLE;
    236 	bt->c_cc[VLNEXT]   = st->c_cc[15] ? st->c_cc[15] : _POSIX_VDISABLE;
    237 	bt->c_cc[VSTATUS]  = st->c_cc[16] ? st->c_cc[16] : _POSIX_VDISABLE;
    238 
    239 	/* if `raw mode', create native VMIN/VTIME from SunOS VEOF/VEOL */
    240 	bt->c_cc[VMIN]	   = (bt->c_lflag & ICANON) ? 1 : bt->c_cc[VEOF];
    241 	bt->c_cc[VTIME]	   = (bt->c_lflag & ICANON) ? 1 : bt->c_cc[VEOL];
    242 }
    243 
    244 
    245 static void
    246 btios2stios(struct termios *bt, struct sunos_termios *st)
    247 {
    248 	u_long l, r;
    249 	int s;
    250 
    251 	l = bt->c_iflag;
    252 	r = 	((l &  IGNBRK) ? 0x00000001	: 0);
    253 	r |=	((l &  BRKINT) ? 0x00000002	: 0);
    254 	r |=	((l &  IGNPAR) ? 0x00000004	: 0);
    255 	r |=	((l &  PARMRK) ? 0x00000008	: 0);
    256 	r |=	((l &   INPCK) ? 0x00000010	: 0);
    257 	r |=	((l &  ISTRIP) ? 0x00000020	: 0);
    258 	r |=	((l &   INLCR) ? 0x00000040	: 0);
    259 	r |=	((l &   IGNCR) ? 0x00000080	: 0);
    260 	r |=	((l &   ICRNL) ? 0x00000100	: 0);
    261 	/*	((l &   IUCLC) ? 0x00000200	: 0) */
    262 	r |=	((l &    IXON) ? 0x00000400	: 0);
    263 	r |=	((l &   IXANY) ? 0x00000800	: 0);
    264 	r |=	((l &   IXOFF) ? 0x00001000	: 0);
    265 	r |=	((l & IMAXBEL) ? 0x00002000	: 0);
    266 	st->c_iflag = r;
    267 
    268 	l = bt->c_oflag;
    269 	r =	((l &   OPOST) ? 0x00000001	: 0);
    270 	/*	((l &   OLCUC) ? 0x00000002	: 0) */
    271 	r |=	((l &   ONLCR) ? 0x00000004	: 0);
    272 	/*	((l &   OCRNL) ? 0x00000008	: 0) */
    273 	/*	((l &   ONOCR) ? 0x00000010	: 0) */
    274 	/*	((l &  ONLRET) ? 0x00000020	: 0) */
    275 	/*	((l &   OFILL) ? 0x00000040	: 0) */
    276 	/*	((l &   OFDEL) ? 0x00000080	: 0) */
    277 	/*	((l &   NLDLY) ? 0x00000100	: 0) */
    278 	/*	((l &     NL1) ? 0x00000100	: 0) */
    279 	/*	((l &   CRDLY) ? 0x00000600	: 0) */
    280 	/*	((l &     CR1) ? 0x00000200	: 0) */
    281 	/*	((l &     CR2) ? 0x00000400	: 0) */
    282 	/*	((l &     CR3) ? 0x00000600	: 0) */
    283 	/*	((l &  TABDLY) ? 0x00001800	: 0) */
    284 	/*	((l &    TAB1) ? 0x00000800	: 0) */
    285 	/*	((l &    TAB2) ? 0x00001000	: 0) */
    286 	r |=	((l &  OXTABS) ? 0x00001800	: 0);
    287 	/*	((l &   BSDLY) ? 0x00002000	: 0) */
    288 	/*	((l &     BS1) ? 0x00002000	: 0) */
    289 	/*	((l &   VTDLY) ? 0x00004000	: 0) */
    290 	/*	((l &     VT1) ? 0x00004000	: 0) */
    291 	/*	((l &   FFDLY) ? 0x00008000	: 0) */
    292 	/*	((l &     FF1) ? 0x00008000	: 0) */
    293 	/*	((l & PAGEOUT) ? 0x00010000	: 0) */
    294 	/*	((l &    WRAP) ? 0x00020000	: 0) */
    295 	st->c_oflag = r;
    296 
    297 	l = bt->c_cflag;
    298 	switch (l & CSIZE) {
    299 	case CS5:
    300 		r = 0;
    301 		break;
    302 	case CS6:
    303 		r = 0x00000010;
    304 		break;
    305 	case CS7:
    306 		r = 0x00000020;
    307 		break;
    308 	case CS8:
    309 		r = 0x00000030;
    310 		break;
    311 	}
    312 	r |=	((l &  CSTOPB) ? 0x00000040	: 0);
    313 	r |=	((l &   CREAD) ? 0x00000080	: 0);
    314 	r |=	((l &  PARENB) ? 0x00000100	: 0);
    315 	r |=	((l &  PARODD) ? 0x00000200	: 0);
    316 	r |=	((l &   HUPCL) ? 0x00000400	: 0);
    317 	r |=	((l &  CLOCAL) ? 0x00000800	: 0);
    318 	/*	((l &   LOBLK) ? 0x00001000	: 0) */
    319 	r |=	((l & (CRTS_IFLOW|CCTS_OFLOW)) ? 0x80000000 : 0);
    320 	st->c_cflag = r;
    321 
    322 	l = bt->c_lflag;
    323 	r =	((l &    ISIG) ? 0x00000001	: 0);
    324 	r |=	((l &  ICANON) ? 0x00000002	: 0);
    325 	/*	((l &   XCASE) ? 0x00000004	: 0) */
    326 	r |=	((l &    ECHO) ? 0x00000008	: 0);
    327 	r |=	((l &   ECHOE) ? 0x00000010	: 0);
    328 	r |=	((l &   ECHOK) ? 0x00000020	: 0);
    329 	r |=	((l &  ECHONL) ? 0x00000040	: 0);
    330 	r |=	((l &  NOFLSH) ? 0x00000080	: 0);
    331 	r |=	((l &  TOSTOP) ? 0x00000100	: 0);
    332 	r |=	((l & ECHOCTL) ? 0x00000200	: 0);
    333 	r |=	((l & ECHOPRT) ? 0x00000400	: 0);
    334 	r |=	((l &  ECHOKE) ? 0x00000800	: 0);
    335 	/*	((l & DEFECHO) ? 0x00001000	: 0) */
    336 	r |=	((l &  FLUSHO) ? 0x00002000	: 0);
    337 	r |=	((l &  PENDIN) ? 0x00004000	: 0);
    338 	st->c_lflag = r;
    339 
    340 	s = ttspeedtab(bt->c_ospeed, sptab);
    341 	if (s >= 0)
    342 		st->c_cflag |= s;
    343 
    344 	st->c_cc[0] = bt->c_cc[VINTR]   != _POSIX_VDISABLE? bt->c_cc[VINTR]:0;
    345 	st->c_cc[1] = bt->c_cc[VQUIT]   != _POSIX_VDISABLE? bt->c_cc[VQUIT]:0;
    346 	st->c_cc[2] = bt->c_cc[VERASE]  != _POSIX_VDISABLE? bt->c_cc[VERASE]:0;
    347 	st->c_cc[3] = bt->c_cc[VKILL]   != _POSIX_VDISABLE? bt->c_cc[VKILL]:0;
    348 	st->c_cc[4] = bt->c_cc[VEOF]    != _POSIX_VDISABLE? bt->c_cc[VEOF]:0;
    349 	st->c_cc[5] = bt->c_cc[VEOL]    != _POSIX_VDISABLE? bt->c_cc[VEOL]:0;
    350 	st->c_cc[6] = bt->c_cc[VEOL2]   != _POSIX_VDISABLE? bt->c_cc[VEOL2]:0;
    351 	st->c_cc[7] = 0;
    352 		/*    bt->c_cc[VSWTCH]  != _POSIX_VDISABLE? bt->c_cc[VSWTCH]: */
    353 	st->c_cc[8] = bt->c_cc[VSTART]  != _POSIX_VDISABLE? bt->c_cc[VSTART]:0;
    354 	st->c_cc[9] = bt->c_cc[VSTOP]   != _POSIX_VDISABLE? bt->c_cc[VSTOP]:0;
    355 	st->c_cc[10]= bt->c_cc[VSUSP]   != _POSIX_VDISABLE? bt->c_cc[VSUSP]:0;
    356 	st->c_cc[11]= bt->c_cc[VDSUSP]  != _POSIX_VDISABLE? bt->c_cc[VDSUSP]:0;
    357 	st->c_cc[12]= bt->c_cc[VREPRINT]!= _POSIX_VDISABLE? bt->c_cc[VREPRINT]:0;
    358 	st->c_cc[13]= bt->c_cc[VDISCARD]!= _POSIX_VDISABLE? bt->c_cc[VDISCARD]:0;
    359 	st->c_cc[14]= bt->c_cc[VWERASE] != _POSIX_VDISABLE? bt->c_cc[VWERASE]:0;
    360 	st->c_cc[15]= bt->c_cc[VLNEXT]  != _POSIX_VDISABLE? bt->c_cc[VLNEXT]:0;
    361 	st->c_cc[16]= bt->c_cc[VSTATUS] != _POSIX_VDISABLE? bt->c_cc[VSTATUS]:0;
    362 
    363 	if (!(bt->c_lflag & ICANON)) {
    364 		/* SunOS stores VMIN/VTIME in VEOF/VEOL (if ICANON is off) */
    365 		st->c_cc[4] = bt->c_cc[VMIN];
    366 		st->c_cc[5] = bt->c_cc[VTIME];
    367 	}
    368 
    369 	st->c_line = 0;
    370 }
    371 
    372 static void
    373 stios2stio(struct sunos_termios *ts, struct sunos_termio *t)
    374 {
    375 	t->c_iflag = ts->c_iflag;
    376 	t->c_oflag = ts->c_oflag;
    377 	t->c_cflag = ts->c_cflag;
    378 	t->c_lflag = ts->c_lflag;
    379 	t->c_line  = ts->c_line;
    380 	memcpy(t->c_cc, ts->c_cc, 8);
    381 }
    382 
    383 static void
    384 stio2stios(struct sunos_termio *t, struct sunos_termios *ts)
    385 {
    386 	ts->c_iflag = t->c_iflag;
    387 	ts->c_oflag = t->c_oflag;
    388 	ts->c_cflag = t->c_cflag;
    389 	ts->c_lflag = t->c_lflag;
    390 	ts->c_line  = t->c_line;
    391 	memcpy(ts->c_cc, t->c_cc, 8); /* don't touch the upper fields! */
    392 }
    393 
    394 int
    395 sunos_sys_ioctl(struct lwp *l, const struct sunos_sys_ioctl_args *uap, register_t *retval)
    396 {
    397 	/* {
    398 		int	fd;
    399 		u_long	com;
    400 		void *	data;
    401 	} */
    402 	file_t *fp;
    403 	int (*ctl)(struct file *, u_long, void *);
    404 	struct sys_ioctl_args pass_ua;
    405 	int error;
    406 
    407 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
    408 		return EBADF;
    409 
    410 	if ((fp->f_flag & (FREAD|FWRITE)) == 0) {
    411 		error = EBADF;
    412 		goto out;
    413 	}
    414 
    415 	error = EPASSTHROUGH;
    416 	SCARG(&pass_ua, com) = SCARG(uap, com);
    417 	ctl = fp->f_ops->fo_ioctl;
    418 
    419 	switch (SCARG(uap, com)) {
    420 	case _IOR('t', 0, int):
    421 		SCARG(&pass_ua, com) = TIOCGETD;
    422 		break;
    423 	case _IOW('t', 1, int):
    424 	    {
    425 		int disc;
    426 
    427 		if ((error = copyin(SCARG(uap, data), (void *)&disc,
    428 		    sizeof disc)) != 0)
    429 			break;
    430 
    431 		/* map SunOS NTTYDISC into our termios discipline */
    432 		if (disc == 2)
    433 			disc = 0;
    434 		/* all other disciplines are not supported by NetBSD */
    435 		if (disc) {
    436 			error = ENXIO;
    437 			break;
    438 		}
    439 
    440 		error = (*ctl)(fp, TIOCSETD, &disc);
    441 	    }
    442 	case _IOW('t', 101, int):	/* sun SUNOS_TIOCSSOFTCAR */
    443 	    {
    444 		int x;	/* unused */
    445 
    446 		error = copyin((void *)&x, SCARG(uap, data), sizeof x);
    447 		break;
    448 	    }
    449 	case _IOR('t', 100, int):	/* sun SUNOS_TIOCSSOFTCAR */
    450 	    {
    451 		int x = 0;
    452 
    453 		error = copyout((void *)&x, SCARG(uap, data), sizeof x);
    454 		break;
    455 	    }
    456 	case _IO('t', 36): 		/* sun TIOCCONS, no parameters */
    457 	    {
    458 		int on = 1;
    459 		error = (*ctl)(fp, TIOCCONS, &on);
    460 		break;
    461 	    }
    462 	case _IOW('t', 37, struct sunos_ttysize):
    463 	    {
    464 		struct winsize ws;
    465 		struct sunos_ttysize ss;
    466 
    467 		if ((error = (*ctl)(fp, TIOCGWINSZ, &ws)) != 0)
    468 			break;
    469 
    470 		if ((error = copyin (SCARG(uap, data), &ss, sizeof (ss))) != 0)
    471 			break;
    472 
    473 		ws.ws_row = ss.ts_row;
    474 		ws.ws_col = ss.ts_col;
    475 
    476 		error = (*ctl)(fp, TIOCSWINSZ, &ws);
    477 		break;
    478 	    }
    479 	case _IOW('t', 38, struct sunos_ttysize):
    480 	    {
    481 		struct winsize ws;
    482 		struct sunos_ttysize ss;
    483 
    484 		if ((error = (*ctl)(fp, TIOCGWINSZ, &ws)) != 0)
    485 			break;
    486 
    487 		ss.ts_row = ws.ws_row;
    488 		ss.ts_col = ws.ws_col;
    489 
    490 		error = copyout((void *)&ss, SCARG(uap, data), sizeof (ss));
    491 		break;
    492 	    }
    493 	case _IOR('t', 119, int):	/* TIOCGPGRP */
    494 	    {
    495 		int pgrp;
    496 
    497 		error = (*ctl)(fp, TIOCGPGRP, &pgrp);
    498 		if (error == 0 && pgrp == 0)
    499 			error = EIO;
    500 		if (error)
    501 			break;
    502 		error = copyout((void *)&pgrp, SCARG(uap, data), sizeof(pgrp));
    503 		break;
    504 	    }
    505 	case _IOW('t', 130, int):	/* TIOCSETPGRP: posix variant */
    506 		SCARG(&pass_ua, com) = TIOCSPGRP;
    507 		break;
    508 	case _IOR('t', 131, int):	/* TIOCGETPGRP: posix variant */
    509 	    {
    510 		/*
    511 		 * sigh, must do error translation on pty devices.  if the pgrp
    512 		 * returned is 0 (and no error), we convert this to EIO, if it
    513 		 * is on a pty.
    514 		 */
    515 		int pgrp;
    516 		struct vnode *vp = NULL;
    517 
    518 		error = (*ctl)(fp, TIOCGPGRP, &pgrp);
    519 		if (error) {
    520 			if (fp->f_type == DTYPE_VNODE)
    521 				vp = fp->f_vnode;
    522 			if ((error == EIO || (error == 0 && pgrp == 0)) &&
    523 			    vp != NULL &&
    524 			    vp->v_type == VCHR &&
    525 			    major(vp->v_rdev) == 21)
    526 				error = ENOTTY;
    527 			break;
    528 		}
    529 		error = copyout((void *)&pgrp, SCARG(uap, data), sizeof(pgrp));
    530 		break;
    531 	    }
    532 	case _IO('t', 132):
    533 		SCARG(&pass_ua, com) = TIOCSCTTY;
    534 		break;
    535 	case SUNOS_TCGETA:
    536 	case SUNOS_TCGETS:
    537 	    {
    538 		struct termios bts;
    539 		struct sunos_termios sts;
    540 		struct sunos_termio st;
    541 
    542 		if ((error = (*ctl)(fp, TIOCGETA, &bts)) != 0)
    543 			break;
    544 
    545 		btios2stios (&bts, &sts);
    546 		if (SCARG(uap, com) == SUNOS_TCGETA) {
    547 			stios2stio (&sts, &st);
    548 			error = copyout((void *)&st, SCARG(uap, data),
    549 			    sizeof (st));
    550 		} else
    551 			error = copyout((void *)&sts, SCARG(uap, data),
    552 			    sizeof (sts));
    553 		break;
    554 	    }
    555 	case SUNOS_TCSETA:
    556 	case SUNOS_TCSETAW:
    557 	case SUNOS_TCSETAF:
    558 	    {
    559 		struct termios bts;
    560 		struct sunos_termios sts;
    561 		struct sunos_termio st;
    562 
    563 		if ((error = copyin(SCARG(uap, data), &st, sizeof (st))) != 0)
    564 			break;
    565 
    566 		/* get full BSD termios so we don't lose information */
    567 		if ((error = (*ctl)(fp, TIOCGETA, &bts)) != 0)
    568 			break;
    569 
    570 		/*
    571 		 * convert to sun termios, copy in information from
    572 		 * termio, and convert back, then set new values.
    573 		 */
    574 		btios2stios(&bts, &sts);
    575 		stio2stios(&st, &sts);
    576 		stios2btios(&sts, &bts);
    577 
    578 		error = (*ctl)(fp, SCARG(uap, com) - SUNOS_TCSETA + TIOCSETA,
    579 		    &bts);
    580 		break;
    581 	    }
    582 	case SUNOS_TCSETS:
    583 	case SUNOS_TCSETSW:
    584 	case SUNOS_TCSETSF:
    585 	    {
    586 		struct termios bts;
    587 		struct sunos_termios sts;
    588 
    589 		if ((error = copyin (SCARG(uap, data), (void *)&sts,
    590 		    sizeof (sts))) != 0)
    591 			break;
    592 		stios2btios (&sts, &bts);
    593 		error = (*ctl)(fp, SCARG(uap, com) - SUNOS_TCSETS + TIOCSETA,
    594 		    &bts);
    595 		break;
    596 	    }
    597 /*
    598  * Pseudo-tty ioctl translations.
    599  */
    600 	case _IOW('t', 32, int): {	/* TIOCTCNTL */
    601 		int on;
    602 
    603 		error = copyin (SCARG(uap, data), (void *)&on, sizeof (on));
    604 		if (error)
    605 			break;
    606 		error = (*ctl)(fp, TIOCUCNTL, &on);
    607 		break;
    608 	}
    609 	case _IOW('t', 33, int): {	/* TIOCSIGNAL */
    610 		int sig;
    611 
    612 		error = copyin (SCARG(uap, data), (void *)&sig, sizeof (sig));
    613 		if (error)
    614 			break;
    615 		error = (*ctl)(fp, TIOCSIG, &sig);
    616 		break;
    617 	}
    618 
    619 /*
    620  * Socket ioctl translations.
    621  */
    622 #define IFREQ_IN(a) { \
    623 	struct oifreq ifreq; \
    624 	error = copyin (SCARG(uap, data), (void *)&ifreq, sizeof (ifreq)); \
    625 	if (error) \
    626 		break; \
    627 	error = (*ctl)(fp, a, &ifreq); \
    628 	break; \
    629 }
    630 #define IFREQ_INOUT(a) { \
    631 	struct oifreq ifreq; \
    632 	error = copyin (SCARG(uap, data), (void *)&ifreq, sizeof (ifreq)); \
    633 	if (error) \
    634 		break; \
    635 	if ((error = (*ctl)(fp, a, &ifreq)) != 0) \
    636 		break; \
    637 	error = copyout ((void *)&ifreq, SCARG(uap, data), sizeof (ifreq)); \
    638 	break; \
    639 }
    640 
    641 	case _IOW('i', 12, struct oifreq):	/* SIOCSIFADDR */
    642 	case _IOW('i', 14, struct oifreq):	/* SIOCSIFDSTADDR */
    643 	case _IOW('i', 16, struct oifreq):	/* SIOCSIFFLAGS */
    644 	case _IOWR('i', 17, struct oifreq):	/* SIOCGIFFLAGS */
    645 	case _IOW('i', 30, struct arpreq):	/* SIOCSARP */
    646 	case _IOWR('i', 31, struct arpreq):	/* SIOCGARP */
    647 	case _IOW('i', 32, struct arpreq):	/* SIOCDARP */
    648 		break;
    649 
    650 	case _IOWR('i', 13, struct oifreq):
    651 		IFREQ_INOUT(OOSIOCGIFADDR);
    652 
    653 	case _IOWR('i', 15, struct oifreq):
    654 		IFREQ_INOUT(OOSIOCGIFDSTADDR);
    655 
    656 	case _IOW('i', 21, struct oifreq):
    657 		IFREQ_IN(SIOCSIFMTU);
    658 
    659 	case _IOWR('i', 22, struct oifreq):
    660 		IFREQ_INOUT(SIOCGIFMTU);
    661 
    662 	case _IOWR('i', 23, struct oifreq):
    663 		IFREQ_INOUT(SIOCGIFBRDADDR);
    664 
    665 	case _IOW('i', 24, struct oifreq):
    666 		IFREQ_IN(SIOCSIFBRDADDR);
    667 
    668 	case _IOWR('i', 25, struct oifreq):
    669 		IFREQ_INOUT(OOSIOCGIFNETMASK);
    670 
    671 	case _IOW('i', 26, struct oifreq):
    672 		IFREQ_IN(SIOCSIFNETMASK);
    673 
    674 	case _IOWR('i', 27, struct oifreq):
    675 		IFREQ_INOUT(SIOCGIFMETRIC);
    676 
    677 	case _IOWR('i', 28, struct oifreq):
    678 		IFREQ_IN(SIOCSIFMETRIC);
    679 
    680 	case _IOW('i', 18, struct oifreq):	/* SIOCSIFMEM */
    681 	case _IOWR('i', 19, struct oifreq):	/* SIOCGIFMEM */
    682 	case _IOW('i', 40, struct oifreq):	/* SIOCUPPER */
    683 	case _IOW('i', 41, struct oifreq):	/* SIOCLOWER */
    684 	case _IOW('i', 44, struct oifreq):	/* SIOCSETSYNC */
    685 	case _IOWR('i', 45, struct oifreq):	/* SIOCGETSYNC */
    686 	case _IOWR('i', 46, struct oifreq):	/* SIOCSDSTATS */
    687 	case _IOWR('i', 47, struct oifreq):	/* SIOCSESTATS */
    688 	case _IOW('i', 48, int):		/* SIOCSPROMISC */
    689 	case _IOW('i', 49, struct oifreq):	/* SIOCADDMULTI */
    690 	case _IOW('i', 50, struct oifreq):	/* SIOCDELMULTI */
    691 		error = EOPNOTSUPP;
    692 		break;
    693 
    694 	case _IOWR('i', 20, struct oifconf):	/* SIOCGIFCONF */
    695 	    {
    696 		struct oifconf ifc;
    697 
    698 		/*
    699 		 * XXX: two more problems
    700 		 * 1. our sockaddr's are variable length, not always sizeof(sockaddr)
    701 		 * 2. this returns a name per protocol, ie. it returns two "lo0"'s
    702 		 */
    703 		error = copyin (SCARG(uap, data), &ifc, sizeof (ifc));
    704 		if (error)
    705 			break;
    706 		error = (*ctl)(fp, OOSIOCGIFCONF, &ifc);
    707 		if (error)
    708 			break;
    709 		error = copyout ((void *)&ifc, SCARG(uap, data),
    710 		    sizeof (ifc));
    711 		break;
    712 	    }
    713 
    714 /*
    715  * Audio ioctl translations.
    716  */
    717 	case _IOR('A', 1, struct sunos_audio_info):	/* AUDIO_GETINFO */
    718 	sunos_au_getinfo:
    719 	    {
    720 		struct audio_info aui;
    721 		struct sunos_audio_info sunos_aui;
    722 
    723 		error = (*ctl)(fp, AUDIO_GETINFO, &aui);
    724 		if (error)
    725 			break;
    726 
    727 		sunos_aui.play = *(struct sunos_audio_prinfo *)&aui.play;
    728 		sunos_aui.record = *(struct sunos_audio_prinfo *)&aui.record;
    729 
    730 		/* `avail_ports' is `seek' in BSD */
    731 		sunos_aui.play.avail_ports = AUDIO_SPEAKER | AUDIO_HEADPHONE;
    732 		sunos_aui.record.avail_ports = AUDIO_SPEAKER | AUDIO_HEADPHONE;
    733 
    734 		sunos_aui.play.waiting = 0;
    735 		sunos_aui.record.waiting = 0;
    736 		sunos_aui.play.eof = 0;
    737 		sunos_aui.record.eof = 0;
    738 		sunos_aui.monitor_gain = 0; /* aui.__spare; XXX */
    739 		/*XXXsunos_aui.output_muted = 0;*/
    740 		/*XXX*/sunos_aui.reserved[0] = 0;
    741 		/*XXX*/sunos_aui.reserved[1] = 0;
    742 		/*XXX*/sunos_aui.reserved[2] = 0;
    743 		/*XXX*/sunos_aui.reserved[3] = 0;
    744 
    745 		error = copyout ((void *)&sunos_aui, SCARG(uap, data),
    746 				sizeof (sunos_aui));
    747 		break;
    748 	    }
    749 
    750 	case _IOWR('A', 2, struct sunos_audio_info):	/* AUDIO_SETINFO */
    751 	    {
    752 		struct audio_info aui;
    753 		struct sunos_audio_info sunos_aui;
    754 
    755 		error = copyin (SCARG(uap, data), (void *)&sunos_aui,
    756 		    sizeof (sunos_aui));
    757 		if (error)
    758 			break;
    759 
    760 		aui.play = *(struct audio_prinfo *)&sunos_aui.play;
    761 		aui.record = *(struct audio_prinfo *)&sunos_aui.record;
    762 		/* aui.__spare = sunos_aui.monitor_gain; */
    763 		aui.blocksize = ~0;
    764 		aui.hiwat = ~0;
    765 		aui.lowat = ~0;
    766 		/* XXX somebody check this please. - is: aui.backlog = ~0; */
    767 		aui.mode = ~0;
    768 		/*
    769 		 * The bsd driver does not distinguish between paused and
    770 		 * active. (In the sun driver, not active means samples are
    771 		 * not output at all, but paused means the last streams buffer
    772 		 * is drained and then output stops.)  If either are 0, then
    773 		 * when stop output. Otherwise, if either are non-zero,
    774 		 * we resume.
    775 		 */
    776 		if (sunos_aui.play.pause == 0 || sunos_aui.play.active == 0)
    777 			aui.play.pause = 0;
    778 		else if (sunos_aui.play.pause != (u_char)~0 ||
    779 			 sunos_aui.play.active != (u_char)~0)
    780 			aui.play.pause = 1;
    781 		if (sunos_aui.record.pause == 0 || sunos_aui.record.active == 0)
    782 			aui.record.pause = 0;
    783 		else if (sunos_aui.record.pause != (u_char)~0 ||
    784 			 sunos_aui.record.active != (u_char)~0)
    785 			aui.record.pause = 1;
    786 
    787 		error = (*ctl)(fp, AUDIO_SETINFO, &aui);
    788 		if (error)
    789 			break;
    790 		/* Return new state */
    791 		goto sunos_au_getinfo;
    792 	    }
    793 	case _IO('A', 3):	/* AUDIO_DRAIN */
    794 		error = (*ctl)(fp, AUDIO_DRAIN, NULL);
    795 		break;
    796 	case _IOR('A', 4, int):	/* AUDIO_GETDEV */
    797 	    {
    798 		int devtype = SUNOS_AUDIO_DEV_AMD;
    799 		error = copyout ((void *)&devtype, SCARG(uap, data),
    800 				sizeof (devtype));
    801 		break;
    802 	    }
    803 
    804 /*
    805  * Selected streams ioctls.
    806  */
    807 #define SUNOS_S_FLUSHR		1
    808 #define SUNOS_S_FLUSHW		2
    809 #define SUNOS_S_FLUSHRW		3
    810 
    811 #define SUNOS_S_INPUT		1
    812 #define SUNOS_S_HIPRI		2
    813 #define SUNOS_S_OUTPUT		4
    814 #define SUNOS_S_MSG		8
    815 
    816 	case _IO('S', 5):	/* I_FLUSH */
    817 	    {
    818 		int tmp = 0;
    819 		switch ((int)(u_long)SCARG(uap, data)) {
    820 		case SUNOS_S_FLUSHR:	tmp = FREAD; break;
    821 		case SUNOS_S_FLUSHW:	tmp = FWRITE; break;
    822 		case SUNOS_S_FLUSHRW:	tmp = FREAD|FWRITE; break;
    823 		}
    824                 error = (*ctl)(fp, TIOCFLUSH, &tmp);
    825 		break;
    826 	    }
    827 	case _IO('S', 9):	/* I_SETSIG */
    828 	    {
    829 		int on = 1;
    830 		if (((int)(u_long)SCARG(uap, data) &
    831 			(SUNOS_S_HIPRI|SUNOS_S_INPUT)) == SUNOS_S_HIPRI) {
    832 			error = EOPNOTSUPP;
    833 			break;
    834 		}
    835                 error = (*ctl)(fp, FIOASYNC, &on);
    836 		break;
    837 	    }
    838 	/*
    839 	 * SunOS disk ioctls, taken from arch/sparc/sparc/disksubr.c
    840 	 * (which was from the old sparc/scsi/sun_disklabel.c), and
    841 	 * modified to suite.
    842 	 */
    843 	case SUN_DKIOCGGEOM:
    844             {
    845 		struct disklabel dl;
    846 
    847 		error = (*ctl)(fp, DIOCGDINFO, &dl);
    848 		if (error)
    849 			break;
    850 
    851 #define datageom	((struct sun_dkgeom *)SCARG(uap, data))
    852 		memset(SCARG(uap, data), 0, sizeof(*datageom));
    853 
    854 		datageom->sdkc_ncylinders = dl.d_ncylinders;
    855 		datageom->sdkc_acylinders = dl.d_acylinders;
    856 		datageom->sdkc_ntracks = dl.d_ntracks;
    857 		datageom->sdkc_nsectors = dl.d_nsectors;
    858 		datageom->sdkc_interleave = dl.d_interleave;
    859 		datageom->sdkc_sparespercyl = dl.d_sparespercyl;
    860 		datageom->sdkc_rpm = dl.d_rpm;
    861 		datageom->sdkc_pcylinders = dl.d_ncylinders + dl.d_acylinders;
    862 #undef datageom
    863 		break;
    864 	    }
    865 
    866 	case SUN_DKIOCINFO:
    867 		/* Homey don't do DKIOCINFO */
    868 		memset(SCARG(uap, data), 0, sizeof(struct sun_dkctlr));
    869 		break;
    870 
    871 	case SUN_DKIOCGPART:
    872             {
    873 		struct partinfo pi;
    874 		struct disklabel label;
    875 
    876 		error = (*ctl)(fp, DIOCGDINFO, &label);
    877 		if (error)
    878 			break;
    879 		error = (*ctl)(fp, DIOCGPARTINFO, &pi);
    880 		if (error)
    881 			break;
    882 
    883 		if (label.d_secpercyl == 0) {
    884 			error = ERANGE;	/* XXX */
    885 			break;
    886 		}
    887 		if (pi.pi_offset % label.d_secpercyl != 0) {
    888 			error = ERANGE;	/* XXX */
    889 			break;
    890 		}
    891 
    892 #define datapart	((struct sun_dkpart *)SCARG(uap, data))
    893 		datapart->sdkp_cyloffset = pi.pi_offset / label.d_secpercyl;
    894 		datapart->sdkp_nsectors = pi.pi_size;
    895 #undef datapart
    896 		break;
    897 	    }
    898 
    899 	}
    900 
    901 out:
    902 	fd_putfile(SCARG(uap, fd));
    903 	if (error == EPASSTHROUGH) {
    904 		SCARG(&pass_ua, fd) = SCARG(uap, fd);
    905 		SCARG(&pass_ua, data) = SCARG(uap, data);
    906 		error = sys_ioctl(l, &pass_ua, retval);
    907 	}
    908 	return (error);
    909 }
    910 
    911 /* SunOS fcntl(2) cmds not implemented */
    912 #define SUN_F_RGETLK	10
    913 #define SUN_F_RSETLK	11
    914 #define SUN_F_CNVT	12
    915 #define SUN_F_RSETLKW	13
    916 
    917 /* SunOS flock translation */
    918 struct sunos_flock {
    919 	short	l_type;
    920 	short	l_whence;
    921 	long	l_start;
    922 	long	l_len;
    923 	short	l_pid;
    924 	short	l_xxx;
    925 };
    926 
    927 static void bsd_to_sunos_flock(struct flock *, struct sunos_flock *);
    928 static void sunos_to_bsd_flock(struct sunos_flock *, struct flock *);
    929 
    930 #define SUNOS_F_RDLCK	1
    931 #define	SUNOS_F_WRLCK	2
    932 #define SUNOS_F_UNLCK	3
    933 
    934 static void
    935 bsd_to_sunos_flock(struct flock *iflp, struct sunos_flock *oflp)
    936 {
    937 	switch (iflp->l_type) {
    938 	case F_RDLCK:
    939 		oflp->l_type = SUNOS_F_RDLCK;
    940 		break;
    941 	case F_WRLCK:
    942 		oflp->l_type = SUNOS_F_WRLCK;
    943 		break;
    944 	case F_UNLCK:
    945 		oflp->l_type = SUNOS_F_UNLCK;
    946 		break;
    947 	default:
    948 		oflp->l_type = -1;
    949 		break;
    950 	}
    951 
    952 	oflp->l_whence = (short) iflp->l_whence;
    953 	oflp->l_start = (long) iflp->l_start;
    954 	oflp->l_len = (long) iflp->l_len;
    955 	oflp->l_pid = (short) iflp->l_pid;
    956 	oflp->l_xxx = 0;
    957 }
    958 
    959 
    960 static void
    961 sunos_to_bsd_flock(struct sunos_flock *iflp, struct flock *oflp)
    962 {
    963 	switch (iflp->l_type) {
    964 	case SUNOS_F_RDLCK:
    965 		oflp->l_type = F_RDLCK;
    966 		break;
    967 	case SUNOS_F_WRLCK:
    968 		oflp->l_type = F_WRLCK;
    969 		break;
    970 	case SUNOS_F_UNLCK:
    971 		oflp->l_type = F_UNLCK;
    972 		break;
    973 	default:
    974 		oflp->l_type = -1;
    975 		break;
    976 	}
    977 
    978 	oflp->l_whence = iflp->l_whence;
    979 	oflp->l_start = (off_t) iflp->l_start;
    980 	oflp->l_len = (off_t) iflp->l_len;
    981 	oflp->l_pid = (pid_t) iflp->l_pid;
    982 
    983 }
    984 static struct {
    985 	long	sun_flg;
    986 	long	bsd_flg;
    987 } sunfcntl_flgtab[] = {
    988 	/* F_[GS]ETFLags that differ: */
    989 #define SUN_FSETBLK	0x0010
    990 #define SUN_SHLOCK	0x0080
    991 #define SUN_EXLOCK	0x0100
    992 #define SUN_FNBIO	0x1000
    993 #define SUN_FSYNC	0x2000
    994 #define SUN_NONBLOCK	0x4000
    995 #define SUN_FNOCTTY	0x8000
    996 	{ SUN_NONBLOCK, O_NONBLOCK },
    997 	{ SUN_FNBIO, O_NONBLOCK },
    998 	{ SUN_SHLOCK, O_SHLOCK },
    999 	{ SUN_EXLOCK, O_EXLOCK },
   1000 	{ SUN_FSYNC, O_FSYNC },
   1001 	{ SUN_FSETBLK, 0 },
   1002 	{ SUN_FNOCTTY, 0 }
   1003 };
   1004 
   1005 int
   1006 sunos_sys_fcntl(struct lwp *l, const struct sunos_sys_fcntl_args *uap, register_t *retval)
   1007 {
   1008 	long flg;
   1009 	int n, ret;
   1010 	struct sys_fcntl_args bsd_ua;
   1011 
   1012 	SCARG(&bsd_ua, fd) = SCARG(uap, fd);
   1013 	SCARG(&bsd_ua, cmd) = SCARG(uap, cmd);
   1014 	SCARG(&bsd_ua, arg) = SCARG(uap, arg);
   1015 
   1016 
   1017 	switch (SCARG(uap, cmd)) {
   1018 	case F_SETFL:
   1019 		flg = (long)SCARG(uap, arg);
   1020 		n = sizeof(sunfcntl_flgtab) / sizeof(sunfcntl_flgtab[0]);
   1021 		while (--n >= 0) {
   1022 			if (flg & sunfcntl_flgtab[n].sun_flg) {
   1023 				flg &= ~sunfcntl_flgtab[n].sun_flg;
   1024 				flg |= sunfcntl_flgtab[n].bsd_flg;
   1025 			}
   1026 		}
   1027 		SCARG(&bsd_ua, arg) = (void *)flg;
   1028 		break;
   1029 
   1030 	case F_GETLK:
   1031 	case F_SETLK:
   1032 	case F_SETLKW:
   1033 		{
   1034 			int error;
   1035 			struct sunos_flock	 ifl;
   1036 			struct flock		 fl;
   1037 
   1038 			error = copyin(SCARG(uap, arg), &ifl, sizeof ifl);
   1039 			if (error)
   1040 				return error;
   1041 			sunos_to_bsd_flock(&ifl, &fl);
   1042 
   1043 			error = do_fcntl_lock(SCARG(uap, fd), SCARG(uap, cmd), &fl);
   1044 			if (error)
   1045 				return error;
   1046 
   1047 			if (error || SCARG(uap, cmd) != F_GETLK)
   1048 				return error;
   1049 
   1050 			bsd_to_sunos_flock(&fl, &ifl);
   1051 
   1052 			return copyout(&ifl, SCARG(uap, arg), sizeof ifl);
   1053 		}
   1054 		break;
   1055 	case SUN_F_RGETLK:
   1056 	case SUN_F_RSETLK:
   1057 	case SUN_F_CNVT:
   1058 	case SUN_F_RSETLKW:
   1059 		return (EOPNOTSUPP);
   1060 
   1061 	default:
   1062 		break;
   1063 	}
   1064 
   1065 	ret = sys_fcntl(l, &bsd_ua, retval);
   1066 
   1067 	switch (SCARG(&bsd_ua, cmd)) {
   1068 	case F_GETFL:
   1069 		n = sizeof(sunfcntl_flgtab) / sizeof(sunfcntl_flgtab[0]);
   1070 		while (--n >= 0) {
   1071 			if (ret & sunfcntl_flgtab[n].bsd_flg) {
   1072 				ret &= ~sunfcntl_flgtab[n].bsd_flg;
   1073 				ret |= sunfcntl_flgtab[n].sun_flg;
   1074 			}
   1075 		}
   1076 		break;
   1077 	default:
   1078 		break;
   1079 	}
   1080 
   1081 	return (ret);
   1082 }
   1083