1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
77static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
78static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
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
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
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
124#define VIA_REV_PRE_8233 0x10
125#define VIA_REV_8233C 0x20
126#define VIA_REV_8233 0x30
127#define VIA_REV_8233A 0x40
128#define VIA_REV_8235 0x50
129#define VIA_REV_8237 0x60
130
131
132
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
139#define VIA_REG_OFFSET_STATUS 0x00
140#define VIA_REG_STAT_ACTIVE 0x80
141#define VIA_REG_STAT_PAUSED 0x40
142#define VIA_REG_STAT_TRIGGER_QUEUED 0x08
143#define VIA_REG_STAT_STOPPED 0x04
144#define VIA_REG_STAT_EOL 0x02
145#define VIA_REG_STAT_FLAG 0x01
146#define VIA_REG_OFFSET_CONTROL 0x01
147#define VIA_REG_CTRL_START 0x80
148#define VIA_REG_CTRL_TERMINATE 0x40
149#define VIA_REG_CTRL_AUTOSTART 0x20
150#define VIA_REG_CTRL_PAUSE 0x08
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
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
157#define VIA_REG_TYPE_AUTOSTART 0x80
158#define VIA_REG_TYPE_16BIT 0x20
159#define VIA_REG_TYPE_STEREO 0x10
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
167#define VIA_REG_OFFSET_CURR_PTR 0x04
168#define VIA_REG_OFFSET_STOP_IDX 0x08
169#define VIA8233_REG_TYPE_16BIT 0x00200000
170#define VIA8233_REG_TYPE_STEREO 0x00100000
171#define VIA_REG_OFFSET_CURR_COUNT 0x0c
172#define VIA_REG_OFFSET_CURR_INDEX 0x0f
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
186DEFINE_VIA_REGSET(PLAYBACK, 0x00);
187DEFINE_VIA_REGSET(CAPTURE, 0x10);
188DEFINE_VIA_REGSET(FM, 0x20);
189
190
191#define VIA_REG_AC97 0x80
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
206
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
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
234DEFINE_VIA_REGSET(MULTPLAY, 0x40);
235DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
236
237
238#define VIA_REG_OFS_PLAYBACK_VOLUME_L 0x02
239#define VIA_REG_OFS_PLAYBACK_VOLUME_R 0x03
240#define VIA_REG_OFS_MULTPLAY_FORMAT 0x02
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
244#define VIA_REG_OFS_CAPTURE_FIFO 0x02
245#define VIA_REG_CAPTURE_FIFO_ENABLE 0x40
246
247#define VIA_DXS_MAX_VOLUME 31
248
249#define VIA_REG_CAPTURE_CHANNEL 0x63
250#define VIA_REG_CAPTURE_CHANNEL_MIC 0x4
251#define VIA_REG_CAPTURE_CHANNEL_LINE 0
252#define VIA_REG_CAPTURE_SELECT_CODEC 0x03
253
254#define VIA_TBL_BIT_FLAG 0x40000000
255#define VIA_TBL_BIT_EOL 0x80000000
256
257
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
262#define VIA_ACLINK_LOWPOWER 0x02
263#define VIA_ACLINK_C00_READY 0x01
264#define VIA_ACLINK_CTRL 0x41
265#define VIA_ACLINK_CTRL_ENABLE 0x80
266#define VIA_ACLINK_CTRL_RESET 0x40
267#define VIA_ACLINK_CTRL_SYNC 0x20
268#define VIA_ACLINK_CTRL_SDO 0x10
269#define VIA_ACLINK_CTRL_VRA 0x08
270#define VIA_ACLINK_CTRL_PCM 0x04
271#define VIA_ACLINK_CTRL_FM 0x02
272#define VIA_ACLINK_CTRL_SB 0x01
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
279#define VIA_FUNC_MIDI_IRQMASK 0x40
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
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;
328 snd_pcm_substream_t *substream;
329 int running;
330 unsigned int tbl_entries;
331 struct snd_dma_buffer table;
332 struct snd_via_sg_table *idx_table;
333
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
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[4][2];
371
372 unsigned int intr_mask;
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];
381 unsigned int dxs_fixed: 1;
382 unsigned int no_vra: 1;
383 unsigned int spdif_on: 1;
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;
392
393 spinlock_t reg_lock;
394 snd_info_entry_t *proc_entry;
395
396#ifdef SUPPORT_JOYSTICK
397 struct gameport gameport;
398 struct resource *res_joystick;
399#endif
400};
401
402static struct pci_device_id snd_via82xx_ids[] = {
403 { 0x1106, 0x3058, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA686, },
404 { 0x1106, 0x3059, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA8233, },
405 { 0, }
406};
407
408MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
409
410
411
412
413
414
415
416
417
418static int build_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
419 struct pci_dev *pci,
420 unsigned int periods, unsigned int fragsize)
421{
422 unsigned int i, idx, ofs, rest;
423 via82xx_t *chip = snd_pcm_substream_chip(substream);
424 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
425
426 if (dev->table.area == NULL) {
427
428
429
430 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
431 PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
432 &dev->table) < 0)
433 return -ENOMEM;
434 }
435 if (! dev->idx_table) {
436 dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
437 if (! dev->idx_table)
438 return -ENOMEM;
439 }
440
441
442 idx = 0;
443 ofs = 0;
444 for (i = 0; i < periods; i++) {
445 rest = fragsize;
446
447
448
449
450 do {
451 unsigned int r;
452 unsigned int flag;
453
454 if (idx >= VIA_TABLE_SIZE) {
455 snd_printk(KERN_ERR "via82xx: too much table size!\n");
456 return -EINVAL;
457 }
458 ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
459 r = PAGE_SIZE - (ofs % PAGE_SIZE);
460 if (rest < r)
461 r = rest;
462 rest -= r;
463 if (! rest) {
464 if (i == periods - 1)
465 flag = VIA_TBL_BIT_EOL;
466 else
467 flag = VIA_TBL_BIT_FLAG;
468 } else
469 flag = 0;
470
471 ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
472 dev->idx_table[idx].offset = ofs;
473 dev->idx_table[idx].size = r;
474 ofs += r;
475 idx++;
476 } while (rest > 0);
477 }
478 dev->tbl_entries = idx;
479 dev->bufsize = periods * fragsize;
480 dev->bufsize2 = dev->bufsize / 2;
481 dev->fragsize = fragsize;
482 return 0;
483}
484
485
486static int clean_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
487 struct pci_dev *pci)
488{
489 if (dev->table.area) {
490 snd_dma_free_pages(&dev->table);
491 dev->table.area = NULL;
492 }
493 if (dev->idx_table) {
494 kfree(dev->idx_table);
495 dev->idx_table = NULL;
496 }
497 return 0;
498}
499
500
501
502
503
504static inline unsigned int snd_via82xx_codec_xread(via82xx_t *chip)
505{
506 return inl(VIAREG(chip, AC97));
507}
508
509static inline void snd_via82xx_codec_xwrite(via82xx_t *chip, unsigned int val)
510{
511 outl(val, VIAREG(chip, AC97));
512}
513
514static int snd_via82xx_codec_ready(via82xx_t *chip, int secondary)
515{
516 unsigned int timeout = 1000;
517 unsigned int val;
518
519 while (timeout-- > 0) {
520 udelay(1);
521 if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
522 return val & 0xffff;
523 }
524 snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_via82xx_codec_xread(chip));
525 return -EIO;
526}
527
528static int snd_via82xx_codec_valid(via82xx_t *chip, int secondary)
529{
530 unsigned int timeout = 1000;
531 unsigned int val, val1;
532 unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
533 VIA_REG_AC97_SECONDARY_VALID;
534
535 while (timeout-- > 0) {
536 val = snd_via82xx_codec_xread(chip);
537 val1 = val & (VIA_REG_AC97_BUSY | stat);
538 if (val1 == stat)
539 return val & 0xffff;
540 udelay(1);
541 }
542 return -EIO;
543}
544
545static void snd_via82xx_codec_wait(ac97_t *ac97)
546{
547 via82xx_t *chip = ac97->private_data;
548 int err;
549 err = snd_via82xx_codec_ready(chip, ac97->num);
550
551 set_current_state(TASK_UNINTERRUPTIBLE);
552 schedule_timeout(HZ/2);
553}
554
555static void snd_via82xx_codec_write(ac97_t *ac97,
556 unsigned short reg,
557 unsigned short val)
558{
559 via82xx_t *chip = ac97->private_data;
560 unsigned int xval;
561
562 xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
563 xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
564 xval |= reg << VIA_REG_AC97_CMD_SHIFT;
565 xval |= val << VIA_REG_AC97_DATA_SHIFT;
566 snd_via82xx_codec_xwrite(chip, xval);
567 snd_via82xx_codec_ready(chip, ac97->num);
568}
569
570static unsigned short snd_via82xx_codec_read(ac97_t *ac97, unsigned short reg)
571{
572 via82xx_t *chip = ac97->private_data;
573 unsigned int xval, val = 0xffff;
574 int again = 0;
575
576 xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
577 xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
578 xval |= VIA_REG_AC97_READ;
579 xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
580 while (1) {
581 if (again++ > 3) {
582 snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n", ac97->num, snd_via82xx_codec_xread(chip));
583 return 0xffff;
584 }
585 snd_via82xx_codec_xwrite(chip, xval);
586 udelay (20);
587 if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
588 udelay(25);
589 val = snd_via82xx_codec_xread(chip);
590 break;
591 }
592 }
593 return val & 0xffff;
594}
595
596static void snd_via82xx_channel_reset(via82xx_t *chip, viadev_t *viadev)
597{
598 outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
599 VIADEV_REG(viadev, OFFSET_CONTROL));
600 inb(VIADEV_REG(viadev, OFFSET_CONTROL));
601 udelay(50);
602
603 outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
604
605 outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
606 outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE));
607
608 viadev->lastpos = 0;
609}
610
611
612
613
614
615
616static irqreturn_t snd_via82xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
617{
618 via82xx_t *chip = dev_id;
619 unsigned int status;
620 unsigned int i;
621
622 status = inl(VIAREG(chip, SGD_SHADOW));
623 if (! (status & chip->intr_mask)) {
624 if (chip->rmidi)
625
626 return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
627 return IRQ_NONE;
628 }
629
630
631 spin_lock(&chip->reg_lock);
632 for (i = 0; i < chip->num_devs; i++) {
633 viadev_t *viadev = &chip->devs[i];
634 unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
635 c_status &= (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED);
636 if (! c_status)
637 continue;
638 if (viadev->substream && viadev->running) {
639 spin_unlock(&chip->reg_lock);
640 snd_pcm_period_elapsed(viadev->substream);
641 spin_lock(&chip->reg_lock);
642 }
643 outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS));
644 }
645 spin_unlock(&chip->reg_lock);
646 return IRQ_HANDLED;
647}
648
649
650
651
652
653
654
655
656static int snd_via82xx_pcm_trigger(snd_pcm_substream_t * substream, int cmd)
657{
658 via82xx_t *chip = snd_pcm_substream_chip(substream);
659 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
660 unsigned char val;
661
662 if (chip->chip_type != TYPE_VIA686)
663 val = VIA_REG_CTRL_INT;
664 else
665 val = 0;
666 switch (cmd) {
667 case SNDRV_PCM_TRIGGER_START:
668 val |= VIA_REG_CTRL_START;
669 viadev->running = 1;
670 break;
671 case SNDRV_PCM_TRIGGER_STOP:
672 val = VIA_REG_CTRL_TERMINATE;
673 viadev->running = 0;
674 break;
675 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
676 val |= VIA_REG_CTRL_PAUSE;
677 viadev->running = 0;
678 break;
679 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
680 viadev->running = 1;
681 break;
682 default:
683 return -EINVAL;
684 }
685 outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
686 if (cmd == SNDRV_PCM_TRIGGER_STOP)
687 snd_via82xx_channel_reset(chip, viadev);
688 return 0;
689}
690
691
692
693
694
695
696
697
698
699
700#define check_invalid_pos(viadev,pos) \
701 ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 || viadev->lastpos < viadev->bufsize2))
702
703static inline unsigned int calc_linear_pos(viadev_t *viadev, unsigned int idx, unsigned int count)
704{
705 unsigned int size, base, res;
706
707 size = viadev->idx_table[idx].size;
708 base = viadev->idx_table[idx].offset;
709 res = base + size - count;
710
711
712 if (size < count) {
713 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n", (int)size, (int)count);
714 res = viadev->lastpos;
715 } else {
716 if (! count) {
717
718
719
720
721 int delta = res - viadev->lastpos;
722 if (delta < 0)
723 delta += viadev->bufsize;
724 if ((unsigned int)delta > viadev->fragsize)
725 res = base;
726 }
727 if (check_invalid_pos(viadev, res)) {
728#ifdef POINTER_DEBUG
729 printk(KERN_DEBUG "fail: idx = %i/%i, lastpos = 0x%x, bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, count = 0x%x\n", idx, viadev->tbl_entries, viadev->lastpos, viadev->bufsize2, viadev->idx_table[idx].offset, viadev->idx_table[idx].size, count);
730#endif
731
732 res = base + size;
733 if (check_invalid_pos(viadev, res)) {
734 snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), using last valid pointer\n");
735 res = viadev->lastpos;
736 }
737 }
738 }
739 viadev->lastpos = res;
740 if (res >= viadev->bufsize)
741 res -= viadev->bufsize;
742 return res;
743}
744
745
746
747
748static snd_pcm_uframes_t snd_via686_pcm_pointer(snd_pcm_substream_t *substream)
749{
750 via82xx_t *chip = snd_pcm_substream_chip(substream);
751 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
752 unsigned int idx, ptr, count, res;
753
754 snd_assert(viadev->tbl_entries, return 0);
755 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
756 return 0;
757
758 spin_lock(&chip->reg_lock);
759 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
760
761
762
763 ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
764 if (ptr <= (unsigned int)viadev->table.addr)
765 idx = 0;
766 else
767 idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
768 res = calc_linear_pos(viadev, idx, count);
769 spin_unlock(&chip->reg_lock);
770
771 return bytes_to_frames(substream->runtime, res);
772}
773
774
775
776
777static snd_pcm_uframes_t snd_via8233_pcm_pointer(snd_pcm_substream_t *substream)
778{
779 via82xx_t *chip = snd_pcm_substream_chip(substream);
780 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
781 unsigned int idx, count, res;
782 int timeout = 5000;
783
784 snd_assert(viadev->tbl_entries, return 0);
785 if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
786 return 0;
787 spin_lock(&chip->reg_lock);
788 do {
789 count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
790
791 if ((count & 0xffffff) || ! viadev->running)
792 break;
793 } while (--timeout);
794 if (! timeout)
795 snd_printd(KERN_ERR "zero position is read\n");
796 idx = count >> 24;
797 if (idx >= viadev->tbl_entries) {
798#ifdef POINTER_DEBUG
799 printk("fail: invalid idx = %i/%i\n", idx, viadev->tbl_entries);
800#endif
801 res = viadev->lastpos;
802 } else {
803 count &= 0xffffff;
804 res = calc_linear_pos(viadev, idx, count);
805 }
806 spin_unlock(&chip->reg_lock);
807
808 return bytes_to_frames(substream->runtime, res);
809}
810
811
812
813
814
815
816static int snd_via82xx_hw_params(snd_pcm_substream_t * substream,
817 snd_pcm_hw_params_t * hw_params)
818{
819 via82xx_t *chip = snd_pcm_substream_chip(substream);
820 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
821 int err;
822
823 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
824 if (err < 0)
825 return err;
826 err = build_via_table(viadev, substream, chip->pci,
827 params_periods(hw_params),
828 params_period_bytes(hw_params));
829 if (err < 0)
830 return err;
831
832 return 0;
833}
834
835
836
837
838
839static int snd_via82xx_hw_free(snd_pcm_substream_t * substream)
840{
841 via82xx_t *chip = snd_pcm_substream_chip(substream);
842 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
843
844 clean_via_table(viadev, substream, chip->pci);
845 snd_pcm_lib_free_pages(substream);
846 return 0;
847}
848
849
850
851
852
853static void snd_via82xx_set_table_ptr(via82xx_t *chip, viadev_t *viadev)
854{
855 snd_via82xx_codec_ready(chip, 0);
856 outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
857 udelay(20);
858 snd_via82xx_codec_ready(chip, 0);
859}
860
861
862
863
864static void via686_setup_format(via82xx_t *chip, viadev_t *viadev, snd_pcm_runtime_t *runtime)
865{
866 snd_via82xx_channel_reset(chip, viadev);
867
868 snd_via82xx_set_table_ptr(chip, viadev);
869 outb(VIA_REG_TYPE_AUTOSTART |
870 (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
871 (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
872 ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
873 VIA_REG_TYPE_INT_EOL |
874 VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
875}
876
877static int snd_via686_playback_prepare(snd_pcm_substream_t *substream)
878{
879 via82xx_t *chip = snd_pcm_substream_chip(substream);
880 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
881 snd_pcm_runtime_t *runtime = substream->runtime;
882
883 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
884 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
885 via686_setup_format(chip, viadev, runtime);
886 return 0;
887}
888
889static int snd_via686_capture_prepare(snd_pcm_substream_t *substream)
890{
891 via82xx_t *chip = snd_pcm_substream_chip(substream);
892 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
893 snd_pcm_runtime_t *runtime = substream->runtime;
894
895 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
896 via686_setup_format(chip, viadev, runtime);
897 return 0;
898}
899
900
901
902
903static int via_lock_rate(struct via_rate_lock *rec, int rate)
904{
905 int changed = 0;
906
907 spin_lock_irq(&rec->lock);
908 if (rec->rate != rate) {
909 if (rec->rate && rec->used > 1)
910 changed = -EINVAL;
911 else {
912 rec->rate = rate;
913 changed = 1;
914 }
915 }
916 spin_unlock_irq(&rec->lock);
917 return changed;
918}
919
920
921
922
923static int snd_via8233_playback_prepare(snd_pcm_substream_t *substream)
924{
925 via82xx_t *chip = snd_pcm_substream_chip(substream);
926 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
927 snd_pcm_runtime_t *runtime = substream->runtime;
928 int rate_changed;
929 u32 rbits;
930
931 if ((rate_changed = via_lock_rate(&chip->rates[0], runtime->rate)) < 0)
932 return rate_changed;
933 if (rate_changed) {
934 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
935 chip->no_vra ? 48000 : runtime->rate);
936 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
937 }
938 if (runtime->rate == 48000)
939 rbits = 0xfffff;
940 else
941 rbits = (0x100000 / 48000) * runtime->rate + ((0x100000 % 48000) * runtime->rate) / 48000;
942 snd_assert((rbits & ~0xfffff) == 0, return -EINVAL);
943 snd_via82xx_channel_reset(chip, viadev);
944 snd_via82xx_set_table_ptr(chip, viadev);
945 outb(chip->playback_volume[viadev->reg_offset / 0x10][0], VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
946 outb(chip->playback_volume[viadev->reg_offset / 0x10][1], VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
947 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
948 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
949 rbits |
950 0xff000000,
951 VIADEV_REG(viadev, OFFSET_STOP_IDX));
952 udelay(20);
953 snd_via82xx_codec_ready(chip, 0);
954 return 0;
955}
956
957
958
959
960static int snd_via8233_multi_prepare(snd_pcm_substream_t *substream)
961{
962 via82xx_t *chip = snd_pcm_substream_chip(substream);
963 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
964 snd_pcm_runtime_t *runtime = substream->runtime;
965 unsigned int slots;
966 int fmt;
967
968 if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
969 return -EINVAL;
970 snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
971 snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
972 snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
973 snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
974 snd_via82xx_channel_reset(chip, viadev);
975 snd_via82xx_set_table_ptr(chip, viadev);
976
977 fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ? VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
978 fmt |= runtime->channels << 4;
979 outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
980#if 0
981 if (chip->revision == VIA_REV_8233A)
982 slots = 0;
983 else
984#endif
985 {
986
987
988 switch (runtime->channels) {
989 case 1: slots = (1<<0) | (1<<4); break;
990 case 2: slots = (1<<0) | (2<<4); break;
991 case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
992 case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
993 case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
994 case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
995 default: slots = 0; break;
996 }
997 }
998
999 outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
1000 udelay(20);
1001 snd_via82xx_codec_ready(chip, 0);
1002 return 0;
1003}
1004
1005
1006
1007
1008static int snd_via8233_capture_prepare(snd_pcm_substream_t *substream)
1009{
1010 via82xx_t *chip = snd_pcm_substream_chip(substream);
1011 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
1012 snd_pcm_runtime_t *runtime = substream->runtime;
1013
1014 if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
1015 return -EINVAL;
1016 snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
1017 snd_via82xx_channel_reset(chip, viadev);
1018 snd_via82xx_set_table_ptr(chip, viadev);
1019 outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
1020 outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
1021 (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
1022 0xff000000,
1023 VIADEV_REG(viadev, OFFSET_STOP_IDX));
1024 udelay(20);
1025 snd_via82xx_codec_ready(chip, 0);
1026 return 0;
1027}
1028
1029
1030
1031
1032
1033static snd_pcm_hardware_t snd_via82xx_hw =
1034{
1035 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1036 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1037 SNDRV_PCM_INFO_MMAP_VALID |
1038 SNDRV_PCM_INFO_RESUME |
1039 SNDRV_PCM_INFO_PAUSE),
1040 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1041 .rates = SNDRV_PCM_RATE_48000,
1042 .rate_min = 48000,
1043 .rate_max = 48000,
1044 .channels_min = 1,
1045 .channels_max = 2,
1046 .buffer_bytes_max = 128 * 1024,
1047 .period_bytes_min = 32,
1048 .period_bytes_max = 128 * 1024,
1049 .periods_min = 2,
1050 .periods_max = VIA_TABLE_SIZE / 2,
1051 .fifo_size = 0,
1052};
1053
1054
1055
1056
1057
1058static int snd_via82xx_pcm_open(via82xx_t *chip, viadev_t *viadev, snd_pcm_substream_t * substream)
1059{
1060 snd_pcm_runtime_t *runtime = substream->runtime;
1061 int err;
1062 struct via_rate_lock *ratep;
1063
1064 runtime->hw = snd_via82xx_hw;
1065
1066
1067 ratep = &chip->rates[viadev->direction];
1068 spin_lock_irq(&ratep->lock);
1069 ratep->used++;
1070 if (chip->spdif_on && viadev->reg_offset == 0x30) {
1071
1072 runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
1073 snd_pcm_limit_hw_rates(runtime);
1074 } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
1075
1076 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1077 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1078 } else if (! ratep->rate) {
1079 int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
1080 runtime->hw.rates = chip->ac97->rates[idx];
1081 snd_pcm_limit_hw_rates(runtime);
1082 } else {
1083
1084 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1085 runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
1086 }
1087 spin_unlock_irq(&ratep->lock);
1088
1089
1090
1091 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1092 return err;
1093
1094 runtime->private_data = viadev;
1095 viadev->substream = substream;
1096
1097 return 0;
1098}
1099
1100
1101
1102
1103
1104static int snd_via82xx_playback_open(snd_pcm_substream_t * substream)
1105{
1106 via82xx_t *chip = snd_pcm_substream_chip(substream);
1107 viadev_t *viadev = &chip->devs[chip->playback_devno + substream->number];
1108 int err;
1109
1110 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1111 return err;
1112 return 0;
1113}
1114
1115
1116
1117
1118static int snd_via8233_multi_open(snd_pcm_substream_t * substream)
1119{
1120 via82xx_t *chip = snd_pcm_substream_chip(substream);
1121 viadev_t *viadev = &chip->devs[chip->multi_devno];
1122 int err;
1123
1124
1125
1126 static unsigned int channels[] = {
1127 1, 2, 4, 6
1128 };
1129 static snd_pcm_hw_constraint_list_t hw_constraints_channels = {
1130 .count = ARRAY_SIZE(channels),
1131 .list = channels,
1132 .mask = 0,
1133 };
1134
1135 if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
1136 return err;
1137 substream->runtime->hw.channels_max = 6;
1138 if (chip->revision == VIA_REV_8233A)
1139 snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels);
1140 return 0;
1141}
1142
1143
1144
1145
1146static int snd_via82xx_capture_open(snd_pcm_substream_t * substream)
1147{
1148 via82xx_t *chip = snd_pcm_substream_chip(substream);
1149 viadev_t *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
1150
1151 return snd_via82xx_pcm_open(chip, viadev, substream);
1152}
1153
1154
1155
1156
1157static int snd_via82xx_pcm_close(snd_pcm_substream_t * substream)
1158{
1159 via82xx_t *chip = snd_pcm_substream_chip(substream);
1160 viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
1161 struct via_rate_lock *ratep;
1162
1163
1164 ratep = &chip->rates[viadev->direction];
1165 spin_lock_irq(&ratep->lock);
1166 ratep->used--;
1167 if (! ratep->used)
1168 ratep->rate = 0;
1169 spin_unlock_irq(&ratep->lock);
1170
1171 viadev->substream = NULL;
1172 return 0;
1173}
1174
1175
1176
1177static snd_pcm_ops_t snd_via686_playback_ops = {
1178 .open = snd_via82xx_playback_open,
1179 .close = snd_via82xx_pcm_close,
1180 .ioctl = snd_pcm_lib_ioctl,
1181 .hw_params = snd_via82xx_hw_params,
1182 .hw_free = snd_via82xx_hw_free,
1183 .prepare = snd_via686_playback_prepare,
1184 .trigger = snd_via82xx_pcm_trigger,
1185 .pointer = snd_via686_pcm_pointer,
1186 .page = snd_pcm_sgbuf_ops_page,
1187};
1188
1189
1190static snd_pcm_ops_t snd_via686_capture_ops = {
1191 .open = snd_via82xx_capture_open,
1192 .close = snd_via82xx_pcm_close,
1193 .ioctl = snd_pcm_lib_ioctl,
1194 .hw_params = snd_via82xx_hw_params,
1195 .hw_free = snd_via82xx_hw_free,
1196 .prepare = snd_via686_capture_prepare,
1197 .trigger = snd_via82xx_pcm_trigger,
1198 .pointer = snd_via686_pcm_pointer,
1199 .page = snd_pcm_sgbuf_ops_page,
1200};
1201
1202
1203static snd_pcm_ops_t snd_via8233_playback_ops = {
1204 .open = snd_via82xx_playback_open,
1205 .close = snd_via82xx_pcm_close,
1206 .ioctl = snd_pcm_lib_ioctl,
1207 .hw_params = snd_via82xx_hw_params,
1208 .hw_free = snd_via82xx_hw_free,
1209 .prepare = snd_via8233_playback_prepare,
1210 .trigger = snd_via82xx_pcm_trigger,
1211 .pointer = snd_via8233_pcm_pointer,
1212 .page = snd_pcm_sgbuf_ops_page,
1213};
1214
1215
1216static snd_pcm_ops_t snd_via8233_multi_ops = {
1217 .open = snd_via8233_multi_open,
1218 .close = snd_via82xx_pcm_close,
1219 .ioctl = snd_pcm_lib_ioctl,
1220 .hw_params = snd_via82xx_hw_params,
1221 .hw_free = snd_via82xx_hw_free,
1222 .prepare = snd_via8233_multi_prepare,
1223 .trigger = snd_via82xx_pcm_trigger,
1224 .pointer = snd_via8233_pcm_pointer,
1225 .page = snd_pcm_sgbuf_ops_page,
1226};
1227
1228
1229static snd_pcm_ops_t snd_via8233_capture_ops = {
1230 .open = snd_via82xx_capture_open,
1231 .close = snd_via82xx_pcm_close,
1232 .ioctl = snd_pcm_lib_ioctl,
1233 .hw_params = snd_via82xx_hw_params,
1234 .hw_free = snd_via82xx_hw_free,
1235 .prepare = snd_via8233_capture_prepare,
1236 .trigger = snd_via82xx_pcm_trigger,
1237 .pointer = snd_via8233_pcm_pointer,
1238 .page = snd_pcm_sgbuf_ops_page,
1239};
1240
1241
1242static void init_viadev(via82xx_t *chip, int idx, unsigned int reg_offset, int direction)
1243{
1244 chip->devs[idx].reg_offset = reg_offset;
1245 chip->devs[idx].direction = direction;
1246 chip->devs[idx].port = chip->port + reg_offset;
1247}
1248
1249
1250
1251
1252static int __devinit snd_via8233_pcm_new(via82xx_t *chip)
1253{
1254 snd_pcm_t *pcm;
1255 int i, err;
1256
1257 chip->playback_devno = 0;
1258 chip->multi_devno = 4;
1259 chip->capture_devno = 5;
1260 chip->num_devs = 7;
1261 chip->intr_mask = 0x33033333;
1262
1263
1264 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
1265 if (err < 0)
1266 return err;
1267 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1268 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1269 pcm->private_data = chip;
1270 strcpy(pcm->name, chip->card->shortname);
1271 chip->pcms[0] = pcm;
1272
1273 for (i = 0; i < 4; i++)
1274 init_viadev(chip, i, 0x10 * i, 0);
1275
1276 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 1);
1277
1278 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1279 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1280 return err;
1281
1282
1283 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
1284 if (err < 0)
1285 return err;
1286 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1287 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1288 pcm->private_data = chip;
1289 strcpy(pcm->name, chip->card->shortname);
1290 chip->pcms[1] = pcm;
1291
1292 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 0);
1293
1294 init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 1);
1295
1296 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1297 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1298 return err;
1299
1300 return 0;
1301}
1302
1303
1304
1305
1306static int __devinit snd_via8233a_pcm_new(via82xx_t *chip)
1307{
1308 snd_pcm_t *pcm;
1309 int err;
1310
1311 chip->multi_devno = 0;
1312 chip->playback_devno = 1;
1313 chip->capture_devno = 2;
1314 chip->num_devs = 3;
1315 chip->intr_mask = 0x03033000;
1316
1317
1318 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1319 if (err < 0)
1320 return err;
1321 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
1322 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
1323 pcm->private_data = chip;
1324 strcpy(pcm->name, chip->card->shortname);
1325 chip->pcms[0] = pcm;
1326
1327 init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 0);
1328
1329 init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 1);
1330
1331 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1332 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1333 return err;
1334
1335
1336 if (! ac97_can_spdif(chip->ac97))
1337 return 0;
1338
1339
1340 err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
1341 if (err < 0)
1342 return err;
1343 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
1344 pcm->private_data = chip;
1345 strcpy(pcm->name, chip->card->shortname);
1346 chip->pcms[1] = pcm;
1347
1348 init_viadev(chip, chip->playback_devno, 0x30, 0);
1349
1350 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1351 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1352 return err;
1353
1354 return 0;
1355}
1356
1357
1358
1359
1360static int __devinit snd_via686_pcm_new(via82xx_t *chip)
1361{
1362 snd_pcm_t *pcm;
1363 int err;
1364
1365 chip->playback_devno = 0;
1366 chip->capture_devno = 1;
1367 chip->num_devs = 2;
1368 chip->intr_mask = 0x77;
1369
1370 err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
1371 if (err < 0)
1372 return err;
1373 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
1374 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
1375 pcm->private_data = chip;
1376 strcpy(pcm->name, chip->card->shortname);
1377 chip->pcms[0] = pcm;
1378 init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0);
1379 init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 1);
1380
1381 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1382 snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
1383 return err;
1384
1385 return 0;
1386}
1387
1388
1389
1390
1391
1392
1393static int snd_via8233_capture_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1394{
1395
1396
1397
1398 static char *texts[2] = {
1399 "Input1", "Input2"
1400 };
1401 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1402 uinfo->count = 1;
1403 uinfo->value.enumerated.items = 2;
1404 if (uinfo->value.enumerated.item >= 2)
1405 uinfo->value.enumerated.item = 1;
1406 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1407 return 0;
1408}
1409
1410static int snd_via8233_capture_source_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1411{
1412 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1413 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1414 ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
1415 return 0;
1416}
1417
1418static int snd_via8233_capture_source_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1419{
1420 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1421 unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
1422 u8 val, oval;
1423
1424 spin_lock_irq(&chip->reg_lock);
1425 oval = inb(port);
1426 val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
1427 if (ucontrol->value.enumerated.item[0])
1428 val |= VIA_REG_CAPTURE_CHANNEL_MIC;
1429 if (val != oval)
1430 outb(val, port);
1431 spin_unlock_irq(&chip->reg_lock);
1432 return val != oval;
1433}
1434
1435static snd_kcontrol_new_t snd_via8233_capture_source __devinitdata = {
1436 .name = "Input Source Select",
1437 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1438 .info = snd_via8233_capture_source_info,
1439 .get = snd_via8233_capture_source_get,
1440 .put = snd_via8233_capture_source_put,
1441};
1442
1443static int snd_via8233_dxs3_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1444{
1445 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1446 uinfo->count = 1;
1447 uinfo->value.integer.min = 0;
1448 uinfo->value.integer.max = 1;
1449 return 0;
1450}
1451
1452static int snd_via8233_dxs3_spdif_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1453{
1454 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1455 u8 val;
1456
1457 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1458 ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
1459 return 0;
1460}
1461
1462static int snd_via8233_dxs3_spdif_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1463{
1464 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1465 u8 val, oval;
1466
1467 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
1468 val = oval & ~VIA8233_SPDIF_DX3;
1469 if (ucontrol->value.integer.value[0])
1470 val |= VIA8233_SPDIF_DX3;
1471
1472 chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
1473 if (val != oval) {
1474 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1475 return 1;
1476 }
1477 return 0;
1478}
1479
1480static snd_kcontrol_new_t snd_via8233_dxs3_spdif_control __devinitdata = {
1481 .name = "IEC958 Output Switch",
1482 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1483 .info = snd_via8233_dxs3_spdif_info,
1484 .get = snd_via8233_dxs3_spdif_get,
1485 .put = snd_via8233_dxs3_spdif_put,
1486};
1487
1488static int snd_via8233_dxs_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1489{
1490 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1491 uinfo->count = 2;
1492 uinfo->value.integer.min = 0;
1493 uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
1494 return 0;
1495}
1496
1497static int snd_via8233_dxs_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1498{
1499 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1500 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1501 ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
1502 ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
1503 return 0;
1504}
1505
1506static int snd_via8233_dxs_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1507{
1508 via82xx_t *chip = snd_kcontrol_chip(kcontrol);
1509 unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
1510 unsigned long port = chip->port + 0x10 * idx;
1511 unsigned char val;
1512 int i, change = 0;
1513
1514 for (i = 0; i < 2; i++) {
1515 val = ucontrol->value.integer.value[i];
1516 if (val > VIA_DXS_MAX_VOLUME)
1517 val = VIA_DXS_MAX_VOLUME;
1518 val = VIA_DXS_MAX_VOLUME - val;
1519 change |= val != chip->playback_volume[idx][i];
1520 if (change) {
1521 chip->playback_volume[idx][i] = val;
1522 outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1523 }
1524 }
1525 return change;
1526}
1527
1528static snd_kcontrol_new_t snd_via8233_dxs_volume_control __devinitdata = {
1529 .name = "VIA DXS Playback Volume",
1530 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1531 .count = 4,
1532 .info = snd_via8233_dxs_volume_info,
1533 .get = snd_via8233_dxs_volume_get,
1534 .put = snd_via8233_dxs_volume_put,
1535};
1536
1537
1538
1539
1540static void snd_via82xx_mixer_free_ac97_bus(ac97_bus_t *bus)
1541{
1542 via82xx_t *chip = bus->private_data;
1543 chip->ac97_bus = NULL;
1544}
1545
1546static void snd_via82xx_mixer_free_ac97(ac97_t *ac97)
1547{
1548 via82xx_t *chip = ac97->private_data;
1549 chip->ac97 = NULL;
1550}
1551
1552static struct ac97_quirk ac97_quirks[] = {
1553 {
1554 .vendor = 0x1106,
1555 .device = 0x4161,
1556 .codec_id = 0x56494161,
1557 .name = "Soltek SL-75DRV5",
1558 .type = AC97_TUNE_NONE
1559 },
1560 {
1561 .vendor = 0x1106,
1562 .device = 0x4161,
1563 .name = "ASRock K7VT2",
1564 .type = AC97_TUNE_HP_ONLY
1565 },
1566 {
1567 .vendor = 0x1019,
1568 .device = 0x0a81,
1569 .name = "ECS K7VTA3",
1570 .type = AC97_TUNE_HP_ONLY
1571 },
1572 {
1573 .vendor = 0x1019,
1574 .device = 0x0a85,
1575 .name = "ECS L7VMM2",
1576 .type = AC97_TUNE_HP_ONLY
1577 },
1578 {
1579 .vendor = 0x1849,
1580 .device = 0x3059,
1581 .name = "ASRock K7VM2",
1582 .type = AC97_TUNE_HP_ONLY
1583 },
1584 {
1585 .vendor = 0x14cd,
1586 .device = 0x7002,
1587 .name = "Unknown",
1588 .type = AC97_TUNE_ALC_JACK
1589 },
1590 {
1591 .vendor = 0x1071,
1592 .device = 0x8590,
1593 .name = "Mitac Mobo",
1594 .type = AC97_TUNE_ALC_JACK
1595 },
1596 {
1597 .vendor = 0x161f,
1598 .device = 0x202b,
1599 .name = "Arima Notebook",
1600 .type = AC97_TUNE_HP_ONLY,
1601 },
1602 { }
1603};
1604
1605static int __devinit snd_via82xx_mixer_new(via82xx_t *chip, const char *quirk_override)
1606{
1607 ac97_template_t ac97;
1608 int err;
1609 static ac97_bus_ops_t ops = {
1610 .write = snd_via82xx_codec_write,
1611 .read = snd_via82xx_codec_read,
1612 .wait = snd_via82xx_codec_wait,
1613 };
1614
1615 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1616 return err;
1617 chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
1618 chip->ac97_bus->clock = chip->ac97_clock;
1619 chip->ac97_bus->shared_type = AC97_SHARED_TYPE_VIA;
1620
1621 memset(&ac97, 0, sizeof(ac97));
1622 ac97.private_data = chip;
1623 ac97.private_free = snd_via82xx_mixer_free_ac97;
1624 ac97.pci = chip->pci;
1625 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1626 return err;
1627
1628 snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
1629
1630 if (chip->chip_type != TYPE_VIA686) {
1631
1632 snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1633 }
1634
1635 return 0;
1636}
1637
1638
1639
1640
1641
1642static int snd_via8233_init_misc(via82xx_t *chip, int dev)
1643{
1644 int i, err, caps;
1645 unsigned char val;
1646
1647 caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
1648 for (i = 0; i < caps; i++) {
1649 snd_via8233_capture_source.index = i;
1650 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
1651 if (err < 0)
1652 return err;
1653 }
1654 if (ac97_can_spdif(chip->ac97)) {
1655 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
1656 if (err < 0)
1657 return err;
1658 }
1659 if (chip->chip_type != TYPE_VIA8233A) {
1660 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs_volume_control, chip));
1661 if (err < 0)
1662 return err;
1663 }
1664
1665
1666 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
1667 val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
1668 val &= ~VIA8233_SPDIF_DX3;
1669 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
1670
1671 return 0;
1672}
1673
1674static int snd_via686_init_misc(via82xx_t *chip, int dev)
1675{
1676 unsigned char legacy, legacy_cfg;
1677 int rev_h = 0;
1678
1679 legacy = chip->old_legacy;
1680 legacy_cfg = chip->old_legacy_cfg;
1681 legacy |= VIA_FUNC_MIDI_IRQMASK;
1682 legacy &= ~VIA_FUNC_ENABLE_GAME;
1683 if (chip->revision >= VIA_REV_686_H) {
1684 rev_h = 1;
1685 if (mpu_port[dev] >= 0x200) {
1686 mpu_port[dev] &= 0xfffc;
1687 pci_write_config_dword(chip->pci, 0x18, mpu_port[dev] | 0x01);
1688#ifdef CONFIG_PM
1689 chip->mpu_port_saved = mpu_port[dev];
1690#endif
1691 } else {
1692 mpu_port[dev] = pci_resource_start(chip->pci, 2);
1693 }
1694 } else {
1695 switch (mpu_port[dev]) {
1696 case 0x300:
1697 case 0x310:
1698 case 0x320:
1699 case 0x330:
1700 legacy_cfg &= ~(3 << 2);
1701 legacy_cfg |= (mpu_port[dev] & 0x0030) >> 2;
1702 break;
1703 default:
1704 if (legacy & VIA_FUNC_ENABLE_MIDI)
1705 mpu_port[dev] = 0x300 + ((legacy_cfg & 0x000c) << 2);
1706 break;
1707 }
1708 }
1709 if (mpu_port[dev] >= 0x200 &&
1710 (chip->mpu_res = request_region(mpu_port[dev], 2, "VIA82xx MPU401")) != NULL) {
1711 if (rev_h)
1712 legacy |= VIA_FUNC_MIDI_PNP;
1713 legacy |= VIA_FUNC_ENABLE_MIDI;
1714 } else {
1715 if (rev_h)
1716 legacy &= ~VIA_FUNC_MIDI_PNP;
1717 legacy &= ~VIA_FUNC_ENABLE_MIDI;
1718 mpu_port[dev] = 0;
1719 }
1720
1721#ifdef SUPPORT_JOYSTICK
1722#define JOYSTICK_ADDR 0x200
1723 if (joystick[dev] &&
1724 (chip->res_joystick = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport")) != NULL) {
1725 legacy |= VIA_FUNC_ENABLE_GAME;
1726 chip->gameport.io = JOYSTICK_ADDR;
1727 }
1728#endif
1729
1730 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1731 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
1732 if (chip->mpu_res) {
1733 if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
1734 mpu_port[dev], 1,
1735 chip->irq, 0, &chip->rmidi) < 0) {
1736 printk(KERN_WARNING "unable to initialize MPU-401 at 0x%lx, skipping\n", mpu_port[dev]);
1737 legacy &= ~VIA_FUNC_ENABLE_MIDI;
1738 } else {
1739 legacy &= ~VIA_FUNC_MIDI_IRQMASK;
1740 }
1741 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
1742 }
1743
1744#ifdef SUPPORT_JOYSTICK
1745 if (chip->res_joystick)
1746 gameport_register_port(&chip->gameport);
1747#endif
1748
1749#ifdef CONFIG_PM
1750 chip->legacy_saved = legacy;
1751 chip->legacy_cfg_saved = legacy_cfg;
1752#endif
1753
1754 return 0;
1755}
1756
1757
1758
1759
1760
1761static void snd_via82xx_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
1762{
1763 via82xx_t *chip = entry->private_data;
1764 int i;
1765
1766 snd_iprintf(buffer, "%s\n\n", chip->card->longname);
1767 for (i = 0; i < 0xa0; i += 4) {
1768 snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
1769 }
1770}
1771
1772static void __devinit snd_via82xx_proc_init(via82xx_t *chip)
1773{
1774 snd_info_entry_t *entry;
1775
1776 if (! snd_card_proc_new(chip->card, "via82xx", &entry))
1777 snd_info_set_text_ops(entry, chip, 1024, snd_via82xx_proc_read);
1778}
1779
1780
1781
1782
1783
1784static int __devinit snd_via82xx_chip_init(via82xx_t *chip)
1785{
1786 ac97_t ac97;
1787 unsigned int val;
1788 int max_count;
1789 unsigned char pval;
1790
1791 memset(&ac97, 0, sizeof(ac97));
1792 ac97.private_data = chip;
1793
1794#if 0
1795 if (chip->chip_type == TYPE_VIA686)
1796
1797 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
1798#endif
1799 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
1800 if (! (pval & VIA_ACLINK_C00_READY)) {
1801
1802 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
1803 VIA_ACLINK_CTRL_ENABLE |
1804 VIA_ACLINK_CTRL_RESET |
1805 VIA_ACLINK_CTRL_SYNC);
1806 udelay(100);
1807#if 1
1808 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
1809 udelay(100);
1810#else
1811
1812 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
1813 VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
1814 udelay(2);
1815#endif
1816
1817
1818 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
1819 udelay(100);
1820 }
1821
1822
1823
1824 pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
1825 if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
1826
1827
1828 pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
1829 udelay(100);
1830 }
1831
1832
1833 max_count = ((3 * HZ) / 4) + 1;
1834 do {
1835 pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
1836 if (pval & VIA_ACLINK_C00_READY)
1837 break;
1838 set_current_state(TASK_UNINTERRUPTIBLE);
1839 schedule_timeout(1);
1840 } while (--max_count > 0);
1841
1842 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
1843 snd_printk("AC'97 codec is not ready [0x%x]\n", val);
1844
1845
1846 snd_via82xx_codec_ready(chip, 0);
1847 snd_via82xx_codec_write(&ac97, AC97_RESET, 0x0000);
1848 snd_via82xx_codec_read(&ac97, 0);
1849
1850#if 0
1851 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
1852 VIA_REG_AC97_SECONDARY_VALID |
1853 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
1854 max_count = ((3 * HZ) / 4) + 1;
1855 snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
1856 VIA_REG_AC97_SECONDARY_VALID |
1857 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
1858 do {
1859 if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
1860 chip->ac97_secondary = 1;
1861 goto __ac97_ok2;
1862 }
1863 set_current_state(TASK_INTERRUPTIBLE);
1864 schedule_timeout(1);
1865 } while (--max_count > 0);
1866
1867
1868 __ac97_ok2:
1869#endif
1870
1871 if (chip->chip_type == TYPE_VIA686) {
1872
1873 pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
1874
1875 outl(0, VIAREG(chip, GPI_INTR));
1876 }
1877
1878 if (chip->chip_type != TYPE_VIA686) {
1879
1880
1881
1882 struct pci_dev *pci;
1883 pci = pci_find_device(0x1106, 0x3068, NULL);
1884 if (pci) {
1885 unsigned char data;
1886 pci_read_config_byte(pci, 0x44, &data);
1887 pci_write_config_byte(pci, 0x44, data | 0x40);
1888 }
1889 }
1890
1891 return 0;
1892}
1893
1894#ifdef CONFIG_PM
1895
1896
1897
1898static int snd_via82xx_suspend(snd_card_t *card, unsigned int state)
1899{
1900 via82xx_t *chip = card->pm_private_data;
1901 int i;
1902
1903 for (i = 0; i < 2; i++)
1904 if (chip->pcms[i])
1905 snd_pcm_suspend_all(chip->pcms[i]);
1906 for (i = 0; i < chip->num_devs; i++)
1907 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1908 synchronize_irq(chip->irq);
1909 snd_ac97_suspend(chip->ac97);
1910
1911
1912 if (chip->chip_type != TYPE_VIA686) {
1913 pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
1914 chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
1915 chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
1916 }
1917
1918 pci_set_power_state(chip->pci, 3);
1919 pci_disable_device(chip->pci);
1920 return 0;
1921}
1922
1923static int snd_via82xx_resume(snd_card_t *card, unsigned int state)
1924{
1925 via82xx_t *chip = card->pm_private_data;
1926 int idx, i;
1927
1928 pci_enable_device(chip->pci);
1929 pci_set_power_state(chip->pci, 0);
1930
1931 snd_via82xx_chip_init(chip);
1932
1933 if (chip->chip_type == TYPE_VIA686) {
1934 if (chip->mpu_port_saved)
1935 pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
1936 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
1937 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
1938 } else {
1939 pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
1940 outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
1941 outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
1942 for (idx = 0; idx < 4; idx++) {
1943 unsigned long port = chip->port + 0x10 * idx;
1944 for (i = 0; i < 2; i++)
1945 outb(chip->playback_volume[idx][i], port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
1946 }
1947 }
1948
1949 snd_ac97_resume(chip->ac97);
1950
1951 for (i = 0; i < chip->num_devs; i++)
1952 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1953
1954 return 0;
1955}
1956#endif
1957
1958static int snd_via82xx_free(via82xx_t *chip)
1959{
1960 unsigned int i;
1961
1962 if (chip->irq < 0)
1963 goto __end_hw;
1964
1965 for (i = 0; i < chip->num_devs; i++)
1966 snd_via82xx_channel_reset(chip, &chip->devs[i]);
1967 synchronize_irq(chip->irq);
1968 __end_hw:
1969 if (chip->irq >= 0)
1970 free_irq(chip->irq, (void *)chip);
1971 if (chip->mpu_res) {
1972 release_resource(chip->mpu_res);
1973 kfree_nocheck(chip->mpu_res);
1974 }
1975 pci_release_regions(chip->pci);
1976 if (chip->chip_type == TYPE_VIA686) {
1977#ifdef SUPPORT_JOYSTICK
1978 if (chip->res_joystick) {
1979 gameport_unregister_port(&chip->gameport);
1980 release_resource(chip->res_joystick);
1981 kfree_nocheck(chip->res_joystick);
1982 }
1983#endif
1984 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
1985 pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
1986 }
1987 pci_disable_device(chip->pci);
1988 kfree(chip);
1989 return 0;
1990}
1991
1992static int snd_via82xx_dev_free(snd_device_t *device)
1993{
1994 via82xx_t *chip = device->device_data;
1995 return snd_via82xx_free(chip);
1996}
1997
1998static int __devinit snd_via82xx_create(snd_card_t * card,
1999 struct pci_dev *pci,
2000 int chip_type,
2001 int revision,
2002 unsigned int ac97_clock,
2003 via82xx_t ** r_via)
2004{
2005 via82xx_t *chip;
2006 int err;
2007 static snd_device_ops_t ops = {
2008 .dev_free = snd_via82xx_dev_free,
2009 };
2010
2011 if ((err = pci_enable_device(pci)) < 0)
2012 return err;
2013
2014 if ((chip = kcalloc(1, sizeof(*chip), GFP_KERNEL)) == NULL) {
2015 pci_disable_device(pci);
2016 return -ENOMEM;
2017 }
2018
2019 chip->chip_type = chip_type;
2020 chip->revision = revision;
2021
2022 spin_lock_init(&chip->reg_lock);
2023 spin_lock_init(&chip->rates[0].lock);
2024 spin_lock_init(&chip->rates[1].lock);
2025 chip->card = card;
2026 chip->pci = pci;
2027 chip->irq = -1;
2028
2029 pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
2030 pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
2031 pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
2032 chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
2033
2034 if ((err = pci_request_regions(pci, card->driver)) < 0) {
2035 kfree(chip);
2036 pci_disable_device(pci);
2037 return err;
2038 }
2039 chip->port = pci_resource_start(pci, 0);
2040 if (request_irq(pci->irq, snd_via82xx_interrupt, SA_INTERRUPT|SA_SHIRQ,
2041 card->driver, (void *)chip)) {
2042 snd_printk("unable to grab IRQ %d\n", pci->irq);
2043 snd_via82xx_free(chip);
2044 return -EBUSY;
2045 }
2046 chip->irq = pci->irq;
2047 if (ac97_clock >= 8000 && ac97_clock <= 48000)
2048 chip->ac97_clock = ac97_clock;
2049 synchronize_irq(chip->irq);
2050
2051 if ((err = snd_via82xx_chip_init(chip)) < 0) {
2052 snd_via82xx_free(chip);
2053 return err;
2054 }
2055
2056 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2057 snd_via82xx_free(chip);
2058 return err;
2059 }
2060
2061
2062
2063
2064 pci_set_master(pci);
2065
2066 snd_card_set_dev(card, &pci->dev);
2067
2068 *r_via = chip;
2069 return 0;
2070}
2071
2072struct via823x_info {
2073 int revision;
2074 char *name;
2075 int type;
2076};
2077static struct via823x_info via823x_cards[] __devinitdata = {
2078 { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
2079 { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
2080 { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
2081 { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
2082 { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
2083 { VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
2084};
2085
2086
2087
2088
2089struct dxs_whitelist {
2090 unsigned short vendor;
2091 unsigned short device;
2092 unsigned short mask;
2093 short action;
2094};
2095
2096static int __devinit check_dxs_list(struct pci_dev *pci)
2097{
2098 static struct dxs_whitelist whitelist[] = {
2099 { .vendor = 0x1005, .device = 0x4710, .action = VIA_DXS_ENABLE },
2100 { .vendor = 0x1019, .device = 0x0996, .action = VIA_DXS_48K },
2101 { .vendor = 0x1019, .device = 0x0a81, .action = VIA_DXS_NO_VRA },
2102 { .vendor = 0x1019, .device = 0x0a85, .action = VIA_DXS_NO_VRA },
2103 { .vendor = 0x1025, .device = 0x0033, .action = VIA_DXS_NO_VRA },
2104 { .vendor = 0x1043, .device = 0x8095, .action = VIA_DXS_NO_VRA },
2105 { .vendor = 0x1043, .device = 0x80a1, .action = VIA_DXS_NO_VRA },
2106 { .vendor = 0x1043, .device = 0x80b0, .action = VIA_DXS_NO_VRA },
2107 { .vendor = 0x1071, .device = 0x8375, .action = VIA_DXS_NO_VRA },
2108 { .vendor = 0x10cf, .device = 0x118e, .action = VIA_DXS_ENABLE },
2109 { .vendor = 0x1106, .device = 0x4161, .action = VIA_DXS_NO_VRA },
2110 { .vendor = 0x1106, .device = 0x4552, .action = VIA_DXS_NO_VRA },
2111 { .vendor = 0x1106, .device = 0xaa01, .action = VIA_DXS_NO_VRA },
2112 { .vendor = 0x1297, .device = 0xa232, .action = VIA_DXS_ENABLE },
2113 { .vendor = 0x1297, .device = 0xc160, .action = VIA_DXS_ENABLE },
2114 { .vendor = 0x1458, .device = 0xa002, .action = VIA_DXS_ENABLE },
2115 { .vendor = 0x1462, .device = 0x3800, .action = VIA_DXS_ENABLE },
2116 { .vendor = 0x1462, .device = 0x5901, .action = VIA_DXS_NO_VRA },
2117 { .vendor = 0x1462, .device = 0x7120, .action = VIA_DXS_ENABLE },
2118 { .vendor = 0x147b, .device = 0x1401, .action = VIA_DXS_ENABLE },
2119 { .vendor = 0x147b, .device = 0x1411, .action = VIA_DXS_ENABLE },
2120 { .vendor = 0x147b, .device = 0x1413, .action = VIA_DXS_ENABLE },
2121 { .vendor = 0x147b, .device = 0x1415, .action = VIA_DXS_NO_VRA },
2122 { .vendor = 0x14ff, .device = 0x0403, .action = VIA_DXS_ENABLE },
2123 { .vendor = 0x1584, .device = 0x8120, .action = VIA_DXS_ENABLE },
2124 { .vendor = 0x1584, .device = 0x8123, .action = VIA_DXS_NO_VRA },
2125 { .vendor = 0x161f, .device = 0x202b, .action = VIA_DXS_NO_VRA },
2126 { .vendor = 0x161f, .device = 0x2032, .action = VIA_DXS_48K },
2127 { .vendor = 0x1631, .device = 0xe004, .action = VIA_DXS_ENABLE },
2128 { .vendor = 0x1695, .device = 0x3005, .action = VIA_DXS_ENABLE },
2129 { .vendor = 0x1849, .device = 0x3059, .action = VIA_DXS_NO_VRA },
2130 { }
2131 };
2132 struct dxs_whitelist *w;
2133 unsigned short subsystem_vendor;
2134 unsigned short subsystem_device;
2135
2136 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
2137 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
2138
2139 for (w = whitelist; w->vendor; w++) {
2140 if (w->vendor != subsystem_vendor)
2141 continue;
2142 if (w->mask) {
2143 if ((w->mask & subsystem_device) == w->device)
2144 return w->action;
2145 } else {
2146 if (subsystem_device == w->device)
2147 return w->action;
2148 }
2149 }
2150
2151
2152
2153
2154 printk(KERN_INFO "via82xx: Assuming DXS channels with 48k fixed sample rate.\n");
2155 printk(KERN_INFO " Please try dxs_support=1 or dxs_support=4 option\n");
2156 printk(KERN_INFO " and report if it works on your machine.\n");
2157 return VIA_DXS_48K;
2158};
2159
2160static int __devinit snd_via82xx_probe(struct pci_dev *pci,
2161 const struct pci_device_id *pci_id)
2162{
2163 static int dev;
2164 snd_card_t *card;
2165 via82xx_t *chip;
2166 unsigned char revision;
2167 int chip_type = 0, card_type;
2168 unsigned int i;
2169 int err;
2170
2171 if (dev >= SNDRV_CARDS)
2172 return -ENODEV;
2173 if (!enable[dev]) {
2174 dev++;
2175 return -ENOENT;
2176 }
2177
2178 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2179 if (card == NULL)
2180 return -ENOMEM;
2181
2182 card_type = pci_id->driver_data;
2183 pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
2184 switch (card_type) {
2185 case TYPE_CARD_VIA686:
2186 strcpy(card->driver, "VIA686A");
2187 sprintf(card->shortname, "VIA 82C686A/B rev%x", revision);
2188 chip_type = TYPE_VIA686;
2189 break;
2190 case TYPE_CARD_VIA8233:
2191 chip_type = TYPE_VIA8233;
2192 sprintf(card->shortname, "VIA 823x rev%x", revision);
2193 for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
2194 if (revision == via823x_cards[i].revision) {
2195 chip_type = via823x_cards[i].type;
2196 strcpy(card->shortname, via823x_cards[i].name);
2197 break;
2198 }
2199 }
2200 if (chip_type != TYPE_VIA8233A) {
2201 if (dxs_support[dev] == VIA_DXS_AUTO)
2202 dxs_support[dev] = check_dxs_list(pci);
2203
2204
2205
2206 if (dxs_support[dev] == VIA_DXS_DISABLE)
2207 chip_type = TYPE_VIA8233A;
2208 else
2209 chip_type = TYPE_VIA8233;
2210 }
2211 if (chip_type == TYPE_VIA8233A)
2212 strcpy(card->driver, "VIA8233A");
2213 else if (revision >= VIA_REV_8237)
2214 strcpy(card->driver, "VIA8237");
2215 else
2216 strcpy(card->driver, "VIA8233");
2217 break;
2218 default:
2219 snd_printk(KERN_ERR "invalid card type %d\n", card_type);
2220 err = -EINVAL;
2221 goto __error;
2222 }
2223
2224 if ((err = snd_via82xx_create(card, pci, chip_type, revision, ac97_clock[dev], &chip)) < 0)
2225 goto __error;
2226 if ((err = snd_via82xx_mixer_new(chip, ac97_quirk[dev])) < 0)
2227 goto __error;
2228
2229 if (chip_type == TYPE_VIA686) {
2230 if ((err = snd_via686_pcm_new(chip)) < 0 ||
2231 (err = snd_via686_init_misc(chip, dev)) < 0)
2232 goto __error;
2233 } else {
2234 if (chip_type == TYPE_VIA8233A) {
2235 if ((err = snd_via8233a_pcm_new(chip)) < 0)
2236 goto __error;
2237
2238 } else {
2239 if ((err = snd_via8233_pcm_new(chip)) < 0)
2240 goto __error;
2241 if (dxs_support[dev] == VIA_DXS_48K)
2242 chip->dxs_fixed = 1;
2243 else if (dxs_support[dev] == VIA_DXS_NO_VRA)
2244 chip->no_vra = 1;
2245 }
2246 if ((err = snd_via8233_init_misc(chip, dev)) < 0)
2247 goto __error;
2248 }
2249
2250 snd_card_set_pm_callback(card, snd_via82xx_suspend, snd_via82xx_resume, chip);
2251
2252
2253 for (i = 0; i < chip->num_devs; i++)
2254 snd_via82xx_channel_reset(chip, &chip->devs[i]);
2255
2256 snprintf(card->longname, sizeof(card->longname),
2257 "%s with %s at %#lx, irq %d", card->shortname,
2258 snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
2259
2260 snd_via82xx_proc_init(chip);
2261
2262 if ((err = snd_card_register(card)) < 0) {
2263 snd_card_free(card);
2264 return err;
2265 }
2266 pci_set_drvdata(pci, card);
2267 dev++;
2268 return 0;
2269
2270 __error:
2271 snd_card_free(card);
2272 return err;
2273}
2274
2275static void __devexit snd_via82xx_remove(struct pci_dev *pci)
2276{
2277 snd_card_free(pci_get_drvdata(pci));
2278 pci_set_drvdata(pci, NULL);
2279}
2280
2281static struct pci_driver driver = {
2282 .name = "VIA 82xx Audio",
2283 .id_table = snd_via82xx_ids,
2284 .probe = snd_via82xx_probe,
2285 .remove = __devexit_p(snd_via82xx_remove),
2286 SND_PCI_PM_CALLBACKS
2287};
2288
2289static int __init alsa_card_via82xx_init(void)
2290{
2291 return pci_module_init(&driver);
2292}
2293
2294static void __exit alsa_card_via82xx_exit(void)
2295{
2296 pci_unregister_driver(&driver);
2297}
2298
2299module_init(alsa_card_via82xx_init)
2300module_exit(alsa_card_via82xx_exit)
2301