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