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