1/* $NetBSD: audio.c,v 1.257.2.3 2012/05/07 03:03:18 riz Exp $ */ 2 3/*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32/* 33 * Copyright (c) 1991-1993 Regents of the University of California. 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. All advertising materials mentioning features or use of this software 45 * must display the following acknowledgement: 46 * This product includes software developed by the Computer Systems 47 * Engineering Group at Lawrence Berkeley Laboratory. 48 * 4. Neither the name of the University nor of the Laboratory may be used 49 * to endorse or promote products derived from this software without 50 * specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 */ 64 65/* 66 * This is a (partially) SunOS-compatible /dev/audio driver for NetBSD. 67 * 68 * This code tries to do something half-way sensible with 69 * half-duplex hardware, such as with the SoundBlaster hardware. With 70 * half-duplex hardware allowing O_RDWR access doesn't really make 71 * sense. However, closing and opening the device to "turn around the 72 * line" is relatively expensive and costs a card reset (which can 73 * take some time, at least for the SoundBlaster hardware). Instead 74 * we allow O_RDWR access, and provide an ioctl to set the "mode", 75 * i.e. playing or recording. 76 * 77 * If you write to a half-duplex device in record mode, the data is 78 * tossed. If you read from the device in play mode, you get silence 79 * filled buffers at the rate at which samples are naturally 80 * generated. 81 * 82 * If you try to set both play and record mode on a half-duplex 83 * device, playing takes precedence. 84 */ 85 86/* 87 * Locking: there are three locks. 88 * 89 * - sc_lock, provided by the underlying driver. This is an adaptive lock, 90 * returned in the second parameter to hw_if->get_locks(). It is known 91 * as the "thread lock". 92 * 93 * It serializes access to state in all places except the 94 * driver's interrupt service routine. This lock is taken from process 95 * context (example: access to /dev/audio). It is also taken from soft 96 * interrupt handlers in this module, primarily to serialize delivery of 97 * wakeups. This lock may be used/provided by modules external to the 98 * audio subsystem, so take care not to introduce a lock order problem. 99 * LONG TERM SLEEPS MUST NOT OCCUR WITH THIS LOCK HELD. 100 * 101 * - sc_intr_lock, provided by the underlying driver. This may be either a 102 * spinlock (at IPL_SCHED or IPL_VM) or an adaptive lock (IPL_NONE), 103 * returned in the first parameter to hw_if->get_locks(). It is known as 104 * the "interrupt lock". 105 * 106 * It provides atomic access to the device's hardware state, and to audio 107 * channel data that may be accessed by the hardware driver's ISR. 108 * In all places outside the ISR, sc_lock must be held before taking 109 * sc_intr_lock. This is to ensure that groups of hardware operations are 110 * made atomically. SLEEPS CANNOT OCCUR WITH THIS LOCK HELD. 111 * 112 * - sc_dvlock, private to this module. This is a custom reader/writer lock 113 * built on sc_lock and a condition variable. Some operations release 114 * sc_lock in order to allocate memory, to wait for in-flight I/O to 115 * complete, to copy to/from user context, etc. sc_dvlock serializes 116 * changes to filters and audio device settings while a read/write to the 117 * hardware is in progress. A write lock is taken only under exceptional 118 * circumstances, for example when opening /dev/audio or changing audio 119 * parameters. Long term sleeps and copy to/from user space may be done 120 * with this lock held. 121 * 122 * List of hardware interface methods, and which locks are held when each 123 * is called by this module: 124 * 125 * METHOD INTR THREAD NOTES 126 * ----------------------- ------- ------- ------------------------- 127 * open x x 128 * close x x 129 * drain x x 130 * query_encoding - x 131 * set_params - x 132 * round_blocksize - x 133 * commit_settings - x 134 * init_output x x 135 * init_input x x 136 * start_output x x 137 * start_input x x 138 * halt_output x x 139 * halt_input x x 140 * speaker_ctl x x 141 * getdev - x 142 * setfd - x 143 * set_port - x 144 * get_port - x 145 * query_devinfo - x 146 * allocm - - Called at attach time 147 * freem - - Called at attach time 148 * round_buffersize - x 149 * mappage - - Mem. unchanged after attach 150 * get_props - x 151 * trigger_output x x 152 * trigger_input x x 153 * dev_ioctl - x 154 * get_locks - - Called at attach time 155 */ 156 157#include <sys/cdefs.h> 158__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.257.2.3 2012/05/07 03:03:18 riz Exp $"); 159 160#include "audio.h" 161#if NAUDIO > 0 162 163#include <sys/param.h> 164#include <sys/ioctl.h> 165#include <sys/fcntl.h> 166#include <sys/vnode.h> 167#include <sys/select.h> 168#include <sys/poll.h> 169#include <sys/kmem.h> 170#include <sys/malloc.h> 171#include <sys/proc.h> 172#include <sys/systm.h> 173#include <sys/syslog.h> 174#include <sys/kernel.h> 175#include <sys/signalvar.h> 176#include <sys/conf.h> 177#include <sys/audioio.h> 178#include <sys/device.h> 179#include <sys/intr.h> 180#include <sys/cpu.h> 181 182#include <dev/audio_if.h> 183#include <dev/audiovar.h> 184 185#include <machine/endian.h> 186 187/* #define AUDIO_DEBUG 1 */ 188#ifdef AUDIO_DEBUG 189#define DPRINTF(x) if (audiodebug) printf x 190#define DPRINTFN(n,x) if (audiodebug>(n)) printf x 191int audiodebug = AUDIO_DEBUG; 192#else 193#define DPRINTF(x) 194#define DPRINTFN(n,x) 195#endif 196 197#define ROUNDSIZE(x) x &= -16 /* round to nice boundary */ 198#define SPECIFIED(x) (x != ~0) 199#define SPECIFIED_CH(x) (x != (u_char)~0) 200 201/* #define AUDIO_PM_IDLE */ 202#ifdef AUDIO_PM_IDLE 203int audio_idle_timeout = 30; 204#endif 205 206int audio_blk_ms = AUDIO_BLK_MS; 207 208int audiosetinfo(struct audio_softc *, struct audio_info *); 209int audiogetinfo(struct audio_softc *, struct audio_info *, int); 210 211int audio_open(dev_t, struct audio_softc *, int, int, struct lwp *); 212int audio_close(struct audio_softc *, int, int, struct lwp *); 213int audio_read(struct audio_softc *, struct uio *, int); 214int audio_write(struct audio_softc *, struct uio *, int); 215int audio_ioctl(struct audio_softc *, u_long, void *, int, struct lwp *); 216int audio_poll(struct audio_softc *, int, struct lwp *); 217int audio_kqfilter(struct audio_softc *, struct knote *); 218paddr_t audio_mmap(struct audio_softc *, off_t, int); 219 220int mixer_open(dev_t, struct audio_softc *, int, int, struct lwp *); 221int mixer_close(struct audio_softc *, int, int, struct lwp *); 222int mixer_ioctl(struct audio_softc *, u_long, void *, int, struct lwp *); 223static void mixer_remove(struct audio_softc *); 224static void mixer_signal(struct audio_softc *); 225 226void audio_init_record(struct audio_softc *); 227void audio_init_play(struct audio_softc *); 228int audiostartr(struct audio_softc *); 229int audiostartp(struct audio_softc *); 230void audio_rint(void *); 231void audio_pint(void *); 232int audio_check_params(struct audio_params *); 233 234void audio_calc_blksize(struct audio_softc *, int); 235void audio_fill_silence(struct audio_params *, uint8_t *, int); 236int audio_silence_copyout(struct audio_softc *, int, struct uio *); 237 238void audio_init_ringbuffer(struct audio_softc *, 239 struct audio_ringbuffer *, int); 240int audio_initbufs(struct audio_softc *); 241void audio_calcwater(struct audio_softc *); 242int audio_drain(struct audio_softc *); 243void audio_clear(struct audio_softc *); 244void audio_clear_intr_unlocked(struct audio_softc *sc); 245static inline void audio_pint_silence 246 (struct audio_softc *, struct audio_ringbuffer *, uint8_t *, int); 247 248int audio_alloc_ring 249 (struct audio_softc *, struct audio_ringbuffer *, int, size_t); 250void audio_free_ring(struct audio_softc *, struct audio_ringbuffer *); 251static int audio_setup_pfilters(struct audio_softc *, const audio_params_t *, 252 stream_filter_list_t *); 253static int audio_setup_rfilters(struct audio_softc *, const audio_params_t *, 254 stream_filter_list_t *); 255static void audio_stream_dtor(audio_stream_t *); 256static int audio_stream_ctor(audio_stream_t *, const audio_params_t *, int); 257static void stream_filter_list_append 258 (stream_filter_list_t *, stream_filter_factory_t, 259 const audio_params_t *); 260static void stream_filter_list_prepend 261 (stream_filter_list_t *, stream_filter_factory_t, 262 const audio_params_t *); 263static void stream_filter_list_set 264 (stream_filter_list_t *, int, stream_filter_factory_t, 265 const audio_params_t *); 266int audio_set_defaults(struct audio_softc *, u_int); 267 268int audioprobe(device_t, cfdata_t, void *); 269void audioattach(device_t, device_t, void *); 270int audiodetach(device_t, int); 271int audioactivate(device_t, enum devact); 272 273#ifdef AUDIO_PM_IDLE 274static void audio_idle(void *); 275static void audio_activity(device_t, devactive_t); 276#endif 277 278static bool audio_suspend(device_t dv, const pmf_qual_t *); 279static bool audio_resume(device_t dv, const pmf_qual_t *); 280static void audio_volume_down(device_t); 281static void audio_volume_up(device_t); 282static void audio_volume_toggle(device_t); 283 284static void audio_mixer_capture(struct audio_softc *); 285static void audio_mixer_restore(struct audio_softc *); 286 287static int audio_get_props(struct audio_softc *); 288static bool audio_can_playback(struct audio_softc *); 289static bool audio_can_capture(struct audio_softc *); 290 291static void audio_softintr_rd(void *); 292static void audio_softintr_wr(void *); 293 294static int audio_enter(dev_t, krw_t, struct audio_softc **); 295static void audio_exit(struct audio_softc *); 296static int audio_waitio(struct audio_softc *, kcondvar_t *); 297 298struct portname { 299 const char *name; 300 int mask; 301}; 302static const struct portname itable[] = { 303 { AudioNmicrophone, AUDIO_MICROPHONE }, 304 { AudioNline, AUDIO_LINE_IN }, 305 { AudioNcd, AUDIO_CD }, 306 { 0, 0 } 307}; 308static const struct portname otable[] = { 309 { AudioNspeaker, AUDIO_SPEAKER }, 310 { AudioNheadphone, AUDIO_HEADPHONE }, 311 { AudioNline, AUDIO_LINE_OUT }, 312 { 0, 0 } 313}; 314void au_setup_ports(struct audio_softc *, struct au_mixer_ports *, 315 mixer_devinfo_t *, const struct portname *); 316int au_set_gain(struct audio_softc *, struct au_mixer_ports *, 317 int, int); 318void au_get_gain(struct audio_softc *, struct au_mixer_ports *, 319 u_int *, u_char *); 320int au_set_port(struct audio_softc *, struct au_mixer_ports *, 321 u_int); 322int au_get_port(struct audio_softc *, struct au_mixer_ports *); 323int au_get_lr_value(struct audio_softc *, mixer_ctrl_t *, 324 int *, int *); 325int au_set_lr_value(struct audio_softc *, mixer_ctrl_t *, 326 int, int); 327int au_portof(struct audio_softc *, char *, int); 328 329typedef struct uio_fetcher { 330 stream_fetcher_t base; 331 struct uio *uio; 332 int usedhigh; 333 int last_used; 334} uio_fetcher_t; 335 336static void uio_fetcher_ctor(uio_fetcher_t *, struct uio *, int); 337static int uio_fetcher_fetch_to(struct audio_softc *, stream_fetcher_t *, 338 audio_stream_t *, int); 339static int null_fetcher_fetch_to(struct audio_softc *, stream_fetcher_t *, 340 audio_stream_t *, int); 341 342dev_type_open(audioopen); 343dev_type_close(audioclose); 344dev_type_read(audioread); 345dev_type_write(audiowrite); 346dev_type_ioctl(audioioctl); 347dev_type_poll(audiopoll); 348dev_type_mmap(audiommap); 349dev_type_kqfilter(audiokqfilter); 350 351const struct cdevsw audio_cdevsw = { 352 audioopen, audioclose, audioread, audiowrite, audioioctl, 353 nostop, notty, audiopoll, audiommap, audiokqfilter, D_OTHER | D_MPSAFE 354}; 355 356/* The default audio mode: 8 kHz mono mu-law */ 357const struct audio_params audio_default = { 358 .sample_rate = 8000, 359 .encoding = AUDIO_ENCODING_ULAW, 360 .precision = 8, 361 .validbits = 8, 362 .channels = 1, 363}; 364 365CFATTACH_DECL3_NEW(audio, sizeof(struct audio_softc), 366 audioprobe, audioattach, audiodetach, audioactivate, NULL, NULL, 367 DVF_DETACH_SHUTDOWN); 368 369extern struct cfdriver audio_cd; 370 371int 372audioprobe(device_t parent, cfdata_t match, void *aux) 373{ 374 struct audio_attach_args *sa; 375 376 sa = aux; 377 DPRINTF(("audioprobe: type=%d sa=%p hw=%p\n", 378 sa->type, sa, sa->hwif)); 379 return (sa->type == AUDIODEV_TYPE_AUDIO) ? 1 : 0; 380} 381 382void 383audioattach(device_t parent, device_t self, void *aux) 384{ 385 struct audio_softc *sc; 386 struct audio_attach_args *sa; 387 const struct audio_hw_if *hwp; 388 void *hdlp; 389 int error; 390 mixer_devinfo_t mi; 391 int iclass, mclass, oclass, rclass, props; 392 int record_master_found, record_source_found; 393 bool can_capture, can_playback; 394 395 sc = device_private(self); 396 sc->dev = self; 397 sa = aux; 398 hwp = sa->hwif; 399 hdlp = sa->hdl; 400 401 cv_init(&sc->sc_rchan, "audiord"); 402 cv_init(&sc->sc_wchan, "audiowr"); 403 cv_init(&sc->sc_lchan, "audiolk"); 404 405 if (hwp == 0 || hwp->get_locks == 0) { 406 printf(": missing method\n"); 407 panic("audioattach"); 408 } 409 410 hwp->get_locks(hdlp, &sc->sc_intr_lock, &sc->sc_lock); 411 412#ifdef DIAGNOSTIC 413 if (hwp->query_encoding == 0 || 414 hwp->set_params == 0 || 415 (hwp->start_output == 0 && hwp->trigger_output == 0) || 416 (hwp->start_input == 0 && hwp->trigger_input == 0) || 417 hwp->halt_output == 0 || 418 hwp->halt_input == 0 || 419 hwp->getdev == 0 || 420 hwp->set_port == 0 || 421 hwp->get_port == 0 || 422 hwp->query_devinfo == 0 || 423 hwp->get_props == 0) { 424 printf(": missing method\n"); 425 sc->hw_if = 0; 426 return; 427 } 428#endif 429 430 sc->hw_if = hwp; 431 sc->hw_hdl = hdlp; 432 sc->sc_dev = parent; 433 sc->sc_lastinfovalid = false; 434 435 mutex_enter(sc->sc_lock); 436 props = audio_get_props(sc); 437 mutex_exit(sc->sc_lock); 438 439 if (props & AUDIO_PROP_FULLDUPLEX) 440 aprint_normal(": full duplex"); 441 else 442 aprint_normal(": half duplex"); 443 444 if (props & AUDIO_PROP_PLAYBACK) 445 aprint_normal(", playback"); 446 if (props & AUDIO_PROP_CAPTURE) 447 aprint_normal(", capture"); 448 if (props & AUDIO_PROP_MMAP) 449 aprint_normal(", mmap"); 450 if (props & AUDIO_PROP_INDEPENDENT) 451 aprint_normal(", independent"); 452 453 aprint_naive("\n"); 454 aprint_normal("\n"); 455 456 mutex_enter(sc->sc_lock); 457 can_playback = audio_can_playback(sc); 458 can_capture = audio_can_capture(sc); 459 mutex_exit(sc->sc_lock); 460 461 if (can_playback) { 462 error = audio_alloc_ring(sc, &sc->sc_pr, 463 AUMODE_PLAY, AU_RING_SIZE); 464 if (error) { 465 sc->hw_if = NULL; 466 aprint_error("audio: could not allocate play buffer\n"); 467 return; 468 } 469 } 470 if (can_capture) { 471 error = audio_alloc_ring(sc, &sc->sc_rr, 472 AUMODE_RECORD, AU_RING_SIZE); 473 if (error) { 474 if (sc->sc_pr.s.start != 0) 475 audio_free_ring(sc, &sc->sc_pr); 476 sc->hw_if = NULL; 477 aprint_error("audio: could not allocate record buffer\n"); 478 return; 479 } 480 } 481 482 sc->sc_lastgain = 128; 483 484 mutex_enter(sc->sc_lock); 485 error = audio_set_defaults(sc, 0); 486 mutex_exit(sc->sc_lock); 487 if (error != 0) { 488 aprint_error("audioattach: audio_set_defaults() failed\n"); 489 sc->hw_if = NULL; 490 return; 491 } 492 493 sc->sc_sih_rd = softint_establish(SOFTINT_SERIAL | SOFTINT_MPSAFE, 494 audio_softintr_rd, sc); 495 sc->sc_sih_wr = softint_establish(SOFTINT_SERIAL | SOFTINT_MPSAFE, 496 audio_softintr_wr, sc); 497 498 iclass = mclass = oclass = rclass = -1; 499 sc->sc_inports.index = -1; 500 sc->sc_inports.master = -1; 501 sc->sc_inports.nports = 0; 502 sc->sc_inports.isenum = false; 503 sc->sc_inports.allports = 0; 504 sc->sc_inports.isdual = false; 505 sc->sc_inports.mixerout = -1; 506 sc->sc_inports.cur_port = -1; 507 sc->sc_outports.index = -1; 508 sc->sc_outports.master = -1; 509 sc->sc_outports.nports = 0; 510 sc->sc_outports.isenum = false; 511 sc->sc_outports.allports = 0; 512 sc->sc_outports.isdual = false; 513 sc->sc_outports.mixerout = -1; 514 sc->sc_outports.cur_port = -1; 515 sc->sc_monitor_port = -1; 516 /* 517 * Read through the underlying driver's list, picking out the class 518 * names from the mixer descriptions. We'll need them to decode the 519 * mixer descriptions on the next pass through the loop. 520 */ 521 mutex_enter(sc->sc_lock); 522 for(mi.index = 0; ; mi.index++) { 523 if (hwp->query_devinfo(hdlp, &mi) != 0) 524 break; 525 /* 526 * The type of AUDIO_MIXER_CLASS merely introduces a class. 527 * All the other types describe an actual mixer. 528 */ 529 if (mi.type == AUDIO_MIXER_CLASS) { 530 if (strcmp(mi.label.name, AudioCinputs) == 0) 531 iclass = mi.mixer_class; 532 if (strcmp(mi.label.name, AudioCmonitor) == 0) 533 mclass = mi.mixer_class; 534 if (strcmp(mi.label.name, AudioCoutputs) == 0) 535 oclass = mi.mixer_class; 536 if (strcmp(mi.label.name, AudioCrecord) == 0) 537 rclass = mi.mixer_class; 538 } 539 } 540 mutex_exit(sc->sc_lock); 541 542 /* Allocate save area. Ensure non-zero allocation. */ 543 sc->sc_nmixer_states = mi.index; 544 sc->sc_mixer_state = kmem_alloc(sizeof(mixer_ctrl_t) * 545 sc->sc_nmixer_states + 1, KM_SLEEP); 546 547 /* 548 * This is where we assign each control in the "audio" model, to the 549 * underlying "mixer" control. We walk through the whole list once, 550 * assigning likely candidates as we come across them. 551 */ 552 record_master_found = 0; 553 record_source_found = 0; 554 mutex_enter(sc->sc_lock); 555 for(mi.index = 0; ; mi.index++) { 556 if (hwp->query_devinfo(hdlp, &mi) != 0) 557 break; 558 KASSERT(mi.index < sc->sc_nmixer_states); 559 if (mi.type == AUDIO_MIXER_CLASS) 560 continue; 561 if (mi.mixer_class == iclass) { 562 /* 563 * AudioCinputs is only a fallback, when we don't 564 * find what we're looking for in AudioCrecord, so 565 * check the flags before accepting one of these. 566 */ 567 if (strcmp(mi.label.name, AudioNmaster) == 0 568 && record_master_found == 0) 569 sc->sc_inports.master = mi.index; 570 if (strcmp(mi.label.name, AudioNsource) == 0 571 && record_source_found == 0) { 572 if (mi.type == AUDIO_MIXER_ENUM) { 573 int i; 574 for(i = 0; i < mi.un.e.num_mem; i++) 575 if (strcmp(mi.un.e.member[i].label.name, 576 AudioNmixerout) == 0) 577 sc->sc_inports.mixerout = 578 mi.un.e.member[i].ord; 579 } 580 au_setup_ports(sc, &sc->sc_inports, &mi, 581 itable); 582 } 583 if (strcmp(mi.label.name, AudioNdac) == 0 && 584 sc->sc_outports.master == -1) 585 sc->sc_outports.master = mi.index; 586 } else if (mi.mixer_class == mclass) { 587 if (strcmp(mi.label.name, AudioNmonitor) == 0) 588 sc->sc_monitor_port = mi.index; 589 } else if (mi.mixer_class == oclass) { 590 if (strcmp(mi.label.name, AudioNmaster) == 0) 591 sc->sc_outports.master = mi.index; 592 if (strcmp(mi.label.name, AudioNselect) == 0) 593 au_setup_ports(sc, &sc->sc_outports, &mi, 594 otable); 595 } else if (mi.mixer_class == rclass) { 596 /* 597 * These are the preferred mixers for the audio record 598 * controls, so set the flags here, but don't check. 599 */ 600 if (strcmp(mi.label.name, AudioNmaster) == 0) { 601 sc->sc_inports.master = mi.index; 602 record_master_found = 1; 603 } 604#if 1 /* Deprecated. Use AudioNmaster. */ 605 if (strcmp(mi.label.name, AudioNrecord) == 0) { 606 sc->sc_inports.master = mi.index; 607 record_master_found = 1; 608 } 609 if (strcmp(mi.label.name, AudioNvolume) == 0) { 610 sc->sc_inports.master = mi.index; 611 record_master_found = 1; 612 } 613#endif 614 if (strcmp(mi.label.name, AudioNsource) == 0) { 615 if (mi.type == AUDIO_MIXER_ENUM) { 616 int i; 617 for(i = 0; i < mi.un.e.num_mem; i++) 618 if (strcmp(mi.un.e.member[i].label.name, 619 AudioNmixerout) == 0) 620 sc->sc_inports.mixerout = 621 mi.un.e.member[i].ord; 622 } 623 au_setup_ports(sc, &sc->sc_inports, &mi, 624 itable); 625 record_source_found = 1; 626 } 627 } 628 } 629 mutex_exit(sc->sc_lock); 630 DPRINTF(("audio_attach: inputs ports=0x%x, input master=%d, " 631 "output ports=0x%x, output master=%d\n", 632 sc->sc_inports.allports, sc->sc_inports.master, 633 sc->sc_outports.allports, sc->sc_outports.master)); 634 635 selinit(&sc->sc_rsel); 636 selinit(&sc->sc_wsel); 637 638#ifdef AUDIO_PM_IDLE 639 callout_init(&sc->sc_idle_counter, 0); 640 callout_setfunc(&sc->sc_idle_counter, audio_idle, self); 641#endif 642 643 if (!pmf_device_register(self, audio_suspend, audio_resume)) 644 aprint_error_dev(self, "couldn't establish power handler\n"); 645#ifdef AUDIO_PM_IDLE 646 if (!device_active_register(self, audio_activity)) 647 aprint_error_dev(self, "couldn't register activity handler\n"); 648#endif 649 650 if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_DOWN, 651 audio_volume_down, true)) 652 aprint_error_dev(self, "couldn't add volume down handler\n"); 653 if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_UP, 654 audio_volume_up, true)) 655 aprint_error_dev(self, "couldn't add volume up handler\n"); 656 if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_TOGGLE, 657 audio_volume_toggle, true)) 658 aprint_error_dev(self, "couldn't add volume toggle handler\n"); 659 660#ifdef AUDIO_PM_IDLE 661 callout_schedule(&sc->sc_idle_counter, audio_idle_timeout * hz); 662#endif 663} 664 665int 666audioactivate(device_t self, enum devact act) 667{ 668 struct audio_softc *sc = device_private(self); 669 670 switch (act) { 671 case DVACT_DEACTIVATE: 672 mutex_enter(sc->sc_lock); 673 sc->sc_dying = true; 674 mutex_exit(sc->sc_lock); 675 return 0; 676 default: 677 return EOPNOTSUPP; 678 } 679} 680 681int 682audiodetach(device_t self, int flags) 683{ 684 struct audio_softc *sc; 685 int maj, mn, i; 686 687 sc = device_private(self); 688 DPRINTF(("audio_detach: sc=%p flags=%d\n", sc, flags)); 689 690 /* Start draining existing accessors of the device. */ 691 mutex_enter(sc->sc_lock); 692 sc->sc_dying = true; 693 cv_broadcast(&sc->sc_wchan); 694 cv_broadcast(&sc->sc_rchan); 695 mutex_exit(sc->sc_lock); 696 697 /* locate the major number */ 698 maj = cdevsw_lookup_major(&audio_cdevsw); 699 700 /* 701 * Nuke the vnodes for any open instances (calls close). 702 * Will wait until any activity on the device nodes has ceased. 703 * 704 * XXXAD NOT YET. 705 * 706 * XXXAD NEED TO PREVENT NEW REFERENCES THROUGH AUDIO_ENTER(). 707 */ 708 mn = device_unit(self); 709 vdevgone(maj, mn | SOUND_DEVICE, mn | SOUND_DEVICE, VCHR); 710 vdevgone(maj, mn | AUDIO_DEVICE, mn | AUDIO_DEVICE, VCHR); 711 vdevgone(maj, mn | AUDIOCTL_DEVICE, mn | AUDIOCTL_DEVICE, VCHR); 712 vdevgone(maj, mn | MIXER_DEVICE, mn | MIXER_DEVICE, VCHR); 713 714 pmf_event_deregister(self, PMFE_AUDIO_VOLUME_DOWN, 715 audio_volume_down, true); 716 pmf_event_deregister(self, PMFE_AUDIO_VOLUME_UP, 717 audio_volume_up, true); 718 pmf_event_deregister(self, PMFE_AUDIO_VOLUME_TOGGLE, 719 audio_volume_toggle, true); 720 721#ifdef AUDIO_PM_IDLE 722 callout_halt(&sc->sc_idle_counter, sc->sc_lock); 723 724 device_active_deregister(self, audio_activity); 725#endif 726 727 pmf_device_deregister(self); 728 729 /* free resources */ 730 audio_free_ring(sc, &sc->sc_pr); 731 audio_free_ring(sc, &sc->sc_rr); 732 for (i = 0; i < sc->sc_nrfilters; i++) { 733 sc->sc_rfilters[i]->dtor(sc->sc_rfilters[i]); 734 sc->sc_rfilters[i] = NULL; 735 audio_stream_dtor(&sc->sc_rstreams[i]); 736 } 737 sc->sc_nrfilters = 0; 738 for (i = 0; i < sc->sc_npfilters; i++) { 739 sc->sc_pfilters[i]->dtor(sc->sc_pfilters[i]); 740 sc->sc_pfilters[i] = NULL; 741 audio_stream_dtor(&sc->sc_pstreams[i]); 742 } 743 sc->sc_npfilters = 0; 744 745 if (sc->sc_sih_rd) { 746 softint_disestablish(sc->sc_sih_rd); 747 sc->sc_sih_rd = NULL; 748 } 749 if (sc->sc_sih_wr) { 750 softint_disestablish(sc->sc_sih_wr); 751 sc->sc_sih_wr = NULL; 752 } 753 754#ifdef AUDIO_PM_IDLE 755 callout_destroy(&sc->sc_idle_counter); 756#endif 757 seldestroy(&sc->sc_rsel); 758 seldestroy(&sc->sc_wsel); 759 760 cv_destroy(&sc->sc_rchan); 761 cv_destroy(&sc->sc_wchan); 762 cv_destroy(&sc->sc_lchan); 763 764 return 0; 765} 766 767int 768au_portof(struct audio_softc *sc, char *name, int class) 769{ 770 mixer_devinfo_t mi; 771 772 for(mi.index = 0; 773 sc->hw_if->query_devinfo(sc->hw_hdl, &mi) == 0; 774 mi.index++) 775 if (mi.mixer_class == class && strcmp(mi.label.name, name) == 0) 776 return mi.index; 777 return -1; 778} 779 780void 781au_setup_ports(struct audio_softc *sc, struct au_mixer_ports *ports, 782 mixer_devinfo_t *mi, const struct portname *tbl) 783{ 784 int i, j; 785 786 ports->index = mi->index; 787 if (mi->type == AUDIO_MIXER_ENUM) { 788 ports->isenum = true; 789 for(i = 0; tbl[i].name; i++) 790 for(j = 0; j < mi->un.e.num_mem; j++) 791 if (strcmp(mi->un.e.member[j].label.name, 792 tbl[i].name) == 0) { 793 ports->allports |= tbl[i].mask; 794 ports->aumask[ports->nports] = tbl[i].mask; 795 ports->misel[ports->nports] = 796 mi->un.e.member[j].ord; 797 ports->miport[ports->nports] = 798 au_portof(sc, mi->un.e.member[j].label.name, 799 mi->mixer_class); 800 if (ports->mixerout != -1 && 801 ports->miport[ports->nports] != -1) 802 ports->isdual = true; 803 ++ports->nports; 804 } 805 } else if (mi->type == AUDIO_MIXER_SET) { 806 for(i = 0; tbl[i].name; i++) 807 for(j = 0; j < mi->un.s.num_mem; j++) 808 if (strcmp(mi->un.s.member[j].label.name, 809 tbl[i].name) == 0) { 810 ports->allports |= tbl[i].mask; 811 ports->aumask[ports->nports] = tbl[i].mask; 812 ports->misel[ports->nports] = 813 mi->un.s.member[j].mask; 814 ports->miport[ports->nports] = 815 au_portof(sc, mi->un.s.member[j].label.name, 816 mi->mixer_class); 817 ++ports->nports; 818 } 819 } 820} 821 822/* 823 * Called from hardware driver. This is where the MI audio driver gets 824 * probed/attached to the hardware driver. 825 */ 826device_t 827audio_attach_mi(const struct audio_hw_if *ahwp, void *hdlp, device_t dev) 828{ 829 struct audio_attach_args arg; 830 831#ifdef DIAGNOSTIC 832 if (ahwp == NULL) { 833 aprint_error("audio_attach_mi: NULL\n"); 834 return 0; 835 } 836#endif 837 arg.type = AUDIODEV_TYPE_AUDIO; 838 arg.hwif = ahwp; 839 arg.hdl = hdlp; 840 return config_found(dev, &arg, audioprint); 841} 842 843#ifdef AUDIO_DEBUG 844void audio_printsc(struct audio_softc *); 845void audio_print_params(const char *, struct audio_params *); 846 847void 848audio_printsc(struct audio_softc *sc) 849{ 850 printf("hwhandle %p hw_if %p ", sc->hw_hdl, sc->hw_if); 851 printf("open 0x%x mode 0x%x\n", sc->sc_open, sc->sc_mode); 852 printf("rchan 0x%x wchan 0x%x ", cv_has_waiters(&sc->sc_rchan), 853 cv_has_waiters(&sc->sc_wchan)); 854 printf("rring used 0x%x pring used=%d\n", 855 audio_stream_get_used(&sc->sc_rr.s), 856 audio_stream_get_used(&sc->sc_pr.s)); 857 printf("rbus 0x%x pbus 0x%x ", sc->sc_rbus, sc->sc_pbus); 858 printf("blksize %d", sc->sc_pr.blksize); 859 printf("hiwat %d lowat %d\n", sc->sc_pr.usedhigh, sc->sc_pr.usedlow); 860} 861 862void 863audio_print_params(const char *s, struct audio_params *p) 864{ 865 printf("%s enc=%u %uch %u/%ubit %uHz\n", s, p->encoding, p->channels, 866 p->validbits, p->precision, p->sample_rate); 867} 868#endif 869 870int 871audio_alloc_ring(struct audio_softc *sc, struct audio_ringbuffer *r, 872 int direction, size_t bufsize) 873{ 874 const struct audio_hw_if *hw; 875 void *hdl; 876 877 hw = sc->hw_if; 878 hdl = sc->hw_hdl; 879 /* 880 * Alloc DMA play and record buffers 881 */ 882 if (bufsize < AUMINBUF) 883 bufsize = AUMINBUF; 884 ROUNDSIZE(bufsize); 885 if (hw->round_buffersize) { 886 mutex_enter(sc->sc_lock); 887 bufsize = hw->round_buffersize(hdl, direction, bufsize); 888 mutex_exit(sc->sc_lock); 889 } 890 if (hw->allocm) 891 r->s.start = hw->allocm(hdl, direction, bufsize); 892 else 893 r->s.start = kmem_alloc(bufsize, KM_SLEEP); 894 if (r->s.start == 0) 895 return ENOMEM; 896 r->s.bufsize = bufsize; 897 return 0; 898} 899 900void 901audio_free_ring(struct audio_softc *sc, struct audio_ringbuffer *r) 902{ 903 if (r->s.start == 0) 904 return; 905 906 if (sc->hw_if->freem) 907 sc->hw_if->freem(sc->hw_hdl, r->s.start, r->s.bufsize); 908 else 909 kmem_free(r->s.start, r->s.bufsize); 910 r->s.start = 0; 911} 912 913static int 914audio_setup_pfilters(struct audio_softc *sc, const audio_params_t *pp, 915 stream_filter_list_t *pfilters) 916{ 917 stream_filter_t *pf[AUDIO_MAX_FILTERS], *of[AUDIO_MAX_FILTERS]; 918 audio_stream_t ps[AUDIO_MAX_FILTERS], os[AUDIO_MAX_FILTERS]; 919 const audio_params_t *from_param; 920 audio_params_t *to_param; 921 int i, n, onfilters; 922 923 KASSERT(mutex_owned(sc->sc_lock)); 924 925 /* Construct new filters. */ 926 mutex_exit(sc->sc_lock); 927 memset(pf, 0, sizeof(pf)); 928 memset(ps, 0, sizeof(ps)); 929 from_param = pp; 930 for (i = 0; i < pfilters->req_size; i++) { 931 n = pfilters->req_size - i - 1; 932 to_param = &pfilters->filters[n].param; 933 audio_check_params(to_param); 934 pf[i] = pfilters->filters[n].factory(sc, from_param, to_param); 935 if (pf[i] == NULL) 936 break; 937 if (audio_stream_ctor(&ps[i], from_param, AU_RING_SIZE)) 938 break; 939 if (i > 0) 940 pf[i]->set_fetcher(pf[i], &pf[i - 1]->base); 941 from_param = to_param; 942 } 943 if (i < pfilters->req_size) { /* failure */ 944 DPRINTF(("%s: pfilters failure\n", __func__)); 945 for (; i >= 0; i--) { 946 if (pf[i] != NULL) 947 pf[i]->dtor(pf[i]); 948 audio_stream_dtor(&ps[i]); 949 } 950 mutex_enter(sc->sc_lock); 951 return EINVAL; 952 } 953 mutex_enter(sc->sc_lock); 954 955 /* Swap in new filters. */ 956 mutex_enter(sc->sc_intr_lock); 957 memcpy(of, sc->sc_pfilters, sizeof(of)); 958 memcpy(os, sc->sc_pstreams, sizeof(os)); 959 onfilters = sc->sc_npfilters; 960 memcpy(sc->sc_pfilters, pf, sizeof(pf)); 961 memcpy(sc->sc_pstreams, ps, sizeof(ps)); 962 sc->sc_npfilters = pfilters->req_size; 963 for (i = 0; i < pfilters->req_size; i++) { 964 pf[i]->set_inputbuffer(pf[i], &sc->sc_pstreams[i]); 965 } 966 /* hardware format and the buffer near to userland */ 967 if (pfilters->req_size <= 0) { 968 sc->sc_pr.s.param = *pp; 969 sc->sc_pustream = &sc->sc_pr.s; 970 } else { 971 sc->sc_pr.s.param = pfilters->filters[0].param; 972 sc->sc_pustream = &sc->sc_pstreams[0]; 973 } 974 mutex_exit(sc->sc_intr_lock); 975 976 /* Destroy old filters. */ 977 mutex_exit(sc->sc_lock); 978 for (i = 0; i < onfilters; i++) { 979 of[i]->dtor(of[i]); 980 audio_stream_dtor(&os[i]); 981 } 982 mutex_enter(sc->sc_lock); 983 984#ifdef AUDIO_DEBUG 985 printf("%s: HW-buffer=%p pustream=%p\n", 986 __func__, &sc->sc_pr.s, sc->sc_pustream); 987 for (i = 0; i < pfilters->req_size; i++) { 988 char num[100]; 989 snprintf(num, 100, "[%d]", i); 990 audio_print_params(num, &sc->sc_pstreams[i].param); 991 } 992 audio_print_params("[HW]", &sc->sc_pr.s.param); 993#endif /* AUDIO_DEBUG */ 994 995 return 0; 996} 997 998static int 999audio_setup_rfilters(struct audio_softc *sc, const audio_params_t *rp, 1000 stream_filter_list_t *rfilters) 1001{ 1002 stream_filter_t *rf[AUDIO_MAX_FILTERS], *of[AUDIO_MAX_FILTERS]; 1003 audio_stream_t rs[AUDIO_MAX_FILTERS], os[AUDIO_MAX_FILTERS]; 1004 const audio_params_t *to_param; 1005 audio_params_t *from_param; 1006 int i, onfilters; 1007 1008 KASSERT(mutex_owned(sc->sc_lock)); 1009 1010 /* Construct new filters. */ 1011 mutex_exit(sc->sc_lock); 1012 memset(rf, 0, sizeof(rf)); 1013 memset(rs, 0, sizeof(rs)); 1014 for (i = 0; i < rfilters->req_size; i++) { 1015 from_param = &rfilters->filters[i].param; 1016 audio_check_params(from_param); 1017 to_param = i + 1 < rfilters->req_size 1018 ? &rfilters->filters[i + 1].param : rp; 1019 rf[i] = rfilters->filters[i].factory(sc, from_param, to_param); 1020 if (rf[i] == NULL) 1021 break; 1022 if (audio_stream_ctor(&rs[i], to_param, AU_RING_SIZE)) 1023 break; 1024 if (i > 0) { 1025 rf[i]->set_fetcher(rf[i], &rf[i - 1]->base); 1026 } else { 1027 /* rf[0] has no previous fetcher because 1028 * the audio hardware fills data to the 1029 * input buffer. */ 1030 rf[0]->set_inputbuffer(rf[0], &sc->sc_rr.s); 1031 } 1032 } 1033 if (i < rfilters->req_size) { /* failure */ 1034 DPRINTF(("%s: rfilters failure\n", __func__)); 1035 for (; i >= 0; i--) { 1036 if (rf[i] != NULL) 1037 rf[i]->dtor(rf[i]); 1038 audio_stream_dtor(&rs[i]); 1039 } 1040 mutex_enter(sc->sc_lock); 1041 return EINVAL; 1042 } 1043 mutex_enter(sc->sc_lock); 1044 1045 /* Swap in new filters. */ 1046 mutex_enter(sc->sc_intr_lock); 1047 memcpy(of, sc->sc_rfilters, sizeof(of)); 1048 memcpy(os, sc->sc_rstreams, sizeof(os)); 1049 onfilters = sc->sc_nrfilters; 1050 memcpy(sc->sc_rfilters, rf, sizeof(rf)); 1051 memcpy(sc->sc_rstreams, rs, sizeof(rs)); 1052 sc->sc_nrfilters = rfilters->req_size; 1053 for (i = 1; i < rfilters->req_size; i++) { 1054 rf[i]->set_inputbuffer(rf[i], &sc->sc_rstreams[i - 1]); 1055 } 1056 /* hardware format and the buffer near to userland */ 1057 if (rfilters->req_size <= 0) { 1058 sc->sc_rr.s.param = *rp; 1059 sc->sc_rustream = &sc->sc_rr.s; 1060 } else { 1061 sc->sc_rr.s.param = rfilters->filters[0].param; 1062 sc->sc_rustream = &sc->sc_rstreams[rfilters->req_size - 1]; 1063 } 1064 mutex_exit(sc->sc_intr_lock); 1065 1066#ifdef AUDIO_DEBUG 1067 printf("%s: HW-buffer=%p pustream=%p\n", 1068 __func__, &sc->sc_rr.s, sc->sc_rustream); 1069 audio_print_params("[HW]", &sc->sc_rr.s.param); 1070 for (i = 0; i < rfilters->req_size; i++) { 1071 char num[100]; 1072 snprintf(num, 100, "[%d]", i); 1073 audio_print_params(num, &sc->sc_rstreams[i].param); 1074 } 1075#endif /* AUDIO_DEBUG */ 1076 1077 /* Destroy old filters. */ 1078 mutex_exit(sc->sc_lock); 1079 for (i = 0; i < onfilters; i++) { 1080 of[i]->dtor(of[i]); 1081 audio_stream_dtor(&os[i]); 1082 } 1083 mutex_enter(sc->sc_lock); 1084 1085 return 0; 1086} 1087 1088static void 1089audio_stream_dtor(audio_stream_t *stream) 1090{ 1091 1092 if (stream->start != NULL) 1093 kmem_free(stream->start, stream->bufsize); 1094 memset(stream, 0, sizeof(audio_stream_t)); 1095} 1096 1097static int 1098audio_stream_ctor(audio_stream_t *stream, const audio_params_t *param, int size) 1099{ 1100 int frame_size; 1101 1102 size = min(size, AU_RING_SIZE); 1103 stream->bufsize = size; 1104 stream->start = kmem_alloc(size, KM_SLEEP); 1105 if (stream->start == NULL) 1106 return ENOMEM; 1107 frame_size = (param->precision + 7) / 8 * param->channels; 1108 size = (size / frame_size) * frame_size; 1109 stream->end = stream->start + size; 1110 stream->inp = stream->start; 1111 stream->outp = stream->start; 1112 stream->used = 0; 1113 stream->param = *param; 1114 stream->loop = false; 1115 return 0; 1116} 1117 1118static void 1119stream_filter_list_append(stream_filter_list_t *list, 1120 stream_filter_factory_t factory, 1121 const audio_params_t *param) 1122{ 1123 1124 if (list->req_size >= AUDIO_MAX_FILTERS) { 1125 printf("%s: increase AUDIO_MAX_FILTERS in sys/dev/audio_if.h\n", 1126 __func__); 1127 return; 1128 } 1129 list->filters[list->req_size].factory = factory; 1130 list->filters[list->req_size].param = *param; 1131 list->req_size++; 1132} 1133 1134static void 1135stream_filter_list_set(stream_filter_list_t *list, int i, 1136 stream_filter_factory_t factory, 1137 const audio_params_t *param) 1138{ 1139 1140 if (i < 0 || i >= AUDIO_MAX_FILTERS) { 1141 printf("%s: invalid index: %d\n", __func__, i); 1142 return; 1143 } 1144 1145 list->filters[i].factory = factory; 1146 list->filters[i].param = *param; 1147 if (list->req_size <= i) 1148 list->req_size = i + 1; 1149} 1150 1151static void 1152stream_filter_list_prepend(stream_filter_list_t *list, 1153 stream_filter_factory_t factory, 1154 const audio_params_t *param) 1155{ 1156 1157 if (list->req_size >= AUDIO_MAX_FILTERS) { 1158 printf("%s: increase AUDIO_MAX_FILTERS in sys/dev/audio_if.h\n", 1159 __func__); 1160 return; 1161 } 1162 memmove(&list->filters[1], &list->filters[0], 1163 sizeof(struct stream_filter_req) * list->req_size); 1164 list->filters[0].factory = factory; 1165 list->filters[0].param = *param; 1166 list->req_size++; 1167} 1168 1169/* 1170 * Look up audio device and acquire locks for device access. 1171 */ 1172static int 1173audio_enter(dev_t dev, krw_t rw, struct audio_softc **scp) 1174{ 1175 struct audio_softc *sc; 1176 1177 /* First, find the device and take sc_lock. */ 1178 sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev)); 1179 if (sc == NULL) 1180 return ENXIO; 1181 mutex_enter(sc->sc_lock); 1182 if (sc->sc_dying) { 1183 mutex_exit(sc->sc_lock); 1184 return EIO; 1185 } 1186 1187 /* Acquire device access lock. */ 1188 switch (rw) { 1189 case RW_WRITER: 1190 while (__predict_false(sc->sc_dvlock != 0)) { 1191 cv_wait(&sc->sc_lchan, sc->sc_lock); 1192 } 1193 sc->sc_dvlock = -1; 1194 break; 1195 case RW_READER: 1196 while (__predict_false(sc->sc_dvlock < 0)) { 1197 cv_wait(&sc->sc_lchan, sc->sc_lock); 1198 } 1199 sc->sc_dvlock++; 1200 break; 1201 default: 1202 panic("audio_enter"); 1203 } 1204 1205 *scp = sc; 1206 return 0; 1207} 1208 1209/* 1210 * Release reference to device acquired with audio_enter(). 1211 */ 1212static void 1213audio_exit(struct audio_softc *sc) 1214{ 1215 1216 KASSERT(mutex_owned(sc->sc_lock)); 1217 KASSERT(sc->sc_dvlock != 0); 1218 1219 /* Release device level lock. */ 1220 if (__predict_false(sc->sc_dvlock < 0)) { 1221 sc->sc_dvlock = 0; 1222 } else { 1223 sc->sc_dvlock--; 1224 } 1225 cv_broadcast(&sc->sc_lchan); 1226 mutex_exit(sc->sc_lock); 1227} 1228 1229/* 1230 * Wait for I/O to complete, releasing device lock. 1231 */ 1232static int 1233audio_waitio(struct audio_softc *sc, kcondvar_t *chan) 1234{ 1235 int error; 1236 krw_t rw; 1237 1238 KASSERT(mutex_owned(sc->sc_lock)); 1239 1240 /* Release device level lock while sleeping. */ 1241 if (__predict_false(sc->sc_dvlock < 0)) { 1242 sc->sc_dvlock = 0; 1243 rw = RW_WRITER; 1244 } else { 1245 KASSERT(sc->sc_dvlock > 0); 1246 sc->sc_dvlock--; 1247 rw = RW_READER; 1248 } 1249 cv_broadcast(&sc->sc_lchan); 1250 1251 /* Wait for pending I/O to complete. */ 1252 error = cv_wait_sig(chan, sc->sc_lock); 1253 1254 /* Re-acquire device level lock. */ 1255 if (__predict_false(rw == RW_WRITER)) { 1256 while (__predict_false(sc->sc_dvlock != 0)) { 1257 cv_wait(&sc->sc_lchan, sc->sc_lock); 1258 } 1259 sc->sc_dvlock = -1; 1260 } else { 1261 while (__predict_false(sc->sc_dvlock < 0)) { 1262 cv_wait(&sc->sc_lchan, sc->sc_lock); 1263 } 1264 sc->sc_dvlock++; 1265 } 1266 1267 return error; 1268} 1269 1270int 1271audioopen(dev_t dev, int flags, int ifmt, struct lwp *l) 1272{ 1273 struct audio_softc *sc; 1274 int error; 1275 1276 if ((error = audio_enter(dev, RW_WRITER, &sc)) != 0) 1277 return error; 1278 device_active(sc->dev, DVA_SYSTEM); 1279 switch (AUDIODEV(dev)) { 1280 case SOUND_DEVICE: 1281 case AUDIO_DEVICE: 1282 error = audio_open(dev, sc, flags, ifmt, l); 1283 break; 1284 case AUDIOCTL_DEVICE: 1285 error = 0; 1286 break; 1287 case MIXER_DEVICE: 1288 error = mixer_open(dev, sc, flags, ifmt, l); 1289 break; 1290 default: 1291 error = ENXIO; 1292 break; 1293 } 1294 audio_exit(sc); 1295 1296 return error; 1297} 1298 1299int 1300audioclose(dev_t dev, int flags, int ifmt, struct lwp *l) 1301{ 1302 struct audio_softc *sc; 1303 int error; 1304 1305 if ((error = audio_enter(dev, RW_WRITER, &sc)) != 0) 1306 return error; 1307 device_active(sc->dev, DVA_SYSTEM); 1308 switch (AUDIODEV(dev)) { 1309 case SOUND_DEVICE: 1310 case AUDIO_DEVICE: 1311 error = audio_close(sc, flags, ifmt, l); 1312 break; 1313 case MIXER_DEVICE: 1314 error = mixer_close(sc, flags, ifmt, l); 1315 break; 1316 case AUDIOCTL_DEVICE: 1317 error = 0; 1318 break; 1319 default: 1320 error = ENXIO; 1321 break; 1322 } 1323 audio_exit(sc); 1324 1325 return error; 1326} 1327 1328int 1329audioread(dev_t dev, struct uio *uio, int ioflag) 1330{ 1331 struct audio_softc *sc; 1332 int error; 1333 1334 if ((error = audio_enter(dev, RW_READER, &sc)) != 0) 1335 return error; 1336 switch (AUDIODEV(dev)) { 1337 case SOUND_DEVICE: 1338 case AUDIO_DEVICE: 1339 error = audio_read(sc, uio, ioflag); 1340 break; 1341 case AUDIOCTL_DEVICE: 1342 case MIXER_DEVICE: 1343 error = ENODEV; 1344 break; 1345 default: 1346 error = ENXIO; 1347 break; 1348 } 1349 audio_exit(sc); 1350 1351 return error; 1352} 1353 1354int 1355audiowrite(dev_t dev, struct uio *uio, int ioflag) 1356{ 1357 struct audio_softc *sc; 1358 int error; 1359 1360 if ((error = audio_enter(dev, RW_READER, &sc)) != 0) 1361 return error; 1362 switch (AUDIODEV(dev)) { 1363 case SOUND_DEVICE: 1364 case AUDIO_DEVICE: 1365 error = audio_write(sc, uio, ioflag); 1366 break; 1367 case AUDIOCTL_DEVICE: 1368 case MIXER_DEVICE: 1369 error = ENODEV; 1370 break; 1371 default: 1372 error = ENXIO; 1373 break; 1374 } 1375 audio_exit(sc); 1376 1377 return error; 1378} 1379 1380int 1381audioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 1382{ 1383 struct audio_softc *sc; 1384 int error; 1385 krw_t rw; 1386 1387 /* Figure out which lock type we need. */ 1388 switch (cmd) { 1389 case AUDIO_FLUSH: 1390 case AUDIO_SETINFO: 1391 case AUDIO_DRAIN: 1392 case AUDIO_SETFD: 1393 rw = RW_WRITER; 1394 break; 1395 default: 1396 rw = RW_READER; 1397 break; 1398 } 1399 1400 if ((error = audio_enter(dev, rw, &sc)) != 0) 1401 return error; 1402 switch (AUDIODEV(dev)) { 1403 case SOUND_DEVICE: 1404 case AUDIO_DEVICE: 1405 case AUDIOCTL_DEVICE: 1406 device_active(sc->dev, DVA_SYSTEM); 1407 if (IOCGROUP(cmd) == IOCGROUP(AUDIO_MIXER_READ)) 1408 error = mixer_ioctl(sc, cmd, addr, flag, l); 1409 else 1410 error = audio_ioctl(sc, cmd, addr, flag, l); 1411 break; 1412 case MIXER_DEVICE: 1413 error = mixer_ioctl(sc, cmd, addr, flag, l); 1414 break; 1415 default: 1416 error = ENXIO; 1417 break; 1418 } 1419 audio_exit(sc); 1420 1421 return error; 1422} 1423 1424int 1425audiopoll(dev_t dev, int events, struct lwp *l) 1426{ 1427 struct audio_softc *sc; 1428 int revents; 1429 1430 /* Don't bother with device level lock here. */ 1431 sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev)); 1432 if (sc == NULL) 1433 return ENXIO; 1434 mutex_enter(sc->sc_lock); 1435 if (sc->sc_dying) { 1436 mutex_exit(sc->sc_lock); 1437 return EIO; 1438 } 1439 switch (AUDIODEV(dev)) { 1440 case SOUND_DEVICE: 1441 case AUDIO_DEVICE: 1442 revents = audio_poll(sc, events, l); 1443 break; 1444 case AUDIOCTL_DEVICE: 1445 case MIXER_DEVICE: 1446 revents = 0; 1447 break; 1448 default: 1449 revents = POLLERR; 1450 break; 1451 } 1452 mutex_exit(sc->sc_lock); 1453 1454 return revents; 1455} 1456 1457int 1458audiokqfilter(dev_t dev, struct knote *kn) 1459{ 1460 struct audio_softc *sc; 1461 int rv; 1462 1463 /* Don't bother with device level lock here. */ 1464 sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev)); 1465 if (sc == NULL) 1466 return ENXIO; 1467 mutex_enter(sc->sc_lock); 1468 if (sc->sc_dying) { 1469 mutex_exit(sc->sc_lock); 1470 return EIO; 1471 } 1472 switch (AUDIODEV(dev)) { 1473 case SOUND_DEVICE: 1474 case AUDIO_DEVICE: 1475 rv = audio_kqfilter(sc, kn); 1476 break; 1477 case AUDIOCTL_DEVICE: 1478 case MIXER_DEVICE: 1479 rv = 1; 1480 break; 1481 default: 1482 rv = 1; 1483 } 1484 mutex_exit(sc->sc_lock); 1485 1486 return rv; 1487} 1488 1489paddr_t 1490audiommap(dev_t dev, off_t off, int prot) 1491{ 1492 struct audio_softc *sc; 1493 paddr_t error; 1494 1495 /* 1496 * Acquire a reader lock. audio_mmap() will drop sc_lock 1497 * in order to allow the device's mmap routine to sleep. 1498 * Although not yet possible, we want to prevent memory 1499 * from being allocated or freed out from under us. 1500 */ 1501 if ((error = audio_enter(dev, RW_READER, &sc)) != 0) 1502 return 1; 1503 device_active(sc->dev, DVA_SYSTEM); /* XXXJDM */ 1504 switch (AUDIODEV(dev)) { 1505 case SOUND_DEVICE: 1506 case AUDIO_DEVICE: 1507 error = audio_mmap(sc, off, prot); 1508 break; 1509 case AUDIOCTL_DEVICE: 1510 case MIXER_DEVICE: 1511 error = -1; 1512 break; 1513 default: 1514 error = -1; 1515 break; 1516 } 1517 audio_exit(sc); 1518 return error; 1519} 1520 1521/* 1522 * Audio driver 1523 */ 1524void 1525audio_init_ringbuffer(struct audio_softc *sc, struct audio_ringbuffer *rp, 1526 int mode) 1527{ 1528 int nblks; 1529 int blksize; 1530 1531 blksize = rp->blksize; 1532 if (blksize < AUMINBLK) 1533 blksize = AUMINBLK; 1534 if (blksize > rp->s.bufsize / AUMINNOBLK) 1535 blksize = rp->s.bufsize / AUMINNOBLK; 1536 ROUNDSIZE(blksize); 1537 DPRINTF(("audio_init_ringbuffer: MI blksize=%d\n", blksize)); 1538 if (sc->hw_if->round_blocksize) 1539 blksize = sc->hw_if->round_blocksize(sc->hw_hdl, blksize, 1540 mode, &rp->s.param); 1541 if (blksize <= 0) 1542 panic("audio_init_ringbuffer: blksize"); 1543 nblks = rp->s.bufsize / blksize; 1544 1545 DPRINTF(("audio_init_ringbuffer: final blksize=%d\n", blksize)); 1546 rp->blksize = blksize; 1547 rp->maxblks = nblks; 1548 rp->s.end = rp->s.start + nblks * blksize; 1549 rp->s.outp = rp->s.inp = rp->s.start; 1550 rp->s.used = 0; 1551 rp->stamp = 0; 1552 rp->stamp_last = 0; 1553 rp->fstamp = 0; 1554 rp->drops = 0; 1555 rp->copying = false; 1556 rp->needfill = false; 1557 rp->mmapped = false; 1558} 1559 1560int 1561audio_initbufs(struct audio_softc *sc) 1562{ 1563 const struct audio_hw_if *hw; 1564 int error; 1565 1566 DPRINTF(("audio_initbufs: mode=0x%x\n", sc->sc_mode)); 1567 hw = sc->hw_if; 1568 if (audio_can_capture(sc)) { 1569 audio_init_ringbuffer(sc, &sc->sc_rr, AUMODE_RECORD); 1570 if (hw->init_input && (sc->sc_mode & AUMODE_RECORD)) { 1571 error = hw->init_input(sc->hw_hdl, sc->sc_rr.s.start, 1572 sc->sc_rr.s.end - sc->sc_rr.s.start); 1573 if (error) 1574 return error; 1575 } 1576 } 1577 1578 if (audio_can_playback(sc)) { 1579 audio_init_ringbuffer(sc, &sc->sc_pr, AUMODE_PLAY); 1580 sc->sc_sil_count = 0; 1581 if (hw->init_output && (sc->sc_mode & AUMODE_PLAY)) { 1582 error = hw->init_output(sc->hw_hdl, sc->sc_pr.s.start, 1583 sc->sc_pr.s.end - sc->sc_pr.s.start); 1584 if (error) 1585 return error; 1586 } 1587 } 1588 1589#ifdef AUDIO_INTR_TIME 1590#define double u_long 1591 if (audio_can_playback(sc)) { 1592 sc->sc_pnintr = 0; 1593 sc->sc_pblktime = (u_long)( 1594 (double)sc->sc_pr.blksize * 100000 / 1595 (double)(sc->sc_pparams.precision / NBBY * 1596 sc->sc_pparams.channels * 1597 sc->sc_pparams.sample_rate)) * 10; 1598 DPRINTF(("audio: play blktime = %lu for %d\n", 1599 sc->sc_pblktime, sc->sc_pr.blksize)); 1600 } 1601 if (audio_can_capture(sc)) { 1602 sc->sc_rnintr = 0; 1603 sc->sc_rblktime = (u_long)( 1604 (double)sc->sc_rr.blksize * 100000 / 1605 (double)(sc->sc_rparams.precision / NBBY * 1606 sc->sc_rparams.channels * 1607 sc->sc_rparams.sample_rate)) * 10; 1608 DPRINTF(("audio: record blktime = %lu for %d\n", 1609 sc->sc_rblktime, sc->sc_rr.blksize)); 1610 } 1611#undef double 1612#endif 1613 1614 return 0; 1615} 1616 1617void 1618audio_calcwater(struct audio_softc *sc) 1619{ 1620 1621 /* set high at 100% */ 1622 if (audio_can_playback(sc)) { 1623 sc->sc_pr.usedhigh = 1624 sc->sc_pustream->end - sc->sc_pustream->start; 1625 /* set low at 75% of usedhigh */ 1626 sc->sc_pr.usedlow = sc->sc_pr.usedhigh * 3 / 4; 1627 if (sc->sc_pr.usedlow == sc->sc_pr.usedhigh) 1628 sc->sc_pr.usedlow -= sc->sc_pr.blksize; 1629 } 1630 1631 if (audio_can_capture(sc)) { 1632 sc->sc_rr.usedhigh = 1633 sc->sc_rustream->end - sc->sc_rustream->start - 1634 sc->sc_rr.blksize; 1635 sc->sc_rr.usedlow = 0; 1636 DPRINTF(("%s: plow=%d phigh=%d rlow=%d rhigh=%d\n", __func__, 1637 sc->sc_pr.usedlow, sc->sc_pr.usedhigh, 1638 sc->sc_rr.usedlow, sc->sc_rr.usedhigh)); 1639 } 1640} 1641 1642int 1643audio_open(dev_t dev, struct audio_softc *sc, int flags, int ifmt, 1644 struct lwp *l) 1645{ 1646 int error; 1647 u_int mode; 1648 const struct audio_hw_if *hw; 1649 1650 KASSERT(mutex_owned(sc->sc_lock)); 1651 1652 hw = sc->hw_if; 1653 if (hw == NULL) 1654 return ENXIO; 1655 1656 DPRINTF(("audio_open: flags=0x%x sc=%p hdl=%p\n", 1657 flags, sc, sc->hw_hdl)); 1658 1659 if (((flags & FREAD) && (sc->sc_open & AUOPEN_READ)) || 1660 ((flags & FWRITE) && (sc->sc_open & AUOPEN_WRITE))) 1661 return EBUSY; 1662 1663 if (hw->open != NULL) { 1664 mutex_enter(sc->sc_intr_lock); 1665 error = hw->open(sc->hw_hdl, flags); 1666 mutex_exit(sc->sc_intr_lock); 1667 if (error) 1668 return error; 1669 } 1670 1671 sc->sc_async_audio = 0; 1672 sc->sc_sil_count = 0; 1673 sc->sc_rbus = false; 1674 sc->sc_pbus = false; 1675 sc->sc_eof = 0; 1676 sc->sc_playdrop = 0; 1677 1678 mutex_enter(sc->sc_intr_lock); 1679 sc->sc_full_duplex = 1680 (flags & (FWRITE|FREAD)) == (FWRITE|FREAD) && 1681 (audio_get_props(sc) & AUDIO_PROP_FULLDUPLEX); 1682 mutex_exit(sc->sc_intr_lock); 1683 1684 mode = 0; 1685 if (flags & FREAD) { 1686 sc->sc_open |= AUOPEN_READ; 1687 mode |= AUMODE_RECORD; 1688 } 1689 if (flags & FWRITE) { 1690 sc->sc_open |= AUOPEN_WRITE; 1691 mode |= AUMODE_PLAY | AUMODE_PLAY_ALL; 1692 } 1693 1694 /* 1695 * Multiplex device: /dev/audio (MU-Law) and /dev/sound (linear) 1696 * The /dev/audio is always (re)set to 8-bit MU-Law mono 1697 * For the other devices, you get what they were last set to. 1698 */ 1699 if (ISDEVAUDIO(dev)) { 1700 error = audio_set_defaults(sc, mode); 1701 } else { 1702 struct audio_info ai; 1703 1704 AUDIO_INITINFO(&ai); 1705 ai.mode = mode; 1706 error = audiosetinfo(sc, &ai); 1707 } 1708 if (error) 1709 goto bad; 1710 1711#ifdef DIAGNOSTIC 1712 /* 1713 * Sample rate and precision are supposed to be set to proper 1714 * default values by the hardware driver, so that it may give 1715 * us these values. 1716 */ 1717 if (sc->sc_rparams.precision == 0 || sc->sc_pparams.precision == 0) { 1718 printf("audio_open: 0 precision\n"); 1719 return EINVAL; 1720 } 1721#endif 1722 1723 /* audio_close() decreases sc_pr.usedlow, recalculate here */ 1724 audio_calcwater(sc); 1725 1726 DPRINTF(("audio_open: done sc_mode = 0x%x\n", sc->sc_mode)); 1727 1728 return 0; 1729 1730bad: 1731 mutex_enter(sc->sc_intr_lock); 1732 if (hw->close != NULL) 1733 hw->close(sc->hw_hdl); 1734 sc->sc_open = 0; 1735 sc->sc_mode = 0; 1736 mutex_exit(sc->sc_intr_lock); 1737 sc->sc_full_duplex = 0; 1738 return error; 1739} 1740 1741/* 1742 * Must be called from task context. 1743 */ 1744void 1745audio_init_record(struct audio_softc *sc) 1746{ 1747 1748 KASSERT(mutex_owned(sc->sc_lock)); 1749 1750 mutex_enter(sc->sc_intr_lock); 1751 if (sc->hw_if->speaker_ctl && 1752 (!sc->sc_full_duplex || (sc->sc_mode & AUMODE_PLAY) == 0)) 1753 sc->hw_if->speaker_ctl(sc->hw_hdl, SPKR_OFF); 1754 mutex_exit(sc->sc_intr_lock); 1755} 1756 1757/* 1758 * Must be called from task context. 1759 */ 1760void 1761audio_init_play(struct audio_softc *sc) 1762{ 1763 1764 KASSERT(mutex_owned(sc->sc_lock)); 1765 1766 mutex_enter(sc->sc_intr_lock); 1767 sc->sc_wstamp = sc->sc_pr.stamp; 1768 if (sc->hw_if->speaker_ctl) 1769 sc->hw_if->speaker_ctl(sc->hw_hdl, SPKR_ON); 1770 mutex_exit(sc->sc_intr_lock); 1771} 1772 1773int 1774audio_drain(struct audio_softc *sc) 1775{ 1776 struct audio_ringbuffer *cb; 1777 int error, drops; 1778 int i, used; 1779 1780 KASSERT(mutex_owned(sc->sc_lock)); 1781 KASSERT(mutex_owned(sc->sc_intr_lock)); 1782 1783 DPRINTF(("audio_drain: enter busy=%d\n", sc->sc_pbus)); 1784 cb = &sc->sc_pr; 1785 if (cb->mmapped) 1786 return 0; 1787 1788 used = audio_stream_get_used(&sc->sc_pr.s); 1789 for (i = 0; i < sc->sc_npfilters; i++) 1790 used += audio_stream_get_used(&sc->sc_pstreams[i]); 1791 if (used <= 0) 1792 return 0; 1793 1794 if (!sc->sc_pbus) { 1795 /* We've never started playing, probably because the 1796 * block was too short. Pad it and start now. 1797 */ 1798 int cc; 1799 uint8_t *inp = cb->s.inp; 1800 1801 cc = cb->blksize - (inp - cb->s.start) % cb->blksize; 1802 audio_fill_silence(&cb->s.param, inp, cc); 1803 cb->s.inp = audio_stream_add_inp(&cb->s, inp, cc); 1804 error = audiostartp(sc); 1805 if (error) 1806 return error; 1807 } 1808 /* 1809 * Play until a silence block has been played, then we 1810 * know all has been drained. 1811 * XXX This should be done some other way to avoid 1812 * playing silence. 1813 */ 1814#ifdef DIAGNOSTIC 1815 if (cb->copying) { 1816 printf("audio_drain: copying in progress!?!\n"); 1817 cb->copying = false; 1818 } 1819#endif 1820 drops = cb->drops; 1821 error = 0; 1822 while (cb->drops == drops && !error) { 1823 DPRINTF(("audio_drain: used=%d, drops=%ld\n", 1824 audio_stream_get_used(&sc->sc_pr.s), cb->drops)); 1825 mutex_exit(sc->sc_intr_lock); 1826 error = audio_waitio(sc, &sc->sc_wchan); 1827 mutex_enter(sc->sc_intr_lock); 1828 if (sc->sc_dying) 1829 error = EIO; 1830 } 1831 return error; 1832} 1833 1834/* 1835 * Close an audio chip. 1836 */ 1837/* ARGSUSED */ 1838int 1839audio_close(struct audio_softc *sc, int flags, int ifmt, 1840 struct lwp *l) 1841{ 1842 const struct audio_hw_if *hw; 1843 1844 KASSERT(mutex_owned(sc->sc_lock)); 1845 1846 DPRINTF(("audio_close: sc=%p\n", sc)); 1847 hw = sc->hw_if; 1848 mutex_enter(sc->sc_intr_lock); 1849 /* Stop recording. */ 1850 if ((flags & FREAD) && sc->sc_rbus) { 1851 /* 1852 * XXX Some drivers (e.g. SB) use the same routine 1853 * to halt input and output so don't halt input if 1854 * in full duplex mode. These drivers should be fixed. 1855 */ 1856 if (!sc->sc_full_duplex || hw->halt_input != hw->halt_output) 1857 hw->halt_input(sc->hw_hdl); 1858 sc->sc_rbus = false; 1859 } 1860 /* 1861 * Block until output drains, but allow ^C interrupt. 1862 */ 1863 sc->sc_pr.usedlow = sc->sc_pr.blksize; /* avoid excessive wakeups */ 1864 /* 1865 * If there is pending output, let it drain (unless 1866 * the output is paused). 1867 */ 1868 if ((flags & FWRITE) && sc->sc_pbus) { 1869 if (!sc->sc_pr.pause && !audio_drain(sc) && hw->drain) 1870 (void)hw->drain(sc->hw_hdl); 1871 hw->halt_output(sc->hw_hdl); 1872 sc->sc_pbus = false; 1873 } 1874 if (hw->close != NULL) 1875 hw->close(sc->hw_hdl); 1876 sc->sc_open = 0; 1877 sc->sc_mode = 0; 1878 sc->sc_full_duplex = 0; 1879 mutex_exit(sc->sc_intr_lock); 1880 sc->sc_async_audio = 0; 1881 1882 return 0; 1883} 1884 1885int 1886audio_read(struct audio_softc *sc, struct uio *uio, int ioflag) 1887{ 1888 struct audio_ringbuffer *cb; 1889 const uint8_t *outp; 1890 uint8_t *inp; 1891 int error, used, cc, n; 1892 1893 KASSERT(mutex_owned(sc->sc_lock)); 1894 1895 cb = &sc->sc_rr; 1896 if (cb->mmapped) 1897 return EINVAL; 1898 1899 DPRINTFN(1,("audio_read: cc=%zu mode=%d\n", 1900 uio->uio_resid, sc->sc_mode)); 1901 1902#ifdef AUDIO_PM_IDLE 1903 if (device_is_active(&sc->dev) || sc->sc_idle) 1904 device_active(&sc->dev, DVA_SYSTEM); 1905#endif 1906 1907 error = 0; 1908 /* 1909 * If hardware is half-duplex and currently playing, return 1910 * silence blocks based on the number of blocks we have output. 1911 */ 1912 if (!sc->sc_full_duplex && (sc->sc_mode & AUMODE_PLAY)) { 1913 while (uio->uio_resid > 0 && !error) { 1914 for(;;) { 1915 /* 1916 * No need to lock, as any wakeup will be 1917 * held for us while holding sc_lock. 1918 */ 1919 cc = sc->sc_pr.stamp - sc->sc_wstamp; 1920 if (cc > 0) 1921 break; 1922 DPRINTF(("audio_read: stamp=%lu, wstamp=%lu\n", 1923 sc->sc_pr.stamp, sc->sc_wstamp)); 1924 if (ioflag & IO_NDELAY) 1925 return EWOULDBLOCK; 1926 error = audio_waitio(sc, &sc->sc_rchan); 1927 if (sc->sc_dying) 1928 error = EIO; 1929 if (error) 1930 return error; 1931 } 1932 1933 if (uio->uio_resid < cc) 1934 cc = uio->uio_resid; 1935 DPRINTFN(1,("audio_read: reading in write mode, " 1936 "cc=%d\n", cc)); 1937 error = audio_silence_copyout(sc, cc, uio); 1938 sc->sc_wstamp += cc; 1939 } 1940 return error; 1941 } 1942 1943 mutex_enter(sc->sc_intr_lock); 1944 while (uio->uio_resid > 0 && !error) { 1945 while ((used = audio_stream_get_used(sc->sc_rustream)) <= 0) { 1946 if (!sc->sc_rbus && !sc->sc_rr.pause) 1947 error = audiostartr(sc); 1948 mutex_exit(sc->sc_intr_lock); 1949 if (error) 1950 return error; 1951 if (ioflag & IO_NDELAY) 1952 return EWOULDBLOCK; 1953 DPRINTFN(2, ("audio_read: sleep used=%d\n", used)); 1954 error = audio_waitio(sc, &sc->sc_rchan); 1955 if (sc->sc_dying) 1956 error = EIO; 1957 if (error) 1958 return error; 1959 mutex_enter(sc->sc_intr_lock); 1960 } 1961 1962 outp = sc->sc_rustream->outp; 1963 inp = sc->sc_rustream->inp; 1964 cb->copying = true; 1965 1966 /* 1967 * cc is the amount of data in the sc_rustream excluding 1968 * wrapped data. Note the tricky case of inp == outp, which 1969 * must mean the buffer is full, not empty, because used > 0. 1970 */ 1971 cc = outp < inp ? inp - outp :sc->sc_rustream->end - outp; 1972 DPRINTFN(1,("audio_read: outp=%p, cc=%d\n", outp, cc)); 1973 1974 n = uio->uio_resid; 1975 mutex_exit(sc->sc_intr_lock); 1976 mutex_exit(sc->sc_lock); 1977 error = uiomove(__UNCONST(outp), cc, uio); 1978 mutex_enter(sc->sc_lock); 1979 mutex_enter(sc->sc_intr_lock); 1980 n -= uio->uio_resid; /* number of bytes actually moved */ 1981 1982 sc->sc_rustream->outp = audio_stream_add_outp 1983 (sc->sc_rustream, outp, n); 1984 cb->copying = false; 1985 } 1986 mutex_exit(sc->sc_intr_lock); 1987 return error; 1988} 1989 1990void 1991audio_clear(struct audio_softc *sc) 1992{ 1993 1994 KASSERT(mutex_owned(sc->sc_intr_lock)); 1995 1996 if (sc->sc_rbus) { 1997 cv_broadcast(&sc->sc_rchan); 1998 sc->hw_if->halt_input(sc->hw_hdl); 1999 sc->sc_rbus = false; 2000 sc->sc_rr.pause = false; 2001 } 2002 if (sc->sc_pbus) { 2003 cv_broadcast(&sc->sc_wchan); 2004 sc->hw_if->halt_output(sc->hw_hdl); 2005 sc->sc_pbus = false; 2006 sc->sc_pr.pause = false; 2007 } 2008} 2009 2010void 2011audio_clear_intr_unlocked(struct audio_softc *sc) 2012{ 2013 2014 mutex_enter(sc->sc_intr_lock); 2015 audio_clear(sc); 2016 mutex_exit(sc->sc_intr_lock); 2017} 2018 2019void 2020audio_calc_blksize(struct audio_softc *sc, int mode) 2021{ 2022 const audio_params_t *parm; 2023 struct audio_ringbuffer *rb; 2024 2025 if (sc->sc_blkset) 2026 return; 2027 2028 if (mode == AUMODE_PLAY) { 2029 rb = &sc->sc_pr; 2030 parm = &rb->s.param; 2031 } else { 2032 rb = &sc->sc_rr; 2033 parm = &rb->s.param; 2034 } 2035 2036 rb->blksize = parm->sample_rate * audio_blk_ms / 1000 * 2037 parm->channels * parm->precision / NBBY; 2038 2039 DPRINTF(("audio_calc_blksize: %s blksize=%d\n", 2040 mode == AUMODE_PLAY ? "play" : "record", rb->blksize)); 2041} 2042 2043void 2044audio_fill_silence(struct audio_params *params, uint8_t *p, int n) 2045{ 2046 uint8_t auzero0, auzero1; 2047 int nfill; 2048 2049 auzero1 = 0; /* initialize to please gcc */ 2050 nfill = 1; 2051 switch (params->encoding) { 2052 case AUDIO_ENCODING_ULAW: 2053 auzero0 = 0x7f; 2054 break; 2055 case AUDIO_ENCODING_ALAW: 2056 auzero0 = 0x55; 2057 break; 2058 case AUDIO_ENCODING_MPEG_L1_STREAM: 2059 case AUDIO_ENCODING_MPEG_L1_PACKETS: 2060 case AUDIO_ENCODING_MPEG_L1_SYSTEM: 2061 case AUDIO_ENCODING_MPEG_L2_STREAM: 2062 case AUDIO_ENCODING_MPEG_L2_PACKETS: 2063 case AUDIO_ENCODING_MPEG_L2_SYSTEM: 2064 case AUDIO_ENCODING_AC3: 2065 case AUDIO_ENCODING_ADPCM: /* is this right XXX */ 2066 case AUDIO_ENCODING_SLINEAR_LE: 2067 case AUDIO_ENCODING_SLINEAR_BE: 2068 auzero0 = 0;/* fortunately this works for any number of bits */ 2069 break; 2070 case AUDIO_ENCODING_ULINEAR_LE: 2071 case AUDIO_ENCODING_ULINEAR_BE: 2072 if (params->precision > 8) { 2073 nfill = (params->precision + NBBY - 1)/ NBBY; 2074 auzero0 = 0x80; 2075 auzero1 = 0; 2076 } else 2077 auzero0 = 0x80; 2078 break; 2079 default: 2080 DPRINTF(("audio: bad encoding %d\n", params->encoding)); 2081 auzero0 = 0; 2082 break; 2083 } 2084 if (nfill == 1) { 2085 while (--n >= 0) 2086 *p++ = auzero0; /* XXX memset */ 2087 } else /* nfill must no longer be 2 */ { 2088 if (params->encoding == AUDIO_ENCODING_ULINEAR_LE) { 2089 int k = nfill; 2090 while (--k > 0) 2091 *p++ = auzero1; 2092 n -= nfill - 1; 2093 } 2094 while (n >= nfill) { 2095 int k = nfill; 2096 *p++ = auzero0; 2097 while (--k > 0) 2098 *p++ = auzero1; 2099 2100 n -= nfill; 2101 } 2102 if (n-- > 0) /* XXX must be 1 - DIAGNOSTIC check? */ 2103 *p++ = auzero0; 2104 } 2105} 2106 2107int 2108audio_silence_copyout(struct audio_softc *sc, int n, struct uio *uio) 2109{ 2110 uint8_t zerobuf[128]; 2111 int error; 2112 int k; 2113 2114 audio_fill_silence(&sc->sc_rparams, zerobuf, sizeof zerobuf); 2115 2116 error = 0; 2117 while (n > 0 && uio->uio_resid > 0 && !error) { 2118 k = min(n, min(uio->uio_resid, sizeof zerobuf)); 2119 mutex_exit(sc->sc_lock); 2120 error = uiomove(zerobuf, k, uio); 2121 mutex_enter(sc->sc_lock); 2122 n -= k; 2123 } 2124 2125 return error; 2126} 2127 2128static int 2129uio_fetcher_fetch_to(struct audio_softc *sc, stream_fetcher_t *self, 2130 audio_stream_t *p, int max_used) 2131{ 2132 uio_fetcher_t *this; 2133 int size; 2134 int stream_space; 2135 int error; 2136 2137 KASSERT(mutex_owned(sc->sc_lock)); 2138 KASSERT(!cpu_intr_p()); 2139 KASSERT(!cpu_softintr_p()); 2140 2141 this = (uio_fetcher_t *)self; 2142 this->last_used = audio_stream_get_used(p); 2143 if (this->last_used >= this->usedhigh) 2144 return 0; 2145 /* 2146 * uio_fetcher ignores max_used and move the data as 2147 * much as possible in order to return the correct value 2148 * for audio_prinfo::seek and kfilters. 2149 */ 2150 stream_space = audio_stream_get_space(p); 2151 size = min(this->uio->uio_resid, stream_space); 2152 2153 /* the first fragment of the space */ 2154 stream_space = p->end - p->inp; 2155 if (stream_space >= size) { 2156 mutex_exit(sc->sc_lock); 2157 error = uiomove(p->inp, size, this->uio); 2158 mutex_enter(sc->sc_lock); 2159 if (error) 2160 return error; 2161 p->inp = audio_stream_add_inp(p, p->inp, size); 2162 } else { 2163 mutex_exit(sc->sc_lock); 2164 error = uiomove(p->inp, stream_space, this->uio); 2165 mutex_enter(sc->sc_lock); 2166 if (error) 2167 return error; 2168 p->inp = audio_stream_add_inp(p, p->inp, stream_space); 2169 mutex_exit(sc->sc_lock); 2170 error = uiomove(p->start, size - stream_space, this->uio); 2171 mutex_enter(sc->sc_lock); 2172 if (error) 2173 return error; 2174 p->inp = audio_stream_add_inp(p, p->inp, size - stream_space); 2175 } 2176 this->last_used = audio_stream_get_used(p); 2177 return 0; 2178} 2179 2180static int 2181null_fetcher_fetch_to(struct audio_softc *sc, stream_fetcher_t *self, 2182 audio_stream_t *p, int max_used) 2183{ 2184 2185 return 0; 2186} 2187 2188static void 2189uio_fetcher_ctor(uio_fetcher_t *this, struct uio *u, int h) 2190{ 2191 2192 this->base.fetch_to = uio_fetcher_fetch_to; 2193 this->uio = u; 2194 this->usedhigh = h; 2195} 2196 2197int 2198audio_write(struct audio_softc *sc, struct uio *uio, int ioflag) 2199{ 2200 uio_fetcher_t ufetcher; 2201 audio_stream_t stream; 2202 struct audio_ringbuffer *cb; 2203 stream_fetcher_t *fetcher; 2204 stream_filter_t *filter; 2205 uint8_t *inp, *einp; 2206 int saveerror, error, n, cc, used; 2207 2208 KASSERT(mutex_owned(sc->sc_lock)); 2209 2210 DPRINTFN(2,("audio_write: sc=%p count=%zu used=%d(hi=%d)\n", 2211 sc, uio->uio_resid, audio_stream_get_used(sc->sc_pustream), 2212 sc->sc_pr.usedhigh)); 2213 cb = &sc->sc_pr; 2214 if (cb->mmapped) 2215 return EINVAL; 2216 2217 if (uio->uio_resid == 0) { 2218 sc->sc_eof++; 2219 return 0; 2220 } 2221 2222#ifdef AUDIO_PM_IDLE 2223 if (device_is_active(&sc->dev) || sc->sc_idle) 2224 device_active(&sc->dev, DVA_SYSTEM); 2225#endif 2226 2227 /* 2228 * If half-duplex and currently recording, throw away data. 2229 */ 2230 if (!sc->sc_full_duplex && 2231 (sc->sc_mode & AUMODE_RECORD)) { 2232 uio->uio_offset += uio->uio_resid; 2233 uio->uio_resid = 0; 2234 DPRINTF(("audio_write: half-dpx read busy\n")); 2235 return 0; 2236 } 2237 2238 if (!(sc->sc_mode & AUMODE_PLAY_ALL) && sc->sc_playdrop > 0) { 2239 n = min(sc->sc_playdrop, uio->uio_resid); 2240 DPRINTF(("audio_write: playdrop %d\n", n)); 2241 uio->uio_offset += n; 2242 uio->uio_resid -= n; 2243 sc->sc_playdrop -= n; 2244 if (uio->uio_resid == 0) 2245 return 0; 2246 } 2247 2248 /** 2249 * setup filter pipeline 2250 */ 2251 uio_fetcher_ctor(&ufetcher, uio, cb->usedhigh); 2252 if (sc->sc_npfilters > 0) { 2253 fetcher = &sc->sc_pfilters[sc->sc_npfilters - 1]->base; 2254 } else { 2255 fetcher = &ufetcher.base; 2256 } 2257 2258 error = 0; 2259 mutex_enter(sc->sc_intr_lock); 2260 while (uio->uio_resid > 0 && !error) { 2261 /* wait if the first buffer is occupied */ 2262 while ((used = audio_stream_get_used(sc->sc_pustream)) 2263 >= cb->usedhigh) { 2264 DPRINTFN(2, ("audio_write: sleep used=%d lowat=%d " 2265 "hiwat=%d\n", used, 2266 cb->usedlow, cb->usedhigh)); 2267 mutex_exit(sc->sc_intr_lock); 2268 if (ioflag & IO_NDELAY) 2269 return EWOULDBLOCK; 2270 error = audio_waitio(sc, &sc->sc_wchan); 2271 if (sc->sc_dying) 2272 error = EIO; 2273 if (error) 2274 return error; 2275 mutex_enter(sc->sc_intr_lock); 2276 } 2277 inp = cb->s.inp; 2278 cb->copying = true; 2279 stream = cb->s; 2280 used = stream.used; 2281 2282 /* Write to the sc_pustream as much as possible. */ 2283 mutex_exit(sc->sc_intr_lock); 2284 if (sc->sc_npfilters > 0) { 2285 filter = sc->sc_pfilters[0]; 2286 filter->set_fetcher(filter, &ufetcher.base); 2287 fetcher = &sc->sc_pfilters[sc->sc_npfilters - 1]->base; 2288 cc = cb->blksize * 2; 2289 error = fetcher->fetch_to(sc, fetcher, &stream, cc); 2290 if (error != 0) { 2291 fetcher = &ufetcher.base; 2292 cc = sc->sc_pustream->end - sc->sc_pustream->start; 2293 error = fetcher->fetch_to(sc, fetcher, 2294 sc->sc_pustream, cc); 2295 } 2296 } else { 2297 fetcher = &ufetcher.base; 2298 cc = stream.end - stream.start; 2299 error = fetcher->fetch_to(sc, fetcher, &stream, cc); 2300 } 2301 mutex_enter(sc->sc_intr_lock); 2302 if (sc->sc_npfilters > 0) { 2303 cb->fstamp += ufetcher.last_used 2304 - audio_stream_get_used(sc->sc_pustream); 2305 } 2306 cb->s.used += stream.used - used; 2307 cb->s.inp = stream.inp; 2308 einp = cb->s.inp; 2309 2310 /* 2311 * This is a very suboptimal way of keeping track of 2312 * silence in the buffer, but it is simple. 2313 */ 2314 sc->sc_sil_count = 0; 2315 2316 /* 2317 * If the interrupt routine wants the last block filled AND 2318 * the copy did not fill the last block completely it needs to 2319 * be padded. 2320 */ 2321 if (cb->needfill && inp < einp && 2322 (inp - cb->s.start) / cb->blksize == 2323 (einp - cb->s.start) / cb->blksize) { 2324 /* Figure out how many bytes to a block boundary. */ 2325 cc = cb->blksize - (einp - cb->s.start) % cb->blksize; 2326 DPRINTF(("audio_write: partial fill %d\n", cc)); 2327 } else 2328 cc = 0; 2329 cb->needfill = false; 2330 cb->copying = false; 2331 if (!sc->sc_pbus && !cb->pause) { 2332 saveerror = error; 2333 error = audiostartp(sc); 2334 if (saveerror != 0) { 2335 /* Report the first error that occurred. */ 2336 error = saveerror; 2337 } 2338 } 2339 if (cc != 0) { 2340 DPRINTFN(1, ("audio_write: fill %d\n", cc)); 2341 audio_fill_silence(&cb->s.param, einp, cc); 2342 } 2343 } 2344 mutex_exit(sc->sc_intr_lock); 2345 2346 return error; 2347} 2348 2349int 2350audio_ioctl(struct audio_softc *sc, u_long cmd, void *addr, int flag, 2351 struct lwp *l) 2352{ 2353 const struct audio_hw_if *hw; 2354 struct audio_offset *ao; 2355 u_long stamp; 2356 int error, offs, fd; 2357 bool rbus, pbus; 2358 2359 KASSERT(mutex_owned(sc->sc_lock)); 2360 2361 DPRINTF(("audio_ioctl(%lu,'%c',%lu)\n", 2362 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff)); 2363 hw = sc->hw_if; 2364 error = 0; 2365 switch (cmd) { 2366 case FIONBIO: 2367 /* All handled in the upper FS layer. */ 2368 break; 2369 2370 case FIONREAD: 2371 *(int *)addr = audio_stream_get_used(sc->sc_rustream); 2372 break; 2373 2374 case FIOASYNC: 2375 if (*(int *)addr) { 2376 if (sc->sc_async_audio != 0) 2377 error = EBUSY; 2378 else 2379 sc->sc_async_audio = curproc->p_pid; 2380 DPRINTF(("audio_ioctl: FIOASYNC pid %d\n", 2381 curproc->p_pid)); 2382 } else 2383 sc->sc_async_audio = 0; 2384 break; 2385 2386 case AUDIO_FLUSH: 2387 DPRINTF(("AUDIO_FLUSH\n")); 2388 rbus = sc->sc_rbus; 2389 pbus = sc->sc_pbus; 2390 mutex_enter(sc->sc_intr_lock); 2391 audio_clear(sc); 2392 error = audio_initbufs(sc); 2393 if (error) { 2394 mutex_exit(sc->sc_intr_lock); 2395 return error; 2396 } 2397 if ((sc->sc_mode & AUMODE_PLAY) && !sc->sc_pbus && pbus) 2398 error = audiostartp(sc); 2399 if (!error && 2400 (sc->sc_mode & AUMODE_RECORD) && !sc->sc_rbus && rbus) 2401 error = audiostartr(sc); 2402 mutex_exit(sc->sc_intr_lock); 2403 break; 2404 2405 /* 2406 * Number of read (write) samples dropped. We don't know where or 2407 * when they were dropped. 2408 */ 2409 case AUDIO_RERROR: 2410 *(int *)addr = sc->sc_rr.drops; 2411 break; 2412 2413 case AUDIO_PERROR: 2414 *(int *)addr = sc->sc_pr.drops; 2415 break; 2416 2417 /* 2418 * Offsets into buffer. 2419 */ 2420 case AUDIO_GETIOFFS: 2421 ao = (struct audio_offset *)addr; 2422 mutex_enter(sc->sc_intr_lock); 2423 /* figure out where next DMA will start */ 2424 stamp = sc->sc_rustream == &sc->sc_rr.s 2425 ? sc->sc_rr.stamp : sc->sc_rr.fstamp; 2426 offs = sc->sc_rustream->inp - sc->sc_rustream->start; 2427 mutex_exit(sc->sc_intr_lock); 2428 ao->samples = stamp; 2429 ao->deltablks = 2430 (stamp / sc->sc_rr.blksize) - 2431 (sc->sc_rr.stamp_last / sc->sc_rr.blksize); 2432 sc->sc_rr.stamp_last = stamp; 2433 ao->offset = offs; 2434 break; 2435 2436 case AUDIO_GETOOFFS: 2437 ao = (struct audio_offset *)addr; 2438 mutex_enter(sc->sc_intr_lock); 2439 /* figure out where next DMA will start */ 2440 stamp = sc->sc_pustream == &sc->sc_pr.s 2441 ? sc->sc_pr.stamp : sc->sc_pr.fstamp; 2442 offs = sc->sc_pustream->outp - sc->sc_pustream->start 2443 + sc->sc_pr.blksize; 2444 mutex_exit(sc->sc_intr_lock); 2445 ao->samples = stamp; 2446 ao->deltablks = 2447 (stamp / sc->sc_pr.blksize) - 2448 (sc->sc_pr.stamp_last / sc->sc_pr.blksize); 2449 sc->sc_pr.stamp_last = stamp; 2450 if (sc->sc_pustream->start + offs >= sc->sc_pustream->end) 2451 offs = 0; 2452 ao->offset = offs; 2453 break; 2454 2455 /* 2456 * How many bytes will elapse until mike hears the first 2457 * sample of what we write next? 2458 */ 2459 case AUDIO_WSEEK: 2460 *(u_long *)addr = audio_stream_get_used(sc->sc_pustream); 2461 break; 2462 2463 case AUDIO_SETINFO: 2464 DPRINTF(("AUDIO_SETINFO mode=0x%x\n", sc->sc_mode)); 2465 error = audiosetinfo(sc, (struct audio_info *)addr); 2466 break; 2467 2468 case AUDIO_GETINFO: 2469 DPRINTF(("AUDIO_GETINFO\n")); 2470 error = audiogetinfo(sc, (struct audio_info *)addr, 0); 2471 break; 2472 2473 case AUDIO_GETBUFINFO: 2474 DPRINTF(("AUDIO_GETBUFINFO\n")); 2475 error = audiogetinfo(sc, (struct audio_info *)addr, 1); 2476 break; 2477 2478 case AUDIO_DRAIN: 2479 DPRINTF(("AUDIO_DRAIN\n")); 2480 mutex_enter(sc->sc_intr_lock); 2481 error = audio_drain(sc); 2482 if (!error && hw->drain) 2483 error = hw->drain(sc->hw_hdl); 2484 mutex_exit(sc->sc_intr_lock); 2485 break; 2486 2487 case AUDIO_GETDEV: 2488 DPRINTF(("AUDIO_GETDEV\n")); 2489 error = hw->getdev(sc->hw_hdl, (audio_device_t *)addr); 2490 break; 2491 2492 case AUDIO_GETENC: 2493 DPRINTF(("AUDIO_GETENC\n")); 2494 error = hw->query_encoding(sc->hw_hdl, 2495 (struct audio_encoding *)addr); 2496 break; 2497 2498 case AUDIO_GETFD: 2499 DPRINTF(("AUDIO_GETFD\n")); 2500 *(int *)addr = sc->sc_full_duplex; 2501 break; 2502 2503 case AUDIO_SETFD: 2504 DPRINTF(("AUDIO_SETFD\n")); 2505 fd = *(int *)addr; 2506 if (audio_get_props(sc) & AUDIO_PROP_FULLDUPLEX) { 2507 if (hw->setfd) 2508 error = hw->setfd(sc->hw_hdl, fd); 2509 else 2510 error = 0; 2511 if (!error) 2512 sc->sc_full_duplex = fd; 2513 } else { 2514 if (fd) 2515 error = ENOTTY; 2516 else 2517 error = 0; 2518 } 2519 break; 2520 2521 case AUDIO_GETPROPS: 2522 DPRINTF(("AUDIO_GETPROPS\n")); 2523 *(int *)addr = audio_get_props(sc); 2524 break; 2525 2526 default: 2527 if (hw->dev_ioctl) { 2528 error = hw->dev_ioctl(sc->hw_hdl, cmd, addr, flag, l); 2529 } else { 2530 DPRINTF(("audio_ioctl: unknown ioctl\n")); 2531 error = EINVAL; 2532 } 2533 break; 2534 } 2535 DPRINTF(("audio_ioctl(%lu,'%c',%lu) result %d\n", 2536 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff, error)); 2537 return error; 2538} 2539 2540int 2541audio_poll(struct audio_softc *sc, int events, struct lwp *l) 2542{ 2543 int revents; 2544 int used; 2545 2546 KASSERT(mutex_owned(sc->sc_lock)); 2547 2548 DPRINTF(("audio_poll: events=0x%x mode=%d\n", events, sc->sc_mode)); 2549 2550 revents = 0; 2551 mutex_enter(sc->sc_intr_lock); 2552 if (events & (POLLIN | POLLRDNORM)) { 2553 used = audio_stream_get_used(sc->sc_rustream); 2554 /* 2555 * If half duplex and playing, audio_read() will generate 2556 * silence at the play rate; poll for silence being 2557 * available. Otherwise, poll for recorded sound. 2558 */ 2559 if ((!sc->sc_full_duplex && (sc->sc_mode & AUMODE_PLAY)) ? 2560 sc->sc_pr.stamp > sc->sc_wstamp : 2561 used > sc->sc_rr.usedlow) 2562 revents |= events & (POLLIN | POLLRDNORM); 2563 } 2564 2565 if (events & (POLLOUT | POLLWRNORM)) { 2566 used = audio_stream_get_used(sc->sc_pustream); 2567 /* 2568 * If half duplex and recording, audio_write() will throw 2569 * away play data, which means we are always ready to write. 2570 * Otherwise, poll for play buffer being below its low water 2571 * mark. 2572 */ 2573 if ((!sc->sc_full_duplex && (sc->sc_mode & AUMODE_RECORD)) || 2574 (!(sc->sc_mode & AUMODE_PLAY_ALL) && sc->sc_playdrop > 0) || 2575 (used <= sc->sc_pr.usedlow)) 2576 revents |= events & (POLLOUT | POLLWRNORM); 2577 } 2578 mutex_exit(sc->sc_intr_lock); 2579 2580 if (revents == 0) { 2581 if (events & (POLLIN | POLLRDNORM)) 2582 selrecord(l, &sc->sc_rsel); 2583 2584 if (events & (POLLOUT | POLLWRNORM)) 2585 selrecord(l, &sc->sc_wsel); 2586 } 2587 2588 return revents; 2589} 2590 2591static void 2592filt_audiordetach(struct knote *kn) 2593{ 2594 struct audio_softc *sc; 2595 2596 sc = kn->kn_hook; 2597 mutex_enter(sc->sc_intr_lock); 2598 SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext); 2599 mutex_exit(sc->sc_intr_lock); 2600} 2601 2602static int 2603filt_audioread(struct knote *kn, long hint) 2604{ 2605 struct audio_softc *sc; 2606 2607 sc = kn->kn_hook; 2608 mutex_enter(sc->sc_intr_lock); 2609 if (!sc->sc_full_duplex && (sc->sc_mode & AUMODE_PLAY)) 2610 kn->kn_data = sc->sc_pr.stamp - sc->sc_wstamp; 2611 else 2612 kn->kn_data = audio_stream_get_used(sc->sc_rustream) 2613 - sc->sc_rr.usedlow; 2614 mutex_exit(sc->sc_intr_lock); 2615 2616 return kn->kn_data > 0; 2617} 2618 2619static const struct filterops audioread_filtops = 2620 { 1, NULL, filt_audiordetach, filt_audioread }; 2621 2622static void 2623filt_audiowdetach(struct knote *kn) 2624{ 2625 struct audio_softc *sc; 2626 2627 sc = kn->kn_hook; 2628 mutex_enter(sc->sc_intr_lock); 2629 SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext); 2630 mutex_exit(sc->sc_intr_lock); 2631} 2632 2633static int 2634filt_audiowrite(struct knote *kn, long hint) 2635{ 2636 struct audio_softc *sc; 2637 audio_stream_t *stream; 2638 2639 sc = kn->kn_hook; 2640 mutex_enter(sc->sc_intr_lock); 2641 stream = sc->sc_pustream; 2642 kn->kn_data = (stream->end - stream->start) 2643 - audio_stream_get_used(stream); 2644 mutex_exit(sc->sc_intr_lock); 2645 2646 return kn->kn_data > 0; 2647} 2648 2649static const struct filterops audiowrite_filtops = 2650 { 1, NULL, filt_audiowdetach, filt_audiowrite }; 2651 2652int 2653audio_kqfilter(struct audio_softc *sc, struct knote *kn) 2654{ 2655 struct klist *klist; 2656 2657 switch (kn->kn_filter) { 2658 case EVFILT_READ: 2659 klist = &sc->sc_rsel.sel_klist; 2660 kn->kn_fop = &audioread_filtops; 2661 break; 2662 2663 case EVFILT_WRITE: 2664 klist = &sc->sc_wsel.sel_klist; 2665 kn->kn_fop = &audiowrite_filtops; 2666 break; 2667 2668 default: 2669 return EINVAL; 2670 } 2671 2672 kn->kn_hook = sc; 2673 2674 mutex_enter(sc->sc_intr_lock); 2675 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 2676 mutex_exit(sc->sc_intr_lock); 2677 2678 return 0; 2679} 2680 2681paddr_t 2682audio_mmap(struct audio_softc *sc, off_t off, int prot) 2683{ 2684 const struct audio_hw_if *hw; 2685 struct audio_ringbuffer *cb; 2686 paddr_t rv; 2687 2688 KASSERT(mutex_owned(sc->sc_lock)); 2689 KASSERT(sc->sc_dvlock > 0); 2690 2691 DPRINTF(("audio_mmap: off=%lld, prot=%d\n", (long long)off, prot)); 2692 hw = sc->hw_if; 2693 if (!(audio_get_props(sc) & AUDIO_PROP_MMAP) || !hw->mappage) 2694 return -1; 2695#if 0 2696/* XXX 2697 * The idea here was to use the protection to determine if 2698 * we are mapping the read or write buffer, but it fails. 2699 * The VM system is broken in (at least) two ways. 2700 * 1) If you map memory VM_PROT_WRITE you SIGSEGV 2701 * when writing to it, so VM_PROT_READ|VM_PROT_WRITE 2702 * has to be used for mmapping the play buffer. 2703 * 2) Even if calling mmap() with VM_PROT_READ|VM_PROT_WRITE 2704 * audio_mmap will get called at some point with VM_PROT_READ 2705 * only. 2706 * So, alas, we always map the play buffer for now. 2707 */ 2708 if (prot == (VM_PROT_READ|VM_PROT_WRITE) || 2709 prot == VM_PROT_WRITE) 2710 cb = &sc->sc_pr; 2711 else if (prot == VM_PROT_READ) 2712 cb = &sc->sc_rr; 2713 else 2714 return -1; 2715#else 2716 cb = &sc->sc_pr; 2717#endif 2718 2719 if ((u_int)off >= cb->s.bufsize) 2720 return -1; 2721 if (!cb->mmapped) { 2722 cb->mmapped = true; 2723 if (cb == &sc->sc_pr) { 2724 audio_fill_silence(&cb->s.param, cb->s.start, 2725 cb->s.bufsize); 2726 mutex_enter(sc->sc_intr_lock); 2727 sc->sc_pustream = &cb->s; 2728 if (!sc->sc_pbus && !sc->sc_pr.pause) 2729 (void)audiostartp(sc); 2730 mutex_exit(sc->sc_intr_lock); 2731 } else { 2732 mutex_enter(sc->sc_intr_lock); 2733 sc->sc_rustream = &cb->s; 2734 if (!sc->sc_rbus && !sc->sc_rr.pause) 2735 (void)audiostartr(sc); 2736 mutex_exit(sc->sc_intr_lock); 2737 } 2738 } 2739 2740 mutex_exit(sc->sc_lock); 2741 rv = hw->mappage(sc->hw_hdl, cb->s.start, off, prot); 2742 mutex_enter(sc->sc_lock); 2743 2744 return rv; 2745} 2746 2747int 2748audiostartr(struct audio_softc *sc) 2749{ 2750 int error; 2751 2752 KASSERT(mutex_owned(sc->sc_lock)); 2753 KASSERT(mutex_owned(sc->sc_intr_lock)); 2754 2755 DPRINTF(("audiostartr: start=%p used=%d(hi=%d) mmapped=%d\n", 2756 sc->sc_rr.s.start, audio_stream_get_used(&sc->sc_rr.s), 2757 sc->sc_rr.usedhigh, sc->sc_rr.mmapped)); 2758 2759 if (!audio_can_capture(sc)) 2760 return EINVAL; 2761 2762 if (sc->hw_if->trigger_input) 2763 error = sc->hw_if->trigger_input(sc->hw_hdl, sc->sc_rr.s.start, 2764 sc->sc_rr.s.end, sc->sc_rr.blksize, 2765 audio_rint, (void *)sc, &sc->sc_rr.s.param); 2766 else 2767 error = sc->hw_if->start_input(sc->hw_hdl, sc->sc_rr.s.start, 2768 sc->sc_rr.blksize, audio_rint, (void *)sc); 2769 if (error) { 2770 DPRINTF(("audiostartr failed: %d\n", error)); 2771 return error; 2772 } 2773 sc->sc_rbus = true; 2774 return 0; 2775} 2776 2777int 2778audiostartp(struct audio_softc *sc) 2779{ 2780 int error; 2781 int used; 2782 2783 KASSERT(mutex_owned(sc->sc_lock)); 2784 KASSERT(mutex_owned(sc->sc_intr_lock)); 2785 2786 used = audio_stream_get_used(&sc->sc_pr.s); 2787 DPRINTF(("audiostartp: start=%p used=%d(hi=%d blk=%d) mmapped=%d\n", 2788 sc->sc_pr.s.start, used, sc->sc_pr.usedhigh, 2789 sc->sc_pr.blksize, sc->sc_pr.mmapped)); 2790 2791 if (!audio_can_playback(sc)) 2792 return EINVAL; 2793 2794 if (!sc->sc_pr.mmapped && used < sc->sc_pr.blksize) { 2795 cv_broadcast(&sc->sc_wchan); 2796 DPRINTF(("%s: wakeup and return\n", __func__)); 2797 return 0; 2798 } 2799 2800 if (sc->hw_if->trigger_output) { 2801 DPRINTF(("%s: call trigger_output\n", __func__)); 2802 error = sc->hw_if->trigger_output(sc->hw_hdl, sc->sc_pr.s.start, 2803 sc->sc_pr.s.end, sc->sc_pr.blksize, 2804 audio_pint, (void *)sc, &sc->sc_pr.s.param); 2805 } else { 2806 DPRINTF(("%s: call start_output\n", __func__)); 2807 error = sc->hw_if->start_output(sc->hw_hdl, 2808 __UNCONST(sc->sc_pr.s.outp), sc->sc_pr.blksize, 2809 audio_pint, (void *)sc); 2810 } 2811 if (error) { 2812 DPRINTF(("audiostartp failed: %d\n", error)); 2813 return error; 2814 } 2815 sc->sc_pbus = true; 2816 return 0; 2817} 2818 2819/* 2820 * When the play interrupt routine finds that the write isn't keeping 2821 * the buffer filled it will insert silence in the buffer to make up 2822 * for this. The part of the buffer that is filled with silence 2823 * is kept track of in a very approximate way: it starts at sc_sil_start 2824 * and extends sc_sil_count bytes. If there is already silence in 2825 * the requested area nothing is done; so when the whole buffer is 2826 * silent nothing happens. When the writer starts again sc_sil_count 2827 * is set to 0. 2828 * 2829 * XXX 2830 * Putting silence into the output buffer should not really be done 2831 * from the device interrupt handler. Consider deferring to the soft 2832 * interrupt. 2833 */ 2834static inline void 2835audio_pint_silence(struct audio_softc *sc, struct audio_ringbuffer *cb, 2836 uint8_t *inp, int cc) 2837{ 2838 uint8_t *s, *e, *p, *q; 2839 2840 KASSERT(mutex_owned(sc->sc_intr_lock)); 2841 2842 if (sc->sc_sil_count > 0) { 2843 s = sc->sc_sil_start; /* start of silence */ 2844 e = s + sc->sc_sil_count; /* end of sil., may be beyond end */ 2845 p = inp; /* adjusted pointer to area to fill */ 2846 if (p < s) 2847 p += cb->s.end - cb->s.start; 2848 q = p + cc; 2849 /* Check if there is already silence. */ 2850 if (!(s <= p && p < e && 2851 s <= q && q <= e)) { 2852 if (s <= p) 2853 sc->sc_sil_count = max(sc->sc_sil_count, q-s); 2854 DPRINTFN(5,("audio_pint_silence: fill cc=%d inp=%p, " 2855 "count=%d size=%d\n", 2856 cc, inp, sc->sc_sil_count, 2857 (int)(cb->s.end - cb->s.start))); 2858 audio_fill_silence(&cb->s.param, inp, cc); 2859 } else { 2860 DPRINTFN(5,("audio_pint_silence: already silent " 2861 "cc=%d inp=%p\n", cc, inp)); 2862 2863 } 2864 } else { 2865 sc->sc_sil_start = inp; 2866 sc->sc_sil_count = cc; 2867 DPRINTFN(5, ("audio_pint_silence: start fill %p %d\n", 2868 inp, cc)); 2869 audio_fill_silence(&cb->s.param, inp, cc); 2870 } 2871} 2872 2873static void 2874audio_softintr_rd(void *cookie) 2875{ 2876 struct audio_softc *sc = cookie; 2877 proc_t *p; 2878 pid_t pid; 2879 2880 mutex_enter(sc->sc_lock); 2881 cv_broadcast(&sc->sc_rchan); 2882 selnotify(&sc->sc_rsel, 0, NOTE_SUBMIT); 2883 if ((pid = sc->sc_async_audio) != 0) { 2884 DPRINTFN(3, ("audio_softintr_rd: sending SIGIO %d\n", pid)); 2885 mutex_enter(proc_lock); 2886 if ((p = proc_find(pid)) != NULL) 2887 psignal(p, SIGIO); 2888 mutex_exit(proc_lock); 2889 } 2890 mutex_exit(sc->sc_lock); 2891} 2892 2893static void 2894audio_softintr_wr(void *cookie) 2895{ 2896 struct audio_softc *sc = cookie; 2897 proc_t *p; 2898 pid_t pid; 2899 2900 mutex_enter(sc->sc_lock); 2901 cv_broadcast(&sc->sc_wchan); 2902 selnotify(&sc->sc_wsel, 0, NOTE_SUBMIT); 2903 if ((pid = sc->sc_async_audio) != 0) { 2904 DPRINTFN(3, ("audio_softintr_wr: sending SIGIO %d\n", pid)); 2905 mutex_enter(proc_lock); 2906 if ((p = proc_find(pid)) != NULL) 2907 psignal(p, SIGIO); 2908 mutex_exit(proc_lock); 2909 } 2910 mutex_exit(sc->sc_lock); 2911} 2912 2913/* 2914 * Called from HW driver module on completion of DMA output. 2915 * Start output of new block, wrap in ring buffer if needed. 2916 * If no more buffers to play, output zero instead. 2917 * Do a wakeup if necessary. 2918 */ 2919void 2920audio_pint(void *v) 2921{ 2922 stream_fetcher_t null_fetcher; 2923 struct audio_softc *sc; 2924 const struct audio_hw_if *hw; 2925 struct audio_ringbuffer *cb; 2926 stream_fetcher_t *fetcher; 2927 uint8_t *inp; 2928 int cc, used; 2929 int blksize; 2930 int error; 2931 2932 sc = v; 2933 2934 KASSERT(mutex_owned(sc->sc_intr_lock)); 2935 2936 if (!sc->sc_open) 2937 return; /* ignore interrupt if not open */ 2938 2939 hw = sc->hw_if; 2940 cb = &sc->sc_pr; 2941 blksize = cb->blksize; 2942 cb->s.outp = audio_stream_add_outp(&cb->s, cb->s.outp, blksize); 2943 cb->stamp += blksize; 2944 if (cb->mmapped) { 2945 DPRINTFN(5, ("audio_pint: mmapped outp=%p cc=%d inp=%p\n", 2946 cb->s.outp, blksize, cb->s.inp)); 2947 if (hw->trigger_output == NULL) 2948 (void)hw->start_output(sc->hw_hdl, __UNCONST(cb->s.outp), 2949 blksize, audio_pint, (void *)sc); 2950 return; 2951 } 2952 2953#ifdef AUDIO_INTR_TIME 2954 { 2955 struct timeval tv; 2956 u_long t; 2957 microtime(&tv); 2958 t = tv.tv_usec + 1000000 * tv.tv_sec; 2959 if (sc->sc_pnintr) { 2960 long lastdelta, totdelta; 2961 lastdelta = t - sc->sc_plastintr - sc->sc_pblktime; 2962 if (lastdelta > sc->sc_pblktime / 3) { 2963 printf("audio: play interrupt(%d) off " 2964 "relative by %ld us (%lu)\n", 2965 sc->sc_pnintr, lastdelta, 2966 sc->sc_pblktime); 2967 } 2968 totdelta = t - sc->sc_pfirstintr - 2969 sc->sc_pblktime * sc->sc_pnintr; 2970 if (totdelta > sc->sc_pblktime) { 2971 printf("audio: play interrupt(%d) off " 2972 "absolute by %ld us (%lu) (LOST)\n", 2973 sc->sc_pnintr, totdelta, 2974 sc->sc_pblktime); 2975 sc->sc_pnintr++; /* avoid repeated messages */ 2976 } 2977 } else 2978 sc->sc_pfirstintr = t; 2979 sc->sc_plastintr = t; 2980 sc->sc_pnintr++; 2981 } 2982#endif 2983 2984 used = audio_stream_get_used(&cb->s); 2985 /* 2986 * "used <= cb->usedlow" should be "used < blksize" ideally. 2987 * Some HW drivers such as uaudio(4) does not call audio_pint() 2988 * at accurate timing. If used < blksize, uaudio(4) already 2989 * request transfer of garbage data. 2990 */ 2991 if (used <= cb->usedlow && !cb->copying && sc->sc_npfilters > 0) { 2992 /* we might have data in filter pipeline */ 2993 null_fetcher.fetch_to = null_fetcher_fetch_to; 2994 fetcher = &sc->sc_pfilters[sc->sc_npfilters - 1]->base; 2995 sc->sc_pfilters[0]->set_fetcher(sc->sc_pfilters[0], 2996 &null_fetcher); 2997 used = audio_stream_get_used(sc->sc_pustream); 2998 cc = cb->s.end - cb->s.start; 2999 if (blksize * 2 < cc) 3000 cc = blksize * 2; 3001 fetcher->fetch_to(sc, fetcher, &cb->s, cc); 3002 cb->fstamp += used - audio_stream_get_used(sc->sc_pustream); 3003 used = audio_stream_get_used(&cb->s); 3004 } 3005 if (used < blksize) { 3006 /* we don't have a full block to use */ 3007 if (cb->copying) { 3008 /* writer is in progress, don't disturb */ 3009 cb->needfill = true; 3010 DPRINTFN(1, ("audio_pint: copying in progress\n")); 3011 } else { 3012 inp = cb->s.inp; 3013 cc = blksize - (inp - cb->s.start) % blksize; 3014 if (cb->pause) 3015 cb->pdrops += cc; 3016 else { 3017 cb->drops += cc; 3018 sc->sc_playdrop += cc; 3019 } 3020 audio_pint_silence(sc, cb, inp, cc); 3021 cb->s.inp = audio_stream_add_inp(&cb->s, inp, cc); 3022 3023 /* Clear next block so we keep ahead of the DMA. */ 3024 used = audio_stream_get_used(&cb->s); 3025 if (used + blksize < cb->s.end - cb->s.start) 3026 audio_pint_silence(sc, cb, cb->s.inp, blksize); 3027 } 3028 } 3029 3030 DPRINTFN(5, ("audio_pint: outp=%p cc=%d\n", cb->s.outp, blksize)); 3031 if (hw->trigger_output == NULL) { 3032 error = hw->start_output(sc->hw_hdl, __UNCONST(cb->s.outp), 3033 blksize, audio_pint, (void *)sc); 3034 if (error) { 3035 /* XXX does this really help? */ 3036 DPRINTF(("audio_pint restart failed: %d\n", error)); 3037 audio_clear(sc); 3038 } 3039 } 3040 3041 DPRINTFN(2, ("audio_pint: mode=%d pause=%d used=%d lowat=%d\n", 3042 sc->sc_mode, cb->pause, 3043 audio_stream_get_used(sc->sc_pustream), cb->usedlow)); 3044 if ((sc->sc_mode & AUMODE_PLAY) && !cb->pause) { 3045 if (audio_stream_get_used(sc->sc_pustream) <= cb->usedlow) 3046 softint_schedule(sc->sc_sih_wr); 3047 } 3048 3049 /* Possible to return one or more "phantom blocks" now. */ 3050 if (!sc->sc_full_duplex) 3051 softint_schedule(sc->sc_sih_rd); 3052} 3053 3054/* 3055 * Called from HW driver module on completion of DMA input. 3056 * Mark it as input in the ring buffer (fiddle pointers). 3057 * Do a wakeup if necessary. 3058 */ 3059void 3060audio_rint(void *v) 3061{ 3062 stream_fetcher_t null_fetcher; 3063 struct audio_softc *sc; 3064 const struct audio_hw_if *hw; 3065 struct audio_ringbuffer *cb; 3066 stream_fetcher_t *last_fetcher; 3067 int cc; 3068 int used; 3069 int blksize; 3070 int error; 3071 3072 sc = v; 3073 cb = &sc->sc_rr; 3074 3075 KASSERT(mutex_owned(sc->sc_intr_lock)); 3076 3077 if (!sc->sc_open) 3078 return; /* ignore interrupt if not open */ 3079 3080 hw = sc->hw_if; 3081 blksize = cb->blksize; 3082 cb->s.inp = audio_stream_add_inp(&cb->s, cb->s.inp, blksize); 3083 cb->stamp += blksize; 3084 if (cb->mmapped) { 3085 DPRINTFN(2, ("audio_rint: mmapped inp=%p cc=%d\n", 3086 cb->s.inp, blksize)); 3087 if (hw->trigger_input == NULL) 3088 (void)hw->start_input(sc->hw_hdl, cb->s.inp, blksize, 3089 audio_rint, (void *)sc); 3090 return; 3091 } 3092 3093#ifdef AUDIO_INTR_TIME 3094 { 3095 struct timeval tv; 3096 u_long t; 3097 microtime(&tv); 3098 t = tv.tv_usec + 1000000 * tv.tv_sec; 3099 if (sc->sc_rnintr) { 3100 long lastdelta, totdelta; 3101 lastdelta = t - sc->sc_rlastintr - sc->sc_rblktime; 3102 if (lastdelta > sc->sc_rblktime / 5) { 3103 printf("audio: record interrupt(%d) off " 3104 "relative by %ld us (%lu)\n", 3105 sc->sc_rnintr, lastdelta, 3106 sc->sc_rblktime); 3107 } 3108 totdelta = t - sc->sc_rfirstintr - 3109 sc->sc_rblktime * sc->sc_rnintr; 3110 if (totdelta > sc->sc_rblktime / 2) { 3111 sc->sc_rnintr++; 3112 printf("audio: record interrupt(%d) off " 3113 "absolute by %ld us (%lu)\n", 3114 sc->sc_rnintr, totdelta, 3115 sc->sc_rblktime); 3116 sc->sc_rnintr++; /* avoid repeated messages */ 3117 } 3118 } else 3119 sc->sc_rfirstintr = t; 3120 sc->sc_rlastintr = t; 3121 sc->sc_rnintr++; 3122 } 3123#endif 3124 3125 if (!cb->pause && sc->sc_nrfilters > 0) { 3126 null_fetcher.fetch_to = null_fetcher_fetch_to; 3127 last_fetcher = &sc->sc_rfilters[sc->sc_nrfilters - 1]->base; 3128 sc->sc_rfilters[0]->set_fetcher(sc->sc_rfilters[0], 3129 &null_fetcher); 3130 used = audio_stream_get_used(sc->sc_rustream); 3131 cc = sc->sc_rustream->end - sc->sc_rustream->start; 3132 error = last_fetcher->fetch_to 3133 (sc, last_fetcher, sc->sc_rustream, cc); 3134 cb->fstamp += audio_stream_get_used(sc->sc_rustream) - used; 3135 /* XXX what should do for error? */ 3136 } 3137 used = audio_stream_get_used(&sc->sc_rr.s); 3138 if (cb->pause) { 3139 DPRINTFN(1, ("audio_rint: pdrops %lu\n", cb->pdrops)); 3140 cb->pdrops += blksize; 3141 cb->s.outp = audio_stream_add_outp(&cb->s, cb->s.outp, blksize); 3142 } else if (used + blksize > cb->s.end - cb->s.start && !cb->copying) { 3143 DPRINTFN(1, ("audio_rint: drops %lu\n", cb->drops)); 3144 cb->drops += blksize; 3145 cb->s.outp = audio_stream_add_outp(&cb->s, cb->s.outp, blksize); 3146 } 3147 3148 DPRINTFN(2, ("audio_rint: inp=%p cc=%d\n", cb->s.inp, blksize)); 3149 if (hw->trigger_input == NULL) { 3150 error = hw->start_input(sc->hw_hdl, cb->s.inp, blksize, 3151 audio_rint, (void *)sc); 3152 if (error) { 3153 /* XXX does this really help? */ 3154 DPRINTF(("audio_rint: restart failed: %d\n", error)); 3155 audio_clear(sc); 3156 } 3157 } 3158 3159 softint_schedule(sc->sc_sih_rd); 3160} 3161 3162int 3163audio_check_params(struct audio_params *p) 3164{ 3165 3166 if (p->encoding == AUDIO_ENCODING_PCM16) { 3167 if (p->precision == 8) 3168 p->encoding = AUDIO_ENCODING_ULINEAR; 3169 else 3170 p->encoding = AUDIO_ENCODING_SLINEAR; 3171 } else if (p->encoding == AUDIO_ENCODING_PCM8) { 3172 if (p->precision == 8) 3173 p->encoding = AUDIO_ENCODING_ULINEAR; 3174 else 3175 return EINVAL; 3176 } 3177 3178 if (p->encoding == AUDIO_ENCODING_SLINEAR) 3179#if BYTE_ORDER == LITTLE_ENDIAN 3180 p->encoding = AUDIO_ENCODING_SLINEAR_LE; 3181#else 3182 p->encoding = AUDIO_ENCODING_SLINEAR_BE; 3183#endif 3184 if (p->encoding == AUDIO_ENCODING_ULINEAR) 3185#if BYTE_ORDER == LITTLE_ENDIAN 3186 p->encoding = AUDIO_ENCODING_ULINEAR_LE; 3187#else 3188 p->encoding = AUDIO_ENCODING_ULINEAR_BE; 3189#endif 3190 3191 switch (p->encoding) { 3192 case AUDIO_ENCODING_ULAW: 3193 case AUDIO_ENCODING_ALAW: 3194 if (p->precision != 8) 3195 return EINVAL; 3196 break; 3197 case AUDIO_ENCODING_ADPCM: 3198 if (p->precision != 4 && p->precision != 8) 3199 return EINVAL; 3200 break; 3201 case AUDIO_ENCODING_SLINEAR_LE: 3202 case AUDIO_ENCODING_SLINEAR_BE: 3203 case AUDIO_ENCODING_ULINEAR_LE: 3204 case AUDIO_ENCODING_ULINEAR_BE: 3205 /* XXX is: our zero-fill can handle any multiple of 8 */ 3206 if (p->precision != 8 && p->precision != 16 && 3207 p->precision != 24 && p->precision != 32) 3208 return EINVAL; 3209 if (p->precision == 8 && p->encoding == AUDIO_ENCODING_SLINEAR_BE) 3210 p->encoding = AUDIO_ENCODING_SLINEAR_LE; 3211 if (p->precision == 8 && p->encoding == AUDIO_ENCODING_ULINEAR_BE) 3212 p->encoding = AUDIO_ENCODING_ULINEAR_LE; 3213 if (p->validbits > p->precision) 3214 return EINVAL; 3215 break; 3216 case AUDIO_ENCODING_MPEG_L1_STREAM: 3217 case AUDIO_ENCODING_MPEG_L1_PACKETS: 3218 case AUDIO_ENCODING_MPEG_L1_SYSTEM: 3219 case AUDIO_ENCODING_MPEG_L2_STREAM: 3220 case AUDIO_ENCODING_MPEG_L2_PACKETS: 3221 case AUDIO_ENCODING_MPEG_L2_SYSTEM: 3222 case AUDIO_ENCODING_AC3: 3223 break; 3224 default: 3225 return EINVAL; 3226 } 3227 3228 /* sanity check # of channels*/ 3229 if (p->channels < 1 || p->channels > AUDIO_MAX_CHANNELS) 3230 return EINVAL; 3231 3232 return 0; 3233} 3234 3235int 3236audio_set_defaults(struct audio_softc *sc, u_int mode) 3237{ 3238 struct audio_info ai; 3239 3240 KASSERT(mutex_owned(sc->sc_lock)); 3241 3242 /* default parameters */ 3243 sc->sc_rparams = audio_default; 3244 sc->sc_pparams = audio_default; 3245 sc->sc_blkset = false; 3246 3247 AUDIO_INITINFO(&ai); 3248 ai.record.sample_rate = sc->sc_rparams.sample_rate; 3249 ai.record.encoding = sc->sc_rparams.encoding; 3250 ai.record.channels = sc->sc_rparams.channels; 3251 ai.record.precision = sc->sc_rparams.precision; 3252 ai.record.pause = false; 3253 ai.play.sample_rate = sc->sc_pparams.sample_rate; 3254 ai.play.encoding = sc->sc_pparams.encoding; 3255 ai.play.channels = sc->sc_pparams.channels; 3256 ai.play.precision = sc->sc_pparams.precision; 3257 ai.play.pause = false; 3258 ai.mode = mode; 3259 3260 return audiosetinfo(sc, &ai); 3261} 3262 3263int 3264au_set_lr_value(struct audio_softc *sc, mixer_ctrl_t *ct, int l, int r) 3265{ 3266 3267 KASSERT(mutex_owned(sc->sc_lock)); 3268 3269 ct->type = AUDIO_MIXER_VALUE; 3270 ct->un.value.num_channels = 2; 3271 ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l; 3272 ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r; 3273 if (sc->hw_if->set_port(sc->hw_hdl, ct) == 0) 3274 return 0; 3275 ct->un.value.num_channels = 1; 3276 ct->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2; 3277 return sc->hw_if->set_port(sc->hw_hdl, ct); 3278} 3279 3280int 3281au_set_gain(struct audio_softc *sc, struct au_mixer_ports *ports, 3282 int gain, int balance) 3283{ 3284 mixer_ctrl_t ct; 3285 int i, error; 3286 int l, r; 3287 u_int mask; 3288 int nset; 3289 3290 KASSERT(mutex_owned(sc->sc_lock)); 3291 3292 if (balance == AUDIO_MID_BALANCE) { 3293 l = r = gain; 3294 } else if (balance < AUDIO_MID_BALANCE) { 3295 l = gain; 3296 r = (balance * gain) / AUDIO_MID_BALANCE; 3297 } else { 3298 r = gain; 3299 l = ((AUDIO_RIGHT_BALANCE - balance) * gain) 3300 / AUDIO_MID_BALANCE; 3301 } 3302 DPRINTF(("au_set_gain: gain=%d balance=%d, l=%d r=%d\n", 3303 gain, balance, l, r)); 3304 3305 if (ports->index == -1) { 3306 usemaster: 3307 if (ports->master == -1) 3308 return 0; /* just ignore it silently */ 3309 ct.dev = ports->master; 3310 error = au_set_lr_value(sc, &ct, l, r); 3311 } else { 3312 ct.dev = ports->index; 3313 if (ports->isenum) { 3314 ct.type = AUDIO_MIXER_ENUM; 3315 error = sc->hw_if->get_port(sc->hw_hdl, &ct); 3316 if (error) 3317 return error; 3318 if (ports->isdual) { 3319 if (ports->cur_port == -1) 3320 ct.dev = ports->master; 3321 else 3322 ct.dev = ports->miport[ports->cur_port]; 3323 error = au_set_lr_value(sc, &ct, l, r); 3324 } else { 3325 for(i = 0; i < ports->nports; i++) 3326 if (ports->misel[i] == ct.un.ord) { 3327 ct.dev = ports->miport[i]; 3328 if (ct.dev == -1 || 3329 au_set_lr_value(sc, &ct, l, r)) 3330 goto usemaster; 3331 else 3332 break; 3333 } 3334 } 3335 } else { 3336 ct.type = AUDIO_MIXER_SET; 3337 error = sc->hw_if->get_port(sc->hw_hdl, &ct); 3338 if (error) 3339 return error; 3340 mask = ct.un.mask; 3341 nset = 0; 3342 for(i = 0; i < ports->nports; i++) { 3343 if (ports->misel[i] & mask) { 3344 ct.dev = ports->miport[i]; 3345 if (ct.dev != -1 && 3346 au_set_lr_value(sc, &ct, l, r) == 0) 3347 nset++; 3348 } 3349 } 3350 if (nset == 0) 3351 goto usemaster; 3352 } 3353 } 3354 if (!error) 3355 mixer_signal(sc); 3356 return error; 3357} 3358 3359int 3360au_get_lr_value(struct audio_softc *sc, mixer_ctrl_t *ct, int *l, int *r) 3361{ 3362 int error; 3363 3364 KASSERT(mutex_owned(sc->sc_lock)); 3365 3366 ct->un.value.num_channels = 2; 3367 if (sc->hw_if->get_port(sc->hw_hdl, ct) == 0) { 3368 *l = ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT]; 3369 *r = ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]; 3370 } else { 3371 ct->un.value.num_channels = 1; 3372 error = sc->hw_if->get_port(sc->hw_hdl, ct); 3373 if (error) 3374 return error; 3375 *r = *l = ct->un.value.level[AUDIO_MIXER_LEVEL_MONO]; 3376 } 3377 return 0; 3378} 3379 3380void 3381au_get_gain(struct audio_softc *sc, struct au_mixer_ports *ports, 3382 u_int *pgain, u_char *pbalance) 3383{ 3384 mixer_ctrl_t ct; 3385 int i, l, r, n; 3386 int lgain, rgain; 3387 3388 KASSERT(mutex_owned(sc->sc_lock)); 3389 3390 lgain = AUDIO_MAX_GAIN / 2; 3391 rgain = AUDIO_MAX_GAIN / 2; 3392 if (ports->index == -1) { 3393 usemaster: 3394 if (ports->master == -1) 3395 goto bad; 3396 ct.dev = ports->master; 3397 ct.type = AUDIO_MIXER_VALUE; 3398 if (au_get_lr_value(sc, &ct, &lgain, &rgain)) 3399 goto bad; 3400 } else { 3401 ct.dev = ports->index; 3402 if (ports->isenum) { 3403 ct.type = AUDIO_MIXER_ENUM; 3404 if (sc->hw_if->get_port(sc->hw_hdl, &ct)) 3405 goto bad; 3406 ct.type = AUDIO_MIXER_VALUE; 3407 if (ports->isdual) { 3408 if (ports->cur_port == -1) 3409 ct.dev = ports->master; 3410 else 3411 ct.dev = ports->miport[ports->cur_port]; 3412 au_get_lr_value(sc, &ct, &lgain, &rgain); 3413 } else { 3414 for(i = 0; i < ports->nports; i++) 3415 if (ports->misel[i] == ct.un.ord) { 3416 ct.dev = ports->miport[i]; 3417 if (ct.dev == -1 || 3418 au_get_lr_value(sc, &ct, 3419 &lgain, &rgain)) 3420 goto usemaster; 3421 else 3422 break; 3423 } 3424 } 3425 } else { 3426 ct.type = AUDIO_MIXER_SET; 3427 if (sc->hw_if->get_port(sc->hw_hdl, &ct)) 3428 goto bad; 3429 ct.type = AUDIO_MIXER_VALUE; 3430 lgain = rgain = n = 0; 3431 for(i = 0; i < ports->nports; i++) { 3432 if (ports->misel[i] & ct.un.mask) { 3433 ct.dev = ports->miport[i]; 3434 if (ct.dev == -1 || 3435 au_get_lr_value(sc, &ct, &l, &r)) 3436 goto usemaster; 3437 else { 3438 lgain += l; 3439 rgain += r; 3440 n++; 3441 } 3442 } 3443 } 3444 if (n != 0) { 3445 lgain /= n; 3446 rgain /= n; 3447 } 3448 } 3449 } 3450bad: 3451 if (lgain == rgain) { /* handles lgain==rgain==0 */ 3452 *pgain = lgain; 3453 *pbalance = AUDIO_MID_BALANCE; 3454 } else if (lgain < rgain) { 3455 *pgain = rgain; 3456 /* balance should be > AUDIO_MID_BALANCE */ 3457 *pbalance = AUDIO_RIGHT_BALANCE - 3458 (AUDIO_MID_BALANCE * lgain) / rgain; 3459 } else /* lgain > rgain */ { 3460 *pgain = lgain; 3461 /* balance should be < AUDIO_MID_BALANCE */ 3462 *pbalance = (AUDIO_MID_BALANCE * rgain) / lgain; 3463 } 3464} 3465 3466int 3467au_set_port(struct audio_softc *sc, struct au_mixer_ports *ports, u_int port) 3468{ 3469 mixer_ctrl_t ct; 3470 int i, error, use_mixerout; 3471 3472 KASSERT(mutex_owned(sc->sc_lock)); 3473 3474 use_mixerout = 1; 3475 if (port == 0) { 3476 if (ports->allports == 0) 3477 return 0; /* Allow this special case. */ 3478 else if (ports->isdual) { 3479 if (ports->cur_port == -1) { 3480 return 0; 3481 } else { 3482 port = ports->aumask[ports->cur_port]; 3483 ports->cur_port = -1; 3484 use_mixerout = 0; 3485 } 3486 } 3487 } 3488 if (ports->index == -1) 3489 return EINVAL; 3490 ct.dev = ports->index; 3491 if (ports->isenum) { 3492 if (port & (port-1)) 3493 return EINVAL; /* Only one port allowed */ 3494 ct.type = AUDIO_MIXER_ENUM; 3495 error = EINVAL; 3496 for(i = 0; i < ports->nports; i++) 3497 if (ports->aumask[i] == port) { 3498 if (ports->isdual && use_mixerout) { 3499 ct.un.ord = ports->mixerout; 3500 ports->cur_port = i; 3501 } else { 3502 ct.un.ord = ports->misel[i]; 3503 } 3504 error = sc->hw_if->set_port(sc->hw_hdl, &ct); 3505 break; 3506 } 3507 } else { 3508 ct.type = AUDIO_MIXER_SET; 3509 ct.un.mask = 0; 3510 for(i = 0; i < ports->nports; i++) 3511 if (ports->aumask[i] & port) 3512 ct.un.mask |= ports->misel[i]; 3513 if (port != 0 && ct.un.mask == 0) 3514 error = EINVAL; 3515 else 3516 error = sc->hw_if->set_port(sc->hw_hdl, &ct); 3517 } 3518 if (!error) 3519 mixer_signal(sc); 3520 return error; 3521} 3522 3523int 3524au_get_port(struct audio_softc *sc, struct au_mixer_ports *ports) 3525{ 3526 mixer_ctrl_t ct; 3527 int i, aumask; 3528 3529 KASSERT(mutex_owned(sc->sc_lock)); 3530 3531 if (ports->index == -1) 3532 return 0; 3533 ct.dev = ports->index; 3534 ct.type = ports->isenum ? AUDIO_MIXER_ENUM : AUDIO_MIXER_SET; 3535 if (sc->hw_if->get_port(sc->hw_hdl, &ct)) 3536 return 0; 3537 aumask = 0; 3538 if (ports->isenum) { 3539 if (ports->isdual && ports->cur_port != -1) { 3540 if (ports->mixerout == ct.un.ord) 3541 aumask = ports->aumask[ports->cur_port]; 3542 else 3543 ports->cur_port = -1; 3544 } 3545 if (aumask == 0) 3546 for(i = 0; i < ports->nports; i++) 3547 if (ports->misel[i] == ct.un.ord) 3548 aumask = ports->aumask[i]; 3549 } else { 3550 for(i = 0; i < ports->nports; i++) 3551 if (ct.un.mask & ports->misel[i]) 3552 aumask |= ports->aumask[i]; 3553 } 3554 return aumask; 3555} 3556 3557int 3558audiosetinfo(struct audio_softc *sc, struct audio_info *ai) 3559{ 3560 stream_filter_list_t pfilters, rfilters; 3561 audio_params_t pp, rp; 3562 struct audio_prinfo *r, *p; 3563 const struct audio_hw_if *hw; 3564 audio_stream_t *oldpus, *oldrus; 3565 int setmode; 3566 int error; 3567 int np, nr; 3568 unsigned int blks; 3569 int oldpblksize, oldrblksize; 3570 u_int gain; 3571 bool rbus, pbus; 3572 bool cleared, modechange, pausechange; 3573 u_char balance; 3574 3575 KASSERT(mutex_owned(sc->sc_lock)); 3576 3577 hw = sc->hw_if; 3578 if (hw == NULL) /* HW has not attached */ 3579 return ENXIO; 3580 3581 DPRINTF(("%s sc=%p ai=%p\n", __func__, sc, ai)); 3582 r = &ai->record; 3583 p = &ai->play; 3584 rbus = sc->sc_rbus; 3585 pbus = sc->sc_pbus; 3586 error = 0; 3587 cleared = false; 3588 modechange = false; 3589 pausechange = false; 3590 3591 pp = sc->sc_pparams; /* Temporary encoding storage in */ 3592 rp = sc->sc_rparams; /* case setting the modes fails. */ 3593 nr = np = 0; 3594 3595 if (SPECIFIED(p->sample_rate)) { 3596 pp.sample_rate = p->sample_rate; 3597 np++; 3598 } 3599 if (SPECIFIED(r->sample_rate)) { 3600 rp.sample_rate = r->sample_rate; 3601 nr++; 3602 } 3603 if (SPECIFIED(p->encoding)) { 3604 pp.encoding = p->encoding; 3605 np++; 3606 } 3607 if (SPECIFIED(r->encoding)) { 3608 rp.encoding = r->encoding; 3609 nr++; 3610 } 3611 if (SPECIFIED(p->precision)) { 3612 pp.precision = p->precision; 3613 /* we don't have API to specify validbits */ 3614 pp.validbits = p->precision; 3615 np++; 3616 } 3617 if (SPECIFIED(r->precision)) { 3618 rp.precision = r->precision; 3619 /* we don't have API to specify validbits */ 3620 rp.validbits = r->precision; 3621 nr++; 3622 } 3623 if (SPECIFIED(p->channels)) { 3624 pp.channels = p->channels; 3625 np++; 3626 } 3627 if (SPECIFIED(r->channels)) { 3628 rp.channels = r->channels; 3629 nr++; 3630 } 3631 3632 if (!audio_can_capture(sc)) 3633 nr = 0; 3634 if (!audio_can_playback(sc)) 3635 np = 0; 3636 3637#ifdef AUDIO_DEBUG 3638 if (audiodebug && nr > 0) 3639 audio_print_params("audiosetinfo() Setting record params:", &rp); 3640 if (audiodebug && np > 0) 3641 audio_print_params("audiosetinfo() Setting play params:", &pp); 3642#endif 3643 if (nr > 0 && (error = audio_check_params(&rp))) 3644 return error; 3645 if (np > 0 && (error = audio_check_params(&pp))) 3646 return error; 3647 3648 oldpblksize = sc->sc_pr.blksize; 3649 oldrblksize = sc->sc_rr.blksize; 3650 3651 setmode = 0; 3652 if (nr > 0) { 3653 if (!cleared) { 3654 audio_clear_intr_unlocked(sc); 3655 cleared = true; 3656 } 3657 modechange = true; 3658 setmode |= AUMODE_RECORD; 3659 } 3660 if (np > 0) { 3661 if (!cleared) { 3662 audio_clear_intr_unlocked(sc); 3663 cleared = true; 3664 } 3665 modechange = true; 3666 setmode |= AUMODE_PLAY; 3667 } 3668 3669 if (SPECIFIED(ai->mode)) { 3670 if (!cleared) { 3671 audio_clear_intr_unlocked(sc); 3672 cleared = true; 3673 } 3674 modechange = true; 3675 sc->sc_mode = ai->mode; 3676 if (sc->sc_mode & AUMODE_PLAY_ALL) 3677 sc->sc_mode |= AUMODE_PLAY; 3678 if ((sc->sc_mode & AUMODE_PLAY) && !sc->sc_full_duplex) 3679 /* Play takes precedence */ 3680 sc->sc_mode &= ~AUMODE_RECORD; 3681 } 3682 3683 oldpus = sc->sc_pustream; 3684 oldrus = sc->sc_rustream; 3685 if (modechange) { 3686 int indep; 3687 3688 indep = audio_get_props(sc) & AUDIO_PROP_INDEPENDENT; 3689 if (!indep) { 3690 if (setmode == AUMODE_RECORD) 3691 pp = rp; 3692 else if (setmode == AUMODE_PLAY) 3693 rp = pp; 3694 } 3695 memset(&pfilters, 0, sizeof(pfilters)); 3696 memset(&rfilters, 0, sizeof(rfilters)); 3697 pfilters.append = stream_filter_list_append; 3698 pfilters.prepend = stream_filter_list_prepend; 3699 pfilters.set = stream_filter_list_set; 3700 rfilters.append = stream_filter_list_append; 3701 rfilters.prepend = stream_filter_list_prepend; 3702 rfilters.set = stream_filter_list_set; 3703 /* Some device drivers change channels/sample_rate and change 3704 * no channels/sample_rate. */ 3705 error = hw->set_params(sc->hw_hdl, setmode, 3706 sc->sc_mode & (AUMODE_PLAY | AUMODE_RECORD), &pp, &rp, 3707 &pfilters, &rfilters); 3708 if (error) { 3709 DPRINTF(("%s: hw->set_params() failed with %d\n", 3710 __func__, error)); 3711 goto cleanup; 3712 } 3713 3714 audio_check_params(&pp); 3715 audio_check_params(&rp); 3716 if (!indep) { 3717 /* XXX for !indep device, we have to use the same 3718 * parameters for the hardware, not userland */ 3719 if (setmode == AUMODE_RECORD) { 3720 pp = rp; 3721 } else if (setmode == AUMODE_PLAY) { 3722 rp = pp; 3723 } 3724 } 3725 3726 if (sc->sc_pr.mmapped && pfilters.req_size > 0) { 3727 DPRINTF(("%s: mmapped, and filters are requested.\n", 3728 __func__)); 3729 error = EINVAL; 3730 goto cleanup; 3731 } 3732 3733 /* construct new filter chain */ 3734 if (setmode & AUMODE_PLAY) { 3735 error = audio_setup_pfilters(sc, &pp, &pfilters); 3736 if (error) 3737 goto cleanup; 3738 } 3739 if (setmode & AUMODE_RECORD) { 3740 error = audio_setup_rfilters(sc, &rp, &rfilters); 3741 if (error) 3742 goto cleanup; 3743 } 3744 DPRINTF(("%s: filter setup is completed.\n", __func__)); 3745 3746 /* userland formats */ 3747 sc->sc_pparams = pp; 3748 sc->sc_rparams = rp; 3749 } 3750 3751 /* Play params can affect the record params, so recalculate blksize. */ 3752 if (nr > 0 || np > 0) { 3753 audio_calc_blksize(sc, AUMODE_RECORD); 3754 audio_calc_blksize(sc, AUMODE_PLAY); 3755 } 3756#ifdef AUDIO_DEBUG 3757 if (audiodebug > 1 && nr > 0) 3758 audio_print_params("audiosetinfo() After setting record params:", &sc->sc_rparams); 3759 if (audiodebug > 1 && np > 0) 3760 audio_print_params("audiosetinfo() After setting play params:", &sc->sc_pparams); 3761#endif 3762 3763 if (SPECIFIED(p->port)) { 3764 if (!cleared) { 3765 audio_clear_intr_unlocked(sc); 3766 cleared = true; 3767 } 3768 error = au_set_port(sc, &sc->sc_outports, p->port); 3769 if (error) 3770 goto cleanup; 3771 } 3772 if (SPECIFIED(r->port)) { 3773 if (!cleared) { 3774 audio_clear_intr_unlocked(sc); 3775 cleared = true; 3776 } 3777 error = au_set_port(sc, &sc->sc_inports, r->port); 3778 if (error) 3779 goto cleanup; 3780 } 3781 if (SPECIFIED(p->gain)) { 3782 au_get_gain(sc, &sc->sc_outports, &gain, &balance); 3783 error = au_set_gain(sc, &sc->sc_outports, p->gain, balance); 3784 if (error) 3785 goto cleanup; 3786 } 3787 if (SPECIFIED(r->gain)) { 3788 au_get_gain(sc, &sc->sc_inports, &gain, &balance); 3789 error = au_set_gain(sc, &sc->sc_inports, r->gain, balance); 3790 if (error) 3791 goto cleanup; 3792 } 3793 3794 if (SPECIFIED_CH(p->balance)) { 3795 au_get_gain(sc, &sc->sc_outports, &gain, &balance); 3796 error = au_set_gain(sc, &sc->sc_outports, gain, p->balance); 3797 if (error) 3798 goto cleanup; 3799 } 3800 if (SPECIFIED_CH(r->balance)) { 3801 au_get_gain(sc, &sc->sc_inports, &gain, &balance); 3802 error = au_set_gain(sc, &sc->sc_inports, gain, r->balance); 3803 if (error) 3804 goto cleanup; 3805 } 3806 3807 if (SPECIFIED(ai->monitor_gain) && sc->sc_monitor_port != -1) { 3808 mixer_ctrl_t ct; 3809 3810 ct.dev = sc->sc_monitor_port; 3811 ct.type = AUDIO_MIXER_VALUE; 3812 ct.un.value.num_channels = 1; 3813 ct.un.value.level[AUDIO_MIXER_LEVEL_MONO] = ai->monitor_gain; 3814 error = sc->hw_if->set_port(sc->hw_hdl, &ct); 3815 if (error) 3816 goto cleanup; 3817 } 3818 3819 if (SPECIFIED_CH(p->pause)) { 3820 sc->sc_pr.pause = p->pause; 3821 pbus = !p->pause; 3822 pausechange = true; 3823 } 3824 if (SPECIFIED_CH(r->pause)) { 3825 sc->sc_rr.pause = r->pause; 3826 rbus = !r->pause; 3827 pausechange = true; 3828 } 3829 3830 if (SPECIFIED(ai->blocksize)) { 3831 int pblksize, rblksize; 3832 3833 /* Block size specified explicitly. */ 3834 if (ai->blocksize == 0) { 3835 if (!cleared) { 3836 audio_clear_intr_unlocked(sc); 3837 cleared = true; 3838 } 3839 sc->sc_blkset = false; 3840 audio_calc_blksize(sc, AUMODE_RECORD); 3841 audio_calc_blksize(sc, AUMODE_PLAY); 3842 } else { 3843 sc->sc_blkset = true; 3844 /* check whether new blocksize changes actually */ 3845 if (hw->round_blocksize == NULL) { 3846 if (!cleared) { 3847 audio_clear_intr_unlocked(sc); 3848 cleared = true; 3849 } 3850 sc->sc_pr.blksize = ai->blocksize; 3851 sc->sc_rr.blksize = ai->blocksize; 3852 } else { 3853 pblksize = hw->round_blocksize(sc->hw_hdl, 3854 ai->blocksize, AUMODE_PLAY, &sc->sc_pr.s.param); 3855 rblksize = hw->round_blocksize(sc->hw_hdl, 3856 ai->blocksize, AUMODE_RECORD, &sc->sc_rr.s.param); 3857 if (pblksize != sc->sc_pr.blksize || 3858 rblksize != sc->sc_rr.blksize) { 3859 if (!cleared) { 3860 audio_clear_intr_unlocked(sc); 3861 cleared = true; 3862 } 3863 sc->sc_pr.blksize = ai->blocksize; 3864 sc->sc_rr.blksize = ai->blocksize; 3865 } 3866 } 3867 } 3868 } 3869 3870 if (SPECIFIED(ai->mode)) { 3871 if (sc->sc_mode & AUMODE_PLAY) 3872 audio_init_play(sc); 3873 if (sc->sc_mode & AUMODE_RECORD) 3874 audio_init_record(sc); 3875 } 3876 3877 if (hw->commit_settings) { 3878 error = hw->commit_settings(sc->hw_hdl); 3879 if (error) 3880 goto cleanup; 3881 } 3882 3883 sc->sc_lastinfo = *ai; 3884 sc->sc_lastinfovalid = true; 3885 3886cleanup: 3887 if (cleared || pausechange) { 3888 int init_error; 3889 3890 mutex_enter(sc->sc_intr_lock); 3891 init_error = audio_initbufs(sc); 3892 if (init_error) goto err; 3893 if (sc->sc_pr.blksize != oldpblksize || 3894 sc->sc_rr.blksize != oldrblksize || 3895 sc->sc_pustream != oldpus || 3896 sc->sc_rustream != oldrus) 3897 audio_calcwater(sc); 3898 if ((sc->sc_mode & AUMODE_PLAY) && 3899 pbus && !sc->sc_pbus) 3900 init_error = audiostartp(sc); 3901 if (!init_error && 3902 (sc->sc_mode & AUMODE_RECORD) && 3903 rbus && !sc->sc_rbus) 3904 init_error = audiostartr(sc); 3905 err: 3906 mutex_exit(sc->sc_intr_lock); 3907 if (init_error) 3908 return init_error; 3909 } 3910 3911 /* Change water marks after initializing the buffers. */ 3912 if (SPECIFIED(ai->hiwat)) { 3913 blks = ai->hiwat; 3914 if (blks > sc->sc_pr.maxblks) 3915 blks = sc->sc_pr.maxblks; 3916 if (blks < 2) 3917 blks = 2; 3918 sc->sc_pr.usedhigh = blks * sc->sc_pr.blksize; 3919 } 3920 if (SPECIFIED(ai->lowat)) { 3921 blks = ai->lowat; 3922 if (blks > sc->sc_pr.maxblks - 1) 3923 blks = sc->sc_pr.maxblks - 1; 3924 sc->sc_pr.usedlow = blks * sc->sc_pr.blksize; 3925 } 3926 if (SPECIFIED(ai->hiwat) || SPECIFIED(ai->lowat)) { 3927 if (sc->sc_pr.usedlow > sc->sc_pr.usedhigh - sc->sc_pr.blksize) 3928 sc->sc_pr.usedlow = 3929 sc->sc_pr.usedhigh - sc->sc_pr.blksize; 3930 } 3931 3932 return error; 3933} 3934 3935int 3936audiogetinfo(struct audio_softc *sc, struct audio_info *ai, int buf_only_mode) 3937{ 3938 struct audio_prinfo *r, *p; 3939 const struct audio_hw_if *hw; 3940 3941 KASSERT(mutex_owned(sc->sc_lock)); 3942 3943 r = &ai->record; 3944 p = &ai->play; 3945 hw = sc->hw_if; 3946 if (hw == NULL) /* HW has not attached */ 3947 return ENXIO; 3948 3949 p->sample_rate = sc->sc_pparams.sample_rate; 3950 r->sample_rate = sc->sc_rparams.sample_rate; 3951 p->channels = sc->sc_pparams.channels; 3952 r->channels = sc->sc_rparams.channels; 3953 p->precision = sc->sc_pparams.precision; 3954 r->precision = sc->sc_rparams.precision; 3955 p->encoding = sc->sc_pparams.encoding; 3956 r->encoding = sc->sc_rparams.encoding; 3957 3958 if (buf_only_mode) { 3959 r->port = 0; 3960 p->port = 0; 3961 3962 r->avail_ports = 0; 3963 p->avail_ports = 0; 3964 3965 r->gain = 0; 3966 r->balance = 0; 3967 3968 p->gain = 0; 3969 p->balance = 0; 3970 } else { 3971 r->port = au_get_port(sc, &sc->sc_inports); 3972 p->port = au_get_port(sc, &sc->sc_outports); 3973 3974 r->avail_ports = sc->sc_inports.allports; 3975 p->avail_ports = sc->sc_outports.allports; 3976 3977 au_get_gain(sc, &sc->sc_inports, &r->gain, &r->balance); 3978 au_get_gain(sc, &sc->sc_outports, &p->gain, &p->balance); 3979 } 3980 3981 if (sc->sc_monitor_port != -1 && buf_only_mode == 0) { 3982 mixer_ctrl_t ct; 3983 3984 ct.dev = sc->sc_monitor_port; 3985 ct.type = AUDIO_MIXER_VALUE; 3986 ct.un.value.num_channels = 1; 3987 if (sc->hw_if->get_port(sc->hw_hdl, &ct)) 3988 ai->monitor_gain = 0; 3989 else 3990 ai->monitor_gain = 3991 ct.un.value.level[AUDIO_MIXER_LEVEL_MONO]; 3992 } else 3993 ai->monitor_gain = 0; 3994 3995 p->seek = audio_stream_get_used(sc->sc_pustream); 3996 r->seek = audio_stream_get_used(sc->sc_rustream); 3997 3998 /* 3999 * XXX samples should be a value for userland data. 4000 * But drops is a value for HW data. 4001 */ 4002 p->samples = (sc->sc_pustream == &sc->sc_pr.s 4003 ? sc->sc_pr.stamp : sc->sc_pr.fstamp) - sc->sc_pr.drops; 4004 r->samples = (sc->sc_rustream == &sc->sc_rr.s 4005 ? sc->sc_rr.stamp : sc->sc_rr.fstamp) - sc->sc_rr.drops; 4006 4007 p->eof = sc->sc_eof; 4008 r->eof = 0; 4009 4010 p->pause = sc->sc_pr.pause; 4011 r->pause = sc->sc_rr.pause; 4012 4013 p->error = sc->sc_pr.drops != 0; 4014 r->error = sc->sc_rr.drops != 0; 4015 4016 p->waiting = r->waiting = 0; /* open never hangs */ 4017 4018 p->open = (sc->sc_open & AUOPEN_WRITE) != 0; 4019 r->open = (sc->sc_open & AUOPEN_READ) != 0; 4020 4021 p->active = sc->sc_pbus; 4022 r->active = sc->sc_rbus; 4023 4024 p->buffer_size = sc->sc_pustream ? sc->sc_pustream->bufsize : 0; 4025 r->buffer_size = sc->sc_rustream ? sc->sc_rustream->bufsize : 0; 4026 4027 ai->blocksize = sc->sc_pr.blksize; 4028 if (sc->sc_pr.blksize > 0) { 4029 ai->hiwat = sc->sc_pr.usedhigh / sc->sc_pr.blksize; 4030 ai->lowat = sc->sc_pr.usedlow / sc->sc_pr.blksize; 4031 } else 4032 ai->hiwat = ai->lowat = 0; 4033 ai->mode = sc->sc_mode; 4034 4035 return 0; 4036} 4037 4038/* 4039 * Mixer driver 4040 */ 4041int 4042mixer_open(dev_t dev, struct audio_softc *sc, int flags, 4043 int ifmt, struct lwp *l) 4044{ 4045 4046 KASSERT(mutex_owned(sc->sc_lock)); 4047 4048 if (sc->hw_if == NULL) 4049 return ENXIO; 4050 4051 DPRINTF(("mixer_open: flags=0x%x sc=%p\n", flags, sc)); 4052 4053 return 0; 4054} 4055 4056/* 4057 * Remove a process from those to be signalled on mixer activity. 4058 */ 4059static void 4060mixer_remove(struct audio_softc *sc) 4061{ 4062 struct mixer_asyncs **pm, *m; 4063 pid_t pid; 4064 4065 KASSERT(mutex_owned(sc->sc_lock)); 4066 4067 pid = curproc->p_pid; 4068 for (pm = &sc->sc_async_mixer; *pm; pm = &(*pm)->next) { 4069 if ((*pm)->pid == pid) { 4070 m = *pm; 4071 *pm = m->next; 4072 kmem_free(m, sizeof(*m)); 4073 return; 4074 } 4075 } 4076} 4077 4078/* 4079 * Signal all processes waiting for the mixer. 4080 */ 4081static void 4082mixer_signal(struct audio_softc *sc) 4083{ 4084 struct mixer_asyncs *m; 4085 proc_t *p; 4086 4087 for (m = sc->sc_async_mixer; m; m = m->next) { 4088 mutex_enter(proc_lock); 4089 if ((p = proc_find(m->pid)) != NULL) 4090 psignal(p, SIGIO); 4091 mutex_exit(proc_lock); 4092 } 4093} 4094 4095/* 4096 * Close a mixer device 4097 */ 4098/* ARGSUSED */ 4099int 4100mixer_close(struct audio_softc *sc, int flags, int ifmt, 4101 struct lwp *l) 4102{ 4103 4104 KASSERT(mutex_owned(sc->sc_lock)); 4105 4106 DPRINTF(("mixer_close: sc %p\n", sc)); 4107 mixer_remove(sc); 4108 return 0; 4109} 4110 4111int 4112mixer_ioctl(struct audio_softc *sc, u_long cmd, void *addr, int flag, 4113 struct lwp *l) 4114{ 4115 const struct audio_hw_if *hw; 4116 struct mixer_asyncs *ma; 4117 mixer_ctrl_t *mc; 4118 int error; 4119 4120 DPRINTF(("mixer_ioctl(%lu,'%c',%lu)\n", 4121 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff)); 4122 hw = sc->hw_if; 4123 error = EINVAL; 4124 4125 /* we can return cached values if we are sleeping */ 4126 if (cmd != AUDIO_MIXER_READ) 4127 device_active(sc->dev, DVA_SYSTEM); 4128 4129 switch (cmd) { 4130 case FIOASYNC: 4131 if (*(int *)addr) { 4132 mutex_exit(sc->sc_lock); 4133 ma = kmem_alloc(sizeof(struct mixer_asyncs), KM_SLEEP); 4134 mutex_enter(sc->sc_lock); 4135 } else { 4136 ma = NULL; 4137 } 4138 mixer_remove(sc); /* remove old entry */ 4139 if (ma != NULL) { 4140 ma->next = sc->sc_async_mixer; 4141 ma->pid = curproc->p_pid; 4142 sc->sc_async_mixer = ma; 4143 } 4144 error = 0; 4145 break; 4146 4147 case AUDIO_GETDEV: 4148 DPRINTF(("AUDIO_GETDEV\n")); 4149 error = hw->getdev(sc->hw_hdl, (audio_device_t *)addr); 4150 break; 4151 4152 case AUDIO_MIXER_DEVINFO: 4153 DPRINTF(("AUDIO_MIXER_DEVINFO\n")); 4154 ((mixer_devinfo_t *)addr)->un.v.delta = 0; /* default */ 4155 error = hw->query_devinfo(sc->hw_hdl, (mixer_devinfo_t *)addr); 4156 break; 4157 4158 case AUDIO_MIXER_READ: 4159 DPRINTF(("AUDIO_MIXER_READ\n")); 4160 mc = (mixer_ctrl_t *)addr; 4161 4162 if (device_is_active(sc->sc_dev)) 4163 error = hw->get_port(sc->hw_hdl, mc); 4164 else if (mc->dev >= sc->sc_nmixer_states) 4165 error = ENXIO; 4166 else { 4167 int dev = mc->dev; 4168 memcpy(mc, &sc->sc_mixer_state[dev], 4169 sizeof(mixer_ctrl_t)); 4170 error = 0; 4171 } 4172 break; 4173 4174 case AUDIO_MIXER_WRITE: 4175 DPRINTF(("AUDIO_MIXER_WRITE\n")); 4176 error = hw->set_port(sc->hw_hdl, (mixer_ctrl_t *)addr); 4177 if (!error && hw->commit_settings) 4178 error = hw->commit_settings(sc->hw_hdl); 4179 if (!error) 4180 mixer_signal(sc); 4181 break; 4182 4183 default: 4184 if (hw->dev_ioctl) 4185 error = hw->dev_ioctl(sc->hw_hdl, cmd, addr, flag, l); 4186 else 4187 error = EINVAL; 4188 break; 4189 } 4190 DPRINTF(("mixer_ioctl(%lu,'%c',%lu) result %d\n", 4191 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff, error)); 4192 return error; 4193} 4194#endif /* NAUDIO > 0 */ 4195 4196#include "midi.h" 4197 4198#if NAUDIO == 0 && (NMIDI > 0 || NMIDIBUS > 0) 4199#include <sys/param.h> 4200#include <sys/systm.h> 4201#include <sys/device.h> 4202#include <sys/audioio.h> 4203#include <dev/audio_if.h> 4204#endif 4205 4206#if NAUDIO > 0 || (NMIDI > 0 || NMIDIBUS > 0) 4207int 4208audioprint(void *aux, const char *pnp) 4209{ 4210 struct audio_attach_args *arg; 4211 const char *type; 4212 4213 if (pnp != NULL) { 4214 arg = aux; 4215 switch (arg->type) { 4216 case AUDIODEV_TYPE_AUDIO: 4217 type = "audio"; 4218 break; 4219 case AUDIODEV_TYPE_MIDI: 4220 type = "midi"; 4221 break; 4222 case AUDIODEV_TYPE_OPL: 4223 type = "opl"; 4224 break; 4225 case AUDIODEV_TYPE_MPU: 4226 type = "mpu"; 4227 break; 4228 default: 4229 panic("audioprint: unknown type %d", arg->type); 4230 } 4231 aprint_normal("%s at %s", type, pnp); 4232 } 4233 return UNCONF; 4234} 4235 4236#endif /* NAUDIO > 0 || (NMIDI > 0 || NMIDIBUS > 0) */ 4237 4238#if NAUDIO > 0 4239static void 4240audio_mixer_capture(struct audio_softc *sc) 4241{ 4242 mixer_devinfo_t mi; 4243 mixer_ctrl_t *mc; 4244 4245 KASSERT(mutex_owned(sc->sc_lock)); 4246 4247 for (mi.index = 0;; mi.index++) { 4248 if (sc->hw_if->query_devinfo(sc->hw_hdl, &mi) != 0) 4249 break; 4250 KASSERT(mi.index < sc->sc_nmixer_states); 4251 if (mi.type == AUDIO_MIXER_CLASS) 4252 continue; 4253 mc = &sc->sc_mixer_state[mi.index]; 4254 mc->dev = mi.index; 4255 mc->type = mi.type; 4256 mc->un.value.num_channels = mi.un.v.num_channels; 4257 (void)sc->hw_if->get_port(sc->hw_hdl, mc); 4258 } 4259 4260 return; 4261} 4262 4263static void 4264audio_mixer_restore(struct audio_softc *sc) 4265{ 4266 mixer_devinfo_t mi; 4267 mixer_ctrl_t *mc; 4268 4269 KASSERT(mutex_owned(sc->sc_lock)); 4270 4271 for (mi.index = 0; ; mi.index++) { 4272 if (sc->hw_if->query_devinfo(sc->hw_hdl, &mi) != 0) 4273 break; 4274 if (mi.type == AUDIO_MIXER_CLASS) 4275 continue; 4276 mc = &sc->sc_mixer_state[mi.index]; 4277 (void)sc->hw_if->set_port(sc->hw_hdl, mc); 4278 } 4279 if (sc->hw_if->commit_settings) 4280 sc->hw_if->commit_settings(sc->hw_hdl); 4281 4282 return; 4283} 4284 4285#ifdef AUDIO_PM_IDLE 4286static void 4287audio_idle(void *arg) 4288{ 4289 device_t dv = arg; 4290 struct audio_softc *sc = device_private(dv); 4291 4292#ifdef PNP_DEBUG 4293 extern int pnp_debug_idle; 4294 if (pnp_debug_idle) 4295 printf("%s: idle handler called\n", device_xname(dv)); 4296#endif 4297 4298 sc->sc_idle = true; 4299 4300 /* XXX joerg Make pmf_device_suspend handle children? */ 4301 if (!pmf_device_suspend(dv, PMF_Q_SELF)) 4302 return; 4303 4304 if (!pmf_device_suspend(sc->sc_dev, PMF_Q_SELF)) 4305 pmf_device_resume(dv, PMF_Q_SELF); 4306} 4307 4308static void 4309audio_activity(device_t dv, devactive_t type) 4310{ 4311 struct audio_softc *sc = device_private(dv); 4312 4313 if (type != DVA_SYSTEM) 4314 return; 4315 4316 callout_schedule(&sc->sc_idle_counter, audio_idle_timeout * hz); 4317 4318 sc->sc_idle = false; 4319 if (!device_is_active(dv)) { 4320 /* XXX joerg How to deal with a failing resume... */ 4321 pmf_device_resume(sc->sc_dev, PMF_Q_SELF); 4322 pmf_device_resume(dv, PMF_Q_SELF); 4323 } 4324} 4325#endif 4326 4327static bool 4328audio_suspend(device_t dv, const pmf_qual_t *qual) 4329{ 4330 struct audio_softc *sc = device_private(dv); 4331 const struct audio_hw_if *hwp = sc->hw_if; 4332 4333 mutex_enter(sc->sc_lock); 4334 audio_mixer_capture(sc); 4335 mutex_enter(sc->sc_intr_lock); 4336 if (sc->sc_pbus == true) 4337 hwp->halt_output(sc->hw_hdl); 4338 if (sc->sc_rbus == true) 4339 hwp->halt_input(sc->hw_hdl); 4340 mutex_exit(sc->sc_intr_lock); 4341#ifdef AUDIO_PM_IDLE 4342 callout_halt(&sc->sc_idle_counter, sc->sc_lock); 4343#endif 4344 mutex_exit(sc->sc_lock); 4345 4346 return true; 4347} 4348 4349static bool 4350audio_resume(device_t dv, const pmf_qual_t *qual) 4351{ 4352 struct audio_softc *sc = device_private(dv); 4353 4354 mutex_enter(sc->sc_lock); 4355 if (sc->sc_lastinfovalid) 4356 audiosetinfo(sc, &sc->sc_lastinfo); 4357 audio_mixer_restore(sc); 4358 mutex_enter(sc->sc_intr_lock); 4359 if ((sc->sc_pbus == true) && !sc->sc_pr.pause) 4360 audiostartp(sc); 4361 if ((sc->sc_rbus == true) && !sc->sc_rr.pause) 4362 audiostartr(sc); 4363 mutex_exit(sc->sc_intr_lock); 4364 mutex_exit(sc->sc_lock); 4365 4366 return true; 4367} 4368 4369static void 4370audio_volume_down(device_t dv) 4371{ 4372 struct audio_softc *sc = device_private(dv); 4373 mixer_devinfo_t mi; 4374 int newgain; 4375 u_int gain; 4376 u_char balance; 4377 4378 mutex_enter(sc->sc_lock); 4379 if (sc->sc_outports.index == -1 && sc->sc_outports.master != -1) { 4380 mi.index = sc->sc_outports.master; 4381 mi.un.v.delta = 0; 4382 if (sc->hw_if->query_devinfo(sc->hw_hdl, &mi) == 0) { 4383 au_get_gain(sc, &sc->sc_outports, &gain, &balance); 4384 newgain = gain - mi.un.v.delta; 4385 if (newgain < AUDIO_MIN_GAIN) 4386 newgain = AUDIO_MIN_GAIN; 4387 au_set_gain(sc, &sc->sc_outports, newgain, balance); 4388 } 4389 } 4390 mutex_exit(sc->sc_lock); 4391} 4392 4393static void 4394audio_volume_up(device_t dv) 4395{ 4396 struct audio_softc *sc = device_private(dv); 4397 mixer_devinfo_t mi; 4398 u_int gain, newgain; 4399 u_char balance; 4400 4401 mutex_enter(sc->sc_lock); 4402 if (sc->sc_outports.index == -1 && sc->sc_outports.master != -1) { 4403 mi.index = sc->sc_outports.master; 4404 mi.un.v.delta = 0; 4405 if (sc->hw_if->query_devinfo(sc->hw_hdl, &mi) == 0) { 4406 au_get_gain(sc, &sc->sc_outports, &gain, &balance); 4407 newgain = gain + mi.un.v.delta; 4408 if (newgain > AUDIO_MAX_GAIN) 4409 newgain = AUDIO_MAX_GAIN; 4410 au_set_gain(sc, &sc->sc_outports, newgain, balance); 4411 } 4412 } 4413 mutex_exit(sc->sc_lock); 4414} 4415 4416static void 4417audio_volume_toggle(device_t dv) 4418{ 4419 struct audio_softc *sc = device_private(dv); 4420 u_int gain, newgain; 4421 u_char balance; 4422 4423 mutex_enter(sc->sc_lock); 4424 au_get_gain(sc, &sc->sc_outports, &gain, &balance); 4425 if (gain != 0) { 4426 sc->sc_lastgain = gain; 4427 newgain = 0; 4428 } else 4429 newgain = sc->sc_lastgain; 4430 au_set_gain(sc, &sc->sc_outports, newgain, balance); 4431 mutex_exit(sc->sc_lock); 4432} 4433 4434static int 4435audio_get_props(struct audio_softc *sc) 4436{ 4437 const struct audio_hw_if *hw; 4438 int props; 4439 4440 KASSERT(mutex_owned(sc->sc_lock)); 4441 4442 hw = sc->hw_if; 4443 props = hw->get_props(sc->hw_hdl); 4444 4445 /* 4446 * if neither playback nor capture properties are reported, 4447 * assume both are supported by the device driver 4448 */ 4449 if ((props & (AUDIO_PROP_PLAYBACK|AUDIO_PROP_CAPTURE)) == 0) 4450 props |= (AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE); 4451 4452 return props; 4453} 4454 4455static bool 4456audio_can_playback(struct audio_softc *sc) 4457{ 4458 return audio_get_props(sc) & AUDIO_PROP_PLAYBACK ? true : false; 4459} 4460 4461static bool 4462audio_can_capture(struct audio_softc *sc) 4463{ 4464 return audio_get_props(sc) & AUDIO_PROP_CAPTURE ? true : false; 4465} 4466 4467#endif /* NAUDIO > 0 */ 4468