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