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