blob: e22b8e2bbd884099339b807045176ceb33ee37d9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02002 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Creative Labs, Inc.
4 * Routines for control of EMU10K1 chips / PCM routines
5 * Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
6 *
7 * BUGS:
8 * --
9 *
10 * TODO:
11 * --
12 *
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.
17 *
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.
22 *
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
26 *
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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>
36
Takashi Iwaieb4698f2005-11-17 14:50:13 +010037static void snd_emu10k1_pcm_interrupt(struct snd_emu10k1 *emu,
38 struct snd_emu10k1_voice *voice)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Takashi Iwaieb4698f2005-11-17 14:50:13 +010040 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 if ((epcm = voice->epcm) == NULL)
43 return;
44 if (epcm->substream == NULL)
45 return;
46#if 0
Takashi Iwai28a97c12009-02-05 16:08:14 +010047 printk(KERN_DEBUG "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 epcm->substream->runtime->hw->pointer(emu, epcm->substream),
49 snd_pcm_lib_period_bytes(epcm->substream),
50 snd_pcm_lib_buffer_bytes(epcm->substream));
51#endif
52 snd_pcm_period_elapsed(epcm->substream);
53}
54
Takashi Iwaieb4698f2005-11-17 14:50:13 +010055static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu,
56 unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58#if 0
59 if (status & IPR_ADCBUFHALFFULL) {
60 if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
61 return;
62 }
63#endif
64 snd_pcm_period_elapsed(emu->pcm_capture_substream);
65}
66
Takashi Iwaieb4698f2005-11-17 14:50:13 +010067static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu,
68 unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70#if 0
71 if (status & IPR_MICBUFHALFFULL) {
72 if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
73 return;
74 }
75#endif
76 snd_pcm_period_elapsed(emu->pcm_capture_mic_substream);
77}
78
Takashi Iwaieb4698f2005-11-17 14:50:13 +010079static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu,
80 unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82#if 0
83 if (status & IPR_EFXBUFHALFFULL) {
84 if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
85 return;
86 }
87#endif
88 snd_pcm_period_elapsed(emu->pcm_capture_efx_substream);
89}
90
Takashi Iwaieb4698f2005-11-17 14:50:13 +010091static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Takashi Iwaieb4698f2005-11-17 14:50:13 +010093 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
94 struct snd_pcm_runtime *runtime = substream->runtime;
95 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 unsigned int ptr;
97
98 if (!epcm->running)
99 return 0;
100 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
101 ptr += runtime->buffer_size;
102 ptr -= epcm->ccca_start_addr;
103 ptr %= runtime->buffer_size;
104
105 return ptr;
106}
107
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100108static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm, int voices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 int err, i;
111
112 if (epcm->voices[1] != NULL && voices < 2) {
113 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
114 epcm->voices[1] = NULL;
115 }
116 for (i = 0; i < voices; i++) {
117 if (epcm->voices[i] == NULL)
118 break;
119 }
120 if (i == voices)
121 return 0; /* already allocated */
122
123 for (i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
124 if (epcm->voices[i]) {
125 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
126 epcm->voices[i] = NULL;
127 }
128 }
129 err = snd_emu10k1_voice_alloc(epcm->emu,
130 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
131 voices,
132 &epcm->voices[0]);
133
134 if (err < 0)
135 return err;
136 epcm->voices[0]->epcm = epcm;
137 if (voices > 1) {
138 for (i = 1; i < voices; i++) {
139 epcm->voices[i] = &epcm->emu->voices[epcm->voices[0]->number + i];
140 epcm->voices[i]->epcm = epcm;
141 }
142 }
143 if (epcm->extra == NULL) {
144 err = snd_emu10k1_voice_alloc(epcm->emu,
145 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
146 1,
147 &epcm->extra);
148 if (err < 0) {
Takashi Iwai28a97c12009-02-05 16:08:14 +0100149 /*
150 printk(KERN_DEBUG "pcm_channel_alloc: "
151 "failed extra: voices=%d, frame=%d\n",
152 voices, frame);
153 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 for (i = 0; i < voices; i++) {
155 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
156 epcm->voices[i] = NULL;
157 }
158 return err;
159 }
160 epcm->extra->epcm = epcm;
161 epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
162 }
163 return 0;
164}
165
166static unsigned int capture_period_sizes[31] = {
167 384, 448, 512, 640,
168 384*2, 448*2, 512*2, 640*2,
169 384*4, 448*4, 512*4, 640*4,
170 384*8, 448*8, 512*8, 640*8,
171 384*16, 448*16, 512*16, 640*16,
172 384*32, 448*32, 512*32, 640*32,
173 384*64, 448*64, 512*64, 640*64,
174 384*128,448*128,512*128
175};
176
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100177static struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .count = 31,
179 .list = capture_period_sizes,
180 .mask = 0
181};
182
183static unsigned int capture_rates[8] = {
184 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
185};
186
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100187static struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 .count = 8,
189 .list = capture_rates,
190 .mask = 0
191};
192
193static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
194{
195 switch (rate) {
196 case 8000: return ADCCR_SAMPLERATE_8;
197 case 11025: return ADCCR_SAMPLERATE_11;
198 case 16000: return ADCCR_SAMPLERATE_16;
199 case 22050: return ADCCR_SAMPLERATE_22;
200 case 24000: return ADCCR_SAMPLERATE_24;
201 case 32000: return ADCCR_SAMPLERATE_32;
202 case 44100: return ADCCR_SAMPLERATE_44;
203 case 48000: return ADCCR_SAMPLERATE_48;
204 default:
205 snd_BUG();
206 return ADCCR_SAMPLERATE_8;
207 }
208}
209
210static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
211{
212 switch (rate) {
213 case 8000: return A_ADCCR_SAMPLERATE_8;
214 case 11025: return A_ADCCR_SAMPLERATE_11;
215 case 12000: return A_ADCCR_SAMPLERATE_12; /* really supported? */
216 case 16000: return ADCCR_SAMPLERATE_16;
217 case 22050: return ADCCR_SAMPLERATE_22;
218 case 24000: return ADCCR_SAMPLERATE_24;
219 case 32000: return ADCCR_SAMPLERATE_32;
220 case 44100: return ADCCR_SAMPLERATE_44;
221 case 48000: return ADCCR_SAMPLERATE_48;
222 default:
223 snd_BUG();
224 return A_ADCCR_SAMPLERATE_8;
225 }
226}
227
228static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
229{
230 unsigned int pitch_target;
231
232 pitch_target = (rate << 8) / 375;
233 pitch_target = (pitch_target >> 1) + (pitch_target & 1);
234 return pitch_target;
235}
236
237#define PITCH_48000 0x00004000
238#define PITCH_96000 0x00008000
239#define PITCH_85000 0x00007155
240#define PITCH_80726 0x00006ba2
241#define PITCH_67882 0x00005a82
242#define PITCH_57081 0x00004c1c
243
244static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
245{
246 if (pitch_target == PITCH_48000)
247 return CCCA_INTERPROM_0;
248 else if (pitch_target < PITCH_48000)
249 return CCCA_INTERPROM_1;
250 else if (pitch_target >= PITCH_96000)
251 return CCCA_INTERPROM_0;
252 else if (pitch_target >= PITCH_85000)
253 return CCCA_INTERPROM_6;
254 else if (pitch_target >= PITCH_80726)
255 return CCCA_INTERPROM_5;
256 else if (pitch_target >= PITCH_67882)
257 return CCCA_INTERPROM_4;
258 else if (pitch_target >= PITCH_57081)
259 return CCCA_INTERPROM_3;
260 else
261 return CCCA_INTERPROM_2;
262}
263
264/*
265 * calculate cache invalidate size
266 *
267 * stereo: channel is stereo
268 * w_16: using 16bit samples
269 *
270 * returns: cache invalidate size in samples
271 */
Jesper Juhlfe5ac9d2005-04-20 10:03:31 +0200272static inline int emu10k1_ccis(int stereo, int w_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 if (w_16) {
275 return stereo ? 24 : 26;
276 } else {
277 return stereo ? 24*2 : 26*2;
278 }
279}
280
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100281static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int master, int extra,
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100283 struct snd_emu10k1_voice *evoice,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 unsigned int start_addr,
285 unsigned int end_addr,
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100286 struct snd_emu10k1_pcm_mixer *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100288 struct snd_pcm_substream *substream = evoice->epcm->substream;
289 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 unsigned int silent_page, tmp;
291 int voice, stereo, w_16;
292 unsigned char attn, send_amount[8];
293 unsigned char send_routing[8];
294 unsigned long flags;
295 unsigned int pitch_target;
296 unsigned int ccis;
297
298 voice = evoice->number;
299 stereo = runtime->channels == 2;
300 w_16 = snd_pcm_format_width(runtime->format) == 16;
301
302 if (!extra && stereo) {
303 start_addr >>= 1;
304 end_addr >>= 1;
305 }
306 if (w_16) {
307 start_addr >>= 1;
308 end_addr >>= 1;
309 }
310
311 spin_lock_irqsave(&emu->reg_lock, flags);
312
313 /* volume parameters */
314 if (extra) {
315 attn = 0;
316 memset(send_routing, 0, sizeof(send_routing));
317 send_routing[0] = 0;
318 send_routing[1] = 1;
319 send_routing[2] = 2;
320 send_routing[3] = 3;
321 memset(send_amount, 0, sizeof(send_amount));
322 } else {
323 /* mono, left, right (master voice = left) */
324 tmp = stereo ? (master ? 1 : 2) : 0;
325 memcpy(send_routing, &mix->send_routing[tmp][0], 8);
326 memcpy(send_amount, &mix->send_volume[tmp][0], 8);
327 }
328
329 ccis = emu10k1_ccis(stereo, w_16);
330
331 if (master) {
332 evoice->epcm->ccca_start_addr = start_addr + ccis;
333 if (extra) {
334 start_addr += ccis;
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200335 end_addr += ccis + emu->delay_pcm_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
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);
342 }
343 }
344
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100345 /* setup routing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 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));
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100359 /* Stop CA */
360 /* Assumption that PT is already 0 so no harm overwriting */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 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));
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200363 snd_emu10k1_ptr_write(emu, PSST, voice,
364 (start_addr + (extra ? emu->delay_pcm_irq : 0)) |
365 (send_amount[2] << 24));
James Courtier-Dutton190d2c42007-11-04 14:08:26 +0000366 if (emu->card_capabilities->emu_model)
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +0100367 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
368 else
369 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 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));
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100378 /* Clear filter delay memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 snd_emu10k1_ptr_write(emu, Z1, voice, 0);
380 snd_emu10k1_ptr_write(emu, Z2, voice, 0);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100381 /* invalidate maps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 silent_page = ((unsigned int)emu->silent_page.addr << 1) | MAP_PTI_MASK;
383 snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
384 snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100385 /* modulation envelope */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 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);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100396 /* volume envelope */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
398 snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100399 /* filter envelope */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100401 /* pitch envelope */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
403
404 spin_unlock_irqrestore(&emu->reg_lock, flags);
405}
406
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100407static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
408 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100410 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 int err;
414
415 if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0)
416 return err;
417 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
418 return err;
419 if (err > 0) { /* change */
Takashi Iwaie017fa52005-11-17 10:44:51 +0100420 int mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (epcm->memblk != NULL)
422 snd_emu10k1_free_pages(emu, epcm->memblk);
Takashi Iwaie017fa52005-11-17 10:44:51 +0100423 epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
424 epcm->start_addr = 0;
425 if (! epcm->memblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return -ENOMEM;
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100427 mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
Takashi Iwaie017fa52005-11-17 10:44:51 +0100428 if (mapped < 0)
429 return -ENOMEM;
430 epcm->start_addr = mapped << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432 return 0;
433}
434
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100435static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100437 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
438 struct snd_pcm_runtime *runtime = substream->runtime;
439 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (runtime->private_data == NULL)
442 return 0;
443 epcm = runtime->private_data;
444 if (epcm->extra) {
445 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
446 epcm->extra = NULL;
447 }
448 if (epcm->voices[1]) {
449 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
450 epcm->voices[1] = NULL;
451 }
452 if (epcm->voices[0]) {
453 snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
454 epcm->voices[0] = NULL;
455 }
456 if (epcm->memblk) {
457 snd_emu10k1_free_pages(emu, epcm->memblk);
458 epcm->memblk = NULL;
459 epcm->start_addr = 0;
460 }
461 snd_pcm_lib_free_pages(substream);
462 return 0;
463}
464
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100465static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100467 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
468 struct snd_pcm_runtime *runtime = substream->runtime;
469 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int i;
471
472 if (runtime->private_data == NULL)
473 return 0;
474 epcm = runtime->private_data;
475 if (epcm->extra) {
476 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
477 epcm->extra = NULL;
478 }
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100479 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (epcm->voices[i]) {
481 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
482 epcm->voices[i] = NULL;
483 }
484 }
485 if (epcm->memblk) {
486 snd_emu10k1_free_pages(emu, epcm->memblk);
487 epcm->memblk = NULL;
488 epcm->start_addr = 0;
489 }
490 snd_pcm_lib_free_pages(substream);
491 return 0;
492}
493
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100494static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100496 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
497 struct snd_pcm_runtime *runtime = substream->runtime;
498 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 unsigned int start_addr, end_addr;
500
501 start_addr = epcm->start_addr;
502 end_addr = snd_pcm_lib_period_bytes(substream);
503 if (runtime->channels == 2) {
504 start_addr >>= 1;
505 end_addr >>= 1;
506 }
507 end_addr += start_addr;
508 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
509 start_addr, end_addr, NULL);
510 start_addr = epcm->start_addr;
511 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
512 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
513 start_addr, end_addr,
514 &emu->pcm_mixer[substream->number]);
515 if (epcm->voices[1])
516 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
517 start_addr, end_addr,
518 &emu->pcm_mixer[substream->number]);
519 return 0;
520}
521
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100522static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100524 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
525 struct snd_pcm_runtime *runtime = substream->runtime;
526 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 unsigned int start_addr, end_addr;
528 unsigned int channel_size;
529 int i;
530
531 start_addr = epcm->start_addr;
532 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
533
534 /*
535 * the kX driver leaves some space between voices
536 */
537 channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
538
539 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
540 start_addr, start_addr + (channel_size / 2), NULL);
541
542 /* only difference with the master voice is we use it for the pointer */
543 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
544 start_addr, start_addr + channel_size,
545 &emu->efx_pcm_mixer[0]);
546
547 start_addr += channel_size;
548 for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
549 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
550 start_addr, start_addr + channel_size,
551 &emu->efx_pcm_mixer[i]);
552 start_addr += channel_size;
553 }
554
555 return 0;
556}
557
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100558static struct snd_pcm_hardware snd_emu10k1_efx_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
561 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai09668b42005-11-17 16:14:10 +0100562 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
564 .formats = SNDRV_PCM_FMTBIT_S16_LE,
565 .rates = SNDRV_PCM_RATE_48000,
566 .rate_min = 48000,
567 .rate_max = 48000,
568 .channels_min = NUM_EFX_PLAYBACK,
569 .channels_max = NUM_EFX_PLAYBACK,
570 .buffer_bytes_max = (64*1024),
571 .period_bytes_min = 64,
572 .period_bytes_max = (64*1024),
573 .periods_min = 2,
574 .periods_max = 2,
575 .fifo_size = 0,
576};
577
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100578static int snd_emu10k1_capture_hw_params(struct snd_pcm_substream *substream,
579 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
582}
583
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100584static int snd_emu10k1_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 return snd_pcm_lib_free_pages(substream);
587}
588
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100589static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100591 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
592 struct snd_pcm_runtime *runtime = substream->runtime;
593 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 int idx;
595
596 /* zeroing the buffer size will stop capture */
597 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
598 switch (epcm->type) {
599 case CAPTURE_AC97ADC:
600 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
601 break;
602 case CAPTURE_EFX:
603 if (emu->audigy) {
604 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
605 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
606 } else
607 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
608 break;
609 default:
610 break;
611 }
612 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
613 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
614 epcm->capture_bs_val = 0;
615 for (idx = 0; idx < 31; idx++) {
616 if (capture_period_sizes[idx] == epcm->capture_bufsize) {
617 epcm->capture_bs_val = idx + 1;
618 break;
619 }
620 }
621 if (epcm->capture_bs_val == 0) {
622 snd_BUG();
623 epcm->capture_bs_val++;
624 }
625 if (epcm->type == CAPTURE_AC97ADC) {
626 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
627 if (runtime->channels > 1)
628 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
629 epcm->capture_cr_val |= emu->audigy ?
630 snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
631 snd_emu10k1_capture_rate_reg(runtime->rate);
632 }
633 return 0;
634}
635
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100636static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100638 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
640
641 if (evoice == NULL)
642 return;
643 runtime = evoice->epcm->substream->runtime;
644 voice = evoice->number;
645 stereo = (!extra && runtime->channels == 2);
646 sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
647 ccis = emu10k1_ccis(stereo, sample == 0);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100648 /* set cs to 2 * number of cache registers beside the invalidated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
650 if (cs > 16) cs = 16;
651 for (i = 0; i < cs; i++) {
652 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
653 if (stereo) {
654 snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
655 }
656 }
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100657 /* reset cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
659 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
660 if (stereo) {
661 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
662 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
663 }
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100664 /* fill cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
666 if (stereo) {
667 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
668 }
669}
670
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100671static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int master, int extra,
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100673 struct snd_emu10k1_pcm_mixer *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100675 struct snd_pcm_substream *substream;
676 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 unsigned int attn, vattn;
678 unsigned int voice, tmp;
679
680 if (evoice == NULL) /* skip second voice for mono */
681 return;
682 substream = evoice->epcm->substream;
683 runtime = substream->runtime;
684 voice = evoice->number;
685
686 attn = extra ? 0 : 0x00ff;
687 tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
688 vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
689 snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
690 snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
691 snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
692 snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
693 snd_emu10k1_voice_clear_loop_stop(emu, voice);
694}
695
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100696static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100698 struct snd_pcm_substream *substream;
699 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 unsigned int voice, pitch, pitch_target;
701
702 if (evoice == NULL) /* skip second voice for mono */
703 return;
704 substream = evoice->epcm->substream;
705 runtime = substream->runtime;
706 voice = evoice->number;
707
708 pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
James Courtier-Dutton190d2c42007-11-04 14:08:26 +0000709 if (emu->card_capabilities->emu_model)
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +0100710 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
711 else
712 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
714 if (master || evoice->epcm->type == PLAYBACK_EFX)
715 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
716 snd_emu10k1_ptr_write(emu, IP, voice, pitch);
717 if (extra)
718 snd_emu10k1_voice_intr_enable(emu, voice);
719}
720
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100721static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 unsigned int voice;
724
725 if (evoice == NULL)
726 return;
727 voice = evoice->number;
728 snd_emu10k1_voice_intr_disable(emu, voice);
729 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
730 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
731 snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
732 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
733 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
734 snd_emu10k1_ptr_write(emu, IP, voice, 0);
735}
736
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200737static inline void snd_emu10k1_playback_mangle_extra(struct snd_emu10k1 *emu,
738 struct snd_emu10k1_pcm *epcm,
739 struct snd_pcm_substream *substream,
740 struct snd_pcm_runtime *runtime)
741{
742 unsigned int ptr, period_pos;
743
744 /* try to sychronize the current position for the interrupt
745 source voice */
746 period_pos = runtime->status->hw_ptr - runtime->hw_ptr_interrupt;
747 period_pos %= runtime->period_size;
748 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->extra->number);
749 ptr &= ~0x00ffffff;
750 ptr |= epcm->ccca_start_addr + period_pos;
751 snd_emu10k1_ptr_write(emu, CCCA, epcm->extra->number, ptr);
752}
753
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100754static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 int cmd)
756{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100757 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
758 struct snd_pcm_runtime *runtime = substream->runtime;
759 struct snd_emu10k1_pcm *epcm = runtime->private_data;
760 struct snd_emu10k1_pcm_mixer *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 int result = 0;
762
Takashi Iwai28a97c12009-02-05 16:08:14 +0100763 /*
764 printk(KERN_DEBUG "trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n",
765 (int)emu, cmd, substream->ops->pointer(substream))
766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 spin_lock(&emu->reg_lock);
768 switch (cmd) {
769 case SNDRV_PCM_TRIGGER_START:
770 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */
771 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
772 /* follow thru */
773 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai09668b42005-11-17 16:14:10 +0100774 case SNDRV_PCM_TRIGGER_RESUME:
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200775 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)
776 snd_emu10k1_playback_mangle_extra(emu, epcm, substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 mix = &emu->pcm_mixer[substream->number];
778 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
779 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
780 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
781 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
782 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
783 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
784 epcm->running = 1;
785 break;
786 case SNDRV_PCM_TRIGGER_STOP:
787 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai09668b42005-11-17 16:14:10 +0100788 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 epcm->running = 0;
790 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
791 snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
792 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
793 break;
794 default:
795 result = -EINVAL;
796 break;
797 }
798 spin_unlock(&emu->reg_lock);
799 return result;
800}
801
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100802static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 int cmd)
804{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100805 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
806 struct snd_pcm_runtime *runtime = substream->runtime;
807 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int result = 0;
809
810 spin_lock(&emu->reg_lock);
811 switch (cmd) {
812 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai09668b42005-11-17 16:14:10 +0100813 case SNDRV_PCM_TRIGGER_RESUME:
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100814 /* hmm this should cause full and half full interrupt to be raised? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 outl(epcm->capture_ipr, emu->port + IPR);
816 snd_emu10k1_intr_enable(emu, epcm->capture_inte);
Takashi Iwai28a97c12009-02-05 16:08:14 +0100817 /*
818 printk(KERN_DEBUG "adccr = 0x%x, adcbs = 0x%x\n",
819 epcm->adccr, epcm->adcbs);
820 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 switch (epcm->type) {
822 case CAPTURE_AC97ADC:
823 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
824 break;
825 case CAPTURE_EFX:
826 if (emu->audigy) {
827 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
828 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100829 snd_printdd("cr_val=0x%x, cr_val2=0x%x\n", epcm->capture_cr_val, epcm->capture_cr_val2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 } else
831 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
832 break;
833 default:
834 break;
835 }
836 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
837 epcm->running = 1;
838 epcm->first_ptr = 1;
839 break;
840 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai09668b42005-11-17 16:14:10 +0100841 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 epcm->running = 0;
843 snd_emu10k1_intr_disable(emu, epcm->capture_inte);
844 outl(epcm->capture_ipr, emu->port + IPR);
845 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
846 switch (epcm->type) {
847 case CAPTURE_AC97ADC:
848 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
849 break;
850 case CAPTURE_EFX:
851 if (emu->audigy) {
852 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
853 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
854 } else
855 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
856 break;
857 default:
858 break;
859 }
860 break;
861 default:
862 result = -EINVAL;
863 }
864 spin_unlock(&emu->reg_lock);
865 return result;
866}
867
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100868static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100870 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
871 struct snd_pcm_runtime *runtime = substream->runtime;
872 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 unsigned int ptr;
874
875 if (!epcm->running)
876 return 0;
877 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
878#if 0 /* Perex's code */
879 ptr += runtime->buffer_size;
880 ptr -= epcm->ccca_start_addr;
881 ptr %= runtime->buffer_size;
882#else /* EMU10K1 Open Source code from Creative */
883 if (ptr < epcm->ccca_start_addr)
884 ptr += runtime->buffer_size - epcm->ccca_start_addr;
885 else {
886 ptr -= epcm->ccca_start_addr;
887 if (ptr >= runtime->buffer_size)
888 ptr -= runtime->buffer_size;
889 }
890#endif
Takashi Iwai28a97c12009-02-05 16:08:14 +0100891 /*
892 printk(KERN_DEBUG
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200893 "ptr = 0x%lx, buffer_size = 0x%lx, period_size = 0x%lx\n",
894 (long)ptr, (long)runtime->buffer_size,
895 (long)runtime->period_size);
Takashi Iwai28a97c12009-02-05 16:08:14 +0100896 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return ptr;
898}
899
900
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100901static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 int cmd)
903{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100904 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
905 struct snd_pcm_runtime *runtime = substream->runtime;
906 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 int i;
908 int result = 0;
909
910 spin_lock(&emu->reg_lock);
911 switch (cmd) {
912 case SNDRV_PCM_TRIGGER_START:
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100913 /* prepare voices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
915 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
916 }
917 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
918
919 /* follow thru */
920 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai09668b42005-11-17 16:14:10 +0100921 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
923 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
924 &emu->efx_pcm_mixer[0]);
925 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
926 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
927 &emu->efx_pcm_mixer[i]);
928 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
929 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
930 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
931 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
932 epcm->running = 1;
933 break;
Takashi Iwai09668b42005-11-17 16:14:10 +0100934 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 case SNDRV_PCM_TRIGGER_STOP:
936 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
937 epcm->running = 0;
938 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
939 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
940 }
941 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
942 break;
943 default:
944 result = -EINVAL;
945 break;
946 }
947 spin_unlock(&emu->reg_lock);
948 return result;
949}
950
951
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100952static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100954 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
955 struct snd_pcm_runtime *runtime = substream->runtime;
956 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 unsigned int ptr;
958
959 if (!epcm->running)
960 return 0;
961 if (epcm->first_ptr) {
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100962 udelay(50); /* hack, it takes awhile until capture is started */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 epcm->first_ptr = 0;
964 }
965 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
966 return bytes_to_frames(runtime, ptr);
967}
968
969/*
970 * Playback support device description
971 */
972
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100973static struct snd_pcm_hardware snd_emu10k1_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
976 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai09668b42005-11-17 16:14:10 +0100977 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
979 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
980 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
981 .rate_min = 4000,
982 .rate_max = 96000,
983 .channels_min = 1,
984 .channels_max = 2,
985 .buffer_bytes_max = (128*1024),
986 .period_bytes_min = 64,
987 .period_bytes_max = (128*1024),
988 .periods_min = 1,
989 .periods_max = 1024,
990 .fifo_size = 0,
991};
992
993/*
994 * Capture support device description
995 */
996
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100997static struct snd_pcm_hardware snd_emu10k1_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1000 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai09668b42005-11-17 16:14:10 +01001001 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 SNDRV_PCM_INFO_MMAP_VALID),
1003 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1004 .rates = SNDRV_PCM_RATE_8000_48000,
1005 .rate_min = 8000,
1006 .rate_max = 48000,
1007 .channels_min = 1,
1008 .channels_max = 2,
1009 .buffer_bytes_max = (64*1024),
1010 .period_bytes_min = 384,
1011 .period_bytes_max = (64*1024),
1012 .periods_min = 2,
1013 .periods_max = 2,
1014 .fifo_size = 0,
1015};
1016
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001017static struct snd_pcm_hardware snd_emu10k1_capture_efx =
1018{
1019 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1020 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1021 SNDRV_PCM_INFO_RESUME |
1022 SNDRV_PCM_INFO_MMAP_VALID),
1023 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1024 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1025 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1026 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
1027 .rate_min = 44100,
1028 .rate_max = 192000,
1029 .channels_min = 8,
1030 .channels_max = 8,
1031 .buffer_bytes_max = (64*1024),
1032 .period_bytes_min = 384,
1033 .period_bytes_max = (64*1024),
1034 .periods_min = 2,
1035 .periods_max = 2,
1036 .fifo_size = 0,
1037};
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039/*
1040 *
1041 */
1042
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001043static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001045 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02001047 if (! kctl)
1048 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (activate)
1050 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1051 else
1052 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1053 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
1054 SNDRV_CTL_EVENT_MASK_INFO,
1055 snd_ctl_build_ioff(&id, kctl, idx));
1056}
1057
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001058static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
1061 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
1062 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
1063}
1064
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001065static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
1068 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
1069 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
1070}
1071
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001072static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Jesper Juhl4d572772005-05-30 17:30:32 +02001074 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001077static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001079 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1080 struct snd_emu10k1_pcm_mixer *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 int i;
1082
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001083 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 mix = &emu->efx_pcm_mixer[i];
1085 mix->epcm = NULL;
1086 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1087 }
1088 return 0;
1089}
1090
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001091static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001093 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1094 struct snd_emu10k1_pcm *epcm;
1095 struct snd_emu10k1_pcm_mixer *mix;
1096 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 int i;
1098
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001099 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (epcm == NULL)
1101 return -ENOMEM;
1102 epcm->emu = emu;
1103 epcm->type = PLAYBACK_EFX;
1104 epcm->substream = substream;
1105
1106 emu->pcm_playback_efx_substream = substream;
1107
1108 runtime->private_data = epcm;
1109 runtime->private_free = snd_emu10k1_pcm_free_substream;
1110 runtime->hw = snd_emu10k1_efx_playback;
1111
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001112 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 mix = &emu->efx_pcm_mixer[i];
1114 mix->send_routing[0][0] = i;
1115 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1116 mix->send_volume[0][0] = 255;
1117 mix->attn[0] = 0xffff;
1118 mix->epcm = epcm;
1119 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1120 }
1121 return 0;
1122}
1123
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001124static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001126 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1127 struct snd_emu10k1_pcm *epcm;
1128 struct snd_emu10k1_pcm_mixer *mix;
1129 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 int i, err;
1131
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001132 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (epcm == NULL)
1134 return -ENOMEM;
1135 epcm->emu = emu;
1136 epcm->type = PLAYBACK_EMUVOICE;
1137 epcm->substream = substream;
1138 runtime->private_data = epcm;
1139 runtime->private_free = snd_emu10k1_pcm_free_substream;
1140 runtime->hw = snd_emu10k1_playback;
1141 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
1142 kfree(epcm);
1143 return err;
1144 }
1145 if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
1146 kfree(epcm);
1147 return err;
1148 }
Clemens Ladisch57e5c632011-09-16 23:13:38 +02001149 err = snd_pcm_hw_rule_noresample(runtime, 48000);
1150 if (err < 0) {
1151 kfree(epcm);
1152 return err;
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 mix = &emu->pcm_mixer[substream->number];
1155 for (i = 0; i < 4; i++)
1156 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1157 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1158 mix->send_volume[0][0] = mix->send_volume[0][1] =
1159 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1160 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1161 mix->epcm = epcm;
1162 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1163 return 0;
1164}
1165
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001166static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001168 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1169 struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 mix->epcm = NULL;
1172 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1173 return 0;
1174}
1175
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001176static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001178 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1179 struct snd_pcm_runtime *runtime = substream->runtime;
1180 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001182 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (epcm == NULL)
1184 return -ENOMEM;
1185 epcm->emu = emu;
1186 epcm->type = CAPTURE_AC97ADC;
1187 epcm->substream = substream;
1188 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1189 epcm->capture_inte = INTE_ADCBUFENABLE;
1190 epcm->capture_ba_reg = ADCBA;
1191 epcm->capture_bs_reg = ADCBS;
1192 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1193 runtime->private_data = epcm;
1194 runtime->private_free = snd_emu10k1_pcm_free_substream;
1195 runtime->hw = snd_emu10k1_capture;
1196 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1197 emu->pcm_capture_substream = substream;
1198 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1199 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1200 return 0;
1201}
1202
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001203static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001205 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 emu->capture_interrupt = NULL;
1208 emu->pcm_capture_substream = NULL;
1209 return 0;
1210}
1211
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001212static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001214 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1215 struct snd_emu10k1_pcm *epcm;
1216 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001218 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (epcm == NULL)
1220 return -ENOMEM;
1221 epcm->emu = emu;
1222 epcm->type = CAPTURE_AC97MIC;
1223 epcm->substream = substream;
1224 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1225 epcm->capture_inte = INTE_MICBUFENABLE;
1226 epcm->capture_ba_reg = MICBA;
1227 epcm->capture_bs_reg = MICBS;
1228 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1229 substream->runtime->private_data = epcm;
1230 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1231 runtime->hw = snd_emu10k1_capture;
1232 runtime->hw.rates = SNDRV_PCM_RATE_8000;
1233 runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1234 runtime->hw.channels_min = 1;
1235 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1236 emu->pcm_capture_mic_substream = substream;
1237 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1238 return 0;
1239}
1240
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001241static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001243 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 emu->capture_interrupt = NULL;
1246 emu->pcm_capture_mic_substream = NULL;
1247 return 0;
1248}
1249
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001250static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001252 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1253 struct snd_emu10k1_pcm *epcm;
1254 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 int nefx = emu->audigy ? 64 : 32;
1256 int idx;
1257
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001258 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (epcm == NULL)
1260 return -ENOMEM;
1261 epcm->emu = emu;
1262 epcm->type = CAPTURE_EFX;
1263 epcm->substream = substream;
1264 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1265 epcm->capture_inte = INTE_EFXBUFENABLE;
1266 epcm->capture_ba_reg = FXBA;
1267 epcm->capture_bs_reg = FXBS;
1268 epcm->capture_idx_reg = FXIDX;
1269 substream->runtime->private_data = epcm;
1270 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001271 runtime->hw = snd_emu10k1_capture_efx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1273 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1274 spin_lock_irq(&emu->reg_lock);
James Courtier-Dutton190d2c42007-11-04 14:08:26 +00001275 if (emu->card_capabilities->emu_model) {
Pavel Hofman13d45702007-06-11 12:21:20 +02001276 /* Nb. of channels has been increased to 16 */
1277 /* TODO
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001278 * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
1279 * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1280 * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1281 * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
1282 * rate_min = 44100,
1283 * rate_max = 192000,
Pavel Hofman13d45702007-06-11 12:21:20 +02001284 * channels_min = 16,
1285 * channels_max = 16,
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001286 * Need to add mixer control to fix sample rate
1287 *
Pavel Hofman13d45702007-06-11 12:21:20 +02001288 * There are 32 mono channels of 16bits each.
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001289 * 24bit Audio uses 2x channels over 16bit
1290 * 96kHz uses 2x channels over 48kHz
1291 * 192kHz uses 4x channels over 48kHz
Pavel Hofman13d45702007-06-11 12:21:20 +02001292 * So, for 48kHz 24bit, one has 16 channels
1293 * for 96kHz 24bit, one has 8 channels
1294 * for 192kHz 24bit, one has 4 channels
1295 *
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001296 */
1297#if 1
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +01001298 switch (emu->emu1010.internal_clock) {
1299 case 0:
1300 /* For 44.1kHz */
1301 runtime->hw.rates = SNDRV_PCM_RATE_44100;
1302 runtime->hw.rate_min = runtime->hw.rate_max = 44100;
Pavel Hofman13d45702007-06-11 12:21:20 +02001303 runtime->hw.channels_min =
1304 runtime->hw.channels_max = 16;
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +01001305 break;
1306 case 1:
1307 /* For 48kHz */
1308 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1309 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
Pavel Hofman13d45702007-06-11 12:21:20 +02001310 runtime->hw.channels_min =
1311 runtime->hw.channels_max = 16;
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +01001312 break;
1313 };
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001314#endif
1315#if 0
1316 /* For 96kHz */
1317 runtime->hw.rates = SNDRV_PCM_RATE_96000;
1318 runtime->hw.rate_min = runtime->hw.rate_max = 96000;
1319 runtime->hw.channels_min = runtime->hw.channels_max = 4;
1320#endif
1321#if 0
1322 /* For 192kHz */
1323 runtime->hw.rates = SNDRV_PCM_RATE_192000;
1324 runtime->hw.rate_min = runtime->hw.rate_max = 192000;
1325 runtime->hw.channels_min = runtime->hw.channels_max = 2;
1326#endif
1327 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1328 /* efx_voices_mask[0] is expected to be zero
Pavel Hofman13d45702007-06-11 12:21:20 +02001329 * efx_voices_mask[1] is expected to have 32bits set
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001330 */
1331 } else {
1332 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1333 for (idx = 0; idx < nefx; idx++) {
1334 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1335 runtime->hw.channels_min++;
1336 runtime->hw.channels_max++;
1337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339 }
1340 epcm->capture_cr_val = emu->efx_voices_mask[0];
1341 epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1342 spin_unlock_irq(&emu->reg_lock);
1343 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1344 emu->pcm_capture_efx_substream = substream;
1345 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1346 return 0;
1347}
1348
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001349static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001351 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 emu->capture_interrupt = NULL;
1354 emu->pcm_capture_efx_substream = NULL;
1355 return 0;
1356}
1357
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001358static struct snd_pcm_ops snd_emu10k1_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 .open = snd_emu10k1_playback_open,
1360 .close = snd_emu10k1_playback_close,
1361 .ioctl = snd_pcm_lib_ioctl,
1362 .hw_params = snd_emu10k1_playback_hw_params,
1363 .hw_free = snd_emu10k1_playback_hw_free,
1364 .prepare = snd_emu10k1_playback_prepare,
1365 .trigger = snd_emu10k1_playback_trigger,
1366 .pointer = snd_emu10k1_playback_pointer,
1367 .page = snd_pcm_sgbuf_ops_page,
1368};
1369
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001370static struct snd_pcm_ops snd_emu10k1_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 .open = snd_emu10k1_capture_open,
1372 .close = snd_emu10k1_capture_close,
1373 .ioctl = snd_pcm_lib_ioctl,
1374 .hw_params = snd_emu10k1_capture_hw_params,
1375 .hw_free = snd_emu10k1_capture_hw_free,
1376 .prepare = snd_emu10k1_capture_prepare,
1377 .trigger = snd_emu10k1_capture_trigger,
1378 .pointer = snd_emu10k1_capture_pointer,
1379};
1380
1381/* EFX playback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001382static struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 .open = snd_emu10k1_efx_playback_open,
1384 .close = snd_emu10k1_efx_playback_close,
1385 .ioctl = snd_pcm_lib_ioctl,
1386 .hw_params = snd_emu10k1_playback_hw_params,
1387 .hw_free = snd_emu10k1_efx_playback_hw_free,
1388 .prepare = snd_emu10k1_efx_playback_prepare,
1389 .trigger = snd_emu10k1_efx_playback_trigger,
1390 .pointer = snd_emu10k1_efx_playback_pointer,
1391 .page = snd_pcm_sgbuf_ops_page,
1392};
1393
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001394int __devinit snd_emu10k1_pcm(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001396 struct snd_pcm *pcm;
1397 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 int err;
1399
1400 if (rpcm)
1401 *rpcm = NULL;
1402
1403 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1404 return err;
1405
1406 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1409 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1410
1411 pcm->info_flags = 0;
1412 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1413 strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1414 emu->pcm = pcm;
1415
1416 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1417 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1418 return err;
1419
1420 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1421 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1422
1423 if (rpcm)
1424 *rpcm = pcm;
1425
1426 return 0;
1427}
1428
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001429int __devinit snd_emu10k1_pcm_multi(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001431 struct snd_pcm *pcm;
1432 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int err;
1434
1435 if (rpcm)
1436 *rpcm = NULL;
1437
1438 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
1439 return err;
1440
1441 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1444
1445 pcm->info_flags = 0;
1446 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1447 strcpy(pcm->name, "Multichannel Playback");
Takashi Iwai09668b42005-11-17 16:14:10 +01001448 emu->pcm_multi = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1451 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1452 return err;
1453
1454 if (rpcm)
1455 *rpcm = pcm;
1456
1457 return 0;
1458}
1459
1460
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001461static struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 .open = snd_emu10k1_capture_mic_open,
1463 .close = snd_emu10k1_capture_mic_close,
1464 .ioctl = snd_pcm_lib_ioctl,
1465 .hw_params = snd_emu10k1_capture_hw_params,
1466 .hw_free = snd_emu10k1_capture_hw_free,
1467 .prepare = snd_emu10k1_capture_prepare,
1468 .trigger = snd_emu10k1_capture_trigger,
1469 .pointer = snd_emu10k1_capture_pointer,
1470};
1471
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001472int __devinit snd_emu10k1_pcm_mic(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001474 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 int err;
1476
1477 if (rpcm)
1478 *rpcm = NULL;
1479
1480 if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
1481 return err;
1482
1483 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1486
1487 pcm->info_flags = 0;
1488 strcpy(pcm->name, "Mic Capture");
1489 emu->pcm_mic = pcm;
1490
1491 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1492
1493 if (rpcm)
1494 *rpcm = pcm;
1495 return 0;
1496}
1497
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001498static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001500 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 int nefx = emu->audigy ? 64 : 32;
1502 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1503 uinfo->count = nefx;
1504 uinfo->value.integer.min = 0;
1505 uinfo->value.integer.max = 1;
1506 return 0;
1507}
1508
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001509static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001511 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 int nefx = emu->audigy ? 64 : 32;
1513 int idx;
1514
1515 spin_lock_irq(&emu->reg_lock);
1516 for (idx = 0; idx < nefx; idx++)
1517 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1518 spin_unlock_irq(&emu->reg_lock);
1519 return 0;
1520}
1521
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001522static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001524 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 unsigned int nval[2], bits;
1526 int nefx = emu->audigy ? 64 : 32;
1527 int nefxb = emu->audigy ? 7 : 6;
1528 int change, idx;
1529
1530 nval[0] = nval[1] = 0;
1531 for (idx = 0, bits = 0; idx < nefx; idx++)
1532 if (ucontrol->value.integer.value[idx]) {
1533 nval[idx / 32] |= 1 << (idx % 32);
1534 bits++;
1535 }
1536
1537 for (idx = 0; idx < nefxb; idx++)
1538 if (1 << idx == bits)
1539 break;
1540
1541 if (idx >= nefxb)
1542 return -EINVAL;
1543
1544 spin_lock_irq(&emu->reg_lock);
1545 change = (nval[0] != emu->efx_voices_mask[0]) ||
1546 (nval[1] != emu->efx_voices_mask[1]);
1547 emu->efx_voices_mask[0] = nval[0];
1548 emu->efx_voices_mask[1] = nval[1];
1549 spin_unlock_irq(&emu->reg_lock);
1550 return change;
1551}
1552
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001553static struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1555 .name = "Captured FX8010 Outputs",
1556 .info = snd_emu10k1_pcm_efx_voices_mask_info,
1557 .get = snd_emu10k1_pcm_efx_voices_mask_get,
1558 .put = snd_emu10k1_pcm_efx_voices_mask_put
1559};
1560
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001561static struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 .open = snd_emu10k1_capture_efx_open,
1563 .close = snd_emu10k1_capture_efx_close,
1564 .ioctl = snd_pcm_lib_ioctl,
1565 .hw_params = snd_emu10k1_capture_hw_params,
1566 .hw_free = snd_emu10k1_capture_hw_free,
1567 .prepare = snd_emu10k1_capture_prepare,
1568 .trigger = snd_emu10k1_capture_trigger,
1569 .pointer = snd_emu10k1_capture_pointer,
1570};
1571
1572
1573/* EFX playback */
1574
1575#define INITIAL_TRAM_SHIFT 14
1576#define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1577
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001578static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001580 struct snd_pcm_substream *substream = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 snd_pcm_period_elapsed(substream);
1582}
1583
1584static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1585 unsigned short *dst_right,
1586 unsigned short *src,
1587 unsigned int count,
1588 unsigned int tram_shift)
1589{
Takashi Iwai28a97c12009-02-05 16:08:14 +01001590 /*
1591 printk(KERN_DEBUG "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);
1594 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if ((tram_shift & 1) == 0) {
1596 while (count--) {
1597 *dst_left-- = *src++;
1598 *dst_right-- = *src++;
1599 }
1600 } else {
1601 while (count--) {
1602 *dst_right-- = *src++;
1603 *dst_left-- = *src++;
1604 }
1605 }
1606}
1607
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001608static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1609 struct snd_pcm_indirect *rec, size_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001611 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1612 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 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;
1618
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++;
1628 }
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;
1635}
1636
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001637static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001639 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1640 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy);
1643 return 0;
1644}
1645
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001646static int snd_emu10k1_fx8010_playback_hw_params(struct snd_pcm_substream *substream,
1647 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1650}
1651
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001652static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001654 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1655 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 unsigned int i;
1657
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;
1662}
1663
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001664static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001666 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];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 unsigned int i;
1670
Takashi Iwai28a97c12009-02-05 16:08:14 +01001671 /*
1672 printk(KERN_DEBUG "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);
1676 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 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;
1691}
1692
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001693static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001695 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1696 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 int result = 0;
1698
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:
Takashi Iwai09668b42005-11-17 16:14:10 +01001704 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705#ifdef EMU10K1_SET_AC3_IEC958
1706 {
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);
1714 }
1715 }
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:
Takashi Iwai09668b42005-11-17 16:14:10 +01001725 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL;
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;
1734 }
1735 __err:
1736 spin_unlock(&emu->reg_lock);
1737 return result;
1738}
1739
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001740static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001742 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1743 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 size_t ptr; /* byte pointer */
1745
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);
1750}
1751
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001752static struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
1754 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Takashi Iwai09668b42005-11-17 16:14:10 +01001755 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /* 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),
Takashi Iwai806d31d2009-06-08 15:50:42 +02001766 .periods_min = 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 .periods_max = 1024,
1768 .fifo_size = 0,
1769};
1770
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001771static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001773 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];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
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;
1784 }
1785 pcm->opened = 1;
1786 spin_unlock_irq(&emu->reg_lock);
1787 return 0;
1788}
1789
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001790static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001792 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1793 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 spin_lock_irq(&emu->reg_lock);
1796 pcm->opened = 0;
1797 spin_unlock_irq(&emu->reg_lock);
1798 return 0;
1799}
1800
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001801static struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 .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,
1811};
1812
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001813int __devinit snd_emu10k1_pcm_efx(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001815 struct snd_pcm *pcm;
1816 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 int err;
1818
1819 if (rpcm)
1820 *rpcm = NULL;
1821
1822 if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1823 return err;
1824
1825 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1828 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1829
1830 pcm->info_flags = 0;
1831 strcpy(pcm->name, "Multichannel Capture/PT Playback");
1832 emu->pcm_efx = pcm;
1833 if (rpcm)
1834 *rpcm = pcm;
1835
1836 /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
1837 * to these
1838 */
1839
1840 /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1841 if (emu->audigy) {
1842 emu->efx_voices_mask[0] = 0;
James Courtier-Dutton190d2c42007-11-04 14:08:26 +00001843 if (emu->card_capabilities->emu_model)
Pavel Hofman13d45702007-06-11 12:21:20 +02001844 /* Pavel Hofman - 32 voices will be used for
1845 * capture (write mode) -
1846 * each bit = corresponding voice
1847 */
1848 emu->efx_voices_mask[1] = 0xffffffff;
1849 else
1850 emu->efx_voices_mask[1] = 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 } else {
1852 emu->efx_voices_mask[0] = 0xffff0000;
1853 emu->efx_voices_mask[1] = 0;
1854 }
Pavel Hofman13d45702007-06-11 12:21:20 +02001855 /* For emu1010, the control has to set 32 upper bits (voices)
1856 * out of the 64 bits (voices) to true for the 16-channels capture
1857 * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
1858 * is already defined but the snd_emu10k1_pcm_efx_voices_mask
1859 * control can override this register's value.
1860 */
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001861 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1862 if (!kctl)
1863 return -ENOMEM;
1864 kctl->id.device = device;
1865 snd_ctl_add(emu->card, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1868
1869 return 0;
1870}