1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 4 * Creative Labs, Inc. 5 * Routines for control of EMU10K1 chips / PCM routines 6 * Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com> 7 * 8 * BUGS: 9 * -- 10 * 11 * TODO: 12 * -- 13 */ 14 15 #include <linux/pci.h> 16 #include <linux/delay.h> 17 #include <linux/slab.h> 18 #include <linux/time.h> 19 #include <linux/init.h> 20 #include <sound/core.h> 21 #include <sound/emu10k1.h> 22 23 static void snd_emu10k1_pcm_interrupt(struct snd_emu10k1 *emu, 24 struct snd_emu10k1_voice *voice) 25 { 26 struct snd_emu10k1_pcm *epcm; 27 28 if ((epcm = voice->epcm) == NULL) 29 return; 30 if (epcm->substream == NULL) 31 return; 32 #if 0 33 dev_dbg(emu->card->dev, 34 "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n", 35 epcm->substream->runtime->hw->pointer(emu, epcm->substream), 36 snd_pcm_lib_period_bytes(epcm->substream), 37 snd_pcm_lib_buffer_bytes(epcm->substream)); 38 #endif 39 snd_pcm_period_elapsed(epcm->substream); 40 } 41 42 static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu, 43 unsigned int status) 44 { 45 #if 0 46 if (status & IPR_ADCBUFHALFFULL) { 47 if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME) 48 return; 49 } 50 #endif 51 snd_pcm_period_elapsed(emu->pcm_capture_substream); 52 } 53 54 static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu, 55 unsigned int status) 56 { 57 #if 0 58 if (status & IPR_MICBUFHALFFULL) { 59 if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME) 60 return; 61 } 62 #endif 63 snd_pcm_period_elapsed(emu->pcm_capture_mic_substream); 64 } 65 66 static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu, 67 unsigned int status) 68 { 69 #if 0 70 if (status & IPR_EFXBUFHALFFULL) { 71 if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME) 72 return; 73 } 74 #endif 75 snd_pcm_period_elapsed(emu->pcm_capture_efx_substream); 76 } 77 78 static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(struct snd_pcm_substream *substream) 79 { 80 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 81 struct snd_pcm_runtime *runtime = substream->runtime; 82 struct snd_emu10k1_pcm *epcm = runtime->private_data; 83 unsigned int ptr; 84 85 if (!epcm->running) 86 return 0; 87 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff; 88 ptr += runtime->buffer_size; 89 ptr -= epcm->ccca_start_addr; 90 ptr %= runtime->buffer_size; 91 92 return ptr; 93 } 94 95 static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm, int voices) 96 { 97 int err, i; 98 99 if (epcm->voices[1] != NULL && voices < 2) { 100 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]); 101 epcm->voices[1] = NULL; 102 } 103 for (i = 0; i < voices; i++) { 104 if (epcm->voices[i] == NULL) 105 break; 106 } 107 if (i == voices) 108 return 0; /* already allocated */ 109 110 for (i = 0; i < ARRAY_SIZE(epcm->voices); i++) { 111 if (epcm->voices[i]) { 112 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]); 113 epcm->voices[i] = NULL; 114 } 115 } 116 err = snd_emu10k1_voice_alloc(epcm->emu, 117 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX, 118 voices, 119 &epcm->voices[0]); 120 121 if (err < 0) 122 return err; 123 epcm->voices[0]->epcm = epcm; 124 if (voices > 1) { 125 for (i = 1; i < voices; i++) { 126 epcm->voices[i] = &epcm->emu->voices[epcm->voices[0]->number + i]; 127 epcm->voices[i]->epcm = epcm; 128 } 129 } 130 if (epcm->extra == NULL) { 131 err = snd_emu10k1_voice_alloc(epcm->emu, 132 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX, 133 1, 134 &epcm->extra); 135 if (err < 0) { 136 /* 137 dev_dbg(emu->card->dev, "pcm_channel_alloc: " 138 "failed extra: voices=%d, frame=%d\n", 139 voices, frame); 140 */ 141 for (i = 0; i < voices; i++) { 142 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]); 143 epcm->voices[i] = NULL; 144 } 145 return err; 146 } 147 epcm->extra->epcm = epcm; 148 epcm->extra->interrupt = snd_emu10k1_pcm_interrupt; 149 } 150 return 0; 151 } 152 153 static const unsigned int capture_period_sizes[31] = { 154 384, 448, 512, 640, 155 384*2, 448*2, 512*2, 640*2, 156 384*4, 448*4, 512*4, 640*4, 157 384*8, 448*8, 512*8, 640*8, 158 384*16, 448*16, 512*16, 640*16, 159 384*32, 448*32, 512*32, 640*32, 160 384*64, 448*64, 512*64, 640*64, 161 384*128,448*128,512*128 162 }; 163 164 static const struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = { 165 .count = 31, 166 .list = capture_period_sizes, 167 .mask = 0 168 }; 169 170 static const unsigned int capture_rates[8] = { 171 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000 172 }; 173 174 static const struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = { 175 .count = 8, 176 .list = capture_rates, 177 .mask = 0 178 }; 179 180 static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate) 181 { 182 switch (rate) { 183 case 8000: return ADCCR_SAMPLERATE_8; 184 case 11025: return ADCCR_SAMPLERATE_11; 185 case 16000: return ADCCR_SAMPLERATE_16; 186 case 22050: return ADCCR_SAMPLERATE_22; 187 case 24000: return ADCCR_SAMPLERATE_24; 188 case 32000: return ADCCR_SAMPLERATE_32; 189 case 44100: return ADCCR_SAMPLERATE_44; 190 case 48000: return ADCCR_SAMPLERATE_48; 191 default: 192 snd_BUG(); 193 return ADCCR_SAMPLERATE_8; 194 } 195 } 196 197 static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate) 198 { 199 switch (rate) { 200 case 8000: return A_ADCCR_SAMPLERATE_8; 201 case 11025: return A_ADCCR_SAMPLERATE_11; 202 case 12000: return A_ADCCR_SAMPLERATE_12; /* really supported? */ 203 case 16000: return ADCCR_SAMPLERATE_16; 204 case 22050: return ADCCR_SAMPLERATE_22; 205 case 24000: return ADCCR_SAMPLERATE_24; 206 case 32000: return ADCCR_SAMPLERATE_32; 207 case 44100: return ADCCR_SAMPLERATE_44; 208 case 48000: return ADCCR_SAMPLERATE_48; 209 default: 210 snd_BUG(); 211 return A_ADCCR_SAMPLERATE_8; 212 } 213 } 214 215 static unsigned int emu10k1_calc_pitch_target(unsigned int rate) 216 { 217 unsigned int pitch_target; 218 219 pitch_target = (rate << 8) / 375; 220 pitch_target = (pitch_target >> 1) + (pitch_target & 1); 221 return pitch_target; 222 } 223 224 #define PITCH_48000 0x00004000 225 #define PITCH_96000 0x00008000 226 #define PITCH_85000 0x00007155 227 #define PITCH_80726 0x00006ba2 228 #define PITCH_67882 0x00005a82 229 #define PITCH_57081 0x00004c1c 230 231 static unsigned int emu10k1_select_interprom(unsigned int pitch_target) 232 { 233 if (pitch_target == PITCH_48000) 234 return CCCA_INTERPROM_0; 235 else if (pitch_target < PITCH_48000) 236 return CCCA_INTERPROM_1; 237 else if (pitch_target >= PITCH_96000) 238 return CCCA_INTERPROM_0; 239 else if (pitch_target >= PITCH_85000) 240 return CCCA_INTERPROM_6; 241 else if (pitch_target >= PITCH_80726) 242 return CCCA_INTERPROM_5; 243 else if (pitch_target >= PITCH_67882) 244 return CCCA_INTERPROM_4; 245 else if (pitch_target >= PITCH_57081) 246 return CCCA_INTERPROM_3; 247 else 248 return CCCA_INTERPROM_2; 249 } 250 251 /* 252 * calculate cache invalidate size 253 * 254 * stereo: channel is stereo 255 * w_16: using 16bit samples 256 * 257 * returns: cache invalidate size in samples 258 */ 259 static inline int emu10k1_ccis(int stereo, int w_16) 260 { 261 if (w_16) { 262 return stereo ? 24 : 26; 263 } else { 264 return stereo ? 24*2 : 26*2; 265 } 266 } 267 268 static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu, 269 int master, int extra, 270 struct snd_emu10k1_voice *evoice, 271 unsigned int start_addr, 272 unsigned int end_addr, 273 struct snd_emu10k1_pcm_mixer *mix) 274 { 275 struct snd_pcm_substream *substream = evoice->epcm->substream; 276 struct snd_pcm_runtime *runtime = substream->runtime; 277 unsigned int silent_page, tmp; 278 int voice, stereo, w_16; 279 unsigned char send_amount[8]; 280 unsigned char send_routing[8]; 281 unsigned long flags; 282 unsigned int pitch_target; 283 unsigned int ccis; 284 285 voice = evoice->number; 286 stereo = runtime->channels == 2; 287 w_16 = snd_pcm_format_width(runtime->format) == 16; 288 289 if (!extra && stereo) { 290 start_addr >>= 1; 291 end_addr >>= 1; 292 } 293 if (w_16) { 294 start_addr >>= 1; 295 end_addr >>= 1; 296 } 297 298 spin_lock_irqsave(&emu->reg_lock, flags); 299 300 /* volume parameters */ 301 if (extra) { 302 memset(send_routing, 0, sizeof(send_routing)); 303 send_routing[0] = 0; 304 send_routing[1] = 1; 305 send_routing[2] = 2; 306 send_routing[3] = 3; 307 memset(send_amount, 0, sizeof(send_amount)); 308 } else { 309 /* mono, left, right (master voice = left) */ 310 tmp = stereo ? (master ? 1 : 2) : 0; 311 memcpy(send_routing, &mix->send_routing[tmp][0], 8); 312 memcpy(send_amount, &mix->send_volume[tmp][0], 8); 313 } 314 315 ccis = emu10k1_ccis(stereo, w_16); 316 317 if (master) { 318 evoice->epcm->ccca_start_addr = start_addr + ccis; 319 if (extra) { 320 start_addr += ccis; 321 end_addr += ccis + emu->delay_pcm_irq; 322 } 323 if (stereo && !extra) { 324 snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK); 325 snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK); 326 } else { 327 snd_emu10k1_ptr_write(emu, CPF, voice, 0); 328 } 329 } 330 331 /* setup routing */ 332 if (emu->audigy) { 333 snd_emu10k1_ptr_write(emu, A_FXRT1, voice, 334 snd_emu10k1_compose_audigy_fxrt1(send_routing)); 335 snd_emu10k1_ptr_write(emu, A_FXRT2, voice, 336 snd_emu10k1_compose_audigy_fxrt2(send_routing)); 337 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice, 338 ((unsigned int)send_amount[4] << 24) | 339 ((unsigned int)send_amount[5] << 16) | 340 ((unsigned int)send_amount[6] << 8) | 341 (unsigned int)send_amount[7]); 342 } else 343 snd_emu10k1_ptr_write(emu, FXRT, voice, 344 snd_emu10k1_compose_send_routing(send_routing)); 345 /* Stop CA */ 346 /* Assumption that PT is already 0 so no harm overwriting */ 347 snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]); 348 snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24)); 349 snd_emu10k1_ptr_write(emu, PSST, voice, 350 (start_addr + (extra ? emu->delay_pcm_irq : 0)) | 351 (send_amount[2] << 24)); 352 if (emu->card_capabilities->emu_model) 353 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */ 354 else 355 pitch_target = emu10k1_calc_pitch_target(runtime->rate); 356 if (extra) 357 snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr | 358 emu10k1_select_interprom(pitch_target) | 359 (w_16 ? 0 : CCCA_8BITSELECT)); 360 else 361 snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) | 362 emu10k1_select_interprom(pitch_target) | 363 (w_16 ? 0 : CCCA_8BITSELECT)); 364 /* Clear filter delay memory */ 365 snd_emu10k1_ptr_write(emu, Z1, voice, 0); 366 snd_emu10k1_ptr_write(emu, Z2, voice, 0); 367 /* invalidate maps */ 368 silent_page = ((unsigned int)emu->silent_page.addr << emu->address_mode) | (emu->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0); 369 snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page); 370 snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page); 371 /* modulation envelope */ 372 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff); 373 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff); 374 snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0); 375 snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f); 376 snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000); 377 snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000); 378 snd_emu10k1_ptr_write(emu, FMMOD, voice, 0); 379 snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0); 380 snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0); 381 snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000); 382 /* volume envelope */ 383 snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f); 384 snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000); 385 /* filter envelope */ 386 snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f); 387 /* pitch envelope */ 388 snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0); 389 390 spin_unlock_irqrestore(&emu->reg_lock, flags); 391 } 392 393 static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream, 394 struct snd_pcm_hw_params *hw_params) 395 { 396 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 397 struct snd_pcm_runtime *runtime = substream->runtime; 398 struct snd_emu10k1_pcm *epcm = runtime->private_data; 399 size_t alloc_size; 400 int err; 401 402 if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0) 403 return err; 404 405 alloc_size = params_buffer_bytes(hw_params); 406 if (emu->iommu_workaround) 407 alloc_size += EMUPAGESIZE; 408 err = snd_pcm_lib_malloc_pages(substream, alloc_size); 409 if (err < 0) 410 return err; 411 if (emu->iommu_workaround && runtime->dma_bytes >= EMUPAGESIZE) 412 runtime->dma_bytes -= EMUPAGESIZE; 413 if (err > 0) { /* change */ 414 int mapped; 415 if (epcm->memblk != NULL) 416 snd_emu10k1_free_pages(emu, epcm->memblk); 417 epcm->memblk = snd_emu10k1_alloc_pages(emu, substream); 418 epcm->start_addr = 0; 419 if (! epcm->memblk) 420 return -ENOMEM; 421 mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page; 422 if (mapped < 0) 423 return -ENOMEM; 424 epcm->start_addr = mapped << PAGE_SHIFT; 425 } 426 return 0; 427 } 428 429 static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream) 430 { 431 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 432 struct snd_pcm_runtime *runtime = substream->runtime; 433 struct snd_emu10k1_pcm *epcm; 434 435 if (runtime->private_data == NULL) 436 return 0; 437 epcm = runtime->private_data; 438 if (epcm->extra) { 439 snd_emu10k1_voice_free(epcm->emu, epcm->extra); 440 epcm->extra = NULL; 441 } 442 if (epcm->voices[1]) { 443 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]); 444 epcm->voices[1] = NULL; 445 } 446 if (epcm->voices[0]) { 447 snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]); 448 epcm->voices[0] = NULL; 449 } 450 if (epcm->memblk) { 451 snd_emu10k1_free_pages(emu, epcm->memblk); 452 epcm->memblk = NULL; 453 epcm->start_addr = 0; 454 } 455 snd_pcm_lib_free_pages(substream); 456 return 0; 457 } 458 459 static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream) 460 { 461 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 462 struct snd_pcm_runtime *runtime = substream->runtime; 463 struct snd_emu10k1_pcm *epcm; 464 int i; 465 466 if (runtime->private_data == NULL) 467 return 0; 468 epcm = runtime->private_data; 469 if (epcm->extra) { 470 snd_emu10k1_voice_free(epcm->emu, epcm->extra); 471 epcm->extra = NULL; 472 } 473 for (i = 0; i < NUM_EFX_PLAYBACK; i++) { 474 if (epcm->voices[i]) { 475 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]); 476 epcm->voices[i] = NULL; 477 } 478 } 479 if (epcm->memblk) { 480 snd_emu10k1_free_pages(emu, epcm->memblk); 481 epcm->memblk = NULL; 482 epcm->start_addr = 0; 483 } 484 snd_pcm_lib_free_pages(substream); 485 return 0; 486 } 487 488 static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream) 489 { 490 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 491 struct snd_pcm_runtime *runtime = substream->runtime; 492 struct snd_emu10k1_pcm *epcm = runtime->private_data; 493 unsigned int start_addr, end_addr; 494 495 start_addr = epcm->start_addr; 496 end_addr = snd_pcm_lib_period_bytes(substream); 497 if (runtime->channels == 2) { 498 start_addr >>= 1; 499 end_addr >>= 1; 500 } 501 end_addr += start_addr; 502 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra, 503 start_addr, end_addr, NULL); 504 start_addr = epcm->start_addr; 505 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream); 506 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0], 507 start_addr, end_addr, 508 &emu->pcm_mixer[substream->number]); 509 if (epcm->voices[1]) 510 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1], 511 start_addr, end_addr, 512 &emu->pcm_mixer[substream->number]); 513 return 0; 514 } 515 516 static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream) 517 { 518 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 519 struct snd_pcm_runtime *runtime = substream->runtime; 520 struct snd_emu10k1_pcm *epcm = runtime->private_data; 521 unsigned int start_addr, end_addr; 522 unsigned int channel_size; 523 int i; 524 525 start_addr = epcm->start_addr; 526 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream); 527 528 /* 529 * the kX driver leaves some space between voices 530 */ 531 channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK; 532 533 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra, 534 start_addr, start_addr + (channel_size / 2), NULL); 535 536 /* only difference with the master voice is we use it for the pointer */ 537 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0], 538 start_addr, start_addr + channel_size, 539 &emu->efx_pcm_mixer[0]); 540 541 start_addr += channel_size; 542 for (i = 1; i < NUM_EFX_PLAYBACK; i++) { 543 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i], 544 start_addr, start_addr + channel_size, 545 &emu->efx_pcm_mixer[i]); 546 start_addr += channel_size; 547 } 548 549 return 0; 550 } 551 552 static const struct snd_pcm_hardware snd_emu10k1_efx_playback = 553 { 554 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED | 555 SNDRV_PCM_INFO_BLOCK_TRANSFER | 556 SNDRV_PCM_INFO_RESUME | 557 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE), 558 .formats = SNDRV_PCM_FMTBIT_S16_LE, 559 .rates = SNDRV_PCM_RATE_48000, 560 .rate_min = 48000, 561 .rate_max = 48000, 562 .channels_min = NUM_EFX_PLAYBACK, 563 .channels_max = NUM_EFX_PLAYBACK, 564 .buffer_bytes_max = (64*1024), 565 .period_bytes_min = 64, 566 .period_bytes_max = (64*1024), 567 .periods_min = 2, 568 .periods_max = 2, 569 .fifo_size = 0, 570 }; 571 572 static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream) 573 { 574 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 575 struct snd_pcm_runtime *runtime = substream->runtime; 576 struct snd_emu10k1_pcm *epcm = runtime->private_data; 577 int idx; 578 579 /* zeroing the buffer size will stop capture */ 580 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0); 581 switch (epcm->type) { 582 case CAPTURE_AC97ADC: 583 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0); 584 break; 585 case CAPTURE_EFX: 586 if (emu->audigy) { 587 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0); 588 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0); 589 } else 590 snd_emu10k1_ptr_write(emu, FXWC, 0, 0); 591 break; 592 default: 593 break; 594 } 595 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr); 596 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream); 597 epcm->capture_bs_val = 0; 598 for (idx = 0; idx < 31; idx++) { 599 if (capture_period_sizes[idx] == epcm->capture_bufsize) { 600 epcm->capture_bs_val = idx + 1; 601 break; 602 } 603 } 604 if (epcm->capture_bs_val == 0) { 605 snd_BUG(); 606 epcm->capture_bs_val++; 607 } 608 if (epcm->type == CAPTURE_AC97ADC) { 609 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE; 610 if (runtime->channels > 1) 611 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE; 612 epcm->capture_cr_val |= emu->audigy ? 613 snd_emu10k1_audigy_capture_rate_reg(runtime->rate) : 614 snd_emu10k1_capture_rate_reg(runtime->rate); 615 } 616 return 0; 617 } 618 619 static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice) 620 { 621 struct snd_pcm_runtime *runtime; 622 unsigned int voice, stereo, i, ccis, cra = 64, cs, sample; 623 624 if (evoice == NULL) 625 return; 626 runtime = evoice->epcm->substream->runtime; 627 voice = evoice->number; 628 stereo = (!extra && runtime->channels == 2); 629 sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080; 630 ccis = emu10k1_ccis(stereo, sample == 0); 631 /* set cs to 2 * number of cache registers beside the invalidated */ 632 cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1; 633 if (cs > 16) cs = 16; 634 for (i = 0; i < cs; i++) { 635 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample); 636 if (stereo) { 637 snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample); 638 } 639 } 640 /* reset cache */ 641 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0); 642 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra); 643 if (stereo) { 644 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0); 645 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra); 646 } 647 /* fill cache */ 648 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis); 649 if (stereo) { 650 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis); 651 } 652 } 653 654 static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, 655 int master, int extra, 656 struct snd_emu10k1_pcm_mixer *mix) 657 { 658 struct snd_pcm_substream *substream; 659 struct snd_pcm_runtime *runtime; 660 unsigned int attn, vattn; 661 unsigned int voice, tmp; 662 663 if (evoice == NULL) /* skip second voice for mono */ 664 return; 665 substream = evoice->epcm->substream; 666 runtime = substream->runtime; 667 voice = evoice->number; 668 669 attn = extra ? 0 : 0x00ff; 670 tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0; 671 vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0; 672 snd_emu10k1_ptr_write(emu, IFATN, voice, attn); 673 snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff); 674 snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff); 675 snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f); 676 snd_emu10k1_voice_clear_loop_stop(emu, voice); 677 } 678 679 static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra) 680 { 681 struct snd_pcm_substream *substream; 682 struct snd_pcm_runtime *runtime; 683 unsigned int voice, pitch, pitch_target; 684 685 if (evoice == NULL) /* skip second voice for mono */ 686 return; 687 substream = evoice->epcm->substream; 688 runtime = substream->runtime; 689 voice = evoice->number; 690 691 pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8; 692 if (emu->card_capabilities->emu_model) 693 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */ 694 else 695 pitch_target = emu10k1_calc_pitch_target(runtime->rate); 696 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target); 697 if (master || evoice->epcm->type == PLAYBACK_EFX) 698 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target); 699 snd_emu10k1_ptr_write(emu, IP, voice, pitch); 700 if (extra) 701 snd_emu10k1_voice_intr_enable(emu, voice); 702 } 703 704 static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice) 705 { 706 unsigned int voice; 707 708 if (evoice == NULL) 709 return; 710 voice = evoice->number; 711 snd_emu10k1_voice_intr_disable(emu, voice); 712 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0); 713 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0); 714 snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff); 715 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff); 716 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff); 717 snd_emu10k1_ptr_write(emu, IP, voice, 0); 718 } 719 720 static inline void snd_emu10k1_playback_mangle_extra(struct snd_emu10k1 *emu, 721 struct snd_emu10k1_pcm *epcm, 722 struct snd_pcm_substream *substream, 723 struct snd_pcm_runtime *runtime) 724 { 725 unsigned int ptr, period_pos; 726 727 /* try to sychronize the current position for the interrupt 728 source voice */ 729 period_pos = runtime->status->hw_ptr - runtime->hw_ptr_interrupt; 730 period_pos %= runtime->period_size; 731 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->extra->number); 732 ptr &= ~0x00ffffff; 733 ptr |= epcm->ccca_start_addr + period_pos; 734 snd_emu10k1_ptr_write(emu, CCCA, epcm->extra->number, ptr); 735 } 736 737 static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream, 738 int cmd) 739 { 740 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 741 struct snd_pcm_runtime *runtime = substream->runtime; 742 struct snd_emu10k1_pcm *epcm = runtime->private_data; 743 struct snd_emu10k1_pcm_mixer *mix; 744 int result = 0; 745 746 /* 747 dev_dbg(emu->card->dev, 748 "trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n", 749 (int)emu, cmd, substream->ops->pointer(substream)) 750 */ 751 spin_lock(&emu->reg_lock); 752 switch (cmd) { 753 case SNDRV_PCM_TRIGGER_START: 754 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */ 755 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]); 756 /* fall through */ 757 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 758 case SNDRV_PCM_TRIGGER_RESUME: 759 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE) 760 snd_emu10k1_playback_mangle_extra(emu, epcm, substream, runtime); 761 mix = &emu->pcm_mixer[substream->number]; 762 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix); 763 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix); 764 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL); 765 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0); 766 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0); 767 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1); 768 epcm->running = 1; 769 break; 770 case SNDRV_PCM_TRIGGER_STOP: 771 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 772 case SNDRV_PCM_TRIGGER_SUSPEND: 773 epcm->running = 0; 774 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]); 775 snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]); 776 snd_emu10k1_playback_stop_voice(emu, epcm->extra); 777 break; 778 default: 779 result = -EINVAL; 780 break; 781 } 782 spin_unlock(&emu->reg_lock); 783 return result; 784 } 785 786 static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream, 787 int cmd) 788 { 789 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 790 struct snd_pcm_runtime *runtime = substream->runtime; 791 struct snd_emu10k1_pcm *epcm = runtime->private_data; 792 int result = 0; 793 794 spin_lock(&emu->reg_lock); 795 switch (cmd) { 796 case SNDRV_PCM_TRIGGER_START: 797 case SNDRV_PCM_TRIGGER_RESUME: 798 /* hmm this should cause full and half full interrupt to be raised? */ 799 outl(epcm->capture_ipr, emu->port + IPR); 800 snd_emu10k1_intr_enable(emu, epcm->capture_inte); 801 /* 802 dev_dbg(emu->card->dev, "adccr = 0x%x, adcbs = 0x%x\n", 803 epcm->adccr, epcm->adcbs); 804 */ 805 switch (epcm->type) { 806 case CAPTURE_AC97ADC: 807 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val); 808 break; 809 case CAPTURE_EFX: 810 if (emu->audigy) { 811 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val); 812 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2); 813 dev_dbg(emu->card->dev, 814 "cr_val=0x%x, cr_val2=0x%x\n", 815 epcm->capture_cr_val, 816 epcm->capture_cr_val2); 817 } else 818 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val); 819 break; 820 default: 821 break; 822 } 823 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val); 824 epcm->running = 1; 825 epcm->first_ptr = 1; 826 break; 827 case SNDRV_PCM_TRIGGER_STOP: 828 case SNDRV_PCM_TRIGGER_SUSPEND: 829 epcm->running = 0; 830 snd_emu10k1_intr_disable(emu, epcm->capture_inte); 831 outl(epcm->capture_ipr, emu->port + IPR); 832 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0); 833 switch (epcm->type) { 834 case CAPTURE_AC97ADC: 835 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0); 836 break; 837 case CAPTURE_EFX: 838 if (emu->audigy) { 839 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0); 840 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0); 841 } else 842 snd_emu10k1_ptr_write(emu, FXWC, 0, 0); 843 break; 844 default: 845 break; 846 } 847 break; 848 default: 849 result = -EINVAL; 850 } 851 spin_unlock(&emu->reg_lock); 852 return result; 853 } 854 855 static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream) 856 { 857 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 858 struct snd_pcm_runtime *runtime = substream->runtime; 859 struct snd_emu10k1_pcm *epcm = runtime->private_data; 860 unsigned int ptr; 861 862 if (!epcm->running) 863 return 0; 864 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff; 865 #if 0 /* Perex's code */ 866 ptr += runtime->buffer_size; 867 ptr -= epcm->ccca_start_addr; 868 ptr %= runtime->buffer_size; 869 #else /* EMU10K1 Open Source code from Creative */ 870 if (ptr < epcm->ccca_start_addr) 871 ptr += runtime->buffer_size - epcm->ccca_start_addr; 872 else { 873 ptr -= epcm->ccca_start_addr; 874 if (ptr >= runtime->buffer_size) 875 ptr -= runtime->buffer_size; 876 } 877 #endif 878 /* 879 dev_dbg(emu->card->dev, 880 "ptr = 0x%lx, buffer_size = 0x%lx, period_size = 0x%lx\n", 881 (long)ptr, (long)runtime->buffer_size, 882 (long)runtime->period_size); 883 */ 884 return ptr; 885 } 886 887 888 static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream, 889 int cmd) 890 { 891 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 892 struct snd_pcm_runtime *runtime = substream->runtime; 893 struct snd_emu10k1_pcm *epcm = runtime->private_data; 894 int i; 895 int result = 0; 896 897 spin_lock(&emu->reg_lock); 898 switch (cmd) { 899 case SNDRV_PCM_TRIGGER_START: 900 /* prepare voices */ 901 for (i = 0; i < NUM_EFX_PLAYBACK; i++) { 902 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]); 903 } 904 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); 905 906 /* fall through */ 907 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 908 case SNDRV_PCM_TRIGGER_RESUME: 909 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL); 910 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0, 911 &emu->efx_pcm_mixer[0]); 912 for (i = 1; i < NUM_EFX_PLAYBACK; i++) 913 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0, 914 &emu->efx_pcm_mixer[i]); 915 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0); 916 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1); 917 for (i = 1; i < NUM_EFX_PLAYBACK; i++) 918 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0); 919 epcm->running = 1; 920 break; 921 case SNDRV_PCM_TRIGGER_SUSPEND: 922 case SNDRV_PCM_TRIGGER_STOP: 923 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 924 epcm->running = 0; 925 for (i = 0; i < NUM_EFX_PLAYBACK; i++) { 926 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]); 927 } 928 snd_emu10k1_playback_stop_voice(emu, epcm->extra); 929 break; 930 default: 931 result = -EINVAL; 932 break; 933 } 934 spin_unlock(&emu->reg_lock); 935 return result; 936 } 937 938 939 static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream) 940 { 941 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 942 struct snd_pcm_runtime *runtime = substream->runtime; 943 struct snd_emu10k1_pcm *epcm = runtime->private_data; 944 unsigned int ptr; 945 946 if (!epcm->running) 947 return 0; 948 if (epcm->first_ptr) { 949 udelay(50); /* hack, it takes awhile until capture is started */ 950 epcm->first_ptr = 0; 951 } 952 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff; 953 return bytes_to_frames(runtime, ptr); 954 } 955 956 /* 957 * Playback support device description 958 */ 959 960 static const struct snd_pcm_hardware snd_emu10k1_playback = 961 { 962 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 963 SNDRV_PCM_INFO_BLOCK_TRANSFER | 964 SNDRV_PCM_INFO_RESUME | 965 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE), 966 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, 967 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000, 968 .rate_min = 4000, 969 .rate_max = 96000, 970 .channels_min = 1, 971 .channels_max = 2, 972 .buffer_bytes_max = (128*1024), 973 .period_bytes_min = 64, 974 .period_bytes_max = (128*1024), 975 .periods_min = 1, 976 .periods_max = 1024, 977 .fifo_size = 0, 978 }; 979 980 /* 981 * Capture support device description 982 */ 983 984 static const struct snd_pcm_hardware snd_emu10k1_capture = 985 { 986 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 987 SNDRV_PCM_INFO_BLOCK_TRANSFER | 988 SNDRV_PCM_INFO_RESUME | 989 SNDRV_PCM_INFO_MMAP_VALID), 990 .formats = SNDRV_PCM_FMTBIT_S16_LE, 991 .rates = SNDRV_PCM_RATE_8000_48000, 992 .rate_min = 8000, 993 .rate_max = 48000, 994 .channels_min = 1, 995 .channels_max = 2, 996 .buffer_bytes_max = (64*1024), 997 .period_bytes_min = 384, 998 .period_bytes_max = (64*1024), 999 .periods_min = 2, 1000 .periods_max = 2, 1001 .fifo_size = 0, 1002 }; 1003 1004 static const struct snd_pcm_hardware snd_emu10k1_capture_efx = 1005 { 1006 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 1007 SNDRV_PCM_INFO_BLOCK_TRANSFER | 1008 SNDRV_PCM_INFO_RESUME | 1009 SNDRV_PCM_INFO_MMAP_VALID), 1010 .formats = SNDRV_PCM_FMTBIT_S16_LE, 1011 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | 1012 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | 1013 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000, 1014 .rate_min = 44100, 1015 .rate_max = 192000, 1016 .channels_min = 8, 1017 .channels_max = 8, 1018 .buffer_bytes_max = (64*1024), 1019 .period_bytes_min = 384, 1020 .period_bytes_max = (64*1024), 1021 .periods_min = 2, 1022 .periods_max = 2, 1023 .fifo_size = 0, 1024 }; 1025 1026 /* 1027 * 1028 */ 1029 1030 static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate) 1031 { 1032 struct snd_ctl_elem_id id; 1033 1034 if (! kctl) 1035 return; 1036 if (activate) 1037 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; 1038 else 1039 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE; 1040 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE | 1041 SNDRV_CTL_EVENT_MASK_INFO, 1042 snd_ctl_build_ioff(&id, kctl, idx)); 1043 } 1044 1045 static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate) 1046 { 1047 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate); 1048 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate); 1049 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate); 1050 } 1051 1052 static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate) 1053 { 1054 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate); 1055 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate); 1056 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate); 1057 } 1058 1059 static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime) 1060 { 1061 kfree(runtime->private_data); 1062 } 1063 1064 static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream) 1065 { 1066 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1067 struct snd_emu10k1_pcm_mixer *mix; 1068 int i; 1069 1070 for (i = 0; i < NUM_EFX_PLAYBACK; i++) { 1071 mix = &emu->efx_pcm_mixer[i]; 1072 mix->epcm = NULL; 1073 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0); 1074 } 1075 return 0; 1076 } 1077 1078 static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream) 1079 { 1080 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1081 struct snd_emu10k1_pcm *epcm; 1082 struct snd_emu10k1_pcm_mixer *mix; 1083 struct snd_pcm_runtime *runtime = substream->runtime; 1084 int i; 1085 1086 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL); 1087 if (epcm == NULL) 1088 return -ENOMEM; 1089 epcm->emu = emu; 1090 epcm->type = PLAYBACK_EFX; 1091 epcm->substream = substream; 1092 1093 emu->pcm_playback_efx_substream = substream; 1094 1095 runtime->private_data = epcm; 1096 runtime->private_free = snd_emu10k1_pcm_free_substream; 1097 runtime->hw = snd_emu10k1_efx_playback; 1098 1099 for (i = 0; i < NUM_EFX_PLAYBACK; i++) { 1100 mix = &emu->efx_pcm_mixer[i]; 1101 mix->send_routing[0][0] = i; 1102 memset(&mix->send_volume, 0, sizeof(mix->send_volume)); 1103 mix->send_volume[0][0] = 255; 1104 mix->attn[0] = 0xffff; 1105 mix->epcm = epcm; 1106 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1); 1107 } 1108 return 0; 1109 } 1110 1111 static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream) 1112 { 1113 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1114 struct snd_emu10k1_pcm *epcm; 1115 struct snd_emu10k1_pcm_mixer *mix; 1116 struct snd_pcm_runtime *runtime = substream->runtime; 1117 int i, err, sample_rate; 1118 1119 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL); 1120 if (epcm == NULL) 1121 return -ENOMEM; 1122 epcm->emu = emu; 1123 epcm->type = PLAYBACK_EMUVOICE; 1124 epcm->substream = substream; 1125 runtime->private_data = epcm; 1126 runtime->private_free = snd_emu10k1_pcm_free_substream; 1127 runtime->hw = snd_emu10k1_playback; 1128 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) { 1129 kfree(epcm); 1130 return err; 1131 } 1132 if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) { 1133 kfree(epcm); 1134 return err; 1135 } 1136 if (emu->card_capabilities->emu_model && emu->emu1010.internal_clock == 0) 1137 sample_rate = 44100; 1138 else 1139 sample_rate = 48000; 1140 err = snd_pcm_hw_rule_noresample(runtime, sample_rate); 1141 if (err < 0) { 1142 kfree(epcm); 1143 return err; 1144 } 1145 mix = &emu->pcm_mixer[substream->number]; 1146 for (i = 0; i < 4; i++) 1147 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i; 1148 memset(&mix->send_volume, 0, sizeof(mix->send_volume)); 1149 mix->send_volume[0][0] = mix->send_volume[0][1] = 1150 mix->send_volume[1][0] = mix->send_volume[2][1] = 255; 1151 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff; 1152 mix->epcm = epcm; 1153 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1); 1154 return 0; 1155 } 1156 1157 static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream) 1158 { 1159 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1160 struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number]; 1161 1162 mix->epcm = NULL; 1163 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0); 1164 return 0; 1165 } 1166 1167 static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream) 1168 { 1169 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1170 struct snd_pcm_runtime *runtime = substream->runtime; 1171 struct snd_emu10k1_pcm *epcm; 1172 1173 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL); 1174 if (epcm == NULL) 1175 return -ENOMEM; 1176 epcm->emu = emu; 1177 epcm->type = CAPTURE_AC97ADC; 1178 epcm->substream = substream; 1179 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL; 1180 epcm->capture_inte = INTE_ADCBUFENABLE; 1181 epcm->capture_ba_reg = ADCBA; 1182 epcm->capture_bs_reg = ADCBS; 1183 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX; 1184 runtime->private_data = epcm; 1185 runtime->private_free = snd_emu10k1_pcm_free_substream; 1186 runtime->hw = snd_emu10k1_capture; 1187 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt; 1188 emu->pcm_capture_substream = substream; 1189 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes); 1190 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates); 1191 return 0; 1192 } 1193 1194 static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream) 1195 { 1196 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1197 1198 emu->capture_interrupt = NULL; 1199 emu->pcm_capture_substream = NULL; 1200 return 0; 1201 } 1202 1203 static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream) 1204 { 1205 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1206 struct snd_emu10k1_pcm *epcm; 1207 struct snd_pcm_runtime *runtime = substream->runtime; 1208 1209 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL); 1210 if (epcm == NULL) 1211 return -ENOMEM; 1212 epcm->emu = emu; 1213 epcm->type = CAPTURE_AC97MIC; 1214 epcm->substream = substream; 1215 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL; 1216 epcm->capture_inte = INTE_MICBUFENABLE; 1217 epcm->capture_ba_reg = MICBA; 1218 epcm->capture_bs_reg = MICBS; 1219 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX; 1220 substream->runtime->private_data = epcm; 1221 substream->runtime->private_free = snd_emu10k1_pcm_free_substream; 1222 runtime->hw = snd_emu10k1_capture; 1223 runtime->hw.rates = SNDRV_PCM_RATE_8000; 1224 runtime->hw.rate_min = runtime->hw.rate_max = 8000; 1225 runtime->hw.channels_min = 1; 1226 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt; 1227 emu->pcm_capture_mic_substream = substream; 1228 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes); 1229 return 0; 1230 } 1231 1232 static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream) 1233 { 1234 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1235 1236 emu->capture_interrupt = NULL; 1237 emu->pcm_capture_mic_substream = NULL; 1238 return 0; 1239 } 1240 1241 static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream) 1242 { 1243 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1244 struct snd_emu10k1_pcm *epcm; 1245 struct snd_pcm_runtime *runtime = substream->runtime; 1246 int nefx = emu->audigy ? 64 : 32; 1247 int idx; 1248 1249 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL); 1250 if (epcm == NULL) 1251 return -ENOMEM; 1252 epcm->emu = emu; 1253 epcm->type = CAPTURE_EFX; 1254 epcm->substream = substream; 1255 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL; 1256 epcm->capture_inte = INTE_EFXBUFENABLE; 1257 epcm->capture_ba_reg = FXBA; 1258 epcm->capture_bs_reg = FXBS; 1259 epcm->capture_idx_reg = FXIDX; 1260 substream->runtime->private_data = epcm; 1261 substream->runtime->private_free = snd_emu10k1_pcm_free_substream; 1262 runtime->hw = snd_emu10k1_capture_efx; 1263 runtime->hw.rates = SNDRV_PCM_RATE_48000; 1264 runtime->hw.rate_min = runtime->hw.rate_max = 48000; 1265 spin_lock_irq(&emu->reg_lock); 1266 if (emu->card_capabilities->emu_model) { 1267 /* Nb. of channels has been increased to 16 */ 1268 /* TODO 1269 * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE 1270 * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | 1271 * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | 1272 * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000 1273 * rate_min = 44100, 1274 * rate_max = 192000, 1275 * channels_min = 16, 1276 * channels_max = 16, 1277 * Need to add mixer control to fix sample rate 1278 * 1279 * There are 32 mono channels of 16bits each. 1280 * 24bit Audio uses 2x channels over 16bit 1281 * 96kHz uses 2x channels over 48kHz 1282 * 192kHz uses 4x channels over 48kHz 1283 * So, for 48kHz 24bit, one has 16 channels 1284 * for 96kHz 24bit, one has 8 channels 1285 * for 192kHz 24bit, one has 4 channels 1286 * 1287 */ 1288 #if 1 1289 switch (emu->emu1010.internal_clock) { 1290 case 0: 1291 /* For 44.1kHz */ 1292 runtime->hw.rates = SNDRV_PCM_RATE_44100; 1293 runtime->hw.rate_min = runtime->hw.rate_max = 44100; 1294 runtime->hw.channels_min = 1295 runtime->hw.channels_max = 16; 1296 break; 1297 case 1: 1298 /* For 48kHz */ 1299 runtime->hw.rates = SNDRV_PCM_RATE_48000; 1300 runtime->hw.rate_min = runtime->hw.rate_max = 48000; 1301 runtime->hw.channels_min = 1302 runtime->hw.channels_max = 16; 1303 break; 1304 } 1305 #endif 1306 #if 0 1307 /* For 96kHz */ 1308 runtime->hw.rates = SNDRV_PCM_RATE_96000; 1309 runtime->hw.rate_min = runtime->hw.rate_max = 96000; 1310 runtime->hw.channels_min = runtime->hw.channels_max = 4; 1311 #endif 1312 #if 0 1313 /* For 192kHz */ 1314 runtime->hw.rates = SNDRV_PCM_RATE_192000; 1315 runtime->hw.rate_min = runtime->hw.rate_max = 192000; 1316 runtime->hw.channels_min = runtime->hw.channels_max = 2; 1317 #endif 1318 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE; 1319 /* efx_voices_mask[0] is expected to be zero 1320 * efx_voices_mask[1] is expected to have 32bits set 1321 */ 1322 } else { 1323 runtime->hw.channels_min = runtime->hw.channels_max = 0; 1324 for (idx = 0; idx < nefx; idx++) { 1325 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) { 1326 runtime->hw.channels_min++; 1327 runtime->hw.channels_max++; 1328 } 1329 } 1330 } 1331 epcm->capture_cr_val = emu->efx_voices_mask[0]; 1332 epcm->capture_cr_val2 = emu->efx_voices_mask[1]; 1333 spin_unlock_irq(&emu->reg_lock); 1334 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt; 1335 emu->pcm_capture_efx_substream = substream; 1336 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes); 1337 return 0; 1338 } 1339 1340 static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream) 1341 { 1342 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1343 1344 emu->capture_interrupt = NULL; 1345 emu->pcm_capture_efx_substream = NULL; 1346 return 0; 1347 } 1348 1349 static const struct snd_pcm_ops snd_emu10k1_playback_ops = { 1350 .open = snd_emu10k1_playback_open, 1351 .close = snd_emu10k1_playback_close, 1352 .ioctl = snd_pcm_lib_ioctl, 1353 .hw_params = snd_emu10k1_playback_hw_params, 1354 .hw_free = snd_emu10k1_playback_hw_free, 1355 .prepare = snd_emu10k1_playback_prepare, 1356 .trigger = snd_emu10k1_playback_trigger, 1357 .pointer = snd_emu10k1_playback_pointer, 1358 }; 1359 1360 static const struct snd_pcm_ops snd_emu10k1_capture_ops = { 1361 .open = snd_emu10k1_capture_open, 1362 .close = snd_emu10k1_capture_close, 1363 .ioctl = snd_pcm_lib_ioctl, 1364 .prepare = snd_emu10k1_capture_prepare, 1365 .trigger = snd_emu10k1_capture_trigger, 1366 .pointer = snd_emu10k1_capture_pointer, 1367 }; 1368 1369 /* EFX playback */ 1370 static const struct snd_pcm_ops snd_emu10k1_efx_playback_ops = { 1371 .open = snd_emu10k1_efx_playback_open, 1372 .close = snd_emu10k1_efx_playback_close, 1373 .ioctl = snd_pcm_lib_ioctl, 1374 .hw_params = snd_emu10k1_playback_hw_params, 1375 .hw_free = snd_emu10k1_efx_playback_hw_free, 1376 .prepare = snd_emu10k1_efx_playback_prepare, 1377 .trigger = snd_emu10k1_efx_playback_trigger, 1378 .pointer = snd_emu10k1_efx_playback_pointer, 1379 }; 1380 1381 int snd_emu10k1_pcm(struct snd_emu10k1 *emu, int device) 1382 { 1383 struct snd_pcm *pcm; 1384 struct snd_pcm_substream *substream; 1385 int err; 1386 1387 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0) 1388 return err; 1389 1390 pcm->private_data = emu; 1391 1392 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops); 1393 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops); 1394 1395 pcm->info_flags = 0; 1396 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX; 1397 strcpy(pcm->name, "ADC Capture/Standard PCM Playback"); 1398 emu->pcm = pcm; 1399 1400 /* playback substream can't use managed buffers due to alignment */ 1401 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) 1402 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, 1403 &emu->pci->dev, 1404 64*1024, 64*1024); 1405 1406 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) 1407 snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV, 1408 &emu->pci->dev, 64*1024, 64*1024); 1409 1410 return 0; 1411 } 1412 1413 int snd_emu10k1_pcm_multi(struct snd_emu10k1 *emu, int device) 1414 { 1415 struct snd_pcm *pcm; 1416 struct snd_pcm_substream *substream; 1417 int err; 1418 1419 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0) 1420 return err; 1421 1422 pcm->private_data = emu; 1423 1424 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops); 1425 1426 pcm->info_flags = 0; 1427 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX; 1428 strcpy(pcm->name, "Multichannel Playback"); 1429 emu->pcm_multi = pcm; 1430 1431 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) 1432 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, 1433 &emu->pci->dev, 1434 64*1024, 64*1024); 1435 1436 return 0; 1437 } 1438 1439 1440 static const struct snd_pcm_ops snd_emu10k1_capture_mic_ops = { 1441 .open = snd_emu10k1_capture_mic_open, 1442 .close = snd_emu10k1_capture_mic_close, 1443 .ioctl = snd_pcm_lib_ioctl, 1444 .prepare = snd_emu10k1_capture_prepare, 1445 .trigger = snd_emu10k1_capture_trigger, 1446 .pointer = snd_emu10k1_capture_pointer, 1447 }; 1448 1449 int snd_emu10k1_pcm_mic(struct snd_emu10k1 *emu, int device) 1450 { 1451 struct snd_pcm *pcm; 1452 int err; 1453 1454 if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0) 1455 return err; 1456 1457 pcm->private_data = emu; 1458 1459 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops); 1460 1461 pcm->info_flags = 0; 1462 strcpy(pcm->name, "Mic Capture"); 1463 emu->pcm_mic = pcm; 1464 1465 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev, 1466 64*1024, 64*1024); 1467 1468 return 0; 1469 } 1470 1471 static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 1472 { 1473 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol); 1474 int nefx = emu->audigy ? 64 : 32; 1475 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 1476 uinfo->count = nefx; 1477 uinfo->value.integer.min = 0; 1478 uinfo->value.integer.max = 1; 1479 return 0; 1480 } 1481 1482 static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 1483 { 1484 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol); 1485 int nefx = emu->audigy ? 64 : 32; 1486 int idx; 1487 1488 spin_lock_irq(&emu->reg_lock); 1489 for (idx = 0; idx < nefx; idx++) 1490 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0; 1491 spin_unlock_irq(&emu->reg_lock); 1492 return 0; 1493 } 1494 1495 static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 1496 { 1497 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol); 1498 unsigned int nval[2], bits; 1499 int nefx = emu->audigy ? 64 : 32; 1500 int nefxb = emu->audigy ? 7 : 6; 1501 int change, idx; 1502 1503 nval[0] = nval[1] = 0; 1504 for (idx = 0, bits = 0; idx < nefx; idx++) 1505 if (ucontrol->value.integer.value[idx]) { 1506 nval[idx / 32] |= 1 << (idx % 32); 1507 bits++; 1508 } 1509 1510 for (idx = 0; idx < nefxb; idx++) 1511 if (1 << idx == bits) 1512 break; 1513 1514 if (idx >= nefxb) 1515 return -EINVAL; 1516 1517 spin_lock_irq(&emu->reg_lock); 1518 change = (nval[0] != emu->efx_voices_mask[0]) || 1519 (nval[1] != emu->efx_voices_mask[1]); 1520 emu->efx_voices_mask[0] = nval[0]; 1521 emu->efx_voices_mask[1] = nval[1]; 1522 spin_unlock_irq(&emu->reg_lock); 1523 return change; 1524 } 1525 1526 static const struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = { 1527 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1528 .name = "Captured FX8010 Outputs", 1529 .info = snd_emu10k1_pcm_efx_voices_mask_info, 1530 .get = snd_emu10k1_pcm_efx_voices_mask_get, 1531 .put = snd_emu10k1_pcm_efx_voices_mask_put 1532 }; 1533 1534 static const struct snd_pcm_ops snd_emu10k1_capture_efx_ops = { 1535 .open = snd_emu10k1_capture_efx_open, 1536 .close = snd_emu10k1_capture_efx_close, 1537 .ioctl = snd_pcm_lib_ioctl, 1538 .prepare = snd_emu10k1_capture_prepare, 1539 .trigger = snd_emu10k1_capture_trigger, 1540 .pointer = snd_emu10k1_capture_pointer, 1541 }; 1542 1543 1544 /* EFX playback */ 1545 1546 #define INITIAL_TRAM_SHIFT 14 1547 #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1) 1548 1549 static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data) 1550 { 1551 struct snd_pcm_substream *substream = private_data; 1552 snd_pcm_period_elapsed(substream); 1553 } 1554 1555 static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left, 1556 unsigned short *dst_right, 1557 unsigned short *src, 1558 unsigned int count, 1559 unsigned int tram_shift) 1560 { 1561 /* 1562 dev_dbg(emu->card->dev, 1563 "tram_poke1: dst_left = 0x%p, dst_right = 0x%p, " 1564 "src = 0x%p, count = 0x%x\n", 1565 dst_left, dst_right, src, count); 1566 */ 1567 if ((tram_shift & 1) == 0) { 1568 while (count--) { 1569 *dst_left-- = *src++; 1570 *dst_right-- = *src++; 1571 } 1572 } else { 1573 while (count--) { 1574 *dst_right-- = *src++; 1575 *dst_left-- = *src++; 1576 } 1577 } 1578 } 1579 1580 static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream, 1581 struct snd_pcm_indirect *rec, size_t bytes) 1582 { 1583 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1584 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number]; 1585 unsigned int tram_size = pcm->buffer_size; 1586 unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data); 1587 unsigned int frames = bytes >> 2, count; 1588 unsigned int tram_pos = pcm->tram_pos; 1589 unsigned int tram_shift = pcm->tram_shift; 1590 1591 while (frames > tram_pos) { 1592 count = tram_pos + 1; 1593 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos, 1594 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2, 1595 src, count, tram_shift); 1596 src += count * 2; 1597 frames -= count; 1598 tram_pos = (tram_size / 2) - 1; 1599 tram_shift++; 1600 } 1601 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos, 1602 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2, 1603 src, frames, tram_shift); 1604 tram_pos -= frames; 1605 pcm->tram_pos = tram_pos; 1606 pcm->tram_shift = tram_shift; 1607 } 1608 1609 static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream) 1610 { 1611 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1612 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number]; 1613 1614 return snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, 1615 fx8010_pb_trans_copy); 1616 } 1617 1618 static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream) 1619 { 1620 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1621 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number]; 1622 unsigned int i; 1623 1624 for (i = 0; i < pcm->channels; i++) 1625 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0); 1626 return 0; 1627 } 1628 1629 static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream) 1630 { 1631 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1632 struct snd_pcm_runtime *runtime = substream->runtime; 1633 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number]; 1634 unsigned int i; 1635 1636 /* 1637 dev_dbg(emu->card->dev, "prepare: etram_pages = 0x%p, dma_area = 0x%x, " 1638 "buffer_size = 0x%x (0x%x)\n", 1639 emu->fx8010.etram_pages, runtime->dma_area, 1640 runtime->buffer_size, runtime->buffer_size << 2); 1641 */ 1642 memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec)); 1643 pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */ 1644 pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream); 1645 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size); 1646 pcm->tram_shift = 0; 1647 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */ 1648 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */ 1649 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size); 1650 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */ 1651 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size); 1652 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size); 1653 for (i = 0; i < pcm->channels; i++) 1654 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels)); 1655 return 0; 1656 } 1657 1658 static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd) 1659 { 1660 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1661 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number]; 1662 int result = 0; 1663 1664 spin_lock(&emu->reg_lock); 1665 switch (cmd) { 1666 case SNDRV_PCM_TRIGGER_START: 1667 /* follow thru */ 1668 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 1669 case SNDRV_PCM_TRIGGER_RESUME: 1670 #ifdef EMU10K1_SET_AC3_IEC958 1671 { 1672 int i; 1673 for (i = 0; i < 3; i++) { 1674 unsigned int bits; 1675 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 | 1676 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS | 1677 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA; 1678 snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits); 1679 } 1680 } 1681 #endif 1682 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq); 1683 if (result < 0) 1684 goto __err; 1685 snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */ 1686 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1); 1687 break; 1688 case SNDRV_PCM_TRIGGER_STOP: 1689 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 1690 case SNDRV_PCM_TRIGGER_SUSPEND: 1691 snd_emu10k1_fx8010_unregister_irq_handler(emu, &pcm->irq); 1692 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); 1693 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size); 1694 pcm->tram_shift = 0; 1695 break; 1696 default: 1697 result = -EINVAL; 1698 break; 1699 } 1700 __err: 1701 spin_unlock(&emu->reg_lock); 1702 return result; 1703 } 1704 1705 static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream) 1706 { 1707 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1708 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number]; 1709 size_t ptr; /* byte pointer */ 1710 1711 if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0)) 1712 return 0; 1713 ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2; 1714 return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr); 1715 } 1716 1717 static const struct snd_pcm_hardware snd_emu10k1_fx8010_playback = 1718 { 1719 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 1720 SNDRV_PCM_INFO_RESUME | 1721 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE | 1722 SNDRV_PCM_INFO_SYNC_APPLPTR), 1723 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, 1724 .rates = SNDRV_PCM_RATE_48000, 1725 .rate_min = 48000, 1726 .rate_max = 48000, 1727 .channels_min = 1, 1728 .channels_max = 1, 1729 .buffer_bytes_max = (128*1024), 1730 .period_bytes_min = 1024, 1731 .period_bytes_max = (128*1024), 1732 .periods_min = 2, 1733 .periods_max = 1024, 1734 .fifo_size = 0, 1735 }; 1736 1737 static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream) 1738 { 1739 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1740 struct snd_pcm_runtime *runtime = substream->runtime; 1741 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number]; 1742 1743 runtime->hw = snd_emu10k1_fx8010_playback; 1744 runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels; 1745 runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2; 1746 spin_lock_irq(&emu->reg_lock); 1747 if (pcm->valid == 0) { 1748 spin_unlock_irq(&emu->reg_lock); 1749 return -ENODEV; 1750 } 1751 pcm->opened = 1; 1752 spin_unlock_irq(&emu->reg_lock); 1753 return 0; 1754 } 1755 1756 static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream) 1757 { 1758 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream); 1759 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number]; 1760 1761 spin_lock_irq(&emu->reg_lock); 1762 pcm->opened = 0; 1763 spin_unlock_irq(&emu->reg_lock); 1764 return 0; 1765 } 1766 1767 static const struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = { 1768 .open = snd_emu10k1_fx8010_playback_open, 1769 .close = snd_emu10k1_fx8010_playback_close, 1770 .ioctl = snd_pcm_lib_ioctl, 1771 .hw_free = snd_emu10k1_fx8010_playback_hw_free, 1772 .prepare = snd_emu10k1_fx8010_playback_prepare, 1773 .trigger = snd_emu10k1_fx8010_playback_trigger, 1774 .pointer = snd_emu10k1_fx8010_playback_pointer, 1775 .ack = snd_emu10k1_fx8010_playback_transfer, 1776 }; 1777 1778 int snd_emu10k1_pcm_efx(struct snd_emu10k1 *emu, int device) 1779 { 1780 struct snd_pcm *pcm; 1781 struct snd_kcontrol *kctl; 1782 int err; 1783 1784 if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0) 1785 return err; 1786 1787 pcm->private_data = emu; 1788 1789 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops); 1790 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops); 1791 1792 pcm->info_flags = 0; 1793 strcpy(pcm->name, "Multichannel Capture/PT Playback"); 1794 emu->pcm_efx = pcm; 1795 1796 /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs 1797 * to these 1798 */ 1799 1800 /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */ 1801 if (emu->audigy) { 1802 emu->efx_voices_mask[0] = 0; 1803 if (emu->card_capabilities->emu_model) 1804 /* Pavel Hofman - 32 voices will be used for 1805 * capture (write mode) - 1806 * each bit = corresponding voice 1807 */ 1808 emu->efx_voices_mask[1] = 0xffffffff; 1809 else 1810 emu->efx_voices_mask[1] = 0xffff; 1811 } else { 1812 emu->efx_voices_mask[0] = 0xffff0000; 1813 emu->efx_voices_mask[1] = 0; 1814 } 1815 /* For emu1010, the control has to set 32 upper bits (voices) 1816 * out of the 64 bits (voices) to true for the 16-channels capture 1817 * to work correctly. Correct A_FXWC2 initial value (0xffffffff) 1818 * is already defined but the snd_emu10k1_pcm_efx_voices_mask 1819 * control can override this register's value. 1820 */ 1821 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu); 1822 if (!kctl) 1823 return -ENOMEM; 1824 kctl->id.device = device; 1825 err = snd_ctl_add(emu->card, kctl); 1826 if (err < 0) 1827 return err; 1828 1829 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev, 1830 64*1024, 64*1024); 1831 1832 return 0; 1833 } 1834