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