1 /*- 2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org> 3 * Copyright (c) 2003-2006 Yuriy Tsibizov <yuriy.tsibizov@gfk.ru> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 #include <sys/param.h> 31 #include <sys/types.h> 32 #include <sys/bus.h> 33 #include <machine/bus.h> 34 #include <sys/rman.h> 35 #include <sys/systm.h> 36 #include <sys/sbuf.h> 37 #include <sys/queue.h> 38 #include <sys/lock.h> 39 #include <sys/mutex.h> 40 #include <sys/sysctl.h> 41 42 #include <dev/pci/pcireg.h> 43 #include <dev/pci/pcivar.h> 44 45 #include <machine/clock.h> /* for DELAY */ 46 47 #include <dev/sound/chip.h> 48 #include <dev/sound/pcm/sound.h> 49 #include <dev/sound/pcm/ac97.h> 50 51 #include "opt_emu10kx.h" 52 #include <dev/sound/pci/emu10kx.h> 53 54 /* hw flags */ 55 #define HAS_51 0x0001 56 #define HAS_71 0x0002 57 #define HAS_AC97 0x0004 58 59 #define IS_EMU10K1 0x0008 60 #define IS_EMU10K2 0x0010 61 #define IS_CA0102 0x0020 62 #define IS_CA0108 0x0040 63 #define IS_UNKNOWN 0x0080 64 65 #define BROKEN_DIGITAL 0x0100 66 #define DIGITAL_ONLY 0x0200 67 68 #define IS_CARDBUS 0x0400 69 70 #define MODE_ANALOG 1 71 #define MODE_DIGITAL 2 72 #define SPDIF_MODE_PCM 1 73 #define SPDIF_MODE_AC3 2 74 75 #define MACS 0x0 76 #define MACS1 0x1 77 #define MACW 0x2 78 #define MACW1 0x3 79 #define MACINTS 0x4 80 #define MACINTW 0x5 81 #define ACC3 0x6 82 #define MACMV 0x7 83 #define ANDXOR 0x8 84 #define TSTNEG 0x9 85 #define LIMIT 0xA 86 #define LIMIT1 0xB 87 #define LOG 0xC 88 #define EXP 0xD 89 #define INTERP 0xE 90 #define SKIP 0xF 91 92 #define GPR(i) (sc->gpr_base+(i)) 93 #define INP(i) (sc->input_base+(i)) 94 #define OUTP(i) (sc->output_base+(i)) 95 #define FX(i) (i) 96 #define FX2(i) (sc->efxc_base+(i)) 97 #define DSP_CONST(i) (sc->dsp_zero+(i)) 98 99 #define COND_NORMALIZED DSP_CONST(0x1) 100 #define COND_BORROW DSP_CONST(0x2) 101 #define COND_MINUS DSP_CONST(0x3) 102 #define COND_LESS_ZERO DSP_CONST(0x4) 103 #define COND_EQ_ZERO DSP_CONST(0x5) 104 #define COND_SATURATION DSP_CONST(0x6) 105 #define COND_NEQ_ZERO DSP_CONST(0x8) 106 107 /* Live! Inputs */ 108 #define IN_AC97_L 0x00 109 #define IN_AC97_R 0x01 110 #define IN_AC97 IN_AC97_L 111 #define IN_SPDIF_CD_L 0x02 112 #define IN_SPDIF_CD_R 0x03 113 #define IN_SPDIF_CD IN_SPDIF_CD_L 114 #define IN_ZOOM_L 0x04 115 #define IN_ZOOM_R 0x05 116 #define IN_ZOOM IN_ZOOM_L 117 #define IN_TOSLINK_L 0x06 118 #define IN_TOSLINK_R 0x07 119 #define IN_TOSLINK IN_TOSLINK_L 120 #define IN_LINE1_L 0x08 121 #define IN_LINE1_R 0x09 122 #define IN_LINE1 IN_LINE1_L 123 #define IN_COAX_SPDIF_L 0x0a 124 #define IN_COAX_SPDIF_R 0x0b 125 #define IN_COAX_SPDIF IN_COAX_SPDIF_L 126 #define IN_LINE2_L 0x0c 127 #define IN_LINE2_R 0x0d 128 #define IN_LINE2 IN_LINE2_L 129 #define IN_0E 0x0e 130 #define IN_0F 0x0f 131 132 /* Outputs */ 133 #define OUT_AC97_L 0x00 134 #define OUT_AC97_R 0x01 135 #define OUT_AC97 OUT_AC97_L 136 #define OUT_A_FRONT OUT_AC97 137 #define OUT_TOSLINK_L 0x02 138 #define OUT_TOSLINK_R 0x03 139 #define OUT_TOSLINK OUT_TOSLINK_L 140 #define OUT_D_CENTER 0x04 141 #define OUT_D_SUB 0x05 142 #define OUT_HEADPHONE_L 0x06 143 #define OUT_HEADPHONE_R 0x07 144 #define OUT_HEADPHONE OUT_HEADPHONE_L 145 #define OUT_REAR_L 0x08 146 #define OUT_REAR_R 0x09 147 #define OUT_REAR OUT_REAR_L 148 #define OUT_ADC_REC_L 0x0a 149 #define OUT_ADC_REC_R 0x0b 150 #define OUT_ADC_REC OUT_ADC_REC_L 151 #define OUT_MIC_CAP 0x0c 152 #define OUT_A_CENTER 0x11 153 #define OUT_A_SUB 0x12 154 155 /* Audigy Inputs */ 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 174 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 187 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 192 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 210 211 #include "emu10k1-alsa%diked.h" 212 #include "p16v-alsa%diked.h" 213 #include "p17v-alsa%diked.h" 214 215 #define C_FRONT_L 0 216 #define C_FRONT_R 1 217 #define C_REC_L 2 218 #define C_REC_R 3 219 #define C_REAR_L 4 220 #define C_REAR_R 5 221 #define C_CENTER 6 222 #define C_SUB 7 223 #define C_SIDE_L 8 224 #define C_SIDE_R 9 225 #define NUM_CACHES 10 226 227 #define EMU_MAX_GPR 512 228 #define EMU_MAX_IRQ_CONSUMERS 32 229 230 struct emu_voice { 231 int vnum; 232 unsigned int b16:1, stereo:1, busy:1, running:1, ismaster:1; 233 int speed; 234 int start; 235 int end; 236 int vol; 237 uint32_t buf; 238 void *vbuf; 239 struct emu_voice *slave; 240 uint32_t sa; 241 uint32_t ea; 242 }; 243 244 struct emu_memblk { 245 SLIST_ENTRY(emu_memblk) link; 246 void *buf; 247 char owner[16]; 248 bus_addr_t buf_addr; 249 uint32_t pte_start, pte_size; 250 }; 251 252 struct emu_mem { 253 uint8_t bmap[EMU_MAXPAGES / 8]; 254 uint32_t *ptb_pages; 255 void *silent_page; 256 bus_addr_t silent_page_addr; 257 bus_addr_t ptb_pages_addr; 258 bus_dma_tag_t dmat; 259 SLIST_HEAD(, emu_memblk) blocks; 260 }; 261 262 /* rm */ 263 struct emu_rm { 264 struct emu_sc_info *card; 265 struct mtx gpr_lock; 266 signed int allocmap[EMU_MAX_GPR]; 267 int num_gprs; 268 int last_free_gpr; 269 int num_used; 270 }; 271 272 struct emu_intr_handler { 273 void* softc; 274 uint32_t intr_mask; 275 uint32_t inte_mask; 276 uint32_t(*irq_func) (void *softc, uint32_t irq); 277 }; 278 279 struct emu_sc_info { 280 struct mtx lock; 281 struct mtx rw; /* Hardware exclusive access lock */ 282 283 /* Hardware and subdevices */ 284 device_t dev; 285 device_t pcm[5]; 286 device_t midi[2]; 287 uint32_t type; 288 uint32_t rev; 289 290 bus_space_tag_t st; 291 bus_space_handle_t sh; 292 293 struct cdev *cdev; /* /dev/emu10k character device */ 294 struct mtx emu10kx_lock; 295 int emu10kx_isopen; 296 struct sbuf emu10kx_sbuf; 297 int emu10kx_bufptr; 298 299 300 /* Resources */ 301 struct resource *reg; 302 struct resource *irq; 303 void *ih; 304 305 /* IRQ handlers */ 306 struct emu_intr_handler ihandler[EMU_MAX_IRQ_CONSUMERS]; 307 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; 325 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; 331 332 /* HW resources */ 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 */ 336 int timerinterval; 337 struct emu_rm *rm; 338 struct emu_mem mem; /* memory */ 339 340 /* Mixer */ 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; 346 }; 347 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); 354 355 /* /dev */ 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); 359 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); 365 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); 369 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); 374 375 static void emu_vstop(struct emu_sc_info *sc, char channel, int enable); 376 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); 381 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); 385 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); 389 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); 394 395 /* Supported cards */ 396 struct emu_hwinfo { 397 uint16_t vendor; 398 uint16_t device; 399 uint16_t subvendor; 400 uint16_t subdevice; 401 char SBcode[8]; 402 char desc[32]; 403 int flags; 404 }; 405 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}, 411 412 /* (range unknown) 5.1 EMU10K1 cards */ 413 {0x1102, 0x0002, 0x1102, 0x100a, "CT????", "SBLive! 5.1", HAS_AC97 | HAS_51 | IS_EMU10K1}, 414 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}, 426 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}, 433 434 /* Generic SB Live! */ 435 {0x1102, 0x0002, 0x1102, 0x0000, "SB????", "SBLive! (Unknown model)", HAS_AC97 | IS_EMU10K1}, 436 437 /* 0x0041..0x0043 EMU10K2 (some kind of Audigy) cards */ 438 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}, 446 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}, 451 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}, 455 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}, 458 459 /* Generic Audigy or Audigy 2 */ 460 {0x1102, 0x0004, 0x1102, 0x0000, "SB????", "Audigy (Unknown model)", HAS_AC97 | HAS_51 | IS_EMU10K2}, 461 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 */ 467 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}, 472 473 {0x1102, 0x0008, 0x1102, 0x2001, "SB0530", "Audigy 2 ZS CardBus", HAS_AC97 | HAS_71 | IS_CA0108 | IS_CARDBUS}, 474 475 {0x1102, 0x0008, 0x0000, 0x0000, "SB????", "Audigy 2 Value (Unknown model)", HAS_AC97 | HAS_51 | IS_CA0108}, 476 }; 477 /* Unsupported cards */ 478 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}, 488 }; 489 490 /* 491 * Get best known information about device. 492 */ 493 static int 494 emu_getcard(device_t dev) 495 { 496 uint16_t device; 497 uint16_t subdevice; 498 int n_cards; 499 int thiscard; 500 int i; 501 502 device = pci_read_config(dev, PCIR_DEVICE, /* bytes */ 2); 503 subdevice = pci_read_config(dev, PCIR_SUBDEV_0, /* bytes */ 2); 504 505 n_cards = sizeof(emu_cards) / sizeof(struct emu_hwinfo); 506 thiscard = (-1); 507 for (i = 0; i < n_cards; i++) { 508 if (device == emu_cards[i].device) { 509 if (subdevice == emu_cards[i].subdevice) { 510 thiscard = i; 511 break; 512 } 513 if (0x0000 == emu_cards[i].subdevice) { 514 thiscard = i; 515 /* don't break, we can get more specific card 516 * later in the list */ 517 } 518 } 519 } 520 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) { 525 thiscard = (-1); 526 break; 527 } 528 if (0x0000 == emu_bad_cards[i].subdevice) { 529 thiscard = (-1); 530 break; /* we avoid all this cards */ 531 } 532 } 533 } 534 return (thiscard); 535 } 536 537 538 /* 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. 541 */ 542 static uint32_t 543 emu_rd_nolock(struct emu_sc_info *sc, unsigned int regno, unsigned int size) 544 { 545 546 KASSERT(sc != NULL, ("emu_rd: NULL sc")); 547 switch (size) { 548 case 1: 549 return (bus_space_read_1(sc->st, sc->sh, regno)); 550 case 2: 551 return (bus_space_read_2(sc->st, sc->sh, regno)); 552 case 4: 553 return (bus_space_read_4(sc->st, sc->sh, regno)); 554 } 555 return (0xffffffff); 556 } 557 558 static void 559 emu_wr_nolock(struct emu_sc_info *sc, unsigned int regno, uint32_t data, unsigned int size) 560 { 561 562 KASSERT(sc != NULL, ("emu_rd: NULL sc")); 563 switch (size) { 564 case 1: 565 bus_space_write_1(sc->st, sc->sh, regno, data); 566 break; 567 case 2: 568 bus_space_write_2(sc->st, sc->sh, regno, data); 569 break; 570 case 4: 571 bus_space_write_4(sc->st, sc->sh, regno, data); 572 break; 573 } 574 } 575 /* 576 * PTR / DATA interface. Access to EMU10Kx is made 577 * via (channel, register) pair. Some registers are channel-specific, 578 * some not. 579 */ 580 uint32_t 581 emu_rdptr(struct emu_sc_info *sc, unsigned int chn, unsigned int reg) 582 { 583 uint32_t ptr, val, mask, size, offset; 584 585 ptr = ((reg << 16) & sc->address_mask) | (chn & PTR_CHANNELNUM_MASK); 586 mtx_lock(&sc->rw); 587 emu_wr_nolock(sc, PTR, ptr, 4); 588 val = emu_rd_nolock(sc, DATA, 4); 589 mtx_unlock(&sc->rw); 590 /* 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! 594 */ 595 if (reg & 0xff000000) { 596 size = (reg >> 24) & 0x3f; 597 offset = (reg >> 16) & 0x1f; 598 mask = ((1 << size) - 1) << offset; 599 val &= mask; 600 val >>= offset; 601 } 602 return (val); 603 } 604 605 void 606 emu_wrptr(struct emu_sc_info *sc, unsigned int chn, unsigned int reg, uint32_t data) 607 { 608 uint32_t ptr, mask, size, offset; 609 ptr = ((reg << 16) & sc->address_mask) | (chn & PTR_CHANNELNUM_MASK); 610 mtx_lock(&sc->rw); 611 emu_wr_nolock(sc, PTR, ptr, 4); 612 /* 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. 616 */ 617 if (reg & 0xff000000) { 618 size = (reg >> 24) & 0x3f; 619 offset = (reg >> 16) & 0x1f; 620 mask = ((1 << size) - 1) << offset; 621 data <<= offset; 622 data &= mask; 623 data |= emu_rd_nolock(sc, DATA, 4) & ~mask; 624 } 625 emu_wr_nolock(sc, DATA, data, 4); 626 mtx_unlock(&sc->rw); 627 } 628 /* 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. 632 */ 633 uint32_t 634 emu_rd_p16vptr(struct emu_sc_info *sc, uint16_t chn, uint16_t reg) 635 { 636 uint32_t val; 637 638 mtx_lock(&sc->rw); 639 emu_wr_nolock(sc, PTR2, (reg << 16) | chn, 4); 640 val = emu_rd_nolock(sc, DATA2, 4); 641 mtx_unlock(&sc->rw); 642 return (val); 643 } 644 645 void 646 emu_wr_p16vptr(struct emu_sc_info *sc, uint16_t chn, uint16_t reg, uint32_t data) 647 { 648 649 mtx_lock(&sc->rw); 650 emu_wr_nolock(sc, PTR2, (reg << 16) | chn, 4); 651 emu_wr_nolock(sc, DATA2, data, 4); 652 mtx_unlock(&sc->rw); 653 } 654 /* 655 * XXX CardBus interface. Not tested on any real hardware. 656 */ 657 static void 658 emu_wr_cbptr(struct emu_sc_info *sc, uint32_t data) 659 { 660 uint32_t val; 661 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 */ 665 mtx_lock(&sc->rw); 666 val = emu_rd_nolock(sc, 0x38, 4); 667 emu_wr_nolock(sc, 0x38, data, 4); 668 val = emu_rd_nolock(sc, 0x38, 4); 669 mtx_unlock(&sc->rw); 670 } 671 672 /* 673 * Direct hardware register access 674 */ 675 void 676 emu_wr(struct emu_sc_info *sc, unsigned int regno, uint32_t data, unsigned int size) 677 { 678 679 mtx_lock(&sc->rw); 680 emu_wr_nolock(sc, regno, data, size); 681 mtx_unlock(&sc->rw); 682 } 683 684 uint32_t 685 emu_rd(struct emu_sc_info *sc, unsigned int regno, unsigned int size) 686 { 687 uint32_t rd; 688 689 mtx_lock(&sc->rw); 690 rd = emu_rd_nolock(sc, regno, size); 691 mtx_unlock(&sc->rw); 692 return (rd); 693 } 694 695 /* 696 * Enabling IR MIDI messages is another kind of black magic. It just 697 * has to be made this way. It really do it. 698 */ 699 void 700 emu_enable_ir(struct emu_sc_info *sc) 701 { 702 uint32_t iocfg; 703 704 mtx_lock(&sc->rw); 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); 708 DELAY(500); 709 emu_wr_nolock(sc, A_IOCFG, iocfg | A_IOCFG_GPOUT1 | A_IOCFG_GPOUT2, 2); 710 DELAY(500); 711 emu_wr_nolock(sc, A_IOCFG, iocfg | A_IOCFG_GPOUT1, 2); 712 DELAY(100); 713 emu_wr_nolock(sc, A_IOCFG, iocfg, 2); 714 device_printf(sc->dev, "Audigy IR MIDI events enabled.\n"); 715 sc->enable_ir = 1; 716 } 717 if (sc->is_emu10k1) { 718 iocfg = emu_rd_nolock(sc, HCFG, 4); 719 emu_wr_nolock(sc, HCFG, iocfg | HCFG_GPOUT2, 4); 720 DELAY(500); 721 emu_wr_nolock(sc, HCFG, iocfg | HCFG_GPOUT1 | HCFG_GPOUT2, 4); 722 DELAY(100); 723 emu_wr_nolock(sc, HCFG, iocfg, 4); 724 device_printf(sc->dev, "SB Live! IR MIDI events enabled.\n"); 725 sc->enable_ir = 1; 726 } 727 mtx_unlock(&sc->rw); 728 } 729 730 731 /* 732 * emu_timer_ - HW timer managment 733 */ 734 int 735 emu_timer_create(struct emu_sc_info *sc) 736 { 737 int i, timer; 738 739 timer = -1; 740 for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++) 741 if (sc->timer[i] == 0) { 742 sc->timer[i] = -1; /* disable it */ 743 timer = i; 744 return (timer); 745 } 746 747 return (-1); 748 } 749 750 int 751 emu_timer_set(struct emu_sc_info *sc, int timer, int delay) 752 { 753 int i; 754 755 RANGE(delay, 16, 1024); 756 RANGE(timer, 0, EMU_MAX_IRQ_CONSUMERS); 757 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]; 762 763 emu_wr(sc, TIMER, sc->timerinterval & 0x03ff, 2); 764 return (timer); 765 } 766 767 int 768 emu_timer_enable(struct emu_sc_info *sc, int timer, int go) 769 { 770 uint32_t x; 771 int ena_int; 772 int i; 773 774 mtx_lock(&sc->lock); 775 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]; 780 781 ena_int = 0; 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) 786 ena_int = 1; 787 } 788 789 emu_wr(sc, TIMER, sc->timerinterval & 0x03ff, 2); 790 791 if (ena_int == 1) { 792 x = emu_rd(sc, INTE, 4); 793 x |= INTE_INTERVALTIMERENB; 794 emu_wr(sc, INTE, x, 4); 795 } else { 796 x = emu_rd(sc, INTE, 4); 797 x &= ~INTE_INTERVALTIMERENB; 798 emu_wr(sc, INTE, x, 4); 799 } 800 mtx_unlock(&sc->lock); 801 return (0); 802 } 803 804 int 805 emu_timer_clear(struct emu_sc_info *sc, int timer) 806 { 807 emu_timer_enable(sc, timer, 0); 808 809 mtx_lock(&sc->lock); 810 if (sc->timer[timer] != 0) 811 sc->timer[timer] = 0; 812 mtx_unlock(&sc->lock); 813 814 return (timer); 815 } 816 817 /* 818 * emu_intr_ - HW interrupt handler managment 819 */ 820 int 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) 822 { 823 int i; 824 uint32_t x; 825 826 mtx_lock(&sc->lock); 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); 834 x |= inte_mask; 835 emu_wr(sc, INTE, x, 4); 836 mtx_unlock(&sc->lock); 837 return (i); 838 } 839 mtx_unlock(&sc->lock); 840 return (-1); 841 } 842 843 int 844 emu_intr_unregister(struct emu_sc_info *sc, int hnumber) 845 { 846 uint32_t x; 847 int i; 848 849 mtx_lock(&sc->lock); 850 851 if (sc->ihandler[hnumber].inte_mask == 0) { 852 mtx_unlock(&sc->lock); 853 return (-1); 854 } 855 856 x = emu_rd(sc, INTE, 4); 857 x &= ~sc->ihandler[hnumber].inte_mask; 858 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; 863 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; 868 869 emu_wr(sc, INTE, x, 4); 870 871 mtx_unlock(&sc->lock); 872 return (hnumber); 873 } 874 875 static void 876 emu_intr(void *p) 877 { 878 struct emu_sc_info *sc = (struct emu_sc_info *)p; 879 uint32_t stat, ack; 880 int i; 881 882 for (;;) { 883 stat = emu_rd(sc, IPR, 4); 884 ack = 0; 885 if (stat == 0) 886 break; 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); 893 } 894 } 895 } 896 897 if ((sc->is_ca0102) || (sc->is_ca0108)) 898 for (;;) { 899 stat = emu_rd(sc, IPR2, 4); 900 ack = 0; 901 if (stat == 0) 902 break; 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. */ 907 } 908 909 if (sc->is_ca0102) 910 for (;;) { 911 stat = emu_rd(sc, IPR3, 4); 912 ack = 0; 913 if (stat == 0) 914 break; 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 */ 919 } 920 } 921 922 923 /* 924 * Get data from private emu10kx structure for PCM buffer allocation. 925 * Used by PCM code only. 926 */ 927 bus_dma_tag_t 928 emu_gettag(struct emu_sc_info *sc) 929 { 930 return (sc->mem.dmat); 931 } 932 933 static void 934 emu_setmap(void *arg, bus_dma_segment_t * segs, int nseg, int error) 935 { 936 bus_addr_t *phys = (bus_addr_t *) arg; 937 938 *phys = error ? 0 : (bus_addr_t) segs->ds_addr; 939 940 if (bootverbose) { 941 printf("emu10kx: setmap (%lx, %lx), nseg=%d, error=%d\n", 942 (unsigned long)segs->ds_addr, (unsigned long)segs->ds_len, 943 nseg, error); 944 } 945 } 946 947 static void * 948 emu_malloc(struct emu_mem *mem, uint32_t sz, bus_addr_t * addr) 949 { 950 void *dmabuf; 951 bus_dmamap_t map; 952 953 *addr = 0; 954 if (bus_dmamem_alloc(mem->dmat, &dmabuf, BUS_DMA_NOWAIT, &map)) 955 return (NULL); 956 if (bus_dmamap_load(mem->dmat, map, dmabuf, sz, emu_setmap, addr, 0) || !*addr) 957 return (NULL); 958 return (dmabuf); 959 } 960 961 static void 962 emu_free(struct emu_mem *mem, void *dmabuf) 963 { 964 bus_dmamem_free(mem->dmat, dmabuf, NULL); 965 } 966 967 static void * 968 emu_memalloc(struct emu_mem *mem, uint32_t sz, bus_addr_t * addr, const char *owner) 969 { 970 uint32_t blksz, start, idx, ofs, tmp, found; 971 struct emu_memblk *blk; 972 void *membuf; 973 974 blksz = sz / EMUPAGESIZE; 975 if (sz > (blksz * EMUPAGESIZE)) 976 blksz++; 977 if (blksz > EMU_MAX_BUFSZ / EMUPAGESIZE) 978 return (NULL); 979 /* find a free block in the bitmap */ 980 found = 0; 981 start = 1; 982 while (!found && start + blksz < EMU_MAXPAGES) { 983 found = 1; 984 for (idx = start; idx < start + blksz; idx++) 985 if (mem->bmap[idx >> 3] & (1 << (idx & 7))) 986 found = 0; 987 if (!found) 988 start++; 989 } 990 if (!found) 991 return (NULL); 992 blk = malloc(sizeof(*blk), M_DEVBUF, M_NOWAIT); 993 if (blk == NULL) 994 return (NULL); 995 bzero(blk, sizeof(*blk)); 996 membuf = emu_malloc(mem, sz, &blk->buf_addr); 997 *addr = blk->buf_addr; 998 if (membuf == NULL) { 999 free(blk, M_DEVBUF); 1000 return (NULL); 1001 } 1002 blk->buf = membuf; 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); 1009 #endif 1010 ofs = 0; 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; 1015 ofs += EMUPAGESIZE; 1016 } 1017 SLIST_INSERT_HEAD(&mem->blocks, blk, link); 1018 return (membuf); 1019 } 1020 1021 static int 1022 emu_memfree(struct emu_mem *mem, void *membuf) 1023 { 1024 uint32_t idx, tmp; 1025 struct emu_memblk *blk, *i; 1026 1027 blk = NULL; 1028 SLIST_FOREACH(i, &mem->blocks, link) { 1029 if (i->buf == membuf) 1030 blk = i; 1031 } 1032 if (blk == NULL) 1033 return (EINVAL); 1034 #ifdef SND_EMU10KX_DEBUG 1035 printf("emu10kx emu_memfree: freeing %d for %s\n", blk->pte_size, blk->owner); 1036 #endif 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; 1043 } 1044 free(blk, M_DEVBUF); 1045 return (0); 1046 } 1047 1048 static int 1049 emu_memstart(struct emu_mem *mem, void *membuf) 1050 { 1051 struct emu_memblk *blk, *i; 1052 1053 blk = NULL; 1054 SLIST_FOREACH(i, &mem->blocks, link) { 1055 if (i->buf == membuf) 1056 blk = i; 1057 } 1058 if (blk == NULL) 1059 return (-1); 1060 return (blk->pte_start); 1061 } 1062 1063 1064 static uint32_t 1065 emu_rate_to_pitch(uint32_t rate) 1066 { 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 1084 }; 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 1102 }; 1103 int i; 1104 1105 if (rate == 0) 1106 return (0); 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)]); 1114 } 1115 rate <<= 1; 1116 } 1117 /* NOTREACHED */ 1118 return (0); 1119 } 1120 1121 static uint32_t 1122 emu_rate_to_linearpitch(uint32_t rate) 1123 { 1124 rate = (rate << 8) / 375; 1125 return ((rate >> 1) + (rate & 1)); 1126 } 1127 1128 struct emu_voice * 1129 emu_valloc(struct emu_sc_info *sc) 1130 { 1131 struct emu_voice *v; 1132 int i; 1133 1134 v = NULL; 1135 mtx_lock(&sc->lock); 1136 for (i = 0; i < NUM_G && sc->voice[i].busy; i++); 1137 if (i < NUM_G) { 1138 v = &sc->voice[i]; 1139 v->busy = 1; 1140 } 1141 mtx_unlock(&sc->lock); 1142 return (v); 1143 } 1144 1145 void 1146 emu_vfree(struct emu_sc_info *sc, struct emu_voice *v) 1147 { 1148 int i, r; 1149 1150 mtx_lock(&sc->lock); 1151 for (i = 0; i < NUM_G; i++) { 1152 if (v == &sc->voice[i] && sc->voice[i].busy) { 1153 v->busy = 0; 1154 /* XXX What we should do with mono channels? 1155 See -pcm.c emupchan_init for other side of 1156 this problem */ 1157 if (v->slave != NULL) 1158 r = emu_memfree(&sc->mem, v->vbuf); 1159 } 1160 } 1161 mtx_unlock(&sc->lock); 1162 } 1163 1164 int 1165 emu_vinit(struct emu_sc_info *sc, struct emu_voice *m, struct emu_voice *s, 1166 uint32_t sz, struct snd_dbuf *b) 1167 { 1168 void *vbuf; 1169 bus_addr_t tmp_addr; 1170 1171 vbuf = emu_memalloc(&sc->mem, sz, &tmp_addr, "vinit"); 1172 if (vbuf == NULL) 1173 return (ENOMEM); 1174 if (b != NULL) 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); 1179 return (ENOMEM); 1180 } 1181 m->end = m->start + sz; 1182 m->speed = 0; 1183 m->b16 = 0; 1184 m->stereo = 0; 1185 m->running = 0; 1186 m->ismaster = 1; 1187 m->vol = 0xff; 1188 m->buf = tmp_addr; 1189 m->vbuf = vbuf; 1190 m->slave = s; 1191 if (s != NULL) { 1192 s->start = m->start; 1193 s->end = m->end; 1194 s->speed = 0; 1195 s->b16 = 0; 1196 s->stereo = 0; 1197 s->running = 0; 1198 s->ismaster = 0; 1199 s->vol = m->vol; 1200 s->buf = m->buf; 1201 s->vbuf = NULL; 1202 s->slave = NULL; 1203 } 1204 return (0); 1205 } 1206 1207 void 1208 emu_vsetup(struct emu_voice *v, int fmt, int spd) 1209 { 1210 if (fmt) { 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; 1216 } 1217 } 1218 if (spd) { 1219 v->speed = spd; 1220 if (v->slave != NULL) 1221 v->slave->speed = v->speed; 1222 } 1223 } 1224 1225 void 1226 emu_vroute(struct emu_sc_info *sc, struct emu_route *rt, struct emu_voice *v) 1227 { 1228 unsigned int routing[8], amounts[8]; 1229 int i; 1230 1231 for (i = 0; i < 8; i++) { 1232 routing[i] = rt->routing_left[i]; 1233 amounts[i] = rt->amounts_left[i]; 1234 } 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]; 1239 } 1240 1241 if (sc->is_emu10k1) { 1242 emu_wrptr(sc, v->vnum, FXRT, ((routing[3] << 12) | 1243 (routing[2] << 8) | 1244 (routing[1] << 4) | 1245 (routing[0] << 0)) << 16); 1246 } else { 1247 emu_wrptr(sc, v->vnum, A_FXRT1, (routing[3] << 24) | 1248 (routing[2] << 16) | 1249 (routing[1] << 8) | 1250 (routing[0] << 0)); 1251 emu_wrptr(sc, v->vnum, A_FXRT2, (routing[7] << 24) | 1252 (routing[6] << 16) | 1253 (routing[5] << 8) | 1254 (routing[4] << 0)); 1255 emu_wrptr(sc, v->vnum, A_SENDAMOUNTS, (amounts[7] << 24) | 1256 (amounts[6] << 26) | 1257 (amounts[5] << 8) | 1258 (amounts[4] << 0)); 1259 } 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); 1265 } 1266 1267 void 1268 emu_vwrite(struct emu_sc_info *sc, struct emu_voice *v) 1269 { 1270 int s; 1271 uint32_t am_2, am_3, start, val, silent_page; 1272 1273 s = (v->stereo ? 1 : 0) + (v->b16 ? 1 : 0); 1274 1275 v->sa = v->start >> s; 1276 v->ea = v->end >> s; 1277 1278 1279 if (v->stereo) { 1280 emu_wrptr(sc, v->vnum, CPF, CPF_STEREO_MASK); 1281 } else { 1282 emu_wrptr(sc, v->vnum, CPF, 0); 1283 } 1284 val = v->stereo ? 28 : 30; 1285 val *= v->b16 ? 1 : 2; 1286 start = v->sa + val; 1287 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); 1292 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); 1296 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); 1300 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); 1311 1312 emu_wrptr(sc, v->vnum, ATKHLDV, ATKHLDV_HOLDTIME_MASK | ATKHLDV_ATTACKTIME_MASK); 1313 emu_wrptr(sc, v->vnum, ENVVOL, 0x8000); 1314 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); 1319 } 1320 1321 static void 1322 emu_vstop(struct emu_sc_info *sc, char channel, int enable) 1323 { 1324 int reg; 1325 1326 reg = (channel & 0x20) ? SOLEH : SOLEL; 1327 channel &= 0x1f; 1328 reg |= 1 << 24; 1329 reg |= channel << 16; 1330 emu_wrptr(sc, 0, reg, enable); 1331 } 1332 1333 void 1334 emu_vtrigger(struct emu_sc_info *sc, struct emu_voice *v, int go) 1335 { 1336 uint32_t pitch_target, initial_pitch; 1337 uint32_t cra, cs, ccis; 1338 uint32_t sample, i; 1339 1340 if (go) { 1341 cra = 64; 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); 1351 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); 1357 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); 1363 } else { 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); 1371 } 1372 if ((v->stereo) && (v->slave != NULL)) 1373 emu_vtrigger(sc, v->slave, go); 1374 } 1375 1376 int 1377 emu_vpos(struct emu_sc_info *sc, struct emu_voice *v) 1378 { 1379 int s, ptr; 1380 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); 1384 } 1385 1386 1387 /* fx */ 1388 static void 1389 emu_wrefx(struct emu_sc_info *sc, unsigned int pc, unsigned int data) 1390 { 1391 emu_wrptr(sc, 0, sc->code_base + pc, data); 1392 } 1393 1394 1395 static void 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) 1397 { 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)); 1400 return; 1401 } 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); 1404 (*pc)++; 1405 } 1406 1407 static int 1408 sysctl_emu_mixer_control(SYSCTL_HANDLER_ARGS) 1409 { 1410 struct emu_sc_info *sc; 1411 int mixer_id; 1412 int new_vol; 1413 int err; 1414 1415 sc = arg1; 1416 mixer_id = arg2; 1417 1418 new_vol = emumix_get_volume(sc, mixer_id); 1419 err = sysctl_handle_int(oidp, &new_vol, sizeof(new_vol), req); 1420 1421 if (err || req->newptr == NULL) 1422 return (err); 1423 if (new_vol < 0 || new_vol > 100) 1424 return (EINVAL); 1425 emumix_set_volume(sc, mixer_id, new_vol); 1426 1427 return (0); 1428 } 1429 1430 static int 1431 emu_addefxmixer(struct emu_sc_info *sc, const char *mix_name, const int mix_id, uint32_t defvolume) 1432 { 1433 int volgpr; 1434 char sysctl_name[32]; 1435 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",""); 1450 } 1451 1452 return (volgpr); 1453 } 1454 1455 /* allocate cache GPRs that will hold mixed output channels 1456 * and clear it on every DSP run. 1457 */ 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]), \ 1462 DSP_CONST(0), \ 1463 DSP_CONST(0), \ 1464 DSP_CONST(0), \ 1465 &pc); \ 1466 } while (0) 1467 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]), \ 1475 INP_NR, \ 1476 GPR(sc->mixer_gpr[IN_GPR_IDX]), \ 1477 &pc); \ 1478 } while (0) 1479 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, \ 1485 OUTP(OUTP_NR), \ 1486 DSP_CONST(0), \ 1487 GPR(sc->cache_gpr[OUT_CACHE_IDX]), \ 1488 GPR(sc->mixer_gpr[OUT_GPR_IDX]), \ 1489 &pc); \ 1490 } while (0) 1491 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, \ 1495 OUTP(OUTP_NR), \ 1496 DSP_CONST(0), \ 1497 GPR(sc->cache_gpr[OUT_CACHE_IDX]), \ 1498 GPR(sc->mixer_gpr[OUT_GPR_IDX]), \ 1499 &pc); \ 1500 } while(0) 1501 1502 static void 1503 emu_initefx(struct emu_sc_info *sc) 1504 { 1505 unsigned int c; 1506 uint32_t pc; 1507 1508 /* stop DSP */ 1509 if (sc->is_emu10k1) { 1510 emu_wrptr(sc, 0, DBG, EMU10K1_DBG_SINGLE_STEP); 1511 } else { 1512 emu_wrptr(sc, 0, A_DBG, A_DBG_SINGLE_STEP); 1513 } 1514 1515 /* code size is in instructions */ 1516 pc = 0; 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); 1520 } else { 1521 emu_addefxop(sc, SKIP, DSP_CONST(0x0), DSP_CONST(0x0), DSP_CONST(0xf), DSP_CONST(0x0), &pc); 1522 } 1523 } 1524 1525 pc = 0; 1526 1527 /* 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 ! 1533 */ 1534 1535 /* clean outputs */ 1536 for (c = 0; c < 16 ; c++) { 1537 emu_addefxop(sc, ACC3, OUTP(c), DSP_CONST(0), DSP_CONST(0), DSP_CONST(0), &pc); 1538 } 1539 1540 1541 if (sc->is_emu10k1) { 1542 EFX_CACHE(C_FRONT_L); 1543 EFX_CACHE(C_FRONT_R); 1544 EFX_CACHE(C_REC_L); 1545 EFX_CACHE(C_REC_R); 1546 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); 1552 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); 1558 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); 1564 #if 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); 1570 #endif 1571 #if 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); 1577 #endif 1578 /* in4, LineIn */ 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); 1583 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); 1589 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); 1595 #if 0 1596 /* in7, unknown */ 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); 1601 #endif 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); 1605 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 1610 /* 1611 * Additional channel volume is controlled by mixer in 1612 * emu_dspmixer_set() in -pcm.c 1613 */ 1614 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); 1620 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); 1626 #endif 1627 } else /* emu10k2 and later */ { 1628 EFX_CACHE(C_FRONT_L); 1629 EFX_CACHE(C_FRONT_R); 1630 EFX_CACHE(C_REC_L); 1631 EFX_CACHE(C_REC_R); 1632 1633 /* fx0 to front/record, 100%/muted by default */ 1634 /* 1635 * FRONT_[L|R] is controlled by AC97 emulation in 1636 * emu_ac97_[read|write]_emulation in -pcm.c 1637 */ 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); 1642 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); 1648 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); 1654 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); 1660 #if 0 1661 /* in3, unknown */ 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); 1666 #endif 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); 1672 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); 1678 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); 1684 #if 0 1685 /* in7, unknown */ 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); 1690 #endif 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); 1699 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 1705 /* 1706 * Additional channel volume is controlled by mixer in 1707 * emu_dspmixer_set() in -pcm.c 1708 */ 1709 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); 1715 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); 1720 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); 1725 #if 0 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); 1729 #endif 1730 /* fx5 (pcm3) to sub */ 1731 EFX_CACHE(C_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); 1734 #if 0 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); 1738 #endif 1739 if (sc->has_71) { 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); 1750 } 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); 1754 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); 1757 #endif 1758 } 1759 1760 sc->routing_code_end = pc; 1761 1762 /* start DSP */ 1763 if (sc->is_emu10k1) { 1764 emu_wrptr(sc, 0, DBG, 0); 1765 } else { 1766 emu_wrptr(sc, 0, A_DBG, 0); 1767 } 1768 } 1769 1770 /* /dev/em10kx */ 1771 static d_open_t emu10kx_open; 1772 static d_close_t emu10kx_close; 1773 static d_read_t emu10kx_read; 1774 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, 1781 }; 1782 1783 1784 static int 1785 emu10kx_open(struct cdev *i_dev, int flags __unused, int mode __unused, struct thread *td __unused) 1786 { 1787 int error; 1788 struct emu_sc_info *sc; 1789 1790 sc = i_dev->si_drv1; 1791 mtx_lock(&sc->emu10kx_lock); 1792 if (sc->emu10kx_isopen) { 1793 mtx_unlock(&sc->emu10kx_lock); 1794 return (EBUSY); 1795 } 1796 sc->emu10kx_isopen = 1; 1797 mtx_unlock(&sc->emu10kx_lock); 1798 if (sbuf_new(&sc->emu10kx_sbuf, NULL, 4096, 0) == NULL) { 1799 error = ENXIO; 1800 goto out; 1801 } 1802 sc->emu10kx_bufptr = 0; 1803 error = (emu10kx_prepare(sc, &sc->emu10kx_sbuf) > 0) ? 0 : ENOMEM; 1804 out: 1805 if (error) { 1806 mtx_lock(&sc->emu10kx_lock); 1807 sc->emu10kx_isopen = 0; 1808 mtx_unlock(&sc->emu10kx_lock); 1809 } 1810 return (error); 1811 } 1812 1813 static int 1814 emu10kx_close(struct cdev *i_dev, int flags __unused, int mode __unused, struct thread *td __unused) 1815 { 1816 struct emu_sc_info *sc; 1817 1818 sc = i_dev->si_drv1; 1819 1820 mtx_lock(&sc->emu10kx_lock); 1821 if (!(sc->emu10kx_isopen)) { 1822 mtx_unlock(&sc->emu10kx_lock); 1823 return (EBADF); 1824 } 1825 sbuf_delete(&sc->emu10kx_sbuf); 1826 sc->emu10kx_isopen = 0; 1827 mtx_unlock(&sc->emu10kx_lock); 1828 1829 return (0); 1830 } 1831 1832 static int 1833 emu10kx_read(struct cdev *i_dev, struct uio *buf, int flag __unused) 1834 { 1835 int l, err; 1836 struct emu_sc_info *sc; 1837 1838 sc = i_dev->si_drv1; 1839 mtx_lock(&sc->emu10kx_lock); 1840 if (!(sc->emu10kx_isopen)) { 1841 mtx_unlock(&sc->emu10kx_lock); 1842 return (EBADF); 1843 } 1844 mtx_unlock(&sc->emu10kx_lock); 1845 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; 1849 1850 return (err); 1851 } 1852 1853 static int 1854 emu10kx_prepare(struct emu_sc_info *sc, struct sbuf *s) 1855 { 1856 int i; 1857 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"); 1863 if (sc->has_ac97) { 1864 sbuf_printf(s, " AC97 codec"); 1865 } else { 1866 sbuf_printf(s, " NO AC97 codec"); 1867 } 1868 if (sc->has_51) { 1869 if (sc->has_71) 1870 sbuf_printf(s, " and 7.1 output"); 1871 else 1872 sbuf_printf(s, " and 5.1 output"); 1873 } 1874 if (sc->is_emu10k1) 1875 sbuf_printf(s, ", SBLive! DSP code"); 1876 if (sc->is_emu10k2) 1877 sbuf_printf(s, ", Audigy DSP code"); 1878 if (sc->is_ca0102) 1879 sbuf_printf(s, ", Audigy DSP code with Audigy2 hacks"); 1880 if (sc->is_ca0108) 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])); 1890 } 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])); 1895 } 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])); 1899 } 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"); 1903 } 1904 sbuf_finish(s); 1905 return (sbuf_len(s)); 1906 } 1907 1908 /* INIT & UNINIT */ 1909 static int 1910 emu10kx_dev_init(struct emu_sc_info *sc) 1911 { 1912 int unit; 1913 1914 mtx_init(&sc->emu10kx_lock, "kxdevlock", NULL, 0); 1915 unit = device_get_unit(sc->dev); 1916 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; 1920 return (0); 1921 } 1922 return (ENXIO); 1923 } 1924 1925 static int 1926 emu10kx_dev_uninit(struct emu_sc_info *sc) 1927 { 1928 intrmask_t s; 1929 1930 s = spltty(); 1931 mtx_lock(&sc->emu10kx_lock); 1932 if (sc->emu10kx_isopen) { 1933 mtx_unlock(&sc->emu10kx_lock); 1934 splx(s); 1935 return (EBUSY); 1936 } 1937 if (sc->cdev) 1938 destroy_dev(sc->cdev); 1939 sc->cdev = 0; 1940 1941 splx(s); 1942 mtx_destroy(&sc->emu10kx_lock); 1943 return (0); 1944 } 1945 1946 /* resource manager */ 1947 int 1948 emu_rm_init(struct emu_sc_info *sc) 1949 { 1950 int i; 1951 int maxcount; 1952 struct emu_rm *rm; 1953 1954 rm = malloc(sizeof(struct emu_rm), M_DEVBUF, M_NOWAIT | M_ZERO); 1955 if (rm == NULL) { 1956 return (ENOMEM); 1957 } 1958 sc->rm = rm; 1959 rm->card = sc; 1960 maxcount = sc->num_gprs; 1961 rm->num_used = 0; 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; 1967 1968 return (0); 1969 } 1970 1971 int 1972 emu_rm_uninit(struct emu_sc_info *sc) 1973 { 1974 #ifdef SND_EMU10KX_DEBUG 1975 int i; 1976 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)); 1982 #endif 1983 mtx_destroy(&(sc->rm->gpr_lock)); 1984 free(sc->rm, M_DEVBUF); 1985 return (0); 1986 } 1987 1988 static int 1989 emu_rm_gpr_alloc(struct emu_rm *rm, int count) 1990 { 1991 int i, j; 1992 int allocated_gpr; 1993 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); 2003 } else { 2004 /* longer */ 2005 i = 0; 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]; 2010 } else { 2011 allocated_gpr = i; 2012 for (j = 1; j < count; j++) { 2013 if (rm->allocmap[i + j] != 0) 2014 allocated_gpr = rm->num_gprs; 2015 } 2016 if (allocated_gpr == i) 2017 break; 2018 } 2019 } 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); 2024 2025 } 2026 } 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); 2033 } 2034 2035 /* mixer */ 2036 void 2037 emumix_set_mode(struct emu_sc_info *sc, int mode) 2038 { 2039 uint32_t a_iocfg; 2040 uint32_t hcfg; 2041 uint32_t tmp; 2042 2043 switch (mode) { 2044 case MODE_DIGITAL: 2045 /* FALLTHROUGH */ 2046 case MODE_ANALOG: 2047 break; 2048 default: 2049 return; 2050 } 2051 2052 hcfg = HCFG_AUDIOENABLE | HCFG_AUTOMUTE; 2053 a_iocfg = 0; 2054 2055 if (sc->rev >= 6) 2056 hcfg |= HCFG_JOYENABLE; 2057 2058 if (sc->is_emu10k1) 2059 hcfg |= HCFG_LOCKTANKCACHE_MASK; 2060 else 2061 hcfg |= HCFG_CODECFORMAT_I2S | HCFG_JOYENABLE; 2062 2063 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"); 2067 } 2068 a_iocfg |= A_IOCFG_ENABLE_DIGITAL; 2069 hcfg |= HCFG_GPOUT0; 2070 } 2071 2072 if (mode == MODE_ANALOG) 2073 emumix_set_spdif_mode(sc, SPDIF_MODE_PCM); 2074 2075 if (sc->is_emu10k2) 2076 a_iocfg |= 0x80; /* XXX */ 2077 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. */ 2081 2082 if (sc->is_ca0108) 2083 a_iocfg |= 0x20; /* XXX */ 2084 2085 emu_wr(sc, HCFG, hcfg, 4); 2086 2087 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) { 2088 tmp = emu_rd(sc, A_IOCFG, 2); 2089 tmp = a_iocfg; 2090 emu_wr(sc, A_IOCFG, tmp, 2); 2091 } 2092 2093 /* 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. 2097 */ 2098 } 2099 2100 void 2101 emumix_set_spdif_mode(struct emu_sc_info *sc, int mode) 2102 { 2103 uint32_t spcs; 2104 2105 switch (mode) { 2106 case SPDIF_MODE_PCM: 2107 break; 2108 case SPDIF_MODE_AC3: 2109 device_printf(sc->dev, "AC3 mode does not work and disabled\n"); 2110 return; 2111 default: 2112 return; 2113 } 2114 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; 2119 2120 mode = SPDIF_MODE_PCM; 2121 2122 emu_wrptr(sc, 0, SPCS0, spcs); 2123 emu_wrptr(sc, 0, SPCS1, spcs); 2124 emu_wrptr(sc, 0, SPCS2, spcs); 2125 } 2126 2127 #define L2L_POINTS 10 2128 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 */ 2140 }; 2141 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 */ 2152 0x00000000 /* 0 */ 2153 }; 2154 2155 2156 static int 2157 log2lin(int log_t) 2158 { 2159 int lin_t; 2160 int idx, lin; 2161 2162 if (log_t <= 0) { 2163 lin_t = 0x00000000; 2164 return (lin_t); 2165 } 2166 2167 if (log_t >= 100) { 2168 lin_t = 0x7fffffff; 2169 return (lin_t); 2170 } 2171 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]; 2175 return (lin_t); 2176 } 2177 2178 2179 void 2180 emumix_set_fxvol(struct emu_sc_info *sc, unsigned gpr, int32_t vol) 2181 { 2182 2183 vol = log2lin(vol); 2184 emumix_set_gpr(sc, gpr, vol); 2185 } 2186 2187 void 2188 emumix_set_gpr(struct emu_sc_info *sc, unsigned gpr, int32_t val) 2189 { 2190 2191 emu_wrptr(sc, 0, GPR(gpr), val); 2192 } 2193 2194 void 2195 emumix_set_volume(struct emu_sc_info *sc, int mixer_idx, int volume) 2196 { 2197 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); 2202 } 2203 } 2204 2205 int 2206 emumix_get_volume(struct emu_sc_info *sc, int mixer_idx) 2207 { 2208 if ((mixer_idx < NUM_MIXERS) && (mixer_idx >= 0)) 2209 return (sc->mixer_volcache[mixer_idx]); 2210 return (-1); 2211 } 2212 2213 /* Init CardBus part */ 2214 static int 2215 emu_cardbus_init(struct emu_sc_info *sc) 2216 { 2217 2218 /* 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". 2222 */ 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); 2227 2228 emu_wr_cbptr(sc, (0x0090 << 16) | 0x007f); 2229 2230 return (0); 2231 } 2232 2233 /* Probe and attach the card */ 2234 static int 2235 emu_init(struct emu_sc_info *sc) 2236 { 2237 uint32_t ch, tmp; 2238 uint32_t spdif_sr; 2239 uint32_t ac97slot; 2240 int def_mode; 2241 int i; 2242 2243 /* disable audio and lock cache */ 2244 emu_wr(sc, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, 4); 2245 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); 2253 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); 2260 2261 /* disable P16V and S/PDIF interrupts */ 2262 if ((sc->is_ca0102) || (sc->is_ca0108)) 2263 emu_wr(sc, INTE2, 0, 4); 2264 2265 if (sc->is_ca0102) 2266 emu_wr(sc, INTE3, 0, 4); 2267 2268 /* init phys inputs and outputs */ 2269 ac97slot = 0; 2270 if (sc->has_51) 2271 ac97slot = AC97SLOT_CNTR | AC97SLOT_LFE; 2272 if (sc->has_71) 2273 ac97slot = AC97SLOT_CNTR | AC97SLOT_LFE | AC97SLOT_REAR_LEFT | AC97SLOT_REAR_RIGHT; 2274 if (sc->is_emu10k2) 2275 ac97slot |= 0x40; 2276 emu_wrptr(sc, 0, AC97SLOT, ac97slot); 2277 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? */ 2281 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); 2291 return (ENOMEM); 2292 } 2293 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) 2297 return (ENOMEM); 2298 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); 2302 return (ENOMEM); 2303 } 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; 2309 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); 2313 } 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 */ 2317 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); 2327 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); 2334 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); 2343 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); 2350 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); 2359 } 2360 } 2361 2362 emumix_set_spdif_mode(sc, SPDIF_MODE_PCM); 2363 2364 if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) 2365 emu_wrptr(sc, 0, A_SPDIF_SAMPLERATE, A_SPDIF_48000); 2366 2367 /* 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 2372 */ 2373 if ((sc->is_ca0102) || (sc->is_ca0108)) { 2374 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); 2379 2380 /* Disable P16v processing */ 2381 emu_wr_p16vptr(sc, 0, SRCSel, 0x14); 2382 2383 /* Setup P16v/P17v sound routing */ 2384 if (sc->is_ca0102) 2385 emu_wr_p16vptr(sc, 0, SRCMULTI_ENABLE, 0xFF00FF00); 2386 else { 2387 emu_wr_p16vptr(sc, 0, P17V_MIXER_I2S_ENABLE, 0xFF000000); 2388 emu_wr_p16vptr(sc, 0, P17V_MIXER_SPDIF_ENABLE, 0xFF000000); 2389 2390 tmp = emu_rd(sc, A_IOCFG, 2); 2391 emu_wr(sc, A_IOCFG, tmp & ~0x8, 2); 2392 } 2393 } 2394 emu_initefx(sc); 2395 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; 2402 } 2403 emumix_set_mode(sc, def_mode); 2404 2405 if (bootverbose) { 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]" : " ")); 2444 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* */ 2469 } /* bootverbose */ 2470 return (0); 2471 } 2472 2473 static int 2474 emu_uninit(struct emu_sc_info *sc) 2475 { 2476 uint32_t ch; 2477 struct emu_memblk *blk; 2478 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); 2487 } 2488 2489 /* disable audio and lock cache */ 2490 emu_wr(sc, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, 4); 2491 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); 2503 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); 2509 2510 if (sc->mem.dmat) 2511 bus_dma_tag_destroy(sc->mem.dmat); 2512 2513 if (!SLIST_EMPTY(&sc->mem.blocks)) 2514 device_printf(sc->dev, "warning: memblock list not empty\n"); 2515 2516 SLIST_FOREACH(blk, &sc->mem.blocks, link) 2517 if (blk != NULL) 2518 device_printf(sc->dev, "lost %d for %s\n", blk->pte_size, blk->owner); 2519 2520 emu_free(&sc->mem, sc->mem.ptb_pages); 2521 emu_free(&sc->mem, sc->mem.silent_page); 2522 2523 return (0); 2524 } 2525 2526 static int 2527 emu_read_ivar(device_t bus, device_t dev, int ivar_index, uintptr_t * result) 2528 { 2529 struct sndcard_func *func = device_get_ivars(dev); 2530 struct emu_sc_info *sc = device_get_softc(bus); 2531 2532 switch (ivar_index) { 2533 case EMU_VAR_FUNC: 2534 *result = func->func; 2535 break; 2536 case EMU_VAR_ROUTE: 2537 *result = ((struct emu_pcminfo *)func->varinfo)->route; 2538 break; 2539 case EMU_VAR_ISEMU10K1: 2540 *result = sc->is_emu10k1; 2541 break; 2542 default: 2543 return (ENOENT); 2544 } 2545 2546 return (0); 2547 } 2548 2549 static int 2550 emu_write_ivar(device_t bus __unused, device_t dev __unused, 2551 int ivar_index, uintptr_t value __unused) 2552 { 2553 2554 switch (ivar_index) { 2555 case 0: 2556 return (EINVAL); 2557 2558 default: 2559 return (ENOENT); 2560 } 2561 } 2562 2563 static int 2564 emu_pci_probe(device_t dev) 2565 { 2566 struct sbuf *s; 2567 int thiscard = 0; 2568 uint16_t vendor; 2569 2570 vendor = pci_read_config(dev, PCIR_DEVVENDOR, /* bytes */ 2); 2571 if (vendor != 0x1102) 2572 return (ENXIO); /* Not Creative */ 2573 2574 thiscard = emu_getcard(dev); 2575 if (thiscard < 0) 2576 return (ENXIO); 2577 2578 s = sbuf_new(NULL, NULL, 4096, 0); 2579 if (s == NULL) 2580 return (ENOMEM); 2581 sbuf_printf(s, "Creative %s [%s]", emu_cards[thiscard].desc, emu_cards[thiscard].SBcode); 2582 sbuf_finish(s); 2583 2584 device_set_desc_copy(dev, sbuf_data(s)); 2585 return (BUS_PROBE_DEFAULT); 2586 } 2587 2588 2589 static int 2590 emu_pci_attach(device_t dev) 2591 { 2592 struct sndcard_func *func; 2593 struct emu_sc_info *sc; 2594 struct emu_pcminfo *pcminfo; 2595 struct emu_midiinfo *midiinfo[3]; 2596 uint32_t data; 2597 int i; 2598 int device_flags; 2599 char status[255]; 2600 int error = ENXIO; 2601 2602 sc = device_get_softc(dev); 2603 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); 2607 sc->dev = dev; 2608 sc->type = pci_get_devid(dev); 2609 sc->rev = pci_get_revid(dev); 2610 sc->enable_ir = 0; 2611 sc->enable_debug = 0; 2612 sc->has_ac97 = 0; 2613 sc->has_51 = 0; 2614 sc->has_71 = 0; 2615 sc->broken_digital = 0; 2616 sc->is_emu10k1 = 0; 2617 sc->is_emu10k2 = 0; 2618 sc->is_ca0102 = 0; 2619 sc->is_ca0108 = 0; 2620 sc->is_cardbus = 0; 2621 2622 device_flags = emu_cards[emu_getcard(dev)].flags; 2623 if (device_flags & HAS_51) 2624 sc->has_51 = 1; 2625 if (device_flags & HAS_71) { 2626 sc->has_51 = 1; 2627 sc->has_71 = 1; 2628 } 2629 if (device_flags & IS_EMU10K1) 2630 sc->is_emu10k1 = 1; 2631 if (device_flags & IS_EMU10K2) 2632 sc->is_emu10k2 = 1; 2633 if (device_flags & IS_CA0102) 2634 sc->is_ca0102 = 1; 2635 if (device_flags & IS_CA0108) 2636 sc->is_ca0108 = 1; 2637 if ((sc->is_emu10k2) && (sc->rev == 4)) { 2638 sc->is_emu10k2 = 0; 2639 sc->is_ca0102 = 1; /* for unknown Audigy 2 cards */ 2640 } 2641 if ((sc->is_ca0102 == 1) || (sc->is_ca0108 == 1)) 2642 if (device_flags & IS_CARDBUS) 2643 sc->is_cardbus = 1; 2644 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"); 2647 goto bad; 2648 } 2649 if (device_flags & BROKEN_DIGITAL) 2650 sc->broken_digital = 1; 2651 if (device_flags & HAS_AC97) 2652 sc->has_ac97 = 1; 2653 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; 2672 sc->num_inputs = 8; 2673 sc->num_outputs = 16; 2674 sc->address_mask = A_PTR_ADDRESS_MASK; 2675 } 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; 2691 sc->num_inputs = 8; 2692 sc->num_outputs = 16; 2693 sc->address_mask = PTR_ADDRESS_MASK; 2694 } 2695 if (sc->opcode_shift == 0) 2696 goto bad; 2697 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); 2702 2703 pci_enable_busmaster(dev); 2704 2705 i = PCIR_BAR(0); 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"); 2709 goto bad; 2710 } 2711 sc->st = rman_get_bustag(sc->reg); 2712 sc->sh = rman_get_bushandle(sc->reg); 2713 2714 for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++) 2715 sc->timer[i] = 0; /* disable it */ 2716 2717 i = 0; 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"); 2721 goto bad; 2722 } 2723 if (emu_rm_init(sc) != 0) { 2724 device_printf(dev, "unable to create resource manager\n"); 2725 goto bad; 2726 } 2727 if (sc->is_cardbus) 2728 if (emu_cardbus_init(sc) != 0) { 2729 device_printf(dev, "unable to initialize CardBus interface\n"); 2730 goto bad; 2731 } 2732 sc->ctx = device_get_sysctl_ctx(dev); 2733 if (sc->ctx == NULL) 2734 goto bad; 2735 sc->root = device_get_sysctl_tree(dev); 2736 if (sc->root == NULL) 2737 goto bad; 2738 if (emu_init(sc) == -1) { 2739 device_printf(dev, "unable to initialize the card\n"); 2740 goto bad; 2741 } 2742 if (emu10kx_dev_init(sc) == ENXIO) { 2743 device_printf(dev, "unable to create control device\n"); 2744 goto bad; 2745 } 2746 snprintf(status, 255, "rev %d at io 0x%lx irq %ld", sc->rev, rman_get_start(sc->reg), rman_get_start(sc->irq)); 2747 2748 /* Voices */ 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; 2760 } 2761 2762 /* PCM Audio */ 2763 /* FRONT */ 2764 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO); 2765 if (func == NULL) { 2766 error = ENOMEM; 2767 goto bad; 2768 } 2769 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO); 2770 if (pcminfo == NULL) { 2771 error = ENOMEM; 2772 goto bad; 2773 } 2774 pcminfo->card = sc; 2775 pcminfo->route = RT_FRONT; 2776 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); 2781 /* REAR */ 2782 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO); 2783 if (func == NULL) { 2784 error = ENOMEM; 2785 goto bad; 2786 } 2787 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO); 2788 if (pcminfo == NULL) { 2789 error = ENOMEM; 2790 goto bad; 2791 } 2792 #ifdef SND_EMU10KX_MULTICHANNEL 2793 pcminfo->card = sc; 2794 pcminfo->route = RT_REAR; 2795 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); 2800 if (sc->has_51) { 2801 /* CENTER */ 2802 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO); 2803 if (func == NULL) { 2804 error = ENOMEM; 2805 goto bad; 2806 } 2807 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO); 2808 if (pcminfo == NULL) { 2809 error = ENOMEM; 2810 goto bad; 2811 } 2812 pcminfo->card = sc; 2813 pcminfo->route = RT_CENTER; 2814 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); 2819 /* SUB */ 2820 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO); 2821 if (func == NULL) { 2822 error = ENOMEM; 2823 goto bad; 2824 } 2825 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO); 2826 if (pcminfo == NULL) { 2827 error = ENOMEM; 2828 goto bad; 2829 } 2830 pcminfo->card = sc; 2831 pcminfo->route = RT_SUB; 2832 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); 2837 } 2838 if (sc->has_71) { 2839 /* SIDE */ 2840 func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT | M_ZERO); 2841 if (func == NULL) { 2842 error = ENOMEM; 2843 goto bad; 2844 } 2845 pcminfo = malloc(sizeof(struct emu_pcminfo), M_DEVBUF, M_NOWAIT | M_ZERO); 2846 if (pcminfo == NULL) { 2847 error = ENOMEM; 2848 goto bad; 2849 } 2850 pcminfo->card = sc; 2851 pcminfo->route = RT_SIDE; 2852 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); 2857 }; 2858 #endif /* SND_EMU10KX_MULTICHANNEL */ 2859 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); 2863 if (func == NULL) { 2864 error = ENOMEM; 2865 goto bad; 2866 } 2867 midiinfo[0] = malloc(sizeof(struct emu_midiinfo), M_DEVBUF, M_NOWAIT | M_ZERO); 2868 if (midiinfo[0] == NULL) { 2869 error = ENOMEM; 2870 goto bad; 2871 } 2872 midiinfo[0]->card = sc; 2873 if (sc->is_emu10k2 || (sc->is_ca0102)) { 2874 midiinfo[0]->port = A_MUDATA1; 2875 midiinfo[0]->portnr = 1; 2876 } 2877 if (sc->is_emu10k1) { 2878 midiinfo[0]->port = MUDATA; 2879 midiinfo[0]->portnr = 1; 2880 } 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); 2885 } 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); 2889 if (func == NULL) { 2890 error = ENOMEM; 2891 goto bad; 2892 } 2893 midiinfo[1] = malloc(sizeof(struct emu_midiinfo), M_DEVBUF, M_NOWAIT | M_ZERO); 2894 if (midiinfo[1] == NULL) { 2895 error = ENOMEM; 2896 goto bad; 2897 } 2898 midiinfo[1]->card = sc; 2899 2900 midiinfo[1]->port = A_MUDATA2; 2901 midiinfo[1]->portnr = 2; 2902 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); 2907 } 2908 2909 return (bus_generic_attach(dev)); 2910 2911 bad: 2912 /* XXX can we just call emu_pci_detach here? */ 2913 if (sc->cdev) 2914 emu10kx_dev_uninit(sc); 2915 if (sc->rm != NULL) 2916 emu_rm_uninit(sc); 2917 if (sc->reg) 2918 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->reg); 2919 if (sc->ih) 2920 bus_teardown_intr(dev, sc->irq, sc->ih); 2921 if (sc->irq) 2922 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq); 2923 mtx_destroy(&sc->lock); 2924 mtx_destroy(&sc->rw); 2925 return (error); 2926 } 2927 2928 static int 2929 emu_pci_detach(device_t dev) 2930 { 2931 struct emu_sc_info *sc; 2932 int devcount, i; 2933 device_t *childlist; 2934 int r = 0; 2935 2936 sc = device_get_softc(dev); 2937 2938 for (i = 0; i < 5; i++) { 2939 if (sc->pcm[i] != NULL) 2940 r = device_delete_child(dev, sc->pcm[i]); 2941 if (r) 2942 return (r); 2943 } 2944 if (sc->midi[0] != NULL) 2945 r = device_delete_child(dev, sc->midi[0]); 2946 if (r) 2947 return (r); 2948 if (sc->midi[1] != NULL) 2949 r = device_delete_child(dev, sc->midi[1]); 2950 if (r) 2951 return (r); 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]); 2956 } 2957 free(childlist, M_TEMP); 2958 2959 /* shutdown chip */ 2960 emu_uninit(sc); 2961 r = emu10kx_dev_uninit(sc); 2962 if (r) 2963 return (r); 2964 emu_rm_uninit(sc); 2965 if (sc->reg) 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)); 2972 } 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), 2979 /* Bus methods */ 2980 DEVMETHOD(bus_read_ivar, emu_read_ivar), 2981 DEVMETHOD(bus_write_ivar, emu_write_ivar), 2982 2983 {0, 0} 2984 }; 2985 2986 2987 static driver_t emu_driver = { 2988 "emu10kx", 2989 emu_methods, 2990 sizeof(struct emu_sc_info), 2991 NULL, 2992 0, 2993 NULL 2994 }; 2995 2996 static int 2997 emu_modevent(module_t mod __unused, int cmd, void *data __unused) 2998 { 2999 int err = 0; 3000 3001 switch (cmd) { 3002 case MOD_LOAD: 3003 break; /* Success */ 3004 3005 case MOD_UNLOAD: 3006 case MOD_SHUTDOWN: 3007 3008 /* XXX Should we check state of pcm & midi subdevices here? */ 3009 3010 break; /* Success */ 3011 3012 default: 3013 err = EINVAL; 3014 break; 3015 } 3016 3017 return (err); 3018 3019 } 3020 3021 static devclass_t emu_devclass; 3022 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); 3026