2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
3 * Copyright (c) 2003-2006 Yuriy Tsibizov <yuriy.tsibizov@gfk.ru>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/param.h>
31 #include <sys/types.h>
33 #include <machine/bus.h>
35 #include <sys/systm.h>
37 #include <sys/queue.h>
39 #include <sys/mutex.h>
40 #include <sys/sysctl.h>
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcivar.h>
45 #include <machine/clock.h> /* for DELAY */
47 #include <dev/sound/chip.h>
48 #include <dev/sound/pcm/sound.h>
49 #include <dev/sound/pcm/ac97.h>
51 #include "opt_emu10kx.h"
52 #include <dev/sound/pci/emu10kx.h>
57 #define HAS_AC97 0x0004
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
65 #define BROKEN_DIGITAL 0x0100
66 #define DIGITAL_ONLY 0x0200
68 #define IS_CARDBUS 0x0400
71 #define MODE_DIGITAL 2
72 #define SPDIF_MODE_PCM 1
73 #define SPDIF_MODE_AC3 2
92 #define GPR(i) (sc->gpr_base+(i))
93 #define INP(i) (sc->input_base+(i))
94 #define OUTP(i) (sc->output_base+(i))
96 #define FX2(i) (sc->efxc_base+(i))
97 #define DSP_CONST(i) (sc->dsp_zero+(i))
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)
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
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
153 /* Live! 5.1 Digital, non-standart 5.1 (center & sub) outputs */
154 #define OUT_A_CENTER 0x11
155 #define OUT_A_SUB 0x12
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
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
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
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
213 #include "emu10k1-alsa%diked.h"
214 #include "p16v-alsa%diked.h"
215 #include "p17v-alsa%diked.h"
227 #define NUM_CACHES 10
229 #define NUM_DUMMIES 64
231 #define EMU_MAX_GPR 512
232 #define EMU_MAX_IRQ_CONSUMERS 32
236 unsigned int b16:1, stereo:1, busy:1, running:1, ismaster:1;
243 struct emu_voice *slave;
249 SLIST_ENTRY(emu_memblk) link;
253 uint32_t pte_start, pte_size;
257 uint8_t bmap[EMU_MAXPAGES / 8];
260 bus_addr_t silent_page_addr;
261 bus_addr_t ptb_pages_addr;
263 SLIST_HEAD(, emu_memblk) blocks;
268 struct emu_sc_info *card;
270 signed int allocmap[EMU_MAX_GPR];
276 struct emu_intr_handler {
280 uint32_t(*irq_func) (void *softc, uint32_t irq);
285 struct mtx rw; /* Hardware exclusive access lock */
287 /* Hardware and subdevices */
289 device_t pcm[RT_COUNT];
295 bus_space_handle_t sh;
297 struct cdev *cdev; /* /dev/emu10k character device */
298 struct mtx emu10kx_lock;
300 struct sbuf emu10kx_sbuf;
305 struct resource *reg;
306 struct resource *irq;
310 struct emu_intr_handler ihandler[EMU_MAX_IRQ_CONSUMERS];
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;
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;
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 */
342 struct emu_mem mem; /* memory */
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;
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);
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);
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);
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);
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);
380 static void emu_vstop(struct emu_sc_info *sc, char channel, int enable);
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);
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);
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);
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);
400 /* Supported cards */
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},
418 /* (range unknown) 5.1 EMU10K1 cards */
419 {0x1102, 0x0002, 0x1102, 0x100a, "CT????", "SBLive! 5.1", HAS_AC97 | HAS_51 | IS_EMU10K1},
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},
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},
440 /* Generic SB Live! */
441 {0x1102, 0x0002, 0x1102, 0x0000, "SB????", "SBLive! (Unknown model)", HAS_AC97 | IS_EMU10K1},
443 /* 0x0041..0x0043 EMU10K2 (some kind of Audigy) cards */
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},
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},
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},
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},
469 /* Generic Audigy or Audigy 2 */
470 {0x1102, 0x0004, 0x1102, 0x0000, "SB????", "Audigy (Unknown model)", HAS_AC97 | HAS_51 | IS_EMU10K2},
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 */
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},
483 {0x1102, 0x0008, 0x1102, 0x2001, "SB0530", "Audigy 2 ZS CardBus", HAS_AC97 | HAS_71 | IS_CA0108 | IS_CARDBUS},
485 {0x1102, 0x0008, 0x0000, 0x0000, "SB????", "Audigy 2 Value (Unknown model)", HAS_AC97 | HAS_51 | IS_CA0108},
487 /* Unsupported cards */
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},
501 * Get best known information about device.
504 emu_getcard(device_t dev)
509 unsigned int thiscard;
512 device = pci_read_config(dev, PCIR_DEVICE, /* bytes */ 2);
513 subdevice = pci_read_config(dev, PCIR_SUBDEV_0, /* bytes */ 2);
515 n_cards = sizeof(emu_cards) / sizeof(struct emu_hwinfo);
517 for (i = 1; i < n_cards; i++) {
518 if (device == emu_cards[i].device) {
519 if (subdevice == emu_cards[i].subdevice) {
523 if (0x0000 == emu_cards[i].subdevice) {
525 /* don't break, we can get more specific card
526 * later in the list */
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) {
538 if (0x0000 == emu_bad_cards[i].subdevice) {
540 break; /* we avoid all this cards */
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.
553 emu_rd_nolock(struct emu_sc_info *sc, unsigned int regno, unsigned int size)
556 KASSERT(sc != NULL, ("emu_rd: NULL sc"));
559 return (bus_space_read_1(sc->st, sc->sh, regno));
561 return (bus_space_read_2(sc->st, sc->sh, regno));
563 return (bus_space_read_4(sc->st, sc->sh, regno));
569 emu_wr_nolock(struct emu_sc_info *sc, unsigned int regno, uint32_t data, unsigned int size)
572 KASSERT(sc != NULL, ("emu_rd: NULL sc"));
575 bus_space_write_1(sc->st, sc->sh, regno, data);
578 bus_space_write_2(sc->st, sc->sh, regno, data);
581 bus_space_write_4(sc->st, sc->sh, regno, data);
586 * PTR / DATA interface. Access to EMU10Kx is made
587 * via (channel, register) pair. Some registers are channel-specific,
591 emu_rdptr(struct emu_sc_info *sc, unsigned int chn, unsigned int reg)
593 uint32_t ptr, val, mask, size, offset;
595 ptr = ((reg << 16) & sc->address_mask) | (chn & PTR_CHANNELNUM_MASK);
597 emu_wr_nolock(sc, PTR, ptr, 4);
598 val = emu_rd_nolock(sc, DATA, 4);
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!
605 if (reg & 0xff000000) {
606 size = (reg >> 24) & 0x3f;
607 offset = (reg >> 16) & 0x1f;
608 mask = ((1 << size) - 1) << offset;
616 emu_wrptr(struct emu_sc_info *sc, unsigned int chn, unsigned int reg, uint32_t data)
618 uint32_t ptr, mask, size, offset;
619 ptr = ((reg << 16) & sc->address_mask) | (chn & PTR_CHANNELNUM_MASK);
621 emu_wr_nolock(sc, PTR, ptr, 4);
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.
627 if (reg & 0xff000000) {
628 size = (reg >> 24) & 0x3f;
629 offset = (reg >> 16) & 0x1f;
630 mask = ((1 << size) - 1) << offset;
633 data |= emu_rd_nolock(sc, DATA, 4) & ~mask;
635 emu_wr_nolock(sc, DATA, data, 4);
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.
644 emu_rd_p16vptr(struct emu_sc_info *sc, uint16_t chn, uint16_t reg)
649 emu_wr_nolock(sc, PTR2, (reg << 16) | chn, 4);
650 val = emu_rd_nolock(sc, DATA2, 4);
656 emu_wr_p16vptr(struct emu_sc_info *sc, uint16_t chn, uint16_t reg, uint32_t data)
660 emu_wr_nolock(sc, PTR2, (reg << 16) | chn, 4);
661 emu_wr_nolock(sc, DATA2, data, 4);
665 * XXX CardBus interface. Not tested on any real hardware.
668 emu_wr_cbptr(struct emu_sc_info *sc, uint32_t data)
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 */
676 val = emu_rd_nolock(sc, 0x38, 4);
677 emu_wr_nolock(sc, 0x38, data, 4);
678 val = emu_rd_nolock(sc, 0x38, 4);
683 * Direct hardware register access
686 emu_wr(struct emu_sc_info *sc, unsigned int regno, uint32_t data, unsigned int size)
690 emu_wr_nolock(sc, regno, data, size);
695 emu_rd(struct emu_sc_info *sc, unsigned int regno, unsigned int size)
700 rd = emu_rd_nolock(sc, regno, size);
706 * Enabling IR MIDI messages is another kind of black magic. It just
707 * has to be made this way. It really do it.
710 emu_enable_ir(struct emu_sc_info *sc)
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);
719 emu_wr_nolock(sc, A_IOCFG, iocfg | A_IOCFG_GPOUT1 | A_IOCFG_GPOUT2, 2);
721 emu_wr_nolock(sc, A_IOCFG, iocfg | A_IOCFG_GPOUT1, 2);
723 emu_wr_nolock(sc, A_IOCFG, iocfg, 2);
724 device_printf(sc->dev, "Audigy IR MIDI events enabled.\n");
727 if (sc->is_emu10k1) {
728 iocfg = emu_rd_nolock(sc, HCFG, 4);
729 emu_wr_nolock(sc, HCFG, iocfg | HCFG_GPOUT2, 4);
731 emu_wr_nolock(sc, HCFG, iocfg | HCFG_GPOUT1 | HCFG_GPOUT2, 4);
733 emu_wr_nolock(sc, HCFG, iocfg, 4);
734 device_printf(sc->dev, "SB Live! IR MIDI events enabled.\n");
742 * emu_timer_ - HW timer managment
745 emu_timer_create(struct emu_sc_info *sc)
750 for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++)
751 if (sc->timer[i] == 0) {
752 sc->timer[i] = -1; /* disable it */
761 emu_timer_set(struct emu_sc_info *sc, int timer, int delay)
768 RANGE(delay, 16, 1024);
769 RANGE(timer, 0, EMU_MAX_IRQ_CONSUMERS-1);
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];
776 emu_wr(sc, TIMER, sc->timerinterval & 0x03ff, 2);
781 emu_timer_enable(struct emu_sc_info *sc, int timer, int go)
790 RANGE(timer, 0, EMU_MAX_IRQ_CONSUMERS-1);
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];
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)
807 emu_wr(sc, TIMER, sc->timerinterval & 0x03ff, 2);
810 x = emu_rd(sc, INTE, 4);
811 x |= INTE_INTERVALTIMERENB;
812 emu_wr(sc, INTE, x, 4);
814 x = emu_rd(sc, INTE, 4);
815 x &= ~INTE_INTERVALTIMERENB;
816 emu_wr(sc, INTE, x, 4);
818 mtx_unlock(&sc->lock);
823 emu_timer_clear(struct emu_sc_info *sc, int timer)
828 RANGE(timer, 0, EMU_MAX_IRQ_CONSUMERS-1);
830 emu_timer_enable(sc, timer, 0);
833 if (sc->timer[timer] != 0)
834 sc->timer[timer] = 0;
835 mtx_unlock(&sc->lock);
841 * emu_intr_ - HW interrupt handler managment
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)
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);
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);
865 mtx_unlock(&sc->lock);
866 #ifdef SND_EMU10KX_DEBUG
867 device_printf(sc->dev, "ihandle not registered\n");
873 emu_intr_unregister(struct emu_sc_info *sc, int hnumber)
880 if (sc->ihandler[hnumber].inte_mask == 0) {
881 mtx_unlock(&sc->lock);
885 x = emu_rd(sc, INTE, 4);
886 x &= ~sc->ihandler[hnumber].inte_mask;
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;
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;
898 emu_wr(sc, INTE, x, 4);
900 mtx_unlock(&sc->lock);
907 struct emu_sc_info *sc = (struct emu_sc_info *)p;
912 stat = emu_rd(sc, IPR, 4);
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);
924 #ifdef SND_EMU10KX_DEBUG
926 device_printf(sc->dev, "Unhandled interrupt: %08x\n", stat & (~ack));
930 if ((sc->is_ca0102) || (sc->is_ca0108))
932 stat = emu_rd(sc, IPR2, 4);
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. */
944 stat = emu_rd(sc, IPR3, 4);
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 */
957 * Get data from private emu10kx structure for PCM buffer allocation.
958 * Used by PCM code only.
961 emu_gettag(struct emu_sc_info *sc)
963 return (sc->mem.dmat);
967 emu_setmap(void *arg, bus_dma_segment_t * segs, int nseg, int error)
969 bus_addr_t *phys = (bus_addr_t *) arg;
971 *phys = error ? 0 : (bus_addr_t) segs->ds_addr;
974 printf("emu10kx: setmap (%lx, %lx), nseg=%d, error=%d\n",
975 (unsigned long)segs->ds_addr, (unsigned long)segs->ds_len,
981 emu_malloc(struct emu_mem *mem, uint32_t sz, bus_addr_t * addr)
987 if (bus_dmamem_alloc(mem->dmat, &dmabuf, BUS_DMA_NOWAIT, &map))
989 if (bus_dmamap_load(mem->dmat, map, dmabuf, sz, emu_setmap, addr, 0) || !*addr)
995 emu_free(struct emu_mem *mem, void *dmabuf)
997 bus_dmamem_free(mem->dmat, dmabuf, NULL);
1001 emu_memalloc(struct emu_mem *mem, uint32_t sz, bus_addr_t * addr, const char *owner)
1003 uint32_t blksz, start, idx, ofs, tmp, found;
1004 struct emu_memblk *blk;
1007 blksz = sz / EMUPAGESIZE;
1008 if (sz > (blksz * EMUPAGESIZE))
1010 if (blksz > EMU_MAX_BUFSZ / EMUPAGESIZE)
1012 /* find a free block in the bitmap */
1015 while (!found && start + blksz < EMU_MAXPAGES) {
1017 for (idx = start; idx < start + blksz; idx++)
1018 if (mem->bmap[idx >> 3] & (1 << (idx & 7)))
1025 blk = malloc(sizeof(*blk), M_DEVBUF, M_NOWAIT);
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);
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);
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;
1050 SLIST_INSERT_HEAD(&mem->blocks, blk, link);
1055 emu_memfree(struct emu_mem *mem, void *membuf)
1058 struct emu_memblk *blk, *i;
1061 SLIST_FOREACH(i, &mem->blocks, link) {
1062 if (i->buf == membuf)
1067 #ifdef SND_EMU10KX_DEBUG
1068 printf("emu10kx emu_memfree: freeing %d for %s\n", blk->pte_size, blk->owner);
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;
1077 free(blk, M_DEVBUF);
1082 emu_memstart(struct emu_mem *mem, void *membuf)
1084 struct emu_memblk *blk, *i;
1087 SLIST_FOREACH(i, &mem->blocks, link) {
1088 if (i->buf == membuf)
1093 return (blk->pte_start);
1098 emu_rate_to_pitch(uint32_t rate)
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
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
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)]);
1155 emu_rate_to_linearpitch(uint32_t rate)
1157 rate = (rate << 8) / 375;
1158 return ((rate >> 1) + (rate & 1));
1162 emu_valloc(struct emu_sc_info *sc)
1164 struct emu_voice *v;
1168 mtx_lock(&sc->lock);
1169 for (i = 0; i < NUM_G && sc->voice[i].busy; i++);
1174 mtx_unlock(&sc->lock);
1179 emu_vfree(struct emu_sc_info *sc, struct emu_voice *v)
1183 mtx_lock(&sc->lock);
1184 for (i = 0; i < NUM_G; i++) {
1185 if (v == &sc->voice[i] && sc->voice[i].busy) {
1187 /* XXX What we should do with mono channels?
1188 See -pcm.c emupchan_init for other side of
1190 if (v->slave != NULL)
1191 r = emu_memfree(&sc->mem, v->vbuf);
1194 mtx_unlock(&sc->lock);
1198 emu_vinit(struct emu_sc_info *sc, struct emu_voice *m, struct emu_voice *s,
1199 uint32_t sz, struct snd_dbuf *b)
1202 bus_addr_t tmp_addr;
1204 vbuf = emu_memalloc(&sc->mem, sz, &tmp_addr, "vinit");
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);
1214 m->end = m->start + sz;
1225 s->start = m->start;
1241 emu_vsetup(struct emu_voice *v, int fmt, int spd)
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;
1253 if (v->slave != NULL)
1254 v->slave->speed = v->speed;
1259 emu_vroute(struct emu_sc_info *sc, struct emu_route *rt, struct emu_voice *v)
1261 unsigned int routing[8], amounts[8];
1264 for (i = 0; i < 8; i++) {
1265 routing[i] = rt->routing_left[i];
1266 amounts[i] = rt->amounts_left[i];
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];
1274 if (sc->is_emu10k1) {
1275 emu_wrptr(sc, v->vnum, FXRT, ((routing[3] << 12) |
1278 (routing[0] << 0)) << 16);
1280 emu_wrptr(sc, v->vnum, A_FXRT1, (routing[3] << 24) |
1281 (routing[2] << 16) |
1284 emu_wrptr(sc, v->vnum, A_FXRT2, (routing[7] << 24) |
1285 (routing[6] << 16) |
1288 emu_wrptr(sc, v->vnum, A_SENDAMOUNTS, (amounts[7] << 24) |
1289 (amounts[6] << 26) |
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);
1301 emu_vwrite(struct emu_sc_info *sc, struct emu_voice *v)
1304 uint32_t am_2, am_3, start, val, silent_page;
1306 s = (v->stereo ? 1 : 0) + (v->b16 ? 1 : 0);
1308 v->sa = v->start >> s;
1309 v->ea = v->end >> s;
1313 emu_wrptr(sc, v->vnum, CPF, CPF_STEREO_MASK);
1315 emu_wrptr(sc, v->vnum, CPF, 0);
1317 val = v->stereo ? 28 : 30;
1318 val *= v->b16 ? 1 : 2;
1319 start = v->sa + val;
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);
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);
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);
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);
1345 emu_wrptr(sc, v->vnum, ATKHLDV, ATKHLDV_HOLDTIME_MASK | ATKHLDV_ATTACKTIME_MASK);
1346 emu_wrptr(sc, v->vnum, ENVVOL, 0x8000);
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);
1355 emu_vstop(struct emu_sc_info *sc, char channel, int enable)
1359 reg = (channel & 0x20) ? SOLEH : SOLEL;
1362 reg |= channel << 16;
1363 emu_wrptr(sc, 0, reg, enable);
1367 emu_vtrigger(struct emu_sc_info *sc, struct emu_voice *v, int go)
1369 uint32_t pitch_target, initial_pitch;
1370 uint32_t cra, cs, ccis;
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);
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);
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);
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);
1405 if ((v->stereo) && (v->slave != NULL))
1406 emu_vtrigger(sc, v->slave, go);
1410 emu_vpos(struct emu_sc_info *sc, struct emu_voice *v)
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);
1422 emu_wrefx(struct emu_sc_info *sc, unsigned int pc, unsigned int data)
1424 emu_wrptr(sc, 0, sc->code_base + pc, data);
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)
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));
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);
1441 sysctl_emu_mixer_control(SYSCTL_HANDLER_ARGS)
1443 struct emu_sc_info *sc;
1451 new_vol = emumix_get_volume(sc, mixer_id);
1452 err = sysctl_handle_int(oidp, &new_vol, sizeof(new_vol), req);
1454 if (err || req->newptr == NULL)
1456 if (new_vol < 0 || new_vol > 100)
1458 emumix_set_volume(sc, mixer_id, new_vol);
1464 emu_addefxmixer(struct emu_sc_info *sc, const char *mix_name, const int mix_id, uint32_t defvolume)
1467 char sysctl_name[32];
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","");
1488 /* allocate cache GPRs that will hold mixed output channels
1489 * and clear it on every DSP run.
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]), \
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]), \
1509 GPR(sc->mixer_gpr[IN_GPR_IDX]), \
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, \
1520 GPR(sc->cache_gpr[OUT_CACHE_IDX]), \
1521 GPR(sc->mixer_gpr[OUT_GPR_IDX]), \
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, \
1530 GPR(sc->cache_gpr[OUT_CACHE_IDX]), \
1531 GPR(sc->mixer_gpr[OUT_GPR_IDX]), \
1535 /* mute, if FLAG != 0 */
1537 #define EFX_MUTEIF(GPR_IDX, FLAG) do { \
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, \
1546 GPR(sc->dummy_gpr[DUMMY_IDX]), \
1554 emu_initefx(struct emu_sc_info *sc)
1560 if (sc->is_emu10k1) {
1561 emu_wrptr(sc, 0, DBG, EMU10K1_DBG_SINGLE_STEP);
1563 emu_wrptr(sc, 0, A_DBG, A_DBG_SINGLE_STEP);
1566 /* code size is in instructions */
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);
1572 emu_addefxop(sc, SKIP, DSP_CONST(0x0), DSP_CONST(0x0), DSP_CONST(0xf), DSP_CONST(0x0), &pc);
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 !
1587 for (i = 0; i < 16 ; i++) {
1588 emu_addefxop(sc, ACC3, OUTP(i), DSP_CONST(0), DSP_CONST(0), DSP_CONST(0), &pc);
1592 if (sc->is_emu10k1) {
1593 EFX_CACHE(C_FRONT_L);
1594 EFX_CACHE(C_FRONT_R);
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);
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);
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);
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);
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);
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);
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
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);
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);
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
1668 * Additional channel volume is controlled by mixer in
1669 * emu_dspmixer_set() in -pcm.c
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);
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);
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);
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);
1690 /* fx5 (pcm3) to 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);
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);
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,
1710 GPR(sc->dummy_gpr[i]),
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);
1719 } else /* emu10k2 and later */ {
1720 EFX_CACHE(C_FRONT_L);
1721 EFX_CACHE(C_FRONT_R);
1725 /* fx0 to front/record, 100%/muted by default */
1727 * FRONT_[L|R] is controlled by AC97 emulation in
1728 * emu_ac97_[read|write]_emulation in -pcm.c
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);
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);
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);
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
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);
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);
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);
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
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);
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);
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
1799 * Additional channel volume is controlled by mixer in
1800 * emu_dspmixer_set() in -pcm.c
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);
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);
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);
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);
1823 /* fx5 (pcm3) to 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);
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);
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);
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,
1853 GPR(sc->dummy_gpr[i]),
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);
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);
1866 sc->routing_code_end = pc;
1869 if (sc->is_emu10k1) {
1870 emu_wrptr(sc, 0, DBG, 0);
1872 emu_wrptr(sc, 0, A_DBG, 0);
1877 static d_open_t emu10kx_open;
1878 static d_close_t emu10kx_close;
1879 static d_read_t emu10kx_read;
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,
1891 emu10kx_open(struct cdev *i_dev, int flags __unused, int mode __unused, struct thread *td __unused)
1894 struct emu_sc_info *sc;
1896 sc = i_dev->si_drv1;
1897 mtx_lock(&sc->emu10kx_lock);
1898 if (sc->emu10kx_isopen) {
1899 mtx_unlock(&sc->emu10kx_lock);
1902 sc->emu10kx_isopen = 1;
1903 mtx_unlock(&sc->emu10kx_lock);
1904 if (sbuf_new(&sc->emu10kx_sbuf, NULL, 4096, 0) == NULL) {
1908 sc->emu10kx_bufptr = 0;
1909 error = (emu10kx_prepare(sc, &sc->emu10kx_sbuf) > 0) ? 0 : ENOMEM;
1912 mtx_lock(&sc->emu10kx_lock);
1913 sc->emu10kx_isopen = 0;
1914 mtx_unlock(&sc->emu10kx_lock);
1920 emu10kx_close(struct cdev *i_dev, int flags __unused, int mode __unused, struct thread *td __unused)
1922 struct emu_sc_info *sc;
1924 sc = i_dev->si_drv1;
1926 mtx_lock(&sc->emu10kx_lock);
1927 if (!(sc->emu10kx_isopen)) {
1928 mtx_unlock(&sc->emu10kx_lock);
1931 sbuf_delete(&sc->emu10kx_sbuf);
1932 sc->emu10kx_isopen = 0;
1933 mtx_unlock(&sc->emu10kx_lock);
1939 emu10kx_read(struct cdev *i_dev, struct uio *buf, int flag __unused)
1942 struct emu_sc_info *sc;
1944 sc = i_dev->si_drv1;
1945 mtx_lock(&sc->emu10kx_lock);
1946 if (!(sc->emu10kx_isopen)) {
1947 mtx_unlock(&sc->emu10kx_lock);
1950 mtx_unlock(&sc->emu10kx_lock);
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;
1960 emu10kx_prepare(struct emu_sc_info *sc, struct sbuf *s)
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");
1970 sbuf_printf(s, " AC97 codec");
1972 sbuf_printf(s, " NO AC97 codec");
1976 sbuf_printf(s, " and 7.1 output");
1978 sbuf_printf(s, " and 5.1 output");
1981 sbuf_printf(s, ", SBLive! DSP code");
1983 sbuf_printf(s, ", Audigy DSP code");
1985 sbuf_printf(s, ", Audigy DSP code with Audigy2 hacks");
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]));
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]));
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]));
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");
2011 return (sbuf_len(s));
2016 emu10kx_dev_init(struct emu_sc_info *sc)
2020 mtx_init(&sc->emu10kx_lock, "kxdevlock", NULL, 0);
2021 unit = device_get_unit(sc->dev);
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;
2032 emu10kx_dev_uninit(struct emu_sc_info *sc)
2037 mtx_lock(&sc->emu10kx_lock);
2038 if (sc->emu10kx_isopen) {
2039 mtx_unlock(&sc->emu10kx_lock);
2044 destroy_dev(sc->cdev);
2048 mtx_destroy(&sc->emu10kx_lock);
2052 /* resource manager */
2054 emu_rm_init(struct emu_sc_info *sc)
2060 rm = malloc(sizeof(struct emu_rm), M_DEVBUF, M_NOWAIT | M_ZERO);
2066 maxcount = sc->num_gprs;
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;
2078 emu_rm_uninit(struct emu_sc_info *sc)
2080 #ifdef SND_EMU10KX_DEBUG
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));
2089 mtx_destroy(&(sc->rm->gpr_lock));
2090 free(sc->rm, M_DEVBUF);
2095 emu_rm_gpr_alloc(struct emu_rm *rm, int count)
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);
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];
2118 for (j = 1; j < count; j++) {
2119 if (rm->allocmap[i + j] != 0)
2120 allocated_gpr = rm->num_gprs;
2122 if (allocated_gpr == i)
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);
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);
2143 emumix_set_mode(struct emu_sc_info *sc, int mode)
2158 hcfg = HCFG_AUDIOENABLE | HCFG_AUTOMUTE;
2162 hcfg |= HCFG_JOYENABLE;
2165 hcfg |= HCFG_LOCKTANKCACHE_MASK;
2167 hcfg |= HCFG_CODECFORMAT_I2S | HCFG_JOYENABLE;
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");
2174 a_iocfg |= A_IOCFG_ENABLE_DIGITAL;
2175 hcfg |= HCFG_GPOUT0;
2178 if (mode == MODE_ANALOG)
2179 emumix_set_spdif_mode(sc, SPDIF_MODE_PCM);
2182 a_iocfg |= 0x80; /* XXX */
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. */
2189 a_iocfg |= 0x20; /* XXX */
2191 emu_wr(sc, HCFG, hcfg, 4);
2193 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
2194 tmp = emu_rd(sc, A_IOCFG, 2);
2196 emu_wr(sc, A_IOCFG, tmp, 2);
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.
2207 emumix_set_spdif_mode(struct emu_sc_info *sc, int mode)
2212 case SPDIF_MODE_PCM:
2214 case SPDIF_MODE_AC3:
2215 device_printf(sc->dev, "AC3 mode does not work and disabled\n");
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;
2226 mode = SPDIF_MODE_PCM;
2228 emu_wrptr(sc, 0, SPCS0, spcs);
2229 emu_wrptr(sc, 0, SPCS1, spcs);
2230 emu_wrptr(sc, 0, SPCS2, spcs);
2233 #define L2L_POINTS 10
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 */
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 */
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];
2286 emumix_set_fxvol(struct emu_sc_info *sc, unsigned gpr, int32_t vol)
2290 emumix_set_gpr(sc, gpr, vol);
2294 emumix_set_gpr(struct emu_sc_info *sc, unsigned gpr, int32_t val)
2297 emu_wrptr(sc, 0, GPR(gpr), val);
2301 emumix_set_volume(struct emu_sc_info *sc, int mixer_idx, int volume)
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);
2312 emumix_get_volume(struct emu_sc_info *sc, int mixer_idx)
2314 if ((mixer_idx < NUM_MIXERS) && (mixer_idx >= 0))
2315 return (sc->mixer_volcache[mixer_idx]);
2319 /* Init CardBus part */
2321 emu_cardbus_init(struct emu_sc_info *sc)
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".
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);
2334 emu_wr_cbptr(sc, (0x0090 << 16) | 0x007f);
2339 /* Probe and attach the card */
2341 emu_init(struct emu_sc_info *sc)
2349 /* disable audio and lock cache */
2350 emu_wr(sc, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, 4);
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);
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);
2367 /* disable P16V and S/PDIF interrupts */
2368 if ((sc->is_ca0102) || (sc->is_ca0108))
2369 emu_wr(sc, INTE2, 0, 4);
2372 emu_wr(sc, INTE3, 0, 4);
2374 /* init phys inputs and outputs */
2377 ac97slot = AC97SLOT_CNTR | AC97SLOT_LFE;
2379 ac97slot = AC97SLOT_CNTR | AC97SLOT_LFE | AC97SLOT_REAR_LEFT | AC97SLOT_REAR_RIGHT;
2382 emu_wrptr(sc, 0, AC97SLOT, ac97slot);
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? */
2388 if (bus_dma_tag_create( /* parent */ NULL, /* alignment */ 2, /* boundary */ 0,
2389 /* lowaddr */ 1 << 31, /* can only access 0-2gb */
2390 /* highaddr */ BUS_SPACE_MAXADDR,
2391 /* filter */ NULL, /* filterarg */ NULL,
2392 /* maxsize */ EMU_MAX_BUFSZ, /* nsegments */ 1, /* maxsegz */ 0x3ffff,
2393 /* flags */ 0, /* lockfunc */ busdma_lock_mutex,
2394 /* lockarg */ &Giant, &(sc->mem.dmat)) != 0) {
2395 device_printf(sc->dev, "unable to create dma tag\n");
2396 bus_dma_tag_destroy(sc->mem.dmat);
2400 SLIST_INIT(&sc->mem.blocks);
2401 sc->mem.ptb_pages = emu_malloc(&sc->mem, EMU_MAXPAGES * sizeof(uint32_t), &sc->mem.ptb_pages_addr);
2402 if (sc->mem.ptb_pages == NULL)
2405 sc->mem.silent_page = emu_malloc(&sc->mem, EMUPAGESIZE, &sc->mem.silent_page_addr);
2406 if (sc->mem.silent_page == NULL) {
2407 emu_free(&sc->mem, sc->mem.ptb_pages);
2410 /* Clear page with silence & setup all pointers to this page */
2411 bzero(sc->mem.silent_page, EMUPAGESIZE);
2412 tmp = (uint32_t) (sc->mem.silent_page_addr) << 1;
2413 for (i = 0; i < EMU_MAXPAGES; i++)
2414 sc->mem.ptb_pages[i] = tmp | i;
2416 for (ch = 0; ch < NUM_G; ch++) {
2417 emu_wrptr(sc, ch, MAPA, tmp | MAP_PTI_MASK);
2418 emu_wrptr(sc, ch, MAPB, tmp | MAP_PTI_MASK);
2420 emu_wrptr(sc, 0, PTB, (sc->mem.ptb_pages_addr));
2421 emu_wrptr(sc, 0, TCB, 0); /* taken from original driver */
2422 emu_wrptr(sc, 0, TCBS, 0); /* taken from original driver */
2424 /* init envelope engine */
2425 for (ch = 0; ch < NUM_G; ch++) {
2426 emu_wrptr(sc, ch, DCYSUSV, 0);
2427 emu_wrptr(sc, ch, IP, 0);
2428 emu_wrptr(sc, ch, VTFT, 0xffff);
2429 emu_wrptr(sc, ch, CVCF, 0xffff);
2430 emu_wrptr(sc, ch, PTRX, 0);
2431 emu_wrptr(sc, ch, CPF, 0);
2432 emu_wrptr(sc, ch, CCR, 0);
2434 emu_wrptr(sc, ch, PSST, 0);
2435 emu_wrptr(sc, ch, DSL, 0x10);
2436 emu_wrptr(sc, ch, CCCA, 0);
2437 emu_wrptr(sc, ch, Z1, 0);
2438 emu_wrptr(sc, ch, Z2, 0);
2439 emu_wrptr(sc, ch, FXRT, 0xd01c0000);
2441 emu_wrptr(sc, ch, ATKHLDM, 0);
2442 emu_wrptr(sc, ch, DCYSUSM, 0);
2443 emu_wrptr(sc, ch, IFATN, 0xffff);
2444 emu_wrptr(sc, ch, PEFE, 0);
2445 emu_wrptr(sc, ch, FMMOD, 0);
2446 emu_wrptr(sc, ch, TREMFRQ, 24); /* 1 Hz */
2447 emu_wrptr(sc, ch, FM2FRQ2, 24); /* 1 Hz */
2448 emu_wrptr(sc, ch, TEMPENV, 0);
2450 /*** these are last so OFF prevents writing ***/
2451 emu_wrptr(sc, ch, LFOVAL2, 0);
2452 emu_wrptr(sc, ch, LFOVAL1, 0);
2453 emu_wrptr(sc, ch, ATKHLDV, 0);
2454 emu_wrptr(sc, ch, ENVVOL, 0);
2455 emu_wrptr(sc, ch, ENVVAL, 0);
2457 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
2458 emu_wrptr(sc, ch, 0x4c, 0x0);
2459 emu_wrptr(sc, ch, 0x4d, 0x0);
2460 emu_wrptr(sc, ch, 0x4e, 0x0);
2461 emu_wrptr(sc, ch, 0x4f, 0x0);
2462 emu_wrptr(sc, ch, A_FXRT1, 0x3f3f3f3f);
2463 emu_wrptr(sc, ch, A_FXRT2, 0x3f3f3f3f);
2464 emu_wrptr(sc, ch, A_SENDAMOUNTS, 0x0);
2468 emumix_set_spdif_mode(sc, SPDIF_MODE_PCM);
2470 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108))
2471 emu_wrptr(sc, 0, A_SPDIF_SAMPLERATE, A_SPDIF_48000);
2474 * CAxxxx cards needs additional setup:
2475 * 1. Set I2S capture sample rate to 96000
2476 * 2. Disable P16v / P17v proceesing
2477 * 3. Allow EMU10K DSP inputs
2479 if ((sc->is_ca0102) || (sc->is_ca0108)) {
2481 spdif_sr = emu_rdptr(sc, 0, A_SPDIF_SAMPLERATE);
2482 spdif_sr &= 0xfffff1ff;
2483 spdif_sr |= A_I2S_CAPTURE_96000;
2484 emu_wrptr(sc, 0, A_SPDIF_SAMPLERATE, spdif_sr);
2486 /* Disable P16v processing */
2487 emu_wr_p16vptr(sc, 0, SRCSel, 0x14);
2489 /* Setup P16v/P17v sound routing */
2491 emu_wr_p16vptr(sc, 0, SRCMULTI_ENABLE, 0xFF00FF00);
2493 emu_wr_p16vptr(sc, 0, P17V_MIXER_I2S_ENABLE, 0xFF000000);
2494 emu_wr_p16vptr(sc, 0, P17V_MIXER_SPDIF_ENABLE, 0xFF000000);
2496 tmp = emu_rd(sc, A_IOCFG, 2);
2497 emu_wr(sc, A_IOCFG, tmp & ~0x8, 2);
2502 def_mode = MODE_ANALOG;
2503 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108))
2504 def_mode = MODE_DIGITAL;
2505 if (((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) && (sc->broken_digital)) {
2506 device_printf(sc->dev, "Audigy card initialized in analog mode.\n");
2507 def_mode = MODE_ANALOG;
2509 emumix_set_mode(sc, def_mode);
2512 tmp = emu_rd(sc, HCFG, 4);
2513 device_printf(sc->dev, "Card Configuration ( 0x%08x )\n", tmp);
2514 device_printf(sc->dev, "Card Configuration ( & 0xff000000 ) : %s%s%s%s%s%s%s%s\n",
2515 (tmp & 0x80000000 ? "[Legacy MPIC] " : ""),
2516 (tmp & 0x40000000 ? "[0x40] " : ""),
2517 (tmp & 0x20000000 ? "[0x20] " : ""),
2518 (tmp & 0x10000000 ? "[0x10] " : ""),
2519 (tmp & 0x08000000 ? "[0x08] " : ""),
2520 (tmp & 0x04000000 ? "[0x04] " : ""),
2521 (tmp & 0x02000000 ? "[0x02] " : ""),
2522 (tmp & 0x01000000 ? "[0x01]" : " "));
2523 device_printf(sc->dev, "Card Configuration ( & 0x00ff0000 ) : %s%s%s%s%s%s%s%s\n",
2524 (tmp & 0x00800000 ? "[0x80] " : ""),
2525 (tmp & 0x00400000 ? "[0x40] " : ""),
2526 (tmp & 0x00200000 ? "[Legacy INT] " : ""),
2527 (tmp & 0x00100000 ? "[0x10] " : ""),
2528 (tmp & 0x00080000 ? "[0x08] " : ""),
2529 (tmp & 0x00040000 ? "[Codec4] " : ""),
2530 (tmp & 0x00020000 ? "[Codec2] " : ""),
2531 (tmp & 0x00010000 ? "[I2S Codec]" : " "));
2532 device_printf(sc->dev, "Card Configuration ( & 0x0000ff00 ) : %s%s%s%s%s%s%s%s\n",
2533 (tmp & 0x00008000 ? "[0x80] " : ""),
2534 (tmp & 0x00004000 ? "[GPINPUT0] " : ""),
2535 (tmp & 0x00002000 ? "[GPINPUT1] " : ""),
2536 (tmp & 0x00001000 ? "[GPOUT0] " : ""),
2537 (tmp & 0x00000800 ? "[GPOUT1] " : ""),
2538 (tmp & 0x00000400 ? "[GPOUT2] " : ""),
2539 (tmp & 0x00000200 ? "[Joystick] " : ""),
2540 (tmp & 0x00000100 ? "[0x01]" : " "));
2541 device_printf(sc->dev, "Card Configuration ( & 0x000000ff ) : %s%s%s%s%s%s%s%s\n",
2542 (tmp & 0x00000080 ? "[0x80] " : ""),
2543 (tmp & 0x00000040 ? "[0x40] " : ""),
2544 (tmp & 0x00000020 ? "[0x20] " : ""),
2545 (tmp & 0x00000010 ? "[AUTOMUTE] " : ""),
2546 (tmp & 0x00000008 ? "[LOCKSOUNDCACHE] " : ""),
2547 (tmp & 0x00000004 ? "[LOCKTANKCACHE] " : ""),
2548 (tmp & 0x00000002 ? "[MUTEBUTTONENABLE] " : ""),
2549 (tmp & 0x00000001 ? "[AUDIOENABLE]" : " "));
2551 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
2552 tmp = emu_rd(sc, A_IOCFG, 2);
2553 device_printf(sc->dev, "Audigy Card Configuration ( 0x%04x )\n", tmp);
2554 device_printf(sc->dev, "Audigy Card Configuration ( & 0xff00 )");
2555 printf(" : %s%s%s%s%s%s%s%s\n",
2556 (tmp & 0x8000 ? "[Rear Speakers] " : ""),
2557 (tmp & 0x4000 ? "[Front Speakers] " : ""),
2558 (tmp & 0x2000 ? "[0x20] " : ""),
2559 (tmp & 0x1000 ? "[0x10] " : ""),
2560 (tmp & 0x0800 ? "[0x08] " : ""),
2561 (tmp & 0x0400 ? "[0x04] " : ""),
2562 (tmp & 0x0200 ? "[0x02] " : ""),
2563 (tmp & 0x0100 ? "[AudigyDrive Phones]" : " "));
2564 device_printf(sc->dev, "Audigy Card Configuration ( & 0x00ff )");
2565 printf(" : %s%s%s%s%s%s%s%s\n",
2566 (tmp & 0x0080 ? "[0x80] " : ""),
2567 (tmp & 0x0040 ? "[Mute AnalogOut] " : ""),
2568 (tmp & 0x0020 ? "[0x20] " : ""),
2569 (tmp & 0x0010 ? "[0x10] " : ""),
2570 (tmp & 0x0008 ? "[0x08] " : ""),
2571 (tmp & 0x0004 ? "[GPOUT0] " : ""),
2572 (tmp & 0x0002 ? "[GPOUT1] " : ""),
2573 (tmp & 0x0001 ? "[GPOUT2]" : " "));
2574 } /* is_emu10k2 or ca* */
2580 emu_uninit(struct emu_sc_info *sc)
2583 struct emu_memblk *blk;
2585 emu_wr(sc, INTE, 0, 4);
2586 for (ch = 0; ch < NUM_G; ch++)
2587 emu_wrptr(sc, ch, DCYSUSV, 0);
2588 for (ch = 0; ch < NUM_G; ch++) {
2589 emu_wrptr(sc, ch, VTFT, 0);
2590 emu_wrptr(sc, ch, CVCF, 0);
2591 emu_wrptr(sc, ch, PTRX, 0);
2592 emu_wrptr(sc, ch, CPF, 0);
2595 /* disable audio and lock cache */
2596 emu_wr(sc, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, 4);
2598 emu_wrptr(sc, 0, PTB, 0);
2599 /* reset recording buffers */
2600 emu_wrptr(sc, 0, MICBS, ADCBS_BUFSIZE_NONE);
2601 emu_wrptr(sc, 0, MICBA, 0);
2602 emu_wrptr(sc, 0, FXBS, ADCBS_BUFSIZE_NONE);
2603 emu_wrptr(sc, 0, FXBA, 0);
2604 emu_wrptr(sc, 0, FXWC, 0);
2605 emu_wrptr(sc, 0, ADCBS, ADCBS_BUFSIZE_NONE);
2606 emu_wrptr(sc, 0, ADCBA, 0);
2607 emu_wrptr(sc, 0, TCB, 0);
2608 emu_wrptr(sc, 0, TCBS, 0);
2610 /* disable channel interrupt */
2611 emu_wrptr(sc, 0, CLIEL, 0);
2612 emu_wrptr(sc, 0, CLIEH, 0);
2613 emu_wrptr(sc, 0, SOLEL, 0);
2614 emu_wrptr(sc, 0, SOLEH, 0);
2617 bus_dma_tag_destroy(sc->mem.dmat);
2619 if (!SLIST_EMPTY(&sc->mem.blocks))
2620 device_printf(sc->dev, "warning: memblock list not empty\n");
2622 SLIST_FOREACH(blk, &sc->mem.blocks, link)
2624 device_printf(sc->dev, "lost %d for %s\n", blk->pte_size, blk->owner);
2626 emu_free(&sc->mem, sc->mem.ptb_pages);
2627 emu_free(&sc->mem, sc->mem.silent_page);
2633 emu_read_ivar(device_t bus, device_t dev, int ivar_index, uintptr_t * result)
2635 struct sndcard_func *func = device_get_ivars(dev);
2636 struct emu_sc_info *sc = device_get_softc(bus);
2638 switch (ivar_index) {
2640 *result = func->func;
2643 *result = ((struct emu_pcminfo *)func->varinfo)->route;
2645 case EMU_VAR_ISEMU10K1:
2646 *result = sc->is_emu10k1;
2656 emu_write_ivar(device_t bus __unused, device_t dev __unused,
2657 int ivar_index, uintptr_t value __unused)
2660 switch (ivar_index) {
2670 emu_pci_probe(device_t dev)
2673 unsigned int thiscard = 0;
2676 vendor = pci_read_config(dev, PCIR_DEVVENDOR, /* bytes */ 2);
2677 if (vendor != 0x1102)
2678 return (ENXIO); /* Not Creative */
2680 thiscard = emu_getcard(dev);
2684 s = sbuf_new(NULL, NULL, 4096, 0);
2687 sbuf_printf(s, "Creative %s [%s]", emu_cards[thiscard].desc, emu_cards[thiscard].SBcode);
2690 device_set_desc_copy(dev, sbuf_data(s));
2691 return (BUS_PROBE_DEFAULT);
2696 emu_pci_attach(device_t dev)
2698 struct sndcard_func *func;
2699 struct emu_sc_info *sc;
2700 struct emu_pcminfo *pcminfo;
2701 struct emu_midiinfo *midiinfo[3];
2708 sc = device_get_softc(dev);
2710 /* Fill in the softc. */
2711 mtx_init(&sc->lock, "emu10kx", "bridge conf", MTX_DEF);
2712 mtx_init(&sc->rw, "emu10kx", "atomic op", MTX_DEF);
2714 sc->type = pci_get_devid(dev);
2715 sc->rev = pci_get_revid(dev);
2717 sc->enable_debug = 0;
2721 sc->broken_digital = 0;
2728 device_flags = emu_cards[emu_getcard(dev)].flags;
2729 if (device_flags & HAS_51)
2731 if (device_flags & HAS_71) {
2735 if (device_flags & IS_EMU10K1)
2737 if (device_flags & IS_EMU10K2)
2739 if (device_flags & IS_CA0102)
2741 if (device_flags & IS_CA0108)
2743 if ((sc->is_emu10k2) && (sc->rev == 4)) {
2745 sc->is_ca0102 = 1; /* for unknown Audigy 2 cards */
2747 if ((sc->is_ca0102 == 1) || (sc->is_ca0108 == 1))
2748 if (device_flags & IS_CARDBUS)
2751 if ((sc->is_emu10k1 + sc->is_emu10k2 + sc->is_ca0102 + sc->is_ca0108) != 1) {
2752 device_printf(sc->dev, "Unable to detect HW chipset\n");
2755 if (device_flags & BROKEN_DIGITAL)
2756 sc->broken_digital = 1;
2757 if (device_flags & HAS_AC97)
2760 sc->opcode_shift = 0;
2761 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
2762 sc->opcode_shift = 24;
2763 sc->high_operand_shift = 12;
2766 /* sc->fx_base = 0x0 */
2767 sc->input_base = 0x40;
2768 /* sc->p16vinput_base = 0x50; */
2769 sc->output_base = 0x60;
2770 sc->efxc_base = 0x80;
2771 /* sc->output32h_base = 0xa0; */
2772 /* sc->output32l_base = 0xb0; */
2773 sc->dsp_zero = 0xc0;
2774 /* 0xe0...0x100 are unknown */
2775 /* sc->tram_base = 0x200 */
2776 /* sc->tram_addr_base = 0x300 */
2777 sc->gpr_base = A_FXGPREGBASE;
2778 sc->num_gprs = 0x200;
2779 sc->code_base = A_MICROCODEBASE;
2780 sc->code_size = 0x800 / 2; /* 0x600-0xdff, 2048 words,
2781 * 1024 instructions */
2783 sc->mchannel_fx = 8;
2784 sc->num_fxbuses = 16;
2786 sc->num_outputs = 16;
2787 sc->address_mask = A_PTR_ADDRESS_MASK;
2789 if (sc->is_emu10k1) {
2790 sc->has_51 = 0; /* We don't support 5.1 sound Live! 5.1 */
2791 sc->opcode_shift = 20;
2792 sc->high_operand_shift = 10;
2793 sc->code_base = MICROCODEBASE;
2794 sc->code_size = 0x400 / 2; /* 0x400-0x7ff, 1024 words,
2795 * 512 instructions */
2796 sc->gpr_base = FXGPREGBASE;
2797 sc->num_gprs = 0x100;
2798 sc->input_base = 0x10;
2799 sc->output_base = 0x20;
2801 * XXX 5.1 Analog outputs are inside efxc address space!
2802 * They use ouput+0x11/+0x12 (=efxc+1/+2).
2803 * Don't use this efx registers for recording on SB Live! 5.1!
2805 sc->efxc_base = 0x30;
2806 sc->dsp_zero = 0x40;
2807 sc->mchannel_fx = 0;
2808 sc->num_fxbuses = 8;
2810 sc->num_outputs = 16;
2811 sc->address_mask = PTR_ADDRESS_MASK;
2813 if (sc->opcode_shift == 0)
2816 data = pci_read_config(dev, PCIR_COMMAND, 2);
2817 data |= (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN);
2818 pci_write_config(dev, PCIR_COMMAND, data, 2);
2819 data = pci_read_config(dev, PCIR_COMMAND, 2);
2821 pci_enable_busmaster(dev);
2824 sc->reg = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &i, RF_ACTIVE);
2825 if (sc->reg == NULL) {
2826 device_printf(dev, "unable to map register space\n");
2829 sc->st = rman_get_bustag(sc->reg);
2830 sc->sh = rman_get_bushandle(sc->reg);
2832 for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++)
2833 sc->timer[i] = 0; /* disable it */
2836 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i, RF_ACTIVE | RF_SHAREABLE);
2837 if ((sc->irq == NULL) || bus_setup_intr(dev, sc->irq, INTR_MPSAFE | INTR_TYPE_AV, emu_intr, sc, &sc->ih)) {
2838 device_printf(dev, "unable to map interrupt\n");
2841 if (emu_rm_init(sc) != 0) {
2842 device_printf(dev, "unable to create resource manager\n");
2846 if (emu_cardbus_init(sc) != 0) {
2847 device_printf(dev, "unable to initialize CardBus interface\n");
2850 sc->ctx = device_get_sysctl_ctx(dev);
2851 if (sc->ctx == NULL)
2853 sc->root = device_get_sysctl_tree(dev);
2854 if (sc->root == NULL)
2856 if (emu_init(sc) == -1) {
2857 device_printf(dev, "unable to initialize the card\n");
2860 if (emu10kx_dev_init(sc) == ENXIO) {
2861 device_printf(dev, "unable to create control device\n");
2864 snprintf(status, 255, "rev %d at io 0x%lx irq %ld", sc->rev, rman_get_start(sc->reg), rman_get_start(sc->irq));
2867 for (i = 0; i < NUM_G; i++) {
2868 sc->voice[i].vnum = i;
2869 sc->voice[i].slave = NULL;
2870 sc->voice[i].busy = 0;
2871 sc->voice[i].ismaster = 0;
2872 sc->voice[i].running = 0;
2873 sc->voice[i].b16 = 0;
2874 sc->voice[i].stereo = 0;
2875 sc->voice[i].speed = 0;
2876 sc->voice[i].start = 0;
2877 sc->voice[i].end = 0;
2882 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
2887 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
2888 if (pcminfo == NULL) {
2893 pcminfo->route = RT_FRONT;
2895 func->func = SCF_PCM;
2896 func->varinfo = pcminfo;
2897 sc->pcm[RT_FRONT] = device_add_child(dev, "pcm", -1);
2898 device_set_ivars(sc->pcm[RT_FRONT], func);
2900 #ifdef SND_EMU10KX_MULTICHANNEL
2902 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
2907 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
2908 if (pcminfo == NULL) {
2913 pcminfo->route = RT_REAR;
2915 func->func = SCF_PCM;
2916 func->varinfo = pcminfo;
2917 sc->pcm[RT_REAR] = device_add_child(dev, "pcm", -1);
2918 device_set_ivars(sc->pcm[RT_REAR], func);
2921 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
2926 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
2927 if (pcminfo == NULL) {
2932 pcminfo->route = RT_CENTER;
2934 func->func = SCF_PCM;
2935 func->varinfo = pcminfo;
2936 sc->pcm[RT_CENTER] = device_add_child(dev, "pcm", -1);
2937 device_set_ivars(sc->pcm[RT_CENTER], func);
2939 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
2944 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
2945 if (pcminfo == NULL) {
2950 pcminfo->route = RT_SUB;
2952 func->func = SCF_PCM;
2953 func->varinfo = pcminfo;
2954 sc->pcm[RT_SUB] = device_add_child(dev, "pcm", -1);
2955 device_set_ivars(sc->pcm[RT_SUB], func);
2959 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
2964 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
2965 if (pcminfo == NULL) {
2970 pcminfo->route = RT_SIDE;
2972 func->func = SCF_PCM;
2973 func->varinfo = pcminfo;
2974 sc->pcm[RT_SIDE] = device_add_child(dev, "pcm", -1);
2975 device_set_ivars(sc->pcm[RT_SIDE], func);
2977 #ifdef SND_EMU10KX_MCH_RECORDING
2978 /* MULTICHANNEL RECORDING */
2979 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
2984 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
2985 if (pcminfo == NULL) {
2990 pcminfo->route = RT_MCHRECORD;
2992 func->func = SCF_PCM;
2993 func->varinfo = pcminfo;
2994 sc->pcm[RT_MCHRECORD] = device_add_child(dev, "pcm", -1);
2995 device_set_ivars(sc->pcm[RT_MCHRECORD], func);
2997 #endif /* SMD_EMU10KX_MCH_RECORDING */
2998 #endif /* SND_EMU10KX_MULTICHANNEL */
3000 /* Midi Interface 1: Live!, Audigy, Audigy 2 */
3001 if ((sc->is_emu10k1) || (sc->is_emu10k2) || (sc->is_ca0102)) {
3002 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
3007 midiinfo[0] = malloc(sizeof(struct emu_midiinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
3008 if (midiinfo[0] == NULL) {
3012 midiinfo[0]->card = sc;
3013 if (sc->is_emu10k2 || (sc->is_ca0102)) {
3014 midiinfo[0]->port = A_MUDATA1;
3015 midiinfo[0]->portnr = 1;
3017 if (sc->is_emu10k1) {
3018 midiinfo[0]->port = MUDATA;
3019 midiinfo[0]->portnr = 1;
3021 func->func = SCF_MIDI;
3022 func->varinfo = midiinfo[0];
3023 sc->midi[0] = device_add_child(dev, "midi", -1);
3024 device_set_ivars(sc->midi[0], func);
3026 /* Midi Interface 2: Audigy, Audigy 2 (on AudigyDrive) */
3027 if (sc->is_emu10k2 || (sc->is_ca0102)) {
3028 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
3033 midiinfo[1] = malloc(sizeof(struct emu_midiinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
3034 if (midiinfo[1] == NULL) {
3038 midiinfo[1]->card = sc;
3040 midiinfo[1]->port = A_MUDATA2;
3041 midiinfo[1]->portnr = 2;
3043 func->func = SCF_MIDI;
3044 func->varinfo = midiinfo[1];
3045 sc->midi[1] = device_add_child(dev, "midi", -1);
3046 device_set_ivars(sc->midi[1], func);
3049 return (bus_generic_attach(dev));
3052 /* XXX can we just call emu_pci_detach here? */
3054 emu10kx_dev_uninit(sc);
3058 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->reg);
3060 bus_teardown_intr(dev, sc->irq, sc->ih);
3062 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
3063 mtx_destroy(&sc->lock);
3064 mtx_destroy(&sc->rw);
3069 emu_pci_detach(device_t dev)
3071 struct emu_sc_info *sc;
3073 device_t *childlist;
3076 sc = device_get_softc(dev);
3078 for (i = 0; i < RT_COUNT; i++) {
3079 if (sc->pcm[i] != NULL)
3080 r = device_delete_child(dev, sc->pcm[i]);
3084 if (sc->midi[0] != NULL)
3085 r = device_delete_child(dev, sc->midi[0]);
3088 if (sc->midi[1] != NULL)
3089 r = device_delete_child(dev, sc->midi[1]);
3092 (void)device_get_children(dev, &childlist, &devcount);
3093 for (i = 0; i < devcount - 1; i++) {
3094 device_printf(dev, "removing stale child %d (unit %d)\n", i, device_get_unit(childlist[i]));
3095 device_delete_child(dev, childlist[i]);
3097 free(childlist, M_TEMP);
3101 r = emu10kx_dev_uninit(sc);
3106 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->reg);
3107 bus_teardown_intr(dev, sc->irq, sc->ih);
3108 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
3109 mtx_destroy(&sc->lock);
3110 mtx_destroy(&sc->rw);
3111 return (bus_generic_detach(dev));
3113 /* add suspend, resume */
3114 static device_method_t emu_methods[] = {
3115 /* Device interface */
3116 DEVMETHOD(device_probe, emu_pci_probe),
3117 DEVMETHOD(device_attach, emu_pci_attach),
3118 DEVMETHOD(device_detach, emu_pci_detach),
3120 DEVMETHOD(bus_read_ivar, emu_read_ivar),
3121 DEVMETHOD(bus_write_ivar, emu_write_ivar),
3127 static driver_t emu_driver = {
3130 sizeof(struct emu_sc_info),
3137 emu_modevent(module_t mod __unused, int cmd, void *data __unused)
3143 break; /* Success */
3148 /* XXX Should we check state of pcm & midi subdevices here? */
3150 break; /* Success */
3161 static devclass_t emu_devclass;
3163 DRIVER_MODULE(snd_emu10kx, pci, emu_driver, emu_devclass, emu_modevent, NULL);
3164 DRIVER_MODULE(snd_emu10kx, cardbus, emu_driver, emu_devclass, emu_modevent, NULL);
3165 MODULE_VERSION(snd_emu10kx, SND_EMU10KX_PREFVER);