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