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