1 /* $OpenBSD: audio.c,v 1.198 2022/03/21 19:22:39 miod Exp $ */ 2 /* 3 * Copyright (c) 2015 Alexandre Ratchov <alex@caoua.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 #include <sys/param.h> 18 #include <sys/fcntl.h> 19 #include <sys/systm.h> 20 #include <sys/ioctl.h> 21 #include <sys/conf.h> 22 #include <sys/poll.h> 23 #include <sys/kernel.h> 24 #include <sys/task.h> 25 #include <sys/vnode.h> 26 #include <sys/malloc.h> 27 #include <sys/device.h> 28 #include <sys/audioio.h> 29 #include <dev/audio_if.h> 30 #include <dev/mulaw.h> 31 #include "audio.h" 32 #include "wskbd.h" 33 34 #ifdef AUDIO_DEBUG 35 #define DPRINTF(...) \ 36 do { \ 37 if (audio_debug) \ 38 printf(__VA_ARGS__); \ 39 } while(0) 40 #define DPRINTFN(n, ...) \ 41 do { \ 42 if (audio_debug > (n)) \ 43 printf(__VA_ARGS__); \ 44 } while(0) 45 #else 46 #define DPRINTF(...) do {} while(0) 47 #define DPRINTFN(n, ...) do {} while(0) 48 #endif 49 50 #define IPL_SOFTAUDIO IPL_SOFTNET 51 52 #define DEVNAME(sc) ((sc)->dev.dv_xname) 53 #define AUDIO_UNIT(n) (minor(n) & 0x0f) 54 #define AUDIO_DEV(n) (minor(n) & 0xf0) 55 #define AUDIO_DEV_AUDIO 0 /* minor of /dev/audio0 */ 56 #define AUDIO_DEV_AUDIOCTL 0xc0 /* minor of /dev/audioctl */ 57 #define AUDIO_BUFSZ 65536 /* buffer size in bytes */ 58 59 /* 60 * mixer entries added by the audio(4) layer 61 */ 62 #define MIXER_RECORD 0 /* record class */ 63 #define MIXER_RECORD_ENABLE 1 /* record.enable control */ 64 #define MIXER_RECORD_ENABLE_OFF 0 /* record.enable=off value */ 65 #define MIXER_RECORD_ENABLE_ON 1 /* record.enable=on value */ 66 #define MIXER_RECORD_ENABLE_SYSCTL 2 /* record.enable=sysctl val */ 67 68 /* 69 * dma buffer 70 */ 71 struct audio_buf { 72 unsigned char *data; /* DMA memory block */ 73 size_t datalen; /* size of DMA memory block */ 74 size_t len; /* size of DMA FIFO */ 75 size_t start; /* first byte used in the FIFO */ 76 size_t used; /* bytes used in the FIFO */ 77 size_t blksz; /* DMA block size */ 78 unsigned int nblks; /* number of blocks */ 79 struct selinfo sel; /* to record & wakeup poll(2) */ 80 void *softintr; /* context to call selwakeup() */ 81 unsigned int pos; /* bytes transferred */ 82 unsigned int xrun; /* bytes lost by xruns */ 83 int blocking; /* read/write blocking */ 84 }; 85 86 #if NWSKBD > 0 87 struct wskbd_vol 88 { 89 int val; /* index of the value control */ 90 int mute; /* index of the mute control */ 91 int step; /* increment/decrement step */ 92 int nch; /* channels in the value control */ 93 int val_pending; /* pending change of val */ 94 int mute_pending; /* pending change of mute */ 95 #define WSKBD_MUTE_TOGGLE 1 96 #define WSKBD_MUTE_DISABLE 2 97 #define WSKBD_MUTE_ENABLE 3 98 }; 99 100 int wskbd_set_mixervolume_unit(int, long, long); 101 #endif 102 103 /* 104 * event indicating that a control was changed 105 */ 106 struct mixer_ev { 107 struct mixer_ev *next; 108 int pending; 109 }; 110 111 /* 112 * device structure 113 */ 114 struct audio_softc { 115 struct device dev; 116 const struct audio_hw_if *ops; /* driver funcs */ 117 void *cookie; /* wskbd cookie */ 118 void *arg; /* first arg to driver funcs */ 119 int mode; /* bitmask of AUMODE_* */ 120 int quiesce; /* device suspended */ 121 struct audio_buf play, rec; 122 unsigned int sw_enc; /* user exposed AUDIO_ENCODING_* */ 123 unsigned int hw_enc; /* hardware AUDIO_ENCODING_* */ 124 unsigned int bits; /* bits per sample */ 125 unsigned int bps; /* bytes-per-sample */ 126 unsigned int msb; /* sample are MSB aligned */ 127 unsigned int rate; /* rate in Hz */ 128 unsigned int round; /* block size in frames */ 129 unsigned int pchan, rchan; /* number of channels */ 130 unsigned char silence[4]; /* a sample of silence */ 131 int pause; /* not trying to start DMA */ 132 int active; /* DMA in process */ 133 int offs; /* offset between play & rec dir */ 134 void (*conv_enc)(unsigned char *, int); /* encode to native */ 135 void (*conv_dec)(unsigned char *, int); /* decode to user */ 136 struct mixer_ctrl *mix_ents; /* mixer state for suspend/resume */ 137 int mix_nent; /* size of mixer state */ 138 int mix_isopen; /* mixer open for reading */ 139 int mix_blocking; /* read() blocking */ 140 struct selinfo mix_sel; /* wakeup poll(2) */ 141 struct mixer_ev *mix_evbuf; /* per mixer-control event */ 142 struct mixer_ev *mix_pending; /* list of changed controls */ 143 void *mix_softintr; /* context to call selwakeup() */ 144 #if NWSKBD > 0 145 struct wskbd_vol spkr, mic; 146 struct task wskbd_task; 147 #endif 148 int record_enable; /* mixer record.enable value */ 149 }; 150 151 int audio_match(struct device *, void *, void *); 152 void audio_attach(struct device *, struct device *, void *); 153 int audio_activate(struct device *, int); 154 int audio_detach(struct device *, int); 155 void audio_pintr(void *); 156 void audio_rintr(void *); 157 #if NWSKBD > 0 158 void wskbd_mixer_init(struct audio_softc *); 159 void wskbd_mixer_cb(void *); 160 #endif 161 162 const struct cfattach audio_ca = { 163 sizeof(struct audio_softc), audio_match, audio_attach, 164 audio_detach, audio_activate 165 }; 166 167 struct cfdriver audio_cd = { 168 NULL, "audio", DV_DULL 169 }; 170 171 void filt_audioctlrdetach(struct knote *); 172 int filt_audioctlread(struct knote *, long); 173 int filt_audiomodify(struct kevent *, struct knote *); 174 int filt_audioprocess(struct knote *, struct kevent *); 175 176 const struct filterops audioctlread_filtops = { 177 .f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE, 178 .f_attach = NULL, 179 .f_detach = filt_audioctlrdetach, 180 .f_event = filt_audioctlread, 181 .f_modify = filt_audiomodify, 182 .f_process = filt_audioprocess, 183 }; 184 185 void filt_audiowdetach(struct knote *); 186 int filt_audiowrite(struct knote *, long); 187 188 const struct filterops audiowrite_filtops = { 189 .f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE, 190 .f_attach = NULL, 191 .f_detach = filt_audiowdetach, 192 .f_event = filt_audiowrite, 193 .f_modify = filt_audiomodify, 194 .f_process = filt_audioprocess, 195 }; 196 197 void filt_audiordetach(struct knote *); 198 int filt_audioread(struct knote *, long); 199 200 const struct filterops audioread_filtops = { 201 .f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE, 202 .f_attach = NULL, 203 .f_detach = filt_audiordetach, 204 .f_event = filt_audioread, 205 .f_modify = filt_audiomodify, 206 .f_process = filt_audioprocess, 207 }; 208 209 /* 210 * This mutex protects data structures (including registers on the 211 * sound-card) that are manipulated by both the interrupt handler and 212 * syscall code-paths. 213 * 214 * Note that driver methods may sleep (e.g. in malloc); consequently the 215 * audio layer calls them with the mutex unlocked. Driver methods are 216 * responsible for locking the mutex when they manipulate data used by 217 * the interrupt handler and interrupts may occur. 218 * 219 * Similarly, the driver is responsible for locking the mutex in its 220 * interrupt handler and to call the audio layer call-backs (i.e. 221 * audio_{p,r}int()) with the mutex locked. 222 */ 223 struct mutex audio_lock = MUTEX_INITIALIZER(IPL_AUDIO); 224 225 /* 226 * Global flag to control if audio recording is enabled when the 227 * mixerctl setting is record.enable=sysctl 228 */ 229 int audio_record_enable = 0; 230 231 #ifdef AUDIO_DEBUG 232 /* 233 * 0 - nothing, as if AUDIO_DEBUG isn't defined 234 * 1 - initialisations & setup 235 * 2 - blocks & interrupts 236 */ 237 int audio_debug = 1; 238 #endif 239 240 unsigned int 241 audio_gcd(unsigned int a, unsigned int b) 242 { 243 unsigned int r; 244 245 while (b > 0) { 246 r = a % b; 247 a = b; 248 b = r; 249 } 250 return a; 251 } 252 253 /* 254 * Calculate the least block size (in frames) such that both the 255 * corresponding play and/or record block sizes (in bytes) are multiple 256 * of the given number of bytes. 257 */ 258 int 259 audio_blksz_bytes(int mode, 260 struct audio_params *p, struct audio_params *r, int bytes) 261 { 262 unsigned int np, nr; 263 264 if (mode & AUMODE_PLAY) { 265 np = bytes / audio_gcd(p->bps * p->channels, bytes); 266 if (!(mode & AUMODE_RECORD)) 267 nr = np; 268 } 269 if (mode & AUMODE_RECORD) { 270 nr = bytes / audio_gcd(r->bps * r->channels, bytes); 271 if (!(mode & AUMODE_PLAY)) 272 np = nr; 273 } 274 275 return nr * np / audio_gcd(nr, np); 276 } 277 278 void 279 audio_mixer_wakeup(void *addr) 280 { 281 struct audio_softc *sc = addr; 282 283 if (sc->mix_blocking) { 284 wakeup(&sc->mix_blocking); 285 sc->mix_blocking = 0; 286 } 287 /* 288 * As long as selwakeup() grabs the KERNEL_LOCK() make sure it is 289 * already held here to avoid lock ordering problems with `audio_lock' 290 */ 291 KERNEL_ASSERT_LOCKED(); 292 mtx_enter(&audio_lock); 293 selwakeup(&sc->mix_sel); 294 mtx_leave(&audio_lock); 295 } 296 297 void 298 audio_buf_wakeup(void *addr) 299 { 300 struct audio_buf *buf = addr; 301 302 if (buf->blocking) { 303 wakeup(&buf->blocking); 304 buf->blocking = 0; 305 } 306 /* 307 * As long as selwakeup() grabs the KERNEL_LOCK() make sure it is 308 * already held here to avoid lock ordering problems with `audio_lock' 309 */ 310 KERNEL_ASSERT_LOCKED(); 311 mtx_enter(&audio_lock); 312 selwakeup(&buf->sel); 313 mtx_leave(&audio_lock); 314 } 315 316 int 317 audio_buf_init(struct audio_softc *sc, struct audio_buf *buf, int dir) 318 { 319 klist_init_mutex(&buf->sel.si_note, &audio_lock); 320 buf->softintr = softintr_establish(IPL_SOFTAUDIO, 321 audio_buf_wakeup, buf); 322 if (buf->softintr == NULL) { 323 printf("%s: can't establish softintr\n", DEVNAME(sc)); 324 goto bad; 325 } 326 if (sc->ops->round_buffersize) { 327 buf->datalen = sc->ops->round_buffersize(sc->arg, 328 dir, AUDIO_BUFSZ); 329 } else 330 buf->datalen = AUDIO_BUFSZ; 331 if (sc->ops->allocm) { 332 buf->data = sc->ops->allocm(sc->arg, dir, buf->datalen, 333 M_DEVBUF, M_WAITOK); 334 } else 335 buf->data = malloc(buf->datalen, M_DEVBUF, M_WAITOK); 336 if (buf->data == NULL) { 337 softintr_disestablish(buf->softintr); 338 goto bad; 339 } 340 return 0; 341 bad: 342 klist_free(&buf->sel.si_note); 343 return ENOMEM; 344 } 345 346 void 347 audio_buf_done(struct audio_softc *sc, struct audio_buf *buf) 348 { 349 if (sc->ops->freem) 350 sc->ops->freem(sc->arg, buf->data, M_DEVBUF); 351 else 352 free(buf->data, M_DEVBUF, buf->datalen); 353 softintr_disestablish(buf->softintr); 354 klist_free(&buf->sel.si_note); 355 } 356 357 /* 358 * return the reader pointer and the number of bytes available 359 */ 360 unsigned char * 361 audio_buf_rgetblk(struct audio_buf *buf, size_t *rsize) 362 { 363 size_t count; 364 365 count = buf->len - buf->start; 366 if (count > buf->used) 367 count = buf->used; 368 *rsize = count; 369 return buf->data + buf->start; 370 } 371 372 /* 373 * discard "count" bytes at the start position. 374 */ 375 void 376 audio_buf_rdiscard(struct audio_buf *buf, size_t count) 377 { 378 #ifdef AUDIO_DEBUG 379 if (count > buf->used) { 380 panic("audio_buf_rdiscard: bad count = %zu, " 381 "start = %zu, used = %zu", count, buf->start, buf->used); 382 } 383 #endif 384 buf->used -= count; 385 buf->start += count; 386 if (buf->start >= buf->len) 387 buf->start -= buf->len; 388 } 389 390 /* 391 * advance the writer pointer by "count" bytes 392 */ 393 void 394 audio_buf_wcommit(struct audio_buf *buf, size_t count) 395 { 396 #ifdef AUDIO_DEBUG 397 if (count > (buf->len - buf->used)) { 398 panic("audio_buf_wcommit: bad count = %zu, " 399 "start = %zu, used = %zu", count, buf->start, buf->used); 400 } 401 #endif 402 buf->used += count; 403 } 404 405 /* 406 * get writer pointer and the number of bytes writable 407 */ 408 unsigned char * 409 audio_buf_wgetblk(struct audio_buf *buf, size_t *rsize) 410 { 411 size_t end, avail, count; 412 413 end = buf->start + buf->used; 414 if (end >= buf->len) 415 end -= buf->len; 416 avail = buf->len - buf->used; 417 count = buf->len - end; 418 if (count > avail) 419 count = avail; 420 *rsize = count; 421 return buf->data + end; 422 } 423 424 void 425 audio_calc_sil(struct audio_softc *sc) 426 { 427 unsigned char *q; 428 unsigned int s, i; 429 int d, e; 430 431 e = sc->sw_enc; 432 #ifdef AUDIO_DEBUG 433 switch (e) { 434 case AUDIO_ENCODING_SLINEAR_LE: 435 case AUDIO_ENCODING_ULINEAR_LE: 436 case AUDIO_ENCODING_SLINEAR_BE: 437 case AUDIO_ENCODING_ULINEAR_BE: 438 break; 439 default: 440 printf("%s: unhandled play encoding %d\n", DEVNAME(sc), e); 441 memset(sc->silence, 0, sc->bps); 442 return; 443 } 444 #endif 445 if (e == AUDIO_ENCODING_SLINEAR_BE || e == AUDIO_ENCODING_ULINEAR_BE) { 446 d = -1; 447 q = sc->silence + sc->bps - 1; 448 } else { 449 d = 1; 450 q = sc->silence; 451 } 452 if (e == AUDIO_ENCODING_SLINEAR_LE || e == AUDIO_ENCODING_SLINEAR_BE) { 453 s = 0; 454 } else { 455 s = 0x80000000; 456 if (sc->msb) 457 s >>= 32 - 8 * sc->bps; 458 else 459 s >>= 32 - sc->bits; 460 } 461 for (i = 0; i < sc->bps; i++) { 462 *q = s; 463 q += d; 464 s >>= 8; 465 } 466 if (sc->conv_enc) 467 sc->conv_enc(sc->silence, sc->bps); 468 } 469 470 void 471 audio_fill_sil(struct audio_softc *sc, unsigned char *ptr, size_t count) 472 { 473 unsigned char *q, *p; 474 size_t i, j; 475 476 q = ptr; 477 for (j = count / sc->bps; j > 0; j--) { 478 p = sc->silence; 479 for (i = sc->bps; i > 0; i--) 480 *q++ = *p++; 481 } 482 } 483 484 void 485 audio_clear(struct audio_softc *sc) 486 { 487 if (sc->mode & AUMODE_PLAY) { 488 sc->play.used = sc->play.start = 0; 489 sc->play.pos = sc->play.xrun = 0; 490 audio_fill_sil(sc, sc->play.data, sc->play.len); 491 } 492 if (sc->mode & AUMODE_RECORD) { 493 sc->rec.used = sc->rec.start = 0; 494 sc->rec.pos = sc->rec.xrun = 0; 495 audio_fill_sil(sc, sc->rec.data, sc->rec.len); 496 } 497 } 498 499 /* 500 * called whenever a block is consumed by the driver 501 */ 502 void 503 audio_pintr(void *addr) 504 { 505 struct audio_softc *sc = addr; 506 unsigned char *ptr; 507 size_t count; 508 int error, nblk, todo; 509 510 MUTEX_ASSERT_LOCKED(&audio_lock); 511 if (!(sc->mode & AUMODE_PLAY) || !sc->active) { 512 printf("%s: play interrupt but not playing\n", DEVNAME(sc)); 513 return; 514 } 515 if (sc->quiesce) { 516 DPRINTF("%s: quiesced, skipping play intr\n", DEVNAME(sc)); 517 return; 518 } 519 520 /* 521 * check if record pointer wrapped, see explanation 522 * in audio_rintr() 523 */ 524 if ((sc->mode & AUMODE_RECORD) && sc->ops->underrun == NULL) { 525 sc->offs--; 526 nblk = sc->rec.len / sc->rec.blksz; 527 todo = -sc->offs; 528 if (todo >= nblk) { 529 todo -= todo % nblk; 530 DPRINTFN(1, "%s: rec ptr wrapped, moving %d blocks\n", 531 DEVNAME(sc), todo); 532 while (todo-- > 0) 533 audio_rintr(sc); 534 } 535 } 536 537 sc->play.pos += sc->play.blksz; 538 if (!sc->ops->underrun) { 539 audio_fill_sil(sc, sc->play.data + sc->play.start, 540 sc->play.blksz); 541 } 542 audio_buf_rdiscard(&sc->play, sc->play.blksz); 543 if (sc->play.used < sc->play.blksz) { 544 DPRINTFN(1, "%s: play underrun\n", DEVNAME(sc)); 545 sc->play.xrun += sc->play.blksz; 546 audio_buf_wcommit(&sc->play, sc->play.blksz); 547 if (sc->ops->underrun) 548 sc->ops->underrun(sc->arg); 549 } 550 551 DPRINTFN(1, "%s: play intr, used -> %zu, start -> %zu\n", 552 DEVNAME(sc), sc->play.used, sc->play.start); 553 554 if (!sc->ops->trigger_output) { 555 ptr = audio_buf_rgetblk(&sc->play, &count); 556 error = sc->ops->start_output(sc->arg, 557 ptr, sc->play.blksz, audio_pintr, sc); 558 if (error) { 559 printf("%s: play restart failed: %d\n", 560 DEVNAME(sc), error); 561 } 562 } 563 564 if (sc->play.used < sc->play.len) { 565 DPRINTFN(1, "%s: play wakeup, chan = %d\n", 566 DEVNAME(sc), sc->play.blocking); 567 /* 568 * As long as selwakeup() needs to be protected by the 569 * KERNEL_LOCK() we have to delay the wakeup to another 570 * context to keep the interrupt context KERNEL_LOCK() 571 * free. 572 */ 573 softintr_schedule(sc->play.softintr); 574 } 575 } 576 577 /* 578 * called whenever a block is produced by the driver 579 */ 580 void 581 audio_rintr(void *addr) 582 { 583 struct audio_softc *sc = addr; 584 unsigned char *ptr; 585 size_t count; 586 int error, nblk, todo; 587 588 MUTEX_ASSERT_LOCKED(&audio_lock); 589 if (!(sc->mode & AUMODE_RECORD) || !sc->active) { 590 printf("%s: rec interrupt but not recording\n", DEVNAME(sc)); 591 return; 592 } 593 if (sc->quiesce) { 594 DPRINTF("%s: quiesced, skipping rec intr\n", DEVNAME(sc)); 595 return; 596 } 597 598 /* 599 * Interrupts may be masked by other sub-systems during 320ms 600 * and more. During such a delay the hardware doesn't stop 601 * playing and the play buffer pointers may wrap, this can't be 602 * detected and corrected by low level drivers. This makes the 603 * record stream ahead of the play stream; this is detected as a 604 * hardware anomaly by userland and cause programs to misbehave. 605 * 606 * We fix this by advancing play position by an integer count of 607 * full buffers, so it reaches the record position. 608 */ 609 if ((sc->mode & AUMODE_PLAY) && sc->ops->underrun == NULL) { 610 sc->offs++; 611 nblk = sc->play.len / sc->play.blksz; 612 todo = sc->offs; 613 if (todo >= nblk) { 614 todo -= todo % nblk; 615 DPRINTFN(1, "%s: play ptr wrapped, moving %d blocks\n", 616 DEVNAME(sc), todo); 617 while (todo-- > 0) 618 audio_pintr(sc); 619 } 620 } 621 622 sc->rec.pos += sc->rec.blksz; 623 if ((sc->record_enable == MIXER_RECORD_ENABLE_SYSCTL && 624 !audio_record_enable) || 625 sc->record_enable == MIXER_RECORD_ENABLE_OFF) { 626 ptr = audio_buf_wgetblk(&sc->rec, &count); 627 audio_fill_sil(sc, ptr, sc->rec.blksz); 628 } 629 audio_buf_wcommit(&sc->rec, sc->rec.blksz); 630 if (sc->rec.used > sc->rec.len - sc->rec.blksz) { 631 DPRINTFN(1, "%s: rec overrun\n", DEVNAME(sc)); 632 sc->rec.xrun += sc->rec.blksz; 633 audio_buf_rdiscard(&sc->rec, sc->rec.blksz); 634 } 635 DPRINTFN(1, "%s: rec intr, used -> %zu\n", DEVNAME(sc), sc->rec.used); 636 637 if (!sc->ops->trigger_input) { 638 ptr = audio_buf_wgetblk(&sc->rec, &count); 639 error = sc->ops->start_input(sc->arg, 640 ptr, sc->rec.blksz, audio_rintr, sc); 641 if (error) { 642 printf("%s: rec restart failed: %d\n", 643 DEVNAME(sc), error); 644 } 645 } 646 647 if (sc->rec.used > 0) { 648 DPRINTFN(1, "%s: rec wakeup, chan = %d\n", 649 DEVNAME(sc), sc->rec.blocking); 650 /* 651 * As long as selwakeup() needs to be protected by the 652 * KERNEL_LOCK() we have to delay the wakeup to another 653 * context to keep the interrupt context KERNEL_LOCK() 654 * free. 655 */ 656 softintr_schedule(sc->rec.softintr); 657 } 658 } 659 660 int 661 audio_start_do(struct audio_softc *sc) 662 { 663 int error; 664 struct audio_params p; 665 unsigned char *ptr; 666 size_t count; 667 668 DPRINTF("%s: starting\n", DEVNAME(sc)); 669 670 error = 0; 671 sc->offs = 0; 672 if (sc->mode & AUMODE_PLAY) { 673 if (sc->ops->trigger_output) { 674 p.encoding = sc->hw_enc; 675 p.precision = sc->bits; 676 p.bps = sc->bps; 677 p.msb = sc->msb; 678 p.sample_rate = sc->rate; 679 p.channels = sc->pchan; 680 error = sc->ops->trigger_output(sc->arg, 681 sc->play.data, 682 sc->play.data + sc->play.len, 683 sc->play.blksz, 684 audio_pintr, sc, &p); 685 } else { 686 mtx_enter(&audio_lock); 687 ptr = audio_buf_rgetblk(&sc->play, &count); 688 error = sc->ops->start_output(sc->arg, 689 ptr, sc->play.blksz, audio_pintr, sc); 690 mtx_leave(&audio_lock); 691 } 692 if (error) 693 printf("%s: failed to start playback\n", DEVNAME(sc)); 694 } 695 if (sc->mode & AUMODE_RECORD) { 696 if (sc->ops->trigger_input) { 697 p.encoding = sc->hw_enc; 698 p.precision = sc->bits; 699 p.bps = sc->bps; 700 p.msb = sc->msb; 701 p.sample_rate = sc->rate; 702 p.channels = sc->rchan; 703 error = sc->ops->trigger_input(sc->arg, 704 sc->rec.data, 705 sc->rec.data + sc->rec.len, 706 sc->rec.blksz, 707 audio_rintr, sc, &p); 708 } else { 709 mtx_enter(&audio_lock); 710 ptr = audio_buf_wgetblk(&sc->rec, &count); 711 error = sc->ops->start_input(sc->arg, 712 ptr, sc->rec.blksz, audio_rintr, sc); 713 mtx_leave(&audio_lock); 714 } 715 if (error) 716 printf("%s: failed to start recording\n", DEVNAME(sc)); 717 } 718 return error; 719 } 720 721 int 722 audio_stop_do(struct audio_softc *sc) 723 { 724 if (sc->mode & AUMODE_PLAY) 725 sc->ops->halt_output(sc->arg); 726 if (sc->mode & AUMODE_RECORD) 727 sc->ops->halt_input(sc->arg); 728 DPRINTF("%s: stopped\n", DEVNAME(sc)); 729 return 0; 730 } 731 732 int 733 audio_start(struct audio_softc *sc) 734 { 735 sc->active = 1; 736 sc->play.xrun = sc->play.pos = sc->rec.xrun = sc->rec.pos = 0; 737 return audio_start_do(sc); 738 } 739 740 int 741 audio_stop(struct audio_softc *sc) 742 { 743 int error; 744 745 error = audio_stop_do(sc); 746 if (error) 747 return error; 748 audio_clear(sc); 749 sc->active = 0; 750 return 0; 751 } 752 753 int 754 audio_canstart(struct audio_softc *sc) 755 { 756 if (sc->active || sc->pause) 757 return 0; 758 if ((sc->mode & AUMODE_RECORD) && sc->rec.used != 0) 759 return 0; 760 if ((sc->mode & AUMODE_PLAY) && sc->play.used != sc->play.len) 761 return 0; 762 return 1; 763 } 764 765 int 766 audio_setpar_blksz(struct audio_softc *sc, 767 struct audio_params *p, struct audio_params *r) 768 { 769 unsigned int nr, np, max, min, mult; 770 unsigned int blk_mult, blk_max; 771 772 if (sc->ops->set_blksz) { 773 /* 774 * Don't allow block size of exceed half the buffer size 775 */ 776 if (sc->mode & AUMODE_PLAY) { 777 max = sc->play.datalen / 2 / (sc->pchan * sc->bps); 778 if (sc->round > max) 779 sc->round = max; 780 } 781 if (sc->mode & AUMODE_RECORD) { 782 max = sc->rec.datalen / 2 / (sc->rchan * sc->bps); 783 if (sc->round > max) 784 sc->round = max; 785 } 786 787 sc->round = sc->ops->set_blksz(sc->arg, sc->mode, 788 p, r, sc->round); 789 790 DPRINTF("%s: block size set to: %u\n", DEVNAME(sc), sc->round); 791 return 0; 792 } 793 794 /* 795 * get least multiplier of the number of frames per block 796 */ 797 if (sc->ops->round_blocksize) { 798 blk_mult = sc->ops->round_blocksize(sc->arg, 1); 799 if (blk_mult == 0) { 800 printf("%s: 0x%x: bad block size multiplier\n", 801 DEVNAME(sc), blk_mult); 802 return ENODEV; 803 } 804 } else 805 blk_mult = 1; 806 DPRINTF("%s: hw block size multiplier: %u\n", DEVNAME(sc), blk_mult); 807 if (sc->mode & AUMODE_PLAY) { 808 np = blk_mult / audio_gcd(sc->pchan * sc->bps, blk_mult); 809 if (!(sc->mode & AUMODE_RECORD)) 810 nr = np; 811 DPRINTF("%s: play number of frames multiplier: %u\n", 812 DEVNAME(sc), np); 813 } 814 if (sc->mode & AUMODE_RECORD) { 815 nr = blk_mult / audio_gcd(sc->rchan * sc->bps, blk_mult); 816 if (!(sc->mode & AUMODE_PLAY)) 817 np = nr; 818 DPRINTF("%s: record number of frames multiplier: %u\n", 819 DEVNAME(sc), nr); 820 } 821 mult = nr * np / audio_gcd(nr, np); 822 DPRINTF("%s: least common number of frames multiplier: %u\n", 823 DEVNAME(sc), mult); 824 825 /* 826 * get minimum and maximum frames per block 827 */ 828 if (sc->ops->round_blocksize) 829 blk_max = sc->ops->round_blocksize(sc->arg, AUDIO_BUFSZ); 830 else 831 blk_max = AUDIO_BUFSZ; 832 if ((sc->mode & AUMODE_PLAY) && blk_max > sc->play.datalen / 2) 833 blk_max = sc->play.datalen / 2; 834 if ((sc->mode & AUMODE_RECORD) && blk_max > sc->rec.datalen / 2) 835 blk_max = sc->rec.datalen / 2; 836 if (sc->mode & AUMODE_PLAY) { 837 np = blk_max / (sc->pchan * sc->bps); 838 if (!(sc->mode & AUMODE_RECORD)) 839 nr = np; 840 } 841 if (sc->mode & AUMODE_RECORD) { 842 nr = blk_max / (sc->rchan * sc->bps); 843 if (!(sc->mode & AUMODE_PLAY)) 844 np = nr; 845 } 846 max = np < nr ? np : nr; 847 max -= max % mult; 848 min = sc->rate / 1000 + mult - 1; 849 min -= min % mult; 850 DPRINTF("%s: frame number range: %u..%u\n", DEVNAME(sc), min, max); 851 if (max < min) { 852 printf("%s: %u: bad max frame number\n", DEVNAME(sc), max); 853 return EIO; 854 } 855 856 /* 857 * adjust the frame per block to match our constraints 858 */ 859 sc->round += mult / 2; 860 sc->round -= sc->round % mult; 861 if (sc->round > max) 862 sc->round = max; 863 else if (sc->round < min) 864 sc->round = min; 865 866 return 0; 867 } 868 869 int 870 audio_setpar_nblks(struct audio_softc *sc, 871 struct audio_params *p, struct audio_params *r) 872 { 873 unsigned int max; 874 875 /* 876 * set buffer size (number of blocks) 877 */ 878 if (sc->mode & AUMODE_PLAY) { 879 max = sc->play.datalen / (sc->round * sc->pchan * sc->bps); 880 if (sc->play.nblks > max) 881 sc->play.nblks = max; 882 else if (sc->play.nblks < 2) 883 sc->play.nblks = 2; 884 if (sc->ops->set_nblks) { 885 sc->play.nblks = sc->ops->set_nblks(sc->arg, sc->mode, 886 p, sc->round, sc->play.nblks); 887 DPRINTF("%s: play nblks -> %u\n", DEVNAME(sc), 888 sc->play.nblks); 889 } 890 } 891 if (sc->mode & AUMODE_RECORD) { 892 /* 893 * for recording, buffer size is not the latency (it's 894 * exactly one block), so let's get the maximum buffer 895 * size of maximum reliability during xruns 896 */ 897 max = sc->rec.datalen / (sc->round * sc->rchan * sc->bps); 898 if (sc->ops->set_nblks) { 899 max = sc->ops->set_nblks(sc->arg, sc->mode, 900 r, sc->round, max); 901 DPRINTF("%s: rec nblks -> %u\n", DEVNAME(sc), max); 902 } 903 sc->rec.nblks = max; 904 } 905 return 0; 906 } 907 908 int 909 audio_setpar(struct audio_softc *sc) 910 { 911 struct audio_params p, r; 912 int error; 913 914 DPRINTF("%s: setpar: req enc=%d bits=%d, bps=%d, msb=%d " 915 "rate=%d, pchan=%d, rchan=%d, round=%u, nblks=%d\n", 916 DEVNAME(sc), sc->sw_enc, sc->bits, sc->bps, sc->msb, 917 sc->rate, sc->pchan, sc->rchan, sc->round, sc->play.nblks); 918 919 /* 920 * check if requested parameters are in the allowed ranges 921 */ 922 if (sc->mode & AUMODE_PLAY) { 923 if (sc->pchan < 1) 924 sc->pchan = 1; 925 else if (sc->pchan > 64) 926 sc->pchan = 64; 927 } 928 if (sc->mode & AUMODE_RECORD) { 929 if (sc->rchan < 1) 930 sc->rchan = 1; 931 else if (sc->rchan > 64) 932 sc->rchan = 64; 933 } 934 switch (sc->sw_enc) { 935 case AUDIO_ENCODING_ULAW: 936 case AUDIO_ENCODING_ALAW: 937 case AUDIO_ENCODING_SLINEAR_LE: 938 case AUDIO_ENCODING_SLINEAR_BE: 939 case AUDIO_ENCODING_ULINEAR_LE: 940 case AUDIO_ENCODING_ULINEAR_BE: 941 break; 942 default: 943 sc->sw_enc = AUDIO_ENCODING_SLINEAR_LE; 944 } 945 if (sc->bits < 8) 946 sc->bits = 8; 947 else if (sc->bits > 32) 948 sc->bits = 32; 949 if (sc->bps < 1) 950 sc->bps = 1; 951 else if (sc->bps > 4) 952 sc->bps = 4; 953 if (sc->rate < 4000) 954 sc->rate = 4000; 955 else if (sc->rate > 192000) 956 sc->rate = 192000; 957 958 /* 959 * copy into struct audio_params, required by drivers 960 */ 961 p.encoding = r.encoding = sc->sw_enc; 962 p.precision = r.precision = sc->bits; 963 p.bps = r.bps = sc->bps; 964 p.msb = r.msb = sc->msb; 965 p.sample_rate = r.sample_rate = sc->rate; 966 p.channels = sc->pchan; 967 r.channels = sc->rchan; 968 969 /* 970 * set parameters 971 */ 972 error = sc->ops->set_params(sc->arg, sc->mode, sc->mode, &p, &r); 973 if (error) 974 return error; 975 if (sc->mode == (AUMODE_PLAY | AUMODE_RECORD)) { 976 if (p.encoding != r.encoding || 977 p.precision != r.precision || 978 p.bps != r.bps || 979 p.msb != r.msb || 980 p.sample_rate != r.sample_rate) { 981 printf("%s: different play and record parameters " 982 "returned by hardware\n", DEVNAME(sc)); 983 return ENODEV; 984 } 985 } 986 if (sc->mode & AUMODE_PLAY) { 987 sc->hw_enc = p.encoding; 988 sc->bits = p.precision; 989 sc->bps = p.bps; 990 sc->msb = p.msb; 991 sc->rate = p.sample_rate; 992 sc->pchan = p.channels; 993 } 994 if (sc->mode & AUMODE_RECORD) { 995 sc->hw_enc = r.encoding; 996 sc->bits = r.precision; 997 sc->bps = r.bps; 998 sc->msb = r.msb; 999 sc->rate = r.sample_rate; 1000 sc->rchan = r.channels; 1001 } 1002 if (sc->rate == 0 || sc->bps == 0 || sc->bits == 0) { 1003 printf("%s: invalid parameters returned by hardware\n", 1004 DEVNAME(sc)); 1005 return ENODEV; 1006 } 1007 if (sc->ops->commit_settings) { 1008 error = sc->ops->commit_settings(sc->arg); 1009 if (error) 1010 return error; 1011 } 1012 1013 /* 1014 * conversion from/to exotic/dead encoding, for drivers not supporting 1015 * linear 1016 */ 1017 switch (sc->hw_enc) { 1018 case AUDIO_ENCODING_SLINEAR_LE: 1019 case AUDIO_ENCODING_SLINEAR_BE: 1020 case AUDIO_ENCODING_ULINEAR_LE: 1021 case AUDIO_ENCODING_ULINEAR_BE: 1022 sc->sw_enc = sc->hw_enc; 1023 sc->conv_dec = sc->conv_enc = NULL; 1024 break; 1025 case AUDIO_ENCODING_ULAW: 1026 #if BYTE_ORDER == LITTLE_ENDIAN 1027 sc->sw_enc = AUDIO_ENCODING_SLINEAR_LE; 1028 #else 1029 sc->sw_enc = AUDIO_ENCODING_SLINEAR_BE; 1030 #endif 1031 if (sc->bits == 8) { 1032 sc->conv_enc = slinear8_to_mulaw; 1033 sc->conv_dec = mulaw_to_slinear8; 1034 } else if (sc->bits == 24) { 1035 sc->conv_enc = slinear24_to_mulaw24; 1036 sc->conv_dec = mulaw24_to_slinear24; 1037 } else { 1038 sc->sw_enc = sc->hw_enc; 1039 sc->conv_dec = sc->conv_enc = NULL; 1040 } 1041 break; 1042 default: 1043 printf("%s: setpar: enc = %d, bits = %d: emulation skipped\n", 1044 DEVNAME(sc), sc->hw_enc, sc->bits); 1045 sc->sw_enc = sc->hw_enc; 1046 sc->conv_dec = sc->conv_enc = NULL; 1047 } 1048 audio_calc_sil(sc); 1049 1050 error = audio_setpar_blksz(sc, &p, &r); 1051 if (error) 1052 return error; 1053 1054 error = audio_setpar_nblks(sc, &p, &r); 1055 if (error) 1056 return error; 1057 1058 /* 1059 * set buffer 1060 */ 1061 if (sc->mode & AUMODE_PLAY) { 1062 sc->play.blksz = sc->round * sc->pchan * sc->bps; 1063 sc->play.len = sc->play.nblks * sc->play.blksz; 1064 } 1065 if (sc->mode & AUMODE_RECORD) { 1066 sc->rec.blksz = sc->round * sc->rchan * sc->bps; 1067 sc->rec.len = sc->rec.nblks * sc->rec.blksz; 1068 } 1069 1070 DPRINTF("%s: setpar: new enc=%d bits=%d, bps=%d, msb=%d " 1071 "rate=%d, pchan=%d, rchan=%d, round=%u, nblks=%d\n", 1072 DEVNAME(sc), sc->sw_enc, sc->bits, sc->bps, sc->msb, 1073 sc->rate, sc->pchan, sc->rchan, sc->round, sc->play.nblks); 1074 return 0; 1075 } 1076 1077 int 1078 audio_ioc_start(struct audio_softc *sc) 1079 { 1080 if (!sc->pause) { 1081 DPRINTF("%s: can't start: already started\n", DEVNAME(sc)); 1082 return EBUSY; 1083 } 1084 if ((sc->mode & AUMODE_PLAY) && sc->play.used != sc->play.len) { 1085 DPRINTF("%s: play buffer not ready\n", DEVNAME(sc)); 1086 return EBUSY; 1087 } 1088 if ((sc->mode & AUMODE_RECORD) && sc->rec.used != 0) { 1089 DPRINTF("%s: record buffer not ready\n", DEVNAME(sc)); 1090 return EBUSY; 1091 } 1092 sc->pause = 0; 1093 return audio_start(sc); 1094 } 1095 1096 int 1097 audio_ioc_stop(struct audio_softc *sc) 1098 { 1099 if (sc->pause) { 1100 DPRINTF("%s: can't stop: not started\n", DEVNAME(sc)); 1101 return EBUSY; 1102 } 1103 sc->pause = 1; 1104 if (sc->active) 1105 return audio_stop(sc); 1106 return 0; 1107 } 1108 1109 int 1110 audio_ioc_getpar(struct audio_softc *sc, struct audio_swpar *p) 1111 { 1112 p->rate = sc->rate; 1113 p->sig = sc->sw_enc == AUDIO_ENCODING_SLINEAR_LE || 1114 sc->sw_enc == AUDIO_ENCODING_SLINEAR_BE; 1115 p->le = sc->sw_enc == AUDIO_ENCODING_SLINEAR_LE || 1116 sc->sw_enc == AUDIO_ENCODING_ULINEAR_LE; 1117 p->bits = sc->bits; 1118 p->bps = sc->bps; 1119 p->msb = sc->msb; 1120 p->pchan = sc->pchan; 1121 p->rchan = sc->rchan; 1122 p->nblks = sc->play.nblks; 1123 p->round = sc->round; 1124 return 0; 1125 } 1126 1127 int 1128 audio_ioc_setpar(struct audio_softc *sc, struct audio_swpar *p) 1129 { 1130 int error, le, sig; 1131 1132 if (sc->active) { 1133 DPRINTF("%s: can't change params during dma\n", 1134 DEVNAME(sc)); 1135 return EBUSY; 1136 } 1137 1138 /* 1139 * copy desired parameters into the softc structure 1140 */ 1141 if (p->sig != ~0U || p->le != ~0U || p->bits != ~0U) { 1142 sig = 1; 1143 le = (BYTE_ORDER == LITTLE_ENDIAN); 1144 sc->bits = 16; 1145 sc->bps = 2; 1146 sc->msb = 1; 1147 if (p->sig != ~0U) 1148 sig = p->sig; 1149 if (p->le != ~0U) 1150 le = p->le; 1151 if (p->bits != ~0U) { 1152 sc->bits = p->bits; 1153 sc->bps = sc->bits <= 8 ? 1154 1 : (sc->bits <= 16 ? 2 : 4); 1155 if (p->bps != ~0U) 1156 sc->bps = p->bps; 1157 if (p->msb != ~0U) 1158 sc->msb = p->msb ? 1 : 0; 1159 } 1160 sc->sw_enc = (sig) ? 1161 (le ? AUDIO_ENCODING_SLINEAR_LE : 1162 AUDIO_ENCODING_SLINEAR_BE) : 1163 (le ? AUDIO_ENCODING_ULINEAR_LE : 1164 AUDIO_ENCODING_ULINEAR_BE); 1165 } 1166 if (p->rate != ~0) 1167 sc->rate = p->rate; 1168 if (p->pchan != ~0) 1169 sc->pchan = p->pchan; 1170 if (p->rchan != ~0) 1171 sc->rchan = p->rchan; 1172 if (p->round != ~0) 1173 sc->round = p->round; 1174 if (p->nblks != ~0) 1175 sc->play.nblks = p->nblks; 1176 1177 /* 1178 * if the device is not opened for playback or recording don't 1179 * touch the hardware yet (ex. if this is /dev/audioctlN) 1180 */ 1181 if (sc->mode == 0) 1182 return 0; 1183 1184 /* 1185 * negotiate parameters with the hardware 1186 */ 1187 error = audio_setpar(sc); 1188 if (error) 1189 return error; 1190 audio_clear(sc); 1191 if ((sc->mode & AUMODE_PLAY) && sc->ops->init_output) { 1192 error = sc->ops->init_output(sc->arg, 1193 sc->play.data, sc->play.len); 1194 if (error) 1195 return error; 1196 } 1197 if ((sc->mode & AUMODE_RECORD) && sc->ops->init_input) { 1198 error = sc->ops->init_input(sc->arg, 1199 sc->rec.data, sc->rec.len); 1200 if (error) 1201 return error; 1202 } 1203 return 0; 1204 } 1205 1206 int 1207 audio_ioc_getstatus(struct audio_softc *sc, struct audio_status *p) 1208 { 1209 p->mode = sc->mode; 1210 p->pause = sc->pause; 1211 p->active = sc->active; 1212 return 0; 1213 } 1214 1215 int 1216 audio_match(struct device *parent, void *match, void *aux) 1217 { 1218 struct audio_attach_args *sa = aux; 1219 1220 return (sa->type == AUDIODEV_TYPE_AUDIO) ? 1 : 0; 1221 } 1222 1223 void 1224 audio_attach(struct device *parent, struct device *self, void *aux) 1225 { 1226 struct audio_softc *sc = (void *)self; 1227 struct audio_attach_args *sa = aux; 1228 const struct audio_hw_if *ops = sa->hwif; 1229 struct mixer_devinfo *mi; 1230 struct mixer_ctrl *ent; 1231 void *arg = sa->hdl; 1232 int error; 1233 1234 printf("\n"); 1235 1236 #ifdef DIAGNOSTIC 1237 if (ops == 0 || 1238 ops->open == 0 || 1239 ops->close == 0 || 1240 ops->set_params == 0 || 1241 (ops->start_output == 0 && ops->trigger_output == 0) || 1242 (ops->start_input == 0 && ops->trigger_input == 0) || 1243 ops->halt_output == 0 || 1244 ops->halt_input == 0 || 1245 ops->set_port == 0 || 1246 ops->get_port == 0 || 1247 ops->query_devinfo == 0 || 1248 ops->get_props == 0) { 1249 printf("%s: missing method\n", DEVNAME(sc)); 1250 sc->ops = 0; 1251 return; 1252 } 1253 #endif 1254 sc->ops = ops; 1255 sc->cookie = sa->cookie; 1256 sc->arg = arg; 1257 1258 #if NWSKBD > 0 1259 wskbd_mixer_init(sc); 1260 #endif /* NWSKBD > 0 */ 1261 1262 error = audio_buf_init(sc, &sc->play, AUMODE_PLAY); 1263 if (error) { 1264 sc->ops = 0; 1265 printf("%s: could not allocate play buffer\n", DEVNAME(sc)); 1266 return; 1267 } 1268 error = audio_buf_init(sc, &sc->rec, AUMODE_RECORD); 1269 if (error) { 1270 audio_buf_done(sc, &sc->play); 1271 sc->ops = 0; 1272 printf("%s: could not allocate record buffer\n", DEVNAME(sc)); 1273 return; 1274 } 1275 1276 klist_init_mutex(&sc->mix_sel.si_note, &audio_lock); 1277 sc->mix_softintr = softintr_establish(IPL_SOFTAUDIO, 1278 audio_mixer_wakeup, sc); 1279 if (sc->mix_softintr == NULL) { 1280 klist_free(&sc->mix_sel.si_note); 1281 audio_buf_done(sc, &sc->rec); 1282 audio_buf_done(sc, &sc->play); 1283 sc->ops = 0; 1284 printf("%s: can't establish softintr\n", DEVNAME(sc)); 1285 return; 1286 } 1287 1288 /* set defaults */ 1289 #if BYTE_ORDER == LITTLE_ENDIAN 1290 sc->sw_enc = AUDIO_ENCODING_SLINEAR_LE; 1291 #else 1292 sc->sw_enc = AUDIO_ENCODING_SLINEAR_BE; 1293 #endif 1294 sc->bits = 16; 1295 sc->bps = 2; 1296 sc->msb = 1; 1297 sc->rate = 48000; 1298 sc->pchan = 2; 1299 sc->rchan = 2; 1300 sc->round = 960; 1301 sc->play.nblks = 2; 1302 sc->play.pos = sc->play.xrun = sc->rec.pos = sc->rec.xrun = 0; 1303 sc->record_enable = MIXER_RECORD_ENABLE_SYSCTL; 1304 1305 /* 1306 * allocate an array of mixer_ctrl structures to save the 1307 * mixer state and prefill them. 1308 */ 1309 1310 mi = malloc(sizeof(struct mixer_devinfo), M_TEMP, M_WAITOK); 1311 1312 mi->index = 0; 1313 while (1) { 1314 if (sc->ops->query_devinfo(sc->arg, mi) != 0) 1315 break; 1316 mi->index++; 1317 } 1318 sc->mix_nent = mi->index; 1319 sc->mix_ents = mallocarray(sc->mix_nent, 1320 sizeof(struct mixer_ctrl), M_DEVBUF, M_WAITOK); 1321 sc->mix_evbuf = mallocarray(sc->mix_nent, 1322 sizeof(struct mixer_ev), M_DEVBUF, M_WAITOK | M_ZERO); 1323 1324 ent = sc->mix_ents; 1325 mi->index = 0; 1326 while (1) { 1327 if (sc->ops->query_devinfo(sc->arg, mi) != 0) 1328 break; 1329 switch (mi->type) { 1330 case AUDIO_MIXER_VALUE: 1331 ent->un.value.num_channels = mi->un.v.num_channels; 1332 /* FALLTHROUGH */ 1333 case AUDIO_MIXER_SET: 1334 case AUDIO_MIXER_ENUM: 1335 ent->dev = mi->index; 1336 ent->type = mi->type; 1337 } 1338 mi->index++; 1339 ent++; 1340 } 1341 1342 free(mi, M_TEMP, sizeof(struct mixer_devinfo)); 1343 } 1344 1345 int 1346 audio_activate(struct device *self, int act) 1347 { 1348 struct audio_softc *sc = (struct audio_softc *)self; 1349 int i; 1350 1351 switch (act) { 1352 case DVACT_QUIESCE: 1353 /* 1354 * good drivers run play and rec handlers in a single 1355 * interrupt. Grab the lock to ensure we expose the same 1356 * sc->quiesce value to both play and rec handlers 1357 */ 1358 mtx_enter(&audio_lock); 1359 sc->quiesce = 1; 1360 mtx_leave(&audio_lock); 1361 1362 /* 1363 * once sc->quiesce is set, interrupts may occur, but 1364 * counters are not advanced and consequently processes 1365 * keep sleeping. 1366 * 1367 * XXX: ensure read/write/ioctl don't start/stop 1368 * DMA at the same time, this needs a "ready" condvar 1369 */ 1370 if (sc->mode != 0 && sc->active) 1371 audio_stop_do(sc); 1372 1373 /* 1374 * save mixer state 1375 */ 1376 for (i = 0; i != sc->mix_nent; i++) 1377 sc->ops->get_port(sc->arg, sc->mix_ents + i); 1378 1379 DPRINTF("%s: quiesce: active = %d\n", DEVNAME(sc), sc->active); 1380 break; 1381 case DVACT_WAKEUP: 1382 DPRINTF("%s: wakeup: active = %d\n", DEVNAME(sc), sc->active); 1383 1384 /* 1385 * restore mixer state 1386 */ 1387 for (i = 0; i != sc->mix_nent; i++) 1388 sc->ops->set_port(sc->arg, sc->mix_ents + i); 1389 1390 /* 1391 * keep buffer usage the same, but set start pointer to 1392 * the beginning of the buffer. 1393 * 1394 * No need to grab the audio_lock as DMA is stopped and 1395 * this is the only thread running (caller ensures this) 1396 */ 1397 sc->quiesce = 0; 1398 wakeup(&sc->quiesce); 1399 1400 if (sc->mode != 0) { 1401 if (audio_setpar(sc) != 0) 1402 break; 1403 if (sc->mode & AUMODE_PLAY) { 1404 sc->play.start = 0; 1405 audio_fill_sil(sc, sc->play.data, sc->play.len); 1406 } 1407 if (sc->mode & AUMODE_RECORD) { 1408 sc->rec.start = sc->rec.len - sc->rec.used; 1409 audio_fill_sil(sc, sc->rec.data, sc->rec.len); 1410 } 1411 if (sc->active) 1412 audio_start_do(sc); 1413 } 1414 break; 1415 } 1416 return 0; 1417 } 1418 1419 int 1420 audio_detach(struct device *self, int flags) 1421 { 1422 struct audio_softc *sc = (struct audio_softc *)self; 1423 int maj, mn; 1424 1425 DPRINTF("%s: audio_detach: flags = %d\n", DEVNAME(sc), flags); 1426 1427 wakeup(&sc->quiesce); 1428 1429 /* locate the major number */ 1430 for (maj = 0; maj < nchrdev; maj++) 1431 if (cdevsw[maj].d_open == audioopen) 1432 break; 1433 /* 1434 * Nuke the vnodes for any open instances, calls close but as 1435 * close uses device_lookup, it returns EXIO and does nothing 1436 */ 1437 mn = self->dv_unit; 1438 vdevgone(maj, mn | AUDIO_DEV_AUDIO, mn | AUDIO_DEV_AUDIO, VCHR); 1439 vdevgone(maj, mn | AUDIO_DEV_AUDIOCTL, mn | AUDIO_DEV_AUDIOCTL, VCHR); 1440 1441 /* 1442 * The close() method did nothing, quickly halt DMA (normally 1443 * parent is already gone, and code below is no-op), and wake-up 1444 * user-land blocked in read/write/ioctl, which return EIO. 1445 */ 1446 if (sc->mode != 0) { 1447 if (sc->active) { 1448 wakeup(&sc->play.blocking); 1449 KERNEL_ASSERT_LOCKED(); 1450 mtx_enter(&audio_lock); 1451 wakeup(&sc->rec.blocking); 1452 selwakeup(&sc->play.sel); 1453 selwakeup(&sc->rec.sel); 1454 mtx_leave(&audio_lock); 1455 audio_stop(sc); 1456 } 1457 sc->ops->close(sc->arg); 1458 sc->mode = 0; 1459 } 1460 if (sc->mix_isopen) { 1461 wakeup(&sc->mix_blocking); 1462 KERNEL_ASSERT_LOCKED(); 1463 mtx_enter(&audio_lock); 1464 selwakeup(&sc->mix_sel); 1465 mtx_leave(&audio_lock); 1466 } 1467 klist_invalidate(&sc->play.sel.si_note); 1468 klist_invalidate(&sc->rec.sel.si_note); 1469 klist_invalidate(&sc->mix_sel.si_note); 1470 1471 /* free resources */ 1472 softintr_disestablish(sc->mix_softintr); 1473 klist_free(&sc->mix_sel.si_note); 1474 free(sc->mix_evbuf, M_DEVBUF, sc->mix_nent * sizeof(struct mixer_ev)); 1475 free(sc->mix_ents, M_DEVBUF, sc->mix_nent * sizeof(struct mixer_ctrl)); 1476 audio_buf_done(sc, &sc->play); 1477 audio_buf_done(sc, &sc->rec); 1478 return 0; 1479 } 1480 1481 int 1482 audio_submatch(struct device *parent, void *match, void *aux) 1483 { 1484 struct cfdata *cf = match; 1485 1486 return (cf->cf_driver == &audio_cd); 1487 } 1488 1489 struct device * 1490 audio_attach_mi(const struct audio_hw_if *ops, void *arg, void *cookie, 1491 struct device *dev) 1492 { 1493 struct audio_attach_args aa; 1494 1495 aa.type = AUDIODEV_TYPE_AUDIO; 1496 aa.hwif = ops; 1497 aa.hdl = arg; 1498 aa.cookie = cookie; 1499 1500 /* 1501 * attach this driver to the caller (hardware driver), this 1502 * checks the kernel config and possibly calls audio_attach() 1503 */ 1504 return config_found_sm(dev, &aa, audioprint, audio_submatch); 1505 } 1506 1507 int 1508 audioprint(void *aux, const char *pnp) 1509 { 1510 struct audio_attach_args *arg = aux; 1511 const char *type; 1512 1513 if (pnp != NULL) { 1514 switch (arg->type) { 1515 case AUDIODEV_TYPE_AUDIO: 1516 type = "audio"; 1517 break; 1518 case AUDIODEV_TYPE_OPL: 1519 type = "opl"; 1520 break; 1521 case AUDIODEV_TYPE_MPU: 1522 type = "mpu"; 1523 break; 1524 default: 1525 panic("audioprint: unknown type %d", arg->type); 1526 } 1527 printf("%s at %s", type, pnp); 1528 } 1529 return UNCONF; 1530 } 1531 1532 int 1533 audio_open(struct audio_softc *sc, int flags) 1534 { 1535 int error; 1536 int props; 1537 1538 if (sc->mode) 1539 return EBUSY; 1540 error = sc->ops->open(sc->arg, flags); 1541 if (error) 1542 return error; 1543 sc->active = 0; 1544 sc->pause = 1; 1545 sc->rec.blocking = 0; 1546 sc->play.blocking = 0; 1547 sc->mode = 0; 1548 if (flags & FWRITE) 1549 sc->mode |= AUMODE_PLAY; 1550 if (flags & FREAD) 1551 sc->mode |= AUMODE_RECORD; 1552 props = sc->ops->get_props(sc->arg); 1553 if (sc->mode == (AUMODE_PLAY | AUMODE_RECORD)) { 1554 if (!(props & AUDIO_PROP_FULLDUPLEX)) { 1555 error = ENOTTY; 1556 goto bad; 1557 } 1558 if (sc->ops->setfd) { 1559 error = sc->ops->setfd(sc->arg, 1); 1560 if (error) 1561 goto bad; 1562 } 1563 } 1564 1565 if (sc->ops->speaker_ctl) { 1566 /* 1567 * XXX: what is this used for? 1568 */ 1569 sc->ops->speaker_ctl(sc->arg, 1570 (sc->mode & AUMODE_PLAY) ? SPKR_ON : SPKR_OFF); 1571 } 1572 1573 error = audio_setpar(sc); 1574 if (error) 1575 goto bad; 1576 audio_clear(sc); 1577 1578 /* 1579 * allow read(2)/write(2) to automatically start DMA, without 1580 * the need for ioctl(), to make /dev/audio usable in scripts 1581 */ 1582 sc->pause = 0; 1583 return 0; 1584 bad: 1585 sc->ops->close(sc->arg); 1586 sc->mode = 0; 1587 return error; 1588 } 1589 1590 int 1591 audio_drain(struct audio_softc *sc) 1592 { 1593 int error, xrun; 1594 unsigned char *ptr; 1595 size_t count, bpf; 1596 1597 DPRINTF("%s: drain: mode = %d, pause = %d, active = %d, used = %zu\n", 1598 DEVNAME(sc), sc->mode, sc->pause, sc->active, sc->play.used); 1599 if (!(sc->mode & AUMODE_PLAY) || sc->pause) 1600 return 0; 1601 1602 /* discard partial samples, required by audio_fill_sil() */ 1603 mtx_enter(&audio_lock); 1604 bpf = sc->pchan * sc->bps; 1605 sc->play.used -= sc->play.used % bpf; 1606 if (sc->play.used == 0) { 1607 mtx_leave(&audio_lock); 1608 return 0; 1609 } 1610 1611 if (!sc->active) { 1612 /* 1613 * dma not started yet because buffer was not full 1614 * enough to start automatically. Pad it and start now. 1615 */ 1616 for (;;) { 1617 ptr = audio_buf_wgetblk(&sc->play, &count); 1618 if (count == 0) 1619 break; 1620 audio_fill_sil(sc, ptr, count); 1621 audio_buf_wcommit(&sc->play, count); 1622 } 1623 mtx_leave(&audio_lock); 1624 error = audio_start(sc); 1625 if (error) 1626 return error; 1627 mtx_enter(&audio_lock); 1628 } 1629 1630 xrun = sc->play.xrun; 1631 while (sc->play.xrun == xrun) { 1632 DPRINTF("%s: drain: used = %zu, xrun = %d\n", 1633 DEVNAME(sc), sc->play.used, sc->play.xrun); 1634 1635 /* 1636 * set a 5 second timeout, in case interrupts don't 1637 * work, useful only for debugging drivers 1638 */ 1639 sc->play.blocking = 1; 1640 error = msleep_nsec(&sc->play.blocking, &audio_lock, 1641 PWAIT | PCATCH, "au_dr", SEC_TO_NSEC(5)); 1642 if (!(sc->dev.dv_flags & DVF_ACTIVE)) 1643 error = EIO; 1644 if (error) { 1645 DPRINTF("%s: drain, err = %d\n", DEVNAME(sc), error); 1646 break; 1647 } 1648 } 1649 mtx_leave(&audio_lock); 1650 return error; 1651 } 1652 1653 int 1654 audio_close(struct audio_softc *sc) 1655 { 1656 audio_drain(sc); 1657 if (sc->active) 1658 audio_stop(sc); 1659 sc->ops->close(sc->arg); 1660 sc->mode = 0; 1661 DPRINTF("%s: close: done\n", DEVNAME(sc)); 1662 return 0; 1663 } 1664 1665 int 1666 audio_read(struct audio_softc *sc, struct uio *uio, int ioflag) 1667 { 1668 unsigned char *ptr; 1669 size_t count; 1670 int error; 1671 1672 DPRINTFN(1, "%s: read: resid = %zd\n", DEVNAME(sc), uio->uio_resid); 1673 1674 /* block if quiesced */ 1675 while (sc->quiesce) 1676 tsleep_nsec(&sc->quiesce, 0, "au_qrd", INFSLP); 1677 1678 /* start automatically if audio_ioc_start() was never called */ 1679 if (audio_canstart(sc)) { 1680 error = audio_start(sc); 1681 if (error) 1682 return error; 1683 } 1684 1685 mtx_enter(&audio_lock); 1686 1687 /* if there is no data then sleep */ 1688 while (sc->rec.used == 0) { 1689 if (ioflag & IO_NDELAY) { 1690 mtx_leave(&audio_lock); 1691 return EWOULDBLOCK; 1692 } 1693 DPRINTFN(1, "%s: read sleep\n", DEVNAME(sc)); 1694 sc->rec.blocking = 1; 1695 error = msleep_nsec(&sc->rec.blocking, 1696 &audio_lock, PWAIT | PCATCH, "au_rd", INFSLP); 1697 if (!(sc->dev.dv_flags & DVF_ACTIVE)) 1698 error = EIO; 1699 if (error) { 1700 DPRINTF("%s: read woke up error = %d\n", 1701 DEVNAME(sc), error); 1702 mtx_leave(&audio_lock); 1703 return error; 1704 } 1705 } 1706 1707 /* at this stage, there is data to transfer */ 1708 while (uio->uio_resid > 0 && sc->rec.used > 0) { 1709 ptr = audio_buf_rgetblk(&sc->rec, &count); 1710 if (count > uio->uio_resid) 1711 count = uio->uio_resid; 1712 mtx_leave(&audio_lock); 1713 DPRINTFN(1, "%s: read: start = %zu, count = %zu\n", 1714 DEVNAME(sc), ptr - sc->rec.data, count); 1715 if (sc->conv_dec) 1716 sc->conv_dec(ptr, count); 1717 error = uiomove(ptr, count, uio); 1718 if (error) 1719 return error; 1720 mtx_enter(&audio_lock); 1721 audio_buf_rdiscard(&sc->rec, count); 1722 } 1723 mtx_leave(&audio_lock); 1724 return 0; 1725 } 1726 1727 int 1728 audio_write(struct audio_softc *sc, struct uio *uio, int ioflag) 1729 { 1730 unsigned char *ptr; 1731 size_t count; 1732 int error; 1733 1734 DPRINTFN(1, "%s: write: resid = %zd\n", DEVNAME(sc), uio->uio_resid); 1735 1736 /* block if quiesced */ 1737 while (sc->quiesce) 1738 tsleep_nsec(&sc->quiesce, 0, "au_qwr", INFSLP); 1739 1740 /* 1741 * if IO_NDELAY flag is set then check if there is enough room 1742 * in the buffer to store at least one byte. If not then don't 1743 * start the write process. 1744 */ 1745 mtx_enter(&audio_lock); 1746 if (uio->uio_resid > 0 && (ioflag & IO_NDELAY)) { 1747 if (sc->play.used == sc->play.len) { 1748 mtx_leave(&audio_lock); 1749 return EWOULDBLOCK; 1750 } 1751 } 1752 1753 while (uio->uio_resid > 0) { 1754 while (1) { 1755 ptr = audio_buf_wgetblk(&sc->play, &count); 1756 if (count > 0) 1757 break; 1758 if (ioflag & IO_NDELAY) { 1759 /* 1760 * At this stage at least one byte is already 1761 * moved so we do not return EWOULDBLOCK 1762 */ 1763 mtx_leave(&audio_lock); 1764 return 0; 1765 } 1766 DPRINTFN(1, "%s: write sleep\n", DEVNAME(sc)); 1767 sc->play.blocking = 1; 1768 error = msleep_nsec(&sc->play.blocking, 1769 &audio_lock, PWAIT | PCATCH, "au_wr", INFSLP); 1770 if (!(sc->dev.dv_flags & DVF_ACTIVE)) 1771 error = EIO; 1772 if (error) { 1773 DPRINTF("%s: write woke up error = %d\n", 1774 DEVNAME(sc), error); 1775 mtx_leave(&audio_lock); 1776 return error; 1777 } 1778 } 1779 if (count > uio->uio_resid) 1780 count = uio->uio_resid; 1781 mtx_leave(&audio_lock); 1782 error = uiomove(ptr, count, uio); 1783 if (error) 1784 return 0; 1785 if (sc->conv_enc) { 1786 sc->conv_enc(ptr, count); 1787 DPRINTFN(1, "audio_write: converted count = %zu\n", 1788 count); 1789 } 1790 if (sc->ops->copy_output) 1791 sc->ops->copy_output(sc->arg, count); 1792 1793 mtx_enter(&audio_lock); 1794 audio_buf_wcommit(&sc->play, count); 1795 1796 /* start automatically if audio_ioc_start() was never called */ 1797 if (audio_canstart(sc)) { 1798 mtx_leave(&audio_lock); 1799 error = audio_start(sc); 1800 if (error) 1801 return error; 1802 mtx_enter(&audio_lock); 1803 } 1804 } 1805 mtx_leave(&audio_lock); 1806 return 0; 1807 } 1808 1809 int 1810 audio_getdev(struct audio_softc *sc, struct audio_device *adev) 1811 { 1812 memset(adev, 0, sizeof(struct audio_device)); 1813 if (sc->dev.dv_parent == NULL) 1814 return EIO; 1815 strlcpy(adev->name, sc->dev.dv_parent->dv_xname, MAX_AUDIO_DEV_LEN); 1816 return 0; 1817 } 1818 1819 int 1820 audio_ioctl(struct audio_softc *sc, unsigned long cmd, void *addr) 1821 { 1822 struct audio_pos *ap; 1823 int error = 0; 1824 1825 /* block if quiesced */ 1826 while (sc->quiesce) 1827 tsleep_nsec(&sc->quiesce, 0, "au_qio", INFSLP); 1828 1829 switch (cmd) { 1830 case FIONBIO: 1831 /* All handled in the upper FS layer. */ 1832 break; 1833 case AUDIO_GETPOS: 1834 mtx_enter(&audio_lock); 1835 ap = (struct audio_pos *)addr; 1836 ap->play_pos = sc->play.pos; 1837 ap->play_xrun = sc->play.xrun; 1838 ap->rec_pos = sc->rec.pos; 1839 ap->rec_xrun = sc->rec.xrun; 1840 mtx_leave(&audio_lock); 1841 break; 1842 case AUDIO_START: 1843 return audio_ioc_start(sc); 1844 case AUDIO_STOP: 1845 return audio_ioc_stop(sc); 1846 case AUDIO_SETPAR: 1847 error = audio_ioc_setpar(sc, (struct audio_swpar *)addr); 1848 break; 1849 case AUDIO_GETPAR: 1850 error = audio_ioc_getpar(sc, (struct audio_swpar *)addr); 1851 break; 1852 case AUDIO_GETSTATUS: 1853 error = audio_ioc_getstatus(sc, (struct audio_status *)addr); 1854 break; 1855 case AUDIO_GETDEV: 1856 error = audio_getdev(sc, (struct audio_device *)addr); 1857 break; 1858 default: 1859 DPRINTF("%s: unknown ioctl 0x%lx\n", DEVNAME(sc), cmd); 1860 error = ENOTTY; 1861 break; 1862 } 1863 return error; 1864 } 1865 1866 void 1867 audio_event(struct audio_softc *sc, int addr) 1868 { 1869 struct mixer_ev *e; 1870 1871 mtx_enter(&audio_lock); 1872 if (sc->mix_isopen) { 1873 e = sc->mix_evbuf + addr; 1874 if (!e->pending) { 1875 e->pending = 1; 1876 e->next = sc->mix_pending; 1877 sc->mix_pending = e; 1878 } 1879 softintr_schedule(sc->mix_softintr); 1880 } 1881 mtx_leave(&audio_lock); 1882 } 1883 1884 int 1885 audio_mixer_devinfo(struct audio_softc *sc, struct mixer_devinfo *devinfo) 1886 { 1887 if (devinfo->index < sc->mix_nent) 1888 return sc->ops->query_devinfo(sc->arg, devinfo); 1889 1890 devinfo->next = -1; 1891 devinfo->prev = -1; 1892 switch (devinfo->index - sc->mix_nent) { 1893 case MIXER_RECORD: 1894 strlcpy(devinfo->label.name, AudioCrecord, MAX_AUDIO_DEV_LEN); 1895 devinfo->type = AUDIO_MIXER_CLASS; 1896 devinfo->mixer_class = -1; 1897 break; 1898 case MIXER_RECORD_ENABLE: 1899 strlcpy(devinfo->label.name, "enable", MAX_AUDIO_DEV_LEN); 1900 devinfo->type = AUDIO_MIXER_ENUM; 1901 devinfo->mixer_class = MIXER_RECORD + sc->mix_nent; 1902 devinfo->un.e.num_mem = 3; 1903 devinfo->un.e.member[0].ord = MIXER_RECORD_ENABLE_OFF; 1904 strlcpy(devinfo->un.e.member[0].label.name, "off", 1905 MAX_AUDIO_DEV_LEN); 1906 devinfo->un.e.member[1].ord = MIXER_RECORD_ENABLE_ON; 1907 strlcpy(devinfo->un.e.member[1].label.name, "on", 1908 MAX_AUDIO_DEV_LEN); 1909 devinfo->un.e.member[2].ord = MIXER_RECORD_ENABLE_SYSCTL; 1910 strlcpy(devinfo->un.e.member[2].label.name, "sysctl", 1911 MAX_AUDIO_DEV_LEN); 1912 break; 1913 default: 1914 return EINVAL; 1915 } 1916 1917 return 0; 1918 } 1919 1920 int 1921 audio_mixer_get(struct audio_softc *sc, struct mixer_ctrl *c) 1922 { 1923 if (c->dev < sc->mix_nent) 1924 return sc->ops->get_port(sc->arg, c); 1925 1926 switch (c->dev - sc->mix_nent) { 1927 case MIXER_RECORD: 1928 return EBADF; 1929 case MIXER_RECORD_ENABLE: 1930 c->un.ord = sc->record_enable; 1931 break; 1932 default: 1933 return EINVAL; 1934 } 1935 1936 return 0; 1937 } 1938 1939 int 1940 audio_mixer_set(struct audio_softc *sc, struct mixer_ctrl *c, struct proc *p) 1941 { 1942 int error; 1943 1944 if (c->dev < sc->mix_nent) { 1945 error = sc->ops->set_port(sc->arg, c); 1946 if (error) 1947 return error; 1948 if (sc->ops->commit_settings) 1949 return sc->ops->commit_settings(sc->arg); 1950 audio_event(sc, c->dev); 1951 return 0; 1952 } 1953 1954 switch (c->dev - sc->mix_nent) { 1955 case MIXER_RECORD: 1956 return EBADF; 1957 case MIXER_RECORD_ENABLE: 1958 switch (c->un.ord) { 1959 case MIXER_RECORD_ENABLE_OFF: 1960 case MIXER_RECORD_ENABLE_ON: 1961 case MIXER_RECORD_ENABLE_SYSCTL: 1962 break; 1963 default: 1964 return EINVAL; 1965 } 1966 if (suser(p) == 0) 1967 sc->record_enable = c->un.ord; 1968 break; 1969 default: 1970 return EINVAL; 1971 } 1972 1973 return 0; 1974 } 1975 1976 int 1977 audio_ioctl_mixer(struct audio_softc *sc, unsigned long cmd, void *addr, 1978 struct proc *p) 1979 { 1980 /* block if quiesced */ 1981 while (sc->quiesce) 1982 tsleep_nsec(&sc->quiesce, 0, "mix_qio", INFSLP); 1983 1984 switch (cmd) { 1985 case FIONBIO: 1986 /* All handled in the upper FS layer. */ 1987 break; 1988 case AUDIO_MIXER_DEVINFO: 1989 return audio_mixer_devinfo(sc, addr); 1990 case AUDIO_MIXER_READ: 1991 return audio_mixer_get(sc, addr); 1992 case AUDIO_MIXER_WRITE: 1993 return audio_mixer_set(sc, addr, p); 1994 default: 1995 return ENOTTY; 1996 } 1997 return 0; 1998 } 1999 2000 int 2001 audio_mixer_read(struct audio_softc *sc, struct uio *uio, int ioflag) 2002 { 2003 struct mixer_ev *e; 2004 int data; 2005 int error; 2006 2007 DPRINTF("%s: mixer read: resid = %zd\n", DEVNAME(sc), uio->uio_resid); 2008 2009 /* block if quiesced */ 2010 while (sc->quiesce) 2011 tsleep_nsec(&sc->quiesce, 0, "mix_qrd", INFSLP); 2012 2013 mtx_enter(&audio_lock); 2014 2015 /* if there are no events then sleep */ 2016 while (!sc->mix_pending) { 2017 if (ioflag & IO_NDELAY) { 2018 mtx_leave(&audio_lock); 2019 return EWOULDBLOCK; 2020 } 2021 DPRINTF("%s: mixer read sleep\n", DEVNAME(sc)); 2022 sc->mix_blocking = 1; 2023 error = msleep_nsec(&sc->mix_blocking, 2024 &audio_lock, PWAIT | PCATCH, "mix_rd", INFSLP); 2025 if (!(sc->dev.dv_flags & DVF_ACTIVE)) 2026 error = EIO; 2027 if (error) { 2028 DPRINTF("%s: mixer read woke up error = %d\n", 2029 DEVNAME(sc), error); 2030 mtx_leave(&audio_lock); 2031 return error; 2032 } 2033 } 2034 2035 /* at this stage, there is an event to transfer */ 2036 while (uio->uio_resid >= sizeof(int) && sc->mix_pending) { 2037 e = sc->mix_pending; 2038 sc->mix_pending = e->next; 2039 e->pending = 0; 2040 data = e - sc->mix_evbuf; 2041 mtx_leave(&audio_lock); 2042 DPRINTF("%s: mixer read: %u\n", DEVNAME(sc), data); 2043 error = uiomove(&data, sizeof(int), uio); 2044 if (error) 2045 return error; 2046 mtx_enter(&audio_lock); 2047 } 2048 2049 mtx_leave(&audio_lock); 2050 return 0; 2051 } 2052 2053 int 2054 audio_mixer_poll(struct audio_softc *sc, int events, struct proc *p) 2055 { 2056 int revents = 0; 2057 2058 mtx_enter(&audio_lock); 2059 if (sc->mix_isopen && sc->mix_pending) 2060 revents |= events & (POLLIN | POLLRDNORM); 2061 if (revents == 0) { 2062 if (events & (POLLIN | POLLRDNORM)) 2063 selrecord(p, &sc->mix_sel); 2064 } 2065 mtx_leave(&audio_lock); 2066 return revents; 2067 } 2068 2069 int 2070 audio_mixer_open(struct audio_softc *sc, int flags) 2071 { 2072 DPRINTF("%s: flags = 0x%x\n", __func__, flags); 2073 2074 if (flags & FREAD) { 2075 if (sc->mix_isopen) 2076 return EBUSY; 2077 sc->mix_isopen = 1; 2078 } 2079 return 0; 2080 } 2081 2082 int 2083 audio_mixer_close(struct audio_softc *sc, int flags) 2084 { 2085 int i; 2086 2087 DPRINTF("%s: flags = 0x%x\n", __func__, flags); 2088 2089 if (flags & FREAD) { 2090 sc->mix_isopen = 0; 2091 2092 mtx_enter(&audio_lock); 2093 sc->mix_pending = NULL; 2094 for (i = 0; i < sc->mix_nent; i++) 2095 sc->mix_evbuf[i].pending = 0; 2096 mtx_leave(&audio_lock); 2097 } 2098 return 0; 2099 } 2100 2101 int 2102 audio_poll(struct audio_softc *sc, int events, struct proc *p) 2103 { 2104 int revents = 0; 2105 2106 mtx_enter(&audio_lock); 2107 if ((sc->mode & AUMODE_RECORD) && sc->rec.used > 0) 2108 revents |= events & (POLLIN | POLLRDNORM); 2109 if ((sc->mode & AUMODE_PLAY) && sc->play.used < sc->play.len) 2110 revents |= events & (POLLOUT | POLLWRNORM); 2111 if (revents == 0) { 2112 if (events & (POLLIN | POLLRDNORM)) 2113 selrecord(p, &sc->rec.sel); 2114 if (events & (POLLOUT | POLLWRNORM)) 2115 selrecord(p, &sc->play.sel); 2116 } 2117 mtx_leave(&audio_lock); 2118 return revents; 2119 } 2120 2121 int 2122 audioopen(dev_t dev, int flags, int mode, struct proc *p) 2123 { 2124 struct audio_softc *sc; 2125 int error; 2126 2127 sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev)); 2128 if (sc == NULL) 2129 return ENXIO; 2130 if (sc->ops == NULL) 2131 error = ENXIO; 2132 else { 2133 switch (AUDIO_DEV(dev)) { 2134 case AUDIO_DEV_AUDIO: 2135 error = audio_open(sc, flags); 2136 break; 2137 case AUDIO_DEV_AUDIOCTL: 2138 error = audio_mixer_open(sc, flags); 2139 break; 2140 default: 2141 error = ENXIO; 2142 } 2143 } 2144 device_unref(&sc->dev); 2145 return error; 2146 } 2147 2148 int 2149 audioclose(dev_t dev, int flags, int ifmt, struct proc *p) 2150 { 2151 struct audio_softc *sc; 2152 int error; 2153 2154 sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev)); 2155 if (sc == NULL) 2156 return ENXIO; 2157 switch (AUDIO_DEV(dev)) { 2158 case AUDIO_DEV_AUDIO: 2159 error = audio_close(sc); 2160 break; 2161 case AUDIO_DEV_AUDIOCTL: 2162 error = audio_mixer_close(sc, flags); 2163 break; 2164 default: 2165 error = ENXIO; 2166 } 2167 device_unref(&sc->dev); 2168 return error; 2169 } 2170 2171 int 2172 audioread(dev_t dev, struct uio *uio, int ioflag) 2173 { 2174 struct audio_softc *sc; 2175 int error; 2176 2177 sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev)); 2178 if (sc == NULL) 2179 return ENXIO; 2180 switch (AUDIO_DEV(dev)) { 2181 case AUDIO_DEV_AUDIO: 2182 error = audio_read(sc, uio, ioflag); 2183 break; 2184 case AUDIO_DEV_AUDIOCTL: 2185 error = audio_mixer_read(sc, uio, ioflag); 2186 break; 2187 default: 2188 error = ENXIO; 2189 } 2190 device_unref(&sc->dev); 2191 return error; 2192 } 2193 2194 int 2195 audiowrite(dev_t dev, struct uio *uio, int ioflag) 2196 { 2197 struct audio_softc *sc; 2198 int error; 2199 2200 sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev)); 2201 if (sc == NULL) 2202 return ENXIO; 2203 switch (AUDIO_DEV(dev)) { 2204 case AUDIO_DEV_AUDIO: 2205 error = audio_write(sc, uio, ioflag); 2206 break; 2207 case AUDIO_DEV_AUDIOCTL: 2208 error = ENODEV; 2209 break; 2210 default: 2211 error = ENXIO; 2212 } 2213 device_unref(&sc->dev); 2214 return error; 2215 } 2216 2217 int 2218 audioioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 2219 { 2220 struct audio_softc *sc; 2221 int error; 2222 2223 sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev)); 2224 if (sc == NULL) 2225 return ENXIO; 2226 switch (AUDIO_DEV(dev)) { 2227 case AUDIO_DEV_AUDIO: 2228 error = audio_ioctl(sc, cmd, addr); 2229 break; 2230 case AUDIO_DEV_AUDIOCTL: 2231 if (cmd == AUDIO_SETPAR && sc->mode != 0) { 2232 error = EBUSY; 2233 break; 2234 } 2235 if (cmd == AUDIO_START || cmd == AUDIO_STOP) { 2236 error = ENXIO; 2237 break; 2238 } 2239 if (cmd == AUDIO_MIXER_DEVINFO || 2240 cmd == AUDIO_MIXER_READ || 2241 cmd == AUDIO_MIXER_WRITE) 2242 error = audio_ioctl_mixer(sc, cmd, addr, p); 2243 else 2244 error = audio_ioctl(sc, cmd, addr); 2245 break; 2246 default: 2247 error = ENXIO; 2248 } 2249 device_unref(&sc->dev); 2250 return error; 2251 } 2252 2253 int 2254 audiopoll(dev_t dev, int events, struct proc *p) 2255 { 2256 struct audio_softc *sc; 2257 int revents; 2258 2259 sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev)); 2260 if (sc == NULL) 2261 return POLLERR; 2262 switch (AUDIO_DEV(dev)) { 2263 case AUDIO_DEV_AUDIO: 2264 revents = audio_poll(sc, events, p); 2265 break; 2266 case AUDIO_DEV_AUDIOCTL: 2267 revents = audio_mixer_poll(sc, events, p); 2268 break; 2269 default: 2270 revents = 0; 2271 break; 2272 } 2273 device_unref(&sc->dev); 2274 return revents; 2275 } 2276 2277 int 2278 audiokqfilter(dev_t dev, struct knote *kn) 2279 { 2280 struct audio_softc *sc; 2281 struct klist *klist; 2282 int error; 2283 2284 sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev)); 2285 if (sc == NULL) 2286 return ENXIO; 2287 error = 0; 2288 switch (AUDIO_DEV(dev)) { 2289 case AUDIO_DEV_AUDIO: 2290 switch (kn->kn_filter) { 2291 case EVFILT_READ: 2292 klist = &sc->rec.sel.si_note; 2293 kn->kn_fop = &audioread_filtops; 2294 break; 2295 case EVFILT_WRITE: 2296 klist = &sc->play.sel.si_note; 2297 kn->kn_fop = &audiowrite_filtops; 2298 break; 2299 default: 2300 error = EINVAL; 2301 goto done; 2302 } 2303 break; 2304 case AUDIO_DEV_AUDIOCTL: 2305 switch (kn->kn_filter) { 2306 case EVFILT_READ: 2307 klist = &sc->mix_sel.si_note; 2308 kn->kn_fop = &audioctlread_filtops; 2309 break; 2310 default: 2311 error = EINVAL; 2312 goto done; 2313 } 2314 break; 2315 } 2316 kn->kn_hook = sc; 2317 2318 klist_insert(klist, kn); 2319 done: 2320 device_unref(&sc->dev); 2321 return error; 2322 } 2323 2324 void 2325 filt_audiordetach(struct knote *kn) 2326 { 2327 struct audio_softc *sc = kn->kn_hook; 2328 2329 klist_remove(&sc->rec.sel.si_note, kn); 2330 } 2331 2332 int 2333 filt_audioread(struct knote *kn, long hint) 2334 { 2335 struct audio_softc *sc = kn->kn_hook; 2336 2337 MUTEX_ASSERT_LOCKED(&audio_lock); 2338 2339 return (sc->mode & AUMODE_RECORD) && (sc->rec.used > 0); 2340 } 2341 2342 void 2343 filt_audiowdetach(struct knote *kn) 2344 { 2345 struct audio_softc *sc = kn->kn_hook; 2346 2347 klist_remove(&sc->play.sel.si_note, kn); 2348 } 2349 2350 int 2351 filt_audiowrite(struct knote *kn, long hint) 2352 { 2353 struct audio_softc *sc = kn->kn_hook; 2354 2355 MUTEX_ASSERT_LOCKED(&audio_lock); 2356 2357 return (sc->mode & AUMODE_PLAY) && (sc->play.used < sc->play.len); 2358 } 2359 2360 void 2361 filt_audioctlrdetach(struct knote *kn) 2362 { 2363 struct audio_softc *sc = kn->kn_hook; 2364 2365 klist_remove(&sc->mix_sel.si_note, kn); 2366 } 2367 2368 int 2369 filt_audioctlread(struct knote *kn, long hint) 2370 { 2371 struct audio_softc *sc = kn->kn_hook; 2372 2373 MUTEX_ASSERT_LOCKED(&audio_lock); 2374 2375 return (sc->mix_isopen && sc->mix_pending); 2376 } 2377 2378 int 2379 filt_audiomodify(struct kevent *kev, struct knote *kn) 2380 { 2381 int active; 2382 2383 mtx_enter(&audio_lock); 2384 active = knote_modify(kev, kn); 2385 mtx_leave(&audio_lock); 2386 2387 return active; 2388 } 2389 2390 int 2391 filt_audioprocess(struct knote *kn, struct kevent *kev) 2392 { 2393 int active; 2394 2395 mtx_enter(&audio_lock); 2396 active = knote_process(kn, kev); 2397 mtx_leave(&audio_lock); 2398 2399 return active; 2400 } 2401 2402 #if NWSKBD > 0 2403 int 2404 wskbd_initmute(struct audio_softc *sc, struct mixer_devinfo *vol) 2405 { 2406 struct mixer_devinfo *mi; 2407 int index = -1; 2408 2409 mi = malloc(sizeof(struct mixer_devinfo), M_TEMP, M_WAITOK); 2410 2411 for (mi->index = vol->next; mi->index != -1; mi->index = mi->next) { 2412 if (sc->ops->query_devinfo(sc->arg, mi) != 0) 2413 break; 2414 if (strcmp(mi->label.name, AudioNmute) == 0) { 2415 index = mi->index; 2416 break; 2417 } 2418 } 2419 2420 free(mi, M_TEMP, sizeof(struct mixer_devinfo)); 2421 return index; 2422 } 2423 2424 int 2425 wskbd_initvol(struct audio_softc *sc, struct wskbd_vol *vol, char *cn, char *dn) 2426 { 2427 struct mixer_devinfo *dev, *cls; 2428 2429 vol->val = vol->mute = -1; 2430 dev = malloc(sizeof(struct mixer_devinfo), M_TEMP, M_WAITOK); 2431 cls = malloc(sizeof(struct mixer_devinfo), M_TEMP, M_WAITOK); 2432 2433 for (dev->index = 0; ; dev->index++) { 2434 if (sc->ops->query_devinfo(sc->arg, dev) != 0) 2435 break; 2436 if (dev->type != AUDIO_MIXER_VALUE) 2437 continue; 2438 cls->index = dev->mixer_class; 2439 if (sc->ops->query_devinfo(sc->arg, cls) != 0) 2440 continue; 2441 if (strcmp(cls->label.name, cn) == 0 && 2442 strcmp(dev->label.name, dn) == 0) { 2443 vol->val = dev->index; 2444 vol->nch = dev->un.v.num_channels; 2445 vol->step = dev->un.v.delta > 8 ? dev->un.v.delta : 8; 2446 vol->mute = wskbd_initmute(sc, dev); 2447 vol->val_pending = vol->mute_pending = 0; 2448 DPRINTF("%s: wskbd using %s.%s%s\n", DEVNAME(sc), 2449 cn, dn, vol->mute >= 0 ? ", mute control" : ""); 2450 break; 2451 } 2452 } 2453 2454 free(cls, M_TEMP, sizeof(struct mixer_devinfo)); 2455 free(dev, M_TEMP, sizeof(struct mixer_devinfo)); 2456 return (vol->val != -1); 2457 } 2458 2459 void 2460 wskbd_mixer_init(struct audio_softc *sc) 2461 { 2462 static struct { 2463 char *cn, *dn; 2464 } spkr_names[] = { 2465 {AudioCoutputs, AudioNmaster}, 2466 {AudioCinputs, AudioNdac}, 2467 {AudioCoutputs, AudioNdac}, 2468 {AudioCoutputs, AudioNoutput} 2469 }, mic_names[] = { 2470 {AudioCrecord, AudioNrecord}, 2471 {AudioCrecord, AudioNvolume}, 2472 {AudioCinputs, AudioNrecord}, 2473 {AudioCinputs, AudioNvolume}, 2474 {AudioCinputs, AudioNinput} 2475 }; 2476 int i; 2477 2478 for (i = 0; i < sizeof(spkr_names) / sizeof(spkr_names[0]); i++) { 2479 if (wskbd_initvol(sc, &sc->spkr, 2480 spkr_names[i].cn, spkr_names[i].dn)) 2481 break; 2482 } 2483 for (i = 0; i < sizeof(mic_names) / sizeof(mic_names[0]); i++) { 2484 if (wskbd_initvol(sc, &sc->mic, 2485 mic_names[i].cn, mic_names[i].dn)) 2486 break; 2487 } 2488 task_set(&sc->wskbd_task, wskbd_mixer_cb, sc); 2489 } 2490 2491 void 2492 wskbd_mixer_update(struct audio_softc *sc, struct wskbd_vol *vol) 2493 { 2494 struct mixer_ctrl ctrl; 2495 int val_pending, mute_pending, i, gain, error, s; 2496 2497 s = spltty(); 2498 val_pending = vol->val_pending; 2499 vol->val_pending = 0; 2500 mute_pending = vol->mute_pending; 2501 vol->mute_pending = 0; 2502 splx(s); 2503 2504 if (sc->ops == NULL) 2505 return; 2506 if (vol->mute >= 0 && mute_pending) { 2507 ctrl.dev = vol->mute; 2508 ctrl.type = AUDIO_MIXER_ENUM; 2509 error = sc->ops->get_port(sc->arg, &ctrl); 2510 if (error) { 2511 DPRINTF("%s: get mute err = %d\n", DEVNAME(sc), error); 2512 return; 2513 } 2514 switch (mute_pending) { 2515 case WSKBD_MUTE_TOGGLE: 2516 ctrl.un.ord = !ctrl.un.ord; 2517 break; 2518 case WSKBD_MUTE_DISABLE: 2519 ctrl.un.ord = 0; 2520 break; 2521 case WSKBD_MUTE_ENABLE: 2522 ctrl.un.ord = 1; 2523 break; 2524 } 2525 DPRINTFN(1, "%s: wskbd mute setting to %d\n", 2526 DEVNAME(sc), ctrl.un.ord); 2527 error = sc->ops->set_port(sc->arg, &ctrl); 2528 if (error) { 2529 DPRINTF("%s: set mute err = %d\n", DEVNAME(sc), error); 2530 return; 2531 } 2532 audio_event(sc, vol->mute); 2533 } 2534 if (vol->val >= 0 && val_pending) { 2535 ctrl.dev = vol->val; 2536 ctrl.type = AUDIO_MIXER_VALUE; 2537 ctrl.un.value.num_channels = vol->nch; 2538 error = sc->ops->get_port(sc->arg, &ctrl); 2539 if (error) { 2540 DPRINTF("%s: get mute err = %d\n", DEVNAME(sc), error); 2541 return; 2542 } 2543 for (i = 0; i < vol->nch; i++) { 2544 gain = ctrl.un.value.level[i] + vol->step * val_pending; 2545 if (gain > AUDIO_MAX_GAIN) 2546 gain = AUDIO_MAX_GAIN; 2547 else if (gain < AUDIO_MIN_GAIN) 2548 gain = AUDIO_MIN_GAIN; 2549 ctrl.un.value.level[i] = gain; 2550 DPRINTFN(1, "%s: wskbd level %d set to %d\n", 2551 DEVNAME(sc), i, gain); 2552 } 2553 error = sc->ops->set_port(sc->arg, &ctrl); 2554 if (error) { 2555 DPRINTF("%s: set vol err = %d\n", DEVNAME(sc), error); 2556 return; 2557 } 2558 audio_event(sc, vol->val); 2559 } 2560 } 2561 2562 void 2563 wskbd_mixer_cb(void *arg) 2564 { 2565 struct audio_softc *sc = arg; 2566 2567 wskbd_mixer_update(sc, &sc->spkr); 2568 wskbd_mixer_update(sc, &sc->mic); 2569 device_unref(&sc->dev); 2570 } 2571 2572 int 2573 wskbd_set_mixermute(long mute, long out) 2574 { 2575 struct audio_softc *sc; 2576 struct wskbd_vol *vol; 2577 2578 sc = (struct audio_softc *)device_lookup(&audio_cd, 0); 2579 if (sc == NULL) 2580 return ENODEV; 2581 vol = out ? &sc->spkr : &sc->mic; 2582 vol->mute_pending = mute ? WSKBD_MUTE_ENABLE : WSKBD_MUTE_DISABLE; 2583 if (!task_add(systq, &sc->wskbd_task)) 2584 device_unref(&sc->dev); 2585 return 0; 2586 } 2587 2588 /* 2589 * Adjust the volume of the audio device associated with the given cookie. 2590 * Otherwise, fallback to audio0. 2591 */ 2592 int 2593 wskbd_set_mixervolume_dev(void *cookie, long dir, long out) 2594 { 2595 int unit = 0; 2596 int i; 2597 2598 for (i = 0; i < audio_cd.cd_ndevs; i++) { 2599 struct audio_softc *sc; 2600 2601 sc = (struct audio_softc *)device_lookup(&audio_cd, i); 2602 if (sc == NULL) 2603 continue; 2604 if (sc->cookie != cookie) { 2605 device_unref(&sc->dev); 2606 continue; 2607 } 2608 2609 device_unref(&sc->dev); 2610 unit = i; 2611 break; 2612 } 2613 2614 return wskbd_set_mixervolume_unit(unit, dir, out); 2615 } 2616 2617 int 2618 wskbd_set_mixervolume(long dir, long out) 2619 { 2620 return wskbd_set_mixervolume_unit(0, dir, out); 2621 } 2622 2623 int 2624 wskbd_set_mixervolume_unit(int unit, long dir, long out) 2625 { 2626 struct audio_softc *sc; 2627 struct wskbd_vol *vol; 2628 2629 sc = (struct audio_softc *)device_lookup(&audio_cd, unit); 2630 if (sc == NULL) 2631 return ENODEV; 2632 vol = out ? &sc->spkr : &sc->mic; 2633 if (dir == 0) 2634 vol->mute_pending ^= WSKBD_MUTE_TOGGLE; 2635 else 2636 vol->val_pending += dir; 2637 if (!task_add(systq, &sc->wskbd_task)) 2638 device_unref(&sc->dev); 2639 return 0; 2640 } 2641 #endif /* NWSKBD > 0 */ 2642