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