1 /* $OpenBSD: audio.c,v 1.32 2001/11/06 19:53:18 miod Exp $ */ 2 /* $NetBSD: audio.c,v 1.105 1998/09/27 16:43:56 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1991-1993 Regents of the University of California. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the Computer Systems 19 * Engineering Group at Lawrence Berkeley Laboratory. 20 * 4. Neither the name of the University nor of the Laboratory may be used 21 * to endorse or promote products derived from this software without 22 * specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 /* 38 * This is a (partially) SunOS-compatible /dev/audio driver for NetBSD. 39 * 40 * This code tries to do something half-way sensible with 41 * half-duplex hardware, such as with the SoundBlaster hardware. With 42 * half-duplex hardware allowing O_RDWR access doesn't really make 43 * sense. However, closing and opening the device to "turn around the 44 * line" is relatively expensive and costs a card reset (which can 45 * take some time, at least for the SoundBlaster hardware). Instead 46 * we allow O_RDWR access, and provide an ioctl to set the "mode", 47 * i.e. playing or recording. 48 * 49 * If you write to a half-duplex device in record mode, the data is 50 * tossed. If you read from the device in play mode, you get silence 51 * filled buffers at the rate at which samples are naturally 52 * generated. 53 * 54 * If you try to set both play and record mode on a half-duplex 55 * device, playing takes precedence. 56 */ 57 58 /* 59 * Todo: 60 * - Add softaudio() isr processing for wakeup, poll, signals, 61 * and silence fill. 62 */ 63 64 #include "audio.h" 65 #if NAUDIO > 0 66 67 #include <sys/param.h> 68 #include <sys/ioctl.h> 69 #include <sys/fcntl.h> 70 #include <sys/vnode.h> 71 #include <sys/select.h> 72 #include <sys/poll.h> 73 #include <sys/malloc.h> 74 #include <sys/proc.h> 75 #include <sys/systm.h> 76 #include <sys/syslog.h> 77 #include <sys/kernel.h> 78 #include <sys/signalvar.h> 79 #include <sys/conf.h> 80 #include <sys/audioio.h> 81 #include <sys/device.h> 82 83 #include <dev/audio_if.h> 84 #include <dev/audiovar.h> 85 86 #include <dev/rndvar.h> 87 88 #include <uvm/uvm_extern.h> 89 90 #include <machine/endian.h> 91 92 #ifdef AUDIO_DEBUG 93 #define DPRINTF(x) if (audiodebug) printf x 94 #define DPRINTFN(n,x) if (audiodebug>(n)) printf x 95 int audiodebug = 0; 96 #else 97 #define DPRINTF(x) 98 #define DPRINTFN(n,x) 99 #endif 100 101 #define ROUNDSIZE(x) x &= -16 /* round to nice boundary */ 102 103 int audio_blk_ms = AUDIO_BLK_MS; 104 105 int audiosetinfo __P((struct audio_softc *, struct audio_info *)); 106 int audiogetinfo __P((struct audio_softc *, struct audio_info *)); 107 108 int audio_open __P((dev_t, struct audio_softc *, int, int, struct proc *)); 109 int audio_close __P((dev_t, int, int, struct proc *)); 110 int audio_read __P((dev_t, struct uio *, int)); 111 int audio_write __P((dev_t, struct uio *, int)); 112 int audio_ioctl __P((dev_t, u_long, caddr_t, int, struct proc *)); 113 int audio_select __P((dev_t, int, struct proc *)); 114 paddr_t audio_mmap __P((dev_t, off_t, int)); 115 116 int mixer_open __P((dev_t, struct audio_softc *, int, int, struct proc *)); 117 int mixer_close __P((dev_t, int, int, struct proc *)); 118 int mixer_ioctl __P((dev_t, u_long, caddr_t, int, struct proc *)); 119 static void mixer_remove __P((struct audio_softc *, struct proc *p)); 120 static void mixer_signal __P((struct audio_softc *)); 121 122 void audio_init_record __P((struct audio_softc *)); 123 void audio_init_play __P((struct audio_softc *)); 124 int audiostartr __P((struct audio_softc *)); 125 int audiostartp __P((struct audio_softc *)); 126 void audio_rint __P((void *)); 127 void audio_pint __P((void *)); 128 int audio_check_params __P((struct audio_params *)); 129 130 void audio_calc_blksize __P((struct audio_softc *, int)); 131 void audio_fill_silence __P((struct audio_params *, u_char *, int)); 132 int audio_silence_copyout __P((struct audio_softc *, int, struct uio *)); 133 134 void audio_init_ringbuffer __P((struct audio_ringbuffer *)); 135 int audio_initbufs __P((struct audio_softc *)); 136 void audio_calcwater __P((struct audio_softc *)); 137 static __inline int audio_sleep_timo __P((int *, char *, int)); 138 static __inline int audio_sleep __P((int *, char *)); 139 static __inline void audio_wakeup __P((int *)); 140 int audio_drain __P((struct audio_softc *)); 141 void audio_clear __P((struct audio_softc *)); 142 static __inline void audio_pint_silence __P((struct audio_softc *, struct audio_ringbuffer *, u_char *, int)); 143 144 int audio_alloc_ring __P((struct audio_softc *, struct audio_ringbuffer *, int, int)); 145 void audio_free_ring __P((struct audio_softc *, struct audio_ringbuffer *)); 146 147 int audioprint __P((void *, const char *)); 148 149 #define __BROKEN_INDIRECT_CONFIG /* XXX */ 150 #ifdef __BROKEN_INDIRECT_CONFIG 151 int audioprobe __P((struct device *, void *, void *)); 152 #else 153 int audioprobe __P((struct device *, struct cfdata *, void *)); 154 #endif 155 void audioattach __P((struct device *, struct device *, void *)); 156 int audiodetach __P((struct device *, int)); 157 int audioactivate __P((struct device *, enum devact)); 158 159 struct portname { 160 char *name; 161 int mask; 162 }; 163 static struct portname itable[] = { 164 { AudioNmicrophone, AUDIO_MICROPHONE }, 165 { AudioNline, AUDIO_LINE_IN }, 166 { AudioNcd, AUDIO_CD }, 167 { 0 } 168 }; 169 static struct portname otable[] = { 170 { AudioNspeaker, AUDIO_SPEAKER }, 171 { AudioNheadphone, AUDIO_HEADPHONE }, 172 { AudioNline, AUDIO_LINE_OUT }, 173 { 0 } 174 }; 175 void au_check_ports __P((struct audio_softc *, struct au_mixer_ports *, 176 mixer_devinfo_t *, int, char *, char *, 177 struct portname *)); 178 int au_set_gain __P((struct audio_softc *, struct au_mixer_ports *, 179 int, int)); 180 void au_get_gain __P((struct audio_softc *, struct au_mixer_ports *, 181 u_int *, u_char *)); 182 int au_set_port __P((struct audio_softc *, struct au_mixer_ports *, 183 u_int)); 184 int au_get_port __P((struct audio_softc *, struct au_mixer_ports *)); 185 int au_get_lr_value __P((struct audio_softc *, mixer_ctrl_t *, 186 int *, int *r)); 187 int au_set_lr_value __P((struct audio_softc *, mixer_ctrl_t *, 188 int, int)); 189 int au_portof __P((struct audio_softc *, char *)); 190 191 192 /* The default audio mode: 8 kHz mono ulaw */ 193 struct audio_params audio_default = 194 { 8000, AUDIO_ENCODING_ULAW, 8, 1, 0, 1 }; 195 196 struct cfattach audio_ca = { 197 sizeof(struct audio_softc), audioprobe, audioattach, 198 audiodetach, audioactivate 199 }; 200 201 struct cfdriver audio_cd = { 202 NULL, "audio", DV_DULL 203 }; 204 205 int 206 audioprobe(parent, match, aux) 207 struct device *parent; 208 #ifdef __BROKEN_INDIRECT_CONFIG 209 void *match; 210 #else 211 struct cfdata *match; 212 #endif 213 void *aux; 214 { 215 struct audio_attach_args *sa = aux; 216 217 DPRINTF(("audioprobe: type=%d sa=%p hw=%p\n", 218 sa->type, sa, sa->hwif)); 219 return (sa->type == AUDIODEV_TYPE_AUDIO) ? 1 : 0; 220 } 221 222 void 223 audioattach(parent, self, aux) 224 struct device *parent, *self; 225 void *aux; 226 { 227 struct audio_softc *sc = (void *)self; 228 struct audio_attach_args *sa = aux; 229 struct audio_hw_if *hwp = sa->hwif; 230 void *hdlp = sa->hdl; 231 int error; 232 mixer_devinfo_t mi; 233 int iclass, oclass; 234 235 printf("\n"); 236 237 #ifdef DIAGNOSTIC 238 if (hwp == 0 || 239 hwp->open == 0 || 240 hwp->close == 0 || 241 hwp->query_encoding == 0 || 242 hwp->set_params == 0 || 243 (hwp->start_output == 0 && hwp->trigger_output == 0) || 244 (hwp->start_input == 0 && hwp->trigger_input == 0) || 245 hwp->halt_output == 0 || 246 hwp->halt_input == 0 || 247 hwp->getdev == 0 || 248 hwp->set_port == 0 || 249 hwp->get_port == 0 || 250 hwp->query_devinfo == 0 || 251 hwp->get_props == 0) { 252 printf("audio: missing method\n"); 253 sc->hw_if = 0; 254 return; 255 } 256 #endif 257 258 sc->hw_if = hwp; 259 sc->hw_hdl = hdlp; 260 sc->sc_dev = parent; 261 262 error = audio_alloc_ring(sc, &sc->sc_pr, AUMODE_PLAY, AU_RING_SIZE); 263 if (error) { 264 sc->hw_if = 0; 265 printf("audio: could not allocate play buffer\n"); 266 return; 267 } 268 error = audio_alloc_ring(sc, &sc->sc_rr, AUMODE_RECORD, AU_RING_SIZE); 269 if (error) { 270 audio_free_ring(sc, &sc->sc_pr); 271 sc->hw_if = 0; 272 printf("audio: could not allocate record buffer\n"); 273 return; 274 } 275 276 /* 277 * Set default softc params 278 */ 279 sc->sc_pparams = audio_default; 280 sc->sc_rparams = audio_default; 281 282 /* Set up some default values */ 283 sc->sc_blkset = 0; 284 audio_calc_blksize(sc, AUMODE_RECORD); 285 audio_calc_blksize(sc, AUMODE_PLAY); 286 audio_init_ringbuffer(&sc->sc_rr); 287 audio_init_ringbuffer(&sc->sc_pr); 288 audio_calcwater(sc); 289 290 iclass = oclass = -1; 291 sc->sc_inports.index = -1; 292 sc->sc_inports.nports = 0; 293 sc->sc_inports.isenum = 0; 294 sc->sc_inports.allports = 0; 295 sc->sc_outports.index = -1; 296 sc->sc_outports.nports = 0; 297 sc->sc_outports.isenum = 0; 298 sc->sc_outports.allports = 0; 299 sc->sc_monitor_port = -1; 300 for(mi.index = 0; ; mi.index++) { 301 if (hwp->query_devinfo(hdlp, &mi) != 0) 302 break; 303 if (mi.type == AUDIO_MIXER_CLASS && 304 strcmp(mi.label.name, AudioCrecord) == 0) 305 iclass = mi.index; 306 if (mi.type == AUDIO_MIXER_CLASS && 307 strcmp(mi.label.name, AudioCmonitor) == 0) 308 oclass = mi.index; 309 } 310 for(mi.index = 0; ; mi.index++) { 311 if (hwp->query_devinfo(hdlp, &mi) != 0) 312 break; 313 if (mi.type == AUDIO_MIXER_CLASS) 314 continue; 315 au_check_ports(sc, &sc->sc_inports, &mi, iclass, 316 AudioNsource, AudioNrecord, itable); 317 au_check_ports(sc, &sc->sc_outports, &mi, oclass, 318 AudioNoutput, AudioNmaster, otable); 319 if (mi.mixer_class == oclass && 320 strcmp(mi.label.name, AudioNmonitor)) 321 sc->sc_monitor_port = mi.index; 322 } 323 DPRINTF(("audio_attach: inputs ports=0x%x, output ports=0x%x\n", 324 sc->sc_inports.allports, sc->sc_outports.allports)); 325 } 326 327 int 328 audioactivate(self, act) 329 struct device *self; 330 enum devact act; 331 { 332 struct audio_softc *sc = (struct audio_softc *)self; 333 334 switch (act) { 335 case DVACT_ACTIVATE: 336 return (EOPNOTSUPP); 337 break; 338 339 case DVACT_DEACTIVATE: 340 sc->sc_dying = 1; 341 break; 342 } 343 return (0); 344 } 345 346 int 347 audiodetach(self, flags) 348 struct device *self; 349 int flags; 350 { 351 struct audio_softc *sc = (struct audio_softc *)self; 352 int maj, mn; 353 int s; 354 355 DPRINTF(("audio_detach: sc=%p flags=%d\n", sc, flags)); 356 357 sc->sc_dying = 1; 358 359 wakeup(&sc->sc_wchan); 360 wakeup(&sc->sc_rchan); 361 s = splaudio(); 362 if (--sc->sc_refcnt >= 0) { 363 if (tsleep(&sc->sc_refcnt, PZERO, "auddet", hz * 120)) 364 printf("audiodetach: %s didn't detach\n", 365 sc->dev.dv_xname); 366 } 367 splx(s); 368 369 /* free resources */ 370 audio_free_ring(sc, &sc->sc_pr); 371 audio_free_ring(sc, &sc->sc_rr); 372 373 /* locate the major number */ 374 for (maj = 0; maj < nchrdev; maj++) 375 if (cdevsw[maj].d_open == audioopen) 376 break; 377 378 /* Nuke the vnodes for any open instances (calls close). */ 379 mn = self->dv_unit; 380 vdevgone(maj, mn | SOUND_DEVICE, mn | SOUND_DEVICE, VCHR); 381 vdevgone(maj, mn | AUDIO_DEVICE, mn | AUDIO_DEVICE, VCHR); 382 vdevgone(maj, mn | AUDIOCTL_DEVICE, mn | AUDIOCTL_DEVICE, VCHR); 383 vdevgone(maj, mn | MIXER_DEVICE, mn | MIXER_DEVICE, VCHR); 384 385 return (0); 386 } 387 388 int 389 au_portof(sc, name) 390 struct audio_softc *sc; 391 char *name; 392 { 393 mixer_devinfo_t mi; 394 395 for(mi.index = 0; 396 sc->hw_if->query_devinfo(sc->hw_hdl, &mi) == 0; 397 mi.index++) 398 if (strcmp(mi.label.name, name) == 0) 399 return mi.index; 400 return -1; 401 } 402 403 void 404 au_check_ports(sc, ports, mi, cls, name, mname, tbl) 405 struct audio_softc *sc; 406 struct au_mixer_ports *ports; 407 mixer_devinfo_t *mi; 408 int cls; 409 char *name; 410 char *mname; 411 struct portname *tbl; 412 { 413 int i, j; 414 415 if (mi->mixer_class != cls) 416 return; 417 if (strcmp(mi->label.name, mname) == 0) { 418 ports->master = mi->index; 419 return; 420 } 421 if (strcmp(mi->label.name, name) != 0) 422 return; 423 if (mi->type == AUDIO_MIXER_ENUM) { 424 ports->index = mi->index; 425 for(i = 0; tbl[i].name; i++) { 426 for(j = 0; j < mi->un.e.num_mem; j++) { 427 if (strcmp(mi->un.e.member[j].label.name, 428 tbl[i].name) == 0) { 429 ports->aumask[ports->nports] = tbl[i].mask; 430 ports->misel [ports->nports] = mi->un.e.member[j].ord; 431 ports->miport[ports->nports++] = 432 au_portof(sc, mi->un.e.member[j].label.name); 433 ports->allports |= tbl[i].mask; 434 } 435 } 436 } 437 ports->isenum = 1; 438 } else if (mi->type == AUDIO_MIXER_SET) { 439 ports->index = mi->index; 440 for(i = 0; tbl[i].name; i++) { 441 for(j = 0; j < mi->un.s.num_mem; j++) { 442 if (strcmp(mi->un.s.member[j].label.name, 443 tbl[i].name) == 0) { 444 ports->aumask[ports->nports] = tbl[i].mask; 445 ports->misel [ports->nports] = mi->un.s.member[j].mask; 446 ports->miport[ports->nports++] = 447 au_portof(sc, mi->un.s.member[j].label.name); 448 ports->allports |= tbl[i].mask; 449 } 450 } 451 } 452 } 453 } 454 455 /* 456 * Called from hardware driver. This is where the MI audio driver gets 457 * probed/attached to the hardware driver. 458 */ 459 struct device * 460 audio_attach_mi(ahwp, hdlp, dev) 461 struct audio_hw_if *ahwp; 462 void *hdlp; 463 struct device *dev; 464 { 465 struct audio_attach_args arg; 466 467 #ifdef DIAGNOSTIC 468 if (ahwp == NULL) { 469 printf ("audio_attach_mi: NULL\n"); 470 return 0; 471 } 472 #endif 473 474 arg.type = AUDIODEV_TYPE_AUDIO; 475 arg.hwif = ahwp; 476 arg.hdl = hdlp; 477 return config_found(dev, &arg, audioprint); 478 } 479 480 #if NAUDIO > 0 481 int 482 audioprint(aux, pnp) 483 void *aux; 484 const char *pnp; 485 { 486 struct audio_attach_args *arg = aux; 487 const char *type; 488 489 if (pnp != NULL) { 490 switch (arg->type) { 491 case AUDIODEV_TYPE_AUDIO: 492 type = "audio"; 493 break; 494 case AUDIODEV_TYPE_OPL: 495 type = "opl"; 496 break; 497 case AUDIODEV_TYPE_MPU: 498 type = "mpu"; 499 break; 500 default: 501 panic("audioprint: unknown type %d", arg->type); 502 } 503 printf("%s at %s", type, pnp); 504 } 505 return (UNCONF); 506 } 507 508 #endif /* NAUDIO > 0 */ 509 510 #ifdef AUDIO_DEBUG 511 void audio_printsc __P((struct audio_softc *)); 512 void audio_print_params __P((char *, struct audio_params *)); 513 514 void 515 audio_printsc(sc) 516 struct audio_softc *sc; 517 { 518 printf("hwhandle %p hw_if %p ", sc->hw_hdl, sc->hw_if); 519 printf("open 0x%x mode 0x%x\n", sc->sc_open, sc->sc_mode); 520 printf("rchan 0x%x wchan 0x%x ", sc->sc_rchan, sc->sc_wchan); 521 printf("rring used 0x%x pring used=%d\n", sc->sc_rr.used, sc->sc_pr.used); 522 printf("rbus 0x%x pbus 0x%x ", sc->sc_rbus, sc->sc_pbus); 523 printf("blksize %d", sc->sc_pr.blksize); 524 printf("hiwat %d lowat %d\n", sc->sc_pr.usedhigh, sc->sc_pr.usedlow); 525 } 526 527 void 528 audio_print_params(s, p) 529 char *s; 530 struct audio_params *p; 531 { 532 printf("audio: %s sr=%ld, enc=%d, chan=%d, prec=%d\n", s, 533 p->sample_rate, p->encoding, p->channels, p->precision); 534 } 535 #endif 536 537 int 538 audio_alloc_ring(sc, r, direction, bufsize) 539 struct audio_softc *sc; 540 struct audio_ringbuffer *r; 541 int direction; 542 int bufsize; 543 { 544 struct audio_hw_if *hw = sc->hw_if; 545 void *hdl = sc->hw_hdl; 546 /* 547 * Alloc DMA play and record buffers 548 */ 549 if (bufsize < AUMINBUF) 550 bufsize = AUMINBUF; 551 ROUNDSIZE(bufsize); 552 if (hw->round_buffersize) 553 bufsize = hw->round_buffersize(hdl, direction, bufsize); 554 else if (hw->round_buffersize_old) 555 bufsize = hw->round_buffersize_old(hdl, bufsize); 556 r->bufsize = bufsize; 557 if (hw->allocm) 558 r->start = hw->allocm(hdl, direction, r->bufsize, M_DEVBUF, 559 M_WAITOK); 560 else if (hw->allocm_old) 561 r->start = hw->allocm_old(hdl, r->bufsize, M_DEVBUF, M_WAITOK); 562 else 563 r->start = malloc(bufsize, M_DEVBUF, M_WAITOK); 564 if (r->start == 0) 565 return ENOMEM; 566 return 0; 567 } 568 569 void 570 audio_free_ring(sc, r) 571 struct audio_softc *sc; 572 struct audio_ringbuffer *r; 573 { 574 if (sc->hw_if->freem) { 575 sc->hw_if->freem(sc->hw_hdl, r->start, M_DEVBUF); 576 } else { 577 free(r->start, M_DEVBUF); 578 } 579 } 580 581 int 582 audioopen(dev, flags, ifmt, p) 583 dev_t dev; 584 int flags, ifmt; 585 struct proc *p; 586 { 587 int unit = AUDIOUNIT(dev); 588 struct audio_softc *sc; 589 int error; 590 591 if (unit >= audio_cd.cd_ndevs || 592 (sc = audio_cd.cd_devs[unit]) == NULL) 593 return ENXIO; 594 595 if (sc->sc_dying) 596 return (EIO); 597 598 if (!sc->hw_if) 599 return (ENXIO); 600 601 sc->sc_refcnt ++; 602 switch (AUDIODEV(dev)) { 603 case SOUND_DEVICE: 604 case AUDIO_DEVICE: 605 case AUDIOCTL_DEVICE: 606 error = audio_open(dev, sc, flags, ifmt, p); 607 break; 608 case MIXER_DEVICE: 609 error = mixer_open(dev, sc, flags, ifmt, p); 610 break; 611 default: 612 error = ENXIO; 613 break; 614 } 615 616 if (--sc->sc_refcnt < 0) 617 wakeup(&sc->sc_refcnt); 618 619 return (error); 620 } 621 622 int 623 audioclose(dev, flags, ifmt, p) 624 dev_t dev; 625 int flags, ifmt; 626 struct proc *p; 627 { 628 629 switch (AUDIODEV(dev)) { 630 case SOUND_DEVICE: 631 case AUDIO_DEVICE: 632 return (audio_close(dev, flags, ifmt, p)); 633 case MIXER_DEVICE: 634 return (mixer_close(dev, flags, ifmt, p)); 635 case AUDIOCTL_DEVICE: 636 return 0; 637 default: 638 return (ENXIO); 639 } 640 } 641 642 int 643 audioread(dev, uio, ioflag) 644 dev_t dev; 645 struct uio *uio; 646 int ioflag; 647 { 648 int unit = AUDIOUNIT(dev); 649 struct audio_softc *sc; 650 int error; 651 652 if (unit >= audio_cd.cd_ndevs || 653 (sc = audio_cd.cd_devs[unit]) == NULL) 654 return ENXIO; 655 656 if (sc->sc_dying) 657 return (EIO); 658 659 sc->sc_refcnt ++; 660 switch (AUDIODEV(dev)) { 661 case SOUND_DEVICE: 662 case AUDIO_DEVICE: 663 error = audio_read(dev, uio, ioflag); 664 break; 665 case AUDIOCTL_DEVICE: 666 case MIXER_DEVICE: 667 error = ENODEV; 668 break; 669 default: 670 error = ENXIO; 671 break; 672 } 673 674 if (--sc->sc_refcnt < 0) 675 wakeup(&sc->sc_refcnt); 676 return (error); 677 } 678 679 int 680 audiowrite(dev, uio, ioflag) 681 dev_t dev; 682 struct uio *uio; 683 int ioflag; 684 { 685 int unit = AUDIOUNIT(dev); 686 struct audio_softc *sc; 687 int error; 688 689 if (unit >= audio_cd.cd_ndevs || 690 (sc = audio_cd.cd_devs[unit]) == NULL) 691 return ENXIO; 692 693 if (sc->sc_dying) 694 return (EIO); 695 696 sc->sc_refcnt ++; 697 switch (AUDIODEV(dev)) { 698 case SOUND_DEVICE: 699 case AUDIO_DEVICE: 700 error = audio_write(dev, uio, ioflag); 701 break; 702 case AUDIOCTL_DEVICE: 703 case MIXER_DEVICE: 704 error = ENODEV; 705 break; 706 default: 707 error = ENXIO; 708 break; 709 } 710 711 if (--sc->sc_refcnt < 0) 712 wakeup(&sc->sc_refcnt); 713 return (error); 714 } 715 716 int 717 audioioctl(dev, cmd, addr, flag, p) 718 dev_t dev; 719 u_long cmd; 720 caddr_t addr; 721 int flag; 722 struct proc *p; 723 { 724 int unit = AUDIOUNIT(dev); 725 struct audio_softc *sc; 726 int error; 727 728 if (unit >= audio_cd.cd_ndevs || 729 (sc = audio_cd.cd_devs[unit]) == NULL) 730 return ENXIO; 731 732 if (sc->sc_dying) 733 return (EIO); 734 735 sc->sc_refcnt ++; 736 switch (AUDIODEV(dev)) { 737 case SOUND_DEVICE: 738 case AUDIO_DEVICE: 739 case AUDIOCTL_DEVICE: 740 error = audio_ioctl(dev, cmd, addr, flag, p); 741 break; 742 case MIXER_DEVICE: 743 error = mixer_ioctl(dev, cmd, addr, flag, p); 744 break; 745 default: 746 error = ENXIO; 747 break; 748 } 749 750 if (--sc->sc_refcnt < 0) 751 wakeup(&sc->sc_refcnt); 752 return (error); 753 } 754 755 int 756 audioselect(dev, events, p) 757 dev_t dev; 758 int events; 759 struct proc *p; 760 { 761 int unit = AUDIOUNIT(dev); 762 struct audio_softc *sc; 763 int error; 764 765 if (unit >= audio_cd.cd_ndevs || 766 (sc = audio_cd.cd_devs[unit]) == NULL) 767 return ENXIO; 768 769 if (sc->sc_dying) 770 return (EIO); 771 772 sc->sc_refcnt ++; 773 switch (AUDIODEV(dev)) { 774 case SOUND_DEVICE: 775 case AUDIO_DEVICE: 776 error = audio_select(dev, events, p); 777 break; 778 case AUDIOCTL_DEVICE: 779 case MIXER_DEVICE: 780 error = 0; 781 break; 782 default: 783 error = 0; 784 break; 785 } 786 787 if (--sc->sc_refcnt < 0) 788 wakeup(&sc->sc_refcnt); 789 return (error); 790 } 791 792 paddr_t 793 audiommap(dev, off, prot) 794 dev_t dev; 795 off_t off; 796 int prot; 797 { 798 int unit = AUDIOUNIT(dev); 799 struct audio_softc *sc; 800 int ret; 801 802 if (unit >= audio_cd.cd_ndevs || 803 (sc = audio_cd.cd_devs[unit]) == NULL) 804 return (-1); 805 806 if (sc->sc_dying) 807 return (-1); 808 809 sc->sc_refcnt ++; 810 switch (AUDIODEV(dev)) { 811 case SOUND_DEVICE: 812 case AUDIO_DEVICE: 813 ret = audio_mmap(dev, off, prot); 814 break; 815 case AUDIOCTL_DEVICE: 816 case MIXER_DEVICE: 817 ret = -1; 818 break; 819 default: 820 ret = -1; 821 break; 822 } 823 824 if (--sc->sc_refcnt < 0) 825 wakeup(&sc->sc_refcnt); 826 return (ret); 827 } 828 829 /* 830 * Audio driver 831 */ 832 void 833 audio_init_ringbuffer(rp) 834 struct audio_ringbuffer *rp; 835 { 836 int nblks; 837 int blksize = rp->blksize; 838 839 if (blksize < AUMINBLK) 840 blksize = AUMINBLK; 841 nblks = rp->bufsize / blksize; 842 if (nblks < AUMINNOBLK) { 843 nblks = AUMINNOBLK; 844 blksize = rp->bufsize / nblks; 845 ROUNDSIZE(blksize); 846 } 847 DPRINTF(("audio_init_ringbuffer: blksize=%d\n", blksize)); 848 rp->blksize = blksize; 849 rp->maxblks = nblks; 850 rp->used = 0; 851 rp->end = rp->start + nblks * blksize; 852 rp->inp = rp->outp = rp->start; 853 rp->stamp = 0; 854 rp->drops = 0; 855 rp->pause = 0; 856 rp->copying = 0; 857 rp->needfill = 0; 858 rp->mmapped = 0; 859 } 860 861 int 862 audio_initbufs(sc) 863 struct audio_softc *sc; 864 { 865 struct audio_hw_if *hw = sc->hw_if; 866 int error; 867 868 DPRINTF(("audio_initbufs: mode=0x%x\n", sc->sc_mode)); 869 audio_init_ringbuffer(&sc->sc_rr); 870 if (hw->init_input && (sc->sc_mode & AUMODE_RECORD)) { 871 error = hw->init_input(sc->hw_hdl, sc->sc_rr.start, 872 sc->sc_rr.end - sc->sc_rr.start); 873 if (error) 874 return error; 875 } 876 877 audio_init_ringbuffer(&sc->sc_pr); 878 sc->sc_sil_count = 0; 879 if (hw->init_output && (sc->sc_mode & AUMODE_PLAY)) { 880 error = hw->init_output(sc->hw_hdl, sc->sc_pr.start, 881 sc->sc_pr.end - sc->sc_pr.start); 882 if (error) 883 return error; 884 } 885 886 #ifdef AUDIO_INTR_TIME 887 #define double u_long 888 sc->sc_pnintr = 0; 889 sc->sc_pblktime = (u_long)( 890 (double)sc->sc_pr.blksize * 100000 / 891 (double)(sc->sc_pparams.precision / NBBY * 892 sc->sc_pparams.channels * 893 sc->sc_pparams.sample_rate)) * 10; 894 DPRINTF(("audio: play blktime = %lu for %d\n", 895 sc->sc_pblktime, sc->sc_pr.blksize)); 896 sc->sc_rnintr = 0; 897 sc->sc_rblktime = (u_long)( 898 (double)sc->sc_rr.blksize * 100000 / 899 (double)(sc->sc_rparams.precision / NBBY * 900 sc->sc_rparams.channels * 901 sc->sc_rparams.sample_rate)) * 10; 902 DPRINTF(("audio: record blktime = %lu for %d\n", 903 sc->sc_rblktime, sc->sc_rr.blksize)); 904 #undef double 905 #endif 906 907 return 0; 908 } 909 910 void 911 audio_calcwater(sc) 912 struct audio_softc *sc; 913 { 914 sc->sc_pr.usedhigh = sc->sc_pr.end - sc->sc_pr.start; 915 sc->sc_pr.usedlow = sc->sc_pr.usedhigh * 3 / 4; /* set lowater at 75% */ 916 if (sc->sc_pr.usedlow == sc->sc_pr.usedhigh) 917 sc->sc_pr.usedlow -= sc->sc_pr.blksize; 918 sc->sc_rr.usedhigh = sc->sc_pr.end - sc->sc_pr.start - sc->sc_pr.blksize; 919 sc->sc_rr.usedlow = 0; 920 } 921 922 static __inline int 923 audio_sleep_timo(chan, label, timo) 924 int *chan; 925 char *label; 926 int timo; 927 { 928 int st; 929 930 if (!label) 931 label = "audio"; 932 933 DPRINTFN(3, ("audio_sleep_timo: chan=%p, label=%s, timo=%d\n", 934 chan, label, timo)); 935 *chan = 1; 936 st = tsleep(chan, PWAIT | PCATCH, label, timo); 937 *chan = 0; 938 #ifdef AUDIO_DEBUG 939 if (st != 0) 940 printf("audio_sleep: woke up st=%d\n", st); 941 #endif 942 return (st); 943 } 944 945 static __inline int 946 audio_sleep(chan, label) 947 int *chan; 948 char *label; 949 { 950 return audio_sleep_timo(chan, label, 0); 951 } 952 953 /* call at splaudio() */ 954 static __inline void 955 audio_wakeup(chan) 956 int *chan; 957 { 958 DPRINTFN(3, ("audio_wakeup: chan=%p, *chan=%d\n", chan, *chan)); 959 if (*chan) { 960 wakeup(chan); 961 *chan = 0; 962 } 963 } 964 965 int 966 audio_open(dev, sc, flags, ifmt, p) 967 dev_t dev; 968 struct audio_softc *sc; 969 int flags, ifmt; 970 struct proc *p; 971 { 972 int error; 973 int mode; 974 struct audio_info ai; 975 976 DPRINTF(("audio_open: dev=0x%x flags=0x%x sc=%p hdl=%p\n", dev, flags, sc, sc->hw_hdl)); 977 978 if (ISDEVAUDIOCTL(dev)) 979 return 0; 980 981 if ((sc->sc_open & (AUOPEN_READ|AUOPEN_WRITE)) != 0) 982 return (EBUSY); 983 984 error = sc->hw_if->open(sc->hw_hdl, flags); 985 if (error) 986 return (error); 987 988 sc->sc_async_audio = 0; 989 sc->sc_rchan = 0; 990 sc->sc_wchan = 0; 991 sc->sc_blkset = 0; /* Block sizes not set yet */ 992 sc->sc_sil_count = 0; 993 sc->sc_rbus = 0; 994 sc->sc_pbus = 0; 995 sc->sc_eof = 0; 996 sc->sc_playdrop = 0; 997 998 sc->sc_full_duplex = 0; 999 /* doesn't always work right on SB. 1000 (flags & (FWRITE|FREAD)) == (FWRITE|FREAD) && 1001 (sc->hw_if->get_props(sc->hw_hdl) & AUDIO_PROP_FULLDUPLEX); 1002 */ 1003 1004 mode = 0; 1005 if (flags & FREAD) { 1006 sc->sc_open |= AUOPEN_READ; 1007 mode |= AUMODE_RECORD; 1008 } 1009 if (flags & FWRITE) { 1010 sc->sc_open |= AUOPEN_WRITE; 1011 mode |= AUMODE_PLAY | AUMODE_PLAY_ALL; 1012 } 1013 1014 /* 1015 * Multiplex device: /dev/audio (MU-Law) and /dev/sound (linear) 1016 * The /dev/audio is always (re)set to 8-bit MU-Law mono 1017 * For the other devices, you get what they were last set to. 1018 */ 1019 if (ISDEVAUDIO(dev)) { 1020 /* /dev/audio */ 1021 sc->sc_rparams = audio_default; 1022 sc->sc_pparams = audio_default; 1023 } 1024 #ifdef DIAGNOSTIC 1025 /* 1026 * Sample rate and precision are supposed to be set to proper 1027 * default values by the hardware driver, so that it may give 1028 * us these values. 1029 */ 1030 if (sc->sc_rparams.precision == 0 || sc->sc_pparams.precision == 0) { 1031 printf("audio_open: 0 precision\n"); 1032 return EINVAL; 1033 } 1034 #endif 1035 1036 AUDIO_INITINFO(&ai); 1037 ai.record.sample_rate = sc->sc_rparams.sample_rate; 1038 ai.record.encoding = sc->sc_rparams.encoding; 1039 ai.record.channels = sc->sc_rparams.channels; 1040 ai.record.precision = sc->sc_rparams.precision; 1041 ai.play.sample_rate = sc->sc_pparams.sample_rate; 1042 ai.play.encoding = sc->sc_pparams.encoding; 1043 ai.play.channels = sc->sc_pparams.channels; 1044 ai.play.precision = sc->sc_pparams.precision; 1045 ai.mode = mode; 1046 sc->sc_pr.blksize = sc->sc_rr.blksize = 0; /* force recalculation */ 1047 error = audiosetinfo(sc, &ai); 1048 if (error) 1049 goto bad; 1050 1051 DPRINTF(("audio_open: done sc_mode = 0x%x\n", sc->sc_mode)); 1052 1053 return 0; 1054 1055 bad: 1056 sc->hw_if->close(sc->hw_hdl); 1057 sc->sc_open = 0; 1058 sc->sc_mode = 0; 1059 sc->sc_full_duplex = 0; 1060 return error; 1061 } 1062 1063 /* 1064 * Must be called from task context. 1065 */ 1066 void 1067 audio_init_record(sc) 1068 struct audio_softc *sc; 1069 { 1070 int s = splaudio(); 1071 1072 if (sc->hw_if->speaker_ctl && 1073 (!sc->sc_full_duplex || (sc->sc_mode & AUMODE_PLAY) == 0)) 1074 sc->hw_if->speaker_ctl(sc->hw_hdl, SPKR_OFF); 1075 splx(s); 1076 } 1077 1078 /* 1079 * Must be called from task context. 1080 */ 1081 void 1082 audio_init_play(sc) 1083 struct audio_softc *sc; 1084 { 1085 int s = splaudio(); 1086 1087 sc->sc_wstamp = sc->sc_pr.stamp; 1088 if (sc->hw_if->speaker_ctl) 1089 sc->hw_if->speaker_ctl(sc->hw_hdl, SPKR_ON); 1090 splx(s); 1091 } 1092 1093 int 1094 audio_drain(sc) 1095 struct audio_softc *sc; 1096 { 1097 int error, drops; 1098 struct audio_ringbuffer *cb = &sc->sc_pr; 1099 int s; 1100 1101 DPRINTF(("audio_drain: enter busy=%d used=%d\n", 1102 sc->sc_pbus, sc->sc_pr.used)); 1103 if (sc->sc_pr.mmapped || sc->sc_pr.used <= 0) 1104 return 0; 1105 if (!sc->sc_pbus) { 1106 /* We've never started playing, probably because the 1107 * block was too short. Pad it and start now. 1108 */ 1109 int cc; 1110 u_char *inp = cb->inp; 1111 1112 cc = cb->blksize - (inp - cb->start) % cb->blksize; 1113 audio_fill_silence(&sc->sc_pparams, inp, cc); 1114 inp += cc; 1115 if (inp >= cb->end) 1116 inp = cb->start; 1117 s = splaudio(); 1118 cb->used += cc; 1119 cb->inp = inp; 1120 error = audiostartp(sc); 1121 splx(s); 1122 if (error) 1123 return error; 1124 } 1125 /* 1126 * Play until a silence block has been played, then we 1127 * know all has been drained. 1128 * XXX This should be done some other way to avoid 1129 * playing silence. 1130 */ 1131 #ifdef DIAGNOSTIC 1132 if (cb->copying) { 1133 printf("audio_drain: copying in progress!?!\n"); 1134 cb->copying = 0; 1135 } 1136 #endif 1137 drops = cb->drops; 1138 error = 0; 1139 s = splaudio(); 1140 while (cb->drops == drops && !error) { 1141 DPRINTF(("audio_drain: used=%d, drops=%ld\n", sc->sc_pr.used, cb->drops)); 1142 /* 1143 * When the process is exiting, it ignores all signals and 1144 * we can't interrupt this sleep, so we set a timeout just in case. 1145 */ 1146 error = audio_sleep_timo(&sc->sc_wchan, "aud_dr", 30*hz); 1147 if (sc->sc_dying) 1148 error = EIO; 1149 } 1150 splx(s); 1151 return error; 1152 } 1153 1154 /* 1155 * Close an audio chip. 1156 */ 1157 /* ARGSUSED */ 1158 int 1159 audio_close(dev, flags, ifmt, p) 1160 dev_t dev; 1161 int flags, ifmt; 1162 struct proc *p; 1163 { 1164 int unit = AUDIOUNIT(dev); 1165 struct audio_softc *sc = audio_cd.cd_devs[unit]; 1166 struct audio_hw_if *hw = sc->hw_if; 1167 int s; 1168 1169 DPRINTF(("audio_close: unit=%d flags=0x%x\n", unit, flags)); 1170 1171 s = splaudio(); 1172 /* Stop recording. */ 1173 if ((flags & FREAD) && sc->sc_rbus) { 1174 /* 1175 * XXX Some drivers (e.g. SB) use the same routine 1176 * to halt input and output so don't halt input if 1177 * in full duplex mode. These drivers should be fixed. 1178 */ 1179 if (!sc->sc_full_duplex || sc->hw_if->halt_input != sc->hw_if->halt_output) 1180 sc->hw_if->halt_input(sc->hw_hdl); 1181 sc->sc_rbus = 0; 1182 } 1183 /* 1184 * Block until output drains, but allow ^C interrupt. 1185 */ 1186 sc->sc_pr.usedlow = sc->sc_pr.blksize; /* avoid excessive wakeups */ 1187 /* 1188 * If there is pending output, let it drain (unless 1189 * the output is paused). 1190 */ 1191 if ((flags & FWRITE) && sc->sc_pbus) { 1192 if (!sc->sc_pr.pause && !audio_drain(sc) && hw->drain) 1193 (void)hw->drain(sc->hw_hdl); 1194 sc->hw_if->halt_output(sc->hw_hdl); 1195 sc->sc_pbus = 0; 1196 } 1197 1198 hw->close(sc->hw_hdl); 1199 1200 /* 1201 * If flags has neither read nor write then reset both 1202 * directions. Encountered when someone runs revoke(2). 1203 */ 1204 1205 if ((flags & FREAD) || ((flags & (FREAD|FWRITE)) == 0)) { 1206 sc->sc_open &= ~AUOPEN_READ; 1207 sc->sc_mode &= ~AUMODE_RECORD; 1208 } 1209 if ((flags & FWRITE) || ((flags & (FREAD|FWRITE)) == 0)) { 1210 sc->sc_open &= ~AUOPEN_WRITE; 1211 sc->sc_mode &= ~(AUMODE_PLAY|AUMODE_PLAY_ALL); 1212 } 1213 1214 sc->sc_async_audio = 0; 1215 sc->sc_full_duplex = 0; 1216 splx(s); 1217 DPRINTF(("audio_close: done\n")); 1218 1219 return (0); 1220 } 1221 1222 int 1223 audio_read(dev, uio, ioflag) 1224 dev_t dev; 1225 struct uio *uio; 1226 int ioflag; 1227 { 1228 int unit = AUDIOUNIT(dev); 1229 struct audio_softc *sc = audio_cd.cd_devs[unit]; 1230 struct audio_ringbuffer *cb = &sc->sc_rr; 1231 u_char *outp; 1232 int error, s, used, cc, n; 1233 1234 if (cb->mmapped) 1235 return EINVAL; 1236 1237 DPRINTFN(1,("audio_read: cc=%d mode=%d\n", 1238 uio->uio_resid, sc->sc_mode)); 1239 1240 error = 0; 1241 /* 1242 * If hardware is half-duplex and currently playing, return 1243 * silence blocks based on the number of blocks we have output. 1244 */ 1245 if (!sc->sc_full_duplex && 1246 (sc->sc_mode & AUMODE_PLAY)) { 1247 while (uio->uio_resid > 0 && !error) { 1248 s = splaudio(); 1249 for(;;) { 1250 cc = sc->sc_pr.stamp - sc->sc_wstamp; 1251 if (cc > 0) 1252 break; 1253 DPRINTF(("audio_read: stamp=%lu, wstamp=%lu\n", 1254 sc->sc_pr.stamp, sc->sc_wstamp)); 1255 if (ioflag & IO_NDELAY) { 1256 splx(s); 1257 return EWOULDBLOCK; 1258 } 1259 error = audio_sleep(&sc->sc_rchan, "aud_hr"); 1260 if (sc->sc_dying) 1261 error = EIO; 1262 if (error) { 1263 splx(s); 1264 return error; 1265 } 1266 } 1267 splx(s); 1268 1269 if (uio->uio_resid < cc) 1270 cc = uio->uio_resid; 1271 DPRINTFN(1, ("audio_read: reading in write mode, cc=%d\n", cc)); 1272 error = audio_silence_copyout(sc, cc, uio); 1273 sc->sc_wstamp += cc; 1274 } 1275 return (error); 1276 } 1277 while (uio->uio_resid > 0 && !error) { 1278 s = splaudio(); 1279 while (cb->used <= 0) { 1280 if (ioflag & IO_NDELAY) { 1281 splx(s); 1282 return EWOULDBLOCK; 1283 } 1284 if (!sc->sc_rbus) { 1285 error = audiostartr(sc); 1286 if (error) { 1287 splx(s); 1288 return error; 1289 } 1290 } 1291 DPRINTFN(2, ("audio_read: sleep used=%d\n", cb->used)); 1292 error = audio_sleep(&sc->sc_rchan, "aud_rd"); 1293 if (sc->sc_dying) 1294 error = EIO; 1295 if (error) { 1296 splx(s); 1297 return error; 1298 } 1299 } 1300 used = cb->used; 1301 outp = cb->outp; 1302 cb->copying = 1; 1303 splx(s); 1304 cc = used - cb->usedlow; /* maximum to read */ 1305 n = cb->end - outp; 1306 if (n < cc) 1307 cc = n; /* don't read beyond end of buffer */ 1308 1309 if (uio->uio_resid < cc) 1310 cc = uio->uio_resid; /* and no more than we want */ 1311 1312 if (sc->sc_rparams.sw_code) 1313 sc->sc_rparams.sw_code(sc->hw_hdl, outp, cc); 1314 DPRINTFN(1,("audio_read: outp=%p, cc=%d\n", outp, cc)); 1315 error = uiomove(outp, cc, uio); 1316 used -= cc; 1317 outp += cc; 1318 if (outp >= cb->end) 1319 outp = cb->start; 1320 s = splaudio(); 1321 cb->outp = outp; 1322 cb->used = used; 1323 cb->copying = 0; 1324 splx(s); 1325 } 1326 return (error); 1327 } 1328 1329 void 1330 audio_clear(sc) 1331 struct audio_softc *sc; 1332 { 1333 int s = splaudio(); 1334 1335 if (sc->sc_rbus) { 1336 audio_wakeup(&sc->sc_rchan); 1337 sc->hw_if->halt_input(sc->hw_hdl); 1338 sc->sc_rbus = 0; 1339 } 1340 if (sc->sc_pbus) { 1341 audio_wakeup(&sc->sc_wchan); 1342 sc->hw_if->halt_output(sc->hw_hdl); 1343 sc->sc_pbus = 0; 1344 } 1345 splx(s); 1346 } 1347 1348 void 1349 audio_calc_blksize(sc, mode) 1350 struct audio_softc *sc; 1351 int mode; 1352 { 1353 struct audio_hw_if *hw = sc->hw_if; 1354 struct audio_params *parm; 1355 struct audio_ringbuffer *rb; 1356 int bs; 1357 1358 if (sc->sc_blkset) 1359 return; 1360 1361 if (mode == AUMODE_PLAY) { 1362 parm = &sc->sc_pparams; 1363 rb = &sc->sc_pr; 1364 } else { 1365 parm = &sc->sc_rparams; 1366 rb = &sc->sc_rr; 1367 } 1368 1369 bs = parm->sample_rate * audio_blk_ms / 1000 * 1370 parm->channels * parm->precision / NBBY * 1371 parm->factor; 1372 ROUNDSIZE(bs); 1373 if (hw->round_blocksize) 1374 bs = hw->round_blocksize(sc->hw_hdl, bs); 1375 rb->blksize = bs; 1376 1377 DPRINTF(("audio_calc_blksize: %s blksize=%d\n", 1378 mode == AUMODE_PLAY ? "play" : "record", bs)); 1379 } 1380 1381 void 1382 audio_fill_silence(params, p, n) 1383 struct audio_params *params; 1384 u_char *p; 1385 int n; 1386 { 1387 u_char auzero0, auzero1 = 0; /* initialize to please gcc */ 1388 int nfill = 1; 1389 1390 switch (params->encoding) { 1391 case AUDIO_ENCODING_ULAW: 1392 auzero0 = 0x7f; 1393 break; 1394 case AUDIO_ENCODING_ALAW: 1395 auzero0 = 0x55; 1396 break; 1397 case AUDIO_ENCODING_MPEG_L1_STREAM: 1398 case AUDIO_ENCODING_MPEG_L1_PACKETS: 1399 case AUDIO_ENCODING_MPEG_L1_SYSTEM: 1400 case AUDIO_ENCODING_MPEG_L2_STREAM: 1401 case AUDIO_ENCODING_MPEG_L2_PACKETS: 1402 case AUDIO_ENCODING_MPEG_L2_SYSTEM: 1403 case AUDIO_ENCODING_ADPCM: /* is this right XXX */ 1404 case AUDIO_ENCODING_SLINEAR_LE: 1405 case AUDIO_ENCODING_SLINEAR_BE: 1406 auzero0 = 0; /* fortunately this works for both 8 and 16 bits */ 1407 break; 1408 case AUDIO_ENCODING_ULINEAR_LE: 1409 case AUDIO_ENCODING_ULINEAR_BE: 1410 if (params->precision == 16) { 1411 nfill = 2; 1412 if (params->encoding == AUDIO_ENCODING_ULINEAR_LE) { 1413 auzero0 = 0; 1414 auzero1 = 0x80; 1415 } else { 1416 auzero0 = 0x80; 1417 auzero1 = 0; 1418 } 1419 } else 1420 auzero0 = 0x80; 1421 break; 1422 default: 1423 DPRINTF(("audio: bad encoding %d\n", params->encoding)); 1424 auzero0 = 0; 1425 break; 1426 } 1427 if (nfill == 1) { 1428 while (--n >= 0) 1429 *p++ = auzero0; /* XXX memset */ 1430 } else /* nfill must be 2 */ { 1431 while (n > 1) { 1432 *p++ = auzero0; 1433 *p++ = auzero1; 1434 n -= 2; 1435 } 1436 } 1437 } 1438 1439 int 1440 audio_silence_copyout(sc, n, uio) 1441 struct audio_softc *sc; 1442 int n; 1443 struct uio *uio; 1444 { 1445 int error; 1446 int k; 1447 u_char zerobuf[128]; 1448 1449 audio_fill_silence(&sc->sc_rparams, zerobuf, sizeof zerobuf); 1450 1451 error = 0; 1452 while (n > 0 && uio->uio_resid > 0 && !error) { 1453 k = min(n, min(uio->uio_resid, sizeof zerobuf)); 1454 error = uiomove(zerobuf, k, uio); 1455 n -= k; 1456 } 1457 return (error); 1458 } 1459 1460 int 1461 audio_write(dev, uio, ioflag) 1462 dev_t dev; 1463 struct uio *uio; 1464 int ioflag; 1465 { 1466 int unit = AUDIOUNIT(dev); 1467 struct audio_softc *sc = audio_cd.cd_devs[unit]; 1468 struct audio_ringbuffer *cb = &sc->sc_pr; 1469 u_char *inp, *einp; 1470 int saveerror, error, s, n, cc, used; 1471 1472 DPRINTFN(2, ("audio_write: sc=%p(unit=%d) count=%d used=%d(hi=%d)\n", sc, unit, 1473 uio->uio_resid, sc->sc_pr.used, sc->sc_pr.usedhigh)); 1474 1475 if (cb->mmapped) 1476 return EINVAL; 1477 1478 if (uio->uio_resid == 0) { 1479 sc->sc_eof++; 1480 return 0; 1481 } 1482 1483 /* 1484 * If half-duplex and currently recording, throw away data. 1485 */ 1486 if (!sc->sc_full_duplex && 1487 (sc->sc_mode & AUMODE_RECORD)) { 1488 uio->uio_offset += uio->uio_resid; 1489 uio->uio_resid = 0; 1490 DPRINTF(("audio_write: half-dpx read busy\n")); 1491 return (0); 1492 } 1493 1494 if (!(sc->sc_mode & AUMODE_PLAY_ALL) && sc->sc_playdrop > 0) { 1495 n = min(sc->sc_playdrop, uio->uio_resid); 1496 DPRINTF(("audio_write: playdrop %d\n", n)); 1497 uio->uio_offset += n; 1498 uio->uio_resid -= n; 1499 sc->sc_playdrop -= n; 1500 if (uio->uio_resid == 0) 1501 return 0; 1502 } 1503 1504 DPRINTFN(1, ("audio_write: sr=%ld, enc=%d, prec=%d, chan=%d, sw=%p, fact=%d\n", 1505 sc->sc_pparams.sample_rate, sc->sc_pparams.encoding, 1506 sc->sc_pparams.precision, sc->sc_pparams.channels, 1507 sc->sc_pparams.sw_code, sc->sc_pparams.factor)); 1508 1509 error = 0; 1510 while (uio->uio_resid > 0 && !error) { 1511 s = splaudio(); 1512 while (cb->used >= cb->usedhigh) { 1513 DPRINTFN(2, ("audio_write: sleep used=%d lowat=%d hiwat=%d\n", 1514 cb->used, cb->usedlow, cb->usedhigh)); 1515 if (ioflag & IO_NDELAY) { 1516 splx(s); 1517 return (EWOULDBLOCK); 1518 } 1519 error = audio_sleep(&sc->sc_wchan, "aud_wr"); 1520 if (sc->sc_dying) 1521 error = EIO; 1522 if (error) { 1523 splx(s); 1524 return error; 1525 } 1526 } 1527 used = cb->used; 1528 inp = cb->inp; 1529 cb->copying = 1; 1530 splx(s); 1531 cc = cb->usedhigh - used; /* maximum to write */ 1532 n = cb->end - inp; 1533 if (sc->sc_pparams.factor != 1) { 1534 /* Compensate for software coding expansion factor. */ 1535 n /= sc->sc_pparams.factor; 1536 cc /= sc->sc_pparams.factor; 1537 } 1538 if (n < cc) 1539 cc = n; /* don't write beyond end of buffer */ 1540 if (uio->uio_resid < cc) 1541 cc = uio->uio_resid; /* and no more than we have */ 1542 1543 #ifdef DIAGNOSTIC 1544 /* 1545 * This should never happen since the block size and and 1546 * block pointers are always nicely aligned. 1547 */ 1548 if (cc == 0) { 1549 printf("audio_write: cc == 0, swcode=%p, factor=%d\n", 1550 sc->sc_pparams.sw_code, sc->sc_pparams.factor); 1551 cb->copying = 0; 1552 return EINVAL; 1553 } 1554 #endif 1555 DPRINTFN(1, ("audio_write: uiomove cc=%d inp=%p, left=%d\n", 1556 cc, inp, uio->uio_resid)); 1557 n = uio->uio_resid; 1558 error = uiomove(inp, cc, uio); 1559 cc = n - uio->uio_resid; /* number of bytes actually moved */ 1560 #ifdef AUDIO_DEBUG 1561 if (error) 1562 printf("audio_write:(1) uiomove failed %d; cc=%d inp=%p\n", 1563 error, cc, inp); 1564 #endif 1565 /* 1566 * Continue even if uiomove() failed because we may have 1567 * gotten a partial block. 1568 */ 1569 1570 if (sc->sc_pparams.sw_code) { 1571 sc->sc_pparams.sw_code(sc->hw_hdl, inp, cc); 1572 /* Adjust count after the expansion. */ 1573 cc *= sc->sc_pparams.factor; 1574 DPRINTFN(1, ("audio_write: expanded cc=%d\n", cc)); 1575 } 1576 1577 einp = cb->inp + cc; 1578 if (einp >= cb->end) 1579 einp = cb->start; 1580 1581 s = splaudio(); 1582 /* 1583 * This is a very suboptimal way of keeping track of 1584 * silence in the buffer, but it is simple. 1585 */ 1586 sc->sc_sil_count = 0; 1587 1588 cb->inp = einp; 1589 cb->used += cc; 1590 /* If the interrupt routine wants the last block filled AND 1591 * the copy did not fill the last block completely it needs to 1592 * be padded. 1593 */ 1594 if (cb->needfill && 1595 (inp - cb->start) / cb->blksize == 1596 (einp - cb->start) / cb->blksize) { 1597 /* Figure out how many bytes there is to a block boundary. */ 1598 cc = cb->blksize - (einp - cb->start) % cb->blksize; 1599 DPRINTF(("audio_write: partial fill %d\n", cc)); 1600 } else 1601 cc = 0; 1602 cb->needfill = 0; 1603 cb->copying = 0; 1604 if (!sc->sc_pbus && !cb->pause) { 1605 saveerror = error; 1606 error = audiostartp(sc); 1607 if (saveerror != 0) { 1608 /* Report the first error that occurred. */ 1609 error = saveerror; 1610 } 1611 } 1612 splx(s); 1613 if (cc) { 1614 DPRINTFN(1, ("audio_write: fill %d\n", cc)); 1615 audio_fill_silence(&sc->sc_pparams, einp, cc); 1616 } 1617 } 1618 return (error); 1619 } 1620 1621 int 1622 audio_ioctl(dev, cmd, addr, flag, p) 1623 dev_t dev; 1624 u_long cmd; 1625 caddr_t addr; 1626 int flag; 1627 struct proc *p; 1628 { 1629 int unit = AUDIOUNIT(dev); 1630 struct audio_softc *sc = audio_cd.cd_devs[unit]; 1631 struct audio_hw_if *hw = sc->hw_if; 1632 struct audio_offset *ao; 1633 int error = 0, s, offs, fd; 1634 int rbus, pbus; 1635 1636 DPRINTF(("audio_ioctl(%d,'%c',%d)\n", 1637 IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd&0xff)); 1638 switch (cmd) { 1639 case FIONBIO: 1640 /* All handled in the upper FS layer. */ 1641 break; 1642 1643 case FIOASYNC: 1644 if (*(int *)addr) { 1645 if (sc->sc_async_audio) 1646 return (EBUSY); 1647 sc->sc_async_audio = p; 1648 DPRINTF(("audio_ioctl: FIOASYNC %p\n", p)); 1649 } else 1650 sc->sc_async_audio = 0; 1651 break; 1652 1653 case AUDIO_FLUSH: 1654 DPRINTF(("AUDIO_FLUSH\n")); 1655 rbus = sc->sc_rbus; 1656 pbus = sc->sc_pbus; 1657 audio_clear(sc); 1658 s = splaudio(); 1659 error = audio_initbufs(sc); 1660 if (error) { 1661 splx(s); 1662 return error; 1663 } 1664 if ((sc->sc_mode & AUMODE_PLAY) && !sc->sc_pbus && pbus) 1665 error = audiostartp(sc); 1666 if (!error && 1667 (sc->sc_mode & AUMODE_RECORD) && !sc->sc_rbus && rbus) 1668 error = audiostartr(sc); 1669 splx(s); 1670 break; 1671 1672 /* 1673 * Number of read (write) samples dropped. We don't know where or 1674 * when they were dropped. 1675 */ 1676 case AUDIO_RERROR: 1677 *(int *)addr = sc->sc_rr.drops; 1678 break; 1679 1680 case AUDIO_PERROR: 1681 *(int *)addr = sc->sc_pr.drops; 1682 break; 1683 1684 /* 1685 * Offsets into buffer. 1686 */ 1687 case AUDIO_GETIOFFS: 1688 s = splaudio(); 1689 /* figure out where next DMA will start */ 1690 ao = (struct audio_offset *)addr; 1691 ao->samples = sc->sc_rr.stamp; 1692 ao->deltablks = (sc->sc_rr.stamp - sc->sc_rr.stamp_last) / sc->sc_rr.blksize; 1693 sc->sc_rr.stamp_last = sc->sc_rr.stamp; 1694 ao->offset = sc->sc_rr.inp - sc->sc_rr.start; 1695 splx(s); 1696 break; 1697 1698 case AUDIO_GETOOFFS: 1699 s = splaudio(); 1700 /* figure out where next DMA will start */ 1701 ao = (struct audio_offset *)addr; 1702 offs = sc->sc_pr.outp - sc->sc_pr.start + sc->sc_pr.blksize; 1703 if (sc->sc_pr.start + offs >= sc->sc_pr.end) 1704 offs = 0; 1705 ao->samples = sc->sc_pr.stamp; 1706 ao->deltablks = (sc->sc_pr.stamp - sc->sc_pr.stamp_last) / sc->sc_pr.blksize; 1707 sc->sc_pr.stamp_last = sc->sc_pr.stamp; 1708 ao->offset = offs; 1709 splx(s); 1710 break; 1711 1712 /* 1713 * How many bytes will elapse until mike hears the first 1714 * sample of what we write next? 1715 */ 1716 case AUDIO_WSEEK: 1717 *(u_long *)addr = sc->sc_rr.used; 1718 break; 1719 1720 case AUDIO_SETINFO: 1721 DPRINTF(("AUDIO_SETINFO mode=0x%x\n", sc->sc_mode)); 1722 error = audiosetinfo(sc, (struct audio_info *)addr); 1723 break; 1724 1725 case AUDIO_GETINFO: 1726 DPRINTF(("AUDIO_GETINFO\n")); 1727 error = audiogetinfo(sc, (struct audio_info *)addr); 1728 break; 1729 1730 case AUDIO_DRAIN: 1731 DPRINTF(("AUDIO_DRAIN\n")); 1732 error = audio_drain(sc); 1733 if (!error && hw->drain) 1734 error = hw->drain(sc->hw_hdl); 1735 break; 1736 1737 case AUDIO_GETDEV: 1738 DPRINTF(("AUDIO_GETDEV\n")); 1739 error = hw->getdev(sc->hw_hdl, (audio_device_t *)addr); 1740 break; 1741 1742 case AUDIO_GETENC: 1743 DPRINTF(("AUDIO_GETENC\n")); 1744 /* Pass read/write info down to query_encoding */ 1745 ((struct audio_encoding *)addr)->flags = sc->sc_open; 1746 error = hw->query_encoding(sc->hw_hdl, (struct audio_encoding *)addr); 1747 break; 1748 1749 case AUDIO_GETFD: 1750 DPRINTF(("AUDIO_GETFD\n")); 1751 *(int *)addr = sc->sc_full_duplex; 1752 break; 1753 1754 case AUDIO_SETFD: 1755 DPRINTF(("AUDIO_SETFD\n")); 1756 fd = *(int *)addr; 1757 if (hw->get_props(sc->hw_hdl) & AUDIO_PROP_FULLDUPLEX) { 1758 if (hw->setfd) 1759 error = hw->setfd(sc->hw_hdl, fd); 1760 else 1761 error = 0; 1762 if (!error) 1763 sc->sc_full_duplex = fd; 1764 } else { 1765 if (fd) 1766 error = ENOTTY; 1767 else 1768 error = 0; 1769 } 1770 break; 1771 1772 case AUDIO_GETPROPS: 1773 DPRINTF(("AUDIO_GETPROPS\n")); 1774 *(int *)addr = hw->get_props(sc->hw_hdl); 1775 break; 1776 1777 default: 1778 DPRINTF(("audio_ioctl: unknown ioctl\n")); 1779 error = EINVAL; 1780 break; 1781 } 1782 DPRINTF(("audio_ioctl(%d,'%c',%d) result %d\n", 1783 IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd&0xff, error)); 1784 return (error); 1785 } 1786 1787 int 1788 audio_select(dev, rw, p) 1789 dev_t dev; 1790 int rw; 1791 struct proc *p; 1792 { 1793 int unit = AUDIOUNIT(dev); 1794 struct audio_softc *sc = audio_cd.cd_devs[unit]; 1795 int s = splaudio(); 1796 1797 DPRINTF(("audio_select: rw=0x%x mode=%d\n", rw, sc->sc_mode)); 1798 1799 switch (rw) { 1800 1801 case FREAD: 1802 if ((sc->sc_mode & AUMODE_PLAY) ? 1803 sc->sc_pr.stamp > sc->sc_wstamp : 1804 sc->sc_rr.used > sc->sc_rr.usedlow) { 1805 splx(s); 1806 return (1); 1807 } 1808 selrecord(p, &sc->sc_rsel); 1809 break; 1810 1811 case FWRITE: 1812 if (sc->sc_mode & AUMODE_RECORD || 1813 sc->sc_pr.used <= sc->sc_pr.usedlow) { 1814 splx(s); 1815 return (1); 1816 } 1817 selrecord(p, &sc->sc_wsel); 1818 break; 1819 } 1820 splx(s); 1821 return (0); 1822 } 1823 1824 paddr_t 1825 audio_mmap(dev, off, prot) 1826 dev_t dev; 1827 off_t off; 1828 int prot; 1829 { 1830 int s; 1831 int unit = AUDIOUNIT(dev); 1832 struct audio_softc *sc = audio_cd.cd_devs[unit]; 1833 struct audio_hw_if *hw = sc->hw_if; 1834 struct audio_ringbuffer *cb; 1835 1836 DPRINTF(("audio_mmap: off=%d, prot=%d\n", off, prot)); 1837 1838 if (!(hw->get_props(sc->hw_hdl) & AUDIO_PROP_MMAP) || !hw->mappage) 1839 return -1; 1840 #if 0 1841 /* XXX 1842 * The idea here was to use the protection to determine if 1843 * we are mapping the read or write buffer, but it fails. 1844 * The VM system is broken in (at least) two ways. 1845 * 1) If you map memory VM_PROT_WRITE you SIGSEGV 1846 * when writing to it, so VM_PROT_READ|VM_PROT_WRITE 1847 * has to be used for mmapping the play buffer. 1848 * 2) Even if calling mmap() with VM_PROT_READ|VM_PROT_WRITE 1849 * audio_mmap will get called at some point with VM_PROT_READ 1850 * only. 1851 * So, alas, we always map the play buffer for now. 1852 */ 1853 if (prot == (VM_PROT_READ|VM_PROT_WRITE) || 1854 prot == VM_PROT_WRITE) 1855 cb = &sc->sc_pr; 1856 else if (prot == VM_PROT_READ) 1857 cb = &sc->sc_rr; 1858 else 1859 return -1; 1860 #else 1861 cb = &sc->sc_pr; 1862 #endif 1863 1864 if ((u_int)off >= cb->bufsize) 1865 return -1; 1866 if (!cb->mmapped) { 1867 cb->mmapped = 1; 1868 if (cb == &sc->sc_pr) { 1869 audio_fill_silence(&sc->sc_pparams, cb->start, cb->bufsize); 1870 s = splaudio(); 1871 if (!sc->sc_pbus) 1872 (void)audiostartp(sc); 1873 splx(s); 1874 } else { 1875 s = splaudio(); 1876 if (!sc->sc_rbus) 1877 (void)audiostartr(sc); 1878 splx(s); 1879 } 1880 } 1881 1882 return hw->mappage(sc->hw_hdl, cb->start, off, prot); 1883 } 1884 1885 int 1886 audiostartr(sc) 1887 struct audio_softc *sc; 1888 { 1889 int error; 1890 1891 DPRINTF(("audiostartr: start=%p used=%d(hi=%d) mmapped=%d\n", 1892 sc->sc_rr.start, sc->sc_rr.used, sc->sc_rr.usedhigh, 1893 sc->sc_rr.mmapped)); 1894 1895 if (sc->hw_if->trigger_input) 1896 error = sc->hw_if->trigger_input(sc->hw_hdl, sc->sc_rr.start, 1897 sc->sc_rr.end, sc->sc_rr.blksize, 1898 audio_rint, (void *)sc, &sc->sc_rparams); 1899 else 1900 error = sc->hw_if->start_input(sc->hw_hdl, sc->sc_rr.start, 1901 sc->sc_rr.blksize, audio_rint, (void *)sc); 1902 if (error) { 1903 DPRINTF(("audiostartr failed: %d\n", error)); 1904 return error; 1905 } 1906 sc->sc_rbus = 1; 1907 return 0; 1908 } 1909 1910 int 1911 audiostartp(sc) 1912 struct audio_softc *sc; 1913 { 1914 int error; 1915 1916 DPRINTF(("audiostartp: start=%p used=%d(hi=%d) mmapped=%d\n", 1917 sc->sc_pr.start, sc->sc_pr.used, sc->sc_pr.usedhigh, 1918 sc->sc_pr.mmapped)); 1919 1920 if (!sc->sc_pr.mmapped && sc->sc_pr.used < sc->sc_pr.blksize) 1921 return 0; 1922 1923 if (sc->hw_if->trigger_output) 1924 error = sc->hw_if->trigger_output(sc->hw_hdl, sc->sc_pr.start, 1925 sc->sc_pr.end, sc->sc_pr.blksize, 1926 audio_pint, (void *)sc, &sc->sc_pparams); 1927 else 1928 error = sc->hw_if->start_output(sc->hw_hdl, sc->sc_pr.outp, 1929 sc->sc_pr.blksize, audio_pint, (void *)sc); 1930 if (error) { 1931 DPRINTF(("audiostartp failed: %d\n", error)); 1932 return error; 1933 } 1934 sc->sc_pbus = 1; 1935 return 0; 1936 } 1937 1938 /* 1939 * When the play interrupt routine finds that the write isn't keeping 1940 * the buffer filled it will insert silence in the buffer to make up 1941 * for this. The part of the buffer that is filled with silence 1942 * is kept track of in a very approximate way: it starts at sc_sil_start 1943 * and extends sc_sil_count bytes. If there is already silence in 1944 * the requested area nothing is done; so when the whole buffer is 1945 * silent nothing happens. When the writer starts again sc_sil_count 1946 * is set to 0. 1947 */ 1948 /* XXX 1949 * Putting silence into the output buffer should not really be done 1950 * at splaudio, but there is no softaudio level to do it at yet. 1951 */ 1952 static __inline void 1953 audio_pint_silence(sc, cb, inp, cc) 1954 struct audio_softc *sc; 1955 struct audio_ringbuffer *cb; 1956 u_char *inp; 1957 int cc; 1958 { 1959 u_char *s, *e, *p, *q; 1960 1961 if (sc->sc_sil_count > 0) { 1962 s = sc->sc_sil_start; /* start of silence */ 1963 e = s + sc->sc_sil_count; /* end of silence, may be beyond end */ 1964 p = inp; /* adjusted pointer to area to fill */ 1965 if (p < s) 1966 p += cb->end - cb->start; 1967 q = p+cc; 1968 /* Check if there is already silence. */ 1969 if (!(s <= p && p < e && 1970 s <= q && q <= e)) { 1971 if (s <= p) 1972 sc->sc_sil_count = max(sc->sc_sil_count, q-s); 1973 DPRINTFN(5, ("audio_pint_silence: fill cc=%d inp=%p, count=%d size=%d\n", 1974 cc, inp, sc->sc_sil_count, (int)(cb->end - cb->start))); 1975 audio_fill_silence(&sc->sc_pparams, inp, cc); 1976 } else { 1977 DPRINTFN(5, ("audio_pint_silence: already silent cc=%d inp=%p\n", cc, inp)); 1978 1979 } 1980 } else { 1981 sc->sc_sil_start = inp; 1982 sc->sc_sil_count = cc; 1983 DPRINTFN(5, ("audio_pint_silence: start fill %p %d\n", 1984 inp, cc)); 1985 audio_fill_silence(&sc->sc_pparams, inp, cc); 1986 } 1987 } 1988 1989 /* 1990 * Called from HW driver module on completion of dma output. 1991 * Start output of new block, wrap in ring buffer if needed. 1992 * If no more buffers to play, output zero instead. 1993 * Do a wakeup if necessary. 1994 */ 1995 void 1996 audio_pint(v) 1997 void *v; 1998 { 1999 struct audio_softc *sc = v; 2000 struct audio_hw_if *hw = sc->hw_if; 2001 struct audio_ringbuffer *cb = &sc->sc_pr; 2002 u_char *inp; 2003 int cc, ccr; 2004 int blksize; 2005 int error; 2006 2007 if (!sc->sc_open) 2008 return; /* ignore interrupt if not open */ 2009 2010 blksize = cb->blksize; 2011 2012 add_audio_randomness((long)cb); 2013 2014 cb->outp += blksize; 2015 if (cb->outp >= cb->end) 2016 cb->outp = cb->start; 2017 cb->stamp += blksize / sc->sc_pparams.factor; 2018 if (cb->mmapped) { 2019 DPRINTFN(5, ("audio_pint: mmapped outp=%p cc=%d inp=%p\n", 2020 cb->outp, blksize, cb->inp)); 2021 if (!hw->trigger_output) 2022 (void)hw->start_output(sc->hw_hdl, cb->outp, 2023 blksize, audio_pint, (void *)sc); 2024 return; 2025 } 2026 2027 #ifdef AUDIO_INTR_TIME 2028 { 2029 struct timeval tv; 2030 u_long t; 2031 microtime(&tv); 2032 t = tv.tv_usec + 1000000 * tv.tv_sec; 2033 if (sc->sc_pnintr) { 2034 long lastdelta, totdelta; 2035 lastdelta = t - sc->sc_plastintr - sc->sc_pblktime; 2036 if (lastdelta > sc->sc_pblktime / 3) { 2037 printf("audio: play interrupt(%d) off relative by %ld us (%lu)\n", 2038 sc->sc_pnintr, lastdelta, sc->sc_pblktime); 2039 } 2040 totdelta = t - sc->sc_pfirstintr - sc->sc_pblktime * sc->sc_pnintr; 2041 if (totdelta > sc->sc_pblktime) { 2042 printf("audio: play interrupt(%d) off absolute by %ld us (%lu) (LOST)\n", 2043 sc->sc_pnintr, totdelta, sc->sc_pblktime); 2044 sc->sc_pnintr++; /* avoid repeated messages */ 2045 } 2046 } else 2047 sc->sc_pfirstintr = t; 2048 sc->sc_plastintr = t; 2049 sc->sc_pnintr++; 2050 } 2051 #endif 2052 2053 cb->used -= blksize; 2054 if (cb->used < blksize) { 2055 /* we don't have a full block to use */ 2056 if (cb->copying) { 2057 /* writer is in progress, don't disturb */ 2058 cb->needfill = 1; 2059 DPRINTFN(1, ("audio_pint: copying in progress\n")); 2060 } else { 2061 inp = cb->inp; 2062 cc = blksize - (inp - cb->start) % blksize; 2063 ccr = cc / sc->sc_pparams.factor; 2064 if (cb->pause) 2065 cb->pdrops += ccr; 2066 else { 2067 cb->drops += ccr; 2068 sc->sc_playdrop += ccr; 2069 } 2070 audio_pint_silence(sc, cb, inp, cc); 2071 inp += cc; 2072 if (inp >= cb->end) 2073 inp = cb->start; 2074 cb->inp = inp; 2075 cb->used += cc; 2076 2077 /* Clear next block so we keep ahead of the DMA. */ 2078 if (cb->used + cc < cb->usedhigh) 2079 audio_pint_silence(sc, cb, inp, blksize); 2080 } 2081 } 2082 2083 DPRINTFN(5, ("audio_pint: outp=%p cc=%d\n", cb->outp, blksize)); 2084 if (!hw->trigger_output) { 2085 error = hw->start_output(sc->hw_hdl, cb->outp, blksize, 2086 audio_pint, (void *)sc); 2087 if (error) { 2088 /* XXX does this really help? */ 2089 DPRINTF(("audio_pint restart failed: %d\n", error)); 2090 audio_clear(sc); 2091 } 2092 } 2093 2094 DPRINTFN(2, ("audio_pint: mode=%d pause=%d used=%d lowat=%d\n", 2095 sc->sc_mode, cb->pause, cb->used, cb->usedlow)); 2096 if ((sc->sc_mode & AUMODE_PLAY) && !cb->pause) { 2097 if (cb->used <= cb->usedlow) { 2098 audio_wakeup(&sc->sc_wchan); 2099 selwakeup(&sc->sc_wsel); 2100 if (sc->sc_async_audio) { 2101 DPRINTFN(3, ("audio_pint: sending SIGIO %p\n", 2102 sc->sc_async_audio)); 2103 psignal(sc->sc_async_audio, SIGIO); 2104 } 2105 } 2106 } 2107 2108 /* Possible to return one or more "phantom blocks" now. */ 2109 if (!sc->sc_full_duplex && sc->sc_rchan) { 2110 audio_wakeup(&sc->sc_rchan); 2111 selwakeup(&sc->sc_rsel); 2112 if (sc->sc_async_audio) 2113 psignal(sc->sc_async_audio, SIGIO); 2114 } 2115 } 2116 2117 /* 2118 * Called from HW driver module on completion of dma input. 2119 * Mark it as input in the ring buffer (fiddle pointers). 2120 * Do a wakeup if necessary. 2121 */ 2122 void 2123 audio_rint(v) 2124 void *v; 2125 { 2126 struct audio_softc *sc = v; 2127 struct audio_hw_if *hw = sc->hw_if; 2128 struct audio_ringbuffer *cb = &sc->sc_rr; 2129 int blksize; 2130 int error; 2131 2132 if (!sc->sc_open) 2133 return; /* ignore interrupt if not open */ 2134 2135 add_audio_randomness((long)cb); 2136 2137 blksize = cb->blksize; 2138 2139 cb->inp += blksize; 2140 if (cb->inp >= cb->end) 2141 cb->inp = cb->start; 2142 cb->stamp += blksize; 2143 if (cb->mmapped) { 2144 DPRINTFN(2, ("audio_rint: mmapped inp=%p cc=%d\n", 2145 cb->inp, blksize)); 2146 if (!hw->trigger_input) 2147 (void)hw->start_input(sc->hw_hdl, cb->inp, blksize, 2148 audio_rint, (void *)sc); 2149 return; 2150 } 2151 2152 #ifdef AUDIO_INTR_TIME 2153 { 2154 struct timeval tv; 2155 u_long t; 2156 microtime(&tv); 2157 t = tv.tv_usec + 1000000 * tv.tv_sec; 2158 if (sc->sc_rnintr) { 2159 long lastdelta, totdelta; 2160 lastdelta = t - sc->sc_rlastintr - sc->sc_rblktime; 2161 if (lastdelta > sc->sc_rblktime / 5) { 2162 printf("audio: record interrupt(%d) off relative by %ld us (%lu)\n", 2163 sc->sc_rnintr, lastdelta, sc->sc_rblktime); 2164 } 2165 totdelta = t - sc->sc_rfirstintr - sc->sc_rblktime * sc->sc_rnintr; 2166 if (totdelta > sc->sc_rblktime / 2) { 2167 sc->sc_rnintr++; 2168 printf("audio: record interrupt(%d) off absolute by %ld us (%lu)\n", 2169 sc->sc_rnintr, totdelta, sc->sc_rblktime); 2170 sc->sc_rnintr++; /* avoid repeated messages */ 2171 } 2172 } else 2173 sc->sc_rfirstintr = t; 2174 sc->sc_rlastintr = t; 2175 sc->sc_rnintr++; 2176 } 2177 #endif 2178 2179 cb->used += blksize; 2180 if (cb->pause) { 2181 DPRINTFN(1, ("audio_rint: pdrops %lu\n", cb->pdrops)); 2182 cb->pdrops += blksize; 2183 cb->outp += blksize; 2184 cb->used -= blksize; 2185 } else if (cb->used + blksize >= cb->usedhigh && !cb->copying) { 2186 DPRINTFN(1, ("audio_rint: drops %lu\n", cb->drops)); 2187 cb->drops += blksize; 2188 cb->outp += blksize; 2189 cb->used -= blksize; 2190 } 2191 2192 DPRINTFN(2, ("audio_rint: inp=%p cc=%d used=%d\n", 2193 cb->inp, blksize, cb->used)); 2194 if (!hw->trigger_input) { 2195 error = hw->start_input(sc->hw_hdl, cb->inp, blksize, 2196 audio_rint, (void *)sc); 2197 if (error) { 2198 /* XXX does this really help? */ 2199 DPRINTF(("audio_rint: restart failed: %d\n", error)); 2200 audio_clear(sc); 2201 } 2202 } 2203 2204 audio_wakeup(&sc->sc_rchan); 2205 selwakeup(&sc->sc_rsel); 2206 if (sc->sc_async_audio) 2207 psignal(sc->sc_async_audio, SIGIO); 2208 } 2209 2210 int 2211 audio_check_params(p) 2212 struct audio_params *p; 2213 { 2214 if (p->encoding == AUDIO_ENCODING_PCM16) { 2215 if (p->precision == 8) 2216 p->encoding = AUDIO_ENCODING_ULINEAR; 2217 else 2218 p->encoding = AUDIO_ENCODING_SLINEAR; 2219 } else if (p->encoding == AUDIO_ENCODING_PCM8) { 2220 if (p->precision == 8) 2221 p->encoding = AUDIO_ENCODING_ULINEAR; 2222 else 2223 return EINVAL; 2224 } 2225 2226 if (p->encoding == AUDIO_ENCODING_SLINEAR) 2227 #if BYTE_ORDER == LITTLE_ENDIAN 2228 p->encoding = AUDIO_ENCODING_SLINEAR_LE; 2229 #else 2230 p->encoding = AUDIO_ENCODING_SLINEAR_BE; 2231 #endif 2232 if (p->encoding == AUDIO_ENCODING_ULINEAR) 2233 #if BYTE_ORDER == LITTLE_ENDIAN 2234 p->encoding = AUDIO_ENCODING_ULINEAR_LE; 2235 #else 2236 p->encoding = AUDIO_ENCODING_ULINEAR_BE; 2237 #endif 2238 2239 switch (p->encoding) { 2240 case AUDIO_ENCODING_ULAW: 2241 case AUDIO_ENCODING_ALAW: 2242 case AUDIO_ENCODING_ADPCM: 2243 if (p->precision != 8) 2244 return (EINVAL); 2245 break; 2246 case AUDIO_ENCODING_SLINEAR_LE: 2247 case AUDIO_ENCODING_SLINEAR_BE: 2248 case AUDIO_ENCODING_ULINEAR_LE: 2249 case AUDIO_ENCODING_ULINEAR_BE: 2250 if (p->precision != 8 && p->precision != 16) 2251 return (EINVAL); 2252 break; 2253 case AUDIO_ENCODING_MPEG_L1_STREAM: 2254 case AUDIO_ENCODING_MPEG_L1_PACKETS: 2255 case AUDIO_ENCODING_MPEG_L1_SYSTEM: 2256 case AUDIO_ENCODING_MPEG_L2_STREAM: 2257 case AUDIO_ENCODING_MPEG_L2_PACKETS: 2258 case AUDIO_ENCODING_MPEG_L2_SYSTEM: 2259 break; 2260 default: 2261 return (EINVAL); 2262 } 2263 2264 if (p->channels < 1 || p->channels > 8) /* sanity check # of channels */ 2265 return (EINVAL); 2266 2267 return (0); 2268 } 2269 2270 int 2271 au_set_lr_value(sc, ct, l, r) 2272 struct audio_softc *sc; 2273 mixer_ctrl_t *ct; 2274 int l, r; 2275 { 2276 ct->type = AUDIO_MIXER_VALUE; 2277 ct->un.value.num_channels = 2; 2278 ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l; 2279 ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r; 2280 if (sc->hw_if->set_port(sc->hw_hdl, ct) == 0) 2281 return 0; 2282 ct->un.value.num_channels = 1; 2283 ct->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2; 2284 return sc->hw_if->set_port(sc->hw_hdl, ct); 2285 } 2286 2287 int 2288 au_set_gain(sc, ports, gain, balance) 2289 struct audio_softc *sc; 2290 struct au_mixer_ports *ports; 2291 int gain; 2292 int balance; 2293 { 2294 mixer_ctrl_t ct; 2295 int i, error; 2296 int l, r; 2297 u_int mask; 2298 int nset; 2299 2300 if (balance == AUDIO_MID_BALANCE) { 2301 l = r = gain; 2302 } else if (balance < AUDIO_MID_BALANCE) { 2303 r = gain; 2304 l = (balance * gain) / AUDIO_MID_BALANCE; 2305 } else { 2306 l = gain; 2307 r = ((AUDIO_RIGHT_BALANCE - balance) * gain) 2308 / AUDIO_MID_BALANCE; 2309 } 2310 DPRINTF(("au_set_gain: gain=%d balance=%d, l=%d r=%d\n", 2311 gain, balance, l, r)); 2312 2313 if (ports->index == -1) { 2314 usemaster: 2315 if (ports->master == -1) 2316 return 0; /* just ignore it silently */ 2317 ct.dev = ports->master; 2318 error = au_set_lr_value(sc, &ct, l, r); 2319 } else { 2320 ct.dev = ports->index; 2321 if (ports->isenum) { 2322 ct.type = AUDIO_MIXER_ENUM; 2323 error = sc->hw_if->get_port(sc->hw_hdl, &ct); 2324 if (error) 2325 return error; 2326 for(i = 0; i < ports->nports; i++) { 2327 if (ports->misel[i] == ct.un.ord) { 2328 ct.dev = ports->miport[i]; 2329 if (ct.dev == -1 || 2330 au_set_lr_value(sc, &ct, l, r)) 2331 goto usemaster; 2332 else 2333 break; 2334 } 2335 } 2336 } else { 2337 ct.type = AUDIO_MIXER_SET; 2338 error = sc->hw_if->get_port(sc->hw_hdl, &ct); 2339 if (error) 2340 return error; 2341 mask = ct.un.mask; 2342 nset = 0; 2343 for(i = 0; i < ports->nports; i++) { 2344 if (ports->misel[i] & mask) { 2345 ct.dev = ports->miport[i]; 2346 if (ct.dev != -1 && 2347 au_set_lr_value(sc, &ct, l, r) == 0) 2348 nset++; 2349 } 2350 } 2351 if (nset == 0) 2352 goto usemaster; 2353 } 2354 } 2355 if (!error) 2356 mixer_signal(sc); 2357 return error; 2358 } 2359 2360 int 2361 au_get_lr_value(sc, ct, l, r) 2362 struct audio_softc *sc; 2363 mixer_ctrl_t *ct; 2364 int *l, *r; 2365 { 2366 int error; 2367 2368 ct->un.value.num_channels = 2; 2369 if (sc->hw_if->get_port(sc->hw_hdl, ct) == 0) { 2370 *l = ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT]; 2371 *r = ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]; 2372 } else { 2373 ct->un.value.num_channels = 1; 2374 error = sc->hw_if->get_port(sc->hw_hdl, ct); 2375 if (error) 2376 return error; 2377 *r = *l = ct->un.value.level[AUDIO_MIXER_LEVEL_MONO]; 2378 } 2379 return 0; 2380 } 2381 2382 void 2383 au_get_gain(sc, ports, pgain, pbalance) 2384 struct audio_softc *sc; 2385 struct au_mixer_ports *ports; 2386 u_int *pgain; 2387 u_char *pbalance; 2388 { 2389 mixer_ctrl_t ct; 2390 int i, l, r, n; 2391 int lgain = AUDIO_MAX_GAIN/2, rgain = AUDIO_MAX_GAIN/2; 2392 2393 if (ports->index == -1) { 2394 usemaster: 2395 if (ports->master == -1) 2396 goto bad; 2397 ct.dev = ports->master; 2398 ct.type = AUDIO_MIXER_VALUE; 2399 if (au_get_lr_value(sc, &ct, &lgain, &rgain)) 2400 goto bad; 2401 } else { 2402 ct.dev = ports->index; 2403 if (ports->isenum) { 2404 ct.type = AUDIO_MIXER_ENUM; 2405 if (sc->hw_if->get_port(sc->hw_hdl, &ct)) 2406 goto bad; 2407 ct.type = AUDIO_MIXER_VALUE; 2408 for(i = 0; i < ports->nports; i++) { 2409 if (ports->misel[i] == ct.un.ord) { 2410 ct.dev = ports->miport[i]; 2411 if (ct.dev == -1 || 2412 au_get_lr_value(sc, &ct, 2413 &lgain, &rgain)) 2414 goto usemaster; 2415 else 2416 break; 2417 } 2418 } 2419 } else { 2420 ct.type = AUDIO_MIXER_SET; 2421 if (sc->hw_if->get_port(sc->hw_hdl, &ct)) 2422 goto bad; 2423 ct.type = AUDIO_MIXER_VALUE; 2424 lgain = rgain = n = 0; 2425 for(i = 0; i < ports->nports; i++) { 2426 if (ports->misel[i] & ct.un.mask) { 2427 ct.dev = ports->miport[i]; 2428 if (ct.dev == -1 || 2429 au_get_lr_value(sc, &ct, &l, &r)) 2430 goto usemaster; 2431 else { 2432 lgain += l; 2433 rgain += r; 2434 n++; 2435 } 2436 } 2437 } 2438 if (n != 0) { 2439 lgain /= n; 2440 rgain /= n; 2441 } 2442 } 2443 } 2444 bad: 2445 if (lgain == rgain) { /* handles lgain==rgain==0 */ 2446 *pgain = lgain; 2447 *pbalance = AUDIO_MID_BALANCE; 2448 } else if (lgain < rgain) { 2449 *pgain = rgain; 2450 *pbalance = (AUDIO_MID_BALANCE * lgain) / rgain; 2451 } else /* lgain > rgain */ { 2452 *pgain = lgain; 2453 *pbalance = AUDIO_RIGHT_BALANCE - 2454 (AUDIO_MID_BALANCE * rgain) / lgain; 2455 } 2456 } 2457 2458 int 2459 au_set_port(sc, ports, port) 2460 struct audio_softc *sc; 2461 struct au_mixer_ports *ports; 2462 u_int port; 2463 { 2464 mixer_ctrl_t ct; 2465 int i, error; 2466 2467 if (port == 0 && ports->allports == 0) 2468 return 0; /* allow this special case */ 2469 2470 if (ports->index == -1) 2471 return EINVAL; 2472 ct.dev = ports->index; 2473 if (ports->isenum) { 2474 if (port & (port-1)) 2475 return EINVAL; /* Only one port allowed */ 2476 ct.type = AUDIO_MIXER_ENUM; 2477 error = EINVAL; 2478 for(i = 0; i < ports->nports; i++) 2479 if (ports->aumask[i] == port) { 2480 ct.un.ord = ports->misel[i]; 2481 error = sc->hw_if->set_port(sc->hw_hdl, &ct); 2482 break; 2483 } 2484 } else { 2485 ct.type = AUDIO_MIXER_SET; 2486 ct.un.mask = 0; 2487 for(i = 0; i < ports->nports; i++) 2488 if (ports->aumask[i] & port) 2489 ct.un.mask |= ports->misel[i]; 2490 if (port != 0 && ct.un.mask == 0) 2491 error = EINVAL; 2492 else 2493 error = sc->hw_if->set_port(sc->hw_hdl, &ct); 2494 } 2495 if (!error) 2496 mixer_signal(sc); 2497 return error; 2498 } 2499 2500 int 2501 au_get_port(sc, ports) 2502 struct audio_softc *sc; 2503 struct au_mixer_ports *ports; 2504 { 2505 mixer_ctrl_t ct; 2506 int i, aumask; 2507 2508 if (ports->index == -1) 2509 return 0; 2510 ct.dev = ports->index; 2511 ct.type = ports->isenum ? AUDIO_MIXER_ENUM : AUDIO_MIXER_SET; 2512 if (sc->hw_if->get_port(sc->hw_hdl, &ct)) 2513 return 0; 2514 aumask = 0; 2515 if (ports->isenum) { 2516 for(i = 0; i < ports->nports; i++) 2517 if (ct.un.ord == ports->misel[i]) 2518 aumask = ports->aumask[i]; 2519 } else { 2520 for(i = 0; i < ports->nports; i++) 2521 if (ct.un.mask & ports->misel[i]) 2522 aumask |= ports->aumask[i]; 2523 } 2524 return aumask; 2525 } 2526 2527 int 2528 audiosetinfo(sc, ai) 2529 struct audio_softc *sc; 2530 struct audio_info *ai; 2531 { 2532 struct audio_prinfo *r = &ai->record, *p = &ai->play; 2533 int cleared; 2534 int s, setmode, modechange = 0; 2535 int error; 2536 struct audio_hw_if *hw = sc->hw_if; 2537 struct audio_params pp, rp; 2538 int np, nr; 2539 unsigned int blks; 2540 int oldpblksize, oldrblksize; 2541 int rbus, pbus; 2542 u_int gain; 2543 u_char balance; 2544 2545 if (hw == 0) /* HW has not attached */ 2546 return(ENXIO); 2547 2548 rbus = sc->sc_rbus; 2549 pbus = sc->sc_pbus; 2550 error = 0; 2551 cleared = 0; 2552 2553 pp = sc->sc_pparams; /* Temporary encoding storage in */ 2554 rp = sc->sc_rparams; /* case setting the modes fails. */ 2555 nr = np = 0; 2556 2557 if (p->sample_rate != ~0) { 2558 pp.sample_rate = p->sample_rate; 2559 np++; 2560 } 2561 if (r->sample_rate != ~0) { 2562 rp.sample_rate = r->sample_rate; 2563 nr++; 2564 } 2565 if (p->encoding != ~0) { 2566 pp.encoding = p->encoding; 2567 np++; 2568 } 2569 if (r->encoding != ~0) { 2570 rp.encoding = r->encoding; 2571 nr++; 2572 } 2573 if (p->precision != ~0) { 2574 pp.precision = p->precision; 2575 np++; 2576 } 2577 if (r->precision != ~0) { 2578 rp.precision = r->precision; 2579 nr++; 2580 } 2581 if (p->channels != ~0) { 2582 pp.channels = p->channels; 2583 np++; 2584 } 2585 if (r->channels != ~0) { 2586 rp.channels = r->channels; 2587 nr++; 2588 } 2589 #ifdef AUDIO_DEBUG 2590 if (audiodebug && nr) 2591 audio_print_params("Setting record params", &rp); 2592 if (audiodebug && np) 2593 audio_print_params("Setting play params", &pp); 2594 #endif 2595 if (nr && (error = audio_check_params(&rp))) 2596 return error; 2597 if (np && (error = audio_check_params(&pp))) 2598 return error; 2599 setmode = 0; 2600 if (nr) { 2601 if (!cleared) 2602 audio_clear(sc); 2603 modechange = cleared = 1; 2604 rp.sw_code = 0; 2605 rp.factor = 1; 2606 setmode |= AUMODE_RECORD; 2607 } 2608 if (np) { 2609 if (!cleared) 2610 audio_clear(sc); 2611 modechange = cleared = 1; 2612 pp.sw_code = 0; 2613 pp.factor = 1; 2614 setmode |= AUMODE_PLAY; 2615 } 2616 2617 if (ai->mode != ~0) { 2618 if (!cleared) 2619 audio_clear(sc); 2620 modechange = cleared = 1; 2621 sc->sc_mode = ai->mode; 2622 if (sc->sc_mode & AUMODE_PLAY_ALL) 2623 sc->sc_mode |= AUMODE_PLAY; 2624 if ((sc->sc_mode & AUMODE_PLAY) && !sc->sc_full_duplex) 2625 /* Play takes precedence */ 2626 sc->sc_mode &= ~AUMODE_RECORD; 2627 } 2628 2629 if (modechange) { 2630 int indep = hw->get_props(sc->hw_hdl) & AUDIO_PROP_INDEPENDENT; 2631 if (!indep) { 2632 if (setmode == AUMODE_RECORD) 2633 pp = rp; 2634 else if (setmode == AUMODE_PLAY) 2635 rp = pp; 2636 } 2637 error = hw->set_params(sc->hw_hdl, setmode, 2638 sc->sc_mode & (AUMODE_PLAY | AUMODE_RECORD), &pp, &rp); 2639 if (error) 2640 return (error); 2641 if (!indep) { 2642 if (setmode == AUMODE_RECORD) { 2643 pp.sample_rate = rp.sample_rate; 2644 pp.encoding = rp.encoding; 2645 pp.channels = rp.channels; 2646 pp.precision = rp.precision; 2647 } else if (setmode == AUMODE_PLAY) { 2648 rp.sample_rate = pp.sample_rate; 2649 rp.encoding = pp.encoding; 2650 rp.channels = pp.channels; 2651 rp.precision = pp.precision; 2652 } 2653 } 2654 sc->sc_rparams = rp; 2655 sc->sc_pparams = pp; 2656 } 2657 2658 oldpblksize = sc->sc_pr.blksize; 2659 oldrblksize = sc->sc_rr.blksize; 2660 /* Play params can affect the record params, so recalculate blksize. */ 2661 if (nr || np) { 2662 audio_calc_blksize(sc, AUMODE_RECORD); 2663 audio_calc_blksize(sc, AUMODE_PLAY); 2664 } 2665 #ifdef AUDIO_DEBUG 2666 if (audiodebug > 1 && nr) 2667 audio_print_params("After setting record params", &sc->sc_rparams); 2668 if (audiodebug > 1 && np) 2669 audio_print_params("After setting play params", &sc->sc_pparams); 2670 #endif 2671 2672 if (p->port != ~0) { 2673 if (!cleared) 2674 audio_clear(sc); 2675 cleared = 1; 2676 2677 error = au_set_port(sc, &sc->sc_outports, p->port); 2678 if (error) 2679 return(error); 2680 } 2681 if (r->port != ~0) { 2682 if (!cleared) 2683 audio_clear(sc); 2684 cleared = 1; 2685 2686 error = au_set_port(sc, &sc->sc_inports, r->port); 2687 if (error) 2688 return(error); 2689 } 2690 if (p->gain != ~0) { 2691 au_get_gain(sc, &sc->sc_outports, &gain, &balance); 2692 error = au_set_gain(sc, &sc->sc_outports, p->gain, balance); 2693 if (error) 2694 return(error); 2695 } 2696 if (r->gain != ~0) { 2697 au_get_gain(sc, &sc->sc_inports, &gain, &balance); 2698 error = au_set_gain(sc, &sc->sc_inports, r->gain, balance); 2699 if (error) 2700 return(error); 2701 } 2702 2703 if (p->balance != (u_char)~0) { 2704 au_get_gain(sc, &sc->sc_outports, &gain, &balance); 2705 error = au_set_gain(sc, &sc->sc_outports, gain, p->balance); 2706 if (error) 2707 return(error); 2708 } 2709 if (r->balance != (u_char)~0) { 2710 au_get_gain(sc, &sc->sc_inports, &gain, &balance); 2711 error = au_set_gain(sc, &sc->sc_inports, gain, r->balance); 2712 if (error) 2713 return(error); 2714 } 2715 2716 if (ai->monitor_gain != ~0 && 2717 sc->sc_monitor_port != -1) { 2718 mixer_ctrl_t ct; 2719 2720 ct.dev = sc->sc_monitor_port; 2721 ct.type = AUDIO_MIXER_VALUE; 2722 ct.un.value.num_channels = 1; 2723 ct.un.value.level[AUDIO_MIXER_LEVEL_MONO] = ai->monitor_gain; 2724 error = sc->hw_if->get_port(sc->hw_hdl, &ct); 2725 if (error) 2726 return(error); 2727 } 2728 2729 if (p->pause != (u_char)~0) { 2730 sc->sc_pr.pause = p->pause; 2731 if (!p->pause && !sc->sc_pbus && (sc->sc_mode & AUMODE_PLAY)) { 2732 s = splaudio(); 2733 error = audiostartp(sc); 2734 splx(s); 2735 if (error) 2736 return error; 2737 } 2738 } 2739 if (r->pause != (u_char)~0) { 2740 sc->sc_rr.pause = r->pause; 2741 if (!r->pause && !sc->sc_rbus && (sc->sc_mode & AUMODE_RECORD)) { 2742 s = splaudio(); 2743 error = audiostartr(sc); 2744 splx(s); 2745 if (error) 2746 return error; 2747 } 2748 } 2749 2750 if (ai->blocksize != ~0) { 2751 /* Block size specified explicitly. */ 2752 if (!cleared) 2753 audio_clear(sc); 2754 cleared = 1; 2755 2756 if (ai->blocksize == 0) { 2757 audio_calc_blksize(sc, AUMODE_RECORD); 2758 audio_calc_blksize(sc, AUMODE_PLAY); 2759 sc->sc_blkset = 0; 2760 } else { 2761 int bs = ai->blocksize; 2762 if (hw->round_blocksize) 2763 bs = hw->round_blocksize(sc->hw_hdl, bs); 2764 sc->sc_pr.blksize = sc->sc_rr.blksize = bs; 2765 sc->sc_blkset = 1; 2766 } 2767 } 2768 2769 if (ai->mode != ~0) { 2770 if (sc->sc_mode & AUMODE_PLAY) 2771 audio_init_play(sc); 2772 if (sc->sc_mode & AUMODE_RECORD) 2773 audio_init_record(sc); 2774 } 2775 2776 if (hw->commit_settings) { 2777 error = hw->commit_settings(sc->hw_hdl); 2778 if (error) 2779 return (error); 2780 } 2781 2782 if (cleared) { 2783 s = splaudio(); 2784 error = audio_initbufs(sc); 2785 if (error) goto err; 2786 if (sc->sc_pr.blksize != oldpblksize || 2787 sc->sc_rr.blksize != oldrblksize) 2788 audio_calcwater(sc); 2789 if ((sc->sc_mode & AUMODE_PLAY) && 2790 pbus && !sc->sc_pbus) 2791 error = audiostartp(sc); 2792 if (!error && 2793 (sc->sc_mode & AUMODE_RECORD) && 2794 rbus && !sc->sc_rbus) 2795 error = audiostartr(sc); 2796 err: 2797 splx(s); 2798 if (error) 2799 return error; 2800 } 2801 2802 /* Change water marks after initializing the buffers. */ 2803 if (ai->hiwat != ~0) { 2804 blks = ai->hiwat; 2805 if (blks > sc->sc_pr.maxblks) 2806 blks = sc->sc_pr.maxblks; 2807 if (blks < 2) 2808 blks = 2; 2809 sc->sc_pr.usedhigh = blks * sc->sc_pr.blksize; 2810 } 2811 if (ai->lowat != ~0) { 2812 blks = ai->lowat; 2813 if (blks > sc->sc_pr.maxblks - 1) 2814 blks = sc->sc_pr.maxblks - 1; 2815 sc->sc_pr.usedlow = blks * sc->sc_pr.blksize; 2816 } 2817 if (ai->hiwat != ~0 || ai->lowat != ~0) { 2818 if (sc->sc_pr.usedlow > sc->sc_pr.usedhigh - sc->sc_pr.blksize) 2819 sc->sc_pr.usedlow = sc->sc_pr.usedhigh - sc->sc_pr.blksize; 2820 } 2821 2822 return (0); 2823 } 2824 2825 int 2826 audiogetinfo(sc, ai) 2827 struct audio_softc *sc; 2828 struct audio_info *ai; 2829 { 2830 struct audio_prinfo *r = &ai->record, *p = &ai->play; 2831 struct audio_hw_if *hw = sc->hw_if; 2832 2833 if (hw == 0) /* HW has not attached */ 2834 return(ENXIO); 2835 2836 p->sample_rate = sc->sc_pparams.sample_rate; 2837 r->sample_rate = sc->sc_rparams.sample_rate; 2838 p->channels = sc->sc_pparams.channels; 2839 r->channels = sc->sc_rparams.channels; 2840 p->precision = sc->sc_pparams.precision; 2841 r->precision = sc->sc_rparams.precision; 2842 p->encoding = sc->sc_pparams.encoding; 2843 r->encoding = sc->sc_rparams.encoding; 2844 2845 r->port = au_get_port(sc, &sc->sc_inports); 2846 p->port = au_get_port(sc, &sc->sc_outports); 2847 2848 r->avail_ports = sc->sc_inports.allports; 2849 p->avail_ports = sc->sc_outports.allports; 2850 2851 au_get_gain(sc, &sc->sc_inports, &r->gain, &r->balance); 2852 au_get_gain(sc, &sc->sc_outports, &p->gain, &p->balance); 2853 2854 if (sc->sc_monitor_port != -1) { 2855 mixer_ctrl_t ct; 2856 2857 ct.dev = sc->sc_monitor_port; 2858 ct.type = AUDIO_MIXER_VALUE; 2859 ct.un.value.num_channels = 1; 2860 if (sc->hw_if->get_port(sc->hw_hdl, &ct)) 2861 ai->monitor_gain = 0; 2862 else 2863 ai->monitor_gain = 2864 ct.un.value.level[AUDIO_MIXER_LEVEL_MONO]; 2865 } else 2866 ai->monitor_gain = 0; 2867 2868 p->seek = sc->sc_pr.used; 2869 r->seek = sc->sc_rr.used; 2870 2871 p->samples = sc->sc_pr.stamp - sc->sc_pr.drops; 2872 r->samples = sc->sc_rr.stamp - sc->sc_rr.drops; 2873 2874 p->eof = sc->sc_eof; 2875 r->eof = 0; 2876 2877 p->pause = sc->sc_pr.pause; 2878 r->pause = sc->sc_rr.pause; 2879 2880 p->error = sc->sc_pr.drops != 0; 2881 r->error = sc->sc_rr.drops != 0; 2882 2883 p->waiting = r->waiting = 0; /* open never hangs */ 2884 2885 p->open = (sc->sc_open & AUOPEN_WRITE) != 0; 2886 r->open = (sc->sc_open & AUOPEN_READ) != 0; 2887 2888 p->active = sc->sc_pbus; 2889 r->active = sc->sc_rbus; 2890 2891 p->buffer_size = sc->sc_pr.bufsize; 2892 r->buffer_size = sc->sc_rr.bufsize; 2893 2894 ai->blocksize = sc->sc_pr.blksize; 2895 ai->hiwat = sc->sc_pr.usedhigh / sc->sc_pr.blksize; 2896 ai->lowat = sc->sc_pr.usedlow / sc->sc_pr.blksize; 2897 ai->mode = sc->sc_mode; 2898 2899 return (0); 2900 } 2901 2902 /* 2903 * Mixer driver 2904 */ 2905 int 2906 mixer_open(dev, sc, flags, ifmt, p) 2907 dev_t dev; 2908 struct audio_softc *sc; 2909 int flags, ifmt; 2910 struct proc *p; 2911 { 2912 DPRINTF(("mixer_open: dev=0x%x flags=0x%x sc=%p\n", dev, flags, sc)); 2913 2914 return (0); 2915 } 2916 2917 /* 2918 * Remove a process from those to be signalled on mixer activity. 2919 */ 2920 static void 2921 mixer_remove(sc, p) 2922 struct audio_softc *sc; 2923 struct proc *p; 2924 { 2925 struct mixer_asyncs **pm, *m; 2926 2927 for(pm = &sc->sc_async_mixer; *pm; pm = &(*pm)->next) { 2928 if ((*pm)->proc == p) { 2929 m = *pm; 2930 *pm = m->next; 2931 free(m, M_DEVBUF); 2932 return; 2933 } 2934 } 2935 } 2936 2937 /* 2938 * Signal all processes waitinf for the mixer. 2939 */ 2940 static void 2941 mixer_signal(sc) 2942 struct audio_softc *sc; 2943 { 2944 struct mixer_asyncs *m; 2945 2946 for(m = sc->sc_async_mixer; m; m = m->next) 2947 psignal(m->proc, SIGIO); 2948 } 2949 2950 /* 2951 * Close a mixer device 2952 */ 2953 /* ARGSUSED */ 2954 int 2955 mixer_close(dev, flags, ifmt, p) 2956 dev_t dev; 2957 int flags, ifmt; 2958 struct proc *p; 2959 { 2960 int unit = AUDIOUNIT(dev); 2961 struct audio_softc *sc = audio_cd.cd_devs[unit]; 2962 2963 DPRINTF(("mixer_close: unit %d\n", AUDIOUNIT(dev))); 2964 2965 mixer_remove(sc, p); 2966 2967 return (0); 2968 } 2969 2970 int 2971 mixer_ioctl(dev, cmd, addr, flag, p) 2972 dev_t dev; 2973 u_long cmd; 2974 caddr_t addr; 2975 int flag; 2976 struct proc *p; 2977 { 2978 int unit = AUDIOUNIT(dev); 2979 struct audio_softc *sc = audio_cd.cd_devs[unit]; 2980 struct audio_hw_if *hw = sc->hw_if; 2981 int error = EINVAL; 2982 2983 DPRINTF(("mixer_ioctl(%d,'%c',%d)\n", 2984 IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd&0xff)); 2985 2986 switch (cmd) { 2987 case FIOASYNC: 2988 mixer_remove(sc, p); /* remove old entry */ 2989 if (*(int *)addr) { 2990 struct mixer_asyncs *ma; 2991 ma = malloc(sizeof (struct mixer_asyncs), 2992 M_DEVBUF, M_WAITOK); 2993 ma->next = sc->sc_async_mixer; 2994 ma->proc = p; 2995 sc->sc_async_mixer = ma; 2996 } 2997 error = 0; 2998 break; 2999 3000 case AUDIO_GETDEV: 3001 DPRINTF(("AUDIO_GETDEV\n")); 3002 error = hw->getdev(sc->hw_hdl, (audio_device_t *)addr); 3003 break; 3004 3005 case AUDIO_MIXER_DEVINFO: 3006 DPRINTF(("AUDIO_MIXER_DEVINFO\n")); 3007 ((mixer_devinfo_t *)addr)->un.v.delta = 0; /* default */ 3008 error = hw->query_devinfo(sc->hw_hdl, (mixer_devinfo_t *)addr); 3009 break; 3010 3011 case AUDIO_MIXER_READ: 3012 DPRINTF(("AUDIO_MIXER_READ\n")); 3013 error = hw->get_port(sc->hw_hdl, (mixer_ctrl_t *)addr); 3014 break; 3015 3016 case AUDIO_MIXER_WRITE: 3017 DPRINTF(("AUDIO_MIXER_WRITE\n")); 3018 error = hw->set_port(sc->hw_hdl, (mixer_ctrl_t *)addr); 3019 if (!error && hw->commit_settings) 3020 error = hw->commit_settings(sc->hw_hdl); 3021 if (!error) 3022 mixer_signal(sc); 3023 break; 3024 3025 default: 3026 error = EINVAL; 3027 break; 3028 } 3029 DPRINTF(("mixer_ioctl(%d,'%c',%d) result %d\n", 3030 IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd&0xff, error)); 3031 return (error); 3032 } 3033 #endif 3034