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