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