[ALSA] trident - Fix PM support
[linux-2.6.git] / sound / pci / trident / trident_main.c
1 /*
2  *  Maintained by Jaroslav Kysela <perex@suse.cz>
3  *  Originated by audio@tridentmicro.com
4  *  Fri Feb 19 15:55:28 MST 1999
5  *  Routines for control of Trident 4DWave (DX and NX) chip
6  *
7  *  BUGS:
8  *
9  *  TODO:
10  *    ---
11  *
12  *   This program is free software; you can redistribute it and/or modify
13  *   it under the terms of the GNU General Public License as published by
14  *   the Free Software Foundation; either version 2 of the License, or
15  *   (at your option) any later version.
16  *
17  *   This program is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with this program; if not, write to the Free Software
24  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25  *
26  *
27  *  SiS7018 S/PDIF support by Thomas Winischhofer <thomas@winischhofer.net>
28  */
29
30 #include <sound/driver.h>
31 #include <linux/delay.h>
32 #include <linux/init.h>
33 #include <linux/interrupt.h>
34 #include <linux/pci.h>
35 #include <linux/slab.h>
36 #include <linux/vmalloc.h>
37 #include <linux/gameport.h>
38
39 #include <sound/core.h>
40 #include <sound/info.h>
41 #include <sound/control.h>
42 #include <sound/trident.h>
43 #include <sound/asoundef.h>
44
45 #include <asm/io.h>
46
47 static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
48                                        struct snd_trident_voice * voice,
49                                        struct snd_pcm_substream *substream);
50 static int snd_trident_pcm_mixer_free(struct snd_trident *trident,
51                                       struct snd_trident_voice * voice,
52                                       struct snd_pcm_substream *substream);
53 static irqreturn_t snd_trident_interrupt(int irq, void *dev_id,
54                                          struct pt_regs *regs);
55 static int snd_trident_sis_reset(struct snd_trident *trident);
56
57 static void snd_trident_clear_voices(struct snd_trident * trident,
58                                      unsigned short v_min, unsigned short v_max);
59 static int snd_trident_free(struct snd_trident *trident);
60
61 /*
62  *  common I/O routines
63  */
64
65
66 #if 0
67 static void snd_trident_print_voice_regs(struct snd_trident *trident, int voice)
68 {
69         unsigned int val, tmp;
70
71         printk("Trident voice %i:\n", voice);
72         outb(voice, TRID_REG(trident, T4D_LFO_GC_CIR));
73         val = inl(TRID_REG(trident, CH_LBA));
74         printk("LBA: 0x%x\n", val);
75         val = inl(TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
76         printk("GVSel: %i\n", val >> 31);
77         printk("Pan: 0x%x\n", (val >> 24) & 0x7f);
78         printk("Vol: 0x%x\n", (val >> 16) & 0xff);
79         printk("CTRL: 0x%x\n", (val >> 12) & 0x0f);
80         printk("EC: 0x%x\n", val & 0x0fff);
81         if (trident->device != TRIDENT_DEVICE_ID_NX) {
82                 val = inl(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS));
83                 printk("CSO: 0x%x\n", val >> 16);
84                 printk("Alpha: 0x%x\n", (val >> 4) & 0x0fff);
85                 printk("FMS: 0x%x\n", val & 0x0f);
86                 val = inl(TRID_REG(trident, CH_DX_ESO_DELTA));
87                 printk("ESO: 0x%x\n", val >> 16);
88                 printk("Delta: 0x%x\n", val & 0xffff);
89                 val = inl(TRID_REG(trident, CH_DX_FMC_RVOL_CVOL));
90         } else {                // TRIDENT_DEVICE_ID_NX
91                 val = inl(TRID_REG(trident, CH_NX_DELTA_CSO));
92                 tmp = (val >> 24) & 0xff;
93                 printk("CSO: 0x%x\n", val & 0x00ffffff);
94                 val = inl(TRID_REG(trident, CH_NX_DELTA_ESO));
95                 tmp |= (val >> 16) & 0xff00;
96                 printk("Delta: 0x%x\n", tmp);
97                 printk("ESO: 0x%x\n", val & 0x00ffffff);
98                 val = inl(TRID_REG(trident, CH_NX_ALPHA_FMS_FMC_RVOL_CVOL));
99                 printk("Alpha: 0x%x\n", val >> 20);
100                 printk("FMS: 0x%x\n", (val >> 16) & 0x0f);
101         }
102         printk("FMC: 0x%x\n", (val >> 14) & 3);
103         printk("RVol: 0x%x\n", (val >> 7) & 0x7f);
104         printk("CVol: 0x%x\n", val & 0x7f);
105 }
106 #endif
107
108 /*---------------------------------------------------------------------------
109    unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
110   
111    Description: This routine will do all of the reading from the external
112                 CODEC (AC97).
113   
114    Parameters:  ac97 - ac97 codec structure
115                 reg - CODEC register index, from AC97 Hal.
116  
117    returns:     16 bit value read from the AC97.
118   
119   ---------------------------------------------------------------------------*/
120 static unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
121 {
122         unsigned int data = 0, treg;
123         unsigned short count = 0xffff;
124         unsigned long flags;
125         struct snd_trident *trident = ac97->private_data;
126
127         spin_lock_irqsave(&trident->reg_lock, flags);
128         if (trident->device == TRIDENT_DEVICE_ID_DX) {
129                 data = (DX_AC97_BUSY_READ | (reg & 0x000000ff));
130                 outl(data, TRID_REG(trident, DX_ACR1_AC97_R));
131                 do {
132                         data = inl(TRID_REG(trident, DX_ACR1_AC97_R));
133                         if ((data & DX_AC97_BUSY_READ) == 0)
134                                 break;
135                 } while (--count);
136         } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
137                 data = (NX_AC97_BUSY_READ | (reg & 0x000000ff));
138                 treg = ac97->num == 0 ? NX_ACR2_AC97_R_PRIMARY : NX_ACR3_AC97_R_SECONDARY;
139                 outl(data, TRID_REG(trident, treg));
140                 do {
141                         data = inl(TRID_REG(trident, treg));
142                         if ((data & 0x00000C00) == 0)
143                                 break;
144                 } while (--count);
145         } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
146                 data = SI_AC97_BUSY_READ | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
147                 if (ac97->num == 1)
148                         data |= SI_AC97_SECONDARY;
149                 outl(data, TRID_REG(trident, SI_AC97_READ));
150                 do {
151                         data = inl(TRID_REG(trident, SI_AC97_READ));
152                         if ((data & (SI_AC97_BUSY_READ)) == 0)
153                                 break;
154                 } while (--count);
155         }
156
157         if (count == 0 && !trident->ac97_detect) {
158                 snd_printk(KERN_ERR "ac97 codec read TIMEOUT [0x%x/0x%x]!!!\n",
159                            reg, data);
160                 data = 0;
161         }
162
163         spin_unlock_irqrestore(&trident->reg_lock, flags);
164         return ((unsigned short) (data >> 16));
165 }
166
167 /*---------------------------------------------------------------------------
168    void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
169    unsigned short wdata)
170   
171    Description: This routine will do all of the writing to the external
172                 CODEC (AC97).
173   
174    Parameters:  ac97 - ac97 codec structure
175                 reg - CODEC register index, from AC97 Hal.
176                 data  - Lower 16 bits are the data to write to CODEC.
177   
178    returns:     TRUE if everything went ok, else FALSE.
179   
180   ---------------------------------------------------------------------------*/
181 static void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
182                                     unsigned short wdata)
183 {
184         unsigned int address, data;
185         unsigned short count = 0xffff;
186         unsigned long flags;
187         struct snd_trident *trident = ac97->private_data;
188
189         data = ((unsigned long) wdata) << 16;
190
191         spin_lock_irqsave(&trident->reg_lock, flags);
192         if (trident->device == TRIDENT_DEVICE_ID_DX) {
193                 address = DX_ACR0_AC97_W;
194
195                 /* read AC-97 write register status */
196                 do {
197                         if ((inw(TRID_REG(trident, address)) & DX_AC97_BUSY_WRITE) == 0)
198                                 break;
199                 } while (--count);
200
201                 data |= (DX_AC97_BUSY_WRITE | (reg & 0x000000ff));
202         } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
203                 address = NX_ACR1_AC97_W;
204
205                 /* read AC-97 write register status */
206                 do {
207                         if ((inw(TRID_REG(trident, address)) & NX_AC97_BUSY_WRITE) == 0)
208                                 break;
209                 } while (--count);
210
211                 data |= (NX_AC97_BUSY_WRITE | (ac97->num << 8) | (reg & 0x000000ff));
212         } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
213                 address = SI_AC97_WRITE;
214
215                 /* read AC-97 write register status */
216                 do {
217                         if ((inw(TRID_REG(trident, address)) & (SI_AC97_BUSY_WRITE)) == 0)
218                                 break;
219                 } while (--count);
220
221                 data |= SI_AC97_BUSY_WRITE | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
222                 if (ac97->num == 1)
223                         data |= SI_AC97_SECONDARY;
224         } else {
225                 address = 0;    /* keep GCC happy */
226                 count = 0;      /* return */
227         }
228
229         if (count == 0) {
230                 spin_unlock_irqrestore(&trident->reg_lock, flags);
231                 return;
232         }
233         outl(data, TRID_REG(trident, address));
234         spin_unlock_irqrestore(&trident->reg_lock, flags);
235 }
236
237 /*---------------------------------------------------------------------------
238    void snd_trident_enable_eso(struct snd_trident *trident)
239   
240    Description: This routine will enable end of loop interrupts.
241                 End of loop interrupts will occur when a running
242                 channel reaches ESO.
243                 Also enables middle of loop interrupts.
244   
245    Parameters:  trident - pointer to target device class for 4DWave.
246   
247   ---------------------------------------------------------------------------*/
248
249 static void snd_trident_enable_eso(struct snd_trident * trident)
250 {
251         unsigned int val;
252
253         val = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
254         val |= ENDLP_IE;
255         val |= MIDLP_IE;
256         if (trident->device == TRIDENT_DEVICE_ID_SI7018)
257                 val |= BANK_B_EN;
258         outl(val, TRID_REG(trident, T4D_LFO_GC_CIR));
259 }
260
261 /*---------------------------------------------------------------------------
262    void snd_trident_disable_eso(struct snd_trident *trident)
263   
264    Description: This routine will disable end of loop interrupts.
265                 End of loop interrupts will occur when a running
266                 channel reaches ESO.
267                 Also disables middle of loop interrupts.
268   
269    Parameters:  
270                 trident - pointer to target device class for 4DWave.
271   
272    returns:     TRUE if everything went ok, else FALSE.
273   
274   ---------------------------------------------------------------------------*/
275
276 static void snd_trident_disable_eso(struct snd_trident * trident)
277 {
278         unsigned int tmp;
279
280         tmp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
281         tmp &= ~ENDLP_IE;
282         tmp &= ~MIDLP_IE;
283         outl(tmp, TRID_REG(trident, T4D_LFO_GC_CIR));
284 }
285
286 /*---------------------------------------------------------------------------
287    void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
288
289     Description: Start a voice, any channel 0 thru 63.
290                  This routine automatically handles the fact that there are
291                  more than 32 channels available.
292
293     Parameters : voice - Voice number 0 thru n.
294                  trident - pointer to target device class for 4DWave.
295
296     Return Value: None.
297
298   ---------------------------------------------------------------------------*/
299
300 void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
301 {
302         unsigned int mask = 1 << (voice & 0x1f);
303         unsigned int reg = (voice & 0x20) ? T4D_START_B : T4D_START_A;
304
305         outl(mask, TRID_REG(trident, reg));
306 }
307
308 /*---------------------------------------------------------------------------
309    void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
310
311     Description: Stop a voice, any channel 0 thru 63.
312                  This routine automatically handles the fact that there are
313                  more than 32 channels available.
314
315     Parameters : voice - Voice number 0 thru n.
316                  trident - pointer to target device class for 4DWave.
317
318     Return Value: None.
319
320   ---------------------------------------------------------------------------*/
321
322 void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
323 {
324         unsigned int mask = 1 << (voice & 0x1f);
325         unsigned int reg = (voice & 0x20) ? T4D_STOP_B : T4D_STOP_A;
326
327         outl(mask, TRID_REG(trident, reg));
328 }
329
330 /*---------------------------------------------------------------------------
331     int snd_trident_allocate_pcm_channel(struct snd_trident *trident)
332   
333     Description: Allocate hardware channel in Bank B (32-63).
334   
335     Parameters :  trident - pointer to target device class for 4DWave.
336   
337     Return Value: hardware channel - 32-63 or -1 when no channel is available
338   
339   ---------------------------------------------------------------------------*/
340
341 static int snd_trident_allocate_pcm_channel(struct snd_trident * trident)
342 {
343         int idx;
344
345         if (trident->ChanPCMcnt >= trident->ChanPCM)
346                 return -1;
347         for (idx = 31; idx >= 0; idx--) {
348                 if (!(trident->ChanMap[T4D_BANK_B] & (1 << idx))) {
349                         trident->ChanMap[T4D_BANK_B] |= 1 << idx;
350                         trident->ChanPCMcnt++;
351                         return idx + 32;
352                 }
353         }
354         return -1;
355 }
356
357 /*---------------------------------------------------------------------------
358     void snd_trident_free_pcm_channel(int channel)
359   
360     Description: Free hardware channel in Bank B (32-63)
361   
362     Parameters :  trident - pointer to target device class for 4DWave.
363                   channel - hardware channel number 0-63
364   
365     Return Value: none
366   
367   ---------------------------------------------------------------------------*/
368
369 static void snd_trident_free_pcm_channel(struct snd_trident *trident, int channel)
370 {
371         if (channel < 32 || channel > 63)
372                 return;
373         channel &= 0x1f;
374         if (trident->ChanMap[T4D_BANK_B] & (1 << channel)) {
375                 trident->ChanMap[T4D_BANK_B] &= ~(1 << channel);
376                 trident->ChanPCMcnt--;
377         }
378 }
379
380 /*---------------------------------------------------------------------------
381     unsigned int snd_trident_allocate_synth_channel(void)
382   
383     Description: Allocate hardware channel in Bank A (0-31).
384   
385     Parameters :  trident - pointer to target device class for 4DWave.
386   
387     Return Value: hardware channel - 0-31 or -1 when no channel is available
388   
389   ---------------------------------------------------------------------------*/
390
391 static int snd_trident_allocate_synth_channel(struct snd_trident * trident)
392 {
393         int idx;
394
395         for (idx = 31; idx >= 0; idx--) {
396                 if (!(trident->ChanMap[T4D_BANK_A] & (1 << idx))) {
397                         trident->ChanMap[T4D_BANK_A] |= 1 << idx;
398                         trident->synth.ChanSynthCount++;
399                         return idx;
400                 }
401         }
402         return -1;
403 }
404
405 /*---------------------------------------------------------------------------
406     void snd_trident_free_synth_channel( int channel )
407   
408     Description: Free hardware channel in Bank B (0-31).
409   
410     Parameters :  trident - pointer to target device class for 4DWave.
411                   channel - hardware channel number 0-63
412   
413     Return Value: none
414   
415   ---------------------------------------------------------------------------*/
416
417 static void snd_trident_free_synth_channel(struct snd_trident *trident, int channel)
418 {
419         if (channel < 0 || channel > 31)
420                 return;
421         channel &= 0x1f;
422         if (trident->ChanMap[T4D_BANK_A] & (1 << channel)) {
423                 trident->ChanMap[T4D_BANK_A] &= ~(1 << channel);
424                 trident->synth.ChanSynthCount--;
425         }
426 }
427
428 /*---------------------------------------------------------------------------
429    snd_trident_write_voice_regs
430   
431    Description: This routine will complete and write the 5 hardware channel
432                 registers to hardware.
433   
434    Paramters:   trident - pointer to target device class for 4DWave.
435                 voice - synthesizer voice structure
436                 Each register field.
437   
438   ---------------------------------------------------------------------------*/
439
440 void snd_trident_write_voice_regs(struct snd_trident * trident,
441                                   struct snd_trident_voice * voice)
442 {
443         unsigned int FmcRvolCvol;
444         unsigned int regs[5];
445
446         regs[1] = voice->LBA;
447         regs[4] = (voice->GVSel << 31) |
448                   ((voice->Pan & 0x0000007f) << 24) |
449                   ((voice->CTRL & 0x0000000f) << 12);
450         FmcRvolCvol = ((voice->FMC & 3) << 14) |
451                       ((voice->RVol & 0x7f) << 7) |
452                       (voice->CVol & 0x7f);
453
454         switch (trident->device) {
455         case TRIDENT_DEVICE_ID_SI7018:
456                 regs[4] |= voice->number > 31 ?
457                                 (voice->Vol & 0x000003ff) :
458                                 ((voice->Vol & 0x00003fc) << (16-2)) |
459                                 (voice->EC & 0x00000fff);
460                 regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
461                         (voice->FMS & 0x0000000f);
462                 regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
463                 regs[3] = (voice->Attribute << 16) | FmcRvolCvol;
464                 break;
465         case TRIDENT_DEVICE_ID_DX:
466                 regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
467                            (voice->EC & 0x00000fff);
468                 regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
469                         (voice->FMS & 0x0000000f);
470                 regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
471                 regs[3] = FmcRvolCvol;
472                 break;
473         case TRIDENT_DEVICE_ID_NX:
474                 regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
475                            (voice->EC & 0x00000fff);
476                 regs[0] = (voice->Delta << 24) | (voice->CSO & 0x00ffffff);
477                 regs[2] = ((voice->Delta << 16) & 0xff000000) |
478                         (voice->ESO & 0x00ffffff);
479                 regs[3] = (voice->Alpha << 20) |
480                         ((voice->FMS & 0x0000000f) << 16) | FmcRvolCvol;
481                 break;
482         default:
483                 snd_BUG();
484                 return;
485         }
486
487         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
488         outl(regs[0], TRID_REG(trident, CH_START + 0));
489         outl(regs[1], TRID_REG(trident, CH_START + 4));
490         outl(regs[2], TRID_REG(trident, CH_START + 8));
491         outl(regs[3], TRID_REG(trident, CH_START + 12));
492         outl(regs[4], TRID_REG(trident, CH_START + 16));
493
494 #if 0
495         printk("written %i channel:\n", voice->number);
496         printk("  regs[0] = 0x%x/0x%x\n", regs[0], inl(TRID_REG(trident, CH_START + 0)));
497         printk("  regs[1] = 0x%x/0x%x\n", regs[1], inl(TRID_REG(trident, CH_START + 4)));
498         printk("  regs[2] = 0x%x/0x%x\n", regs[2], inl(TRID_REG(trident, CH_START + 8)));
499         printk("  regs[3] = 0x%x/0x%x\n", regs[3], inl(TRID_REG(trident, CH_START + 12)));
500         printk("  regs[4] = 0x%x/0x%x\n", regs[4], inl(TRID_REG(trident, CH_START + 16)));
501 #endif
502 }
503
504 /*---------------------------------------------------------------------------
505    snd_trident_write_cso_reg
506   
507    Description: This routine will write the new CSO offset
508                 register to hardware.
509   
510    Paramters:   trident - pointer to target device class for 4DWave.
511                 voice - synthesizer voice structure
512                 CSO - new CSO value
513   
514   ---------------------------------------------------------------------------*/
515
516 static void snd_trident_write_cso_reg(struct snd_trident * trident,
517                                       struct snd_trident_voice * voice,
518                                       unsigned int CSO)
519 {
520         voice->CSO = CSO;
521         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
522         if (trident->device != TRIDENT_DEVICE_ID_NX) {
523                 outw(voice->CSO, TRID_REG(trident, CH_DX_CSO_ALPHA_FMS) + 2);
524         } else {
525                 outl((voice->Delta << 24) |
526                      (voice->CSO & 0x00ffffff), TRID_REG(trident, CH_NX_DELTA_CSO));
527         }
528 }
529
530 /*---------------------------------------------------------------------------
531    snd_trident_write_eso_reg
532   
533    Description: This routine will write the new ESO offset
534                 register to hardware.
535   
536    Paramters:   trident - pointer to target device class for 4DWave.
537                 voice - synthesizer voice structure
538                 ESO - new ESO value
539   
540   ---------------------------------------------------------------------------*/
541
542 static void snd_trident_write_eso_reg(struct snd_trident * trident,
543                                       struct snd_trident_voice * voice,
544                                       unsigned int ESO)
545 {
546         voice->ESO = ESO;
547         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
548         if (trident->device != TRIDENT_DEVICE_ID_NX) {
549                 outw(voice->ESO, TRID_REG(trident, CH_DX_ESO_DELTA) + 2);
550         } else {
551                 outl(((voice->Delta << 16) & 0xff000000) | (voice->ESO & 0x00ffffff),
552                      TRID_REG(trident, CH_NX_DELTA_ESO));
553         }
554 }
555
556 /*---------------------------------------------------------------------------
557    snd_trident_write_vol_reg
558   
559    Description: This routine will write the new voice volume
560                 register to hardware.
561   
562    Paramters:   trident - pointer to target device class for 4DWave.
563                 voice - synthesizer voice structure
564                 Vol - new voice volume
565   
566   ---------------------------------------------------------------------------*/
567
568 static void snd_trident_write_vol_reg(struct snd_trident * trident,
569                                       struct snd_trident_voice * voice,
570                                       unsigned int Vol)
571 {
572         voice->Vol = Vol;
573         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
574         switch (trident->device) {
575         case TRIDENT_DEVICE_ID_DX:
576         case TRIDENT_DEVICE_ID_NX:
577                 outb(voice->Vol >> 2, TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 2));
578                 break;
579         case TRIDENT_DEVICE_ID_SI7018:
580                 // printk("voice->Vol = 0x%x\n", voice->Vol);
581                 outw((voice->CTRL << 12) | voice->Vol,
582                      TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
583                 break;
584         }
585 }
586
587 /*---------------------------------------------------------------------------
588    snd_trident_write_pan_reg
589   
590    Description: This routine will write the new voice pan
591                 register to hardware.
592   
593    Paramters:   trident - pointer to target device class for 4DWave.
594                 voice - synthesizer voice structure
595                 Pan - new pan value
596   
597   ---------------------------------------------------------------------------*/
598
599 static void snd_trident_write_pan_reg(struct snd_trident * trident,
600                                       struct snd_trident_voice * voice,
601                                       unsigned int Pan)
602 {
603         voice->Pan = Pan;
604         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
605         outb(((voice->GVSel & 0x01) << 7) | (voice->Pan & 0x7f),
606              TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 3));
607 }
608
609 /*---------------------------------------------------------------------------
610    snd_trident_write_rvol_reg
611   
612    Description: This routine will write the new reverb volume
613                 register to hardware.
614   
615    Paramters:   trident - pointer to target device class for 4DWave.
616                 voice - synthesizer voice structure
617                 RVol - new reverb volume
618   
619   ---------------------------------------------------------------------------*/
620
621 static void snd_trident_write_rvol_reg(struct snd_trident * trident,
622                                        struct snd_trident_voice * voice,
623                                        unsigned int RVol)
624 {
625         voice->RVol = RVol;
626         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
627         outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
628              (voice->CVol & 0x007f),
629              TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
630                       CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
631 }
632
633 /*---------------------------------------------------------------------------
634    snd_trident_write_cvol_reg
635   
636    Description: This routine will write the new chorus volume
637                 register to hardware.
638   
639    Paramters:   trident - pointer to target device class for 4DWave.
640                 voice - synthesizer voice structure
641                 CVol - new chorus volume
642   
643   ---------------------------------------------------------------------------*/
644
645 static void snd_trident_write_cvol_reg(struct snd_trident * trident,
646                                        struct snd_trident_voice * voice,
647                                        unsigned int CVol)
648 {
649         voice->CVol = CVol;
650         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
651         outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
652              (voice->CVol & 0x007f),
653              TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
654                       CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
655 }
656
657 /*---------------------------------------------------------------------------
658    snd_trident_convert_rate
659
660    Description: This routine converts rate in HZ to hardware delta value.
661   
662    Paramters:   trident - pointer to target device class for 4DWave.
663                 rate - Real or Virtual channel number.
664   
665    Returns:     Delta value.
666   
667   ---------------------------------------------------------------------------*/
668 static unsigned int snd_trident_convert_rate(unsigned int rate)
669 {
670         unsigned int delta;
671
672         // We special case 44100 and 8000 since rounding with the equation
673         // does not give us an accurate enough value. For 11025 and 22050
674         // the equation gives us the best answer. All other frequencies will
675         // also use the equation. JDW
676         if (rate == 44100)
677                 delta = 0xeb3;
678         else if (rate == 8000)
679                 delta = 0x2ab;
680         else if (rate == 48000)
681                 delta = 0x1000;
682         else
683                 delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
684         return delta;
685 }
686
687 /*---------------------------------------------------------------------------
688    snd_trident_convert_adc_rate
689
690    Description: This routine converts rate in HZ to hardware delta value.
691   
692    Paramters:   trident - pointer to target device class for 4DWave.
693                 rate - Real or Virtual channel number.
694   
695    Returns:     Delta value.
696   
697   ---------------------------------------------------------------------------*/
698 static unsigned int snd_trident_convert_adc_rate(unsigned int rate)
699 {
700         unsigned int delta;
701
702         // We special case 44100 and 8000 since rounding with the equation
703         // does not give us an accurate enough value. For 11025 and 22050
704         // the equation gives us the best answer. All other frequencies will
705         // also use the equation. JDW
706         if (rate == 44100)
707                 delta = 0x116a;
708         else if (rate == 8000)
709                 delta = 0x6000;
710         else if (rate == 48000)
711                 delta = 0x1000;
712         else
713                 delta = ((48000 << 12) / rate) & 0x0000ffff;
714         return delta;
715 }
716
717 /*---------------------------------------------------------------------------
718    snd_trident_spurious_threshold
719
720    Description: This routine converts rate in HZ to spurious threshold.
721   
722    Paramters:   trident - pointer to target device class for 4DWave.
723                 rate - Real or Virtual channel number.
724   
725    Returns:     Delta value.
726   
727   ---------------------------------------------------------------------------*/
728 static unsigned int snd_trident_spurious_threshold(unsigned int rate,
729                                                    unsigned int period_size)
730 {
731         unsigned int res = (rate * period_size) / 48000;
732         if (res < 64)
733                 res = res / 2;
734         else
735                 res -= 32;
736         return res;
737 }
738
739 /*---------------------------------------------------------------------------
740    snd_trident_control_mode
741
742    Description: This routine returns a control mode for a PCM channel.
743   
744    Paramters:   trident - pointer to target device class for 4DWave.
745                 substream  - PCM substream
746   
747    Returns:     Control value.
748   
749   ---------------------------------------------------------------------------*/
750 static unsigned int snd_trident_control_mode(struct snd_pcm_substream *substream)
751 {
752         unsigned int CTRL;
753         struct snd_pcm_runtime *runtime = substream->runtime;
754
755         /* set ctrl mode
756            CTRL default: 8-bit (unsigned) mono, loop mode enabled
757          */
758         CTRL = 0x00000001;
759         if (snd_pcm_format_width(runtime->format) == 16)
760                 CTRL |= 0x00000008;     // 16-bit data
761         if (snd_pcm_format_signed(runtime->format))
762                 CTRL |= 0x00000002;     // signed data
763         if (runtime->channels > 1)
764                 CTRL |= 0x00000004;     // stereo data
765         return CTRL;
766 }
767
768 /*
769  *  PCM part
770  */
771
772 /*---------------------------------------------------------------------------
773    snd_trident_ioctl
774   
775    Description: Device I/O control handler for playback/capture parameters.
776   
777    Paramters:   substream  - PCM substream class
778                 cmd     - what ioctl message to process
779                 arg     - additional message infoarg     
780   
781    Returns:     Error status
782   
783   ---------------------------------------------------------------------------*/
784
785 static int snd_trident_ioctl(struct snd_pcm_substream *substream,
786                              unsigned int cmd,
787                              void *arg)
788 {
789         /* FIXME: it seems that with small periods the behaviour of
790                   trident hardware is unpredictable and interrupt generator
791                   is broken */
792         return snd_pcm_lib_ioctl(substream, cmd, arg);
793 }
794
795 /*---------------------------------------------------------------------------
796    snd_trident_allocate_pcm_mem
797   
798    Description: Allocate PCM ring buffer for given substream
799   
800    Parameters:  substream  - PCM substream class
801                 hw_params  - hardware parameters
802   
803    Returns:     Error status
804   
805   ---------------------------------------------------------------------------*/
806
807 static int snd_trident_allocate_pcm_mem(struct snd_pcm_substream *substream,
808                                         struct snd_pcm_hw_params *hw_params)
809 {
810         struct snd_trident *trident = snd_pcm_substream_chip(substream);
811         struct snd_pcm_runtime *runtime = substream->runtime;
812         struct snd_trident_voice *voice = runtime->private_data;
813         int err;
814
815         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
816                 return err;
817         if (trident->tlb.entries) {
818                 if (err > 0) { /* change */
819                         if (voice->memblk)
820                                 snd_trident_free_pages(trident, voice->memblk);
821                         voice->memblk = snd_trident_alloc_pages(trident, substream);
822                         if (voice->memblk == NULL)
823                                 return -ENOMEM;
824                 }
825         }
826         return 0;
827 }
828
829 /*---------------------------------------------------------------------------
830    snd_trident_allocate_evoice
831   
832    Description: Allocate extra voice as interrupt generator
833   
834    Parameters:  substream  - PCM substream class
835                 hw_params  - hardware parameters
836   
837    Returns:     Error status
838   
839   ---------------------------------------------------------------------------*/
840
841 static int snd_trident_allocate_evoice(struct snd_pcm_substream *substream,
842                                        struct snd_pcm_hw_params *hw_params)
843 {
844         struct snd_trident *trident = snd_pcm_substream_chip(substream);
845         struct snd_pcm_runtime *runtime = substream->runtime;
846         struct snd_trident_voice *voice = runtime->private_data;
847         struct snd_trident_voice *evoice = voice->extra;
848
849         /* voice management */
850
851         if (params_buffer_size(hw_params) / 2 != params_period_size(hw_params)) {
852                 if (evoice == NULL) {
853                         evoice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
854                         if (evoice == NULL)
855                                 return -ENOMEM;
856                         voice->extra = evoice;
857                         evoice->substream = substream;
858                 }
859         } else {
860                 if (evoice != NULL) {
861                         snd_trident_free_voice(trident, evoice);
862                         voice->extra = evoice = NULL;
863                 }
864         }
865
866         return 0;
867 }
868
869 /*---------------------------------------------------------------------------
870    snd_trident_hw_params
871   
872    Description: Set the hardware parameters for the playback device.
873   
874    Parameters:  substream  - PCM substream class
875                 hw_params  - hardware parameters
876   
877    Returns:     Error status
878   
879   ---------------------------------------------------------------------------*/
880
881 static int snd_trident_hw_params(struct snd_pcm_substream *substream,
882                                  struct snd_pcm_hw_params *hw_params)
883 {
884         int err;
885
886         err = snd_trident_allocate_pcm_mem(substream, hw_params);
887         if (err >= 0)
888                 err = snd_trident_allocate_evoice(substream, hw_params);
889         return err;
890 }
891
892 /*---------------------------------------------------------------------------
893    snd_trident_playback_hw_free
894   
895    Description: Release the hardware resources for the playback device.
896   
897    Parameters:  substream  - PCM substream class
898   
899    Returns:     Error status
900   
901   ---------------------------------------------------------------------------*/
902
903 static int snd_trident_hw_free(struct snd_pcm_substream *substream)
904 {
905         struct snd_trident *trident = snd_pcm_substream_chip(substream);
906         struct snd_pcm_runtime *runtime = substream->runtime;
907         struct snd_trident_voice *voice = runtime->private_data;
908         struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
909
910         if (trident->tlb.entries) {
911                 if (voice && voice->memblk) {
912                         snd_trident_free_pages(trident, voice->memblk);
913                         voice->memblk = NULL;
914                 }
915         }
916         snd_pcm_lib_free_pages(substream);
917         if (evoice != NULL) {
918                 snd_trident_free_voice(trident, evoice);
919                 voice->extra = NULL;
920         }
921         return 0;
922 }
923
924 /*---------------------------------------------------------------------------
925    snd_trident_playback_prepare
926   
927    Description: Prepare playback device for playback.
928   
929    Parameters:  substream  - PCM substream class
930   
931    Returns:     Error status
932   
933   ---------------------------------------------------------------------------*/
934
935 static int snd_trident_playback_prepare(struct snd_pcm_substream *substream)
936 {
937         struct snd_trident *trident = snd_pcm_substream_chip(substream);
938         struct snd_pcm_runtime *runtime = substream->runtime;
939         struct snd_trident_voice *voice = runtime->private_data;
940         struct snd_trident_voice *evoice = voice->extra;
941         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
942
943         spin_lock_irq(&trident->reg_lock);      
944
945         /* set delta (rate) value */
946         voice->Delta = snd_trident_convert_rate(runtime->rate);
947         voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
948
949         /* set Loop Begin Address */
950         if (voice->memblk)
951                 voice->LBA = voice->memblk->offset;
952         else
953                 voice->LBA = runtime->dma_addr;
954  
955         voice->CSO = 0;
956         voice->ESO = runtime->buffer_size - 1;  /* in samples */
957         voice->CTRL = snd_trident_control_mode(substream);
958         voice->FMC = 3;
959         voice->GVSel = 1;
960         voice->EC = 0;
961         voice->Alpha = 0;
962         voice->FMS = 0;
963         voice->Vol = mix->vol;
964         voice->RVol = mix->rvol;
965         voice->CVol = mix->cvol;
966         voice->Pan = mix->pan;
967         voice->Attribute = 0;
968 #if 0
969         voice->Attribute = (1<<(30-16))|(2<<(26-16))|
970                            (0<<(24-16))|(0x1f<<(19-16));
971 #else
972         voice->Attribute = 0;
973 #endif
974
975         snd_trident_write_voice_regs(trident, voice);
976
977         if (evoice != NULL) {
978                 evoice->Delta = voice->Delta;
979                 evoice->spurious_threshold = voice->spurious_threshold;
980                 evoice->LBA = voice->LBA;
981                 evoice->CSO = 0;
982                 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
983                 evoice->CTRL = voice->CTRL;
984                 evoice->FMC = 3;
985                 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
986                 evoice->EC = 0;
987                 evoice->Alpha = 0;
988                 evoice->FMS = 0;
989                 evoice->Vol = 0x3ff;                    /* mute */
990                 evoice->RVol = evoice->CVol = 0x7f;     /* mute */
991                 evoice->Pan = 0x7f;                     /* mute */
992 #if 0
993                 evoice->Attribute = (1<<(30-16))|(2<<(26-16))|
994                                     (0<<(24-16))|(0x1f<<(19-16));
995 #else
996                 evoice->Attribute = 0;
997 #endif
998                 snd_trident_write_voice_regs(trident, evoice);
999                 evoice->isync2 = 1;
1000                 evoice->isync_mark = runtime->period_size;
1001                 evoice->ESO = (runtime->period_size * 2) - 1;
1002         }
1003
1004         spin_unlock_irq(&trident->reg_lock);
1005
1006         return 0;
1007 }
1008
1009 /*---------------------------------------------------------------------------
1010    snd_trident_capture_hw_params
1011   
1012    Description: Set the hardware parameters for the capture device.
1013   
1014    Parameters:  substream  - PCM substream class
1015                 hw_params  - hardware parameters
1016   
1017    Returns:     Error status
1018   
1019   ---------------------------------------------------------------------------*/
1020
1021 static int snd_trident_capture_hw_params(struct snd_pcm_substream *substream,
1022                                          struct snd_pcm_hw_params *hw_params)
1023 {
1024         return snd_trident_allocate_pcm_mem(substream, hw_params);
1025 }
1026
1027 /*---------------------------------------------------------------------------
1028    snd_trident_capture_prepare
1029   
1030    Description: Prepare capture device for playback.
1031   
1032    Parameters:  substream  - PCM substream class
1033   
1034    Returns:     Error status
1035   
1036   ---------------------------------------------------------------------------*/
1037
1038 static int snd_trident_capture_prepare(struct snd_pcm_substream *substream)
1039 {
1040         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1041         struct snd_pcm_runtime *runtime = substream->runtime;
1042         struct snd_trident_voice *voice = runtime->private_data;
1043         unsigned int val, ESO_bytes;
1044
1045         spin_lock_irq(&trident->reg_lock);
1046
1047         // Initilize the channel and set channel Mode
1048         outb(0, TRID_REG(trident, LEGACY_DMAR15));
1049
1050         // Set DMA channel operation mode register
1051         outb(0x54, TRID_REG(trident, LEGACY_DMAR11));
1052
1053         // Set channel buffer Address, DMAR0 expects contiguous PCI memory area 
1054         voice->LBA = runtime->dma_addr;
1055         outl(voice->LBA, TRID_REG(trident, LEGACY_DMAR0));
1056         if (voice->memblk)
1057                 voice->LBA = voice->memblk->offset;
1058
1059         // set ESO
1060         ESO_bytes = snd_pcm_lib_buffer_bytes(substream) - 1;
1061         outb((ESO_bytes & 0x00ff0000) >> 16, TRID_REG(trident, LEGACY_DMAR6));
1062         outw((ESO_bytes & 0x0000ffff), TRID_REG(trident, LEGACY_DMAR4));
1063         ESO_bytes++;
1064
1065         // Set channel sample rate, 4.12 format
1066         val = (((unsigned int) 48000L << 12) + (runtime->rate/2)) / runtime->rate;
1067         outw(val, TRID_REG(trident, T4D_SBDELTA_DELTA_R));
1068
1069         // Set channel interrupt blk length
1070         if (snd_pcm_format_width(runtime->format) == 16) {
1071                 val = (unsigned short) ((ESO_bytes >> 1) - 1);
1072         } else {
1073                 val = (unsigned short) (ESO_bytes - 1);
1074         }
1075
1076         outl((val << 16) | val, TRID_REG(trident, T4D_SBBL_SBCL));
1077
1078         // Right now, set format and start to run captureing, 
1079         // continuous run loop enable.
1080         trident->bDMAStart = 0x19;      // 0001 1001b
1081
1082         if (snd_pcm_format_width(runtime->format) == 16)
1083                 trident->bDMAStart |= 0x80;
1084         if (snd_pcm_format_signed(runtime->format))
1085                 trident->bDMAStart |= 0x20;
1086         if (runtime->channels > 1)
1087                 trident->bDMAStart |= 0x40;
1088
1089         // Prepare capture intr channel
1090
1091         voice->Delta = snd_trident_convert_rate(runtime->rate);
1092         voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1093         voice->isync = 1;
1094         voice->isync_mark = runtime->period_size;
1095         voice->isync_max = runtime->buffer_size;
1096
1097         // Set voice parameters
1098         voice->CSO = 0;
1099         voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1100         voice->CTRL = snd_trident_control_mode(substream);
1101         voice->FMC = 3;
1102         voice->RVol = 0x7f;
1103         voice->CVol = 0x7f;
1104         voice->GVSel = 1;
1105         voice->Pan = 0x7f;              /* mute */
1106         voice->Vol = 0x3ff;             /* mute */
1107         voice->EC = 0;
1108         voice->Alpha = 0;
1109         voice->FMS = 0;
1110         voice->Attribute = 0;
1111
1112         snd_trident_write_voice_regs(trident, voice);
1113
1114         spin_unlock_irq(&trident->reg_lock);
1115         return 0;
1116 }
1117
1118 /*---------------------------------------------------------------------------
1119    snd_trident_si7018_capture_hw_params
1120   
1121    Description: Set the hardware parameters for the capture device.
1122   
1123    Parameters:  substream  - PCM substream class
1124                 hw_params  - hardware parameters
1125   
1126    Returns:     Error status
1127   
1128   ---------------------------------------------------------------------------*/
1129
1130 static int snd_trident_si7018_capture_hw_params(struct snd_pcm_substream *substream,
1131                                                 struct snd_pcm_hw_params *hw_params)
1132 {
1133         int err;
1134
1135         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
1136                 return err;
1137
1138         return snd_trident_allocate_evoice(substream, hw_params);
1139 }
1140
1141 /*---------------------------------------------------------------------------
1142    snd_trident_si7018_capture_hw_free
1143   
1144    Description: Release the hardware resources for the capture device.
1145   
1146    Parameters:  substream  - PCM substream class
1147   
1148    Returns:     Error status
1149   
1150   ---------------------------------------------------------------------------*/
1151
1152 static int snd_trident_si7018_capture_hw_free(struct snd_pcm_substream *substream)
1153 {
1154         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1155         struct snd_pcm_runtime *runtime = substream->runtime;
1156         struct snd_trident_voice *voice = runtime->private_data;
1157         struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
1158
1159         snd_pcm_lib_free_pages(substream);
1160         if (evoice != NULL) {
1161                 snd_trident_free_voice(trident, evoice);
1162                 voice->extra = NULL;
1163         }
1164         return 0;
1165 }
1166
1167 /*---------------------------------------------------------------------------
1168    snd_trident_si7018_capture_prepare
1169   
1170    Description: Prepare capture device for playback.
1171   
1172    Parameters:  substream  - PCM substream class
1173   
1174    Returns:     Error status
1175   
1176   ---------------------------------------------------------------------------*/
1177
1178 static int snd_trident_si7018_capture_prepare(struct snd_pcm_substream *substream)
1179 {
1180         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1181         struct snd_pcm_runtime *runtime = substream->runtime;
1182         struct snd_trident_voice *voice = runtime->private_data;
1183         struct snd_trident_voice *evoice = voice->extra;
1184
1185         spin_lock_irq(&trident->reg_lock);
1186
1187         voice->LBA = runtime->dma_addr;
1188         voice->Delta = snd_trident_convert_adc_rate(runtime->rate);
1189         voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1190
1191         // Set voice parameters
1192         voice->CSO = 0;
1193         voice->ESO = runtime->buffer_size - 1;          /* in samples */
1194         voice->CTRL = snd_trident_control_mode(substream);
1195         voice->FMC = 0;
1196         voice->RVol = 0;
1197         voice->CVol = 0;
1198         voice->GVSel = 1;
1199         voice->Pan = T4D_DEFAULT_PCM_PAN;
1200         voice->Vol = 0;
1201         voice->EC = 0;
1202         voice->Alpha = 0;
1203         voice->FMS = 0;
1204
1205         voice->Attribute = (2 << (30-16)) |
1206                            (2 << (26-16)) |
1207                            (2 << (24-16)) |
1208                            (1 << (23-16));
1209
1210         snd_trident_write_voice_regs(trident, voice);
1211
1212         if (evoice != NULL) {
1213                 evoice->Delta = snd_trident_convert_rate(runtime->rate);
1214                 evoice->spurious_threshold = voice->spurious_threshold;
1215                 evoice->LBA = voice->LBA;
1216                 evoice->CSO = 0;
1217                 evoice->ESO = (runtime->period_size * 2) + 20 - 1; /* in samples, 20 means correction */
1218                 evoice->CTRL = voice->CTRL;
1219                 evoice->FMC = 3;
1220                 evoice->GVSel = 0;
1221                 evoice->EC = 0;
1222                 evoice->Alpha = 0;
1223                 evoice->FMS = 0;
1224                 evoice->Vol = 0x3ff;                    /* mute */
1225                 evoice->RVol = evoice->CVol = 0x7f;     /* mute */
1226                 evoice->Pan = 0x7f;                     /* mute */
1227                 evoice->Attribute = 0;
1228                 snd_trident_write_voice_regs(trident, evoice);
1229                 evoice->isync2 = 1;
1230                 evoice->isync_mark = runtime->period_size;
1231                 evoice->ESO = (runtime->period_size * 2) - 1;
1232         }
1233         
1234         spin_unlock_irq(&trident->reg_lock);
1235         return 0;
1236 }
1237
1238 /*---------------------------------------------------------------------------
1239    snd_trident_foldback_prepare
1240   
1241    Description: Prepare foldback capture device for playback.
1242   
1243    Parameters:  substream  - PCM substream class
1244   
1245    Returns:     Error status
1246   
1247   ---------------------------------------------------------------------------*/
1248
1249 static int snd_trident_foldback_prepare(struct snd_pcm_substream *substream)
1250 {
1251         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1252         struct snd_pcm_runtime *runtime = substream->runtime;
1253         struct snd_trident_voice *voice = runtime->private_data;
1254         struct snd_trident_voice *evoice = voice->extra;
1255
1256         spin_lock_irq(&trident->reg_lock);
1257
1258         /* Set channel buffer Address */
1259         if (voice->memblk)
1260                 voice->LBA = voice->memblk->offset;
1261         else
1262                 voice->LBA = runtime->dma_addr;
1263
1264         /* set target ESO for channel */
1265         voice->ESO = runtime->buffer_size - 1;  /* in samples */
1266
1267         /* set sample rate */
1268         voice->Delta = 0x1000;
1269         voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1270
1271         voice->CSO = 0;
1272         voice->CTRL = snd_trident_control_mode(substream);
1273         voice->FMC = 3;
1274         voice->RVol = 0x7f;
1275         voice->CVol = 0x7f;
1276         voice->GVSel = 1;
1277         voice->Pan = 0x7f;      /* mute */
1278         voice->Vol = 0x3ff;     /* mute */
1279         voice->EC = 0;
1280         voice->Alpha = 0;
1281         voice->FMS = 0;
1282         voice->Attribute = 0;
1283
1284         /* set up capture channel */
1285         outb(((voice->number & 0x3f) | 0x80), TRID_REG(trident, T4D_RCI + voice->foldback_chan));
1286
1287         snd_trident_write_voice_regs(trident, voice);
1288
1289         if (evoice != NULL) {
1290                 evoice->Delta = voice->Delta;
1291                 evoice->spurious_threshold = voice->spurious_threshold;
1292                 evoice->LBA = voice->LBA;
1293                 evoice->CSO = 0;
1294                 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1295                 evoice->CTRL = voice->CTRL;
1296                 evoice->FMC = 3;
1297                 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1298                 evoice->EC = 0;
1299                 evoice->Alpha = 0;
1300                 evoice->FMS = 0;
1301                 evoice->Vol = 0x3ff;                    /* mute */
1302                 evoice->RVol = evoice->CVol = 0x7f;     /* mute */
1303                 evoice->Pan = 0x7f;                     /* mute */
1304                 evoice->Attribute = 0;
1305                 snd_trident_write_voice_regs(trident, evoice);
1306                 evoice->isync2 = 1;
1307                 evoice->isync_mark = runtime->period_size;
1308                 evoice->ESO = (runtime->period_size * 2) - 1;
1309         }
1310
1311         spin_unlock_irq(&trident->reg_lock);
1312         return 0;
1313 }
1314
1315 /*---------------------------------------------------------------------------
1316    snd_trident_spdif_hw_params
1317   
1318    Description: Set the hardware parameters for the spdif device.
1319   
1320    Parameters:  substream  - PCM substream class
1321                 hw_params  - hardware parameters
1322   
1323    Returns:     Error status
1324   
1325   ---------------------------------------------------------------------------*/
1326
1327 static int snd_trident_spdif_hw_params(struct snd_pcm_substream *substream,
1328                                        struct snd_pcm_hw_params *hw_params)
1329 {
1330         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1331         unsigned int old_bits = 0, change = 0;
1332         int err;
1333
1334         err = snd_trident_allocate_pcm_mem(substream, hw_params);
1335         if (err < 0)
1336                 return err;
1337
1338         if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
1339                 err = snd_trident_allocate_evoice(substream, hw_params);
1340                 if (err < 0)
1341                         return err;
1342         }
1343
1344         /* prepare SPDIF channel */
1345         spin_lock_irq(&trident->reg_lock);
1346         old_bits = trident->spdif_pcm_bits;
1347         if (old_bits & IEC958_AES0_PROFESSIONAL)
1348                 trident->spdif_pcm_bits &= ~IEC958_AES0_PRO_FS;
1349         else
1350                 trident->spdif_pcm_bits &= ~(IEC958_AES3_CON_FS << 24);
1351         if (params_rate(hw_params) >= 48000) {
1352                 trident->spdif_pcm_ctrl = 0x3c; // 48000 Hz
1353                 trident->spdif_pcm_bits |=
1354                         trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1355                                 IEC958_AES0_PRO_FS_48000 :
1356                                 (IEC958_AES3_CON_FS_48000 << 24);
1357         }
1358         else if (params_rate(hw_params) >= 44100) {
1359                 trident->spdif_pcm_ctrl = 0x3e; // 44100 Hz
1360                 trident->spdif_pcm_bits |=
1361                         trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1362                                 IEC958_AES0_PRO_FS_44100 :
1363                                 (IEC958_AES3_CON_FS_44100 << 24);
1364         }
1365         else {
1366                 trident->spdif_pcm_ctrl = 0x3d; // 32000 Hz
1367                 trident->spdif_pcm_bits |=
1368                         trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1369                                 IEC958_AES0_PRO_FS_32000 :
1370                                 (IEC958_AES3_CON_FS_32000 << 24);
1371         }
1372         change = old_bits != trident->spdif_pcm_bits;
1373         spin_unlock_irq(&trident->reg_lock);
1374
1375         if (change)
1376                 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE, &trident->spdif_pcm_ctl->id);
1377
1378         return 0;
1379 }
1380
1381 /*---------------------------------------------------------------------------
1382    snd_trident_spdif_prepare
1383   
1384    Description: Prepare SPDIF device for playback.
1385   
1386    Parameters:  substream  - PCM substream class
1387   
1388    Returns:     Error status
1389   
1390   ---------------------------------------------------------------------------*/
1391
1392 static int snd_trident_spdif_prepare(struct snd_pcm_substream *substream)
1393 {
1394         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1395         struct snd_pcm_runtime *runtime = substream->runtime;
1396         struct snd_trident_voice *voice = runtime->private_data;
1397         struct snd_trident_voice *evoice = voice->extra;
1398         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
1399         unsigned int RESO, LBAO;
1400         unsigned int temp;
1401
1402         spin_lock_irq(&trident->reg_lock);
1403
1404         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1405
1406                 /* set delta (rate) value */
1407                 voice->Delta = snd_trident_convert_rate(runtime->rate);
1408                 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1409
1410                 /* set Loop Back Address */
1411                 LBAO = runtime->dma_addr;
1412                 if (voice->memblk)
1413                         voice->LBA = voice->memblk->offset;
1414                 else
1415                         voice->LBA = LBAO;
1416
1417                 voice->isync = 1;
1418                 voice->isync3 = 1;
1419                 voice->isync_mark = runtime->period_size;
1420                 voice->isync_max = runtime->buffer_size;
1421
1422                 /* set target ESO for channel */
1423                 RESO = runtime->buffer_size - 1;
1424                 voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1425
1426                 /* set ctrl mode */
1427                 voice->CTRL = snd_trident_control_mode(substream);
1428
1429                 voice->FMC = 3;
1430                 voice->RVol = 0x7f;
1431                 voice->CVol = 0x7f;
1432                 voice->GVSel = 1;
1433                 voice->Pan = 0x7f;
1434                 voice->Vol = 0x3ff;
1435                 voice->EC = 0;
1436                 voice->CSO = 0;
1437                 voice->Alpha = 0;
1438                 voice->FMS = 0;
1439                 voice->Attribute = 0;
1440
1441                 /* prepare surrogate IRQ channel */
1442                 snd_trident_write_voice_regs(trident, voice);
1443
1444                 outw((RESO & 0xffff), TRID_REG(trident, NX_SPESO));
1445                 outb((RESO >> 16), TRID_REG(trident, NX_SPESO + 2));
1446                 outl((LBAO & 0xfffffffc), TRID_REG(trident, NX_SPLBA));
1447                 outw((voice->CSO & 0xffff), TRID_REG(trident, NX_SPCTRL_SPCSO));
1448                 outb((voice->CSO >> 16), TRID_REG(trident, NX_SPCTRL_SPCSO + 2));
1449
1450                 /* set SPDIF setting */
1451                 outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1452                 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
1453
1454         } else {        /* SiS */
1455         
1456                 /* set delta (rate) value */
1457                 voice->Delta = 0x800;
1458                 voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1459
1460                 /* set Loop Begin Address */
1461                 if (voice->memblk)
1462                         voice->LBA = voice->memblk->offset;
1463                 else
1464                         voice->LBA = runtime->dma_addr;
1465
1466                 voice->CSO = 0;
1467                 voice->ESO = runtime->buffer_size - 1;  /* in samples */
1468                 voice->CTRL = snd_trident_control_mode(substream);
1469                 voice->FMC = 3;
1470                 voice->GVSel = 1;
1471                 voice->EC = 0;
1472                 voice->Alpha = 0;
1473                 voice->FMS = 0;
1474                 voice->Vol = mix->vol;
1475                 voice->RVol = mix->rvol;
1476                 voice->CVol = mix->cvol;
1477                 voice->Pan = mix->pan;
1478                 voice->Attribute = (1<<(30-16))|(7<<(26-16))|
1479                                    (0<<(24-16))|(0<<(19-16));
1480
1481                 snd_trident_write_voice_regs(trident, voice);
1482
1483                 if (evoice != NULL) {
1484                         evoice->Delta = voice->Delta;
1485                         evoice->spurious_threshold = voice->spurious_threshold;
1486                         evoice->LBA = voice->LBA;
1487                         evoice->CSO = 0;
1488                         evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1489                         evoice->CTRL = voice->CTRL;
1490                         evoice->FMC = 3;
1491                         evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1492                         evoice->EC = 0;
1493                         evoice->Alpha = 0;
1494                         evoice->FMS = 0;
1495                         evoice->Vol = 0x3ff;                    /* mute */
1496                         evoice->RVol = evoice->CVol = 0x7f;     /* mute */
1497                         evoice->Pan = 0x7f;                     /* mute */
1498                         evoice->Attribute = 0;
1499                         snd_trident_write_voice_regs(trident, evoice);
1500                         evoice->isync2 = 1;
1501                         evoice->isync_mark = runtime->period_size;
1502                         evoice->ESO = (runtime->period_size * 2) - 1;
1503                 }
1504
1505                 outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
1506                 temp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
1507                 temp &= ~(1<<19);
1508                 outl(temp, TRID_REG(trident, T4D_LFO_GC_CIR));
1509                 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1510                 temp |= SPDIF_EN;
1511                 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1512         }
1513
1514         spin_unlock_irq(&trident->reg_lock);
1515
1516         return 0;
1517 }
1518
1519 /*---------------------------------------------------------------------------
1520    snd_trident_trigger
1521   
1522    Description: Start/stop devices
1523   
1524    Parameters:  substream  - PCM substream class
1525                 cmd     - trigger command (STOP, GO)
1526   
1527    Returns:     Error status
1528   
1529   ---------------------------------------------------------------------------*/
1530
1531 static int snd_trident_trigger(struct snd_pcm_substream *substream,
1532                                int cmd)
1533                                     
1534 {
1535         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1536         struct list_head *pos;
1537         struct snd_pcm_substream *s;
1538         unsigned int what, whati, capture_flag, spdif_flag;
1539         struct snd_trident_voice *voice, *evoice;
1540         unsigned int val, go;
1541
1542         switch (cmd) {
1543         case SNDRV_PCM_TRIGGER_START:
1544         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1545         case SNDRV_PCM_TRIGGER_RESUME:
1546                 go = 1;
1547                 break;
1548         case SNDRV_PCM_TRIGGER_STOP:
1549         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1550         case SNDRV_PCM_TRIGGER_SUSPEND:
1551                 go = 0;
1552                 break;
1553         default:
1554                 return -EINVAL;
1555         }
1556         what = whati = capture_flag = spdif_flag = 0;
1557         spin_lock(&trident->reg_lock);
1558         val = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
1559         snd_pcm_group_for_each(pos, substream) {
1560                 s = snd_pcm_group_substream_entry(pos);
1561                 if ((struct snd_trident *) snd_pcm_substream_chip(s) == trident) {
1562                         voice = s->runtime->private_data;
1563                         evoice = voice->extra;
1564                         what |= 1 << (voice->number & 0x1f);
1565                         if (evoice == NULL) {
1566                                 whati |= 1 << (voice->number & 0x1f);
1567                         } else {
1568                                 what |= 1 << (evoice->number & 0x1f);
1569                                 whati |= 1 << (evoice->number & 0x1f);
1570                                 if (go)
1571                                         evoice->stimer = val;
1572                         }
1573                         if (go) {
1574                                 voice->running = 1;
1575                                 voice->stimer = val;
1576                         } else {
1577                                 voice->running = 0;
1578                         }
1579                         snd_pcm_trigger_done(s, substream);
1580                         if (voice->capture)
1581                                 capture_flag = 1;
1582                         if (voice->spdif)
1583                                 spdif_flag = 1;
1584                 }
1585         }
1586         if (spdif_flag) {
1587                 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1588                         outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
1589                         outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1590                 } else {
1591                         outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
1592                         val = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) | SPDIF_EN;
1593                         outl(val, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1594                 }
1595         }
1596         if (!go)
1597                 outl(what, TRID_REG(trident, T4D_STOP_B));
1598         val = inl(TRID_REG(trident, T4D_AINTEN_B));
1599         if (go) {
1600                 val |= whati;
1601         } else {
1602                 val &= ~whati;
1603         }
1604         outl(val, TRID_REG(trident, T4D_AINTEN_B));
1605         if (go) {
1606                 outl(what, TRID_REG(trident, T4D_START_B));
1607
1608                 if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
1609                         outb(trident->bDMAStart, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
1610         } else {
1611                 if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
1612                         outb(0x00, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
1613         }
1614         spin_unlock(&trident->reg_lock);
1615         return 0;
1616 }
1617
1618 /*---------------------------------------------------------------------------
1619    snd_trident_playback_pointer
1620   
1621    Description: This routine return the playback position
1622                 
1623    Parameters:  substream  - PCM substream class
1624
1625    Returns:     position of buffer
1626   
1627   ---------------------------------------------------------------------------*/
1628
1629 static snd_pcm_uframes_t snd_trident_playback_pointer(struct snd_pcm_substream *substream)
1630 {
1631         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1632         struct snd_pcm_runtime *runtime = substream->runtime;
1633         struct snd_trident_voice *voice = runtime->private_data;
1634         unsigned int cso;
1635
1636         if (!voice->running)
1637                 return 0;
1638
1639         spin_lock(&trident->reg_lock);
1640
1641         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
1642
1643         if (trident->device != TRIDENT_DEVICE_ID_NX) {
1644                 cso = inw(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS + 2));
1645         } else {                // ID_4DWAVE_NX
1646                 cso = (unsigned int) inl(TRID_REG(trident, CH_NX_DELTA_CSO)) & 0x00ffffff;
1647         }
1648
1649         spin_unlock(&trident->reg_lock);
1650
1651         if (cso >= runtime->buffer_size)
1652                 cso = 0;
1653
1654         return cso;
1655 }
1656
1657 /*---------------------------------------------------------------------------
1658    snd_trident_capture_pointer
1659   
1660    Description: This routine return the capture position
1661                 
1662    Paramters:   pcm1    - PCM device class
1663
1664    Returns:     position of buffer
1665   
1666   ---------------------------------------------------------------------------*/
1667
1668 static snd_pcm_uframes_t snd_trident_capture_pointer(struct snd_pcm_substream *substream)
1669 {
1670         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1671         struct snd_pcm_runtime *runtime = substream->runtime;
1672         struct snd_trident_voice *voice = runtime->private_data;
1673         unsigned int result;
1674
1675         if (!voice->running)
1676                 return 0;
1677
1678         result = inw(TRID_REG(trident, T4D_SBBL_SBCL));
1679         if (runtime->channels > 1)
1680                 result >>= 1;
1681         if (result > 0)
1682                 result = runtime->buffer_size - result;
1683
1684         return result;
1685 }
1686
1687 /*---------------------------------------------------------------------------
1688    snd_trident_spdif_pointer
1689   
1690    Description: This routine return the SPDIF playback position
1691                 
1692    Parameters:  substream  - PCM substream class
1693
1694    Returns:     position of buffer
1695   
1696   ---------------------------------------------------------------------------*/
1697
1698 static snd_pcm_uframes_t snd_trident_spdif_pointer(struct snd_pcm_substream *substream)
1699 {
1700         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1701         struct snd_pcm_runtime *runtime = substream->runtime;
1702         struct snd_trident_voice *voice = runtime->private_data;
1703         unsigned int result;
1704
1705         if (!voice->running)
1706                 return 0;
1707
1708         result = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
1709
1710         return result;
1711 }
1712
1713 /*
1714  *  Playback support device description
1715  */
1716
1717 static struct snd_pcm_hardware snd_trident_playback =
1718 {
1719         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1720                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1721                                  SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1722                                  SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1723         .formats =              (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1724                                  SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1725         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1726         .rate_min =             4000,
1727         .rate_max =             48000,
1728         .channels_min =         1,
1729         .channels_max =         2,
1730         .buffer_bytes_max =     (256*1024),
1731         .period_bytes_min =     64,
1732         .period_bytes_max =     (256*1024),
1733         .periods_min =          1,
1734         .periods_max =          1024,
1735         .fifo_size =            0,
1736 };
1737
1738 /*
1739  *  Capture support device description
1740  */
1741
1742 static struct snd_pcm_hardware snd_trident_capture =
1743 {
1744         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1745                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1746                                  SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1747                                  SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1748         .formats =              (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1749                                  SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1750         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1751         .rate_min =             4000,
1752         .rate_max =             48000,
1753         .channels_min =         1,
1754         .channels_max =         2,
1755         .buffer_bytes_max =     (128*1024),
1756         .period_bytes_min =     64,
1757         .period_bytes_max =     (128*1024),
1758         .periods_min =          1,
1759         .periods_max =          1024,
1760         .fifo_size =            0,
1761 };
1762
1763 /*
1764  *  Foldback capture support device description
1765  */
1766
1767 static struct snd_pcm_hardware snd_trident_foldback =
1768 {
1769         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1770                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1771                                  SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1772                                  SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1773         .formats =              SNDRV_PCM_FMTBIT_S16_LE,
1774         .rates =                SNDRV_PCM_RATE_48000,
1775         .rate_min =             48000,
1776         .rate_max =             48000,
1777         .channels_min =         2,
1778         .channels_max =         2,
1779         .buffer_bytes_max =     (128*1024),
1780         .period_bytes_min =     64,
1781         .period_bytes_max =     (128*1024),
1782         .periods_min =          1,
1783         .periods_max =          1024,
1784         .fifo_size =            0,
1785 };
1786
1787 /*
1788  *  SPDIF playback support device description
1789  */
1790
1791 static struct snd_pcm_hardware snd_trident_spdif =
1792 {
1793         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1794                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1795                                  SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1796                                  SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1797         .formats =              SNDRV_PCM_FMTBIT_S16_LE,
1798         .rates =                (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
1799                                  SNDRV_PCM_RATE_48000),
1800         .rate_min =             32000,
1801         .rate_max =             48000,
1802         .channels_min =         2,
1803         .channels_max =         2,
1804         .buffer_bytes_max =     (128*1024),
1805         .period_bytes_min =     64,
1806         .period_bytes_max =     (128*1024),
1807         .periods_min =          1,
1808         .periods_max =          1024,
1809         .fifo_size =            0,
1810 };
1811
1812 static struct snd_pcm_hardware snd_trident_spdif_7018 =
1813 {
1814         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1815                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1816                                  SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
1817                                  SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1818         .formats =              SNDRV_PCM_FMTBIT_S16_LE,
1819         .rates =                SNDRV_PCM_RATE_48000,
1820         .rate_min =             48000,
1821         .rate_max =             48000,
1822         .channels_min =         2,
1823         .channels_max =         2,
1824         .buffer_bytes_max =     (128*1024),
1825         .period_bytes_min =     64,
1826         .period_bytes_max =     (128*1024),
1827         .periods_min =          1,
1828         .periods_max =          1024,
1829         .fifo_size =            0,
1830 };
1831
1832 static void snd_trident_pcm_free_substream(struct snd_pcm_runtime *runtime)
1833 {
1834         struct snd_trident_voice *voice = runtime->private_data;
1835         struct snd_trident *trident;
1836
1837         if (voice) {
1838                 trident = voice->trident;
1839                 snd_trident_free_voice(trident, voice);
1840         }
1841 }
1842
1843 static int snd_trident_playback_open(struct snd_pcm_substream *substream)
1844 {
1845         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1846         struct snd_pcm_runtime *runtime = substream->runtime;
1847         struct snd_trident_voice *voice;
1848
1849         voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1850         if (voice == NULL)
1851                 return -EAGAIN;
1852         snd_trident_pcm_mixer_build(trident, voice, substream);
1853         voice->substream = substream;
1854         runtime->private_data = voice;
1855         runtime->private_free = snd_trident_pcm_free_substream;
1856         runtime->hw = snd_trident_playback;
1857         snd_pcm_set_sync(substream);
1858         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1859         return 0;
1860 }
1861
1862 /*---------------------------------------------------------------------------
1863    snd_trident_playback_close
1864   
1865    Description: This routine will close the 4DWave playback device. For now 
1866                 we will simply free the dma transfer buffer.
1867                 
1868    Parameters:  substream  - PCM substream class
1869
1870   ---------------------------------------------------------------------------*/
1871 static int snd_trident_playback_close(struct snd_pcm_substream *substream)
1872 {
1873         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1874         struct snd_pcm_runtime *runtime = substream->runtime;
1875         struct snd_trident_voice *voice = runtime->private_data;
1876
1877         snd_trident_pcm_mixer_free(trident, voice, substream);
1878         return 0;
1879 }
1880
1881 /*---------------------------------------------------------------------------
1882    snd_trident_spdif_open
1883   
1884    Description: This routine will open the 4DWave SPDIF device.
1885
1886    Parameters:  substream  - PCM substream class
1887
1888    Returns:     status  - success or failure flag
1889   
1890   ---------------------------------------------------------------------------*/
1891
1892 static int snd_trident_spdif_open(struct snd_pcm_substream *substream)
1893 {
1894         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1895         struct snd_trident_voice *voice;
1896         struct snd_pcm_runtime *runtime = substream->runtime;
1897         
1898         voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1899         if (voice == NULL)
1900                 return -EAGAIN;
1901         voice->spdif = 1;
1902         voice->substream = substream;
1903         spin_lock_irq(&trident->reg_lock);
1904         trident->spdif_pcm_bits = trident->spdif_bits;
1905         spin_unlock_irq(&trident->reg_lock);
1906
1907         runtime->private_data = voice;
1908         runtime->private_free = snd_trident_pcm_free_substream;
1909         if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
1910                 runtime->hw = snd_trident_spdif;
1911         } else {
1912                 runtime->hw = snd_trident_spdif_7018;
1913         }
1914
1915         trident->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1916         snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
1917                        SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
1918
1919         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1920         return 0;
1921 }
1922
1923
1924 /*---------------------------------------------------------------------------
1925    snd_trident_spdif_close
1926   
1927    Description: This routine will close the 4DWave SPDIF device.
1928                 
1929    Parameters:  substream  - PCM substream class
1930
1931   ---------------------------------------------------------------------------*/
1932
1933 static int snd_trident_spdif_close(struct snd_pcm_substream *substream)
1934 {
1935         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1936         unsigned int temp;
1937
1938         spin_lock_irq(&trident->reg_lock);
1939         // restore default SPDIF setting
1940         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1941                 outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1942                 outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
1943         } else {
1944                 outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
1945                 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1946                 if (trident->spdif_ctrl) {
1947                         temp |= SPDIF_EN;
1948                 } else {
1949                         temp &= ~SPDIF_EN;
1950                 }
1951                 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1952         }
1953         spin_unlock_irq(&trident->reg_lock);
1954         trident->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1955         snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
1956                        SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
1957         return 0;
1958 }
1959
1960 /*---------------------------------------------------------------------------
1961    snd_trident_capture_open
1962   
1963    Description: This routine will open the 4DWave capture device.
1964
1965    Parameters:  substream  - PCM substream class
1966
1967    Returns:     status  - success or failure flag
1968
1969   ---------------------------------------------------------------------------*/
1970
1971 static int snd_trident_capture_open(struct snd_pcm_substream *substream)
1972 {
1973         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1974         struct snd_trident_voice *voice;
1975         struct snd_pcm_runtime *runtime = substream->runtime;
1976
1977         voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
1978         if (voice == NULL)
1979                 return -EAGAIN;
1980         voice->capture = 1;
1981         voice->substream = substream;
1982         runtime->private_data = voice;
1983         runtime->private_free = snd_trident_pcm_free_substream;
1984         runtime->hw = snd_trident_capture;
1985         snd_pcm_set_sync(substream);
1986         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1987         return 0;
1988 }
1989
1990 /*---------------------------------------------------------------------------
1991    snd_trident_capture_close
1992   
1993    Description: This routine will close the 4DWave capture device. For now 
1994                 we will simply free the dma transfer buffer.
1995                 
1996    Parameters:  substream  - PCM substream class
1997
1998   ---------------------------------------------------------------------------*/
1999 static int snd_trident_capture_close(struct snd_pcm_substream *substream)
2000 {
2001         return 0;
2002 }
2003
2004 /*---------------------------------------------------------------------------
2005    snd_trident_foldback_open
2006   
2007    Description: This routine will open the 4DWave foldback capture device.
2008
2009    Parameters:  substream  - PCM substream class
2010
2011    Returns:     status  - success or failure flag
2012
2013   ---------------------------------------------------------------------------*/
2014
2015 static int snd_trident_foldback_open(struct snd_pcm_substream *substream)
2016 {
2017         struct snd_trident *trident = snd_pcm_substream_chip(substream);
2018         struct snd_trident_voice *voice;
2019         struct snd_pcm_runtime *runtime = substream->runtime;
2020
2021         voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
2022         if (voice == NULL)
2023                 return -EAGAIN;
2024         voice->foldback_chan = substream->number;
2025         voice->substream = substream;
2026         runtime->private_data = voice;
2027         runtime->private_free = snd_trident_pcm_free_substream;
2028         runtime->hw = snd_trident_foldback;
2029         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
2030         return 0;
2031 }
2032
2033 /*---------------------------------------------------------------------------
2034    snd_trident_foldback_close
2035   
2036    Description: This routine will close the 4DWave foldback capture device. 
2037                 For now we will simply free the dma transfer buffer.
2038                 
2039    Parameters:  substream  - PCM substream class
2040
2041   ---------------------------------------------------------------------------*/
2042 static int snd_trident_foldback_close(struct snd_pcm_substream *substream)
2043 {
2044         struct snd_trident *trident = snd_pcm_substream_chip(substream);
2045         struct snd_trident_voice *voice;
2046         struct snd_pcm_runtime *runtime = substream->runtime;
2047         voice = runtime->private_data;
2048         
2049         /* stop capture channel */
2050         spin_lock_irq(&trident->reg_lock);
2051         outb(0x00, TRID_REG(trident, T4D_RCI + voice->foldback_chan));
2052         spin_unlock_irq(&trident->reg_lock);
2053         return 0;
2054 }
2055
2056 /*---------------------------------------------------------------------------
2057    PCM operations
2058   ---------------------------------------------------------------------------*/
2059
2060 static struct snd_pcm_ops snd_trident_playback_ops = {
2061         .open =         snd_trident_playback_open,
2062         .close =        snd_trident_playback_close,
2063         .ioctl =        snd_trident_ioctl,
2064         .hw_params =    snd_trident_hw_params,
2065         .hw_free =      snd_trident_hw_free,
2066         .prepare =      snd_trident_playback_prepare,
2067         .trigger =      snd_trident_trigger,
2068         .pointer =      snd_trident_playback_pointer,
2069 };
2070
2071 static struct snd_pcm_ops snd_trident_nx_playback_ops = {
2072         .open =         snd_trident_playback_open,
2073         .close =        snd_trident_playback_close,
2074         .ioctl =        snd_trident_ioctl,
2075         .hw_params =    snd_trident_hw_params,
2076         .hw_free =      snd_trident_hw_free,
2077         .prepare =      snd_trident_playback_prepare,
2078         .trigger =      snd_trident_trigger,
2079         .pointer =      snd_trident_playback_pointer,
2080         .page =         snd_pcm_sgbuf_ops_page,
2081 };
2082
2083 static struct snd_pcm_ops snd_trident_capture_ops = {
2084         .open =         snd_trident_capture_open,
2085         .close =        snd_trident_capture_close,
2086         .ioctl =        snd_trident_ioctl,
2087         .hw_params =    snd_trident_capture_hw_params,
2088         .hw_free =      snd_trident_hw_free,
2089         .prepare =      snd_trident_capture_prepare,
2090         .trigger =      snd_trident_trigger,
2091         .pointer =      snd_trident_capture_pointer,
2092 };
2093
2094 static struct snd_pcm_ops snd_trident_si7018_capture_ops = {
2095         .open =         snd_trident_capture_open,
2096         .close =        snd_trident_capture_close,
2097         .ioctl =        snd_trident_ioctl,
2098         .hw_params =    snd_trident_si7018_capture_hw_params,
2099         .hw_free =      snd_trident_si7018_capture_hw_free,
2100         .prepare =      snd_trident_si7018_capture_prepare,
2101         .trigger =      snd_trident_trigger,
2102         .pointer =      snd_trident_playback_pointer,
2103 };
2104
2105 static struct snd_pcm_ops snd_trident_foldback_ops = {
2106         .open =         snd_trident_foldback_open,
2107         .close =        snd_trident_foldback_close,
2108         .ioctl =        snd_trident_ioctl,
2109         .hw_params =    snd_trident_hw_params,
2110         .hw_free =      snd_trident_hw_free,
2111         .prepare =      snd_trident_foldback_prepare,
2112         .trigger =      snd_trident_trigger,
2113         .pointer =      snd_trident_playback_pointer,
2114 };
2115
2116 static struct snd_pcm_ops snd_trident_nx_foldback_ops = {
2117         .open =         snd_trident_foldback_open,
2118         .close =        snd_trident_foldback_close,
2119         .ioctl =        snd_trident_ioctl,
2120         .hw_params =    snd_trident_hw_params,
2121         .hw_free =      snd_trident_hw_free,
2122         .prepare =      snd_trident_foldback_prepare,
2123         .trigger =      snd_trident_trigger,
2124         .pointer =      snd_trident_playback_pointer,
2125         .page =         snd_pcm_sgbuf_ops_page,
2126 };
2127
2128 static struct snd_pcm_ops snd_trident_spdif_ops = {
2129         .open =         snd_trident_spdif_open,
2130         .close =        snd_trident_spdif_close,
2131         .ioctl =        snd_trident_ioctl,
2132         .hw_params =    snd_trident_spdif_hw_params,
2133         .hw_free =      snd_trident_hw_free,
2134         .prepare =      snd_trident_spdif_prepare,
2135         .trigger =      snd_trident_trigger,
2136         .pointer =      snd_trident_spdif_pointer,
2137 };
2138
2139 static struct snd_pcm_ops snd_trident_spdif_7018_ops = {
2140         .open =         snd_trident_spdif_open,
2141         .close =        snd_trident_spdif_close,
2142         .ioctl =        snd_trident_ioctl,
2143         .hw_params =    snd_trident_spdif_hw_params,
2144         .hw_free =      snd_trident_hw_free,
2145         .prepare =      snd_trident_spdif_prepare,
2146         .trigger =      snd_trident_trigger,
2147         .pointer =      snd_trident_playback_pointer,
2148 };
2149
2150 /*---------------------------------------------------------------------------
2151    snd_trident_pcm
2152   
2153    Description: This routine registers the 4DWave device for PCM support.
2154                 
2155    Paramters:   trident - pointer to target device class for 4DWave.
2156
2157    Returns:     None
2158   
2159   ---------------------------------------------------------------------------*/
2160
2161 int __devinit snd_trident_pcm(struct snd_trident * trident,
2162                               int device, struct snd_pcm ** rpcm)
2163 {
2164         struct snd_pcm *pcm;
2165         int err;
2166
2167         if (rpcm)
2168                 *rpcm = NULL;
2169         if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, trident->ChanPCM, 1, &pcm)) < 0)
2170                 return err;
2171
2172         pcm->private_data = trident;
2173
2174         if (trident->tlb.entries) {
2175                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_nx_playback_ops);
2176         } else {
2177                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_playback_ops);
2178         }
2179         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
2180                         trident->device != TRIDENT_DEVICE_ID_SI7018 ?
2181                         &snd_trident_capture_ops :
2182                         &snd_trident_si7018_capture_ops);
2183
2184         pcm->info_flags = 0;
2185         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
2186         strcpy(pcm->name, "Trident 4DWave");
2187         trident->pcm = pcm;
2188
2189         if (trident->tlb.entries) {
2190                 struct snd_pcm_substream *substream;
2191                 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
2192                         snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
2193                                                       snd_dma_pci_data(trident->pci),
2194                                                       64*1024, 128*1024);
2195                 snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
2196                                               SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
2197                                               64*1024, 128*1024);
2198         } else {
2199                 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
2200                                                       snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2201         }
2202
2203         if (rpcm)
2204                 *rpcm = pcm;
2205         return 0;
2206 }
2207
2208 /*---------------------------------------------------------------------------
2209    snd_trident_foldback_pcm
2210   
2211    Description: This routine registers the 4DWave device for foldback PCM support.
2212                 
2213    Paramters:   trident - pointer to target device class for 4DWave.
2214
2215    Returns:     None
2216   
2217   ---------------------------------------------------------------------------*/
2218
2219 int __devinit snd_trident_foldback_pcm(struct snd_trident * trident,
2220                                        int device, struct snd_pcm ** rpcm)
2221 {
2222         struct snd_pcm *foldback;
2223         int err;
2224         int num_chan = 3;
2225         struct snd_pcm_substream *substream;
2226
2227         if (rpcm)
2228                 *rpcm = NULL;
2229         if (trident->device == TRIDENT_DEVICE_ID_NX)
2230                 num_chan = 4;
2231         if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, 0, num_chan, &foldback)) < 0)
2232                 return err;
2233
2234         foldback->private_data = trident;
2235         if (trident->tlb.entries)
2236                 snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_nx_foldback_ops);
2237         else
2238                 snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_foldback_ops);
2239         foldback->info_flags = 0;
2240         strcpy(foldback->name, "Trident 4DWave");
2241         substream = foldback->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
2242         strcpy(substream->name, "Front Mixer");
2243         substream = substream->next;
2244         strcpy(substream->name, "Reverb Mixer");
2245         substream = substream->next;
2246         strcpy(substream->name, "Chorus Mixer");
2247         if (num_chan == 4) {
2248                 substream = substream->next;
2249                 strcpy(substream->name, "Second AC'97 ADC");
2250         }
2251         trident->foldback = foldback;
2252
2253         if (trident->tlb.entries)
2254                 snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV_SG,
2255                                                       snd_dma_pci_data(trident->pci), 0, 128*1024);
2256         else
2257                 snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV,
2258                                                       snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2259
2260         if (rpcm)
2261                 *rpcm = foldback;
2262         return 0;
2263 }
2264
2265 /*---------------------------------------------------------------------------
2266    snd_trident_spdif
2267   
2268    Description: This routine registers the 4DWave-NX device for SPDIF support.
2269                 
2270    Paramters:   trident - pointer to target device class for 4DWave-NX.
2271
2272    Returns:     None
2273   
2274   ---------------------------------------------------------------------------*/
2275
2276 int __devinit snd_trident_spdif_pcm(struct snd_trident * trident,
2277                                     int device, struct snd_pcm ** rpcm)
2278 {
2279         struct snd_pcm *spdif;
2280         int err;
2281
2282         if (rpcm)
2283                 *rpcm = NULL;
2284         if ((err = snd_pcm_new(trident->card, "trident_dx_nx IEC958", device, 1, 0, &spdif)) < 0)
2285                 return err;
2286
2287         spdif->private_data = trident;
2288         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2289                 snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_ops);
2290         } else {
2291                 snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_7018_ops);
2292         }
2293         spdif->info_flags = 0;
2294         strcpy(spdif->name, "Trident 4DWave IEC958");
2295         trident->spdif = spdif;
2296
2297         snd_pcm_lib_preallocate_pages_for_all(spdif, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
2298
2299         if (rpcm)
2300                 *rpcm = spdif;
2301         return 0;
2302 }
2303
2304 /*
2305  *  Mixer part
2306  */
2307
2308
2309 /*---------------------------------------------------------------------------
2310     snd_trident_spdif_control
2311
2312     Description: enable/disable S/PDIF out from ac97 mixer
2313   ---------------------------------------------------------------------------*/
2314
2315 static int snd_trident_spdif_control_info(struct snd_kcontrol *kcontrol,
2316                                           struct snd_ctl_elem_info *uinfo)
2317 {
2318         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2319         uinfo->count = 1;
2320         uinfo->value.integer.min = 0;
2321         uinfo->value.integer.max = 1;
2322         return 0;
2323 }
2324
2325 static int snd_trident_spdif_control_get(struct snd_kcontrol *kcontrol,
2326                                          struct snd_ctl_elem_value *ucontrol)
2327 {
2328         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2329         unsigned char val;
2330
2331         spin_lock_irq(&trident->reg_lock);
2332         val = trident->spdif_ctrl;
2333         ucontrol->value.integer.value[0] = val == kcontrol->private_value;
2334         spin_unlock_irq(&trident->reg_lock);
2335         return 0;
2336 }
2337
2338 static int snd_trident_spdif_control_put(struct snd_kcontrol *kcontrol,
2339                                          struct snd_ctl_elem_value *ucontrol)
2340 {
2341         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2342         unsigned char val;
2343         int change;
2344
2345         val = ucontrol->value.integer.value[0] ? (unsigned char) kcontrol->private_value : 0x00;
2346         spin_lock_irq(&trident->reg_lock);
2347         /* S/PDIF C Channel bits 0-31 : 48khz, SCMS disabled */
2348         change = trident->spdif_ctrl != val;
2349         trident->spdif_ctrl = val;
2350         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2351                 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0) {
2352                         outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2353                         outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
2354                 }
2355         } else {
2356                 if (trident->spdif == NULL) {
2357                         unsigned int temp;
2358                         outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2359                         temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & ~SPDIF_EN;
2360                         if (val)
2361                                 temp |= SPDIF_EN;
2362                         outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
2363                 }
2364         }
2365         spin_unlock_irq(&trident->reg_lock);
2366         return change;
2367 }
2368
2369 static struct snd_kcontrol_new snd_trident_spdif_control __devinitdata =
2370 {
2371         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2372         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2373         .info =         snd_trident_spdif_control_info,
2374         .get =          snd_trident_spdif_control_get,
2375         .put =          snd_trident_spdif_control_put,
2376         .private_value = 0x28,
2377 };
2378
2379 /*---------------------------------------------------------------------------
2380     snd_trident_spdif_default
2381
2382     Description: put/get the S/PDIF default settings
2383   ---------------------------------------------------------------------------*/
2384
2385 static int snd_trident_spdif_default_info(struct snd_kcontrol *kcontrol,
2386                                           struct snd_ctl_elem_info *uinfo)
2387 {
2388         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2389         uinfo->count = 1;
2390         return 0;
2391 }
2392
2393 static int snd_trident_spdif_default_get(struct snd_kcontrol *kcontrol,
2394                                          struct snd_ctl_elem_value *ucontrol)
2395 {
2396         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2397
2398         spin_lock_irq(&trident->reg_lock);
2399         ucontrol->value.iec958.status[0] = (trident->spdif_bits >> 0) & 0xff;
2400         ucontrol->value.iec958.status[1] = (trident->spdif_bits >> 8) & 0xff;
2401         ucontrol->value.iec958.status[2] = (trident->spdif_bits >> 16) & 0xff;
2402         ucontrol->value.iec958.status[3] = (trident->spdif_bits >> 24) & 0xff;
2403         spin_unlock_irq(&trident->reg_lock);
2404         return 0;
2405 }
2406
2407 static int snd_trident_spdif_default_put(struct snd_kcontrol *kcontrol,
2408                                          struct snd_ctl_elem_value *ucontrol)
2409 {
2410         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2411         unsigned int val;
2412         int change;
2413
2414         val = (ucontrol->value.iec958.status[0] << 0) |
2415               (ucontrol->value.iec958.status[1] << 8) |
2416               (ucontrol->value.iec958.status[2] << 16) |
2417               (ucontrol->value.iec958.status[3] << 24);
2418         spin_lock_irq(&trident->reg_lock);
2419         change = trident->spdif_bits != val;
2420         trident->spdif_bits = val;
2421         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2422                 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0)
2423                         outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2424         } else {
2425                 if (trident->spdif == NULL)
2426                         outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2427         }
2428         spin_unlock_irq(&trident->reg_lock);
2429         return change;
2430 }
2431
2432 static struct snd_kcontrol_new snd_trident_spdif_default __devinitdata =
2433 {
2434         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
2435         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2436         .info =         snd_trident_spdif_default_info,
2437         .get =          snd_trident_spdif_default_get,
2438         .put =          snd_trident_spdif_default_put
2439 };
2440
2441 /*---------------------------------------------------------------------------
2442     snd_trident_spdif_mask
2443
2444     Description: put/get the S/PDIF mask
2445   ---------------------------------------------------------------------------*/
2446
2447 static int snd_trident_spdif_mask_info(struct snd_kcontrol *kcontrol,
2448                                        struct snd_ctl_elem_info *uinfo)
2449 {
2450         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2451         uinfo->count = 1;
2452         return 0;
2453 }
2454
2455 static int snd_trident_spdif_mask_get(struct snd_kcontrol *kcontrol,
2456                                       struct snd_ctl_elem_value *ucontrol)
2457 {
2458         ucontrol->value.iec958.status[0] = 0xff;
2459         ucontrol->value.iec958.status[1] = 0xff;
2460         ucontrol->value.iec958.status[2] = 0xff;
2461         ucontrol->value.iec958.status[3] = 0xff;
2462         return 0;
2463 }
2464
2465 static struct snd_kcontrol_new snd_trident_spdif_mask __devinitdata =
2466 {
2467         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
2468         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
2469         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
2470         .info =         snd_trident_spdif_mask_info,
2471         .get =          snd_trident_spdif_mask_get,
2472 };
2473
2474 /*---------------------------------------------------------------------------
2475     snd_trident_spdif_stream
2476
2477     Description: put/get the S/PDIF stream settings
2478   ---------------------------------------------------------------------------*/
2479
2480 static int snd_trident_spdif_stream_info(struct snd_kcontrol *kcontrol,
2481                                          struct snd_ctl_elem_info *uinfo)
2482 {
2483         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2484         uinfo->count = 1;
2485         return 0;
2486 }
2487
2488 static int snd_trident_spdif_stream_get(struct snd_kcontrol *kcontrol,
2489                                         struct snd_ctl_elem_value *ucontrol)
2490 {
2491         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2492
2493         spin_lock_irq(&trident->reg_lock);
2494         ucontrol->value.iec958.status[0] = (trident->spdif_pcm_bits >> 0) & 0xff;
2495         ucontrol->value.iec958.status[1] = (trident->spdif_pcm_bits >> 8) & 0xff;
2496         ucontrol->value.iec958.status[2] = (trident->spdif_pcm_bits >> 16) & 0xff;
2497         ucontrol->value.iec958.status[3] = (trident->spdif_pcm_bits >> 24) & 0xff;
2498         spin_unlock_irq(&trident->reg_lock);
2499         return 0;
2500 }
2501
2502 static int snd_trident_spdif_stream_put(struct snd_kcontrol *kcontrol,
2503                                         struct snd_ctl_elem_value *ucontrol)
2504 {
2505         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2506         unsigned int val;
2507         int change;
2508
2509         val = (ucontrol->value.iec958.status[0] << 0) |
2510               (ucontrol->value.iec958.status[1] << 8) |
2511               (ucontrol->value.iec958.status[2] << 16) |
2512               (ucontrol->value.iec958.status[3] << 24);
2513         spin_lock_irq(&trident->reg_lock);
2514         change = trident->spdif_pcm_bits != val;
2515         trident->spdif_pcm_bits = val;
2516         if (trident->spdif != NULL) {
2517                 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2518                         outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
2519                 } else {
2520                         outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2521                 }
2522         }
2523         spin_unlock_irq(&trident->reg_lock);
2524         return change;
2525 }
2526
2527 static struct snd_kcontrol_new snd_trident_spdif_stream __devinitdata =
2528 {
2529         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2530         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
2531         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2532         .info =         snd_trident_spdif_stream_info,
2533         .get =          snd_trident_spdif_stream_get,
2534         .put =          snd_trident_spdif_stream_put
2535 };
2536
2537 /*---------------------------------------------------------------------------
2538     snd_trident_ac97_control
2539
2540     Description: enable/disable rear path for ac97
2541   ---------------------------------------------------------------------------*/
2542
2543 static int snd_trident_ac97_control_info(struct snd_kcontrol *kcontrol,
2544                                          struct snd_ctl_elem_info *uinfo)
2545 {
2546         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2547         uinfo->count = 1;
2548         uinfo->value.integer.min = 0;
2549         uinfo->value.integer.max = 1;
2550         return 0;
2551 }
2552
2553 static int snd_trident_ac97_control_get(struct snd_kcontrol *kcontrol,
2554                                         struct snd_ctl_elem_value *ucontrol)
2555 {
2556         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2557         unsigned char val;
2558
2559         spin_lock_irq(&trident->reg_lock);
2560         val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2561         ucontrol->value.integer.value[0] = (val & (1 << kcontrol->private_value)) ? 1 : 0;
2562         spin_unlock_irq(&trident->reg_lock);
2563         return 0;
2564 }
2565
2566 static int snd_trident_ac97_control_put(struct snd_kcontrol *kcontrol,
2567                                         struct snd_ctl_elem_value *ucontrol)
2568 {
2569         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2570         unsigned char val;
2571         int change = 0;
2572
2573         spin_lock_irq(&trident->reg_lock);
2574         val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2575         val &= ~(1 << kcontrol->private_value);
2576         if (ucontrol->value.integer.value[0])
2577                 val |= 1 << kcontrol->private_value;
2578         change = val != trident->ac97_ctrl;
2579         trident->ac97_ctrl = val;
2580         outl(trident->ac97_ctrl = val, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2581         spin_unlock_irq(&trident->reg_lock);
2582         return change;
2583 }
2584
2585 static struct snd_kcontrol_new snd_trident_ac97_rear_control __devinitdata =
2586 {
2587         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2588         .name =         "Rear Path",
2589         .info =         snd_trident_ac97_control_info,
2590         .get =          snd_trident_ac97_control_get,
2591         .put =          snd_trident_ac97_control_put,
2592         .private_value = 4,
2593 };
2594
2595 /*---------------------------------------------------------------------------
2596     snd_trident_vol_control
2597
2598     Description: wave & music volume control
2599   ---------------------------------------------------------------------------*/
2600
2601 static int snd_trident_vol_control_info(struct snd_kcontrol *kcontrol,
2602                                         struct snd_ctl_elem_info *uinfo)
2603 {
2604         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2605         uinfo->count = 2;
2606         uinfo->value.integer.min = 0;
2607         uinfo->value.integer.max = 255;
2608         return 0;
2609 }
2610
2611 static int snd_trident_vol_control_get(struct snd_kcontrol *kcontrol,
2612                                        struct snd_ctl_elem_value *ucontrol)
2613 {
2614         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2615         unsigned int val;
2616
2617         val = trident->musicvol_wavevol;
2618         ucontrol->value.integer.value[0] = 255 - ((val >> kcontrol->private_value) & 0xff);
2619         ucontrol->value.integer.value[1] = 255 - ((val >> (kcontrol->private_value + 8)) & 0xff);
2620         return 0;
2621 }
2622
2623 static int snd_trident_vol_control_put(struct snd_kcontrol *kcontrol,
2624                                        struct snd_ctl_elem_value *ucontrol)
2625 {
2626         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2627         unsigned int val;
2628         int change = 0;
2629
2630         spin_lock_irq(&trident->reg_lock);
2631         val = trident->musicvol_wavevol;
2632         val &= ~(0xffff << kcontrol->private_value);
2633         val |= ((255 - (ucontrol->value.integer.value[0] & 0xff)) |
2634                 ((255 - (ucontrol->value.integer.value[1] & 0xff)) << 8)) << kcontrol->private_value;
2635         change = val != trident->musicvol_wavevol;
2636         outl(trident->musicvol_wavevol = val, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
2637         spin_unlock_irq(&trident->reg_lock);
2638         return change;
2639 }
2640
2641 static struct snd_kcontrol_new snd_trident_vol_music_control __devinitdata =
2642 {
2643         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2644         .name =         "Music Playback Volume",
2645         .info =         snd_trident_vol_control_info,
2646         .get =          snd_trident_vol_control_get,
2647         .put =          snd_trident_vol_control_put,
2648         .private_value = 16,
2649 };
2650
2651 static struct snd_kcontrol_new snd_trident_vol_wave_control __devinitdata =
2652 {
2653         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2654         .name =         "Wave Playback Volume",
2655         .info =         snd_trident_vol_control_info,
2656         .get =          snd_trident_vol_control_get,
2657         .put =          snd_trident_vol_control_put,
2658         .private_value = 0,
2659 };
2660
2661 /*---------------------------------------------------------------------------
2662     snd_trident_pcm_vol_control
2663
2664     Description: PCM front volume control
2665   ---------------------------------------------------------------------------*/
2666
2667 static int snd_trident_pcm_vol_control_info(struct snd_kcontrol *kcontrol,
2668                                             struct snd_ctl_elem_info *uinfo)
2669 {
2670         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2671
2672         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2673         uinfo->count = 1;
2674         uinfo->value.integer.min = 0;
2675         uinfo->value.integer.max = 255;
2676         if (trident->device == TRIDENT_DEVICE_ID_SI7018)
2677                 uinfo->value.integer.max = 1023;
2678         return 0;
2679 }
2680
2681 static int snd_trident_pcm_vol_control_get(struct snd_kcontrol *kcontrol,
2682                                            struct snd_ctl_elem_value *ucontrol)
2683 {
2684         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2685         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2686
2687         if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2688                 ucontrol->value.integer.value[0] = 1023 - mix->vol;
2689         } else {
2690                 ucontrol->value.integer.value[0] = 255 - (mix->vol>>2);
2691         }
2692         return 0;
2693 }
2694
2695 static int snd_trident_pcm_vol_control_put(struct snd_kcontrol *kcontrol,
2696                                            struct snd_ctl_elem_value *ucontrol)
2697 {
2698         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2699         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2700         unsigned int val;
2701         int change = 0;
2702
2703         if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2704                 val = 1023 - (ucontrol->value.integer.value[0] & 1023);
2705         } else {
2706                 val = (255 - (ucontrol->value.integer.value[0] & 255)) << 2;
2707         }
2708         spin_lock_irq(&trident->reg_lock);
2709         change = val != mix->vol;
2710         mix->vol = val;
2711         if (mix->voice != NULL)
2712                 snd_trident_write_vol_reg(trident, mix->voice, val);
2713         spin_unlock_irq(&trident->reg_lock);
2714         return change;
2715 }
2716
2717 static struct snd_kcontrol_new snd_trident_pcm_vol_control __devinitdata =
2718 {
2719         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2720         .name =         "PCM Front Playback Volume",
2721         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2722         .count =        32,
2723         .info =         snd_trident_pcm_vol_control_info,
2724         .get =          snd_trident_pcm_vol_control_get,
2725         .put =          snd_trident_pcm_vol_control_put,
2726 };
2727
2728 /*---------------------------------------------------------------------------
2729     snd_trident_pcm_pan_control
2730
2731     Description: PCM front pan control
2732   ---------------------------------------------------------------------------*/
2733
2734 static int snd_trident_pcm_pan_control_info(struct snd_kcontrol *kcontrol,
2735                                             struct snd_ctl_elem_info *uinfo)
2736 {
2737         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2738         uinfo->count = 1;
2739         uinfo->value.integer.min = 0;
2740         uinfo->value.integer.max = 127;
2741         return 0;
2742 }
2743
2744 static int snd_trident_pcm_pan_control_get(struct snd_kcontrol *kcontrol,
2745                                            struct snd_ctl_elem_value *ucontrol)
2746 {
2747         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2748         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2749
2750         ucontrol->value.integer.value[0] = mix->pan;
2751         if (ucontrol->value.integer.value[0] & 0x40) {
2752                 ucontrol->value.integer.value[0] = (0x3f - (ucontrol->value.integer.value[0] & 0x3f));
2753         } else {
2754                 ucontrol->value.integer.value[0] |= 0x40;
2755         }
2756         return 0;
2757 }
2758
2759 static int snd_trident_pcm_pan_control_put(struct snd_kcontrol *kcontrol,
2760                                            struct snd_ctl_elem_value *ucontrol)
2761 {
2762         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2763         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2764         unsigned char val;
2765         int change = 0;
2766
2767         if (ucontrol->value.integer.value[0] & 0x40)
2768                 val = ucontrol->value.integer.value[0] & 0x3f;
2769         else
2770                 val = (0x3f - (ucontrol->value.integer.value[0] & 0x3f)) | 0x40;
2771         spin_lock_irq(&trident->reg_lock);
2772         change = val != mix->pan;
2773         mix->pan = val;
2774         if (mix->voice != NULL)
2775                 snd_trident_write_pan_reg(trident, mix->voice, val);
2776         spin_unlock_irq(&trident->reg_lock);
2777         return change;
2778 }
2779
2780 static struct snd_kcontrol_new snd_trident_pcm_pan_control __devinitdata =
2781 {
2782         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2783         .name =         "PCM Pan Playback Control",
2784         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2785         .count =        32,
2786         .info =         snd_trident_pcm_pan_control_info,
2787         .get =          snd_trident_pcm_pan_control_get,
2788         .put =          snd_trident_pcm_pan_control_put,
2789 };
2790
2791 /*---------------------------------------------------------------------------
2792     snd_trident_pcm_rvol_control
2793
2794     Description: PCM reverb volume control
2795   ---------------------------------------------------------------------------*/
2796
2797 static int snd_trident_pcm_rvol_control_info(struct snd_kcontrol *kcontrol,
2798                                              struct snd_ctl_elem_info *uinfo)
2799 {
2800         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2801         uinfo->count = 1;
2802         uinfo->value.integer.min = 0;
2803         uinfo->value.integer.max = 127;
2804         return 0;
2805 }
2806
2807 static int snd_trident_pcm_rvol_control_get(struct snd_kcontrol *kcontrol,
2808                                             struct snd_ctl_elem_value *ucontrol)
2809 {
2810         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2811         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2812
2813         ucontrol->value.integer.value[0] = 127 - mix->rvol;
2814         return 0;
2815 }
2816
2817 static int snd_trident_pcm_rvol_control_put(struct snd_kcontrol *kcontrol,
2818                                             struct snd_ctl_elem_value *ucontrol)
2819 {
2820         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2821         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2822         unsigned short val;
2823         int change = 0;
2824
2825         val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2826         spin_lock_irq(&trident->reg_lock);
2827         change = val != mix->rvol;
2828         mix->rvol = val;
2829         if (mix->voice != NULL)
2830                 snd_trident_write_rvol_reg(trident, mix->voice, val);
2831         spin_unlock_irq(&trident->reg_lock);
2832         return change;
2833 }
2834
2835 static struct snd_kcontrol_new snd_trident_pcm_rvol_control __devinitdata =
2836 {
2837         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2838         .name =         "PCM Reverb Playback Volume",
2839         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2840         .count =        32,
2841         .info =         snd_trident_pcm_rvol_control_info,
2842         .get =          snd_trident_pcm_rvol_control_get,
2843         .put =          snd_trident_pcm_rvol_control_put,
2844 };
2845
2846 /*---------------------------------------------------------------------------
2847     snd_trident_pcm_cvol_control
2848
2849     Description: PCM chorus volume control
2850   ---------------------------------------------------------------------------*/
2851
2852 static int snd_trident_pcm_cvol_control_info(struct snd_kcontrol *kcontrol,
2853                                              struct snd_ctl_elem_info *uinfo)
2854 {
2855         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2856         uinfo->count = 1;
2857         uinfo->value.integer.min = 0;
2858         uinfo->value.integer.max = 127;
2859         return 0;
2860 }
2861
2862 static int snd_trident_pcm_cvol_control_get(struct snd_kcontrol *kcontrol,
2863                                             struct snd_ctl_elem_value *ucontrol)
2864 {
2865         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2866         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2867
2868         ucontrol->value.integer.value[0] = 127 - mix->cvol;
2869         return 0;
2870 }
2871
2872 static int snd_trident_pcm_cvol_control_put(struct snd_kcontrol *kcontrol,
2873                                             struct snd_ctl_elem_value *ucontrol)
2874 {
2875         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2876         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2877         unsigned short val;
2878         int change = 0;
2879
2880         val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2881         spin_lock_irq(&trident->reg_lock);
2882         change = val != mix->cvol;
2883         mix->cvol = val;
2884         if (mix->voice != NULL)
2885                 snd_trident_write_cvol_reg(trident, mix->voice, val);
2886         spin_unlock_irq(&trident->reg_lock);
2887         return change;
2888 }
2889
2890 static struct snd_kcontrol_new snd_trident_pcm_cvol_control __devinitdata =
2891 {
2892         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2893         .name =         "PCM Chorus Playback Volume",
2894         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2895         .count =        32,
2896         .info =         snd_trident_pcm_cvol_control_info,
2897         .get =          snd_trident_pcm_cvol_control_get,
2898         .put =          snd_trident_pcm_cvol_control_put,
2899 };
2900
2901 static void snd_trident_notify_pcm_change1(struct snd_card *card,
2902                                            struct snd_kcontrol *kctl,
2903                                            int num, int activate)
2904 {
2905         struct snd_ctl_elem_id id;
2906
2907         if (! kctl)
2908                 return;
2909         if (activate)
2910                 kctl->vd[num].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2911         else
2912                 kctl->vd[num].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2913         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
2914                        SNDRV_CTL_EVENT_MASK_INFO,
2915                        snd_ctl_build_ioff(&id, kctl, num));
2916 }
2917
2918 static void snd_trident_notify_pcm_change(struct snd_trident *trident,
2919                                           struct snd_trident_pcm_mixer *tmix,
2920                                           int num, int activate)
2921 {
2922         snd_trident_notify_pcm_change1(trident->card, trident->ctl_vol, num, activate);
2923         snd_trident_notify_pcm_change1(trident->card, trident->ctl_pan, num, activate);
2924         snd_trident_notify_pcm_change1(trident->card, trident->ctl_rvol, num, activate);
2925         snd_trident_notify_pcm_change1(trident->card, trident->ctl_cvol, num, activate);
2926 }
2927
2928 static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
2929                                        struct snd_trident_voice *voice,
2930                                        struct snd_pcm_substream *substream)
2931 {
2932         struct snd_trident_pcm_mixer *tmix;
2933
2934         snd_assert(trident != NULL && voice != NULL && substream != NULL, return -EINVAL);
2935         tmix = &trident->pcm_mixer[substream->number];
2936         tmix->voice = voice;
2937         tmix->vol = T4D_DEFAULT_PCM_VOL;
2938         tmix->pan = T4D_DEFAULT_PCM_PAN;
2939         tmix->rvol = T4D_DEFAULT_PCM_RVOL;
2940         tmix->cvol = T4D_DEFAULT_PCM_CVOL;
2941         snd_trident_notify_pcm_change(trident, tmix, substream->number, 1);
2942         return 0;
2943 }
2944
2945 static int snd_trident_pcm_mixer_free(struct snd_trident *trident, struct snd_trident_voice *voice, struct snd_pcm_substream *substream)
2946 {
2947         struct snd_trident_pcm_mixer *tmix;
2948
2949         snd_assert(trident != NULL && substream != NULL, return -EINVAL);
2950         tmix = &trident->pcm_mixer[substream->number];
2951         tmix->voice = NULL;
2952         snd_trident_notify_pcm_change(trident, tmix, substream->number, 0);
2953         return 0;
2954 }
2955
2956 /*---------------------------------------------------------------------------
2957    snd_trident_mixer
2958   
2959    Description: This routine registers the 4DWave device for mixer support.
2960                 
2961    Paramters:   trident - pointer to target device class for 4DWave.
2962
2963    Returns:     None
2964   
2965   ---------------------------------------------------------------------------*/
2966
2967 static int __devinit snd_trident_mixer(struct snd_trident * trident, int pcm_spdif_device)
2968 {
2969         struct snd_ac97_template _ac97;
2970         struct snd_card *card = trident->card;
2971         struct snd_kcontrol *kctl;
2972         struct snd_ctl_elem_value *uctl;
2973         int idx, err, retries = 2;
2974         static struct snd_ac97_bus_ops ops = {
2975                 .write = snd_trident_codec_write,
2976                 .read = snd_trident_codec_read,
2977         };
2978
2979         uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
2980         if (!uctl)
2981                 return -ENOMEM;
2982
2983         if ((err = snd_ac97_bus(trident->card, 0, &ops, NULL, &trident->ac97_bus)) < 0)
2984                 goto __out;
2985
2986         memset(&_ac97, 0, sizeof(_ac97));
2987         _ac97.private_data = trident;
2988         trident->ac97_detect = 1;
2989
2990       __again:
2991         if ((err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97)) < 0) {
2992                 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2993                         if ((err = snd_trident_sis_reset(trident)) < 0)
2994                                 goto __out;
2995                         if (retries-- > 0)
2996                                 goto __again;
2997                         err = -EIO;
2998                 }
2999                 goto __out;
3000         }
3001         
3002         /* secondary codec? */
3003         if (trident->device == TRIDENT_DEVICE_ID_SI7018 &&
3004             (inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0) {
3005                 _ac97.num = 1;
3006                 err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97_sec);
3007                 if (err < 0)
3008                         snd_printk(KERN_ERR "SI7018: the secondary codec - invalid access\n");
3009 #if 0   // only for my testing purpose --jk
3010                 {
3011                         struct snd_ac97 *mc97;
3012                         err = snd_ac97_modem(trident->card, &_ac97, &mc97);
3013                         if (err < 0)
3014                                 snd_printk(KERN_ERR "snd_ac97_modem returned error %i\n", err);
3015                 }
3016 #endif
3017         }
3018         
3019         trident->ac97_detect = 0;
3020
3021         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
3022                 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_wave_control, trident))) < 0)
3023                         goto __out;
3024                 kctl->put(kctl, uctl);
3025                 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_music_control, trident))) < 0)
3026                         goto __out;
3027                 kctl->put(kctl, uctl);
3028                 outl(trident->musicvol_wavevol = 0x00000000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3029         } else {
3030                 outl(trident->musicvol_wavevol = 0xffff0000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3031         }
3032
3033         for (idx = 0; idx < 32; idx++) {
3034                 struct snd_trident_pcm_mixer *tmix;
3035                 
3036                 tmix = &trident->pcm_mixer[idx];
3037                 tmix->voice = NULL;
3038         }
3039         if ((trident->ctl_vol = snd_ctl_new1(&snd_trident_pcm_vol_control, trident)) == NULL)
3040                 goto __nomem;
3041         if ((err = snd_ctl_add(card, trident->ctl_vol)))
3042                 goto __out;
3043                 
3044         if ((trident->ctl_pan = snd_ctl_new1(&snd_trident_pcm_pan_control, trident)) == NULL)
3045                 goto __nomem;
3046         if ((err = snd_ctl_add(card, trident->ctl_pan)))
3047                 goto __out;
3048
3049         if ((trident->ctl_rvol = snd_ctl_new1(&snd_trident_pcm_rvol_control, trident)) == NULL)
3050                 goto __nomem;
3051         if ((err = snd_ctl_add(card, trident->ctl_rvol)))
3052                 goto __out;
3053
3054         if ((trident->ctl_cvol = snd_ctl_new1(&snd_trident_pcm_cvol_control, trident)) == NULL)
3055                 goto __nomem;
3056         if ((err = snd_ctl_add(card, trident->ctl_cvol)))
3057                 goto __out;
3058
3059         if (trident->device == TRIDENT_DEVICE_ID_NX) {
3060                 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_ac97_rear_control, trident))) < 0)
3061                         goto __out;
3062                 kctl->put(kctl, uctl);
3063         }
3064         if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018) {
3065
3066                 kctl = snd_ctl_new1(&snd_trident_spdif_control, trident);
3067                 if (kctl == NULL) {
3068                         err = -ENOMEM;
3069                         goto __out;
3070                 }
3071                 if (trident->ac97->ext_id & AC97_EI_SPDIF)
3072                         kctl->id.index++;
3073                 if (trident->ac97_sec && (trident->ac97_sec->ext_id & AC97_EI_SPDIF))
3074                         kctl->id.index++;
3075                 idx = kctl->id.index;
3076                 if ((err = snd_ctl_add(card, kctl)) < 0)
3077                         goto __out;
3078                 kctl->put(kctl, uctl);
3079
3080                 kctl = snd_ctl_new1(&snd_trident_spdif_default, trident);
3081                 if (kctl == NULL) {
3082                         err = -ENOMEM;
3083                         goto __out;
3084                 }
3085                 kctl->id.index = idx;
3086                 kctl->id.device = pcm_spdif_device;
3087                 if ((err = snd_ctl_add(card, kctl)) < 0)
3088                         goto __out;
3089
3090                 kctl = snd_ctl_new1(&snd_trident_spdif_mask, trident);
3091                 if (kctl == NULL) {
3092                         err = -ENOMEM;
3093                         goto __out;
3094                 }
3095                 kctl->id.index = idx;
3096                 kctl->id.device = pcm_spdif_device;
3097                 if ((err = snd_ctl_add(card, kctl)) < 0)
3098                         goto __out;
3099
3100                 kctl = snd_ctl_new1(&snd_trident_spdif_stream, trident);
3101                 if (kctl == NULL) {
3102                         err = -ENOMEM;
3103                         goto __out;
3104                 }
3105                 kctl->id.index = idx;
3106                 kctl->id.device = pcm_spdif_device;
3107                 if ((err = snd_ctl_add(card, kctl)) < 0)
3108                         goto __out;
3109                 trident->spdif_pcm_ctl = kctl;
3110         }
3111
3112         err = 0;
3113         goto __out;
3114
3115  __nomem:
3116         err = -ENOMEM;
3117
3118  __out:
3119         kfree(uctl);
3120
3121         return err;
3122 }
3123
3124 /*
3125  * gameport interface
3126  */
3127
3128 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
3129
3130 static unsigned char snd_trident_gameport_read(struct gameport *gameport)
3131 {
3132         struct snd_trident *chip = gameport_get_port_data(gameport);
3133
3134         snd_assert(chip, return 0);
3135         return inb(TRID_REG(chip, GAMEPORT_LEGACY));
3136 }
3137
3138 static void snd_trident_gameport_trigger(struct gameport *gameport)
3139 {
3140         struct snd_trident *chip = gameport_get_port_data(gameport);
3141
3142         snd_assert(chip, return);
3143         outb(0xff, TRID_REG(chip, GAMEPORT_LEGACY));
3144 }
3145
3146 static int snd_trident_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
3147 {
3148         struct snd_trident *chip = gameport_get_port_data(gameport);
3149         int i;
3150
3151         snd_assert(chip, return 0);
3152
3153         *buttons = (~inb(TRID_REG(chip, GAMEPORT_LEGACY)) >> 4) & 0xf;
3154
3155         for (i = 0; i < 4; i++) {
3156                 axes[i] = inw(TRID_REG(chip, GAMEPORT_AXES + i * 2));
3157                 if (axes[i] == 0xffff) axes[i] = -1;
3158         }
3159         
3160         return 0;
3161 }
3162
3163 static int snd_trident_gameport_open(struct gameport *gameport, int mode)
3164 {
3165         struct snd_trident *chip = gameport_get_port_data(gameport);
3166
3167         snd_assert(chip, return 0);
3168
3169         switch (mode) {
3170                 case GAMEPORT_MODE_COOKED:
3171                         outb(GAMEPORT_MODE_ADC, TRID_REG(chip, GAMEPORT_GCR));
3172                         msleep(20);
3173                         return 0;
3174                 case GAMEPORT_MODE_RAW:
3175                         outb(0, TRID_REG(chip, GAMEPORT_GCR));
3176                         return 0;
3177                 default:
3178                         return -1;
3179         }
3180 }
3181
3182 int __devinit snd_trident_create_gameport(struct snd_trident *chip)
3183 {
3184         struct gameport *gp;
3185
3186         chip->gameport = gp = gameport_allocate_port();
3187         if (!gp) {
3188                 printk(KERN_ERR "trident: cannot allocate memory for gameport\n");
3189                 return -ENOMEM;
3190         }
3191
3192         gameport_set_name(gp, "Trident 4DWave");
3193         gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
3194         gameport_set_dev_parent(gp, &chip->pci->dev);
3195
3196         gameport_set_port_data(gp, chip);
3197         gp->fuzz = 64;
3198         gp->read = snd_trident_gameport_read;
3199         gp->trigger = snd_trident_gameport_trigger;
3200         gp->cooked_read = snd_trident_gameport_cooked_read;
3201         gp->open = snd_trident_gameport_open;
3202
3203         gameport_register_port(gp);
3204
3205         return 0;
3206 }
3207
3208 static inline void snd_trident_free_gameport(struct snd_trident *chip)
3209 {
3210         if (chip->gameport) {
3211                 gameport_unregister_port(chip->gameport);
3212                 chip->gameport = NULL;
3213         }
3214 }
3215 #else
3216 int __devinit snd_trident_create_gameport(struct snd_trident *chip) { return -ENOSYS; }
3217 static inline void snd_trident_free_gameport(struct snd_trident *chip) { }
3218 #endif /* CONFIG_GAMEPORT */
3219
3220 /*
3221  * delay for 1 tick
3222  */
3223 static inline void do_delay(struct snd_trident *chip)
3224 {
3225         schedule_timeout_uninterruptible(1);
3226 }
3227
3228 /*
3229  *  SiS reset routine
3230  */
3231
3232 static int snd_trident_sis_reset(struct snd_trident *trident)
3233 {
3234         unsigned long end_time;
3235         unsigned int i;
3236         int r;
3237
3238         r = trident->in_suspend ? 0 : 2;        /* count of retries */
3239       __si7018_retry:
3240         pci_write_config_byte(trident->pci, 0x46, 0x04);        /* SOFTWARE RESET */
3241         udelay(100);
3242         pci_write_config_byte(trident->pci, 0x46, 0x00);
3243         udelay(100);
3244         /* disable AC97 GPIO interrupt */
3245         outb(0x00, TRID_REG(trident, SI_AC97_GPIO));
3246         /* initialize serial interface, force cold reset */
3247         i = PCMOUT|SURROUT|CENTEROUT|LFEOUT|SECONDARY_ID|COLD_RESET;
3248         outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3249         udelay(1000);
3250         /* remove cold reset */
3251         i &= ~COLD_RESET;
3252         outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3253         udelay(2000);
3254         /* wait, until the codec is ready */
3255         end_time = (jiffies + (HZ * 3) / 4) + 1;
3256         do {
3257                 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0)
3258                         goto __si7018_ok;
3259                 do_delay(trident);
3260         } while (time_after_eq(end_time, jiffies));
3261         snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)));
3262         if (r-- > 0) {
3263                 end_time = jiffies + HZ;
3264                 do {
3265                         do_delay(trident);
3266                 } while (time_after_eq(end_time, jiffies));
3267                 goto __si7018_retry;
3268         }
3269       __si7018_ok:
3270         /* wait for the second codec */
3271         do {
3272                 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_SECONDARY_READY) != 0)
3273                         break;
3274                 do_delay(trident);
3275         } while (time_after_eq(end_time, jiffies));
3276         /* enable 64 channel mode */
3277         outl(BANK_B_EN, TRID_REG(trident, T4D_LFO_GC_CIR));
3278         return 0;
3279 }
3280
3281 /*  
3282  *  /proc interface
3283  */
3284
3285 static void snd_trident_proc_read(struct snd_info_entry *entry, 
3286                                   struct snd_info_buffer *buffer)
3287 {
3288         struct snd_trident *trident = entry->private_data;
3289         char *s;
3290
3291         switch (trident->device) {
3292         case TRIDENT_DEVICE_ID_SI7018:
3293                 s = "SiS 7018 Audio";
3294                 break;
3295         case TRIDENT_DEVICE_ID_DX:
3296                 s = "Trident 4DWave PCI DX";
3297                 break;
3298         case TRIDENT_DEVICE_ID_NX:
3299                 s = "Trident 4DWave PCI NX";
3300                 break;
3301         default:
3302                 s = "???";
3303         }
3304         snd_iprintf(buffer, "%s\n\n", s);
3305         snd_iprintf(buffer, "Spurious IRQs    : %d\n", trident->spurious_irq_count);
3306         snd_iprintf(buffer, "Spurious IRQ dlta: %d\n", trident->spurious_irq_max_delta);
3307         if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018)
3308                 snd_iprintf(buffer, "IEC958 Mixer Out : %s\n", trident->spdif_ctrl == 0x28 ? "on" : "off");
3309         if (trident->device == TRIDENT_DEVICE_ID_NX) {
3310                 snd_iprintf(buffer, "Rear Speakers    : %s\n", trident->ac97_ctrl & 0x00000010 ? "on" : "off");
3311                 if (trident->tlb.entries) {
3312                         snd_iprintf(buffer,"\nVirtual Memory\n");
3313                         snd_iprintf(buffer, "Memory Maximum : %d\n", trident->tlb.memhdr->size);
3314                         snd_iprintf(buffer, "Memory Used    : %d\n", trident->tlb.memhdr->used);
3315                         snd_iprintf(buffer, "Memory Free    : %d\n", snd_util_mem_avail(trident->tlb.memhdr));
3316                 }
3317         }
3318 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
3319         snd_iprintf(buffer,"\nWavetable Synth\n");
3320         snd_iprintf(buffer, "Memory Maximum : %d\n", trident->synth.max_size);
3321         snd_iprintf(buffer, "Memory Used    : %d\n", trident->synth.current_size);
3322         snd_iprintf(buffer, "Memory Free    : %d\n", (trident->synth.max_size-trident->synth.current_size));
3323 #endif
3324 }
3325
3326 static void __devinit snd_trident_proc_init(struct snd_trident * trident)
3327 {
3328         struct snd_info_entry *entry;
3329         const char *s = "trident";
3330         
3331         if (trident->device == TRIDENT_DEVICE_ID_SI7018)
3332                 s = "sis7018";
3333         if (! snd_card_proc_new(trident->card, s, &entry))
3334                 snd_info_set_text_ops(entry, trident, 1024, snd_trident_proc_read);
3335 }
3336
3337 static int snd_trident_dev_free(struct snd_device *device)
3338 {
3339         struct snd_trident *trident = device->device_data;
3340         return snd_trident_free(trident);
3341 }
3342
3343 /*---------------------------------------------------------------------------
3344    snd_trident_tlb_alloc
3345   
3346    Description: Allocate and set up the TLB page table on 4D NX.
3347                 Each entry has 4 bytes (physical PCI address).
3348                 
3349    Paramters:   trident - pointer to target device class for 4DWave.
3350
3351    Returns:     0 or negative error code
3352   
3353   ---------------------------------------------------------------------------*/
3354
3355 static int __devinit snd_trident_tlb_alloc(struct snd_trident *trident)
3356 {
3357         int i;
3358
3359         /* TLB array must be aligned to 16kB !!! so we allocate
3360            32kB region and correct offset when necessary */
3361
3362         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3363                                 2 * SNDRV_TRIDENT_MAX_PAGES * 4, &trident->tlb.buffer) < 0) {
3364                 snd_printk(KERN_ERR "trident: unable to allocate TLB buffer\n");
3365                 return -ENOMEM;
3366         }
3367         trident->tlb.entries = (unsigned int*)(((unsigned long)trident->tlb.buffer.area + SNDRV_TRIDENT_MAX_PAGES * 4 - 1) & ~(SNDRV_TRIDENT_MAX_PAGES * 4 - 1));
3368         trident->tlb.entries_dmaaddr = (trident->tlb.buffer.addr + SNDRV_TRIDENT_MAX_PAGES * 4 - 1) & ~(SNDRV_TRIDENT_MAX_PAGES * 4 - 1);
3369         /* allocate shadow TLB page table (virtual addresses) */
3370         trident->tlb.shadow_entries = vmalloc(SNDRV_TRIDENT_MAX_PAGES*sizeof(unsigned long));
3371         if (trident->tlb.shadow_entries == NULL) {
3372                 snd_printk(KERN_ERR "trident: unable to allocate shadow TLB entries\n");
3373                 return -ENOMEM;
3374         }
3375         /* allocate and setup silent page and initialise TLB entries */
3376         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3377                                 SNDRV_TRIDENT_PAGE_SIZE, &trident->tlb.silent_page) < 0) {
3378                 snd_printk(KERN_ERR "trident: unable to allocate silent page\n");
3379                 return -ENOMEM;
3380         }
3381         memset(trident->tlb.silent_page.area, 0, SNDRV_TRIDENT_PAGE_SIZE);
3382         for (i = 0; i < SNDRV_TRIDENT_MAX_PAGES; i++) {
3383                 trident->tlb.entries[i] = cpu_to_le32(trident->tlb.silent_page.addr & ~(SNDRV_TRIDENT_PAGE_SIZE-1));
3384                 trident->tlb.shadow_entries[i] = (unsigned long)trident->tlb.silent_page.area;
3385         }
3386
3387         /* use emu memory block manager code to manage tlb page allocation */
3388         trident->tlb.memhdr = snd_util_memhdr_new(SNDRV_TRIDENT_PAGE_SIZE * SNDRV_TRIDENT_MAX_PAGES);
3389         if (trident->tlb.memhdr == NULL)
3390                 return -ENOMEM;
3391
3392         trident->tlb.memhdr->block_extra_size = sizeof(struct snd_trident_memblk_arg);
3393         return 0;
3394 }
3395
3396 /*
3397  * initialize 4D DX chip
3398  */
3399
3400 static void snd_trident_stop_all_voices(struct snd_trident *trident)
3401 {
3402         outl(0xffffffff, TRID_REG(trident, T4D_STOP_A));
3403         outl(0xffffffff, TRID_REG(trident, T4D_STOP_B));
3404         outl(0, TRID_REG(trident, T4D_AINTEN_A));
3405         outl(0, TRID_REG(trident, T4D_AINTEN_B));
3406 }
3407
3408 static int snd_trident_4d_dx_init(struct snd_trident *trident)
3409 {
3410         struct pci_dev *pci = trident->pci;
3411         unsigned long end_time;
3412
3413         /* reset the legacy configuration and whole audio/wavetable block */
3414         pci_write_config_dword(pci, 0x40, 0);   /* DDMA */
3415         pci_write_config_byte(pci, 0x44, 0);    /* ports */
3416         pci_write_config_byte(pci, 0x45, 0);    /* Legacy DMA */
3417         pci_write_config_byte(pci, 0x46, 4); /* reset */
3418         udelay(100);
3419         pci_write_config_byte(pci, 0x46, 0); /* release reset */
3420         udelay(100);
3421         
3422         /* warm reset of the AC'97 codec */
3423         outl(0x00000001, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3424         udelay(100);
3425         outl(0x00000000, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3426         /* DAC on, disable SB IRQ and try to force ADC valid signal */
3427         trident->ac97_ctrl = 0x0000004a;
3428         outl(trident->ac97_ctrl, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3429         /* wait, until the codec is ready */
3430         end_time = (jiffies + (HZ * 3) / 4) + 1;
3431         do {
3432                 if ((inl(TRID_REG(trident, DX_ACR2_AC97_COM_STAT)) & 0x0010) != 0)
3433                         goto __dx_ok;
3434                 do_delay(trident);
3435         } while (time_after_eq(end_time, jiffies));
3436         snd_printk(KERN_ERR "AC'97 codec ready error\n");
3437         return -EIO;
3438
3439  __dx_ok:
3440         snd_trident_stop_all_voices(trident);
3441
3442         return 0;
3443 }
3444
3445 /*
3446  * initialize 4D NX chip
3447  */
3448 static int snd_trident_4d_nx_init(struct snd_trident *trident)
3449 {
3450         struct pci_dev *pci = trident->pci;
3451         unsigned long end_time;
3452
3453         /* reset the legacy configuration and whole audio/wavetable block */
3454         pci_write_config_dword(pci, 0x40, 0);   /* DDMA */
3455         pci_write_config_byte(pci, 0x44, 0);    /* ports */
3456         pci_write_config_byte(pci, 0x45, 0);    /* Legacy DMA */
3457
3458         pci_write_config_byte(pci, 0x46, 1); /* reset */
3459         udelay(100);
3460         pci_write_config_byte(pci, 0x46, 0); /* release reset */
3461         udelay(100);
3462
3463         /* warm reset of the AC'97 codec */
3464         outl(0x00000001, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3465         udelay(100);
3466         outl(0x00000000, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3467         /* wait, until the codec is ready */
3468         end_time = (jiffies + (HZ * 3) / 4) + 1;
3469         do {
3470                 if ((inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)) & 0x0008) != 0)
3471                         goto __nx_ok;
3472                 do_delay(trident);
3473         } while (time_after_eq(end_time, jiffies));
3474         snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)));
3475         return -EIO;
3476
3477  __nx_ok:
3478         /* DAC on */
3479         trident->ac97_ctrl = 0x00000002;
3480         outl(trident->ac97_ctrl, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3481         /* disable SB IRQ */
3482         outl(NX_SB_IRQ_DISABLE, TRID_REG(trident, T4D_MISCINT));
3483
3484         snd_trident_stop_all_voices(trident);
3485
3486         if (trident->tlb.entries != NULL) {
3487                 unsigned int i;
3488                 /* enable virtual addressing via TLB */
3489                 i = trident->tlb.entries_dmaaddr;
3490                 i |= 0x00000001;
3491                 outl(i, TRID_REG(trident, NX_TLBC));
3492         } else {
3493                 outl(0, TRID_REG(trident, NX_TLBC));
3494         }
3495         /* initialize S/PDIF */
3496         outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
3497         outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3498
3499         return 0;
3500 }
3501
3502 /*
3503  * initialize sis7018 chip
3504  */
3505 static int snd_trident_sis_init(struct snd_trident *trident)
3506 {
3507         int err;
3508
3509         if ((err = snd_trident_sis_reset(trident)) < 0)
3510                 return err;
3511
3512         snd_trident_stop_all_voices(trident);
3513
3514         /* initialize S/PDIF */
3515         outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
3516
3517         return 0;
3518 }
3519
3520 /*---------------------------------------------------------------------------
3521    snd_trident_create
3522   
3523    Description: This routine will create the device specific class for
3524                 the 4DWave card. It will also perform basic initialization.
3525                 
3526    Paramters:   card  - which card to create
3527                 pci   - interface to PCI bus resource info
3528                 dma1ptr - playback dma buffer
3529                 dma2ptr - capture dma buffer
3530                 irqptr  -  interrupt resource info
3531
3532    Returns:     4DWave device class private data
3533   
3534   ---------------------------------------------------------------------------*/
3535
3536 int __devinit snd_trident_create(struct snd_card *card,
3537                        struct pci_dev *pci,
3538                        int pcm_streams,
3539                        int pcm_spdif_device,
3540                        int max_wavetable_size,
3541                        struct snd_trident ** rtrident)
3542 {
3543         struct snd_trident *trident;
3544         int i, err;
3545         struct snd_trident_voice *voice;
3546         struct snd_trident_pcm_mixer *tmix;
3547         static struct snd_device_ops ops = {
3548                 .dev_free =     snd_trident_dev_free,
3549         };
3550
3551         *rtrident = NULL;
3552
3553         /* enable PCI device */
3554         if ((err = pci_enable_device(pci)) < 0)
3555                 return err;
3556         /* check, if we can restrict PCI DMA transfers to 30 bits */
3557         if (pci_set_dma_mask(pci, 0x3fffffff) < 0 ||
3558             pci_set_consistent_dma_mask(pci, 0x3fffffff) < 0) {
3559                 snd_printk(KERN_ERR "architecture does not support 30bit PCI busmaster DMA\n");
3560                 pci_disable_device(pci);
3561                 return -ENXIO;
3562         }
3563         
3564         trident = kzalloc(sizeof(*trident), GFP_KERNEL);
3565         if (trident == NULL) {
3566                 pci_disable_device(pci);
3567                 return -ENOMEM;
3568         }
3569         trident->device = (pci->vendor << 16) | pci->device;
3570         trident->card = card;
3571         trident->pci = pci;
3572         spin_lock_init(&trident->reg_lock);
3573         spin_lock_init(&trident->event_lock);
3574         spin_lock_init(&trident->voice_alloc);
3575         if (pcm_streams < 1)
3576                 pcm_streams = 1;
3577         if (pcm_streams > 32)
3578                 pcm_streams = 32;
3579         trident->ChanPCM = pcm_streams;
3580         if (max_wavetable_size < 0 )
3581                 max_wavetable_size = 0;
3582         trident->synth.max_size = max_wavetable_size * 1024;
3583         trident->irq = -1;
3584
3585         trident->midi_port = TRID_REG(trident, T4D_MPU401_BASE);
3586         pci_set_master(pci);
3587
3588         if ((err = pci_request_regions(pci, "Trident Audio")) < 0) {
3589                 kfree(trident);
3590                 pci_disable_device(pci);
3591                 return err;
3592         }
3593         trident->port = pci_resource_start(pci, 0);
3594
3595         if (request_irq(pci->irq, snd_trident_interrupt, SA_INTERRUPT|SA_SHIRQ,
3596                         "Trident Audio", trident)) {
3597                 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
3598                 snd_trident_free(trident);
3599                 return -EBUSY;
3600         }
3601         trident->irq = pci->irq;
3602
3603         /* allocate 16k-aligned TLB for NX cards */
3604         trident->tlb.entries = NULL;
3605         trident->tlb.buffer.area = NULL;
3606         if (trident->device == TRIDENT_DEVICE_ID_NX) {
3607                 if ((err = snd_trident_tlb_alloc(trident)) < 0) {
3608                         snd_trident_free(trident);
3609                         return err;
3610                 }
3611         }
3612
3613         trident->spdif_bits = trident->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
3614
3615         /* initialize chip */
3616         switch (trident->device) {
3617         case TRIDENT_DEVICE_ID_DX:
3618                 err = snd_trident_4d_dx_init(trident);
3619                 break;
3620         case TRIDENT_DEVICE_ID_NX:
3621                 err = snd_trident_4d_nx_init(trident);
3622                 break;
3623         case TRIDENT_DEVICE_ID_SI7018:
3624                 err = snd_trident_sis_init(trident);
3625                 break;
3626         default:
3627                 snd_BUG();
3628                 break;
3629         }
3630         if (err < 0) {
3631                 snd_trident_free(trident);
3632                 return err;
3633         }
3634
3635         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, trident, &ops)) < 0) {
3636                 snd_trident_free(trident);
3637                 return err;
3638         }
3639
3640         if ((err = snd_trident_mixer(trident, pcm_spdif_device)) < 0)
3641                 return err;
3642         
3643         /* initialise synth voices */
3644         for (i = 0; i < 64; i++) {
3645                 voice = &trident->synth.voices[i];
3646                 voice->number = i;
3647                 voice->trident = trident;
3648         }
3649         /* initialize pcm mixer entries */
3650         for (i = 0; i < 32; i++) {
3651                 tmix = &trident->pcm_mixer[i];
3652                 tmix->vol = T4D_DEFAULT_PCM_VOL;
3653                 tmix->pan = T4D_DEFAULT_PCM_PAN;
3654                 tmix->rvol = T4D_DEFAULT_PCM_RVOL;
3655                 tmix->cvol = T4D_DEFAULT_PCM_CVOL;
3656         }
3657
3658         snd_trident_enable_eso(trident);
3659
3660         snd_trident_proc_init(trident);
3661         snd_card_set_dev(card, &pci->dev);
3662         *rtrident = trident;
3663         return 0;
3664 }
3665
3666 /*---------------------------------------------------------------------------
3667    snd_trident_free
3668   
3669    Description: This routine will free the device specific class for
3670                 the 4DWave card. 
3671                 
3672    Paramters:   trident  - device specific private data for 4DWave card
3673
3674    Returns:     None.
3675   
3676   ---------------------------------------------------------------------------*/
3677
3678 static int snd_trident_free(struct snd_trident *trident)
3679 {
3680         snd_trident_free_gameport(trident);
3681         snd_trident_disable_eso(trident);
3682         // Disable S/PDIF out
3683         if (trident->device == TRIDENT_DEVICE_ID_NX)
3684                 outb(0x00, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3685         else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
3686                 outl(0, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3687         }
3688         if (trident->tlb.buffer.area) {
3689                 outl(0, TRID_REG(trident, NX_TLBC));
3690                 if (trident->tlb.memhdr)
3691                         snd_util_memhdr_free(trident->tlb.memhdr);
3692                 if (trident->tlb.silent_page.area)
3693                         snd_dma_free_pages(&trident->tlb.silent_page);
3694                 vfree(trident->tlb.shadow_entries);
3695                 snd_dma_free_pages(&trident->tlb.buffer);
3696         }
3697         if (trident->irq >= 0)
3698                 free_irq(trident->irq, trident);
3699         pci_release_regions(trident->pci);
3700         pci_disable_device(trident->pci);
3701         kfree(trident);
3702         return 0;
3703 }
3704
3705 /*---------------------------------------------------------------------------
3706    snd_trident_interrupt
3707   
3708    Description: ISR for Trident 4DWave device
3709                 
3710    Paramters:   trident  - device specific private data for 4DWave card
3711
3712    Problems:    It seems that Trident chips generates interrupts more than
3713                 one time in special cases. The spurious interrupts are
3714                 detected via sample timer (T4D_STIMER) and computing
3715                 corresponding delta value. The limits are detected with
3716                 the method try & fail so it is possible that it won't
3717                 work on all computers. [jaroslav]
3718
3719    Returns:     None.
3720   
3721   ---------------------------------------------------------------------------*/
3722
3723 static irqreturn_t snd_trident_interrupt(int irq, void *dev_id, struct pt_regs *regs)
3724 {
3725         struct snd_trident *trident = dev_id;
3726         unsigned int audio_int, chn_int, stimer, channel, mask, tmp;
3727         int delta;
3728         struct snd_trident_voice *voice;
3729
3730         audio_int = inl(TRID_REG(trident, T4D_MISCINT));
3731         if ((audio_int & (ADDRESS_IRQ|MPU401_IRQ)) == 0)
3732                 return IRQ_NONE;
3733         if (audio_int & ADDRESS_IRQ) {
3734                 // get interrupt status for all channels
3735                 spin_lock(&trident->reg_lock);
3736                 stimer = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
3737                 chn_int = inl(TRID_REG(trident, T4D_AINT_A));
3738                 if (chn_int == 0)
3739                         goto __skip1;
3740                 outl(chn_int, TRID_REG(trident, T4D_AINT_A));   /* ack */
3741               __skip1:
3742                 chn_int = inl(TRID_REG(trident, T4D_AINT_B));
3743                 if (chn_int == 0)
3744                         goto __skip2;
3745                 for (channel = 63; channel >= 32; channel--) {
3746                         mask = 1 << (channel&0x1f);
3747                         if ((chn_int & mask) == 0)
3748                                 continue;
3749                         voice = &trident->synth.voices[channel];
3750                         if (!voice->pcm || voice->substream == NULL) {
3751                                 outl(mask, TRID_REG(trident, T4D_STOP_B));
3752                                 continue;
3753                         }
3754                         delta = (int)stimer - (int)voice->stimer;
3755                         if (delta < 0)
3756                                 delta = -delta;
3757                         if ((unsigned int)delta < voice->spurious_threshold) {
3758                                 /* do some statistics here */
3759                                 trident->spurious_irq_count++;
3760                                 if (trident->spurious_irq_max_delta < (unsigned int)delta)
3761                                         trident->spurious_irq_max_delta = delta;
3762                                 continue;
3763                         }
3764                         voice->stimer = stimer;
3765                         if (voice->isync) {
3766                                 if (!voice->isync3) {
3767                                         tmp = inw(TRID_REG(trident, T4D_SBBL_SBCL));
3768                                         if (trident->bDMAStart & 0x40)
3769                                                 tmp >>= 1;
3770                                         if (tmp > 0)
3771                                                 tmp = voice->isync_max - tmp;
3772                                 } else {
3773                                         tmp = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
3774                                 }
3775                                 if (tmp < voice->isync_mark) {
3776                                         if (tmp > 0x10)
3777                                                 tmp = voice->isync_ESO - 7;
3778                                         else
3779                                                 tmp = voice->isync_ESO + 2;
3780                                         /* update ESO for IRQ voice to preserve sync */
3781                                         snd_trident_stop_voice(trident, voice->number);
3782                                         snd_trident_write_eso_reg(trident, voice, tmp);
3783                                         snd_trident_start_voice(trident, voice->number);
3784                                 }
3785                         } else if (voice->isync2) {
3786                                 voice->isync2 = 0;
3787                                 /* write original ESO and update CSO for IRQ voice to preserve sync */
3788                                 snd_trident_stop_voice(trident, voice->number);
3789                                 snd_trident_write_cso_reg(trident, voice, voice->isync_mark);
3790                                 snd_trident_write_eso_reg(trident, voice, voice->ESO);
3791                                 snd_trident_start_voice(trident, voice->number);
3792                         }
3793 #if 0
3794                         if (voice->extra) {
3795                                 /* update CSO for extra voice to preserve sync */
3796                                 snd_trident_stop_voice(trident, voice->extra->number);
3797                                 snd_trident_write_cso_reg(trident, voice->extra, 0);
3798                                 snd_trident_start_voice(trident, voice->extra->number);
3799                         }
3800 #endif
3801                         spin_unlock(&trident->reg_lock);
3802                         snd_pcm_period_elapsed(voice->substream);
3803                         spin_lock(&trident->reg_lock);
3804                 }
3805                 outl(chn_int, TRID_REG(trident, T4D_AINT_B));   /* ack */
3806               __skip2:
3807                 spin_unlock(&trident->reg_lock);
3808         }
3809         if (audio_int & MPU401_IRQ) {
3810                 if (trident->rmidi) {
3811                         snd_mpu401_uart_interrupt(irq, trident->rmidi->private_data, regs);
3812                 } else {
3813                         inb(TRID_REG(trident, T4D_MPUR0));
3814                 }
3815         }
3816         // outl((ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW), TRID_REG(trident, T4D_MISCINT));
3817         return IRQ_HANDLED;
3818 }
3819
3820 /*---------------------------------------------------------------------------
3821    snd_trident_attach_synthesizer
3822   
3823    Description: Attach synthesizer hooks
3824                 
3825    Paramters:   trident  - device specific private data for 4DWave card
3826
3827    Returns:     None.
3828   
3829   ---------------------------------------------------------------------------*/
3830 int snd_trident_attach_synthesizer(struct snd_trident *trident)
3831 {       
3832 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
3833         if (snd_seq_device_new(trident->card, 1, SNDRV_SEQ_DEV_ID_TRIDENT,
3834                                sizeof(struct snd_trident *), &trident->seq_dev) >= 0) {
3835                 strcpy(trident->seq_dev->name, "4DWave");
3836                 *(struct snd_trident **)SNDRV_SEQ_DEVICE_ARGPTR(trident->seq_dev) = trident;
3837         }
3838 #endif
3839         return 0;
3840 }
3841
3842 struct snd_trident_voice *snd_trident_alloc_voice(struct snd_trident * trident, int type, int client, int port)
3843 {
3844         struct snd_trident_voice *pvoice;
3845         unsigned long flags;
3846         int idx;
3847
3848         spin_lock_irqsave(&trident->voice_alloc, flags);
3849         if (type == SNDRV_TRIDENT_VOICE_TYPE_PCM) {
3850                 idx = snd_trident_allocate_pcm_channel(trident);
3851                 if(idx < 0) {
3852                         spin_unlock_irqrestore(&trident->voice_alloc, flags);
3853                         return NULL;
3854                 }
3855                 pvoice = &trident->synth.voices[idx];
3856                 pvoice->use = 1;
3857                 pvoice->pcm = 1;
3858                 pvoice->capture = 0;
3859                 pvoice->spdif = 0;
3860                 pvoice->memblk = NULL;
3861                 pvoice->substream = NULL;
3862                 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3863                 return pvoice;
3864         }
3865         if (type == SNDRV_TRIDENT_VOICE_TYPE_SYNTH) {
3866                 idx = snd_trident_allocate_synth_channel(trident);
3867                 if(idx < 0) {
3868                         spin_unlock_irqrestore(&trident->voice_alloc, flags);
3869                         return NULL;
3870                 }
3871                 pvoice = &trident->synth.voices[idx];
3872                 pvoice->use = 1;
3873                 pvoice->synth = 1;
3874                 pvoice->client = client;
3875                 pvoice->port = port;
3876                 pvoice->memblk = NULL;
3877                 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3878                 return pvoice;
3879         }
3880         if (type == SNDRV_TRIDENT_VOICE_TYPE_MIDI) {
3881         }
3882         spin_unlock_irqrestore(&trident->voice_alloc, flags);
3883         return NULL;
3884 }
3885
3886 void snd_trident_free_voice(struct snd_trident * trident, struct snd_trident_voice *voice)
3887 {
3888         unsigned long flags;
3889         void (*private_free)(struct snd_trident_voice *);
3890         void *private_data;
3891
3892         if (voice == NULL || !voice->use)
3893                 return;
3894         snd_trident_clear_voices(trident, voice->number, voice->number);
3895         spin_lock_irqsave(&trident->voice_alloc, flags);
3896         private_free = voice->private_free;
3897         private_data = voice->private_data;
3898         voice->private_free = NULL;
3899         voice->private_data = NULL;
3900         if (voice->pcm)
3901                 snd_trident_free_pcm_channel(trident, voice->number);
3902         if (voice->synth)
3903                 snd_trident_free_synth_channel(trident, voice->number);
3904         voice->use = voice->pcm = voice->synth = voice->midi = 0;
3905         voice->capture = voice->spdif = 0;
3906         voice->sample_ops = NULL;
3907         voice->substream = NULL;
3908         voice->extra = NULL;
3909         spin_unlock_irqrestore(&trident->voice_alloc, flags);
3910         if (private_free)
3911                 private_free(voice);
3912 }
3913
3914 static void snd_trident_clear_voices(struct snd_trident * trident, unsigned short v_min, unsigned short v_max)
3915 {
3916         unsigned int i, val, mask[2] = { 0, 0 };
3917
3918         snd_assert(v_min <= 63, return);
3919         snd_assert(v_max <= 63, return);
3920         for (i = v_min; i <= v_max; i++)
3921                 mask[i >> 5] |= 1 << (i & 0x1f);
3922         if (mask[0]) {
3923                 outl(mask[0], TRID_REG(trident, T4D_STOP_A));
3924                 val = inl(TRID_REG(trident, T4D_AINTEN_A));
3925                 outl(val & ~mask[0], TRID_REG(trident, T4D_AINTEN_A));
3926         }
3927         if (mask[1]) {
3928                 outl(mask[1], TRID_REG(trident, T4D_STOP_B));
3929                 val = inl(TRID_REG(trident, T4D_AINTEN_B));
3930                 outl(val & ~mask[1], TRID_REG(trident, T4D_AINTEN_B));
3931         }
3932 }
3933
3934 #ifdef CONFIG_PM
3935 int snd_trident_suspend(struct pci_dev *pci, pm_message_t state)
3936 {
3937         struct snd_card *card = pci_get_drvdata(pci);
3938         struct snd_trident *trident = card->private_data;
3939
3940         trident->in_suspend = 1;
3941         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
3942         snd_pcm_suspend_all(trident->pcm);
3943         snd_pcm_suspend_all(trident->foldback);
3944         snd_pcm_suspend_all(trident->spdif);
3945
3946         snd_ac97_suspend(trident->ac97);
3947         snd_ac97_suspend(trident->ac97_sec);
3948
3949         switch (trident->device) {
3950         case TRIDENT_DEVICE_ID_DX:
3951         case TRIDENT_DEVICE_ID_NX:
3952                 break;                  /* TODO */
3953         case TRIDENT_DEVICE_ID_SI7018:
3954                 break;
3955         }
3956         pci_disable_device(pci);
3957         pci_save_state(pci);
3958         return 0;
3959 }
3960
3961 int snd_trident_resume(struct pci_dev *pci)
3962 {
3963         struct snd_card *card = pci_get_drvdata(pci);
3964         struct snd_trident *trident = card->private_data;
3965
3966         pci_restore_state(pci);
3967         pci_enable_device(pci);
3968         pci_set_master(pci); /* to be sure */
3969
3970         switch (trident->device) {
3971         case TRIDENT_DEVICE_ID_DX:
3972                 snd_trident_4d_dx_init(trident);
3973                 break;
3974         case TRIDENT_DEVICE_ID_NX:
3975                 snd_trident_4d_nx_init(trident);
3976                 break;
3977         case TRIDENT_DEVICE_ID_SI7018:
3978                 snd_trident_sis_init(trident);
3979                 break;
3980         }
3981
3982         snd_ac97_resume(trident->ac97);
3983         snd_ac97_resume(trident->ac97_sec);
3984
3985         /* restore some registers */
3986         outl(trident->musicvol_wavevol, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3987
3988         snd_trident_enable_eso(trident);
3989
3990         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
3991         trident->in_suspend = 0;
3992         return 0;
3993 }
3994 #endif /* CONFIG_PM */
3995
3996 EXPORT_SYMBOL(snd_trident_alloc_voice);
3997 EXPORT_SYMBOL(snd_trident_free_voice);
3998 EXPORT_SYMBOL(snd_trident_start_voice);
3999 EXPORT_SYMBOL(snd_trident_stop_voice);
4000 EXPORT_SYMBOL(snd_trident_write_voice_regs);
4001 /* trident_memory.c symbols */
4002 EXPORT_SYMBOL(snd_trident_synth_alloc);
4003 EXPORT_SYMBOL(snd_trident_synth_free);
4004 EXPORT_SYMBOL(snd_trident_synth_copy_from_user);