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