1 /*- 2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk> 3 * Copyright (c) 2003-2007 Yuriy Tsibizov <yuriy.tsibizov@gfk.ru> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 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/systm.h> 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 42 #ifdef HAVE_KERNEL_OPTION_HEADERS 43 #include "opt_snd.h" 44 #endif 45 46 #include <dev/sound/chip.h> 47 #include <dev/sound/pcm/sound.h> 48 #include <dev/sound/pcm/ac97.h> 49 50 #include "mixer_if.h" 51 52 #include <dev/sound/pci/emuxkireg.h> 53 #include <dev/sound/pci/emu10kx.h> 54 55 struct emu_pcm_pchinfo { 56 int spd; 57 int fmt; 58 unsigned int blksz; 59 int run; 60 struct emu_voice *master; 61 struct emu_voice *slave; 62 struct snd_dbuf *buffer; 63 struct pcm_channel *channel; 64 struct emu_pcm_info *pcm; 65 int timer; 66 }; 67 68 struct emu_pcm_rchinfo { 69 int spd; 70 int fmt; 71 unsigned int blksz; 72 int run; 73 uint32_t idxreg; 74 uint32_t basereg; 75 uint32_t sizereg; 76 uint32_t setupreg; 77 uint32_t irqmask; 78 uint32_t iprmask; 79 int ihandle; 80 struct snd_dbuf *buffer; 81 struct pcm_channel *channel; 82 struct emu_pcm_info *pcm; 83 int timer; 84 }; 85 86 /* XXX Hardware playback channels */ 87 #define MAX_CHANNELS 4 88 89 #if MAX_CHANNELS > 13 90 #error Too many hardware channels defined. 13 is the maximum 91 #endif 92 93 struct emu_pcm_info { 94 struct mtx *lock; 95 device_t dev; /* device information */ 96 struct emu_sc_info *card; 97 struct emu_pcm_pchinfo pch[MAX_CHANNELS]; /* hardware channels */ 98 int pnum; /* next free channel number */ 99 struct emu_pcm_rchinfo rch_adc; 100 struct emu_pcm_rchinfo rch_efx; 101 struct emu_route rt; 102 struct emu_route rt_mono; 103 int route; 104 int ihandle; /* interrupt handler */ 105 unsigned int bufsz; 106 int is_emu10k1; 107 struct ac97_info *codec; 108 uint32_t ac97_state[0x7F]; 109 kobj_class_t ac97_mixerclass; 110 uint32_t ac97_recdevs; 111 uint32_t ac97_playdevs; 112 struct snd_mixer *sm; 113 int mch_disabled; 114 unsigned int emu10k1_volcache[2][2]; 115 }; 116 117 118 static uint32_t emu_rfmt_adc[] = { 119 SND_FORMAT(AFMT_S16_LE, 1, 0), 120 SND_FORMAT(AFMT_S16_LE, 2, 0), 121 0 122 }; 123 static struct pcmchan_caps emu_reccaps_adc = { 124 8000, 48000, emu_rfmt_adc, 0 125 }; 126 127 static uint32_t emu_rfmt_efx[] = { 128 SND_FORMAT(AFMT_S16_LE, 1, 0), 129 0 130 }; 131 132 static struct pcmchan_caps emu_reccaps_efx_live = { 133 48000*32, 48000*32, emu_rfmt_efx, 0 134 }; 135 136 static struct pcmchan_caps emu_reccaps_efx_audigy = { 137 48000*64, 48000*64, emu_rfmt_efx, 0 138 }; 139 140 static int emu_rates_live[] = { 141 48000*32 142 }; 143 144 static int emu_rates_audigy[] = { 145 48000*64 146 }; 147 148 static uint32_t emu_pfmt[] = { 149 SND_FORMAT(AFMT_U8, 1, 0), 150 SND_FORMAT(AFMT_U8, 2, 0), 151 SND_FORMAT(AFMT_S16_LE, 1, 0), 152 SND_FORMAT(AFMT_S16_LE, 2, 0), 153 0 154 }; 155 static uint32_t emu_pfmt_mono[] = { 156 SND_FORMAT(AFMT_U8, 1, 0), 157 SND_FORMAT(AFMT_S16_LE, 1, 0), 158 0 159 }; 160 161 static struct pcmchan_caps emu_playcaps = {4000, 48000, emu_pfmt, 0}; 162 static struct pcmchan_caps emu_playcaps_mono = {4000, 48000, emu_pfmt_mono, 0}; 163 164 static int emu10k1_adcspeed[8] = {48000, 44100, 32000, 24000, 22050, 16000, 11025, 8000}; 165 /* audigy supports 12kHz. */ 166 static int emu10k2_adcspeed[9] = {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000}; 167 168 static uint32_t emu_pcm_intr(void *pcm, uint32_t stat); 169 170 static const struct emu_dspmix_props_k1 { 171 uint8_t present; 172 uint8_t recdev; 173 int8_t input; 174 } dspmix_k1 [SOUND_MIXER_NRDEVICES] = { 175 /* no mixer device for ac97 */ /* in0 AC97 */ 176 [SOUND_MIXER_DIGITAL1] = {1, 1, 1}, /* in1 CD SPDIF */ 177 /* not connected */ /* in2 (zoom) */ 178 [SOUND_MIXER_DIGITAL2] = {1, 1, 3}, /* in3 toslink */ 179 [SOUND_MIXER_LINE2] = {1, 1, 4}, /* in4 Line-In2 */ 180 [SOUND_MIXER_DIGITAL3] = {1, 1, 5}, /* in5 on-card SPDIF */ 181 [SOUND_MIXER_LINE3] = {1, 1, 6}, /* in6 AUX2 */ 182 /* not connected */ /* in7 */ 183 }; 184 static const struct emu_dspmix_props_k2 { 185 uint8_t present; 186 uint8_t recdev; 187 int8_t input; 188 } dspmix_k2 [SOUND_MIXER_NRDEVICES] = { 189 [SOUND_MIXER_VOLUME] = {1, 0, (-1)}, 190 [SOUND_MIXER_PCM] = {1, 0, (-1)}, 191 192 /* no mixer device */ /* in0 AC97 */ 193 [SOUND_MIXER_DIGITAL1] = {1, 1, 1}, /* in1 CD SPDIF */ 194 [SOUND_MIXER_DIGITAL2] = {1, 1, 2}, /* in2 COAX SPDIF */ 195 /* not connected */ /* in3 */ 196 [SOUND_MIXER_LINE2] = {1, 1, 4}, /* in4 Line-In2 */ 197 [SOUND_MIXER_DIGITAL3] = {1, 1, 5}, /* in5 on-card SPDIF */ 198 [SOUND_MIXER_LINE3] = {1, 1, 6}, /* in6 AUX2 */ 199 /* not connected */ /* in7 */ 200 }; 201 202 static int 203 emu_dspmixer_init(struct snd_mixer *m) 204 { 205 struct emu_pcm_info *sc; 206 int i; 207 int p, r; 208 209 p = 0; 210 r = 0; 211 212 sc = mix_getdevinfo(m); 213 214 if (sc->route == RT_FRONT) { 215 /* create submixer for AC97 codec */ 216 if ((sc->ac97_mixerclass != NULL) && (sc->codec != NULL)) { 217 sc->sm = mixer_create(sc->dev, sc->ac97_mixerclass, sc->codec, "ac97"); 218 if (sc->sm != NULL) { 219 p = mix_getdevs(sc->sm); 220 r = mix_getrecdevs(sc->sm); 221 } 222 } 223 224 sc->ac97_playdevs = p; 225 sc->ac97_recdevs = r; 226 } 227 228 /* This two are always here */ 229 p |= (1 << SOUND_MIXER_PCM); 230 p |= (1 << SOUND_MIXER_VOLUME); 231 232 if (sc->route == RT_FRONT) { 233 if (sc->is_emu10k1) { 234 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 235 if (dspmix_k1[i].present) 236 p |= (1 << i); 237 if (dspmix_k1[i].recdev) 238 r |= (1 << i); 239 } 240 } else { 241 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 242 if (dspmix_k2[i].present) 243 p |= (1 << i); 244 if (dspmix_k2[i].recdev) 245 r |= (1 << i); 246 } 247 } 248 } 249 250 mix_setdevs(m, p); 251 mix_setrecdevs(m, r); 252 253 return (0); 254 } 255 256 static int 257 emu_dspmixer_uninit(struct snd_mixer *m) 258 { 259 struct emu_pcm_info *sc; 260 int err = 0; 261 262 /* drop submixer for AC97 codec */ 263 sc = mix_getdevinfo(m); 264 if (sc->sm != NULL) 265 err = mixer_delete(sc->sm); 266 if (err) 267 return (err); 268 sc->sm = NULL; 269 return (0); 270 } 271 272 static int 273 emu_dspmixer_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right) 274 { 275 struct emu_pcm_info *sc; 276 277 sc = mix_getdevinfo(m); 278 279 switch (dev) { 280 case SOUND_MIXER_VOLUME: 281 switch (sc->route) { 282 case RT_FRONT: 283 if (sc->sm != NULL) 284 mix_set(sc->sm, dev, left, right); 285 if (sc->mch_disabled) { 286 /* In emu10k1 case PCM volume does not affect 287 sound routed to rear & center/sub (it is connected 288 to AC97 codec). Calculate it manually. */ 289 /* This really should belong to emu10kx.c */ 290 if (sc->is_emu10k1) { 291 sc->emu10k1_volcache[0][0] = left; 292 left = left * sc->emu10k1_volcache[1][0] / 100; 293 sc->emu10k1_volcache[0][1] = right; 294 right = right * sc->emu10k1_volcache[1][1] / 100; 295 } 296 297 emumix_set_volume(sc->card, M_MASTER_REAR_L, left); 298 emumix_set_volume(sc->card, M_MASTER_REAR_R, right); 299 if (!sc->is_emu10k1) { 300 emumix_set_volume(sc->card, M_MASTER_CENTER, (left+right)/2); 301 emumix_set_volume(sc->card, M_MASTER_SUBWOOFER, (left+right)/2); 302 /* XXX side */ 303 } 304 } /* mch disabled */ 305 break; 306 case RT_REAR: 307 emumix_set_volume(sc->card, M_MASTER_REAR_L, left); 308 emumix_set_volume(sc->card, M_MASTER_REAR_R, right); 309 break; 310 case RT_CENTER: 311 emumix_set_volume(sc->card, M_MASTER_CENTER, (left+right)/2); 312 break; 313 case RT_SUB: 314 emumix_set_volume(sc->card, M_MASTER_SUBWOOFER, (left+right)/2); 315 break; 316 } 317 break; 318 case SOUND_MIXER_PCM: 319 switch (sc->route) { 320 case RT_FRONT: 321 if (sc->sm != NULL) 322 mix_set(sc->sm, dev, left, right); 323 if (sc->mch_disabled) { 324 /* See SOUND_MIXER_VOLUME case */ 325 if (sc->is_emu10k1) { 326 sc->emu10k1_volcache[1][0] = left; 327 left = left * sc->emu10k1_volcache[0][0] / 100; 328 sc->emu10k1_volcache[1][1] = right; 329 right = right * sc->emu10k1_volcache[0][1] / 100; 330 } 331 emumix_set_volume(sc->card, M_MASTER_REAR_L, left); 332 emumix_set_volume(sc->card, M_MASTER_REAR_R, right); 333 334 if (!sc->is_emu10k1) { 335 emumix_set_volume(sc->card, M_MASTER_CENTER, (left+right)/2); 336 emumix_set_volume(sc->card, M_MASTER_SUBWOOFER, (left+right)/2); 337 /* XXX side */ 338 } 339 } /* mch_disabled */ 340 break; 341 case RT_REAR: 342 emumix_set_volume(sc->card, M_FX2_REAR_L, left); 343 emumix_set_volume(sc->card, M_FX3_REAR_R, right); 344 break; 345 case RT_CENTER: 346 emumix_set_volume(sc->card, M_FX4_CENTER, (left+right)/2); 347 break; 348 case RT_SUB: 349 emumix_set_volume(sc->card, M_FX5_SUBWOOFER, (left+right)/2); 350 break; 351 } 352 break; 353 case SOUND_MIXER_DIGITAL1: /* CD SPDIF, in1 */ 354 emumix_set_volume(sc->card, M_IN1_FRONT_L, left); 355 emumix_set_volume(sc->card, M_IN1_FRONT_R, right); 356 break; 357 case SOUND_MIXER_DIGITAL2: 358 if (sc->is_emu10k1) { 359 /* TOSLink, in3 */ 360 emumix_set_volume(sc->card, M_IN3_FRONT_L, left); 361 emumix_set_volume(sc->card, M_IN3_FRONT_R, right); 362 } else { 363 /* COAX SPDIF, in2 */ 364 emumix_set_volume(sc->card, M_IN2_FRONT_L, left); 365 emumix_set_volume(sc->card, M_IN2_FRONT_R, right); 366 } 367 break; 368 case SOUND_MIXER_LINE2: /* Line-In2, in4 */ 369 emumix_set_volume(sc->card, M_IN4_FRONT_L, left); 370 emumix_set_volume(sc->card, M_IN4_FRONT_R, right); 371 break; 372 case SOUND_MIXER_DIGITAL3: /* on-card SPDIF, in5 */ 373 emumix_set_volume(sc->card, M_IN5_FRONT_L, left); 374 emumix_set_volume(sc->card, M_IN5_FRONT_R, right); 375 break; 376 case SOUND_MIXER_LINE3: /* AUX2, in6 */ 377 emumix_set_volume(sc->card, M_IN6_FRONT_L, left); 378 emumix_set_volume(sc->card, M_IN6_FRONT_R, right); 379 break; 380 default: 381 if (sc->sm != NULL) { 382 /* XXX emumix_set_volume is not required here */ 383 emumix_set_volume(sc->card, M_IN0_FRONT_L, 100); 384 emumix_set_volume(sc->card, M_IN0_FRONT_R, 100); 385 mix_set(sc->sm, dev, left, right); 386 } else 387 device_printf(sc->dev, "mixer error: unknown device %d\n", dev); 388 } 389 return (0); 390 } 391 392 static u_int32_t 393 emu_dspmixer_setrecsrc(struct snd_mixer *m, u_int32_t src) 394 { 395 struct emu_pcm_info *sc; 396 int i; 397 u_int32_t recmask; 398 int input[8]; 399 400 sc = mix_getdevinfo(m); 401 recmask = 0; 402 for (i=0; i < 8; i++) 403 input[i]=0; 404 405 if (sc->sm != NULL) 406 if ((src & sc->ac97_recdevs) !=0) 407 if (mix_setrecsrc(sc->sm, src & sc->ac97_recdevs) == 0) { 408 recmask |= (src & sc->ac97_recdevs); 409 /* Recording from AC97 codec. 410 Enable AC97 route to rec on DSP */ 411 input[0] = 1; 412 } 413 if (sc->is_emu10k1) { 414 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 415 if (dspmix_k1[i].recdev) 416 if ((src & (1 << i)) == ((uint32_t)1 << i)) { 417 recmask |= (1 << i); 418 /* enable device i */ 419 input[dspmix_k1[i].input] = 1; 420 } 421 } 422 } else { 423 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 424 if (dspmix_k2[i].recdev) 425 if ((src & (1 << i)) == ((uint32_t)1 << i)) { 426 recmask |= (1 << i); 427 /* enable device i */ 428 input[dspmix_k2[i].input] = 1; 429 } 430 } 431 } 432 emumix_set_volume(sc->card, M_IN0_REC_L, input[0] == 1 ? 100 : 0); 433 emumix_set_volume(sc->card, M_IN0_REC_R, input[0] == 1 ? 100 : 0); 434 435 emumix_set_volume(sc->card, M_IN1_REC_L, input[1] == 1 ? 100 : 0); 436 emumix_set_volume(sc->card, M_IN1_REC_R, input[1] == 1 ? 100 : 0); 437 438 if (!sc->is_emu10k1) { 439 emumix_set_volume(sc->card, M_IN2_REC_L, input[2] == 1 ? 100 : 0); 440 emumix_set_volume(sc->card, M_IN2_REC_R, input[2] == 1 ? 100 : 0); 441 } 442 443 if (sc->is_emu10k1) { 444 emumix_set_volume(sc->card, M_IN3_REC_L, input[3] == 1 ? 100 : 0); 445 emumix_set_volume(sc->card, M_IN3_REC_R, input[3] == 1 ? 100 : 0); 446 } 447 448 emumix_set_volume(sc->card, M_IN4_REC_L, input[4] == 1 ? 100 : 0); 449 emumix_set_volume(sc->card, M_IN4_REC_R, input[4] == 1 ? 100 : 0); 450 451 emumix_set_volume(sc->card, M_IN5_REC_L, input[5] == 1 ? 100 : 0); 452 emumix_set_volume(sc->card, M_IN5_REC_R, input[5] == 1 ? 100 : 0); 453 454 emumix_set_volume(sc->card, M_IN6_REC_L, input[6] == 1 ? 100 : 0); 455 emumix_set_volume(sc->card, M_IN6_REC_R, input[6] == 1 ? 100 : 0); 456 457 /* XXX check for K1/k2 differences? */ 458 if ((src & (1 << SOUND_MIXER_PCM)) == (1 << SOUND_MIXER_PCM)) { 459 emumix_set_volume(sc->card, M_FX0_REC_L, emumix_get_volume(sc->card, M_FX0_FRONT_L)); 460 emumix_set_volume(sc->card, M_FX1_REC_R, emumix_get_volume(sc->card, M_FX1_FRONT_R)); 461 } else { 462 emumix_set_volume(sc->card, M_FX0_REC_L, 0); 463 emumix_set_volume(sc->card, M_FX1_REC_R, 0); 464 } 465 466 return (recmask); 467 } 468 469 static kobj_method_t emudspmixer_methods[] = { 470 KOBJMETHOD(mixer_init, emu_dspmixer_init), 471 KOBJMETHOD(mixer_uninit, emu_dspmixer_uninit), 472 KOBJMETHOD(mixer_set, emu_dspmixer_set), 473 KOBJMETHOD(mixer_setrecsrc, emu_dspmixer_setrecsrc), 474 KOBJMETHOD_END 475 }; 476 MIXER_DECLARE(emudspmixer); 477 478 static int 479 emu_efxmixer_init(struct snd_mixer *m) 480 { 481 mix_setdevs(m, SOUND_MASK_VOLUME); 482 mix_setrecdevs(m, SOUND_MASK_MONITOR); 483 return (0); 484 } 485 486 static int 487 emu_efxmixer_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right) 488 { 489 if (left + right == 200) return (0); 490 return (0); 491 } 492 493 static u_int32_t 494 emu_efxmixer_setrecsrc(struct snd_mixer *m __unused, u_int32_t src __unused) 495 { 496 return (SOUND_MASK_MONITOR); 497 } 498 499 static kobj_method_t emuefxmixer_methods[] = { 500 KOBJMETHOD(mixer_init, emu_efxmixer_init), 501 KOBJMETHOD(mixer_set, emu_efxmixer_set), 502 KOBJMETHOD(mixer_setrecsrc, emu_efxmixer_setrecsrc), 503 KOBJMETHOD_END 504 }; 505 MIXER_DECLARE(emuefxmixer); 506 507 /* 508 * AC97 emulation code for Audigy and later cards. 509 * Some parts of AC97 codec are not used by hardware, but can be used 510 * to change some DSP controls via AC97 mixer interface. This includes: 511 * - master volume controls MASTER_FRONT_[R|L] 512 * - pcm volume controls FX[0|1]_FRONT_[R|L] 513 * - rec volume controls MASTER_REC_[R|L] 514 * We do it because we need to put it under user control.... 515 * We also keep some parts of AC97 disabled to get better sound quality 516 */ 517 518 #define AC97LEFT(x) ((x & 0x7F00)>>8) 519 #define AC97RIGHT(x) (x & 0x007F) 520 #define AC97MUTE(x) ((x & 0x8000)>>15) 521 #define BIT4_TO100(x) (100-(x)*100/(0x0f)) 522 #define BIT6_TO100(x) (100-(x)*100/(0x3f)) 523 #define BIT4_TO255(x) (255-(x)*255/(0x0f)) 524 #define BIT6_TO255(x) (255-(x)*255/(0x3f)) 525 #define V100_TOBIT6(x) (0x3f*(100-x)/100) 526 #define V100_TOBIT4(x) (0x0f*(100-x)/100) 527 #define AC97ENCODE(x_muted, x_left, x_right) (((x_muted & 1)<<15) | ((x_left & 0x3f)<<8) | (x_right & 0x3f)) 528 529 static int 530 emu_ac97_read_emulation(struct emu_pcm_info *sc, int regno) 531 { 532 int use_ac97; 533 int emulated; 534 int tmp; 535 536 use_ac97 = 1; 537 emulated = 0; 538 539 switch (regno) { 540 case AC97_MIX_MASTER: 541 emulated = sc->ac97_state[AC97_MIX_MASTER]; 542 use_ac97 = 0; 543 break; 544 case AC97_MIX_PCM: 545 emulated = sc->ac97_state[AC97_MIX_PCM]; 546 use_ac97 = 0; 547 break; 548 case AC97_REG_RECSEL: 549 emulated = 0x0505; 550 use_ac97 = 0; 551 break; 552 case AC97_MIX_RGAIN: 553 emulated = sc->ac97_state[AC97_MIX_RGAIN]; 554 use_ac97 = 0; 555 break; 556 } 557 558 emu_wr(sc->card, EMU_AC97ADDR, regno, 1); 559 tmp = emu_rd(sc->card, EMU_AC97DATA, 2); 560 561 if (use_ac97) 562 emulated = tmp; 563 564 return (emulated); 565 } 566 567 static void 568 emu_ac97_write_emulation(struct emu_pcm_info *sc, int regno, uint32_t data) 569 { 570 int write_ac97; 571 int left, right; 572 uint32_t emu_left, emu_right; 573 int is_mute; 574 575 write_ac97 = 1; 576 577 left = AC97LEFT(data); 578 emu_left = BIT6_TO100(left); /* We show us as 6-bit AC97 mixer */ 579 right = AC97RIGHT(data); 580 emu_right = BIT6_TO100(right); 581 is_mute = AC97MUTE(data); 582 if (is_mute) 583 emu_left = emu_right = 0; 584 585 switch (regno) { 586 /* TODO: reset emulator on AC97_RESET */ 587 case AC97_MIX_MASTER: 588 emumix_set_volume(sc->card, M_MASTER_FRONT_L, emu_left); 589 emumix_set_volume(sc->card, M_MASTER_FRONT_R, emu_right); 590 sc->ac97_state[AC97_MIX_MASTER] = data & (0x8000 | 0x3f3f); 591 data = 0x8000; /* Mute AC97 main out */ 592 break; 593 case AC97_MIX_PCM: /* PCM OUT VOL */ 594 emumix_set_volume(sc->card, M_FX0_FRONT_L, emu_left); 595 emumix_set_volume(sc->card, M_FX1_FRONT_R, emu_right); 596 sc->ac97_state[AC97_MIX_PCM] = data & (0x8000 | 0x3f3f); 597 data = 0x8000; /* Mute AC97 PCM out */ 598 break; 599 case AC97_REG_RECSEL: 600 /* 601 * PCM recording source is set to "stereo mix" (labeled "vol" 602 * in mixer). There is no 'playback' from AC97 codec - 603 * if you want to hear anything from AC97 you have to _record_ 604 * it. Keep things simple and record "stereo mix". 605 */ 606 data = 0x0505; 607 break; 608 case AC97_MIX_RGAIN: /* RECORD GAIN */ 609 emu_left = BIT4_TO100(left); /* rgain is 4-bit */ 610 emu_right = BIT4_TO100(right); 611 emumix_set_volume(sc->card, M_MASTER_REC_L, 100-emu_left); 612 emumix_set_volume(sc->card, M_MASTER_REC_R, 100-emu_right); 613 /* 614 * Record gain on AC97 should stay zero to get AC97 sound on 615 * AC97_[RL] connectors on EMU10K2 chip. AC97 on Audigy is not 616 * directly connected to any output, only to EMU10K2 chip Use 617 * this control to set AC97 mix volume inside EMU10K2 chip 618 */ 619 sc->ac97_state[AC97_MIX_RGAIN] = data & (0x8000 | 0x0f0f); 620 data = 0x0000; 621 break; 622 } 623 if (write_ac97) { 624 emu_wr(sc->card, EMU_AC97ADDR, regno, 1); 625 emu_wr(sc->card, EMU_AC97DATA, data, 2); 626 } 627 } 628 629 static int 630 emu_erdcd(kobj_t obj __unused, void *devinfo, int regno) 631 { 632 struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo; 633 634 return (emu_ac97_read_emulation(sc, regno)); 635 } 636 637 static int 638 emu_ewrcd(kobj_t obj __unused, void *devinfo, int regno, uint32_t data) 639 { 640 struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo; 641 642 emu_ac97_write_emulation(sc, regno, data); 643 return (0); 644 } 645 646 static kobj_method_t emu_eac97_methods[] = { 647 KOBJMETHOD(ac97_read, emu_erdcd), 648 KOBJMETHOD(ac97_write, emu_ewrcd), 649 KOBJMETHOD_END 650 }; 651 AC97_DECLARE(emu_eac97); 652 653 /* real ac97 codec */ 654 static int 655 emu_rdcd(kobj_t obj __unused, void *devinfo, int regno) 656 { 657 int rd; 658 struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo; 659 660 KASSERT(sc->card != NULL, ("emu_rdcd: no soundcard")); 661 emu_wr(sc->card, EMU_AC97ADDR, regno, 1); 662 rd = emu_rd(sc->card, EMU_AC97DATA, 2); 663 return (rd); 664 } 665 666 static int 667 emu_wrcd(kobj_t obj __unused, void *devinfo, int regno, uint32_t data) 668 { 669 struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo; 670 671 KASSERT(sc->card != NULL, ("emu_wrcd: no soundcard")); 672 emu_wr(sc->card, EMU_AC97ADDR, regno, 1); 673 emu_wr(sc->card, EMU_AC97DATA, data, 2); 674 return (0); 675 } 676 677 static kobj_method_t emu_ac97_methods[] = { 678 KOBJMETHOD(ac97_read, emu_rdcd), 679 KOBJMETHOD(ac97_write, emu_wrcd), 680 KOBJMETHOD_END 681 }; 682 AC97_DECLARE(emu_ac97); 683 684 685 static int 686 emu_k1_recval(int speed) 687 { 688 int val; 689 690 val = 0; 691 while ((val < 7) && (speed < emu10k1_adcspeed[val])) 692 val++; 693 return (val); 694 } 695 696 static int 697 emu_k2_recval(int speed) 698 { 699 int val; 700 701 val = 0; 702 while ((val < 8) && (speed < emu10k2_adcspeed[val])) 703 val++; 704 return (val); 705 } 706 707 static void * 708 emupchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused) 709 { 710 struct emu_pcm_info *sc = devinfo; 711 struct emu_pcm_pchinfo *ch; 712 void *r; 713 714 KASSERT(dir == PCMDIR_PLAY, ("emupchan_init: bad direction")); 715 KASSERT(sc->card != NULL, ("empchan_init: no soundcard")); 716 717 718 if (sc->pnum >= MAX_CHANNELS) 719 return (NULL); 720 ch = &(sc->pch[sc->pnum++]); 721 ch->buffer = b; 722 ch->pcm = sc; 723 ch->channel = c; 724 ch->blksz = sc->bufsz; 725 ch->fmt = SND_FORMAT(AFMT_U8, 1, 0); 726 ch->spd = 8000; 727 ch->master = emu_valloc(sc->card); 728 /* 729 * XXX we have to allocate slave even for mono channel until we 730 * fix emu_vfree to handle this case. 731 */ 732 ch->slave = emu_valloc(sc->card); 733 ch->timer = emu_timer_create(sc->card); 734 r = (emu_vinit(sc->card, ch->master, ch->slave, EMU_PLAY_BUFSZ, ch->buffer)) ? NULL : ch; 735 return (r); 736 } 737 738 static int 739 emupchan_free(kobj_t obj __unused, void *c_devinfo) 740 { 741 struct emu_pcm_pchinfo *ch = c_devinfo; 742 struct emu_pcm_info *sc = ch->pcm; 743 744 emu_timer_clear(sc->card, ch->timer); 745 if (ch->slave != NULL) 746 emu_vfree(sc->card, ch->slave); 747 emu_vfree(sc->card, ch->master); 748 return (0); 749 } 750 751 static int 752 emupchan_setformat(kobj_t obj __unused, void *c_devinfo, uint32_t format) 753 { 754 struct emu_pcm_pchinfo *ch = c_devinfo; 755 756 ch->fmt = format; 757 return (0); 758 } 759 760 static uint32_t 761 emupchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed) 762 { 763 struct emu_pcm_pchinfo *ch = c_devinfo; 764 765 ch->spd = speed; 766 return (ch->spd); 767 } 768 769 static uint32_t 770 emupchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize) 771 { 772 struct emu_pcm_pchinfo *ch = c_devinfo; 773 struct emu_pcm_info *sc = ch->pcm; 774 775 if (blocksize > ch->pcm->bufsz) 776 blocksize = ch->pcm->bufsz; 777 snd_mtxlock(sc->lock); 778 ch->blksz = blocksize; 779 emu_timer_set(sc->card, ch->timer, ch->blksz / sndbuf_getalign(ch->buffer)); 780 snd_mtxunlock(sc->lock); 781 return (ch->blksz); 782 } 783 784 static int 785 emupchan_trigger(kobj_t obj __unused, void *c_devinfo, int go) 786 { 787 struct emu_pcm_pchinfo *ch = c_devinfo; 788 struct emu_pcm_info *sc = ch->pcm; 789 790 if (!PCMTRIG_COMMON(go)) 791 return (0); 792 793 snd_mtxlock(sc->lock); /* XXX can we trigger on parallel threads ? */ 794 if (go == PCMTRIG_START) { 795 emu_vsetup(ch->master, ch->fmt, ch->spd); 796 if (AFMT_CHANNEL(ch->fmt) > 1) 797 emu_vroute(sc->card, &(sc->rt), ch->master); 798 else 799 emu_vroute(sc->card, &(sc->rt_mono), ch->master); 800 emu_vwrite(sc->card, ch->master); 801 emu_timer_set(sc->card, ch->timer, ch->blksz / sndbuf_getalign(ch->buffer)); 802 emu_timer_enable(sc->card, ch->timer, 1); 803 } 804 /* PCM interrupt handler will handle PCMTRIG_STOP event */ 805 ch->run = (go == PCMTRIG_START) ? 1 : 0; 806 emu_vtrigger(sc->card, ch->master, ch->run); 807 snd_mtxunlock(sc->lock); 808 return (0); 809 } 810 811 static uint32_t 812 emupchan_getptr(kobj_t obj __unused, void *c_devinfo) 813 { 814 struct emu_pcm_pchinfo *ch = c_devinfo; 815 struct emu_pcm_info *sc = ch->pcm; 816 int r; 817 818 r = emu_vpos(sc->card, ch->master); 819 820 return (r); 821 } 822 823 static struct pcmchan_caps * 824 emupchan_getcaps(kobj_t obj __unused, void *c_devinfo __unused) 825 { 826 struct emu_pcm_pchinfo *ch = c_devinfo; 827 struct emu_pcm_info *sc = ch->pcm; 828 829 switch (sc->route) { 830 case RT_FRONT: 831 /* FALLTHROUGH */ 832 case RT_REAR: 833 /* FALLTHROUGH */ 834 case RT_SIDE: 835 return (&emu_playcaps); 836 break; 837 case RT_CENTER: 838 /* FALLTHROUGH */ 839 case RT_SUB: 840 return (&emu_playcaps_mono); 841 break; 842 } 843 return (NULL); 844 } 845 846 static kobj_method_t emupchan_methods[] = { 847 KOBJMETHOD(channel_init, emupchan_init), 848 KOBJMETHOD(channel_free, emupchan_free), 849 KOBJMETHOD(channel_setformat, emupchan_setformat), 850 KOBJMETHOD(channel_setspeed, emupchan_setspeed), 851 KOBJMETHOD(channel_setblocksize, emupchan_setblocksize), 852 KOBJMETHOD(channel_trigger, emupchan_trigger), 853 KOBJMETHOD(channel_getptr, emupchan_getptr), 854 KOBJMETHOD(channel_getcaps, emupchan_getcaps), 855 KOBJMETHOD_END 856 }; 857 CHANNEL_DECLARE(emupchan); 858 859 static void * 860 emurchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused) 861 { 862 struct emu_pcm_info *sc = devinfo; 863 struct emu_pcm_rchinfo *ch; 864 865 KASSERT(dir == PCMDIR_REC, ("emurchan_init: bad direction")); 866 ch = &sc->rch_adc; 867 ch->buffer = b; 868 ch->pcm = sc; 869 ch->channel = c; 870 ch->blksz = sc->bufsz / 2; /* We rise interrupt for half-full buffer */ 871 ch->fmt = SND_FORMAT(AFMT_U8, 1, 0); 872 ch->spd = 8000; 873 ch->idxreg = sc->is_emu10k1 ? EMU_ADCIDX : EMU_A_ADCIDX; 874 ch->basereg = EMU_ADCBA; 875 ch->sizereg = EMU_ADCBS; 876 ch->setupreg = EMU_ADCCR; 877 ch->irqmask = EMU_INTE_ADCBUFENABLE; 878 ch->iprmask = EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL; 879 880 if (sndbuf_alloc(ch->buffer, emu_gettag(sc->card), 0, sc->bufsz) != 0) 881 return (NULL); 882 else { 883 ch->timer = emu_timer_create(sc->card); 884 emu_wrptr(sc->card, 0, ch->basereg, sndbuf_getbufaddr(ch->buffer)); 885 emu_wrptr(sc->card, 0, ch->sizereg, 0); /* off */ 886 return (ch); 887 } 888 } 889 890 static int 891 emurchan_free(kobj_t obj __unused, void *c_devinfo) 892 { 893 struct emu_pcm_rchinfo *ch = c_devinfo; 894 struct emu_pcm_info *sc = ch->pcm; 895 896 emu_timer_clear(sc->card, ch->timer); 897 return (0); 898 } 899 900 static int 901 emurchan_setformat(kobj_t obj __unused, void *c_devinfo, uint32_t format) 902 { 903 struct emu_pcm_rchinfo *ch = c_devinfo; 904 905 ch->fmt = format; 906 return (0); 907 } 908 909 static uint32_t 910 emurchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed) 911 { 912 struct emu_pcm_rchinfo *ch = c_devinfo; 913 914 if (ch->pcm->is_emu10k1) { 915 speed = emu10k1_adcspeed[emu_k1_recval(speed)]; 916 } else { 917 speed = emu10k2_adcspeed[emu_k2_recval(speed)]; 918 } 919 ch->spd = speed; 920 return (ch->spd); 921 } 922 923 static uint32_t 924 emurchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize) 925 { 926 struct emu_pcm_rchinfo *ch = c_devinfo; 927 struct emu_pcm_info *sc = ch->pcm; 928 929 ch->blksz = blocksize; 930 /* 931 * If blocksize is less than half of buffer size we will not get 932 * BUFHALFFULL interrupt in time and channel will need to generate 933 * (and use) timer interrupts. Otherwise channel will be marked dead. 934 */ 935 if (ch->blksz < (ch->pcm->bufsz / 2)) { 936 emu_timer_set(sc->card, ch->timer, ch->blksz / sndbuf_getalign(ch->buffer)); 937 emu_timer_enable(sc->card, ch->timer, 1); 938 } else { 939 emu_timer_enable(sc->card, ch->timer, 0); 940 } 941 return (ch->blksz); 942 } 943 944 static int 945 emurchan_trigger(kobj_t obj __unused, void *c_devinfo, int go) 946 { 947 struct emu_pcm_rchinfo *ch = c_devinfo; 948 struct emu_pcm_info *sc = ch->pcm; 949 uint32_t val, sz; 950 951 if (!PCMTRIG_COMMON(go)) 952 return (0); 953 954 switch (sc->bufsz) { 955 case 4096: 956 sz = EMU_RECBS_BUFSIZE_4096; 957 break; 958 case 8192: 959 sz = EMU_RECBS_BUFSIZE_8192; 960 break; 961 case 16384: 962 sz = EMU_RECBS_BUFSIZE_16384; 963 break; 964 case 32768: 965 sz = EMU_RECBS_BUFSIZE_32768; 966 break; 967 case 65536: 968 sz = EMU_RECBS_BUFSIZE_65536; 969 break; 970 default: 971 sz = EMU_RECBS_BUFSIZE_4096; 972 } 973 974 snd_mtxlock(sc->lock); 975 switch (go) { 976 case PCMTRIG_START: 977 ch->run = 1; 978 emu_wrptr(sc->card, 0, ch->sizereg, sz); 979 val = sc->is_emu10k1 ? EMU_ADCCR_LCHANENABLE : EMU_A_ADCCR_LCHANENABLE; 980 if (AFMT_CHANNEL(ch->fmt) > 1) 981 val |= sc->is_emu10k1 ? EMU_ADCCR_RCHANENABLE : EMU_A_ADCCR_RCHANENABLE; 982 val |= sc->is_emu10k1 ? emu_k1_recval(ch->spd) : emu_k2_recval(ch->spd); 983 emu_wrptr(sc->card, 0, ch->setupreg, 0); 984 emu_wrptr(sc->card, 0, ch->setupreg, val); 985 ch->ihandle = emu_intr_register(sc->card, ch->irqmask, ch->iprmask, &emu_pcm_intr, sc); 986 break; 987 case PCMTRIG_STOP: 988 /* FALLTHROUGH */ 989 case PCMTRIG_ABORT: 990 ch->run = 0; 991 emu_wrptr(sc->card, 0, ch->sizereg, 0); 992 if (ch->setupreg) 993 emu_wrptr(sc->card, 0, ch->setupreg, 0); 994 (void)emu_intr_unregister(sc->card, ch->ihandle); 995 break; 996 case PCMTRIG_EMLDMAWR: 997 /* FALLTHROUGH */ 998 case PCMTRIG_EMLDMARD: 999 /* FALLTHROUGH */ 1000 default: 1001 break; 1002 } 1003 snd_mtxunlock(sc->lock); 1004 1005 return (0); 1006 } 1007 1008 static uint32_t 1009 emurchan_getptr(kobj_t obj __unused, void *c_devinfo) 1010 { 1011 struct emu_pcm_rchinfo *ch = c_devinfo; 1012 struct emu_pcm_info *sc = ch->pcm; 1013 int r; 1014 1015 r = emu_rdptr(sc->card, 0, ch->idxreg) & 0x0000ffff; 1016 1017 return (r); 1018 } 1019 1020 static struct pcmchan_caps * 1021 emurchan_getcaps(kobj_t obj __unused, void *c_devinfo __unused) 1022 { 1023 return (&emu_reccaps_adc); 1024 } 1025 1026 static kobj_method_t emurchan_methods[] = { 1027 KOBJMETHOD(channel_init, emurchan_init), 1028 KOBJMETHOD(channel_free, emurchan_free), 1029 KOBJMETHOD(channel_setformat, emurchan_setformat), 1030 KOBJMETHOD(channel_setspeed, emurchan_setspeed), 1031 KOBJMETHOD(channel_setblocksize, emurchan_setblocksize), 1032 KOBJMETHOD(channel_trigger, emurchan_trigger), 1033 KOBJMETHOD(channel_getptr, emurchan_getptr), 1034 KOBJMETHOD(channel_getcaps, emurchan_getcaps), 1035 KOBJMETHOD_END 1036 }; 1037 CHANNEL_DECLARE(emurchan); 1038 1039 static void * 1040 emufxrchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused) 1041 { 1042 struct emu_pcm_info *sc = devinfo; 1043 struct emu_pcm_rchinfo *ch; 1044 1045 KASSERT(dir == PCMDIR_REC, ("emurchan_init: bad direction")); 1046 1047 if (sc == NULL) return (NULL); 1048 1049 ch = &(sc->rch_efx); 1050 ch->fmt = SND_FORMAT(AFMT_S16_LE, 1, 0); 1051 ch->spd = sc->is_emu10k1 ? 48000*32 : 48000 * 64; 1052 ch->idxreg = EMU_FXIDX; 1053 ch->basereg = EMU_FXBA; 1054 ch->sizereg = EMU_FXBS; 1055 ch->irqmask = EMU_INTE_EFXBUFENABLE; 1056 ch->iprmask = EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL; 1057 ch->buffer = b; 1058 ch->pcm = sc; 1059 ch->channel = c; 1060 ch->blksz = sc->bufsz / 2; 1061 1062 if (sndbuf_alloc(ch->buffer, emu_gettag(sc->card), 0, sc->bufsz) != 0) 1063 return (NULL); 1064 else { 1065 emu_wrptr(sc->card, 0, ch->basereg, sndbuf_getbufaddr(ch->buffer)); 1066 emu_wrptr(sc->card, 0, ch->sizereg, 0); /* off */ 1067 return (ch); 1068 } 1069 } 1070 1071 static int 1072 emufxrchan_setformat(kobj_t obj __unused, void *c_devinfo __unused, uint32_t format) 1073 { 1074 if (format == SND_FORMAT(AFMT_S16_LE, 1, 0)) return (0); 1075 return (EINVAL); 1076 } 1077 1078 static uint32_t 1079 emufxrchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed) 1080 { 1081 struct emu_pcm_rchinfo *ch = c_devinfo; 1082 1083 /* FIXED RATE CHANNEL */ 1084 return (ch->spd); 1085 } 1086 1087 static uint32_t 1088 emufxrchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize) 1089 { 1090 struct emu_pcm_rchinfo *ch = c_devinfo; 1091 1092 ch->blksz = blocksize; 1093 /* 1094 * XXX If blocksize is less than half of buffer size we will not get 1095 * interrupt in time and channel will die due to interrupt timeout. 1096 * This should not happen with FX rchan, because it will fill buffer 1097 * very fast (64K buffer is 0.021seconds on Audigy). 1098 */ 1099 if (ch->blksz < (ch->pcm->bufsz / 2)) 1100 ch->blksz = ch->pcm->bufsz / 2; 1101 return (ch->blksz); 1102 } 1103 1104 static int 1105 emufxrchan_trigger(kobj_t obj __unused, void *c_devinfo, int go) 1106 { 1107 struct emu_pcm_rchinfo *ch = c_devinfo; 1108 struct emu_pcm_info *sc = ch->pcm; 1109 uint32_t sz; 1110 1111 if (!PCMTRIG_COMMON(go)) 1112 return (0); 1113 1114 switch (sc->bufsz) { 1115 case 4096: 1116 sz = EMU_RECBS_BUFSIZE_4096; 1117 break; 1118 case 8192: 1119 sz = EMU_RECBS_BUFSIZE_8192; 1120 break; 1121 case 16384: 1122 sz = EMU_RECBS_BUFSIZE_16384; 1123 break; 1124 case 32768: 1125 sz = EMU_RECBS_BUFSIZE_32768; 1126 break; 1127 case 65536: 1128 sz = EMU_RECBS_BUFSIZE_65536; 1129 break; 1130 default: 1131 sz = EMU_RECBS_BUFSIZE_4096; 1132 } 1133 1134 snd_mtxlock(sc->lock); 1135 switch (go) { 1136 case PCMTRIG_START: 1137 ch->run = 1; 1138 emu_wrptr(sc->card, 0, ch->sizereg, sz); 1139 ch->ihandle = emu_intr_register(sc->card, ch->irqmask, ch->iprmask, &emu_pcm_intr, sc); 1140 /* 1141 * SB Live! is limited to 32 mono channels. Audigy 1142 * has 64 mono channels. Channels are enabled 1143 * by setting a bit in EMU_A_FXWC[1|2] registers. 1144 */ 1145 /* XXX there is no way to demultiplex this streams for now */ 1146 if (sc->is_emu10k1) { 1147 emu_wrptr(sc->card, 0, EMU_FXWC, 0xffffffff); 1148 } else { 1149 emu_wrptr(sc->card, 0, EMU_A_FXWC1, 0xffffffff); 1150 emu_wrptr(sc->card, 0, EMU_A_FXWC2, 0xffffffff); 1151 } 1152 break; 1153 case PCMTRIG_STOP: 1154 /* FALLTHROUGH */ 1155 case PCMTRIG_ABORT: 1156 ch->run = 0; 1157 if (sc->is_emu10k1) { 1158 emu_wrptr(sc->card, 0, EMU_FXWC, 0x0); 1159 } else { 1160 emu_wrptr(sc->card, 0, EMU_A_FXWC1, 0x0); 1161 emu_wrptr(sc->card, 0, EMU_A_FXWC2, 0x0); 1162 } 1163 emu_wrptr(sc->card, 0, ch->sizereg, 0); 1164 (void)emu_intr_unregister(sc->card, ch->ihandle); 1165 break; 1166 case PCMTRIG_EMLDMAWR: 1167 /* FALLTHROUGH */ 1168 case PCMTRIG_EMLDMARD: 1169 /* FALLTHROUGH */ 1170 default: 1171 break; 1172 } 1173 snd_mtxunlock(sc->lock); 1174 1175 return (0); 1176 } 1177 1178 static uint32_t 1179 emufxrchan_getptr(kobj_t obj __unused, void *c_devinfo) 1180 { 1181 struct emu_pcm_rchinfo *ch = c_devinfo; 1182 struct emu_pcm_info *sc = ch->pcm; 1183 int r; 1184 1185 r = emu_rdptr(sc->card, 0, ch->idxreg) & 0x0000ffff; 1186 1187 return (r); 1188 } 1189 1190 static struct pcmchan_caps * 1191 emufxrchan_getcaps(kobj_t obj __unused, void *c_devinfo) 1192 { 1193 struct emu_pcm_rchinfo *ch = c_devinfo; 1194 struct emu_pcm_info *sc = ch->pcm; 1195 1196 if (sc->is_emu10k1) 1197 return (&emu_reccaps_efx_live); 1198 return (&emu_reccaps_efx_audigy); 1199 1200 } 1201 1202 static int 1203 emufxrchan_getrates(kobj_t obj __unused, void *c_devinfo, int **rates) 1204 { 1205 struct emu_pcm_rchinfo *ch = c_devinfo; 1206 struct emu_pcm_info *sc = ch->pcm; 1207 1208 if (sc->is_emu10k1) 1209 *rates = emu_rates_live; 1210 else 1211 *rates = emu_rates_audigy; 1212 1213 return 1; 1214 } 1215 1216 static kobj_method_t emufxrchan_methods[] = { 1217 KOBJMETHOD(channel_init, emufxrchan_init), 1218 KOBJMETHOD(channel_setformat, emufxrchan_setformat), 1219 KOBJMETHOD(channel_setspeed, emufxrchan_setspeed), 1220 KOBJMETHOD(channel_setblocksize, emufxrchan_setblocksize), 1221 KOBJMETHOD(channel_trigger, emufxrchan_trigger), 1222 KOBJMETHOD(channel_getptr, emufxrchan_getptr), 1223 KOBJMETHOD(channel_getcaps, emufxrchan_getcaps), 1224 KOBJMETHOD(channel_getrates, emufxrchan_getrates), 1225 KOBJMETHOD_END 1226 }; 1227 CHANNEL_DECLARE(emufxrchan); 1228 1229 1230 static uint32_t 1231 emu_pcm_intr(void *pcm, uint32_t stat) 1232 { 1233 struct emu_pcm_info *sc = (struct emu_pcm_info *)pcm; 1234 uint32_t ack; 1235 int i; 1236 1237 ack = 0; 1238 1239 snd_mtxlock(sc->lock); 1240 1241 if (stat & EMU_IPR_INTERVALTIMER) { 1242 ack |= EMU_IPR_INTERVALTIMER; 1243 for (i = 0; i < MAX_CHANNELS; i++) 1244 if (sc->pch[i].channel) { 1245 if (sc->pch[i].run == 1) { 1246 snd_mtxunlock(sc->lock); 1247 chn_intr(sc->pch[i].channel); 1248 snd_mtxlock(sc->lock); 1249 } else 1250 emu_timer_enable(sc->card, sc->pch[i].timer, 0); 1251 } 1252 /* ADC may install timer to get low-latency interrupts */ 1253 if ((sc->rch_adc.channel) && (sc->rch_adc.run)) { 1254 snd_mtxunlock(sc->lock); 1255 chn_intr(sc->rch_adc.channel); 1256 snd_mtxlock(sc->lock); 1257 } 1258 /* 1259 * EFX does not use timer, because it will fill 1260 * buffer at least 32x times faster than ADC. 1261 */ 1262 } 1263 1264 1265 if (stat & (EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL)) { 1266 ack |= stat & (EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL); 1267 if (sc->rch_adc.channel) { 1268 snd_mtxunlock(sc->lock); 1269 chn_intr(sc->rch_adc.channel); 1270 snd_mtxlock(sc->lock); 1271 } 1272 } 1273 1274 if (stat & (EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL)) { 1275 ack |= stat & (EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL); 1276 if (sc->rch_efx.channel) { 1277 snd_mtxunlock(sc->lock); 1278 chn_intr(sc->rch_efx.channel); 1279 snd_mtxlock(sc->lock); 1280 } 1281 } 1282 snd_mtxunlock(sc->lock); 1283 1284 return (ack); 1285 } 1286 1287 static int 1288 emu_pcm_init(struct emu_pcm_info *sc) 1289 { 1290 sc->bufsz = pcm_getbuffersize(sc->dev, EMUPAGESIZE, EMU_REC_BUFSZ, EMU_MAX_BUFSZ); 1291 return (0); 1292 } 1293 1294 static int 1295 emu_pcm_uninit(struct emu_pcm_info *sc __unused) 1296 { 1297 return (0); 1298 } 1299 1300 static int 1301 emu_pcm_probe(device_t dev) 1302 { 1303 uintptr_t func, route, r; 1304 const char *rt; 1305 char buffer[255]; 1306 1307 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_FUNC, &func); 1308 1309 if (func != SCF_PCM) 1310 return (ENXIO); 1311 1312 rt = "UNKNOWN"; 1313 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ROUTE, &route); 1314 switch (route) { 1315 case RT_FRONT: 1316 rt = "front"; 1317 break; 1318 case RT_REAR: 1319 rt = "rear"; 1320 break; 1321 case RT_CENTER: 1322 rt = "center"; 1323 break; 1324 case RT_SUB: 1325 rt = "subwoofer"; 1326 break; 1327 case RT_SIDE: 1328 rt = "side"; 1329 break; 1330 case RT_MCHRECORD: 1331 rt = "multichannel recording"; 1332 break; 1333 } 1334 1335 snprintf(buffer, 255, "EMU10Kx DSP %s PCM interface", rt); 1336 device_set_desc_copy(dev, buffer); 1337 return (0); 1338 } 1339 1340 static int 1341 emu_pcm_attach(device_t dev) 1342 { 1343 struct emu_pcm_info *sc; 1344 unsigned int i; 1345 char status[SND_STATUSLEN]; 1346 uint32_t inte, ipr; 1347 uintptr_t route, r, ivar; 1348 1349 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO); 1350 sc->card = (struct emu_sc_info *)(device_get_softc(device_get_parent(dev))); 1351 if (sc->card == NULL) { 1352 device_printf(dev, "cannot get bridge conf\n"); 1353 free(sc, M_DEVBUF); 1354 return (ENXIO); 1355 } 1356 1357 sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_emu10kx pcm softc"); 1358 sc->dev = dev; 1359 1360 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ISEMU10K1, &ivar); 1361 sc->is_emu10k1 = ivar ? 1 : 0; 1362 1363 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_MCH_DISABLED, &ivar); 1364 sc->mch_disabled = ivar ? 1 : 0; 1365 1366 sc->codec = NULL; 1367 1368 for (i = 0; i < 8; i++) { 1369 sc->rt.routing_left[i] = i; 1370 sc->rt.amounts_left[i] = 0x00; 1371 sc->rt.routing_right[i] = i; 1372 sc->rt.amounts_right[i] = 0x00; 1373 } 1374 1375 for (i = 0; i < 8; i++) { 1376 sc->rt_mono.routing_left[i] = i; 1377 sc->rt_mono.amounts_left[i] = 0x00; 1378 sc->rt_mono.routing_right[i] = i; 1379 sc->rt_mono.amounts_right[i] = 0x00; 1380 } 1381 1382 sc->emu10k1_volcache[0][0] = 75; 1383 sc->emu10k1_volcache[1][0] = 75; 1384 sc->emu10k1_volcache[0][1] = 75; 1385 sc->emu10k1_volcache[1][1] = 75; 1386 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ROUTE, &route); 1387 sc->route = route; 1388 switch (route) { 1389 case RT_FRONT: 1390 sc->rt.amounts_left[0] = 0xff; 1391 sc->rt.amounts_right[1] = 0xff; 1392 sc->rt_mono.amounts_left[0] = 0xff; 1393 sc->rt_mono.amounts_left[1] = 0xff; 1394 if (sc->is_emu10k1) 1395 sc->codec = AC97_CREATE(dev, sc, emu_ac97); 1396 else 1397 sc->codec = AC97_CREATE(dev, sc, emu_eac97); 1398 sc->ac97_mixerclass = NULL; 1399 if (sc->codec != NULL) 1400 sc->ac97_mixerclass = ac97_getmixerclass(); 1401 if (mixer_init(dev, &emudspmixer_class, sc)) { 1402 device_printf(dev, "failed to initialize DSP mixer\n"); 1403 goto bad; 1404 } 1405 break; 1406 case RT_REAR: 1407 sc->rt.amounts_left[2] = 0xff; 1408 sc->rt.amounts_right[3] = 0xff; 1409 sc->rt_mono.amounts_left[2] = 0xff; 1410 sc->rt_mono.amounts_left[3] = 0xff; 1411 if (mixer_init(dev, &emudspmixer_class, sc)) { 1412 device_printf(dev, "failed to initialize mixer\n"); 1413 goto bad; 1414 } 1415 break; 1416 case RT_CENTER: 1417 sc->rt.amounts_left[4] = 0xff; 1418 sc->rt_mono.amounts_left[4] = 0xff; 1419 if (mixer_init(dev, &emudspmixer_class, sc)) { 1420 device_printf(dev, "failed to initialize mixer\n"); 1421 goto bad; 1422 } 1423 break; 1424 case RT_SUB: 1425 sc->rt.amounts_left[5] = 0xff; 1426 sc->rt_mono.amounts_left[5] = 0xff; 1427 if (mixer_init(dev, &emudspmixer_class, sc)) { 1428 device_printf(dev, "failed to initialize mixer\n"); 1429 goto bad; 1430 } 1431 break; 1432 case RT_SIDE: 1433 sc->rt.amounts_left[6] = 0xff; 1434 sc->rt.amounts_right[7] = 0xff; 1435 sc->rt_mono.amounts_left[6] = 0xff; 1436 sc->rt_mono.amounts_left[7] = 0xff; 1437 if (mixer_init(dev, &emudspmixer_class, sc)) { 1438 device_printf(dev, "failed to initialize mixer\n"); 1439 goto bad; 1440 } 1441 break; 1442 case RT_MCHRECORD: 1443 if (mixer_init(dev, &emuefxmixer_class, sc)) { 1444 device_printf(dev, "failed to initialize EFX mixer\n"); 1445 goto bad; 1446 } 1447 break; 1448 default: 1449 device_printf(dev, "invalid default route\n"); 1450 goto bad; 1451 } 1452 1453 inte = EMU_INTE_INTERTIMERENB; 1454 ipr = EMU_IPR_INTERVALTIMER; /* Used by playback & ADC */ 1455 sc->ihandle = emu_intr_register(sc->card, inte, ipr, &emu_pcm_intr, sc); 1456 1457 if (emu_pcm_init(sc) == -1) { 1458 device_printf(dev, "unable to initialize PCM part of the card\n"); 1459 goto bad; 1460 } 1461 1462 /* 1463 * We don't register interrupt handler with snd_setup_intr 1464 * in pcm device. Mark pcm device as MPSAFE manually. 1465 */ 1466 pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE); 1467 1468 /* XXX we should better get number of available channels from parent */ 1469 if (pcm_register(dev, sc, (route == RT_FRONT) ? MAX_CHANNELS : 1, (route == RT_FRONT) ? 1 : 0)) { 1470 device_printf(dev, "can't register PCM channels!\n"); 1471 goto bad; 1472 } 1473 sc->pnum = 0; 1474 if (route != RT_MCHRECORD) 1475 pcm_addchan(dev, PCMDIR_PLAY, &emupchan_class, sc); 1476 if (route == RT_FRONT) { 1477 for (i = 1; i < MAX_CHANNELS; i++) 1478 pcm_addchan(dev, PCMDIR_PLAY, &emupchan_class, sc); 1479 pcm_addchan(dev, PCMDIR_REC, &emurchan_class, sc); 1480 } 1481 if (route == RT_MCHRECORD) 1482 pcm_addchan(dev, PCMDIR_REC, &emufxrchan_class, sc); 1483 1484 snprintf(status, SND_STATUSLEN, "on %s", device_get_nameunit(device_get_parent(dev))); 1485 pcm_setstatus(dev, status); 1486 1487 return (0); 1488 1489 bad: 1490 if (sc->codec) 1491 ac97_destroy(sc->codec); 1492 if (sc->lock) 1493 snd_mtxfree(sc->lock); 1494 free(sc, M_DEVBUF); 1495 return (ENXIO); 1496 } 1497 1498 static int 1499 emu_pcm_detach(device_t dev) 1500 { 1501 int r; 1502 struct emu_pcm_info *sc; 1503 1504 sc = pcm_getdevinfo(dev); 1505 1506 r = pcm_unregister(dev); 1507 1508 if (r) return (r); 1509 1510 emu_pcm_uninit(sc); 1511 1512 if (sc->lock) 1513 snd_mtxfree(sc->lock); 1514 free(sc, M_DEVBUF); 1515 1516 return (0); 1517 } 1518 1519 static device_method_t emu_pcm_methods[] = { 1520 DEVMETHOD(device_probe, emu_pcm_probe), 1521 DEVMETHOD(device_attach, emu_pcm_attach), 1522 DEVMETHOD(device_detach, emu_pcm_detach), 1523 1524 DEVMETHOD_END 1525 }; 1526 1527 static driver_t emu_pcm_driver = { 1528 "pcm", 1529 emu_pcm_methods, 1530 PCM_SOFTC_SIZE, 1531 NULL, 1532 0, 1533 NULL 1534 }; 1535 DRIVER_MODULE(snd_emu10kx_pcm, emu10kx, emu_pcm_driver, pcm_devclass, 0, 0); 1536 MODULE_DEPEND(snd_emu10kx_pcm, snd_emu10kx, SND_EMU10KX_MINVER, SND_EMU10KX_PREFVER, SND_EMU10KX_MAXVER); 1537 MODULE_DEPEND(snd_emu10kx_pcm, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 1538 MODULE_VERSION(snd_emu10kx_pcm, SND_EMU10KX_PREFVER); 1539