xref: /openbmc/linux/sound/pci/via82xx.c (revision 4f550df5)
1 /*
2  *   ALSA driver for VIA VT82xx (South Bridge)
3  *
4  *   VT82C686A/B/C, VT8233A/C, VT8235
5  *
6  *	Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
7  *	                   Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
8  *                    2002 Takashi Iwai <tiwai@suse.de>
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU General Public License as published by
12  *   the Free Software Foundation; either version 2 of the License, or
13  *   (at your option) any later version.
14  *
15  *   This program is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with this program; if not, write to the Free Software
22  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  *
24  */
25 
26 /*
27  * Changes:
28  *
29  * Dec. 19, 2002	Takashi Iwai <tiwai@suse.de>
30  *	- use the DSX channels for the first pcm playback.
31  *	  (on VIA8233, 8233C and 8235 only)
32  *	  this will allow you play simultaneously up to 4 streams.
33  *	  multi-channel playback is assigned to the second device
34  *	  on these chips.
35  *	- support the secondary capture (on VIA8233/C,8235)
36  *	- SPDIF support
37  *	  the DSX3 channel can be used for SPDIF output.
38  *	  on VIA8233A, this channel is assigned to the second pcm
39  *	  playback.
40  *	  the card config of alsa-lib will assign the correct
41  *	  device for applications.
42  *	- clean up the code, separate low-level initialization
43  *	  routines for each chipset.
44  *
45  * Sep. 26, 2005	Karsten Wiese <annabellesgarden@yahoo.de>
46  *	- Optimize position calculation for the 823x chips.
47  */
48 
49 #include <sound/driver.h>
50 #include <asm/io.h>
51 #include <linux/delay.h>
52 #include <linux/interrupt.h>
53 #include <linux/init.h>
54 #include <linux/pci.h>
55 #include <linux/slab.h>
56 #include <linux/gameport.h>
57 #include <linux/moduleparam.h>
58 #include <sound/core.h>
59 #include <sound/pcm.h>
60 #include <sound/pcm_params.h>
61 #include <sound/info.h>
62 #include <sound/ac97_codec.h>
63 #include <sound/mpu401.h>
64 #include <sound/initval.h>
65 
66 #if 0
67 #define POINTER_DEBUG
68 #endif
69 
70 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
71 MODULE_DESCRIPTION("VIA VT82xx audio");
72 MODULE_LICENSE("GPL");
73 MODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C,pci},{VIA,VT8233A/C,8235}}");
74 
75 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
76 #define SUPPORT_JOYSTICK 1
77 #endif
78 
79 static int index = SNDRV_DEFAULT_IDX1;	/* Index 0-MAX */
80 static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
81 static long mpu_port;
82 #ifdef SUPPORT_JOYSTICK
83 static int joystick;
84 #endif
85 static int ac97_clock = 48000;
86 static char *ac97_quirk;
87 static int dxs_support;
88 
89 module_param(index, int, 0444);
90 MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
91 module_param(id, charp, 0444);
92 MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
93 module_param(mpu_port, long, 0444);
94 MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
95 #ifdef SUPPORT_JOYSTICK
96 module_param(joystick, bool, 0444);
97 MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
98 #endif
99 module_param(ac97_clock, int, 0444);
100 MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
101 module_param(ac97_quirk, charp, 0444);
102 MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
103 module_param(dxs_support, int, 0444);
104 MODULE_PARM_DESC(dxs_support, "Support for DXS channels (0 = auto, 1 = enable, 2 = disable, 3 = 48k only, 4 = no VRA, 5 = enable any sample rate)");
105 
106 /* just for backward compatibility */
107 static int enable;
108 module_param(enable, int, 0444);
109 
110 
111 /* revision numbers for via686 */
112 #define VIA_REV_686_A		0x10
113 #define VIA_REV_686_B		0x11
114 #define VIA_REV_686_C		0x12
115 #define VIA_REV_686_D		0x13
116 #define VIA_REV_686_E		0x14
117 #define VIA_REV_686_H		0x20
118 
119 /* revision numbers for via8233 */
120 #define VIA_REV_PRE_8233	0x10	/* not in market */
121 #define VIA_REV_8233C		0x20	/* 2 rec, 4 pb, 1 multi-pb */
122 #define VIA_REV_8233		0x30	/* 2 rec, 4 pb, 1 multi-pb, spdif */
123 #define VIA_REV_8233A		0x40	/* 1 rec, 1 multi-pb, spdf */
124 #define VIA_REV_8235		0x50	/* 2 rec, 4 pb, 1 multi-pb, spdif */
125 #define VIA_REV_8237		0x60
126 
127 /*
128  *  Direct registers
129  */
130 
131 #define VIAREG(via, x) ((via)->port + VIA_REG_##x)
132 #define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
133 
134 /* common offsets */
135 #define VIA_REG_OFFSET_STATUS		0x00	/* byte - channel status */
136 #define   VIA_REG_STAT_ACTIVE		0x80	/* RO */
137 #define   VIA8233_SHADOW_STAT_ACTIVE	0x08	/* RO */
138 #define   VIA_REG_STAT_PAUSED		0x40	/* RO */
139 #define   VIA_REG_STAT_TRIGGER_QUEUED	0x08	/* RO */
140 #define   VIA_REG_STAT_STOPPED		0x04	/* RWC */
141 #define   VIA_REG_STAT_EOL		0x02	/* RWC */
142 #define   VIA_REG_STAT_FLAG		0x01	/* RWC */
143 #define VIA_REG_OFFSET_CONTROL		0x01	/* byte - channel control */
144 #define   VIA_REG_CTRL_START		0x80	/* WO */
145 #define   VIA_REG_CTRL_TERMINATE	0x40	/* WO */
146 #define   VIA_REG_CTRL_AUTOSTART	0x20
147 #define   VIA_REG_CTRL_PAUSE		0x08	/* RW */
148 #define   VIA_REG_CTRL_INT_STOP		0x04
149 #define   VIA_REG_CTRL_INT_EOL		0x02
150 #define   VIA_REG_CTRL_INT_FLAG		0x01
151 #define   VIA_REG_CTRL_RESET		0x01	/* RW - probably reset? undocumented */
152 #define   VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
153 #define VIA_REG_OFFSET_TYPE		0x02	/* byte - channel type (686 only) */
154 #define   VIA_REG_TYPE_AUTOSTART	0x80	/* RW - autostart at EOL */
155 #define   VIA_REG_TYPE_16BIT		0x20	/* RW */
156 #define   VIA_REG_TYPE_STEREO		0x10	/* RW */
157 #define   VIA_REG_TYPE_INT_LLINE	0x00
158 #define   VIA_REG_TYPE_INT_LSAMPLE	0x04
159 #define   VIA_REG_TYPE_INT_LESSONE	0x08
160 #define   VIA_REG_TYPE_INT_MASK		0x0c
161 #define   VIA_REG_TYPE_INT_EOL		0x02
162 #define   VIA_REG_TYPE_INT_FLAG		0x01
163 #define VIA_REG_OFFSET_TABLE_PTR	0x04	/* dword - channel table pointer */
164 #define VIA_REG_OFFSET_CURR_PTR		0x04	/* dword - channel current pointer */
165 #define VIA_REG_OFFSET_STOP_IDX		0x08	/* dword - stop index, channel type, sample rate */
166 #define   VIA8233_REG_TYPE_16BIT	0x00200000	/* RW */
167 #define   VIA8233_REG_TYPE_STEREO	0x00100000	/* RW */
168 #define VIA_REG_OFFSET_CURR_COUNT	0x0c	/* dword - channel current count (24 bit) */
169 #define VIA_REG_OFFSET_CURR_INDEX	0x0f	/* byte - channel current index (for via8233 only) */
170 
171 #define DEFINE_VIA_REGSET(name,val) \
172 enum {\
173 	VIA_REG_##name##_STATUS		= (val),\
174 	VIA_REG_##name##_CONTROL	= (val) + 0x01,\
175 	VIA_REG_##name##_TYPE		= (val) + 0x02,\
176 	VIA_REG_##name##_TABLE_PTR	= (val) + 0x04,\
177 	VIA_REG_##name##_CURR_PTR	= (val) + 0x04,\
178 	VIA_REG_##name##_STOP_IDX	= (val) + 0x08,\
179 	VIA_REG_##name##_CURR_COUNT	= (val) + 0x0c,\
180 }
181 
182 /* playback block */
183 DEFINE_VIA_REGSET(PLAYBACK, 0x00);
184 DEFINE_VIA_REGSET(CAPTURE, 0x10);
185 DEFINE_VIA_REGSET(FM, 0x20);
186 
187 /* AC'97 */
188 #define VIA_REG_AC97			0x80	/* dword */
189 #define   VIA_REG_AC97_CODEC_ID_MASK	(3<<30)
190 #define   VIA_REG_AC97_CODEC_ID_SHIFT	30
191 #define   VIA_REG_AC97_CODEC_ID_PRIMARY	0x00
192 #define   VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
193 #define   VIA_REG_AC97_SECONDARY_VALID	(1<<27)
194 #define   VIA_REG_AC97_PRIMARY_VALID	(1<<25)
195 #define   VIA_REG_AC97_BUSY		(1<<24)
196 #define   VIA_REG_AC97_READ		(1<<23)
197 #define   VIA_REG_AC97_CMD_SHIFT	16
198 #define   VIA_REG_AC97_CMD_MASK		0x7e
199 #define   VIA_REG_AC97_DATA_SHIFT	0
200 #define   VIA_REG_AC97_DATA_MASK	0xffff
201 
202 #define VIA_REG_SGD_SHADOW		0x84	/* dword */
203 /* via686 */
204 #define   VIA_REG_SGD_STAT_PB_FLAG	(1<<0)
205 #define   VIA_REG_SGD_STAT_CP_FLAG	(1<<1)
206 #define   VIA_REG_SGD_STAT_FM_FLAG	(1<<2)
207 #define   VIA_REG_SGD_STAT_PB_EOL	(1<<4)
208 #define   VIA_REG_SGD_STAT_CP_EOL	(1<<5)
209 #define   VIA_REG_SGD_STAT_FM_EOL	(1<<6)
210 #define   VIA_REG_SGD_STAT_PB_STOP	(1<<8)
211 #define   VIA_REG_SGD_STAT_CP_STOP	(1<<9)
212 #define   VIA_REG_SGD_STAT_FM_STOP	(1<<10)
213 #define   VIA_REG_SGD_STAT_PB_ACTIVE	(1<<12)
214 #define   VIA_REG_SGD_STAT_CP_ACTIVE	(1<<13)
215 #define   VIA_REG_SGD_STAT_FM_ACTIVE	(1<<14)
216 /* via8233 */
217 #define   VIA8233_REG_SGD_STAT_FLAG	(1<<0)
218 #define   VIA8233_REG_SGD_STAT_EOL	(1<<1)
219 #define   VIA8233_REG_SGD_STAT_STOP	(1<<2)
220 #define   VIA8233_REG_SGD_STAT_ACTIVE	(1<<3)
221 #define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
222 #define   VIA8233_REG_SGD_CHAN_SDX	0
223 #define   VIA8233_REG_SGD_CHAN_MULTI	4
224 #define   VIA8233_REG_SGD_CHAN_REC	6
225 #define   VIA8233_REG_SGD_CHAN_REC1	7
226 
227 #define VIA_REG_GPI_STATUS		0x88
228 #define VIA_REG_GPI_INTR		0x8c
229 
230 /* multi-channel and capture registers for via8233 */
231 DEFINE_VIA_REGSET(MULTPLAY, 0x40);
232 DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
233 
234 /* via8233-specific registers */
235 #define VIA_REG_OFS_PLAYBACK_VOLUME_L	0x02	/* byte */
236 #define VIA_REG_OFS_PLAYBACK_VOLUME_R	0x03	/* byte */
237 #define VIA_REG_OFS_MULTPLAY_FORMAT	0x02	/* byte - format and channels */
238 #define   VIA_REG_MULTPLAY_FMT_8BIT	0x00
239 #define   VIA_REG_MULTPLAY_FMT_16BIT	0x80
240 #define   VIA_REG_MULTPLAY_FMT_CH_MASK	0x70	/* # channels << 4 (valid = 1,2,4,6) */
241 #define VIA_REG_OFS_CAPTURE_FIFO	0x02	/* byte - bit 6 = fifo  enable */
242 #define   VIA_REG_CAPTURE_FIFO_ENABLE	0x40
243 
244 #define VIA_DXS_MAX_VOLUME		31	/* max. volume (attenuation) of reg 0x32/33 */
245 
246 #define VIA_REG_CAPTURE_CHANNEL		0x63	/* byte - input select */
247 #define   VIA_REG_CAPTURE_CHANNEL_MIC	0x4
248 #define   VIA_REG_CAPTURE_CHANNEL_LINE	0
249 #define   VIA_REG_CAPTURE_SELECT_CODEC	0x03	/* recording source codec (0 = primary) */
250 
251 #define VIA_TBL_BIT_FLAG	0x40000000
252 #define VIA_TBL_BIT_EOL		0x80000000
253 
254 /* pci space */
255 #define VIA_ACLINK_STAT		0x40
256 #define  VIA_ACLINK_C11_READY	0x20
257 #define  VIA_ACLINK_C10_READY	0x10
258 #define  VIA_ACLINK_C01_READY	0x04 /* secondary codec ready */
259 #define  VIA_ACLINK_LOWPOWER	0x02 /* low-power state */
260 #define  VIA_ACLINK_C00_READY	0x01 /* primary codec ready */
261 #define VIA_ACLINK_CTRL		0x41
262 #define  VIA_ACLINK_CTRL_ENABLE	0x80 /* 0: disable, 1: enable */
263 #define  VIA_ACLINK_CTRL_RESET	0x40 /* 0: assert, 1: de-assert */
264 #define  VIA_ACLINK_CTRL_SYNC	0x20 /* 0: release SYNC, 1: force SYNC hi */
265 #define  VIA_ACLINK_CTRL_SDO	0x10 /* 0: release SDO, 1: force SDO hi */
266 #define  VIA_ACLINK_CTRL_VRA	0x08 /* 0: disable VRA, 1: enable VRA */
267 #define  VIA_ACLINK_CTRL_PCM	0x04 /* 0: disable PCM, 1: enable PCM */
268 #define  VIA_ACLINK_CTRL_FM	0x02 /* via686 only */
269 #define  VIA_ACLINK_CTRL_SB	0x01 /* via686 only */
270 #define  VIA_ACLINK_CTRL_INIT	(VIA_ACLINK_CTRL_ENABLE|\
271 				 VIA_ACLINK_CTRL_RESET|\
272 				 VIA_ACLINK_CTRL_PCM|\
273 				 VIA_ACLINK_CTRL_VRA)
274 #define VIA_FUNC_ENABLE		0x42
275 #define  VIA_FUNC_MIDI_PNP	0x80 /* FIXME: it's 0x40 in the datasheet! */
276 #define  VIA_FUNC_MIDI_IRQMASK	0x40 /* FIXME: not documented! */
277 #define  VIA_FUNC_RX2C_WRITE	0x20
278 #define  VIA_FUNC_SB_FIFO_EMPTY	0x10
279 #define  VIA_FUNC_ENABLE_GAME	0x08
280 #define  VIA_FUNC_ENABLE_FM	0x04
281 #define  VIA_FUNC_ENABLE_MIDI	0x02
282 #define  VIA_FUNC_ENABLE_SB	0x01
283 #define VIA_PNP_CONTROL		0x43
284 #define VIA_FM_NMI_CTRL		0x48
285 #define VIA8233_VOLCHG_CTRL	0x48
286 #define VIA8233_SPDIF_CTRL	0x49
287 #define  VIA8233_SPDIF_DX3	0x08
288 #define  VIA8233_SPDIF_SLOT_MASK	0x03
289 #define  VIA8233_SPDIF_SLOT_1011	0x00
290 #define  VIA8233_SPDIF_SLOT_34		0x01
291 #define  VIA8233_SPDIF_SLOT_78		0x02
292 #define  VIA8233_SPDIF_SLOT_69		0x03
293 
294 /*
295  */
296 
297 #define VIA_DXS_AUTO	0
298 #define VIA_DXS_ENABLE	1
299 #define VIA_DXS_DISABLE	2
300 #define VIA_DXS_48K	3
301 #define VIA_DXS_NO_VRA	4
302 #define VIA_DXS_SRC	5
303 
304 
305 /*
306  */
307 
308 typedef struct _snd_via82xx via82xx_t;
309 typedef struct via_dev viadev_t;
310 
311 /*
312  * pcm stream
313  */
314 
315 struct snd_via_sg_table {
316 	unsigned int offset;
317 	unsigned int size;
318 } ;
319 
320 #define VIA_TABLE_SIZE	255
321 
322 struct via_dev {
323 	unsigned int reg_offset;
324 	unsigned long port;
325 	int direction;	/* playback = 0, capture = 1 */
326         snd_pcm_substream_t *substream;
327 	int running;
328 	unsigned int tbl_entries; /* # descriptors */
329 	struct snd_dma_buffer table;
330 	struct snd_via_sg_table *idx_table;
331 	/* for recovery from the unexpected pointer */
332 	unsigned int lastpos;
333 	unsigned int fragsize;
334 	unsigned int bufsize;
335 	unsigned int bufsize2;
336 	int hwptr_done;		/* processed frame position in the buffer */
337 	int in_interrupt;
338 	int shadow_shift;
339 };
340 
341 
342 enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
343 enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
344 
345 #define VIA_MAX_DEVS	7	/* 4 playback, 1 multi, 2 capture */
346 
347 struct via_rate_lock {
348 	spinlock_t lock;
349 	int rate;
350 	int used;
351 };
352 
353 struct _snd_via82xx {
354 	int irq;
355 
356 	unsigned long port;
357 	struct resource *mpu_res;
358 	int chip_type;
359 	unsigned char revision;
360 
361 	unsigned char old_legacy;
362 	unsigned char old_legacy_cfg;
363 #ifdef CONFIG_PM
364 	unsigned char legacy_saved;
365 	unsigned char legacy_cfg_saved;
366 	unsigned char spdif_ctrl_saved;
367 	unsigned char capture_src_saved[2];
368 	unsigned int mpu_port_saved;
369 #endif
370 
371 	unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
372 	unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
373 
374 	unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
375 
376 	struct pci_dev *pci;
377 	snd_card_t *card;
378 
379 	unsigned int num_devs;
380 	unsigned int playback_devno, multi_devno, capture_devno;
381 	viadev_t devs[VIA_MAX_DEVS];
382 	struct via_rate_lock rates[2]; /* playback and capture */
383 	unsigned int dxs_fixed: 1;	/* DXS channel accepts only 48kHz */
384 	unsigned int no_vra: 1;		/* no need to set VRA on DXS channels */
385 	unsigned int dxs_src: 1;	/* use full SRC capabilities of DXS */
386 	unsigned int spdif_on: 1;	/* only spdif rates work to external DACs */
387 
388 	snd_pcm_t *pcms[2];
389 	snd_rawmidi_t *rmidi;
390 
391 	ac97_bus_t *ac97_bus;
392 	ac97_t *ac97;
393 	unsigned int ac97_clock;
394 	unsigned int ac97_secondary;	/* secondary AC'97 codec is present */
395 
396 	spinlock_t reg_lock;
397 	snd_info_entry_t *proc_entry;
398 
399 #ifdef SUPPORT_JOYSTICK
400 	struct gameport *gameport;
401 #endif
402 };
403 
404 static struct pci_device_id snd_via82xx_ids[] = {
405 	/* 0x1106, 0x3058 */
406 	{ PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA686, },	/* 686A */
407 	/* 0x1106, 0x3059 */
408 	{ PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA8233, },	/* VT8233 */
409 	{ 0, }
410 };
411 
412 MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
413 
414 /*
415  */
416 
417 /*
418  * allocate and initialize the descriptor buffers
419  * periods = number of periods
420  * fragsize = period size in bytes
421  */
422 static int build_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
423 			   struct pci_dev *pci,
424 			   unsigned int periods, unsigned int fragsize)
425 {
426 	unsigned int i, idx, ofs, rest;
427 	via82xx_t *chip = snd_pcm_substream_chip(substream);
428 	struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
429 
430 	if (dev->table.area == NULL) {
431 		/* the start of each lists must be aligned to 8 bytes,
432 		 * but the kernel pages are much bigger, so we don't care
433 		 */
434 		if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
435 					PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
436 					&dev->table) < 0)
437 			return -ENOMEM;
438 	}
439 	if (! dev->idx_table) {
440 		dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
441 		if (! dev->idx_table)
442 			return -ENOMEM;
443 	}
444 
445 	/* fill the entries */
446 	idx = 0;
447 	ofs = 0;
448 	for (i = 0; i < periods; i++) {
449 		rest = fragsize;
450 		/* fill descriptors for a period.
451 		 * a period can be split to several descriptors if it's
452 		 * over page boundary.
453 		 */
454 		do {
455 			unsigned int r;
456 			unsigned int flag;
457 
458 			if (idx >= VIA_TABLE_SIZE) {
459 				snd_printk(KERN_ERR "via82xx: too much table size!\n");
460 				return -EINVAL;
461 			}
462 			((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
463 			r = PAGE_SIZE - (ofs % PAGE_SIZE);
464 			if (rest < r)
465 				r = rest;
466 			rest -= r;
467 			if (! rest) {
468 				if (i == periods - 1)
469 					flag = VIA_TBL_BIT_EOL; /* buffer boundary */
470 				else
471 					flag = VIA_TBL_BIT_FLAG; /* period boundary */
472 			} else
473 				flag = 0; /* period continues to the next */
474 			// printk("via: tbl %d: at %d  size %d (rest %d)\n", idx, ofs, r, rest);
475 			((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
476 			dev->idx_table[idx].offset = ofs;
477 			dev->idx_table[idx].size = r;
478 			ofs += r;
479 			idx++;
480 		} while (rest > 0);
481 	}
482 	dev->tbl_entries = idx;
483 	dev->bufsize = periods * fragsize;
484 	dev->bufsize2 = dev->bufsize / 2;
485 	dev->fragsize = fragsize;
486 	return 0;
487 }
488 
489 
490 static int clean_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
491 			   struct pci_dev *pci)
492 {
493 	if (dev->table.area) {
494 		snd_dma_free_pages(&dev->table);
495 		dev->table.area = NULL;
496 	}
497 	kfree(dev->idx_table);
498 	dev->idx_table = NULL;
499 	return 0;
500 }
501 
502 /*
503  *  Basic I/O
504  */
505 
506 static inline unsigned int snd_via82xx_codec_xread(via82xx_t *chip)
507 {
508 	return inl(VIAREG(chip, AC97));
509 }
510 
511 static inline void snd_via82xx_codec_xwrite(via82xx_t *chip, unsigned int val)
512 {
513 	outl(val, VIAREG(chip, AC97));
514 }
515 
516 static int snd_via82xx_codec_ready(via82xx_t *chip, int secondary)
517 {
518 	unsigned int timeout = 1000;	/* 1ms */
519 	unsigned int val;
520 
521 	while (timeout-- > 0) {
522 		udelay(1);
523 		if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
524 			return val & 0xffff;
525 	}
526 	snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_via82xx_codec_xread(chip));
527 	return -EIO;
528 }
529 
530 static int snd_via82xx_codec_valid(via82xx_t *chip, int secondary)
531 {
532 	unsigned int timeout = 1000;	/* 1ms */
533 	unsigned int val, val1;
534 	unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
535 					 VIA_REG_AC97_SECONDARY_VALID;
536 
537 	while (timeout-- > 0) {
538 		val = snd_via82xx_codec_xread(chip);
539 		val1 = val & (VIA_REG_AC97_BUSY | stat);
540 		if (val1 == stat)
541 			return val & 0xffff;
542 		udelay(1);
543 	}
544 	return -EIO;
545 }
546 
547 static void snd_via82xx_codec_wait(ac97_t *ac97)
548 {
549 	via82xx_t *chip = ac97->private_data;
550 	int err;
551 	err = snd_via82xx_codec_ready(chip, ac97->num);
552 	/* here we need to wait fairly for long time.. */
553 	msleep(500);
554 }
555 
556 static void snd_via82xx_codec_write(ac97_t *ac97,
557 				    unsigned short reg,
558 				    unsigned short val)
559 {
560 	via82xx_t *chip = ac97->private_data;
561 	unsigned int xval;
562 
563 	xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
564 	xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
565 	xval |= reg << VIA_REG_AC97_CMD_SHIFT;
566 	xval |= val << VIA_REG_AC97_DATA_SHIFT;
567 	snd_via82xx_codec_xwrite(chip, xval);
568 	snd_via82xx_codec_ready(chip, ac97->num);
569 }
570 
571 static unsigned short snd_via82xx_codec_read(ac97_t *ac97, unsigned short reg)
572 {
573 	via82xx_t *chip = ac97->private_data;
574 	unsigned int xval, val = 0xffff;
575 	int again = 0;
576 
577 	xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
578 	xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
579 	xval |= VIA_REG_AC97_READ;
580 	xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
581       	while (1) {
582       		if (again++ > 3) {
583 			snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n", ac97->num, snd_via82xx_codec_xread(chip));
584 		      	return 0xffff;
585 		}
586 		snd_via82xx_codec_xwrite(chip, xval);
587 		udelay (20);
588 		if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
589 			udelay(25);
590 			val = snd_via82xx_codec_xread(chip);
591 			break;
592 		}
593 	}
594 	return val & 0xffff;
595 }
596 
597 static void snd_via82xx_channel_reset(via82xx_t *chip, viadev_t *viadev)
598 {
599 	outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
600 	     VIADEV_REG(viadev, OFFSET_CONTROL));
601 	inb(VIADEV_REG(viadev, OFFSET_CONTROL));
602 	udelay(50);
603 	/* disable interrupts */
604 	outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
605 	/* clear interrupts */
606 	outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
607 	outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
608 	// outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
609 	viadev->lastpos = 0;
610 	viadev->hwptr_done = 0;
611 }
612 
613 
614 /*
615  *  Interrupt handler
616  *  Used for 686 and 8233A
617  */
618 static irqreturn_t snd_via686_interrupt(int irq, void *dev_id, struct pt_regs *regs)
619 {
620 	via82xx_t *chip = dev_id;
621 	unsigned int status;
622 	unsigned int i;
623 
624 	status = inl(VIAREG(chip, SGD_SHADOW));
625 	if (! (status & chip->intr_mask)) {
626 		if (chip->rmidi)
627 			/* check mpu401 interrupt */
628 			return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
629 		return IRQ_NONE;
630 	}
631 
632 	/* check status for each stream */
633 	spin_lock(&chip->reg_lock);
634 	for (i = 0; i < chip->num_devs; i++) {
635 		viadev_t *viadev = &chip->devs[i];
636 		unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
637 		if (! (c_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED)))
638 			continue;
639 		if (viadev->substream && viadev->running) {
640 			/*
641 			 * Update hwptr_done based on 'period elapsed'
642 			 * interrupts. We'll use it, when the chip returns 0
643 			 * for OFFSET_CURR_COUNT.
644 			 */
645 			if (c_status & VIA_REG_STAT_EOL)
646 				viadev->hwptr_done = 0;
647 			else
648 				viadev->hwptr_done += viadev->fragsize;
649 			viadev->in_interrupt = c_status;
650 			spin_unlock(&chip->reg_lock);
651 			snd_pcm_period_elapsed(viadev->substream);
652 			spin_lock(&chip->reg_lock);
653 			viadev->in_interrupt = 0;
654 		}
655 		outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
656 	}
657 	spin_unlock(&chip->reg_lock);
658 	return IRQ_HANDLED;
659 }
660 
661 /*
662  *  Interrupt handler
663  */
664 static irqreturn_t snd_via8233_interrupt(int irq, void *dev_id, struct pt_regs *regs)
665 {
666 	via82xx_t *chip = dev_id;
667 	unsigned int status;
668 	unsigned int i;
669 	int irqreturn = 0;
670 
671 	/* check status for each stream */
672 	spin_lock(&chip->reg_lock);
673 	status = inl(VIAREG(chip, SGD_SHADOW));
674 
675 	for (i = 0; i < chip->num_devs; i++) {
676 		viadev_t *viadev = &chip->devs[i];
677 		snd_pcm_substream_t *substream;
678 		unsigned char c_status, shadow_status;
679 
680 		shadow_status = (status >> viadev->shadow_shift) &
681 			(VIA8233_SHADOW_STAT_ACTIVE|VIA_REG_STAT_EOL|
682 			 VIA_REG_STAT_FLAG);
683 		c_status = shadow_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG);
684 		if (!c_status)
685 			continue;
686 
687 		substream = viadev->substream;
688 		if (substream && viadev->running) {
689 			/*
690 			 * Update hwptr_done based on 'period elapsed'
691 			 * interrupts. We'll use it, when the chip returns 0
692 			 * for OFFSET_CURR_COUNT.
693 			 */
694 			if (c_status & VIA_REG_STAT_EOL)
695 				viadev->hwptr_done = 0;
696 			else
697 				viadev->hwptr_done += viadev->fragsize;
698 			viadev->in_interrupt = c_status;
699 			if (shadow_status & VIA8233_SHADOW_STAT_ACTIVE)
700 				viadev->in_interrupt |= VIA_REG_STAT_ACTIVE;
701 			spin_unlock(&chip->reg_lock);
702 
703 			snd_pcm_period_elapsed(substream);
704 
705 			spin_lock(&chip->reg_lock);
706 			viadev->in_interrupt = 0;
707 		}
708 		outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
709 		irqreturn = 1;
710 	}
711 	spin_unlock(&chip->reg_lock);
712 	return IRQ_RETVAL(irqreturn);
713 }
714 
715 /*
716  *  PCM callbacks
717  */
718 
719 /*
720  * trigger callback
721  */
722 static int snd_via82xx_pcm_trigger(snd_pcm_substream_t * substream, int cmd)
723 {
724 	via82xx_t *chip = snd_pcm_substream_chip(substream);
725 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
726 	unsigned char val;
727 
728 	if (chip->chip_type != TYPE_VIA686)
729 		val = VIA_REG_CTRL_INT;
730 	else
731 		val = 0;
732 	switch (cmd) {
733 	case SNDRV_PCM_TRIGGER_START:
734 	case SNDRV_PCM_TRIGGER_RESUME:
735 		val |= VIA_REG_CTRL_START;
736 		viadev->running = 1;
737 		break;
738 	case SNDRV_PCM_TRIGGER_STOP:
739 	case SNDRV_PCM_TRIGGER_SUSPEND:
740 		val = VIA_REG_CTRL_TERMINATE;
741 		viadev->running = 0;
742 		break;
743 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
744 		val |= VIA_REG_CTRL_PAUSE;
745 		viadev->running = 0;
746 		break;
747 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
748 		viadev->running = 1;
749 		break;
750 	default:
751 		return -EINVAL;
752 	}
753 	outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
754 	if (cmd == SNDRV_PCM_TRIGGER_STOP)
755 		snd_via82xx_channel_reset(chip, viadev);
756 	return 0;
757 }
758 
759 
760 /*
761  * pointer callbacks
762  */
763 
764 /*
765  * calculate the linear position at the given sg-buffer index and the rest count
766  */
767 
768 #define check_invalid_pos(viadev,pos) \
769 	((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 || viadev->lastpos < viadev->bufsize2))
770 
771 static inline unsigned int calc_linear_pos(viadev_t *viadev, unsigned int idx, unsigned int count)
772 {
773 	unsigned int size, base, res;
774 
775 	size = viadev->idx_table[idx].size;
776 	base = viadev->idx_table[idx].offset;
777 	res = base + size - count;
778 	if (res >= viadev->bufsize)
779 		res -= viadev->bufsize;
780 
781 	/* check the validity of the calculated position */
782 	if (size < count) {
783 		snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n", (int)size, (int)count);
784 		res = viadev->lastpos;
785 	} else {
786 		if (! count) {
787 			/* Some mobos report count = 0 on the DMA boundary,
788 			 * i.e. count = size indeed.
789 			 * Let's check whether this step is above the expected size.
790 			 */
791 			int delta = res - viadev->lastpos;
792 			if (delta < 0)
793 				delta += viadev->bufsize;
794 			if ((unsigned int)delta > viadev->fragsize)
795 				res = base;
796 		}
797 		if (check_invalid_pos(viadev, res)) {
798 #ifdef POINTER_DEBUG
799 			printk(KERN_DEBUG "fail: idx = %i/%i, lastpos = 0x%x, bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, count = 0x%x\n", idx, viadev->tbl_entries, viadev->lastpos, viadev->bufsize2, viadev->idx_table[idx].offset, viadev->idx_table[idx].size, count);
800 #endif
801 			/* count register returns full size when end of buffer is reached */
802 			res = base + size;
803 			if (check_invalid_pos(viadev, res)) {
804 				snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), using last valid pointer\n");
805 				res = viadev->lastpos;
806 			}
807 		}
808 	}
809 	return res;
810 }
811 
812 /*
813  * get the current pointer on via686
814  */
815 static snd_pcm_uframes_t snd_via686_pcm_pointer(snd_pcm_substream_t *substream)
816 {
817 	via82xx_t *chip = snd_pcm_substream_chip(substream);
818 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
819 	unsigned int idx, ptr, count, res;
820 
821 	snd_assert(viadev->tbl_entries, return 0);
822 	if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
823 		return 0;
824 
825 	spin_lock(&chip->reg_lock);
826 	count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
827 	/* The via686a does not have the current index register,
828 	 * so we need to calculate the index from CURR_PTR.
829 	 */
830 	ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
831 	if (ptr <= (unsigned int)viadev->table.addr)
832 		idx = 0;
833 	else /* CURR_PTR holds the address + 8 */
834 		idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
835 	res = calc_linear_pos(viadev, idx, count);
836 	viadev->lastpos = res; /* remember the last position */
837 	spin_unlock(&chip->reg_lock);
838 
839 	return bytes_to_frames(substream->runtime, res);
840 }
841 
842 /*
843  * get the current pointer on via823x
844  */
845 static snd_pcm_uframes_t snd_via8233_pcm_pointer(snd_pcm_substream_t *substream)
846 {
847 	via82xx_t *chip = snd_pcm_substream_chip(substream);
848 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
849 	unsigned int idx, count, res;
850 	int status;
851 
852 	snd_assert(viadev->tbl_entries, return 0);
853 
854 	spin_lock(&chip->reg_lock);
855 	count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
856 	status = viadev->in_interrupt;
857 	if (!status)
858 		status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
859 
860 	if (!(status & VIA_REG_STAT_ACTIVE)) {
861 		res = 0;
862 		goto unlock;
863 	}
864 	if (count & 0xffffff) {
865 		idx = count >> 24;
866 		if (idx >= viadev->tbl_entries) {
867 #ifdef POINTER_DEBUG
868 			printk("fail: invalid idx = %i/%i\n", idx, viadev->tbl_entries);
869 #endif
870 			res = viadev->lastpos;
871 		} else {
872 			count &= 0xffffff;
873 			res = calc_linear_pos(viadev, idx, count);
874 		}
875 	} else {
876 		res = viadev->hwptr_done;
877 		if (!viadev->in_interrupt) {
878 			if (status & VIA_REG_STAT_EOL) {
879 				res = 0;
880 			} else
881 				if (status & VIA_REG_STAT_FLAG) {
882 					res += viadev->fragsize;
883 				}
884 		}
885 	}
886 unlock:
887 	viadev->lastpos = res;
888 	spin_unlock(&chip->reg_lock);
889 
890 	return bytes_to_frames(substream->runtime, res);
891 }
892 
893 
894 /*
895  * hw_params callback:
896  * allocate the buffer and build up the buffer description table
897  */
898 static int snd_via82xx_hw_params(snd_pcm_substream_t * substream,
899 				 snd_pcm_hw_params_t * hw_params)
900 {
901 	via82xx_t *chip = snd_pcm_substream_chip(substream);
902 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
903 	int err;
904 
905 	err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
906 	if (err < 0)
907 		return err;
908 	err = build_via_table(viadev, substream, chip->pci,
909 			      params_periods(hw_params),
910 			      params_period_bytes(hw_params));
911 	if (err < 0)
912 		return err;
913 
914 	return 0;
915 }
916 
917 /*
918  * hw_free callback:
919  * clean up the buffer description table and release the buffer
920  */
921 static int snd_via82xx_hw_free(snd_pcm_substream_t * substream)
922 {
923 	via82xx_t *chip = snd_pcm_substream_chip(substream);
924 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
925 
926 	clean_via_table(viadev, substream, chip->pci);
927 	snd_pcm_lib_free_pages(substream);
928 	return 0;
929 }
930 
931 
932 /*
933  * set up the table pointer
934  */
935 static void snd_via82xx_set_table_ptr(via82xx_t *chip, viadev_t *viadev)
936 {
937 	snd_via82xx_codec_ready(chip, 0);
938 	outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
939 	udelay(20);
940 	snd_via82xx_codec_ready(chip, 0);
941 }
942 
943 /*
944  * prepare callback for playback and capture on via686
945  */
946 static void via686_setup_format(via82xx_t *chip, viadev_t *viadev, snd_pcm_runtime_t *runtime)
947 {
948 	snd_via82xx_channel_reset(chip, viadev);
949 	/* this must be set after channel_reset */
950 	snd_via82xx_set_table_ptr(chip, viadev);
951 	outb(VIA_REG_TYPE_AUTOSTART |
952 	     (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
953 	     (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
954 	     ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
955 	     VIA_REG_TYPE_INT_EOL |
956 	     VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
957 }
958 
959 static int snd_via686_playback_prepare(snd_pcm_substream_t *substream)
960 {
961 	via82xx_t *chip = snd_pcm_substream_chip(substream);
962 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
963 	snd_pcm_runtime_t *runtime = substream->runtime;
964 
965 	snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
966 	snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
967 	via686_setup_format(chip, viadev, runtime);
968 	return 0;
969 }
970 
971 static int snd_via686_capture_prepare(snd_pcm_substream_t *substream)
972 {
973 	via82xx_t *chip = snd_pcm_substream_chip(substream);
974 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
975 	snd_pcm_runtime_t *runtime = substream->runtime;
976 
977 	snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
978 	via686_setup_format(chip, viadev, runtime);
979 	return 0;
980 }
981 
982 /*
983  * lock the current rate
984  */
985 static int via_lock_rate(struct via_rate_lock *rec, int rate)
986 {
987 	int changed = 0;
988 
989 	spin_lock_irq(&rec->lock);
990 	if (rec->rate != rate) {
991 		if (rec->rate && rec->used > 1) /* already set */
992 			changed = -EINVAL;
993 		else {
994 			rec->rate = rate;
995 			changed = 1;
996 		}
997 	}
998 	spin_unlock_irq(&rec->lock);
999 	return changed;
1000 }
1001 
1002 /*
1003  * prepare callback for DSX playback on via823x
1004  */
1005 static int snd_via8233_playback_prepare(snd_pcm_substream_t *substream)
1006 {
1007 	via82xx_t *chip = snd_pcm_substream_chip(substream);
1008 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
1009 	snd_pcm_runtime_t *runtime = substream->runtime;
1010 	int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
1011 	int rate_changed;
1012 	u32 rbits;
1013 
1014 	if ((rate_changed = via_lock_rate(&chip->rates[0], ac97_rate)) < 0)
1015 		return rate_changed;
1016 	if (rate_changed)
1017 		snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
1018 				  chip->no_vra ? 48000 : runtime->rate);
1019 	if (chip->spdif_on && viadev->reg_offset == 0x30)
1020 		snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1021 
1022 	if (runtime->rate == 48000)
1023 		rbits = 0xfffff;
1024 	else
1025 		rbits = (0x100000 / 48000) * runtime->rate + ((0x100000 % 48000) * runtime->rate) / 48000;
1026 	snd_assert((rbits & ~0xfffff) == 0, return -EINVAL);
1027 	snd_via82xx_channel_reset(chip, viadev);
1028 	snd_via82xx_set_table_ptr(chip, viadev);
1029 	outb(chip->playback_volume[viadev->reg_offset / 0x10][0], VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
1030 	outb(chip->playback_volume[viadev->reg_offset / 0x10][1], VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
1031 	outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
1032 	     (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
1033 	     rbits | /* rate */
1034 	     0xff000000,    /* STOP index is never reached */
1035 	     VIADEV_REG(viadev, OFFSET_STOP_IDX));
1036 	udelay(20);
1037 	snd_via82xx_codec_ready(chip, 0);
1038 	return 0;
1039 }
1040 
1041 /*
1042  * prepare callback for multi-channel playback on via823x
1043  */
1044 static int snd_via8233_multi_prepare(snd_pcm_substream_t *substream)
1045 {
1046 	via82xx_t *chip = snd_pcm_substream_chip(substream);
1047 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
1048 	snd_pcm_runtime_t *runtime = substream->runtime;
1049 	unsigned int slots;
1050 	int fmt;
1051 
1052 	if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
1053 		return -EINVAL;
1054 	snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
1055 	snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
1056 	snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
1057 	snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
1058 	snd_via82xx_channel_reset(chip, viadev);
1059 	snd_via82xx_set_table_ptr(chip, viadev);
1060 
1061 	fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ? VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
1062 	fmt |= runtime->channels << 4;
1063 	outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
1064 #if 0
1065 	if (chip->revision == VIA_REV_8233A)
1066 		slots = 0;
1067 	else
1068 #endif
1069 	{
1070 		/* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
1071 		/* corresponding to FL, FR, RL, RR, C, LFE ?? */
1072 		switch (runtime->channels) {
1073 		case 1: slots = (1<<0) | (1<<4); break;
1074 		case 2: slots = (1<<0) | (2<<4); break;
1075 		case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
1076 		case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
1077 		case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
1078 		case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
1079 		default: slots = 0; break;
1080 		}
1081 	}
1082 	/* STOP index is never reached */
1083 	outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
1084 	udelay(20);
1085 	snd_via82xx_codec_ready(chip, 0);
1086 	return 0;
1087 }
1088 
1089 /*
1090  * prepare callback for capture on via823x
1091  */
1092 static int snd_via8233_capture_prepare(snd_pcm_substream_t *substream)
1093 {
1094 	via82xx_t *chip = snd_pcm_substream_chip(substream);
1095 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
1096 	snd_pcm_runtime_t *runtime = substream->runtime;
1097 
1098 	if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1099 		return -EINVAL;
1100 	snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1101 	snd_via82xx_channel_reset(chip, viadev);
1102 	snd_via82xx_set_table_ptr(chip, viadev);
1103 	outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1104 	outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1105 	     (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1106 	     0xff000000,    /* STOP index is never reached */
1107 	     VIADEV_REG(viadev, OFFSET_STOP_IDX));
1108 	udelay(20);
1109 	snd_via82xx_codec_ready(chip, 0);
1110 	return 0;
1111 }
1112 
1113 
1114 /*
1115  * pcm hardware definition, identical for both playback and capture
1116  */
1117 static snd_pcm_hardware_t snd_via82xx_hw =
1118 {
1119 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1120 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1121 				 SNDRV_PCM_INFO_MMAP_VALID |
1122 				 /* SNDRV_PCM_INFO_RESUME | */
1123 				 SNDRV_PCM_INFO_PAUSE),
1124 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1125 	.rates =		SNDRV_PCM_RATE_48000,
1126 	.rate_min =		48000,
1127 	.rate_max =		48000,
1128 	.channels_min =		1,
1129 	.channels_max =		2,
1130 	.buffer_bytes_max =	128 * 1024,
1131 	.period_bytes_min =	32,
1132 	.period_bytes_max =	128 * 1024,
1133 	.periods_min =		2,
1134 	.periods_max =		VIA_TABLE_SIZE / 2,
1135 	.fifo_size =		0,
1136 };
1137 
1138 
1139 /*
1140  * open callback skeleton
1141  */
1142 static int snd_via82xx_pcm_open(via82xx_t *chip, viadev_t *viadev, snd_pcm_substream_t * substream)
1143 {
1144 	snd_pcm_runtime_t *runtime = substream->runtime;
1145 	int err;
1146 	struct via_rate_lock *ratep;
1147 
1148 	runtime->hw = snd_via82xx_hw;
1149 
1150 	/* set the hw rate condition */
1151 	ratep = &chip->rates[viadev->direction];
1152 	spin_lock_irq(&ratep->lock);
1153 	ratep->used++;
1154 	if (chip->spdif_on && viadev->reg_offset == 0x30) {
1155 		/* DXS#3 and spdif is on */
1156 		runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
1157 		snd_pcm_limit_hw_rates(runtime);
1158 	} else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1159 		/* fixed DXS playback rate */
1160 		runtime->hw.rates = SNDRV_PCM_RATE_48000;
1161 		runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1162 	} else if (chip->dxs_src && viadev->reg_offset < 0x40) {
1163 		/* use full SRC capabilities of DXS */
1164 		runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
1165 				     SNDRV_PCM_RATE_8000_48000);
1166 		runtime->hw.rate_min = 8000;
1167 		runtime->hw.rate_max = 48000;
1168 	} else if (! ratep->rate) {
1169 		int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1170 		runtime->hw.rates = chip->ac97->rates[idx];
1171 		snd_pcm_limit_hw_rates(runtime);
1172 	} else {
1173 		/* a fixed rate */
1174 		runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1175 		runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1176 	}
1177 	spin_unlock_irq(&ratep->lock);
1178 
1179 	/* we may remove following constaint when we modify table entries
1180 	   in interrupt */
1181 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1182 		return err;
1183 
1184 	runtime->private_data = viadev;
1185 	viadev->substream = substream;
1186 
1187 	return 0;
1188 }
1189 
1190 
1191 /*
1192  * open callback for playback on via686 and via823x DSX
1193  */
1194 static int snd_via82xx_playback_open(snd_pcm_substream_t * substream)
1195 {
1196 	via82xx_t *chip = snd_pcm_substream_chip(substream);
1197 	viadev_t *viadev = &chip->devs[chip->playback_devno + substream->number];
1198 	int err;
1199 
1200 	if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1201 		return err;
1202 	return 0;
1203 }
1204 
1205 /*
1206  * open callback for playback on via823x multi-channel
1207  */
1208 static int snd_via8233_multi_open(snd_pcm_substream_t * substream)
1209 {
1210 	via82xx_t *chip = snd_pcm_substream_chip(substream);
1211 	viadev_t *viadev = &chip->devs[chip->multi_devno];
1212 	int err;
1213 	/* channels constraint for VIA8233A
1214 	 * 3 and 5 channels are not supported
1215 	 */
1216 	static unsigned int channels[] = {
1217 		1, 2, 4, 6
1218 	};
1219 	static snd_pcm_hw_constraint_list_t hw_constraints_channels = {
1220 		.count = ARRAY_SIZE(channels),
1221 		.list = channels,
1222 		.mask = 0,
1223 	};
1224 
1225 	if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1226 		return err;
1227 	substream->runtime->hw.channels_max = 6;
1228 	if (chip->revision == VIA_REV_8233A)
1229 		snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels);
1230 	return 0;
1231 }
1232 
1233 /*
1234  * open callback for capture on via686 and via823x
1235  */
1236 static int snd_via82xx_capture_open(snd_pcm_substream_t * substream)
1237 {
1238 	via82xx_t *chip = snd_pcm_substream_chip(substream);
1239 	viadev_t *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
1240 
1241 	return snd_via82xx_pcm_open(chip, viadev, substream);
1242 }
1243 
1244 /*
1245  * close callback
1246  */
1247 static int snd_via82xx_pcm_close(snd_pcm_substream_t * substream)
1248 {
1249 	via82xx_t *chip = snd_pcm_substream_chip(substream);
1250 	viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
1251 	struct via_rate_lock *ratep;
1252 
1253 	/* release the rate lock */
1254 	ratep = &chip->rates[viadev->direction];
1255 	spin_lock_irq(&ratep->lock);
1256 	ratep->used--;
1257 	if (! ratep->used)
1258 		ratep->rate = 0;
1259 	spin_unlock_irq(&ratep->lock);
1260 
1261 	viadev->substream = NULL;
1262 	return 0;
1263 }
1264 
1265 
1266 /* via686 playback callbacks */
1267 static snd_pcm_ops_t snd_via686_playback_ops = {
1268 	.open =		snd_via82xx_playback_open,
1269 	.close =	snd_via82xx_pcm_close,
1270 	.ioctl =	snd_pcm_lib_ioctl,
1271 	.hw_params =	snd_via82xx_hw_params,
1272 	.hw_free =	snd_via82xx_hw_free,
1273 	.prepare =	snd_via686_playback_prepare,
1274 	.trigger =	snd_via82xx_pcm_trigger,
1275 	.pointer =	snd_via686_pcm_pointer,
1276 	.page =		snd_pcm_sgbuf_ops_page,
1277 };
1278 
1279 /* via686 capture callbacks */
1280 static snd_pcm_ops_t snd_via686_capture_ops = {
1281 	.open =		snd_via82xx_capture_open,
1282 	.close =	snd_via82xx_pcm_close,
1283 	.ioctl =	snd_pcm_lib_ioctl,
1284 	.hw_params =	snd_via82xx_hw_params,
1285 	.hw_free =	snd_via82xx_hw_free,
1286 	.prepare =	snd_via686_capture_prepare,
1287 	.trigger =	snd_via82xx_pcm_trigger,
1288 	.pointer =	snd_via686_pcm_pointer,
1289 	.page =		snd_pcm_sgbuf_ops_page,
1290 };
1291 
1292 /* via823x DSX playback callbacks */
1293 static snd_pcm_ops_t snd_via8233_playback_ops = {
1294 	.open =		snd_via82xx_playback_open,
1295 	.close =	snd_via82xx_pcm_close,
1296 	.ioctl =	snd_pcm_lib_ioctl,
1297 	.hw_params =	snd_via82xx_hw_params,
1298 	.hw_free =	snd_via82xx_hw_free,
1299 	.prepare =	snd_via8233_playback_prepare,
1300 	.trigger =	snd_via82xx_pcm_trigger,
1301 	.pointer =	snd_via8233_pcm_pointer,
1302 	.page =		snd_pcm_sgbuf_ops_page,
1303 };
1304 
1305 /* via823x multi-channel playback callbacks */
1306 static snd_pcm_ops_t snd_via8233_multi_ops = {
1307 	.open =		snd_via8233_multi_open,
1308 	.close =	snd_via82xx_pcm_close,
1309 	.ioctl =	snd_pcm_lib_ioctl,
1310 	.hw_params =	snd_via82xx_hw_params,
1311 	.hw_free =	snd_via82xx_hw_free,
1312 	.prepare =	snd_via8233_multi_prepare,
1313 	.trigger =	snd_via82xx_pcm_trigger,
1314 	.pointer =	snd_via8233_pcm_pointer,
1315 	.page =		snd_pcm_sgbuf_ops_page,
1316 };
1317 
1318 /* via823x capture callbacks */
1319 static snd_pcm_ops_t snd_via8233_capture_ops = {
1320 	.open =		snd_via82xx_capture_open,
1321 	.close =	snd_via82xx_pcm_close,
1322 	.ioctl =	snd_pcm_lib_ioctl,
1323 	.hw_params =	snd_via82xx_hw_params,
1324 	.hw_free =	snd_via82xx_hw_free,
1325 	.prepare =	snd_via8233_capture_prepare,
1326 	.trigger =	snd_via82xx_pcm_trigger,
1327 	.pointer =	snd_via8233_pcm_pointer,
1328 	.page =		snd_pcm_sgbuf_ops_page,
1329 };
1330 
1331 
1332 static void init_viadev(via82xx_t *chip, int idx, unsigned int reg_offset, int shadow_pos, int direction)
1333 {
1334 	chip->devs[idx].reg_offset = reg_offset;
1335 	chip->devs[idx].shadow_shift = shadow_pos * 4;
1336 	chip->devs[idx].direction = direction;
1337 	chip->devs[idx].port = chip->port + reg_offset;
1338 }
1339 
1340 /*
1341  * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
1342  */
1343 static int __devinit snd_via8233_pcm_new(via82xx_t *chip)
1344 {
1345 	snd_pcm_t *pcm;
1346 	int i, err;
1347 
1348 	chip->playback_devno = 0;	/* x 4 */
1349 	chip->multi_devno = 4;		/* x 1 */
1350 	chip->capture_devno = 5;	/* x 2 */
1351 	chip->num_devs = 7;
1352 	chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
1353 
1354 	/* PCM #0:  4 DSX playbacks and 1 capture */
1355 	err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1356 	if (err < 0)
1357 		return err;
1358 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1359 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1360 	pcm->private_data = chip;
1361 	strcpy(pcm->name, chip->card->shortname);
1362 	chip->pcms[0] = pcm;
1363 	/* set up playbacks */
1364 	for (i = 0; i < 4; i++)
1365 		init_viadev(chip, i, 0x10 * i, i, 0);
1366 	/* capture */
1367 	init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
1368 
1369 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1370 							 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1371 		return err;
1372 
1373 	/* PCM #1:  multi-channel playback and 2nd capture */
1374 	err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1375 	if (err < 0)
1376 		return err;
1377 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1378 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1379 	pcm->private_data = chip;
1380 	strcpy(pcm->name, chip->card->shortname);
1381 	chip->pcms[1] = pcm;
1382 	/* set up playback */
1383 	init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
1384 	/* set up capture */
1385 	init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 7, 1);
1386 
1387 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1388 						         snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1389 		return err;
1390 
1391 	return 0;
1392 }
1393 
1394 /*
1395  * create pcm instances for VIA8233A
1396  */
1397 static int __devinit snd_via8233a_pcm_new(via82xx_t *chip)
1398 {
1399 	snd_pcm_t *pcm;
1400 	int err;
1401 
1402 	chip->multi_devno = 0;
1403 	chip->playback_devno = 1;
1404 	chip->capture_devno = 2;
1405 	chip->num_devs = 3;
1406 	chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
1407 
1408 	/* PCM #0:  multi-channel playback and capture */
1409 	err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1410 	if (err < 0)
1411 		return err;
1412 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1413 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1414 	pcm->private_data = chip;
1415 	strcpy(pcm->name, chip->card->shortname);
1416 	chip->pcms[0] = pcm;
1417 	/* set up playback */
1418 	init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
1419 	/* capture */
1420 	init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
1421 
1422 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1423 							 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1424 		return err;
1425 
1426 	/* SPDIF supported? */
1427 	if (! ac97_can_spdif(chip->ac97))
1428 		return 0;
1429 
1430 	/* PCM #1:  DXS3 playback (for spdif) */
1431 	err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1432 	if (err < 0)
1433 		return err;
1434 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1435 	pcm->private_data = chip;
1436 	strcpy(pcm->name, chip->card->shortname);
1437 	chip->pcms[1] = pcm;
1438 	/* set up playback */
1439 	init_viadev(chip, chip->playback_devno, 0x30, 3, 0);
1440 
1441 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1442 							 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1443 		return err;
1444 
1445 	return 0;
1446 }
1447 
1448 /*
1449  * create a pcm instance for via686a/b
1450  */
1451 static int __devinit snd_via686_pcm_new(via82xx_t *chip)
1452 {
1453 	snd_pcm_t *pcm;
1454 	int err;
1455 
1456 	chip->playback_devno = 0;
1457 	chip->capture_devno = 1;
1458 	chip->num_devs = 2;
1459 	chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
1460 
1461 	err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1462 	if (err < 0)
1463 		return err;
1464 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1465 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1466 	pcm->private_data = chip;
1467 	strcpy(pcm->name, chip->card->shortname);
1468 	chip->pcms[0] = pcm;
1469 	init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0, 0);
1470 	init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 0, 1);
1471 
1472 	if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1473 							 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1474 		return err;
1475 
1476 	return 0;
1477 }
1478 
1479 
1480 /*
1481  *  Mixer part
1482  */
1483 
1484 static int snd_via8233_capture_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1485 {
1486 	/* formerly they were "Line" and "Mic", but it looks like that they
1487 	 * have nothing to do with the actual physical connections...
1488 	 */
1489 	static char *texts[2] = {
1490 		"Input1", "Input2"
1491 	};
1492 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1493 	uinfo->count = 1;
1494 	uinfo->value.enumerated.items = 2;
1495 	if (uinfo->value.enumerated.item >= 2)
1496 		uinfo->value.enumerated.item = 1;
1497 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1498 	return 0;
1499 }
1500 
1501 static int snd_via8233_capture_source_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1502 {
1503 	via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1504 	unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1505 	ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1506 	return 0;
1507 }
1508 
1509 static int snd_via8233_capture_source_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1510 {
1511 	via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1512 	unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1513 	u8 val, oval;
1514 
1515 	spin_lock_irq(&chip->reg_lock);
1516 	oval = inb(port);
1517 	val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1518 	if (ucontrol->value.enumerated.item[0])
1519 		val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1520 	if (val != oval)
1521 		outb(val, port);
1522 	spin_unlock_irq(&chip->reg_lock);
1523 	return val != oval;
1524 }
1525 
1526 static snd_kcontrol_new_t snd_via8233_capture_source __devinitdata = {
1527 	.name = "Input Source Select",
1528 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1529 	.info = snd_via8233_capture_source_info,
1530 	.get = snd_via8233_capture_source_get,
1531 	.put = snd_via8233_capture_source_put,
1532 };
1533 
1534 static int snd_via8233_dxs3_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1535 {
1536 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1537 	uinfo->count = 1;
1538 	uinfo->value.integer.min = 0;
1539 	uinfo->value.integer.max = 1;
1540 	return 0;
1541 }
1542 
1543 static int snd_via8233_dxs3_spdif_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1544 {
1545 	via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1546 	u8 val;
1547 
1548 	pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1549 	ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1550 	return 0;
1551 }
1552 
1553 static int snd_via8233_dxs3_spdif_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1554 {
1555 	via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1556 	u8 val, oval;
1557 
1558 	pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1559 	val = oval & ~VIA8233_SPDIF_DX3;
1560 	if (ucontrol->value.integer.value[0])
1561 		val |= VIA8233_SPDIF_DX3;
1562 	/* save the spdif flag for rate filtering */
1563 	chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1564 	if (val != oval) {
1565 		pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1566 		return 1;
1567 	}
1568 	return 0;
1569 }
1570 
1571 static snd_kcontrol_new_t snd_via8233_dxs3_spdif_control __devinitdata = {
1572 	.name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
1573 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1574 	.info = snd_via8233_dxs3_spdif_info,
1575 	.get = snd_via8233_dxs3_spdif_get,
1576 	.put = snd_via8233_dxs3_spdif_put,
1577 };
1578 
1579 static int snd_via8233_dxs_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1580 {
1581 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1582 	uinfo->count = 2;
1583 	uinfo->value.integer.min = 0;
1584 	uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1585 	return 0;
1586 }
1587 
1588 static int snd_via8233_dxs_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1589 {
1590 	via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1591 	unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1592 
1593 	ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1594 	ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1595 	return 0;
1596 }
1597 
1598 static int snd_via8233_pcmdxs_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1599 {
1600 	via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1601 	ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
1602 	ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
1603 	return 0;
1604 }
1605 
1606 static int snd_via8233_dxs_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1607 {
1608 	via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1609 	unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1610 	unsigned long port = chip->port + 0x10 * idx;
1611 	unsigned char val;
1612 	int i, change = 0;
1613 
1614 	for (i = 0; i < 2; i++) {
1615 		val = ucontrol->value.integer.value[i];
1616 		if (val > VIA_DXS_MAX_VOLUME)
1617 			val = VIA_DXS_MAX_VOLUME;
1618 		val = VIA_DXS_MAX_VOLUME - val;
1619 		change |= val != chip->playback_volume[idx][i];
1620 		if (change) {
1621 			chip->playback_volume[idx][i] = val;
1622 			outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1623 		}
1624 	}
1625 	return change;
1626 }
1627 
1628 static int snd_via8233_pcmdxs_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1629 {
1630 	via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1631 	unsigned int idx;
1632 	unsigned char val;
1633 	int i, change = 0;
1634 
1635 	for (i = 0; i < 2; i++) {
1636 		val = ucontrol->value.integer.value[i];
1637 		if (val > VIA_DXS_MAX_VOLUME)
1638 			val = VIA_DXS_MAX_VOLUME;
1639 		val = VIA_DXS_MAX_VOLUME - val;
1640 		if (val != chip->playback_volume_c[i]) {
1641 			change = 1;
1642 			chip->playback_volume_c[i] = val;
1643 			for (idx = 0; idx < 4; idx++) {
1644 				unsigned long port = chip->port + 0x10 * idx;
1645 				chip->playback_volume[idx][i] = val;
1646 				outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1647 			}
1648 		}
1649 	}
1650 	return change;
1651 }
1652 
1653 static snd_kcontrol_new_t snd_via8233_pcmdxs_volume_control __devinitdata = {
1654 	.name = "PCM Playback Volume",
1655 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1656 	.info = snd_via8233_dxs_volume_info,
1657 	.get = snd_via8233_pcmdxs_volume_get,
1658 	.put = snd_via8233_pcmdxs_volume_put,
1659 };
1660 
1661 static snd_kcontrol_new_t snd_via8233_dxs_volume_control __devinitdata = {
1662 	.name = "VIA DXS Playback Volume",
1663 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1664 	.count = 4,
1665 	.info = snd_via8233_dxs_volume_info,
1666 	.get = snd_via8233_dxs_volume_get,
1667 	.put = snd_via8233_dxs_volume_put,
1668 };
1669 
1670 /*
1671  */
1672 
1673 static void snd_via82xx_mixer_free_ac97_bus(ac97_bus_t *bus)
1674 {
1675 	via82xx_t *chip = bus->private_data;
1676 	chip->ac97_bus = NULL;
1677 }
1678 
1679 static void snd_via82xx_mixer_free_ac97(ac97_t *ac97)
1680 {
1681 	via82xx_t *chip = ac97->private_data;
1682 	chip->ac97 = NULL;
1683 }
1684 
1685 static struct ac97_quirk ac97_quirks[] = {
1686 	{
1687 		.subvendor = 0x1106,
1688 		.subdevice = 0x4161,
1689 		.codec_id = 0x56494161, /* VT1612A */
1690 		.name = "Soltek SL-75DRV5",
1691 		.type = AC97_TUNE_NONE
1692 	},
1693 	{	/* FIXME: which codec? */
1694 		.subvendor = 0x1106,
1695 		.subdevice = 0x4161,
1696 		.name = "ASRock K7VT2",
1697 		.type = AC97_TUNE_HP_ONLY
1698 	},
1699 	{
1700 		.subvendor = 0x1019,
1701 		.subdevice = 0x0a81,
1702 		.name = "ECS K7VTA3",
1703 		.type = AC97_TUNE_HP_ONLY
1704 	},
1705 	{
1706 		.subvendor = 0x1019,
1707 		.subdevice = 0x0a85,
1708 		.name = "ECS L7VMM2",
1709 		.type = AC97_TUNE_HP_ONLY
1710 	},
1711 	{
1712 		.subvendor = 0x1849,
1713 		.subdevice = 0x3059,
1714 		.name = "ASRock K7VM2",
1715 		.type = AC97_TUNE_HP_ONLY	/* VT1616 */
1716 	},
1717 	{
1718 		.subvendor = 0x14cd,
1719 		.subdevice = 0x7002,
1720 		.name = "Unknown",
1721 		.type = AC97_TUNE_ALC_JACK
1722 	},
1723 	{
1724 		.subvendor = 0x1071,
1725 		.subdevice = 0x8590,
1726 		.name = "Mitac Mobo",
1727 		.type = AC97_TUNE_ALC_JACK
1728 	},
1729 	{
1730 		.subvendor = 0x161f,
1731 		.subdevice = 0x202b,
1732 		.name = "Arima Notebook",
1733 		.type = AC97_TUNE_HP_ONLY,
1734 	},
1735 	{ } /* terminator */
1736 };
1737 
1738 static int __devinit snd_via82xx_mixer_new(via82xx_t *chip, const char *quirk_override)
1739 {
1740 	ac97_template_t ac97;
1741 	int err;
1742 	static ac97_bus_ops_t ops = {
1743 		.write = snd_via82xx_codec_write,
1744 		.read = snd_via82xx_codec_read,
1745 		.wait = snd_via82xx_codec_wait,
1746 	};
1747 
1748 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1749 		return err;
1750 	chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
1751 	chip->ac97_bus->clock = chip->ac97_clock;
1752 
1753 	memset(&ac97, 0, sizeof(ac97));
1754 	ac97.private_data = chip;
1755 	ac97.private_free = snd_via82xx_mixer_free_ac97;
1756 	ac97.pci = chip->pci;
1757 	ac97.scaps = AC97_SCAP_SKIP_MODEM;
1758 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1759 		return err;
1760 
1761 	snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
1762 
1763 	if (chip->chip_type != TYPE_VIA686) {
1764 		/* use slot 10/11 */
1765 		snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1766 	}
1767 
1768 	return 0;
1769 }
1770 
1771 #ifdef SUPPORT_JOYSTICK
1772 #define JOYSTICK_ADDR	0x200
1773 static int __devinit snd_via686_create_gameport(via82xx_t *chip, unsigned char *legacy)
1774 {
1775 	struct gameport *gp;
1776 	struct resource *r;
1777 
1778 	if (!joystick)
1779 		return -ENODEV;
1780 
1781 	r = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport");
1782 	if (!r) {
1783 		printk(KERN_WARNING "via82xx: cannot reserve joystick port 0x%#x\n", JOYSTICK_ADDR);
1784 		return -EBUSY;
1785 	}
1786 
1787 	chip->gameport = gp = gameport_allocate_port();
1788 	if (!gp) {
1789 		printk(KERN_ERR "via82xx: cannot allocate memory for gameport\n");
1790 		release_and_free_resource(r);
1791 		return -ENOMEM;
1792 	}
1793 
1794 	gameport_set_name(gp, "VIA686 Gameport");
1795 	gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1796 	gameport_set_dev_parent(gp, &chip->pci->dev);
1797 	gp->io = JOYSTICK_ADDR;
1798 	gameport_set_port_data(gp, r);
1799 
1800 	/* Enable legacy joystick port */
1801 	*legacy |= VIA_FUNC_ENABLE_GAME;
1802 	pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
1803 
1804 	gameport_register_port(chip->gameport);
1805 
1806 	return 0;
1807 }
1808 
1809 static void snd_via686_free_gameport(via82xx_t *chip)
1810 {
1811 	if (chip->gameport) {
1812 		struct resource *r = gameport_get_port_data(chip->gameport);
1813 
1814 		gameport_unregister_port(chip->gameport);
1815 		chip->gameport = NULL;
1816 		release_and_free_resource(r);
1817 	}
1818 }
1819 #else
1820 static inline int snd_via686_create_gameport(via82xx_t *chip, unsigned char *legacy)
1821 {
1822 	return -ENOSYS;
1823 }
1824 static inline void snd_via686_free_gameport(via82xx_t *chip) { }
1825 #endif
1826 
1827 
1828 /*
1829  *
1830  */
1831 
1832 static int __devinit snd_via8233_init_misc(via82xx_t *chip)
1833 {
1834 	int i, err, caps;
1835 	unsigned char val;
1836 
1837 	caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
1838 	for (i = 0; i < caps; i++) {
1839 		snd_via8233_capture_source.index = i;
1840 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
1841 		if (err < 0)
1842 			return err;
1843 	}
1844 	if (ac97_can_spdif(chip->ac97)) {
1845 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
1846 		if (err < 0)
1847 			return err;
1848 	}
1849 	if (chip->chip_type != TYPE_VIA8233A) {
1850 		/* when no h/w PCM volume control is found, use DXS volume control
1851 		 * as the PCM vol control
1852 		 */
1853 		snd_ctl_elem_id_t sid;
1854 		memset(&sid, 0, sizeof(sid));
1855 		strcpy(sid.name, "PCM Playback Volume");
1856 		sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1857 		if (! snd_ctl_find_id(chip->card, &sid)) {
1858 			snd_printd(KERN_INFO "Using DXS as PCM Playback\n");
1859 			err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
1860 			if (err < 0)
1861 				return err;
1862 		}
1863 		else /* Using DXS when PCM emulation is enabled is really weird */
1864 		{
1865 			/* Standalone DXS controls */
1866 			err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs_volume_control, chip));
1867 			if (err < 0)
1868 				return err;
1869 		}
1870 	}
1871 	/* select spdif data slot 10/11 */
1872 	pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1873 	val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
1874 	val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
1875 	pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1876 
1877 	return 0;
1878 }
1879 
1880 static int __devinit snd_via686_init_misc(via82xx_t *chip)
1881 {
1882 	unsigned char legacy, legacy_cfg;
1883 	int rev_h = 0;
1884 
1885 	legacy = chip->old_legacy;
1886 	legacy_cfg = chip->old_legacy_cfg;
1887 	legacy |= VIA_FUNC_MIDI_IRQMASK;	/* FIXME: correct? (disable MIDI) */
1888 	legacy &= ~VIA_FUNC_ENABLE_GAME;	/* disable joystick */
1889 	if (chip->revision >= VIA_REV_686_H) {
1890 		rev_h = 1;
1891 		if (mpu_port >= 0x200) {	/* force MIDI */
1892 			mpu_port &= 0xfffc;
1893 			pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
1894 #ifdef CONFIG_PM
1895 			chip->mpu_port_saved = mpu_port;
1896 #endif
1897 		} else {
1898 			mpu_port = pci_resource_start(chip->pci, 2);
1899 		}
1900 	} else {
1901 		switch (mpu_port) {	/* force MIDI */
1902 		case 0x300:
1903 		case 0x310:
1904 		case 0x320:
1905 		case 0x330:
1906 			legacy_cfg &= ~(3 << 2);
1907 			legacy_cfg |= (mpu_port & 0x0030) >> 2;
1908 			break;
1909 		default:			/* no, use BIOS settings */
1910 			if (legacy & VIA_FUNC_ENABLE_MIDI)
1911 				mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
1912 			break;
1913 		}
1914 	}
1915 	if (mpu_port >= 0x200 &&
1916 	    (chip->mpu_res = request_region(mpu_port, 2, "VIA82xx MPU401"))
1917 	    != NULL) {
1918 		if (rev_h)
1919 			legacy |= VIA_FUNC_MIDI_PNP;	/* enable PCI I/O 2 */
1920 		legacy |= VIA_FUNC_ENABLE_MIDI;
1921 	} else {
1922 		if (rev_h)
1923 			legacy &= ~VIA_FUNC_MIDI_PNP;	/* disable PCI I/O 2 */
1924 		legacy &= ~VIA_FUNC_ENABLE_MIDI;
1925 		mpu_port = 0;
1926 	}
1927 
1928 	pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1929 	pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
1930 	if (chip->mpu_res) {
1931 		if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
1932 					mpu_port, 1,
1933 					chip->irq, 0, &chip->rmidi) < 0) {
1934 			printk(KERN_WARNING "unable to initialize MPU-401"
1935 			       " at 0x%lx, skipping\n", mpu_port);
1936 			legacy &= ~VIA_FUNC_ENABLE_MIDI;
1937 		} else {
1938 			legacy &= ~VIA_FUNC_MIDI_IRQMASK;	/* enable MIDI interrupt */
1939 		}
1940 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1941 	}
1942 
1943 	snd_via686_create_gameport(chip, &legacy);
1944 
1945 #ifdef CONFIG_PM
1946 	chip->legacy_saved = legacy;
1947 	chip->legacy_cfg_saved = legacy_cfg;
1948 #endif
1949 
1950 	return 0;
1951 }
1952 
1953 
1954 /*
1955  * proc interface
1956  */
1957 static void snd_via82xx_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
1958 {
1959 	via82xx_t *chip = entry->private_data;
1960 	int i;
1961 
1962 	snd_iprintf(buffer, "%s\n\n", chip->card->longname);
1963 	for (i = 0; i < 0xa0; i += 4) {
1964 		snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
1965 	}
1966 }
1967 
1968 static void __devinit snd_via82xx_proc_init(via82xx_t *chip)
1969 {
1970 	snd_info_entry_t *entry;
1971 
1972 	if (! snd_card_proc_new(chip->card, "via82xx", &entry))
1973 		snd_info_set_text_ops(entry, chip, 1024, snd_via82xx_proc_read);
1974 }
1975 
1976 /*
1977  *
1978  */
1979 
1980 static int snd_via82xx_chip_init(via82xx_t *chip)
1981 {
1982 	unsigned int val;
1983 	unsigned long end_time;
1984 	unsigned char pval;
1985 
1986 #if 0 /* broken on K7M? */
1987 	if (chip->chip_type == TYPE_VIA686)
1988 		/* disable all legacy ports */
1989 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
1990 #endif
1991 	pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
1992 	if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
1993 		/* deassert ACLink reset, force SYNC */
1994 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
1995 				      VIA_ACLINK_CTRL_ENABLE |
1996 				      VIA_ACLINK_CTRL_RESET |
1997 				      VIA_ACLINK_CTRL_SYNC);
1998 		udelay(100);
1999 #if 1 /* FIXME: should we do full reset here for all chip models? */
2000 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
2001 		udelay(100);
2002 #else
2003 		/* deassert ACLink reset, force SYNC (warm AC'97 reset) */
2004 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
2005 				      VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
2006 		udelay(2);
2007 #endif
2008 		/* ACLink on, deassert ACLink reset, VSR, SGD data out */
2009 		/* note - FM data out has trouble with non VRA codecs !! */
2010 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2011 		udelay(100);
2012 	}
2013 
2014 	/* Make sure VRA is enabled, in case we didn't do a
2015 	 * complete codec reset, above */
2016 	pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
2017 	if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
2018 		/* ACLink on, deassert ACLink reset, VSR, SGD data out */
2019 		/* note - FM data out has trouble with non VRA codecs !! */
2020 		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
2021 		udelay(100);
2022 	}
2023 
2024 	/* wait until codec ready */
2025 	end_time = jiffies + msecs_to_jiffies(750);
2026 	do {
2027 		pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
2028 		if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
2029 			break;
2030 		set_current_state(TASK_UNINTERRUPTIBLE);
2031 		schedule_timeout(1);
2032 	} while (time_before(jiffies, end_time));
2033 
2034 	if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
2035 		snd_printk("AC'97 codec is not ready [0x%x]\n", val);
2036 
2037 #if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
2038 	snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2039 				 VIA_REG_AC97_SECONDARY_VALID |
2040 				 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2041 	end_time = jiffies + msecs_to_jiffies(750);
2042 	snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
2043 				 VIA_REG_AC97_SECONDARY_VALID |
2044 				 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
2045 	do {
2046 		if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
2047 			chip->ac97_secondary = 1;
2048 			goto __ac97_ok2;
2049 		}
2050 		set_current_state(TASK_INTERRUPTIBLE);
2051 		schedule_timeout(1);
2052 	} while (time_before(jiffies, end_time));
2053 	/* This is ok, the most of motherboards have only one codec */
2054 
2055       __ac97_ok2:
2056 #endif
2057 
2058 	if (chip->chip_type == TYPE_VIA686) {
2059 		/* route FM trap to IRQ, disable FM trap */
2060 		pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
2061 		/* disable all GPI interrupts */
2062 		outl(0, VIAREG(chip, GPI_INTR));
2063 	}
2064 
2065 	if (chip->chip_type != TYPE_VIA686) {
2066 		/* Workaround for Award BIOS bug:
2067 		 * DXS channels don't work properly with VRA if MC97 is disabled.
2068 		 */
2069 		struct pci_dev *pci;
2070 		pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
2071 		if (pci) {
2072 			unsigned char data;
2073 			pci_read_config_byte(pci, 0x44, &data);
2074 			pci_write_config_byte(pci, 0x44, data | 0x40);
2075 			pci_dev_put(pci);
2076 		}
2077 	}
2078 
2079 	if (chip->chip_type != TYPE_VIA8233A) {
2080 		int i, idx;
2081 		for (idx = 0; idx < 4; idx++) {
2082 			unsigned long port = chip->port + 0x10 * idx;
2083 			for (i = 0; i < 2; i++) {
2084 				chip->playback_volume[idx][i]=chip->playback_volume_c[i];
2085 				outb(chip->playback_volume_c[i], port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
2086 			}
2087 		}
2088 	}
2089 
2090 	return 0;
2091 }
2092 
2093 #ifdef CONFIG_PM
2094 /*
2095  * power management
2096  */
2097 static int snd_via82xx_suspend(snd_card_t *card, pm_message_t state)
2098 {
2099 	via82xx_t *chip = card->pm_private_data;
2100 	int i;
2101 
2102 	for (i = 0; i < 2; i++)
2103 		if (chip->pcms[i])
2104 			snd_pcm_suspend_all(chip->pcms[i]);
2105 	for (i = 0; i < chip->num_devs; i++)
2106 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
2107 	synchronize_irq(chip->irq);
2108 	snd_ac97_suspend(chip->ac97);
2109 
2110 	/* save misc values */
2111 	if (chip->chip_type != TYPE_VIA686) {
2112 		pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
2113 		chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
2114 		chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2115 	}
2116 
2117 	pci_set_power_state(chip->pci, 3);
2118 	pci_disable_device(chip->pci);
2119 	return 0;
2120 }
2121 
2122 static int snd_via82xx_resume(snd_card_t *card)
2123 {
2124 	via82xx_t *chip = card->pm_private_data;
2125 	int i;
2126 
2127 	pci_enable_device(chip->pci);
2128 	pci_set_power_state(chip->pci, 0);
2129 
2130 	snd_via82xx_chip_init(chip);
2131 
2132 	if (chip->chip_type == TYPE_VIA686) {
2133 		if (chip->mpu_port_saved)
2134 			pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
2135 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
2136 		pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
2137 	} else {
2138 		pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
2139 		outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
2140 		outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
2141 	}
2142 
2143 	snd_ac97_resume(chip->ac97);
2144 
2145 	for (i = 0; i < chip->num_devs; i++)
2146 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
2147 
2148 	return 0;
2149 }
2150 #endif /* CONFIG_PM */
2151 
2152 static int snd_via82xx_free(via82xx_t *chip)
2153 {
2154 	unsigned int i;
2155 
2156 	if (chip->irq < 0)
2157 		goto __end_hw;
2158 	/* disable interrupts */
2159 	for (i = 0; i < chip->num_devs; i++)
2160 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
2161 	synchronize_irq(chip->irq);
2162       __end_hw:
2163 	if (chip->irq >= 0)
2164 		free_irq(chip->irq, (void *)chip);
2165 	release_and_free_resource(chip->mpu_res);
2166 	pci_release_regions(chip->pci);
2167 
2168 	if (chip->chip_type == TYPE_VIA686) {
2169 		snd_via686_free_gameport(chip);
2170 		pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
2171 		pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
2172 	}
2173 	pci_disable_device(chip->pci);
2174 	kfree(chip);
2175 	return 0;
2176 }
2177 
2178 static int snd_via82xx_dev_free(snd_device_t *device)
2179 {
2180 	via82xx_t *chip = device->device_data;
2181 	return snd_via82xx_free(chip);
2182 }
2183 
2184 static int __devinit snd_via82xx_create(snd_card_t * card,
2185 					struct pci_dev *pci,
2186 					int chip_type,
2187 					int revision,
2188 					unsigned int ac97_clock,
2189 					via82xx_t ** r_via)
2190 {
2191 	via82xx_t *chip;
2192 	int err;
2193         static snd_device_ops_t ops = {
2194 		.dev_free =	snd_via82xx_dev_free,
2195         };
2196 
2197 	if ((err = pci_enable_device(pci)) < 0)
2198 		return err;
2199 
2200 	if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
2201 		pci_disable_device(pci);
2202 		return -ENOMEM;
2203 	}
2204 
2205 	chip->chip_type = chip_type;
2206 	chip->revision = revision;
2207 
2208 	spin_lock_init(&chip->reg_lock);
2209 	spin_lock_init(&chip->rates[0].lock);
2210 	spin_lock_init(&chip->rates[1].lock);
2211 	chip->card = card;
2212 	chip->pci = pci;
2213 	chip->irq = -1;
2214 
2215 	pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
2216 	pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
2217 	pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
2218 			      chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
2219 
2220 	if ((err = pci_request_regions(pci, card->driver)) < 0) {
2221 		kfree(chip);
2222 		pci_disable_device(pci);
2223 		return err;
2224 	}
2225 	chip->port = pci_resource_start(pci, 0);
2226 	if (request_irq(pci->irq,
2227 			chip_type == TYPE_VIA8233 ?
2228 			snd_via8233_interrupt :	snd_via686_interrupt,
2229 			SA_INTERRUPT|SA_SHIRQ,
2230 			card->driver, (void *)chip)) {
2231 		snd_printk("unable to grab IRQ %d\n", pci->irq);
2232 		snd_via82xx_free(chip);
2233 		return -EBUSY;
2234 	}
2235 	chip->irq = pci->irq;
2236 	if (ac97_clock >= 8000 && ac97_clock <= 48000)
2237 		chip->ac97_clock = ac97_clock;
2238 	synchronize_irq(chip->irq);
2239 
2240 	if ((err = snd_via82xx_chip_init(chip)) < 0) {
2241 		snd_via82xx_free(chip);
2242 		return err;
2243 	}
2244 
2245 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2246 		snd_via82xx_free(chip);
2247 		return err;
2248 	}
2249 
2250 	/* The 8233 ac97 controller does not implement the master bit
2251 	 * in the pci command register. IMHO this is a violation of the PCI spec.
2252 	 * We call pci_set_master here because it does not hurt. */
2253 	pci_set_master(pci);
2254 
2255 	snd_card_set_dev(card, &pci->dev);
2256 
2257 	*r_via = chip;
2258 	return 0;
2259 }
2260 
2261 struct via823x_info {
2262 	int revision;
2263 	char *name;
2264 	int type;
2265 };
2266 static struct via823x_info via823x_cards[] __devinitdata = {
2267 	{ VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
2268 	{ VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
2269 	{ VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
2270 	{ VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
2271 	{ VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
2272 	{ VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
2273 };
2274 
2275 /*
2276  * auto detection of DXS channel supports.
2277  */
2278 struct dxs_whitelist {
2279 	unsigned short subvendor;
2280 	unsigned short subdevice;
2281 	unsigned short mask;
2282 	short action;	/* new dxs_support value */
2283 };
2284 
2285 static int __devinit check_dxs_list(struct pci_dev *pci)
2286 {
2287 	static struct dxs_whitelist whitelist[] = {
2288 		{ .subvendor = 0x1005, .subdevice = 0x4710, .action = VIA_DXS_ENABLE }, /* Avance Logic Mobo */
2289 		{ .subvendor = 0x1019, .subdevice = 0x0996, .action = VIA_DXS_48K },
2290 		{ .subvendor = 0x1019, .subdevice = 0x0a81, .action = VIA_DXS_NO_VRA }, /* ECS K7VTA3 v8.0 */
2291 		{ .subvendor = 0x1019, .subdevice = 0x0a85, .action = VIA_DXS_NO_VRA }, /* ECS L7VMM2 */
2292 		{ .subvendor = 0x1019, .subdevice = 0xa101, .action = VIA_DXS_SRC },
2293 		{ .subvendor = 0x1025, .subdevice = 0x0033, .action = VIA_DXS_NO_VRA }, /* Acer Inspire 1353LM */
2294 		{ .subvendor = 0x1025, .subdevice = 0x0046, .action = VIA_DXS_SRC }, /* Acer Aspire 1524 WLMi */
2295 		{ .subvendor = 0x1043, .subdevice = 0x8095, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8X (FIXME: possibly VIA_DXS_ENABLE?)*/
2296 		{ .subvendor = 0x1043, .subdevice = 0x80a1, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8-X */
2297 		{ .subvendor = 0x1043, .subdevice = 0x80b0, .action = VIA_DXS_NO_VRA }, /* ASUS A7V600 & K8V*/
2298 		{ .subvendor = 0x1043, .subdevice = 0x810d, .action = VIA_DXS_SRC }, /* ASUS */
2299 		{ .subvendor = 0x1043, .subdevice = 0x812a, .action = VIA_DXS_SRC    }, /* ASUS A8V Deluxe */
2300 		{ .subvendor = 0x1071, .subdevice = 0x8375, .action = VIA_DXS_NO_VRA }, /* Vobis/Yakumo/Mitac notebook */
2301 		{ .subvendor = 0x1071, .subdevice = 0x8399, .action = VIA_DXS_NO_VRA }, /* Umax AB 595T (VIA K8N800A - VT8237) */
2302 		{ .subvendor = 0x10cf, .subdevice = 0x118e, .action = VIA_DXS_ENABLE }, /* FSC laptop */
2303 		{ .subvendor = 0x1106, .subdevice = 0x4161, .action = VIA_DXS_NO_VRA }, /* ASRock K7VT2 */
2304 		{ .subvendor = 0x1106, .subdevice = 0x4552, .action = VIA_DXS_NO_VRA }, /* QDI Kudoz 7X/600-6AL */
2305 		{ .subvendor = 0x1106, .subdevice = 0xaa01, .action = VIA_DXS_NO_VRA }, /* EPIA MII */
2306 		{ .subvendor = 0x1106, .subdevice = 0xc001, .action = VIA_DXS_SRC }, /* Insight P4-ITX */
2307 		{ .subvendor = 0x1297, .subdevice = 0xa232, .action = VIA_DXS_ENABLE }, /* Shuttle ?? */
2308 		{ .subvendor = 0x1297, .subdevice = 0xc160, .action = VIA_DXS_ENABLE }, /* Shuttle SK41G */
2309 		{ .subvendor = 0x1458, .subdevice = 0xa002, .action = VIA_DXS_ENABLE }, /* Gigabyte GA-7VAXP */
2310 		{ .subvendor = 0x1462, .subdevice = 0x0080, .action = VIA_DXS_SRC }, /* MSI K8T Neo-FIS2R */
2311 		{ .subvendor = 0x1462, .subdevice = 0x0430, .action = VIA_DXS_SRC }, /* MSI 7142 (K8MM-V) */
2312 		{ .subvendor = 0x1462, .subdevice = 0x3800, .action = VIA_DXS_ENABLE }, /* MSI KT266 */
2313 		{ .subvendor = 0x1462, .subdevice = 0x5901, .action = VIA_DXS_NO_VRA }, /* MSI KT6 Delta-SR */
2314 		{ .subvendor = 0x1462, .subdevice = 0x7023, .action = VIA_DXS_NO_VRA }, /* MSI K8T Neo2-FI */
2315 		{ .subvendor = 0x1462, .subdevice = 0x7120, .action = VIA_DXS_ENABLE }, /* MSI KT4V */
2316 		{ .subvendor = 0x1462, .subdevice = 0x7142, .action = VIA_DXS_ENABLE }, /* MSI K8MM-V */
2317 		{ .subvendor = 0x147b, .subdevice = 0x1401, .action = VIA_DXS_ENABLE }, /* ABIT KD7(-RAID) */
2318 		{ .subvendor = 0x147b, .subdevice = 0x1411, .action = VIA_DXS_ENABLE }, /* ABIT VA-20 */
2319 		{ .subvendor = 0x147b, .subdevice = 0x1413, .action = VIA_DXS_ENABLE }, /* ABIT KV8 Pro */
2320 		{ .subvendor = 0x147b, .subdevice = 0x1415, .action = VIA_DXS_NO_VRA }, /* Abit AV8 */
2321 		{ .subvendor = 0x14ff, .subdevice = 0x0403, .action = VIA_DXS_ENABLE }, /* Twinhead mobo */
2322 		{ .subvendor = 0x14ff, .subdevice = 0x0408, .action = VIA_DXS_SRC }, /* Twinhead laptop */
2323 		{ .subvendor = 0x1558, .subdevice = 0x4701, .action = VIA_DXS_SRC }, /* Clevo D470 */
2324 		{ .subvendor = 0x1584, .subdevice = 0x8120, .action = VIA_DXS_ENABLE }, /* Gericom/Targa/Vobis/Uniwill laptop */
2325 		{ .subvendor = 0x1584, .subdevice = 0x8123, .action = VIA_DXS_NO_VRA }, /* Uniwill (Targa Visionary XP-210) */
2326 		{ .subvendor = 0x161f, .subdevice = 0x202b, .action = VIA_DXS_NO_VRA }, /* Amira Note book */
2327 		{ .subvendor = 0x161f, .subdevice = 0x2032, .action = VIA_DXS_48K }, /* m680x machines */
2328 		{ .subvendor = 0x1631, .subdevice = 0xe004, .action = VIA_DXS_ENABLE }, /* Easy Note 3174, Packard Bell */
2329 		{ .subvendor = 0x1695, .subdevice = 0x3005, .action = VIA_DXS_ENABLE }, /* EPoX EP-8K9A */
2330 		{ .subvendor = 0x1849, .subdevice = 0x3059, .action = VIA_DXS_NO_VRA }, /* ASRock K7VM2 */
2331 		{ .subvendor = 0x1919, .subdevice = 0x200a, .action = VIA_DXS_NO_VRA }, /* Soltek SL-K8Tpro-939 */
2332 		{ .subvendor = 0x4005, .subdevice = 0x4710, .action = VIA_DXS_SRC },	/* MSI K7T266 Pro2 (MS-6380 V2.0) BIOS 3.7 */
2333 		{ } /* terminator */
2334 	};
2335 	struct dxs_whitelist *w;
2336 	unsigned short subsystem_vendor;
2337 	unsigned short subsystem_device;
2338 
2339 	pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
2340 	pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
2341 
2342 	for (w = whitelist; w->subvendor; w++) {
2343 		if (w->subvendor != subsystem_vendor)
2344 			continue;
2345 		if (w->mask) {
2346 			if ((w->mask & subsystem_device) == w->subdevice)
2347 				return w->action;
2348 		} else {
2349 			if (subsystem_device == w->subdevice)
2350 				return w->action;
2351 		}
2352 	}
2353 
2354 	/*
2355 	 * not detected, try 48k rate only to be sure.
2356 	 */
2357 	printk(KERN_INFO "via82xx: Assuming DXS channels with 48k fixed sample rate.\n");
2358 	printk(KERN_INFO "         Please try dxs_support=5 option\n");
2359 	printk(KERN_INFO "         and report if it works on your machine.\n");
2360 	printk(KERN_INFO "         For more details, read ALSA-Configuration.txt.\n");
2361 	return VIA_DXS_48K;
2362 };
2363 
2364 static int __devinit snd_via82xx_probe(struct pci_dev *pci,
2365 				       const struct pci_device_id *pci_id)
2366 {
2367 	snd_card_t *card;
2368 	via82xx_t *chip;
2369 	unsigned char revision;
2370 	int chip_type = 0, card_type;
2371 	unsigned int i;
2372 	int err;
2373 
2374 	card = snd_card_new(index, id, THIS_MODULE, 0);
2375 	if (card == NULL)
2376 		return -ENOMEM;
2377 
2378 	card_type = pci_id->driver_data;
2379 	pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
2380 	switch (card_type) {
2381 	case TYPE_CARD_VIA686:
2382 		strcpy(card->driver, "VIA686A");
2383 		sprintf(card->shortname, "VIA 82C686A/B rev%x", revision);
2384 		chip_type = TYPE_VIA686;
2385 		break;
2386 	case TYPE_CARD_VIA8233:
2387 		chip_type = TYPE_VIA8233;
2388 		sprintf(card->shortname, "VIA 823x rev%x", revision);
2389 		for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
2390 			if (revision == via823x_cards[i].revision) {
2391 				chip_type = via823x_cards[i].type;
2392 				strcpy(card->shortname, via823x_cards[i].name);
2393 				break;
2394 			}
2395 		}
2396 		if (chip_type != TYPE_VIA8233A) {
2397 			if (dxs_support == VIA_DXS_AUTO)
2398 				dxs_support = check_dxs_list(pci);
2399 			/* force to use VIA8233 or 8233A model according to
2400 			 * dxs_support module option
2401 			 */
2402 			if (dxs_support == VIA_DXS_DISABLE)
2403 				chip_type = TYPE_VIA8233A;
2404 			else
2405 				chip_type = TYPE_VIA8233;
2406 		}
2407 		if (chip_type == TYPE_VIA8233A)
2408 			strcpy(card->driver, "VIA8233A");
2409 		else if (revision >= VIA_REV_8237)
2410 			strcpy(card->driver, "VIA8237"); /* no slog assignment */
2411 		else
2412 			strcpy(card->driver, "VIA8233");
2413 		break;
2414 	default:
2415 		snd_printk(KERN_ERR "invalid card type %d\n", card_type);
2416 		err = -EINVAL;
2417 		goto __error;
2418 	}
2419 
2420 	if ((err = snd_via82xx_create(card, pci, chip_type, revision,
2421 				      ac97_clock, &chip)) < 0)
2422 		goto __error;
2423 	if ((err = snd_via82xx_mixer_new(chip, ac97_quirk)) < 0)
2424 		goto __error;
2425 
2426 	if (chip_type == TYPE_VIA686) {
2427 		if ((err = snd_via686_pcm_new(chip)) < 0 ||
2428 		    (err = snd_via686_init_misc(chip)) < 0)
2429 			goto __error;
2430 	} else {
2431 		if (chip_type == TYPE_VIA8233A) {
2432 			if ((err = snd_via8233a_pcm_new(chip)) < 0)
2433 				goto __error;
2434 			// chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
2435 		} else {
2436 			if ((err = snd_via8233_pcm_new(chip)) < 0)
2437 				goto __error;
2438 			if (dxs_support == VIA_DXS_48K)
2439 				chip->dxs_fixed = 1;
2440 			else if (dxs_support == VIA_DXS_NO_VRA)
2441 				chip->no_vra = 1;
2442 			else if (dxs_support == VIA_DXS_SRC) {
2443 				chip->no_vra = 1;
2444 				chip->dxs_src = 1;
2445 			}
2446 		}
2447 		if ((err = snd_via8233_init_misc(chip)) < 0)
2448 			goto __error;
2449 	}
2450 
2451 	snd_card_set_pm_callback(card, snd_via82xx_suspend, snd_via82xx_resume, chip);
2452 
2453 	/* disable interrupts */
2454 	for (i = 0; i < chip->num_devs; i++)
2455 		snd_via82xx_channel_reset(chip, &chip->devs[i]);
2456 
2457 	snprintf(card->longname, sizeof(card->longname),
2458 		 "%s with %s at %#lx, irq %d", card->shortname,
2459 		 snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
2460 
2461 	snd_via82xx_proc_init(chip);
2462 
2463 	if ((err = snd_card_register(card)) < 0) {
2464 		snd_card_free(card);
2465 		return err;
2466 	}
2467 	pci_set_drvdata(pci, card);
2468 	return 0;
2469 
2470  __error:
2471 	snd_card_free(card);
2472 	return err;
2473 }
2474 
2475 static void __devexit snd_via82xx_remove(struct pci_dev *pci)
2476 {
2477 	snd_card_free(pci_get_drvdata(pci));
2478 	pci_set_drvdata(pci, NULL);
2479 }
2480 
2481 static struct pci_driver driver = {
2482 	.name = "VIA 82xx Audio",
2483 	.owner = THIS_MODULE,
2484 	.id_table = snd_via82xx_ids,
2485 	.probe = snd_via82xx_probe,
2486 	.remove = __devexit_p(snd_via82xx_remove),
2487 	SND_PCI_PM_CALLBACKS
2488 };
2489 
2490 static int __init alsa_card_via82xx_init(void)
2491 {
2492 	return pci_register_driver(&driver);
2493 }
2494 
2495 static void __exit alsa_card_via82xx_exit(void)
2496 {
2497 	pci_unregister_driver(&driver);
2498 }
2499 
2500 module_init(alsa_card_via82xx_init)
2501 module_exit(alsa_card_via82xx_exit)
2502