Merge master.kernel.org:/home/rmk/linux-2.6-arm
[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(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         kfree(runtime->private_data);
995 }
996
997 static int snd_emu10k1_efx_playback_close(snd_pcm_substream_t * substream)
998 {
999         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1000         emu10k1_pcm_mixer_t *mix;
1001         int i;
1002
1003         for (i=0; i < NUM_EFX_PLAYBACK; i++) {
1004                 mix = &emu->efx_pcm_mixer[i];
1005                 mix->epcm = NULL;
1006                 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1007         }
1008         return 0;
1009 }
1010
1011 static int snd_emu10k1_efx_playback_open(snd_pcm_substream_t * substream)
1012 {
1013         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1014         emu10k1_pcm_t *epcm;
1015         emu10k1_pcm_mixer_t *mix;
1016         snd_pcm_runtime_t *runtime = substream->runtime;
1017         int i;
1018
1019         epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1020         if (epcm == NULL)
1021                 return -ENOMEM;
1022         epcm->emu = emu;
1023         epcm->type = PLAYBACK_EFX;
1024         epcm->substream = substream;
1025         
1026         emu->pcm_playback_efx_substream = substream;
1027
1028         runtime->private_data = epcm;
1029         runtime->private_free = snd_emu10k1_pcm_free_substream;
1030         runtime->hw = snd_emu10k1_efx_playback;
1031         
1032         for (i=0; i < NUM_EFX_PLAYBACK; i++) {
1033                 mix = &emu->efx_pcm_mixer[i];
1034                 mix->send_routing[0][0] = i;
1035                 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1036                 mix->send_volume[0][0] = 255;
1037                 mix->attn[0] = 0xffff;
1038                 mix->epcm = epcm;
1039                 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1040         }
1041         return 0;
1042 }
1043
1044 static int snd_emu10k1_playback_open(snd_pcm_substream_t * substream)
1045 {
1046         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1047         emu10k1_pcm_t *epcm;
1048         emu10k1_pcm_mixer_t *mix;
1049         snd_pcm_runtime_t *runtime = substream->runtime;
1050         int i, err;
1051
1052         epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1053         if (epcm == NULL)
1054                 return -ENOMEM;
1055         epcm->emu = emu;
1056         epcm->type = PLAYBACK_EMUVOICE;
1057         epcm->substream = substream;
1058         runtime->private_data = epcm;
1059         runtime->private_free = snd_emu10k1_pcm_free_substream;
1060         runtime->hw = snd_emu10k1_playback;
1061         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
1062                 kfree(epcm);
1063                 return err;
1064         }
1065         if ((err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX)) < 0) {
1066                 kfree(epcm);
1067                 return err;
1068         }
1069         mix = &emu->pcm_mixer[substream->number];
1070         for (i = 0; i < 4; i++)
1071                 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1072         memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1073         mix->send_volume[0][0] = mix->send_volume[0][1] =
1074         mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1075         mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1076         mix->epcm = epcm;
1077         snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1078         return 0;
1079 }
1080
1081 static int snd_emu10k1_playback_close(snd_pcm_substream_t * substream)
1082 {
1083         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1084         emu10k1_pcm_mixer_t *mix = &emu->pcm_mixer[substream->number];
1085
1086         mix->epcm = NULL;
1087         snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1088         return 0;
1089 }
1090
1091 static int snd_emu10k1_capture_open(snd_pcm_substream_t * substream)
1092 {
1093         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1094         snd_pcm_runtime_t *runtime = substream->runtime;
1095         emu10k1_pcm_t *epcm;
1096
1097         epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1098         if (epcm == NULL)
1099                 return -ENOMEM;
1100         epcm->emu = emu;
1101         epcm->type = CAPTURE_AC97ADC;
1102         epcm->substream = substream;
1103         epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1104         epcm->capture_inte = INTE_ADCBUFENABLE;
1105         epcm->capture_ba_reg = ADCBA;
1106         epcm->capture_bs_reg = ADCBS;
1107         epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1108         runtime->private_data = epcm;
1109         runtime->private_free = snd_emu10k1_pcm_free_substream;
1110         runtime->hw = snd_emu10k1_capture;
1111         emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1112         emu->pcm_capture_substream = substream;
1113         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1114         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1115         return 0;
1116 }
1117
1118 static int snd_emu10k1_capture_close(snd_pcm_substream_t * substream)
1119 {
1120         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1121
1122         emu->capture_interrupt = NULL;
1123         emu->pcm_capture_substream = NULL;
1124         return 0;
1125 }
1126
1127 static int snd_emu10k1_capture_mic_open(snd_pcm_substream_t * substream)
1128 {
1129         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1130         emu10k1_pcm_t *epcm;
1131         snd_pcm_runtime_t *runtime = substream->runtime;
1132
1133         epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1134         if (epcm == NULL)
1135                 return -ENOMEM;
1136         epcm->emu = emu;
1137         epcm->type = CAPTURE_AC97MIC;
1138         epcm->substream = substream;
1139         epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1140         epcm->capture_inte = INTE_MICBUFENABLE;
1141         epcm->capture_ba_reg = MICBA;
1142         epcm->capture_bs_reg = MICBS;
1143         epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1144         substream->runtime->private_data = epcm;
1145         substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1146         runtime->hw = snd_emu10k1_capture;
1147         runtime->hw.rates = SNDRV_PCM_RATE_8000;
1148         runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1149         runtime->hw.channels_min = 1;
1150         emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1151         emu->pcm_capture_mic_substream = substream;
1152         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1153         return 0;
1154 }
1155
1156 static int snd_emu10k1_capture_mic_close(snd_pcm_substream_t * substream)
1157 {
1158         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1159
1160         emu->capture_interrupt = NULL;
1161         emu->pcm_capture_mic_substream = NULL;
1162         return 0;
1163 }
1164
1165 static int snd_emu10k1_capture_efx_open(snd_pcm_substream_t * substream)
1166 {
1167         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1168         emu10k1_pcm_t *epcm;
1169         snd_pcm_runtime_t *runtime = substream->runtime;
1170         int nefx = emu->audigy ? 64 : 32;
1171         int idx;
1172
1173         epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1174         if (epcm == NULL)
1175                 return -ENOMEM;
1176         epcm->emu = emu;
1177         epcm->type = CAPTURE_EFX;
1178         epcm->substream = substream;
1179         epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1180         epcm->capture_inte = INTE_EFXBUFENABLE;
1181         epcm->capture_ba_reg = FXBA;
1182         epcm->capture_bs_reg = FXBS;
1183         epcm->capture_idx_reg = FXIDX;
1184         substream->runtime->private_data = epcm;
1185         substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1186         runtime->hw = snd_emu10k1_capture;
1187         runtime->hw.rates = SNDRV_PCM_RATE_48000;
1188         runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1189         spin_lock_irq(&emu->reg_lock);
1190         runtime->hw.channels_min = runtime->hw.channels_max = 0;
1191         for (idx = 0; idx < nefx; idx++) {
1192                 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1193                         runtime->hw.channels_min++;
1194                         runtime->hw.channels_max++;
1195                 }
1196         }
1197         epcm->capture_cr_val = emu->efx_voices_mask[0];
1198         epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1199         spin_unlock_irq(&emu->reg_lock);
1200         emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1201         emu->pcm_capture_efx_substream = substream;
1202         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1203         return 0;
1204 }
1205
1206 static int snd_emu10k1_capture_efx_close(snd_pcm_substream_t * substream)
1207 {
1208         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1209
1210         emu->capture_interrupt = NULL;
1211         emu->pcm_capture_efx_substream = NULL;
1212         return 0;
1213 }
1214
1215 static snd_pcm_ops_t snd_emu10k1_playback_ops = {
1216         .open =                 snd_emu10k1_playback_open,
1217         .close =                snd_emu10k1_playback_close,
1218         .ioctl =                snd_pcm_lib_ioctl,
1219         .hw_params =            snd_emu10k1_playback_hw_params,
1220         .hw_free =              snd_emu10k1_playback_hw_free,
1221         .prepare =              snd_emu10k1_playback_prepare,
1222         .trigger =              snd_emu10k1_playback_trigger,
1223         .pointer =              snd_emu10k1_playback_pointer,
1224         .page =                 snd_pcm_sgbuf_ops_page,
1225 };
1226
1227 static snd_pcm_ops_t snd_emu10k1_capture_ops = {
1228         .open =                 snd_emu10k1_capture_open,
1229         .close =                snd_emu10k1_capture_close,
1230         .ioctl =                snd_pcm_lib_ioctl,
1231         .hw_params =            snd_emu10k1_capture_hw_params,
1232         .hw_free =              snd_emu10k1_capture_hw_free,
1233         .prepare =              snd_emu10k1_capture_prepare,
1234         .trigger =              snd_emu10k1_capture_trigger,
1235         .pointer =              snd_emu10k1_capture_pointer,
1236 };
1237
1238 /* EFX playback */
1239 static snd_pcm_ops_t snd_emu10k1_efx_playback_ops = {
1240         .open =                 snd_emu10k1_efx_playback_open,
1241         .close =                snd_emu10k1_efx_playback_close,
1242         .ioctl =                snd_pcm_lib_ioctl,
1243         .hw_params =            snd_emu10k1_playback_hw_params,
1244         .hw_free =              snd_emu10k1_efx_playback_hw_free,
1245         .prepare =              snd_emu10k1_efx_playback_prepare,
1246         .trigger =              snd_emu10k1_efx_playback_trigger,
1247         .pointer =              snd_emu10k1_efx_playback_pointer,
1248         .page =                 snd_pcm_sgbuf_ops_page,
1249 };
1250
1251 static void snd_emu10k1_pcm_free(snd_pcm_t *pcm)
1252 {
1253         emu10k1_t *emu = pcm->private_data;
1254         emu->pcm = NULL;
1255         snd_pcm_lib_preallocate_free_for_all(pcm);
1256 }
1257
1258 int __devinit snd_emu10k1_pcm(emu10k1_t * emu, int device, snd_pcm_t ** rpcm)
1259 {
1260         snd_pcm_t *pcm;
1261         snd_pcm_substream_t *substream;
1262         int err;
1263
1264         if (rpcm)
1265                 *rpcm = NULL;
1266
1267         if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1268                 return err;
1269
1270         pcm->private_data = emu;
1271         pcm->private_free = snd_emu10k1_pcm_free;
1272
1273         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1274         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1275
1276         pcm->info_flags = 0;
1277         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1278         strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1279         emu->pcm = pcm;
1280
1281         for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1282                 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1283                         return err;
1284
1285         for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1286                 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1287
1288         if (rpcm)
1289                 *rpcm = pcm;
1290
1291         return 0;
1292 }
1293
1294 int __devinit snd_emu10k1_pcm_multi(emu10k1_t * emu, int device, snd_pcm_t ** rpcm)
1295 {
1296         snd_pcm_t *pcm;
1297         snd_pcm_substream_t *substream;
1298         int err;
1299
1300         if (rpcm)
1301                 *rpcm = NULL;
1302
1303         if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
1304                 return err;
1305
1306         pcm->private_data = emu;
1307         pcm->private_free = snd_emu10k1_pcm_free;
1308
1309         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1310
1311         pcm->info_flags = 0;
1312         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1313         strcpy(pcm->name, "Multichannel Playback");
1314         emu->pcm = pcm;
1315
1316         for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1317                 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1318                         return err;
1319
1320         if (rpcm)
1321                 *rpcm = pcm;
1322
1323         return 0;
1324 }
1325
1326
1327 static snd_pcm_ops_t snd_emu10k1_capture_mic_ops = {
1328         .open =                 snd_emu10k1_capture_mic_open,
1329         .close =                snd_emu10k1_capture_mic_close,
1330         .ioctl =                snd_pcm_lib_ioctl,
1331         .hw_params =            snd_emu10k1_capture_hw_params,
1332         .hw_free =              snd_emu10k1_capture_hw_free,
1333         .prepare =              snd_emu10k1_capture_prepare,
1334         .trigger =              snd_emu10k1_capture_trigger,
1335         .pointer =              snd_emu10k1_capture_pointer,
1336 };
1337
1338 static void snd_emu10k1_pcm_mic_free(snd_pcm_t *pcm)
1339 {
1340         emu10k1_t *emu = pcm->private_data;
1341         emu->pcm_mic = NULL;
1342         snd_pcm_lib_preallocate_free_for_all(pcm);
1343 }
1344
1345 int __devinit snd_emu10k1_pcm_mic(emu10k1_t * emu, int device, snd_pcm_t ** rpcm)
1346 {
1347         snd_pcm_t *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         pcm->private_free = snd_emu10k1_pcm_mic_free;
1358
1359         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1360
1361         pcm->info_flags = 0;
1362         strcpy(pcm->name, "Mic Capture");
1363         emu->pcm_mic = pcm;
1364
1365         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1366
1367         if (rpcm)
1368                 *rpcm = pcm;
1369         return 0;
1370 }
1371
1372 static int snd_emu10k1_pcm_efx_voices_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1373 {
1374         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
1375         int nefx = emu->audigy ? 64 : 32;
1376         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1377         uinfo->count = nefx;
1378         uinfo->value.integer.min = 0;
1379         uinfo->value.integer.max = 1;
1380         return 0;
1381 }
1382
1383 static int snd_emu10k1_pcm_efx_voices_mask_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1384 {
1385         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
1386         int nefx = emu->audigy ? 64 : 32;
1387         int idx;
1388         
1389         spin_lock_irq(&emu->reg_lock);
1390         for (idx = 0; idx < nefx; idx++)
1391                 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1392         spin_unlock_irq(&emu->reg_lock);
1393         return 0;
1394 }
1395
1396 static int snd_emu10k1_pcm_efx_voices_mask_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1397 {
1398         emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
1399         unsigned int nval[2], bits;
1400         int nefx = emu->audigy ? 64 : 32;
1401         int nefxb = emu->audigy ? 7 : 6;
1402         int change, idx;
1403         
1404         nval[0] = nval[1] = 0;
1405         for (idx = 0, bits = 0; idx < nefx; idx++)
1406                 if (ucontrol->value.integer.value[idx]) {
1407                         nval[idx / 32] |= 1 << (idx % 32);
1408                         bits++;
1409                 }
1410                 
1411         for (idx = 0; idx < nefxb; idx++)
1412                 if (1 << idx == bits)
1413                         break;
1414         
1415         if (idx >= nefxb)
1416                 return -EINVAL;
1417
1418         spin_lock_irq(&emu->reg_lock);
1419         change = (nval[0] != emu->efx_voices_mask[0]) ||
1420                 (nval[1] != emu->efx_voices_mask[1]);
1421         emu->efx_voices_mask[0] = nval[0];
1422         emu->efx_voices_mask[1] = nval[1];
1423         spin_unlock_irq(&emu->reg_lock);
1424         return change;
1425 }
1426
1427 static snd_kcontrol_new_t snd_emu10k1_pcm_efx_voices_mask = {
1428         .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1429         .name = "Captured FX8010 Outputs",
1430         .info = snd_emu10k1_pcm_efx_voices_mask_info,
1431         .get = snd_emu10k1_pcm_efx_voices_mask_get,
1432         .put = snd_emu10k1_pcm_efx_voices_mask_put
1433 };
1434
1435 static snd_pcm_ops_t snd_emu10k1_capture_efx_ops = {
1436         .open =                 snd_emu10k1_capture_efx_open,
1437         .close =                snd_emu10k1_capture_efx_close,
1438         .ioctl =                snd_pcm_lib_ioctl,
1439         .hw_params =            snd_emu10k1_capture_hw_params,
1440         .hw_free =              snd_emu10k1_capture_hw_free,
1441         .prepare =              snd_emu10k1_capture_prepare,
1442         .trigger =              snd_emu10k1_capture_trigger,
1443         .pointer =              snd_emu10k1_capture_pointer,
1444 };
1445
1446
1447 /* EFX playback */
1448
1449 #define INITIAL_TRAM_SHIFT     14
1450 #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1451
1452 static void snd_emu10k1_fx8010_playback_irq(emu10k1_t *emu, void *private_data)
1453 {
1454         snd_pcm_substream_t *substream = private_data;
1455         snd_pcm_period_elapsed(substream);
1456 }
1457
1458 static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1459                                                    unsigned short *dst_right,
1460                                                    unsigned short *src,
1461                                                    unsigned int count,
1462                                                    unsigned int tram_shift)
1463 {
1464         // printk("tram_poke1: dst_left = 0x%p, dst_right = 0x%p, src = 0x%p, count = 0x%x\n", dst_left, dst_right, src, count);
1465         if ((tram_shift & 1) == 0) {
1466                 while (count--) {
1467                         *dst_left-- = *src++;
1468                         *dst_right-- = *src++;
1469                 }
1470         } else {
1471                 while (count--) {
1472                         *dst_right-- = *src++;
1473                         *dst_left-- = *src++;
1474                 }
1475         }
1476 }
1477
1478 static void fx8010_pb_trans_copy(snd_pcm_substream_t *substream,
1479                                  snd_pcm_indirect_t *rec, size_t bytes)
1480 {
1481         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1482         snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1483         unsigned int tram_size = pcm->buffer_size;
1484         unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1485         unsigned int frames = bytes >> 2, count;
1486         unsigned int tram_pos = pcm->tram_pos;
1487         unsigned int tram_shift = pcm->tram_shift;
1488
1489         while (frames > tram_pos) {
1490                 count = tram_pos + 1;
1491                 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1492                                                        (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1493                                                        src, count, tram_shift);
1494                 src += count * 2;
1495                 frames -= count;
1496                 tram_pos = (tram_size / 2) - 1;
1497                 tram_shift++;
1498         }
1499         snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1500                                                (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1501                                                src, frames, tram_shift);
1502         tram_pos -= frames;
1503         pcm->tram_pos = tram_pos;
1504         pcm->tram_shift = tram_shift;
1505 }
1506
1507 static int snd_emu10k1_fx8010_playback_transfer(snd_pcm_substream_t *substream)
1508 {
1509         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1510         snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1511
1512         snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy);
1513         return 0;
1514 }
1515
1516 static int snd_emu10k1_fx8010_playback_hw_params(snd_pcm_substream_t * substream,
1517                                                  snd_pcm_hw_params_t * hw_params)
1518 {
1519         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1520 }
1521
1522 static int snd_emu10k1_fx8010_playback_hw_free(snd_pcm_substream_t * substream)
1523 {
1524         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1525         snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1526         unsigned int i;
1527
1528         for (i = 0; i < pcm->channels; i++)
1529                 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1530         snd_pcm_lib_free_pages(substream);
1531         return 0;
1532 }
1533
1534 static int snd_emu10k1_fx8010_playback_prepare(snd_pcm_substream_t * substream)
1535 {
1536         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1537         snd_pcm_runtime_t *runtime = substream->runtime;
1538         snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1539         unsigned int i;
1540         
1541         // 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);
1542         memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1543         pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1544         pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1545         pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1546         pcm->tram_shift = 0;
1547         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0);     /* reset */
1548         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);     /* reset */
1549         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1550         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0);         /* reset ptr number */
1551         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1552         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1553         for (i = 0; i < pcm->channels; i++)
1554                 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1555         return 0;
1556 }
1557
1558 static int snd_emu10k1_fx8010_playback_trigger(snd_pcm_substream_t * substream, int cmd)
1559 {
1560         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1561         snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1562         int result = 0;
1563
1564         spin_lock(&emu->reg_lock);
1565         switch (cmd) {
1566         case SNDRV_PCM_TRIGGER_START:
1567                 /* follow thru */
1568         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
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                 snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL;
1590                 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1591                 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1592                 pcm->tram_shift = 0;
1593                 break;
1594         default:
1595                 result = -EINVAL;
1596                 break;
1597         }
1598       __err:
1599         spin_unlock(&emu->reg_lock);
1600         return result;
1601 }
1602
1603 static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(snd_pcm_substream_t * substream)
1604 {
1605         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1606         snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1607         size_t ptr; /* byte pointer */
1608
1609         if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1610                 return 0;
1611         ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1612         return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1613 }
1614
1615 static snd_pcm_hardware_t snd_emu10k1_fx8010_playback =
1616 {
1617         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1618                                  /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE),
1619         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1620         .rates =                SNDRV_PCM_RATE_48000,
1621         .rate_min =             48000,
1622         .rate_max =             48000,
1623         .channels_min =         1,
1624         .channels_max =         1,
1625         .buffer_bytes_max =     (128*1024),
1626         .period_bytes_min =     1024,
1627         .period_bytes_max =     (128*1024),
1628         .periods_min =          1,
1629         .periods_max =          1024,
1630         .fifo_size =            0,
1631 };
1632
1633 static int snd_emu10k1_fx8010_playback_open(snd_pcm_substream_t * substream)
1634 {
1635         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1636         snd_pcm_runtime_t *runtime = substream->runtime;
1637         snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1638
1639         runtime->hw = snd_emu10k1_fx8010_playback;
1640         runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1641         runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1642         spin_lock_irq(&emu->reg_lock);
1643         if (pcm->valid == 0) {
1644                 spin_unlock_irq(&emu->reg_lock);
1645                 return -ENODEV;
1646         }
1647         pcm->opened = 1;
1648         spin_unlock_irq(&emu->reg_lock);
1649         return 0;
1650 }
1651
1652 static int snd_emu10k1_fx8010_playback_close(snd_pcm_substream_t * substream)
1653 {
1654         emu10k1_t *emu = snd_pcm_substream_chip(substream);
1655         snd_emu10k1_fx8010_pcm_t *pcm = &emu->fx8010.pcm[substream->number];
1656
1657         spin_lock_irq(&emu->reg_lock);
1658         pcm->opened = 0;
1659         spin_unlock_irq(&emu->reg_lock);
1660         return 0;
1661 }
1662
1663 static snd_pcm_ops_t snd_emu10k1_fx8010_playback_ops = {
1664         .open =                 snd_emu10k1_fx8010_playback_open,
1665         .close =                snd_emu10k1_fx8010_playback_close,
1666         .ioctl =                snd_pcm_lib_ioctl,
1667         .hw_params =            snd_emu10k1_fx8010_playback_hw_params,
1668         .hw_free =              snd_emu10k1_fx8010_playback_hw_free,
1669         .prepare =              snd_emu10k1_fx8010_playback_prepare,
1670         .trigger =              snd_emu10k1_fx8010_playback_trigger,
1671         .pointer =              snd_emu10k1_fx8010_playback_pointer,
1672         .ack =                  snd_emu10k1_fx8010_playback_transfer,
1673 };
1674
1675 static void snd_emu10k1_pcm_efx_free(snd_pcm_t *pcm)
1676 {
1677         emu10k1_t *emu = pcm->private_data;
1678         emu->pcm_efx = NULL;
1679         snd_pcm_lib_preallocate_free_for_all(pcm);
1680 }
1681
1682 int __devinit snd_emu10k1_pcm_efx(emu10k1_t * emu, int device, snd_pcm_t ** rpcm)
1683 {
1684         snd_pcm_t *pcm;
1685         snd_kcontrol_t *kctl;
1686         int err;
1687
1688         if (rpcm)
1689                 *rpcm = NULL;
1690
1691         if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1692                 return err;
1693
1694         pcm->private_data = emu;
1695         pcm->private_free = snd_emu10k1_pcm_efx_free;
1696
1697         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1698         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1699
1700         pcm->info_flags = 0;
1701         strcpy(pcm->name, "Multichannel Capture/PT Playback");
1702         emu->pcm_efx = pcm;
1703         if (rpcm)
1704                 *rpcm = pcm;
1705
1706         /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs 
1707          * to these
1708          */     
1709         
1710         /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1711         if (emu->audigy) {
1712                 emu->efx_voices_mask[0] = 0;
1713                 emu->efx_voices_mask[1] = 0xffff;
1714         } else {
1715                 emu->efx_voices_mask[0] = 0xffff0000;
1716                 emu->efx_voices_mask[1] = 0;
1717         }
1718         kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1719         if (!kctl)
1720                 return -ENOMEM;
1721         kctl->id.device = device;
1722         snd_ctl_add(emu->card, kctl);
1723
1724         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1725
1726         return 0;
1727 }