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/poison.h>
28 #include <linux/init.h>
29 #include <linux/interrupt.h>
30 #include <linux/proc_fs.h>
31 #include <linux/spinlock.h>
32 #include <linux/sound.h>
33 #include <linux/poll.h>
34 #include <linux/soundcard.h>
35 #include <linux/ac97_codec.h>
36 #include <linux/smp_lock.h>
37 #include <linux/ioport.h>
38 #include <linux/delay.h>
39 #include <linux/dma-mapping.h>
41 #include <asm/uaccess.h>
42 #include <linux/mutex.h>
44 #include "sound_config.h"
45 #include "dev_table.h"
49 #undef VIA_DEBUG /* define to enable debugging output and checks */
51 /* note: prints function name for you */
52 #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
54 #define DPRINTK(fmt, args...)
57 #undef VIA_NDEBUG /* define to disable lightweight runtime checks */
61 #define assert(expr) \
63 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
64 #expr,__FILE__,__FUNCTION__,__LINE__); \
68 #define VIA_SUPPORT_MMAP 1 /* buggy, for now... */
72 #define VIA_CARD_NAME "VIA 82Cxxx Audio driver " VIA_VERSION
73 #define VIA_MODULE_NAME "via82cxxx"
74 #define PFX VIA_MODULE_NAME ": "
76 #define VIA_COUNTER_LIMIT 100000
78 /* size of DMA buffers */
79 #define VIA_MAX_BUFFER_DMA_PAGES 32
81 /* buffering default values in ms */
82 #define VIA_DEFAULT_FRAG_TIME 20
83 #define VIA_DEFAULT_BUFFER_TIME 500
85 /* the hardware has a 256 fragment limit */
86 #define VIA_MIN_FRAG_NUMBER 2
87 #define VIA_MAX_FRAG_NUMBER 128
89 #define VIA_MAX_FRAG_SIZE PAGE_SIZE
90 #define VIA_MIN_FRAG_SIZE (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE / VIA_MAX_FRAG_NUMBER)
93 /* 82C686 function 5 (audio codec) PCI configuration registers */
94 #define VIA_ACLINK_STATUS 0x40
95 #define VIA_ACLINK_CTRL 0x41
96 #define VIA_FUNC_ENABLE 0x42
97 #define VIA_PNP_CONTROL 0x43
98 #define VIA_FM_NMI_CTRL 0x48
101 * controller base 0 (scatter-gather) registers
103 * NOTE: Via datasheet lists first channel as "read"
104 * channel and second channel as "write" channel.
105 * I changed the naming of the constants to be more
106 * clear than I felt the datasheet to be.
109 #define VIA_BASE0_PCM_OUT_CHAN 0x00 /* output PCM to user */
110 #define VIA_BASE0_PCM_OUT_CHAN_STATUS 0x00
111 #define VIA_BASE0_PCM_OUT_CHAN_CTRL 0x01
112 #define VIA_BASE0_PCM_OUT_CHAN_TYPE 0x02
114 #define VIA_BASE0_PCM_IN_CHAN 0x10 /* input PCM from user */
115 #define VIA_BASE0_PCM_IN_CHAN_STATUS 0x10
116 #define VIA_BASE0_PCM_IN_CHAN_CTRL 0x11
117 #define VIA_BASE0_PCM_IN_CHAN_TYPE 0x12
119 /* offsets from base */
120 #define VIA_PCM_STATUS 0x00
121 #define VIA_PCM_CONTROL 0x01
122 #define VIA_PCM_TYPE 0x02
123 #define VIA_PCM_LEFTVOL 0x02
124 #define VIA_PCM_RIGHTVOL 0x03
125 #define VIA_PCM_TABLE_ADDR 0x04
126 #define VIA_PCM_STOPRATE 0x08 /* 8233+ */
127 #define VIA_PCM_BLOCK_COUNT 0x0C
129 /* XXX unused DMA channel for FM PCM data */
130 #define VIA_BASE0_FM_OUT_CHAN 0x20
131 #define VIA_BASE0_FM_OUT_CHAN_STATUS 0x20
132 #define VIA_BASE0_FM_OUT_CHAN_CTRL 0x21
133 #define VIA_BASE0_FM_OUT_CHAN_TYPE 0x22
135 /* Six channel audio output on 8233 */
136 #define VIA_BASE0_MULTI_OUT_CHAN 0x40
137 #define VIA_BASE0_MULTI_OUT_CHAN_STATUS 0x40
138 #define VIA_BASE0_MULTI_OUT_CHAN_CTRL 0x41
139 #define VIA_BASE0_MULTI_OUT_CHAN_TYPE 0x42
141 #define VIA_BASE0_AC97_CTRL 0x80
142 #define VIA_BASE0_SGD_STATUS_SHADOW 0x84
143 #define VIA_BASE0_GPI_INT_ENABLE 0x8C
144 #define VIA_INTR_OUT ((1<<0) | (1<<4) | (1<<8))
145 #define VIA_INTR_IN ((1<<1) | (1<<5) | (1<<9))
146 #define VIA_INTR_FM ((1<<2) | (1<<6) | (1<<10))
147 #define VIA_INTR_MASK (VIA_INTR_OUT | VIA_INTR_IN | VIA_INTR_FM)
149 /* Newer VIA we need to monitor the low 3 bits of each channel. This
150 mask covers the channels we don't yet use as well
153 #define VIA_NEW_INTR_MASK 0x77077777UL
155 /* VIA_BASE0_AUDIO_xxx_CHAN_TYPE bits */
156 #define VIA_IRQ_ON_FLAG (1<<0) /* int on each flagged scatter block */
157 #define VIA_IRQ_ON_EOL (1<<1) /* int at end of scatter list */
158 #define VIA_INT_SEL_PCI_LAST_LINE_READ (0) /* int at PCI read of last line */
159 #define VIA_INT_SEL_LAST_SAMPLE_SENT (1<<2) /* int at last sample sent */
160 #define VIA_INT_SEL_ONE_LINE_LEFT (1<<3) /* int at less than one line to send */
161 #define VIA_PCM_FMT_STEREO (1<<4) /* PCM stereo format (bit clear == mono) */
162 #define VIA_PCM_FMT_16BIT (1<<5) /* PCM 16-bit format (bit clear == 8-bit) */
163 #define VIA_PCM_REC_FIFO (1<<6) /* PCM Recording FIFO */
164 #define VIA_RESTART_SGD_ON_EOL (1<<7) /* restart scatter-gather at EOL */
165 #define VIA_PCM_FMT_MASK (VIA_PCM_FMT_STEREO|VIA_PCM_FMT_16BIT)
166 #define VIA_CHAN_TYPE_MASK (VIA_RESTART_SGD_ON_EOL | \
169 #define VIA_CHAN_TYPE_INT_SELECT (VIA_INT_SEL_LAST_SAMPLE_SENT)
171 /* PCI configuration register bits and masks */
172 #define VIA_CR40_AC97_READY 0x01
173 #define VIA_CR40_AC97_LOW_POWER 0x02
174 #define VIA_CR40_SECONDARY_READY 0x04
176 #define VIA_CR41_AC97_ENABLE 0x80 /* enable AC97 codec */
177 #define VIA_CR41_AC97_RESET 0x40 /* clear bit to reset AC97 */
178 #define VIA_CR41_AC97_WAKEUP 0x20 /* wake up from power-down mode */
179 #define VIA_CR41_AC97_SDO 0x10 /* force Serial Data Out (SDO) high */
180 #define VIA_CR41_VRA 0x08 /* enable variable sample rate */
181 #define VIA_CR41_PCM_ENABLE 0x04 /* AC Link SGD Read Channel PCM Data Output */
182 #define VIA_CR41_FM_PCM_ENABLE 0x02 /* AC Link FM Channel PCM Data Out */
183 #define VIA_CR41_SB_PCM_ENABLE 0x01 /* AC Link SB PCM Data Output */
184 #define VIA_CR41_BOOT_MASK (VIA_CR41_AC97_ENABLE | \
185 VIA_CR41_AC97_WAKEUP | \
187 #define VIA_CR41_RUN_MASK (VIA_CR41_AC97_ENABLE | \
188 VIA_CR41_AC97_RESET | \
192 #define VIA_CR42_SB_ENABLE 0x01
193 #define VIA_CR42_MIDI_ENABLE 0x02
194 #define VIA_CR42_FM_ENABLE 0x04
195 #define VIA_CR42_GAME_ENABLE 0x08
196 #define VIA_CR42_MIDI_IRQMASK 0x40
197 #define VIA_CR42_MIDI_PNP 0x80
199 #define VIA_CR44_SECOND_CODEC_SUPPORT (1 << 6)
200 #define VIA_CR44_AC_LINK_ACCESS (1 << 7)
202 #define VIA_CR48_FM_TRAP_TO_NMI (1 << 2)
204 /* controller base 0 register bitmasks */
205 #define VIA_INT_DISABLE_MASK (~(0x01|0x02))
206 #define VIA_SGD_STOPPED (1 << 2)
207 #define VIA_SGD_PAUSED (1 << 6)
208 #define VIA_SGD_ACTIVE (1 << 7)
209 #define VIA_SGD_TERMINATE (1 << 6)
210 #define VIA_SGD_FLAG (1 << 0)
211 #define VIA_SGD_EOL (1 << 1)
212 #define VIA_SGD_START (1 << 7)
214 #define VIA_CR80_FIRST_CODEC 0
215 #define VIA_CR80_SECOND_CODEC (1 << 30)
216 #define VIA_CR80_FIRST_CODEC_VALID (1 << 25)
217 #define VIA_CR80_VALID (1 << 25)
218 #define VIA_CR80_SECOND_CODEC_VALID (1 << 27)
219 #define VIA_CR80_BUSY (1 << 24)
220 #define VIA_CR83_BUSY (1)
221 #define VIA_CR83_FIRST_CODEC_VALID (1 << 1)
222 #define VIA_CR80_READ (1 << 23)
223 #define VIA_CR80_WRITE_MODE 0
224 #define VIA_CR80_REG_IDX(idx) ((((idx) & 0xFF) >> 1) << 16)
226 /* capabilities we announce */
227 #ifdef VIA_SUPPORT_MMAP
228 #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | DSP_CAP_MMAP | \
229 DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
231 #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | \
232 DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
235 /* scatter-gather DMA table entry, exactly as passed to hardware */
236 struct via_sgd_table {
238 u32 count; /* includes additional VIA_xxx bits also */
241 #define VIA_EOL (1 << 31)
242 #define VIA_FLAG (1 << 30)
243 #define VIA_STOP (1 << 29)
246 enum via_channel_states {
252 struct via_buffer_pgtbl {
261 wait_queue_head_t wait;
264 unsigned int slop_len;
268 unsigned is_active : 1;
269 unsigned is_record : 1;
270 unsigned is_mapped : 1;
271 unsigned is_enabled : 1;
272 unsigned is_multi: 1; /* 8233 6 channel */
273 u8 pcm_fmt; /* VIA_PCM_FMT_xxx */
274 u8 channels; /* Channel count */
276 unsigned rate; /* sample rate */
277 unsigned int frag_size;
278 unsigned int frag_number;
280 unsigned char intmask;
282 volatile struct via_sgd_table *sgtable;
283 dma_addr_t sgt_handle;
285 unsigned int page_number;
286 struct via_buffer_pgtbl pgtbl[VIA_MAX_BUFFER_DMA_PAGES];
294 /* data stored for each chip */
296 struct pci_dev *pdev;
299 struct ac97_codec *ac97;
300 spinlock_t ac97_lock;
302 int card_num; /* unique card number, from 0 */
304 int dev_dsp; /* /dev/dsp index from register_sound_dsp() */
307 unsigned legacy: 1; /* Has legacy ports */
308 unsigned intmask: 1; /* Needs int bits */
309 unsigned sixchannel: 1; /* 8233/35 with 6 channel support */
314 int mixer_vol; /* 8233/35 volume - not yet implemented */
316 struct mutex syscall_mutex;
317 struct mutex open_mutex;
319 /* The 8233/8235 have 4 DX audio channels, two record and
320 one six channel out. We bind ch_in to DX 1, ch_out to multichannel
321 and ch_fm to DX 2. DX 3 and REC0/REC1 are unused at the
324 struct via_channel ch_in;
325 struct via_channel ch_out;
326 struct via_channel ch_fm;
328 #ifdef CONFIG_MIDI_VIA82CXXX
330 struct address_info midi_info;
335 /* number of cards, used for assigning unique numbers to cards */
336 static unsigned via_num_cards;
340 /****************************************************************
347 static int via_init_one (struct pci_dev *dev, const struct pci_device_id *id);
348 static void __devexit via_remove_one (struct pci_dev *pdev);
350 static ssize_t via_dsp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos);
351 static ssize_t via_dsp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos);
352 static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait);
353 static int via_dsp_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
354 static int via_dsp_open (struct inode *inode, struct file *file);
355 static int via_dsp_release(struct inode *inode, struct file *file);
356 static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma);
358 static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg);
359 static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value);
360 static u8 via_ac97_wait_idle (struct via_info *card);
362 static void via_chan_free (struct via_info *card, struct via_channel *chan);
363 static void via_chan_clear (struct via_info *card, struct via_channel *chan);
364 static void via_chan_pcm_fmt (struct via_channel *chan, int reset);
365 static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan);
368 /****************************************************************
370 * Various data the driver needs
376 static struct pci_device_id via_pci_tbl[] = {
377 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5,
378 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
379 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_5,
380 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
383 MODULE_DEVICE_TABLE(pci,via_pci_tbl);
386 static struct pci_driver via_driver = {
387 .name = VIA_MODULE_NAME,
388 .id_table = via_pci_tbl,
389 .probe = via_init_one,
390 .remove = __devexit_p(via_remove_one),
394 /****************************************************************
396 * Low-level base 0 register read/write helpers
402 * via_chan_stop - Terminate DMA on specified PCM channel
403 * @iobase: PCI base address for SGD channel registers
405 * Terminate scatter-gather DMA operation for given
406 * channel (derived from @iobase), if DMA is active.
408 * Note that @iobase is not the PCI base address,
409 * but the PCI base address plus an offset to
410 * one of three PCM channels supported by the chip.
414 static inline void via_chan_stop (long iobase)
416 if (inb (iobase + VIA_PCM_STATUS) & VIA_SGD_ACTIVE)
417 outb (VIA_SGD_TERMINATE, iobase + VIA_PCM_CONTROL);
422 * via_chan_status_clear - Clear status flags on specified DMA channel
423 * @iobase: PCI base address for SGD channel registers
425 * Clear any pending status flags for the given
426 * DMA channel (derived from @iobase), if any
427 * flags are asserted.
429 * Note that @iobase is not the PCI base address,
430 * but the PCI base address plus an offset to
431 * one of three PCM channels supported by the chip.
435 static inline void via_chan_status_clear (long iobase)
437 u8 tmp = inb (iobase + VIA_PCM_STATUS);
440 outb (tmp, iobase + VIA_PCM_STATUS);
445 * sg_begin - Begin recording or playback on a PCM channel
446 * @chan: Channel for which DMA operation shall begin
448 * Start scatter-gather DMA for the given channel.
452 static inline void sg_begin (struct via_channel *chan)
454 DPRINTK("Start with intmask %d\n", chan->intmask);
455 DPRINTK("About to start from %d to %d\n",
456 inl(chan->iobase + VIA_PCM_BLOCK_COUNT),
457 inb(chan->iobase + VIA_PCM_STOPRATE + 3));
458 outb (VIA_SGD_START|chan->intmask, chan->iobase + VIA_PCM_CONTROL);
459 DPRINTK("Status is now %02X\n", inb(chan->iobase + VIA_PCM_STATUS));
460 DPRINTK("Control is now %02X\n", inb(chan->iobase + VIA_PCM_CONTROL));
464 static int sg_active (long iobase)
466 u8 tmp = inb (iobase + VIA_PCM_STATUS);
467 if ((tmp & VIA_SGD_STOPPED) || (tmp & VIA_SGD_PAUSED)) {
468 printk(KERN_WARNING "via82cxxx warning: SG stopped or paused\n");
471 if (tmp & VIA_SGD_ACTIVE)
476 static int via_sg_offset(struct via_channel *chan)
478 return inl (chan->iobase + VIA_PCM_BLOCK_COUNT) & 0x00FFFFFF;
481 /****************************************************************
483 * Miscellaneous debris
490 * via_syscall_down - down the card-specific syscell semaphore
491 * @card: Private info for specified board
492 * @nonblock: boolean, non-zero if O_NONBLOCK is set
494 * Encapsulates standard method of acquiring the syscall sem.
496 * Returns negative errno on error, or zero for success.
499 static inline int via_syscall_down (struct via_info *card, int nonblock)
502 * EAGAIN is supposed to be used if IO is pending,
503 * not if there is contention on some internal
504 * synchronization primitive which should be
505 * held only for a short time anyway
510 if (!mutex_trylock(&card->syscall_mutex))
513 if (mutex_lock_interruptible(&card->syscall_mutex))
522 * via_stop_everything - Stop all audio operations
523 * @card: Private info for specified board
525 * Stops all DMA operations and interrupts, and clear
526 * any pending status bits resulting from those operations.
529 static void via_stop_everything (struct via_info *card)
535 assert (card != NULL);
538 * terminate any existing operations on audio read/write channels
540 via_chan_stop (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
541 via_chan_stop (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
542 via_chan_stop (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
544 via_chan_stop (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN);
547 * clear any existing stops / flags (sanity check mainly)
549 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
550 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
551 via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
553 via_chan_status_clear (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN);
556 * clear any enabled interrupt bits
558 tmp = inb (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
559 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
561 outb (0, card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
563 tmp = inb (card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
564 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
566 outb (0, card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
568 tmp = inb (card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
569 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
571 outb (0, card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
575 tmp = inb (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
576 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
578 outb (0, card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
584 * clear any existing flags
586 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
587 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
588 via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
595 * via_set_rate - Set PCM rate for given channel
596 * @ac97: Pointer to generic codec info struct
597 * @chan: Private info for specified channel
598 * @rate: Desired PCM sample rate, in Khz
600 * Sets the PCM sample rate for a channel.
602 * Values for @rate are clamped to a range of 4000 Khz through 48000 Khz,
603 * due to hardware constraints.
606 static int via_set_rate (struct ac97_codec *ac97,
607 struct via_channel *chan, unsigned rate)
609 struct via_info *card = ac97->private_data;
612 u32 mast_vol, phone_vol, mono_vol, pcm_vol;
613 u32 mute_vol = 0x8000; /* The mute volume? -- Seems to work! */
615 DPRINTK ("ENTER, rate = %d\n", rate);
617 if (chan->rate == rate)
619 if (card->locked_rate) {
624 if (rate > 48000) rate = 48000;
625 if (rate < 4000) rate = 4000;
627 rate_reg = chan->is_record ? AC97_PCM_LR_ADC_RATE :
628 AC97_PCM_FRONT_DAC_RATE;
630 /* Save current state */
631 dacp=via_ac97_read_reg(ac97, AC97_POWER_CONTROL);
632 mast_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_STEREO);
633 mono_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_MONO);
634 phone_vol = via_ac97_read_reg(ac97, AC97_HEADPHONE_VOL);
635 pcm_vol = via_ac97_read_reg(ac97, AC97_PCMOUT_VOL);
636 /* Mute - largely reduces popping */
637 via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mute_vol);
638 via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mute_vol);
639 via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, mute_vol);
640 via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, mute_vol);
641 /* Power down the DAC */
642 via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp|0x0200);
645 via_ac97_write_reg (ac97, rate_reg, rate);
647 /* Power DAC back up */
648 via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp);
649 udelay (200); /* reduces popping */
651 /* Restore volumes */
652 via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mast_vol);
653 via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mono_vol);
654 via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, phone_vol);
655 via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, pcm_vol);
657 /* the hardware might return a value different than what we
658 * passed to it, so read the rate value back from hardware
659 * to see what we came up with
661 chan->rate = via_ac97_read_reg (ac97, rate_reg);
663 if (chan->rate == 0) {
664 card->locked_rate = 1;
666 printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n");
670 DPRINTK ("EXIT, returning rate %d Hz\n", chan->rate);
675 /****************************************************************
677 * Channel-specific operations
684 * via_chan_init_defaults - Initialize a struct via_channel
685 * @card: Private audio chip info
686 * @chan: Channel to be initialized
688 * Zero @chan, and then set all static defaults for the structure.
691 static void via_chan_init_defaults (struct via_info *card, struct via_channel *chan)
693 memset (chan, 0, sizeof (*chan));
696 chan->intmask = 0x23; /* Turn on the IRQ bits */
698 if (chan == &card->ch_out) {
699 chan->name = "PCM-OUT";
702 chan->iobase = card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN;
704 DPRINTK("Using multichannel for pcm out\n");
707 chan->iobase = card->baseaddr + VIA_BASE0_PCM_OUT_CHAN;
708 } else if (chan == &card->ch_in) {
709 chan->name = "PCM-IN";
710 chan->iobase = card->baseaddr + VIA_BASE0_PCM_IN_CHAN;
712 } else if (chan == &card->ch_fm) {
713 chan->name = "PCM-OUT-FM";
714 chan->iobase = card->baseaddr + VIA_BASE0_FM_OUT_CHAN;
719 init_waitqueue_head (&chan->wait);
721 chan->pcm_fmt = VIA_PCM_FMT_MASK;
722 chan->is_enabled = 1;
724 chan->frag_number = 0;
726 atomic_set(&chan->n_frags, 0);
727 atomic_set (&chan->hw_ptr, 0);
731 * via_chan_init - Initialize PCM channel
732 * @card: Private audio chip info
733 * @chan: Channel to be initialized
735 * Performs some of the preparations necessary to begin
736 * using a PCM channel.
738 * Currently the preparations consist of
739 * setting the PCM channel to a known state.
743 static void via_chan_init (struct via_info *card, struct via_channel *chan)
748 /* bzero channel structure, and init members to defaults */
749 via_chan_init_defaults (card, chan);
751 /* stop any existing channel output */
752 via_chan_clear (card, chan);
753 via_chan_status_clear (chan->iobase);
754 via_chan_pcm_fmt (chan, 1);
760 * via_chan_buffer_init - Initialize PCM channel buffer
761 * @card: Private audio chip info
762 * @chan: Channel to be initialized
764 * Performs some of the preparations necessary to begin
765 * using a PCM channel.
767 * Currently the preparations include allocating the
768 * scatter-gather DMA table and buffers,
770 * address of the DMA table to the hardware.
772 * Note that special care is taken when passing the
773 * DMA table address to hardware, because it was found
774 * during driver development that the hardware did not
775 * always "take" the address.
778 static int via_chan_buffer_init (struct via_info *card, struct via_channel *chan)
788 chan->intmask = 0x23; /* Turn on the IRQ bits */
790 if (chan->sgtable != NULL) {
795 /* alloc DMA-able memory for scatter-gather table */
796 chan->sgtable = pci_alloc_consistent (card->pdev,
797 (sizeof (struct via_sgd_table) * chan->frag_number),
799 if (!chan->sgtable) {
800 printk (KERN_ERR PFX "DMA table alloc fail, aborting\n");
805 memset ((void*)chan->sgtable, 0,
806 (sizeof (struct via_sgd_table) * chan->frag_number));
808 /* alloc DMA-able memory for scatter-gather buffers */
810 chan->page_number = (chan->frag_number * chan->frag_size) / PAGE_SIZE +
811 (((chan->frag_number * chan->frag_size) % PAGE_SIZE) ? 1 : 0);
813 for (i = 0; i < chan->page_number; i++) {
814 chan->pgtbl[i].cpuaddr = pci_alloc_consistent (card->pdev, PAGE_SIZE,
815 &chan->pgtbl[i].handle);
817 if (!chan->pgtbl[i].cpuaddr) {
818 chan->page_number = i;
823 memset (chan->pgtbl[i].cpuaddr, 0xBC, chan->frag_size);
827 DPRINTK ("dmabuf_pg #%d (h=%lx, v2p=%lx, a=%p)\n",
828 i, (long)chan->pgtbl[i].handle,
829 virt_to_phys(chan->pgtbl[i].cpuaddr),
830 chan->pgtbl[i].cpuaddr);
834 for (i = 0; i < chan->frag_number; i++) {
836 page = i / (PAGE_SIZE / chan->frag_size);
837 offset = (i % (PAGE_SIZE / chan->frag_size)) * chan->frag_size;
839 chan->sgtable[i].count = cpu_to_le32 (chan->frag_size | VIA_FLAG);
840 chan->sgtable[i].addr = cpu_to_le32 (chan->pgtbl[page].handle + offset);
843 DPRINTK ("dmabuf #%d (32(h)=%lx)\n",
845 (long)chan->sgtable[i].addr);
849 /* overwrite the last buffer information */
850 chan->sgtable[chan->frag_number - 1].count = cpu_to_le32 (chan->frag_size | VIA_EOL);
852 /* set location of DMA-able scatter-gather info table */
853 DPRINTK ("outl (0x%X, 0x%04lX)\n",
854 chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR);
856 via_ac97_wait_idle (card);
857 outl (chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR);
859 via_ac97_wait_idle (card);
860 /* load no rate adaption, stereo 16bit, set up ring slots */
865 outl (0xFFFFF | (0x3 << 20) | (chan->frag_number << 24), chan->iobase + VIA_PCM_STOPRATE);
867 via_ac97_wait_idle (card);
871 DPRINTK ("inl (0x%lX) = %x\n",
872 chan->iobase + VIA_PCM_TABLE_ADDR,
873 inl(chan->iobase + VIA_PCM_TABLE_ADDR));
879 printk (KERN_ERR PFX "DMA buffer alloc fail, aborting\n");
880 via_chan_buffer_free (card, chan);
887 * via_chan_free - Release a PCM channel
888 * @card: Private audio chip info
889 * @chan: Channel to be released
891 * Performs all the functions necessary to clean up
892 * an initialized channel.
894 * Currently these functions include disabled any
895 * active DMA operations, setting the PCM channel
896 * back to a known state, and releasing any allocated
900 static void via_chan_free (struct via_info *card, struct via_channel *chan)
904 spin_lock_irq (&card->lock);
906 /* stop any existing channel output */
907 via_chan_status_clear (chan->iobase);
908 via_chan_stop (chan->iobase);
909 via_chan_status_clear (chan->iobase);
911 spin_unlock_irq (&card->lock);
913 synchronize_irq(card->pdev->irq);
918 static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan)
924 /* zero location of DMA-able scatter-gather info table */
925 via_ac97_wait_idle(card);
926 outl (0, chan->iobase + VIA_PCM_TABLE_ADDR);
928 for (i = 0; i < chan->page_number; i++)
929 if (chan->pgtbl[i].cpuaddr) {
930 pci_free_consistent (card->pdev, PAGE_SIZE,
931 chan->pgtbl[i].cpuaddr,
932 chan->pgtbl[i].handle);
933 chan->pgtbl[i].cpuaddr = NULL;
934 chan->pgtbl[i].handle = 0;
937 chan->page_number = 0;
940 pci_free_consistent (card->pdev,
941 (sizeof (struct via_sgd_table) * chan->frag_number),
942 (void*)chan->sgtable, chan->sgt_handle);
943 chan->sgtable = NULL;
951 * via_chan_pcm_fmt - Update PCM channel settings
952 * @chan: Channel to be updated
953 * @reset: Boolean. If non-zero, channel will be reset
954 * to 8-bit mono mode.
956 * Stores the settings of the current PCM format,
957 * 8-bit or 16-bit, and mono/stereo, into the
958 * hardware settings for the specified channel.
959 * If @reset is non-zero, the channel is reset
960 * to 8-bit mono mode. Otherwise, the channel
961 * is set to the values stored in the channel
962 * information struct @chan.
965 static void via_chan_pcm_fmt (struct via_channel *chan, int reset)
967 DPRINTK ("ENTER, pcm_fmt=0x%02X, reset=%s\n",
968 chan->pcm_fmt, reset ? "yes" : "no");
970 assert (chan != NULL);
974 /* reset to 8-bit mono mode */
979 /* enable interrupts on FLAG and EOL */
980 chan->pcm_fmt |= VIA_CHAN_TYPE_MASK;
982 /* if we are recording, enable recording fifo bit */
984 chan->pcm_fmt |= VIA_PCM_REC_FIFO;
985 /* set interrupt select bits where applicable (PCM in & out channels) */
986 if (!chan->is_record)
987 chan->pcm_fmt |= VIA_CHAN_TYPE_INT_SELECT;
989 DPRINTK("SET FMT - %02x %02x\n", chan->intmask , chan->is_multi);
996 * Channel 0x4 is up to 6 x 16bit and has to be
997 * programmed differently
1005 * Load the type bit for num channels
1009 if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
1011 if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
1016 outb(c, chan->iobase + VIA_PCM_TYPE);
1019 * Set the channel steering
1021 * Channel 0 to slot 3
1022 * Channel 0 to slot 4
1024 * Channel 0 to slot 3
1025 * Channel 1 to slot 4
1028 switch(chan->channels)
1031 outl(0xFF000000 | (1<<0) | (1<<4) , chan->iobase + VIA_PCM_STOPRATE);
1034 outl(0xFF000000 | (1<<0) | (2<<4) , chan->iobase + VIA_PCM_STOPRATE);
1037 outl(0xFF000000 | (1<<0) | (2<<4) | (3<<8) | (4<<12), chan->iobase + VIA_PCM_STOPRATE);
1040 outl(0xFF000000 | (1<<0) | (2<<4) | (5<<8) | (6<<12) | (3<<16) | (4<<20), chan->iobase + VIA_PCM_STOPRATE);
1047 * New style, turn off channel volume
1048 * control, set bits in the right register
1050 outb(0x0, chan->iobase + VIA_PCM_LEFTVOL);
1051 outb(0x0, chan->iobase + VIA_PCM_RIGHTVOL);
1053 m = inl(chan->iobase + VIA_PCM_STOPRATE);
1055 if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
1057 if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
1059 outl(m, chan->iobase + VIA_PCM_STOPRATE);
1063 outb (chan->pcm_fmt, chan->iobase + VIA_PCM_TYPE);
1066 DPRINTK ("EXIT, pcm_fmt = 0x%02X, reg = 0x%02X\n",
1068 inb (chan->iobase + VIA_PCM_TYPE));
1073 * via_chan_clear - Stop DMA channel operation, and reset pointers
1074 * @card: the chip to accessed
1075 * @chan: Channel to be cleared
1077 * Call via_chan_stop to halt DMA operations, and then resets
1078 * all software pointers which track DMA operation.
1081 static void via_chan_clear (struct via_info *card, struct via_channel *chan)
1083 DPRINTK ("ENTER\n");
1084 via_chan_stop (chan->iobase);
1085 via_chan_buffer_free(card, chan);
1086 chan->is_active = 0;
1087 chan->is_mapped = 0;
1088 chan->is_enabled = 1;
1092 atomic_set (&chan->hw_ptr, 0);
1098 * via_chan_set_speed - Set PCM sample rate for given channel
1099 * @card: Private info for specified board
1100 * @chan: Channel whose sample rate will be adjusted
1101 * @val: New sample rate, in Khz
1103 * Helper function for the %SNDCTL_DSP_SPEED ioctl. OSS semantics
1104 * demand that all audio operations halt (if they are not already
1105 * halted) when the %SNDCTL_DSP_SPEED is given.
1107 * This function halts all audio operations for the given channel
1108 * @chan, and then calls via_set_rate to set the audio hardware
1112 static int via_chan_set_speed (struct via_info *card,
1113 struct via_channel *chan, int val)
1115 DPRINTK ("ENTER, requested rate = %d\n", val);
1117 via_chan_clear (card, chan);
1119 val = via_set_rate (card->ac97, chan, val);
1121 DPRINTK ("EXIT, returning %d\n", val);
1127 * via_chan_set_fmt - Set PCM sample size for given channel
1128 * @card: Private info for specified board
1129 * @chan: Channel whose sample size will be adjusted
1130 * @val: New sample size, use the %AFMT_xxx constants
1132 * Helper function for the %SNDCTL_DSP_SETFMT ioctl. OSS semantics
1133 * demand that all audio operations halt (if they are not already
1134 * halted) when the %SNDCTL_DSP_SETFMT is given.
1136 * This function halts all audio operations for the given channel
1137 * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
1138 * to the new sample size, either 8-bit or 16-bit.
1141 static int via_chan_set_fmt (struct via_info *card,
1142 struct via_channel *chan, int val)
1144 DPRINTK ("ENTER, val=%s\n",
1145 val == AFMT_U8 ? "AFMT_U8" :
1146 val == AFMT_S16_LE ? "AFMT_S16_LE" :
1149 via_chan_clear (card, chan);
1151 assert (val != AFMT_QUERY); /* this case is handled elsewhere */
1155 if ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) == 0) {
1156 chan->pcm_fmt |= VIA_PCM_FMT_16BIT;
1157 via_chan_pcm_fmt (chan, 0);
1162 if (chan->pcm_fmt & VIA_PCM_FMT_16BIT) {
1163 chan->pcm_fmt &= ~VIA_PCM_FMT_16BIT;
1164 via_chan_pcm_fmt (chan, 0);
1169 DPRINTK ("unknown AFMT: 0x%X\n", val);
1179 * via_chan_set_stereo - Enable or disable stereo for a DMA channel
1180 * @card: Private info for specified board
1181 * @chan: Channel whose stereo setting will be adjusted
1182 * @val: New sample size, use the %AFMT_xxx constants
1184 * Helper function for the %SNDCTL_DSP_CHANNELS and %SNDCTL_DSP_STEREO ioctls. OSS semantics
1185 * demand that all audio operations halt (if they are not already
1186 * halted) when %SNDCTL_DSP_CHANNELS or SNDCTL_DSP_STEREO is given.
1188 * This function halts all audio operations for the given channel
1189 * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
1190 * to enable or disable stereo.
1193 static int via_chan_set_stereo (struct via_info *card,
1194 struct via_channel *chan, int val)
1196 DPRINTK ("ENTER, channels = %d\n", val);
1198 via_chan_clear (card, chan);
1204 chan->pcm_fmt &= ~VIA_PCM_FMT_STEREO;
1206 via_chan_pcm_fmt (chan, 0);
1211 chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
1213 via_chan_pcm_fmt (chan, 0);
1220 chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
1221 chan->channels = val;
1230 DPRINTK ("EXIT, returning %d\n", val);
1234 static int via_chan_set_buffering (struct via_info *card,
1235 struct via_channel *chan, int val)
1239 DPRINTK ("ENTER\n");
1241 /* in both cases the buffer cannot be changed */
1242 if (chan->is_active || chan->is_mapped) {
1247 /* called outside SETFRAGMENT */
1248 /* set defaults or do nothing */
1251 if (chan->frag_size && chan->frag_number)
1256 chan->frag_size = (VIA_DEFAULT_FRAG_TIME * chan->rate * chan->channels
1257 * ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) ? 2 : 1)) / 1000 - 1;
1260 while (chan->frag_size) {
1261 chan->frag_size >>= 1;
1264 chan->frag_size = 1 << shift;
1266 chan->frag_number = (VIA_DEFAULT_BUFFER_TIME / VIA_DEFAULT_FRAG_TIME);
1268 DPRINTK ("setting default values %d %d\n", chan->frag_size, chan->frag_number);
1270 chan->frag_size = 1 << (val & 0xFFFF);
1271 chan->frag_number = (val >> 16) & 0xFFFF;
1273 DPRINTK ("using user values %d %d\n", chan->frag_size, chan->frag_number);
1276 /* quake3 wants frag_number to be a power of two */
1278 while (chan->frag_number) {
1279 chan->frag_number >>= 1;
1282 chan->frag_number = 1 << shift;
1284 if (chan->frag_size > VIA_MAX_FRAG_SIZE)
1285 chan->frag_size = VIA_MAX_FRAG_SIZE;
1286 else if (chan->frag_size < VIA_MIN_FRAG_SIZE)
1287 chan->frag_size = VIA_MIN_FRAG_SIZE;
1289 if (chan->frag_number < VIA_MIN_FRAG_NUMBER)
1290 chan->frag_number = VIA_MIN_FRAG_NUMBER;
1291 if (chan->frag_number > VIA_MAX_FRAG_NUMBER)
1292 chan->frag_number = VIA_MAX_FRAG_NUMBER;
1294 if ((chan->frag_number * chan->frag_size) / PAGE_SIZE > VIA_MAX_BUFFER_DMA_PAGES)
1295 chan->frag_number = (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE) / chan->frag_size;
1298 if (chan->is_record)
1299 atomic_set (&chan->n_frags, 0);
1301 atomic_set (&chan->n_frags, chan->frag_number);
1308 #ifdef VIA_CHAN_DUMP_BUFS
1310 * via_chan_dump_bufs - Display DMA table contents
1311 * @chan: Channel whose DMA table will be displayed
1313 * Debugging function which displays the contents of the
1314 * scatter-gather DMA table for the given channel @chan.
1317 static void via_chan_dump_bufs (struct via_channel *chan)
1321 for (i = 0; i < chan->frag_number; i++) {
1322 DPRINTK ("#%02d: addr=%x, count=%u, flag=%d, eol=%d\n",
1323 i, chan->sgtable[i].addr,
1324 chan->sgtable[i].count & 0x00FFFFFF,
1325 chan->sgtable[i].count & VIA_FLAG ? 1 : 0,
1326 chan->sgtable[i].count & VIA_EOL ? 1 : 0);
1328 DPRINTK ("buf_in_use = %d, nextbuf = %d\n",
1329 atomic_read (&chan->buf_in_use),
1330 atomic_read (&chan->sw_ptr));
1332 #endif /* VIA_CHAN_DUMP_BUFS */
1336 * via_chan_flush_frag - Flush partially-full playback buffer to hardware
1337 * @chan: Channel whose DMA table will be flushed
1339 * Flushes partially-full playback buffer to hardware.
1342 static void via_chan_flush_frag (struct via_channel *chan)
1344 DPRINTK ("ENTER\n");
1346 assert (chan->slop_len > 0);
1348 if (chan->sw_ptr == (chan->frag_number - 1))
1355 assert (atomic_read (&chan->n_frags) > 0);
1356 atomic_dec (&chan->n_frags);
1364 * via_chan_maybe_start - Initiate audio hardware DMA operation
1365 * @chan: Channel whose DMA is to be started
1367 * Initiate DMA operation, if the DMA engine for the given
1368 * channel @chan is not already active.
1371 static inline void via_chan_maybe_start (struct via_channel *chan)
1373 assert (chan->is_active == sg_active(chan->iobase));
1375 DPRINTK ("MAYBE START %s\n", chan->name);
1376 if (!chan->is_active && chan->is_enabled) {
1377 chan->is_active = 1;
1379 DPRINTK ("starting channel %s\n", chan->name);
1384 /****************************************************************
1386 * Interface to ac97-codec module
1392 * via_ac97_wait_idle - Wait until AC97 codec is not busy
1393 * @card: Private info for specified board
1395 * Sleep until the AC97 codec is no longer busy.
1396 * Returns the final value read from the SGD
1397 * register being polled.
1400 static u8 via_ac97_wait_idle (struct via_info *card)
1403 int counter = VIA_COUNTER_LIMIT;
1405 DPRINTK ("ENTER/EXIT\n");
1407 assert (card != NULL);
1408 assert (card->pdev != NULL);
1413 tmp8 = inb (card->baseaddr + 0x83);
1414 } while ((tmp8 & VIA_CR83_BUSY) && (counter-- > 0));
1416 if (tmp8 & VIA_CR83_BUSY)
1417 printk (KERN_WARNING PFX "timeout waiting on AC97 codec\n");
1423 * via_ac97_read_reg - Read AC97 standard register
1424 * @codec: Pointer to generic AC97 codec info
1425 * @reg: Index of AC97 register to be read
1427 * Read the value of a single AC97 codec register,
1428 * as defined by the Intel AC97 specification.
1430 * Defines the standard AC97 read-register operation
1431 * required by the kernel's ac97_codec interface.
1433 * Returns the 16-bit value stored in the specified
1437 static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg)
1440 struct via_info *card;
1443 DPRINTK ("ENTER\n");
1445 assert (codec != NULL);
1446 assert (codec->private_data != NULL);
1448 card = codec->private_data;
1450 spin_lock(&card->ac97_lock);
1452 /* Every time we write to register 80 we cause a transaction.
1453 The only safe way to clear the valid bit is to write it at
1454 the same time as the command */
1455 data = (reg << 16) | VIA_CR80_READ | VIA_CR80_VALID;
1457 outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
1460 for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
1462 if ((((data = inl(card->baseaddr + VIA_BASE0_AC97_CTRL)) &
1463 (VIA_CR80_VALID|VIA_CR80_BUSY)) == VIA_CR80_VALID))
1467 printk (KERN_WARNING PFX "timeout while reading AC97 codec (0x%lX)\n", data);
1471 /* Once the valid bit has become set, we must wait a complete AC97
1472 frame before the data has settled. */
1474 data = (unsigned long) inl (card->baseaddr + VIA_BASE0_AC97_CTRL);
1476 outb (0x02, card->baseaddr + 0x83);
1478 if (((data & 0x007F0000) >> 16) == reg) {
1479 DPRINTK ("EXIT, success, data=0x%lx, retval=0x%lx\n",
1480 data, data & 0x0000FFFF);
1481 spin_unlock(&card->ac97_lock);
1482 return data & 0x0000FFFF;
1485 printk (KERN_WARNING "via82cxxx_audio: not our index: reg=0x%x, newreg=0x%lx\n",
1486 reg, ((data & 0x007F0000) >> 16));
1489 spin_unlock(&card->ac97_lock);
1490 DPRINTK ("EXIT, returning 0\n");
1496 * via_ac97_write_reg - Write AC97 standard register
1497 * @codec: Pointer to generic AC97 codec info
1498 * @reg: Index of AC97 register to be written
1499 * @value: Value to be written to AC97 register
1501 * Write the value of a single AC97 codec register,
1502 * as defined by the Intel AC97 specification.
1504 * Defines the standard AC97 write-register operation
1505 * required by the kernel's ac97_codec interface.
1508 static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value)
1511 struct via_info *card;
1514 DPRINTK ("ENTER\n");
1516 assert (codec != NULL);
1517 assert (codec->private_data != NULL);
1519 card = codec->private_data;
1521 spin_lock(&card->ac97_lock);
1523 data = (reg << 16) + value;
1524 outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
1527 for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
1528 if ((inb (card->baseaddr + 0x83) & VIA_CR83_BUSY) == 0)
1534 printk (KERN_WARNING PFX "timeout after AC97 codec write (0x%X, 0x%X)\n", reg, value);
1537 spin_unlock(&card->ac97_lock);
1542 static int via_mixer_open (struct inode *inode, struct file *file)
1544 int minor = iminor(inode);
1545 struct via_info *card;
1546 struct pci_dev *pdev = NULL;
1547 struct pci_driver *drvr;
1549 DPRINTK ("ENTER\n");
1551 while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
1552 drvr = pci_dev_driver (pdev);
1553 if (drvr == &via_driver) {
1554 assert (pci_get_drvdata (pdev) != NULL);
1556 card = pci_get_drvdata (pdev);
1557 if (card->ac97->dev_mixer == minor)
1562 DPRINTK ("EXIT, returning -ENODEV\n");
1566 file->private_data = card->ac97;
1568 DPRINTK ("EXIT, returning 0\n");
1569 return nonseekable_open(inode, file);
1572 static int via_mixer_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
1575 struct ac97_codec *codec = file->private_data;
1576 struct via_info *card;
1577 int nonblock = (file->f_flags & O_NONBLOCK);
1580 DPRINTK ("ENTER\n");
1582 assert (codec != NULL);
1583 card = codec->private_data;
1584 assert (card != NULL);
1586 rc = via_syscall_down (card, nonblock);
1591 * Intercept volume control on 8233 and 8235
1597 case SOUND_MIXER_READ_VOLUME:
1598 return card->mixer_vol;
1599 case SOUND_MIXER_WRITE_VOLUME:
1602 if(get_user(v, (int *)arg))
1607 card->mixer_vol = v;
1612 rc = codec->mixer_ioctl(codec, cmd, arg);
1614 mutex_unlock(&card->syscall_mutex);
1617 DPRINTK ("EXIT, returning %d\n", rc);
1622 static struct file_operations via_mixer_fops = {
1623 .owner = THIS_MODULE,
1624 .open = via_mixer_open,
1625 .llseek = no_llseek,
1626 .ioctl = via_mixer_ioctl,
1630 static int __devinit via_ac97_reset (struct via_info *card)
1632 struct pci_dev *pdev = card->pdev;
1636 DPRINTK ("ENTER\n");
1638 assert (pdev != NULL);
1642 u8 r40,r41,r42,r43,r44,r48;
1643 pci_read_config_byte (card->pdev, 0x40, &r40);
1644 pci_read_config_byte (card->pdev, 0x41, &r41);
1645 pci_read_config_byte (card->pdev, 0x42, &r42);
1646 pci_read_config_byte (card->pdev, 0x43, &r43);
1647 pci_read_config_byte (card->pdev, 0x44, &r44);
1648 pci_read_config_byte (card->pdev, 0x48, &r48);
1649 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
1650 r40,r41,r42,r43,r44,r48);
1652 spin_lock_irq (&card->lock);
1653 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
1654 inb (card->baseaddr + 0x00),
1655 inb (card->baseaddr + 0x01),
1656 inb (card->baseaddr + 0x02),
1657 inl (card->baseaddr + 0x04),
1658 inl (card->baseaddr + 0x0C),
1659 inl (card->baseaddr + 0x80),
1660 inl (card->baseaddr + 0x84));
1661 spin_unlock_irq (&card->lock);
1667 * Reset AC97 controller: enable, disable, enable,
1668 * pausing after each command for good luck. Only
1669 * do this if the codec is not ready, because it causes
1670 * loud pops and such due to such a hard codec reset.
1672 pci_read_config_byte (pdev, VIA_ACLINK_STATUS, &tmp8);
1673 if ((tmp8 & VIA_CR40_AC97_READY) == 0) {
1674 pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
1675 VIA_CR41_AC97_ENABLE |
1676 VIA_CR41_AC97_RESET |
1677 VIA_CR41_AC97_WAKEUP);
1680 pci_write_config_byte (pdev, VIA_ACLINK_CTRL, 0);
1683 pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
1684 VIA_CR41_AC97_ENABLE |
1685 VIA_CR41_PCM_ENABLE |
1686 VIA_CR41_VRA | VIA_CR41_AC97_RESET);
1690 /* Make sure VRA is enabled, in case we didn't do a
1691 * complete codec reset, above
1693 pci_read_config_byte (pdev, VIA_ACLINK_CTRL, &tmp8);
1694 if (((tmp8 & VIA_CR41_VRA) == 0) ||
1695 ((tmp8 & VIA_CR41_AC97_ENABLE) == 0) ||
1696 ((tmp8 & VIA_CR41_PCM_ENABLE) == 0) ||
1697 ((tmp8 & VIA_CR41_AC97_RESET) == 0)) {
1698 pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
1699 VIA_CR41_AC97_ENABLE |
1700 VIA_CR41_PCM_ENABLE |
1701 VIA_CR41_VRA | VIA_CR41_AC97_RESET);
1707 #if 0 /* this breaks on K7M */
1708 /* disable legacy stuff */
1709 pci_write_config_byte (pdev, 0x42, 0x00);
1713 /* route FM trap to IRQ, disable FM trap */
1714 pci_write_config_byte (pdev, 0x48, 0x05);
1718 /* disable all codec GPI interrupts */
1719 outl (0, pci_resource_start (pdev, 0) + 0x8C);
1721 /* WARNING: this line is magic. Remove this
1722 * and things break. */
1723 /* enable variable rate */
1724 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1725 if ((tmp16 & 1) == 0)
1726 via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1728 DPRINTK ("EXIT, returning 0\n");
1733 static void via_ac97_codec_wait (struct ac97_codec *codec)
1735 assert (codec->private_data != NULL);
1736 via_ac97_wait_idle (codec->private_data);
1740 static int __devinit via_ac97_init (struct via_info *card)
1745 DPRINTK ("ENTER\n");
1747 assert (card != NULL);
1749 card->ac97 = ac97_alloc_codec();
1750 if(card->ac97 == NULL)
1753 card->ac97->private_data = card;
1754 card->ac97->codec_read = via_ac97_read_reg;
1755 card->ac97->codec_write = via_ac97_write_reg;
1756 card->ac97->codec_wait = via_ac97_codec_wait;
1758 card->ac97->dev_mixer = register_sound_mixer (&via_mixer_fops, -1);
1759 if (card->ac97->dev_mixer < 0) {
1760 printk (KERN_ERR PFX "unable to register AC97 mixer, aborting\n");
1761 DPRINTK ("EXIT, returning -EIO\n");
1762 ac97_release_codec(card->ac97);
1766 rc = via_ac97_reset (card);
1768 printk (KERN_ERR PFX "unable to reset AC97 codec, aborting\n");
1774 if (ac97_probe_codec (card->ac97) == 0) {
1775 printk (KERN_ERR PFX "unable to probe AC97 codec, aborting\n");
1780 /* enable variable rate */
1781 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1782 via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1785 * If we cannot enable VRA, we have a locked-rate codec.
1786 * We try again to enable VRA before assuming so, however.
1788 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1789 if ((tmp16 & 1) == 0) {
1790 via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1791 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1792 if ((tmp16 & 1) == 0) {
1793 card->locked_rate = 1;
1794 printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n");
1798 DPRINTK ("EXIT, returning 0\n");
1802 unregister_sound_mixer (card->ac97->dev_mixer);
1803 DPRINTK ("EXIT, returning %d\n", rc);
1804 ac97_release_codec(card->ac97);
1809 static void via_ac97_cleanup (struct via_info *card)
1811 DPRINTK ("ENTER\n");
1813 assert (card != NULL);
1814 assert (card->ac97->dev_mixer >= 0);
1816 unregister_sound_mixer (card->ac97->dev_mixer);
1817 ac97_release_codec(card->ac97);
1824 /****************************************************************
1826 * Interrupt-related code
1831 * via_intr_channel - handle an interrupt for a single channel
1833 * @chan: handle interrupt for this channel
1835 * This is the "meat" of the interrupt handler,
1836 * containing the actions taken each time an interrupt
1837 * occurs. All communication and coordination with
1838 * userspace takes place here.
1840 * Locking: inside card->lock
1843 static void via_intr_channel (struct via_info *card, struct via_channel *chan)
1848 /* check pertinent bits of status register for action bits */
1849 status = inb (chan->iobase) & (VIA_SGD_FLAG | VIA_SGD_EOL | VIA_SGD_STOPPED);
1853 /* acknowledge any flagged bits ASAP */
1854 outb (status, chan->iobase);
1856 if (!chan->sgtable) /* XXX: temporary solution */
1859 /* grab current h/w ptr value */
1860 n = atomic_read (&chan->hw_ptr);
1862 /* sanity check: make sure our h/w ptr doesn't have a weird value */
1864 assert (n < chan->frag_number);
1867 /* reset SGD data structure in memory to reflect a full buffer,
1868 * and advance the h/w ptr, wrapping around to zero if needed
1870 if (n == (chan->frag_number - 1)) {
1871 chan->sgtable[n].count = cpu_to_le32(chan->frag_size | VIA_EOL);
1872 atomic_set (&chan->hw_ptr, 0);
1874 chan->sgtable[n].count = cpu_to_le32(chan->frag_size | VIA_FLAG);
1875 atomic_inc (&chan->hw_ptr);
1878 /* accounting crap for SNDCTL_DSP_GETxPTR */
1880 chan->bytes += chan->frag_size;
1881 /* FIXME - signed overflow is undefined */
1882 if (chan->bytes < 0) /* handle overflow of 31-bit value */
1883 chan->bytes = chan->frag_size;
1884 /* all following checks only occur when not in mmap(2) mode */
1885 if (!chan->is_mapped)
1887 /* If we are recording, then n_frags represents the number
1888 * of fragments waiting to be handled by userspace.
1889 * If we are playback, then n_frags represents the number
1890 * of fragments remaining to be filled by userspace.
1891 * We increment here. If we reach max number of fragments,
1892 * this indicates an underrun/overrun. For this case under OSS,
1893 * we stop the record/playback process.
1895 if (atomic_read (&chan->n_frags) < chan->frag_number)
1896 atomic_inc (&chan->n_frags);
1897 assert (atomic_read (&chan->n_frags) <= chan->frag_number);
1898 if (atomic_read (&chan->n_frags) == chan->frag_number) {
1899 chan->is_active = 0;
1900 via_chan_stop (chan->iobase);
1903 /* wake up anyone listening to see when interrupts occur */
1904 wake_up_all (&chan->wait);
1906 DPRINTK ("%s intr, status=0x%02X, hwptr=0x%lX, chan->hw_ptr=%d\n",
1907 chan->name, status, (long) inl (chan->iobase + 0x04),
1908 atomic_read (&chan->hw_ptr));
1910 DPRINTK ("%s intr, channel n_frags == %d, missed %d\n", chan->name,
1911 atomic_read (&chan->n_frags), missed);
1915 static irqreturn_t via_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1917 struct via_info *card = dev_id;
1920 /* to minimize interrupt sharing costs, we use the SGD status
1921 * shadow register to check the status of all inputs and
1922 * outputs with a single 32-bit bus read. If no interrupt
1923 * conditions are flagged, we exit immediately
1925 status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW);
1926 if (!(status32 & VIA_INTR_MASK))
1928 #ifdef CONFIG_MIDI_VIA82CXXX
1929 if (card->midi_devc)
1930 uart401intr(irq, card->midi_devc, regs);
1934 DPRINTK ("intr, status32 == 0x%08X\n", status32);
1936 /* synchronize interrupt handling under SMP. this spinlock
1937 * goes away completely on UP
1939 spin_lock (&card->lock);
1941 if (status32 & VIA_INTR_OUT)
1942 via_intr_channel (card, &card->ch_out);
1943 if (status32 & VIA_INTR_IN)
1944 via_intr_channel (card, &card->ch_in);
1945 if (status32 & VIA_INTR_FM)
1946 via_intr_channel (card, &card->ch_fm);
1948 spin_unlock (&card->lock);
1953 static irqreturn_t via_new_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1955 struct via_info *card = dev_id;
1958 /* to minimize interrupt sharing costs, we use the SGD status
1959 * shadow register to check the status of all inputs and
1960 * outputs with a single 32-bit bus read. If no interrupt
1961 * conditions are flagged, we exit immediately
1963 status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW);
1964 if (!(status32 & VIA_NEW_INTR_MASK))
1967 * goes away completely on UP
1969 spin_lock (&card->lock);
1971 via_intr_channel (card, &card->ch_out);
1972 via_intr_channel (card, &card->ch_in);
1973 via_intr_channel (card, &card->ch_fm);
1975 spin_unlock (&card->lock);
1981 * via_interrupt_init - Initialize interrupt handling
1982 * @card: Private info for specified board
1984 * Obtain and reserve IRQ for using in handling audio events.
1985 * Also, disable any IRQ-generating resources, to make sure
1986 * we don't get interrupts before we want them.
1989 static int via_interrupt_init (struct via_info *card)
1993 DPRINTK ("ENTER\n");
1995 assert (card != NULL);
1996 assert (card->pdev != NULL);
1998 /* check for sane IRQ number. can this ever happen? */
1999 if (card->pdev->irq < 2) {
2000 printk (KERN_ERR PFX "insane IRQ %d, aborting\n",
2002 DPRINTK ("EXIT, returning -EIO\n");
2006 /* VIA requires this is done */
2007 pci_write_config_byte(card->pdev, PCI_INTERRUPT_LINE, card->pdev->irq);
2011 /* make sure FM irq is not routed to us */
2012 pci_read_config_byte (card->pdev, VIA_FM_NMI_CTRL, &tmp8);
2013 if ((tmp8 & VIA_CR48_FM_TRAP_TO_NMI) == 0) {
2014 tmp8 |= VIA_CR48_FM_TRAP_TO_NMI;
2015 pci_write_config_byte (card->pdev, VIA_FM_NMI_CTRL, tmp8);
2017 if (request_irq (card->pdev->irq, via_interrupt, SA_SHIRQ, VIA_MODULE_NAME, card)) {
2018 printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n",
2020 DPRINTK ("EXIT, returning -EBUSY\n");
2026 if (request_irq (card->pdev->irq, via_new_interrupt, SA_SHIRQ, VIA_MODULE_NAME, card)) {
2027 printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n",
2029 DPRINTK ("EXIT, returning -EBUSY\n");
2034 DPRINTK ("EXIT, returning 0\n");
2039 /****************************************************************
2045 static struct file_operations via_dsp_fops = {
2046 .owner = THIS_MODULE,
2047 .open = via_dsp_open,
2048 .release = via_dsp_release,
2049 .read = via_dsp_read,
2050 .write = via_dsp_write,
2051 .poll = via_dsp_poll,
2052 .llseek = no_llseek,
2053 .ioctl = via_dsp_ioctl,
2054 .mmap = via_dsp_mmap,
2058 static int __devinit via_dsp_init (struct via_info *card)
2062 DPRINTK ("ENTER\n");
2064 assert (card != NULL);
2068 /* turn off legacy features, if not already */
2069 pci_read_config_byte (card->pdev, VIA_FUNC_ENABLE, &tmp8);
2070 if (tmp8 & (VIA_CR42_SB_ENABLE | VIA_CR42_FM_ENABLE)) {
2071 tmp8 &= ~(VIA_CR42_SB_ENABLE | VIA_CR42_FM_ENABLE);
2072 pci_write_config_byte (card->pdev, VIA_FUNC_ENABLE, tmp8);
2076 via_stop_everything (card);
2078 card->dev_dsp = register_sound_dsp (&via_dsp_fops, -1);
2079 if (card->dev_dsp < 0) {
2080 DPRINTK ("EXIT, returning -ENODEV\n");
2083 DPRINTK ("EXIT, returning 0\n");
2088 static void via_dsp_cleanup (struct via_info *card)
2090 DPRINTK ("ENTER\n");
2092 assert (card != NULL);
2093 assert (card->dev_dsp >= 0);
2095 via_stop_everything (card);
2097 unregister_sound_dsp (card->dev_dsp);
2103 static struct page * via_mm_nopage (struct vm_area_struct * vma,
2104 unsigned long address, int *type)
2106 struct via_info *card = vma->vm_private_data;
2107 struct via_channel *chan = &card->ch_out;
2108 struct page *dmapage;
2109 unsigned long pgoff;
2112 DPRINTK ("ENTER, start %lXh, ofs %lXh, pgoff %ld, addr %lXh\n",
2114 address - vma->vm_start,
2115 (address - vma->vm_start) >> PAGE_SHIFT,
2118 if (address > vma->vm_end) {
2119 DPRINTK ("EXIT, returning NOPAGE_SIGBUS\n");
2120 return NOPAGE_SIGBUS; /* Disallow mremap */
2123 DPRINTK ("EXIT, returning NOPAGE_OOM\n");
2124 return NOPAGE_OOM; /* Nothing allocated */
2127 pgoff = vma->vm_pgoff + ((address - vma->vm_start) >> PAGE_SHIFT);
2128 rd = card->ch_in.is_mapped;
2129 wr = card->ch_out.is_mapped;
2133 unsigned long max_bufs = chan->frag_number;
2134 if (rd && wr) max_bufs *= 2;
2135 /* via_dsp_mmap() should ensure this */
2136 assert (pgoff < max_bufs);
2140 /* if full-duplex (read+write) and we have two sets of bufs,
2141 * then the playback buffers come first, sez soundcard.c */
2142 if (pgoff >= chan->page_number) {
2143 pgoff -= chan->page_number;
2144 chan = &card->ch_in;
2146 chan = &card->ch_in;
2148 assert ((((unsigned long)chan->pgtbl[pgoff].cpuaddr) % PAGE_SIZE) == 0);
2150 dmapage = virt_to_page (chan->pgtbl[pgoff].cpuaddr);
2151 DPRINTK ("EXIT, returning page %p for cpuaddr %lXh\n",
2152 dmapage, (unsigned long) chan->pgtbl[pgoff].cpuaddr);
2155 *type = VM_FAULT_MINOR;
2161 static int via_mm_swapout (struct page *page, struct file *filp)
2165 #endif /* VM_RESERVED */
2168 static struct vm_operations_struct via_mm_ops = {
2169 .nopage = via_mm_nopage,
2172 .swapout = via_mm_swapout,
2177 static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma)
2179 struct via_info *card;
2180 int nonblock = (file->f_flags & O_NONBLOCK);
2181 int rc = -EINVAL, rd=0, wr=0;
2182 unsigned long max_size, size, start, offset;
2184 assert (file != NULL);
2185 assert (vma != NULL);
2186 card = file->private_data;
2187 assert (card != NULL);
2189 DPRINTK ("ENTER, start %lXh, size %ld, pgoff %ld\n",
2191 vma->vm_end - vma->vm_start,
2195 if (vma->vm_flags & VM_READ) {
2197 via_chan_set_buffering(card, &card->ch_in, -1);
2198 via_chan_buffer_init (card, &card->ch_in);
2199 max_size += card->ch_in.page_number << PAGE_SHIFT;
2201 if (vma->vm_flags & VM_WRITE) {
2203 via_chan_set_buffering(card, &card->ch_out, -1);
2204 via_chan_buffer_init (card, &card->ch_out);
2205 max_size += card->ch_out.page_number << PAGE_SHIFT;
2208 start = vma->vm_start;
2209 offset = (vma->vm_pgoff << PAGE_SHIFT);
2210 size = vma->vm_end - vma->vm_start;
2212 /* some basic size/offset sanity checks */
2213 if (size > max_size)
2215 if (offset > max_size - size)
2218 rc = via_syscall_down (card, nonblock);
2221 vma->vm_ops = &via_mm_ops;
2222 vma->vm_private_data = card;
2225 vma->vm_flags |= VM_RESERVED;
2229 card->ch_in.is_mapped = 1;
2231 card->ch_out.is_mapped = 1;
2233 mutex_unlock(&card->syscall_mutex);
2237 DPRINTK ("EXIT, returning %d\n", rc);
2242 static ssize_t via_dsp_do_read (struct via_info *card,
2243 char __user *userbuf, size_t count,
2246 DECLARE_WAITQUEUE(wait, current);
2247 const char __user *orig_userbuf = userbuf;
2248 struct via_channel *chan = &card->ch_in;
2253 /* if SGD has not yet been started, start it */
2254 via_chan_maybe_start (chan);
2257 /* just to be a nice neighbor */
2259 * But also to ourselves, release semaphore if we do so */
2260 if (need_resched()) {
2261 mutex_unlock(&card->syscall_mutex);
2263 ret = via_syscall_down (card, nonblock);
2268 /* grab current channel software pointer. In the case of
2269 * recording, this is pointing to the next buffer that
2270 * will receive data from the audio hardware.
2274 /* n_frags represents the number of fragments waiting
2275 * to be copied to userland. sleep until at least
2276 * one buffer has been read from the audio hardware.
2278 add_wait_queue(&chan->wait, &wait);
2280 __set_current_state(TASK_INTERRUPTIBLE);
2281 tmp = atomic_read (&chan->n_frags);
2283 assert (tmp <= chan->frag_number);
2286 if (nonblock || !chan->is_active) {
2291 mutex_unlock(&card->syscall_mutex);
2293 DPRINTK ("Sleeping on block %d\n", n);
2296 ret = via_syscall_down (card, nonblock);
2300 if (signal_pending (current)) {
2305 set_current_state(TASK_RUNNING);
2306 remove_wait_queue(&chan->wait, &wait);
2310 /* Now that we have a buffer we can read from, send
2311 * as much as sample data possible to userspace.
2313 while ((count > 0) && (chan->slop_len < chan->frag_size)) {
2314 size_t slop_left = chan->frag_size - chan->slop_len;
2315 void *base = chan->pgtbl[n / (PAGE_SIZE / chan->frag_size)].cpuaddr;
2316 unsigned ofs = (n % (PAGE_SIZE / chan->frag_size)) * chan->frag_size;
2318 size = (count < slop_left) ? count : slop_left;
2319 if (copy_to_user (userbuf,
2320 base + ofs + chan->slop_len,
2327 chan->slop_len += size;
2331 /* If we didn't copy the buffer completely to userspace,
2334 if (chan->slop_len < chan->frag_size)
2338 * If we get to this point, we copied one buffer completely
2339 * to userspace, give the buffer back to the hardware.
2342 /* advance channel software pointer to point to
2343 * the next buffer from which we will copy
2345 if (chan->sw_ptr == (chan->frag_number - 1))
2350 /* mark one less buffer waiting to be processed */
2351 assert (atomic_read (&chan->n_frags) > 0);
2352 atomic_dec (&chan->n_frags);
2354 /* we are at a block boundary, there is no fragment data */
2357 DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
2358 n, chan->sw_ptr, atomic_read (&chan->n_frags));
2360 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2361 inb (card->baseaddr + 0x00),
2362 inb (card->baseaddr + 0x01),
2363 inb (card->baseaddr + 0x02),
2364 inl (card->baseaddr + 0x04),
2365 inl (card->baseaddr + 0x0C),
2366 inl (card->baseaddr + 0x80),
2367 inl (card->baseaddr + 0x84));
2370 goto handle_one_block;
2373 return (userbuf != orig_userbuf) ? (userbuf - orig_userbuf) : ret;
2377 static ssize_t via_dsp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
2379 struct via_info *card;
2380 int nonblock = (file->f_flags & O_NONBLOCK);
2383 DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
2384 file, buffer, count, ppos ? ((unsigned long)*ppos) : 0);
2386 assert (file != NULL);
2387 card = file->private_data;
2388 assert (card != NULL);
2390 rc = via_syscall_down (card, nonblock);
2393 if (card->ch_in.is_mapped) {
2398 via_chan_set_buffering(card, &card->ch_in, -1);
2399 rc = via_chan_buffer_init (card, &card->ch_in);
2404 rc = via_dsp_do_read (card, buffer, count, nonblock);
2407 mutex_unlock(&card->syscall_mutex);
2409 DPRINTK ("EXIT, returning %ld\n",(long) rc);
2414 static ssize_t via_dsp_do_write (struct via_info *card,
2415 const char __user *userbuf, size_t count,
2418 DECLARE_WAITQUEUE(wait, current);
2419 const char __user *orig_userbuf = userbuf;
2420 struct via_channel *chan = &card->ch_out;
2421 volatile struct via_sgd_table *sgtable = chan->sgtable;
2427 /* just to be a nice neighbor */
2429 * But also to ourselves, release semaphore if we do so */
2430 if (need_resched()) {
2431 mutex_unlock(&card->syscall_mutex);
2433 ret = via_syscall_down (card, nonblock);
2438 /* grab current channel fragment pointer. In the case of
2439 * playback, this is pointing to the next fragment that
2440 * should receive data from userland.
2444 /* n_frags represents the number of fragments remaining
2445 * to be filled by userspace. Sleep until
2446 * at least one fragment is available for our use.
2448 add_wait_queue(&chan->wait, &wait);
2450 __set_current_state(TASK_INTERRUPTIBLE);
2451 tmp = atomic_read (&chan->n_frags);
2453 assert (tmp <= chan->frag_number);
2456 if (nonblock || !chan->is_active) {
2461 mutex_unlock(&card->syscall_mutex);
2463 DPRINTK ("Sleeping on page %d, tmp==%d, ir==%d\n", n, tmp, chan->is_record);
2466 ret = via_syscall_down (card, nonblock);
2470 if (signal_pending (current)) {
2475 set_current_state(TASK_RUNNING);
2476 remove_wait_queue(&chan->wait, &wait);
2480 /* Now that we have at least one fragment we can write to, fill the buffer
2481 * as much as possible with data from userspace.
2483 while ((count > 0) && (chan->slop_len < chan->frag_size)) {
2484 size_t slop_left = chan->frag_size - chan->slop_len;
2486 size = (count < slop_left) ? count : slop_left;
2487 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,
2494 chan->slop_len += size;
2498 /* If we didn't fill up the buffer with data, stop now.
2499 * Put a 'stop' marker in the DMA table too, to tell the
2500 * audio hardware to stop if it gets here.
2502 if (chan->slop_len < chan->frag_size) {
2503 sgtable[n].count = cpu_to_le32 (chan->slop_len | VIA_EOL | VIA_STOP);
2508 * If we get to this point, we have filled a buffer with
2509 * audio data, flush the buffer to audio hardware.
2512 /* Record the true size for the audio hardware to notice */
2513 if (n == (chan->frag_number - 1))
2514 sgtable[n].count = cpu_to_le32 (chan->frag_size | VIA_EOL);
2516 sgtable[n].count = cpu_to_le32 (chan->frag_size | VIA_FLAG);
2518 /* advance channel software pointer to point to
2519 * the next buffer we will fill with data
2521 if (chan->sw_ptr == (chan->frag_number - 1))
2526 /* mark one less buffer as being available for userspace consumption */
2527 assert (atomic_read (&chan->n_frags) > 0);
2528 atomic_dec (&chan->n_frags);
2530 /* we are at a block boundary, there is no fragment data */
2533 /* if SGD has not yet been started, start it */
2534 via_chan_maybe_start (chan);
2536 DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
2537 n, chan->sw_ptr, atomic_read (&chan->n_frags));
2539 DPRINTK ("regs==S=%02X C=%02X TP=%02X BP=%08X RT=%08X SG=%08X CC=%08X SS=%08X\n",
2540 inb (card->baseaddr + 0x00),
2541 inb (card->baseaddr + 0x01),
2542 inb (card->baseaddr + 0x02),
2543 inl (card->baseaddr + 0x04),
2544 inl (card->baseaddr + 0x08),
2545 inl (card->baseaddr + 0x0C),
2546 inl (card->baseaddr + 0x80),
2547 inl (card->baseaddr + 0x84));
2550 goto handle_one_block;
2553 if (userbuf - orig_userbuf)
2554 return userbuf - orig_userbuf;
2560 static ssize_t via_dsp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
2562 struct via_info *card;
2564 int nonblock = (file->f_flags & O_NONBLOCK);
2566 DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
2567 file, buffer, count, ppos ? ((unsigned long)*ppos) : 0);
2569 assert (file != NULL);
2570 card = file->private_data;
2571 assert (card != NULL);
2573 rc = via_syscall_down (card, nonblock);
2576 if (card->ch_out.is_mapped) {
2581 via_chan_set_buffering(card, &card->ch_out, -1);
2582 rc = via_chan_buffer_init (card, &card->ch_out);
2587 rc = via_dsp_do_write (card, buffer, count, nonblock);
2590 mutex_unlock(&card->syscall_mutex);
2592 DPRINTK ("EXIT, returning %ld\n",(long) rc);
2597 static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait)
2599 struct via_info *card;
2600 struct via_channel *chan;
2601 unsigned int mask = 0;
2603 DPRINTK ("ENTER\n");
2605 assert (file != NULL);
2606 card = file->private_data;
2607 assert (card != NULL);
2609 if (file->f_mode & FMODE_READ) {
2610 chan = &card->ch_in;
2611 if (sg_active (chan->iobase))
2612 poll_wait(file, &chan->wait, wait);
2613 if (atomic_read (&chan->n_frags) > 0)
2614 mask |= POLLIN | POLLRDNORM;
2617 if (file->f_mode & FMODE_WRITE) {
2618 chan = &card->ch_out;
2619 if (sg_active (chan->iobase))
2620 poll_wait(file, &chan->wait, wait);
2621 if (atomic_read (&chan->n_frags) > 0)
2622 mask |= POLLOUT | POLLWRNORM;
2625 DPRINTK ("EXIT, returning %u\n", mask);
2631 * via_dsp_drain_playback - sleep until all playback samples are flushed
2632 * @card: Private info for specified board
2633 * @chan: Channel to drain
2634 * @nonblock: boolean, non-zero if O_NONBLOCK is set
2636 * Sleeps until all playback has been flushed to the audio
2639 * Locking: inside card->syscall_mutex
2642 static int via_dsp_drain_playback (struct via_info *card,
2643 struct via_channel *chan, int nonblock)
2645 DECLARE_WAITQUEUE(wait, current);
2648 DPRINTK ("ENTER, nonblock = %d\n", nonblock);
2650 if (chan->slop_len > 0)
2651 via_chan_flush_frag (chan);
2653 if (atomic_read (&chan->n_frags) == chan->frag_number)
2656 via_chan_maybe_start (chan);
2658 add_wait_queue(&chan->wait, &wait);
2660 DPRINTK ("FRAGS %d FRAGNUM %d\n", atomic_read(&chan->n_frags), chan->frag_number);
2661 __set_current_state(TASK_INTERRUPTIBLE);
2662 if (atomic_read (&chan->n_frags) >= chan->frag_number)
2666 DPRINTK ("EXIT, returning -EAGAIN\n");
2673 u8 r40,r41,r42,r43,r44,r48;
2674 pci_read_config_byte (card->pdev, 0x40, &r40);
2675 pci_read_config_byte (card->pdev, 0x41, &r41);
2676 pci_read_config_byte (card->pdev, 0x42, &r42);
2677 pci_read_config_byte (card->pdev, 0x43, &r43);
2678 pci_read_config_byte (card->pdev, 0x44, &r44);
2679 pci_read_config_byte (card->pdev, 0x48, &r48);
2680 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
2681 r40,r41,r42,r43,r44,r48);
2683 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2684 inb (card->baseaddr + 0x00),
2685 inb (card->baseaddr + 0x01),
2686 inb (card->baseaddr + 0x02),
2687 inl (card->baseaddr + 0x04),
2688 inl (card->baseaddr + 0x0C),
2689 inl (card->baseaddr + 0x80),
2690 inl (card->baseaddr + 0x84));
2693 if (!chan->is_active)
2694 printk (KERN_ERR "sleeping but not active\n");
2697 mutex_unlock(&card->syscall_mutex);
2699 DPRINTK ("sleeping, nbufs=%d\n", atomic_read (&chan->n_frags));
2702 if ((ret = via_syscall_down (card, nonblock)))
2705 if (signal_pending (current)) {
2706 DPRINTK ("EXIT, returning -ERESTARTSYS\n");
2711 set_current_state(TASK_RUNNING);
2712 remove_wait_queue(&chan->wait, &wait);
2716 u8 r40,r41,r42,r43,r44,r48;
2717 pci_read_config_byte (card->pdev, 0x40, &r40);
2718 pci_read_config_byte (card->pdev, 0x41, &r41);
2719 pci_read_config_byte (card->pdev, 0x42, &r42);
2720 pci_read_config_byte (card->pdev, 0x43, &r43);
2721 pci_read_config_byte (card->pdev, 0x44, &r44);
2722 pci_read_config_byte (card->pdev, 0x48, &r48);
2723 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
2724 r40,r41,r42,r43,r44,r48);
2726 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2727 inb (card->baseaddr + 0x00),
2728 inb (card->baseaddr + 0x01),
2729 inb (card->baseaddr + 0x02),
2730 inl (card->baseaddr + 0x04),
2731 inl (card->baseaddr + 0x0C),
2732 inl (card->baseaddr + 0x80),
2733 inl (card->baseaddr + 0x84));
2735 DPRINTK ("final nbufs=%d\n", atomic_read (&chan->n_frags));
2740 DPRINTK ("EXIT, returning %d\n", ret);
2746 * via_dsp_ioctl_space - get information about channel buffering
2747 * @card: Private info for specified board
2748 * @chan: pointer to channel-specific info
2749 * @arg: user buffer for returned information
2751 * Handles SNDCTL_DSP_GETISPACE and SNDCTL_DSP_GETOSPACE.
2753 * Locking: inside card->syscall_mutex
2756 static int via_dsp_ioctl_space (struct via_info *card,
2757 struct via_channel *chan,
2760 audio_buf_info info;
2762 via_chan_set_buffering(card, chan, -1);
2764 info.fragstotal = chan->frag_number;
2765 info.fragsize = chan->frag_size;
2767 /* number of full fragments we can read/write without blocking */
2768 info.fragments = atomic_read (&chan->n_frags);
2770 if ((chan->slop_len % chan->frag_size > 0) && (info.fragments > 0))
2773 /* number of bytes that can be read or written immediately
2776 info.bytes = (info.fragments * chan->frag_size);
2777 if (chan->slop_len % chan->frag_size > 0)
2778 info.bytes += chan->frag_size - (chan->slop_len % chan->frag_size);
2780 DPRINTK ("EXIT, returning fragstotal=%d, fragsize=%d, fragments=%d, bytes=%d\n",
2786 return copy_to_user (arg, &info, sizeof (info))?-EFAULT:0;
2791 * via_dsp_ioctl_ptr - get information about hardware buffer ptr
2792 * @card: Private info for specified board
2793 * @chan: pointer to channel-specific info
2794 * @arg: user buffer for returned information
2796 * Handles SNDCTL_DSP_GETIPTR and SNDCTL_DSP_GETOPTR.
2798 * Locking: inside card->syscall_mutex
2801 static int via_dsp_ioctl_ptr (struct via_info *card,
2802 struct via_channel *chan,
2807 spin_lock_irq (&card->lock);
2809 info.bytes = chan->bytes;
2810 info.blocks = chan->n_irqs;
2813 spin_unlock_irq (&card->lock);
2815 if (chan->is_active) {
2816 unsigned long extra;
2817 info.ptr = atomic_read (&chan->hw_ptr) * chan->frag_size;
2818 extra = chan->frag_size - via_sg_offset(chan);
2820 info.bytes += extra;
2825 DPRINTK ("EXIT, returning bytes=%d, blocks=%d, ptr=%d\n",
2830 return copy_to_user (arg, &info, sizeof (info))?-EFAULT:0;
2834 static int via_dsp_ioctl_trigger (struct via_channel *chan, int val)
2836 int enable, do_something;
2838 if (chan->is_record)
2839 enable = (val & PCM_ENABLE_INPUT);
2841 enable = (val & PCM_ENABLE_OUTPUT);
2843 if (!chan->is_enabled && enable) {
2845 } else if (chan->is_enabled && !enable) {
2851 DPRINTK ("enable=%d, do_something=%d\n",
2852 enable, do_something);
2854 if (chan->is_active && do_something)
2857 if (do_something == 1) {
2858 chan->is_enabled = 1;
2859 via_chan_maybe_start (chan);
2860 DPRINTK ("Triggering input\n");
2863 else if (do_something == -1) {
2864 chan->is_enabled = 0;
2865 DPRINTK ("Setup input trigger\n");
2872 static int via_dsp_ioctl (struct inode *inode, struct file *file,
2873 unsigned int cmd, unsigned long arg)
2875 int rc, rd=0, wr=0, val=0;
2876 struct via_info *card;
2877 struct via_channel *chan;
2878 int nonblock = (file->f_flags & O_NONBLOCK);
2879 int __user *ip = (int __user *)arg;
2880 void __user *p = (void __user *)arg;
2882 assert (file != NULL);
2883 card = file->private_data;
2884 assert (card != NULL);
2886 if (file->f_mode & FMODE_WRITE)
2888 if (file->f_mode & FMODE_READ)
2891 rc = via_syscall_down (card, nonblock);
2898 /* OSS API version. XXX unverified */
2899 case OSS_GETVERSION:
2900 DPRINTK ("ioctl OSS_GETVERSION, EXIT, returning SOUND_VERSION\n");
2901 rc = put_user (SOUND_VERSION, ip);
2904 /* list of supported PCM data formats */
2905 case SNDCTL_DSP_GETFMTS:
2906 DPRINTK ("DSP_GETFMTS, EXIT, returning AFMT U8|S16_LE\n");
2907 rc = put_user (AFMT_U8 | AFMT_S16_LE, ip);
2910 /* query or set current channel's PCM data format */
2911 case SNDCTL_DSP_SETFMT:
2912 if (get_user(val, ip)) {
2916 DPRINTK ("DSP_SETFMT, val==%d\n", val);
2917 if (val != AFMT_QUERY) {
2921 rc = via_chan_set_fmt (card, &card->ch_in, val);
2924 rc = via_chan_set_fmt (card, &card->ch_out, val);
2931 if ((rd && (card->ch_in.pcm_fmt & VIA_PCM_FMT_16BIT)) ||
2932 (wr && (card->ch_out.pcm_fmt & VIA_PCM_FMT_16BIT)))
2937 DPRINTK ("SETFMT EXIT, returning %d\n", val);
2938 rc = put_user (val, ip);
2941 /* query or set number of channels (1=mono, 2=stereo, 4/6 for multichannel) */
2942 case SNDCTL_DSP_CHANNELS:
2943 if (get_user(val, ip)) {
2947 DPRINTK ("DSP_CHANNELS, val==%d\n", val);
2952 rc = via_chan_set_stereo (card, &card->ch_in, val);
2955 rc = via_chan_set_stereo (card, &card->ch_out, val);
2963 val = card->ch_in.channels;
2965 val = card->ch_out.channels;
2967 DPRINTK ("CHANNELS EXIT, returning %d\n", val);
2968 rc = put_user (val, ip);
2971 /* enable (val is not zero) or disable (val == 0) stereo */
2972 case SNDCTL_DSP_STEREO:
2973 if (get_user(val, ip)) {
2977 DPRINTK ("DSP_STEREO, val==%d\n", val);
2981 rc = via_chan_set_stereo (card, &card->ch_in, val ? 2 : 1);
2983 rc = via_chan_set_stereo (card, &card->ch_out, val ? 2 : 1);
2990 DPRINTK ("STEREO EXIT, returning %d\n", val);
2991 rc = put_user(val, ip);
2994 /* query or set sampling rate */
2995 case SNDCTL_DSP_SPEED:
2996 if (get_user(val, ip)) {
3000 DPRINTK ("DSP_SPEED, val==%d\n", val);
3009 rc = via_chan_set_speed (card, &card->ch_in, val);
3011 rc = via_chan_set_speed (card, &card->ch_out, val);
3019 val = card->ch_in.rate;
3021 val = card->ch_out.rate;
3025 DPRINTK ("SPEED EXIT, returning %d\n", val);
3026 rc = put_user (val, ip);
3029 /* wait until all buffers have been played, and then stop device */
3030 case SNDCTL_DSP_SYNC:
3031 DPRINTK ("DSP_SYNC\n");
3034 DPRINTK ("SYNC EXIT (after calling via_dsp_drain_playback)\n");
3035 rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
3039 /* stop recording/playback immediately */
3040 case SNDCTL_DSP_RESET:
3041 DPRINTK ("DSP_RESET\n");
3043 via_chan_clear (card, &card->ch_in);
3044 card->ch_in.frag_number = 0;
3045 card->ch_in.frag_size = 0;
3046 atomic_set(&card->ch_in.n_frags, 0);
3050 via_chan_clear (card, &card->ch_out);
3051 card->ch_out.frag_number = 0;
3052 card->ch_out.frag_size = 0;
3053 atomic_set(&card->ch_out.n_frags, 0);
3059 case SNDCTL_DSP_NONBLOCK:
3060 file->f_flags |= O_NONBLOCK;
3064 /* obtain bitmask of device capabilities, such as mmap, full duplex, etc. */
3065 case SNDCTL_DSP_GETCAPS:
3066 DPRINTK ("DSP_GETCAPS\n");
3067 rc = put_user(VIA_DSP_CAP, ip);
3070 /* obtain buffer fragment size */
3071 case SNDCTL_DSP_GETBLKSIZE:
3072 DPRINTK ("DSP_GETBLKSIZE\n");
3075 via_chan_set_buffering(card, &card->ch_in, -1);
3076 rc = put_user(card->ch_in.frag_size, ip);
3078 via_chan_set_buffering(card, &card->ch_out, -1);
3079 rc = put_user(card->ch_out.frag_size, ip);
3083 /* obtain information about input buffering */
3084 case SNDCTL_DSP_GETISPACE:
3085 DPRINTK ("DSP_GETISPACE\n");
3087 rc = via_dsp_ioctl_space (card, &card->ch_in, p);
3090 /* obtain information about output buffering */
3091 case SNDCTL_DSP_GETOSPACE:
3092 DPRINTK ("DSP_GETOSPACE\n");
3094 rc = via_dsp_ioctl_space (card, &card->ch_out, p);
3097 /* obtain information about input hardware pointer */
3098 case SNDCTL_DSP_GETIPTR:
3099 DPRINTK ("DSP_GETIPTR\n");
3101 rc = via_dsp_ioctl_ptr (card, &card->ch_in, p);
3104 /* obtain information about output hardware pointer */
3105 case SNDCTL_DSP_GETOPTR:
3106 DPRINTK ("DSP_GETOPTR\n");
3108 rc = via_dsp_ioctl_ptr (card, &card->ch_out, p);
3111 /* return number of bytes remaining to be played by DMA engine */
3112 case SNDCTL_DSP_GETODELAY:
3114 DPRINTK ("DSP_GETODELAY\n");
3116 chan = &card->ch_out;
3121 if (chan->is_active) {
3123 val = chan->frag_number - atomic_read (&chan->n_frags);
3128 val *= chan->frag_size;
3129 val -= chan->frag_size - via_sg_offset(chan);
3131 val += chan->slop_len % chan->frag_size;
3135 assert (val <= (chan->frag_size * chan->frag_number));
3137 DPRINTK ("GETODELAY EXIT, val = %d bytes\n", val);
3138 rc = put_user (val, ip);
3142 /* handle the quick-start of a channel,
3143 * or the notification that a quick-start will
3144 * occur in the future
3146 case SNDCTL_DSP_SETTRIGGER:
3147 if (get_user(val, ip)) {
3151 DPRINTK ("DSP_SETTRIGGER, rd=%d, wr=%d, act=%d/%d, en=%d/%d\n",
3152 rd, wr, card->ch_in.is_active, card->ch_out.is_active,
3153 card->ch_in.is_enabled, card->ch_out.is_enabled);
3158 rc = via_dsp_ioctl_trigger (&card->ch_in, val);
3161 rc = via_dsp_ioctl_trigger (&card->ch_out, val);
3165 case SNDCTL_DSP_GETTRIGGER:
3167 if ((file->f_mode & FMODE_READ) && card->ch_in.is_enabled)
3168 val |= PCM_ENABLE_INPUT;
3169 if ((file->f_mode & FMODE_WRITE) && card->ch_out.is_enabled)
3170 val |= PCM_ENABLE_OUTPUT;
3171 rc = put_user(val, ip);
3174 /* Enable full duplex. Since we do this as soon as we are opened
3175 * with O_RDWR, this is mainly a no-op that always returns success.
3177 case SNDCTL_DSP_SETDUPLEX:
3178 DPRINTK ("DSP_SETDUPLEX\n");
3184 /* set fragment size. implemented as a successful no-op for now */
3185 case SNDCTL_DSP_SETFRAGMENT:
3186 if (get_user(val, ip)) {
3190 DPRINTK ("DSP_SETFRAGMENT, val==%d\n", val);
3193 rc = via_chan_set_buffering(card, &card->ch_in, val);
3196 rc = via_chan_set_buffering(card, &card->ch_out, val);
3198 DPRINTK ("SNDCTL_DSP_SETFRAGMENT (fragshift==0x%04X (%d), maxfrags==0x%04X (%d))\n",
3201 (val >> 16) & 0xFFFF,
3202 (val >> 16) & 0xFFFF);
3207 /* inform device of an upcoming pause in input (or output). */
3208 case SNDCTL_DSP_POST:
3209 DPRINTK ("DSP_POST\n");
3211 if (card->ch_out.slop_len > 0)
3212 via_chan_flush_frag (&card->ch_out);
3213 via_chan_maybe_start (&card->ch_out);
3219 /* not implemented */
3221 DPRINTK ("unhandled ioctl, cmd==%u, arg==%p\n",
3226 mutex_unlock(&card->syscall_mutex);
3227 DPRINTK ("EXIT, returning %d\n", rc);
3232 static int via_dsp_open (struct inode *inode, struct file *file)
3234 int minor = iminor(inode);
3235 struct via_info *card;
3236 struct pci_dev *pdev = NULL;
3237 struct via_channel *chan;
3238 struct pci_driver *drvr;
3239 int nonblock = (file->f_flags & O_NONBLOCK);
3241 DPRINTK ("ENTER, minor=%d, file->f_mode=0x%x\n", minor, file->f_mode);
3243 if (!(file->f_mode & (FMODE_READ | FMODE_WRITE))) {
3244 DPRINTK ("EXIT, returning -EINVAL\n");
3249 while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
3250 drvr = pci_dev_driver (pdev);
3251 if (drvr == &via_driver) {
3252 assert (pci_get_drvdata (pdev) != NULL);
3254 card = pci_get_drvdata (pdev);
3255 DPRINTK ("dev_dsp = %d, minor = %d, assn = %d\n",
3256 card->dev_dsp, minor,
3257 (card->dev_dsp ^ minor) & ~0xf);
3259 if (((card->dev_dsp ^ minor) & ~0xf) == 0)
3264 DPRINTK ("no matching %s found\n", card ? "minor" : "driver");
3269 if (!mutex_trylock(&card->open_mutex)) {
3270 DPRINTK ("EXIT, returning -EAGAIN\n");
3274 if (mutex_lock_interruptible(&card->open_mutex)) {
3275 DPRINTK ("EXIT, returning -ERESTARTSYS\n");
3276 return -ERESTARTSYS;
3280 file->private_data = card;
3281 DPRINTK ("file->f_mode == 0x%x\n", file->f_mode);
3283 /* handle input from analog source */
3284 if (file->f_mode & FMODE_READ) {
3285 chan = &card->ch_in;
3287 via_chan_init (card, chan);
3289 /* why is this forced to 16-bit stereo in all drivers? */
3290 chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
3293 // TO DO - use FIFO: via_capture_fifo(card, 1);
3294 via_chan_pcm_fmt (chan, 0);
3295 via_set_rate (card->ac97, chan, 44100);
3298 /* handle output to analog source */
3299 if (file->f_mode & FMODE_WRITE) {
3300 chan = &card->ch_out;
3302 via_chan_init (card, chan);
3304 if (file->f_mode & FMODE_READ) {
3305 /* if in duplex mode make the recording and playback channels
3306 have the same settings */
3307 chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
3309 via_chan_pcm_fmt (chan, 0);
3310 via_set_rate (card->ac97, chan, 44100);
3312 if ((minor & 0xf) == SND_DEV_DSP16) {
3313 chan->pcm_fmt = VIA_PCM_FMT_16BIT;
3314 via_chan_pcm_fmt (chan, 0);
3315 via_set_rate (card->ac97, chan, 44100);
3317 via_chan_pcm_fmt (chan, 1);
3318 via_set_rate (card->ac97, chan, 8000);
3323 DPRINTK ("EXIT, returning 0\n");
3324 return nonseekable_open(inode, file);
3328 static int via_dsp_release(struct inode *inode, struct file *file)
3330 struct via_info *card;
3331 int nonblock = (file->f_flags & O_NONBLOCK);
3334 DPRINTK ("ENTER\n");
3336 assert (file != NULL);
3337 card = file->private_data;
3338 assert (card != NULL);
3340 rc = via_syscall_down (card, nonblock);
3342 DPRINTK ("EXIT (syscall_down error), rc=%d\n", rc);
3346 if (file->f_mode & FMODE_WRITE) {
3347 rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
3348 if (rc && rc != -ERESTARTSYS) /* Nobody needs to know about ^C */
3349 printk (KERN_DEBUG "via_audio: ignoring drain playback error %d\n", rc);
3351 via_chan_free (card, &card->ch_out);
3352 via_chan_buffer_free(card, &card->ch_out);
3355 if (file->f_mode & FMODE_READ) {
3356 via_chan_free (card, &card->ch_in);
3357 via_chan_buffer_free (card, &card->ch_in);
3360 mutex_unlock(&card->syscall_mutex);
3361 mutex_unlock(&card->open_mutex);
3363 DPRINTK ("EXIT, returning 0\n");
3368 /****************************************************************
3370 * Chip setup and kernel registration
3375 static int __devinit via_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
3377 #ifdef CONFIG_MIDI_VIA82CXXX
3381 struct via_info *card;
3382 static int printed_version;
3384 DPRINTK ("ENTER\n");
3386 if (printed_version++ == 0)
3387 printk (KERN_INFO "Via 686a/8233/8235 audio driver " VIA_VERSION "\n");
3389 rc = pci_enable_device (pdev);
3393 rc = pci_request_regions (pdev, "via82cxxx_audio");
3395 goto err_out_disable;
3397 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3400 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
3404 card = kmalloc (sizeof (*card), GFP_KERNEL);
3406 printk (KERN_ERR PFX "out of memory, aborting\n");
3411 pci_set_drvdata (pdev, card);
3413 memset (card, 0, sizeof (*card));
3415 card->baseaddr = pci_resource_start (pdev, 0);
3416 card->card_num = via_num_cards++;
3417 spin_lock_init (&card->lock);
3418 spin_lock_init (&card->ac97_lock);
3419 mutex_init(&card->syscall_mutex);
3420 mutex_init(&card->open_mutex);
3422 /* we must init these now, in case the intr handler needs them */
3423 via_chan_init_defaults (card, &card->ch_out);
3424 via_chan_init_defaults (card, &card->ch_in);
3425 via_chan_init_defaults (card, &card->ch_fm);
3427 /* if BAR 2 is present, chip is Rev H or later,
3428 * which means it has a few extra features */
3429 if (pci_resource_start (pdev, 2) > 0)
3432 /* Overkill for now, but more flexible done right */
3434 card->intmask = id->driver_data;
3435 card->legacy = !card->intmask;
3436 card->sixchannel = id->driver_data;
3438 if(card->sixchannel)
3439 printk(KERN_INFO PFX "Six channel audio available\n");
3440 if (pdev->irq < 1) {
3441 printk (KERN_ERR PFX "invalid PCI IRQ %d, aborting\n", pdev->irq);
3446 if (!(pci_resource_flags (pdev, 0) & IORESOURCE_IO)) {
3447 printk (KERN_ERR PFX "unable to locate I/O resources, aborting\n");
3452 pci_set_master(pdev);
3455 * init AC97 mixer and codec
3457 rc = via_ac97_init (card);
3459 printk (KERN_ERR PFX "AC97 init failed, aborting\n");
3466 rc = via_dsp_init (card);
3468 printk (KERN_ERR PFX "DSP device init failed, aborting\n");
3469 goto err_out_have_mixer;
3473 * init and turn on interrupts, as the last thing we do
3475 rc = via_interrupt_init (card);
3477 printk (KERN_ERR PFX "interrupt init failed, aborting\n");
3478 goto err_out_have_dsp;
3481 printk (KERN_INFO PFX "board #%d at 0x%04lX, IRQ %d\n",
3482 card->card_num + 1, card->baseaddr, pdev->irq);
3484 #ifdef CONFIG_MIDI_VIA82CXXX
3485 /* Disable by default */
3486 card->midi_info.io_base = 0;
3490 pci_read_config_byte (pdev, 0x42, &r42);
3491 /* Disable MIDI interrupt */
3492 pci_write_config_byte (pdev, 0x42, r42 | VIA_CR42_MIDI_IRQMASK);
3493 if (r42 & VIA_CR42_MIDI_ENABLE)
3495 if (r42 & VIA_CR42_MIDI_PNP) /* Address selected by iobase 2 - not tested */
3496 card->midi_info.io_base = pci_resource_start (pdev, 2);
3497 else /* Address selected by byte 0x43 */
3500 pci_read_config_byte (pdev, 0x43, &r43);
3501 card->midi_info.io_base = 0x300 + ((r43 & 0x0c) << 2);
3504 card->midi_info.irq = -pdev->irq;
3505 if (probe_uart401(& card->midi_info, THIS_MODULE))
3507 card->midi_devc=midi_devs[card->midi_info.slots[4]]->devc;
3508 pci_write_config_byte(pdev, 0x42, r42 & ~VIA_CR42_MIDI_IRQMASK);
3509 printk("Enabled Via MIDI\n");
3515 DPRINTK ("EXIT, returning 0\n");
3519 via_dsp_cleanup (card);
3522 via_ac97_cleanup (card);
3526 memset (card, OSS_POISON_FREE, sizeof (*card)); /* poison memory */
3531 pci_release_regions (pdev);
3534 pci_disable_device (pdev);
3537 pci_set_drvdata (pdev, NULL);
3538 DPRINTK ("EXIT - returning %d\n", rc);
3543 static void __devexit via_remove_one (struct pci_dev *pdev)
3545 struct via_info *card;
3547 DPRINTK ("ENTER\n");
3549 assert (pdev != NULL);
3550 card = pci_get_drvdata (pdev);
3551 assert (card != NULL);
3553 #ifdef CONFIG_MIDI_VIA82CXXX
3554 if (card->midi_info.io_base)
3555 unload_uart401(&card->midi_info);
3558 free_irq (card->pdev->irq, card);
3559 via_dsp_cleanup (card);
3560 via_ac97_cleanup (card);
3563 memset (card, OSS_POISON_FREE, sizeof (*card)); /* poison memory */
3567 pci_set_drvdata (pdev, NULL);
3569 pci_release_regions (pdev);
3570 pci_disable_device (pdev);
3571 pci_set_power_state (pdev, 3); /* ...zzzzzz */
3578 /****************************************************************
3580 * Driver initialization and cleanup
3585 static int __init init_via82cxxx_audio(void)
3589 DPRINTK ("ENTER\n");
3591 rc = pci_register_driver (&via_driver);
3593 DPRINTK ("EXIT, returning %d\n", rc);
3597 DPRINTK ("EXIT, returning 0\n");
3602 static void __exit cleanup_via82cxxx_audio(void)
3604 DPRINTK ("ENTER\n");
3606 pci_unregister_driver (&via_driver);
3612 module_init(init_via82cxxx_audio);
3613 module_exit(cleanup_via82cxxx_audio);
3615 MODULE_AUTHOR("Jeff Garzik");
3616 MODULE_DESCRIPTION("DSP audio and mixer driver for Via 82Cxxx audio devices");
3617 MODULE_LICENSE("GPL");