The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/audio.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 982cc6f7a5584fcb73d6fcc9c7032bc3


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.