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