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