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