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