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