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