1 /* $NetBSD: ossaudio.c,v 1.47 2005/02/26 23:10:21 perry Exp $ */ 2 3 /*- 4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the NetBSD 18 * Foundation, Inc. and its contributors. 19 * 4. Neither the name of The NetBSD Foundation nor the names of its 20 * contributors may be used to endorse or promote products derived 21 * from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: ossaudio.c,v 1.47 2005/02/26 23:10:21 perry Exp $"); 38 39 #include <sys/param.h> 40 #include <sys/proc.h> 41 #include <sys/systm.h> 42 #include <sys/file.h> 43 #include <sys/vnode.h> 44 #include <sys/filedesc.h> 45 #include <sys/ioctl.h> 46 #include <sys/mount.h> 47 #include <sys/kernel.h> 48 #include <sys/audioio.h> 49 #include <sys/midiio.h> 50 51 #include <sys/sa.h> 52 #include <sys/syscallargs.h> 53 54 #include <compat/ossaudio/ossaudio.h> 55 #include <compat/ossaudio/ossaudiovar.h> 56 57 #ifdef AUDIO_DEBUG 58 #define DPRINTF(x) if (ossdebug) printf x 59 int ossdebug = 0; 60 #else 61 #define DPRINTF(x) 62 #endif 63 64 #define TO_OSSVOL(x) (((x) * 100 + 127) / 255) 65 #define FROM_OSSVOL(x) ((((x) > 100 ? 100 : (x)) * 255 + 50) / 100) 66 67 static struct audiodevinfo *getdevinfo __P((struct file *, struct proc *)); 68 static int opaque_to_enum(struct audiodevinfo *di, audio_mixer_name_t *label, int opq); 69 static int enum_to_ord(struct audiodevinfo *di, int enm); 70 static int enum_to_mask(struct audiodevinfo *di, int enm); 71 72 static void setblocksize __P((struct file *, struct audio_info *, struct proc *)); 73 74 75 int 76 oss_ioctl_audio(p, uap, retval) 77 struct proc *p; 78 struct oss_sys_ioctl_args /* { 79 syscallarg(int) fd; 80 syscallarg(u_long) com; 81 syscallarg(caddr_t) data; 82 } */ *uap; 83 register_t *retval; 84 { 85 struct file *fp; 86 struct filedesc *fdp; 87 u_long com; 88 struct audio_info tmpinfo; 89 struct audio_offset tmpoffs; 90 struct oss_audio_buf_info bufinfo; 91 struct oss_count_info cntinfo; 92 struct audio_encoding tmpenc; 93 u_int u; 94 int idat, idata; 95 int error = 0; 96 int (*ioctlf)(struct file *, u_long, void *, struct proc *); 97 98 fdp = p->p_fd; 99 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL) 100 return (EBADF); 101 102 FILE_USE(fp); 103 104 if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 105 error = EBADF; 106 goto out; 107 } 108 109 com = SCARG(uap, com); 110 DPRINTF(("oss_ioctl_audio: com=%08lx\n", com)); 111 112 retval[0] = 0; 113 114 ioctlf = fp->f_ops->fo_ioctl; 115 switch (com) { 116 case OSS_SNDCTL_DSP_RESET: 117 error = ioctlf(fp, AUDIO_FLUSH, (caddr_t)0, p); 118 if (error) 119 goto out; 120 break; 121 case OSS_SNDCTL_DSP_SYNC: 122 error = ioctlf(fp, AUDIO_DRAIN, (caddr_t)0, p); 123 if (error) 124 goto out; 125 break; 126 case OSS_SNDCTL_DSP_POST: 127 /* This call is merely advisory, and may be a nop. */ 128 break; 129 case OSS_SNDCTL_DSP_SPEED: 130 AUDIO_INITINFO(&tmpinfo); 131 error = copyin(SCARG(uap, data), &idat, sizeof idat); 132 if (error) 133 goto out; 134 tmpinfo.play.sample_rate = 135 tmpinfo.record.sample_rate = idat; 136 error = ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p); 137 DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_SPEED %d = %d\n", 138 idat, error)); 139 if (error) 140 goto out; 141 /* fall into ... */ 142 case OSS_SOUND_PCM_READ_RATE: 143 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p); 144 if (error) 145 goto out; 146 idat = tmpinfo.play.sample_rate; 147 error = copyout(&idat, SCARG(uap, data), sizeof idat); 148 if (error) 149 goto out; 150 break; 151 case OSS_SNDCTL_DSP_STEREO: 152 AUDIO_INITINFO(&tmpinfo); 153 error = copyin(SCARG(uap, data), &idat, sizeof idat); 154 if (error) 155 goto out; 156 tmpinfo.play.channels = 157 tmpinfo.record.channels = idat ? 2 : 1; 158 (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p); 159 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p); 160 if (error) 161 goto out; 162 idat = tmpinfo.play.channels - 1; 163 error = copyout(&idat, SCARG(uap, data), sizeof idat); 164 if (error) 165 goto out; 166 break; 167 case OSS_SNDCTL_DSP_GETBLKSIZE: 168 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p); 169 if (error) 170 goto out; 171 setblocksize(fp, &tmpinfo, p); 172 idat = tmpinfo.blocksize; 173 error = copyout(&idat, SCARG(uap, data), sizeof idat); 174 if (error) 175 goto out; 176 break; 177 case OSS_SNDCTL_DSP_SETFMT: 178 AUDIO_INITINFO(&tmpinfo); 179 error = copyin(SCARG(uap, data), &idat, sizeof idat); 180 if (error) 181 goto out; 182 switch (idat) { 183 case OSS_AFMT_MU_LAW: 184 tmpinfo.play.precision = 185 tmpinfo.record.precision = 8; 186 tmpinfo.play.encoding = 187 tmpinfo.record.encoding = AUDIO_ENCODING_ULAW; 188 break; 189 case OSS_AFMT_A_LAW: 190 tmpinfo.play.precision = 191 tmpinfo.record.precision = 8; 192 tmpinfo.play.encoding = 193 tmpinfo.record.encoding = AUDIO_ENCODING_ALAW; 194 break; 195 case OSS_AFMT_U8: 196 tmpinfo.play.precision = 197 tmpinfo.record.precision = 8; 198 tmpinfo.play.encoding = 199 tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR; 200 break; 201 case OSS_AFMT_S8: 202 tmpinfo.play.precision = 203 tmpinfo.record.precision = 8; 204 tmpinfo.play.encoding = 205 tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR; 206 break; 207 case OSS_AFMT_S16_LE: 208 tmpinfo.play.precision = 209 tmpinfo.record.precision = 16; 210 tmpinfo.play.encoding = 211 tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE; 212 break; 213 case OSS_AFMT_S16_BE: 214 tmpinfo.play.precision = 215 tmpinfo.record.precision = 16; 216 tmpinfo.play.encoding = 217 tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_BE; 218 break; 219 case OSS_AFMT_U16_LE: 220 tmpinfo.play.precision = 221 tmpinfo.record.precision = 16; 222 tmpinfo.play.encoding = 223 tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_LE; 224 break; 225 case OSS_AFMT_U16_BE: 226 tmpinfo.play.precision = 227 tmpinfo.record.precision = 16; 228 tmpinfo.play.encoding = 229 tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_BE; 230 break; 231 default: 232 error = EINVAL; 233 goto out; 234 } 235 (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p); 236 /* fall into ... */ 237 case OSS_SOUND_PCM_READ_BITS: 238 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p); 239 if (error) 240 goto out; 241 switch (tmpinfo.play.encoding) { 242 case AUDIO_ENCODING_ULAW: 243 idat = OSS_AFMT_MU_LAW; 244 break; 245 case AUDIO_ENCODING_ALAW: 246 idat = OSS_AFMT_A_LAW; 247 break; 248 case AUDIO_ENCODING_SLINEAR_LE: 249 if (tmpinfo.play.precision == 16) 250 idat = OSS_AFMT_S16_LE; 251 else 252 idat = OSS_AFMT_S8; 253 break; 254 case AUDIO_ENCODING_SLINEAR_BE: 255 if (tmpinfo.play.precision == 16) 256 idat = OSS_AFMT_S16_BE; 257 else 258 idat = OSS_AFMT_S8; 259 break; 260 case AUDIO_ENCODING_ULINEAR_LE: 261 if (tmpinfo.play.precision == 16) 262 idat = OSS_AFMT_U16_LE; 263 else 264 idat = OSS_AFMT_U8; 265 break; 266 case AUDIO_ENCODING_ULINEAR_BE: 267 if (tmpinfo.play.precision == 16) 268 idat = OSS_AFMT_U16_BE; 269 else 270 idat = OSS_AFMT_U8; 271 break; 272 case AUDIO_ENCODING_ADPCM: 273 idat = OSS_AFMT_IMA_ADPCM; 274 break; 275 } 276 error = copyout(&idat, SCARG(uap, data), sizeof idat); 277 if (error) 278 goto out; 279 break; 280 case OSS_SNDCTL_DSP_CHANNELS: 281 AUDIO_INITINFO(&tmpinfo); 282 error = copyin(SCARG(uap, data), &idat, sizeof idat); 283 if (error) 284 goto out; 285 tmpinfo.play.channels = 286 tmpinfo.record.channels = idat; 287 (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p); 288 /* fall into ... */ 289 case OSS_SOUND_PCM_READ_CHANNELS: 290 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p); 291 if (error) 292 goto out; 293 idat = tmpinfo.play.channels; 294 error = copyout(&idat, SCARG(uap, data), sizeof idat); 295 if (error) 296 goto out; 297 break; 298 case OSS_SOUND_PCM_WRITE_FILTER: 299 case OSS_SOUND_PCM_READ_FILTER: 300 error = EINVAL; /* XXX unimplemented */ 301 goto out; 302 case OSS_SNDCTL_DSP_SUBDIVIDE: 303 error = copyin(SCARG(uap, data), &idat, sizeof idat); 304 if (error) 305 goto out; 306 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p); 307 setblocksize(fp, &tmpinfo, p); 308 if (error) 309 goto out; 310 if (idat == 0) 311 idat = tmpinfo.play.buffer_size / tmpinfo.blocksize; 312 idat = (tmpinfo.play.buffer_size / idat) & -4; 313 AUDIO_INITINFO(&tmpinfo); 314 tmpinfo.blocksize = idat; 315 error = ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p); 316 if (error) 317 goto out; 318 idat = tmpinfo.play.buffer_size / tmpinfo.blocksize; 319 error = copyout(&idat, SCARG(uap, data), sizeof idat); 320 if (error) 321 goto out; 322 break; 323 case OSS_SNDCTL_DSP_SETFRAGMENT: 324 AUDIO_INITINFO(&tmpinfo); 325 error = copyin(SCARG(uap, data), &idat, sizeof idat); 326 if (error) 327 goto out; 328 if ((idat & 0xffff) < 4 || (idat & 0xffff) > 17) { 329 error = EINVAL; 330 goto out; 331 } 332 tmpinfo.blocksize = 1 << (idat & 0xffff); 333 tmpinfo.hiwat = (idat >> 16) & 0x7fff; 334 DPRINTF(("oss_audio: SETFRAGMENT blksize=%d, hiwat=%d\n", 335 tmpinfo.blocksize, tmpinfo.hiwat)); 336 if (tmpinfo.hiwat == 0) /* 0 means set to max */ 337 tmpinfo.hiwat = 65536; 338 (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p); 339 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p); 340 if (error) 341 goto out; 342 u = tmpinfo.blocksize; 343 for(idat = 0; u > 1; idat++, u >>= 1) 344 ; 345 idat |= (tmpinfo.hiwat & 0x7fff) << 16; 346 error = copyout(&idat, SCARG(uap, data), sizeof idat); 347 if (error) 348 goto out; 349 break; 350 case OSS_SNDCTL_DSP_GETFMTS: 351 for(idat = 0, tmpenc.index = 0; 352 ioctlf(fp, AUDIO_GETENC, (caddr_t)&tmpenc, p) == 0; 353 tmpenc.index++) { 354 switch(tmpenc.encoding) { 355 case AUDIO_ENCODING_ULAW: 356 idat |= OSS_AFMT_MU_LAW; 357 break; 358 case AUDIO_ENCODING_ALAW: 359 idat |= OSS_AFMT_A_LAW; 360 break; 361 case AUDIO_ENCODING_SLINEAR: 362 idat |= OSS_AFMT_S8; 363 break; 364 case AUDIO_ENCODING_SLINEAR_LE: 365 if (tmpenc.precision == 16) 366 idat |= OSS_AFMT_S16_LE; 367 else 368 idat |= OSS_AFMT_S8; 369 break; 370 case AUDIO_ENCODING_SLINEAR_BE: 371 if (tmpenc.precision == 16) 372 idat |= OSS_AFMT_S16_BE; 373 else 374 idat |= OSS_AFMT_S8; 375 break; 376 case AUDIO_ENCODING_ULINEAR: 377 idat |= OSS_AFMT_U8; 378 break; 379 case AUDIO_ENCODING_ULINEAR_LE: 380 if (tmpenc.precision == 16) 381 idat |= OSS_AFMT_U16_LE; 382 else 383 idat |= OSS_AFMT_U8; 384 break; 385 case AUDIO_ENCODING_ULINEAR_BE: 386 if (tmpenc.precision == 16) 387 idat |= OSS_AFMT_U16_BE; 388 else 389 idat |= OSS_AFMT_U8; 390 break; 391 case AUDIO_ENCODING_ADPCM: 392 idat |= OSS_AFMT_IMA_ADPCM; 393 break; 394 default: 395 break; 396 } 397 } 398 DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_GETFMTS = %x\n", idat)); 399 error = copyout(&idat, SCARG(uap, data), sizeof idat); 400 if (error) 401 goto out; 402 break; 403 case OSS_SNDCTL_DSP_GETOSPACE: 404 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p); 405 if (error) 406 goto out; 407 setblocksize(fp, &tmpinfo, p); 408 bufinfo.fragsize = tmpinfo.blocksize; 409 bufinfo.fragments = tmpinfo.hiwat - 410 (tmpinfo.play.seek + tmpinfo.blocksize - 1) / 411 tmpinfo.blocksize; 412 bufinfo.fragstotal = tmpinfo.hiwat; 413 bufinfo.bytes = 414 tmpinfo.hiwat * tmpinfo.blocksize - tmpinfo.play.seek; 415 error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo); 416 if (error) 417 goto out; 418 break; 419 case OSS_SNDCTL_DSP_GETISPACE: 420 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p); 421 if (error) 422 goto out; 423 setblocksize(fp, &tmpinfo, p); 424 bufinfo.fragsize = tmpinfo.blocksize; 425 bufinfo.fragments = tmpinfo.hiwat - 426 (tmpinfo.record.seek + tmpinfo.blocksize - 1) / 427 tmpinfo.blocksize; 428 bufinfo.fragstotal = tmpinfo.hiwat; 429 bufinfo.bytes = 430 tmpinfo.hiwat * tmpinfo.blocksize - tmpinfo.record.seek; 431 DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_GETxSPACE = %d %d %d %d\n", 432 bufinfo.fragsize, bufinfo.fragments, 433 bufinfo.fragstotal, bufinfo.bytes)); 434 error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo); 435 if (error) 436 goto out; 437 break; 438 case OSS_SNDCTL_DSP_NONBLOCK: 439 idat = 1; 440 error = ioctlf(fp, FIONBIO, (caddr_t)&idat, p); 441 if (error) 442 goto out; 443 break; 444 case OSS_SNDCTL_DSP_GETCAPS: 445 error = ioctlf(fp, AUDIO_GETPROPS, (caddr_t)&idata, p); 446 if (error) 447 goto out; 448 idat = OSS_DSP_CAP_TRIGGER; /* pretend we have trigger */ 449 if (idata & AUDIO_PROP_FULLDUPLEX) 450 idat |= OSS_DSP_CAP_DUPLEX; 451 if (idata & AUDIO_PROP_MMAP) 452 idat |= OSS_DSP_CAP_MMAP; 453 DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_GETCAPS = %x\n", idat)); 454 error = copyout(&idat, SCARG(uap, data), sizeof idat); 455 if (error) 456 goto out; 457 break; 458 #if 0 459 case OSS_SNDCTL_DSP_GETTRIGGER: 460 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p); 461 if (error) 462 goto out; 463 idat = (tmpinfo.play.pause ? 0 : OSS_PCM_ENABLE_OUTPUT) | 464 (tmpinfo.record.pause ? 0 : OSS_PCM_ENABLE_INPUT); 465 error = copyout(&idat, SCARG(uap, data), sizeof idat); 466 if (error) 467 goto out; 468 break; 469 case OSS_SNDCTL_DSP_SETTRIGGER: 470 AUDIO_INITINFO(&tmpinfo); 471 error = copyin(SCARG(uap, data), &idat, sizeof idat); 472 if (error) 473 goto out; 474 tmpinfo.play.pause = (idat & OSS_PCM_ENABLE_OUTPUT) == 0; 475 tmpinfo.record.pause = (idat & OSS_PCM_ENABLE_INPUT) == 0; 476 (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p); 477 error = copyout(&idat, SCARG(uap, data), sizeof idat); 478 if (error) 479 goto out; 480 break; 481 #else 482 case OSS_SNDCTL_DSP_GETTRIGGER: 483 case OSS_SNDCTL_DSP_SETTRIGGER: 484 /* XXX Do nothing for now. */ 485 idat = OSS_PCM_ENABLE_OUTPUT; 486 error = copyout(&idat, SCARG(uap, data), sizeof idat); 487 goto out; 488 #endif 489 case OSS_SNDCTL_DSP_GETIPTR: 490 error = ioctlf(fp, AUDIO_GETIOFFS, (caddr_t)&tmpoffs, p); 491 if (error) 492 goto out; 493 cntinfo.bytes = tmpoffs.samples; 494 cntinfo.blocks = tmpoffs.deltablks; 495 cntinfo.ptr = tmpoffs.offset; 496 error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo); 497 if (error) 498 goto out; 499 break; 500 case OSS_SNDCTL_DSP_GETOPTR: 501 error = ioctlf(fp, AUDIO_GETOOFFS, (caddr_t)&tmpoffs, p); 502 if (error) 503 goto out; 504 cntinfo.bytes = tmpoffs.samples; 505 cntinfo.blocks = tmpoffs.deltablks; 506 cntinfo.ptr = tmpoffs.offset; 507 error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo); 508 if (error) 509 goto out; 510 break; 511 case OSS_SNDCTL_DSP_SETDUPLEX: 512 idat = 1; 513 error = ioctlf(fp, AUDIO_SETFD, (caddr_t)&idat, p); 514 goto out; 515 case OSS_SNDCTL_DSP_MAPINBUF: 516 case OSS_SNDCTL_DSP_MAPOUTBUF: 517 case OSS_SNDCTL_DSP_SETSYNCRO: 518 case OSS_SNDCTL_DSP_PROFILE: 519 error = EINVAL; 520 goto out; 521 default: 522 error = EINVAL; 523 goto out; 524 } 525 526 out: 527 FILE_UNUSE(fp, p); 528 return error; 529 } 530 531 /* If the NetBSD mixer device should have more than 32 devices 532 * some will not be available to Linux */ 533 #define NETBSD_MAXDEVS 64 534 struct audiodevinfo { 535 int done; 536 dev_t dev; 537 int16_t devmap[OSS_SOUND_MIXER_NRDEVICES], 538 rdevmap[NETBSD_MAXDEVS]; 539 char names[NETBSD_MAXDEVS][MAX_AUDIO_DEV_LEN]; 540 int enum2opaque[NETBSD_MAXDEVS]; 541 u_long devmask, recmask, stereomask; 542 u_long caps, source; 543 }; 544 545 static int 546 opaque_to_enum(struct audiodevinfo *di, audio_mixer_name_t *label, int opq) 547 { 548 int i, o; 549 550 for (i = 0; i < NETBSD_MAXDEVS; i++) { 551 o = di->enum2opaque[i]; 552 if (o == opq) 553 break; 554 if (o == -1 && label != NULL && 555 !strncmp(di->names[i], label->name, sizeof di->names[i])) { 556 di->enum2opaque[i] = opq; 557 break; 558 } 559 } 560 if (i >= NETBSD_MAXDEVS) 561 i = -1; 562 /*printf("opq_to_enum %s %d -> %d\n", label->name, opq, i);*/ 563 return (i); 564 } 565 566 static int 567 enum_to_ord(struct audiodevinfo *di, int enm) 568 { 569 if (enm >= NETBSD_MAXDEVS) 570 return (-1); 571 572 /*printf("enum_to_ord %d -> %d\n", enm, di->enum2opaque[enm]);*/ 573 return (di->enum2opaque[enm]); 574 } 575 576 static int 577 enum_to_mask(struct audiodevinfo *di, int enm) 578 { 579 int m; 580 if (enm >= NETBSD_MAXDEVS) 581 return (0); 582 583 m = di->enum2opaque[enm]; 584 if (m == -1) 585 m = 0; 586 /*printf("enum_to_mask %d -> %d\n", enm, di->enum2opaque[enm]);*/ 587 return (m); 588 } 589 590 /* 591 * Collect the audio device information to allow faster 592 * emulation of the Linux mixer ioctls. Cache the information 593 * to eliminate the overhead of repeating all the ioctls needed 594 * to collect the information. 595 */ 596 static struct audiodevinfo * 597 getdevinfo(fp, p) 598 struct file *fp; 599 struct proc *p; 600 { 601 mixer_devinfo_t mi; 602 int i, j, e; 603 static const struct { 604 const char *name; 605 int code; 606 } *dp, devs[] = { 607 { AudioNmicrophone, OSS_SOUND_MIXER_MIC }, 608 { AudioNline, OSS_SOUND_MIXER_LINE }, 609 { AudioNcd, OSS_SOUND_MIXER_CD }, 610 { AudioNdac, OSS_SOUND_MIXER_PCM }, 611 { AudioNaux, OSS_SOUND_MIXER_LINE1 }, 612 { AudioNrecord, OSS_SOUND_MIXER_IMIX }, 613 { AudioNmaster, OSS_SOUND_MIXER_VOLUME }, 614 { AudioNtreble, OSS_SOUND_MIXER_TREBLE }, 615 { AudioNbass, OSS_SOUND_MIXER_BASS }, 616 { AudioNspeaker, OSS_SOUND_MIXER_SPEAKER }, 617 /* { AudioNheadphone, ?? },*/ 618 { AudioNoutput, OSS_SOUND_MIXER_OGAIN }, 619 { AudioNinput, OSS_SOUND_MIXER_IGAIN }, 620 /* { AudioNmaster, OSS_SOUND_MIXER_SPEAKER },*/ 621 /* { AudioNstereo, ?? },*/ 622 /* { AudioNmono, ?? },*/ 623 { AudioNfmsynth, OSS_SOUND_MIXER_SYNTH }, 624 /* { AudioNwave, OSS_SOUND_MIXER_PCM },*/ 625 { AudioNmidi, OSS_SOUND_MIXER_SYNTH }, 626 /* { AudioNmixerout, ?? },*/ 627 { 0, -1 } 628 }; 629 int (*ioctlf)(struct file *, u_long, void *, struct proc *) = 630 fp->f_ops->fo_ioctl; 631 struct vnode *vp; 632 struct vattr va; 633 static struct audiodevinfo devcache = { 0 }; 634 struct audiodevinfo *di = &devcache; 635 int mlen, dlen; 636 637 /* 638 * Figure out what device it is so we can check if the 639 * cached data is valid. 640 */ 641 vp = (struct vnode *)fp->f_data; 642 if (vp->v_type != VCHR) 643 return 0; 644 if (VOP_GETATTR(vp, &va, p->p_ucred, p)) 645 return 0; 646 if (di->done && di->dev == va.va_rdev) 647 return di; 648 649 di->done = 1; 650 di->dev = va.va_rdev; 651 di->devmask = 0; 652 di->recmask = 0; 653 di->stereomask = 0; 654 di->source = ~0; 655 di->caps = 0; 656 for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++) 657 di->devmap[i] = -1; 658 for(i = 0; i < NETBSD_MAXDEVS; i++) { 659 di->rdevmap[i] = -1; 660 di->names[i][0] = '\0'; 661 di->enum2opaque[i] = -1; 662 } 663 for(i = 0; i < NETBSD_MAXDEVS; i++) { 664 mi.index = i; 665 if (ioctlf(fp, AUDIO_MIXER_DEVINFO, (caddr_t)&mi, p) < 0) 666 break; 667 switch(mi.type) { 668 case AUDIO_MIXER_VALUE: 669 for(dp = devs; dp->name; dp++) { 670 if (strcmp(dp->name, mi.label.name) == 0) 671 break; 672 dlen = strlen(dp->name); 673 mlen = strlen(mi.label.name); 674 if (dlen < mlen 675 && mi.label.name[mlen-dlen-1] == '.' 676 && strcmp(dp->name, mi.label.name + mlen - dlen) == 0) 677 break; 678 } 679 if (dp->code >= 0) { 680 di->devmap[dp->code] = i; 681 di->rdevmap[i] = dp->code; 682 di->devmask |= 1 << dp->code; 683 if (mi.un.v.num_channels == 2) 684 di->stereomask |= 1 << dp->code; 685 strncpy(di->names[i], mi.label.name, 686 sizeof di->names[i]); 687 } 688 break; 689 } 690 } 691 for(i = 0; i < NETBSD_MAXDEVS; i++) { 692 mi.index = i; 693 if (ioctlf(fp, AUDIO_MIXER_DEVINFO, (caddr_t)&mi, p) < 0) 694 break; 695 if (strcmp(mi.label.name, AudioNsource) != 0) 696 continue; 697 di->source = i; 698 switch(mi.type) { 699 case AUDIO_MIXER_ENUM: 700 for(j = 0; j < mi.un.e.num_mem; j++) { 701 e = opaque_to_enum(di, 702 &mi.un.e.member[j].label, 703 mi.un.e.member[j].ord); 704 if (e >= 0) 705 di->recmask |= 1 << di->rdevmap[e]; 706 } 707 di->caps = OSS_SOUND_CAP_EXCL_INPUT; 708 break; 709 case AUDIO_MIXER_SET: 710 for(j = 0; j < mi.un.s.num_mem; j++) { 711 e = opaque_to_enum(di, 712 &mi.un.s.member[j].label, 713 mi.un.s.member[j].mask); 714 if (e >= 0) 715 di->recmask |= 1 << di->rdevmap[e]; 716 } 717 break; 718 } 719 } 720 return di; 721 } 722 723 int 724 oss_ioctl_mixer(p, uap, retval) 725 struct proc *p; 726 struct oss_sys_ioctl_args /* { 727 syscallarg(int) fd; 728 syscallarg(u_long) com; 729 syscallarg(caddr_t) data; 730 } */ *uap; 731 register_t *retval; 732 { 733 struct file *fp; 734 struct filedesc *fdp; 735 u_long com; 736 struct audiodevinfo *di; 737 mixer_ctrl_t mc; 738 struct oss_mixer_info omi; 739 struct audio_device adev; 740 int idat; 741 int i; 742 int error; 743 int l, r, n, e; 744 int (*ioctlf)(struct file *, u_long, void *, struct proc *); 745 746 fdp = p->p_fd; 747 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL) 748 return (EBADF); 749 750 FILE_USE(fp); 751 752 if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 753 error = EBADF; 754 goto out; 755 } 756 757 com = SCARG(uap, com); 758 DPRINTF(("oss_ioctl_mixer: com=%08lx\n", com)); 759 760 retval[0] = 0; 761 762 di = getdevinfo(fp, p); 763 if (di == 0) { 764 error = EINVAL; 765 goto out; 766 } 767 768 ioctlf = fp->f_ops->fo_ioctl; 769 switch (com) { 770 case OSS_GET_VERSION: 771 idat = OSS_SOUND_VERSION; 772 break; 773 case OSS_SOUND_MIXER_INFO: 774 case OSS_SOUND_OLD_MIXER_INFO: 775 error = ioctlf(fp, AUDIO_GETDEV, (caddr_t)&adev, p); 776 if (error) 777 goto out; 778 omi.modify_counter = 1; 779 strncpy(omi.id, adev.name, sizeof omi.id); 780 strncpy(omi.name, adev.name, sizeof omi.name); 781 error = copyout(&omi, SCARG(uap, data), OSS_IOCTL_SIZE(com)); 782 goto out; 783 case OSS_SOUND_MIXER_READ_RECSRC: 784 if (di->source == -1) { 785 error = EINVAL; 786 goto out; 787 } 788 mc.dev = di->source; 789 if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) { 790 mc.type = AUDIO_MIXER_ENUM; 791 error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p); 792 if (error) 793 goto out; 794 e = opaque_to_enum(di, NULL, mc.un.ord); 795 if (e >= 0) 796 idat = 1 << di->rdevmap[e]; 797 } else { 798 mc.type = AUDIO_MIXER_SET; 799 error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p); 800 if (error) 801 goto out; 802 e = opaque_to_enum(di, NULL, mc.un.mask); 803 if (e >= 0) 804 idat = 1 << di->rdevmap[e]; 805 } 806 break; 807 case OSS_SOUND_MIXER_READ_DEVMASK: 808 idat = di->devmask; 809 break; 810 case OSS_SOUND_MIXER_READ_RECMASK: 811 idat = di->recmask; 812 break; 813 case OSS_SOUND_MIXER_READ_STEREODEVS: 814 idat = di->stereomask; 815 break; 816 case OSS_SOUND_MIXER_READ_CAPS: 817 idat = di->caps; 818 break; 819 case OSS_SOUND_MIXER_WRITE_RECSRC: 820 case OSS_SOUND_MIXER_WRITE_R_RECSRC: 821 if (di->source == -1) { 822 error = EINVAL; 823 goto out; 824 } 825 mc.dev = di->source; 826 error = copyin(SCARG(uap, data), &idat, sizeof idat); 827 if (error) 828 goto out; 829 if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) { 830 mc.type = AUDIO_MIXER_ENUM; 831 for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++) 832 if (idat & (1 << i)) 833 break; 834 if (i >= OSS_SOUND_MIXER_NRDEVICES || 835 di->devmap[i] == -1) { 836 error = EINVAL; 837 goto out; 838 } 839 mc.un.ord = enum_to_ord(di, di->devmap[i]); 840 } else { 841 mc.type = AUDIO_MIXER_SET; 842 mc.un.mask = 0; 843 for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++) { 844 if (idat & (1 << i)) { 845 if (di->devmap[i] == -1) { 846 error = EINVAL; 847 goto out; 848 } 849 mc.un.mask |= enum_to_mask(di, di->devmap[i]); 850 } 851 } 852 } 853 error = ioctlf(fp, AUDIO_MIXER_WRITE, (caddr_t)&mc, p); 854 goto out; 855 default: 856 if (OSS_MIXER_READ(OSS_SOUND_MIXER_FIRST) <= com && 857 com < OSS_MIXER_READ(OSS_SOUND_MIXER_NRDEVICES)) { 858 n = OSS_GET_DEV(com); 859 if (di->devmap[n] == -1) { 860 error = EINVAL; 861 goto out; 862 } 863 doread: 864 mc.dev = di->devmap[n]; 865 mc.type = AUDIO_MIXER_VALUE; 866 mc.un.value.num_channels = di->stereomask & (1<<n) ? 2 : 1; 867 error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p); 868 if (error) 869 goto out; 870 if (mc.un.value.num_channels != 2) { 871 l = r = mc.un.value.level[AUDIO_MIXER_LEVEL_MONO]; 872 } else { 873 l = mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT]; 874 r = mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT]; 875 } 876 idat = TO_OSSVOL(l) | (TO_OSSVOL(r) << 8); 877 DPRINTF(("OSS_MIXER_READ n=%d (dev=%d) l=%d, r=%d, idat=%04x\n", 878 n, di->devmap[n], l, r, idat)); 879 break; 880 } else if ((OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_FIRST) <= com && 881 com < OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_NRDEVICES)) || 882 (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com && 883 com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES))) { 884 n = OSS_GET_DEV(com); 885 if (di->devmap[n] == -1) { 886 error = EINVAL; 887 goto out; 888 } 889 error = copyin(SCARG(uap, data), &idat, sizeof idat); 890 if (error) 891 goto out; 892 l = FROM_OSSVOL( idat & 0xff); 893 r = FROM_OSSVOL((idat >> 8) & 0xff); 894 mc.dev = di->devmap[n]; 895 mc.type = AUDIO_MIXER_VALUE; 896 if (di->stereomask & (1<<n)) { 897 mc.un.value.num_channels = 2; 898 mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l; 899 mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r; 900 } else { 901 mc.un.value.num_channels = 1; 902 mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2; 903 } 904 DPRINTF(("OSS_MIXER_WRITE n=%d (dev=%d) l=%d, r=%d, idat=%04x\n", 905 n, di->devmap[n], l, r, idat)); 906 error = ioctlf(fp, AUDIO_MIXER_WRITE, (caddr_t)&mc, p); 907 if (error) 908 goto out; 909 if (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com && 910 com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES)) { 911 error = 0; 912 goto out; 913 } 914 goto doread; 915 } else { 916 #ifdef AUDIO_DEBUG 917 printf("oss_audio: unknown mixer ioctl %04lx\n", com); 918 #endif 919 error = EINVAL; 920 goto out; 921 } 922 } 923 error = copyout(&idat, SCARG(uap, data), sizeof idat); 924 out: 925 FILE_UNUSE(fp, p); 926 return error; 927 } 928 929 /* Sequencer emulation */ 930 int 931 oss_ioctl_sequencer(p, uap, retval) 932 struct proc *p; 933 struct oss_sys_ioctl_args /* { 934 syscallarg(int) fd; 935 syscallarg(u_long) com; 936 syscallarg(caddr_t) data; 937 } */ *uap; 938 register_t *retval; 939 { 940 struct file *fp; 941 struct filedesc *fdp; 942 u_long com; 943 int idat, idat1; 944 struct synth_info si; 945 struct oss_synth_info osi; 946 struct oss_seq_event_rec oser; 947 int error; 948 int (*ioctlf)(struct file *, u_long, void *, struct proc *); 949 950 fdp = p->p_fd; 951 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL) 952 return (EBADF); 953 954 FILE_USE(fp); 955 956 if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 957 error = EBADF; 958 goto out; 959 } 960 961 com = SCARG(uap, com); 962 DPRINTF(("oss_ioctl_sequencer: com=%08lx\n", com)); 963 964 retval[0] = 0; 965 966 ioctlf = fp->f_ops->fo_ioctl; 967 switch (com) { 968 case OSS_SEQ_RESET: 969 error = ioctlf(fp, SEQUENCER_RESET, (caddr_t)&idat, p); 970 goto out; 971 case OSS_SEQ_SYNC: 972 error = ioctlf(fp, SEQUENCER_SYNC, (caddr_t)&idat, p); 973 goto out; 974 case OSS_SYNTH_INFO: 975 error = copyin(SCARG(uap, data), &osi, sizeof osi); 976 if (error) 977 goto out; 978 si.device = osi.device; 979 error = ioctlf(fp, SEQUENCER_INFO, (caddr_t)&si, p); 980 if (error) 981 goto out; 982 strncpy(osi.name, si.name, sizeof osi.name); 983 osi.device = si.device; 984 switch(si.synth_type) { 985 case SYNTH_TYPE_FM: 986 osi.synth_type = OSS_SYNTH_TYPE_FM; break; 987 case SYNTH_TYPE_SAMPLE: 988 osi.synth_type = OSS_SYNTH_TYPE_SAMPLE; break; 989 case SYNTH_TYPE_MIDI: 990 osi.synth_type = OSS_SYNTH_TYPE_MIDI; break; 991 default: 992 osi.synth_type = 0; break; 993 } 994 switch(si.synth_subtype) { 995 case SYNTH_SUB_FM_TYPE_ADLIB: 996 osi.synth_subtype = OSS_FM_TYPE_ADLIB; break; 997 case SYNTH_SUB_FM_TYPE_OPL3: 998 osi.synth_subtype = OSS_FM_TYPE_OPL3; break; 999 case SYNTH_SUB_MIDI_TYPE_MPU401: 1000 osi.synth_subtype = OSS_MIDI_TYPE_MPU401; break; 1001 case SYNTH_SUB_SAMPLE_TYPE_BASIC: 1002 osi.synth_subtype = OSS_SAMPLE_TYPE_BASIC; break; 1003 default: 1004 osi.synth_subtype = 0; break; 1005 } 1006 osi.perc_mode = 0; 1007 osi.nr_voices = si.nr_voices; 1008 osi.nr_drums = 0; 1009 osi.instr_bank_size = si.instr_bank_size; 1010 osi.capabilities = 0; 1011 if (si.capabilities & SYNTH_CAP_OPL3) 1012 osi.capabilities |= OSS_SYNTH_CAP_OPL3; 1013 if (si.capabilities & SYNTH_CAP_INPUT) 1014 osi.capabilities |= OSS_SYNTH_CAP_INPUT; 1015 error = copyout(&osi, SCARG(uap, data), sizeof osi); 1016 goto out; 1017 case OSS_SEQ_CTRLRATE: 1018 error = copyin(SCARG(uap, data), &idat, sizeof idat); 1019 if (error) 1020 goto out; 1021 error = ioctlf(fp, SEQUENCER_CTRLRATE, (caddr_t)&idat, p); 1022 if (error) 1023 goto out; 1024 retval[0] = idat; 1025 break; 1026 case OSS_SEQ_GETOUTCOUNT: 1027 error = ioctlf(fp, SEQUENCER_GETOUTCOUNT, (caddr_t)&idat, p); 1028 if (error) 1029 goto out; 1030 retval[0] = idat; 1031 break; 1032 case OSS_SEQ_GETINCOUNT: 1033 error = ioctlf(fp, SEQUENCER_GETINCOUNT, (caddr_t)&idat, p); 1034 if (error) 1035 goto out; 1036 retval[0] = idat; 1037 break; 1038 case OSS_SEQ_NRSYNTHS: 1039 error = ioctlf(fp, SEQUENCER_NRSYNTHS, (caddr_t)&idat, p); 1040 if (error) 1041 goto out; 1042 retval[0] = idat; 1043 break; 1044 case OSS_SEQ_NRMIDIS: 1045 error = ioctlf(fp, SEQUENCER_NRMIDIS, (caddr_t)&idat, p); 1046 if (error) 1047 goto out; 1048 retval[0] = idat; 1049 break; 1050 case OSS_SEQ_THRESHOLD: 1051 error = copyin(SCARG(uap, data), &idat, sizeof idat); 1052 if (error) 1053 goto out; 1054 error = ioctlf(fp, SEQUENCER_THRESHOLD, (caddr_t)&idat, p); 1055 goto out; 1056 case OSS_MEMAVL: 1057 error = copyin(SCARG(uap, data), &idat, sizeof idat); 1058 if (error) 1059 goto out; 1060 error = ioctlf(fp, SEQUENCER_MEMAVL, (caddr_t)&idat, p); 1061 if (error) 1062 goto out; 1063 retval[0] = idat; 1064 break; 1065 case OSS_SEQ_PANIC: 1066 error = ioctlf(fp, SEQUENCER_PANIC, (caddr_t)&idat, p); 1067 goto out; 1068 case OSS_SEQ_OUTOFBAND: 1069 error = copyin(SCARG(uap, data), &oser, sizeof oser); 1070 if (error) 1071 goto out; 1072 error = ioctlf(fp, SEQUENCER_OUTOFBAND, (caddr_t)&oser, p); 1073 if (error) 1074 goto out; 1075 break; 1076 case OSS_SEQ_GETTIME: 1077 error = ioctlf(fp, SEQUENCER_GETTIME, (caddr_t)&idat, p); 1078 if (error) 1079 goto out; 1080 retval[0] = idat; 1081 break; 1082 case OSS_TMR_TIMEBASE: 1083 error = copyin(SCARG(uap, data), &idat, sizeof idat); 1084 if (error) 1085 goto out; 1086 error = ioctlf(fp, SEQUENCER_TMR_TIMEBASE, (caddr_t)&idat, p); 1087 if (error) 1088 goto out; 1089 retval[0] = idat; 1090 break; 1091 case OSS_TMR_START: 1092 error = ioctlf(fp, SEQUENCER_TMR_START, (caddr_t)&idat, p); 1093 goto out; 1094 case OSS_TMR_STOP: 1095 error = ioctlf(fp, SEQUENCER_TMR_STOP, (caddr_t)&idat, p); 1096 goto out; 1097 case OSS_TMR_CONTINUE: 1098 error = ioctlf(fp, SEQUENCER_TMR_CONTINUE, (caddr_t)&idat, p); 1099 goto out; 1100 case OSS_TMR_TEMPO: 1101 error = copyin(SCARG(uap, data), &idat, sizeof idat); 1102 if (error) 1103 goto out; 1104 error = ioctlf(fp, SEQUENCER_TMR_TEMPO, (caddr_t)&idat, p); 1105 if (error) 1106 goto out; 1107 retval[0] = idat; 1108 break; 1109 case OSS_TMR_SOURCE: 1110 error = copyin(SCARG(uap, data), &idat1, sizeof idat); 1111 if (error) 1112 goto out; 1113 idat = 0; 1114 if (idat1 & OSS_TMR_INTERNAL) idat |= SEQUENCER_TMR_INTERNAL; 1115 error = ioctlf(fp, SEQUENCER_TMR_SOURCE, (caddr_t)&idat, p); 1116 if (error) 1117 goto out; 1118 idat1 = idat; 1119 if (idat1 & SEQUENCER_TMR_INTERNAL) idat |= OSS_TMR_INTERNAL; 1120 retval[0] = idat; 1121 break; 1122 case OSS_TMR_METRONOME: 1123 error = copyin(SCARG(uap, data), &idat, sizeof idat); 1124 if (error) 1125 goto out; 1126 error = ioctlf(fp, SEQUENCER_TMR_METRONOME, (caddr_t)&idat, p); 1127 goto out; 1128 case OSS_TMR_SELECT: 1129 error = copyin(SCARG(uap, data), &idat, sizeof idat); 1130 if (error) 1131 goto out; 1132 retval[0] = idat; 1133 error = ioctlf(fp, SEQUENCER_TMR_SELECT, (caddr_t)&idat, p); 1134 goto out; 1135 default: 1136 error = EINVAL; 1137 goto out; 1138 } 1139 1140 error = copyout(&idat, SCARG(uap, data), sizeof idat); 1141 out: 1142 FILE_UNUSE(fp, p); 1143 return error; 1144 } 1145 1146 /* 1147 * Check that the blocksize is a power of 2 as OSS wants. 1148 * If not, set it to be. 1149 */ 1150 static void 1151 setblocksize(fp, info, p) 1152 struct file *fp; 1153 struct audio_info *info; 1154 struct proc *p; 1155 { 1156 struct audio_info set; 1157 int s; 1158 1159 if (info->blocksize & (info->blocksize-1)) { 1160 for(s = 32; s < info->blocksize; s <<= 1) 1161 ; 1162 AUDIO_INITINFO(&set); 1163 set.blocksize = s; 1164 fp->f_ops->fo_ioctl(fp, AUDIO_SETINFO, (caddr_t)&set, p); 1165 fp->f_ops->fo_ioctl(fp, AUDIO_GETINFO, (caddr_t)info, p); 1166 } 1167 } 1168