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