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