Blame view

sound/pci/trident/trident_main.c 122 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
c1017a4cd   Jaroslav Kysela   [ALSA] Changed Ja...
2
   *  Maintained by Jaroslav Kysela <perex@perex.cz>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
   *  Originated by audio@tridentmicro.com
   *  Fri Feb 19 15:55:28 MST 1999
   *  Routines for control of Trident 4DWave (DX and NX) chip
   *
   *  BUGS:
   *
   *  TODO:
   *    ---
   *
   *   This program is free software; you can redistribute it and/or modify
   *   it under the terms of the GNU General Public License as published by
   *   the Free Software Foundation; either version 2 of the License, or
   *   (at your option) any later version.
   *
   *   This program is distributed in the hope that it will be useful,
   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *   GNU General Public License for more details.
   *
   *   You should have received a copy of the GNU General Public License
   *   along with this program; if not, write to the Free Software
   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   *
   *
   *  SiS7018 S/PDIF support by Thomas Winischhofer <thomas@winischhofer.net>
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
31
32
33
34
35
  #include <linux/delay.h>
  #include <linux/init.h>
  #include <linux/interrupt.h>
  #include <linux/pci.h>
  #include <linux/slab.h>
  #include <linux/vmalloc.h>
  #include <linux/gameport.h>
910638ae7   Matthias Gehre   [PATCH] Replace 0...
36
  #include <linux/dma-mapping.h>
d81a6d717   Paul Gortmaker   sound: Add export...
37
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
39
40
41
  
  #include <sound/core.h>
  #include <sound/info.h>
  #include <sound/control.h>
a0aef8edf   Takashi Iwai   [ALSA] Add dB sca...
42
  #include <sound/tlv.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
45
46
  #include <sound/trident.h>
  #include <sound/asoundef.h>
  
  #include <asm/io.h>
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
47
48
49
50
51
52
  static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
  				       struct snd_trident_voice * voice,
  				       struct snd_pcm_substream *substream);
  static int snd_trident_pcm_mixer_free(struct snd_trident *trident,
  				      struct snd_trident_voice * voice,
  				      struct snd_pcm_substream *substream);
7d12e780e   David Howells   IRQ: Maintain reg...
53
  static irqreturn_t snd_trident_interrupt(int irq, void *dev_id);
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
54
  static int snd_trident_sis_reset(struct snd_trident *trident);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55

bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
56
57
58
  static void snd_trident_clear_voices(struct snd_trident * trident,
  				     unsigned short v_min, unsigned short v_max);
  static int snd_trident_free(struct snd_trident *trident);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
61
62
63
64
65
  
  /*
   *  common I/O routines
   */
  
  
  #if 0
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
66
  static void snd_trident_print_voice_regs(struct snd_trident *trident, int voice)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
68
  {
  	unsigned int val, tmp;
ee419653a   Takashi Iwai   ALSA: Fix missing...
69
70
  	printk(KERN_DEBUG "Trident voice %i:
  ", voice);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
  	outb(voice, TRID_REG(trident, T4D_LFO_GC_CIR));
  	val = inl(TRID_REG(trident, CH_LBA));
ee419653a   Takashi Iwai   ALSA: Fix missing...
73
74
  	printk(KERN_DEBUG "LBA: 0x%x
  ", val);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
  	val = inl(TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
ee419653a   Takashi Iwai   ALSA: Fix missing...
76
77
78
79
80
81
82
83
84
85
  	printk(KERN_DEBUG "GVSel: %i
  ", val >> 31);
  	printk(KERN_DEBUG "Pan: 0x%x
  ", (val >> 24) & 0x7f);
  	printk(KERN_DEBUG "Vol: 0x%x
  ", (val >> 16) & 0xff);
  	printk(KERN_DEBUG "CTRL: 0x%x
  ", (val >> 12) & 0x0f);
  	printk(KERN_DEBUG "EC: 0x%x
  ", val & 0x0fff);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
  	if (trident->device != TRIDENT_DEVICE_ID_NX) {
  		val = inl(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS));
ee419653a   Takashi Iwai   ALSA: Fix missing...
88
89
  		printk(KERN_DEBUG "CSO: 0x%x
  ", val >> 16);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
  		printk("Alpha: 0x%x
  ", (val >> 4) & 0x0fff);
ee419653a   Takashi Iwai   ALSA: Fix missing...
92
93
  		printk(KERN_DEBUG "FMS: 0x%x
  ", val & 0x0f);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
  		val = inl(TRID_REG(trident, CH_DX_ESO_DELTA));
ee419653a   Takashi Iwai   ALSA: Fix missing...
95
96
97
98
  		printk(KERN_DEBUG "ESO: 0x%x
  ", val >> 16);
  		printk(KERN_DEBUG "Delta: 0x%x
  ", val & 0xffff);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
101
102
  		val = inl(TRID_REG(trident, CH_DX_FMC_RVOL_CVOL));
  	} else {		// TRIDENT_DEVICE_ID_NX
  		val = inl(TRID_REG(trident, CH_NX_DELTA_CSO));
  		tmp = (val >> 24) & 0xff;
ee419653a   Takashi Iwai   ALSA: Fix missing...
103
104
  		printk(KERN_DEBUG "CSO: 0x%x
  ", val & 0x00ffffff);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105
106
  		val = inl(TRID_REG(trident, CH_NX_DELTA_ESO));
  		tmp |= (val >> 16) & 0xff00;
ee419653a   Takashi Iwai   ALSA: Fix missing...
107
108
109
110
  		printk(KERN_DEBUG "Delta: 0x%x
  ", tmp);
  		printk(KERN_DEBUG "ESO: 0x%x
  ", val & 0x00ffffff);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
  		val = inl(TRID_REG(trident, CH_NX_ALPHA_FMS_FMC_RVOL_CVOL));
ee419653a   Takashi Iwai   ALSA: Fix missing...
112
113
114
115
  		printk(KERN_DEBUG "Alpha: 0x%x
  ", val >> 20);
  		printk(KERN_DEBUG "FMS: 0x%x
  ", (val >> 16) & 0x0f);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
  	}
ee419653a   Takashi Iwai   ALSA: Fix missing...
117
118
119
120
121
122
  	printk(KERN_DEBUG "FMC: 0x%x
  ", (val >> 14) & 3);
  	printk(KERN_DEBUG "RVol: 0x%x
  ", (val >> 7) & 0x7f);
  	printk(KERN_DEBUG "CVol: 0x%x
  ", val & 0x7f);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
124
125
126
  }
  #endif
  
  /*---------------------------------------------------------------------------
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
127
     unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
129
130
131
132
133
134
135
136
137
    
     Description: This routine will do all of the reading from the external
                  CODEC (AC97).
    
     Parameters:  ac97 - ac97 codec structure
                  reg - CODEC register index, from AC97 Hal.
   
     returns:     16 bit value read from the AC97.
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
138
  static unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
141
142
  {
  	unsigned int data = 0, treg;
  	unsigned short count = 0xffff;
  	unsigned long flags;
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
143
  	struct snd_trident *trident = ac97->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  
  	spin_lock_irqsave(&trident->reg_lock, flags);
  	if (trident->device == TRIDENT_DEVICE_ID_DX) {
  		data = (DX_AC97_BUSY_READ | (reg & 0x000000ff));
  		outl(data, TRID_REG(trident, DX_ACR1_AC97_R));
  		do {
  			data = inl(TRID_REG(trident, DX_ACR1_AC97_R));
  			if ((data & DX_AC97_BUSY_READ) == 0)
  				break;
  		} while (--count);
  	} else if (trident->device == TRIDENT_DEVICE_ID_NX) {
  		data = (NX_AC97_BUSY_READ | (reg & 0x000000ff));
  		treg = ac97->num == 0 ? NX_ACR2_AC97_R_PRIMARY : NX_ACR3_AC97_R_SECONDARY;
  		outl(data, TRID_REG(trident, treg));
  		do {
  			data = inl(TRID_REG(trident, treg));
  			if ((data & 0x00000C00) == 0)
  				break;
  		} while (--count);
  	} else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  		data = SI_AC97_BUSY_READ | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
  		if (ac97->num == 1)
  			data |= SI_AC97_SECONDARY;
  		outl(data, TRID_REG(trident, SI_AC97_READ));
  		do {
  			data = inl(TRID_REG(trident, SI_AC97_READ));
  			if ((data & (SI_AC97_BUSY_READ)) == 0)
  				break;
  		} while (--count);
  	}
  
  	if (count == 0 && !trident->ac97_detect) {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
176
177
178
  		snd_printk(KERN_ERR "ac97 codec read TIMEOUT [0x%x/0x%x]!!!
  ",
  			   reg, data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
180
181
182
183
184
185
186
  		data = 0;
  	}
  
  	spin_unlock_irqrestore(&trident->reg_lock, flags);
  	return ((unsigned short) (data >> 16));
  }
  
  /*---------------------------------------------------------------------------
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
187
188
     void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
     unsigned short wdata)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189
190
191
192
193
194
195
196
197
198
199
    
     Description: This routine will do all of the writing to the external
                  CODEC (AC97).
    
     Parameters:	ac97 - ac97 codec structure
     	        reg - CODEC register index, from AC97 Hal.
                  data  - Lower 16 bits are the data to write to CODEC.
    
     returns:     TRUE if everything went ok, else FALSE.
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
200
201
  static void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
  				    unsigned short wdata)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
203
204
205
  {
  	unsigned int address, data;
  	unsigned short count = 0xffff;
  	unsigned long flags;
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
206
  	struct snd_trident *trident = ac97->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
  
  	data = ((unsigned long) wdata) << 16;
  
  	spin_lock_irqsave(&trident->reg_lock, flags);
  	if (trident->device == TRIDENT_DEVICE_ID_DX) {
  		address = DX_ACR0_AC97_W;
  
  		/* read AC-97 write register status */
  		do {
  			if ((inw(TRID_REG(trident, address)) & DX_AC97_BUSY_WRITE) == 0)
  				break;
  		} while (--count);
  
  		data |= (DX_AC97_BUSY_WRITE | (reg & 0x000000ff));
  	} else if (trident->device == TRIDENT_DEVICE_ID_NX) {
  		address = NX_ACR1_AC97_W;
  
  		/* read AC-97 write register status */
  		do {
  			if ((inw(TRID_REG(trident, address)) & NX_AC97_BUSY_WRITE) == 0)
  				break;
  		} while (--count);
  
  		data |= (NX_AC97_BUSY_WRITE | (ac97->num << 8) | (reg & 0x000000ff));
  	} else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  		address = SI_AC97_WRITE;
  
  		/* read AC-97 write register status */
  		do {
  			if ((inw(TRID_REG(trident, address)) & (SI_AC97_BUSY_WRITE)) == 0)
  				break;
  		} while (--count);
  
  		data |= SI_AC97_BUSY_WRITE | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
  		if (ac97->num == 1)
  			data |= SI_AC97_SECONDARY;
  	} else {
  		address = 0;	/* keep GCC happy */
  		count = 0;	/* return */
  	}
  
  	if (count == 0) {
  		spin_unlock_irqrestore(&trident->reg_lock, flags);
  		return;
  	}
  	outl(data, TRID_REG(trident, address));
  	spin_unlock_irqrestore(&trident->reg_lock, flags);
  }
  
  /*---------------------------------------------------------------------------
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
257
     void snd_trident_enable_eso(struct snd_trident *trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
258
259
260
261
262
263
264
265
266
    
     Description: This routine will enable end of loop interrupts.
                  End of loop interrupts will occur when a running
                  channel reaches ESO.
                  Also enables middle of loop interrupts.
    
     Parameters:  trident - pointer to target device class for 4DWave.
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
267
  static void snd_trident_enable_eso(struct snd_trident * trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268
269
270
271
272
273
274
275
276
277
278
279
  {
  	unsigned int val;
  
  	val = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
  	val |= ENDLP_IE;
  	val |= MIDLP_IE;
  	if (trident->device == TRIDENT_DEVICE_ID_SI7018)
  		val |= BANK_B_EN;
  	outl(val, TRID_REG(trident, T4D_LFO_GC_CIR));
  }
  
  /*---------------------------------------------------------------------------
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
280
     void snd_trident_disable_eso(struct snd_trident *trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
282
283
284
285
286
287
288
289
290
291
292
    
     Description: This routine will disable end of loop interrupts.
                  End of loop interrupts will occur when a running
                  channel reaches ESO.
                  Also disables middle of loop interrupts.
    
     Parameters:  
                  trident - pointer to target device class for 4DWave.
    
     returns:     TRUE if everything went ok, else FALSE.
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
293
  static void snd_trident_disable_eso(struct snd_trident * trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
294
295
296
297
298
299
300
301
302
303
  {
  	unsigned int tmp;
  
  	tmp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
  	tmp &= ~ENDLP_IE;
  	tmp &= ~MIDLP_IE;
  	outl(tmp, TRID_REG(trident, T4D_LFO_GC_CIR));
  }
  
  /*---------------------------------------------------------------------------
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
304
     void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
306
307
308
309
310
311
312
313
314
315
  
      Description: Start a voice, any channel 0 thru 63.
                   This routine automatically handles the fact that there are
                   more than 32 channels available.
  
      Parameters : voice - Voice number 0 thru n.
                   trident - pointer to target device class for 4DWave.
  
      Return Value: None.
  
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
316
  void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
318
319
320
321
322
  {
  	unsigned int mask = 1 << (voice & 0x1f);
  	unsigned int reg = (voice & 0x20) ? T4D_START_B : T4D_START_A;
  
  	outl(mask, TRID_REG(trident, reg));
  }
cbef55f3d   Takashi Iwai   [ALSA] trident - ...
323
  EXPORT_SYMBOL(snd_trident_start_voice);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
324
  /*---------------------------------------------------------------------------
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
325
     void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326
327
328
329
330
331
332
333
334
335
336
  
      Description: Stop a voice, any channel 0 thru 63.
                   This routine automatically handles the fact that there are
                   more than 32 channels available.
  
      Parameters : voice - Voice number 0 thru n.
                   trident - pointer to target device class for 4DWave.
  
      Return Value: None.
  
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
337
  void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
338
339
340
341
342
343
  {
  	unsigned int mask = 1 << (voice & 0x1f);
  	unsigned int reg = (voice & 0x20) ? T4D_STOP_B : T4D_STOP_A;
  
  	outl(mask, TRID_REG(trident, reg));
  }
cbef55f3d   Takashi Iwai   [ALSA] trident - ...
344
  EXPORT_SYMBOL(snd_trident_stop_voice);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
345
  /*---------------------------------------------------------------------------
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
346
      int snd_trident_allocate_pcm_channel(struct snd_trident *trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
347
348
349
350
351
352
353
354
    
      Description: Allocate hardware channel in Bank B (32-63).
    
      Parameters :  trident - pointer to target device class for 4DWave.
    
      Return Value: hardware channel - 32-63 or -1 when no channel is available
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
355
  static int snd_trident_allocate_pcm_channel(struct snd_trident * trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
  {
  	int idx;
  
  	if (trident->ChanPCMcnt >= trident->ChanPCM)
  		return -1;
  	for (idx = 31; idx >= 0; idx--) {
  		if (!(trident->ChanMap[T4D_BANK_B] & (1 << idx))) {
  			trident->ChanMap[T4D_BANK_B] |= 1 << idx;
  			trident->ChanPCMcnt++;
  			return idx + 32;
  		}
  	}
  	return -1;
  }
  
  /*---------------------------------------------------------------------------
      void snd_trident_free_pcm_channel(int channel)
    
      Description: Free hardware channel in Bank B (32-63)
    
      Parameters :  trident - pointer to target device class for 4DWave.
  	          channel - hardware channel number 0-63
    
      Return Value: none
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
382
  static void snd_trident_free_pcm_channel(struct snd_trident *trident, int channel)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
  {
  	if (channel < 32 || channel > 63)
  		return;
  	channel &= 0x1f;
  	if (trident->ChanMap[T4D_BANK_B] & (1 << channel)) {
  		trident->ChanMap[T4D_BANK_B] &= ~(1 << channel);
  		trident->ChanPCMcnt--;
  	}
  }
  
  /*---------------------------------------------------------------------------
      unsigned int snd_trident_allocate_synth_channel(void)
    
      Description: Allocate hardware channel in Bank A (0-31).
    
      Parameters :  trident - pointer to target device class for 4DWave.
    
      Return Value: hardware channel - 0-31 or -1 when no channel is available
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
403
  static int snd_trident_allocate_synth_channel(struct snd_trident * trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
  {
  	int idx;
  
  	for (idx = 31; idx >= 0; idx--) {
  		if (!(trident->ChanMap[T4D_BANK_A] & (1 << idx))) {
  			trident->ChanMap[T4D_BANK_A] |= 1 << idx;
  			trident->synth.ChanSynthCount++;
  			return idx;
  		}
  	}
  	return -1;
  }
  
  /*---------------------------------------------------------------------------
      void snd_trident_free_synth_channel( int channel )
    
      Description: Free hardware channel in Bank B (0-31).
    
      Parameters :  trident - pointer to target device class for 4DWave.
  	          channel - hardware channel number 0-63
    
      Return Value: none
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
428
  static void snd_trident_free_synth_channel(struct snd_trident *trident, int channel)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
  {
  	if (channel < 0 || channel > 31)
  		return;
  	channel &= 0x1f;
  	if (trident->ChanMap[T4D_BANK_A] & (1 << channel)) {
  		trident->ChanMap[T4D_BANK_A] &= ~(1 << channel);
  		trident->synth.ChanSynthCount--;
  	}
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_write_voice_regs
    
     Description: This routine will complete and write the 5 hardware channel
                  registers to hardware.
    
561de31a2   Joe Perches   [ALSA] sound/: Sp...
445
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
446
447
448
449
                  voice - synthesizer voice structure
                  Each register field.
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
450
451
  void snd_trident_write_voice_regs(struct snd_trident * trident,
  				  struct snd_trident_voice * voice)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
  {
  	unsigned int FmcRvolCvol;
  	unsigned int regs[5];
  
  	regs[1] = voice->LBA;
  	regs[4] = (voice->GVSel << 31) |
  		  ((voice->Pan & 0x0000007f) << 24) |
  		  ((voice->CTRL & 0x0000000f) << 12);
  	FmcRvolCvol = ((voice->FMC & 3) << 14) |
  	              ((voice->RVol & 0x7f) << 7) |
  	              (voice->CVol & 0x7f);
  
  	switch (trident->device) {
  	case TRIDENT_DEVICE_ID_SI7018:
  		regs[4] |= voice->number > 31 ?
  				(voice->Vol & 0x000003ff) :
  				((voice->Vol & 0x00003fc) << (16-2)) |
  				(voice->EC & 0x00000fff);
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
470
471
  		regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
  			(voice->FMS & 0x0000000f);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
472
473
474
475
476
477
  		regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
  		regs[3] = (voice->Attribute << 16) | FmcRvolCvol;
  		break;
  	case TRIDENT_DEVICE_ID_DX:
  		regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
  			   (voice->EC & 0x00000fff);
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
478
479
  		regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
  			(voice->FMS & 0x0000000f);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
480
481
482
483
484
485
486
  		regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
  		regs[3] = FmcRvolCvol;
  		break;
  	case TRIDENT_DEVICE_ID_NX:
  		regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
  			   (voice->EC & 0x00000fff);
  		regs[0] = (voice->Delta << 24) | (voice->CSO & 0x00ffffff);
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
487
488
489
490
  		regs[2] = ((voice->Delta << 16) & 0xff000000) |
  			(voice->ESO & 0x00ffffff);
  		regs[3] = (voice->Alpha << 20) |
  			((voice->FMS & 0x0000000f) << 16) | FmcRvolCvol;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
493
  		break;
  	default:
  		snd_BUG();
b95eed7cd   Takashi Iwai   [ALSA] trident - ...
494
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
495
496
497
498
499
500
501
502
503
504
  	}
  
  	outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  	outl(regs[0], TRID_REG(trident, CH_START + 0));
  	outl(regs[1], TRID_REG(trident, CH_START + 4));
  	outl(regs[2], TRID_REG(trident, CH_START + 8));
  	outl(regs[3], TRID_REG(trident, CH_START + 12));
  	outl(regs[4], TRID_REG(trident, CH_START + 16));
  
  #if 0
ee419653a   Takashi Iwai   ALSA: Fix missing...
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
  	printk(KERN_DEBUG "written %i channel:
  ", voice->number);
  	printk(KERN_DEBUG "  regs[0] = 0x%x/0x%x
  ",
  	       regs[0], inl(TRID_REG(trident, CH_START + 0)));
  	printk(KERN_DEBUG "  regs[1] = 0x%x/0x%x
  ",
  	       regs[1], inl(TRID_REG(trident, CH_START + 4)));
  	printk(KERN_DEBUG "  regs[2] = 0x%x/0x%x
  ",
  	       regs[2], inl(TRID_REG(trident, CH_START + 8)));
  	printk(KERN_DEBUG "  regs[3] = 0x%x/0x%x
  ",
  	       regs[3], inl(TRID_REG(trident, CH_START + 12)));
  	printk(KERN_DEBUG "  regs[4] = 0x%x/0x%x
  ",
  	       regs[4], inl(TRID_REG(trident, CH_START + 16)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
522
523
  #endif
  }
cbef55f3d   Takashi Iwai   [ALSA] trident - ...
524
  EXPORT_SYMBOL(snd_trident_write_voice_regs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
525
526
527
528
529
530
  /*---------------------------------------------------------------------------
     snd_trident_write_cso_reg
    
     Description: This routine will write the new CSO offset
                  register to hardware.
    
561de31a2   Joe Perches   [ALSA] sound/: Sp...
531
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
532
533
534
535
                  voice - synthesizer voice structure
                  CSO - new CSO value
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
536
537
538
  static void snd_trident_write_cso_reg(struct snd_trident * trident,
  				      struct snd_trident_voice * voice,
  				      unsigned int CSO)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
539
540
541
542
543
544
  {
  	voice->CSO = CSO;
  	outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  	if (trident->device != TRIDENT_DEVICE_ID_NX) {
  		outw(voice->CSO, TRID_REG(trident, CH_DX_CSO_ALPHA_FMS) + 2);
  	} else {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
545
546
  		outl((voice->Delta << 24) |
  		     (voice->CSO & 0x00ffffff), TRID_REG(trident, CH_NX_DELTA_CSO));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
547
548
549
550
551
552
553
554
555
  	}
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_write_eso_reg
    
     Description: This routine will write the new ESO offset
                  register to hardware.
    
561de31a2   Joe Perches   [ALSA] sound/: Sp...
556
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
557
558
559
560
                  voice - synthesizer voice structure
                  ESO - new ESO value
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
561
562
563
  static void snd_trident_write_eso_reg(struct snd_trident * trident,
  				      struct snd_trident_voice * voice,
  				      unsigned int ESO)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
565
566
567
568
569
  {
  	voice->ESO = ESO;
  	outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  	if (trident->device != TRIDENT_DEVICE_ID_NX) {
  		outw(voice->ESO, TRID_REG(trident, CH_DX_ESO_DELTA) + 2);
  	} else {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
570
571
  		outl(((voice->Delta << 16) & 0xff000000) | (voice->ESO & 0x00ffffff),
  		     TRID_REG(trident, CH_NX_DELTA_ESO));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
572
573
574
575
576
577
578
579
580
  	}
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_write_vol_reg
    
     Description: This routine will write the new voice volume
                  register to hardware.
    
561de31a2   Joe Perches   [ALSA] sound/: Sp...
581
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
582
583
584
585
                  voice - synthesizer voice structure
                  Vol - new voice volume
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
586
587
588
  static void snd_trident_write_vol_reg(struct snd_trident * trident,
  				      struct snd_trident_voice * voice,
  				      unsigned int Vol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
589
590
591
592
593
594
595
596
597
  {
  	voice->Vol = Vol;
  	outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  	switch (trident->device) {
  	case TRIDENT_DEVICE_ID_DX:
  	case TRIDENT_DEVICE_ID_NX:
  		outb(voice->Vol >> 2, TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 2));
  		break;
  	case TRIDENT_DEVICE_ID_SI7018:
ee419653a   Takashi Iwai   ALSA: Fix missing...
598
599
  		/* printk(KERN_DEBUG "voice->Vol = 0x%x
  ", voice->Vol); */
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
600
601
  		outw((voice->CTRL << 12) | voice->Vol,
  		     TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
602
603
604
605
606
607
608
609
610
611
  		break;
  	}
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_write_pan_reg
    
     Description: This routine will write the new voice pan
                  register to hardware.
    
561de31a2   Joe Perches   [ALSA] sound/: Sp...
612
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
613
614
615
616
                  voice - synthesizer voice structure
                  Pan - new pan value
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
617
618
619
  static void snd_trident_write_pan_reg(struct snd_trident * trident,
  				      struct snd_trident_voice * voice,
  				      unsigned int Pan)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
620
621
622
  {
  	voice->Pan = Pan;
  	outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
623
624
  	outb(((voice->GVSel & 0x01) << 7) | (voice->Pan & 0x7f),
  	     TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 3));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
625
626
627
628
629
630
631
632
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_write_rvol_reg
    
     Description: This routine will write the new reverb volume
                  register to hardware.
    
561de31a2   Joe Perches   [ALSA] sound/: Sp...
633
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
634
635
636
637
                  voice - synthesizer voice structure
                  RVol - new reverb volume
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
638
639
640
  static void snd_trident_write_rvol_reg(struct snd_trident * trident,
  				       struct snd_trident_voice * voice,
  				       unsigned int RVol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
641
642
643
  {
  	voice->RVol = RVol;
  	outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
644
645
646
647
  	outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
  	     (voice->CVol & 0x007f),
  	     TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
  		      CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
648
649
650
651
652
653
654
655
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_write_cvol_reg
    
     Description: This routine will write the new chorus volume
                  register to hardware.
    
561de31a2   Joe Perches   [ALSA] sound/: Sp...
656
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
657
658
659
660
                  voice - synthesizer voice structure
                  CVol - new chorus volume
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
661
662
663
  static void snd_trident_write_cvol_reg(struct snd_trident * trident,
  				       struct snd_trident_voice * voice,
  				       unsigned int CVol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
664
665
666
  {
  	voice->CVol = CVol;
  	outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
667
668
669
670
  	outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
  	     (voice->CVol & 0x007f),
  	     TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
  		      CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
671
672
673
674
675
676
677
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_convert_rate
  
     Description: This routine converts rate in HZ to hardware delta value.
    
561de31a2   Joe Perches   [ALSA] sound/: Sp...
678
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
                  rate - Real or Virtual channel number.
    
     Returns:     Delta value.
    
    ---------------------------------------------------------------------------*/
  static unsigned int snd_trident_convert_rate(unsigned int rate)
  {
  	unsigned int delta;
  
  	// We special case 44100 and 8000 since rounding with the equation
  	// does not give us an accurate enough value. For 11025 and 22050
  	// the equation gives us the best answer. All other frequencies will
  	// also use the equation. JDW
  	if (rate == 44100)
  		delta = 0xeb3;
  	else if (rate == 8000)
  		delta = 0x2ab;
  	else if (rate == 48000)
  		delta = 0x1000;
  	else
  		delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
  	return delta;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_convert_adc_rate
  
     Description: This routine converts rate in HZ to hardware delta value.
    
561de31a2   Joe Perches   [ALSA] sound/: Sp...
708
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
                  rate - Real or Virtual channel number.
    
     Returns:     Delta value.
    
    ---------------------------------------------------------------------------*/
  static unsigned int snd_trident_convert_adc_rate(unsigned int rate)
  {
  	unsigned int delta;
  
  	// We special case 44100 and 8000 since rounding with the equation
  	// does not give us an accurate enough value. For 11025 and 22050
  	// the equation gives us the best answer. All other frequencies will
  	// also use the equation. JDW
  	if (rate == 44100)
  		delta = 0x116a;
  	else if (rate == 8000)
  		delta = 0x6000;
  	else if (rate == 48000)
  		delta = 0x1000;
  	else
  		delta = ((48000 << 12) / rate) & 0x0000ffff;
  	return delta;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_spurious_threshold
  
     Description: This routine converts rate in HZ to spurious threshold.
    
561de31a2   Joe Perches   [ALSA] sound/: Sp...
738
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
739
740
741
742
743
                  rate - Real or Virtual channel number.
    
     Returns:     Delta value.
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
744
745
  static unsigned int snd_trident_spurious_threshold(unsigned int rate,
  						   unsigned int period_size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
746
747
748
749
750
751
752
753
754
755
756
757
758
759
  {
  	unsigned int res = (rate * period_size) / 48000;
  	if (res < 64)
  		res = res / 2;
  	else
  		res -= 32;
  	return res;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_control_mode
  
     Description: This routine returns a control mode for a PCM channel.
    
561de31a2   Joe Perches   [ALSA] sound/: Sp...
760
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
761
762
763
764
765
                  substream  - PCM substream
    
     Returns:     Control value.
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
766
  static unsigned int snd_trident_control_mode(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
767
768
  {
  	unsigned int CTRL;
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
769
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
  
  	/* set ctrl mode
  	   CTRL default: 8-bit (unsigned) mono, loop mode enabled
  	 */
  	CTRL = 0x00000001;
  	if (snd_pcm_format_width(runtime->format) == 16)
  		CTRL |= 0x00000008;	// 16-bit data
  	if (snd_pcm_format_signed(runtime->format))
  		CTRL |= 0x00000002;	// signed data
  	if (runtime->channels > 1)
  		CTRL |= 0x00000004;	// stereo data
  	return CTRL;
  }
  
  /*
   *  PCM part
   */
  
  /*---------------------------------------------------------------------------
     snd_trident_ioctl
    
     Description: Device I/O control handler for playback/capture parameters.
    
561de31a2   Joe Perches   [ALSA] sound/: Sp...
793
     Parameters:   substream  - PCM substream class
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
794
795
796
797
798
799
                  cmd     - what ioctl message to process
                  arg     - additional message infoarg     
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
800
  static int snd_trident_ioctl(struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
  			     unsigned int cmd,
  			     void *arg)
  {
  	/* FIXME: it seems that with small periods the behaviour of
  	          trident hardware is unpredictable and interrupt generator
  	          is broken */
  	return snd_pcm_lib_ioctl(substream, cmd, arg);
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_allocate_pcm_mem
    
     Description: Allocate PCM ring buffer for given substream
    
     Parameters:  substream  - PCM substream class
  		hw_params  - hardware parameters
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
821
822
  static int snd_trident_allocate_pcm_mem(struct snd_pcm_substream *substream,
  					struct snd_pcm_hw_params *hw_params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
824
825
826
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_trident_voice *voice = runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
  	int err;
  
  	if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  		return err;
  	if (trident->tlb.entries) {
  		if (err > 0) { /* change */
  			if (voice->memblk)
  				snd_trident_free_pages(trident, voice->memblk);
  			voice->memblk = snd_trident_alloc_pages(trident, substream);
  			if (voice->memblk == NULL)
  				return -ENOMEM;
  		}
  	}
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_allocate_evoice
    
     Description: Allocate extra voice as interrupt generator
    
     Parameters:  substream  - PCM substream class
  		hw_params  - hardware parameters
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
854
855
  static int snd_trident_allocate_evoice(struct snd_pcm_substream *substream,
  				       struct snd_pcm_hw_params *hw_params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
856
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
857
858
859
860
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_trident_voice *voice = runtime->private_data;
  	struct snd_trident_voice *evoice = voice->extra;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
  
  	/* voice management */
  
  	if (params_buffer_size(hw_params) / 2 != params_period_size(hw_params)) {
  		if (evoice == NULL) {
  			evoice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  			if (evoice == NULL)
  				return -ENOMEM;
  			voice->extra = evoice;
  			evoice->substream = substream;
  		}
  	} else {
  		if (evoice != NULL) {
  			snd_trident_free_voice(trident, evoice);
  			voice->extra = evoice = NULL;
  		}
  	}
  
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_hw_params
    
     Description: Set the hardware parameters for the playback device.
    
     Parameters:  substream  - PCM substream class
  		hw_params  - hardware parameters
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
893
894
  static int snd_trident_hw_params(struct snd_pcm_substream *substream,
  				 struct snd_pcm_hw_params *hw_params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
  {
  	int err;
  
  	err = snd_trident_allocate_pcm_mem(substream, hw_params);
  	if (err >= 0)
  		err = snd_trident_allocate_evoice(substream, hw_params);
  	return err;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_playback_hw_free
    
     Description: Release the hardware resources for the playback device.
    
     Parameters:  substream  - PCM substream class
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
914
  static int snd_trident_hw_free(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
915
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
916
917
918
919
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_trident_voice *voice = runtime->private_data;
  	struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
  
  	if (trident->tlb.entries) {
  		if (voice && voice->memblk) {
  			snd_trident_free_pages(trident, voice->memblk);
  			voice->memblk = NULL;
  		}
  	}
  	snd_pcm_lib_free_pages(substream);
  	if (evoice != NULL) {
  		snd_trident_free_voice(trident, evoice);
  		voice->extra = NULL;
  	}
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_playback_prepare
    
     Description: Prepare playback device for playback.
    
     Parameters:  substream  - PCM substream class
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
945
  static int snd_trident_playback_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
946
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
947
948
949
950
951
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_trident_voice *voice = runtime->private_data;
  	struct snd_trident_voice *evoice = voice->extra;
  	struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
  
  	spin_lock_irq(&trident->reg_lock);	
  
  	/* set delta (rate) value */
  	voice->Delta = snd_trident_convert_rate(runtime->rate);
  	voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  
  	/* set Loop Begin Address */
  	if (voice->memblk)
  		voice->LBA = voice->memblk->offset;
  	else
  		voice->LBA = runtime->dma_addr;
   
  	voice->CSO = 0;
  	voice->ESO = runtime->buffer_size - 1;	/* in samples */
  	voice->CTRL = snd_trident_control_mode(substream);
  	voice->FMC = 3;
  	voice->GVSel = 1;
  	voice->EC = 0;
  	voice->Alpha = 0;
  	voice->FMS = 0;
  	voice->Vol = mix->vol;
  	voice->RVol = mix->rvol;
  	voice->CVol = mix->cvol;
  	voice->Pan = mix->pan;
  	voice->Attribute = 0;
  #if 0
  	voice->Attribute = (1<<(30-16))|(2<<(26-16))|
  			   (0<<(24-16))|(0x1f<<(19-16));
  #else
  	voice->Attribute = 0;
  #endif
  
  	snd_trident_write_voice_regs(trident, voice);
  
  	if (evoice != NULL) {
  		evoice->Delta = voice->Delta;
  		evoice->spurious_threshold = voice->spurious_threshold;
  		evoice->LBA = voice->LBA;
  		evoice->CSO = 0;
  		evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
  		evoice->CTRL = voice->CTRL;
  		evoice->FMC = 3;
  		evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
  		evoice->EC = 0;
  		evoice->Alpha = 0;
  		evoice->FMS = 0;
  		evoice->Vol = 0x3ff;			/* mute */
  		evoice->RVol = evoice->CVol = 0x7f;	/* mute */
  		evoice->Pan = 0x7f;			/* mute */
  #if 0
  		evoice->Attribute = (1<<(30-16))|(2<<(26-16))|
  				    (0<<(24-16))|(0x1f<<(19-16));
  #else
  		evoice->Attribute = 0;
  #endif
  		snd_trident_write_voice_regs(trident, evoice);
  		evoice->isync2 = 1;
  		evoice->isync_mark = runtime->period_size;
  		evoice->ESO = (runtime->period_size * 2) - 1;
  	}
  
  	spin_unlock_irq(&trident->reg_lock);
  
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_capture_hw_params
    
     Description: Set the hardware parameters for the capture device.
    
     Parameters:  substream  - PCM substream class
  		hw_params  - hardware parameters
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1030
1031
  static int snd_trident_capture_hw_params(struct snd_pcm_substream *substream,
  					 struct snd_pcm_hw_params *hw_params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
  {
  	return snd_trident_allocate_pcm_mem(substream, hw_params);
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_capture_prepare
    
     Description: Prepare capture device for playback.
    
     Parameters:  substream  - PCM substream class
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1046
  static int snd_trident_capture_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1047
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1048
1049
1050
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_trident_voice *voice = runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1051
1052
1053
  	unsigned int val, ESO_bytes;
  
  	spin_lock_irq(&trident->reg_lock);
421f91d21   Uwe Kleine-König   fix typos concern...
1054
  	// Initialize the channel and set channel Mode
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
  	outb(0, TRID_REG(trident, LEGACY_DMAR15));
  
  	// Set DMA channel operation mode register
  	outb(0x54, TRID_REG(trident, LEGACY_DMAR11));
  
  	// Set channel buffer Address, DMAR0 expects contiguous PCI memory area	
  	voice->LBA = runtime->dma_addr;
  	outl(voice->LBA, TRID_REG(trident, LEGACY_DMAR0));
  	if (voice->memblk)
  		voice->LBA = voice->memblk->offset;
  
  	// set ESO
  	ESO_bytes = snd_pcm_lib_buffer_bytes(substream) - 1;
  	outb((ESO_bytes & 0x00ff0000) >> 16, TRID_REG(trident, LEGACY_DMAR6));
  	outw((ESO_bytes & 0x0000ffff), TRID_REG(trident, LEGACY_DMAR4));
  	ESO_bytes++;
  
  	// Set channel sample rate, 4.12 format
  	val = (((unsigned int) 48000L << 12) + (runtime->rate/2)) / runtime->rate;
  	outw(val, TRID_REG(trident, T4D_SBDELTA_DELTA_R));
  
  	// Set channel interrupt blk length
  	if (snd_pcm_format_width(runtime->format) == 16) {
  		val = (unsigned short) ((ESO_bytes >> 1) - 1);
  	} else {
  		val = (unsigned short) (ESO_bytes - 1);
  	}
  
  	outl((val << 16) | val, TRID_REG(trident, T4D_SBBL_SBCL));
  
  	// Right now, set format and start to run captureing, 
  	// continuous run loop enable.
  	trident->bDMAStart = 0x19;	// 0001 1001b
  
  	if (snd_pcm_format_width(runtime->format) == 16)
  		trident->bDMAStart |= 0x80;
  	if (snd_pcm_format_signed(runtime->format))
  		trident->bDMAStart |= 0x20;
  	if (runtime->channels > 1)
  		trident->bDMAStart |= 0x40;
  
  	// Prepare capture intr channel
  
  	voice->Delta = snd_trident_convert_rate(runtime->rate);
  	voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  	voice->isync = 1;
  	voice->isync_mark = runtime->period_size;
  	voice->isync_max = runtime->buffer_size;
  
  	// Set voice parameters
  	voice->CSO = 0;
  	voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
  	voice->CTRL = snd_trident_control_mode(substream);
  	voice->FMC = 3;
  	voice->RVol = 0x7f;
  	voice->CVol = 0x7f;
  	voice->GVSel = 1;
  	voice->Pan = 0x7f;		/* mute */
  	voice->Vol = 0x3ff;		/* mute */
  	voice->EC = 0;
  	voice->Alpha = 0;
  	voice->FMS = 0;
  	voice->Attribute = 0;
  
  	snd_trident_write_voice_regs(trident, voice);
  
  	spin_unlock_irq(&trident->reg_lock);
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_si7018_capture_hw_params
    
     Description: Set the hardware parameters for the capture device.
    
     Parameters:  substream  - PCM substream class
  		hw_params  - hardware parameters
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1136
1137
  static int snd_trident_si7018_capture_hw_params(struct snd_pcm_substream *substream,
  						struct snd_pcm_hw_params *hw_params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
  {
  	int err;
  
  	if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  		return err;
  
  	return snd_trident_allocate_evoice(substream, hw_params);
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_si7018_capture_hw_free
    
     Description: Release the hardware resources for the capture device.
    
     Parameters:  substream  - PCM substream class
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1157
  static int snd_trident_si7018_capture_hw_free(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1158
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1159
1160
1161
1162
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_trident_voice *voice = runtime->private_data;
  	struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
  
  	snd_pcm_lib_free_pages(substream);
  	if (evoice != NULL) {
  		snd_trident_free_voice(trident, evoice);
  		voice->extra = NULL;
  	}
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_si7018_capture_prepare
    
     Description: Prepare capture device for playback.
    
     Parameters:  substream  - PCM substream class
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1182
  static int snd_trident_si7018_capture_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1183
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1184
1185
1186
1187
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_trident_voice *voice = runtime->private_data;
  	struct snd_trident_voice *evoice = voice->extra;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
  
  	spin_lock_irq(&trident->reg_lock);
  
  	voice->LBA = runtime->dma_addr;
  	voice->Delta = snd_trident_convert_adc_rate(runtime->rate);
  	voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  
  	// Set voice parameters
  	voice->CSO = 0;
  	voice->ESO = runtime->buffer_size - 1;		/* in samples */
  	voice->CTRL = snd_trident_control_mode(substream);
  	voice->FMC = 0;
  	voice->RVol = 0;
  	voice->CVol = 0;
  	voice->GVSel = 1;
  	voice->Pan = T4D_DEFAULT_PCM_PAN;
  	voice->Vol = 0;
  	voice->EC = 0;
  	voice->Alpha = 0;
  	voice->FMS = 0;
  
  	voice->Attribute = (2 << (30-16)) |
  			   (2 << (26-16)) |
  			   (2 << (24-16)) |
  			   (1 << (23-16));
  
  	snd_trident_write_voice_regs(trident, voice);
  
  	if (evoice != NULL) {
  		evoice->Delta = snd_trident_convert_rate(runtime->rate);
  		evoice->spurious_threshold = voice->spurious_threshold;
  		evoice->LBA = voice->LBA;
  		evoice->CSO = 0;
  		evoice->ESO = (runtime->period_size * 2) + 20 - 1; /* in samples, 20 means correction */
  		evoice->CTRL = voice->CTRL;
  		evoice->FMC = 3;
  		evoice->GVSel = 0;
  		evoice->EC = 0;
  		evoice->Alpha = 0;
  		evoice->FMS = 0;
  		evoice->Vol = 0x3ff;			/* mute */
  		evoice->RVol = evoice->CVol = 0x7f;	/* mute */
  		evoice->Pan = 0x7f;			/* mute */
  		evoice->Attribute = 0;
  		snd_trident_write_voice_regs(trident, evoice);
  		evoice->isync2 = 1;
  		evoice->isync_mark = runtime->period_size;
  		evoice->ESO = (runtime->period_size * 2) - 1;
  	}
  	
  	spin_unlock_irq(&trident->reg_lock);
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_foldback_prepare
    
     Description: Prepare foldback capture device for playback.
    
     Parameters:  substream  - PCM substream class
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1252
  static int snd_trident_foldback_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1253
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1254
1255
1256
1257
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_trident_voice *voice = runtime->private_data;
  	struct snd_trident_voice *evoice = voice->extra;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
  
  	spin_lock_irq(&trident->reg_lock);
  
  	/* Set channel buffer Address */
  	if (voice->memblk)
  		voice->LBA = voice->memblk->offset;
  	else
  		voice->LBA = runtime->dma_addr;
  
  	/* set target ESO for channel */
  	voice->ESO = runtime->buffer_size - 1;	/* in samples */
  
  	/* set sample rate */
  	voice->Delta = 0x1000;
  	voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
  
  	voice->CSO = 0;
  	voice->CTRL = snd_trident_control_mode(substream);
  	voice->FMC = 3;
  	voice->RVol = 0x7f;
  	voice->CVol = 0x7f;
  	voice->GVSel = 1;
  	voice->Pan = 0x7f;	/* mute */
  	voice->Vol = 0x3ff;	/* mute */
  	voice->EC = 0;
  	voice->Alpha = 0;
  	voice->FMS = 0;
  	voice->Attribute = 0;
  
  	/* set up capture channel */
  	outb(((voice->number & 0x3f) | 0x80), TRID_REG(trident, T4D_RCI + voice->foldback_chan));
  
  	snd_trident_write_voice_regs(trident, voice);
  
  	if (evoice != NULL) {
  		evoice->Delta = voice->Delta;
  		evoice->spurious_threshold = voice->spurious_threshold;
  		evoice->LBA = voice->LBA;
  		evoice->CSO = 0;
  		evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
  		evoice->CTRL = voice->CTRL;
  		evoice->FMC = 3;
  		evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
  		evoice->EC = 0;
  		evoice->Alpha = 0;
  		evoice->FMS = 0;
  		evoice->Vol = 0x3ff;			/* mute */
  		evoice->RVol = evoice->CVol = 0x7f;	/* mute */
  		evoice->Pan = 0x7f;			/* mute */
  		evoice->Attribute = 0;
  		snd_trident_write_voice_regs(trident, evoice);
  		evoice->isync2 = 1;
  		evoice->isync_mark = runtime->period_size;
  		evoice->ESO = (runtime->period_size * 2) - 1;
  	}
  
  	spin_unlock_irq(&trident->reg_lock);
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_spdif_hw_params
    
     Description: Set the hardware parameters for the spdif device.
    
     Parameters:  substream  - PCM substream class
  		hw_params  - hardware parameters
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1329
1330
  static int snd_trident_spdif_hw_params(struct snd_pcm_substream *substream,
  				       struct snd_pcm_hw_params *hw_params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1331
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1332
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
  	unsigned int old_bits = 0, change = 0;
  	int err;
  
  	err = snd_trident_allocate_pcm_mem(substream, hw_params);
  	if (err < 0)
  		return err;
  
  	if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  		err = snd_trident_allocate_evoice(substream, hw_params);
  		if (err < 0)
  			return err;
  	}
  
  	/* prepare SPDIF channel */
  	spin_lock_irq(&trident->reg_lock);
  	old_bits = trident->spdif_pcm_bits;
  	if (old_bits & IEC958_AES0_PROFESSIONAL)
  		trident->spdif_pcm_bits &= ~IEC958_AES0_PRO_FS;
  	else
  		trident->spdif_pcm_bits &= ~(IEC958_AES3_CON_FS << 24);
  	if (params_rate(hw_params) >= 48000) {
  		trident->spdif_pcm_ctrl = 0x3c;	// 48000 Hz
  		trident->spdif_pcm_bits |=
  			trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
  				IEC958_AES0_PRO_FS_48000 :
  				(IEC958_AES3_CON_FS_48000 << 24);
  	}
  	else if (params_rate(hw_params) >= 44100) {
  		trident->spdif_pcm_ctrl = 0x3e;	// 44100 Hz
  		trident->spdif_pcm_bits |=
  			trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
  				IEC958_AES0_PRO_FS_44100 :
  				(IEC958_AES3_CON_FS_44100 << 24);
  	}
  	else {
  		trident->spdif_pcm_ctrl = 0x3d;	// 32000 Hz
  		trident->spdif_pcm_bits |=
  			trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
  				IEC958_AES0_PRO_FS_32000 :
  				(IEC958_AES3_CON_FS_32000 << 24);
  	}
  	change = old_bits != trident->spdif_pcm_bits;
  	spin_unlock_irq(&trident->reg_lock);
  
  	if (change)
  		snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE, &trident->spdif_pcm_ctl->id);
  
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_spdif_prepare
    
     Description: Prepare SPDIF device for playback.
    
     Parameters:  substream  - PCM substream class
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1393
  static int snd_trident_spdif_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1394
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1395
1396
1397
1398
1399
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_trident_voice *voice = runtime->private_data;
  	struct snd_trident_voice *evoice = voice->extra;
  	struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
  	unsigned int RESO, LBAO;
  	unsigned int temp;
  
  	spin_lock_irq(&trident->reg_lock);
  
  	if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  
  		/* set delta (rate) value */
  		voice->Delta = snd_trident_convert_rate(runtime->rate);
  		voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  
  		/* set Loop Back Address */
  		LBAO = runtime->dma_addr;
  		if (voice->memblk)
  			voice->LBA = voice->memblk->offset;
  		else
  			voice->LBA = LBAO;
  
  		voice->isync = 1;
  		voice->isync3 = 1;
  		voice->isync_mark = runtime->period_size;
  		voice->isync_max = runtime->buffer_size;
  
  		/* set target ESO for channel */
  		RESO = runtime->buffer_size - 1;
  		voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
  
  		/* set ctrl mode */
  		voice->CTRL = snd_trident_control_mode(substream);
  
  		voice->FMC = 3;
  		voice->RVol = 0x7f;
  		voice->CVol = 0x7f;
  		voice->GVSel = 1;
  		voice->Pan = 0x7f;
  		voice->Vol = 0x3ff;
  		voice->EC = 0;
  		voice->CSO = 0;
  		voice->Alpha = 0;
  		voice->FMS = 0;
  		voice->Attribute = 0;
  
  		/* prepare surrogate IRQ channel */
  		snd_trident_write_voice_regs(trident, voice);
  
  		outw((RESO & 0xffff), TRID_REG(trident, NX_SPESO));
  		outb((RESO >> 16), TRID_REG(trident, NX_SPESO + 2));
  		outl((LBAO & 0xfffffffc), TRID_REG(trident, NX_SPLBA));
  		outw((voice->CSO & 0xffff), TRID_REG(trident, NX_SPCTRL_SPCSO));
  		outb((voice->CSO >> 16), TRID_REG(trident, NX_SPCTRL_SPCSO + 2));
  
  		/* set SPDIF setting */
  		outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  		outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
  
  	} else {	/* SiS */
  	
  		/* set delta (rate) value */
  		voice->Delta = 0x800;
  		voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
  
  		/* set Loop Begin Address */
  		if (voice->memblk)
  			voice->LBA = voice->memblk->offset;
  		else
  			voice->LBA = runtime->dma_addr;
  
  		voice->CSO = 0;
  		voice->ESO = runtime->buffer_size - 1;	/* in samples */
  		voice->CTRL = snd_trident_control_mode(substream);
  		voice->FMC = 3;
  		voice->GVSel = 1;
  		voice->EC = 0;
  		voice->Alpha = 0;
  		voice->FMS = 0;
  		voice->Vol = mix->vol;
  		voice->RVol = mix->rvol;
  		voice->CVol = mix->cvol;
  		voice->Pan = mix->pan;
  		voice->Attribute = (1<<(30-16))|(7<<(26-16))|
  				   (0<<(24-16))|(0<<(19-16));
  
  		snd_trident_write_voice_regs(trident, voice);
  
  		if (evoice != NULL) {
  			evoice->Delta = voice->Delta;
  			evoice->spurious_threshold = voice->spurious_threshold;
  			evoice->LBA = voice->LBA;
  			evoice->CSO = 0;
  			evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
  			evoice->CTRL = voice->CTRL;
  			evoice->FMC = 3;
  			evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
  			evoice->EC = 0;
  			evoice->Alpha = 0;
  			evoice->FMS = 0;
  			evoice->Vol = 0x3ff;			/* mute */
  			evoice->RVol = evoice->CVol = 0x7f;	/* mute */
  			evoice->Pan = 0x7f;			/* mute */
  			evoice->Attribute = 0;
  			snd_trident_write_voice_regs(trident, evoice);
  			evoice->isync2 = 1;
  			evoice->isync_mark = runtime->period_size;
  			evoice->ESO = (runtime->period_size * 2) - 1;
  		}
  
  		outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
  		temp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
  		temp &= ~(1<<19);
  		outl(temp, TRID_REG(trident, T4D_LFO_GC_CIR));
  		temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  		temp |= SPDIF_EN;
  		outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  	}
  
  	spin_unlock_irq(&trident->reg_lock);
  
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_trigger
    
     Description: Start/stop devices
    
     Parameters:  substream  - PCM substream class
     		cmd	- trigger command (STOP, GO)
    
     Returns:     Error status
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1531
  static int snd_trident_trigger(struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1532
1533
1534
  			       int cmd)
  				    
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1535
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1536
  	struct snd_pcm_substream *s;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1537
  	unsigned int what, whati, capture_flag, spdif_flag;
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1538
  	struct snd_trident_voice *voice, *evoice;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
  	unsigned int val, go;
  
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
  	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  	case SNDRV_PCM_TRIGGER_RESUME:
  		go = 1;
  		break;
  	case SNDRV_PCM_TRIGGER_STOP:
  	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  	case SNDRV_PCM_TRIGGER_SUSPEND:
  		go = 0;
  		break;
  	default:
  		return -EINVAL;
  	}
  	what = whati = capture_flag = spdif_flag = 0;
  	spin_lock(&trident->reg_lock);
  	val = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
ef991b95a   Takashi Iwai   [ALSA] Add snd_pc...
1558
  	snd_pcm_group_for_each_entry(s, substream) {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1559
1560
  		if ((struct snd_trident *) snd_pcm_substream_chip(s) == trident) {
  			voice = s->runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
  			evoice = voice->extra;
  			what |= 1 << (voice->number & 0x1f);
  			if (evoice == NULL) {
  				whati |= 1 << (voice->number & 0x1f);
  			} else {
  				what |= 1 << (evoice->number & 0x1f);
  				whati |= 1 << (evoice->number & 0x1f);
  				if (go)
  					evoice->stimer = val;
  			}
  			if (go) {
  				voice->running = 1;
  				voice->stimer = val;
  			} else {
  				voice->running = 0;
  			}
  			snd_pcm_trigger_done(s, substream);
  			if (voice->capture)
  				capture_flag = 1;
  			if (voice->spdif)
  				spdif_flag = 1;
  		}
  	}
  	if (spdif_flag) {
  		if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  			outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
981bcead3   Pierre Ossman   ALSA: trident - p...
1587
1588
1589
1590
  			val = trident->spdif_pcm_ctrl;
  			if (!go)
  				val &= ~(0x28);
  			outb(val, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
  		} else {
  			outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
  			val = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) | SPDIF_EN;
  			outl(val, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  		}
  	}
  	if (!go)
  		outl(what, TRID_REG(trident, T4D_STOP_B));
  	val = inl(TRID_REG(trident, T4D_AINTEN_B));
  	if (go) {
  		val |= whati;
  	} else {
  		val &= ~whati;
  	}
  	outl(val, TRID_REG(trident, T4D_AINTEN_B));
  	if (go) {
  		outl(what, TRID_REG(trident, T4D_START_B));
  
  		if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
  			outb(trident->bDMAStart, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
  	} else {
  		if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
  			outb(0x00, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
  	}
  	spin_unlock(&trident->reg_lock);
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_playback_pointer
    
     Description: This routine return the playback position
                  
     Parameters:	substream  - PCM substream class
  
     Returns:     position of buffer
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1629
  static snd_pcm_uframes_t snd_trident_playback_pointer(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1630
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1631
1632
1633
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_trident_voice *voice = runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
  	unsigned int cso;
  
  	if (!voice->running)
  		return 0;
  
  	spin_lock(&trident->reg_lock);
  
  	outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  
  	if (trident->device != TRIDENT_DEVICE_ID_NX) {
  		cso = inw(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS + 2));
  	} else {		// ID_4DWAVE_NX
  		cso = (unsigned int) inl(TRID_REG(trident, CH_NX_DELTA_CSO)) & 0x00ffffff;
  	}
  
  	spin_unlock(&trident->reg_lock);
  
  	if (cso >= runtime->buffer_size)
  		cso = 0;
  
  	return cso;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_capture_pointer
    
     Description: This routine return the capture position
                  
561de31a2   Joe Perches   [ALSA] sound/: Sp...
1662
     Parameters:   pcm1    - PCM device class
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1663
1664
1665
1666
  
     Returns:     position of buffer
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1667
  static snd_pcm_uframes_t snd_trident_capture_pointer(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1668
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1669
1670
1671
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_trident_voice *voice = runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
  	unsigned int result;
  
  	if (!voice->running)
  		return 0;
  
  	result = inw(TRID_REG(trident, T4D_SBBL_SBCL));
  	if (runtime->channels > 1)
  		result >>= 1;
  	if (result > 0)
  		result = runtime->buffer_size - result;
  
  	return result;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_spdif_pointer
    
     Description: This routine return the SPDIF playback position
                  
     Parameters:	substream  - PCM substream class
  
     Returns:     position of buffer
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1696
  static snd_pcm_uframes_t snd_trident_spdif_pointer(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1697
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1698
1699
1700
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_trident_voice *voice = runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
  	unsigned int result;
  
  	if (!voice->running)
  		return 0;
  
  	result = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
  
  	return result;
  }
  
  /*
   *  Playback support device description
   */
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1714
  static struct snd_pcm_hardware snd_trident_playback =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1715
1716
1717
1718
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
  				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
41e4845c4   Jaroslav Kysela   [ALSA] PCM resume...
1719
  				 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
  	.formats =		(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  				 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  	.rate_min =		4000,
  	.rate_max =		48000,
  	.channels_min =		1,
  	.channels_max =		2,
  	.buffer_bytes_max =	(256*1024),
  	.period_bytes_min =	64,
  	.period_bytes_max =	(256*1024),
  	.periods_min =		1,
  	.periods_max =		1024,
  	.fifo_size =		0,
  };
  
  /*
   *  Capture support device description
   */
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1738
  static struct snd_pcm_hardware snd_trident_capture =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1739
1740
1741
1742
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
  				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
41e4845c4   Jaroslav Kysela   [ALSA] PCM resume...
1743
  				 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
  	.formats =		(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  				 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  	.rate_min =		4000,
  	.rate_max =		48000,
  	.channels_min =		1,
  	.channels_max =		2,
  	.buffer_bytes_max =	(128*1024),
  	.period_bytes_min =	64,
  	.period_bytes_max =	(128*1024),
  	.periods_min =		1,
  	.periods_max =		1024,
  	.fifo_size =		0,
  };
  
  /*
   *  Foldback capture support device description
   */
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1762
  static struct snd_pcm_hardware snd_trident_foldback =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1763
1764
1765
1766
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
  				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
41e4845c4   Jaroslav Kysela   [ALSA] PCM resume...
1767
  				 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
  	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
  	.rates =		SNDRV_PCM_RATE_48000,
  	.rate_min =		48000,
  	.rate_max =		48000,
  	.channels_min =		2,
  	.channels_max =		2,
  	.buffer_bytes_max =	(128*1024),
  	.period_bytes_min =	64,
  	.period_bytes_max =	(128*1024),
  	.periods_min =		1,
  	.periods_max =		1024,
  	.fifo_size =		0,
  };
  
  /*
   *  SPDIF playback support device description
   */
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1785
  static struct snd_pcm_hardware snd_trident_spdif =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1786
1787
1788
1789
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
  				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
41e4845c4   Jaroslav Kysela   [ALSA] PCM resume...
1790
  				 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
  	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
  	.rates =		(SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  				 SNDRV_PCM_RATE_48000),
  	.rate_min =		32000,
  	.rate_max =		48000,
  	.channels_min =		2,
  	.channels_max =		2,
  	.buffer_bytes_max =	(128*1024),
  	.period_bytes_min =	64,
  	.period_bytes_max =	(128*1024),
  	.periods_min =		1,
  	.periods_max =		1024,
  	.fifo_size =		0,
  };
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1805
  static struct snd_pcm_hardware snd_trident_spdif_7018 =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1806
1807
1808
1809
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
  				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
41e4845c4   Jaroslav Kysela   [ALSA] PCM resume...
1810
  				 SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
  	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
  	.rates =		SNDRV_PCM_RATE_48000,
  	.rate_min =		48000,
  	.rate_max =		48000,
  	.channels_min =		2,
  	.channels_max =		2,
  	.buffer_bytes_max =	(128*1024),
  	.period_bytes_min =	64,
  	.period_bytes_max =	(128*1024),
  	.periods_min =		1,
  	.periods_max =		1024,
  	.fifo_size =		0,
  };
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1824
  static void snd_trident_pcm_free_substream(struct snd_pcm_runtime *runtime)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1825
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1826
1827
  	struct snd_trident_voice *voice = runtime->private_data;
  	struct snd_trident *trident;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1828
1829
1830
1831
1832
1833
  
  	if (voice) {
  		trident = voice->trident;
  		snd_trident_free_voice(trident, voice);
  	}
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1834
  static int snd_trident_playback_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1835
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1836
1837
1838
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_trident_voice *voice;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
  
  	voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  	if (voice == NULL)
  		return -EAGAIN;
  	snd_trident_pcm_mixer_build(trident, voice, substream);
  	voice->substream = substream;
  	runtime->private_data = voice;
  	runtime->private_free = snd_trident_pcm_free_substream;
  	runtime->hw = snd_trident_playback;
  	snd_pcm_set_sync(substream);
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_playback_close
    
     Description: This routine will close the 4DWave playback device. For now 
                  we will simply free the dma transfer buffer.
                  
     Parameters:	substream  - PCM substream class
  
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1862
  static int snd_trident_playback_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1863
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1864
1865
1866
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct snd_trident_voice *voice = runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
  
  	snd_trident_pcm_mixer_free(trident, voice, substream);
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_spdif_open
    
     Description: This routine will open the 4DWave SPDIF device.
  
     Parameters:	substream  - PCM substream class
  
     Returns:     status  - success or failure flag
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1882
  static int snd_trident_spdif_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1883
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1884
1885
1886
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_trident_voice *voice;
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
  	
  	voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  	if (voice == NULL)
  		return -EAGAIN;
  	voice->spdif = 1;
  	voice->substream = substream;
  	spin_lock_irq(&trident->reg_lock);
  	trident->spdif_pcm_bits = trident->spdif_bits;
  	spin_unlock_irq(&trident->reg_lock);
  
  	runtime->private_data = voice;
  	runtime->private_free = snd_trident_pcm_free_substream;
  	if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  		runtime->hw = snd_trident_spdif;
  	} else {
  		runtime->hw = snd_trident_spdif_7018;
  	}
  
  	trident->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  	snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
  		       SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
  
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  	return 0;
  }
  
  
  /*---------------------------------------------------------------------------
     snd_trident_spdif_close
    
     Description: This routine will close the 4DWave SPDIF device.
                  
     Parameters:	substream  - PCM substream class
  
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1922
  static int snd_trident_spdif_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1923
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1924
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
  	unsigned int temp;
  
  	spin_lock_irq(&trident->reg_lock);
  	// restore default SPDIF setting
  	if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  		outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  		outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  	} else {
  		outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  		temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  		if (trident->spdif_ctrl) {
  			temp |= SPDIF_EN;
  		} else {
  			temp &= ~SPDIF_EN;
  		}
  		outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  	}
  	spin_unlock_irq(&trident->reg_lock);
  	trident->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  	snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
  		       SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_capture_open
    
     Description: This routine will open the 4DWave capture device.
  
     Parameters:	substream  - PCM substream class
  
     Returns:     status  - success or failure flag
  
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1959
  static int snd_trident_capture_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1960
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1961
1962
1963
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_trident_voice *voice;
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
  
  	voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  	if (voice == NULL)
  		return -EAGAIN;
  	voice->capture = 1;
  	voice->substream = substream;
  	runtime->private_data = voice;
  	runtime->private_free = snd_trident_pcm_free_substream;
  	runtime->hw = snd_trident_capture;
  	snd_pcm_set_sync(substream);
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_capture_close
    
     Description: This routine will close the 4DWave capture device. For now 
                  we will simply free the dma transfer buffer.
                  
     Parameters:	substream  - PCM substream class
  
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
1987
  static int snd_trident_capture_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
  {
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_foldback_open
    
     Description: This routine will open the 4DWave foldback capture device.
  
     Parameters:	substream  - PCM substream class
  
     Returns:     status  - success or failure flag
  
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2002
  static int snd_trident_foldback_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2003
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2004
2005
2006
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_trident_voice *voice;
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
  
  	voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  	if (voice == NULL)
  		return -EAGAIN;
  	voice->foldback_chan = substream->number;
  	voice->substream = substream;
  	runtime->private_data = voice;
  	runtime->private_free = snd_trident_pcm_free_substream;
  	runtime->hw = snd_trident_foldback;
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_foldback_close
    
     Description: This routine will close the 4DWave foldback capture device. 
  		For now we will simply free the dma transfer buffer.
                  
     Parameters:	substream  - PCM substream class
  
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2029
  static int snd_trident_foldback_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2030
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2031
2032
2033
2034
  	struct snd_trident *trident = snd_pcm_substream_chip(substream);
  	struct snd_trident_voice *voice;
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	voice = runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
  	
  	/* stop capture channel */
  	spin_lock_irq(&trident->reg_lock);
  	outb(0x00, TRID_REG(trident, T4D_RCI + voice->foldback_chan));
  	spin_unlock_irq(&trident->reg_lock);
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     PCM operations
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2046
  static struct snd_pcm_ops snd_trident_playback_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2047
2048
2049
2050
2051
2052
2053
2054
2055
  	.open =		snd_trident_playback_open,
  	.close =	snd_trident_playback_close,
  	.ioctl =	snd_trident_ioctl,
  	.hw_params =	snd_trident_hw_params,
  	.hw_free =	snd_trident_hw_free,
  	.prepare =	snd_trident_playback_prepare,
  	.trigger =	snd_trident_trigger,
  	.pointer =	snd_trident_playback_pointer,
  };
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2056
  static struct snd_pcm_ops snd_trident_nx_playback_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
  	.open =		snd_trident_playback_open,
  	.close =	snd_trident_playback_close,
  	.ioctl =	snd_trident_ioctl,
  	.hw_params =	snd_trident_hw_params,
  	.hw_free =	snd_trident_hw_free,
  	.prepare =	snd_trident_playback_prepare,
  	.trigger =	snd_trident_trigger,
  	.pointer =	snd_trident_playback_pointer,
  	.page =		snd_pcm_sgbuf_ops_page,
  };
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2067
  static struct snd_pcm_ops snd_trident_capture_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2068
2069
2070
2071
2072
2073
2074
2075
2076
  	.open =		snd_trident_capture_open,
  	.close =	snd_trident_capture_close,
  	.ioctl =	snd_trident_ioctl,
  	.hw_params =	snd_trident_capture_hw_params,
  	.hw_free =	snd_trident_hw_free,
  	.prepare =	snd_trident_capture_prepare,
  	.trigger =	snd_trident_trigger,
  	.pointer =	snd_trident_capture_pointer,
  };
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2077
  static struct snd_pcm_ops snd_trident_si7018_capture_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2078
2079
2080
2081
2082
2083
2084
2085
2086
  	.open =		snd_trident_capture_open,
  	.close =	snd_trident_capture_close,
  	.ioctl =	snd_trident_ioctl,
  	.hw_params =	snd_trident_si7018_capture_hw_params,
  	.hw_free =	snd_trident_si7018_capture_hw_free,
  	.prepare =	snd_trident_si7018_capture_prepare,
  	.trigger =	snd_trident_trigger,
  	.pointer =	snd_trident_playback_pointer,
  };
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2087
  static struct snd_pcm_ops snd_trident_foldback_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2088
2089
2090
2091
2092
2093
2094
2095
2096
  	.open =		snd_trident_foldback_open,
  	.close =	snd_trident_foldback_close,
  	.ioctl =	snd_trident_ioctl,
  	.hw_params =	snd_trident_hw_params,
  	.hw_free =	snd_trident_hw_free,
  	.prepare =	snd_trident_foldback_prepare,
  	.trigger =	snd_trident_trigger,
  	.pointer =	snd_trident_playback_pointer,
  };
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2097
  static struct snd_pcm_ops snd_trident_nx_foldback_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
  	.open =		snd_trident_foldback_open,
  	.close =	snd_trident_foldback_close,
  	.ioctl =	snd_trident_ioctl,
  	.hw_params =	snd_trident_hw_params,
  	.hw_free =	snd_trident_hw_free,
  	.prepare =	snd_trident_foldback_prepare,
  	.trigger =	snd_trident_trigger,
  	.pointer =	snd_trident_playback_pointer,
  	.page =		snd_pcm_sgbuf_ops_page,
  };
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2108
  static struct snd_pcm_ops snd_trident_spdif_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2109
2110
2111
2112
2113
2114
2115
2116
2117
  	.open =		snd_trident_spdif_open,
  	.close =	snd_trident_spdif_close,
  	.ioctl =	snd_trident_ioctl,
  	.hw_params =	snd_trident_spdif_hw_params,
  	.hw_free =	snd_trident_hw_free,
  	.prepare =	snd_trident_spdif_prepare,
  	.trigger =	snd_trident_trigger,
  	.pointer =	snd_trident_spdif_pointer,
  };
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2118
  static struct snd_pcm_ops snd_trident_spdif_7018_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
  	.open =		snd_trident_spdif_open,
  	.close =	snd_trident_spdif_close,
  	.ioctl =	snd_trident_ioctl,
  	.hw_params =	snd_trident_spdif_hw_params,
  	.hw_free =	snd_trident_hw_free,
  	.prepare =	snd_trident_spdif_prepare,
  	.trigger =	snd_trident_trigger,
  	.pointer =	snd_trident_playback_pointer,
  };
  
  /*---------------------------------------------------------------------------
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2130
2131
2132
2133
     snd_trident_pcm
    
     Description: This routine registers the 4DWave device for PCM support.
                  
561de31a2   Joe Perches   [ALSA] sound/: Sp...
2134
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2135
2136
2137
2138
  
     Returns:     None
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2139
2140
  int __devinit snd_trident_pcm(struct snd_trident * trident,
  			      int device, struct snd_pcm ** rpcm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2141
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2142
  	struct snd_pcm *pcm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2143
2144
2145
2146
2147
2148
2149
2150
  	int err;
  
  	if (rpcm)
  		*rpcm = NULL;
  	if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, trident->ChanPCM, 1, &pcm)) < 0)
  		return err;
  
  	pcm->private_data = trident;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
  
  	if (trident->tlb.entries) {
  		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_nx_playback_ops);
  	} else {
  		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_playback_ops);
  	}
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  			trident->device != TRIDENT_DEVICE_ID_SI7018 ?
  			&snd_trident_capture_ops :
  			&snd_trident_si7018_capture_ops);
  
  	pcm->info_flags = 0;
  	pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  	strcpy(pcm->name, "Trident 4DWave");
  	trident->pcm = pcm;
  
  	if (trident->tlb.entries) {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2168
  		struct snd_pcm_substream *substream;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
  		for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
  			snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
  						      snd_dma_pci_data(trident->pci),
  						      64*1024, 128*1024);
  		snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  					      SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
  					      64*1024, 128*1024);
  	} else {
  		snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  						      snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
  	}
  
  	if (rpcm)
  		*rpcm = pcm;
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_foldback_pcm
    
     Description: This routine registers the 4DWave device for foldback PCM support.
                  
561de31a2   Joe Perches   [ALSA] sound/: Sp...
2191
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2192
2193
2194
2195
  
     Returns:     None
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2196
2197
  int __devinit snd_trident_foldback_pcm(struct snd_trident * trident,
  				       int device, struct snd_pcm ** rpcm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2198
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2199
  	struct snd_pcm *foldback;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2200
2201
  	int err;
  	int num_chan = 3;
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2202
  	struct snd_pcm_substream *substream;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2203
2204
2205
2206
2207
2208
2209
2210
2211
  
  	if (rpcm)
  		*rpcm = NULL;
  	if (trident->device == TRIDENT_DEVICE_ID_NX)
  		num_chan = 4;
  	if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, 0, num_chan, &foldback)) < 0)
  		return err;
  
  	foldback->private_data = trident;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
  	if (trident->tlb.entries)
  		snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_nx_foldback_ops);
  	else
  		snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_foldback_ops);
  	foldback->info_flags = 0;
  	strcpy(foldback->name, "Trident 4DWave");
  	substream = foldback->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  	strcpy(substream->name, "Front Mixer");
  	substream = substream->next;
  	strcpy(substream->name, "Reverb Mixer");
  	substream = substream->next;
  	strcpy(substream->name, "Chorus Mixer");
  	if (num_chan == 4) {
  		substream = substream->next;
  		strcpy(substream->name, "Second AC'97 ADC");
  	}
  	trident->foldback = foldback;
  
  	if (trident->tlb.entries)
  		snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV_SG,
  						      snd_dma_pci_data(trident->pci), 0, 128*1024);
  	else
  		snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV,
  						      snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
  
  	if (rpcm)
  		*rpcm = foldback;
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_spdif
    
     Description: This routine registers the 4DWave-NX device for SPDIF support.
                  
561de31a2   Joe Perches   [ALSA] sound/: Sp...
2247
     Parameters:  trident - pointer to target device class for 4DWave-NX.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2248
2249
2250
2251
  
     Returns:     None
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2252
2253
  int __devinit snd_trident_spdif_pcm(struct snd_trident * trident,
  				    int device, struct snd_pcm ** rpcm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2254
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2255
  	struct snd_pcm *spdif;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2256
2257
2258
2259
2260
2261
2262
2263
  	int err;
  
  	if (rpcm)
  		*rpcm = NULL;
  	if ((err = snd_pcm_new(trident->card, "trident_dx_nx IEC958", device, 1, 0, &spdif)) < 0)
  		return err;
  
  	spdif->private_data = trident;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
  	if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  		snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_ops);
  	} else {
  		snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_7018_ops);
  	}
  	spdif->info_flags = 0;
  	strcpy(spdif->name, "Trident 4DWave IEC958");
  	trident->spdif = spdif;
  
  	snd_pcm_lib_preallocate_pages_for_all(spdif, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
  
  	if (rpcm)
  		*rpcm = spdif;
  	return 0;
  }
  
  /*
   *  Mixer part
   */
  
  
  /*---------------------------------------------------------------------------
      snd_trident_spdif_control
  
      Description: enable/disable S/PDIF out from ac97 mixer
    ---------------------------------------------------------------------------*/
a5ce88909   Takashi Iwai   [ALSA] Clean up w...
2290
  #define snd_trident_spdif_control_info	snd_ctl_boolean_mono_info
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2291

bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2292
2293
  static int snd_trident_spdif_control_get(struct snd_kcontrol *kcontrol,
  					 struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2294
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2295
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2296
2297
2298
2299
2300
2301
2302
2303
  	unsigned char val;
  
  	spin_lock_irq(&trident->reg_lock);
  	val = trident->spdif_ctrl;
  	ucontrol->value.integer.value[0] = val == kcontrol->private_value;
  	spin_unlock_irq(&trident->reg_lock);
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2304
2305
  static int snd_trident_spdif_control_put(struct snd_kcontrol *kcontrol,
  					 struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2306
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2307
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
  	unsigned char val;
  	int change;
  
  	val = ucontrol->value.integer.value[0] ? (unsigned char) kcontrol->private_value : 0x00;
  	spin_lock_irq(&trident->reg_lock);
  	/* S/PDIF C Channel bits 0-31 : 48khz, SCMS disabled */
  	change = trident->spdif_ctrl != val;
  	trident->spdif_ctrl = val;
  	if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  		if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0) {
  			outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  			outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  		}
  	} else {
  		if (trident->spdif == NULL) {
  			unsigned int temp;
  			outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  			temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & ~SPDIF_EN;
  			if (val)
  				temp |= SPDIF_EN;
  			outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  		}
  	}
  	spin_unlock_irq(&trident->reg_lock);
  	return change;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2334
  static struct snd_kcontrol_new snd_trident_spdif_control __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
  {
  	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  	.info =		snd_trident_spdif_control_info,
  	.get =		snd_trident_spdif_control_get,
  	.put =		snd_trident_spdif_control_put,
  	.private_value = 0x28,
  };
  
  /*---------------------------------------------------------------------------
      snd_trident_spdif_default
  
      Description: put/get the S/PDIF default settings
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2349
2350
  static int snd_trident_spdif_default_info(struct snd_kcontrol *kcontrol,
  					  struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2351
2352
2353
2354
2355
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  	uinfo->count = 1;
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2356
2357
  static int snd_trident_spdif_default_get(struct snd_kcontrol *kcontrol,
  					 struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2358
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2359
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2360
2361
2362
2363
2364
2365
2366
2367
2368
  
  	spin_lock_irq(&trident->reg_lock);
  	ucontrol->value.iec958.status[0] = (trident->spdif_bits >> 0) & 0xff;
  	ucontrol->value.iec958.status[1] = (trident->spdif_bits >> 8) & 0xff;
  	ucontrol->value.iec958.status[2] = (trident->spdif_bits >> 16) & 0xff;
  	ucontrol->value.iec958.status[3] = (trident->spdif_bits >> 24) & 0xff;
  	spin_unlock_irq(&trident->reg_lock);
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2369
2370
  static int snd_trident_spdif_default_put(struct snd_kcontrol *kcontrol,
  					 struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2371
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2372
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
  	unsigned int val;
  	int change;
  
  	val = (ucontrol->value.iec958.status[0] << 0) |
  	      (ucontrol->value.iec958.status[1] << 8) |
  	      (ucontrol->value.iec958.status[2] << 16) |
  	      (ucontrol->value.iec958.status[3] << 24);
  	spin_lock_irq(&trident->reg_lock);
  	change = trident->spdif_bits != val;
  	trident->spdif_bits = val;
  	if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  		if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0)
  			outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  	} else {
  		if (trident->spdif == NULL)
  			outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  	}
  	spin_unlock_irq(&trident->reg_lock);
  	return change;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2393
  static struct snd_kcontrol_new snd_trident_spdif_default __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
  {
  	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
  	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  	.info =		snd_trident_spdif_default_info,
  	.get =		snd_trident_spdif_default_get,
  	.put =		snd_trident_spdif_default_put
  };
  
  /*---------------------------------------------------------------------------
      snd_trident_spdif_mask
  
      Description: put/get the S/PDIF mask
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2407
2408
  static int snd_trident_spdif_mask_info(struct snd_kcontrol *kcontrol,
  				       struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2409
2410
2411
2412
2413
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  	uinfo->count = 1;
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2414
2415
  static int snd_trident_spdif_mask_get(struct snd_kcontrol *kcontrol,
  				      struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2416
2417
2418
2419
2420
2421
2422
  {
  	ucontrol->value.iec958.status[0] = 0xff;
  	ucontrol->value.iec958.status[1] = 0xff;
  	ucontrol->value.iec958.status[2] = 0xff;
  	ucontrol->value.iec958.status[3] = 0xff;
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2423
  static struct snd_kcontrol_new snd_trident_spdif_mask __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
  {
  	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
  	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
  	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  	.info =		snd_trident_spdif_mask_info,
  	.get =		snd_trident_spdif_mask_get,
  };
  
  /*---------------------------------------------------------------------------
      snd_trident_spdif_stream
  
      Description: put/get the S/PDIF stream settings
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2437
2438
  static int snd_trident_spdif_stream_info(struct snd_kcontrol *kcontrol,
  					 struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2439
2440
2441
2442
2443
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  	uinfo->count = 1;
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2444
2445
  static int snd_trident_spdif_stream_get(struct snd_kcontrol *kcontrol,
  					struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2446
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2447
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2448
2449
2450
2451
2452
2453
2454
2455
2456
  
  	spin_lock_irq(&trident->reg_lock);
  	ucontrol->value.iec958.status[0] = (trident->spdif_pcm_bits >> 0) & 0xff;
  	ucontrol->value.iec958.status[1] = (trident->spdif_pcm_bits >> 8) & 0xff;
  	ucontrol->value.iec958.status[2] = (trident->spdif_pcm_bits >> 16) & 0xff;
  	ucontrol->value.iec958.status[3] = (trident->spdif_pcm_bits >> 24) & 0xff;
  	spin_unlock_irq(&trident->reg_lock);
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2457
2458
  static int snd_trident_spdif_stream_put(struct snd_kcontrol *kcontrol,
  					struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2459
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2460
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
  	unsigned int val;
  	int change;
  
  	val = (ucontrol->value.iec958.status[0] << 0) |
  	      (ucontrol->value.iec958.status[1] << 8) |
  	      (ucontrol->value.iec958.status[2] << 16) |
  	      (ucontrol->value.iec958.status[3] << 24);
  	spin_lock_irq(&trident->reg_lock);
  	change = trident->spdif_pcm_bits != val;
  	trident->spdif_pcm_bits = val;
  	if (trident->spdif != NULL) {
  		if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  			outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
  		} else {
  			outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  		}
  	}
  	spin_unlock_irq(&trident->reg_lock);
  	return change;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2481
  static struct snd_kcontrol_new snd_trident_spdif_stream __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
  {
  	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
  	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  	.info =		snd_trident_spdif_stream_info,
  	.get =		snd_trident_spdif_stream_get,
  	.put =		snd_trident_spdif_stream_put
  };
  
  /*---------------------------------------------------------------------------
      snd_trident_ac97_control
  
      Description: enable/disable rear path for ac97
    ---------------------------------------------------------------------------*/
a5ce88909   Takashi Iwai   [ALSA] Clean up w...
2496
  #define snd_trident_ac97_control_info	snd_ctl_boolean_mono_info
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2497

bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2498
2499
  static int snd_trident_ac97_control_get(struct snd_kcontrol *kcontrol,
  					struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2500
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2501
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2502
2503
2504
2505
2506
2507
2508
2509
  	unsigned char val;
  
  	spin_lock_irq(&trident->reg_lock);
  	val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  	ucontrol->value.integer.value[0] = (val & (1 << kcontrol->private_value)) ? 1 : 0;
  	spin_unlock_irq(&trident->reg_lock);
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2510
2511
  static int snd_trident_ac97_control_put(struct snd_kcontrol *kcontrol,
  					struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2512
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2513
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
  	unsigned char val;
  	int change = 0;
  
  	spin_lock_irq(&trident->reg_lock);
  	val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  	val &= ~(1 << kcontrol->private_value);
  	if (ucontrol->value.integer.value[0])
  		val |= 1 << kcontrol->private_value;
  	change = val != trident->ac97_ctrl;
  	trident->ac97_ctrl = val;
  	outl(trident->ac97_ctrl = val, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  	spin_unlock_irq(&trident->reg_lock);
  	return change;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2528
  static struct snd_kcontrol_new snd_trident_ac97_rear_control __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
  {
  	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name =         "Rear Path",
  	.info =		snd_trident_ac97_control_info,
  	.get =		snd_trident_ac97_control_get,
  	.put =		snd_trident_ac97_control_put,
  	.private_value = 4,
  };
  
  /*---------------------------------------------------------------------------
      snd_trident_vol_control
  
      Description: wave & music volume control
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2543
2544
  static int snd_trident_vol_control_info(struct snd_kcontrol *kcontrol,
  					struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2545
2546
2547
2548
2549
2550
2551
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 2;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = 255;
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2552
2553
  static int snd_trident_vol_control_get(struct snd_kcontrol *kcontrol,
  				       struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2554
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2555
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2556
2557
2558
2559
2560
2561
2562
  	unsigned int val;
  
  	val = trident->musicvol_wavevol;
  	ucontrol->value.integer.value[0] = 255 - ((val >> kcontrol->private_value) & 0xff);
  	ucontrol->value.integer.value[1] = 255 - ((val >> (kcontrol->private_value + 8)) & 0xff);
  	return 0;
  }
0cb29ea0d   Takashi Iwai   [ALSA] Add even m...
2563
  static const DECLARE_TLV_DB_SCALE(db_scale_gvol, -6375, 25, 0);
a0aef8edf   Takashi Iwai   [ALSA] Add dB sca...
2564

bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2565
2566
  static int snd_trident_vol_control_put(struct snd_kcontrol *kcontrol,
  				       struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2567
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2568
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
  	unsigned int val;
  	int change = 0;
  
  	spin_lock_irq(&trident->reg_lock);
  	val = trident->musicvol_wavevol;
  	val &= ~(0xffff << kcontrol->private_value);
  	val |= ((255 - (ucontrol->value.integer.value[0] & 0xff)) |
  	        ((255 - (ucontrol->value.integer.value[1] & 0xff)) << 8)) << kcontrol->private_value;
  	change = val != trident->musicvol_wavevol;
  	outl(trident->musicvol_wavevol = val, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  	spin_unlock_irq(&trident->reg_lock);
  	return change;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2582
  static struct snd_kcontrol_new snd_trident_vol_music_control __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2583
2584
2585
2586
2587
2588
2589
  {
  	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name =         "Music Playback Volume",
  	.info =		snd_trident_vol_control_info,
  	.get =		snd_trident_vol_control_get,
  	.put =		snd_trident_vol_control_put,
  	.private_value = 16,
a0aef8edf   Takashi Iwai   [ALSA] Add dB sca...
2590
  	.tlv = { .p = db_scale_gvol },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2591
  };
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2592
  static struct snd_kcontrol_new snd_trident_vol_wave_control __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2593
2594
2595
2596
2597
2598
2599
  {
  	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name =         "Wave Playback Volume",
  	.info =		snd_trident_vol_control_info,
  	.get =		snd_trident_vol_control_get,
  	.put =		snd_trident_vol_control_put,
  	.private_value = 0,
a0aef8edf   Takashi Iwai   [ALSA] Add dB sca...
2600
  	.tlv = { .p = db_scale_gvol },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2601
2602
2603
2604
2605
2606
2607
  };
  
  /*---------------------------------------------------------------------------
      snd_trident_pcm_vol_control
  
      Description: PCM front volume control
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2608
2609
  static int snd_trident_pcm_vol_control_info(struct snd_kcontrol *kcontrol,
  					    struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2610
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2611
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2612
2613
2614
2615
2616
2617
2618
2619
2620
  
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 1;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = 255;
  	if (trident->device == TRIDENT_DEVICE_ID_SI7018)
  		uinfo->value.integer.max = 1023;
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2621
2622
  static int snd_trident_pcm_vol_control_get(struct snd_kcontrol *kcontrol,
  					   struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2623
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2624
2625
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  	struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2626
2627
2628
2629
2630
2631
2632
2633
  
  	if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  		ucontrol->value.integer.value[0] = 1023 - mix->vol;
  	} else {
  		ucontrol->value.integer.value[0] = 255 - (mix->vol>>2);
  	}
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2634
2635
  static int snd_trident_pcm_vol_control_put(struct snd_kcontrol *kcontrol,
  					   struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2636
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2637
2638
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  	struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
  	unsigned int val;
  	int change = 0;
  
  	if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  		val = 1023 - (ucontrol->value.integer.value[0] & 1023);
  	} else {
  		val = (255 - (ucontrol->value.integer.value[0] & 255)) << 2;
  	}
  	spin_lock_irq(&trident->reg_lock);
  	change = val != mix->vol;
  	mix->vol = val;
  	if (mix->voice != NULL)
  		snd_trident_write_vol_reg(trident, mix->voice, val);
  	spin_unlock_irq(&trident->reg_lock);
  	return change;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2655
  static struct snd_kcontrol_new snd_trident_pcm_vol_control __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2656
2657
2658
2659
2660
2661
2662
2663
  {
  	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name =         "PCM Front Playback Volume",
  	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  	.count =	32,
  	.info =		snd_trident_pcm_vol_control_info,
  	.get =		snd_trident_pcm_vol_control_get,
  	.put =		snd_trident_pcm_vol_control_put,
a0aef8edf   Takashi Iwai   [ALSA] Add dB sca...
2664
  	/* FIXME: no tlv yet */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2665
2666
2667
2668
2669
2670
2671
  };
  
  /*---------------------------------------------------------------------------
      snd_trident_pcm_pan_control
  
      Description: PCM front pan control
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2672
2673
  static int snd_trident_pcm_pan_control_info(struct snd_kcontrol *kcontrol,
  					    struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2674
2675
2676
2677
2678
2679
2680
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 1;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = 127;
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2681
2682
  static int snd_trident_pcm_pan_control_get(struct snd_kcontrol *kcontrol,
  					   struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2683
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2684
2685
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  	struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2686
2687
2688
2689
2690
2691
2692
2693
2694
  
  	ucontrol->value.integer.value[0] = mix->pan;
  	if (ucontrol->value.integer.value[0] & 0x40) {
  		ucontrol->value.integer.value[0] = (0x3f - (ucontrol->value.integer.value[0] & 0x3f));
  	} else {
  		ucontrol->value.integer.value[0] |= 0x40;
  	}
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2695
2696
  static int snd_trident_pcm_pan_control_put(struct snd_kcontrol *kcontrol,
  					   struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2697
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2698
2699
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  	struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
  	unsigned char val;
  	int change = 0;
  
  	if (ucontrol->value.integer.value[0] & 0x40)
  		val = ucontrol->value.integer.value[0] & 0x3f;
  	else
  		val = (0x3f - (ucontrol->value.integer.value[0] & 0x3f)) | 0x40;
  	spin_lock_irq(&trident->reg_lock);
  	change = val != mix->pan;
  	mix->pan = val;
  	if (mix->voice != NULL)
  		snd_trident_write_pan_reg(trident, mix->voice, val);
  	spin_unlock_irq(&trident->reg_lock);
  	return change;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2715
  static struct snd_kcontrol_new snd_trident_pcm_pan_control __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
  {
  	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name =         "PCM Pan Playback Control",
  	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  	.count =	32,
  	.info =		snd_trident_pcm_pan_control_info,
  	.get =		snd_trident_pcm_pan_control_get,
  	.put =		snd_trident_pcm_pan_control_put,
  };
  
  /*---------------------------------------------------------------------------
      snd_trident_pcm_rvol_control
  
      Description: PCM reverb volume control
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2731
2732
  static int snd_trident_pcm_rvol_control_info(struct snd_kcontrol *kcontrol,
  					     struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2733
2734
2735
2736
2737
2738
2739
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 1;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = 127;
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2740
2741
  static int snd_trident_pcm_rvol_control_get(struct snd_kcontrol *kcontrol,
  					    struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2742
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2743
2744
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  	struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2745
2746
2747
2748
  
  	ucontrol->value.integer.value[0] = 127 - mix->rvol;
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2749
2750
  static int snd_trident_pcm_rvol_control_put(struct snd_kcontrol *kcontrol,
  					    struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2751
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2752
2753
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  	struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
  	unsigned short val;
  	int change = 0;
  
  	val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
  	spin_lock_irq(&trident->reg_lock);
  	change = val != mix->rvol;
  	mix->rvol = val;
  	if (mix->voice != NULL)
  		snd_trident_write_rvol_reg(trident, mix->voice, val);
  	spin_unlock_irq(&trident->reg_lock);
  	return change;
  }
0cb29ea0d   Takashi Iwai   [ALSA] Add even m...
2766
  static const DECLARE_TLV_DB_SCALE(db_scale_crvol, -3175, 25, 1);
a0aef8edf   Takashi Iwai   [ALSA] Add dB sca...
2767

bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2768
  static struct snd_kcontrol_new snd_trident_pcm_rvol_control __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2769
2770
2771
2772
2773
2774
2775
2776
  {
  	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name =         "PCM Reverb Playback Volume",
  	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  	.count = 	32,
  	.info =		snd_trident_pcm_rvol_control_info,
  	.get =		snd_trident_pcm_rvol_control_get,
  	.put =		snd_trident_pcm_rvol_control_put,
a0aef8edf   Takashi Iwai   [ALSA] Add dB sca...
2777
  	.tlv = { .p = db_scale_crvol },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2778
2779
2780
2781
2782
2783
2784
  };
  
  /*---------------------------------------------------------------------------
      snd_trident_pcm_cvol_control
  
      Description: PCM chorus volume control
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2785
2786
  static int snd_trident_pcm_cvol_control_info(struct snd_kcontrol *kcontrol,
  					     struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2787
2788
2789
2790
2791
2792
2793
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 1;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = 127;
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2794
2795
  static int snd_trident_pcm_cvol_control_get(struct snd_kcontrol *kcontrol,
  					    struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2796
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2797
2798
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  	struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2799
2800
2801
2802
  
  	ucontrol->value.integer.value[0] = 127 - mix->cvol;
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2803
2804
  static int snd_trident_pcm_cvol_control_put(struct snd_kcontrol *kcontrol,
  					    struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2805
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2806
2807
  	struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  	struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
  	unsigned short val;
  	int change = 0;
  
  	val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
  	spin_lock_irq(&trident->reg_lock);
  	change = val != mix->cvol;
  	mix->cvol = val;
  	if (mix->voice != NULL)
  		snd_trident_write_cvol_reg(trident, mix->voice, val);
  	spin_unlock_irq(&trident->reg_lock);
  	return change;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2820
  static struct snd_kcontrol_new snd_trident_pcm_cvol_control __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2821
2822
2823
2824
2825
2826
2827
2828
  {
  	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name =         "PCM Chorus Playback Volume",
  	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  	.count =	32,
  	.info =		snd_trident_pcm_cvol_control_info,
  	.get =		snd_trident_pcm_cvol_control_get,
  	.put =		snd_trident_pcm_cvol_control_put,
a0aef8edf   Takashi Iwai   [ALSA] Add dB sca...
2829
  	.tlv = { .p = db_scale_crvol },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2830
  };
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2831
2832
2833
  static void snd_trident_notify_pcm_change1(struct snd_card *card,
  					   struct snd_kcontrol *kctl,
  					   int num, int activate)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2834
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2835
  	struct snd_ctl_elem_id id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2836

7c22f1aaa   Takashi Iwai   [ALSA] Remove snd...
2837
2838
  	if (! kctl)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2839
2840
2841
2842
2843
2844
2845
2846
  	if (activate)
  		kctl->vd[num].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  	else
  		kctl->vd[num].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  	snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
  		       SNDRV_CTL_EVENT_MASK_INFO,
  		       snd_ctl_build_ioff(&id, kctl, num));
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2847
2848
2849
  static void snd_trident_notify_pcm_change(struct snd_trident *trident,
  					  struct snd_trident_pcm_mixer *tmix,
  					  int num, int activate)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2850
2851
2852
2853
2854
2855
  {
  	snd_trident_notify_pcm_change1(trident->card, trident->ctl_vol, num, activate);
  	snd_trident_notify_pcm_change1(trident->card, trident->ctl_pan, num, activate);
  	snd_trident_notify_pcm_change1(trident->card, trident->ctl_rvol, num, activate);
  	snd_trident_notify_pcm_change1(trident->card, trident->ctl_cvol, num, activate);
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2856
2857
2858
  static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
  				       struct snd_trident_voice *voice,
  				       struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2859
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2860
  	struct snd_trident_pcm_mixer *tmix;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2861

da3cec35d   Takashi Iwai   ALSA: Kill snd_as...
2862
2863
  	if (snd_BUG_ON(!trident || !voice || !substream))
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2864
2865
2866
2867
2868
2869
2870
2871
2872
  	tmix = &trident->pcm_mixer[substream->number];
  	tmix->voice = voice;
  	tmix->vol = T4D_DEFAULT_PCM_VOL;
  	tmix->pan = T4D_DEFAULT_PCM_PAN;
  	tmix->rvol = T4D_DEFAULT_PCM_RVOL;
  	tmix->cvol = T4D_DEFAULT_PCM_CVOL;
  	snd_trident_notify_pcm_change(trident, tmix, substream->number, 1);
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2873
  static int snd_trident_pcm_mixer_free(struct snd_trident *trident, struct snd_trident_voice *voice, struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2874
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2875
  	struct snd_trident_pcm_mixer *tmix;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2876

da3cec35d   Takashi Iwai   ALSA: Kill snd_as...
2877
2878
  	if (snd_BUG_ON(!trident || !substream))
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
  	tmix = &trident->pcm_mixer[substream->number];
  	tmix->voice = NULL;
  	snd_trident_notify_pcm_change(trident, tmix, substream->number, 0);
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_mixer
    
     Description: This routine registers the 4DWave device for mixer support.
                  
561de31a2   Joe Perches   [ALSA] sound/: Sp...
2890
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2891
2892
2893
2894
  
     Returns:     None
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2895
  static int __devinit snd_trident_mixer(struct snd_trident * trident, int pcm_spdif_device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2896
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2897
2898
2899
2900
  	struct snd_ac97_template _ac97;
  	struct snd_card *card = trident->card;
  	struct snd_kcontrol *kctl;
  	struct snd_ctl_elem_value *uctl;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2901
  	int idx, err, retries = 2;
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2902
  	static struct snd_ac97_bus_ops ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2903
2904
2905
  		.write = snd_trident_codec_write,
  		.read = snd_trident_codec_read,
  	};
e560d8d83   Takashi Iwai   [ALSA] Replace wi...
2906
  	uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
  	if (!uctl)
  		return -ENOMEM;
  
  	if ((err = snd_ac97_bus(trident->card, 0, &ops, NULL, &trident->ac97_bus)) < 0)
  		goto __out;
  
  	memset(&_ac97, 0, sizeof(_ac97));
  	_ac97.private_data = trident;
  	trident->ac97_detect = 1;
  
        __again:
  	if ((err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97)) < 0) {
  		if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  			if ((err = snd_trident_sis_reset(trident)) < 0)
  				goto __out;
  			if (retries-- > 0)
  				goto __again;
  			err = -EIO;
  		}
  		goto __out;
  	}
  	
  	/* secondary codec? */
  	if (trident->device == TRIDENT_DEVICE_ID_SI7018 &&
  	    (inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0) {
  		_ac97.num = 1;
  		err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97_sec);
  		if (err < 0)
99b359ba1   Takashi Iwai   [ALSA] Add missin...
2935
2936
  			snd_printk(KERN_ERR "SI7018: the secondary codec - invalid access
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2937
2938
  #if 0	// only for my testing purpose --jk
  		{
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2939
  			struct snd_ac97 *mc97;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2940
2941
  			err = snd_ac97_modem(trident->card, &_ac97, &mc97);
  			if (err < 0)
99b359ba1   Takashi Iwai   [ALSA] Add missin...
2942
2943
  				snd_printk(KERN_ERR "snd_ac97_modem returned error %i
  ", err);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
  		}
  #endif
  	}
  	
  	trident->ac97_detect = 0;
  
  	if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_wave_control, trident))) < 0)
  			goto __out;
  		kctl->put(kctl, uctl);
  		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_music_control, trident))) < 0)
  			goto __out;
  		kctl->put(kctl, uctl);
  		outl(trident->musicvol_wavevol = 0x00000000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  	} else {
  		outl(trident->musicvol_wavevol = 0xffff0000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  	}
  
  	for (idx = 0; idx < 32; idx++) {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
2963
  		struct snd_trident_pcm_mixer *tmix;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
  		
  		tmix = &trident->pcm_mixer[idx];
  		tmix->voice = NULL;
  	}
  	if ((trident->ctl_vol = snd_ctl_new1(&snd_trident_pcm_vol_control, trident)) == NULL)
  		goto __nomem;
  	if ((err = snd_ctl_add(card, trident->ctl_vol)))
  		goto __out;
  		
  	if ((trident->ctl_pan = snd_ctl_new1(&snd_trident_pcm_pan_control, trident)) == NULL)
  		goto __nomem;
  	if ((err = snd_ctl_add(card, trident->ctl_pan)))
  		goto __out;
  
  	if ((trident->ctl_rvol = snd_ctl_new1(&snd_trident_pcm_rvol_control, trident)) == NULL)
  		goto __nomem;
  	if ((err = snd_ctl_add(card, trident->ctl_rvol)))
  		goto __out;
  
  	if ((trident->ctl_cvol = snd_ctl_new1(&snd_trident_pcm_cvol_control, trident)) == NULL)
  		goto __nomem;
  	if ((err = snd_ctl_add(card, trident->ctl_cvol)))
  		goto __out;
  
  	if (trident->device == TRIDENT_DEVICE_ID_NX) {
  		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_ac97_rear_control, trident))) < 0)
  			goto __out;
  		kctl->put(kctl, uctl);
  	}
  	if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018) {
  
  		kctl = snd_ctl_new1(&snd_trident_spdif_control, trident);
  		if (kctl == NULL) {
  			err = -ENOMEM;
  			goto __out;
  		}
  		if (trident->ac97->ext_id & AC97_EI_SPDIF)
  			kctl->id.index++;
  		if (trident->ac97_sec && (trident->ac97_sec->ext_id & AC97_EI_SPDIF))
  			kctl->id.index++;
  		idx = kctl->id.index;
  		if ((err = snd_ctl_add(card, kctl)) < 0)
  			goto __out;
  		kctl->put(kctl, uctl);
  
  		kctl = snd_ctl_new1(&snd_trident_spdif_default, trident);
  		if (kctl == NULL) {
  			err = -ENOMEM;
  			goto __out;
  		}
  		kctl->id.index = idx;
  		kctl->id.device = pcm_spdif_device;
  		if ((err = snd_ctl_add(card, kctl)) < 0)
  			goto __out;
  
  		kctl = snd_ctl_new1(&snd_trident_spdif_mask, trident);
  		if (kctl == NULL) {
  			err = -ENOMEM;
  			goto __out;
  		}
  		kctl->id.index = idx;
  		kctl->id.device = pcm_spdif_device;
  		if ((err = snd_ctl_add(card, kctl)) < 0)
  			goto __out;
  
  		kctl = snd_ctl_new1(&snd_trident_spdif_stream, trident);
  		if (kctl == NULL) {
  			err = -ENOMEM;
  			goto __out;
  		}
  		kctl->id.index = idx;
  		kctl->id.device = pcm_spdif_device;
  		if ((err = snd_ctl_add(card, kctl)) < 0)
  			goto __out;
  		trident->spdif_pcm_ctl = kctl;
  	}
  
  	err = 0;
  	goto __out;
  
   __nomem:
  	err = -ENOMEM;
  
   __out:
  	kfree(uctl);
  
  	return err;
  }
  
  /*
   * gameport interface
   */
  
  #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  
  static unsigned char snd_trident_gameport_read(struct gameport *gameport)
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3061
  	struct snd_trident *chip = gameport_get_port_data(gameport);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3062

da3cec35d   Takashi Iwai   ALSA: Kill snd_as...
3063
3064
  	if (snd_BUG_ON(!chip))
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3065
3066
3067
3068
3069
  	return inb(TRID_REG(chip, GAMEPORT_LEGACY));
  }
  
  static void snd_trident_gameport_trigger(struct gameport *gameport)
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3070
  	struct snd_trident *chip = gameport_get_port_data(gameport);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3071

da3cec35d   Takashi Iwai   ALSA: Kill snd_as...
3072
3073
  	if (snd_BUG_ON(!chip))
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3074
3075
3076
3077
3078
  	outb(0xff, TRID_REG(chip, GAMEPORT_LEGACY));
  }
  
  static int snd_trident_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3079
  	struct snd_trident *chip = gameport_get_port_data(gameport);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3080
  	int i;
da3cec35d   Takashi Iwai   ALSA: Kill snd_as...
3081
3082
  	if (snd_BUG_ON(!chip))
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
  
  	*buttons = (~inb(TRID_REG(chip, GAMEPORT_LEGACY)) >> 4) & 0xf;
  
  	for (i = 0; i < 4; i++) {
  		axes[i] = inw(TRID_REG(chip, GAMEPORT_AXES + i * 2));
  		if (axes[i] == 0xffff) axes[i] = -1;
  	}
          
          return 0;
  }
  
  static int snd_trident_gameport_open(struct gameport *gameport, int mode)
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3096
  	struct snd_trident *chip = gameport_get_port_data(gameport);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3097

da3cec35d   Takashi Iwai   ALSA: Kill snd_as...
3098
3099
  	if (snd_BUG_ON(!chip))
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3100
3101
3102
3103
  
  	switch (mode) {
  		case GAMEPORT_MODE_COOKED:
  			outb(GAMEPORT_MODE_ADC, TRID_REG(chip, GAMEPORT_GCR));
ef21ca24f   Nishanth Aravamudan   [ALSA] sound/pci:...
3104
  			msleep(20);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3105
3106
3107
3108
3109
3110
3111
3112
  			return 0;
  		case GAMEPORT_MODE_RAW:
  			outb(0, TRID_REG(chip, GAMEPORT_GCR));
  			return 0;
  		default:
  			return -1;
  	}
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3113
  int __devinit snd_trident_create_gameport(struct snd_trident *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
  {
  	struct gameport *gp;
  
  	chip->gameport = gp = gameport_allocate_port();
  	if (!gp) {
  		printk(KERN_ERR "trident: cannot allocate memory for gameport
  ");
  		return -ENOMEM;
  	}
  
  	gameport_set_name(gp, "Trident 4DWave");
  	gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  	gameport_set_dev_parent(gp, &chip->pci->dev);
  
  	gameport_set_port_data(gp, chip);
  	gp->fuzz = 64;
  	gp->read = snd_trident_gameport_read;
  	gp->trigger = snd_trident_gameport_trigger;
  	gp->cooked_read = snd_trident_gameport_cooked_read;
  	gp->open = snd_trident_gameport_open;
  
  	gameport_register_port(gp);
  
  	return 0;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3139
  static inline void snd_trident_free_gameport(struct snd_trident *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3140
3141
3142
3143
3144
3145
3146
  {
  	if (chip->gameport) {
  		gameport_unregister_port(chip->gameport);
  		chip->gameport = NULL;
  	}
  }
  #else
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3147
3148
  int __devinit snd_trident_create_gameport(struct snd_trident *chip) { return -ENOSYS; }
  static inline void snd_trident_free_gameport(struct snd_trident *chip) { }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3149
3150
3151
3152
3153
  #endif /* CONFIG_GAMEPORT */
  
  /*
   * delay for 1 tick
   */
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3154
  static inline void do_delay(struct snd_trident *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3155
  {
8433a509c   Nishanth Aravamudan   [ALSA] Fix schedu...
3156
  	schedule_timeout_uninterruptible(1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3157
3158
3159
3160
3161
  }
  
  /*
   *  SiS reset routine
   */
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3162
  static int snd_trident_sis_reset(struct snd_trident *trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
  {
  	unsigned long end_time;
  	unsigned int i;
  	int r;
  
  	r = trident->in_suspend ? 0 : 2;	/* count of retries */
        __si7018_retry:
  	pci_write_config_byte(trident->pci, 0x46, 0x04);	/* SOFTWARE RESET */
  	udelay(100);
  	pci_write_config_byte(trident->pci, 0x46, 0x00);
  	udelay(100);
  	/* disable AC97 GPIO interrupt */
  	outb(0x00, TRID_REG(trident, SI_AC97_GPIO));
  	/* initialize serial interface, force cold reset */
  	i = PCMOUT|SURROUT|CENTEROUT|LFEOUT|SECONDARY_ID|COLD_RESET;
  	outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  	udelay(1000);
  	/* remove cold reset */
  	i &= ~COLD_RESET;
  	outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  	udelay(2000);
  	/* wait, until the codec is ready */
  	end_time = (jiffies + (HZ * 3) / 4) + 1;
  	do {
  		if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0)
  			goto __si7018_ok;
  		do_delay(trident);
  	} while (time_after_eq(end_time, jiffies));
99b359ba1   Takashi Iwai   [ALSA] Add missin...
3191
3192
  	snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]
  ", inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
  	if (r-- > 0) {
  		end_time = jiffies + HZ;
  		do {
  			do_delay(trident);
  		} while (time_after_eq(end_time, jiffies));
  		goto __si7018_retry;
  	}
        __si7018_ok:
  	/* wait for the second codec */
  	do {
  		if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_SECONDARY_READY) != 0)
  			break;
  		do_delay(trident);
  	} while (time_after_eq(end_time, jiffies));
  	/* enable 64 channel mode */
  	outl(BANK_B_EN, TRID_REG(trident, T4D_LFO_GC_CIR));
  	return 0;
  }
  
  /*  
   *  /proc interface
   */
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3215
3216
  static void snd_trident_proc_read(struct snd_info_entry *entry, 
  				  struct snd_info_buffer *buffer)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3217
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3218
  	struct snd_trident *trident = entry->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
  	char *s;
  
  	switch (trident->device) {
  	case TRIDENT_DEVICE_ID_SI7018:
  		s = "SiS 7018 Audio";
  		break;
  	case TRIDENT_DEVICE_ID_DX:
  		s = "Trident 4DWave PCI DX";
  		break;
  	case TRIDENT_DEVICE_ID_NX:
  		s = "Trident 4DWave PCI NX";
  		break;
  	default:
  		s = "???";
  	}
  	snd_iprintf(buffer, "%s
  
  ", s);
  	snd_iprintf(buffer, "Spurious IRQs    : %d
  ", trident->spurious_irq_count);
  	snd_iprintf(buffer, "Spurious IRQ dlta: %d
  ", trident->spurious_irq_max_delta);
  	if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018)
  		snd_iprintf(buffer, "IEC958 Mixer Out : %s
  ", trident->spdif_ctrl == 0x28 ? "on" : "off");
  	if (trident->device == TRIDENT_DEVICE_ID_NX) {
  		snd_iprintf(buffer, "Rear Speakers    : %s
  ", trident->ac97_ctrl & 0x00000010 ? "on" : "off");
  		if (trident->tlb.entries) {
  			snd_iprintf(buffer,"
  Virtual Memory
  ");
  			snd_iprintf(buffer, "Memory Maximum : %d
  ", trident->tlb.memhdr->size);
  			snd_iprintf(buffer, "Memory Used    : %d
  ", trident->tlb.memhdr->used);
  			snd_iprintf(buffer, "Memory Free    : %d
  ", snd_util_mem_avail(trident->tlb.memhdr));
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3259
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3260
  static void __devinit snd_trident_proc_init(struct snd_trident * trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3261
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3262
  	struct snd_info_entry *entry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3263
3264
3265
3266
3267
  	const char *s = "trident";
  	
  	if (trident->device == TRIDENT_DEVICE_ID_SI7018)
  		s = "sis7018";
  	if (! snd_card_proc_new(trident->card, s, &entry))
bf850204a   Takashi Iwai   [ALSA] Remove unn...
3268
  		snd_info_set_text_ops(entry, trident, snd_trident_proc_read);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3269
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3270
  static int snd_trident_dev_free(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3271
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3272
  	struct snd_trident *trident = device->device_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3273
3274
3275
3276
3277
3278
3279
3280
3281
  	return snd_trident_free(trident);
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_tlb_alloc
    
     Description: Allocate and set up the TLB page table on 4D NX.
  		Each entry has 4 bytes (physical PCI address).
                  
561de31a2   Joe Perches   [ALSA] sound/: Sp...
3282
     Parameters:  trident - pointer to target device class for 4DWave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3283
3284
3285
3286
  
     Returns:     0 or negative error code
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3287
  static int __devinit snd_trident_tlb_alloc(struct snd_trident *trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
  {
  	int i;
  
  	/* TLB array must be aligned to 16kB !!! so we allocate
  	   32kB region and correct offset when necessary */
  
  	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
  				2 * SNDRV_TRIDENT_MAX_PAGES * 4, &trident->tlb.buffer) < 0) {
  		snd_printk(KERN_ERR "trident: unable to allocate TLB buffer
  ");
  		return -ENOMEM;
  	}
7ab399262   Clemens Ladisch   [ALSA] use the AL...
3300
3301
  	trident->tlb.entries = (unsigned int*)ALIGN((unsigned long)trident->tlb.buffer.area, SNDRV_TRIDENT_MAX_PAGES * 4);
  	trident->tlb.entries_dmaaddr = ALIGN(trident->tlb.buffer.addr, SNDRV_TRIDENT_MAX_PAGES * 4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3302
  	/* allocate shadow TLB page table (virtual addresses) */
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3303
  	trident->tlb.shadow_entries = vmalloc(SNDRV_TRIDENT_MAX_PAGES*sizeof(unsigned long));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
  	if (trident->tlb.shadow_entries == NULL) {
  		snd_printk(KERN_ERR "trident: unable to allocate shadow TLB entries
  ");
  		return -ENOMEM;
  	}
  	/* allocate and setup silent page and initialise TLB entries */
  	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
  				SNDRV_TRIDENT_PAGE_SIZE, &trident->tlb.silent_page) < 0) {
  		snd_printk(KERN_ERR "trident: unable to allocate silent page
  ");
  		return -ENOMEM;
  	}
  	memset(trident->tlb.silent_page.area, 0, SNDRV_TRIDENT_PAGE_SIZE);
  	for (i = 0; i < SNDRV_TRIDENT_MAX_PAGES; i++) {
  		trident->tlb.entries[i] = cpu_to_le32(trident->tlb.silent_page.addr & ~(SNDRV_TRIDENT_PAGE_SIZE-1));
  		trident->tlb.shadow_entries[i] = (unsigned long)trident->tlb.silent_page.area;
  	}
  
  	/* use emu memory block manager code to manage tlb page allocation */
  	trident->tlb.memhdr = snd_util_memhdr_new(SNDRV_TRIDENT_PAGE_SIZE * SNDRV_TRIDENT_MAX_PAGES);
  	if (trident->tlb.memhdr == NULL)
  		return -ENOMEM;
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3326
  	trident->tlb.memhdr->block_extra_size = sizeof(struct snd_trident_memblk_arg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3327
3328
3329
3330
3331
3332
  	return 0;
  }
  
  /*
   * initialize 4D DX chip
   */
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3333
  static void snd_trident_stop_all_voices(struct snd_trident *trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3334
3335
3336
3337
3338
3339
  {
  	outl(0xffffffff, TRID_REG(trident, T4D_STOP_A));
  	outl(0xffffffff, TRID_REG(trident, T4D_STOP_B));
  	outl(0, TRID_REG(trident, T4D_AINTEN_A));
  	outl(0, TRID_REG(trident, T4D_AINTEN_B));
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3340
  static int snd_trident_4d_dx_init(struct snd_trident *trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
  {
  	struct pci_dev *pci = trident->pci;
  	unsigned long end_time;
  
  	/* reset the legacy configuration and whole audio/wavetable block */
  	pci_write_config_dword(pci, 0x40, 0);	/* DDMA */
  	pci_write_config_byte(pci, 0x44, 0);	/* ports */
  	pci_write_config_byte(pci, 0x45, 0);	/* Legacy DMA */
  	pci_write_config_byte(pci, 0x46, 4); /* reset */
  	udelay(100);
  	pci_write_config_byte(pci, 0x46, 0); /* release reset */
  	udelay(100);
  	
  	/* warm reset of the AC'97 codec */
  	outl(0x00000001, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
  	udelay(100);
  	outl(0x00000000, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
  	/* DAC on, disable SB IRQ and try to force ADC valid signal */
  	trident->ac97_ctrl = 0x0000004a;
  	outl(trident->ac97_ctrl, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
  	/* wait, until the codec is ready */
  	end_time = (jiffies + (HZ * 3) / 4) + 1;
  	do {
  		if ((inl(TRID_REG(trident, DX_ACR2_AC97_COM_STAT)) & 0x0010) != 0)
  			goto __dx_ok;
  		do_delay(trident);
  	} while (time_after_eq(end_time, jiffies));
  	snd_printk(KERN_ERR "AC'97 codec ready error
  ");
  	return -EIO;
  
   __dx_ok:
  	snd_trident_stop_all_voices(trident);
  
  	return 0;
  }
  
  /*
   * initialize 4D NX chip
   */
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3381
  static int snd_trident_4d_nx_init(struct snd_trident *trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
  {
  	struct pci_dev *pci = trident->pci;
  	unsigned long end_time;
  
  	/* reset the legacy configuration and whole audio/wavetable block */
  	pci_write_config_dword(pci, 0x40, 0);	/* DDMA */
  	pci_write_config_byte(pci, 0x44, 0);	/* ports */
  	pci_write_config_byte(pci, 0x45, 0);	/* Legacy DMA */
  
  	pci_write_config_byte(pci, 0x46, 1); /* reset */
  	udelay(100);
  	pci_write_config_byte(pci, 0x46, 0); /* release reset */
  	udelay(100);
  
  	/* warm reset of the AC'97 codec */
  	outl(0x00000001, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  	udelay(100);
  	outl(0x00000000, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  	/* wait, until the codec is ready */
  	end_time = (jiffies + (HZ * 3) / 4) + 1;
  	do {
  		if ((inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)) & 0x0008) != 0)
  			goto __nx_ok;
  		do_delay(trident);
  	} while (time_after_eq(end_time, jiffies));
  	snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]
  ", inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)));
  	return -EIO;
  
   __nx_ok:
  	/* DAC on */
  	trident->ac97_ctrl = 0x00000002;
  	outl(trident->ac97_ctrl, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  	/* disable SB IRQ */
  	outl(NX_SB_IRQ_DISABLE, TRID_REG(trident, T4D_MISCINT));
  
  	snd_trident_stop_all_voices(trident);
  
  	if (trident->tlb.entries != NULL) {
  		unsigned int i;
  		/* enable virtual addressing via TLB */
  		i = trident->tlb.entries_dmaaddr;
  		i |= 0x00000001;
  		outl(i, TRID_REG(trident, NX_TLBC));
  	} else {
  		outl(0, TRID_REG(trident, NX_TLBC));
  	}
  	/* initialize S/PDIF */
  	outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  	outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  
  	return 0;
  }
  
  /*
   * initialize sis7018 chip
   */
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3439
  static int snd_trident_sis_init(struct snd_trident *trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
  {
  	int err;
  
  	if ((err = snd_trident_sis_reset(trident)) < 0)
  		return err;
  
  	snd_trident_stop_all_voices(trident);
  
  	/* initialize S/PDIF */
  	outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_create
    
     Description: This routine will create the device specific class for
                  the 4DWave card. It will also perform basic initialization.
                  
561de31a2   Joe Perches   [ALSA] sound/: Sp...
3460
     Parameters:  card  - which card to create
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3461
3462
3463
3464
3465
3466
3467
3468
                  pci   - interface to PCI bus resource info
                  dma1ptr - playback dma buffer
                  dma2ptr - capture dma buffer
                  irqptr  -  interrupt resource info
  
     Returns:     4DWave device class private data
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3469
  int __devinit snd_trident_create(struct snd_card *card,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3470
3471
3472
3473
  		       struct pci_dev *pci,
  		       int pcm_streams,
  		       int pcm_spdif_device,
  		       int max_wavetable_size,
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3474
  		       struct snd_trident ** rtrident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3475
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3476
  	struct snd_trident *trident;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3477
  	int i, err;
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3478
3479
3480
  	struct snd_trident_voice *voice;
  	struct snd_trident_pcm_mixer *tmix;
  	static struct snd_device_ops ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3481
3482
3483
3484
3485
3486
3487
3488
3489
  		.dev_free =	snd_trident_dev_free,
  	};
  
  	*rtrident = NULL;
  
  	/* enable PCI device */
  	if ((err = pci_enable_device(pci)) < 0)
  		return err;
  	/* check, if we can restrict PCI DMA transfers to 30 bits */
28b767967   Yang Hongyang   dma-mapping: repl...
3490
3491
  	if (pci_set_dma_mask(pci, DMA_BIT_MASK(30)) < 0 ||
  	    pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(30)) < 0) {
99b359ba1   Takashi Iwai   [ALSA] Add missin...
3492
3493
  		snd_printk(KERN_ERR "architecture does not support 30bit PCI busmaster DMA
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3494
3495
3496
3497
  		pci_disable_device(pci);
  		return -ENXIO;
  	}
  	
e560d8d83   Takashi Iwai   [ALSA] Replace wi...
3498
  	trident = kzalloc(sizeof(*trident), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
  	if (trident == NULL) {
  		pci_disable_device(pci);
  		return -ENOMEM;
  	}
  	trident->device = (pci->vendor << 16) | pci->device;
  	trident->card = card;
  	trident->pci = pci;
  	spin_lock_init(&trident->reg_lock);
  	spin_lock_init(&trident->event_lock);
  	spin_lock_init(&trident->voice_alloc);
  	if (pcm_streams < 1)
  		pcm_streams = 1;
  	if (pcm_streams > 32)
  		pcm_streams = 32;
  	trident->ChanPCM = pcm_streams;
  	if (max_wavetable_size < 0 )
  		max_wavetable_size = 0;
  	trident->synth.max_size = max_wavetable_size * 1024;
  	trident->irq = -1;
  
  	trident->midi_port = TRID_REG(trident, T4D_MPU401_BASE);
  	pci_set_master(pci);
  
  	if ((err = pci_request_regions(pci, "Trident Audio")) < 0) {
  		kfree(trident);
  		pci_disable_device(pci);
  		return err;
  	}
  	trident->port = pci_resource_start(pci, 0);
437a5a460   Takashi Iwai   [ALSA] Remove IRQ...
3528
  	if (request_irq(pci->irq, snd_trident_interrupt, IRQF_SHARED,
934c2b6d0   Takashi Iwai   ALSA: use KBUILD_...
3529
  			KBUILD_MODNAME, trident)) {
99b359ba1   Takashi Iwai   [ALSA] Add missin...
3530
3531
  		snd_printk(KERN_ERR "unable to grab IRQ %d
  ", pci->irq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
  		snd_trident_free(trident);
  		return -EBUSY;
  	}
  	trident->irq = pci->irq;
  
  	/* allocate 16k-aligned TLB for NX cards */
  	trident->tlb.entries = NULL;
  	trident->tlb.buffer.area = NULL;
  	if (trident->device == TRIDENT_DEVICE_ID_NX) {
  		if ((err = snd_trident_tlb_alloc(trident)) < 0) {
  			snd_trident_free(trident);
  			return err;
  		}
  	}
  
  	trident->spdif_bits = trident->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
  
  	/* initialize chip */
  	switch (trident->device) {
  	case TRIDENT_DEVICE_ID_DX:
  		err = snd_trident_4d_dx_init(trident);
  		break;
  	case TRIDENT_DEVICE_ID_NX:
  		err = snd_trident_4d_nx_init(trident);
  		break;
  	case TRIDENT_DEVICE_ID_SI7018:
  		err = snd_trident_sis_init(trident);
  		break;
  	default:
  		snd_BUG();
  		break;
  	}
  	if (err < 0) {
  		snd_trident_free(trident);
  		return err;
  	}
  
  	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, trident, &ops)) < 0) {
  		snd_trident_free(trident);
  		return err;
  	}
  
  	if ((err = snd_trident_mixer(trident, pcm_spdif_device)) < 0)
  		return err;
  	
  	/* initialise synth voices */
  	for (i = 0; i < 64; i++) {
  		voice = &trident->synth.voices[i];
  		voice->number = i;
  		voice->trident = trident;
  	}
  	/* initialize pcm mixer entries */
  	for (i = 0; i < 32; i++) {
  		tmix = &trident->pcm_mixer[i];
  		tmix->vol = T4D_DEFAULT_PCM_VOL;
  		tmix->pan = T4D_DEFAULT_PCM_PAN;
  		tmix->rvol = T4D_DEFAULT_PCM_RVOL;
  		tmix->cvol = T4D_DEFAULT_PCM_CVOL;
  	}
  
  	snd_trident_enable_eso(trident);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
  	snd_trident_proc_init(trident);
  	snd_card_set_dev(card, &pci->dev);
  	*rtrident = trident;
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_free
    
     Description: This routine will free the device specific class for
                  the 4DWave card. 
                  
561de31a2   Joe Perches   [ALSA] sound/: Sp...
3605
     Parameters:  trident  - device specific private data for 4DWave card
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3606
3607
3608
3609
  
     Returns:     None.
    
    ---------------------------------------------------------------------------*/
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3610
  static int snd_trident_free(struct snd_trident *trident)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3611
3612
3613
3614
3615
3616
3617
3618
3619
  {
  	snd_trident_free_gameport(trident);
  	snd_trident_disable_eso(trident);
  	// Disable S/PDIF out
  	if (trident->device == TRIDENT_DEVICE_ID_NX)
  		outb(0x00, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  	else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  		outl(0, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  	}
ebf029da3   Takashi Iwai   [ALSA] Fix possib...
3620
3621
  	if (trident->irq >= 0)
  		free_irq(trident->irq, trident);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3622
3623
3624
3625
3626
3627
3628
3629
3630
  	if (trident->tlb.buffer.area) {
  		outl(0, TRID_REG(trident, NX_TLBC));
  		if (trident->tlb.memhdr)
  			snd_util_memhdr_free(trident->tlb.memhdr);
  		if (trident->tlb.silent_page.area)
  			snd_dma_free_pages(&trident->tlb.silent_page);
  		vfree(trident->tlb.shadow_entries);
  		snd_dma_free_pages(&trident->tlb.buffer);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
  	pci_release_regions(trident->pci);
  	pci_disable_device(trident->pci);
  	kfree(trident);
  	return 0;
  }
  
  /*---------------------------------------------------------------------------
     snd_trident_interrupt
    
     Description: ISR for Trident 4DWave device
                  
561de31a2   Joe Perches   [ALSA] sound/: Sp...
3642
     Parameters:  trident  - device specific private data for 4DWave card
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
  
     Problems:    It seems that Trident chips generates interrupts more than
                  one time in special cases. The spurious interrupts are
                  detected via sample timer (T4D_STIMER) and computing
                  corresponding delta value. The limits are detected with
                  the method try & fail so it is possible that it won't
                  work on all computers. [jaroslav]
  
     Returns:     None.
    
    ---------------------------------------------------------------------------*/
7d12e780e   David Howells   IRQ: Maintain reg...
3654
  static irqreturn_t snd_trident_interrupt(int irq, void *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3655
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3656
  	struct snd_trident *trident = dev_id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3657
3658
  	unsigned int audio_int, chn_int, stimer, channel, mask, tmp;
  	int delta;
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3659
  	struct snd_trident_voice *voice;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
  
  	audio_int = inl(TRID_REG(trident, T4D_MISCINT));
  	if ((audio_int & (ADDRESS_IRQ|MPU401_IRQ)) == 0)
  		return IRQ_NONE;
  	if (audio_int & ADDRESS_IRQ) {
  		// get interrupt status for all channels
  		spin_lock(&trident->reg_lock);
  		stimer = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
  		chn_int = inl(TRID_REG(trident, T4D_AINT_A));
  		if (chn_int == 0)
  			goto __skip1;
  		outl(chn_int, TRID_REG(trident, T4D_AINT_A));	/* ack */
  	      __skip1:
  		chn_int = inl(TRID_REG(trident, T4D_AINT_B));
  		if (chn_int == 0)
  			goto __skip2;
  		for (channel = 63; channel >= 32; channel--) {
  			mask = 1 << (channel&0x1f);
  			if ((chn_int & mask) == 0)
  				continue;
  			voice = &trident->synth.voices[channel];
  			if (!voice->pcm || voice->substream == NULL) {
  				outl(mask, TRID_REG(trident, T4D_STOP_B));
  				continue;
  			}
  			delta = (int)stimer - (int)voice->stimer;
  			if (delta < 0)
  				delta = -delta;
  			if ((unsigned int)delta < voice->spurious_threshold) {
  				/* do some statistics here */
  				trident->spurious_irq_count++;
  				if (trident->spurious_irq_max_delta < (unsigned int)delta)
  					trident->spurious_irq_max_delta = delta;
  				continue;
  			}
  			voice->stimer = stimer;
  			if (voice->isync) {
  				if (!voice->isync3) {
  					tmp = inw(TRID_REG(trident, T4D_SBBL_SBCL));
  					if (trident->bDMAStart & 0x40)
  						tmp >>= 1;
  					if (tmp > 0)
  						tmp = voice->isync_max - tmp;
  				} else {
  					tmp = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
  				}
  				if (tmp < voice->isync_mark) {
  					if (tmp > 0x10)
  						tmp = voice->isync_ESO - 7;
  					else
  						tmp = voice->isync_ESO + 2;
  					/* update ESO for IRQ voice to preserve sync */
  					snd_trident_stop_voice(trident, voice->number);
  					snd_trident_write_eso_reg(trident, voice, tmp);
  					snd_trident_start_voice(trident, voice->number);
  				}
  			} else if (voice->isync2) {
  				voice->isync2 = 0;
  				/* write original ESO and update CSO for IRQ voice to preserve sync */
  				snd_trident_stop_voice(trident, voice->number);
  				snd_trident_write_cso_reg(trident, voice, voice->isync_mark);
  				snd_trident_write_eso_reg(trident, voice, voice->ESO);
  				snd_trident_start_voice(trident, voice->number);
  			}
  #if 0
  			if (voice->extra) {
  				/* update CSO for extra voice to preserve sync */
  				snd_trident_stop_voice(trident, voice->extra->number);
  				snd_trident_write_cso_reg(trident, voice->extra, 0);
  				snd_trident_start_voice(trident, voice->extra->number);
  			}
  #endif
  			spin_unlock(&trident->reg_lock);
  			snd_pcm_period_elapsed(voice->substream);
  			spin_lock(&trident->reg_lock);
  		}
  		outl(chn_int, TRID_REG(trident, T4D_AINT_B));	/* ack */
  	      __skip2:
  		spin_unlock(&trident->reg_lock);
  	}
  	if (audio_int & MPU401_IRQ) {
  		if (trident->rmidi) {
7d12e780e   David Howells   IRQ: Maintain reg...
3742
  			snd_mpu401_uart_interrupt(irq, trident->rmidi->private_data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3743
3744
3745
3746
3747
3748
3749
  		} else {
  			inb(TRID_REG(trident, T4D_MPUR0));
  		}
  	}
  	// outl((ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW), TRID_REG(trident, T4D_MISCINT));
  	return IRQ_HANDLED;
  }
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3750
  struct snd_trident_voice *snd_trident_alloc_voice(struct snd_trident * trident, int type, int client, int port)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3751
  {
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3752
  	struct snd_trident_voice *pvoice;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
  	unsigned long flags;
  	int idx;
  
  	spin_lock_irqsave(&trident->voice_alloc, flags);
  	if (type == SNDRV_TRIDENT_VOICE_TYPE_PCM) {
  		idx = snd_trident_allocate_pcm_channel(trident);
  		if(idx < 0) {
  			spin_unlock_irqrestore(&trident->voice_alloc, flags);
  			return NULL;
  		}
  		pvoice = &trident->synth.voices[idx];
  		pvoice->use = 1;
  		pvoice->pcm = 1;
  		pvoice->capture = 0;
  		pvoice->spdif = 0;
  		pvoice->memblk = NULL;
  		pvoice->substream = NULL;
  		spin_unlock_irqrestore(&trident->voice_alloc, flags);
  		return pvoice;
  	}
  	if (type == SNDRV_TRIDENT_VOICE_TYPE_SYNTH) {
  		idx = snd_trident_allocate_synth_channel(trident);
  		if(idx < 0) {
  			spin_unlock_irqrestore(&trident->voice_alloc, flags);
  			return NULL;
  		}
  		pvoice = &trident->synth.voices[idx];
  		pvoice->use = 1;
  		pvoice->synth = 1;
  		pvoice->client = client;
  		pvoice->port = port;
  		pvoice->memblk = NULL;
  		spin_unlock_irqrestore(&trident->voice_alloc, flags);
  		return pvoice;
  	}
  	if (type == SNDRV_TRIDENT_VOICE_TYPE_MIDI) {
  	}
  	spin_unlock_irqrestore(&trident->voice_alloc, flags);
  	return NULL;
  }
cbef55f3d   Takashi Iwai   [ALSA] trident - ...
3793
  EXPORT_SYMBOL(snd_trident_alloc_voice);
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3794
  void snd_trident_free_voice(struct snd_trident * trident, struct snd_trident_voice *voice)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3795
3796
  {
  	unsigned long flags;
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3797
  	void (*private_free)(struct snd_trident_voice *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
  	void *private_data;
  
  	if (voice == NULL || !voice->use)
  		return;
  	snd_trident_clear_voices(trident, voice->number, voice->number);
  	spin_lock_irqsave(&trident->voice_alloc, flags);
  	private_free = voice->private_free;
  	private_data = voice->private_data;
  	voice->private_free = NULL;
  	voice->private_data = NULL;
  	if (voice->pcm)
  		snd_trident_free_pcm_channel(trident, voice->number);
  	if (voice->synth)
  		snd_trident_free_synth_channel(trident, voice->number);
  	voice->use = voice->pcm = voice->synth = voice->midi = 0;
  	voice->capture = voice->spdif = 0;
  	voice->sample_ops = NULL;
  	voice->substream = NULL;
  	voice->extra = NULL;
  	spin_unlock_irqrestore(&trident->voice_alloc, flags);
  	if (private_free)
  		private_free(voice);
  }
cbef55f3d   Takashi Iwai   [ALSA] trident - ...
3821
  EXPORT_SYMBOL(snd_trident_free_voice);
bee1a5be8   Takashi Iwai   [ALSA] Remove xxx...
3822
  static void snd_trident_clear_voices(struct snd_trident * trident, unsigned short v_min, unsigned short v_max)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3823
3824
  {
  	unsigned int i, val, mask[2] = { 0, 0 };
da3cec35d   Takashi Iwai   ALSA: Kill snd_as...
3825
3826
  	if (snd_BUG_ON(v_min > 63 || v_max > 63))
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
  	for (i = v_min; i <= v_max; i++)
  		mask[i >> 5] |= 1 << (i & 0x1f);
  	if (mask[0]) {
  		outl(mask[0], TRID_REG(trident, T4D_STOP_A));
  		val = inl(TRID_REG(trident, T4D_AINTEN_A));
  		outl(val & ~mask[0], TRID_REG(trident, T4D_AINTEN_A));
  	}
  	if (mask[1]) {
  		outl(mask[1], TRID_REG(trident, T4D_STOP_B));
  		val = inl(TRID_REG(trident, T4D_AINTEN_B));
  		outl(val & ~mask[1], TRID_REG(trident, T4D_AINTEN_B));
  	}
  }
  
  #ifdef CONFIG_PM
fb0700b4d   Takashi Iwai   [ALSA] trident - ...
3842
  int snd_trident_suspend(struct pci_dev *pci, pm_message_t state)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3843
  {
fb0700b4d   Takashi Iwai   [ALSA] trident - ...
3844
3845
  	struct snd_card *card = pci_get_drvdata(pci);
  	struct snd_trident *trident = card->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3846
3847
  
  	trident->in_suspend = 1;
fb0700b4d   Takashi Iwai   [ALSA] trident - ...
3848
  	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3849
  	snd_pcm_suspend_all(trident->pcm);
fb0700b4d   Takashi Iwai   [ALSA] trident - ...
3850
3851
  	snd_pcm_suspend_all(trident->foldback);
  	snd_pcm_suspend_all(trident->spdif);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3852
3853
  
  	snd_ac97_suspend(trident->ac97);
fb0700b4d   Takashi Iwai   [ALSA] trident - ...
3854
  	snd_ac97_suspend(trident->ac97_sec);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3855

fb0700b4d   Takashi Iwai   [ALSA] trident - ...
3856
3857
  	pci_disable_device(pci);
  	pci_save_state(pci);
30b35399c   Takashi Iwai   [ALSA] Various fi...
3858
  	pci_set_power_state(pci, pci_choose_state(pci, state));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3859
3860
  	return 0;
  }
fb0700b4d   Takashi Iwai   [ALSA] trident - ...
3861
  int snd_trident_resume(struct pci_dev *pci)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3862
  {
fb0700b4d   Takashi Iwai   [ALSA] trident - ...
3863
3864
  	struct snd_card *card = pci_get_drvdata(pci);
  	struct snd_trident *trident = card->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3865

30b35399c   Takashi Iwai   [ALSA] Various fi...
3866
  	pci_set_power_state(pci, PCI_D0);
fb0700b4d   Takashi Iwai   [ALSA] trident - ...
3867
  	pci_restore_state(pci);
30b35399c   Takashi Iwai   [ALSA] Various fi...
3868
3869
3870
3871
3872
3873
3874
3875
  	if (pci_enable_device(pci) < 0) {
  		printk(KERN_ERR "trident: pci_enable_device failed, "
  		       "disabling device
  ");
  		snd_card_disconnect(card);
  		return -EIO;
  	}
  	pci_set_master(pci);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
  
  	switch (trident->device) {
  	case TRIDENT_DEVICE_ID_DX:
  		snd_trident_4d_dx_init(trident);
  		break;
  	case TRIDENT_DEVICE_ID_NX:
  		snd_trident_4d_nx_init(trident);
  		break;
  	case TRIDENT_DEVICE_ID_SI7018:
  		snd_trident_sis_init(trident);
  		break;
  	}
  
  	snd_ac97_resume(trident->ac97);
fb0700b4d   Takashi Iwai   [ALSA] trident - ...
3890
  	snd_ac97_resume(trident->ac97_sec);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3891
3892
3893
3894
3895
  
  	/* restore some registers */
  	outl(trident->musicvol_wavevol, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  
  	snd_trident_enable_eso(trident);
fb0700b4d   Takashi Iwai   [ALSA] trident - ...
3896
  	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3897
3898
3899
3900
  	trident->in_suspend = 0;
  	return 0;
  }
  #endif /* CONFIG_PM */