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