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