~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/pci/emu10k1/emupcm.c

Version: ~ [ linux-5.16-rc1 ] ~ [ linux-5.15.2 ] ~ [ linux-5.14.18 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.79 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.159 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.217 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.255 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.290 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.292 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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;
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         mix = &emu->pcm_mixer[substream->number];
1150         for (i = 0; i < 4; i++)
1151                 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1152         memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1153         mix->send_volume[0][0] = mix->send_volume[0][1] =
1154         mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1155         mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1156         mix->epcm = epcm;
1157         snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1158         return 0;
1159 }
1160 
1161 static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
1162 {
1163         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1164         struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
1165 
1166         mix->epcm = NULL;
1167         snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1168         return 0;
1169 }
1170 
1171 static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
1172 {
1173         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1174         struct snd_pcm_runtime *runtime = substream->runtime;
1175         struct snd_emu10k1_pcm *epcm;
1176 
1177         epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1178         if (epcm == NULL)
1179                 return -ENOMEM;
1180         epcm->emu = emu;
1181         epcm->type = CAPTURE_AC97ADC;
1182         epcm->substream = substream;
1183         epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1184         epcm->capture_inte = INTE_ADCBUFENABLE;
1185         epcm->capture_ba_reg = ADCBA;
1186         epcm->capture_bs_reg = ADCBS;
1187         epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1188         runtime->private_data = epcm;
1189         runtime->private_free = snd_emu10k1_pcm_free_substream;
1190         runtime->hw = snd_emu10k1_capture;
1191         emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1192         emu->pcm_capture_substream = substream;
1193         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1194         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1195         return 0;
1196 }
1197 
1198 static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
1199 {
1200         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1201 
1202         emu->capture_interrupt = NULL;
1203         emu->pcm_capture_substream = NULL;
1204         return 0;
1205 }
1206 
1207 static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
1208 {
1209         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1210         struct snd_emu10k1_pcm *epcm;
1211         struct snd_pcm_runtime *runtime = substream->runtime;
1212 
1213         epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1214         if (epcm == NULL)
1215                 return -ENOMEM;
1216         epcm->emu = emu;
1217         epcm->type = CAPTURE_AC97MIC;
1218         epcm->substream = substream;
1219         epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1220         epcm->capture_inte = INTE_MICBUFENABLE;
1221         epcm->capture_ba_reg = MICBA;
1222         epcm->capture_bs_reg = MICBS;
1223         epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1224         substream->runtime->private_data = epcm;
1225         substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1226         runtime->hw = snd_emu10k1_capture;
1227         runtime->hw.rates = SNDRV_PCM_RATE_8000;
1228         runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1229         runtime->hw.channels_min = 1;
1230         emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1231         emu->pcm_capture_mic_substream = substream;
1232         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1233         return 0;
1234 }
1235 
1236 static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
1237 {
1238         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1239 
1240         emu->capture_interrupt = NULL;
1241         emu->pcm_capture_mic_substream = NULL;
1242         return 0;
1243 }
1244 
1245 static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
1246 {
1247         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1248         struct snd_emu10k1_pcm *epcm;
1249         struct snd_pcm_runtime *runtime = substream->runtime;
1250         int nefx = emu->audigy ? 64 : 32;
1251         int idx;
1252 
1253         epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1254         if (epcm == NULL)
1255                 return -ENOMEM;
1256         epcm->emu = emu;
1257         epcm->type = CAPTURE_EFX;
1258         epcm->substream = substream;
1259         epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1260         epcm->capture_inte = INTE_EFXBUFENABLE;
1261         epcm->capture_ba_reg = FXBA;
1262         epcm->capture_bs_reg = FXBS;
1263         epcm->capture_idx_reg = FXIDX;
1264         substream->runtime->private_data = epcm;
1265         substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1266         runtime->hw = snd_emu10k1_capture_efx;
1267         runtime->hw.rates = SNDRV_PCM_RATE_48000;
1268         runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1269         spin_lock_irq(&emu->reg_lock);
1270         if (emu->card_capabilities->emu_model) {
1271                 /*  Nb. of channels has been increased to 16 */
1272                 /* TODO
1273                  * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
1274                  * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1275                  * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1276                  * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
1277                  * rate_min = 44100,
1278                  * rate_max = 192000,
1279                  * channels_min = 16,
1280                  * channels_max = 16,
1281                  * Need to add mixer control to fix sample rate
1282                  *                 
1283                  * There are 32 mono channels of 16bits each.
1284                  * 24bit Audio uses 2x channels over 16bit
1285                  * 96kHz uses 2x channels over 48kHz
1286                  * 192kHz uses 4x channels over 48kHz
1287                  * So, for 48kHz 24bit, one has 16 channels
1288                  * for 96kHz 24bit, one has 8 channels
1289                  * for 192kHz 24bit, one has 4 channels
1290                  *
1291                  */
1292 #if 1
1293                 switch (emu->emu1010.internal_clock) {
1294                 case 0:
1295                         /* For 44.1kHz */
1296                         runtime->hw.rates = SNDRV_PCM_RATE_44100;
1297                         runtime->hw.rate_min = runtime->hw.rate_max = 44100;
1298                         runtime->hw.channels_min =
1299                                 runtime->hw.channels_max = 16;
1300                         break;
1301                 case 1:
1302                         /* For 48kHz */
1303                         runtime->hw.rates = SNDRV_PCM_RATE_48000;
1304                         runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1305                         runtime->hw.channels_min =
1306                                 runtime->hw.channels_max = 16;
1307                         break;
1308                 };
1309 #endif
1310 #if 0
1311                 /* For 96kHz */
1312                 runtime->hw.rates = SNDRV_PCM_RATE_96000;
1313                 runtime->hw.rate_min = runtime->hw.rate_max = 96000;
1314                 runtime->hw.channels_min = runtime->hw.channels_max = 4;
1315 #endif
1316 #if 0
1317                 /* For 192kHz */
1318                 runtime->hw.rates = SNDRV_PCM_RATE_192000;
1319                 runtime->hw.rate_min = runtime->hw.rate_max = 192000;
1320                 runtime->hw.channels_min = runtime->hw.channels_max = 2;
1321 #endif
1322                 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1323                 /* efx_voices_mask[0] is expected to be zero
1324                  * efx_voices_mask[1] is expected to have 32bits set
1325                  */
1326         } else {
1327                 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1328                 for (idx = 0; idx < nefx; idx++) {
1329                         if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1330                                 runtime->hw.channels_min++;
1331                                 runtime->hw.channels_max++;
1332                         }
1333                 }
1334         }
1335         epcm->capture_cr_val = emu->efx_voices_mask[0];
1336         epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1337         spin_unlock_irq(&emu->reg_lock);
1338         emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1339         emu->pcm_capture_efx_substream = substream;
1340         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1341         return 0;
1342 }
1343 
1344 static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
1345 {
1346         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1347 
1348         emu->capture_interrupt = NULL;
1349         emu->pcm_capture_efx_substream = NULL;
1350         return 0;
1351 }
1352 
1353 static struct snd_pcm_ops snd_emu10k1_playback_ops = {
1354         .open =                 snd_emu10k1_playback_open,
1355         .close =                snd_emu10k1_playback_close,
1356         .ioctl =                snd_pcm_lib_ioctl,
1357         .hw_params =            snd_emu10k1_playback_hw_params,
1358         .hw_free =              snd_emu10k1_playback_hw_free,
1359         .prepare =              snd_emu10k1_playback_prepare,
1360         .trigger =              snd_emu10k1_playback_trigger,
1361         .pointer =              snd_emu10k1_playback_pointer,
1362         .page =                 snd_pcm_sgbuf_ops_page,
1363 };
1364 
1365 static struct snd_pcm_ops snd_emu10k1_capture_ops = {
1366         .open =                 snd_emu10k1_capture_open,
1367         .close =                snd_emu10k1_capture_close,
1368         .ioctl =                snd_pcm_lib_ioctl,
1369         .hw_params =            snd_emu10k1_capture_hw_params,
1370         .hw_free =              snd_emu10k1_capture_hw_free,
1371         .prepare =              snd_emu10k1_capture_prepare,
1372         .trigger =              snd_emu10k1_capture_trigger,
1373         .pointer =              snd_emu10k1_capture_pointer,
1374 };
1375 
1376 /* EFX playback */
1377 static struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
1378         .open =                 snd_emu10k1_efx_playback_open,
1379         .close =                snd_emu10k1_efx_playback_close,
1380         .ioctl =                snd_pcm_lib_ioctl,
1381         .hw_params =            snd_emu10k1_playback_hw_params,
1382         .hw_free =              snd_emu10k1_efx_playback_hw_free,
1383         .prepare =              snd_emu10k1_efx_playback_prepare,
1384         .trigger =              snd_emu10k1_efx_playback_trigger,
1385         .pointer =              snd_emu10k1_efx_playback_pointer,
1386         .page =                 snd_pcm_sgbuf_ops_page,
1387 };
1388 
1389 int __devinit snd_emu10k1_pcm(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1390 {
1391         struct snd_pcm *pcm;
1392         struct snd_pcm_substream *substream;
1393         int err;
1394 
1395         if (rpcm)
1396                 *rpcm = NULL;
1397 
1398         if ((err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm)) < 0)
1399                 return err;
1400 
1401         pcm->private_data = emu;
1402 
1403         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1404         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1405 
1406         pcm->info_flags = 0;
1407         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1408         strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1409         emu->pcm = pcm;
1410 
1411         for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1412                 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1413                         return err;
1414 
1415         for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1416                 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1417 
1418         if (rpcm)
1419                 *rpcm = pcm;
1420 
1421         return 0;
1422 }
1423 
1424 int __devinit snd_emu10k1_pcm_multi(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1425 {
1426         struct snd_pcm *pcm;
1427         struct snd_pcm_substream *substream;
1428         int err;
1429 
1430         if (rpcm)
1431                 *rpcm = NULL;
1432 
1433         if ((err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm)) < 0)
1434                 return err;
1435 
1436         pcm->private_data = emu;
1437 
1438         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1439 
1440         pcm->info_flags = 0;
1441         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1442         strcpy(pcm->name, "Multichannel Playback");
1443         emu->pcm_multi = pcm;
1444 
1445         for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1446                 if ((err = snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG, snd_dma_pci_data(emu->pci), 64*1024, 64*1024)) < 0)
1447                         return err;
1448 
1449         if (rpcm)
1450                 *rpcm = pcm;
1451 
1452         return 0;
1453 }
1454 
1455 
1456 static struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
1457         .open =                 snd_emu10k1_capture_mic_open,
1458         .close =                snd_emu10k1_capture_mic_close,
1459         .ioctl =                snd_pcm_lib_ioctl,
1460         .hw_params =            snd_emu10k1_capture_hw_params,
1461         .hw_free =              snd_emu10k1_capture_hw_free,
1462         .prepare =              snd_emu10k1_capture_prepare,
1463         .trigger =              snd_emu10k1_capture_trigger,
1464         .pointer =              snd_emu10k1_capture_pointer,
1465 };
1466 
1467 int __devinit snd_emu10k1_pcm_mic(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1468 {
1469         struct snd_pcm *pcm;
1470         int err;
1471 
1472         if (rpcm)
1473                 *rpcm = NULL;
1474 
1475         if ((err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm)) < 0)
1476                 return err;
1477 
1478         pcm->private_data = emu;
1479 
1480         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1481 
1482         pcm->info_flags = 0;
1483         strcpy(pcm->name, "Mic Capture");
1484         emu->pcm_mic = pcm;
1485 
1486         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1487 
1488         if (rpcm)
1489                 *rpcm = pcm;
1490         return 0;
1491 }
1492 
1493 static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1494 {
1495         struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1496         int nefx = emu->audigy ? 64 : 32;
1497         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1498         uinfo->count = nefx;
1499         uinfo->value.integer.min = 0;
1500         uinfo->value.integer.max = 1;
1501         return 0;
1502 }
1503 
1504 static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1505 {
1506         struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1507         int nefx = emu->audigy ? 64 : 32;
1508         int idx;
1509         
1510         spin_lock_irq(&emu->reg_lock);
1511         for (idx = 0; idx < nefx; idx++)
1512                 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1513         spin_unlock_irq(&emu->reg_lock);
1514         return 0;
1515 }
1516 
1517 static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1518 {
1519         struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1520         unsigned int nval[2], bits;
1521         int nefx = emu->audigy ? 64 : 32;
1522         int nefxb = emu->audigy ? 7 : 6;
1523         int change, idx;
1524         
1525         nval[0] = nval[1] = 0;
1526         for (idx = 0, bits = 0; idx < nefx; idx++)
1527                 if (ucontrol->value.integer.value[idx]) {
1528                         nval[idx / 32] |= 1 << (idx % 32);
1529                         bits++;
1530                 }
1531                 
1532         for (idx = 0; idx < nefxb; idx++)
1533                 if (1 << idx == bits)
1534                         break;
1535         
1536         if (idx >= nefxb)
1537                 return -EINVAL;
1538 
1539         spin_lock_irq(&emu->reg_lock);
1540         change = (nval[0] != emu->efx_voices_mask[0]) ||
1541                 (nval[1] != emu->efx_voices_mask[1]);
1542         emu->efx_voices_mask[0] = nval[0];
1543         emu->efx_voices_mask[1] = nval[1];
1544         spin_unlock_irq(&emu->reg_lock);
1545         return change;
1546 }
1547 
1548 static struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
1549         .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1550         .name = "Captured FX8010 Outputs",
1551         .info = snd_emu10k1_pcm_efx_voices_mask_info,
1552         .get = snd_emu10k1_pcm_efx_voices_mask_get,
1553         .put = snd_emu10k1_pcm_efx_voices_mask_put
1554 };
1555 
1556 static struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
1557         .open =                 snd_emu10k1_capture_efx_open,
1558         .close =                snd_emu10k1_capture_efx_close,
1559         .ioctl =                snd_pcm_lib_ioctl,
1560         .hw_params =            snd_emu10k1_capture_hw_params,
1561         .hw_free =              snd_emu10k1_capture_hw_free,
1562         .prepare =              snd_emu10k1_capture_prepare,
1563         .trigger =              snd_emu10k1_capture_trigger,
1564         .pointer =              snd_emu10k1_capture_pointer,
1565 };
1566 
1567 
1568 /* EFX playback */
1569 
1570 #define INITIAL_TRAM_SHIFT     14
1571 #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1572 
1573 static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
1574 {
1575         struct snd_pcm_substream *substream = private_data;
1576         snd_pcm_period_elapsed(substream);
1577 }
1578 
1579 static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1580                                                    unsigned short *dst_right,
1581                                                    unsigned short *src,
1582                                                    unsigned int count,
1583                                                    unsigned int tram_shift)
1584 {
1585         /*
1586         printk(KERN_DEBUG "tram_poke1: dst_left = 0x%p, dst_right = 0x%p, "
1587                "src = 0x%p, count = 0x%x\n",
1588                dst_left, dst_right, src, count);
1589         */
1590         if ((tram_shift & 1) == 0) {
1591                 while (count--) {
1592                         *dst_left-- = *src++;
1593                         *dst_right-- = *src++;
1594                 }
1595         } else {
1596                 while (count--) {
1597                         *dst_right-- = *src++;
1598                         *dst_left-- = *src++;
1599                 }
1600         }
1601 }
1602 
1603 static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1604                                  struct snd_pcm_indirect *rec, size_t bytes)
1605 {
1606         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1607         struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1608         unsigned int tram_size = pcm->buffer_size;
1609         unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1610         unsigned int frames = bytes >> 2, count;
1611         unsigned int tram_pos = pcm->tram_pos;
1612         unsigned int tram_shift = pcm->tram_shift;
1613 
1614         while (frames > tram_pos) {
1615                 count = tram_pos + 1;
1616                 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1617                                                        (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1618                                                        src, count, tram_shift);
1619                 src += count * 2;
1620                 frames -= count;
1621                 tram_pos = (tram_size / 2) - 1;
1622                 tram_shift++;
1623         }
1624         snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1625                                                (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1626                                                src, frames, tram_shift);
1627         tram_pos -= frames;
1628         pcm->tram_pos = tram_pos;
1629         pcm->tram_shift = tram_shift;
1630 }
1631 
1632 static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
1633 {
1634         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1635         struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1636 
1637         snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec, fx8010_pb_trans_copy);
1638         return 0;
1639 }
1640 
1641 static int snd_emu10k1_fx8010_playback_hw_params(struct snd_pcm_substream *substream,
1642                                                  struct snd_pcm_hw_params *hw_params)
1643 {
1644         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1645 }
1646 
1647 static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
1648 {
1649         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1650         struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1651         unsigned int i;
1652 
1653         for (i = 0; i < pcm->channels; i++)
1654                 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1655         snd_pcm_lib_free_pages(substream);
1656         return 0;
1657 }
1658 
1659 static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
1660 {
1661         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1662         struct snd_pcm_runtime *runtime = substream->runtime;
1663         struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1664         unsigned int i;
1665         
1666         /*
1667         printk(KERN_DEBUG "prepare: etram_pages = 0x%p, dma_area = 0x%x, "
1668                "buffer_size = 0x%x (0x%x)\n",
1669                emu->fx8010.etram_pages, runtime->dma_area,
1670                runtime->buffer_size, runtime->buffer_size << 2);
1671         */
1672         memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1673         pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1674         pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1675         pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1676         pcm->tram_shift = 0;
1677         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0);     /* reset */
1678         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);     /* reset */
1679         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1680         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0);         /* reset ptr number */
1681         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1682         snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1683         for (i = 0; i < pcm->channels; i++)
1684                 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1685         return 0;
1686 }
1687 
1688 static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
1689 {
1690         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1691         struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1692         int result = 0;
1693 
1694         spin_lock(&emu->reg_lock);
1695         switch (cmd) {
1696         case SNDRV_PCM_TRIGGER_START:
1697                 /* follow thru */
1698         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1699         case SNDRV_PCM_TRIGGER_RESUME:
1700 #ifdef EMU10K1_SET_AC3_IEC958
1701         {
1702                 int i;
1703                 for (i = 0; i < 3; i++) {
1704                         unsigned int bits;
1705                         bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1706                                SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1707                                0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1708                         snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1709                 }
1710         }
1711 #endif
1712                 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1713                 if (result < 0)
1714                         goto __err;
1715                 snd_emu10k1_fx8010_playback_transfer(substream);        /* roll the ball */
1716                 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1717                 break;
1718         case SNDRV_PCM_TRIGGER_STOP:
1719         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1720         case SNDRV_PCM_TRIGGER_SUSPEND:
1721                 snd_emu10k1_fx8010_unregister_irq_handler(emu, pcm->irq); pcm->irq = NULL;
1722                 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1723                 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1724                 pcm->tram_shift = 0;
1725                 break;
1726         default:
1727                 result = -EINVAL;
1728                 break;
1729         }
1730       __err:
1731         spin_unlock(&emu->reg_lock);
1732         return result;
1733 }
1734 
1735 static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
1736 {
1737         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1738         struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1739         size_t ptr; /* byte pointer */
1740 
1741         if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1742                 return 0;
1743         ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1744         return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1745 }
1746 
1747 static struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
1748 {
1749         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1750                                  SNDRV_PCM_INFO_RESUME |
1751                                  /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE),
1752         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1753         .rates =                SNDRV_PCM_RATE_48000,
1754         .rate_min =             48000,
1755         .rate_max =             48000,
1756         .channels_min =         1,
1757         .channels_max =         1,
1758         .buffer_bytes_max =     (128*1024),
1759         .period_bytes_min =     1024,
1760         .period_bytes_max =     (128*1024),
1761         .periods_min =          2,
1762         .periods_max =          1024,
1763         .fifo_size =            0,
1764 };
1765 
1766 static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
1767 {
1768         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1769         struct snd_pcm_runtime *runtime = substream->runtime;
1770         struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1771 
1772         runtime->hw = snd_emu10k1_fx8010_playback;
1773         runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1774         runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1775         spin_lock_irq(&emu->reg_lock);
1776         if (pcm->valid == 0) {
1777                 spin_unlock_irq(&emu->reg_lock);
1778                 return -ENODEV;
1779         }
1780         pcm->opened = 1;
1781         spin_unlock_irq(&emu->reg_lock);
1782         return 0;
1783 }
1784 
1785 static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
1786 {
1787         struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1788         struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1789 
1790         spin_lock_irq(&emu->reg_lock);
1791         pcm->opened = 0;
1792         spin_unlock_irq(&emu->reg_lock);
1793         return 0;
1794 }
1795 
1796 static struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
1797         .open =                 snd_emu10k1_fx8010_playback_open,
1798         .close =                snd_emu10k1_fx8010_playback_close,
1799         .ioctl =                snd_pcm_lib_ioctl,
1800         .hw_params =            snd_emu10k1_fx8010_playback_hw_params,
1801         .hw_free =              snd_emu10k1_fx8010_playback_hw_free,
1802         .prepare =              snd_emu10k1_fx8010_playback_prepare,
1803         .trigger =              snd_emu10k1_fx8010_playback_trigger,
1804         .pointer =              snd_emu10k1_fx8010_playback_pointer,
1805         .ack =                  snd_emu10k1_fx8010_playback_transfer,
1806 };
1807 
1808 int __devinit snd_emu10k1_pcm_efx(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm)
1809 {
1810         struct snd_pcm *pcm;
1811         struct snd_kcontrol *kctl;
1812         int err;
1813 
1814         if (rpcm)
1815                 *rpcm = NULL;
1816 
1817         if ((err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm)) < 0)
1818                 return err;
1819 
1820         pcm->private_data = emu;
1821 
1822         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1823         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1824 
1825         pcm->info_flags = 0;
1826         strcpy(pcm->name, "Multichannel Capture/PT Playback");
1827         emu->pcm_efx = pcm;
1828         if (rpcm)
1829                 *rpcm = pcm;
1830 
1831         /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs 
1832          * to these
1833          */     
1834         
1835         /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1836         if (emu->audigy) {
1837                 emu->efx_voices_mask[0] = 0;
1838                 if (emu->card_capabilities->emu_model)
1839                         /* Pavel Hofman - 32 voices will be used for
1840                          * capture (write mode) -
1841                          * each bit = corresponding voice
1842                          */
1843                         emu->efx_voices_mask[1] = 0xffffffff;
1844                 else
1845                         emu->efx_voices_mask[1] = 0xffff;
1846         } else {
1847                 emu->efx_voices_mask[0] = 0xffff0000;
1848                 emu->efx_voices_mask[1] = 0;
1849         }
1850         /* For emu1010, the control has to set 32 upper bits (voices)
1851          * out of the 64 bits (voices) to true for the 16-channels capture
1852          * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
1853          * is already defined but the snd_emu10k1_pcm_efx_voices_mask
1854          * control can override this register's value.
1855          */
1856         kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1857         if (!kctl)
1858                 return -ENOMEM;
1859         kctl->id.device = device;
1860         snd_ctl_add(emu->card, kctl);
1861 
1862         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci), 64*1024, 64*1024);
1863 
1864         return 0;
1865 }
1866 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp