1 /*- 2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org> 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 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/sys/dev/sound/pcm/dsp.c,v 1.80.2.6 2006/04/04 17:43:48 ariff Exp $ 27 * $DragonFly: src/sys/dev/sound/pcm/dsp.c,v 1.15 2007/06/14 21:48:36 corecode Exp $ 28 */ 29 30 #include <sys/param.h> 31 #include <sys/queue.h> 32 33 #include <dev/sound/pcm/dsp.h> 34 #include <dev/sound/pcm/sound.h> 35 36 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/dsp.c,v 1.15 2007/06/14 21:48:36 corecode Exp $"); 37 38 #define OLDPCM_IOCTL 39 40 static d_open_t dsp_open; 41 static d_close_t dsp_close; 42 static d_read_t dsp_read; 43 static d_write_t dsp_write; 44 static d_ioctl_t dsp_ioctl; 45 static d_poll_t dsp_poll; 46 static d_mmap_t dsp_mmap; 47 48 struct dev_ops dsp_cdevsw = { 49 { "dsp", SND_CDEV_MAJOR, 0}, 50 /*.d_flags = D_NEEDGIANT,*/ 51 .d_open = dsp_open, 52 .d_close = dsp_close, 53 .d_read = dsp_read, 54 .d_write = dsp_write, 55 .d_ioctl = dsp_ioctl, 56 .d_poll = dsp_poll, 57 .d_mmap = dsp_mmap, 58 }; 59 60 #ifdef USING_DEVFS 61 static eventhandler_tag dsp_ehtag; 62 #endif 63 64 struct snddev_info * 65 dsp_get_info(struct cdev *dev) 66 { 67 struct snddev_info *d; 68 int unit; 69 70 unit = PCMUNIT(dev); 71 if (unit >= devclass_get_maxunit(pcm_devclass)) 72 return NULL; 73 d = devclass_get_softc(pcm_devclass, unit); 74 75 return d; 76 } 77 78 static u_int32_t 79 dsp_get_flags(struct cdev *dev) 80 { 81 device_t bdev; 82 int unit; 83 84 unit = PCMUNIT(dev); 85 if (unit >= devclass_get_maxunit(pcm_devclass)) 86 return 0xffffffff; 87 bdev = devclass_get_device(pcm_devclass, unit); 88 89 return pcm_getflags(bdev); 90 } 91 92 static void 93 dsp_set_flags(struct cdev *dev, u_int32_t flags) 94 { 95 device_t bdev; 96 int unit; 97 98 unit = PCMUNIT(dev); 99 if (unit >= devclass_get_maxunit(pcm_devclass)) 100 return; 101 bdev = devclass_get_device(pcm_devclass, unit); 102 103 pcm_setflags(bdev, flags); 104 } 105 106 /* 107 * return the channels associated with an open device instance. 108 * set the priority if the device is simplex and one direction (only) is 109 * specified. 110 * lock channels specified. 111 */ 112 static int 113 getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch, u_int32_t prio) 114 { 115 struct snddev_info *d; 116 u_int32_t flags; 117 118 flags = dsp_get_flags(dev); 119 d = dsp_get_info(dev); 120 pcm_inprog(d, 1); 121 pcm_lock(d); 122 KASSERT((flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \ 123 ("getchns: read and write both prioritised")); 124 125 if ((flags & SD_F_PRIO_SET) == 0 && (prio != (SD_F_PRIO_RD | SD_F_PRIO_WR))) { 126 flags |= prio & (SD_F_PRIO_RD | SD_F_PRIO_WR); 127 dsp_set_flags(dev, flags); 128 } 129 130 *rdch = dev->si_drv1; 131 *wrch = dev->si_drv2; 132 if ((flags & SD_F_SIMPLEX) && (flags & SD_F_PRIO_SET)) { 133 if (prio) { 134 if (*rdch && flags & SD_F_PRIO_WR) { 135 dev->si_drv1 = NULL; 136 *rdch = pcm_getfakechan(d); 137 } else if (*wrch && flags & SD_F_PRIO_RD) { 138 dev->si_drv2 = NULL; 139 *wrch = pcm_getfakechan(d); 140 } 141 } 142 143 pcm_getfakechan(d)->flags |= CHN_F_BUSY; 144 } 145 pcm_unlock(d); 146 147 if (*rdch && *rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD)) 148 CHN_LOCK(*rdch); 149 if (*wrch && *wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR)) 150 CHN_LOCK(*wrch); 151 152 return 0; 153 } 154 155 /* unlock specified channels */ 156 static void 157 relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch, u_int32_t prio) 158 { 159 struct snddev_info *d; 160 161 d = dsp_get_info(dev); 162 if (wrch && wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR)) 163 CHN_UNLOCK(wrch); 164 if (rdch && rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD)) 165 CHN_UNLOCK(rdch); 166 pcm_inprog(d, -1); 167 } 168 169 static int 170 dsp_open(struct dev_open_args *ap) 171 { 172 struct cdev *i_dev = ap->a_head.a_dev; 173 struct thread *td = curthread; 174 int flags = ap->a_oflags; 175 struct pcm_channel *rdch, *wrch; 176 struct snddev_info *d; 177 u_int32_t fmt; 178 int devtype; 179 int error; 180 int chnum; 181 182 if (i_dev == NULL || td == NULL) 183 return ENODEV; 184 185 if ((flags & (FREAD | FWRITE)) == 0) 186 return EINVAL; 187 188 d = dsp_get_info(i_dev); 189 devtype = PCMDEV(i_dev); 190 chnum = -1; 191 192 /* decide default format */ 193 switch (devtype) { 194 case SND_DEV_DSP16: 195 fmt = AFMT_S16_LE; 196 break; 197 198 case SND_DEV_DSP: 199 fmt = AFMT_U8; 200 break; 201 202 case SND_DEV_AUDIO: 203 fmt = AFMT_MU_LAW; 204 break; 205 206 case SND_DEV_NORESET: 207 fmt = 0; 208 break; 209 210 case SND_DEV_DSPREC: 211 fmt = AFMT_U8; 212 if (flags & FWRITE) 213 return EINVAL; 214 chnum = PCMCHAN(i_dev); 215 break; 216 217 default: 218 panic("impossible devtype %d", devtype); 219 } 220 221 /* lock snddev so nobody else can monkey with it */ 222 pcm_lock(d); 223 224 rdch = i_dev->si_drv1; 225 wrch = i_dev->si_drv2; 226 227 if (rdch || wrch || ((dsp_get_flags(i_dev) & SD_F_SIMPLEX) && 228 (flags & (FREAD | FWRITE)) == (FREAD | FWRITE))) { 229 /* simplex or not, better safe than sorry. */ 230 pcm_unlock(d); 231 return EBUSY; 232 } 233 234 /* 235 * if we get here, the open request is valid- either: 236 * * we were previously not open 237 * * we were open for play xor record and the opener wants 238 * the non-open direction 239 */ 240 if (flags & FREAD) { 241 /* open for read */ 242 pcm_unlock(d); 243 error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid, chnum); 244 if (error != 0 && error != EBUSY && chnum != -1 && (flags & FWRITE)) 245 error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid, -1); 246 247 if (error == 0 && (chn_reset(rdch, fmt) || 248 (fmt && chn_setspeed(rdch, DSP_DEFAULT_SPEED)))) 249 error = ENODEV; 250 251 if (error != 0) { 252 if (rdch) 253 pcm_chnrelease(rdch); 254 return error; 255 } 256 257 if (flags & O_NONBLOCK) 258 rdch->flags |= CHN_F_NBIO; 259 pcm_chnref(rdch, 1); 260 CHN_UNLOCK(rdch); 261 pcm_lock(d); 262 } 263 264 if (flags & FWRITE) { 265 /* open for write */ 266 pcm_unlock(d); 267 error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid, chnum); 268 if (error != 0 && error != EBUSY && chnum != -1 && (flags & FREAD)) 269 error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid, -1); 270 271 if (error == 0 && (chn_reset(wrch, fmt) || 272 (fmt && chn_setspeed(wrch, DSP_DEFAULT_SPEED)))) 273 error = ENODEV; 274 275 if (error != 0) { 276 if (wrch) 277 pcm_chnrelease(wrch); 278 if (rdch) { 279 /* 280 * Lock, deref and release previously created record channel 281 */ 282 CHN_LOCK(rdch); 283 pcm_chnref(rdch, -1); 284 pcm_chnrelease(rdch); 285 } 286 287 return error; 288 } 289 290 if (flags & O_NONBLOCK) 291 wrch->flags |= CHN_F_NBIO; 292 pcm_chnref(wrch, 1); 293 CHN_UNLOCK(wrch); 294 pcm_lock(d); 295 } 296 297 i_dev->si_drv1 = rdch; 298 i_dev->si_drv2 = wrch; 299 300 pcm_unlock(d); 301 return 0; 302 } 303 304 static int 305 dsp_close(struct dev_close_args *ap) 306 { 307 struct cdev *i_dev = ap->a_head.a_dev; 308 struct pcm_channel *rdch, *wrch; 309 struct snddev_info *d; 310 int refs; 311 312 d = dsp_get_info(i_dev); 313 pcm_lock(d); 314 rdch = i_dev->si_drv1; 315 wrch = i_dev->si_drv2; 316 pcm_unlock(d); 317 318 if (rdch || wrch) { 319 refs = 0; 320 if (rdch) { 321 CHN_LOCK(rdch); 322 refs += pcm_chnref(rdch, -1); 323 chn_abort(rdch); /* won't sleep */ 324 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD); 325 chn_reset(rdch, 0); 326 pcm_chnrelease(rdch); 327 } 328 if (wrch) { 329 CHN_LOCK(wrch); 330 refs += pcm_chnref(wrch, -1); 331 /* 332 * XXX: Maybe the right behaviour is to abort on non_block. 333 * It seems that mplayer flushes the audio queue by quickly 334 * closing and re-opening. In FBSD, there's a long pause 335 * while the audio queue flushes that I presume isn't there in 336 * linux. 337 */ 338 chn_flush(wrch); /* may sleep */ 339 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD); 340 chn_reset(wrch, 0); 341 pcm_chnrelease(wrch); 342 } 343 344 pcm_lock(d); 345 if (rdch) 346 i_dev->si_drv1 = NULL; 347 if (wrch) 348 i_dev->si_drv2 = NULL; 349 /* 350 * If there are no more references, release the channels. 351 */ 352 if (refs == 0 && i_dev->si_drv1 == NULL && 353 i_dev->si_drv2 == NULL) { 354 if (pcm_getfakechan(d)) 355 pcm_getfakechan(d)->flags = 0; 356 /* What is this?!? */ 357 dsp_set_flags(i_dev, dsp_get_flags(i_dev) & ~SD_F_TRANSIENT); 358 } 359 pcm_unlock(d); 360 } 361 return 0; 362 } 363 364 static int 365 dsp_read(struct dev_read_args *ap) 366 { 367 struct cdev *i_dev = ap->a_head.a_dev; 368 struct uio *buf = ap->a_uio; 369 int flag = ap->a_ioflag; 370 struct pcm_channel *rdch, *wrch; 371 int ret; 372 373 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD); 374 375 KASSERT(rdch, ("dsp_read: nonexistant channel")); 376 KASSERT(rdch->flags & CHN_F_BUSY, ("dsp_read: nonbusy channel")); 377 378 if (rdch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) { 379 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD); 380 return EINVAL; 381 } 382 if (!(rdch->flags & CHN_F_RUNNING)) 383 rdch->flags |= CHN_F_RUNNING; 384 ret = chn_read(rdch, buf, flag); 385 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD); 386 387 return ret; 388 } 389 390 static int 391 dsp_write(struct dev_write_args *ap) 392 { 393 struct cdev *i_dev = ap->a_head.a_dev; 394 struct uio *buf = ap->a_uio; 395 int flag = ap->a_ioflag; 396 struct pcm_channel *rdch, *wrch; 397 int ret; 398 399 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_WR); 400 401 KASSERT(wrch, ("dsp_write: nonexistant channel")); 402 KASSERT(wrch->flags & CHN_F_BUSY, ("dsp_write: nonbusy channel")); 403 404 if (wrch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) { 405 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR); 406 return EINVAL; 407 } 408 if (!(wrch->flags & CHN_F_RUNNING)) 409 wrch->flags |= CHN_F_RUNNING; 410 ret = chn_write(wrch, buf, flag); 411 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR); 412 413 return ret; 414 } 415 416 static int 417 dsp_ioctl(struct dev_ioctl_args *ap) 418 { 419 struct cdev *i_dev = ap->a_head.a_dev; 420 u_long cmd = ap->a_cmd; 421 caddr_t arg = ap->a_data; 422 struct pcm_channel *chn, *rdch, *wrch; 423 struct snddev_info *d; 424 int kill; 425 int ret = 0, *arg_i = (int *)arg, tmp; 426 427 d = dsp_get_info(i_dev); 428 getchns(i_dev, &rdch, &wrch, 0); 429 430 kill = 0; 431 if (wrch && (wrch->flags & CHN_F_DEAD)) 432 kill |= 1; 433 if (rdch && (rdch->flags & CHN_F_DEAD)) 434 kill |= 2; 435 if (kill == 3) { 436 relchns(i_dev, rdch, wrch, 0); 437 return EINVAL; 438 } 439 if (kill & 1) 440 wrch = NULL; 441 if (kill & 2) 442 rdch = NULL; 443 444 /* 445 * 4Front OSS specifies that dsp devices allow mixer controls to 446 * control PCM == their volume. 447 */ 448 if (IOCGROUP(cmd) == 'M') { 449 /* 450 * For now only set the channel volume for vchans, pass 451 * all others to the mixer. 452 */ 453 if (wrch != NULL && wrch->flags & CHN_F_VIRTUAL && 454 (cmd & 0xff) == SOUND_MIXER_PCM) { 455 if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) { 456 int vol_raw = *(int *)arg; 457 int vol_left, vol_right; 458 459 vol_left = min(vol_raw & 0x00ff, 100); 460 vol_right = min((vol_raw & 0xff00) >> 8, 100); 461 ret = chn_setvolume(wrch, vol_left, vol_right); 462 } else { 463 *(int *)arg = wrch->volume; 464 } 465 } else { 466 ap->a_head.a_dev = d->mixer_dev; 467 ret = mixer_ioctl(ap); 468 } 469 470 relchns(i_dev, rdch, wrch, 0); 471 crit_exit(); 472 return ret; 473 } 474 475 switch(cmd) { 476 #ifdef OLDPCM_IOCTL 477 /* 478 * we start with the new ioctl interface. 479 */ 480 case AIONWRITE: /* how many bytes can write ? */ 481 if (wrch) { 482 CHN_LOCK(wrch); 483 /* 484 if (wrch && wrch->bufhard.dl) 485 while (chn_wrfeed(wrch) == 0); 486 */ 487 *arg_i = sndbuf_getfree(wrch->bufsoft); 488 CHN_UNLOCK(wrch); 489 } else { 490 *arg_i = 0; 491 ret = EINVAL; 492 } 493 break; 494 495 case AIOSSIZE: /* set the current blocksize */ 496 { 497 struct snd_size *p = (struct snd_size *)arg; 498 499 p->play_size = 0; 500 p->rec_size = 0; 501 if (wrch) { 502 CHN_LOCK(wrch); 503 chn_setblocksize(wrch, 2, p->play_size); 504 p->play_size = sndbuf_getblksz(wrch->bufsoft); 505 CHN_UNLOCK(wrch); 506 } 507 if (rdch) { 508 CHN_LOCK(rdch); 509 chn_setblocksize(rdch, 2, p->rec_size); 510 p->rec_size = sndbuf_getblksz(rdch->bufsoft); 511 CHN_UNLOCK(rdch); 512 } 513 } 514 break; 515 case AIOGSIZE: /* get the current blocksize */ 516 { 517 struct snd_size *p = (struct snd_size *)arg; 518 519 if (wrch) { 520 CHN_LOCK(wrch); 521 p->play_size = sndbuf_getblksz(wrch->bufsoft); 522 CHN_UNLOCK(wrch); 523 } 524 if (rdch) { 525 CHN_LOCK(rdch); 526 p->rec_size = sndbuf_getblksz(rdch->bufsoft); 527 CHN_UNLOCK(rdch); 528 } 529 } 530 break; 531 532 case AIOSFMT: 533 case AIOGFMT: 534 { 535 snd_chan_param *p = (snd_chan_param *)arg; 536 537 if (cmd == AIOSFMT && 538 ((p->play_format != 0 && p->play_rate == 0) || 539 (p->rec_format != 0 && p->rec_rate == 0))) { 540 ret = EINVAL; 541 break; 542 } 543 if (wrch) { 544 CHN_LOCK(wrch); 545 if (cmd == AIOSFMT && p->play_format != 0) { 546 chn_setformat(wrch, p->play_format); 547 chn_setspeed(wrch, p->play_rate); 548 } 549 p->play_rate = wrch->speed; 550 p->play_format = wrch->format; 551 CHN_UNLOCK(wrch); 552 } else { 553 p->play_rate = 0; 554 p->play_format = 0; 555 } 556 if (rdch) { 557 CHN_LOCK(rdch); 558 if (cmd == AIOSFMT && p->rec_format != 0) { 559 chn_setformat(rdch, p->rec_format); 560 chn_setspeed(rdch, p->rec_rate); 561 } 562 p->rec_rate = rdch->speed; 563 p->rec_format = rdch->format; 564 CHN_UNLOCK(rdch); 565 } else { 566 p->rec_rate = 0; 567 p->rec_format = 0; 568 } 569 } 570 break; 571 572 case AIOGCAP: /* get capabilities */ 573 { 574 snd_capabilities *p = (snd_capabilities *)arg; 575 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL; 576 struct cdev *pdev; 577 578 if (rdch) { 579 CHN_LOCK(rdch); 580 rcaps = chn_getcaps(rdch); 581 } 582 if (wrch) { 583 CHN_LOCK(wrch); 584 pcaps = chn_getcaps(wrch); 585 } 586 p->rate_min = max(rcaps? rcaps->minspeed : 0, 587 pcaps? pcaps->minspeed : 0); 588 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000, 589 pcaps? pcaps->maxspeed : 1000000); 590 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000, 591 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000); 592 /* XXX bad on sb16 */ 593 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) & 594 (wrch? chn_getformats(wrch) : 0xffffffff); 595 if (rdch && wrch) 596 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX; 597 pdev = d->mixer_dev; 598 p->mixers = 1; /* default: one mixer */ 599 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0; 600 p->left = p->right = 100; 601 if (rdch) 602 CHN_UNLOCK(rdch); 603 if (wrch) 604 CHN_UNLOCK(wrch); 605 } 606 break; 607 608 case AIOSTOP: 609 if (*arg_i == AIOSYNC_PLAY && wrch) { 610 CHN_LOCK(wrch); 611 *arg_i = chn_abort(wrch); 612 CHN_UNLOCK(wrch); 613 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) { 614 CHN_LOCK(rdch); 615 *arg_i = chn_abort(rdch); 616 CHN_UNLOCK(rdch); 617 } else { 618 kprintf("AIOSTOP: bad channel 0x%x\n", *arg_i); 619 *arg_i = 0; 620 } 621 break; 622 623 case AIOSYNC: 624 kprintf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n", 625 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos); 626 break; 627 #endif 628 /* 629 * here follow the standard ioctls (filio.h etc.) 630 */ 631 case FIONREAD: /* get # bytes to read */ 632 if (rdch) { 633 CHN_LOCK(rdch); 634 /* if (rdch && rdch->bufhard.dl) 635 while (chn_rdfeed(rdch) == 0); 636 */ 637 *arg_i = sndbuf_getready(rdch->bufsoft); 638 CHN_UNLOCK(rdch); 639 } else { 640 *arg_i = 0; 641 ret = EINVAL; 642 } 643 break; 644 645 case FIOASYNC: /*set/clear async i/o */ 646 DEB( kprintf("FIOASYNC\n") ; ) 647 break; 648 649 case SNDCTL_DSP_NONBLOCK: 650 case FIONBIO: /* set/clear non-blocking i/o */ 651 if (rdch) { 652 CHN_LOCK(rdch); 653 if (*arg_i) 654 rdch->flags |= CHN_F_NBIO; 655 else 656 rdch->flags &= ~CHN_F_NBIO; 657 CHN_UNLOCK(rdch); 658 } 659 if (wrch) { 660 CHN_LOCK(wrch); 661 if (*arg_i) 662 wrch->flags |= CHN_F_NBIO; 663 else 664 wrch->flags &= ~CHN_F_NBIO; 665 CHN_UNLOCK(wrch); 666 } 667 break; 668 669 /* 670 * Finally, here is the linux-compatible ioctl interface 671 */ 672 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int) 673 case THE_REAL_SNDCTL_DSP_GETBLKSIZE: 674 case SNDCTL_DSP_GETBLKSIZE: 675 chn = wrch ? wrch : rdch; 676 if (chn) { 677 CHN_LOCK(chn); 678 *arg_i = sndbuf_getblksz(chn->bufsoft); 679 CHN_UNLOCK(chn); 680 } else { 681 *arg_i = 0; 682 ret = EINVAL; 683 } 684 break ; 685 686 case SNDCTL_DSP_SETBLKSIZE: 687 RANGE(*arg_i, 16, 65536); 688 if (wrch) { 689 CHN_LOCK(wrch); 690 chn_setblocksize(wrch, 2, *arg_i); 691 CHN_UNLOCK(wrch); 692 } 693 if (rdch) { 694 CHN_LOCK(rdch); 695 chn_setblocksize(rdch, 2, *arg_i); 696 CHN_UNLOCK(rdch); 697 } 698 break; 699 700 case SNDCTL_DSP_RESET: 701 DEB(kprintf("dsp reset\n")); 702 if (wrch) { 703 CHN_LOCK(wrch); 704 chn_abort(wrch); 705 chn_resetbuf(wrch); 706 CHN_UNLOCK(wrch); 707 } 708 if (rdch) { 709 CHN_LOCK(rdch); 710 chn_abort(rdch); 711 chn_resetbuf(rdch); 712 CHN_UNLOCK(rdch); 713 } 714 break; 715 716 case SNDCTL_DSP_SYNC: 717 DEB(kprintf("dsp sync\n")); 718 /* chn_sync may sleep */ 719 if (wrch) { 720 CHN_LOCK(wrch); 721 chn_sync(wrch, sndbuf_getsize(wrch->bufsoft) - 4); 722 CHN_UNLOCK(wrch); 723 } 724 break; 725 726 case SNDCTL_DSP_SPEED: 727 /* chn_setspeed may sleep */ 728 tmp = 0; 729 if (wrch) { 730 CHN_LOCK(wrch); 731 ret = chn_setspeed(wrch, *arg_i); 732 tmp = wrch->speed; 733 CHN_UNLOCK(wrch); 734 } 735 if (rdch && ret == 0) { 736 CHN_LOCK(rdch); 737 ret = chn_setspeed(rdch, *arg_i); 738 if (tmp == 0) 739 tmp = rdch->speed; 740 CHN_UNLOCK(rdch); 741 } 742 *arg_i = tmp; 743 break; 744 745 case SOUND_PCM_READ_RATE: 746 chn = wrch ? wrch : rdch; 747 if (chn) { 748 CHN_LOCK(chn); 749 *arg_i = chn->speed; 750 CHN_UNLOCK(chn); 751 } else { 752 *arg_i = 0; 753 ret = EINVAL; 754 } 755 break; 756 757 case SNDCTL_DSP_STEREO: 758 tmp = -1; 759 *arg_i = (*arg_i)? AFMT_STEREO : 0; 760 if (wrch) { 761 CHN_LOCK(wrch); 762 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i); 763 tmp = (wrch->format & AFMT_STEREO)? 1 : 0; 764 CHN_UNLOCK(wrch); 765 } 766 if (rdch && ret == 0) { 767 CHN_LOCK(rdch); 768 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i); 769 if (tmp == -1) 770 tmp = (rdch->format & AFMT_STEREO)? 1 : 0; 771 CHN_UNLOCK(rdch); 772 } 773 *arg_i = tmp; 774 break; 775 776 case SOUND_PCM_WRITE_CHANNELS: 777 /* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */ 778 if (*arg_i != 0) { 779 tmp = 0; 780 *arg_i = (*arg_i != 1)? AFMT_STEREO : 0; 781 if (wrch) { 782 CHN_LOCK(wrch); 783 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i); 784 tmp = (wrch->format & AFMT_STEREO)? 2 : 1; 785 CHN_UNLOCK(wrch); 786 } 787 if (rdch && ret == 0) { 788 CHN_LOCK(rdch); 789 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i); 790 if (tmp == 0) 791 tmp = (rdch->format & AFMT_STEREO)? 2 : 1; 792 CHN_UNLOCK(rdch); 793 } 794 *arg_i = tmp; 795 } else { 796 chn = wrch ? wrch : rdch; 797 CHN_LOCK(chn); 798 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1; 799 CHN_UNLOCK(chn); 800 } 801 break; 802 803 case SOUND_PCM_READ_CHANNELS: 804 chn = wrch ? wrch : rdch; 805 if (chn) { 806 CHN_LOCK(chn); 807 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1; 808 CHN_UNLOCK(chn); 809 } else { 810 *arg_i = 0; 811 ret = EINVAL; 812 } 813 break; 814 815 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */ 816 chn = wrch ? wrch : rdch; 817 if (chn) { 818 CHN_LOCK(chn); 819 *arg_i = chn_getformats(chn); 820 CHN_UNLOCK(chn); 821 } else { 822 *arg_i = 0; 823 ret = EINVAL; 824 } 825 break ; 826 827 case SNDCTL_DSP_SETFMT: /* sets _one_ format */ 828 if ((*arg_i != AFMT_QUERY)) { 829 tmp = 0; 830 if (wrch) { 831 CHN_LOCK(wrch); 832 ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO)); 833 tmp = wrch->format & ~AFMT_STEREO; 834 CHN_UNLOCK(wrch); 835 } 836 if (rdch && ret == 0) { 837 CHN_LOCK(rdch); 838 ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO)); 839 if (tmp == 0) 840 tmp = rdch->format & ~AFMT_STEREO; 841 CHN_UNLOCK(rdch); 842 } 843 *arg_i = tmp; 844 } else { 845 chn = wrch ? wrch : rdch; 846 CHN_LOCK(chn); 847 *arg_i = chn->format & ~AFMT_STEREO; 848 CHN_UNLOCK(chn); 849 } 850 break; 851 852 case SNDCTL_DSP_SETFRAGMENT: 853 DEB(kprintf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg)); 854 { 855 u_int32_t fragln = (*arg_i) & 0x0000ffff; 856 u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16; 857 u_int32_t fragsz; 858 u_int32_t r_maxfrags, r_fragsz; 859 860 RANGE(fragln, 4, 16); 861 fragsz = 1 << fragln; 862 863 if (maxfrags == 0) 864 maxfrags = CHN_2NDBUFMAXSIZE / fragsz; 865 if (maxfrags < 2) 866 maxfrags = 2; 867 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE) 868 maxfrags = CHN_2NDBUFMAXSIZE / fragsz; 869 870 DEB(kprintf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz)); 871 if (rdch) { 872 CHN_LOCK(rdch); 873 ret = chn_setblocksize(rdch, maxfrags, fragsz); 874 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft); 875 r_fragsz = sndbuf_getblksz(rdch->bufsoft); 876 CHN_UNLOCK(rdch); 877 } else { 878 r_maxfrags = maxfrags; 879 r_fragsz = fragsz; 880 } 881 if (wrch && ret == 0) { 882 CHN_LOCK(wrch); 883 ret = chn_setblocksize(wrch, maxfrags, fragsz); 884 maxfrags = sndbuf_getblkcnt(wrch->bufsoft); 885 fragsz = sndbuf_getblksz(wrch->bufsoft); 886 CHN_UNLOCK(wrch); 887 } else { /* use whatever came from the read channel */ 888 maxfrags = r_maxfrags; 889 fragsz = r_fragsz; 890 } 891 892 fragln = 0; 893 while (fragsz > 1) { 894 fragln++; 895 fragsz >>= 1; 896 } 897 *arg_i = (maxfrags << 16) | fragln; 898 } 899 break; 900 901 case SNDCTL_DSP_GETISPACE: 902 /* return the size of data available in the input queue */ 903 { 904 audio_buf_info *a = (audio_buf_info *)arg; 905 if (rdch) { 906 struct snd_dbuf *bs = rdch->bufsoft; 907 908 CHN_LOCK(rdch); 909 a->bytes = sndbuf_getready(bs); 910 a->fragments = a->bytes / sndbuf_getblksz(bs); 911 a->fragstotal = sndbuf_getblkcnt(bs); 912 a->fragsize = sndbuf_getblksz(bs); 913 CHN_UNLOCK(rdch); 914 } 915 } 916 break; 917 918 case SNDCTL_DSP_GETOSPACE: 919 /* return space available in the output queue */ 920 { 921 audio_buf_info *a = (audio_buf_info *)arg; 922 if (wrch) { 923 struct snd_dbuf *bs = wrch->bufsoft; 924 925 CHN_LOCK(wrch); 926 /* XXX abusive DMA update: chn_wrupdate(wrch); */ 927 a->bytes = sndbuf_getfree(bs); 928 a->fragments = a->bytes / sndbuf_getblksz(bs); 929 a->fragstotal = sndbuf_getblkcnt(bs); 930 a->fragsize = sndbuf_getblksz(bs); 931 CHN_UNLOCK(wrch); 932 } 933 } 934 break; 935 936 case SNDCTL_DSP_GETIPTR: 937 { 938 count_info *a = (count_info *)arg; 939 if (rdch) { 940 struct snd_dbuf *bs = rdch->bufsoft; 941 942 CHN_LOCK(rdch); 943 /* XXX abusive DMA update: chn_rdupdate(rdch); */ 944 a->bytes = sndbuf_gettotal(bs); 945 a->blocks = sndbuf_getblocks(bs) - rdch->blocks; 946 a->ptr = sndbuf_getreadyptr(bs); 947 rdch->blocks = sndbuf_getblocks(bs); 948 CHN_UNLOCK(rdch); 949 } else 950 ret = EINVAL; 951 } 952 break; 953 954 case SNDCTL_DSP_GETOPTR: 955 { 956 count_info *a = (count_info *)arg; 957 if (wrch) { 958 struct snd_dbuf *bs = wrch->bufsoft; 959 960 CHN_LOCK(wrch); 961 /* XXX abusive DMA update: chn_wrupdate(wrch); */ 962 a->bytes = sndbuf_gettotal(bs); 963 a->blocks = sndbuf_getblocks(bs) - wrch->blocks; 964 a->ptr = sndbuf_getreadyptr(bs); 965 wrch->blocks = sndbuf_getblocks(bs); 966 CHN_UNLOCK(wrch); 967 } else 968 ret = EINVAL; 969 } 970 break; 971 972 case SNDCTL_DSP_GETCAPS: 973 *arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER; 974 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX)) 975 *arg_i |= DSP_CAP_DUPLEX; 976 break; 977 978 case SOUND_PCM_READ_BITS: 979 chn = wrch ? wrch : rdch; 980 if (chn) { 981 CHN_LOCK(chn); 982 if (chn->format & AFMT_8BIT) 983 *arg_i = 8; 984 else if (chn->format & AFMT_16BIT) 985 *arg_i = 16; 986 else if (chn->format & AFMT_24BIT) 987 *arg_i = 24; 988 else if (chn->format & AFMT_32BIT) 989 *arg_i = 32; 990 else 991 ret = EINVAL; 992 CHN_UNLOCK(chn); 993 } else { 994 *arg_i = 0; 995 ret = EINVAL; 996 } 997 break; 998 999 case SNDCTL_DSP_SETTRIGGER: 1000 if (rdch) { 1001 CHN_LOCK(rdch); 1002 rdch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER); 1003 if (*arg_i & PCM_ENABLE_INPUT) 1004 chn_start(rdch, 1); 1005 else 1006 rdch->flags |= CHN_F_NOTRIGGER; 1007 CHN_UNLOCK(rdch); 1008 } 1009 if (wrch) { 1010 CHN_LOCK(wrch); 1011 wrch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER); 1012 if (*arg_i & PCM_ENABLE_OUTPUT) 1013 chn_start(wrch, 1); 1014 else 1015 wrch->flags |= CHN_F_NOTRIGGER; 1016 CHN_UNLOCK(wrch); 1017 } 1018 break; 1019 1020 case SNDCTL_DSP_GETTRIGGER: 1021 *arg_i = 0; 1022 if (wrch) { 1023 CHN_LOCK(wrch); 1024 if (wrch->flags & CHN_F_TRIGGERED) 1025 *arg_i |= PCM_ENABLE_OUTPUT; 1026 CHN_UNLOCK(wrch); 1027 } 1028 if (rdch) { 1029 CHN_LOCK(rdch); 1030 if (rdch->flags & CHN_F_TRIGGERED) 1031 *arg_i |= PCM_ENABLE_INPUT; 1032 CHN_UNLOCK(rdch); 1033 } 1034 break; 1035 1036 case SNDCTL_DSP_GETODELAY: 1037 if (wrch) { 1038 struct snd_dbuf *b = wrch->bufhard; 1039 struct snd_dbuf *bs = wrch->bufsoft; 1040 1041 CHN_LOCK(wrch); 1042 /* XXX abusive DMA update: chn_wrupdate(wrch); */ 1043 *arg_i = sndbuf_getready(b) + sndbuf_getready(bs); 1044 CHN_UNLOCK(wrch); 1045 } else 1046 ret = EINVAL; 1047 break; 1048 1049 case SNDCTL_DSP_POST: 1050 if (wrch) { 1051 CHN_LOCK(wrch); 1052 wrch->flags &= ~CHN_F_NOTRIGGER; 1053 chn_start(wrch, 1); 1054 CHN_UNLOCK(wrch); 1055 } 1056 break; 1057 1058 case SNDCTL_DSP_SETDUPLEX: 1059 /* 1060 * switch to full-duplex mode if card is in half-duplex 1061 * mode and is able to work in full-duplex mode 1062 */ 1063 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX)) 1064 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX); 1065 break; 1066 1067 case SNDCTL_DSP_MAPINBUF: 1068 case SNDCTL_DSP_MAPOUTBUF: 1069 case SNDCTL_DSP_SETSYNCRO: 1070 /* undocumented */ 1071 1072 case SNDCTL_DSP_SUBDIVIDE: 1073 case SOUND_PCM_WRITE_FILTER: 1074 case SOUND_PCM_READ_FILTER: 1075 /* dunno what these do, don't sound important */ 1076 1077 default: 1078 DEB(kprintf("default ioctl fn 0x%08lx fail\n", cmd)); 1079 ret = EINVAL; 1080 break; 1081 } 1082 relchns(i_dev, rdch, wrch, 0); 1083 return ret; 1084 } 1085 1086 static int 1087 dsp_poll(struct dev_poll_args *ap) 1088 { 1089 struct cdev *i_dev = ap->a_head.a_dev; 1090 int events = ap->a_events; 1091 struct thread *td = curthread; 1092 struct pcm_channel *wrch = NULL, *rdch = NULL; 1093 int ret, e; 1094 1095 ret = 0; 1096 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR); 1097 1098 if (wrch) { 1099 e = (events & (POLLOUT | POLLWRNORM)); 1100 if (e) 1101 ret |= chn_poll(wrch, e, td); 1102 } 1103 if (rdch) { 1104 e = (events & (POLLIN | POLLRDNORM)); 1105 if (e) 1106 ret |= chn_poll(rdch, e, td); 1107 } 1108 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR); 1109 1110 ap->a_events = ret; 1111 return (0); 1112 } 1113 1114 static int 1115 dsp_mmap(struct dev_mmap_args *ap) 1116 { 1117 struct cdev *i_dev = ap->a_head.a_dev; 1118 vm_offset_t offset = ap->a_offset; 1119 int nprot = ap->a_nprot; 1120 struct pcm_channel *wrch = NULL, *rdch = NULL, *c; 1121 1122 if (nprot & PROT_EXEC) 1123 return -1; 1124 1125 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR); 1126 #if 0 1127 /* 1128 * XXX the linux api uses the nprot to select read/write buffer 1129 * our vm system doesn't allow this, so force write buffer 1130 */ 1131 1132 if (wrch && (nprot & PROT_WRITE)) { 1133 c = wrch; 1134 } else if (rdch && (nprot & PROT_READ)) { 1135 c = rdch; 1136 } else { 1137 return -1; 1138 } 1139 #else 1140 c = wrch; 1141 #endif 1142 1143 if (c == NULL) { 1144 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR); 1145 return -1; 1146 } 1147 1148 if (offset >= sndbuf_getsize(c->bufsoft)) { 1149 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR); 1150 return -1; 1151 } 1152 1153 if (!(c->flags & CHN_F_MAPPED)) 1154 c->flags |= CHN_F_MAPPED; 1155 1156 ap->a_result = vtophys(sndbuf_getbufofs(c->bufsoft, offset)); 1157 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR); 1158 1159 return (0); 1160 } 1161 1162 #ifdef USING_DEVFS 1163 1164 /* 1165 * Clone logic is this: 1166 * x E X = {dsp, dspW, audio} 1167 * x -> x${sysctl("hw.snd.unit")} 1168 * xN-> 1169 * for i N = 1 to channels of device N 1170 * if xN.i isn't busy, return its dev_t 1171 */ 1172 static void 1173 dsp_clone(void *arg, struct ucred *cred, char *name, int namelen, 1174 struct cdev **dev) 1175 { 1176 struct cdev *pdev; 1177 struct snddev_info *pcm_dev; 1178 struct snddev_channel *pcm_chan; 1179 int i, unit, devtype; 1180 static int devtypes[3] = {SND_DEV_DSP, SND_DEV_DSP16, SND_DEV_AUDIO}; 1181 static char *devnames[3] = {"dsp", "dspW", "audio"}; 1182 1183 if (*dev != NULL) 1184 return; 1185 if (pcm_devclass == NULL) 1186 return; 1187 1188 devtype = 0; 1189 unit = -1; 1190 for (i = 0; (i < 3) && (unit == -1); i++) { 1191 devtype = devtypes[i]; 1192 if (strcmp(name, devnames[i]) == 0) { 1193 unit = snd_unit; 1194 } else { 1195 if (dev_stdclone(name, NULL, devnames[i], &unit) != 1) 1196 unit = -1; 1197 } 1198 } 1199 if (unit == -1 || unit >= devclass_get_maxunit(pcm_devclass)) 1200 return; 1201 1202 pcm_dev = devclass_get_softc(pcm_devclass, unit); 1203 1204 if (pcm_dev == NULL) 1205 return; 1206 1207 SLIST_FOREACH(pcm_chan, &pcm_dev->channels, link) { 1208 1209 switch(devtype) { 1210 case SND_DEV_DSP: 1211 pdev = pcm_chan->dsp_devt; 1212 break; 1213 case SND_DEV_DSP16: 1214 pdev = pcm_chan->dspW_devt; 1215 break; 1216 case SND_DEV_AUDIO: 1217 pdev = pcm_chan->audio_devt; 1218 break; 1219 default: 1220 panic("Unknown devtype %d", devtype); 1221 } 1222 1223 if ((pdev != NULL) && (pdev->si_drv1 == NULL) && (pdev->si_drv2 == NULL)) { 1224 *dev = pdev; 1225 dev_ref(*dev); 1226 return; 1227 } 1228 } 1229 } 1230 1231 static void 1232 dsp_sysinit(void *p) 1233 { 1234 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000); 1235 } 1236 1237 static void 1238 dsp_sysuninit(void *p) 1239 { 1240 if (dsp_ehtag != NULL) 1241 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag); 1242 } 1243 1244 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL); 1245 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL); 1246 #endif 1247 1248 1249