net: dsa: mt7530: set CPU port to fallback mode
[linux/fpc-iii.git] / sound / pci / emu10k1 / emupcm.c
blob9f2b6097f486d1ae065e3b9680e1c81e8091740c
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 const 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 const struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
179 .count = 31,
180 .list = capture_period_sizes,
181 .mask = 0
184 static const unsigned int capture_rates[8] = {
185 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
188 static const 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 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 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 + emu->delay_pcm_irq;
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,
364 (start_addr + (extra ? emu->delay_pcm_irq : 0)) |
365 (send_amount[2] << 24));
366 if (emu->card_capabilities->emu_model)
367 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
368 else
369 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
370 if (extra)
371 snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
372 emu10k1_select_interprom(pitch_target) |
373 (w_16 ? 0 : CCCA_8BITSELECT));
374 else
375 snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
376 emu10k1_select_interprom(pitch_target) |
377 (w_16 ? 0 : CCCA_8BITSELECT));
378 /* Clear filter delay memory */
379 snd_emu10k1_ptr_write(emu, Z1, voice, 0);
380 snd_emu10k1_ptr_write(emu, Z2, voice, 0);
381 /* invalidate maps */
382 silent_page = ((unsigned int)emu->silent_page.addr << emu->address_mode) | (emu->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
383 snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
384 snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
385 /* modulation envelope */
386 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
387 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
388 snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
389 snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
390 snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
391 snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
392 snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
393 snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
394 snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
395 snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
396 /* volume envelope */
397 snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
398 snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
399 /* filter envelope */
400 snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
401 /* pitch envelope */
402 snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
404 spin_unlock_irqrestore(&emu->reg_lock, flags);
407 static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
408 struct snd_pcm_hw_params *hw_params)
410 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
411 struct snd_pcm_runtime *runtime = substream->runtime;
412 struct snd_emu10k1_pcm *epcm = runtime->private_data;
413 size_t alloc_size;
414 int err;
416 if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0)
417 return err;
419 alloc_size = params_buffer_bytes(hw_params);
420 if (emu->iommu_workaround)
421 alloc_size += EMUPAGESIZE;
422 err = snd_pcm_lib_malloc_pages(substream, alloc_size);
423 if (err < 0)
424 return err;
425 if (emu->iommu_workaround && runtime->dma_bytes >= EMUPAGESIZE)
426 runtime->dma_bytes -= EMUPAGESIZE;
427 if (err > 0) { /* change */
428 int mapped;
429 if (epcm->memblk != NULL)
430 snd_emu10k1_free_pages(emu, epcm->memblk);
431 epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
432 epcm->start_addr = 0;
433 if (! epcm->memblk)
434 return -ENOMEM;
435 mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
436 if (mapped < 0)
437 return -ENOMEM;
438 epcm->start_addr = mapped << PAGE_SHIFT;
440 return 0;
443 static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
445 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
446 struct snd_pcm_runtime *runtime = substream->runtime;
447 struct snd_emu10k1_pcm *epcm;
449 if (runtime->private_data == NULL)
450 return 0;
451 epcm = runtime->private_data;
452 if (epcm->extra) {
453 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
454 epcm->extra = NULL;
456 if (epcm->voices[1]) {
457 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
458 epcm->voices[1] = NULL;
460 if (epcm->voices[0]) {
461 snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
462 epcm->voices[0] = NULL;
464 if (epcm->memblk) {
465 snd_emu10k1_free_pages(emu, epcm->memblk);
466 epcm->memblk = NULL;
467 epcm->start_addr = 0;
469 snd_pcm_lib_free_pages(substream);
470 return 0;
473 static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
475 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
476 struct snd_pcm_runtime *runtime = substream->runtime;
477 struct snd_emu10k1_pcm *epcm;
478 int i;
480 if (runtime->private_data == NULL)
481 return 0;
482 epcm = runtime->private_data;
483 if (epcm->extra) {
484 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
485 epcm->extra = NULL;
487 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
488 if (epcm->voices[i]) {
489 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
490 epcm->voices[i] = NULL;
493 if (epcm->memblk) {
494 snd_emu10k1_free_pages(emu, epcm->memblk);
495 epcm->memblk = NULL;
496 epcm->start_addr = 0;
498 snd_pcm_lib_free_pages(substream);
499 return 0;
502 static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
504 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
505 struct snd_pcm_runtime *runtime = substream->runtime;
506 struct snd_emu10k1_pcm *epcm = runtime->private_data;
507 unsigned int start_addr, end_addr;
509 start_addr = epcm->start_addr;
510 end_addr = snd_pcm_lib_period_bytes(substream);
511 if (runtime->channels == 2) {
512 start_addr >>= 1;
513 end_addr >>= 1;
515 end_addr += start_addr;
516 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
517 start_addr, end_addr, NULL);
518 start_addr = epcm->start_addr;
519 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
520 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
521 start_addr, end_addr,
522 &emu->pcm_mixer[substream->number]);
523 if (epcm->voices[1])
524 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
525 start_addr, end_addr,
526 &emu->pcm_mixer[substream->number]);
527 return 0;
530 static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
532 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
533 struct snd_pcm_runtime *runtime = substream->runtime;
534 struct snd_emu10k1_pcm *epcm = runtime->private_data;
535 unsigned int start_addr, end_addr;
536 unsigned int channel_size;
537 int i;
539 start_addr = epcm->start_addr;
540 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
543 * the kX driver leaves some space between voices
545 channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
547 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
548 start_addr, start_addr + (channel_size / 2), NULL);
550 /* only difference with the master voice is we use it for the pointer */
551 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
552 start_addr, start_addr + channel_size,
553 &emu->efx_pcm_mixer[0]);
555 start_addr += channel_size;
556 for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
557 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
558 start_addr, start_addr + channel_size,
559 &emu->efx_pcm_mixer[i]);
560 start_addr += channel_size;
563 return 0;
566 static const struct snd_pcm_hardware snd_emu10k1_efx_playback =
568 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
569 SNDRV_PCM_INFO_BLOCK_TRANSFER |
570 SNDRV_PCM_INFO_RESUME |
571 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
572 .formats = SNDRV_PCM_FMTBIT_S16_LE,
573 .rates = SNDRV_PCM_RATE_48000,
574 .rate_min = 48000,
575 .rate_max = 48000,
576 .channels_min = NUM_EFX_PLAYBACK,
577 .channels_max = NUM_EFX_PLAYBACK,
578 .buffer_bytes_max = (64*1024),
579 .period_bytes_min = 64,
580 .period_bytes_max = (64*1024),
581 .periods_min = 2,
582 .periods_max = 2,
583 .fifo_size = 0,
586 static int snd_emu10k1_capture_hw_params(struct snd_pcm_substream *substream,
587 struct snd_pcm_hw_params *hw_params)
589 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
592 static int snd_emu10k1_capture_hw_free(struct snd_pcm_substream *substream)
594 return snd_pcm_lib_free_pages(substream);
597 static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
599 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
600 struct snd_pcm_runtime *runtime = substream->runtime;
601 struct snd_emu10k1_pcm *epcm = runtime->private_data;
602 int idx;
604 /* zeroing the buffer size will stop capture */
605 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
606 switch (epcm->type) {
607 case CAPTURE_AC97ADC:
608 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
609 break;
610 case CAPTURE_EFX:
611 if (emu->audigy) {
612 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
613 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
614 } else
615 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
616 break;
617 default:
618 break;
620 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
621 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
622 epcm->capture_bs_val = 0;
623 for (idx = 0; idx < 31; idx++) {
624 if (capture_period_sizes[idx] == epcm->capture_bufsize) {
625 epcm->capture_bs_val = idx + 1;
626 break;
629 if (epcm->capture_bs_val == 0) {
630 snd_BUG();
631 epcm->capture_bs_val++;
633 if (epcm->type == CAPTURE_AC97ADC) {
634 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
635 if (runtime->channels > 1)
636 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
637 epcm->capture_cr_val |= emu->audigy ?
638 snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
639 snd_emu10k1_capture_rate_reg(runtime->rate);
641 return 0;
644 static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
646 struct snd_pcm_runtime *runtime;
647 unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
649 if (evoice == NULL)
650 return;
651 runtime = evoice->epcm->substream->runtime;
652 voice = evoice->number;
653 stereo = (!extra && runtime->channels == 2);
654 sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
655 ccis = emu10k1_ccis(stereo, sample == 0);
656 /* set cs to 2 * number of cache registers beside the invalidated */
657 cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
658 if (cs > 16) cs = 16;
659 for (i = 0; i < cs; i++) {
660 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
661 if (stereo) {
662 snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
665 /* reset cache */
666 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
667 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
668 if (stereo) {
669 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
670 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
672 /* fill cache */
673 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
674 if (stereo) {
675 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
679 static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
680 int master, int extra,
681 struct snd_emu10k1_pcm_mixer *mix)
683 struct snd_pcm_substream *substream;
684 struct snd_pcm_runtime *runtime;
685 unsigned int attn, vattn;
686 unsigned int voice, tmp;
688 if (evoice == NULL) /* skip second voice for mono */
689 return;
690 substream = evoice->epcm->substream;
691 runtime = substream->runtime;
692 voice = evoice->number;
694 attn = extra ? 0 : 0x00ff;
695 tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
696 vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
697 snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
698 snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
699 snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
700 snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
701 snd_emu10k1_voice_clear_loop_stop(emu, voice);
704 static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra)
706 struct snd_pcm_substream *substream;
707 struct snd_pcm_runtime *runtime;
708 unsigned int voice, pitch, pitch_target;
710 if (evoice == NULL) /* skip second voice for mono */
711 return;
712 substream = evoice->epcm->substream;
713 runtime = substream->runtime;
714 voice = evoice->number;
716 pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
717 if (emu->card_capabilities->emu_model)
718 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
719 else
720 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
721 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
722 if (master || evoice->epcm->type == PLAYBACK_EFX)
723 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
724 snd_emu10k1_ptr_write(emu, IP, voice, pitch);
725 if (extra)
726 snd_emu10k1_voice_intr_enable(emu, voice);
729 static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
731 unsigned int voice;
733 if (evoice == NULL)
734 return;
735 voice = evoice->number;
736 snd_emu10k1_voice_intr_disable(emu, voice);
737 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
738 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
739 snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
740 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
741 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
742 snd_emu10k1_ptr_write(emu, IP, voice, 0);
745 static inline void snd_emu10k1_playback_mangle_extra(struct snd_emu10k1 *emu,
746 struct snd_emu10k1_pcm *epcm,
747 struct snd_pcm_substream *substream,
748 struct snd_pcm_runtime *runtime)
750 unsigned int ptr, period_pos;
752 /* try to sychronize the current position for the interrupt
753 source voice */
754 period_pos = runtime->status->hw_ptr - runtime->hw_ptr_interrupt;
755 period_pos %= runtime->period_size;
756 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->extra->number);
757 ptr &= ~0x00ffffff;
758 ptr |= epcm->ccca_start_addr + period_pos;
759 snd_emu10k1_ptr_write(emu, CCCA, epcm->extra->number, ptr);
762 static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
763 int cmd)
765 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
766 struct snd_pcm_runtime *runtime = substream->runtime;
767 struct snd_emu10k1_pcm *epcm = runtime->private_data;
768 struct snd_emu10k1_pcm_mixer *mix;
769 int result = 0;
772 dev_dbg(emu->card->dev,
773 "trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n",
774 (int)emu, cmd, substream->ops->pointer(substream))
776 spin_lock(&emu->reg_lock);
777 switch (cmd) {
778 case SNDRV_PCM_TRIGGER_START:
779 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */
780 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
781 /* fall through */
782 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
783 case SNDRV_PCM_TRIGGER_RESUME:
784 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)
785 snd_emu10k1_playback_mangle_extra(emu, epcm, substream, runtime);
786 mix = &emu->pcm_mixer[substream->number];
787 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
788 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
789 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
790 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
791 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
792 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
793 epcm->running = 1;
794 break;
795 case SNDRV_PCM_TRIGGER_STOP:
796 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
797 case SNDRV_PCM_TRIGGER_SUSPEND:
798 epcm->running = 0;
799 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
800 snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
801 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
802 break;
803 default:
804 result = -EINVAL;
805 break;
807 spin_unlock(&emu->reg_lock);
808 return result;
811 static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
812 int cmd)
814 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
815 struct snd_pcm_runtime *runtime = substream->runtime;
816 struct snd_emu10k1_pcm *epcm = runtime->private_data;
817 int result = 0;
819 spin_lock(&emu->reg_lock);
820 switch (cmd) {
821 case SNDRV_PCM_TRIGGER_START:
822 case SNDRV_PCM_TRIGGER_RESUME:
823 /* hmm this should cause full and half full interrupt to be raised? */
824 outl(epcm->capture_ipr, emu->port + IPR);
825 snd_emu10k1_intr_enable(emu, epcm->capture_inte);
827 dev_dbg(emu->card->dev, "adccr = 0x%x, adcbs = 0x%x\n",
828 epcm->adccr, epcm->adcbs);
830 switch (epcm->type) {
831 case CAPTURE_AC97ADC:
832 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
833 break;
834 case CAPTURE_EFX:
835 if (emu->audigy) {
836 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
837 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
838 dev_dbg(emu->card->dev,
839 "cr_val=0x%x, cr_val2=0x%x\n",
840 epcm->capture_cr_val,
841 epcm->capture_cr_val2);
842 } else
843 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
844 break;
845 default:
846 break;
848 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
849 epcm->running = 1;
850 epcm->first_ptr = 1;
851 break;
852 case SNDRV_PCM_TRIGGER_STOP:
853 case SNDRV_PCM_TRIGGER_SUSPEND:
854 epcm->running = 0;
855 snd_emu10k1_intr_disable(emu, epcm->capture_inte);
856 outl(epcm->capture_ipr, emu->port + IPR);
857 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
858 switch (epcm->type) {
859 case CAPTURE_AC97ADC:
860 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
861 break;
862 case CAPTURE_EFX:
863 if (emu->audigy) {
864 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
865 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
866 } else
867 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
868 break;
869 default:
870 break;
872 break;
873 default:
874 result = -EINVAL;
876 spin_unlock(&emu->reg_lock);
877 return result;
880 static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
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 unsigned int ptr;
887 if (!epcm->running)
888 return 0;
889 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
890 #if 0 /* Perex's code */
891 ptr += runtime->buffer_size;
892 ptr -= epcm->ccca_start_addr;
893 ptr %= runtime->buffer_size;
894 #else /* EMU10K1 Open Source code from Creative */
895 if (ptr < epcm->ccca_start_addr)
896 ptr += runtime->buffer_size - epcm->ccca_start_addr;
897 else {
898 ptr -= epcm->ccca_start_addr;
899 if (ptr >= runtime->buffer_size)
900 ptr -= runtime->buffer_size;
902 #endif
904 dev_dbg(emu->card->dev,
905 "ptr = 0x%lx, buffer_size = 0x%lx, period_size = 0x%lx\n",
906 (long)ptr, (long)runtime->buffer_size,
907 (long)runtime->period_size);
909 return ptr;
913 static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
914 int cmd)
916 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
917 struct snd_pcm_runtime *runtime = substream->runtime;
918 struct snd_emu10k1_pcm *epcm = runtime->private_data;
919 int i;
920 int result = 0;
922 spin_lock(&emu->reg_lock);
923 switch (cmd) {
924 case SNDRV_PCM_TRIGGER_START:
925 /* prepare voices */
926 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
927 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
929 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
931 /* fall through */
932 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
933 case SNDRV_PCM_TRIGGER_RESUME:
934 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
935 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
936 &emu->efx_pcm_mixer[0]);
937 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
938 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
939 &emu->efx_pcm_mixer[i]);
940 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
941 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
942 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
943 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
944 epcm->running = 1;
945 break;
946 case SNDRV_PCM_TRIGGER_SUSPEND:
947 case SNDRV_PCM_TRIGGER_STOP:
948 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
949 epcm->running = 0;
950 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
951 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
953 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
954 break;
955 default:
956 result = -EINVAL;
957 break;
959 spin_unlock(&emu->reg_lock);
960 return result;
964 static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
966 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
967 struct snd_pcm_runtime *runtime = substream->runtime;
968 struct snd_emu10k1_pcm *epcm = runtime->private_data;
969 unsigned int ptr;
971 if (!epcm->running)
972 return 0;
973 if (epcm->first_ptr) {
974 udelay(50); /* hack, it takes awhile until capture is started */
975 epcm->first_ptr = 0;
977 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
978 return bytes_to_frames(runtime, ptr);
982 * Playback support device description
985 static const struct snd_pcm_hardware snd_emu10k1_playback =
987 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
988 SNDRV_PCM_INFO_BLOCK_TRANSFER |
989 SNDRV_PCM_INFO_RESUME |
990 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
991 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
992 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
993 .rate_min = 4000,
994 .rate_max = 96000,
995 .channels_min = 1,
996 .channels_max = 2,
997 .buffer_bytes_max = (128*1024),
998 .period_bytes_min = 64,
999 .period_bytes_max = (128*1024),
1000 .periods_min = 1,
1001 .periods_max = 1024,
1002 .fifo_size = 0,
1006 * Capture support device description
1009 static const struct snd_pcm_hardware snd_emu10k1_capture =
1011 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1012 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1013 SNDRV_PCM_INFO_RESUME |
1014 SNDRV_PCM_INFO_MMAP_VALID),
1015 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1016 .rates = SNDRV_PCM_RATE_8000_48000,
1017 .rate_min = 8000,
1018 .rate_max = 48000,
1019 .channels_min = 1,
1020 .channels_max = 2,
1021 .buffer_bytes_max = (64*1024),
1022 .period_bytes_min = 384,
1023 .period_bytes_max = (64*1024),
1024 .periods_min = 2,
1025 .periods_max = 2,
1026 .fifo_size = 0,
1029 static const struct snd_pcm_hardware snd_emu10k1_capture_efx =
1031 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1032 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1033 SNDRV_PCM_INFO_RESUME |
1034 SNDRV_PCM_INFO_MMAP_VALID),
1035 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1036 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1037 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1038 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
1039 .rate_min = 44100,
1040 .rate_max = 192000,
1041 .channels_min = 8,
1042 .channels_max = 8,
1043 .buffer_bytes_max = (64*1024),
1044 .period_bytes_min = 384,
1045 .period_bytes_max = (64*1024),
1046 .periods_min = 2,
1047 .periods_max = 2,
1048 .fifo_size = 0,
1055 static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
1057 struct snd_ctl_elem_id id;
1059 if (! kctl)
1060 return;
1061 if (activate)
1062 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1063 else
1064 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1065 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
1066 SNDRV_CTL_EVENT_MASK_INFO,
1067 snd_ctl_build_ioff(&id, kctl, idx));
1070 static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1072 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
1073 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
1074 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
1077 static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1079 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
1080 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
1081 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
1084 static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
1086 kfree(runtime->private_data);
1089 static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
1091 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1092 struct snd_emu10k1_pcm_mixer *mix;
1093 int i;
1095 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1096 mix = &emu->efx_pcm_mixer[i];
1097 mix->epcm = NULL;
1098 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1100 return 0;
1103 static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
1105 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1106 struct snd_emu10k1_pcm *epcm;
1107 struct snd_emu10k1_pcm_mixer *mix;
1108 struct snd_pcm_runtime *runtime = substream->runtime;
1109 int i;
1111 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1112 if (epcm == NULL)
1113 return -ENOMEM;
1114 epcm->emu = emu;
1115 epcm->type = PLAYBACK_EFX;
1116 epcm->substream = substream;
1118 emu->pcm_playback_efx_substream = substream;
1120 runtime->private_data = epcm;
1121 runtime->private_free = snd_emu10k1_pcm_free_substream;
1122 runtime->hw = snd_emu10k1_efx_playback;
1124 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1125 mix = &emu->efx_pcm_mixer[i];
1126 mix->send_routing[0][0] = i;
1127 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1128 mix->send_volume[0][0] = 255;
1129 mix->attn[0] = 0xffff;
1130 mix->epcm = epcm;
1131 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1133 return 0;
1136 static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
1138 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1139 struct snd_emu10k1_pcm *epcm;
1140 struct snd_emu10k1_pcm_mixer *mix;
1141 struct snd_pcm_runtime *runtime = substream->runtime;
1142 int i, err, sample_rate;
1144 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1145 if (epcm == NULL)
1146 return -ENOMEM;
1147 epcm->emu = emu;
1148 epcm->type = PLAYBACK_EMUVOICE;
1149 epcm->substream = substream;
1150 runtime->private_data = epcm;
1151 runtime->private_free = snd_emu10k1_pcm_free_substream;
1152 runtime->hw = snd_emu10k1_playback;
1153 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
1154 kfree(epcm);
1155 return err;
1157 if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
1158 kfree(epcm);
1159 return err;
1161 if (emu->card_capabilities->emu_model && emu->emu1010.internal_clock == 0)
1162 sample_rate = 44100;
1163 else
1164 sample_rate = 48000;
1165 err = snd_pcm_hw_rule_noresample(runtime, sample_rate);
1166 if (err < 0) {
1167 kfree(epcm);
1168 return err;
1170 mix = &emu->pcm_mixer[substream->number];
1171 for (i = 0; i < 4; i++)
1172 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1173 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1174 mix->send_volume[0][0] = mix->send_volume[0][1] =
1175 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1176 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1177 mix->epcm = epcm;
1178 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1179 return 0;
1182 static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
1184 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1185 struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
1187 mix->epcm = NULL;
1188 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1189 return 0;
1192 static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
1194 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1195 struct snd_pcm_runtime *runtime = substream->runtime;
1196 struct snd_emu10k1_pcm *epcm;
1198 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1199 if (epcm == NULL)
1200 return -ENOMEM;
1201 epcm->emu = emu;
1202 epcm->type = CAPTURE_AC97ADC;
1203 epcm->substream = substream;
1204 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1205 epcm->capture_inte = INTE_ADCBUFENABLE;
1206 epcm->capture_ba_reg = ADCBA;
1207 epcm->capture_bs_reg = ADCBS;
1208 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1209 runtime->private_data = epcm;
1210 runtime->private_free = snd_emu10k1_pcm_free_substream;
1211 runtime->hw = snd_emu10k1_capture;
1212 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1213 emu->pcm_capture_substream = substream;
1214 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1215 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1216 return 0;
1219 static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
1221 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1223 emu->capture_interrupt = NULL;
1224 emu->pcm_capture_substream = NULL;
1225 return 0;
1228 static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
1230 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1231 struct snd_emu10k1_pcm *epcm;
1232 struct snd_pcm_runtime *runtime = substream->runtime;
1234 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1235 if (epcm == NULL)
1236 return -ENOMEM;
1237 epcm->emu = emu;
1238 epcm->type = CAPTURE_AC97MIC;
1239 epcm->substream = substream;
1240 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1241 epcm->capture_inte = INTE_MICBUFENABLE;
1242 epcm->capture_ba_reg = MICBA;
1243 epcm->capture_bs_reg = MICBS;
1244 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1245 substream->runtime->private_data = epcm;
1246 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1247 runtime->hw = snd_emu10k1_capture;
1248 runtime->hw.rates = SNDRV_PCM_RATE_8000;
1249 runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1250 runtime->hw.channels_min = 1;
1251 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1252 emu->pcm_capture_mic_substream = substream;
1253 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1254 return 0;
1257 static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
1259 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1261 emu->capture_interrupt = NULL;
1262 emu->pcm_capture_mic_substream = NULL;
1263 return 0;
1266 static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
1268 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1269 struct snd_emu10k1_pcm *epcm;
1270 struct snd_pcm_runtime *runtime = substream->runtime;
1271 int nefx = emu->audigy ? 64 : 32;
1272 int idx;
1274 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1275 if (epcm == NULL)
1276 return -ENOMEM;
1277 epcm->emu = emu;
1278 epcm->type = CAPTURE_EFX;
1279 epcm->substream = substream;
1280 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1281 epcm->capture_inte = INTE_EFXBUFENABLE;
1282 epcm->capture_ba_reg = FXBA;
1283 epcm->capture_bs_reg = FXBS;
1284 epcm->capture_idx_reg = FXIDX;
1285 substream->runtime->private_data = epcm;
1286 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1287 runtime->hw = snd_emu10k1_capture_efx;
1288 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1289 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1290 spin_lock_irq(&emu->reg_lock);
1291 if (emu->card_capabilities->emu_model) {
1292 /* Nb. of channels has been increased to 16 */
1293 /* TODO
1294 * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
1295 * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1296 * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1297 * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
1298 * rate_min = 44100,
1299 * rate_max = 192000,
1300 * channels_min = 16,
1301 * channels_max = 16,
1302 * Need to add mixer control to fix sample rate
1304 * There are 32 mono channels of 16bits each.
1305 * 24bit Audio uses 2x channels over 16bit
1306 * 96kHz uses 2x channels over 48kHz
1307 * 192kHz uses 4x channels over 48kHz
1308 * So, for 48kHz 24bit, one has 16 channels
1309 * for 96kHz 24bit, one has 8 channels
1310 * for 192kHz 24bit, one has 4 channels
1313 #if 1
1314 switch (emu->emu1010.internal_clock) {
1315 case 0:
1316 /* For 44.1kHz */
1317 runtime->hw.rates = SNDRV_PCM_RATE_44100;
1318 runtime->hw.rate_min = runtime->hw.rate_max = 44100;
1319 runtime->hw.channels_min =
1320 runtime->hw.channels_max = 16;
1321 break;
1322 case 1:
1323 /* For 48kHz */
1324 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1325 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1326 runtime->hw.channels_min =
1327 runtime->hw.channels_max = 16;
1328 break;
1330 #endif
1331 #if 0
1332 /* For 96kHz */
1333 runtime->hw.rates = SNDRV_PCM_RATE_96000;
1334 runtime->hw.rate_min = runtime->hw.rate_max = 96000;
1335 runtime->hw.channels_min = runtime->hw.channels_max = 4;
1336 #endif
1337 #if 0
1338 /* For 192kHz */
1339 runtime->hw.rates = SNDRV_PCM_RATE_192000;
1340 runtime->hw.rate_min = runtime->hw.rate_max = 192000;
1341 runtime->hw.channels_min = runtime->hw.channels_max = 2;
1342 #endif
1343 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1344 /* efx_voices_mask[0] is expected to be zero
1345 * efx_voices_mask[1] is expected to have 32bits set
1347 } else {
1348 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1349 for (idx = 0; idx < nefx; idx++) {
1350 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1351 runtime->hw.channels_min++;
1352 runtime->hw.channels_max++;
1356 epcm->capture_cr_val = emu->efx_voices_mask[0];
1357 epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1358 spin_unlock_irq(&emu->reg_lock);
1359 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1360 emu->pcm_capture_efx_substream = substream;
1361 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1362 return 0;
1365 static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
1367 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1369 emu->capture_interrupt = NULL;
1370 emu->pcm_capture_efx_substream = NULL;
1371 return 0;
1374 static const struct snd_pcm_ops snd_emu10k1_playback_ops = {
1375 .open = snd_emu10k1_playback_open,
1376 .close = snd_emu10k1_playback_close,
1377 .ioctl = snd_pcm_lib_ioctl,
1378 .hw_params = snd_emu10k1_playback_hw_params,
1379 .hw_free = snd_emu10k1_playback_hw_free,
1380 .prepare = snd_emu10k1_playback_prepare,
1381 .trigger = snd_emu10k1_playback_trigger,
1382 .pointer = snd_emu10k1_playback_pointer,
1383 .page = snd_pcm_sgbuf_ops_page,
1386 static const struct snd_pcm_ops snd_emu10k1_capture_ops = {
1387 .open = snd_emu10k1_capture_open,
1388 .close = snd_emu10k1_capture_close,
1389 .ioctl = snd_pcm_lib_ioctl,
1390 .hw_params = snd_emu10k1_capture_hw_params,
1391 .hw_free = snd_emu10k1_capture_hw_free,
1392 .prepare = snd_emu10k1_capture_prepare,
1393 .trigger = snd_emu10k1_capture_trigger,
1394 .pointer = snd_emu10k1_capture_pointer,
1397 /* EFX playback */
1398 static const struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
1399 .open = snd_emu10k1_efx_playback_open,
1400 .close = snd_emu10k1_efx_playback_close,
1401 .ioctl = snd_pcm_lib_ioctl,
1402 .hw_params = snd_emu10k1_playback_hw_params,
1403 .hw_free = snd_emu10k1_efx_playback_hw_free,
1404 .prepare = snd_emu10k1_efx_playback_prepare,
1405 .trigger = snd_emu10k1_efx_playback_trigger,
1406 .pointer = snd_emu10k1_efx_playback_pointer,
1407 .page = snd_pcm_sgbuf_ops_page,
1410 int snd_emu10k1_pcm(struct snd_emu10k1 *emu, int device)
1412 struct snd_pcm *pcm;
1413 struct snd_pcm_substream *substream;
1414 int err;
1416 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1417 return err;
1419 pcm->private_data = emu;
1421 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1422 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1424 pcm->info_flags = 0;
1425 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1426 strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1427 emu->pcm = pcm;
1429 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1430 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1431 return err;
1433 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1434 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1436 return 0;
1439 int snd_emu10k1_pcm_multi(struct snd_emu10k1 *emu, int device)
1441 struct snd_pcm *pcm;
1442 struct snd_pcm_substream *substream;
1443 int err;
1445 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
1446 return err;
1448 pcm->private_data = emu;
1450 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1452 pcm->info_flags = 0;
1453 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1454 strcpy(pcm->name, "Multichannel Playback");
1455 emu->pcm_multi = pcm;
1457 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1458 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1459 return err;
1461 return 0;
1465 static const struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
1466 .open = snd_emu10k1_capture_mic_open,
1467 .close = snd_emu10k1_capture_mic_close,
1468 .ioctl = snd_pcm_lib_ioctl,
1469 .hw_params = snd_emu10k1_capture_hw_params,
1470 .hw_free = snd_emu10k1_capture_hw_free,
1471 .prepare = snd_emu10k1_capture_prepare,
1472 .trigger = snd_emu10k1_capture_trigger,
1473 .pointer = snd_emu10k1_capture_pointer,
1476 int snd_emu10k1_pcm_mic(struct snd_emu10k1 *emu, int device)
1478 struct snd_pcm *pcm;
1479 int err;
1481 if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
1482 return err;
1484 pcm->private_data = emu;
1486 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1488 pcm->info_flags = 0;
1489 strcpy(pcm->name, "Mic Capture");
1490 emu->pcm_mic = pcm;
1492 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1494 return 0;
1497 static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1499 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1500 int nefx = emu->audigy ? 64 : 32;
1501 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1502 uinfo->count = nefx;
1503 uinfo->value.integer.min = 0;
1504 uinfo->value.integer.max = 1;
1505 return 0;
1508 static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1510 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1511 int nefx = emu->audigy ? 64 : 32;
1512 int idx;
1514 spin_lock_irq(&emu->reg_lock);
1515 for (idx = 0; idx < nefx; idx++)
1516 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1517 spin_unlock_irq(&emu->reg_lock);
1518 return 0;
1521 static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1523 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1524 unsigned int nval[2], bits;
1525 int nefx = emu->audigy ? 64 : 32;
1526 int nefxb = emu->audigy ? 7 : 6;
1527 int change, idx;
1529 nval[0] = nval[1] = 0;
1530 for (idx = 0, bits = 0; idx < nefx; idx++)
1531 if (ucontrol->value.integer.value[idx]) {
1532 nval[idx / 32] |= 1 << (idx % 32);
1533 bits++;
1536 for (idx = 0; idx < nefxb; idx++)
1537 if (1 << idx == bits)
1538 break;
1540 if (idx >= nefxb)
1541 return -EINVAL;
1543 spin_lock_irq(&emu->reg_lock);
1544 change = (nval[0] != emu->efx_voices_mask[0]) ||
1545 (nval[1] != emu->efx_voices_mask[1]);
1546 emu->efx_voices_mask[0] = nval[0];
1547 emu->efx_voices_mask[1] = nval[1];
1548 spin_unlock_irq(&emu->reg_lock);
1549 return change;
1552 static const struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
1553 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1554 .name = "Captured FX8010 Outputs",
1555 .info = snd_emu10k1_pcm_efx_voices_mask_info,
1556 .get = snd_emu10k1_pcm_efx_voices_mask_get,
1557 .put = snd_emu10k1_pcm_efx_voices_mask_put
1560 static const struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
1561 .open = snd_emu10k1_capture_efx_open,
1562 .close = snd_emu10k1_capture_efx_close,
1563 .ioctl = snd_pcm_lib_ioctl,
1564 .hw_params = snd_emu10k1_capture_hw_params,
1565 .hw_free = snd_emu10k1_capture_hw_free,
1566 .prepare = snd_emu10k1_capture_prepare,
1567 .trigger = snd_emu10k1_capture_trigger,
1568 .pointer = snd_emu10k1_capture_pointer,
1572 /* EFX playback */
1574 #define INITIAL_TRAM_SHIFT 14
1575 #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1577 static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
1579 struct snd_pcm_substream *substream = private_data;
1580 snd_pcm_period_elapsed(substream);
1583 static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1584 unsigned short *dst_right,
1585 unsigned short *src,
1586 unsigned int count,
1587 unsigned int tram_shift)
1590 dev_dbg(emu->card->dev,
1591 "tram_poke1: dst_left = 0x%p, dst_right = 0x%p, "
1592 "src = 0x%p, count = 0x%x\n",
1593 dst_left, dst_right, src, count);
1595 if ((tram_shift & 1) == 0) {
1596 while (count--) {
1597 *dst_left-- = *src++;
1598 *dst_right-- = *src++;
1600 } else {
1601 while (count--) {
1602 *dst_right-- = *src++;
1603 *dst_left-- = *src++;
1608 static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1609 struct snd_pcm_indirect *rec, size_t bytes)
1611 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1612 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1613 unsigned int tram_size = pcm->buffer_size;
1614 unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1615 unsigned int frames = bytes >> 2, count;
1616 unsigned int tram_pos = pcm->tram_pos;
1617 unsigned int tram_shift = pcm->tram_shift;
1619 while (frames > tram_pos) {
1620 count = tram_pos + 1;
1621 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1622 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1623 src, count, tram_shift);
1624 src += count * 2;
1625 frames -= count;
1626 tram_pos = (tram_size / 2) - 1;
1627 tram_shift++;
1629 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1630 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1631 src, frames, tram_shift);
1632 tram_pos -= frames;
1633 pcm->tram_pos = tram_pos;
1634 pcm->tram_shift = tram_shift;
1637 static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
1639 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1640 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1642 return snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec,
1643 fx8010_pb_trans_copy);
1646 static int snd_emu10k1_fx8010_playback_hw_params(struct snd_pcm_substream *substream,
1647 struct snd_pcm_hw_params *hw_params)
1649 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1652 static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
1654 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1655 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1656 unsigned int i;
1658 for (i = 0; i < pcm->channels; i++)
1659 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1660 snd_pcm_lib_free_pages(substream);
1661 return 0;
1664 static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
1666 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1667 struct snd_pcm_runtime *runtime = substream->runtime;
1668 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1669 unsigned int i;
1672 dev_dbg(emu->card->dev, "prepare: etram_pages = 0x%p, dma_area = 0x%x, "
1673 "buffer_size = 0x%x (0x%x)\n",
1674 emu->fx8010.etram_pages, runtime->dma_area,
1675 runtime->buffer_size, runtime->buffer_size << 2);
1677 memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1678 pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1679 pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1680 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1681 pcm->tram_shift = 0;
1682 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */
1683 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */
1684 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1685 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */
1686 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1687 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1688 for (i = 0; i < pcm->channels; i++)
1689 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1690 return 0;
1693 static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
1695 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1696 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1697 int result = 0;
1699 spin_lock(&emu->reg_lock);
1700 switch (cmd) {
1701 case SNDRV_PCM_TRIGGER_START:
1702 /* follow thru */
1703 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1704 case SNDRV_PCM_TRIGGER_RESUME:
1705 #ifdef EMU10K1_SET_AC3_IEC958
1707 int i;
1708 for (i = 0; i < 3; i++) {
1709 unsigned int bits;
1710 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1711 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1712 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1713 snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1716 #endif
1717 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1718 if (result < 0)
1719 goto __err;
1720 snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
1721 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1722 break;
1723 case SNDRV_PCM_TRIGGER_STOP:
1724 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1725 case SNDRV_PCM_TRIGGER_SUSPEND:
1726 snd_emu10k1_fx8010_unregister_irq_handler(emu, &pcm->irq);
1727 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1728 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1729 pcm->tram_shift = 0;
1730 break;
1731 default:
1732 result = -EINVAL;
1733 break;
1735 __err:
1736 spin_unlock(&emu->reg_lock);
1737 return result;
1740 static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
1742 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1743 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1744 size_t ptr; /* byte pointer */
1746 if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1747 return 0;
1748 ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1749 return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1752 static const struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
1754 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1755 SNDRV_PCM_INFO_RESUME |
1756 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE),
1757 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1758 .rates = SNDRV_PCM_RATE_48000,
1759 .rate_min = 48000,
1760 .rate_max = 48000,
1761 .channels_min = 1,
1762 .channels_max = 1,
1763 .buffer_bytes_max = (128*1024),
1764 .period_bytes_min = 1024,
1765 .period_bytes_max = (128*1024),
1766 .periods_min = 2,
1767 .periods_max = 1024,
1768 .fifo_size = 0,
1771 static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
1773 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1774 struct snd_pcm_runtime *runtime = substream->runtime;
1775 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1777 runtime->hw = snd_emu10k1_fx8010_playback;
1778 runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1779 runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1780 spin_lock_irq(&emu->reg_lock);
1781 if (pcm->valid == 0) {
1782 spin_unlock_irq(&emu->reg_lock);
1783 return -ENODEV;
1785 pcm->opened = 1;
1786 spin_unlock_irq(&emu->reg_lock);
1787 return 0;
1790 static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
1792 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1793 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1795 spin_lock_irq(&emu->reg_lock);
1796 pcm->opened = 0;
1797 spin_unlock_irq(&emu->reg_lock);
1798 return 0;
1801 static const struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
1802 .open = snd_emu10k1_fx8010_playback_open,
1803 .close = snd_emu10k1_fx8010_playback_close,
1804 .ioctl = snd_pcm_lib_ioctl,
1805 .hw_params = snd_emu10k1_fx8010_playback_hw_params,
1806 .hw_free = snd_emu10k1_fx8010_playback_hw_free,
1807 .prepare = snd_emu10k1_fx8010_playback_prepare,
1808 .trigger = snd_emu10k1_fx8010_playback_trigger,
1809 .pointer = snd_emu10k1_fx8010_playback_pointer,
1810 .ack = snd_emu10k1_fx8010_playback_transfer,
1813 int snd_emu10k1_pcm_efx(struct snd_emu10k1 *emu, int device)
1815 struct snd_pcm *pcm;
1816 struct snd_kcontrol *kctl;
1817 int err;
1819 if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1820 return err;
1822 pcm->private_data = emu;
1824 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1825 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1827 pcm->info_flags = 0;
1828 strcpy(pcm->name, "Multichannel Capture/PT Playback");
1829 emu->pcm_efx = pcm;
1831 /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
1832 * to these
1835 /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1836 if (emu->audigy) {
1837 emu->efx_voices_mask[0] = 0;
1838 if (emu->card_capabilities->emu_model)
1839 /* Pavel Hofman - 32 voices will be used for
1840 * capture (write mode) -
1841 * each bit = corresponding voice
1843 emu->efx_voices_mask[1] = 0xffffffff;
1844 else
1845 emu->efx_voices_mask[1] = 0xffff;
1846 } else {
1847 emu->efx_voices_mask[0] = 0xffff0000;
1848 emu->efx_voices_mask[1] = 0;
1850 /* For emu1010, the control has to set 32 upper bits (voices)
1851 * out of the 64 bits (voices) to true for the 16-channels capture
1852 * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
1853 * is already defined but the snd_emu10k1_pcm_efx_voices_mask
1854 * control can override this register's value.
1856 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1857 if (!kctl)
1858 return -ENOMEM;
1859 kctl->id.device = device;
1860 err = snd_ctl_add(emu->card, kctl);
1861 if (err < 0)
1862 return err;
1864 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1866 return 0;