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