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