blob: 8e6caf581c0ccf4e351359c58fdd2b33b49169fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Creative Labs, Inc.
4 * Routines for control of EMU10K1 chips / PCM routines
5 * Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
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
29#include <sound/driver.h>
30#include <linux/pci.h>
31#include <linux/delay.h>
32#include <linux/slab.h>
33#include <linux/time.h>
34#include <linux/init.h>
35#include <sound/core.h>
36#include <sound/emu10k1.h>
37
Takashi Iwaieb4698f2005-11-17 14:50:13 +010038static void snd_emu10k1_pcm_interrupt(struct snd_emu10k1 *emu,
39 struct snd_emu10k1_voice *voice)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Takashi Iwaieb4698f2005-11-17 14:50:13 +010041 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 if ((epcm = voice->epcm) == NULL)
44 return;
45 if (epcm->substream == NULL)
46 return;
47#if 0
48 printk("IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
49 epcm->substream->runtime->hw->pointer(emu, epcm->substream),
50 snd_pcm_lib_period_bytes(epcm->substream),
51 snd_pcm_lib_buffer_bytes(epcm->substream));
52#endif
53 snd_pcm_period_elapsed(epcm->substream);
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) {
150 // printk("pcm_channel_alloc: failed extra: voices=%d, frame=%d\n", voices, frame);
151 for (i = 0; i < voices; i++) {
152 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
153 epcm->voices[i] = NULL;
154 }
155 return err;
156 }
157 epcm->extra->epcm = epcm;
158 epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
159 }
160 return 0;
161}
162
163static unsigned int capture_period_sizes[31] = {
164 384, 448, 512, 640,
165 384*2, 448*2, 512*2, 640*2,
166 384*4, 448*4, 512*4, 640*4,
167 384*8, 448*8, 512*8, 640*8,
168 384*16, 448*16, 512*16, 640*16,
169 384*32, 448*32, 512*32, 640*32,
170 384*64, 448*64, 512*64, 640*64,
171 384*128,448*128,512*128
172};
173
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100174static struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 .count = 31,
176 .list = capture_period_sizes,
177 .mask = 0
178};
179
180static unsigned int capture_rates[8] = {
181 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
182};
183
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100184static struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 .count = 8,
186 .list = capture_rates,
187 .mask = 0
188};
189
190static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
191{
192 switch (rate) {
193 case 8000: return ADCCR_SAMPLERATE_8;
194 case 11025: return ADCCR_SAMPLERATE_11;
195 case 16000: return ADCCR_SAMPLERATE_16;
196 case 22050: return ADCCR_SAMPLERATE_22;
197 case 24000: return ADCCR_SAMPLERATE_24;
198 case 32000: return ADCCR_SAMPLERATE_32;
199 case 44100: return ADCCR_SAMPLERATE_44;
200 case 48000: return ADCCR_SAMPLERATE_48;
201 default:
202 snd_BUG();
203 return ADCCR_SAMPLERATE_8;
204 }
205}
206
207static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
208{
209 switch (rate) {
210 case 8000: return A_ADCCR_SAMPLERATE_8;
211 case 11025: return A_ADCCR_SAMPLERATE_11;
212 case 12000: return A_ADCCR_SAMPLERATE_12; /* really supported? */
213 case 16000: return ADCCR_SAMPLERATE_16;
214 case 22050: return ADCCR_SAMPLERATE_22;
215 case 24000: return ADCCR_SAMPLERATE_24;
216 case 32000: return ADCCR_SAMPLERATE_32;
217 case 44100: return ADCCR_SAMPLERATE_44;
218 case 48000: return ADCCR_SAMPLERATE_48;
219 default:
220 snd_BUG();
221 return A_ADCCR_SAMPLERATE_8;
222 }
223}
224
225static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
226{
227 unsigned int pitch_target;
228
229 pitch_target = (rate << 8) / 375;
230 pitch_target = (pitch_target >> 1) + (pitch_target & 1);
231 return pitch_target;
232}
233
234#define PITCH_48000 0x00004000
235#define PITCH_96000 0x00008000
236#define PITCH_85000 0x00007155
237#define PITCH_80726 0x00006ba2
238#define PITCH_67882 0x00005a82
239#define PITCH_57081 0x00004c1c
240
241static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
242{
243 if (pitch_target == PITCH_48000)
244 return CCCA_INTERPROM_0;
245 else if (pitch_target < PITCH_48000)
246 return CCCA_INTERPROM_1;
247 else if (pitch_target >= PITCH_96000)
248 return CCCA_INTERPROM_0;
249 else if (pitch_target >= PITCH_85000)
250 return CCCA_INTERPROM_6;
251 else if (pitch_target >= PITCH_80726)
252 return CCCA_INTERPROM_5;
253 else if (pitch_target >= PITCH_67882)
254 return CCCA_INTERPROM_4;
255 else if (pitch_target >= PITCH_57081)
256 return CCCA_INTERPROM_3;
257 else
258 return CCCA_INTERPROM_2;
259}
260
261/*
262 * calculate cache invalidate size
263 *
264 * stereo: channel is stereo
265 * w_16: using 16bit samples
266 *
267 * returns: cache invalidate size in samples
268 */
Jesper Juhlfe5ac9d2005-04-20 10:03:31 +0200269static inline int emu10k1_ccis(int stereo, int w_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 if (w_16) {
272 return stereo ? 24 : 26;
273 } else {
274 return stereo ? 24*2 : 26*2;
275 }
276}
277
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100278static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int master, int extra,
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100280 struct snd_emu10k1_voice *evoice,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 unsigned int start_addr,
282 unsigned int end_addr,
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100283 struct snd_emu10k1_pcm_mixer *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100285 struct snd_pcm_substream *substream = evoice->epcm->substream;
286 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 unsigned int silent_page, tmp;
288 int voice, stereo, w_16;
289 unsigned char attn, send_amount[8];
290 unsigned char send_routing[8];
291 unsigned long flags;
292 unsigned int pitch_target;
293 unsigned int ccis;
294
295 voice = evoice->number;
296 stereo = runtime->channels == 2;
297 w_16 = snd_pcm_format_width(runtime->format) == 16;
298
299 if (!extra && stereo) {
300 start_addr >>= 1;
301 end_addr >>= 1;
302 }
303 if (w_16) {
304 start_addr >>= 1;
305 end_addr >>= 1;
306 }
307
308 spin_lock_irqsave(&emu->reg_lock, flags);
309
310 /* volume parameters */
311 if (extra) {
312 attn = 0;
313 memset(send_routing, 0, sizeof(send_routing));
314 send_routing[0] = 0;
315 send_routing[1] = 1;
316 send_routing[2] = 2;
317 send_routing[3] = 3;
318 memset(send_amount, 0, sizeof(send_amount));
319 } else {
320 /* mono, left, right (master voice = left) */
321 tmp = stereo ? (master ? 1 : 2) : 0;
322 memcpy(send_routing, &mix->send_routing[tmp][0], 8);
323 memcpy(send_amount, &mix->send_volume[tmp][0], 8);
324 }
325
326 ccis = emu10k1_ccis(stereo, w_16);
327
328 if (master) {
329 evoice->epcm->ccca_start_addr = start_addr + ccis;
330 if (extra) {
331 start_addr += ccis;
332 end_addr += ccis;
333 }
334 if (stereo && !extra) {
335 snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK);
336 snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK);
337 } else {
338 snd_emu10k1_ptr_write(emu, CPF, voice, 0);
339 }
340 }
341
342 // setup routing
343 if (emu->audigy) {
344 snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
345 snd_emu10k1_compose_audigy_fxrt1(send_routing));
346 snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
347 snd_emu10k1_compose_audigy_fxrt2(send_routing));
348 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
349 ((unsigned int)send_amount[4] << 24) |
350 ((unsigned int)send_amount[5] << 16) |
351 ((unsigned int)send_amount[6] << 8) |
352 (unsigned int)send_amount[7]);
353 } else
354 snd_emu10k1_ptr_write(emu, FXRT, voice,
355 snd_emu10k1_compose_send_routing(send_routing));
356 // Stop CA
357 // Assumption that PT is already 0 so no harm overwriting
358 snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]);
359 snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24));
360 snd_emu10k1_ptr_write(emu, PSST, voice, start_addr | (send_amount[2] << 24));
361 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
362 if (extra)
363 snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
364 emu10k1_select_interprom(pitch_target) |
365 (w_16 ? 0 : CCCA_8BITSELECT));
366 else
367 snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
368 emu10k1_select_interprom(pitch_target) |
369 (w_16 ? 0 : CCCA_8BITSELECT));
370 // Clear filter delay memory
371 snd_emu10k1_ptr_write(emu, Z1, voice, 0);
372 snd_emu10k1_ptr_write(emu, Z2, voice, 0);
373 // invalidate maps
374 silent_page = ((unsigned int)emu->silent_page.addr << 1) | MAP_PTI_MASK;
375 snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
376 snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
377 // modulation envelope
378 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
379 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
380 snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
381 snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
382 snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
383 snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
384 snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
385 snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
386 snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
387 snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
388 // volume envelope
389 snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
390 snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
391 // filter envelope
392 snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
393 // pitch envelope
394 snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
395
396 spin_unlock_irqrestore(&emu->reg_lock, flags);
397}
398
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100399static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
400 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100402 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
403 struct snd_pcm_runtime *runtime = substream->runtime;
404 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int err;
406
407 if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0)
408 return err;
409 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
410 return err;
411 if (err > 0) { /* change */
Takashi Iwaie017fa52005-11-17 10:44:51 +0100412 int mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (epcm->memblk != NULL)
414 snd_emu10k1_free_pages(emu, epcm->memblk);
Takashi Iwaie017fa52005-11-17 10:44:51 +0100415 epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
416 epcm->start_addr = 0;
417 if (! epcm->memblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return -ENOMEM;
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100419 mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
Takashi Iwaie017fa52005-11-17 10:44:51 +0100420 if (mapped < 0)
421 return -ENOMEM;
422 epcm->start_addr = mapped << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 return 0;
425}
426
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100427static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100429 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
430 struct snd_pcm_runtime *runtime = substream->runtime;
431 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (runtime->private_data == NULL)
434 return 0;
435 epcm = runtime->private_data;
436 if (epcm->extra) {
437 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
438 epcm->extra = NULL;
439 }
440 if (epcm->voices[1]) {
441 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
442 epcm->voices[1] = NULL;
443 }
444 if (epcm->voices[0]) {
445 snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
446 epcm->voices[0] = NULL;
447 }
448 if (epcm->memblk) {
449 snd_emu10k1_free_pages(emu, epcm->memblk);
450 epcm->memblk = NULL;
451 epcm->start_addr = 0;
452 }
453 snd_pcm_lib_free_pages(substream);
454 return 0;
455}
456
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100457static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100459 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
460 struct snd_pcm_runtime *runtime = substream->runtime;
461 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int i;
463
464 if (runtime->private_data == NULL)
465 return 0;
466 epcm = runtime->private_data;
467 if (epcm->extra) {
468 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
469 epcm->extra = NULL;
470 }
471 for (i=0; i < NUM_EFX_PLAYBACK; i++) {
472 if (epcm->voices[i]) {
473 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
474 epcm->voices[i] = NULL;
475 }
476 }
477 if (epcm->memblk) {
478 snd_emu10k1_free_pages(emu, epcm->memblk);
479 epcm->memblk = NULL;
480 epcm->start_addr = 0;
481 }
482 snd_pcm_lib_free_pages(substream);
483 return 0;
484}
485
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100486static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100488 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
489 struct snd_pcm_runtime *runtime = substream->runtime;
490 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 unsigned int start_addr, end_addr;
492
493 start_addr = epcm->start_addr;
494 end_addr = snd_pcm_lib_period_bytes(substream);
495 if (runtime->channels == 2) {
496 start_addr >>= 1;
497 end_addr >>= 1;
498 }
499 end_addr += start_addr;
500 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
501 start_addr, end_addr, NULL);
502 start_addr = epcm->start_addr;
503 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
504 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
505 start_addr, end_addr,
506 &emu->pcm_mixer[substream->number]);
507 if (epcm->voices[1])
508 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
509 start_addr, end_addr,
510 &emu->pcm_mixer[substream->number]);
511 return 0;
512}
513
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100514static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100516 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
517 struct snd_pcm_runtime *runtime = substream->runtime;
518 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 unsigned int start_addr, end_addr;
520 unsigned int channel_size;
521 int i;
522
523 start_addr = epcm->start_addr;
524 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
525
526 /*
527 * the kX driver leaves some space between voices
528 */
529 channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
530
531 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
532 start_addr, start_addr + (channel_size / 2), NULL);
533
534 /* only difference with the master voice is we use it for the pointer */
535 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
536 start_addr, start_addr + channel_size,
537 &emu->efx_pcm_mixer[0]);
538
539 start_addr += channel_size;
540 for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
541 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
542 start_addr, start_addr + channel_size,
543 &emu->efx_pcm_mixer[i]);
544 start_addr += channel_size;
545 }
546
547 return 0;
548}
549
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100550static struct snd_pcm_hardware snd_emu10k1_efx_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
553 SNDRV_PCM_INFO_BLOCK_TRANSFER |
554 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
555 .formats = SNDRV_PCM_FMTBIT_S16_LE,
556 .rates = SNDRV_PCM_RATE_48000,
557 .rate_min = 48000,
558 .rate_max = 48000,
559 .channels_min = NUM_EFX_PLAYBACK,
560 .channels_max = NUM_EFX_PLAYBACK,
561 .buffer_bytes_max = (64*1024),
562 .period_bytes_min = 64,
563 .period_bytes_max = (64*1024),
564 .periods_min = 2,
565 .periods_max = 2,
566 .fifo_size = 0,
567};
568
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100569static int snd_emu10k1_capture_hw_params(struct snd_pcm_substream *substream,
570 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
573}
574
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100575static int snd_emu10k1_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 return snd_pcm_lib_free_pages(substream);
578}
579
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100580static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100582 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
583 struct snd_pcm_runtime *runtime = substream->runtime;
584 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int idx;
586
587 /* zeroing the buffer size will stop capture */
588 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
589 switch (epcm->type) {
590 case CAPTURE_AC97ADC:
591 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
592 break;
593 case CAPTURE_EFX:
594 if (emu->audigy) {
595 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
596 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
597 } else
598 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
599 break;
600 default:
601 break;
602 }
603 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
604 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
605 epcm->capture_bs_val = 0;
606 for (idx = 0; idx < 31; idx++) {
607 if (capture_period_sizes[idx] == epcm->capture_bufsize) {
608 epcm->capture_bs_val = idx + 1;
609 break;
610 }
611 }
612 if (epcm->capture_bs_val == 0) {
613 snd_BUG();
614 epcm->capture_bs_val++;
615 }
616 if (epcm->type == CAPTURE_AC97ADC) {
617 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
618 if (runtime->channels > 1)
619 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
620 epcm->capture_cr_val |= emu->audigy ?
621 snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
622 snd_emu10k1_capture_rate_reg(runtime->rate);
623 }
624 return 0;
625}
626
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100627static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100629 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
631
632 if (evoice == NULL)
633 return;
634 runtime = evoice->epcm->substream->runtime;
635 voice = evoice->number;
636 stereo = (!extra && runtime->channels == 2);
637 sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
638 ccis = emu10k1_ccis(stereo, sample == 0);
639 // set cs to 2 * number of cache registers beside the invalidated
640 cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
641 if (cs > 16) cs = 16;
642 for (i = 0; i < cs; i++) {
643 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
644 if (stereo) {
645 snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
646 }
647 }
648 // reset cache
649 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
650 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
651 if (stereo) {
652 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
653 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
654 }
655 // fill cache
656 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
657 if (stereo) {
658 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
659 }
660}
661
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100662static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 int master, int extra,
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100664 struct snd_emu10k1_pcm_mixer *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100666 struct snd_pcm_substream *substream;
667 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 unsigned int attn, vattn;
669 unsigned int voice, tmp;
670
671 if (evoice == NULL) /* skip second voice for mono */
672 return;
673 substream = evoice->epcm->substream;
674 runtime = substream->runtime;
675 voice = evoice->number;
676
677 attn = extra ? 0 : 0x00ff;
678 tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
679 vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
680 snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
681 snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
682 snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
683 snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
684 snd_emu10k1_voice_clear_loop_stop(emu, voice);
685}
686
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100687static 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 -0700688{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100689 struct snd_pcm_substream *substream;
690 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 unsigned int voice, pitch, pitch_target;
692
693 if (evoice == NULL) /* skip second voice for mono */
694 return;
695 substream = evoice->epcm->substream;
696 runtime = substream->runtime;
697 voice = evoice->number;
698
699 pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
700 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
701 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
702 if (master || evoice->epcm->type == PLAYBACK_EFX)
703 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
704 snd_emu10k1_ptr_write(emu, IP, voice, pitch);
705 if (extra)
706 snd_emu10k1_voice_intr_enable(emu, voice);
707}
708
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100709static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 unsigned int voice;
712
713 if (evoice == NULL)
714 return;
715 voice = evoice->number;
716 snd_emu10k1_voice_intr_disable(emu, voice);
717 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
718 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
719 snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
720 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
721 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
722 snd_emu10k1_ptr_write(emu, IP, voice, 0);
723}
724
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100725static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 int cmd)
727{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100728 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
729 struct snd_pcm_runtime *runtime = substream->runtime;
730 struct snd_emu10k1_pcm *epcm = runtime->private_data;
731 struct snd_emu10k1_pcm_mixer *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 int result = 0;
733
734 // printk("trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n", (int)emu, cmd, substream->ops->pointer(substream));
735 spin_lock(&emu->reg_lock);
736 switch (cmd) {
737 case SNDRV_PCM_TRIGGER_START:
738 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */
739 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
740 /* follow thru */
741 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
742 mix = &emu->pcm_mixer[substream->number];
743 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
744 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
745 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
746 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
747 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
748 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
749 epcm->running = 1;
750 break;
751 case SNDRV_PCM_TRIGGER_STOP:
752 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
753 epcm->running = 0;
754 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
755 snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
756 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
757 break;
758 default:
759 result = -EINVAL;
760 break;
761 }
762 spin_unlock(&emu->reg_lock);
763 return result;
764}
765
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100766static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int cmd)
768{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100769 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
770 struct snd_pcm_runtime *runtime = substream->runtime;
771 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 int result = 0;
773
774 spin_lock(&emu->reg_lock);
775 switch (cmd) {
776 case SNDRV_PCM_TRIGGER_START:
777 // hmm this should cause full and half full interrupt to be raised?
778 outl(epcm->capture_ipr, emu->port + IPR);
779 snd_emu10k1_intr_enable(emu, epcm->capture_inte);
780 // printk("adccr = 0x%x, adcbs = 0x%x\n", epcm->adccr, epcm->adcbs);
781 switch (epcm->type) {
782 case CAPTURE_AC97ADC:
783 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
784 break;
785 case CAPTURE_EFX:
786 if (emu->audigy) {
787 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
788 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
789 } else
790 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
791 break;
792 default:
793 break;
794 }
795 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
796 epcm->running = 1;
797 epcm->first_ptr = 1;
798 break;
799 case SNDRV_PCM_TRIGGER_STOP:
800 epcm->running = 0;
801 snd_emu10k1_intr_disable(emu, epcm->capture_inte);
802 outl(epcm->capture_ipr, emu->port + IPR);
803 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
804 switch (epcm->type) {
805 case CAPTURE_AC97ADC:
806 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
807 break;
808 case CAPTURE_EFX:
809 if (emu->audigy) {
810 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
811 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
812 } else
813 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
814 break;
815 default:
816 break;
817 }
818 break;
819 default:
820 result = -EINVAL;
821 }
822 spin_unlock(&emu->reg_lock);
823 return result;
824}
825
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100826static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100828 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
829 struct snd_pcm_runtime *runtime = substream->runtime;
830 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 unsigned int ptr;
832
833 if (!epcm->running)
834 return 0;
835 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
836#if 0 /* Perex's code */
837 ptr += runtime->buffer_size;
838 ptr -= epcm->ccca_start_addr;
839 ptr %= runtime->buffer_size;
840#else /* EMU10K1 Open Source code from Creative */
841 if (ptr < epcm->ccca_start_addr)
842 ptr += runtime->buffer_size - epcm->ccca_start_addr;
843 else {
844 ptr -= epcm->ccca_start_addr;
845 if (ptr >= runtime->buffer_size)
846 ptr -= runtime->buffer_size;
847 }
848#endif
849 // printk("ptr = 0x%x, buffer_size = 0x%x, period_size = 0x%x\n", ptr, runtime->buffer_size, runtime->period_size);
850 return ptr;
851}
852
853
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100854static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 int cmd)
856{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100857 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
858 struct snd_pcm_runtime *runtime = substream->runtime;
859 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 int i;
861 int result = 0;
862
863 spin_lock(&emu->reg_lock);
864 switch (cmd) {
865 case SNDRV_PCM_TRIGGER_START:
866 // prepare voices
867 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
868 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
869 }
870 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
871
872 /* follow thru */
873 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
874 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
875 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
876 &emu->efx_pcm_mixer[0]);
877 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
878 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
879 &emu->efx_pcm_mixer[i]);
880 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
881 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
882 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
883 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
884 epcm->running = 1;
885 break;
886 case SNDRV_PCM_TRIGGER_STOP:
887 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
888 epcm->running = 0;
889 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
890 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
891 }
892 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
893 break;
894 default:
895 result = -EINVAL;
896 break;
897 }
898 spin_unlock(&emu->reg_lock);
899 return result;
900}
901
902
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100903static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100905 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
906 struct snd_pcm_runtime *runtime = substream->runtime;
907 struct snd_emu10k1_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 unsigned int ptr;
909
910 if (!epcm->running)
911 return 0;
912 if (epcm->first_ptr) {
913 udelay(50); // hack, it takes awhile until capture is started
914 epcm->first_ptr = 0;
915 }
916 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
917 return bytes_to_frames(runtime, ptr);
918}
919
920/*
921 * Playback support device description
922 */
923
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100924static struct snd_pcm_hardware snd_emu10k1_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
927 SNDRV_PCM_INFO_BLOCK_TRANSFER |
928 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
929 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
930 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
931 .rate_min = 4000,
932 .rate_max = 96000,
933 .channels_min = 1,
934 .channels_max = 2,
935 .buffer_bytes_max = (128*1024),
936 .period_bytes_min = 64,
937 .period_bytes_max = (128*1024),
938 .periods_min = 1,
939 .periods_max = 1024,
940 .fifo_size = 0,
941};
942
943/*
944 * Capture support device description
945 */
946
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100947static struct snd_pcm_hardware snd_emu10k1_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
950 SNDRV_PCM_INFO_BLOCK_TRANSFER |
951 SNDRV_PCM_INFO_MMAP_VALID),
952 .formats = SNDRV_PCM_FMTBIT_S16_LE,
953 .rates = SNDRV_PCM_RATE_8000_48000,
954 .rate_min = 8000,
955 .rate_max = 48000,
956 .channels_min = 1,
957 .channels_max = 2,
958 .buffer_bytes_max = (64*1024),
959 .period_bytes_min = 384,
960 .period_bytes_max = (64*1024),
961 .periods_min = 2,
962 .periods_max = 2,
963 .fifo_size = 0,
964};
965
966/*
967 *
968 */
969
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100970static 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 -0700971{
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100972 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200974 if (! kctl)
975 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (activate)
977 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
978 else
979 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
980 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
981 SNDRV_CTL_EVENT_MASK_INFO,
982 snd_ctl_build_ioff(&id, kctl, idx));
983}
984
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100985static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
988 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
989 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
990}
991
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100992static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
995 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
996 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
997}
998
Takashi Iwaieb4698f2005-11-17 14:50:13 +0100999static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Jesper Juhl4d572772005-05-30 17:30:32 +02001001 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001004static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001006 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1007 struct snd_emu10k1_pcm_mixer *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 int i;
1009
1010 for (i=0; i < NUM_EFX_PLAYBACK; i++) {
1011 mix = &emu->efx_pcm_mixer[i];
1012 mix->epcm = NULL;
1013 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1014 }
1015 return 0;
1016}
1017
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001018static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001020 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1021 struct snd_emu10k1_pcm *epcm;
1022 struct snd_emu10k1_pcm_mixer *mix;
1023 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int i;
1025
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001026 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (epcm == NULL)
1028 return -ENOMEM;
1029 epcm->emu = emu;
1030 epcm->type = PLAYBACK_EFX;
1031 epcm->substream = substream;
1032
1033 emu->pcm_playback_efx_substream = substream;
1034
1035 runtime->private_data = epcm;
1036 runtime->private_free = snd_emu10k1_pcm_free_substream;
1037 runtime->hw = snd_emu10k1_efx_playback;
1038
1039 for (i=0; i < NUM_EFX_PLAYBACK; i++) {
1040 mix = &emu->efx_pcm_mixer[i];
1041 mix->send_routing[0][0] = i;
1042 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1043 mix->send_volume[0][0] = 255;
1044 mix->attn[0] = 0xffff;
1045 mix->epcm = epcm;
1046 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1047 }
1048 return 0;
1049}
1050
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001051static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001053 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1054 struct snd_emu10k1_pcm *epcm;
1055 struct snd_emu10k1_pcm_mixer *mix;
1056 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 int i, err;
1058
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001059 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (epcm == NULL)
1061 return -ENOMEM;
1062 epcm->emu = emu;
1063 epcm->type = PLAYBACK_EMUVOICE;
1064 epcm->substream = substream;
1065 runtime->private_data = epcm;
1066 runtime->private_free = snd_emu10k1_pcm_free_substream;
1067 runtime->hw = snd_emu10k1_playback;
1068 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
1069 kfree(epcm);
1070 return err;
1071 }
1072 if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
1073 kfree(epcm);
1074 return err;
1075 }
1076 mix = &emu->pcm_mixer[substream->number];
1077 for (i = 0; i < 4; i++)
1078 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1079 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1080 mix->send_volume[0][0] = mix->send_volume[0][1] =
1081 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1082 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1083 mix->epcm = epcm;
1084 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1085 return 0;
1086}
1087
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001088static int snd_emu10k1_playback_close(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_mixer *mix = &emu->pcm_mixer[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 mix->epcm = NULL;
1094 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1095 return 0;
1096}
1097
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001098static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001100 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1101 struct snd_pcm_runtime *runtime = substream->runtime;
1102 struct snd_emu10k1_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
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 = CAPTURE_AC97ADC;
1109 epcm->substream = substream;
1110 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1111 epcm->capture_inte = INTE_ADCBUFENABLE;
1112 epcm->capture_ba_reg = ADCBA;
1113 epcm->capture_bs_reg = ADCBS;
1114 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1115 runtime->private_data = epcm;
1116 runtime->private_free = snd_emu10k1_pcm_free_substream;
1117 runtime->hw = snd_emu10k1_capture;
1118 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1119 emu->pcm_capture_substream = substream;
1120 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1121 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1122 return 0;
1123}
1124
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001125static int snd_emu10k1_capture_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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 emu->capture_interrupt = NULL;
1130 emu->pcm_capture_substream = NULL;
1131 return 0;
1132}
1133
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001134static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001136 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1137 struct snd_emu10k1_pcm *epcm;
1138 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001140 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (epcm == NULL)
1142 return -ENOMEM;
1143 epcm->emu = emu;
1144 epcm->type = CAPTURE_AC97MIC;
1145 epcm->substream = substream;
1146 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1147 epcm->capture_inte = INTE_MICBUFENABLE;
1148 epcm->capture_ba_reg = MICBA;
1149 epcm->capture_bs_reg = MICBS;
1150 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1151 substream->runtime->private_data = epcm;
1152 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1153 runtime->hw = snd_emu10k1_capture;
1154 runtime->hw.rates = SNDRV_PCM_RATE_8000;
1155 runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1156 runtime->hw.channels_min = 1;
1157 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1158 emu->pcm_capture_mic_substream = substream;
1159 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1160 return 0;
1161}
1162
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001163static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001165 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 emu->capture_interrupt = NULL;
1168 emu->pcm_capture_mic_substream = NULL;
1169 return 0;
1170}
1171
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001172static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001174 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1175 struct snd_emu10k1_pcm *epcm;
1176 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 int nefx = emu->audigy ? 64 : 32;
1178 int idx;
1179
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001180 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (epcm == NULL)
1182 return -ENOMEM;
1183 epcm->emu = emu;
1184 epcm->type = CAPTURE_EFX;
1185 epcm->substream = substream;
1186 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1187 epcm->capture_inte = INTE_EFXBUFENABLE;
1188 epcm->capture_ba_reg = FXBA;
1189 epcm->capture_bs_reg = FXBS;
1190 epcm->capture_idx_reg = FXIDX;
1191 substream->runtime->private_data = epcm;
1192 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1193 runtime->hw = snd_emu10k1_capture;
1194 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1195 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1196 spin_lock_irq(&emu->reg_lock);
1197 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1198 for (idx = 0; idx < nefx; idx++) {
1199 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1200 runtime->hw.channels_min++;
1201 runtime->hw.channels_max++;
1202 }
1203 }
1204 epcm->capture_cr_val = emu->efx_voices_mask[0];
1205 epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1206 spin_unlock_irq(&emu->reg_lock);
1207 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1208 emu->pcm_capture_efx_substream = substream;
1209 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1210 return 0;
1211}
1212
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001213static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001215 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 emu->capture_interrupt = NULL;
1218 emu->pcm_capture_efx_substream = NULL;
1219 return 0;
1220}
1221
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001222static struct snd_pcm_ops snd_emu10k1_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 .open = snd_emu10k1_playback_open,
1224 .close = snd_emu10k1_playback_close,
1225 .ioctl = snd_pcm_lib_ioctl,
1226 .hw_params = snd_emu10k1_playback_hw_params,
1227 .hw_free = snd_emu10k1_playback_hw_free,
1228 .prepare = snd_emu10k1_playback_prepare,
1229 .trigger = snd_emu10k1_playback_trigger,
1230 .pointer = snd_emu10k1_playback_pointer,
1231 .page = snd_pcm_sgbuf_ops_page,
1232};
1233
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001234static struct snd_pcm_ops snd_emu10k1_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 .open = snd_emu10k1_capture_open,
1236 .close = snd_emu10k1_capture_close,
1237 .ioctl = snd_pcm_lib_ioctl,
1238 .hw_params = snd_emu10k1_capture_hw_params,
1239 .hw_free = snd_emu10k1_capture_hw_free,
1240 .prepare = snd_emu10k1_capture_prepare,
1241 .trigger = snd_emu10k1_capture_trigger,
1242 .pointer = snd_emu10k1_capture_pointer,
1243};
1244
1245/* EFX playback */
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001246static struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 .open = snd_emu10k1_efx_playback_open,
1248 .close = snd_emu10k1_efx_playback_close,
1249 .ioctl = snd_pcm_lib_ioctl,
1250 .hw_params = snd_emu10k1_playback_hw_params,
1251 .hw_free = snd_emu10k1_efx_playback_hw_free,
1252 .prepare = snd_emu10k1_efx_playback_prepare,
1253 .trigger = snd_emu10k1_efx_playback_trigger,
1254 .pointer = snd_emu10k1_efx_playback_pointer,
1255 .page = snd_pcm_sgbuf_ops_page,
1256};
1257
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001258int __devinit snd_emu10k1_pcm(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001260 struct snd_pcm *pcm;
1261 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int err;
1263
1264 if (rpcm)
1265 *rpcm = NULL;
1266
1267 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1268 return err;
1269
1270 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1273 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1274
1275 pcm->info_flags = 0;
1276 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1277 strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1278 emu->pcm = pcm;
1279
1280 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1281 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1282 return err;
1283
1284 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1285 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1286
1287 if (rpcm)
1288 *rpcm = pcm;
1289
1290 return 0;
1291}
1292
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001293int __devinit snd_emu10k1_pcm_multi(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001295 struct snd_pcm *pcm;
1296 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 int err;
1298
1299 if (rpcm)
1300 *rpcm = NULL;
1301
1302 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
1303 return err;
1304
1305 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1308
1309 pcm->info_flags = 0;
1310 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1311 strcpy(pcm->name, "Multichannel Playback");
1312 emu->pcm = pcm;
1313
1314 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1315 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1316 return err;
1317
1318 if (rpcm)
1319 *rpcm = pcm;
1320
1321 return 0;
1322}
1323
1324
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001325static struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 .open = snd_emu10k1_capture_mic_open,
1327 .close = snd_emu10k1_capture_mic_close,
1328 .ioctl = snd_pcm_lib_ioctl,
1329 .hw_params = snd_emu10k1_capture_hw_params,
1330 .hw_free = snd_emu10k1_capture_hw_free,
1331 .prepare = snd_emu10k1_capture_prepare,
1332 .trigger = snd_emu10k1_capture_trigger,
1333 .pointer = snd_emu10k1_capture_pointer,
1334};
1335
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001336int __devinit snd_emu10k1_pcm_mic(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001338 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 int err;
1340
1341 if (rpcm)
1342 *rpcm = NULL;
1343
1344 if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
1345 return err;
1346
1347 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1350
1351 pcm->info_flags = 0;
1352 strcpy(pcm->name, "Mic Capture");
1353 emu->pcm_mic = pcm;
1354
1355 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1356
1357 if (rpcm)
1358 *rpcm = pcm;
1359 return 0;
1360}
1361
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001362static 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 -07001363{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001364 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 int nefx = emu->audigy ? 64 : 32;
1366 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1367 uinfo->count = nefx;
1368 uinfo->value.integer.min = 0;
1369 uinfo->value.integer.max = 1;
1370 return 0;
1371}
1372
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001373static 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 -07001374{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001375 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 int nefx = emu->audigy ? 64 : 32;
1377 int idx;
1378
1379 spin_lock_irq(&emu->reg_lock);
1380 for (idx = 0; idx < nefx; idx++)
1381 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1382 spin_unlock_irq(&emu->reg_lock);
1383 return 0;
1384}
1385
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001386static 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 -07001387{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001388 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 unsigned int nval[2], bits;
1390 int nefx = emu->audigy ? 64 : 32;
1391 int nefxb = emu->audigy ? 7 : 6;
1392 int change, idx;
1393
1394 nval[0] = nval[1] = 0;
1395 for (idx = 0, bits = 0; idx < nefx; idx++)
1396 if (ucontrol->value.integer.value[idx]) {
1397 nval[idx / 32] |= 1 << (idx % 32);
1398 bits++;
1399 }
1400
1401 for (idx = 0; idx < nefxb; idx++)
1402 if (1 << idx == bits)
1403 break;
1404
1405 if (idx >= nefxb)
1406 return -EINVAL;
1407
1408 spin_lock_irq(&emu->reg_lock);
1409 change = (nval[0] != emu->efx_voices_mask[0]) ||
1410 (nval[1] != emu->efx_voices_mask[1]);
1411 emu->efx_voices_mask[0] = nval[0];
1412 emu->efx_voices_mask[1] = nval[1];
1413 spin_unlock_irq(&emu->reg_lock);
1414 return change;
1415}
1416
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001417static struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1419 .name = "Captured FX8010 Outputs",
1420 .info = snd_emu10k1_pcm_efx_voices_mask_info,
1421 .get = snd_emu10k1_pcm_efx_voices_mask_get,
1422 .put = snd_emu10k1_pcm_efx_voices_mask_put
1423};
1424
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001425static struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 .open = snd_emu10k1_capture_efx_open,
1427 .close = snd_emu10k1_capture_efx_close,
1428 .ioctl = snd_pcm_lib_ioctl,
1429 .hw_params = snd_emu10k1_capture_hw_params,
1430 .hw_free = snd_emu10k1_capture_hw_free,
1431 .prepare = snd_emu10k1_capture_prepare,
1432 .trigger = snd_emu10k1_capture_trigger,
1433 .pointer = snd_emu10k1_capture_pointer,
1434};
1435
1436
1437/* EFX playback */
1438
1439#define INITIAL_TRAM_SHIFT 14
1440#define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1441
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001442static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001444 struct snd_pcm_substream *substream = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 snd_pcm_period_elapsed(substream);
1446}
1447
1448static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1449 unsigned short *dst_right,
1450 unsigned short *src,
1451 unsigned int count,
1452 unsigned int tram_shift)
1453{
1454 // printk("tram_poke1: dst_left = 0x%p, dst_right = 0x%p, src = 0x%p, count = 0x%x\n", dst_left, dst_right, src, count);
1455 if ((tram_shift & 1) == 0) {
1456 while (count--) {
1457 *dst_left-- = *src++;
1458 *dst_right-- = *src++;
1459 }
1460 } else {
1461 while (count--) {
1462 *dst_right-- = *src++;
1463 *dst_left-- = *src++;
1464 }
1465 }
1466}
1467
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001468static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1469 struct snd_pcm_indirect *rec, size_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001471 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1472 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 unsigned int tram_size = pcm->buffer_size;
1474 unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1475 unsigned int frames = bytes >> 2, count;
1476 unsigned int tram_pos = pcm->tram_pos;
1477 unsigned int tram_shift = pcm->tram_shift;
1478
1479 while (frames > tram_pos) {
1480 count = tram_pos + 1;
1481 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1482 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1483 src, count, tram_shift);
1484 src += count * 2;
1485 frames -= count;
1486 tram_pos = (tram_size / 2) - 1;
1487 tram_shift++;
1488 }
1489 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1490 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1491 src, frames, tram_shift);
1492 tram_pos -= frames;
1493 pcm->tram_pos = tram_pos;
1494 pcm->tram_shift = tram_shift;
1495}
1496
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001497static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001499 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1500 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy);
1503 return 0;
1504}
1505
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001506static int snd_emu10k1_fx8010_playback_hw_params(struct snd_pcm_substream *substream,
1507 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1510}
1511
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001512static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001514 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1515 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 unsigned int i;
1517
1518 for (i = 0; i < pcm->channels; i++)
1519 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1520 snd_pcm_lib_free_pages(substream);
1521 return 0;
1522}
1523
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001524static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001526 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1527 struct snd_pcm_runtime *runtime = substream->runtime;
1528 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 unsigned int i;
1530
1531 // 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);
1532 memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1533 pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1534 pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1535 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1536 pcm->tram_shift = 0;
1537 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */
1538 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */
1539 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1540 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */
1541 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1542 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1543 for (i = 0; i < pcm->channels; i++)
1544 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1545 return 0;
1546}
1547
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001548static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001550 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1551 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 int result = 0;
1553
1554 spin_lock(&emu->reg_lock);
1555 switch (cmd) {
1556 case SNDRV_PCM_TRIGGER_START:
1557 /* follow thru */
1558 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1559#ifdef EMU10K1_SET_AC3_IEC958
1560 {
1561 int i;
1562 for (i = 0; i < 3; i++) {
1563 unsigned int bits;
1564 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1565 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1566 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1567 snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1568 }
1569 }
1570#endif
1571 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1572 if (result < 0)
1573 goto __err;
1574 snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
1575 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1576 break;
1577 case SNDRV_PCM_TRIGGER_STOP:
1578 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1579 snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL;
1580 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1581 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1582 pcm->tram_shift = 0;
1583 break;
1584 default:
1585 result = -EINVAL;
1586 break;
1587 }
1588 __err:
1589 spin_unlock(&emu->reg_lock);
1590 return result;
1591}
1592
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001593static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001595 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1596 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 size_t ptr; /* byte pointer */
1598
1599 if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1600 return 0;
1601 ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1602 return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1603}
1604
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001605static struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1608 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE),
1609 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1610 .rates = SNDRV_PCM_RATE_48000,
1611 .rate_min = 48000,
1612 .rate_max = 48000,
1613 .channels_min = 1,
1614 .channels_max = 1,
1615 .buffer_bytes_max = (128*1024),
1616 .period_bytes_min = 1024,
1617 .period_bytes_max = (128*1024),
1618 .periods_min = 1,
1619 .periods_max = 1024,
1620 .fifo_size = 0,
1621};
1622
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001623static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001625 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1626 struct snd_pcm_runtime *runtime = substream->runtime;
1627 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 runtime->hw = snd_emu10k1_fx8010_playback;
1630 runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1631 runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1632 spin_lock_irq(&emu->reg_lock);
1633 if (pcm->valid == 0) {
1634 spin_unlock_irq(&emu->reg_lock);
1635 return -ENODEV;
1636 }
1637 pcm->opened = 1;
1638 spin_unlock_irq(&emu->reg_lock);
1639 return 0;
1640}
1641
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001642static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001644 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1645 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 spin_lock_irq(&emu->reg_lock);
1648 pcm->opened = 0;
1649 spin_unlock_irq(&emu->reg_lock);
1650 return 0;
1651}
1652
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001653static struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 .open = snd_emu10k1_fx8010_playback_open,
1655 .close = snd_emu10k1_fx8010_playback_close,
1656 .ioctl = snd_pcm_lib_ioctl,
1657 .hw_params = snd_emu10k1_fx8010_playback_hw_params,
1658 .hw_free = snd_emu10k1_fx8010_playback_hw_free,
1659 .prepare = snd_emu10k1_fx8010_playback_prepare,
1660 .trigger = snd_emu10k1_fx8010_playback_trigger,
1661 .pointer = snd_emu10k1_fx8010_playback_pointer,
1662 .ack = snd_emu10k1_fx8010_playback_transfer,
1663};
1664
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001665int __devinit snd_emu10k1_pcm_efx(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Takashi Iwaieb4698f2005-11-17 14:50:13 +01001667 struct snd_pcm *pcm;
1668 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 int err;
1670
1671 if (rpcm)
1672 *rpcm = NULL;
1673
1674 if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1675 return err;
1676
1677 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1680 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1681
1682 pcm->info_flags = 0;
1683 strcpy(pcm->name, "Multichannel Capture/PT Playback");
1684 emu->pcm_efx = pcm;
1685 if (rpcm)
1686 *rpcm = pcm;
1687
1688 /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
1689 * to these
1690 */
1691
1692 /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1693 if (emu->audigy) {
1694 emu->efx_voices_mask[0] = 0;
1695 emu->efx_voices_mask[1] = 0xffff;
1696 } else {
1697 emu->efx_voices_mask[0] = 0xffff0000;
1698 emu->efx_voices_mask[1] = 0;
1699 }
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001700 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1701 if (!kctl)
1702 return -ENOMEM;
1703 kctl->id.device = device;
1704 snd_ctl_add(emu->card, kctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1707
1708 return 0;
1709}