39f058c5630f7c0d69b60426afb1bbaff3ddc469
[dragonfly.git] / sys / emulation / linux / linux_ioctl.c
blob39f058c5630f7c0d69b60426afb1bbaff3ddc469
1 /*
2 * Copyright (c) 1994-1995 Søren Schmidt
3 * Copyright (c) 2004 Simon 'corecode' Schubert
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software withough specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $FreeBSD: src/sys/compat/linux/linux_ioctl.c,v 1.55.2.11 2003/05/01 20:16:09 anholt Exp $
30 * $DragonFly: src/sys/emulation/linux/linux_ioctl.c,v 1.25 2008/03/07 11:34:19 sephe Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysproto.h>
36 #include <sys/cdio.h>
37 #include <sys/consio.h>
38 #include <sys/ctype.h>
39 #include <sys/diskslice.h>
40 #include <sys/fcntl.h>
41 #include <sys/file.h>
42 #include <sys/filedesc.h>
43 #include <sys/filio.h>
44 #include <sys/kbio.h>
45 #include <sys/kernel.h>
46 #include <sys/linker_set.h>
47 #include <sys/malloc.h>
48 #include <sys/mapped_ioctl.h>
49 #include <sys/proc.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/soundcard.h>
53 #include <sys/tty.h>
54 #include <sys/uio.h>
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
59 #include <sys/file2.h>
60 #include <sys/mplock2.h>
62 #include <arch_linux/linux.h>
63 #include <arch_linux/linux_proto.h>
65 #include "linux_ioctl.h"
66 #include "linux_mib.h"
67 #include "linux_util.h"
70 static int
71 linux_ioctl_BLKGETSIZE32(struct file *fp, u_long cmd, u_long ocmd,
72 caddr_t data, struct ucred *cred)
74 struct partinfo dpart;
75 u_int32_t value;
76 int error;
78 error = fo_ioctl(fp, DIOCGPART, (caddr_t)&dpart, cred, NULL);
79 if (error)
80 return (error);
81 value = dpart.media_blocks; /* 64->32 */
82 bcopy(&value, data, sizeof(value));
83 return (0);
88 * termio related ioctls
91 struct linux_termio {
92 unsigned short c_iflag;
93 unsigned short c_oflag;
94 unsigned short c_cflag;
95 unsigned short c_lflag;
96 unsigned char c_line;
97 unsigned char c_cc[LINUX_NCC];
100 struct linux_termios {
101 unsigned int c_iflag;
102 unsigned int c_oflag;
103 unsigned int c_cflag;
104 unsigned int c_lflag;
105 unsigned char c_line;
106 unsigned char c_cc[LINUX_NCCS];
109 struct linux_winsize {
110 unsigned short ws_row, ws_col;
111 unsigned short ws_xpixel, ws_ypixel;
114 static struct speedtab sptab[] = {
115 { B0, LINUX_B0 }, { B50, LINUX_B50 },
116 { B75, LINUX_B75 }, { B110, LINUX_B110 },
117 { B134, LINUX_B134 }, { B150, LINUX_B150 },
118 { B200, LINUX_B200 }, { B300, LINUX_B300 },
119 { B600, LINUX_B600 }, { B1200, LINUX_B1200 },
120 { B1800, LINUX_B1800 }, { B2400, LINUX_B2400 },
121 { B4800, LINUX_B4800 }, { B9600, LINUX_B9600 },
122 { B19200, LINUX_B19200 }, { B38400, LINUX_B38400 },
123 { B57600, LINUX_B57600 }, { B115200, LINUX_B115200 },
124 {-1, -1 }
127 struct linux_serial_struct {
128 int type;
129 int line;
130 int port;
131 int irq;
132 int flags;
133 int xmit_fifo_size;
134 int custom_divisor;
135 int baud_base;
136 unsigned short close_delay;
137 char reserved_char[2];
138 int hub6;
139 unsigned short closing_wait;
140 unsigned short closing_wait2;
141 int reserved[4];
144 static int
145 linux_to_bsd_speed(int code, struct speedtab *table)
147 for ( ; table->sp_code != -1; table++)
148 if (table->sp_code == code)
149 return (table->sp_speed);
150 return -1;
153 static int
154 bsd_to_linux_speed(int speed, struct speedtab *table)
156 for ( ; table->sp_speed != -1; table++)
157 if (table->sp_speed == speed)
158 return (table->sp_code);
159 return -1;
162 static void
163 bsd_to_linux_termios(struct termios *bios, struct linux_termios *lios)
165 int i;
167 #ifdef DEBUG
168 if (ldebug(ioctl)) {
169 kprintf("LINUX: BSD termios structure (input):\n");
170 kprintf("i=%08x o=%08x c=%08x l=%08x ispeed=%d ospeed=%d\n",
171 bios->c_iflag, bios->c_oflag, bios->c_cflag, bios->c_lflag,
172 bios->c_ispeed, bios->c_ospeed);
173 kprintf("c_cc ");
174 for (i=0; i<NCCS; i++)
175 kprintf("%02x ", bios->c_cc[i]);
176 kprintf("\n");
178 #endif
180 lios->c_iflag = 0;
181 if (bios->c_iflag & IGNBRK)
182 lios->c_iflag |= LINUX_IGNBRK;
183 if (bios->c_iflag & BRKINT)
184 lios->c_iflag |= LINUX_BRKINT;
185 if (bios->c_iflag & IGNPAR)
186 lios->c_iflag |= LINUX_IGNPAR;
187 if (bios->c_iflag & PARMRK)
188 lios->c_iflag |= LINUX_PARMRK;
189 if (bios->c_iflag & INPCK)
190 lios->c_iflag |= LINUX_INPCK;
191 if (bios->c_iflag & ISTRIP)
192 lios->c_iflag |= LINUX_ISTRIP;
193 if (bios->c_iflag & INLCR)
194 lios->c_iflag |= LINUX_INLCR;
195 if (bios->c_iflag & IGNCR)
196 lios->c_iflag |= LINUX_IGNCR;
197 if (bios->c_iflag & ICRNL)
198 lios->c_iflag |= LINUX_ICRNL;
199 if (bios->c_iflag & IXON)
200 lios->c_iflag |= LINUX_IXON;
201 if (bios->c_iflag & IXANY)
202 lios->c_iflag |= LINUX_IXANY;
203 if (bios->c_iflag & IXOFF)
204 lios->c_iflag |= LINUX_IXOFF;
205 if (bios->c_iflag & IMAXBEL)
206 lios->c_iflag |= LINUX_IMAXBEL;
208 lios->c_oflag = 0;
209 if (bios->c_oflag & OPOST)
210 lios->c_oflag |= LINUX_OPOST;
211 if (bios->c_oflag & ONLCR)
212 lios->c_oflag |= LINUX_ONLCR;
213 if (bios->c_oflag & OXTABS)
214 lios->c_oflag |= LINUX_XTABS;
216 lios->c_cflag = bsd_to_linux_speed(bios->c_ispeed, sptab);
217 lios->c_cflag |= (bios->c_cflag & CSIZE) >> 4;
218 if (bios->c_cflag & CSTOPB)
219 lios->c_cflag |= LINUX_CSTOPB;
220 if (bios->c_cflag & CREAD)
221 lios->c_cflag |= LINUX_CREAD;
222 if (bios->c_cflag & PARENB)
223 lios->c_cflag |= LINUX_PARENB;
224 if (bios->c_cflag & PARODD)
225 lios->c_cflag |= LINUX_PARODD;
226 if (bios->c_cflag & HUPCL)
227 lios->c_cflag |= LINUX_HUPCL;
228 if (bios->c_cflag & CLOCAL)
229 lios->c_cflag |= LINUX_CLOCAL;
230 if (bios->c_cflag & CRTSCTS)
231 lios->c_cflag |= LINUX_CRTSCTS;
233 lios->c_lflag = 0;
234 if (bios->c_lflag & ISIG)
235 lios->c_lflag |= LINUX_ISIG;
236 if (bios->c_lflag & ICANON)
237 lios->c_lflag |= LINUX_ICANON;
238 if (bios->c_lflag & ECHO)
239 lios->c_lflag |= LINUX_ECHO;
240 if (bios->c_lflag & ECHOE)
241 lios->c_lflag |= LINUX_ECHOE;
242 if (bios->c_lflag & ECHOK)
243 lios->c_lflag |= LINUX_ECHOK;
244 if (bios->c_lflag & ECHONL)
245 lios->c_lflag |= LINUX_ECHONL;
246 if (bios->c_lflag & NOFLSH)
247 lios->c_lflag |= LINUX_NOFLSH;
248 if (bios->c_lflag & TOSTOP)
249 lios->c_lflag |= LINUX_TOSTOP;
250 if (bios->c_lflag & ECHOCTL)
251 lios->c_lflag |= LINUX_ECHOCTL;
252 if (bios->c_lflag & ECHOPRT)
253 lios->c_lflag |= LINUX_ECHOPRT;
254 if (bios->c_lflag & ECHOKE)
255 lios->c_lflag |= LINUX_ECHOKE;
256 if (bios->c_lflag & FLUSHO)
257 lios->c_lflag |= LINUX_FLUSHO;
258 if (bios->c_lflag & PENDIN)
259 lios->c_lflag |= LINUX_PENDIN;
260 if (bios->c_lflag & IEXTEN)
261 lios->c_lflag |= LINUX_IEXTEN;
263 for (i=0; i<LINUX_NCCS; i++)
264 lios->c_cc[i] = LINUX_POSIX_VDISABLE;
265 lios->c_cc[LINUX_VINTR] = bios->c_cc[VINTR];
266 lios->c_cc[LINUX_VQUIT] = bios->c_cc[VQUIT];
267 lios->c_cc[LINUX_VERASE] = bios->c_cc[VERASE];
268 lios->c_cc[LINUX_VKILL] = bios->c_cc[VKILL];
269 lios->c_cc[LINUX_VEOF] = bios->c_cc[VEOF];
270 lios->c_cc[LINUX_VEOL] = bios->c_cc[VEOL];
271 lios->c_cc[LINUX_VMIN] = bios->c_cc[VMIN];
272 lios->c_cc[LINUX_VTIME] = bios->c_cc[VTIME];
273 lios->c_cc[LINUX_VEOL2] = bios->c_cc[VEOL2];
274 lios->c_cc[LINUX_VSUSP] = bios->c_cc[VSUSP];
275 lios->c_cc[LINUX_VSTART] = bios->c_cc[VSTART];
276 lios->c_cc[LINUX_VSTOP] = bios->c_cc[VSTOP];
277 lios->c_cc[LINUX_VREPRINT] = bios->c_cc[VREPRINT];
278 lios->c_cc[LINUX_VDISCARD] = bios->c_cc[VDISCARD];
279 lios->c_cc[LINUX_VWERASE] = bios->c_cc[VWERASE];
280 lios->c_cc[LINUX_VLNEXT] = bios->c_cc[VLNEXT];
282 for (i=0; i<LINUX_NCCS; i++) {
283 if (i != LINUX_VMIN && i != LINUX_VTIME &&
284 lios->c_cc[i] == _POSIX_VDISABLE)
285 lios->c_cc[i] = LINUX_POSIX_VDISABLE;
287 lios->c_line = 0;
289 #ifdef DEBUG
290 if (ldebug(ioctl)) {
291 kprintf("LINUX: LINUX termios structure (output):\n");
292 kprintf("i=%08x o=%08x c=%08x l=%08x line=%d\n",
293 lios->c_iflag, lios->c_oflag, lios->c_cflag,
294 lios->c_lflag, (int)lios->c_line);
295 kprintf("c_cc ");
296 for (i=0; i<LINUX_NCCS; i++)
297 kprintf("%02x ", lios->c_cc[i]);
298 kprintf("\n");
300 #endif
303 static void
304 linux_to_bsd_termios(struct linux_termios *lios, struct termios *bios)
306 int i;
308 #ifdef DEBUG
309 if (ldebug(ioctl)) {
310 kprintf("LINUX: LINUX termios structure (input):\n");
311 kprintf("i=%08x o=%08x c=%08x l=%08x line=%d\n",
312 lios->c_iflag, lios->c_oflag, lios->c_cflag,
313 lios->c_lflag, (int)lios->c_line);
314 kprintf("c_cc ");
315 for (i=0; i<LINUX_NCCS; i++)
316 kprintf("%02x ", lios->c_cc[i]);
317 kprintf("\n");
319 #endif
321 bios->c_iflag = 0;
322 if (lios->c_iflag & LINUX_IGNBRK)
323 bios->c_iflag |= IGNBRK;
324 if (lios->c_iflag & LINUX_BRKINT)
325 bios->c_iflag |= BRKINT;
326 if (lios->c_iflag & LINUX_IGNPAR)
327 bios->c_iflag |= IGNPAR;
328 if (lios->c_iflag & LINUX_PARMRK)
329 bios->c_iflag |= PARMRK;
330 if (lios->c_iflag & LINUX_INPCK)
331 bios->c_iflag |= INPCK;
332 if (lios->c_iflag & LINUX_ISTRIP)
333 bios->c_iflag |= ISTRIP;
334 if (lios->c_iflag & LINUX_INLCR)
335 bios->c_iflag |= INLCR;
336 if (lios->c_iflag & LINUX_IGNCR)
337 bios->c_iflag |= IGNCR;
338 if (lios->c_iflag & LINUX_ICRNL)
339 bios->c_iflag |= ICRNL;
340 if (lios->c_iflag & LINUX_IXON)
341 bios->c_iflag |= IXON;
342 if (lios->c_iflag & LINUX_IXANY)
343 bios->c_iflag |= IXANY;
344 if (lios->c_iflag & LINUX_IXOFF)
345 bios->c_iflag |= IXOFF;
346 if (lios->c_iflag & LINUX_IMAXBEL)
347 bios->c_iflag |= IMAXBEL;
349 bios->c_oflag = 0;
350 if (lios->c_oflag & LINUX_OPOST)
351 bios->c_oflag |= OPOST;
352 if (lios->c_oflag & LINUX_ONLCR)
353 bios->c_oflag |= ONLCR;
354 if (lios->c_oflag & LINUX_XTABS)
355 bios->c_oflag |= OXTABS;
357 bios->c_cflag = (lios->c_cflag & LINUX_CSIZE) << 4;
358 if (lios->c_cflag & LINUX_CSTOPB)
359 bios->c_cflag |= CSTOPB;
360 if (lios->c_cflag & LINUX_CREAD)
361 bios->c_cflag |= CREAD;
362 if (lios->c_cflag & LINUX_PARENB)
363 bios->c_cflag |= PARENB;
364 if (lios->c_cflag & LINUX_PARODD)
365 bios->c_cflag |= PARODD;
366 if (lios->c_cflag & LINUX_HUPCL)
367 bios->c_cflag |= HUPCL;
368 if (lios->c_cflag & LINUX_CLOCAL)
369 bios->c_cflag |= CLOCAL;
370 if (lios->c_cflag & LINUX_CRTSCTS)
371 bios->c_cflag |= CRTSCTS;
373 bios->c_lflag = 0;
374 if (lios->c_lflag & LINUX_ISIG)
375 bios->c_lflag |= ISIG;
376 if (lios->c_lflag & LINUX_ICANON)
377 bios->c_lflag |= ICANON;
378 if (lios->c_lflag & LINUX_ECHO)
379 bios->c_lflag |= ECHO;
380 if (lios->c_lflag & LINUX_ECHOE)
381 bios->c_lflag |= ECHOE;
382 if (lios->c_lflag & LINUX_ECHOK)
383 bios->c_lflag |= ECHOK;
384 if (lios->c_lflag & LINUX_ECHONL)
385 bios->c_lflag |= ECHONL;
386 if (lios->c_lflag & LINUX_NOFLSH)
387 bios->c_lflag |= NOFLSH;
388 if (lios->c_lflag & LINUX_TOSTOP)
389 bios->c_lflag |= TOSTOP;
390 if (lios->c_lflag & LINUX_ECHOCTL)
391 bios->c_lflag |= ECHOCTL;
392 if (lios->c_lflag & LINUX_ECHOPRT)
393 bios->c_lflag |= ECHOPRT;
394 if (lios->c_lflag & LINUX_ECHOKE)
395 bios->c_lflag |= ECHOKE;
396 if (lios->c_lflag & LINUX_FLUSHO)
397 bios->c_lflag |= FLUSHO;
398 if (lios->c_lflag & LINUX_PENDIN)
399 bios->c_lflag |= PENDIN;
400 if (lios->c_lflag & LINUX_IEXTEN)
401 bios->c_lflag |= IEXTEN;
403 for (i=0; i<NCCS; i++)
404 bios->c_cc[i] = _POSIX_VDISABLE;
405 bios->c_cc[VINTR] = lios->c_cc[LINUX_VINTR];
406 bios->c_cc[VQUIT] = lios->c_cc[LINUX_VQUIT];
407 bios->c_cc[VERASE] = lios->c_cc[LINUX_VERASE];
408 bios->c_cc[VKILL] = lios->c_cc[LINUX_VKILL];
409 bios->c_cc[VEOF] = lios->c_cc[LINUX_VEOF];
410 bios->c_cc[VEOL] = lios->c_cc[LINUX_VEOL];
411 bios->c_cc[VMIN] = lios->c_cc[LINUX_VMIN];
412 bios->c_cc[VTIME] = lios->c_cc[LINUX_VTIME];
413 bios->c_cc[VEOL2] = lios->c_cc[LINUX_VEOL2];
414 bios->c_cc[VSUSP] = lios->c_cc[LINUX_VSUSP];
415 bios->c_cc[VSTART] = lios->c_cc[LINUX_VSTART];
416 bios->c_cc[VSTOP] = lios->c_cc[LINUX_VSTOP];
417 bios->c_cc[VREPRINT] = lios->c_cc[LINUX_VREPRINT];
418 bios->c_cc[VDISCARD] = lios->c_cc[LINUX_VDISCARD];
419 bios->c_cc[VWERASE] = lios->c_cc[LINUX_VWERASE];
420 bios->c_cc[VLNEXT] = lios->c_cc[LINUX_VLNEXT];
422 for (i=0; i<NCCS; i++) {
423 if (i != VMIN && i != VTIME &&
424 bios->c_cc[i] == LINUX_POSIX_VDISABLE)
425 bios->c_cc[i] = _POSIX_VDISABLE;
428 bios->c_ispeed = bios->c_ospeed =
429 linux_to_bsd_speed(lios->c_cflag & LINUX_CBAUD, sptab);
431 #ifdef DEBUG
432 if (ldebug(ioctl)) {
433 kprintf("LINUX: BSD termios structure (output):\n");
434 kprintf("i=%08x o=%08x c=%08x l=%08x ispeed=%d ospeed=%d\n",
435 bios->c_iflag, bios->c_oflag, bios->c_cflag, bios->c_lflag,
436 bios->c_ispeed, bios->c_ospeed);
437 kprintf("c_cc ");
438 for (i=0; i<NCCS; i++)
439 kprintf("%02x ", bios->c_cc[i]);
440 kprintf("\n");
442 #endif
445 static void
446 bsd_to_linux_termio(struct termios *bios, struct linux_termio *lio)
448 struct linux_termios lios;
450 bsd_to_linux_termios(bios, &lios);
451 lio->c_iflag = lios.c_iflag;
452 lio->c_oflag = lios.c_oflag;
453 lio->c_cflag = lios.c_cflag;
454 lio->c_lflag = lios.c_lflag;
455 lio->c_line = lios.c_line;
456 memcpy(lio->c_cc, lios.c_cc, LINUX_NCC);
459 static void
460 linux_to_bsd_termio(struct linux_termio *lio, struct termios *bios)
462 struct linux_termios lios;
463 int i;
465 lios.c_iflag = lio->c_iflag;
466 lios.c_oflag = lio->c_oflag;
467 lios.c_cflag = lio->c_cflag;
468 lios.c_lflag = lio->c_lflag;
469 for (i=LINUX_NCC; i<LINUX_NCCS; i++)
470 lios.c_cc[i] = LINUX_POSIX_VDISABLE;
471 memcpy(lios.c_cc, lio->c_cc, LINUX_NCC);
472 linux_to_bsd_termios(&lios, bios);
475 static int
476 linux_ioctl_TCGETS(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
478 struct termios bios;
479 struct linux_termios lios;
480 int error;
482 error = fo_ioctl(fp, TIOCGETA, (caddr_t)&bios, cred, NULL);
483 if (error)
484 return (error);
485 bsd_to_linux_termios(&bios, &lios);
486 bcopy(&lios, data, sizeof(lios));
487 return (0);
490 static int
491 linux_ioctl_TCSETS(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
493 struct termios bios;
494 struct linux_termios lios;
496 bcopy(data, &lios, sizeof(lios));
497 linux_to_bsd_termios(&lios, &bios);
498 return (fo_ioctl(fp, TIOCSETA, (caddr_t)&bios, cred, NULL));
501 static int
502 linux_ioctl_TCSETSW(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
504 struct termios bios;
505 struct linux_termios lios;
507 bcopy(data, &lios, sizeof(lios));
508 linux_to_bsd_termios(&lios, &bios);
509 return (fo_ioctl(fp, TIOCSETAW, (caddr_t)&bios, cred, NULL));
512 static int
513 linux_ioctl_TCSETSF(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
515 struct termios bios;
516 struct linux_termios lios;
518 bcopy(data, &lios, sizeof(lios));
519 linux_to_bsd_termios(&lios, &bios);
520 return (fo_ioctl(fp, TIOCSETAF, (caddr_t)&bios, cred, NULL));
523 static int
524 linux_ioctl_TCGETA(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
526 struct termios bios;
527 struct linux_termio lio;
528 int error;
530 error = fo_ioctl(fp, TIOCGETA, (caddr_t)&bios, cred, NULL);
531 if (error)
532 return (error);
533 bsd_to_linux_termio(&bios, &lio);
534 bcopy(&lio, data, sizeof(lio));
535 return (0);
538 static int
539 linux_ioctl_TCSETA(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
541 struct termios bios;
542 struct linux_termio lio;
544 bcopy(data, &lio, sizeof(lio));
545 linux_to_bsd_termio(&lio, &bios);
546 return (fo_ioctl(fp, TIOCSETA, (caddr_t)&bios, cred, NULL));
549 static int
550 linux_ioctl_TCSETAW(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
552 struct termios bios;
553 struct linux_termio lio;
555 bcopy(data, &lio, sizeof(lio));
556 linux_to_bsd_termio(&lio, &bios);
557 return (fo_ioctl(fp, TIOCSETAW, (caddr_t)&bios, cred, NULL));
560 static int
561 linux_ioctl_TCSETAF(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
563 struct termios bios;
564 struct linux_termio lio;
566 bcopy(data, &lio, sizeof(lio));
567 linux_to_bsd_termio(&lio, &bios);
568 return (fo_ioctl(fp, TIOCSETAF, (caddr_t)&bios, cred, NULL));
571 static int
572 linux_ioctl_TCXONC(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
574 switch ((u_long)data) {
575 case LINUX_TCOOFF:
576 cmd = TIOCSTOP;
577 break;
578 case LINUX_TCOON:
579 cmd = TIOCSTART;
580 break;
581 case LINUX_TCIOFF:
582 case LINUX_TCION: {
583 struct termios bios;
584 int error, c;
586 error = fo_ioctl(fp, TIOCGETA, (caddr_t)&bios, cred, NULL);
587 if (error)
588 return (error);
589 c = ((u_long)data == LINUX_TCIOFF) ? VSTOP : VSTART;
590 c = bios.c_cc[c];
591 if (c != _POSIX_VDISABLE) {
592 struct uio auio;
593 struct iovec aiov;
595 aiov.iov_base = (char *)&c;
596 aiov.iov_len = sizeof(*bios.c_cc);
597 auio.uio_iov = &aiov;
598 auio.uio_iovcnt = 1;
599 auio.uio_offset = -1;
600 auio.uio_resid = sizeof(*bios.c_cc);
601 auio.uio_rw = UIO_WRITE;
602 auio.uio_segflg = UIO_SYSSPACE;
603 auio.uio_td = curthread;
605 return (fo_write(fp, &auio, fp->f_cred, 0));
608 return (0);
610 default:
611 return (EINVAL);
613 return (fo_ioctl(fp, cmd, 0, cred, NULL));
616 static int
617 linux_ioctl_TCFLSH(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
619 switch ((u_long)data) {
620 case LINUX_TCIFLUSH:
621 *(u_long *)data = FREAD;
622 break;
623 case LINUX_TCOFLUSH:
624 *(u_long *)data = FWRITE;
625 break;
626 case LINUX_TCIOFLUSH:
627 *(u_long *)data = FREAD | FWRITE;
628 break;
629 default:
630 return (EINVAL);
632 return (fo_ioctl(fp, TIOCFLUSH, data, cred, NULL));
635 static int
636 linux_ioctl_TIOCGSERIAL(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
638 struct linux_serial_struct lss;
640 lss.type = LINUX_PORT_16550A;
641 lss.flags = 0;
642 lss.close_delay = 0;
643 bcopy(&lss, data, sizeof(lss));
644 return (0);
647 static int
648 linux_ioctl_TIOCSSERIAL(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
650 #if 0
651 struct linux_serial_struct lss;
653 bcopy(data, &lss, sizeof(lss));
654 /* XXX - It really helps to have an implementation that
655 * does nothing. NOT!
657 #endif
658 return (0);
661 static int
662 linux_ioctl_TIOCSETD(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
664 int line;
666 switch ((u_long)data) {
667 case LINUX_N_TTY:
668 line = TTYDISC;
669 break;
670 case LINUX_N_SLIP:
671 line = SLIPDISC;
672 break;
673 case LINUX_N_PPP:
674 line = PPPDISC;
675 break;
676 default:
677 return (EINVAL);
679 return (fo_ioctl(fp, TIOCSETD, (caddr_t)&line, cred, NULL));
682 static int
683 linux_ioctl_TIOCGETD(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
685 int linux_line, error;
686 int bsd_line = TTYDISC;
688 error = fo_ioctl(fp, TIOCGETD, (caddr_t)&bsd_line, cred, NULL);
689 if (error)
690 return (error);
691 switch (bsd_line) {
692 case TTYDISC:
693 linux_line = LINUX_N_TTY;
694 break;
695 case SLIPDISC:
696 linux_line = LINUX_N_SLIP;
697 break;
698 case PPPDISC:
699 linux_line = LINUX_N_PPP;
700 break;
701 default:
702 return (EINVAL);
704 bcopy(&linux_line, data, sizeof(int));
705 return (0);
710 * CDROM related ioctls
713 struct linux_cdrom_msf
715 u_char cdmsf_min0;
716 u_char cdmsf_sec0;
717 u_char cdmsf_frame0;
718 u_char cdmsf_min1;
719 u_char cdmsf_sec1;
720 u_char cdmsf_frame1;
723 struct linux_cdrom_tochdr
725 u_char cdth_trk0;
726 u_char cdth_trk1;
729 union linux_cdrom_addr
731 struct {
732 u_char minute;
733 u_char second;
734 u_char frame;
735 } msf;
736 int lba;
739 struct linux_cdrom_tocentry
741 u_char cdte_track;
742 u_char cdte_adr:4;
743 u_char cdte_ctrl:4;
744 u_char cdte_format;
745 union linux_cdrom_addr cdte_addr;
746 u_char cdte_datamode;
749 struct linux_cdrom_subchnl
751 u_char cdsc_format;
752 u_char cdsc_audiostatus;
753 u_char cdsc_adr:4;
754 u_char cdsc_ctrl:4;
755 u_char cdsc_trk;
756 u_char cdsc_ind;
757 union linux_cdrom_addr cdsc_absaddr;
758 union linux_cdrom_addr cdsc_reladdr;
761 static void
762 bsd_to_linux_msf_lba(u_char af, union msf_lba *bp, union linux_cdrom_addr *lp)
764 if (af == CD_LBA_FORMAT)
765 lp->lba = bp->lba;
766 else {
767 lp->msf.minute = bp->msf.minute;
768 lp->msf.second = bp->msf.second;
769 lp->msf.frame = bp->msf.frame;
773 static void
774 set_linux_cdrom_addr(union linux_cdrom_addr *addr, int format, int lba)
776 if (format == LINUX_CDROM_MSF) {
777 addr->msf.frame = lba % 75;
778 lba /= 75;
779 lba += 2;
780 addr->msf.second = lba % 60;
781 addr->msf.minute = lba / 60;
782 } else
783 addr->lba = lba;
786 static int
787 linux_ioctl_CDROMREADTOCHDR(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
789 struct ioc_toc_header th;
790 struct linux_cdrom_tochdr lth;
791 int error;
793 error = fo_ioctl(fp, CDIOREADTOCHEADER, (caddr_t)&th, cred, NULL);
794 if (error)
795 return (error);
796 lth.cdth_trk0 = th.starting_track;
797 lth.cdth_trk1 = th.ending_track;
798 bcopy(&lth, data, sizeof(lth));
799 return (0);
802 static int
803 linux_ioctl_CDROMREADTOCENTRY(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
805 struct linux_cdrom_tocentry *ltep = (struct linux_cdrom_tocentry *)data;
806 struct ioc_read_toc_single_entry irtse;
807 int error;
809 irtse.address_format = ltep->cdte_format;
810 irtse.track = ltep->cdte_track;
811 error = fo_ioctl(fp, CDIOREADTOCENTRY, (caddr_t)&irtse, cred, NULL);
812 if (error)
813 return (error);
815 ltep->cdte_ctrl = irtse.entry.control;
816 ltep->cdte_adr = irtse.entry.addr_type;
817 bsd_to_linux_msf_lba(irtse.address_format, &irtse.entry.addr,
818 &ltep->cdte_addr);
819 return (0);
822 static int
823 linux_ioctl_CDROMSUBCHNL(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
825 struct linux_cdrom_subchnl *sc = (struct linux_cdrom_subchnl *)data;
826 struct ioc_read_subchannel bsdsc;
827 struct cd_sub_channel_info *bsdinfo;
828 int error;
829 caddr_t sg = stackgap_init();
831 bsdinfo = stackgap_alloc(&sg, sizeof(struct cd_sub_channel_info));
832 bsdsc.address_format = CD_LBA_FORMAT;
833 bsdsc.data_format = CD_CURRENT_POSITION;
834 bsdsc.track = 0;
835 bsdsc.data_len = sizeof(struct cd_sub_channel_info);
836 bsdsc.data = bsdinfo;
837 error = fo_ioctl(fp, CDIOCREADSUBCHANNEL, (caddr_t)&bsdsc, cred, NULL);
838 if (error)
839 return (error);
840 sc->cdsc_audiostatus = bsdinfo->header.audio_status;
841 sc->cdsc_adr = bsdinfo->what.position.addr_type;
842 sc->cdsc_ctrl = bsdinfo->what.position.control;
843 sc->cdsc_trk = bsdinfo->what.position.track_number;
844 sc->cdsc_ind = bsdinfo->what.position.index_number;
845 set_linux_cdrom_addr(&sc->cdsc_absaddr, sc->cdsc_format, bsdinfo->what.position.absaddr.lba);
846 set_linux_cdrom_addr(&sc->cdsc_reladdr, sc->cdsc_format, bsdinfo->what.position.reladdr.lba);
847 return (0);
852 * Sound related ioctls
855 static int
856 linux_ioctl_OSS_GETVERSION(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
858 int version = linux_get_oss_version(curthread);
860 bcopy(&version, data, sizeof(int));
861 return (0);
866 * Console related ioctls
869 #define ISSIGVALID(sig) ((sig) > 0 && (sig) < NSIG)
871 static int
872 linux_ioctl_KDSKBMODE(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
874 int kbdmode;
876 switch ((u_long)data) {
877 case LINUX_KBD_RAW:
878 kbdmode = K_RAW;
879 break;
880 case LINUX_KBD_XLATE:
881 kbdmode = K_XLATE;
882 break;
883 case LINUX_KBD_MEDIUMRAW:
884 kbdmode = K_RAW;
885 break;
886 default:
887 return (EINVAL);
889 return (fo_ioctl(fp, KDSKBMODE, (caddr_t)&kbdmode, cred, NULL));
892 static int
893 linux_ioctl_VT_SETMODE(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
895 struct vt_mode *mode = (struct vt_mode *)data;
897 if (!ISSIGVALID(mode->frsig) && ISSIGVALID(mode->acqsig))
898 mode->frsig = mode->acqsig;
899 return (fo_ioctl(fp, VT_SETMODE, data, cred, NULL));
904 * Socket related ioctls
908 * Criteria for interface name translation
910 #define IFP_IS_ETH(ifp) (ifp->if_type == IFT_ETHER)
913 * Interface function used by linprocfs (at the time of writing). It's not
914 * used by the Linuxulator itself.
917 linux_ifname(struct ifnet *ifp, char *buffer, size_t buflen)
919 struct ifnet *ifscan;
920 int ethno;
922 /* Short-circuit non ethernet interfaces */
923 if (!IFP_IS_ETH(ifp))
924 return (strlcpy(buffer, ifp->if_xname, buflen));
926 /* Determine the (relative) unit number for ethernet interfaces */
927 ethno = 0;
928 TAILQ_FOREACH(ifscan, &ifnet, if_link) {
929 if (ifscan == ifp)
930 return (ksnprintf(buffer, buflen, "eth%d", ethno));
931 if (IFP_IS_ETH(ifscan))
932 ethno++;
935 return (0);
939 * Translate a Linux interface name to a FreeBSD interface name,
940 * and return the associated ifnet structure
941 * bsdname and lxname need to be least IFNAMSIZ bytes long, but
942 * can point to the same buffer.
945 static struct ifnet *
946 ifname_linux_to_bsd(const char *lxname, char *bsdname)
948 struct ifnet *ifp;
949 int len, unit;
950 char *ep;
951 int is_eth, index;
953 for (len = 0; len < LINUX_IFNAMSIZ; ++len)
954 if (!isalpha(lxname[len]))
955 break;
956 if (len == 0 || len == LINUX_IFNAMSIZ)
957 return (NULL);
958 unit = (int)strtoul(lxname + len, &ep, 10);
959 if (ep == NULL || ep == lxname + len || ep >= lxname + LINUX_IFNAMSIZ)
960 return (NULL);
961 index = 0;
962 is_eth = (len == 3 && !strncmp(lxname, "eth", len)) ? 1 : 0;
963 TAILQ_FOREACH(ifp, &ifnet, if_link) {
965 * Allow Linux programs to use FreeBSD names. Don't presume
966 * we never have an interface named "eth", so don't make
967 * the test optional based on is_eth.
969 if (strncmp(ifp->if_xname, lxname, LINUX_IFNAMSIZ) == 0)
970 break;
971 if (is_eth && IFP_IS_ETH(ifp) && unit == index++)
972 break;
974 if (ifp != NULL)
975 strlcpy(bsdname, ifp->if_xname, IFNAMSIZ);
976 return (ifp);
979 static int
980 linux_ioctl_SIOCGIFCONF(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
982 struct ifconf *ifc = (struct ifconf *)data;
983 struct l_ifreq ifr;
984 struct ifnet *ifp;
985 struct iovec iov;
986 struct uio uio;
987 int error, ethno;
989 /* much easier to use uiomove than keep track ourselves */
990 iov.iov_base = ifc->ifc_buf;
991 iov.iov_len = ifc->ifc_len;
992 uio.uio_iov = &iov;
993 uio.uio_iovcnt = 1;
994 uio.uio_offset = 0;
995 uio.uio_resid = ifc->ifc_len;
996 uio.uio_segflg = UIO_USERSPACE;
997 uio.uio_rw = UIO_READ;
998 uio.uio_td = curthread;
1000 /* Keep track of eth interfaces */
1001 ethno = 0;
1003 /* Return all AF_INET addresses of all interfaces */
1004 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1005 struct ifaddr_container *ifac;
1007 if (uio.uio_resid <= 0)
1008 break;
1010 bzero(&ifr, sizeof ifr);
1011 if (IFP_IS_ETH(ifp))
1012 ksnprintf(ifr.ifr_name, LINUX_IFNAMSIZ, "eth%d",
1013 ethno++);
1014 else
1015 strlcpy(ifr.ifr_name, ifp->if_xname, LINUX_IFNAMSIZ);
1017 /* Walk the address list */
1018 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1019 struct ifaddr *ifa = ifac->ifa;
1020 struct sockaddr *sa = ifa->ifa_addr;
1022 if (uio.uio_resid <= 0)
1023 break;
1025 if (sa->sa_family == AF_INET) {
1026 ifr.ifr_addr.sa_family = LINUX_AF_INET;
1027 memcpy(ifr.ifr_addr.sa_data, sa->sa_data,
1028 sizeof(ifr.ifr_addr.sa_data));
1030 error = uiomove((caddr_t)&ifr, sizeof ifr,
1031 &uio);
1032 if (error != 0)
1033 return (error);
1038 ifc->ifc_len -= uio.uio_resid;
1040 return (0);
1043 static int
1044 linux_ioctl_SIOCGIFFLAGS(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
1046 struct l_ifreq *ifr = (struct l_ifreq *)data;
1047 struct ifnet *ifp;
1048 char ifname[IFNAMSIZ];
1049 l_short flags;
1051 if (fp->f_type != DTYPE_SOCKET) {
1052 /* XXX: I doubt this is correct because
1053 * we don't translate the ifname and
1054 * use l_ifreq instead of ifreq
1056 return (fo_ioctl(fp, SIOCGIFFLAGS, data, cred, NULL));
1059 ifp = ifname_linux_to_bsd(ifr->ifr_name, ifname);
1060 flags = ifp->if_flags;
1061 /* these flags have no Linux equivalent */
1062 flags &= ~(IFF_SMART|IFF_OACTIVE|IFF_SIMPLEX|
1063 IFF_LINK0|IFF_LINK1|IFF_LINK2);
1064 /* Linux' multicast flag is in a different bit */
1065 if (flags & IFF_MULTICAST) {
1066 flags &= ~IFF_MULTICAST;
1067 flags |= 0x1000;
1070 ifr->ifr_flags = flags;
1071 return (0);
1074 #define ARPHRD_ETHER 1
1075 #define ARPHRD_LOOPBACK 772
1077 static int
1078 linux_ioctl_SIOGIFHWADDR(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
1080 struct l_ifreq *ifr = (struct l_ifreq *)data;
1081 struct ifnet *ifp;
1082 char ifname[IFNAMSIZ];
1083 struct sockaddr_dl *sdl;
1084 struct l_sockaddr lsa;
1085 struct ifaddr_container *ifac;
1087 ifp = ifname_linux_to_bsd(ifr->ifr_name, ifname);
1088 if (ifp->if_type == IFT_LOOP) {
1089 bzero(&ifr->ifr_hwaddr, sizeof lsa);
1090 ifr->ifr_hwaddr.sa_family = ARPHRD_LOOPBACK;
1091 return (0);
1094 if (ifp->if_type != IFT_ETHER)
1095 return (ENOENT);
1097 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1098 struct ifaddr *ifa = ifac->ifa;
1100 sdl = (struct sockaddr_dl*)ifa->ifa_addr;
1101 if (sdl != NULL && (sdl->sdl_family == AF_LINK) &&
1102 (sdl->sdl_type == IFT_ETHER)) {
1103 bzero(&ifr->ifr_hwaddr, sizeof lsa);
1104 ifr->ifr_hwaddr.sa_family = ARPHRD_ETHER;
1105 bcopy(LLADDR(sdl), ifr->ifr_hwaddr.sa_data, LINUX_IFHWADDRLEN);
1106 return (0);
1110 return (ENOENT);
1113 static int
1114 linux_ioctl_map_ifname(struct file *fp, u_long cmd, u_long ocmd, caddr_t data, struct ucred *cred)
1116 struct ifnet *ifp;
1117 int error;
1118 char *oifname = (char *)data;
1119 char lifname[LINUX_IFNAMSIZ];
1121 KASSERT(LINUX_IFNAMSIZ == IFNAMSIZ,
1122 ("%s(): LINUX_IFNAMSIZ != IFNAMSIZ", __func__));
1124 if (fp->f_type != DTYPE_SOCKET) {
1126 * XXX: I doubt this is correct because
1127 * we don't map the ifname
1129 /* not a socket - probably a tap / vmnet device */
1130 if (ocmd == LINUX_SIOCGIFADDR || ocmd == LINUX_SIOCSIFADDR) {
1131 cmd = (ocmd == LINUX_SIOCGIFADDR) ? SIOCGIFADDR : SIOCSIFADDR;
1132 return (fo_ioctl(fp, cmd, data, cred, NULL));
1133 } else
1134 return (ENOIOCTL);
1137 /* Save the original ifname */
1138 bcopy(oifname, lifname, LINUX_IFNAMSIZ);
1139 #ifdef DEBUG
1140 kprintf("%s(): ioctl %d on %.*s\n", __func__,
1141 (int)(cmd & 0xffff), LINUX_IFNAMSIZ, lifname);
1142 #endif
1143 /* Replace linux ifname with bsd ifname */
1144 ifp = ifname_linux_to_bsd(lifname, oifname);
1145 if (ifp == NULL) {
1146 error = EINVAL;
1147 goto clean_ifname;
1150 #ifdef DEBUG
1151 kprintf("%s(): %s translated to %s\n", __func__,
1152 lifname, oifname);
1153 #endif
1155 error = fo_ioctl(fp, cmd, data, cred, NULL);
1157 clean_ifname:
1158 bcopy(lifname, oifname, LINUX_IFNAMSIZ);
1159 return (error);
1164 * generic linux -> BSD syscall direction mapper
1166 u_long
1167 linux_gen_dirmap(u_long lstart, u_long lend, u_long bstart, u_long bend, u_long cmd, u_long ocmd)
1169 static u_int32_t dirbits[4] = { IOC_VOID, IOC_IN, IOC_OUT, IOC_INOUT };
1171 return ((cmd & ~IOC_DIRMASK) | dirbits[ocmd >> 30]);
1175 static struct ioctl_map_range linux_ioctl_map_entries[] = {
1176 /* disk ioctl */
1177 MAPPED_IOCTL_IOR(LINUX_BLKGETSIZE, linux_ioctl_BLKGETSIZE32, uint32_t),
1178 /* termio ioctl */
1179 MAPPED_IOCTL_IOR(LINUX_TCGETS, linux_ioctl_TCGETS, struct linux_termios),
1180 MAPPED_IOCTL_IOW(LINUX_TCSETS, linux_ioctl_TCSETS, struct linux_termios),
1181 MAPPED_IOCTL_IOW(LINUX_TCSETSW, linux_ioctl_TCSETSW, struct linux_termios),
1182 MAPPED_IOCTL_IOW(LINUX_TCSETSF, linux_ioctl_TCSETSF, struct linux_termios),
1183 MAPPED_IOCTL_IOR(LINUX_TCGETA, linux_ioctl_TCGETA, struct linux_termio),
1184 MAPPED_IOCTL_IOW(LINUX_TCSETA, linux_ioctl_TCSETA, struct linux_termio),
1185 MAPPED_IOCTL_IOW(LINUX_TCSETAW, linux_ioctl_TCSETAW, struct linux_termio),
1186 MAPPED_IOCTL_IOW(LINUX_TCSETAF, linux_ioctl_TCSETAF, struct linux_termio),
1187 MAPPED_IOCTL_IO(LINUX_TCXONC, linux_ioctl_TCXONC),
1188 MAPPED_IOCTL_IO(LINUX_TCFLSH, linux_ioctl_TCFLSH),
1189 MAPPED_IOCTL_MAP(LINUX_TIOCEXCL, TIOCEXCL),
1190 MAPPED_IOCTL_MAP(LINUX_TIOCNXCL, TIOCNXCL),
1191 MAPPED_IOCTL_MAP(LINUX_TIOCGPGRP, TIOCGPGRP),
1192 MAPPED_IOCTL_MAP(LINUX_TIOCSPGRP, TIOCSPGRP),
1193 MAPPED_IOCTL_MAP(LINUX_TIOCGWINSZ, TIOCGWINSZ),
1194 MAPPED_IOCTL_MAP(LINUX_TIOCSWINSZ, TIOCSWINSZ),
1195 MAPPED_IOCTL_MAP(LINUX_TIOCMGET, TIOCMGET),
1196 MAPPED_IOCTL_MAP(LINUX_TIOCMBIS, TIOCMBIS),
1197 MAPPED_IOCTL_MAP(LINUX_TIOCMBIC, TIOCMBIC),
1198 MAPPED_IOCTL_MAP(LINUX_TIOCMSET, TIOCMSET),
1199 MAPPED_IOCTL_MAP(LINUX_FIONREAD, FIONREAD),
1200 MAPPED_IOCTL_MAP(LINUX_TIOCCONS, TIOCCONS),
1201 MAPPED_IOCTL_IOR(LINUX_TIOCGSERIAL, linux_ioctl_TIOCGSERIAL, struct linux_serial_struct),
1202 MAPPED_IOCTL_IOW(LINUX_TIOCSSERIAL, linux_ioctl_TIOCSSERIAL, struct linux_serial_struct),
1203 MAPPED_IOCTL_MAP(LINUX_FIONBIO, FIONBIO),
1204 MAPPED_IOCTL_MAP(LINUX_TIOCNOTTY, TIOCNOTTY),
1205 MAPPED_IOCTL_IO(LINUX_TIOCSETD, linux_ioctl_TIOCSETD),
1206 MAPPED_IOCTL_IOR(LINUX_TIOCGETD, linux_ioctl_TIOCGETD, int),
1207 MAPPED_IOCTL_MAP(LINUX_FIONCLEX, FIONCLEX),
1208 MAPPED_IOCTL_MAP(LINUX_FIOCLEX, FIOCLEX),
1209 MAPPED_IOCTL_MAP(LINUX_FIOASYNC, FIOASYNC),
1210 /* cdrom ioctl */
1211 MAPPED_IOCTL_MAP(LINUX_CDROMPAUSE, CDIOCPAUSE),
1212 MAPPED_IOCTL_MAP(LINUX_CDROMRESUME, CDIOCRESUME),
1213 MAPPED_IOCTL_MAP(LINUX_CDROMPLAYMSF, CDIOCPLAYMSF),
1214 MAPPED_IOCTL_MAP(LINUX_CDROMPLAYTRKIND, CDIOCPLAYTRACKS),
1215 MAPPED_IOCTL_IOR(LINUX_CDROMREADTOCHDR, linux_ioctl_CDROMREADTOCHDR, struct linux_cdrom_tochdr),
1216 MAPPED_IOCTL_IOWR(LINUX_CDROMREADTOCENTRY, linux_ioctl_CDROMREADTOCENTRY, struct linux_cdrom_tocentry),
1217 MAPPED_IOCTL_MAP(LINUX_CDROMSTOP, CDIOCSTOP),
1218 MAPPED_IOCTL_MAP(LINUX_CDROMSTART, CDIOCSTART),
1219 MAPPED_IOCTL_MAP(LINUX_CDROMEJECT, CDIOCEJECT),
1220 MAPPED_IOCTL_IOWR(LINUX_CDROMSUBCHNL, linux_ioctl_CDROMSUBCHNL, struct linux_cdrom_subchnl),
1221 MAPPED_IOCTL_MAP(LINUX_CDROMRESET, CDIOCRESET),
1222 /* sound ioctl */
1223 MAPPED_IOCTL_MAPRANGE(LINUX_SOUND_MIXER_WRITE_MIN, LINUX_SOUND_MIXER_WRITE_MAX,
1224 LINUX_SOUND_MIXER_WRITE_MIN, LINUX_SOUND_MIXER_WRITE_MAX,
1225 NULL, linux_gen_dirmap),
1226 MAPPED_IOCTL_IOR(LINUX_OSS_GETVERSION, linux_ioctl_OSS_GETVERSION, int),
1227 MAPPED_IOCTL_MAP(LINUX_SOUND_MIXER_READ_DEVMASK, SOUND_MIXER_READ_DEVMASK),
1228 MAPPED_IOCTL_MAPRANGE(LINUX_SNDCTL_DSP_MIN, LINUX_SNDCTL_DSP_MAX, LINUX_SNDCTL_DSP_MIN,
1229 LINUX_SNDCTL_DSP_MAX, NULL, linux_gen_dirmap),
1230 MAPPED_IOCTL_MAPRANGE(LINUX_SNDCTL_SEQ_MIN, LINUX_SNDCTL_SEQ_MAX, LINUX_SNDCTL_SEQ_MIN,
1231 LINUX_SNDCTL_SEQ_MAX, NULL, linux_gen_dirmap),
1232 /* console ioctl */
1233 MAPPED_IOCTL_MAP(LINUX_KIOCSOUND, KIOCSOUND),
1234 MAPPED_IOCTL_MAP(LINUX_KDMKTONE, KDMKTONE),
1235 MAPPED_IOCTL_MAP(LINUX_KDGETLED, KDGETLED),
1236 MAPPED_IOCTL_MAP(LINUX_KDSETLED, KDSETLED),
1237 MAPPED_IOCTL_MAP(LINUX_KDSETMODE, KDSETMODE),
1238 MAPPED_IOCTL_MAP(LINUX_KDGETMODE, KDGETMODE),
1239 MAPPED_IOCTL_MAP(LINUX_KDGKBMODE, KDGKBMODE),
1240 MAPPED_IOCTL_IOW(LINUX_KDSKBMODE, linux_ioctl_KDSKBMODE, int),
1241 MAPPED_IOCTL_MAP(LINUX_VT_OPENQRY, VT_OPENQRY),
1242 MAPPED_IOCTL_MAP(LINUX_VT_GETMODE, VT_GETMODE),
1243 MAPPED_IOCTL_IOW(LINUX_VT_SETMODE, linux_ioctl_VT_SETMODE, struct vt_mode),
1244 MAPPED_IOCTL_MAP(LINUX_VT_GETSTATE, VT_GETACTIVE),
1245 MAPPED_IOCTL_MAP(LINUX_VT_RELDISP, VT_RELDISP),
1246 MAPPED_IOCTL_MAP(LINUX_VT_ACTIVATE, VT_ACTIVATE),
1247 MAPPED_IOCTL_MAP(LINUX_VT_WAITACTIVE, VT_WAITACTIVE),
1248 /* socket ioctl */
1249 MAPPED_IOCTL_MAP(LINUX_FIOSETOWN, FIOSETOWN),
1250 MAPPED_IOCTL_MAP(LINUX_SIOCSPGRP, SIOCSPGRP),
1251 MAPPED_IOCTL_MAP(LINUX_FIOGETOWN, FIOGETOWN),
1252 MAPPED_IOCTL_MAP(LINUX_SIOCGPGRP, SIOCGPGRP),
1253 MAPPED_IOCTL_MAP(LINUX_SIOCATMARK, SIOCATMARK),
1254 MAPPED_IOCTL_IOWR(LINUX_SIOCGIFCONF, linux_ioctl_SIOCGIFCONF, struct ifconf),
1255 MAPPED_IOCTL_IOWR(LINUX_SIOCGIFFLAGS, linux_ioctl_SIOCGIFFLAGS, struct l_ifreq),
1256 MAPPED_IOCTL_MAPF(LINUX_SIOCGIFADDR, OSIOCGIFADDR, linux_ioctl_map_ifname),
1257 MAPPED_IOCTL_MAPF(LINUX_SIOCSIFADDR, SIOCSIFADDR, linux_ioctl_map_ifname),
1258 MAPPED_IOCTL_MAPF(LINUX_SIOCGIFDSTADDR, OSIOCGIFDSTADDR, linux_ioctl_map_ifname),
1259 MAPPED_IOCTL_MAPF(LINUX_SIOCGIFBRDADDR, OSIOCGIFBRDADDR, linux_ioctl_map_ifname),
1260 MAPPED_IOCTL_MAPF(LINUX_SIOCGIFNETMASK, OSIOCGIFNETMASK, linux_ioctl_map_ifname),
1261 /*MAPPED_IOCTL_IOx(LINUX_SIOCSIFNETMASK, x, x),*/
1262 MAPPED_IOCTL_MAPF(LINUX_SIOCGIFMTU, SIOCGIFMTU, linux_ioctl_map_ifname),
1263 MAPPED_IOCTL_MAPF(LINUX_SIOCSIFMTU, SIOCSIFMTU, linux_ioctl_map_ifname),
1264 MAPPED_IOCTL_IOWR(LINUX_SIOCGIFHWADDR, linux_ioctl_SIOGIFHWADDR, struct l_ifreq),
1265 MAPPED_IOCTL_MAP(LINUX_SIOCADDMULTI, SIOCADDMULTI),
1266 MAPPED_IOCTL_MAP(LINUX_SIOCDELMULTI, SIOCDELMULTI),
1268 * XXX This is slightly bogus, but these ioctls are currently
1269 * XXX only used by the aironet (if_an) network driver.
1271 MAPPED_IOCTL_MAPF(LINUX_SIOCDEVPRIVATE, SIOCGPRIVATE_0, linux_ioctl_map_ifname),
1272 MAPPED_IOCTL_MAPF(LINUX_SIOCDEVPRIVATE+1, SIOCGPRIVATE_1, linux_ioctl_map_ifname),
1273 MAPPED_IOCTL_MAPF(0, 0, NULL)
1276 struct ioctl_map linux_ioctl_map = {
1277 0xffff, /* mask */
1278 "linux", /* subsys */
1279 LIST_HEAD_INITIALIZER(mapping)
1282 static struct ioctl_map_handler linux_ioctl_base_handler = {
1283 &linux_ioctl_map,
1284 "base",
1285 linux_ioctl_map_entries
1289 * main ioctl syscall function
1291 * MPALMOSTSAFE
1294 sys_linux_ioctl(struct linux_ioctl_args *args)
1296 int error;
1298 #ifdef DEBUG
1299 if (ldebug(ioctl))
1300 kprintf(ARGS(ioctl, "%d, %04x, *"), args->fd, args->cmd);
1301 #endif
1303 get_mplock();
1304 error = mapped_ioctl(args->fd, args->cmd, (caddr_t)args->arg,
1305 &linux_ioctl_map, &args->sysmsg);
1306 rel_mplock();
1307 return (error);
1310 SYSINIT (linux_ioctl_register, SI_BOOT2_KLD, SI_ORDER_MIDDLE,
1311 mapped_ioctl_register_handler, &linux_ioctl_base_handler);
1312 SYSUNINIT(linux_ioctl_register, SI_BOOT2_KLD, SI_ORDER_MIDDLE,
1313 mapped_ioctl_unregister_handler, &linux_ioctl_base_handler);