sunos_ioctl.c revision 1.56.8.1 1 /* $NetBSD: sunos_ioctl.c,v 1.56.8.1 2008/01/09 01:51:47 matt 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.56.8.1 2008/01/09 01:51:47 matt 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 struct proc *p = l->l_proc;
407 struct filedesc *fdp = p->p_fd;
408 struct file *fp;
409 int (*ctl)(struct file *, u_long, void *, struct lwp *);
410 struct sys_ioctl_args pass_ua;
411 int error;
412
413 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
414 return EBADF;
415
416 FILE_USE(fp);
417
418 if ((fp->f_flag & (FREAD|FWRITE)) == 0) {
419 error = EBADF;
420 goto out;
421 }
422
423 error = EPASSTHROUGH;
424 SCARG(&pass_ua, com) = SCARG(uap, com);
425 ctl = fp->f_ops->fo_ioctl;
426
427 switch (SCARG(uap, com)) {
428 case _IOR('t', 0, int):
429 SCARG(&pass_ua, com) = TIOCGETD;
430 break;
431 case _IOW('t', 1, int):
432 {
433 int disc;
434
435 if ((error = copyin(SCARG(uap, data), (void *)&disc,
436 sizeof disc)) != 0)
437 break;
438
439 /* map SunOS NTTYDISC into our termios discipline */
440 if (disc == 2)
441 disc = 0;
442 /* all other disciplines are not supported by NetBSD */
443 if (disc) {
444 error = ENXIO;
445 break;
446 }
447
448 error = (*ctl)(fp, TIOCSETD, (void *)&disc, l);
449 }
450 case _IOW('t', 101, int): /* sun SUNOS_TIOCSSOFTCAR */
451 {
452 int x; /* unused */
453
454 error = copyin((void *)&x, SCARG(uap, data), sizeof x);
455 break;
456 }
457 case _IOR('t', 100, int): /* sun SUNOS_TIOCSSOFTCAR */
458 {
459 int x = 0;
460
461 error = copyout((void *)&x, SCARG(uap, data), sizeof x);
462 break;
463 }
464 case _IO('t', 36): /* sun TIOCCONS, no parameters */
465 {
466 int on = 1;
467 error = (*ctl)(fp, TIOCCONS, (void *)&on, l);
468 break;
469 }
470 case _IOW('t', 37, struct sunos_ttysize):
471 {
472 struct winsize ws;
473 struct sunos_ttysize ss;
474
475 if ((error = (*ctl)(fp, TIOCGWINSZ, (void *)&ws, l)) != 0)
476 break;
477
478 if ((error = copyin (SCARG(uap, data), &ss, sizeof (ss))) != 0)
479 break;
480
481 ws.ws_row = ss.ts_row;
482 ws.ws_col = ss.ts_col;
483
484 error = (*ctl)(fp, TIOCSWINSZ, (void *)&ws, l);
485 break;
486 }
487 case _IOW('t', 38, struct sunos_ttysize):
488 {
489 struct winsize ws;
490 struct sunos_ttysize ss;
491
492 if ((error = (*ctl)(fp, TIOCGWINSZ, (void *)&ws, l)) != 0)
493 break;
494
495 ss.ts_row = ws.ws_row;
496 ss.ts_col = ws.ws_col;
497
498 error = copyout((void *)&ss, SCARG(uap, data), sizeof (ss));
499 break;
500 }
501 case _IOR('t', 119, int): /* TIOCGPGRP */
502 {
503 int pgrp;
504
505 error = (*ctl)(fp, TIOCGPGRP, (void *)&pgrp, l);
506 if (error == 0 && pgrp == 0)
507 error = EIO;
508 if (error)
509 break;
510 error = copyout((void *)&pgrp, SCARG(uap, data), sizeof(pgrp));
511 break;
512 }
513 case _IOW('t', 130, int): /* TIOCSETPGRP: posix variant */
514 SCARG(&pass_ua, com) = TIOCSPGRP;
515 break;
516 case _IOR('t', 131, int): /* TIOCGETPGRP: posix variant */
517 {
518 /*
519 * sigh, must do error translation on pty devices. if the pgrp
520 * returned is 0 (and no error), we convert this to EIO, if it
521 * is on a pty.
522 */
523 int pgrp;
524 struct vnode *vp;
525
526 error = (*ctl)(fp, TIOCGPGRP, (void *)&pgrp, l);
527 if (error) {
528 vp = (struct vnode *)fp->f_data;
529 if ((error == EIO || (error == 0 && pgrp == 0)) &&
530 vp != NULL &&
531 vp->v_type == VCHR &&
532 major(vp->v_rdev) == 21)
533 error = ENOTTY;
534 break;
535 }
536 error = copyout((void *)&pgrp, SCARG(uap, data), sizeof(pgrp));
537 break;
538 }
539 case _IO('t', 132):
540 SCARG(&pass_ua, com) = TIOCSCTTY;
541 break;
542 case SUNOS_TCGETA:
543 case SUNOS_TCGETS:
544 {
545 struct termios bts;
546 struct sunos_termios sts;
547 struct sunos_termio st;
548
549 if ((error = (*ctl)(fp, TIOCGETA, (void *)&bts, l)) != 0)
550 break;
551
552 btios2stios (&bts, &sts);
553 if (SCARG(uap, com) == SUNOS_TCGETA) {
554 stios2stio (&sts, &st);
555 error = copyout((void *)&st, SCARG(uap, data),
556 sizeof (st));
557 } else
558 error = copyout((void *)&sts, SCARG(uap, data),
559 sizeof (sts));
560 break;
561 }
562 case SUNOS_TCSETA:
563 case SUNOS_TCSETAW:
564 case SUNOS_TCSETAF:
565 {
566 struct termios bts;
567 struct sunos_termios sts;
568 struct sunos_termio st;
569
570 if ((error = copyin(SCARG(uap, data), (void *)&st,
571 sizeof (st))) != 0)
572 break;
573
574 /* get full BSD termios so we don't lose information */
575 if ((error = (*ctl)(fp, TIOCGETA, (void *)&bts, l)) != 0)
576 break;
577
578 /*
579 * convert to sun termios, copy in information from
580 * termio, and convert back, then set new values.
581 */
582 btios2stios(&bts, &sts);
583 stio2stios(&st, &sts);
584 stios2btios(&sts, &bts);
585
586 error = (*ctl)(fp, SCARG(uap, com) - SUNOS_TCSETA + TIOCSETA,
587 (void *)&bts, l);
588 break;
589 }
590 case SUNOS_TCSETS:
591 case SUNOS_TCSETSW:
592 case SUNOS_TCSETSF:
593 {
594 struct termios bts;
595 struct sunos_termios sts;
596
597 if ((error = copyin (SCARG(uap, data), (void *)&sts,
598 sizeof (sts))) != 0)
599 break;
600 stios2btios (&sts, &bts);
601 error = (*ctl)(fp, SCARG(uap, com) - SUNOS_TCSETS + TIOCSETA,
602 (void *)&bts, l);
603 break;
604 }
605 /*
606 * Pseudo-tty ioctl translations.
607 */
608 case _IOW('t', 32, int): { /* TIOCTCNTL */
609 int on;
610
611 error = copyin (SCARG(uap, data), (void *)&on, sizeof (on));
612 if (error)
613 break;
614 error = (*ctl)(fp, TIOCUCNTL, (void *)&on, l);
615 break;
616 }
617 case _IOW('t', 33, int): { /* TIOCSIGNAL */
618 int sig;
619
620 error = copyin (SCARG(uap, data), (void *)&sig, sizeof (sig));
621 if (error)
622 break;
623 error = (*ctl)(fp, TIOCSIG, (void *)&sig, l);
624 break;
625 }
626
627 /*
628 * Socket ioctl translations.
629 */
630 #define IFREQ_IN(a) { \
631 struct oifreq ifreq; \
632 error = copyin (SCARG(uap, data), (void *)&ifreq, sizeof (ifreq)); \
633 if (error) \
634 break; \
635 error = (*ctl)(fp, a, (void *)&ifreq, l); \
636 break; \
637 }
638 #define IFREQ_INOUT(a) { \
639 struct oifreq ifreq; \
640 error = copyin (SCARG(uap, data), (void *)&ifreq, sizeof (ifreq)); \
641 if (error) \
642 break; \
643 if ((error = (*ctl)(fp, a, (void *)&ifreq, l)) != 0) \
644 break; \
645 error = copyout ((void *)&ifreq, SCARG(uap, data), sizeof (ifreq)); \
646 break; \
647 }
648
649 case _IOW('i', 12, struct oifreq): /* SIOCSIFADDR */
650 case _IOW('i', 14, struct oifreq): /* SIOCSIFDSTADDR */
651 case _IOW('i', 16, struct oifreq): /* SIOCSIFFLAGS */
652 case _IOWR('i', 17, struct oifreq): /* SIOCGIFFLAGS */
653 case _IOW('i', 30, struct arpreq): /* SIOCSARP */
654 case _IOWR('i', 31, struct arpreq): /* SIOCGARP */
655 case _IOW('i', 32, struct arpreq): /* SIOCDARP */
656 break;
657
658 case _IOWR('i', 13, struct oifreq):
659 IFREQ_INOUT(OOSIOCGIFADDR);
660
661 case _IOWR('i', 15, struct oifreq):
662 IFREQ_INOUT(OOSIOCGIFDSTADDR);
663
664 case _IOW('i', 21, struct oifreq):
665 IFREQ_IN(SIOCSIFMTU);
666
667 case _IOWR('i', 22, struct oifreq):
668 IFREQ_INOUT(SIOCGIFMTU);
669
670 case _IOWR('i', 23, struct oifreq):
671 IFREQ_INOUT(SIOCGIFBRDADDR);
672
673 case _IOW('i', 24, struct oifreq):
674 IFREQ_IN(SIOCSIFBRDADDR);
675
676 case _IOWR('i', 25, struct oifreq):
677 IFREQ_INOUT(OOSIOCGIFNETMASK);
678
679 case _IOW('i', 26, struct oifreq):
680 IFREQ_IN(SIOCSIFNETMASK);
681
682 case _IOWR('i', 27, struct oifreq):
683 IFREQ_INOUT(SIOCGIFMETRIC);
684
685 case _IOWR('i', 28, struct oifreq):
686 IFREQ_IN(SIOCSIFMETRIC);
687
688 case _IOW('i', 18, struct oifreq): /* SIOCSIFMEM */
689 case _IOWR('i', 19, struct oifreq): /* SIOCGIFMEM */
690 case _IOW('i', 40, struct oifreq): /* SIOCUPPER */
691 case _IOW('i', 41, struct oifreq): /* SIOCLOWER */
692 case _IOW('i', 44, struct oifreq): /* SIOCSETSYNC */
693 case _IOWR('i', 45, struct oifreq): /* SIOCGETSYNC */
694 case _IOWR('i', 46, struct oifreq): /* SIOCSDSTATS */
695 case _IOWR('i', 47, struct oifreq): /* SIOCSESTATS */
696 case _IOW('i', 48, int): /* SIOCSPROMISC */
697 case _IOW('i', 49, struct oifreq): /* SIOCADDMULTI */
698 case _IOW('i', 50, struct oifreq): /* SIOCDELMULTI */
699 error = EOPNOTSUPP;
700 break;
701
702 case _IOWR('i', 20, struct oifconf): /* SIOCGIFCONF */
703 {
704 struct oifconf ifc;
705
706 /*
707 * XXX: two more problems
708 * 1. our sockaddr's are variable length, not always sizeof(sockaddr)
709 * 2. this returns a name per protocol, ie. it returns two "lo0"'s
710 */
711 error = copyin (SCARG(uap, data), (void *)&ifc,
712 sizeof (ifc));
713 if (error)
714 break;
715 error = (*ctl)(fp, OOSIOCGIFCONF, (void *)&ifc, l);
716 if (error)
717 break;
718 error = copyout ((void *)&ifc, SCARG(uap, data),
719 sizeof (ifc));
720 break;
721 }
722
723 /*
724 * Audio ioctl translations.
725 */
726 case _IOR('A', 1, struct sunos_audio_info): /* AUDIO_GETINFO */
727 sunos_au_getinfo:
728 {
729 struct audio_info aui;
730 struct sunos_audio_info sunos_aui;
731
732 error = (*ctl)(fp, AUDIO_GETINFO, (void *)&aui, l);
733 if (error)
734 break;
735
736 sunos_aui.play = *(struct sunos_audio_prinfo *)&aui.play;
737 sunos_aui.record = *(struct sunos_audio_prinfo *)&aui.record;
738
739 /* `avail_ports' is `seek' in BSD */
740 sunos_aui.play.avail_ports = AUDIO_SPEAKER | AUDIO_HEADPHONE;
741 sunos_aui.record.avail_ports = AUDIO_SPEAKER | AUDIO_HEADPHONE;
742
743 sunos_aui.play.waiting = 0;
744 sunos_aui.record.waiting = 0;
745 sunos_aui.play.eof = 0;
746 sunos_aui.record.eof = 0;
747 sunos_aui.monitor_gain = 0; /* aui.__spare; XXX */
748 /*XXXsunos_aui.output_muted = 0;*/
749 /*XXX*/sunos_aui.reserved[0] = 0;
750 /*XXX*/sunos_aui.reserved[1] = 0;
751 /*XXX*/sunos_aui.reserved[2] = 0;
752 /*XXX*/sunos_aui.reserved[3] = 0;
753
754 error = copyout ((void *)&sunos_aui, SCARG(uap, data),
755 sizeof (sunos_aui));
756 break;
757 }
758
759 case _IOWR('A', 2, struct sunos_audio_info): /* AUDIO_SETINFO */
760 {
761 struct audio_info aui;
762 struct sunos_audio_info sunos_aui;
763
764 error = copyin (SCARG(uap, data), (void *)&sunos_aui,
765 sizeof (sunos_aui));
766 if (error)
767 break;
768
769 aui.play = *(struct audio_prinfo *)&sunos_aui.play;
770 aui.record = *(struct audio_prinfo *)&sunos_aui.record;
771 /* aui.__spare = sunos_aui.monitor_gain; */
772 aui.blocksize = ~0;
773 aui.hiwat = ~0;
774 aui.lowat = ~0;
775 /* XXX somebody check this please. - is: aui.backlog = ~0; */
776 aui.mode = ~0;
777 /*
778 * The bsd driver does not distinguish between paused and
779 * active. (In the sun driver, not active means samples are
780 * not output at all, but paused means the last streams buffer
781 * is drained and then output stops.) If either are 0, then
782 * when stop output. Otherwise, if either are non-zero,
783 * we resume.
784 */
785 if (sunos_aui.play.pause == 0 || sunos_aui.play.active == 0)
786 aui.play.pause = 0;
787 else if (sunos_aui.play.pause != (u_char)~0 ||
788 sunos_aui.play.active != (u_char)~0)
789 aui.play.pause = 1;
790 if (sunos_aui.record.pause == 0 || sunos_aui.record.active == 0)
791 aui.record.pause = 0;
792 else if (sunos_aui.record.pause != (u_char)~0 ||
793 sunos_aui.record.active != (u_char)~0)
794 aui.record.pause = 1;
795
796 error = (*ctl)(fp, AUDIO_SETINFO, (void *)&aui, l);
797 if (error)
798 break;
799 /* Return new state */
800 goto sunos_au_getinfo;
801 }
802 case _IO('A', 3): /* AUDIO_DRAIN */
803 error = (*ctl)(fp, AUDIO_DRAIN, (void *)0, l);
804 break;
805 case _IOR('A', 4, int): /* AUDIO_GETDEV */
806 {
807 int devtype = SUNOS_AUDIO_DEV_AMD;
808 error = copyout ((void *)&devtype, SCARG(uap, data),
809 sizeof (devtype));
810 break;
811 }
812
813 /*
814 * Selected streams ioctls.
815 */
816 #define SUNOS_S_FLUSHR 1
817 #define SUNOS_S_FLUSHW 2
818 #define SUNOS_S_FLUSHRW 3
819
820 #define SUNOS_S_INPUT 1
821 #define SUNOS_S_HIPRI 2
822 #define SUNOS_S_OUTPUT 4
823 #define SUNOS_S_MSG 8
824
825 case _IO('S', 5): /* I_FLUSH */
826 {
827 int tmp = 0;
828 switch ((int)(u_long)SCARG(uap, data)) {
829 case SUNOS_S_FLUSHR: tmp = FREAD;
830 case SUNOS_S_FLUSHW: tmp = FWRITE;
831 case SUNOS_S_FLUSHRW: tmp = FREAD|FWRITE;
832 }
833 error = (*ctl)(fp, TIOCFLUSH, (void *)&tmp, l);
834 break;
835 }
836 case _IO('S', 9): /* I_SETSIG */
837 {
838 int on = 1;
839 if (((int)(u_long)SCARG(uap, data) &
840 (SUNOS_S_HIPRI|SUNOS_S_INPUT)) == SUNOS_S_HIPRI) {
841 error = EOPNOTSUPP;
842 break;
843 }
844 error = (*ctl)(fp, FIOASYNC, (void *)&on, l);
845 break;
846 }
847 /*
848 * SunOS disk ioctls, taken from arch/sparc/sparc/disksubr.c
849 * (which was from the old sparc/scsi/sun_disklabel.c), and
850 * modified to suite.
851 */
852 case DKIOCGGEOM:
853 {
854 struct disklabel dl;
855
856 error = (*ctl)(fp, DIOCGDINFO, (void *)&dl, l);
857 if (error)
858 break;
859
860 #define datageom ((struct sun_dkgeom *)SCARG(uap, data))
861 memset(SCARG(uap, data), 0, sizeof(*datageom));
862
863 datageom->sdkc_ncylinders = dl.d_ncylinders;
864 datageom->sdkc_acylinders = dl.d_acylinders;
865 datageom->sdkc_ntracks = dl.d_ntracks;
866 datageom->sdkc_nsectors = dl.d_nsectors;
867 datageom->sdkc_interleave = dl.d_interleave;
868 datageom->sdkc_sparespercyl = dl.d_sparespercyl;
869 datageom->sdkc_rpm = dl.d_rpm;
870 datageom->sdkc_pcylinders = dl.d_ncylinders + dl.d_acylinders;
871 #undef datageom
872 break;
873 }
874
875 case DKIOCINFO:
876 /* Homey don't do DKIOCINFO */
877 memset(SCARG(uap, data), 0, sizeof(struct sun_dkctlr));
878 break;
879
880 case DKIOCGPART:
881 {
882 struct partinfo pi;
883
884 error = (*ctl)(fp, DIOCGPART, (void *)&pi, l);
885 if (error)
886 break;
887
888 if (pi.disklab->d_secpercyl == 0) {
889 error = ERANGE; /* XXX */
890 break;
891 }
892 if (pi.part->p_offset % pi.disklab->d_secpercyl != 0) {
893 error = ERANGE; /* XXX */
894 break;
895 }
896
897 #define datapart ((struct sun_dkpart *)SCARG(uap, data))
898 datapart->sdkp_cyloffset = pi.part->p_offset / pi.disklab->d_secpercyl;
899 datapart->sdkp_nsectors = pi.part->p_size;
900 #undef datapart
901 break;
902 }
903
904 }
905
906 out:
907 FILE_UNUSE(fp, l);
908 if (error == EPASSTHROUGH) {
909 SCARG(&pass_ua, fd) = SCARG(uap, fd);
910 SCARG(&pass_ua, data) = SCARG(uap, data);
911 error = sys_ioctl(l, &pass_ua, retval);
912 }
913 return (error);
914 }
915
916 /* SunOS fcntl(2) cmds not implemented */
917 #define SUN_F_RGETLK 10
918 #define SUN_F_RSETLK 11
919 #define SUN_F_CNVT 12
920 #define SUN_F_RSETLKW 13
921
922 /* SunOS flock translation */
923 struct sunos_flock {
924 short l_type;
925 short l_whence;
926 long l_start;
927 long l_len;
928 short l_pid;
929 short l_xxx;
930 };
931
932 static void bsd_to_sunos_flock(struct flock *, struct sunos_flock *);
933 static void sunos_to_bsd_flock(struct sunos_flock *, struct flock *);
934
935 #define SUNOS_F_RDLCK 1
936 #define SUNOS_F_WRLCK 2
937 #define SUNOS_F_UNLCK 3
938
939 static void
940 bsd_to_sunos_flock(struct flock *iflp, struct sunos_flock *oflp)
941 {
942 switch (iflp->l_type) {
943 case F_RDLCK:
944 oflp->l_type = SUNOS_F_RDLCK;
945 break;
946 case F_WRLCK:
947 oflp->l_type = SUNOS_F_WRLCK;
948 break;
949 case F_UNLCK:
950 oflp->l_type = SUNOS_F_UNLCK;
951 break;
952 default:
953 oflp->l_type = -1;
954 break;
955 }
956
957 oflp->l_whence = (short) iflp->l_whence;
958 oflp->l_start = (long) iflp->l_start;
959 oflp->l_len = (long) iflp->l_len;
960 oflp->l_pid = (short) iflp->l_pid;
961 oflp->l_xxx = 0;
962 }
963
964
965 static void
966 sunos_to_bsd_flock(struct sunos_flock *iflp, struct flock *oflp)
967 {
968 switch (iflp->l_type) {
969 case SUNOS_F_RDLCK:
970 oflp->l_type = F_RDLCK;
971 break;
972 case SUNOS_F_WRLCK:
973 oflp->l_type = F_WRLCK;
974 break;
975 case SUNOS_F_UNLCK:
976 oflp->l_type = F_UNLCK;
977 break;
978 default:
979 oflp->l_type = -1;
980 break;
981 }
982
983 oflp->l_whence = iflp->l_whence;
984 oflp->l_start = (off_t) iflp->l_start;
985 oflp->l_len = (off_t) iflp->l_len;
986 oflp->l_pid = (pid_t) iflp->l_pid;
987
988 }
989 static struct {
990 long sun_flg;
991 long bsd_flg;
992 } sunfcntl_flgtab[] = {
993 /* F_[GS]ETFLags that differ: */
994 #define SUN_FSETBLK 0x0010
995 #define SUN_SHLOCK 0x0080
996 #define SUN_EXLOCK 0x0100
997 #define SUN_FNBIO 0x1000
998 #define SUN_FSYNC 0x2000
999 #define SUN_NONBLOCK 0x4000
1000 #define SUN_FNOCTTY 0x8000
1001 { SUN_NONBLOCK, O_NONBLOCK },
1002 { SUN_FNBIO, O_NONBLOCK },
1003 { SUN_SHLOCK, O_SHLOCK },
1004 { SUN_EXLOCK, O_EXLOCK },
1005 { SUN_FSYNC, O_FSYNC },
1006 { SUN_FSETBLK, 0 },
1007 { SUN_FNOCTTY, 0 }
1008 };
1009
1010 int
1011 sunos_sys_fcntl(struct lwp *l, const struct sunos_sys_fcntl_args *uap, register_t *retval)
1012 {
1013 long flg;
1014 int n, ret;
1015 struct sys_fcntl_args bsd_ua;
1016
1017 SCARG(&bsd_ua, fd) = SCARG(uap, fd);
1018 SCARG(&bsd_ua, cmd) = SCARG(uap, cmd);
1019 SCARG(&bsd_ua, arg) = SCARG(uap, arg);
1020
1021
1022 switch (SCARG(uap, cmd)) {
1023 case F_SETFL:
1024 flg = (long)SCARG(uap, arg);
1025 n = sizeof(sunfcntl_flgtab) / sizeof(sunfcntl_flgtab[0]);
1026 while (--n >= 0) {
1027 if (flg & sunfcntl_flgtab[n].sun_flg) {
1028 flg &= ~sunfcntl_flgtab[n].sun_flg;
1029 flg |= sunfcntl_flgtab[n].bsd_flg;
1030 }
1031 }
1032 SCARG(&bsd_ua, arg) = (void *)flg;
1033 break;
1034
1035 case F_GETLK:
1036 case F_SETLK:
1037 case F_SETLKW:
1038 {
1039 int error;
1040 struct sunos_flock ifl;
1041 struct flock fl;
1042
1043 error = copyin(SCARG(uap, arg), &ifl, sizeof ifl);
1044 if (error)
1045 return error;
1046 sunos_to_bsd_flock(&ifl, &fl);
1047
1048 error = do_fcntl_lock(l, SCARG(uap, fd), SCARG(uap, cmd), &fl);
1049 if (error)
1050 return error;
1051
1052 if (error || SCARG(uap, cmd) != F_GETLK)
1053 return error;
1054
1055 bsd_to_sunos_flock(&fl, &ifl);
1056
1057 return copyout(&ifl, SCARG(uap, arg), sizeof ifl);
1058 }
1059 break;
1060 case SUN_F_RGETLK:
1061 case SUN_F_RSETLK:
1062 case SUN_F_CNVT:
1063 case SUN_F_RSETLKW:
1064 return (EOPNOTSUPP);
1065
1066 default:
1067 break;
1068 }
1069
1070 ret = sys_fcntl(l, &bsd_ua, retval);
1071
1072 switch (SCARG(&bsd_ua, cmd)) {
1073 case F_GETFL:
1074 n = sizeof(sunfcntl_flgtab) / sizeof(sunfcntl_flgtab[0]);
1075 while (--n >= 0) {
1076 if (ret & sunfcntl_flgtab[n].bsd_flg) {
1077 ret &= ~sunfcntl_flgtab[n].bsd_flg;
1078 ret |= sunfcntl_flgtab[n].sun_flg;
1079 }
1080 }
1081 break;
1082 default:
1083 break;
1084 }
1085
1086 return (ret);
1087 }
1088