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/sound/pci/emu10kx.c

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

    1 /*-
    2  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
    3  * Copyright (c) 2003-2007 Yuriy Tsibizov <yuriy.tsibizov@gfk.ru>
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  *
   27  * $FreeBSD: releng/8.1/sys/dev/sound/pci/emu10kx.c 193640 2009-06-07 19:12:08Z ariff $
   28  */
   29 
   30 #include <sys/param.h>
   31 #include <sys/types.h>
   32 #include <sys/bus.h>
   33 #include <machine/bus.h>
   34 #include <sys/rman.h>
   35 #include <sys/systm.h>
   36 #include <sys/sbuf.h>
   37 #include <sys/queue.h>
   38 #include <sys/lock.h>
   39 #include <sys/mutex.h>
   40 #include <sys/sysctl.h>
   41 #include <sys/kdb.h>
   42 
   43 #include <dev/pci/pcireg.h>
   44 #include <dev/pci/pcivar.h>
   45 
   46 #include <machine/clock.h>      /* for DELAY */
   47 
   48 #ifdef HAVE_KERNEL_OPTION_HEADERS
   49 #include "opt_snd.h"
   50 #endif
   51 
   52 #include <dev/sound/chip.h>
   53 #include <dev/sound/pcm/sound.h>
   54 #include <dev/sound/pcm/ac97.h>
   55 
   56 #include <dev/sound/pci/emu10kx.h>
   57 
   58 /* hw flags */
   59 #define HAS_51          0x0001
   60 #define HAS_71          0x0002
   61 #define HAS_AC97        0x0004
   62 
   63 #define IS_EMU10K1      0x0008
   64 #define IS_EMU10K2      0x0010
   65 #define IS_CA0102       0x0020
   66 #define IS_CA0108       0x0040
   67 #define IS_UNKNOWN      0x0080
   68 
   69 #define BROKEN_DIGITAL  0x0100
   70 #define DIGITAL_ONLY    0x0200
   71 
   72 #define IS_CARDBUS      0x0400
   73 
   74 #define MODE_ANALOG     1
   75 #define MODE_DIGITAL    2
   76 #define SPDIF_MODE_PCM  1
   77 #define SPDIF_MODE_AC3  2
   78 
   79 #define MACS    0x0
   80 #define MACS1   0x1
   81 #define MACW    0x2
   82 #define MACW1   0x3
   83 #define MACINTS 0x4
   84 #define MACINTW 0x5
   85 #define ACC3    0x6
   86 #define MACMV   0x7
   87 #define ANDXOR  0x8
   88 #define TSTNEG  0x9
   89 #define LIMIT   0xA
   90 #define LIMIT1  0xB
   91 #define LOG     0xC
   92 #define EXP     0xD
   93 #define INTERP  0xE
   94 #define SKIP    0xF
   95 
   96 #define GPR(i)  (sc->gpr_base+(i))
   97 #define INP(i)  (sc->input_base+(i))
   98 #define OUTP(i) (sc->output_base+(i))
   99 #define FX(i)   (i)
  100 #define FX2(i)  (sc->efxc_base+(i))
  101 #define DSP_CONST(i) (sc->dsp_zero+(i))
  102 
  103 #define COND_NORMALIZED DSP_CONST(0x1)
  104 #define COND_BORROW     DSP_CONST(0x2)
  105 #define COND_MINUS      DSP_CONST(0x3)
  106 #define COND_LESS_ZERO  DSP_CONST(0x4)
  107 #define COND_EQ_ZERO    DSP_CONST(0x5)
  108 #define COND_SATURATION DSP_CONST(0x6)
  109 #define COND_NEQ_ZERO   DSP_CONST(0x8)
  110 
  111 #define DSP_ACCUM       DSP_CONST(0x16)
  112 #define DSP_CCR         DSP_CONST(0x17)
  113 
  114 /* Live! Inputs */
  115 #define IN_AC97_L       0x00
  116 #define IN_AC97_R       0x01
  117 #define IN_AC97         IN_AC97_L
  118 #define IN_SPDIF_CD_L   0x02
  119 #define IN_SPDIF_CD_R   0x03
  120 #define IN_SPDIF_CD     IN_SPDIF_CD_L
  121 #define IN_ZOOM_L       0x04
  122 #define IN_ZOOM_R       0x05
  123 #define IN_ZOOM         IN_ZOOM_L
  124 #define IN_TOSLINK_L    0x06
  125 #define IN_TOSLINK_R    0x07
  126 #define IN_TOSLINK      IN_TOSLINK_L
  127 #define IN_LINE1_L      0x08
  128 #define IN_LINE1_R      0x09
  129 #define IN_LINE1        IN_LINE1_L
  130 #define IN_COAX_SPDIF_L 0x0a
  131 #define IN_COAX_SPDIF_R 0x0b
  132 #define IN_COAX_SPDIF   IN_COAX_SPDIF_L
  133 #define IN_LINE2_L      0x0c
  134 #define IN_LINE2_R      0x0d
  135 #define IN_LINE2        IN_LINE2_L
  136 #define IN_0E           0x0e
  137 #define IN_0F           0x0f
  138 
  139 /* Outputs */
  140 #define OUT_AC97_L      0x00
  141 #define OUT_AC97_R      0x01
  142 #define OUT_AC97        OUT_AC97_L
  143 #define OUT_A_FRONT     OUT_AC97
  144 #define OUT_TOSLINK_L   0x02
  145 #define OUT_TOSLINK_R   0x03
  146 #define OUT_TOSLINK     OUT_TOSLINK_L
  147 #define OUT_D_CENTER    0x04
  148 #define OUT_D_SUB       0x05
  149 #define OUT_HEADPHONE_L 0x06
  150 #define OUT_HEADPHONE_R 0x07
  151 #define OUT_HEADPHONE   OUT_HEADPHONE_L
  152 #define OUT_REAR_L      0x08
  153 #define OUT_REAR_R      0x09
  154 #define OUT_REAR        OUT_REAR_L
  155 #define OUT_ADC_REC_L   0x0a
  156 #define OUT_ADC_REC_R   0x0b
  157 #define OUT_ADC_REC     OUT_ADC_REC_L
  158 #define OUT_MIC_CAP     0x0c
  159 
  160 /* Live! 5.1 Digital, non-standart 5.1 (center & sub) outputs */
  161 #define OUT_A_CENTER    0x11
  162 #define OUT_A_SUB       0x12
  163 
  164 /* Audigy Inputs */
  165 #define A_IN_AC97_L     0x00
  166 #define A_IN_AC97_R     0x01
  167 #define A_IN_AC97       A_IN_AC97_L
  168 #define A_IN_SPDIF_CD_L 0x02
  169 #define A_IN_SPDIF_CD_R 0x03
  170 #define A_IN_SPDIF_CD   A_IN_SPDIF_CD_L
  171 #define A_IN_O_SPDIF_L  0x04
  172 #define A_IN_O_SPDIF_R  0x05
  173 #define A_IN_O_SPDIF    A_IN_O_SPDIF_L
  174 #define A_IN_LINE2_L    0x08
  175 #define A_IN_LINE2_R    0x09
  176 #define A_IN_LINE2      A_IN_LINE2_L
  177 #define A_IN_R_SPDIF_L  0x0a
  178 #define A_IN_R_SPDIF_R  0x0b
  179 #define A_IN_R_SPDIF    A_IN_R_SPDIF_L
  180 #define A_IN_AUX2_L     0x0c
  181 #define A_IN_AUX2_R     0x0d
  182 #define A_IN_AUX2       A_IN_AUX2_L
  183 
  184 /* Audigiy Outputs */
  185 #define A_OUT_D_FRONT_L 0x00
  186 #define A_OUT_D_FRONT_R 0x01
  187 #define A_OUT_D_FRONT   A_OUT_D_FRONT_L
  188 #define A_OUT_D_CENTER  0x02
  189 #define A_OUT_D_SUB     0x03
  190 #define A_OUT_D_SIDE_L  0x04
  191 #define A_OUT_D_SIDE_R  0x05
  192 #define A_OUT_D_SIDE    A_OUT_D_SIDE_L
  193 #define A_OUT_D_REAR_L  0x06
  194 #define A_OUT_D_REAR_R  0x07
  195 #define A_OUT_D_REAR    A_OUT_D_REAR_L
  196 
  197 /* on Audigy Platinum only */
  198 #define A_OUT_HPHONE_L  0x04
  199 #define A_OUT_HPHONE_R  0x05
  200 #define A_OUT_HPHONE    A_OUT_HPHONE_L
  201 
  202 #define A_OUT_A_FRONT_L 0x08
  203 #define A_OUT_A_FRONT_R 0x09
  204 #define A_OUT_A_FRONT   A_OUT_A_FRONT_L
  205 #define A_OUT_A_CENTER  0x0a
  206 #define A_OUT_A_SUB     0x0b
  207 #define A_OUT_A_SIDE_L  0x0c
  208 #define A_OUT_A_SIDE_R  0x0d
  209 #define A_OUT_A_SIDE    A_OUT_A_SIDE_L
  210 #define A_OUT_A_REAR_L  0x0e
  211 #define A_OUT_A_REAR_R  0x0f
  212 #define A_OUT_A_REAR    A_OUT_A_REAR_L
  213 #define A_OUT_AC97_L    0x10
  214 #define A_OUT_AC97_R    0x11
  215 #define A_OUT_AC97      A_OUT_AC97_L
  216 #define A_OUT_ADC_REC_L 0x16
  217 #define A_OUT_ADC_REC_R 0x17
  218 #define A_OUT_ADC_REC   A_OUT_ADC_REC_L
  219 
  220 #include "emu10k1-alsa%diked.h"
  221 #include "p16v-alsa%diked.h"
  222 #include "p17v-alsa%diked.h"
  223 
  224 #define C_FRONT_L       0
  225 #define C_FRONT_R       1
  226 #define C_REC_L         2
  227 #define C_REC_R         3
  228 #define C_REAR_L        4
  229 #define C_REAR_R        5
  230 #define C_CENTER        6
  231 #define C_SUB           7
  232 #define C_SIDE_L        8
  233 #define C_SIDE_R        9
  234 #define NUM_CACHES      10
  235 
  236 #define CDSPDIFMUTE     0
  237 #define ANALOGMUTE      1
  238 #define NUM_MUTE        2
  239 
  240 #define EMU_MAX_GPR     512
  241 #define EMU_MAX_IRQ_CONSUMERS 32
  242 
  243 struct emu_voice {
  244         int     vnum;
  245         unsigned int    b16:1, stereo:1, busy:1, running:1, ismaster:1;
  246         int     speed;
  247         int     start;
  248         int     end;
  249         int     vol;
  250         uint32_t buf;
  251         void    *vbuf;
  252         struct emu_voice *slave;
  253         uint32_t sa;
  254         uint32_t ea;
  255         uint32_t routing[8];
  256         uint32_t amounts[8];
  257 };
  258 
  259 struct emu_memblk {
  260         SLIST_ENTRY(emu_memblk) link;
  261         void            *buf;
  262         char            owner[16];
  263         bus_addr_t      buf_addr;
  264         uint32_t        pte_start, pte_size;
  265 };
  266 
  267 struct emu_mem {
  268         uint8_t         bmap[EMU_MAXPAGES / 8];
  269         uint32_t        *ptb_pages;
  270         void            *silent_page;
  271         bus_addr_t      silent_page_addr;
  272         bus_addr_t      ptb_pages_addr;
  273         bus_dma_tag_t   dmat;
  274         struct emu_sc_info *card;
  275         SLIST_HEAD(, emu_memblk) blocks;
  276 };
  277 
  278 /* rm */
  279 struct emu_rm {
  280         struct emu_sc_info *card;
  281         struct mtx      gpr_lock;
  282         signed int      allocmap[EMU_MAX_GPR];
  283         int             num_gprs;
  284         int             last_free_gpr;
  285         int             num_used;
  286 };
  287 
  288 struct emu_intr_handler {
  289         void*           softc;
  290         uint32_t        intr_mask;
  291         uint32_t        inte_mask;
  292         uint32_t(*irq_func) (void *softc, uint32_t irq);
  293 };
  294 
  295 struct emu_sc_info {
  296         struct mtx      lock;
  297         struct mtx      rw;             /* Hardware exclusive access lock */
  298 
  299         /* Hardware and subdevices */
  300         device_t        dev;
  301         device_t        pcm[RT_COUNT];
  302         device_t        midi[2];
  303         uint32_t        type;
  304         uint32_t        rev;
  305 
  306         bus_space_tag_t st;
  307         bus_space_handle_t sh;
  308 
  309         struct cdev     *cdev;          /* /dev/emu10k character device */
  310         struct mtx      emu10kx_lock;
  311         int             emu10kx_isopen;
  312         struct sbuf     emu10kx_sbuf;
  313         int             emu10kx_bufptr;
  314 
  315 
  316         /* Resources */
  317         struct resource *reg;
  318         struct resource *irq;
  319         void            *ih;
  320 
  321         /* IRQ handlers */
  322         struct emu_intr_handler ihandler[EMU_MAX_IRQ_CONSUMERS];
  323 
  324         /* Card HW configuration */
  325         unsigned int    mode;   /* analog / digital */
  326         unsigned int    mchannel_fx;
  327         unsigned int    dsp_zero;
  328         unsigned int    code_base;
  329         unsigned int    code_size;
  330         unsigned int    gpr_base;
  331         unsigned int    num_gprs;
  332         unsigned int    input_base;
  333         unsigned int    output_base;
  334         unsigned int    efxc_base;
  335         unsigned int    opcode_shift;
  336         unsigned int    high_operand_shift;
  337         unsigned int    address_mask;
  338         uint32_t        is_emu10k1:1, is_emu10k2, is_ca0102, is_ca0108:1,
  339                         has_ac97:1, has_51:1, has_71:1,
  340                         enable_ir:1,
  341                         broken_digital:1, is_cardbus:1;
  342 
  343         signed int      mch_disabled, mch_rec, dbg_level;
  344         signed int      num_inputs;
  345         unsigned int    num_outputs;
  346         unsigned int    num_fxbuses;
  347         unsigned int    routing_code_start;
  348         unsigned int    routing_code_end;
  349 
  350         /* HW resources */
  351         struct emu_voice voice[NUM_G];                  /* Hardware voices */
  352         uint32_t        irq_mask[EMU_MAX_IRQ_CONSUMERS]; /* IRQ manager data */
  353         int             timer[EMU_MAX_IRQ_CONSUMERS];   /* timer */
  354         int             timerinterval;
  355         struct          emu_rm *rm;
  356         struct          emu_mem mem;                    /* memory */
  357 
  358         /* Mixer */
  359         int             mixer_gpr[NUM_MIXERS];
  360         int             mixer_volcache[NUM_MIXERS];
  361         int             cache_gpr[NUM_CACHES];
  362         int             dummy_gpr;
  363         int             mute_gpr[NUM_MUTE];
  364         struct sysctl_ctx_list  *ctx;
  365         struct sysctl_oid       *root;
  366 };
  367 
  368 static void     emu_setmap(void *arg, bus_dma_segment_t * segs, int nseg, int error);
  369 static void*    emu_malloc(struct emu_mem *mem, uint32_t sz, bus_addr_t * addr);
  370 static void     emu_free(struct emu_mem *mem, void *dmabuf);
  371 static void*    emu_memalloc(struct emu_mem *mem, uint32_t sz, bus_addr_t * addr, const char * owner);
  372 static int      emu_memfree(struct emu_mem *mem, void *membuf);
  373 static int      emu_memstart(struct emu_mem *mem, void *membuf);
  374 
  375 /* /dev */
  376 static int      emu10kx_dev_init(struct emu_sc_info *sc);
  377 static int      emu10kx_dev_uninit(struct emu_sc_info *sc);
  378 static int      emu10kx_prepare(struct emu_sc_info *sc, struct sbuf *s);
  379 
  380 static void     emumix_set_mode(struct emu_sc_info *sc, int mode);
  381 static void     emumix_set_spdif_mode(struct emu_sc_info *sc, int mode);
  382 static void     emumix_set_fxvol(struct emu_sc_info *sc, unsigned gpr, int32_t vol);
  383 static void     emumix_set_gpr(struct emu_sc_info *sc, unsigned gpr, int32_t val);
  384 static int      sysctl_emu_mixer_control(SYSCTL_HANDLER_ARGS);
  385 
  386 static int      emu_rm_init(struct emu_sc_info *sc);
  387 static int      emu_rm_uninit(struct emu_sc_info *sc);
  388 static int      emu_rm_gpr_alloc(struct emu_rm *rm, int count);
  389 
  390 static unsigned int emu_getcard(device_t dev);
  391 static uint32_t emu_rd_nolock(struct emu_sc_info *sc, unsigned int regno, unsigned int size);
  392 static void     emu_wr_nolock(struct emu_sc_info *sc, unsigned int regno, uint32_t data, unsigned int size);
  393 static void     emu_wr_cbptr(struct emu_sc_info *sc, uint32_t data);
  394 
  395 static void     emu_vstop(struct emu_sc_info *sc, char channel, int enable);
  396 
  397 static void     emu_intr(void *p);
  398 static void     emu_wrefx(struct emu_sc_info *sc, unsigned int pc, unsigned int data);
  399 static void     emu_addefxop(struct emu_sc_info *sc, unsigned int op, unsigned int z, unsigned int w, unsigned int x, unsigned int y, uint32_t * pc);
  400 static void     emu_initefx(struct emu_sc_info *sc);
  401 
  402 static int      emu_cardbus_init(struct emu_sc_info *sc);
  403 static int      emu_init(struct emu_sc_info *sc);
  404 static int      emu_uninit(struct emu_sc_info *sc);
  405 
  406 static int      emu_read_ivar(device_t bus __unused, device_t dev, int ivar_index, uintptr_t * result);
  407 static int      emu_write_ivar(device_t bus __unused, device_t dev __unused,
  408     int ivar_index, uintptr_t value __unused);
  409 
  410 static int      emu_pci_probe(device_t dev);
  411 static int      emu_pci_attach(device_t dev);
  412 static int      emu_pci_detach(device_t dev);
  413 static int      emu_modevent(module_t mod __unused, int cmd, void *data __unused);
  414 
  415 #ifdef  SND_EMU10KX_DEBUG
  416 
  417 #define EMU_MTX_DEBUG() do {                                            \
  418                 if (mtx_owned(&sc->rw)) {                               \
  419                 printf("RW owned in %s line %d for %s\n", __func__,     \
  420                         __LINE__ , device_get_nameunit(sc->dev));       \
  421                 printf("rw lock owned: %d\n", mtx_owned(&sc->rw));      \
  422                 printf("rw lock: value %x thread %x\n",                 \
  423                         ((&sc->rw)->mtx_lock & ~MTX_FLAGMASK),          \
  424                         (uintptr_t)curthread);                          \
  425                 printf("rw lock: recursed %d\n", mtx_recursed(&sc->rw));\
  426                 db_show_mtx(&sc->rw);                                   \
  427                 }                                                       \
  428         } while (0)
  429 #else
  430 #define EMU_MTX_DEBUG() do {                                            \
  431         } while (0)
  432 #endif
  433 
  434 #define EMU_RWLOCK() do {               \
  435         EMU_MTX_DEBUG();                \
  436         mtx_lock(&(sc->rw));            \
  437         } while (0)
  438 
  439 #define EMU_RWUNLOCK() do {             \
  440         mtx_unlock(&(sc->rw));          \
  441         EMU_MTX_DEBUG();                \
  442         } while (0)
  443 
  444 /* Supported cards */
  445 struct emu_hwinfo {
  446         uint16_t        vendor;
  447         uint16_t        device;
  448         uint16_t        subvendor;
  449         uint16_t        subdevice;
  450         char            SBcode[8];
  451         char            desc[32];
  452         int             flags;
  453 };
  454 
  455 static struct emu_hwinfo emu_cards[] = {
  456         {0xffff, 0xffff, 0xffff, 0xffff, "BADCRD", "Not a compatible card", 0},
  457         /* 0x0020..0x002f 4.0 EMU10K1 cards */
  458         {0x1102, 0x0002, 0x1102, 0x0020, "CT4850", "SBLive! Value", HAS_AC97 | IS_EMU10K1},
  459         {0x1102, 0x0002, 0x1102, 0x0021, "CT4620", "SBLive!", HAS_AC97 | IS_EMU10K1},
  460         {0x1102, 0x0002, 0x1102, 0x002f, "CT????", "SBLive! mainboard implementation", HAS_AC97 | IS_EMU10K1},
  461 
  462         /* (range unknown) 5.1 EMU10K1 cards */
  463         {0x1102, 0x0002, 0x1102, 0x100a, "CT????", "SBLive! 5.1", HAS_AC97 | HAS_51 | IS_EMU10K1},
  464 
  465         /* 0x80??..0x805? 4.0 EMU10K1 cards */
  466         {0x1102, 0x0002, 0x1102, 0x8022, "CT4780", "SBLive! Value", HAS_AC97 | IS_EMU10K1},
  467         {0x1102, 0x0002, 0x1102, 0x8023, "CT4790", "SB PCI512", HAS_AC97 | IS_EMU10K1},
  468         {0x1102, 0x0002, 0x1102, 0x8024, "CT4760", "SBLive!", HAS_AC97 | IS_EMU10K1},
  469         {0x1102, 0x0002, 0x1102, 0x8025, "CT????", "SBLive! Mainboard Implementation", HAS_AC97 | IS_EMU10K1},
  470         {0x1102, 0x0002, 0x1102, 0x8026, "CT4830", "SBLive! Value", HAS_AC97 | IS_EMU10K1},
  471         {0x1102, 0x0002, 0x1102, 0x8027, "CT4832", "SBLive! Value", HAS_AC97 | IS_EMU10K1},
  472         {0x1102, 0x0002, 0x1102, 0x8028, "CT4760", "SBLive! OEM version", HAS_AC97 | IS_EMU10K1},
  473         {0x1102, 0x0002, 0x1102, 0x8031, "CT4831", "SBLive! Value", HAS_AC97 | IS_EMU10K1},
  474         {0x1102, 0x0002, 0x1102, 0x8040, "CT4760", "SBLive!", HAS_AC97 | IS_EMU10K1},
  475         {0x1102, 0x0002, 0x1102, 0x8051, "CT4850", "SBLive! Value", HAS_AC97 | IS_EMU10K1},
  476 
  477         /* 0x8061..0x???? 5.1 EMU10K1  cards */
  478         {0x1102, 0x0002, 0x1102, 0x8061, "SB????", "SBLive! Player 5.1", HAS_AC97 | HAS_51 | IS_EMU10K1},
  479         {0x1102, 0x0002, 0x1102, 0x8062, "CT4830", "SBLive! 1024", HAS_AC97 | HAS_51 | IS_EMU10K1},
  480         {0x1102, 0x0002, 0x1102, 0x8064, "SB????", "SBLive! 5.1", HAS_AC97 | HAS_51 | IS_EMU10K1},
  481         {0x1102, 0x0002, 0x1102, 0x8065, "SB0220", "SBLive! 5.1 Digital", HAS_AC97 | HAS_51 | IS_EMU10K1},
  482         {0x1102, 0x0002, 0x1102, 0x8066, "CT4780", "SBLive! 5.1 Digital", HAS_AC97 | HAS_51 | IS_EMU10K1},
  483         {0x1102, 0x0002, 0x1102, 0x8067, "SB????", "SBLive!", HAS_AC97 | HAS_51 | IS_EMU10K1},
  484 
  485         /* Generic SB Live! */
  486         {0x1102, 0x0002, 0x1102, 0x0000, "SB????", "SBLive! (Unknown model)", HAS_AC97 | IS_EMU10K1},
  487 
  488         /* 0x0041..0x0043 EMU10K2 (some kind of Audigy) cards */
  489 
  490         /* 0x0051..0x0051 5.1 CA0100-IAF cards */
  491         {0x1102, 0x0004, 0x1102, 0x0051, "SB0090", "Audigy", HAS_AC97 | HAS_51 | IS_EMU10K2},
  492         /* ES is CA0100-IDF chip that don't work in digital mode */
  493         {0x1102, 0x0004, 0x1102, 0x0052, "SB0160", "Audigy ES", HAS_AC97 | HAS_71 | IS_EMU10K2 | BROKEN_DIGITAL},
  494         /* 0x0053..0x005C 5.1 CA0101-NAF cards */
  495         {0x1102, 0x0004, 0x1102, 0x0053, "SB0090", "Audigy Player/OEM", HAS_AC97 | HAS_51 | IS_EMU10K2},
  496         {0x1102, 0x0004, 0x1102, 0x0058, "SB0090", "Audigy Player/OEM", HAS_AC97 | HAS_51 | IS_EMU10K2},
  497 
  498         /* 0x1002..0x1009 5.1 CA0102-IAT cards */
  499         {0x1102, 0x0004, 0x1102, 0x1002, "SB????", "Audigy 2 Platinum", HAS_51 | IS_CA0102},
  500         {0x1102, 0x0004, 0x1102, 0x1005, "SB????", "Audigy 2 Platinum EX", HAS_51 | IS_CA0102},
  501         {0x1102, 0x0004, 0x1102, 0x1007, "SB0240", "Audigy 2", HAS_AC97 | HAS_51 | IS_CA0102},
  502 
  503         /* 0x2001..0x2003 7.1 CA0102-ICT cards */
  504         {0x1102, 0x0004, 0x1102, 0x2001, "SB0350", "Audigy 2 ZS", HAS_AC97 | HAS_71 | IS_CA0102},
  505         {0x1102, 0x0004, 0x1102, 0x2002, "SB0350", "Audigy 2 ZS", HAS_AC97 | HAS_71 | IS_CA0102},
  506         /* XXX No reports about 0x2003 & 0x2004 cards */
  507         {0x1102, 0x0004, 0x1102, 0x2003, "SB0350", "Audigy 2 ZS", HAS_AC97 | HAS_71 | IS_CA0102},
  508         {0x1102, 0x0004, 0x1102, 0x2004, "SB0350", "Audigy 2 ZS", HAS_AC97 | HAS_71 | IS_CA0102},
  509         {0x1102, 0x0004, 0x1102, 0x2005, "SB0350", "Audigy 2 ZS", HAS_AC97 | HAS_71 | IS_CA0102},
  510 
  511         /* (range unknown) 7.1 CA0102-xxx Audigy 4 cards */
  512         {0x1102, 0x0004, 0x1102, 0x2007, "SB0380", "Audigy 4 Pro", HAS_AC97 | HAS_71 | IS_CA0102},
  513 
  514         /* Generic Audigy or Audigy 2 */
  515         {0x1102, 0x0004, 0x1102, 0x0000, "SB????", "Audigy (Unknown model)", HAS_AC97 | HAS_51 | IS_EMU10K2},
  516 
  517         /* We don't support CA0103-DAT (Audigy LS) cards */
  518         /* There is NO CA0104-xxx cards */
  519         /* There is NO CA0105-xxx cards */
  520         /* We don't support CA0106-DAT (SB Live! 24 bit) cards */
  521         /* There is NO CA0107-xxx cards */
  522 
  523         /* 0x1000..0x1001 7.1 CA0108-IAT cards */
  524         {0x1102, 0x0008, 0x1102, 0x1000, "SB????", "Audigy 2 LS", HAS_AC97 | HAS_51 | IS_CA0108 | DIGITAL_ONLY},
  525         {0x1102, 0x0008, 0x1102, 0x1001, "SB0400", "Audigy 2 Value", HAS_AC97 | HAS_71 | IS_CA0108 | DIGITAL_ONLY},
  526         {0x1102, 0x0008, 0x1102, 0x1021, "SB0610", "Audigy 4", HAS_AC97 | HAS_71 | IS_CA0108 | DIGITAL_ONLY},
  527 
  528         {0x1102, 0x0008, 0x1102, 0x2001, "SB0530", "Audigy 2 ZS CardBus", HAS_AC97 | HAS_71 | IS_CA0108 | IS_CARDBUS},
  529 
  530         {0x1102, 0x0008, 0x0000, 0x0000, "SB????", "Audigy 2 Value (Unknown model)", HAS_AC97 | HAS_51 | IS_CA0108},
  531 };
  532 /* Unsupported cards */
  533 
  534 static struct emu_hwinfo emu_bad_cards[] = {
  535         /* APS cards should be possible to support */
  536         {0x1102, 0x0002, 0x1102, 0x4001, "EMUAPS", "E-mu APS", 0},
  537         {0x1102, 0x0002, 0x1102, 0x4002, "EMUAPS", "E-mu APS", 0},
  538         {0x1102, 0x0004, 0x1102, 0x4001, "EMU???", "E-mu 1212m [4001]", 0},
  539         /* Similar-named ("Live!" or "Audigy") cards on different chipsets */
  540         {0x1102, 0x8064, 0x0000, 0x0000, "SB0100", "SBLive! 5.1 OEM", 0},
  541         {0x1102, 0x0006, 0x0000, 0x0000, "SB0200", "DELL OEM SBLive! Value", 0},
  542         {0x1102, 0x0007, 0x0000, 0x0000, "SB0310", "Audigy LS", 0},
  543 };
  544 
  545 /*
  546  * Get best known information about device.
  547  */
  548 static unsigned int
  549 emu_getcard(device_t dev)
  550 {
  551         uint16_t device;
  552         uint16_t subdevice;
  553         int n_cards;
  554         unsigned int thiscard;
  555         int i;
  556 
  557         device = pci_read_config(dev, PCIR_DEVICE, /* bytes */ 2);
  558         subdevice = pci_read_config(dev, PCIR_SUBDEV_0, /* bytes */ 2);
  559 
  560         n_cards = sizeof(emu_cards) / sizeof(struct emu_hwinfo);
  561         thiscard = 0;
  562         for (i = 1; i < n_cards; i++) {
  563                 if (device == emu_cards[i].device) {
  564                         if (subdevice == emu_cards[i].subdevice) {
  565                                 thiscard = i;
  566                                 break;
  567                         }
  568                         if (0x0000 == emu_cards[i].subdevice) {
  569                                 thiscard = i;
  570                                 /*
  571                                  * don't break, we can get more specific card
  572                                  * later in the list.
  573                                  */
  574                         }
  575                 }
  576         }
  577 
  578         n_cards = sizeof(emu_bad_cards) / sizeof(struct emu_hwinfo);
  579         for (i = 0; i < n_cards; i++) {
  580                 if (device == emu_bad_cards[i].device) {
  581                         if (subdevice == emu_bad_cards[i].subdevice) {
  582                                 thiscard = 0;
  583                                 break;
  584                         }
  585                         if (0x0000 == emu_bad_cards[i].subdevice) {
  586                                 thiscard = 0;
  587                                 break;  /* we avoid all this cards */
  588                         }
  589                 }
  590         }
  591         return (thiscard);
  592 }
  593 
  594 
  595 /*
  596  * Base hardware interface are 32 (Audigy) or 64 (Audigy2) registers.
  597  * Some of them are used directly, some of them provide pointer / data pairs.
  598  */
  599 static uint32_t
  600 emu_rd_nolock(struct emu_sc_info *sc, unsigned int regno, unsigned int size)
  601 {
  602 
  603         KASSERT(sc != NULL, ("emu_rd: NULL sc"));
  604         switch (size) {
  605         case 1:
  606                 return (bus_space_read_1(sc->st, sc->sh, regno));
  607         case 2:
  608                 return (bus_space_read_2(sc->st, sc->sh, regno));
  609         case 4:
  610                 return (bus_space_read_4(sc->st, sc->sh, regno));
  611         }
  612         return (0xffffffff);
  613 }
  614 
  615 static void
  616 emu_wr_nolock(struct emu_sc_info *sc, unsigned int regno, uint32_t data, unsigned int size)
  617 {
  618 
  619         KASSERT(sc != NULL, ("emu_rd: NULL sc"));
  620         switch (size) {
  621         case 1:
  622                 bus_space_write_1(sc->st, sc->sh, regno, data);
  623                 break;
  624         case 2:
  625                 bus_space_write_2(sc->st, sc->sh, regno, data);
  626                 break;
  627         case 4:
  628                 bus_space_write_4(sc->st, sc->sh, regno, data);
  629                 break;
  630         }
  631 }
  632 /*
  633  * PTR / DATA interface. Access to EMU10Kx is made
  634  * via (channel, register) pair. Some registers are channel-specific,
  635  * some not.
  636  */
  637 uint32_t
  638 emu_rdptr(struct emu_sc_info *sc, unsigned int chn, unsigned int reg)
  639 {
  640         uint32_t ptr, val, mask, size, offset;
  641 
  642         ptr = ((reg << 16) & sc->address_mask) | (chn & PTR_CHANNELNUM_MASK);
  643 
  644         EMU_RWLOCK();
  645         emu_wr_nolock(sc, PTR, ptr, 4);
  646         val = emu_rd_nolock(sc, DATA, 4);
  647         EMU_RWUNLOCK();
  648 
  649         /*
  650          * XXX Some register numbers has data size and offset encoded in
  651          * it to get only part of 32bit register. This use is not described
  652          * in register name, be careful!
  653          */
  654         if (reg & 0xff000000) {
  655                 size = (reg >> 24) & 0x3f;
  656                 offset = (reg >> 16) & 0x1f;
  657                 mask = ((1 << size) - 1) << offset;
  658                 val &= mask;
  659                 val >>= offset;
  660         }
  661         return (val);
  662 }
  663 
  664 void
  665 emu_wrptr(struct emu_sc_info *sc, unsigned int chn, unsigned int reg, uint32_t data)
  666 {
  667         uint32_t ptr, mask, size, offset;
  668 
  669         ptr = ((reg << 16) & sc->address_mask) | (chn & PTR_CHANNELNUM_MASK);
  670 
  671         EMU_RWLOCK();
  672         emu_wr_nolock(sc, PTR, ptr, 4);
  673         /*
  674          * XXX Another kind of magic encoding in register number. This can
  675          * give you side effect - it will read previous data from register
  676          * and change only required bits.
  677          */
  678         if (reg & 0xff000000) {
  679                 size = (reg >> 24) & 0x3f;
  680                 offset = (reg >> 16) & 0x1f;
  681                 mask = ((1 << size) - 1) << offset;
  682                 data <<= offset;
  683                 data &= mask;
  684                 data |= emu_rd_nolock(sc, DATA, 4) & ~mask;
  685         }
  686         emu_wr_nolock(sc, DATA, data, 4);
  687         EMU_RWUNLOCK();
  688 }
  689 /*
  690  * PTR2 / DATA2 interface. Access to P16v is made
  691  * via (channel, register) pair. Some registers are channel-specific,
  692  * some not. This interface is supported by CA0102 and CA0108 chips only.
  693  */
  694 uint32_t
  695 emu_rd_p16vptr(struct emu_sc_info *sc, uint16_t chn, uint16_t reg)
  696 {
  697         uint32_t val;
  698 
  699         /* XXX separate lock? */
  700         EMU_RWLOCK();
  701         emu_wr_nolock(sc, PTR2, (reg << 16) | chn, 4);
  702         val = emu_rd_nolock(sc, DATA2, 4);
  703 
  704         EMU_RWUNLOCK();
  705 
  706         return (val);
  707 }
  708 
  709 void
  710 emu_wr_p16vptr(struct emu_sc_info *sc, uint16_t chn, uint16_t reg, uint32_t data)
  711 {
  712 
  713         EMU_RWLOCK();
  714         emu_wr_nolock(sc, PTR2, (reg << 16) | chn, 4);
  715         emu_wr_nolock(sc, DATA2, data, 4);
  716         EMU_RWUNLOCK();
  717 }
  718 /*
  719  * XXX CardBus interface. Not tested on any real hardware.
  720  */
  721 static void
  722 emu_wr_cbptr(struct emu_sc_info *sc, uint32_t data)
  723 {
  724         uint32_t val;
  725 
  726         /*
  727          * 0x38 is IPE3 (CD S/PDIF interrupt pending register) on CA0102. Seems
  728          * to be some reg/value accessible kind of config register on CardBus
  729          * CA0108, with value(?) in top 16 bit, address(?) in low 16
  730          */
  731 
  732         val = emu_rd_nolock(sc, 0x38, 4);
  733         emu_wr_nolock(sc, 0x38, data, 4);
  734         val = emu_rd_nolock(sc, 0x38, 4);
  735 
  736 }
  737 
  738 /*
  739  * Direct hardware register access
  740  * Assume that it is never used to access PTR-based registers and can run unlocked.
  741  */
  742 void
  743 emu_wr(struct emu_sc_info *sc, unsigned int regno, uint32_t data, unsigned int size)
  744 {
  745         KASSERT(regno != PTR, ("emu_wr: attempt to write to PTR"));
  746         KASSERT(regno != PTR2, ("emu_wr: attempt to write to PTR2"));
  747 
  748         emu_wr_nolock(sc, regno, data, size);
  749 }
  750 
  751 uint32_t
  752 emu_rd(struct emu_sc_info *sc, unsigned int regno, unsigned int size)
  753 {
  754         uint32_t rd;
  755 
  756         KASSERT(regno != DATA, ("emu_rd: attempt to read DATA"));
  757         KASSERT(regno != DATA2, ("emu_rd: attempt to read DATA2"));
  758 
  759         rd = emu_rd_nolock(sc, regno, size);
  760         return (rd);
  761 }
  762 
  763 /*
  764  * Enabling IR MIDI messages is another kind of black magic. It just
  765  * has to be made this way. It really do it.
  766  */
  767 void
  768 emu_enable_ir(struct emu_sc_info *sc)
  769 {
  770         uint32_t iocfg;
  771 
  772         if (sc->is_emu10k2 || sc->is_ca0102) {
  773                 iocfg = emu_rd_nolock(sc, A_IOCFG, 2);
  774                 emu_wr_nolock(sc, A_IOCFG, iocfg | A_IOCFG_GPOUT2, 2);
  775                 DELAY(500);
  776                 emu_wr_nolock(sc, A_IOCFG, iocfg | A_IOCFG_GPOUT1 | A_IOCFG_GPOUT2, 2);
  777                 DELAY(500);
  778                 emu_wr_nolock(sc, A_IOCFG, iocfg | A_IOCFG_GPOUT1, 2);
  779                 DELAY(100);
  780                 emu_wr_nolock(sc, A_IOCFG, iocfg, 2);
  781                 device_printf(sc->dev, "Audigy IR MIDI events enabled.\n");
  782                 sc->enable_ir = 1;
  783         }
  784         if (sc->is_emu10k1) {
  785                 iocfg = emu_rd_nolock(sc, HCFG, 4);
  786                 emu_wr_nolock(sc, HCFG, iocfg | HCFG_GPOUT2, 4);
  787                 DELAY(500);
  788                 emu_wr_nolock(sc, HCFG, iocfg | HCFG_GPOUT1 | HCFG_GPOUT2, 4);
  789                 DELAY(100);
  790                 emu_wr_nolock(sc, HCFG, iocfg, 4);
  791                 device_printf(sc->dev, "SB Live! IR MIDI events enabled.\n");
  792                 sc->enable_ir = 1;
  793         }
  794 }
  795 
  796 
  797 /*
  798  * emu_timer_ - HW timer managment
  799  */
  800 int
  801 emu_timer_create(struct emu_sc_info *sc)
  802 {
  803         int i, timer;
  804 
  805         timer = -1;
  806 
  807         mtx_lock(&sc->lock);
  808         for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++)
  809                 if (sc->timer[i] == 0) {
  810                         sc->timer[i] = -1;      /* disable it */
  811                         timer = i;
  812                         mtx_unlock(&sc->lock);
  813                         return (timer);
  814                 }
  815         mtx_unlock(&sc->lock);
  816 
  817         return (-1);
  818 }
  819 
  820 int
  821 emu_timer_set(struct emu_sc_info *sc, int timer, int delay)
  822 {
  823         int i;
  824 
  825         if (timer < 0)
  826                 return (-1);
  827 
  828         RANGE(delay, 16, 1024);
  829         RANGE(timer, 0, EMU_MAX_IRQ_CONSUMERS-1);
  830 
  831         mtx_lock(&sc->lock);
  832         sc->timer[timer] = delay;
  833         for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++)
  834                 if (sc->timerinterval > sc->timer[i])
  835                         sc->timerinterval = sc->timer[i];
  836 
  837         /* XXX */
  838         emu_wr(sc, TIMER, sc->timerinterval & 0x03ff, 2);
  839         mtx_unlock(&sc->lock);
  840 
  841         return (timer);
  842 }
  843 
  844 int
  845 emu_timer_enable(struct emu_sc_info *sc, int timer, int go)
  846 {
  847         uint32_t x;
  848         int ena_int;
  849         int i;
  850 
  851         if (timer < 0)
  852                 return (-1);
  853 
  854         RANGE(timer, 0, EMU_MAX_IRQ_CONSUMERS-1);
  855 
  856         mtx_lock(&sc->lock);
  857 
  858         if ((go == 1) && (sc->timer[timer] < 0))
  859                 sc->timer[timer] = -sc->timer[timer];
  860         if ((go == 0) && (sc->timer[timer] > 0))
  861                 sc->timer[timer] = -sc->timer[timer];
  862 
  863         ena_int = 0;
  864         for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++) {
  865                 if (sc->timerinterval > sc->timer[i])
  866                         sc->timerinterval = sc->timer[i];
  867                 if (sc->timer[i] > 0)
  868                         ena_int = 1;
  869         }
  870 
  871         emu_wr(sc, TIMER, sc->timerinterval & 0x03ff, 2);
  872 
  873         if (ena_int == 1) {
  874                 x = emu_rd(sc, INTE, 4);
  875                 x |= INTE_INTERVALTIMERENB;
  876                 emu_wr(sc, INTE, x, 4);
  877         } else {
  878                 x = emu_rd(sc, INTE, 4);
  879                 x &= ~INTE_INTERVALTIMERENB;
  880                 emu_wr(sc, INTE, x, 4);
  881         }
  882         mtx_unlock(&sc->lock);
  883         return (0);
  884 }
  885 
  886 int
  887 emu_timer_clear(struct emu_sc_info *sc, int timer)
  888 {
  889         if (timer < 0)
  890                 return (-1);
  891 
  892         RANGE(timer, 0, EMU_MAX_IRQ_CONSUMERS-1);
  893 
  894         emu_timer_enable(sc, timer, 0);
  895 
  896         mtx_lock(&sc->lock);
  897         if (sc->timer[timer] != 0)
  898                 sc->timer[timer] = 0;
  899         mtx_unlock(&sc->lock);
  900 
  901         return (timer);
  902 }
  903 
  904 /*
  905  * emu_intr_ - HW interrupt handler managment
  906  */
  907 int
  908 emu_intr_register(struct emu_sc_info *sc, uint32_t inte_mask, uint32_t intr_mask, uint32_t(*func) (void *softc, uint32_t irq), void *isc)
  909 {
  910         int i;
  911         uint32_t x;
  912 
  913         mtx_lock(&sc->lock);
  914         for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++)
  915                 if (sc->ihandler[i].inte_mask == 0) {
  916                         sc->ihandler[i].inte_mask = inte_mask;
  917                         sc->ihandler[i].intr_mask = intr_mask;
  918                         sc->ihandler[i].softc = isc;
  919                         sc->ihandler[i].irq_func = func;
  920                         x = emu_rd(sc, INTE, 4);
  921                         x |= inte_mask;
  922                         emu_wr(sc, INTE, x, 4);
  923                         mtx_unlock(&sc->lock);
  924                         if (sc->dbg_level > 1)
  925                                 device_printf(sc->dev, "ihandle %d registered\n", i);
  926 
  927                         return (i);
  928                 }
  929         mtx_unlock(&sc->lock);
  930         if (sc->dbg_level > 1)
  931                 device_printf(sc->dev, "ihandle not registered\n");
  932 
  933         return (-1);
  934 }
  935 
  936 int
  937 emu_intr_unregister(struct emu_sc_info *sc, int hnumber)
  938 {
  939         uint32_t x;
  940         int i;
  941 
  942         mtx_lock(&sc->lock);
  943 
  944         if (sc->ihandler[hnumber].inte_mask == 0) {
  945                 mtx_unlock(&sc->lock);
  946                 return (-1);
  947         }
  948 
  949         x = emu_rd(sc, INTE, 4);
  950         x &= ~sc->ihandler[hnumber].inte_mask;
  951 
  952         sc->ihandler[hnumber].inte_mask = 0;
  953         sc->ihandler[hnumber].intr_mask = 0;
  954         sc->ihandler[hnumber].softc = NULL;
  955         sc->ihandler[hnumber].irq_func = NULL;
  956 
  957         /* other interrupt handlers may use this INTE value */
  958         for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++)
  959                 if (sc->ihandler[i].inte_mask != 0)
  960                         x |= sc->ihandler[i].inte_mask;
  961 
  962         emu_wr(sc, INTE, x, 4);
  963 
  964         mtx_unlock(&sc->lock);
  965         return (hnumber);
  966 }
  967 
  968 static void
  969 emu_intr(void *p)
  970 {
  971         struct emu_sc_info *sc = (struct emu_sc_info *)p;
  972         uint32_t stat, ack;
  973         int i;
  974 
  975         for (;;) {
  976                 stat = emu_rd(sc, IPR, 4);
  977                 ack = 0;
  978                 if (stat == 0)
  979                         break;
  980                 emu_wr(sc, IPR, stat, 4);
  981                 for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++) {
  982                         if ((((sc->ihandler[i].intr_mask) & stat) != 0) &&
  983                             (((void *)sc->ihandler[i].irq_func) != NULL)) {
  984                                 ack |= sc->ihandler[i].irq_func(sc->ihandler[i].softc,
  985                                     (sc->ihandler[i].intr_mask) & stat);
  986                         }
  987                 }
  988         if (sc->dbg_level > 1)
  989                 if (stat & (~ack))
  990                         device_printf(sc->dev, "Unhandled interrupt: %08x\n", stat & (~ack));
  991 
  992         }
  993 
  994         if ((sc->is_ca0102) || (sc->is_ca0108))
  995                 for (;;) {
  996                         stat = emu_rd(sc, IPR2, 4);
  997                         ack = 0;
  998                         if (stat == 0)
  999                                 break;
 1000                         emu_wr(sc, IPR2, stat, 4);
 1001                         if (sc->dbg_level > 1)
 1002                                 device_printf(sc->dev, "IPR2: %08x\n", stat);
 1003 
 1004                         break;  /* to avoid infinite loop. shoud be removed
 1005                                  * after completion of P16V interface. */
 1006                 }
 1007 
 1008         if (sc->is_ca0102)
 1009                 for (;;) {
 1010                         stat = emu_rd(sc, IPR3, 4);
 1011                         ack = 0;
 1012                         if (stat == 0)
 1013                                 break;
 1014                         emu_wr(sc, IPR3, stat, 4);
 1015                         if (sc->dbg_level > 1)
 1016                                 device_printf(sc->dev, "IPR3: %08x\n", stat);
 1017 
 1018                         break;  /* to avoid infinite loop. should be removed
 1019                                  * after completion of S/PDIF interface */
 1020                 }
 1021 }
 1022 
 1023 
 1024 /*
 1025  * Get data from private emu10kx structure for PCM buffer allocation.
 1026  * Used by PCM code only.
 1027  */
 1028 bus_dma_tag_t
 1029 emu_gettag(struct emu_sc_info *sc)
 1030 {
 1031         return (sc->mem.dmat);
 1032 }
 1033 
 1034 static void
 1035 emu_setmap(void *arg, bus_dma_segment_t * segs, int nseg, int error)
 1036 {
 1037         bus_addr_t *phys = (bus_addr_t *) arg;
 1038 
 1039         *phys = error ? 0 : (bus_addr_t) segs->ds_addr;
 1040 
 1041         if (bootverbose) {
 1042                 printf("emu10kx: setmap (%lx, %lx), nseg=%d, error=%d\n",
 1043                     (unsigned long)segs->ds_addr, (unsigned long)segs->ds_len,
 1044                     nseg, error);
 1045         }
 1046 }
 1047 
 1048 static void *
 1049 emu_malloc(struct emu_mem *mem, uint32_t sz, bus_addr_t * addr)
 1050 {
 1051         void *dmabuf;
 1052         bus_dmamap_t map;
 1053         int error;
 1054 
 1055         *addr = 0;
 1056         if ((error = bus_dmamem_alloc(mem->dmat, &dmabuf, BUS_DMA_NOWAIT, &map))) {
 1057                 if (mem->card->dbg_level > 2)
 1058                         device_printf(mem->card->dev, "emu_malloc: failed to alloc DMA map: %d\n", error);
 1059                 return (NULL);
 1060                 }
 1061         if ((error = bus_dmamap_load(mem->dmat, map, dmabuf, sz, emu_setmap, addr, 0)) || !*addr) {
 1062                 if (mem->card->dbg_level > 2)
 1063                         device_printf(mem->card->dev, "emu_malloc: failed to load DMA memory: %d\n", error);
 1064                 return (NULL);
 1065                 }
 1066         return (dmabuf);
 1067 }
 1068 
 1069 static void
 1070 emu_free(struct emu_mem *mem, void *dmabuf)
 1071 {
 1072         bus_dmamem_free(mem->dmat, dmabuf, NULL);
 1073 }
 1074 
 1075 static void *
 1076 emu_memalloc(struct emu_mem *mem, uint32_t sz, bus_addr_t * addr, const char *owner)
 1077 {
 1078         uint32_t blksz, start, idx, ofs, tmp, found;
 1079         struct emu_memblk *blk;
 1080         void *membuf;
 1081 
 1082         blksz = sz / EMUPAGESIZE;
 1083         if (sz > (blksz * EMUPAGESIZE))
 1084                 blksz++;
 1085         if (blksz > EMU_MAX_BUFSZ / EMUPAGESIZE) {
 1086                 if (mem->card->dbg_level > 2)
 1087                         device_printf(mem->card->dev, "emu_memalloc: memory request tool large\n");
 1088                 return (NULL);
 1089                 }
 1090         /* find a free block in the bitmap */
 1091         found = 0;
 1092         start = 1;
 1093         while (!found && start + blksz < EMU_MAXPAGES) {
 1094                 found = 1;
 1095                 for (idx = start; idx < start + blksz; idx++)
 1096                         if (mem->bmap[idx >> 3] & (1 << (idx & 7)))
 1097                                 found = 0;
 1098                 if (!found)
 1099                         start++;
 1100         }
 1101         if (!found) {
 1102                 if (mem->card->dbg_level > 2)
 1103                         device_printf(mem->card->dev, "emu_memalloc: no free space in bitmap\n");
 1104                 return (NULL);
 1105                 }
 1106         blk = malloc(sizeof(*blk), M_DEVBUF, M_NOWAIT);
 1107         if (blk == NULL) {
 1108                 if (mem->card->dbg_level > 2)
 1109                         device_printf(mem->card->dev, "emu_memalloc: buffer allocation failed\n");
 1110                 return (NULL);
 1111                 }
 1112         bzero(blk, sizeof(*blk));
 1113         membuf = emu_malloc(mem, sz, &blk->buf_addr);
 1114         *addr = blk->buf_addr;
 1115         if (membuf == NULL) {
 1116                 if (mem->card->dbg_level > 2)
 1117                         device_printf(mem->card->dev, "emu_memalloc: can't setup HW memory\n");
 1118                 free(blk, M_DEVBUF);
 1119                 return (NULL);
 1120         }
 1121         blk->buf = membuf;
 1122         blk->pte_start = start;
 1123         blk->pte_size = blksz;
 1124         strncpy(blk->owner, owner, 15);
 1125         blk->owner[15] = '\0';
 1126         ofs = 0;
 1127         for (idx = start; idx < start + blksz; idx++) {
 1128                 mem->bmap[idx >> 3] |= 1 << (idx & 7);
 1129                 tmp = (uint32_t) (u_long) ((uint8_t *) blk->buf_addr + ofs);
 1130                 mem->ptb_pages[idx] = (tmp << 1) | idx;
 1131                 ofs += EMUPAGESIZE;
 1132         }
 1133         SLIST_INSERT_HEAD(&mem->blocks, blk, link);
 1134         return (membuf);
 1135 }
 1136 
 1137 static int
 1138 emu_memfree(struct emu_mem *mem, void *membuf)
 1139 {
 1140         uint32_t idx, tmp;
 1141         struct emu_memblk *blk, *i;
 1142 
 1143         blk = NULL;
 1144         SLIST_FOREACH(i, &mem->blocks, link) {
 1145                 if (i->buf == membuf)
 1146                         blk = i;
 1147         }
 1148         if (blk == NULL)
 1149                 return (EINVAL);
 1150         SLIST_REMOVE(&mem->blocks, blk, emu_memblk, link);
 1151         emu_free(mem, membuf);
 1152         tmp = (uint32_t) (mem->silent_page_addr) << 1;
 1153         for (idx = blk->pte_start; idx < blk->pte_start + blk->pte_size; idx++) {
 1154                 mem->bmap[idx >> 3] &= ~(1 << (idx & 7));
 1155                 mem->ptb_pages[idx] = tmp | idx;
 1156         }
 1157         free(blk, M_DEVBUF);
 1158         return (0);
 1159 }
 1160 
 1161 static int
 1162 emu_memstart(struct emu_mem *mem, void *membuf)
 1163 {
 1164         struct emu_memblk *blk, *i;
 1165 
 1166         blk = NULL;
 1167         SLIST_FOREACH(i, &mem->blocks, link) {
 1168                 if (i->buf == membuf)
 1169                         blk = i;
 1170         }
 1171         if (blk == NULL)
 1172                 return (-1);
 1173         return (blk->pte_start);
 1174 }
 1175 
 1176 
 1177 static uint32_t
 1178 emu_rate_to_pitch(uint32_t rate)
 1179 {
 1180         static uint32_t logMagTable[128] = {
 1181                 0x00000, 0x02dfc, 0x05b9e, 0x088e6, 0x0b5d6, 0x0e26f, 0x10eb3, 0x13aa2,
 1182                 0x1663f, 0x1918a, 0x1bc84, 0x1e72e, 0x2118b, 0x23b9a, 0x2655d, 0x28ed5,
 1183                 0x2b803, 0x2e0e8, 0x30985, 0x331db, 0x359eb, 0x381b6, 0x3a93d, 0x3d081,
 1184                 0x3f782, 0x41e42, 0x444c1, 0x46b01, 0x49101, 0x4b6c4, 0x4dc49, 0x50191,
 1185                 0x5269e, 0x54b6f, 0x57006, 0x59463, 0x5b888, 0x5dc74, 0x60029, 0x623a7,
 1186                 0x646ee, 0x66a00, 0x68cdd, 0x6af86, 0x6d1fa, 0x6f43c, 0x7164b, 0x73829,
 1187                 0x759d4, 0x77b4f, 0x79c9a, 0x7bdb5, 0x7dea1, 0x7ff5e, 0x81fed, 0x8404e,
 1188                 0x86082, 0x88089, 0x8a064, 0x8c014, 0x8df98, 0x8fef1, 0x91e20, 0x93d26,
 1189                 0x95c01, 0x97ab4, 0x9993e, 0x9b79f, 0x9d5d9, 0x9f3ec, 0xa11d8, 0xa2f9d,
 1190                 0xa4d3c, 0xa6ab5, 0xa8808, 0xaa537, 0xac241, 0xadf26, 0xafbe7, 0xb1885,
 1191                 0xb3500, 0xb5157, 0xb6d8c, 0xb899f, 0xba58f, 0xbc15e, 0xbdd0c, 0xbf899,
 1192                 0xc1404, 0xc2f50, 0xc4a7b, 0xc6587, 0xc8073, 0xc9b3f, 0xcb5ed, 0xcd07c,
 1193                 0xceaec, 0xd053f, 0xd1f73, 0xd398a, 0xd5384, 0xd6d60, 0xd8720, 0xda0c3,
 1194                 0xdba4a, 0xdd3b4, 0xded03, 0xe0636, 0xe1f4e, 0xe384a, 0xe512c, 0xe69f3,
 1195                 0xe829f, 0xe9b31, 0xeb3a9, 0xecc08, 0xee44c, 0xefc78, 0xf148a, 0xf2c83,
 1196                 0xf4463, 0xf5c2a, 0xf73da, 0xf8b71, 0xfa2f0, 0xfba57, 0xfd1a7, 0xfe8df
 1197         };
 1198         static char logSlopeTable[128] = {
 1199                 0x5c, 0x5c, 0x5b, 0x5a, 0x5a, 0x59, 0x58, 0x58,
 1200                 0x57, 0x56, 0x56, 0x55, 0x55, 0x54, 0x53, 0x53,
 1201                 0x52, 0x52, 0x51, 0x51, 0x50, 0x50, 0x4f, 0x4f,
 1202                 0x4e, 0x4d, 0x4d, 0x4d, 0x4c, 0x4c, 0x4b, 0x4b,
 1203                 0x4a, 0x4a, 0x49, 0x49, 0x48, 0x48, 0x47, 0x47,
 1204                 0x47, 0x46, 0x46, 0x45, 0x45, 0x45, 0x44, 0x44,
 1205                 0x43, 0x43, 0x43, 0x42, 0x42, 0x42, 0x41, 0x41,
 1206                 0x41, 0x40, 0x40, 0x40, 0x3f, 0x3f, 0x3f, 0x3e,
 1207                 0x3e, 0x3e, 0x3d, 0x3d, 0x3d, 0x3c, 0x3c, 0x3c,
 1208                 0x3b, 0x3b, 0x3b, 0x3b, 0x3a, 0x3a, 0x3a, 0x39,
 1209                 0x39, 0x39, 0x39, 0x38, 0x38, 0x38, 0x38, 0x37,
 1210                 0x37, 0x37, 0x37, 0x36, 0x36, 0x36, 0x36, 0x35,
 1211                 0x35, 0x35, 0x35, 0x34, 0x34, 0x34, 0x34, 0x34,
 1212                 0x33, 0x33, 0x33, 0x33, 0x32, 0x32, 0x32, 0x32,
 1213                 0x32, 0x31, 0x31, 0x31, 0x31, 0x31, 0x30, 0x30,
 1214                 0x30, 0x30, 0x30, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f
 1215         };
 1216         int i;
 1217 
 1218         if (rate == 0)
 1219                 return (0);
 1220         rate *= 11185;          /* Scale 48000 to 0x20002380 */
 1221         for (i = 31; i > 0; i--) {
 1222                 if (rate & 0x80000000) {        /* Detect leading "1" */
 1223                         return (((uint32_t) (i - 15) << 20) +
 1224                             logMagTable[0x7f & (rate >> 24)] +
 1225                             (0x7f & (rate >> 17)) *
 1226                             logSlopeTable[0x7f & (rate >> 24)]);
 1227                 }
 1228                 rate <<= 1;
 1229         }
 1230         /* NOTREACHED */
 1231         return (0);
 1232 }
 1233 
 1234 static uint32_t
 1235 emu_rate_to_linearpitch(uint32_t rate)
 1236 {
 1237         rate = (rate << 8) / 375;
 1238         return ((rate >> 1) + (rate & 1));
 1239 }
 1240 
 1241 struct emu_voice *
 1242 emu_valloc(struct emu_sc_info *sc)
 1243 {
 1244         struct emu_voice *v;
 1245         int i;
 1246 
 1247         v = NULL;
 1248         mtx_lock(&sc->lock);
 1249         for (i = 0; i < NUM_G && sc->voice[i].busy; i++);
 1250         if (i < NUM_G) {
 1251                 v = &sc->voice[i];
 1252                 v->busy = 1;
 1253         }
 1254         mtx_unlock(&sc->lock);
 1255         return (v);
 1256 }
 1257 
 1258 void
 1259 emu_vfree(struct emu_sc_info *sc, struct emu_voice *v)
 1260 {
 1261         int i, r;
 1262 
 1263         mtx_lock(&sc->lock);
 1264         for (i = 0; i < NUM_G; i++) {
 1265                 if (v == &sc->voice[i] && sc->voice[i].busy) {
 1266                         v->busy = 0;
 1267                         /*
 1268                          * XXX What we should do with mono channels?
 1269                          * See -pcm.c emupchan_init for other side of
 1270                          * this problem
 1271                          */
 1272                         if (v->slave != NULL)
 1273                                 r = emu_memfree(&sc->mem, v->vbuf);
 1274                 }
 1275         }
 1276         mtx_unlock(&sc->lock);
 1277 }
 1278 
 1279 int
 1280 emu_vinit(struct emu_sc_info *sc, struct emu_voice *m, struct emu_voice *s,
 1281     uint32_t sz, struct snd_dbuf *b)
 1282 {
 1283         void *vbuf;
 1284         bus_addr_t tmp_addr;
 1285 
 1286         vbuf = emu_memalloc(&sc->mem, sz, &tmp_addr, "vinit");
 1287         if (vbuf == NULL) {
 1288                 if(sc->dbg_level > 2)
 1289                         device_printf(sc->dev, "emu_memalloc returns NULL in enu_vinit\n");
 1290                 return (ENOMEM);
 1291                 }
 1292         if (b != NULL)
 1293                 sndbuf_setup(b, vbuf, sz);
 1294         m->start = emu_memstart(&sc->mem, vbuf) * EMUPAGESIZE;
 1295         if (m->start < 0) {
 1296                 if(sc->dbg_level > 2)
 1297                         device_printf(sc->dev, "emu_memstart returns (-1) in enu_vinit\n");
 1298                 emu_memfree(&sc->mem, vbuf);
 1299                 return (ENOMEM);
 1300         }
 1301         m->end = m->start + sz;
 1302         m->speed = 0;
 1303         m->b16 = 0;
 1304         m->stereo = 0;
 1305         m->running = 0;
 1306         m->ismaster = 1;
 1307         m->vol = 0xff;
 1308         m->buf = tmp_addr;
 1309         m->vbuf = vbuf;
 1310         m->slave = s;
 1311         if (s != NULL) {
 1312                 s->start = m->start;
 1313                 s->end = m->end;
 1314                 s->speed = 0;
 1315                 s->b16 = 0;
 1316                 s->stereo = 0;
 1317                 s->running = 0;
 1318                 s->ismaster = 0;
 1319                 s->vol = m->vol;
 1320                 s->buf = m->buf;
 1321                 s->vbuf = NULL;
 1322                 s->slave = NULL;
 1323         }
 1324         return (0);
 1325 }
 1326 
 1327 void
 1328 emu_vsetup(struct emu_voice *v, int fmt, int spd)
 1329 {
 1330         if (fmt) {
 1331                 v->b16 = (fmt & AFMT_16BIT) ? 1 : 0;
 1332                 v->stereo = (AFMT_CHANNEL(fmt) > 1) ? 1 : 0;
 1333                 if (v->slave != NULL) {
 1334                         v->slave->b16 = v->b16;
 1335                         v->slave->stereo = v->stereo;
 1336                 }
 1337         }
 1338         if (spd) {
 1339                 v->speed = spd;
 1340                 if (v->slave != NULL)
 1341                         v->slave->speed = v->speed;
 1342         }
 1343 }
 1344 
 1345 void
 1346 emu_vroute(struct emu_sc_info *sc, struct emu_route *rt,  struct emu_voice *v)
 1347 {
 1348         int i;
 1349 
 1350         for (i = 0; i < 8; i++) {
 1351                 v->routing[i] = rt->routing_left[i];
 1352                 v->amounts[i] = rt->amounts_left[i];
 1353         }
 1354         if ((v->stereo) && (v->ismaster == 0))
 1355                 for (i = 0; i < 8; i++) {
 1356                         v->routing[i] = rt->routing_right[i];
 1357                         v->amounts[i] = rt->amounts_right[i];
 1358                 }
 1359 
 1360         if ((v->stereo) && (v->slave != NULL))
 1361                 emu_vroute(sc, rt, v->slave);
 1362 }
 1363 
 1364 void
 1365 emu_vwrite(struct emu_sc_info *sc, struct emu_voice *v)
 1366 {
 1367         int s;
 1368         uint32_t start, val, silent_page;
 1369 
 1370         s = (v->stereo ? 1 : 0) + (v->b16 ? 1 : 0);
 1371 
 1372         v->sa = v->start >> s;
 1373         v->ea = v->end >> s;
 1374 
 1375 
 1376         if (v->stereo) {
 1377                 emu_wrptr(sc, v->vnum, CPF, CPF_STEREO_MASK);
 1378         } else {
 1379                 emu_wrptr(sc, v->vnum, CPF, 0);
 1380         }
 1381         val = v->stereo ? 28 : 30;
 1382         val *= v->b16 ? 1 : 2;
 1383         start = v->sa + val;
 1384 
 1385         if (sc->is_emu10k1) {
 1386                 emu_wrptr(sc, v->vnum, FXRT, ((v->routing[3] << 12) |
 1387                     (v->routing[2] << 8) |
 1388                     (v->routing[1] << 4) |
 1389                     (v->routing[0] << 0)) << 16);
 1390         } else {
 1391                 emu_wrptr(sc, v->vnum, A_FXRT1, (v->routing[3] << 24) |
 1392                     (v->routing[2] << 16) |
 1393                     (v->routing[1] << 8) |
 1394                     (v->routing[0] << 0));
 1395                 emu_wrptr(sc, v->vnum, A_FXRT2, (v->routing[7] << 24) |
 1396                     (v->routing[6] << 16) |
 1397                     (v->routing[5] << 8) |
 1398                     (v->routing[4] << 0));
 1399                 emu_wrptr(sc, v->vnum, A_SENDAMOUNTS, (v->amounts[7] << 24) |
 1400                     (v->amounts[6] << 26) |
 1401                     (v->amounts[5] << 8) |
 1402                     (v->amounts[4] << 0));
 1403         }
 1404         emu_wrptr(sc, v->vnum, PTRX, (v->amounts[0] << 8) | (v->amounts[1] << 0));
 1405         emu_wrptr(sc, v->vnum, DSL, v->ea | (v->amounts[3] << 24));
 1406         emu_wrptr(sc, v->vnum, PSST, v->sa | (v->amounts[2] << 24));
 1407 
 1408         emu_wrptr(sc, v->vnum, CCCA, start | (v->b16 ? 0 : CCCA_8BITSELECT));
 1409         emu_wrptr(sc, v->vnum, Z1, 0);
 1410         emu_wrptr(sc, v->vnum, Z2, 0);
 1411 
 1412         silent_page = ((uint32_t) (sc->mem.silent_page_addr) << 1) | MAP_PTI_MASK;
 1413         emu_wrptr(sc, v->vnum, MAPA, silent_page);
 1414         emu_wrptr(sc, v->vnum, MAPB, silent_page);
 1415 
 1416         emu_wrptr(sc, v->vnum, CVCF, CVCF_CURRENTFILTER_MASK);
 1417         emu_wrptr(sc, v->vnum, VTFT, VTFT_FILTERTARGET_MASK);
 1418         emu_wrptr(sc, v->vnum, ATKHLDM, 0);
 1419         emu_wrptr(sc, v->vnum, DCYSUSM, DCYSUSM_DECAYTIME_MASK);
 1420         emu_wrptr(sc, v->vnum, LFOVAL1, 0x8000);
 1421         emu_wrptr(sc, v->vnum, LFOVAL2, 0x8000);
 1422         emu_wrptr(sc, v->vnum, FMMOD, 0);
 1423         emu_wrptr(sc, v->vnum, TREMFRQ, 0);
 1424         emu_wrptr(sc, v->vnum, FM2FRQ2, 0);
 1425         emu_wrptr(sc, v->vnum, ENVVAL, 0x8000);
 1426 
 1427         emu_wrptr(sc, v->vnum, ATKHLDV, ATKHLDV_HOLDTIME_MASK | ATKHLDV_ATTACKTIME_MASK);
 1428         emu_wrptr(sc, v->vnum, ENVVOL, 0x8000);
 1429 
 1430         emu_wrptr(sc, v->vnum, PEFE_FILTERAMOUNT, 0x7f);
 1431         emu_wrptr(sc, v->vnum, PEFE_PITCHAMOUNT, 0);
 1432         if ((v->stereo) && (v->slave != NULL))
 1433                 emu_vwrite(sc, v->slave);
 1434 }
 1435 
 1436 static void
 1437 emu_vstop(struct emu_sc_info *sc, char channel, int enable)
 1438 {
 1439         int reg;
 1440 
 1441         reg = (channel & 0x20) ? SOLEH : SOLEL;
 1442         channel &= 0x1f;
 1443         reg |= 1 << 24;
 1444         reg |= channel << 16;
 1445         emu_wrptr(sc, 0, reg, enable);
 1446 }
 1447 
 1448 void
 1449 emu_vtrigger(struct emu_sc_info *sc, struct emu_voice *v, int go)
 1450 {
 1451         uint32_t pitch_target, initial_pitch;
 1452         uint32_t cra, cs, ccis;
 1453         uint32_t sample, i;
 1454 
 1455         if (go) {
 1456                 cra = 64;
 1457                 cs = v->stereo ? 4 : 2;
 1458                 ccis = v->stereo ? 28 : 30;
 1459                 ccis *= v->b16 ? 1 : 2;
 1460                 sample = v->b16 ? 0x00000000 : 0x80808080;
 1461                 for (i = 0; i < cs; i++)
 1462                         emu_wrptr(sc, v->vnum, CD0 + i, sample);
 1463                 emu_wrptr(sc, v->vnum, CCR_CACHEINVALIDSIZE, 0);
 1464                 emu_wrptr(sc, v->vnum, CCR_READADDRESS, cra);
 1465                 emu_wrptr(sc, v->vnum, CCR_CACHEINVALIDSIZE, ccis);
 1466 
 1467                 emu_wrptr(sc, v->vnum, IFATN, 0xff00);
 1468                 emu_wrptr(sc, v->vnum, VTFT, 0xffffffff);
 1469                 emu_wrptr(sc, v->vnum, CVCF, 0xffffffff);
 1470                 emu_wrptr(sc, v->vnum, DCYSUSV, 0x00007f7f);
 1471                 emu_vstop(sc, v->vnum, 0);
 1472 
 1473                 pitch_target = emu_rate_to_linearpitch(v->speed);
 1474                 initial_pitch = emu_rate_to_pitch(v->speed) >> 8;
 1475                 emu_wrptr(sc, v->vnum, PTRX_PITCHTARGET, pitch_target);
 1476                 emu_wrptr(sc, v->vnum, CPF_CURRENTPITCH, pitch_target);
 1477                 emu_wrptr(sc, v->vnum, IP, initial_pitch);
 1478         } else {
 1479                 emu_wrptr(sc, v->vnum, PTRX_PITCHTARGET, 0);
 1480                 emu_wrptr(sc, v->vnum, CPF_CURRENTPITCH, 0);
 1481                 emu_wrptr(sc, v->vnum, IFATN, 0xffff);
 1482                 emu_wrptr(sc, v->vnum, VTFT, 0x0000ffff);
 1483                 emu_wrptr(sc, v->vnum, CVCF, 0x0000ffff);
 1484                 emu_wrptr(sc, v->vnum, IP, 0);
 1485                 emu_vstop(sc, v->vnum, 1);
 1486         }
 1487         if ((v->stereo) && (v->slave != NULL))
 1488                 emu_vtrigger(sc, v->slave, go);
 1489 }
 1490 
 1491 int
 1492 emu_vpos(struct emu_sc_info *sc, struct emu_voice *v)
 1493 {
 1494         int s, ptr;
 1495 
 1496         s = (v->b16 ? 1 : 0) + (v->stereo ? 1 : 0);
 1497         ptr = (emu_rdptr(sc, v->vnum, CCCA_CURRADDR) - (v->start >> s)) << s;
 1498         return (ptr & ~0x0000001f);
 1499 }
 1500 
 1501 
 1502 /* fx */
 1503 static void
 1504 emu_wrefx(struct emu_sc_info *sc, unsigned int pc, unsigned int data)
 1505 {
 1506         emu_wrptr(sc, 0, sc->code_base + pc, data);
 1507 }
 1508 
 1509 
 1510 static void
 1511 emu_addefxop(struct emu_sc_info *sc, unsigned int op, unsigned int z, unsigned int w, unsigned int x, unsigned int y, uint32_t * pc)
 1512 {
 1513         if ((*pc) + 1 > sc->code_size) {
 1514                 device_printf(sc->dev, "DSP CODE OVERRUN: attept to write past code_size (pc=%d)\n", (*pc));
 1515                 return;
 1516         }
 1517         emu_wrefx(sc, (*pc) * 2, (x << sc->high_operand_shift) | y);
 1518         emu_wrefx(sc, (*pc) * 2 + 1, (op << sc->opcode_shift) | (z << sc->high_operand_shift) | w);
 1519         (*pc)++;
 1520 }
 1521 
 1522 static int
 1523 sysctl_emu_mixer_control(SYSCTL_HANDLER_ARGS)
 1524 {
 1525         struct emu_sc_info *sc;
 1526         int     mixer_id;
 1527         int     new_vol;
 1528         int     err;
 1529 
 1530         sc = arg1;
 1531         mixer_id = arg2;
 1532 
 1533         new_vol = emumix_get_volume(sc, mixer_id);
 1534         err = sysctl_handle_int(oidp, &new_vol, 0, req);
 1535 
 1536         if (err || req->newptr == NULL)
 1537                 return (err);
 1538         if (new_vol < 0 || new_vol > 100)
 1539                 return (EINVAL);
 1540         emumix_set_volume(sc, mixer_id, new_vol);
 1541 
 1542         return (0);
 1543 }
 1544 
 1545 static int
 1546 emu_addefxmixer(struct emu_sc_info *sc, const char *mix_name, const int mix_id, uint32_t defvolume)
 1547 {
 1548         int volgpr;
 1549         char    sysctl_name[32];
 1550 
 1551         volgpr = emu_rm_gpr_alloc(sc->rm, 1);
 1552         emumix_set_fxvol(sc, volgpr, defvolume);
 1553         /*
 1554          * Mixer controls with NULL mix_name are handled
 1555          * by AC97 emulation code or PCM mixer.
 1556          */
 1557         if (mix_name != NULL) {
 1558                 /*
 1559                  * Temporary sysctls should start with underscore,
 1560                  * see freebsd-current mailing list, emu10kx driver
 1561                  * discussion around 2006-05-24.
 1562                  */
 1563                 snprintf(sysctl_name, 32, "_%s", mix_name);
 1564                 SYSCTL_ADD_PROC(sc->ctx,
 1565                         SYSCTL_CHILDREN(sc->root),
 1566                         OID_AUTO, sysctl_name,
 1567                         CTLTYPE_INT | CTLFLAG_RW, sc, mix_id,
 1568                         sysctl_emu_mixer_control, "I", "");
 1569         }
 1570 
 1571         return (volgpr);
 1572 }
 1573 
 1574 static int
 1575 sysctl_emu_digitalswitch_control(SYSCTL_HANDLER_ARGS)
 1576 {
 1577         struct emu_sc_info *sc;
 1578         int     new_val;
 1579         int     err;
 1580 
 1581         sc = arg1;
 1582 
 1583         new_val = (sc->mode == MODE_DIGITAL) ? 1 : 0;   
 1584         err = sysctl_handle_int(oidp, &new_val, 0, req);
 1585 
 1586         if (err || req->newptr == NULL)
 1587                 return (err);
 1588         if (new_val < 0 || new_val > 1)
 1589                 return (EINVAL);
 1590 
 1591         switch (new_val) {
 1592                 case 0:
 1593                         emumix_set_mode(sc, MODE_ANALOG);
 1594                         break;
 1595                 case 1:
 1596                         emumix_set_mode(sc, MODE_DIGITAL);
 1597                         break;
 1598         }
 1599         return (0);
 1600 }
 1601 
 1602 static void
 1603 emu_digitalswitch(struct emu_sc_info *sc)
 1604 {
 1605         /* XXX temporary? */
 1606         SYSCTL_ADD_PROC(sc->ctx,
 1607                 SYSCTL_CHILDREN(sc->root),
 1608                 OID_AUTO, "_digital",
 1609                 CTLTYPE_INT | CTLFLAG_RW, sc, 0,
 1610                 sysctl_emu_digitalswitch_control, "I", "Enable digital output");
 1611 
 1612         return;
 1613 }
 1614 
 1615 /*
 1616  * Allocate cache GPRs that will hold mixed output channels
 1617  * and clear it on every DSP run.
 1618  */
 1619 #define EFX_CACHE(CACHE_IDX) do {                               \
 1620         sc->cache_gpr[CACHE_IDX] = emu_rm_gpr_alloc(sc->rm, 1); \
 1621         emu_addefxop(sc, ACC3,                                  \
 1622                 GPR(sc->cache_gpr[CACHE_IDX]),                  \
 1623                 DSP_CONST(0),                                   \
 1624                 DSP_CONST(0),                                   \
 1625                 DSP_CONST(0),                                   \
 1626                 &pc);                                           \
 1627 } while (0)
 1628 
 1629 /* Allocate GPR for volume control and route sound: OUT = OUT + IN * VOL */
 1630 #define EFX_ROUTE(TITLE, INP_NR, IN_GPR_IDX, OUT_CACHE_IDX, DEF) do {   \
 1631         sc->mixer_gpr[IN_GPR_IDX] = emu_addefxmixer(sc, TITLE, IN_GPR_IDX,  DEF); \
 1632         sc->mixer_volcache[IN_GPR_IDX] = DEF;                   \
 1633         emu_addefxop(sc, MACS,                                  \
 1634                 GPR(sc->cache_gpr[OUT_CACHE_IDX]),              \
 1635                 GPR(sc->cache_gpr[OUT_CACHE_IDX]),              \
 1636                 INP_NR,                                         \
 1637                 GPR(sc->mixer_gpr[IN_GPR_IDX]),                 \
 1638                 &pc);                                           \
 1639 } while (0)
 1640 
 1641 /* allocate GPR, OUT = IN * VOL */
 1642 #define EFX_OUTPUT(TITLE, OUT_CACHE_IDX, OUT_GPR_IDX, OUTP_NR, DEF) do {        \
 1643         sc->mixer_gpr[OUT_GPR_IDX] = emu_addefxmixer(sc, TITLE, OUT_GPR_IDX, DEF); \
 1644         sc->mixer_volcache[OUT_GPR_IDX] = DEF;                  \
 1645         emu_addefxop(sc, MACS,                                  \
 1646                 OUTP(OUTP_NR),                                  \
 1647                 DSP_CONST(0),                                   \
 1648                 GPR(sc->cache_gpr[OUT_CACHE_IDX]),              \
 1649                 GPR(sc->mixer_gpr[OUT_GPR_IDX]),                \
 1650                 &pc);                                           \
 1651 } while (0)
 1652 
 1653 /* like EFX_OUTPUT, but don't allocate mixer gpr */
 1654 #define EFX_OUTPUTD(OUT_CACHE_IDX, OUT_GPR_IDX, OUTP_NR) do {   \
 1655         emu_addefxop(sc, MACS,                                  \
 1656                 OUTP(OUTP_NR),                                  \
 1657                 DSP_CONST(0),                                   \
 1658                 GPR(sc->cache_gpr[OUT_CACHE_IDX]),              \
 1659                 GPR(sc->mixer_gpr[OUT_GPR_IDX]),                \
 1660                 &pc);                                           \
 1661 } while (0)
 1662 
 1663 /* skip next OPCOUNT instructions if FLAG != 0 */
 1664 #define EFX_SKIP(OPCOUNT, FLAG_GPR) do {                        \
 1665         emu_addefxop(sc, MACS,                                  \
 1666                 DSP_CONST(0),                                   \
 1667                 GPR(sc->mute_gpr[FLAG_GPR]),                                    \
 1668                 DSP_CONST(0),                                   \
 1669                 DSP_CONST(0),                                   \
 1670                 &pc);                                           \
 1671         emu_addefxop(sc, SKIP,                                  \
 1672                 DSP_CCR,                                        \
 1673                 DSP_CCR,                                        \
 1674                 COND_NEQ_ZERO,                                  \
 1675                 OPCOUNT,                                        \
 1676                 &pc);                                           \
 1677 } while (0)
 1678 
 1679 #define EFX_COPY(TO, FROM) do {                                 \
 1680         emu_addefxop(sc, ACC3,                                  \
 1681                 TO,                                             \
 1682                 DSP_CONST(0),                                   \
 1683                 DSP_CONST(0),                                   \
 1684                 FROM,                                           \
 1685                 &pc);                                           \
 1686 } while (0)
 1687 
 1688 
 1689 static void
 1690 emu_initefx(struct emu_sc_info *sc)
 1691 {
 1692         unsigned int i;
 1693         uint32_t pc;
 1694 
 1695         /* stop DSP */
 1696         if (sc->is_emu10k1) {
 1697                 emu_wrptr(sc, 0, DBG, EMU10K1_DBG_SINGLE_STEP);
 1698         } else {
 1699                 emu_wrptr(sc, 0, A_DBG, A_DBG_SINGLE_STEP);
 1700         }
 1701 
 1702         /* code size is in instructions */
 1703         pc = 0;
 1704         for (i = 0; i < sc->code_size; i++) {
 1705                 if (sc->is_emu10k1) {
 1706                         emu_addefxop(sc, ACC3, DSP_CONST(0x0), DSP_CONST(0x0), DSP_CONST(0x0), DSP_CONST(0x0), &pc);
 1707                 } else {
 1708                         emu_addefxop(sc, SKIP, DSP_CONST(0x0), DSP_CONST(0x0), DSP_CONST(0xf), DSP_CONST(0x0), &pc);
 1709                 }
 1710         }
 1711 
 1712         /* allocate GPRs for mute switches (EFX_SKIP). Mute by default */
 1713         for (i = 0; i < NUM_MUTE; i++) {
 1714                 sc->mute_gpr[i] = emu_rm_gpr_alloc(sc->rm, 1);
 1715                 emumix_set_gpr(sc, sc->mute_gpr[i], 1);
 1716         }
 1717         emu_digitalswitch(sc);
 1718 
 1719         pc = 0;
 1720 
 1721         /*
 1722          * DSP code below is not good, because:
 1723          * 1. It can be written smaller, if it can use DSP accumulator register
 1724          * instead of cache_gpr[].
 1725          * 2. It can be more careful when volume is 100%, because in DSP
 1726          * x*0x7fffffff may not be equal to x !
 1727          */
 1728 
 1729         /* clean outputs */
 1730         for (i = 0; i < 16 ; i++) {
 1731                 emu_addefxop(sc, ACC3, OUTP(i), DSP_CONST(0), DSP_CONST(0), DSP_CONST(0), &pc);
 1732         }
 1733 
 1734 
 1735         if (sc->is_emu10k1) {
 1736                 EFX_CACHE(C_FRONT_L);
 1737                 EFX_CACHE(C_FRONT_R);
 1738                 EFX_CACHE(C_REC_L);
 1739                 EFX_CACHE(C_REC_R);
 1740 
 1741                 /* fx0 to front/record, 100%/muted by default */
 1742                 EFX_ROUTE("pcm_front_l", FX(0), M_FX0_FRONT_L, C_FRONT_L, 100);
 1743                 EFX_ROUTE("pcm_front_r", FX(1), M_FX1_FRONT_R, C_FRONT_R, 100);
 1744                 EFX_ROUTE(NULL, FX(0), M_FX0_REC_L, C_REC_L, 0);
 1745                 EFX_ROUTE(NULL, FX(1), M_FX1_REC_R, C_REC_R, 0);
 1746 
 1747                 /* in0, from AC97 codec output */
 1748                 EFX_ROUTE("ac97_front_l", INP(IN_AC97_L), M_IN0_FRONT_L, C_FRONT_L, 0);
 1749                 EFX_ROUTE("ac97_front_r", INP(IN_AC97_R), M_IN0_FRONT_R, C_FRONT_R, 0);
 1750                 EFX_ROUTE("ac97_rec_l", INP(IN_AC97_L), M_IN0_REC_L, C_REC_L, 0);
 1751                 EFX_ROUTE("ac97_rec_r", INP(IN_AC97_R), M_IN0_REC_R, C_REC_R, 0);
 1752 
 1753                 /* in1, from CD S/PDIF */
 1754                 /* XXX EFX_SKIP 4 assumes that each EFX_ROUTE is one DSP op */
 1755                 EFX_SKIP(4, CDSPDIFMUTE);
 1756                 EFX_ROUTE(NULL, INP(IN_SPDIF_CD_L), M_IN1_FRONT_L, C_FRONT_L, 0);
 1757                 EFX_ROUTE(NULL, INP(IN_SPDIF_CD_R), M_IN1_FRONT_R, C_FRONT_R, 0);
 1758                 EFX_ROUTE(NULL, INP(IN_SPDIF_CD_L), M_IN1_REC_L, C_REC_L, 0);
 1759                 EFX_ROUTE(NULL, INP(IN_SPDIF_CD_R), M_IN1_REC_R, C_REC_R, 0);
 1760                 
 1761                 if (sc->dbg_level > 0) {
 1762                         /* in2, ZoomVide (???) */
 1763                         EFX_ROUTE("zoom_front_l", INP(IN_ZOOM_L), M_IN2_FRONT_L, C_FRONT_L, 0);
 1764                         EFX_ROUTE("zoom_front_r", INP(IN_ZOOM_R), M_IN2_FRONT_R, C_FRONT_R, 0);
 1765                         EFX_ROUTE("zoom_rec_l", INP(IN_ZOOM_L), M_IN2_REC_L, C_REC_L, 0);
 1766                         EFX_ROUTE("zoom_rec_r", INP(IN_ZOOM_R), M_IN2_REC_R, C_REC_R, 0);
 1767                 }
 1768 
 1769                 /* in3, TOSLink  */
 1770                 EFX_ROUTE(NULL, INP(IN_TOSLINK_L), M_IN3_FRONT_L, C_FRONT_L, 0);
 1771                 EFX_ROUTE(NULL, INP(IN_TOSLINK_R), M_IN3_FRONT_R, C_FRONT_R, 0);
 1772                 EFX_ROUTE(NULL, INP(IN_TOSLINK_L), M_IN3_REC_L, C_REC_L, 0);
 1773                 EFX_ROUTE(NULL, INP(IN_TOSLINK_R), M_IN3_REC_R, C_REC_R, 0);
 1774                 /* in4, LineIn  */
 1775                 EFX_ROUTE(NULL, INP(IN_LINE1_L), M_IN4_FRONT_L, C_FRONT_L, 0);
 1776                 EFX_ROUTE(NULL, INP(IN_LINE1_R), M_IN4_FRONT_R, C_FRONT_R, 0);
 1777                 EFX_ROUTE(NULL, INP(IN_LINE1_L), M_IN4_REC_L, C_REC_L, 0);
 1778                 EFX_ROUTE(NULL, INP(IN_LINE1_R), M_IN4_REC_R, C_REC_R, 0);
 1779 
 1780                 /* in5, on-card S/PDIF */
 1781                 EFX_ROUTE(NULL, INP(IN_COAX_SPDIF_L), M_IN5_FRONT_L, C_FRONT_L, 0);
 1782                 EFX_ROUTE(NULL, INP(IN_COAX_SPDIF_R), M_IN5_FRONT_R, C_FRONT_R, 0);
 1783                 EFX_ROUTE(NULL, INP(IN_COAX_SPDIF_L), M_IN5_REC_L, C_REC_L, 0);
 1784                 EFX_ROUTE(NULL, INP(IN_COAX_SPDIF_R), M_IN5_REC_R, C_REC_R, 0);
 1785 
 1786                 /* in6, Line2 on Live!Drive */
 1787                 EFX_ROUTE(NULL, INP(IN_LINE2_L), M_IN6_FRONT_L, C_FRONT_L, 0);
 1788                 EFX_ROUTE(NULL, INP(IN_LINE2_R), M_IN6_FRONT_R, C_FRONT_R, 0);
 1789                 EFX_ROUTE(NULL, INP(IN_LINE2_L), M_IN6_REC_L, C_REC_L, 0);
 1790                 EFX_ROUTE(NULL, INP(IN_LINE2_R), M_IN6_REC_R, C_REC_R, 0);
 1791 
 1792                 if (sc->dbg_level > 0) {
 1793                         /* in7, unknown */
 1794                         EFX_ROUTE("in7_front_l", INP(0xE), M_IN7_FRONT_L, C_FRONT_L, 0);
 1795                         EFX_ROUTE("in7_front_r", INP(0xF), M_IN7_FRONT_R, C_FRONT_R, 0);
 1796                         EFX_ROUTE("in7_rec_l", INP(0xE), M_IN7_REC_L, C_REC_L, 0);
 1797                         EFX_ROUTE("in7_rec_r", INP(0xF), M_IN7_REC_R, C_REC_R, 0);
 1798                 }
 1799 
 1800                 /* analog and digital */
 1801                 EFX_OUTPUT("master_front_l", C_FRONT_L, M_MASTER_FRONT_L, OUT_AC97_L, 100);
 1802                 EFX_OUTPUT("master_front_r", C_FRONT_R, M_MASTER_FRONT_R, OUT_AC97_R, 100);
 1803                 /* S/PDIF */
 1804                 EFX_OUTPUTD(C_FRONT_L, M_MASTER_FRONT_L, OUT_TOSLINK_L);
 1805                 EFX_OUTPUTD(C_FRONT_R, M_MASTER_FRONT_R, OUT_TOSLINK_R);
 1806                 /* Headphones */
 1807                 EFX_OUTPUTD(C_FRONT_L, M_MASTER_FRONT_L, OUT_HEADPHONE_L);
 1808                 EFX_OUTPUTD(C_FRONT_R, M_MASTER_FRONT_R, OUT_HEADPHONE_R);
 1809 
 1810                 /* rec output to "ADC" */
 1811                 EFX_OUTPUT("master_rec_l", C_REC_L, M_MASTER_REC_L, OUT_ADC_REC_L, 100);
 1812                 EFX_OUTPUT("master_rec_r", C_REC_R, M_MASTER_REC_R, OUT_ADC_REC_R, 100);
 1813 
 1814                 if (!(sc->mch_disabled)) {
 1815                         /*
 1816                          * Additional channel volume is controlled by mixer in
 1817                          * emu_dspmixer_set() in -pcm.c
 1818                          */
 1819 
 1820                         /* fx2/3 (pcm1) to rear */
 1821                         EFX_CACHE(C_REAR_L);
 1822                         EFX_CACHE(C_REAR_R);
 1823                         EFX_ROUTE(NULL, FX(2), M_FX2_REAR_L, C_REAR_L, 100);
 1824                         EFX_ROUTE(NULL, FX(3), M_FX3_REAR_R, C_REAR_R, 100);
 1825 
 1826                         EFX_OUTPUT(NULL, C_REAR_L, M_MASTER_REAR_L, OUT_REAR_L, 100);
 1827                         EFX_OUTPUT(NULL, C_REAR_R, M_MASTER_REAR_R, OUT_REAR_R, 100);
 1828                         if (sc->has_51) {
 1829                                 /* fx4 (pcm2) to center */
 1830                                 EFX_CACHE(C_CENTER);
 1831                                 EFX_ROUTE(NULL, FX(4), M_FX4_CENTER, C_CENTER, 100);
 1832                                 EFX_OUTPUT(NULL, C_CENTER, M_MASTER_CENTER, OUT_D_CENTER, 100);
 1833 
 1834                                 /* XXX in digital mode (default) this should be muted because
 1835                                 this output is shared with digital out */
 1836                                 EFX_SKIP(1, ANALOGMUTE);
 1837                                 EFX_OUTPUTD(C_CENTER, M_MASTER_CENTER, OUT_A_CENTER);
 1838 
 1839                                 /* fx5 (pcm3) to sub */
 1840                                 EFX_CACHE(C_SUB);
 1841                                 EFX_ROUTE(NULL, FX(5), M_FX5_SUBWOOFER, C_SUB, 100);
 1842                                 EFX_OUTPUT(NULL, C_SUB, M_MASTER_SUBWOOFER, OUT_D_SUB, 100);
 1843 
 1844                                 /* XXX in digital mode (default) this should be muted because
 1845                                 this output is shared with digital out */
 1846                                 EFX_SKIP(1, ANALOGMUTE);
 1847                                 EFX_OUTPUTD(C_SUB, M_MASTER_SUBWOOFER, OUT_A_SUB);
 1848 
 1849                         }
 1850                 } else {
 1851                         /* SND_EMU10KX_MULTICHANNEL_DISABLED */
 1852                         EFX_OUTPUT(NULL, C_FRONT_L, M_MASTER_REAR_L, OUT_REAR_L, 57); /* 75%*75% */
 1853                         EFX_OUTPUT(NULL, C_FRONT_R, M_MASTER_REAR_R, OUT_REAR_R, 57); /* 75%*75% */
 1854 
 1855 #if 0
 1856                         /* XXX 5.1 does not work */
 1857 
 1858                         if (sc->has_51) {
 1859                                 /* (fx0+fx1)/2 to center */
 1860                                 EFX_CACHE(C_CENTER);
 1861                                 emu_addefxop(sc, MACS,
 1862                                         GPR(sc->cache_gpr[C_CENTER]),
 1863                                         GPR(sc->cache_gpr[C_CENTER]),
 1864                                         DSP_CONST(0xd), /* = 1/2 */
 1865                                         GPR(sc->cache_gpr[C_FRONT_L]),
 1866                                         &pc);
 1867                                 emu_addefxop(sc, MACS,
 1868                                         GPR(sc->cache_gpr[C_CENTER]),
 1869                                         GPR(sc->cache_gpr[C_CENTER]),
 1870                                         DSP_CONST(0xd), /* = 1/2 */
 1871                                         GPR(sc->cache_gpr[C_FRONT_R]),
 1872                                         &pc);
 1873                                 EFX_OUTPUT(NULL, C_CENTER, M_MASTER_CENTER, OUT_D_CENTER, 100);
 1874 
 1875                                 /* XXX in digital mode (default) this should be muted because
 1876                                 this output is shared with digital out */
 1877                                 EFX_SKIP(1, ANALOGMUTE);
 1878                                 EFX_OUTPUTD(C_CENTER, M_MASTER_CENTER, OUT_A_CENTER);
 1879 
 1880                                 /* (fx0+fx1)/2  to sub */
 1881                                 EFX_CACHE(C_SUB);
 1882                                 emu_addefxop(sc, MACS,
 1883                                         GPR(sc->cache_gpr[C_CENTER]),
 1884                                         GPR(sc->cache_gpr[C_CENTER]),
 1885                                         DSP_CONST(0xd), /* = 1/2 */
 1886                                         GPR(sc->cache_gpr[C_FRONT_L]),
 1887                                         &pc);
 1888                                 emu_addefxop(sc, MACS,
 1889                                         GPR(sc->cache_gpr[C_CENTER]),
 1890                                         GPR(sc->cache_gpr[C_CENTER]),
 1891                                         DSP_CONST(0xd), /* = 1/2 */
 1892                                         GPR(sc->cache_gpr[C_FRONT_R]),
 1893                                         &pc);
 1894                                 /* XXX add lowpass filter here */
 1895 
 1896                                 EFX_OUTPUT(NULL, C_SUB, M_MASTER_SUBWOOFER, OUT_D_SUB, 100);
 1897 
 1898                                 /* XXX in digital mode (default) this should be muted because
 1899                                 this output is shared with digital out */
 1900                                 EFX_SKIP(1, ANALOGMUTE);
 1901                                 EFX_OUTPUTD(C_SUB, M_MASTER_SUBWOOFER, OUT_A_SUB);
 1902                         }
 1903 #endif
 1904                 } /* !mch_disabled */
 1905                 if (sc->mch_rec) {
 1906                         /*
 1907                          * MCH RECORDING , hight 16 slots. On 5.1 cards first 4 slots
 1908                          * are used as outputs and already filled with data
 1909                          */
 1910                         /*
 1911                          * XXX On Live! cards stream does not begin at zero offset.
 1912                          * It can be HW, driver or sound buffering problem.
 1913                          * Use sync substream (offset 0x3E) to let userland find
 1914                          * correct data.
 1915                          */
 1916 
 1917                         /*
 1918                          * Substream map (in byte offsets, each substream is 2 bytes):
 1919                          *      0x00..0x1E - outputs
 1920                          *      0x20..0x3E - FX, inputs ans sync stream
 1921                          */
 1922 
 1923                         /* First 2 channels (offset 0x20,0x22) are empty */
 1924                         for(i = (sc->has_51 ? 2 : 0); i < 2; i++)
 1925                                 EFX_COPY(FX2(i), DSP_CONST(0));
 1926 
 1927                         /* PCM Playback monitoring, offset 0x24..0x2A */
 1928                         for(i = 0; i < 4; i++)
 1929                                 EFX_COPY(FX2(i+2), FX(i));
 1930 
 1931                         /* Copy of some inputs, offset 0x2C..0x3C */
 1932                         for(i = 0; i < 9; i++)
 1933                                 EFX_COPY(FX2(i+8), INP(i));
 1934 
 1935                         /* sync data (0xc0de, offset 0x3E) */
 1936                         sc->dummy_gpr = emu_rm_gpr_alloc(sc->rm, 1);
 1937                         emumix_set_gpr(sc, sc->dummy_gpr, 0xc0de0000);
 1938 
 1939                         EFX_COPY(FX2(15), GPR(sc->dummy_gpr));
 1940                 } /* mch_rec */
 1941         } else /* emu10k2 and later */ {
 1942                 EFX_CACHE(C_FRONT_L);
 1943                 EFX_CACHE(C_FRONT_R);
 1944                 EFX_CACHE(C_REC_L);
 1945                 EFX_CACHE(C_REC_R);
 1946 
 1947                 /* fx0 to front/record, 100%/muted by default */
 1948                 /*
 1949                  * FRONT_[L|R] is controlled by AC97 emulation in
 1950                  * emu_ac97_[read|write]_emulation in -pcm.c
 1951                  */
 1952                 EFX_ROUTE(NULL, FX(0), M_FX0_FRONT_L, C_FRONT_L, 100);
 1953                 EFX_ROUTE(NULL, FX(1), M_FX1_FRONT_R, C_FRONT_R, 100);
 1954                 EFX_ROUTE(NULL, FX(0), M_FX0_REC_L, C_REC_L, 0);
 1955                 EFX_ROUTE(NULL, FX(1), M_FX1_REC_R, C_REC_R, 0);
 1956 
 1957                 /* in0, from AC97 codec output */
 1958                 EFX_ROUTE(NULL, INP(A_IN_AC97_L), M_IN0_FRONT_L, C_FRONT_L, 100);
 1959                 EFX_ROUTE(NULL, INP(A_IN_AC97_R), M_IN0_FRONT_R, C_FRONT_R, 100);
 1960                 EFX_ROUTE(NULL, INP(A_IN_AC97_L), M_IN0_REC_L, C_REC_L, 0);
 1961                 EFX_ROUTE(NULL, INP(A_IN_AC97_R), M_IN0_REC_R, C_REC_R, 0);
 1962 
 1963                 /* in1, from CD S/PDIF */
 1964                 EFX_ROUTE(NULL, INP(A_IN_SPDIF_CD_L), M_IN1_FRONT_L, C_FRONT_L, 0);
 1965                 EFX_ROUTE(NULL, INP(A_IN_SPDIF_CD_R), M_IN1_FRONT_R, C_FRONT_R, 0);
 1966                 EFX_ROUTE(NULL, INP(A_IN_SPDIF_CD_L), M_IN1_REC_L, C_REC_L, 0);
 1967                 EFX_ROUTE(NULL, INP(A_IN_SPDIF_CD_R), M_IN1_REC_R, C_REC_R, 0);
 1968 
 1969                 /* in2, optical & coax S/PDIF on AudigyDrive*/
 1970                 /* XXX Should be muted when GPRSCS valid stream == 0 */
 1971                 EFX_ROUTE(NULL, INP(A_IN_O_SPDIF_L), M_IN2_FRONT_L, C_FRONT_L, 0);
 1972                 EFX_ROUTE(NULL, INP(A_IN_O_SPDIF_R), M_IN2_FRONT_R, C_FRONT_R, 0);
 1973                 EFX_ROUTE(NULL, INP(A_IN_O_SPDIF_L), M_IN2_REC_L, C_REC_L, 0);
 1974                 EFX_ROUTE(NULL, INP(A_IN_O_SPDIF_R), M_IN2_REC_R, C_REC_R, 0);
 1975 
 1976                 if (sc->dbg_level > 0) {
 1977                         /* in3, unknown */
 1978                         EFX_ROUTE("in3_front_l", INP(0x6), M_IN3_FRONT_L, C_FRONT_L, 0);
 1979                         EFX_ROUTE("in3_front_r", INP(0x7), M_IN3_FRONT_R, C_FRONT_R, 0);
 1980                         EFX_ROUTE("in3_rec_l", INP(0x6), M_IN3_REC_L, C_REC_L, 0);
 1981                         EFX_ROUTE("in3_rec_r", INP(0x7), M_IN3_REC_R, C_REC_R, 0);
 1982                 }
 1983 
 1984                 /* in4, LineIn 2 on AudigyDrive */
 1985                 EFX_ROUTE(NULL, INP(A_IN_LINE2_L), M_IN4_FRONT_L, C_FRONT_L, 0);
 1986                 EFX_ROUTE(NULL, INP(A_IN_LINE2_R), M_IN4_FRONT_R, C_FRONT_R, 0);
 1987                 EFX_ROUTE(NULL, INP(A_IN_LINE2_L), M_IN4_REC_L, C_REC_L, 0);
 1988                 EFX_ROUTE(NULL, INP(A_IN_LINE2_R), M_IN4_REC_R, C_REC_R, 0);
 1989 
 1990                 /* in5, on-card S/PDIF */
 1991                 EFX_ROUTE(NULL, INP(A_IN_R_SPDIF_L), M_IN5_FRONT_L, C_FRONT_L, 0);
 1992                 EFX_ROUTE(NULL, INP(A_IN_R_SPDIF_R), M_IN5_FRONT_R, C_FRONT_R, 0);
 1993                 EFX_ROUTE(NULL, INP(A_IN_R_SPDIF_L), M_IN5_REC_L, C_REC_L, 0);
 1994                 EFX_ROUTE(NULL, INP(A_IN_R_SPDIF_R), M_IN5_REC_R, C_REC_R, 0);
 1995 
 1996                 /* in6, AUX2 on AudigyDrive */
 1997                 EFX_ROUTE(NULL, INP(A_IN_AUX2_L), M_IN6_FRONT_L, C_FRONT_L, 0);
 1998                 EFX_ROUTE(NULL, INP(A_IN_AUX2_R), M_IN6_FRONT_R, C_FRONT_R, 0);
 1999                 EFX_ROUTE(NULL, INP(A_IN_AUX2_L), M_IN6_REC_L, C_REC_L, 0);
 2000                 EFX_ROUTE(NULL, INP(A_IN_AUX2_R), M_IN6_REC_R, C_REC_R, 0);
 2001 
 2002                 if (sc->dbg_level > 0) {
 2003                         /* in7, unknown */
 2004                         EFX_ROUTE("in7_front_l", INP(0xE), M_IN7_FRONT_L, C_FRONT_L, 0);
 2005                         EFX_ROUTE("in7_front_r", INP(0xF), M_IN7_FRONT_R, C_FRONT_R, 0);
 2006                         EFX_ROUTE("in7_rec_l", INP(0xE), M_IN7_REC_L, C_REC_L, 0);
 2007                         EFX_ROUTE("in7_rec_r", INP(0xF), M_IN7_REC_R, C_REC_R, 0);
 2008                 }
 2009 
 2010                 /* front output to headphones and  alog and digital *front */
 2011                 /* volume controlled by AC97 emulation */
 2012                 EFX_OUTPUT(NULL, C_FRONT_L, M_MASTER_FRONT_L, A_OUT_A_FRONT_L, 100);
 2013                 EFX_OUTPUT(NULL, C_FRONT_R, M_MASTER_FRONT_R, A_OUT_A_FRONT_R, 100);
 2014                 EFX_OUTPUTD(C_FRONT_L, M_MASTER_FRONT_L, A_OUT_D_FRONT_L);
 2015                 EFX_OUTPUTD(C_FRONT_R, M_MASTER_FRONT_R, A_OUT_D_FRONT_R);
 2016                 EFX_OUTPUTD(C_FRONT_L, M_MASTER_FRONT_L, A_OUT_HPHONE_L);
 2017                 EFX_OUTPUTD(C_FRONT_R, M_MASTER_FRONT_R, A_OUT_HPHONE_R);
 2018 
 2019                 /* rec output to "ADC" */
 2020                 /* volume controlled by AC97 emulation */
 2021                 EFX_OUTPUT(NULL, C_REC_L, M_MASTER_REC_L, A_OUT_ADC_REC_L, 100);
 2022                 EFX_OUTPUT(NULL, C_REC_R, M_MASTER_REC_R, A_OUT_ADC_REC_R, 100);
 2023 
 2024                 if (!(sc->mch_disabled)) {
 2025                         /*
 2026                          * Additional channel volume is controlled by mixer in
 2027                          * emu_dspmixer_set() in -pcm.c
 2028                          */
 2029 
 2030                         /* fx2/3 (pcm1) to rear */
 2031                         EFX_CACHE(C_REAR_L);
 2032                         EFX_CACHE(C_REAR_R);
 2033                         EFX_ROUTE(NULL, FX(2), M_FX2_REAR_L, C_REAR_L, 100);
 2034                         EFX_ROUTE(NULL, FX(3), M_FX3_REAR_R, C_REAR_R, 100);
 2035 
 2036                         EFX_OUTPUT(NULL, C_REAR_L, M_MASTER_REAR_L, A_OUT_A_REAR_L, 100);
 2037                         EFX_OUTPUT(NULL, C_REAR_R, M_MASTER_REAR_R, A_OUT_A_REAR_R, 100);
 2038                         EFX_OUTPUTD(C_REAR_L, M_MASTER_REAR_L, A_OUT_D_REAR_L);
 2039                         EFX_OUTPUTD(C_REAR_R, M_MASTER_REAR_R, A_OUT_D_REAR_R);
 2040 
 2041                         /* fx4 (pcm2) to center */
 2042                         EFX_CACHE(C_CENTER);
 2043                         EFX_ROUTE(NULL, FX(4), M_FX4_CENTER, C_CENTER, 100);
 2044                         EFX_OUTPUT(NULL, C_CENTER, M_MASTER_CENTER, A_OUT_D_CENTER, 100);
 2045 #if 0
 2046                         /*
 2047                          * XXX in digital mode (default) this should be muted
 2048                          * because this output is shared with digital out
 2049                          */
 2050                         EFX_OUTPUTD(C_CENTER, M_MASTER_CENTER, A_OUT_A_CENTER);
 2051 #endif
 2052                         /* fx5 (pcm3) to sub */
 2053                         EFX_CACHE(C_SUB);
 2054                         EFX_ROUTE(NULL, FX(5), M_FX5_SUBWOOFER, C_SUB, 100);
 2055                         EFX_OUTPUT(NULL, C_SUB, M_MASTER_SUBWOOFER, A_OUT_D_SUB, 100);
 2056 #if 0
 2057                         /*
 2058                          * XXX in digital mode (default) this should be muted
 2059                          * because this output is shared with digital out
 2060                          */
 2061                         EFX_OUTPUTD(C_SUB, M_MASTER_SUBWOOFER, A_OUT_A_SUB);
 2062 #endif
 2063                         if (sc->has_71) {
 2064                                 /* XXX this will broke headphones on AudigyDrive */
 2065                                 /* fx6/7 (pcm4) to side */
 2066                                 EFX_CACHE(C_SIDE_L);
 2067                                 EFX_CACHE(C_SIDE_R);
 2068                                 EFX_ROUTE(NULL, FX(6), M_FX6_SIDE_L, C_SIDE_L, 100);
 2069                                 EFX_ROUTE(NULL, FX(7), M_FX7_SIDE_R, C_SIDE_R, 100);
 2070                                 EFX_OUTPUT(NULL, C_SIDE_L, M_MASTER_SIDE_L, A_OUT_A_SIDE_L, 100);
 2071                                 EFX_OUTPUT(NULL, C_SIDE_R, M_MASTER_SIDE_R, A_OUT_A_SIDE_R, 100);
 2072                                 EFX_OUTPUTD(C_SIDE_L, M_MASTER_SIDE_L, A_OUT_D_SIDE_L);
 2073                                 EFX_OUTPUTD(C_SIDE_R, M_MASTER_SIDE_R, A_OUT_D_SIDE_R);
 2074                         }
 2075                 } else {        /* mch_disabled */
 2076                         EFX_OUTPUTD(C_FRONT_L, M_MASTER_FRONT_L, A_OUT_A_REAR_L);
 2077                         EFX_OUTPUTD(C_FRONT_R, M_MASTER_FRONT_R, A_OUT_A_REAR_R);
 2078 
 2079                         EFX_OUTPUTD(C_FRONT_L, M_MASTER_FRONT_L, A_OUT_D_REAR_L);
 2080                         EFX_OUTPUTD(C_FRONT_R, M_MASTER_FRONT_R, A_OUT_D_REAR_R);
 2081 
 2082                         if (sc->has_51) {
 2083                                 /* (fx0+fx1)/2 to center */
 2084                                 EFX_CACHE(C_CENTER);
 2085                                 emu_addefxop(sc, MACS,
 2086                                         GPR(sc->cache_gpr[C_CENTER]),
 2087                                         GPR(sc->cache_gpr[C_CENTER]),
 2088                                         DSP_CONST(0xd), /* = 1/2 */
 2089                                         GPR(sc->cache_gpr[C_FRONT_L]),
 2090                                         &pc);
 2091                                 emu_addefxop(sc, MACS,
 2092                                         GPR(sc->cache_gpr[C_CENTER]),
 2093                                         GPR(sc->cache_gpr[C_CENTER]),
 2094                                         DSP_CONST(0xd), /* = 1/2 */
 2095                                         GPR(sc->cache_gpr[C_FRONT_R]),
 2096                                         &pc);
 2097                                 EFX_OUTPUT(NULL, C_CENTER, M_MASTER_CENTER, A_OUT_D_CENTER, 100);
 2098 
 2099                                 /* XXX in digital mode (default) this should be muted because
 2100                                 this output is shared with digital out */
 2101                                 EFX_SKIP(1, ANALOGMUTE);
 2102                                 EFX_OUTPUTD(C_CENTER, M_MASTER_CENTER, A_OUT_A_CENTER);
 2103 
 2104                                 /* (fx0+fx1)/2  to sub */
 2105                                 EFX_CACHE(C_SUB);
 2106                                 emu_addefxop(sc, MACS,
 2107                                         GPR(sc->cache_gpr[C_SUB]),
 2108                                         GPR(sc->cache_gpr[C_SUB]),
 2109                                         DSP_CONST(0xd), /* = 1/2 */
 2110                                         GPR(sc->cache_gpr[C_FRONT_L]),
 2111                                         &pc);
 2112                                 emu_addefxop(sc, MACS,
 2113                                         GPR(sc->cache_gpr[C_SUB]),
 2114                                         GPR(sc->cache_gpr[C_SUB]),
 2115                                         DSP_CONST(0xd), /* = 1/2 */
 2116                                         GPR(sc->cache_gpr[C_FRONT_R]),
 2117                                         &pc);
 2118                                 /* XXX add lowpass filter here */
 2119 
 2120                                 EFX_OUTPUT(NULL, C_SUB, M_MASTER_SUBWOOFER, A_OUT_D_SUB, 100);
 2121 
 2122                                 /* XXX in digital mode (default) this should be muted because
 2123                                 this output is shared with digital out */
 2124                                 EFX_SKIP(1, ANALOGMUTE);
 2125                                 EFX_OUTPUTD(C_SUB, M_MASTER_SUBWOOFER, A_OUT_A_SUB);
 2126                         }
 2127                 } /* mch_disabled */
 2128                 if (sc->mch_rec) {
 2129                         /* MCH RECORDING, high 32 slots */
 2130 
 2131                         /*
 2132                          * Stream map (in byte offsets):
 2133                          *      0x00..0x3E - outputs
 2134                          *      0x40..0x7E - FX, inputs
 2135                          *      each substream is 2 bytes.
 2136                          */
 2137                         /*
 2138                          * XXX Audigy 2 Value cards (and, possibly,
 2139                          * Audigy 4) write some unknown data in place of
 2140                          * some outputs (offsets 0x20..0x3F) and one
 2141                          * input (offset 0x7E).
 2142                          */
 2143 
 2144                         /* PCM Playback monitoring, offsets 0x40..0x5E */
 2145                         for(i = 0; i < 16; i++)
 2146                                 EFX_COPY(FX2(i), FX(i));
 2147 
 2148                         /* Copy of all inputs, offsets 0x60..0x7E */
 2149                         for(i = 0; i < 16; i++)
 2150                                 EFX_COPY(FX2(i+16), INP(i));
 2151 #if 0
 2152                         /* XXX Audigy seems to work correct and does not need this */
 2153                         /* sync data (0xc0de), offset 0x7E */
 2154                         sc->dummy_gpr = emu_rm_gpr_alloc(sc->rm, 1);
 2155                         emumix_set_gpr(sc, sc->dummy_gpr, 0xc0de0000);
 2156                         EFX_COPY(FX2(31), GPR(sc->dummy_gpr));
 2157 #endif
 2158                 } /* mch_rec */
 2159         }
 2160 
 2161         sc->routing_code_end = pc;
 2162 
 2163         /* start DSP */
 2164         if (sc->is_emu10k1) {
 2165                 emu_wrptr(sc, 0, DBG, 0);
 2166         } else {
 2167                 emu_wrptr(sc, 0, A_DBG, 0);
 2168         }
 2169 }
 2170 
 2171 /* /dev/em10kx */
 2172 static d_open_t         emu10kx_open;
 2173 static d_close_t        emu10kx_close;
 2174 static d_read_t         emu10kx_read;
 2175 
 2176 static struct cdevsw emu10kx_cdevsw = {
 2177         .d_open =       emu10kx_open,
 2178         .d_close =      emu10kx_close,
 2179         .d_read =       emu10kx_read,
 2180         .d_name =       "emu10kx",
 2181         .d_version =    D_VERSION,
 2182 };
 2183 
 2184 
 2185 static int
 2186 emu10kx_open(struct cdev *i_dev, int flags __unused, int mode __unused, struct thread *td __unused)
 2187 {
 2188         int error;
 2189         struct emu_sc_info *sc;
 2190 
 2191         sc = i_dev->si_drv1;
 2192         mtx_lock(&sc->emu10kx_lock);
 2193         if (sc->emu10kx_isopen) {
 2194                 mtx_unlock(&sc->emu10kx_lock);
 2195                 return (EBUSY);
 2196         }
 2197         sc->emu10kx_isopen = 1;
 2198         mtx_unlock(&sc->emu10kx_lock);
 2199         if (sbuf_new(&sc->emu10kx_sbuf, NULL, 4096, 0) == NULL) {
 2200                 error = ENXIO;
 2201                 goto out;
 2202         }
 2203         sc->emu10kx_bufptr = 0;
 2204         error = (emu10kx_prepare(sc, &sc->emu10kx_sbuf) > 0) ? 0 : ENOMEM;
 2205 out:
 2206         if (error) {
 2207                 mtx_lock(&sc->emu10kx_lock);
 2208                 sc->emu10kx_isopen = 0;
 2209                 mtx_unlock(&sc->emu10kx_lock);
 2210         }
 2211         return (error);
 2212 }
 2213 
 2214 static int
 2215 emu10kx_close(struct cdev *i_dev, int flags __unused, int mode __unused, struct thread *td __unused)
 2216 {
 2217         struct emu_sc_info *sc;
 2218 
 2219         sc = i_dev->si_drv1;
 2220 
 2221         mtx_lock(&sc->emu10kx_lock);
 2222         if (!(sc->emu10kx_isopen)) {
 2223                 mtx_unlock(&sc->emu10kx_lock);
 2224                 return (EBADF);
 2225         }
 2226         sbuf_delete(&sc->emu10kx_sbuf);
 2227         sc->emu10kx_isopen = 0;
 2228         mtx_unlock(&sc->emu10kx_lock);
 2229 
 2230         return (0);
 2231 }
 2232 
 2233 static int
 2234 emu10kx_read(struct cdev *i_dev, struct uio *buf, int flag __unused)
 2235 {
 2236         int l, err;
 2237         struct emu_sc_info *sc;
 2238 
 2239         sc = i_dev->si_drv1;
 2240         mtx_lock(&sc->emu10kx_lock);
 2241         if (!(sc->emu10kx_isopen)) {
 2242                 mtx_unlock(&sc->emu10kx_lock);
 2243                 return (EBADF);
 2244         }
 2245         mtx_unlock(&sc->emu10kx_lock);
 2246 
 2247         l = min(buf->uio_resid, sbuf_len(&sc->emu10kx_sbuf) - sc->emu10kx_bufptr);
 2248         err = (l > 0) ? uiomove(sbuf_data(&sc->emu10kx_sbuf) + sc->emu10kx_bufptr, l, buf) : 0;
 2249         sc->emu10kx_bufptr += l;
 2250 
 2251         return (err);
 2252 }
 2253 
 2254 static int
 2255 emu10kx_prepare(struct emu_sc_info *sc, struct sbuf *s)
 2256 {
 2257         int i;
 2258 
 2259         sbuf_printf(s, "FreeBSD EMU10Kx Audio Driver\n");
 2260         sbuf_printf(s, "\nHardware resource usage:\n");
 2261         sbuf_printf(s, "DSP General Purpose Registers: %d used, %d total\n", sc->rm->num_used, sc->rm->num_gprs);
 2262         sbuf_printf(s, "DSP Instruction Registers: %d used, %d total\n", sc->routing_code_end, sc->code_size);
 2263         sbuf_printf(s, "Card supports");
 2264         if (sc->has_ac97) {
 2265                 sbuf_printf(s, " AC97 codec");
 2266         } else {
 2267                 sbuf_printf(s, " NO AC97 codec");
 2268         }
 2269         if (sc->has_51) {
 2270                 if (sc->has_71)
 2271                         sbuf_printf(s, " and 7.1 output");
 2272                 else
 2273                         sbuf_printf(s, " and 5.1 output");
 2274         }
 2275         if (sc->is_emu10k1)
 2276                 sbuf_printf(s, ", SBLive! DSP code");
 2277         if (sc->is_emu10k2)
 2278                 sbuf_printf(s, ", Audigy DSP code");
 2279         if (sc->is_ca0102)
 2280                 sbuf_printf(s, ", Audigy DSP code with Audigy2 hacks");
 2281         if (sc->is_ca0108)
 2282                 sbuf_printf(s, ", Audigy DSP code with Audigy2Value hacks");
 2283         sbuf_printf(s, "\n");
 2284         if (sc->broken_digital)
 2285                 sbuf_printf(s, "Digital mode unsupported\n");
 2286         sbuf_printf(s, "\nInstalled devices:\n");
 2287         for (i = 0; i < RT_COUNT; i++)
 2288                 if (sc->pcm[i] != NULL)
 2289                         if (device_is_attached(sc->pcm[i])) {
 2290                                 sbuf_printf(s, "%s on %s\n", device_get_desc(sc->pcm[i]), device_get_nameunit(sc->pcm[i]));
 2291                         }
 2292         if (sc->midi[0] != NULL)
 2293                 if (device_is_attached(sc->midi[0])) {
 2294                         sbuf_printf(s, "EMU10Kx MIDI Interface\n");
 2295                         sbuf_printf(s, "\tOn-card connector on %s\n", device_get_nameunit(sc->midi[0]));
 2296                 }
 2297         if (sc->midi[1] != NULL)
 2298                 if (device_is_attached(sc->midi[1])) {
 2299                         sbuf_printf(s, "\tOn-Drive connector on %s\n", device_get_nameunit(sc->midi[1]));
 2300                 }
 2301         if (sc->midi[0] != NULL)
 2302                 if (device_is_attached(sc->midi[0])) {
 2303                         sbuf_printf(s, "\tIR reciever MIDI events %s\n", sc->enable_ir ? "enabled" : "disabled");
 2304                 }
 2305         sbuf_printf(s, "Card is in %s mode\n", (sc->mode == MODE_ANALOG) ? "analog" : "digital");
 2306 
 2307         sbuf_finish(s);
 2308         return (sbuf_len(s));
 2309 }
 2310 
 2311 /* INIT & UNINIT */
 2312 static int
 2313 emu10kx_dev_init(struct emu_sc_info *sc)
 2314 {
 2315         int unit;
 2316 
 2317         mtx_init(&sc->emu10kx_lock, device_get_nameunit(sc->dev), "kxdevlock", 0);
 2318         unit = device_get_unit(sc->dev);
 2319 
 2320         sc->cdev = make_dev(&emu10kx_cdevsw, PCMMINOR(unit), UID_ROOT, GID_WHEEL, 0640, "emu10kx%d", unit);
 2321         if (sc->cdev != NULL) {
 2322                 sc->cdev->si_drv1 = sc;
 2323                 return (0);
 2324         }
 2325         return (ENXIO);
 2326 }
 2327 
 2328 static int
 2329 emu10kx_dev_uninit(struct emu_sc_info *sc)
 2330 {
 2331         mtx_lock(&sc->emu10kx_lock);
 2332         if (sc->emu10kx_isopen) {
 2333                 mtx_unlock(&sc->emu10kx_lock);
 2334                 return (EBUSY);
 2335         }
 2336         if (sc->cdev)
 2337                 destroy_dev(sc->cdev);
 2338         sc->cdev = 0;
 2339 
 2340         mtx_destroy(&sc->emu10kx_lock);
 2341         return (0);
 2342 }
 2343 
 2344 /* resource manager */
 2345 int
 2346 emu_rm_init(struct emu_sc_info *sc)
 2347 {
 2348         int i;
 2349         int maxcount;
 2350         struct emu_rm *rm;
 2351 
 2352         rm = malloc(sizeof(struct emu_rm), M_DEVBUF, M_NOWAIT | M_ZERO);
 2353         if (rm == NULL) {
 2354                 return (ENOMEM);
 2355         }
 2356         sc->rm = rm;
 2357         rm->card = sc;
 2358         maxcount = sc->num_gprs;
 2359         rm->num_used = 0;
 2360         mtx_init(&(rm->gpr_lock), device_get_nameunit(sc->dev), "gpr alloc", MTX_DEF);
 2361         rm->num_gprs = (maxcount < EMU_MAX_GPR ? maxcount : EMU_MAX_GPR);
 2362         for (i = 0; i < rm->num_gprs; i++)
 2363                 rm->allocmap[i] = 0;
 2364         /* pre-allocate gpr[0] */
 2365         rm->allocmap[0] = 1;
 2366         rm->last_free_gpr = 1;
 2367 
 2368         return (0);
 2369 }
 2370 
 2371 int
 2372 emu_rm_uninit(struct emu_sc_info *sc)
 2373 {
 2374         int i;
 2375 
 2376         if (sc->dbg_level > 1) {
 2377                 mtx_lock(&(sc->rm->gpr_lock));
 2378                 for (i = 1; i < sc->rm->last_free_gpr; i++)
 2379                         if (sc->rm->allocmap[i] > 0)
 2380                                 device_printf(sc->dev, "rm: gpr %d not free before uninit\n", i);
 2381                 mtx_unlock(&(sc->rm->gpr_lock));
 2382         }
 2383 
 2384         mtx_destroy(&(sc->rm->gpr_lock));
 2385         free(sc->rm, M_DEVBUF);
 2386         return (0);
 2387 }
 2388 
 2389 static int
 2390 emu_rm_gpr_alloc(struct emu_rm *rm, int count)
 2391 {
 2392         int i, j;
 2393         int allocated_gpr;
 2394 
 2395         allocated_gpr = rm->num_gprs;
 2396         /* try fast way first */
 2397         mtx_lock(&(rm->gpr_lock));
 2398         if (rm->last_free_gpr + count <= rm->num_gprs) {
 2399                 allocated_gpr = rm->last_free_gpr;
 2400                 rm->last_free_gpr += count;
 2401                 rm->allocmap[allocated_gpr] = count;
 2402                 for (i = 1; i < count; i++)
 2403                         rm->allocmap[allocated_gpr + i] = -(count - i);
 2404         } else {
 2405                 /* longer */
 2406                 i = 0;
 2407                 allocated_gpr = rm->num_gprs;
 2408                 while (i < rm->last_free_gpr - count) {
 2409                         if (rm->allocmap[i] > 0) {
 2410                                 i += rm->allocmap[i];
 2411                         } else {
 2412                                 allocated_gpr = i;
 2413                                 for (j = 1; j < count; j++) {
 2414                                         if (rm->allocmap[i + j] != 0)
 2415                                                 allocated_gpr = rm->num_gprs;
 2416                                 }
 2417                                 if (allocated_gpr == i)
 2418                                         break;
 2419                         }
 2420                 }
 2421                 if (allocated_gpr + count < rm->last_free_gpr) {
 2422                         rm->allocmap[allocated_gpr] = count;
 2423                         for (i = 1; i < count; i++)
 2424                                 rm->allocmap[allocated_gpr + i] = -(count - i);
 2425 
 2426                 }
 2427         }
 2428         if (allocated_gpr == rm->num_gprs)
 2429                 allocated_gpr = (-1);
 2430         if (allocated_gpr >= 0)
 2431                 rm->num_used += count;
 2432         mtx_unlock(&(rm->gpr_lock));
 2433         return (allocated_gpr);
 2434 }
 2435 
 2436 /* mixer */
 2437 void
 2438 emumix_set_mode(struct emu_sc_info *sc, int mode)
 2439 {
 2440         uint32_t a_iocfg;
 2441         uint32_t hcfg;
 2442         uint32_t tmp;
 2443 
 2444         switch (mode) {
 2445         case MODE_DIGITAL:
 2446                 /* FALLTHROUGH */
 2447         case MODE_ANALOG:
 2448                 break;
 2449         default:
 2450                 return;
 2451         }
 2452 
 2453         hcfg = HCFG_AUDIOENABLE | HCFG_AUTOMUTE;
 2454         a_iocfg = 0;
 2455 
 2456         if (sc->rev >= 6)
 2457                 hcfg |= HCFG_JOYENABLE;
 2458 
 2459         if (sc->is_emu10k1)
 2460                 hcfg |= HCFG_LOCKTANKCACHE_MASK;
 2461         else
 2462                 hcfg |= HCFG_CODECFORMAT_I2S | HCFG_JOYENABLE;
 2463 
 2464 
 2465         if (mode == MODE_DIGITAL) {
 2466                 if (sc->broken_digital) {
 2467                         device_printf(sc->dev, "Digital mode is reported as broken on this card.\n");
 2468                 }
 2469                 a_iocfg |= A_IOCFG_ENABLE_DIGITAL;
 2470                 hcfg |= HCFG_GPOUT0;
 2471         }
 2472 
 2473         if (mode == MODE_ANALOG)
 2474                 emumix_set_spdif_mode(sc, SPDIF_MODE_PCM);
 2475 
 2476         if (sc->is_emu10k2)
 2477                 a_iocfg |= 0x80; /* XXX */
 2478 
 2479         if ((sc->is_ca0102) || (sc->is_ca0108))
 2480                 /*
 2481                  * Setting A_IOCFG_DISABLE_ANALOG will do opposite things
 2482                  * on diffrerent cards.
 2483                  * "don't disable analog outs" on Audigy 2 (ca0102/ca0108)
 2484                  * "disable analog outs" on Audigy (emu10k2)
 2485                  */
 2486                 a_iocfg |= A_IOCFG_DISABLE_ANALOG;
 2487 
 2488         if (sc->is_ca0108)
 2489                 a_iocfg |= 0x20; /* XXX */
 2490 
 2491         /* Mute analog center & subwoofer before mode change */
 2492         if (mode == MODE_DIGITAL)
 2493                 emumix_set_gpr(sc, sc->mute_gpr[ANALOGMUTE], 1);
 2494 
 2495         emu_wr(sc, HCFG, hcfg, 4);
 2496 
 2497         if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
 2498                 tmp = emu_rd(sc, A_IOCFG, 2);
 2499                 tmp = a_iocfg;
 2500                 emu_wr(sc, A_IOCFG, tmp, 2);
 2501         }
 2502 
 2503         /* Unmute if we have changed mode to analog. */
 2504 
 2505         if (mode == MODE_ANALOG)
 2506                 emumix_set_gpr(sc, sc->mute_gpr[ANALOGMUTE], 0);
 2507 
 2508         sc->mode = mode;
 2509 }
 2510 
 2511 void
 2512 emumix_set_spdif_mode(struct emu_sc_info *sc, int mode)
 2513 {
 2514         uint32_t spcs;
 2515 
 2516         switch (mode) {
 2517         case SPDIF_MODE_PCM:
 2518                 break;
 2519         case SPDIF_MODE_AC3:
 2520                 device_printf(sc->dev, "AC3 mode does not work and disabled\n");
 2521                 return;
 2522         default:
 2523                 return;
 2524         }
 2525 
 2526         spcs = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
 2527             SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
 2528             SPCS_GENERATIONSTATUS | 0x00001200 | 0x00000000 |
 2529             SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
 2530 
 2531         mode = SPDIF_MODE_PCM;
 2532 
 2533         emu_wrptr(sc, 0, SPCS0, spcs);
 2534         emu_wrptr(sc, 0, SPCS1, spcs);
 2535         emu_wrptr(sc, 0, SPCS2, spcs);
 2536 }
 2537 
 2538 #define L2L_POINTS      10
 2539 
 2540 static int l2l_df[L2L_POINTS] = {
 2541         0x572C5CA,              /* 100..90 */
 2542         0x3211625,              /* 90..80 */
 2543         0x1CC1A76,              /* 80..70 */
 2544         0x108428F,              /* 70..60 */
 2545         0x097C70A,              /* 60..50 */
 2546         0x0572C5C,              /* 50..40 */
 2547         0x0321162,              /* 40..30 */
 2548         0x01CC1A7,              /* 30..20 */
 2549         0x0108428,              /* 20..10 */
 2550         0x016493D               /* 10..0 */
 2551 };
 2552 
 2553 static int l2l_f[L2L_POINTS] = {
 2554         0x4984461A,             /* 90 */
 2555         0x2A3968A7,             /* 80 */
 2556         0x18406003,             /* 70 */
 2557         0x0DEDC66D,             /* 60 */
 2558         0x07FFFFFF,             /* 50 */
 2559         0x04984461,             /* 40 */
 2560         0x02A3968A,             /* 30 */
 2561         0x01840600,             /* 20 */
 2562         0x00DEDC66,             /* 10 */
 2563         0x00000000              /* 0 */
 2564 };
 2565 
 2566 
 2567 static int
 2568 log2lin(int log_t)
 2569 {
 2570         int lin_t;
 2571         int idx, lin;
 2572 
 2573         if (log_t <= 0) {
 2574                 lin_t = 0x00000000;
 2575                 return (lin_t);
 2576         }
 2577 
 2578         if (log_t >= 100) {
 2579                 lin_t = 0x7fffffff;
 2580                 return (lin_t);
 2581         }
 2582 
 2583         idx = (L2L_POINTS - 1) - log_t / (L2L_POINTS);
 2584         lin = log_t % (L2L_POINTS);
 2585         lin_t = l2l_df[idx] * lin + l2l_f[idx];
 2586         return (lin_t);
 2587 }
 2588 
 2589 
 2590 void
 2591 emumix_set_fxvol(struct emu_sc_info *sc, unsigned gpr, int32_t vol)
 2592 {
 2593 
 2594         vol = log2lin(vol);
 2595         emumix_set_gpr(sc, gpr, vol);
 2596 }
 2597 
 2598 void
 2599 emumix_set_gpr(struct emu_sc_info *sc, unsigned gpr, int32_t val)
 2600 {
 2601         if (sc->dbg_level > 1)
 2602                 if (gpr == 0) {
 2603                         device_printf(sc->dev, "Zero gpr write access\n");
 2604 #ifdef KDB
 2605                         kdb_backtrace();
 2606 #endif
 2607                         return;
 2608                         }
 2609 
 2610         emu_wrptr(sc, 0, GPR(gpr), val);
 2611 }
 2612 
 2613 void
 2614 emumix_set_volume(struct emu_sc_info *sc, int mixer_idx, int volume)
 2615 {
 2616 
 2617         RANGE(volume, 0, 100);
 2618         if (mixer_idx < NUM_MIXERS) {
 2619                 sc->mixer_volcache[mixer_idx] = volume;
 2620                 emumix_set_fxvol(sc, sc->mixer_gpr[mixer_idx], volume);
 2621         }
 2622 }
 2623 
 2624 int
 2625 emumix_get_volume(struct emu_sc_info *sc, int mixer_idx)
 2626 {
 2627         if ((mixer_idx < NUM_MIXERS) && (mixer_idx >= 0))
 2628                 return (sc->mixer_volcache[mixer_idx]);
 2629         return (-1);
 2630 }
 2631 
 2632 /* Init CardBus part */
 2633 static int
 2634 emu_cardbus_init(struct emu_sc_info *sc)
 2635 {
 2636 
 2637         /*
 2638          * XXX May not need this if we have IPR3 handler.
 2639          * Is it a real init calls, or IPR3 interrupt acknowledgments?
 2640          * Looks much like "(data << 16) | register".
 2641          */
 2642         emu_wr_cbptr(sc, (0x00d0 << 16) | 0x0000);
 2643         emu_wr_cbptr(sc, (0x00d0 << 16) | 0x0001);
 2644         emu_wr_cbptr(sc, (0x00d0 << 16) | 0x005f);
 2645         emu_wr_cbptr(sc, (0x00d0 << 16) | 0x007f);
 2646 
 2647         emu_wr_cbptr(sc, (0x0090 << 16) | 0x007f);
 2648 
 2649         return (0);
 2650 }
 2651 
 2652 /* Probe and attach the card */
 2653 static int
 2654 emu_init(struct emu_sc_info *sc)
 2655 {
 2656         uint32_t ch, tmp;
 2657         uint32_t spdif_sr;
 2658         uint32_t ac97slot;
 2659         int def_mode;
 2660         int i;
 2661 
 2662         /* disable audio and lock cache */
 2663         emu_wr(sc, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, 4);
 2664 
 2665         /* reset recording buffers */
 2666         emu_wrptr(sc, 0, MICBS, ADCBS_BUFSIZE_NONE);
 2667         emu_wrptr(sc, 0, MICBA, 0);
 2668         emu_wrptr(sc, 0, FXBS, ADCBS_BUFSIZE_NONE);
 2669         emu_wrptr(sc, 0, FXBA, 0);
 2670         emu_wrptr(sc, 0, ADCBS, ADCBS_BUFSIZE_NONE);
 2671         emu_wrptr(sc, 0, ADCBA, 0);
 2672 
 2673         /* disable channel interrupt */
 2674         emu_wr(sc, INTE, INTE_INTERVALTIMERENB | INTE_SAMPLERATETRACKER | INTE_PCIERRORENABLE, 4);
 2675         emu_wrptr(sc, 0, CLIEL, 0);
 2676         emu_wrptr(sc, 0, CLIEH, 0);
 2677         emu_wrptr(sc, 0, SOLEL, 0);
 2678         emu_wrptr(sc, 0, SOLEH, 0);
 2679 
 2680         /* disable P16V and S/PDIF interrupts */
 2681         if ((sc->is_ca0102) || (sc->is_ca0108))
 2682                 emu_wr(sc, INTE2, 0, 4);
 2683 
 2684         if (sc->is_ca0102)
 2685                 emu_wr(sc, INTE3, 0, 4);
 2686 
 2687         /* init phys inputs and outputs */
 2688         ac97slot = 0;
 2689         if (sc->has_51)
 2690                 ac97slot = AC97SLOT_CNTR | AC97SLOT_LFE;
 2691         if (sc->has_71)
 2692                 ac97slot = AC97SLOT_CNTR | AC97SLOT_LFE | AC97SLOT_REAR_LEFT | AC97SLOT_REAR_RIGHT;
 2693         if (sc->is_emu10k2)
 2694                 ac97slot |= 0x40;
 2695         emu_wrptr(sc, 0, AC97SLOT, ac97slot);
 2696 
 2697         if (sc->is_emu10k2)     /* XXX for later cards? */
 2698                 emu_wrptr(sc, 0, SPBYPASS, 0xf00);      /* What will happen if
 2699                                                          * we write 1 here? */
 2700 
 2701         if (bus_dma_tag_create( /* parent */ bus_get_dma_tag(sc->dev),
 2702              /* alignment */ 2, /* boundary */ 0,
 2703              /* lowaddr */ 1 << 31,     /* can only access 0-2gb */
 2704              /* highaddr */ BUS_SPACE_MAXADDR,
 2705              /* filter */ NULL, /* filterarg */ NULL,
 2706              /* maxsize */ EMU_MAX_BUFSZ, /* nsegments */ 1, /* maxsegz */ 0x3ffff,
 2707              /* flags */ 0, /* lockfunc */ busdma_lock_mutex,
 2708              /* lockarg */ &Giant, &(sc->mem.dmat)) != 0) {
 2709                 device_printf(sc->dev, "unable to create dma tag\n");
 2710                 bus_dma_tag_destroy(sc->mem.dmat);
 2711                 return (ENOMEM);
 2712         }
 2713 
 2714         sc->mem.card = sc;
 2715         SLIST_INIT(&sc->mem.blocks);
 2716         sc->mem.ptb_pages = emu_malloc(&sc->mem, EMU_MAXPAGES * sizeof(uint32_t), &sc->mem.ptb_pages_addr);
 2717         if (sc->mem.ptb_pages == NULL)
 2718                 return (ENOMEM);
 2719 
 2720         sc->mem.silent_page = emu_malloc(&sc->mem, EMUPAGESIZE, &sc->mem.silent_page_addr);
 2721         if (sc->mem.silent_page == NULL) {
 2722                 emu_free(&sc->mem, sc->mem.ptb_pages);
 2723                 return (ENOMEM);
 2724         }
 2725         /* Clear page with silence & setup all pointers to this page */
 2726         bzero(sc->mem.silent_page, EMUPAGESIZE);
 2727         tmp = (uint32_t) (sc->mem.silent_page_addr) << 1;
 2728         for (i = 0; i < EMU_MAXPAGES; i++)
 2729                 sc->mem.ptb_pages[i] = tmp | i;
 2730 
 2731         for (ch = 0; ch < NUM_G; ch++) {
 2732                 emu_wrptr(sc, ch, MAPA, tmp | MAP_PTI_MASK);
 2733                 emu_wrptr(sc, ch, MAPB, tmp | MAP_PTI_MASK);
 2734         }
 2735         emu_wrptr(sc, 0, PTB, (sc->mem.ptb_pages_addr));
 2736         emu_wrptr(sc, 0, TCB, 0);       /* taken from original driver */
 2737         emu_wrptr(sc, 0, TCBS, 0);      /* taken from original driver */
 2738 
 2739         /* init envelope engine */
 2740         for (ch = 0; ch < NUM_G; ch++) {
 2741                 emu_wrptr(sc, ch, DCYSUSV, 0);
 2742                 emu_wrptr(sc, ch, IP, 0);
 2743                 emu_wrptr(sc, ch, VTFT, 0xffff);
 2744                 emu_wrptr(sc, ch, CVCF, 0xffff);
 2745                 emu_wrptr(sc, ch, PTRX, 0);
 2746                 emu_wrptr(sc, ch, CPF, 0);
 2747                 emu_wrptr(sc, ch, CCR, 0);
 2748 
 2749                 emu_wrptr(sc, ch, PSST, 0);
 2750                 emu_wrptr(sc, ch, DSL, 0x10);
 2751                 emu_wrptr(sc, ch, CCCA, 0);
 2752                 emu_wrptr(sc, ch, Z1, 0);
 2753                 emu_wrptr(sc, ch, Z2, 0);
 2754                 emu_wrptr(sc, ch, FXRT, 0xd01c0000);
 2755 
 2756                 emu_wrptr(sc, ch, ATKHLDM, 0);
 2757                 emu_wrptr(sc, ch, DCYSUSM, 0);
 2758                 emu_wrptr(sc, ch, IFATN, 0xffff);
 2759                 emu_wrptr(sc, ch, PEFE, 0);
 2760                 emu_wrptr(sc, ch, FMMOD, 0);
 2761                 emu_wrptr(sc, ch, TREMFRQ, 24); /* 1 Hz */
 2762                 emu_wrptr(sc, ch, FM2FRQ2, 24); /* 1 Hz */
 2763                 emu_wrptr(sc, ch, TEMPENV, 0);
 2764 
 2765                 /*** these are last so OFF prevents writing ***/
 2766                 emu_wrptr(sc, ch, LFOVAL2, 0);
 2767                 emu_wrptr(sc, ch, LFOVAL1, 0);
 2768                 emu_wrptr(sc, ch, ATKHLDV, 0);
 2769                 emu_wrptr(sc, ch, ENVVOL, 0);
 2770                 emu_wrptr(sc, ch, ENVVAL, 0);
 2771 
 2772                 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
 2773                         emu_wrptr(sc, ch, 0x4c, 0x0);
 2774                         emu_wrptr(sc, ch, 0x4d, 0x0);
 2775                         emu_wrptr(sc, ch, 0x4e, 0x0);
 2776                         emu_wrptr(sc, ch, 0x4f, 0x0);
 2777                         emu_wrptr(sc, ch, A_FXRT1, 0x3f3f3f3f);
 2778                         emu_wrptr(sc, ch, A_FXRT2, 0x3f3f3f3f);
 2779                         emu_wrptr(sc, ch, A_SENDAMOUNTS, 0x0);
 2780                 }
 2781         }
 2782 
 2783         emumix_set_spdif_mode(sc, SPDIF_MODE_PCM);
 2784 
 2785         if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108))
 2786                 emu_wrptr(sc, 0, A_SPDIF_SAMPLERATE, A_SPDIF_48000);
 2787 
 2788         /*
 2789          * CAxxxx cards needs additional setup:
 2790          * 1. Set I2S capture sample rate to 96000
 2791          * 2. Disable P16v / P17v proceesing
 2792          * 3. Allow EMU10K DSP inputs
 2793          */
 2794         if ((sc->is_ca0102) || (sc->is_ca0108)) {
 2795 
 2796                 spdif_sr = emu_rdptr(sc, 0, A_SPDIF_SAMPLERATE);
 2797                 spdif_sr &= 0xfffff1ff;
 2798                 spdif_sr |= A_I2S_CAPTURE_96000;
 2799                 emu_wrptr(sc, 0, A_SPDIF_SAMPLERATE, spdif_sr);
 2800 
 2801                 /* Disable P16v processing */
 2802                 emu_wr_p16vptr(sc, 0, SRCSel, 0x14);
 2803 
 2804                 /* Setup P16v/P17v sound routing */
 2805                 if (sc->is_ca0102)
 2806                         emu_wr_p16vptr(sc, 0, SRCMULTI_ENABLE, 0xFF00FF00);
 2807                 else {
 2808                         emu_wr_p16vptr(sc, 0, P17V_MIXER_I2S_ENABLE, 0xFF000000);
 2809                         emu_wr_p16vptr(sc, 0, P17V_MIXER_SPDIF_ENABLE, 0xFF000000);
 2810 
 2811                         tmp = emu_rd(sc, A_IOCFG, 2);
 2812                         emu_wr(sc, A_IOCFG, tmp & ~0x8, 2);
 2813                 }
 2814         }
 2815         emu_initefx(sc);
 2816 
 2817         def_mode = MODE_ANALOG;
 2818         if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108))
 2819                 def_mode = MODE_DIGITAL;
 2820         if (((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) && (sc->broken_digital)) {
 2821                 device_printf(sc->dev, "Audigy card initialized in analog mode.\n");
 2822                 def_mode = MODE_ANALOG;
 2823         }
 2824         emumix_set_mode(sc, def_mode);
 2825 
 2826         if (bootverbose) {
 2827                 tmp = emu_rd(sc, HCFG, 4);
 2828                 device_printf(sc->dev, "Card Configuration (   0x%08x )\n", tmp);
 2829                 device_printf(sc->dev, "Card Configuration ( & 0xff000000 ) : %s%s%s%s%s%s%s%s\n",
 2830                     (tmp & 0x80000000 ? "[Legacy MPIC] " : ""),
 2831                     (tmp & 0x40000000 ? "[0x40] " : ""),
 2832                     (tmp & 0x20000000 ? "[0x20] " : ""),
 2833                     (tmp & 0x10000000 ? "[0x10] " : ""),
 2834                     (tmp & 0x08000000 ? "[0x08] " : ""),
 2835                     (tmp & 0x04000000 ? "[0x04] " : ""),
 2836                     (tmp & 0x02000000 ? "[0x02] " : ""),
 2837                     (tmp & 0x01000000 ? "[0x01]" : " "));
 2838                 device_printf(sc->dev, "Card Configuration ( & 0x00ff0000 ) : %s%s%s%s%s%s%s%s\n",
 2839                     (tmp & 0x00800000 ? "[0x80] " : ""),
 2840                     (tmp & 0x00400000 ? "[0x40] " : ""),
 2841                     (tmp & 0x00200000 ? "[Legacy INT] " : ""),
 2842                     (tmp & 0x00100000 ? "[0x10] " : ""),
 2843                     (tmp & 0x00080000 ? "[0x08] " : ""),
 2844                     (tmp & 0x00040000 ? "[Codec4] " : ""),
 2845                     (tmp & 0x00020000 ? "[Codec2] " : ""),
 2846                     (tmp & 0x00010000 ? "[I2S Codec]" : " "));
 2847                 device_printf(sc->dev, "Card Configuration ( & 0x0000ff00 ) : %s%s%s%s%s%s%s%s\n",
 2848                     (tmp & 0x00008000 ? "[0x80] " : ""),
 2849                     (tmp & 0x00004000 ? "[GPINPUT0] " : ""),
 2850                     (tmp & 0x00002000 ? "[GPINPUT1] " : ""),
 2851                     (tmp & 0x00001000 ? "[GPOUT0] " : ""),
 2852                     (tmp & 0x00000800 ? "[GPOUT1] " : ""),
 2853                     (tmp & 0x00000400 ? "[GPOUT2] " : ""),
 2854                     (tmp & 0x00000200 ? "[Joystick] " : ""),
 2855                     (tmp & 0x00000100 ? "[0x01]" : " "));
 2856                 device_printf(sc->dev, "Card Configuration ( & 0x000000ff ) : %s%s%s%s%s%s%s%s\n",
 2857                     (tmp & 0x00000080 ? "[0x80] " : ""),
 2858                     (tmp & 0x00000040 ? "[0x40] " : ""),
 2859                     (tmp & 0x00000020 ? "[0x20] " : ""),
 2860                     (tmp & 0x00000010 ? "[AUTOMUTE] " : ""),
 2861                     (tmp & 0x00000008 ? "[LOCKSOUNDCACHE] " : ""),
 2862                     (tmp & 0x00000004 ? "[LOCKTANKCACHE] " : ""),
 2863                     (tmp & 0x00000002 ? "[MUTEBUTTONENABLE] " : ""),
 2864                     (tmp & 0x00000001 ? "[AUDIOENABLE]" : " "));
 2865 
 2866                 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
 2867                         tmp = emu_rd(sc, A_IOCFG, 2);
 2868                         device_printf(sc->dev, "Audigy Card Configuration (    0x%04x )\n", tmp);
 2869                         device_printf(sc->dev, "Audigy Card Configuration (  & 0xff00 )");
 2870                         printf(" : %s%s%s%s%s%s%s%s\n",
 2871                             (tmp & 0x8000 ? "[Rear Speakers] " : ""),
 2872                             (tmp & 0x4000 ? "[Front Speakers] " : ""),
 2873                             (tmp & 0x2000 ? "[0x20] " : ""),
 2874                             (tmp & 0x1000 ? "[0x10] " : ""),
 2875                             (tmp & 0x0800 ? "[0x08] " : ""),
 2876                             (tmp & 0x0400 ? "[0x04] " : ""),
 2877                             (tmp & 0x0200 ? "[0x02] " : ""),
 2878                             (tmp & 0x0100 ? "[AudigyDrive Phones]" : " "));
 2879                         device_printf(sc->dev, "Audigy Card Configuration (  & 0x00ff )");
 2880                         printf(" : %s%s%s%s%s%s%s%s\n",
 2881                             (tmp & 0x0080 ? "[0x80] " : ""),
 2882                             (tmp & 0x0040 ? "[Mute AnalogOut] " : ""),
 2883                             (tmp & 0x0020 ? "[0x20] " : ""),
 2884                             (tmp & 0x0010 ? "[0x10] " : ""),
 2885                             (tmp & 0x0008 ? "[0x08] " : ""),
 2886                             (tmp & 0x0004 ? "[GPOUT0] " : ""),
 2887                             (tmp & 0x0002 ? "[GPOUT1] " : ""),
 2888                             (tmp & 0x0001 ? "[GPOUT2]" : " "));
 2889                 }               /* is_emu10k2 or ca* */
 2890         }                       /* bootverbose */
 2891         return (0);
 2892 }
 2893 
 2894 static int
 2895 emu_uninit(struct emu_sc_info *sc)
 2896 {
 2897         uint32_t ch;
 2898         struct emu_memblk *blk;
 2899 
 2900         emu_wr(sc, INTE, 0, 4);
 2901         for (ch = 0; ch < NUM_G; ch++)
 2902                 emu_wrptr(sc, ch, DCYSUSV, 0);
 2903         for (ch = 0; ch < NUM_G; ch++) {
 2904                 emu_wrptr(sc, ch, VTFT, 0);
 2905                 emu_wrptr(sc, ch, CVCF, 0);
 2906                 emu_wrptr(sc, ch, PTRX, 0);
 2907                 emu_wrptr(sc, ch, CPF, 0);
 2908         }
 2909 
 2910         /* disable audio and lock cache */
 2911         emu_wr(sc, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, 4);
 2912 
 2913         emu_wrptr(sc, 0, PTB, 0);
 2914         /* reset recording buffers */
 2915         emu_wrptr(sc, 0, MICBS, ADCBS_BUFSIZE_NONE);
 2916         emu_wrptr(sc, 0, MICBA, 0);
 2917         emu_wrptr(sc, 0, FXBS, ADCBS_BUFSIZE_NONE);
 2918         emu_wrptr(sc, 0, FXBA, 0);
 2919         emu_wrptr(sc, 0, FXWC, 0);
 2920         emu_wrptr(sc, 0, ADCBS, ADCBS_BUFSIZE_NONE);
 2921         emu_wrptr(sc, 0, ADCBA, 0);
 2922         emu_wrptr(sc, 0, TCB, 0);
 2923         emu_wrptr(sc, 0, TCBS, 0);
 2924 
 2925         /* disable channel interrupt */
 2926         emu_wrptr(sc, 0, CLIEL, 0);
 2927         emu_wrptr(sc, 0, CLIEH, 0);
 2928         emu_wrptr(sc, 0, SOLEL, 0);
 2929         emu_wrptr(sc, 0, SOLEH, 0);
 2930 
 2931         if (!SLIST_EMPTY(&sc->mem.blocks))
 2932                 device_printf(sc->dev, "warning: memblock list not empty\n");
 2933 
 2934         SLIST_FOREACH(blk, &sc->mem.blocks, link)
 2935                 if (blk != NULL)
 2936                 device_printf(sc->dev, "lost %d for %s\n", blk->pte_size, blk->owner);
 2937 
 2938         emu_free(&sc->mem, sc->mem.ptb_pages);
 2939         emu_free(&sc->mem, sc->mem.silent_page);
 2940 
 2941         return (0);
 2942 }
 2943 
 2944 static int
 2945 emu_read_ivar(device_t bus, device_t dev, int ivar_index, uintptr_t * result)
 2946 {
 2947         struct sndcard_func *func = device_get_ivars(dev);
 2948         struct emu_sc_info *sc = device_get_softc(bus);
 2949 
 2950         if (func==NULL)
 2951                 return (ENOMEM);
 2952         if (sc == NULL)
 2953                 return (ENOMEM);
 2954 
 2955         switch (ivar_index) {
 2956         case EMU_VAR_FUNC:
 2957                 *result = func->func;
 2958                 break;
 2959         case EMU_VAR_ROUTE:
 2960                 if (func->varinfo == NULL)
 2961                         return (ENOMEM);
 2962                 *result = ((struct emu_pcminfo *)func->varinfo)->route;
 2963                 break;
 2964         case EMU_VAR_ISEMU10K1:
 2965                 *result = sc->is_emu10k1;
 2966                 break;
 2967         case EMU_VAR_MCH_DISABLED:
 2968                 *result = sc->mch_disabled;
 2969                 break;
 2970         case EMU_VAR_MCH_REC:
 2971                 *result = sc->mch_rec;
 2972                 break;
 2973         default:
 2974                 return (ENOENT);
 2975         }
 2976 
 2977         return (0);
 2978 }
 2979 
 2980 static int
 2981 emu_write_ivar(device_t bus __unused, device_t dev __unused,
 2982     int ivar_index, uintptr_t value __unused)
 2983 {
 2984 
 2985         switch (ivar_index) {
 2986                 case 0:
 2987                 return (EINVAL);
 2988 
 2989         default:
 2990                 return (ENOENT);
 2991         }
 2992 }
 2993 
 2994 static int
 2995 emu_pci_probe(device_t dev)
 2996 {
 2997         struct sbuf *s;
 2998         unsigned int thiscard = 0;
 2999         uint16_t vendor;
 3000 
 3001         vendor = pci_read_config(dev, PCIR_DEVVENDOR, /* bytes */ 2);
 3002         if (vendor != 0x1102)
 3003                 return (ENXIO); /* Not Creative */
 3004 
 3005         thiscard = emu_getcard(dev);
 3006         if (thiscard == 0)
 3007                 return (ENXIO);
 3008 
 3009         s = sbuf_new(NULL, NULL, 4096, 0);
 3010         if (s == NULL)
 3011                 return (ENOMEM);
 3012         sbuf_printf(s, "Creative %s [%s]", emu_cards[thiscard].desc, emu_cards[thiscard].SBcode);
 3013         sbuf_finish(s);
 3014 
 3015         device_set_desc_copy(dev, sbuf_data(s));
 3016 
 3017         sbuf_delete(s);
 3018 
 3019         return (BUS_PROBE_DEFAULT);
 3020 }
 3021 
 3022 
 3023 static int
 3024 emu_pci_attach(device_t dev)
 3025 {
 3026         struct sndcard_func *func;
 3027         struct emu_sc_info *sc;
 3028         struct emu_pcminfo *pcminfo;
 3029 #if 0
 3030         struct emu_midiinfo *midiinfo;
 3031 #endif
 3032         uint32_t data;
 3033         int i;
 3034         int device_flags;
 3035         char status[255];
 3036         int error = ENXIO;
 3037         int unit;
 3038 
 3039         sc = device_get_softc(dev);
 3040         unit = device_get_unit(dev);
 3041 
 3042         if (resource_disabled("emu10kx", unit)) {
 3043                 device_printf(dev, "disabled by kernel hints\n");
 3044                 return (ENXIO); /* XXX to avoid unit reuse */
 3045         }
 3046 
 3047         /* Get configuration */
 3048 
 3049         sc->ctx = device_get_sysctl_ctx(dev);
 3050         if (sc->ctx == NULL)
 3051                 goto bad;
 3052         sc->root = device_get_sysctl_tree(dev);
 3053         if (sc->root == NULL)
 3054                 goto bad;
 3055 
 3056         if (resource_int_value("emu10kx", unit, "multichannel_disabled", &(sc->mch_disabled)))
 3057                 RANGE(sc->mch_disabled, 0, 1);
 3058         SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
 3059             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
 3060             OID_AUTO, "multichannel_disabled", CTLFLAG_RD, &(sc->mch_disabled), 0, "Multichannel playback setting");
 3061 
 3062         if (resource_int_value("emu10kx", unit, "multichannel_recording", &(sc->mch_rec)))
 3063                 RANGE(sc->mch_rec, 0, 1);
 3064         SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
 3065             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
 3066             OID_AUTO, "multichannel_recording", CTLFLAG_RD,  &(sc->mch_rec), 0, "Multichannel recording setting");
 3067 
 3068         if (resource_int_value("emu10kx", unit, "debug", &(sc->dbg_level)))
 3069                 RANGE(sc->mch_rec, 0, 2);
 3070         SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
 3071             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
 3072             OID_AUTO, "debug", CTLFLAG_RW, &(sc->dbg_level), 0, "Debug level");
 3073 
 3074         /* Fill in the softc. */
 3075         mtx_init(&sc->lock, device_get_nameunit(dev), "bridge conf", MTX_DEF);
 3076         mtx_init(&sc->rw, device_get_nameunit(dev), "exclusive io", MTX_DEF);
 3077         sc->dev = dev;
 3078         sc->type = pci_get_devid(dev);
 3079         sc->rev = pci_get_revid(dev);
 3080         sc->enable_ir = 0;
 3081         sc->has_ac97 = 0;
 3082         sc->has_51 = 0;
 3083         sc->has_71 = 0;
 3084         sc->broken_digital = 0;
 3085         sc->is_emu10k1 = 0;
 3086         sc->is_emu10k2 = 0;
 3087         sc->is_ca0102 = 0;
 3088         sc->is_ca0108 = 0;
 3089         sc->is_cardbus = 0;
 3090 
 3091         device_flags = emu_cards[emu_getcard(dev)].flags;
 3092         if (device_flags & HAS_51)
 3093                 sc->has_51 = 1;
 3094         if (device_flags & HAS_71) {
 3095                 sc->has_51 = 1;
 3096                 sc->has_71 = 1;
 3097         }
 3098         if (device_flags & IS_EMU10K1)
 3099                 sc->is_emu10k1 = 1;
 3100         if (device_flags & IS_EMU10K2)
 3101                 sc->is_emu10k2 = 1;
 3102         if (device_flags & IS_CA0102)
 3103                 sc->is_ca0102 = 1;
 3104         if (device_flags & IS_CA0108)
 3105                 sc->is_ca0108 = 1;
 3106         if ((sc->is_emu10k2) && (sc->rev == 4)) {
 3107                 sc->is_emu10k2 = 0;
 3108                 sc->is_ca0102 = 1;      /* for unknown Audigy 2 cards */
 3109         }
 3110         if ((sc->is_ca0102 == 1) || (sc->is_ca0108 == 1))
 3111                 if (device_flags & IS_CARDBUS)
 3112                         sc->is_cardbus = 1;
 3113 
 3114         if ((sc->is_emu10k1 + sc->is_emu10k2 + sc->is_ca0102 + sc->is_ca0108) != 1) {
 3115                 device_printf(sc->dev, "Unable to detect HW chipset\n");
 3116                 goto bad;
 3117         }
 3118         if (device_flags & BROKEN_DIGITAL)
 3119                 sc->broken_digital = 1;
 3120         if (device_flags & HAS_AC97)
 3121                 sc->has_ac97 = 1;
 3122 
 3123         sc->opcode_shift = 0;
 3124         if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
 3125                 sc->opcode_shift = 24;
 3126                 sc->high_operand_shift = 12;
 3127 
 3128         /*      DSP map                         */
 3129         /*      sc->fx_base = 0x0               */
 3130                 sc->input_base = 0x40;
 3131         /*      sc->p16vinput_base = 0x50;      */
 3132                 sc->output_base = 0x60;
 3133                 sc->efxc_base = 0x80;
 3134         /*      sc->output32h_base = 0xa0;      */
 3135         /*      sc->output32l_base = 0xb0;      */
 3136                 sc->dsp_zero = 0xc0;
 3137         /*      0xe0...0x100 are unknown        */
 3138         /*      sc->tram_base = 0x200           */
 3139         /*      sc->tram_addr_base = 0x300      */
 3140                 sc->gpr_base = A_FXGPREGBASE;
 3141                 sc->num_gprs = 0x200;
 3142                 sc->code_base = A_MICROCODEBASE;
 3143                 sc->code_size = 0x800 / 2;      /* 0x600-0xdff,  2048 words,
 3144                                                  * 1024 instructions */
 3145 
 3146                 sc->mchannel_fx = 8;
 3147                 sc->num_fxbuses = 16;
 3148                 sc->num_inputs = 8;
 3149                 sc->num_outputs = 16;
 3150                 sc->address_mask = A_PTR_ADDRESS_MASK;
 3151         }
 3152         if (sc->is_emu10k1) {
 3153                 sc->has_51 = 0; /* We don't support 5.1 sound on SB Live! 5.1 */
 3154                 sc->opcode_shift = 20;
 3155                 sc->high_operand_shift = 10;
 3156                 sc->code_base = MICROCODEBASE;
 3157                 sc->code_size = 0x400 / 2;      /* 0x400-0x7ff,  1024 words,
 3158                                                  * 512 instructions */
 3159                 sc->gpr_base = FXGPREGBASE;
 3160                 sc->num_gprs = 0x100;
 3161                 sc->input_base = 0x10;
 3162                 sc->output_base = 0x20;
 3163                 /*
 3164                  * XXX 5.1 Analog outputs are inside efxc address space!
 3165                  * They use ouput+0x11/+0x12 (=efxc+1/+2).
 3166                  * Don't use this efx registers for recording on SB Live! 5.1!
 3167                  */
 3168                 sc->efxc_base = 0x30;
 3169                 sc->dsp_zero = 0x40;
 3170                 sc->mchannel_fx = 0;
 3171                 sc->num_fxbuses = 8;
 3172                 sc->num_inputs = 8;
 3173                 sc->num_outputs = 16;
 3174                 sc->address_mask = PTR_ADDRESS_MASK;
 3175         }
 3176         if (sc->opcode_shift == 0)
 3177                 goto bad;
 3178 
 3179         data = pci_read_config(dev, PCIR_COMMAND, 2);
 3180         data |= (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN);
 3181         pci_write_config(dev, PCIR_COMMAND, data, 2);
 3182         data = pci_read_config(dev, PCIR_COMMAND, 2);
 3183 
 3184         pci_enable_busmaster(dev);
 3185 
 3186         i = PCIR_BAR(0);
 3187         sc->reg = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &i, RF_ACTIVE);
 3188         if (sc->reg == NULL) {
 3189                 device_printf(dev, "unable to map register space\n");
 3190                 goto bad;
 3191         }
 3192         sc->st = rman_get_bustag(sc->reg);
 3193         sc->sh = rman_get_bushandle(sc->reg);
 3194 
 3195         for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++)
 3196                 sc->timer[i] = 0;       /* disable it */
 3197 
 3198         i = 0;
 3199         sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i, RF_ACTIVE | RF_SHAREABLE);
 3200         if ((sc->irq == NULL) || bus_setup_intr(dev, sc->irq, INTR_MPSAFE | INTR_TYPE_AV,
 3201 #if __FreeBSD_version >= 700031
 3202             NULL,
 3203 #endif
 3204             emu_intr, sc, &sc->ih)) {
 3205                 device_printf(dev, "unable to map interrupt\n");
 3206                 goto bad;
 3207         }
 3208         if (emu_rm_init(sc) != 0) {
 3209                 device_printf(dev, "unable to create resource manager\n");
 3210                 goto bad;
 3211         }
 3212         if (sc->is_cardbus)
 3213                 if (emu_cardbus_init(sc) != 0) {
 3214                         device_printf(dev, "unable to initialize CardBus interface\n");
 3215                         goto bad;
 3216                 }
 3217         if (emu_init(sc) != 0) {
 3218                 device_printf(dev, "unable to initialize the card\n");
 3219                 goto bad;
 3220         }
 3221         if (emu10kx_dev_init(sc) != 0) {
 3222                 device_printf(dev, "unable to create control device\n");
 3223                 goto bad;
 3224         }
 3225         snprintf(status, 255, "rev %d at io 0x%lx irq %ld", sc->rev, rman_get_start(sc->reg), rman_get_start(sc->irq));
 3226 
 3227         /* Voices */
 3228         for (i = 0; i < NUM_G; i++) {
 3229                 sc->voice[i].vnum = i;
 3230                 sc->voice[i].slave = NULL;
 3231                 sc->voice[i].busy = 0;
 3232                 sc->voice[i].ismaster = 0;
 3233                 sc->voice[i].running = 0;
 3234                 sc->voice[i].b16 = 0;
 3235                 sc->voice[i].stereo = 0;
 3236                 sc->voice[i].speed = 0;
 3237                 sc->voice[i].start = 0;
 3238                 sc->voice[i].end = 0;
 3239         }
 3240 
 3241         /* PCM Audio */
 3242         for (i = 0; i < RT_COUNT; i++)
 3243                 sc->pcm[i] = NULL;
 3244 
 3245         /* FRONT */
 3246         func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
 3247         if (func == NULL) {
 3248                 error = ENOMEM;
 3249                 goto bad;
 3250         }
 3251         pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
 3252         if (pcminfo == NULL) {
 3253                 error = ENOMEM;
 3254                 goto bad;
 3255         }
 3256         pcminfo->card = sc;
 3257         pcminfo->route = RT_FRONT;
 3258 
 3259         func->func = SCF_PCM;
 3260         func->varinfo = pcminfo;
 3261         sc->pcm[RT_FRONT] = device_add_child(dev, "pcm", -1);
 3262         device_set_ivars(sc->pcm[RT_FRONT], func);
 3263 
 3264         if (!(sc->mch_disabled)) {
 3265                 /* REAR */
 3266                 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
 3267                 if (func == NULL) {
 3268                         error = ENOMEM;
 3269                         goto bad;
 3270                 }
 3271                 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
 3272                 if (pcminfo == NULL) {
 3273                         error = ENOMEM;
 3274                         goto bad;
 3275                 }
 3276                 pcminfo->card = sc;
 3277                 pcminfo->route = RT_REAR;
 3278 
 3279                 func->func = SCF_PCM;
 3280                 func->varinfo = pcminfo;
 3281                 sc->pcm[RT_REAR] = device_add_child(dev, "pcm", -1);
 3282                 device_set_ivars(sc->pcm[RT_REAR], func);
 3283                 if (sc->has_51) {
 3284                         /* CENTER */
 3285                         func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
 3286                         if (func == NULL) {
 3287                                 error = ENOMEM;
 3288                                 goto bad;
 3289                         }
 3290                         pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
 3291                         if (pcminfo == NULL) {
 3292                                 error = ENOMEM;
 3293                                 goto bad;
 3294                         }
 3295                         pcminfo->card = sc;
 3296                         pcminfo->route = RT_CENTER;
 3297 
 3298                         func->func = SCF_PCM;
 3299                         func->varinfo = pcminfo;
 3300                         sc->pcm[RT_CENTER] = device_add_child(dev, "pcm", -1);
 3301                         device_set_ivars(sc->pcm[RT_CENTER], func);
 3302                         /* SUB */
 3303                         func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
 3304                         if (func == NULL) {
 3305                                 error = ENOMEM;
 3306                                 goto bad;
 3307                         }
 3308                         pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
 3309                         if (pcminfo == NULL) {
 3310                                 error = ENOMEM;
 3311                                 goto bad;
 3312                         }
 3313                         pcminfo->card = sc;
 3314                         pcminfo->route = RT_SUB;
 3315 
 3316                         func->func = SCF_PCM;
 3317                         func->varinfo = pcminfo;
 3318                         sc->pcm[RT_SUB] = device_add_child(dev, "pcm", -1);
 3319                         device_set_ivars(sc->pcm[RT_SUB], func);
 3320                 }
 3321                 if (sc->has_71) {
 3322                         /* SIDE */
 3323                         func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
 3324                         if (func == NULL) {
 3325                                 error = ENOMEM;
 3326                                 goto bad;
 3327                         }
 3328                         pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
 3329                         if (pcminfo == NULL) {
 3330                                 error = ENOMEM;
 3331                                 goto bad;
 3332                         }
 3333                         pcminfo->card = sc;
 3334                         pcminfo->route = RT_SIDE;
 3335 
 3336                         func->func = SCF_PCM;
 3337                         func->varinfo = pcminfo;
 3338                         sc->pcm[RT_SIDE] = device_add_child(dev, "pcm", -1);
 3339                         device_set_ivars(sc->pcm[RT_SIDE], func);
 3340                 }
 3341         } /* mch_disabled */
 3342 
 3343         if (sc->mch_rec) {
 3344                 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
 3345                 if (func == NULL) {
 3346                         error = ENOMEM;
 3347                         goto bad;
 3348                 }
 3349                 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
 3350                 if (pcminfo == NULL) {
 3351                         error = ENOMEM;
 3352                         goto bad;
 3353                 }
 3354                 pcminfo->card = sc;
 3355                 pcminfo->route = RT_MCHRECORD;
 3356 
 3357                 func->func = SCF_PCM;
 3358                 func->varinfo = pcminfo;
 3359                 sc->pcm[RT_MCHRECORD] = device_add_child(dev, "pcm", -1);
 3360                 device_set_ivars(sc->pcm[RT_MCHRECORD], func);
 3361         } /*mch_rec */
 3362 
 3363         for (i = 0; i < 2; i++)
 3364                 sc->midi[i] = NULL;
 3365 
 3366         /* MIDI has some memory mangament and (possible) locking problems */
 3367 #if 0
 3368         /* Midi Interface 1: Live!, Audigy, Audigy 2 */
 3369         if ((sc->is_emu10k1) || (sc->is_emu10k2) || (sc->is_ca0102)) {
 3370                 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
 3371                 if (func == NULL) {
 3372                         error = ENOMEM;
 3373                         goto bad;
 3374                 }
 3375                 midiinfo = malloc(sizeof(struct emu_midiinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
 3376                 if (midiinfo == NULL) {
 3377                         error = ENOMEM;
 3378                         goto bad;
 3379                 }
 3380                 midiinfo->card = sc;
 3381                 if (sc->is_emu10k2 || (sc->is_ca0102)) {
 3382                         midiinfo->port = A_MUDATA1;
 3383                         midiinfo->portnr = 1;
 3384                 }
 3385                 if (sc->is_emu10k1) {
 3386                         midiinfo->port = MUDATA;
 3387                         midiinfo->portnr = 1;
 3388                 }
 3389                 func->func = SCF_MIDI;
 3390                 func->varinfo = midiinfo;
 3391                 sc->midi[0] = device_add_child(dev, "midi", -1);
 3392                 device_set_ivars(sc->midi[0], func);
 3393         }
 3394         /* Midi Interface 2: Audigy, Audigy 2 (on AudigyDrive) */
 3395         if (sc->is_emu10k2 || (sc->is_ca0102)) {
 3396                 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
 3397                 if (func == NULL) {
 3398                         error = ENOMEM;
 3399                         goto bad;
 3400                 }
 3401                 midiinfo = malloc(sizeof(struct emu_midiinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
 3402                 if (midiinfo == NULL) {
 3403                         error = ENOMEM;
 3404                         goto bad;
 3405                 }
 3406                 midiinfo->card = sc;
 3407 
 3408                 midiinfo->port = A_MUDATA2;
 3409                 midiinfo->portnr = 2;
 3410 
 3411                 func->func = SCF_MIDI;
 3412                 func->varinfo = midiinfo;
 3413                 sc->midi[1] = device_add_child(dev, "midi", -1);
 3414                 device_set_ivars(sc->midi[1], func);
 3415         }
 3416 #endif
 3417         return (bus_generic_attach(dev));
 3418 
 3419 bad:
 3420         /* XXX can we just call emu_pci_detach here? */
 3421         if (sc->cdev)
 3422                 emu10kx_dev_uninit(sc);
 3423         if (sc->rm != NULL)
 3424                 emu_rm_uninit(sc);
 3425         if (sc->reg)
 3426                 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->reg);
 3427         if (sc->ih)
 3428                 bus_teardown_intr(dev, sc->irq, sc->ih);
 3429         if (sc->irq)
 3430                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
 3431         mtx_destroy(&sc->rw);
 3432         mtx_destroy(&sc->lock);
 3433         return (error);
 3434 }
 3435 
 3436 static int
 3437 emu_pci_detach(device_t dev)
 3438 {
 3439         struct emu_sc_info *sc;
 3440         struct sndcard_func *func;
 3441         int devcount, i;
 3442         device_t *childlist;
 3443         int r = 0;
 3444 
 3445         sc = device_get_softc(dev);
 3446         
 3447         for (i = 0; i < RT_COUNT; i++) {
 3448                 if (sc->pcm[i] != NULL) {
 3449                         func = device_get_ivars(sc->pcm[i]);
 3450                         if (func != NULL && func->func == SCF_PCM) {
 3451                                 device_set_ivars(sc->pcm[i], NULL);
 3452                                 free(func->varinfo, M_DEVBUF);
 3453                                 free(func, M_DEVBUF);
 3454                         }
 3455                         r = device_delete_child(dev, sc->pcm[i]);
 3456                         if (r)  return (r);
 3457                 }
 3458         }
 3459 
 3460         if (sc->midi[0] != NULL) {
 3461                 func = device_get_ivars(sc->midi[0]);
 3462                 if (func != NULL && func->func == SCF_MIDI) {
 3463                         device_set_ivars(sc->midi[0], NULL);
 3464                         free(func->varinfo, M_DEVBUF);
 3465                         free(func, M_DEVBUF);
 3466                 }
 3467                 r = device_delete_child(dev, sc->midi[0]);
 3468                 if (r)  return (r);
 3469         }
 3470 
 3471         if (sc->midi[1] != NULL) {
 3472                 func = device_get_ivars(sc->midi[1]);
 3473                 if (func != NULL && func->func == SCF_MIDI) {
 3474                         device_set_ivars(sc->midi[1], NULL);
 3475                         free(func->varinfo, M_DEVBUF);
 3476                         free(func, M_DEVBUF);
 3477                 }
 3478                 r = device_delete_child(dev, sc->midi[1]);
 3479                 if (r)  return (r);
 3480         }
 3481 
 3482         if (device_get_children(dev, &childlist, &devcount) == 0)
 3483                 for (i = 0; i < devcount - 1; i++) {
 3484                         device_printf(dev, "removing stale child %d (unit %d)\n", i, device_get_unit(childlist[i]));
 3485                         func = device_get_ivars(childlist[i]);
 3486                         if (func != NULL && (func->func == SCF_MIDI || func->func == SCF_PCM)) {
 3487                                 device_set_ivars(childlist[i], NULL);
 3488                                 free(func->varinfo, M_DEVBUF);
 3489                                 free(func, M_DEVBUF);
 3490                         }
 3491                         device_delete_child(dev, childlist[i]);
 3492                 }
 3493         if (childlist != NULL)
 3494                 free(childlist, M_TEMP);
 3495 
 3496         r = emu10kx_dev_uninit(sc);
 3497         if (r)
 3498                 return (r);
 3499 
 3500         /* shutdown chip */
 3501         emu_uninit(sc);
 3502         emu_rm_uninit(sc);
 3503 
 3504         if (sc->mem.dmat)
 3505                 bus_dma_tag_destroy(sc->mem.dmat);
 3506 
 3507         if (sc->reg)
 3508                 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->reg);
 3509         bus_teardown_intr(dev, sc->irq, sc->ih);
 3510         bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
 3511         mtx_destroy(&sc->rw);
 3512         mtx_destroy(&sc->lock);
 3513 
 3514         return (bus_generic_detach(dev));
 3515 }
 3516 /* add suspend, resume */
 3517 static device_method_t emu_methods[] = {
 3518         /* Device interface */
 3519         DEVMETHOD(device_probe, emu_pci_probe),
 3520         DEVMETHOD(device_attach, emu_pci_attach),
 3521         DEVMETHOD(device_detach, emu_pci_detach),
 3522         /* Bus methods */
 3523         DEVMETHOD(bus_read_ivar, emu_read_ivar),
 3524         DEVMETHOD(bus_write_ivar, emu_write_ivar),
 3525 
 3526         {0, 0}
 3527 };
 3528 
 3529 
 3530 static driver_t emu_driver = {
 3531         "emu10kx",
 3532         emu_methods,
 3533         sizeof(struct emu_sc_info),
 3534         NULL,
 3535         0,
 3536         NULL
 3537 };
 3538 
 3539 static int
 3540 emu_modevent(module_t mod __unused, int cmd, void *data __unused)
 3541 {
 3542         int err = 0;
 3543 
 3544         switch (cmd) {
 3545         case MOD_LOAD:
 3546                 break;          /* Success */
 3547 
 3548         case MOD_UNLOAD:
 3549         case MOD_SHUTDOWN:
 3550 
 3551                 /* XXX  Should we check state of pcm & midi subdevices here? */
 3552 
 3553                 break;          /* Success */
 3554 
 3555         default:
 3556                 err = EINVAL;
 3557                 break;
 3558         }
 3559 
 3560         return (err);
 3561 
 3562 }
 3563 
 3564 static devclass_t emu_devclass;
 3565 
 3566 DRIVER_MODULE(snd_emu10kx, pci, emu_driver, emu_devclass, emu_modevent, NULL);
 3567 MODULE_VERSION(snd_emu10kx, SND_EMU10KX_PREFVER);

Cache object: c61621cff6e16912eb3549a35854021d


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