2 * Support for VIA 82Cxxx Audio Codecs
3 * Copyright 1999,2000 Jeff Garzik
5 * Updated to support the VIA 8233/8235 audio subsystem
6 * Alan Cox <alan@redhat.com> (C) Copyright 2002, 2003 Red Hat Inc
8 * Distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2.
9 * See the "COPYING" file distributed with this software for more info.
12 * For a list of known bugs (errata) and documentation,
13 * see via-audio.pdf in Documentation/DocBook.
14 * If this documentation does not exist, run "make pdfdocs".
18 #define VIA_VERSION "1.9.1-ac4-2.5"
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
26 #include <linux/pci.h>
27 #include <linux/init.h>
28 #include <linux/interrupt.h>
29 #include <linux/proc_fs.h>
30 #include <linux/spinlock.h>
31 #include <linux/sound.h>
32 #include <linux/poll.h>
33 #include <linux/soundcard.h>
34 #include <linux/ac97_codec.h>
35 #include <linux/smp_lock.h>
36 #include <linux/ioport.h>
37 #include <linux/delay.h>
38 #include <linux/dma-mapping.h>
40 #include <asm/uaccess.h>
41 #include <linux/mutex.h>
43 #include "sound_config.h"
44 #include "dev_table.h"
48 #undef VIA_DEBUG /* define to enable debugging output and checks */
50 /* note: prints function name for you */
51 #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
53 #define DPRINTK(fmt, args...)
56 #undef VIA_NDEBUG /* define to disable lightweight runtime checks */
60 #define assert(expr) \
62 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
63 #expr,__FILE__,__FUNCTION__,__LINE__); \
67 #define VIA_SUPPORT_MMAP 1 /* buggy, for now... */
71 #define VIA_CARD_NAME "VIA 82Cxxx Audio driver " VIA_VERSION
72 #define VIA_MODULE_NAME "via82cxxx"
73 #define PFX VIA_MODULE_NAME ": "
75 #define VIA_COUNTER_LIMIT 100000
77 /* size of DMA buffers */
78 #define VIA_MAX_BUFFER_DMA_PAGES 32
80 /* buffering default values in ms */
81 #define VIA_DEFAULT_FRAG_TIME 20
82 #define VIA_DEFAULT_BUFFER_TIME 500
84 /* the hardware has a 256 fragment limit */
85 #define VIA_MIN_FRAG_NUMBER 2
86 #define VIA_MAX_FRAG_NUMBER 128
88 #define VIA_MAX_FRAG_SIZE PAGE_SIZE
89 #define VIA_MIN_FRAG_SIZE (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE / VIA_MAX_FRAG_NUMBER)
92 /* 82C686 function 5 (audio codec) PCI configuration registers */
93 #define VIA_ACLINK_STATUS 0x40
94 #define VIA_ACLINK_CTRL 0x41
95 #define VIA_FUNC_ENABLE 0x42
96 #define VIA_PNP_CONTROL 0x43
97 #define VIA_FM_NMI_CTRL 0x48
100 * controller base 0 (scatter-gather) registers
102 * NOTE: Via datasheet lists first channel as "read"
103 * channel and second channel as "write" channel.
104 * I changed the naming of the constants to be more
105 * clear than I felt the datasheet to be.
108 #define VIA_BASE0_PCM_OUT_CHAN 0x00 /* output PCM to user */
109 #define VIA_BASE0_PCM_OUT_CHAN_STATUS 0x00
110 #define VIA_BASE0_PCM_OUT_CHAN_CTRL 0x01
111 #define VIA_BASE0_PCM_OUT_CHAN_TYPE 0x02
113 #define VIA_BASE0_PCM_IN_CHAN 0x10 /* input PCM from user */
114 #define VIA_BASE0_PCM_IN_CHAN_STATUS 0x10
115 #define VIA_BASE0_PCM_IN_CHAN_CTRL 0x11
116 #define VIA_BASE0_PCM_IN_CHAN_TYPE 0x12
118 /* offsets from base */
119 #define VIA_PCM_STATUS 0x00
120 #define VIA_PCM_CONTROL 0x01
121 #define VIA_PCM_TYPE 0x02
122 #define VIA_PCM_LEFTVOL 0x02
123 #define VIA_PCM_RIGHTVOL 0x03
124 #define VIA_PCM_TABLE_ADDR 0x04
125 #define VIA_PCM_STOPRATE 0x08 /* 8233+ */
126 #define VIA_PCM_BLOCK_COUNT 0x0C
128 /* XXX unused DMA channel for FM PCM data */
129 #define VIA_BASE0_FM_OUT_CHAN 0x20
130 #define VIA_BASE0_FM_OUT_CHAN_STATUS 0x20
131 #define VIA_BASE0_FM_OUT_CHAN_CTRL 0x21
132 #define VIA_BASE0_FM_OUT_CHAN_TYPE 0x22
134 /* Six channel audio output on 8233 */
135 #define VIA_BASE0_MULTI_OUT_CHAN 0x40
136 #define VIA_BASE0_MULTI_OUT_CHAN_STATUS 0x40
137 #define VIA_BASE0_MULTI_OUT_CHAN_CTRL 0x41
138 #define VIA_BASE0_MULTI_OUT_CHAN_TYPE 0x42
140 #define VIA_BASE0_AC97_CTRL 0x80
141 #define VIA_BASE0_SGD_STATUS_SHADOW 0x84
142 #define VIA_BASE0_GPI_INT_ENABLE 0x8C
143 #define VIA_INTR_OUT ((1<<0) | (1<<4) | (1<<8))
144 #define VIA_INTR_IN ((1<<1) | (1<<5) | (1<<9))
145 #define VIA_INTR_FM ((1<<2) | (1<<6) | (1<<10))
146 #define VIA_INTR_MASK (VIA_INTR_OUT | VIA_INTR_IN | VIA_INTR_FM)
148 /* Newer VIA we need to monitor the low 3 bits of each channel. This
149 mask covers the channels we don't yet use as well
152 #define VIA_NEW_INTR_MASK 0x77077777UL
154 /* VIA_BASE0_AUDIO_xxx_CHAN_TYPE bits */
155 #define VIA_IRQ_ON_FLAG (1<<0) /* int on each flagged scatter block */
156 #define VIA_IRQ_ON_EOL (1<<1) /* int at end of scatter list */
157 #define VIA_INT_SEL_PCI_LAST_LINE_READ (0) /* int at PCI read of last line */
158 #define VIA_INT_SEL_LAST_SAMPLE_SENT (1<<2) /* int at last sample sent */
159 #define VIA_INT_SEL_ONE_LINE_LEFT (1<<3) /* int at less than one line to send */
160 #define VIA_PCM_FMT_STEREO (1<<4) /* PCM stereo format (bit clear == mono) */
161 #define VIA_PCM_FMT_16BIT (1<<5) /* PCM 16-bit format (bit clear == 8-bit) */
162 #define VIA_PCM_REC_FIFO (1<<6) /* PCM Recording FIFO */
163 #define VIA_RESTART_SGD_ON_EOL (1<<7) /* restart scatter-gather at EOL */
164 #define VIA_PCM_FMT_MASK (VIA_PCM_FMT_STEREO|VIA_PCM_FMT_16BIT)
165 #define VIA_CHAN_TYPE_MASK (VIA_RESTART_SGD_ON_EOL | \
168 #define VIA_CHAN_TYPE_INT_SELECT (VIA_INT_SEL_LAST_SAMPLE_SENT)
170 /* PCI configuration register bits and masks */
171 #define VIA_CR40_AC97_READY 0x01
172 #define VIA_CR40_AC97_LOW_POWER 0x02
173 #define VIA_CR40_SECONDARY_READY 0x04
175 #define VIA_CR41_AC97_ENABLE 0x80 /* enable AC97 codec */
176 #define VIA_CR41_AC97_RESET 0x40 /* clear bit to reset AC97 */
177 #define VIA_CR41_AC97_WAKEUP 0x20 /* wake up from power-down mode */
178 #define VIA_CR41_AC97_SDO 0x10 /* force Serial Data Out (SDO) high */
179 #define VIA_CR41_VRA 0x08 /* enable variable sample rate */
180 #define VIA_CR41_PCM_ENABLE 0x04 /* AC Link SGD Read Channel PCM Data Output */
181 #define VIA_CR41_FM_PCM_ENABLE 0x02 /* AC Link FM Channel PCM Data Out */
182 #define VIA_CR41_SB_PCM_ENABLE 0x01 /* AC Link SB PCM Data Output */
183 #define VIA_CR41_BOOT_MASK (VIA_CR41_AC97_ENABLE | \
184 VIA_CR41_AC97_WAKEUP | \
186 #define VIA_CR41_RUN_MASK (VIA_CR41_AC97_ENABLE | \
187 VIA_CR41_AC97_RESET | \
191 #define VIA_CR42_SB_ENABLE 0x01
192 #define VIA_CR42_MIDI_ENABLE 0x02
193 #define VIA_CR42_FM_ENABLE 0x04
194 #define VIA_CR42_GAME_ENABLE 0x08
195 #define VIA_CR42_MIDI_IRQMASK 0x40
196 #define VIA_CR42_MIDI_PNP 0x80
198 #define VIA_CR44_SECOND_CODEC_SUPPORT (1 << 6)
199 #define VIA_CR44_AC_LINK_ACCESS (1 << 7)
201 #define VIA_CR48_FM_TRAP_TO_NMI (1 << 2)
203 /* controller base 0 register bitmasks */
204 #define VIA_INT_DISABLE_MASK (~(0x01|0x02))
205 #define VIA_SGD_STOPPED (1 << 2)
206 #define VIA_SGD_PAUSED (1 << 6)
207 #define VIA_SGD_ACTIVE (1 << 7)
208 #define VIA_SGD_TERMINATE (1 << 6)
209 #define VIA_SGD_FLAG (1 << 0)
210 #define VIA_SGD_EOL (1 << 1)
211 #define VIA_SGD_START (1 << 7)
213 #define VIA_CR80_FIRST_CODEC 0
214 #define VIA_CR80_SECOND_CODEC (1 << 30)
215 #define VIA_CR80_FIRST_CODEC_VALID (1 << 25)
216 #define VIA_CR80_VALID (1 << 25)
217 #define VIA_CR80_SECOND_CODEC_VALID (1 << 27)
218 #define VIA_CR80_BUSY (1 << 24)
219 #define VIA_CR83_BUSY (1)
220 #define VIA_CR83_FIRST_CODEC_VALID (1 << 1)
221 #define VIA_CR80_READ (1 << 23)
222 #define VIA_CR80_WRITE_MODE 0
223 #define VIA_CR80_REG_IDX(idx) ((((idx) & 0xFF) >> 1) << 16)
225 /* capabilities we announce */
226 #ifdef VIA_SUPPORT_MMAP
227 #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | DSP_CAP_MMAP | \
228 DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
230 #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | \
231 DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
234 /* scatter-gather DMA table entry, exactly as passed to hardware */
235 struct via_sgd_table {
237 u32 count; /* includes additional VIA_xxx bits also */
240 #define VIA_EOL (1 << 31)
241 #define VIA_FLAG (1 << 30)
242 #define VIA_STOP (1 << 29)
245 enum via_channel_states {
251 struct via_buffer_pgtbl {
260 wait_queue_head_t wait;
263 unsigned int slop_len;
267 unsigned is_active : 1;
268 unsigned is_record : 1;
269 unsigned is_mapped : 1;
270 unsigned is_enabled : 1;
271 unsigned is_multi: 1; /* 8233 6 channel */
272 u8 pcm_fmt; /* VIA_PCM_FMT_xxx */
273 u8 channels; /* Channel count */
275 unsigned rate; /* sample rate */
276 unsigned int frag_size;
277 unsigned int frag_number;
279 unsigned char intmask;
281 volatile struct via_sgd_table *sgtable;
282 dma_addr_t sgt_handle;
284 unsigned int page_number;
285 struct via_buffer_pgtbl pgtbl[VIA_MAX_BUFFER_DMA_PAGES];
293 /* data stored for each chip */
295 struct pci_dev *pdev;
298 struct ac97_codec *ac97;
299 spinlock_t ac97_lock;
301 int card_num; /* unique card number, from 0 */
303 int dev_dsp; /* /dev/dsp index from register_sound_dsp() */
306 unsigned legacy: 1; /* Has legacy ports */
307 unsigned intmask: 1; /* Needs int bits */
308 unsigned sixchannel: 1; /* 8233/35 with 6 channel support */
313 int mixer_vol; /* 8233/35 volume - not yet implemented */
315 struct mutex syscall_mutex;
316 struct mutex open_mutex;
318 /* The 8233/8235 have 4 DX audio channels, two record and
319 one six channel out. We bind ch_in to DX 1, ch_out to multichannel
320 and ch_fm to DX 2. DX 3 and REC0/REC1 are unused at the
323 struct via_channel ch_in;
324 struct via_channel ch_out;
325 struct via_channel ch_fm;
327 #ifdef CONFIG_MIDI_VIA82CXXX
329 struct address_info midi_info;
334 /* number of cards, used for assigning unique numbers to cards */
335 static unsigned via_num_cards;
339 /****************************************************************
346 static int via_init_one (struct pci_dev *dev, const struct pci_device_id *id);
347 static void __devexit via_remove_one (struct pci_dev *pdev);
349 static ssize_t via_dsp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos);
350 static ssize_t via_dsp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos);
351 static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait);
352 static int via_dsp_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
353 static int via_dsp_open (struct inode *inode, struct file *file);
354 static int via_dsp_release(struct inode *inode, struct file *file);
355 static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma);
357 static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg);
358 static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value);
359 static u8 via_ac97_wait_idle (struct via_info *card);
361 static void via_chan_free (struct via_info *card, struct via_channel *chan);
362 static void via_chan_clear (struct via_info *card, struct via_channel *chan);
363 static void via_chan_pcm_fmt (struct via_channel *chan, int reset);
364 static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan);
367 /****************************************************************
369 * Various data the driver needs
375 static struct pci_device_id via_pci_tbl[] = {
376 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5,
377 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
378 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_5,
379 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
382 MODULE_DEVICE_TABLE(pci,via_pci_tbl);
385 static struct pci_driver via_driver = {
386 .name = VIA_MODULE_NAME,
387 .id_table = via_pci_tbl,
388 .probe = via_init_one,
389 .remove = __devexit_p(via_remove_one),
393 /****************************************************************
395 * Low-level base 0 register read/write helpers
401 * via_chan_stop - Terminate DMA on specified PCM channel
402 * @iobase: PCI base address for SGD channel registers
404 * Terminate scatter-gather DMA operation for given
405 * channel (derived from @iobase), if DMA is active.
407 * Note that @iobase is not the PCI base address,
408 * but the PCI base address plus an offset to
409 * one of three PCM channels supported by the chip.
413 static inline void via_chan_stop (long iobase)
415 if (inb (iobase + VIA_PCM_STATUS) & VIA_SGD_ACTIVE)
416 outb (VIA_SGD_TERMINATE, iobase + VIA_PCM_CONTROL);
421 * via_chan_status_clear - Clear status flags on specified DMA channel
422 * @iobase: PCI base address for SGD channel registers
424 * Clear any pending status flags for the given
425 * DMA channel (derived from @iobase), if any
426 * flags are asserted.
428 * Note that @iobase is not the PCI base address,
429 * but the PCI base address plus an offset to
430 * one of three PCM channels supported by the chip.
434 static inline void via_chan_status_clear (long iobase)
436 u8 tmp = inb (iobase + VIA_PCM_STATUS);
439 outb (tmp, iobase + VIA_PCM_STATUS);
444 * sg_begin - Begin recording or playback on a PCM channel
445 * @chan: Channel for which DMA operation shall begin
447 * Start scatter-gather DMA for the given channel.
451 static inline void sg_begin (struct via_channel *chan)
453 DPRINTK("Start with intmask %d\n", chan->intmask);
454 DPRINTK("About to start from %d to %d\n",
455 inl(chan->iobase + VIA_PCM_BLOCK_COUNT),
456 inb(chan->iobase + VIA_PCM_STOPRATE + 3));
457 outb (VIA_SGD_START|chan->intmask, chan->iobase + VIA_PCM_CONTROL);
458 DPRINTK("Status is now %02X\n", inb(chan->iobase + VIA_PCM_STATUS));
459 DPRINTK("Control is now %02X\n", inb(chan->iobase + VIA_PCM_CONTROL));
463 static int sg_active (long iobase)
465 u8 tmp = inb (iobase + VIA_PCM_STATUS);
466 if ((tmp & VIA_SGD_STOPPED) || (tmp & VIA_SGD_PAUSED)) {
467 printk(KERN_WARNING "via82cxxx warning: SG stopped or paused\n");
470 if (tmp & VIA_SGD_ACTIVE)
475 static int via_sg_offset(struct via_channel *chan)
477 return inl (chan->iobase + VIA_PCM_BLOCK_COUNT) & 0x00FFFFFF;
480 /****************************************************************
482 * Miscellaneous debris
489 * via_syscall_down - down the card-specific syscell semaphore
490 * @card: Private info for specified board
491 * @nonblock: boolean, non-zero if O_NONBLOCK is set
493 * Encapsulates standard method of acquiring the syscall sem.
495 * Returns negative errno on error, or zero for success.
498 static inline int via_syscall_down (struct via_info *card, int nonblock)
501 * EAGAIN is supposed to be used if IO is pending,
502 * not if there is contention on some internal
503 * synchronization primitive which should be
504 * held only for a short time anyway
509 if (!mutex_trylock(&card->syscall_mutex))
512 if (mutex_lock_interruptible(&card->syscall_mutex))
521 * via_stop_everything - Stop all audio operations
522 * @card: Private info for specified board
524 * Stops all DMA operations and interrupts, and clear
525 * any pending status bits resulting from those operations.
528 static void via_stop_everything (struct via_info *card)
534 assert (card != NULL);
537 * terminate any existing operations on audio read/write channels
539 via_chan_stop (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
540 via_chan_stop (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
541 via_chan_stop (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
543 via_chan_stop (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN);
546 * clear any existing stops / flags (sanity check mainly)
548 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
549 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
550 via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
552 via_chan_status_clear (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN);
555 * clear any enabled interrupt bits
557 tmp = inb (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
558 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
560 outb (0, card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
562 tmp = inb (card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
563 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
565 outb (0, card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
567 tmp = inb (card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
568 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
570 outb (0, card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
574 tmp = inb (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
575 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
577 outb (0, card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
583 * clear any existing flags
585 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
586 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
587 via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
594 * via_set_rate - Set PCM rate for given channel
595 * @ac97: Pointer to generic codec info struct
596 * @chan: Private info for specified channel
597 * @rate: Desired PCM sample rate, in Khz
599 * Sets the PCM sample rate for a channel.
601 * Values for @rate are clamped to a range of 4000 Khz through 48000 Khz,
602 * due to hardware constraints.
605 static int via_set_rate (struct ac97_codec *ac97,
606 struct via_channel *chan, unsigned rate)
608 struct via_info *card = ac97->private_data;
611 u32 mast_vol, phone_vol, mono_vol, pcm_vol;
612 u32 mute_vol = 0x8000; /* The mute volume? -- Seems to work! */
614 DPRINTK ("ENTER, rate = %d\n", rate);
616 if (chan->rate == rate)
618 if (card->locked_rate) {
623 if (rate > 48000) rate = 48000;
624 if (rate < 4000) rate = 4000;
626 rate_reg = chan->is_record ? AC97_PCM_LR_ADC_RATE :
627 AC97_PCM_FRONT_DAC_RATE;
629 /* Save current state */
630 dacp=via_ac97_read_reg(ac97, AC97_POWER_CONTROL);
631 mast_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_STEREO);
632 mono_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_MONO);
633 phone_vol = via_ac97_read_reg(ac97, AC97_HEADPHONE_VOL);
634 pcm_vol = via_ac97_read_reg(ac97, AC97_PCMOUT_VOL);
635 /* Mute - largely reduces popping */
636 via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mute_vol);
637 via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mute_vol);
638 via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, mute_vol);
639 via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, mute_vol);
640 /* Power down the DAC */
641 via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp|0x0200);
644 via_ac97_write_reg (ac97, rate_reg, rate);
646 /* Power DAC back up */
647 via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp);
648 udelay (200); /* reduces popping */
650 /* Restore volumes */
651 via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mast_vol);
652 via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mono_vol);
653 via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, phone_vol);
654 via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, pcm_vol);
656 /* the hardware might return a value different than what we
657 * passed to it, so read the rate value back from hardware
658 * to see what we came up with
660 chan->rate = via_ac97_read_reg (ac97, rate_reg);
662 if (chan->rate == 0) {
663 card->locked_rate = 1;
665 printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n");
669 DPRINTK ("EXIT, returning rate %d Hz\n", chan->rate);
674 /****************************************************************
676 * Channel-specific operations
683 * via_chan_init_defaults - Initialize a struct via_channel
684 * @card: Private audio chip info
685 * @chan: Channel to be initialized
687 * Zero @chan, and then set all static defaults for the structure.
690 static void via_chan_init_defaults (struct via_info *card, struct via_channel *chan)
692 memset (chan, 0, sizeof (*chan));
695 chan->intmask = 0x23; /* Turn on the IRQ bits */
697 if (chan == &card->ch_out) {
698 chan->name = "PCM-OUT";
701 chan->iobase = card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN;
703 DPRINTK("Using multichannel for pcm out\n");
706 chan->iobase = card->baseaddr + VIA_BASE0_PCM_OUT_CHAN;
707 } else if (chan == &card->ch_in) {
708 chan->name = "PCM-IN";
709 chan->iobase = card->baseaddr + VIA_BASE0_PCM_IN_CHAN;
711 } else if (chan == &card->ch_fm) {
712 chan->name = "PCM-OUT-FM";
713 chan->iobase = card->baseaddr + VIA_BASE0_FM_OUT_CHAN;
718 init_waitqueue_head (&chan->wait);
720 chan->pcm_fmt = VIA_PCM_FMT_MASK;
721 chan->is_enabled = 1;
723 chan->frag_number = 0;
725 atomic_set(&chan->n_frags, 0);
726 atomic_set (&chan->hw_ptr, 0);
730 * via_chan_init - Initialize PCM channel
731 * @card: Private audio chip info
732 * @chan: Channel to be initialized
734 * Performs some of the preparations necessary to begin
735 * using a PCM channel.
737 * Currently the preparations consist of
738 * setting the PCM channel to a known state.
742 static void via_chan_init (struct via_info *card, struct via_channel *chan)
747 /* bzero channel structure, and init members to defaults */
748 via_chan_init_defaults (card, chan);
750 /* stop any existing channel output */
751 via_chan_clear (card, chan);
752 via_chan_status_clear (chan->iobase);
753 via_chan_pcm_fmt (chan, 1);
759 * via_chan_buffer_init - Initialize PCM channel buffer
760 * @card: Private audio chip info
761 * @chan: Channel to be initialized
763 * Performs some of the preparations necessary to begin
764 * using a PCM channel.
766 * Currently the preparations include allocating the
767 * scatter-gather DMA table and buffers,
769 * address of the DMA table to the hardware.
771 * Note that special care is taken when passing the
772 * DMA table address to hardware, because it was found
773 * during driver development that the hardware did not
774 * always "take" the address.
777 static int via_chan_buffer_init (struct via_info *card, struct via_channel *chan)
787 chan->intmask = 0x23; /* Turn on the IRQ bits */
789 if (chan->sgtable != NULL) {
794 /* alloc DMA-able memory for scatter-gather table */
795 chan->sgtable = pci_alloc_consistent (card->pdev,
796 (sizeof (struct via_sgd_table) * chan->frag_number),
798 if (!chan->sgtable) {
799 printk (KERN_ERR PFX "DMA table alloc fail, aborting\n");
804 memset ((void*)chan->sgtable, 0,
805 (sizeof (struct via_sgd_table) * chan->frag_number));
807 /* alloc DMA-able memory for scatter-gather buffers */
809 chan->page_number = (chan->frag_number * chan->frag_size) / PAGE_SIZE +
810 (((chan->frag_number * chan->frag_size) % PAGE_SIZE) ? 1 : 0);
812 for (i = 0; i < chan->page_number; i++) {
813 chan->pgtbl[i].cpuaddr = pci_alloc_consistent (card->pdev, PAGE_SIZE,
814 &chan->pgtbl[i].handle);
816 if (!chan->pgtbl[i].cpuaddr) {
817 chan->page_number = i;
822 memset (chan->pgtbl[i].cpuaddr, 0xBC, chan->frag_size);
826 DPRINTK ("dmabuf_pg #%d (h=%lx, v2p=%lx, a=%p)\n",
827 i, (long)chan->pgtbl[i].handle,
828 virt_to_phys(chan->pgtbl[i].cpuaddr),
829 chan->pgtbl[i].cpuaddr);
833 for (i = 0; i < chan->frag_number; i++) {
835 page = i / (PAGE_SIZE / chan->frag_size);
836 offset = (i % (PAGE_SIZE / chan->frag_size)) * chan->frag_size;
838 chan->sgtable[i].count = cpu_to_le32 (chan->frag_size | VIA_FLAG);
839 chan->sgtable[i].addr = cpu_to_le32 (chan->pgtbl[page].handle + offset);
842 DPRINTK ("dmabuf #%d (32(h)=%lx)\n",
844 (long)chan->sgtable[i].addr);
848 /* overwrite the last buffer information */
849 chan->sgtable[chan->frag_number - 1].count = cpu_to_le32 (chan->frag_size | VIA_EOL);
851 /* set location of DMA-able scatter-gather info table */
852 DPRINTK ("outl (0x%X, 0x%04lX)\n",
853 chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR);
855 via_ac97_wait_idle (card);
856 outl (chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR);
858 via_ac97_wait_idle (card);
859 /* load no rate adaption, stereo 16bit, set up ring slots */
864 outl (0xFFFFF | (0x3 << 20) | (chan->frag_number << 24), chan->iobase + VIA_PCM_STOPRATE);
866 via_ac97_wait_idle (card);
870 DPRINTK ("inl (0x%lX) = %x\n",
871 chan->iobase + VIA_PCM_TABLE_ADDR,
872 inl(chan->iobase + VIA_PCM_TABLE_ADDR));
878 printk (KERN_ERR PFX "DMA buffer alloc fail, aborting\n");
879 via_chan_buffer_free (card, chan);
886 * via_chan_free - Release a PCM channel
887 * @card: Private audio chip info
888 * @chan: Channel to be released
890 * Performs all the functions necessary to clean up
891 * an initialized channel.
893 * Currently these functions include disabled any
894 * active DMA operations, setting the PCM channel
895 * back to a known state, and releasing any allocated
899 static void via_chan_free (struct via_info *card, struct via_channel *chan)
903 spin_lock_irq (&card->lock);
905 /* stop any existing channel output */
906 via_chan_status_clear (chan->iobase);
907 via_chan_stop (chan->iobase);
908 via_chan_status_clear (chan->iobase);
910 spin_unlock_irq (&card->lock);
912 synchronize_irq(card->pdev->irq);
917 static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan)
923 /* zero location of DMA-able scatter-gather info table */
924 via_ac97_wait_idle(card);
925 outl (0, chan->iobase + VIA_PCM_TABLE_ADDR);
927 for (i = 0; i < chan->page_number; i++)
928 if (chan->pgtbl[i].cpuaddr) {
929 pci_free_consistent (card->pdev, PAGE_SIZE,
930 chan->pgtbl[i].cpuaddr,
931 chan->pgtbl[i].handle);
932 chan->pgtbl[i].cpuaddr = NULL;
933 chan->pgtbl[i].handle = 0;
936 chan->page_number = 0;
939 pci_free_consistent (card->pdev,
940 (sizeof (struct via_sgd_table) * chan->frag_number),
941 (void*)chan->sgtable, chan->sgt_handle);
942 chan->sgtable = NULL;
950 * via_chan_pcm_fmt - Update PCM channel settings
951 * @chan: Channel to be updated
952 * @reset: Boolean. If non-zero, channel will be reset
953 * to 8-bit mono mode.
955 * Stores the settings of the current PCM format,
956 * 8-bit or 16-bit, and mono/stereo, into the
957 * hardware settings for the specified channel.
958 * If @reset is non-zero, the channel is reset
959 * to 8-bit mono mode. Otherwise, the channel
960 * is set to the values stored in the channel
961 * information struct @chan.
964 static void via_chan_pcm_fmt (struct via_channel *chan, int reset)
966 DPRINTK ("ENTER, pcm_fmt=0x%02X, reset=%s\n",
967 chan->pcm_fmt, reset ? "yes" : "no");
969 assert (chan != NULL);
973 /* reset to 8-bit mono mode */
978 /* enable interrupts on FLAG and EOL */
979 chan->pcm_fmt |= VIA_CHAN_TYPE_MASK;
981 /* if we are recording, enable recording fifo bit */
983 chan->pcm_fmt |= VIA_PCM_REC_FIFO;
984 /* set interrupt select bits where applicable (PCM in & out channels) */
985 if (!chan->is_record)
986 chan->pcm_fmt |= VIA_CHAN_TYPE_INT_SELECT;
988 DPRINTK("SET FMT - %02x %02x\n", chan->intmask , chan->is_multi);
995 * Channel 0x4 is up to 6 x 16bit and has to be
996 * programmed differently
1004 * Load the type bit for num channels
1008 if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
1010 if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
1015 outb(c, chan->iobase + VIA_PCM_TYPE);
1018 * Set the channel steering
1020 * Channel 0 to slot 3
1021 * Channel 0 to slot 4
1023 * Channel 0 to slot 3
1024 * Channel 1 to slot 4
1027 switch(chan->channels)
1030 outl(0xFF000000 | (1<<0) | (1<<4) , chan->iobase + VIA_PCM_STOPRATE);
1033 outl(0xFF000000 | (1<<0) | (2<<4) , chan->iobase + VIA_PCM_STOPRATE);
1036 outl(0xFF000000 | (1<<0) | (2<<4) | (3<<8) | (4<<12), chan->iobase + VIA_PCM_STOPRATE);
1039 outl(0xFF000000 | (1<<0) | (2<<4) | (5<<8) | (6<<12) | (3<<16) | (4<<20), chan->iobase + VIA_PCM_STOPRATE);
1046 * New style, turn off channel volume
1047 * control, set bits in the right register
1049 outb(0x0, chan->iobase + VIA_PCM_LEFTVOL);
1050 outb(0x0, chan->iobase + VIA_PCM_RIGHTVOL);
1052 m = inl(chan->iobase + VIA_PCM_STOPRATE);
1054 if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
1056 if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
1058 outl(m, chan->iobase + VIA_PCM_STOPRATE);
1062 outb (chan->pcm_fmt, chan->iobase + VIA_PCM_TYPE);
1065 DPRINTK ("EXIT, pcm_fmt = 0x%02X, reg = 0x%02X\n",
1067 inb (chan->iobase + VIA_PCM_TYPE));
1072 * via_chan_clear - Stop DMA channel operation, and reset pointers
1073 * @card: the chip to accessed
1074 * @chan: Channel to be cleared
1076 * Call via_chan_stop to halt DMA operations, and then resets
1077 * all software pointers which track DMA operation.
1080 static void via_chan_clear (struct via_info *card, struct via_channel *chan)
1082 DPRINTK ("ENTER\n");
1083 via_chan_stop (chan->iobase);
1084 via_chan_buffer_free(card, chan);
1085 chan->is_active = 0;
1086 chan->is_mapped = 0;
1087 chan->is_enabled = 1;
1091 atomic_set (&chan->hw_ptr, 0);
1097 * via_chan_set_speed - Set PCM sample rate for given channel
1098 * @card: Private info for specified board
1099 * @chan: Channel whose sample rate will be adjusted
1100 * @val: New sample rate, in Khz
1102 * Helper function for the %SNDCTL_DSP_SPEED ioctl. OSS semantics
1103 * demand that all audio operations halt (if they are not already
1104 * halted) when the %SNDCTL_DSP_SPEED is given.
1106 * This function halts all audio operations for the given channel
1107 * @chan, and then calls via_set_rate to set the audio hardware
1111 static int via_chan_set_speed (struct via_info *card,
1112 struct via_channel *chan, int val)
1114 DPRINTK ("ENTER, requested rate = %d\n", val);
1116 via_chan_clear (card, chan);
1118 val = via_set_rate (card->ac97, chan, val);
1120 DPRINTK ("EXIT, returning %d\n", val);
1126 * via_chan_set_fmt - Set PCM sample size for given channel
1127 * @card: Private info for specified board
1128 * @chan: Channel whose sample size will be adjusted
1129 * @val: New sample size, use the %AFMT_xxx constants
1131 * Helper function for the %SNDCTL_DSP_SETFMT ioctl. OSS semantics
1132 * demand that all audio operations halt (if they are not already
1133 * halted) when the %SNDCTL_DSP_SETFMT is given.
1135 * This function halts all audio operations for the given channel
1136 * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
1137 * to the new sample size, either 8-bit or 16-bit.
1140 static int via_chan_set_fmt (struct via_info *card,
1141 struct via_channel *chan, int val)
1143 DPRINTK ("ENTER, val=%s\n",
1144 val == AFMT_U8 ? "AFMT_U8" :
1145 val == AFMT_S16_LE ? "AFMT_S16_LE" :
1148 via_chan_clear (card, chan);
1150 assert (val != AFMT_QUERY); /* this case is handled elsewhere */
1154 if ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) == 0) {
1155 chan->pcm_fmt |= VIA_PCM_FMT_16BIT;
1156 via_chan_pcm_fmt (chan, 0);
1161 if (chan->pcm_fmt & VIA_PCM_FMT_16BIT) {
1162 chan->pcm_fmt &= ~VIA_PCM_FMT_16BIT;
1163 via_chan_pcm_fmt (chan, 0);
1168 DPRINTK ("unknown AFMT: 0x%X\n", val);
1178 * via_chan_set_stereo - Enable or disable stereo for a DMA channel
1179 * @card: Private info for specified board
1180 * @chan: Channel whose stereo setting will be adjusted
1181 * @val: New sample size, use the %AFMT_xxx constants
1183 * Helper function for the %SNDCTL_DSP_CHANNELS and %SNDCTL_DSP_STEREO ioctls. OSS semantics
1184 * demand that all audio operations halt (if they are not already
1185 * halted) when %SNDCTL_DSP_CHANNELS or SNDCTL_DSP_STEREO is given.
1187 * This function halts all audio operations for the given channel
1188 * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
1189 * to enable or disable stereo.
1192 static int via_chan_set_stereo (struct via_info *card,
1193 struct via_channel *chan, int val)
1195 DPRINTK ("ENTER, channels = %d\n", val);
1197 via_chan_clear (card, chan);
1203 chan->pcm_fmt &= ~VIA_PCM_FMT_STEREO;
1205 via_chan_pcm_fmt (chan, 0);
1210 chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
1212 via_chan_pcm_fmt (chan, 0);
1219 chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
1220 chan->channels = val;
1229 DPRINTK ("EXIT, returning %d\n", val);
1233 static int via_chan_set_buffering (struct via_info *card,
1234 struct via_channel *chan, int val)
1238 DPRINTK ("ENTER\n");
1240 /* in both cases the buffer cannot be changed */
1241 if (chan->is_active || chan->is_mapped) {
1246 /* called outside SETFRAGMENT */
1247 /* set defaults or do nothing */
1250 if (chan->frag_size && chan->frag_number)
1255 chan->frag_size = (VIA_DEFAULT_FRAG_TIME * chan->rate * chan->channels
1256 * ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) ? 2 : 1)) / 1000 - 1;
1259 while (chan->frag_size) {
1260 chan->frag_size >>= 1;
1263 chan->frag_size = 1 << shift;
1265 chan->frag_number = (VIA_DEFAULT_BUFFER_TIME / VIA_DEFAULT_FRAG_TIME);
1267 DPRINTK ("setting default values %d %d\n", chan->frag_size, chan->frag_number);
1269 chan->frag_size = 1 << (val & 0xFFFF);
1270 chan->frag_number = (val >> 16) & 0xFFFF;
1272 DPRINTK ("using user values %d %d\n", chan->frag_size, chan->frag_number);
1275 /* quake3 wants frag_number to be a power of two */
1277 while (chan->frag_number) {
1278 chan->frag_number >>= 1;
1281 chan->frag_number = 1 << shift;
1283 if (chan->frag_size > VIA_MAX_FRAG_SIZE)
1284 chan->frag_size = VIA_MAX_FRAG_SIZE;
1285 else if (chan->frag_size < VIA_MIN_FRAG_SIZE)
1286 chan->frag_size = VIA_MIN_FRAG_SIZE;
1288 if (chan->frag_number < VIA_MIN_FRAG_NUMBER)
1289 chan->frag_number = VIA_MIN_FRAG_NUMBER;
1290 if (chan->frag_number > VIA_MAX_FRAG_NUMBER)
1291 chan->frag_number = VIA_MAX_FRAG_NUMBER;
1293 if ((chan->frag_number * chan->frag_size) / PAGE_SIZE > VIA_MAX_BUFFER_DMA_PAGES)
1294 chan->frag_number = (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE) / chan->frag_size;
1297 if (chan->is_record)
1298 atomic_set (&chan->n_frags, 0);
1300 atomic_set (&chan->n_frags, chan->frag_number);
1307 #ifdef VIA_CHAN_DUMP_BUFS
1309 * via_chan_dump_bufs - Display DMA table contents
1310 * @chan: Channel whose DMA table will be displayed
1312 * Debugging function which displays the contents of the
1313 * scatter-gather DMA table for the given channel @chan.
1316 static void via_chan_dump_bufs (struct via_channel *chan)
1320 for (i = 0; i < chan->frag_number; i++) {
1321 DPRINTK ("#%02d: addr=%x, count=%u, flag=%d, eol=%d\n",
1322 i, chan->sgtable[i].addr,
1323 chan->sgtable[i].count & 0x00FFFFFF,
1324 chan->sgtable[i].count & VIA_FLAG ? 1 : 0,
1325 chan->sgtable[i].count & VIA_EOL ? 1 : 0);
1327 DPRINTK ("buf_in_use = %d, nextbuf = %d\n",
1328 atomic_read (&chan->buf_in_use),
1329 atomic_read (&chan->sw_ptr));
1331 #endif /* VIA_CHAN_DUMP_BUFS */
1335 * via_chan_flush_frag - Flush partially-full playback buffer to hardware
1336 * @chan: Channel whose DMA table will be flushed
1338 * Flushes partially-full playback buffer to hardware.
1341 static void via_chan_flush_frag (struct via_channel *chan)
1343 DPRINTK ("ENTER\n");
1345 assert (chan->slop_len > 0);
1347 if (chan->sw_ptr == (chan->frag_number - 1))
1354 assert (atomic_read (&chan->n_frags) > 0);
1355 atomic_dec (&chan->n_frags);
1363 * via_chan_maybe_start - Initiate audio hardware DMA operation
1364 * @chan: Channel whose DMA is to be started
1366 * Initiate DMA operation, if the DMA engine for the given
1367 * channel @chan is not already active.
1370 static inline void via_chan_maybe_start (struct via_channel *chan)
1372 assert (chan->is_active == sg_active(chan->iobase));
1374 DPRINTK ("MAYBE START %s\n", chan->name);
1375 if (!chan->is_active && chan->is_enabled) {
1376 chan->is_active = 1;
1378 DPRINTK ("starting channel %s\n", chan->name);
1383 /****************************************************************
1385 * Interface to ac97-codec module
1391 * via_ac97_wait_idle - Wait until AC97 codec is not busy
1392 * @card: Private info for specified board
1394 * Sleep until the AC97 codec is no longer busy.
1395 * Returns the final value read from the SGD
1396 * register being polled.
1399 static u8 via_ac97_wait_idle (struct via_info *card)
1402 int counter = VIA_COUNTER_LIMIT;
1404 DPRINTK ("ENTER/EXIT\n");
1406 assert (card != NULL);
1407 assert (card->pdev != NULL);
1412 tmp8 = inb (card->baseaddr + 0x83);
1413 } while ((tmp8 & VIA_CR83_BUSY) && (counter-- > 0));
1415 if (tmp8 & VIA_CR83_BUSY)
1416 printk (KERN_WARNING PFX "timeout waiting on AC97 codec\n");
1422 * via_ac97_read_reg - Read AC97 standard register
1423 * @codec: Pointer to generic AC97 codec info
1424 * @reg: Index of AC97 register to be read
1426 * Read the value of a single AC97 codec register,
1427 * as defined by the Intel AC97 specification.
1429 * Defines the standard AC97 read-register operation
1430 * required by the kernel's ac97_codec interface.
1432 * Returns the 16-bit value stored in the specified
1436 static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg)
1439 struct via_info *card;
1442 DPRINTK ("ENTER\n");
1444 assert (codec != NULL);
1445 assert (codec->private_data != NULL);
1447 card = codec->private_data;
1449 spin_lock(&card->ac97_lock);
1451 /* Every time we write to register 80 we cause a transaction.
1452 The only safe way to clear the valid bit is to write it at
1453 the same time as the command */
1454 data = (reg << 16) | VIA_CR80_READ | VIA_CR80_VALID;
1456 outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
1459 for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
1461 if ((((data = inl(card->baseaddr + VIA_BASE0_AC97_CTRL)) &
1462 (VIA_CR80_VALID|VIA_CR80_BUSY)) == VIA_CR80_VALID))
1466 printk (KERN_WARNING PFX "timeout while reading AC97 codec (0x%lX)\n", data);
1470 /* Once the valid bit has become set, we must wait a complete AC97
1471 frame before the data has settled. */
1473 data = (unsigned long) inl (card->baseaddr + VIA_BASE0_AC97_CTRL);
1475 outb (0x02, card->baseaddr + 0x83);
1477 if (((data & 0x007F0000) >> 16) == reg) {
1478 DPRINTK ("EXIT, success, data=0x%lx, retval=0x%lx\n",
1479 data, data & 0x0000FFFF);
1480 spin_unlock(&card->ac97_lock);
1481 return data & 0x0000FFFF;
1484 printk (KERN_WARNING "via82cxxx_audio: not our index: reg=0x%x, newreg=0x%lx\n",
1485 reg, ((data & 0x007F0000) >> 16));
1488 spin_unlock(&card->ac97_lock);
1489 DPRINTK ("EXIT, returning 0\n");
1495 * via_ac97_write_reg - Write AC97 standard register
1496 * @codec: Pointer to generic AC97 codec info
1497 * @reg: Index of AC97 register to be written
1498 * @value: Value to be written to AC97 register
1500 * Write the value of a single AC97 codec register,
1501 * as defined by the Intel AC97 specification.
1503 * Defines the standard AC97 write-register operation
1504 * required by the kernel's ac97_codec interface.
1507 static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value)
1510 struct via_info *card;
1513 DPRINTK ("ENTER\n");
1515 assert (codec != NULL);
1516 assert (codec->private_data != NULL);
1518 card = codec->private_data;
1520 spin_lock(&card->ac97_lock);
1522 data = (reg << 16) + value;
1523 outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
1526 for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
1527 if ((inb (card->baseaddr + 0x83) & VIA_CR83_BUSY) == 0)
1533 printk (KERN_WARNING PFX "timeout after AC97 codec write (0x%X, 0x%X)\n", reg, value);
1536 spin_unlock(&card->ac97_lock);
1541 static int via_mixer_open (struct inode *inode, struct file *file)
1543 int minor = iminor(inode);
1544 struct via_info *card;
1545 struct pci_dev *pdev = NULL;
1546 struct pci_driver *drvr;
1548 DPRINTK ("ENTER\n");
1550 while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
1551 drvr = pci_dev_driver (pdev);
1552 if (drvr == &via_driver) {
1553 assert (pci_get_drvdata (pdev) != NULL);
1555 card = pci_get_drvdata (pdev);
1556 if (card->ac97->dev_mixer == minor)
1561 DPRINTK ("EXIT, returning -ENODEV\n");
1565 file->private_data = card->ac97;
1567 DPRINTK ("EXIT, returning 0\n");
1568 return nonseekable_open(inode, file);
1571 static int via_mixer_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
1574 struct ac97_codec *codec = file->private_data;
1575 struct via_info *card;
1576 int nonblock = (file->f_flags & O_NONBLOCK);
1579 DPRINTK ("ENTER\n");
1581 assert (codec != NULL);
1582 card = codec->private_data;
1583 assert (card != NULL);
1585 rc = via_syscall_down (card, nonblock);
1590 * Intercept volume control on 8233 and 8235
1596 case SOUND_MIXER_READ_VOLUME:
1597 return card->mixer_vol;
1598 case SOUND_MIXER_WRITE_VOLUME:
1601 if(get_user(v, (int *)arg))
1606 card->mixer_vol = v;
1611 rc = codec->mixer_ioctl(codec, cmd, arg);
1613 mutex_unlock(&card->syscall_mutex);
1616 DPRINTK ("EXIT, returning %d\n", rc);
1621 static struct file_operations via_mixer_fops = {
1622 .owner = THIS_MODULE,
1623 .open = via_mixer_open,
1624 .llseek = no_llseek,
1625 .ioctl = via_mixer_ioctl,
1629 static int __devinit via_ac97_reset (struct via_info *card)
1631 struct pci_dev *pdev = card->pdev;
1635 DPRINTK ("ENTER\n");
1637 assert (pdev != NULL);
1641 u8 r40,r41,r42,r43,r44,r48;
1642 pci_read_config_byte (card->pdev, 0x40, &r40);
1643 pci_read_config_byte (card->pdev, 0x41, &r41);
1644 pci_read_config_byte (card->pdev, 0x42, &r42);
1645 pci_read_config_byte (card->pdev, 0x43, &r43);
1646 pci_read_config_byte (card->pdev, 0x44, &r44);
1647 pci_read_config_byte (card->pdev, 0x48, &r48);
1648 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
1649 r40,r41,r42,r43,r44,r48);
1651 spin_lock_irq (&card->lock);
1652 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
1653 inb (card->baseaddr + 0x00),
1654 inb (card->baseaddr + 0x01),
1655 inb (card->baseaddr + 0x02),
1656 inl (card->baseaddr + 0x04),
1657 inl (card->baseaddr + 0x0C),
1658 inl (card->baseaddr + 0x80),
1659 inl (card->baseaddr + 0x84));
1660 spin_unlock_irq (&card->lock);
1666 * Reset AC97 controller: enable, disable, enable,
1667 * pausing after each command for good luck. Only
1668 * do this if the codec is not ready, because it causes
1669 * loud pops and such due to such a hard codec reset.
1671 pci_read_config_byte (pdev, VIA_ACLINK_STATUS, &tmp8);
1672 if ((tmp8 & VIA_CR40_AC97_READY) == 0) {
1673 pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
1674 VIA_CR41_AC97_ENABLE |
1675 VIA_CR41_AC97_RESET |
1676 VIA_CR41_AC97_WAKEUP);
1679 pci_write_config_byte (pdev, VIA_ACLINK_CTRL, 0);
1682 pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
1683 VIA_CR41_AC97_ENABLE |
1684 VIA_CR41_PCM_ENABLE |
1685 VIA_CR41_VRA | VIA_CR41_AC97_RESET);
1689 /* Make sure VRA is enabled, in case we didn't do a
1690 * complete codec reset, above
1692 pci_read_config_byte (pdev, VIA_ACLINK_CTRL, &tmp8);
1693 if (((tmp8 & VIA_CR41_VRA) == 0) ||
1694 ((tmp8 & VIA_CR41_AC97_ENABLE) == 0) ||
1695 ((tmp8 & VIA_CR41_PCM_ENABLE) == 0) ||
1696 ((tmp8 & VIA_CR41_AC97_RESET) == 0)) {
1697 pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
1698 VIA_CR41_AC97_ENABLE |
1699 VIA_CR41_PCM_ENABLE |
1700 VIA_CR41_VRA | VIA_CR41_AC97_RESET);
1706 #if 0 /* this breaks on K7M */
1707 /* disable legacy stuff */
1708 pci_write_config_byte (pdev, 0x42, 0x00);
1712 /* route FM trap to IRQ, disable FM trap */
1713 pci_write_config_byte (pdev, 0x48, 0x05);
1717 /* disable all codec GPI interrupts */
1718 outl (0, pci_resource_start (pdev, 0) + 0x8C);
1720 /* WARNING: this line is magic. Remove this
1721 * and things break. */
1722 /* enable variable rate */
1723 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1724 if ((tmp16 & 1) == 0)
1725 via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1727 DPRINTK ("EXIT, returning 0\n");
1732 static void via_ac97_codec_wait (struct ac97_codec *codec)
1734 assert (codec->private_data != NULL);
1735 via_ac97_wait_idle (codec->private_data);
1739 static int __devinit via_ac97_init (struct via_info *card)
1744 DPRINTK ("ENTER\n");
1746 assert (card != NULL);
1748 card->ac97 = ac97_alloc_codec();
1749 if(card->ac97 == NULL)
1752 card->ac97->private_data = card;
1753 card->ac97->codec_read = via_ac97_read_reg;
1754 card->ac97->codec_write = via_ac97_write_reg;
1755 card->ac97->codec_wait = via_ac97_codec_wait;
1757 card->ac97->dev_mixer = register_sound_mixer (&via_mixer_fops, -1);
1758 if (card->ac97->dev_mixer < 0) {
1759 printk (KERN_ERR PFX "unable to register AC97 mixer, aborting\n");
1760 DPRINTK ("EXIT, returning -EIO\n");
1761 ac97_release_codec(card->ac97);
1765 rc = via_ac97_reset (card);
1767 printk (KERN_ERR PFX "unable to reset AC97 codec, aborting\n");
1773 if (ac97_probe_codec (card->ac97) == 0) {
1774 printk (KERN_ERR PFX "unable to probe AC97 codec, aborting\n");
1779 /* enable variable rate */
1780 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1781 via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1784 * If we cannot enable VRA, we have a locked-rate codec.
1785 * We try again to enable VRA before assuming so, however.
1787 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1788 if ((tmp16 & 1) == 0) {
1789 via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1790 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1791 if ((tmp16 & 1) == 0) {
1792 card->locked_rate = 1;
1793 printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n");
1797 DPRINTK ("EXIT, returning 0\n");
1801 unregister_sound_mixer (card->ac97->dev_mixer);
1802 DPRINTK ("EXIT, returning %d\n", rc);
1803 ac97_release_codec(card->ac97);
1808 static void via_ac97_cleanup (struct via_info *card)
1810 DPRINTK ("ENTER\n");
1812 assert (card != NULL);
1813 assert (card->ac97->dev_mixer >= 0);
1815 unregister_sound_mixer (card->ac97->dev_mixer);
1816 ac97_release_codec(card->ac97);
1823 /****************************************************************
1825 * Interrupt-related code
1830 * via_intr_channel - handle an interrupt for a single channel
1832 * @chan: handle interrupt for this channel
1834 * This is the "meat" of the interrupt handler,
1835 * containing the actions taken each time an interrupt
1836 * occurs. All communication and coordination with
1837 * userspace takes place here.
1839 * Locking: inside card->lock
1842 static void via_intr_channel (struct via_info *card, struct via_channel *chan)
1847 /* check pertinent bits of status register for action bits */
1848 status = inb (chan->iobase) & (VIA_SGD_FLAG | VIA_SGD_EOL | VIA_SGD_STOPPED);
1852 /* acknowledge any flagged bits ASAP */
1853 outb (status, chan->iobase);
1855 if (!chan->sgtable) /* XXX: temporary solution */
1858 /* grab current h/w ptr value */
1859 n = atomic_read (&chan->hw_ptr);
1861 /* sanity check: make sure our h/w ptr doesn't have a weird value */
1863 assert (n < chan->frag_number);
1866 /* reset SGD data structure in memory to reflect a full buffer,
1867 * and advance the h/w ptr, wrapping around to zero if needed
1869 if (n == (chan->frag_number - 1)) {
1870 chan->sgtable[n].count = cpu_to_le32(chan->frag_size | VIA_EOL);
1871 atomic_set (&chan->hw_ptr, 0);
1873 chan->sgtable[n].count = cpu_to_le32(chan->frag_size | VIA_FLAG);
1874 atomic_inc (&chan->hw_ptr);
1877 /* accounting crap for SNDCTL_DSP_GETxPTR */
1879 chan->bytes += chan->frag_size;
1880 /* FIXME - signed overflow is undefined */
1881 if (chan->bytes < 0) /* handle overflow of 31-bit value */
1882 chan->bytes = chan->frag_size;
1883 /* all following checks only occur when not in mmap(2) mode */
1884 if (!chan->is_mapped)
1886 /* If we are recording, then n_frags represents the number
1887 * of fragments waiting to be handled by userspace.
1888 * If we are playback, then n_frags represents the number
1889 * of fragments remaining to be filled by userspace.
1890 * We increment here. If we reach max number of fragments,
1891 * this indicates an underrun/overrun. For this case under OSS,
1892 * we stop the record/playback process.
1894 if (atomic_read (&chan->n_frags) < chan->frag_number)
1895 atomic_inc (&chan->n_frags);
1896 assert (atomic_read (&chan->n_frags) <= chan->frag_number);
1897 if (atomic_read (&chan->n_frags) == chan->frag_number) {
1898 chan->is_active = 0;
1899 via_chan_stop (chan->iobase);
1902 /* wake up anyone listening to see when interrupts occur */
1903 wake_up_all (&chan->wait);
1905 DPRINTK ("%s intr, status=0x%02X, hwptr=0x%lX, chan->hw_ptr=%d\n",
1906 chan->name, status, (long) inl (chan->iobase + 0x04),
1907 atomic_read (&chan->hw_ptr));
1909 DPRINTK ("%s intr, channel n_frags == %d, missed %d\n", chan->name,
1910 atomic_read (&chan->n_frags), missed);
1914 static irqreturn_t via_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1916 struct via_info *card = dev_id;
1919 /* to minimize interrupt sharing costs, we use the SGD status
1920 * shadow register to check the status of all inputs and
1921 * outputs with a single 32-bit bus read. If no interrupt
1922 * conditions are flagged, we exit immediately
1924 status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW);
1925 if (!(status32 & VIA_INTR_MASK))
1927 #ifdef CONFIG_MIDI_VIA82CXXX
1928 if (card->midi_devc)
1929 uart401intr(irq, card->midi_devc, regs);
1933 DPRINTK ("intr, status32 == 0x%08X\n", status32);
1935 /* synchronize interrupt handling under SMP. this spinlock
1936 * goes away completely on UP
1938 spin_lock (&card->lock);
1940 if (status32 & VIA_INTR_OUT)
1941 via_intr_channel (card, &card->ch_out);
1942 if (status32 & VIA_INTR_IN)
1943 via_intr_channel (card, &card->ch_in);
1944 if (status32 & VIA_INTR_FM)
1945 via_intr_channel (card, &card->ch_fm);
1947 spin_unlock (&card->lock);
1952 static irqreturn_t via_new_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1954 struct via_info *card = dev_id;
1957 /* to minimize interrupt sharing costs, we use the SGD status
1958 * shadow register to check the status of all inputs and
1959 * outputs with a single 32-bit bus read. If no interrupt
1960 * conditions are flagged, we exit immediately
1962 status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW);
1963 if (!(status32 & VIA_NEW_INTR_MASK))
1966 * goes away completely on UP
1968 spin_lock (&card->lock);
1970 via_intr_channel (card, &card->ch_out);
1971 via_intr_channel (card, &card->ch_in);
1972 via_intr_channel (card, &card->ch_fm);
1974 spin_unlock (&card->lock);
1980 * via_interrupt_init - Initialize interrupt handling
1981 * @card: Private info for specified board
1983 * Obtain and reserve IRQ for using in handling audio events.
1984 * Also, disable any IRQ-generating resources, to make sure
1985 * we don't get interrupts before we want them.
1988 static int via_interrupt_init (struct via_info *card)
1992 DPRINTK ("ENTER\n");
1994 assert (card != NULL);
1995 assert (card->pdev != NULL);
1997 /* check for sane IRQ number. can this ever happen? */
1998 if (card->pdev->irq < 2) {
1999 printk (KERN_ERR PFX "insane IRQ %d, aborting\n",
2001 DPRINTK ("EXIT, returning -EIO\n");
2005 /* VIA requires this is done */
2006 pci_write_config_byte(card->pdev, PCI_INTERRUPT_LINE, card->pdev->irq);
2010 /* make sure FM irq is not routed to us */
2011 pci_read_config_byte (card->pdev, VIA_FM_NMI_CTRL, &tmp8);
2012 if ((tmp8 & VIA_CR48_FM_TRAP_TO_NMI) == 0) {
2013 tmp8 |= VIA_CR48_FM_TRAP_TO_NMI;
2014 pci_write_config_byte (card->pdev, VIA_FM_NMI_CTRL, tmp8);
2016 if (request_irq (card->pdev->irq, via_interrupt, SA_SHIRQ, VIA_MODULE_NAME, card)) {
2017 printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n",
2019 DPRINTK ("EXIT, returning -EBUSY\n");
2025 if (request_irq (card->pdev->irq, via_new_interrupt, SA_SHIRQ, VIA_MODULE_NAME, card)) {
2026 printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n",
2028 DPRINTK ("EXIT, returning -EBUSY\n");
2033 DPRINTK ("EXIT, returning 0\n");
2038 /****************************************************************
2044 static struct file_operations via_dsp_fops = {
2045 .owner = THIS_MODULE,
2046 .open = via_dsp_open,
2047 .release = via_dsp_release,
2048 .read = via_dsp_read,
2049 .write = via_dsp_write,
2050 .poll = via_dsp_poll,
2051 .llseek = no_llseek,
2052 .ioctl = via_dsp_ioctl,
2053 .mmap = via_dsp_mmap,
2057 static int __devinit via_dsp_init (struct via_info *card)
2061 DPRINTK ("ENTER\n");
2063 assert (card != NULL);
2067 /* turn off legacy features, if not already */
2068 pci_read_config_byte (card->pdev, VIA_FUNC_ENABLE, &tmp8);
2069 if (tmp8 & (VIA_CR42_SB_ENABLE | VIA_CR42_FM_ENABLE)) {
2070 tmp8 &= ~(VIA_CR42_SB_ENABLE | VIA_CR42_FM_ENABLE);
2071 pci_write_config_byte (card->pdev, VIA_FUNC_ENABLE, tmp8);
2075 via_stop_everything (card);
2077 card->dev_dsp = register_sound_dsp (&via_dsp_fops, -1);
2078 if (card->dev_dsp < 0) {
2079 DPRINTK ("EXIT, returning -ENODEV\n");
2082 DPRINTK ("EXIT, returning 0\n");
2087 static void via_dsp_cleanup (struct via_info *card)
2089 DPRINTK ("ENTER\n");
2091 assert (card != NULL);
2092 assert (card->dev_dsp >= 0);
2094 via_stop_everything (card);
2096 unregister_sound_dsp (card->dev_dsp);
2102 static struct page * via_mm_nopage (struct vm_area_struct * vma,
2103 unsigned long address, int *type)
2105 struct via_info *card = vma->vm_private_data;
2106 struct via_channel *chan = &card->ch_out;
2107 struct page *dmapage;
2108 unsigned long pgoff;
2111 DPRINTK ("ENTER, start %lXh, ofs %lXh, pgoff %ld, addr %lXh\n",
2113 address - vma->vm_start,
2114 (address - vma->vm_start) >> PAGE_SHIFT,
2117 if (address > vma->vm_end) {
2118 DPRINTK ("EXIT, returning NOPAGE_SIGBUS\n");
2119 return NOPAGE_SIGBUS; /* Disallow mremap */
2122 DPRINTK ("EXIT, returning NOPAGE_OOM\n");
2123 return NOPAGE_OOM; /* Nothing allocated */
2126 pgoff = vma->vm_pgoff + ((address - vma->vm_start) >> PAGE_SHIFT);
2127 rd = card->ch_in.is_mapped;
2128 wr = card->ch_out.is_mapped;
2132 unsigned long max_bufs = chan->frag_number;
2133 if (rd && wr) max_bufs *= 2;
2134 /* via_dsp_mmap() should ensure this */
2135 assert (pgoff < max_bufs);
2139 /* if full-duplex (read+write) and we have two sets of bufs,
2140 * then the playback buffers come first, sez soundcard.c */
2141 if (pgoff >= chan->page_number) {
2142 pgoff -= chan->page_number;
2143 chan = &card->ch_in;
2145 chan = &card->ch_in;
2147 assert ((((unsigned long)chan->pgtbl[pgoff].cpuaddr) % PAGE_SIZE) == 0);
2149 dmapage = virt_to_page (chan->pgtbl[pgoff].cpuaddr);
2150 DPRINTK ("EXIT, returning page %p for cpuaddr %lXh\n",
2151 dmapage, (unsigned long) chan->pgtbl[pgoff].cpuaddr);
2154 *type = VM_FAULT_MINOR;
2160 static int via_mm_swapout (struct page *page, struct file *filp)
2164 #endif /* VM_RESERVED */
2167 static struct vm_operations_struct via_mm_ops = {
2168 .nopage = via_mm_nopage,
2171 .swapout = via_mm_swapout,
2176 static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma)
2178 struct via_info *card;
2179 int nonblock = (file->f_flags & O_NONBLOCK);
2180 int rc = -EINVAL, rd=0, wr=0;
2181 unsigned long max_size, size, start, offset;
2183 assert (file != NULL);
2184 assert (vma != NULL);
2185 card = file->private_data;
2186 assert (card != NULL);
2188 DPRINTK ("ENTER, start %lXh, size %ld, pgoff %ld\n",
2190 vma->vm_end - vma->vm_start,
2194 if (vma->vm_flags & VM_READ) {
2196 via_chan_set_buffering(card, &card->ch_in, -1);
2197 via_chan_buffer_init (card, &card->ch_in);
2198 max_size += card->ch_in.page_number << PAGE_SHIFT;
2200 if (vma->vm_flags & VM_WRITE) {
2202 via_chan_set_buffering(card, &card->ch_out, -1);
2203 via_chan_buffer_init (card, &card->ch_out);
2204 max_size += card->ch_out.page_number << PAGE_SHIFT;
2207 start = vma->vm_start;
2208 offset = (vma->vm_pgoff << PAGE_SHIFT);
2209 size = vma->vm_end - vma->vm_start;
2211 /* some basic size/offset sanity checks */
2212 if (size > max_size)
2214 if (offset > max_size - size)
2217 rc = via_syscall_down (card, nonblock);
2220 vma->vm_ops = &via_mm_ops;
2221 vma->vm_private_data = card;
2224 vma->vm_flags |= VM_RESERVED;
2228 card->ch_in.is_mapped = 1;
2230 card->ch_out.is_mapped = 1;
2232 mutex_unlock(&card->syscall_mutex);
2236 DPRINTK ("EXIT, returning %d\n", rc);
2241 static ssize_t via_dsp_do_read (struct via_info *card,
2242 char __user *userbuf, size_t count,
2245 DECLARE_WAITQUEUE(wait, current);
2246 const char __user *orig_userbuf = userbuf;
2247 struct via_channel *chan = &card->ch_in;
2252 /* if SGD has not yet been started, start it */
2253 via_chan_maybe_start (chan);
2256 /* just to be a nice neighbor */
2258 * But also to ourselves, release semaphore if we do so */
2259 if (need_resched()) {
2260 mutex_unlock(&card->syscall_mutex);
2262 ret = via_syscall_down (card, nonblock);
2267 /* grab current channel software pointer. In the case of
2268 * recording, this is pointing to the next buffer that
2269 * will receive data from the audio hardware.
2273 /* n_frags represents the number of fragments waiting
2274 * to be copied to userland. sleep until at least
2275 * one buffer has been read from the audio hardware.
2277 add_wait_queue(&chan->wait, &wait);
2279 __set_current_state(TASK_INTERRUPTIBLE);
2280 tmp = atomic_read (&chan->n_frags);
2282 assert (tmp <= chan->frag_number);
2285 if (nonblock || !chan->is_active) {
2290 mutex_unlock(&card->syscall_mutex);
2292 DPRINTK ("Sleeping on block %d\n", n);
2295 ret = via_syscall_down (card, nonblock);
2299 if (signal_pending (current)) {
2304 set_current_state(TASK_RUNNING);
2305 remove_wait_queue(&chan->wait, &wait);
2309 /* Now that we have a buffer we can read from, send
2310 * as much as sample data possible to userspace.
2312 while ((count > 0) && (chan->slop_len < chan->frag_size)) {
2313 size_t slop_left = chan->frag_size - chan->slop_len;
2314 void *base = chan->pgtbl[n / (PAGE_SIZE / chan->frag_size)].cpuaddr;
2315 unsigned ofs = (n % (PAGE_SIZE / chan->frag_size)) * chan->frag_size;
2317 size = (count < slop_left) ? count : slop_left;
2318 if (copy_to_user (userbuf,
2319 base + ofs + chan->slop_len,
2326 chan->slop_len += size;
2330 /* If we didn't copy the buffer completely to userspace,
2333 if (chan->slop_len < chan->frag_size)
2337 * If we get to this point, we copied one buffer completely
2338 * to userspace, give the buffer back to the hardware.
2341 /* advance channel software pointer to point to
2342 * the next buffer from which we will copy
2344 if (chan->sw_ptr == (chan->frag_number - 1))
2349 /* mark one less buffer waiting to be processed */
2350 assert (atomic_read (&chan->n_frags) > 0);
2351 atomic_dec (&chan->n_frags);
2353 /* we are at a block boundary, there is no fragment data */
2356 DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
2357 n, chan->sw_ptr, atomic_read (&chan->n_frags));
2359 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2360 inb (card->baseaddr + 0x00),
2361 inb (card->baseaddr + 0x01),
2362 inb (card->baseaddr + 0x02),
2363 inl (card->baseaddr + 0x04),
2364 inl (card->baseaddr + 0x0C),
2365 inl (card->baseaddr + 0x80),
2366 inl (card->baseaddr + 0x84));
2369 goto handle_one_block;
2372 return (userbuf != orig_userbuf) ? (userbuf - orig_userbuf) : ret;
2376 static ssize_t via_dsp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
2378 struct via_info *card;
2379 int nonblock = (file->f_flags & O_NONBLOCK);
2382 DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
2383 file, buffer, count, ppos ? ((unsigned long)*ppos) : 0);
2385 assert (file != NULL);
2386 card = file->private_data;
2387 assert (card != NULL);
2389 rc = via_syscall_down (card, nonblock);
2392 if (card->ch_in.is_mapped) {
2397 via_chan_set_buffering(card, &card->ch_in, -1);
2398 rc = via_chan_buffer_init (card, &card->ch_in);
2403 rc = via_dsp_do_read (card, buffer, count, nonblock);
2406 mutex_unlock(&card->syscall_mutex);
2408 DPRINTK ("EXIT, returning %ld\n",(long) rc);
2413 static ssize_t via_dsp_do_write (struct via_info *card,
2414 const char __user *userbuf, size_t count,
2417 DECLARE_WAITQUEUE(wait, current);
2418 const char __user *orig_userbuf = userbuf;
2419 struct via_channel *chan = &card->ch_out;
2420 volatile struct via_sgd_table *sgtable = chan->sgtable;
2426 /* just to be a nice neighbor */
2428 * But also to ourselves, release semaphore if we do so */
2429 if (need_resched()) {
2430 mutex_unlock(&card->syscall_mutex);
2432 ret = via_syscall_down (card, nonblock);
2437 /* grab current channel fragment pointer. In the case of
2438 * playback, this is pointing to the next fragment that
2439 * should receive data from userland.
2443 /* n_frags represents the number of fragments remaining
2444 * to be filled by userspace. Sleep until
2445 * at least one fragment is available for our use.
2447 add_wait_queue(&chan->wait, &wait);
2449 __set_current_state(TASK_INTERRUPTIBLE);
2450 tmp = atomic_read (&chan->n_frags);
2452 assert (tmp <= chan->frag_number);
2455 if (nonblock || !chan->is_active) {
2460 mutex_unlock(&card->syscall_mutex);
2462 DPRINTK ("Sleeping on page %d, tmp==%d, ir==%d\n", n, tmp, chan->is_record);
2465 ret = via_syscall_down (card, nonblock);
2469 if (signal_pending (current)) {
2474 set_current_state(TASK_RUNNING);
2475 remove_wait_queue(&chan->wait, &wait);
2479 /* Now that we have at least one fragment we can write to, fill the buffer
2480 * as much as possible with data from userspace.
2482 while ((count > 0) && (chan->slop_len < chan->frag_size)) {
2483 size_t slop_left = chan->frag_size - chan->slop_len;
2485 size = (count < slop_left) ? count : slop_left;
2486 if (copy_from_user (chan->pgtbl[n / (PAGE_SIZE / chan->frag_size)].cpuaddr + (n % (PAGE_SIZE / chan->frag_size)) * chan->frag_size + chan->slop_len,
2493 chan->slop_len += size;
2497 /* If we didn't fill up the buffer with data, stop now.
2498 * Put a 'stop' marker in the DMA table too, to tell the
2499 * audio hardware to stop if it gets here.
2501 if (chan->slop_len < chan->frag_size) {
2502 sgtable[n].count = cpu_to_le32 (chan->slop_len | VIA_EOL | VIA_STOP);
2507 * If we get to this point, we have filled a buffer with
2508 * audio data, flush the buffer to audio hardware.
2511 /* Record the true size for the audio hardware to notice */
2512 if (n == (chan->frag_number - 1))
2513 sgtable[n].count = cpu_to_le32 (chan->frag_size | VIA_EOL);
2515 sgtable[n].count = cpu_to_le32 (chan->frag_size | VIA_FLAG);
2517 /* advance channel software pointer to point to
2518 * the next buffer we will fill with data
2520 if (chan->sw_ptr == (chan->frag_number - 1))
2525 /* mark one less buffer as being available for userspace consumption */
2526 assert (atomic_read (&chan->n_frags) > 0);
2527 atomic_dec (&chan->n_frags);
2529 /* we are at a block boundary, there is no fragment data */
2532 /* if SGD has not yet been started, start it */
2533 via_chan_maybe_start (chan);
2535 DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
2536 n, chan->sw_ptr, atomic_read (&chan->n_frags));
2538 DPRINTK ("regs==S=%02X C=%02X TP=%02X BP=%08X RT=%08X SG=%08X CC=%08X SS=%08X\n",
2539 inb (card->baseaddr + 0x00),
2540 inb (card->baseaddr + 0x01),
2541 inb (card->baseaddr + 0x02),
2542 inl (card->baseaddr + 0x04),
2543 inl (card->baseaddr + 0x08),
2544 inl (card->baseaddr + 0x0C),
2545 inl (card->baseaddr + 0x80),
2546 inl (card->baseaddr + 0x84));
2549 goto handle_one_block;
2552 if (userbuf - orig_userbuf)
2553 return userbuf - orig_userbuf;
2559 static ssize_t via_dsp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
2561 struct via_info *card;
2563 int nonblock = (file->f_flags & O_NONBLOCK);
2565 DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
2566 file, buffer, count, ppos ? ((unsigned long)*ppos) : 0);
2568 assert (file != NULL);
2569 card = file->private_data;
2570 assert (card != NULL);
2572 rc = via_syscall_down (card, nonblock);
2575 if (card->ch_out.is_mapped) {
2580 via_chan_set_buffering(card, &card->ch_out, -1);
2581 rc = via_chan_buffer_init (card, &card->ch_out);
2586 rc = via_dsp_do_write (card, buffer, count, nonblock);
2589 mutex_unlock(&card->syscall_mutex);
2591 DPRINTK ("EXIT, returning %ld\n",(long) rc);
2596 static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait)
2598 struct via_info *card;
2599 struct via_channel *chan;
2600 unsigned int mask = 0;
2602 DPRINTK ("ENTER\n");
2604 assert (file != NULL);
2605 card = file->private_data;
2606 assert (card != NULL);
2608 if (file->f_mode & FMODE_READ) {
2609 chan = &card->ch_in;
2610 if (sg_active (chan->iobase))
2611 poll_wait(file, &chan->wait, wait);
2612 if (atomic_read (&chan->n_frags) > 0)
2613 mask |= POLLIN | POLLRDNORM;
2616 if (file->f_mode & FMODE_WRITE) {
2617 chan = &card->ch_out;
2618 if (sg_active (chan->iobase))
2619 poll_wait(file, &chan->wait, wait);
2620 if (atomic_read (&chan->n_frags) > 0)
2621 mask |= POLLOUT | POLLWRNORM;
2624 DPRINTK ("EXIT, returning %u\n", mask);
2630 * via_dsp_drain_playback - sleep until all playback samples are flushed
2631 * @card: Private info for specified board
2632 * @chan: Channel to drain
2633 * @nonblock: boolean, non-zero if O_NONBLOCK is set
2635 * Sleeps until all playback has been flushed to the audio
2638 * Locking: inside card->syscall_mutex
2641 static int via_dsp_drain_playback (struct via_info *card,
2642 struct via_channel *chan, int nonblock)
2644 DECLARE_WAITQUEUE(wait, current);
2647 DPRINTK ("ENTER, nonblock = %d\n", nonblock);
2649 if (chan->slop_len > 0)
2650 via_chan_flush_frag (chan);
2652 if (atomic_read (&chan->n_frags) == chan->frag_number)
2655 via_chan_maybe_start (chan);
2657 add_wait_queue(&chan->wait, &wait);
2659 DPRINTK ("FRAGS %d FRAGNUM %d\n", atomic_read(&chan->n_frags), chan->frag_number);
2660 __set_current_state(TASK_INTERRUPTIBLE);
2661 if (atomic_read (&chan->n_frags) >= chan->frag_number)
2665 DPRINTK ("EXIT, returning -EAGAIN\n");
2672 u8 r40,r41,r42,r43,r44,r48;
2673 pci_read_config_byte (card->pdev, 0x40, &r40);
2674 pci_read_config_byte (card->pdev, 0x41, &r41);
2675 pci_read_config_byte (card->pdev, 0x42, &r42);
2676 pci_read_config_byte (card->pdev, 0x43, &r43);
2677 pci_read_config_byte (card->pdev, 0x44, &r44);
2678 pci_read_config_byte (card->pdev, 0x48, &r48);
2679 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
2680 r40,r41,r42,r43,r44,r48);
2682 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2683 inb (card->baseaddr + 0x00),
2684 inb (card->baseaddr + 0x01),
2685 inb (card->baseaddr + 0x02),
2686 inl (card->baseaddr + 0x04),
2687 inl (card->baseaddr + 0x0C),
2688 inl (card->baseaddr + 0x80),
2689 inl (card->baseaddr + 0x84));
2692 if (!chan->is_active)
2693 printk (KERN_ERR "sleeping but not active\n");
2696 mutex_unlock(&card->syscall_mutex);
2698 DPRINTK ("sleeping, nbufs=%d\n", atomic_read (&chan->n_frags));
2701 if ((ret = via_syscall_down (card, nonblock)))
2704 if (signal_pending (current)) {
2705 DPRINTK ("EXIT, returning -ERESTARTSYS\n");
2710 set_current_state(TASK_RUNNING);
2711 remove_wait_queue(&chan->wait, &wait);
2715 u8 r40,r41,r42,r43,r44,r48;
2716 pci_read_config_byte (card->pdev, 0x40, &r40);
2717 pci_read_config_byte (card->pdev, 0x41, &r41);
2718 pci_read_config_byte (card->pdev, 0x42, &r42);
2719 pci_read_config_byte (card->pdev, 0x43, &r43);
2720 pci_read_config_byte (card->pdev, 0x44, &r44);
2721 pci_read_config_byte (card->pdev, 0x48, &r48);
2722 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
2723 r40,r41,r42,r43,r44,r48);
2725 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2726 inb (card->baseaddr + 0x00),
2727 inb (card->baseaddr + 0x01),
2728 inb (card->baseaddr + 0x02),
2729 inl (card->baseaddr + 0x04),
2730 inl (card->baseaddr + 0x0C),
2731 inl (card->baseaddr + 0x80),
2732 inl (card->baseaddr + 0x84));
2734 DPRINTK ("final nbufs=%d\n", atomic_read (&chan->n_frags));
2739 DPRINTK ("EXIT, returning %d\n", ret);
2745 * via_dsp_ioctl_space - get information about channel buffering
2746 * @card: Private info for specified board
2747 * @chan: pointer to channel-specific info
2748 * @arg: user buffer for returned information
2750 * Handles SNDCTL_DSP_GETISPACE and SNDCTL_DSP_GETOSPACE.
2752 * Locking: inside card->syscall_mutex
2755 static int via_dsp_ioctl_space (struct via_info *card,
2756 struct via_channel *chan,
2759 audio_buf_info info;
2761 via_chan_set_buffering(card, chan, -1);
2763 info.fragstotal = chan->frag_number;
2764 info.fragsize = chan->frag_size;
2766 /* number of full fragments we can read/write without blocking */
2767 info.fragments = atomic_read (&chan->n_frags);
2769 if ((chan->slop_len % chan->frag_size > 0) && (info.fragments > 0))
2772 /* number of bytes that can be read or written immediately
2775 info.bytes = (info.fragments * chan->frag_size);
2776 if (chan->slop_len % chan->frag_size > 0)
2777 info.bytes += chan->frag_size - (chan->slop_len % chan->frag_size);
2779 DPRINTK ("EXIT, returning fragstotal=%d, fragsize=%d, fragments=%d, bytes=%d\n",
2785 return copy_to_user (arg, &info, sizeof (info))?-EFAULT:0;
2790 * via_dsp_ioctl_ptr - get information about hardware buffer ptr
2791 * @card: Private info for specified board
2792 * @chan: pointer to channel-specific info
2793 * @arg: user buffer for returned information
2795 * Handles SNDCTL_DSP_GETIPTR and SNDCTL_DSP_GETOPTR.
2797 * Locking: inside card->syscall_mutex
2800 static int via_dsp_ioctl_ptr (struct via_info *card,
2801 struct via_channel *chan,
2806 spin_lock_irq (&card->lock);
2808 info.bytes = chan->bytes;
2809 info.blocks = chan->n_irqs;
2812 spin_unlock_irq (&card->lock);
2814 if (chan->is_active) {
2815 unsigned long extra;
2816 info.ptr = atomic_read (&chan->hw_ptr) * chan->frag_size;
2817 extra = chan->frag_size - via_sg_offset(chan);
2819 info.bytes += extra;
2824 DPRINTK ("EXIT, returning bytes=%d, blocks=%d, ptr=%d\n",
2829 return copy_to_user (arg, &info, sizeof (info))?-EFAULT:0;
2833 static int via_dsp_ioctl_trigger (struct via_channel *chan, int val)
2835 int enable, do_something;
2837 if (chan->is_record)
2838 enable = (val & PCM_ENABLE_INPUT);
2840 enable = (val & PCM_ENABLE_OUTPUT);
2842 if (!chan->is_enabled && enable) {
2844 } else if (chan->is_enabled && !enable) {
2850 DPRINTK ("enable=%d, do_something=%d\n",
2851 enable, do_something);
2853 if (chan->is_active && do_something)
2856 if (do_something == 1) {
2857 chan->is_enabled = 1;
2858 via_chan_maybe_start (chan);
2859 DPRINTK ("Triggering input\n");
2862 else if (do_something == -1) {
2863 chan->is_enabled = 0;
2864 DPRINTK ("Setup input trigger\n");
2871 static int via_dsp_ioctl (struct inode *inode, struct file *file,
2872 unsigned int cmd, unsigned long arg)
2874 int rc, rd=0, wr=0, val=0;
2875 struct via_info *card;
2876 struct via_channel *chan;
2877 int nonblock = (file->f_flags & O_NONBLOCK);
2878 int __user *ip = (int __user *)arg;
2879 void __user *p = (void __user *)arg;
2881 assert (file != NULL);
2882 card = file->private_data;
2883 assert (card != NULL);
2885 if (file->f_mode & FMODE_WRITE)
2887 if (file->f_mode & FMODE_READ)
2890 rc = via_syscall_down (card, nonblock);
2897 /* OSS API version. XXX unverified */
2898 case OSS_GETVERSION:
2899 DPRINTK ("ioctl OSS_GETVERSION, EXIT, returning SOUND_VERSION\n");
2900 rc = put_user (SOUND_VERSION, ip);
2903 /* list of supported PCM data formats */
2904 case SNDCTL_DSP_GETFMTS:
2905 DPRINTK ("DSP_GETFMTS, EXIT, returning AFMT U8|S16_LE\n");
2906 rc = put_user (AFMT_U8 | AFMT_S16_LE, ip);
2909 /* query or set current channel's PCM data format */
2910 case SNDCTL_DSP_SETFMT:
2911 if (get_user(val, ip)) {
2915 DPRINTK ("DSP_SETFMT, val==%d\n", val);
2916 if (val != AFMT_QUERY) {
2920 rc = via_chan_set_fmt (card, &card->ch_in, val);
2923 rc = via_chan_set_fmt (card, &card->ch_out, val);
2930 if ((rd && (card->ch_in.pcm_fmt & VIA_PCM_FMT_16BIT)) ||
2931 (wr && (card->ch_out.pcm_fmt & VIA_PCM_FMT_16BIT)))
2936 DPRINTK ("SETFMT EXIT, returning %d\n", val);
2937 rc = put_user (val, ip);
2940 /* query or set number of channels (1=mono, 2=stereo, 4/6 for multichannel) */
2941 case SNDCTL_DSP_CHANNELS:
2942 if (get_user(val, ip)) {
2946 DPRINTK ("DSP_CHANNELS, val==%d\n", val);
2951 rc = via_chan_set_stereo (card, &card->ch_in, val);
2954 rc = via_chan_set_stereo (card, &card->ch_out, val);
2962 val = card->ch_in.channels;
2964 val = card->ch_out.channels;
2966 DPRINTK ("CHANNELS EXIT, returning %d\n", val);
2967 rc = put_user (val, ip);
2970 /* enable (val is not zero) or disable (val == 0) stereo */
2971 case SNDCTL_DSP_STEREO:
2972 if (get_user(val, ip)) {
2976 DPRINTK ("DSP_STEREO, val==%d\n", val);
2980 rc = via_chan_set_stereo (card, &card->ch_in, val ? 2 : 1);
2982 rc = via_chan_set_stereo (card, &card->ch_out, val ? 2 : 1);
2989 DPRINTK ("STEREO EXIT, returning %d\n", val);
2990 rc = put_user(val, ip);
2993 /* query or set sampling rate */
2994 case SNDCTL_DSP_SPEED:
2995 if (get_user(val, ip)) {
2999 DPRINTK ("DSP_SPEED, val==%d\n", val);
3008 rc = via_chan_set_speed (card, &card->ch_in, val);
3010 rc = via_chan_set_speed (card, &card->ch_out, val);
3018 val = card->ch_in.rate;
3020 val = card->ch_out.rate;
3024 DPRINTK ("SPEED EXIT, returning %d\n", val);
3025 rc = put_user (val, ip);
3028 /* wait until all buffers have been played, and then stop device */
3029 case SNDCTL_DSP_SYNC:
3030 DPRINTK ("DSP_SYNC\n");
3033 DPRINTK ("SYNC EXIT (after calling via_dsp_drain_playback)\n");
3034 rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
3038 /* stop recording/playback immediately */
3039 case SNDCTL_DSP_RESET:
3040 DPRINTK ("DSP_RESET\n");
3042 via_chan_clear (card, &card->ch_in);
3043 card->ch_in.frag_number = 0;
3044 card->ch_in.frag_size = 0;
3045 atomic_set(&card->ch_in.n_frags, 0);
3049 via_chan_clear (card, &card->ch_out);
3050 card->ch_out.frag_number = 0;
3051 card->ch_out.frag_size = 0;
3052 atomic_set(&card->ch_out.n_frags, 0);
3058 case SNDCTL_DSP_NONBLOCK:
3059 file->f_flags |= O_NONBLOCK;
3063 /* obtain bitmask of device capabilities, such as mmap, full duplex, etc. */
3064 case SNDCTL_DSP_GETCAPS:
3065 DPRINTK ("DSP_GETCAPS\n");
3066 rc = put_user(VIA_DSP_CAP, ip);
3069 /* obtain buffer fragment size */
3070 case SNDCTL_DSP_GETBLKSIZE:
3071 DPRINTK ("DSP_GETBLKSIZE\n");
3074 via_chan_set_buffering(card, &card->ch_in, -1);
3075 rc = put_user(card->ch_in.frag_size, ip);
3077 via_chan_set_buffering(card, &card->ch_out, -1);
3078 rc = put_user(card->ch_out.frag_size, ip);
3082 /* obtain information about input buffering */
3083 case SNDCTL_DSP_GETISPACE:
3084 DPRINTK ("DSP_GETISPACE\n");
3086 rc = via_dsp_ioctl_space (card, &card->ch_in, p);
3089 /* obtain information about output buffering */
3090 case SNDCTL_DSP_GETOSPACE:
3091 DPRINTK ("DSP_GETOSPACE\n");
3093 rc = via_dsp_ioctl_space (card, &card->ch_out, p);
3096 /* obtain information about input hardware pointer */
3097 case SNDCTL_DSP_GETIPTR:
3098 DPRINTK ("DSP_GETIPTR\n");
3100 rc = via_dsp_ioctl_ptr (card, &card->ch_in, p);
3103 /* obtain information about output hardware pointer */
3104 case SNDCTL_DSP_GETOPTR:
3105 DPRINTK ("DSP_GETOPTR\n");
3107 rc = via_dsp_ioctl_ptr (card, &card->ch_out, p);
3110 /* return number of bytes remaining to be played by DMA engine */
3111 case SNDCTL_DSP_GETODELAY:
3113 DPRINTK ("DSP_GETODELAY\n");
3115 chan = &card->ch_out;
3120 if (chan->is_active) {
3122 val = chan->frag_number - atomic_read (&chan->n_frags);
3127 val *= chan->frag_size;
3128 val -= chan->frag_size - via_sg_offset(chan);
3130 val += chan->slop_len % chan->frag_size;
3134 assert (val <= (chan->frag_size * chan->frag_number));
3136 DPRINTK ("GETODELAY EXIT, val = %d bytes\n", val);
3137 rc = put_user (val, ip);
3141 /* handle the quick-start of a channel,
3142 * or the notification that a quick-start will
3143 * occur in the future
3145 case SNDCTL_DSP_SETTRIGGER:
3146 if (get_user(val, ip)) {
3150 DPRINTK ("DSP_SETTRIGGER, rd=%d, wr=%d, act=%d/%d, en=%d/%d\n",
3151 rd, wr, card->ch_in.is_active, card->ch_out.is_active,
3152 card->ch_in.is_enabled, card->ch_out.is_enabled);
3157 rc = via_dsp_ioctl_trigger (&card->ch_in, val);
3160 rc = via_dsp_ioctl_trigger (&card->ch_out, val);
3164 case SNDCTL_DSP_GETTRIGGER:
3166 if ((file->f_mode & FMODE_READ) && card->ch_in.is_enabled)
3167 val |= PCM_ENABLE_INPUT;
3168 if ((file->f_mode & FMODE_WRITE) && card->ch_out.is_enabled)
3169 val |= PCM_ENABLE_OUTPUT;
3170 rc = put_user(val, ip);
3173 /* Enable full duplex. Since we do this as soon as we are opened
3174 * with O_RDWR, this is mainly a no-op that always returns success.
3176 case SNDCTL_DSP_SETDUPLEX:
3177 DPRINTK ("DSP_SETDUPLEX\n");
3183 /* set fragment size. implemented as a successful no-op for now */
3184 case SNDCTL_DSP_SETFRAGMENT:
3185 if (get_user(val, ip)) {
3189 DPRINTK ("DSP_SETFRAGMENT, val==%d\n", val);
3192 rc = via_chan_set_buffering(card, &card->ch_in, val);
3195 rc = via_chan_set_buffering(card, &card->ch_out, val);
3197 DPRINTK ("SNDCTL_DSP_SETFRAGMENT (fragshift==0x%04X (%d), maxfrags==0x%04X (%d))\n",
3200 (val >> 16) & 0xFFFF,
3201 (val >> 16) & 0xFFFF);
3206 /* inform device of an upcoming pause in input (or output). */
3207 case SNDCTL_DSP_POST:
3208 DPRINTK ("DSP_POST\n");
3210 if (card->ch_out.slop_len > 0)
3211 via_chan_flush_frag (&card->ch_out);
3212 via_chan_maybe_start (&card->ch_out);
3218 /* not implemented */
3220 DPRINTK ("unhandled ioctl, cmd==%u, arg==%p\n",
3225 mutex_unlock(&card->syscall_mutex);
3226 DPRINTK ("EXIT, returning %d\n", rc);
3231 static int via_dsp_open (struct inode *inode, struct file *file)
3233 int minor = iminor(inode);
3234 struct via_info *card;
3235 struct pci_dev *pdev = NULL;
3236 struct via_channel *chan;
3237 struct pci_driver *drvr;
3238 int nonblock = (file->f_flags & O_NONBLOCK);
3240 DPRINTK ("ENTER, minor=%d, file->f_mode=0x%x\n", minor, file->f_mode);
3242 if (!(file->f_mode & (FMODE_READ | FMODE_WRITE))) {
3243 DPRINTK ("EXIT, returning -EINVAL\n");
3248 while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
3249 drvr = pci_dev_driver (pdev);
3250 if (drvr == &via_driver) {
3251 assert (pci_get_drvdata (pdev) != NULL);
3253 card = pci_get_drvdata (pdev);
3254 DPRINTK ("dev_dsp = %d, minor = %d, assn = %d\n",
3255 card->dev_dsp, minor,
3256 (card->dev_dsp ^ minor) & ~0xf);
3258 if (((card->dev_dsp ^ minor) & ~0xf) == 0)
3263 DPRINTK ("no matching %s found\n", card ? "minor" : "driver");
3268 if (!mutex_trylock(&card->open_mutex)) {
3269 DPRINTK ("EXIT, returning -EAGAIN\n");
3273 if (mutex_lock_interruptible(&card->open_mutex)) {
3274 DPRINTK ("EXIT, returning -ERESTARTSYS\n");
3275 return -ERESTARTSYS;
3279 file->private_data = card;
3280 DPRINTK ("file->f_mode == 0x%x\n", file->f_mode);
3282 /* handle input from analog source */
3283 if (file->f_mode & FMODE_READ) {
3284 chan = &card->ch_in;
3286 via_chan_init (card, chan);
3288 /* why is this forced to 16-bit stereo in all drivers? */
3289 chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
3292 // TO DO - use FIFO: via_capture_fifo(card, 1);
3293 via_chan_pcm_fmt (chan, 0);
3294 via_set_rate (card->ac97, chan, 44100);
3297 /* handle output to analog source */
3298 if (file->f_mode & FMODE_WRITE) {
3299 chan = &card->ch_out;
3301 via_chan_init (card, chan);
3303 if (file->f_mode & FMODE_READ) {
3304 /* if in duplex mode make the recording and playback channels
3305 have the same settings */
3306 chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
3308 via_chan_pcm_fmt (chan, 0);
3309 via_set_rate (card->ac97, chan, 44100);
3311 if ((minor & 0xf) == SND_DEV_DSP16) {
3312 chan->pcm_fmt = VIA_PCM_FMT_16BIT;
3313 via_chan_pcm_fmt (chan, 0);
3314 via_set_rate (card->ac97, chan, 44100);
3316 via_chan_pcm_fmt (chan, 1);
3317 via_set_rate (card->ac97, chan, 8000);
3322 DPRINTK ("EXIT, returning 0\n");
3323 return nonseekable_open(inode, file);
3327 static int via_dsp_release(struct inode *inode, struct file *file)
3329 struct via_info *card;
3330 int nonblock = (file->f_flags & O_NONBLOCK);
3333 DPRINTK ("ENTER\n");
3335 assert (file != NULL);
3336 card = file->private_data;
3337 assert (card != NULL);
3339 rc = via_syscall_down (card, nonblock);
3341 DPRINTK ("EXIT (syscall_down error), rc=%d\n", rc);
3345 if (file->f_mode & FMODE_WRITE) {
3346 rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
3347 if (rc && rc != -ERESTARTSYS) /* Nobody needs to know about ^C */
3348 printk (KERN_DEBUG "via_audio: ignoring drain playback error %d\n", rc);
3350 via_chan_free (card, &card->ch_out);
3351 via_chan_buffer_free(card, &card->ch_out);
3354 if (file->f_mode & FMODE_READ) {
3355 via_chan_free (card, &card->ch_in);
3356 via_chan_buffer_free (card, &card->ch_in);
3359 mutex_unlock(&card->syscall_mutex);
3360 mutex_unlock(&card->open_mutex);
3362 DPRINTK ("EXIT, returning 0\n");
3367 /****************************************************************
3369 * Chip setup and kernel registration
3374 static int __devinit via_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
3376 #ifdef CONFIG_MIDI_VIA82CXXX
3380 struct via_info *card;
3381 static int printed_version;
3383 DPRINTK ("ENTER\n");
3385 if (printed_version++ == 0)
3386 printk (KERN_INFO "Via 686a/8233/8235 audio driver " VIA_VERSION "\n");
3388 rc = pci_enable_device (pdev);
3392 rc = pci_request_regions (pdev, "via82cxxx_audio");
3394 goto err_out_disable;
3396 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3399 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
3403 card = kmalloc (sizeof (*card), GFP_KERNEL);
3405 printk (KERN_ERR PFX "out of memory, aborting\n");
3410 pci_set_drvdata (pdev, card);
3412 memset (card, 0, sizeof (*card));
3414 card->baseaddr = pci_resource_start (pdev, 0);
3415 card->card_num = via_num_cards++;
3416 spin_lock_init (&card->lock);
3417 spin_lock_init (&card->ac97_lock);
3418 mutex_init(&card->syscall_mutex);
3419 mutex_init(&card->open_mutex);
3421 /* we must init these now, in case the intr handler needs them */
3422 via_chan_init_defaults (card, &card->ch_out);
3423 via_chan_init_defaults (card, &card->ch_in);
3424 via_chan_init_defaults (card, &card->ch_fm);
3426 /* if BAR 2 is present, chip is Rev H or later,
3427 * which means it has a few extra features */
3428 if (pci_resource_start (pdev, 2) > 0)
3431 /* Overkill for now, but more flexible done right */
3433 card->intmask = id->driver_data;
3434 card->legacy = !card->intmask;
3435 card->sixchannel = id->driver_data;
3437 if(card->sixchannel)
3438 printk(KERN_INFO PFX "Six channel audio available\n");
3439 if (pdev->irq < 1) {
3440 printk (KERN_ERR PFX "invalid PCI IRQ %d, aborting\n", pdev->irq);
3445 if (!(pci_resource_flags (pdev, 0) & IORESOURCE_IO)) {
3446 printk (KERN_ERR PFX "unable to locate I/O resources, aborting\n");
3451 pci_set_master(pdev);
3454 * init AC97 mixer and codec
3456 rc = via_ac97_init (card);
3458 printk (KERN_ERR PFX "AC97 init failed, aborting\n");
3465 rc = via_dsp_init (card);
3467 printk (KERN_ERR PFX "DSP device init failed, aborting\n");
3468 goto err_out_have_mixer;
3472 * init and turn on interrupts, as the last thing we do
3474 rc = via_interrupt_init (card);
3476 printk (KERN_ERR PFX "interrupt init failed, aborting\n");
3477 goto err_out_have_dsp;
3480 printk (KERN_INFO PFX "board #%d at 0x%04lX, IRQ %d\n",
3481 card->card_num + 1, card->baseaddr, pdev->irq);
3483 #ifdef CONFIG_MIDI_VIA82CXXX
3484 /* Disable by default */
3485 card->midi_info.io_base = 0;
3489 pci_read_config_byte (pdev, 0x42, &r42);
3490 /* Disable MIDI interrupt */
3491 pci_write_config_byte (pdev, 0x42, r42 | VIA_CR42_MIDI_IRQMASK);
3492 if (r42 & VIA_CR42_MIDI_ENABLE)
3494 if (r42 & VIA_CR42_MIDI_PNP) /* Address selected by iobase 2 - not tested */
3495 card->midi_info.io_base = pci_resource_start (pdev, 2);
3496 else /* Address selected by byte 0x43 */
3499 pci_read_config_byte (pdev, 0x43, &r43);
3500 card->midi_info.io_base = 0x300 + ((r43 & 0x0c) << 2);
3503 card->midi_info.irq = -pdev->irq;
3504 if (probe_uart401(& card->midi_info, THIS_MODULE))
3506 card->midi_devc=midi_devs[card->midi_info.slots[4]]->devc;
3507 pci_write_config_byte(pdev, 0x42, r42 & ~VIA_CR42_MIDI_IRQMASK);
3508 printk("Enabled Via MIDI\n");
3514 DPRINTK ("EXIT, returning 0\n");
3518 via_dsp_cleanup (card);
3521 via_ac97_cleanup (card);
3525 memset (card, 0xAB, sizeof (*card)); /* poison memory */
3530 pci_release_regions (pdev);
3533 pci_disable_device (pdev);
3536 pci_set_drvdata (pdev, NULL);
3537 DPRINTK ("EXIT - returning %d\n", rc);
3542 static void __devexit via_remove_one (struct pci_dev *pdev)
3544 struct via_info *card;
3546 DPRINTK ("ENTER\n");
3548 assert (pdev != NULL);
3549 card = pci_get_drvdata (pdev);
3550 assert (card != NULL);
3552 #ifdef CONFIG_MIDI_VIA82CXXX
3553 if (card->midi_info.io_base)
3554 unload_uart401(&card->midi_info);
3557 free_irq (card->pdev->irq, card);
3558 via_dsp_cleanup (card);
3559 via_ac97_cleanup (card);
3562 memset (card, 0xAB, sizeof (*card)); /* poison memory */
3566 pci_set_drvdata (pdev, NULL);
3568 pci_release_regions (pdev);
3569 pci_disable_device (pdev);
3570 pci_set_power_state (pdev, 3); /* ...zzzzzz */
3577 /****************************************************************
3579 * Driver initialization and cleanup
3584 static int __init init_via82cxxx_audio(void)
3588 DPRINTK ("ENTER\n");
3590 rc = pci_register_driver (&via_driver);
3592 DPRINTK ("EXIT, returning %d\n", rc);
3596 DPRINTK ("EXIT, returning 0\n");
3601 static void __exit cleanup_via82cxxx_audio(void)
3603 DPRINTK ("ENTER\n");
3605 pci_unregister_driver (&via_driver);
3611 module_init(init_via82cxxx_audio);
3612 module_exit(cleanup_via82cxxx_audio);
3614 MODULE_AUTHOR("Jeff Garzik");
3615 MODULE_DESCRIPTION("DSP audio and mixer driver for Via 82Cxxx audio devices");
3616 MODULE_LICENSE("GPL");