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