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
152 #define OUT_A_CENTER 0x11
153 #define OUT_A_SUB 0x12
156 #define A_IN_AC97_L 0x00
157 #define A_IN_AC97_R 0x01
158 #define A_IN_AC97 A_IN_AC97_L
159 #define A_IN_SPDIF_CD_L 0x02
160 #define A_IN_SPDIF_CD_R 0x03
161 #define A_IN_SPDIF_CD A_IN_SPDIF_CD_L
162 #define A_IN_O_SPDIF_L 0x04
163 #define A_IN_O_SPDIF_R 0x05
164 #define A_IN_O_SPDIF A_IN_O_SPDIF_L
165 #define A_IN_LINE2_L 0x08
166 #define A_IN_LINE2_R 0x09
167 #define A_IN_LINE2 A_IN_LINE2_L
168 #define A_IN_R_SPDIF_L 0x0a
169 #define A_IN_R_SPDIF_R 0x0b
170 #define A_IN_R_SPDIF A_IN_R_SPDIF_L
171 #define A_IN_AUX2_L 0x0c
172 #define A_IN_AUX2_R 0x0d
173 #define A_IN_AUX2 A_IN_AUX2_L
175 /* Audigiy Outputs */
176 #define A_OUT_D_FRONT_L 0x00
177 #define A_OUT_D_FRONT_R 0x01
178 #define A_OUT_D_FRONT A_OUT_D_FRONT_L
179 #define A_OUT_D_CENTER 0x02
180 #define A_OUT_D_SUB 0x03
181 #define A_OUT_D_SIDE_L 0x04
182 #define A_OUT_D_SIDE_R 0x05
183 #define A_OUT_D_SIDE A_OUT_D_SIDE_L
184 #define A_OUT_D_REAR_L 0x06
185 #define A_OUT_D_REAR_R 0x07
186 #define A_OUT_D_REAR A_OUT_D_REAR_L
188 /* on Audigy Platinum only */
189 #define A_OUT_HPHONE_L 0x04
190 #define A_OUT_HPHONE_R 0x05
191 #define A_OUT_HPHONE A_OUT_HPHONE_L
193 #define A_OUT_A_FRONT_L 0x08
194 #define A_OUT_A_FRONT_R 0x09
195 #define A_OUT_A_FRONT A_OUT_A_FRONT_L
196 #define A_OUT_A_CENTER 0x0a
197 #define A_OUT_A_SUB 0x0b
198 #define A_OUT_A_SIDE_L 0x0c
199 #define A_OUT_A_SIDE_R 0x0d
200 #define A_OUT_A_SIDE A_OUT_A_SIDE_L
201 #define A_OUT_A_REAR_L 0x0e
202 #define A_OUT_A_REAR_R 0x0f
203 #define A_OUT_A_REAR A_OUT_A_REAR_L
204 #define A_OUT_AC97_L 0x10
205 #define A_OUT_AC97_R 0x11
206 #define A_OUT_AC97 A_OUT_AC97_L
207 #define A_OUT_ADC_REC_L 0x16
208 #define A_OUT_ADC_REC_R 0x17
209 #define A_OUT_ADC_REC A_OUT_ADC_REC_L
211 #include "emu10k1-alsa%diked.h"
212 #include "p16v-alsa%diked.h"
213 #include "p17v-alsa%diked.h"
225 #define NUM_CACHES 10
227 #define EMU_MAX_GPR 512
228 #define EMU_MAX_IRQ_CONSUMERS 32
232 unsigned int b16:1, stereo:1, busy:1, running:1, ismaster:1;
239 struct emu_voice *slave;
245 SLIST_ENTRY(emu_memblk) link;
249 uint32_t pte_start, pte_size;
253 uint8_t bmap[EMU_MAXPAGES / 8];
256 bus_addr_t silent_page_addr;
257 bus_addr_t ptb_pages_addr;
259 SLIST_HEAD(, emu_memblk) blocks;
264 struct emu_sc_info *card;
266 signed int allocmap[EMU_MAX_GPR];
272 struct emu_intr_handler {
276 uint32_t(*irq_func) (void *softc, uint32_t irq);
281 struct mtx rw; /* Hardware exclusive access lock */
283 /* Hardware and subdevices */
291 bus_space_handle_t sh;
293 struct cdev *cdev; /* /dev/emu10k character device */
294 struct mtx emu10kx_lock;
296 struct sbuf emu10kx_sbuf;
301 struct resource *reg;
302 struct resource *irq;
306 struct emu_intr_handler ihandler[EMU_MAX_IRQ_CONSUMERS];
308 /* Card HW configuration */
309 unsigned int mchannel_fx;
310 unsigned int dsp_zero;
311 unsigned int code_base;
312 unsigned int code_size;
313 unsigned int gpr_base;
314 unsigned int num_gprs;
315 unsigned int input_base;
316 unsigned int output_base;
317 unsigned int efxc_base;
318 unsigned int opcode_shift;
319 unsigned int high_operand_shift;
320 unsigned int address_mask;
321 uint32_t is_emu10k1:1, is_emu10k2, is_ca0102, is_ca0108:1,
322 has_ac97:1, has_51:1, has_71:1,
323 enable_ir:1, enable_debug:1,
324 broken_digital:1, is_cardbus:1;
326 unsigned int num_inputs;
327 unsigned int num_outputs;
328 unsigned int num_fxbuses;
329 unsigned int routing_code_start;
330 unsigned int routing_code_end;
333 struct emu_voice voice[NUM_G]; /* Hardware voices */
334 uint32_t irq_mask[EMU_MAX_IRQ_CONSUMERS]; /* IRQ manager data */
335 int timer[EMU_MAX_IRQ_CONSUMERS]; /* timer */
338 struct emu_mem mem; /* memory */
341 int mixer_gpr[NUM_MIXERS];
342 int mixer_volcache[NUM_MIXERS];
343 int cache_gpr[NUM_CACHES];
344 struct sysctl_ctx_list *ctx;
345 struct sysctl_oid *root;
348 static void emu_setmap(void *arg, bus_dma_segment_t * segs, int nseg, int error);
349 static void* emu_malloc(struct emu_mem *mem, uint32_t sz, bus_addr_t * addr);
350 static void emu_free(struct emu_mem *mem, void *dmabuf);
351 static void* emu_memalloc(struct emu_mem *mem, uint32_t sz, bus_addr_t * addr, const char * owner);
352 static int emu_memfree(struct emu_mem *mem, void *membuf);
353 static int emu_memstart(struct emu_mem *mem, void *membuf);
356 static int emu10kx_dev_init(struct emu_sc_info *sc);
357 static int emu10kx_dev_uninit(struct emu_sc_info *sc);
358 static int emu10kx_prepare(struct emu_sc_info *sc, struct sbuf *s);
360 static void emumix_set_mode(struct emu_sc_info *sc, int mode);
361 static void emumix_set_spdif_mode(struct emu_sc_info *sc, int mode);
362 static void emumix_set_fxvol(struct emu_sc_info *sc, unsigned gpr, int32_t vol);
363 static void emumix_set_gpr(struct emu_sc_info *sc, unsigned gpr, int32_t val);
364 static int sysctl_emu_mixer_control(SYSCTL_HANDLER_ARGS);
366 static int emu_rm_init(struct emu_sc_info *sc);
367 static int emu_rm_uninit(struct emu_sc_info *sc);
368 static int emu_rm_gpr_alloc(struct emu_rm *rm, int count);
370 static int emu_getcard(device_t dev);
371 static uint32_t emu_rd_nolock(struct emu_sc_info *sc, unsigned int regno, unsigned int size);
372 static void emu_wr_nolock(struct emu_sc_info *sc, unsigned int regno, uint32_t data, unsigned int size);
373 static void emu_wr_cbptr(struct emu_sc_info *sc, uint32_t data);
375 static void emu_vstop(struct emu_sc_info *sc, char channel, int enable);
377 static void emu_intr(void *p);
378 static void emu_wrefx(struct emu_sc_info *sc, unsigned int pc, unsigned int data);
379 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);
380 static void emu_initefx(struct emu_sc_info *sc);
382 static int emu_cardbus_init(struct emu_sc_info *sc);
383 static int emu_init(struct emu_sc_info *sc);
384 static int emu_uninit(struct emu_sc_info *sc);
386 static int emu_read_ivar(device_t bus __unused, device_t dev, int ivar_index, uintptr_t * result);
387 static int emu_write_ivar(device_t bus __unused, device_t dev __unused,
388 int ivar_index, uintptr_t value __unused);
390 static int emu_pci_probe(device_t dev);
391 static int emu_pci_attach(device_t dev);
392 static int emu_pci_detach(device_t dev);
393 static int emu_modevent(module_t mod __unused, int cmd, void *data __unused);
395 /* Supported cards */
406 static struct emu_hwinfo emu_cards[] = {
407 /* 0x0020..0x002f 4.0 EMU10K1 cards */
408 {0x1102, 0x0002, 0x1102, 0x0020, "CT4850", "SBLive! Value", HAS_AC97 | IS_EMU10K1},
409 {0x1102, 0x0002, 0x1102, 0x0021, "CT4620", "SBLive!", HAS_AC97 | IS_EMU10K1},
410 {0x1102, 0x0002, 0x1102, 0x002f, "CT????", "SBLive! mainboard implementation", HAS_AC97 | IS_EMU10K1},
412 /* (range unknown) 5.1 EMU10K1 cards */
413 {0x1102, 0x0002, 0x1102, 0x100a, "CT????", "SBLive! 5.1", HAS_AC97 | HAS_51 | IS_EMU10K1},
415 /* 0x80??..0x805? 4.0 EMU10K1 cards */
416 {0x1102, 0x0002, 0x1102, 0x8022, "CT4780", "SBLive! Value", HAS_AC97 | IS_EMU10K1},
417 {0x1102, 0x0002, 0x1102, 0x8023, "CT4790", "SB PCI512", HAS_AC97 | IS_EMU10K1},
418 {0x1102, 0x0002, 0x1102, 0x8024, "CT4760", "SBLive!", HAS_AC97 | IS_EMU10K1},
419 {0x1102, 0x0002, 0x1102, 0x8025, "CT????", "SBLive! Mainboard Implementation", HAS_AC97 | IS_EMU10K1},
420 {0x1102, 0x0002, 0x1102, 0x8026, "CT4830", "SBLive! Value", HAS_AC97 | IS_EMU10K1},
421 {0x1102, 0x0002, 0x1102, 0x8027, "CT4832", "SBLive! Value", HAS_AC97 | IS_EMU10K1},
422 {0x1102, 0x0002, 0x1102, 0x8028, "CT4760", "SBLive! OEM version", HAS_AC97 | IS_EMU10K1},
423 {0x1102, 0x0002, 0x1102, 0x8031, "CT4831", "SBLive! Value", HAS_AC97 | IS_EMU10K1},
424 {0x1102, 0x0002, 0x1102, 0x8040, "CT4760", "SBLive!", HAS_AC97 | IS_EMU10K1},
425 {0x1102, 0x0002, 0x1102, 0x8051, "CT4850", "SBLive! Value", HAS_AC97 | IS_EMU10K1},
427 /* 0x8061..0x???? 5.1 EMU10K1 cards */
428 {0x1102, 0x0002, 0x1102, 0x8061, "SB????", "SBLive! Player 5.1", HAS_AC97 | HAS_51 | IS_EMU10K1},
429 {0x1102, 0x0002, 0x1102, 0x8064, "SB????", "SBLive! 5.1", HAS_AC97 | HAS_51 | IS_EMU10K1},
430 {0x1102, 0x0002, 0x1102, 0x8065, "SB0220", "SBLive! 5.1 Digital", HAS_AC97 | HAS_51 | IS_EMU10K1},
431 {0x1102, 0x0002, 0x1102, 0x8066, "CT4780", "SBLive! 5.1 Digital", HAS_AC97 | HAS_51 | IS_EMU10K1},
432 {0x1102, 0x0002, 0x1102, 0x8067, "SB????", "SBLive!", HAS_AC97 | HAS_51 | IS_EMU10K1},
434 /* Generic SB Live! */
435 {0x1102, 0x0002, 0x1102, 0x0000, "SB????", "SBLive! (Unknown model)", HAS_AC97 | IS_EMU10K1},
437 /* 0x0041..0x0043 EMU10K2 (some kind of Audigy) cards */
439 /* 0x0051..0x0051 5.1 CA0100-IAF cards */
440 {0x1102, 0x0004, 0x1102, 0x0051, "SB0090", "Audigy", HAS_AC97 | HAS_51 | IS_EMU10K2},
441 /* ES is CA0100-IDF chip that don't work in digital mode */
442 {0x1102, 0x0004, 0x1102, 0x0052, "SB0160", "Audigy ES", HAS_AC97 | HAS_71 | IS_EMU10K2 | BROKEN_DIGITAL},
443 /* 0x0053..0x005C 5.1 CA0101-NAF cards */
444 {0x1102, 0x0004, 0x1102, 0x0053, "SB0090", "Audigy Player/OEM", HAS_AC97 | HAS_51 | IS_EMU10K2},
445 {0x1102, 0x0004, 0x1102, 0x0058, "SB0090", "Audigy Player/OEM", HAS_AC97 | HAS_51 | IS_EMU10K2},
447 /* 0x1002..0x1009 5.1 CA0102-IAT cards */
448 {0x1102, 0x0004, 0x1102, 0x1002, "SB????", "Audigy 2 Platinum", HAS_51 | IS_CA0102},
449 {0x1102, 0x0004, 0x1102, 0x1005, "SB????", "Audigy 2 Platinum EX", HAS_51 | IS_CA0102},
450 {0x1102, 0x0004, 0x1102, 0x1007, "SB0240", "Audigy 2", HAS_AC97 | HAS_51 | IS_CA0102},
452 /* 0x2001..0x2003 7.1 CA0102-ICT cards */
453 {0x1102, 0x0004, 0x1102, 0x2001, "SB0350", "Audigy 2 ZS", HAS_AC97 | HAS_71 | IS_CA0102},
454 {0x1102, 0x0004, 0x1102, 0x2002, "SB0350", "Audigy 2 ZS", HAS_AC97 | HAS_71 | IS_CA0102},
456 /* (range unknown) 7.1 CA0102-xxx Audigy 4 cards */
457 {0x1102, 0x0004, 0x1102, 0x2007, "SB0380", "Audigy 4 Pro", HAS_AC97 | HAS_71 | IS_CA0102},
459 /* Generic Audigy or Audigy 2 */
460 {0x1102, 0x0004, 0x1102, 0x0000, "SB????", "Audigy (Unknown model)", HAS_AC97 | HAS_51 | IS_EMU10K2},
462 /* We don't support CA0103-DAT (Audigy LS) cards */
463 /* There is NO CA0104-xxx cards */
464 /* There is NO CA0105-xxx cards */
465 /* We don't support CA0106-DAT (SB Live! 24 bit) cards */
466 /* There is NO CA0107-xxx cards */
468 /* 0x1000..0x1001 7.1 CA0108-IAT cards */
469 {0x1102, 0x0008, 0x1102, 0x1000, "SB????", "Audigy 2 LS", HAS_AC97 | HAS_51 | IS_CA0108 | DIGITAL_ONLY},
470 {0x1102, 0x0008, 0x1102, 0x1001, "SB0400", "Audigy 2 Value", HAS_AC97 | HAS_71 | IS_CA0108 | DIGITAL_ONLY},
471 {0x1102, 0x0008, 0x1102, 0x1021, "SB0610", "Audigy 4", HAS_AC97 | HAS_71 | IS_CA0108 | DIGITAL_ONLY},
473 {0x1102, 0x0008, 0x1102, 0x2001, "SB0530", "Audigy 2 ZS CardBus", HAS_AC97 | HAS_71 | IS_CA0108 | IS_CARDBUS},
475 {0x1102, 0x0008, 0x0000, 0x0000, "SB????", "Audigy 2 Value (Unknown model)", HAS_AC97 | HAS_51 | IS_CA0108},
477 /* Unsupported cards */
479 static struct emu_hwinfo emu_bad_cards[] = {
480 /* APS cards should be possible to support */
481 {0x1102, 0x0002, 0x1102, 0x4001, "EMUAPS", "E-mu APS", 0},
482 {0x1102, 0x0002, 0x1102, 0x4002, "EMUAPS", "E-mu APS", 0},
483 {0x1102, 0x0004, 0x1102, 0x4001, "EMU???", "E-mu 1212m [4001]", 0},
484 /* Similar-named ("Live!" or "Audigy") cards on different chipsets */
485 {0x1102, 0x8064, 0x0000, 0x0000, "SB0100", "SBLive! 5.1 OEM", 0},
486 {0x1102, 0x0006, 0x0000, 0x0000, "SB0200", "DELL OEM SBLive! Value", 0},
487 {0x1102, 0x0007, 0x0000, 0x0000, "SB0310", "Audigy LS", 0},
491 * Get best known information about device.
494 emu_getcard(device_t dev)
502 device = pci_read_config(dev, PCIR_DEVICE, /* bytes */ 2);
503 subdevice = pci_read_config(dev, PCIR_SUBDEV_0, /* bytes */ 2);
505 n_cards = sizeof(emu_cards) / sizeof(struct emu_hwinfo);
507 for (i = 0; i < n_cards; i++) {
508 if (device == emu_cards[i].device) {
509 if (subdevice == emu_cards[i].subdevice) {
513 if (0x0000 == emu_cards[i].subdevice) {
515 /* don't break, we can get more specific card
516 * later in the list */
521 n_cards = sizeof(emu_bad_cards) / sizeof(struct emu_hwinfo);
522 for (i = 0; i < n_cards; i++) {
523 if (device == emu_bad_cards[i].device) {
524 if (subdevice == emu_bad_cards[i].subdevice) {
528 if (0x0000 == emu_bad_cards[i].subdevice) {
530 break; /* we avoid all this cards */
539 * Base hardware interface are 32 (Audigy) or 64 (Audigy2) registers.
540 * Some of them are used directly, some of them provide pointer / data pairs.
543 emu_rd_nolock(struct emu_sc_info *sc, unsigned int regno, unsigned int size)
546 KASSERT(sc != NULL, ("emu_rd: NULL sc"));
549 return (bus_space_read_1(sc->st, sc->sh, regno));
551 return (bus_space_read_2(sc->st, sc->sh, regno));
553 return (bus_space_read_4(sc->st, sc->sh, regno));
559 emu_wr_nolock(struct emu_sc_info *sc, unsigned int regno, uint32_t data, unsigned int size)
562 KASSERT(sc != NULL, ("emu_rd: NULL sc"));
565 bus_space_write_1(sc->st, sc->sh, regno, data);
568 bus_space_write_2(sc->st, sc->sh, regno, data);
571 bus_space_write_4(sc->st, sc->sh, regno, data);
576 * PTR / DATA interface. Access to EMU10Kx is made
577 * via (channel, register) pair. Some registers are channel-specific,
581 emu_rdptr(struct emu_sc_info *sc, unsigned int chn, unsigned int reg)
583 uint32_t ptr, val, mask, size, offset;
585 ptr = ((reg << 16) & sc->address_mask) | (chn & PTR_CHANNELNUM_MASK);
587 emu_wr_nolock(sc, PTR, ptr, 4);
588 val = emu_rd_nolock(sc, DATA, 4);
591 * XXX Some register numbers has data size and offset encoded in
592 * it to get only part of 32bit register. This use is not described
593 * in register name, be careful!
595 if (reg & 0xff000000) {
596 size = (reg >> 24) & 0x3f;
597 offset = (reg >> 16) & 0x1f;
598 mask = ((1 << size) - 1) << offset;
606 emu_wrptr(struct emu_sc_info *sc, unsigned int chn, unsigned int reg, uint32_t data)
608 uint32_t ptr, mask, size, offset;
609 ptr = ((reg << 16) & sc->address_mask) | (chn & PTR_CHANNELNUM_MASK);
611 emu_wr_nolock(sc, PTR, ptr, 4);
613 * XXX Another kind of magic encoding in register number. This can
614 * give you side effect - it will read previous data from register
615 * and change only required bits.
617 if (reg & 0xff000000) {
618 size = (reg >> 24) & 0x3f;
619 offset = (reg >> 16) & 0x1f;
620 mask = ((1 << size) - 1) << offset;
623 data |= emu_rd_nolock(sc, DATA, 4) & ~mask;
625 emu_wr_nolock(sc, DATA, data, 4);
629 * PTR2 / DATA2 interface. Access to P16v is made
630 * via (channel, register) pair. Some registers are channel-specific,
631 * some not. This interface is supported by CA0102 and CA0108 chips only.
634 emu_rd_p16vptr(struct emu_sc_info *sc, uint16_t chn, uint16_t reg)
639 emu_wr_nolock(sc, PTR2, (reg << 16) | chn, 4);
640 val = emu_rd_nolock(sc, DATA2, 4);
646 emu_wr_p16vptr(struct emu_sc_info *sc, uint16_t chn, uint16_t reg, uint32_t data)
650 emu_wr_nolock(sc, PTR2, (reg << 16) | chn, 4);
651 emu_wr_nolock(sc, DATA2, data, 4);
655 * XXX CardBus interface. Not tested on any real hardware.
658 emu_wr_cbptr(struct emu_sc_info *sc, uint32_t data)
662 /* 0x38 is IPE3 (CD S/PDIF interrupt pending register) on CA0102 Seems
663 * to be some reg/value accessible kind of config register on CardBus
664 * CA0108, with value(?) in top 16 bit, address(?) in low 16 */
666 val = emu_rd_nolock(sc, 0x38, 4);
667 emu_wr_nolock(sc, 0x38, data, 4);
668 val = emu_rd_nolock(sc, 0x38, 4);
673 * Direct hardware register access
676 emu_wr(struct emu_sc_info *sc, unsigned int regno, uint32_t data, unsigned int size)
680 emu_wr_nolock(sc, regno, data, size);
685 emu_rd(struct emu_sc_info *sc, unsigned int regno, unsigned int size)
690 rd = emu_rd_nolock(sc, regno, size);
696 * Enabling IR MIDI messages is another kind of black magic. It just
697 * has to be made this way. It really do it.
700 emu_enable_ir(struct emu_sc_info *sc)
705 if (sc->is_emu10k2 || sc->is_ca0102) {
706 iocfg = emu_rd_nolock(sc, A_IOCFG, 2);
707 emu_wr_nolock(sc, A_IOCFG, iocfg | A_IOCFG_GPOUT2, 2);
709 emu_wr_nolock(sc, A_IOCFG, iocfg | A_IOCFG_GPOUT1 | A_IOCFG_GPOUT2, 2);
711 emu_wr_nolock(sc, A_IOCFG, iocfg | A_IOCFG_GPOUT1, 2);
713 emu_wr_nolock(sc, A_IOCFG, iocfg, 2);
714 device_printf(sc->dev, "Audigy IR MIDI events enabled.\n");
717 if (sc->is_emu10k1) {
718 iocfg = emu_rd_nolock(sc, HCFG, 4);
719 emu_wr_nolock(sc, HCFG, iocfg | HCFG_GPOUT2, 4);
721 emu_wr_nolock(sc, HCFG, iocfg | HCFG_GPOUT1 | HCFG_GPOUT2, 4);
723 emu_wr_nolock(sc, HCFG, iocfg, 4);
724 device_printf(sc->dev, "SB Live! IR MIDI events enabled.\n");
732 * emu_timer_ - HW timer managment
735 emu_timer_create(struct emu_sc_info *sc)
740 for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++)
741 if (sc->timer[i] == 0) {
742 sc->timer[i] = -1; /* disable it */
751 emu_timer_set(struct emu_sc_info *sc, int timer, int delay)
755 RANGE(delay, 16, 1024);
756 RANGE(timer, 0, EMU_MAX_IRQ_CONSUMERS);
758 sc->timer[timer] = delay;
759 for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++)
760 if (sc->timerinterval > sc->timer[i])
761 sc->timerinterval = sc->timer[i];
763 emu_wr(sc, TIMER, sc->timerinterval & 0x03ff, 2);
768 emu_timer_enable(struct emu_sc_info *sc, int timer, int go)
776 if ((go == 1) && (sc->timer[timer] < 0))
777 sc->timer[timer] = -sc->timer[timer];
778 if ((go == 0) && (sc->timer[timer] > 0))
779 sc->timer[timer] = -sc->timer[timer];
782 for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++) {
783 if (sc->timerinterval > sc->timer[i])
784 sc->timerinterval = sc->timer[i];
785 if (sc->timer[i] > 0)
789 emu_wr(sc, TIMER, sc->timerinterval & 0x03ff, 2);
792 x = emu_rd(sc, INTE, 4);
793 x |= INTE_INTERVALTIMERENB;
794 emu_wr(sc, INTE, x, 4);
796 x = emu_rd(sc, INTE, 4);
797 x &= ~INTE_INTERVALTIMERENB;
798 emu_wr(sc, INTE, x, 4);
800 mtx_unlock(&sc->lock);
805 emu_timer_clear(struct emu_sc_info *sc, int timer)
807 emu_timer_enable(sc, timer, 0);
810 if (sc->timer[timer] != 0)
811 sc->timer[timer] = 0;
812 mtx_unlock(&sc->lock);
818 * emu_intr_ - HW interrupt handler managment
821 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)
827 for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++)
828 if (sc->ihandler[i].inte_mask == 0) {
829 sc->ihandler[i].inte_mask = inte_mask;
830 sc->ihandler[i].intr_mask = intr_mask;
831 sc->ihandler[i].softc = isc;
832 sc->ihandler[i].irq_func = func;
833 x = emu_rd(sc, INTE, 4);
835 emu_wr(sc, INTE, x, 4);
836 mtx_unlock(&sc->lock);
839 mtx_unlock(&sc->lock);
844 emu_intr_unregister(struct emu_sc_info *sc, int hnumber)
851 if (sc->ihandler[hnumber].inte_mask == 0) {
852 mtx_unlock(&sc->lock);
856 x = emu_rd(sc, INTE, 4);
857 x &= ~sc->ihandler[hnumber].inte_mask;
859 sc->ihandler[hnumber].inte_mask = 0;
860 sc->ihandler[hnumber].intr_mask = 0;
861 sc->ihandler[hnumber].softc = NULL;
862 sc->ihandler[hnumber].irq_func = NULL;
864 /* other interupt handlers may use this INTE value */
865 for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++)
866 if (sc->ihandler[i].inte_mask != 0)
867 x |= sc->ihandler[i].inte_mask;
869 emu_wr(sc, INTE, x, 4);
871 mtx_unlock(&sc->lock);
878 struct emu_sc_info *sc = (struct emu_sc_info *)p;
883 stat = emu_rd(sc, IPR, 4);
887 emu_wr(sc, IPR, stat, 4);
888 for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++) {
889 if ((((sc->ihandler[i].intr_mask) & stat) != 0) &&
890 (((void *)sc->ihandler[i].irq_func) != NULL)) {
891 ack |= sc->ihandler[i].irq_func(sc->ihandler[i].softc,
892 (sc->ihandler[i].intr_mask) & stat);
897 if ((sc->is_ca0102) || (sc->is_ca0108))
899 stat = emu_rd(sc, IPR2, 4);
903 emu_wr(sc, IPR2, stat, 4);
904 device_printf(sc->dev, "IPR2: %08x\n", stat);
905 break; /* to avoid infinite loop. shoud be removed
906 * after completion of P16V interface. */
911 stat = emu_rd(sc, IPR3, 4);
915 emu_wr(sc, IPR3, stat, 4);
916 device_printf(sc->dev, "IPR3: %08x\n", stat);
917 break; /* to avoid infinite loop. should be removed
918 * after completion of S/PDIF interface */
924 * Get data from private emu10kx structure for PCM buffer allocation.
925 * Used by PCM code only.
928 emu_gettag(struct emu_sc_info *sc)
930 return (sc->mem.dmat);
934 emu_setmap(void *arg, bus_dma_segment_t * segs, int nseg, int error)
936 bus_addr_t *phys = (bus_addr_t *) arg;
938 *phys = error ? 0 : (bus_addr_t) segs->ds_addr;
941 printf("emu10kx: setmap (%lx, %lx), nseg=%d, error=%d\n",
942 (unsigned long)segs->ds_addr, (unsigned long)segs->ds_len,
948 emu_malloc(struct emu_mem *mem, uint32_t sz, bus_addr_t * addr)
954 if (bus_dmamem_alloc(mem->dmat, &dmabuf, BUS_DMA_NOWAIT, &map))
956 if (bus_dmamap_load(mem->dmat, map, dmabuf, sz, emu_setmap, addr, 0) || !*addr)
962 emu_free(struct emu_mem *mem, void *dmabuf)
964 bus_dmamem_free(mem->dmat, dmabuf, NULL);
968 emu_memalloc(struct emu_mem *mem, uint32_t sz, bus_addr_t * addr, const char *owner)
970 uint32_t blksz, start, idx, ofs, tmp, found;
971 struct emu_memblk *blk;
974 blksz = sz / EMUPAGESIZE;
975 if (sz > (blksz * EMUPAGESIZE))
977 if (blksz > EMU_MAX_BUFSZ / EMUPAGESIZE)
979 /* find a free block in the bitmap */
982 while (!found && start + blksz < EMU_MAXPAGES) {
984 for (idx = start; idx < start + blksz; idx++)
985 if (mem->bmap[idx >> 3] & (1 << (idx & 7)))
992 blk = malloc(sizeof(*blk), M_DEVBUF, M_NOWAIT);
995 bzero(blk, sizeof(*blk));
996 membuf = emu_malloc(mem, sz, &blk->buf_addr);
997 *addr = blk->buf_addr;
998 if (membuf == NULL) {
1003 blk->pte_start = start;
1004 blk->pte_size = blksz;
1005 strncpy(blk->owner, owner, 15);
1006 blk->owner[15] = '\0';
1007 #ifdef SND_EMU10KX_DEBUG
1008 printf("emu10kx emu_memalloc: allocating %d for %s\n", blk->pte_size, blk->owner);
1011 for (idx = start; idx < start + blksz; idx++) {
1012 mem->bmap[idx >> 3] |= 1 << (idx & 7);
1013 tmp = (uint32_t) (u_long) ((uint8_t *) blk->buf_addr + ofs);
1014 mem->ptb_pages[idx] = (tmp << 1) | idx;
1017 SLIST_INSERT_HEAD(&mem->blocks, blk, link);
1022 emu_memfree(struct emu_mem *mem, void *membuf)
1025 struct emu_memblk *blk, *i;
1028 SLIST_FOREACH(i, &mem->blocks, link) {
1029 if (i->buf == membuf)
1034 #ifdef SND_EMU10KX_DEBUG
1035 printf("emu10kx emu_memfree: freeing %d for %s\n", blk->pte_size, blk->owner);
1037 SLIST_REMOVE(&mem->blocks, blk, emu_memblk, link);
1038 emu_free(mem, membuf);
1039 tmp = (uint32_t) (mem->silent_page_addr) << 1;
1040 for (idx = blk->pte_start; idx < blk->pte_start + blk->pte_size; idx++) {
1041 mem->bmap[idx >> 3] &= ~(1 << (idx & 7));
1042 mem->ptb_pages[idx] = tmp | idx;
1044 free(blk, M_DEVBUF);
1049 emu_memstart(struct emu_mem *mem, void *membuf)
1051 struct emu_memblk *blk, *i;
1054 SLIST_FOREACH(i, &mem->blocks, link) {
1055 if (i->buf == membuf)
1060 return (blk->pte_start);
1065 emu_rate_to_pitch(uint32_t rate)
1067 static uint32_t logMagTable[128] = {
1068 0x00000, 0x02dfc, 0x05b9e, 0x088e6, 0x0b5d6, 0x0e26f, 0x10eb3, 0x13aa2,
1069 0x1663f, 0x1918a, 0x1bc84, 0x1e72e, 0x2118b, 0x23b9a, 0x2655d, 0x28ed5,
1070 0x2b803, 0x2e0e8, 0x30985, 0x331db, 0x359eb, 0x381b6, 0x3a93d, 0x3d081,
1071 0x3f782, 0x41e42, 0x444c1, 0x46b01, 0x49101, 0x4b6c4, 0x4dc49, 0x50191,
1072 0x5269e, 0x54b6f, 0x57006, 0x59463, 0x5b888, 0x5dc74, 0x60029, 0x623a7,
1073 0x646ee, 0x66a00, 0x68cdd, 0x6af86, 0x6d1fa, 0x6f43c, 0x7164b, 0x73829,
1074 0x759d4, 0x77b4f, 0x79c9a, 0x7bdb5, 0x7dea1, 0x7ff5e, 0x81fed, 0x8404e,
1075 0x86082, 0x88089, 0x8a064, 0x8c014, 0x8df98, 0x8fef1, 0x91e20, 0x93d26,
1076 0x95c01, 0x97ab4, 0x9993e, 0x9b79f, 0x9d5d9, 0x9f3ec, 0xa11d8, 0xa2f9d,
1077 0xa4d3c, 0xa6ab5, 0xa8808, 0xaa537, 0xac241, 0xadf26, 0xafbe7, 0xb1885,
1078 0xb3500, 0xb5157, 0xb6d8c, 0xb899f, 0xba58f, 0xbc15e, 0xbdd0c, 0xbf899,
1079 0xc1404, 0xc2f50, 0xc4a7b, 0xc6587, 0xc8073, 0xc9b3f, 0xcb5ed, 0xcd07c,
1080 0xceaec, 0xd053f, 0xd1f73, 0xd398a, 0xd5384, 0xd6d60, 0xd8720, 0xda0c3,
1081 0xdba4a, 0xdd3b4, 0xded03, 0xe0636, 0xe1f4e, 0xe384a, 0xe512c, 0xe69f3,
1082 0xe829f, 0xe9b31, 0xeb3a9, 0xecc08, 0xee44c, 0xefc78, 0xf148a, 0xf2c83,
1083 0xf4463, 0xf5c2a, 0xf73da, 0xf8b71, 0xfa2f0, 0xfba57, 0xfd1a7, 0xfe8df
1085 static char logSlopeTable[128] = {
1086 0x5c, 0x5c, 0x5b, 0x5a, 0x5a, 0x59, 0x58, 0x58,
1087 0x57, 0x56, 0x56, 0x55, 0x55, 0x54, 0x53, 0x53,
1088 0x52, 0x52, 0x51, 0x51, 0x50, 0x50, 0x4f, 0x4f,
1089 0x4e, 0x4d, 0x4d, 0x4d, 0x4c, 0x4c, 0x4b, 0x4b,
1090 0x4a, 0x4a, 0x49, 0x49, 0x48, 0x48, 0x47, 0x47,
1091 0x47, 0x46, 0x46, 0x45, 0x45, 0x45, 0x44, 0x44,
1092 0x43, 0x43, 0x43, 0x42, 0x42, 0x42, 0x41, 0x41,
1093 0x41, 0x40, 0x40, 0x40, 0x3f, 0x3f, 0x3f, 0x3e,
1094 0x3e, 0x3e, 0x3d, 0x3d, 0x3d, 0x3c, 0x3c, 0x3c,
1095 0x3b, 0x3b, 0x3b, 0x3b, 0x3a, 0x3a, 0x3a, 0x39,
1096 0x39, 0x39, 0x39, 0x38, 0x38, 0x38, 0x38, 0x37,
1097 0x37, 0x37, 0x37, 0x36, 0x36, 0x36, 0x36, 0x35,
1098 0x35, 0x35, 0x35, 0x34, 0x34, 0x34, 0x34, 0x34,
1099 0x33, 0x33, 0x33, 0x33, 0x32, 0x32, 0x32, 0x32,
1100 0x32, 0x31, 0x31, 0x31, 0x31, 0x31, 0x30, 0x30,
1101 0x30, 0x30, 0x30, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f
1107 rate *= 11185; /* Scale 48000 to 0x20002380 */
1108 for (i = 31; i > 0; i--) {
1109 if (rate & 0x80000000) { /* Detect leading "1" */
1110 return (((uint32_t) (i - 15) << 20) +
1111 logMagTable[0x7f & (rate >> 24)] +
1112 (0x7f & (rate >> 17)) *
1113 logSlopeTable[0x7f & (rate >> 24)]);
1122 emu_rate_to_linearpitch(uint32_t rate)
1124 rate = (rate << 8) / 375;
1125 return ((rate >> 1) + (rate & 1));
1129 emu_valloc(struct emu_sc_info *sc)
1131 struct emu_voice *v;
1135 mtx_lock(&sc->lock);
1136 for (i = 0; i < NUM_G && sc->voice[i].busy; i++);
1141 mtx_unlock(&sc->lock);
1146 emu_vfree(struct emu_sc_info *sc, struct emu_voice *v)
1150 mtx_lock(&sc->lock);
1151 for (i = 0; i < NUM_G; i++) {
1152 if (v == &sc->voice[i] && sc->voice[i].busy) {
1154 /* XXX What we should do with mono channels?
1155 See -pcm.c emupchan_init for other side of
1157 if (v->slave != NULL)
1158 r = emu_memfree(&sc->mem, v->vbuf);
1161 mtx_unlock(&sc->lock);
1165 emu_vinit(struct emu_sc_info *sc, struct emu_voice *m, struct emu_voice *s,
1166 uint32_t sz, struct snd_dbuf *b)
1169 bus_addr_t tmp_addr;
1171 vbuf = emu_memalloc(&sc->mem, sz, &tmp_addr, "vinit");
1175 sndbuf_setup(b, vbuf, sz);
1176 m->start = emu_memstart(&sc->mem, vbuf) * EMUPAGESIZE;
1177 if (m->start == -1) {
1178 emu_memfree(&sc->mem, vbuf);
1181 m->end = m->start + sz;
1192 s->start = m->start;
1208 emu_vsetup(struct emu_voice *v, int fmt, int spd)
1211 v->b16 = (fmt & AFMT_16BIT) ? 1 : 0;
1212 v->stereo = (fmt & AFMT_STEREO) ? 1 : 0;
1213 if (v->slave != NULL) {
1214 v->slave->b16 = v->b16;
1215 v->slave->stereo = v->stereo;
1220 if (v->slave != NULL)
1221 v->slave->speed = v->speed;
1226 emu_vroute(struct emu_sc_info *sc, struct emu_route *rt, struct emu_voice *v)
1228 unsigned int routing[8], amounts[8];
1231 for (i = 0; i < 8; i++) {
1232 routing[i] = rt->routing_left[i];
1233 amounts[i] = rt->amounts_left[i];
1235 if ((v->stereo) && (v->ismaster == 0))
1236 for (i = 0; i < 8; i++) {
1237 routing[i] = rt->routing_right[i];
1238 amounts[i] = rt->amounts_right[i];
1241 if (sc->is_emu10k1) {
1242 emu_wrptr(sc, v->vnum, FXRT, ((routing[3] << 12) |
1245 (routing[0] << 0)) << 16);
1247 emu_wrptr(sc, v->vnum, A_FXRT1, (routing[3] << 24) |
1248 (routing[2] << 16) |
1251 emu_wrptr(sc, v->vnum, A_FXRT2, (routing[7] << 24) |
1252 (routing[6] << 16) |
1255 emu_wrptr(sc, v->vnum, A_SENDAMOUNTS, (amounts[7] << 24) |
1256 (amounts[6] << 26) |
1260 emu_wrptr(sc, v->vnum, PTRX, (amounts[0] << 8) | (amounts[1] << 0));
1261 emu_wrptr(sc, v->vnum, DSL, v->ea | (amounts[3] << 24));
1262 emu_wrptr(sc, v->vnum, PSST, v->sa | (amounts[2] << 24));
1263 if ((v->stereo) && (v->slave != NULL))
1264 emu_vroute(sc, rt, v->slave);
1268 emu_vwrite(struct emu_sc_info *sc, struct emu_voice *v)
1271 uint32_t am_2, am_3, start, val, silent_page;
1273 s = (v->stereo ? 1 : 0) + (v->b16 ? 1 : 0);
1275 v->sa = v->start >> s;
1276 v->ea = v->end >> s;
1280 emu_wrptr(sc, v->vnum, CPF, CPF_STEREO_MASK);
1282 emu_wrptr(sc, v->vnum, CPF, 0);
1284 val = v->stereo ? 28 : 30;
1285 val *= v->b16 ? 1 : 2;
1286 start = v->sa + val;
1288 am_3 = emu_rdptr(sc, v->vnum, DSL) & 0xff000000;
1289 emu_wrptr(sc, v->vnum, DSL, v->ea | am_3);
1290 am_2 = emu_rdptr(sc, v->vnum, PSST) & 0xff000000;
1291 emu_wrptr(sc, v->vnum, PSST, v->sa | am_2);
1293 emu_wrptr(sc, v->vnum, CCCA, start | (v->b16 ? 0 : CCCA_8BITSELECT));
1294 emu_wrptr(sc, v->vnum, Z1, 0);
1295 emu_wrptr(sc, v->vnum, Z2, 0);
1297 silent_page = ((uint32_t) (sc->mem.silent_page_addr) << 1) | MAP_PTI_MASK;
1298 emu_wrptr(sc, v->vnum, MAPA, silent_page);
1299 emu_wrptr(sc, v->vnum, MAPB, silent_page);
1301 emu_wrptr(sc, v->vnum, CVCF, CVCF_CURRENTFILTER_MASK);
1302 emu_wrptr(sc, v->vnum, VTFT, VTFT_FILTERTARGET_MASK);
1303 emu_wrptr(sc, v->vnum, ATKHLDM, 0);
1304 emu_wrptr(sc, v->vnum, DCYSUSM, DCYSUSM_DECAYTIME_MASK);
1305 emu_wrptr(sc, v->vnum, LFOVAL1, 0x8000);
1306 emu_wrptr(sc, v->vnum, LFOVAL2, 0x8000);
1307 emu_wrptr(sc, v->vnum, FMMOD, 0);
1308 emu_wrptr(sc, v->vnum, TREMFRQ, 0);
1309 emu_wrptr(sc, v->vnum, FM2FRQ2, 0);
1310 emu_wrptr(sc, v->vnum, ENVVAL, 0x8000);
1312 emu_wrptr(sc, v->vnum, ATKHLDV, ATKHLDV_HOLDTIME_MASK | ATKHLDV_ATTACKTIME_MASK);
1313 emu_wrptr(sc, v->vnum, ENVVOL, 0x8000);
1315 emu_wrptr(sc, v->vnum, PEFE_FILTERAMOUNT, 0x7f);
1316 emu_wrptr(sc, v->vnum, PEFE_PITCHAMOUNT, 0);
1317 if ((v->stereo) && (v->slave != NULL))
1318 emu_vwrite(sc, v->slave);
1322 emu_vstop(struct emu_sc_info *sc, char channel, int enable)
1326 reg = (channel & 0x20) ? SOLEH : SOLEL;
1329 reg |= channel << 16;
1330 emu_wrptr(sc, 0, reg, enable);
1334 emu_vtrigger(struct emu_sc_info *sc, struct emu_voice *v, int go)
1336 uint32_t pitch_target, initial_pitch;
1337 uint32_t cra, cs, ccis;
1342 cs = v->stereo ? 4 : 2;
1343 ccis = v->stereo ? 28 : 30;
1344 ccis *= v->b16 ? 1 : 2;
1345 sample = v->b16 ? 0x00000000 : 0x80808080;
1346 for (i = 0; i < cs; i++)
1347 emu_wrptr(sc, v->vnum, CD0 + i, sample);
1348 emu_wrptr(sc, v->vnum, CCR_CACHEINVALIDSIZE, 0);
1349 emu_wrptr(sc, v->vnum, CCR_READADDRESS, cra);
1350 emu_wrptr(sc, v->vnum, CCR_CACHEINVALIDSIZE, ccis);
1352 emu_wrptr(sc, v->vnum, IFATN, 0xff00);
1353 emu_wrptr(sc, v->vnum, VTFT, 0xffffffff);
1354 emu_wrptr(sc, v->vnum, CVCF, 0xffffffff);
1355 emu_wrptr(sc, v->vnum, DCYSUSV, 0x00007f7f);
1356 emu_vstop(sc, v->vnum, 0);
1358 pitch_target = emu_rate_to_linearpitch(v->speed);
1359 initial_pitch = emu_rate_to_pitch(v->speed) >> 8;
1360 emu_wrptr(sc, v->vnum, PTRX_PITCHTARGET, pitch_target);
1361 emu_wrptr(sc, v->vnum, CPF_CURRENTPITCH, pitch_target);
1362 emu_wrptr(sc, v->vnum, IP, initial_pitch);
1364 emu_wrptr(sc, v->vnum, PTRX_PITCHTARGET, 0);
1365 emu_wrptr(sc, v->vnum, CPF_CURRENTPITCH, 0);
1366 emu_wrptr(sc, v->vnum, IFATN, 0xffff);
1367 emu_wrptr(sc, v->vnum, VTFT, 0x0000ffff);
1368 emu_wrptr(sc, v->vnum, CVCF, 0x0000ffff);
1369 emu_wrptr(sc, v->vnum, IP, 0);
1370 emu_vstop(sc, v->vnum, 1);
1372 if ((v->stereo) && (v->slave != NULL))
1373 emu_vtrigger(sc, v->slave, go);
1377 emu_vpos(struct emu_sc_info *sc, struct emu_voice *v)
1381 s = (v->b16 ? 1 : 0) + (v->stereo ? 1 : 0);
1382 ptr = (emu_rdptr(sc, v->vnum, CCCA_CURRADDR) - (v->start >> s)) << s;
1383 return (ptr & ~0x0000001f);
1389 emu_wrefx(struct emu_sc_info *sc, unsigned int pc, unsigned int data)
1391 emu_wrptr(sc, 0, sc->code_base + pc, data);
1396 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)
1398 if ((*pc) + 1 > sc->code_size) {
1399 device_printf(sc->dev, "DSP CODE OVERRUN: attept to write past code_size (pc=%d)\n", (*pc));
1402 emu_wrefx(sc, (*pc) * 2, (x << sc->high_operand_shift) | y);
1403 emu_wrefx(sc, (*pc) * 2 + 1, (op << sc->opcode_shift) | (z << sc->high_operand_shift) | w);
1408 sysctl_emu_mixer_control(SYSCTL_HANDLER_ARGS)
1410 struct emu_sc_info *sc;
1418 new_vol = emumix_get_volume(sc, mixer_id);
1419 err = sysctl_handle_int(oidp, &new_vol, sizeof(new_vol), req);
1421 if (err || req->newptr == NULL)
1423 if (new_vol < 0 || new_vol > 100)
1425 emumix_set_volume(sc, mixer_id, new_vol);
1431 emu_addefxmixer(struct emu_sc_info *sc, const char *mix_name, const int mix_id, uint32_t defvolume)
1434 char sysctl_name[32];
1436 volgpr = emu_rm_gpr_alloc(sc->rm, 1);
1437 emumix_set_fxvol(sc, volgpr, defvolume);
1438 /* Mixer controls with NULL mix_name are handled by AC97 emulation
1439 code or PCM mixer. */
1440 if (mix_name != NULL) {
1441 /* Temporary sysctls should start with underscore,
1442 * see freebsd-current mailing list, emu10kx driver
1443 * discussion around 2006-05-24. */
1444 snprintf(sysctl_name, 32, "_%s", mix_name);
1445 SYSCTL_ADD_PROC(sc->ctx,
1446 SYSCTL_CHILDREN(sc->root),
1447 OID_AUTO, sysctl_name,
1448 CTLTYPE_INT | CTLFLAG_RW, sc, mix_id,
1449 sysctl_emu_mixer_control, "I","");
1455 /* allocate cache GPRs that will hold mixed output channels
1456 * and clear it on every DSP run.
1458 #define EFX_CACHE(CACHE_IDX) do { \
1459 sc->cache_gpr[CACHE_IDX] = emu_rm_gpr_alloc(sc->rm, 1); \
1460 emu_addefxop(sc, ACC3, \
1461 GPR(sc->cache_gpr[CACHE_IDX]), \
1468 /* Allocate GPR for volume control and route sound: OUT = OUT + IN * VOL */
1469 #define EFX_ROUTE(TITLE, INP_NR, IN_GPR_IDX, OUT_CACHE_IDX, DEF) do { \
1470 sc->mixer_gpr[IN_GPR_IDX] = emu_addefxmixer(sc, TITLE, IN_GPR_IDX, DEF); \
1471 sc->mixer_volcache[IN_GPR_IDX] = DEF; \
1472 emu_addefxop(sc, MACS, \
1473 GPR(sc->cache_gpr[OUT_CACHE_IDX]), \
1474 GPR(sc->cache_gpr[OUT_CACHE_IDX]), \
1476 GPR(sc->mixer_gpr[IN_GPR_IDX]), \
1480 /* allocate GPR, OUT = IN * VOL */
1481 #define EFX_OUTPUT(TITLE,OUT_CACHE_IDX, OUT_GPR_IDX, OUTP_NR, DEF) do { \
1482 sc->mixer_gpr[OUT_GPR_IDX] = emu_addefxmixer(sc, TITLE, OUT_GPR_IDX, DEF); \
1483 sc->mixer_volcache[OUT_GPR_IDX] = DEF; \
1484 emu_addefxop(sc, MACS, \
1487 GPR(sc->cache_gpr[OUT_CACHE_IDX]), \
1488 GPR(sc->mixer_gpr[OUT_GPR_IDX]), \
1492 /* like EFX_OUTPUT, but don't allocate mixer gpr */
1493 #define EFX_OUTPUTD(OUT_CACHE_IDX, OUT_GPR_IDX, OUTP_NR) do{ \
1494 emu_addefxop(sc, MACS, \
1497 GPR(sc->cache_gpr[OUT_CACHE_IDX]), \
1498 GPR(sc->mixer_gpr[OUT_GPR_IDX]), \
1503 emu_initefx(struct emu_sc_info *sc)
1509 if (sc->is_emu10k1) {
1510 emu_wrptr(sc, 0, DBG, EMU10K1_DBG_SINGLE_STEP);
1512 emu_wrptr(sc, 0, A_DBG, A_DBG_SINGLE_STEP);
1515 /* code size is in instructions */
1517 for (c = 0; c < sc->code_size; c++) {
1518 if (sc->is_emu10k1) {
1519 emu_addefxop(sc, ACC3, DSP_CONST(0x0), DSP_CONST(0x0), DSP_CONST(0x0), DSP_CONST(0x0), &pc);
1521 emu_addefxop(sc, SKIP, DSP_CONST(0x0), DSP_CONST(0x0), DSP_CONST(0xf), DSP_CONST(0x0), &pc);
1528 * DSP code below is not good, because:
1529 * 1. It can be written smaller, if it can use DSP accumulator register
1530 * instead of cache_gpr[].
1531 * 2. It can be more careful when volume is 100%, because in DSP
1532 * x*0x7fffffff may not be equal to x !
1536 for (c = 0; c < 16 ; c++) {
1537 emu_addefxop(sc, ACC3, OUTP(c), DSP_CONST(0), DSP_CONST(0), DSP_CONST(0), &pc);
1541 if (sc->is_emu10k1) {
1542 EFX_CACHE(C_FRONT_L);
1543 EFX_CACHE(C_FRONT_R);
1547 /* fx0 to front/record, 100%/muted by default */
1548 EFX_ROUTE("pcm_front_l", FX(0), M_FX0_FRONT_L, C_FRONT_L, 100);
1549 EFX_ROUTE("pcm_front_r", FX(1), M_FX1_FRONT_R, C_FRONT_R, 100);
1550 EFX_ROUTE("pcm_rec_l", FX(0), M_FX0_REC_L, C_REC_L, 0);
1551 EFX_ROUTE("pcm_rec_r", FX(1), M_FX1_REC_R, C_REC_R, 0);
1553 /* in0, from AC97 codec output */
1554 EFX_ROUTE("ac97_front_l", INP(IN_AC97_L), M_IN0_FRONT_L, C_FRONT_L, 0);
1555 EFX_ROUTE("ac97_front_r", INP(IN_AC97_R), M_IN0_FRONT_R, C_FRONT_R, 0);
1556 EFX_ROUTE("ac97_rec_l", INP(IN_AC97_L), M_IN0_REC_L, C_REC_L, 0);
1557 EFX_ROUTE("ac97_rec_r", INP(IN_AC97_R), M_IN0_REC_R, C_REC_R, 0);
1559 /* in1, from CD S/PDIF */
1560 EFX_ROUTE("cdspdif_front_l", INP(IN_SPDIF_CD_L), M_IN1_FRONT_L, C_FRONT_L, 0);
1561 EFX_ROUTE("cdspdif_front_r", INP(IN_SPDIF_CD_R), M_IN1_FRONT_R, C_FRONT_R, 0);
1562 EFX_ROUTE("cdspdif_rec_l", INP(IN_SPDIF_CD_L), M_IN1_REC_L, C_REC_L, 0);
1563 EFX_ROUTE("cdspdif_rec_r", INP(IN_SPDIF_CD_R), M_IN1_REC_R, C_REC_R, 0);
1565 /* in2, ZoomVide (???) */
1566 EFX_ROUTE("zoom_front_l", INP(IN_ZOOM_L), M_IN2_FRONT_L, C_FRONT_L, 0);
1567 EFX_ROUTE("zoom_front_r", INP(IN_ZOOM_R), M_IN2_FRONT_R, C_FRONT_R, 0);
1568 EFX_ROUTE("zoom_rec_l", INP(IN_ZOOM_L), M_IN2_REC_L, C_REC_L, 0);
1569 EFX_ROUTE("zoom_rec_r", INP(IN_ZOOM_R), M_IN2_REC_R, C_REC_R, 0);
1572 /* in3, TOSLink (???) */
1573 EFX_ROUTE("toslink_front_l", INP(IN_TOSLINK_L), M_IN3_FRONT_L, C_FRONT_L, 0);
1574 EFX_ROUTE("toslink_front_r", INP(IN_TOSLINK_R), M_IN3_FRONT_R, C_FRONT_R, 0);
1575 EFX_ROUTE("toslink_rec_l", INP(IN_TOSLINK_L), M_IN3_REC_L, C_REC_L, 0);
1576 EFX_ROUTE("toslink_rec_r", INP(IN_TOSLINK_R), M_IN3_REC_R, C_REC_R, 0);
1579 EFX_ROUTE("linein_front_l", INP(IN_LINE1_L), M_IN4_FRONT_L, C_FRONT_L, 0);
1580 EFX_ROUTE("linein_front_r", INP(IN_LINE1_R), M_IN4_FRONT_R, C_FRONT_R, 0);
1581 EFX_ROUTE("linein_rec_l", INP(IN_LINE1_L), M_IN4_REC_L, C_REC_L, 0);
1582 EFX_ROUTE("linein_rec_r", INP(IN_LINE1_R), M_IN4_REC_R, C_REC_R, 0);
1584 /* in5, on-card S/PDIF */
1585 EFX_ROUTE("spdif_front_l", INP(IN_COAX_SPDIF_L), M_IN5_FRONT_L, C_FRONT_L, 0);
1586 EFX_ROUTE("spdif_front_r", INP(IN_COAX_SPDIF_R), M_IN5_FRONT_R, C_FRONT_R, 0);
1587 EFX_ROUTE("spdif_rec_l", INP(IN_COAX_SPDIF_L), M_IN5_REC_L, C_REC_L, 0);
1588 EFX_ROUTE("spdif_rec_r", INP(IN_COAX_SPDIF_R), M_IN5_REC_R, C_REC_R, 0);
1590 /* in6, Line2 on Live!Drive */
1591 EFX_ROUTE("line2_front_l", INP(IN_LINE2_L), M_IN6_FRONT_L, C_FRONT_L, 0);
1592 EFX_ROUTE("line2_front_r", INP(IN_LINE2_R), M_IN6_FRONT_R, C_FRONT_R, 0);
1593 EFX_ROUTE("line2_rec_l", INP(IN_LINE2_L), M_IN6_REC_L, C_REC_L, 0);
1594 EFX_ROUTE("line2_rec_r", INP(IN_LINE2_R), M_IN6_REC_R, C_REC_R, 0);
1597 EFX_ROUTE("in7_front_l", INP(0xE), M_IN7_FRONT_L, C_FRONT_L, 0);
1598 EFX_ROUTE("in7_front_r", INP(0xF), M_IN7_FRONT_R, C_FRONT_R, 0);
1599 EFX_ROUTE("in7_rec_l", INP(0xE), M_IN7_REC_L, C_REC_L, 0);
1600 EFX_ROUTE("in7_rec_r", INP(0xF), M_IN7_REC_R, C_REC_R, 0);
1602 /* front output to both analog and digital */
1603 EFX_OUTPUT("master_front_l", C_FRONT_L, M_MASTER_FRONT_L, OUT_AC97_L, 100);
1604 EFX_OUTPUT("master_front_r", C_FRONT_R, M_MASTER_FRONT_R, OUT_AC97_R, 100);
1606 /* rec output to "ADC" */
1607 EFX_OUTPUT("master_rec_l", C_REC_L, M_MASTER_REC_L, OUT_ADC_REC_L, 100);
1608 EFX_OUTPUT("master_rec_r", C_REC_R, M_MASTER_REC_R, OUT_ADC_REC_R, 100);
1609 #ifdef SND_EMU10KX_MULTICHANNEL
1611 * Additional channel volume is controlled by mixer in
1612 * emu_dspmixer_set() in -pcm.c
1615 /* fx2/3 (pcm1) to rear */
1616 EFX_CACHE(C_REAR_L);
1617 EFX_CACHE(C_REAR_R);
1618 EFX_ROUTE(NULL, FX(2), M_FX2_REAR_L, C_REAR_L, 100);
1619 EFX_ROUTE(NULL, FX(3), M_FX3_REAR_R, C_REAR_R, 100);
1621 EFX_OUTPUT(NULL, C_REAR_L, M_MASTER_REAR_L, OUT_REAR_L, 100);
1622 EFX_OUTPUT(NULL, C_REAR_R, M_MASTER_REAR_R, OUT_REAR_R, 100);
1623 #else /* !SND_EMU10KX_MULTICHANNEL */
1624 EFX_OUTPUTD(C_FRONT_L, M_MASTER_FRONT_L, OUT_REAR_L);
1625 EFX_OUTPUTD(C_FRONT_R, M_MASTER_FRONT_R, OUT_REAR_R);
1627 } else /* emu10k2 and later */ {
1628 EFX_CACHE(C_FRONT_L);
1629 EFX_CACHE(C_FRONT_R);
1633 /* fx0 to front/record, 100%/muted by default */
1635 * FRONT_[L|R] is controlled by AC97 emulation in
1636 * emu_ac97_[read|write]_emulation in -pcm.c
1638 EFX_ROUTE(NULL, FX(0), M_FX0_FRONT_L, C_FRONT_L, 100);
1639 EFX_ROUTE(NULL, FX(1), M_FX1_FRONT_R, C_FRONT_R, 100);
1640 EFX_ROUTE("pcm_rec_l", FX(0), M_FX0_REC_L, C_REC_L, 0);
1641 EFX_ROUTE("pcm_rec_r", FX(1), M_FX1_REC_R, C_REC_R, 0);
1643 /* in0, from AC97 codec output */
1644 EFX_ROUTE("ac97_front_l", INP(A_IN_AC97_L), M_IN0_FRONT_L, C_FRONT_L, 100);
1645 EFX_ROUTE("ac97_front_r", INP(A_IN_AC97_R), M_IN0_FRONT_R, C_FRONT_R, 100);
1646 EFX_ROUTE("ac97_rec_l", INP(A_IN_AC97_L), M_IN0_REC_L, C_REC_L, 0);
1647 EFX_ROUTE("ac97_rec_r", INP(A_IN_AC97_R), M_IN0_REC_R, C_REC_R, 0);
1649 /* in1, from CD S/PDIF */
1650 EFX_ROUTE("cdspdif_front_l", INP(A_IN_SPDIF_CD_L), M_IN1_FRONT_L, C_FRONT_L, 0);
1651 EFX_ROUTE("cdspdif_front_r", INP(A_IN_SPDIF_CD_R), M_IN1_FRONT_R, C_FRONT_R, 0);
1652 EFX_ROUTE("cdspdif_rec_l", INP(A_IN_SPDIF_CD_L), M_IN1_REC_L, C_REC_L, 0);
1653 EFX_ROUTE("cdspdif_rec_r", INP(A_IN_SPDIF_CD_R), M_IN1_REC_R, C_REC_R, 0);
1655 /* in2, optical & coax S/PDIF on AudigyDrive*/
1656 EFX_ROUTE("ospdif_front_l", INP(A_IN_O_SPDIF_L), M_IN2_FRONT_L, C_FRONT_L, 0);
1657 EFX_ROUTE("ospdif_front_r", INP(A_IN_O_SPDIF_R), M_IN2_FRONT_R, C_FRONT_R, 0);
1658 EFX_ROUTE("ospdif_rec_l", INP(A_IN_O_SPDIF_L), M_IN2_REC_L, C_REC_L, 0);
1659 EFX_ROUTE("ospdif_rec_r", INP(A_IN_O_SPDIF_R), M_IN2_REC_R, C_REC_R, 0);
1662 EFX_ROUTE("in3_front_l", INP(0x6), M_IN3_FRONT_L, C_FRONT_L, 0);
1663 EFX_ROUTE("in3_front_r", INP(0x7), M_IN3_FRONT_R, C_FRONT_R, 0);
1664 EFX_ROUTE("in3_rec_l", INP(0x6), M_IN3_REC_L, C_REC_L, 0);
1665 EFX_ROUTE("in3_rec_r", INP(0x7), M_IN3_REC_R, C_REC_R, 0);
1667 /* in4, LineIn 2 on AudigyDrive */
1668 EFX_ROUTE("linein2_front_l", INP(A_IN_LINE2_L), M_IN4_FRONT_L, C_FRONT_L, 0);
1669 EFX_ROUTE("linein2_front_r", INP(A_IN_LINE2_R), M_IN4_FRONT_R, C_FRONT_R, 0);
1670 EFX_ROUTE("linein2_rec_l", INP(A_IN_LINE2_L), M_IN4_REC_L, C_REC_L, 0);
1671 EFX_ROUTE("linein2_rec_r", INP(A_IN_LINE2_R), M_IN4_REC_R, C_REC_R, 0);
1673 /* in5, on-card S/PDIF */
1674 EFX_ROUTE("spdif_front_l", INP(A_IN_R_SPDIF_L), M_IN5_FRONT_L, C_FRONT_L, 0);
1675 EFX_ROUTE("spdif_front_r", INP(A_IN_R_SPDIF_R), M_IN5_FRONT_R, C_FRONT_R, 0);
1676 EFX_ROUTE("spdif_rec_l", INP(A_IN_R_SPDIF_L), M_IN5_REC_L, C_REC_L, 0);
1677 EFX_ROUTE("spdif_rec_r", INP(A_IN_R_SPDIF_R), M_IN5_REC_R, C_REC_R, 0);
1679 /* in6, AUX2 on AudigyDrive */
1680 EFX_ROUTE("aux2_front_l", INP(A_IN_AUX2_L), M_IN6_FRONT_L, C_FRONT_L, 0);
1681 EFX_ROUTE("aux2_front_r", INP(A_IN_AUX2_R), M_IN6_FRONT_R, C_FRONT_R, 0);
1682 EFX_ROUTE("aux2_rec_l", INP(A_IN_AUX2_L), M_IN6_REC_L, C_REC_L, 0);
1683 EFX_ROUTE("aux2_rec_r", INP(A_IN_AUX2_R), M_IN6_REC_R, C_REC_R, 0);
1686 EFX_ROUTE("in7_front_l", INP(0xE), M_IN7_FRONT_L, C_FRONT_L, 0);
1687 EFX_ROUTE("in7_front_r", INP(0xF), M_IN7_FRONT_R, C_FRONT_R, 0);
1688 EFX_ROUTE("in7_rec_l", INP(0xE), M_IN7_REC_L, C_REC_L, 0);
1689 EFX_ROUTE("in7_rec_r", INP(0xF), M_IN7_REC_R, C_REC_R, 0);
1691 /* front output to headphones and alog and digital *front */
1692 /* volume controlled by AC97 emulation */
1693 EFX_OUTPUT(NULL, C_FRONT_L, M_MASTER_FRONT_L, A_OUT_A_FRONT_L, 100);
1694 EFX_OUTPUT(NULL, C_FRONT_R, M_MASTER_FRONT_R, A_OUT_A_FRONT_R, 100);
1695 EFX_OUTPUTD(C_FRONT_L, M_MASTER_FRONT_L, A_OUT_D_FRONT_L);
1696 EFX_OUTPUTD(C_FRONT_R, M_MASTER_FRONT_R, A_OUT_D_FRONT_R);
1697 EFX_OUTPUTD(C_FRONT_L, M_MASTER_FRONT_L, A_OUT_HPHONE_L);
1698 EFX_OUTPUTD(C_FRONT_R, M_MASTER_FRONT_R, A_OUT_HPHONE_R);
1700 /* rec output to "ADC" */
1701 /* volume controlled by AC97 emulation */
1702 EFX_OUTPUT(NULL, C_REC_L, M_MASTER_REC_L, A_OUT_ADC_REC_L, 100);
1703 EFX_OUTPUT(NULL, C_REC_R, M_MASTER_REC_R, A_OUT_ADC_REC_R, 100);
1704 #ifdef SND_EMU10KX_MULTICHANNEL
1706 * Additional channel volume is controlled by mixer in
1707 * emu_dspmixer_set() in -pcm.c
1710 /* fx2/3 (pcm1) to rear */
1711 EFX_CACHE(C_REAR_L);
1712 EFX_CACHE(C_REAR_R);
1713 EFX_ROUTE(NULL, FX(2), M_FX2_REAR_L, C_REAR_L, 100);
1714 EFX_ROUTE(NULL, FX(3), M_FX3_REAR_R, C_REAR_R, 100);
1716 EFX_OUTPUT(NULL, C_REAR_L, M_MASTER_REAR_L, A_OUT_A_REAR_L, 100);
1717 EFX_OUTPUT(NULL, C_REAR_R, M_MASTER_REAR_R, A_OUT_A_REAR_R, 100);
1718 EFX_OUTPUTD(C_REAR_L, M_MASTER_REAR_L, A_OUT_D_REAR_L);
1719 EFX_OUTPUTD(C_REAR_R, M_MASTER_REAR_R, A_OUT_D_REAR_R);
1721 /* fx4 (pcm2) to center */
1722 EFX_CACHE(C_CENTER);
1723 EFX_ROUTE(NULL, FX(4), M_FX4_CENTER, C_CENTER, 100);
1724 EFX_OUTPUT(NULL, C_CENTER, M_MASTER_CENTER, A_OUT_D_CENTER, 100);
1726 /* XXX in digital mode (default) this should be muted because
1727 this output is shared with digital out */
1728 EFX_OUTPUTD(C_CENTER, M_MASTER_CENTER, A_OUT_A_CENTER);
1730 /* fx5 (pcm3) to sub */
1732 EFX_ROUTE(NULL, FX(5), M_FX5_SUBWOOFER, C_SUB, 100);
1733 EFX_OUTPUT(NULL, C_SUB, M_MASTER_SUBWOOFER, A_OUT_D_SUB, 100);
1735 /* XXX in digital mode (default) this should be muted because
1736 this output is shared with digital out */
1737 EFX_OUTPUTD(C_SUB, M_MASTER_SUBWOOFER, A_OUT_A_SUB);
1740 /* XXX this will broke headphones on AudigyDrive */
1741 /* fx6/7 (pcm4) to side */
1742 EFX_CACHE(C_SIDE_L);
1743 EFX_CACHE(C_SIDE_R);
1744 EFX_ROUTE(NULL, FX(6), M_FX6_SIDE_L, C_SIDE_L, 100);
1745 EFX_ROUTE(NULL, FX(7), M_FX7_SIDE_R, C_SIDE_R, 100);
1746 EFX_OUTPUT(NULL, C_SIDE_L, M_MASTER_SIDE_L, A_OUT_A_SIDE_L, 100);
1747 EFX_OUTPUT(NULL, C_SIDE_R, M_MASTER_SIDE_R, A_OUT_A_SIDE_R, 100);
1748 EFX_OUTPUTD(C_SIDE_L, M_MASTER_SIDE_L, A_OUT_D_SIDE_L);
1749 EFX_OUTPUTD(C_SIDE_R, M_MASTER_SIDE_R, A_OUT_D_SIDE_R);
1751 #else /* !SND_EMU10KX_MULTICHANNEL */
1752 EFX_OUTPUTD(C_FRONT_L, M_MASTER_FRONT_L, A_OUT_A_REAR_L);
1753 EFX_OUTPUTD(C_FRONT_R, M_MASTER_FRONT_R, A_OUT_A_REAR_R);
1755 EFX_OUTPUTD(C_FRONT_L, M_MASTER_FRONT_L, A_OUT_D_REAR_L);
1756 EFX_OUTPUTD(C_FRONT_R, M_MASTER_FRONT_R, A_OUT_D_REAR_R);
1760 sc->routing_code_end = pc;
1763 if (sc->is_emu10k1) {
1764 emu_wrptr(sc, 0, DBG, 0);
1766 emu_wrptr(sc, 0, A_DBG, 0);
1771 static d_open_t emu10kx_open;
1772 static d_close_t emu10kx_close;
1773 static d_read_t emu10kx_read;
1775 static struct cdevsw emu10kx_cdevsw = {
1776 .d_open = emu10kx_open,
1777 .d_close = emu10kx_close,
1778 .d_read = emu10kx_read,
1779 .d_name = "emu10kx",
1780 .d_version = D_VERSION,
1785 emu10kx_open(struct cdev *i_dev, int flags __unused, int mode __unused, struct thread *td __unused)
1788 struct emu_sc_info *sc;
1790 sc = i_dev->si_drv1;
1791 mtx_lock(&sc->emu10kx_lock);
1792 if (sc->emu10kx_isopen) {
1793 mtx_unlock(&sc->emu10kx_lock);
1796 sc->emu10kx_isopen = 1;
1797 mtx_unlock(&sc->emu10kx_lock);
1798 if (sbuf_new(&sc->emu10kx_sbuf, NULL, 4096, 0) == NULL) {
1802 sc->emu10kx_bufptr = 0;
1803 error = (emu10kx_prepare(sc, &sc->emu10kx_sbuf) > 0) ? 0 : ENOMEM;
1806 mtx_lock(&sc->emu10kx_lock);
1807 sc->emu10kx_isopen = 0;
1808 mtx_unlock(&sc->emu10kx_lock);
1814 emu10kx_close(struct cdev *i_dev, int flags __unused, int mode __unused, struct thread *td __unused)
1816 struct emu_sc_info *sc;
1818 sc = i_dev->si_drv1;
1820 mtx_lock(&sc->emu10kx_lock);
1821 if (!(sc->emu10kx_isopen)) {
1822 mtx_unlock(&sc->emu10kx_lock);
1825 sbuf_delete(&sc->emu10kx_sbuf);
1826 sc->emu10kx_isopen = 0;
1827 mtx_unlock(&sc->emu10kx_lock);
1833 emu10kx_read(struct cdev *i_dev, struct uio *buf, int flag __unused)
1836 struct emu_sc_info *sc;
1838 sc = i_dev->si_drv1;
1839 mtx_lock(&sc->emu10kx_lock);
1840 if (!(sc->emu10kx_isopen)) {
1841 mtx_unlock(&sc->emu10kx_lock);
1844 mtx_unlock(&sc->emu10kx_lock);
1846 l = min(buf->uio_resid, sbuf_len(&sc->emu10kx_sbuf) - sc->emu10kx_bufptr);
1847 err = (l > 0) ? uiomove(sbuf_data(&sc->emu10kx_sbuf) + sc->emu10kx_bufptr, l, buf) : 0;
1848 sc->emu10kx_bufptr += l;
1854 emu10kx_prepare(struct emu_sc_info *sc, struct sbuf *s)
1858 sbuf_printf(s, "FreeBSD EMU10Kx Audio Driver\n");
1859 sbuf_printf(s, "\nHardware resource usage:\n");
1860 sbuf_printf(s, "DSP General Purpose Registers: %d used, %d total\n", sc->rm->num_used, sc->rm->num_gprs);
1861 sbuf_printf(s, "DSP Instruction Registers: %d used, %d total\n", sc->routing_code_end, sc->code_size);
1862 sbuf_printf(s, "Card supports");
1864 sbuf_printf(s, " AC97 codec");
1866 sbuf_printf(s, " NO AC97 codec");
1870 sbuf_printf(s, " and 7.1 output");
1872 sbuf_printf(s, " and 5.1 output");
1875 sbuf_printf(s, ", SBLive! DSP code");
1877 sbuf_printf(s, ", Audigy DSP code");
1879 sbuf_printf(s, ", Audigy DSP code with Audigy2 hacks");
1881 sbuf_printf(s, ", Audigy DSP code with Audigy2Value hacks");
1882 sbuf_printf(s, "\n");
1883 if (sc->broken_digital)
1884 sbuf_printf(s, "Digital mode unsupported\n");
1885 sbuf_printf(s, "\nInstalled devices:\n");
1886 for (i = 0; i < 5; i++)
1887 if (sc->pcm[i] != NULL)
1888 if (device_is_attached(sc->pcm[i])) {
1889 sbuf_printf(s, "%s on %s\n", device_get_desc(sc->pcm[i]), device_get_nameunit(sc->pcm[i]));
1891 if (sc->midi[0] != NULL)
1892 if (device_is_attached(sc->midi[0])) {
1893 sbuf_printf(s, "EMU10Kx MIDI Interface\n");
1894 sbuf_printf(s, "\tOn-card connector on %s\n", device_get_nameunit(sc->midi[0]));
1896 if (sc->midi[1] != NULL)
1897 if (device_is_attached(sc->midi[1])) {
1898 sbuf_printf(s, "\tOn-Drive connector on %s\n", device_get_nameunit(sc->midi[1]));
1900 if (sc->midi[0] != NULL)
1901 if (device_is_attached(sc->midi[0])) {
1902 sbuf_printf(s, "\tIR reciever MIDI events %s\n", sc->enable_ir ? "enabled" : "disabled");
1905 return (sbuf_len(s));
1910 emu10kx_dev_init(struct emu_sc_info *sc)
1914 mtx_init(&sc->emu10kx_lock, "kxdevlock", NULL, 0);
1915 unit = device_get_unit(sc->dev);
1917 sc->cdev = make_dev(&emu10kx_cdevsw, unit2minor(unit), UID_ROOT, GID_WHEEL, 0640, "emu10kx%d", unit);
1918 if (sc->cdev != NULL) {
1919 sc->cdev->si_drv1 = sc;
1926 emu10kx_dev_uninit(struct emu_sc_info *sc)
1931 mtx_lock(&sc->emu10kx_lock);
1932 if (sc->emu10kx_isopen) {
1933 mtx_unlock(&sc->emu10kx_lock);
1938 destroy_dev(sc->cdev);
1942 mtx_destroy(&sc->emu10kx_lock);
1946 /* resource manager */
1948 emu_rm_init(struct emu_sc_info *sc)
1954 rm = malloc(sizeof(struct emu_rm), M_DEVBUF, M_NOWAIT | M_ZERO);
1960 maxcount = sc->num_gprs;
1962 mtx_init(&(rm->gpr_lock), "emu10k", "gpr alloc", MTX_DEF);
1963 rm->num_gprs = (maxcount < EMU_MAX_GPR ? maxcount : EMU_MAX_GPR);
1964 for (i = 0; i < rm->num_gprs; i++)
1965 rm->allocmap[i] = 0;
1966 rm->last_free_gpr = 0;
1972 emu_rm_uninit(struct emu_sc_info *sc)
1974 #ifdef SND_EMU10KX_DEBUG
1977 mtx_lock(&(sc->rm->gpr_lock));
1978 for (i = 0; i < sc->rm->last_free_gpr; i++)
1979 if (sc->rm->allocmap[i] > 0)
1980 device_printf(sc->dev, "rm: gpr %d not free before uninit\n", i);
1981 mtx_unlock(&(sc->rm->gpr_lock));
1983 mtx_destroy(&(sc->rm->gpr_lock));
1984 free(sc->rm, M_DEVBUF);
1989 emu_rm_gpr_alloc(struct emu_rm *rm, int count)
1994 allocated_gpr = rm->num_gprs;
1995 /* try fast way first */
1996 mtx_lock(&(rm->gpr_lock));
1997 if (rm->last_free_gpr + count <= rm->num_gprs) {
1998 allocated_gpr = rm->last_free_gpr;
1999 rm->last_free_gpr += count;
2000 rm->allocmap[allocated_gpr] = count;
2001 for (i = 1; i < count; i++)
2002 rm->allocmap[allocated_gpr + i] = -(count - i);
2006 allocated_gpr = rm->num_gprs;
2007 while (i < rm->last_free_gpr - count) {
2008 if (rm->allocmap[i] > 0) {
2009 i += rm->allocmap[i];
2012 for (j = 1; j < count; j++) {
2013 if (rm->allocmap[i + j] != 0)
2014 allocated_gpr = rm->num_gprs;
2016 if (allocated_gpr == i)
2020 if (allocated_gpr + count < rm->last_free_gpr) {
2021 rm->allocmap[allocated_gpr] = count;
2022 for (i = 1; i < count; i++)
2023 rm->allocmap[allocated_gpr + i] = -(count - i);
2027 if (allocated_gpr == rm->num_gprs)
2028 allocated_gpr = (-1);
2029 if (allocated_gpr >= 0)
2030 rm->num_used += count;
2031 mtx_unlock(&(rm->gpr_lock));
2032 return (allocated_gpr);
2037 emumix_set_mode(struct emu_sc_info *sc, int mode)
2052 hcfg = HCFG_AUDIOENABLE | HCFG_AUTOMUTE;
2056 hcfg |= HCFG_JOYENABLE;
2059 hcfg |= HCFG_LOCKTANKCACHE_MASK;
2061 hcfg |= HCFG_CODECFORMAT_I2S | HCFG_JOYENABLE;
2064 if (mode == MODE_DIGITAL) {
2065 if (sc->broken_digital) {
2066 device_printf(sc->dev, "Digital mode is reported as broken on this card,\n");
2068 a_iocfg |= A_IOCFG_ENABLE_DIGITAL;
2069 hcfg |= HCFG_GPOUT0;
2072 if (mode == MODE_ANALOG)
2073 emumix_set_spdif_mode(sc, SPDIF_MODE_PCM);
2076 a_iocfg |= 0x80; /* XXX */
2078 if ((sc->is_ca0102) || (sc->is_ca0108))
2079 a_iocfg |= A_IOCFG_DISABLE_ANALOG; /* means "don't disable"
2080 on this two cards. Means "disable" on emu10k2. */
2083 a_iocfg |= 0x20; /* XXX */
2085 emu_wr(sc, HCFG, hcfg, 4);
2087 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
2088 tmp = emu_rd(sc, A_IOCFG, 2);
2090 emu_wr(sc, A_IOCFG, tmp, 2);
2094 * XXX Mute center/sub if we go digital on Audigy or later card.
2095 * Route to analog center / sub in emu_initef should be disabled
2096 * until this problem is fixed.
2101 emumix_set_spdif_mode(struct emu_sc_info *sc, int mode)
2106 case SPDIF_MODE_PCM:
2108 case SPDIF_MODE_AC3:
2109 device_printf(sc->dev, "AC3 mode does not work and disabled\n");
2115 spcs = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
2116 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
2117 SPCS_GENERATIONSTATUS | 0x00001200 | 0x00000000 |
2118 SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
2120 mode = SPDIF_MODE_PCM;
2122 emu_wrptr(sc, 0, SPCS0, spcs);
2123 emu_wrptr(sc, 0, SPCS1, spcs);
2124 emu_wrptr(sc, 0, SPCS2, spcs);
2127 #define L2L_POINTS 10
2129 static int l2l_df[L2L_POINTS] = {
2130 0x572C5CA, /* 100..90 */
2131 0x3211625, /* 90..80 */
2132 0x1CC1A76, /* 80..70 */
2133 0x108428F, /* 70..60 */
2134 0x097C70A, /* 60..50 */
2135 0x0572C5C, /* 50..40 */
2136 0x0321162, /* 40..30 */
2137 0x01CC1A7, /* 30..20 */
2138 0x0108428, /* 20..10 */
2139 0x016493D /* 10..0 */
2142 static int l2l_f[L2L_POINTS] = {
2143 0x4984461A, /* 90 */
2144 0x2A3968A7, /* 80 */
2145 0x18406003, /* 70 */
2146 0x0DEDC66D, /* 60 */
2147 0x07FFFFFF, /* 50 */
2148 0x04984461, /* 40 */
2149 0x02A3968A, /* 30 */
2150 0x01840600, /* 20 */
2151 0x00DEDC66, /* 10 */
2172 idx = (L2L_POINTS - 1) - log_t / (L2L_POINTS);
2173 lin = log_t % (L2L_POINTS);
2174 lin_t = l2l_df[idx] * lin + l2l_f[idx];
2180 emumix_set_fxvol(struct emu_sc_info *sc, unsigned gpr, int32_t vol)
2184 emumix_set_gpr(sc, gpr, vol);
2188 emumix_set_gpr(struct emu_sc_info *sc, unsigned gpr, int32_t val)
2191 emu_wrptr(sc, 0, GPR(gpr), val);
2195 emumix_set_volume(struct emu_sc_info *sc, int mixer_idx, int volume)
2198 RANGE(volume, 0, 100);
2199 if (mixer_idx < NUM_MIXERS) {
2200 sc->mixer_volcache[mixer_idx] = volume;
2201 emumix_set_fxvol(sc, sc->mixer_gpr[mixer_idx], volume);
2206 emumix_get_volume(struct emu_sc_info *sc, int mixer_idx)
2208 if ((mixer_idx < NUM_MIXERS) && (mixer_idx >= 0))
2209 return (sc->mixer_volcache[mixer_idx]);
2213 /* Init CardBus part */
2215 emu_cardbus_init(struct emu_sc_info *sc)
2219 * XXX May not need this if we have IPR3 handler.
2220 * Is it a real init calls, or IPR3 interrupt acknowledgments?
2221 * Looks much like "(data << 16) | register".
2223 emu_wr_cbptr(sc, (0x00d0 << 16) | 0x0000);
2224 emu_wr_cbptr(sc, (0x00d0 << 16) | 0x0001);
2225 emu_wr_cbptr(sc, (0x00d0 << 16) | 0x005f);
2226 emu_wr_cbptr(sc, (0x00d0 << 16) | 0x007f);
2228 emu_wr_cbptr(sc, (0x0090 << 16) | 0x007f);
2233 /* Probe and attach the card */
2235 emu_init(struct emu_sc_info *sc)
2243 /* disable audio and lock cache */
2244 emu_wr(sc, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, 4);
2246 /* reset recording buffers */
2247 emu_wrptr(sc, 0, MICBS, ADCBS_BUFSIZE_NONE);
2248 emu_wrptr(sc, 0, MICBA, 0);
2249 emu_wrptr(sc, 0, FXBS, ADCBS_BUFSIZE_NONE);
2250 emu_wrptr(sc, 0, FXBA, 0);
2251 emu_wrptr(sc, 0, ADCBS, ADCBS_BUFSIZE_NONE);
2252 emu_wrptr(sc, 0, ADCBA, 0);
2254 /* disable channel interrupt */
2255 emu_wr(sc, INTE, INTE_INTERVALTIMERENB | INTE_SAMPLERATETRACKER | INTE_PCIERRORENABLE, 4);
2256 emu_wrptr(sc, 0, CLIEL, 0);
2257 emu_wrptr(sc, 0, CLIEH, 0);
2258 emu_wrptr(sc, 0, SOLEL, 0);
2259 emu_wrptr(sc, 0, SOLEH, 0);
2261 /* disable P16V and S/PDIF interrupts */
2262 if ((sc->is_ca0102) || (sc->is_ca0108))
2263 emu_wr(sc, INTE2, 0, 4);
2266 emu_wr(sc, INTE3, 0, 4);
2268 /* init phys inputs and outputs */
2271 ac97slot = AC97SLOT_CNTR | AC97SLOT_LFE;
2273 ac97slot = AC97SLOT_CNTR | AC97SLOT_LFE | AC97SLOT_REAR_LEFT | AC97SLOT_REAR_RIGHT;
2276 emu_wrptr(sc, 0, AC97SLOT, ac97slot);
2278 if (sc->is_emu10k2) /* XXX for later cards? */
2279 emu_wrptr(sc, 0, SPBYPASS, 0xf00); /* What will happen if
2280 * we write 1 here? */
2282 if (bus_dma_tag_create( /* parent */ NULL, /* alignment */ 2, /* boundary */ 0,
2283 /* lowaddr */ 1 << 31, /* can only access 0-2gb */
2284 /* highaddr */ BUS_SPACE_MAXADDR,
2285 /* filter */ NULL, /* filterarg */ NULL,
2286 /* maxsize */ EMU_MAX_BUFSZ, /* nsegments */ 1, /* maxsegz */ 0x3ffff,
2287 /* flags */ 0, /* lockfunc */ busdma_lock_mutex,
2288 /* lockarg */ &Giant, &(sc->mem.dmat)) != 0) {
2289 device_printf(sc->dev, "unable to create dma tag\n");
2290 bus_dma_tag_destroy(sc->mem.dmat);
2294 SLIST_INIT(&sc->mem.blocks);
2295 sc->mem.ptb_pages = emu_malloc(&sc->mem, EMU_MAXPAGES * sizeof(uint32_t), &sc->mem.ptb_pages_addr);
2296 if (sc->mem.ptb_pages == NULL)
2299 sc->mem.silent_page = emu_malloc(&sc->mem, EMUPAGESIZE, &sc->mem.silent_page_addr);
2300 if (sc->mem.silent_page == NULL) {
2301 emu_free(&sc->mem, sc->mem.ptb_pages);
2304 /* Clear page with silence & setup all pointers to this page */
2305 bzero(sc->mem.silent_page, EMUPAGESIZE);
2306 tmp = (uint32_t) (sc->mem.silent_page_addr) << 1;
2307 for (i = 0; i < EMU_MAXPAGES; i++)
2308 sc->mem.ptb_pages[i] = tmp | i;
2310 for (ch = 0; ch < NUM_G; ch++) {
2311 emu_wrptr(sc, ch, MAPA, tmp | MAP_PTI_MASK);
2312 emu_wrptr(sc, ch, MAPB, tmp | MAP_PTI_MASK);
2314 emu_wrptr(sc, 0, PTB, (sc->mem.ptb_pages_addr));
2315 emu_wrptr(sc, 0, TCB, 0); /* taken from original driver */
2316 emu_wrptr(sc, 0, TCBS, 0); /* taken from original driver */
2318 /* init envelope engine */
2319 for (ch = 0; ch < NUM_G; ch++) {
2320 emu_wrptr(sc, ch, DCYSUSV, 0);
2321 emu_wrptr(sc, ch, IP, 0);
2322 emu_wrptr(sc, ch, VTFT, 0xffff);
2323 emu_wrptr(sc, ch, CVCF, 0xffff);
2324 emu_wrptr(sc, ch, PTRX, 0);
2325 emu_wrptr(sc, ch, CPF, 0);
2326 emu_wrptr(sc, ch, CCR, 0);
2328 emu_wrptr(sc, ch, PSST, 0);
2329 emu_wrptr(sc, ch, DSL, 0x10);
2330 emu_wrptr(sc, ch, CCCA, 0);
2331 emu_wrptr(sc, ch, Z1, 0);
2332 emu_wrptr(sc, ch, Z2, 0);
2333 emu_wrptr(sc, ch, FXRT, 0xd01c0000);
2335 emu_wrptr(sc, ch, ATKHLDM, 0);
2336 emu_wrptr(sc, ch, DCYSUSM, 0);
2337 emu_wrptr(sc, ch, IFATN, 0xffff);
2338 emu_wrptr(sc, ch, PEFE, 0);
2339 emu_wrptr(sc, ch, FMMOD, 0);
2340 emu_wrptr(sc, ch, TREMFRQ, 24); /* 1 Hz */
2341 emu_wrptr(sc, ch, FM2FRQ2, 24); /* 1 Hz */
2342 emu_wrptr(sc, ch, TEMPENV, 0);
2344 /*** these are last so OFF prevents writing ***/
2345 emu_wrptr(sc, ch, LFOVAL2, 0);
2346 emu_wrptr(sc, ch, LFOVAL1, 0);
2347 emu_wrptr(sc, ch, ATKHLDV, 0);
2348 emu_wrptr(sc, ch, ENVVOL, 0);
2349 emu_wrptr(sc, ch, ENVVAL, 0);
2351 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
2352 emu_wrptr(sc, ch, 0x4c, 0x0);
2353 emu_wrptr(sc, ch, 0x4d, 0x0);
2354 emu_wrptr(sc, ch, 0x4e, 0x0);
2355 emu_wrptr(sc, ch, 0x4f, 0x0);
2356 emu_wrptr(sc, ch, A_FXRT1, 0x3f3f3f3f);
2357 emu_wrptr(sc, ch, A_FXRT2, 0x3f3f3f3f);
2358 emu_wrptr(sc, ch, A_SENDAMOUNTS, 0x0);
2362 emumix_set_spdif_mode(sc, SPDIF_MODE_PCM);
2364 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108))
2365 emu_wrptr(sc, 0, A_SPDIF_SAMPLERATE, A_SPDIF_48000);
2368 * CAxxxx cards needs additional setup:
2369 * 1. Set I2S capture sample rate to 96000
2370 * 2. Disable P16v / P17v proceesing
2371 * 3. Allow EMU10K DSP inputs
2373 if ((sc->is_ca0102) || (sc->is_ca0108)) {
2375 spdif_sr = emu_rdptr(sc, 0, A_SPDIF_SAMPLERATE);
2376 spdif_sr &= 0xfffff1ff;
2377 spdif_sr |= A_I2S_CAPTURE_96000;
2378 emu_wrptr(sc, 0, A_SPDIF_SAMPLERATE, spdif_sr);
2380 /* Disable P16v processing */
2381 emu_wr_p16vptr(sc, 0, SRCSel, 0x14);
2383 /* Setup P16v/P17v sound routing */
2385 emu_wr_p16vptr(sc, 0, SRCMULTI_ENABLE, 0xFF00FF00);
2387 emu_wr_p16vptr(sc, 0, P17V_MIXER_I2S_ENABLE, 0xFF000000);
2388 emu_wr_p16vptr(sc, 0, P17V_MIXER_SPDIF_ENABLE, 0xFF000000);
2390 tmp = emu_rd(sc, A_IOCFG, 2);
2391 emu_wr(sc, A_IOCFG, tmp & ~0x8, 2);
2396 def_mode = MODE_ANALOG;
2397 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108))
2398 def_mode = MODE_DIGITAL;
2399 if (((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) && (sc->broken_digital)) {
2400 device_printf(sc->dev, "Audigy card initialized in analog mode.\n");
2401 def_mode = MODE_ANALOG;
2403 emumix_set_mode(sc, def_mode);
2406 tmp = emu_rd(sc, HCFG, 4);
2407 device_printf(sc->dev, "Card Configuration ( 0x%08x )\n", tmp);
2408 device_printf(sc->dev, "Card Configuration ( & 0xff000000 ) : %s%s%s%s%s%s%s%s\n",
2409 (tmp & 0x80000000 ? "[Legacy MPIC] " : ""),
2410 (tmp & 0x40000000 ? "[0x40] " : ""),
2411 (tmp & 0x20000000 ? "[0x20] " : ""),
2412 (tmp & 0x10000000 ? "[0x10] " : ""),
2413 (tmp & 0x08000000 ? "[0x08] " : ""),
2414 (tmp & 0x04000000 ? "[0x04] " : ""),
2415 (tmp & 0x02000000 ? "[0x02] " : ""),
2416 (tmp & 0x01000000 ? "[0x01]" : " "));
2417 device_printf(sc->dev, "Card Configuration ( & 0x00ff0000 ) : %s%s%s%s%s%s%s%s\n",
2418 (tmp & 0x00800000 ? "[0x80] " : ""),
2419 (tmp & 0x00400000 ? "[0x40] " : ""),
2420 (tmp & 0x00200000 ? "[Legacy INT] " : ""),
2421 (tmp & 0x00100000 ? "[0x10] " : ""),
2422 (tmp & 0x00080000 ? "[0x08] " : ""),
2423 (tmp & 0x00040000 ? "[Codec4] " : ""),
2424 (tmp & 0x00020000 ? "[Codec2] " : ""),
2425 (tmp & 0x00010000 ? "[I2S Codec]" : " "));
2426 device_printf(sc->dev, "Card Configuration ( & 0x0000ff00 ) : %s%s%s%s%s%s%s%s\n",
2427 (tmp & 0x00008000 ? "[0x80] " : ""),
2428 (tmp & 0x00004000 ? "[GPINPUT0] " : ""),
2429 (tmp & 0x00002000 ? "[GPINPUT1] " : ""),
2430 (tmp & 0x00001000 ? "[GPOUT0] " : ""),
2431 (tmp & 0x00000800 ? "[GPOUT1] " : ""),
2432 (tmp & 0x00000400 ? "[GPOUT2] " : ""),
2433 (tmp & 0x00000200 ? "[Joystick] " : ""),
2434 (tmp & 0x00000100 ? "[0x01]" : " "));
2435 device_printf(sc->dev, "Card Configuration ( & 0x000000ff ) : %s%s%s%s%s%s%s%s\n",
2436 (tmp & 0x00000080 ? "[0x80] " : ""),
2437 (tmp & 0x00000040 ? "[0x40] " : ""),
2438 (tmp & 0x00000020 ? "[0x20] " : ""),
2439 (tmp & 0x00000010 ? "[AUTOMUTE] " : ""),
2440 (tmp & 0x00000008 ? "[LOCKSOUNDCACHE] " : ""),
2441 (tmp & 0x00000004 ? "[LOCKTANKCACHE] " : ""),
2442 (tmp & 0x00000002 ? "[MUTEBUTTONENABLE] " : ""),
2443 (tmp & 0x00000001 ? "[AUDIOENABLE]" : " "));
2445 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
2446 tmp = emu_rd(sc, A_IOCFG, 2);
2447 device_printf(sc->dev, "Audigy Card Configuration ( 0x%04x )\n", tmp);
2448 device_printf(sc->dev, "Audigy Card Configuration ( & 0xff00 )");
2449 printf(" : %s%s%s%s%s%s%s%s\n",
2450 (tmp & 0x8000 ? "[Rear Speakers] " : ""),
2451 (tmp & 0x4000 ? "[Front Speakers] " : ""),
2452 (tmp & 0x2000 ? "[0x20] " : ""),
2453 (tmp & 0x1000 ? "[0x10] " : ""),
2454 (tmp & 0x0800 ? "[0x08] " : ""),
2455 (tmp & 0x0400 ? "[0x04] " : ""),
2456 (tmp & 0x0200 ? "[0x02] " : ""),
2457 (tmp & 0x0100 ? "[AudigyDrive Phones]" : " "));
2458 device_printf(sc->dev, "Audigy Card Configuration ( & 0x00ff )");
2459 printf(" : %s%s%s%s%s%s%s%s\n",
2460 (tmp & 0x0080 ? "[0x80] " : ""),
2461 (tmp & 0x0040 ? "[Mute AnalogOut] " : ""),
2462 (tmp & 0x0020 ? "[0x20] " : ""),
2463 (tmp & 0x0010 ? "[0x10] " : ""),
2464 (tmp & 0x0008 ? "[0x08] " : ""),
2465 (tmp & 0x0004 ? "[GPOUT0] " : ""),
2466 (tmp & 0x0002 ? "[GPOUT1] " : ""),
2467 (tmp & 0x0001 ? "[GPOUT2]" : " "));
2468 } /* is_emu10k2 or ca* */
2474 emu_uninit(struct emu_sc_info *sc)
2477 struct emu_memblk *blk;
2479 emu_wr(sc, INTE, 0, 4);
2480 for (ch = 0; ch < NUM_G; ch++)
2481 emu_wrptr(sc, ch, DCYSUSV, 0);
2482 for (ch = 0; ch < NUM_G; ch++) {
2483 emu_wrptr(sc, ch, VTFT, 0);
2484 emu_wrptr(sc, ch, CVCF, 0);
2485 emu_wrptr(sc, ch, PTRX, 0);
2486 emu_wrptr(sc, ch, CPF, 0);
2489 /* disable audio and lock cache */
2490 emu_wr(sc, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, 4);
2492 emu_wrptr(sc, 0, PTB, 0);
2493 /* reset recording buffers */
2494 emu_wrptr(sc, 0, MICBS, ADCBS_BUFSIZE_NONE);
2495 emu_wrptr(sc, 0, MICBA, 0);
2496 emu_wrptr(sc, 0, FXBS, ADCBS_BUFSIZE_NONE);
2497 emu_wrptr(sc, 0, FXBA, 0);
2498 emu_wrptr(sc, 0, FXWC, 0);
2499 emu_wrptr(sc, 0, ADCBS, ADCBS_BUFSIZE_NONE);
2500 emu_wrptr(sc, 0, ADCBA, 0);
2501 emu_wrptr(sc, 0, TCB, 0);
2502 emu_wrptr(sc, 0, TCBS, 0);
2504 /* disable channel interrupt */
2505 emu_wrptr(sc, 0, CLIEL, 0);
2506 emu_wrptr(sc, 0, CLIEH, 0);
2507 emu_wrptr(sc, 0, SOLEL, 0);
2508 emu_wrptr(sc, 0, SOLEH, 0);
2511 bus_dma_tag_destroy(sc->mem.dmat);
2513 if (!SLIST_EMPTY(&sc->mem.blocks))
2514 device_printf(sc->dev, "warning: memblock list not empty\n");
2516 SLIST_FOREACH(blk, &sc->mem.blocks, link)
2518 device_printf(sc->dev, "lost %d for %s\n", blk->pte_size, blk->owner);
2520 emu_free(&sc->mem, sc->mem.ptb_pages);
2521 emu_free(&sc->mem, sc->mem.silent_page);
2527 emu_read_ivar(device_t bus, device_t dev, int ivar_index, uintptr_t * result)
2529 struct sndcard_func *func = device_get_ivars(dev);
2530 struct emu_sc_info *sc = device_get_softc(bus);
2532 switch (ivar_index) {
2534 *result = func->func;
2537 *result = ((struct emu_pcminfo *)func->varinfo)->route;
2539 case EMU_VAR_ISEMU10K1:
2540 *result = sc->is_emu10k1;
2550 emu_write_ivar(device_t bus __unused, device_t dev __unused,
2551 int ivar_index, uintptr_t value __unused)
2554 switch (ivar_index) {
2564 emu_pci_probe(device_t dev)
2570 vendor = pci_read_config(dev, PCIR_DEVVENDOR, /* bytes */ 2);
2571 if (vendor != 0x1102)
2572 return (ENXIO); /* Not Creative */
2574 thiscard = emu_getcard(dev);
2578 s = sbuf_new(NULL, NULL, 4096, 0);
2581 sbuf_printf(s, "Creative %s [%s]", emu_cards[thiscard].desc, emu_cards[thiscard].SBcode);
2584 device_set_desc_copy(dev, sbuf_data(s));
2585 return (BUS_PROBE_DEFAULT);
2590 emu_pci_attach(device_t dev)
2592 struct sndcard_func *func;
2593 struct emu_sc_info *sc;
2594 struct emu_pcminfo *pcminfo;
2595 struct emu_midiinfo *midiinfo[3];
2602 sc = device_get_softc(dev);
2604 /* Fill in the softc. */
2605 mtx_init(&sc->lock, "emu10kx", "bridge conf", MTX_DEF);
2606 mtx_init(&sc->rw, "emu10kx", "atomic op", MTX_DEF);
2608 sc->type = pci_get_devid(dev);
2609 sc->rev = pci_get_revid(dev);
2611 sc->enable_debug = 0;
2615 sc->broken_digital = 0;
2622 device_flags = emu_cards[emu_getcard(dev)].flags;
2623 if (device_flags & HAS_51)
2625 if (device_flags & HAS_71) {
2629 if (device_flags & IS_EMU10K1)
2631 if (device_flags & IS_EMU10K2)
2633 if (device_flags & IS_CA0102)
2635 if (device_flags & IS_CA0108)
2637 if ((sc->is_emu10k2) && (sc->rev == 4)) {
2639 sc->is_ca0102 = 1; /* for unknown Audigy 2 cards */
2641 if ((sc->is_ca0102 == 1) || (sc->is_ca0108 == 1))
2642 if (device_flags & IS_CARDBUS)
2645 if ((sc->is_emu10k1 + sc->is_emu10k2 + sc->is_ca0102 + sc->is_ca0108) != 1) {
2646 device_printf(sc->dev, "Unable to detect HW chipset\n");
2649 if (device_flags & BROKEN_DIGITAL)
2650 sc->broken_digital = 1;
2651 if (device_flags & HAS_AC97)
2654 sc->opcode_shift = 0;
2655 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
2656 sc->opcode_shift = 24;
2657 sc->high_operand_shift = 12;
2658 sc->code_base = A_MICROCODEBASE;
2659 sc->code_size = 0x800 / 2; /* 0x600-0xdff, 2048 words,
2660 * 1024 instructions */
2661 sc->gpr_base = A_FXGPREGBASE;
2662 sc->num_gprs = 0x200;
2663 sc->input_base = 0x40;
2664 /* sc->p16vinput_base = 0x50; */
2665 sc->output_base = 0x60;
2666 sc->efxc_base = 0x80;
2667 /* sc->output32h_base = 0xa0; */
2668 /* sc->output32l_base = 0xb0; */
2669 sc->dsp_zero = 0xc0;
2670 sc->mchannel_fx = 8;
2671 sc->num_fxbuses = 16;
2673 sc->num_outputs = 16;
2674 sc->address_mask = A_PTR_ADDRESS_MASK;
2676 if (sc->is_emu10k1) {
2677 sc->has_51 = 0; /* We don't support 5.1 sound Live! 5.1 */
2678 sc->opcode_shift = 20;
2679 sc->high_operand_shift = 10;
2680 sc->code_base = MICROCODEBASE;
2681 sc->code_size = 0x400 / 2; /* 0x400-0x7ff, 1024 words,
2682 * 512 instructions */
2683 sc->gpr_base = FXGPREGBASE;
2684 sc->num_gprs = 0x100;
2685 sc->input_base = 0x10;
2686 sc->output_base = 0x20;
2687 sc->efxc_base = 0x30;
2688 sc->dsp_zero = 0x40;
2689 sc->mchannel_fx = 0;
2690 sc->num_fxbuses = 8;
2692 sc->num_outputs = 16;
2693 sc->address_mask = PTR_ADDRESS_MASK;
2695 if (sc->opcode_shift == 0)
2698 data = pci_read_config(dev, PCIR_COMMAND, 2);
2699 data |= (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN);
2700 pci_write_config(dev, PCIR_COMMAND, data, 2);
2701 data = pci_read_config(dev, PCIR_COMMAND, 2);
2703 pci_enable_busmaster(dev);
2706 sc->reg = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &i, RF_ACTIVE);
2707 if (sc->reg == NULL) {
2708 device_printf(dev, "unable to map register space\n");
2711 sc->st = rman_get_bustag(sc->reg);
2712 sc->sh = rman_get_bushandle(sc->reg);
2714 for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++)
2715 sc->timer[i] = 0; /* disable it */
2718 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i, RF_ACTIVE | RF_SHAREABLE);
2719 if ((sc->irq == NULL) || bus_setup_intr(dev, sc->irq, INTR_MPSAFE | INTR_TYPE_AV, emu_intr, sc, &sc->ih)) {
2720 device_printf(dev, "unable to map interrupt\n");
2723 if (emu_rm_init(sc) != 0) {
2724 device_printf(dev, "unable to create resource manager\n");
2728 if (emu_cardbus_init(sc) != 0) {
2729 device_printf(dev, "unable to initialize CardBus interface\n");
2732 sc->ctx = device_get_sysctl_ctx(dev);
2733 if (sc->ctx == NULL)
2735 sc->root = device_get_sysctl_tree(dev);
2736 if (sc->root == NULL)
2738 if (emu_init(sc) == -1) {
2739 device_printf(dev, "unable to initialize the card\n");
2742 if (emu10kx_dev_init(sc) == ENXIO) {
2743 device_printf(dev, "unable to create control device\n");
2746 snprintf(status, 255, "rev %d at io 0x%lx irq %ld", sc->rev, rman_get_start(sc->reg), rman_get_start(sc->irq));
2749 for (i = 0; i < NUM_G; i++) {
2750 sc->voice[i].vnum = i;
2751 sc->voice[i].slave = NULL;
2752 sc->voice[i].busy = 0;
2753 sc->voice[i].ismaster = 0;
2754 sc->voice[i].running = 0;
2755 sc->voice[i].b16 = 0;
2756 sc->voice[i].stereo = 0;
2757 sc->voice[i].speed = 0;
2758 sc->voice[i].start = 0;
2759 sc->voice[i].end = 0;
2764 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
2769 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
2770 if (pcminfo == NULL) {
2775 pcminfo->route = RT_FRONT;
2777 func->func = SCF_PCM;
2778 func->varinfo = pcminfo;
2779 sc->pcm[RT_FRONT] = device_add_child(dev, "pcm", -1);
2780 device_set_ivars(sc->pcm[RT_FRONT], func);
2782 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
2787 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
2788 if (pcminfo == NULL) {
2792 #ifdef SND_EMU10KX_MULTICHANNEL
2794 pcminfo->route = RT_REAR;
2796 func->func = SCF_PCM;
2797 func->varinfo = pcminfo;
2798 sc->pcm[RT_REAR] = device_add_child(dev, "pcm", -1);
2799 device_set_ivars(sc->pcm[RT_REAR], func);
2802 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
2807 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
2808 if (pcminfo == NULL) {
2813 pcminfo->route = RT_CENTER;
2815 func->func = SCF_PCM;
2816 func->varinfo = pcminfo;
2817 sc->pcm[RT_CENTER] = device_add_child(dev, "pcm", -1);
2818 device_set_ivars(sc->pcm[RT_CENTER], func);
2820 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
2825 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
2826 if (pcminfo == NULL) {
2831 pcminfo->route = RT_SUB;
2833 func->func = SCF_PCM;
2834 func->varinfo = pcminfo;
2835 sc->pcm[RT_SUB] = device_add_child(dev, "pcm", -1);
2836 device_set_ivars(sc->pcm[RT_SUB], func);
2840 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
2845 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO);
2846 if (pcminfo == NULL) {
2851 pcminfo->route = RT_SIDE;
2853 func->func = SCF_PCM;
2854 func->varinfo = pcminfo;
2855 sc->pcm[RT_SIDE] = device_add_child(dev, "pcm", -1);
2856 device_set_ivars(sc->pcm[RT_SIDE], func);
2858 #endif /* SND_EMU10KX_MULTICHANNEL */
2860 /* Midi Interface 1: Live!, Audigy, Audigy 2 */
2861 if ((sc->is_emu10k1) || (sc->is_emu10k2) || (sc->is_ca0102)) {
2862 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
2867 midiinfo[0] = malloc(sizeof(struct emu_midiinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
2868 if (midiinfo[0] == NULL) {
2872 midiinfo[0]->card = sc;
2873 if (sc->is_emu10k2 || (sc->is_ca0102)) {
2874 midiinfo[0]->port = A_MUDATA1;
2875 midiinfo[0]->portnr = 1;
2877 if (sc->is_emu10k1) {
2878 midiinfo[0]->port = MUDATA;
2879 midiinfo[0]->portnr = 1;
2881 func->func = SCF_MIDI;
2882 func->varinfo = midiinfo[0];
2883 sc->midi[0] = device_add_child(dev, "midi", -1);
2884 device_set_ivars(sc->midi[0], func);
2886 /* Midi Interface 2: Audigy, Audigy 2 (on AudigyDrive) */
2887 if (sc->is_emu10k2 || (sc->is_ca0102)) {
2888 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO);
2893 midiinfo[1] = malloc(sizeof(struct emu_midiinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
2894 if (midiinfo[1] == NULL) {
2898 midiinfo[1]->card = sc;
2900 midiinfo[1]->port = A_MUDATA2;
2901 midiinfo[1]->portnr = 2;
2903 func->func = SCF_MIDI;
2904 func->varinfo = midiinfo[1];
2905 sc->midi[1] = device_add_child(dev, "midi", -1);
2906 device_set_ivars(sc->midi[1], func);
2909 return (bus_generic_attach(dev));
2912 /* XXX can we just call emu_pci_detach here? */
2914 emu10kx_dev_uninit(sc);
2918 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->reg);
2920 bus_teardown_intr(dev, sc->irq, sc->ih);
2922 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
2923 mtx_destroy(&sc->lock);
2924 mtx_destroy(&sc->rw);
2929 emu_pci_detach(device_t dev)
2931 struct emu_sc_info *sc;
2933 device_t *childlist;
2936 sc = device_get_softc(dev);
2938 for (i = 0; i < 5; i++) {
2939 if (sc->pcm[i] != NULL)
2940 r = device_delete_child(dev, sc->pcm[i]);
2944 if (sc->midi[0] != NULL)
2945 r = device_delete_child(dev, sc->midi[0]);
2948 if (sc->midi[1] != NULL)
2949 r = device_delete_child(dev, sc->midi[1]);
2952 (void)device_get_children(dev, &childlist, &devcount);
2953 for (i = 0; i < devcount - 1; i++) {
2954 device_printf(dev, "removing stale child %d (unit %d)\n", i, device_get_unit(childlist[i]));
2955 device_delete_child(dev, childlist[i]);
2957 free(childlist, M_TEMP);
2961 r = emu10kx_dev_uninit(sc);
2966 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->reg);
2967 bus_teardown_intr(dev, sc->irq, sc->ih);
2968 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
2969 mtx_destroy(&sc->lock);
2970 mtx_destroy(&sc->rw);
2971 return (bus_generic_detach(dev));
2973 /* add suspend, resume */
2974 static device_method_t emu_methods[] = {
2975 /* Device interface */
2976 DEVMETHOD(device_probe, emu_pci_probe),
2977 DEVMETHOD(device_attach, emu_pci_attach),
2978 DEVMETHOD(device_detach, emu_pci_detach),
2980 DEVMETHOD(bus_read_ivar, emu_read_ivar),
2981 DEVMETHOD(bus_write_ivar, emu_write_ivar),
2987 static driver_t emu_driver = {
2990 sizeof(struct emu_sc_info),
2997 emu_modevent(module_t mod __unused, int cmd, void *data __unused)
3003 break; /* Success */
3008 /* XXX Should we check state of pcm & midi subdevices here? */
3010 break; /* Success */
3021 static devclass_t emu_devclass;
3023 DRIVER_MODULE(snd_emu10kx, pci, emu_driver, emu_devclass, emu_modevent, NULL);
3024 DRIVER_MODULE(snd_emu10kx, cardbus, emu_driver, emu_devclass, emu_modevent, NULL);
3025 MODULE_VERSION(snd_emu10kx, SND_EMU10KX_PREFVER);