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 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 687 static int 688 emu_k1_recval(int speed) 689 { 690 int val; 691 692 val = 0; 693 while ((val < 7) && (speed < emu10k1_adcspeed[val])) 694 val++; 695 return (val); 696 } 697 698 static int 699 emu_k2_recval(int speed) 700 { 701 int val; 702 703 val = 0; 704 while ((val < 8) && (speed < emu10k2_adcspeed[val])) 705 val++; 706 return (val); 707 } 708 709 static void * 710 emupchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused) 711 { 712 struct emu_pcm_info *sc = devinfo; 713 struct emu_pcm_pchinfo *ch; 714 void *r; 715 716 KASSERT(dir == PCMDIR_PLAY, ("emupchan_init: bad direction")); 717 KASSERT(sc->card != NULL, ("empchan_init: no soundcard")); 718 719 720 if (sc->pnum >= MAX_CHANNELS) 721 return (NULL); 722 ch = &(sc->pch[sc->pnum++]); 723 ch->buffer = b; 724 ch->pcm = sc; 725 ch->channel = c; 726 ch->blksz = sc->bufsz; 727 ch->fmt = SND_FORMAT(AFMT_U8, 1, 0); 728 ch->spd = 8000; 729 ch->master = emu_valloc(sc->card); 730 /* 731 * XXX we have to allocate slave even for mono channel until we 732 * fix emu_vfree to handle this case. 733 */ 734 ch->slave = emu_valloc(sc->card); 735 ch->timer = emu_timer_create(sc->card); 736 r = (emu_vinit(sc->card, ch->master, ch->slave, EMU_PLAY_BUFSZ, ch->buffer)) ? NULL : ch; 737 return (r); 738 } 739 740 static int 741 emupchan_free(kobj_t obj __unused, void *c_devinfo) 742 { 743 struct emu_pcm_pchinfo *ch = c_devinfo; 744 struct emu_pcm_info *sc = ch->pcm; 745 746 emu_timer_clear(sc->card, ch->timer); 747 if (ch->slave != NULL) 748 emu_vfree(sc->card, ch->slave); 749 emu_vfree(sc->card, ch->master); 750 return (0); 751 } 752 753 static int 754 emupchan_setformat(kobj_t obj __unused, void *c_devinfo, uint32_t format) 755 { 756 struct emu_pcm_pchinfo *ch = c_devinfo; 757 758 ch->fmt = format; 759 return (0); 760 } 761 762 static uint32_t 763 emupchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed) 764 { 765 struct emu_pcm_pchinfo *ch = c_devinfo; 766 767 ch->spd = speed; 768 return (ch->spd); 769 } 770 771 static uint32_t 772 emupchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize) 773 { 774 struct emu_pcm_pchinfo *ch = c_devinfo; 775 struct emu_pcm_info *sc = ch->pcm; 776 777 if (blocksize > ch->pcm->bufsz) 778 blocksize = ch->pcm->bufsz; 779 snd_mtxlock(sc->lock); 780 ch->blksz = blocksize; 781 emu_timer_set(sc->card, ch->timer, ch->blksz / sndbuf_getalign(ch->buffer)); 782 snd_mtxunlock(sc->lock); 783 return (ch->blksz); 784 } 785 786 static int 787 emupchan_trigger(kobj_t obj __unused, void *c_devinfo, int go) 788 { 789 struct emu_pcm_pchinfo *ch = c_devinfo; 790 struct emu_pcm_info *sc = ch->pcm; 791 792 if (!PCMTRIG_COMMON(go)) 793 return (0); 794 795 snd_mtxlock(sc->lock); /* XXX can we trigger on parallel threads ? */ 796 if (go == PCMTRIG_START) { 797 emu_vsetup(ch->master, ch->fmt, ch->spd); 798 if (AFMT_CHANNEL(ch->fmt) > 1) 799 emu_vroute(sc->card, &(sc->rt), ch->master); 800 else 801 emu_vroute(sc->card, &(sc->rt_mono), ch->master); 802 emu_vwrite(sc->card, ch->master); 803 emu_timer_set(sc->card, ch->timer, ch->blksz / sndbuf_getalign(ch->buffer)); 804 emu_timer_enable(sc->card, ch->timer, 1); 805 } 806 /* PCM interrupt handler will handle PCMTRIG_STOP event */ 807 ch->run = (go == PCMTRIG_START) ? 1 : 0; 808 emu_vtrigger(sc->card, ch->master, ch->run); 809 snd_mtxunlock(sc->lock); 810 return (0); 811 } 812 813 static uint32_t 814 emupchan_getptr(kobj_t obj __unused, void *c_devinfo) 815 { 816 struct emu_pcm_pchinfo *ch = c_devinfo; 817 struct emu_pcm_info *sc = ch->pcm; 818 int r; 819 820 r = emu_vpos(sc->card, ch->master); 821 822 return (r); 823 } 824 825 static struct pcmchan_caps * 826 emupchan_getcaps(kobj_t obj __unused, void *c_devinfo __unused) 827 { 828 struct emu_pcm_pchinfo *ch = c_devinfo; 829 struct emu_pcm_info *sc = ch->pcm; 830 831 switch (sc->route) { 832 case RT_FRONT: 833 /* FALLTHROUGH */ 834 case RT_REAR: 835 /* FALLTHROUGH */ 836 case RT_SIDE: 837 return (&emu_playcaps); 838 break; 839 case RT_CENTER: 840 /* FALLTHROUGH */ 841 case RT_SUB: 842 return (&emu_playcaps_mono); 843 break; 844 } 845 return (NULL); 846 } 847 848 static kobj_method_t emupchan_methods[] = { 849 KOBJMETHOD(channel_init, emupchan_init), 850 KOBJMETHOD(channel_free, emupchan_free), 851 KOBJMETHOD(channel_setformat, emupchan_setformat), 852 KOBJMETHOD(channel_setspeed, emupchan_setspeed), 853 KOBJMETHOD(channel_setblocksize, emupchan_setblocksize), 854 KOBJMETHOD(channel_trigger, emupchan_trigger), 855 KOBJMETHOD(channel_getptr, emupchan_getptr), 856 KOBJMETHOD(channel_getcaps, emupchan_getcaps), 857 KOBJMETHOD_END 858 }; 859 CHANNEL_DECLARE(emupchan); 860 861 static void * 862 emurchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused) 863 { 864 struct emu_pcm_info *sc = devinfo; 865 struct emu_pcm_rchinfo *ch; 866 867 KASSERT(dir == PCMDIR_REC, ("emurchan_init: bad direction")); 868 ch = &sc->rch_adc; 869 ch->buffer = b; 870 ch->pcm = sc; 871 ch->channel = c; 872 ch->blksz = sc->bufsz / 2; /* We rise interrupt for half-full buffer */ 873 ch->fmt = SND_FORMAT(AFMT_U8, 1, 0); 874 ch->spd = 8000; 875 ch->idxreg = sc->is_emu10k1 ? EMU_ADCIDX : EMU_A_ADCIDX; 876 ch->basereg = EMU_ADCBA; 877 ch->sizereg = EMU_ADCBS; 878 ch->setupreg = EMU_ADCCR; 879 ch->irqmask = EMU_INTE_ADCBUFENABLE; 880 ch->iprmask = EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL; 881 882 if (sndbuf_alloc(ch->buffer, emu_gettag(sc->card), 0, sc->bufsz) != 0) 883 return (NULL); 884 else { 885 ch->timer = emu_timer_create(sc->card); 886 emu_wrptr(sc->card, 0, ch->basereg, sndbuf_getbufaddr(ch->buffer)); 887 emu_wrptr(sc->card, 0, ch->sizereg, 0); /* off */ 888 return (ch); 889 } 890 } 891 892 static int 893 emurchan_free(kobj_t obj __unused, void *c_devinfo) 894 { 895 struct emu_pcm_rchinfo *ch = c_devinfo; 896 struct emu_pcm_info *sc = ch->pcm; 897 898 emu_timer_clear(sc->card, ch->timer); 899 return (0); 900 } 901 902 static int 903 emurchan_setformat(kobj_t obj __unused, void *c_devinfo, uint32_t format) 904 { 905 struct emu_pcm_rchinfo *ch = c_devinfo; 906 907 ch->fmt = format; 908 return (0); 909 } 910 911 static uint32_t 912 emurchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed) 913 { 914 struct emu_pcm_rchinfo *ch = c_devinfo; 915 916 if (ch->pcm->is_emu10k1) { 917 speed = emu10k1_adcspeed[emu_k1_recval(speed)]; 918 } else { 919 speed = emu10k2_adcspeed[emu_k2_recval(speed)]; 920 } 921 ch->spd = speed; 922 return (ch->spd); 923 } 924 925 static uint32_t 926 emurchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize) 927 { 928 struct emu_pcm_rchinfo *ch = c_devinfo; 929 struct emu_pcm_info *sc = ch->pcm; 930 931 ch->blksz = blocksize; 932 /* 933 * If blocksize is less than half of buffer size we will not get 934 * BUFHALFFULL interrupt in time and channel will need to generate 935 * (and use) timer interrupts. Otherwise channel will be marked dead. 936 */ 937 if (ch->blksz < (ch->pcm->bufsz / 2)) { 938 emu_timer_set(sc->card, ch->timer, ch->blksz / sndbuf_getalign(ch->buffer)); 939 emu_timer_enable(sc->card, ch->timer, 1); 940 } else { 941 emu_timer_enable(sc->card, ch->timer, 0); 942 } 943 return (ch->blksz); 944 } 945 946 static int 947 emurchan_trigger(kobj_t obj __unused, void *c_devinfo, int go) 948 { 949 struct emu_pcm_rchinfo *ch = c_devinfo; 950 struct emu_pcm_info *sc = ch->pcm; 951 uint32_t val, sz; 952 953 if (!PCMTRIG_COMMON(go)) 954 return (0); 955 956 switch (sc->bufsz) { 957 case 4096: 958 sz = EMU_RECBS_BUFSIZE_4096; 959 break; 960 case 8192: 961 sz = EMU_RECBS_BUFSIZE_8192; 962 break; 963 case 16384: 964 sz = EMU_RECBS_BUFSIZE_16384; 965 break; 966 case 32768: 967 sz = EMU_RECBS_BUFSIZE_32768; 968 break; 969 case 65536: 970 sz = EMU_RECBS_BUFSIZE_65536; 971 break; 972 default: 973 sz = EMU_RECBS_BUFSIZE_4096; 974 } 975 976 snd_mtxlock(sc->lock); 977 switch (go) { 978 case PCMTRIG_START: 979 ch->run = 1; 980 emu_wrptr(sc->card, 0, ch->sizereg, sz); 981 val = sc->is_emu10k1 ? EMU_ADCCR_LCHANENABLE : EMU_A_ADCCR_LCHANENABLE; 982 if (AFMT_CHANNEL(ch->fmt) > 1) 983 val |= sc->is_emu10k1 ? EMU_ADCCR_RCHANENABLE : EMU_A_ADCCR_RCHANENABLE; 984 val |= sc->is_emu10k1 ? emu_k1_recval(ch->spd) : emu_k2_recval(ch->spd); 985 emu_wrptr(sc->card, 0, ch->setupreg, 0); 986 emu_wrptr(sc->card, 0, ch->setupreg, val); 987 ch->ihandle = emu_intr_register(sc->card, ch->irqmask, ch->iprmask, &emu_pcm_intr, sc); 988 break; 989 case PCMTRIG_STOP: 990 /* FALLTHROUGH */ 991 case PCMTRIG_ABORT: 992 ch->run = 0; 993 emu_wrptr(sc->card, 0, ch->sizereg, 0); 994 if (ch->setupreg) 995 emu_wrptr(sc->card, 0, ch->setupreg, 0); 996 (void)emu_intr_unregister(sc->card, ch->ihandle); 997 break; 998 case PCMTRIG_EMLDMAWR: 999 /* FALLTHROUGH */ 1000 case PCMTRIG_EMLDMARD: 1001 /* FALLTHROUGH */ 1002 default: 1003 break; 1004 } 1005 snd_mtxunlock(sc->lock); 1006 1007 return (0); 1008 } 1009 1010 static uint32_t 1011 emurchan_getptr(kobj_t obj __unused, void *c_devinfo) 1012 { 1013 struct emu_pcm_rchinfo *ch = c_devinfo; 1014 struct emu_pcm_info *sc = ch->pcm; 1015 int r; 1016 1017 r = emu_rdptr(sc->card, 0, ch->idxreg) & 0x0000ffff; 1018 1019 return (r); 1020 } 1021 1022 static struct pcmchan_caps * 1023 emurchan_getcaps(kobj_t obj __unused, void *c_devinfo __unused) 1024 { 1025 return (&emu_reccaps_adc); 1026 } 1027 1028 static kobj_method_t emurchan_methods[] = { 1029 KOBJMETHOD(channel_init, emurchan_init), 1030 KOBJMETHOD(channel_free, emurchan_free), 1031 KOBJMETHOD(channel_setformat, emurchan_setformat), 1032 KOBJMETHOD(channel_setspeed, emurchan_setspeed), 1033 KOBJMETHOD(channel_setblocksize, emurchan_setblocksize), 1034 KOBJMETHOD(channel_trigger, emurchan_trigger), 1035 KOBJMETHOD(channel_getptr, emurchan_getptr), 1036 KOBJMETHOD(channel_getcaps, emurchan_getcaps), 1037 KOBJMETHOD_END 1038 }; 1039 CHANNEL_DECLARE(emurchan); 1040 1041 static void * 1042 emufxrchan_init(kobj_t obj __unused, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir __unused) 1043 { 1044 struct emu_pcm_info *sc = devinfo; 1045 struct emu_pcm_rchinfo *ch; 1046 1047 KASSERT(dir == PCMDIR_REC, ("emurchan_init: bad direction")); 1048 1049 if (sc == NULL) return (NULL); 1050 1051 ch = &(sc->rch_efx); 1052 ch->fmt = SND_FORMAT(AFMT_S16_LE, 1, 0); 1053 ch->spd = sc->is_emu10k1 ? 48000*32 : 48000 * 64; 1054 ch->idxreg = EMU_FXIDX; 1055 ch->basereg = EMU_FXBA; 1056 ch->sizereg = EMU_FXBS; 1057 ch->irqmask = EMU_INTE_EFXBUFENABLE; 1058 ch->iprmask = EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL; 1059 ch->buffer = b; 1060 ch->pcm = sc; 1061 ch->channel = c; 1062 ch->blksz = sc->bufsz / 2; 1063 1064 if (sndbuf_alloc(ch->buffer, emu_gettag(sc->card), 0, sc->bufsz) != 0) 1065 return (NULL); 1066 else { 1067 emu_wrptr(sc->card, 0, ch->basereg, sndbuf_getbufaddr(ch->buffer)); 1068 emu_wrptr(sc->card, 0, ch->sizereg, 0); /* off */ 1069 return (ch); 1070 } 1071 } 1072 1073 static int 1074 emufxrchan_setformat(kobj_t obj __unused, void *c_devinfo __unused, uint32_t format) 1075 { 1076 if (format == SND_FORMAT(AFMT_S16_LE, 1, 0)) return (0); 1077 return (EINVAL); 1078 } 1079 1080 static uint32_t 1081 emufxrchan_setspeed(kobj_t obj __unused, void *c_devinfo, uint32_t speed) 1082 { 1083 struct emu_pcm_rchinfo *ch = c_devinfo; 1084 1085 /* FIXED RATE CHANNEL */ 1086 return (ch->spd); 1087 } 1088 1089 static uint32_t 1090 emufxrchan_setblocksize(kobj_t obj __unused, void *c_devinfo, uint32_t blocksize) 1091 { 1092 struct emu_pcm_rchinfo *ch = c_devinfo; 1093 1094 ch->blksz = blocksize; 1095 /* 1096 * XXX If blocksize is less than half of buffer size we will not get 1097 * interrupt in time and channel will die due to interrupt timeout. 1098 * This should not happen with FX rchan, because it will fill buffer 1099 * very fast (64K buffer is 0.021seconds on Audigy). 1100 */ 1101 if (ch->blksz < (ch->pcm->bufsz / 2)) 1102 ch->blksz = ch->pcm->bufsz / 2; 1103 return (ch->blksz); 1104 } 1105 1106 static int 1107 emufxrchan_trigger(kobj_t obj __unused, void *c_devinfo, int go) 1108 { 1109 struct emu_pcm_rchinfo *ch = c_devinfo; 1110 struct emu_pcm_info *sc = ch->pcm; 1111 uint32_t sz; 1112 1113 if (!PCMTRIG_COMMON(go)) 1114 return (0); 1115 1116 switch (sc->bufsz) { 1117 case 4096: 1118 sz = EMU_RECBS_BUFSIZE_4096; 1119 break; 1120 case 8192: 1121 sz = EMU_RECBS_BUFSIZE_8192; 1122 break; 1123 case 16384: 1124 sz = EMU_RECBS_BUFSIZE_16384; 1125 break; 1126 case 32768: 1127 sz = EMU_RECBS_BUFSIZE_32768; 1128 break; 1129 case 65536: 1130 sz = EMU_RECBS_BUFSIZE_65536; 1131 break; 1132 default: 1133 sz = EMU_RECBS_BUFSIZE_4096; 1134 } 1135 1136 snd_mtxlock(sc->lock); 1137 switch (go) { 1138 case PCMTRIG_START: 1139 ch->run = 1; 1140 emu_wrptr(sc->card, 0, ch->sizereg, sz); 1141 ch->ihandle = emu_intr_register(sc->card, ch->irqmask, ch->iprmask, &emu_pcm_intr, sc); 1142 /* 1143 * SB Live! is limited to 32 mono channels. Audigy 1144 * has 64 mono channels. Channels are enabled 1145 * by setting a bit in EMU_A_FXWC[1|2] registers. 1146 */ 1147 /* XXX there is no way to demultiplex this streams for now */ 1148 if (sc->is_emu10k1) { 1149 emu_wrptr(sc->card, 0, EMU_FXWC, 0xffffffff); 1150 } else { 1151 emu_wrptr(sc->card, 0, EMU_A_FXWC1, 0xffffffff); 1152 emu_wrptr(sc->card, 0, EMU_A_FXWC2, 0xffffffff); 1153 } 1154 break; 1155 case PCMTRIG_STOP: 1156 /* FALLTHROUGH */ 1157 case PCMTRIG_ABORT: 1158 ch->run = 0; 1159 if (sc->is_emu10k1) { 1160 emu_wrptr(sc->card, 0, EMU_FXWC, 0x0); 1161 } else { 1162 emu_wrptr(sc->card, 0, EMU_A_FXWC1, 0x0); 1163 emu_wrptr(sc->card, 0, EMU_A_FXWC2, 0x0); 1164 } 1165 emu_wrptr(sc->card, 0, ch->sizereg, 0); 1166 (void)emu_intr_unregister(sc->card, ch->ihandle); 1167 break; 1168 case PCMTRIG_EMLDMAWR: 1169 /* FALLTHROUGH */ 1170 case PCMTRIG_EMLDMARD: 1171 /* FALLTHROUGH */ 1172 default: 1173 break; 1174 } 1175 snd_mtxunlock(sc->lock); 1176 1177 return (0); 1178 } 1179 1180 static uint32_t 1181 emufxrchan_getptr(kobj_t obj __unused, void *c_devinfo) 1182 { 1183 struct emu_pcm_rchinfo *ch = c_devinfo; 1184 struct emu_pcm_info *sc = ch->pcm; 1185 int r; 1186 1187 r = emu_rdptr(sc->card, 0, ch->idxreg) & 0x0000ffff; 1188 1189 return (r); 1190 } 1191 1192 static struct pcmchan_caps * 1193 emufxrchan_getcaps(kobj_t obj __unused, void *c_devinfo) 1194 { 1195 struct emu_pcm_rchinfo *ch = c_devinfo; 1196 struct emu_pcm_info *sc = ch->pcm; 1197 1198 if (sc->is_emu10k1) 1199 return (&emu_reccaps_efx_live); 1200 return (&emu_reccaps_efx_audigy); 1201 1202 } 1203 1204 static int 1205 emufxrchan_getrates(kobj_t obj __unused, void *c_devinfo, int **rates) 1206 { 1207 struct emu_pcm_rchinfo *ch = c_devinfo; 1208 struct emu_pcm_info *sc = ch->pcm; 1209 1210 if (sc->is_emu10k1) 1211 *rates = emu_rates_live; 1212 else 1213 *rates = emu_rates_audigy; 1214 1215 return 1; 1216 } 1217 1218 static kobj_method_t emufxrchan_methods[] = { 1219 KOBJMETHOD(channel_init, emufxrchan_init), 1220 KOBJMETHOD(channel_setformat, emufxrchan_setformat), 1221 KOBJMETHOD(channel_setspeed, emufxrchan_setspeed), 1222 KOBJMETHOD(channel_setblocksize, emufxrchan_setblocksize), 1223 KOBJMETHOD(channel_trigger, emufxrchan_trigger), 1224 KOBJMETHOD(channel_getptr, emufxrchan_getptr), 1225 KOBJMETHOD(channel_getcaps, emufxrchan_getcaps), 1226 KOBJMETHOD(channel_getrates, emufxrchan_getrates), 1227 KOBJMETHOD_END 1228 }; 1229 CHANNEL_DECLARE(emufxrchan); 1230 1231 1232 static uint32_t 1233 emu_pcm_intr(void *pcm, uint32_t stat) 1234 { 1235 struct emu_pcm_info *sc = (struct emu_pcm_info *)pcm; 1236 uint32_t ack; 1237 int i; 1238 1239 ack = 0; 1240 1241 snd_mtxlock(sc->lock); 1242 1243 if (stat & EMU_IPR_INTERVALTIMER) { 1244 ack |= EMU_IPR_INTERVALTIMER; 1245 for (i = 0; i < MAX_CHANNELS; i++) 1246 if (sc->pch[i].channel) { 1247 if (sc->pch[i].run == 1) { 1248 snd_mtxunlock(sc->lock); 1249 chn_intr(sc->pch[i].channel); 1250 snd_mtxlock(sc->lock); 1251 } else 1252 emu_timer_enable(sc->card, sc->pch[i].timer, 0); 1253 } 1254 /* ADC may install timer to get low-latency interrupts */ 1255 if ((sc->rch_adc.channel) && (sc->rch_adc.run)) { 1256 snd_mtxunlock(sc->lock); 1257 chn_intr(sc->rch_adc.channel); 1258 snd_mtxlock(sc->lock); 1259 } 1260 /* 1261 * EFX does not use timer, because it will fill 1262 * buffer at least 32x times faster than ADC. 1263 */ 1264 } 1265 1266 1267 if (stat & (EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL)) { 1268 ack |= stat & (EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL); 1269 if (sc->rch_adc.channel) { 1270 snd_mtxunlock(sc->lock); 1271 chn_intr(sc->rch_adc.channel); 1272 snd_mtxlock(sc->lock); 1273 } 1274 } 1275 1276 if (stat & (EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL)) { 1277 ack |= stat & (EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL); 1278 if (sc->rch_efx.channel) { 1279 snd_mtxunlock(sc->lock); 1280 chn_intr(sc->rch_efx.channel); 1281 snd_mtxlock(sc->lock); 1282 } 1283 } 1284 snd_mtxunlock(sc->lock); 1285 1286 return (ack); 1287 } 1288 1289 static int 1290 emu_pcm_init(struct emu_pcm_info *sc) 1291 { 1292 sc->bufsz = pcm_getbuffersize(sc->dev, EMUPAGESIZE, EMU_REC_BUFSZ, EMU_MAX_BUFSZ); 1293 return (0); 1294 } 1295 1296 static int 1297 emu_pcm_uninit(struct emu_pcm_info *sc __unused) 1298 { 1299 return (0); 1300 } 1301 1302 static int 1303 emu_pcm_probe(device_t dev) 1304 { 1305 uintptr_t func, route, r; 1306 const char *rt; 1307 char buffer[255]; 1308 1309 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_FUNC, &func); 1310 1311 if (func != SCF_PCM) 1312 return (ENXIO); 1313 1314 rt = "UNKNOWN"; 1315 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ROUTE, &route); 1316 switch (route) { 1317 case RT_FRONT: 1318 rt = "front"; 1319 break; 1320 case RT_REAR: 1321 rt = "rear"; 1322 break; 1323 case RT_CENTER: 1324 rt = "center"; 1325 break; 1326 case RT_SUB: 1327 rt = "subwoofer"; 1328 break; 1329 case RT_SIDE: 1330 rt = "side"; 1331 break; 1332 case RT_MCHRECORD: 1333 rt = "multichannel recording"; 1334 break; 1335 } 1336 1337 snprintf(buffer, 255, "EMU10Kx DSP %s PCM interface", rt); 1338 device_set_desc_copy(dev, buffer); 1339 return (0); 1340 } 1341 1342 static int 1343 emu_pcm_attach(device_t dev) 1344 { 1345 struct emu_pcm_info *sc; 1346 unsigned int i; 1347 char status[SND_STATUSLEN]; 1348 uint32_t inte, ipr; 1349 uintptr_t route, r, ivar; 1350 1351 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO); 1352 sc->card = (struct emu_sc_info *)(device_get_softc(device_get_parent(dev))); 1353 if (sc->card == NULL) { 1354 device_printf(dev, "cannot get bridge conf\n"); 1355 free(sc, M_DEVBUF); 1356 return (ENXIO); 1357 } 1358 1359 sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_emu10kx pcm softc"); 1360 sc->dev = dev; 1361 1362 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ISEMU10K1, &ivar); 1363 sc->is_emu10k1 = ivar ? 1 : 0; 1364 1365 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_MCH_DISABLED, &ivar); 1366 sc->mch_disabled = ivar ? 1 : 0; 1367 1368 sc->codec = NULL; 1369 1370 for (i = 0; i < 8; i++) { 1371 sc->rt.routing_left[i] = i; 1372 sc->rt.amounts_left[i] = 0x00; 1373 sc->rt.routing_right[i] = i; 1374 sc->rt.amounts_right[i] = 0x00; 1375 } 1376 1377 for (i = 0; i < 8; i++) { 1378 sc->rt_mono.routing_left[i] = i; 1379 sc->rt_mono.amounts_left[i] = 0x00; 1380 sc->rt_mono.routing_right[i] = i; 1381 sc->rt_mono.amounts_right[i] = 0x00; 1382 } 1383 1384 sc->emu10k1_volcache[0][0] = 75; 1385 sc->emu10k1_volcache[1][0] = 75; 1386 sc->emu10k1_volcache[0][1] = 75; 1387 sc->emu10k1_volcache[1][1] = 75; 1388 r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ROUTE, &route); 1389 sc->route = route; 1390 switch (route) { 1391 case RT_FRONT: 1392 sc->rt.amounts_left[0] = 0xff; 1393 sc->rt.amounts_right[1] = 0xff; 1394 sc->rt_mono.amounts_left[0] = 0xff; 1395 sc->rt_mono.amounts_left[1] = 0xff; 1396 if (sc->is_emu10k1) 1397 sc->codec = AC97_CREATE(dev, sc, emu_ac97); 1398 else 1399 sc->codec = AC97_CREATE(dev, sc, emu_eac97); 1400 sc->ac97_mixerclass = NULL; 1401 if (sc->codec != NULL) 1402 sc->ac97_mixerclass = ac97_getmixerclass(); 1403 if (mixer_init(dev, &emudspmixer_class, sc)) { 1404 device_printf(dev, "failed to initialize DSP mixer\n"); 1405 goto bad; 1406 } 1407 break; 1408 case RT_REAR: 1409 sc->rt.amounts_left[2] = 0xff; 1410 sc->rt.amounts_right[3] = 0xff; 1411 sc->rt_mono.amounts_left[2] = 0xff; 1412 sc->rt_mono.amounts_left[3] = 0xff; 1413 if (mixer_init(dev, &emudspmixer_class, sc)) { 1414 device_printf(dev, "failed to initialize mixer\n"); 1415 goto bad; 1416 } 1417 break; 1418 case RT_CENTER: 1419 sc->rt.amounts_left[4] = 0xff; 1420 sc->rt_mono.amounts_left[4] = 0xff; 1421 if (mixer_init(dev, &emudspmixer_class, sc)) { 1422 device_printf(dev, "failed to initialize mixer\n"); 1423 goto bad; 1424 } 1425 break; 1426 case RT_SUB: 1427 sc->rt.amounts_left[5] = 0xff; 1428 sc->rt_mono.amounts_left[5] = 0xff; 1429 if (mixer_init(dev, &emudspmixer_class, sc)) { 1430 device_printf(dev, "failed to initialize mixer\n"); 1431 goto bad; 1432 } 1433 break; 1434 case RT_SIDE: 1435 sc->rt.amounts_left[6] = 0xff; 1436 sc->rt.amounts_right[7] = 0xff; 1437 sc->rt_mono.amounts_left[6] = 0xff; 1438 sc->rt_mono.amounts_left[7] = 0xff; 1439 if (mixer_init(dev, &emudspmixer_class, sc)) { 1440 device_printf(dev, "failed to initialize mixer\n"); 1441 goto bad; 1442 } 1443 break; 1444 case RT_MCHRECORD: 1445 if (mixer_init(dev, &emuefxmixer_class, sc)) { 1446 device_printf(dev, "failed to initialize EFX mixer\n"); 1447 goto bad; 1448 } 1449 break; 1450 default: 1451 device_printf(dev, "invalid default route\n"); 1452 goto bad; 1453 } 1454 1455 inte = EMU_INTE_INTERTIMERENB; 1456 ipr = EMU_IPR_INTERVALTIMER; /* Used by playback & ADC */ 1457 sc->ihandle = emu_intr_register(sc->card, inte, ipr, &emu_pcm_intr, sc); 1458 1459 if (emu_pcm_init(sc) == -1) { 1460 device_printf(dev, "unable to initialize PCM part of the card\n"); 1461 goto bad; 1462 } 1463 1464 /* 1465 * We don't register interrupt handler with snd_setup_intr 1466 * in pcm device. Mark pcm device as MPSAFE manually. 1467 */ 1468 pcm_setflags(dev, pcm_getflags(dev) | SD_F_MPSAFE); 1469 1470 /* XXX we should better get number of available channels from parent */ 1471 if (pcm_register(dev, sc, (route == RT_FRONT) ? MAX_CHANNELS : 1, (route == RT_FRONT) ? 1 : 0)) { 1472 device_printf(dev, "can't register PCM channels!\n"); 1473 goto bad; 1474 } 1475 sc->pnum = 0; 1476 if (route != RT_MCHRECORD) 1477 pcm_addchan(dev, PCMDIR_PLAY, &emupchan_class, sc); 1478 if (route == RT_FRONT) { 1479 for (i = 1; i < MAX_CHANNELS; i++) 1480 pcm_addchan(dev, PCMDIR_PLAY, &emupchan_class, sc); 1481 pcm_addchan(dev, PCMDIR_REC, &emurchan_class, sc); 1482 } 1483 if (route == RT_MCHRECORD) 1484 pcm_addchan(dev, PCMDIR_REC, &emufxrchan_class, sc); 1485 1486 snprintf(status, SND_STATUSLEN, "on %s", device_get_nameunit(device_get_parent(dev))); 1487 pcm_setstatus(dev, status); 1488 1489 return (0); 1490 1491 bad: 1492 if (sc->codec) 1493 ac97_destroy(sc->codec); 1494 if (sc->lock) 1495 snd_mtxfree(sc->lock); 1496 free(sc, M_DEVBUF); 1497 return (ENXIO); 1498 } 1499 1500 static int 1501 emu_pcm_detach(device_t dev) 1502 { 1503 int r; 1504 struct emu_pcm_info *sc; 1505 1506 sc = pcm_getdevinfo(dev); 1507 1508 r = pcm_unregister(dev); 1509 1510 if (r) return (r); 1511 1512 emu_pcm_uninit(sc); 1513 1514 if (sc->lock) 1515 snd_mtxfree(sc->lock); 1516 free(sc, M_DEVBUF); 1517 1518 return (0); 1519 } 1520 1521 static device_method_t emu_pcm_methods[] = { 1522 DEVMETHOD(device_probe, emu_pcm_probe), 1523 DEVMETHOD(device_attach, emu_pcm_attach), 1524 DEVMETHOD(device_detach, emu_pcm_detach), 1525 1526 DEVMETHOD_END 1527 }; 1528 1529 static driver_t emu_pcm_driver = { 1530 "pcm", 1531 emu_pcm_methods, 1532 PCM_SOFTC_SIZE, 1533 NULL, 1534 0, 1535 NULL 1536 }; 1537 DRIVER_MODULE(snd_emu10kx_pcm, emu10kx, emu_pcm_driver, pcm_devclass, 0, 0); 1538 MODULE_DEPEND(snd_emu10kx_pcm, snd_emu10kx, SND_EMU10KX_MINVER, SND_EMU10KX_PREFVER, SND_EMU10KX_MAXVER); 1539 MODULE_DEPEND(snd_emu10kx_pcm, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 1540 MODULE_VERSION(snd_emu10kx_pcm, SND_EMU10KX_PREFVER); 1541