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