1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Lee Revell <rlrevell@joe-job.com>
5 * James Courtier-Dutton <James@superbug.co.uk>
6 * Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
9 * Routines for control of EMU10K1 chips / PCM routines
12 #include <linux/pci.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/time.h>
16 #include <linux/init.h>
17 #include <sound/core.h>
18 #include <sound/emu10k1.h>
20 static void snd_emu10k1_pcm_interrupt(struct snd_emu10k1 *emu,
21 struct snd_emu10k1_voice *voice)
23 struct snd_emu10k1_pcm *epcm;
28 if (epcm->substream == NULL)
31 dev_dbg(emu->card->dev,
32 "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
33 epcm->substream->runtime->hw->pointer(emu, epcm->substream),
34 snd_pcm_lib_period_bytes(epcm->substream),
35 snd_pcm_lib_buffer_bytes(epcm->substream));
37 snd_pcm_period_elapsed(epcm->substream);
40 static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu,
44 if (status & IPR_ADCBUFHALFFULL) {
45 if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
49 snd_pcm_period_elapsed(emu->pcm_capture_substream);
52 static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu,
56 if (status & IPR_MICBUFHALFFULL) {
57 if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
61 snd_pcm_period_elapsed(emu->pcm_capture_mic_substream);
64 static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu,
68 if (status & IPR_EFXBUFHALFFULL) {
69 if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
73 snd_pcm_period_elapsed(emu->pcm_capture_efx_substream);
76 static void snd_emu10k1_pcm_free_voices(struct snd_emu10k1_pcm *epcm)
78 for (unsigned i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
79 if (epcm->voices[i]) {
80 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
81 epcm->voices[i] = NULL;
86 static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm *epcm,
87 int type, int count, int channels)
91 snd_emu10k1_pcm_free_voices(epcm);
93 err = snd_emu10k1_voice_alloc(epcm->emu,
94 type, count, channels,
95 epcm, &epcm->voices[0]);
99 if (epcm->extra == NULL) {
100 // The hardware supports only (half-)loop interrupts, so to support an
101 // arbitrary number of periods per buffer, we use an extra voice with a
102 // period-sized loop as the interrupt source. Additionally, the interrupt
103 // timing of the hardware is "suboptimal" and needs some compensation.
104 err = snd_emu10k1_voice_alloc(epcm->emu,
109 dev_dbg(emu->card->dev, "pcm_channel_alloc: "
110 "failed extra: voices=%d, frame=%d\n",
113 snd_emu10k1_pcm_free_voices(epcm);
116 epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
122 // Primes 2-7 and 2^n multiples thereof, up to 16.
123 static const unsigned int efx_capture_channels[] = {
124 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16
127 static const struct snd_pcm_hw_constraint_list hw_constraints_efx_capture_channels = {
128 .count = ARRAY_SIZE(efx_capture_channels),
129 .list = efx_capture_channels,
133 static const unsigned int capture_buffer_sizes[31] = {
135 384*2, 448*2, 512*2, 640*2,
136 384*4, 448*4, 512*4, 640*4,
137 384*8, 448*8, 512*8, 640*8,
138 384*16, 448*16, 512*16, 640*16,
139 384*32, 448*32, 512*32, 640*32,
140 384*64, 448*64, 512*64, 640*64,
141 384*128,448*128,512*128
144 static const struct snd_pcm_hw_constraint_list hw_constraints_capture_buffer_sizes = {
146 .list = capture_buffer_sizes,
150 static const unsigned int capture_rates[8] = {
151 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
154 static const struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
156 .list = capture_rates,
160 static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
163 case 8000: return ADCCR_SAMPLERATE_8;
164 case 11025: return ADCCR_SAMPLERATE_11;
165 case 16000: return ADCCR_SAMPLERATE_16;
166 case 22050: return ADCCR_SAMPLERATE_22;
167 case 24000: return ADCCR_SAMPLERATE_24;
168 case 32000: return ADCCR_SAMPLERATE_32;
169 case 44100: return ADCCR_SAMPLERATE_44;
170 case 48000: return ADCCR_SAMPLERATE_48;
173 return ADCCR_SAMPLERATE_8;
177 static const unsigned int audigy_capture_rates[9] = {
178 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
181 static const struct snd_pcm_hw_constraint_list hw_constraints_audigy_capture_rates = {
183 .list = audigy_capture_rates,
187 static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
190 case 8000: return A_ADCCR_SAMPLERATE_8;
191 case 11025: return A_ADCCR_SAMPLERATE_11;
192 case 12000: return A_ADCCR_SAMPLERATE_12;
193 case 16000: return ADCCR_SAMPLERATE_16;
194 case 22050: return ADCCR_SAMPLERATE_22;
195 case 24000: return ADCCR_SAMPLERATE_24;
196 case 32000: return ADCCR_SAMPLERATE_32;
197 case 44100: return ADCCR_SAMPLERATE_44;
198 case 48000: return ADCCR_SAMPLERATE_48;
201 return A_ADCCR_SAMPLERATE_8;
205 static void snd_emu10k1_constrain_capture_rates(struct snd_emu10k1 *emu,
206 struct snd_pcm_runtime *runtime)
208 if (emu->card_capabilities->emu_model &&
209 emu->emu1010.word_clock == 44100) {
210 // This also sets the rate constraint by deleting SNDRV_PCM_RATE_KNOT
211 runtime->hw.rates = SNDRV_PCM_RATE_11025 | \
212 SNDRV_PCM_RATE_22050 | \
213 SNDRV_PCM_RATE_44100;
214 runtime->hw.rate_min = 11025;
215 runtime->hw.rate_max = 44100;
218 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
219 emu->audigy ? &hw_constraints_audigy_capture_rates :
220 &hw_constraints_capture_rates);
223 static void snd_emu1010_constrain_efx_rate(struct snd_emu10k1 *emu,
224 struct snd_pcm_runtime *runtime)
228 rate = emu->emu1010.word_clock;
229 runtime->hw.rate_min = runtime->hw.rate_max = rate;
230 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
233 static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
235 unsigned int pitch_target;
237 pitch_target = (rate << 8) / 375;
238 pitch_target = (pitch_target >> 1) + (pitch_target & 1);
242 #define PITCH_48000 0x00004000
243 #define PITCH_96000 0x00008000
244 #define PITCH_85000 0x00007155
245 #define PITCH_80726 0x00006ba2
246 #define PITCH_67882 0x00005a82
247 #define PITCH_57081 0x00004c1c
249 static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
251 if (pitch_target == PITCH_48000)
252 return CCCA_INTERPROM_0;
253 else if (pitch_target < PITCH_48000)
254 return CCCA_INTERPROM_1;
255 else if (pitch_target >= PITCH_96000)
256 return CCCA_INTERPROM_0;
257 else if (pitch_target >= PITCH_85000)
258 return CCCA_INTERPROM_6;
259 else if (pitch_target >= PITCH_80726)
260 return CCCA_INTERPROM_5;
261 else if (pitch_target >= PITCH_67882)
262 return CCCA_INTERPROM_4;
263 else if (pitch_target >= PITCH_57081)
264 return CCCA_INTERPROM_3;
266 return CCCA_INTERPROM_2;
269 static u16 emu10k1_send_target_from_amount(u8 amount)
271 static const u8 shifts[8] = { 4, 4, 5, 6, 7, 8, 9, 10 };
272 static const u16 offsets[8] = { 0, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000 };
278 return ((amount & 0x1f) << shifts[exp]) + offsets[exp];
281 static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
282 struct snd_emu10k1_voice *evoice,
283 bool w_16, bool stereo,
284 unsigned int start_addr,
285 unsigned int end_addr,
286 const unsigned char *send_routing,
287 const unsigned char *send_amount)
289 unsigned int silent_page;
292 voice = evoice->number;
294 silent_page = ((unsigned int)emu->silent_page.addr << emu->address_mode) |
295 (emu->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
296 snd_emu10k1_ptr_write_multiple(emu, voice,
297 // Not really necessary for the slave, but it doesn't hurt
298 CPF, stereo ? CPF_STEREO_MASK : 0,
299 // Assumption that PT is already 0 so no harm overwriting
300 PTRX, (send_amount[0] << 8) | send_amount[1],
301 // Stereo slaves don't need to have the addresses set, but it doesn't hurt
302 DSL, end_addr | (send_amount[3] << 24),
303 PSST, start_addr | (send_amount[2] << 24),
304 CCCA, emu10k1_select_interprom(evoice->epcm->pitch_target) |
305 (w_16 ? 0 : CCCA_8BITSELECT),
306 // Clear filter delay memory
312 // Disable filter (in conjunction with CCCA_RESONANCE == 0)
313 VTFT, VTFT_FILTERTARGET_MASK,
314 CVCF, CVCF_CURRENTFILTER_MASK,
318 snd_emu10k1_ptr_write_multiple(emu, voice,
319 A_FXRT1, snd_emu10k1_compose_audigy_fxrt1(send_routing),
320 A_FXRT2, snd_emu10k1_compose_audigy_fxrt2(send_routing),
321 A_SENDAMOUNTS, snd_emu10k1_compose_audigy_sendamounts(send_amount),
323 for (int i = 0; i < 4; i++) {
324 u32 aml = emu10k1_send_target_from_amount(send_amount[2 * i]);
325 u32 amh = emu10k1_send_target_from_amount(send_amount[2 * i + 1]);
326 snd_emu10k1_ptr_write(emu, A_CSBA + i, voice, (amh << 16) | aml);
329 snd_emu10k1_ptr_write(emu, FXRT, voice,
330 snd_emu10k1_compose_send_routing(send_routing));
333 emu->voices[voice].dirty = 1;
336 static void snd_emu10k1_pcm_init_voices(struct snd_emu10k1 *emu,
337 struct snd_emu10k1_voice *evoice,
338 bool w_16, bool stereo,
339 unsigned int start_addr,
340 unsigned int end_addr,
341 struct snd_emu10k1_pcm_mixer *mix)
343 spin_lock_irq(&emu->reg_lock);
344 snd_emu10k1_pcm_init_voice(emu, evoice, w_16, stereo,
345 start_addr, end_addr,
346 &mix->send_routing[stereo][0],
347 &mix->send_volume[stereo][0]);
349 snd_emu10k1_pcm_init_voice(emu, evoice + 1, w_16, true,
350 start_addr, end_addr,
351 &mix->send_routing[2][0],
352 &mix->send_volume[2][0]);
353 spin_unlock_irq(&emu->reg_lock);
356 static void snd_emu10k1_pcm_init_extra_voice(struct snd_emu10k1 *emu,
357 struct snd_emu10k1_voice *evoice,
359 unsigned int start_addr,
360 unsigned int end_addr)
362 static const unsigned char send_routing[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
363 static const unsigned char send_amount[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
365 snd_emu10k1_pcm_init_voice(emu, evoice, w_16, false,
366 start_addr, end_addr,
367 send_routing, send_amount);
370 static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
371 struct snd_pcm_hw_params *hw_params)
373 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
374 struct snd_pcm_runtime *runtime = substream->runtime;
375 struct snd_emu10k1_pcm *epcm = runtime->private_data;
377 int type, channels, count;
380 if (epcm->type == PLAYBACK_EMUVOICE) {
383 count = params_channels(hw_params);
386 channels = params_channels(hw_params);
389 err = snd_emu10k1_pcm_channel_alloc(epcm, type, count, channels);
393 alloc_size = params_buffer_bytes(hw_params);
394 if (emu->iommu_workaround)
395 alloc_size += EMUPAGESIZE;
396 err = snd_pcm_lib_malloc_pages(substream, alloc_size);
399 if (emu->iommu_workaround && runtime->dma_bytes >= EMUPAGESIZE)
400 runtime->dma_bytes -= EMUPAGESIZE;
401 if (err > 0) { /* change */
403 if (epcm->memblk != NULL)
404 snd_emu10k1_free_pages(emu, epcm->memblk);
405 epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
406 epcm->start_addr = 0;
409 mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
412 epcm->start_addr = mapped << PAGE_SHIFT;
417 static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
419 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
420 struct snd_pcm_runtime *runtime = substream->runtime;
421 struct snd_emu10k1_pcm *epcm;
423 if (runtime->private_data == NULL)
425 epcm = runtime->private_data;
427 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
430 snd_emu10k1_pcm_free_voices(epcm);
432 snd_emu10k1_free_pages(emu, epcm->memblk);
434 epcm->start_addr = 0;
436 snd_pcm_lib_free_pages(substream);
440 static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
442 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
443 struct snd_pcm_runtime *runtime = substream->runtime;
444 struct snd_emu10k1_pcm *epcm = runtime->private_data;
445 bool w_16 = snd_pcm_format_width(runtime->format) == 16;
446 bool stereo = runtime->channels == 2;
447 unsigned int start_addr, end_addr;
450 rate = runtime->rate;
451 if (emu->card_capabilities->emu_model &&
452 emu->emu1010.word_clock == 44100)
453 rate = rate * 480 / 441;
454 epcm->pitch_target = emu10k1_calc_pitch_target(rate);
456 start_addr = epcm->start_addr >> w_16;
457 end_addr = start_addr + runtime->period_size;
458 snd_emu10k1_pcm_init_extra_voice(emu, epcm->extra, w_16,
459 start_addr, end_addr);
460 start_addr >>= stereo;
461 epcm->ccca_start_addr = start_addr;
462 end_addr = start_addr + runtime->buffer_size;
463 snd_emu10k1_pcm_init_voices(emu, epcm->voices[0], w_16, stereo,
464 start_addr, end_addr,
465 &emu->pcm_mixer[substream->number]);
470 static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
472 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
473 struct snd_pcm_runtime *runtime = substream->runtime;
474 struct snd_emu10k1_pcm *epcm = runtime->private_data;
475 unsigned int start_addr;
476 unsigned int extra_size, channel_size;
479 epcm->pitch_target = PITCH_48000;
481 start_addr = epcm->start_addr >> 1; // 16-bit voices
483 extra_size = runtime->period_size;
484 channel_size = runtime->buffer_size;
486 snd_emu10k1_pcm_init_extra_voice(emu, epcm->extra, true,
487 start_addr, start_addr + extra_size);
489 epcm->ccca_start_addr = start_addr;
490 for (i = 0; i < runtime->channels; i++) {
491 snd_emu10k1_pcm_init_voices(emu, epcm->voices[i], true, false,
492 start_addr, start_addr + channel_size,
493 &emu->efx_pcm_mixer[i]);
494 start_addr += channel_size;
500 static const struct snd_pcm_hardware snd_emu10k1_efx_playback =
502 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
503 SNDRV_PCM_INFO_BLOCK_TRANSFER |
504 SNDRV_PCM_INFO_RESUME |
505 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
506 .formats = SNDRV_PCM_FMTBIT_S16_LE,
507 .rates = SNDRV_PCM_RATE_48000,
511 .channels_max = NUM_EFX_PLAYBACK,
512 .buffer_bytes_max = (128*1024),
513 .period_bytes_max = (128*1024),
519 static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
521 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
522 struct snd_pcm_runtime *runtime = substream->runtime;
523 struct snd_emu10k1_pcm *epcm = runtime->private_data;
526 /* zeroing the buffer size will stop capture */
527 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
528 switch (epcm->type) {
529 case CAPTURE_AC97ADC:
530 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
533 if (emu->card_capabilities->emu_model) {
534 // The upper 32 16-bit capture voices, two for each of the 16 32-bit channels.
535 // The lower voices are occupied by A_EXTOUT_*_CAP*.
536 epcm->capture_cr_val = 0;
537 epcm->capture_cr_val2 = 0xffffffff >> (32 - runtime->channels * 2);
540 snd_emu10k1_ptr_write_multiple(emu, 0,
545 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
550 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
551 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
552 epcm->capture_bs_val = 0;
553 for (idx = 0; idx < 31; idx++) {
554 if (capture_buffer_sizes[idx] == epcm->capture_bufsize) {
555 epcm->capture_bs_val = idx + 1;
559 if (epcm->capture_bs_val == 0) {
561 epcm->capture_bs_val++;
563 if (epcm->type == CAPTURE_AC97ADC) {
564 unsigned rate = runtime->rate;
565 if (!(runtime->hw.rates & SNDRV_PCM_RATE_48000))
566 rate = rate * 480 / 441;
568 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
569 if (runtime->channels > 1)
570 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
571 epcm->capture_cr_val |= emu->audigy ?
572 snd_emu10k1_audigy_capture_rate_reg(rate) :
573 snd_emu10k1_capture_rate_reg(rate);
578 static void snd_emu10k1_playback_fill_cache(struct snd_emu10k1 *emu,
580 u32 sample, bool stereo)
584 // We assume that the cache is resting at this point (i.e.,
585 // CCR_CACHEINVALIDSIZE is very small).
587 // Clear leading frames. For simplicitly, this does too much,
588 // except for 16-bit stereo. And the interpolator will actually
589 // access them at all only when we're pitch-shifting.
590 for (int i = 0; i < 3; i++)
591 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
594 ccr = (64 - 3) << REG_SHIFT(CCR_CACHEINVALIDSIZE);
596 // The engine goes haywire if CCR_READADDRESS is out of sync
597 snd_emu10k1_ptr_write(emu, CCR, voice + 1, ccr);
599 snd_emu10k1_ptr_write(emu, CCR, voice, ccr);
602 static void snd_emu10k1_playback_prepare_voices(struct snd_emu10k1 *emu,
603 struct snd_emu10k1_pcm *epcm,
604 bool w_16, bool stereo,
607 struct snd_pcm_substream *substream = epcm->substream;
608 struct snd_pcm_runtime *runtime = substream->runtime;
609 unsigned eloop_start = epcm->start_addr >> w_16;
610 unsigned loop_start = eloop_start >> stereo;
611 unsigned eloop_size = runtime->period_size;
612 unsigned loop_size = runtime->buffer_size;
613 u32 sample = w_16 ? 0 : 0x80808080;
615 // To make the playback actually start at the 1st frame,
616 // we need to compensate for two circumstances:
617 // - The actual position is delayed by the cache size (64 frames)
618 // - The interpolator is centered around the 4th frame
619 loop_start += (epcm->resume_pos + 64 - 3) % loop_size;
620 for (int i = 0; i < channels; i++) {
621 unsigned voice = epcm->voices[i]->number;
622 snd_emu10k1_ptr_write(emu, CCCA_CURRADDR, voice, loop_start);
623 loop_start += loop_size;
624 snd_emu10k1_playback_fill_cache(emu, voice, sample, stereo);
627 // The interrupt is triggered when CCCA_CURRADDR (CA) wraps around,
628 // which is ahead of the actual playback position, so the interrupt
629 // source needs to be delayed.
631 // In principle, this wouldn't need to be the cache's entire size - in
632 // practice, CCR_CACHEINVALIDSIZE (CIS) > `fetch threshold` has never
633 // been observed, and assuming 40 _bytes_ should be safe.
635 // The cache fills are somewhat random, which makes it impossible to
636 // align them with the interrupts. This makes a non-delayed interrupt
637 // source not practical, as the interrupt handler would have to wait
638 // for (CA - CIS) >= period_boundary for every channel in the stream.
640 // This is why all other (open) drivers for these chips use timer-based
643 eloop_start += (epcm->resume_pos + eloop_size - 3) % eloop_size;
644 snd_emu10k1_ptr_write(emu, CCCA_CURRADDR, epcm->extra->number, eloop_start);
646 // It takes a moment until the cache fills complete,
647 // but the unmuting takes long enough for that.
650 static void snd_emu10k1_playback_commit_volume(struct snd_emu10k1 *emu,
651 struct snd_emu10k1_voice *evoice,
654 snd_emu10k1_ptr_write_multiple(emu, evoice->number,
655 VTFT, vattn | VTFT_FILTERTARGET_MASK,
656 CVCF, vattn | CVCF_CURRENTFILTER_MASK,
660 static void snd_emu10k1_playback_unmute_voice(struct snd_emu10k1 *emu,
661 struct snd_emu10k1_voice *evoice,
662 bool stereo, bool master,
663 struct snd_emu10k1_pcm_mixer *mix)
668 tmp = stereo ? (master ? 1 : 2) : 0;
669 vattn = mix->attn[tmp] << 16;
670 snd_emu10k1_playback_commit_volume(emu, evoice, vattn);
673 static void snd_emu10k1_playback_unmute_voices(struct snd_emu10k1 *emu,
674 struct snd_emu10k1_voice *evoice,
676 struct snd_emu10k1_pcm_mixer *mix)
678 snd_emu10k1_playback_unmute_voice(emu, evoice, stereo, true, mix);
680 snd_emu10k1_playback_unmute_voice(emu, evoice + 1, true, false, mix);
683 static void snd_emu10k1_playback_mute_voice(struct snd_emu10k1 *emu,
684 struct snd_emu10k1_voice *evoice)
686 snd_emu10k1_playback_commit_volume(emu, evoice, 0);
689 static void snd_emu10k1_playback_mute_voices(struct snd_emu10k1 *emu,
690 struct snd_emu10k1_voice *evoice,
693 snd_emu10k1_playback_mute_voice(emu, evoice);
695 snd_emu10k1_playback_mute_voice(emu, evoice + 1);
698 static void snd_emu10k1_playback_commit_pitch(struct snd_emu10k1 *emu,
699 u32 voice, u32 pitch_target)
701 u32 ptrx = snd_emu10k1_ptr_read(emu, PTRX, voice);
702 u32 cpf = snd_emu10k1_ptr_read(emu, CPF, voice);
703 snd_emu10k1_ptr_write_multiple(emu, voice,
704 PTRX, (ptrx & ~PTRX_PITCHTARGET_MASK) | pitch_target,
705 CPF, (cpf & ~(CPF_CURRENTPITCH_MASK | CPF_FRACADDRESS_MASK)) | pitch_target,
709 static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu,
710 struct snd_emu10k1_voice *evoice)
714 voice = evoice->number;
715 snd_emu10k1_playback_commit_pitch(emu, voice, evoice->epcm->pitch_target << 16);
718 static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu,
719 struct snd_emu10k1_voice *evoice)
723 voice = evoice->number;
724 snd_emu10k1_playback_commit_pitch(emu, voice, 0);
727 static void snd_emu10k1_playback_set_running(struct snd_emu10k1 *emu,
728 struct snd_emu10k1_pcm *epcm)
731 snd_emu10k1_voice_intr_enable(emu, epcm->extra->number);
734 static void snd_emu10k1_playback_set_stopped(struct snd_emu10k1 *emu,
735 struct snd_emu10k1_pcm *epcm)
737 snd_emu10k1_voice_intr_disable(emu, epcm->extra->number);
741 static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
744 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
745 struct snd_pcm_runtime *runtime = substream->runtime;
746 struct snd_emu10k1_pcm *epcm = runtime->private_data;
747 struct snd_emu10k1_pcm_mixer *mix;
748 bool w_16 = snd_pcm_format_width(runtime->format) == 16;
749 bool stereo = runtime->channels == 2;
753 dev_dbg(emu->card->dev,
754 "trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n",
755 (int)emu, cmd, substream->ops->pointer(substream))
757 spin_lock(&emu->reg_lock);
759 case SNDRV_PCM_TRIGGER_START:
760 snd_emu10k1_playback_prepare_voices(emu, epcm, w_16, stereo, 1);
762 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
763 case SNDRV_PCM_TRIGGER_RESUME:
764 mix = &emu->pcm_mixer[substream->number];
765 snd_emu10k1_playback_unmute_voices(emu, epcm->voices[0], stereo, mix);
766 snd_emu10k1_playback_set_running(emu, epcm);
767 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0]);
768 snd_emu10k1_playback_trigger_voice(emu, epcm->extra);
770 case SNDRV_PCM_TRIGGER_STOP:
771 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
772 case SNDRV_PCM_TRIGGER_SUSPEND:
773 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
774 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
775 snd_emu10k1_playback_set_stopped(emu, epcm);
776 snd_emu10k1_playback_mute_voices(emu, epcm->voices[0], stereo);
782 spin_unlock(&emu->reg_lock);
786 static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
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;
794 spin_lock(&emu->reg_lock);
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);
802 dev_dbg(emu->card->dev, "adccr = 0x%x, adcbs = 0x%x\n",
803 epcm->adccr, epcm->adcbs);
805 switch (epcm->type) {
806 case CAPTURE_AC97ADC:
807 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
811 snd_emu10k1_ptr_write_multiple(emu, 0,
812 A_FXWC1, epcm->capture_cr_val,
813 A_FXWC2, epcm->capture_cr_val2,
815 dev_dbg(emu->card->dev,
816 "cr_val=0x%x, cr_val2=0x%x\n",
817 epcm->capture_cr_val,
818 epcm->capture_cr_val2);
820 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
825 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
829 case SNDRV_PCM_TRIGGER_STOP:
830 case SNDRV_PCM_TRIGGER_SUSPEND:
832 snd_emu10k1_intr_disable(emu, epcm->capture_inte);
833 outl(epcm->capture_ipr, emu->port + IPR);
834 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
835 switch (epcm->type) {
836 case CAPTURE_AC97ADC:
837 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
841 snd_emu10k1_ptr_write_multiple(emu, 0,
846 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
855 spin_unlock(&emu->reg_lock);
859 static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
861 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
862 struct snd_pcm_runtime *runtime = substream->runtime;
863 struct snd_emu10k1_pcm *epcm = runtime->private_data;
869 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
870 ptr -= epcm->ccca_start_addr;
872 // This is the size of the whole cache minus the interpolator read-ahead,
873 // which leads us to the actual playback position.
875 // The cache is constantly kept mostly filled, so in principle we could
876 // return a more advanced position representing how far the hardware has
877 // already read the buffer, and set runtime->delay accordingly. However,
878 // this would be slightly different for every channel (and remarkably slow
879 // to obtain), so only a fixed worst-case value would be practical.
883 ptr += runtime->buffer_size;
886 dev_dbg(emu->card->dev,
887 "ptr = 0x%lx, buffer_size = 0x%lx, period_size = 0x%lx\n",
888 (long)ptr, (long)runtime->buffer_size,
889 (long)runtime->period_size);
894 static u64 snd_emu10k1_efx_playback_voice_mask(struct snd_emu10k1_pcm *epcm,
899 for (int i = 0; i < channels; i++) {
900 int voice = epcm->voices[i]->number;
901 mask |= 1ULL << voice;
906 static void snd_emu10k1_efx_playback_freeze_voices(struct snd_emu10k1 *emu,
907 struct snd_emu10k1_pcm *epcm,
910 for (int i = 0; i < channels; i++) {
911 int voice = epcm->voices[i]->number;
912 snd_emu10k1_ptr_write(emu, CPF_STOP, voice, 1);
913 snd_emu10k1_playback_commit_pitch(emu, voice, PITCH_48000 << 16);
917 static void snd_emu10k1_efx_playback_unmute_voices(struct snd_emu10k1 *emu,
918 struct snd_emu10k1_pcm *epcm,
921 for (int i = 0; i < channels; i++)
922 snd_emu10k1_playback_unmute_voice(emu, epcm->voices[i], false, true,
923 &emu->efx_pcm_mixer[i]);
926 static void snd_emu10k1_efx_playback_stop_voices(struct snd_emu10k1 *emu,
927 struct snd_emu10k1_pcm *epcm,
930 for (int i = 0; i < channels; i++)
931 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
932 snd_emu10k1_playback_set_stopped(emu, epcm);
934 for (int i = 0; i < channels; i++)
935 snd_emu10k1_playback_mute_voice(emu, epcm->voices[i]);
938 static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
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;
947 spin_lock(&emu->reg_lock);
949 case SNDRV_PCM_TRIGGER_START:
950 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
951 case SNDRV_PCM_TRIGGER_RESUME:
952 mask = snd_emu10k1_efx_playback_voice_mask(
953 epcm, runtime->channels);
954 for (int i = 0; i < 10; i++) {
955 // Note that the freeze is not interruptible, so we make no
956 // effort to reset the bits outside the error handling here.
957 snd_emu10k1_voice_set_loop_stop_multiple(emu, mask);
958 snd_emu10k1_efx_playback_freeze_voices(
959 emu, epcm, runtime->channels);
960 snd_emu10k1_playback_prepare_voices(
961 emu, epcm, true, false, runtime->channels);
963 // It might seem to make more sense to unmute the voices only after
964 // they have been started, to potentially avoid torturing the speakers
965 // if something goes wrong. However, we cannot unmute atomically,
966 // which means that we'd get some mild artifacts in the regular case.
967 snd_emu10k1_efx_playback_unmute_voices(emu, epcm, runtime->channels);
969 snd_emu10k1_playback_set_running(emu, epcm);
970 result = snd_emu10k1_voice_clear_loop_stop_multiple_atomic(emu, mask);
972 // The extra voice is allowed to lag a bit
973 snd_emu10k1_playback_trigger_voice(emu, epcm->extra);
977 snd_emu10k1_efx_playback_stop_voices(
978 emu, epcm, runtime->channels);
980 if (result != -EAGAIN)
982 // The sync start can legitimately fail due to NMIs, etc.
984 snd_emu10k1_voice_clear_loop_stop_multiple(emu, mask);
986 case SNDRV_PCM_TRIGGER_SUSPEND:
987 case SNDRV_PCM_TRIGGER_STOP:
988 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
989 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
990 snd_emu10k1_efx_playback_stop_voices(
991 emu, epcm, runtime->channels);
993 epcm->resume_pos = snd_emu10k1_playback_pointer(substream);
1000 spin_unlock(&emu->reg_lock);
1005 static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
1007 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1008 struct snd_pcm_runtime *runtime = substream->runtime;
1009 struct snd_emu10k1_pcm *epcm = runtime->private_data;
1014 if (epcm->first_ptr) {
1015 udelay(50); /* hack, it takes awhile until capture is started */
1016 epcm->first_ptr = 0;
1018 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
1019 return bytes_to_frames(runtime, ptr);
1023 * Playback support device description
1026 static const struct snd_pcm_hardware snd_emu10k1_playback =
1028 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1029 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1030 SNDRV_PCM_INFO_RESUME |
1031 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
1032 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1033 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
1038 .buffer_bytes_max = (128*1024),
1039 .period_bytes_max = (128*1024),
1041 .periods_max = 1024,
1046 * Capture support device description
1049 static const struct snd_pcm_hardware snd_emu10k1_capture =
1051 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1052 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1053 SNDRV_PCM_INFO_RESUME |
1054 SNDRV_PCM_INFO_MMAP_VALID),
1055 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1056 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_KNOT,
1061 .buffer_bytes_max = (64*1024),
1062 .period_bytes_min = 384,
1063 .period_bytes_max = (64*1024),
1069 static const struct snd_pcm_hardware snd_emu10k1_capture_efx =
1071 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1072 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1073 SNDRV_PCM_INFO_RESUME |
1074 SNDRV_PCM_INFO_MMAP_VALID),
1075 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1076 .rates = SNDRV_PCM_RATE_48000,
1081 .buffer_bytes_max = (64*1024),
1082 .period_bytes_min = 384,
1083 .period_bytes_max = (64*1024),
1093 static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
1095 struct snd_ctl_elem_id id;
1100 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1102 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1103 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
1104 SNDRV_CTL_EVENT_MASK_INFO,
1105 snd_ctl_build_ioff(&id, kctl, idx));
1108 static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1110 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
1111 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
1112 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
1115 static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1117 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
1118 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
1119 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
1122 static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
1124 kfree(runtime->private_data);
1127 static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
1129 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1130 struct snd_emu10k1_pcm_mixer *mix;
1133 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1134 mix = &emu->efx_pcm_mixer[i];
1136 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1141 static int snd_emu10k1_playback_set_constraints(struct snd_pcm_runtime *runtime)
1145 // The buffer size must be a multiple of the period size, to avoid a
1146 // mismatch between the extra voice and the regular voices.
1147 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
1150 // The hardware is typically the cache's size of 64 frames ahead.
1151 // Leave enough time for actually filling up the buffer.
1152 err = snd_pcm_hw_constraint_minmax(
1153 runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 128, UINT_MAX);
1157 static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
1159 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1160 struct snd_emu10k1_pcm *epcm;
1161 struct snd_emu10k1_pcm_mixer *mix;
1162 struct snd_pcm_runtime *runtime = substream->runtime;
1165 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1169 epcm->type = PLAYBACK_EFX;
1170 epcm->substream = substream;
1172 runtime->private_data = epcm;
1173 runtime->private_free = snd_emu10k1_pcm_free_substream;
1174 runtime->hw = snd_emu10k1_efx_playback;
1175 if (emu->card_capabilities->emu_model)
1176 snd_emu1010_constrain_efx_rate(emu, runtime);
1177 err = snd_emu10k1_playback_set_constraints(runtime);
1183 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1184 mix = &emu->efx_pcm_mixer[i];
1185 for (j = 0; j < 8; j++)
1186 mix->send_routing[0][j] = i + j;
1187 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1188 mix->send_volume[0][0] = 255;
1189 mix->attn[0] = 0x8000;
1191 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1196 static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
1198 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1199 struct snd_emu10k1_pcm *epcm;
1200 struct snd_emu10k1_pcm_mixer *mix;
1201 struct snd_pcm_runtime *runtime = substream->runtime;
1202 int i, err, sample_rate;
1204 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1208 epcm->type = PLAYBACK_EMUVOICE;
1209 epcm->substream = substream;
1210 runtime->private_data = epcm;
1211 runtime->private_free = snd_emu10k1_pcm_free_substream;
1212 runtime->hw = snd_emu10k1_playback;
1213 err = snd_emu10k1_playback_set_constraints(runtime);
1218 if (emu->card_capabilities->emu_model)
1219 sample_rate = emu->emu1010.word_clock;
1221 sample_rate = 48000;
1222 err = snd_pcm_hw_rule_noresample(runtime, sample_rate);
1227 mix = &emu->pcm_mixer[substream->number];
1228 for (i = 0; i < 8; i++)
1229 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1230 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1231 mix->send_volume[0][0] = mix->send_volume[0][1] =
1232 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1233 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0x8000;
1235 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1239 static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
1241 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1242 struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
1245 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1249 static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
1251 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1252 struct snd_pcm_runtime *runtime = substream->runtime;
1253 struct snd_emu10k1_pcm *epcm;
1255 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1259 epcm->type = CAPTURE_AC97ADC;
1260 epcm->substream = substream;
1261 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1262 epcm->capture_inte = INTE_ADCBUFENABLE;
1263 epcm->capture_ba_reg = ADCBA;
1264 epcm->capture_bs_reg = ADCBS;
1265 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1266 runtime->private_data = epcm;
1267 runtime->private_free = snd_emu10k1_pcm_free_substream;
1268 runtime->hw = snd_emu10k1_capture;
1269 snd_emu10k1_constrain_capture_rates(emu, runtime);
1270 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1271 &hw_constraints_capture_buffer_sizes);
1272 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1273 emu->pcm_capture_substream = substream;
1277 static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
1279 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1281 emu->capture_interrupt = NULL;
1282 emu->pcm_capture_substream = NULL;
1286 static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
1288 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1289 struct snd_emu10k1_pcm *epcm;
1290 struct snd_pcm_runtime *runtime = substream->runtime;
1292 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1296 epcm->type = CAPTURE_AC97MIC;
1297 epcm->substream = substream;
1298 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1299 epcm->capture_inte = INTE_MICBUFENABLE;
1300 epcm->capture_ba_reg = MICBA;
1301 epcm->capture_bs_reg = MICBS;
1302 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1303 substream->runtime->private_data = epcm;
1304 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1305 runtime->hw = snd_emu10k1_capture;
1306 runtime->hw.rates = SNDRV_PCM_RATE_8000;
1307 runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1308 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1309 &hw_constraints_capture_buffer_sizes);
1310 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1311 emu->pcm_capture_mic_substream = substream;
1315 static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
1317 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1319 emu->capture_mic_interrupt = NULL;
1320 emu->pcm_capture_mic_substream = NULL;
1324 static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
1326 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1327 struct snd_emu10k1_pcm *epcm;
1328 struct snd_pcm_runtime *runtime = substream->runtime;
1329 int nefx = emu->audigy ? 64 : 32;
1332 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1336 epcm->type = CAPTURE_EFX;
1337 epcm->substream = substream;
1338 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1339 epcm->capture_inte = INTE_EFXBUFENABLE;
1340 epcm->capture_ba_reg = FXBA;
1341 epcm->capture_bs_reg = FXBS;
1342 epcm->capture_idx_reg = FXIDX;
1343 substream->runtime->private_data = epcm;
1344 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1345 runtime->hw = snd_emu10k1_capture_efx;
1346 if (emu->card_capabilities->emu_model) {
1347 snd_emu1010_constrain_efx_rate(emu, runtime);
1349 * There are 32 mono channels of 16bits each.
1350 * 24bit Audio uses 2x channels over 16bit,
1351 * 96kHz uses 2x channels over 48kHz,
1352 * 192kHz uses 4x channels over 48kHz.
1353 * So, for 48kHz 24bit, one has 16 channels,
1354 * for 96kHz 24bit, one has 8 channels,
1355 * for 192kHz 24bit, one has 4 channels.
1356 * 1010rev2 and 1616(m) cards have double that,
1357 * but we don't exceed 16 channels anyway.
1361 runtime->hw.channels_min = runtime->hw.channels_max = 4;
1365 runtime->hw.channels_min = runtime->hw.channels_max = 2;
1367 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1369 spin_lock_irq(&emu->reg_lock);
1370 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1371 for (idx = 0; idx < nefx; idx++) {
1372 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1373 runtime->hw.channels_min++;
1374 runtime->hw.channels_max++;
1377 epcm->capture_cr_val = emu->efx_voices_mask[0];
1378 epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1379 spin_unlock_irq(&emu->reg_lock);
1381 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1382 &hw_constraints_efx_capture_channels);
1387 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1388 &hw_constraints_capture_buffer_sizes);
1389 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1390 emu->pcm_capture_efx_substream = substream;
1394 static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
1396 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1398 emu->capture_efx_interrupt = NULL;
1399 emu->pcm_capture_efx_substream = NULL;
1403 static const struct snd_pcm_ops snd_emu10k1_playback_ops = {
1404 .open = snd_emu10k1_playback_open,
1405 .close = snd_emu10k1_playback_close,
1406 .hw_params = snd_emu10k1_playback_hw_params,
1407 .hw_free = snd_emu10k1_playback_hw_free,
1408 .prepare = snd_emu10k1_playback_prepare,
1409 .trigger = snd_emu10k1_playback_trigger,
1410 .pointer = snd_emu10k1_playback_pointer,
1413 static const struct snd_pcm_ops snd_emu10k1_capture_ops = {
1414 .open = snd_emu10k1_capture_open,
1415 .close = snd_emu10k1_capture_close,
1416 .prepare = snd_emu10k1_capture_prepare,
1417 .trigger = snd_emu10k1_capture_trigger,
1418 .pointer = snd_emu10k1_capture_pointer,
1422 static const struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
1423 .open = snd_emu10k1_efx_playback_open,
1424 .close = snd_emu10k1_efx_playback_close,
1425 .hw_params = snd_emu10k1_playback_hw_params,
1426 .hw_free = snd_emu10k1_playback_hw_free,
1427 .prepare = snd_emu10k1_efx_playback_prepare,
1428 .trigger = snd_emu10k1_efx_playback_trigger,
1429 .pointer = snd_emu10k1_playback_pointer,
1432 int snd_emu10k1_pcm(struct snd_emu10k1 *emu, int device)
1434 struct snd_pcm *pcm;
1435 struct snd_pcm_substream *substream;
1438 err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm);
1442 pcm->private_data = emu;
1444 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1445 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1447 pcm->info_flags = 0;
1448 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1449 strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1452 /* playback substream can't use managed buffers due to alignment */
1453 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1454 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
1458 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1459 snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV,
1460 &emu->pci->dev, 64*1024, 64*1024);
1465 int snd_emu10k1_pcm_multi(struct snd_emu10k1 *emu, int device)
1467 struct snd_pcm *pcm;
1468 struct snd_pcm_substream *substream;
1471 err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm);
1475 pcm->private_data = emu;
1477 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1479 pcm->info_flags = 0;
1480 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1481 strcpy(pcm->name, "Multichannel Playback");
1482 emu->pcm_multi = pcm;
1484 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1485 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
1493 static const struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
1494 .open = snd_emu10k1_capture_mic_open,
1495 .close = snd_emu10k1_capture_mic_close,
1496 .prepare = snd_emu10k1_capture_prepare,
1497 .trigger = snd_emu10k1_capture_trigger,
1498 .pointer = snd_emu10k1_capture_pointer,
1501 int snd_emu10k1_pcm_mic(struct snd_emu10k1 *emu, int device)
1503 struct snd_pcm *pcm;
1506 err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm);
1510 pcm->private_data = emu;
1512 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1514 pcm->info_flags = 0;
1515 strcpy(pcm->name, "Mic Capture");
1518 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev,
1524 static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1526 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1527 int nefx = emu->audigy ? 64 : 32;
1528 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1529 uinfo->count = nefx;
1530 uinfo->value.integer.min = 0;
1531 uinfo->value.integer.max = 1;
1535 static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1537 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1538 int nefx = emu->audigy ? 64 : 32;
1541 for (idx = 0; idx < nefx; idx++)
1542 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1546 static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1548 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1549 unsigned int nval[2], bits;
1550 int nefx = emu->audigy ? 64 : 32;
1553 nval[0] = nval[1] = 0;
1554 for (idx = 0, bits = 0; idx < nefx; idx++)
1555 if (ucontrol->value.integer.value[idx]) {
1556 nval[idx / 32] |= 1 << (idx % 32);
1560 if (bits == 9 || bits == 11 || bits == 13 || bits == 15 || bits > 16)
1563 spin_lock_irq(&emu->reg_lock);
1564 change = (nval[0] != emu->efx_voices_mask[0]) ||
1565 (nval[1] != emu->efx_voices_mask[1]);
1566 emu->efx_voices_mask[0] = nval[0];
1567 emu->efx_voices_mask[1] = nval[1];
1568 spin_unlock_irq(&emu->reg_lock);
1572 static const struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
1573 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1574 .name = "Captured FX8010 Outputs",
1575 .info = snd_emu10k1_pcm_efx_voices_mask_info,
1576 .get = snd_emu10k1_pcm_efx_voices_mask_get,
1577 .put = snd_emu10k1_pcm_efx_voices_mask_put
1580 static const struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
1581 .open = snd_emu10k1_capture_efx_open,
1582 .close = snd_emu10k1_capture_efx_close,
1583 .prepare = snd_emu10k1_capture_prepare,
1584 .trigger = snd_emu10k1_capture_trigger,
1585 .pointer = snd_emu10k1_capture_pointer,
1591 #define INITIAL_TRAM_SHIFT 14
1592 #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1594 static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
1596 struct snd_pcm_substream *substream = private_data;
1597 snd_pcm_period_elapsed(substream);
1600 static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1601 unsigned short *dst_right,
1602 unsigned short *src,
1604 unsigned int tram_shift)
1607 dev_dbg(emu->card->dev,
1608 "tram_poke1: dst_left = 0x%p, dst_right = 0x%p, "
1609 "src = 0x%p, count = 0x%x\n",
1610 dst_left, dst_right, src, count);
1612 if ((tram_shift & 1) == 0) {
1614 *dst_left-- = *src++;
1615 *dst_right-- = *src++;
1619 *dst_right-- = *src++;
1620 *dst_left-- = *src++;
1625 static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1626 struct snd_pcm_indirect *rec, size_t bytes)
1628 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1629 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1630 unsigned int tram_size = pcm->buffer_size;
1631 unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1632 unsigned int frames = bytes >> 2, count;
1633 unsigned int tram_pos = pcm->tram_pos;
1634 unsigned int tram_shift = pcm->tram_shift;
1636 while (frames > tram_pos) {
1637 count = tram_pos + 1;
1638 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1639 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1640 src, count, tram_shift);
1643 tram_pos = (tram_size / 2) - 1;
1646 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1647 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1648 src, frames, tram_shift);
1650 pcm->tram_pos = tram_pos;
1651 pcm->tram_shift = tram_shift;
1654 static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
1656 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1657 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1659 return snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec,
1660 fx8010_pb_trans_copy);
1663 static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
1665 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1666 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1669 for (i = 0; i < pcm->channels; i++)
1670 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1674 static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
1676 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1677 struct snd_pcm_runtime *runtime = substream->runtime;
1678 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1682 dev_dbg(emu->card->dev, "prepare: etram_pages = 0x%p, dma_area = 0x%x, "
1683 "buffer_size = 0x%x (0x%x)\n",
1684 emu->fx8010.etram_pages, runtime->dma_area,
1685 runtime->buffer_size, runtime->buffer_size << 2);
1687 memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1688 pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1689 pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1690 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1691 pcm->tram_shift = 0;
1692 snd_emu10k1_ptr_write_multiple(emu, 0,
1693 emu->gpr_base + pcm->gpr_running, 0, /* reset */
1694 emu->gpr_base + pcm->gpr_trigger, 0, /* reset */
1695 emu->gpr_base + pcm->gpr_size, runtime->buffer_size,
1696 emu->gpr_base + pcm->gpr_ptr, 0, /* reset ptr number */
1697 emu->gpr_base + pcm->gpr_count, runtime->period_size,
1698 emu->gpr_base + pcm->gpr_tmpcount, runtime->period_size,
1700 for (i = 0; i < pcm->channels; i++)
1701 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1705 static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
1707 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1708 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1711 spin_lock(&emu->reg_lock);
1713 case SNDRV_PCM_TRIGGER_START:
1715 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1716 case SNDRV_PCM_TRIGGER_RESUME:
1717 #ifdef EMU10K1_SET_AC3_IEC958
1720 for (i = 0; i < 3; i++) {
1722 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1723 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1724 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1725 snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1729 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1732 snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
1733 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1735 case SNDRV_PCM_TRIGGER_STOP:
1736 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1737 case SNDRV_PCM_TRIGGER_SUSPEND:
1738 snd_emu10k1_fx8010_unregister_irq_handler(emu, &pcm->irq);
1739 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1740 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1741 pcm->tram_shift = 0;
1748 spin_unlock(&emu->reg_lock);
1752 static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
1754 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1755 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1756 size_t ptr; /* byte pointer */
1758 if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1760 ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1761 return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1764 static const struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
1766 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1767 SNDRV_PCM_INFO_RESUME |
1768 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE |
1769 SNDRV_PCM_INFO_SYNC_APPLPTR),
1770 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1771 .rates = SNDRV_PCM_RATE_48000,
1776 .buffer_bytes_max = (128*1024),
1777 .period_bytes_min = 1024,
1778 .period_bytes_max = (128*1024),
1780 .periods_max = 1024,
1784 static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
1786 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1787 struct snd_pcm_runtime *runtime = substream->runtime;
1788 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1790 runtime->hw = snd_emu10k1_fx8010_playback;
1791 runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1792 runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1793 spin_lock_irq(&emu->reg_lock);
1794 if (pcm->valid == 0) {
1795 spin_unlock_irq(&emu->reg_lock);
1799 spin_unlock_irq(&emu->reg_lock);
1803 static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
1805 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1806 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1808 spin_lock_irq(&emu->reg_lock);
1810 spin_unlock_irq(&emu->reg_lock);
1814 static const struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
1815 .open = snd_emu10k1_fx8010_playback_open,
1816 .close = snd_emu10k1_fx8010_playback_close,
1817 .hw_free = snd_emu10k1_fx8010_playback_hw_free,
1818 .prepare = snd_emu10k1_fx8010_playback_prepare,
1819 .trigger = snd_emu10k1_fx8010_playback_trigger,
1820 .pointer = snd_emu10k1_fx8010_playback_pointer,
1821 .ack = snd_emu10k1_fx8010_playback_transfer,
1824 int snd_emu10k1_pcm_efx(struct snd_emu10k1 *emu, int device)
1826 struct snd_pcm *pcm;
1827 struct snd_kcontrol *kctl;
1830 err = snd_pcm_new(emu->card, "emu10k1 efx", device, emu->audigy ? 0 : 8, 1, &pcm);
1834 pcm->private_data = emu;
1837 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1838 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1840 pcm->info_flags = 0;
1842 strcpy(pcm->name, "Multichannel Capture");
1844 strcpy(pcm->name, "Multichannel Capture/PT Playback");
1847 if (!emu->card_capabilities->emu_model) {
1848 // On Sound Blasters, the DSP code copies the EXTINs to FXBUS2.
1849 // The mask determines which of these and the EXTOUTs the multi-
1850 // channel capture actually records (the channel order is fixed).
1852 emu->efx_voices_mask[0] = 0;
1853 emu->efx_voices_mask[1] = 0xffff;
1855 emu->efx_voices_mask[0] = 0xffff0000;
1856 emu->efx_voices_mask[1] = 0;
1858 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1861 kctl->id.device = device;
1862 err = snd_ctl_add(emu->card, kctl);
1866 // On E-MU cards, the DSP code copies the P16VINs/EMU32INs to
1867 // FXBUS2. These are already selected & routed by the FPGA,
1868 // so there is no need to apply additional masking.
1871 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev,