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