7cf4fc193419789f81b9a88f09e08da8b1fa98e3
[safe/jmp/linux-2.6] / sound / pci / cmipci.c
1 /*
2  * Driver for C-Media CMI8338 and 8738 PCI soundcards.
3  * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  */
19  
20 /* Does not work. Warning may block system in capture mode */
21 /* #define USE_VAR48KRATE */
22
23 #include <sound/driver.h>
24 #include <asm/io.h>
25 #include <linux/delay.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <linux/pci.h>
29 #include <linux/slab.h>
30 #include <linux/gameport.h>
31 #include <linux/moduleparam.h>
32 #include <linux/mutex.h>
33 #include <sound/core.h>
34 #include <sound/info.h>
35 #include <sound/control.h>
36 #include <sound/pcm.h>
37 #include <sound/rawmidi.h>
38 #include <sound/mpu401.h>
39 #include <sound/opl3.h>
40 #include <sound/sb.h>
41 #include <sound/asoundef.h>
42 #include <sound/initval.h>
43
44 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
45 MODULE_DESCRIPTION("C-Media CMI8x38 PCI");
46 MODULE_LICENSE("GPL");
47 MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8738},"
48                 "{C-Media,CMI8738B},"
49                 "{C-Media,CMI8338A},"
50                 "{C-Media,CMI8338B}}");
51
52 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
53 #define SUPPORT_JOYSTICK 1
54 #endif
55
56 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
57 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
58 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable switches */
59 static long mpu_port[SNDRV_CARDS];
60 static long fm_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
61 static int soft_ac3[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
62 #ifdef SUPPORT_JOYSTICK
63 static int joystick_port[SNDRV_CARDS];
64 #endif
65
66 module_param_array(index, int, NULL, 0444);
67 MODULE_PARM_DESC(index, "Index value for C-Media PCI soundcard.");
68 module_param_array(id, charp, NULL, 0444);
69 MODULE_PARM_DESC(id, "ID string for C-Media PCI soundcard.");
70 module_param_array(enable, bool, NULL, 0444);
71 MODULE_PARM_DESC(enable, "Enable C-Media PCI soundcard.");
72 module_param_array(mpu_port, long, NULL, 0444);
73 MODULE_PARM_DESC(mpu_port, "MPU-401 port.");
74 module_param_array(fm_port, long, NULL, 0444);
75 MODULE_PARM_DESC(fm_port, "FM port.");
76 module_param_array(soft_ac3, bool, NULL, 0444);
77 MODULE_PARM_DESC(soft_ac3, "Sofware-conversion of raw SPDIF packets (model 033 only).");
78 #ifdef SUPPORT_JOYSTICK
79 module_param_array(joystick_port, int, NULL, 0444);
80 MODULE_PARM_DESC(joystick_port, "Joystick port address.");
81 #endif
82
83 /*
84  * CM8x38 registers definition
85  */
86
87 #define CM_REG_FUNCTRL0         0x00
88 #define CM_RST_CH1              0x00080000
89 #define CM_RST_CH0              0x00040000
90 #define CM_CHEN1                0x00020000      /* ch1: enable */
91 #define CM_CHEN0                0x00010000      /* ch0: enable */
92 #define CM_PAUSE1               0x00000008      /* ch1: pause */
93 #define CM_PAUSE0               0x00000004      /* ch0: pause */
94 #define CM_CHADC1               0x00000002      /* ch1, 0:playback, 1:record */
95 #define CM_CHADC0               0x00000001      /* ch0, 0:playback, 1:record */
96
97 #define CM_REG_FUNCTRL1         0x04
98 #define CM_DSFC_MASK            0x0000E000      /* channel 1 (DAC?) sampling frequency */
99 #define CM_DSFC_SHIFT           13
100 #define CM_ASFC_MASK            0x00001C00      /* channel 0 (ADC?) sampling frequency */
101 #define CM_ASFC_SHIFT           10
102 #define CM_SPDF_1               0x00000200      /* SPDIF IN/OUT at channel B */
103 #define CM_SPDF_0               0x00000100      /* SPDIF OUT only channel A */
104 #define CM_SPDFLOOP             0x00000080      /* ext. SPDIIF/IN -> OUT loopback */
105 #define CM_SPDO2DAC             0x00000040      /* SPDIF/OUT can be heard from internal DAC */
106 #define CM_INTRM                0x00000020      /* master control block (MCB) interrupt enabled */
107 #define CM_BREQ                 0x00000010      /* bus master enabled */
108 #define CM_VOICE_EN             0x00000008      /* legacy voice (SB16,FM) */
109 #define CM_UART_EN              0x00000004      /* legacy UART */
110 #define CM_JYSTK_EN             0x00000002      /* legacy joystick */
111 #define CM_ZVPORT               0x00000001      /* ZVPORT */
112
113 #define CM_REG_CHFORMAT         0x08
114
115 #define CM_CHB3D5C              0x80000000      /* 5,6 channels */
116 #define CM_FMOFFSET2            0x40000000      /* initial FM PCM offset 2 when Fmute=1 */
117 #define CM_CHB3D                0x20000000      /* 4 channels */
118
119 #define CM_CHIP_MASK1           0x1f000000
120 #define CM_CHIP_037             0x01000000
121 #define CM_SETLAT48             0x00800000      /* set latency timer 48h */
122 #define CM_EDGEIRQ              0x00400000      /* emulated edge trigger legacy IRQ */
123 #define CM_SPD24SEL39           0x00200000      /* 24-bit spdif: model 039 */
124 #define CM_AC3EN1               0x00100000      /* enable AC3: model 037 */
125 #define CM_SPDIF_SELECT1        0x00080000      /* for model <= 037 ? */
126 #define CM_SPD24SEL             0x00020000      /* 24bit spdif: model 037 */
127 /* #define CM_SPDIF_INVERSE     0x00010000 */ /* ??? */
128
129 #define CM_ADCBITLEN_MASK       0x0000C000      
130 #define CM_ADCBITLEN_16         0x00000000
131 #define CM_ADCBITLEN_15         0x00004000
132 #define CM_ADCBITLEN_14         0x00008000
133 #define CM_ADCBITLEN_13         0x0000C000
134
135 #define CM_ADCDACLEN_MASK       0x00003000      /* model 037 */
136 #define CM_ADCDACLEN_060        0x00000000
137 #define CM_ADCDACLEN_066        0x00001000
138 #define CM_ADCDACLEN_130        0x00002000
139 #define CM_ADCDACLEN_280        0x00003000
140
141 #define CM_ADCDLEN_MASK         0x00003000      /* model 039 */
142 #define CM_ADCDLEN_ORIGINAL     0x00000000
143 #define CM_ADCDLEN_EXTRA        0x00001000
144 #define CM_ADCDLEN_24K          0x00002000
145 #define CM_ADCDLEN_WEIGHT       0x00003000
146
147 #define CM_CH1_SRATE_176K       0x00000800
148 #define CM_CH1_SRATE_96K        0x00000800      /* model 055? */
149 #define CM_CH1_SRATE_88K        0x00000400
150 #define CM_CH0_SRATE_176K       0x00000200
151 #define CM_CH0_SRATE_96K        0x00000200      /* model 055? */
152 #define CM_CH0_SRATE_88K        0x00000100
153
154 #define CM_SPDIF_INVERSE2       0x00000080      /* model 055? */
155 #define CM_DBLSPDS              0x00000040      /* double SPDIF sample rate 88.2/96 */
156 #define CM_POLVALID             0x00000020      /* inverse SPDIF/IN valid bit */
157 #define CM_SPDLOCKED            0x00000010
158
159 #define CM_CH1FMT_MASK          0x0000000C      /* bit 3: 16 bits, bit 2: stereo */
160 #define CM_CH1FMT_SHIFT         2
161 #define CM_CH0FMT_MASK          0x00000003      /* bit 1: 16 bits, bit 0: stereo */
162 #define CM_CH0FMT_SHIFT         0
163
164 #define CM_REG_INT_HLDCLR       0x0C
165 #define CM_CHIP_MASK2           0xff000000
166 #define CM_CHIP_8768            0x20000000
167 #define CM_CHIP_055             0x08000000
168 #define CM_CHIP_039             0x04000000
169 #define CM_CHIP_039_6CH         0x01000000
170 #define CM_UNKNOWN_INT_EN       0x00080000      /* ? */
171 #define CM_TDMA_INT_EN          0x00040000
172 #define CM_CH1_INT_EN           0x00020000
173 #define CM_CH0_INT_EN           0x00010000
174
175 #define CM_REG_INT_STATUS       0x10
176 #define CM_INTR                 0x80000000
177 #define CM_VCO                  0x08000000      /* Voice Control? CMI8738 */
178 #define CM_MCBINT               0x04000000      /* Master Control Block abort cond.? */
179 #define CM_UARTINT              0x00010000
180 #define CM_LTDMAINT             0x00008000
181 #define CM_HTDMAINT             0x00004000
182 #define CM_XDO46                0x00000080      /* Modell 033? Direct programming EEPROM (read data register) */
183 #define CM_LHBTOG               0x00000040      /* High/Low status from DMA ctrl register */
184 #define CM_LEG_HDMA             0x00000020      /* Legacy is in High DMA channel */
185 #define CM_LEG_STEREO           0x00000010      /* Legacy is in Stereo mode */
186 #define CM_CH1BUSY              0x00000008
187 #define CM_CH0BUSY              0x00000004
188 #define CM_CHINT1               0x00000002
189 #define CM_CHINT0               0x00000001
190
191 #define CM_REG_LEGACY_CTRL      0x14
192 #define CM_NXCHG                0x80000000      /* don't map base reg dword->sample */
193 #define CM_VMPU_MASK            0x60000000      /* MPU401 i/o port address */
194 #define CM_VMPU_330             0x00000000
195 #define CM_VMPU_320             0x20000000
196 #define CM_VMPU_310             0x40000000
197 #define CM_VMPU_300             0x60000000
198 #define CM_ENWR8237             0x10000000      /* enable bus master to write 8237 base reg */
199 #define CM_VSBSEL_MASK          0x0C000000      /* SB16 base address */
200 #define CM_VSBSEL_220           0x00000000
201 #define CM_VSBSEL_240           0x04000000
202 #define CM_VSBSEL_260           0x08000000
203 #define CM_VSBSEL_280           0x0C000000
204 #define CM_FMSEL_MASK           0x03000000      /* FM OPL3 base address */
205 #define CM_FMSEL_388            0x00000000
206 #define CM_FMSEL_3C8            0x01000000
207 #define CM_FMSEL_3E0            0x02000000
208 #define CM_FMSEL_3E8            0x03000000
209 #define CM_ENSPDOUT             0x00800000      /* enable XSPDIF/OUT to I/O interface */
210 #define CM_SPDCOPYRHT           0x00400000      /* spdif in/out copyright bit */
211 #define CM_DAC2SPDO             0x00200000      /* enable wave+fm_midi -> SPDIF/OUT */
212 #define CM_INVIDWEN             0x00100000      /* internal vendor ID write enable, model 039? */
213 #define CM_SETRETRY             0x00100000      /* 0: legacy i/o wait (default), 1: legacy i/o bus retry */
214 #define CM_C_EEACCESS           0x00080000      /* direct programming eeprom regs */
215 #define CM_C_EECS               0x00040000
216 #define CM_C_EEDI46             0x00020000
217 #define CM_C_EECK46             0x00010000
218 #define CM_CHB3D6C              0x00008000      /* 5.1 channels support */
219 #define CM_CENTR2LIN            0x00004000      /* line-in as center out */
220 #define CM_BASE2LIN             0x00002000      /* line-in as bass out */
221 #define CM_EXBASEN              0x00001000      /* external bass input enable */
222
223 #define CM_REG_MISC_CTRL        0x18
224 #define CM_PWD                  0x80000000      /* power down */
225 #define CM_RESET                0x40000000
226 #define CM_SFIL_MASK            0x30000000      /* filter control at front end DAC, model 037? */
227 #define CM_VMGAIN               0x10000000      /* analog master amp +6dB, model 039? */
228 #define CM_TXVX                 0x08000000      /* model 037? */
229 #define CM_N4SPK3D              0x04000000      /* copy front to rear */
230 #define CM_SPDO5V               0x02000000      /* 5V spdif output (1 = 0.5v (coax)) */
231 #define CM_SPDIF48K             0x01000000      /* write */
232 #define CM_SPATUS48K            0x01000000      /* read */
233 #define CM_ENDBDAC              0x00800000      /* enable double dac */
234 #define CM_XCHGDAC              0x00400000      /* 0: front=ch0, 1: front=ch1 */
235 #define CM_SPD32SEL             0x00200000      /* 0: 16bit SPDIF, 1: 32bit */
236 #define CM_SPDFLOOPI            0x00100000      /* int. SPDIF-OUT -> int. IN */
237 #define CM_FM_EN                0x00080000      /* enable legacy FM */
238 #define CM_AC3EN2               0x00040000      /* enable AC3: model 039 */
239 #define CM_ENWRASID             0x00010000      /* choose writable internal SUBID (audio) */
240 #define CM_VIDWPDSB             0x00010000      /* model 037? */
241 #define CM_SPDF_AC97            0x00008000      /* 0: SPDIF/OUT 44.1K, 1: 48K */
242 #define CM_MASK_EN              0x00004000      /* activate channel mask on legacy DMA */
243 #define CM_ENWRMSID             0x00002000      /* choose writable internal SUBID (modem) */
244 #define CM_VIDWPPRT             0x00002000      /* model 037? */
245 #define CM_SFILENB              0x00001000      /* filter stepping at front end DAC, model 037? */
246 #define CM_MMODE_MASK           0x00000E00      /* model DAA interface mode */
247 #define CM_SPDIF_SELECT2        0x00000100      /* for model > 039 ? */
248 #define CM_ENCENTER             0x00000080
249 #define CM_FLINKON              0x00000080      /* force modem link detection on, model 037 */
250 #define CM_MUTECH1              0x00000040      /* mute PCI ch1 to DAC */
251 #define CM_FLINKOFF             0x00000040      /* force modem link detection off, model 037 */
252 #define CM_UNKNOWN_18_5         0x00000020      /* ? */
253 #define CM_MIDSMP               0x00000010      /* 1/2 interpolation at front end DAC */
254 #define CM_UPDDMA_MASK          0x0000000C      /* TDMA position update notification */
255 #define CM_UPDDMA_2048          0x00000000
256 #define CM_UPDDMA_1024          0x00000004
257 #define CM_UPDDMA_512           0x00000008
258 #define CM_UPDDMA_256           0x0000000C              
259 #define CM_TWAIT_MASK           0x00000003      /* model 037 */
260 #define CM_TWAIT1               0x00000002      /* FM i/o cycle, 0: 48, 1: 64 PCICLKs */
261 #define CM_TWAIT0               0x00000001      /* i/o cycle, 0: 4, 1: 6 PCICLKs */
262
263 #define CM_REG_TDMA_POSITION    0x1C
264 #define CM_TDMA_CNT_MASK        0xFFFF0000      /* current byte/word count */
265 #define CM_TDMA_ADR_MASK        0x0000FFFF      /* current address */
266
267         /* byte */
268 #define CM_REG_MIXER0           0x20
269 #define CM_REG_SBVR             0x20            /* write: sb16 version */
270 #define CM_REG_DEV              0x20            /* read: hardware device version */
271
272 #define CM_REG_MIXER21          0x21
273 #define CM_UNKNOWN_21_MASK      0x78            /* ? */
274 #define CM_X_ADPCM              0x04            /* SB16 ADPCM enable */
275 #define CM_PROINV               0x02            /* SBPro left/right channel switching */
276 #define CM_X_SB16               0x01            /* SB16 compatible */
277
278 #define CM_REG_SB16_DATA        0x22
279 #define CM_REG_SB16_ADDR        0x23
280
281 #define CM_REFFREQ_XIN          (315*1000*1000)/22      /* 14.31818 Mhz reference clock frequency pin XIN */
282 #define CM_ADCMULT_XIN          512                     /* Guessed (487 best for 44.1kHz, not for 88/176kHz) */
283 #define CM_TOLERANCE_RATE       0.001                   /* Tolerance sample rate pitch (1000ppm) */
284 #define CM_MAXIMUM_RATE         80000000                /* Note more than 80MHz */
285
286 #define CM_REG_MIXER1           0x24
287 #define CM_FMMUTE               0x80    /* mute FM */
288 #define CM_FMMUTE_SHIFT         7
289 #define CM_WSMUTE               0x40    /* mute PCM */
290 #define CM_WSMUTE_SHIFT         6
291 #define CM_REAR2LIN             0x20    /* lin-in -> rear line out */
292 #define CM_REAR2LIN_SHIFT       5
293 #define CM_REAR2FRONT           0x10    /* exchange rear/front */
294 #define CM_REAR2FRONT_SHIFT     4
295 #define CM_WAVEINL              0x08    /* digital wave rec. left chan */
296 #define CM_WAVEINL_SHIFT        3
297 #define CM_WAVEINR              0x04    /* digical wave rec. right */
298 #define CM_WAVEINR_SHIFT        2
299 #define CM_X3DEN                0x02    /* 3D surround enable */
300 #define CM_X3DEN_SHIFT          1
301 #define CM_CDPLAY               0x01    /* enable SPDIF/IN PCM -> DAC */
302 #define CM_CDPLAY_SHIFT         0
303
304 #define CM_REG_MIXER2           0x25
305 #define CM_RAUXREN              0x80    /* AUX right capture */
306 #define CM_RAUXREN_SHIFT        7
307 #define CM_RAUXLEN              0x40    /* AUX left capture */
308 #define CM_RAUXLEN_SHIFT        6
309 #define CM_VAUXRM               0x20    /* AUX right mute */
310 #define CM_VAUXRM_SHIFT         5
311 #define CM_VAUXLM               0x10    /* AUX left mute */
312 #define CM_VAUXLM_SHIFT         4
313 #define CM_VADMIC_MASK          0x0e    /* mic gain level (0-3) << 1 */
314 #define CM_VADMIC_SHIFT         1
315 #define CM_MICGAINZ             0x01    /* mic boost */
316 #define CM_MICGAINZ_SHIFT       0
317
318 #define CM_REG_MIXER3           0x24
319 #define CM_REG_AUX_VOL          0x26
320 #define CM_VAUXL_MASK           0xf0
321 #define CM_VAUXR_MASK           0x0f
322
323 #define CM_REG_MISC             0x27
324 #define CM_UNKNOWN_27_MASK      0xd8    /* ? */
325 #define CM_XGPO1                0x20
326 // #define CM_XGPBIO            0x04
327 #define CM_MIC_CENTER_LFE       0x04    /* mic as center/lfe out? (model 039 or later?) */
328 #define CM_SPDIF_INVERSE        0x04    /* spdif input phase inverse (model 037) */
329 #define CM_SPDVALID             0x02    /* spdif input valid check */
330 #define CM_DMAUTO               0x01    /* SB16 DMA auto detect */
331
332 #define CM_REG_AC97             0x28    /* hmmm.. do we have ac97 link? */
333 /*
334  * For CMI-8338 (0x28 - 0x2b) .. is this valid for CMI-8738
335  * or identical with AC97 codec?
336  */
337 #define CM_REG_EXTERN_CODEC     CM_REG_AC97
338
339 /*
340  * MPU401 pci port index address 0x40 - 0x4f (CMI-8738 spec ver. 0.6)
341  */
342 #define CM_REG_MPU_PCI          0x40
343
344 /*
345  * FM pci port index address 0x50 - 0x5f (CMI-8738 spec ver. 0.6)
346  */
347 #define CM_REG_FM_PCI           0x50
348
349 /*
350  * access from SB-mixer port
351  */
352 #define CM_REG_EXTENT_IND       0xf0
353 #define CM_VPHONE_MASK          0xe0    /* Phone volume control (0-3) << 5 */
354 #define CM_VPHONE_SHIFT         5
355 #define CM_VPHOM                0x10    /* Phone mute control */
356 #define CM_VSPKM                0x08    /* Speaker mute control, default high */
357 #define CM_RLOOPREN             0x04    /* Rec. R-channel enable */
358 #define CM_RLOOPLEN             0x02    /* Rec. L-channel enable */
359 #define CM_VADMIC3              0x01    /* Mic record boost */
360
361 /*
362  * CMI-8338 spec ver 0.5 (this is not valid for CMI-8738):
363  * the 8 registers 0xf8 - 0xff are used for programming m/n counter by the PLL
364  * unit (readonly?).
365  */
366 #define CM_REG_PLL              0xf8
367
368 /*
369  * extended registers
370  */
371 #define CM_REG_CH0_FRAME1       0x80    /* write: base address */
372 #define CM_REG_CH0_FRAME2       0x84    /* read: current address */
373 #define CM_REG_CH1_FRAME1       0x88    /* 0-15: count of samples at bus master; buffer size */
374 #define CM_REG_CH1_FRAME2       0x8C    /* 16-31: count of samples at codec; fragment size */
375
376 #define CM_REG_EXT_MISC         0x90
377 #define CM_ADC48K44K            0x10000000      /* ADC parameters group, 0: 44k, 1: 48k */
378 #define CM_CHB3D8C              0x00200000      /* 7.1 channels support */
379 #define CM_SPD32FMT             0x00100000      /* SPDIF/IN 32k sample rate */
380 #define CM_ADC2SPDIF            0x00080000      /* ADC output to SPDIF/OUT */
381 #define CM_SHAREADC             0x00040000      /* DAC in ADC as Center/LFE */
382 #define CM_REALTCMP             0x00020000      /* monitor the CMPL/CMPR of ADC */
383 #define CM_INVLRCK              0x00010000      /* invert ZVPORT's LRCK */
384 #define CM_UNKNOWN_90_MASK      0x0000FFFF      /* ? */
385
386 /*
387  * size of i/o region
388  */
389 #define CM_EXTENT_CODEC   0x100
390 #define CM_EXTENT_MIDI    0x2
391 #define CM_EXTENT_SYNTH   0x4
392
393
394 /*
395  * channels for playback / capture
396  */
397 #define CM_CH_PLAY      0
398 #define CM_CH_CAPT      1
399
400 /*
401  * flags to check device open/close
402  */
403 #define CM_OPEN_NONE    0
404 #define CM_OPEN_CH_MASK 0x01
405 #define CM_OPEN_DAC     0x10
406 #define CM_OPEN_ADC     0x20
407 #define CM_OPEN_SPDIF   0x40
408 #define CM_OPEN_MCHAN   0x80
409 #define CM_OPEN_PLAYBACK        (CM_CH_PLAY | CM_OPEN_DAC)
410 #define CM_OPEN_PLAYBACK2       (CM_CH_CAPT | CM_OPEN_DAC)
411 #define CM_OPEN_PLAYBACK_MULTI  (CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_MCHAN)
412 #define CM_OPEN_CAPTURE         (CM_CH_CAPT | CM_OPEN_ADC)
413 #define CM_OPEN_SPDIF_PLAYBACK  (CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_SPDIF)
414 #define CM_OPEN_SPDIF_CAPTURE   (CM_CH_CAPT | CM_OPEN_ADC | CM_OPEN_SPDIF)
415
416
417 #if CM_CH_PLAY == 1
418 #define CM_PLAYBACK_SRATE_176K  CM_CH1_SRATE_176K
419 #define CM_PLAYBACK_SPDF        CM_SPDF_1
420 #define CM_CAPTURE_SPDF         CM_SPDF_0
421 #else
422 #define CM_PLAYBACK_SRATE_176K CM_CH0_SRATE_176K
423 #define CM_PLAYBACK_SPDF        CM_SPDF_0
424 #define CM_CAPTURE_SPDF         CM_SPDF_1
425 #endif
426
427
428 /*
429  * driver data
430  */
431
432 struct cmipci_pcm {
433         struct snd_pcm_substream *substream;
434         int running;            /* dac/adc running? */
435         unsigned int dma_size;  /* in frames */
436         unsigned int period_size;       /* in frames */
437         unsigned int offset;    /* physical address of the buffer */
438         unsigned int fmt;       /* format bits */
439         int ch;                 /* channel (0/1) */
440         unsigned int is_dac;            /* is dac? */
441         int bytes_per_frame;
442         int shift;
443 };
444
445 /* mixer elements toggled/resumed during ac3 playback */
446 struct cmipci_mixer_auto_switches {
447         const char *name;       /* switch to toggle */
448         int toggle_on;          /* value to change when ac3 mode */
449 };
450 static const struct cmipci_mixer_auto_switches cm_saved_mixer[] = {
451         {"PCM Playback Switch", 0},
452         {"IEC958 Output Switch", 1},
453         {"IEC958 Mix Analog", 0},
454         // {"IEC958 Out To DAC", 1}, // no longer used
455         {"IEC958 Loop", 0},
456 };
457 #define CM_SAVED_MIXERS         ARRAY_SIZE(cm_saved_mixer)
458
459 struct cmipci {
460         struct snd_card *card;
461
462         struct pci_dev *pci;
463         unsigned int device;    /* device ID */
464         int irq;
465
466         unsigned long iobase;
467         unsigned int ctrl;      /* FUNCTRL0 current value */
468
469         struct snd_pcm *pcm;            /* DAC/ADC PCM */
470         struct snd_pcm *pcm2;   /* 2nd DAC */
471         struct snd_pcm *pcm_spdif;      /* SPDIF */
472
473         int chip_version;
474         int max_channels;
475         unsigned int can_ac3_sw: 1;
476         unsigned int can_ac3_hw: 1;
477         unsigned int can_multi_ch: 1;
478         unsigned int do_soft_ac3: 1;
479
480         unsigned int spdif_playback_avail: 1;   /* spdif ready? */
481         unsigned int spdif_playback_enabled: 1; /* spdif switch enabled? */
482         int spdif_counter;      /* for software AC3 */
483
484         unsigned int dig_status;
485         unsigned int dig_pcm_status;
486
487         struct snd_pcm_hardware *hw_info[3]; /* for playbacks */
488
489         int opened[2];  /* open mode */
490         struct mutex open_mutex;
491
492         unsigned int mixer_insensitive: 1;
493         struct snd_kcontrol *mixer_res_ctl[CM_SAVED_MIXERS];
494         int mixer_res_status[CM_SAVED_MIXERS];
495
496         struct cmipci_pcm channel[2];   /* ch0 - DAC, ch1 - ADC or 2nd DAC */
497
498         /* external MIDI */
499         struct snd_rawmidi *rmidi;
500
501 #ifdef SUPPORT_JOYSTICK
502         struct gameport *gameport;
503 #endif
504
505         spinlock_t reg_lock;
506
507 #ifdef CONFIG_PM
508         unsigned int saved_regs[0x20];
509         unsigned char saved_mixers[0x20];
510 #endif
511 };
512
513
514 /* read/write operations for dword register */
515 static inline void snd_cmipci_write(struct cmipci *cm, unsigned int cmd, unsigned int data)
516 {
517         outl(data, cm->iobase + cmd);
518 }
519
520 static inline unsigned int snd_cmipci_read(struct cmipci *cm, unsigned int cmd)
521 {
522         return inl(cm->iobase + cmd);
523 }
524
525 /* read/write operations for word register */
526 static inline void snd_cmipci_write_w(struct cmipci *cm, unsigned int cmd, unsigned short data)
527 {
528         outw(data, cm->iobase + cmd);
529 }
530
531 static inline unsigned short snd_cmipci_read_w(struct cmipci *cm, unsigned int cmd)
532 {
533         return inw(cm->iobase + cmd);
534 }
535
536 /* read/write operations for byte register */
537 static inline void snd_cmipci_write_b(struct cmipci *cm, unsigned int cmd, unsigned char data)
538 {
539         outb(data, cm->iobase + cmd);
540 }
541
542 static inline unsigned char snd_cmipci_read_b(struct cmipci *cm, unsigned int cmd)
543 {
544         return inb(cm->iobase + cmd);
545 }
546
547 /* bit operations for dword register */
548 static int snd_cmipci_set_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
549 {
550         unsigned int val, oval;
551         val = oval = inl(cm->iobase + cmd);
552         val |= flag;
553         if (val == oval)
554                 return 0;
555         outl(val, cm->iobase + cmd);
556         return 1;
557 }
558
559 static int snd_cmipci_clear_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
560 {
561         unsigned int val, oval;
562         val = oval = inl(cm->iobase + cmd);
563         val &= ~flag;
564         if (val == oval)
565                 return 0;
566         outl(val, cm->iobase + cmd);
567         return 1;
568 }
569
570 /* bit operations for byte register */
571 static int snd_cmipci_set_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
572 {
573         unsigned char val, oval;
574         val = oval = inb(cm->iobase + cmd);
575         val |= flag;
576         if (val == oval)
577                 return 0;
578         outb(val, cm->iobase + cmd);
579         return 1;
580 }
581
582 static int snd_cmipci_clear_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
583 {
584         unsigned char val, oval;
585         val = oval = inb(cm->iobase + cmd);
586         val &= ~flag;
587         if (val == oval)
588                 return 0;
589         outb(val, cm->iobase + cmd);
590         return 1;
591 }
592
593
594 /*
595  * PCM interface
596  */
597
598 /*
599  * calculate frequency
600  */
601
602 static unsigned int rates[] = { 5512, 11025, 22050, 44100, 8000, 16000, 32000, 48000 };
603
604 static unsigned int snd_cmipci_rate_freq(unsigned int rate)
605 {
606         unsigned int i;
607
608         if (rate > 48000)
609                 rate /= 2;
610         for (i = 0; i < ARRAY_SIZE(rates); i++) {
611                 if (rates[i] == rate)
612                         return i;
613         }
614         snd_BUG();
615         return 0;
616 }
617
618 #ifdef USE_VAR48KRATE
619 /*
620  * Determine PLL values for frequency setup, maybe the CMI8338 (CMI8738???)
621  * does it this way .. maybe not.  Never get any information from C-Media about
622  * that <werner@suse.de>.
623  */
624 static int snd_cmipci_pll_rmn(unsigned int rate, unsigned int adcmult, int *r, int *m, int *n)
625 {
626         unsigned int delta, tolerance;
627         int xm, xn, xr;
628
629         for (*r = 0; rate < CM_MAXIMUM_RATE/adcmult; *r += (1<<5))
630                 rate <<= 1;
631         *n = -1;
632         if (*r > 0xff)
633                 goto out;
634         tolerance = rate*CM_TOLERANCE_RATE;
635
636         for (xn = (1+2); xn < (0x1f+2); xn++) {
637                 for (xm = (1+2); xm < (0xff+2); xm++) {
638                         xr = ((CM_REFFREQ_XIN/adcmult) * xm) / xn;
639
640                         if (xr < rate)
641                                 delta = rate - xr;
642                         else
643                                 delta = xr - rate;
644
645                         /*
646                          * If we found one, remember this,
647                          * and try to find a closer one
648                          */
649                         if (delta < tolerance) {
650                                 tolerance = delta;
651                                 *m = xm - 2;
652                                 *n = xn - 2;
653                         }
654                 }
655         }
656 out:
657         return (*n > -1);
658 }
659
660 /*
661  * Program pll register bits, I assume that the 8 registers 0xf8 upto 0xff
662  * are mapped onto the 8 ADC/DAC sampling frequency which can be choosen
663  * at the register CM_REG_FUNCTRL1 (0x04).
664  * Problem: other ways are also possible (any information about that?)
665  */
666 static void snd_cmipci_set_pll(struct cmipci *cm, unsigned int rate, unsigned int slot)
667 {
668         unsigned int reg = CM_REG_PLL + slot;
669         /*
670          * Guess that this programs at reg. 0x04 the pos 15:13/12:10
671          * for DSFC/ASFC (000 upto 111).
672          */
673
674         /* FIXME: Init (Do we've to set an other register first before programming?) */
675
676         /* FIXME: Is this correct? Or shouldn't the m/n/r values be used for that? */
677         snd_cmipci_write_b(cm, reg, rate>>8);
678         snd_cmipci_write_b(cm, reg, rate&0xff);
679
680         /* FIXME: Setup (Do we've to set an other register first to enable this?) */
681 }
682 #endif /* USE_VAR48KRATE */
683
684 static int snd_cmipci_hw_params(struct snd_pcm_substream *substream,
685                                 struct snd_pcm_hw_params *hw_params)
686 {
687         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
688 }
689
690 static int snd_cmipci_playback2_hw_params(struct snd_pcm_substream *substream,
691                                           struct snd_pcm_hw_params *hw_params)
692 {
693         struct cmipci *cm = snd_pcm_substream_chip(substream);
694         if (params_channels(hw_params) > 2) {
695                 mutex_lock(&cm->open_mutex);
696                 if (cm->opened[CM_CH_PLAY]) {
697                         mutex_unlock(&cm->open_mutex);
698                         return -EBUSY;
699                 }
700                 /* reserve the channel A */
701                 cm->opened[CM_CH_PLAY] = CM_OPEN_PLAYBACK_MULTI;
702                 mutex_unlock(&cm->open_mutex);
703         }
704         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
705 }
706
707 static void snd_cmipci_ch_reset(struct cmipci *cm, int ch)
708 {
709         int reset = CM_RST_CH0 << (cm->channel[ch].ch);
710         snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
711         snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
712         udelay(10);
713 }
714
715 static int snd_cmipci_hw_free(struct snd_pcm_substream *substream)
716 {
717         return snd_pcm_lib_free_pages(substream);
718 }
719
720
721 /*
722  */
723
724 static unsigned int hw_channels[] = {1, 2, 4, 6, 8};
725 static struct snd_pcm_hw_constraint_list hw_constraints_channels_4 = {
726         .count = 3,
727         .list = hw_channels,
728         .mask = 0,
729 };
730 static struct snd_pcm_hw_constraint_list hw_constraints_channels_6 = {
731         .count = 4,
732         .list = hw_channels,
733         .mask = 0,
734 };
735 static struct snd_pcm_hw_constraint_list hw_constraints_channels_8 = {
736         .count = 5,
737         .list = hw_channels,
738         .mask = 0,
739 };
740
741 static int set_dac_channels(struct cmipci *cm, struct cmipci_pcm *rec, int channels)
742 {
743         if (channels > 2) {
744                 if (!cm->can_multi_ch || !rec->ch)
745                         return -EINVAL;
746                 if (rec->fmt != 0x03) /* stereo 16bit only */
747                         return -EINVAL;
748         }
749
750         if (cm->can_multi_ch) {
751                 spin_lock_irq(&cm->reg_lock);
752                 if (channels > 2) {
753                         snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
754                         snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
755                 } else {
756                         snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
757                         snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
758                 }
759                 if (channels == 8)
760                         snd_cmipci_set_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
761                 else
762                         snd_cmipci_clear_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
763                 if (channels == 6) {
764                         snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
765                         snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
766                 } else {
767                         snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
768                         snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
769                 }
770                 if (channels == 4)
771                         snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
772                 else
773                         snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
774                 spin_unlock_irq(&cm->reg_lock);
775         }
776         return 0;
777 }
778
779
780 /*
781  * prepare playback/capture channel
782  * channel to be used must have been set in rec->ch.
783  */
784 static int snd_cmipci_pcm_prepare(struct cmipci *cm, struct cmipci_pcm *rec,
785                                  struct snd_pcm_substream *substream)
786 {
787         unsigned int reg, freq, val;
788         struct snd_pcm_runtime *runtime = substream->runtime;
789
790         rec->fmt = 0;
791         rec->shift = 0;
792         if (snd_pcm_format_width(runtime->format) >= 16) {
793                 rec->fmt |= 0x02;
794                 if (snd_pcm_format_width(runtime->format) > 16)
795                         rec->shift++; /* 24/32bit */
796         }
797         if (runtime->channels > 1)
798                 rec->fmt |= 0x01;
799         if (rec->is_dac && set_dac_channels(cm, rec, runtime->channels) < 0) {
800                 snd_printd("cannot set dac channels\n");
801                 return -EINVAL;
802         }
803
804         rec->offset = runtime->dma_addr;
805         /* buffer and period sizes in frame */
806         rec->dma_size = runtime->buffer_size << rec->shift;
807         rec->period_size = runtime->period_size << rec->shift;
808         if (runtime->channels > 2) {
809                 /* multi-channels */
810                 rec->dma_size = (rec->dma_size * runtime->channels) / 2;
811                 rec->period_size = (rec->period_size * runtime->channels) / 2;
812         }
813
814         spin_lock_irq(&cm->reg_lock);
815
816         /* set buffer address */
817         reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
818         snd_cmipci_write(cm, reg, rec->offset);
819         /* program sample counts */
820         reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
821         snd_cmipci_write_w(cm, reg, rec->dma_size - 1);
822         snd_cmipci_write_w(cm, reg + 2, rec->period_size - 1);
823
824         /* set adc/dac flag */
825         val = rec->ch ? CM_CHADC1 : CM_CHADC0;
826         if (rec->is_dac)
827                 cm->ctrl &= ~val;
828         else
829                 cm->ctrl |= val;
830         snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
831         //snd_printd("cmipci: functrl0 = %08x\n", cm->ctrl);
832
833         /* set sample rate */
834         freq = snd_cmipci_rate_freq(runtime->rate);
835         val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
836         if (rec->ch) {
837                 val &= ~CM_DSFC_MASK;
838                 val |= (freq << CM_DSFC_SHIFT) & CM_DSFC_MASK;
839         } else {
840                 val &= ~CM_ASFC_MASK;
841                 val |= (freq << CM_ASFC_SHIFT) & CM_ASFC_MASK;
842         }
843         snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
844         //snd_printd("cmipci: functrl1 = %08x\n", val);
845
846         /* set format */
847         val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
848         if (rec->ch) {
849                 val &= ~CM_CH1FMT_MASK;
850                 val |= rec->fmt << CM_CH1FMT_SHIFT;
851         } else {
852                 val &= ~CM_CH0FMT_MASK;
853                 val |= rec->fmt << CM_CH0FMT_SHIFT;
854         }
855         if (cm->chip_version == 68) {
856                 if (runtime->rate == 88200)
857                         val |= CM_CH0_SRATE_88K << (rec->ch * 2);
858                 else
859                         val &= ~(CM_CH0_SRATE_88K << (rec->ch * 2));
860                 if (runtime->rate == 96000)
861                         val |= CM_CH0_SRATE_96K << (rec->ch * 2);
862                 else
863                         val &= ~(CM_CH0_SRATE_96K << (rec->ch * 2));
864         }
865         snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
866         //snd_printd("cmipci: chformat = %08x\n", val);
867
868         rec->running = 0;
869         spin_unlock_irq(&cm->reg_lock);
870
871         return 0;
872 }
873
874 /*
875  * PCM trigger/stop
876  */
877 static int snd_cmipci_pcm_trigger(struct cmipci *cm, struct cmipci_pcm *rec,
878                                   struct snd_pcm_substream *substream, int cmd)
879 {
880         unsigned int inthld, chen, reset, pause;
881         int result = 0;
882
883         inthld = CM_CH0_INT_EN << rec->ch;
884         chen = CM_CHEN0 << rec->ch;
885         reset = CM_RST_CH0 << rec->ch;
886         pause = CM_PAUSE0 << rec->ch;
887
888         spin_lock(&cm->reg_lock);
889         switch (cmd) {
890         case SNDRV_PCM_TRIGGER_START:
891                 rec->running = 1;
892                 /* set interrupt */
893                 snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, inthld);
894                 cm->ctrl |= chen;
895                 /* enable channel */
896                 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
897                 //snd_printd("cmipci: functrl0 = %08x\n", cm->ctrl);
898                 break;
899         case SNDRV_PCM_TRIGGER_STOP:
900                 rec->running = 0;
901                 /* disable interrupt */
902                 snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, inthld);
903                 /* reset */
904                 cm->ctrl &= ~chen;
905                 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
906                 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
907                 break;
908         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
909         case SNDRV_PCM_TRIGGER_SUSPEND:
910                 cm->ctrl |= pause;
911                 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
912                 break;
913         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
914         case SNDRV_PCM_TRIGGER_RESUME:
915                 cm->ctrl &= ~pause;
916                 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
917                 break;
918         default:
919                 result = -EINVAL;
920                 break;
921         }
922         spin_unlock(&cm->reg_lock);
923         return result;
924 }
925
926 /*
927  * return the current pointer
928  */
929 static snd_pcm_uframes_t snd_cmipci_pcm_pointer(struct cmipci *cm, struct cmipci_pcm *rec,
930                                                 struct snd_pcm_substream *substream)
931 {
932         size_t ptr;
933         unsigned int reg;
934         if (!rec->running)
935                 return 0;
936 #if 1 // this seems better..
937         reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
938         ptr = rec->dma_size - (snd_cmipci_read_w(cm, reg) + 1);
939         ptr >>= rec->shift;
940 #else
941         reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
942         ptr = snd_cmipci_read(cm, reg) - rec->offset;
943         ptr = bytes_to_frames(substream->runtime, ptr);
944 #endif
945         if (substream->runtime->channels > 2)
946                 ptr = (ptr * 2) / substream->runtime->channels;
947         return ptr;
948 }
949
950 /*
951  * playback
952  */
953
954 static int snd_cmipci_playback_trigger(struct snd_pcm_substream *substream,
955                                        int cmd)
956 {
957         struct cmipci *cm = snd_pcm_substream_chip(substream);
958         return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_PLAY], substream, cmd);
959 }
960
961 static snd_pcm_uframes_t snd_cmipci_playback_pointer(struct snd_pcm_substream *substream)
962 {
963         struct cmipci *cm = snd_pcm_substream_chip(substream);
964         return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_PLAY], substream);
965 }
966
967
968
969 /*
970  * capture
971  */
972
973 static int snd_cmipci_capture_trigger(struct snd_pcm_substream *substream,
974                                      int cmd)
975 {
976         struct cmipci *cm = snd_pcm_substream_chip(substream);
977         return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_CAPT], substream, cmd);
978 }
979
980 static snd_pcm_uframes_t snd_cmipci_capture_pointer(struct snd_pcm_substream *substream)
981 {
982         struct cmipci *cm = snd_pcm_substream_chip(substream);
983         return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_CAPT], substream);
984 }
985
986
987 /*
988  * hw preparation for spdif
989  */
990
991 static int snd_cmipci_spdif_default_info(struct snd_kcontrol *kcontrol,
992                                          struct snd_ctl_elem_info *uinfo)
993 {
994         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
995         uinfo->count = 1;
996         return 0;
997 }
998
999 static int snd_cmipci_spdif_default_get(struct snd_kcontrol *kcontrol,
1000                                         struct snd_ctl_elem_value *ucontrol)
1001 {
1002         struct cmipci *chip = snd_kcontrol_chip(kcontrol);
1003         int i;
1004
1005         spin_lock_irq(&chip->reg_lock);
1006         for (i = 0; i < 4; i++)
1007                 ucontrol->value.iec958.status[i] = (chip->dig_status >> (i * 8)) & 0xff;
1008         spin_unlock_irq(&chip->reg_lock);
1009         return 0;
1010 }
1011
1012 static int snd_cmipci_spdif_default_put(struct snd_kcontrol *kcontrol,
1013                                          struct snd_ctl_elem_value *ucontrol)
1014 {
1015         struct cmipci *chip = snd_kcontrol_chip(kcontrol);
1016         int i, change;
1017         unsigned int val;
1018
1019         val = 0;
1020         spin_lock_irq(&chip->reg_lock);
1021         for (i = 0; i < 4; i++)
1022                 val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
1023         change = val != chip->dig_status;
1024         chip->dig_status = val;
1025         spin_unlock_irq(&chip->reg_lock);
1026         return change;
1027 }
1028
1029 static struct snd_kcontrol_new snd_cmipci_spdif_default __devinitdata =
1030 {
1031         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1032         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1033         .info =         snd_cmipci_spdif_default_info,
1034         .get =          snd_cmipci_spdif_default_get,
1035         .put =          snd_cmipci_spdif_default_put
1036 };
1037
1038 static int snd_cmipci_spdif_mask_info(struct snd_kcontrol *kcontrol,
1039                                       struct snd_ctl_elem_info *uinfo)
1040 {
1041         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1042         uinfo->count = 1;
1043         return 0;
1044 }
1045
1046 static int snd_cmipci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1047                                      struct snd_ctl_elem_value *ucontrol)
1048 {
1049         ucontrol->value.iec958.status[0] = 0xff;
1050         ucontrol->value.iec958.status[1] = 0xff;
1051         ucontrol->value.iec958.status[2] = 0xff;
1052         ucontrol->value.iec958.status[3] = 0xff;
1053         return 0;
1054 }
1055
1056 static struct snd_kcontrol_new snd_cmipci_spdif_mask __devinitdata =
1057 {
1058         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
1059         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1060         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1061         .info =         snd_cmipci_spdif_mask_info,
1062         .get =          snd_cmipci_spdif_mask_get,
1063 };
1064
1065 static int snd_cmipci_spdif_stream_info(struct snd_kcontrol *kcontrol,
1066                                         struct snd_ctl_elem_info *uinfo)
1067 {
1068         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1069         uinfo->count = 1;
1070         return 0;
1071 }
1072
1073 static int snd_cmipci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1074                                        struct snd_ctl_elem_value *ucontrol)
1075 {
1076         struct cmipci *chip = snd_kcontrol_chip(kcontrol);
1077         int i;
1078
1079         spin_lock_irq(&chip->reg_lock);
1080         for (i = 0; i < 4; i++)
1081                 ucontrol->value.iec958.status[i] = (chip->dig_pcm_status >> (i * 8)) & 0xff;
1082         spin_unlock_irq(&chip->reg_lock);
1083         return 0;
1084 }
1085
1086 static int snd_cmipci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1087                                        struct snd_ctl_elem_value *ucontrol)
1088 {
1089         struct cmipci *chip = snd_kcontrol_chip(kcontrol);
1090         int i, change;
1091         unsigned int val;
1092
1093         val = 0;
1094         spin_lock_irq(&chip->reg_lock);
1095         for (i = 0; i < 4; i++)
1096                 val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
1097         change = val != chip->dig_pcm_status;
1098         chip->dig_pcm_status = val;
1099         spin_unlock_irq(&chip->reg_lock);
1100         return change;
1101 }
1102
1103 static struct snd_kcontrol_new snd_cmipci_spdif_stream __devinitdata =
1104 {
1105         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1106         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1107         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1108         .info =         snd_cmipci_spdif_stream_info,
1109         .get =          snd_cmipci_spdif_stream_get,
1110         .put =          snd_cmipci_spdif_stream_put
1111 };
1112
1113 /*
1114  */
1115
1116 /* save mixer setting and mute for AC3 playback */
1117 static int save_mixer_state(struct cmipci *cm)
1118 {
1119         if (! cm->mixer_insensitive) {
1120                 struct snd_ctl_elem_value *val;
1121                 unsigned int i;
1122
1123                 val = kmalloc(sizeof(*val), GFP_ATOMIC);
1124                 if (!val)
1125                         return -ENOMEM;
1126                 for (i = 0; i < CM_SAVED_MIXERS; i++) {
1127                         struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
1128                         if (ctl) {
1129                                 int event;
1130                                 memset(val, 0, sizeof(*val));
1131                                 ctl->get(ctl, val);
1132                                 cm->mixer_res_status[i] = val->value.integer.value[0];
1133                                 val->value.integer.value[0] = cm_saved_mixer[i].toggle_on;
1134                                 event = SNDRV_CTL_EVENT_MASK_INFO;
1135                                 if (cm->mixer_res_status[i] != val->value.integer.value[0]) {
1136                                         ctl->put(ctl, val); /* toggle */
1137                                         event |= SNDRV_CTL_EVENT_MASK_VALUE;
1138                                 }
1139                                 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1140                                 snd_ctl_notify(cm->card, event, &ctl->id);
1141                         }
1142                 }
1143                 kfree(val);
1144                 cm->mixer_insensitive = 1;
1145         }
1146         return 0;
1147 }
1148
1149
1150 /* restore the previously saved mixer status */
1151 static void restore_mixer_state(struct cmipci *cm)
1152 {
1153         if (cm->mixer_insensitive) {
1154                 struct snd_ctl_elem_value *val;
1155                 unsigned int i;
1156
1157                 val = kmalloc(sizeof(*val), GFP_KERNEL);
1158                 if (!val)
1159                         return;
1160                 cm->mixer_insensitive = 0; /* at first clear this;
1161                                               otherwise the changes will be ignored */
1162                 for (i = 0; i < CM_SAVED_MIXERS; i++) {
1163                         struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
1164                         if (ctl) {
1165                                 int event;
1166
1167                                 memset(val, 0, sizeof(*val));
1168                                 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1169                                 ctl->get(ctl, val);
1170                                 event = SNDRV_CTL_EVENT_MASK_INFO;
1171                                 if (val->value.integer.value[0] != cm->mixer_res_status[i]) {
1172                                         val->value.integer.value[0] = cm->mixer_res_status[i];
1173                                         ctl->put(ctl, val);
1174                                         event |= SNDRV_CTL_EVENT_MASK_VALUE;
1175                                 }
1176                                 snd_ctl_notify(cm->card, event, &ctl->id);
1177                         }
1178                 }
1179                 kfree(val);
1180         }
1181 }
1182
1183 /* spinlock held! */
1184 static void setup_ac3(struct cmipci *cm, struct snd_pcm_substream *subs, int do_ac3, int rate)
1185 {
1186         if (do_ac3) {
1187                 /* AC3EN for 037 */
1188                 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
1189                 /* AC3EN for 039 */
1190                 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
1191         
1192                 if (cm->can_ac3_hw) {
1193                         /* SPD24SEL for 037, 0x02 */
1194                         /* SPD24SEL for 039, 0x20, but cannot be set */
1195                         snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1196                         snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1197                 } else { /* can_ac3_sw */
1198                         /* SPD32SEL for 037 & 039, 0x20 */
1199                         snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1200                         /* set 176K sample rate to fix 033 HW bug */
1201                         if (cm->chip_version == 33) {
1202                                 if (rate >= 48000) {
1203                                         snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
1204                                 } else {
1205                                         snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
1206                                 }
1207                         }
1208                 }
1209
1210         } else {
1211                 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
1212                 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
1213
1214                 if (cm->can_ac3_hw) {
1215                         /* chip model >= 37 */
1216                         if (snd_pcm_format_width(subs->runtime->format) > 16) {
1217                                 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1218                                 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1219                         } else {
1220                                 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1221                                 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1222                         }
1223                 } else {
1224                         snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1225                         snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1226                         snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
1227                 }
1228         }
1229 }
1230
1231 static int setup_spdif_playback(struct cmipci *cm, struct snd_pcm_substream *subs, int up, int do_ac3)
1232 {
1233         int rate, err;
1234
1235         rate = subs->runtime->rate;
1236
1237         if (up && do_ac3)
1238                 if ((err = save_mixer_state(cm)) < 0)
1239                         return err;
1240
1241         spin_lock_irq(&cm->reg_lock);
1242         cm->spdif_playback_avail = up;
1243         if (up) {
1244                 /* they are controlled via "IEC958 Output Switch" */
1245                 /* snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
1246                 /* snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
1247                 if (cm->spdif_playback_enabled)
1248                         snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
1249                 setup_ac3(cm, subs, do_ac3, rate);
1250
1251                 if (rate == 48000 || rate == 96000)
1252                         snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
1253                 else
1254                         snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
1255                 if (rate > 48000)
1256                         snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1257                 else
1258                         snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1259         } else {
1260                 /* they are controlled via "IEC958 Output Switch" */
1261                 /* snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
1262                 /* snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
1263                 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1264                 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
1265                 setup_ac3(cm, subs, 0, 0);
1266         }
1267         spin_unlock_irq(&cm->reg_lock);
1268         return 0;
1269 }
1270
1271
1272 /*
1273  * preparation
1274  */
1275
1276 /* playback - enable spdif only on the certain condition */
1277 static int snd_cmipci_playback_prepare(struct snd_pcm_substream *substream)
1278 {
1279         struct cmipci *cm = snd_pcm_substream_chip(substream);
1280         int rate = substream->runtime->rate;
1281         int err, do_spdif, do_ac3 = 0;
1282
1283         do_spdif = (rate >= 44100 &&
1284                     substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE &&
1285                     substream->runtime->channels == 2);
1286         if (do_spdif && cm->can_ac3_hw) 
1287                 do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
1288         if ((err = setup_spdif_playback(cm, substream, do_spdif, do_ac3)) < 0)
1289                 return err;
1290         return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
1291 }
1292
1293 /* playback  (via device #2) - enable spdif always */
1294 static int snd_cmipci_playback_spdif_prepare(struct snd_pcm_substream *substream)
1295 {
1296         struct cmipci *cm = snd_pcm_substream_chip(substream);
1297         int err, do_ac3;
1298
1299         if (cm->can_ac3_hw) 
1300                 do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
1301         else
1302                 do_ac3 = 1; /* doesn't matter */
1303         if ((err = setup_spdif_playback(cm, substream, 1, do_ac3)) < 0)
1304                 return err;
1305         return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
1306 }
1307
1308 static int snd_cmipci_playback_hw_free(struct snd_pcm_substream *substream)
1309 {
1310         struct cmipci *cm = snd_pcm_substream_chip(substream);
1311         setup_spdif_playback(cm, substream, 0, 0);
1312         restore_mixer_state(cm);
1313         return snd_cmipci_hw_free(substream);
1314 }
1315
1316 /* capture */
1317 static int snd_cmipci_capture_prepare(struct snd_pcm_substream *substream)
1318 {
1319         struct cmipci *cm = snd_pcm_substream_chip(substream);
1320         return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
1321 }
1322
1323 /* capture with spdif (via device #2) */
1324 static int snd_cmipci_capture_spdif_prepare(struct snd_pcm_substream *substream)
1325 {
1326         struct cmipci *cm = snd_pcm_substream_chip(substream);
1327
1328         spin_lock_irq(&cm->reg_lock);
1329         snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
1330         spin_unlock_irq(&cm->reg_lock);
1331
1332         return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
1333 }
1334
1335 static int snd_cmipci_capture_spdif_hw_free(struct snd_pcm_substream *subs)
1336 {
1337         struct cmipci *cm = snd_pcm_substream_chip(subs);
1338
1339         spin_lock_irq(&cm->reg_lock);
1340         snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
1341         spin_unlock_irq(&cm->reg_lock);
1342
1343         return snd_cmipci_hw_free(subs);
1344 }
1345
1346
1347 /*
1348  * interrupt handler
1349  */
1350 static irqreturn_t snd_cmipci_interrupt(int irq, void *dev_id)
1351 {
1352         struct cmipci *cm = dev_id;
1353         unsigned int status, mask = 0;
1354         
1355         /* fastpath out, to ease interrupt sharing */
1356         status = snd_cmipci_read(cm, CM_REG_INT_STATUS);
1357         if (!(status & CM_INTR))
1358                 return IRQ_NONE;
1359
1360         /* acknowledge interrupt */
1361         spin_lock(&cm->reg_lock);
1362         if (status & CM_CHINT0)
1363                 mask |= CM_CH0_INT_EN;
1364         if (status & CM_CHINT1)
1365                 mask |= CM_CH1_INT_EN;
1366         snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, mask);
1367         snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, mask);
1368         spin_unlock(&cm->reg_lock);
1369
1370         if (cm->rmidi && (status & CM_UARTINT))
1371                 snd_mpu401_uart_interrupt(irq, cm->rmidi->private_data);
1372
1373         if (cm->pcm) {
1374                 if ((status & CM_CHINT0) && cm->channel[0].running)
1375                         snd_pcm_period_elapsed(cm->channel[0].substream);
1376                 if ((status & CM_CHINT1) && cm->channel[1].running)
1377                         snd_pcm_period_elapsed(cm->channel[1].substream);
1378         }
1379         return IRQ_HANDLED;
1380 }
1381
1382 /*
1383  * h/w infos
1384  */
1385
1386 /* playback on channel A */
1387 static struct snd_pcm_hardware snd_cmipci_playback =
1388 {
1389         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1390                                  SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1391                                  SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1392         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1393         .rates =                SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
1394         .rate_min =             5512,
1395         .rate_max =             48000,
1396         .channels_min =         1,
1397         .channels_max =         2,
1398         .buffer_bytes_max =     (128*1024),
1399         .period_bytes_min =     64,
1400         .period_bytes_max =     (128*1024),
1401         .periods_min =          2,
1402         .periods_max =          1024,
1403         .fifo_size =            0,
1404 };
1405
1406 /* capture on channel B */
1407 static struct snd_pcm_hardware snd_cmipci_capture =
1408 {
1409         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1410                                  SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1411                                  SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1412         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1413         .rates =                SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
1414         .rate_min =             5512,
1415         .rate_max =             48000,
1416         .channels_min =         1,
1417         .channels_max =         2,
1418         .buffer_bytes_max =     (128*1024),
1419         .period_bytes_min =     64,
1420         .period_bytes_max =     (128*1024),
1421         .periods_min =          2,
1422         .periods_max =          1024,
1423         .fifo_size =            0,
1424 };
1425
1426 /* playback on channel B - stereo 16bit only? */
1427 static struct snd_pcm_hardware snd_cmipci_playback2 =
1428 {
1429         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1430                                  SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1431                                  SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1432         .formats =              SNDRV_PCM_FMTBIT_S16_LE,
1433         .rates =                SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
1434         .rate_min =             5512,
1435         .rate_max =             48000,
1436         .channels_min =         2,
1437         .channels_max =         2,
1438         .buffer_bytes_max =     (128*1024),
1439         .period_bytes_min =     64,
1440         .period_bytes_max =     (128*1024),
1441         .periods_min =          2,
1442         .periods_max =          1024,
1443         .fifo_size =            0,
1444 };
1445
1446 /* spdif playback on channel A */
1447 static struct snd_pcm_hardware snd_cmipci_playback_spdif =
1448 {
1449         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1450                                  SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1451                                  SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1452         .formats =              SNDRV_PCM_FMTBIT_S16_LE,
1453         .rates =                SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
1454         .rate_min =             44100,
1455         .rate_max =             48000,
1456         .channels_min =         2,
1457         .channels_max =         2,
1458         .buffer_bytes_max =     (128*1024),
1459         .period_bytes_min =     64,
1460         .period_bytes_max =     (128*1024),
1461         .periods_min =          2,
1462         .periods_max =          1024,
1463         .fifo_size =            0,
1464 };
1465
1466 /* spdif playback on channel A (32bit, IEC958 subframes) */
1467 static struct snd_pcm_hardware snd_cmipci_playback_iec958_subframe =
1468 {
1469         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1470                                  SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1471                                  SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1472         .formats =              SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1473         .rates =                SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
1474         .rate_min =             44100,
1475         .rate_max =             48000,
1476         .channels_min =         2,
1477         .channels_max =         2,
1478         .buffer_bytes_max =     (128*1024),
1479         .period_bytes_min =     64,
1480         .period_bytes_max =     (128*1024),
1481         .periods_min =          2,
1482         .periods_max =          1024,
1483         .fifo_size =            0,
1484 };
1485
1486 /* spdif capture on channel B */
1487 static struct snd_pcm_hardware snd_cmipci_capture_spdif =
1488 {
1489         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1490                                  SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1491                                  SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1492         .formats =              SNDRV_PCM_FMTBIT_S16_LE,
1493         .rates =                SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
1494         .rate_min =             44100,
1495         .rate_max =             48000,
1496         .channels_min =         2,
1497         .channels_max =         2,
1498         .buffer_bytes_max =     (128*1024),
1499         .period_bytes_min =     64,
1500         .period_bytes_max =     (128*1024),
1501         .periods_min =          2,
1502         .periods_max =          1024,
1503         .fifo_size =            0,
1504 };
1505
1506 /*
1507  * check device open/close
1508  */
1509 static int open_device_check(struct cmipci *cm, int mode, struct snd_pcm_substream *subs)
1510 {
1511         int ch = mode & CM_OPEN_CH_MASK;
1512
1513         /* FIXME: a file should wait until the device becomes free
1514          * when it's opened on blocking mode.  however, since the current
1515          * pcm framework doesn't pass file pointer before actually opened,
1516          * we can't know whether blocking mode or not in open callback..
1517          */
1518         mutex_lock(&cm->open_mutex);
1519         if (cm->opened[ch]) {
1520                 mutex_unlock(&cm->open_mutex);
1521                 return -EBUSY;
1522         }
1523         cm->opened[ch] = mode;
1524         cm->channel[ch].substream = subs;
1525         if (! (mode & CM_OPEN_DAC)) {
1526                 /* disable dual DAC mode */
1527                 cm->channel[ch].is_dac = 0;
1528                 spin_lock_irq(&cm->reg_lock);
1529                 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
1530                 spin_unlock_irq(&cm->reg_lock);
1531         }
1532         mutex_unlock(&cm->open_mutex);
1533         return 0;
1534 }
1535
1536 static void close_device_check(struct cmipci *cm, int mode)
1537 {
1538         int ch = mode & CM_OPEN_CH_MASK;
1539
1540         mutex_lock(&cm->open_mutex);
1541         if (cm->opened[ch] == mode) {
1542                 if (cm->channel[ch].substream) {
1543                         snd_cmipci_ch_reset(cm, ch);
1544                         cm->channel[ch].running = 0;
1545                         cm->channel[ch].substream = NULL;
1546                 }
1547                 cm->opened[ch] = 0;
1548                 if (! cm->channel[ch].is_dac) {
1549                         /* enable dual DAC mode again */
1550                         cm->channel[ch].is_dac = 1;
1551                         spin_lock_irq(&cm->reg_lock);
1552                         snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
1553                         spin_unlock_irq(&cm->reg_lock);
1554                 }
1555         }
1556         mutex_unlock(&cm->open_mutex);
1557 }
1558
1559 /*
1560  */
1561
1562 static int snd_cmipci_playback_open(struct snd_pcm_substream *substream)
1563 {
1564         struct cmipci *cm = snd_pcm_substream_chip(substream);
1565         struct snd_pcm_runtime *runtime = substream->runtime;
1566         int err;
1567
1568         if ((err = open_device_check(cm, CM_OPEN_PLAYBACK, substream)) < 0)
1569                 return err;
1570         runtime->hw = snd_cmipci_playback;
1571         if (cm->chip_version == 68) {
1572                 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1573                                      SNDRV_PCM_RATE_96000;
1574                 runtime->hw.rate_max = 96000;
1575         }
1576         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
1577         cm->dig_pcm_status = cm->dig_status;
1578         return 0;
1579 }
1580
1581 static int snd_cmipci_capture_open(struct snd_pcm_substream *substream)
1582 {
1583         struct cmipci *cm = snd_pcm_substream_chip(substream);
1584         struct snd_pcm_runtime *runtime = substream->runtime;
1585         int err;
1586
1587         if ((err = open_device_check(cm, CM_OPEN_CAPTURE, substream)) < 0)
1588                 return err;
1589         runtime->hw = snd_cmipci_capture;
1590         if (cm->chip_version == 68) {   // 8768 only supports 44k/48k recording
1591                 runtime->hw.rate_min = 41000;
1592                 runtime->hw.rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000;
1593         }
1594         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
1595         return 0;
1596 }
1597
1598 static int snd_cmipci_playback2_open(struct snd_pcm_substream *substream)
1599 {
1600         struct cmipci *cm = snd_pcm_substream_chip(substream);
1601         struct snd_pcm_runtime *runtime = substream->runtime;
1602         int err;
1603
1604         if ((err = open_device_check(cm, CM_OPEN_PLAYBACK2, substream)) < 0) /* use channel B */
1605                 return err;
1606         runtime->hw = snd_cmipci_playback2;
1607         mutex_lock(&cm->open_mutex);
1608         if (! cm->opened[CM_CH_PLAY]) {
1609                 if (cm->can_multi_ch) {
1610                         runtime->hw.channels_max = cm->max_channels;
1611                         if (cm->max_channels == 4)
1612                                 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_4);
1613                         else if (cm->max_channels == 6)
1614                                 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_6);
1615                         else if (cm->max_channels == 8)
1616                                 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_8);
1617                 }
1618         }
1619         mutex_unlock(&cm->open_mutex);
1620         if (cm->chip_version == 68) {
1621                 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1622                                      SNDRV_PCM_RATE_96000;
1623                 runtime->hw.rate_max = 96000;
1624         }
1625         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
1626         return 0;
1627 }
1628
1629 static int snd_cmipci_playback_spdif_open(struct snd_pcm_substream *substream)
1630 {
1631         struct cmipci *cm = snd_pcm_substream_chip(substream);
1632         struct snd_pcm_runtime *runtime = substream->runtime;
1633         int err;
1634
1635         if ((err = open_device_check(cm, CM_OPEN_SPDIF_PLAYBACK, substream)) < 0) /* use channel A */
1636                 return err;
1637         if (cm->can_ac3_hw) {
1638                 runtime->hw = snd_cmipci_playback_spdif;
1639                 if (cm->chip_version >= 37) {
1640                         runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
1641                         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1642                 }
1643                 if (cm->chip_version == 68) {
1644                         runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1645                                              SNDRV_PCM_RATE_96000;
1646                         runtime->hw.rate_max = 96000;
1647                 }
1648         } else {
1649                 runtime->hw = snd_cmipci_playback_iec958_subframe;
1650         }
1651         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
1652         cm->dig_pcm_status = cm->dig_status;
1653         return 0;
1654 }
1655
1656 static int snd_cmipci_capture_spdif_open(struct snd_pcm_substream *substream)
1657 {
1658         struct cmipci *cm = snd_pcm_substream_chip(substream);
1659         struct snd_pcm_runtime *runtime = substream->runtime;
1660         int err;
1661
1662         if ((err = open_device_check(cm, CM_OPEN_SPDIF_CAPTURE, substream)) < 0) /* use channel B */
1663                 return err;
1664         runtime->hw = snd_cmipci_capture_spdif;
1665         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
1666         return 0;
1667 }
1668
1669
1670 /*
1671  */
1672
1673 static int snd_cmipci_playback_close(struct snd_pcm_substream *substream)
1674 {
1675         struct cmipci *cm = snd_pcm_substream_chip(substream);
1676         close_device_check(cm, CM_OPEN_PLAYBACK);
1677         return 0;
1678 }
1679
1680 static int snd_cmipci_capture_close(struct snd_pcm_substream *substream)
1681 {
1682         struct cmipci *cm = snd_pcm_substream_chip(substream);
1683         close_device_check(cm, CM_OPEN_CAPTURE);
1684         return 0;
1685 }
1686
1687 static int snd_cmipci_playback2_close(struct snd_pcm_substream *substream)
1688 {
1689         struct cmipci *cm = snd_pcm_substream_chip(substream);
1690         close_device_check(cm, CM_OPEN_PLAYBACK2);
1691         close_device_check(cm, CM_OPEN_PLAYBACK_MULTI);
1692         return 0;
1693 }
1694
1695 static int snd_cmipci_playback_spdif_close(struct snd_pcm_substream *substream)
1696 {
1697         struct cmipci *cm = snd_pcm_substream_chip(substream);
1698         close_device_check(cm, CM_OPEN_SPDIF_PLAYBACK);
1699         return 0;
1700 }
1701
1702 static int snd_cmipci_capture_spdif_close(struct snd_pcm_substream *substream)
1703 {
1704         struct cmipci *cm = snd_pcm_substream_chip(substream);
1705         close_device_check(cm, CM_OPEN_SPDIF_CAPTURE);
1706         return 0;
1707 }
1708
1709
1710 /*
1711  */
1712
1713 static struct snd_pcm_ops snd_cmipci_playback_ops = {
1714         .open =         snd_cmipci_playback_open,
1715         .close =        snd_cmipci_playback_close,
1716         .ioctl =        snd_pcm_lib_ioctl,
1717         .hw_params =    snd_cmipci_hw_params,
1718         .hw_free =      snd_cmipci_playback_hw_free,
1719         .prepare =      snd_cmipci_playback_prepare,
1720         .trigger =      snd_cmipci_playback_trigger,
1721         .pointer =      snd_cmipci_playback_pointer,
1722 };
1723
1724 static struct snd_pcm_ops snd_cmipci_capture_ops = {
1725         .open =         snd_cmipci_capture_open,
1726         .close =        snd_cmipci_capture_close,
1727         .ioctl =        snd_pcm_lib_ioctl,
1728         .hw_params =    snd_cmipci_hw_params,
1729         .hw_free =      snd_cmipci_hw_free,
1730         .prepare =      snd_cmipci_capture_prepare,
1731         .trigger =      snd_cmipci_capture_trigger,
1732         .pointer =      snd_cmipci_capture_pointer,
1733 };
1734
1735 static struct snd_pcm_ops snd_cmipci_playback2_ops = {
1736         .open =         snd_cmipci_playback2_open,
1737         .close =        snd_cmipci_playback2_close,
1738         .ioctl =        snd_pcm_lib_ioctl,
1739         .hw_params =    snd_cmipci_playback2_hw_params,
1740         .hw_free =      snd_cmipci_hw_free,
1741         .prepare =      snd_cmipci_capture_prepare,     /* channel B */
1742         .trigger =      snd_cmipci_capture_trigger,     /* channel B */
1743         .pointer =      snd_cmipci_capture_pointer,     /* channel B */
1744 };
1745
1746 static struct snd_pcm_ops snd_cmipci_playback_spdif_ops = {
1747         .open =         snd_cmipci_playback_spdif_open,
1748         .close =        snd_cmipci_playback_spdif_close,
1749         .ioctl =        snd_pcm_lib_ioctl,
1750         .hw_params =    snd_cmipci_hw_params,
1751         .hw_free =      snd_cmipci_playback_hw_free,
1752         .prepare =      snd_cmipci_playback_spdif_prepare,      /* set up rate */
1753         .trigger =      snd_cmipci_playback_trigger,
1754         .pointer =      snd_cmipci_playback_pointer,
1755 };
1756
1757 static struct snd_pcm_ops snd_cmipci_capture_spdif_ops = {
1758         .open =         snd_cmipci_capture_spdif_open,
1759         .close =        snd_cmipci_capture_spdif_close,
1760         .ioctl =        snd_pcm_lib_ioctl,
1761         .hw_params =    snd_cmipci_hw_params,
1762         .hw_free =      snd_cmipci_capture_spdif_hw_free,
1763         .prepare =      snd_cmipci_capture_spdif_prepare,
1764         .trigger =      snd_cmipci_capture_trigger,
1765         .pointer =      snd_cmipci_capture_pointer,
1766 };
1767
1768
1769 /*
1770  */
1771
1772 static int __devinit snd_cmipci_pcm_new(struct cmipci *cm, int device)
1773 {
1774         struct snd_pcm *pcm;
1775         int err;
1776
1777         err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
1778         if (err < 0)
1779                 return err;
1780
1781         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_ops);
1782         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_ops);
1783
1784         pcm->private_data = cm;
1785         pcm->info_flags = 0;
1786         strcpy(pcm->name, "C-Media PCI DAC/ADC");
1787         cm->pcm = pcm;
1788
1789         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1790                                               snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
1791
1792         return 0;
1793 }
1794
1795 static int __devinit snd_cmipci_pcm2_new(struct cmipci *cm, int device)
1796 {
1797         struct snd_pcm *pcm;
1798         int err;
1799
1800         err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 0, &pcm);
1801         if (err < 0)
1802                 return err;
1803
1804         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback2_ops);
1805
1806         pcm->private_data = cm;
1807         pcm->info_flags = 0;
1808         strcpy(pcm->name, "C-Media PCI 2nd DAC");
1809         cm->pcm2 = pcm;
1810
1811         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1812                                               snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
1813
1814         return 0;
1815 }
1816
1817 static int __devinit snd_cmipci_pcm_spdif_new(struct cmipci *cm, int device)
1818 {
1819         struct snd_pcm *pcm;
1820         int err;
1821
1822         err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
1823         if (err < 0)
1824                 return err;
1825
1826         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_spdif_ops);
1827         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_spdif_ops);
1828
1829         pcm->private_data = cm;
1830         pcm->info_flags = 0;
1831         strcpy(pcm->name, "C-Media PCI IEC958");
1832         cm->pcm_spdif = pcm;
1833
1834         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1835                                               snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
1836
1837         return 0;
1838 }
1839
1840 /*
1841  * mixer interface:
1842  * - CM8338/8738 has a compatible mixer interface with SB16, but
1843  *   lack of some elements like tone control, i/o gain and AGC.
1844  * - Access to native registers:
1845  *   - A 3D switch
1846  *   - Output mute switches
1847  */
1848
1849 static void snd_cmipci_mixer_write(struct cmipci *s, unsigned char idx, unsigned char data)
1850 {
1851         outb(idx, s->iobase + CM_REG_SB16_ADDR);
1852         outb(data, s->iobase + CM_REG_SB16_DATA);
1853 }
1854
1855 static unsigned char snd_cmipci_mixer_read(struct cmipci *s, unsigned char idx)
1856 {
1857         unsigned char v;
1858
1859         outb(idx, s->iobase + CM_REG_SB16_ADDR);
1860         v = inb(s->iobase + CM_REG_SB16_DATA);
1861         return v;
1862 }
1863
1864 /*
1865  * general mixer element
1866  */
1867 struct cmipci_sb_reg {
1868         unsigned int left_reg, right_reg;
1869         unsigned int left_shift, right_shift;
1870         unsigned int mask;
1871         unsigned int invert: 1;
1872         unsigned int stereo: 1;
1873 };
1874
1875 #define COMPOSE_SB_REG(lreg,rreg,lshift,rshift,mask,invert,stereo) \
1876  ((lreg) | ((rreg) << 8) | (lshift << 16) | (rshift << 19) | (mask << 24) | (invert << 22) | (stereo << 23))
1877
1878 #define CMIPCI_DOUBLE(xname, left_reg, right_reg, left_shift, right_shift, mask, invert, stereo) \
1879 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1880   .info = snd_cmipci_info_volume, \
1881   .get = snd_cmipci_get_volume, .put = snd_cmipci_put_volume, \
1882   .private_value = COMPOSE_SB_REG(left_reg, right_reg, left_shift, right_shift, mask, invert, stereo), \
1883 }
1884
1885 #define CMIPCI_SB_VOL_STEREO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg+1, shift, shift, mask, 0, 1)
1886 #define CMIPCI_SB_VOL_MONO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg, shift, shift, mask, 0, 0)
1887 #define CMIPCI_SB_SW_STEREO(xname,lshift,rshift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, lshift, rshift, 1, 0, 1)
1888 #define CMIPCI_SB_SW_MONO(xname,shift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, shift, shift, 1, 0, 0)
1889
1890 static void cmipci_sb_reg_decode(struct cmipci_sb_reg *r, unsigned long val)
1891 {
1892         r->left_reg = val & 0xff;
1893         r->right_reg = (val >> 8) & 0xff;
1894         r->left_shift = (val >> 16) & 0x07;
1895         r->right_shift = (val >> 19) & 0x07;
1896         r->invert = (val >> 22) & 1;
1897         r->stereo = (val >> 23) & 1;
1898         r->mask = (val >> 24) & 0xff;
1899 }
1900
1901 static int snd_cmipci_info_volume(struct snd_kcontrol *kcontrol,
1902                                   struct snd_ctl_elem_info *uinfo)
1903 {
1904         struct cmipci_sb_reg reg;
1905
1906         cmipci_sb_reg_decode(&reg, kcontrol->private_value);
1907         uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1908         uinfo->count = reg.stereo + 1;
1909         uinfo->value.integer.min = 0;
1910         uinfo->value.integer.max = reg.mask;
1911         return 0;
1912 }
1913  
1914 static int snd_cmipci_get_volume(struct snd_kcontrol *kcontrol,
1915                                  struct snd_ctl_elem_value *ucontrol)
1916 {
1917         struct cmipci *cm = snd_kcontrol_chip(kcontrol);
1918         struct cmipci_sb_reg reg;
1919         int val;
1920
1921         cmipci_sb_reg_decode(&reg, kcontrol->private_value);
1922         spin_lock_irq(&cm->reg_lock);
1923         val = (snd_cmipci_mixer_read(cm, reg.left_reg) >> reg.left_shift) & reg.mask;
1924         if (reg.invert)
1925                 val = reg.mask - val;
1926         ucontrol->value.integer.value[0] = val;
1927         if (reg.stereo) {
1928                 val = (snd_cmipci_mixer_read(cm, reg.right_reg) >> reg.right_shift) & reg.mask;
1929                 if (reg.invert)
1930                         val = reg.mask - val;
1931                  ucontrol->value.integer.value[1] = val;
1932         }
1933         spin_unlock_irq(&cm->reg_lock);
1934         return 0;
1935 }
1936
1937 static int snd_cmipci_put_volume(struct snd_kcontrol *kcontrol,
1938                                  struct snd_ctl_elem_value *ucontrol)
1939 {
1940         struct cmipci *cm = snd_kcontrol_chip(kcontrol);
1941         struct cmipci_sb_reg reg;
1942         int change;
1943         int left, right, oleft, oright;
1944
1945         cmipci_sb_reg_decode(&reg, kcontrol->private_value);
1946         left = ucontrol->value.integer.value[0] & reg.mask;
1947         if (reg.invert)
1948                 left = reg.mask - left;
1949         left <<= reg.left_shift;
1950         if (reg.stereo) {
1951                 right = ucontrol->value.integer.value[1] & reg.mask;
1952                 if (reg.invert)
1953                         right = reg.mask - right;
1954                 right <<= reg.right_shift;
1955         } else
1956                 right = 0;
1957         spin_lock_irq(&cm->reg_lock);
1958         oleft = snd_cmipci_mixer_read(cm, reg.left_reg);
1959         left |= oleft & ~(reg.mask << reg.left_shift);
1960         change = left != oleft;
1961         if (reg.stereo) {
1962                 if (reg.left_reg != reg.right_reg) {
1963                         snd_cmipci_mixer_write(cm, reg.left_reg, left);
1964                         oright = snd_cmipci_mixer_read(cm, reg.right_reg);
1965                 } else
1966                         oright = left;
1967                 right |= oright & ~(reg.mask << reg.right_shift);
1968                 change |= right != oright;
1969                 snd_cmipci_mixer_write(cm, reg.right_reg, right);
1970         } else
1971                 snd_cmipci_mixer_write(cm, reg.left_reg, left);
1972         spin_unlock_irq(&cm->reg_lock);
1973         return change;
1974 }
1975
1976 /*
1977  * input route (left,right) -> (left,right)
1978  */
1979 #define CMIPCI_SB_INPUT_SW(xname, left_shift, right_shift) \
1980 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1981   .info = snd_cmipci_info_input_sw, \
1982   .get = snd_cmipci_get_input_sw, .put = snd_cmipci_put_input_sw, \
1983   .private_value = COMPOSE_SB_REG(SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, left_shift, right_shift, 1, 0, 1), \
1984 }
1985
1986 static int snd_cmipci_info_input_sw(struct snd_kcontrol *kcontrol,
1987                                     struct snd_ctl_elem_info *uinfo)
1988 {
1989         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1990         uinfo->count = 4;
1991         uinfo->value.integer.min = 0;
1992         uinfo->value.integer.max = 1;
1993         return 0;
1994 }
1995  
1996 static int snd_cmipci_get_input_sw(struct snd_kcontrol *kcontrol,
1997                                    struct snd_ctl_elem_value *ucontrol)
1998 {
1999         struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2000         struct cmipci_sb_reg reg;
2001         int val1, val2;
2002
2003         cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2004         spin_lock_irq(&cm->reg_lock);
2005         val1 = snd_cmipci_mixer_read(cm, reg.left_reg);
2006         val2 = snd_cmipci_mixer_read(cm, reg.right_reg);
2007         spin_unlock_irq(&cm->reg_lock);
2008         ucontrol->value.integer.value[0] = (val1 >> reg.left_shift) & 1;
2009         ucontrol->value.integer.value[1] = (val2 >> reg.left_shift) & 1;
2010         ucontrol->value.integer.value[2] = (val1 >> reg.right_shift) & 1;
2011         ucontrol->value.integer.value[3] = (val2 >> reg.right_shift) & 1;
2012         return 0;
2013 }
2014
2015 static int snd_cmipci_put_input_sw(struct snd_kcontrol *kcontrol,
2016                                    struct snd_ctl_elem_value *ucontrol)
2017 {
2018         struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2019         struct cmipci_sb_reg reg;
2020         int change;
2021         int val1, val2, oval1, oval2;
2022
2023         cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2024         spin_lock_irq(&cm->reg_lock);
2025         oval1 = snd_cmipci_mixer_read(cm, reg.left_reg);
2026         oval2 = snd_cmipci_mixer_read(cm, reg.right_reg);
2027         val1 = oval1 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
2028         val2 = oval2 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
2029         val1 |= (ucontrol->value.integer.value[0] & 1) << reg.left_shift;
2030         val2 |= (ucontrol->value.integer.value[1] & 1) << reg.left_shift;
2031         val1 |= (ucontrol->value.integer.value[2] & 1) << reg.right_shift;
2032         val2 |= (ucontrol->value.integer.value[3] & 1) << reg.right_shift;
2033         change = val1 != oval1 || val2 != oval2;
2034         snd_cmipci_mixer_write(cm, reg.left_reg, val1);
2035         snd_cmipci_mixer_write(cm, reg.right_reg, val2);
2036         spin_unlock_irq(&cm->reg_lock);
2037         return change;
2038 }
2039
2040 /*
2041  * native mixer switches/volumes
2042  */
2043
2044 #define CMIPCI_MIXER_SW_STEREO(xname, reg, lshift, rshift, invert) \
2045 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2046   .info = snd_cmipci_info_native_mixer, \
2047   .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2048   .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, 1, invert, 1), \
2049 }
2050
2051 #define CMIPCI_MIXER_SW_MONO(xname, reg, shift, invert) \
2052 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2053   .info = snd_cmipci_info_native_mixer, \
2054   .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2055   .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, 1, invert, 0), \
2056 }
2057
2058 #define CMIPCI_MIXER_VOL_STEREO(xname, reg, lshift, rshift, mask) \
2059 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2060   .info = snd_cmipci_info_native_mixer, \
2061   .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2062   .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, mask, 0, 1), \
2063 }
2064
2065 #define CMIPCI_MIXER_VOL_MONO(xname, reg, shift, mask) \
2066 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2067   .info = snd_cmipci_info_native_mixer, \
2068   .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2069   .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, mask, 0, 0), \
2070 }
2071
2072 static int snd_cmipci_info_native_mixer(struct snd_kcontrol *kcontrol,
2073                                         struct snd_ctl_elem_info *uinfo)
2074 {
2075         struct cmipci_sb_reg reg;
2076
2077         cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2078         uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
2079         uinfo->count = reg.stereo + 1;
2080         uinfo->value.integer.min = 0;
2081         uinfo->value.integer.max = reg.mask;
2082         return 0;
2083
2084 }
2085
2086 static int snd_cmipci_get_native_mixer(struct snd_kcontrol *kcontrol,
2087                                        struct snd_ctl_elem_value *ucontrol)
2088 {
2089         struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2090         struct cmipci_sb_reg reg;
2091         unsigned char oreg, val;
2092
2093         cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2094         spin_lock_irq(&cm->reg_lock);
2095         oreg = inb(cm->iobase + reg.left_reg);
2096         val = (oreg >> reg.left_shift) & reg.mask;
2097         if (reg.invert)
2098                 val = reg.mask - val;
2099         ucontrol->value.integer.value[0] = val;
2100         if (reg.stereo) {
2101                 val = (oreg >> reg.right_shift) & reg.mask;
2102                 if (reg.invert)
2103                         val = reg.mask - val;
2104                 ucontrol->value.integer.value[1] = val;
2105         }
2106         spin_unlock_irq(&cm->reg_lock);
2107         return 0;
2108 }
2109
2110 static int snd_cmipci_put_native_mixer(struct snd_kcontrol *kcontrol,
2111                                        struct snd_ctl_elem_value *ucontrol)
2112 {
2113         struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2114         struct cmipci_sb_reg reg;
2115         unsigned char oreg, nreg, val;
2116
2117         cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2118         spin_lock_irq(&cm->reg_lock);
2119         oreg = inb(cm->iobase + reg.left_reg);
2120         val = ucontrol->value.integer.value[0] & reg.mask;
2121         if (reg.invert)
2122                 val = reg.mask - val;
2123         nreg = oreg & ~(reg.mask << reg.left_shift);
2124         nreg |= (val << reg.left_shift);
2125         if (reg.stereo) {
2126                 val = ucontrol->value.integer.value[1] & reg.mask;
2127                 if (reg.invert)
2128                         val = reg.mask - val;
2129                 nreg &= ~(reg.mask << reg.right_shift);
2130                 nreg |= (val << reg.right_shift);
2131         }
2132         outb(nreg, cm->iobase + reg.left_reg);
2133         spin_unlock_irq(&cm->reg_lock);
2134         return (nreg != oreg);
2135 }
2136
2137 /*
2138  * special case - check mixer sensitivity
2139  */
2140 static int snd_cmipci_get_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
2141                                                  struct snd_ctl_elem_value *ucontrol)
2142 {
2143         //struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2144         return snd_cmipci_get_native_mixer(kcontrol, ucontrol);
2145 }
2146
2147 static int snd_cmipci_put_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
2148                                                  struct snd_ctl_elem_value *ucontrol)
2149 {
2150         struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2151         if (cm->mixer_insensitive) {
2152                 /* ignored */
2153                 return 0;
2154         }
2155         return snd_cmipci_put_native_mixer(kcontrol, ucontrol);
2156 }
2157
2158
2159 static struct snd_kcontrol_new snd_cmipci_mixers[] __devinitdata = {
2160         CMIPCI_SB_VOL_STEREO("Master Playback Volume", SB_DSP4_MASTER_DEV, 3, 31),
2161         CMIPCI_MIXER_SW_MONO("3D Control - Switch", CM_REG_MIXER1, CM_X3DEN_SHIFT, 0),
2162         CMIPCI_SB_VOL_STEREO("PCM Playback Volume", SB_DSP4_PCM_DEV, 3, 31),
2163         //CMIPCI_MIXER_SW_MONO("PCM Playback Switch", CM_REG_MIXER1, CM_WSMUTE_SHIFT, 1),
2164         { /* switch with sensitivity */
2165                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2166                 .name = "PCM Playback Switch",
2167                 .info = snd_cmipci_info_native_mixer,
2168                 .get = snd_cmipci_get_native_mixer_sensitive,
2169                 .put = snd_cmipci_put_native_mixer_sensitive,
2170                 .private_value = COMPOSE_SB_REG(CM_REG_MIXER1, CM_REG_MIXER1, CM_WSMUTE_SHIFT, CM_WSMUTE_SHIFT, 1, 1, 0),
2171         },
2172         CMIPCI_MIXER_SW_STEREO("PCM Capture Switch", CM_REG_MIXER1, CM_WAVEINL_SHIFT, CM_WAVEINR_SHIFT, 0),
2173         CMIPCI_SB_VOL_STEREO("Synth Playback Volume", SB_DSP4_SYNTH_DEV, 3, 31),
2174         CMIPCI_MIXER_SW_MONO("Synth Playback Switch", CM_REG_MIXER1, CM_FMMUTE_SHIFT, 1),
2175         CMIPCI_SB_INPUT_SW("Synth Capture Route", 6, 5),
2176         CMIPCI_SB_VOL_STEREO("CD Playback Volume", SB_DSP4_CD_DEV, 3, 31),
2177         CMIPCI_SB_SW_STEREO("CD Playback Switch", 2, 1),
2178         CMIPCI_SB_INPUT_SW("CD Capture Route", 2, 1),
2179         CMIPCI_SB_VOL_STEREO("Line Playback Volume", SB_DSP4_LINE_DEV, 3, 31),
2180         CMIPCI_SB_SW_STEREO("Line Playback Switch", 4, 3),
2181         CMIPCI_SB_INPUT_SW("Line Capture Route", 4, 3),
2182         CMIPCI_SB_VOL_MONO("Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
2183         CMIPCI_SB_SW_MONO("Mic Playback Switch", 0),
2184         CMIPCI_DOUBLE("Mic Capture Switch", SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, 0, 0, 1, 0, 0),
2185         CMIPCI_SB_VOL_MONO("PC Speaker Playback Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
2186         CMIPCI_MIXER_VOL_STEREO("Aux Playback Volume", CM_REG_AUX_VOL, 4, 0, 15),
2187         CMIPCI_MIXER_SW_STEREO("Aux Playback Switch", CM_REG_MIXER2, CM_VAUXLM_SHIFT, CM_VAUXRM_SHIFT, 0),
2188         CMIPCI_MIXER_SW_STEREO("Aux Capture Switch", CM_REG_MIXER2, CM_RAUXLEN_SHIFT, CM_RAUXREN_SHIFT, 0),
2189         CMIPCI_MIXER_SW_MONO("Mic Boost Playback Switch", CM_REG_MIXER2, CM_MICGAINZ_SHIFT, 1),
2190         CMIPCI_MIXER_VOL_MONO("Mic Capture Volume", CM_REG_MIXER2, CM_VADMIC_SHIFT, 7),
2191         CMIPCI_SB_VOL_MONO("Phone Playback Volume", CM_REG_EXTENT_IND, 5, 7),
2192         CMIPCI_DOUBLE("Phone Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 4, 4, 1, 0, 0),
2193         CMIPCI_DOUBLE("PC Speaker Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 3, 3, 1, 0, 0),
2194         CMIPCI_DOUBLE("Mic Boost Capture Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 0, 0, 1, 0, 0),
2195 };
2196
2197 /*
2198  * other switches
2199  */
2200
2201 struct cmipci_switch_args {
2202         int reg;                /* register index */
2203         unsigned int mask;      /* mask bits */
2204         unsigned int mask_on;   /* mask bits to turn on */
2205         unsigned int is_byte: 1;                /* byte access? */
2206         unsigned int ac3_sensitive: 1;  /* access forbidden during
2207                                          * non-audio operation?
2208                                          */
2209 };
2210
2211 #define snd_cmipci_uswitch_info         snd_ctl_boolean_mono_info
2212
2213 static int _snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
2214                                    struct snd_ctl_elem_value *ucontrol,
2215                                    struct cmipci_switch_args *args)
2216 {
2217         unsigned int val;
2218         struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2219
2220         spin_lock_irq(&cm->reg_lock);
2221         if (args->ac3_sensitive && cm->mixer_insensitive) {
2222                 ucontrol->value.integer.value[0] = 0;
2223                 spin_unlock_irq(&cm->reg_lock);
2224                 return 0;
2225         }
2226         if (args->is_byte)
2227                 val = inb(cm->iobase + args->reg);
2228         else
2229                 val = snd_cmipci_read(cm, args->reg);
2230         ucontrol->value.integer.value[0] = ((val & args->mask) == args->mask_on) ? 1 : 0;
2231         spin_unlock_irq(&cm->reg_lock);
2232         return 0;
2233 }
2234
2235 static int snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
2236                                   struct snd_ctl_elem_value *ucontrol)
2237 {
2238         struct cmipci_switch_args *args;
2239         args = (struct cmipci_switch_args *)kcontrol->private_value;
2240         snd_assert(args != NULL, return -EINVAL);
2241         return _snd_cmipci_uswitch_get(kcontrol, ucontrol, args);
2242 }
2243
2244 static int _snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
2245                                    struct snd_ctl_elem_value *ucontrol,
2246                                    struct cmipci_switch_args *args)
2247 {
2248         unsigned int val;
2249         int change;
2250         struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2251
2252         spin_lock_irq(&cm->reg_lock);
2253         if (args->ac3_sensitive && cm->mixer_insensitive) {
2254                 /* ignored */
2255                 spin_unlock_irq(&cm->reg_lock);
2256                 return 0;
2257         }
2258         if (args->is_byte)
2259                 val = inb(cm->iobase + args->reg);
2260         else
2261                 val = snd_cmipci_read(cm, args->reg);
2262         change = (val & args->mask) != (ucontrol->value.integer.value[0] ? 
2263                         args->mask_on : (args->mask & ~args->mask_on));
2264         if (change) {
2265                 val &= ~args->mask;
2266                 if (ucontrol->value.integer.value[0])
2267                         val |= args->mask_on;
2268                 else
2269                         val |= (args->mask & ~args->mask_on);
2270                 if (args->is_byte)
2271                         outb((unsigned char)val, cm->iobase + args->reg);
2272                 else
2273                         snd_cmipci_write(cm, args->reg, val);
2274         }
2275         spin_unlock_irq(&cm->reg_lock);
2276         return change;
2277 }
2278
2279 static int snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
2280                                   struct snd_ctl_elem_value *ucontrol)
2281 {
2282         struct cmipci_switch_args *args;
2283         args = (struct cmipci_switch_args *)kcontrol->private_value;
2284         snd_assert(args != NULL, return -EINVAL);
2285         return _snd_cmipci_uswitch_put(kcontrol, ucontrol, args);
2286 }
2287
2288 #define DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask_on, xis_byte, xac3) \
2289 static struct cmipci_switch_args cmipci_switch_arg_##sname = { \
2290   .reg = xreg, \
2291   .mask = xmask, \
2292   .mask_on = xmask_on, \
2293   .is_byte = xis_byte, \
2294   .ac3_sensitive = xac3, \
2295 }
2296         
2297 #define DEFINE_BIT_SWITCH_ARG(sname, xreg, xmask, xis_byte, xac3) \
2298         DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask, xis_byte, xac3)
2299
2300 #if 0 /* these will be controlled in pcm device */
2301 DEFINE_BIT_SWITCH_ARG(spdif_in, CM_REG_FUNCTRL1, CM_SPDF_1, 0, 0);
2302 DEFINE_BIT_SWITCH_ARG(spdif_out, CM_REG_FUNCTRL1, CM_SPDF_0, 0, 0);
2303 #endif
2304 DEFINE_BIT_SWITCH_ARG(spdif_in_sel1, CM_REG_CHFORMAT, CM_SPDIF_SELECT1, 0, 0);
2305 DEFINE_BIT_SWITCH_ARG(spdif_in_sel2, CM_REG_MISC_CTRL, CM_SPDIF_SELECT2, 0, 0);
2306 DEFINE_BIT_SWITCH_ARG(spdif_enable, CM_REG_LEGACY_CTRL, CM_ENSPDOUT, 0, 0);
2307 DEFINE_BIT_SWITCH_ARG(spdo2dac, CM_REG_FUNCTRL1, CM_SPDO2DAC, 0, 1);
2308 DEFINE_BIT_SWITCH_ARG(spdi_valid, CM_REG_MISC, CM_SPDVALID, 1, 0);
2309 DEFINE_BIT_SWITCH_ARG(spdif_copyright, CM_REG_LEGACY_CTRL, CM_SPDCOPYRHT, 0, 0);
2310 DEFINE_BIT_SWITCH_ARG(spdif_dac_out, CM_REG_LEGACY_CTRL, CM_DAC2SPDO, 0, 1);
2311 DEFINE_SWITCH_ARG(spdo_5v, CM_REG_MISC_CTRL, CM_SPDO5V, 0, 0, 0); /* inverse: 0 = 5V */
2312 // DEFINE_BIT_SWITCH_ARG(spdo_48k, CM_REG_MISC_CTRL, CM_SPDF_AC97|CM_SPDIF48K, 0, 1);
2313 DEFINE_BIT_SWITCH_ARG(spdif_loop, CM_REG_FUNCTRL1, CM_SPDFLOOP, 0, 1);
2314 DEFINE_BIT_SWITCH_ARG(spdi_monitor, CM_REG_MIXER1, CM_CDPLAY, 1, 0);
2315 /* DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_CHFORMAT, CM_SPDIF_INVERSE, 0, 0); */
2316 DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_MISC, CM_SPDIF_INVERSE, 1, 0);
2317 DEFINE_BIT_SWITCH_ARG(spdi_phase2, CM_REG_CHFORMAT, CM_SPDIF_INVERSE2, 0, 0);
2318 #if CM_CH_PLAY == 1
2319 DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, 0, 0, 0); /* reversed */
2320 #else
2321 DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, CM_XCHGDAC, 0, 0);
2322 #endif
2323 DEFINE_BIT_SWITCH_ARG(fourch, CM_REG_MISC_CTRL, CM_N4SPK3D, 0, 0);
2324 // DEFINE_BIT_SWITCH_ARG(line_rear, CM_REG_MIXER1, CM_REAR2LIN, 1, 0);
2325 // DEFINE_BIT_SWITCH_ARG(line_bass, CM_REG_LEGACY_CTRL, CM_CENTR2LIN|CM_BASE2LIN, 0, 0);
2326 // DEFINE_BIT_SWITCH_ARG(joystick, CM_REG_FUNCTRL1, CM_JYSTK_EN, 0, 0); /* now module option */
2327 DEFINE_SWITCH_ARG(modem, CM_REG_MISC_CTRL, CM_FLINKON|CM_FLINKOFF, CM_FLINKON, 0, 0);
2328
2329 #define DEFINE_SWITCH(sname, stype, sarg) \
2330 { .name = sname, \
2331   .iface = stype, \
2332   .info = snd_cmipci_uswitch_info, \
2333   .get = snd_cmipci_uswitch_get, \
2334   .put = snd_cmipci_uswitch_put, \
2335   .private_value = (unsigned long)&cmipci_switch_arg_##sarg,\
2336 }
2337
2338 #define DEFINE_CARD_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_CARD, sarg)
2339 #define DEFINE_MIXER_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_MIXER, sarg)
2340
2341
2342 /*
2343  * callbacks for spdif output switch
2344  * needs toggle two registers..
2345  */
2346 static int snd_cmipci_spdout_enable_get(struct snd_kcontrol *kcontrol,
2347                                         struct snd_ctl_elem_value *ucontrol)
2348 {
2349         int changed;
2350         changed = _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
2351         changed |= _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
2352         return changed;
2353 }
2354
2355 static int snd_cmipci_spdout_enable_put(struct snd_kcontrol *kcontrol,
2356                                         struct snd_ctl_elem_value *ucontrol)
2357 {
2358         struct cmipci *chip = snd_kcontrol_chip(kcontrol);
2359         int changed;
2360         changed = _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
2361         changed |= _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
2362         if (changed) {
2363                 if (ucontrol->value.integer.value[0]) {
2364                         if (chip->spdif_playback_avail)
2365                                 snd_cmipci_set_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
2366                 } else {
2367                         if (chip->spdif_playback_avail)
2368                                 snd_cmipci_clear_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
2369                 }
2370         }
2371         chip->spdif_playback_enabled = ucontrol->value.integer.value[0];
2372         return changed;
2373 }
2374
2375
2376 static int snd_cmipci_line_in_mode_info(struct snd_kcontrol *kcontrol,
2377                                         struct snd_ctl_elem_info *uinfo)
2378 {
2379         struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2380         static char *texts[3] = { "Line-In", "Rear Output", "Bass Output" };
2381         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2382         uinfo->count = 1;
2383         uinfo->value.enumerated.items = cm->chip_version >= 39 ? 3 : 2;
2384         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2385                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2386         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2387         return 0;
2388 }
2389
2390 static inline unsigned int get_line_in_mode(struct cmipci *cm)
2391 {
2392         unsigned int val;
2393         if (cm->chip_version >= 39) {
2394                 val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL);
2395                 if (val & (CM_CENTR2LIN | CM_BASE2LIN))
2396                         return 2;
2397         }
2398         val = snd_cmipci_read_b(cm, CM_REG_MIXER1);
2399         if (val & CM_REAR2LIN)
2400                 return 1;
2401         return 0;
2402 }
2403
2404 static int snd_cmipci_line_in_mode_get(struct snd_kcontrol *kcontrol,
2405                                        struct snd_ctl_elem_value *ucontrol)
2406 {
2407         struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2408
2409         spin_lock_irq(&cm->reg_lock);
2410         ucontrol->value.enumerated.item[0] = get_line_in_mode(cm);
2411         spin_unlock_irq(&cm->reg_lock);
2412         return 0;
2413 }
2414
2415 static int snd_cmipci_line_in_mode_put(struct snd_kcontrol *kcontrol,
2416                                        struct snd_ctl_elem_value *ucontrol)
2417 {
2418         struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2419         int change;
2420
2421         spin_lock_irq(&cm->reg_lock);
2422         if (ucontrol->value.enumerated.item[0] == 2)
2423                 change = snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
2424         else
2425                 change = snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
2426         if (ucontrol->value.enumerated.item[0] == 1)
2427                 change |= snd_cmipci_set_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
2428         else
2429                 change |= snd_cmipci_clear_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
2430         spin_unlock_irq(&cm->reg_lock);
2431         return change;
2432 }
2433
2434 static int snd_cmipci_mic_in_mode_info(struct snd_kcontrol *kcontrol,
2435                                        struct snd_ctl_elem_info *uinfo)
2436 {
2437         static char *texts[2] = { "Mic-In", "Center/LFE Output" };
2438         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2439         uinfo->count = 1;
2440         uinfo->value.enumerated.items = 2;
2441         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2442                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2443         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2444         return 0;
2445 }
2446
2447 static int snd_cmipci_mic_in_mode_get(struct snd_kcontrol *kcontrol,
2448                                       struct snd_ctl_elem_value *ucontrol)
2449 {
2450         struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2451         /* same bit as spdi_phase */
2452         spin_lock_irq(&cm->reg_lock);
2453         ucontrol->value.enumerated.item[0] = 
2454                 (snd_cmipci_read_b(cm, CM_REG_MISC) & CM_SPDIF_INVERSE) ? 1 : 0;
2455         spin_unlock_irq(&cm->reg_lock);
2456         return 0;
2457 }
2458
2459 static int snd_cmipci_mic_in_mode_put(struct snd_kcontrol *kcontrol,
2460                                       struct snd_ctl_elem_value *ucontrol)
2461 {
2462         struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2463         int change;
2464
2465         spin_lock_irq(&cm->reg_lock);
2466         if (ucontrol->value.enumerated.item[0])
2467                 change = snd_cmipci_set_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
2468         else
2469                 change = snd_cmipci_clear_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
2470         spin_unlock_irq(&cm->reg_lock);
2471         return change;
2472 }
2473
2474 /* both for CM8338/8738 */
2475 static struct snd_kcontrol_new snd_cmipci_mixer_switches[] __devinitdata = {
2476         DEFINE_MIXER_SWITCH("Four Channel Mode", fourch),
2477         {
2478                 .name = "Line-In Mode",
2479                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2480                 .info = snd_cmipci_line_in_mode_info,
2481                 .get = snd_cmipci_line_in_mode_get,
2482                 .put = snd_cmipci_line_in_mode_put,
2483         },
2484 };
2485
2486 /* for non-multichannel chips */
2487 static struct snd_kcontrol_new snd_cmipci_nomulti_switch __devinitdata =
2488 DEFINE_MIXER_SWITCH("Exchange DAC", exchange_dac);
2489
2490 /* only for CM8738 */
2491 static struct snd_kcontrol_new snd_cmipci_8738_mixer_switches[] __devinitdata = {
2492 #if 0 /* controlled in pcm device */
2493         DEFINE_MIXER_SWITCH("IEC958 In Record", spdif_in),
2494         DEFINE_MIXER_SWITCH("IEC958 Out", spdif_out),
2495         DEFINE_MIXER_SWITCH("IEC958 Out To DAC", spdo2dac),
2496 #endif
2497         // DEFINE_MIXER_SWITCH("IEC958 Output Switch", spdif_enable),
2498         { .name = "IEC958 Output Switch",
2499           .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2500           .info = snd_cmipci_uswitch_info,
2501           .get = snd_cmipci_spdout_enable_get,
2502           .put = snd_cmipci_spdout_enable_put,
2503         },
2504         DEFINE_MIXER_SWITCH("IEC958 In Valid", spdi_valid),
2505         DEFINE_MIXER_SWITCH("IEC958 Copyright", spdif_copyright),
2506         DEFINE_MIXER_SWITCH("IEC958 5V", spdo_5v),
2507 //      DEFINE_MIXER_SWITCH("IEC958 In/Out 48KHz", spdo_48k),
2508         DEFINE_MIXER_SWITCH("IEC958 Loop", spdif_loop),
2509         DEFINE_MIXER_SWITCH("IEC958 In Monitor", spdi_monitor),
2510 };
2511
2512 /* only for model 033/037 */
2513 static struct snd_kcontrol_new snd_cmipci_old_mixer_switches[] __devinitdata = {
2514         DEFINE_MIXER_SWITCH("IEC958 Mix Analog", spdif_dac_out),
2515         DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase),
2516         DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel1),
2517 };
2518
2519 /* only for model 039 or later */
2520 static struct snd_kcontrol_new snd_cmipci_extra_mixer_switches[] __devinitdata = {
2521         DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel2),
2522         DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase2),
2523         {
2524                 .name = "Mic-In Mode",
2525                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2526                 .info = snd_cmipci_mic_in_mode_info,
2527                 .get = snd_cmipci_mic_in_mode_get,
2528                 .put = snd_cmipci_mic_in_mode_put,
2529         }
2530 };
2531
2532 /* card control switches */
2533 static struct snd_kcontrol_new snd_cmipci_control_switches[] __devinitdata = {
2534         // DEFINE_CARD_SWITCH("Joystick", joystick), /* now module option */
2535         DEFINE_CARD_SWITCH("Modem", modem),
2536 };
2537
2538
2539 static int __devinit snd_cmipci_mixer_new(struct cmipci *cm, int pcm_spdif_device)
2540 {
2541         struct snd_card *card;
2542         struct snd_kcontrol_new *sw;
2543         struct snd_kcontrol *kctl;
2544         unsigned int idx;
2545         int err;
2546
2547         snd_assert(cm != NULL && cm->card != NULL, return -EINVAL);
2548
2549         card = cm->card;
2550
2551         strcpy(card->mixername, "CMedia PCI");
2552
2553         spin_lock_irq(&cm->reg_lock);
2554         snd_cmipci_mixer_write(cm, 0x00, 0x00);         /* mixer reset */
2555         spin_unlock_irq(&cm->reg_lock);
2556
2557         for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixers); idx++) {
2558                 if (cm->chip_version == 68) {   // 8768 has no PCM volume
2559                         if (!strcmp(snd_cmipci_mixers[idx].name,
2560                                 "PCM Playback Volume"))
2561                                 continue;
2562                 }
2563                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cmipci_mixers[idx], cm))) < 0)
2564                         return err;
2565         }
2566
2567         /* mixer switches */
2568         sw = snd_cmipci_mixer_switches;
2569         for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixer_switches); idx++, sw++) {
2570                 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2571                 if (err < 0)
2572                         return err;
2573         }
2574         if (! cm->can_multi_ch) {
2575                 err = snd_ctl_add(cm->card, snd_ctl_new1(&snd_cmipci_nomulti_switch, cm));
2576                 if (err < 0)
2577                         return err;
2578         }
2579         if (cm->device == PCI_DEVICE_ID_CMEDIA_CM8738 ||
2580             cm->device == PCI_DEVICE_ID_CMEDIA_CM8738B) {
2581                 sw = snd_cmipci_8738_mixer_switches;
2582                 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_8738_mixer_switches); idx++, sw++) {
2583                         err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2584                         if (err < 0)
2585                                 return err;
2586                 }
2587                 if (cm->can_ac3_hw) {
2588                         if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_default, cm))) < 0)
2589                                 return err;
2590                         kctl->id.device = pcm_spdif_device;
2591                         if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_mask, cm))) < 0)
2592                                 return err;
2593                         kctl->id.device = pcm_spdif_device;
2594                         if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_stream, cm))) < 0)
2595                                 return err;
2596                         kctl->id.device = pcm_spdif_device;
2597                 }
2598                 if (cm->chip_version <= 37) {
2599                         sw = snd_cmipci_old_mixer_switches;
2600                         for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_old_mixer_switches); idx++, sw++) {
2601                                 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2602                                 if (err < 0)
2603                                         return err;
2604                         }
2605                 }
2606         }
2607         if (cm->chip_version >= 39) {
2608                 sw = snd_cmipci_extra_mixer_switches;
2609                 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_extra_mixer_switches); idx++, sw++) {
2610                         err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2611                         if (err < 0)
2612                                 return err;
2613                 }
2614         }
2615
2616         /* card switches */
2617         sw = snd_cmipci_control_switches;
2618         for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_control_switches); idx++, sw++) {
2619                 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2620                 if (err < 0)
2621                         return err;
2622         }
2623
2624         for (idx = 0; idx < CM_SAVED_MIXERS; idx++) {
2625                 struct snd_ctl_elem_id id;
2626                 struct snd_kcontrol *ctl;
2627                 memset(&id, 0, sizeof(id));
2628                 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2629                 strcpy(id.name, cm_saved_mixer[idx].name);
2630                 if ((ctl = snd_ctl_find_id(cm->card, &id)) != NULL)
2631                         cm->mixer_res_ctl[idx] = ctl;
2632         }
2633
2634         return 0;
2635 }
2636
2637
2638 /*
2639  * proc interface
2640  */
2641
2642 #ifdef CONFIG_PROC_FS
2643 static void snd_cmipci_proc_read(struct snd_info_entry *entry, 
2644                                  struct snd_info_buffer *buffer)
2645 {
2646         struct cmipci *cm = entry->private_data;
2647         int i, v;
2648         
2649         snd_iprintf(buffer, "%s\n", cm->card->longname);
2650         for (i = 0; i < 0x94; i++) {
2651                 if (i == 0x28)
2652                         i = 0x90;
2653                 v = inb(cm->iobase + i);
2654                 if (i % 4 == 0)
2655                         snd_iprintf(buffer, "\n%02x:", i);
2656                 snd_iprintf(buffer, " %02x", v);
2657         }
2658         snd_iprintf(buffer, "\n");
2659 }
2660
2661 static void __devinit snd_cmipci_proc_init(struct cmipci *cm)
2662 {
2663         struct snd_info_entry *entry;
2664
2665         if (! snd_card_proc_new(cm->card, "cmipci", &entry))
2666                 snd_info_set_text_ops(entry, cm, snd_cmipci_proc_read);
2667 }
2668 #else /* !CONFIG_PROC_FS */
2669 static inline void snd_cmipci_proc_init(struct cmipci *cm) {}
2670 #endif
2671
2672
2673 static struct pci_device_id snd_cmipci_ids[] = {
2674         {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338A, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2675         {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2676         {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2677         {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2678         {PCI_VENDOR_ID_AL, PCI_DEVICE_ID_CMEDIA_CM8738, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2679         {0,},
2680 };
2681
2682
2683 /*
2684  * check chip version and capabilities
2685  * driver name is modified according to the chip model
2686  */
2687 static void __devinit query_chip(struct cmipci *cm)
2688 {
2689         unsigned int detect;
2690
2691         /* check reg 0Ch, bit 24-31 */
2692         detect = snd_cmipci_read(cm, CM_REG_INT_HLDCLR) & CM_CHIP_MASK2;
2693         if (! detect) {
2694                 /* check reg 08h, bit 24-28 */
2695                 detect = snd_cmipci_read(cm, CM_REG_CHFORMAT) & CM_CHIP_MASK1;
2696                 switch (detect) {
2697                 case 0:
2698                         cm->chip_version = 33;
2699                         if (cm->do_soft_ac3)
2700                                 cm->can_ac3_sw = 1;
2701                         else
2702                                 cm->can_ac3_hw = 1;
2703                         break;
2704                 case CM_CHIP_037:
2705                         cm->chip_version = 37;
2706                         cm->can_ac3_hw = 1;
2707                         break;
2708                 default:
2709                         cm->chip_version = 39;
2710                         cm->can_ac3_hw = 1;
2711                         break;
2712                 }
2713                 cm->max_channels = 2;
2714         } else {
2715                 if (detect & CM_CHIP_039) {
2716                         cm->chip_version = 39;
2717                         if (detect & CM_CHIP_039_6CH) /* 4 or 6 channels */
2718                                 cm->max_channels = 6;
2719                         else
2720                                 cm->max_channels = 4;
2721                 } else if (detect & CM_CHIP_8768) {
2722                         cm->chip_version = 68;
2723                         cm->max_channels = 8;
2724                 } else {
2725                         cm->chip_version = 55;
2726                         cm->max_channels = 6;
2727                 }
2728                 cm->can_ac3_hw = 1;
2729                 cm->can_multi_ch = 1;
2730         }
2731 }
2732
2733 #ifdef SUPPORT_JOYSTICK
2734 static int __devinit snd_cmipci_create_gameport(struct cmipci *cm, int dev)
2735 {
2736         static int ports[] = { 0x201, 0x200, 0 }; /* FIXME: majority is 0x201? */
2737         struct gameport *gp;
2738         struct resource *r = NULL;
2739         int i, io_port = 0;
2740
2741         if (joystick_port[dev] == 0)
2742                 return -ENODEV;
2743
2744         if (joystick_port[dev] == 1) { /* auto-detect */
2745                 for (i = 0; ports[i]; i++) {
2746                         io_port = ports[i];
2747                         r = request_region(io_port, 1, "CMIPCI gameport");
2748                         if (r)
2749                                 break;
2750                 }
2751         } else {
2752                 io_port = joystick_port[dev];
2753                 r = request_region(io_port, 1, "CMIPCI gameport");
2754         }
2755
2756         if (!r) {
2757                 printk(KERN_WARNING "cmipci: cannot reserve joystick ports\n");
2758                 return -EBUSY;
2759         }
2760
2761         cm->gameport = gp = gameport_allocate_port();
2762         if (!gp) {
2763                 printk(KERN_ERR "cmipci: cannot allocate memory for gameport\n");
2764                 release_and_free_resource(r);
2765                 return -ENOMEM;
2766         }
2767         gameport_set_name(gp, "C-Media Gameport");
2768         gameport_set_phys(gp, "pci%s/gameport0", pci_name(cm->pci));
2769         gameport_set_dev_parent(gp, &cm->pci->dev);
2770         gp->io = io_port;
2771         gameport_set_port_data(gp, r);
2772
2773         snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
2774
2775         gameport_register_port(cm->gameport);
2776
2777         return 0;
2778 }
2779
2780 static void snd_cmipci_free_gameport(struct cmipci *cm)
2781 {
2782         if (cm->gameport) {
2783                 struct resource *r = gameport_get_port_data(cm->gameport);
2784
2785                 gameport_unregister_port(cm->gameport);
2786                 cm->gameport = NULL;
2787
2788                 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
2789                 release_and_free_resource(r);
2790         }
2791 }
2792 #else
2793 static inline int snd_cmipci_create_gameport(struct cmipci *cm, int dev) { return -ENOSYS; }
2794 static inline void snd_cmipci_free_gameport(struct cmipci *cm) { }
2795 #endif
2796
2797 static int snd_cmipci_free(struct cmipci *cm)
2798 {
2799         if (cm->irq >= 0) {
2800                 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
2801                 snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT);
2802                 snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);  /* disable ints */
2803                 snd_cmipci_ch_reset(cm, CM_CH_PLAY);
2804                 snd_cmipci_ch_reset(cm, CM_CH_CAPT);
2805                 snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
2806                 snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
2807
2808                 /* reset mixer */
2809                 snd_cmipci_mixer_write(cm, 0, 0);
2810
2811                 synchronize_irq(cm->irq);
2812
2813                 free_irq(cm->irq, cm);
2814         }
2815
2816         snd_cmipci_free_gameport(cm);
2817         pci_release_regions(cm->pci);
2818         pci_disable_device(cm->pci);
2819         kfree(cm);
2820         return 0;
2821 }
2822
2823 static int snd_cmipci_dev_free(struct snd_device *device)
2824 {
2825         struct cmipci *cm = device->device_data;
2826         return snd_cmipci_free(cm);
2827 }
2828
2829 static int __devinit snd_cmipci_create_fm(struct cmipci *cm, long fm_port)
2830 {
2831         long iosynth;
2832         unsigned int val;
2833         struct snd_opl3 *opl3;
2834         int err;
2835
2836         if (!fm_port)
2837                 goto disable_fm;
2838
2839         if (cm->chip_version >= 39) {
2840                 /* first try FM regs in PCI port range */
2841                 iosynth = cm->iobase + CM_REG_FM_PCI;
2842                 err = snd_opl3_create(cm->card, iosynth, iosynth + 2,
2843                                       OPL3_HW_OPL3, 1, &opl3);
2844         } else {
2845                 err = -EIO;
2846         }
2847         if (err < 0) {
2848                 /* then try legacy ports */
2849                 val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL) & ~CM_FMSEL_MASK;
2850                 iosynth = fm_port;
2851                 switch (iosynth) {
2852                 case 0x3E8: val |= CM_FMSEL_3E8; break;
2853                 case 0x3E0: val |= CM_FMSEL_3E0; break;
2854                 case 0x3C8: val |= CM_FMSEL_3C8; break;
2855                 case 0x388: val |= CM_FMSEL_388; break;
2856                 default:
2857                         goto disable_fm;
2858                 }
2859                 snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
2860                 /* enable FM */
2861                 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
2862
2863                 if (snd_opl3_create(cm->card, iosynth, iosynth + 2,
2864                                     OPL3_HW_OPL3, 0, &opl3) < 0) {
2865                         printk(KERN_ERR "cmipci: no OPL device at %#lx, "
2866                                "skipping...\n", iosynth);
2867                         goto disable_fm;
2868                 }
2869         }
2870         if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
2871                 printk(KERN_ERR "cmipci: cannot create OPL3 hwdep\n");
2872                 return err;
2873         }
2874         return 0;
2875
2876  disable_fm:
2877         snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_FMSEL_MASK);
2878         snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
2879         return 0;
2880 }
2881
2882 static int __devinit snd_cmipci_create(struct snd_card *card, struct pci_dev *pci,
2883                                        int dev, struct cmipci **rcmipci)
2884 {
2885         struct cmipci *cm;
2886         int err;
2887         static struct snd_device_ops ops = {
2888                 .dev_free =     snd_cmipci_dev_free,
2889         };
2890         unsigned int val;
2891         long iomidi;
2892         int integrated_midi = 0;
2893         char modelstr[16];
2894         int pcm_index, pcm_spdif_index;
2895         static struct pci_device_id intel_82437vx[] = {
2896                 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437VX) },
2897                 { },
2898         };
2899
2900         *rcmipci = NULL;
2901
2902         if ((err = pci_enable_device(pci)) < 0)
2903                 return err;
2904
2905         cm = kzalloc(sizeof(*cm), GFP_KERNEL);
2906         if (cm == NULL) {
2907                 pci_disable_device(pci);
2908                 return -ENOMEM;
2909         }
2910
2911         spin_lock_init(&cm->reg_lock);
2912         mutex_init(&cm->open_mutex);
2913         cm->device = pci->device;
2914         cm->card = card;
2915         cm->pci = pci;
2916         cm->irq = -1;
2917         cm->channel[0].ch = 0;
2918         cm->channel[1].ch = 1;
2919         cm->channel[0].is_dac = cm->channel[1].is_dac = 1; /* dual DAC mode */
2920
2921         if ((err = pci_request_regions(pci, card->driver)) < 0) {
2922                 kfree(cm);
2923                 pci_disable_device(pci);
2924                 return err;
2925         }
2926         cm->iobase = pci_resource_start(pci, 0);
2927
2928         if (request_irq(pci->irq, snd_cmipci_interrupt,
2929                         IRQF_SHARED, card->driver, cm)) {
2930                 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
2931                 snd_cmipci_free(cm);
2932                 return -EBUSY;
2933         }
2934         cm->irq = pci->irq;
2935
2936         pci_set_master(cm->pci);
2937
2938         /*
2939          * check chip version, max channels and capabilities
2940          */
2941
2942         cm->chip_version = 0;
2943         cm->max_channels = 2;
2944         cm->do_soft_ac3 = soft_ac3[dev];
2945
2946         if (pci->device != PCI_DEVICE_ID_CMEDIA_CM8338A &&
2947             pci->device != PCI_DEVICE_ID_CMEDIA_CM8338B)
2948                 query_chip(cm);
2949         /* added -MCx suffix for chip supporting multi-channels */
2950         if (cm->can_multi_ch)
2951                 sprintf(cm->card->driver + strlen(cm->card->driver),
2952                         "-MC%d", cm->max_channels);
2953         else if (cm->can_ac3_sw)
2954                 strcpy(cm->card->driver + strlen(cm->card->driver), "-SWIEC");
2955
2956         cm->dig_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
2957         cm->dig_pcm_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
2958
2959 #if CM_CH_PLAY == 1
2960         cm->ctrl = CM_CHADC0;   /* default FUNCNTRL0 */
2961 #else
2962         cm->ctrl = CM_CHADC1;   /* default FUNCNTRL0 */
2963 #endif
2964
2965         /* initialize codec registers */
2966         snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
2967         snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
2968         snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);     /* disable ints */
2969         snd_cmipci_ch_reset(cm, CM_CH_PLAY);
2970         snd_cmipci_ch_reset(cm, CM_CH_CAPT);
2971         snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0);       /* disable channels */
2972         snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
2973
2974         snd_cmipci_write(cm, CM_REG_CHFORMAT, 0);
2975         snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC|CM_N4SPK3D);
2976 #if CM_CH_PLAY == 1
2977         snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
2978 #else
2979         snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
2980 #endif
2981         if (cm->chip_version) {
2982                 snd_cmipci_write_b(cm, CM_REG_EXT_MISC, 0x20); /* magic */
2983                 snd_cmipci_write_b(cm, CM_REG_EXT_MISC + 1, 0x09); /* more magic */
2984         }
2985         /* Set Bus Master Request */
2986         snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_BREQ);
2987
2988         /* Assume TX and compatible chip set (Autodetection required for VX chip sets) */
2989         switch (pci->device) {
2990         case PCI_DEVICE_ID_CMEDIA_CM8738:
2991         case PCI_DEVICE_ID_CMEDIA_CM8738B:
2992                 if (!pci_dev_present(intel_82437vx)) 
2993                         snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_TXVX);
2994                 break;
2995         default:
2996                 break;
2997         }
2998
2999         if (cm->chip_version < 68) {
3000                 val = pci->device < 0x110 ? 8338 : 8738;
3001         } else {
3002                 switch (snd_cmipci_read_b(cm, CM_REG_INT_HLDCLR + 3) & 0x03) {
3003                 case 0:
3004                         val = 8769;
3005                         break;
3006                 case 2:
3007                         val = 8762;
3008                         break;
3009                 default:
3010                         switch ((pci->subsystem_vendor << 16) |
3011                                 pci->subsystem_device) {
3012                         case 0x13f69761:
3013                         case 0x584d3741:
3014                         case 0x584d3751:
3015                         case 0x584d3761:
3016                         case 0x584d3771:
3017                         case 0x72848384:
3018                                 val = 8770;
3019                                 break;
3020                         default:
3021                                 val = 8768;
3022                                 break;
3023                         }
3024                 }
3025         }
3026         sprintf(card->shortname, "C-Media CMI%d", val);
3027         if (cm->chip_version < 68)
3028                 sprintf(modelstr, " (model %d)", cm->chip_version);
3029         else
3030                 modelstr[0] = '\0';
3031         sprintf(card->longname, "%s%s at %#lx, irq %i",
3032                 card->shortname, modelstr, cm->iobase, cm->irq);
3033
3034         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, cm, &ops)) < 0) {
3035                 snd_cmipci_free(cm);
3036                 return err;
3037         }
3038
3039         if (cm->chip_version >= 39) {
3040                 val = snd_cmipci_read_b(cm, CM_REG_MPU_PCI + 1);
3041                 if (val != 0x00 && val != 0xff) {
3042                         iomidi = cm->iobase + CM_REG_MPU_PCI;
3043                         integrated_midi = 1;
3044                 }
3045         }
3046         if (!integrated_midi) {
3047                 val = 0;
3048                 iomidi = mpu_port[dev];
3049                 switch (iomidi) {
3050                 case 0x320: val = CM_VMPU_320; break;
3051                 case 0x310: val = CM_VMPU_310; break;
3052                 case 0x300: val = CM_VMPU_300; break;
3053                 case 0x330: val = CM_VMPU_330; break;
3054                 default:
3055                             iomidi = 0; break;
3056                 }
3057                 if (iomidi > 0) {
3058                         snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
3059                         /* enable UART */
3060                         snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_UART_EN);
3061                         if (inb(iomidi + 1) == 0xff) {
3062                                 snd_printk(KERN_ERR "cannot enable MPU-401 port"
3063                                            " at %#lx\n", iomidi);
3064                                 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1,
3065                                                      CM_UART_EN);
3066                                 iomidi = 0;
3067                         }
3068                 }
3069         }
3070
3071         if (cm->chip_version < 68) {
3072                 err = snd_cmipci_create_fm(cm, fm_port[dev]);
3073                 if (err < 0)
3074                         return err;
3075         }
3076
3077         /* reset mixer */
3078         snd_cmipci_mixer_write(cm, 0, 0);
3079
3080         snd_cmipci_proc_init(cm);
3081
3082         /* create pcm devices */
3083         pcm_index = pcm_spdif_index = 0;
3084         if ((err = snd_cmipci_pcm_new(cm, pcm_index)) < 0)
3085                 return err;
3086         pcm_index++;
3087         if ((err = snd_cmipci_pcm2_new(cm, pcm_index)) < 0)
3088                 return err;
3089         pcm_index++;
3090         if (cm->can_ac3_hw || cm->can_ac3_sw) {
3091                 pcm_spdif_index = pcm_index;
3092                 if ((err = snd_cmipci_pcm_spdif_new(cm, pcm_index)) < 0)
3093                         return err;
3094         }
3095
3096         /* create mixer interface & switches */
3097         if ((err = snd_cmipci_mixer_new(cm, pcm_spdif_index)) < 0)
3098                 return err;
3099
3100         if (iomidi > 0) {
3101                 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
3102                                                iomidi,
3103                                                (integrated_midi ?
3104                                                 MPU401_INFO_INTEGRATED : 0),
3105                                                cm->irq, 0, &cm->rmidi)) < 0) {
3106                         printk(KERN_ERR "cmipci: no UART401 device at 0x%lx\n", iomidi);
3107                 }
3108         }
3109
3110 #ifdef USE_VAR48KRATE
3111         for (val = 0; val < ARRAY_SIZE(rates); val++)
3112                 snd_cmipci_set_pll(cm, rates[val], val);
3113
3114         /*
3115          * (Re-)Enable external switch spdo_48k
3116          */
3117         snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K|CM_SPDF_AC97);
3118 #endif /* USE_VAR48KRATE */
3119
3120         if (snd_cmipci_create_gameport(cm, dev) < 0)
3121                 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
3122
3123         snd_card_set_dev(card, &pci->dev);
3124
3125         *rcmipci = cm;
3126         return 0;
3127 }
3128
3129 /*
3130  */
3131
3132 MODULE_DEVICE_TABLE(pci, snd_cmipci_ids);
3133
3134 static int __devinit snd_cmipci_probe(struct pci_dev *pci,
3135                                       const struct pci_device_id *pci_id)
3136 {
3137         static int dev;
3138         struct snd_card *card;
3139         struct cmipci *cm;
3140         int err;
3141
3142         if (dev >= SNDRV_CARDS)
3143                 return -ENODEV;
3144         if (! enable[dev]) {
3145                 dev++;
3146                 return -ENOENT;
3147         }
3148
3149         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
3150         if (card == NULL)
3151                 return -ENOMEM;
3152         
3153         switch (pci->device) {
3154         case PCI_DEVICE_ID_CMEDIA_CM8738:
3155         case PCI_DEVICE_ID_CMEDIA_CM8738B:
3156                 strcpy(card->driver, "CMI8738");
3157                 break;
3158         case PCI_DEVICE_ID_CMEDIA_CM8338A:
3159         case PCI_DEVICE_ID_CMEDIA_CM8338B:
3160                 strcpy(card->driver, "CMI8338");
3161                 break;
3162         default:
3163                 strcpy(card->driver, "CMIPCI");
3164                 break;
3165         }
3166
3167         if ((err = snd_cmipci_create(card, pci, dev, &cm)) < 0) {
3168                 snd_card_free(card);
3169                 return err;
3170         }
3171         card->private_data = cm;
3172
3173         if ((err = snd_card_register(card)) < 0) {
3174                 snd_card_free(card);
3175                 return err;
3176         }
3177         pci_set_drvdata(pci, card);
3178         dev++;
3179         return 0;
3180
3181 }
3182
3183 static void __devexit snd_cmipci_remove(struct pci_dev *pci)
3184 {
3185         snd_card_free(pci_get_drvdata(pci));
3186         pci_set_drvdata(pci, NULL);
3187 }
3188
3189
3190 #ifdef CONFIG_PM
3191 /*
3192  * power management
3193  */
3194 static unsigned char saved_regs[] = {
3195         CM_REG_FUNCTRL1, CM_REG_CHFORMAT, CM_REG_LEGACY_CTRL, CM_REG_MISC_CTRL,
3196         CM_REG_MIXER0, CM_REG_MIXER1, CM_REG_MIXER2, CM_REG_MIXER3, CM_REG_PLL,
3197         CM_REG_CH0_FRAME1, CM_REG_CH0_FRAME2,
3198         CM_REG_CH1_FRAME1, CM_REG_CH1_FRAME2, CM_REG_EXT_MISC,
3199         CM_REG_INT_STATUS, CM_REG_INT_HLDCLR, CM_REG_FUNCTRL0,
3200 };
3201
3202 static unsigned char saved_mixers[] = {
3203         SB_DSP4_MASTER_DEV, SB_DSP4_MASTER_DEV + 1,
3204         SB_DSP4_PCM_DEV, SB_DSP4_PCM_DEV + 1,
3205         SB_DSP4_SYNTH_DEV, SB_DSP4_SYNTH_DEV + 1,
3206         SB_DSP4_CD_DEV, SB_DSP4_CD_DEV + 1,
3207         SB_DSP4_LINE_DEV, SB_DSP4_LINE_DEV + 1,
3208         SB_DSP4_MIC_DEV, SB_DSP4_SPEAKER_DEV,
3209         CM_REG_EXTENT_IND, SB_DSP4_OUTPUT_SW,
3210         SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT,
3211 };
3212
3213 static int snd_cmipci_suspend(struct pci_dev *pci, pm_message_t state)
3214 {
3215         struct snd_card *card = pci_get_drvdata(pci);
3216         struct cmipci *cm = card->private_data;
3217         int i;
3218
3219         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
3220         
3221         snd_pcm_suspend_all(cm->pcm);
3222         snd_pcm_suspend_all(cm->pcm2);
3223         snd_pcm_suspend_all(cm->pcm_spdif);
3224
3225         /* save registers */
3226         for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
3227                 cm->saved_regs[i] = snd_cmipci_read(cm, saved_regs[i]);
3228         for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
3229                 cm->saved_mixers[i] = snd_cmipci_mixer_read(cm, saved_mixers[i]);
3230
3231         /* disable ints */
3232         snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
3233
3234         pci_disable_device(pci);
3235         pci_save_state(pci);
3236         pci_set_power_state(pci, pci_choose_state(pci, state));
3237         return 0;
3238 }
3239
3240 static int snd_cmipci_resume(struct pci_dev *pci)
3241 {
3242         struct snd_card *card = pci_get_drvdata(pci);
3243         struct cmipci *cm = card->private_data;
3244         int i;
3245
3246         pci_set_power_state(pci, PCI_D0);
3247         pci_restore_state(pci);
3248         if (pci_enable_device(pci) < 0) {
3249                 printk(KERN_ERR "cmipci: pci_enable_device failed, "
3250                        "disabling device\n");
3251                 snd_card_disconnect(card);
3252                 return -EIO;
3253         }
3254         pci_set_master(pci);
3255
3256         /* reset / initialize to a sane state */
3257         snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
3258         snd_cmipci_ch_reset(cm, CM_CH_PLAY);
3259         snd_cmipci_ch_reset(cm, CM_CH_CAPT);
3260         snd_cmipci_mixer_write(cm, 0, 0);
3261
3262         /* restore registers */
3263         for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
3264                 snd_cmipci_write(cm, saved_regs[i], cm->saved_regs[i]);
3265         for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
3266                 snd_cmipci_mixer_write(cm, saved_mixers[i], cm->saved_mixers[i]);
3267
3268         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
3269         return 0;
3270 }
3271 #endif /* CONFIG_PM */
3272
3273 static struct pci_driver driver = {
3274         .name = "C-Media PCI",
3275         .id_table = snd_cmipci_ids,
3276         .probe = snd_cmipci_probe,
3277         .remove = __devexit_p(snd_cmipci_remove),
3278 #ifdef CONFIG_PM
3279         .suspend = snd_cmipci_suspend,
3280         .resume = snd_cmipci_resume,
3281 #endif
3282 };
3283         
3284 static int __init alsa_card_cmipci_init(void)
3285 {
3286         return pci_register_driver(&driver);
3287 }
3288
3289 static void __exit alsa_card_cmipci_exit(void)
3290 {
3291         pci_unregister_driver(&driver);
3292 }
3293
3294 module_init(alsa_card_cmipci_init)
3295 module_exit(alsa_card_cmipci_exit)