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