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