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