Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / sound / pci / emu10k1 / emupcm.c
blobf82481bd2542a912924088e8725dc56bf9bbdf23
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 dev_dbg(emu->card->dev,
48 "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));
52 #endif
53 snd_pcm_period_elapsed(epcm->substream);
56 static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu,
57 unsigned int status)
59 #if 0
60 if (status & IPR_ADCBUFHALFFULL) {
61 if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
62 return;
64 #endif
65 snd_pcm_period_elapsed(emu->pcm_capture_substream);
68 static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu,
69 unsigned int status)
71 #if 0
72 if (status & IPR_MICBUFHALFFULL) {
73 if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
74 return;
76 #endif
77 snd_pcm_period_elapsed(emu->pcm_capture_mic_substream);
80 static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu,
81 unsigned int status)
83 #if 0
84 if (status & IPR_EFXBUFHALFFULL) {
85 if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
86 return;
88 #endif
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;
97 unsigned int ptr;
99 if (!epcm->running)
100 return 0;
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;
106 return ptr;
109 static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm, int voices)
111 int err, i;
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)
119 break;
121 if (i == voices)
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,
132 voices,
133 &epcm->voices[0]);
135 if (err < 0)
136 return err;
137 epcm->voices[0]->epcm = epcm;
138 if (voices > 1) {
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,
148 &epcm->extra);
149 if (err < 0) {
151 dev_dbg(emu->card->dev, "pcm_channel_alloc: "
152 "failed extra: voices=%d, frame=%d\n",
153 voices, frame);
155 for (i = 0; i < voices; i++) {
156 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
157 epcm->voices[i] = NULL;
159 return err;
161 epcm->extra->epcm = epcm;
162 epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
164 return 0;
167 static unsigned int capture_period_sizes[31] = {
168 384, 448, 512, 640,
169 384*2, 448*2, 512*2, 640*2,
170 384*4, 448*4, 512*4, 640*4,
171 384*8, 448*8, 512*8, 640*8,
172 384*16, 448*16, 512*16, 640*16,
173 384*32, 448*32, 512*32, 640*32,
174 384*64, 448*64, 512*64, 640*64,
175 384*128,448*128,512*128
178 static struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
179 .count = 31,
180 .list = capture_period_sizes,
181 .mask = 0
184 static unsigned int capture_rates[8] = {
185 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
188 static struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
189 .count = 8,
190 .list = capture_rates,
191 .mask = 0
194 static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
196 switch (rate) {
197 case 8000: return ADCCR_SAMPLERATE_8;
198 case 11025: return ADCCR_SAMPLERATE_11;
199 case 16000: return ADCCR_SAMPLERATE_16;
200 case 22050: return ADCCR_SAMPLERATE_22;
201 case 24000: return ADCCR_SAMPLERATE_24;
202 case 32000: return ADCCR_SAMPLERATE_32;
203 case 44100: return ADCCR_SAMPLERATE_44;
204 case 48000: return ADCCR_SAMPLERATE_48;
205 default:
206 snd_BUG();
207 return ADCCR_SAMPLERATE_8;
211 static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
213 switch (rate) {
214 case 8000: return A_ADCCR_SAMPLERATE_8;
215 case 11025: return A_ADCCR_SAMPLERATE_11;
216 case 12000: return A_ADCCR_SAMPLERATE_12; /* really supported? */
217 case 16000: return ADCCR_SAMPLERATE_16;
218 case 22050: return ADCCR_SAMPLERATE_22;
219 case 24000: return ADCCR_SAMPLERATE_24;
220 case 32000: return ADCCR_SAMPLERATE_32;
221 case 44100: return ADCCR_SAMPLERATE_44;
222 case 48000: return ADCCR_SAMPLERATE_48;
223 default:
224 snd_BUG();
225 return A_ADCCR_SAMPLERATE_8;
229 static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
231 unsigned int pitch_target;
233 pitch_target = (rate << 8) / 375;
234 pitch_target = (pitch_target >> 1) + (pitch_target & 1);
235 return pitch_target;
238 #define PITCH_48000 0x00004000
239 #define PITCH_96000 0x00008000
240 #define PITCH_85000 0x00007155
241 #define PITCH_80726 0x00006ba2
242 #define PITCH_67882 0x00005a82
243 #define PITCH_57081 0x00004c1c
245 static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
247 if (pitch_target == PITCH_48000)
248 return CCCA_INTERPROM_0;
249 else if (pitch_target < PITCH_48000)
250 return CCCA_INTERPROM_1;
251 else if (pitch_target >= PITCH_96000)
252 return CCCA_INTERPROM_0;
253 else if (pitch_target >= PITCH_85000)
254 return CCCA_INTERPROM_6;
255 else if (pitch_target >= PITCH_80726)
256 return CCCA_INTERPROM_5;
257 else if (pitch_target >= PITCH_67882)
258 return CCCA_INTERPROM_4;
259 else if (pitch_target >= PITCH_57081)
260 return CCCA_INTERPROM_3;
261 else
262 return CCCA_INTERPROM_2;
266 * calculate cache invalidate size
268 * stereo: channel is stereo
269 * w_16: using 16bit samples
271 * returns: cache invalidate size in samples
273 static inline int emu10k1_ccis(int stereo, int w_16)
275 if (w_16) {
276 return stereo ? 24 : 26;
277 } else {
278 return stereo ? 24*2 : 26*2;
282 static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
283 int master, int extra,
284 struct snd_emu10k1_voice *evoice,
285 unsigned int start_addr,
286 unsigned int end_addr,
287 struct snd_emu10k1_pcm_mixer *mix)
289 struct snd_pcm_substream *substream = evoice->epcm->substream;
290 struct snd_pcm_runtime *runtime = substream->runtime;
291 unsigned int silent_page, tmp;
292 int voice, stereo, w_16;
293 unsigned char attn, send_amount[8];
294 unsigned char send_routing[8];
295 unsigned long flags;
296 unsigned int pitch_target;
297 unsigned int ccis;
299 voice = evoice->number;
300 stereo = runtime->channels == 2;
301 w_16 = snd_pcm_format_width(runtime->format) == 16;
303 if (!extra && stereo) {
304 start_addr >>= 1;
305 end_addr >>= 1;
307 if (w_16) {
308 start_addr >>= 1;
309 end_addr >>= 1;
312 spin_lock_irqsave(&emu->reg_lock, flags);
314 /* volume parameters */
315 if (extra) {
316 attn = 0;
317 memset(send_routing, 0, sizeof(send_routing));
318 send_routing[0] = 0;
319 send_routing[1] = 1;
320 send_routing[2] = 2;
321 send_routing[3] = 3;
322 memset(send_amount, 0, sizeof(send_amount));
323 } else {
324 /* mono, left, right (master voice = left) */
325 tmp = stereo ? (master ? 1 : 2) : 0;
326 memcpy(send_routing, &mix->send_routing[tmp][0], 8);
327 memcpy(send_amount, &mix->send_volume[tmp][0], 8);
330 ccis = emu10k1_ccis(stereo, w_16);
332 if (master) {
333 evoice->epcm->ccca_start_addr = start_addr + ccis;
334 if (extra) {
335 start_addr += ccis;
336 end_addr += ccis + emu->delay_pcm_irq;
338 if (stereo && !extra) {
339 snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK);
340 snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK);
341 } else {
342 snd_emu10k1_ptr_write(emu, CPF, voice, 0);
346 /* setup routing */
347 if (emu->audigy) {
348 snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
349 snd_emu10k1_compose_audigy_fxrt1(send_routing));
350 snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
351 snd_emu10k1_compose_audigy_fxrt2(send_routing));
352 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
353 ((unsigned int)send_amount[4] << 24) |
354 ((unsigned int)send_amount[5] << 16) |
355 ((unsigned int)send_amount[6] << 8) |
356 (unsigned int)send_amount[7]);
357 } else
358 snd_emu10k1_ptr_write(emu, FXRT, voice,
359 snd_emu10k1_compose_send_routing(send_routing));
360 /* Stop CA */
361 /* Assumption that PT is already 0 so no harm overwriting */
362 snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]);
363 snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24));
364 snd_emu10k1_ptr_write(emu, PSST, voice,
365 (start_addr + (extra ? emu->delay_pcm_irq : 0)) |
366 (send_amount[2] << 24));
367 if (emu->card_capabilities->emu_model)
368 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
369 else
370 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
371 if (extra)
372 snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
373 emu10k1_select_interprom(pitch_target) |
374 (w_16 ? 0 : CCCA_8BITSELECT));
375 else
376 snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
377 emu10k1_select_interprom(pitch_target) |
378 (w_16 ? 0 : CCCA_8BITSELECT));
379 /* Clear filter delay memory */
380 snd_emu10k1_ptr_write(emu, Z1, voice, 0);
381 snd_emu10k1_ptr_write(emu, Z2, voice, 0);
382 /* invalidate maps */
383 silent_page = ((unsigned int)emu->silent_page.addr << 1) | MAP_PTI_MASK;
384 snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
385 snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
386 /* modulation envelope */
387 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
388 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
389 snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
390 snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
391 snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
392 snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
393 snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
394 snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
395 snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
396 snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
397 /* volume envelope */
398 snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
399 snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
400 /* filter envelope */
401 snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
402 /* pitch envelope */
403 snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
405 spin_unlock_irqrestore(&emu->reg_lock, flags);
408 static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
409 struct snd_pcm_hw_params *hw_params)
411 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
412 struct snd_pcm_runtime *runtime = substream->runtime;
413 struct snd_emu10k1_pcm *epcm = runtime->private_data;
414 int err;
416 if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0)
417 return err;
418 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
419 return err;
420 if (err > 0) { /* change */
421 int mapped;
422 if (epcm->memblk != NULL)
423 snd_emu10k1_free_pages(emu, epcm->memblk);
424 epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
425 epcm->start_addr = 0;
426 if (! epcm->memblk)
427 return -ENOMEM;
428 mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
429 if (mapped < 0)
430 return -ENOMEM;
431 epcm->start_addr = mapped << PAGE_SHIFT;
433 return 0;
436 static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
438 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
439 struct snd_pcm_runtime *runtime = substream->runtime;
440 struct snd_emu10k1_pcm *epcm;
442 if (runtime->private_data == NULL)
443 return 0;
444 epcm = runtime->private_data;
445 if (epcm->extra) {
446 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
447 epcm->extra = NULL;
449 if (epcm->voices[1]) {
450 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
451 epcm->voices[1] = NULL;
453 if (epcm->voices[0]) {
454 snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
455 epcm->voices[0] = NULL;
457 if (epcm->memblk) {
458 snd_emu10k1_free_pages(emu, epcm->memblk);
459 epcm->memblk = NULL;
460 epcm->start_addr = 0;
462 snd_pcm_lib_free_pages(substream);
463 return 0;
466 static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
468 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
469 struct snd_pcm_runtime *runtime = substream->runtime;
470 struct snd_emu10k1_pcm *epcm;
471 int i;
473 if (runtime->private_data == NULL)
474 return 0;
475 epcm = runtime->private_data;
476 if (epcm->extra) {
477 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
478 epcm->extra = NULL;
480 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
481 if (epcm->voices[i]) {
482 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
483 epcm->voices[i] = NULL;
486 if (epcm->memblk) {
487 snd_emu10k1_free_pages(emu, epcm->memblk);
488 epcm->memblk = NULL;
489 epcm->start_addr = 0;
491 snd_pcm_lib_free_pages(substream);
492 return 0;
495 static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
497 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
498 struct snd_pcm_runtime *runtime = substream->runtime;
499 struct snd_emu10k1_pcm *epcm = runtime->private_data;
500 unsigned int start_addr, end_addr;
502 start_addr = epcm->start_addr;
503 end_addr = snd_pcm_lib_period_bytes(substream);
504 if (runtime->channels == 2) {
505 start_addr >>= 1;
506 end_addr >>= 1;
508 end_addr += start_addr;
509 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
510 start_addr, end_addr, NULL);
511 start_addr = epcm->start_addr;
512 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
513 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
514 start_addr, end_addr,
515 &emu->pcm_mixer[substream->number]);
516 if (epcm->voices[1])
517 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
518 start_addr, end_addr,
519 &emu->pcm_mixer[substream->number]);
520 return 0;
523 static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
525 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
526 struct snd_pcm_runtime *runtime = substream->runtime;
527 struct snd_emu10k1_pcm *epcm = runtime->private_data;
528 unsigned int start_addr, end_addr;
529 unsigned int channel_size;
530 int i;
532 start_addr = epcm->start_addr;
533 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
536 * the kX driver leaves some space between voices
538 channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
540 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
541 start_addr, start_addr + (channel_size / 2), NULL);
543 /* only difference with the master voice is we use it for the pointer */
544 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
545 start_addr, start_addr + channel_size,
546 &emu->efx_pcm_mixer[0]);
548 start_addr += channel_size;
549 for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
550 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
551 start_addr, start_addr + channel_size,
552 &emu->efx_pcm_mixer[i]);
553 start_addr += channel_size;
556 return 0;
559 static struct snd_pcm_hardware snd_emu10k1_efx_playback =
561 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
562 SNDRV_PCM_INFO_BLOCK_TRANSFER |
563 SNDRV_PCM_INFO_RESUME |
564 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
565 .formats = SNDRV_PCM_FMTBIT_S16_LE,
566 .rates = SNDRV_PCM_RATE_48000,
567 .rate_min = 48000,
568 .rate_max = 48000,
569 .channels_min = NUM_EFX_PLAYBACK,
570 .channels_max = NUM_EFX_PLAYBACK,
571 .buffer_bytes_max = (64*1024),
572 .period_bytes_min = 64,
573 .period_bytes_max = (64*1024),
574 .periods_min = 2,
575 .periods_max = 2,
576 .fifo_size = 0,
579 static int snd_emu10k1_capture_hw_params(struct snd_pcm_substream *substream,
580 struct snd_pcm_hw_params *hw_params)
582 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
585 static int snd_emu10k1_capture_hw_free(struct snd_pcm_substream *substream)
587 return snd_pcm_lib_free_pages(substream);
590 static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
592 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
593 struct snd_pcm_runtime *runtime = substream->runtime;
594 struct snd_emu10k1_pcm *epcm = runtime->private_data;
595 int idx;
597 /* zeroing the buffer size will stop capture */
598 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
599 switch (epcm->type) {
600 case CAPTURE_AC97ADC:
601 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
602 break;
603 case CAPTURE_EFX:
604 if (emu->audigy) {
605 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
606 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
607 } else
608 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
609 break;
610 default:
611 break;
613 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
614 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
615 epcm->capture_bs_val = 0;
616 for (idx = 0; idx < 31; idx++) {
617 if (capture_period_sizes[idx] == epcm->capture_bufsize) {
618 epcm->capture_bs_val = idx + 1;
619 break;
622 if (epcm->capture_bs_val == 0) {
623 snd_BUG();
624 epcm->capture_bs_val++;
626 if (epcm->type == CAPTURE_AC97ADC) {
627 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
628 if (runtime->channels > 1)
629 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
630 epcm->capture_cr_val |= emu->audigy ?
631 snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
632 snd_emu10k1_capture_rate_reg(runtime->rate);
634 return 0;
637 static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
639 struct snd_pcm_runtime *runtime;
640 unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
642 if (evoice == NULL)
643 return;
644 runtime = evoice->epcm->substream->runtime;
645 voice = evoice->number;
646 stereo = (!extra && runtime->channels == 2);
647 sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
648 ccis = emu10k1_ccis(stereo, sample == 0);
649 /* set cs to 2 * number of cache registers beside the invalidated */
650 cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
651 if (cs > 16) cs = 16;
652 for (i = 0; i < cs; i++) {
653 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
654 if (stereo) {
655 snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
658 /* reset cache */
659 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
660 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
661 if (stereo) {
662 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
663 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
665 /* fill cache */
666 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
667 if (stereo) {
668 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
672 static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
673 int master, int extra,
674 struct snd_emu10k1_pcm_mixer *mix)
676 struct snd_pcm_substream *substream;
677 struct snd_pcm_runtime *runtime;
678 unsigned int attn, vattn;
679 unsigned int voice, tmp;
681 if (evoice == NULL) /* skip second voice for mono */
682 return;
683 substream = evoice->epcm->substream;
684 runtime = substream->runtime;
685 voice = evoice->number;
687 attn = extra ? 0 : 0x00ff;
688 tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
689 vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
690 snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
691 snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
692 snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
693 snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
694 snd_emu10k1_voice_clear_loop_stop(emu, voice);
697 static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra)
699 struct snd_pcm_substream *substream;
700 struct snd_pcm_runtime *runtime;
701 unsigned int voice, pitch, pitch_target;
703 if (evoice == NULL) /* skip second voice for mono */
704 return;
705 substream = evoice->epcm->substream;
706 runtime = substream->runtime;
707 voice = evoice->number;
709 pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
710 if (emu->card_capabilities->emu_model)
711 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
712 else
713 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
714 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
715 if (master || evoice->epcm->type == PLAYBACK_EFX)
716 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
717 snd_emu10k1_ptr_write(emu, IP, voice, pitch);
718 if (extra)
719 snd_emu10k1_voice_intr_enable(emu, voice);
722 static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
724 unsigned int voice;
726 if (evoice == NULL)
727 return;
728 voice = evoice->number;
729 snd_emu10k1_voice_intr_disable(emu, voice);
730 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
731 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
732 snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
733 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
734 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
735 snd_emu10k1_ptr_write(emu, IP, voice, 0);
738 static inline void snd_emu10k1_playback_mangle_extra(struct snd_emu10k1 *emu,
739 struct snd_emu10k1_pcm *epcm,
740 struct snd_pcm_substream *substream,
741 struct snd_pcm_runtime *runtime)
743 unsigned int ptr, period_pos;
745 /* try to sychronize the current position for the interrupt
746 source voice */
747 period_pos = runtime->status->hw_ptr - runtime->hw_ptr_interrupt;
748 period_pos %= runtime->period_size;
749 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->extra->number);
750 ptr &= ~0x00ffffff;
751 ptr |= epcm->ccca_start_addr + period_pos;
752 snd_emu10k1_ptr_write(emu, CCCA, epcm->extra->number, ptr);
755 static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
756 int cmd)
758 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
759 struct snd_pcm_runtime *runtime = substream->runtime;
760 struct snd_emu10k1_pcm *epcm = runtime->private_data;
761 struct snd_emu10k1_pcm_mixer *mix;
762 int result = 0;
765 dev_dbg(emu->card->dev,
766 "trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n",
767 (int)emu, cmd, substream->ops->pointer(substream))
769 spin_lock(&emu->reg_lock);
770 switch (cmd) {
771 case SNDRV_PCM_TRIGGER_START:
772 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */
773 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
774 /* follow thru */
775 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
776 case SNDRV_PCM_TRIGGER_RESUME:
777 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)
778 snd_emu10k1_playback_mangle_extra(emu, epcm, substream, runtime);
779 mix = &emu->pcm_mixer[substream->number];
780 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
781 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
782 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
783 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
784 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
785 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
786 epcm->running = 1;
787 break;
788 case SNDRV_PCM_TRIGGER_STOP:
789 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
790 case SNDRV_PCM_TRIGGER_SUSPEND:
791 epcm->running = 0;
792 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
793 snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
794 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
795 break;
796 default:
797 result = -EINVAL;
798 break;
800 spin_unlock(&emu->reg_lock);
801 return result;
804 static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
805 int cmd)
807 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
808 struct snd_pcm_runtime *runtime = substream->runtime;
809 struct snd_emu10k1_pcm *epcm = runtime->private_data;
810 int result = 0;
812 spin_lock(&emu->reg_lock);
813 switch (cmd) {
814 case SNDRV_PCM_TRIGGER_START:
815 case SNDRV_PCM_TRIGGER_RESUME:
816 /* hmm this should cause full and half full interrupt to be raised? */
817 outl(epcm->capture_ipr, emu->port + IPR);
818 snd_emu10k1_intr_enable(emu, epcm->capture_inte);
820 dev_dbg(emu->card->dev, "adccr = 0x%x, adcbs = 0x%x\n",
821 epcm->adccr, epcm->adcbs);
823 switch (epcm->type) {
824 case CAPTURE_AC97ADC:
825 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
826 break;
827 case CAPTURE_EFX:
828 if (emu->audigy) {
829 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
830 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
831 dev_dbg(emu->card->dev,
832 "cr_val=0x%x, cr_val2=0x%x\n",
833 epcm->capture_cr_val,
834 epcm->capture_cr_val2);
835 } else
836 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
837 break;
838 default:
839 break;
841 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
842 epcm->running = 1;
843 epcm->first_ptr = 1;
844 break;
845 case SNDRV_PCM_TRIGGER_STOP:
846 case SNDRV_PCM_TRIGGER_SUSPEND:
847 epcm->running = 0;
848 snd_emu10k1_intr_disable(emu, epcm->capture_inte);
849 outl(epcm->capture_ipr, emu->port + IPR);
850 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
851 switch (epcm->type) {
852 case CAPTURE_AC97ADC:
853 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
854 break;
855 case CAPTURE_EFX:
856 if (emu->audigy) {
857 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
858 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
859 } else
860 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
861 break;
862 default:
863 break;
865 break;
866 default:
867 result = -EINVAL;
869 spin_unlock(&emu->reg_lock);
870 return result;
873 static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
875 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
876 struct snd_pcm_runtime *runtime = substream->runtime;
877 struct snd_emu10k1_pcm *epcm = runtime->private_data;
878 unsigned int ptr;
880 if (!epcm->running)
881 return 0;
882 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
883 #if 0 /* Perex's code */
884 ptr += runtime->buffer_size;
885 ptr -= epcm->ccca_start_addr;
886 ptr %= runtime->buffer_size;
887 #else /* EMU10K1 Open Source code from Creative */
888 if (ptr < epcm->ccca_start_addr)
889 ptr += runtime->buffer_size - epcm->ccca_start_addr;
890 else {
891 ptr -= epcm->ccca_start_addr;
892 if (ptr >= runtime->buffer_size)
893 ptr -= runtime->buffer_size;
895 #endif
897 dev_dbg(emu->card->dev,
898 "ptr = 0x%lx, buffer_size = 0x%lx, period_size = 0x%lx\n",
899 (long)ptr, (long)runtime->buffer_size,
900 (long)runtime->period_size);
902 return ptr;
906 static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
907 int cmd)
909 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
910 struct snd_pcm_runtime *runtime = substream->runtime;
911 struct snd_emu10k1_pcm *epcm = runtime->private_data;
912 int i;
913 int result = 0;
915 spin_lock(&emu->reg_lock);
916 switch (cmd) {
917 case SNDRV_PCM_TRIGGER_START:
918 /* prepare voices */
919 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
920 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
922 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
924 /* follow thru */
925 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
926 case SNDRV_PCM_TRIGGER_RESUME:
927 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
928 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
929 &emu->efx_pcm_mixer[0]);
930 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
931 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
932 &emu->efx_pcm_mixer[i]);
933 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
934 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
935 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
936 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
937 epcm->running = 1;
938 break;
939 case SNDRV_PCM_TRIGGER_SUSPEND:
940 case SNDRV_PCM_TRIGGER_STOP:
941 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
942 epcm->running = 0;
943 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
944 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
946 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
947 break;
948 default:
949 result = -EINVAL;
950 break;
952 spin_unlock(&emu->reg_lock);
953 return result;
957 static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
959 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
960 struct snd_pcm_runtime *runtime = substream->runtime;
961 struct snd_emu10k1_pcm *epcm = runtime->private_data;
962 unsigned int ptr;
964 if (!epcm->running)
965 return 0;
966 if (epcm->first_ptr) {
967 udelay(50); /* hack, it takes awhile until capture is started */
968 epcm->first_ptr = 0;
970 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
971 return bytes_to_frames(runtime, ptr);
975 * Playback support device description
978 static struct snd_pcm_hardware snd_emu10k1_playback =
980 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
981 SNDRV_PCM_INFO_BLOCK_TRANSFER |
982 SNDRV_PCM_INFO_RESUME |
983 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
984 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
985 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
986 .rate_min = 4000,
987 .rate_max = 96000,
988 .channels_min = 1,
989 .channels_max = 2,
990 .buffer_bytes_max = (128*1024),
991 .period_bytes_min = 64,
992 .period_bytes_max = (128*1024),
993 .periods_min = 1,
994 .periods_max = 1024,
995 .fifo_size = 0,
999 * Capture support device description
1002 static struct snd_pcm_hardware snd_emu10k1_capture =
1004 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1005 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1006 SNDRV_PCM_INFO_RESUME |
1007 SNDRV_PCM_INFO_MMAP_VALID),
1008 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1009 .rates = SNDRV_PCM_RATE_8000_48000,
1010 .rate_min = 8000,
1011 .rate_max = 48000,
1012 .channels_min = 1,
1013 .channels_max = 2,
1014 .buffer_bytes_max = (64*1024),
1015 .period_bytes_min = 384,
1016 .period_bytes_max = (64*1024),
1017 .periods_min = 2,
1018 .periods_max = 2,
1019 .fifo_size = 0,
1022 static struct snd_pcm_hardware snd_emu10k1_capture_efx =
1024 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1025 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1026 SNDRV_PCM_INFO_RESUME |
1027 SNDRV_PCM_INFO_MMAP_VALID),
1028 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1029 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1030 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1031 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
1032 .rate_min = 44100,
1033 .rate_max = 192000,
1034 .channels_min = 8,
1035 .channels_max = 8,
1036 .buffer_bytes_max = (64*1024),
1037 .period_bytes_min = 384,
1038 .period_bytes_max = (64*1024),
1039 .periods_min = 2,
1040 .periods_max = 2,
1041 .fifo_size = 0,
1048 static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
1050 struct snd_ctl_elem_id id;
1052 if (! kctl)
1053 return;
1054 if (activate)
1055 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1056 else
1057 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1058 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
1059 SNDRV_CTL_EVENT_MASK_INFO,
1060 snd_ctl_build_ioff(&id, kctl, idx));
1063 static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1065 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
1066 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
1067 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
1070 static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1072 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
1073 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
1074 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
1077 static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
1079 kfree(runtime->private_data);
1082 static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
1084 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1085 struct snd_emu10k1_pcm_mixer *mix;
1086 int i;
1088 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1089 mix = &emu->efx_pcm_mixer[i];
1090 mix->epcm = NULL;
1091 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1093 return 0;
1096 static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
1098 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1099 struct snd_emu10k1_pcm *epcm;
1100 struct snd_emu10k1_pcm_mixer *mix;
1101 struct snd_pcm_runtime *runtime = substream->runtime;
1102 int i;
1104 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1105 if (epcm == NULL)
1106 return -ENOMEM;
1107 epcm->emu = emu;
1108 epcm->type = PLAYBACK_EFX;
1109 epcm->substream = substream;
1111 emu->pcm_playback_efx_substream = substream;
1113 runtime->private_data = epcm;
1114 runtime->private_free = snd_emu10k1_pcm_free_substream;
1115 runtime->hw = snd_emu10k1_efx_playback;
1117 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1118 mix = &emu->efx_pcm_mixer[i];
1119 mix->send_routing[0][0] = i;
1120 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1121 mix->send_volume[0][0] = 255;
1122 mix->attn[0] = 0xffff;
1123 mix->epcm = epcm;
1124 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1126 return 0;
1129 static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
1131 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1132 struct snd_emu10k1_pcm *epcm;
1133 struct snd_emu10k1_pcm_mixer *mix;
1134 struct snd_pcm_runtime *runtime = substream->runtime;
1135 int i, err, sample_rate;
1137 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1138 if (epcm == NULL)
1139 return -ENOMEM;
1140 epcm->emu = emu;
1141 epcm->type = PLAYBACK_EMUVOICE;
1142 epcm->substream = substream;
1143 runtime->private_data = epcm;
1144 runtime->private_free = snd_emu10k1_pcm_free_substream;
1145 runtime->hw = snd_emu10k1_playback;
1146 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
1147 kfree(epcm);
1148 return err;
1150 if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
1151 kfree(epcm);
1152 return err;
1154 if (emu->card_capabilities->emu_model && emu->emu1010.internal_clock == 0)
1155 sample_rate = 44100;
1156 else
1157 sample_rate = 48000;
1158 err = snd_pcm_hw_rule_noresample(runtime, sample_rate);
1159 if (err < 0) {
1160 kfree(epcm);
1161 return err;
1163 mix = &emu->pcm_mixer[substream->number];
1164 for (i = 0; i < 4; i++)
1165 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1166 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1167 mix->send_volume[0][0] = mix->send_volume[0][1] =
1168 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1169 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1170 mix->epcm = epcm;
1171 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1172 return 0;
1175 static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
1177 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1178 struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
1180 mix->epcm = NULL;
1181 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1182 return 0;
1185 static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
1187 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1188 struct snd_pcm_runtime *runtime = substream->runtime;
1189 struct snd_emu10k1_pcm *epcm;
1191 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1192 if (epcm == NULL)
1193 return -ENOMEM;
1194 epcm->emu = emu;
1195 epcm->type = CAPTURE_AC97ADC;
1196 epcm->substream = substream;
1197 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1198 epcm->capture_inte = INTE_ADCBUFENABLE;
1199 epcm->capture_ba_reg = ADCBA;
1200 epcm->capture_bs_reg = ADCBS;
1201 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1202 runtime->private_data = epcm;
1203 runtime->private_free = snd_emu10k1_pcm_free_substream;
1204 runtime->hw = snd_emu10k1_capture;
1205 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1206 emu->pcm_capture_substream = substream;
1207 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1208 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1209 return 0;
1212 static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
1214 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1216 emu->capture_interrupt = NULL;
1217 emu->pcm_capture_substream = NULL;
1218 return 0;
1221 static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
1223 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1224 struct snd_emu10k1_pcm *epcm;
1225 struct snd_pcm_runtime *runtime = substream->runtime;
1227 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1228 if (epcm == NULL)
1229 return -ENOMEM;
1230 epcm->emu = emu;
1231 epcm->type = CAPTURE_AC97MIC;
1232 epcm->substream = substream;
1233 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1234 epcm->capture_inte = INTE_MICBUFENABLE;
1235 epcm->capture_ba_reg = MICBA;
1236 epcm->capture_bs_reg = MICBS;
1237 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1238 substream->runtime->private_data = epcm;
1239 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1240 runtime->hw = snd_emu10k1_capture;
1241 runtime->hw.rates = SNDRV_PCM_RATE_8000;
1242 runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1243 runtime->hw.channels_min = 1;
1244 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1245 emu->pcm_capture_mic_substream = substream;
1246 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1247 return 0;
1250 static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
1252 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1254 emu->capture_interrupt = NULL;
1255 emu->pcm_capture_mic_substream = NULL;
1256 return 0;
1259 static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
1261 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1262 struct snd_emu10k1_pcm *epcm;
1263 struct snd_pcm_runtime *runtime = substream->runtime;
1264 int nefx = emu->audigy ? 64 : 32;
1265 int idx;
1267 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1268 if (epcm == NULL)
1269 return -ENOMEM;
1270 epcm->emu = emu;
1271 epcm->type = CAPTURE_EFX;
1272 epcm->substream = substream;
1273 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1274 epcm->capture_inte = INTE_EFXBUFENABLE;
1275 epcm->capture_ba_reg = FXBA;
1276 epcm->capture_bs_reg = FXBS;
1277 epcm->capture_idx_reg = FXIDX;
1278 substream->runtime->private_data = epcm;
1279 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1280 runtime->hw = snd_emu10k1_capture_efx;
1281 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1282 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1283 spin_lock_irq(&emu->reg_lock);
1284 if (emu->card_capabilities->emu_model) {
1285 /* Nb. of channels has been increased to 16 */
1286 /* TODO
1287 * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
1288 * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1289 * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1290 * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
1291 * rate_min = 44100,
1292 * rate_max = 192000,
1293 * channels_min = 16,
1294 * channels_max = 16,
1295 * Need to add mixer control to fix sample rate
1297 * There are 32 mono channels of 16bits each.
1298 * 24bit Audio uses 2x channels over 16bit
1299 * 96kHz uses 2x channels over 48kHz
1300 * 192kHz uses 4x channels over 48kHz
1301 * So, for 48kHz 24bit, one has 16 channels
1302 * for 96kHz 24bit, one has 8 channels
1303 * for 192kHz 24bit, one has 4 channels
1306 #if 1
1307 switch (emu->emu1010.internal_clock) {
1308 case 0:
1309 /* For 44.1kHz */
1310 runtime->hw.rates = SNDRV_PCM_RATE_44100;
1311 runtime->hw.rate_min = runtime->hw.rate_max = 44100;
1312 runtime->hw.channels_min =
1313 runtime->hw.channels_max = 16;
1314 break;
1315 case 1:
1316 /* For 48kHz */
1317 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1318 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1319 runtime->hw.channels_min =
1320 runtime->hw.channels_max = 16;
1321 break;
1323 #endif
1324 #if 0
1325 /* For 96kHz */
1326 runtime->hw.rates = SNDRV_PCM_RATE_96000;
1327 runtime->hw.rate_min = runtime->hw.rate_max = 96000;
1328 runtime->hw.channels_min = runtime->hw.channels_max = 4;
1329 #endif
1330 #if 0
1331 /* For 192kHz */
1332 runtime->hw.rates = SNDRV_PCM_RATE_192000;
1333 runtime->hw.rate_min = runtime->hw.rate_max = 192000;
1334 runtime->hw.channels_min = runtime->hw.channels_max = 2;
1335 #endif
1336 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1337 /* efx_voices_mask[0] is expected to be zero
1338 * efx_voices_mask[1] is expected to have 32bits set
1340 } else {
1341 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1342 for (idx = 0; idx < nefx; idx++) {
1343 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1344 runtime->hw.channels_min++;
1345 runtime->hw.channels_max++;
1349 epcm->capture_cr_val = emu->efx_voices_mask[0];
1350 epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1351 spin_unlock_irq(&emu->reg_lock);
1352 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1353 emu->pcm_capture_efx_substream = substream;
1354 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1355 return 0;
1358 static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
1360 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1362 emu->capture_interrupt = NULL;
1363 emu->pcm_capture_efx_substream = NULL;
1364 return 0;
1367 static struct snd_pcm_ops snd_emu10k1_playback_ops = {
1368 .open = snd_emu10k1_playback_open,
1369 .close = snd_emu10k1_playback_close,
1370 .ioctl = snd_pcm_lib_ioctl,
1371 .hw_params = snd_emu10k1_playback_hw_params,
1372 .hw_free = snd_emu10k1_playback_hw_free,
1373 .prepare = snd_emu10k1_playback_prepare,
1374 .trigger = snd_emu10k1_playback_trigger,
1375 .pointer = snd_emu10k1_playback_pointer,
1376 .page = snd_pcm_sgbuf_ops_page,
1379 static struct snd_pcm_ops snd_emu10k1_capture_ops = {
1380 .open = snd_emu10k1_capture_open,
1381 .close = snd_emu10k1_capture_close,
1382 .ioctl = snd_pcm_lib_ioctl,
1383 .hw_params = snd_emu10k1_capture_hw_params,
1384 .hw_free = snd_emu10k1_capture_hw_free,
1385 .prepare = snd_emu10k1_capture_prepare,
1386 .trigger = snd_emu10k1_capture_trigger,
1387 .pointer = snd_emu10k1_capture_pointer,
1390 /* EFX playback */
1391 static struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
1392 .open = snd_emu10k1_efx_playback_open,
1393 .close = snd_emu10k1_efx_playback_close,
1394 .ioctl = snd_pcm_lib_ioctl,
1395 .hw_params = snd_emu10k1_playback_hw_params,
1396 .hw_free = snd_emu10k1_efx_playback_hw_free,
1397 .prepare = snd_emu10k1_efx_playback_prepare,
1398 .trigger = snd_emu10k1_efx_playback_trigger,
1399 .pointer = snd_emu10k1_efx_playback_pointer,
1400 .page = snd_pcm_sgbuf_ops_page,
1403 int snd_emu10k1_pcm(struct snd_emu10k1 *emu, int device, struct snd_pcm **rpcm)
1405 struct snd_pcm *pcm;
1406 struct snd_pcm_substream *substream;
1407 int err;
1409 if (rpcm)
1410 *rpcm = NULL;
1412 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1413 return err;
1415 pcm->private_data = emu;
1417 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1418 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1420 pcm->info_flags = 0;
1421 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1422 strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1423 emu->pcm = pcm;
1425 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1426 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1427 return err;
1429 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1430 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1432 if (rpcm)
1433 *rpcm = pcm;
1435 return 0;
1438 int snd_emu10k1_pcm_multi(struct snd_emu10k1 *emu, int device,
1439 struct snd_pcm **rpcm)
1441 struct snd_pcm *pcm;
1442 struct snd_pcm_substream *substream;
1443 int err;
1445 if (rpcm)
1446 *rpcm = NULL;
1448 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
1449 return err;
1451 pcm->private_data = emu;
1453 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1455 pcm->info_flags = 0;
1456 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1457 strcpy(pcm->name, "Multichannel Playback");
1458 emu->pcm_multi = pcm;
1460 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1461 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1462 return err;
1464 if (rpcm)
1465 *rpcm = pcm;
1467 return 0;
1471 static struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
1472 .open = snd_emu10k1_capture_mic_open,
1473 .close = snd_emu10k1_capture_mic_close,
1474 .ioctl = snd_pcm_lib_ioctl,
1475 .hw_params = snd_emu10k1_capture_hw_params,
1476 .hw_free = snd_emu10k1_capture_hw_free,
1477 .prepare = snd_emu10k1_capture_prepare,
1478 .trigger = snd_emu10k1_capture_trigger,
1479 .pointer = snd_emu10k1_capture_pointer,
1482 int snd_emu10k1_pcm_mic(struct snd_emu10k1 *emu, int device,
1483 struct snd_pcm **rpcm)
1485 struct snd_pcm *pcm;
1486 int err;
1488 if (rpcm)
1489 *rpcm = NULL;
1491 if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
1492 return err;
1494 pcm->private_data = emu;
1496 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1498 pcm->info_flags = 0;
1499 strcpy(pcm->name, "Mic Capture");
1500 emu->pcm_mic = pcm;
1502 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1504 if (rpcm)
1505 *rpcm = pcm;
1506 return 0;
1509 static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1511 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1512 int nefx = emu->audigy ? 64 : 32;
1513 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1514 uinfo->count = nefx;
1515 uinfo->value.integer.min = 0;
1516 uinfo->value.integer.max = 1;
1517 return 0;
1520 static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1522 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1523 int nefx = emu->audigy ? 64 : 32;
1524 int idx;
1526 spin_lock_irq(&emu->reg_lock);
1527 for (idx = 0; idx < nefx; idx++)
1528 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1529 spin_unlock_irq(&emu->reg_lock);
1530 return 0;
1533 static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1535 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1536 unsigned int nval[2], bits;
1537 int nefx = emu->audigy ? 64 : 32;
1538 int nefxb = emu->audigy ? 7 : 6;
1539 int change, idx;
1541 nval[0] = nval[1] = 0;
1542 for (idx = 0, bits = 0; idx < nefx; idx++)
1543 if (ucontrol->value.integer.value[idx]) {
1544 nval[idx / 32] |= 1 << (idx % 32);
1545 bits++;
1548 for (idx = 0; idx < nefxb; idx++)
1549 if (1 << idx == bits)
1550 break;
1552 if (idx >= nefxb)
1553 return -EINVAL;
1555 spin_lock_irq(&emu->reg_lock);
1556 change = (nval[0] != emu->efx_voices_mask[0]) ||
1557 (nval[1] != emu->efx_voices_mask[1]);
1558 emu->efx_voices_mask[0] = nval[0];
1559 emu->efx_voices_mask[1] = nval[1];
1560 spin_unlock_irq(&emu->reg_lock);
1561 return change;
1564 static struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
1565 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1566 .name = "Captured FX8010 Outputs",
1567 .info = snd_emu10k1_pcm_efx_voices_mask_info,
1568 .get = snd_emu10k1_pcm_efx_voices_mask_get,
1569 .put = snd_emu10k1_pcm_efx_voices_mask_put
1572 static struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
1573 .open = snd_emu10k1_capture_efx_open,
1574 .close = snd_emu10k1_capture_efx_close,
1575 .ioctl = snd_pcm_lib_ioctl,
1576 .hw_params = snd_emu10k1_capture_hw_params,
1577 .hw_free = snd_emu10k1_capture_hw_free,
1578 .prepare = snd_emu10k1_capture_prepare,
1579 .trigger = snd_emu10k1_capture_trigger,
1580 .pointer = snd_emu10k1_capture_pointer,
1584 /* EFX playback */
1586 #define INITIAL_TRAM_SHIFT 14
1587 #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1589 static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
1591 struct snd_pcm_substream *substream = private_data;
1592 snd_pcm_period_elapsed(substream);
1595 static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1596 unsigned short *dst_right,
1597 unsigned short *src,
1598 unsigned int count,
1599 unsigned int tram_shift)
1602 dev_dbg(emu->card->dev,
1603 "tram_poke1: dst_left = 0x%p, dst_right = 0x%p, "
1604 "src = 0x%p, count = 0x%x\n",
1605 dst_left, dst_right, src, count);
1607 if ((tram_shift & 1) == 0) {
1608 while (count--) {
1609 *dst_left-- = *src++;
1610 *dst_right-- = *src++;
1612 } else {
1613 while (count--) {
1614 *dst_right-- = *src++;
1615 *dst_left-- = *src++;
1620 static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1621 struct snd_pcm_indirect *rec, size_t bytes)
1623 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1624 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1625 unsigned int tram_size = pcm->buffer_size;
1626 unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1627 unsigned int frames = bytes >> 2, count;
1628 unsigned int tram_pos = pcm->tram_pos;
1629 unsigned int tram_shift = pcm->tram_shift;
1631 while (frames > tram_pos) {
1632 count = tram_pos + 1;
1633 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1634 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1635 src, count, tram_shift);
1636 src += count * 2;
1637 frames -= count;
1638 tram_pos = (tram_size / 2) - 1;
1639 tram_shift++;
1641 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1642 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1643 src, frames, tram_shift);
1644 tram_pos -= frames;
1645 pcm->tram_pos = tram_pos;
1646 pcm->tram_shift = tram_shift;
1649 static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
1651 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1652 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1654 snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy);
1655 return 0;
1658 static int snd_emu10k1_fx8010_playback_hw_params(struct snd_pcm_substream *substream,
1659 struct snd_pcm_hw_params *hw_params)
1661 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1664 static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
1666 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1667 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1668 unsigned int i;
1670 for (i = 0; i < pcm->channels; i++)
1671 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1672 snd_pcm_lib_free_pages(substream);
1673 return 0;
1676 static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
1678 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1679 struct snd_pcm_runtime *runtime = substream->runtime;
1680 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1681 unsigned int i;
1684 dev_dbg(emu->card->dev, "prepare: etram_pages = 0x%p, dma_area = 0x%x, "
1685 "buffer_size = 0x%x (0x%x)\n",
1686 emu->fx8010.etram_pages, runtime->dma_area,
1687 runtime->buffer_size, runtime->buffer_size << 2);
1689 memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1690 pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1691 pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1692 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1693 pcm->tram_shift = 0;
1694 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */
1695 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */
1696 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1697 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */
1698 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1699 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1700 for (i = 0; i < pcm->channels; i++)
1701 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1702 return 0;
1705 static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
1707 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1708 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1709 int result = 0;
1711 spin_lock(&emu->reg_lock);
1712 switch (cmd) {
1713 case SNDRV_PCM_TRIGGER_START:
1714 /* follow thru */
1715 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1716 case SNDRV_PCM_TRIGGER_RESUME:
1717 #ifdef EMU10K1_SET_AC3_IEC958
1719 int i;
1720 for (i = 0; i < 3; i++) {
1721 unsigned int bits;
1722 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1723 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1724 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1725 snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1728 #endif
1729 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1730 if (result < 0)
1731 goto __err;
1732 snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
1733 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1734 break;
1735 case SNDRV_PCM_TRIGGER_STOP:
1736 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1737 case SNDRV_PCM_TRIGGER_SUSPEND:
1738 snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL;
1739 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1740 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1741 pcm->tram_shift = 0;
1742 break;
1743 default:
1744 result = -EINVAL;
1745 break;
1747 __err:
1748 spin_unlock(&emu->reg_lock);
1749 return result;
1752 static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
1754 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1755 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1756 size_t ptr; /* byte pointer */
1758 if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1759 return 0;
1760 ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1761 return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1764 static struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
1766 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1767 SNDRV_PCM_INFO_RESUME |
1768 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE),
1769 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1770 .rates = SNDRV_PCM_RATE_48000,
1771 .rate_min = 48000,
1772 .rate_max = 48000,
1773 .channels_min = 1,
1774 .channels_max = 1,
1775 .buffer_bytes_max = (128*1024),
1776 .period_bytes_min = 1024,
1777 .period_bytes_max = (128*1024),
1778 .periods_min = 2,
1779 .periods_max = 1024,
1780 .fifo_size = 0,
1783 static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
1785 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1786 struct snd_pcm_runtime *runtime = substream->runtime;
1787 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1789 runtime->hw = snd_emu10k1_fx8010_playback;
1790 runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1791 runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1792 spin_lock_irq(&emu->reg_lock);
1793 if (pcm->valid == 0) {
1794 spin_unlock_irq(&emu->reg_lock);
1795 return -ENODEV;
1797 pcm->opened = 1;
1798 spin_unlock_irq(&emu->reg_lock);
1799 return 0;
1802 static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
1804 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1805 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1807 spin_lock_irq(&emu->reg_lock);
1808 pcm->opened = 0;
1809 spin_unlock_irq(&emu->reg_lock);
1810 return 0;
1813 static struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
1814 .open = snd_emu10k1_fx8010_playback_open,
1815 .close = snd_emu10k1_fx8010_playback_close,
1816 .ioctl = snd_pcm_lib_ioctl,
1817 .hw_params = snd_emu10k1_fx8010_playback_hw_params,
1818 .hw_free = snd_emu10k1_fx8010_playback_hw_free,
1819 .prepare = snd_emu10k1_fx8010_playback_prepare,
1820 .trigger = snd_emu10k1_fx8010_playback_trigger,
1821 .pointer = snd_emu10k1_fx8010_playback_pointer,
1822 .ack = snd_emu10k1_fx8010_playback_transfer,
1825 int snd_emu10k1_pcm_efx(struct snd_emu10k1 *emu, int device,
1826 struct snd_pcm **rpcm)
1828 struct snd_pcm *pcm;
1829 struct snd_kcontrol *kctl;
1830 int err;
1832 if (rpcm)
1833 *rpcm = NULL;
1835 if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1836 return err;
1838 pcm->private_data = emu;
1840 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1841 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1843 pcm->info_flags = 0;
1844 strcpy(pcm->name, "Multichannel Capture/PT Playback");
1845 emu->pcm_efx = pcm;
1846 if (rpcm)
1847 *rpcm = pcm;
1849 /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
1850 * to these
1853 /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1854 if (emu->audigy) {
1855 emu->efx_voices_mask[0] = 0;
1856 if (emu->card_capabilities->emu_model)
1857 /* Pavel Hofman - 32 voices will be used for
1858 * capture (write mode) -
1859 * each bit = corresponding voice
1861 emu->efx_voices_mask[1] = 0xffffffff;
1862 else
1863 emu->efx_voices_mask[1] = 0xffff;
1864 } else {
1865 emu->efx_voices_mask[0] = 0xffff0000;
1866 emu->efx_voices_mask[1] = 0;
1868 /* For emu1010, the control has to set 32 upper bits (voices)
1869 * out of the 64 bits (voices) to true for the 16-channels capture
1870 * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
1871 * is already defined but the snd_emu10k1_pcm_efx_voices_mask
1872 * control can override this register's value.
1874 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1875 if (!kctl)
1876 return -ENOMEM;
1877 kctl->id.device = device;
1878 snd_ctl_add(emu->card, kctl);
1880 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1882 return 0;