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