2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 * Routines for control of EMU10K1 chips / PCM routines
5 * Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sound/driver.h>
30 #include <linux/pci.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/time.h>
34 #include <linux/init.h>
35 #include <sound/core.h>
36 #include <sound/emu10k1.h>
38 static void snd_emu10k1_pcm_interrupt(emu10k1_t *emu, emu10k1_voice_t *voice)
42 if ((epcm = voice->epcm) == NULL)
44 if (epcm->substream == NULL)
47 printk("IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
48 epcm->substream->runtime->hw->pointer(emu, epcm->substream),
49 snd_pcm_lib_period_bytes(epcm->substream),
50 snd_pcm_lib_buffer_bytes(epcm->substream));
52 snd_pcm_period_elapsed(epcm->substream);
55 static void snd_emu10k1_pcm_ac97adc_interrupt(emu10k1_t *emu, unsigned int status)
58 if (status & IPR_ADCBUFHALFFULL) {
59 if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
63 snd_pcm_period_elapsed(emu->pcm_capture_substream);
66 static void snd_emu10k1_pcm_ac97mic_interrupt(emu10k1_t *emu, unsigned int status)
69 if (status & IPR_MICBUFHALFFULL) {
70 if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
74 snd_pcm_period_elapsed(emu->pcm_capture_mic_substream);
77 static void snd_emu10k1_pcm_efx_interrupt(emu10k1_t *emu, unsigned int status)
80 if (status & IPR_EFXBUFHALFFULL) {
81 if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
85 snd_pcm_period_elapsed(emu->pcm_capture_efx_substream);
88 static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(snd_pcm_substream_t * substream)
90 emu10k1_t *emu = snd_pcm_substream_chip(substream);
91 snd_pcm_runtime_t *runtime = substream->runtime;
92 emu10k1_pcm_t *epcm = runtime->private_data;
97 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
98 ptr += runtime->buffer_size;
99 ptr -= epcm->ccca_start_addr;
100 ptr %= runtime->buffer_size;
105 static int snd_emu10k1_pcm_channel_alloc(emu10k1_pcm_t * epcm, int voices)
109 if (epcm->voices[1] != NULL && voices < 2) {
110 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
111 epcm->voices[1] = NULL;
113 for (i = 0; i < voices; i++) {
114 if (epcm->voices[i] == NULL)
118 return 0; /* already allocated */
120 for (i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
121 if (epcm->voices[i]) {
122 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
123 epcm->voices[i] = NULL;
126 err = snd_emu10k1_voice_alloc(epcm->emu,
127 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
133 epcm->voices[0]->epcm = epcm;
135 for (i = 1; i < voices; i++) {
136 epcm->voices[i] = &epcm->emu->voices[epcm->voices[0]->number + i];
137 epcm->voices[i]->epcm = epcm;
140 if (epcm->extra == NULL) {
141 err = snd_emu10k1_voice_alloc(epcm->emu,
142 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
146 // printk("pcm_channel_alloc: failed extra: voices=%d, frame=%d\n", voices, frame);
147 for (i = 0; i < voices; i++) {
148 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
149 epcm->voices[i] = NULL;
153 epcm->extra->epcm = epcm;
154 epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
159 static unsigned int capture_period_sizes[31] = {
161 384*2, 448*2, 512*2, 640*2,
162 384*4, 448*4, 512*4, 640*4,
163 384*8, 448*8, 512*8, 640*8,
164 384*16, 448*16, 512*16, 640*16,
165 384*32, 448*32, 512*32, 640*32,
166 384*64, 448*64, 512*64, 640*64,
167 384*128,448*128,512*128
170 static snd_pcm_hw_constraint_list_t hw_constraints_capture_period_sizes = {
172 .list = capture_period_sizes,
176 static unsigned int capture_rates[8] = {
177 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
180 static snd_pcm_hw_constraint_list_t hw_constraints_capture_rates = {
182 .list = capture_rates,
186 static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
189 case 8000: return ADCCR_SAMPLERATE_8;
190 case 11025: return ADCCR_SAMPLERATE_11;
191 case 16000: return ADCCR_SAMPLERATE_16;
192 case 22050: return ADCCR_SAMPLERATE_22;
193 case 24000: return ADCCR_SAMPLERATE_24;
194 case 32000: return ADCCR_SAMPLERATE_32;
195 case 44100: return ADCCR_SAMPLERATE_44;
196 case 48000: return ADCCR_SAMPLERATE_48;
199 return ADCCR_SAMPLERATE_8;
203 static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
206 case 8000: return A_ADCCR_SAMPLERATE_8;
207 case 11025: return A_ADCCR_SAMPLERATE_11;
208 case 12000: return A_ADCCR_SAMPLERATE_12; /* really supported? */
209 case 16000: return ADCCR_SAMPLERATE_16;
210 case 22050: return ADCCR_SAMPLERATE_22;
211 case 24000: return ADCCR_SAMPLERATE_24;
212 case 32000: return ADCCR_SAMPLERATE_32;
213 case 44100: return ADCCR_SAMPLERATE_44;
214 case 48000: return ADCCR_SAMPLERATE_48;
217 return A_ADCCR_SAMPLERATE_8;
221 static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
223 unsigned int pitch_target;
225 pitch_target = (rate << 8) / 375;
226 pitch_target = (pitch_target >> 1) + (pitch_target & 1);
230 #define PITCH_48000 0x00004000
231 #define PITCH_96000 0x00008000
232 #define PITCH_85000 0x00007155
233 #define PITCH_80726 0x00006ba2
234 #define PITCH_67882 0x00005a82
235 #define PITCH_57081 0x00004c1c
237 static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
239 if (pitch_target == PITCH_48000)
240 return CCCA_INTERPROM_0;
241 else if (pitch_target < PITCH_48000)
242 return CCCA_INTERPROM_1;
243 else if (pitch_target >= PITCH_96000)
244 return CCCA_INTERPROM_0;
245 else if (pitch_target >= PITCH_85000)
246 return CCCA_INTERPROM_6;
247 else if (pitch_target >= PITCH_80726)
248 return CCCA_INTERPROM_5;
249 else if (pitch_target >= PITCH_67882)
250 return CCCA_INTERPROM_4;
251 else if (pitch_target >= PITCH_57081)
252 return CCCA_INTERPROM_3;
254 return CCCA_INTERPROM_2;
258 * calculate cache invalidate size
260 * stereo: channel is stereo
261 * w_16: using 16bit samples
263 * returns: cache invalidate size in samples
265 static inline int emu10k1_ccis(int stereo, int w_16)
268 return stereo ? 24 : 26;
270 return stereo ? 24*2 : 26*2;
274 static void snd_emu10k1_pcm_init_voice(emu10k1_t *emu,
275 int master, int extra,
276 emu10k1_voice_t *evoice,
277 unsigned int start_addr,
278 unsigned int end_addr,
279 emu10k1_pcm_mixer_t *mix)
281 snd_pcm_substream_t *substream = evoice->epcm->substream;
282 snd_pcm_runtime_t *runtime = substream->runtime;
283 unsigned int silent_page, tmp;
284 int voice, stereo, w_16;
285 unsigned char attn, send_amount[8];
286 unsigned char send_routing[8];
288 unsigned int pitch_target;
291 voice = evoice->number;
292 stereo = runtime->channels == 2;
293 w_16 = snd_pcm_format_width(runtime->format) == 16;
295 if (!extra && stereo) {
304 spin_lock_irqsave(&emu->reg_lock, flags);
306 /* volume parameters */
309 memset(send_routing, 0, sizeof(send_routing));
314 memset(send_amount, 0, sizeof(send_amount));
316 /* mono, left, right (master voice = left) */
317 tmp = stereo ? (master ? 1 : 2) : 0;
318 memcpy(send_routing, &mix->send_routing[tmp][0], 8);
319 memcpy(send_amount, &mix->send_volume[tmp][0], 8);
322 ccis = emu10k1_ccis(stereo, w_16);
325 evoice->epcm->ccca_start_addr = start_addr + ccis;
330 if (stereo && !extra) {
331 snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK);
332 snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK);
334 snd_emu10k1_ptr_write(emu, CPF, voice, 0);
340 snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
341 snd_emu10k1_compose_audigy_fxrt1(send_routing));
342 snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
343 snd_emu10k1_compose_audigy_fxrt2(send_routing));
344 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
345 ((unsigned int)send_amount[4] << 24) |
346 ((unsigned int)send_amount[5] << 16) |
347 ((unsigned int)send_amount[6] << 8) |
348 (unsigned int)send_amount[7]);
350 snd_emu10k1_ptr_write(emu, FXRT, voice,
351 snd_emu10k1_compose_send_routing(send_routing));
353 // Assumption that PT is already 0 so no harm overwriting
354 snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]);
355 snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24));
356 snd_emu10k1_ptr_write(emu, PSST, voice, start_addr | (send_amount[2] << 24));
357 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
359 snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
360 emu10k1_select_interprom(pitch_target) |
361 (w_16 ? 0 : CCCA_8BITSELECT));
363 snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
364 emu10k1_select_interprom(pitch_target) |
365 (w_16 ? 0 : CCCA_8BITSELECT));
366 // Clear filter delay memory
367 snd_emu10k1_ptr_write(emu, Z1, voice, 0);
368 snd_emu10k1_ptr_write(emu, Z2, voice, 0);
370 silent_page = ((unsigned int)emu->silent_page.addr << 1) | MAP_PTI_MASK;
371 snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
372 snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
373 // modulation envelope
374 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
375 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
376 snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
377 snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
378 snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
379 snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
380 snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
381 snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
382 snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
383 snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
385 snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
386 snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
388 snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
390 snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
392 spin_unlock_irqrestore(&emu->reg_lock, flags);
395 static int snd_emu10k1_playback_hw_params(snd_pcm_substream_t * substream,
396 snd_pcm_hw_params_t * hw_params)
398 emu10k1_t *emu = snd_pcm_substream_chip(substream);
399 snd_pcm_runtime_t *runtime = substream->runtime;
400 emu10k1_pcm_t *epcm = runtime->private_data;
403 if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0)
405 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
407 if (err > 0) { /* change */
408 snd_util_memblk_t *memblk;
409 if (epcm->memblk != NULL)
410 snd_emu10k1_free_pages(emu, epcm->memblk);
411 memblk = snd_emu10k1_alloc_pages(emu, substream);
412 if ((epcm->memblk = memblk) == NULL || ((emu10k1_memblk_t *)memblk)->mapped_page < 0) {
413 epcm->start_addr = 0;
416 epcm->start_addr = ((emu10k1_memblk_t *)memblk)->mapped_page << PAGE_SHIFT;
421 static int snd_emu10k1_playback_hw_free(snd_pcm_substream_t * substream)
423 emu10k1_t *emu = snd_pcm_substream_chip(substream);
424 snd_pcm_runtime_t *runtime = substream->runtime;
427 if (runtime->private_data == NULL)
429 epcm = runtime->private_data;
431 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
434 if (epcm->voices[1]) {
435 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
436 epcm->voices[1] = NULL;
438 if (epcm->voices[0]) {
439 snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
440 epcm->voices[0] = NULL;
443 snd_emu10k1_free_pages(emu, epcm->memblk);
445 epcm->start_addr = 0;
447 snd_pcm_lib_free_pages(substream);
451 static int snd_emu10k1_efx_playback_hw_free(snd_pcm_substream_t * substream)
453 emu10k1_t *emu = snd_pcm_substream_chip(substream);
454 snd_pcm_runtime_t *runtime = substream->runtime;
458 if (runtime->private_data == NULL)
460 epcm = runtime->private_data;
462 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
465 for (i=0; i < NUM_EFX_PLAYBACK; i++) {
466 if (epcm->voices[i]) {
467 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
468 epcm->voices[i] = NULL;
472 snd_emu10k1_free_pages(emu, epcm->memblk);
474 epcm->start_addr = 0;
476 snd_pcm_lib_free_pages(substream);
480 static int snd_emu10k1_playback_prepare(snd_pcm_substream_t * substream)
482 emu10k1_t *emu = snd_pcm_substream_chip(substream);
483 snd_pcm_runtime_t *runtime = substream->runtime;
484 emu10k1_pcm_t *epcm = runtime->private_data;
485 unsigned int start_addr, end_addr;
487 start_addr = epcm->start_addr;
488 end_addr = snd_pcm_lib_period_bytes(substream);
489 if (runtime->channels == 2) {
493 end_addr += start_addr;
494 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
495 start_addr, end_addr, NULL);
496 start_addr = epcm->start_addr;
497 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
498 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
499 start_addr, end_addr,
500 &emu->pcm_mixer[substream->number]);
502 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
503 start_addr, end_addr,
504 &emu->pcm_mixer[substream->number]);
508 static int snd_emu10k1_efx_playback_prepare(snd_pcm_substream_t * substream)
510 emu10k1_t *emu = snd_pcm_substream_chip(substream);
511 snd_pcm_runtime_t *runtime = substream->runtime;
512 emu10k1_pcm_t *epcm = runtime->private_data;
513 unsigned int start_addr, end_addr;
514 unsigned int channel_size;
517 start_addr = epcm->start_addr;
518 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
521 * the kX driver leaves some space between voices
523 channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
525 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
526 start_addr, start_addr + (channel_size / 2), NULL);
528 /* only difference with the master voice is we use it for the pointer */
529 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
530 start_addr, start_addr + channel_size,
531 &emu->efx_pcm_mixer[0]);
533 start_addr += channel_size;
534 for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
535 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
536 start_addr, start_addr + channel_size,
537 &emu->efx_pcm_mixer[i]);
538 start_addr += channel_size;
544 static snd_pcm_hardware_t snd_emu10k1_efx_playback =
546 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
547 SNDRV_PCM_INFO_BLOCK_TRANSFER |
548 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
549 .formats = SNDRV_PCM_FMTBIT_S16_LE,
550 .rates = SNDRV_PCM_RATE_48000,
553 .channels_min = NUM_EFX_PLAYBACK,
554 .channels_max = NUM_EFX_PLAYBACK,
555 .buffer_bytes_max = (64*1024),
556 .period_bytes_min = 64,
557 .period_bytes_max = (64*1024),
563 static int snd_emu10k1_capture_hw_params(snd_pcm_substream_t * substream,
564 snd_pcm_hw_params_t * hw_params)
566 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
569 static int snd_emu10k1_capture_hw_free(snd_pcm_substream_t * substream)
571 return snd_pcm_lib_free_pages(substream);
574 static int snd_emu10k1_capture_prepare(snd_pcm_substream_t * substream)
576 emu10k1_t *emu = snd_pcm_substream_chip(substream);
577 snd_pcm_runtime_t *runtime = substream->runtime;
578 emu10k1_pcm_t *epcm = runtime->private_data;
581 /* zeroing the buffer size will stop capture */
582 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
583 switch (epcm->type) {
584 case CAPTURE_AC97ADC:
585 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
589 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
590 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
592 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
597 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
598 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
599 epcm->capture_bs_val = 0;
600 for (idx = 0; idx < 31; idx++) {
601 if (capture_period_sizes[idx] == epcm->capture_bufsize) {
602 epcm->capture_bs_val = idx + 1;
606 if (epcm->capture_bs_val == 0) {
608 epcm->capture_bs_val++;
610 if (epcm->type == CAPTURE_AC97ADC) {
611 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
612 if (runtime->channels > 1)
613 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
614 epcm->capture_cr_val |= emu->audigy ?
615 snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
616 snd_emu10k1_capture_rate_reg(runtime->rate);
621 static void snd_emu10k1_playback_invalidate_cache(emu10k1_t *emu, int extra, emu10k1_voice_t *evoice)
623 snd_pcm_runtime_t *runtime;
624 unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
628 runtime = evoice->epcm->substream->runtime;
629 voice = evoice->number;
630 stereo = (!extra && runtime->channels == 2);
631 sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
632 ccis = emu10k1_ccis(stereo, sample == 0);
633 // set cs to 2 * number of cache registers beside the invalidated
634 cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
635 if (cs > 16) cs = 16;
636 for (i = 0; i < cs; i++) {
637 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
639 snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
643 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
644 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
646 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
647 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
650 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
652 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
656 static void snd_emu10k1_playback_prepare_voice(emu10k1_t *emu, emu10k1_voice_t *evoice,
657 int master, int extra,
658 emu10k1_pcm_mixer_t *mix)
660 snd_pcm_substream_t *substream;
661 snd_pcm_runtime_t *runtime;
662 unsigned int attn, vattn;
663 unsigned int voice, tmp;
665 if (evoice == NULL) /* skip second voice for mono */
667 substream = evoice->epcm->substream;
668 runtime = substream->runtime;
669 voice = evoice->number;
671 attn = extra ? 0 : 0x00ff;
672 tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
673 vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
674 snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
675 snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
676 snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
677 snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
678 snd_emu10k1_voice_clear_loop_stop(emu, voice);
681 static void snd_emu10k1_playback_trigger_voice(emu10k1_t *emu, emu10k1_voice_t *evoice, int master, int extra)
683 snd_pcm_substream_t *substream;
684 snd_pcm_runtime_t *runtime;
685 unsigned int voice, pitch, pitch_target;
687 if (evoice == NULL) /* skip second voice for mono */
689 substream = evoice->epcm->substream;
690 runtime = substream->runtime;
691 voice = evoice->number;
693 pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
694 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
695 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
696 if (master || evoice->epcm->type == PLAYBACK_EFX)
697 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
698 snd_emu10k1_ptr_write(emu, IP, voice, pitch);
700 snd_emu10k1_voice_intr_enable(emu, voice);
703 static void snd_emu10k1_playback_stop_voice(emu10k1_t *emu, emu10k1_voice_t *evoice)
709 voice = evoice->number;
710 snd_emu10k1_voice_intr_disable(emu, voice);
711 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
712 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
713 snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
714 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
715 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
716 snd_emu10k1_ptr_write(emu, IP, voice, 0);
719 static int snd_emu10k1_playback_trigger(snd_pcm_substream_t * substream,
722 emu10k1_t *emu = snd_pcm_substream_chip(substream);
723 snd_pcm_runtime_t *runtime = substream->runtime;
724 emu10k1_pcm_t *epcm = runtime->private_data;
725 emu10k1_pcm_mixer_t *mix;
728 // printk("trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n", (int)emu, cmd, substream->ops->pointer(substream));
729 spin_lock(&emu->reg_lock);
731 case SNDRV_PCM_TRIGGER_START:
732 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */
733 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
735 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
736 mix = &emu->pcm_mixer[substream->number];
737 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
738 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
739 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
740 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
741 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
742 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
745 case SNDRV_PCM_TRIGGER_STOP:
746 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
748 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
749 snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
750 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
756 spin_unlock(&emu->reg_lock);
760 static int snd_emu10k1_capture_trigger(snd_pcm_substream_t * substream,
763 emu10k1_t *emu = snd_pcm_substream_chip(substream);
764 snd_pcm_runtime_t *runtime = substream->runtime;
765 emu10k1_pcm_t *epcm = runtime->private_data;
768 spin_lock(&emu->reg_lock);
770 case SNDRV_PCM_TRIGGER_START:
771 // hmm this should cause full and half full interrupt to be raised?
772 outl(epcm->capture_ipr, emu->port + IPR);
773 snd_emu10k1_intr_enable(emu, epcm->capture_inte);
774 // printk("adccr = 0x%x, adcbs = 0x%x\n", epcm->adccr, epcm->adcbs);
775 switch (epcm->type) {
776 case CAPTURE_AC97ADC:
777 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
781 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
782 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
784 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
789 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
793 case SNDRV_PCM_TRIGGER_STOP:
795 snd_emu10k1_intr_disable(emu, epcm->capture_inte);
796 outl(epcm->capture_ipr, emu->port + IPR);
797 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
798 switch (epcm->type) {
799 case CAPTURE_AC97ADC:
800 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
804 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
805 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
807 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
816 spin_unlock(&emu->reg_lock);
820 static snd_pcm_uframes_t snd_emu10k1_playback_pointer(snd_pcm_substream_t * substream)
822 emu10k1_t *emu = snd_pcm_substream_chip(substream);
823 snd_pcm_runtime_t *runtime = substream->runtime;
824 emu10k1_pcm_t *epcm = runtime->private_data;
829 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
830 #if 0 /* Perex's code */
831 ptr += runtime->buffer_size;
832 ptr -= epcm->ccca_start_addr;
833 ptr %= runtime->buffer_size;
834 #else /* EMU10K1 Open Source code from Creative */
835 if (ptr < epcm->ccca_start_addr)
836 ptr += runtime->buffer_size - epcm->ccca_start_addr;
838 ptr -= epcm->ccca_start_addr;
839 if (ptr >= runtime->buffer_size)
840 ptr -= runtime->buffer_size;
843 // printk("ptr = 0x%x, buffer_size = 0x%x, period_size = 0x%x\n", ptr, runtime->buffer_size, runtime->period_size);
848 static int snd_emu10k1_efx_playback_trigger(snd_pcm_substream_t * substream,
851 emu10k1_t *emu = snd_pcm_substream_chip(substream);
852 snd_pcm_runtime_t *runtime = substream->runtime;
853 emu10k1_pcm_t *epcm = runtime->private_data;
857 spin_lock(&emu->reg_lock);
859 case SNDRV_PCM_TRIGGER_START:
861 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
862 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
864 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
867 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
868 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
869 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
870 &emu->efx_pcm_mixer[0]);
871 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
872 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
873 &emu->efx_pcm_mixer[i]);
874 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
875 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
876 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
877 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
880 case SNDRV_PCM_TRIGGER_STOP:
881 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
883 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
884 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
886 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
892 spin_unlock(&emu->reg_lock);
897 static snd_pcm_uframes_t snd_emu10k1_capture_pointer(snd_pcm_substream_t * substream)
899 emu10k1_t *emu = snd_pcm_substream_chip(substream);
900 snd_pcm_runtime_t *runtime = substream->runtime;
901 emu10k1_pcm_t *epcm = runtime->private_data;
906 if (epcm->first_ptr) {
907 udelay(50); // hack, it takes awhile until capture is started
910 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
911 return bytes_to_frames(runtime, ptr);
915 * Playback support device description
918 static snd_pcm_hardware_t snd_emu10k1_playback =
920 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
921 SNDRV_PCM_INFO_BLOCK_TRANSFER |
922 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
923 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
924 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
929 .buffer_bytes_max = (128*1024),
930 .period_bytes_min = 64,
931 .period_bytes_max = (128*1024),
938 * Capture support device description
941 static snd_pcm_hardware_t snd_emu10k1_capture =
943 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
944 SNDRV_PCM_INFO_BLOCK_TRANSFER |
945 SNDRV_PCM_INFO_MMAP_VALID),
946 .formats = SNDRV_PCM_FMTBIT_S16_LE,
947 .rates = SNDRV_PCM_RATE_8000_48000,
952 .buffer_bytes_max = (64*1024),
953 .period_bytes_min = 384,
954 .period_bytes_max = (64*1024),
964 static void snd_emu10k1_pcm_mixer_notify1(emu10k1_t *emu, snd_kcontrol_t *kctl, int idx, int activate)
966 snd_ctl_elem_id_t id;
968 snd_runtime_check(kctl != NULL, return);
970 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
972 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
973 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
974 SNDRV_CTL_EVENT_MASK_INFO,
975 snd_ctl_build_ioff(&id, kctl, idx));
978 static void snd_emu10k1_pcm_mixer_notify(emu10k1_t *emu, int idx, int activate)
980 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
981 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
982 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
985 static void snd_emu10k1_pcm_efx_mixer_notify(emu10k1_t *emu, int idx, int activate)
987 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
988 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
989 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
992 static void snd_emu10k1_pcm_free_substream(snd_pcm_runtime_t *runtime)
994 kfree(runtime->private_data);
997 static int snd_emu10k1_efx_playback_close(snd_pcm_substream_t * substream)
999 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1000 emu10k1_pcm_mixer_t *mix;
1003 for (i=0; i < NUM_EFX_PLAYBACK; i++) {
1004 mix = &emu->efx_pcm_mixer[i];
1006 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1011 static int snd_emu10k1_efx_playback_open(snd_pcm_substream_t * substream)
1013 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1014 emu10k1_pcm_t *epcm;
1015 emu10k1_pcm_mixer_t *mix;
1016 snd_pcm_runtime_t *runtime = substream->runtime;
1019 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1023 epcm->type = PLAYBACK_EFX;
1024 epcm->substream = substream;
1026 emu->pcm_playback_efx_substream = substream;
1028 runtime->private_data = epcm;
1029 runtime->private_free = snd_emu10k1_pcm_free_substream;
1030 runtime->hw = snd_emu10k1_efx_playback;
1032 for (i=0; i < NUM_EFX_PLAYBACK; i++) {
1033 mix = &emu->efx_pcm_mixer[i];
1034 mix->send_routing[0][0] = i;
1035 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1036 mix->send_volume[0][0] = 255;
1037 mix->attn[0] = 0xffff;
1039 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1044 static int snd_emu10k1_playback_open(snd_pcm_substream_t * substream)
1046 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1047 emu10k1_pcm_t *epcm;
1048 emu10k1_pcm_mixer_t *mix;
1049 snd_pcm_runtime_t *runtime = substream->runtime;
1052 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1056 epcm->type = PLAYBACK_EMUVOICE;
1057 epcm->substream = substream;
1058 runtime->private_data = epcm;
1059 runtime->private_free = snd_emu10k1_pcm_free_substream;
1060 runtime->hw = snd_emu10k1_playback;
1061 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
1065 if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
1069 mix = &emu->pcm_mixer[substream->number];
1070 for (i = 0; i < 4; i++)
1071 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1072 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1073 mix->send_volume[0][0] = mix->send_volume[0][1] =
1074 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1075 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1077 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1081 static int snd_emu10k1_playback_close(snd_pcm_substream_t * substream)
1083 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1084 emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[substream->number];
1087 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1091 static int snd_emu10k1_capture_open(snd_pcm_substream_t * substream)
1093 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1094 snd_pcm_runtime_t *runtime = substream->runtime;
1095 emu10k1_pcm_t *epcm;
1097 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1101 epcm->type = CAPTURE_AC97ADC;
1102 epcm->substream = substream;
1103 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1104 epcm->capture_inte = INTE_ADCBUFENABLE;
1105 epcm->capture_ba_reg = ADCBA;
1106 epcm->capture_bs_reg = ADCBS;
1107 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1108 runtime->private_data = epcm;
1109 runtime->private_free = snd_emu10k1_pcm_free_substream;
1110 runtime->hw = snd_emu10k1_capture;
1111 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1112 emu->pcm_capture_substream = substream;
1113 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1114 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1118 static int snd_emu10k1_capture_close(snd_pcm_substream_t * substream)
1120 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1122 emu->capture_interrupt = NULL;
1123 emu->pcm_capture_substream = NULL;
1127 static int snd_emu10k1_capture_mic_open(snd_pcm_substream_t * substream)
1129 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1130 emu10k1_pcm_t *epcm;
1131 snd_pcm_runtime_t *runtime = substream->runtime;
1133 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1137 epcm->type = CAPTURE_AC97MIC;
1138 epcm->substream = substream;
1139 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1140 epcm->capture_inte = INTE_MICBUFENABLE;
1141 epcm->capture_ba_reg = MICBA;
1142 epcm->capture_bs_reg = MICBS;
1143 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1144 substream->runtime->private_data = epcm;
1145 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1146 runtime->hw = snd_emu10k1_capture;
1147 runtime->hw.rates = SNDRV_PCM_RATE_8000;
1148 runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1149 runtime->hw.channels_min = 1;
1150 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1151 emu->pcm_capture_mic_substream = substream;
1152 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1156 static int snd_emu10k1_capture_mic_close(snd_pcm_substream_t * substream)
1158 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1160 emu->capture_interrupt = NULL;
1161 emu->pcm_capture_mic_substream = NULL;
1165 static int snd_emu10k1_capture_efx_open(snd_pcm_substream_t * substream)
1167 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1168 emu10k1_pcm_t *epcm;
1169 snd_pcm_runtime_t *runtime = substream->runtime;
1170 int nefx = emu->audigy ? 64 : 32;
1173 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1177 epcm->type = CAPTURE_EFX;
1178 epcm->substream = substream;
1179 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1180 epcm->capture_inte = INTE_EFXBUFENABLE;
1181 epcm->capture_ba_reg = FXBA;
1182 epcm->capture_bs_reg = FXBS;
1183 epcm->capture_idx_reg = FXIDX;
1184 substream->runtime->private_data = epcm;
1185 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1186 runtime->hw = snd_emu10k1_capture;
1187 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1188 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1189 spin_lock_irq(&emu->reg_lock);
1190 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1191 for (idx = 0; idx < nefx; idx++) {
1192 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1193 runtime->hw.channels_min++;
1194 runtime->hw.channels_max++;
1197 epcm->capture_cr_val = emu->efx_voices_mask[0];
1198 epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1199 spin_unlock_irq(&emu->reg_lock);
1200 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1201 emu->pcm_capture_efx_substream = substream;
1202 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1206 static int snd_emu10k1_capture_efx_close(snd_pcm_substream_t * substream)
1208 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1210 emu->capture_interrupt = NULL;
1211 emu->pcm_capture_efx_substream = NULL;
1215 static snd_pcm_ops_t snd_emu10k1_playback_ops = {
1216 .open = snd_emu10k1_playback_open,
1217 .close = snd_emu10k1_playback_close,
1218 .ioctl = snd_pcm_lib_ioctl,
1219 .hw_params = snd_emu10k1_playback_hw_params,
1220 .hw_free = snd_emu10k1_playback_hw_free,
1221 .prepare = snd_emu10k1_playback_prepare,
1222 .trigger = snd_emu10k1_playback_trigger,
1223 .pointer = snd_emu10k1_playback_pointer,
1224 .page = snd_pcm_sgbuf_ops_page,
1227 static snd_pcm_ops_t snd_emu10k1_capture_ops = {
1228 .open = snd_emu10k1_capture_open,
1229 .close = snd_emu10k1_capture_close,
1230 .ioctl = snd_pcm_lib_ioctl,
1231 .hw_params = snd_emu10k1_capture_hw_params,
1232 .hw_free = snd_emu10k1_capture_hw_free,
1233 .prepare = snd_emu10k1_capture_prepare,
1234 .trigger = snd_emu10k1_capture_trigger,
1235 .pointer = snd_emu10k1_capture_pointer,
1239 static snd_pcm_ops_t snd_emu10k1_efx_playback_ops = {
1240 .open = snd_emu10k1_efx_playback_open,
1241 .close = snd_emu10k1_efx_playback_close,
1242 .ioctl = snd_pcm_lib_ioctl,
1243 .hw_params = snd_emu10k1_playback_hw_params,
1244 .hw_free = snd_emu10k1_efx_playback_hw_free,
1245 .prepare = snd_emu10k1_efx_playback_prepare,
1246 .trigger = snd_emu10k1_efx_playback_trigger,
1247 .pointer = snd_emu10k1_efx_playback_pointer,
1248 .page = snd_pcm_sgbuf_ops_page,
1251 static void snd_emu10k1_pcm_free(snd_pcm_t *pcm)
1253 emu10k1_t *emu = pcm->private_data;
1255 snd_pcm_lib_preallocate_free_for_all(pcm);
1258 int __devinit snd_emu10k1_pcm(emu10k1_t * emu, int device, snd_pcm_t ** rpcm)
1261 snd_pcm_substream_t *substream;
1267 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1270 pcm->private_data = emu;
1271 pcm->private_free = snd_emu10k1_pcm_free;
1273 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1274 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1276 pcm->info_flags = 0;
1277 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1278 strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1281 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1282 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1285 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1286 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1294 int __devinit snd_emu10k1_pcm_multi(emu10k1_t * emu, int device, snd_pcm_t ** rpcm)
1297 snd_pcm_substream_t *substream;
1303 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
1306 pcm->private_data = emu;
1307 pcm->private_free = snd_emu10k1_pcm_free;
1309 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1311 pcm->info_flags = 0;
1312 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1313 strcpy(pcm->name, "Multichannel Playback");
1316 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1317 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1327 static snd_pcm_ops_t snd_emu10k1_capture_mic_ops = {
1328 .open = snd_emu10k1_capture_mic_open,
1329 .close = snd_emu10k1_capture_mic_close,
1330 .ioctl = snd_pcm_lib_ioctl,
1331 .hw_params = snd_emu10k1_capture_hw_params,
1332 .hw_free = snd_emu10k1_capture_hw_free,
1333 .prepare = snd_emu10k1_capture_prepare,
1334 .trigger = snd_emu10k1_capture_trigger,
1335 .pointer = snd_emu10k1_capture_pointer,
1338 static void snd_emu10k1_pcm_mic_free(snd_pcm_t *pcm)
1340 emu10k1_t *emu = pcm->private_data;
1341 emu->pcm_mic = NULL;
1342 snd_pcm_lib_preallocate_free_for_all(pcm);
1345 int __devinit snd_emu10k1_pcm_mic(emu10k1_t * emu, int device, snd_pcm_t ** rpcm)
1353 if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
1356 pcm->private_data = emu;
1357 pcm->private_free = snd_emu10k1_pcm_mic_free;
1359 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1361 pcm->info_flags = 0;
1362 strcpy(pcm->name, "Mic Capture");
1365 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1372 static int snd_emu10k1_pcm_efx_voices_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1374 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
1375 int nefx = emu->audigy ? 64 : 32;
1376 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1377 uinfo->count = nefx;
1378 uinfo->value.integer.min = 0;
1379 uinfo->value.integer.max = 1;
1383 static int snd_emu10k1_pcm_efx_voices_mask_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1385 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
1386 int nefx = emu->audigy ? 64 : 32;
1389 spin_lock_irq(&emu->reg_lock);
1390 for (idx = 0; idx < nefx; idx++)
1391 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1392 spin_unlock_irq(&emu->reg_lock);
1396 static int snd_emu10k1_pcm_efx_voices_mask_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1398 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
1399 unsigned int nval[2], bits;
1400 int nefx = emu->audigy ? 64 : 32;
1401 int nefxb = emu->audigy ? 7 : 6;
1404 nval[0] = nval[1] = 0;
1405 for (idx = 0, bits = 0; idx < nefx; idx++)
1406 if (ucontrol->value.integer.value[idx]) {
1407 nval[idx / 32] |= 1 << (idx % 32);
1411 for (idx = 0; idx < nefxb; idx++)
1412 if (1 << idx == bits)
1418 spin_lock_irq(&emu->reg_lock);
1419 change = (nval[0] != emu->efx_voices_mask[0]) ||
1420 (nval[1] != emu->efx_voices_mask[1]);
1421 emu->efx_voices_mask[0] = nval[0];
1422 emu->efx_voices_mask[1] = nval[1];
1423 spin_unlock_irq(&emu->reg_lock);
1427 static snd_kcontrol_new_t snd_emu10k1_pcm_efx_voices_mask = {
1428 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1429 .name = "Captured FX8010 Outputs",
1430 .info = snd_emu10k1_pcm_efx_voices_mask_info,
1431 .get = snd_emu10k1_pcm_efx_voices_mask_get,
1432 .put = snd_emu10k1_pcm_efx_voices_mask_put
1435 static snd_pcm_ops_t snd_emu10k1_capture_efx_ops = {
1436 .open = snd_emu10k1_capture_efx_open,
1437 .close = snd_emu10k1_capture_efx_close,
1438 .ioctl = snd_pcm_lib_ioctl,
1439 .hw_params = snd_emu10k1_capture_hw_params,
1440 .hw_free = snd_emu10k1_capture_hw_free,
1441 .prepare = snd_emu10k1_capture_prepare,
1442 .trigger = snd_emu10k1_capture_trigger,
1443 .pointer = snd_emu10k1_capture_pointer,
1449 #define INITIAL_TRAM_SHIFT 14
1450 #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1452 static void snd_emu10k1_fx8010_playback_irq(emu10k1_t *emu, void *private_data)
1454 snd_pcm_substream_t *substream = private_data;
1455 snd_pcm_period_elapsed(substream);
1458 static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1459 unsigned short *dst_right,
1460 unsigned short *src,
1462 unsigned int tram_shift)
1464 // printk("tram_poke1: dst_left = 0x%p, dst_right = 0x%p, src = 0x%p, count = 0x%x\n", dst_left, dst_right, src, count);
1465 if ((tram_shift & 1) == 0) {
1467 *dst_left-- = *src++;
1468 *dst_right-- = *src++;
1472 *dst_right-- = *src++;
1473 *dst_left-- = *src++;
1478 static void fx8010_pb_trans_copy(snd_pcm_substream_t *substream,
1479 snd_pcm_indirect_t *rec, size_t bytes)
1481 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1482 snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1483 unsigned int tram_size = pcm->buffer_size;
1484 unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1485 unsigned int frames = bytes >> 2, count;
1486 unsigned int tram_pos = pcm->tram_pos;
1487 unsigned int tram_shift = pcm->tram_shift;
1489 while (frames > tram_pos) {
1490 count = tram_pos + 1;
1491 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1492 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1493 src, count, tram_shift);
1496 tram_pos = (tram_size / 2) - 1;
1499 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1500 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1501 src, frames, tram_shift);
1503 pcm->tram_pos = tram_pos;
1504 pcm->tram_shift = tram_shift;
1507 static int snd_emu10k1_fx8010_playback_transfer(snd_pcm_substream_t *substream)
1509 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1510 snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1512 snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy);
1516 static int snd_emu10k1_fx8010_playback_hw_params(snd_pcm_substream_t * substream,
1517 snd_pcm_hw_params_t * hw_params)
1519 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1522 static int snd_emu10k1_fx8010_playback_hw_free(snd_pcm_substream_t * substream)
1524 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1525 snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1528 for (i = 0; i < pcm->channels; i++)
1529 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1530 snd_pcm_lib_free_pages(substream);
1534 static int snd_emu10k1_fx8010_playback_prepare(snd_pcm_substream_t * substream)
1536 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1537 snd_pcm_runtime_t *runtime = substream->runtime;
1538 snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1541 // printk("prepare: etram_pages = 0x%p, dma_area = 0x%x, buffer_size = 0x%x (0x%x)\n", emu->fx8010.etram_pages, runtime->dma_area, runtime->buffer_size, runtime->buffer_size << 2);
1542 memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1543 pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1544 pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1545 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1546 pcm->tram_shift = 0;
1547 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */
1548 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */
1549 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1550 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */
1551 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1552 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1553 for (i = 0; i < pcm->channels; i++)
1554 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1558 static int snd_emu10k1_fx8010_playback_trigger(snd_pcm_substream_t * substream, int cmd)
1560 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1561 snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1564 spin_lock(&emu->reg_lock);
1566 case SNDRV_PCM_TRIGGER_START:
1568 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1569 #ifdef EMU10K1_SET_AC3_IEC958
1572 for (i = 0; i < 3; i++) {
1574 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1575 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1576 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1577 snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1581 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1584 snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
1585 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1587 case SNDRV_PCM_TRIGGER_STOP:
1588 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1589 snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL;
1590 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1591 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1592 pcm->tram_shift = 0;
1599 spin_unlock(&emu->reg_lock);
1603 static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(snd_pcm_substream_t * substream)
1605 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1606 snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1607 size_t ptr; /* byte pointer */
1609 if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1611 ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1612 return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1615 static snd_pcm_hardware_t snd_emu10k1_fx8010_playback =
1617 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1618 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE),
1619 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1620 .rates = SNDRV_PCM_RATE_48000,
1625 .buffer_bytes_max = (128*1024),
1626 .period_bytes_min = 1024,
1627 .period_bytes_max = (128*1024),
1629 .periods_max = 1024,
1633 static int snd_emu10k1_fx8010_playback_open(snd_pcm_substream_t * substream)
1635 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1636 snd_pcm_runtime_t *runtime = substream->runtime;
1637 snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1639 runtime->hw = snd_emu10k1_fx8010_playback;
1640 runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1641 runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1642 spin_lock_irq(&emu->reg_lock);
1643 if (pcm->valid == 0) {
1644 spin_unlock_irq(&emu->reg_lock);
1648 spin_unlock_irq(&emu->reg_lock);
1652 static int snd_emu10k1_fx8010_playback_close(snd_pcm_substream_t * substream)
1654 emu10k1_t *emu = snd_pcm_substream_chip(substream);
1655 snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1657 spin_lock_irq(&emu->reg_lock);
1659 spin_unlock_irq(&emu->reg_lock);
1663 static snd_pcm_ops_t snd_emu10k1_fx8010_playback_ops = {
1664 .open = snd_emu10k1_fx8010_playback_open,
1665 .close = snd_emu10k1_fx8010_playback_close,
1666 .ioctl = snd_pcm_lib_ioctl,
1667 .hw_params = snd_emu10k1_fx8010_playback_hw_params,
1668 .hw_free = snd_emu10k1_fx8010_playback_hw_free,
1669 .prepare = snd_emu10k1_fx8010_playback_prepare,
1670 .trigger = snd_emu10k1_fx8010_playback_trigger,
1671 .pointer = snd_emu10k1_fx8010_playback_pointer,
1672 .ack = snd_emu10k1_fx8010_playback_transfer,
1675 static void snd_emu10k1_pcm_efx_free(snd_pcm_t *pcm)
1677 emu10k1_t *emu = pcm->private_data;
1678 emu->pcm_efx = NULL;
1679 snd_pcm_lib_preallocate_free_for_all(pcm);
1682 int __devinit snd_emu10k1_pcm_efx(emu10k1_t * emu, int device, snd_pcm_t ** rpcm)
1685 snd_kcontrol_t *kctl;
1691 if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1694 pcm->private_data = emu;
1695 pcm->private_free = snd_emu10k1_pcm_efx_free;
1697 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1698 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1700 pcm->info_flags = 0;
1701 strcpy(pcm->name, "Multichannel Capture/PT Playback");
1706 /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
1710 /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1712 emu->efx_voices_mask[0] = 0;
1713 emu->efx_voices_mask[1] = 0xffff;
1715 emu->efx_voices_mask[0] = 0xffff0000;
1716 emu->efx_voices_mask[1] = 0;
1718 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1721 kctl->id.device = device;
1722 snd_ctl_add(emu->card, kctl);
1724 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);