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