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