blob: 0dc07385af0ebaee5ea66126bf8d758e17f71054 [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 Iwai6f002b02014-02-25 17:02:09 +010047 dev_dbg(emu->card->dev,
48 "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 epcm->substream->runtime->hw->pointer(emu, epcm->substream),
50 snd_pcm_lib_period_bytes(epcm->substream),
51 snd_pcm_lib_buffer_bytes(epcm->substream));
52#endif
53 snd_pcm_period_elapsed(epcm->substream);
54}
55
Takashi Iwaieb4698f2005-11-17 14:50:13 +010056static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu,
57 unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59#if 0
60 if (status & IPR_ADCBUFHALFFULL) {
61 if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
62 return;
63 }
64#endif
65 snd_pcm_period_elapsed(emu->pcm_capture_substream);
66}
67
Takashi Iwaieb4698f2005-11-17 14:50:13 +010068static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu,
69 unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71#if 0
72 if (status & IPR_MICBUFHALFFULL) {
73 if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
74 return;
75 }
76#endif
77 snd_pcm_period_elapsed(emu->pcm_capture_mic_substream);
78}
79
Takashi Iwaieb4698f2005-11-17 14:50:13 +010080static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu,
81 unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83#if 0
84 if (status & IPR_EFXBUFHALFFULL) {
85 if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
86 return;
87 }
88#endif
89 snd_pcm_period_elapsed(emu->pcm_capture_efx_substream);
90}
91
Takashi Iwaieb4698f2005-11-17 14:50:13 +010092static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Takashi Iwaieb4698f2005-11-17 14:50:13 +010094 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
95 struct snd_pcm_runtime *runtime = substream->runtime;
96 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 unsigned int ptr;
98
99 if (!epcm->running)
100 return 0;
101 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
102 ptr += runtime->buffer_size;
103 ptr -= epcm->ccca_start_addr;
104 ptr %= runtime->buffer_size;
105
106 return ptr;
107}
108
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100109static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm, int voices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 int err, i;
112
113 if (epcm->voices[1] != NULL && voices < 2) {
114 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
115 epcm->voices[1] = NULL;
116 }
117 for (i = 0; i < voices; i++) {
118 if (epcm->voices[i] == NULL)
119 break;
120 }
121 if (i == voices)
122 return 0; /* already allocated */
123
124 for (i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
125 if (epcm->voices[i]) {
126 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
127 epcm->voices[i] = NULL;
128 }
129 }
130 err = snd_emu10k1_voice_alloc(epcm->emu,
131 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
132 voices,
133 &epcm->voices[0]);
134
135 if (err < 0)
136 return err;
137 epcm->voices[0]->epcm = epcm;
138 if (voices > 1) {
139 for (i = 1; i < voices; i++) {
140 epcm->voices[i] = &epcm->emu->voices[epcm->voices[0]->number + i];
141 epcm->voices[i]->epcm = epcm;
142 }
143 }
144 if (epcm->extra == NULL) {
145 err = snd_emu10k1_voice_alloc(epcm->emu,
146 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
147 1,
148 &epcm->extra);
149 if (err < 0) {
Takashi Iwai28a97c12009-02-05 16:08:14 +0100150 /*
Takashi Iwai6f002b02014-02-25 17:02:09 +0100151 dev_dbg(emu->card->dev, "pcm_channel_alloc: "
Takashi Iwai28a97c12009-02-05 16:08:14 +0100152 "failed extra: voices=%d, frame=%d\n",
153 voices, frame);
154 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 for (i = 0; i < voices; i++) {
156 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
157 epcm->voices[i] = NULL;
158 }
159 return err;
160 }
161 epcm->extra->epcm = epcm;
162 epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
163 }
164 return 0;
165}
166
167static unsigned int capture_period_sizes[31] = {
168 384, 448, 512, 640,
169 384*2, 448*2, 512*2, 640*2,
170 384*4, 448*4, 512*4, 640*4,
171 384*8, 448*8, 512*8, 640*8,
172 384*16, 448*16, 512*16, 640*16,
173 384*32, 448*32, 512*32, 640*32,
174 384*64, 448*64, 512*64, 640*64,
175 384*128,448*128,512*128
176};
177
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100178static struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .count = 31,
180 .list = capture_period_sizes,
181 .mask = 0
182};
183
184static unsigned int capture_rates[8] = {
185 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
186};
187
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100188static struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 .count = 8,
190 .list = capture_rates,
191 .mask = 0
192};
193
194static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
195{
196 switch (rate) {
197 case 8000: return ADCCR_SAMPLERATE_8;
198 case 11025: return ADCCR_SAMPLERATE_11;
199 case 16000: return ADCCR_SAMPLERATE_16;
200 case 22050: return ADCCR_SAMPLERATE_22;
201 case 24000: return ADCCR_SAMPLERATE_24;
202 case 32000: return ADCCR_SAMPLERATE_32;
203 case 44100: return ADCCR_SAMPLERATE_44;
204 case 48000: return ADCCR_SAMPLERATE_48;
205 default:
206 snd_BUG();
207 return ADCCR_SAMPLERATE_8;
208 }
209}
210
211static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
212{
213 switch (rate) {
214 case 8000: return A_ADCCR_SAMPLERATE_8;
215 case 11025: return A_ADCCR_SAMPLERATE_11;
216 case 12000: return A_ADCCR_SAMPLERATE_12; /* really supported? */
217 case 16000: return ADCCR_SAMPLERATE_16;
218 case 22050: return ADCCR_SAMPLERATE_22;
219 case 24000: return ADCCR_SAMPLERATE_24;
220 case 32000: return ADCCR_SAMPLERATE_32;
221 case 44100: return ADCCR_SAMPLERATE_44;
222 case 48000: return ADCCR_SAMPLERATE_48;
223 default:
224 snd_BUG();
225 return A_ADCCR_SAMPLERATE_8;
226 }
227}
228
229static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
230{
231 unsigned int pitch_target;
232
233 pitch_target = (rate << 8) / 375;
234 pitch_target = (pitch_target >> 1) + (pitch_target & 1);
235 return pitch_target;
236}
237
238#define PITCH_48000 0x00004000
239#define PITCH_96000 0x00008000
240#define PITCH_85000 0x00007155
241#define PITCH_80726 0x00006ba2
242#define PITCH_67882 0x00005a82
243#define PITCH_57081 0x00004c1c
244
245static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
246{
247 if (pitch_target == PITCH_48000)
248 return CCCA_INTERPROM_0;
249 else if (pitch_target < PITCH_48000)
250 return CCCA_INTERPROM_1;
251 else if (pitch_target >= PITCH_96000)
252 return CCCA_INTERPROM_0;
253 else if (pitch_target >= PITCH_85000)
254 return CCCA_INTERPROM_6;
255 else if (pitch_target >= PITCH_80726)
256 return CCCA_INTERPROM_5;
257 else if (pitch_target >= PITCH_67882)
258 return CCCA_INTERPROM_4;
259 else if (pitch_target >= PITCH_57081)
260 return CCCA_INTERPROM_3;
261 else
262 return CCCA_INTERPROM_2;
263}
264
265/*
266 * calculate cache invalidate size
267 *
268 * stereo: channel is stereo
269 * w_16: using 16bit samples
270 *
271 * returns: cache invalidate size in samples
272 */
Jesper Juhlfe5ac9d2005-04-20 10:03:31 +0200273static inline int emu10k1_ccis(int stereo, int w_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 if (w_16) {
276 return stereo ? 24 : 26;
277 } else {
278 return stereo ? 24*2 : 26*2;
279 }
280}
281
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100282static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 int master, int extra,
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100284 struct snd_emu10k1_voice *evoice,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 unsigned int start_addr,
286 unsigned int end_addr,
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100287 struct snd_emu10k1_pcm_mixer *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100289 struct snd_pcm_substream *substream = evoice->epcm->substream;
290 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 unsigned int silent_page, tmp;
292 int voice, stereo, w_16;
293 unsigned char attn, send_amount[8];
294 unsigned char send_routing[8];
295 unsigned long flags;
296 unsigned int pitch_target;
297 unsigned int ccis;
298
299 voice = evoice->number;
300 stereo = runtime->channels == 2;
301 w_16 = snd_pcm_format_width(runtime->format) == 16;
302
303 if (!extra && stereo) {
304 start_addr >>= 1;
305 end_addr >>= 1;
306 }
307 if (w_16) {
308 start_addr >>= 1;
309 end_addr >>= 1;
310 }
311
312 spin_lock_irqsave(&emu->reg_lock, flags);
313
314 /* volume parameters */
315 if (extra) {
316 attn = 0;
317 memset(send_routing, 0, sizeof(send_routing));
318 send_routing[0] = 0;
319 send_routing[1] = 1;
320 send_routing[2] = 2;
321 send_routing[3] = 3;
322 memset(send_amount, 0, sizeof(send_amount));
323 } else {
324 /* mono, left, right (master voice = left) */
325 tmp = stereo ? (master ? 1 : 2) : 0;
326 memcpy(send_routing, &mix->send_routing[tmp][0], 8);
327 memcpy(send_amount, &mix->send_volume[tmp][0], 8);
328 }
329
330 ccis = emu10k1_ccis(stereo, w_16);
331
332 if (master) {
333 evoice->epcm->ccca_start_addr = start_addr + ccis;
334 if (extra) {
335 start_addr += ccis;
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200336 end_addr += ccis + emu->delay_pcm_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338 if (stereo && !extra) {
339 snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK);
340 snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK);
341 } else {
342 snd_emu10k1_ptr_write(emu, CPF, voice, 0);
343 }
344 }
345
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100346 /* setup routing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (emu->audigy) {
348 snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
349 snd_emu10k1_compose_audigy_fxrt1(send_routing));
350 snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
351 snd_emu10k1_compose_audigy_fxrt2(send_routing));
352 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
353 ((unsigned int)send_amount[4] << 24) |
354 ((unsigned int)send_amount[5] << 16) |
355 ((unsigned int)send_amount[6] << 8) |
356 (unsigned int)send_amount[7]);
357 } else
358 snd_emu10k1_ptr_write(emu, FXRT, voice,
359 snd_emu10k1_compose_send_routing(send_routing));
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100360 /* Stop CA */
361 /* Assumption that PT is already 0 so no harm overwriting */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]);
363 snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24));
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200364 snd_emu10k1_ptr_write(emu, PSST, voice,
365 (start_addr + (extra ? emu->delay_pcm_irq : 0)) |
366 (send_amount[2] << 24));
James Courtier-Dutton190d2c42007-11-04 14:08:26 +0000367 if (emu->card_capabilities->emu_model)
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +0100368 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
369 else
370 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (extra)
372 snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
373 emu10k1_select_interprom(pitch_target) |
374 (w_16 ? 0 : CCCA_8BITSELECT));
375 else
376 snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
377 emu10k1_select_interprom(pitch_target) |
378 (w_16 ? 0 : CCCA_8BITSELECT));
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100379 /* Clear filter delay memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 snd_emu10k1_ptr_write(emu, Z1, voice, 0);
381 snd_emu10k1_ptr_write(emu, Z2, voice, 0);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100382 /* invalidate maps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 silent_page = ((unsigned int)emu->silent_page.addr << 1) | MAP_PTI_MASK;
384 snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
385 snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100386 /* modulation envelope */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
388 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
389 snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
390 snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
391 snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
392 snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
393 snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
394 snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
395 snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
396 snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100397 /* volume envelope */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
399 snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100400 /* filter envelope */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100402 /* pitch envelope */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
404
405 spin_unlock_irqrestore(&emu->reg_lock, flags);
406}
407
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100408static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
409 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100411 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
412 struct snd_pcm_runtime *runtime = substream->runtime;
413 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 int err;
415
416 if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0)
417 return err;
418 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
419 return err;
420 if (err > 0) { /* change */
Takashi Iwaie017fa52005-11-17 10:44:51 +0100421 int mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (epcm->memblk != NULL)
423 snd_emu10k1_free_pages(emu, epcm->memblk);
Takashi Iwaie017fa52005-11-17 10:44:51 +0100424 epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
425 epcm->start_addr = 0;
426 if (! epcm->memblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -ENOMEM;
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100428 mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
Takashi Iwaie017fa52005-11-17 10:44:51 +0100429 if (mapped < 0)
430 return -ENOMEM;
431 epcm->start_addr = mapped << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433 return 0;
434}
435
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100436static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100438 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
439 struct snd_pcm_runtime *runtime = substream->runtime;
440 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 if (runtime->private_data == NULL)
443 return 0;
444 epcm = runtime->private_data;
445 if (epcm->extra) {
446 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
447 epcm->extra = NULL;
448 }
449 if (epcm->voices[1]) {
450 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
451 epcm->voices[1] = NULL;
452 }
453 if (epcm->voices[0]) {
454 snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
455 epcm->voices[0] = NULL;
456 }
457 if (epcm->memblk) {
458 snd_emu10k1_free_pages(emu, epcm->memblk);
459 epcm->memblk = NULL;
460 epcm->start_addr = 0;
461 }
462 snd_pcm_lib_free_pages(substream);
463 return 0;
464}
465
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100466static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100468 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
469 struct snd_pcm_runtime *runtime = substream->runtime;
470 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 int i;
472
473 if (runtime->private_data == NULL)
474 return 0;
475 epcm = runtime->private_data;
476 if (epcm->extra) {
477 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
478 epcm->extra = NULL;
479 }
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100480 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (epcm->voices[i]) {
482 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
483 epcm->voices[i] = NULL;
484 }
485 }
486 if (epcm->memblk) {
487 snd_emu10k1_free_pages(emu, epcm->memblk);
488 epcm->memblk = NULL;
489 epcm->start_addr = 0;
490 }
491 snd_pcm_lib_free_pages(substream);
492 return 0;
493}
494
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100495static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100497 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
498 struct snd_pcm_runtime *runtime = substream->runtime;
499 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 unsigned int start_addr, end_addr;
501
502 start_addr = epcm->start_addr;
503 end_addr = snd_pcm_lib_period_bytes(substream);
504 if (runtime->channels == 2) {
505 start_addr >>= 1;
506 end_addr >>= 1;
507 }
508 end_addr += start_addr;
509 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
510 start_addr, end_addr, NULL);
511 start_addr = epcm->start_addr;
512 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
513 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
514 start_addr, end_addr,
515 &emu->pcm_mixer[substream->number]);
516 if (epcm->voices[1])
517 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
518 start_addr, end_addr,
519 &emu->pcm_mixer[substream->number]);
520 return 0;
521}
522
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100523static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100525 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
526 struct snd_pcm_runtime *runtime = substream->runtime;
527 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 unsigned int start_addr, end_addr;
529 unsigned int channel_size;
530 int i;
531
532 start_addr = epcm->start_addr;
533 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
534
535 /*
536 * the kX driver leaves some space between voices
537 */
538 channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
539
540 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
541 start_addr, start_addr + (channel_size / 2), NULL);
542
543 /* only difference with the master voice is we use it for the pointer */
544 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
545 start_addr, start_addr + channel_size,
546 &emu->efx_pcm_mixer[0]);
547
548 start_addr += channel_size;
549 for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
550 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
551 start_addr, start_addr + channel_size,
552 &emu->efx_pcm_mixer[i]);
553 start_addr += channel_size;
554 }
555
556 return 0;
557}
558
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100559static struct snd_pcm_hardware snd_emu10k1_efx_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
562 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai09668b42005-11-17 16:14:10 +0100563 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
565 .formats = SNDRV_PCM_FMTBIT_S16_LE,
566 .rates = SNDRV_PCM_RATE_48000,
567 .rate_min = 48000,
568 .rate_max = 48000,
569 .channels_min = NUM_EFX_PLAYBACK,
570 .channels_max = NUM_EFX_PLAYBACK,
571 .buffer_bytes_max = (64*1024),
572 .period_bytes_min = 64,
573 .period_bytes_max = (64*1024),
574 .periods_min = 2,
575 .periods_max = 2,
576 .fifo_size = 0,
577};
578
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100579static int snd_emu10k1_capture_hw_params(struct snd_pcm_substream *substream,
580 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
583}
584
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100585static int snd_emu10k1_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 return snd_pcm_lib_free_pages(substream);
588}
589
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100590static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100592 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
593 struct snd_pcm_runtime *runtime = substream->runtime;
594 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int idx;
596
597 /* zeroing the buffer size will stop capture */
598 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
599 switch (epcm->type) {
600 case CAPTURE_AC97ADC:
601 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
602 break;
603 case CAPTURE_EFX:
604 if (emu->audigy) {
605 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
606 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
607 } else
608 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
609 break;
610 default:
611 break;
612 }
613 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
614 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
615 epcm->capture_bs_val = 0;
616 for (idx = 0; idx < 31; idx++) {
617 if (capture_period_sizes[idx] == epcm->capture_bufsize) {
618 epcm->capture_bs_val = idx + 1;
619 break;
620 }
621 }
622 if (epcm->capture_bs_val == 0) {
623 snd_BUG();
624 epcm->capture_bs_val++;
625 }
626 if (epcm->type == CAPTURE_AC97ADC) {
627 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
628 if (runtime->channels > 1)
629 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
630 epcm->capture_cr_val |= emu->audigy ?
631 snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
632 snd_emu10k1_capture_rate_reg(runtime->rate);
633 }
634 return 0;
635}
636
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100637static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100639 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
641
642 if (evoice == NULL)
643 return;
644 runtime = evoice->epcm->substream->runtime;
645 voice = evoice->number;
646 stereo = (!extra && runtime->channels == 2);
647 sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
648 ccis = emu10k1_ccis(stereo, sample == 0);
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100649 /* set cs to 2 * number of cache registers beside the invalidated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
651 if (cs > 16) cs = 16;
652 for (i = 0; i < cs; i++) {
653 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
654 if (stereo) {
655 snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
656 }
657 }
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100658 /* reset cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
660 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
661 if (stereo) {
662 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
663 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
664 }
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100665 /* fill cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
667 if (stereo) {
668 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
669 }
670}
671
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100672static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int master, int extra,
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100674 struct snd_emu10k1_pcm_mixer *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100676 struct snd_pcm_substream *substream;
677 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 unsigned int attn, vattn;
679 unsigned int voice, tmp;
680
681 if (evoice == NULL) /* skip second voice for mono */
682 return;
683 substream = evoice->epcm->substream;
684 runtime = substream->runtime;
685 voice = evoice->number;
686
687 attn = extra ? 0 : 0x00ff;
688 tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
689 vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
690 snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
691 snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
692 snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
693 snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
694 snd_emu10k1_voice_clear_loop_stop(emu, voice);
695}
696
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100697static 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 -0700698{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100699 struct snd_pcm_substream *substream;
700 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 unsigned int voice, pitch, pitch_target;
702
703 if (evoice == NULL) /* skip second voice for mono */
704 return;
705 substream = evoice->epcm->substream;
706 runtime = substream->runtime;
707 voice = evoice->number;
708
709 pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
James Courtier-Dutton190d2c42007-11-04 14:08:26 +0000710 if (emu->card_capabilities->emu_model)
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +0100711 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
712 else
713 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
715 if (master || evoice->epcm->type == PLAYBACK_EFX)
716 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
717 snd_emu10k1_ptr_write(emu, IP, voice, pitch);
718 if (extra)
719 snd_emu10k1_voice_intr_enable(emu, voice);
720}
721
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100722static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 unsigned int voice;
725
726 if (evoice == NULL)
727 return;
728 voice = evoice->number;
729 snd_emu10k1_voice_intr_disable(emu, voice);
730 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
731 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
732 snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
733 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
734 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
735 snd_emu10k1_ptr_write(emu, IP, voice, 0);
736}
737
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200738static inline void snd_emu10k1_playback_mangle_extra(struct snd_emu10k1 *emu,
739 struct snd_emu10k1_pcm *epcm,
740 struct snd_pcm_substream *substream,
741 struct snd_pcm_runtime *runtime)
742{
743 unsigned int ptr, period_pos;
744
745 /* try to sychronize the current position for the interrupt
746 source voice */
747 period_pos = runtime->status->hw_ptr - runtime->hw_ptr_interrupt;
748 period_pos %= runtime->period_size;
749 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->extra->number);
750 ptr &= ~0x00ffffff;
751 ptr |= epcm->ccca_start_addr + period_pos;
752 snd_emu10k1_ptr_write(emu, CCCA, epcm->extra->number, ptr);
753}
754
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100755static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 int cmd)
757{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100758 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
759 struct snd_pcm_runtime *runtime = substream->runtime;
760 struct snd_emu10k1_pcm *epcm = runtime->private_data;
761 struct snd_emu10k1_pcm_mixer *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 int result = 0;
763
Takashi Iwai28a97c12009-02-05 16:08:14 +0100764 /*
Takashi Iwai6f002b02014-02-25 17:02:09 +0100765 dev_dbg(emu->card->dev,
766 "trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n",
Takashi Iwai28a97c12009-02-05 16:08:14 +0100767 (int)emu, cmd, substream->ops->pointer(substream))
768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 spin_lock(&emu->reg_lock);
770 switch (cmd) {
771 case SNDRV_PCM_TRIGGER_START:
772 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */
773 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
774 /* follow thru */
775 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai09668b42005-11-17 16:14:10 +0100776 case SNDRV_PCM_TRIGGER_RESUME:
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200777 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)
778 snd_emu10k1_playback_mangle_extra(emu, epcm, substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 mix = &emu->pcm_mixer[substream->number];
780 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
781 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
782 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
783 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
784 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
785 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
786 epcm->running = 1;
787 break;
788 case SNDRV_PCM_TRIGGER_STOP:
789 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai09668b42005-11-17 16:14:10 +0100790 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 epcm->running = 0;
792 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
793 snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
794 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
795 break;
796 default:
797 result = -EINVAL;
798 break;
799 }
800 spin_unlock(&emu->reg_lock);
801 return result;
802}
803
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100804static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 int cmd)
806{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100807 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
808 struct snd_pcm_runtime *runtime = substream->runtime;
809 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 int result = 0;
811
812 spin_lock(&emu->reg_lock);
813 switch (cmd) {
814 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai09668b42005-11-17 16:14:10 +0100815 case SNDRV_PCM_TRIGGER_RESUME:
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100816 /* hmm this should cause full and half full interrupt to be raised? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 outl(epcm->capture_ipr, emu->port + IPR);
818 snd_emu10k1_intr_enable(emu, epcm->capture_inte);
Takashi Iwai28a97c12009-02-05 16:08:14 +0100819 /*
Takashi Iwai6f002b02014-02-25 17:02:09 +0100820 dev_dbg(emu->card->dev, "adccr = 0x%x, adcbs = 0x%x\n",
Takashi Iwai28a97c12009-02-05 16:08:14 +0100821 epcm->adccr, epcm->adcbs);
822 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 switch (epcm->type) {
824 case CAPTURE_AC97ADC:
825 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
826 break;
827 case CAPTURE_EFX:
828 if (emu->audigy) {
829 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
830 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
Takashi Iwai6f002b02014-02-25 17:02:09 +0100831 dev_dbg(emu->card->dev,
832 "cr_val=0x%x, cr_val2=0x%x\n",
833 epcm->capture_cr_val,
834 epcm->capture_cr_val2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 } else
836 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
837 break;
838 default:
839 break;
840 }
841 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
842 epcm->running = 1;
843 epcm->first_ptr = 1;
844 break;
845 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai09668b42005-11-17 16:14:10 +0100846 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 epcm->running = 0;
848 snd_emu10k1_intr_disable(emu, epcm->capture_inte);
849 outl(epcm->capture_ipr, emu->port + IPR);
850 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
851 switch (epcm->type) {
852 case CAPTURE_AC97ADC:
853 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
854 break;
855 case CAPTURE_EFX:
856 if (emu->audigy) {
857 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
858 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
859 } else
860 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
861 break;
862 default:
863 break;
864 }
865 break;
866 default:
867 result = -EINVAL;
868 }
869 spin_unlock(&emu->reg_lock);
870 return result;
871}
872
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100873static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100875 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
876 struct snd_pcm_runtime *runtime = substream->runtime;
877 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 unsigned int ptr;
879
880 if (!epcm->running)
881 return 0;
882 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
883#if 0 /* Perex's code */
884 ptr += runtime->buffer_size;
885 ptr -= epcm->ccca_start_addr;
886 ptr %= runtime->buffer_size;
887#else /* EMU10K1 Open Source code from Creative */
888 if (ptr < epcm->ccca_start_addr)
889 ptr += runtime->buffer_size - epcm->ccca_start_addr;
890 else {
891 ptr -= epcm->ccca_start_addr;
892 if (ptr >= runtime->buffer_size)
893 ptr -= runtime->buffer_size;
894 }
895#endif
Takashi Iwai28a97c12009-02-05 16:08:14 +0100896 /*
Takashi Iwai6f002b02014-02-25 17:02:09 +0100897 dev_dbg(emu->card->dev,
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200898 "ptr = 0x%lx, buffer_size = 0x%lx, period_size = 0x%lx\n",
899 (long)ptr, (long)runtime->buffer_size,
900 (long)runtime->period_size);
Takashi Iwai28a97c12009-02-05 16:08:14 +0100901 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return ptr;
903}
904
905
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100906static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 int cmd)
908{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100909 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
910 struct snd_pcm_runtime *runtime = substream->runtime;
911 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 int i;
913 int result = 0;
914
915 spin_lock(&emu->reg_lock);
916 switch (cmd) {
917 case SNDRV_PCM_TRIGGER_START:
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100918 /* prepare voices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
920 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
921 }
922 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
923
924 /* follow thru */
925 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai09668b42005-11-17 16:14:10 +0100926 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
928 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
929 &emu->efx_pcm_mixer[0]);
930 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
931 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
932 &emu->efx_pcm_mixer[i]);
933 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
934 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
935 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
936 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
937 epcm->running = 1;
938 break;
Takashi Iwai09668b42005-11-17 16:14:10 +0100939 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 case SNDRV_PCM_TRIGGER_STOP:
941 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
942 epcm->running = 0;
943 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
944 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
945 }
946 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
947 break;
948 default:
949 result = -EINVAL;
950 break;
951 }
952 spin_unlock(&emu->reg_lock);
953 return result;
954}
955
956
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100957static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100959 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
960 struct snd_pcm_runtime *runtime = substream->runtime;
961 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 unsigned int ptr;
963
964 if (!epcm->running)
965 return 0;
966 if (epcm->first_ptr) {
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100967 udelay(50); /* hack, it takes awhile until capture is started */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 epcm->first_ptr = 0;
969 }
970 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
971 return bytes_to_frames(runtime, ptr);
972}
973
974/*
975 * Playback support device description
976 */
977
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100978static struct snd_pcm_hardware snd_emu10k1_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
981 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai09668b42005-11-17 16:14:10 +0100982 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
984 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
985 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
986 .rate_min = 4000,
987 .rate_max = 96000,
988 .channels_min = 1,
989 .channels_max = 2,
990 .buffer_bytes_max = (128*1024),
991 .period_bytes_min = 64,
992 .period_bytes_max = (128*1024),
993 .periods_min = 1,
994 .periods_max = 1024,
995 .fifo_size = 0,
996};
997
998/*
999 * Capture support device description
1000 */
1001
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001002static struct snd_pcm_hardware snd_emu10k1_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1005 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai09668b42005-11-17 16:14:10 +01001006 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 SNDRV_PCM_INFO_MMAP_VALID),
1008 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1009 .rates = SNDRV_PCM_RATE_8000_48000,
1010 .rate_min = 8000,
1011 .rate_max = 48000,
1012 .channels_min = 1,
1013 .channels_max = 2,
1014 .buffer_bytes_max = (64*1024),
1015 .period_bytes_min = 384,
1016 .period_bytes_max = (64*1024),
1017 .periods_min = 2,
1018 .periods_max = 2,
1019 .fifo_size = 0,
1020};
1021
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001022static struct snd_pcm_hardware snd_emu10k1_capture_efx =
1023{
1024 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1025 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1026 SNDRV_PCM_INFO_RESUME |
1027 SNDRV_PCM_INFO_MMAP_VALID),
1028 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1029 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1030 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1031 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
1032 .rate_min = 44100,
1033 .rate_max = 192000,
1034 .channels_min = 8,
1035 .channels_max = 8,
1036 .buffer_bytes_max = (64*1024),
1037 .period_bytes_min = 384,
1038 .period_bytes_max = (64*1024),
1039 .periods_min = 2,
1040 .periods_max = 2,
1041 .fifo_size = 0,
1042};
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044/*
1045 *
1046 */
1047
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001048static 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 -07001049{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001050 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02001052 if (! kctl)
1053 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (activate)
1055 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1056 else
1057 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1058 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
1059 SNDRV_CTL_EVENT_MASK_INFO,
1060 snd_ctl_build_ioff(&id, kctl, idx));
1061}
1062
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001063static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
1066 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
1067 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
1068}
1069
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001070static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
1073 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
1074 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
1075}
1076
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001077static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Jesper Juhl4d572772005-05-30 17:30:32 +02001079 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001082static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001084 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1085 struct snd_emu10k1_pcm_mixer *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int i;
1087
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001088 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 mix = &emu->efx_pcm_mixer[i];
1090 mix->epcm = NULL;
1091 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1092 }
1093 return 0;
1094}
1095
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001096static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001098 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1099 struct snd_emu10k1_pcm *epcm;
1100 struct snd_emu10k1_pcm_mixer *mix;
1101 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 int i;
1103
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001104 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (epcm == NULL)
1106 return -ENOMEM;
1107 epcm->emu = emu;
1108 epcm->type = PLAYBACK_EFX;
1109 epcm->substream = substream;
1110
1111 emu->pcm_playback_efx_substream = substream;
1112
1113 runtime->private_data = epcm;
1114 runtime->private_free = snd_emu10k1_pcm_free_substream;
1115 runtime->hw = snd_emu10k1_efx_playback;
1116
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001117 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 mix = &emu->efx_pcm_mixer[i];
1119 mix->send_routing[0][0] = i;
1120 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1121 mix->send_volume[0][0] = 255;
1122 mix->attn[0] = 0xffff;
1123 mix->epcm = epcm;
1124 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1125 }
1126 return 0;
1127}
1128
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001129static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001131 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1132 struct snd_emu10k1_pcm *epcm;
1133 struct snd_emu10k1_pcm_mixer *mix;
1134 struct snd_pcm_runtime *runtime = substream->runtime;
Mihail Zenkovd0ec95f2013-02-22 17:59:38 +00001135 int i, err, sample_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001137 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (epcm == NULL)
1139 return -ENOMEM;
1140 epcm->emu = emu;
1141 epcm->type = PLAYBACK_EMUVOICE;
1142 epcm->substream = substream;
1143 runtime->private_data = epcm;
1144 runtime->private_free = snd_emu10k1_pcm_free_substream;
1145 runtime->hw = snd_emu10k1_playback;
1146 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
1147 kfree(epcm);
1148 return err;
1149 }
1150 if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
1151 kfree(epcm);
1152 return err;
1153 }
Mihail Zenkovd0ec95f2013-02-22 17:59:38 +00001154 if (emu->card_capabilities->emu_model && emu->emu1010.internal_clock == 0)
1155 sample_rate = 44100;
1156 else
1157 sample_rate = 48000;
1158 err = snd_pcm_hw_rule_noresample(runtime, sample_rate);
Clemens Ladisch57e5c632011-09-16 23:13:38 +02001159 if (err < 0) {
1160 kfree(epcm);
1161 return err;
1162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 mix = &emu->pcm_mixer[substream->number];
1164 for (i = 0; i < 4; i++)
1165 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1166 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1167 mix->send_volume[0][0] = mix->send_volume[0][1] =
1168 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1169 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1170 mix->epcm = epcm;
1171 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1172 return 0;
1173}
1174
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001175static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001177 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1178 struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 mix->epcm = NULL;
1181 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1182 return 0;
1183}
1184
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001185static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001187 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1188 struct snd_pcm_runtime *runtime = substream->runtime;
1189 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001191 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (epcm == NULL)
1193 return -ENOMEM;
1194 epcm->emu = emu;
1195 epcm->type = CAPTURE_AC97ADC;
1196 epcm->substream = substream;
1197 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1198 epcm->capture_inte = INTE_ADCBUFENABLE;
1199 epcm->capture_ba_reg = ADCBA;
1200 epcm->capture_bs_reg = ADCBS;
1201 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1202 runtime->private_data = epcm;
1203 runtime->private_free = snd_emu10k1_pcm_free_substream;
1204 runtime->hw = snd_emu10k1_capture;
1205 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1206 emu->pcm_capture_substream = substream;
1207 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1208 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1209 return 0;
1210}
1211
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001212static int snd_emu10k1_capture_close(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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 emu->capture_interrupt = NULL;
1217 emu->pcm_capture_substream = NULL;
1218 return 0;
1219}
1220
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001221static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001223 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1224 struct snd_emu10k1_pcm *epcm;
1225 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001227 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (epcm == NULL)
1229 return -ENOMEM;
1230 epcm->emu = emu;
1231 epcm->type = CAPTURE_AC97MIC;
1232 epcm->substream = substream;
1233 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1234 epcm->capture_inte = INTE_MICBUFENABLE;
1235 epcm->capture_ba_reg = MICBA;
1236 epcm->capture_bs_reg = MICBS;
1237 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1238 substream->runtime->private_data = epcm;
1239 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1240 runtime->hw = snd_emu10k1_capture;
1241 runtime->hw.rates = SNDRV_PCM_RATE_8000;
1242 runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1243 runtime->hw.channels_min = 1;
1244 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1245 emu->pcm_capture_mic_substream = substream;
1246 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1247 return 0;
1248}
1249
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001250static int snd_emu10k1_capture_mic_close(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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 emu->capture_interrupt = NULL;
1255 emu->pcm_capture_mic_substream = NULL;
1256 return 0;
1257}
1258
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001259static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001261 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1262 struct snd_emu10k1_pcm *epcm;
1263 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int nefx = emu->audigy ? 64 : 32;
1265 int idx;
1266
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001267 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (epcm == NULL)
1269 return -ENOMEM;
1270 epcm->emu = emu;
1271 epcm->type = CAPTURE_EFX;
1272 epcm->substream = substream;
1273 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1274 epcm->capture_inte = INTE_EFXBUFENABLE;
1275 epcm->capture_ba_reg = FXBA;
1276 epcm->capture_bs_reg = FXBS;
1277 epcm->capture_idx_reg = FXIDX;
1278 substream->runtime->private_data = epcm;
1279 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001280 runtime->hw = snd_emu10k1_capture_efx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1282 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1283 spin_lock_irq(&emu->reg_lock);
James Courtier-Dutton190d2c42007-11-04 14:08:26 +00001284 if (emu->card_capabilities->emu_model) {
Pavel Hofman13d45702007-06-11 12:21:20 +02001285 /* Nb. of channels has been increased to 16 */
1286 /* TODO
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001287 * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
1288 * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1289 * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1290 * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
1291 * rate_min = 44100,
1292 * rate_max = 192000,
Pavel Hofman13d45702007-06-11 12:21:20 +02001293 * channels_min = 16,
1294 * channels_max = 16,
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001295 * Need to add mixer control to fix sample rate
1296 *
Pavel Hofman13d45702007-06-11 12:21:20 +02001297 * There are 32 mono channels of 16bits each.
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001298 * 24bit Audio uses 2x channels over 16bit
1299 * 96kHz uses 2x channels over 48kHz
1300 * 192kHz uses 4x channels over 48kHz
Pavel Hofman13d45702007-06-11 12:21:20 +02001301 * So, for 48kHz 24bit, one has 16 channels
1302 * for 96kHz 24bit, one has 8 channels
1303 * for 192kHz 24bit, one has 4 channels
1304 *
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001305 */
1306#if 1
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +01001307 switch (emu->emu1010.internal_clock) {
1308 case 0:
1309 /* For 44.1kHz */
1310 runtime->hw.rates = SNDRV_PCM_RATE_44100;
1311 runtime->hw.rate_min = runtime->hw.rate_max = 44100;
Pavel Hofman13d45702007-06-11 12:21:20 +02001312 runtime->hw.channels_min =
1313 runtime->hw.channels_max = 16;
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +01001314 break;
1315 case 1:
1316 /* For 48kHz */
1317 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1318 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
Pavel Hofman13d45702007-06-11 12:21:20 +02001319 runtime->hw.channels_min =
1320 runtime->hw.channels_max = 16;
James Courtier-Duttonb0dbdae2006-10-10 18:08:45 +01001321 break;
Peter Senna Tschudin395d9dd2012-09-28 11:24:57 +02001322 }
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001323#endif
1324#if 0
1325 /* For 96kHz */
1326 runtime->hw.rates = SNDRV_PCM_RATE_96000;
1327 runtime->hw.rate_min = runtime->hw.rate_max = 96000;
1328 runtime->hw.channels_min = runtime->hw.channels_max = 4;
1329#endif
1330#if 0
1331 /* For 192kHz */
1332 runtime->hw.rates = SNDRV_PCM_RATE_192000;
1333 runtime->hw.rate_min = runtime->hw.rate_max = 192000;
1334 runtime->hw.channels_min = runtime->hw.channels_max = 2;
1335#endif
1336 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1337 /* efx_voices_mask[0] is expected to be zero
Pavel Hofman13d45702007-06-11 12:21:20 +02001338 * efx_voices_mask[1] is expected to have 32bits set
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001339 */
1340 } else {
1341 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1342 for (idx = 0; idx < nefx; idx++) {
1343 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1344 runtime->hw.channels_min++;
1345 runtime->hw.channels_max++;
1346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348 }
1349 epcm->capture_cr_val = emu->efx_voices_mask[0];
1350 epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1351 spin_unlock_irq(&emu->reg_lock);
1352 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1353 emu->pcm_capture_efx_substream = substream;
1354 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1355 return 0;
1356}
1357
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001358static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001360 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 emu->capture_interrupt = NULL;
1363 emu->pcm_capture_efx_substream = NULL;
1364 return 0;
1365}
1366
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001367static struct snd_pcm_ops snd_emu10k1_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 .open = snd_emu10k1_playback_open,
1369 .close = snd_emu10k1_playback_close,
1370 .ioctl = snd_pcm_lib_ioctl,
1371 .hw_params = snd_emu10k1_playback_hw_params,
1372 .hw_free = snd_emu10k1_playback_hw_free,
1373 .prepare = snd_emu10k1_playback_prepare,
1374 .trigger = snd_emu10k1_playback_trigger,
1375 .pointer = snd_emu10k1_playback_pointer,
1376 .page = snd_pcm_sgbuf_ops_page,
1377};
1378
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001379static struct snd_pcm_ops snd_emu10k1_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 .open = snd_emu10k1_capture_open,
1381 .close = snd_emu10k1_capture_close,
1382 .ioctl = snd_pcm_lib_ioctl,
1383 .hw_params = snd_emu10k1_capture_hw_params,
1384 .hw_free = snd_emu10k1_capture_hw_free,
1385 .prepare = snd_emu10k1_capture_prepare,
1386 .trigger = snd_emu10k1_capture_trigger,
1387 .pointer = snd_emu10k1_capture_pointer,
1388};
1389
1390/* EFX playback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001391static struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 .open = snd_emu10k1_efx_playback_open,
1393 .close = snd_emu10k1_efx_playback_close,
1394 .ioctl = snd_pcm_lib_ioctl,
1395 .hw_params = snd_emu10k1_playback_hw_params,
1396 .hw_free = snd_emu10k1_efx_playback_hw_free,
1397 .prepare = snd_emu10k1_efx_playback_prepare,
1398 .trigger = snd_emu10k1_efx_playback_trigger,
1399 .pointer = snd_emu10k1_efx_playback_pointer,
1400 .page = snd_pcm_sgbuf_ops_page,
1401};
1402
Lars-Peter Clausenbb814c32015-01-02 12:24:49 +01001403int snd_emu10k1_pcm(struct snd_emu10k1 *emu, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001405 struct snd_pcm *pcm;
1406 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 int err;
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1410 return err;
1411
1412 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1415 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1416
1417 pcm->info_flags = 0;
1418 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1419 strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1420 emu->pcm = pcm;
1421
1422 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1423 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1424 return err;
1425
1426 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1427 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return 0;
1430}
1431
Lars-Peter Clausenbb814c32015-01-02 12:24:49 +01001432int snd_emu10k1_pcm_multi(struct snd_emu10k1 *emu, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001434 struct snd_pcm *pcm;
1435 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int err;
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return 0;
1455}
1456
1457
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001458static struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 .open = snd_emu10k1_capture_mic_open,
1460 .close = snd_emu10k1_capture_mic_close,
1461 .ioctl = snd_pcm_lib_ioctl,
1462 .hw_params = snd_emu10k1_capture_hw_params,
1463 .hw_free = snd_emu10k1_capture_hw_free,
1464 .prepare = snd_emu10k1_capture_prepare,
1465 .trigger = snd_emu10k1_capture_trigger,
1466 .pointer = snd_emu10k1_capture_pointer,
1467};
1468
Lars-Peter Clausenbb814c32015-01-02 12:24:49 +01001469int snd_emu10k1_pcm_mic(struct snd_emu10k1 *emu, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001471 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int err;
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
1475 return err;
1476
1477 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1480
1481 pcm->info_flags = 0;
1482 strcpy(pcm->name, "Mic Capture");
1483 emu->pcm_mic = pcm;
1484
1485 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return 0;
1488}
1489
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001490static 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 -07001491{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001492 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 int nefx = emu->audigy ? 64 : 32;
1494 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1495 uinfo->count = nefx;
1496 uinfo->value.integer.min = 0;
1497 uinfo->value.integer.max = 1;
1498 return 0;
1499}
1500
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001501static 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 -07001502{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001503 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 int nefx = emu->audigy ? 64 : 32;
1505 int idx;
1506
1507 spin_lock_irq(&emu->reg_lock);
1508 for (idx = 0; idx < nefx; idx++)
1509 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1510 spin_unlock_irq(&emu->reg_lock);
1511 return 0;
1512}
1513
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001514static 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 -07001515{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001516 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 unsigned int nval[2], bits;
1518 int nefx = emu->audigy ? 64 : 32;
1519 int nefxb = emu->audigy ? 7 : 6;
1520 int change, idx;
1521
1522 nval[0] = nval[1] = 0;
1523 for (idx = 0, bits = 0; idx < nefx; idx++)
1524 if (ucontrol->value.integer.value[idx]) {
1525 nval[idx / 32] |= 1 << (idx % 32);
1526 bits++;
1527 }
1528
1529 for (idx = 0; idx < nefxb; idx++)
1530 if (1 << idx == bits)
1531 break;
1532
1533 if (idx >= nefxb)
1534 return -EINVAL;
1535
1536 spin_lock_irq(&emu->reg_lock);
1537 change = (nval[0] != emu->efx_voices_mask[0]) ||
1538 (nval[1] != emu->efx_voices_mask[1]);
1539 emu->efx_voices_mask[0] = nval[0];
1540 emu->efx_voices_mask[1] = nval[1];
1541 spin_unlock_irq(&emu->reg_lock);
1542 return change;
1543}
1544
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001545static struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1547 .name = "Captured FX8010 Outputs",
1548 .info = snd_emu10k1_pcm_efx_voices_mask_info,
1549 .get = snd_emu10k1_pcm_efx_voices_mask_get,
1550 .put = snd_emu10k1_pcm_efx_voices_mask_put
1551};
1552
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001553static struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 .open = snd_emu10k1_capture_efx_open,
1555 .close = snd_emu10k1_capture_efx_close,
1556 .ioctl = snd_pcm_lib_ioctl,
1557 .hw_params = snd_emu10k1_capture_hw_params,
1558 .hw_free = snd_emu10k1_capture_hw_free,
1559 .prepare = snd_emu10k1_capture_prepare,
1560 .trigger = snd_emu10k1_capture_trigger,
1561 .pointer = snd_emu10k1_capture_pointer,
1562};
1563
1564
1565/* EFX playback */
1566
1567#define INITIAL_TRAM_SHIFT 14
1568#define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1569
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001570static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001572 struct snd_pcm_substream *substream = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 snd_pcm_period_elapsed(substream);
1574}
1575
1576static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1577 unsigned short *dst_right,
1578 unsigned short *src,
1579 unsigned int count,
1580 unsigned int tram_shift)
1581{
Takashi Iwai28a97c12009-02-05 16:08:14 +01001582 /*
Takashi Iwai6f002b02014-02-25 17:02:09 +01001583 dev_dbg(emu->card->dev,
1584 "tram_poke1: dst_left = 0x%p, dst_right = 0x%p, "
Takashi Iwai28a97c12009-02-05 16:08:14 +01001585 "src = 0x%p, count = 0x%x\n",
1586 dst_left, dst_right, src, count);
1587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if ((tram_shift & 1) == 0) {
1589 while (count--) {
1590 *dst_left-- = *src++;
1591 *dst_right-- = *src++;
1592 }
1593 } else {
1594 while (count--) {
1595 *dst_right-- = *src++;
1596 *dst_left-- = *src++;
1597 }
1598 }
1599}
1600
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001601static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1602 struct snd_pcm_indirect *rec, size_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001604 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1605 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 unsigned int tram_size = pcm->buffer_size;
1607 unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1608 unsigned int frames = bytes >> 2, count;
1609 unsigned int tram_pos = pcm->tram_pos;
1610 unsigned int tram_shift = pcm->tram_shift;
1611
1612 while (frames > tram_pos) {
1613 count = tram_pos + 1;
1614 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1615 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1616 src, count, tram_shift);
1617 src += count * 2;
1618 frames -= count;
1619 tram_pos = (tram_size / 2) - 1;
1620 tram_shift++;
1621 }
1622 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1623 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1624 src, frames, tram_shift);
1625 tram_pos -= frames;
1626 pcm->tram_pos = tram_pos;
1627 pcm->tram_shift = tram_shift;
1628}
1629
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001630static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001632 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1633 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy);
1636 return 0;
1637}
1638
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001639static int snd_emu10k1_fx8010_playback_hw_params(struct snd_pcm_substream *substream,
1640 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1643}
1644
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001645static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001647 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1648 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 unsigned int i;
1650
1651 for (i = 0; i < pcm->channels; i++)
1652 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1653 snd_pcm_lib_free_pages(substream);
1654 return 0;
1655}
1656
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001657static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001659 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1660 struct snd_pcm_runtime *runtime = substream->runtime;
1661 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 unsigned int i;
1663
Takashi Iwai28a97c12009-02-05 16:08:14 +01001664 /*
Takashi Iwai6f002b02014-02-25 17:02:09 +01001665 dev_dbg(emu->card->dev, "prepare: etram_pages = 0x%p, dma_area = 0x%x, "
Takashi Iwai28a97c12009-02-05 16:08:14 +01001666 "buffer_size = 0x%x (0x%x)\n",
1667 emu->fx8010.etram_pages, runtime->dma_area,
1668 runtime->buffer_size, runtime->buffer_size << 2);
1669 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1671 pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1672 pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1673 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1674 pcm->tram_shift = 0;
1675 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */
1676 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */
1677 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1678 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */
1679 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1680 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1681 for (i = 0; i < pcm->channels; i++)
1682 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1683 return 0;
1684}
1685
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001686static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001688 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1689 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 int result = 0;
1691
1692 spin_lock(&emu->reg_lock);
1693 switch (cmd) {
1694 case SNDRV_PCM_TRIGGER_START:
1695 /* follow thru */
1696 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai09668b42005-11-17 16:14:10 +01001697 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698#ifdef EMU10K1_SET_AC3_IEC958
1699 {
1700 int i;
1701 for (i = 0; i < 3; i++) {
1702 unsigned int bits;
1703 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1704 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1705 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1706 snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1707 }
1708 }
1709#endif
1710 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1711 if (result < 0)
1712 goto __err;
1713 snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
1714 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1715 break;
1716 case SNDRV_PCM_TRIGGER_STOP:
1717 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai09668b42005-11-17 16:14:10 +01001718 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL;
1720 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1721 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1722 pcm->tram_shift = 0;
1723 break;
1724 default:
1725 result = -EINVAL;
1726 break;
1727 }
1728 __err:
1729 spin_unlock(&emu->reg_lock);
1730 return result;
1731}
1732
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001733static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001735 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1736 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 size_t ptr; /* byte pointer */
1738
1739 if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1740 return 0;
1741 ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1742 return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1743}
1744
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001745static struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Takashi Iwai09668b42005-11-17 16:14:10 +01001748 SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE),
1750 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1751 .rates = SNDRV_PCM_RATE_48000,
1752 .rate_min = 48000,
1753 .rate_max = 48000,
1754 .channels_min = 1,
1755 .channels_max = 1,
1756 .buffer_bytes_max = (128*1024),
1757 .period_bytes_min = 1024,
1758 .period_bytes_max = (128*1024),
Takashi Iwai806d31d2009-06-08 15:50:42 +02001759 .periods_min = 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 .periods_max = 1024,
1761 .fifo_size = 0,
1762};
1763
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001764static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001766 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1767 struct snd_pcm_runtime *runtime = substream->runtime;
1768 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 runtime->hw = snd_emu10k1_fx8010_playback;
1771 runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1772 runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1773 spin_lock_irq(&emu->reg_lock);
1774 if (pcm->valid == 0) {
1775 spin_unlock_irq(&emu->reg_lock);
1776 return -ENODEV;
1777 }
1778 pcm->opened = 1;
1779 spin_unlock_irq(&emu->reg_lock);
1780 return 0;
1781}
1782
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001783static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001785 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1786 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 spin_lock_irq(&emu->reg_lock);
1789 pcm->opened = 0;
1790 spin_unlock_irq(&emu->reg_lock);
1791 return 0;
1792}
1793
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001794static struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 .open = snd_emu10k1_fx8010_playback_open,
1796 .close = snd_emu10k1_fx8010_playback_close,
1797 .ioctl = snd_pcm_lib_ioctl,
1798 .hw_params = snd_emu10k1_fx8010_playback_hw_params,
1799 .hw_free = snd_emu10k1_fx8010_playback_hw_free,
1800 .prepare = snd_emu10k1_fx8010_playback_prepare,
1801 .trigger = snd_emu10k1_fx8010_playback_trigger,
1802 .pointer = snd_emu10k1_fx8010_playback_pointer,
1803 .ack = snd_emu10k1_fx8010_playback_transfer,
1804};
1805
Lars-Peter Clausenbb814c32015-01-02 12:24:49 +01001806int snd_emu10k1_pcm_efx(struct snd_emu10k1 *emu, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001808 struct snd_pcm *pcm;
1809 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 int err;
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1813 return err;
1814
1815 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1818 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1819
1820 pcm->info_flags = 0;
1821 strcpy(pcm->name, "Multichannel Capture/PT Playback");
1822 emu->pcm_efx = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
1825 * to these
1826 */
1827
1828 /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1829 if (emu->audigy) {
1830 emu->efx_voices_mask[0] = 0;
James Courtier-Dutton190d2c42007-11-04 14:08:26 +00001831 if (emu->card_capabilities->emu_model)
Pavel Hofman13d45702007-06-11 12:21:20 +02001832 /* Pavel Hofman - 32 voices will be used for
1833 * capture (write mode) -
1834 * each bit = corresponding voice
1835 */
1836 emu->efx_voices_mask[1] = 0xffffffff;
1837 else
1838 emu->efx_voices_mask[1] = 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 } else {
1840 emu->efx_voices_mask[0] = 0xffff0000;
1841 emu->efx_voices_mask[1] = 0;
1842 }
Pavel Hofman13d45702007-06-11 12:21:20 +02001843 /* For emu1010, the control has to set 32 upper bits (voices)
1844 * out of the 64 bits (voices) to true for the 16-channels capture
1845 * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
1846 * is already defined but the snd_emu10k1_pcm_efx_voices_mask
1847 * control can override this register's value.
1848 */
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001849 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1850 if (!kctl)
1851 return -ENOMEM;
1852 kctl->id.device = device;
1853 snd_ctl_add(emu->card, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1856
1857 return 0;
1858}