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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: audio.c,v 1.182.2.2 2005/06/12 21:28:27 tron 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.182.2.2 2005/06/12 21:28:27 tron 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 
   85 #include <dev/audio_if.h>
   86 #include <dev/audiovar.h>
   87 
   88 #include <machine/endian.h>
   89 
   90 #ifdef AUDIO_DEBUG
   91 #define DPRINTF(x)      if (audiodebug) printf x
   92 #define DPRINTFN(n,x)   if (audiodebug>(n)) printf x
   93 int     audiodebug = AUDIO_DEBUG;
   94 #else
   95 #define DPRINTF(x)
   96 #define DPRINTFN(n,x)
   97 #endif
   98 
   99 #define ROUNDSIZE(x) x &= -16   /* round to nice boundary */
  100 
  101 int     audio_blk_ms = AUDIO_BLK_MS;
  102 
  103 int     audiosetinfo(struct audio_softc *, struct audio_info *);
  104 int     audiogetinfo(struct audio_softc *, struct audio_info *);
  105 
  106 int     audio_open(dev_t, struct audio_softc *, int, int, struct proc *);
  107 int     audio_close(struct audio_softc *, int, int, struct proc *);
  108 int     audio_read(struct audio_softc *, struct uio *, int);
  109 int     audio_write(struct audio_softc *, struct uio *, int);
  110 int     audio_ioctl(struct audio_softc *, u_long, caddr_t, int, struct proc *);
  111 int     audio_poll(struct audio_softc *, int, struct proc *);
  112 int     audio_kqfilter(struct audio_softc *, struct knote *);
  113 paddr_t audio_mmap(struct audio_softc *, off_t, int);
  114 
  115 int     mixer_open(dev_t, struct audio_softc *, int, int, struct proc *);
  116 int     mixer_close(struct audio_softc *, int, int, struct proc *);
  117 int     mixer_ioctl(struct audio_softc *, u_long, caddr_t, int, struct proc *);
  118 static  void mixer_remove(struct audio_softc *, struct proc *p);
  119 static  void mixer_signal(struct audio_softc *);
  120 
  121 void    audio_init_record(struct audio_softc *);
  122 void    audio_init_play(struct audio_softc *);
  123 int     audiostartr(struct audio_softc *);
  124 int     audiostartp(struct audio_softc *);
  125 void    audio_rint(void *);
  126 void    audio_pint(void *);
  127 int     audio_check_params(struct audio_params *);
  128 
  129 void    audio_calc_blksize(struct audio_softc *, int);
  130 void    audio_fill_silence(struct audio_params *, u_char *, int);
  131 int     audio_silence_copyout(struct audio_softc *, int, struct uio *);
  132 
  133 void    audio_init_ringbuffer(struct audio_softc *, struct audio_ringbuffer *);
  134 int     audio_initbufs(struct audio_softc *);
  135 void    audio_calcwater(struct audio_softc *);
  136 static __inline int audio_sleep_timo(int *, char *, int);
  137 static __inline int audio_sleep(int *, char *);
  138 static __inline void audio_wakeup(int *);
  139 int     audio_drain(struct audio_softc *);
  140 void    audio_clear(struct audio_softc *);
  141 static __inline void audio_pint_silence
  142         (struct audio_softc *, struct audio_ringbuffer *, u_char *, int);
  143 
  144 int     audio_alloc_ring
  145         (struct audio_softc *, struct audio_ringbuffer *, int, size_t);
  146 void    audio_free_ring(struct audio_softc *, struct audio_ringbuffer *);
  147 int     audio_set_defaults(struct audio_softc *, u_int);
  148 
  149 int     audioprobe(struct device *, struct cfdata *, void *);
  150 void    audioattach(struct device *, struct device *, void *);
  151 int     audiodetach(struct device *, int);
  152 int     audioactivate(struct device *, enum devact);
  153 
  154 struct portname {
  155         const char      *name;
  156         int     mask;
  157 };
  158 static const struct portname itable[] = {
  159         { AudioNmicrophone,     AUDIO_MICROPHONE },
  160         { AudioNline,           AUDIO_LINE_IN },
  161         { AudioNcd,             AUDIO_CD },
  162         { 0 }
  163 };
  164 static const struct portname otable[] = {
  165         { AudioNspeaker,        AUDIO_SPEAKER },
  166         { AudioNheadphone,      AUDIO_HEADPHONE },
  167         { AudioNline,           AUDIO_LINE_OUT },
  168         { 0 }
  169 };
  170 void    au_setup_ports(struct audio_softc *, struct au_mixer_ports *,
  171                         mixer_devinfo_t *, const struct portname *);
  172 int     au_set_gain(struct audio_softc *, struct au_mixer_ports *,
  173                          int, int);
  174 void    au_get_gain(struct audio_softc *, struct au_mixer_ports *,
  175                          u_int *, u_char *);
  176 int     au_set_port(struct audio_softc *, struct au_mixer_ports *,
  177                          u_int);
  178 int     au_get_port(struct audio_softc *, struct au_mixer_ports *);
  179 int     au_get_lr_value(struct audio_softc *, mixer_ctrl_t *,
  180                              int *, int *r);
  181 int     au_set_lr_value(struct audio_softc *, mixer_ctrl_t *,
  182                              int, int);
  183 int     au_portof(struct audio_softc *, char *, int);
  184 
  185 dev_type_open(audioopen);
  186 dev_type_close(audioclose);
  187 dev_type_read(audioread);
  188 dev_type_write(audiowrite);
  189 dev_type_ioctl(audioioctl);
  190 dev_type_poll(audiopoll);
  191 dev_type_mmap(audiommap);
  192 dev_type_kqfilter(audiokqfilter);
  193 
  194 const struct cdevsw audio_cdevsw = {
  195         audioopen, audioclose, audioread, audiowrite, audioioctl,
  196         nostop, notty, audiopoll, audiommap, audiokqfilter,
  197 };
  198 
  199 /* The default audio mode: 8 kHz mono mu-law */
  200 const struct audio_params audio_default = {
  201         .sample_rate = 8000,
  202         .encoding = AUDIO_ENCODING_ULAW,
  203         .precision = 8,
  204         .channels = 1,
  205         .sw_code = 0,
  206         .factor = 1,
  207         .factor_denom = 1
  208 };
  209 
  210 CFATTACH_DECL(audio, sizeof(struct audio_softc),
  211     audioprobe, audioattach, audiodetach, audioactivate);
  212 
  213 extern struct cfdriver audio_cd;
  214 
  215 int
  216 audioprobe(struct device *parent, struct cfdata *match, void *aux)
  217 {
  218         struct audio_attach_args *sa = aux;
  219 
  220         DPRINTF(("audioprobe: type=%d sa=%p hw=%p\n",
  221                  sa->type, sa, sa->hwif));
  222         return (sa->type == AUDIODEV_TYPE_AUDIO) ? 1 : 0;
  223 }
  224 
  225 void
  226 audioattach(struct device *parent, struct device *self, void *aux)
  227 {
  228         struct audio_softc *sc = (void *)self;
  229         struct audio_attach_args *sa = aux;
  230         struct audio_hw_if *hwp = sa->hwif;
  231         void *hdlp = sa->hdl;
  232         int error;
  233         mixer_devinfo_t mi;
  234         int iclass, mclass, oclass, props;
  235 
  236 #ifdef DIAGNOSTIC
  237         if (hwp == 0 ||
  238             hwp->open == 0 ||
  239             hwp->close == 0 ||
  240             hwp->query_encoding == 0 ||
  241             hwp->set_params == 0 ||
  242             (hwp->start_output == 0 && hwp->trigger_output == 0) ||
  243             (hwp->start_input == 0 && hwp->trigger_input == 0) ||
  244             hwp->halt_output == 0 ||
  245             hwp->halt_input == 0 ||
  246             hwp->getdev == 0 ||
  247             hwp->set_port == 0 ||
  248             hwp->get_port == 0 ||
  249             hwp->query_devinfo == 0 ||
  250             hwp->get_props == 0) {
  251                 printf(": missing method\n");
  252                 sc->hw_if = 0;
  253                 return;
  254         }
  255 #endif
  256 
  257         props = hwp->get_props(hdlp);
  258 
  259         if (props & AUDIO_PROP_FULLDUPLEX)
  260                 printf(": full duplex");
  261         else
  262                 printf(": half duplex");
  263 
  264         if (props & AUDIO_PROP_MMAP)
  265                 printf(", mmap");
  266         if (props & AUDIO_PROP_INDEPENDENT)
  267                 printf(", independent");
  268 
  269         printf("\n");
  270 
  271         sc->hw_if = hwp;
  272         sc->hw_hdl = hdlp;
  273         sc->sc_dev = parent;
  274 
  275         error = audio_alloc_ring(sc, &sc->sc_pr, AUMODE_PLAY, AU_RING_SIZE);
  276         if (error) {
  277                 sc->hw_if = 0;
  278                 printf("audio: could not allocate play buffer\n");
  279                 return;
  280         }
  281         error = audio_alloc_ring(sc, &sc->sc_rr, AUMODE_RECORD, AU_RING_SIZE);
  282         if (error) {
  283                 audio_free_ring(sc, &sc->sc_pr);
  284                 sc->hw_if = 0;
  285                 printf("audio: could not allocate record buffer\n");
  286                 return;
  287         }
  288 
  289         sc->sc_pconvbuffer = malloc(AU_RING_SIZE, M_DEVBUF, M_WAITOK);
  290         sc->sc_pconvbuffer_size = AU_RING_SIZE;
  291         sc->sc_rconvbuffer = malloc(AU_RING_SIZE, M_DEVBUF, M_WAITOK);
  292         sc->sc_rconvbuffer_size = AU_RING_SIZE;
  293 
  294         if (audio_set_defaults(sc, 0))
  295                 panic("audioattach: audio_set_defaults failed");
  296 
  297         sc->sc_input_fragment_length = 0;
  298 
  299         iclass = mclass = oclass = -1;
  300         sc->sc_inports.index = -1;
  301         sc->sc_inports.master = -1;
  302         sc->sc_inports.nports = 0;
  303         sc->sc_inports.isenum = 0;
  304         sc->sc_inports.allports = 0;
  305         sc->sc_inports.isdual = 0;
  306         sc->sc_inports.mixerout = -1;
  307         sc->sc_inports.cur_port = -1;
  308         sc->sc_outports.index = -1;
  309         sc->sc_outports.master = -1;
  310         sc->sc_outports.nports = 0;
  311         sc->sc_outports.isenum = 0;
  312         sc->sc_outports.allports = 0;
  313         sc->sc_outports.isdual = 0;
  314         sc->sc_outports.mixerout = -1;
  315         sc->sc_outports.cur_port = -1;
  316         sc->sc_monitor_port = -1;
  317         for(mi.index = 0; ; mi.index++) {
  318                 if (hwp->query_devinfo(hdlp, &mi) != 0)
  319                         break;
  320                 if (mi.type == AUDIO_MIXER_CLASS) {
  321                         if (strcmp(mi.label.name, AudioCrecord) == 0)
  322                                 iclass = mi.mixer_class;
  323                         if (strcmp(mi.label.name, AudioCmonitor) == 0)
  324                                 mclass = mi.mixer_class;
  325                         if (strcmp(mi.label.name, AudioCoutputs) == 0)
  326                                 oclass = mi.mixer_class;
  327                 }
  328         }
  329         for(mi.index = 0; ; mi.index++) {
  330                 if (hwp->query_devinfo(hdlp, &mi) != 0)
  331                         break;
  332                 if (mi.type == AUDIO_MIXER_CLASS)
  333                         continue;
  334                 if (mi.mixer_class == iclass) {
  335                         if (strcmp(mi.label.name, AudioNmaster) == 0)
  336                                 sc->sc_inports.master = mi.index;
  337 #if 1   /* Deprecated. Use AudioNmaster. */
  338                         if (strcmp(mi.label.name, AudioNrecord) == 0)
  339                                 sc->sc_inports.master = mi.index;
  340                         if (strcmp(mi.label.name, AudioNvolume) == 0)
  341                                 sc->sc_inports.master = mi.index;
  342 #endif
  343                         if (strcmp(mi.label.name, AudioNsource) == 0) {
  344                                 if (mi.type == AUDIO_MIXER_ENUM) {
  345                                     int i;
  346                                     for(i = 0; i < mi.un.e.num_mem; i++)
  347                                         if (strcmp(mi.un.e.member[i].label.name,
  348                                                     AudioNmixerout) == 0)
  349                                                 sc->sc_inports.mixerout =
  350                                                     mi.un.e.member[i].ord;
  351                                 }
  352                                 au_setup_ports(sc, &sc->sc_inports, &mi,
  353                                     itable);
  354                         }
  355                 } else if (mi.mixer_class == mclass) {
  356                         if (strcmp(mi.label.name, AudioNmonitor) == 0)
  357                                 sc->sc_monitor_port = mi.index;
  358                 } else if (mi.mixer_class == oclass) {
  359                         if (strcmp(mi.label.name, AudioNmaster) == 0)
  360                                 sc->sc_outports.master = mi.index;
  361                         if (strcmp(mi.label.name, AudioNselect) == 0)
  362                                 au_setup_ports(sc, &sc->sc_outports, &mi,
  363                                     otable);
  364                 }
  365         }
  366         DPRINTF(("audio_attach: inputs ports=0x%x, input master=%d, "
  367                  "output ports=0x%x, output master=%d\n",
  368                  sc->sc_inports.allports, sc->sc_inports.master,
  369                  sc->sc_outports.allports, sc->sc_outports.master));
  370 }
  371 
  372 int
  373 audioactivate(struct device *self, enum devact act)
  374 {
  375         struct audio_softc *sc = (struct audio_softc *)self;
  376 
  377         switch (act) {
  378         case DVACT_ACTIVATE:
  379                 return (EOPNOTSUPP);
  380 
  381         case DVACT_DEACTIVATE:
  382                 sc->sc_dying = 1;
  383                 break;
  384         }
  385         return (0);
  386 }
  387 
  388 int
  389 audiodetach(struct device *self, int flags)
  390 {
  391         struct audio_softc *sc = (struct audio_softc *)self;
  392         int maj, mn;
  393         int s;
  394 
  395         DPRINTF(("audio_detach: sc=%p flags=%d\n", sc, flags));
  396 
  397         sc->sc_dying = 1;
  398 
  399         wakeup(&sc->sc_wchan);
  400         wakeup(&sc->sc_rchan);
  401         s = splaudio();
  402         if (--sc->sc_refcnt >= 0) {
  403                 if (tsleep(&sc->sc_refcnt, PZERO, "auddet", hz * 120))
  404                         printf("audiodetach: %s didn't detach\n",
  405                                sc->dev.dv_xname);
  406         }
  407         splx(s);
  408 
  409         /* free resources */
  410         audio_free_ring(sc, &sc->sc_pr);
  411         audio_free_ring(sc, &sc->sc_rr);
  412         free(sc->sc_pconvbuffer, M_DEVBUF);
  413         free(sc->sc_rconvbuffer, M_DEVBUF);
  414 
  415         /* locate the major number */
  416         maj = cdevsw_lookup_major(&audio_cdevsw);
  417 
  418         /* Nuke the vnodes for any open instances (calls close). */
  419         mn = self->dv_unit;
  420         vdevgone(maj, mn | SOUND_DEVICE,    mn | SOUND_DEVICE, VCHR);
  421         vdevgone(maj, mn | AUDIO_DEVICE,    mn | AUDIO_DEVICE, VCHR);
  422         vdevgone(maj, mn | AUDIOCTL_DEVICE, mn | AUDIOCTL_DEVICE, VCHR);
  423         vdevgone(maj, mn | MIXER_DEVICE,    mn | MIXER_DEVICE, VCHR);
  424 
  425         return (0);
  426 }
  427 
  428 int
  429 au_portof(struct audio_softc *sc, char *name, int class)
  430 {
  431         mixer_devinfo_t mi;
  432 
  433         for(mi.index = 0;
  434             sc->hw_if->query_devinfo(sc->hw_hdl, &mi) == 0;
  435             mi.index++)
  436                 if (mi.mixer_class == class && strcmp(mi.label.name, name) == 0)
  437                         return mi.index;
  438         return -1;
  439 }
  440 
  441 void
  442 au_setup_ports(struct audio_softc *sc, struct au_mixer_ports *ports,
  443                mixer_devinfo_t *mi, const struct portname *tbl)
  444 {
  445         int i, j;
  446 
  447         ports->index = mi->index;
  448         if (mi->type == AUDIO_MIXER_ENUM) {
  449                 ports->isenum = 1;
  450                 for(i = 0; tbl[i].name; i++)
  451                     for(j = 0; j < mi->un.e.num_mem; j++)
  452                         if (strcmp(mi->un.e.member[j].label.name,
  453                                                             tbl[i].name) == 0) {
  454                                 ports->allports |= tbl[i].mask;
  455                                 ports->aumask[ports->nports] = tbl[i].mask;
  456                                 ports->misel[ports->nports] =
  457                                     mi->un.e.member[j].ord;
  458                                 ports->miport[ports->nports] =
  459                                     au_portof(sc, mi->un.e.member[j].label.name,
  460                                     mi->mixer_class);
  461                                 if (ports->mixerout != -1 &&
  462                                     ports->miport[ports->nports++] != -1)
  463                                         ports->isdual = 1;
  464                         }
  465         } else if (mi->type == AUDIO_MIXER_SET) {
  466                 for(i = 0; tbl[i].name; i++)
  467                     for(j = 0; j < mi->un.s.num_mem; j++)
  468                         if (strcmp(mi->un.s.member[j].label.name,
  469                                                             tbl[i].name) == 0) {
  470                                 ports->allports |= tbl[i].mask;
  471                                 ports->aumask[ports->nports] = tbl[i].mask;
  472                                 ports->misel[ports->nports] =
  473                                     mi->un.s.member[j].mask;
  474                                 ports->miport[ports->nports++] =
  475                                     au_portof(sc, mi->un.s.member[j].label.name,
  476                                     mi->mixer_class);
  477                         }
  478         }
  479 }
  480 
  481 /*
  482  * Called from hardware driver.  This is where the MI audio driver gets
  483  * probed/attached to the hardware driver.
  484  */
  485 struct device *
  486 audio_attach_mi(struct audio_hw_if *ahwp, void *hdlp, struct device *dev)
  487 {
  488         struct audio_attach_args arg;
  489 
  490 #ifdef DIAGNOSTIC
  491         if (ahwp == NULL) {
  492                 aprint_error("audio_attach_mi: NULL\n");
  493                 return (0);
  494         }
  495 #endif
  496         arg.type = AUDIODEV_TYPE_AUDIO;
  497         arg.hwif = ahwp;
  498         arg.hdl = hdlp;
  499         return (config_found(dev, &arg, audioprint));
  500 }
  501 
  502 #ifdef AUDIO_DEBUG
  503 void    audio_printsc(struct audio_softc *);
  504 void    audio_print_params(char *, struct audio_params *);
  505 
  506 void
  507 audio_printsc(struct audio_softc *sc)
  508 {
  509         printf("hwhandle %p hw_if %p ", sc->hw_hdl, sc->hw_if);
  510         printf("open 0x%x mode 0x%x\n", sc->sc_open, sc->sc_mode);
  511         printf("rchan 0x%x wchan 0x%x ", sc->sc_rchan, sc->sc_wchan);
  512         printf("rring used 0x%x pring used=%d\n",
  513                sc->sc_rr.used, sc->sc_pr.used);
  514         printf("rbus 0x%x pbus 0x%x ", sc->sc_rbus, sc->sc_pbus);
  515         printf("blksize %d", sc->sc_pr.blksize);
  516         printf("hiwat %d lowat %d\n", sc->sc_pr.usedhigh, sc->sc_pr.usedlow);
  517 }
  518 
  519 void
  520 audio_print_params(char *s, struct audio_params *p)
  521 {
  522         printf("audio: %s sr=%ld, enc=%d, chan=%d, prec=%d\n", s,
  523                p->sample_rate, p->encoding, p->channels, p->precision);
  524 }
  525 #endif
  526 
  527 int
  528 audio_alloc_ring(struct audio_softc *sc, struct audio_ringbuffer *r,
  529                  int direction, size_t bufsize)
  530 {
  531         struct audio_hw_if *hw = sc->hw_if;
  532         void *hdl = sc->hw_hdl;
  533         /*
  534          * Alloc DMA play and record buffers
  535          */
  536         if (bufsize < AUMINBUF)
  537                 bufsize = AUMINBUF;
  538         ROUNDSIZE(bufsize);
  539         if (hw->round_buffersize)
  540                 bufsize = hw->round_buffersize(hdl, direction, bufsize);
  541         if (hw->allocm)
  542                 r->start = hw->allocm(hdl, direction, bufsize,
  543                                       M_DEVBUF, M_WAITOK);
  544         else
  545                 r->start = malloc(bufsize, M_DEVBUF, M_WAITOK);
  546         if (r->start == 0)
  547                 return ENOMEM;
  548         r->bufsize = bufsize;
  549         return 0;
  550 }
  551 
  552 void
  553 audio_free_ring(struct audio_softc *sc, struct audio_ringbuffer *r)
  554 {
  555 
  556         if (sc->hw_if->freem)
  557                 sc->hw_if->freem(sc->hw_hdl, r->start, M_DEVBUF);
  558         else
  559                 free(r->start, M_DEVBUF);
  560         r->start = 0;
  561 }
  562 
  563 int
  564 audioopen(dev_t dev, int flags, int ifmt, struct proc *p)
  565 {
  566         struct audio_softc *sc;
  567         int error;
  568 
  569         sc = device_lookup(&audio_cd, AUDIOUNIT(dev));
  570         if (sc == NULL)
  571                 return (ENXIO);
  572 
  573         if (sc->sc_dying)
  574                 return (EIO);
  575 
  576         sc->sc_refcnt++;
  577         switch (AUDIODEV(dev)) {
  578         case SOUND_DEVICE:
  579         case AUDIO_DEVICE:
  580                 error = audio_open(dev, sc, flags, ifmt, p);
  581                 break;
  582         case AUDIOCTL_DEVICE:
  583                 error = 0;
  584                 break;
  585         case MIXER_DEVICE:
  586                 error = mixer_open(dev, sc, flags, ifmt, p);
  587                 break;
  588         default:
  589                 error = ENXIO;
  590                 break;
  591         }
  592         if (--sc->sc_refcnt < 0)
  593                 wakeup(&sc->sc_refcnt);
  594         return (error);
  595 }
  596 
  597 int
  598 audioclose(dev_t dev, int flags, int ifmt, struct proc *p)
  599 {
  600         int unit = AUDIOUNIT(dev);
  601         struct audio_softc *sc = audio_cd.cd_devs[unit];
  602         int error;
  603 
  604         switch (AUDIODEV(dev)) {
  605         case SOUND_DEVICE:
  606         case AUDIO_DEVICE:
  607                 error = audio_close(sc, flags, ifmt, p);
  608                 break;
  609         case MIXER_DEVICE:
  610                 error = mixer_close(sc, flags, ifmt, p);
  611                 break;
  612         case AUDIOCTL_DEVICE:
  613                 error = 0;
  614                 break;
  615         default:
  616                 error = ENXIO;
  617                 break;
  618         }
  619         return (error);
  620 }
  621 
  622 int
  623 audioread(dev_t dev, struct uio *uio, int ioflag)
  624 {
  625         struct audio_softc *sc;
  626         int error;
  627 
  628         sc = device_lookup(&audio_cd, AUDIOUNIT(dev));
  629         if (sc == NULL)
  630                 return (ENXIO);
  631 
  632         if (sc->sc_dying)
  633                 return (EIO);
  634 
  635         sc->sc_refcnt++;
  636         switch (AUDIODEV(dev)) {
  637         case SOUND_DEVICE:
  638         case AUDIO_DEVICE:
  639                 error = audio_read(sc, uio, ioflag);
  640                 break;
  641         case AUDIOCTL_DEVICE:
  642         case MIXER_DEVICE:
  643                 error = ENODEV;
  644                 break;
  645         default:
  646                 error = ENXIO;
  647                 break;
  648         }
  649         if (--sc->sc_refcnt < 0)
  650                 wakeup(&sc->sc_refcnt);
  651         return (error);
  652 }
  653 
  654 int
  655 audiowrite(dev_t dev, struct uio *uio, int ioflag)
  656 {
  657         struct audio_softc *sc;
  658         int error;
  659 
  660         sc = device_lookup(&audio_cd, AUDIOUNIT(dev));
  661         if (sc == NULL)
  662                 return (ENXIO);
  663 
  664         if (sc->sc_dying)
  665                 return (EIO);
  666 
  667         sc->sc_refcnt++;
  668         switch (AUDIODEV(dev)) {
  669         case SOUND_DEVICE:
  670         case AUDIO_DEVICE:
  671                 error = audio_write(sc, uio, ioflag);
  672                 break;
  673         case AUDIOCTL_DEVICE:
  674         case MIXER_DEVICE:
  675                 error = ENODEV;
  676                 break;
  677         default:
  678                 error = ENXIO;
  679                 break;
  680         }
  681         if (--sc->sc_refcnt < 0)
  682                 wakeup(&sc->sc_refcnt);
  683         return (error);
  684 }
  685 
  686 int
  687 audioioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
  688 {
  689         int unit = AUDIOUNIT(dev);
  690         struct audio_softc *sc = audio_cd.cd_devs[unit];
  691         int error;
  692 
  693         if (sc->sc_dying)
  694                 return (EIO);
  695 
  696         sc->sc_refcnt++;
  697         switch (AUDIODEV(dev)) {
  698         case SOUND_DEVICE:
  699         case AUDIO_DEVICE:
  700         case AUDIOCTL_DEVICE:
  701                 error = audio_ioctl(sc, cmd, addr, flag, p);
  702                 break;
  703         case MIXER_DEVICE:
  704                 error = mixer_ioctl(sc, cmd, addr, flag, p);
  705                 break;
  706         default:
  707                 error = ENXIO;
  708                 break;
  709         }
  710         if (--sc->sc_refcnt < 0)
  711                 wakeup(&sc->sc_refcnt);
  712         return (error);
  713 }
  714 
  715 int
  716 audiopoll(dev_t dev, int events, struct proc *p)
  717 {
  718         int unit = AUDIOUNIT(dev);
  719         struct audio_softc *sc = audio_cd.cd_devs[unit];
  720         int error;
  721 
  722         if (sc->sc_dying)
  723                 return (EIO);
  724 
  725         sc->sc_refcnt++;
  726         switch (AUDIODEV(dev)) {
  727         case SOUND_DEVICE:
  728         case AUDIO_DEVICE:
  729                 error = audio_poll(sc, events, p);
  730                 break;
  731         case AUDIOCTL_DEVICE:
  732         case MIXER_DEVICE:
  733                 error = ENODEV;
  734                 break;
  735         default:
  736                 error = ENXIO;
  737                 break;
  738         }
  739         if (--sc->sc_refcnt < 0)
  740                 wakeup(&sc->sc_refcnt);
  741         return (error);
  742 }
  743 
  744 int
  745 audiokqfilter(dev_t dev, struct knote *kn)
  746 {
  747         int unit = AUDIOUNIT(dev);  
  748         struct audio_softc *sc = audio_cd.cd_devs[unit];
  749         int rv;
  750 
  751         if (sc->sc_dying)
  752                 return (1);
  753 
  754         sc->sc_refcnt++;
  755         switch (AUDIODEV(dev)) {
  756         case SOUND_DEVICE:
  757         case AUDIO_DEVICE:
  758                 rv = audio_kqfilter(sc, kn);
  759                 break;
  760         case AUDIOCTL_DEVICE:
  761         case MIXER_DEVICE:
  762                 rv = 1;
  763                 break;
  764         default:
  765                 rv = 1;
  766         }
  767         if (--sc->sc_refcnt < 0)
  768                 wakeup(&sc->sc_refcnt);
  769         return (rv);
  770 }
  771 
  772 paddr_t
  773 audiommap(dev_t dev, off_t off, int prot)
  774 {
  775         int unit = AUDIOUNIT(dev);
  776         struct audio_softc *sc = audio_cd.cd_devs[unit];
  777         paddr_t error;
  778 
  779         if (sc->sc_dying)
  780                 return (-1);
  781 
  782         sc->sc_refcnt++;
  783         switch (AUDIODEV(dev)) {
  784         case SOUND_DEVICE:
  785         case AUDIO_DEVICE:
  786                 error = audio_mmap(sc, off, prot);
  787                 break;
  788         case AUDIOCTL_DEVICE:
  789         case MIXER_DEVICE:
  790                 error = -1;
  791                 break;
  792         default:
  793                 error = -1;
  794                 break;
  795         }
  796         if (--sc->sc_refcnt < 0)
  797                 wakeup(&sc->sc_refcnt);
  798         return (error);
  799 }
  800 
  801 /*
  802  * Audio driver
  803  */
  804 void
  805 audio_init_ringbuffer(struct audio_softc *sc, struct audio_ringbuffer *rp)
  806 {
  807         int nblks;
  808         int blksize = rp->blksize;
  809 
  810         if (blksize < AUMINBLK)
  811                 blksize = AUMINBLK;
  812         if (blksize > rp->bufsize / AUMINNOBLK)
  813                 blksize = rp->bufsize / AUMINNOBLK;
  814         ROUNDSIZE(blksize);
  815         if (sc->hw_if->round_blocksize)
  816                 blksize = sc->hw_if->round_blocksize(sc->hw_hdl, blksize);
  817         if (blksize <= 0)
  818                 panic("audio_init_ringbuffer: blksize");
  819         nblks = rp->bufsize / blksize;
  820 
  821         DPRINTF(("audio_init_ringbuffer: blksize=%d\n", blksize));
  822         rp->blksize = blksize;
  823         rp->maxblks = nblks;
  824         rp->used = 0;
  825         rp->end = rp->start + nblks * blksize;
  826         rp->inp = rp->outp = rp->start;
  827         rp->stamp = 0;
  828         rp->drops = 0;
  829         rp->pause = 0;
  830         rp->copying = 0;
  831         rp->needfill = 0;
  832         rp->mmapped = 0;
  833 }
  834 
  835 int
  836 audio_initbufs(struct audio_softc *sc)
  837 {
  838         struct audio_hw_if *hw = sc->hw_if;
  839         int error;
  840 
  841         DPRINTF(("audio_initbufs: mode=0x%x\n", sc->sc_mode));
  842         audio_init_ringbuffer(sc, &sc->sc_rr);
  843 #if NAURATECONV > 0
  844         auconv_init_context(&sc->sc_rconv, sc->sc_rparams.hw_sample_rate,
  845                             sc->sc_rparams.sample_rate,
  846                             sc->sc_rr.start, sc->sc_rr.end);
  847 #endif
  848         if (hw->init_input && (sc->sc_mode & AUMODE_RECORD)) {
  849                 error = hw->init_input(sc->hw_hdl, sc->sc_rr.start,
  850                                        sc->sc_rr.end - sc->sc_rr.start);
  851                 if (error)
  852                         return error;
  853         }
  854 
  855         audio_init_ringbuffer(sc, &sc->sc_pr);
  856 #if NAURATECONV > 0
  857         auconv_init_context(&sc->sc_pconv, sc->sc_pparams.sample_rate,
  858                             sc->sc_pparams.hw_sample_rate,
  859                             sc->sc_pr.start, sc->sc_pr.end);
  860 #endif
  861         sc->sc_sil_count = 0;
  862         if (hw->init_output && (sc->sc_mode & AUMODE_PLAY)) {
  863                 error = hw->init_output(sc->hw_hdl, sc->sc_pr.start,
  864                                         sc->sc_pr.end - sc->sc_pr.start);
  865                 if (error)
  866                         return error;
  867         }
  868 
  869 #ifdef AUDIO_INTR_TIME
  870 #define double u_long
  871         sc->sc_pnintr = 0;
  872         sc->sc_pblktime = (u_long)(
  873             (double)sc->sc_pr.blksize * 100000 /
  874             (double)(sc->sc_pparams.precision / NBBY *
  875                      sc->sc_pparams.channels *
  876                      sc->sc_pparams.sample_rate)) * 10;
  877         DPRINTF(("audio: play blktime = %lu for %d\n",
  878                  sc->sc_pblktime, sc->sc_pr.blksize));
  879         sc->sc_rnintr = 0;
  880         sc->sc_rblktime = (u_long)(
  881             (double)sc->sc_rr.blksize * 100000 /
  882             (double)(sc->sc_rparams.precision / NBBY *
  883                      sc->sc_rparams.channels *
  884                      sc->sc_rparams.sample_rate)) * 10;
  885         DPRINTF(("audio: record blktime = %lu for %d\n",
  886                  sc->sc_rblktime, sc->sc_rr.blksize));
  887 #undef double
  888 #endif
  889 
  890         return 0;
  891 }
  892 
  893 void
  894 audio_calcwater(struct audio_softc *sc)
  895 {
  896         sc->sc_pr.usedhigh = sc->sc_pr.end - sc->sc_pr.start;
  897         sc->sc_pr.usedlow = sc->sc_pr.usedhigh * 3 / 4; /* set low at 75% */
  898         if (sc->sc_pr.usedlow == sc->sc_pr.usedhigh)
  899                 sc->sc_pr.usedlow -= sc->sc_pr.blksize;
  900         sc->sc_rr.usedhigh =
  901                 sc->sc_rr.end - sc->sc_rr.start - sc->sc_rr.blksize;
  902         sc->sc_rr.usedlow = 0;
  903 }
  904 
  905 static __inline int
  906 audio_sleep_timo(int *chan, char *label, int timo)
  907 {
  908         int st;
  909 
  910         if (!label)
  911                 label = "audio";
  912 
  913         DPRINTFN(3, ("audio_sleep_timo: chan=%p, label=%s, timo=%d\n",
  914                      chan, label, timo));
  915         *chan = 1;
  916         st = tsleep(chan, PWAIT | PCATCH, label, timo);
  917         *chan = 0;
  918 #ifdef AUDIO_DEBUG
  919         if (st != 0 && st != EINTR)
  920             DPRINTF(("audio_sleep: woke up st=%d\n", st));
  921 #endif
  922         return (st);
  923 }
  924 
  925 static __inline int
  926 audio_sleep(int *chan, char *label)
  927 {
  928         return audio_sleep_timo(chan, label, 0);
  929 }
  930 
  931 /* call at splaudio() */
  932 static __inline void
  933 audio_wakeup(int *chan)
  934 {
  935         DPRINTFN(3, ("audio_wakeup: chan=%p, *chan=%d\n", chan, *chan));
  936         if (*chan) {
  937                 wakeup(chan);
  938                 *chan = 0;
  939         }
  940 }
  941 
  942 int
  943 audio_open(dev_t dev, struct audio_softc *sc, int flags, int ifmt,
  944            struct proc *p)
  945 {
  946         int error;
  947         u_int mode;
  948         struct audio_hw_if *hw;
  949 
  950         hw = sc->hw_if;
  951         if (!hw)
  952                 return ENXIO;
  953 
  954         DPRINTF(("audio_open: flags=0x%x sc=%p hdl=%p\n",
  955                  flags, sc, sc->hw_hdl));
  956 
  957         if ((sc->sc_open & (AUOPEN_READ|AUOPEN_WRITE)) != 0)
  958                 return (EBUSY);
  959 
  960         error = hw->open(sc->hw_hdl, flags);
  961         if (error)
  962                 return (error);
  963 
  964         sc->sc_async_audio = 0;
  965         sc->sc_rchan = 0;
  966         sc->sc_wchan = 0;
  967         sc->sc_sil_count = 0;
  968         sc->sc_rbus = 0;
  969         sc->sc_pbus = 0;
  970         sc->sc_eof = 0;
  971         sc->sc_playdrop = 0;
  972 
  973         sc->sc_full_duplex = 0;
  974 /* doesn't always work right on SB.
  975                 (flags & (FWRITE|FREAD)) == (FWRITE|FREAD) &&
  976                 (hw->get_props(sc->hw_hdl) & AUDIO_PROP_FULLDUPLEX);
  977 */
  978 
  979         mode = 0;
  980         if (flags & FREAD) {
  981                 sc->sc_open |= AUOPEN_READ;
  982                 mode |= AUMODE_RECORD;
  983         }
  984         if (flags & FWRITE) {
  985                 sc->sc_open |= AUOPEN_WRITE;
  986                 mode |= AUMODE_PLAY | AUMODE_PLAY_ALL;
  987         }
  988 
  989         /*
  990          * Multiplex device: /dev/audio (MU-Law) and /dev/sound (linear)
  991          * The /dev/audio is always (re)set to 8-bit MU-Law mono
  992          * For the other devices, you get what they were last set to.
  993          */
  994         if (ISDEVAUDIO(dev)) {
  995                 error = audio_set_defaults(sc, mode);
  996         } else {
  997                 struct audio_info ai;
  998 
  999                 AUDIO_INITINFO(&ai);
 1000                 ai.mode = mode;
 1001                 error = audiosetinfo(sc, &ai);
 1002         }
 1003         if (error)
 1004                 goto bad;
 1005 
 1006 #ifdef DIAGNOSTIC
 1007         /*
 1008          * Sample rate and precision are supposed to be set to proper
 1009          * default values by the hardware driver, so that it may give
 1010          * us these values.
 1011          */
 1012         if (sc->sc_rparams.precision == 0 || sc->sc_pparams.precision == 0) {
 1013                 printf("audio_open: 0 precision\n");
 1014                 return EINVAL;
 1015         }
 1016 #endif
 1017 
 1018         /* audio_close() decreases sc_pr.usedlow, recalculate here */
 1019         audio_calcwater(sc);
 1020 
 1021         DPRINTF(("audio_open: done sc_mode = 0x%x\n", sc->sc_mode));
 1022 
 1023         return 0;
 1024 
 1025 bad:
 1026         hw->close(sc->hw_hdl);
 1027         sc->sc_open = 0;
 1028         sc->sc_mode = 0;
 1029         sc->sc_full_duplex = 0;
 1030         return error;
 1031 }
 1032 
 1033 /*
 1034  * Must be called from task context.
 1035  */
 1036 void
 1037 audio_init_record(struct audio_softc *sc)
 1038 {
 1039         int s = splaudio();
 1040 
 1041         if (sc->hw_if->speaker_ctl &&
 1042             (!sc->sc_full_duplex || (sc->sc_mode & AUMODE_PLAY) == 0))
 1043                 sc->hw_if->speaker_ctl(sc->hw_hdl, SPKR_OFF);
 1044         sc->sc_rconvbuffer_begin = 0;
 1045         sc->sc_rconvbuffer_end = 0;
 1046         splx(s);
 1047 }
 1048 
 1049 /*
 1050  * Must be called from task context.
 1051  */
 1052 void
 1053 audio_init_play(struct audio_softc *sc)
 1054 {
 1055         int s = splaudio();
 1056 
 1057         sc->sc_wstamp = sc->sc_pr.stamp;
 1058         if (sc->hw_if->speaker_ctl)
 1059                 sc->hw_if->speaker_ctl(sc->hw_hdl, SPKR_ON);
 1060         sc->sc_input_fragment_length = 0;
 1061         splx(s);
 1062 }
 1063 
 1064 int
 1065 audio_drain(struct audio_softc *sc)
 1066 {
 1067         int error, drops;
 1068         struct audio_ringbuffer *cb = &sc->sc_pr;
 1069         int s;
 1070 
 1071         DPRINTF(("audio_drain: enter busy=%d used=%d\n",
 1072                  sc->sc_pbus, sc->sc_pr.used));
 1073         if (sc->sc_pr.mmapped || sc->sc_pr.used <= 0)
 1074                 return 0;
 1075         if (!sc->sc_pbus) {
 1076                 /* We've never started playing, probably because the
 1077                  * block was too short.  Pad it and start now.
 1078                  */
 1079                 int cc;
 1080                 u_char *inp = cb->inp;
 1081 
 1082                 cc = cb->blksize - (inp - cb->start) % cb->blksize;
 1083                 audio_fill_silence(&sc->sc_pparams, inp, cc);
 1084                 inp += cc;
 1085                 if (inp >= cb->end)
 1086                         inp = cb->start;
 1087                 s = splaudio();
 1088                 cb->used += cc;
 1089                 cb->inp = inp;
 1090                 error = audiostartp(sc);
 1091                 splx(s);
 1092                 if (error)
 1093                         return error;
 1094         }
 1095         /*
 1096          * Play until a silence block has been played, then we
 1097          * know all has been drained.
 1098          * XXX This should be done some other way to avoid
 1099          * playing silence.
 1100          */
 1101 #ifdef DIAGNOSTIC
 1102         if (cb->copying) {
 1103                 printf("audio_drain: copying in progress!?!\n");
 1104                 cb->copying = 0;
 1105         }
 1106 #endif
 1107         drops = cb->drops;
 1108         error = 0;
 1109         s = splaudio();
 1110         while (cb->drops == drops && !error) {
 1111                 DPRINTF(("audio_drain: used=%d, drops=%ld\n",
 1112                          sc->sc_pr.used, cb->drops));
 1113                 /*
 1114                  * When the process is exiting, it ignores all signals and
 1115                  * we can't interrupt this sleep, so we set a timeout
 1116                  * just in case.
 1117                  */
 1118                 error = audio_sleep_timo(&sc->sc_wchan, "aud_dr", 30*hz);
 1119                 if (sc->sc_dying)
 1120                         error = EIO;
 1121         }
 1122         splx(s);
 1123         return error;
 1124 }
 1125 
 1126 /*
 1127  * Close an audio chip.
 1128  */
 1129 /* ARGSUSED */
 1130 int
 1131 audio_close(struct audio_softc *sc, int flags, int ifmt, struct proc *p)
 1132 {
 1133         struct audio_hw_if *hw = sc->hw_if;
 1134         int s;
 1135 
 1136         DPRINTF(("audio_close: sc=%p\n", sc));
 1137 
 1138         s = splaudio();
 1139         /* Stop recording. */
 1140         if ((flags & FREAD) && sc->sc_rbus) {
 1141                 /*
 1142                  * XXX Some drivers (e.g. SB) use the same routine
 1143                  * to halt input and output so don't halt input if
 1144                  * in full duplex mode.  These drivers should be fixed.
 1145                  */
 1146                 if (!sc->sc_full_duplex ||
 1147                     sc->hw_if->halt_input != sc->hw_if->halt_output)
 1148                         sc->hw_if->halt_input(sc->hw_hdl);
 1149                 sc->sc_rbus = 0;
 1150         }
 1151         /*
 1152          * Block until output drains, but allow ^C interrupt.
 1153          */
 1154         sc->sc_pr.usedlow = sc->sc_pr.blksize;  /* avoid excessive wakeups */
 1155         /*
 1156          * If there is pending output, let it drain (unless
 1157          * the output is paused).
 1158          */
 1159         if ((flags & FWRITE) && sc->sc_pbus) {
 1160                 if (!sc->sc_pr.pause && !audio_drain(sc) && hw->drain)
 1161                         (void)hw->drain(sc->hw_hdl);
 1162                 sc->hw_if->halt_output(sc->hw_hdl);
 1163                 sc->sc_pbus = 0;
 1164         }
 1165 
 1166         hw->close(sc->hw_hdl);
 1167 
 1168         if (flags & FREAD) {
 1169                 sc->sc_open &= ~AUOPEN_READ;
 1170                 sc->sc_mode &= ~AUMODE_RECORD;
 1171         }
 1172         if (flags & FWRITE) {
 1173                 sc->sc_open &= ~AUOPEN_WRITE;
 1174                 sc->sc_mode &= ~(AUMODE_PLAY|AUMODE_PLAY_ALL);
 1175         }
 1176 
 1177         sc->sc_async_audio = 0;
 1178         sc->sc_full_duplex = 0;
 1179         splx(s);
 1180         DPRINTF(("audio_close: done\n"));
 1181 
 1182         return (0);
 1183 }
 1184 
 1185 int
 1186 audio_read(struct audio_softc *sc, struct uio *uio, int ioflag)
 1187 {
 1188         struct audio_ringbuffer *cb = &sc->sc_rr;
 1189         u_char *outp;
 1190         int error, s, used, cc, n;
 1191         const struct audio_params *params;
 1192         int hw_bits_per_sample;
 1193 
 1194         if (cb->mmapped)
 1195                 return EINVAL;
 1196 
 1197         DPRINTFN(1,("audio_read: cc=%lu mode=%d\n",
 1198                     (unsigned long)uio->uio_resid, sc->sc_mode));
 1199 
 1200         params = &sc->sc_rparams;
 1201         switch (params->hw_encoding) {
 1202         case AUDIO_ENCODING_SLINEAR_LE:
 1203         case AUDIO_ENCODING_SLINEAR_BE:
 1204         case AUDIO_ENCODING_ULINEAR_LE:
 1205         case AUDIO_ENCODING_ULINEAR_BE:
 1206                 hw_bits_per_sample = params->hw_channels * params->precision
 1207                         * params->factor;
 1208                 break;
 1209         default:
 1210                 hw_bits_per_sample = 8 * params->factor / params->factor_denom;
 1211         }
 1212         error = 0;
 1213         /*
 1214          * If hardware is half-duplex and currently playing, return
 1215          * silence blocks based on the number of blocks we have output.
 1216          */
 1217         if (!sc->sc_full_duplex &&
 1218             (sc->sc_mode & AUMODE_PLAY)) {
 1219                 while (uio->uio_resid > 0 && !error) {
 1220                         s = splaudio();
 1221                         for(;;) {
 1222                                 cc = sc->sc_pr.stamp - sc->sc_wstamp;
 1223                                 if (cc > 0)
 1224                                         break;
 1225                                 DPRINTF(("audio_read: stamp=%lu, wstamp=%lu\n",
 1226                                          sc->sc_pr.stamp, sc->sc_wstamp));
 1227                                 if (ioflag & IO_NDELAY) {
 1228                                         splx(s);
 1229                                         return (EWOULDBLOCK);
 1230                                 }
 1231                                 error = audio_sleep(&sc->sc_rchan, "aud_hr");
 1232                                 if (sc->sc_dying)
 1233                                         error = EIO;
 1234                                 if (error) {
 1235                                         splx(s);
 1236                                         return (error);
 1237                                 }
 1238                         }
 1239                         splx(s);
 1240 
 1241                         if (uio->uio_resid < cc)
 1242                                 cc = uio->uio_resid;
 1243                         DPRINTFN(1,("audio_read: reading in write mode, "
 1244                                     "cc=%d\n", cc));
 1245                         error = audio_silence_copyout(sc, cc, uio);
 1246                         sc->sc_wstamp += cc;
 1247                 }
 1248                 return (error);
 1249         }
 1250         while (uio->uio_resid > 0 && !error) {
 1251                 if (sc->sc_rconvbuffer_end - sc->sc_rconvbuffer_begin <= 0) {
 1252                         s = splaudio();
 1253                         while (cb->used * 8 < hw_bits_per_sample) {
 1254                                 if (!sc->sc_rbus) {
 1255                                         error = audiostartr(sc);
 1256                                         if (error) {
 1257                                                 splx(s);
 1258                                                 return (error);
 1259                                         }
 1260                                 }
 1261                                 if (ioflag & IO_NDELAY) {
 1262                                         splx(s);
 1263                                         return (EWOULDBLOCK);
 1264                                 }
 1265                                 DPRINTFN(1, ("audio_read: sleep used=%d\n",
 1266                                              cb->used));
 1267                                 error = audio_sleep(&sc->sc_rchan, "aud_rd");
 1268                                 if (sc->sc_dying)
 1269                                         error = EIO;
 1270                                 if (error) {
 1271                                         splx(s);
 1272                                         return error;
 1273                                 }
 1274                         }
 1275 
 1276                         /*
 1277                          * Move data in the ring buffer to sc_rconvbuffer as
 1278                          * possible with/without rate conversion.
 1279                          */
 1280                         used = cb->used;
 1281                         outp = cb->outp;
 1282                         cb->copying = 1;
 1283                         splx(s);
 1284                         cc = used - cb->usedlow; /* maximum to read */
 1285                         if (cc > sc->sc_rconvbuffer_size)
 1286                                 cc = sc->sc_rconvbuffer_size;
 1287                         n = cc * params->factor / params->factor_denom;
 1288                         if (n < cc)
 1289                                 cc = n;
 1290                         /* cc must be aligned by the sample size */
 1291                         cc = (cc * 8 / hw_bits_per_sample) * hw_bits_per_sample / 8;
 1292 #ifdef DIAGNOSTIC
 1293                         if (cc == 0)
 1294                                 printf("audio_read: cc=0 hw_bits_per_sample=%d\n",
 1295                                        hw_bits_per_sample);
 1296 #endif
 1297 
 1298                         /*
 1299                          * The format of data in the ring buffer is
 1300                          * [hw_sample_rate, hw_encoding, hw_precision, hw_channels]
 1301                          */
 1302 #if NAURATECONV > 0
 1303                         sc->sc_rconvbuffer_end =
 1304                                 auconv_record(&sc->sc_rconv, params,
 1305                                               sc->sc_rconvbuffer, outp, cc);
 1306 #else
 1307                         n = cb->end - outp;
 1308                         if (cc <= n) {
 1309                                 memcpy(sc->sc_rconvbuffer, outp, cc);
 1310                         } else {
 1311                                 memcpy(sc->sc_rconvbuffer, outp, n);
 1312                                 memcpy(sc->sc_rconvbuffer + n, cb->start,
 1313                                        cc - n);
 1314                         }
 1315                         sc->sc_rconvbuffer_end = cc;
 1316 #endif /* !NAURATECONV */
 1317                         /*
 1318                          * The format of data in sc_rconvbuffer is
 1319                          * [sample_rate, hw_encoding, hw_precision, channels]
 1320                          */
 1321                         outp += cc;
 1322                         if (outp >= cb->end)
 1323                                 outp -= cb->end - cb->start;
 1324                         s = splaudio();
 1325                         cb->outp = outp;
 1326                         cb->used -= cc;
 1327                         cb->copying = 0;
 1328                         splx(s);
 1329 
 1330                         if (params->sw_code) {
 1331                                 cc = sc->sc_rconvbuffer_end;
 1332 #ifdef DIAGNOSTIC
 1333                                 if (cc % params->factor != 0)
 1334                                         printf("audio_read: cc is not aligned"
 1335                                                ": cc=%d factor=%d\n", cc,
 1336                                                params->factor);
 1337 #endif
 1338                                 cc = cc * params->factor_denom / params->factor;
 1339 #ifdef DIAGNOSTIC
 1340                                 if (cc == 0)
 1341                                         printf("audio_read: cc=0 "
 1342                                                "factor=%d/%d\n",
 1343                                                params->factor,
 1344                                                params->factor_denom);
 1345 #endif
 1346                                 params->sw_code(sc->hw_hdl, sc->sc_rconvbuffer,
 1347                                                 cc);
 1348                                 sc->sc_rconvbuffer_end = cc;
 1349                         }
 1350                         sc->sc_rconvbuffer_begin = 0;
 1351                         /*
 1352                          * The format of data in sc_rconvbuffer is
 1353                          * [sample_rate, encoding, precision, channels]
 1354                          */
 1355                 }
 1356 
 1357                 cc = sc->sc_rconvbuffer_end - sc->sc_rconvbuffer_begin;
 1358                 if (uio->uio_resid < cc)
 1359                         cc = uio->uio_resid; /* and no more than we want */
 1360 
 1361                 DPRINTFN(0,("audio_read: buffer=%p[%d] (~ %d), cc=%d\n",
 1362                             sc->sc_rconvbuffer, sc->sc_rconvbuffer_begin,
 1363                             sc->sc_rconvbuffer_end, cc));
 1364                 n = uio->uio_resid;
 1365                 error = uiomove(sc->sc_rconvbuffer + sc->sc_rconvbuffer_begin,
 1366                                 cc, uio);
 1367                 cc = n - uio->uio_resid; /* number of bytes actually moved */
 1368                 sc->sc_rconvbuffer_begin += cc;
 1369 
 1370         }
 1371         return (error);
 1372 }
 1373 
 1374 void
 1375 audio_clear(struct audio_softc *sc)
 1376 {
 1377         int s = splaudio();
 1378 
 1379         if (sc->sc_rbus) {
 1380                 audio_wakeup(&sc->sc_rchan);
 1381                 sc->hw_if->halt_input(sc->hw_hdl);
 1382                 sc->sc_rbus = 0;
 1383         }
 1384         if (sc->sc_pbus) {
 1385                 audio_wakeup(&sc->sc_wchan);
 1386                 sc->hw_if->halt_output(sc->hw_hdl);
 1387                 sc->sc_pbus = 0;
 1388         }
 1389         splx(s);
 1390 }
 1391 
 1392 void
 1393 audio_calc_blksize(struct audio_softc *sc, int mode)
 1394 {
 1395         struct audio_params *parm;
 1396         struct audio_ringbuffer *rb;
 1397 
 1398         if (sc->sc_blkset)
 1399                 return;
 1400 
 1401         if (mode == AUMODE_PLAY) {
 1402                 parm = &sc->sc_pparams;
 1403                 rb = &sc->sc_pr;
 1404         } else {
 1405                 parm = &sc->sc_rparams;
 1406                 rb = &sc->sc_rr;
 1407         }
 1408 
 1409         rb->blksize = parm->hw_sample_rate * audio_blk_ms / 1000 *
 1410              parm->hw_channels * parm->precision / NBBY *
 1411              parm->factor;
 1412 
 1413         DPRINTF(("audio_calc_blksize: %s blksize=%d\n",
 1414                  mode == AUMODE_PLAY ? "play" : "record", rb->blksize));
 1415 }
 1416 
 1417 void
 1418 audio_fill_silence(struct audio_params *params, u_char *p, int n)
 1419 {
 1420         u_char auzero0, auzero1 = 0; /* initialize to please gcc */
 1421         int nfill = 1;
 1422 
 1423         switch (params->hw_encoding) {
 1424         case AUDIO_ENCODING_ULAW:
 1425                 auzero0 = 0x7f;
 1426                 break;
 1427         case AUDIO_ENCODING_ALAW:
 1428                 auzero0 = 0x55;
 1429                 break;
 1430         case AUDIO_ENCODING_MPEG_L1_STREAM:
 1431         case AUDIO_ENCODING_MPEG_L1_PACKETS:
 1432         case AUDIO_ENCODING_MPEG_L1_SYSTEM:
 1433         case AUDIO_ENCODING_MPEG_L2_STREAM:
 1434         case AUDIO_ENCODING_MPEG_L2_PACKETS:
 1435         case AUDIO_ENCODING_MPEG_L2_SYSTEM:
 1436         case AUDIO_ENCODING_ADPCM: /* is this right XXX */
 1437         case AUDIO_ENCODING_SLINEAR_LE:
 1438         case AUDIO_ENCODING_SLINEAR_BE:
 1439                 auzero0 = 0;/* fortunately this works for any number of bits */
 1440                 break;
 1441         case AUDIO_ENCODING_ULINEAR_LE:
 1442         case AUDIO_ENCODING_ULINEAR_BE:
 1443                 if (params->hw_precision > 8) {
 1444                         nfill = (params->hw_precision + NBBY - 1)/ NBBY;
 1445                         auzero0 = 0x80;
 1446                         auzero1 = 0;
 1447                 } else
 1448                         auzero0 = 0x80;
 1449                 break;
 1450         default:
 1451                 DPRINTF(("audio: bad encoding %d\n", params->encoding));
 1452                 auzero0 = 0;
 1453                 break;
 1454         }
 1455         if (nfill == 1) {
 1456                 while (--n >= 0)
 1457                         *p++ = auzero0; /* XXX memset */
 1458         } else /* nfill must no longer be 2 */ {
 1459                 if (params->hw_encoding == AUDIO_ENCODING_ULINEAR_LE) {
 1460                         int k = nfill;
 1461                         while (--k > 0)
 1462                                 *p++ = auzero1;
 1463                         n -= nfill - 1;
 1464                 }
 1465                 while (n >= nfill) {
 1466                         int k = nfill;
 1467                         *p++ = auzero0;
 1468                         while (--k > 0)
 1469                                 *p++ = auzero1;
 1470 
 1471                         n -= nfill;
 1472                 }
 1473                 if (n-- > 0)    /* XXX must be 1 - DIAGNOSTIC check? */
 1474                         *p++ = auzero0;
 1475         }
 1476 }
 1477 
 1478 int
 1479 audio_silence_copyout(struct audio_softc *sc, int n, struct uio *uio)
 1480 {
 1481         int error;
 1482         int k;
 1483         u_char zerobuf[128];
 1484 
 1485         audio_fill_silence(&sc->sc_rparams, zerobuf, sizeof zerobuf);
 1486 
 1487         error = 0;
 1488         while (n > 0 && uio->uio_resid > 0 && !error) {
 1489                 k = min(n, min(uio->uio_resid, sizeof zerobuf));
 1490                 error = uiomove(zerobuf, k, uio);
 1491                 n -= k;
 1492         }
 1493         return (error);
 1494 }
 1495 
 1496 int
 1497 audio_write(struct audio_softc *sc, struct uio *uio, int ioflag)
 1498 {
 1499         struct audio_ringbuffer *cb = &sc->sc_pr;
 1500         u_char *inp, *einp;
 1501         int saveerror, error, s, n, cc, used;
 1502         struct audio_params *params;
 1503         int samples, hw_bits_per_sample, user_bits_per_sample;
 1504         int input_remain, space;
 1505 
 1506         DPRINTFN(2,("audio_write: sc=%p count=%lu used=%d(hi=%d)\n",
 1507                     sc, (unsigned long)uio->uio_resid, sc->sc_pr.used,
 1508                     sc->sc_pr.usedhigh));
 1509 
 1510         if (cb->mmapped)
 1511                 return EINVAL;
 1512 
 1513         if (uio->uio_resid == 0) {
 1514                 sc->sc_eof++;
 1515                 return 0;
 1516         }
 1517 
 1518         /*
 1519          * If half-duplex and currently recording, throw away data.
 1520          */
 1521         if (!sc->sc_full_duplex &&
 1522             (sc->sc_mode & AUMODE_RECORD)) {
 1523                 uio->uio_offset += uio->uio_resid;
 1524                 uio->uio_resid = 0;
 1525                 DPRINTF(("audio_write: half-dpx read busy\n"));
 1526                 return (0);
 1527         }
 1528 
 1529         if (!(sc->sc_mode & AUMODE_PLAY_ALL) && sc->sc_playdrop > 0) {
 1530                 n = min(sc->sc_playdrop, uio->uio_resid);
 1531                 DPRINTF(("audio_write: playdrop %d\n", n));
 1532                 uio->uio_offset += n;
 1533                 uio->uio_resid -= n;
 1534                 sc->sc_playdrop -= n;
 1535                 if (uio->uio_resid == 0)
 1536                         return 0;
 1537         }
 1538 
 1539         params = &sc->sc_pparams;
 1540         DPRINTFN(1, ("audio_write: sr=%ld, enc=%d, prec=%d, chan=%d, sw=%p, "
 1541                      "fact=%d\n",
 1542                      sc->sc_pparams.sample_rate, sc->sc_pparams.encoding,
 1543                      sc->sc_pparams.precision, sc->sc_pparams.channels,
 1544                      sc->sc_pparams.sw_code, sc->sc_pparams.factor));
 1545 
 1546         /*
 1547          * For some encodings, handle data in sample unit.
 1548          */
 1549         switch (params->hw_encoding) {
 1550         case AUDIO_ENCODING_SLINEAR_LE:
 1551         case AUDIO_ENCODING_SLINEAR_BE:
 1552         case AUDIO_ENCODING_ULINEAR_LE:
 1553         case AUDIO_ENCODING_ULINEAR_BE:
 1554                 hw_bits_per_sample = params->hw_channels * params->precision
 1555                         * params->factor;
 1556                 user_bits_per_sample = params->channels * params->precision;
 1557                 break;
 1558         default:
 1559                 hw_bits_per_sample = 8 * params->factor / params->factor_denom;
 1560                 user_bits_per_sample = 8;
 1561         }
 1562 #ifdef DIAGNOSTIC
 1563         if (hw_bits_per_sample > MAX_SAMPLE_SIZE * 8) {
 1564                 printf("audio_write(): Invalid sample size: cur=%d max=%d\n",
 1565                        hw_bits_per_sample / 8, MAX_SAMPLE_SIZE);
 1566         }
 1567 #endif
 1568         space = ((params->hw_sample_rate / params->sample_rate) + 1)
 1569                 * hw_bits_per_sample / 8;
 1570         error = 0;
 1571         while ((input_remain = uio->uio_resid + sc->sc_input_fragment_length) > 0
 1572                && !error) {
 1573                 s = splaudio();
 1574                 if (input_remain < user_bits_per_sample / 8) {
 1575                         n = uio->uio_resid;
 1576                         DPRINTF(("audio_write: fragment uiomove length=%d\n", n));
 1577                         error = uiomove(sc->sc_input_fragment
 1578                                         + sc->sc_input_fragment_length,
 1579                                         n, uio);
 1580                         if (!error)
 1581                                 sc->sc_input_fragment_length += n;
 1582                         splx(s);
 1583                         return (error);
 1584                 }
 1585                 while (cb->used + space >= cb->usedhigh) {
 1586                         DPRINTFN(2, ("audio_write: sleep used=%d lowat=%d "
 1587                                      "hiwat=%d\n",
 1588                                      cb->used, cb->usedlow, cb->usedhigh));
 1589                         if (ioflag & IO_NDELAY) {
 1590                                 splx(s);
 1591                                 return (EWOULDBLOCK);
 1592                         }
 1593                         error = audio_sleep(&sc->sc_wchan, "aud_wr");
 1594                         if (sc->sc_dying)
 1595                                 error = EIO;
 1596                         if (error) {
 1597                                 splx(s);
 1598                                 return (error);
 1599                         }
 1600                 }
 1601                 used = cb->used;
 1602                 inp = cb->inp;
 1603                 cb->copying = 1;
 1604                 splx(s);
 1605                 cc = cb->usedhigh - used;       /* maximum to write */
 1606                 /* cc may be greater than the size of the ring buffer */
 1607                 if (cc > cb->end - cb->start)
 1608                         cc = cb->end - cb->start;
 1609 
 1610                 /* cc: # of bytes we can write to the ring buffer */
 1611                 samples = cc * 8 / hw_bits_per_sample;
 1612 #ifdef DIAGNOSTIC
 1613                 if (samples == 0)
 1614                         printf("audio_write: samples (cc/hw_bps) == 0\n");
 1615 #endif
 1616                 /* samples: # of samples we can write to the ring buffer */
 1617                 samples = samples * params->sample_rate / params->hw_sample_rate;
 1618 #ifdef DIAGNOSTIC
 1619                 if (samples == 0)
 1620                         printf("audio_write: samples (rate/hw_rate) == 0 "
 1621                                "usedhigh-used=%d cc/hw_bps=%d/%d "
 1622                                "rate/hw_rate=%ld/%ld space=%d\n",
 1623                                cb->usedhigh - cb->used, cc,
 1624                                hw_bits_per_sample / 8, params->sample_rate,
 1625                                params->hw_sample_rate, space);
 1626 #endif
 1627                 /* samples: # of samples in source data */
 1628                 cc = samples * user_bits_per_sample / 8;
 1629                 /* cc: # of bytes in source data */
 1630                 if (input_remain < cc)  /* and no more than we have */
 1631                         cc = (input_remain * 8 / user_bits_per_sample)
 1632                                 * user_bits_per_sample / 8;
 1633 #ifdef DIAGNOSTIC
 1634                 if (cc == 0)
 1635                         printf("audio_write: cc == 0\n");
 1636 #endif
 1637                 if (cc * params->factor / params->factor_denom
 1638                     > sc->sc_pconvbuffer_size) {
 1639                         /*
 1640                          * cc = (pconv / factor / user_bps ) * user_bps
 1641                          */
 1642                         cc = (sc->sc_pconvbuffer_size * params->factor_denom
 1643                               * 8 / params->factor / user_bits_per_sample)
 1644                              * user_bits_per_sample / 8;
 1645                 }
 1646 
 1647 #ifdef DIAGNOSTIC
 1648                 /*
 1649                  * This should never happen since the block size and and
 1650                  * block pointers are always nicely aligned.
 1651                  */
 1652                 if (cc == 0) {
 1653                         printf("audio_write: cc == 0, swcode=%p, factor=%d "
 1654                                "remain=%d u_bps=%d hw_bps=%d\n",
 1655                                sc->sc_pparams.sw_code, sc->sc_pparams.factor,
 1656                                input_remain, user_bits_per_sample,
 1657                                hw_bits_per_sample);
 1658                         cb->copying = 0;
 1659                         return EINVAL;
 1660                 }
 1661 #endif
 1662                 DPRINTFN(1, ("audio_write: uiomove cc=%d inp=%p, left=%lu\n",
 1663                              cc, inp, (unsigned long)uio->uio_resid));
 1664                 memcpy(sc->sc_pconvbuffer, sc->sc_input_fragment,
 1665                        sc->sc_input_fragment_length);
 1666                 cc -= sc->sc_input_fragment_length;
 1667                 n = uio->uio_resid;
 1668                 error = uiomove(sc->sc_pconvbuffer + sc->sc_input_fragment_length,
 1669                                 cc, uio);
 1670                 if (cc != n - uio->uio_resid) {
 1671                         printf("audio_write: uiomove didn't move requested "
 1672                                "amount: requested=%d, actual=%ld\n",
 1673                                cc, (long)n - uio->uio_resid);
 1674                 }
 1675                             /* number of bytes actually moved */
 1676                 cc = sc->sc_input_fragment_length + n - uio->uio_resid;
 1677                 sc->sc_input_fragment_length = 0;
 1678 #ifdef AUDIO_DEBUG
 1679                 if (error)
 1680                         printf("audio_write:(1) uiomove failed %d; cc=%d "
 1681                                "inp=%p\n", error, cc, inp);
 1682 #endif
 1683                 /*
 1684                  * Continue even if uiomove() failed because we may have
 1685                  * gotten a partial block.
 1686                  */
 1687 
 1688                 /*
 1689                  * The format of data in sc_pconvbuffer is:
 1690                  * [sample_rate, encoding, precision, channels]
 1691                  */
 1692                 if (sc->sc_pparams.sw_code) {
 1693                         sc->sc_pparams.sw_code(sc->hw_hdl,
 1694                                                sc->sc_pconvbuffer, cc);
 1695                         /* Adjust count after the expansion. */
 1696                         cc = cc * sc->sc_pparams.factor
 1697                                 / sc->sc_pparams.factor_denom;
 1698                         DPRINTFN(1, ("audio_write: expanded cc=%d\n", cc));
 1699                 }
 1700                 /*
 1701                  * The format of data in sc_pconvbuffer is:
 1702                  * [sample_rate, hw_encoding, hw_precision, channels]
 1703                  */
 1704 #if NAURATECONV > 0
 1705                 cc = auconv_play(&sc->sc_pconv, params, inp,
 1706                                  sc->sc_pconvbuffer, cc);
 1707 #else
 1708                 n = cb->end - inp;
 1709                 if (cc <= n) {
 1710                         memcpy(inp, sc->sc_pconvbuffer, cc);
 1711                 } else {
 1712                         memcpy(inp, sc->sc_pconvbuffer, n);
 1713                         memcpy(cb->start, sc->sc_pconvbuffer + n, cc - n);
 1714                 }
 1715 #endif /* !NAURATECONV */
 1716                 /*
 1717                  * The format of data in inp is:
 1718                  * [hw_sample_rate, hw_encoding, hw_precision, hw_channels]
 1719                  * cc is the size of data actually written to inp.
 1720                  */
 1721 
 1722                 einp = cb->inp + cc;
 1723                 if (einp >= cb->end)
 1724                         einp -= cb->end - cb->start; /* not cb->bufsize */
 1725 
 1726                 s = splaudio();
 1727                 /*
 1728                  * This is a very suboptimal way of keeping track of
 1729                  * silence in the buffer, but it is simple.
 1730                  */
 1731                 sc->sc_sil_count = 0;
 1732 
 1733                 cb->inp = einp;
 1734                 cb->used += cc;
 1735                 /*
 1736                  * If the interrupt routine wants the last block filled AND
 1737                  * the copy did not fill the last block completely it needs to
 1738                  * be padded.
 1739                  */
 1740                 if (cb->needfill &&
 1741                     (inp  - cb->start) / cb->blksize ==
 1742                     (einp - cb->start) / cb->blksize) {
 1743                         /* Figure out how many bytes to a block boundary. */
 1744                         cc = cb->blksize - (einp - cb->start) % cb->blksize;
 1745                         DPRINTF(("audio_write: partial fill %d\n", cc));
 1746                 } else
 1747                         cc = 0;
 1748                 cb->needfill = 0;
 1749                 cb->copying = 0;
 1750                 if (!sc->sc_pbus && !cb->pause) {
 1751                         saveerror = error;
 1752                         error = audiostartp(sc);
 1753                         if (saveerror != 0) {
 1754                                 /* Report the first error that occurred. */
 1755                                 error = saveerror;
 1756                         }
 1757                 }
 1758                 splx(s);
 1759                 if (cc != 0) {
 1760                         DPRINTFN(1, ("audio_write: fill %d\n", cc));
 1761                         audio_fill_silence(&sc->sc_pparams, einp, cc);
 1762                 }
 1763         }
 1764         return (error);
 1765 }
 1766 
 1767 int
 1768 audio_ioctl(struct audio_softc *sc, u_long cmd, caddr_t addr, int flag,
 1769             struct proc *p)
 1770 {
 1771         struct audio_hw_if *hw = sc->hw_if;
 1772         struct audio_offset *ao;
 1773         int error = 0, s, offs, fd;
 1774         u_long stamp;
 1775         int rbus, pbus;
 1776 
 1777         DPRINTF(("audio_ioctl(%lu,'%c',%lu)\n",
 1778                  IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff));
 1779         switch (cmd) {
 1780         case FIONBIO:
 1781                 /* All handled in the upper FS layer. */
 1782                 break;
 1783 
 1784         case FIONREAD:
 1785                 *(int *)addr = sc->sc_rr.used;
 1786                 break;
 1787 
 1788         case FIOASYNC:
 1789                 if (*(int *)addr) {
 1790                         if (sc->sc_async_audio)
 1791                                 return (EBUSY);
 1792                         sc->sc_async_audio = p;
 1793                         DPRINTF(("audio_ioctl: FIOASYNC %p\n", p));
 1794                 } else
 1795                         sc->sc_async_audio = 0;
 1796                 break;
 1797 
 1798         case AUDIO_FLUSH:
 1799                 DPRINTF(("AUDIO_FLUSH\n"));
 1800                 rbus = sc->sc_rbus;
 1801                 pbus = sc->sc_pbus;
 1802                 audio_clear(sc);
 1803                 s = splaudio();
 1804                 error = audio_initbufs(sc);
 1805                 if (error) {
 1806                         splx(s);
 1807                         return error;
 1808                 }
 1809                 if ((sc->sc_mode & AUMODE_PLAY) && !sc->sc_pbus && pbus)
 1810                         error = audiostartp(sc);
 1811                 if (!error &&
 1812                     (sc->sc_mode & AUMODE_RECORD) && !sc->sc_rbus && rbus)
 1813                         error = audiostartr(sc);
 1814                 splx(s);
 1815                 break;
 1816 
 1817         /*
 1818          * Number of read (write) samples dropped.  We don't know where or
 1819          * when they were dropped.
 1820          */
 1821         case AUDIO_RERROR:
 1822                 *(int *)addr = sc->sc_rr.drops;
 1823                 break;
 1824 
 1825         case AUDIO_PERROR:
 1826                 *(int *)addr = sc->sc_pr.drops;
 1827                 break;
 1828 
 1829         /*
 1830          * Offsets into buffer.
 1831          */
 1832         case AUDIO_GETIOFFS:
 1833                 ao = (struct audio_offset *)addr;
 1834                 s = splaudio();
 1835                 /* figure out where next DMA will start */
 1836                 stamp = sc->sc_rr.stamp;
 1837                 offs = sc->sc_rr.inp - sc->sc_rr.start;
 1838                 splx(s);
 1839                 ao->samples = stamp;
 1840                 ao->deltablks =
 1841                   (stamp / sc->sc_rr.blksize) -
 1842                   (sc->sc_rr.stamp_last / sc->sc_rr.blksize);
 1843                 sc->sc_rr.stamp_last = stamp;
 1844                 ao->offset = offs;
 1845                 break;
 1846 
 1847         case AUDIO_GETOOFFS:
 1848                 ao = (struct audio_offset *)addr;
 1849                 s = splaudio();
 1850                 /* figure out where next DMA will start */
 1851                 stamp = sc->sc_pr.stamp;
 1852                 offs = sc->sc_pr.outp - sc->sc_pr.start + sc->sc_pr.blksize;
 1853                 splx(s);
 1854                 ao->samples = stamp;
 1855                 ao->deltablks =
 1856                   (stamp / sc->sc_pr.blksize) -
 1857                   (sc->sc_pr.stamp_last / sc->sc_pr.blksize);
 1858                 sc->sc_pr.stamp_last = stamp;
 1859                 if (sc->sc_pr.start + offs >= sc->sc_pr.end)
 1860                         offs = 0;
 1861                 ao->offset = offs;
 1862                 break;
 1863 
 1864         /*
 1865          * How many bytes will elapse until mike hears the first
 1866          * sample of what we write next?
 1867          */
 1868         case AUDIO_WSEEK:
 1869                 *(u_long *)addr = sc->sc_rr.used;
 1870                 break;
 1871 
 1872         case AUDIO_SETINFO:
 1873                 DPRINTF(("AUDIO_SETINFO mode=0x%x\n", sc->sc_mode));
 1874                 error = audiosetinfo(sc, (struct audio_info *)addr);
 1875                 break;
 1876 
 1877         case AUDIO_GETINFO:
 1878                 DPRINTF(("AUDIO_GETINFO\n"));
 1879                 error = audiogetinfo(sc, (struct audio_info *)addr);
 1880                 break;
 1881 
 1882         case AUDIO_DRAIN:
 1883                 DPRINTF(("AUDIO_DRAIN\n"));
 1884                 error = audio_drain(sc);
 1885                 if (!error && hw->drain)
 1886                     error = hw->drain(sc->hw_hdl);
 1887                 break;
 1888 
 1889         case AUDIO_GETDEV:
 1890                 DPRINTF(("AUDIO_GETDEV\n"));
 1891                 error = hw->getdev(sc->hw_hdl, (audio_device_t *)addr);
 1892                 break;
 1893 
 1894         case AUDIO_GETENC:
 1895                 DPRINTF(("AUDIO_GETENC\n"));
 1896                 error =
 1897                  hw->query_encoding(sc->hw_hdl, (struct audio_encoding *)addr);
 1898                 break;
 1899 
 1900         case AUDIO_GETFD:
 1901                 DPRINTF(("AUDIO_GETFD\n"));
 1902                 *(int *)addr = sc->sc_full_duplex;
 1903                 break;
 1904 
 1905         case AUDIO_SETFD:
 1906                 DPRINTF(("AUDIO_SETFD\n"));
 1907                 fd = *(int *)addr;
 1908                 if (hw->get_props(sc->hw_hdl) & AUDIO_PROP_FULLDUPLEX) {
 1909                         if (hw->setfd)
 1910                                 error = hw->setfd(sc->hw_hdl, fd);
 1911                         else
 1912                                 error = 0;
 1913                         if (!error)
 1914                                 sc->sc_full_duplex = fd;
 1915                 } else {
 1916                         if (fd)
 1917                                 error = ENOTTY;
 1918                         else
 1919                                 error = 0;
 1920                 }
 1921                 break;
 1922 
 1923         case AUDIO_GETPROPS:
 1924                 DPRINTF(("AUDIO_GETPROPS\n"));
 1925                 *(int *)addr = hw->get_props(sc->hw_hdl);
 1926                 break;
 1927 
 1928         default:
 1929                 if (hw->dev_ioctl) {
 1930                         error = hw->dev_ioctl(sc->hw_hdl, cmd, addr, flag, p);
 1931                 } else {
 1932                         DPRINTF(("audio_ioctl: unknown ioctl\n"));
 1933                         error = EINVAL;
 1934                 }
 1935                 break;
 1936         }
 1937         DPRINTF(("audio_ioctl(%lu,'%c',%lu) result %d\n",
 1938                  IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff, error));
 1939         return (error);
 1940 }
 1941 
 1942 int
 1943 audio_poll(struct audio_softc *sc, int events, struct proc *p)
 1944 {
 1945         int revents = 0;
 1946         int s = splaudio();
 1947 
 1948         DPRINTF(("audio_poll: events=0x%x mode=%d\n", events, sc->sc_mode));
 1949 
 1950         if (events & (POLLIN | POLLRDNORM)) {
 1951                 /*
 1952                  * If half duplex and playing, audio_read() will generate
 1953                  * silence at the play rate; poll for silence being
 1954                  * available.  Otherwise, poll for recorded sound.
 1955                  */
 1956                 if ((!sc->sc_full_duplex && (sc->sc_mode & AUMODE_PLAY)) ?
 1957                     sc->sc_pr.stamp > sc->sc_wstamp :
 1958                     sc->sc_rr.used > sc->sc_rr.usedlow)
 1959                         revents |= events & (POLLIN | POLLRDNORM);
 1960         }
 1961 
 1962         if (events & (POLLOUT | POLLWRNORM)) {
 1963                 /*
 1964                  * If half duplex and recording, audio_write() will throw
 1965                  * away play data, which means we are always ready to write.
 1966                  * Otherwise, poll for play buffer being below its low water
 1967                  * mark.
 1968                  */
 1969                 if ((!sc->sc_full_duplex && (sc->sc_mode & AUMODE_RECORD)) ||
 1970                     sc->sc_pr.used <= sc->sc_pr.usedlow)
 1971                         revents |= events & (POLLOUT | POLLWRNORM);
 1972         }
 1973 
 1974         if (revents == 0) {
 1975                 if (events & (POLLIN | POLLRDNORM))
 1976                         selrecord(p, &sc->sc_rsel);
 1977 
 1978                 if (events & (POLLOUT | POLLWRNORM))
 1979                         selrecord(p, &sc->sc_wsel);
 1980         }
 1981 
 1982         splx(s);
 1983         return (revents);
 1984 }
 1985 
 1986 static void
 1987 filt_audiordetach(struct knote *kn)
 1988 {
 1989         struct audio_softc *sc = kn->kn_hook;
 1990         int s;
 1991 
 1992         s = splaudio();
 1993         SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
 1994         splx(s);
 1995 }
 1996 
 1997 static int
 1998 filt_audioread(struct knote *kn, long hint)
 1999 {
 2000         struct audio_softc *sc = kn->kn_hook;
 2001         int s;
 2002 
 2003         /* XXXLUKEM (thorpej): please make sure this is right */
 2004 
 2005         s = splaudio();
 2006         if (sc->sc_mode & AUMODE_PLAY)
 2007                 kn->kn_data = sc->sc_pr.stamp - sc->sc_wstamp;
 2008         else
 2009                 kn->kn_data = sc->sc_rr.used - sc->sc_rr.usedlow;
 2010         splx(s);
 2011 
 2012         return (kn->kn_data > 0);
 2013 }
 2014 
 2015 static const struct filterops audioread_filtops =
 2016         { 1, NULL, filt_audiordetach, filt_audioread };
 2017 
 2018 static void
 2019 filt_audiowdetach(struct knote *kn)
 2020 {
 2021         struct audio_softc *sc = kn->kn_hook;
 2022         int s;
 2023 
 2024         s = splaudio();
 2025         SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
 2026         splx(s);
 2027 }
 2028 
 2029 static int
 2030 filt_audiowrite(struct knote *kn, long hint)
 2031 {
 2032         struct audio_softc *sc = kn->kn_hook;
 2033         int s;
 2034 
 2035         /* XXXLUKEM (thorpej): please make sure this is right */
 2036 
 2037         s = splaudio();
 2038         kn->kn_data = sc->sc_pr.usedlow - sc->sc_pr.used;
 2039         splx(s);
 2040 
 2041         return (kn->kn_data > 0);
 2042 }
 2043 
 2044 static const struct filterops audiowrite_filtops =
 2045         { 1, NULL, filt_audiowdetach, filt_audiowrite };
 2046 
 2047 int
 2048 audio_kqfilter(struct audio_softc *sc, struct knote *kn)
 2049 {
 2050         struct klist *klist;
 2051         int s;
 2052 
 2053         switch (kn->kn_filter) {
 2054         case EVFILT_READ:
 2055                 klist = &sc->sc_rsel.sel_klist;
 2056                 kn->kn_fop = &audioread_filtops;
 2057                 break;
 2058 
 2059         case EVFILT_WRITE:
 2060                 klist = &sc->sc_wsel.sel_klist;
 2061                 kn->kn_fop = &audiowrite_filtops;
 2062                 break;
 2063 
 2064         default:
 2065                 return (1);
 2066         }
 2067 
 2068         kn->kn_hook = sc;
 2069 
 2070         s = splaudio();
 2071         SLIST_INSERT_HEAD(klist, kn, kn_selnext);
 2072         splx(s);
 2073 
 2074         return (0);
 2075 }
 2076 
 2077 paddr_t
 2078 audio_mmap(struct audio_softc *sc, off_t off, int prot)
 2079 {
 2080         struct audio_hw_if *hw = sc->hw_if;
 2081         struct audio_ringbuffer *cb;
 2082         int s;
 2083 
 2084         DPRINTF(("audio_mmap: off=%lld, prot=%d\n", (long long)off, prot));
 2085 
 2086         if (!(hw->get_props(sc->hw_hdl) & AUDIO_PROP_MMAP) || !hw->mappage)
 2087                 return -1;
 2088 #if 0
 2089 /* XXX
 2090  * The idea here was to use the protection to determine if
 2091  * we are mapping the read or write buffer, but it fails.
 2092  * The VM system is broken in (at least) two ways.
 2093  * 1) If you map memory VM_PROT_WRITE you SIGSEGV
 2094  *    when writing to it, so VM_PROT_READ|VM_PROT_WRITE
 2095  *    has to be used for mmapping the play buffer.
 2096  * 2) Even if calling mmap() with VM_PROT_READ|VM_PROT_WRITE
 2097  *    audio_mmap will get called at some point with VM_PROT_READ
 2098  *    only.
 2099  * So, alas, we always map the play buffer for now.
 2100  */
 2101         if (prot == (VM_PROT_READ|VM_PROT_WRITE) ||
 2102             prot == VM_PROT_WRITE)
 2103                 cb = &sc->sc_pr;
 2104         else if (prot == VM_PROT_READ)
 2105                 cb = &sc->sc_rr;
 2106         else
 2107                 return -1;
 2108 #else
 2109         cb = &sc->sc_pr;
 2110 #endif
 2111 
 2112         if ((u_int)off >= cb->bufsize)
 2113                 return -1;
 2114         if (!cb->mmapped) {
 2115                 cb->mmapped = 1;
 2116                 if (cb == &sc->sc_pr) {
 2117                         audio_fill_silence(&sc->sc_pparams, cb->start,
 2118                                            cb->bufsize);
 2119                         s = splaudio();
 2120                         if (!sc->sc_pbus)
 2121                                 (void)audiostartp(sc);
 2122                         splx(s);
 2123                 } else {
 2124                         s = splaudio();
 2125                         if (!sc->sc_rbus)
 2126                                 (void)audiostartr(sc);
 2127                         splx(s);
 2128                 }
 2129         }
 2130 
 2131         return hw->mappage(sc->hw_hdl, cb->start, off, prot);
 2132 }
 2133 
 2134 int
 2135 audiostartr(struct audio_softc *sc)
 2136 {
 2137         int error;
 2138 
 2139         DPRINTF(("audiostartr: start=%p used=%d(hi=%d) mmapped=%d\n",
 2140                  sc->sc_rr.start, sc->sc_rr.used, sc->sc_rr.usedhigh,
 2141                  sc->sc_rr.mmapped));
 2142 
 2143         if (sc->hw_if->trigger_input)
 2144                 error = sc->hw_if->trigger_input(sc->hw_hdl, sc->sc_rr.start,
 2145                     sc->sc_rr.end, sc->sc_rr.blksize,
 2146                     audio_rint, (void *)sc, &sc->sc_rparams);
 2147         else
 2148                 error = sc->hw_if->start_input(sc->hw_hdl, sc->sc_rr.start,
 2149                     sc->sc_rr.blksize, audio_rint, (void *)sc);
 2150         if (error) {
 2151                 DPRINTF(("audiostartr failed: %d\n", error));
 2152                 return error;
 2153         }
 2154         sc->sc_rbus = 1;
 2155         return 0;
 2156 }
 2157 
 2158 int
 2159 audiostartp(struct audio_softc *sc)
 2160 {
 2161         int error;
 2162 
 2163         DPRINTF(("audiostartp: start=%p used=%d(hi=%d) mmapped=%d\n",
 2164                  sc->sc_pr.start, sc->sc_pr.used, sc->sc_pr.usedhigh,
 2165                  sc->sc_pr.mmapped));
 2166 
 2167         if (!sc->sc_pr.mmapped && sc->sc_pr.used < sc->sc_pr.blksize) {
 2168                 wakeup(&sc->sc_wchan);
 2169                 return 0;
 2170         }
 2171 
 2172         if (sc->hw_if->trigger_output)
 2173                 error = sc->hw_if->trigger_output(sc->hw_hdl, sc->sc_pr.start,
 2174                     sc->sc_pr.end, sc->sc_pr.blksize,
 2175                     audio_pint, (void *)sc, &sc->sc_pparams);
 2176         else
 2177                 error = sc->hw_if->start_output(sc->hw_hdl, sc->sc_pr.outp,
 2178                     sc->sc_pr.blksize, audio_pint, (void *)sc);
 2179         if (error) {
 2180                 DPRINTF(("audiostartp failed: %d\n", error));
 2181                 return error;
 2182         }
 2183         sc->sc_pbus = 1;
 2184         return 0;
 2185 }
 2186 
 2187 /*
 2188  * When the play interrupt routine finds that the write isn't keeping
 2189  * the buffer filled it will insert silence in the buffer to make up
 2190  * for this.  The part of the buffer that is filled with silence
 2191  * is kept track of in a very approximate way: it starts at sc_sil_start
 2192  * and extends sc_sil_count bytes.  If there is already silence in
 2193  * the requested area nothing is done; so when the whole buffer is
 2194  * silent nothing happens.  When the writer starts again sc_sil_count
 2195  * is set to 0.
 2196  */
 2197 /* XXX
 2198  * Putting silence into the output buffer should not really be done
 2199  * at splaudio, but there is no softaudio level to do it at yet.
 2200  */
 2201 static __inline void
 2202 audio_pint_silence(struct audio_softc *sc, struct audio_ringbuffer *cb,
 2203                    u_char *inp, int cc)
 2204 {
 2205         u_char *s, *e, *p, *q;
 2206 
 2207         if (sc->sc_sil_count > 0) {
 2208                 s = sc->sc_sil_start; /* start of silence */
 2209                 e = s + sc->sc_sil_count; /* end of sil., may be beyond end */
 2210                 p = inp;        /* adjusted pointer to area to fill */
 2211                 if (p < s)
 2212                         p += cb->end - cb->start;
 2213                 q = p+cc;
 2214                 /* Check if there is already silence. */
 2215                 if (!(s <= p && p <  e &&
 2216                       s <= q && q <= e)) {
 2217                         if (s <= p)
 2218                                 sc->sc_sil_count = max(sc->sc_sil_count, q-s);
 2219                         DPRINTFN(5,("audio_pint_silence: fill cc=%d inp=%p, "
 2220                                     "count=%d size=%d\n",
 2221                                     cc, inp, sc->sc_sil_count,
 2222                                     (int)(cb->end - cb->start)));
 2223                         audio_fill_silence(&sc->sc_pparams, inp, cc);
 2224                 } else {
 2225                         DPRINTFN(5,("audio_pint_silence: already silent "
 2226                                     "cc=%d inp=%p\n", cc, inp));
 2227 
 2228                 }
 2229         } else {
 2230                 sc->sc_sil_start = inp;
 2231                 sc->sc_sil_count = cc;
 2232                 DPRINTFN(5, ("audio_pint_silence: start fill %p %d\n",
 2233                              inp, cc));
 2234                 audio_fill_silence(&sc->sc_pparams, inp, cc);
 2235         }
 2236 }
 2237 
 2238 /*
 2239  * Called from HW driver module on completion of DMA output.
 2240  * Start output of new block, wrap in ring buffer if needed.
 2241  * If no more buffers to play, output zero instead.
 2242  * Do a wakeup if necessary.
 2243  */
 2244 void
 2245 audio_pint(void *v)
 2246 {
 2247         struct audio_softc *sc = v;
 2248         struct audio_hw_if *hw = sc->hw_if;
 2249         struct audio_ringbuffer *cb = &sc->sc_pr;
 2250         u_char *inp;
 2251         int cc, ccr;
 2252         int blksize;
 2253         int error;
 2254 
 2255         if (!sc->sc_open)
 2256                 return;         /* ignore interrupt if not open */
 2257 
 2258         blksize = cb->blksize;
 2259 
 2260         cb->outp += blksize;
 2261         if (cb->outp >= cb->end)
 2262                 cb->outp = cb->start;
 2263         cb->stamp += blksize / sc->sc_pparams.factor;
 2264         if (cb->mmapped) {
 2265                 DPRINTFN(5, ("audio_pint: mmapped outp=%p cc=%d inp=%p\n",
 2266                              cb->outp, blksize, cb->inp));
 2267                 if (!hw->trigger_output)
 2268                         (void)hw->start_output(sc->hw_hdl, cb->outp,
 2269                             blksize, audio_pint, (void *)sc);
 2270                 return;
 2271         }
 2272 
 2273 #ifdef AUDIO_INTR_TIME
 2274         {
 2275                 struct timeval tv;
 2276                 u_long t;
 2277                 microtime(&tv);
 2278                 t = tv.tv_usec + 1000000 * tv.tv_sec;
 2279                 if (sc->sc_pnintr) {
 2280                         long lastdelta, totdelta;
 2281                         lastdelta = t - sc->sc_plastintr - sc->sc_pblktime;
 2282                         if (lastdelta > sc->sc_pblktime / 3) {
 2283                                 printf("audio: play interrupt(%d) off "
 2284                                        "relative by %ld us (%lu)\n",
 2285                                        sc->sc_pnintr, lastdelta,
 2286                                        sc->sc_pblktime);
 2287                         }
 2288                         totdelta = t - sc->sc_pfirstintr -
 2289                                 sc->sc_pblktime * sc->sc_pnintr;
 2290                         if (totdelta > sc->sc_pblktime) {
 2291                                 printf("audio: play interrupt(%d) off "
 2292                                        "absolute by %ld us (%lu) (LOST)\n",
 2293                                        sc->sc_pnintr, totdelta,
 2294                                        sc->sc_pblktime);
 2295                                 sc->sc_pnintr++; /* avoid repeated messages */
 2296                         }
 2297                 } else
 2298                         sc->sc_pfirstintr = t;
 2299                 sc->sc_plastintr = t;
 2300                 sc->sc_pnintr++;
 2301         }
 2302 #endif
 2303 
 2304         cb->used -= blksize;
 2305         if (cb->used < blksize) {
 2306                 /* we don't have a full block to use */
 2307                 if (cb->copying) {
 2308                         /* writer is in progress, don't disturb */
 2309                         cb->needfill = 1;
 2310                         DPRINTFN(1, ("audio_pint: copying in progress\n"));
 2311                 } else {
 2312                         inp = cb->inp;
 2313                         cc = blksize - (inp - cb->start) % blksize;
 2314                         ccr = cc / sc->sc_pparams.factor;
 2315                         if (cb->pause)
 2316                                 cb->pdrops += ccr;
 2317                         else {
 2318                                 cb->drops += ccr;
 2319                                 sc->sc_playdrop += ccr;
 2320                         }
 2321                         audio_pint_silence(sc, cb, inp, cc);
 2322                         inp += cc;
 2323                         if (inp >= cb->end)
 2324                                 inp = cb->start;
 2325                         cb->inp = inp;
 2326                         cb->used += cc;
 2327 
 2328                         /* Clear next block so we keep ahead of the DMA. */
 2329                         if (cb->used + cc < cb->usedhigh)
 2330                                 audio_pint_silence(sc, cb, inp, blksize);
 2331                 }
 2332         }
 2333 
 2334         DPRINTFN(5, ("audio_pint: outp=%p cc=%d\n", cb->outp, blksize));
 2335         if (!hw->trigger_output) {
 2336                 error = hw->start_output(sc->hw_hdl, cb->outp, blksize,
 2337                     audio_pint, (void *)sc);
 2338                 if (error) {
 2339                         /* XXX does this really help? */
 2340                         DPRINTF(("audio_pint restart failed: %d\n", error));
 2341                         audio_clear(sc);
 2342                 }
 2343         }
 2344 
 2345         DPRINTFN(2, ("audio_pint: mode=%d pause=%d used=%d lowat=%d\n",
 2346                      sc->sc_mode, cb->pause, cb->used, cb->usedlow));
 2347         if ((sc->sc_mode & AUMODE_PLAY) && !cb->pause) {
 2348                 if (cb->used <= cb->usedlow) {
 2349                         audio_wakeup(&sc->sc_wchan);
 2350                         selnotify(&sc->sc_wsel, 0);
 2351                         if (sc->sc_async_audio) {
 2352                                 DPRINTFN(3, ("audio_pint: sending SIGIO %p\n",
 2353                                              sc->sc_async_audio));
 2354                                 psignal(sc->sc_async_audio, SIGIO);
 2355                         }
 2356                 }
 2357         }
 2358 
 2359         /* Possible to return one or more "phantom blocks" now. */
 2360         if (!sc->sc_full_duplex && sc->sc_rchan) {
 2361                 audio_wakeup(&sc->sc_rchan);
 2362                 selnotify(&sc->sc_rsel, 0);
 2363                 if (sc->sc_async_audio)
 2364                         psignal(sc->sc_async_audio, SIGIO);
 2365         }
 2366 }
 2367 
 2368 /*
 2369  * Called from HW driver module on completion of DMA input.
 2370  * Mark it as input in the ring buffer (fiddle pointers).
 2371  * Do a wakeup if necessary.
 2372  */
 2373 void
 2374 audio_rint(void *v)
 2375 {
 2376         struct audio_softc *sc = v;
 2377         struct audio_hw_if *hw = sc->hw_if;
 2378         struct audio_ringbuffer *cb = &sc->sc_rr;
 2379         int blksize;
 2380         int error;
 2381 
 2382         if (!sc->sc_open)
 2383                 return;         /* ignore interrupt if not open */
 2384 
 2385         blksize = cb->blksize;
 2386 
 2387         cb->inp += blksize;
 2388         if (cb->inp >= cb->end)
 2389                 cb->inp = cb->start;
 2390         cb->stamp += blksize;
 2391         if (cb->mmapped) {
 2392                 DPRINTFN(2, ("audio_rint: mmapped inp=%p cc=%d\n",
 2393                              cb->inp, blksize));
 2394                 if (!hw->trigger_input)
 2395                         (void)hw->start_input(sc->hw_hdl, cb->inp, blksize,
 2396                             audio_rint, (void *)sc);
 2397                 return;
 2398         }
 2399 
 2400 #ifdef AUDIO_INTR_TIME
 2401         {
 2402                 struct timeval tv;
 2403                 u_long t;
 2404                 microtime(&tv);
 2405                 t = tv.tv_usec + 1000000 * tv.tv_sec;
 2406                 if (sc->sc_rnintr) {
 2407                         long lastdelta, totdelta;
 2408                         lastdelta = t - sc->sc_rlastintr - sc->sc_rblktime;
 2409                         if (lastdelta > sc->sc_rblktime / 5) {
 2410                                 printf("audio: record interrupt(%d) off "
 2411                                        "relative by %ld us (%lu)\n",
 2412                                        sc->sc_rnintr, lastdelta,
 2413                                        sc->sc_rblktime);
 2414                         }
 2415                         totdelta = t - sc->sc_rfirstintr -
 2416                                 sc->sc_rblktime * sc->sc_rnintr;
 2417                         if (totdelta > sc->sc_rblktime / 2) {
 2418                                 sc->sc_rnintr++;
 2419                                 printf("audio: record interrupt(%d) off "
 2420                                        "absolute by %ld us (%lu)\n",
 2421                                        sc->sc_rnintr, totdelta,
 2422                                        sc->sc_rblktime);
 2423                                 sc->sc_rnintr++; /* avoid repeated messages */
 2424                         }
 2425                 } else
 2426                         sc->sc_rfirstintr = t;
 2427                 sc->sc_rlastintr = t;
 2428                 sc->sc_rnintr++;
 2429         }
 2430 #endif
 2431 
 2432         cb->used += blksize;
 2433         if (cb->pause) {
 2434                 DPRINTFN(1, ("audio_rint: pdrops %lu\n", cb->pdrops));
 2435                 cb->pdrops += blksize;
 2436                 cb->outp += blksize;
 2437                 if (cb->outp >= cb->end)
 2438                         cb->outp = cb->start;
 2439                 cb->used -= blksize;
 2440         } else if (cb->used + blksize >= cb->usedhigh && !cb->copying) {
 2441                 DPRINTFN(1, ("audio_rint: drops %lu\n", cb->drops));
 2442                 cb->drops += blksize;
 2443                 cb->outp += blksize;
 2444                 if (cb->outp >= cb->end)
 2445                         cb->outp = cb->start;
 2446                 cb->used -= blksize;
 2447         }
 2448 
 2449         DPRINTFN(2, ("audio_rint: inp=%p cc=%d used=%d\n",
 2450                      cb->inp, blksize, cb->used));
 2451         if (!hw->trigger_input) {
 2452                 error = hw->start_input(sc->hw_hdl, cb->inp, blksize,
 2453                     audio_rint, (void *)sc);
 2454                 if (error) {
 2455                         /* XXX does this really help? */
 2456                         DPRINTF(("audio_rint: restart failed: %d\n", error));
 2457                         audio_clear(sc);
 2458                 }
 2459         }
 2460 
 2461         audio_wakeup(&sc->sc_rchan);
 2462         selnotify(&sc->sc_rsel, 0);
 2463         if (sc->sc_async_audio)
 2464                 psignal(sc->sc_async_audio, SIGIO);
 2465 }
 2466 
 2467 int
 2468 audio_check_params(struct audio_params *p)
 2469 {
 2470         if (p->encoding == AUDIO_ENCODING_PCM16) {
 2471                 if (p->precision == 8)
 2472                         p->encoding = AUDIO_ENCODING_ULINEAR;
 2473                 else
 2474                         p->encoding = AUDIO_ENCODING_SLINEAR;
 2475         } else if (p->encoding == AUDIO_ENCODING_PCM8) {
 2476                 if (p->precision == 8)
 2477                         p->encoding = AUDIO_ENCODING_ULINEAR;
 2478                 else
 2479                         return EINVAL;
 2480         }
 2481 
 2482         if (p->encoding == AUDIO_ENCODING_SLINEAR)
 2483 #if BYTE_ORDER == LITTLE_ENDIAN
 2484                 p->encoding = AUDIO_ENCODING_SLINEAR_LE;
 2485 #else
 2486                 p->encoding = AUDIO_ENCODING_SLINEAR_BE;
 2487 #endif
 2488         if (p->encoding == AUDIO_ENCODING_ULINEAR)
 2489 #if BYTE_ORDER == LITTLE_ENDIAN
 2490                 p->encoding = AUDIO_ENCODING_ULINEAR_LE;
 2491 #else
 2492                 p->encoding = AUDIO_ENCODING_ULINEAR_BE;
 2493 #endif
 2494 
 2495         switch (p->encoding) {
 2496         case AUDIO_ENCODING_ULAW:
 2497         case AUDIO_ENCODING_ALAW:
 2498                 if (p->precision != 8)
 2499                         return (EINVAL);
 2500                 break;
 2501         case AUDIO_ENCODING_ADPCM:
 2502                 if (p->precision != 4 && p->precision != 8)
 2503                         return (EINVAL);
 2504                 break;
 2505         case AUDIO_ENCODING_SLINEAR_LE:
 2506         case AUDIO_ENCODING_SLINEAR_BE:
 2507         case AUDIO_ENCODING_ULINEAR_LE:
 2508         case AUDIO_ENCODING_ULINEAR_BE:
 2509                 /* XXX is: our zero-fill can handle any multiple of 8 */
 2510                 if (p->precision !=  8 && p->precision != 16 &&
 2511                     p->precision != 24 && p->precision != 32)
 2512                         return (EINVAL);
 2513                 break;
 2514         case AUDIO_ENCODING_MPEG_L1_STREAM:
 2515         case AUDIO_ENCODING_MPEG_L1_PACKETS:
 2516         case AUDIO_ENCODING_MPEG_L1_SYSTEM:
 2517         case AUDIO_ENCODING_MPEG_L2_STREAM:
 2518         case AUDIO_ENCODING_MPEG_L2_PACKETS:
 2519         case AUDIO_ENCODING_MPEG_L2_SYSTEM:
 2520                 break;
 2521         default:
 2522                 return (EINVAL);
 2523         }
 2524 
 2525         if (p->channels < 1 || p->channels > 8) /* sanity check # of channels*/
 2526                 return (EINVAL);
 2527 
 2528         return (0);
 2529 }
 2530 
 2531 int
 2532 audio_set_defaults(struct audio_softc *sc, u_int mode)
 2533 {
 2534         struct audio_info ai;
 2535 
 2536         /* default parameters */
 2537         sc->sc_rparams = audio_default;
 2538         sc->sc_pparams = audio_default;
 2539         sc->sc_blkset = 0;
 2540 
 2541         AUDIO_INITINFO(&ai);
 2542         ai.record.sample_rate = sc->sc_rparams.sample_rate;
 2543         ai.record.encoding    = sc->sc_rparams.encoding;
 2544         ai.record.channels    = sc->sc_rparams.channels;
 2545         ai.record.precision   = sc->sc_rparams.precision;
 2546         ai.play.sample_rate   = sc->sc_pparams.sample_rate;
 2547         ai.play.encoding      = sc->sc_pparams.encoding;
 2548         ai.play.channels      = sc->sc_pparams.channels;
 2549         ai.play.precision     = sc->sc_pparams.precision;
 2550         ai.mode               = mode;
 2551 
 2552         return (audiosetinfo(sc, &ai));
 2553 }
 2554 
 2555 int
 2556 au_set_lr_value(struct  audio_softc *sc, mixer_ctrl_t *ct, int l, int r)
 2557 {
 2558         ct->type = AUDIO_MIXER_VALUE;
 2559         ct->un.value.num_channels = 2;
 2560         ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
 2561         ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
 2562         if (sc->hw_if->set_port(sc->hw_hdl, ct) == 0)
 2563                 return 0;
 2564         ct->un.value.num_channels = 1;
 2565         ct->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2;
 2566         return sc->hw_if->set_port(sc->hw_hdl, ct);
 2567 }
 2568 
 2569 int
 2570 au_set_gain(struct audio_softc *sc, struct au_mixer_ports *ports,
 2571             int gain, int balance)
 2572 {
 2573         mixer_ctrl_t ct;
 2574         int i, error;
 2575         int l, r;
 2576         u_int mask;
 2577         int nset;
 2578 
 2579         if (balance == AUDIO_MID_BALANCE) {
 2580                 l = r = gain;
 2581         } else if (balance < AUDIO_MID_BALANCE) {
 2582                 l = gain;
 2583                 r = (balance * gain) / AUDIO_MID_BALANCE;
 2584         } else {
 2585                 r = gain;
 2586                 l = ((AUDIO_RIGHT_BALANCE - balance) * gain)
 2587                     / AUDIO_MID_BALANCE;
 2588         }
 2589         DPRINTF(("au_set_gain: gain=%d balance=%d, l=%d r=%d\n",
 2590                  gain, balance, l, r));
 2591 
 2592         if (ports->index == -1) {
 2593         usemaster:
 2594                 if (ports->master == -1)
 2595                         return 0; /* just ignore it silently */
 2596                 ct.dev = ports->master;
 2597                 error = au_set_lr_value(sc, &ct, l, r);
 2598         } else {
 2599                 ct.dev = ports->index;
 2600                 if (ports->isenum) {
 2601                         ct.type = AUDIO_MIXER_ENUM;
 2602                         error = sc->hw_if->get_port(sc->hw_hdl, &ct);
 2603                         if (error)
 2604                                 return error;
 2605                         if (ports->isdual) {
 2606                                 if (ports->cur_port == -1)
 2607                                         ct.dev = ports->master;
 2608                                 else
 2609                                         ct.dev = ports->miport[ports->cur_port];
 2610                                 error = au_set_lr_value(sc, &ct, l, r);
 2611                         } else {
 2612                                 for(i = 0; i < ports->nports; i++)
 2613                                     if (ports->misel[i] == ct.un.ord) {
 2614                                             ct.dev = ports->miport[i];
 2615                                             if (ct.dev == -1 ||
 2616                                                 au_set_lr_value(sc, &ct, l, r))
 2617                                                     goto usemaster;
 2618                                             else
 2619                                                     break;
 2620                                     }
 2621                         }
 2622                 } else {
 2623                         ct.type = AUDIO_MIXER_SET;
 2624                         error = sc->hw_if->get_port(sc->hw_hdl, &ct);
 2625                         if (error)
 2626                                 return error;
 2627                         mask = ct.un.mask;
 2628                         nset = 0;
 2629                         for(i = 0; i < ports->nports; i++) {
 2630                                 if (ports->misel[i] & mask) {
 2631                                     ct.dev = ports->miport[i];
 2632                                     if (ct.dev != -1 &&
 2633                                         au_set_lr_value(sc, &ct, l, r) == 0)
 2634                                             nset++;
 2635                                 }
 2636                         }
 2637                         if (nset == 0)
 2638                                 goto usemaster;
 2639                 }
 2640         }
 2641         if (!error)
 2642                 mixer_signal(sc);
 2643         return error;
 2644 }
 2645 
 2646 int
 2647 au_get_lr_value(struct  audio_softc *sc, mixer_ctrl_t *ct, int *l, int *r)
 2648 {
 2649         int error;
 2650 
 2651         ct->un.value.num_channels = 2;
 2652         if (sc->hw_if->get_port(sc->hw_hdl, ct) == 0) {
 2653                 *l = ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
 2654                 *r = ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
 2655         } else {
 2656                 ct->un.value.num_channels = 1;
 2657                 error = sc->hw_if->get_port(sc->hw_hdl, ct);
 2658                 if (error)
 2659                         return error;
 2660                 *r = *l = ct->un.value.level[AUDIO_MIXER_LEVEL_MONO];
 2661         }
 2662         return 0;
 2663 }
 2664 
 2665 void
 2666 au_get_gain(struct audio_softc *sc, struct au_mixer_ports *ports,
 2667             u_int *pgain, u_char *pbalance)
 2668 {
 2669         mixer_ctrl_t ct;
 2670         int i, l, r, n;
 2671         int lgain = AUDIO_MAX_GAIN/2, rgain = AUDIO_MAX_GAIN/2;
 2672 
 2673         if (ports->index == -1) {
 2674         usemaster:
 2675                 if (ports->master == -1)
 2676                         goto bad;
 2677                 ct.dev = ports->master;
 2678                 ct.type = AUDIO_MIXER_VALUE;
 2679                 if (au_get_lr_value(sc, &ct, &lgain, &rgain))
 2680                         goto bad;
 2681         } else {
 2682                 ct.dev = ports->index;
 2683                 if (ports->isenum) {
 2684                         ct.type = AUDIO_MIXER_ENUM;
 2685                         if (sc->hw_if->get_port(sc->hw_hdl, &ct))
 2686                                 goto bad;
 2687                         ct.type = AUDIO_MIXER_VALUE;
 2688                         if (ports->isdual) {
 2689                                 if (ports->cur_port == -1)
 2690                                         ct.dev = ports->master;
 2691                                 else
 2692                                         ct.dev = ports->miport[ports->cur_port];
 2693                                 au_get_lr_value(sc, &ct, &lgain, &rgain);
 2694                         } else {
 2695                                 for(i = 0; i < ports->nports; i++)
 2696                                     if (ports->misel[i] == ct.un.ord) {
 2697                                             ct.dev = ports->miport[i];
 2698                                             if (ct.dev == -1 ||
 2699                                                 au_get_lr_value(sc, &ct,
 2700                                                                 &lgain, &rgain))
 2701                                                     goto usemaster;
 2702                                             else
 2703                                                     break;
 2704                                     }
 2705                         }
 2706                 } else {
 2707                         ct.type = AUDIO_MIXER_SET;
 2708                         if (sc->hw_if->get_port(sc->hw_hdl, &ct))
 2709                                 goto bad;
 2710                         ct.type = AUDIO_MIXER_VALUE;
 2711                         lgain = rgain = n = 0;
 2712                         for(i = 0; i < ports->nports; i++) {
 2713                                 if (ports->misel[i] & ct.un.mask) {
 2714                                         ct.dev = ports->miport[i];
 2715                                         if (ct.dev == -1 ||
 2716                                             au_get_lr_value(sc, &ct, &l, &r))
 2717                                                 goto usemaster;
 2718                                         else {
 2719                                                 lgain += l;
 2720                                                 rgain += r;
 2721                                                 n++;
 2722                                         }
 2723                                 }
 2724                         }
 2725                         if (n != 0) {
 2726                                 lgain /= n;
 2727                                 rgain /= n;
 2728                         }
 2729                 }
 2730         }
 2731 bad:
 2732         if (lgain == rgain) {   /* handles lgain==rgain==0 */
 2733                 *pgain = lgain;
 2734                 *pbalance = AUDIO_MID_BALANCE;
 2735         } else if (lgain < rgain) {
 2736                 *pgain = rgain;
 2737                 /* balance should be > AUDIO_MID_BALANCE */
 2738                 *pbalance = AUDIO_RIGHT_BALANCE -
 2739                         (AUDIO_MID_BALANCE * lgain) / rgain;
 2740         } else /* lgain > rgain */ {
 2741                 *pgain = lgain;
 2742                 /* balance should be < AUDIO_MID_BALANCE */
 2743                 *pbalance = (AUDIO_MID_BALANCE * rgain) / lgain;
 2744         }
 2745 }
 2746 
 2747 int
 2748 au_set_port(struct audio_softc *sc, struct au_mixer_ports *ports, u_int port)
 2749 {
 2750         mixer_ctrl_t ct;
 2751         int i, error, use_mixerout;
 2752 
 2753         use_mixerout = 1;
 2754         if (port == 0) {
 2755                 if (ports->allports == 0)
 2756                         return 0;               /* Allow this special case. */
 2757                 else if (ports->isdual) {
 2758                         if (ports->cur_port == -1) {
 2759                                 return 0;
 2760                         } else {
 2761                                 port = ports->aumask[ports->cur_port];
 2762                                 ports->cur_port = -1;
 2763                                 use_mixerout = 0;
 2764                         }
 2765                 }
 2766         }
 2767         if (ports->index == -1)
 2768                 return EINVAL;
 2769         ct.dev = ports->index;
 2770         if (ports->isenum) {
 2771                 if (port & (port-1))
 2772                         return EINVAL; /* Only one port allowed */
 2773                 ct.type = AUDIO_MIXER_ENUM;
 2774                 error = EINVAL;
 2775                 for(i = 0; i < ports->nports; i++)
 2776                         if (ports->aumask[i] == port) {
 2777                                 if (ports->isdual && use_mixerout) {
 2778                                         ct.un.ord = ports->mixerout;
 2779                                         ports->cur_port = i;
 2780                                 } else {
 2781                                         ct.un.ord = ports->misel[i];
 2782                                 }
 2783                                 error = sc->hw_if->set_port(sc->hw_hdl, &ct);
 2784                                 break;
 2785                         }
 2786         } else {
 2787                 ct.type = AUDIO_MIXER_SET;
 2788                 ct.un.mask = 0;
 2789                 for(i = 0; i < ports->nports; i++)
 2790                         if (ports->aumask[i] & port)
 2791                                 ct.un.mask |= ports->misel[i];
 2792                 if (port != 0 && ct.un.mask == 0)
 2793                         error = EINVAL;
 2794                 else
 2795                         error = sc->hw_if->set_port(sc->hw_hdl, &ct);
 2796         }
 2797         if (!error)
 2798                 mixer_signal(sc);
 2799         return error;
 2800 }
 2801 
 2802 int
 2803 au_get_port(struct audio_softc *sc, struct au_mixer_ports *ports)
 2804 {
 2805         mixer_ctrl_t ct;
 2806         int i, aumask;
 2807 
 2808         if (ports->index == -1)
 2809                 return 0;
 2810         ct.dev = ports->index;
 2811         ct.type = ports->isenum ? AUDIO_MIXER_ENUM : AUDIO_MIXER_SET;
 2812         if (sc->hw_if->get_port(sc->hw_hdl, &ct))
 2813                 return 0;
 2814         aumask = 0;
 2815         if (ports->isenum) {
 2816                 if (ports->isdual && ports->cur_port != -1) {
 2817                         if (ports->mixerout == ct.un.ord)
 2818                                 aumask = ports->aumask[ports->cur_port];
 2819                         else
 2820                                 ports->cur_port = -1;
 2821                 }
 2822                 if (aumask == 0)
 2823                         for(i = 0; i < ports->nports; i++)
 2824                                 if (ports->misel[i] == ct.un.ord)
 2825                                         aumask = ports->aumask[i];
 2826         } else {
 2827                 for(i = 0; i < ports->nports; i++)
 2828                         if (ct.un.mask & ports->misel[i])
 2829                                 aumask |= ports->aumask[i];
 2830         }
 2831         return aumask;
 2832 }
 2833 
 2834 #if NAURATECONV <= 0
 2835 /* dummy function for the case that aurateconv is not linked */
 2836 int
 2837 auconv_check_params(const struct audio_params *params)
 2838 {
 2839         if (params->hw_channels == params->channels
 2840             && params->hw_sample_rate == params->sample_rate)
 2841                 return 0;       /* No conversion */
 2842         return (EINVAL);
 2843 }
 2844 #endif /* !NAURATECONV */
 2845 
 2846 int
 2847 audiosetinfo(struct audio_softc *sc, struct audio_info *ai)
 2848 {
 2849         struct audio_prinfo *r = &ai->record, *p = &ai->play;
 2850         int cleared, pausechange;
 2851         int s, setmode, modechange = 0;
 2852         int error;
 2853         struct audio_hw_if *hw = sc->hw_if;
 2854         struct audio_params pp, rp;
 2855         int np, nr;
 2856         unsigned int blks;
 2857         int oldpblksize, oldrblksize;
 2858         int rbus, pbus;
 2859         u_int gain;
 2860         u_char balance;
 2861 
 2862         if (hw == 0)            /* HW has not attached */
 2863                 return(ENXIO);
 2864 
 2865         rbus = sc->sc_rbus;
 2866         pbus = sc->sc_pbus;
 2867         error = 0;
 2868         cleared = 0;
 2869         pausechange = 0;
 2870 
 2871         pp = sc->sc_pparams;    /* Temporary encoding storage in */
 2872         rp = sc->sc_rparams;    /* case setting the modes fails. */
 2873         nr = np = 0;
 2874 
 2875         if (p->sample_rate != ~0) {
 2876                 pp.sample_rate = p->sample_rate;
 2877                 np++;
 2878         }
 2879         if (r->sample_rate != ~0) {
 2880                 rp.sample_rate = r->sample_rate;
 2881                 nr++;
 2882         }
 2883         if (p->encoding != ~0) {
 2884                 pp.encoding = p->encoding;
 2885                 np++;
 2886         }
 2887         if (r->encoding != ~0) {
 2888                 rp.encoding = r->encoding;
 2889                 nr++;
 2890         }
 2891         if (p->precision != ~0) {
 2892                 pp.precision = p->precision;
 2893                 np++;
 2894         }
 2895         if (r->precision != ~0) {
 2896                 rp.precision = r->precision;
 2897                 nr++;
 2898         }
 2899         if (p->channels != ~0) {
 2900                 pp.channels = p->channels;
 2901                 np++;
 2902         }
 2903         if (r->channels != ~0) {
 2904                 rp.channels = r->channels;
 2905                 nr++;
 2906         }
 2907 #ifdef AUDIO_DEBUG
 2908         if (audiodebug && nr)
 2909             audio_print_params("Setting record params", &rp);
 2910         if (audiodebug && np)
 2911             audio_print_params("Setting play params", &pp);
 2912 #endif
 2913         if (nr && (error = audio_check_params(&rp)))
 2914                 return error;
 2915         if (np && (error = audio_check_params(&pp)))
 2916                 return error;
 2917         setmode = 0;
 2918         if (nr) {
 2919                 if (!cleared) {
 2920                         audio_clear(sc);
 2921                         cleared = 1;
 2922                 }
 2923                 modechange = 1;
 2924                 rp.sw_code = 0;
 2925                 rp.factor = 1;
 2926                 rp.factor_denom = 1;
 2927                 rp.hw_sample_rate = rp.sample_rate;
 2928                 rp.hw_encoding = rp.encoding;
 2929                 rp.hw_precision = rp.precision;
 2930                 rp.hw_channels = rp.channels;
 2931                 setmode |= AUMODE_RECORD;
 2932         }
 2933         if (np) {
 2934                 if (!cleared) {
 2935                         audio_clear(sc);
 2936                         cleared = 1;
 2937                 }
 2938                 modechange = 1;
 2939                 pp.sw_code = 0;
 2940                 pp.factor = 1;
 2941                 pp.factor_denom = 1;
 2942                 pp.hw_sample_rate = pp.sample_rate;
 2943                 pp.hw_encoding = pp.encoding;
 2944                 pp.hw_precision = pp.precision;
 2945                 pp.hw_channels = pp.channels;
 2946                 setmode |= AUMODE_PLAY;
 2947         }
 2948 
 2949         if (ai->mode != ~0) {
 2950                 if (!cleared) {
 2951                         audio_clear(sc);
 2952                         cleared = 1;
 2953                 }
 2954                 modechange = 1;
 2955                 sc->sc_mode = ai->mode;
 2956                 if (sc->sc_mode & AUMODE_PLAY_ALL)
 2957                         sc->sc_mode |= AUMODE_PLAY;
 2958                 if ((sc->sc_mode & AUMODE_PLAY) && !sc->sc_full_duplex)
 2959                         /* Play takes precedence */
 2960                         sc->sc_mode &= ~AUMODE_RECORD;
 2961         }
 2962 
 2963         if (modechange) {
 2964                 int orig_p_channels, orig_p_rate;
 2965                 int orig_r_channels, orig_r_rate;
 2966                 int indep;
 2967 
 2968                 indep = hw->get_props(sc->hw_hdl) & AUDIO_PROP_INDEPENDENT;
 2969                 if (!indep) {
 2970                         if (setmode == AUMODE_RECORD)
 2971                                 pp = rp;
 2972                         else if (setmode == AUMODE_PLAY)
 2973                                 rp = pp;
 2974                 }
 2975                 /* Some device drivers change channels/sample_rate and change
 2976                  * no channels/sample_rate. */
 2977                 orig_p_channels = pp.channels;
 2978                 orig_p_rate = pp.sample_rate;
 2979                 orig_r_channels = rp.channels;
 2980                 orig_r_rate = rp.sample_rate;
 2981                 error = hw->set_params(sc->hw_hdl, setmode,
 2982                     sc->sc_mode & (AUMODE_PLAY | AUMODE_RECORD), &pp, &rp);
 2983                 if (error)
 2984                         return (error);
 2985 
 2986                 if (np) {
 2987                         if (orig_p_channels != pp.channels)
 2988                                 pp.hw_channels = pp.channels;
 2989                         if (orig_p_rate != pp.sample_rate)
 2990                                 pp.hw_sample_rate = pp.sample_rate;
 2991                         error = auconv_check_params(&pp);
 2992                         if (error)
 2993                                 return (error);
 2994                 }
 2995                 if (nr) {
 2996                         if (orig_r_channels != rp.channels)
 2997                                 rp.hw_channels = rp.channels;
 2998                         if (orig_r_rate != rp.sample_rate)
 2999                                 rp.hw_sample_rate = rp.sample_rate;
 3000                         error = auconv_check_params(&rp);
 3001                         if (error)
 3002                                 return (error);
 3003                 }
 3004 
 3005                 if (!indep) {
 3006                         if (setmode == AUMODE_RECORD) {
 3007                                 pp.sample_rate = rp.sample_rate;
 3008                                 pp.encoding    = rp.encoding;
 3009                                 pp.channels    = rp.channels;
 3010                                 pp.precision   = rp.precision;
 3011                         } else if (setmode == AUMODE_PLAY) {
 3012                                 rp.sample_rate = pp.sample_rate;
 3013                                 rp.encoding    = pp.encoding;
 3014                                 rp.channels    = pp.channels;
 3015                                 rp.precision   = pp.precision;
 3016                         }
 3017                 }
 3018                 sc->sc_rparams = rp;
 3019                 sc->sc_pparams = pp;
 3020         }
 3021 
 3022         oldpblksize = sc->sc_pr.blksize;
 3023         oldrblksize = sc->sc_rr.blksize;
 3024         /* Play params can affect the record params, so recalculate blksize. */
 3025         if (nr || np) {
 3026                 audio_calc_blksize(sc, AUMODE_RECORD);
 3027                 audio_calc_blksize(sc, AUMODE_PLAY);
 3028         }
 3029 #ifdef AUDIO_DEBUG
 3030         if (audiodebug > 1 && nr)
 3031             audio_print_params("After setting record params", &sc->sc_rparams);
 3032         if (audiodebug > 1 && np)
 3033             audio_print_params("After setting play params", &sc->sc_pparams);
 3034 #endif
 3035 
 3036         if (p->port != ~0) {
 3037                 if (!cleared) {
 3038                         audio_clear(sc);
 3039                         cleared = 1;
 3040                 }
 3041                 error = au_set_port(sc, &sc->sc_outports, p->port);
 3042                 if (error)
 3043                         return(error);
 3044         }
 3045         if (r->port != ~0) {
 3046                 if (!cleared) {
 3047                         audio_clear(sc);
 3048                         cleared = 1;
 3049                 }
 3050                 error = au_set_port(sc, &sc->sc_inports, r->port);
 3051                 if (error)
 3052                         return(error);
 3053         }
 3054         if (p->gain != ~0) {
 3055                 au_get_gain(sc, &sc->sc_outports, &gain, &balance);
 3056                 error = au_set_gain(sc, &sc->sc_outports, p->gain, balance);
 3057                 if (error)
 3058                         return(error);
 3059         }
 3060         if (r->gain != ~0) {
 3061                 au_get_gain(sc, &sc->sc_inports, &gain, &balance);
 3062                 error = au_set_gain(sc, &sc->sc_inports, r->gain, balance);
 3063                 if (error)
 3064                         return(error);
 3065         }
 3066 
 3067         if (p->balance != (u_char)~0) {
 3068                 au_get_gain(sc, &sc->sc_outports, &gain, &balance);
 3069                 error = au_set_gain(sc, &sc->sc_outports, gain, p->balance);
 3070                 if (error)
 3071                         return(error);
 3072         }
 3073         if (r->balance != (u_char)~0) {
 3074                 au_get_gain(sc, &sc->sc_inports, &gain, &balance);
 3075                 error = au_set_gain(sc, &sc->sc_inports, gain, r->balance);
 3076                 if (error)
 3077                         return(error);
 3078         }
 3079 
 3080         if (ai->monitor_gain != ~0 &&
 3081             sc->sc_monitor_port != -1) {
 3082                 mixer_ctrl_t ct;
 3083 
 3084                 ct.dev = sc->sc_monitor_port;
 3085                 ct.type = AUDIO_MIXER_VALUE;
 3086                 ct.un.value.num_channels = 1;
 3087                 ct.un.value.level[AUDIO_MIXER_LEVEL_MONO] = ai->monitor_gain;
 3088                 error = sc->hw_if->set_port(sc->hw_hdl, &ct);
 3089                 if (error)
 3090                         return(error);
 3091         }
 3092 
 3093         if (p->pause != (u_char)~0) {
 3094                 sc->sc_pr.pause = p->pause;
 3095                 pbus = !p->pause;
 3096                 pausechange=1;
 3097         }
 3098         if (r->pause != (u_char)~0) {
 3099                 sc->sc_rr.pause = r->pause;
 3100                 rbus = !r->pause;
 3101                 pausechange=1;
 3102         }
 3103 
 3104         if (ai->blocksize != ~0) {
 3105                 /* Block size specified explicitly. */
 3106                 if (!cleared) {
 3107                         audio_clear(sc);
 3108                         cleared = 1;
 3109                 }
 3110                 if (ai->blocksize == 0) {
 3111                         sc->sc_blkset = 0;
 3112                         audio_calc_blksize(sc, AUMODE_RECORD);
 3113                         audio_calc_blksize(sc, AUMODE_PLAY);
 3114                 } else {
 3115                         sc->sc_blkset = 1;
 3116                         sc->sc_pr.blksize = sc->sc_rr.blksize = ai->blocksize;
 3117                 }
 3118         }
 3119 
 3120         if (ai->mode != ~0) {
 3121                 if (sc->sc_mode & AUMODE_PLAY)
 3122                         audio_init_play(sc);
 3123                 if (sc->sc_mode & AUMODE_RECORD)
 3124                         audio_init_record(sc);
 3125         }
 3126 
 3127         if (hw->commit_settings) {
 3128                 error = hw->commit_settings(sc->hw_hdl);
 3129                 if (error)
 3130                         return (error);
 3131         }
 3132 
 3133         if (cleared || pausechange) {
 3134                 s = splaudio();
 3135                 error = audio_initbufs(sc);
 3136                 if (error) goto err;
 3137                 if (sc->sc_pr.blksize != oldpblksize ||
 3138                     sc->sc_rr.blksize != oldrblksize)
 3139                         audio_calcwater(sc);
 3140                 if ((sc->sc_mode & AUMODE_PLAY) &&
 3141                     pbus && !sc->sc_pbus)
 3142                         error = audiostartp(sc);
 3143                 if (!error &&
 3144                     (sc->sc_mode & AUMODE_RECORD) &&
 3145                     rbus && !sc->sc_rbus)
 3146                         error = audiostartr(sc);
 3147         err:
 3148                 splx(s);
 3149                 if (error)
 3150                         return error;
 3151         }
 3152 
 3153         /* Change water marks after initializing the buffers. */
 3154         if (ai->hiwat != ~0) {
 3155                 blks = ai->hiwat;
 3156                 if (blks > sc->sc_pr.maxblks)
 3157                         blks = sc->sc_pr.maxblks;
 3158                 if (blks < 2)
 3159                         blks = 2;
 3160                 sc->sc_pr.usedhigh = blks * sc->sc_pr.blksize;
 3161         }
 3162         if (ai->lowat != ~0) {
 3163                 blks = ai->lowat;
 3164                 if (blks > sc->sc_pr.maxblks - 1)
 3165                         blks = sc->sc_pr.maxblks - 1;
 3166                 sc->sc_pr.usedlow = blks * sc->sc_pr.blksize;
 3167         }
 3168         if (ai->hiwat != ~0 || ai->lowat != ~0) {
 3169                 if (sc->sc_pr.usedlow > sc->sc_pr.usedhigh - sc->sc_pr.blksize)
 3170                         sc->sc_pr.usedlow =
 3171                                 sc->sc_pr.usedhigh - sc->sc_pr.blksize;
 3172         }
 3173 
 3174         return (0);
 3175 }
 3176 
 3177 int
 3178 audiogetinfo(struct audio_softc *sc, struct audio_info *ai)
 3179 {
 3180         struct audio_prinfo *r = &ai->record, *p = &ai->play;
 3181         struct audio_hw_if *hw = sc->hw_if;
 3182 
 3183         if (hw == 0)            /* HW has not attached */
 3184                 return(ENXIO);
 3185 
 3186         p->sample_rate = sc->sc_pparams.sample_rate;
 3187         r->sample_rate = sc->sc_rparams.sample_rate;
 3188         p->channels = sc->sc_pparams.channels;
 3189         r->channels = sc->sc_rparams.channels;
 3190         p->precision = sc->sc_pparams.precision;
 3191         r->precision = sc->sc_rparams.precision;
 3192         p->encoding = sc->sc_pparams.encoding;
 3193         r->encoding = sc->sc_rparams.encoding;
 3194 
 3195         r->port = au_get_port(sc, &sc->sc_inports);
 3196         p->port = au_get_port(sc, &sc->sc_outports);
 3197 
 3198         r->avail_ports = sc->sc_inports.allports;
 3199         p->avail_ports = sc->sc_outports.allports;
 3200 
 3201         au_get_gain(sc, &sc->sc_inports,  &r->gain, &r->balance);
 3202         au_get_gain(sc, &sc->sc_outports, &p->gain, &p->balance);
 3203 
 3204         if (sc->sc_monitor_port != -1) {
 3205                 mixer_ctrl_t ct;
 3206 
 3207                 ct.dev = sc->sc_monitor_port;
 3208                 ct.type = AUDIO_MIXER_VALUE;
 3209                 ct.un.value.num_channels = 1;
 3210                 if (sc->hw_if->get_port(sc->hw_hdl, &ct))
 3211                         ai->monitor_gain = 0;
 3212                 else
 3213                         ai->monitor_gain =
 3214                                 ct.un.value.level[AUDIO_MIXER_LEVEL_MONO];
 3215         } else
 3216                 ai->monitor_gain = 0;
 3217 
 3218         p->seek = sc->sc_pr.used;
 3219         r->seek = sc->sc_rr.used;
 3220 
 3221         p->samples = sc->sc_pr.stamp - sc->sc_pr.drops;
 3222         r->samples = sc->sc_rr.stamp - sc->sc_rr.drops;
 3223 
 3224         p->eof = sc->sc_eof;
 3225         r->eof = 0;
 3226 
 3227         p->pause = sc->sc_pr.pause;
 3228         r->pause = sc->sc_rr.pause;
 3229 
 3230         p->error = sc->sc_pr.drops != 0;
 3231         r->error = sc->sc_rr.drops != 0;
 3232 
 3233         p->waiting = r->waiting = 0;            /* open never hangs */
 3234 
 3235         p->open = (sc->sc_open & AUOPEN_WRITE) != 0;
 3236         r->open = (sc->sc_open & AUOPEN_READ) != 0;
 3237 
 3238         p->active = sc->sc_pbus;
 3239         r->active = sc->sc_rbus;
 3240 
 3241         p->buffer_size = sc->sc_pr.bufsize;
 3242         r->buffer_size = sc->sc_rr.bufsize;
 3243 
 3244         ai->blocksize = sc->sc_pr.blksize;
 3245         ai->hiwat = sc->sc_pr.usedhigh / sc->sc_pr.blksize;
 3246         ai->lowat = sc->sc_pr.usedlow / sc->sc_pr.blksize;
 3247         ai->mode = sc->sc_mode;
 3248 
 3249         return (0);
 3250 }
 3251 
 3252 /*
 3253  * Mixer driver
 3254  */
 3255 int
 3256 mixer_open(dev_t dev, struct audio_softc *sc, int flags, int ifmt,
 3257            struct proc *p)
 3258 {
 3259         if (!sc->hw_if)
 3260                 return (ENXIO);
 3261 
 3262         DPRINTF(("mixer_open: flags=0x%x sc=%p\n", flags, sc));
 3263 
 3264         return (0);
 3265 }
 3266 
 3267 /*
 3268  * Remove a process from those to be signalled on mixer activity.
 3269  */
 3270 static void
 3271 mixer_remove(struct audio_softc *sc, struct proc *p)
 3272 {
 3273         struct mixer_asyncs **pm, *m;
 3274 
 3275         for(pm = &sc->sc_async_mixer; *pm; pm = &(*pm)->next) {
 3276                 if ((*pm)->proc == p) {
 3277                         m = *pm;
 3278                         *pm = m->next;
 3279                         free(m, M_DEVBUF);
 3280                         return;
 3281                 }
 3282         }
 3283 }
 3284 
 3285 /*
 3286  * Signal all processes waiting for the mixer.
 3287  */
 3288 static void
 3289 mixer_signal(struct audio_softc *sc)
 3290 {
 3291         struct mixer_asyncs *m;
 3292 
 3293         for(m = sc->sc_async_mixer; m; m = m->next)
 3294                 psignal(m->proc, SIGIO);
 3295 }
 3296 
 3297 /*
 3298  * Close a mixer device
 3299  */
 3300 /* ARGSUSED */
 3301 int
 3302 mixer_close(struct audio_softc *sc, int flags, int ifmt, struct proc *p)
 3303 {
 3304         DPRINTF(("mixer_close: sc %p\n", sc));
 3305 
 3306         mixer_remove(sc, p);
 3307 
 3308         return (0);
 3309 }
 3310 
 3311 int
 3312 mixer_ioctl(struct audio_softc *sc, u_long cmd, caddr_t addr, int flag,
 3313             struct proc *p)
 3314 {
 3315         struct audio_hw_if *hw = sc->hw_if;
 3316         int error = EINVAL;
 3317 
 3318         DPRINTF(("mixer_ioctl(%lu,'%c',%lu)\n",
 3319                  IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff));
 3320 
 3321         switch (cmd) {
 3322         case FIOASYNC:
 3323                 mixer_remove(sc, p); /* remove old entry */
 3324                 if (*(int *)addr) {
 3325                         struct mixer_asyncs *ma;
 3326                         ma = malloc(sizeof (struct mixer_asyncs),
 3327                                     M_DEVBUF, M_WAITOK);
 3328                         ma->next = sc->sc_async_mixer;
 3329                         ma->proc = p;
 3330                         sc->sc_async_mixer = ma;
 3331                 }
 3332                 error = 0;
 3333                 break;
 3334 
 3335         case AUDIO_GETDEV:
 3336                 DPRINTF(("AUDIO_GETDEV\n"));
 3337                 error = hw->getdev(sc->hw_hdl, (audio_device_t *)addr);
 3338                 break;
 3339 
 3340         case AUDIO_MIXER_DEVINFO:
 3341                 DPRINTF(("AUDIO_MIXER_DEVINFO\n"));
 3342                 ((mixer_devinfo_t *)addr)->un.v.delta = 0; /* default */
 3343                 error = hw->query_devinfo(sc->hw_hdl, (mixer_devinfo_t *)addr);
 3344                 break;
 3345 
 3346         case AUDIO_MIXER_READ:
 3347                 DPRINTF(("AUDIO_MIXER_READ\n"));
 3348                 error = hw->get_port(sc->hw_hdl, (mixer_ctrl_t *)addr);
 3349                 break;
 3350 
 3351         case AUDIO_MIXER_WRITE:
 3352                 DPRINTF(("AUDIO_MIXER_WRITE\n"));
 3353                 error = hw->set_port(sc->hw_hdl, (mixer_ctrl_t *)addr);
 3354                 if (!error && hw->commit_settings)
 3355                         error = hw->commit_settings(sc->hw_hdl);
 3356                 if (!error)
 3357                         mixer_signal(sc);
 3358                 break;
 3359 
 3360         default:
 3361                 if (hw->dev_ioctl)
 3362                         error = hw->dev_ioctl(sc->hw_hdl, cmd, addr, flag, p);
 3363                 else
 3364                         error = EINVAL;
 3365                 break;
 3366         }
 3367         DPRINTF(("mixer_ioctl(%lu,'%c',%lu) result %d\n",
 3368                  IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff, error));
 3369         return (error);
 3370 }
 3371 #endif /* NAUDIO > 0 */
 3372 
 3373 #include "midi.h"
 3374 
 3375 #if NAUDIO == 0 && (NMIDI > 0 || NMIDIBUS > 0)
 3376 #include <sys/param.h>
 3377 #include <sys/systm.h>
 3378 #include <sys/device.h>
 3379 #include <sys/audioio.h>
 3380 #include <dev/audio_if.h>
 3381 #endif
 3382 
 3383 #if NAUDIO > 0 || (NMIDI > 0 || NMIDIBUS > 0)
 3384 int
 3385 audioprint(void *aux, const char *pnp)
 3386 {
 3387         struct audio_attach_args *arg = aux;
 3388         const char *type;
 3389 
 3390         if (pnp != NULL) {
 3391                 switch (arg->type) {
 3392                 case AUDIODEV_TYPE_AUDIO:
 3393                         type = "audio";
 3394                         break;
 3395                 case AUDIODEV_TYPE_MIDI:
 3396                         type = "midi";
 3397                         break;
 3398                 case AUDIODEV_TYPE_OPL:
 3399                         type = "opl";
 3400                         break;
 3401                 case AUDIODEV_TYPE_MPU:
 3402                         type = "mpu";
 3403                         break;
 3404                 default:
 3405                         panic("audioprint: unknown type %d", arg->type);
 3406                 }
 3407                 aprint_normal("%s at %s", type, pnp);
 3408         }
 3409         return (UNCONF);
 3410 }
 3411 
 3412 #endif /* NAUDIO > 0 || (NMIDI > 0 || NMIDIBUS > 0) */

Cache object: ce34cf9a128f4bd8c42b92cd8e3260f0


[ 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.