[ALSA] Remove xxx_t typedefs: PCI Trident
[pandora-kernel.git] / sound / pci / trident / trident_main.c
1 /*
2  *  Maintained by Jaroslav Kysela <perex@suse.cz>
3  *  Originated by audio@tridentmicro.com
4  *  Fri Feb 19 15:55:28 MST 1999
5  *  Routines for control of Trident 4DWave (DX and NX) chip
6  *
7  *  BUGS:
8  *
9  *  TODO:
10  *    ---
11  *
12  *   This program is free software; you can redistribute it and/or modify
13  *   it under the terms of the GNU General Public License as published by
14  *   the Free Software Foundation; either version 2 of the License, or
15  *   (at your option) any later version.
16  *
17  *   This program is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with this program; if not, write to the Free Software
24  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25  *
26  *
27  *  SiS7018 S/PDIF support by Thomas Winischhofer <thomas@winischhofer.net>
28  */
29
30 #include <sound/driver.h>
31 #include <linux/delay.h>
32 #include <linux/init.h>
33 #include <linux/interrupt.h>
34 #include <linux/pci.h>
35 #include <linux/slab.h>
36 #include <linux/vmalloc.h>
37 #include <linux/gameport.h>
38
39 #include <sound/core.h>
40 #include <sound/info.h>
41 #include <sound/control.h>
42 #include <sound/trident.h>
43 #include <sound/asoundef.h>
44
45 #include <asm/io.h>
46
47 static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
48                                        struct snd_trident_voice * voice,
49                                        struct snd_pcm_substream *substream);
50 static int snd_trident_pcm_mixer_free(struct snd_trident *trident,
51                                       struct snd_trident_voice * voice,
52                                       struct snd_pcm_substream *substream);
53 static irqreturn_t snd_trident_interrupt(int irq, void *dev_id,
54                                          struct pt_regs *regs);
55 #ifdef CONFIG_PM
56 static int snd_trident_suspend(struct snd_card *card, pm_message_t state);
57 static int snd_trident_resume(struct snd_card *card);
58 #endif
59 static int snd_trident_sis_reset(struct snd_trident *trident);
60
61 static void snd_trident_clear_voices(struct snd_trident * trident,
62                                      unsigned short v_min, unsigned short v_max);
63 static int snd_trident_free(struct snd_trident *trident);
64
65 /*
66  *  common I/O routines
67  */
68
69
70 #if 0
71 static void snd_trident_print_voice_regs(struct snd_trident *trident, int voice)
72 {
73         unsigned int val, tmp;
74
75         printk("Trident voice %i:\n", voice);
76         outb(voice, TRID_REG(trident, T4D_LFO_GC_CIR));
77         val = inl(TRID_REG(trident, CH_LBA));
78         printk("LBA: 0x%x\n", val);
79         val = inl(TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
80         printk("GVSel: %i\n", val >> 31);
81         printk("Pan: 0x%x\n", (val >> 24) & 0x7f);
82         printk("Vol: 0x%x\n", (val >> 16) & 0xff);
83         printk("CTRL: 0x%x\n", (val >> 12) & 0x0f);
84         printk("EC: 0x%x\n", val & 0x0fff);
85         if (trident->device != TRIDENT_DEVICE_ID_NX) {
86                 val = inl(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS));
87                 printk("CSO: 0x%x\n", val >> 16);
88                 printk("Alpha: 0x%x\n", (val >> 4) & 0x0fff);
89                 printk("FMS: 0x%x\n", val & 0x0f);
90                 val = inl(TRID_REG(trident, CH_DX_ESO_DELTA));
91                 printk("ESO: 0x%x\n", val >> 16);
92                 printk("Delta: 0x%x\n", val & 0xffff);
93                 val = inl(TRID_REG(trident, CH_DX_FMC_RVOL_CVOL));
94         } else {                // TRIDENT_DEVICE_ID_NX
95                 val = inl(TRID_REG(trident, CH_NX_DELTA_CSO));
96                 tmp = (val >> 24) & 0xff;
97                 printk("CSO: 0x%x\n", val & 0x00ffffff);
98                 val = inl(TRID_REG(trident, CH_NX_DELTA_ESO));
99                 tmp |= (val >> 16) & 0xff00;
100                 printk("Delta: 0x%x\n", tmp);
101                 printk("ESO: 0x%x\n", val & 0x00ffffff);
102                 val = inl(TRID_REG(trident, CH_NX_ALPHA_FMS_FMC_RVOL_CVOL));
103                 printk("Alpha: 0x%x\n", val >> 20);
104                 printk("FMS: 0x%x\n", (val >> 16) & 0x0f);
105         }
106         printk("FMC: 0x%x\n", (val >> 14) & 3);
107         printk("RVol: 0x%x\n", (val >> 7) & 0x7f);
108         printk("CVol: 0x%x\n", val & 0x7f);
109 }
110 #endif
111
112 /*---------------------------------------------------------------------------
113    unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
114   
115    Description: This routine will do all of the reading from the external
116                 CODEC (AC97).
117   
118    Parameters:  ac97 - ac97 codec structure
119                 reg - CODEC register index, from AC97 Hal.
120  
121    returns:     16 bit value read from the AC97.
122   
123   ---------------------------------------------------------------------------*/
124 static unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
125 {
126         unsigned int data = 0, treg;
127         unsigned short count = 0xffff;
128         unsigned long flags;
129         struct snd_trident *trident = ac97->private_data;
130
131         spin_lock_irqsave(&trident->reg_lock, flags);
132         if (trident->device == TRIDENT_DEVICE_ID_DX) {
133                 data = (DX_AC97_BUSY_READ | (reg & 0x000000ff));
134                 outl(data, TRID_REG(trident, DX_ACR1_AC97_R));
135                 do {
136                         data = inl(TRID_REG(trident, DX_ACR1_AC97_R));
137                         if ((data & DX_AC97_BUSY_READ) == 0)
138                                 break;
139                 } while (--count);
140         } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
141                 data = (NX_AC97_BUSY_READ | (reg & 0x000000ff));
142                 treg = ac97->num == 0 ? NX_ACR2_AC97_R_PRIMARY : NX_ACR3_AC97_R_SECONDARY;
143                 outl(data, TRID_REG(trident, treg));
144                 do {
145                         data = inl(TRID_REG(trident, treg));
146                         if ((data & 0x00000C00) == 0)
147                                 break;
148                 } while (--count);
149         } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
150                 data = SI_AC97_BUSY_READ | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
151                 if (ac97->num == 1)
152                         data |= SI_AC97_SECONDARY;
153                 outl(data, TRID_REG(trident, SI_AC97_READ));
154                 do {
155                         data = inl(TRID_REG(trident, SI_AC97_READ));
156                         if ((data & (SI_AC97_BUSY_READ)) == 0)
157                                 break;
158                 } while (--count);
159         }
160
161         if (count == 0 && !trident->ac97_detect) {
162                 snd_printk(KERN_ERR "ac97 codec read TIMEOUT [0x%x/0x%x]!!!\n",
163                            reg, data);
164                 data = 0;
165         }
166
167         spin_unlock_irqrestore(&trident->reg_lock, flags);
168         return ((unsigned short) (data >> 16));
169 }
170
171 /*---------------------------------------------------------------------------
172    void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
173    unsigned short wdata)
174   
175    Description: This routine will do all of the writing to the external
176                 CODEC (AC97).
177   
178    Parameters:  ac97 - ac97 codec structure
179                 reg - CODEC register index, from AC97 Hal.
180                 data  - Lower 16 bits are the data to write to CODEC.
181   
182    returns:     TRUE if everything went ok, else FALSE.
183   
184   ---------------------------------------------------------------------------*/
185 static void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
186                                     unsigned short wdata)
187 {
188         unsigned int address, data;
189         unsigned short count = 0xffff;
190         unsigned long flags;
191         struct snd_trident *trident = ac97->private_data;
192
193         data = ((unsigned long) wdata) << 16;
194
195         spin_lock_irqsave(&trident->reg_lock, flags);
196         if (trident->device == TRIDENT_DEVICE_ID_DX) {
197                 address = DX_ACR0_AC97_W;
198
199                 /* read AC-97 write register status */
200                 do {
201                         if ((inw(TRID_REG(trident, address)) & DX_AC97_BUSY_WRITE) == 0)
202                                 break;
203                 } while (--count);
204
205                 data |= (DX_AC97_BUSY_WRITE | (reg & 0x000000ff));
206         } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
207                 address = NX_ACR1_AC97_W;
208
209                 /* read AC-97 write register status */
210                 do {
211                         if ((inw(TRID_REG(trident, address)) & NX_AC97_BUSY_WRITE) == 0)
212                                 break;
213                 } while (--count);
214
215                 data |= (NX_AC97_BUSY_WRITE | (ac97->num << 8) | (reg & 0x000000ff));
216         } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
217                 address = SI_AC97_WRITE;
218
219                 /* read AC-97 write register status */
220                 do {
221                         if ((inw(TRID_REG(trident, address)) & (SI_AC97_BUSY_WRITE)) == 0)
222                                 break;
223                 } while (--count);
224
225                 data |= SI_AC97_BUSY_WRITE | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
226                 if (ac97->num == 1)
227                         data |= SI_AC97_SECONDARY;
228         } else {
229                 address = 0;    /* keep GCC happy */
230                 count = 0;      /* return */
231         }
232
233         if (count == 0) {
234                 spin_unlock_irqrestore(&trident->reg_lock, flags);
235                 return;
236         }
237         outl(data, TRID_REG(trident, address));
238         spin_unlock_irqrestore(&trident->reg_lock, flags);
239 }
240
241 /*---------------------------------------------------------------------------
242    void snd_trident_enable_eso(struct snd_trident *trident)
243   
244    Description: This routine will enable end of loop interrupts.
245                 End of loop interrupts will occur when a running
246                 channel reaches ESO.
247                 Also enables middle of loop interrupts.
248   
249    Parameters:  trident - pointer to target device class for 4DWave.
250   
251   ---------------------------------------------------------------------------*/
252
253 static void snd_trident_enable_eso(struct snd_trident * trident)
254 {
255         unsigned int val;
256
257         val = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
258         val |= ENDLP_IE;
259         val |= MIDLP_IE;
260         if (trident->device == TRIDENT_DEVICE_ID_SI7018)
261                 val |= BANK_B_EN;
262         outl(val, TRID_REG(trident, T4D_LFO_GC_CIR));
263 }
264
265 /*---------------------------------------------------------------------------
266    void snd_trident_disable_eso(struct snd_trident *trident)
267   
268    Description: This routine will disable end of loop interrupts.
269                 End of loop interrupts will occur when a running
270                 channel reaches ESO.
271                 Also disables middle of loop interrupts.
272   
273    Parameters:  
274                 trident - pointer to target device class for 4DWave.
275   
276    returns:     TRUE if everything went ok, else FALSE.
277   
278   ---------------------------------------------------------------------------*/
279
280 static void snd_trident_disable_eso(struct snd_trident * trident)
281 {
282         unsigned int tmp;
283
284         tmp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
285         tmp &= ~ENDLP_IE;
286         tmp &= ~MIDLP_IE;
287         outl(tmp, TRID_REG(trident, T4D_LFO_GC_CIR));
288 }
289
290 /*---------------------------------------------------------------------------
291    void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
292
293     Description: Start a voice, any channel 0 thru 63.
294                  This routine automatically handles the fact that there are
295                  more than 32 channels available.
296
297     Parameters : voice - Voice number 0 thru n.
298                  trident - pointer to target device class for 4DWave.
299
300     Return Value: None.
301
302   ---------------------------------------------------------------------------*/
303
304 void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
305 {
306         unsigned int mask = 1 << (voice & 0x1f);
307         unsigned int reg = (voice & 0x20) ? T4D_START_B : T4D_START_A;
308
309         outl(mask, TRID_REG(trident, reg));
310 }
311
312 /*---------------------------------------------------------------------------
313    void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
314
315     Description: Stop a voice, any channel 0 thru 63.
316                  This routine automatically handles the fact that there are
317                  more than 32 channels available.
318
319     Parameters : voice - Voice number 0 thru n.
320                  trident - pointer to target device class for 4DWave.
321
322     Return Value: None.
323
324   ---------------------------------------------------------------------------*/
325
326 void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
327 {
328         unsigned int mask = 1 << (voice & 0x1f);
329         unsigned int reg = (voice & 0x20) ? T4D_STOP_B : T4D_STOP_A;
330
331         outl(mask, TRID_REG(trident, reg));
332 }
333
334 /*---------------------------------------------------------------------------
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    Paramters:   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 /*---------------------------------------------------------------------------
509    snd_trident_write_cso_reg
510   
511    Description: This routine will write the new CSO offset
512                 register to hardware.
513   
514    Paramters:   trident - pointer to target device class for 4DWave.
515                 voice - synthesizer voice structure
516                 CSO - new CSO value
517   
518   ---------------------------------------------------------------------------*/
519
520 static void snd_trident_write_cso_reg(struct snd_trident * trident,
521                                       struct snd_trident_voice * voice,
522                                       unsigned int CSO)
523 {
524         voice->CSO = CSO;
525         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
526         if (trident->device != TRIDENT_DEVICE_ID_NX) {
527                 outw(voice->CSO, TRID_REG(trident, CH_DX_CSO_ALPHA_FMS) + 2);
528         } else {
529                 outl((voice->Delta << 24) |
530                      (voice->CSO & 0x00ffffff), TRID_REG(trident, CH_NX_DELTA_CSO));
531         }
532 }
533
534 /*---------------------------------------------------------------------------
535    snd_trident_write_eso_reg
536   
537    Description: This routine will write the new ESO offset
538                 register to hardware.
539   
540    Paramters:   trident - pointer to target device class for 4DWave.
541                 voice - synthesizer voice structure
542                 ESO - new ESO value
543   
544   ---------------------------------------------------------------------------*/
545
546 static void snd_trident_write_eso_reg(struct snd_trident * trident,
547                                       struct snd_trident_voice * voice,
548                                       unsigned int ESO)
549 {
550         voice->ESO = ESO;
551         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
552         if (trident->device != TRIDENT_DEVICE_ID_NX) {
553                 outw(voice->ESO, TRID_REG(trident, CH_DX_ESO_DELTA) + 2);
554         } else {
555                 outl(((voice->Delta << 16) & 0xff000000) | (voice->ESO & 0x00ffffff),
556                      TRID_REG(trident, CH_NX_DELTA_ESO));
557         }
558 }
559
560 /*---------------------------------------------------------------------------
561    snd_trident_write_vol_reg
562   
563    Description: This routine will write the new voice volume
564                 register to hardware.
565   
566    Paramters:   trident - pointer to target device class for 4DWave.
567                 voice - synthesizer voice structure
568                 Vol - new voice volume
569   
570   ---------------------------------------------------------------------------*/
571
572 static void snd_trident_write_vol_reg(struct snd_trident * trident,
573                                       struct snd_trident_voice * voice,
574                                       unsigned int Vol)
575 {
576         voice->Vol = Vol;
577         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
578         switch (trident->device) {
579         case TRIDENT_DEVICE_ID_DX:
580         case TRIDENT_DEVICE_ID_NX:
581                 outb(voice->Vol >> 2, TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 2));
582                 break;
583         case TRIDENT_DEVICE_ID_SI7018:
584                 // printk("voice->Vol = 0x%x\n", voice->Vol);
585                 outw((voice->CTRL << 12) | voice->Vol,
586                      TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
587                 break;
588         }
589 }
590
591 /*---------------------------------------------------------------------------
592    snd_trident_write_pan_reg
593   
594    Description: This routine will write the new voice pan
595                 register to hardware.
596   
597    Paramters:   trident - pointer to target device class for 4DWave.
598                 voice - synthesizer voice structure
599                 Pan - new pan value
600   
601   ---------------------------------------------------------------------------*/
602
603 static void snd_trident_write_pan_reg(struct snd_trident * trident,
604                                       struct snd_trident_voice * voice,
605                                       unsigned int Pan)
606 {
607         voice->Pan = Pan;
608         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
609         outb(((voice->GVSel & 0x01) << 7) | (voice->Pan & 0x7f),
610              TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 3));
611 }
612
613 /*---------------------------------------------------------------------------
614    snd_trident_write_rvol_reg
615   
616    Description: This routine will write the new reverb volume
617                 register to hardware.
618   
619    Paramters:   trident - pointer to target device class for 4DWave.
620                 voice - synthesizer voice structure
621                 RVol - new reverb volume
622   
623   ---------------------------------------------------------------------------*/
624
625 static void snd_trident_write_rvol_reg(struct snd_trident * trident,
626                                        struct snd_trident_voice * voice,
627                                        unsigned int RVol)
628 {
629         voice->RVol = RVol;
630         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
631         outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
632              (voice->CVol & 0x007f),
633              TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
634                       CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
635 }
636
637 /*---------------------------------------------------------------------------
638    snd_trident_write_cvol_reg
639   
640    Description: This routine will write the new chorus volume
641                 register to hardware.
642   
643    Paramters:   trident - pointer to target device class for 4DWave.
644                 voice - synthesizer voice structure
645                 CVol - new chorus volume
646   
647   ---------------------------------------------------------------------------*/
648
649 static void snd_trident_write_cvol_reg(struct snd_trident * trident,
650                                        struct snd_trident_voice * voice,
651                                        unsigned int CVol)
652 {
653         voice->CVol = CVol;
654         outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
655         outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
656              (voice->CVol & 0x007f),
657              TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
658                       CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
659 }
660
661 /*---------------------------------------------------------------------------
662    snd_trident_convert_rate
663
664    Description: This routine converts rate in HZ to hardware delta value.
665   
666    Paramters:   trident - pointer to target device class for 4DWave.
667                 rate - Real or Virtual channel number.
668   
669    Returns:     Delta value.
670   
671   ---------------------------------------------------------------------------*/
672 static unsigned int snd_trident_convert_rate(unsigned int rate)
673 {
674         unsigned int delta;
675
676         // We special case 44100 and 8000 since rounding with the equation
677         // does not give us an accurate enough value. For 11025 and 22050
678         // the equation gives us the best answer. All other frequencies will
679         // also use the equation. JDW
680         if (rate == 44100)
681                 delta = 0xeb3;
682         else if (rate == 8000)
683                 delta = 0x2ab;
684         else if (rate == 48000)
685                 delta = 0x1000;
686         else
687                 delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
688         return delta;
689 }
690
691 /*---------------------------------------------------------------------------
692    snd_trident_convert_adc_rate
693
694    Description: This routine converts rate in HZ to hardware delta value.
695   
696    Paramters:   trident - pointer to target device class for 4DWave.
697                 rate - Real or Virtual channel number.
698   
699    Returns:     Delta value.
700   
701   ---------------------------------------------------------------------------*/
702 static unsigned int snd_trident_convert_adc_rate(unsigned int rate)
703 {
704         unsigned int delta;
705
706         // We special case 44100 and 8000 since rounding with the equation
707         // does not give us an accurate enough value. For 11025 and 22050
708         // the equation gives us the best answer. All other frequencies will
709         // also use the equation. JDW
710         if (rate == 44100)
711                 delta = 0x116a;
712         else if (rate == 8000)
713                 delta = 0x6000;
714         else if (rate == 48000)
715                 delta = 0x1000;
716         else
717                 delta = ((48000 << 12) / rate) & 0x0000ffff;
718         return delta;
719 }
720
721 /*---------------------------------------------------------------------------
722    snd_trident_spurious_threshold
723
724    Description: This routine converts rate in HZ to spurious threshold.
725   
726    Paramters:   trident - pointer to target device class for 4DWave.
727                 rate - Real or Virtual channel number.
728   
729    Returns:     Delta value.
730   
731   ---------------------------------------------------------------------------*/
732 static unsigned int snd_trident_spurious_threshold(unsigned int rate,
733                                                    unsigned int period_size)
734 {
735         unsigned int res = (rate * period_size) / 48000;
736         if (res < 64)
737                 res = res / 2;
738         else
739                 res -= 32;
740         return res;
741 }
742
743 /*---------------------------------------------------------------------------
744    snd_trident_control_mode
745
746    Description: This routine returns a control mode for a PCM channel.
747   
748    Paramters:   trident - pointer to target device class for 4DWave.
749                 substream  - PCM substream
750   
751    Returns:     Control value.
752   
753   ---------------------------------------------------------------------------*/
754 static unsigned int snd_trident_control_mode(struct snd_pcm_substream *substream)
755 {
756         unsigned int CTRL;
757         struct snd_pcm_runtime *runtime = substream->runtime;
758
759         /* set ctrl mode
760            CTRL default: 8-bit (unsigned) mono, loop mode enabled
761          */
762         CTRL = 0x00000001;
763         if (snd_pcm_format_width(runtime->format) == 16)
764                 CTRL |= 0x00000008;     // 16-bit data
765         if (snd_pcm_format_signed(runtime->format))
766                 CTRL |= 0x00000002;     // signed data
767         if (runtime->channels > 1)
768                 CTRL |= 0x00000004;     // stereo data
769         return CTRL;
770 }
771
772 /*
773  *  PCM part
774  */
775
776 /*---------------------------------------------------------------------------
777    snd_trident_ioctl
778   
779    Description: Device I/O control handler for playback/capture parameters.
780   
781    Paramters:   substream  - PCM substream class
782                 cmd     - what ioctl message to process
783                 arg     - additional message infoarg     
784   
785    Returns:     Error status
786   
787   ---------------------------------------------------------------------------*/
788
789 static int snd_trident_ioctl(struct snd_pcm_substream *substream,
790                              unsigned int cmd,
791                              void *arg)
792 {
793         /* FIXME: it seems that with small periods the behaviour of
794                   trident hardware is unpredictable and interrupt generator
795                   is broken */
796         return snd_pcm_lib_ioctl(substream, cmd, arg);
797 }
798
799 /*---------------------------------------------------------------------------
800    snd_trident_allocate_pcm_mem
801   
802    Description: Allocate PCM ring buffer for given substream
803   
804    Parameters:  substream  - PCM substream class
805                 hw_params  - hardware parameters
806   
807    Returns:     Error status
808   
809   ---------------------------------------------------------------------------*/
810
811 static int snd_trident_allocate_pcm_mem(struct snd_pcm_substream *substream,
812                                         struct snd_pcm_hw_params *hw_params)
813 {
814         struct snd_trident *trident = snd_pcm_substream_chip(substream);
815         struct snd_pcm_runtime *runtime = substream->runtime;
816         struct snd_trident_voice *voice = runtime->private_data;
817         int err;
818
819         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
820                 return err;
821         if (trident->tlb.entries) {
822                 if (err > 0) { /* change */
823                         if (voice->memblk)
824                                 snd_trident_free_pages(trident, voice->memblk);
825                         voice->memblk = snd_trident_alloc_pages(trident, substream);
826                         if (voice->memblk == NULL)
827                                 return -ENOMEM;
828                 }
829         }
830         return 0;
831 }
832
833 /*---------------------------------------------------------------------------
834    snd_trident_allocate_evoice
835   
836    Description: Allocate extra voice as interrupt generator
837   
838    Parameters:  substream  - PCM substream class
839                 hw_params  - hardware parameters
840   
841    Returns:     Error status
842   
843   ---------------------------------------------------------------------------*/
844
845 static int snd_trident_allocate_evoice(struct snd_pcm_substream *substream,
846                                        struct snd_pcm_hw_params *hw_params)
847 {
848         struct snd_trident *trident = snd_pcm_substream_chip(substream);
849         struct snd_pcm_runtime *runtime = substream->runtime;
850         struct snd_trident_voice *voice = runtime->private_data;
851         struct snd_trident_voice *evoice = voice->extra;
852
853         /* voice management */
854
855         if (params_buffer_size(hw_params) / 2 != params_period_size(hw_params)) {
856                 if (evoice == NULL) {
857                         evoice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
858                         if (evoice == NULL)
859                                 return -ENOMEM;
860                         voice->extra = evoice;
861                         evoice->substream = substream;
862                 }
863         } else {
864                 if (evoice != NULL) {
865                         snd_trident_free_voice(trident, evoice);
866                         voice->extra = evoice = NULL;
867                 }
868         }
869
870         return 0;
871 }
872
873 /*---------------------------------------------------------------------------
874    snd_trident_hw_params
875   
876    Description: Set the hardware parameters for the playback device.
877   
878    Parameters:  substream  - PCM substream class
879                 hw_params  - hardware parameters
880   
881    Returns:     Error status
882   
883   ---------------------------------------------------------------------------*/
884
885 static int snd_trident_hw_params(struct snd_pcm_substream *substream,
886                                  struct snd_pcm_hw_params *hw_params)
887 {
888         int err;
889
890         err = snd_trident_allocate_pcm_mem(substream, hw_params);
891         if (err >= 0)
892                 err = snd_trident_allocate_evoice(substream, hw_params);
893         return err;
894 }
895
896 /*---------------------------------------------------------------------------
897    snd_trident_playback_hw_free
898   
899    Description: Release the hardware resources for the playback device.
900   
901    Parameters:  substream  - PCM substream class
902   
903    Returns:     Error status
904   
905   ---------------------------------------------------------------------------*/
906
907 static int snd_trident_hw_free(struct snd_pcm_substream *substream)
908 {
909         struct snd_trident *trident = snd_pcm_substream_chip(substream);
910         struct snd_pcm_runtime *runtime = substream->runtime;
911         struct snd_trident_voice *voice = runtime->private_data;
912         struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
913
914         if (trident->tlb.entries) {
915                 if (voice && voice->memblk) {
916                         snd_trident_free_pages(trident, voice->memblk);
917                         voice->memblk = NULL;
918                 }
919         }
920         snd_pcm_lib_free_pages(substream);
921         if (evoice != NULL) {
922                 snd_trident_free_voice(trident, evoice);
923                 voice->extra = NULL;
924         }
925         return 0;
926 }
927
928 /*---------------------------------------------------------------------------
929    snd_trident_playback_prepare
930   
931    Description: Prepare playback device for playback.
932   
933    Parameters:  substream  - PCM substream class
934   
935    Returns:     Error status
936   
937   ---------------------------------------------------------------------------*/
938
939 static int snd_trident_playback_prepare(struct snd_pcm_substream *substream)
940 {
941         struct snd_trident *trident = snd_pcm_substream_chip(substream);
942         struct snd_pcm_runtime *runtime = substream->runtime;
943         struct snd_trident_voice *voice = runtime->private_data;
944         struct snd_trident_voice *evoice = voice->extra;
945         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
946
947         spin_lock_irq(&trident->reg_lock);      
948
949         /* set delta (rate) value */
950         voice->Delta = snd_trident_convert_rate(runtime->rate);
951         voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
952
953         /* set Loop Begin Address */
954         if (voice->memblk)
955                 voice->LBA = voice->memblk->offset;
956         else
957                 voice->LBA = runtime->dma_addr;
958  
959         voice->CSO = 0;
960         voice->ESO = runtime->buffer_size - 1;  /* in samples */
961         voice->CTRL = snd_trident_control_mode(substream);
962         voice->FMC = 3;
963         voice->GVSel = 1;
964         voice->EC = 0;
965         voice->Alpha = 0;
966         voice->FMS = 0;
967         voice->Vol = mix->vol;
968         voice->RVol = mix->rvol;
969         voice->CVol = mix->cvol;
970         voice->Pan = mix->pan;
971         voice->Attribute = 0;
972 #if 0
973         voice->Attribute = (1<<(30-16))|(2<<(26-16))|
974                            (0<<(24-16))|(0x1f<<(19-16));
975 #else
976         voice->Attribute = 0;
977 #endif
978
979         snd_trident_write_voice_regs(trident, voice);
980
981         if (evoice != NULL) {
982                 evoice->Delta = voice->Delta;
983                 evoice->spurious_threshold = voice->spurious_threshold;
984                 evoice->LBA = voice->LBA;
985                 evoice->CSO = 0;
986                 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
987                 evoice->CTRL = voice->CTRL;
988                 evoice->FMC = 3;
989                 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
990                 evoice->EC = 0;
991                 evoice->Alpha = 0;
992                 evoice->FMS = 0;
993                 evoice->Vol = 0x3ff;                    /* mute */
994                 evoice->RVol = evoice->CVol = 0x7f;     /* mute */
995                 evoice->Pan = 0x7f;                     /* mute */
996 #if 0
997                 evoice->Attribute = (1<<(30-16))|(2<<(26-16))|
998                                     (0<<(24-16))|(0x1f<<(19-16));
999 #else
1000                 evoice->Attribute = 0;
1001 #endif
1002                 snd_trident_write_voice_regs(trident, evoice);
1003                 evoice->isync2 = 1;
1004                 evoice->isync_mark = runtime->period_size;
1005                 evoice->ESO = (runtime->period_size * 2) - 1;
1006         }
1007
1008         spin_unlock_irq(&trident->reg_lock);
1009
1010         return 0;
1011 }
1012
1013 /*---------------------------------------------------------------------------
1014    snd_trident_capture_hw_params
1015   
1016    Description: Set the hardware parameters for the capture device.
1017   
1018    Parameters:  substream  - PCM substream class
1019                 hw_params  - hardware parameters
1020   
1021    Returns:     Error status
1022   
1023   ---------------------------------------------------------------------------*/
1024
1025 static int snd_trident_capture_hw_params(struct snd_pcm_substream *substream,
1026                                          struct snd_pcm_hw_params *hw_params)
1027 {
1028         return snd_trident_allocate_pcm_mem(substream, hw_params);
1029 }
1030
1031 /*---------------------------------------------------------------------------
1032    snd_trident_capture_prepare
1033   
1034    Description: Prepare capture device for playback.
1035   
1036    Parameters:  substream  - PCM substream class
1037   
1038    Returns:     Error status
1039   
1040   ---------------------------------------------------------------------------*/
1041
1042 static int snd_trident_capture_prepare(struct snd_pcm_substream *substream)
1043 {
1044         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1045         struct snd_pcm_runtime *runtime = substream->runtime;
1046         struct snd_trident_voice *voice = runtime->private_data;
1047         unsigned int val, ESO_bytes;
1048
1049         spin_lock_irq(&trident->reg_lock);
1050
1051         // Initilize the channel and set channel Mode
1052         outb(0, TRID_REG(trident, LEGACY_DMAR15));
1053
1054         // Set DMA channel operation mode register
1055         outb(0x54, TRID_REG(trident, LEGACY_DMAR11));
1056
1057         // Set channel buffer Address, DMAR0 expects contiguous PCI memory area 
1058         voice->LBA = runtime->dma_addr;
1059         outl(voice->LBA, TRID_REG(trident, LEGACY_DMAR0));
1060         if (voice->memblk)
1061                 voice->LBA = voice->memblk->offset;
1062
1063         // set ESO
1064         ESO_bytes = snd_pcm_lib_buffer_bytes(substream) - 1;
1065         outb((ESO_bytes & 0x00ff0000) >> 16, TRID_REG(trident, LEGACY_DMAR6));
1066         outw((ESO_bytes & 0x0000ffff), TRID_REG(trident, LEGACY_DMAR4));
1067         ESO_bytes++;
1068
1069         // Set channel sample rate, 4.12 format
1070         val = (((unsigned int) 48000L << 12) + (runtime->rate/2)) / runtime->rate;
1071         outw(val, TRID_REG(trident, T4D_SBDELTA_DELTA_R));
1072
1073         // Set channel interrupt blk length
1074         if (snd_pcm_format_width(runtime->format) == 16) {
1075                 val = (unsigned short) ((ESO_bytes >> 1) - 1);
1076         } else {
1077                 val = (unsigned short) (ESO_bytes - 1);
1078         }
1079
1080         outl((val << 16) | val, TRID_REG(trident, T4D_SBBL_SBCL));
1081
1082         // Right now, set format and start to run captureing, 
1083         // continuous run loop enable.
1084         trident->bDMAStart = 0x19;      // 0001 1001b
1085
1086         if (snd_pcm_format_width(runtime->format) == 16)
1087                 trident->bDMAStart |= 0x80;
1088         if (snd_pcm_format_signed(runtime->format))
1089                 trident->bDMAStart |= 0x20;
1090         if (runtime->channels > 1)
1091                 trident->bDMAStart |= 0x40;
1092
1093         // Prepare capture intr channel
1094
1095         voice->Delta = snd_trident_convert_rate(runtime->rate);
1096         voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1097         voice->isync = 1;
1098         voice->isync_mark = runtime->period_size;
1099         voice->isync_max = runtime->buffer_size;
1100
1101         // Set voice parameters
1102         voice->CSO = 0;
1103         voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1104         voice->CTRL = snd_trident_control_mode(substream);
1105         voice->FMC = 3;
1106         voice->RVol = 0x7f;
1107         voice->CVol = 0x7f;
1108         voice->GVSel = 1;
1109         voice->Pan = 0x7f;              /* mute */
1110         voice->Vol = 0x3ff;             /* mute */
1111         voice->EC = 0;
1112         voice->Alpha = 0;
1113         voice->FMS = 0;
1114         voice->Attribute = 0;
1115
1116         snd_trident_write_voice_regs(trident, voice);
1117
1118         spin_unlock_irq(&trident->reg_lock);
1119         return 0;
1120 }
1121
1122 /*---------------------------------------------------------------------------
1123    snd_trident_si7018_capture_hw_params
1124   
1125    Description: Set the hardware parameters for the capture device.
1126   
1127    Parameters:  substream  - PCM substream class
1128                 hw_params  - hardware parameters
1129   
1130    Returns:     Error status
1131   
1132   ---------------------------------------------------------------------------*/
1133
1134 static int snd_trident_si7018_capture_hw_params(struct snd_pcm_substream *substream,
1135                                                 struct snd_pcm_hw_params *hw_params)
1136 {
1137         int err;
1138
1139         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
1140                 return err;
1141
1142         return snd_trident_allocate_evoice(substream, hw_params);
1143 }
1144
1145 /*---------------------------------------------------------------------------
1146    snd_trident_si7018_capture_hw_free
1147   
1148    Description: Release the hardware resources for the capture device.
1149   
1150    Parameters:  substream  - PCM substream class
1151   
1152    Returns:     Error status
1153   
1154   ---------------------------------------------------------------------------*/
1155
1156 static int snd_trident_si7018_capture_hw_free(struct snd_pcm_substream *substream)
1157 {
1158         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1159         struct snd_pcm_runtime *runtime = substream->runtime;
1160         struct snd_trident_voice *voice = runtime->private_data;
1161         struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
1162
1163         snd_pcm_lib_free_pages(substream);
1164         if (evoice != NULL) {
1165                 snd_trident_free_voice(trident, evoice);
1166                 voice->extra = NULL;
1167         }
1168         return 0;
1169 }
1170
1171 /*---------------------------------------------------------------------------
1172    snd_trident_si7018_capture_prepare
1173   
1174    Description: Prepare capture device for playback.
1175   
1176    Parameters:  substream  - PCM substream class
1177   
1178    Returns:     Error status
1179   
1180   ---------------------------------------------------------------------------*/
1181
1182 static int snd_trident_si7018_capture_prepare(struct snd_pcm_substream *substream)
1183 {
1184         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1185         struct snd_pcm_runtime *runtime = substream->runtime;
1186         struct snd_trident_voice *voice = runtime->private_data;
1187         struct snd_trident_voice *evoice = voice->extra;
1188
1189         spin_lock_irq(&trident->reg_lock);
1190
1191         voice->LBA = runtime->dma_addr;
1192         voice->Delta = snd_trident_convert_adc_rate(runtime->rate);
1193         voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1194
1195         // Set voice parameters
1196         voice->CSO = 0;
1197         voice->ESO = runtime->buffer_size - 1;          /* in samples */
1198         voice->CTRL = snd_trident_control_mode(substream);
1199         voice->FMC = 0;
1200         voice->RVol = 0;
1201         voice->CVol = 0;
1202         voice->GVSel = 1;
1203         voice->Pan = T4D_DEFAULT_PCM_PAN;
1204         voice->Vol = 0;
1205         voice->EC = 0;
1206         voice->Alpha = 0;
1207         voice->FMS = 0;
1208
1209         voice->Attribute = (2 << (30-16)) |
1210                            (2 << (26-16)) |
1211                            (2 << (24-16)) |
1212                            (1 << (23-16));
1213
1214         snd_trident_write_voice_regs(trident, voice);
1215
1216         if (evoice != NULL) {
1217                 evoice->Delta = snd_trident_convert_rate(runtime->rate);
1218                 evoice->spurious_threshold = voice->spurious_threshold;
1219                 evoice->LBA = voice->LBA;
1220                 evoice->CSO = 0;
1221                 evoice->ESO = (runtime->period_size * 2) + 20 - 1; /* in samples, 20 means correction */
1222                 evoice->CTRL = voice->CTRL;
1223                 evoice->FMC = 3;
1224                 evoice->GVSel = 0;
1225                 evoice->EC = 0;
1226                 evoice->Alpha = 0;
1227                 evoice->FMS = 0;
1228                 evoice->Vol = 0x3ff;                    /* mute */
1229                 evoice->RVol = evoice->CVol = 0x7f;     /* mute */
1230                 evoice->Pan = 0x7f;                     /* mute */
1231                 evoice->Attribute = 0;
1232                 snd_trident_write_voice_regs(trident, evoice);
1233                 evoice->isync2 = 1;
1234                 evoice->isync_mark = runtime->period_size;
1235                 evoice->ESO = (runtime->period_size * 2) - 1;
1236         }
1237         
1238         spin_unlock_irq(&trident->reg_lock);
1239         return 0;
1240 }
1241
1242 /*---------------------------------------------------------------------------
1243    snd_trident_foldback_prepare
1244   
1245    Description: Prepare foldback capture device for playback.
1246   
1247    Parameters:  substream  - PCM substream class
1248   
1249    Returns:     Error status
1250   
1251   ---------------------------------------------------------------------------*/
1252
1253 static int snd_trident_foldback_prepare(struct snd_pcm_substream *substream)
1254 {
1255         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1256         struct snd_pcm_runtime *runtime = substream->runtime;
1257         struct snd_trident_voice *voice = runtime->private_data;
1258         struct snd_trident_voice *evoice = voice->extra;
1259
1260         spin_lock_irq(&trident->reg_lock);
1261
1262         /* Set channel buffer Address */
1263         if (voice->memblk)
1264                 voice->LBA = voice->memblk->offset;
1265         else
1266                 voice->LBA = runtime->dma_addr;
1267
1268         /* set target ESO for channel */
1269         voice->ESO = runtime->buffer_size - 1;  /* in samples */
1270
1271         /* set sample rate */
1272         voice->Delta = 0x1000;
1273         voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1274
1275         voice->CSO = 0;
1276         voice->CTRL = snd_trident_control_mode(substream);
1277         voice->FMC = 3;
1278         voice->RVol = 0x7f;
1279         voice->CVol = 0x7f;
1280         voice->GVSel = 1;
1281         voice->Pan = 0x7f;      /* mute */
1282         voice->Vol = 0x3ff;     /* mute */
1283         voice->EC = 0;
1284         voice->Alpha = 0;
1285         voice->FMS = 0;
1286         voice->Attribute = 0;
1287
1288         /* set up capture channel */
1289         outb(((voice->number & 0x3f) | 0x80), TRID_REG(trident, T4D_RCI + voice->foldback_chan));
1290
1291         snd_trident_write_voice_regs(trident, voice);
1292
1293         if (evoice != NULL) {
1294                 evoice->Delta = voice->Delta;
1295                 evoice->spurious_threshold = voice->spurious_threshold;
1296                 evoice->LBA = voice->LBA;
1297                 evoice->CSO = 0;
1298                 evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1299                 evoice->CTRL = voice->CTRL;
1300                 evoice->FMC = 3;
1301                 evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1302                 evoice->EC = 0;
1303                 evoice->Alpha = 0;
1304                 evoice->FMS = 0;
1305                 evoice->Vol = 0x3ff;                    /* mute */
1306                 evoice->RVol = evoice->CVol = 0x7f;     /* mute */
1307                 evoice->Pan = 0x7f;                     /* mute */
1308                 evoice->Attribute = 0;
1309                 snd_trident_write_voice_regs(trident, evoice);
1310                 evoice->isync2 = 1;
1311                 evoice->isync_mark = runtime->period_size;
1312                 evoice->ESO = (runtime->period_size * 2) - 1;
1313         }
1314
1315         spin_unlock_irq(&trident->reg_lock);
1316         return 0;
1317 }
1318
1319 /*---------------------------------------------------------------------------
1320    snd_trident_spdif_hw_params
1321   
1322    Description: Set the hardware parameters for the spdif device.
1323   
1324    Parameters:  substream  - PCM substream class
1325                 hw_params  - hardware parameters
1326   
1327    Returns:     Error status
1328   
1329   ---------------------------------------------------------------------------*/
1330
1331 static int snd_trident_spdif_hw_params(struct snd_pcm_substream *substream,
1332                                        struct snd_pcm_hw_params *hw_params)
1333 {
1334         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1335         unsigned int old_bits = 0, change = 0;
1336         int err;
1337
1338         err = snd_trident_allocate_pcm_mem(substream, hw_params);
1339         if (err < 0)
1340                 return err;
1341
1342         if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
1343                 err = snd_trident_allocate_evoice(substream, hw_params);
1344                 if (err < 0)
1345                         return err;
1346         }
1347
1348         /* prepare SPDIF channel */
1349         spin_lock_irq(&trident->reg_lock);
1350         old_bits = trident->spdif_pcm_bits;
1351         if (old_bits & IEC958_AES0_PROFESSIONAL)
1352                 trident->spdif_pcm_bits &= ~IEC958_AES0_PRO_FS;
1353         else
1354                 trident->spdif_pcm_bits &= ~(IEC958_AES3_CON_FS << 24);
1355         if (params_rate(hw_params) >= 48000) {
1356                 trident->spdif_pcm_ctrl = 0x3c; // 48000 Hz
1357                 trident->spdif_pcm_bits |=
1358                         trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1359                                 IEC958_AES0_PRO_FS_48000 :
1360                                 (IEC958_AES3_CON_FS_48000 << 24);
1361         }
1362         else if (params_rate(hw_params) >= 44100) {
1363                 trident->spdif_pcm_ctrl = 0x3e; // 44100 Hz
1364                 trident->spdif_pcm_bits |=
1365                         trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1366                                 IEC958_AES0_PRO_FS_44100 :
1367                                 (IEC958_AES3_CON_FS_44100 << 24);
1368         }
1369         else {
1370                 trident->spdif_pcm_ctrl = 0x3d; // 32000 Hz
1371                 trident->spdif_pcm_bits |=
1372                         trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
1373                                 IEC958_AES0_PRO_FS_32000 :
1374                                 (IEC958_AES3_CON_FS_32000 << 24);
1375         }
1376         change = old_bits != trident->spdif_pcm_bits;
1377         spin_unlock_irq(&trident->reg_lock);
1378
1379         if (change)
1380                 snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE, &trident->spdif_pcm_ctl->id);
1381
1382         return 0;
1383 }
1384
1385 /*---------------------------------------------------------------------------
1386    snd_trident_spdif_prepare
1387   
1388    Description: Prepare SPDIF device for playback.
1389   
1390    Parameters:  substream  - PCM substream class
1391   
1392    Returns:     Error status
1393   
1394   ---------------------------------------------------------------------------*/
1395
1396 static int snd_trident_spdif_prepare(struct snd_pcm_substream *substream)
1397 {
1398         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1399         struct snd_pcm_runtime *runtime = substream->runtime;
1400         struct snd_trident_voice *voice = runtime->private_data;
1401         struct snd_trident_voice *evoice = voice->extra;
1402         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
1403         unsigned int RESO, LBAO;
1404         unsigned int temp;
1405
1406         spin_lock_irq(&trident->reg_lock);
1407
1408         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
1409
1410                 /* set delta (rate) value */
1411                 voice->Delta = snd_trident_convert_rate(runtime->rate);
1412                 voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
1413
1414                 /* set Loop Back Address */
1415                 LBAO = runtime->dma_addr;
1416                 if (voice->memblk)
1417                         voice->LBA = voice->memblk->offset;
1418                 else
1419                         voice->LBA = LBAO;
1420
1421                 voice->isync = 1;
1422                 voice->isync3 = 1;
1423                 voice->isync_mark = runtime->period_size;
1424                 voice->isync_max = runtime->buffer_size;
1425
1426                 /* set target ESO for channel */
1427                 RESO = runtime->buffer_size - 1;
1428                 voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
1429
1430                 /* set ctrl mode */
1431                 voice->CTRL = snd_trident_control_mode(substream);
1432
1433                 voice->FMC = 3;
1434                 voice->RVol = 0x7f;
1435                 voice->CVol = 0x7f;
1436                 voice->GVSel = 1;
1437                 voice->Pan = 0x7f;
1438                 voice->Vol = 0x3ff;
1439                 voice->EC = 0;
1440                 voice->CSO = 0;
1441                 voice->Alpha = 0;
1442                 voice->FMS = 0;
1443                 voice->Attribute = 0;
1444
1445                 /* prepare surrogate IRQ channel */
1446                 snd_trident_write_voice_regs(trident, voice);
1447
1448                 outw((RESO & 0xffff), TRID_REG(trident, NX_SPESO));
1449                 outb((RESO >> 16), TRID_REG(trident, NX_SPESO + 2));
1450                 outl((LBAO & 0xfffffffc), TRID_REG(trident, NX_SPLBA));
1451                 outw((voice->CSO & 0xffff), TRID_REG(trident, NX_SPCTRL_SPCSO));
1452                 outb((voice->CSO >> 16), TRID_REG(trident, NX_SPCTRL_SPCSO + 2));
1453
1454                 /* set SPDIF setting */
1455                 outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1456                 outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
1457
1458         } else {        /* SiS */
1459         
1460                 /* set delta (rate) value */
1461                 voice->Delta = 0x800;
1462                 voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
1463
1464                 /* set Loop Begin Address */
1465                 if (voice->memblk)
1466                         voice->LBA = voice->memblk->offset;
1467                 else
1468                         voice->LBA = runtime->dma_addr;
1469
1470                 voice->CSO = 0;
1471                 voice->ESO = runtime->buffer_size - 1;  /* in samples */
1472                 voice->CTRL = snd_trident_control_mode(substream);
1473                 voice->FMC = 3;
1474                 voice->GVSel = 1;
1475                 voice->EC = 0;
1476                 voice->Alpha = 0;
1477                 voice->FMS = 0;
1478                 voice->Vol = mix->vol;
1479                 voice->RVol = mix->rvol;
1480                 voice->CVol = mix->cvol;
1481                 voice->Pan = mix->pan;
1482                 voice->Attribute = (1<<(30-16))|(7<<(26-16))|
1483                                    (0<<(24-16))|(0<<(19-16));
1484
1485                 snd_trident_write_voice_regs(trident, voice);
1486
1487                 if (evoice != NULL) {
1488                         evoice->Delta = voice->Delta;
1489                         evoice->spurious_threshold = voice->spurious_threshold;
1490                         evoice->LBA = voice->LBA;
1491                         evoice->CSO = 0;
1492                         evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
1493                         evoice->CTRL = voice->CTRL;
1494                         evoice->FMC = 3;
1495                         evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
1496                         evoice->EC = 0;
1497                         evoice->Alpha = 0;
1498                         evoice->FMS = 0;
1499                         evoice->Vol = 0x3ff;                    /* mute */
1500                         evoice->RVol = evoice->CVol = 0x7f;     /* mute */
1501                         evoice->Pan = 0x7f;                     /* mute */
1502                         evoice->Attribute = 0;
1503                         snd_trident_write_voice_regs(trident, evoice);
1504                         evoice->isync2 = 1;
1505                         evoice->isync_mark = runtime->period_size;
1506                         evoice->ESO = (runtime->period_size * 2) - 1;
1507                 }
1508
1509                 outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
1510                 temp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
1511                 temp &= ~(1<<19);
1512                 outl(temp, TRID_REG(trident, T4D_LFO_GC_CIR));
1513                 temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1514                 temp |= SPDIF_EN;
1515                 outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
1516         }
1517
1518         spin_unlock_irq(&trident->reg_lock);
1519
1520         return 0;
1521 }
1522
1523 /*---------------------------------------------------------------------------
1524    snd_trident_trigger
1525   
1526    Description: Start/stop devices
1527   
1528    Parameters:  substream  - PCM substream class
1529                 cmd     - trigger command (STOP, GO)
1530   
1531    Returns:     Error status
1532   
1533   ---------------------------------------------------------------------------*/
1534
1535 static int snd_trident_trigger(struct snd_pcm_substream *substream,
1536                                int cmd)
1537                                     
1538 {
1539         struct snd_trident *trident = snd_pcm_substream_chip(substream);
1540         struct list_head *pos;
1541         struct snd_pcm_substream *s;
1542         unsigned int what, whati, capture_flag, spdif_flag;
1543         struct snd_trident_voice *voice, *evoice;
1544         unsigned int val, go;
1545
1546         switch (cmd) {
1547         case SNDRV_PCM_TRIGGER_START:
1548         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1549         case SNDRV_PCM_TRIGGER_RESUME:
1550                 go = 1;
1551                 break;
1552         case SNDRV_PCM_TRIGGER_STOP:
1553         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1554         case SNDRV_PCM_TRIGGER_SUSPEND:
1555                 go = 0;
1556                 break;
1557         default:
1558                 return -EINVAL;
1559         }
1560         what = whati = capture_flag = spdif_flag = 0;
1561         spin_lock(&trident->reg_lock);
1562         val = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
1563         snd_pcm_group_for_each(pos, substream) {
1564                 s = snd_pcm_group_substream_entry(pos);
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    Paramters:   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    Paramters:   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    Paramters:   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    Paramters:   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 static int snd_trident_spdif_control_info(struct snd_kcontrol *kcontrol,
2320                                           struct snd_ctl_elem_info *uinfo)
2321 {
2322         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2323         uinfo->count = 1;
2324         uinfo->value.integer.min = 0;
2325         uinfo->value.integer.max = 1;
2326         return 0;
2327 }
2328
2329 static int snd_trident_spdif_control_get(struct snd_kcontrol *kcontrol,
2330                                          struct snd_ctl_elem_value *ucontrol)
2331 {
2332         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2333         unsigned char val;
2334
2335         spin_lock_irq(&trident->reg_lock);
2336         val = trident->spdif_ctrl;
2337         ucontrol->value.integer.value[0] = val == kcontrol->private_value;
2338         spin_unlock_irq(&trident->reg_lock);
2339         return 0;
2340 }
2341
2342 static int snd_trident_spdif_control_put(struct snd_kcontrol *kcontrol,
2343                                          struct snd_ctl_elem_value *ucontrol)
2344 {
2345         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2346         unsigned char val;
2347         int change;
2348
2349         val = ucontrol->value.integer.value[0] ? (unsigned char) kcontrol->private_value : 0x00;
2350         spin_lock_irq(&trident->reg_lock);
2351         /* S/PDIF C Channel bits 0-31 : 48khz, SCMS disabled */
2352         change = trident->spdif_ctrl != val;
2353         trident->spdif_ctrl = val;
2354         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2355                 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0) {
2356                         outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2357                         outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
2358                 }
2359         } else {
2360                 if (trident->spdif == NULL) {
2361                         unsigned int temp;
2362                         outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2363                         temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & ~SPDIF_EN;
2364                         if (val)
2365                                 temp |= SPDIF_EN;
2366                         outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
2367                 }
2368         }
2369         spin_unlock_irq(&trident->reg_lock);
2370         return change;
2371 }
2372
2373 static struct snd_kcontrol_new snd_trident_spdif_control __devinitdata =
2374 {
2375         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2376         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2377         .info =         snd_trident_spdif_control_info,
2378         .get =          snd_trident_spdif_control_get,
2379         .put =          snd_trident_spdif_control_put,
2380         .private_value = 0x28,
2381 };
2382
2383 /*---------------------------------------------------------------------------
2384     snd_trident_spdif_default
2385
2386     Description: put/get the S/PDIF default settings
2387   ---------------------------------------------------------------------------*/
2388
2389 static int snd_trident_spdif_default_info(struct snd_kcontrol *kcontrol,
2390                                           struct snd_ctl_elem_info *uinfo)
2391 {
2392         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2393         uinfo->count = 1;
2394         return 0;
2395 }
2396
2397 static int snd_trident_spdif_default_get(struct snd_kcontrol *kcontrol,
2398                                          struct snd_ctl_elem_value *ucontrol)
2399 {
2400         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2401
2402         spin_lock_irq(&trident->reg_lock);
2403         ucontrol->value.iec958.status[0] = (trident->spdif_bits >> 0) & 0xff;
2404         ucontrol->value.iec958.status[1] = (trident->spdif_bits >> 8) & 0xff;
2405         ucontrol->value.iec958.status[2] = (trident->spdif_bits >> 16) & 0xff;
2406         ucontrol->value.iec958.status[3] = (trident->spdif_bits >> 24) & 0xff;
2407         spin_unlock_irq(&trident->reg_lock);
2408         return 0;
2409 }
2410
2411 static int snd_trident_spdif_default_put(struct snd_kcontrol *kcontrol,
2412                                          struct snd_ctl_elem_value *ucontrol)
2413 {
2414         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2415         unsigned int val;
2416         int change;
2417
2418         val = (ucontrol->value.iec958.status[0] << 0) |
2419               (ucontrol->value.iec958.status[1] << 8) |
2420               (ucontrol->value.iec958.status[2] << 16) |
2421               (ucontrol->value.iec958.status[3] << 24);
2422         spin_lock_irq(&trident->reg_lock);
2423         change = trident->spdif_bits != val;
2424         trident->spdif_bits = val;
2425         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2426                 if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0)
2427                         outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
2428         } else {
2429                 if (trident->spdif == NULL)
2430                         outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2431         }
2432         spin_unlock_irq(&trident->reg_lock);
2433         return change;
2434 }
2435
2436 static struct snd_kcontrol_new snd_trident_spdif_default __devinitdata =
2437 {
2438         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
2439         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2440         .info =         snd_trident_spdif_default_info,
2441         .get =          snd_trident_spdif_default_get,
2442         .put =          snd_trident_spdif_default_put
2443 };
2444
2445 /*---------------------------------------------------------------------------
2446     snd_trident_spdif_mask
2447
2448     Description: put/get the S/PDIF mask
2449   ---------------------------------------------------------------------------*/
2450
2451 static int snd_trident_spdif_mask_info(struct snd_kcontrol *kcontrol,
2452                                        struct snd_ctl_elem_info *uinfo)
2453 {
2454         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2455         uinfo->count = 1;
2456         return 0;
2457 }
2458
2459 static int snd_trident_spdif_mask_get(struct snd_kcontrol *kcontrol,
2460                                       struct snd_ctl_elem_value *ucontrol)
2461 {
2462         ucontrol->value.iec958.status[0] = 0xff;
2463         ucontrol->value.iec958.status[1] = 0xff;
2464         ucontrol->value.iec958.status[2] = 0xff;
2465         ucontrol->value.iec958.status[3] = 0xff;
2466         return 0;
2467 }
2468
2469 static struct snd_kcontrol_new snd_trident_spdif_mask __devinitdata =
2470 {
2471         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
2472         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
2473         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
2474         .info =         snd_trident_spdif_mask_info,
2475         .get =          snd_trident_spdif_mask_get,
2476 };
2477
2478 /*---------------------------------------------------------------------------
2479     snd_trident_spdif_stream
2480
2481     Description: put/get the S/PDIF stream settings
2482   ---------------------------------------------------------------------------*/
2483
2484 static int snd_trident_spdif_stream_info(struct snd_kcontrol *kcontrol,
2485                                          struct snd_ctl_elem_info *uinfo)
2486 {
2487         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2488         uinfo->count = 1;
2489         return 0;
2490 }
2491
2492 static int snd_trident_spdif_stream_get(struct snd_kcontrol *kcontrol,
2493                                         struct snd_ctl_elem_value *ucontrol)
2494 {
2495         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2496
2497         spin_lock_irq(&trident->reg_lock);
2498         ucontrol->value.iec958.status[0] = (trident->spdif_pcm_bits >> 0) & 0xff;
2499         ucontrol->value.iec958.status[1] = (trident->spdif_pcm_bits >> 8) & 0xff;
2500         ucontrol->value.iec958.status[2] = (trident->spdif_pcm_bits >> 16) & 0xff;
2501         ucontrol->value.iec958.status[3] = (trident->spdif_pcm_bits >> 24) & 0xff;
2502         spin_unlock_irq(&trident->reg_lock);
2503         return 0;
2504 }
2505
2506 static int snd_trident_spdif_stream_put(struct snd_kcontrol *kcontrol,
2507                                         struct snd_ctl_elem_value *ucontrol)
2508 {
2509         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2510         unsigned int val;
2511         int change;
2512
2513         val = (ucontrol->value.iec958.status[0] << 0) |
2514               (ucontrol->value.iec958.status[1] << 8) |
2515               (ucontrol->value.iec958.status[2] << 16) |
2516               (ucontrol->value.iec958.status[3] << 24);
2517         spin_lock_irq(&trident->reg_lock);
2518         change = trident->spdif_pcm_bits != val;
2519         trident->spdif_pcm_bits = val;
2520         if (trident->spdif != NULL) {
2521                 if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
2522                         outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
2523                 } else {
2524                         outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
2525                 }
2526         }
2527         spin_unlock_irq(&trident->reg_lock);
2528         return change;
2529 }
2530
2531 static struct snd_kcontrol_new snd_trident_spdif_stream __devinitdata =
2532 {
2533         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2534         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
2535         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
2536         .info =         snd_trident_spdif_stream_info,
2537         .get =          snd_trident_spdif_stream_get,
2538         .put =          snd_trident_spdif_stream_put
2539 };
2540
2541 /*---------------------------------------------------------------------------
2542     snd_trident_ac97_control
2543
2544     Description: enable/disable rear path for ac97
2545   ---------------------------------------------------------------------------*/
2546
2547 static int snd_trident_ac97_control_info(struct snd_kcontrol *kcontrol,
2548                                          struct snd_ctl_elem_info *uinfo)
2549 {
2550         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2551         uinfo->count = 1;
2552         uinfo->value.integer.min = 0;
2553         uinfo->value.integer.max = 1;
2554         return 0;
2555 }
2556
2557 static int snd_trident_ac97_control_get(struct snd_kcontrol *kcontrol,
2558                                         struct snd_ctl_elem_value *ucontrol)
2559 {
2560         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2561         unsigned char val;
2562
2563         spin_lock_irq(&trident->reg_lock);
2564         val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2565         ucontrol->value.integer.value[0] = (val & (1 << kcontrol->private_value)) ? 1 : 0;
2566         spin_unlock_irq(&trident->reg_lock);
2567         return 0;
2568 }
2569
2570 static int snd_trident_ac97_control_put(struct snd_kcontrol *kcontrol,
2571                                         struct snd_ctl_elem_value *ucontrol)
2572 {
2573         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2574         unsigned char val;
2575         int change = 0;
2576
2577         spin_lock_irq(&trident->reg_lock);
2578         val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2579         val &= ~(1 << kcontrol->private_value);
2580         if (ucontrol->value.integer.value[0])
2581                 val |= 1 << kcontrol->private_value;
2582         change = val != trident->ac97_ctrl;
2583         trident->ac97_ctrl = val;
2584         outl(trident->ac97_ctrl = val, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
2585         spin_unlock_irq(&trident->reg_lock);
2586         return change;
2587 }
2588
2589 static struct snd_kcontrol_new snd_trident_ac97_rear_control __devinitdata =
2590 {
2591         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2592         .name =         "Rear Path",
2593         .info =         snd_trident_ac97_control_info,
2594         .get =          snd_trident_ac97_control_get,
2595         .put =          snd_trident_ac97_control_put,
2596         .private_value = 4,
2597 };
2598
2599 /*---------------------------------------------------------------------------
2600     snd_trident_vol_control
2601
2602     Description: wave & music volume control
2603   ---------------------------------------------------------------------------*/
2604
2605 static int snd_trident_vol_control_info(struct snd_kcontrol *kcontrol,
2606                                         struct snd_ctl_elem_info *uinfo)
2607 {
2608         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2609         uinfo->count = 2;
2610         uinfo->value.integer.min = 0;
2611         uinfo->value.integer.max = 255;
2612         return 0;
2613 }
2614
2615 static int snd_trident_vol_control_get(struct snd_kcontrol *kcontrol,
2616                                        struct snd_ctl_elem_value *ucontrol)
2617 {
2618         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2619         unsigned int val;
2620
2621         val = trident->musicvol_wavevol;
2622         ucontrol->value.integer.value[0] = 255 - ((val >> kcontrol->private_value) & 0xff);
2623         ucontrol->value.integer.value[1] = 255 - ((val >> (kcontrol->private_value + 8)) & 0xff);
2624         return 0;
2625 }
2626
2627 static int snd_trident_vol_control_put(struct snd_kcontrol *kcontrol,
2628                                        struct snd_ctl_elem_value *ucontrol)
2629 {
2630         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2631         unsigned int val;
2632         int change = 0;
2633
2634         spin_lock_irq(&trident->reg_lock);
2635         val = trident->musicvol_wavevol;
2636         val &= ~(0xffff << kcontrol->private_value);
2637         val |= ((255 - (ucontrol->value.integer.value[0] & 0xff)) |
2638                 ((255 - (ucontrol->value.integer.value[1] & 0xff)) << 8)) << kcontrol->private_value;
2639         change = val != trident->musicvol_wavevol;
2640         outl(trident->musicvol_wavevol = val, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
2641         spin_unlock_irq(&trident->reg_lock);
2642         return change;
2643 }
2644
2645 static struct snd_kcontrol_new snd_trident_vol_music_control __devinitdata =
2646 {
2647         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2648         .name =         "Music Playback Volume",
2649         .info =         snd_trident_vol_control_info,
2650         .get =          snd_trident_vol_control_get,
2651         .put =          snd_trident_vol_control_put,
2652         .private_value = 16,
2653 };
2654
2655 static struct snd_kcontrol_new snd_trident_vol_wave_control __devinitdata =
2656 {
2657         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2658         .name =         "Wave Playback Volume",
2659         .info =         snd_trident_vol_control_info,
2660         .get =          snd_trident_vol_control_get,
2661         .put =          snd_trident_vol_control_put,
2662         .private_value = 0,
2663 };
2664
2665 /*---------------------------------------------------------------------------
2666     snd_trident_pcm_vol_control
2667
2668     Description: PCM front volume control
2669   ---------------------------------------------------------------------------*/
2670
2671 static int snd_trident_pcm_vol_control_info(struct snd_kcontrol *kcontrol,
2672                                             struct snd_ctl_elem_info *uinfo)
2673 {
2674         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2675
2676         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2677         uinfo->count = 1;
2678         uinfo->value.integer.min = 0;
2679         uinfo->value.integer.max = 255;
2680         if (trident->device == TRIDENT_DEVICE_ID_SI7018)
2681                 uinfo->value.integer.max = 1023;
2682         return 0;
2683 }
2684
2685 static int snd_trident_pcm_vol_control_get(struct snd_kcontrol *kcontrol,
2686                                            struct snd_ctl_elem_value *ucontrol)
2687 {
2688         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2689         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2690
2691         if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2692                 ucontrol->value.integer.value[0] = 1023 - mix->vol;
2693         } else {
2694                 ucontrol->value.integer.value[0] = 255 - (mix->vol>>2);
2695         }
2696         return 0;
2697 }
2698
2699 static int snd_trident_pcm_vol_control_put(struct snd_kcontrol *kcontrol,
2700                                            struct snd_ctl_elem_value *ucontrol)
2701 {
2702         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2703         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2704         unsigned int val;
2705         int change = 0;
2706
2707         if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2708                 val = 1023 - (ucontrol->value.integer.value[0] & 1023);
2709         } else {
2710                 val = (255 - (ucontrol->value.integer.value[0] & 255)) << 2;
2711         }
2712         spin_lock_irq(&trident->reg_lock);
2713         change = val != mix->vol;
2714         mix->vol = val;
2715         if (mix->voice != NULL)
2716                 snd_trident_write_vol_reg(trident, mix->voice, val);
2717         spin_unlock_irq(&trident->reg_lock);
2718         return change;
2719 }
2720
2721 static struct snd_kcontrol_new snd_trident_pcm_vol_control __devinitdata =
2722 {
2723         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2724         .name =         "PCM Front Playback Volume",
2725         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2726         .count =        32,
2727         .info =         snd_trident_pcm_vol_control_info,
2728         .get =          snd_trident_pcm_vol_control_get,
2729         .put =          snd_trident_pcm_vol_control_put,
2730 };
2731
2732 /*---------------------------------------------------------------------------
2733     snd_trident_pcm_pan_control
2734
2735     Description: PCM front pan control
2736   ---------------------------------------------------------------------------*/
2737
2738 static int snd_trident_pcm_pan_control_info(struct snd_kcontrol *kcontrol,
2739                                             struct snd_ctl_elem_info *uinfo)
2740 {
2741         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2742         uinfo->count = 1;
2743         uinfo->value.integer.min = 0;
2744         uinfo->value.integer.max = 127;
2745         return 0;
2746 }
2747
2748 static int snd_trident_pcm_pan_control_get(struct snd_kcontrol *kcontrol,
2749                                            struct snd_ctl_elem_value *ucontrol)
2750 {
2751         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2752         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2753
2754         ucontrol->value.integer.value[0] = mix->pan;
2755         if (ucontrol->value.integer.value[0] & 0x40) {
2756                 ucontrol->value.integer.value[0] = (0x3f - (ucontrol->value.integer.value[0] & 0x3f));
2757         } else {
2758                 ucontrol->value.integer.value[0] |= 0x40;
2759         }
2760         return 0;
2761 }
2762
2763 static int snd_trident_pcm_pan_control_put(struct snd_kcontrol *kcontrol,
2764                                            struct snd_ctl_elem_value *ucontrol)
2765 {
2766         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2767         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2768         unsigned char val;
2769         int change = 0;
2770
2771         if (ucontrol->value.integer.value[0] & 0x40)
2772                 val = ucontrol->value.integer.value[0] & 0x3f;
2773         else
2774                 val = (0x3f - (ucontrol->value.integer.value[0] & 0x3f)) | 0x40;
2775         spin_lock_irq(&trident->reg_lock);
2776         change = val != mix->pan;
2777         mix->pan = val;
2778         if (mix->voice != NULL)
2779                 snd_trident_write_pan_reg(trident, mix->voice, val);
2780         spin_unlock_irq(&trident->reg_lock);
2781         return change;
2782 }
2783
2784 static struct snd_kcontrol_new snd_trident_pcm_pan_control __devinitdata =
2785 {
2786         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2787         .name =         "PCM Pan Playback Control",
2788         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2789         .count =        32,
2790         .info =         snd_trident_pcm_pan_control_info,
2791         .get =          snd_trident_pcm_pan_control_get,
2792         .put =          snd_trident_pcm_pan_control_put,
2793 };
2794
2795 /*---------------------------------------------------------------------------
2796     snd_trident_pcm_rvol_control
2797
2798     Description: PCM reverb volume control
2799   ---------------------------------------------------------------------------*/
2800
2801 static int snd_trident_pcm_rvol_control_info(struct snd_kcontrol *kcontrol,
2802                                              struct snd_ctl_elem_info *uinfo)
2803 {
2804         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2805         uinfo->count = 1;
2806         uinfo->value.integer.min = 0;
2807         uinfo->value.integer.max = 127;
2808         return 0;
2809 }
2810
2811 static int snd_trident_pcm_rvol_control_get(struct snd_kcontrol *kcontrol,
2812                                             struct snd_ctl_elem_value *ucontrol)
2813 {
2814         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2815         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2816
2817         ucontrol->value.integer.value[0] = 127 - mix->rvol;
2818         return 0;
2819 }
2820
2821 static int snd_trident_pcm_rvol_control_put(struct snd_kcontrol *kcontrol,
2822                                             struct snd_ctl_elem_value *ucontrol)
2823 {
2824         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2825         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2826         unsigned short val;
2827         int change = 0;
2828
2829         val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2830         spin_lock_irq(&trident->reg_lock);
2831         change = val != mix->rvol;
2832         mix->rvol = val;
2833         if (mix->voice != NULL)
2834                 snd_trident_write_rvol_reg(trident, mix->voice, val);
2835         spin_unlock_irq(&trident->reg_lock);
2836         return change;
2837 }
2838
2839 static struct snd_kcontrol_new snd_trident_pcm_rvol_control __devinitdata =
2840 {
2841         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2842         .name =         "PCM Reverb Playback Volume",
2843         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2844         .count =        32,
2845         .info =         snd_trident_pcm_rvol_control_info,
2846         .get =          snd_trident_pcm_rvol_control_get,
2847         .put =          snd_trident_pcm_rvol_control_put,
2848 };
2849
2850 /*---------------------------------------------------------------------------
2851     snd_trident_pcm_cvol_control
2852
2853     Description: PCM chorus volume control
2854   ---------------------------------------------------------------------------*/
2855
2856 static int snd_trident_pcm_cvol_control_info(struct snd_kcontrol *kcontrol,
2857                                              struct snd_ctl_elem_info *uinfo)
2858 {
2859         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2860         uinfo->count = 1;
2861         uinfo->value.integer.min = 0;
2862         uinfo->value.integer.max = 127;
2863         return 0;
2864 }
2865
2866 static int snd_trident_pcm_cvol_control_get(struct snd_kcontrol *kcontrol,
2867                                             struct snd_ctl_elem_value *ucontrol)
2868 {
2869         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2870         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2871
2872         ucontrol->value.integer.value[0] = 127 - mix->cvol;
2873         return 0;
2874 }
2875
2876 static int snd_trident_pcm_cvol_control_put(struct snd_kcontrol *kcontrol,
2877                                             struct snd_ctl_elem_value *ucontrol)
2878 {
2879         struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
2880         struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
2881         unsigned short val;
2882         int change = 0;
2883
2884         val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
2885         spin_lock_irq(&trident->reg_lock);
2886         change = val != mix->cvol;
2887         mix->cvol = val;
2888         if (mix->voice != NULL)
2889                 snd_trident_write_cvol_reg(trident, mix->voice, val);
2890         spin_unlock_irq(&trident->reg_lock);
2891         return change;
2892 }
2893
2894 static struct snd_kcontrol_new snd_trident_pcm_cvol_control __devinitdata =
2895 {
2896         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
2897         .name =         "PCM Chorus Playback Volume",
2898         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
2899         .count =        32,
2900         .info =         snd_trident_pcm_cvol_control_info,
2901         .get =          snd_trident_pcm_cvol_control_get,
2902         .put =          snd_trident_pcm_cvol_control_put,
2903 };
2904
2905 static void snd_trident_notify_pcm_change1(struct snd_card *card,
2906                                            struct snd_kcontrol *kctl,
2907                                            int num, int activate)
2908 {
2909         struct snd_ctl_elem_id id;
2910
2911         if (! kctl)
2912                 return;
2913         if (activate)
2914                 kctl->vd[num].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2915         else
2916                 kctl->vd[num].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2917         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
2918                        SNDRV_CTL_EVENT_MASK_INFO,
2919                        snd_ctl_build_ioff(&id, kctl, num));
2920 }
2921
2922 static void snd_trident_notify_pcm_change(struct snd_trident *trident,
2923                                           struct snd_trident_pcm_mixer *tmix,
2924                                           int num, int activate)
2925 {
2926         snd_trident_notify_pcm_change1(trident->card, trident->ctl_vol, num, activate);
2927         snd_trident_notify_pcm_change1(trident->card, trident->ctl_pan, num, activate);
2928         snd_trident_notify_pcm_change1(trident->card, trident->ctl_rvol, num, activate);
2929         snd_trident_notify_pcm_change1(trident->card, trident->ctl_cvol, num, activate);
2930 }
2931
2932 static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
2933                                        struct snd_trident_voice *voice,
2934                                        struct snd_pcm_substream *substream)
2935 {
2936         struct snd_trident_pcm_mixer *tmix;
2937
2938         snd_assert(trident != NULL && voice != NULL && substream != NULL, return -EINVAL);
2939         tmix = &trident->pcm_mixer[substream->number];
2940         tmix->voice = voice;
2941         tmix->vol = T4D_DEFAULT_PCM_VOL;
2942         tmix->pan = T4D_DEFAULT_PCM_PAN;
2943         tmix->rvol = T4D_DEFAULT_PCM_RVOL;
2944         tmix->cvol = T4D_DEFAULT_PCM_CVOL;
2945         snd_trident_notify_pcm_change(trident, tmix, substream->number, 1);
2946         return 0;
2947 }
2948
2949 static int snd_trident_pcm_mixer_free(struct snd_trident *trident, struct snd_trident_voice *voice, struct snd_pcm_substream *substream)
2950 {
2951         struct snd_trident_pcm_mixer *tmix;
2952
2953         snd_assert(trident != NULL && substream != NULL, return -EINVAL);
2954         tmix = &trident->pcm_mixer[substream->number];
2955         tmix->voice = NULL;
2956         snd_trident_notify_pcm_change(trident, tmix, substream->number, 0);
2957         return 0;
2958 }
2959
2960 /*---------------------------------------------------------------------------
2961    snd_trident_mixer
2962   
2963    Description: This routine registers the 4DWave device for mixer support.
2964                 
2965    Paramters:   trident - pointer to target device class for 4DWave.
2966
2967    Returns:     None
2968   
2969   ---------------------------------------------------------------------------*/
2970
2971 static int __devinit snd_trident_mixer(struct snd_trident * trident, int pcm_spdif_device)
2972 {
2973         struct snd_ac97_template _ac97;
2974         struct snd_card *card = trident->card;
2975         struct snd_kcontrol *kctl;
2976         struct snd_ctl_elem_value *uctl;
2977         int idx, err, retries = 2;
2978         static struct snd_ac97_bus_ops ops = {
2979                 .write = snd_trident_codec_write,
2980                 .read = snd_trident_codec_read,
2981         };
2982
2983         uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
2984         if (!uctl)
2985                 return -ENOMEM;
2986
2987         if ((err = snd_ac97_bus(trident->card, 0, &ops, NULL, &trident->ac97_bus)) < 0)
2988                 goto __out;
2989
2990         memset(&_ac97, 0, sizeof(_ac97));
2991         _ac97.private_data = trident;
2992         trident->ac97_detect = 1;
2993
2994       __again:
2995         if ((err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97)) < 0) {
2996                 if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
2997                         if ((err = snd_trident_sis_reset(trident)) < 0)
2998                                 goto __out;
2999                         if (retries-- > 0)
3000                                 goto __again;
3001                         err = -EIO;
3002                 }
3003                 goto __out;
3004         }
3005         
3006         /* secondary codec? */
3007         if (trident->device == TRIDENT_DEVICE_ID_SI7018 &&
3008             (inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0) {
3009                 _ac97.num = 1;
3010                 err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97_sec);
3011                 if (err < 0)
3012                         snd_printk(KERN_ERR "SI7018: the secondary codec - invalid access\n");
3013 #if 0   // only for my testing purpose --jk
3014                 {
3015                         struct snd_ac97 *mc97;
3016                         err = snd_ac97_modem(trident->card, &_ac97, &mc97);
3017                         if (err < 0)
3018                                 snd_printk(KERN_ERR "snd_ac97_modem returned error %i\n", err);
3019                 }
3020 #endif
3021         }
3022         
3023         trident->ac97_detect = 0;
3024
3025         if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
3026                 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_wave_control, trident))) < 0)
3027                         goto __out;
3028                 kctl->put(kctl, uctl);
3029                 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_music_control, trident))) < 0)
3030                         goto __out;
3031                 kctl->put(kctl, uctl);
3032                 outl(trident->musicvol_wavevol = 0x00000000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3033         } else {
3034                 outl(trident->musicvol_wavevol = 0xffff0000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3035         }
3036
3037         for (idx = 0; idx < 32; idx++) {
3038                 struct snd_trident_pcm_mixer *tmix;
3039                 
3040                 tmix = &trident->pcm_mixer[idx];
3041                 tmix->voice = NULL;
3042         }
3043         if ((trident->ctl_vol = snd_ctl_new1(&snd_trident_pcm_vol_control, trident)) == NULL)
3044                 goto __nomem;
3045         if ((err = snd_ctl_add(card, trident->ctl_vol)))
3046                 goto __out;
3047                 
3048         if ((trident->ctl_pan = snd_ctl_new1(&snd_trident_pcm_pan_control, trident)) == NULL)
3049                 goto __nomem;
3050         if ((err = snd_ctl_add(card, trident->ctl_pan)))
3051                 goto __out;
3052
3053         if ((trident->ctl_rvol = snd_ctl_new1(&snd_trident_pcm_rvol_control, trident)) == NULL)
3054                 goto __nomem;
3055         if ((err = snd_ctl_add(card, trident->ctl_rvol)))
3056                 goto __out;
3057
3058         if ((trident->ctl_cvol = snd_ctl_new1(&snd_trident_pcm_cvol_control, trident)) == NULL)
3059                 goto __nomem;
3060         if ((err = snd_ctl_add(card, trident->ctl_cvol)))
3061                 goto __out;
3062
3063         if (trident->device == TRIDENT_DEVICE_ID_NX) {
3064                 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_ac97_rear_control, trident))) < 0)
3065                         goto __out;
3066                 kctl->put(kctl, uctl);
3067         }
3068         if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018) {
3069
3070                 kctl = snd_ctl_new1(&snd_trident_spdif_control, trident);
3071                 if (kctl == NULL) {
3072                         err = -ENOMEM;
3073                         goto __out;
3074                 }
3075                 if (trident->ac97->ext_id & AC97_EI_SPDIF)
3076                         kctl->id.index++;
3077                 if (trident->ac97_sec && (trident->ac97_sec->ext_id & AC97_EI_SPDIF))
3078                         kctl->id.index++;
3079                 idx = kctl->id.index;
3080                 if ((err = snd_ctl_add(card, kctl)) < 0)
3081                         goto __out;
3082                 kctl->put(kctl, uctl);
3083
3084                 kctl = snd_ctl_new1(&snd_trident_spdif_default, trident);
3085                 if (kctl == NULL) {
3086                         err = -ENOMEM;
3087                         goto __out;
3088                 }
3089                 kctl->id.index = idx;
3090                 kctl->id.device = pcm_spdif_device;
3091                 if ((err = snd_ctl_add(card, kctl)) < 0)
3092                         goto __out;
3093
3094                 kctl = snd_ctl_new1(&snd_trident_spdif_mask, trident);
3095                 if (kctl == NULL) {
3096                         err = -ENOMEM;
3097                         goto __out;
3098                 }
3099                 kctl->id.index = idx;
3100                 kctl->id.device = pcm_spdif_device;
3101                 if ((err = snd_ctl_add(card, kctl)) < 0)
3102                         goto __out;
3103
3104                 kctl = snd_ctl_new1(&snd_trident_spdif_stream, trident);
3105                 if (kctl == NULL) {
3106                         err = -ENOMEM;
3107                         goto __out;
3108                 }
3109                 kctl->id.index = idx;
3110                 kctl->id.device = pcm_spdif_device;
3111                 if ((err = snd_ctl_add(card, kctl)) < 0)
3112                         goto __out;
3113                 trident->spdif_pcm_ctl = kctl;
3114         }
3115
3116         err = 0;
3117         goto __out;
3118
3119  __nomem:
3120         err = -ENOMEM;
3121
3122  __out:
3123         kfree(uctl);
3124
3125         return err;
3126 }
3127
3128 /*
3129  * gameport interface
3130  */
3131
3132 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
3133
3134 static unsigned char snd_trident_gameport_read(struct gameport *gameport)
3135 {
3136         struct snd_trident *chip = gameport_get_port_data(gameport);
3137
3138         snd_assert(chip, return 0);
3139         return inb(TRID_REG(chip, GAMEPORT_LEGACY));
3140 }
3141
3142 static void snd_trident_gameport_trigger(struct gameport *gameport)
3143 {
3144         struct snd_trident *chip = gameport_get_port_data(gameport);
3145
3146         snd_assert(chip, return);
3147         outb(0xff, TRID_REG(chip, GAMEPORT_LEGACY));
3148 }
3149
3150 static int snd_trident_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
3151 {
3152         struct snd_trident *chip = gameport_get_port_data(gameport);
3153         int i;
3154
3155         snd_assert(chip, return 0);
3156
3157         *buttons = (~inb(TRID_REG(chip, GAMEPORT_LEGACY)) >> 4) & 0xf;
3158
3159         for (i = 0; i < 4; i++) {
3160                 axes[i] = inw(TRID_REG(chip, GAMEPORT_AXES + i * 2));
3161                 if (axes[i] == 0xffff) axes[i] = -1;
3162         }
3163         
3164         return 0;
3165 }
3166
3167 static int snd_trident_gameport_open(struct gameport *gameport, int mode)
3168 {
3169         struct snd_trident *chip = gameport_get_port_data(gameport);
3170
3171         snd_assert(chip, return 0);
3172
3173         switch (mode) {
3174                 case GAMEPORT_MODE_COOKED:
3175                         outb(GAMEPORT_MODE_ADC, TRID_REG(chip, GAMEPORT_GCR));
3176                         msleep(20);
3177                         return 0;
3178                 case GAMEPORT_MODE_RAW:
3179                         outb(0, TRID_REG(chip, GAMEPORT_GCR));
3180                         return 0;
3181                 default:
3182                         return -1;
3183         }
3184 }
3185
3186 int __devinit snd_trident_create_gameport(struct snd_trident *chip)
3187 {
3188         struct gameport *gp;
3189
3190         chip->gameport = gp = gameport_allocate_port();
3191         if (!gp) {
3192                 printk(KERN_ERR "trident: cannot allocate memory for gameport\n");
3193                 return -ENOMEM;
3194         }
3195
3196         gameport_set_name(gp, "Trident 4DWave");
3197         gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
3198         gameport_set_dev_parent(gp, &chip->pci->dev);
3199
3200         gameport_set_port_data(gp, chip);
3201         gp->fuzz = 64;
3202         gp->read = snd_trident_gameport_read;
3203         gp->trigger = snd_trident_gameport_trigger;
3204         gp->cooked_read = snd_trident_gameport_cooked_read;
3205         gp->open = snd_trident_gameport_open;
3206
3207         gameport_register_port(gp);
3208
3209         return 0;
3210 }
3211
3212 static inline void snd_trident_free_gameport(struct snd_trident *chip)
3213 {
3214         if (chip->gameport) {
3215                 gameport_unregister_port(chip->gameport);
3216                 chip->gameport = NULL;
3217         }
3218 }
3219 #else
3220 int __devinit snd_trident_create_gameport(struct snd_trident *chip) { return -ENOSYS; }
3221 static inline void snd_trident_free_gameport(struct snd_trident *chip) { }
3222 #endif /* CONFIG_GAMEPORT */
3223
3224 /*
3225  * delay for 1 tick
3226  */
3227 static inline void do_delay(struct snd_trident *chip)
3228 {
3229         schedule_timeout_uninterruptible(1);
3230 }
3231
3232 /*
3233  *  SiS reset routine
3234  */
3235
3236 static int snd_trident_sis_reset(struct snd_trident *trident)
3237 {
3238         unsigned long end_time;
3239         unsigned int i;
3240         int r;
3241
3242         r = trident->in_suspend ? 0 : 2;        /* count of retries */
3243       __si7018_retry:
3244         pci_write_config_byte(trident->pci, 0x46, 0x04);        /* SOFTWARE RESET */
3245         udelay(100);
3246         pci_write_config_byte(trident->pci, 0x46, 0x00);
3247         udelay(100);
3248         /* disable AC97 GPIO interrupt */
3249         outb(0x00, TRID_REG(trident, SI_AC97_GPIO));
3250         /* initialize serial interface, force cold reset */
3251         i = PCMOUT|SURROUT|CENTEROUT|LFEOUT|SECONDARY_ID|COLD_RESET;
3252         outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3253         udelay(1000);
3254         /* remove cold reset */
3255         i &= ~COLD_RESET;
3256         outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3257         udelay(2000);
3258         /* wait, until the codec is ready */
3259         end_time = (jiffies + (HZ * 3) / 4) + 1;
3260         do {
3261                 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0)
3262                         goto __si7018_ok;
3263                 do_delay(trident);
3264         } while (time_after_eq(end_time, jiffies));
3265         snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)));
3266         if (r-- > 0) {
3267                 end_time = jiffies + HZ;
3268                 do {
3269                         do_delay(trident);
3270                 } while (time_after_eq(end_time, jiffies));
3271                 goto __si7018_retry;
3272         }
3273       __si7018_ok:
3274         /* wait for the second codec */
3275         do {
3276                 if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_SECONDARY_READY) != 0)
3277                         break;
3278                 do_delay(trident);
3279         } while (time_after_eq(end_time, jiffies));
3280         /* enable 64 channel mode */
3281         outl(BANK_B_EN, TRID_REG(trident, T4D_LFO_GC_CIR));
3282         return 0;
3283 }
3284
3285 /*  
3286  *  /proc interface
3287  */
3288
3289 static void snd_trident_proc_read(struct snd_info_entry *entry, 
3290                                   struct snd_info_buffer *buffer)
3291 {
3292         struct snd_trident *trident = entry->private_data;
3293         char *s;
3294
3295         switch (trident->device) {
3296         case TRIDENT_DEVICE_ID_SI7018:
3297                 s = "SiS 7018 Audio";
3298                 break;
3299         case TRIDENT_DEVICE_ID_DX:
3300                 s = "Trident 4DWave PCI DX";
3301                 break;
3302         case TRIDENT_DEVICE_ID_NX:
3303                 s = "Trident 4DWave PCI NX";
3304                 break;
3305         default:
3306                 s = "???";
3307         }
3308         snd_iprintf(buffer, "%s\n\n", s);
3309         snd_iprintf(buffer, "Spurious IRQs    : %d\n", trident->spurious_irq_count);
3310         snd_iprintf(buffer, "Spurious IRQ dlta: %d\n", trident->spurious_irq_max_delta);
3311         if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018)
3312                 snd_iprintf(buffer, "IEC958 Mixer Out : %s\n", trident->spdif_ctrl == 0x28 ? "on" : "off");
3313         if (trident->device == TRIDENT_DEVICE_ID_NX) {
3314                 snd_iprintf(buffer, "Rear Speakers    : %s\n", trident->ac97_ctrl & 0x00000010 ? "on" : "off");
3315                 if (trident->tlb.entries) {
3316                         snd_iprintf(buffer,"\nVirtual Memory\n");
3317                         snd_iprintf(buffer, "Memory Maximum : %d\n", trident->tlb.memhdr->size);
3318                         snd_iprintf(buffer, "Memory Used    : %d\n", trident->tlb.memhdr->used);
3319                         snd_iprintf(buffer, "Memory Free    : %d\n", snd_util_mem_avail(trident->tlb.memhdr));
3320                 }
3321         }
3322 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
3323         snd_iprintf(buffer,"\nWavetable Synth\n");
3324         snd_iprintf(buffer, "Memory Maximum : %d\n", trident->synth.max_size);
3325         snd_iprintf(buffer, "Memory Used    : %d\n", trident->synth.current_size);
3326         snd_iprintf(buffer, "Memory Free    : %d\n", (trident->synth.max_size-trident->synth.current_size));
3327 #endif
3328 }
3329
3330 static void __devinit snd_trident_proc_init(struct snd_trident * trident)
3331 {
3332         struct snd_info_entry *entry;
3333         const char *s = "trident";
3334         
3335         if (trident->device == TRIDENT_DEVICE_ID_SI7018)
3336                 s = "sis7018";
3337         if (! snd_card_proc_new(trident->card, s, &entry))
3338                 snd_info_set_text_ops(entry, trident, 1024, snd_trident_proc_read);
3339 }
3340
3341 static int snd_trident_dev_free(struct snd_device *device)
3342 {
3343         struct snd_trident *trident = device->device_data;
3344         return snd_trident_free(trident);
3345 }
3346
3347 /*---------------------------------------------------------------------------
3348    snd_trident_tlb_alloc
3349   
3350    Description: Allocate and set up the TLB page table on 4D NX.
3351                 Each entry has 4 bytes (physical PCI address).
3352                 
3353    Paramters:   trident - pointer to target device class for 4DWave.
3354
3355    Returns:     0 or negative error code
3356   
3357   ---------------------------------------------------------------------------*/
3358
3359 static int __devinit snd_trident_tlb_alloc(struct snd_trident *trident)
3360 {
3361         int i;
3362
3363         /* TLB array must be aligned to 16kB !!! so we allocate
3364            32kB region and correct offset when necessary */
3365
3366         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3367                                 2 * SNDRV_TRIDENT_MAX_PAGES * 4, &trident->tlb.buffer) < 0) {
3368                 snd_printk(KERN_ERR "trident: unable to allocate TLB buffer\n");
3369                 return -ENOMEM;
3370         }
3371         trident->tlb.entries = (unsigned int*)(((unsigned long)trident->tlb.buffer.area + SNDRV_TRIDENT_MAX_PAGES * 4 - 1) & ~(SNDRV_TRIDENT_MAX_PAGES * 4 - 1));
3372         trident->tlb.entries_dmaaddr = (trident->tlb.buffer.addr + SNDRV_TRIDENT_MAX_PAGES * 4 - 1) & ~(SNDRV_TRIDENT_MAX_PAGES * 4 - 1);
3373         /* allocate shadow TLB page table (virtual addresses) */
3374         trident->tlb.shadow_entries = vmalloc(SNDRV_TRIDENT_MAX_PAGES*sizeof(unsigned long));
3375         if (trident->tlb.shadow_entries == NULL) {
3376                 snd_printk(KERN_ERR "trident: unable to allocate shadow TLB entries\n");
3377                 return -ENOMEM;
3378         }
3379         /* allocate and setup silent page and initialise TLB entries */
3380         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
3381                                 SNDRV_TRIDENT_PAGE_SIZE, &trident->tlb.silent_page) < 0) {
3382                 snd_printk(KERN_ERR "trident: unable to allocate silent page\n");
3383                 return -ENOMEM;
3384         }
3385         memset(trident->tlb.silent_page.area, 0, SNDRV_TRIDENT_PAGE_SIZE);
3386         for (i = 0; i < SNDRV_TRIDENT_MAX_PAGES; i++) {
3387                 trident->tlb.entries[i] = cpu_to_le32(trident->tlb.silent_page.addr & ~(SNDRV_TRIDENT_PAGE_SIZE-1));
3388                 trident->tlb.shadow_entries[i] = (unsigned long)trident->tlb.silent_page.area;
3389         }
3390
3391         /* use emu memory block manager code to manage tlb page allocation */
3392         trident->tlb.memhdr = snd_util_memhdr_new(SNDRV_TRIDENT_PAGE_SIZE * SNDRV_TRIDENT_MAX_PAGES);
3393         if (trident->tlb.memhdr == NULL)
3394                 return -ENOMEM;
3395
3396         trident->tlb.memhdr->block_extra_size = sizeof(struct snd_trident_memblk_arg);
3397         return 0;
3398 }
3399
3400 /*
3401  * initialize 4D DX chip
3402  */
3403
3404 static void snd_trident_stop_all_voices(struct snd_trident *trident)
3405 {
3406         outl(0xffffffff, TRID_REG(trident, T4D_STOP_A));
3407         outl(0xffffffff, TRID_REG(trident, T4D_STOP_B));
3408         outl(0, TRID_REG(trident, T4D_AINTEN_A));
3409         outl(0, TRID_REG(trident, T4D_AINTEN_B));
3410 }
3411
3412 static int snd_trident_4d_dx_init(struct snd_trident *trident)
3413 {
3414         struct pci_dev *pci = trident->pci;
3415         unsigned long end_time;
3416
3417         /* reset the legacy configuration and whole audio/wavetable block */
3418         pci_write_config_dword(pci, 0x40, 0);   /* DDMA */
3419         pci_write_config_byte(pci, 0x44, 0);    /* ports */
3420         pci_write_config_byte(pci, 0x45, 0);    /* Legacy DMA */
3421         pci_write_config_byte(pci, 0x46, 4); /* reset */
3422         udelay(100);
3423         pci_write_config_byte(pci, 0x46, 0); /* release reset */
3424         udelay(100);
3425         
3426         /* warm reset of the AC'97 codec */
3427         outl(0x00000001, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3428         udelay(100);
3429         outl(0x00000000, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3430         /* DAC on, disable SB IRQ and try to force ADC valid signal */
3431         trident->ac97_ctrl = 0x0000004a;
3432         outl(trident->ac97_ctrl, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
3433         /* wait, until the codec is ready */
3434         end_time = (jiffies + (HZ * 3) / 4) + 1;
3435         do {
3436                 if ((inl(TRID_REG(trident, DX_ACR2_AC97_COM_STAT)) & 0x0010) != 0)
3437                         goto __dx_ok;
3438                 do_delay(trident);
3439         } while (time_after_eq(end_time, jiffies));
3440         snd_printk(KERN_ERR "AC'97 codec ready error\n");
3441         return -EIO;
3442
3443  __dx_ok:
3444         snd_trident_stop_all_voices(trident);
3445
3446         return 0;
3447 }
3448
3449 /*
3450  * initialize 4D NX chip
3451  */
3452 static int snd_trident_4d_nx_init(struct snd_trident *trident)
3453 {
3454         struct pci_dev *pci = trident->pci;
3455         unsigned long end_time;
3456
3457         /* reset the legacy configuration and whole audio/wavetable block */
3458         pci_write_config_dword(pci, 0x40, 0);   /* DDMA */
3459         pci_write_config_byte(pci, 0x44, 0);    /* ports */
3460         pci_write_config_byte(pci, 0x45, 0);    /* Legacy DMA */
3461
3462         pci_write_config_byte(pci, 0x46, 1); /* reset */
3463         udelay(100);
3464         pci_write_config_byte(pci, 0x46, 0); /* release reset */
3465         udelay(100);
3466
3467         /* warm reset of the AC'97 codec */
3468         outl(0x00000001, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3469         udelay(100);
3470         outl(0x00000000, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3471         /* wait, until the codec is ready */
3472         end_time = (jiffies + (HZ * 3) / 4) + 1;
3473         do {
3474                 if ((inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)) & 0x0008) != 0)
3475                         goto __nx_ok;
3476                 do_delay(trident);
3477         } while (time_after_eq(end_time, jiffies));
3478         snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)));
3479         return -EIO;
3480
3481  __nx_ok:
3482         /* DAC on */
3483         trident->ac97_ctrl = 0x00000002;
3484         outl(trident->ac97_ctrl, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
3485         /* disable SB IRQ */
3486         outl(NX_SB_IRQ_DISABLE, TRID_REG(trident, T4D_MISCINT));
3487
3488         snd_trident_stop_all_voices(trident);
3489
3490         if (trident->tlb.entries != NULL) {
3491                 unsigned int i;
3492                 /* enable virtual addressing via TLB */
3493                 i = trident->tlb.entries_dmaaddr;
3494                 i |= 0x00000001;
3495                 outl(i, TRID_REG(trident, NX_TLBC));
3496         } else {
3497                 outl(0, TRID_REG(trident, NX_TLBC));
3498         }
3499         /* initialize S/PDIF */
3500         outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
3501         outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3502
3503         return 0;
3504 }
3505
3506 /*
3507  * initialize sis7018 chip
3508  */
3509 static int snd_trident_sis_init(struct snd_trident *trident)
3510 {
3511         int err;
3512
3513         if ((err = snd_trident_sis_reset(trident)) < 0)
3514                 return err;
3515
3516         snd_trident_stop_all_voices(trident);
3517
3518         /* initialize S/PDIF */
3519         outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
3520
3521         return 0;
3522 }
3523
3524 /*---------------------------------------------------------------------------
3525    snd_trident_create
3526   
3527    Description: This routine will create the device specific class for
3528                 the 4DWave card. It will also perform basic initialization.
3529                 
3530    Paramters:   card  - which card to create
3531                 pci   - interface to PCI bus resource info
3532                 dma1ptr - playback dma buffer
3533                 dma2ptr - capture dma buffer
3534                 irqptr  -  interrupt resource info
3535
3536    Returns:     4DWave device class private data
3537   
3538   ---------------------------------------------------------------------------*/
3539
3540 int __devinit snd_trident_create(struct snd_card *card,
3541                        struct pci_dev *pci,
3542                        int pcm_streams,
3543                        int pcm_spdif_device,
3544                        int max_wavetable_size,
3545                        struct snd_trident ** rtrident)
3546 {
3547         struct snd_trident *trident;
3548         int i, err;
3549         struct snd_trident_voice *voice;
3550         struct snd_trident_pcm_mixer *tmix;
3551         static struct snd_device_ops ops = {
3552                 .dev_free =     snd_trident_dev_free,
3553         };
3554
3555         *rtrident = NULL;
3556
3557         /* enable PCI device */
3558         if ((err = pci_enable_device(pci)) < 0)
3559                 return err;
3560         /* check, if we can restrict PCI DMA transfers to 30 bits */
3561         if (pci_set_dma_mask(pci, 0x3fffffff) < 0 ||
3562             pci_set_consistent_dma_mask(pci, 0x3fffffff) < 0) {
3563                 snd_printk(KERN_ERR "architecture does not support 30bit PCI busmaster DMA\n");
3564                 pci_disable_device(pci);
3565                 return -ENXIO;
3566         }
3567         
3568         trident = kzalloc(sizeof(*trident), GFP_KERNEL);
3569         if (trident == NULL) {
3570                 pci_disable_device(pci);
3571                 return -ENOMEM;
3572         }
3573         trident->device = (pci->vendor << 16) | pci->device;
3574         trident->card = card;
3575         trident->pci = pci;
3576         spin_lock_init(&trident->reg_lock);
3577         spin_lock_init(&trident->event_lock);
3578         spin_lock_init(&trident->voice_alloc);
3579         if (pcm_streams < 1)
3580                 pcm_streams = 1;
3581         if (pcm_streams > 32)
3582                 pcm_streams = 32;
3583         trident->ChanPCM = pcm_streams;
3584         if (max_wavetable_size < 0 )
3585                 max_wavetable_size = 0;
3586         trident->synth.max_size = max_wavetable_size * 1024;
3587         trident->irq = -1;
3588
3589         trident->midi_port = TRID_REG(trident, T4D_MPU401_BASE);
3590         pci_set_master(pci);
3591
3592         if ((err = pci_request_regions(pci, "Trident Audio")) < 0) {
3593                 kfree(trident);
3594                 pci_disable_device(pci);
3595                 return err;
3596         }
3597         trident->port = pci_resource_start(pci, 0);
3598
3599         if (request_irq(pci->irq, snd_trident_interrupt, SA_INTERRUPT|SA_SHIRQ,
3600                         "Trident Audio", trident)) {
3601                 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
3602                 snd_trident_free(trident);
3603                 return -EBUSY;
3604         }
3605         trident->irq = pci->irq;
3606
3607         /* allocate 16k-aligned TLB for NX cards */
3608         trident->tlb.entries = NULL;
3609         trident->tlb.buffer.area = NULL;
3610         if (trident->device == TRIDENT_DEVICE_ID_NX) {
3611                 if ((err = snd_trident_tlb_alloc(trident)) < 0) {
3612                         snd_trident_free(trident);
3613                         return err;
3614                 }
3615         }
3616
3617         trident->spdif_bits = trident->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
3618
3619         /* initialize chip */
3620         switch (trident->device) {
3621         case TRIDENT_DEVICE_ID_DX:
3622                 err = snd_trident_4d_dx_init(trident);
3623                 break;
3624         case TRIDENT_DEVICE_ID_NX:
3625                 err = snd_trident_4d_nx_init(trident);
3626                 break;
3627         case TRIDENT_DEVICE_ID_SI7018:
3628                 err = snd_trident_sis_init(trident);
3629                 break;
3630         default:
3631                 snd_BUG();
3632                 break;
3633         }
3634         if (err < 0) {
3635                 snd_trident_free(trident);
3636                 return err;
3637         }
3638
3639         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, trident, &ops)) < 0) {
3640                 snd_trident_free(trident);
3641                 return err;
3642         }
3643
3644         if ((err = snd_trident_mixer(trident, pcm_spdif_device)) < 0)
3645                 return err;
3646         
3647         /* initialise synth voices */
3648         for (i = 0; i < 64; i++) {
3649                 voice = &trident->synth.voices[i];
3650                 voice->number = i;
3651                 voice->trident = trident;
3652         }
3653         /* initialize pcm mixer entries */
3654         for (i = 0; i < 32; i++) {
3655                 tmix = &trident->pcm_mixer[i];
3656                 tmix->vol = T4D_DEFAULT_PCM_VOL;
3657                 tmix->pan = T4D_DEFAULT_PCM_PAN;
3658                 tmix->rvol = T4D_DEFAULT_PCM_RVOL;
3659                 tmix->cvol = T4D_DEFAULT_PCM_CVOL;
3660         }
3661
3662         snd_trident_enable_eso(trident);
3663
3664         
3665         snd_card_set_pm_callback(card, snd_trident_suspend, snd_trident_resume, trident);
3666         snd_trident_proc_init(trident);
3667         snd_card_set_dev(card, &pci->dev);
3668         *rtrident = trident;
3669         return 0;
3670 }
3671
3672 /*---------------------------------------------------------------------------
3673    snd_trident_free
3674   
3675    Description: This routine will free the device specific class for
3676                 the 4DWave card. 
3677                 
3678    Paramters:   trident  - device specific private data for 4DWave card
3679
3680    Returns:     None.
3681   
3682   ---------------------------------------------------------------------------*/
3683
3684 static int snd_trident_free(struct snd_trident *trident)
3685 {
3686         snd_trident_free_gameport(trident);
3687         snd_trident_disable_eso(trident);
3688         // Disable S/PDIF out
3689         if (trident->device == TRIDENT_DEVICE_ID_NX)
3690                 outb(0x00, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
3691         else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
3692                 outl(0, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
3693         }
3694         if (trident->tlb.buffer.area) {
3695                 outl(0, TRID_REG(trident, NX_TLBC));
3696                 if (trident->tlb.memhdr)
3697                         snd_util_memhdr_free(trident->tlb.memhdr);
3698                 if (trident->tlb.silent_page.area)
3699                         snd_dma_free_pages(&trident->tlb.silent_page);
3700                 vfree(trident->tlb.shadow_entries);
3701                 snd_dma_free_pages(&trident->tlb.buffer);
3702         }
3703         if (trident->irq >= 0)
3704                 free_irq(trident->irq, trident);
3705         pci_release_regions(trident->pci);
3706         pci_disable_device(trident->pci);
3707         kfree(trident);
3708         return 0;
3709 }
3710
3711 /*---------------------------------------------------------------------------
3712    snd_trident_interrupt
3713   
3714    Description: ISR for Trident 4DWave device
3715                 
3716    Paramters:   trident  - device specific private data for 4DWave card
3717
3718    Problems:    It seems that Trident chips generates interrupts more than
3719                 one time in special cases. The spurious interrupts are
3720                 detected via sample timer (T4D_STIMER) and computing
3721                 corresponding delta value. The limits are detected with
3722                 the method try & fail so it is possible that it won't
3723                 work on all computers. [jaroslav]
3724
3725    Returns:     None.
3726   
3727   ---------------------------------------------------------------------------*/
3728
3729 static irqreturn_t snd_trident_interrupt(int irq, void *dev_id, struct pt_regs *regs)
3730 {
3731         struct snd_trident *trident = dev_id;
3732         unsigned int audio_int, chn_int, stimer, channel, mask, tmp;
3733         int delta;
3734         struct snd_trident_voice *voice;
3735
3736         audio_int = inl(TRID_REG(trident, T4D_MISCINT));
3737         if ((audio_int & (ADDRESS_IRQ|MPU401_IRQ)) == 0)
3738                 return IRQ_NONE;
3739         if (audio_int & ADDRESS_IRQ) {
3740                 // get interrupt status for all channels
3741                 spin_lock(&trident->reg_lock);
3742                 stimer = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
3743                 chn_int = inl(TRID_REG(trident, T4D_AINT_A));
3744                 if (chn_int == 0)
3745                         goto __skip1;
3746                 outl(chn_int, TRID_REG(trident, T4D_AINT_A));   /* ack */
3747               __skip1:
3748                 chn_int = inl(TRID_REG(trident, T4D_AINT_B));
3749                 if (chn_int == 0)
3750                         goto __skip2;
3751                 for (channel = 63; channel >= 32; channel--) {
3752                         mask = 1 << (channel&0x1f);
3753                         if ((chn_int & mask) == 0)
3754                                 continue;
3755                         voice = &trident->synth.voices[channel];
3756                         if (!voice->pcm || voice->substream == NULL) {
3757                                 outl(mask, TRID_REG(trident, T4D_STOP_B));
3758                                 continue;
3759                         }
3760                         delta = (int)stimer - (int)voice->stimer;
3761                         if (delta < 0)
3762                                 delta = -delta;
3763                         if ((unsigned int)delta < voice->spurious_threshold) {
3764                                 /* do some statistics here */
3765                                 trident->spurious_irq_count++;
3766                                 if (trident->spurious_irq_max_delta < (unsigned int)delta)
3767                                         trident->spurious_irq_max_delta = delta;
3768                                 continue;
3769                         }
3770                         voice->stimer = stimer;
3771                         if (voice->isync) {
3772                                 if (!voice->isync3) {
3773                                         tmp = inw(TRID_REG(trident, T4D_SBBL_SBCL));
3774                                         if (trident->bDMAStart & 0x40)
3775                                                 tmp >>= 1;
3776                                         if (tmp > 0)
3777                                                 tmp = voice->isync_max - tmp;
3778                                 } else {
3779                                         tmp = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
3780                                 }
3781                                 if (tmp < voice->isync_mark) {
3782                                         if (tmp > 0x10)
3783                                                 tmp = voice->isync_ESO - 7;
3784                                         else
3785                                                 tmp = voice->isync_ESO + 2;
3786                                         /* update ESO for IRQ voice to preserve sync */
3787                                         snd_trident_stop_voice(trident, voice->number);
3788                                         snd_trident_write_eso_reg(trident, voice, tmp);
3789                                         snd_trident_start_voice(trident, voice->number);
3790                                 }
3791                         } else if (voice->isync2) {
3792                                 voice->isync2 = 0;
3793                                 /* write original ESO and update CSO for IRQ voice to preserve sync */
3794                                 snd_trident_stop_voice(trident, voice->number);
3795                                 snd_trident_write_cso_reg(trident, voice, voice->isync_mark);
3796                                 snd_trident_write_eso_reg(trident, voice, voice->ESO);
3797                                 snd_trident_start_voice(trident, voice->number);
3798                         }
3799 #if 0
3800                         if (voice->extra) {
3801                                 /* update CSO for extra voice to preserve sync */
3802                                 snd_trident_stop_voice(trident, voice->extra->number);
3803                                 snd_trident_write_cso_reg(trident, voice->extra, 0);
3804                                 snd_trident_start_voice(trident, voice->extra->number);
3805                         }
3806 #endif
3807                         spin_unlock(&trident->reg_lock);
3808                         snd_pcm_period_elapsed(voice->substream);
3809                         spin_lock(&trident->reg_lock);
3810                 }
3811                 outl(chn_int, TRID_REG(trident, T4D_AINT_B));   /* ack */
3812               __skip2:
3813                 spin_unlock(&trident->reg_lock);
3814         }
3815         if (audio_int & MPU401_IRQ) {
3816                 if (trident->rmidi) {
3817                         snd_mpu401_uart_interrupt(irq, trident->rmidi->private_data, regs);
3818                 } else {
3819                         inb(TRID_REG(trident, T4D_MPUR0));
3820                 }
3821         }
3822         // outl((ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW), TRID_REG(trident, T4D_MISCINT));
3823         return IRQ_HANDLED;
3824 }
3825
3826 /*---------------------------------------------------------------------------
3827    snd_trident_attach_synthesizer
3828   
3829    Description: Attach synthesizer hooks
3830                 
3831    Paramters:   trident  - device specific private data for 4DWave card
3832
3833    Returns:     None.
3834   
3835   ---------------------------------------------------------------------------*/
3836 int snd_trident_attach_synthesizer(struct snd_trident *trident)
3837 {       
3838 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
3839         if (snd_seq_device_new(trident->card, 1, SNDRV_SEQ_DEV_ID_TRIDENT,
3840                                sizeof(struct snd_trident *), &trident->seq_dev) >= 0) {
3841                 strcpy(trident->seq_dev->name, "4DWave");
3842                 *(struct snd_trident **)SNDRV_SEQ_DEVICE_ARGPTR(trident->seq_dev) = trident;
3843         }
3844 #endif
3845         return 0;
3846 }
3847
3848 struct snd_trident_voice *snd_trident_alloc_voice(struct snd_trident * trident, int type, int client, int port)
3849 {
3850         struct snd_trident_voice *pvoice;
3851         unsigned long flags;
3852         int idx;
3853
3854         spin_lock_irqsave(&trident->voice_alloc, flags);
3855         if (type == SNDRV_TRIDENT_VOICE_TYPE_PCM) {
3856                 idx = snd_trident_allocate_pcm_channel(trident);
3857                 if(idx < 0) {
3858                         spin_unlock_irqrestore(&trident->voice_alloc, flags);
3859                         return NULL;
3860                 }
3861                 pvoice = &trident->synth.voices[idx];
3862                 pvoice->use = 1;
3863                 pvoice->pcm = 1;
3864                 pvoice->capture = 0;
3865                 pvoice->spdif = 0;
3866                 pvoice->memblk = NULL;
3867                 pvoice->substream = NULL;
3868                 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3869                 return pvoice;
3870         }
3871         if (type == SNDRV_TRIDENT_VOICE_TYPE_SYNTH) {
3872                 idx = snd_trident_allocate_synth_channel(trident);
3873                 if(idx < 0) {
3874                         spin_unlock_irqrestore(&trident->voice_alloc, flags);
3875                         return NULL;
3876                 }
3877                 pvoice = &trident->synth.voices[idx];
3878                 pvoice->use = 1;
3879                 pvoice->synth = 1;
3880                 pvoice->client = client;
3881                 pvoice->port = port;
3882                 pvoice->memblk = NULL;
3883                 spin_unlock_irqrestore(&trident->voice_alloc, flags);
3884                 return pvoice;
3885         }
3886         if (type == SNDRV_TRIDENT_VOICE_TYPE_MIDI) {
3887         }
3888         spin_unlock_irqrestore(&trident->voice_alloc, flags);
3889         return NULL;
3890 }
3891
3892 void snd_trident_free_voice(struct snd_trident * trident, struct snd_trident_voice *voice)
3893 {
3894         unsigned long flags;
3895         void (*private_free)(struct snd_trident_voice *);
3896         void *private_data;
3897
3898         if (voice == NULL || !voice->use)
3899                 return;
3900         snd_trident_clear_voices(trident, voice->number, voice->number);
3901         spin_lock_irqsave(&trident->voice_alloc, flags);
3902         private_free = voice->private_free;
3903         private_data = voice->private_data;
3904         voice->private_free = NULL;
3905         voice->private_data = NULL;
3906         if (voice->pcm)
3907                 snd_trident_free_pcm_channel(trident, voice->number);
3908         if (voice->synth)
3909                 snd_trident_free_synth_channel(trident, voice->number);
3910         voice->use = voice->pcm = voice->synth = voice->midi = 0;
3911         voice->capture = voice->spdif = 0;
3912         voice->sample_ops = NULL;
3913         voice->substream = NULL;
3914         voice->extra = NULL;
3915         spin_unlock_irqrestore(&trident->voice_alloc, flags);
3916         if (private_free)
3917                 private_free(voice);
3918 }
3919
3920 static void snd_trident_clear_voices(struct snd_trident * trident, unsigned short v_min, unsigned short v_max)
3921 {
3922         unsigned int i, val, mask[2] = { 0, 0 };
3923
3924         snd_assert(v_min <= 63, return);
3925         snd_assert(v_max <= 63, return);
3926         for (i = v_min; i <= v_max; i++)
3927                 mask[i >> 5] |= 1 << (i & 0x1f);
3928         if (mask[0]) {
3929                 outl(mask[0], TRID_REG(trident, T4D_STOP_A));
3930                 val = inl(TRID_REG(trident, T4D_AINTEN_A));
3931                 outl(val & ~mask[0], TRID_REG(trident, T4D_AINTEN_A));
3932         }
3933         if (mask[1]) {
3934                 outl(mask[1], TRID_REG(trident, T4D_STOP_B));
3935                 val = inl(TRID_REG(trident, T4D_AINTEN_B));
3936                 outl(val & ~mask[1], TRID_REG(trident, T4D_AINTEN_B));
3937         }
3938 }
3939
3940 #ifdef CONFIG_PM
3941 static int snd_trident_suspend(struct snd_card *card, pm_message_t state)
3942 {
3943         struct snd_trident *trident = card->pm_private_data;
3944
3945         trident->in_suspend = 1;
3946         snd_pcm_suspend_all(trident->pcm);
3947         if (trident->foldback)
3948                 snd_pcm_suspend_all(trident->foldback);
3949         if (trident->spdif)
3950                 snd_pcm_suspend_all(trident->spdif);
3951
3952         snd_ac97_suspend(trident->ac97);
3953         if (trident->ac97_sec)
3954                 snd_ac97_suspend(trident->ac97_sec);
3955
3956         switch (trident->device) {
3957         case TRIDENT_DEVICE_ID_DX:
3958         case TRIDENT_DEVICE_ID_NX:
3959                 break;                  /* TODO */
3960         case TRIDENT_DEVICE_ID_SI7018:
3961                 break;
3962         }
3963         pci_disable_device(trident->pci);
3964         return 0;
3965 }
3966
3967 static int snd_trident_resume(struct snd_card *card)
3968 {
3969         struct snd_trident *trident = card->pm_private_data;
3970
3971         pci_enable_device(trident->pci);
3972         if (pci_set_dma_mask(trident->pci, 0x3fffffff) < 0 ||
3973             pci_set_consistent_dma_mask(trident->pci, 0x3fffffff) < 0)
3974                 snd_printk(KERN_WARNING "trident: can't set the proper DMA mask\n");
3975         pci_set_master(trident->pci); /* to be sure */
3976
3977         switch (trident->device) {
3978         case TRIDENT_DEVICE_ID_DX:
3979                 snd_trident_4d_dx_init(trident);
3980                 break;
3981         case TRIDENT_DEVICE_ID_NX:
3982                 snd_trident_4d_nx_init(trident);
3983                 break;
3984         case TRIDENT_DEVICE_ID_SI7018:
3985                 snd_trident_sis_init(trident);
3986                 break;
3987         }
3988
3989         snd_ac97_resume(trident->ac97);
3990         if (trident->ac97_sec)
3991                 snd_ac97_resume(trident->ac97_sec);
3992
3993         /* restore some registers */
3994         outl(trident->musicvol_wavevol, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
3995
3996         snd_trident_enable_eso(trident);
3997
3998         trident->in_suspend = 0;
3999         return 0;
4000 }
4001 #endif /* CONFIG_PM */
4002
4003 EXPORT_SYMBOL(snd_trident_alloc_voice);
4004 EXPORT_SYMBOL(snd_trident_free_voice);
4005 EXPORT_SYMBOL(snd_trident_start_voice);
4006 EXPORT_SYMBOL(snd_trident_stop_voice);
4007 EXPORT_SYMBOL(snd_trident_write_voice_regs);
4008 /* trident_memory.c symbols */
4009 EXPORT_SYMBOL(snd_trident_synth_alloc);
4010 EXPORT_SYMBOL(snd_trident_synth_free);
4011 EXPORT_SYMBOL(snd_trident_synth_copy_from_user);