1 /*- 2 * Copyright (c) 1994-1995 S�ren Schmidt 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "opt_compat.h" 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/sysproto.h> 37 #include <sys/cdio.h> 38 #include <sys/dvdio.h> 39 #include <sys/conf.h> 40 #include <sys/disk.h> 41 #include <sys/consio.h> 42 #include <sys/ctype.h> 43 #include <sys/fcntl.h> 44 #include <sys/file.h> 45 #include <sys/filedesc.h> 46 #include <sys/filio.h> 47 #include <sys/jail.h> 48 #include <sys/kbio.h> 49 #include <sys/kernel.h> 50 #include <sys/linker_set.h> 51 #include <sys/lock.h> 52 #include <sys/malloc.h> 53 #include <sys/proc.h> 54 #include <sys/sbuf.h> 55 #include <sys/socket.h> 56 #include <sys/sockio.h> 57 #include <sys/soundcard.h> 58 #include <sys/stdint.h> 59 #include <sys/sx.h> 60 #include <sys/tty.h> 61 #include <sys/uio.h> 62 63 #include <net/if.h> 64 #include <net/if_dl.h> 65 #include <net/if_types.h> 66 #include <net/vnet.h> 67 68 #include <dev/usb/usb_ioctl.h> 69 70 #ifdef COMPAT_LINUX32 71 #include <machine/../linux32/linux.h> 72 #include <machine/../linux32/linux32_proto.h> 73 #else 74 #include <machine/../linux/linux.h> 75 #include <machine/../linux/linux_proto.h> 76 #endif 77 78 #include <compat/linux/linux_ioctl.h> 79 #include <compat/linux/linux_mib.h> 80 #include <compat/linux/linux_socket.h> 81 #include <compat/linux/linux_util.h> 82 83 #include <compat/linux/linux_videodev.h> 84 #include <compat/linux/linux_videodev_compat.h> 85 86 CTASSERT(LINUX_IFNAMSIZ == IFNAMSIZ); 87 88 static linux_ioctl_function_t linux_ioctl_cdrom; 89 static linux_ioctl_function_t linux_ioctl_vfat; 90 static linux_ioctl_function_t linux_ioctl_console; 91 static linux_ioctl_function_t linux_ioctl_hdio; 92 static linux_ioctl_function_t linux_ioctl_disk; 93 static linux_ioctl_function_t linux_ioctl_socket; 94 static linux_ioctl_function_t linux_ioctl_sound; 95 static linux_ioctl_function_t linux_ioctl_termio; 96 static linux_ioctl_function_t linux_ioctl_private; 97 static linux_ioctl_function_t linux_ioctl_drm; 98 static linux_ioctl_function_t linux_ioctl_sg; 99 static linux_ioctl_function_t linux_ioctl_v4l; 100 static linux_ioctl_function_t linux_ioctl_special; 101 static linux_ioctl_function_t linux_ioctl_fbsd_usb; 102 103 static struct linux_ioctl_handler cdrom_handler = 104 { linux_ioctl_cdrom, LINUX_IOCTL_CDROM_MIN, LINUX_IOCTL_CDROM_MAX }; 105 static struct linux_ioctl_handler vfat_handler = 106 { linux_ioctl_vfat, LINUX_IOCTL_VFAT_MIN, LINUX_IOCTL_VFAT_MAX }; 107 static struct linux_ioctl_handler console_handler = 108 { linux_ioctl_console, LINUX_IOCTL_CONSOLE_MIN, LINUX_IOCTL_CONSOLE_MAX }; 109 static struct linux_ioctl_handler hdio_handler = 110 { linux_ioctl_hdio, LINUX_IOCTL_HDIO_MIN, LINUX_IOCTL_HDIO_MAX }; 111 static struct linux_ioctl_handler disk_handler = 112 { linux_ioctl_disk, LINUX_IOCTL_DISK_MIN, LINUX_IOCTL_DISK_MAX }; 113 static struct linux_ioctl_handler socket_handler = 114 { linux_ioctl_socket, LINUX_IOCTL_SOCKET_MIN, LINUX_IOCTL_SOCKET_MAX }; 115 static struct linux_ioctl_handler sound_handler = 116 { linux_ioctl_sound, LINUX_IOCTL_SOUND_MIN, LINUX_IOCTL_SOUND_MAX }; 117 static struct linux_ioctl_handler termio_handler = 118 { linux_ioctl_termio, LINUX_IOCTL_TERMIO_MIN, LINUX_IOCTL_TERMIO_MAX }; 119 static struct linux_ioctl_handler private_handler = 120 { linux_ioctl_private, LINUX_IOCTL_PRIVATE_MIN, LINUX_IOCTL_PRIVATE_MAX }; 121 static struct linux_ioctl_handler drm_handler = 122 { linux_ioctl_drm, LINUX_IOCTL_DRM_MIN, LINUX_IOCTL_DRM_MAX }; 123 static struct linux_ioctl_handler sg_handler = 124 { linux_ioctl_sg, LINUX_IOCTL_SG_MIN, LINUX_IOCTL_SG_MAX }; 125 static struct linux_ioctl_handler video_handler = 126 { linux_ioctl_v4l, LINUX_IOCTL_VIDEO_MIN, LINUX_IOCTL_VIDEO_MAX }; 127 static struct linux_ioctl_handler fbsd_usb = 128 { linux_ioctl_fbsd_usb, FBSD_LUSB_MIN, FBSD_LUSB_MAX }; 129 130 DATA_SET(linux_ioctl_handler_set, cdrom_handler); 131 DATA_SET(linux_ioctl_handler_set, vfat_handler); 132 DATA_SET(linux_ioctl_handler_set, console_handler); 133 DATA_SET(linux_ioctl_handler_set, hdio_handler); 134 DATA_SET(linux_ioctl_handler_set, disk_handler); 135 DATA_SET(linux_ioctl_handler_set, socket_handler); 136 DATA_SET(linux_ioctl_handler_set, sound_handler); 137 DATA_SET(linux_ioctl_handler_set, termio_handler); 138 DATA_SET(linux_ioctl_handler_set, private_handler); 139 DATA_SET(linux_ioctl_handler_set, drm_handler); 140 DATA_SET(linux_ioctl_handler_set, sg_handler); 141 DATA_SET(linux_ioctl_handler_set, video_handler); 142 DATA_SET(linux_ioctl_handler_set, fbsd_usb); 143 144 struct handler_element 145 { 146 TAILQ_ENTRY(handler_element) list; 147 int (*func)(struct thread *, struct linux_ioctl_args *); 148 int low, high, span; 149 }; 150 151 static TAILQ_HEAD(, handler_element) handlers = 152 TAILQ_HEAD_INITIALIZER(handlers); 153 static struct sx linux_ioctl_sx; 154 SX_SYSINIT(linux_ioctl, &linux_ioctl_sx, "linux ioctl handlers"); 155 156 /* 157 * hdio related ioctls for VMWare support 158 */ 159 160 struct linux_hd_geometry { 161 u_int8_t heads; 162 u_int8_t sectors; 163 u_int16_t cylinders; 164 u_int32_t start; 165 }; 166 167 struct linux_hd_big_geometry { 168 u_int8_t heads; 169 u_int8_t sectors; 170 u_int32_t cylinders; 171 u_int32_t start; 172 }; 173 174 static int 175 linux_ioctl_hdio(struct thread *td, struct linux_ioctl_args *args) 176 { 177 struct file *fp; 178 int error; 179 u_int sectorsize, fwcylinders, fwheads, fwsectors; 180 off_t mediasize, bytespercyl; 181 182 if ((error = fget(td, args->fd, &fp)) != 0) 183 return (error); 184 switch (args->cmd & 0xffff) { 185 case LINUX_HDIO_GET_GEO: 186 case LINUX_HDIO_GET_GEO_BIG: 187 error = fo_ioctl(fp, DIOCGMEDIASIZE, 188 (caddr_t)&mediasize, td->td_ucred, td); 189 if (!error) 190 error = fo_ioctl(fp, DIOCGSECTORSIZE, 191 (caddr_t)§orsize, td->td_ucred, td); 192 if (!error) 193 error = fo_ioctl(fp, DIOCGFWHEADS, 194 (caddr_t)&fwheads, td->td_ucred, td); 195 if (!error) 196 error = fo_ioctl(fp, DIOCGFWSECTORS, 197 (caddr_t)&fwsectors, td->td_ucred, td); 198 /* 199 * XXX: DIOCGFIRSTOFFSET is not yet implemented, so 200 * so pretend that GEOM always says 0. This is NOT VALID 201 * for slices or partitions, only the per-disk raw devices. 202 */ 203 204 fdrop(fp, td); 205 if (error) 206 return (error); 207 /* 208 * 1. Calculate the number of bytes in a cylinder, 209 * given the firmware's notion of heads and sectors 210 * per cylinder. 211 * 2. Calculate the number of cylinders, given the total 212 * size of the media. 213 * All internal calculations should have 64-bit precision. 214 */ 215 bytespercyl = (off_t) sectorsize * fwheads * fwsectors; 216 fwcylinders = mediasize / bytespercyl; 217 #if defined(DEBUG) 218 linux_msg(td, "HDIO_GET_GEO: mediasize %jd, c/h/s %d/%d/%d, " 219 "bpc %jd", 220 (intmax_t)mediasize, fwcylinders, fwheads, fwsectors, 221 (intmax_t)bytespercyl); 222 #endif 223 if ((args->cmd & 0xffff) == LINUX_HDIO_GET_GEO) { 224 struct linux_hd_geometry hdg; 225 226 hdg.cylinders = fwcylinders; 227 hdg.heads = fwheads; 228 hdg.sectors = fwsectors; 229 hdg.start = 0; 230 error = copyout(&hdg, (void *)args->arg, sizeof(hdg)); 231 } else if ((args->cmd & 0xffff) == LINUX_HDIO_GET_GEO_BIG) { 232 struct linux_hd_big_geometry hdbg; 233 234 hdbg.cylinders = fwcylinders; 235 hdbg.heads = fwheads; 236 hdbg.sectors = fwsectors; 237 hdbg.start = 0; 238 error = copyout(&hdbg, (void *)args->arg, sizeof(hdbg)); 239 } 240 return (error); 241 break; 242 default: 243 /* XXX */ 244 linux_msg(td, 245 "ioctl fd=%d, cmd=0x%x ('%c',%d) is not implemented", 246 args->fd, (int)(args->cmd & 0xffff), 247 (int)(args->cmd & 0xff00) >> 8, 248 (int)(args->cmd & 0xff)); 249 break; 250 } 251 fdrop(fp, td); 252 return (ENOIOCTL); 253 } 254 255 static int 256 linux_ioctl_disk(struct thread *td, struct linux_ioctl_args *args) 257 { 258 struct file *fp; 259 int error; 260 u_int sectorsize; 261 off_t mediasize; 262 263 if ((error = fget(td, args->fd, &fp)) != 0) 264 return (error); 265 switch (args->cmd & 0xffff) { 266 case LINUX_BLKGETSIZE: 267 error = fo_ioctl(fp, DIOCGSECTORSIZE, 268 (caddr_t)§orsize, td->td_ucred, td); 269 if (!error) 270 error = fo_ioctl(fp, DIOCGMEDIASIZE, 271 (caddr_t)&mediasize, td->td_ucred, td); 272 fdrop(fp, td); 273 if (error) 274 return (error); 275 sectorsize = mediasize / sectorsize; 276 /* 277 * XXX: How do we know we return the right size of integer ? 278 */ 279 return (copyout(§orsize, (void *)args->arg, 280 sizeof(sectorsize))); 281 break; 282 } 283 fdrop(fp, td); 284 return (ENOIOCTL); 285 } 286 287 /* 288 * termio related ioctls 289 */ 290 291 struct linux_termio { 292 unsigned short c_iflag; 293 unsigned short c_oflag; 294 unsigned short c_cflag; 295 unsigned short c_lflag; 296 unsigned char c_line; 297 unsigned char c_cc[LINUX_NCC]; 298 }; 299 300 struct linux_termios { 301 unsigned int c_iflag; 302 unsigned int c_oflag; 303 unsigned int c_cflag; 304 unsigned int c_lflag; 305 unsigned char c_line; 306 unsigned char c_cc[LINUX_NCCS]; 307 }; 308 309 struct linux_winsize { 310 unsigned short ws_row, ws_col; 311 unsigned short ws_xpixel, ws_ypixel; 312 }; 313 314 struct speedtab { 315 int sp_speed; /* Speed. */ 316 int sp_code; /* Code. */ 317 }; 318 319 static struct speedtab sptab[] = { 320 { B0, LINUX_B0 }, { B50, LINUX_B50 }, 321 { B75, LINUX_B75 }, { B110, LINUX_B110 }, 322 { B134, LINUX_B134 }, { B150, LINUX_B150 }, 323 { B200, LINUX_B200 }, { B300, LINUX_B300 }, 324 { B600, LINUX_B600 }, { B1200, LINUX_B1200 }, 325 { B1800, LINUX_B1800 }, { B2400, LINUX_B2400 }, 326 { B4800, LINUX_B4800 }, { B9600, LINUX_B9600 }, 327 { B19200, LINUX_B19200 }, { B38400, LINUX_B38400 }, 328 { B57600, LINUX_B57600 }, { B115200, LINUX_B115200 }, 329 {-1, -1 } 330 }; 331 332 struct linux_serial_struct { 333 int type; 334 int line; 335 int port; 336 int irq; 337 int flags; 338 int xmit_fifo_size; 339 int custom_divisor; 340 int baud_base; 341 unsigned short close_delay; 342 char reserved_char[2]; 343 int hub6; 344 unsigned short closing_wait; 345 unsigned short closing_wait2; 346 int reserved[4]; 347 }; 348 349 static int 350 linux_to_bsd_speed(int code, struct speedtab *table) 351 { 352 for ( ; table->sp_code != -1; table++) 353 if (table->sp_code == code) 354 return (table->sp_speed); 355 return -1; 356 } 357 358 static int 359 bsd_to_linux_speed(int speed, struct speedtab *table) 360 { 361 for ( ; table->sp_speed != -1; table++) 362 if (table->sp_speed == speed) 363 return (table->sp_code); 364 return -1; 365 } 366 367 static void 368 bsd_to_linux_termios(struct termios *bios, struct linux_termios *lios) 369 { 370 int i; 371 372 #ifdef DEBUG 373 if (ldebug(ioctl)) { 374 printf("LINUX: BSD termios structure (input):\n"); 375 printf("i=%08x o=%08x c=%08x l=%08x ispeed=%d ospeed=%d\n", 376 bios->c_iflag, bios->c_oflag, bios->c_cflag, bios->c_lflag, 377 bios->c_ispeed, bios->c_ospeed); 378 printf("c_cc "); 379 for (i=0; i<NCCS; i++) 380 printf("%02x ", bios->c_cc[i]); 381 printf("\n"); 382 } 383 #endif 384 385 lios->c_iflag = 0; 386 if (bios->c_iflag & IGNBRK) 387 lios->c_iflag |= LINUX_IGNBRK; 388 if (bios->c_iflag & BRKINT) 389 lios->c_iflag |= LINUX_BRKINT; 390 if (bios->c_iflag & IGNPAR) 391 lios->c_iflag |= LINUX_IGNPAR; 392 if (bios->c_iflag & PARMRK) 393 lios->c_iflag |= LINUX_PARMRK; 394 if (bios->c_iflag & INPCK) 395 lios->c_iflag |= LINUX_INPCK; 396 if (bios->c_iflag & ISTRIP) 397 lios->c_iflag |= LINUX_ISTRIP; 398 if (bios->c_iflag & INLCR) 399 lios->c_iflag |= LINUX_INLCR; 400 if (bios->c_iflag & IGNCR) 401 lios->c_iflag |= LINUX_IGNCR; 402 if (bios->c_iflag & ICRNL) 403 lios->c_iflag |= LINUX_ICRNL; 404 if (bios->c_iflag & IXON) 405 lios->c_iflag |= LINUX_IXON; 406 if (bios->c_iflag & IXANY) 407 lios->c_iflag |= LINUX_IXANY; 408 if (bios->c_iflag & IXOFF) 409 lios->c_iflag |= LINUX_IXOFF; 410 if (bios->c_iflag & IMAXBEL) 411 lios->c_iflag |= LINUX_IMAXBEL; 412 413 lios->c_oflag = 0; 414 if (bios->c_oflag & OPOST) 415 lios->c_oflag |= LINUX_OPOST; 416 if (bios->c_oflag & ONLCR) 417 lios->c_oflag |= LINUX_ONLCR; 418 if (bios->c_oflag & TAB3) 419 lios->c_oflag |= LINUX_XTABS; 420 421 lios->c_cflag = bsd_to_linux_speed(bios->c_ispeed, sptab); 422 lios->c_cflag |= (bios->c_cflag & CSIZE) >> 4; 423 if (bios->c_cflag & CSTOPB) 424 lios->c_cflag |= LINUX_CSTOPB; 425 if (bios->c_cflag & CREAD) 426 lios->c_cflag |= LINUX_CREAD; 427 if (bios->c_cflag & PARENB) 428 lios->c_cflag |= LINUX_PARENB; 429 if (bios->c_cflag & PARODD) 430 lios->c_cflag |= LINUX_PARODD; 431 if (bios->c_cflag & HUPCL) 432 lios->c_cflag |= LINUX_HUPCL; 433 if (bios->c_cflag & CLOCAL) 434 lios->c_cflag |= LINUX_CLOCAL; 435 if (bios->c_cflag & CRTSCTS) 436 lios->c_cflag |= LINUX_CRTSCTS; 437 438 lios->c_lflag = 0; 439 if (bios->c_lflag & ISIG) 440 lios->c_lflag |= LINUX_ISIG; 441 if (bios->c_lflag & ICANON) 442 lios->c_lflag |= LINUX_ICANON; 443 if (bios->c_lflag & ECHO) 444 lios->c_lflag |= LINUX_ECHO; 445 if (bios->c_lflag & ECHOE) 446 lios->c_lflag |= LINUX_ECHOE; 447 if (bios->c_lflag & ECHOK) 448 lios->c_lflag |= LINUX_ECHOK; 449 if (bios->c_lflag & ECHONL) 450 lios->c_lflag |= LINUX_ECHONL; 451 if (bios->c_lflag & NOFLSH) 452 lios->c_lflag |= LINUX_NOFLSH; 453 if (bios->c_lflag & TOSTOP) 454 lios->c_lflag |= LINUX_TOSTOP; 455 if (bios->c_lflag & ECHOCTL) 456 lios->c_lflag |= LINUX_ECHOCTL; 457 if (bios->c_lflag & ECHOPRT) 458 lios->c_lflag |= LINUX_ECHOPRT; 459 if (bios->c_lflag & ECHOKE) 460 lios->c_lflag |= LINUX_ECHOKE; 461 if (bios->c_lflag & FLUSHO) 462 lios->c_lflag |= LINUX_FLUSHO; 463 if (bios->c_lflag & PENDIN) 464 lios->c_lflag |= LINUX_PENDIN; 465 if (bios->c_lflag & IEXTEN) 466 lios->c_lflag |= LINUX_IEXTEN; 467 468 for (i=0; i<LINUX_NCCS; i++) 469 lios->c_cc[i] = LINUX_POSIX_VDISABLE; 470 lios->c_cc[LINUX_VINTR] = bios->c_cc[VINTR]; 471 lios->c_cc[LINUX_VQUIT] = bios->c_cc[VQUIT]; 472 lios->c_cc[LINUX_VERASE] = bios->c_cc[VERASE]; 473 lios->c_cc[LINUX_VKILL] = bios->c_cc[VKILL]; 474 lios->c_cc[LINUX_VEOF] = bios->c_cc[VEOF]; 475 lios->c_cc[LINUX_VEOL] = bios->c_cc[VEOL]; 476 lios->c_cc[LINUX_VMIN] = bios->c_cc[VMIN]; 477 lios->c_cc[LINUX_VTIME] = bios->c_cc[VTIME]; 478 lios->c_cc[LINUX_VEOL2] = bios->c_cc[VEOL2]; 479 lios->c_cc[LINUX_VSUSP] = bios->c_cc[VSUSP]; 480 lios->c_cc[LINUX_VSTART] = bios->c_cc[VSTART]; 481 lios->c_cc[LINUX_VSTOP] = bios->c_cc[VSTOP]; 482 lios->c_cc[LINUX_VREPRINT] = bios->c_cc[VREPRINT]; 483 lios->c_cc[LINUX_VDISCARD] = bios->c_cc[VDISCARD]; 484 lios->c_cc[LINUX_VWERASE] = bios->c_cc[VWERASE]; 485 lios->c_cc[LINUX_VLNEXT] = bios->c_cc[VLNEXT]; 486 487 for (i=0; i<LINUX_NCCS; i++) { 488 if (i != LINUX_VMIN && i != LINUX_VTIME && 489 lios->c_cc[i] == _POSIX_VDISABLE) 490 lios->c_cc[i] = LINUX_POSIX_VDISABLE; 491 } 492 lios->c_line = 0; 493 494 #ifdef DEBUG 495 if (ldebug(ioctl)) { 496 printf("LINUX: LINUX termios structure (output):\n"); 497 printf("i=%08x o=%08x c=%08x l=%08x line=%d\n", 498 lios->c_iflag, lios->c_oflag, lios->c_cflag, 499 lios->c_lflag, (int)lios->c_line); 500 printf("c_cc "); 501 for (i=0; i<LINUX_NCCS; i++) 502 printf("%02x ", lios->c_cc[i]); 503 printf("\n"); 504 } 505 #endif 506 } 507 508 static void 509 linux_to_bsd_termios(struct linux_termios *lios, struct termios *bios) 510 { 511 int i; 512 513 #ifdef DEBUG 514 if (ldebug(ioctl)) { 515 printf("LINUX: LINUX termios structure (input):\n"); 516 printf("i=%08x o=%08x c=%08x l=%08x line=%d\n", 517 lios->c_iflag, lios->c_oflag, lios->c_cflag, 518 lios->c_lflag, (int)lios->c_line); 519 printf("c_cc "); 520 for (i=0; i<LINUX_NCCS; i++) 521 printf("%02x ", lios->c_cc[i]); 522 printf("\n"); 523 } 524 #endif 525 526 bios->c_iflag = 0; 527 if (lios->c_iflag & LINUX_IGNBRK) 528 bios->c_iflag |= IGNBRK; 529 if (lios->c_iflag & LINUX_BRKINT) 530 bios->c_iflag |= BRKINT; 531 if (lios->c_iflag & LINUX_IGNPAR) 532 bios->c_iflag |= IGNPAR; 533 if (lios->c_iflag & LINUX_PARMRK) 534 bios->c_iflag |= PARMRK; 535 if (lios->c_iflag & LINUX_INPCK) 536 bios->c_iflag |= INPCK; 537 if (lios->c_iflag & LINUX_ISTRIP) 538 bios->c_iflag |= ISTRIP; 539 if (lios->c_iflag & LINUX_INLCR) 540 bios->c_iflag |= INLCR; 541 if (lios->c_iflag & LINUX_IGNCR) 542 bios->c_iflag |= IGNCR; 543 if (lios->c_iflag & LINUX_ICRNL) 544 bios->c_iflag |= ICRNL; 545 if (lios->c_iflag & LINUX_IXON) 546 bios->c_iflag |= IXON; 547 if (lios->c_iflag & LINUX_IXANY) 548 bios->c_iflag |= IXANY; 549 if (lios->c_iflag & LINUX_IXOFF) 550 bios->c_iflag |= IXOFF; 551 if (lios->c_iflag & LINUX_IMAXBEL) 552 bios->c_iflag |= IMAXBEL; 553 554 bios->c_oflag = 0; 555 if (lios->c_oflag & LINUX_OPOST) 556 bios->c_oflag |= OPOST; 557 if (lios->c_oflag & LINUX_ONLCR) 558 bios->c_oflag |= ONLCR; 559 if (lios->c_oflag & LINUX_XTABS) 560 bios->c_oflag |= TAB3; 561 562 bios->c_cflag = (lios->c_cflag & LINUX_CSIZE) << 4; 563 if (lios->c_cflag & LINUX_CSTOPB) 564 bios->c_cflag |= CSTOPB; 565 if (lios->c_cflag & LINUX_CREAD) 566 bios->c_cflag |= CREAD; 567 if (lios->c_cflag & LINUX_PARENB) 568 bios->c_cflag |= PARENB; 569 if (lios->c_cflag & LINUX_PARODD) 570 bios->c_cflag |= PARODD; 571 if (lios->c_cflag & LINUX_HUPCL) 572 bios->c_cflag |= HUPCL; 573 if (lios->c_cflag & LINUX_CLOCAL) 574 bios->c_cflag |= CLOCAL; 575 if (lios->c_cflag & LINUX_CRTSCTS) 576 bios->c_cflag |= CRTSCTS; 577 578 bios->c_lflag = 0; 579 if (lios->c_lflag & LINUX_ISIG) 580 bios->c_lflag |= ISIG; 581 if (lios->c_lflag & LINUX_ICANON) 582 bios->c_lflag |= ICANON; 583 if (lios->c_lflag & LINUX_ECHO) 584 bios->c_lflag |= ECHO; 585 if (lios->c_lflag & LINUX_ECHOE) 586 bios->c_lflag |= ECHOE; 587 if (lios->c_lflag & LINUX_ECHOK) 588 bios->c_lflag |= ECHOK; 589 if (lios->c_lflag & LINUX_ECHONL) 590 bios->c_lflag |= ECHONL; 591 if (lios->c_lflag & LINUX_NOFLSH) 592 bios->c_lflag |= NOFLSH; 593 if (lios->c_lflag & LINUX_TOSTOP) 594 bios->c_lflag |= TOSTOP; 595 if (lios->c_lflag & LINUX_ECHOCTL) 596 bios->c_lflag |= ECHOCTL; 597 if (lios->c_lflag & LINUX_ECHOPRT) 598 bios->c_lflag |= ECHOPRT; 599 if (lios->c_lflag & LINUX_ECHOKE) 600 bios->c_lflag |= ECHOKE; 601 if (lios->c_lflag & LINUX_FLUSHO) 602 bios->c_lflag |= FLUSHO; 603 if (lios->c_lflag & LINUX_PENDIN) 604 bios->c_lflag |= PENDIN; 605 if (lios->c_lflag & LINUX_IEXTEN) 606 bios->c_lflag |= IEXTEN; 607 608 for (i=0; i<NCCS; i++) 609 bios->c_cc[i] = _POSIX_VDISABLE; 610 bios->c_cc[VINTR] = lios->c_cc[LINUX_VINTR]; 611 bios->c_cc[VQUIT] = lios->c_cc[LINUX_VQUIT]; 612 bios->c_cc[VERASE] = lios->c_cc[LINUX_VERASE]; 613 bios->c_cc[VKILL] = lios->c_cc[LINUX_VKILL]; 614 bios->c_cc[VEOF] = lios->c_cc[LINUX_VEOF]; 615 bios->c_cc[VEOL] = lios->c_cc[LINUX_VEOL]; 616 bios->c_cc[VMIN] = lios->c_cc[LINUX_VMIN]; 617 bios->c_cc[VTIME] = lios->c_cc[LINUX_VTIME]; 618 bios->c_cc[VEOL2] = lios->c_cc[LINUX_VEOL2]; 619 bios->c_cc[VSUSP] = lios->c_cc[LINUX_VSUSP]; 620 bios->c_cc[VSTART] = lios->c_cc[LINUX_VSTART]; 621 bios->c_cc[VSTOP] = lios->c_cc[LINUX_VSTOP]; 622 bios->c_cc[VREPRINT] = lios->c_cc[LINUX_VREPRINT]; 623 bios->c_cc[VDISCARD] = lios->c_cc[LINUX_VDISCARD]; 624 bios->c_cc[VWERASE] = lios->c_cc[LINUX_VWERASE]; 625 bios->c_cc[VLNEXT] = lios->c_cc[LINUX_VLNEXT]; 626 627 for (i=0; i<NCCS; i++) { 628 if (i != VMIN && i != VTIME && 629 bios->c_cc[i] == LINUX_POSIX_VDISABLE) 630 bios->c_cc[i] = _POSIX_VDISABLE; 631 } 632 633 bios->c_ispeed = bios->c_ospeed = 634 linux_to_bsd_speed(lios->c_cflag & LINUX_CBAUD, sptab); 635 636 #ifdef DEBUG 637 if (ldebug(ioctl)) { 638 printf("LINUX: BSD termios structure (output):\n"); 639 printf("i=%08x o=%08x c=%08x l=%08x ispeed=%d ospeed=%d\n", 640 bios->c_iflag, bios->c_oflag, bios->c_cflag, bios->c_lflag, 641 bios->c_ispeed, bios->c_ospeed); 642 printf("c_cc "); 643 for (i=0; i<NCCS; i++) 644 printf("%02x ", bios->c_cc[i]); 645 printf("\n"); 646 } 647 #endif 648 } 649 650 static void 651 bsd_to_linux_termio(struct termios *bios, struct linux_termio *lio) 652 { 653 struct linux_termios lios; 654 655 bsd_to_linux_termios(bios, &lios); 656 lio->c_iflag = lios.c_iflag; 657 lio->c_oflag = lios.c_oflag; 658 lio->c_cflag = lios.c_cflag; 659 lio->c_lflag = lios.c_lflag; 660 lio->c_line = lios.c_line; 661 memcpy(lio->c_cc, lios.c_cc, LINUX_NCC); 662 } 663 664 static void 665 linux_to_bsd_termio(struct linux_termio *lio, struct termios *bios) 666 { 667 struct linux_termios lios; 668 int i; 669 670 lios.c_iflag = lio->c_iflag; 671 lios.c_oflag = lio->c_oflag; 672 lios.c_cflag = lio->c_cflag; 673 lios.c_lflag = lio->c_lflag; 674 for (i=LINUX_NCC; i<LINUX_NCCS; i++) 675 lios.c_cc[i] = LINUX_POSIX_VDISABLE; 676 memcpy(lios.c_cc, lio->c_cc, LINUX_NCC); 677 linux_to_bsd_termios(&lios, bios); 678 } 679 680 static int 681 linux_ioctl_termio(struct thread *td, struct linux_ioctl_args *args) 682 { 683 struct termios bios; 684 struct linux_termios lios; 685 struct linux_termio lio; 686 struct file *fp; 687 int error; 688 689 if ((error = fget(td, args->fd, &fp)) != 0) 690 return (error); 691 692 switch (args->cmd & 0xffff) { 693 694 case LINUX_TCGETS: 695 error = fo_ioctl(fp, TIOCGETA, (caddr_t)&bios, td->td_ucred, 696 td); 697 if (error) 698 break; 699 bsd_to_linux_termios(&bios, &lios); 700 error = copyout(&lios, (void *)args->arg, sizeof(lios)); 701 break; 702 703 case LINUX_TCSETS: 704 error = copyin((void *)args->arg, &lios, sizeof(lios)); 705 if (error) 706 break; 707 linux_to_bsd_termios(&lios, &bios); 708 error = (fo_ioctl(fp, TIOCSETA, (caddr_t)&bios, td->td_ucred, 709 td)); 710 break; 711 712 case LINUX_TCSETSW: 713 error = copyin((void *)args->arg, &lios, sizeof(lios)); 714 if (error) 715 break; 716 linux_to_bsd_termios(&lios, &bios); 717 error = (fo_ioctl(fp, TIOCSETAW, (caddr_t)&bios, td->td_ucred, 718 td)); 719 break; 720 721 case LINUX_TCSETSF: 722 error = copyin((void *)args->arg, &lios, sizeof(lios)); 723 if (error) 724 break; 725 linux_to_bsd_termios(&lios, &bios); 726 error = (fo_ioctl(fp, TIOCSETAF, (caddr_t)&bios, td->td_ucred, 727 td)); 728 break; 729 730 case LINUX_TCGETA: 731 error = fo_ioctl(fp, TIOCGETA, (caddr_t)&bios, td->td_ucred, 732 td); 733 if (error) 734 break; 735 bsd_to_linux_termio(&bios, &lio); 736 error = (copyout(&lio, (void *)args->arg, sizeof(lio))); 737 break; 738 739 case LINUX_TCSETA: 740 error = copyin((void *)args->arg, &lio, sizeof(lio)); 741 if (error) 742 break; 743 linux_to_bsd_termio(&lio, &bios); 744 error = (fo_ioctl(fp, TIOCSETA, (caddr_t)&bios, td->td_ucred, 745 td)); 746 break; 747 748 case LINUX_TCSETAW: 749 error = copyin((void *)args->arg, &lio, sizeof(lio)); 750 if (error) 751 break; 752 linux_to_bsd_termio(&lio, &bios); 753 error = (fo_ioctl(fp, TIOCSETAW, (caddr_t)&bios, td->td_ucred, 754 td)); 755 break; 756 757 case LINUX_TCSETAF: 758 error = copyin((void *)args->arg, &lio, sizeof(lio)); 759 if (error) 760 break; 761 linux_to_bsd_termio(&lio, &bios); 762 error = (fo_ioctl(fp, TIOCSETAF, (caddr_t)&bios, td->td_ucred, 763 td)); 764 break; 765 766 /* LINUX_TCSBRK */ 767 768 case LINUX_TCXONC: { 769 switch (args->arg) { 770 case LINUX_TCOOFF: 771 args->cmd = TIOCSTOP; 772 break; 773 case LINUX_TCOON: 774 args->cmd = TIOCSTART; 775 break; 776 case LINUX_TCIOFF: 777 case LINUX_TCION: { 778 int c; 779 struct write_args wr; 780 error = fo_ioctl(fp, TIOCGETA, (caddr_t)&bios, 781 td->td_ucred, td); 782 if (error) 783 break; 784 fdrop(fp, td); 785 c = (args->arg == LINUX_TCIOFF) ? VSTOP : VSTART; 786 c = bios.c_cc[c]; 787 if (c != _POSIX_VDISABLE) { 788 wr.fd = args->fd; 789 wr.buf = &c; 790 wr.nbyte = sizeof(c); 791 return (write(td, &wr)); 792 } else 793 return (0); 794 } 795 default: 796 fdrop(fp, td); 797 return (EINVAL); 798 } 799 args->arg = 0; 800 error = (ioctl(td, (struct ioctl_args *)args)); 801 break; 802 } 803 804 case LINUX_TCFLSH: { 805 int val; 806 switch (args->arg) { 807 case LINUX_TCIFLUSH: 808 val = FREAD; 809 break; 810 case LINUX_TCOFLUSH: 811 val = FWRITE; 812 break; 813 case LINUX_TCIOFLUSH: 814 val = FREAD | FWRITE; 815 break; 816 default: 817 fdrop(fp, td); 818 return (EINVAL); 819 } 820 error = (fo_ioctl(fp,TIOCFLUSH,(caddr_t)&val,td->td_ucred,td)); 821 break; 822 } 823 824 case LINUX_TIOCEXCL: 825 args->cmd = TIOCEXCL; 826 error = (ioctl(td, (struct ioctl_args *)args)); 827 break; 828 829 case LINUX_TIOCNXCL: 830 args->cmd = TIOCNXCL; 831 error = (ioctl(td, (struct ioctl_args *)args)); 832 break; 833 834 case LINUX_TIOCSCTTY: 835 args->cmd = TIOCSCTTY; 836 error = (ioctl(td, (struct ioctl_args *)args)); 837 break; 838 839 case LINUX_TIOCGPGRP: 840 args->cmd = TIOCGPGRP; 841 error = (ioctl(td, (struct ioctl_args *)args)); 842 break; 843 844 case LINUX_TIOCSPGRP: 845 args->cmd = TIOCSPGRP; 846 error = (ioctl(td, (struct ioctl_args *)args)); 847 break; 848 849 /* LINUX_TIOCOUTQ */ 850 /* LINUX_TIOCSTI */ 851 852 case LINUX_TIOCGWINSZ: 853 args->cmd = TIOCGWINSZ; 854 error = (ioctl(td, (struct ioctl_args *)args)); 855 break; 856 857 case LINUX_TIOCSWINSZ: 858 args->cmd = TIOCSWINSZ; 859 error = (ioctl(td, (struct ioctl_args *)args)); 860 break; 861 862 case LINUX_TIOCMGET: 863 args->cmd = TIOCMGET; 864 error = (ioctl(td, (struct ioctl_args *)args)); 865 break; 866 867 case LINUX_TIOCMBIS: 868 args->cmd = TIOCMBIS; 869 error = (ioctl(td, (struct ioctl_args *)args)); 870 break; 871 872 case LINUX_TIOCMBIC: 873 args->cmd = TIOCMBIC; 874 error = (ioctl(td, (struct ioctl_args *)args)); 875 break; 876 877 case LINUX_TIOCMSET: 878 args->cmd = TIOCMSET; 879 error = (ioctl(td, (struct ioctl_args *)args)); 880 break; 881 882 /* TIOCGSOFTCAR */ 883 /* TIOCSSOFTCAR */ 884 885 case LINUX_FIONREAD: /* LINUX_TIOCINQ */ 886 args->cmd = FIONREAD; 887 error = (ioctl(td, (struct ioctl_args *)args)); 888 break; 889 890 /* LINUX_TIOCLINUX */ 891 892 case LINUX_TIOCCONS: 893 args->cmd = TIOCCONS; 894 error = (ioctl(td, (struct ioctl_args *)args)); 895 break; 896 897 case LINUX_TIOCGSERIAL: { 898 struct linux_serial_struct lss; 899 lss.type = LINUX_PORT_16550A; 900 lss.flags = 0; 901 lss.close_delay = 0; 902 error = copyout(&lss, (void *)args->arg, sizeof(lss)); 903 break; 904 } 905 906 case LINUX_TIOCSSERIAL: { 907 struct linux_serial_struct lss; 908 error = copyin((void *)args->arg, &lss, sizeof(lss)); 909 if (error) 910 break; 911 /* XXX - It really helps to have an implementation that 912 * does nothing. NOT! 913 */ 914 error = 0; 915 break; 916 } 917 918 case LINUX_TIOCPKT: 919 args->cmd = TIOCPKT; 920 error = (ioctl(td, (struct ioctl_args *)args)); 921 break; 922 923 case LINUX_FIONBIO: 924 args->cmd = FIONBIO; 925 error = (ioctl(td, (struct ioctl_args *)args)); 926 break; 927 928 case LINUX_TIOCNOTTY: 929 args->cmd = TIOCNOTTY; 930 error = (ioctl(td, (struct ioctl_args *)args)); 931 break; 932 933 case LINUX_TIOCSETD: { 934 int line; 935 switch (args->arg) { 936 case LINUX_N_TTY: 937 line = TTYDISC; 938 break; 939 case LINUX_N_SLIP: 940 line = SLIPDISC; 941 break; 942 case LINUX_N_PPP: 943 line = PPPDISC; 944 break; 945 default: 946 fdrop(fp, td); 947 return (EINVAL); 948 } 949 error = (fo_ioctl(fp, TIOCSETD, (caddr_t)&line, td->td_ucred, 950 td)); 951 break; 952 } 953 954 case LINUX_TIOCGETD: { 955 int linux_line; 956 int bsd_line = TTYDISC; 957 error = fo_ioctl(fp, TIOCGETD, (caddr_t)&bsd_line, 958 td->td_ucred, td); 959 if (error) 960 return (error); 961 switch (bsd_line) { 962 case TTYDISC: 963 linux_line = LINUX_N_TTY; 964 break; 965 case SLIPDISC: 966 linux_line = LINUX_N_SLIP; 967 break; 968 case PPPDISC: 969 linux_line = LINUX_N_PPP; 970 break; 971 default: 972 fdrop(fp, td); 973 return (EINVAL); 974 } 975 error = (copyout(&linux_line, (void *)args->arg, sizeof(int))); 976 break; 977 } 978 979 /* LINUX_TCSBRKP */ 980 /* LINUX_TIOCTTYGSTRUCT */ 981 982 case LINUX_FIONCLEX: 983 args->cmd = FIONCLEX; 984 error = (ioctl(td, (struct ioctl_args *)args)); 985 break; 986 987 case LINUX_FIOCLEX: 988 args->cmd = FIOCLEX; 989 error = (ioctl(td, (struct ioctl_args *)args)); 990 break; 991 992 case LINUX_FIOASYNC: 993 args->cmd = FIOASYNC; 994 error = (ioctl(td, (struct ioctl_args *)args)); 995 break; 996 997 /* LINUX_TIOCSERCONFIG */ 998 /* LINUX_TIOCSERGWILD */ 999 /* LINUX_TIOCSERSWILD */ 1000 /* LINUX_TIOCGLCKTRMIOS */ 1001 /* LINUX_TIOCSLCKTRMIOS */ 1002 1003 case LINUX_TIOCSBRK: 1004 args->cmd = TIOCSBRK; 1005 error = (ioctl(td, (struct ioctl_args *)args)); 1006 break; 1007 1008 case LINUX_TIOCCBRK: 1009 args->cmd = TIOCCBRK; 1010 error = (ioctl(td, (struct ioctl_args *)args)); 1011 break; 1012 case LINUX_TIOCGPTN: { 1013 int nb; 1014 1015 error = fo_ioctl(fp, TIOCGPTN, (caddr_t)&nb, td->td_ucred, td); 1016 if (!error) 1017 error = copyout(&nb, (void *)args->arg, 1018 sizeof(int)); 1019 break; 1020 } 1021 case LINUX_TIOCSPTLCK: 1022 /* Our unlockpt() does nothing. */ 1023 error = 0; 1024 break; 1025 default: 1026 error = ENOIOCTL; 1027 break; 1028 } 1029 1030 fdrop(fp, td); 1031 return (error); 1032 } 1033 1034 /* 1035 * CDROM related ioctls 1036 */ 1037 1038 struct linux_cdrom_msf 1039 { 1040 u_char cdmsf_min0; 1041 u_char cdmsf_sec0; 1042 u_char cdmsf_frame0; 1043 u_char cdmsf_min1; 1044 u_char cdmsf_sec1; 1045 u_char cdmsf_frame1; 1046 }; 1047 1048 struct linux_cdrom_tochdr 1049 { 1050 u_char cdth_trk0; 1051 u_char cdth_trk1; 1052 }; 1053 1054 union linux_cdrom_addr 1055 { 1056 struct { 1057 u_char minute; 1058 u_char second; 1059 u_char frame; 1060 } msf; 1061 int lba; 1062 }; 1063 1064 struct linux_cdrom_tocentry 1065 { 1066 u_char cdte_track; 1067 u_char cdte_adr:4; 1068 u_char cdte_ctrl:4; 1069 u_char cdte_format; 1070 union linux_cdrom_addr cdte_addr; 1071 u_char cdte_datamode; 1072 }; 1073 1074 struct linux_cdrom_subchnl 1075 { 1076 u_char cdsc_format; 1077 u_char cdsc_audiostatus; 1078 u_char cdsc_adr:4; 1079 u_char cdsc_ctrl:4; 1080 u_char cdsc_trk; 1081 u_char cdsc_ind; 1082 union linux_cdrom_addr cdsc_absaddr; 1083 union linux_cdrom_addr cdsc_reladdr; 1084 }; 1085 1086 struct l_cdrom_read_audio { 1087 union linux_cdrom_addr addr; 1088 u_char addr_format; 1089 l_int nframes; 1090 u_char *buf; 1091 }; 1092 1093 struct l_dvd_layer { 1094 u_char book_version:4; 1095 u_char book_type:4; 1096 u_char min_rate:4; 1097 u_char disc_size:4; 1098 u_char layer_type:4; 1099 u_char track_path:1; 1100 u_char nlayers:2; 1101 u_char track_density:4; 1102 u_char linear_density:4; 1103 u_char bca:1; 1104 u_int32_t start_sector; 1105 u_int32_t end_sector; 1106 u_int32_t end_sector_l0; 1107 }; 1108 1109 struct l_dvd_physical { 1110 u_char type; 1111 u_char layer_num; 1112 struct l_dvd_layer layer[4]; 1113 }; 1114 1115 struct l_dvd_copyright { 1116 u_char type; 1117 u_char layer_num; 1118 u_char cpst; 1119 u_char rmi; 1120 }; 1121 1122 struct l_dvd_disckey { 1123 u_char type; 1124 l_uint agid:2; 1125 u_char value[2048]; 1126 }; 1127 1128 struct l_dvd_bca { 1129 u_char type; 1130 l_int len; 1131 u_char value[188]; 1132 }; 1133 1134 struct l_dvd_manufact { 1135 u_char type; 1136 u_char layer_num; 1137 l_int len; 1138 u_char value[2048]; 1139 }; 1140 1141 typedef union { 1142 u_char type; 1143 struct l_dvd_physical physical; 1144 struct l_dvd_copyright copyright; 1145 struct l_dvd_disckey disckey; 1146 struct l_dvd_bca bca; 1147 struct l_dvd_manufact manufact; 1148 } l_dvd_struct; 1149 1150 typedef u_char l_dvd_key[5]; 1151 typedef u_char l_dvd_challenge[10]; 1152 1153 struct l_dvd_lu_send_agid { 1154 u_char type; 1155 l_uint agid:2; 1156 }; 1157 1158 struct l_dvd_host_send_challenge { 1159 u_char type; 1160 l_uint agid:2; 1161 l_dvd_challenge chal; 1162 }; 1163 1164 struct l_dvd_send_key { 1165 u_char type; 1166 l_uint agid:2; 1167 l_dvd_key key; 1168 }; 1169 1170 struct l_dvd_lu_send_challenge { 1171 u_char type; 1172 l_uint agid:2; 1173 l_dvd_challenge chal; 1174 }; 1175 1176 struct l_dvd_lu_send_title_key { 1177 u_char type; 1178 l_uint agid:2; 1179 l_dvd_key title_key; 1180 l_int lba; 1181 l_uint cpm:1; 1182 l_uint cp_sec:1; 1183 l_uint cgms:2; 1184 }; 1185 1186 struct l_dvd_lu_send_asf { 1187 u_char type; 1188 l_uint agid:2; 1189 l_uint asf:1; 1190 }; 1191 1192 struct l_dvd_host_send_rpcstate { 1193 u_char type; 1194 u_char pdrc; 1195 }; 1196 1197 struct l_dvd_lu_send_rpcstate { 1198 u_char type:2; 1199 u_char vra:3; 1200 u_char ucca:3; 1201 u_char region_mask; 1202 u_char rpc_scheme; 1203 }; 1204 1205 typedef union { 1206 u_char type; 1207 struct l_dvd_lu_send_agid lsa; 1208 struct l_dvd_host_send_challenge hsc; 1209 struct l_dvd_send_key lsk; 1210 struct l_dvd_lu_send_challenge lsc; 1211 struct l_dvd_send_key hsk; 1212 struct l_dvd_lu_send_title_key lstk; 1213 struct l_dvd_lu_send_asf lsasf; 1214 struct l_dvd_host_send_rpcstate hrpcs; 1215 struct l_dvd_lu_send_rpcstate lrpcs; 1216 } l_dvd_authinfo; 1217 1218 static void 1219 bsd_to_linux_msf_lba(u_char af, union msf_lba *bp, union linux_cdrom_addr *lp) 1220 { 1221 if (af == CD_LBA_FORMAT) 1222 lp->lba = bp->lba; 1223 else { 1224 lp->msf.minute = bp->msf.minute; 1225 lp->msf.second = bp->msf.second; 1226 lp->msf.frame = bp->msf.frame; 1227 } 1228 } 1229 1230 static void 1231 set_linux_cdrom_addr(union linux_cdrom_addr *addr, int format, int lba) 1232 { 1233 if (format == LINUX_CDROM_MSF) { 1234 addr->msf.frame = lba % 75; 1235 lba /= 75; 1236 lba += 2; 1237 addr->msf.second = lba % 60; 1238 addr->msf.minute = lba / 60; 1239 } else 1240 addr->lba = lba; 1241 } 1242 1243 static int 1244 linux_to_bsd_dvd_struct(l_dvd_struct *lp, struct dvd_struct *bp) 1245 { 1246 bp->format = lp->type; 1247 switch (bp->format) { 1248 case DVD_STRUCT_PHYSICAL: 1249 if (bp->layer_num >= 4) 1250 return (EINVAL); 1251 bp->layer_num = lp->physical.layer_num; 1252 break; 1253 case DVD_STRUCT_COPYRIGHT: 1254 bp->layer_num = lp->copyright.layer_num; 1255 break; 1256 case DVD_STRUCT_DISCKEY: 1257 bp->agid = lp->disckey.agid; 1258 break; 1259 case DVD_STRUCT_BCA: 1260 case DVD_STRUCT_MANUFACT: 1261 break; 1262 default: 1263 return (EINVAL); 1264 } 1265 return (0); 1266 } 1267 1268 static int 1269 bsd_to_linux_dvd_struct(struct dvd_struct *bp, l_dvd_struct *lp) 1270 { 1271 switch (bp->format) { 1272 case DVD_STRUCT_PHYSICAL: { 1273 struct dvd_layer *blp = (struct dvd_layer *)bp->data; 1274 struct l_dvd_layer *llp = &lp->physical.layer[bp->layer_num]; 1275 memset(llp, 0, sizeof(*llp)); 1276 llp->book_version = blp->book_version; 1277 llp->book_type = blp->book_type; 1278 llp->min_rate = blp->max_rate; 1279 llp->disc_size = blp->disc_size; 1280 llp->layer_type = blp->layer_type; 1281 llp->track_path = blp->track_path; 1282 llp->nlayers = blp->nlayers; 1283 llp->track_density = blp->track_density; 1284 llp->linear_density = blp->linear_density; 1285 llp->bca = blp->bca; 1286 llp->start_sector = blp->start_sector; 1287 llp->end_sector = blp->end_sector; 1288 llp->end_sector_l0 = blp->end_sector_l0; 1289 break; 1290 } 1291 case DVD_STRUCT_COPYRIGHT: 1292 lp->copyright.cpst = bp->cpst; 1293 lp->copyright.rmi = bp->rmi; 1294 break; 1295 case DVD_STRUCT_DISCKEY: 1296 memcpy(lp->disckey.value, bp->data, sizeof(lp->disckey.value)); 1297 break; 1298 case DVD_STRUCT_BCA: 1299 lp->bca.len = bp->length; 1300 memcpy(lp->bca.value, bp->data, sizeof(lp->bca.value)); 1301 break; 1302 case DVD_STRUCT_MANUFACT: 1303 lp->manufact.len = bp->length; 1304 memcpy(lp->manufact.value, bp->data, 1305 sizeof(lp->manufact.value)); 1306 /* lp->manufact.layer_num is unused in linux (redhat 7.0) */ 1307 break; 1308 default: 1309 return (EINVAL); 1310 } 1311 return (0); 1312 } 1313 1314 static int 1315 linux_to_bsd_dvd_authinfo(l_dvd_authinfo *lp, int *bcode, 1316 struct dvd_authinfo *bp) 1317 { 1318 switch (lp->type) { 1319 case LINUX_DVD_LU_SEND_AGID: 1320 *bcode = DVDIOCREPORTKEY; 1321 bp->format = DVD_REPORT_AGID; 1322 bp->agid = lp->lsa.agid; 1323 break; 1324 case LINUX_DVD_HOST_SEND_CHALLENGE: 1325 *bcode = DVDIOCSENDKEY; 1326 bp->format = DVD_SEND_CHALLENGE; 1327 bp->agid = lp->hsc.agid; 1328 memcpy(bp->keychal, lp->hsc.chal, 10); 1329 break; 1330 case LINUX_DVD_LU_SEND_KEY1: 1331 *bcode = DVDIOCREPORTKEY; 1332 bp->format = DVD_REPORT_KEY1; 1333 bp->agid = lp->lsk.agid; 1334 break; 1335 case LINUX_DVD_LU_SEND_CHALLENGE: 1336 *bcode = DVDIOCREPORTKEY; 1337 bp->format = DVD_REPORT_CHALLENGE; 1338 bp->agid = lp->lsc.agid; 1339 break; 1340 case LINUX_DVD_HOST_SEND_KEY2: 1341 *bcode = DVDIOCSENDKEY; 1342 bp->format = DVD_SEND_KEY2; 1343 bp->agid = lp->hsk.agid; 1344 memcpy(bp->keychal, lp->hsk.key, 5); 1345 break; 1346 case LINUX_DVD_LU_SEND_TITLE_KEY: 1347 *bcode = DVDIOCREPORTKEY; 1348 bp->format = DVD_REPORT_TITLE_KEY; 1349 bp->agid = lp->lstk.agid; 1350 bp->lba = lp->lstk.lba; 1351 break; 1352 case LINUX_DVD_LU_SEND_ASF: 1353 *bcode = DVDIOCREPORTKEY; 1354 bp->format = DVD_REPORT_ASF; 1355 bp->agid = lp->lsasf.agid; 1356 break; 1357 case LINUX_DVD_INVALIDATE_AGID: 1358 *bcode = DVDIOCREPORTKEY; 1359 bp->format = DVD_INVALIDATE_AGID; 1360 bp->agid = lp->lsa.agid; 1361 break; 1362 case LINUX_DVD_LU_SEND_RPC_STATE: 1363 *bcode = DVDIOCREPORTKEY; 1364 bp->format = DVD_REPORT_RPC; 1365 break; 1366 case LINUX_DVD_HOST_SEND_RPC_STATE: 1367 *bcode = DVDIOCSENDKEY; 1368 bp->format = DVD_SEND_RPC; 1369 bp->region = lp->hrpcs.pdrc; 1370 break; 1371 default: 1372 return (EINVAL); 1373 } 1374 return (0); 1375 } 1376 1377 static int 1378 bsd_to_linux_dvd_authinfo(struct dvd_authinfo *bp, l_dvd_authinfo *lp) 1379 { 1380 switch (lp->type) { 1381 case LINUX_DVD_LU_SEND_AGID: 1382 lp->lsa.agid = bp->agid; 1383 break; 1384 case LINUX_DVD_HOST_SEND_CHALLENGE: 1385 lp->type = LINUX_DVD_LU_SEND_KEY1; 1386 break; 1387 case LINUX_DVD_LU_SEND_KEY1: 1388 memcpy(lp->lsk.key, bp->keychal, sizeof(lp->lsk.key)); 1389 break; 1390 case LINUX_DVD_LU_SEND_CHALLENGE: 1391 memcpy(lp->lsc.chal, bp->keychal, sizeof(lp->lsc.chal)); 1392 break; 1393 case LINUX_DVD_HOST_SEND_KEY2: 1394 lp->type = LINUX_DVD_AUTH_ESTABLISHED; 1395 break; 1396 case LINUX_DVD_LU_SEND_TITLE_KEY: 1397 memcpy(lp->lstk.title_key, bp->keychal, 1398 sizeof(lp->lstk.title_key)); 1399 lp->lstk.cpm = bp->cpm; 1400 lp->lstk.cp_sec = bp->cp_sec; 1401 lp->lstk.cgms = bp->cgms; 1402 break; 1403 case LINUX_DVD_LU_SEND_ASF: 1404 lp->lsasf.asf = bp->asf; 1405 break; 1406 case LINUX_DVD_INVALIDATE_AGID: 1407 break; 1408 case LINUX_DVD_LU_SEND_RPC_STATE: 1409 lp->lrpcs.type = bp->reg_type; 1410 lp->lrpcs.vra = bp->vend_rsts; 1411 lp->lrpcs.ucca = bp->user_rsts; 1412 lp->lrpcs.region_mask = bp->region; 1413 lp->lrpcs.rpc_scheme = bp->rpc_scheme; 1414 break; 1415 case LINUX_DVD_HOST_SEND_RPC_STATE: 1416 break; 1417 default: 1418 return (EINVAL); 1419 } 1420 return (0); 1421 } 1422 1423 static int 1424 linux_ioctl_cdrom(struct thread *td, struct linux_ioctl_args *args) 1425 { 1426 struct file *fp; 1427 int error; 1428 1429 if ((error = fget(td, args->fd, &fp)) != 0) 1430 return (error); 1431 switch (args->cmd & 0xffff) { 1432 1433 case LINUX_CDROMPAUSE: 1434 args->cmd = CDIOCPAUSE; 1435 error = (ioctl(td, (struct ioctl_args *)args)); 1436 break; 1437 1438 case LINUX_CDROMRESUME: 1439 args->cmd = CDIOCRESUME; 1440 error = (ioctl(td, (struct ioctl_args *)args)); 1441 break; 1442 1443 case LINUX_CDROMPLAYMSF: 1444 args->cmd = CDIOCPLAYMSF; 1445 error = (ioctl(td, (struct ioctl_args *)args)); 1446 break; 1447 1448 case LINUX_CDROMPLAYTRKIND: 1449 args->cmd = CDIOCPLAYTRACKS; 1450 error = (ioctl(td, (struct ioctl_args *)args)); 1451 break; 1452 1453 case LINUX_CDROMREADTOCHDR: { 1454 struct ioc_toc_header th; 1455 struct linux_cdrom_tochdr lth; 1456 error = fo_ioctl(fp, CDIOREADTOCHEADER, (caddr_t)&th, 1457 td->td_ucred, td); 1458 if (!error) { 1459 lth.cdth_trk0 = th.starting_track; 1460 lth.cdth_trk1 = th.ending_track; 1461 copyout(<h, (void *)args->arg, sizeof(lth)); 1462 } 1463 break; 1464 } 1465 1466 case LINUX_CDROMREADTOCENTRY: { 1467 struct linux_cdrom_tocentry lte; 1468 struct ioc_read_toc_single_entry irtse; 1469 1470 error = copyin((void *)args->arg, <e, sizeof(lte)); 1471 if (error) 1472 break; 1473 irtse.address_format = lte.cdte_format; 1474 irtse.track = lte.cdte_track; 1475 error = fo_ioctl(fp, CDIOREADTOCENTRY, (caddr_t)&irtse, 1476 td->td_ucred, td); 1477 if (!error) { 1478 lte.cdte_ctrl = irtse.entry.control; 1479 lte.cdte_adr = irtse.entry.addr_type; 1480 bsd_to_linux_msf_lba(irtse.address_format, 1481 &irtse.entry.addr, <e.cdte_addr); 1482 error = copyout(<e, (void *)args->arg, sizeof(lte)); 1483 } 1484 break; 1485 } 1486 1487 case LINUX_CDROMSTOP: 1488 args->cmd = CDIOCSTOP; 1489 error = (ioctl(td, (struct ioctl_args *)args)); 1490 break; 1491 1492 case LINUX_CDROMSTART: 1493 args->cmd = CDIOCSTART; 1494 error = (ioctl(td, (struct ioctl_args *)args)); 1495 break; 1496 1497 case LINUX_CDROMEJECT: 1498 args->cmd = CDIOCEJECT; 1499 error = (ioctl(td, (struct ioctl_args *)args)); 1500 break; 1501 1502 /* LINUX_CDROMVOLCTRL */ 1503 1504 case LINUX_CDROMSUBCHNL: { 1505 struct linux_cdrom_subchnl sc; 1506 struct ioc_read_subchannel bsdsc; 1507 struct cd_sub_channel_info bsdinfo; 1508 1509 bsdsc.address_format = CD_LBA_FORMAT; 1510 bsdsc.data_format = CD_CURRENT_POSITION; 1511 bsdsc.track = 0; 1512 bsdsc.data_len = sizeof(bsdinfo); 1513 bsdsc.data = &bsdinfo; 1514 error = fo_ioctl(fp, CDIOCREADSUBCHANNEL_SYSSPACE, 1515 (caddr_t)&bsdsc, td->td_ucred, td); 1516 if (error) 1517 break; 1518 error = copyin((void *)args->arg, &sc, sizeof(sc)); 1519 if (error) 1520 break; 1521 sc.cdsc_audiostatus = bsdinfo.header.audio_status; 1522 sc.cdsc_adr = bsdinfo.what.position.addr_type; 1523 sc.cdsc_ctrl = bsdinfo.what.position.control; 1524 sc.cdsc_trk = bsdinfo.what.position.track_number; 1525 sc.cdsc_ind = bsdinfo.what.position.index_number; 1526 set_linux_cdrom_addr(&sc.cdsc_absaddr, sc.cdsc_format, 1527 bsdinfo.what.position.absaddr.lba); 1528 set_linux_cdrom_addr(&sc.cdsc_reladdr, sc.cdsc_format, 1529 bsdinfo.what.position.reladdr.lba); 1530 error = copyout(&sc, (void *)args->arg, sizeof(sc)); 1531 break; 1532 } 1533 1534 /* LINUX_CDROMREADMODE2 */ 1535 /* LINUX_CDROMREADMODE1 */ 1536 /* LINUX_CDROMREADAUDIO */ 1537 /* LINUX_CDROMEJECT_SW */ 1538 /* LINUX_CDROMMULTISESSION */ 1539 /* LINUX_CDROM_GET_UPC */ 1540 1541 case LINUX_CDROMRESET: 1542 args->cmd = CDIOCRESET; 1543 error = (ioctl(td, (struct ioctl_args *)args)); 1544 break; 1545 1546 /* LINUX_CDROMVOLREAD */ 1547 /* LINUX_CDROMREADRAW */ 1548 /* LINUX_CDROMREADCOOKED */ 1549 /* LINUX_CDROMSEEK */ 1550 /* LINUX_CDROMPLAYBLK */ 1551 /* LINUX_CDROMREADALL */ 1552 /* LINUX_CDROMCLOSETRAY */ 1553 /* LINUX_CDROMLOADFROMSLOT */ 1554 /* LINUX_CDROMGETSPINDOWN */ 1555 /* LINUX_CDROMSETSPINDOWN */ 1556 /* LINUX_CDROM_SET_OPTIONS */ 1557 /* LINUX_CDROM_CLEAR_OPTIONS */ 1558 /* LINUX_CDROM_SELECT_SPEED */ 1559 /* LINUX_CDROM_SELECT_DISC */ 1560 /* LINUX_CDROM_MEDIA_CHANGED */ 1561 /* LINUX_CDROM_DRIVE_STATUS */ 1562 /* LINUX_CDROM_DISC_STATUS */ 1563 /* LINUX_CDROM_CHANGER_NSLOTS */ 1564 /* LINUX_CDROM_LOCKDOOR */ 1565 /* LINUX_CDROM_DEBUG */ 1566 /* LINUX_CDROM_GET_CAPABILITY */ 1567 /* LINUX_CDROMAUDIOBUFSIZ */ 1568 1569 case LINUX_DVD_READ_STRUCT: { 1570 l_dvd_struct *lds; 1571 struct dvd_struct *bds; 1572 1573 lds = malloc(sizeof(*lds), M_LINUX, M_WAITOK); 1574 bds = malloc(sizeof(*bds), M_LINUX, M_WAITOK); 1575 error = copyin((void *)args->arg, lds, sizeof(*lds)); 1576 if (error) 1577 goto out; 1578 error = linux_to_bsd_dvd_struct(lds, bds); 1579 if (error) 1580 goto out; 1581 error = fo_ioctl(fp, DVDIOCREADSTRUCTURE, (caddr_t)bds, 1582 td->td_ucred, td); 1583 if (error) 1584 goto out; 1585 error = bsd_to_linux_dvd_struct(bds, lds); 1586 if (error) 1587 goto out; 1588 error = copyout(lds, (void *)args->arg, sizeof(*lds)); 1589 out: 1590 free(bds, M_LINUX); 1591 free(lds, M_LINUX); 1592 break; 1593 } 1594 1595 /* LINUX_DVD_WRITE_STRUCT */ 1596 1597 case LINUX_DVD_AUTH: { 1598 l_dvd_authinfo lda; 1599 struct dvd_authinfo bda; 1600 int bcode; 1601 1602 error = copyin((void *)args->arg, &lda, sizeof(lda)); 1603 if (error) 1604 break; 1605 error = linux_to_bsd_dvd_authinfo(&lda, &bcode, &bda); 1606 if (error) 1607 break; 1608 error = fo_ioctl(fp, bcode, (caddr_t)&bda, td->td_ucred, 1609 td); 1610 if (error) { 1611 if (lda.type == LINUX_DVD_HOST_SEND_KEY2) { 1612 lda.type = LINUX_DVD_AUTH_FAILURE; 1613 copyout(&lda, (void *)args->arg, sizeof(lda)); 1614 } 1615 break; 1616 } 1617 error = bsd_to_linux_dvd_authinfo(&bda, &lda); 1618 if (error) 1619 break; 1620 error = copyout(&lda, (void *)args->arg, sizeof(lda)); 1621 break; 1622 } 1623 1624 case LINUX_SCSI_GET_BUS_NUMBER: 1625 case LINUX_SCSI_GET_IDLUN: 1626 error = linux_ioctl_sg(td, args); 1627 break; 1628 1629 /* LINUX_CDROM_SEND_PACKET */ 1630 /* LINUX_CDROM_NEXT_WRITABLE */ 1631 /* LINUX_CDROM_LAST_WRITTEN */ 1632 1633 default: 1634 error = ENOIOCTL; 1635 break; 1636 } 1637 1638 fdrop(fp, td); 1639 return (error); 1640 } 1641 1642 static int 1643 linux_ioctl_vfat(struct thread *td, struct linux_ioctl_args *args) 1644 { 1645 1646 return (ENOTTY); 1647 } 1648 1649 /* 1650 * Sound related ioctls 1651 */ 1652 1653 struct linux_mixer_info { 1654 char id[16]; 1655 char name[32]; 1656 int modify_counter; 1657 int fillers[10]; 1658 }; 1659 1660 struct linux_old_mixer_info { 1661 char id[16]; 1662 char name[32]; 1663 }; 1664 1665 static u_int32_t dirbits[4] = { IOC_VOID, IOC_IN, IOC_OUT, IOC_INOUT }; 1666 1667 #define SETDIR(c) (((c) & ~IOC_DIRMASK) | dirbits[args->cmd >> 30]) 1668 1669 static int 1670 linux_ioctl_sound(struct thread *td, struct linux_ioctl_args *args) 1671 { 1672 1673 switch (args->cmd & 0xffff) { 1674 1675 case LINUX_SOUND_MIXER_WRITE_VOLUME: 1676 args->cmd = SETDIR(SOUND_MIXER_WRITE_VOLUME); 1677 return (ioctl(td, (struct ioctl_args *)args)); 1678 1679 case LINUX_SOUND_MIXER_WRITE_BASS: 1680 args->cmd = SETDIR(SOUND_MIXER_WRITE_BASS); 1681 return (ioctl(td, (struct ioctl_args *)args)); 1682 1683 case LINUX_SOUND_MIXER_WRITE_TREBLE: 1684 args->cmd = SETDIR(SOUND_MIXER_WRITE_TREBLE); 1685 return (ioctl(td, (struct ioctl_args *)args)); 1686 1687 case LINUX_SOUND_MIXER_WRITE_SYNTH: 1688 args->cmd = SETDIR(SOUND_MIXER_WRITE_SYNTH); 1689 return (ioctl(td, (struct ioctl_args *)args)); 1690 1691 case LINUX_SOUND_MIXER_WRITE_PCM: 1692 args->cmd = SETDIR(SOUND_MIXER_WRITE_PCM); 1693 return (ioctl(td, (struct ioctl_args *)args)); 1694 1695 case LINUX_SOUND_MIXER_WRITE_SPEAKER: 1696 args->cmd = SETDIR(SOUND_MIXER_WRITE_SPEAKER); 1697 return (ioctl(td, (struct ioctl_args *)args)); 1698 1699 case LINUX_SOUND_MIXER_WRITE_LINE: 1700 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE); 1701 return (ioctl(td, (struct ioctl_args *)args)); 1702 1703 case LINUX_SOUND_MIXER_WRITE_MIC: 1704 args->cmd = SETDIR(SOUND_MIXER_WRITE_MIC); 1705 return (ioctl(td, (struct ioctl_args *)args)); 1706 1707 case LINUX_SOUND_MIXER_WRITE_CD: 1708 args->cmd = SETDIR(SOUND_MIXER_WRITE_CD); 1709 return (ioctl(td, (struct ioctl_args *)args)); 1710 1711 case LINUX_SOUND_MIXER_WRITE_IMIX: 1712 args->cmd = SETDIR(SOUND_MIXER_WRITE_IMIX); 1713 return (ioctl(td, (struct ioctl_args *)args)); 1714 1715 case LINUX_SOUND_MIXER_WRITE_ALTPCM: 1716 args->cmd = SETDIR(SOUND_MIXER_WRITE_ALTPCM); 1717 return (ioctl(td, (struct ioctl_args *)args)); 1718 1719 case LINUX_SOUND_MIXER_WRITE_RECLEV: 1720 args->cmd = SETDIR(SOUND_MIXER_WRITE_RECLEV); 1721 return (ioctl(td, (struct ioctl_args *)args)); 1722 1723 case LINUX_SOUND_MIXER_WRITE_IGAIN: 1724 args->cmd = SETDIR(SOUND_MIXER_WRITE_IGAIN); 1725 return (ioctl(td, (struct ioctl_args *)args)); 1726 1727 case LINUX_SOUND_MIXER_WRITE_OGAIN: 1728 args->cmd = SETDIR(SOUND_MIXER_WRITE_OGAIN); 1729 return (ioctl(td, (struct ioctl_args *)args)); 1730 1731 case LINUX_SOUND_MIXER_WRITE_LINE1: 1732 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE1); 1733 return (ioctl(td, (struct ioctl_args *)args)); 1734 1735 case LINUX_SOUND_MIXER_WRITE_LINE2: 1736 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE2); 1737 return (ioctl(td, (struct ioctl_args *)args)); 1738 1739 case LINUX_SOUND_MIXER_WRITE_LINE3: 1740 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE3); 1741 return (ioctl(td, (struct ioctl_args *)args)); 1742 1743 case LINUX_SOUND_MIXER_INFO: { 1744 /* Key on encoded length */ 1745 switch ((args->cmd >> 16) & 0x1fff) { 1746 case 0x005c: { /* SOUND_MIXER_INFO */ 1747 struct linux_mixer_info info; 1748 bzero(&info, sizeof(info)); 1749 strncpy(info.id, "OSS", sizeof(info.id) - 1); 1750 strncpy(info.name, "FreeBSD OSS Mixer", sizeof(info.name) - 1); 1751 copyout(&info, (void *)args->arg, sizeof(info)); 1752 break; 1753 } 1754 case 0x0030: { /* SOUND_OLD_MIXER_INFO */ 1755 struct linux_old_mixer_info info; 1756 bzero(&info, sizeof(info)); 1757 strncpy(info.id, "OSS", sizeof(info.id) - 1); 1758 strncpy(info.name, "FreeBSD OSS Mixer", sizeof(info.name) - 1); 1759 copyout(&info, (void *)args->arg, sizeof(info)); 1760 break; 1761 } 1762 default: 1763 return (ENOIOCTL); 1764 } 1765 break; 1766 } 1767 1768 case LINUX_OSS_GETVERSION: { 1769 int version = linux_get_oss_version(td); 1770 return (copyout(&version, (void *)args->arg, sizeof(int))); 1771 } 1772 1773 case LINUX_SOUND_MIXER_READ_STEREODEVS: 1774 args->cmd = SOUND_MIXER_READ_STEREODEVS; 1775 return (ioctl(td, (struct ioctl_args *)args)); 1776 1777 case LINUX_SOUND_MIXER_READ_RECMASK: 1778 args->cmd = SOUND_MIXER_READ_RECMASK; 1779 return (ioctl(td, (struct ioctl_args *)args)); 1780 1781 case LINUX_SOUND_MIXER_READ_DEVMASK: 1782 args->cmd = SOUND_MIXER_READ_DEVMASK; 1783 return (ioctl(td, (struct ioctl_args *)args)); 1784 1785 case LINUX_SOUND_MIXER_WRITE_RECSRC: 1786 args->cmd = SETDIR(SOUND_MIXER_WRITE_RECSRC); 1787 return (ioctl(td, (struct ioctl_args *)args)); 1788 1789 case LINUX_SNDCTL_DSP_RESET: 1790 args->cmd = SNDCTL_DSP_RESET; 1791 return (ioctl(td, (struct ioctl_args *)args)); 1792 1793 case LINUX_SNDCTL_DSP_SYNC: 1794 args->cmd = SNDCTL_DSP_SYNC; 1795 return (ioctl(td, (struct ioctl_args *)args)); 1796 1797 case LINUX_SNDCTL_DSP_SPEED: 1798 args->cmd = SNDCTL_DSP_SPEED; 1799 return (ioctl(td, (struct ioctl_args *)args)); 1800 1801 case LINUX_SNDCTL_DSP_STEREO: 1802 args->cmd = SNDCTL_DSP_STEREO; 1803 return (ioctl(td, (struct ioctl_args *)args)); 1804 1805 case LINUX_SNDCTL_DSP_GETBLKSIZE: /* LINUX_SNDCTL_DSP_SETBLKSIZE */ 1806 args->cmd = SNDCTL_DSP_GETBLKSIZE; 1807 return (ioctl(td, (struct ioctl_args *)args)); 1808 1809 case LINUX_SNDCTL_DSP_SETFMT: 1810 args->cmd = SNDCTL_DSP_SETFMT; 1811 return (ioctl(td, (struct ioctl_args *)args)); 1812 1813 case LINUX_SOUND_PCM_WRITE_CHANNELS: 1814 args->cmd = SOUND_PCM_WRITE_CHANNELS; 1815 return (ioctl(td, (struct ioctl_args *)args)); 1816 1817 case LINUX_SOUND_PCM_WRITE_FILTER: 1818 args->cmd = SOUND_PCM_WRITE_FILTER; 1819 return (ioctl(td, (struct ioctl_args *)args)); 1820 1821 case LINUX_SNDCTL_DSP_POST: 1822 args->cmd = SNDCTL_DSP_POST; 1823 return (ioctl(td, (struct ioctl_args *)args)); 1824 1825 case LINUX_SNDCTL_DSP_SUBDIVIDE: 1826 args->cmd = SNDCTL_DSP_SUBDIVIDE; 1827 return (ioctl(td, (struct ioctl_args *)args)); 1828 1829 case LINUX_SNDCTL_DSP_SETFRAGMENT: 1830 args->cmd = SNDCTL_DSP_SETFRAGMENT; 1831 return (ioctl(td, (struct ioctl_args *)args)); 1832 1833 case LINUX_SNDCTL_DSP_GETFMTS: 1834 args->cmd = SNDCTL_DSP_GETFMTS; 1835 return (ioctl(td, (struct ioctl_args *)args)); 1836 1837 case LINUX_SNDCTL_DSP_GETOSPACE: 1838 args->cmd = SNDCTL_DSP_GETOSPACE; 1839 return (ioctl(td, (struct ioctl_args *)args)); 1840 1841 case LINUX_SNDCTL_DSP_GETISPACE: 1842 args->cmd = SNDCTL_DSP_GETISPACE; 1843 return (ioctl(td, (struct ioctl_args *)args)); 1844 1845 case LINUX_SNDCTL_DSP_NONBLOCK: 1846 args->cmd = SNDCTL_DSP_NONBLOCK; 1847 return (ioctl(td, (struct ioctl_args *)args)); 1848 1849 case LINUX_SNDCTL_DSP_GETCAPS: 1850 args->cmd = SNDCTL_DSP_GETCAPS; 1851 return (ioctl(td, (struct ioctl_args *)args)); 1852 1853 case LINUX_SNDCTL_DSP_SETTRIGGER: /* LINUX_SNDCTL_GETTRIGGER */ 1854 args->cmd = SNDCTL_DSP_SETTRIGGER; 1855 return (ioctl(td, (struct ioctl_args *)args)); 1856 1857 case LINUX_SNDCTL_DSP_GETIPTR: 1858 args->cmd = SNDCTL_DSP_GETIPTR; 1859 return (ioctl(td, (struct ioctl_args *)args)); 1860 1861 case LINUX_SNDCTL_DSP_GETOPTR: 1862 args->cmd = SNDCTL_DSP_GETOPTR; 1863 return (ioctl(td, (struct ioctl_args *)args)); 1864 1865 case LINUX_SNDCTL_DSP_SETDUPLEX: 1866 args->cmd = SNDCTL_DSP_SETDUPLEX; 1867 return (ioctl(td, (struct ioctl_args *)args)); 1868 1869 case LINUX_SNDCTL_DSP_GETODELAY: 1870 args->cmd = SNDCTL_DSP_GETODELAY; 1871 return (ioctl(td, (struct ioctl_args *)args)); 1872 1873 case LINUX_SNDCTL_SEQ_RESET: 1874 args->cmd = SNDCTL_SEQ_RESET; 1875 return (ioctl(td, (struct ioctl_args *)args)); 1876 1877 case LINUX_SNDCTL_SEQ_SYNC: 1878 args->cmd = SNDCTL_SEQ_SYNC; 1879 return (ioctl(td, (struct ioctl_args *)args)); 1880 1881 case LINUX_SNDCTL_SYNTH_INFO: 1882 args->cmd = SNDCTL_SYNTH_INFO; 1883 return (ioctl(td, (struct ioctl_args *)args)); 1884 1885 case LINUX_SNDCTL_SEQ_CTRLRATE: 1886 args->cmd = SNDCTL_SEQ_CTRLRATE; 1887 return (ioctl(td, (struct ioctl_args *)args)); 1888 1889 case LINUX_SNDCTL_SEQ_GETOUTCOUNT: 1890 args->cmd = SNDCTL_SEQ_GETOUTCOUNT; 1891 return (ioctl(td, (struct ioctl_args *)args)); 1892 1893 case LINUX_SNDCTL_SEQ_GETINCOUNT: 1894 args->cmd = SNDCTL_SEQ_GETINCOUNT; 1895 return (ioctl(td, (struct ioctl_args *)args)); 1896 1897 case LINUX_SNDCTL_SEQ_PERCMODE: 1898 args->cmd = SNDCTL_SEQ_PERCMODE; 1899 return (ioctl(td, (struct ioctl_args *)args)); 1900 1901 case LINUX_SNDCTL_FM_LOAD_INSTR: 1902 args->cmd = SNDCTL_FM_LOAD_INSTR; 1903 return (ioctl(td, (struct ioctl_args *)args)); 1904 1905 case LINUX_SNDCTL_SEQ_TESTMIDI: 1906 args->cmd = SNDCTL_SEQ_TESTMIDI; 1907 return (ioctl(td, (struct ioctl_args *)args)); 1908 1909 case LINUX_SNDCTL_SEQ_RESETSAMPLES: 1910 args->cmd = SNDCTL_SEQ_RESETSAMPLES; 1911 return (ioctl(td, (struct ioctl_args *)args)); 1912 1913 case LINUX_SNDCTL_SEQ_NRSYNTHS: 1914 args->cmd = SNDCTL_SEQ_NRSYNTHS; 1915 return (ioctl(td, (struct ioctl_args *)args)); 1916 1917 case LINUX_SNDCTL_SEQ_NRMIDIS: 1918 args->cmd = SNDCTL_SEQ_NRMIDIS; 1919 return (ioctl(td, (struct ioctl_args *)args)); 1920 1921 case LINUX_SNDCTL_MIDI_INFO: 1922 args->cmd = SNDCTL_MIDI_INFO; 1923 return (ioctl(td, (struct ioctl_args *)args)); 1924 1925 case LINUX_SNDCTL_SEQ_TRESHOLD: 1926 args->cmd = SNDCTL_SEQ_TRESHOLD; 1927 return (ioctl(td, (struct ioctl_args *)args)); 1928 1929 case LINUX_SNDCTL_SYNTH_MEMAVL: 1930 args->cmd = SNDCTL_SYNTH_MEMAVL; 1931 return (ioctl(td, (struct ioctl_args *)args)); 1932 1933 } 1934 1935 return (ENOIOCTL); 1936 } 1937 1938 /* 1939 * Console related ioctls 1940 */ 1941 1942 #define ISSIGVALID(sig) ((sig) > 0 && (sig) < NSIG) 1943 1944 static int 1945 linux_ioctl_console(struct thread *td, struct linux_ioctl_args *args) 1946 { 1947 struct file *fp; 1948 int error; 1949 1950 if ((error = fget(td, args->fd, &fp)) != 0) 1951 return (error); 1952 switch (args->cmd & 0xffff) { 1953 1954 case LINUX_KIOCSOUND: 1955 args->cmd = KIOCSOUND; 1956 error = (ioctl(td, (struct ioctl_args *)args)); 1957 break; 1958 1959 case LINUX_KDMKTONE: 1960 args->cmd = KDMKTONE; 1961 error = (ioctl(td, (struct ioctl_args *)args)); 1962 break; 1963 1964 case LINUX_KDGETLED: 1965 args->cmd = KDGETLED; 1966 error = (ioctl(td, (struct ioctl_args *)args)); 1967 break; 1968 1969 case LINUX_KDSETLED: 1970 args->cmd = KDSETLED; 1971 error = (ioctl(td, (struct ioctl_args *)args)); 1972 break; 1973 1974 case LINUX_KDSETMODE: 1975 args->cmd = KDSETMODE; 1976 error = (ioctl(td, (struct ioctl_args *)args)); 1977 break; 1978 1979 case LINUX_KDGETMODE: 1980 args->cmd = KDGETMODE; 1981 error = (ioctl(td, (struct ioctl_args *)args)); 1982 break; 1983 1984 case LINUX_KDGKBMODE: 1985 args->cmd = KDGKBMODE; 1986 error = (ioctl(td, (struct ioctl_args *)args)); 1987 break; 1988 1989 case LINUX_KDSKBMODE: { 1990 int kbdmode; 1991 switch (args->arg) { 1992 case LINUX_KBD_RAW: 1993 kbdmode = K_RAW; 1994 break; 1995 case LINUX_KBD_XLATE: 1996 kbdmode = K_XLATE; 1997 break; 1998 case LINUX_KBD_MEDIUMRAW: 1999 kbdmode = K_RAW; 2000 break; 2001 default: 2002 fdrop(fp, td); 2003 return (EINVAL); 2004 } 2005 error = (fo_ioctl(fp, KDSKBMODE, (caddr_t)&kbdmode, 2006 td->td_ucred, td)); 2007 break; 2008 } 2009 2010 case LINUX_VT_OPENQRY: 2011 args->cmd = VT_OPENQRY; 2012 error = (ioctl(td, (struct ioctl_args *)args)); 2013 break; 2014 2015 case LINUX_VT_GETMODE: 2016 args->cmd = VT_GETMODE; 2017 error = (ioctl(td, (struct ioctl_args *)args)); 2018 break; 2019 2020 case LINUX_VT_SETMODE: { 2021 struct vt_mode mode; 2022 if ((error = copyin((void *)args->arg, &mode, sizeof(mode)))) 2023 break; 2024 if (!ISSIGVALID(mode.frsig) && ISSIGVALID(mode.acqsig)) 2025 mode.frsig = mode.acqsig; 2026 if ((error = copyout(&mode, (void *)args->arg, sizeof(mode)))) 2027 break; 2028 args->cmd = VT_SETMODE; 2029 error = (ioctl(td, (struct ioctl_args *)args)); 2030 break; 2031 } 2032 2033 case LINUX_VT_GETSTATE: 2034 args->cmd = VT_GETACTIVE; 2035 error = (ioctl(td, (struct ioctl_args *)args)); 2036 break; 2037 2038 case LINUX_VT_RELDISP: 2039 args->cmd = VT_RELDISP; 2040 error = (ioctl(td, (struct ioctl_args *)args)); 2041 break; 2042 2043 case LINUX_VT_ACTIVATE: 2044 args->cmd = VT_ACTIVATE; 2045 error = (ioctl(td, (struct ioctl_args *)args)); 2046 break; 2047 2048 case LINUX_VT_WAITACTIVE: 2049 args->cmd = VT_WAITACTIVE; 2050 error = (ioctl(td, (struct ioctl_args *)args)); 2051 break; 2052 2053 default: 2054 error = ENOIOCTL; 2055 break; 2056 } 2057 2058 fdrop(fp, td); 2059 return (error); 2060 } 2061 2062 /* 2063 * Criteria for interface name translation 2064 */ 2065 #define IFP_IS_ETH(ifp) (ifp->if_type == IFT_ETHER) 2066 2067 /* 2068 * Interface function used by linprocfs (at the time of writing). It's not 2069 * used by the Linuxulator itself. 2070 */ 2071 int 2072 linux_ifname(struct ifnet *ifp, char *buffer, size_t buflen) 2073 { 2074 struct ifnet *ifscan; 2075 int ethno; 2076 2077 IFNET_RLOCK_ASSERT(); 2078 2079 /* Short-circuit non ethernet interfaces */ 2080 if (!IFP_IS_ETH(ifp)) 2081 return (strlcpy(buffer, ifp->if_xname, buflen)); 2082 2083 /* Determine the (relative) unit number for ethernet interfaces */ 2084 ethno = 0; 2085 TAILQ_FOREACH(ifscan, &V_ifnet, if_link) { 2086 if (ifscan == ifp) 2087 return (snprintf(buffer, buflen, "eth%d", ethno)); 2088 if (IFP_IS_ETH(ifscan)) 2089 ethno++; 2090 } 2091 2092 return (0); 2093 } 2094 2095 /* 2096 * Translate a Linux interface name to a FreeBSD interface name, 2097 * and return the associated ifnet structure 2098 * bsdname and lxname need to be least IFNAMSIZ bytes long, but 2099 * can point to the same buffer. 2100 */ 2101 2102 static struct ifnet * 2103 ifname_linux_to_bsd(struct thread *td, const char *lxname, char *bsdname) 2104 { 2105 struct ifnet *ifp; 2106 int len, unit; 2107 char *ep; 2108 int is_eth, index; 2109 2110 for (len = 0; len < LINUX_IFNAMSIZ; ++len) 2111 if (!isalpha(lxname[len])) 2112 break; 2113 if (len == 0 || len == LINUX_IFNAMSIZ) 2114 return (NULL); 2115 unit = (int)strtoul(lxname + len, &ep, 10); 2116 if (ep == NULL || ep == lxname + len || ep >= lxname + LINUX_IFNAMSIZ) 2117 return (NULL); 2118 index = 0; 2119 is_eth = (len == 3 && !strncmp(lxname, "eth", len)) ? 1 : 0; 2120 CURVNET_SET(TD_TO_VNET(td)); 2121 IFNET_RLOCK(); 2122 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2123 /* 2124 * Allow Linux programs to use FreeBSD names. Don't presume 2125 * we never have an interface named "eth", so don't make 2126 * the test optional based on is_eth. 2127 */ 2128 if (strncmp(ifp->if_xname, lxname, LINUX_IFNAMSIZ) == 0) 2129 break; 2130 if (is_eth && IFP_IS_ETH(ifp) && unit == index++) 2131 break; 2132 } 2133 IFNET_RUNLOCK(); 2134 CURVNET_RESTORE(); 2135 if (ifp != NULL) 2136 strlcpy(bsdname, ifp->if_xname, IFNAMSIZ); 2137 return (ifp); 2138 } 2139 2140 /* 2141 * Implement the SIOCGIFCONF ioctl 2142 */ 2143 2144 static int 2145 linux_ifconf(struct thread *td, struct ifconf *uifc) 2146 { 2147 #ifdef COMPAT_LINUX32 2148 struct l_ifconf ifc; 2149 #else 2150 struct ifconf ifc; 2151 #endif 2152 struct l_ifreq ifr; 2153 struct ifnet *ifp; 2154 struct ifaddr *ifa; 2155 struct sbuf *sb; 2156 int error, ethno, full = 0, valid_len, max_len; 2157 2158 error = copyin(uifc, &ifc, sizeof(ifc)); 2159 if (error != 0) 2160 return (error); 2161 2162 max_len = MAXPHYS - 1; 2163 2164 CURVNET_SET(TD_TO_VNET(td)); 2165 /* handle the 'request buffer size' case */ 2166 if (ifc.ifc_buf == PTROUT(NULL)) { 2167 ifc.ifc_len = 0; 2168 IFNET_RLOCK(); 2169 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2170 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2171 struct sockaddr *sa = ifa->ifa_addr; 2172 if (sa->sa_family == AF_INET) 2173 ifc.ifc_len += sizeof(ifr); 2174 } 2175 } 2176 IFNET_RUNLOCK(); 2177 error = copyout(&ifc, uifc, sizeof(ifc)); 2178 CURVNET_RESTORE(); 2179 return (error); 2180 } 2181 2182 if (ifc.ifc_len <= 0) { 2183 CURVNET_RESTORE(); 2184 return (EINVAL); 2185 } 2186 2187 again: 2188 /* Keep track of eth interfaces */ 2189 ethno = 0; 2190 if (ifc.ifc_len <= max_len) { 2191 max_len = ifc.ifc_len; 2192 full = 1; 2193 } 2194 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 2195 max_len = 0; 2196 valid_len = 0; 2197 2198 /* Return all AF_INET addresses of all interfaces */ 2199 IFNET_RLOCK(); 2200 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2201 int addrs = 0; 2202 2203 bzero(&ifr, sizeof(ifr)); 2204 if (IFP_IS_ETH(ifp)) 2205 snprintf(ifr.ifr_name, LINUX_IFNAMSIZ, "eth%d", 2206 ethno++); 2207 else 2208 strlcpy(ifr.ifr_name, ifp->if_xname, LINUX_IFNAMSIZ); 2209 2210 /* Walk the address list */ 2211 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2212 struct sockaddr *sa = ifa->ifa_addr; 2213 2214 if (sa->sa_family == AF_INET) { 2215 ifr.ifr_addr.sa_family = LINUX_AF_INET; 2216 memcpy(ifr.ifr_addr.sa_data, sa->sa_data, 2217 sizeof(ifr.ifr_addr.sa_data)); 2218 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2219 max_len += sizeof(ifr); 2220 addrs++; 2221 } 2222 2223 if (!sbuf_overflowed(sb)) 2224 valid_len = sbuf_len(sb); 2225 } 2226 if (addrs == 0) { 2227 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 2228 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2229 max_len += sizeof(ifr); 2230 2231 if (!sbuf_overflowed(sb)) 2232 valid_len = sbuf_len(sb); 2233 } 2234 } 2235 IFNET_RUNLOCK(); 2236 2237 if (valid_len != max_len && !full) { 2238 sbuf_delete(sb); 2239 goto again; 2240 } 2241 2242 ifc.ifc_len = valid_len; 2243 sbuf_finish(sb); 2244 memcpy(PTRIN(ifc.ifc_buf), sbuf_data(sb), ifc.ifc_len); 2245 error = copyout(&ifc, uifc, sizeof(ifc)); 2246 sbuf_delete(sb); 2247 CURVNET_RESTORE(); 2248 2249 return (error); 2250 } 2251 2252 static int 2253 linux_gifflags(struct thread *td, struct ifnet *ifp, struct l_ifreq *ifr) 2254 { 2255 l_short flags; 2256 2257 flags = (ifp->if_flags | ifp->if_drv_flags) & 0xffff; 2258 /* these flags have no Linux equivalent */ 2259 flags &= ~(IFF_SMART|IFF_DRV_OACTIVE|IFF_SIMPLEX| 2260 IFF_LINK0|IFF_LINK1|IFF_LINK2); 2261 /* Linux' multicast flag is in a different bit */ 2262 if (flags & IFF_MULTICAST) { 2263 flags &= ~IFF_MULTICAST; 2264 flags |= 0x1000; 2265 } 2266 2267 return (copyout(&flags, &ifr->ifr_flags, sizeof(flags))); 2268 } 2269 2270 #define ARPHRD_ETHER 1 2271 #define ARPHRD_LOOPBACK 772 2272 2273 static int 2274 linux_gifhwaddr(struct ifnet *ifp, struct l_ifreq *ifr) 2275 { 2276 struct ifaddr *ifa; 2277 struct sockaddr_dl *sdl; 2278 struct l_sockaddr lsa; 2279 2280 if (ifp->if_type == IFT_LOOP) { 2281 bzero(&lsa, sizeof(lsa)); 2282 lsa.sa_family = ARPHRD_LOOPBACK; 2283 return (copyout(&lsa, &ifr->ifr_hwaddr, sizeof(lsa))); 2284 } 2285 2286 if (ifp->if_type != IFT_ETHER) 2287 return (ENOENT); 2288 2289 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2290 sdl = (struct sockaddr_dl*)ifa->ifa_addr; 2291 if (sdl != NULL && (sdl->sdl_family == AF_LINK) && 2292 (sdl->sdl_type == IFT_ETHER)) { 2293 bzero(&lsa, sizeof(lsa)); 2294 lsa.sa_family = ARPHRD_ETHER; 2295 bcopy(LLADDR(sdl), lsa.sa_data, LINUX_IFHWADDRLEN); 2296 return (copyout(&lsa, &ifr->ifr_hwaddr, sizeof(lsa))); 2297 } 2298 } 2299 2300 return (ENOENT); 2301 } 2302 2303 2304 /* 2305 * If we fault in bsd_to_linux_ifreq() then we will fault when we call 2306 * the native ioctl(). Thus, we don't really need to check the return 2307 * value of this function. 2308 */ 2309 static int 2310 bsd_to_linux_ifreq(struct ifreq *arg) 2311 { 2312 struct ifreq ifr; 2313 size_t ifr_len = sizeof(struct ifreq); 2314 int error; 2315 2316 if ((error = copyin(arg, &ifr, ifr_len))) 2317 return (error); 2318 2319 *(u_short *)&ifr.ifr_addr = ifr.ifr_addr.sa_family; 2320 2321 error = copyout(&ifr, arg, ifr_len); 2322 2323 return (error); 2324 } 2325 2326 /* 2327 * Socket related ioctls 2328 */ 2329 2330 static int 2331 linux_ioctl_socket(struct thread *td, struct linux_ioctl_args *args) 2332 { 2333 char lifname[LINUX_IFNAMSIZ], ifname[IFNAMSIZ]; 2334 struct ifnet *ifp; 2335 struct file *fp; 2336 int error, type; 2337 2338 ifp = NULL; 2339 error = 0; 2340 2341 if ((error = fget(td, args->fd, &fp)) != 0) 2342 return (error); 2343 type = fp->f_type; 2344 fdrop(fp, td); 2345 if (type != DTYPE_SOCKET) { 2346 /* not a socket - probably a tap / vmnet device */ 2347 switch (args->cmd) { 2348 case LINUX_SIOCGIFADDR: 2349 case LINUX_SIOCSIFADDR: 2350 case LINUX_SIOCGIFFLAGS: 2351 return (linux_ioctl_special(td, args)); 2352 default: 2353 return (ENOIOCTL); 2354 } 2355 } 2356 2357 switch (args->cmd & 0xffff) { 2358 2359 case LINUX_FIOGETOWN: 2360 case LINUX_FIOSETOWN: 2361 case LINUX_SIOCADDMULTI: 2362 case LINUX_SIOCATMARK: 2363 case LINUX_SIOCDELMULTI: 2364 case LINUX_SIOCGIFCONF: 2365 case LINUX_SIOCGPGRP: 2366 case LINUX_SIOCSPGRP: 2367 case LINUX_SIOCGIFCOUNT: 2368 /* these ioctls don't take an interface name */ 2369 #ifdef DEBUG 2370 printf("%s(): ioctl %d\n", __func__, 2371 args->cmd & 0xffff); 2372 #endif 2373 break; 2374 2375 case LINUX_SIOCGIFFLAGS: 2376 case LINUX_SIOCGIFADDR: 2377 case LINUX_SIOCSIFADDR: 2378 case LINUX_SIOCGIFDSTADDR: 2379 case LINUX_SIOCGIFBRDADDR: 2380 case LINUX_SIOCGIFNETMASK: 2381 case LINUX_SIOCSIFNETMASK: 2382 case LINUX_SIOCGIFMTU: 2383 case LINUX_SIOCSIFMTU: 2384 case LINUX_SIOCSIFNAME: 2385 case LINUX_SIOCGIFHWADDR: 2386 case LINUX_SIOCSIFHWADDR: 2387 case LINUX_SIOCDEVPRIVATE: 2388 case LINUX_SIOCDEVPRIVATE+1: 2389 case LINUX_SIOCGIFINDEX: 2390 /* copy in the interface name and translate it. */ 2391 error = copyin((void *)args->arg, lifname, LINUX_IFNAMSIZ); 2392 if (error != 0) 2393 return (error); 2394 #ifdef DEBUG 2395 printf("%s(): ioctl %d on %.*s\n", __func__, 2396 args->cmd & 0xffff, LINUX_IFNAMSIZ, lifname); 2397 #endif 2398 ifp = ifname_linux_to_bsd(td, lifname, ifname); 2399 if (ifp == NULL) 2400 return (EINVAL); 2401 /* 2402 * We need to copy it back out in case we pass the 2403 * request on to our native ioctl(), which will expect 2404 * the ifreq to be in user space and have the correct 2405 * interface name. 2406 */ 2407 error = copyout(ifname, (void *)args->arg, IFNAMSIZ); 2408 if (error != 0) 2409 return (error); 2410 #ifdef DEBUG 2411 printf("%s(): %s translated to %s\n", __func__, 2412 lifname, ifname); 2413 #endif 2414 break; 2415 2416 default: 2417 return (ENOIOCTL); 2418 } 2419 2420 switch (args->cmd & 0xffff) { 2421 2422 case LINUX_FIOSETOWN: 2423 args->cmd = FIOSETOWN; 2424 error = ioctl(td, (struct ioctl_args *)args); 2425 break; 2426 2427 case LINUX_SIOCSPGRP: 2428 args->cmd = SIOCSPGRP; 2429 error = ioctl(td, (struct ioctl_args *)args); 2430 break; 2431 2432 case LINUX_FIOGETOWN: 2433 args->cmd = FIOGETOWN; 2434 error = ioctl(td, (struct ioctl_args *)args); 2435 break; 2436 2437 case LINUX_SIOCGPGRP: 2438 args->cmd = SIOCGPGRP; 2439 error = ioctl(td, (struct ioctl_args *)args); 2440 break; 2441 2442 case LINUX_SIOCATMARK: 2443 args->cmd = SIOCATMARK; 2444 error = ioctl(td, (struct ioctl_args *)args); 2445 break; 2446 2447 /* LINUX_SIOCGSTAMP */ 2448 2449 case LINUX_SIOCGIFCONF: 2450 error = linux_ifconf(td, (struct ifconf *)args->arg); 2451 break; 2452 2453 case LINUX_SIOCGIFFLAGS: 2454 args->cmd = SIOCGIFFLAGS; 2455 error = linux_gifflags(td, ifp, (struct l_ifreq *)args->arg); 2456 break; 2457 2458 case LINUX_SIOCGIFADDR: 2459 args->cmd = SIOCGIFADDR; 2460 error = ioctl(td, (struct ioctl_args *)args); 2461 bsd_to_linux_ifreq((struct ifreq *)args->arg); 2462 break; 2463 2464 case LINUX_SIOCSIFADDR: 2465 /* XXX probably doesn't work, included for completeness */ 2466 args->cmd = SIOCSIFADDR; 2467 error = ioctl(td, (struct ioctl_args *)args); 2468 break; 2469 2470 case LINUX_SIOCGIFDSTADDR: 2471 args->cmd = SIOCGIFDSTADDR; 2472 error = ioctl(td, (struct ioctl_args *)args); 2473 bsd_to_linux_ifreq((struct ifreq *)args->arg); 2474 break; 2475 2476 case LINUX_SIOCGIFBRDADDR: 2477 args->cmd = SIOCGIFBRDADDR; 2478 error = ioctl(td, (struct ioctl_args *)args); 2479 bsd_to_linux_ifreq((struct ifreq *)args->arg); 2480 break; 2481 2482 case LINUX_SIOCGIFNETMASK: 2483 args->cmd = SIOCGIFNETMASK; 2484 error = ioctl(td, (struct ioctl_args *)args); 2485 bsd_to_linux_ifreq((struct ifreq *)args->arg); 2486 break; 2487 2488 case LINUX_SIOCSIFNETMASK: 2489 error = ENOIOCTL; 2490 break; 2491 2492 case LINUX_SIOCGIFMTU: 2493 args->cmd = SIOCGIFMTU; 2494 error = ioctl(td, (struct ioctl_args *)args); 2495 break; 2496 2497 case LINUX_SIOCSIFMTU: 2498 args->cmd = SIOCSIFMTU; 2499 error = ioctl(td, (struct ioctl_args *)args); 2500 break; 2501 2502 case LINUX_SIOCSIFNAME: 2503 error = ENOIOCTL; 2504 break; 2505 2506 case LINUX_SIOCGIFHWADDR: 2507 error = linux_gifhwaddr(ifp, (struct l_ifreq *)args->arg); 2508 break; 2509 2510 case LINUX_SIOCSIFHWADDR: 2511 error = ENOIOCTL; 2512 break; 2513 2514 case LINUX_SIOCADDMULTI: 2515 args->cmd = SIOCADDMULTI; 2516 error = ioctl(td, (struct ioctl_args *)args); 2517 break; 2518 2519 case LINUX_SIOCDELMULTI: 2520 args->cmd = SIOCDELMULTI; 2521 error = ioctl(td, (struct ioctl_args *)args); 2522 break; 2523 2524 case LINUX_SIOCGIFINDEX: 2525 args->cmd = SIOCGIFINDEX; 2526 error = ioctl(td, (struct ioctl_args *)args); 2527 break; 2528 2529 case LINUX_SIOCGIFCOUNT: 2530 error = 0; 2531 break; 2532 2533 /* 2534 * XXX This is slightly bogus, but these ioctls are currently 2535 * XXX only used by the aironet (if_an) network driver. 2536 */ 2537 case LINUX_SIOCDEVPRIVATE: 2538 args->cmd = SIOCGPRIVATE_0; 2539 error = ioctl(td, (struct ioctl_args *)args); 2540 break; 2541 2542 case LINUX_SIOCDEVPRIVATE+1: 2543 args->cmd = SIOCGPRIVATE_1; 2544 error = ioctl(td, (struct ioctl_args *)args); 2545 break; 2546 } 2547 2548 if (ifp != NULL) 2549 /* restore the original interface name */ 2550 copyout(lifname, (void *)args->arg, LINUX_IFNAMSIZ); 2551 2552 #ifdef DEBUG 2553 printf("%s(): returning %d\n", __func__, error); 2554 #endif 2555 return (error); 2556 } 2557 2558 /* 2559 * Device private ioctl handler 2560 */ 2561 static int 2562 linux_ioctl_private(struct thread *td, struct linux_ioctl_args *args) 2563 { 2564 struct file *fp; 2565 int error, type; 2566 2567 if ((error = fget(td, args->fd, &fp)) != 0) 2568 return (error); 2569 type = fp->f_type; 2570 fdrop(fp, td); 2571 if (type == DTYPE_SOCKET) 2572 return (linux_ioctl_socket(td, args)); 2573 return (ENOIOCTL); 2574 } 2575 2576 /* 2577 * DRM ioctl handler (sys/dev/drm) 2578 */ 2579 static int 2580 linux_ioctl_drm(struct thread *td, struct linux_ioctl_args *args) 2581 { 2582 args->cmd = SETDIR(args->cmd); 2583 return ioctl(td, (struct ioctl_args *)args); 2584 } 2585 2586 static int 2587 linux_ioctl_sg(struct thread *td, struct linux_ioctl_args *args) 2588 { 2589 struct file *fp; 2590 u_long cmd; 2591 int error; 2592 2593 if ((error = fget(td, args->fd, &fp)) != 0) { 2594 printf("sg_linux_ioctl: fget returned %d\n", error); 2595 return (error); 2596 } 2597 cmd = args->cmd; 2598 2599 error = (fo_ioctl(fp, cmd, (caddr_t)args->arg, td->td_ucred, td)); 2600 fdrop(fp, td); 2601 return (error); 2602 } 2603 2604 /* 2605 * Video4Linux (V4L) ioctl handler 2606 */ 2607 static int 2608 linux_to_bsd_v4l_tuner(struct l_video_tuner *lvt, struct video_tuner *vt) 2609 { 2610 vt->tuner = lvt->tuner; 2611 strlcpy(vt->name, lvt->name, LINUX_VIDEO_TUNER_NAME_SIZE); 2612 vt->rangelow = lvt->rangelow; /* possible long size conversion */ 2613 vt->rangehigh = lvt->rangehigh; /* possible long size conversion */ 2614 vt->flags = lvt->flags; 2615 vt->mode = lvt->mode; 2616 vt->signal = lvt->signal; 2617 return (0); 2618 } 2619 2620 static int 2621 bsd_to_linux_v4l_tuner(struct video_tuner *vt, struct l_video_tuner *lvt) 2622 { 2623 lvt->tuner = vt->tuner; 2624 strlcpy(lvt->name, vt->name, LINUX_VIDEO_TUNER_NAME_SIZE); 2625 lvt->rangelow = vt->rangelow; /* possible long size conversion */ 2626 lvt->rangehigh = vt->rangehigh; /* possible long size conversion */ 2627 lvt->flags = vt->flags; 2628 lvt->mode = vt->mode; 2629 lvt->signal = vt->signal; 2630 return (0); 2631 } 2632 2633 #ifdef COMPAT_LINUX_V4L_CLIPLIST 2634 static int 2635 linux_to_bsd_v4l_clip(struct l_video_clip *lvc, struct video_clip *vc) 2636 { 2637 vc->x = lvc->x; 2638 vc->y = lvc->y; 2639 vc->width = lvc->width; 2640 vc->height = lvc->height; 2641 vc->next = PTRIN(lvc->next); /* possible pointer size conversion */ 2642 return (0); 2643 } 2644 #endif 2645 2646 static int 2647 linux_to_bsd_v4l_window(struct l_video_window *lvw, struct video_window *vw) 2648 { 2649 vw->x = lvw->x; 2650 vw->y = lvw->y; 2651 vw->width = lvw->width; 2652 vw->height = lvw->height; 2653 vw->chromakey = lvw->chromakey; 2654 vw->flags = lvw->flags; 2655 vw->clips = PTRIN(lvw->clips); /* possible pointer size conversion */ 2656 vw->clipcount = lvw->clipcount; 2657 return (0); 2658 } 2659 2660 static int 2661 bsd_to_linux_v4l_window(struct video_window *vw, struct l_video_window *lvw) 2662 { 2663 lvw->x = vw->x; 2664 lvw->y = vw->y; 2665 lvw->width = vw->width; 2666 lvw->height = vw->height; 2667 lvw->chromakey = vw->chromakey; 2668 lvw->flags = vw->flags; 2669 lvw->clips = PTROUT(vw->clips); /* possible pointer size conversion */ 2670 lvw->clipcount = vw->clipcount; 2671 return (0); 2672 } 2673 2674 static int 2675 linux_to_bsd_v4l_buffer(struct l_video_buffer *lvb, struct video_buffer *vb) 2676 { 2677 vb->base = PTRIN(lvb->base); /* possible pointer size conversion */ 2678 vb->height = lvb->height; 2679 vb->width = lvb->width; 2680 vb->depth = lvb->depth; 2681 vb->bytesperline = lvb->bytesperline; 2682 return (0); 2683 } 2684 2685 static int 2686 bsd_to_linux_v4l_buffer(struct video_buffer *vb, struct l_video_buffer *lvb) 2687 { 2688 lvb->base = PTROUT(vb->base); /* possible pointer size conversion */ 2689 lvb->height = vb->height; 2690 lvb->width = vb->width; 2691 lvb->depth = vb->depth; 2692 lvb->bytesperline = vb->bytesperline; 2693 return (0); 2694 } 2695 2696 static int 2697 linux_to_bsd_v4l_code(struct l_video_code *lvc, struct video_code *vc) 2698 { 2699 strlcpy(vc->loadwhat, lvc->loadwhat, LINUX_VIDEO_CODE_LOADWHAT_SIZE); 2700 vc->datasize = lvc->datasize; 2701 vc->data = PTRIN(lvc->data); /* possible pointer size conversion */ 2702 return (0); 2703 } 2704 2705 #ifdef COMPAT_LINUX_V4L_CLIPLIST 2706 static int 2707 linux_v4l_clip_copy(void *lvc, struct video_clip **ppvc) 2708 { 2709 int error; 2710 struct video_clip vclip; 2711 struct l_video_clip l_vclip; 2712 2713 error = copyin(lvc, &l_vclip, sizeof(l_vclip)); 2714 if (error) return (error); 2715 linux_to_bsd_v4l_clip(&l_vclip, &vclip); 2716 /* XXX: If there can be no concurrency: s/M_NOWAIT/M_WAITOK/ */ 2717 if ((*ppvc = malloc(sizeof(**ppvc), M_LINUX, M_NOWAIT)) == NULL) 2718 return (ENOMEM); /* XXX: linux has no ENOMEM here */ 2719 memcpy(*ppvc, &vclip, sizeof(vclip)); 2720 (*ppvc)->next = NULL; 2721 return (0); 2722 } 2723 2724 static int 2725 linux_v4l_cliplist_free(struct video_window *vw) 2726 { 2727 struct video_clip **ppvc; 2728 struct video_clip **ppvc_next; 2729 2730 for (ppvc = &(vw->clips); *ppvc != NULL; ppvc = ppvc_next) { 2731 ppvc_next = &((*ppvc)->next); 2732 free(*ppvc, M_LINUX); 2733 } 2734 vw->clips = NULL; 2735 2736 return (0); 2737 } 2738 2739 static int 2740 linux_v4l_cliplist_copy(struct l_video_window *lvw, struct video_window *vw) 2741 { 2742 int error; 2743 int clipcount; 2744 void *plvc; 2745 struct video_clip **ppvc; 2746 2747 /* 2748 * XXX: The cliplist is used to pass in a list of clipping 2749 * rectangles or, if clipcount == VIDEO_CLIP_BITMAP, a 2750 * clipping bitmap. Some Linux apps, however, appear to 2751 * leave cliplist and clips uninitialized. In any case, 2752 * the cliplist is not used by pwc(4), at the time of 2753 * writing, FreeBSD's only V4L driver. When a driver 2754 * that uses the cliplist is developed, this code may 2755 * need re-examiniation. 2756 */ 2757 error = 0; 2758 clipcount = vw->clipcount; 2759 if (clipcount == VIDEO_CLIP_BITMAP) { 2760 /* 2761 * In this case, the pointer (clips) is overloaded 2762 * to be a "void *" to a bitmap, therefore there 2763 * is no struct video_clip to copy now. 2764 */ 2765 } else if (clipcount > 0 && clipcount <= 16384) { 2766 /* 2767 * Clips points to list of clip rectangles, so 2768 * copy the list. 2769 * 2770 * XXX: Upper limit of 16384 was used here to try to 2771 * avoid cases when clipcount and clips pointer 2772 * are uninitialized and therefore have high random 2773 * values, as is the case in the Linux Skype 2774 * application. The value 16384 was chosen as that 2775 * is what is used in the Linux stradis(4) MPEG 2776 * decoder driver, the only place we found an 2777 * example of cliplist use. 2778 */ 2779 plvc = PTRIN(lvw->clips); 2780 vw->clips = NULL; 2781 ppvc = &(vw->clips); 2782 while (clipcount-- > 0) { 2783 if (plvc == 0) { 2784 error = EFAULT; 2785 break; 2786 } else { 2787 error = linux_v4l_clip_copy(plvc, ppvc); 2788 if (error) { 2789 linux_v4l_cliplist_free(vw); 2790 break; 2791 } 2792 } 2793 ppvc = &((*ppvc)->next); 2794 plvc = PTRIN(((struct l_video_clip *) plvc)->next); 2795 } 2796 } else { 2797 /* 2798 * clipcount == 0 or negative (but not VIDEO_CLIP_BITMAP) 2799 * Force cliplist to null. 2800 */ 2801 vw->clipcount = 0; 2802 vw->clips = NULL; 2803 } 2804 return (error); 2805 } 2806 #endif 2807 2808 static int 2809 linux_ioctl_v4l(struct thread *td, struct linux_ioctl_args *args) 2810 { 2811 struct file *fp; 2812 int error; 2813 struct video_tuner vtun; 2814 struct video_window vwin; 2815 struct video_buffer vbuf; 2816 struct video_code vcode; 2817 struct l_video_tuner l_vtun; 2818 struct l_video_window l_vwin; 2819 struct l_video_buffer l_vbuf; 2820 struct l_video_code l_vcode; 2821 2822 switch (args->cmd & 0xffff) { 2823 case LINUX_VIDIOCGCAP: args->cmd = VIDIOCGCAP; break; 2824 case LINUX_VIDIOCGCHAN: args->cmd = VIDIOCGCHAN; break; 2825 case LINUX_VIDIOCSCHAN: args->cmd = VIDIOCSCHAN; break; 2826 2827 case LINUX_VIDIOCGTUNER: 2828 if ((error = fget(td, args->fd, &fp)) != 0) 2829 return (error); 2830 error = copyin((void *) args->arg, &l_vtun, sizeof(l_vtun)); 2831 if (error) { 2832 fdrop(fp, td); 2833 return (error); 2834 } 2835 linux_to_bsd_v4l_tuner(&l_vtun, &vtun); 2836 error = fo_ioctl(fp, VIDIOCGTUNER, &vtun, td->td_ucred, td); 2837 if (!error) { 2838 bsd_to_linux_v4l_tuner(&vtun, &l_vtun); 2839 error = copyout(&l_vtun, (void *) args->arg, 2840 sizeof(l_vtun)); 2841 } 2842 fdrop(fp, td); 2843 return (error); 2844 2845 case LINUX_VIDIOCSTUNER: 2846 if ((error = fget(td, args->fd, &fp)) != 0) 2847 return (error); 2848 error = copyin((void *) args->arg, &l_vtun, sizeof(l_vtun)); 2849 if (error) { 2850 fdrop(fp, td); 2851 return (error); 2852 } 2853 linux_to_bsd_v4l_tuner(&l_vtun, &vtun); 2854 error = fo_ioctl(fp, VIDIOCSTUNER, &vtun, td->td_ucred, td); 2855 fdrop(fp, td); 2856 return (error); 2857 2858 case LINUX_VIDIOCGPICT: args->cmd = VIDIOCGPICT; break; 2859 case LINUX_VIDIOCSPICT: args->cmd = VIDIOCSPICT; break; 2860 case LINUX_VIDIOCCAPTURE: args->cmd = VIDIOCCAPTURE; break; 2861 2862 case LINUX_VIDIOCGWIN: 2863 if ((error = fget(td, args->fd, &fp)) != 0) 2864 return (error); 2865 error = fo_ioctl(fp, VIDIOCGWIN, &vwin, td->td_ucred, td); 2866 if (!error) { 2867 bsd_to_linux_v4l_window(&vwin, &l_vwin); 2868 error = copyout(&l_vwin, (void *) args->arg, 2869 sizeof(l_vwin)); 2870 } 2871 fdrop(fp, td); 2872 return (error); 2873 2874 case LINUX_VIDIOCSWIN: 2875 if ((error = fget(td, args->fd, &fp)) != 0) 2876 return (error); 2877 error = copyin((void *) args->arg, &l_vwin, sizeof(l_vwin)); 2878 if (error) { 2879 fdrop(fp, td); 2880 return (error); 2881 } 2882 linux_to_bsd_v4l_window(&l_vwin, &vwin); 2883 #ifdef COMPAT_LINUX_V4L_CLIPLIST 2884 error = linux_v4l_cliplist_copy(&l_vwin, &vwin); 2885 if (error) { 2886 fdrop(fp, td); 2887 return (error); 2888 } 2889 #endif 2890 error = fo_ioctl(fp, VIDIOCSWIN, &vwin, td->td_ucred, td); 2891 fdrop(fp, td); 2892 #ifdef COMPAT_LINUX_V4L_CLIPLIST 2893 linux_v4l_cliplist_free(&vwin); 2894 #endif 2895 return (error); 2896 2897 case LINUX_VIDIOCGFBUF: 2898 if ((error = fget(td, args->fd, &fp)) != 0) 2899 return (error); 2900 error = fo_ioctl(fp, VIDIOCGFBUF, &vbuf, td->td_ucred, td); 2901 if (!error) { 2902 bsd_to_linux_v4l_buffer(&vbuf, &l_vbuf); 2903 error = copyout(&l_vbuf, (void *) args->arg, 2904 sizeof(l_vbuf)); 2905 } 2906 fdrop(fp, td); 2907 return (error); 2908 2909 case LINUX_VIDIOCSFBUF: 2910 if ((error = fget(td, args->fd, &fp)) != 0) 2911 return (error); 2912 error = copyin((void *) args->arg, &l_vbuf, sizeof(l_vbuf)); 2913 if (error) { 2914 fdrop(fp, td); 2915 return (error); 2916 } 2917 linux_to_bsd_v4l_buffer(&l_vbuf, &vbuf); 2918 error = fo_ioctl(fp, VIDIOCSFBUF, &vbuf, td->td_ucred, td); 2919 fdrop(fp, td); 2920 return (error); 2921 2922 case LINUX_VIDIOCKEY: args->cmd = VIDIOCKEY; break; 2923 case LINUX_VIDIOCGFREQ: args->cmd = VIDIOCGFREQ; break; 2924 case LINUX_VIDIOCSFREQ: args->cmd = VIDIOCSFREQ; break; 2925 case LINUX_VIDIOCGAUDIO: args->cmd = VIDIOCGAUDIO; break; 2926 case LINUX_VIDIOCSAUDIO: args->cmd = VIDIOCSAUDIO; break; 2927 case LINUX_VIDIOCSYNC: args->cmd = VIDIOCSYNC; break; 2928 case LINUX_VIDIOCMCAPTURE: args->cmd = VIDIOCMCAPTURE; break; 2929 case LINUX_VIDIOCGMBUF: args->cmd = VIDIOCGMBUF; break; 2930 case LINUX_VIDIOCGUNIT: args->cmd = VIDIOCGUNIT; break; 2931 case LINUX_VIDIOCGCAPTURE: args->cmd = VIDIOCGCAPTURE; break; 2932 case LINUX_VIDIOCSCAPTURE: args->cmd = VIDIOCSCAPTURE; break; 2933 case LINUX_VIDIOCSPLAYMODE: args->cmd = VIDIOCSPLAYMODE; break; 2934 case LINUX_VIDIOCSWRITEMODE: args->cmd = VIDIOCSWRITEMODE; break; 2935 case LINUX_VIDIOCGPLAYINFO: args->cmd = VIDIOCGPLAYINFO; break; 2936 2937 case LINUX_VIDIOCSMICROCODE: 2938 if ((error = fget(td, args->fd, &fp)) != 0) 2939 return (error); 2940 error = copyin((void *) args->arg, &l_vcode, sizeof(l_vcode)); 2941 if (error) { 2942 fdrop(fp, td); 2943 return (error); 2944 } 2945 linux_to_bsd_v4l_code(&l_vcode, &vcode); 2946 error = fo_ioctl(fp, VIDIOCSMICROCODE, &vcode, td->td_ucred, td); 2947 fdrop(fp, td); 2948 return (error); 2949 2950 case LINUX_VIDIOCGVBIFMT: args->cmd = VIDIOCGVBIFMT; break; 2951 case LINUX_VIDIOCSVBIFMT: args->cmd = VIDIOCSVBIFMT; break; 2952 default: return (ENOIOCTL); 2953 } 2954 2955 error = ioctl(td, (struct ioctl_args *)args); 2956 return (error); 2957 } 2958 2959 /* 2960 * Special ioctl handler 2961 */ 2962 static int 2963 linux_ioctl_special(struct thread *td, struct linux_ioctl_args *args) 2964 { 2965 int error; 2966 2967 switch (args->cmd) { 2968 case LINUX_SIOCGIFADDR: 2969 args->cmd = SIOCGIFADDR; 2970 error = ioctl(td, (struct ioctl_args *)args); 2971 break; 2972 case LINUX_SIOCSIFADDR: 2973 args->cmd = SIOCSIFADDR; 2974 error = ioctl(td, (struct ioctl_args *)args); 2975 break; 2976 case LINUX_SIOCGIFFLAGS: 2977 args->cmd = SIOCGIFFLAGS; 2978 error = ioctl(td, (struct ioctl_args *)args); 2979 break; 2980 default: 2981 error = ENOIOCTL; 2982 } 2983 2984 return (error); 2985 } 2986 2987 /* 2988 * Support for emulators/linux-libusb. This port uses FBSD_LUSB* macros 2989 * instead of USB* ones. This lets us to provide correct values for cmd. 2990 * 0xffffffe0 -- 0xffffffff range seemed to be the least collision-prone. 2991 */ 2992 static int 2993 linux_ioctl_fbsd_usb(struct thread *td, struct linux_ioctl_args *args) 2994 { 2995 int error; 2996 2997 error = 0; 2998 switch (args->cmd) { 2999 case FBSD_LUSB_DEVICEENUMERATE: 3000 args->cmd = USB_DEVICEENUMERATE; 3001 break; 3002 case FBSD_LUSB_DEV_QUIRK_ADD: 3003 args->cmd = USB_DEV_QUIRK_ADD; 3004 break; 3005 case FBSD_LUSB_DEV_QUIRK_GET: 3006 args->cmd = USB_DEV_QUIRK_GET; 3007 break; 3008 case FBSD_LUSB_DEV_QUIRK_REMOVE: 3009 args->cmd = USB_DEV_QUIRK_REMOVE; 3010 break; 3011 case FBSD_LUSB_DO_REQUEST: 3012 args->cmd = USB_DO_REQUEST; 3013 break; 3014 case FBSD_LUSB_FS_CLEAR_STALL_SYNC: 3015 args->cmd = USB_FS_CLEAR_STALL_SYNC; 3016 break; 3017 case FBSD_LUSB_FS_CLOSE: 3018 args->cmd = USB_FS_CLOSE; 3019 break; 3020 case FBSD_LUSB_FS_COMPLETE: 3021 args->cmd = USB_FS_COMPLETE; 3022 break; 3023 case FBSD_LUSB_FS_INIT: 3024 args->cmd = USB_FS_INIT; 3025 break; 3026 case FBSD_LUSB_FS_OPEN: 3027 args->cmd = USB_FS_OPEN; 3028 break; 3029 case FBSD_LUSB_FS_START: 3030 args->cmd = USB_FS_START; 3031 break; 3032 case FBSD_LUSB_FS_STOP: 3033 args->cmd = USB_FS_STOP; 3034 break; 3035 case FBSD_LUSB_FS_UNINIT: 3036 args->cmd = USB_FS_UNINIT; 3037 break; 3038 case FBSD_LUSB_GET_CONFIG: 3039 args->cmd = USB_GET_CONFIG; 3040 break; 3041 case FBSD_LUSB_GET_DEVICEINFO: 3042 args->cmd = USB_GET_DEVICEINFO; 3043 break; 3044 case FBSD_LUSB_GET_DEVICE_DESC: 3045 args->cmd = USB_GET_DEVICE_DESC; 3046 break; 3047 case FBSD_LUSB_GET_FULL_DESC: 3048 args->cmd = USB_GET_FULL_DESC; 3049 break; 3050 case FBSD_LUSB_GET_IFACE_DRIVER: 3051 args->cmd = USB_GET_IFACE_DRIVER; 3052 break; 3053 case FBSD_LUSB_GET_PLUGTIME: 3054 args->cmd = USB_GET_PLUGTIME; 3055 break; 3056 case FBSD_LUSB_GET_POWER_MODE: 3057 args->cmd = USB_GET_POWER_MODE; 3058 break; 3059 case FBSD_LUSB_GET_REPORT_DESC: 3060 args->cmd = USB_GET_REPORT_DESC; 3061 break; 3062 case FBSD_LUSB_GET_REPORT_ID: 3063 args->cmd = USB_GET_REPORT_ID; 3064 break; 3065 case FBSD_LUSB_GET_TEMPLATE: 3066 args->cmd = USB_GET_TEMPLATE; 3067 break; 3068 case FBSD_LUSB_IFACE_DRIVER_ACTIVE: 3069 args->cmd = USB_IFACE_DRIVER_ACTIVE; 3070 break; 3071 case FBSD_LUSB_IFACE_DRIVER_DETACH: 3072 args->cmd = USB_IFACE_DRIVER_DETACH; 3073 break; 3074 case FBSD_LUSB_QUIRK_NAME_GET: 3075 args->cmd = USB_QUIRK_NAME_GET; 3076 break; 3077 case FBSD_LUSB_READ_DIR: 3078 args->cmd = USB_READ_DIR; 3079 break; 3080 case FBSD_LUSB_SET_ALTINTERFACE: 3081 args->cmd = USB_SET_ALTINTERFACE; 3082 break; 3083 case FBSD_LUSB_SET_CONFIG: 3084 args->cmd = USB_SET_CONFIG; 3085 break; 3086 case FBSD_LUSB_SET_IMMED: 3087 args->cmd = USB_SET_IMMED; 3088 break; 3089 case FBSD_LUSB_SET_POWER_MODE: 3090 args->cmd = USB_SET_POWER_MODE; 3091 break; 3092 case FBSD_LUSB_SET_TEMPLATE: 3093 args->cmd = USB_SET_TEMPLATE; 3094 break; 3095 default: 3096 error = ENOIOCTL; 3097 } 3098 if (error != ENOIOCTL) 3099 error = ioctl(td, (struct ioctl_args *)args); 3100 return (error); 3101 } 3102 3103 /* 3104 * main ioctl syscall function 3105 */ 3106 3107 int 3108 linux_ioctl(struct thread *td, struct linux_ioctl_args *args) 3109 { 3110 struct file *fp; 3111 struct handler_element *he; 3112 int error, cmd; 3113 3114 #ifdef DEBUG 3115 if (ldebug(ioctl)) 3116 printf(ARGS(ioctl, "%d, %04lx, *"), args->fd, 3117 (unsigned long)args->cmd); 3118 #endif 3119 3120 if ((error = fget(td, args->fd, &fp)) != 0) 3121 return (error); 3122 if ((fp->f_flag & (FREAD|FWRITE)) == 0) { 3123 fdrop(fp, td); 3124 return (EBADF); 3125 } 3126 3127 /* Iterate over the ioctl handlers */ 3128 cmd = args->cmd & 0xffff; 3129 sx_slock(&linux_ioctl_sx); 3130 mtx_lock(&Giant); 3131 TAILQ_FOREACH(he, &handlers, list) { 3132 if (cmd >= he->low && cmd <= he->high) { 3133 error = (*he->func)(td, args); 3134 if (error != ENOIOCTL) { 3135 mtx_unlock(&Giant); 3136 sx_sunlock(&linux_ioctl_sx); 3137 fdrop(fp, td); 3138 return (error); 3139 } 3140 } 3141 } 3142 mtx_unlock(&Giant); 3143 sx_sunlock(&linux_ioctl_sx); 3144 fdrop(fp, td); 3145 3146 linux_msg(td, "ioctl fd=%d, cmd=0x%x ('%c',%d) is not implemented", 3147 args->fd, (int)(args->cmd & 0xffff), 3148 (int)(args->cmd & 0xff00) >> 8, (int)(args->cmd & 0xff)); 3149 3150 return (EINVAL); 3151 } 3152 3153 int 3154 linux_ioctl_register_handler(struct linux_ioctl_handler *h) 3155 { 3156 struct handler_element *he, *cur; 3157 3158 if (h == NULL || h->func == NULL) 3159 return (EINVAL); 3160 3161 /* 3162 * Reuse the element if the handler is already on the list, otherwise 3163 * create a new element. 3164 */ 3165 sx_xlock(&linux_ioctl_sx); 3166 TAILQ_FOREACH(he, &handlers, list) { 3167 if (he->func == h->func) 3168 break; 3169 } 3170 if (he == NULL) { 3171 he = malloc(sizeof(*he), 3172 M_LINUX, M_WAITOK); 3173 he->func = h->func; 3174 } else 3175 TAILQ_REMOVE(&handlers, he, list); 3176 3177 /* Initialize range information. */ 3178 he->low = h->low; 3179 he->high = h->high; 3180 he->span = h->high - h->low + 1; 3181 3182 /* Add the element to the list, sorted on span. */ 3183 TAILQ_FOREACH(cur, &handlers, list) { 3184 if (cur->span > he->span) { 3185 TAILQ_INSERT_BEFORE(cur, he, list); 3186 sx_xunlock(&linux_ioctl_sx); 3187 return (0); 3188 } 3189 } 3190 TAILQ_INSERT_TAIL(&handlers, he, list); 3191 sx_xunlock(&linux_ioctl_sx); 3192 3193 return (0); 3194 } 3195 3196 int 3197 linux_ioctl_unregister_handler(struct linux_ioctl_handler *h) 3198 { 3199 struct handler_element *he; 3200 3201 if (h == NULL || h->func == NULL) 3202 return (EINVAL); 3203 3204 sx_xlock(&linux_ioctl_sx); 3205 TAILQ_FOREACH(he, &handlers, list) { 3206 if (he->func == h->func) { 3207 TAILQ_REMOVE(&handlers, he, list); 3208 sx_xunlock(&linux_ioctl_sx); 3209 free(he, M_LINUX); 3210 return (0); 3211 } 3212 } 3213 sx_xunlock(&linux_ioctl_sx); 3214 3215 return (EINVAL); 3216 } 3217