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