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