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