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