blob: cf9276ddad42dba9d3c91965f788124cd2d48990 [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
47 printk("IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
48 epcm->substream->runtime->hw->pointer(emu, epcm->substream),
49 snd_pcm_lib_period_bytes(epcm->substream),
50 snd_pcm_lib_buffer_bytes(epcm->substream));
51#endif
52 snd_pcm_period_elapsed(epcm->substream);
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) {
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100149 /* printk("pcm_channel_alloc: failed extra: voices=%d, frame=%d\n", voices, frame); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 for (i = 0; i < voices; i++) {
151 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
152 epcm->voices[i] = NULL;
153 }
154 return err;
155 }
156 epcm->extra->epcm = epcm;
157 epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
158 }
159 return 0;
160}
161
162static unsigned int capture_period_sizes[31] = {
163 384, 448, 512, 640,
164 384*2, 448*2, 512*2, 640*2,
165 384*4, 448*4, 512*4, 640*4,
166 384*8, 448*8, 512*8, 640*8,
167 384*16, 448*16, 512*16, 640*16,
168 384*32, 448*32, 512*32, 640*32,
169 384*64, 448*64, 512*64, 640*64,
170 384*128,448*128,512*128
171};
172
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100173static struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 .count = 31,
175 .list = capture_period_sizes,
176 .mask = 0
177};
178
179static unsigned int capture_rates[8] = {
180 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
181};
182
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100183static struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 .count = 8,
185 .list = capture_rates,
186 .mask = 0
187};
188
189static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
190{
191 switch (rate) {
192 case 8000: return ADCCR_SAMPLERATE_8;
193 case 11025: return ADCCR_SAMPLERATE_11;
194 case 16000: return ADCCR_SAMPLERATE_16;
195 case 22050: return ADCCR_SAMPLERATE_22;
196 case 24000: return ADCCR_SAMPLERATE_24;
197 case 32000: return ADCCR_SAMPLERATE_32;
198 case 44100: return ADCCR_SAMPLERATE_44;
199 case 48000: return ADCCR_SAMPLERATE_48;
200 default:
201 snd_BUG();
202 return ADCCR_SAMPLERATE_8;
203 }
204}
205
206static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
207{
208 switch (rate) {
209 case 8000: return A_ADCCR_SAMPLERATE_8;
210 case 11025: return A_ADCCR_SAMPLERATE_11;
211 case 12000: return A_ADCCR_SAMPLERATE_12; /* really supported? */
212 case 16000: return ADCCR_SAMPLERATE_16;
213 case 22050: return ADCCR_SAMPLERATE_22;
214 case 24000: return ADCCR_SAMPLERATE_24;
215 case 32000: return ADCCR_SAMPLERATE_32;
216 case 44100: return ADCCR_SAMPLERATE_44;
217 case 48000: return ADCCR_SAMPLERATE_48;
218 default:
219 snd_BUG();
220 return A_ADCCR_SAMPLERATE_8;
221 }
222}
223
224static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
225{
226 unsigned int pitch_target;
227
228 pitch_target = (rate << 8) / 375;
229 pitch_target = (pitch_target >> 1) + (pitch_target & 1);
230 return pitch_target;
231}
232
233#define PITCH_48000 0x00004000
234#define PITCH_96000 0x00008000
235#define PITCH_85000 0x00007155
236#define PITCH_80726 0x00006ba2
237#define PITCH_67882 0x00005a82
238#define PITCH_57081 0x00004c1c
239
240static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
241{
242 if (pitch_target == PITCH_48000)
243 return CCCA_INTERPROM_0;
244 else if (pitch_target < PITCH_48000)
245 return CCCA_INTERPROM_1;
246 else if (pitch_target >= PITCH_96000)
247 return CCCA_INTERPROM_0;
248 else if (pitch_target >= PITCH_85000)
249 return CCCA_INTERPROM_6;
250 else if (pitch_target >= PITCH_80726)
251 return CCCA_INTERPROM_5;
252 else if (pitch_target >= PITCH_67882)
253 return CCCA_INTERPROM_4;
254 else if (pitch_target >= PITCH_57081)
255 return CCCA_INTERPROM_3;
256 else
257 return CCCA_INTERPROM_2;
258}
259
260/*
261 * calculate cache invalidate size
262 *
263 * stereo: channel is stereo
264 * w_16: using 16bit samples
265 *
266 * returns: cache invalidate size in samples
267 */
Jesper Juhlfe5ac9d2005-04-20 10:03:31 +0200268static inline int emu10k1_ccis(int stereo, int w_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 if (w_16) {
271 return stereo ? 24 : 26;
272 } else {
273 return stereo ? 24*2 : 26*2;
274 }
275}
276
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100277static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int master, int extra,
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100279 struct snd_emu10k1_voice *evoice,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 unsigned int start_addr,
281 unsigned int end_addr,
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100282 struct snd_emu10k1_pcm_mixer *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100284 struct snd_pcm_substream *substream = evoice->epcm->substream;
285 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 unsigned int silent_page, tmp;
287 int voice, stereo, w_16;
288 unsigned char attn, send_amount[8];
289 unsigned char send_routing[8];
290 unsigned long flags;
291 unsigned int pitch_target;
292 unsigned int ccis;
293
294 voice = evoice->number;
295 stereo = runtime->channels == 2;
296 w_16 = snd_pcm_format_width(runtime->format) == 16;
297
298 if (!extra && stereo) {
299 start_addr >>= 1;
300 end_addr >>= 1;
301 }
302 if (w_16) {
303 start_addr >>= 1;
304 end_addr >>= 1;
305 }
306
307 spin_lock_irqsave(&emu->reg_lock, flags);
308
309 /* volume parameters */
310 if (extra) {
311 attn = 0;
312 memset(send_routing, 0, sizeof(send_routing));
313 send_routing[0] = 0;
314 send_routing[1] = 1;
315 send_routing[2] = 2;
316 send_routing[3] = 3;
317 memset(send_amount, 0, sizeof(send_amount));
318 } else {
319 /* mono, left, right (master voice = left) */
320 tmp = stereo ? (master ? 1 : 2) : 0;
321 memcpy(send_routing, &mix->send_routing[tmp][0], 8);
322 memcpy(send_amount, &mix->send_volume[tmp][0], 8);
323 }
324
325 ccis = emu10k1_ccis(stereo, w_16);
326
327 if (master) {
328 evoice->epcm->ccca_start_addr = start_addr + ccis;
329 if (extra) {
330 start_addr += ccis;
331 end_addr += ccis;
332 }
333 if (stereo && !extra) {
334 snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK);
335 snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK);
336 } else {
337 snd_emu10k1_ptr_write(emu, CPF, voice, 0);
338 }
339 }
340
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100341 /* setup routing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (emu->audigy) {
343 snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
344 snd_emu10k1_compose_audigy_fxrt1(send_routing));
345 snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
346 snd_emu10k1_compose_audigy_fxrt2(send_routing));
347 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
348 ((unsigned int)send_amount[4] << 24) |
349 ((unsigned int)send_amount[5] << 16) |
350 ((unsigned int)send_amount[6] << 8) |
351 (unsigned int)send_amount[7]);
352 } else
353 snd_emu10k1_ptr_write(emu, FXRT, voice,
354 snd_emu10k1_compose_send_routing(send_routing));
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100355 /* Stop CA */
356 /* Assumption that PT is already 0 so no harm overwriting */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]);
358 snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24));
359 snd_emu10k1_ptr_write(emu, PSST, voice, start_addr | (send_amount[2] << 24));
James Courtier-Dutton190d2c42007-11-04 14:08:26 +0000360 if (emu->card_capabilities->emu_model)
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +0100361 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
362 else
363 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (extra)
365 snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
366 emu10k1_select_interprom(pitch_target) |
367 (w_16 ? 0 : CCCA_8BITSELECT));
368 else
369 snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
370 emu10k1_select_interprom(pitch_target) |
371 (w_16 ? 0 : CCCA_8BITSELECT));
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100372 /* Clear filter delay memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 snd_emu10k1_ptr_write(emu, Z1, voice, 0);
374 snd_emu10k1_ptr_write(emu, Z2, voice, 0);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100375 /* invalidate maps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 silent_page = ((unsigned int)emu->silent_page.addr << 1) | MAP_PTI_MASK;
377 snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
378 snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100379 /* modulation envelope */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
381 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
382 snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
383 snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
384 snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
385 snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
386 snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
387 snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
388 snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
389 snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100390 /* volume envelope */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
392 snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100393 /* filter envelope */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100395 /* pitch envelope */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
397
398 spin_unlock_irqrestore(&emu->reg_lock, flags);
399}
400
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100401static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
402 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100404 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
405 struct snd_pcm_runtime *runtime = substream->runtime;
406 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int err;
408
409 if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0)
410 return err;
411 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
412 return err;
413 if (err > 0) { /* change */
Takashi Iwaie017fa52005-11-17 10:44:51 +0100414 int mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (epcm->memblk != NULL)
416 snd_emu10k1_free_pages(emu, epcm->memblk);
Takashi Iwaie017fa52005-11-17 10:44:51 +0100417 epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
418 epcm->start_addr = 0;
419 if (! epcm->memblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return -ENOMEM;
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100421 mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
Takashi Iwaie017fa52005-11-17 10:44:51 +0100422 if (mapped < 0)
423 return -ENOMEM;
424 epcm->start_addr = mapped << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 return 0;
427}
428
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100429static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100431 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
432 struct snd_pcm_runtime *runtime = substream->runtime;
433 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 if (runtime->private_data == NULL)
436 return 0;
437 epcm = runtime->private_data;
438 if (epcm->extra) {
439 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
440 epcm->extra = NULL;
441 }
442 if (epcm->voices[1]) {
443 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
444 epcm->voices[1] = NULL;
445 }
446 if (epcm->voices[0]) {
447 snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
448 epcm->voices[0] = NULL;
449 }
450 if (epcm->memblk) {
451 snd_emu10k1_free_pages(emu, epcm->memblk);
452 epcm->memblk = NULL;
453 epcm->start_addr = 0;
454 }
455 snd_pcm_lib_free_pages(substream);
456 return 0;
457}
458
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100459static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100461 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
462 struct snd_pcm_runtime *runtime = substream->runtime;
463 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int i;
465
466 if (runtime->private_data == NULL)
467 return 0;
468 epcm = runtime->private_data;
469 if (epcm->extra) {
470 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
471 epcm->extra = NULL;
472 }
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100473 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (epcm->voices[i]) {
475 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
476 epcm->voices[i] = NULL;
477 }
478 }
479 if (epcm->memblk) {
480 snd_emu10k1_free_pages(emu, epcm->memblk);
481 epcm->memblk = NULL;
482 epcm->start_addr = 0;
483 }
484 snd_pcm_lib_free_pages(substream);
485 return 0;
486}
487
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100488static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100490 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
491 struct snd_pcm_runtime *runtime = substream->runtime;
492 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 unsigned int start_addr, end_addr;
494
495 start_addr = epcm->start_addr;
496 end_addr = snd_pcm_lib_period_bytes(substream);
497 if (runtime->channels == 2) {
498 start_addr >>= 1;
499 end_addr >>= 1;
500 }
501 end_addr += start_addr;
502 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
503 start_addr, end_addr, NULL);
504 start_addr = epcm->start_addr;
505 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
506 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
507 start_addr, end_addr,
508 &emu->pcm_mixer[substream->number]);
509 if (epcm->voices[1])
510 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
511 start_addr, end_addr,
512 &emu->pcm_mixer[substream->number]);
513 return 0;
514}
515
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100516static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100518 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
519 struct snd_pcm_runtime *runtime = substream->runtime;
520 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 unsigned int start_addr, end_addr;
522 unsigned int channel_size;
523 int i;
524
525 start_addr = epcm->start_addr;
526 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
527
528 /*
529 * the kX driver leaves some space between voices
530 */
531 channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
532
533 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
534 start_addr, start_addr + (channel_size / 2), NULL);
535
536 /* only difference with the master voice is we use it for the pointer */
537 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
538 start_addr, start_addr + channel_size,
539 &emu->efx_pcm_mixer[0]);
540
541 start_addr += channel_size;
542 for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
543 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
544 start_addr, start_addr + channel_size,
545 &emu->efx_pcm_mixer[i]);
546 start_addr += channel_size;
547 }
548
549 return 0;
550}
551
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100552static struct snd_pcm_hardware snd_emu10k1_efx_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
555 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai09668b42005-11-17 16:14:10 +0100556 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
558 .formats = SNDRV_PCM_FMTBIT_S16_LE,
559 .rates = SNDRV_PCM_RATE_48000,
560 .rate_min = 48000,
561 .rate_max = 48000,
562 .channels_min = NUM_EFX_PLAYBACK,
563 .channels_max = NUM_EFX_PLAYBACK,
564 .buffer_bytes_max = (64*1024),
565 .period_bytes_min = 64,
566 .period_bytes_max = (64*1024),
567 .periods_min = 2,
568 .periods_max = 2,
569 .fifo_size = 0,
570};
571
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100572static int snd_emu10k1_capture_hw_params(struct snd_pcm_substream *substream,
573 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
576}
577
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100578static int snd_emu10k1_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 return snd_pcm_lib_free_pages(substream);
581}
582
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100583static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100585 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
586 struct snd_pcm_runtime *runtime = substream->runtime;
587 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 int idx;
589
590 /* zeroing the buffer size will stop capture */
591 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
592 switch (epcm->type) {
593 case CAPTURE_AC97ADC:
594 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
595 break;
596 case CAPTURE_EFX:
597 if (emu->audigy) {
598 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
599 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
600 } else
601 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
602 break;
603 default:
604 break;
605 }
606 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
607 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
608 epcm->capture_bs_val = 0;
609 for (idx = 0; idx < 31; idx++) {
610 if (capture_period_sizes[idx] == epcm->capture_bufsize) {
611 epcm->capture_bs_val = idx + 1;
612 break;
613 }
614 }
615 if (epcm->capture_bs_val == 0) {
616 snd_BUG();
617 epcm->capture_bs_val++;
618 }
619 if (epcm->type == CAPTURE_AC97ADC) {
620 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
621 if (runtime->channels > 1)
622 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
623 epcm->capture_cr_val |= emu->audigy ?
624 snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
625 snd_emu10k1_capture_rate_reg(runtime->rate);
626 }
627 return 0;
628}
629
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100630static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100632 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
634
635 if (evoice == NULL)
636 return;
637 runtime = evoice->epcm->substream->runtime;
638 voice = evoice->number;
639 stereo = (!extra && runtime->channels == 2);
640 sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
641 ccis = emu10k1_ccis(stereo, sample == 0);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100642 /* set cs to 2 * number of cache registers beside the invalidated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
644 if (cs > 16) cs = 16;
645 for (i = 0; i < cs; i++) {
646 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
647 if (stereo) {
648 snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
649 }
650 }
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100651 /* reset cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
653 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
654 if (stereo) {
655 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
656 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
657 }
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100658 /* fill cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
660 if (stereo) {
661 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
662 }
663}
664
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100665static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int master, int extra,
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100667 struct snd_emu10k1_pcm_mixer *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100669 struct snd_pcm_substream *substream;
670 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 unsigned int attn, vattn;
672 unsigned int voice, tmp;
673
674 if (evoice == NULL) /* skip second voice for mono */
675 return;
676 substream = evoice->epcm->substream;
677 runtime = substream->runtime;
678 voice = evoice->number;
679
680 attn = extra ? 0 : 0x00ff;
681 tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
682 vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
683 snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
684 snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
685 snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
686 snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
687 snd_emu10k1_voice_clear_loop_stop(emu, voice);
688}
689
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100690static 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 -0700691{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100692 struct snd_pcm_substream *substream;
693 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 unsigned int voice, pitch, pitch_target;
695
696 if (evoice == NULL) /* skip second voice for mono */
697 return;
698 substream = evoice->epcm->substream;
699 runtime = substream->runtime;
700 voice = evoice->number;
701
702 pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
James Courtier-Dutton190d2c42007-11-04 14:08:26 +0000703 if (emu->card_capabilities->emu_model)
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +0100704 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
705 else
706 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
708 if (master || evoice->epcm->type == PLAYBACK_EFX)
709 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
710 snd_emu10k1_ptr_write(emu, IP, voice, pitch);
711 if (extra)
712 snd_emu10k1_voice_intr_enable(emu, voice);
713}
714
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100715static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 unsigned int voice;
718
719 if (evoice == NULL)
720 return;
721 voice = evoice->number;
722 snd_emu10k1_voice_intr_disable(emu, voice);
723 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
724 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
725 snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
726 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
727 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
728 snd_emu10k1_ptr_write(emu, IP, voice, 0);
729}
730
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100731static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 int cmd)
733{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100734 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
735 struct snd_pcm_runtime *runtime = substream->runtime;
736 struct snd_emu10k1_pcm *epcm = runtime->private_data;
737 struct snd_emu10k1_pcm_mixer *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 int result = 0;
739
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100740 /* printk("trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n", (int)emu, cmd, substream->ops->pointer(substream)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 spin_lock(&emu->reg_lock);
742 switch (cmd) {
743 case SNDRV_PCM_TRIGGER_START:
744 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */
745 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
746 /* follow thru */
747 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai09668b42005-11-17 16:14:10 +0100748 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 mix = &emu->pcm_mixer[substream->number];
750 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
751 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
752 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
753 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
754 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
755 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
756 epcm->running = 1;
757 break;
758 case SNDRV_PCM_TRIGGER_STOP:
759 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai09668b42005-11-17 16:14:10 +0100760 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 epcm->running = 0;
762 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
763 snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
764 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
765 break;
766 default:
767 result = -EINVAL;
768 break;
769 }
770 spin_unlock(&emu->reg_lock);
771 return result;
772}
773
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100774static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 int cmd)
776{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100777 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
778 struct snd_pcm_runtime *runtime = substream->runtime;
779 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int result = 0;
781
782 spin_lock(&emu->reg_lock);
783 switch (cmd) {
784 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai09668b42005-11-17 16:14:10 +0100785 case SNDRV_PCM_TRIGGER_RESUME:
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100786 /* hmm this should cause full and half full interrupt to be raised? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 outl(epcm->capture_ipr, emu->port + IPR);
788 snd_emu10k1_intr_enable(emu, epcm->capture_inte);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100789 /* printk("adccr = 0x%x, adcbs = 0x%x\n", epcm->adccr, epcm->adcbs); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 switch (epcm->type) {
791 case CAPTURE_AC97ADC:
792 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
793 break;
794 case CAPTURE_EFX:
795 if (emu->audigy) {
796 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
797 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100798 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 -0700799 } else
800 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
801 break;
802 default:
803 break;
804 }
805 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
806 epcm->running = 1;
807 epcm->first_ptr = 1;
808 break;
809 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai09668b42005-11-17 16:14:10 +0100810 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 epcm->running = 0;
812 snd_emu10k1_intr_disable(emu, epcm->capture_inte);
813 outl(epcm->capture_ipr, emu->port + IPR);
814 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
815 switch (epcm->type) {
816 case CAPTURE_AC97ADC:
817 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
818 break;
819 case CAPTURE_EFX:
820 if (emu->audigy) {
821 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
822 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
823 } else
824 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
825 break;
826 default:
827 break;
828 }
829 break;
830 default:
831 result = -EINVAL;
832 }
833 spin_unlock(&emu->reg_lock);
834 return result;
835}
836
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100837static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100839 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
840 struct snd_pcm_runtime *runtime = substream->runtime;
841 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 unsigned int ptr;
843
844 if (!epcm->running)
845 return 0;
846 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
847#if 0 /* Perex's code */
848 ptr += runtime->buffer_size;
849 ptr -= epcm->ccca_start_addr;
850 ptr %= runtime->buffer_size;
851#else /* EMU10K1 Open Source code from Creative */
852 if (ptr < epcm->ccca_start_addr)
853 ptr += runtime->buffer_size - epcm->ccca_start_addr;
854 else {
855 ptr -= epcm->ccca_start_addr;
856 if (ptr >= runtime->buffer_size)
857 ptr -= runtime->buffer_size;
858 }
859#endif
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100860 /* printk("ptr = 0x%x, buffer_size = 0x%x, period_size = 0x%x\n", ptr, runtime->buffer_size, runtime->period_size); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return ptr;
862}
863
864
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100865static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 int cmd)
867{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100868 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
869 struct snd_pcm_runtime *runtime = substream->runtime;
870 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 int i;
872 int result = 0;
873
874 spin_lock(&emu->reg_lock);
875 switch (cmd) {
876 case SNDRV_PCM_TRIGGER_START:
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100877 /* prepare voices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
879 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
880 }
881 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
882
883 /* follow thru */
884 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai09668b42005-11-17 16:14:10 +0100885 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
887 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
888 &emu->efx_pcm_mixer[0]);
889 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
890 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
891 &emu->efx_pcm_mixer[i]);
892 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
893 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
894 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
895 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
896 epcm->running = 1;
897 break;
Takashi Iwai09668b42005-11-17 16:14:10 +0100898 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 case SNDRV_PCM_TRIGGER_STOP:
900 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
901 epcm->running = 0;
902 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
903 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
904 }
905 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
906 break;
907 default:
908 result = -EINVAL;
909 break;
910 }
911 spin_unlock(&emu->reg_lock);
912 return result;
913}
914
915
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100916static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100918 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
919 struct snd_pcm_runtime *runtime = substream->runtime;
920 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 unsigned int ptr;
922
923 if (!epcm->running)
924 return 0;
925 if (epcm->first_ptr) {
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100926 udelay(50); /* hack, it takes awhile until capture is started */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 epcm->first_ptr = 0;
928 }
929 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
930 return bytes_to_frames(runtime, ptr);
931}
932
933/*
934 * Playback support device description
935 */
936
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100937static struct snd_pcm_hardware snd_emu10k1_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
940 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai09668b42005-11-17 16:14:10 +0100941 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
943 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
944 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
945 .rate_min = 4000,
946 .rate_max = 96000,
947 .channels_min = 1,
948 .channels_max = 2,
949 .buffer_bytes_max = (128*1024),
950 .period_bytes_min = 64,
951 .period_bytes_max = (128*1024),
952 .periods_min = 1,
953 .periods_max = 1024,
954 .fifo_size = 0,
955};
956
957/*
958 * Capture support device description
959 */
960
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100961static struct snd_pcm_hardware snd_emu10k1_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
964 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai09668b42005-11-17 16:14:10 +0100965 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 SNDRV_PCM_INFO_MMAP_VALID),
967 .formats = SNDRV_PCM_FMTBIT_S16_LE,
968 .rates = SNDRV_PCM_RATE_8000_48000,
969 .rate_min = 8000,
970 .rate_max = 48000,
971 .channels_min = 1,
972 .channels_max = 2,
973 .buffer_bytes_max = (64*1024),
974 .period_bytes_min = 384,
975 .period_bytes_max = (64*1024),
976 .periods_min = 2,
977 .periods_max = 2,
978 .fifo_size = 0,
979};
980
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100981static struct snd_pcm_hardware snd_emu10k1_capture_efx =
982{
983 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
984 SNDRV_PCM_INFO_BLOCK_TRANSFER |
985 SNDRV_PCM_INFO_RESUME |
986 SNDRV_PCM_INFO_MMAP_VALID),
987 .formats = SNDRV_PCM_FMTBIT_S16_LE,
988 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
989 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
990 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
991 .rate_min = 44100,
992 .rate_max = 192000,
993 .channels_min = 8,
994 .channels_max = 8,
995 .buffer_bytes_max = (64*1024),
996 .period_bytes_min = 384,
997 .period_bytes_max = (64*1024),
998 .periods_min = 2,
999 .periods_max = 2,
1000 .fifo_size = 0,
1001};
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003/*
1004 *
1005 */
1006
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001007static 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 -07001008{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001009 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02001011 if (! kctl)
1012 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (activate)
1014 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1015 else
1016 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1017 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
1018 SNDRV_CTL_EVENT_MASK_INFO,
1019 snd_ctl_build_ioff(&id, kctl, idx));
1020}
1021
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001022static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
1025 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
1026 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
1027}
1028
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001029static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
1032 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
1033 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
1034}
1035
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001036static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Jesper Juhl4d572772005-05-30 17:30:32 +02001038 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001041static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001043 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1044 struct snd_emu10k1_pcm_mixer *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 int i;
1046
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001047 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 mix = &emu->efx_pcm_mixer[i];
1049 mix->epcm = NULL;
1050 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1051 }
1052 return 0;
1053}
1054
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001055static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001057 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1058 struct snd_emu10k1_pcm *epcm;
1059 struct snd_emu10k1_pcm_mixer *mix;
1060 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 int i;
1062
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001063 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (epcm == NULL)
1065 return -ENOMEM;
1066 epcm->emu = emu;
1067 epcm->type = PLAYBACK_EFX;
1068 epcm->substream = substream;
1069
1070 emu->pcm_playback_efx_substream = substream;
1071
1072 runtime->private_data = epcm;
1073 runtime->private_free = snd_emu10k1_pcm_free_substream;
1074 runtime->hw = snd_emu10k1_efx_playback;
1075
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001076 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 mix = &emu->efx_pcm_mixer[i];
1078 mix->send_routing[0][0] = i;
1079 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1080 mix->send_volume[0][0] = 255;
1081 mix->attn[0] = 0xffff;
1082 mix->epcm = epcm;
1083 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1084 }
1085 return 0;
1086}
1087
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001088static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001090 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1091 struct snd_emu10k1_pcm *epcm;
1092 struct snd_emu10k1_pcm_mixer *mix;
1093 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 int i, err;
1095
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001096 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (epcm == NULL)
1098 return -ENOMEM;
1099 epcm->emu = emu;
1100 epcm->type = PLAYBACK_EMUVOICE;
1101 epcm->substream = substream;
1102 runtime->private_data = epcm;
1103 runtime->private_free = snd_emu10k1_pcm_free_substream;
1104 runtime->hw = snd_emu10k1_playback;
1105 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
1106 kfree(epcm);
1107 return err;
1108 }
1109 if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
1110 kfree(epcm);
1111 return err;
1112 }
1113 mix = &emu->pcm_mixer[substream->number];
1114 for (i = 0; i < 4; i++)
1115 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1116 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1117 mix->send_volume[0][0] = mix->send_volume[0][1] =
1118 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1119 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1120 mix->epcm = epcm;
1121 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1122 return 0;
1123}
1124
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001125static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001127 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1128 struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 mix->epcm = NULL;
1131 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1132 return 0;
1133}
1134
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001135static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001137 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1138 struct snd_pcm_runtime *runtime = substream->runtime;
1139 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001141 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (epcm == NULL)
1143 return -ENOMEM;
1144 epcm->emu = emu;
1145 epcm->type = CAPTURE_AC97ADC;
1146 epcm->substream = substream;
1147 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1148 epcm->capture_inte = INTE_ADCBUFENABLE;
1149 epcm->capture_ba_reg = ADCBA;
1150 epcm->capture_bs_reg = ADCBS;
1151 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1152 runtime->private_data = epcm;
1153 runtime->private_free = snd_emu10k1_pcm_free_substream;
1154 runtime->hw = snd_emu10k1_capture;
1155 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1156 emu->pcm_capture_substream = substream;
1157 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1158 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1159 return 0;
1160}
1161
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001162static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001164 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 emu->capture_interrupt = NULL;
1167 emu->pcm_capture_substream = NULL;
1168 return 0;
1169}
1170
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001171static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001173 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1174 struct snd_emu10k1_pcm *epcm;
1175 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001177 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (epcm == NULL)
1179 return -ENOMEM;
1180 epcm->emu = emu;
1181 epcm->type = CAPTURE_AC97MIC;
1182 epcm->substream = substream;
1183 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1184 epcm->capture_inte = INTE_MICBUFENABLE;
1185 epcm->capture_ba_reg = MICBA;
1186 epcm->capture_bs_reg = MICBS;
1187 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1188 substream->runtime->private_data = epcm;
1189 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1190 runtime->hw = snd_emu10k1_capture;
1191 runtime->hw.rates = SNDRV_PCM_RATE_8000;
1192 runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1193 runtime->hw.channels_min = 1;
1194 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1195 emu->pcm_capture_mic_substream = substream;
1196 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1197 return 0;
1198}
1199
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001200static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001202 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 emu->capture_interrupt = NULL;
1205 emu->pcm_capture_mic_substream = NULL;
1206 return 0;
1207}
1208
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001209static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001211 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1212 struct snd_emu10k1_pcm *epcm;
1213 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int nefx = emu->audigy ? 64 : 32;
1215 int idx;
1216
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001217 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (epcm == NULL)
1219 return -ENOMEM;
1220 epcm->emu = emu;
1221 epcm->type = CAPTURE_EFX;
1222 epcm->substream = substream;
1223 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1224 epcm->capture_inte = INTE_EFXBUFENABLE;
1225 epcm->capture_ba_reg = FXBA;
1226 epcm->capture_bs_reg = FXBS;
1227 epcm->capture_idx_reg = FXIDX;
1228 substream->runtime->private_data = epcm;
1229 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001230 runtime->hw = snd_emu10k1_capture_efx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1232 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1233 spin_lock_irq(&emu->reg_lock);
James Courtier-Dutton190d2c42007-11-04 14:08:26 +00001234 if (emu->card_capabilities->emu_model) {
Pavel Hofman13d45702007-06-11 12:21:20 +02001235 /* Nb. of channels has been increased to 16 */
1236 /* TODO
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001237 * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
1238 * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1239 * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1240 * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
1241 * rate_min = 44100,
1242 * rate_max = 192000,
Pavel Hofman13d45702007-06-11 12:21:20 +02001243 * channels_min = 16,
1244 * channels_max = 16,
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001245 * Need to add mixer control to fix sample rate
1246 *
Pavel Hofman13d45702007-06-11 12:21:20 +02001247 * There are 32 mono channels of 16bits each.
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001248 * 24bit Audio uses 2x channels over 16bit
1249 * 96kHz uses 2x channels over 48kHz
1250 * 192kHz uses 4x channels over 48kHz
Pavel Hofman13d45702007-06-11 12:21:20 +02001251 * So, for 48kHz 24bit, one has 16 channels
1252 * for 96kHz 24bit, one has 8 channels
1253 * for 192kHz 24bit, one has 4 channels
1254 *
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001255 */
1256#if 1
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +01001257 switch (emu->emu1010.internal_clock) {
1258 case 0:
1259 /* For 44.1kHz */
1260 runtime->hw.rates = SNDRV_PCM_RATE_44100;
1261 runtime->hw.rate_min = runtime->hw.rate_max = 44100;
Pavel Hofman13d45702007-06-11 12:21:20 +02001262 runtime->hw.channels_min =
1263 runtime->hw.channels_max = 16;
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +01001264 break;
1265 case 1:
1266 /* For 48kHz */
1267 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1268 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
Pavel Hofman13d45702007-06-11 12:21:20 +02001269 runtime->hw.channels_min =
1270 runtime->hw.channels_max = 16;
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +01001271 break;
1272 };
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001273#endif
1274#if 0
1275 /* For 96kHz */
1276 runtime->hw.rates = SNDRV_PCM_RATE_96000;
1277 runtime->hw.rate_min = runtime->hw.rate_max = 96000;
1278 runtime->hw.channels_min = runtime->hw.channels_max = 4;
1279#endif
1280#if 0
1281 /* For 192kHz */
1282 runtime->hw.rates = SNDRV_PCM_RATE_192000;
1283 runtime->hw.rate_min = runtime->hw.rate_max = 192000;
1284 runtime->hw.channels_min = runtime->hw.channels_max = 2;
1285#endif
1286 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1287 /* efx_voices_mask[0] is expected to be zero
Pavel Hofman13d45702007-06-11 12:21:20 +02001288 * efx_voices_mask[1] is expected to have 32bits set
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001289 */
1290 } else {
1291 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1292 for (idx = 0; idx < nefx; idx++) {
1293 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1294 runtime->hw.channels_min++;
1295 runtime->hw.channels_max++;
1296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298 }
1299 epcm->capture_cr_val = emu->efx_voices_mask[0];
1300 epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1301 spin_unlock_irq(&emu->reg_lock);
1302 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1303 emu->pcm_capture_efx_substream = substream;
1304 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1305 return 0;
1306}
1307
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001308static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001310 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 emu->capture_interrupt = NULL;
1313 emu->pcm_capture_efx_substream = NULL;
1314 return 0;
1315}
1316
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001317static struct snd_pcm_ops snd_emu10k1_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 .open = snd_emu10k1_playback_open,
1319 .close = snd_emu10k1_playback_close,
1320 .ioctl = snd_pcm_lib_ioctl,
1321 .hw_params = snd_emu10k1_playback_hw_params,
1322 .hw_free = snd_emu10k1_playback_hw_free,
1323 .prepare = snd_emu10k1_playback_prepare,
1324 .trigger = snd_emu10k1_playback_trigger,
1325 .pointer = snd_emu10k1_playback_pointer,
1326 .page = snd_pcm_sgbuf_ops_page,
1327};
1328
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001329static struct snd_pcm_ops snd_emu10k1_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 .open = snd_emu10k1_capture_open,
1331 .close = snd_emu10k1_capture_close,
1332 .ioctl = snd_pcm_lib_ioctl,
1333 .hw_params = snd_emu10k1_capture_hw_params,
1334 .hw_free = snd_emu10k1_capture_hw_free,
1335 .prepare = snd_emu10k1_capture_prepare,
1336 .trigger = snd_emu10k1_capture_trigger,
1337 .pointer = snd_emu10k1_capture_pointer,
1338};
1339
1340/* EFX playback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001341static struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 .open = snd_emu10k1_efx_playback_open,
1343 .close = snd_emu10k1_efx_playback_close,
1344 .ioctl = snd_pcm_lib_ioctl,
1345 .hw_params = snd_emu10k1_playback_hw_params,
1346 .hw_free = snd_emu10k1_efx_playback_hw_free,
1347 .prepare = snd_emu10k1_efx_playback_prepare,
1348 .trigger = snd_emu10k1_efx_playback_trigger,
1349 .pointer = snd_emu10k1_efx_playback_pointer,
1350 .page = snd_pcm_sgbuf_ops_page,
1351};
1352
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001353int __devinit snd_emu10k1_pcm(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001355 struct snd_pcm *pcm;
1356 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 int err;
1358
1359 if (rpcm)
1360 *rpcm = NULL;
1361
1362 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1363 return err;
1364
1365 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1368 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1369
1370 pcm->info_flags = 0;
1371 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1372 strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1373 emu->pcm = pcm;
1374
1375 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1376 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1377 return err;
1378
1379 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1380 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1381
1382 if (rpcm)
1383 *rpcm = pcm;
1384
1385 return 0;
1386}
1387
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001388int __devinit snd_emu10k1_pcm_multi(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001390 struct snd_pcm *pcm;
1391 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 int err;
1393
1394 if (rpcm)
1395 *rpcm = NULL;
1396
1397 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
1398 return err;
1399
1400 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1403
1404 pcm->info_flags = 0;
1405 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1406 strcpy(pcm->name, "Multichannel Playback");
Takashi Iwai09668b42005-11-17 16:14:10 +01001407 emu->pcm_multi = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1410 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1411 return err;
1412
1413 if (rpcm)
1414 *rpcm = pcm;
1415
1416 return 0;
1417}
1418
1419
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001420static struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 .open = snd_emu10k1_capture_mic_open,
1422 .close = snd_emu10k1_capture_mic_close,
1423 .ioctl = snd_pcm_lib_ioctl,
1424 .hw_params = snd_emu10k1_capture_hw_params,
1425 .hw_free = snd_emu10k1_capture_hw_free,
1426 .prepare = snd_emu10k1_capture_prepare,
1427 .trigger = snd_emu10k1_capture_trigger,
1428 .pointer = snd_emu10k1_capture_pointer,
1429};
1430
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001431int __devinit snd_emu10k1_pcm_mic(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001433 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 int err;
1435
1436 if (rpcm)
1437 *rpcm = NULL;
1438
1439 if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
1440 return err;
1441
1442 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1445
1446 pcm->info_flags = 0;
1447 strcpy(pcm->name, "Mic Capture");
1448 emu->pcm_mic = pcm;
1449
1450 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1451
1452 if (rpcm)
1453 *rpcm = pcm;
1454 return 0;
1455}
1456
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001457static 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 -07001458{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001459 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 int nefx = emu->audigy ? 64 : 32;
1461 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1462 uinfo->count = nefx;
1463 uinfo->value.integer.min = 0;
1464 uinfo->value.integer.max = 1;
1465 return 0;
1466}
1467
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001468static 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 -07001469{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001470 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 int nefx = emu->audigy ? 64 : 32;
1472 int idx;
1473
1474 spin_lock_irq(&emu->reg_lock);
1475 for (idx = 0; idx < nefx; idx++)
1476 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1477 spin_unlock_irq(&emu->reg_lock);
1478 return 0;
1479}
1480
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001481static 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 -07001482{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001483 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 unsigned int nval[2], bits;
1485 int nefx = emu->audigy ? 64 : 32;
1486 int nefxb = emu->audigy ? 7 : 6;
1487 int change, idx;
1488
1489 nval[0] = nval[1] = 0;
1490 for (idx = 0, bits = 0; idx < nefx; idx++)
1491 if (ucontrol->value.integer.value[idx]) {
1492 nval[idx / 32] |= 1 << (idx % 32);
1493 bits++;
1494 }
1495
1496 for (idx = 0; idx < nefxb; idx++)
1497 if (1 << idx == bits)
1498 break;
1499
1500 if (idx >= nefxb)
1501 return -EINVAL;
1502
1503 spin_lock_irq(&emu->reg_lock);
1504 change = (nval[0] != emu->efx_voices_mask[0]) ||
1505 (nval[1] != emu->efx_voices_mask[1]);
1506 emu->efx_voices_mask[0] = nval[0];
1507 emu->efx_voices_mask[1] = nval[1];
1508 spin_unlock_irq(&emu->reg_lock);
1509 return change;
1510}
1511
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001512static struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1514 .name = "Captured FX8010 Outputs",
1515 .info = snd_emu10k1_pcm_efx_voices_mask_info,
1516 .get = snd_emu10k1_pcm_efx_voices_mask_get,
1517 .put = snd_emu10k1_pcm_efx_voices_mask_put
1518};
1519
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001520static struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 .open = snd_emu10k1_capture_efx_open,
1522 .close = snd_emu10k1_capture_efx_close,
1523 .ioctl = snd_pcm_lib_ioctl,
1524 .hw_params = snd_emu10k1_capture_hw_params,
1525 .hw_free = snd_emu10k1_capture_hw_free,
1526 .prepare = snd_emu10k1_capture_prepare,
1527 .trigger = snd_emu10k1_capture_trigger,
1528 .pointer = snd_emu10k1_capture_pointer,
1529};
1530
1531
1532/* EFX playback */
1533
1534#define INITIAL_TRAM_SHIFT 14
1535#define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1536
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001537static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001539 struct snd_pcm_substream *substream = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 snd_pcm_period_elapsed(substream);
1541}
1542
1543static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1544 unsigned short *dst_right,
1545 unsigned short *src,
1546 unsigned int count,
1547 unsigned int tram_shift)
1548{
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001549 /* printk("tram_poke1: dst_left = 0x%p, dst_right = 0x%p, src = 0x%p, count = 0x%x\n", dst_left, dst_right, src, count); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if ((tram_shift & 1) == 0) {
1551 while (count--) {
1552 *dst_left-- = *src++;
1553 *dst_right-- = *src++;
1554 }
1555 } else {
1556 while (count--) {
1557 *dst_right-- = *src++;
1558 *dst_left-- = *src++;
1559 }
1560 }
1561}
1562
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001563static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1564 struct snd_pcm_indirect *rec, size_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001566 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1567 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 unsigned int tram_size = pcm->buffer_size;
1569 unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1570 unsigned int frames = bytes >> 2, count;
1571 unsigned int tram_pos = pcm->tram_pos;
1572 unsigned int tram_shift = pcm->tram_shift;
1573
1574 while (frames > tram_pos) {
1575 count = tram_pos + 1;
1576 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1577 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1578 src, count, tram_shift);
1579 src += count * 2;
1580 frames -= count;
1581 tram_pos = (tram_size / 2) - 1;
1582 tram_shift++;
1583 }
1584 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1585 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1586 src, frames, tram_shift);
1587 tram_pos -= frames;
1588 pcm->tram_pos = tram_pos;
1589 pcm->tram_shift = tram_shift;
1590}
1591
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001592static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001594 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1595 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy);
1598 return 0;
1599}
1600
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001601static int snd_emu10k1_fx8010_playback_hw_params(struct snd_pcm_substream *substream,
1602 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1605}
1606
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001607static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001609 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1610 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 unsigned int i;
1612
1613 for (i = 0; i < pcm->channels; i++)
1614 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1615 snd_pcm_lib_free_pages(substream);
1616 return 0;
1617}
1618
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001619static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001621 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1622 struct snd_pcm_runtime *runtime = substream->runtime;
1623 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 unsigned int i;
1625
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001626 /* printk("prepare: etram_pages = 0x%p, dma_area = 0x%x, buffer_size = 0x%x (0x%x)\n", emu->fx8010.etram_pages, runtime->dma_area, runtime->buffer_size, runtime->buffer_size << 2); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1628 pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1629 pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1630 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1631 pcm->tram_shift = 0;
1632 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */
1633 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */
1634 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1635 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */
1636 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1637 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1638 for (i = 0; i < pcm->channels; i++)
1639 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1640 return 0;
1641}
1642
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001643static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001645 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1646 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 int result = 0;
1648
1649 spin_lock(&emu->reg_lock);
1650 switch (cmd) {
1651 case SNDRV_PCM_TRIGGER_START:
1652 /* follow thru */
1653 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai09668b42005-11-17 16:14:10 +01001654 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655#ifdef EMU10K1_SET_AC3_IEC958
1656 {
1657 int i;
1658 for (i = 0; i < 3; i++) {
1659 unsigned int bits;
1660 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1661 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1662 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1663 snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1664 }
1665 }
1666#endif
1667 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1668 if (result < 0)
1669 goto __err;
1670 snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
1671 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1672 break;
1673 case SNDRV_PCM_TRIGGER_STOP:
1674 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai09668b42005-11-17 16:14:10 +01001675 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL;
1677 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1678 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1679 pcm->tram_shift = 0;
1680 break;
1681 default:
1682 result = -EINVAL;
1683 break;
1684 }
1685 __err:
1686 spin_unlock(&emu->reg_lock);
1687 return result;
1688}
1689
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001690static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001692 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1693 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 size_t ptr; /* byte pointer */
1695
1696 if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1697 return 0;
1698 ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1699 return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1700}
1701
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001702static struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Takashi Iwai09668b42005-11-17 16:14:10 +01001705 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE),
1707 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1708 .rates = SNDRV_PCM_RATE_48000,
1709 .rate_min = 48000,
1710 .rate_max = 48000,
1711 .channels_min = 1,
1712 .channels_max = 1,
1713 .buffer_bytes_max = (128*1024),
1714 .period_bytes_min = 1024,
1715 .period_bytes_max = (128*1024),
1716 .periods_min = 1,
1717 .periods_max = 1024,
1718 .fifo_size = 0,
1719};
1720
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001721static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001723 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1724 struct snd_pcm_runtime *runtime = substream->runtime;
1725 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 runtime->hw = snd_emu10k1_fx8010_playback;
1728 runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1729 runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1730 spin_lock_irq(&emu->reg_lock);
1731 if (pcm->valid == 0) {
1732 spin_unlock_irq(&emu->reg_lock);
1733 return -ENODEV;
1734 }
1735 pcm->opened = 1;
1736 spin_unlock_irq(&emu->reg_lock);
1737 return 0;
1738}
1739
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001740static int snd_emu10k1_fx8010_playback_close(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
1745 spin_lock_irq(&emu->reg_lock);
1746 pcm->opened = 0;
1747 spin_unlock_irq(&emu->reg_lock);
1748 return 0;
1749}
1750
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001751static struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 .open = snd_emu10k1_fx8010_playback_open,
1753 .close = snd_emu10k1_fx8010_playback_close,
1754 .ioctl = snd_pcm_lib_ioctl,
1755 .hw_params = snd_emu10k1_fx8010_playback_hw_params,
1756 .hw_free = snd_emu10k1_fx8010_playback_hw_free,
1757 .prepare = snd_emu10k1_fx8010_playback_prepare,
1758 .trigger = snd_emu10k1_fx8010_playback_trigger,
1759 .pointer = snd_emu10k1_fx8010_playback_pointer,
1760 .ack = snd_emu10k1_fx8010_playback_transfer,
1761};
1762
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001763int __devinit snd_emu10k1_pcm_efx(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001765 struct snd_pcm *pcm;
1766 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 int err;
1768
1769 if (rpcm)
1770 *rpcm = NULL;
1771
1772 if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1773 return err;
1774
1775 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1778 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1779
1780 pcm->info_flags = 0;
1781 strcpy(pcm->name, "Multichannel Capture/PT Playback");
1782 emu->pcm_efx = pcm;
1783 if (rpcm)
1784 *rpcm = pcm;
1785
1786 /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
1787 * to these
1788 */
1789
1790 /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1791 if (emu->audigy) {
1792 emu->efx_voices_mask[0] = 0;
James Courtier-Dutton190d2c42007-11-04 14:08:26 +00001793 if (emu->card_capabilities->emu_model)
Pavel Hofman13d45702007-06-11 12:21:20 +02001794 /* Pavel Hofman - 32 voices will be used for
1795 * capture (write mode) -
1796 * each bit = corresponding voice
1797 */
1798 emu->efx_voices_mask[1] = 0xffffffff;
1799 else
1800 emu->efx_voices_mask[1] = 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 } else {
1802 emu->efx_voices_mask[0] = 0xffff0000;
1803 emu->efx_voices_mask[1] = 0;
1804 }
Pavel Hofman13d45702007-06-11 12:21:20 +02001805 /* For emu1010, the control has to set 32 upper bits (voices)
1806 * out of the 64 bits (voices) to true for the 16-channels capture
1807 * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
1808 * is already defined but the snd_emu10k1_pcm_efx_voices_mask
1809 * control can override this register's value.
1810 */
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001811 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1812 if (!kctl)
1813 return -ENOMEM;
1814 kctl->id.device = device;
1815 snd_ctl_add(emu->card, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1818
1819 return 0;
1820}