Blame view

sound/pci/cmipci.c 102 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  /*
   * Driver for C-Media CMI8338 and 8738 PCI soundcards.
   * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
   *
   *   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
   */
   
  /* Does not work. Warning may block system in capture mode */
  /* #define USE_VAR48KRATE */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
23
24
25
26
27
28
29
  #include <asm/io.h>
  #include <linux/delay.h>
  #include <linux/interrupt.h>
  #include <linux/init.h>
  #include <linux/pci.h>
  #include <linux/slab.h>
  #include <linux/gameport.h>
  #include <linux/moduleparam.h>
62932df8f   Ingo Molnar   [ALSA] semaphore ...
30
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include <sound/core.h>
  #include <sound/info.h>
  #include <sound/control.h>
  #include <sound/pcm.h>
  #include <sound/rawmidi.h>
  #include <sound/mpu401.h>
  #include <sound/opl3.h>
  #include <sound/sb.h>
  #include <sound/asoundef.h>
  #include <sound/initval.h>
  
  MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  MODULE_DESCRIPTION("C-Media CMI8x38 PCI");
  MODULE_LICENSE("GPL");
  MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8738},"
  		"{C-Media,CMI8738B},"
  		"{C-Media,CMI8338A},"
  		"{C-Media,CMI8338B}}");
  
  #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  #define SUPPORT_JOYSTICK 1
  #endif
  
  static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
  static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
  static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable switches */
  static long mpu_port[SNDRV_CARDS];
2f24d159d   Takashi Iwai   [ALSA] cmipci - A...
58
  static long fm_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  static int soft_ac3[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
  #ifdef SUPPORT_JOYSTICK
  static int joystick_port[SNDRV_CARDS];
  #endif
  
  module_param_array(index, int, NULL, 0444);
  MODULE_PARM_DESC(index, "Index value for C-Media PCI soundcard.");
  module_param_array(id, charp, NULL, 0444);
  MODULE_PARM_DESC(id, "ID string for C-Media PCI soundcard.");
  module_param_array(enable, bool, NULL, 0444);
  MODULE_PARM_DESC(enable, "Enable C-Media PCI soundcard.");
  module_param_array(mpu_port, long, NULL, 0444);
  MODULE_PARM_DESC(mpu_port, "MPU-401 port.");
  module_param_array(fm_port, long, NULL, 0444);
  MODULE_PARM_DESC(fm_port, "FM port.");
  module_param_array(soft_ac3, bool, NULL, 0444);
25985edce   Lucas De Marchi   Fix common misspe...
75
  MODULE_PARM_DESC(soft_ac3, "Software-conversion of raw SPDIF packets (model 033 only).");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
77
78
79
  #ifdef SUPPORT_JOYSTICK
  module_param_array(joystick_port, int, NULL, 0444);
  MODULE_PARM_DESC(joystick_port, "Joystick port address.");
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  /*
   * CM8x38 registers definition
   */
  
  #define CM_REG_FUNCTRL0		0x00
  #define CM_RST_CH1		0x00080000
  #define CM_RST_CH0		0x00040000
  #define CM_CHEN1		0x00020000	/* ch1: enable */
  #define CM_CHEN0		0x00010000	/* ch0: enable */
  #define CM_PAUSE1		0x00000008	/* ch1: pause */
  #define CM_PAUSE0		0x00000004	/* ch0: pause */
  #define CM_CHADC1		0x00000002	/* ch1, 0:playback, 1:record */
  #define CM_CHADC0		0x00000001	/* ch0, 0:playback, 1:record */
  
  #define CM_REG_FUNCTRL1		0x04
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
95
96
97
98
  #define CM_DSFC_MASK		0x0000E000	/* channel 1 (DAC?) sampling frequency */
  #define CM_DSFC_SHIFT		13
  #define CM_ASFC_MASK		0x00001C00	/* channel 0 (ADC?) sampling frequency */
  #define CM_ASFC_SHIFT		10
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
  #define CM_SPDF_1		0x00000200	/* SPDIF IN/OUT at channel B */
  #define CM_SPDF_0		0x00000100	/* SPDIF OUT only channel A */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
101
  #define CM_SPDFLOOP		0x00000080	/* ext. SPDIIF/IN -> OUT loopback */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
103
104
105
  #define CM_SPDO2DAC		0x00000040	/* SPDIF/OUT can be heard from internal DAC */
  #define CM_INTRM		0x00000020	/* master control block (MCB) interrupt enabled */
  #define CM_BREQ			0x00000010	/* bus master enabled */
  #define CM_VOICE_EN		0x00000008	/* legacy voice (SB16,FM) */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
106
107
108
  #define CM_UART_EN		0x00000004	/* legacy UART */
  #define CM_JYSTK_EN		0x00000002	/* legacy joystick */
  #define CM_ZVPORT		0x00000001	/* ZVPORT */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
111
112
  
  #define CM_REG_CHFORMAT		0x08
  
  #define CM_CHB3D5C		0x80000000	/* 5,6 channels */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
113
  #define CM_FMOFFSET2		0x40000000	/* initial FM PCM offset 2 when Fmute=1 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
115
116
117
  #define CM_CHB3D		0x20000000	/* 4 channels */
  
  #define CM_CHIP_MASK1		0x1f000000
  #define CM_CHIP_037		0x01000000
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
118
119
120
  #define CM_SETLAT48		0x00800000	/* set latency timer 48h */
  #define CM_EDGEIRQ		0x00400000	/* emulated edge trigger legacy IRQ */
  #define CM_SPD24SEL39		0x00200000	/* 24-bit spdif: model 039 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
  #define CM_AC3EN1		0x00100000	/* enable AC3: model 037 */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
122
  #define CM_SPDIF_SELECT1	0x00080000	/* for model <= 037 ? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
124
125
126
127
128
129
130
  #define CM_SPD24SEL		0x00020000	/* 24bit spdif: model 037 */
  /* #define CM_SPDIF_INVERSE	0x00010000 */ /* ??? */
  
  #define CM_ADCBITLEN_MASK	0x0000C000	
  #define CM_ADCBITLEN_16		0x00000000
  #define CM_ADCBITLEN_15		0x00004000
  #define CM_ADCBITLEN_14		0x00008000
  #define CM_ADCBITLEN_13		0x0000C000
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
131
  #define CM_ADCDACLEN_MASK	0x00003000	/* model 037 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
132
133
134
135
  #define CM_ADCDACLEN_060	0x00000000
  #define CM_ADCDACLEN_066	0x00001000
  #define CM_ADCDACLEN_130	0x00002000
  #define CM_ADCDACLEN_280	0x00003000
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
136
137
138
139
140
  #define CM_ADCDLEN_MASK		0x00003000	/* model 039 */
  #define CM_ADCDLEN_ORIGINAL	0x00000000
  #define CM_ADCDLEN_EXTRA	0x00001000
  #define CM_ADCDLEN_24K		0x00002000
  #define CM_ADCDLEN_WEIGHT	0x00003000
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
  #define CM_CH1_SRATE_176K	0x00000800
8992e18db   Clemens Ladisch   [ALSA] cmipci: ad...
142
  #define CM_CH1_SRATE_96K	0x00000800	/* model 055? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
144
  #define CM_CH1_SRATE_88K	0x00000400
  #define CM_CH0_SRATE_176K	0x00000200
8992e18db   Clemens Ladisch   [ALSA] cmipci: ad...
145
  #define CM_CH0_SRATE_96K	0x00000200	/* model 055? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
  #define CM_CH0_SRATE_88K	0x00000100
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
147
148
  #define CM_CH0_SRATE_128K	0x00000300
  #define CM_CH0_SRATE_MASK	0x00000300
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
150
  
  #define CM_SPDIF_INVERSE2	0x00000080	/* model 055? */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
151
152
153
  #define CM_DBLSPDS		0x00000040	/* double SPDIF sample rate 88.2/96 */
  #define CM_POLVALID		0x00000020	/* inverse SPDIF/IN valid bit */
  #define CM_SPDLOCKED		0x00000010
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154

a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
155
  #define CM_CH1FMT_MASK		0x0000000C	/* bit 3: 16 bits, bit 2: stereo */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
  #define CM_CH1FMT_SHIFT		2
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
157
  #define CM_CH0FMT_MASK		0x00000003	/* bit 1: 16 bits, bit 0: stereo */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
160
161
  #define CM_CH0FMT_SHIFT		0
  
  #define CM_REG_INT_HLDCLR	0x0C
  #define CM_CHIP_MASK2		0xff000000
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
162
163
  #define CM_CHIP_8768		0x20000000
  #define CM_CHIP_055		0x08000000
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164
165
  #define CM_CHIP_039		0x04000000
  #define CM_CHIP_039_6CH		0x01000000
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
166
  #define CM_UNKNOWN_INT_EN	0x00080000	/* ? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
168
169
  #define CM_TDMA_INT_EN		0x00040000
  #define CM_CH1_INT_EN		0x00020000
  #define CM_CH0_INT_EN		0x00010000
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  
  #define CM_REG_INT_STATUS	0x10
  #define CM_INTR			0x80000000
  #define CM_VCO			0x08000000	/* Voice Control? CMI8738 */
  #define CM_MCBINT		0x04000000	/* Master Control Block abort cond.? */
  #define CM_UARTINT		0x00010000
  #define CM_LTDMAINT		0x00008000
  #define CM_HTDMAINT		0x00004000
  #define CM_XDO46		0x00000080	/* Modell 033? Direct programming EEPROM (read data register) */
  #define CM_LHBTOG		0x00000040	/* High/Low status from DMA ctrl register */
  #define CM_LEG_HDMA		0x00000020	/* Legacy is in High DMA channel */
  #define CM_LEG_STEREO		0x00000010	/* Legacy is in Stereo mode */
  #define CM_CH1BUSY		0x00000008
  #define CM_CH0BUSY		0x00000004
  #define CM_CHINT1		0x00000002
  #define CM_CHINT0		0x00000001
  
  #define CM_REG_LEGACY_CTRL	0x14
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
188
  #define CM_NXCHG		0x80000000	/* don't map base reg dword->sample */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189
190
191
192
193
  #define CM_VMPU_MASK		0x60000000	/* MPU401 i/o port address */
  #define CM_VMPU_330		0x00000000
  #define CM_VMPU_320		0x20000000
  #define CM_VMPU_310		0x40000000
  #define CM_VMPU_300		0x60000000
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
194
  #define CM_ENWR8237		0x10000000	/* enable bus master to write 8237 base reg */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
195
196
197
198
199
200
201
202
203
204
  #define CM_VSBSEL_MASK		0x0C000000	/* SB16 base address */
  #define CM_VSBSEL_220		0x00000000
  #define CM_VSBSEL_240		0x04000000
  #define CM_VSBSEL_260		0x08000000
  #define CM_VSBSEL_280		0x0C000000
  #define CM_FMSEL_MASK		0x03000000	/* FM OPL3 base address */
  #define CM_FMSEL_388		0x00000000
  #define CM_FMSEL_3C8		0x01000000
  #define CM_FMSEL_3E0		0x02000000
  #define CM_FMSEL_3E8		0x03000000
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
205
206
  #define CM_ENSPDOUT		0x00800000	/* enable XSPDIF/OUT to I/O interface */
  #define CM_SPDCOPYRHT		0x00400000	/* spdif in/out copyright bit */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
  #define CM_DAC2SPDO		0x00200000	/* enable wave+fm_midi -> SPDIF/OUT */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
208
209
210
211
212
213
  #define CM_INVIDWEN		0x00100000	/* internal vendor ID write enable, model 039? */
  #define CM_SETRETRY		0x00100000	/* 0: legacy i/o wait (default), 1: legacy i/o bus retry */
  #define CM_C_EEACCESS		0x00080000	/* direct programming eeprom regs */
  #define CM_C_EECS		0x00040000
  #define CM_C_EEDI46		0x00020000
  #define CM_C_EECK46		0x00010000
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
  #define CM_CHB3D6C		0x00008000	/* 5.1 channels support */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
215
216
217
  #define CM_CENTR2LIN		0x00004000	/* line-in as center out */
  #define CM_BASE2LIN		0x00002000	/* line-in as bass out */
  #define CM_EXBASEN		0x00001000	/* external bass input enable */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
219
  
  #define CM_REG_MISC_CTRL	0x18
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
220
  #define CM_PWD			0x80000000	/* power down */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
221
  #define CM_RESET		0x40000000
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
222
223
224
225
  #define CM_SFIL_MASK		0x30000000	/* filter control at front end DAC, model 037? */
  #define CM_VMGAIN		0x10000000	/* analog master amp +6dB, model 039? */
  #define CM_TXVX			0x08000000	/* model 037? */
  #define CM_N4SPK3D		0x04000000	/* copy front to rear */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226
227
228
  #define CM_SPDO5V		0x02000000	/* 5V spdif output (1 = 0.5v (coax)) */
  #define CM_SPDIF48K		0x01000000	/* write */
  #define CM_SPATUS48K		0x01000000	/* read */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
229
  #define CM_ENDBDAC		0x00800000	/* enable double dac */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
231
  #define CM_XCHGDAC		0x00400000	/* 0: front=ch0, 1: front=ch1 */
  #define CM_SPD32SEL		0x00200000	/* 0: 16bit SPDIF, 1: 32bit */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
232
233
  #define CM_SPDFLOOPI		0x00100000	/* int. SPDIF-OUT -> int. IN */
  #define CM_FM_EN		0x00080000	/* enable legacy FM */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
  #define CM_AC3EN2		0x00040000	/* enable AC3: model 039 */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
235
236
  #define CM_ENWRASID		0x00010000	/* choose writable internal SUBID (audio) */
  #define CM_VIDWPDSB		0x00010000	/* model 037? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
  #define CM_SPDF_AC97		0x00008000	/* 0: SPDIF/OUT 44.1K, 1: 48K */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
238
239
240
241
242
  #define CM_MASK_EN		0x00004000	/* activate channel mask on legacy DMA */
  #define CM_ENWRMSID		0x00002000	/* choose writable internal SUBID (modem) */
  #define CM_VIDWPPRT		0x00002000	/* model 037? */
  #define CM_SFILENB		0x00001000	/* filter stepping at front end DAC, model 037? */
  #define CM_MMODE_MASK		0x00000E00	/* model DAA interface mode */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
244
  #define CM_SPDIF_SELECT2	0x00000100	/* for model > 039 ? */
  #define CM_ENCENTER		0x00000080
56c36ca3b   Clemens Ladisch   [ALSA] cmipci: fi...
245
  #define CM_FLINKON		0x00000040	/* force modem link detection on, model 037 */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
246
  #define CM_MUTECH1		0x00000040	/* mute PCI ch1 to DAC */
56c36ca3b   Clemens Ladisch   [ALSA] cmipci: fi...
247
  #define CM_FLINKOFF		0x00000020	/* force modem link detection off, model 037 */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
248
249
250
251
252
253
254
255
256
257
258
259
260
  #define CM_MIDSMP		0x00000010	/* 1/2 interpolation at front end DAC */
  #define CM_UPDDMA_MASK		0x0000000C	/* TDMA position update notification */
  #define CM_UPDDMA_2048		0x00000000
  #define CM_UPDDMA_1024		0x00000004
  #define CM_UPDDMA_512		0x00000008
  #define CM_UPDDMA_256		0x0000000C		
  #define CM_TWAIT_MASK		0x00000003	/* model 037 */
  #define CM_TWAIT1		0x00000002	/* FM i/o cycle, 0: 48, 1: 64 PCICLKs */
  #define CM_TWAIT0		0x00000001	/* i/o cycle, 0: 4, 1: 6 PCICLKs */
  
  #define CM_REG_TDMA_POSITION	0x1C
  #define CM_TDMA_CNT_MASK	0xFFFF0000	/* current byte/word count */
  #define CM_TDMA_ADR_MASK	0x0000FFFF	/* current address */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
261
262
263
  
  	/* byte */
  #define CM_REG_MIXER0		0x20
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
264
265
266
267
268
269
270
271
  #define CM_REG_SBVR		0x20		/* write: sb16 version */
  #define CM_REG_DEV		0x20		/* read: hardware device version */
  
  #define CM_REG_MIXER21		0x21
  #define CM_UNKNOWN_21_MASK	0x78		/* ? */
  #define CM_X_ADPCM		0x04		/* SB16 ADPCM enable */
  #define CM_PROINV		0x02		/* SBPro left/right channel switching */
  #define CM_X_SB16		0x01		/* SB16 compatible */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  
  #define CM_REG_SB16_DATA	0x22
  #define CM_REG_SB16_ADDR	0x23
  
  #define CM_REFFREQ_XIN		(315*1000*1000)/22	/* 14.31818 Mhz reference clock frequency pin XIN */
  #define CM_ADCMULT_XIN		512			/* Guessed (487 best for 44.1kHz, not for 88/176kHz) */
  #define CM_TOLERANCE_RATE	0.001			/* Tolerance sample rate pitch (1000ppm) */
  #define CM_MAXIMUM_RATE		80000000		/* Note more than 80MHz */
  
  #define CM_REG_MIXER1		0x24
  #define CM_FMMUTE		0x80	/* mute FM */
  #define CM_FMMUTE_SHIFT		7
  #define CM_WSMUTE		0x40	/* mute PCM */
  #define CM_WSMUTE_SHIFT		6
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
286
287
  #define CM_REAR2LIN		0x20	/* lin-in -> rear line out */
  #define CM_REAR2LIN_SHIFT	5
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
  #define CM_REAR2FRONT		0x10	/* exchange rear/front */
  #define CM_REAR2FRONT_SHIFT	4
  #define CM_WAVEINL		0x08	/* digital wave rec. left chan */
  #define CM_WAVEINL_SHIFT	3
  #define CM_WAVEINR		0x04	/* digical wave rec. right */
  #define CM_WAVEINR_SHIFT	2
  #define CM_X3DEN		0x02	/* 3D surround enable */
  #define CM_X3DEN_SHIFT		1
  #define CM_CDPLAY		0x01	/* enable SPDIF/IN PCM -> DAC */
  #define CM_CDPLAY_SHIFT		0
  
  #define CM_REG_MIXER2		0x25
  #define CM_RAUXREN		0x80	/* AUX right capture */
  #define CM_RAUXREN_SHIFT	7
  #define CM_RAUXLEN		0x40	/* AUX left capture */
  #define CM_RAUXLEN_SHIFT	6
  #define CM_VAUXRM		0x20	/* AUX right mute */
  #define CM_VAUXRM_SHIFT		5
  #define CM_VAUXLM		0x10	/* AUX left mute */
  #define CM_VAUXLM_SHIFT		4
  #define CM_VADMIC_MASK		0x0e	/* mic gain level (0-3) << 1 */
  #define CM_VADMIC_SHIFT		1
  #define CM_MICGAINZ		0x01	/* mic boost */
  #define CM_MICGAINZ_SHIFT	0
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
312
  #define CM_REG_MIXER3		0x24
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
313
314
315
316
317
  #define CM_REG_AUX_VOL		0x26
  #define CM_VAUXL_MASK		0xf0
  #define CM_VAUXR_MASK		0x0f
  
  #define CM_REG_MISC		0x27
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
318
  #define CM_UNKNOWN_27_MASK	0xd8	/* ? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319
320
321
322
323
  #define CM_XGPO1		0x20
  // #define CM_XGPBIO		0x04
  #define CM_MIC_CENTER_LFE	0x04	/* mic as center/lfe out? (model 039 or later?) */
  #define CM_SPDIF_INVERSE	0x04	/* spdif input phase inverse (model 037) */
  #define CM_SPDVALID		0x02	/* spdif input valid check */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
324
  #define CM_DMAUTO		0x01	/* SB16 DMA auto detect */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  
  #define CM_REG_AC97		0x28	/* hmmm.. do we have ac97 link? */
  /*
   * For CMI-8338 (0x28 - 0x2b) .. is this valid for CMI-8738
   * or identical with AC97 codec?
   */
  #define CM_REG_EXTERN_CODEC	CM_REG_AC97
  
  /*
   * MPU401 pci port index address 0x40 - 0x4f (CMI-8738 spec ver. 0.6)
   */
  #define CM_REG_MPU_PCI		0x40
  
  /*
   * FM pci port index address 0x50 - 0x5f (CMI-8738 spec ver. 0.6)
   */
  #define CM_REG_FM_PCI		0x50
  
  /*
2eff7ec81   Takashi Iwai   [ALSA] cmipci - A...
344
   * access from SB-mixer port
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
345
346
347
348
349
350
351
352
   */
  #define CM_REG_EXTENT_IND	0xf0
  #define CM_VPHONE_MASK		0xe0	/* Phone volume control (0-3) << 5 */
  #define CM_VPHONE_SHIFT		5
  #define CM_VPHOM		0x10	/* Phone mute control */
  #define CM_VSPKM		0x08	/* Speaker mute control, default high */
  #define CM_RLOOPREN		0x04    /* Rec. R-channel enable */
  #define CM_RLOOPLEN		0x02	/* Rec. L-channel enable */
2eff7ec81   Takashi Iwai   [ALSA] cmipci - A...
353
  #define CM_VADMIC3		0x01	/* Mic record boost */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
354
355
356
357
358
359
360
361
362
363
364
  
  /*
   * CMI-8338 spec ver 0.5 (this is not valid for CMI-8738):
   * the 8 registers 0xf8 - 0xff are used for programming m/n counter by the PLL
   * unit (readonly?).
   */
  #define CM_REG_PLL		0xf8
  
  /*
   * extended registers
   */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
365
366
  #define CM_REG_CH0_FRAME1	0x80	/* write: base address */
  #define CM_REG_CH0_FRAME2	0x84	/* read: current address */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
367
368
  #define CM_REG_CH1_FRAME1	0x88	/* 0-15: count of samples at bus master; buffer size */
  #define CM_REG_CH1_FRAME2	0x8C	/* 16-31: count of samples at codec; fragment size */
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
369

cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
370
  #define CM_REG_EXT_MISC		0x90
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
371
372
373
374
375
376
377
378
  #define CM_ADC48K44K		0x10000000	/* ADC parameters group, 0: 44k, 1: 48k */
  #define CM_CHB3D8C		0x00200000	/* 7.1 channels support */
  #define CM_SPD32FMT		0x00100000	/* SPDIF/IN 32k sample rate */
  #define CM_ADC2SPDIF		0x00080000	/* ADC output to SPDIF/OUT */
  #define CM_SHAREADC		0x00040000	/* DAC in ADC as Center/LFE */
  #define CM_REALTCMP		0x00020000	/* monitor the CMPL/CMPR of ADC */
  #define CM_INVLRCK		0x00010000	/* invert ZVPORT's LRCK */
  #define CM_UNKNOWN_90_MASK	0x0000FFFF	/* ? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379
380
381
382
383
384
385
386
387
388
  
  /*
   * size of i/o region
   */
  #define CM_EXTENT_CODEC	  0x100
  #define CM_EXTENT_MIDI	  0x2
  #define CM_EXTENT_SYNTH	  0x4
  
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
   * channels for playback / capture
   */
  #define CM_CH_PLAY	0
  #define CM_CH_CAPT	1
  
  /*
   * flags to check device open/close
   */
  #define CM_OPEN_NONE	0
  #define CM_OPEN_CH_MASK	0x01
  #define CM_OPEN_DAC	0x10
  #define CM_OPEN_ADC	0x20
  #define CM_OPEN_SPDIF	0x40
  #define CM_OPEN_MCHAN	0x80
  #define CM_OPEN_PLAYBACK	(CM_CH_PLAY | CM_OPEN_DAC)
  #define CM_OPEN_PLAYBACK2	(CM_CH_CAPT | CM_OPEN_DAC)
  #define CM_OPEN_PLAYBACK_MULTI	(CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_MCHAN)
  #define CM_OPEN_CAPTURE		(CM_CH_CAPT | CM_OPEN_ADC)
  #define CM_OPEN_SPDIF_PLAYBACK	(CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_SPDIF)
  #define CM_OPEN_SPDIF_CAPTURE	(CM_CH_CAPT | CM_OPEN_ADC | CM_OPEN_SPDIF)
  
  
  #if CM_CH_PLAY == 1
  #define CM_PLAYBACK_SRATE_176K	CM_CH1_SRATE_176K
  #define CM_PLAYBACK_SPDF	CM_SPDF_1
  #define CM_CAPTURE_SPDF		CM_SPDF_0
  #else
  #define CM_PLAYBACK_SRATE_176K CM_CH0_SRATE_176K
  #define CM_PLAYBACK_SPDF	CM_SPDF_0
  #define CM_CAPTURE_SPDF		CM_SPDF_1
  #endif
  
  
  /*
   * driver data
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
425
426
  struct cmipci_pcm {
  	struct snd_pcm_substream *substream;
ebe9e289d   Clemens Ladisch   [ALSA] cmipci: cl...
427
428
429
  	u8 running;		/* dac/adc running? */
  	u8 fmt;			/* format bits */
  	u8 is_dac;
c36fd8c3c   Clemens Ladisch   [ALSA] cmipci: fi...
430
  	u8 needs_silencing;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431
  	unsigned int dma_size;	/* in frames */
ebe9e289d   Clemens Ladisch   [ALSA] cmipci: cl...
432
433
  	unsigned int shift;
  	unsigned int ch;	/* channel (0/1) */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
434
  	unsigned int offset;	/* physical address of the buffer */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
  };
  
  /* mixer elements toggled/resumed during ac3 playback */
  struct cmipci_mixer_auto_switches {
  	const char *name;	/* switch to toggle */
  	int toggle_on;		/* value to change when ac3 mode */
  };
  static const struct cmipci_mixer_auto_switches cm_saved_mixer[] = {
  	{"PCM Playback Switch", 0},
  	{"IEC958 Output Switch", 1},
  	{"IEC958 Mix Analog", 0},
  	// {"IEC958 Out To DAC", 1}, // no longer used
  	{"IEC958 Loop", 0},
  };
  #define CM_SAVED_MIXERS		ARRAY_SIZE(cm_saved_mixer)
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
450
451
  struct cmipci {
  	struct snd_card *card;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
453
454
455
456
457
458
  
  	struct pci_dev *pci;
  	unsigned int device;	/* device ID */
  	int irq;
  
  	unsigned long iobase;
  	unsigned int ctrl;	/* FUNCTRL0 current value */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
459
460
461
  	struct snd_pcm *pcm;		/* DAC/ADC PCM */
  	struct snd_pcm *pcm2;	/* 2nd DAC */
  	struct snd_pcm *pcm_spdif;	/* SPDIF */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
462
463
464
  
  	int chip_version;
  	int max_channels;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
465
466
467
  	unsigned int can_ac3_sw: 1;
  	unsigned int can_ac3_hw: 1;
  	unsigned int can_multi_ch: 1;
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
468
  	unsigned int can_96k: 1;	/* samplerate above 48k */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
469
470
471
472
473
474
475
476
  	unsigned int do_soft_ac3: 1;
  
  	unsigned int spdif_playback_avail: 1;	/* spdif ready? */
  	unsigned int spdif_playback_enabled: 1;	/* spdif switch enabled? */
  	int spdif_counter;	/* for software AC3 */
  
  	unsigned int dig_status;
  	unsigned int dig_pcm_status;
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
477
  	struct snd_pcm_hardware *hw_info[3]; /* for playbacks */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478
479
  
  	int opened[2];	/* open mode */
62932df8f   Ingo Molnar   [ALSA] semaphore ...
480
  	struct mutex open_mutex;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
481
482
  
  	unsigned int mixer_insensitive: 1;
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
483
  	struct snd_kcontrol *mixer_res_ctl[CM_SAVED_MIXERS];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484
  	int mixer_res_status[CM_SAVED_MIXERS];
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
485
  	struct cmipci_pcm channel[2];	/* ch0 - DAC, ch1 - ADC or 2nd DAC */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
486
487
  
  	/* external MIDI */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
488
  	struct snd_rawmidi *rmidi;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
489
490
491
492
493
494
  
  #ifdef SUPPORT_JOYSTICK
  	struct gameport *gameport;
  #endif
  
  	spinlock_t reg_lock;
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
495
496
497
498
499
  
  #ifdef CONFIG_PM
  	unsigned int saved_regs[0x20];
  	unsigned char saved_mixers[0x20];
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
500
501
502
503
  };
  
  
  /* read/write operations for dword register */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
504
  static inline void snd_cmipci_write(struct cmipci *cm, unsigned int cmd, unsigned int data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
505
506
507
  {
  	outl(data, cm->iobase + cmd);
  }
77933d727   Jesper Juhl   [PATCH] clean up ...
508

2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
509
  static inline unsigned int snd_cmipci_read(struct cmipci *cm, unsigned int cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
510
511
512
513
514
  {
  	return inl(cm->iobase + cmd);
  }
  
  /* read/write operations for word register */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
515
  static inline void snd_cmipci_write_w(struct cmipci *cm, unsigned int cmd, unsigned short data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
516
517
518
  {
  	outw(data, cm->iobase + cmd);
  }
77933d727   Jesper Juhl   [PATCH] clean up ...
519

2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
520
  static inline unsigned short snd_cmipci_read_w(struct cmipci *cm, unsigned int cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
521
522
523
524
525
  {
  	return inw(cm->iobase + cmd);
  }
  
  /* read/write operations for byte register */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
526
  static inline void snd_cmipci_write_b(struct cmipci *cm, unsigned int cmd, unsigned char data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
527
528
529
  {
  	outb(data, cm->iobase + cmd);
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
530
  static inline unsigned char snd_cmipci_read_b(struct cmipci *cm, unsigned int cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
531
532
533
534
535
  {
  	return inb(cm->iobase + cmd);
  }
  
  /* bit operations for dword register */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
536
  static int snd_cmipci_set_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
537
  {
01d25d460   Takashi Iwai   [ALSA] Replace pc...
538
539
  	unsigned int val, oval;
  	val = oval = inl(cm->iobase + cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
540
  	val |= flag;
01d25d460   Takashi Iwai   [ALSA] Replace pc...
541
542
  	if (val == oval)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
543
  	outl(val, cm->iobase + cmd);
01d25d460   Takashi Iwai   [ALSA] Replace pc...
544
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
545
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
546
  static int snd_cmipci_clear_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
547
  {
01d25d460   Takashi Iwai   [ALSA] Replace pc...
548
549
  	unsigned int val, oval;
  	val = oval = inl(cm->iobase + cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
550
  	val &= ~flag;
01d25d460   Takashi Iwai   [ALSA] Replace pc...
551
552
  	if (val == oval)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
553
  	outl(val, cm->iobase + cmd);
01d25d460   Takashi Iwai   [ALSA] Replace pc...
554
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
555
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
556
  /* bit operations for byte register */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
557
  static int snd_cmipci_set_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
558
  {
01d25d460   Takashi Iwai   [ALSA] Replace pc...
559
560
  	unsigned char val, oval;
  	val = oval = inb(cm->iobase + cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
561
  	val |= flag;
01d25d460   Takashi Iwai   [ALSA] Replace pc...
562
563
  	if (val == oval)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
  	outb(val, cm->iobase + cmd);
01d25d460   Takashi Iwai   [ALSA] Replace pc...
565
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
566
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
567
  static int snd_cmipci_clear_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
568
  {
01d25d460   Takashi Iwai   [ALSA] Replace pc...
569
570
  	unsigned char val, oval;
  	val = oval = inb(cm->iobase + cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
571
  	val &= ~flag;
01d25d460   Takashi Iwai   [ALSA] Replace pc...
572
573
  	if (val == oval)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
574
  	outb(val, cm->iobase + cmd);
01d25d460   Takashi Iwai   [ALSA] Replace pc...
575
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
576
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
  
  
  /*
   * PCM interface
   */
  
  /*
   * calculate frequency
   */
  
  static unsigned int rates[] = { 5512, 11025, 22050, 44100, 8000, 16000, 32000, 48000 };
  
  static unsigned int snd_cmipci_rate_freq(unsigned int rate)
  {
  	unsigned int i;
0f28eca32   Clemens Ladisch   [ALSA] cmipci: fi...
592

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
  	for (i = 0; i < ARRAY_SIZE(rates); i++) {
  		if (rates[i] == rate)
  			return i;
  	}
  	snd_BUG();
  	return 0;
  }
  
  #ifdef USE_VAR48KRATE
  /*
   * Determine PLL values for frequency setup, maybe the CMI8338 (CMI8738???)
   * does it this way .. maybe not.  Never get any information from C-Media about
   * that <werner@suse.de>.
   */
  static int snd_cmipci_pll_rmn(unsigned int rate, unsigned int adcmult, int *r, int *m, int *n)
  {
  	unsigned int delta, tolerance;
  	int xm, xn, xr;
  
  	for (*r = 0; rate < CM_MAXIMUM_RATE/adcmult; *r += (1<<5))
  		rate <<= 1;
  	*n = -1;
  	if (*r > 0xff)
  		goto out;
  	tolerance = rate*CM_TOLERANCE_RATE;
  
  	for (xn = (1+2); xn < (0x1f+2); xn++) {
  		for (xm = (1+2); xm < (0xff+2); xm++) {
  			xr = ((CM_REFFREQ_XIN/adcmult) * xm) / xn;
  
  			if (xr < rate)
  				delta = rate - xr;
  			else
  				delta = xr - rate;
  
  			/*
  			 * If we found one, remember this,
  			 * and try to find a closer one
  			 */
  			if (delta < tolerance) {
  				tolerance = delta;
  				*m = xm - 2;
  				*n = xn - 2;
  			}
  		}
  	}
  out:
  	return (*n > -1);
  }
  
  /*
25985edce   Lucas De Marchi   Fix common misspe...
644
645
   * Program pll register bits, I assume that the 8 registers 0xf8 up to 0xff
   * are mapped onto the 8 ADC/DAC sampling frequency which can be chosen
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
646
647
648
   * at the register CM_REG_FUNCTRL1 (0x04).
   * Problem: other ways are also possible (any information about that?)
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
649
  static void snd_cmipci_set_pll(struct cmipci *cm, unsigned int rate, unsigned int slot)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
650
651
652
653
  {
  	unsigned int reg = CM_REG_PLL + slot;
  	/*
  	 * Guess that this programs at reg. 0x04 the pos 15:13/12:10
25985edce   Lucas De Marchi   Fix common misspe...
654
  	 * for DSFC/ASFC (000 up to 111).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
655
656
657
658
659
660
661
662
663
664
665
  	 */
  
  	/* FIXME: Init (Do we've to set an other register first before programming?) */
  
  	/* FIXME: Is this correct? Or shouldn't the m/n/r values be used for that? */
  	snd_cmipci_write_b(cm, reg, rate>>8);
  	snd_cmipci_write_b(cm, reg, rate&0xff);
  
  	/* FIXME: Setup (Do we've to set an other register first to enable this?) */
  }
  #endif /* USE_VAR48KRATE */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
666
667
  static int snd_cmipci_hw_params(struct snd_pcm_substream *substream,
  				struct snd_pcm_hw_params *hw_params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
668
669
670
  {
  	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
671
672
  static int snd_cmipci_playback2_hw_params(struct snd_pcm_substream *substream,
  					  struct snd_pcm_hw_params *hw_params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
673
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
674
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
675
  	if (params_channels(hw_params) > 2) {
62932df8f   Ingo Molnar   [ALSA] semaphore ...
676
  		mutex_lock(&cm->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
677
  		if (cm->opened[CM_CH_PLAY]) {
62932df8f   Ingo Molnar   [ALSA] semaphore ...
678
  			mutex_unlock(&cm->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
679
680
681
682
  			return -EBUSY;
  		}
  		/* reserve the channel A */
  		cm->opened[CM_CH_PLAY] = CM_OPEN_PLAYBACK_MULTI;
62932df8f   Ingo Molnar   [ALSA] semaphore ...
683
  		mutex_unlock(&cm->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
684
685
686
  	}
  	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
687
  static void snd_cmipci_ch_reset(struct cmipci *cm, int ch)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
688
689
690
691
692
693
  {
  	int reset = CM_RST_CH0 << (cm->channel[ch].ch);
  	snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
  	snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
  	udelay(10);
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
694
  static int snd_cmipci_hw_free(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
695
696
697
698
699
700
701
  {
  	return snd_pcm_lib_free_pages(substream);
  }
  
  
  /*
   */
35add1c29   Clemens Ladisch   [ALSA] cmipci: re...
702
  static unsigned int hw_channels[] = {1, 2, 4, 6, 8};
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
703
  static struct snd_pcm_hw_constraint_list hw_constraints_channels_4 = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
704
705
706
707
  	.count = 3,
  	.list = hw_channels,
  	.mask = 0,
  };
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
708
  static struct snd_pcm_hw_constraint_list hw_constraints_channels_6 = {
35add1c29   Clemens Ladisch   [ALSA] cmipci: re...
709
  	.count = 4,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
710
711
712
  	.list = hw_channels,
  	.mask = 0,
  };
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
713
  static struct snd_pcm_hw_constraint_list hw_constraints_channels_8 = {
35add1c29   Clemens Ladisch   [ALSA] cmipci: re...
714
  	.count = 5,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
715
716
717
  	.list = hw_channels,
  	.mask = 0,
  };
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
718
  static int set_dac_channels(struct cmipci *cm, struct cmipci_pcm *rec, int channels)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
719
720
  {
  	if (channels > 2) {
8ffbc01e2   Clemens Ladisch   [ALSA] cmipci: re...
721
  		if (!cm->can_multi_ch || !rec->ch)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
722
723
724
  			return -EINVAL;
  		if (rec->fmt != 0x03) /* stereo 16bit only */
  			return -EINVAL;
8ffbc01e2   Clemens Ladisch   [ALSA] cmipci: re...
725
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
726

8ffbc01e2   Clemens Ladisch   [ALSA] cmipci: re...
727
  	if (cm->can_multi_ch) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
728
  		spin_lock_irq(&cm->reg_lock);
8ffbc01e2   Clemens Ladisch   [ALSA] cmipci: re...
729
730
731
  		if (channels > 2) {
  			snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
  			snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
732
  		} else {
8ffbc01e2   Clemens Ladisch   [ALSA] cmipci: re...
733
734
  			snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
  			snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
735
  		}
8ffbc01e2   Clemens Ladisch   [ALSA] cmipci: re...
736
737
738
739
740
741
  		if (channels == 8)
  			snd_cmipci_set_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
  		else
  			snd_cmipci_clear_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
  		if (channels == 6) {
  			snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
742
  			snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
743
  		} else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
744
745
  			snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
  			snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
746
  		}
8ffbc01e2   Clemens Ladisch   [ALSA] cmipci: re...
747
748
749
750
751
  		if (channels == 4)
  			snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
  		else
  			snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
  		spin_unlock_irq(&cm->reg_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
752
753
754
755
756
757
758
759
760
  	}
  	return 0;
  }
  
  
  /*
   * prepare playback/capture channel
   * channel to be used must have been set in rec->ch.
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
761
762
  static int snd_cmipci_pcm_prepare(struct cmipci *cm, struct cmipci_pcm *rec,
  				 struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
763
  {
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
764
  	unsigned int reg, freq, freq_ext, val;
ebe9e289d   Clemens Ladisch   [ALSA] cmipci: cl...
765
  	unsigned int period_size;
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
766
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
  
  	rec->fmt = 0;
  	rec->shift = 0;
  	if (snd_pcm_format_width(runtime->format) >= 16) {
  		rec->fmt |= 0x02;
  		if (snd_pcm_format_width(runtime->format) > 16)
  			rec->shift++; /* 24/32bit */
  	}
  	if (runtime->channels > 1)
  		rec->fmt |= 0x01;
  	if (rec->is_dac && set_dac_channels(cm, rec, runtime->channels) < 0) {
  		snd_printd("cannot set dac channels
  ");
  		return -EINVAL;
  	}
  
  	rec->offset = runtime->dma_addr;
  	/* buffer and period sizes in frame */
  	rec->dma_size = runtime->buffer_size << rec->shift;
ebe9e289d   Clemens Ladisch   [ALSA] cmipci: cl...
786
  	period_size = runtime->period_size << rec->shift;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
787
788
789
  	if (runtime->channels > 2) {
  		/* multi-channels */
  		rec->dma_size = (rec->dma_size * runtime->channels) / 2;
ebe9e289d   Clemens Ladisch   [ALSA] cmipci: cl...
790
  		period_size = (period_size * runtime->channels) / 2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
791
792
793
794
795
796
797
798
799
800
  	}
  
  	spin_lock_irq(&cm->reg_lock);
  
  	/* set buffer address */
  	reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
  	snd_cmipci_write(cm, reg, rec->offset);
  	/* program sample counts */
  	reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
  	snd_cmipci_write_w(cm, reg, rec->dma_size - 1);
ebe9e289d   Clemens Ladisch   [ALSA] cmipci: cl...
801
  	snd_cmipci_write_w(cm, reg + 2, period_size - 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
802
803
804
805
806
807
808
809
810
811
812
813
  
  	/* set adc/dac flag */
  	val = rec->ch ? CM_CHADC1 : CM_CHADC0;
  	if (rec->is_dac)
  		cm->ctrl &= ~val;
  	else
  		cm->ctrl |= val;
  	snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  	//snd_printd("cmipci: functrl0 = %08x
  ", cm->ctrl);
  
  	/* set sample rate */
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
814
815
816
817
818
819
820
821
822
823
824
  	freq = 0;
  	freq_ext = 0;
  	if (runtime->rate > 48000)
  		switch (runtime->rate) {
  		case 88200:  freq_ext = CM_CH0_SRATE_88K; break;
  		case 96000:  freq_ext = CM_CH0_SRATE_96K; break;
  		case 128000: freq_ext = CM_CH0_SRATE_128K; break;
  		default:     snd_BUG(); break;
  		}
  	else
  		freq = snd_cmipci_rate_freq(runtime->rate);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
825
826
  	val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
  	if (rec->ch) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
827
828
  		val &= ~CM_DSFC_MASK;
  		val |= (freq << CM_DSFC_SHIFT) & CM_DSFC_MASK;
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
829
830
831
  	} else {
  		val &= ~CM_ASFC_MASK;
  		val |= (freq << CM_ASFC_SHIFT) & CM_ASFC_MASK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
832
833
834
835
836
837
838
839
840
841
842
843
844
845
  	}
  	snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
  	//snd_printd("cmipci: functrl1 = %08x
  ", val);
  
  	/* set format */
  	val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
  	if (rec->ch) {
  		val &= ~CM_CH1FMT_MASK;
  		val |= rec->fmt << CM_CH1FMT_SHIFT;
  	} else {
  		val &= ~CM_CH0FMT_MASK;
  		val |= rec->fmt << CM_CH0FMT_SHIFT;
  	}
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
846
847
848
  	if (cm->can_96k) {
  		val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
  		val |= freq_ext << (rec->ch * 2);
8992e18db   Clemens Ladisch   [ALSA] cmipci: ad...
849
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
850
851
852
  	snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
  	//snd_printd("cmipci: chformat = %08x
  ", val);
feb77712b   Timofei Bondarenko   [ALSA] cmipci - u...
853
854
855
856
857
858
  	if (!rec->is_dac && cm->chip_version) {
  		if (runtime->rate > 44100)
  			snd_cmipci_set_bit(cm, CM_REG_EXT_MISC, CM_ADC48K44K);
  		else
  			snd_cmipci_clear_bit(cm, CM_REG_EXT_MISC, CM_ADC48K44K);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
859
860
861
862
863
864
865
866
867
  	rec->running = 0;
  	spin_unlock_irq(&cm->reg_lock);
  
  	return 0;
  }
  
  /*
   * PCM trigger/stop
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
868
  static int snd_cmipci_pcm_trigger(struct cmipci *cm, struct cmipci_pcm *rec,
ebe9e289d   Clemens Ladisch   [ALSA] cmipci: cl...
869
  				  int cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
  {
  	unsigned int inthld, chen, reset, pause;
  	int result = 0;
  
  	inthld = CM_CH0_INT_EN << rec->ch;
  	chen = CM_CHEN0 << rec->ch;
  	reset = CM_RST_CH0 << rec->ch;
  	pause = CM_PAUSE0 << rec->ch;
  
  	spin_lock(&cm->reg_lock);
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
  		rec->running = 1;
  		/* set interrupt */
  		snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, inthld);
  		cm->ctrl |= chen;
  		/* enable channel */
  		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  		//snd_printd("cmipci: functrl0 = %08x
  ", cm->ctrl);
  		break;
  	case SNDRV_PCM_TRIGGER_STOP:
  		rec->running = 0;
  		/* disable interrupt */
  		snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, inthld);
  		/* reset */
  		cm->ctrl &= ~chen;
  		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
  		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
c36fd8c3c   Clemens Ladisch   [ALSA] cmipci: fi...
899
  		rec->needs_silencing = rec->is_dac;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
900
901
  		break;
  	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
902
  	case SNDRV_PCM_TRIGGER_SUSPEND:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
903
904
905
906
  		cm->ctrl |= pause;
  		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  		break;
  	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
907
  	case SNDRV_PCM_TRIGGER_RESUME:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
908
909
910
911
912
913
914
915
916
917
918
919
920
921
  		cm->ctrl &= ~pause;
  		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  		break;
  	default:
  		result = -EINVAL;
  		break;
  	}
  	spin_unlock(&cm->reg_lock);
  	return result;
  }
  
  /*
   * return the current pointer
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
922
923
  static snd_pcm_uframes_t snd_cmipci_pcm_pointer(struct cmipci *cm, struct cmipci_pcm *rec,
  						struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
925
  {
  	size_t ptr;
1c583063a   Clemens Ladisch   ALSA: cmipci: wor...
926
  	unsigned int reg, rem, tries;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
927
928
929
930
  	if (!rec->running)
  		return 0;
  #if 1 // this seems better..
  	reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
1c583063a   Clemens Ladisch   ALSA: cmipci: wor...
931
932
933
934
935
936
937
938
939
940
  	for (tries = 0; tries < 3; tries++) {
  		rem = snd_cmipci_read_w(cm, reg);
  		if (rem < rec->dma_size)
  			goto ok;
  	} 
  	printk(KERN_ERR "cmipci: invalid PCM pointer: %#x
  ", rem);
  	return SNDRV_PCM_POS_XRUN;
  ok:
  	ptr = (rec->dma_size - (rem + 1)) >> rec->shift;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
941
942
943
944
945
946
947
948
949
950
951
952
953
  #else
  	reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
  	ptr = snd_cmipci_read(cm, reg) - rec->offset;
  	ptr = bytes_to_frames(substream->runtime, ptr);
  #endif
  	if (substream->runtime->channels > 2)
  		ptr = (ptr * 2) / substream->runtime->channels;
  	return ptr;
  }
  
  /*
   * playback
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
954
  static int snd_cmipci_playback_trigger(struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
955
956
  				       int cmd)
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
957
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
ebe9e289d   Clemens Ladisch   [ALSA] cmipci: cl...
958
  	return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_PLAY], cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
959
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
960
  static snd_pcm_uframes_t snd_cmipci_playback_pointer(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
961
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
962
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
963
964
965
966
967
968
969
970
  	return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_PLAY], substream);
  }
  
  
  
  /*
   * capture
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
971
  static int snd_cmipci_capture_trigger(struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
972
973
  				     int cmd)
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
974
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
ebe9e289d   Clemens Ladisch   [ALSA] cmipci: cl...
975
  	return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_CAPT], cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
976
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
977
  static snd_pcm_uframes_t snd_cmipci_capture_pointer(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
978
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
979
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
980
981
982
983
984
985
986
  	return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_CAPT], substream);
  }
  
  
  /*
   * hw preparation for spdif
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
987
988
  static int snd_cmipci_spdif_default_info(struct snd_kcontrol *kcontrol,
  					 struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
989
990
991
992
993
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  	uinfo->count = 1;
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
994
995
  static int snd_cmipci_spdif_default_get(struct snd_kcontrol *kcontrol,
  					struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
996
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
997
  	struct cmipci *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
998
999
1000
1001
1002
1003
1004
1005
  	int i;
  
  	spin_lock_irq(&chip->reg_lock);
  	for (i = 0; i < 4; i++)
  		ucontrol->value.iec958.status[i] = (chip->dig_status >> (i * 8)) & 0xff;
  	spin_unlock_irq(&chip->reg_lock);
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1006
1007
  static int snd_cmipci_spdif_default_put(struct snd_kcontrol *kcontrol,
  					 struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1008
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1009
  	struct cmipci *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
  	int i, change;
  	unsigned int val;
  
  	val = 0;
  	spin_lock_irq(&chip->reg_lock);
  	for (i = 0; i < 4; i++)
  		val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  	change = val != chip->dig_status;
  	chip->dig_status = val;
  	spin_unlock_irq(&chip->reg_lock);
  	return change;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1022
  static struct snd_kcontrol_new snd_cmipci_spdif_default __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1023
1024
1025
1026
1027
1028
1029
  {
  	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
  	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  	.info =		snd_cmipci_spdif_default_info,
  	.get =		snd_cmipci_spdif_default_get,
  	.put =		snd_cmipci_spdif_default_put
  };
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1030
1031
  static int snd_cmipci_spdif_mask_info(struct snd_kcontrol *kcontrol,
  				      struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1032
1033
1034
1035
1036
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  	uinfo->count = 1;
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1037
1038
  static int snd_cmipci_spdif_mask_get(struct snd_kcontrol *kcontrol,
  				     struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1039
1040
1041
1042
1043
1044
1045
  {
  	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;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1046
  static struct snd_kcontrol_new snd_cmipci_spdif_mask __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1047
1048
  {
  	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
67ed4161f   Clemens Ladisch   [ALSA] sound - fi...
1049
  	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1050
1051
1052
1053
  	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  	.info =		snd_cmipci_spdif_mask_info,
  	.get =		snd_cmipci_spdif_mask_get,
  };
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1054
1055
  static int snd_cmipci_spdif_stream_info(struct snd_kcontrol *kcontrol,
  					struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1056
1057
1058
1059
1060
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  	uinfo->count = 1;
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1061
1062
  static int snd_cmipci_spdif_stream_get(struct snd_kcontrol *kcontrol,
  				       struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1063
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1064
  	struct cmipci *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1065
1066
1067
1068
1069
1070
1071
1072
  	int i;
  
  	spin_lock_irq(&chip->reg_lock);
  	for (i = 0; i < 4; i++)
  		ucontrol->value.iec958.status[i] = (chip->dig_pcm_status >> (i * 8)) & 0xff;
  	spin_unlock_irq(&chip->reg_lock);
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1073
1074
  static int snd_cmipci_spdif_stream_put(struct snd_kcontrol *kcontrol,
  				       struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1075
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1076
  	struct cmipci *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
  	int i, change;
  	unsigned int val;
  
  	val = 0;
  	spin_lock_irq(&chip->reg_lock);
  	for (i = 0; i < 4; i++)
  		val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  	change = val != chip->dig_pcm_status;
  	chip->dig_pcm_status = val;
  	spin_unlock_irq(&chip->reg_lock);
  	return change;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1089
  static struct snd_kcontrol_new snd_cmipci_spdif_stream __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
  {
  	.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_cmipci_spdif_stream_info,
  	.get =		snd_cmipci_spdif_stream_get,
  	.put =		snd_cmipci_spdif_stream_put
  };
  
  /*
   */
  
  /* save mixer setting and mute for AC3 playback */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1103
  static int save_mixer_state(struct cmipci *cm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1104
1105
  {
  	if (! cm->mixer_insensitive) {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1106
  		struct snd_ctl_elem_value *val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1107
1108
1109
1110
1111
1112
  		unsigned int i;
  
  		val = kmalloc(sizeof(*val), GFP_ATOMIC);
  		if (!val)
  			return -ENOMEM;
  		for (i = 0; i < CM_SAVED_MIXERS; i++) {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1113
  			struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
  			if (ctl) {
  				int event;
  				memset(val, 0, sizeof(*val));
  				ctl->get(ctl, val);
  				cm->mixer_res_status[i] = val->value.integer.value[0];
  				val->value.integer.value[0] = cm_saved_mixer[i].toggle_on;
  				event = SNDRV_CTL_EVENT_MASK_INFO;
  				if (cm->mixer_res_status[i] != val->value.integer.value[0]) {
  					ctl->put(ctl, val); /* toggle */
  					event |= SNDRV_CTL_EVENT_MASK_VALUE;
  				}
  				ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  				snd_ctl_notify(cm->card, event, &ctl->id);
  			}
  		}
  		kfree(val);
  		cm->mixer_insensitive = 1;
  	}
  	return 0;
  }
  
  
  /* restore the previously saved mixer status */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1137
  static void restore_mixer_state(struct cmipci *cm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1138
1139
  {
  	if (cm->mixer_insensitive) {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1140
  		struct snd_ctl_elem_value *val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1141
1142
1143
1144
1145
1146
1147
1148
  		unsigned int i;
  
  		val = kmalloc(sizeof(*val), GFP_KERNEL);
  		if (!val)
  			return;
  		cm->mixer_insensitive = 0; /* at first clear this;
  					      otherwise the changes will be ignored */
  		for (i = 0; i < CM_SAVED_MIXERS; i++) {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1149
  			struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
  			if (ctl) {
  				int event;
  
  				memset(val, 0, sizeof(*val));
  				ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  				ctl->get(ctl, val);
  				event = SNDRV_CTL_EVENT_MASK_INFO;
  				if (val->value.integer.value[0] != cm->mixer_res_status[i]) {
  					val->value.integer.value[0] = cm->mixer_res_status[i];
  					ctl->put(ctl, val);
  					event |= SNDRV_CTL_EVENT_MASK_VALUE;
  				}
  				snd_ctl_notify(cm->card, event, &ctl->id);
  			}
  		}
  		kfree(val);
  	}
  }
  
  /* spinlock held! */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1170
  static void setup_ac3(struct cmipci *cm, struct snd_pcm_substream *subs, int do_ac3, int rate)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
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
  {
  	if (do_ac3) {
  		/* AC3EN for 037 */
  		snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
  		/* AC3EN for 039 */
  		snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
  	
  		if (cm->can_ac3_hw) {
  			/* SPD24SEL for 037, 0x02 */
  			/* SPD24SEL for 039, 0x20, but cannot be set */
  			snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  			snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  		} else { /* can_ac3_sw */
  			/* SPD32SEL for 037 & 039, 0x20 */
  			snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  			/* set 176K sample rate to fix 033 HW bug */
  			if (cm->chip_version == 33) {
  				if (rate >= 48000) {
  					snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  				} else {
  					snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  				}
  			}
  		}
  
  	} else {
  		snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
  		snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
  
  		if (cm->can_ac3_hw) {
  			/* chip model >= 37 */
  			if (snd_pcm_format_width(subs->runtime->format) > 16) {
  				snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  				snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  			} else {
  				snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  				snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  			}
  		} else {
  			snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  			snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  			snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  		}
  	}
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1216
  static int setup_spdif_playback(struct cmipci *cm, struct snd_pcm_substream *subs, int up, int do_ac3)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
  {
  	int rate, err;
  
  	rate = subs->runtime->rate;
  
  	if (up && do_ac3)
  		if ((err = save_mixer_state(cm)) < 0)
  			return err;
  
  	spin_lock_irq(&cm->reg_lock);
  	cm->spdif_playback_avail = up;
  	if (up) {
  		/* they are controlled via "IEC958 Output Switch" */
  		/* snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
  		/* snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
  		if (cm->spdif_playback_enabled)
  			snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  		setup_ac3(cm, subs, do_ac3, rate);
8992e18db   Clemens Ladisch   [ALSA] cmipci: ad...
1235
  		if (rate == 48000 || rate == 96000)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1236
1237
1238
  			snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
  		else
  			snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
8992e18db   Clemens Ladisch   [ALSA] cmipci: ad...
1239
1240
1241
1242
  		if (rate > 48000)
  			snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
  		else
  			snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1243
1244
1245
1246
  	} else {
  		/* they are controlled via "IEC958 Output Switch" */
  		/* snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
  		/* snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
8992e18db   Clemens Ladisch   [ALSA] cmipci: ad...
1247
  		snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
  		snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  		setup_ac3(cm, subs, 0, 0);
  	}
  	spin_unlock_irq(&cm->reg_lock);
  	return 0;
  }
  
  
  /*
   * preparation
   */
  
  /* playback - enable spdif only on the certain condition */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1261
  static int snd_cmipci_playback_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1262
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1263
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1264
1265
  	int rate = substream->runtime->rate;
  	int err, do_spdif, do_ac3 = 0;
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
1266
  	do_spdif = (rate >= 44100 && rate <= 96000 &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
  		    substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE &&
  		    substream->runtime->channels == 2);
  	if (do_spdif && cm->can_ac3_hw) 
  		do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
  	if ((err = setup_spdif_playback(cm, substream, do_spdif, do_ac3)) < 0)
  		return err;
  	return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
  }
  
  /* playback  (via device #2) - enable spdif always */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1277
  static int snd_cmipci_playback_spdif_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1278
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1279
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
  	int err, do_ac3;
  
  	if (cm->can_ac3_hw) 
  		do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
  	else
  		do_ac3 = 1; /* doesn't matter */
  	if ((err = setup_spdif_playback(cm, substream, 1, do_ac3)) < 0)
  		return err;
  	return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
  }
c36fd8c3c   Clemens Ladisch   [ALSA] cmipci: fi...
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
  /*
   * Apparently, the samples last played on channel A stay in some buffer, even
   * after the channel is reset, and get added to the data for the rear DACs when
   * playing a multichannel stream on channel B.  This is likely to generate
   * wraparounds and thus distortions.
   * To avoid this, we play at least one zero sample after the actual stream has
   * stopped.
   */
  static void snd_cmipci_silence_hack(struct cmipci *cm, struct cmipci_pcm *rec)
  {
  	struct snd_pcm_runtime *runtime = rec->substream->runtime;
  	unsigned int reg, val;
  
  	if (rec->needs_silencing && runtime && runtime->dma_area) {
  		/* set up a small silence buffer */
  		memset(runtime->dma_area, 0, PAGE_SIZE);
  		reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
  		val = ((PAGE_SIZE / 4) - 1) | (((PAGE_SIZE / 4) / 2 - 1) << 16);
  		snd_cmipci_write(cm, reg, val);
  	
  		/* configure for 16 bits, 2 channels, 8 kHz */
  		if (runtime->channels > 2)
  			set_dac_channels(cm, rec, 2);
  		spin_lock_irq(&cm->reg_lock);
  		val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
  		val &= ~(CM_ASFC_MASK << (rec->ch * 3));
  		val |= (4 << CM_ASFC_SHIFT) << (rec->ch * 3);
  		snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
  		val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
  		val &= ~(CM_CH0FMT_MASK << (rec->ch * 2));
  		val |= (3 << CM_CH0FMT_SHIFT) << (rec->ch * 2);
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
1321
1322
  		if (cm->can_96k)
  			val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
c36fd8c3c   Clemens Ladisch   [ALSA] cmipci: fi...
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
  		snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
  	
  		/* start stream (we don't need interrupts) */
  		cm->ctrl |= CM_CHEN0 << rec->ch;
  		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  		spin_unlock_irq(&cm->reg_lock);
  
  		msleep(1);
  
  		/* stop and reset stream */
  		spin_lock_irq(&cm->reg_lock);
  		cm->ctrl &= ~(CM_CHEN0 << rec->ch);
  		val = CM_RST_CH0 << rec->ch;
  		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | val);
  		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~val);
  		spin_unlock_irq(&cm->reg_lock);
  
  		rec->needs_silencing = 0;
  	}
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1343
  static int snd_cmipci_playback_hw_free(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1344
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1345
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1346
1347
  	setup_spdif_playback(cm, substream, 0, 0);
  	restore_mixer_state(cm);
c36fd8c3c   Clemens Ladisch   [ALSA] cmipci: fi...
1348
1349
1350
1351
1352
1353
1354
1355
  	snd_cmipci_silence_hack(cm, &cm->channel[0]);
  	return snd_cmipci_hw_free(substream);
  }
  
  static int snd_cmipci_playback2_hw_free(struct snd_pcm_substream *substream)
  {
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
  	snd_cmipci_silence_hack(cm, &cm->channel[1]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1356
1357
1358
1359
  	return snd_cmipci_hw_free(substream);
  }
  
  /* capture */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1360
  static int snd_cmipci_capture_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1361
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1362
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1363
1364
1365
1366
  	return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
  }
  
  /* capture with spdif (via device #2) */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1367
  static int snd_cmipci_capture_spdif_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1368
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1369
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1370
1371
1372
  
  	spin_lock_irq(&cm->reg_lock);
  	snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
1373
1374
1375
1376
1377
1378
  	if (cm->can_96k) {
  		if (substream->runtime->rate > 48000)
  			snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
  		else
  			snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
  	}
b46be7272   Timofei Bondarenko   [ALSA] cmipci - a...
1379
1380
1381
1382
  	if (snd_pcm_format_width(substream->runtime->format) > 16)
  		snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  	else
  		snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1383
1384
1385
1386
  	spin_unlock_irq(&cm->reg_lock);
  
  	return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1387
  static int snd_cmipci_capture_spdif_hw_free(struct snd_pcm_substream *subs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1388
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1389
  	struct cmipci *cm = snd_pcm_substream_chip(subs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1390
1391
1392
  
  	spin_lock_irq(&cm->reg_lock);
  	snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
b46be7272   Timofei Bondarenko   [ALSA] cmipci - a...
1393
  	snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1394
1395
1396
1397
1398
1399
1400
1401
1402
  	spin_unlock_irq(&cm->reg_lock);
  
  	return snd_cmipci_hw_free(subs);
  }
  
  
  /*
   * interrupt handler
   */
7d12e780e   David Howells   IRQ: Maintain reg...
1403
  static irqreturn_t snd_cmipci_interrupt(int irq, void *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1404
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1405
  	struct cmipci *cm = dev_id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
  	unsigned int status, mask = 0;
  	
  	/* fastpath out, to ease interrupt sharing */
  	status = snd_cmipci_read(cm, CM_REG_INT_STATUS);
  	if (!(status & CM_INTR))
  		return IRQ_NONE;
  
  	/* acknowledge interrupt */
  	spin_lock(&cm->reg_lock);
  	if (status & CM_CHINT0)
  		mask |= CM_CH0_INT_EN;
  	if (status & CM_CHINT1)
  		mask |= CM_CH1_INT_EN;
  	snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, mask);
  	snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, mask);
  	spin_unlock(&cm->reg_lock);
  
  	if (cm->rmidi && (status & CM_UARTINT))
7d12e780e   David Howells   IRQ: Maintain reg...
1424
  		snd_mpu401_uart_interrupt(irq, cm->rmidi->private_data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
  
  	if (cm->pcm) {
  		if ((status & CM_CHINT0) && cm->channel[0].running)
  			snd_pcm_period_elapsed(cm->channel[0].substream);
  		if ((status & CM_CHINT1) && cm->channel[1].running)
  			snd_pcm_period_elapsed(cm->channel[1].substream);
  	}
  	return IRQ_HANDLED;
  }
  
  /*
   * h/w infos
   */
  
  /* playback on channel A */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1440
  static struct snd_pcm_hardware snd_cmipci_playback =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1441
1442
1443
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
1444
  				 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
  	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  	.rates =		SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  	.rate_min =		5512,
  	.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 =		2,
  	.periods_max =		1024,
  	.fifo_size =		0,
  };
  
  /* capture on channel B */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1460
  static struct snd_pcm_hardware snd_cmipci_capture =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1461
1462
1463
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
1464
  				 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
  	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  	.rates =		SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  	.rate_min =		5512,
  	.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 =		2,
  	.periods_max =		1024,
  	.fifo_size =		0,
  };
  
  /* playback on channel B - stereo 16bit only? */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1480
  static struct snd_pcm_hardware snd_cmipci_playback2 =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1481
1482
1483
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
1484
  				 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
  	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
  	.rates =		SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  	.rate_min =		5512,
  	.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 =		2,
  	.periods_max =		1024,
  	.fifo_size =		0,
  };
  
  /* spdif playback on channel A */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1500
  static struct snd_pcm_hardware snd_cmipci_playback_spdif =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1501
1502
1503
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
1504
  				 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
  	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
  	.rates =		SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  	.rate_min =		44100,
  	.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 =		2,
  	.periods_max =		1024,
  	.fifo_size =		0,
  };
  
  /* spdif playback on channel A (32bit, IEC958 subframes) */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1520
  static struct snd_pcm_hardware snd_cmipci_playback_iec958_subframe =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1521
1522
1523
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
1524
  				 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
  	.formats =		SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  	.rates =		SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  	.rate_min =		44100,
  	.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 =		2,
  	.periods_max =		1024,
  	.fifo_size =		0,
  };
  
  /* spdif capture on channel B */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1540
  static struct snd_pcm_hardware snd_cmipci_capture_spdif =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1541
1542
1543
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
1544
  				 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
b46be7272   Timofei Bondarenko   [ALSA] cmipci - a...
1545
1546
  	.formats =	        SNDRV_PCM_FMTBIT_S16_LE |
  				SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
  	.rates =		SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  	.rate_min =		44100,
  	.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 =		2,
  	.periods_max =		1024,
  	.fifo_size =		0,
  };
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
1559
1560
1561
1562
1563
1564
1565
  static unsigned int rate_constraints[] = { 5512, 8000, 11025, 16000, 22050,
  			32000, 44100, 48000, 88200, 96000, 128000 };
  static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  		.count = ARRAY_SIZE(rate_constraints),
  		.list = rate_constraints,
  		.mask = 0,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1566
1567
1568
  /*
   * check device open/close
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1569
  static int open_device_check(struct cmipci *cm, int mode, struct snd_pcm_substream *subs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1570
1571
1572
1573
1574
1575
1576
1577
  {
  	int ch = mode & CM_OPEN_CH_MASK;
  
  	/* FIXME: a file should wait until the device becomes free
  	 * when it's opened on blocking mode.  however, since the current
  	 * pcm framework doesn't pass file pointer before actually opened,
  	 * we can't know whether blocking mode or not in open callback..
  	 */
62932df8f   Ingo Molnar   [ALSA] semaphore ...
1578
  	mutex_lock(&cm->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1579
  	if (cm->opened[ch]) {
62932df8f   Ingo Molnar   [ALSA] semaphore ...
1580
  		mutex_unlock(&cm->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
  		return -EBUSY;
  	}
  	cm->opened[ch] = mode;
  	cm->channel[ch].substream = subs;
  	if (! (mode & CM_OPEN_DAC)) {
  		/* disable dual DAC mode */
  		cm->channel[ch].is_dac = 0;
  		spin_lock_irq(&cm->reg_lock);
  		snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
  		spin_unlock_irq(&cm->reg_lock);
  	}
62932df8f   Ingo Molnar   [ALSA] semaphore ...
1592
  	mutex_unlock(&cm->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1593
1594
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1595
  static void close_device_check(struct cmipci *cm, int mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1596
1597
  {
  	int ch = mode & CM_OPEN_CH_MASK;
62932df8f   Ingo Molnar   [ALSA] semaphore ...
1598
  	mutex_lock(&cm->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
  	if (cm->opened[ch] == mode) {
  		if (cm->channel[ch].substream) {
  			snd_cmipci_ch_reset(cm, ch);
  			cm->channel[ch].running = 0;
  			cm->channel[ch].substream = NULL;
  		}
  		cm->opened[ch] = 0;
  		if (! cm->channel[ch].is_dac) {
  			/* enable dual DAC mode again */
  			cm->channel[ch].is_dac = 1;
  			spin_lock_irq(&cm->reg_lock);
  			snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
  			spin_unlock_irq(&cm->reg_lock);
  		}
  	}
62932df8f   Ingo Molnar   [ALSA] semaphore ...
1614
  	mutex_unlock(&cm->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1615
1616
1617
1618
  }
  
  /*
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1619
  static int snd_cmipci_playback_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1620
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1621
1622
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1623
1624
1625
1626
1627
  	int err;
  
  	if ((err = open_device_check(cm, CM_OPEN_PLAYBACK, substream)) < 0)
  		return err;
  	runtime->hw = snd_cmipci_playback;
8992e18db   Clemens Ladisch   [ALSA] cmipci: ad...
1628
1629
1630
1631
  	if (cm->chip_version == 68) {
  		runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
  				     SNDRV_PCM_RATE_96000;
  		runtime->hw.rate_max = 96000;
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
1632
1633
1634
1635
1636
1637
1638
  	} else if (cm->chip_version == 55) {
  		err = snd_pcm_hw_constraint_list(runtime, 0,
  			SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  		if (err < 0)
  			return err;
  		runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
  		runtime->hw.rate_max = 128000;
8992e18db   Clemens Ladisch   [ALSA] cmipci: ad...
1639
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1640
1641
1642
1643
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
  	cm->dig_pcm_status = cm->dig_status;
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1644
  static int snd_cmipci_capture_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1645
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1646
1647
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1648
1649
1650
1651
1652
1653
1654
1655
  	int err;
  
  	if ((err = open_device_check(cm, CM_OPEN_CAPTURE, substream)) < 0)
  		return err;
  	runtime->hw = snd_cmipci_capture;
  	if (cm->chip_version == 68) {	// 8768 only supports 44k/48k recording
  		runtime->hw.rate_min = 41000;
  		runtime->hw.rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000;
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
1656
1657
1658
1659
1660
1661
1662
  	} else if (cm->chip_version == 55) {
  		err = snd_pcm_hw_constraint_list(runtime, 0,
  			SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  		if (err < 0)
  			return err;
  		runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
  		runtime->hw.rate_max = 128000;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1663
1664
1665
1666
  	}
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1667
  static int snd_cmipci_playback2_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1668
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1669
1670
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1671
1672
1673
1674
1675
  	int err;
  
  	if ((err = open_device_check(cm, CM_OPEN_PLAYBACK2, substream)) < 0) /* use channel B */
  		return err;
  	runtime->hw = snd_cmipci_playback2;
62932df8f   Ingo Molnar   [ALSA] semaphore ...
1676
  	mutex_lock(&cm->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
  	if (! cm->opened[CM_CH_PLAY]) {
  		if (cm->can_multi_ch) {
  			runtime->hw.channels_max = cm->max_channels;
  			if (cm->max_channels == 4)
  				snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_4);
  			else if (cm->max_channels == 6)
  				snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_6);
  			else if (cm->max_channels == 8)
  				snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_8);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1687
  	}
62932df8f   Ingo Molnar   [ALSA] semaphore ...
1688
  	mutex_unlock(&cm->open_mutex);
22a22f5aa   Clemens Ladisch   [ALSA] cmipci: al...
1689
1690
1691
1692
  	if (cm->chip_version == 68) {
  		runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
  				     SNDRV_PCM_RATE_96000;
  		runtime->hw.rate_max = 96000;
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
1693
1694
1695
1696
1697
1698
1699
  	} else if (cm->chip_version == 55) {
  		err = snd_pcm_hw_constraint_list(runtime, 0,
  			SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  		if (err < 0)
  			return err;
  		runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
  		runtime->hw.rate_max = 128000;
22a22f5aa   Clemens Ladisch   [ALSA] cmipci: al...
1700
1701
  	}
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1702
1703
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1704
  static int snd_cmipci_playback_spdif_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1705
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1706
1707
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1708
1709
1710
1711
1712
1713
  	int err;
  
  	if ((err = open_device_check(cm, CM_OPEN_SPDIF_PLAYBACK, substream)) < 0) /* use channel A */
  		return err;
  	if (cm->can_ac3_hw) {
  		runtime->hw = snd_cmipci_playback_spdif;
57bd68b8b   Clemens Ladisch   [ALSA] cmipci: ad...
1714
  		if (cm->chip_version >= 37) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1715
  			runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
57bd68b8b   Clemens Ladisch   [ALSA] cmipci: ad...
1716
1717
  			snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  		}
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
1718
  		if (cm->can_96k) {
8992e18db   Clemens Ladisch   [ALSA] cmipci: ad...
1719
1720
1721
1722
  			runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
  					     SNDRV_PCM_RATE_96000;
  			runtime->hw.rate_max = 96000;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1723
1724
1725
1726
1727
1728
1729
  	} else {
  		runtime->hw = snd_cmipci_playback_iec958_subframe;
  	}
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
  	cm->dig_pcm_status = cm->dig_status;
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1730
  static int snd_cmipci_capture_spdif_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1731
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1732
1733
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1734
1735
1736
1737
1738
  	int err;
  
  	if ((err = open_device_check(cm, CM_OPEN_SPDIF_CAPTURE, substream)) < 0) /* use channel B */
  		return err;
  	runtime->hw = snd_cmipci_capture_spdif;
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
1739
1740
1741
1742
1743
  	if (cm->can_96k && !(cm->chip_version == 68)) {
  		runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
  				     SNDRV_PCM_RATE_96000;
  		runtime->hw.rate_max = 96000;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1744
1745
1746
1747
1748
1749
1750
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
  	return 0;
  }
  
  
  /*
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1751
  static int snd_cmipci_playback_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1752
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1753
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1754
1755
1756
  	close_device_check(cm, CM_OPEN_PLAYBACK);
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1757
  static int snd_cmipci_capture_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1758
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1759
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1760
1761
1762
  	close_device_check(cm, CM_OPEN_CAPTURE);
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1763
  static int snd_cmipci_playback2_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1764
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1765
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1766
1767
1768
1769
  	close_device_check(cm, CM_OPEN_PLAYBACK2);
  	close_device_check(cm, CM_OPEN_PLAYBACK_MULTI);
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1770
  static int snd_cmipci_playback_spdif_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1771
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1772
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1773
1774
1775
  	close_device_check(cm, CM_OPEN_SPDIF_PLAYBACK);
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1776
  static int snd_cmipci_capture_spdif_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1777
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1778
  	struct cmipci *cm = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1779
1780
1781
1782
1783
1784
1785
  	close_device_check(cm, CM_OPEN_SPDIF_CAPTURE);
  	return 0;
  }
  
  
  /*
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1786
  static struct snd_pcm_ops snd_cmipci_playback_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1787
1788
1789
1790
1791
1792
1793
1794
1795
  	.open =		snd_cmipci_playback_open,
  	.close =	snd_cmipci_playback_close,
  	.ioctl =	snd_pcm_lib_ioctl,
  	.hw_params =	snd_cmipci_hw_params,
  	.hw_free =	snd_cmipci_playback_hw_free,
  	.prepare =	snd_cmipci_playback_prepare,
  	.trigger =	snd_cmipci_playback_trigger,
  	.pointer =	snd_cmipci_playback_pointer,
  };
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1796
  static struct snd_pcm_ops snd_cmipci_capture_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1797
1798
1799
1800
1801
1802
1803
1804
1805
  	.open =		snd_cmipci_capture_open,
  	.close =	snd_cmipci_capture_close,
  	.ioctl =	snd_pcm_lib_ioctl,
  	.hw_params =	snd_cmipci_hw_params,
  	.hw_free =	snd_cmipci_hw_free,
  	.prepare =	snd_cmipci_capture_prepare,
  	.trigger =	snd_cmipci_capture_trigger,
  	.pointer =	snd_cmipci_capture_pointer,
  };
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1806
  static struct snd_pcm_ops snd_cmipci_playback2_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1807
1808
1809
1810
  	.open =		snd_cmipci_playback2_open,
  	.close =	snd_cmipci_playback2_close,
  	.ioctl =	snd_pcm_lib_ioctl,
  	.hw_params =	snd_cmipci_playback2_hw_params,
c36fd8c3c   Clemens Ladisch   [ALSA] cmipci: fi...
1811
  	.hw_free =	snd_cmipci_playback2_hw_free,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1812
1813
1814
1815
  	.prepare =	snd_cmipci_capture_prepare,	/* channel B */
  	.trigger =	snd_cmipci_capture_trigger,	/* channel B */
  	.pointer =	snd_cmipci_capture_pointer,	/* channel B */
  };
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1816
  static struct snd_pcm_ops snd_cmipci_playback_spdif_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1817
1818
1819
1820
1821
1822
1823
1824
1825
  	.open =		snd_cmipci_playback_spdif_open,
  	.close =	snd_cmipci_playback_spdif_close,
  	.ioctl =	snd_pcm_lib_ioctl,
  	.hw_params =	snd_cmipci_hw_params,
  	.hw_free =	snd_cmipci_playback_hw_free,
  	.prepare =	snd_cmipci_playback_spdif_prepare,	/* set up rate */
  	.trigger =	snd_cmipci_playback_trigger,
  	.pointer =	snd_cmipci_playback_pointer,
  };
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1826
  static struct snd_pcm_ops snd_cmipci_capture_spdif_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
  	.open =		snd_cmipci_capture_spdif_open,
  	.close =	snd_cmipci_capture_spdif_close,
  	.ioctl =	snd_pcm_lib_ioctl,
  	.hw_params =	snd_cmipci_hw_params,
  	.hw_free =	snd_cmipci_capture_spdif_hw_free,
  	.prepare =	snd_cmipci_capture_spdif_prepare,
  	.trigger =	snd_cmipci_capture_trigger,
  	.pointer =	snd_cmipci_capture_pointer,
  };
  
  
  /*
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1840
  static int __devinit snd_cmipci_pcm_new(struct cmipci *cm, int device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1841
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1842
  	struct snd_pcm *pcm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
  	int err;
  
  	err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
  	if (err < 0)
  		return err;
  
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_ops);
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_ops);
  
  	pcm->private_data = cm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1853
1854
1855
1856
1857
1858
1859
1860
1861
  	pcm->info_flags = 0;
  	strcpy(pcm->name, "C-Media PCI DAC/ADC");
  	cm->pcm = pcm;
  
  	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  					      snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1862
  static int __devinit snd_cmipci_pcm2_new(struct cmipci *cm, int device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1863
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1864
  	struct snd_pcm *pcm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1865
1866
1867
1868
1869
1870
1871
1872
1873
  	int err;
  
  	err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 0, &pcm);
  	if (err < 0)
  		return err;
  
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback2_ops);
  
  	pcm->private_data = cm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1874
1875
1876
1877
1878
1879
1880
1881
1882
  	pcm->info_flags = 0;
  	strcpy(pcm->name, "C-Media PCI 2nd DAC");
  	cm->pcm2 = pcm;
  
  	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  					      snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1883
  static int __devinit snd_cmipci_pcm_spdif_new(struct cmipci *cm, int device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1884
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1885
  	struct snd_pcm *pcm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
  	int err;
  
  	err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
  	if (err < 0)
  		return err;
  
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_spdif_ops);
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_spdif_ops);
  
  	pcm->private_data = cm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
  	pcm->info_flags = 0;
  	strcpy(pcm->name, "C-Media PCI IEC958");
  	cm->pcm_spdif = pcm;
  
  	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  					      snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  
  	return 0;
  }
  
  /*
   * mixer interface:
   * - CM8338/8738 has a compatible mixer interface with SB16, but
   *   lack of some elements like tone control, i/o gain and AGC.
   * - Access to native registers:
   *   - A 3D switch
   *   - Output mute switches
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1914
  static void snd_cmipci_mixer_write(struct cmipci *s, unsigned char idx, unsigned char data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1915
1916
1917
1918
  {
  	outb(idx, s->iobase + CM_REG_SB16_ADDR);
  	outb(data, s->iobase + CM_REG_SB16_DATA);
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1919
  static unsigned char snd_cmipci_mixer_read(struct cmipci *s, unsigned char idx)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
  {
  	unsigned char v;
  
  	outb(idx, s->iobase + CM_REG_SB16_ADDR);
  	v = inb(s->iobase + CM_REG_SB16_DATA);
  	return v;
  }
  
  /*
   * general mixer element
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1931
  struct cmipci_sb_reg {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1932
1933
1934
1935
1936
  	unsigned int left_reg, right_reg;
  	unsigned int left_shift, right_shift;
  	unsigned int mask;
  	unsigned int invert: 1;
  	unsigned int stereo: 1;
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1937
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
  
  #define COMPOSE_SB_REG(lreg,rreg,lshift,rshift,mask,invert,stereo) \
   ((lreg) | ((rreg) << 8) | (lshift << 16) | (rshift << 19) | (mask << 24) | (invert << 22) | (stereo << 23))
  
  #define CMIPCI_DOUBLE(xname, left_reg, right_reg, left_shift, right_shift, mask, invert, stereo) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
    .info = snd_cmipci_info_volume, \
    .get = snd_cmipci_get_volume, .put = snd_cmipci_put_volume, \
    .private_value = COMPOSE_SB_REG(left_reg, right_reg, left_shift, right_shift, mask, invert, stereo), \
  }
  
  #define CMIPCI_SB_VOL_STEREO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg+1, shift, shift, mask, 0, 1)
  #define CMIPCI_SB_VOL_MONO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg, shift, shift, mask, 0, 0)
  #define CMIPCI_SB_SW_STEREO(xname,lshift,rshift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, lshift, rshift, 1, 0, 1)
  #define CMIPCI_SB_SW_MONO(xname,shift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, shift, shift, 1, 0, 0)
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1953
  static void cmipci_sb_reg_decode(struct cmipci_sb_reg *r, unsigned long val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1954
1955
1956
1957
1958
1959
1960
1961
1962
  {
  	r->left_reg = val & 0xff;
  	r->right_reg = (val >> 8) & 0xff;
  	r->left_shift = (val >> 16) & 0x07;
  	r->right_shift = (val >> 19) & 0x07;
  	r->invert = (val >> 22) & 1;
  	r->stereo = (val >> 23) & 1;
  	r->mask = (val >> 24) & 0xff;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1963
1964
  static int snd_cmipci_info_volume(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1965
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1966
  	struct cmipci_sb_reg reg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1967
1968
1969
1970
1971
1972
1973
1974
1975
  
  	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  	uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = reg.stereo + 1;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = reg.mask;
  	return 0;
  }
   
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1976
1977
  static int snd_cmipci_get_volume(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1978
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1979
1980
  	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  	struct cmipci_sb_reg reg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
  	int val;
  
  	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  	spin_lock_irq(&cm->reg_lock);
  	val = (snd_cmipci_mixer_read(cm, reg.left_reg) >> reg.left_shift) & reg.mask;
  	if (reg.invert)
  		val = reg.mask - val;
  	ucontrol->value.integer.value[0] = val;
  	if (reg.stereo) {
  		val = (snd_cmipci_mixer_read(cm, reg.right_reg) >> reg.right_shift) & reg.mask;
  		if (reg.invert)
  			val = reg.mask - val;
  		 ucontrol->value.integer.value[1] = val;
  	}
  	spin_unlock_irq(&cm->reg_lock);
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
1998
1999
  static int snd_cmipci_put_volume(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2000
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2001
2002
  	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  	struct cmipci_sb_reg reg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
  	int change;
  	int left, right, oleft, oright;
  
  	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  	left = ucontrol->value.integer.value[0] & reg.mask;
  	if (reg.invert)
  		left = reg.mask - left;
  	left <<= reg.left_shift;
  	if (reg.stereo) {
  		right = ucontrol->value.integer.value[1] & reg.mask;
  		if (reg.invert)
  			right = reg.mask - right;
  		right <<= reg.right_shift;
  	} else
  		right = 0;
  	spin_lock_irq(&cm->reg_lock);
  	oleft = snd_cmipci_mixer_read(cm, reg.left_reg);
  	left |= oleft & ~(reg.mask << reg.left_shift);
  	change = left != oleft;
  	if (reg.stereo) {
  		if (reg.left_reg != reg.right_reg) {
  			snd_cmipci_mixer_write(cm, reg.left_reg, left);
  			oright = snd_cmipci_mixer_read(cm, reg.right_reg);
  		} else
  			oright = left;
  		right |= oright & ~(reg.mask << reg.right_shift);
  		change |= right != oright;
  		snd_cmipci_mixer_write(cm, reg.right_reg, right);
  	} else
  		snd_cmipci_mixer_write(cm, reg.left_reg, left);
  	spin_unlock_irq(&cm->reg_lock);
  	return change;
  }
  
  /*
   * input route (left,right) -> (left,right)
   */
  #define CMIPCI_SB_INPUT_SW(xname, left_shift, right_shift) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
    .info = snd_cmipci_info_input_sw, \
    .get = snd_cmipci_get_input_sw, .put = snd_cmipci_put_input_sw, \
    .private_value = COMPOSE_SB_REG(SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, left_shift, right_shift, 1, 0, 1), \
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2046
2047
  static int snd_cmipci_info_input_sw(struct snd_kcontrol *kcontrol,
  				    struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2048
2049
2050
2051
2052
2053
2054
2055
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  	uinfo->count = 4;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = 1;
  	return 0;
  }
   
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2056
2057
  static int snd_cmipci_get_input_sw(struct snd_kcontrol *kcontrol,
  				   struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2058
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2059
2060
  	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  	struct cmipci_sb_reg reg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
  	int val1, val2;
  
  	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  	spin_lock_irq(&cm->reg_lock);
  	val1 = snd_cmipci_mixer_read(cm, reg.left_reg);
  	val2 = snd_cmipci_mixer_read(cm, reg.right_reg);
  	spin_unlock_irq(&cm->reg_lock);
  	ucontrol->value.integer.value[0] = (val1 >> reg.left_shift) & 1;
  	ucontrol->value.integer.value[1] = (val2 >> reg.left_shift) & 1;
  	ucontrol->value.integer.value[2] = (val1 >> reg.right_shift) & 1;
  	ucontrol->value.integer.value[3] = (val2 >> reg.right_shift) & 1;
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2074
2075
  static int snd_cmipci_put_input_sw(struct snd_kcontrol *kcontrol,
  				   struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2076
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2077
2078
  	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  	struct cmipci_sb_reg reg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
  	int change;
  	int val1, val2, oval1, oval2;
  
  	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  	spin_lock_irq(&cm->reg_lock);
  	oval1 = snd_cmipci_mixer_read(cm, reg.left_reg);
  	oval2 = snd_cmipci_mixer_read(cm, reg.right_reg);
  	val1 = oval1 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
  	val2 = oval2 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
  	val1 |= (ucontrol->value.integer.value[0] & 1) << reg.left_shift;
  	val2 |= (ucontrol->value.integer.value[1] & 1) << reg.left_shift;
  	val1 |= (ucontrol->value.integer.value[2] & 1) << reg.right_shift;
  	val2 |= (ucontrol->value.integer.value[3] & 1) << reg.right_shift;
  	change = val1 != oval1 || val2 != oval2;
  	snd_cmipci_mixer_write(cm, reg.left_reg, val1);
  	snd_cmipci_mixer_write(cm, reg.right_reg, val2);
  	spin_unlock_irq(&cm->reg_lock);
  	return change;
  }
  
  /*
   * native mixer switches/volumes
   */
  
  #define CMIPCI_MIXER_SW_STEREO(xname, reg, lshift, rshift, invert) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
    .info = snd_cmipci_info_native_mixer, \
    .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
    .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, 1, invert, 1), \
  }
  
  #define CMIPCI_MIXER_SW_MONO(xname, reg, shift, invert) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
    .info = snd_cmipci_info_native_mixer, \
    .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
    .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, 1, invert, 0), \
  }
  
  #define CMIPCI_MIXER_VOL_STEREO(xname, reg, lshift, rshift, mask) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
    .info = snd_cmipci_info_native_mixer, \
    .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
    .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, mask, 0, 1), \
  }
  
  #define CMIPCI_MIXER_VOL_MONO(xname, reg, shift, mask) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
    .info = snd_cmipci_info_native_mixer, \
    .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
    .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, mask, 0, 0), \
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2130
2131
  static int snd_cmipci_info_native_mixer(struct snd_kcontrol *kcontrol,
  					struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2132
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2133
  	struct cmipci_sb_reg reg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2134
2135
2136
2137
2138
2139
2140
2141
2142
  
  	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  	uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = reg.stereo + 1;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = reg.mask;
  	return 0;
  
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2143
2144
  static int snd_cmipci_get_native_mixer(struct snd_kcontrol *kcontrol,
  				       struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2145
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2146
2147
  	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  	struct cmipci_sb_reg reg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
  	unsigned char oreg, val;
  
  	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  	spin_lock_irq(&cm->reg_lock);
  	oreg = inb(cm->iobase + reg.left_reg);
  	val = (oreg >> reg.left_shift) & reg.mask;
  	if (reg.invert)
  		val = reg.mask - val;
  	ucontrol->value.integer.value[0] = val;
  	if (reg.stereo) {
  		val = (oreg >> reg.right_shift) & reg.mask;
  		if (reg.invert)
  			val = reg.mask - val;
  		ucontrol->value.integer.value[1] = val;
  	}
  	spin_unlock_irq(&cm->reg_lock);
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2166
2167
  static int snd_cmipci_put_native_mixer(struct snd_kcontrol *kcontrol,
  				       struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2168
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2169
2170
  	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  	struct cmipci_sb_reg reg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
  	unsigned char oreg, nreg, val;
  
  	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  	spin_lock_irq(&cm->reg_lock);
  	oreg = inb(cm->iobase + reg.left_reg);
  	val = ucontrol->value.integer.value[0] & reg.mask;
  	if (reg.invert)
  		val = reg.mask - val;
  	nreg = oreg & ~(reg.mask << reg.left_shift);
  	nreg |= (val << reg.left_shift);
  	if (reg.stereo) {
  		val = ucontrol->value.integer.value[1] & reg.mask;
  		if (reg.invert)
  			val = reg.mask - val;
  		nreg &= ~(reg.mask << reg.right_shift);
  		nreg |= (val << reg.right_shift);
  	}
  	outb(nreg, cm->iobase + reg.left_reg);
  	spin_unlock_irq(&cm->reg_lock);
  	return (nreg != oreg);
  }
  
  /*
   * special case - check mixer sensitivity
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2196
2197
  static int snd_cmipci_get_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
  						 struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2198
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2199
  	//struct cmipci *cm = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2200
2201
  	return snd_cmipci_get_native_mixer(kcontrol, ucontrol);
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2202
2203
  static int snd_cmipci_put_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
  						 struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2204
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2205
  	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2206
2207
2208
2209
2210
2211
  	if (cm->mixer_insensitive) {
  		/* ignored */
  		return 0;
  	}
  	return snd_cmipci_put_native_mixer(kcontrol, ucontrol);
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2212
  static struct snd_kcontrol_new snd_cmipci_mixers[] __devinitdata = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  	CMIPCI_SB_VOL_STEREO("Master Playback Volume", SB_DSP4_MASTER_DEV, 3, 31),
  	CMIPCI_MIXER_SW_MONO("3D Control - Switch", CM_REG_MIXER1, CM_X3DEN_SHIFT, 0),
  	CMIPCI_SB_VOL_STEREO("PCM Playback Volume", SB_DSP4_PCM_DEV, 3, 31),
  	//CMIPCI_MIXER_SW_MONO("PCM Playback Switch", CM_REG_MIXER1, CM_WSMUTE_SHIFT, 1),
  	{ /* switch with sensitivity */
  		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  		.name = "PCM Playback Switch",
  		.info = snd_cmipci_info_native_mixer,
  		.get = snd_cmipci_get_native_mixer_sensitive,
  		.put = snd_cmipci_put_native_mixer_sensitive,
  		.private_value = COMPOSE_SB_REG(CM_REG_MIXER1, CM_REG_MIXER1, CM_WSMUTE_SHIFT, CM_WSMUTE_SHIFT, 1, 1, 0),
  	},
  	CMIPCI_MIXER_SW_STEREO("PCM Capture Switch", CM_REG_MIXER1, CM_WAVEINL_SHIFT, CM_WAVEINR_SHIFT, 0),
  	CMIPCI_SB_VOL_STEREO("Synth Playback Volume", SB_DSP4_SYNTH_DEV, 3, 31),
  	CMIPCI_MIXER_SW_MONO("Synth Playback Switch", CM_REG_MIXER1, CM_FMMUTE_SHIFT, 1),
  	CMIPCI_SB_INPUT_SW("Synth Capture Route", 6, 5),
  	CMIPCI_SB_VOL_STEREO("CD Playback Volume", SB_DSP4_CD_DEV, 3, 31),
  	CMIPCI_SB_SW_STEREO("CD Playback Switch", 2, 1),
  	CMIPCI_SB_INPUT_SW("CD Capture Route", 2, 1),
  	CMIPCI_SB_VOL_STEREO("Line Playback Volume", SB_DSP4_LINE_DEV, 3, 31),
  	CMIPCI_SB_SW_STEREO("Line Playback Switch", 4, 3),
  	CMIPCI_SB_INPUT_SW("Line Capture Route", 4, 3),
  	CMIPCI_SB_VOL_MONO("Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
  	CMIPCI_SB_SW_MONO("Mic Playback Switch", 0),
  	CMIPCI_DOUBLE("Mic Capture Switch", SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, 0, 0, 1, 0, 0),
d355c82a0   Jaroslav Kysela   ALSA: rename "PC ...
2238
  	CMIPCI_SB_VOL_MONO("Beep Playback Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2239
2240
2241
  	CMIPCI_MIXER_VOL_STEREO("Aux Playback Volume", CM_REG_AUX_VOL, 4, 0, 15),
  	CMIPCI_MIXER_SW_STEREO("Aux Playback Switch", CM_REG_MIXER2, CM_VAUXLM_SHIFT, CM_VAUXRM_SHIFT, 0),
  	CMIPCI_MIXER_SW_STEREO("Aux Capture Switch", CM_REG_MIXER2, CM_RAUXLEN_SHIFT, CM_RAUXREN_SHIFT, 0),
2eff7ec81   Takashi Iwai   [ALSA] cmipci - A...
2242
  	CMIPCI_MIXER_SW_MONO("Mic Boost Playback Switch", CM_REG_MIXER2, CM_MICGAINZ_SHIFT, 1),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2243
  	CMIPCI_MIXER_VOL_MONO("Mic Capture Volume", CM_REG_MIXER2, CM_VADMIC_SHIFT, 7),
2eff7ec81   Takashi Iwai   [ALSA] cmipci - A...
2244
2245
  	CMIPCI_SB_VOL_MONO("Phone Playback Volume", CM_REG_EXTENT_IND, 5, 7),
  	CMIPCI_DOUBLE("Phone Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 4, 4, 1, 0, 0),
d355c82a0   Jaroslav Kysela   ALSA: rename "PC ...
2246
  	CMIPCI_DOUBLE("Beep Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 3, 3, 1, 0, 0),
2eff7ec81   Takashi Iwai   [ALSA] cmipci - A...
2247
  	CMIPCI_DOUBLE("Mic Boost Capture Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 0, 0, 1, 0, 0),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2248
2249
2250
2251
2252
  };
  
  /*
   * other switches
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2253
  struct cmipci_switch_args {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2254
2255
2256
2257
  	int reg;		/* register index */
  	unsigned int mask;	/* mask bits */
  	unsigned int mask_on;	/* mask bits to turn on */
  	unsigned int is_byte: 1;		/* byte access? */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2258
2259
2260
2261
  	unsigned int ac3_sensitive: 1;	/* access forbidden during
  					 * non-audio operation?
  					 */
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2262

a5ce88909   Takashi Iwai   [ALSA] Clean up w...
2263
  #define snd_cmipci_uswitch_info		snd_ctl_boolean_mono_info
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2264

2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2265
2266
2267
  static int _snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
  				   struct snd_ctl_elem_value *ucontrol,
  				   struct cmipci_switch_args *args)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2268
2269
  {
  	unsigned int val;
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2270
  	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
  
  	spin_lock_irq(&cm->reg_lock);
  	if (args->ac3_sensitive && cm->mixer_insensitive) {
  		ucontrol->value.integer.value[0] = 0;
  		spin_unlock_irq(&cm->reg_lock);
  		return 0;
  	}
  	if (args->is_byte)
  		val = inb(cm->iobase + args->reg);
  	else
  		val = snd_cmipci_read(cm, args->reg);
  	ucontrol->value.integer.value[0] = ((val & args->mask) == args->mask_on) ? 1 : 0;
  	spin_unlock_irq(&cm->reg_lock);
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2286
2287
  static int snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2288
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2289
2290
  	struct cmipci_switch_args *args;
  	args = (struct cmipci_switch_args *)kcontrol->private_value;
da3cec35d   Takashi Iwai   ALSA: Kill snd_as...
2291
2292
  	if (snd_BUG_ON(!args))
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2293
2294
  	return _snd_cmipci_uswitch_get(kcontrol, ucontrol, args);
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2295
2296
2297
  static int _snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
  				   struct snd_ctl_elem_value *ucontrol,
  				   struct cmipci_switch_args *args)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2298
2299
2300
  {
  	unsigned int val;
  	int change;
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2301
  	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
  
  	spin_lock_irq(&cm->reg_lock);
  	if (args->ac3_sensitive && cm->mixer_insensitive) {
  		/* ignored */
  		spin_unlock_irq(&cm->reg_lock);
  		return 0;
  	}
  	if (args->is_byte)
  		val = inb(cm->iobase + args->reg);
  	else
  		val = snd_cmipci_read(cm, args->reg);
8c6707142   Timofei V. Bondarenko   [ALSA] _snd_cmipc...
2313
2314
  	change = (val & args->mask) != (ucontrol->value.integer.value[0] ? 
  			args->mask_on : (args->mask & ~args->mask_on));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
  	if (change) {
  		val &= ~args->mask;
  		if (ucontrol->value.integer.value[0])
  			val |= args->mask_on;
  		else
  			val |= (args->mask & ~args->mask_on);
  		if (args->is_byte)
  			outb((unsigned char)val, cm->iobase + args->reg);
  		else
  			snd_cmipci_write(cm, args->reg, val);
  	}
  	spin_unlock_irq(&cm->reg_lock);
  	return change;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2329
2330
  static int snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2331
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2332
2333
  	struct cmipci_switch_args *args;
  	args = (struct cmipci_switch_args *)kcontrol->private_value;
da3cec35d   Takashi Iwai   ALSA: Kill snd_as...
2334
2335
  	if (snd_BUG_ON(!args))
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2336
2337
2338
2339
  	return _snd_cmipci_uswitch_put(kcontrol, ucontrol, args);
  }
  
  #define DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask_on, xis_byte, xac3) \
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2340
  static struct cmipci_switch_args cmipci_switch_arg_##sname = { \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
    .reg = xreg, \
    .mask = xmask, \
    .mask_on = xmask_on, \
    .is_byte = xis_byte, \
    .ac3_sensitive = xac3, \
  }
  	
  #define DEFINE_BIT_SWITCH_ARG(sname, xreg, xmask, xis_byte, xac3) \
  	DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask, xis_byte, xac3)
  
  #if 0 /* these will be controlled in pcm device */
  DEFINE_BIT_SWITCH_ARG(spdif_in, CM_REG_FUNCTRL1, CM_SPDF_1, 0, 0);
  DEFINE_BIT_SWITCH_ARG(spdif_out, CM_REG_FUNCTRL1, CM_SPDF_0, 0, 0);
  #endif
  DEFINE_BIT_SWITCH_ARG(spdif_in_sel1, CM_REG_CHFORMAT, CM_SPDIF_SELECT1, 0, 0);
  DEFINE_BIT_SWITCH_ARG(spdif_in_sel2, CM_REG_MISC_CTRL, CM_SPDIF_SELECT2, 0, 0);
  DEFINE_BIT_SWITCH_ARG(spdif_enable, CM_REG_LEGACY_CTRL, CM_ENSPDOUT, 0, 0);
  DEFINE_BIT_SWITCH_ARG(spdo2dac, CM_REG_FUNCTRL1, CM_SPDO2DAC, 0, 1);
  DEFINE_BIT_SWITCH_ARG(spdi_valid, CM_REG_MISC, CM_SPDVALID, 1, 0);
  DEFINE_BIT_SWITCH_ARG(spdif_copyright, CM_REG_LEGACY_CTRL, CM_SPDCOPYRHT, 0, 0);
  DEFINE_BIT_SWITCH_ARG(spdif_dac_out, CM_REG_LEGACY_CTRL, CM_DAC2SPDO, 0, 1);
  DEFINE_SWITCH_ARG(spdo_5v, CM_REG_MISC_CTRL, CM_SPDO5V, 0, 0, 0); /* inverse: 0 = 5V */
  // DEFINE_BIT_SWITCH_ARG(spdo_48k, CM_REG_MISC_CTRL, CM_SPDF_AC97|CM_SPDIF48K, 0, 1);
  DEFINE_BIT_SWITCH_ARG(spdif_loop, CM_REG_FUNCTRL1, CM_SPDFLOOP, 0, 1);
  DEFINE_BIT_SWITCH_ARG(spdi_monitor, CM_REG_MIXER1, CM_CDPLAY, 1, 0);
  /* DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_CHFORMAT, CM_SPDIF_INVERSE, 0, 0); */
  DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_MISC, CM_SPDIF_INVERSE, 1, 0);
  DEFINE_BIT_SWITCH_ARG(spdi_phase2, CM_REG_CHFORMAT, CM_SPDIF_INVERSE2, 0, 0);
  #if CM_CH_PLAY == 1
  DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, 0, 0, 0); /* reversed */
  #else
  DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, CM_XCHGDAC, 0, 0);
  #endif
  DEFINE_BIT_SWITCH_ARG(fourch, CM_REG_MISC_CTRL, CM_N4SPK3D, 0, 0);
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
2375
2376
  // DEFINE_BIT_SWITCH_ARG(line_rear, CM_REG_MIXER1, CM_REAR2LIN, 1, 0);
  // DEFINE_BIT_SWITCH_ARG(line_bass, CM_REG_LEGACY_CTRL, CM_CENTR2LIN|CM_BASE2LIN, 0, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
  // DEFINE_BIT_SWITCH_ARG(joystick, CM_REG_FUNCTRL1, CM_JYSTK_EN, 0, 0); /* now module option */
  DEFINE_SWITCH_ARG(modem, CM_REG_MISC_CTRL, CM_FLINKON|CM_FLINKOFF, CM_FLINKON, 0, 0);
  
  #define DEFINE_SWITCH(sname, stype, sarg) \
  { .name = sname, \
    .iface = stype, \
    .info = snd_cmipci_uswitch_info, \
    .get = snd_cmipci_uswitch_get, \
    .put = snd_cmipci_uswitch_put, \
    .private_value = (unsigned long)&cmipci_switch_arg_##sarg,\
  }
  
  #define DEFINE_CARD_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_CARD, sarg)
  #define DEFINE_MIXER_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_MIXER, sarg)
  
  
  /*
   * callbacks for spdif output switch
   * needs toggle two registers..
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2397
2398
  static int snd_cmipci_spdout_enable_get(struct snd_kcontrol *kcontrol,
  					struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2399
2400
2401
2402
2403
2404
  {
  	int changed;
  	changed = _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
  	changed |= _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
  	return changed;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2405
2406
  static int snd_cmipci_spdout_enable_put(struct snd_kcontrol *kcontrol,
  					struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2407
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2408
  	struct cmipci *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
  	int changed;
  	changed = _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
  	changed |= _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
  	if (changed) {
  		if (ucontrol->value.integer.value[0]) {
  			if (chip->spdif_playback_avail)
  				snd_cmipci_set_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  		} else {
  			if (chip->spdif_playback_avail)
  				snd_cmipci_clear_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  		}
  	}
  	chip->spdif_playback_enabled = ucontrol->value.integer.value[0];
  	return changed;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2424
2425
  static int snd_cmipci_line_in_mode_info(struct snd_kcontrol *kcontrol,
  					struct snd_ctl_elem_info *uinfo)
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2426
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2427
  	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
60c4ce4a0   Clemens Ladisch   ALSA: cmipci: use...
2428
2429
2430
2431
2432
2433
  	static const char *const texts[3] = {
  		"Line-In", "Rear Output", "Bass Output"
  	};
  
  	return snd_ctl_enum_info(uinfo, 1,
  				 cm->chip_version >= 39 ? 3 : 2, texts);
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2434
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2435
  static inline unsigned int get_line_in_mode(struct cmipci *cm)
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2436
2437
2438
2439
  {
  	unsigned int val;
  	if (cm->chip_version >= 39) {
  		val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL);
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
2440
  		if (val & (CM_CENTR2LIN | CM_BASE2LIN))
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2441
2442
2443
  			return 2;
  	}
  	val = snd_cmipci_read_b(cm, CM_REG_MIXER1);
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
2444
  	if (val & CM_REAR2LIN)
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2445
2446
2447
  		return 1;
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2448
2449
  static int snd_cmipci_line_in_mode_get(struct snd_kcontrol *kcontrol,
  				       struct snd_ctl_elem_value *ucontrol)
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2450
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2451
  	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2452
2453
2454
2455
2456
2457
  
  	spin_lock_irq(&cm->reg_lock);
  	ucontrol->value.enumerated.item[0] = get_line_in_mode(cm);
  	spin_unlock_irq(&cm->reg_lock);
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2458
2459
  static int snd_cmipci_line_in_mode_put(struct snd_kcontrol *kcontrol,
  				       struct snd_ctl_elem_value *ucontrol)
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2460
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2461
  	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2462
2463
2464
2465
  	int change;
  
  	spin_lock_irq(&cm->reg_lock);
  	if (ucontrol->value.enumerated.item[0] == 2)
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
2466
  		change = snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2467
  	else
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
2468
  		change = snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2469
  	if (ucontrol->value.enumerated.item[0] == 1)
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
2470
  		change |= snd_cmipci_set_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2471
  	else
a839a33d8   Clemens Ladisch   [ALSA] cmipci: up...
2472
  		change |= snd_cmipci_clear_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2473
2474
2475
  	spin_unlock_irq(&cm->reg_lock);
  	return change;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2476
2477
  static int snd_cmipci_mic_in_mode_info(struct snd_kcontrol *kcontrol,
  				       struct snd_ctl_elem_info *uinfo)
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2478
  {
60c4ce4a0   Clemens Ladisch   ALSA: cmipci: use...
2479
2480
2481
  	static const char *const texts[2] = { "Mic-In", "Center/LFE Output" };
  
  	return snd_ctl_enum_info(uinfo, 1, 2, texts);
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2482
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2483
2484
  static int snd_cmipci_mic_in_mode_get(struct snd_kcontrol *kcontrol,
  				      struct snd_ctl_elem_value *ucontrol)
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2485
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2486
  	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2487
2488
2489
2490
2491
2492
2493
  	/* same bit as spdi_phase */
  	spin_lock_irq(&cm->reg_lock);
  	ucontrol->value.enumerated.item[0] = 
  		(snd_cmipci_read_b(cm, CM_REG_MISC) & CM_SPDIF_INVERSE) ? 1 : 0;
  	spin_unlock_irq(&cm->reg_lock);
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2494
2495
  static int snd_cmipci_mic_in_mode_put(struct snd_kcontrol *kcontrol,
  				      struct snd_ctl_elem_value *ucontrol)
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2496
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2497
  	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
  	int change;
  
  	spin_lock_irq(&cm->reg_lock);
  	if (ucontrol->value.enumerated.item[0])
  		change = snd_cmipci_set_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
  	else
  		change = snd_cmipci_clear_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
  	spin_unlock_irq(&cm->reg_lock);
  	return change;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2508
  /* both for CM8338/8738 */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2509
  static struct snd_kcontrol_new snd_cmipci_mixer_switches[] __devinitdata = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2510
  	DEFINE_MIXER_SWITCH("Four Channel Mode", fourch),
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2511
2512
2513
2514
2515
2516
2517
  	{
  		.name = "Line-In Mode",
  		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  		.info = snd_cmipci_line_in_mode_info,
  		.get = snd_cmipci_line_in_mode_get,
  		.put = snd_cmipci_line_in_mode_put,
  	},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2518
2519
2520
  };
  
  /* for non-multichannel chips */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2521
  static struct snd_kcontrol_new snd_cmipci_nomulti_switch __devinitdata =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2522
2523
2524
  DEFINE_MIXER_SWITCH("Exchange DAC", exchange_dac);
  
  /* only for CM8738 */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2525
  static struct snd_kcontrol_new snd_cmipci_8738_mixer_switches[] __devinitdata = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
  #if 0 /* controlled in pcm device */
  	DEFINE_MIXER_SWITCH("IEC958 In Record", spdif_in),
  	DEFINE_MIXER_SWITCH("IEC958 Out", spdif_out),
  	DEFINE_MIXER_SWITCH("IEC958 Out To DAC", spdo2dac),
  #endif
  	// DEFINE_MIXER_SWITCH("IEC958 Output Switch", spdif_enable),
  	{ .name = "IEC958 Output Switch",
  	  .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	  .info = snd_cmipci_uswitch_info,
  	  .get = snd_cmipci_spdout_enable_get,
  	  .put = snd_cmipci_spdout_enable_put,
  	},
  	DEFINE_MIXER_SWITCH("IEC958 In Valid", spdi_valid),
  	DEFINE_MIXER_SWITCH("IEC958 Copyright", spdif_copyright),
  	DEFINE_MIXER_SWITCH("IEC958 5V", spdo_5v),
  //	DEFINE_MIXER_SWITCH("IEC958 In/Out 48KHz", spdo_48k),
  	DEFINE_MIXER_SWITCH("IEC958 Loop", spdif_loop),
  	DEFINE_MIXER_SWITCH("IEC958 In Monitor", spdi_monitor),
  };
  
  /* only for model 033/037 */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2547
  static struct snd_kcontrol_new snd_cmipci_old_mixer_switches[] __devinitdata = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2548
2549
2550
2551
2552
2553
  	DEFINE_MIXER_SWITCH("IEC958 Mix Analog", spdif_dac_out),
  	DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase),
  	DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel1),
  };
  
  /* only for model 039 or later */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2554
  static struct snd_kcontrol_new snd_cmipci_extra_mixer_switches[] __devinitdata = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2555
2556
  	DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel2),
  	DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase2),
01d25d460   Takashi Iwai   [ALSA] Replace pc...
2557
2558
2559
2560
2561
2562
2563
  	{
  		.name = "Mic-In Mode",
  		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  		.info = snd_cmipci_mic_in_mode_info,
  		.get = snd_cmipci_mic_in_mode_get,
  		.put = snd_cmipci_mic_in_mode_put,
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2564
2565
2566
  };
  
  /* card control switches */
69a07304d   Clemens Ladisch   [ALSA] cmipci: di...
2567
2568
  static struct snd_kcontrol_new snd_cmipci_modem_switch __devinitdata =
  DEFINE_CARD_SWITCH("Modem", modem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2569

2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2570
  static int __devinit snd_cmipci_mixer_new(struct cmipci *cm, int pcm_spdif_device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2571
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2572
2573
2574
  	struct snd_card *card;
  	struct snd_kcontrol_new *sw;
  	struct snd_kcontrol *kctl;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2575
2576
  	unsigned int idx;
  	int err;
da3cec35d   Takashi Iwai   ALSA: Kill snd_as...
2577
2578
  	if (snd_BUG_ON(!cm || !cm->card))
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
  
  	card = cm->card;
  
  	strcpy(card->mixername, "CMedia PCI");
  
  	spin_lock_irq(&cm->reg_lock);
  	snd_cmipci_mixer_write(cm, 0x00, 0x00);		/* mixer reset */
  	spin_unlock_irq(&cm->reg_lock);
  
  	for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixers); idx++) {
  		if (cm->chip_version == 68) {	// 8768 has no PCM volume
  			if (!strcmp(snd_cmipci_mixers[idx].name,
  				"PCM Playback Volume"))
  				continue;
  		}
  		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cmipci_mixers[idx], cm))) < 0)
  			return err;
  	}
  
  	/* mixer switches */
  	sw = snd_cmipci_mixer_switches;
  	for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixer_switches); idx++, sw++) {
  		err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  		if (err < 0)
  			return err;
  	}
  	if (! cm->can_multi_ch) {
  		err = snd_ctl_add(cm->card, snd_ctl_new1(&snd_cmipci_nomulti_switch, cm));
  		if (err < 0)
  			return err;
  	}
  	if (cm->device == PCI_DEVICE_ID_CMEDIA_CM8738 ||
  	    cm->device == PCI_DEVICE_ID_CMEDIA_CM8738B) {
  		sw = snd_cmipci_8738_mixer_switches;
  		for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_8738_mixer_switches); idx++, sw++) {
  			err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  			if (err < 0)
  				return err;
  		}
  		if (cm->can_ac3_hw) {
  			if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_default, cm))) < 0)
  				return err;
  			kctl->id.device = pcm_spdif_device;
  			if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_mask, cm))) < 0)
  				return err;
  			kctl->id.device = pcm_spdif_device;
  			if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_stream, cm))) < 0)
  				return err;
  			kctl->id.device = pcm_spdif_device;
  		}
  		if (cm->chip_version <= 37) {
  			sw = snd_cmipci_old_mixer_switches;
  			for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_old_mixer_switches); idx++, sw++) {
  				err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  				if (err < 0)
  					return err;
  			}
  		}
  	}
  	if (cm->chip_version >= 39) {
  		sw = snd_cmipci_extra_mixer_switches;
  		for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_extra_mixer_switches); idx++, sw++) {
  			err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  			if (err < 0)
  				return err;
  		}
  	}
  
  	/* card switches */
25543fa78   Clemens Ladisch   [ALSA] cmipci: do...
2648
2649
2650
2651
  	/*
  	 * newer chips don't have the register bits to force modem link
  	 * detection; the bit that was FLINKON now mutes CH1
  	 */
69a07304d   Clemens Ladisch   [ALSA] cmipci: di...
2652
2653
2654
  	if (cm->chip_version < 39) {
  		err = snd_ctl_add(cm->card,
  				  snd_ctl_new1(&snd_cmipci_modem_switch, cm));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2655
2656
2657
2658
2659
  		if (err < 0)
  			return err;
  	}
  
  	for (idx = 0; idx < CM_SAVED_MIXERS; idx++) {
7dfa31ed5   Harvey Harrison   [ALSA] sound: cmi...
2660
  		struct snd_ctl_elem_id elem_id;
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2661
  		struct snd_kcontrol *ctl;
7dfa31ed5   Harvey Harrison   [ALSA] sound: cmi...
2662
2663
2664
2665
2666
  		memset(&elem_id, 0, sizeof(elem_id));
  		elem_id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  		strcpy(elem_id.name, cm_saved_mixer[idx].name);
  		ctl = snd_ctl_find_id(cm->card, &elem_id);
  		if (ctl)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
  			cm->mixer_res_ctl[idx] = ctl;
  	}
  
  	return 0;
  }
  
  
  /*
   * proc interface
   */
  
  #ifdef CONFIG_PROC_FS
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2679
2680
  static void snd_cmipci_proc_read(struct snd_info_entry *entry, 
  				 struct snd_info_buffer *buffer)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2681
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2682
  	struct cmipci *cm = entry->private_data;
54d030cc4   Clemens Ladisch   [ALSA] cmipci: sh...
2683
  	int i, v;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2684
  	
54d030cc4   Clemens Ladisch   [ALSA] cmipci: sh...
2685
2686
2687
2688
2689
2690
  	snd_iprintf(buffer, "%s
  ", cm->card->longname);
  	for (i = 0; i < 0x94; i++) {
  		if (i == 0x28)
  			i = 0x90;
  		v = inb(cm->iobase + i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2691
  		if (i % 4 == 0)
54d030cc4   Clemens Ladisch   [ALSA] cmipci: sh...
2692
2693
2694
  			snd_iprintf(buffer, "
  %02x:", i);
  		snd_iprintf(buffer, " %02x", v);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2695
  	}
54d030cc4   Clemens Ladisch   [ALSA] cmipci: sh...
2696
2697
  	snd_iprintf(buffer, "
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2698
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2699
  static void __devinit snd_cmipci_proc_init(struct cmipci *cm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2700
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2701
  	struct snd_info_entry *entry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2702
2703
  
  	if (! snd_card_proc_new(cm->card, "cmipci", &entry))
bf850204a   Takashi Iwai   [ALSA] Remove unn...
2704
  		snd_info_set_text_ops(entry, cm, snd_cmipci_proc_read);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2705
2706
  }
  #else /* !CONFIG_PROC_FS */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2707
  static inline void snd_cmipci_proc_init(struct cmipci *cm) {}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2708
  #endif
cebe41d4b   Alexey Dobriyan   sound: use DEFINE...
2709
  static DEFINE_PCI_DEVICE_TABLE(snd_cmipci_ids) = {
28d27aae9   Joe Perches   sound: Use PCI_VD...
2710
2711
2712
2713
2714
  	{PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338A), 0},
  	{PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338B), 0},
  	{PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738), 0},
  	{PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738B), 0},
  	{PCI_VDEVICE(AL, PCI_DEVICE_ID_CMEDIA_CM8738), 0},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2715
2716
2717
2718
2719
2720
2721
2722
  	{0,},
  };
  
  
  /*
   * check chip version and capabilities
   * driver name is modified according to the chip model
   */
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2723
  static void __devinit query_chip(struct cmipci *cm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2724
2725
2726
2727
2728
2729
2730
2731
  {
  	unsigned int detect;
  
  	/* check reg 0Ch, bit 24-31 */
  	detect = snd_cmipci_read(cm, CM_REG_INT_HLDCLR) & CM_CHIP_MASK2;
  	if (! detect) {
  		/* check reg 08h, bit 24-28 */
  		detect = snd_cmipci_read(cm, CM_REG_CHFORMAT) & CM_CHIP_MASK1;
133271fef   Clemens Ladisch   [ALSA] cmipci: re...
2732
2733
  		switch (detect) {
  		case 0:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2734
  			cm->chip_version = 33;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2735
2736
2737
2738
  			if (cm->do_soft_ac3)
  				cm->can_ac3_sw = 1;
  			else
  				cm->can_ac3_hw = 1;
133271fef   Clemens Ladisch   [ALSA] cmipci: re...
2739
  			break;
6935e6887   Clemens Ladisch   [ALSA] cmipci: fi...
2740
  		case CM_CHIP_037:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2741
  			cm->chip_version = 37;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2742
  			cm->can_ac3_hw = 1;
133271fef   Clemens Ladisch   [ALSA] cmipci: re...
2743
2744
2745
2746
2747
  			break;
  		default:
  			cm->chip_version = 39;
  			cm->can_ac3_hw = 1;
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2748
  		}
133271fef   Clemens Ladisch   [ALSA] cmipci: re...
2749
  		cm->max_channels = 2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2750
  	} else {
133271fef   Clemens Ladisch   [ALSA] cmipci: re...
2751
  		if (detect & CM_CHIP_039) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2752
2753
2754
2755
2756
  			cm->chip_version = 39;
  			if (detect & CM_CHIP_039_6CH) /* 4 or 6 channels */
  				cm->max_channels = 6;
  			else
  				cm->max_channels = 4;
133271fef   Clemens Ladisch   [ALSA] cmipci: re...
2757
2758
2759
  		} else if (detect & CM_CHIP_8768) {
  			cm->chip_version = 68;
  			cm->max_channels = 8;
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
2760
  			cm->can_96k = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2761
  		} else {
133271fef   Clemens Ladisch   [ALSA] cmipci: re...
2762
2763
  			cm->chip_version = 55;
  			cm->max_channels = 6;
755c48abd   Timofei Bondarenko   [ALSA] cmipci at ...
2764
  			cm->can_96k = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2765
  		}
133271fef   Clemens Ladisch   [ALSA] cmipci: re...
2766
  		cm->can_ac3_hw = 1;
133271fef   Clemens Ladisch   [ALSA] cmipci: re...
2767
  		cm->can_multi_ch = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2768
2769
2770
2771
  	}
  }
  
  #ifdef SUPPORT_JOYSTICK
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2772
  static int __devinit snd_cmipci_create_gameport(struct cmipci *cm, int dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
  {
  	static int ports[] = { 0x201, 0x200, 0 }; /* FIXME: majority is 0x201? */
  	struct gameport *gp;
  	struct resource *r = NULL;
  	int i, io_port = 0;
  
  	if (joystick_port[dev] == 0)
  		return -ENODEV;
  
  	if (joystick_port[dev] == 1) { /* auto-detect */
  		for (i = 0; ports[i]; i++) {
  			io_port = ports[i];
  			r = request_region(io_port, 1, "CMIPCI gameport");
  			if (r)
  				break;
  		}
  	} else {
  		io_port = joystick_port[dev];
  		r = request_region(io_port, 1, "CMIPCI gameport");
  	}
  
  	if (!r) {
  		printk(KERN_WARNING "cmipci: cannot reserve joystick ports
  ");
  		return -EBUSY;
  	}
  
  	cm->gameport = gp = gameport_allocate_port();
  	if (!gp) {
  		printk(KERN_ERR "cmipci: cannot allocate memory for gameport
  ");
b1d5776d8   Takashi Iwai   [ALSA] Remove vma...
2804
  		release_and_free_resource(r);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
  		return -ENOMEM;
  	}
  	gameport_set_name(gp, "C-Media Gameport");
  	gameport_set_phys(gp, "pci%s/gameport0", pci_name(cm->pci));
  	gameport_set_dev_parent(gp, &cm->pci->dev);
  	gp->io = io_port;
  	gameport_set_port_data(gp, r);
  
  	snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
  
  	gameport_register_port(cm->gameport);
  
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2819
  static void snd_cmipci_free_gameport(struct cmipci *cm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2820
2821
2822
2823
2824
2825
2826
2827
  {
  	if (cm->gameport) {
  		struct resource *r = gameport_get_port_data(cm->gameport);
  
  		gameport_unregister_port(cm->gameport);
  		cm->gameport = NULL;
  
  		snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
b1d5776d8   Takashi Iwai   [ALSA] Remove vma...
2828
  		release_and_free_resource(r);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2829
2830
2831
  	}
  }
  #else
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2832
2833
  static inline int snd_cmipci_create_gameport(struct cmipci *cm, int dev) { return -ENOSYS; }
  static inline void snd_cmipci_free_gameport(struct cmipci *cm) { }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2834
  #endif
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2835
  static int snd_cmipci_free(struct cmipci *cm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
  {
  	if (cm->irq >= 0) {
  		snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  		snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT);
  		snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);  /* disable ints */
  		snd_cmipci_ch_reset(cm, CM_CH_PLAY);
  		snd_cmipci_ch_reset(cm, CM_CH_CAPT);
  		snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
  		snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
  
  		/* reset mixer */
  		snd_cmipci_mixer_write(cm, 0, 0);
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2848
  		free_irq(cm->irq, cm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2849
2850
2851
2852
2853
2854
2855
2856
  	}
  
  	snd_cmipci_free_gameport(cm);
  	pci_release_regions(cm->pci);
  	pci_disable_device(cm->pci);
  	kfree(cm);
  	return 0;
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2857
  static int snd_cmipci_dev_free(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2858
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2859
  	struct cmipci *cm = device->device_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2860
2861
  	return snd_cmipci_free(cm);
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2862
  static int __devinit snd_cmipci_create_fm(struct cmipci *cm, long fm_port)
5747e5404   Clemens Ladisch   [ALSA] cmipci: us...
2863
2864
2865
  {
  	long iosynth;
  	unsigned int val;
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2866
  	struct snd_opl3 *opl3;
5747e5404   Clemens Ladisch   [ALSA] cmipci: us...
2867
  	int err;
2f24d159d   Takashi Iwai   [ALSA] cmipci - A...
2868
2869
  	if (!fm_port)
  		goto disable_fm;
c78c950d2   Clemens Ladisch   [ALSA] cmipci: do...
2870
  	if (cm->chip_version >= 39) {
45c41b486   Clemens Ladisch   [ALSA] cmipci: fi...
2871
2872
2873
2874
2875
2876
2877
  		/* first try FM regs in PCI port range */
  		iosynth = cm->iobase + CM_REG_FM_PCI;
  		err = snd_opl3_create(cm->card, iosynth, iosynth + 2,
  				      OPL3_HW_OPL3, 1, &opl3);
  	} else {
  		err = -EIO;
  	}
5747e5404   Clemens Ladisch   [ALSA] cmipci: us...
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
  	if (err < 0) {
  		/* then try legacy ports */
  		val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL) & ~CM_FMSEL_MASK;
  		iosynth = fm_port;
  		switch (iosynth) {
  		case 0x3E8: val |= CM_FMSEL_3E8; break;
  		case 0x3E0: val |= CM_FMSEL_3E0; break;
  		case 0x3C8: val |= CM_FMSEL_3C8; break;
  		case 0x388: val |= CM_FMSEL_388; break;
  		default:
2f24d159d   Takashi Iwai   [ALSA] cmipci - A...
2888
  			goto disable_fm;
5747e5404   Clemens Ladisch   [ALSA] cmipci: us...
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
  		}
  		snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
  		/* enable FM */
  		snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  
  		if (snd_opl3_create(cm->card, iosynth, iosynth + 2,
  				    OPL3_HW_OPL3, 0, &opl3) < 0) {
  			printk(KERN_ERR "cmipci: no OPL device at %#lx, "
  			       "skipping...
  ", iosynth);
2f24d159d   Takashi Iwai   [ALSA] cmipci - A...
2899
  			goto disable_fm;
5747e5404   Clemens Ladisch   [ALSA] cmipci: us...
2900
2901
2902
2903
2904
2905
2906
2907
  		}
  	}
  	if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  		printk(KERN_ERR "cmipci: cannot create OPL3 hwdep
  ");
  		return err;
  	}
  	return 0;
2f24d159d   Takashi Iwai   [ALSA] cmipci - A...
2908
2909
2910
2911
2912
  
   disable_fm:
  	snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_FMSEL_MASK);
  	snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  	return 0;
5747e5404   Clemens Ladisch   [ALSA] cmipci: us...
2913
  }
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2914
2915
  static int __devinit snd_cmipci_create(struct snd_card *card, struct pci_dev *pci,
  				       int dev, struct cmipci **rcmipci)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2916
  {
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2917
  	struct cmipci *cm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2918
  	int err;
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2919
  	static struct snd_device_ops ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2920
2921
  		.dev_free =	snd_cmipci_dev_free,
  	};
d6426257d   Clemens Ladisch   [ALSA] cmipci: sh...
2922
  	unsigned int val;
395a434e3   Subrata Modak   ALSA: Fix Trivial...
2923
  	long iomidi = 0;
c9116ae45   Clemens Ladisch   [ALSA] cmipci: ma...
2924
  	int integrated_midi = 0;
b7e054a76   Clemens Ladisch   [ALSA] cmipci: sh...
2925
  	char modelstr[16];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2926
  	int pcm_index, pcm_spdif_index;
cebe41d4b   Alexey Dobriyan   sound: use DEFINE...
2927
  	static DEFINE_PCI_DEVICE_TABLE(intel_82437vx) = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2928
2929
2930
2931
2932
2933
2934
2935
  		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437VX) },
  		{ },
  	};
  
  	*rcmipci = NULL;
  
  	if ((err = pci_enable_device(pci)) < 0)
  		return err;
e560d8d83   Takashi Iwai   [ALSA] Replace wi...
2936
  	cm = kzalloc(sizeof(*cm), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2937
2938
2939
2940
2941
2942
  	if (cm == NULL) {
  		pci_disable_device(pci);
  		return -ENOMEM;
  	}
  
  	spin_lock_init(&cm->reg_lock);
62932df8f   Ingo Molnar   [ALSA] semaphore ...
2943
  	mutex_init(&cm->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
  	cm->device = pci->device;
  	cm->card = card;
  	cm->pci = pci;
  	cm->irq = -1;
  	cm->channel[0].ch = 0;
  	cm->channel[1].ch = 1;
  	cm->channel[0].is_dac = cm->channel[1].is_dac = 1; /* dual DAC mode */
  
  	if ((err = pci_request_regions(pci, card->driver)) < 0) {
  		kfree(cm);
  		pci_disable_device(pci);
  		return err;
  	}
  	cm->iobase = pci_resource_start(pci, 0);
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
2958
  	if (request_irq(pci->irq, snd_cmipci_interrupt,
934c2b6d0   Takashi Iwai   ALSA: use KBUILD_...
2959
  			IRQF_SHARED, KBUILD_MODNAME, cm)) {
99b359ba1   Takashi Iwai   [ALSA] Add missin...
2960
2961
  		snd_printk(KERN_ERR "unable to grab IRQ %d
  ", pci->irq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2962
2963
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
  		snd_cmipci_free(cm);
  		return -EBUSY;
  	}
  	cm->irq = pci->irq;
  
  	pci_set_master(cm->pci);
  
  	/*
  	 * check chip version, max channels and capabilities
  	 */
  
  	cm->chip_version = 0;
  	cm->max_channels = 2;
  	cm->do_soft_ac3 = soft_ac3[dev];
  
  	if (pci->device != PCI_DEVICE_ID_CMEDIA_CM8338A &&
  	    pci->device != PCI_DEVICE_ID_CMEDIA_CM8338B)
  		query_chip(cm);
  	/* added -MCx suffix for chip supporting multi-channels */
  	if (cm->can_multi_ch)
  		sprintf(cm->card->driver + strlen(cm->card->driver),
  			"-MC%d", cm->max_channels);
  	else if (cm->can_ac3_sw)
  		strcpy(cm->card->driver + strlen(cm->card->driver), "-SWIEC");
  
  	cm->dig_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
  	cm->dig_pcm_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
  
  #if CM_CH_PLAY == 1
  	cm->ctrl = CM_CHADC0;	/* default FUNCNTRL0 */
  #else
  	cm->ctrl = CM_CHADC1;	/* default FUNCNTRL0 */
  #endif
  
  	/* initialize codec registers */
3042ef752   Clemens Ladisch   [ALSA] cmipci: re...
2997
2998
  	snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
  	snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
  	snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);	/* disable ints */
  	snd_cmipci_ch_reset(cm, CM_CH_PLAY);
  	snd_cmipci_ch_reset(cm, CM_CH_CAPT);
  	snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0);	/* disable channels */
  	snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
  
  	snd_cmipci_write(cm, CM_REG_CHFORMAT, 0);
  	snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC|CM_N4SPK3D);
  #if CM_CH_PLAY == 1
  	snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  #else
  	snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  #endif
4ee727171   Clemens Ladisch   [ALSA] cmipci: in...
3012
3013
3014
3015
  	if (cm->chip_version) {
  		snd_cmipci_write_b(cm, CM_REG_EXT_MISC, 0x20); /* magic */
  		snd_cmipci_write_b(cm, CM_REG_EXT_MISC + 1, 0x09); /* more magic */
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
  	/* Set Bus Master Request */
  	snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_BREQ);
  
  	/* Assume TX and compatible chip set (Autodetection required for VX chip sets) */
  	switch (pci->device) {
  	case PCI_DEVICE_ID_CMEDIA_CM8738:
  	case PCI_DEVICE_ID_CMEDIA_CM8738B:
  		if (!pci_dev_present(intel_82437vx)) 
  			snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_TXVX);
  		break;
  	default:
  		break;
  	}
d6426257d   Clemens Ladisch   [ALSA] cmipci: sh...
3029
3030
  	if (cm->chip_version < 68) {
  		val = pci->device < 0x110 ? 8338 : 8738;
d6426257d   Clemens Ladisch   [ALSA] cmipci: sh...
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
  	} else {
  		switch (snd_cmipci_read_b(cm, CM_REG_INT_HLDCLR + 3) & 0x03) {
  		case 0:
  			val = 8769;
  			break;
  		case 2:
  			val = 8762;
  			break;
  		default:
  			switch ((pci->subsystem_vendor << 16) |
  				pci->subsystem_device) {
  			case 0x13f69761:
  			case 0x584d3741:
  			case 0x584d3751:
  			case 0x584d3761:
  			case 0x584d3771:
  			case 0x72848384:
  				val = 8770;
  				break;
  			default:
  				val = 8768;
  				break;
  			}
  		}
d6426257d   Clemens Ladisch   [ALSA] cmipci: sh...
3055
  	}
b7e054a76   Clemens Ladisch   [ALSA] cmipci: sh...
3056
3057
3058
3059
3060
3061
3062
  	sprintf(card->shortname, "C-Media CMI%d", val);
  	if (cm->chip_version < 68)
  		sprintf(modelstr, " (model %d)", cm->chip_version);
  	else
  		modelstr[0] = '\0';
  	sprintf(card->longname, "%s%s at %#lx, irq %i",
  		card->shortname, modelstr, cm->iobase, cm->irq);
1e02d6ea8   Clemens Ladisch   [ALSA] cmipci: fi...
3063

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3064
3065
3066
3067
  	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, cm, &ops)) < 0) {
  		snd_cmipci_free(cm);
  		return err;
  	}
c78c950d2   Clemens Ladisch   [ALSA] cmipci: do...
3068
  	if (cm->chip_version >= 39) {
c9116ae45   Clemens Ladisch   [ALSA] cmipci: ma...
3069
3070
3071
3072
3073
3074
3075
  		val = snd_cmipci_read_b(cm, CM_REG_MPU_PCI + 1);
  		if (val != 0x00 && val != 0xff) {
  			iomidi = cm->iobase + CM_REG_MPU_PCI;
  			integrated_midi = 1;
  		}
  	}
  	if (!integrated_midi) {
c78c950d2   Clemens Ladisch   [ALSA] cmipci: do...
3076
  		val = 0;
5747e5404   Clemens Ladisch   [ALSA] cmipci: us...
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
  		iomidi = mpu_port[dev];
  		switch (iomidi) {
  		case 0x320: val = CM_VMPU_320; break;
  		case 0x310: val = CM_VMPU_310; break;
  		case 0x300: val = CM_VMPU_300; break;
  		case 0x330: val = CM_VMPU_330; break;
  		default:
  			    iomidi = 0; break;
  		}
  		if (iomidi > 0) {
  			snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
  			/* enable UART */
  			snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_UART_EN);
88039815d   Clemens Ladisch   [ALSA] cmipci: ch...
3090
3091
3092
3093
3094
3095
3096
3097
  			if (inb(iomidi + 1) == 0xff) {
  				snd_printk(KERN_ERR "cannot enable MPU-401 port"
  					   " at %#lx
  ", iomidi);
  				snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1,
  						     CM_UART_EN);
  				iomidi = 0;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3098
3099
  		}
  	}
5747e5404   Clemens Ladisch   [ALSA] cmipci: us...
3100

45c41b486   Clemens Ladisch   [ALSA] cmipci: fi...
3101
3102
3103
3104
3105
  	if (cm->chip_version < 68) {
  		err = snd_cmipci_create_fm(cm, fm_port[dev]);
  		if (err < 0)
  			return err;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
  
  	/* reset mixer */
  	snd_cmipci_mixer_write(cm, 0, 0);
  
  	snd_cmipci_proc_init(cm);
  
  	/* create pcm devices */
  	pcm_index = pcm_spdif_index = 0;
  	if ((err = snd_cmipci_pcm_new(cm, pcm_index)) < 0)
  		return err;
  	pcm_index++;
b080ebbf9   Clemens Ladisch   [ALSA] cmipci: re...
3117
3118
3119
  	if ((err = snd_cmipci_pcm2_new(cm, pcm_index)) < 0)
  		return err;
  	pcm_index++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
  	if (cm->can_ac3_hw || cm->can_ac3_sw) {
  		pcm_spdif_index = pcm_index;
  		if ((err = snd_cmipci_pcm_spdif_new(cm, pcm_index)) < 0)
  			return err;
  	}
  
  	/* create mixer interface & switches */
  	if ((err = snd_cmipci_mixer_new(cm, pcm_spdif_index)) < 0)
  		return err;
  
  	if (iomidi > 0) {
  		if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
302e4c2f9   Takashi Iwai   [ALSA] Change an ...
3132
3133
3134
  					       iomidi,
  					       (integrated_midi ?
  						MPU401_INFO_INTEGRATED : 0),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
  					       cm->irq, 0, &cm->rmidi)) < 0) {
  			printk(KERN_ERR "cmipci: no UART401 device at 0x%lx
  ", iomidi);
  		}
  	}
  
  #ifdef USE_VAR48KRATE
  	for (val = 0; val < ARRAY_SIZE(rates); val++)
  		snd_cmipci_set_pll(cm, rates[val], val);
  
  	/*
  	 * (Re-)Enable external switch spdo_48k
  	 */
  	snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K|CM_SPDF_AC97);
  #endif /* USE_VAR48KRATE */
  
  	if (snd_cmipci_create_gameport(cm, dev) < 0)
  		snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
  
  	snd_card_set_dev(card, &pci->dev);
  
  	*rcmipci = cm;
  	return 0;
  }
  
  /*
   */
  
  MODULE_DEVICE_TABLE(pci, snd_cmipci_ids);
  
  static int __devinit snd_cmipci_probe(struct pci_dev *pci,
  				      const struct pci_device_id *pci_id)
  {
  	static int dev;
2cbdb686d   Takashi Iwai   [ALSA] Remove xxx...
3169
3170
  	struct snd_card *card;
  	struct cmipci *cm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3171
3172
3173
3174
3175
3176
3177
3178
  	int err;
  
  	if (dev >= SNDRV_CARDS)
  		return -ENODEV;
  	if (! enable[dev]) {
  		dev++;
  		return -ENOENT;
  	}
e58de7baf   Takashi Iwai   ALSA: Convert to ...
3179
3180
3181
  	err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  	if (err < 0)
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
  	
  	switch (pci->device) {
  	case PCI_DEVICE_ID_CMEDIA_CM8738:
  	case PCI_DEVICE_ID_CMEDIA_CM8738B:
  		strcpy(card->driver, "CMI8738");
  		break;
  	case PCI_DEVICE_ID_CMEDIA_CM8338A:
  	case PCI_DEVICE_ID_CMEDIA_CM8338B:
  		strcpy(card->driver, "CMI8338");
  		break;
  	default:
  		strcpy(card->driver, "CMIPCI");
  		break;
  	}
  
  	if ((err = snd_cmipci_create(card, pci, dev, &cm)) < 0) {
  		snd_card_free(card);
  		return err;
  	}
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
3201
  	card->private_data = cm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3202

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
  	if ((err = snd_card_register(card)) < 0) {
  		snd_card_free(card);
  		return err;
  	}
  	pci_set_drvdata(pci, card);
  	dev++;
  	return 0;
  
  }
  
  static void __devexit snd_cmipci_remove(struct pci_dev *pci)
  {
  	snd_card_free(pci_get_drvdata(pci));
  	pci_set_drvdata(pci, NULL);
  }
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
3218
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
3259
3260
  #ifdef CONFIG_PM
  /*
   * power management
   */
  static unsigned char saved_regs[] = {
  	CM_REG_FUNCTRL1, CM_REG_CHFORMAT, CM_REG_LEGACY_CTRL, CM_REG_MISC_CTRL,
  	CM_REG_MIXER0, CM_REG_MIXER1, CM_REG_MIXER2, CM_REG_MIXER3, CM_REG_PLL,
  	CM_REG_CH0_FRAME1, CM_REG_CH0_FRAME2,
  	CM_REG_CH1_FRAME1, CM_REG_CH1_FRAME2, CM_REG_EXT_MISC,
  	CM_REG_INT_STATUS, CM_REG_INT_HLDCLR, CM_REG_FUNCTRL0,
  };
  
  static unsigned char saved_mixers[] = {
  	SB_DSP4_MASTER_DEV, SB_DSP4_MASTER_DEV + 1,
  	SB_DSP4_PCM_DEV, SB_DSP4_PCM_DEV + 1,
  	SB_DSP4_SYNTH_DEV, SB_DSP4_SYNTH_DEV + 1,
  	SB_DSP4_CD_DEV, SB_DSP4_CD_DEV + 1,
  	SB_DSP4_LINE_DEV, SB_DSP4_LINE_DEV + 1,
  	SB_DSP4_MIC_DEV, SB_DSP4_SPEAKER_DEV,
  	CM_REG_EXTENT_IND, SB_DSP4_OUTPUT_SW,
  	SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT,
  };
  
  static int snd_cmipci_suspend(struct pci_dev *pci, pm_message_t state)
  {
  	struct snd_card *card = pci_get_drvdata(pci);
  	struct cmipci *cm = card->private_data;
  	int i;
  
  	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  	
  	snd_pcm_suspend_all(cm->pcm);
  	snd_pcm_suspend_all(cm->pcm2);
  	snd_pcm_suspend_all(cm->pcm_spdif);
  
  	/* save registers */
  	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  		cm->saved_regs[i] = snd_cmipci_read(cm, saved_regs[i]);
  	for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
  		cm->saved_mixers[i] = snd_cmipci_mixer_read(cm, saved_mixers[i]);
  
  	/* disable ints */
  	snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
3261
3262
  	pci_disable_device(pci);
  	pci_save_state(pci);
30b35399c   Takashi Iwai   [ALSA] Various fi...
3263
  	pci_set_power_state(pci, pci_choose_state(pci, state));
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
3264
3265
3266
3267
3268
3269
3270
3271
  	return 0;
  }
  
  static int snd_cmipci_resume(struct pci_dev *pci)
  {
  	struct snd_card *card = pci_get_drvdata(pci);
  	struct cmipci *cm = card->private_data;
  	int i;
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
3272
  	pci_set_power_state(pci, PCI_D0);
30b35399c   Takashi Iwai   [ALSA] Various fi...
3273
3274
3275
3276
3277
3278
3279
3280
  	pci_restore_state(pci);
  	if (pci_enable_device(pci) < 0) {
  		printk(KERN_ERR "cmipci: pci_enable_device failed, "
  		       "disabling device
  ");
  		snd_card_disconnect(card);
  		return -EIO;
  	}
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
  	pci_set_master(pci);
  
  	/* reset / initialize to a sane state */
  	snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
  	snd_cmipci_ch_reset(cm, CM_CH_PLAY);
  	snd_cmipci_ch_reset(cm, CM_CH_CAPT);
  	snd_cmipci_mixer_write(cm, 0, 0);
  
  	/* restore registers */
  	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  		snd_cmipci_write(cm, saved_regs[i], cm->saved_regs[i]);
  	for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
  		snd_cmipci_mixer_write(cm, saved_mixers[i], cm->saved_mixers[i]);
  
  	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  	return 0;
  }
  #endif /* CONFIG_PM */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3299
  static struct pci_driver driver = {
3733e424c   Takashi Iwai   ALSA: Use KBUILD_...
3300
  	.name = KBUILD_MODNAME,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3301
3302
3303
  	.id_table = snd_cmipci_ids,
  	.probe = snd_cmipci_probe,
  	.remove = __devexit_p(snd_cmipci_remove),
cb60e5f5b   Takashi Iwai   [ALSA] cmipci - A...
3304
3305
3306
3307
  #ifdef CONFIG_PM
  	.suspend = snd_cmipci_suspend,
  	.resume = snd_cmipci_resume,
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3308
3309
3310
3311
  };
  	
  static int __init alsa_card_cmipci_init(void)
  {
01d25d460   Takashi Iwai   [ALSA] Replace pc...
3312
  	return pci_register_driver(&driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3313
3314
3315
3316
3317
3318
3319
3320
3321
  }
  
  static void __exit alsa_card_cmipci_exit(void)
  {
  	pci_unregister_driver(&driver);
  }
  
  module_init(alsa_card_cmipci_init)
  module_exit(alsa_card_cmipci_exit)