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(struct snd_emu10k1 *emu,
39 struct snd_emu10k1_voice *voice)
41 struct snd_emu10k1_pcm *epcm;
43 if ((epcm = voice->epcm) == NULL)
45 if (epcm->substream == NULL)
48 printk("IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
49 epcm->substream->runtime->hw->pointer(emu, epcm->substream),
50 snd_pcm_lib_period_bytes(epcm->substream),
51 snd_pcm_lib_buffer_bytes(epcm->substream));
53 snd_pcm_period_elapsed(epcm->substream);
56 static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu,
60 if (status & IPR_ADCBUFHALFFULL) {
61 if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
65 snd_pcm_period_elapsed(emu->pcm_capture_substream);
68 static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu,
72 if (status & IPR_MICBUFHALFFULL) {
73 if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
77 snd_pcm_period_elapsed(emu->pcm_capture_mic_substream);
80 static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu,
84 if (status & IPR_EFXBUFHALFFULL) {
85 if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
89 snd_pcm_period_elapsed(emu->pcm_capture_efx_substream);
92 static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(struct snd_pcm_substream *substream)
94 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
95 struct snd_pcm_runtime *runtime = substream->runtime;
96 struct snd_emu10k1_pcm *epcm = runtime->private_data;
101 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
102 ptr += runtime->buffer_size;
103 ptr -= epcm->ccca_start_addr;
104 ptr %= runtime->buffer_size;
109 static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm, int voices)
113 if (epcm->voices[1] != NULL && voices < 2) {
114 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
115 epcm->voices[1] = NULL;
117 for (i = 0; i < voices; i++) {
118 if (epcm->voices[i] == NULL)
122 return 0; /* already allocated */
124 for (i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
125 if (epcm->voices[i]) {
126 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
127 epcm->voices[i] = NULL;
130 err = snd_emu10k1_voice_alloc(epcm->emu,
131 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
137 epcm->voices[0]->epcm = epcm;
139 for (i = 1; i < voices; i++) {
140 epcm->voices[i] = &epcm->emu->voices[epcm->voices[0]->number + i];
141 epcm->voices[i]->epcm = epcm;
144 if (epcm->extra == NULL) {
145 err = snd_emu10k1_voice_alloc(epcm->emu,
146 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
150 // printk("pcm_channel_alloc: failed extra: voices=%d, frame=%d\n", voices, frame);
151 for (i = 0; i < voices; i++) {
152 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
153 epcm->voices[i] = NULL;
157 epcm->extra->epcm = epcm;
158 epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
163 static unsigned int capture_period_sizes[31] = {
165 384*2, 448*2, 512*2, 640*2,
166 384*4, 448*4, 512*4, 640*4,
167 384*8, 448*8, 512*8, 640*8,
168 384*16, 448*16, 512*16, 640*16,
169 384*32, 448*32, 512*32, 640*32,
170 384*64, 448*64, 512*64, 640*64,
171 384*128,448*128,512*128
174 static struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
176 .list = capture_period_sizes,
180 static unsigned int capture_rates[8] = {
181 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
184 static struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
186 .list = capture_rates,
190 static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
193 case 8000: return ADCCR_SAMPLERATE_8;
194 case 11025: return ADCCR_SAMPLERATE_11;
195 case 16000: return ADCCR_SAMPLERATE_16;
196 case 22050: return ADCCR_SAMPLERATE_22;
197 case 24000: return ADCCR_SAMPLERATE_24;
198 case 32000: return ADCCR_SAMPLERATE_32;
199 case 44100: return ADCCR_SAMPLERATE_44;
200 case 48000: return ADCCR_SAMPLERATE_48;
203 return ADCCR_SAMPLERATE_8;
207 static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
210 case 8000: return A_ADCCR_SAMPLERATE_8;
211 case 11025: return A_ADCCR_SAMPLERATE_11;
212 case 12000: return A_ADCCR_SAMPLERATE_12; /* really supported? */
213 case 16000: return ADCCR_SAMPLERATE_16;
214 case 22050: return ADCCR_SAMPLERATE_22;
215 case 24000: return ADCCR_SAMPLERATE_24;
216 case 32000: return ADCCR_SAMPLERATE_32;
217 case 44100: return ADCCR_SAMPLERATE_44;
218 case 48000: return ADCCR_SAMPLERATE_48;
221 return A_ADCCR_SAMPLERATE_8;
225 static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
227 unsigned int pitch_target;
229 pitch_target = (rate << 8) / 375;
230 pitch_target = (pitch_target >> 1) + (pitch_target & 1);
234 #define PITCH_48000 0x00004000
235 #define PITCH_96000 0x00008000
236 #define PITCH_85000 0x00007155
237 #define PITCH_80726 0x00006ba2
238 #define PITCH_67882 0x00005a82
239 #define PITCH_57081 0x00004c1c
241 static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
243 if (pitch_target == PITCH_48000)
244 return CCCA_INTERPROM_0;
245 else if (pitch_target < PITCH_48000)
246 return CCCA_INTERPROM_1;
247 else if (pitch_target >= PITCH_96000)
248 return CCCA_INTERPROM_0;
249 else if (pitch_target >= PITCH_85000)
250 return CCCA_INTERPROM_6;
251 else if (pitch_target >= PITCH_80726)
252 return CCCA_INTERPROM_5;
253 else if (pitch_target >= PITCH_67882)
254 return CCCA_INTERPROM_4;
255 else if (pitch_target >= PITCH_57081)
256 return CCCA_INTERPROM_3;
258 return CCCA_INTERPROM_2;
262 * calculate cache invalidate size
264 * stereo: channel is stereo
265 * w_16: using 16bit samples
267 * returns: cache invalidate size in samples
269 static inline int emu10k1_ccis(int stereo, int w_16)
272 return stereo ? 24 : 26;
274 return stereo ? 24*2 : 26*2;
278 static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
279 int master, int extra,
280 struct snd_emu10k1_voice *evoice,
281 unsigned int start_addr,
282 unsigned int end_addr,
283 struct snd_emu10k1_pcm_mixer *mix)
285 struct snd_pcm_substream *substream = evoice->epcm->substream;
286 struct snd_pcm_runtime *runtime = substream->runtime;
287 unsigned int silent_page, tmp;
288 int voice, stereo, w_16;
289 unsigned char attn, send_amount[8];
290 unsigned char send_routing[8];
292 unsigned int pitch_target;
295 voice = evoice->number;
296 stereo = runtime->channels == 2;
297 w_16 = snd_pcm_format_width(runtime->format) == 16;
299 if (!extra && stereo) {
308 spin_lock_irqsave(&emu->reg_lock, flags);
310 /* volume parameters */
313 memset(send_routing, 0, sizeof(send_routing));
318 memset(send_amount, 0, sizeof(send_amount));
320 /* mono, left, right (master voice = left) */
321 tmp = stereo ? (master ? 1 : 2) : 0;
322 memcpy(send_routing, &mix->send_routing[tmp][0], 8);
323 memcpy(send_amount, &mix->send_volume[tmp][0], 8);
326 ccis = emu10k1_ccis(stereo, w_16);
329 evoice->epcm->ccca_start_addr = start_addr + ccis;
334 if (stereo && !extra) {
335 snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK);
336 snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK);
338 snd_emu10k1_ptr_write(emu, CPF, voice, 0);
344 snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
345 snd_emu10k1_compose_audigy_fxrt1(send_routing));
346 snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
347 snd_emu10k1_compose_audigy_fxrt2(send_routing));
348 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
349 ((unsigned int)send_amount[4] << 24) |
350 ((unsigned int)send_amount[5] << 16) |
351 ((unsigned int)send_amount[6] << 8) |
352 (unsigned int)send_amount[7]);
354 snd_emu10k1_ptr_write(emu, FXRT, voice,
355 snd_emu10k1_compose_send_routing(send_routing));
357 // Assumption that PT is already 0 so no harm overwriting
358 snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]);
359 snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24));
360 snd_emu10k1_ptr_write(emu, PSST, voice, start_addr | (send_amount[2] << 24));
361 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
363 snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
364 emu10k1_select_interprom(pitch_target) |
365 (w_16 ? 0 : CCCA_8BITSELECT));
367 snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
368 emu10k1_select_interprom(pitch_target) |
369 (w_16 ? 0 : CCCA_8BITSELECT));
370 // Clear filter delay memory
371 snd_emu10k1_ptr_write(emu, Z1, voice, 0);
372 snd_emu10k1_ptr_write(emu, Z2, voice, 0);
374 silent_page = ((unsigned int)emu->silent_page.addr << 1) | MAP_PTI_MASK;
375 snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
376 snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
377 // modulation envelope
378 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
379 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
380 snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
381 snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
382 snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
383 snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
384 snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
385 snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
386 snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
387 snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
389 snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
390 snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
392 snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
394 snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
396 spin_unlock_irqrestore(&emu->reg_lock, flags);
399 static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
400 struct snd_pcm_hw_params *hw_params)
402 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
403 struct snd_pcm_runtime *runtime = substream->runtime;
404 struct snd_emu10k1_pcm *epcm = runtime->private_data;
407 if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0)
409 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
411 if (err > 0) { /* change */
413 if (epcm->memblk != NULL)
414 snd_emu10k1_free_pages(emu, epcm->memblk);
415 epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
416 epcm->start_addr = 0;
419 mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
422 epcm->start_addr = mapped << PAGE_SHIFT;
427 static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
429 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
430 struct snd_pcm_runtime *runtime = substream->runtime;
431 struct snd_emu10k1_pcm *epcm;
433 if (runtime->private_data == NULL)
435 epcm = runtime->private_data;
437 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
440 if (epcm->voices[1]) {
441 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
442 epcm->voices[1] = NULL;
444 if (epcm->voices[0]) {
445 snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
446 epcm->voices[0] = NULL;
449 snd_emu10k1_free_pages(emu, epcm->memblk);
451 epcm->start_addr = 0;
453 snd_pcm_lib_free_pages(substream);
457 static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
459 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
460 struct snd_pcm_runtime *runtime = substream->runtime;
461 struct snd_emu10k1_pcm *epcm;
464 if (runtime->private_data == NULL)
466 epcm = runtime->private_data;
468 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
471 for (i=0; i < NUM_EFX_PLAYBACK; i++) {
472 if (epcm->voices[i]) {
473 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
474 epcm->voices[i] = NULL;
478 snd_emu10k1_free_pages(emu, epcm->memblk);
480 epcm->start_addr = 0;
482 snd_pcm_lib_free_pages(substream);
486 static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
488 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
489 struct snd_pcm_runtime *runtime = substream->runtime;
490 struct snd_emu10k1_pcm *epcm = runtime->private_data;
491 unsigned int start_addr, end_addr;
493 start_addr = epcm->start_addr;
494 end_addr = snd_pcm_lib_period_bytes(substream);
495 if (runtime->channels == 2) {
499 end_addr += start_addr;
500 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
501 start_addr, end_addr, NULL);
502 start_addr = epcm->start_addr;
503 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
504 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
505 start_addr, end_addr,
506 &emu->pcm_mixer[substream->number]);
508 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
509 start_addr, end_addr,
510 &emu->pcm_mixer[substream->number]);
514 static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
516 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
517 struct snd_pcm_runtime *runtime = substream->runtime;
518 struct snd_emu10k1_pcm *epcm = runtime->private_data;
519 unsigned int start_addr, end_addr;
520 unsigned int channel_size;
523 start_addr = epcm->start_addr;
524 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
527 * the kX driver leaves some space between voices
529 channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
531 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
532 start_addr, start_addr + (channel_size / 2), NULL);
534 /* only difference with the master voice is we use it for the pointer */
535 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
536 start_addr, start_addr + channel_size,
537 &emu->efx_pcm_mixer[0]);
539 start_addr += channel_size;
540 for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
541 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
542 start_addr, start_addr + channel_size,
543 &emu->efx_pcm_mixer[i]);
544 start_addr += channel_size;
550 static struct snd_pcm_hardware snd_emu10k1_efx_playback =
552 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
553 SNDRV_PCM_INFO_BLOCK_TRANSFER |
554 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
555 .formats = SNDRV_PCM_FMTBIT_S16_LE,
556 .rates = SNDRV_PCM_RATE_48000,
559 .channels_min = NUM_EFX_PLAYBACK,
560 .channels_max = NUM_EFX_PLAYBACK,
561 .buffer_bytes_max = (64*1024),
562 .period_bytes_min = 64,
563 .period_bytes_max = (64*1024),
569 static int snd_emu10k1_capture_hw_params(struct snd_pcm_substream *substream,
570 struct snd_pcm_hw_params *hw_params)
572 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
575 static int snd_emu10k1_capture_hw_free(struct snd_pcm_substream *substream)
577 return snd_pcm_lib_free_pages(substream);
580 static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
582 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
583 struct snd_pcm_runtime *runtime = substream->runtime;
584 struct snd_emu10k1_pcm *epcm = runtime->private_data;
587 /* zeroing the buffer size will stop capture */
588 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
589 switch (epcm->type) {
590 case CAPTURE_AC97ADC:
591 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
595 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
596 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
598 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
603 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
604 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
605 epcm->capture_bs_val = 0;
606 for (idx = 0; idx < 31; idx++) {
607 if (capture_period_sizes[idx] == epcm->capture_bufsize) {
608 epcm->capture_bs_val = idx + 1;
612 if (epcm->capture_bs_val == 0) {
614 epcm->capture_bs_val++;
616 if (epcm->type == CAPTURE_AC97ADC) {
617 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
618 if (runtime->channels > 1)
619 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
620 epcm->capture_cr_val |= emu->audigy ?
621 snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
622 snd_emu10k1_capture_rate_reg(runtime->rate);
627 static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
629 struct snd_pcm_runtime *runtime;
630 unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
634 runtime = evoice->epcm->substream->runtime;
635 voice = evoice->number;
636 stereo = (!extra && runtime->channels == 2);
637 sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
638 ccis = emu10k1_ccis(stereo, sample == 0);
639 // set cs to 2 * number of cache registers beside the invalidated
640 cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
641 if (cs > 16) cs = 16;
642 for (i = 0; i < cs; i++) {
643 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
645 snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
649 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
650 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
652 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
653 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
656 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
658 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
662 static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
663 int master, int extra,
664 struct snd_emu10k1_pcm_mixer *mix)
666 struct snd_pcm_substream *substream;
667 struct snd_pcm_runtime *runtime;
668 unsigned int attn, vattn;
669 unsigned int voice, tmp;
671 if (evoice == NULL) /* skip second voice for mono */
673 substream = evoice->epcm->substream;
674 runtime = substream->runtime;
675 voice = evoice->number;
677 attn = extra ? 0 : 0x00ff;
678 tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
679 vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
680 snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
681 snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
682 snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
683 snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
684 snd_emu10k1_voice_clear_loop_stop(emu, voice);
687 static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra)
689 struct snd_pcm_substream *substream;
690 struct snd_pcm_runtime *runtime;
691 unsigned int voice, pitch, pitch_target;
693 if (evoice == NULL) /* skip second voice for mono */
695 substream = evoice->epcm->substream;
696 runtime = substream->runtime;
697 voice = evoice->number;
699 pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
700 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
701 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
702 if (master || evoice->epcm->type == PLAYBACK_EFX)
703 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
704 snd_emu10k1_ptr_write(emu, IP, voice, pitch);
706 snd_emu10k1_voice_intr_enable(emu, voice);
709 static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
715 voice = evoice->number;
716 snd_emu10k1_voice_intr_disable(emu, voice);
717 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
718 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
719 snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
720 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
721 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
722 snd_emu10k1_ptr_write(emu, IP, voice, 0);
725 static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
728 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
729 struct snd_pcm_runtime *runtime = substream->runtime;
730 struct snd_emu10k1_pcm *epcm = runtime->private_data;
731 struct snd_emu10k1_pcm_mixer *mix;
734 // printk("trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n", (int)emu, cmd, substream->ops->pointer(substream));
735 spin_lock(&emu->reg_lock);
737 case SNDRV_PCM_TRIGGER_START:
738 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */
739 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
741 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
742 mix = &emu->pcm_mixer[substream->number];
743 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
744 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
745 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
746 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
747 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
748 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
751 case SNDRV_PCM_TRIGGER_STOP:
752 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
754 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
755 snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
756 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
762 spin_unlock(&emu->reg_lock);
766 static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
769 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
770 struct snd_pcm_runtime *runtime = substream->runtime;
771 struct snd_emu10k1_pcm *epcm = runtime->private_data;
774 spin_lock(&emu->reg_lock);
776 case SNDRV_PCM_TRIGGER_START:
777 // hmm this should cause full and half full interrupt to be raised?
778 outl(epcm->capture_ipr, emu->port + IPR);
779 snd_emu10k1_intr_enable(emu, epcm->capture_inte);
780 // printk("adccr = 0x%x, adcbs = 0x%x\n", epcm->adccr, epcm->adcbs);
781 switch (epcm->type) {
782 case CAPTURE_AC97ADC:
783 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
787 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
788 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
790 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
795 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
799 case SNDRV_PCM_TRIGGER_STOP:
801 snd_emu10k1_intr_disable(emu, epcm->capture_inte);
802 outl(epcm->capture_ipr, emu->port + IPR);
803 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
804 switch (epcm->type) {
805 case CAPTURE_AC97ADC:
806 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
810 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
811 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
813 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
822 spin_unlock(&emu->reg_lock);
826 static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
828 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
829 struct snd_pcm_runtime *runtime = substream->runtime;
830 struct snd_emu10k1_pcm *epcm = runtime->private_data;
835 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
836 #if 0 /* Perex's code */
837 ptr += runtime->buffer_size;
838 ptr -= epcm->ccca_start_addr;
839 ptr %= runtime->buffer_size;
840 #else /* EMU10K1 Open Source code from Creative */
841 if (ptr < epcm->ccca_start_addr)
842 ptr += runtime->buffer_size - epcm->ccca_start_addr;
844 ptr -= epcm->ccca_start_addr;
845 if (ptr >= runtime->buffer_size)
846 ptr -= runtime->buffer_size;
849 // printk("ptr = 0x%x, buffer_size = 0x%x, period_size = 0x%x\n", ptr, runtime->buffer_size, runtime->period_size);
854 static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
857 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
858 struct snd_pcm_runtime *runtime = substream->runtime;
859 struct snd_emu10k1_pcm *epcm = runtime->private_data;
863 spin_lock(&emu->reg_lock);
865 case SNDRV_PCM_TRIGGER_START:
867 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
868 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
870 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
873 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
874 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
875 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
876 &emu->efx_pcm_mixer[0]);
877 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
878 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
879 &emu->efx_pcm_mixer[i]);
880 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
881 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
882 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
883 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
886 case SNDRV_PCM_TRIGGER_STOP:
887 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
889 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
890 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
892 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
898 spin_unlock(&emu->reg_lock);
903 static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
905 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
906 struct snd_pcm_runtime *runtime = substream->runtime;
907 struct snd_emu10k1_pcm *epcm = runtime->private_data;
912 if (epcm->first_ptr) {
913 udelay(50); // hack, it takes awhile until capture is started
916 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
917 return bytes_to_frames(runtime, ptr);
921 * Playback support device description
924 static struct snd_pcm_hardware snd_emu10k1_playback =
926 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
927 SNDRV_PCM_INFO_BLOCK_TRANSFER |
928 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
929 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
930 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
935 .buffer_bytes_max = (128*1024),
936 .period_bytes_min = 64,
937 .period_bytes_max = (128*1024),
944 * Capture support device description
947 static struct snd_pcm_hardware snd_emu10k1_capture =
949 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
950 SNDRV_PCM_INFO_BLOCK_TRANSFER |
951 SNDRV_PCM_INFO_MMAP_VALID),
952 .formats = SNDRV_PCM_FMTBIT_S16_LE,
953 .rates = SNDRV_PCM_RATE_8000_48000,
958 .buffer_bytes_max = (64*1024),
959 .period_bytes_min = 384,
960 .period_bytes_max = (64*1024),
970 static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
972 struct snd_ctl_elem_id id;
977 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
979 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
980 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
981 SNDRV_CTL_EVENT_MASK_INFO,
982 snd_ctl_build_ioff(&id, kctl, idx));
985 static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
987 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
988 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
989 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
992 static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
994 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
995 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
996 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
999 static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
1001 kfree(runtime->private_data);
1004 static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
1006 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1007 struct snd_emu10k1_pcm_mixer *mix;
1010 for (i=0; i < NUM_EFX_PLAYBACK; i++) {
1011 mix = &emu->efx_pcm_mixer[i];
1013 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1018 static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
1020 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1021 struct snd_emu10k1_pcm *epcm;
1022 struct snd_emu10k1_pcm_mixer *mix;
1023 struct snd_pcm_runtime *runtime = substream->runtime;
1026 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1030 epcm->type = PLAYBACK_EFX;
1031 epcm->substream = substream;
1033 emu->pcm_playback_efx_substream = substream;
1035 runtime->private_data = epcm;
1036 runtime->private_free = snd_emu10k1_pcm_free_substream;
1037 runtime->hw = snd_emu10k1_efx_playback;
1039 for (i=0; i < NUM_EFX_PLAYBACK; i++) {
1040 mix = &emu->efx_pcm_mixer[i];
1041 mix->send_routing[0][0] = i;
1042 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1043 mix->send_volume[0][0] = 255;
1044 mix->attn[0] = 0xffff;
1046 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1051 static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
1053 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1054 struct snd_emu10k1_pcm *epcm;
1055 struct snd_emu10k1_pcm_mixer *mix;
1056 struct snd_pcm_runtime *runtime = substream->runtime;
1059 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1063 epcm->type = PLAYBACK_EMUVOICE;
1064 epcm->substream = substream;
1065 runtime->private_data = epcm;
1066 runtime->private_free = snd_emu10k1_pcm_free_substream;
1067 runtime->hw = snd_emu10k1_playback;
1068 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
1072 if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
1076 mix = &emu->pcm_mixer[substream->number];
1077 for (i = 0; i < 4; i++)
1078 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1079 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1080 mix->send_volume[0][0] = mix->send_volume[0][1] =
1081 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1082 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1084 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1088 static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
1090 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1091 struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
1094 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1098 static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
1100 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1101 struct snd_pcm_runtime *runtime = substream->runtime;
1102 struct snd_emu10k1_pcm *epcm;
1104 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1108 epcm->type = CAPTURE_AC97ADC;
1109 epcm->substream = substream;
1110 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1111 epcm->capture_inte = INTE_ADCBUFENABLE;
1112 epcm->capture_ba_reg = ADCBA;
1113 epcm->capture_bs_reg = ADCBS;
1114 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1115 runtime->private_data = epcm;
1116 runtime->private_free = snd_emu10k1_pcm_free_substream;
1117 runtime->hw = snd_emu10k1_capture;
1118 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1119 emu->pcm_capture_substream = substream;
1120 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1121 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1125 static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
1127 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1129 emu->capture_interrupt = NULL;
1130 emu->pcm_capture_substream = NULL;
1134 static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
1136 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1137 struct snd_emu10k1_pcm *epcm;
1138 struct snd_pcm_runtime *runtime = substream->runtime;
1140 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1144 epcm->type = CAPTURE_AC97MIC;
1145 epcm->substream = substream;
1146 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1147 epcm->capture_inte = INTE_MICBUFENABLE;
1148 epcm->capture_ba_reg = MICBA;
1149 epcm->capture_bs_reg = MICBS;
1150 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1151 substream->runtime->private_data = epcm;
1152 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1153 runtime->hw = snd_emu10k1_capture;
1154 runtime->hw.rates = SNDRV_PCM_RATE_8000;
1155 runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1156 runtime->hw.channels_min = 1;
1157 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1158 emu->pcm_capture_mic_substream = substream;
1159 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1163 static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
1165 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1167 emu->capture_interrupt = NULL;
1168 emu->pcm_capture_mic_substream = NULL;
1172 static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
1174 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1175 struct snd_emu10k1_pcm *epcm;
1176 struct snd_pcm_runtime *runtime = substream->runtime;
1177 int nefx = emu->audigy ? 64 : 32;
1180 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1184 epcm->type = CAPTURE_EFX;
1185 epcm->substream = substream;
1186 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1187 epcm->capture_inte = INTE_EFXBUFENABLE;
1188 epcm->capture_ba_reg = FXBA;
1189 epcm->capture_bs_reg = FXBS;
1190 epcm->capture_idx_reg = FXIDX;
1191 substream->runtime->private_data = epcm;
1192 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1193 runtime->hw = snd_emu10k1_capture;
1194 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1195 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1196 spin_lock_irq(&emu->reg_lock);
1197 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1198 for (idx = 0; idx < nefx; idx++) {
1199 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1200 runtime->hw.channels_min++;
1201 runtime->hw.channels_max++;
1204 epcm->capture_cr_val = emu->efx_voices_mask[0];
1205 epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1206 spin_unlock_irq(&emu->reg_lock);
1207 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1208 emu->pcm_capture_efx_substream = substream;
1209 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1213 static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
1215 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1217 emu->capture_interrupt = NULL;
1218 emu->pcm_capture_efx_substream = NULL;
1222 static struct snd_pcm_ops snd_emu10k1_playback_ops = {
1223 .open = snd_emu10k1_playback_open,
1224 .close = snd_emu10k1_playback_close,
1225 .ioctl = snd_pcm_lib_ioctl,
1226 .hw_params = snd_emu10k1_playback_hw_params,
1227 .hw_free = snd_emu10k1_playback_hw_free,
1228 .prepare = snd_emu10k1_playback_prepare,
1229 .trigger = snd_emu10k1_playback_trigger,
1230 .pointer = snd_emu10k1_playback_pointer,
1231 .page = snd_pcm_sgbuf_ops_page,
1234 static struct snd_pcm_ops snd_emu10k1_capture_ops = {
1235 .open = snd_emu10k1_capture_open,
1236 .close = snd_emu10k1_capture_close,
1237 .ioctl = snd_pcm_lib_ioctl,
1238 .hw_params = snd_emu10k1_capture_hw_params,
1239 .hw_free = snd_emu10k1_capture_hw_free,
1240 .prepare = snd_emu10k1_capture_prepare,
1241 .trigger = snd_emu10k1_capture_trigger,
1242 .pointer = snd_emu10k1_capture_pointer,
1246 static struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
1247 .open = snd_emu10k1_efx_playback_open,
1248 .close = snd_emu10k1_efx_playback_close,
1249 .ioctl = snd_pcm_lib_ioctl,
1250 .hw_params = snd_emu10k1_playback_hw_params,
1251 .hw_free = snd_emu10k1_efx_playback_hw_free,
1252 .prepare = snd_emu10k1_efx_playback_prepare,
1253 .trigger = snd_emu10k1_efx_playback_trigger,
1254 .pointer = snd_emu10k1_efx_playback_pointer,
1255 .page = snd_pcm_sgbuf_ops_page,
1258 int __devinit snd_emu10k1_pcm(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1260 struct snd_pcm *pcm;
1261 struct snd_pcm_substream *substream;
1267 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1270 pcm->private_data = emu;
1272 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1273 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1275 pcm->info_flags = 0;
1276 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1277 strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1280 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1281 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1284 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1285 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1293 int __devinit snd_emu10k1_pcm_multi(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1295 struct snd_pcm *pcm;
1296 struct snd_pcm_substream *substream;
1302 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
1305 pcm->private_data = emu;
1307 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1309 pcm->info_flags = 0;
1310 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1311 strcpy(pcm->name, "Multichannel Playback");
1314 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1315 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1325 static struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
1326 .open = snd_emu10k1_capture_mic_open,
1327 .close = snd_emu10k1_capture_mic_close,
1328 .ioctl = snd_pcm_lib_ioctl,
1329 .hw_params = snd_emu10k1_capture_hw_params,
1330 .hw_free = snd_emu10k1_capture_hw_free,
1331 .prepare = snd_emu10k1_capture_prepare,
1332 .trigger = snd_emu10k1_capture_trigger,
1333 .pointer = snd_emu10k1_capture_pointer,
1336 int __devinit snd_emu10k1_pcm_mic(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1338 struct snd_pcm *pcm;
1344 if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
1347 pcm->private_data = emu;
1349 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1351 pcm->info_flags = 0;
1352 strcpy(pcm->name, "Mic Capture");
1355 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1362 static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1364 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1365 int nefx = emu->audigy ? 64 : 32;
1366 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1367 uinfo->count = nefx;
1368 uinfo->value.integer.min = 0;
1369 uinfo->value.integer.max = 1;
1373 static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1375 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1376 int nefx = emu->audigy ? 64 : 32;
1379 spin_lock_irq(&emu->reg_lock);
1380 for (idx = 0; idx < nefx; idx++)
1381 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1382 spin_unlock_irq(&emu->reg_lock);
1386 static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1388 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1389 unsigned int nval[2], bits;
1390 int nefx = emu->audigy ? 64 : 32;
1391 int nefxb = emu->audigy ? 7 : 6;
1394 nval[0] = nval[1] = 0;
1395 for (idx = 0, bits = 0; idx < nefx; idx++)
1396 if (ucontrol->value.integer.value[idx]) {
1397 nval[idx / 32] |= 1 << (idx % 32);
1401 for (idx = 0; idx < nefxb; idx++)
1402 if (1 << idx == bits)
1408 spin_lock_irq(&emu->reg_lock);
1409 change = (nval[0] != emu->efx_voices_mask[0]) ||
1410 (nval[1] != emu->efx_voices_mask[1]);
1411 emu->efx_voices_mask[0] = nval[0];
1412 emu->efx_voices_mask[1] = nval[1];
1413 spin_unlock_irq(&emu->reg_lock);
1417 static struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
1418 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1419 .name = "Captured FX8010 Outputs",
1420 .info = snd_emu10k1_pcm_efx_voices_mask_info,
1421 .get = snd_emu10k1_pcm_efx_voices_mask_get,
1422 .put = snd_emu10k1_pcm_efx_voices_mask_put
1425 static struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
1426 .open = snd_emu10k1_capture_efx_open,
1427 .close = snd_emu10k1_capture_efx_close,
1428 .ioctl = snd_pcm_lib_ioctl,
1429 .hw_params = snd_emu10k1_capture_hw_params,
1430 .hw_free = snd_emu10k1_capture_hw_free,
1431 .prepare = snd_emu10k1_capture_prepare,
1432 .trigger = snd_emu10k1_capture_trigger,
1433 .pointer = snd_emu10k1_capture_pointer,
1439 #define INITIAL_TRAM_SHIFT 14
1440 #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1442 static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
1444 struct snd_pcm_substream *substream = private_data;
1445 snd_pcm_period_elapsed(substream);
1448 static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1449 unsigned short *dst_right,
1450 unsigned short *src,
1452 unsigned int tram_shift)
1454 // printk("tram_poke1: dst_left = 0x%p, dst_right = 0x%p, src = 0x%p, count = 0x%x\n", dst_left, dst_right, src, count);
1455 if ((tram_shift & 1) == 0) {
1457 *dst_left-- = *src++;
1458 *dst_right-- = *src++;
1462 *dst_right-- = *src++;
1463 *dst_left-- = *src++;
1468 static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1469 struct snd_pcm_indirect *rec, size_t bytes)
1471 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1472 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1473 unsigned int tram_size = pcm->buffer_size;
1474 unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1475 unsigned int frames = bytes >> 2, count;
1476 unsigned int tram_pos = pcm->tram_pos;
1477 unsigned int tram_shift = pcm->tram_shift;
1479 while (frames > tram_pos) {
1480 count = tram_pos + 1;
1481 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1482 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1483 src, count, tram_shift);
1486 tram_pos = (tram_size / 2) - 1;
1489 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1490 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1491 src, frames, tram_shift);
1493 pcm->tram_pos = tram_pos;
1494 pcm->tram_shift = tram_shift;
1497 static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
1499 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1500 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1502 snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy);
1506 static int snd_emu10k1_fx8010_playback_hw_params(struct snd_pcm_substream *substream,
1507 struct snd_pcm_hw_params *hw_params)
1509 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1512 static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
1514 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1515 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1518 for (i = 0; i < pcm->channels; i++)
1519 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1520 snd_pcm_lib_free_pages(substream);
1524 static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
1526 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1527 struct snd_pcm_runtime *runtime = substream->runtime;
1528 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1531 // 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);
1532 memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1533 pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1534 pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1535 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1536 pcm->tram_shift = 0;
1537 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */
1538 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */
1539 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1540 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */
1541 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1542 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1543 for (i = 0; i < pcm->channels; i++)
1544 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1548 static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
1550 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1551 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1554 spin_lock(&emu->reg_lock);
1556 case SNDRV_PCM_TRIGGER_START:
1558 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1559 #ifdef EMU10K1_SET_AC3_IEC958
1562 for (i = 0; i < 3; i++) {
1564 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1565 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1566 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1567 snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1571 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1574 snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
1575 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1577 case SNDRV_PCM_TRIGGER_STOP:
1578 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1579 snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL;
1580 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1581 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1582 pcm->tram_shift = 0;
1589 spin_unlock(&emu->reg_lock);
1593 static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
1595 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1596 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1597 size_t ptr; /* byte pointer */
1599 if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1601 ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1602 return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1605 static struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
1607 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1608 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE),
1609 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1610 .rates = SNDRV_PCM_RATE_48000,
1615 .buffer_bytes_max = (128*1024),
1616 .period_bytes_min = 1024,
1617 .period_bytes_max = (128*1024),
1619 .periods_max = 1024,
1623 static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
1625 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1626 struct snd_pcm_runtime *runtime = substream->runtime;
1627 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1629 runtime->hw = snd_emu10k1_fx8010_playback;
1630 runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1631 runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1632 spin_lock_irq(&emu->reg_lock);
1633 if (pcm->valid == 0) {
1634 spin_unlock_irq(&emu->reg_lock);
1638 spin_unlock_irq(&emu->reg_lock);
1642 static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
1644 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1645 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1647 spin_lock_irq(&emu->reg_lock);
1649 spin_unlock_irq(&emu->reg_lock);
1653 static struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
1654 .open = snd_emu10k1_fx8010_playback_open,
1655 .close = snd_emu10k1_fx8010_playback_close,
1656 .ioctl = snd_pcm_lib_ioctl,
1657 .hw_params = snd_emu10k1_fx8010_playback_hw_params,
1658 .hw_free = snd_emu10k1_fx8010_playback_hw_free,
1659 .prepare = snd_emu10k1_fx8010_playback_prepare,
1660 .trigger = snd_emu10k1_fx8010_playback_trigger,
1661 .pointer = snd_emu10k1_fx8010_playback_pointer,
1662 .ack = snd_emu10k1_fx8010_playback_transfer,
1665 int __devinit snd_emu10k1_pcm_efx(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1667 struct snd_pcm *pcm;
1668 struct snd_kcontrol *kctl;
1674 if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1677 pcm->private_data = emu;
1679 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1680 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1682 pcm->info_flags = 0;
1683 strcpy(pcm->name, "Multichannel Capture/PT Playback");
1688 /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
1692 /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1694 emu->efx_voices_mask[0] = 0;
1695 emu->efx_voices_mask[1] = 0xffff;
1697 emu->efx_voices_mask[0] = 0xffff0000;
1698 emu->efx_voices_mask[1] = 0;
1700 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1703 kctl->id.device = device;
1704 snd_ctl_add(emu->card, kctl);
1706 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);