]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/pci/emu10k1/emupcm.c
[ALSA] Changed Jaroslav Kysela's e-mail from perex@suse.cz to perex@perex.cz
[net-next-2.6.git] / sound / pci / emu10k1 / emupcm.c
CommitLineData
1da177e4 1/*
c1017a4c 2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
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
eb4698f3
TI
38static void snd_emu10k1_pcm_interrupt(struct snd_emu10k1 *emu,
39 struct snd_emu10k1_voice *voice)
1da177e4 40{
eb4698f3 41 struct snd_emu10k1_pcm *epcm;
1da177e4
LT
42
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
eb4698f3
TI
56static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu,
57 unsigned int status)
1da177e4
LT
58{
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
eb4698f3
TI
68static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu,
69 unsigned int status)
1da177e4
LT
70{
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
eb4698f3
TI
80static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu,
81 unsigned int status)
1da177e4
LT
82{
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
eb4698f3 92static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(struct snd_pcm_substream *substream)
1da177e4 93{
eb4698f3
TI
94 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
95 struct snd_pcm_runtime *runtime = substream->runtime;
96 struct snd_emu10k1_pcm *epcm = runtime->private_data;
1da177e4
LT
97 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
eb4698f3 109static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm, int voices)
1da177e4
LT
110{
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) {
9f4bd5dd 150 /* printk("pcm_channel_alloc: failed extra: voices=%d, frame=%d\n", voices, frame); */
1da177e4
LT
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
eb4698f3 174static struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
1da177e4
LT
175 .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
eb4698f3 184static struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
1da177e4
LT
185 .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 */
fe5ac9dc 269static inline int emu10k1_ccis(int stereo, int w_16)
1da177e4
LT
270{
271 if (w_16) {
272 return stereo ? 24 : 26;
273 } else {
274 return stereo ? 24*2 : 26*2;
275 }
276}
277
eb4698f3 278static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
1da177e4 279 int master, int extra,
eb4698f3 280 struct snd_emu10k1_voice *evoice,
1da177e4
LT
281 unsigned int start_addr,
282 unsigned int end_addr,
eb4698f3 283 struct snd_emu10k1_pcm_mixer *mix)
1da177e4 284{
eb4698f3
TI
285 struct snd_pcm_substream *substream = evoice->epcm->substream;
286 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
287 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
9f4bd5dd 342 /* setup routing */
1da177e4
LT
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));
9f4bd5dd
JCD
356 /* Stop CA */
357 /* Assumption that PT is already 0 so no harm overwriting */
1da177e4
LT
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));
b0dbdaea
JCD
361 if (emu->card_capabilities->emu1010)
362 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
363 else
364 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
1da177e4
LT
365 if (extra)
366 snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
367 emu10k1_select_interprom(pitch_target) |
368 (w_16 ? 0 : CCCA_8BITSELECT));
369 else
370 snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
371 emu10k1_select_interprom(pitch_target) |
372 (w_16 ? 0 : CCCA_8BITSELECT));
9f4bd5dd 373 /* Clear filter delay memory */
1da177e4
LT
374 snd_emu10k1_ptr_write(emu, Z1, voice, 0);
375 snd_emu10k1_ptr_write(emu, Z2, voice, 0);
9f4bd5dd 376 /* invalidate maps */
1da177e4
LT
377 silent_page = ((unsigned int)emu->silent_page.addr << 1) | MAP_PTI_MASK;
378 snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
379 snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
9f4bd5dd 380 /* modulation envelope */
1da177e4
LT
381 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
382 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
383 snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
384 snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
385 snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
386 snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
387 snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
388 snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
389 snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
390 snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
9f4bd5dd 391 /* volume envelope */
1da177e4
LT
392 snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
393 snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
9f4bd5dd 394 /* filter envelope */
1da177e4 395 snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
9f4bd5dd 396 /* pitch envelope */
1da177e4
LT
397 snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
398
399 spin_unlock_irqrestore(&emu->reg_lock, flags);
400}
401
eb4698f3
TI
402static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
403 struct snd_pcm_hw_params *hw_params)
1da177e4 404{
eb4698f3
TI
405 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
406 struct snd_pcm_runtime *runtime = substream->runtime;
407 struct snd_emu10k1_pcm *epcm = runtime->private_data;
1da177e4
LT
408 int err;
409
410 if ((err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params))) < 0)
411 return err;
412 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
413 return err;
414 if (err > 0) { /* change */
e017fa57 415 int mapped;
1da177e4
LT
416 if (epcm->memblk != NULL)
417 snd_emu10k1_free_pages(emu, epcm->memblk);
e017fa57
TI
418 epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
419 epcm->start_addr = 0;
420 if (! epcm->memblk)
1da177e4 421 return -ENOMEM;
eb4698f3 422 mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
e017fa57
TI
423 if (mapped < 0)
424 return -ENOMEM;
425 epcm->start_addr = mapped << PAGE_SHIFT;
1da177e4
LT
426 }
427 return 0;
428}
429
eb4698f3 430static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
1da177e4 431{
eb4698f3
TI
432 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
433 struct snd_pcm_runtime *runtime = substream->runtime;
434 struct snd_emu10k1_pcm *epcm;
1da177e4
LT
435
436 if (runtime->private_data == NULL)
437 return 0;
438 epcm = runtime->private_data;
439 if (epcm->extra) {
440 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
441 epcm->extra = NULL;
442 }
443 if (epcm->voices[1]) {
444 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
445 epcm->voices[1] = NULL;
446 }
447 if (epcm->voices[0]) {
448 snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
449 epcm->voices[0] = NULL;
450 }
451 if (epcm->memblk) {
452 snd_emu10k1_free_pages(emu, epcm->memblk);
453 epcm->memblk = NULL;
454 epcm->start_addr = 0;
455 }
456 snd_pcm_lib_free_pages(substream);
457 return 0;
458}
459
eb4698f3 460static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
1da177e4 461{
eb4698f3
TI
462 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
463 struct snd_pcm_runtime *runtime = substream->runtime;
464 struct snd_emu10k1_pcm *epcm;
1da177e4
LT
465 int i;
466
467 if (runtime->private_data == NULL)
468 return 0;
469 epcm = runtime->private_data;
470 if (epcm->extra) {
471 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
472 epcm->extra = NULL;
473 }
9f4bd5dd 474 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1da177e4
LT
475 if (epcm->voices[i]) {
476 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
477 epcm->voices[i] = NULL;
478 }
479 }
480 if (epcm->memblk) {
481 snd_emu10k1_free_pages(emu, epcm->memblk);
482 epcm->memblk = NULL;
483 epcm->start_addr = 0;
484 }
485 snd_pcm_lib_free_pages(substream);
486 return 0;
487}
488
eb4698f3 489static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
1da177e4 490{
eb4698f3
TI
491 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
492 struct snd_pcm_runtime *runtime = substream->runtime;
493 struct snd_emu10k1_pcm *epcm = runtime->private_data;
1da177e4
LT
494 unsigned int start_addr, end_addr;
495
496 start_addr = epcm->start_addr;
497 end_addr = snd_pcm_lib_period_bytes(substream);
498 if (runtime->channels == 2) {
499 start_addr >>= 1;
500 end_addr >>= 1;
501 }
502 end_addr += start_addr;
503 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
504 start_addr, end_addr, NULL);
505 start_addr = epcm->start_addr;
506 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
507 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
508 start_addr, end_addr,
509 &emu->pcm_mixer[substream->number]);
510 if (epcm->voices[1])
511 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
512 start_addr, end_addr,
513 &emu->pcm_mixer[substream->number]);
514 return 0;
515}
516
eb4698f3 517static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
1da177e4 518{
eb4698f3
TI
519 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
520 struct snd_pcm_runtime *runtime = substream->runtime;
521 struct snd_emu10k1_pcm *epcm = runtime->private_data;
1da177e4
LT
522 unsigned int start_addr, end_addr;
523 unsigned int channel_size;
524 int i;
525
526 start_addr = epcm->start_addr;
527 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
528
529 /*
530 * the kX driver leaves some space between voices
531 */
532 channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
533
534 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
535 start_addr, start_addr + (channel_size / 2), NULL);
536
537 /* only difference with the master voice is we use it for the pointer */
538 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
539 start_addr, start_addr + channel_size,
540 &emu->efx_pcm_mixer[0]);
541
542 start_addr += channel_size;
543 for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
544 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
545 start_addr, start_addr + channel_size,
546 &emu->efx_pcm_mixer[i]);
547 start_addr += channel_size;
548 }
549
550 return 0;
551}
552
eb4698f3 553static struct snd_pcm_hardware snd_emu10k1_efx_playback =
1da177e4
LT
554{
555 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
556 SNDRV_PCM_INFO_BLOCK_TRANSFER |
09668b44 557 SNDRV_PCM_INFO_RESUME |
1da177e4
LT
558 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
559 .formats = SNDRV_PCM_FMTBIT_S16_LE,
560 .rates = SNDRV_PCM_RATE_48000,
561 .rate_min = 48000,
562 .rate_max = 48000,
563 .channels_min = NUM_EFX_PLAYBACK,
564 .channels_max = NUM_EFX_PLAYBACK,
565 .buffer_bytes_max = (64*1024),
566 .period_bytes_min = 64,
567 .period_bytes_max = (64*1024),
568 .periods_min = 2,
569 .periods_max = 2,
570 .fifo_size = 0,
571};
572
eb4698f3
TI
573static int snd_emu10k1_capture_hw_params(struct snd_pcm_substream *substream,
574 struct snd_pcm_hw_params *hw_params)
1da177e4
LT
575{
576 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
577}
578
eb4698f3 579static int snd_emu10k1_capture_hw_free(struct snd_pcm_substream *substream)
1da177e4
LT
580{
581 return snd_pcm_lib_free_pages(substream);
582}
583
eb4698f3 584static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
1da177e4 585{
eb4698f3
TI
586 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
587 struct snd_pcm_runtime *runtime = substream->runtime;
588 struct snd_emu10k1_pcm *epcm = runtime->private_data;
1da177e4
LT
589 int idx;
590
591 /* zeroing the buffer size will stop capture */
592 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
593 switch (epcm->type) {
594 case CAPTURE_AC97ADC:
595 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
596 break;
597 case CAPTURE_EFX:
598 if (emu->audigy) {
599 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
600 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
601 } else
602 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
603 break;
604 default:
605 break;
606 }
607 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
608 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
609 epcm->capture_bs_val = 0;
610 for (idx = 0; idx < 31; idx++) {
611 if (capture_period_sizes[idx] == epcm->capture_bufsize) {
612 epcm->capture_bs_val = idx + 1;
613 break;
614 }
615 }
616 if (epcm->capture_bs_val == 0) {
617 snd_BUG();
618 epcm->capture_bs_val++;
619 }
620 if (epcm->type == CAPTURE_AC97ADC) {
621 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
622 if (runtime->channels > 1)
623 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
624 epcm->capture_cr_val |= emu->audigy ?
625 snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
626 snd_emu10k1_capture_rate_reg(runtime->rate);
627 }
628 return 0;
629}
630
eb4698f3 631static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
1da177e4 632{
eb4698f3 633 struct snd_pcm_runtime *runtime;
1da177e4
LT
634 unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
635
636 if (evoice == NULL)
637 return;
638 runtime = evoice->epcm->substream->runtime;
639 voice = evoice->number;
640 stereo = (!extra && runtime->channels == 2);
641 sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
642 ccis = emu10k1_ccis(stereo, sample == 0);
9f4bd5dd 643 /* set cs to 2 * number of cache registers beside the invalidated */
1da177e4
LT
644 cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
645 if (cs > 16) cs = 16;
646 for (i = 0; i < cs; i++) {
647 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
648 if (stereo) {
649 snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
650 }
651 }
9f4bd5dd 652 /* reset cache */
1da177e4
LT
653 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
654 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
655 if (stereo) {
656 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
657 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
658 }
9f4bd5dd 659 /* fill cache */
1da177e4
LT
660 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
661 if (stereo) {
662 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
663 }
664}
665
eb4698f3 666static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
1da177e4 667 int master, int extra,
eb4698f3 668 struct snd_emu10k1_pcm_mixer *mix)
1da177e4 669{
eb4698f3
TI
670 struct snd_pcm_substream *substream;
671 struct snd_pcm_runtime *runtime;
1da177e4
LT
672 unsigned int attn, vattn;
673 unsigned int voice, tmp;
674
675 if (evoice == NULL) /* skip second voice for mono */
676 return;
677 substream = evoice->epcm->substream;
678 runtime = substream->runtime;
679 voice = evoice->number;
680
681 attn = extra ? 0 : 0x00ff;
682 tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
683 vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
684 snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
685 snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
686 snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
687 snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
688 snd_emu10k1_voice_clear_loop_stop(emu, voice);
689}
690
eb4698f3 691static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra)
1da177e4 692{
eb4698f3
TI
693 struct snd_pcm_substream *substream;
694 struct snd_pcm_runtime *runtime;
1da177e4
LT
695 unsigned int voice, pitch, pitch_target;
696
697 if (evoice == NULL) /* skip second voice for mono */
698 return;
699 substream = evoice->epcm->substream;
700 runtime = substream->runtime;
701 voice = evoice->number;
702
703 pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
b0dbdaea
JCD
704 if (emu->card_capabilities->emu1010)
705 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
706 else
707 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
1da177e4
LT
708 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
709 if (master || evoice->epcm->type == PLAYBACK_EFX)
710 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
711 snd_emu10k1_ptr_write(emu, IP, voice, pitch);
712 if (extra)
713 snd_emu10k1_voice_intr_enable(emu, voice);
714}
715
eb4698f3 716static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
1da177e4
LT
717{
718 unsigned int voice;
719
720 if (evoice == NULL)
721 return;
722 voice = evoice->number;
723 snd_emu10k1_voice_intr_disable(emu, voice);
724 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
725 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
726 snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
727 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
728 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
729 snd_emu10k1_ptr_write(emu, IP, voice, 0);
730}
731
eb4698f3 732static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
1da177e4
LT
733 int cmd)
734{
eb4698f3
TI
735 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
736 struct snd_pcm_runtime *runtime = substream->runtime;
737 struct snd_emu10k1_pcm *epcm = runtime->private_data;
738 struct snd_emu10k1_pcm_mixer *mix;
1da177e4
LT
739 int result = 0;
740
9f4bd5dd 741 /* printk("trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n", (int)emu, cmd, substream->ops->pointer(substream)); */
1da177e4
LT
742 spin_lock(&emu->reg_lock);
743 switch (cmd) {
744 case SNDRV_PCM_TRIGGER_START:
745 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */
746 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
747 /* follow thru */
748 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
09668b44 749 case SNDRV_PCM_TRIGGER_RESUME:
1da177e4
LT
750 mix = &emu->pcm_mixer[substream->number];
751 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
752 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
753 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
754 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
755 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
756 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
757 epcm->running = 1;
758 break;
759 case SNDRV_PCM_TRIGGER_STOP:
760 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
09668b44 761 case SNDRV_PCM_TRIGGER_SUSPEND:
1da177e4
LT
762 epcm->running = 0;
763 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
764 snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
765 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
766 break;
767 default:
768 result = -EINVAL;
769 break;
770 }
771 spin_unlock(&emu->reg_lock);
772 return result;
773}
774
eb4698f3 775static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
1da177e4
LT
776 int cmd)
777{
eb4698f3
TI
778 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
779 struct snd_pcm_runtime *runtime = substream->runtime;
780 struct snd_emu10k1_pcm *epcm = runtime->private_data;
1da177e4
LT
781 int result = 0;
782
783 spin_lock(&emu->reg_lock);
784 switch (cmd) {
785 case SNDRV_PCM_TRIGGER_START:
09668b44 786 case SNDRV_PCM_TRIGGER_RESUME:
9f4bd5dd 787 /* hmm this should cause full and half full interrupt to be raised? */
1da177e4
LT
788 outl(epcm->capture_ipr, emu->port + IPR);
789 snd_emu10k1_intr_enable(emu, epcm->capture_inte);
9f4bd5dd 790 /* printk("adccr = 0x%x, adcbs = 0x%x\n", epcm->adccr, epcm->adcbs); */
1da177e4
LT
791 switch (epcm->type) {
792 case CAPTURE_AC97ADC:
793 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
794 break;
795 case CAPTURE_EFX:
796 if (emu->audigy) {
797 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
798 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
9f4bd5dd 799 snd_printdd("cr_val=0x%x, cr_val2=0x%x\n", epcm->capture_cr_val, epcm->capture_cr_val2);
1da177e4
LT
800 } else
801 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
802 break;
803 default:
804 break;
805 }
806 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
807 epcm->running = 1;
808 epcm->first_ptr = 1;
809 break;
810 case SNDRV_PCM_TRIGGER_STOP:
09668b44 811 case SNDRV_PCM_TRIGGER_SUSPEND:
1da177e4
LT
812 epcm->running = 0;
813 snd_emu10k1_intr_disable(emu, epcm->capture_inte);
814 outl(epcm->capture_ipr, emu->port + IPR);
815 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
816 switch (epcm->type) {
817 case CAPTURE_AC97ADC:
818 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
819 break;
820 case CAPTURE_EFX:
821 if (emu->audigy) {
822 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
823 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
824 } else
825 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
826 break;
827 default:
828 break;
829 }
830 break;
831 default:
832 result = -EINVAL;
833 }
834 spin_unlock(&emu->reg_lock);
835 return result;
836}
837
eb4698f3 838static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
1da177e4 839{
eb4698f3
TI
840 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
841 struct snd_pcm_runtime *runtime = substream->runtime;
842 struct snd_emu10k1_pcm *epcm = runtime->private_data;
1da177e4
LT
843 unsigned int ptr;
844
845 if (!epcm->running)
846 return 0;
847 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
848#if 0 /* Perex's code */
849 ptr += runtime->buffer_size;
850 ptr -= epcm->ccca_start_addr;
851 ptr %= runtime->buffer_size;
852#else /* EMU10K1 Open Source code from Creative */
853 if (ptr < epcm->ccca_start_addr)
854 ptr += runtime->buffer_size - epcm->ccca_start_addr;
855 else {
856 ptr -= epcm->ccca_start_addr;
857 if (ptr >= runtime->buffer_size)
858 ptr -= runtime->buffer_size;
859 }
860#endif
9f4bd5dd 861 /* printk("ptr = 0x%x, buffer_size = 0x%x, period_size = 0x%x\n", ptr, runtime->buffer_size, runtime->period_size); */
1da177e4
LT
862 return ptr;
863}
864
865
eb4698f3 866static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
1da177e4
LT
867 int cmd)
868{
eb4698f3
TI
869 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
870 struct snd_pcm_runtime *runtime = substream->runtime;
871 struct snd_emu10k1_pcm *epcm = runtime->private_data;
1da177e4
LT
872 int i;
873 int result = 0;
874
875 spin_lock(&emu->reg_lock);
876 switch (cmd) {
877 case SNDRV_PCM_TRIGGER_START:
9f4bd5dd 878 /* prepare voices */
1da177e4
LT
879 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
880 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
881 }
882 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
883
884 /* follow thru */
885 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
09668b44 886 case SNDRV_PCM_TRIGGER_RESUME:
1da177e4
LT
887 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
888 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
889 &emu->efx_pcm_mixer[0]);
890 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
891 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
892 &emu->efx_pcm_mixer[i]);
893 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
894 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
895 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
896 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
897 epcm->running = 1;
898 break;
09668b44 899 case SNDRV_PCM_TRIGGER_SUSPEND:
1da177e4
LT
900 case SNDRV_PCM_TRIGGER_STOP:
901 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
902 epcm->running = 0;
903 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
904 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
905 }
906 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
907 break;
908 default:
909 result = -EINVAL;
910 break;
911 }
912 spin_unlock(&emu->reg_lock);
913 return result;
914}
915
916
eb4698f3 917static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
1da177e4 918{
eb4698f3
TI
919 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
920 struct snd_pcm_runtime *runtime = substream->runtime;
921 struct snd_emu10k1_pcm *epcm = runtime->private_data;
1da177e4
LT
922 unsigned int ptr;
923
924 if (!epcm->running)
925 return 0;
926 if (epcm->first_ptr) {
9f4bd5dd 927 udelay(50); /* hack, it takes awhile until capture is started */
1da177e4
LT
928 epcm->first_ptr = 0;
929 }
930 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
931 return bytes_to_frames(runtime, ptr);
932}
933
934/*
935 * Playback support device description
936 */
937
eb4698f3 938static struct snd_pcm_hardware snd_emu10k1_playback =
1da177e4
LT
939{
940 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
941 SNDRV_PCM_INFO_BLOCK_TRANSFER |
09668b44 942 SNDRV_PCM_INFO_RESUME |
1da177e4
LT
943 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
944 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
945 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
946 .rate_min = 4000,
947 .rate_max = 96000,
948 .channels_min = 1,
949 .channels_max = 2,
950 .buffer_bytes_max = (128*1024),
951 .period_bytes_min = 64,
952 .period_bytes_max = (128*1024),
953 .periods_min = 1,
954 .periods_max = 1024,
955 .fifo_size = 0,
956};
957
958/*
959 * Capture support device description
960 */
961
eb4698f3 962static struct snd_pcm_hardware snd_emu10k1_capture =
1da177e4
LT
963{
964 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
965 SNDRV_PCM_INFO_BLOCK_TRANSFER |
09668b44 966 SNDRV_PCM_INFO_RESUME |
1da177e4
LT
967 SNDRV_PCM_INFO_MMAP_VALID),
968 .formats = SNDRV_PCM_FMTBIT_S16_LE,
969 .rates = SNDRV_PCM_RATE_8000_48000,
970 .rate_min = 8000,
971 .rate_max = 48000,
972 .channels_min = 1,
973 .channels_max = 2,
974 .buffer_bytes_max = (64*1024),
975 .period_bytes_min = 384,
976 .period_bytes_max = (64*1024),
977 .periods_min = 2,
978 .periods_max = 2,
979 .fifo_size = 0,
980};
981
9f4bd5dd
JCD
982static struct snd_pcm_hardware snd_emu10k1_capture_efx =
983{
984 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
985 SNDRV_PCM_INFO_BLOCK_TRANSFER |
986 SNDRV_PCM_INFO_RESUME |
987 SNDRV_PCM_INFO_MMAP_VALID),
988 .formats = SNDRV_PCM_FMTBIT_S16_LE,
989 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
990 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
991 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
992 .rate_min = 44100,
993 .rate_max = 192000,
994 .channels_min = 8,
995 .channels_max = 8,
996 .buffer_bytes_max = (64*1024),
997 .period_bytes_min = 384,
998 .period_bytes_max = (64*1024),
999 .periods_min = 2,
1000 .periods_max = 2,
1001 .fifo_size = 0,
1002};
1003
1da177e4
LT
1004/*
1005 *
1006 */
1007
eb4698f3 1008static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
1da177e4 1009{
eb4698f3 1010 struct snd_ctl_elem_id id;
1da177e4 1011
7c22f1aa
TI
1012 if (! kctl)
1013 return;
1da177e4
LT
1014 if (activate)
1015 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1016 else
1017 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1018 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
1019 SNDRV_CTL_EVENT_MASK_INFO,
1020 snd_ctl_build_ioff(&id, kctl, idx));
1021}
1022
eb4698f3 1023static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1da177e4
LT
1024{
1025 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
1026 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
1027 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
1028}
1029
eb4698f3 1030static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1da177e4
LT
1031{
1032 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
1033 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
1034 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
1035}
1036
eb4698f3 1037static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
1da177e4 1038{
4d572776 1039 kfree(runtime->private_data);
1da177e4
LT
1040}
1041
eb4698f3 1042static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
1da177e4 1043{
eb4698f3
TI
1044 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1045 struct snd_emu10k1_pcm_mixer *mix;
1da177e4
LT
1046 int i;
1047
9f4bd5dd 1048 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1da177e4
LT
1049 mix = &emu->efx_pcm_mixer[i];
1050 mix->epcm = NULL;
1051 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1052 }
1053 return 0;
1054}
1055
eb4698f3 1056static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
1da177e4 1057{
eb4698f3
TI
1058 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1059 struct snd_emu10k1_pcm *epcm;
1060 struct snd_emu10k1_pcm_mixer *mix;
1061 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1062 int i;
1063
e560d8d8 1064 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1da177e4
LT
1065 if (epcm == NULL)
1066 return -ENOMEM;
1067 epcm->emu = emu;
1068 epcm->type = PLAYBACK_EFX;
1069 epcm->substream = substream;
1070
1071 emu->pcm_playback_efx_substream = substream;
1072
1073 runtime->private_data = epcm;
1074 runtime->private_free = snd_emu10k1_pcm_free_substream;
1075 runtime->hw = snd_emu10k1_efx_playback;
1076
9f4bd5dd 1077 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1da177e4
LT
1078 mix = &emu->efx_pcm_mixer[i];
1079 mix->send_routing[0][0] = i;
1080 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1081 mix->send_volume[0][0] = 255;
1082 mix->attn[0] = 0xffff;
1083 mix->epcm = epcm;
1084 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1085 }
1086 return 0;
1087}
1088
eb4698f3 1089static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
1da177e4 1090{
eb4698f3
TI
1091 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1092 struct snd_emu10k1_pcm *epcm;
1093 struct snd_emu10k1_pcm_mixer *mix;
1094 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1095 int i, err;
1096
e560d8d8 1097 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1da177e4
LT
1098 if (epcm == NULL)
1099 return -ENOMEM;
1100 epcm->emu = emu;
1101 epcm->type = PLAYBACK_EMUVOICE;
1102 epcm->substream = substream;
1103 runtime->private_data = epcm;
1104 runtime->private_free = snd_emu10k1_pcm_free_substream;
1105 runtime->hw = snd_emu10k1_playback;
1106 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
1107 kfree(epcm);
1108 return err;
1109 }
1110 if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
1111 kfree(epcm);
1112 return err;
1113 }
1114 mix = &emu->pcm_mixer[substream->number];
1115 for (i = 0; i < 4; i++)
1116 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1117 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1118 mix->send_volume[0][0] = mix->send_volume[0][1] =
1119 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1120 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1121 mix->epcm = epcm;
1122 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1123 return 0;
1124}
1125
eb4698f3 1126static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
1da177e4 1127{
eb4698f3
TI
1128 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1129 struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
1da177e4
LT
1130
1131 mix->epcm = NULL;
1132 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1133 return 0;
1134}
1135
eb4698f3 1136static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
1da177e4 1137{
eb4698f3
TI
1138 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1139 struct snd_pcm_runtime *runtime = substream->runtime;
1140 struct snd_emu10k1_pcm *epcm;
1da177e4 1141
e560d8d8 1142 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1da177e4
LT
1143 if (epcm == NULL)
1144 return -ENOMEM;
1145 epcm->emu = emu;
1146 epcm->type = CAPTURE_AC97ADC;
1147 epcm->substream = substream;
1148 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1149 epcm->capture_inte = INTE_ADCBUFENABLE;
1150 epcm->capture_ba_reg = ADCBA;
1151 epcm->capture_bs_reg = ADCBS;
1152 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1153 runtime->private_data = epcm;
1154 runtime->private_free = snd_emu10k1_pcm_free_substream;
1155 runtime->hw = snd_emu10k1_capture;
1156 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1157 emu->pcm_capture_substream = substream;
1158 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1159 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1160 return 0;
1161}
1162
eb4698f3 1163static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
1da177e4 1164{
eb4698f3 1165 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1da177e4
LT
1166
1167 emu->capture_interrupt = NULL;
1168 emu->pcm_capture_substream = NULL;
1169 return 0;
1170}
1171
eb4698f3 1172static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
1da177e4 1173{
eb4698f3
TI
1174 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1175 struct snd_emu10k1_pcm *epcm;
1176 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4 1177
e560d8d8 1178 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1da177e4
LT
1179 if (epcm == NULL)
1180 return -ENOMEM;
1181 epcm->emu = emu;
1182 epcm->type = CAPTURE_AC97MIC;
1183 epcm->substream = substream;
1184 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1185 epcm->capture_inte = INTE_MICBUFENABLE;
1186 epcm->capture_ba_reg = MICBA;
1187 epcm->capture_bs_reg = MICBS;
1188 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1189 substream->runtime->private_data = epcm;
1190 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1191 runtime->hw = snd_emu10k1_capture;
1192 runtime->hw.rates = SNDRV_PCM_RATE_8000;
1193 runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1194 runtime->hw.channels_min = 1;
1195 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1196 emu->pcm_capture_mic_substream = substream;
1197 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1198 return 0;
1199}
1200
eb4698f3 1201static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
1da177e4 1202{
eb4698f3 1203 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1da177e4
LT
1204
1205 emu->capture_interrupt = NULL;
1206 emu->pcm_capture_mic_substream = NULL;
1207 return 0;
1208}
1209
eb4698f3 1210static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
1da177e4 1211{
eb4698f3
TI
1212 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1213 struct snd_emu10k1_pcm *epcm;
1214 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1215 int nefx = emu->audigy ? 64 : 32;
1216 int idx;
1217
e560d8d8 1218 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1da177e4
LT
1219 if (epcm == NULL)
1220 return -ENOMEM;
1221 epcm->emu = emu;
1222 epcm->type = CAPTURE_EFX;
1223 epcm->substream = substream;
1224 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1225 epcm->capture_inte = INTE_EFXBUFENABLE;
1226 epcm->capture_ba_reg = FXBA;
1227 epcm->capture_bs_reg = FXBS;
1228 epcm->capture_idx_reg = FXIDX;
1229 substream->runtime->private_data = epcm;
1230 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
9f4bd5dd 1231 runtime->hw = snd_emu10k1_capture_efx;
1da177e4
LT
1232 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1233 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1234 spin_lock_irq(&emu->reg_lock);
9f4bd5dd 1235 if (emu->card_capabilities->emu1010) {
13d45709
PH
1236 /* Nb. of channels has been increased to 16 */
1237 /* TODO
9f4bd5dd
JCD
1238 * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
1239 * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1240 * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1241 * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
1242 * rate_min = 44100,
1243 * rate_max = 192000,
13d45709
PH
1244 * channels_min = 16,
1245 * channels_max = 16,
9f4bd5dd
JCD
1246 * Need to add mixer control to fix sample rate
1247 *
13d45709 1248 * There are 32 mono channels of 16bits each.
9f4bd5dd
JCD
1249 * 24bit Audio uses 2x channels over 16bit
1250 * 96kHz uses 2x channels over 48kHz
1251 * 192kHz uses 4x channels over 48kHz
13d45709
PH
1252 * So, for 48kHz 24bit, one has 16 channels
1253 * for 96kHz 24bit, one has 8 channels
1254 * for 192kHz 24bit, one has 4 channels
1255 *
9f4bd5dd
JCD
1256 */
1257#if 1
b0dbdaea
JCD
1258 switch (emu->emu1010.internal_clock) {
1259 case 0:
1260 /* For 44.1kHz */
1261 runtime->hw.rates = SNDRV_PCM_RATE_44100;
1262 runtime->hw.rate_min = runtime->hw.rate_max = 44100;
13d45709
PH
1263 runtime->hw.channels_min =
1264 runtime->hw.channels_max = 16;
b0dbdaea
JCD
1265 break;
1266 case 1:
1267 /* For 48kHz */
1268 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1269 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
13d45709
PH
1270 runtime->hw.channels_min =
1271 runtime->hw.channels_max = 16;
b0dbdaea
JCD
1272 break;
1273 };
9f4bd5dd
JCD
1274#endif
1275#if 0
1276 /* For 96kHz */
1277 runtime->hw.rates = SNDRV_PCM_RATE_96000;
1278 runtime->hw.rate_min = runtime->hw.rate_max = 96000;
1279 runtime->hw.channels_min = runtime->hw.channels_max = 4;
1280#endif
1281#if 0
1282 /* For 192kHz */
1283 runtime->hw.rates = SNDRV_PCM_RATE_192000;
1284 runtime->hw.rate_min = runtime->hw.rate_max = 192000;
1285 runtime->hw.channels_min = runtime->hw.channels_max = 2;
1286#endif
1287 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1288 /* efx_voices_mask[0] is expected to be zero
13d45709 1289 * efx_voices_mask[1] is expected to have 32bits set
9f4bd5dd
JCD
1290 */
1291 } else {
1292 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1293 for (idx = 0; idx < nefx; idx++) {
1294 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1295 runtime->hw.channels_min++;
1296 runtime->hw.channels_max++;
1297 }
1da177e4
LT
1298 }
1299 }
1300 epcm->capture_cr_val = emu->efx_voices_mask[0];
1301 epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1302 spin_unlock_irq(&emu->reg_lock);
1303 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1304 emu->pcm_capture_efx_substream = substream;
1305 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1306 return 0;
1307}
1308
eb4698f3 1309static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
1da177e4 1310{
eb4698f3 1311 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1da177e4
LT
1312
1313 emu->capture_interrupt = NULL;
1314 emu->pcm_capture_efx_substream = NULL;
1315 return 0;
1316}
1317
eb4698f3 1318static struct snd_pcm_ops snd_emu10k1_playback_ops = {
1da177e4
LT
1319 .open = snd_emu10k1_playback_open,
1320 .close = snd_emu10k1_playback_close,
1321 .ioctl = snd_pcm_lib_ioctl,
1322 .hw_params = snd_emu10k1_playback_hw_params,
1323 .hw_free = snd_emu10k1_playback_hw_free,
1324 .prepare = snd_emu10k1_playback_prepare,
1325 .trigger = snd_emu10k1_playback_trigger,
1326 .pointer = snd_emu10k1_playback_pointer,
1327 .page = snd_pcm_sgbuf_ops_page,
1328};
1329
eb4698f3 1330static struct snd_pcm_ops snd_emu10k1_capture_ops = {
1da177e4
LT
1331 .open = snd_emu10k1_capture_open,
1332 .close = snd_emu10k1_capture_close,
1333 .ioctl = snd_pcm_lib_ioctl,
1334 .hw_params = snd_emu10k1_capture_hw_params,
1335 .hw_free = snd_emu10k1_capture_hw_free,
1336 .prepare = snd_emu10k1_capture_prepare,
1337 .trigger = snd_emu10k1_capture_trigger,
1338 .pointer = snd_emu10k1_capture_pointer,
1339};
1340
1341/* EFX playback */
eb4698f3 1342static struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
1da177e4
LT
1343 .open = snd_emu10k1_efx_playback_open,
1344 .close = snd_emu10k1_efx_playback_close,
1345 .ioctl = snd_pcm_lib_ioctl,
1346 .hw_params = snd_emu10k1_playback_hw_params,
1347 .hw_free = snd_emu10k1_efx_playback_hw_free,
1348 .prepare = snd_emu10k1_efx_playback_prepare,
1349 .trigger = snd_emu10k1_efx_playback_trigger,
1350 .pointer = snd_emu10k1_efx_playback_pointer,
1351 .page = snd_pcm_sgbuf_ops_page,
1352};
1353
eb4698f3 1354int __devinit snd_emu10k1_pcm(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1da177e4 1355{
eb4698f3
TI
1356 struct snd_pcm *pcm;
1357 struct snd_pcm_substream *substream;
1da177e4
LT
1358 int err;
1359
1360 if (rpcm)
1361 *rpcm = NULL;
1362
1363 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1364 return err;
1365
1366 pcm->private_data = emu;
1da177e4
LT
1367
1368 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1369 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1370
1371 pcm->info_flags = 0;
1372 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1373 strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1374 emu->pcm = pcm;
1375
1376 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1377 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1378 return err;
1379
1380 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1381 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1382
1383 if (rpcm)
1384 *rpcm = pcm;
1385
1386 return 0;
1387}
1388
eb4698f3 1389int __devinit snd_emu10k1_pcm_multi(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1da177e4 1390{
eb4698f3
TI
1391 struct snd_pcm *pcm;
1392 struct snd_pcm_substream *substream;
1da177e4
LT
1393 int err;
1394
1395 if (rpcm)
1396 *rpcm = NULL;
1397
1398 if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
1399 return err;
1400
1401 pcm->private_data = emu;
1da177e4
LT
1402
1403 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1404
1405 pcm->info_flags = 0;
1406 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1407 strcpy(pcm->name, "Multichannel Playback");
09668b44 1408 emu->pcm_multi = pcm;
1da177e4
LT
1409
1410 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1411 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1412 return err;
1413
1414 if (rpcm)
1415 *rpcm = pcm;
1416
1417 return 0;
1418}
1419
1420
eb4698f3 1421static struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
1da177e4
LT
1422 .open = snd_emu10k1_capture_mic_open,
1423 .close = snd_emu10k1_capture_mic_close,
1424 .ioctl = snd_pcm_lib_ioctl,
1425 .hw_params = snd_emu10k1_capture_hw_params,
1426 .hw_free = snd_emu10k1_capture_hw_free,
1427 .prepare = snd_emu10k1_capture_prepare,
1428 .trigger = snd_emu10k1_capture_trigger,
1429 .pointer = snd_emu10k1_capture_pointer,
1430};
1431
eb4698f3 1432int __devinit snd_emu10k1_pcm_mic(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1da177e4 1433{
eb4698f3 1434 struct snd_pcm *pcm;
1da177e4
LT
1435 int err;
1436
1437 if (rpcm)
1438 *rpcm = NULL;
1439
1440 if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
1441 return err;
1442
1443 pcm->private_data = emu;
1da177e4
LT
1444
1445 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1446
1447 pcm->info_flags = 0;
1448 strcpy(pcm->name, "Mic Capture");
1449 emu->pcm_mic = pcm;
1450
1451 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1452
1453 if (rpcm)
1454 *rpcm = pcm;
1455 return 0;
1456}
1457
eb4698f3 1458static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4 1459{
eb4698f3 1460 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1461 int nefx = emu->audigy ? 64 : 32;
1462 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1463 uinfo->count = nefx;
1464 uinfo->value.integer.min = 0;
1465 uinfo->value.integer.max = 1;
1466 return 0;
1467}
1468
eb4698f3 1469static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 1470{
eb4698f3 1471 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1472 int nefx = emu->audigy ? 64 : 32;
1473 int idx;
1474
1475 spin_lock_irq(&emu->reg_lock);
1476 for (idx = 0; idx < nefx; idx++)
1477 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1478 spin_unlock_irq(&emu->reg_lock);
1479 return 0;
1480}
1481
eb4698f3 1482static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 1483{
eb4698f3 1484 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1485 unsigned int nval[2], bits;
1486 int nefx = emu->audigy ? 64 : 32;
1487 int nefxb = emu->audigy ? 7 : 6;
1488 int change, idx;
1489
1490 nval[0] = nval[1] = 0;
1491 for (idx = 0, bits = 0; idx < nefx; idx++)
1492 if (ucontrol->value.integer.value[idx]) {
1493 nval[idx / 32] |= 1 << (idx % 32);
1494 bits++;
1495 }
1496
1497 for (idx = 0; idx < nefxb; idx++)
1498 if (1 << idx == bits)
1499 break;
1500
1501 if (idx >= nefxb)
1502 return -EINVAL;
1503
1504 spin_lock_irq(&emu->reg_lock);
1505 change = (nval[0] != emu->efx_voices_mask[0]) ||
1506 (nval[1] != emu->efx_voices_mask[1]);
1507 emu->efx_voices_mask[0] = nval[0];
1508 emu->efx_voices_mask[1] = nval[1];
1509 spin_unlock_irq(&emu->reg_lock);
1510 return change;
1511}
1512
eb4698f3 1513static struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
1da177e4
LT
1514 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1515 .name = "Captured FX8010 Outputs",
1516 .info = snd_emu10k1_pcm_efx_voices_mask_info,
1517 .get = snd_emu10k1_pcm_efx_voices_mask_get,
1518 .put = snd_emu10k1_pcm_efx_voices_mask_put
1519};
1520
eb4698f3 1521static struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
1da177e4
LT
1522 .open = snd_emu10k1_capture_efx_open,
1523 .close = snd_emu10k1_capture_efx_close,
1524 .ioctl = snd_pcm_lib_ioctl,
1525 .hw_params = snd_emu10k1_capture_hw_params,
1526 .hw_free = snd_emu10k1_capture_hw_free,
1527 .prepare = snd_emu10k1_capture_prepare,
1528 .trigger = snd_emu10k1_capture_trigger,
1529 .pointer = snd_emu10k1_capture_pointer,
1530};
1531
1532
1533/* EFX playback */
1534
1535#define INITIAL_TRAM_SHIFT 14
1536#define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1537
eb4698f3 1538static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
1da177e4 1539{
eb4698f3 1540 struct snd_pcm_substream *substream = private_data;
1da177e4
LT
1541 snd_pcm_period_elapsed(substream);
1542}
1543
1544static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1545 unsigned short *dst_right,
1546 unsigned short *src,
1547 unsigned int count,
1548 unsigned int tram_shift)
1549{
9f4bd5dd 1550 /* printk("tram_poke1: dst_left = 0x%p, dst_right = 0x%p, src = 0x%p, count = 0x%x\n", dst_left, dst_right, src, count); */
1da177e4
LT
1551 if ((tram_shift & 1) == 0) {
1552 while (count--) {
1553 *dst_left-- = *src++;
1554 *dst_right-- = *src++;
1555 }
1556 } else {
1557 while (count--) {
1558 *dst_right-- = *src++;
1559 *dst_left-- = *src++;
1560 }
1561 }
1562}
1563
eb4698f3
TI
1564static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1565 struct snd_pcm_indirect *rec, size_t bytes)
1da177e4 1566{
eb4698f3
TI
1567 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1568 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1da177e4
LT
1569 unsigned int tram_size = pcm->buffer_size;
1570 unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1571 unsigned int frames = bytes >> 2, count;
1572 unsigned int tram_pos = pcm->tram_pos;
1573 unsigned int tram_shift = pcm->tram_shift;
1574
1575 while (frames > tram_pos) {
1576 count = tram_pos + 1;
1577 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1578 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1579 src, count, tram_shift);
1580 src += count * 2;
1581 frames -= count;
1582 tram_pos = (tram_size / 2) - 1;
1583 tram_shift++;
1584 }
1585 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1586 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1587 src, frames, tram_shift);
1588 tram_pos -= frames;
1589 pcm->tram_pos = tram_pos;
1590 pcm->tram_shift = tram_shift;
1591}
1592
eb4698f3 1593static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
1da177e4 1594{
eb4698f3
TI
1595 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1596 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1da177e4
LT
1597
1598 snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy);
1599 return 0;
1600}
1601
eb4698f3
TI
1602static int snd_emu10k1_fx8010_playback_hw_params(struct snd_pcm_substream *substream,
1603 struct snd_pcm_hw_params *hw_params)
1da177e4
LT
1604{
1605 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1606}
1607
eb4698f3 1608static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
1da177e4 1609{
eb4698f3
TI
1610 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1611 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1da177e4
LT
1612 unsigned int i;
1613
1614 for (i = 0; i < pcm->channels; i++)
1615 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1616 snd_pcm_lib_free_pages(substream);
1617 return 0;
1618}
1619
eb4698f3 1620static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
1da177e4 1621{
eb4698f3
TI
1622 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1623 struct snd_pcm_runtime *runtime = substream->runtime;
1624 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1da177e4
LT
1625 unsigned int i;
1626
9f4bd5dd 1627 /* 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); */
1da177e4
LT
1628 memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1629 pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1630 pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1631 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1632 pcm->tram_shift = 0;
1633 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */
1634 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */
1635 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1636 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */
1637 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1638 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1639 for (i = 0; i < pcm->channels; i++)
1640 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1641 return 0;
1642}
1643
eb4698f3 1644static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
1da177e4 1645{
eb4698f3
TI
1646 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1647 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1da177e4
LT
1648 int result = 0;
1649
1650 spin_lock(&emu->reg_lock);
1651 switch (cmd) {
1652 case SNDRV_PCM_TRIGGER_START:
1653 /* follow thru */
1654 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
09668b44 1655 case SNDRV_PCM_TRIGGER_RESUME:
1da177e4
LT
1656#ifdef EMU10K1_SET_AC3_IEC958
1657 {
1658 int i;
1659 for (i = 0; i < 3; i++) {
1660 unsigned int bits;
1661 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1662 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1663 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1664 snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1665 }
1666 }
1667#endif
1668 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1669 if (result < 0)
1670 goto __err;
1671 snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
1672 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1673 break;
1674 case SNDRV_PCM_TRIGGER_STOP:
1675 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
09668b44 1676 case SNDRV_PCM_TRIGGER_SUSPEND:
1da177e4
LT
1677 snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL;
1678 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1679 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1680 pcm->tram_shift = 0;
1681 break;
1682 default:
1683 result = -EINVAL;
1684 break;
1685 }
1686 __err:
1687 spin_unlock(&emu->reg_lock);
1688 return result;
1689}
1690
eb4698f3 1691static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
1da177e4 1692{
eb4698f3
TI
1693 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1694 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1da177e4
LT
1695 size_t ptr; /* byte pointer */
1696
1697 if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1698 return 0;
1699 ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1700 return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1701}
1702
eb4698f3 1703static struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
1da177e4
LT
1704{
1705 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
09668b44 1706 SNDRV_PCM_INFO_RESUME |
1da177e4
LT
1707 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE),
1708 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1709 .rates = SNDRV_PCM_RATE_48000,
1710 .rate_min = 48000,
1711 .rate_max = 48000,
1712 .channels_min = 1,
1713 .channels_max = 1,
1714 .buffer_bytes_max = (128*1024),
1715 .period_bytes_min = 1024,
1716 .period_bytes_max = (128*1024),
1717 .periods_min = 1,
1718 .periods_max = 1024,
1719 .fifo_size = 0,
1720};
1721
eb4698f3 1722static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
1da177e4 1723{
eb4698f3
TI
1724 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1725 struct snd_pcm_runtime *runtime = substream->runtime;
1726 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1da177e4
LT
1727
1728 runtime->hw = snd_emu10k1_fx8010_playback;
1729 runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1730 runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1731 spin_lock_irq(&emu->reg_lock);
1732 if (pcm->valid == 0) {
1733 spin_unlock_irq(&emu->reg_lock);
1734 return -ENODEV;
1735 }
1736 pcm->opened = 1;
1737 spin_unlock_irq(&emu->reg_lock);
1738 return 0;
1739}
1740
eb4698f3 1741static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
1da177e4 1742{
eb4698f3
TI
1743 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1744 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1da177e4
LT
1745
1746 spin_lock_irq(&emu->reg_lock);
1747 pcm->opened = 0;
1748 spin_unlock_irq(&emu->reg_lock);
1749 return 0;
1750}
1751
eb4698f3 1752static struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
1da177e4
LT
1753 .open = snd_emu10k1_fx8010_playback_open,
1754 .close = snd_emu10k1_fx8010_playback_close,
1755 .ioctl = snd_pcm_lib_ioctl,
1756 .hw_params = snd_emu10k1_fx8010_playback_hw_params,
1757 .hw_free = snd_emu10k1_fx8010_playback_hw_free,
1758 .prepare = snd_emu10k1_fx8010_playback_prepare,
1759 .trigger = snd_emu10k1_fx8010_playback_trigger,
1760 .pointer = snd_emu10k1_fx8010_playback_pointer,
1761 .ack = snd_emu10k1_fx8010_playback_transfer,
1762};
1763
eb4698f3 1764int __devinit snd_emu10k1_pcm_efx(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1da177e4 1765{
eb4698f3
TI
1766 struct snd_pcm *pcm;
1767 struct snd_kcontrol *kctl;
1da177e4
LT
1768 int err;
1769
1770 if (rpcm)
1771 *rpcm = NULL;
1772
1773 if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1774 return err;
1775
1776 pcm->private_data = emu;
1da177e4
LT
1777
1778 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1779 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1780
1781 pcm->info_flags = 0;
1782 strcpy(pcm->name, "Multichannel Capture/PT Playback");
1783 emu->pcm_efx = pcm;
1784 if (rpcm)
1785 *rpcm = pcm;
1786
1787 /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
1788 * to these
1789 */
1790
1791 /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1792 if (emu->audigy) {
1793 emu->efx_voices_mask[0] = 0;
13d45709
PH
1794 if (emu->card_capabilities->emu1010)
1795 /* Pavel Hofman - 32 voices will be used for
1796 * capture (write mode) -
1797 * each bit = corresponding voice
1798 */
1799 emu->efx_voices_mask[1] = 0xffffffff;
1800 else
1801 emu->efx_voices_mask[1] = 0xffff;
1da177e4
LT
1802 } else {
1803 emu->efx_voices_mask[0] = 0xffff0000;
1804 emu->efx_voices_mask[1] = 0;
1805 }
13d45709
PH
1806 /* For emu1010, the control has to set 32 upper bits (voices)
1807 * out of the 64 bits (voices) to true for the 16-channels capture
1808 * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
1809 * is already defined but the snd_emu10k1_pcm_efx_voices_mask
1810 * control can override this register's value.
1811 */
67ed4161
CL
1812 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1813 if (!kctl)
1814 return -ENOMEM;
1815 kctl->id.device = device;
1816 snd_ctl_add(emu->card, kctl);
1da177e4
LT
1817
1818 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1819
1820 return 0;
1821}