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