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 static uint32_t emu_rfmt_adc[] = { 120 SND_FORMAT(AFMT_S16_LE, 1, 0), 121 SND_FORMAT(AFMT_S16_LE, 2, 0), 122 0 123 }; 124 static struct pcmchan_caps emu_reccaps_adc = { 125 8000, 48000, emu_rfmt_adc, 0 126 }; 127 128 static uint32_t emu_rfmt_efx[] = { 129 SND_FORMAT(AFMT_S16_LE, 1, 0), 130 0 131 }; 132 133 static struct pcmchan_caps emu_reccaps_efx_live = { 134 48000*32, 48000*32, emu_rfmt_efx, 0 135 }; 136 137 static struct pcmchan_caps emu_reccaps_efx_audigy = { 138 48000*64, 48000*64, emu_rfmt_efx, 0 139 }; 140 141 static int emu_rates_live[] = { 142 48000*32 143 }; 144 145 static int emu_rates_audigy[] = { 146 48000*64 147 }; 148 149 static uint32_t emu_pfmt[] = { 150 SND_FORMAT(AFMT_U8, 1, 0), 151 SND_FORMAT(AFMT_U8, 2, 0), 152 SND_FORMAT(AFMT_S16_LE, 1, 0), 153 SND_FORMAT(AFMT_S16_LE, 2, 0), 154 0 155 }; 156 static uint32_t emu_pfmt_mono[] = { 157 SND_FORMAT(AFMT_U8, 1, 0), 158 SND_FORMAT(AFMT_S16_LE, 1, 0), 159 0 160 }; 161 162 static struct pcmchan_caps emu_playcaps = {4000, 48000, emu_pfmt, 0}; 163 static struct pcmchan_caps emu_playcaps_mono = {4000, 48000, emu_pfmt_mono, 0}; 164 165 static int emu10k1_adcspeed[8] = {48000, 44100, 32000, 24000, 22050, 16000, 11025, 8000}; 166 /* audigy supports 12kHz. */ 167 static int emu10k2_adcspeed[9] = {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000}; 168 169 static uint32_t emu_pcm_intr(void *pcm, uint32_t stat); 170 171 static const struct emu_dspmix_props_k1 { 172 uint8_t present; 173 uint8_t recdev; 174 int8_t input; 175 } dspmix_k1 [SOUND_MIXER_NRDEVICES] = { 176 /* no mixer device for ac97 */ /* in0 AC97 */ 177 [SOUND_MIXER_DIGITAL1] = {1, 1, 1}, /* in1 CD SPDIF */ 178 /* not connected */ /* in2 (zoom) */ 179 [SOUND_MIXER_DIGITAL2] = {1, 1, 3}, /* in3 toslink */ 180 [SOUND_MIXER_LINE2] = {1, 1, 4}, /* in4 Line-In2 */ 181 [SOUND_MIXER_DIGITAL3] = {1, 1, 5}, /* in5 on-card SPDIF */ 182 [SOUND_MIXER_LINE3] = {1, 1, 6}, /* in6 AUX2 */ 183 /* not connected */ /* in7 */ 184 }; 185 static const struct emu_dspmix_props_k2 { 186 uint8_t present; 187 uint8_t recdev; 188 int8_t input; 189 } dspmix_k2 [SOUND_MIXER_NRDEVICES] = { 190 [SOUND_MIXER_VOLUME] = {1, 0, (-1)}, 191 [SOUND_MIXER_PCM] = {1, 0, (-1)}, 192 193 /* no mixer device */ /* in0 AC97 */ 194 [SOUND_MIXER_DIGITAL1] = {1, 1, 1}, /* in1 CD SPDIF */ 195 [SOUND_MIXER_DIGITAL2] = {1, 1, 2}, /* in2 COAX SPDIF */ 196 /* not connected */ /* in3 */ 197 [SOUND_MIXER_LINE2] = {1, 1, 4}, /* in4 Line-In2 */ 198 [SOUND_MIXER_DIGITAL3] = {1, 1, 5}, /* in5 on-card SPDIF */ 199 [SOUND_MIXER_LINE3] = {1, 1, 6}, /* in6 AUX2 */ 200 /* not connected */ /* in7 */ 201 }; 202 203 static int 204 emu_dspmixer_init(struct snd_mixer *m) 205 { 206 struct emu_pcm_info *sc; 207 int i; 208 int p, r; 209 210 p = 0; 211 r = 0; 212 213 sc = mix_getdevinfo(m); 214 215 if (sc->route == RT_FRONT) { 216 /* create submixer for AC97 codec */ 217 if ((sc->ac97_mixerclass != NULL) && (sc->codec != NULL)) { 218 sc->sm = mixer_create(sc->dev, sc->ac97_mixerclass, sc->codec, "ac97"); 219 if (sc->sm != NULL) { 220 p = mix_getdevs(sc->sm); 221 r = mix_getrecdevs(sc->sm); 222 } 223 } 224 225 sc->ac97_playdevs = p; 226 sc->ac97_recdevs = r; 227 } 228 229 /* This two are always here */ 230 p |= (1 << SOUND_MIXER_PCM); 231 p |= (1 << SOUND_MIXER_VOLUME); 232 233 if (sc->route == RT_FRONT) { 234 if (sc->is_emu10k1) { 235 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 236 if (dspmix_k1[i].present) 237 p |= (1 << i); 238 if (dspmix_k1[i].recdev) 239 r |= (1 << i); 240 } 241 } else { 242 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 243 if (dspmix_k2[i].present) 244 p |= (1 << i); 245 if (dspmix_k2[i].recdev) 246 r |= (1 << i); 247 } 248 } 249 } 250 251 mix_setdevs(m, p); 252 mix_setrecdevs(m, r); 253 254 return (0); 255 } 256 257 static int 258 emu_dspmixer_uninit(struct snd_mixer *m) 259 { 260 struct emu_pcm_info *sc; 261 int err = 0; 262 263 /* drop submixer for AC97 codec */ 264 sc = mix_getdevinfo(m); 265 if (sc->sm != NULL) { 266 err = mixer_delete(sc->sm); 267 if (err) 268 return (err); 269 sc->sm = NULL; 270 } 271 return (0); 272 } 273 274 static int 275 emu_dspmixer_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right) 276 { 277 struct emu_pcm_info *sc; 278 279 sc = mix_getdevinfo(m); 280 281 switch (dev) { 282 case SOUND_MIXER_VOLUME: 283 switch (sc->route) { 284 case RT_FRONT: 285 if (sc->sm != NULL) 286 mix_set(sc->sm, dev, left, right); 287 if (sc->mch_disabled) { 288 /* In emu10k1 case PCM volume does not affect 289 sound routed to rear & center/sub (it is connected 290 to AC97 codec). Calculate it manually. */ 291 /* This really should belong to emu10kx.c */ 292 if (sc->is_emu10k1) { 293 sc->emu10k1_volcache[0][0] = left; 294 left = left * sc->emu10k1_volcache[1][0] / 100; 295 sc->emu10k1_volcache[0][1] = right; 296 right = right * sc->emu10k1_volcache[1][1] / 100; 297 } 298 299 emumix_set_volume(sc->card, M_MASTER_REAR_L, left); 300 emumix_set_volume(sc->card, M_MASTER_REAR_R, right); 301 if (!sc->is_emu10k1) { 302 emumix_set_volume(sc->card, M_MASTER_CENTER, (left+right)/2); 303 emumix_set_volume(sc->card, M_MASTER_SUBWOOFER, (left+right)/2); 304 /* XXX side */ 305 } 306 } /* mch disabled */ 307 break; 308 case RT_REAR: 309 emumix_set_volume(sc->card, M_MASTER_REAR_L, left); 310 emumix_set_volume(sc->card, M_MASTER_REAR_R, right); 311 break; 312 case RT_CENTER: 313 emumix_set_volume(sc->card, M_MASTER_CENTER, (left+right)/2); 314 break; 315 case RT_SUB: 316 emumix_set_volume(sc->card, M_MASTER_SUBWOOFER, (left+right)/2); 317 break; 318 } 319 break; 320 case SOUND_MIXER_PCM: 321 switch (sc->route) { 322 case RT_FRONT: 323 if (sc->sm != NULL) 324 mix_set(sc->sm, dev, left, right); 325 if (sc->mch_disabled) { 326 /* See SOUND_MIXER_VOLUME case */ 327 if (sc->is_emu10k1) { 328 sc->emu10k1_volcache[1][0] = left; 329 left = left * sc->emu10k1_volcache[0][0] / 100; 330 sc->emu10k1_volcache[1][1] = right; 331 right = right * sc->emu10k1_volcache[0][1] / 100; 332 } 333 emumix_set_volume(sc->card, M_MASTER_REAR_L, left); 334 emumix_set_volume(sc->card, M_MASTER_REAR_R, right); 335 336 if (!sc->is_emu10k1) { 337 emumix_set_volume(sc->card, M_MASTER_CENTER, (left+right)/2); 338 emumix_set_volume(sc->card, M_MASTER_SUBWOOFER, (left+right)/2); 339 /* XXX side */ 340 } 341 } /* mch_disabled */ 342 break; 343 case RT_REAR: 344 emumix_set_volume(sc->card, M_FX2_REAR_L, left); 345 emumix_set_volume(sc->card, M_FX3_REAR_R, right); 346 break; 347 case RT_CENTER: 348 emumix_set_volume(sc->card, M_FX4_CENTER, (left+right)/2); 349 break; 350 case RT_SUB: 351 emumix_set_volume(sc->card, M_FX5_SUBWOOFER, (left+right)/2); 352 break; 353 } 354 break; 355 case SOUND_MIXER_DIGITAL1: /* CD SPDIF, in1 */ 356 emumix_set_volume(sc->card, M_IN1_FRONT_L, left); 357 emumix_set_volume(sc->card, M_IN1_FRONT_R, right); 358 break; 359 case SOUND_MIXER_DIGITAL2: 360 if (sc->is_emu10k1) { 361 /* TOSLink, in3 */ 362 emumix_set_volume(sc->card, M_IN3_FRONT_L, left); 363 emumix_set_volume(sc->card, M_IN3_FRONT_R, right); 364 } else { 365 /* COAX SPDIF, in2 */ 366 emumix_set_volume(sc->card, M_IN2_FRONT_L, left); 367 emumix_set_volume(sc->card, M_IN2_FRONT_R, right); 368 } 369 break; 370 case SOUND_MIXER_LINE2: /* Line-In2, in4 */ 371 emumix_set_volume(sc->card, M_IN4_FRONT_L, left); 372 emumix_set_volume(sc->card, M_IN4_FRONT_R, right); 373 break; 374 case SOUND_MIXER_DIGITAL3: /* on-card SPDIF, in5 */ 375 emumix_set_volume(sc->card, M_IN5_FRONT_L, left); 376 emumix_set_volume(sc->card, M_IN5_FRONT_R, right); 377 break; 378 case SOUND_MIXER_LINE3: /* AUX2, in6 */ 379 emumix_set_volume(sc->card, M_IN6_FRONT_L, left); 380 emumix_set_volume(sc->card, M_IN6_FRONT_R, right); 381 break; 382 default: 383 if (sc->sm != NULL) { 384 /* XXX emumix_set_volume is not required here */ 385 emumix_set_volume(sc->card, M_IN0_FRONT_L, 100); 386 emumix_set_volume(sc->card, M_IN0_FRONT_R, 100); 387 mix_set(sc->sm, dev, left, right); 388 } else 389 device_printf(sc->dev, "mixer error: unknown device %d\n", dev); 390 } 391 return (0); 392 } 393 394 static u_int32_t 395 emu_dspmixer_setrecsrc(struct snd_mixer *m, u_int32_t src) 396 { 397 struct emu_pcm_info *sc; 398 int i; 399 u_int32_t recmask; 400 int input[8]; 401 402 sc = mix_getdevinfo(m); 403 recmask = 0; 404 for (i=0; i < 8; i++) 405 input[i]=0; 406 407 if (sc->sm != NULL) 408 if ((src & sc->ac97_recdevs) !=0) 409 if (mix_setrecsrc(sc->sm, src & sc->ac97_recdevs) == 0) { 410 recmask |= (src & sc->ac97_recdevs); 411 /* Recording from AC97 codec. 412 Enable AC97 route to rec on DSP */ 413 input[0] = 1; 414 } 415 if (sc->is_emu10k1) { 416 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 417 if (dspmix_k1[i].recdev) 418 if ((src & (1 << i)) == ((uint32_t)1 << i)) { 419 recmask |= (1 << i); 420 /* enable device i */ 421 input[dspmix_k1[i].input] = 1; 422 } 423 } 424 } else { 425 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 426 if (dspmix_k2[i].recdev) 427 if ((src & (1 << i)) == ((uint32_t)1 << i)) { 428 recmask |= (1 << i); 429 /* enable device i */ 430 input[dspmix_k2[i].input] = 1; 431 } 432 } 433 } 434 emumix_set_volume(sc->card, M_IN0_REC_L, input[0] == 1 ? 100 : 0); 435 emumix_set_volume(sc->card, M_IN0_REC_R, input[0] == 1 ? 100 : 0); 436 437 emumix_set_volume(sc->card, M_IN1_REC_L, input[1] == 1 ? 100 : 0); 438 emumix_set_volume(sc->card, M_IN1_REC_R, input[1] == 1 ? 100 : 0); 439 440 if (!sc->is_emu10k1) { 441 emumix_set_volume(sc->card, M_IN2_REC_L, input[2] == 1 ? 100 : 0); 442 emumix_set_volume(sc->card, M_IN2_REC_R, input[2] == 1 ? 100 : 0); 443 } 444 445 if (sc->is_emu10k1) { 446 emumix_set_volume(sc->card, M_IN3_REC_L, input[3] == 1 ? 100 : 0); 447 emumix_set_volume(sc->card, M_IN3_REC_R, input[3] == 1 ? 100 : 0); 448 } 449 450 emumix_set_volume(sc->card, M_IN4_REC_L, input[4] == 1 ? 100 : 0); 451 emumix_set_volume(sc->card, M_IN4_REC_R, input[4] == 1 ? 100 : 0); 452 453 emumix_set_volume(sc->card, M_IN5_REC_L, input[5] == 1 ? 100 : 0); 454 emumix_set_volume(sc->card, M_IN5_REC_R, input[5] == 1 ? 100 : 0); 455 456 emumix_set_volume(sc->card, M_IN6_REC_L, input[6] == 1 ? 100 : 0); 457 emumix_set_volume(sc->card, M_IN6_REC_R, input[6] == 1 ? 100 : 0); 458 459 /* XXX check for K1/k2 differences? */ 460 if ((src & (1 << SOUND_MIXER_PCM)) == (1 << SOUND_MIXER_PCM)) { 461 emumix_set_volume(sc->card, M_FX0_REC_L, emumix_get_volume(sc->card, M_FX0_FRONT_L)); 462 emumix_set_volume(sc->card, M_FX1_REC_R, emumix_get_volume(sc->card, M_FX1_FRONT_R)); 463 } else { 464 emumix_set_volume(sc->card, M_FX0_REC_L, 0); 465 emumix_set_volume(sc->card, M_FX1_REC_R, 0); 466 } 467 468 return (recmask); 469 } 470 471 static kobj_method_t emudspmixer_methods[] = { 472 KOBJMETHOD(mixer_init, emu_dspmixer_init), 473 KOBJMETHOD(mixer_uninit, emu_dspmixer_uninit), 474 KOBJMETHOD(mixer_set, emu_dspmixer_set), 475 KOBJMETHOD(mixer_setrecsrc, emu_dspmixer_setrecsrc), 476 KOBJMETHOD_END 477 }; 478 MIXER_DECLARE(emudspmixer); 479 480 static int 481 emu_efxmixer_init(struct snd_mixer *m) 482 { 483 mix_setdevs(m, SOUND_MASK_VOLUME); 484 mix_setrecdevs(m, SOUND_MASK_MONITOR); 485 return (0); 486 } 487 488 static int 489 emu_efxmixer_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right) 490 { 491 if (left + right == 200) return (0); 492 return (0); 493 } 494 495 static u_int32_t 496 emu_efxmixer_setrecsrc(struct snd_mixer *m __unused, u_int32_t src __unused) 497 { 498 return (SOUND_MASK_MONITOR); 499 } 500 501 static kobj_method_t emuefxmixer_methods[] = { 502 KOBJMETHOD(mixer_init, emu_efxmixer_init), 503 KOBJMETHOD(mixer_set, emu_efxmixer_set), 504 KOBJMETHOD(mixer_setrecsrc, emu_efxmixer_setrecsrc), 505 KOBJMETHOD_END 506 }; 507 MIXER_DECLARE(emuefxmixer); 508 509 /* 510 * AC97 emulation code for Audigy and later cards. 511 * Some parts of AC97 codec are not used by hardware, but can be used 512 * to change some DSP controls via AC97 mixer interface. This includes: 513 * - master volume controls MASTER_FRONT_[R|L] 514 * - pcm volume controls FX[0|1]_FRONT_[R|L] 515 * - rec volume controls MASTER_REC_[R|L] 516 * We do it because we need to put it under user control.... 517 * We also keep some parts of AC97 disabled to get better sound quality 518 */ 519 520 #define AC97LEFT(x) ((x & 0x7F00)>>8) 521 #define AC97RIGHT(x) (x & 0x007F) 522 #define AC97MUTE(x) ((x & 0x8000)>>15) 523 #define BIT4_TO100(x) (100-(x)*100/(0x0f)) 524 #define BIT6_TO100(x) (100-(x)*100/(0x3f)) 525 #define BIT4_TO255(x) (255-(x)*255/(0x0f)) 526 #define BIT6_TO255(x) (255-(x)*255/(0x3f)) 527 #define V100_TOBIT6(x) (0x3f*(100-x)/100) 528 #define V100_TOBIT4(x) (0x0f*(100-x)/100) 529 #define AC97ENCODE(x_muted, x_left, x_right) (((x_muted & 1)<<15) | ((x_left & 0x3f)<<8) | (x_right & 0x3f)) 530 531 static int 532 emu_ac97_read_emulation(struct emu_pcm_info *sc, int regno) 533 { 534 int use_ac97; 535 int emulated; 536 int tmp; 537 538 use_ac97 = 1; 539 emulated = 0; 540 541 switch (regno) { 542 case AC97_MIX_MASTER: 543 emulated = sc->ac97_state[AC97_MIX_MASTER]; 544 use_ac97 = 0; 545 break; 546 case AC97_MIX_PCM: 547 emulated = sc->ac97_state[AC97_MIX_PCM]; 548 use_ac97 = 0; 549 break; 550 case AC97_REG_RECSEL: 551 emulated = 0x0505; 552 use_ac97 = 0; 553 break; 554 case AC97_MIX_RGAIN: 555 emulated = sc->ac97_state[AC97_MIX_RGAIN]; 556 use_ac97 = 0; 557 break; 558 } 559 560 emu_wr(sc->card, EMU_AC97ADDR, regno, 1); 561 tmp = emu_rd(sc->card, EMU_AC97DATA, 2); 562 563 if (use_ac97) 564 emulated = tmp; 565 566 return (emulated); 567 } 568 569 static void 570 emu_ac97_write_emulation(struct emu_pcm_info *sc, int regno, uint32_t data) 571 { 572 int write_ac97; 573 int left, right; 574 uint32_t emu_left, emu_right; 575 int is_mute; 576 577 write_ac97 = 1; 578 579 left = AC97LEFT(data); 580 emu_left = BIT6_TO100(left); /* We show us as 6-bit AC97 mixer */ 581 right = AC97RIGHT(data); 582 emu_right = BIT6_TO100(right); 583 is_mute = AC97MUTE(data); 584 if (is_mute) 585 emu_left = emu_right = 0; 586 587 switch (regno) { 588 /* TODO: reset emulator on AC97_RESET */ 589 case AC97_MIX_MASTER: 590 emumix_set_volume(sc->card, M_MASTER_FRONT_L, emu_left); 591 emumix_set_volume(sc->card, M_MASTER_FRONT_R, emu_right); 592 sc->ac97_state[AC97_MIX_MASTER] = data & (0x8000 | 0x3f3f); 593 data = 0x8000; /* Mute AC97 main out */ 594 break; 595 case AC97_MIX_PCM: /* PCM OUT VOL */ 596 emumix_set_volume(sc->card, M_FX0_FRONT_L, emu_left); 597 emumix_set_volume(sc->card, M_FX1_FRONT_R, emu_right); 598 sc->ac97_state[AC97_MIX_PCM] = data & (0x8000 | 0x3f3f); 599 data = 0x8000; /* Mute AC97 PCM out */ 600 break; 601 case AC97_REG_RECSEL: 602 /* 603 * PCM recording source is set to "stereo mix" (labeled "vol" 604 * in mixer). There is no 'playback' from AC97 codec - 605 * if you want to hear anything from AC97 you have to _record_ 606 * it. Keep things simple and record "stereo mix". 607 */ 608 data = 0x0505; 609 break; 610 case AC97_MIX_RGAIN: /* RECORD GAIN */ 611 emu_left = BIT4_TO100(left); /* rgain is 4-bit */ 612 emu_right = BIT4_TO100(right); 613 emumix_set_volume(sc->card, M_MASTER_REC_L, 100-emu_left); 614 emumix_set_volume(sc->card, M_MASTER_REC_R, 100-emu_right); 615 /* 616 * Record gain on AC97 should stay zero to get AC97 sound on 617 * AC97_[RL] connectors on EMU10K2 chip. AC97 on Audigy is not 618 * directly connected to any output, only to EMU10K2 chip Use 619 * this control to set AC97 mix volume inside EMU10K2 chip 620 */ 621 sc->ac97_state[AC97_MIX_RGAIN] = data & (0x8000 | 0x0f0f); 622 data = 0x0000; 623 break; 624 } 625 if (write_ac97) { 626 emu_wr(sc->card, EMU_AC97ADDR, regno, 1); 627 emu_wr(sc->card, EMU_AC97DATA, data, 2); 628 } 629 } 630 631 static int 632 emu_erdcd(kobj_t obj __unused, void *devinfo, int regno) 633 { 634 struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo; 635 636 return (emu_ac97_read_emulation(sc, regno)); 637 } 638 639 static int 640 emu_ewrcd(kobj_t obj __unused, void *devinfo, int regno, uint32_t data) 641 { 642 struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo; 643 644 emu_ac97_write_emulation(sc, regno, data); 645 return (0); 646 } 647 648 static kobj_method_t emu_eac97_methods[] = { 649 KOBJMETHOD(ac97_read, emu_erdcd), 650 KOBJMETHOD(ac97_write, emu_ewrcd), 651 KOBJMETHOD_END 652 }; 653 AC97_DECLARE(emu_eac97); 654 655 /* real ac97 codec */ 656 static int 657 emu_rdcd(kobj_t obj __unused, void *devinfo, int regno) 658 { 659 int rd; 660 struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo; 661 662 KASSERT(sc->card != NULL, ("emu_rdcd: no soundcard")); 663 emu_wr(sc->card, EMU_AC97ADDR, regno, 1); 664 rd = emu_rd(sc->card, EMU_AC97DATA, 2); 665 return (rd); 666 } 667 668 static int 669 emu_wrcd(kobj_t obj __unused, void *devinfo, int regno, uint32_t data) 670 { 671 struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo; 672 673 KASSERT(sc->card != NULL, ("emu_wrcd: no soundcard")); 674 emu_wr(sc->card, EMU_AC97ADDR, regno, 1); 675 emu_wr(sc->card, EMU_AC97DATA, data, 2); 676 return (0); 677 } 678 679 static kobj_method_t emu_ac97_methods[] = { 680 KOBJMETHOD(ac97_read, emu_rdcd), 681 KOBJMETHOD(ac97_write, emu_wrcd), 682 KOBJMETHOD_END 683 }; 684 AC97_DECLARE(emu_ac97); 685 686 static int 687 emu_k1_recval(int speed) 688 { 689 int val; 690 691 val = 0; 692 while ((val < 7) && (speed < emu10k1_adcspeed[val])) 693 val++; 694 return (val); 695 } 696 697 static int 698 emu_k2_recval(int speed) 699 { 700 int val; 701 702 val = 0; 703 while ((val < 8) && (speed < emu10k2_adcspeed[val])) 704 val++; 705 return (val); 706 } 707 708 static void * 709 emupchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused) 710 { 711 struct emu_pcm_info *sc = devinfo; 712 struct emu_pcm_pchinfo *ch; 713 void *r; 714 715 KASSERT(dir == PCMDIR_PLAY, ("emupchan_init: bad direction")); 716 KASSERT(sc->card != NULL, ("empchan_init: no soundcard")); 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 static uint32_t 1230 emu_pcm_intr(void *pcm, uint32_t stat) 1231 { 1232 struct emu_pcm_info *sc = (struct emu_pcm_info *)pcm; 1233 uint32_t ack; 1234 int i; 1235 1236 ack = 0; 1237 1238 snd_mtxlock(sc->lock); 1239 1240 if (stat & EMU_IPR_INTERVALTIMER) { 1241 ack |= EMU_IPR_INTERVALTIMER; 1242 for (i = 0; i < MAX_CHANNELS; i++) 1243 if (sc->pch[i].channel) { 1244 if (sc->pch[i].run == 1) { 1245 snd_mtxunlock(sc->lock); 1246 chn_intr(sc->pch[i].channel); 1247 snd_mtxlock(sc->lock); 1248 } else 1249 emu_timer_enable(sc->card, sc->pch[i].timer, 0); 1250 } 1251 /* ADC may install timer to get low-latency interrupts */ 1252 if ((sc->rch_adc.channel) && (sc->rch_adc.run)) { 1253 snd_mtxunlock(sc->lock); 1254 chn_intr(sc->rch_adc.channel); 1255 snd_mtxlock(sc->lock); 1256 } 1257 /* 1258 * EFX does not use timer, because it will fill 1259 * buffer at least 32x times faster than ADC. 1260 */ 1261 } 1262 1263 if (stat & (EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL)) { 1264 ack |= stat & (EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL); 1265 if (sc->rch_adc.channel) { 1266 snd_mtxunlock(sc->lock); 1267 chn_intr(sc->rch_adc.channel); 1268 snd_mtxlock(sc->lock); 1269 } 1270 } 1271 1272 if (stat & (EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL)) { 1273 ack |= stat & (EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL); 1274 if (sc->rch_efx.channel) { 1275 snd_mtxunlock(sc->lock); 1276 chn_intr(sc->rch_efx.channel); 1277 snd_mtxlock(sc->lock); 1278 } 1279 } 1280 snd_mtxunlock(sc->lock); 1281 1282 return (ack); 1283 } 1284 1285 static int 1286 emu_pcm_init(struct emu_pcm_info *sc) 1287 { 1288 sc->bufsz = pcm_getbuffersize(sc->dev, EMUPAGESIZE, EMU_REC_BUFSZ, EMU_MAX_BUFSZ); 1289 return (0); 1290 } 1291 1292 static int 1293 emu_pcm_uninit(struct emu_pcm_info *sc __unused) 1294 { 1295 return (0); 1296 } 1297 1298 static int 1299 emu_pcm_probe(device_t dev) 1300 { 1301 uintptr_t func, route, r; 1302 const char *rt; 1303 char buffer[255]; 1304 1305 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_FUNC, &func); 1306 1307 if (func != SCF_PCM) 1308 return (ENXIO); 1309 1310 rt = "UNKNOWN"; 1311 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ROUTE, &route); 1312 switch (route) { 1313 case RT_FRONT: 1314 rt = "front"; 1315 break; 1316 case RT_REAR: 1317 rt = "rear"; 1318 break; 1319 case RT_CENTER: 1320 rt = "center"; 1321 break; 1322 case RT_SUB: 1323 rt = "subwoofer"; 1324 break; 1325 case RT_SIDE: 1326 rt = "side"; 1327 break; 1328 case RT_MCHRECORD: 1329 rt = "multichannel recording"; 1330 break; 1331 } 1332 1333 snprintf(buffer, 255, "EMU10Kx DSP %s PCM interface", rt); 1334 device_set_desc_copy(dev, buffer); 1335 return (0); 1336 } 1337 1338 static int 1339 emu_pcm_attach(device_t dev) 1340 { 1341 struct emu_pcm_info *sc; 1342 unsigned int i; 1343 char status[SND_STATUSLEN]; 1344 uint32_t inte, ipr; 1345 uintptr_t route, r, ivar; 1346 1347 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO); 1348 sc->card = (struct emu_sc_info *)(device_get_softc(device_get_parent(dev))); 1349 if (sc->card == NULL) { 1350 device_printf(dev, "cannot get bridge conf\n"); 1351 free(sc, M_DEVBUF); 1352 return (ENXIO); 1353 } 1354 1355 sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_emu10kx pcm softc"); 1356 sc->dev = dev; 1357 1358 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ISEMU10K1, &ivar); 1359 sc->is_emu10k1 = ivar ? 1 : 0; 1360 1361 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_MCH_DISABLED, &ivar); 1362 sc->mch_disabled = ivar ? 1 : 0; 1363 1364 sc->codec = NULL; 1365 1366 for (i = 0; i < 8; i++) { 1367 sc->rt.routing_left[i] = i; 1368 sc->rt.amounts_left[i] = 0x00; 1369 sc->rt.routing_right[i] = i; 1370 sc->rt.amounts_right[i] = 0x00; 1371 } 1372 1373 for (i = 0; i < 8; i++) { 1374 sc->rt_mono.routing_left[i] = i; 1375 sc->rt_mono.amounts_left[i] = 0x00; 1376 sc->rt_mono.routing_right[i] = i; 1377 sc->rt_mono.amounts_right[i] = 0x00; 1378 } 1379 1380 sc->emu10k1_volcache[0][0] = 75; 1381 sc->emu10k1_volcache[1][0] = 75; 1382 sc->emu10k1_volcache[0][1] = 75; 1383 sc->emu10k1_volcache[1][1] = 75; 1384 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ROUTE, &route); 1385 sc->route = route; 1386 switch (route) { 1387 case RT_FRONT: 1388 sc->rt.amounts_left[0] = 0xff; 1389 sc->rt.amounts_right[1] = 0xff; 1390 sc->rt_mono.amounts_left[0] = 0xff; 1391 sc->rt_mono.amounts_left[1] = 0xff; 1392 if (sc->is_emu10k1) 1393 sc->codec = AC97_CREATE(dev, sc, emu_ac97); 1394 else 1395 sc->codec = AC97_CREATE(dev, sc, emu_eac97); 1396 sc->ac97_mixerclass = NULL; 1397 if (sc->codec != NULL) 1398 sc->ac97_mixerclass = ac97_getmixerclass(); 1399 if (mixer_init(dev, &emudspmixer_class, sc)) { 1400 device_printf(dev, "failed to initialize DSP mixer\n"); 1401 goto bad; 1402 } 1403 break; 1404 case RT_REAR: 1405 sc->rt.amounts_left[2] = 0xff; 1406 sc->rt.amounts_right[3] = 0xff; 1407 sc->rt_mono.amounts_left[2] = 0xff; 1408 sc->rt_mono.amounts_left[3] = 0xff; 1409 if (mixer_init(dev, &emudspmixer_class, sc)) { 1410 device_printf(dev, "failed to initialize mixer\n"); 1411 goto bad; 1412 } 1413 break; 1414 case RT_CENTER: 1415 sc->rt.amounts_left[4] = 0xff; 1416 sc->rt_mono.amounts_left[4] = 0xff; 1417 if (mixer_init(dev, &emudspmixer_class, sc)) { 1418 device_printf(dev, "failed to initialize mixer\n"); 1419 goto bad; 1420 } 1421 break; 1422 case RT_SUB: 1423 sc->rt.amounts_left[5] = 0xff; 1424 sc->rt_mono.amounts_left[5] = 0xff; 1425 if (mixer_init(dev, &emudspmixer_class, sc)) { 1426 device_printf(dev, "failed to initialize mixer\n"); 1427 goto bad; 1428 } 1429 break; 1430 case RT_SIDE: 1431 sc->rt.amounts_left[6] = 0xff; 1432 sc->rt.amounts_right[7] = 0xff; 1433 sc->rt_mono.amounts_left[6] = 0xff; 1434 sc->rt_mono.amounts_left[7] = 0xff; 1435 if (mixer_init(dev, &emudspmixer_class, sc)) { 1436 device_printf(dev, "failed to initialize mixer\n"); 1437 goto bad; 1438 } 1439 break; 1440 case RT_MCHRECORD: 1441 if (mixer_init(dev, &emuefxmixer_class, sc)) { 1442 device_printf(dev, "failed to initialize EFX mixer\n"); 1443 goto bad; 1444 } 1445 break; 1446 default: 1447 device_printf(dev, "invalid default route\n"); 1448 goto bad; 1449 } 1450 1451 inte = EMU_INTE_INTERTIMERENB; 1452 ipr = EMU_IPR_INTERVALTIMER; /* Used by playback & ADC */ 1453 sc->ihandle = emu_intr_register(sc->card, inte, ipr, &emu_pcm_intr, sc); 1454 1455 if (emu_pcm_init(sc) == -1) { 1456 device_printf(dev, "unable to initialize PCM part of the card\n"); 1457 goto bad; 1458 } 1459 1460 /* 1461 * We don't register interrupt handler with snd_setup_intr 1462 * in pcm device. Mark pcm device as MPSAFE manually. 1463 */ 1464 pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE); 1465 1466 /* XXX we should better get number of available channels from parent */ 1467 if (pcm_register(dev, sc, (route == RT_FRONT) ? MAX_CHANNELS : 1, (route == RT_FRONT) ? 1 : 0)) { 1468 device_printf(dev, "can't register PCM channels!\n"); 1469 goto bad; 1470 } 1471 sc->pnum = 0; 1472 if (route != RT_MCHRECORD) 1473 pcm_addchan(dev, PCMDIR_PLAY, &emupchan_class, sc); 1474 if (route == RT_FRONT) { 1475 for (i = 1; i < MAX_CHANNELS; i++) 1476 pcm_addchan(dev, PCMDIR_PLAY, &emupchan_class, sc); 1477 pcm_addchan(dev, PCMDIR_REC, &emurchan_class, sc); 1478 } 1479 if (route == RT_MCHRECORD) 1480 pcm_addchan(dev, PCMDIR_REC, &emufxrchan_class, sc); 1481 1482 snprintf(status, SND_STATUSLEN, "on %s", device_get_nameunit(device_get_parent(dev))); 1483 pcm_setstatus(dev, status); 1484 1485 return (0); 1486 1487 bad: 1488 if (sc->codec) 1489 ac97_destroy(sc->codec); 1490 if (sc->lock) 1491 snd_mtxfree(sc->lock); 1492 free(sc, M_DEVBUF); 1493 return (ENXIO); 1494 } 1495 1496 static int 1497 emu_pcm_detach(device_t dev) 1498 { 1499 int r; 1500 struct emu_pcm_info *sc; 1501 1502 sc = pcm_getdevinfo(dev); 1503 1504 r = pcm_unregister(dev); 1505 1506 if (r) return (r); 1507 1508 emu_pcm_uninit(sc); 1509 1510 if (sc->lock) 1511 snd_mtxfree(sc->lock); 1512 free(sc, M_DEVBUF); 1513 1514 return (0); 1515 } 1516 1517 static device_method_t emu_pcm_methods[] = { 1518 DEVMETHOD(device_probe, emu_pcm_probe), 1519 DEVMETHOD(device_attach, emu_pcm_attach), 1520 DEVMETHOD(device_detach, emu_pcm_detach), 1521 1522 DEVMETHOD_END 1523 }; 1524 1525 static driver_t emu_pcm_driver = { 1526 "pcm", 1527 emu_pcm_methods, 1528 PCM_SOFTC_SIZE, 1529 NULL, 1530 0, 1531 NULL 1532 }; 1533 DRIVER_MODULE(snd_emu10kx_pcm, emu10kx, emu_pcm_driver, pcm_devclass, 0, 0); 1534 MODULE_DEPEND(snd_emu10kx_pcm, snd_emu10kx, SND_EMU10KX_MINVER, SND_EMU10KX_PREFVER, SND_EMU10KX_MAXVER); 1535 MODULE_DEPEND(snd_emu10kx_pcm, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 1536 MODULE_VERSION(snd_emu10kx_pcm, SND_EMU10KX_PREFVER); 1537