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 linux/Documentation/DocBook.
14 * If this documentation does not exist, run "make pdfdocs".
18 #define VIA_VERSION "1.9.1-ac3-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>
39 #include <asm/uaccess.h>
40 #include <asm/semaphore.h>
41 #include "sound_config.h"
42 #include "dev_table.h"
46 #undef VIA_DEBUG /* define to enable debugging output and checks */
48 /* note: prints function name for you */
49 #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
51 #define DPRINTK(fmt, args...)
54 #undef VIA_NDEBUG /* define to disable lightweight runtime checks */
58 #define assert(expr) \
60 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
61 #expr,__FILE__,__FUNCTION__,__LINE__); \
65 #if defined(CONFIG_PROC_FS) && \
66 defined(CONFIG_SOUND_VIA82CXXX_PROCFS)
70 #define VIA_SUPPORT_MMAP 1 /* buggy, for now... */
74 #define VIA_CARD_NAME "VIA 82Cxxx Audio driver " VIA_VERSION
75 #define VIA_MODULE_NAME "via82cxxx"
76 #define PFX VIA_MODULE_NAME ": "
78 #define VIA_COUNTER_LIMIT 100000
80 /* size of DMA buffers */
81 #define VIA_MAX_BUFFER_DMA_PAGES 32
83 /* buffering default values in ms */
84 #define VIA_DEFAULT_FRAG_TIME 20
85 #define VIA_DEFAULT_BUFFER_TIME 500
87 /* the hardware has a 256 fragment limit */
88 #define VIA_MIN_FRAG_NUMBER 2
89 #define VIA_MAX_FRAG_NUMBER 128
91 #define VIA_MAX_FRAG_SIZE PAGE_SIZE
92 #define VIA_MIN_FRAG_SIZE (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE / VIA_MAX_FRAG_NUMBER)
95 /* 82C686 function 5 (audio codec) PCI configuration registers */
96 #define VIA_ACLINK_STATUS 0x40
97 #define VIA_ACLINK_CTRL 0x41
98 #define VIA_FUNC_ENABLE 0x42
99 #define VIA_PNP_CONTROL 0x43
100 #define VIA_FM_NMI_CTRL 0x48
103 * controller base 0 (scatter-gather) registers
105 * NOTE: Via datasheet lists first channel as "read"
106 * channel and second channel as "write" channel.
107 * I changed the naming of the constants to be more
108 * clear than I felt the datasheet to be.
111 #define VIA_BASE0_PCM_OUT_CHAN 0x00 /* output PCM to user */
112 #define VIA_BASE0_PCM_OUT_CHAN_STATUS 0x00
113 #define VIA_BASE0_PCM_OUT_CHAN_CTRL 0x01
114 #define VIA_BASE0_PCM_OUT_CHAN_TYPE 0x02
116 #define VIA_BASE0_PCM_IN_CHAN 0x10 /* input PCM from user */
117 #define VIA_BASE0_PCM_IN_CHAN_STATUS 0x10
118 #define VIA_BASE0_PCM_IN_CHAN_CTRL 0x11
119 #define VIA_BASE0_PCM_IN_CHAN_TYPE 0x12
121 /* offsets from base */
122 #define VIA_PCM_STATUS 0x00
123 #define VIA_PCM_CONTROL 0x01
124 #define VIA_PCM_TYPE 0x02
125 #define VIA_PCM_LEFTVOL 0x02
126 #define VIA_PCM_RIGHTVOL 0x03
127 #define VIA_PCM_TABLE_ADDR 0x04
128 #define VIA_PCM_STOPRATE 0x08 /* 8233+ */
129 #define VIA_PCM_BLOCK_COUNT 0x0C
131 /* XXX unused DMA channel for FM PCM data */
132 #define VIA_BASE0_FM_OUT_CHAN 0x20
133 #define VIA_BASE0_FM_OUT_CHAN_STATUS 0x20
134 #define VIA_BASE0_FM_OUT_CHAN_CTRL 0x21
135 #define VIA_BASE0_FM_OUT_CHAN_TYPE 0x22
137 /* Six channel audio output on 8233 */
138 #define VIA_BASE0_MULTI_OUT_CHAN 0x40
139 #define VIA_BASE0_MULTI_OUT_CHAN_STATUS 0x40
140 #define VIA_BASE0_MULTI_OUT_CHAN_CTRL 0x41
141 #define VIA_BASE0_MULTI_OUT_CHAN_TYPE 0x42
143 #define VIA_BASE0_AC97_CTRL 0x80
144 #define VIA_BASE0_SGD_STATUS_SHADOW 0x84
145 #define VIA_BASE0_GPI_INT_ENABLE 0x8C
146 #define VIA_INTR_OUT ((1<<0) | (1<<4) | (1<<8))
147 #define VIA_INTR_IN ((1<<1) | (1<<5) | (1<<9))
148 #define VIA_INTR_FM ((1<<2) | (1<<6) | (1<<10))
149 #define VIA_INTR_MASK (VIA_INTR_OUT | VIA_INTR_IN | VIA_INTR_FM)
151 /* Newer VIA we need to monitor the low 3 bits of each channel. This
152 mask covers the channels we don't yet use as well
155 #define VIA_NEW_INTR_MASK 0x77077777UL
157 /* VIA_BASE0_AUDIO_xxx_CHAN_TYPE bits */
158 #define VIA_IRQ_ON_FLAG (1<<0) /* int on each flagged scatter block */
159 #define VIA_IRQ_ON_EOL (1<<1) /* int at end of scatter list */
160 #define VIA_INT_SEL_PCI_LAST_LINE_READ (0) /* int at PCI read of last line */
161 #define VIA_INT_SEL_LAST_SAMPLE_SENT (1<<2) /* int at last sample sent */
162 #define VIA_INT_SEL_ONE_LINE_LEFT (1<<3) /* int at less than one line to send */
163 #define VIA_PCM_FMT_STEREO (1<<4) /* PCM stereo format (bit clear == mono) */
164 #define VIA_PCM_FMT_16BIT (1<<5) /* PCM 16-bit format (bit clear == 8-bit) */
165 #define VIA_PCM_REC_FIFO (1<<6) /* PCM Recording FIFO */
166 #define VIA_RESTART_SGD_ON_EOL (1<<7) /* restart scatter-gather at EOL */
167 #define VIA_PCM_FMT_MASK (VIA_PCM_FMT_STEREO|VIA_PCM_FMT_16BIT)
168 #define VIA_CHAN_TYPE_MASK (VIA_RESTART_SGD_ON_EOL | \
171 #define VIA_CHAN_TYPE_INT_SELECT (VIA_INT_SEL_LAST_SAMPLE_SENT)
173 /* PCI configuration register bits and masks */
174 #define VIA_CR40_AC97_READY 0x01
175 #define VIA_CR40_AC97_LOW_POWER 0x02
176 #define VIA_CR40_SECONDARY_READY 0x04
178 #define VIA_CR41_AC97_ENABLE 0x80 /* enable AC97 codec */
179 #define VIA_CR41_AC97_RESET 0x40 /* clear bit to reset AC97 */
180 #define VIA_CR41_AC97_WAKEUP 0x20 /* wake up from power-down mode */
181 #define VIA_CR41_AC97_SDO 0x10 /* force Serial Data Out (SDO) high */
182 #define VIA_CR41_VRA 0x08 /* enable variable sample rate */
183 #define VIA_CR41_PCM_ENABLE 0x04 /* AC Link SGD Read Channel PCM Data Output */
184 #define VIA_CR41_FM_PCM_ENABLE 0x02 /* AC Link FM Channel PCM Data Out */
185 #define VIA_CR41_SB_PCM_ENABLE 0x01 /* AC Link SB PCM Data Output */
186 #define VIA_CR41_BOOT_MASK (VIA_CR41_AC97_ENABLE | \
187 VIA_CR41_AC97_WAKEUP | \
189 #define VIA_CR41_RUN_MASK (VIA_CR41_AC97_ENABLE | \
190 VIA_CR41_AC97_RESET | \
194 #define VIA_CR42_SB_ENABLE 0x01
195 #define VIA_CR42_MIDI_ENABLE 0x02
196 #define VIA_CR42_FM_ENABLE 0x04
197 #define VIA_CR42_GAME_ENABLE 0x08
198 #define VIA_CR42_MIDI_IRQMASK 0x40
199 #define VIA_CR42_MIDI_PNP 0x80
201 #define VIA_CR44_SECOND_CODEC_SUPPORT (1 << 6)
202 #define VIA_CR44_AC_LINK_ACCESS (1 << 7)
204 #define VIA_CR48_FM_TRAP_TO_NMI (1 << 2)
206 /* controller base 0 register bitmasks */
207 #define VIA_INT_DISABLE_MASK (~(0x01|0x02))
208 #define VIA_SGD_STOPPED (1 << 2)
209 #define VIA_SGD_PAUSED (1 << 6)
210 #define VIA_SGD_ACTIVE (1 << 7)
211 #define VIA_SGD_TERMINATE (1 << 6)
212 #define VIA_SGD_FLAG (1 << 0)
213 #define VIA_SGD_EOL (1 << 1)
214 #define VIA_SGD_START (1 << 7)
216 #define VIA_CR80_FIRST_CODEC 0
217 #define VIA_CR80_SECOND_CODEC (1 << 30)
218 #define VIA_CR80_FIRST_CODEC_VALID (1 << 25)
219 #define VIA_CR80_VALID (1 << 25)
220 #define VIA_CR80_SECOND_CODEC_VALID (1 << 27)
221 #define VIA_CR80_BUSY (1 << 24)
222 #define VIA_CR83_BUSY (1)
223 #define VIA_CR83_FIRST_CODEC_VALID (1 << 1)
224 #define VIA_CR80_READ (1 << 23)
225 #define VIA_CR80_WRITE_MODE 0
226 #define VIA_CR80_REG_IDX(idx) ((((idx) & 0xFF) >> 1) << 16)
228 /* capabilities we announce */
229 #ifdef VIA_SUPPORT_MMAP
230 #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | DSP_CAP_MMAP | \
231 DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
233 #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | \
234 DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
237 /* scatter-gather DMA table entry, exactly as passed to hardware */
238 struct via_sgd_table {
240 u32 count; /* includes additional VIA_xxx bits also */
243 #define VIA_EOL (1 << 31)
244 #define VIA_FLAG (1 << 30)
245 #define VIA_STOP (1 << 29)
248 enum via_channel_states {
254 struct via_buffer_pgtbl {
263 wait_queue_head_t wait;
266 unsigned int slop_len;
270 unsigned is_active : 1;
271 unsigned is_record : 1;
272 unsigned is_mapped : 1;
273 unsigned is_enabled : 1;
274 unsigned is_multi: 1; /* 8233 6 channel */
275 u8 pcm_fmt; /* VIA_PCM_FMT_xxx */
276 u8 channels; /* Channel count */
278 unsigned rate; /* sample rate */
279 unsigned int frag_size;
280 unsigned int frag_number;
282 unsigned char intmask;
284 volatile struct via_sgd_table *sgtable;
285 dma_addr_t sgt_handle;
287 unsigned int page_number;
288 struct via_buffer_pgtbl pgtbl[VIA_MAX_BUFFER_DMA_PAGES];
296 /* data stored for each chip */
298 struct pci_dev *pdev;
301 struct ac97_codec *ac97;
302 spinlock_t ac97_lock;
304 int card_num; /* unique card number, from 0 */
306 int dev_dsp; /* /dev/dsp index from register_sound_dsp() */
309 unsigned legacy: 1; /* Has legacy ports */
310 unsigned intmask: 1; /* Needs int bits */
311 unsigned sixchannel: 1; /* 8233/35 with 6 channel support */
316 int mixer_vol; /* 8233/35 volume - not yet implemented */
318 struct semaphore syscall_sem;
319 struct semaphore open_sem;
321 /* The 8233/8235 have 4 DX audio channels, two record and
322 one six channel out. We bind ch_in to DX 1, ch_out to multichannel
323 and ch_fm to DX 2. DX 3 and REC0/REC1 are unused at the
326 struct via_channel ch_in;
327 struct via_channel ch_out;
328 struct via_channel ch_fm;
330 #ifdef CONFIG_MIDI_VIA82CXXX
332 struct address_info midi_info;
337 /* number of cards, used for assigning unique numbers to cards */
338 static unsigned via_num_cards = 0;
342 /****************************************************************
349 static int via_init_one (struct pci_dev *dev, const struct pci_device_id *id);
350 static void __devexit via_remove_one (struct pci_dev *pdev);
352 static ssize_t via_dsp_read(struct file *file, char *buffer, size_t count, loff_t *ppos);
353 static ssize_t via_dsp_write(struct file *file, const char *buffer, size_t count, loff_t *ppos);
354 static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait);
355 static int via_dsp_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
356 static int via_dsp_open (struct inode *inode, struct file *file);
357 static int via_dsp_release(struct inode *inode, struct file *file);
358 static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma);
360 static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg);
361 static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value);
362 static u8 via_ac97_wait_idle (struct via_info *card);
364 static void via_chan_free (struct via_info *card, struct via_channel *chan);
365 static void via_chan_clear (struct via_info *card, struct via_channel *chan);
366 static void via_chan_pcm_fmt (struct via_channel *chan, int reset);
367 static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan);
370 static int via_init_proc (void);
371 static void via_cleanup_proc (void);
372 static int via_card_init_proc (struct via_info *card);
373 static void via_card_cleanup_proc (struct via_info *card);
375 static inline int via_init_proc (void) { return 0; }
376 static inline void via_cleanup_proc (void) {}
377 static inline int via_card_init_proc (struct via_info *card) { return 0; }
378 static inline void via_card_cleanup_proc (struct via_info *card) {}
382 /****************************************************************
384 * Various data the driver needs
390 static struct pci_device_id via_pci_tbl[] = {
391 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5,
392 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
393 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_5,
394 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
397 MODULE_DEVICE_TABLE(pci,via_pci_tbl);
400 static struct pci_driver via_driver = {
401 name: VIA_MODULE_NAME,
402 id_table: via_pci_tbl,
404 remove: __devexit_p(via_remove_one),
408 /****************************************************************
410 * Low-level base 0 register read/write helpers
416 * via_chan_stop - Terminate DMA on specified PCM channel
417 * @iobase: PCI base address for SGD channel registers
419 * Terminate scatter-gather DMA operation for given
420 * channel (derived from @iobase), if DMA is active.
422 * Note that @iobase is not the PCI base address,
423 * but the PCI base address plus an offset to
424 * one of three PCM channels supported by the chip.
428 static inline void via_chan_stop (long iobase)
430 if (inb (iobase + VIA_PCM_STATUS) & VIA_SGD_ACTIVE)
431 outb (VIA_SGD_TERMINATE, iobase + VIA_PCM_CONTROL);
436 * via_chan_status_clear - Clear status flags on specified DMA channel
437 * @iobase: PCI base address for SGD channel registers
439 * Clear any pending status flags for the given
440 * DMA channel (derived from @iobase), if any
441 * flags are asserted.
443 * Note that @iobase is not the PCI base address,
444 * but the PCI base address plus an offset to
445 * one of three PCM channels supported by the chip.
449 static inline void via_chan_status_clear (long iobase)
451 u8 tmp = inb (iobase + VIA_PCM_STATUS);
454 outb (tmp, iobase + VIA_PCM_STATUS);
459 * sg_begin - Begin recording or playback on a PCM channel
460 * @chan: Channel for which DMA operation shall begin
462 * Start scatter-gather DMA for the given channel.
466 static inline void sg_begin (struct via_channel *chan)
468 DPRINTK("Start with intmask %d\n", chan->intmask);
469 DPRINTK("About to start from %d to %d\n",
470 inl(chan->iobase + VIA_PCM_BLOCK_COUNT),
471 inb(chan->iobase + VIA_PCM_STOPRATE + 3));
472 outb (VIA_SGD_START|chan->intmask, chan->iobase + VIA_PCM_CONTROL);
473 DPRINTK("Status is now %02X\n", inb(chan->iobase + VIA_PCM_STATUS));
474 DPRINTK("Control is now %02X\n", inb(chan->iobase + VIA_PCM_CONTROL));
478 static int sg_active (long iobase)
480 u8 tmp = inb (iobase + VIA_PCM_STATUS);
481 if ((tmp & VIA_SGD_STOPPED) || (tmp & VIA_SGD_PAUSED)) {
482 printk(KERN_WARNING "via82cxxx warning: SG stopped or paused\n");
485 if (tmp & VIA_SGD_ACTIVE)
490 static int via_sg_offset(struct via_channel *chan)
492 return inl (chan->iobase + VIA_PCM_BLOCK_COUNT) & 0x00FFFFFF;
495 /****************************************************************
497 * Miscellaneous debris
504 * via_syscall_down - down the card-specific syscell semaphore
505 * @card: Private info for specified board
506 * @nonblock: boolean, non-zero if O_NONBLOCK is set
508 * Encapsulates standard method of acquiring the syscall sem.
510 * Returns negative errno on error, or zero for success.
513 static inline int via_syscall_down (struct via_info *card, int nonblock)
516 * EAGAIN is supposed to be used if IO is pending,
517 * not if there is contention on some internal
518 * synchronization primitive which should be
519 * held only for a short time anyway
524 if (down_trylock (&card->syscall_sem))
527 if (down_interruptible (&card->syscall_sem))
536 * via_stop_everything - Stop all audio operations
537 * @card: Private info for specified board
539 * Stops all DMA operations and interrupts, and clear
540 * any pending status bits resulting from those operations.
543 static void via_stop_everything (struct via_info *card)
549 assert (card != NULL);
552 * terminate any existing operations on audio read/write channels
554 via_chan_stop (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
555 via_chan_stop (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
556 via_chan_stop (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
558 via_chan_stop (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN);
561 * clear any existing stops / flags (sanity check mainly)
563 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
564 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
565 via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
567 via_chan_status_clear (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN);
570 * clear any enabled interrupt bits
572 tmp = inb (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
573 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
575 outb (0, card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
577 tmp = inb (card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
578 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
580 outb (0, card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
582 tmp = inb (card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
583 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
585 outb (0, card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
589 tmp = inb (card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
590 new_tmp = tmp & ~(VIA_IRQ_ON_FLAG|VIA_IRQ_ON_EOL|VIA_RESTART_SGD_ON_EOL);
592 outb (0, card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
598 * clear any existing flags
600 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
601 via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
602 via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
609 * via_set_rate - Set PCM rate for given channel
610 * @ac97: Pointer to generic codec info struct
611 * @chan: Private info for specified channel
612 * @rate: Desired PCM sample rate, in Khz
614 * Sets the PCM sample rate for a channel.
616 * Values for @rate are clamped to a range of 4000 Khz through 48000 Khz,
617 * due to hardware constraints.
620 static int via_set_rate (struct ac97_codec *ac97,
621 struct via_channel *chan, unsigned rate)
623 struct via_info *card = ac97->private_data;
626 u32 mast_vol, phone_vol, mono_vol, pcm_vol;
627 u32 mute_vol = 0x8000; /* The mute volume? -- Seems to work! */
629 DPRINTK ("ENTER, rate = %d\n", rate);
631 if (chan->rate == rate)
633 if (card->locked_rate) {
638 if (rate > 48000) rate = 48000;
639 if (rate < 4000) rate = 4000;
641 rate_reg = chan->is_record ? AC97_PCM_LR_ADC_RATE :
642 AC97_PCM_FRONT_DAC_RATE;
644 /* Save current state */
645 dacp=via_ac97_read_reg(ac97, AC97_POWER_CONTROL);
646 mast_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_STEREO);
647 mono_vol = via_ac97_read_reg(ac97, AC97_MASTER_VOL_MONO);
648 phone_vol = via_ac97_read_reg(ac97, AC97_HEADPHONE_VOL);
649 pcm_vol = via_ac97_read_reg(ac97, AC97_PCMOUT_VOL);
650 /* Mute - largely reduces popping */
651 via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mute_vol);
652 via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mute_vol);
653 via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, mute_vol);
654 via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, mute_vol);
655 /* Power down the DAC */
656 via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp|0x0200);
659 via_ac97_write_reg (ac97, rate_reg, rate);
661 /* Power DAC back up */
662 via_ac97_write_reg(ac97, AC97_POWER_CONTROL, dacp);
663 udelay (200); /* reduces popping */
665 /* Restore volumes */
666 via_ac97_write_reg(ac97, AC97_MASTER_VOL_STEREO, mast_vol);
667 via_ac97_write_reg(ac97, AC97_MASTER_VOL_MONO, mono_vol);
668 via_ac97_write_reg(ac97, AC97_HEADPHONE_VOL, phone_vol);
669 via_ac97_write_reg(ac97, AC97_PCMOUT_VOL, pcm_vol);
671 /* the hardware might return a value different than what we
672 * passed to it, so read the rate value back from hardware
673 * to see what we came up with
675 chan->rate = via_ac97_read_reg (ac97, rate_reg);
677 if (chan->rate == 0) {
678 card->locked_rate = 1;
680 printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n");
684 DPRINTK ("EXIT, returning rate %d Hz\n", chan->rate);
689 /****************************************************************
691 * Channel-specific operations
698 * via_chan_init_defaults - Initialize a struct via_channel
699 * @card: Private audio chip info
700 * @chan: Channel to be initialized
702 * Zero @chan, and then set all static defaults for the structure.
705 static void via_chan_init_defaults (struct via_info *card, struct via_channel *chan)
707 memset (chan, 0, sizeof (*chan));
710 chan->intmask = 0x23; /* Turn on the IRQ bits */
712 if (chan == &card->ch_out) {
713 chan->name = "PCM-OUT";
716 chan->iobase = card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN;
718 DPRINTK("Using multichannel for pcm out\n");
721 chan->iobase = card->baseaddr + VIA_BASE0_PCM_OUT_CHAN;
722 } else if (chan == &card->ch_in) {
723 chan->name = "PCM-IN";
724 chan->iobase = card->baseaddr + VIA_BASE0_PCM_IN_CHAN;
726 } else if (chan == &card->ch_fm) {
727 chan->name = "PCM-OUT-FM";
728 chan->iobase = card->baseaddr + VIA_BASE0_FM_OUT_CHAN;
733 init_waitqueue_head (&chan->wait);
735 chan->pcm_fmt = VIA_PCM_FMT_MASK;
736 chan->is_enabled = 1;
738 chan->frag_number = 0;
740 atomic_set(&chan->n_frags, 0);
741 atomic_set (&chan->hw_ptr, 0);
745 * via_chan_init - Initialize PCM channel
746 * @card: Private audio chip info
747 * @chan: Channel to be initialized
749 * Performs some of the preparations necessary to begin
750 * using a PCM channel.
752 * Currently the preparations consist of
753 * setting the PCM channel to a known state.
757 static void via_chan_init (struct via_info *card, struct via_channel *chan)
762 /* bzero channel structure, and init members to defaults */
763 via_chan_init_defaults (card, chan);
765 /* stop any existing channel output */
766 via_chan_clear (card, chan);
767 via_chan_status_clear (chan->iobase);
768 via_chan_pcm_fmt (chan, 1);
774 * via_chan_buffer_init - Initialize PCM channel buffer
775 * @card: Private audio chip info
776 * @chan: Channel to be initialized
778 * Performs some of the preparations necessary to begin
779 * using a PCM channel.
781 * Currently the preparations include allocating the
782 * scatter-gather DMA table and buffers,
784 * address of the DMA table to the hardware.
786 * Note that special care is taken when passing the
787 * DMA table address to hardware, because it was found
788 * during driver development that the hardware did not
789 * always "take" the address.
792 static int via_chan_buffer_init (struct via_info *card, struct via_channel *chan)
802 chan->intmask = 0x23; /* Turn on the IRQ bits */
804 if (chan->sgtable != NULL) {
809 /* alloc DMA-able memory for scatter-gather table */
810 chan->sgtable = pci_alloc_consistent (card->pdev,
811 (sizeof (struct via_sgd_table) * chan->frag_number),
813 if (!chan->sgtable) {
814 printk (KERN_ERR PFX "DMA table alloc fail, aborting\n");
819 memset ((void*)chan->sgtable, 0,
820 (sizeof (struct via_sgd_table) * chan->frag_number));
822 /* alloc DMA-able memory for scatter-gather buffers */
824 chan->page_number = (chan->frag_number * chan->frag_size) / PAGE_SIZE +
825 (((chan->frag_number * chan->frag_size) % PAGE_SIZE) ? 1 : 0);
827 for (i = 0; i < chan->page_number; i++) {
828 chan->pgtbl[i].cpuaddr = pci_alloc_consistent (card->pdev, PAGE_SIZE,
829 &chan->pgtbl[i].handle);
831 if (!chan->pgtbl[i].cpuaddr) {
832 chan->page_number = i;
837 memset (chan->pgtbl[i].cpuaddr, 0xBC, chan->frag_size);
841 DPRINTK ("dmabuf_pg #%d (h=%lx, v2p=%lx, a=%p)\n",
842 i, (long)chan->pgtbl[i].handle,
843 virt_to_phys(chan->pgtbl[i].cpuaddr),
844 chan->pgtbl[i].cpuaddr);
848 for (i = 0; i < chan->frag_number; i++) {
850 page = i / (PAGE_SIZE / chan->frag_size);
851 offset = (i % (PAGE_SIZE / chan->frag_size)) * chan->frag_size;
853 chan->sgtable[i].count = cpu_to_le32 (chan->frag_size | VIA_FLAG);
854 chan->sgtable[i].addr = cpu_to_le32 (chan->pgtbl[page].handle + offset);
857 DPRINTK ("dmabuf #%d (32(h)=%lx)\n",
859 (long)chan->sgtable[i].addr);
863 /* overwrite the last buffer information */
864 chan->sgtable[chan->frag_number - 1].count = cpu_to_le32 (chan->frag_size | VIA_EOL);
866 /* set location of DMA-able scatter-gather info table */
867 DPRINTK ("outl (0x%X, 0x%04lX)\n",
868 chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR);
870 via_ac97_wait_idle (card);
871 outl (chan->sgt_handle, chan->iobase + VIA_PCM_TABLE_ADDR);
873 via_ac97_wait_idle (card);
874 /* load no rate adaption, stereo 16bit, set up ring slots */
879 outl (0xFFFFF | (0x3 << 20) | (chan->frag_number << 24), chan->iobase + VIA_PCM_STOPRATE);
881 via_ac97_wait_idle (card);
885 DPRINTK ("inl (0x%lX) = %x\n",
886 chan->iobase + VIA_PCM_TABLE_ADDR,
887 inl(chan->iobase + VIA_PCM_TABLE_ADDR));
893 printk (KERN_ERR PFX "DMA buffer alloc fail, aborting\n");
894 via_chan_buffer_free (card, chan);
901 * via_chan_free - Release a PCM channel
902 * @card: Private audio chip info
903 * @chan: Channel to be released
905 * Performs all the functions necessary to clean up
906 * an initialized channel.
908 * Currently these functions include disabled any
909 * active DMA operations, setting the PCM channel
910 * back to a known state, and releasing any allocated
914 static void via_chan_free (struct via_info *card, struct via_channel *chan)
918 spin_lock_irq (&card->lock);
920 /* stop any existing channel output */
921 via_chan_status_clear (chan->iobase);
922 via_chan_stop (chan->iobase);
923 via_chan_status_clear (chan->iobase);
925 spin_unlock_irq (&card->lock);
927 synchronize_irq(card->pdev->irq);
932 static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan)
938 /* zero location of DMA-able scatter-gather info table */
939 via_ac97_wait_idle(card);
940 outl (0, chan->iobase + VIA_PCM_TABLE_ADDR);
942 for (i = 0; i < chan->page_number; i++)
943 if (chan->pgtbl[i].cpuaddr) {
944 pci_free_consistent (card->pdev, PAGE_SIZE,
945 chan->pgtbl[i].cpuaddr,
946 chan->pgtbl[i].handle);
947 chan->pgtbl[i].cpuaddr = NULL;
948 chan->pgtbl[i].handle = 0;
951 chan->page_number = 0;
954 pci_free_consistent (card->pdev,
955 (sizeof (struct via_sgd_table) * chan->frag_number),
956 (void*)chan->sgtable, chan->sgt_handle);
957 chan->sgtable = NULL;
965 * via_chan_pcm_fmt - Update PCM channel settings
966 * @chan: Channel to be updated
967 * @reset: Boolean. If non-zero, channel will be reset
968 * to 8-bit mono mode.
970 * Stores the settings of the current PCM format,
971 * 8-bit or 16-bit, and mono/stereo, into the
972 * hardware settings for the specified channel.
973 * If @reset is non-zero, the channel is reset
974 * to 8-bit mono mode. Otherwise, the channel
975 * is set to the values stored in the channel
976 * information struct @chan.
979 static void via_chan_pcm_fmt (struct via_channel *chan, int reset)
981 DPRINTK ("ENTER, pcm_fmt=0x%02X, reset=%s\n",
982 chan->pcm_fmt, reset ? "yes" : "no");
984 assert (chan != NULL);
988 /* reset to 8-bit mono mode */
993 /* enable interrupts on FLAG and EOL */
994 chan->pcm_fmt |= VIA_CHAN_TYPE_MASK;
996 /* if we are recording, enable recording fifo bit */
998 chan->pcm_fmt |= VIA_PCM_REC_FIFO;
999 /* set interrupt select bits where applicable (PCM in & out channels) */
1000 if (!chan->is_record)
1001 chan->pcm_fmt |= VIA_CHAN_TYPE_INT_SELECT;
1003 DPRINTK("SET FMT - %02x %02x\n", chan->intmask , chan->is_multi);
1010 * Channel 0x4 is up to 6 x 16bit and has to be
1011 * programmed differently
1019 * Load the type bit for num channels
1023 if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
1025 if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
1030 outb(c, chan->iobase + VIA_PCM_TYPE);
1033 * Set the channel steering
1035 * Channel 0 to slot 3
1036 * Channel 0 to slot 4
1038 * Channel 0 to slot 3
1039 * Channel 1 to slot 4
1042 switch(chan->channels)
1045 outl(0xFF000000 | (1<<0) | (1<<4) , chan->iobase + VIA_PCM_STOPRATE);
1048 outl(0xFF000000 | (1<<0) | (2<<4) , chan->iobase + VIA_PCM_STOPRATE);
1051 outl(0xFF000000 | (1<<0) | (2<<4) | (3<<8) | (4<<12), chan->iobase + VIA_PCM_STOPRATE);
1054 outl(0xFF000000 | (1<<0) | (2<<4) | (5<<8) | (6<<12) | (3<<16) | (4<<20), chan->iobase + VIA_PCM_STOPRATE);
1061 * New style, turn off channel volume
1062 * control, set bits in the right register
1064 outb(0x0, chan->iobase + VIA_PCM_LEFTVOL);
1065 outb(0x0, chan->iobase + VIA_PCM_RIGHTVOL);
1067 m = inl(chan->iobase + VIA_PCM_STOPRATE);
1069 if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
1071 if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
1073 outl(m, chan->iobase + VIA_PCM_STOPRATE);
1077 outb (chan->pcm_fmt, chan->iobase + VIA_PCM_TYPE);
1080 DPRINTK ("EXIT, pcm_fmt = 0x%02X, reg = 0x%02X\n",
1082 inb (chan->iobase + VIA_PCM_TYPE));
1087 * via_chan_clear - Stop DMA channel operation, and reset pointers
1088 * @card: the chip to accessed
1089 * @chan: Channel to be cleared
1091 * Call via_chan_stop to halt DMA operations, and then resets
1092 * all software pointers which track DMA operation.
1095 static void via_chan_clear (struct via_info *card, struct via_channel *chan)
1097 DPRINTK ("ENTER\n");
1098 via_chan_stop (chan->iobase);
1099 via_chan_buffer_free(card, chan);
1100 chan->is_active = 0;
1101 chan->is_mapped = 0;
1102 chan->is_enabled = 1;
1106 atomic_set (&chan->hw_ptr, 0);
1112 * via_chan_set_speed - Set PCM sample rate for given channel
1113 * @card: Private info for specified board
1114 * @chan: Channel whose sample rate will be adjusted
1115 * @val: New sample rate, in Khz
1117 * Helper function for the %SNDCTL_DSP_SPEED ioctl. OSS semantics
1118 * demand that all audio operations halt (if they are not already
1119 * halted) when the %SNDCTL_DSP_SPEED is given.
1121 * This function halts all audio operations for the given channel
1122 * @chan, and then calls via_set_rate to set the audio hardware
1126 static int via_chan_set_speed (struct via_info *card,
1127 struct via_channel *chan, int val)
1129 DPRINTK ("ENTER, requested rate = %d\n", val);
1131 via_chan_clear (card, chan);
1133 val = via_set_rate (card->ac97, chan, val);
1135 DPRINTK ("EXIT, returning %d\n", val);
1141 * via_chan_set_fmt - Set PCM sample size for given channel
1142 * @card: Private info for specified board
1143 * @chan: Channel whose sample size will be adjusted
1144 * @val: New sample size, use the %AFMT_xxx constants
1146 * Helper function for the %SNDCTL_DSP_SETFMT ioctl. OSS semantics
1147 * demand that all audio operations halt (if they are not already
1148 * halted) when the %SNDCTL_DSP_SETFMT is given.
1150 * This function halts all audio operations for the given channel
1151 * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
1152 * to the new sample size, either 8-bit or 16-bit.
1155 static int via_chan_set_fmt (struct via_info *card,
1156 struct via_channel *chan, int val)
1158 DPRINTK ("ENTER, val=%s\n",
1159 val == AFMT_U8 ? "AFMT_U8" :
1160 val == AFMT_S16_LE ? "AFMT_S16_LE" :
1163 via_chan_clear (card, chan);
1165 assert (val != AFMT_QUERY); /* this case is handled elsewhere */
1169 if ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) == 0) {
1170 chan->pcm_fmt |= VIA_PCM_FMT_16BIT;
1171 via_chan_pcm_fmt (chan, 0);
1176 if (chan->pcm_fmt & VIA_PCM_FMT_16BIT) {
1177 chan->pcm_fmt &= ~VIA_PCM_FMT_16BIT;
1178 via_chan_pcm_fmt (chan, 0);
1183 DPRINTK ("unknown AFMT: 0x%X\n", val);
1193 * via_chan_set_stereo - Enable or disable stereo for a DMA channel
1194 * @card: Private info for specified board
1195 * @chan: Channel whose stereo setting will be adjusted
1196 * @val: New sample size, use the %AFMT_xxx constants
1198 * Helper function for the %SNDCTL_DSP_CHANNELS and %SNDCTL_DSP_STEREO ioctls. OSS semantics
1199 * demand that all audio operations halt (if they are not already
1200 * halted) when %SNDCTL_DSP_CHANNELS or SNDCTL_DSP_STEREO is given.
1202 * This function halts all audio operations for the given channel
1203 * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
1204 * to enable or disable stereo.
1207 static int via_chan_set_stereo (struct via_info *card,
1208 struct via_channel *chan, int val)
1210 DPRINTK ("ENTER, channels = %d\n", val);
1212 via_chan_clear (card, chan);
1218 chan->pcm_fmt &= ~VIA_PCM_FMT_STEREO;
1220 via_chan_pcm_fmt (chan, 0);
1225 chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
1227 via_chan_pcm_fmt (chan, 0);
1234 chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
1235 chan->channels = val;
1240 printk (KERN_WARNING PFX "unknown number of channels\n");
1245 DPRINTK ("EXIT, returning %d\n", val);
1249 static int via_chan_set_buffering (struct via_info *card,
1250 struct via_channel *chan, int val)
1254 DPRINTK ("ENTER\n");
1256 /* in both cases the buffer cannot be changed */
1257 if (chan->is_active || chan->is_mapped) {
1262 /* called outside SETFRAGMENT */
1263 /* set defaults or do nothing */
1266 if (chan->frag_size && chan->frag_number)
1271 chan->frag_size = (VIA_DEFAULT_FRAG_TIME * chan->rate * chan->channels
1272 * ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) ? 2 : 1)) / 1000 - 1;
1275 while (chan->frag_size) {
1276 chan->frag_size >>= 1;
1279 chan->frag_size = 1 << shift;
1281 chan->frag_number = (VIA_DEFAULT_BUFFER_TIME / VIA_DEFAULT_FRAG_TIME);
1283 DPRINTK ("setting default values %d %d\n", chan->frag_size, chan->frag_number);
1285 chan->frag_size = 1 << (val & 0xFFFF);
1286 chan->frag_number = (val >> 16) & 0xFFFF;
1288 DPRINTK ("using user values %d %d\n", chan->frag_size, chan->frag_number);
1291 /* quake3 wants frag_number to be a power of two */
1293 while (chan->frag_number) {
1294 chan->frag_number >>= 1;
1297 chan->frag_number = 1 << shift;
1299 if (chan->frag_size > VIA_MAX_FRAG_SIZE)
1300 chan->frag_size = VIA_MAX_FRAG_SIZE;
1301 else if (chan->frag_size < VIA_MIN_FRAG_SIZE)
1302 chan->frag_size = VIA_MIN_FRAG_SIZE;
1304 if (chan->frag_number < VIA_MIN_FRAG_NUMBER)
1305 chan->frag_number = VIA_MIN_FRAG_NUMBER;
1306 if (chan->frag_number > VIA_MAX_FRAG_NUMBER)
1307 chan->frag_number = VIA_MAX_FRAG_NUMBER;
1309 if ((chan->frag_number * chan->frag_size) / PAGE_SIZE > VIA_MAX_BUFFER_DMA_PAGES)
1310 chan->frag_number = (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE) / chan->frag_size;
1313 if (chan->is_record)
1314 atomic_set (&chan->n_frags, 0);
1316 atomic_set (&chan->n_frags, chan->frag_number);
1323 #ifdef VIA_CHAN_DUMP_BUFS
1325 * via_chan_dump_bufs - Display DMA table contents
1326 * @chan: Channel whose DMA table will be displayed
1328 * Debugging function which displays the contents of the
1329 * scatter-gather DMA table for the given channel @chan.
1332 static void via_chan_dump_bufs (struct via_channel *chan)
1336 for (i = 0; i < chan->frag_number; i++) {
1337 DPRINTK ("#%02d: addr=%x, count=%u, flag=%d, eol=%d\n",
1338 i, chan->sgtable[i].addr,
1339 chan->sgtable[i].count & 0x00FFFFFF,
1340 chan->sgtable[i].count & VIA_FLAG ? 1 : 0,
1341 chan->sgtable[i].count & VIA_EOL ? 1 : 0);
1343 DPRINTK ("buf_in_use = %d, nextbuf = %d\n",
1344 atomic_read (&chan->buf_in_use),
1345 atomic_read (&chan->sw_ptr));
1347 #endif /* VIA_CHAN_DUMP_BUFS */
1351 * via_chan_flush_frag - Flush partially-full playback buffer to hardware
1352 * @chan: Channel whose DMA table will be flushed
1354 * Flushes partially-full playback buffer to hardware.
1357 static void via_chan_flush_frag (struct via_channel *chan)
1359 DPRINTK ("ENTER\n");
1361 assert (chan->slop_len > 0);
1363 if (chan->sw_ptr == (chan->frag_number - 1))
1370 assert (atomic_read (&chan->n_frags) > 0);
1371 atomic_dec (&chan->n_frags);
1379 * via_chan_maybe_start - Initiate audio hardware DMA operation
1380 * @chan: Channel whose DMA is to be started
1382 * Initiate DMA operation, if the DMA engine for the given
1383 * channel @chan is not already active.
1386 static inline void via_chan_maybe_start (struct via_channel *chan)
1388 assert (chan->is_active == sg_active(chan->iobase));
1390 DPRINTK ("MAYBE START %s\n", chan->name);
1391 if (!chan->is_active && chan->is_enabled) {
1392 chan->is_active = 1;
1394 DPRINTK ("starting channel %s\n", chan->name);
1399 /****************************************************************
1401 * Interface to ac97-codec module
1407 * via_ac97_wait_idle - Wait until AC97 codec is not busy
1408 * @card: Private info for specified board
1410 * Sleep until the AC97 codec is no longer busy.
1411 * Returns the final value read from the SGD
1412 * register being polled.
1415 static u8 via_ac97_wait_idle (struct via_info *card)
1418 int counter = VIA_COUNTER_LIMIT;
1420 DPRINTK ("ENTER/EXIT\n");
1422 assert (card != NULL);
1423 assert (card->pdev != NULL);
1428 tmp8 = inb (card->baseaddr + 0x83);
1429 } while ((tmp8 & VIA_CR83_BUSY) && (counter-- > 0));
1431 if (tmp8 & VIA_CR83_BUSY)
1432 printk (KERN_WARNING PFX "timeout waiting on AC97 codec\n");
1438 * via_ac97_read_reg - Read AC97 standard register
1439 * @codec: Pointer to generic AC97 codec info
1440 * @reg: Index of AC97 register to be read
1442 * Read the value of a single AC97 codec register,
1443 * as defined by the Intel AC97 specification.
1445 * Defines the standard AC97 read-register operation
1446 * required by the kernel's ac97_codec interface.
1448 * Returns the 16-bit value stored in the specified
1452 static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg)
1455 struct via_info *card;
1458 DPRINTK ("ENTER\n");
1460 assert (codec != NULL);
1461 assert (codec->private_data != NULL);
1463 card = codec->private_data;
1465 spin_lock(&card->ac97_lock);
1467 /* Every time we write to register 80 we cause a transaction.
1468 The only safe way to clear the valid bit is to write it at
1469 the same time as the command */
1470 data = (reg << 16) | VIA_CR80_READ | VIA_CR80_VALID;
1472 outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
1475 for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
1477 if ((((data = inl(card->baseaddr + VIA_BASE0_AC97_CTRL)) &
1478 (VIA_CR80_VALID|VIA_CR80_BUSY)) == VIA_CR80_VALID))
1482 printk (KERN_WARNING PFX "timeout while reading AC97 codec (0x%lX)\n", data);
1486 /* Once the valid bit has become set, we must wait a complete AC97
1487 frame before the data has settled. */
1489 data = (unsigned long) inl (card->baseaddr + VIA_BASE0_AC97_CTRL);
1491 outb (0x02, card->baseaddr + 0x83);
1493 if (((data & 0x007F0000) >> 16) == reg) {
1494 DPRINTK ("EXIT, success, data=0x%lx, retval=0x%lx\n",
1495 data, data & 0x0000FFFF);
1496 spin_unlock(&card->ac97_lock);
1497 return data & 0x0000FFFF;
1500 printk (KERN_WARNING "via82cxxx_audio: not our index: reg=0x%x, newreg=0x%lx\n",
1501 reg, ((data & 0x007F0000) >> 16));
1504 spin_unlock(&card->ac97_lock);
1505 DPRINTK ("EXIT, returning 0\n");
1511 * via_ac97_write_reg - Write AC97 standard register
1512 * @codec: Pointer to generic AC97 codec info
1513 * @reg: Index of AC97 register to be written
1514 * @value: Value to be written to AC97 register
1516 * Write the value of a single AC97 codec register,
1517 * as defined by the Intel AC97 specification.
1519 * Defines the standard AC97 write-register operation
1520 * required by the kernel's ac97_codec interface.
1523 static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value)
1526 struct via_info *card;
1529 DPRINTK ("ENTER\n");
1531 assert (codec != NULL);
1532 assert (codec->private_data != NULL);
1534 card = codec->private_data;
1536 spin_lock(&card->ac97_lock);
1538 data = (reg << 16) + value;
1539 outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
1542 for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
1543 if ((inb (card->baseaddr + 0x83) & VIA_CR83_BUSY) == 0)
1549 printk (KERN_WARNING PFX "timeout after AC97 codec write (0x%X, 0x%X)\n", reg, value);
1552 spin_unlock(&card->ac97_lock);
1557 static int via_mixer_open (struct inode *inode, struct file *file)
1559 int minor = minor(inode->i_rdev);
1560 struct via_info *card;
1561 struct pci_dev *pdev = NULL;
1562 struct pci_driver *drvr;
1564 DPRINTK ("ENTER\n");
1566 while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
1567 drvr = pci_dev_driver (pdev);
1568 if (drvr == &via_driver) {
1569 assert (pci_get_drvdata (pdev) != NULL);
1571 card = pci_get_drvdata (pdev);
1572 if (card->ac97->dev_mixer == minor)
1577 DPRINTK ("EXIT, returning -ENODEV\n");
1581 file->private_data = card->ac97;
1583 DPRINTK ("EXIT, returning 0\n");
1587 static int via_mixer_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
1590 struct ac97_codec *codec = file->private_data;
1591 struct via_info *card;
1592 int nonblock = (file->f_flags & O_NONBLOCK);
1595 DPRINTK ("ENTER\n");
1597 assert (codec != NULL);
1598 card = codec->private_data;
1599 assert (card != NULL);
1601 rc = via_syscall_down (card, nonblock);
1606 * Intercept volume control on 8233 and 8235
1612 case SOUND_MIXER_READ_VOLUME:
1613 return card->mixer_vol;
1614 case SOUND_MIXER_WRITE_VOLUME:
1617 if(get_user(v, (int *)arg))
1622 card->mixer_vol = v;
1627 rc = codec->mixer_ioctl(codec, cmd, arg);
1629 up (&card->syscall_sem);
1632 DPRINTK ("EXIT, returning %d\n", rc);
1637 static struct file_operations via_mixer_fops = {
1638 .owner = THIS_MODULE,
1639 .open = via_mixer_open,
1640 .llseek = no_llseek,
1641 .ioctl = via_mixer_ioctl,
1645 static int __init via_ac97_reset (struct via_info *card)
1647 struct pci_dev *pdev = card->pdev;
1651 DPRINTK ("ENTER\n");
1653 assert (pdev != NULL);
1657 u8 r40,r41,r42,r43,r44,r48;
1658 pci_read_config_byte (card->pdev, 0x40, &r40);
1659 pci_read_config_byte (card->pdev, 0x41, &r41);
1660 pci_read_config_byte (card->pdev, 0x42, &r42);
1661 pci_read_config_byte (card->pdev, 0x43, &r43);
1662 pci_read_config_byte (card->pdev, 0x44, &r44);
1663 pci_read_config_byte (card->pdev, 0x48, &r48);
1664 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
1665 r40,r41,r42,r43,r44,r48);
1667 spin_lock_irq (&card->lock);
1668 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
1669 inb (card->baseaddr + 0x00),
1670 inb (card->baseaddr + 0x01),
1671 inb (card->baseaddr + 0x02),
1672 inl (card->baseaddr + 0x04),
1673 inl (card->baseaddr + 0x0C),
1674 inl (card->baseaddr + 0x80),
1675 inl (card->baseaddr + 0x84));
1676 spin_unlock_irq (&card->lock);
1682 * Reset AC97 controller: enable, disable, enable,
1683 * pausing after each command for good luck. Only
1684 * do this if the codec is not ready, because it causes
1685 * loud pops and such due to such a hard codec reset.
1687 pci_read_config_byte (pdev, VIA_ACLINK_STATUS, &tmp8);
1688 if ((tmp8 & VIA_CR40_AC97_READY) == 0) {
1689 pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
1690 VIA_CR41_AC97_ENABLE |
1691 VIA_CR41_AC97_RESET |
1692 VIA_CR41_AC97_WAKEUP);
1695 pci_write_config_byte (pdev, VIA_ACLINK_CTRL, 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);
1705 /* Make sure VRA is enabled, in case we didn't do a
1706 * complete codec reset, above
1708 pci_read_config_byte (pdev, VIA_ACLINK_CTRL, &tmp8);
1709 if (((tmp8 & VIA_CR41_VRA) == 0) ||
1710 ((tmp8 & VIA_CR41_AC97_ENABLE) == 0) ||
1711 ((tmp8 & VIA_CR41_PCM_ENABLE) == 0) ||
1712 ((tmp8 & VIA_CR41_AC97_RESET) == 0)) {
1713 pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
1714 VIA_CR41_AC97_ENABLE |
1715 VIA_CR41_PCM_ENABLE |
1716 VIA_CR41_VRA | VIA_CR41_AC97_RESET);
1722 #if 0 /* this breaks on K7M */
1723 /* disable legacy stuff */
1724 pci_write_config_byte (pdev, 0x42, 0x00);
1728 /* route FM trap to IRQ, disable FM trap */
1729 pci_write_config_byte (pdev, 0x48, 0x05);
1733 /* disable all codec GPI interrupts */
1734 outl (0, pci_resource_start (pdev, 0) + 0x8C);
1736 /* WARNING: this line is magic. Remove this
1737 * and things break. */
1738 /* enable variable rate */
1739 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1740 if ((tmp16 & 1) == 0)
1741 via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1743 DPRINTK ("EXIT, returning 0\n");
1748 static void via_ac97_codec_wait (struct ac97_codec *codec)
1750 assert (codec->private_data != NULL);
1751 via_ac97_wait_idle (codec->private_data);
1755 static int __init via_ac97_init (struct via_info *card)
1760 DPRINTK ("ENTER\n");
1762 assert (card != NULL);
1764 card->ac97 = ac97_alloc_codec();
1765 if(card->ac97 == NULL)
1768 card->ac97->private_data = card;
1769 card->ac97->codec_read = via_ac97_read_reg;
1770 card->ac97->codec_write = via_ac97_write_reg;
1771 card->ac97->codec_wait = via_ac97_codec_wait;
1773 card->ac97->dev_mixer = register_sound_mixer (&via_mixer_fops, -1);
1774 if (card->ac97->dev_mixer < 0) {
1775 printk (KERN_ERR PFX "unable to register AC97 mixer, aborting\n");
1776 DPRINTK ("EXIT, returning -EIO\n");
1777 ac97_release_codec(card->ac97);
1781 rc = via_ac97_reset (card);
1783 printk (KERN_ERR PFX "unable to reset AC97 codec, aborting\n");
1789 if (ac97_probe_codec (card->ac97) == 0) {
1790 printk (KERN_ERR PFX "unable to probe AC97 codec, aborting\n");
1795 /* enable variable rate */
1796 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1797 via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1800 * If we cannot enable VRA, we have a locked-rate codec.
1801 * We try again to enable VRA before assuming so, however.
1803 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1804 if ((tmp16 & 1) == 0) {
1805 via_ac97_write_reg (card->ac97, AC97_EXTENDED_STATUS, tmp16 | 1);
1806 tmp16 = via_ac97_read_reg (card->ac97, AC97_EXTENDED_STATUS);
1807 if ((tmp16 & 1) == 0) {
1808 card->locked_rate = 1;
1809 printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n");
1813 DPRINTK ("EXIT, returning 0\n");
1817 unregister_sound_mixer (card->ac97->dev_mixer);
1818 DPRINTK ("EXIT, returning %d\n", rc);
1819 ac97_release_codec(card->ac97);
1824 static void via_ac97_cleanup (struct via_info *card)
1826 DPRINTK ("ENTER\n");
1828 assert (card != NULL);
1829 assert (card->ac97->dev_mixer >= 0);
1831 unregister_sound_mixer (card->ac97->dev_mixer);
1832 ac97_release_codec(card->ac97);
1839 /****************************************************************
1841 * Interrupt-related code
1846 * via_intr_channel - handle an interrupt for a single channel
1847 * @chan: handle interrupt for this channel
1849 * This is the "meat" of the interrupt handler,
1850 * containing the actions taken each time an interrupt
1851 * occurs. All communication and coordination with
1852 * userspace takes place here.
1854 * Locking: inside card->lock
1857 static void via_intr_channel (struct via_info *card, struct via_channel *chan)
1862 /* check pertinent bits of status register for action bits */
1863 status = inb (chan->iobase) & (VIA_SGD_FLAG | VIA_SGD_EOL | VIA_SGD_STOPPED);
1867 /* acknowledge any flagged bits ASAP */
1868 outb (status, chan->iobase);
1870 if (!chan->sgtable) /* XXX: temporary solution */
1873 /* grab current h/w ptr value */
1874 n = atomic_read (&chan->hw_ptr);
1876 /* sanity check: make sure our h/w ptr doesn't have a weird value */
1878 assert (n < chan->frag_number);
1881 /* reset SGD data structure in memory to reflect a full buffer,
1882 * and advance the h/w ptr, wrapping around to zero if needed
1884 if (n == (chan->frag_number - 1)) {
1885 chan->sgtable[n].count = cpu_to_le32(chan->frag_size | VIA_EOL);
1886 atomic_set (&chan->hw_ptr, 0);
1888 chan->sgtable[n].count = cpu_to_le32(chan->frag_size | VIA_FLAG);
1889 atomic_inc (&chan->hw_ptr);
1892 /* accounting crap for SNDCTL_DSP_GETxPTR */
1894 chan->bytes += chan->frag_size;
1895 /* FIXME - signed overflow is undefined */
1896 if (chan->bytes < 0) /* handle overflow of 31-bit value */
1897 chan->bytes = chan->frag_size;
1898 /* all following checks only occur when not in mmap(2) mode */
1899 if (!chan->is_mapped)
1901 /* If we are recording, then n_frags represents the number
1902 * of fragments waiting to be handled by userspace.
1903 * If we are playback, then n_frags represents the number
1904 * of fragments remaining to be filled by userspace.
1905 * We increment here. If we reach max number of fragments,
1906 * this indicates an underrun/overrun. For this case under OSS,
1907 * we stop the record/playback process.
1909 if (atomic_read (&chan->n_frags) < chan->frag_number)
1910 atomic_inc (&chan->n_frags);
1911 assert (atomic_read (&chan->n_frags) <= chan->frag_number);
1912 if (atomic_read (&chan->n_frags) == chan->frag_number) {
1913 chan->is_active = 0;
1914 via_chan_stop (chan->iobase);
1917 /* wake up anyone listening to see when interrupts occur */
1918 wake_up_all (&chan->wait);
1920 DPRINTK ("%s intr, status=0x%02X, hwptr=0x%lX, chan->hw_ptr=%d\n",
1921 chan->name, status, (long) inl (chan->iobase + 0x04),
1922 atomic_read (&chan->hw_ptr));
1924 DPRINTK ("%s intr, channel n_frags == %d, missed %d\n", chan->name,
1925 atomic_read (&chan->n_frags), missed);
1929 static irqreturn_t via_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1931 struct via_info *card = dev_id;
1934 /* to minimize interrupt sharing costs, we use the SGD status
1935 * shadow register to check the status of all inputs and
1936 * outputs with a single 32-bit bus read. If no interrupt
1937 * conditions are flagged, we exit immediately
1939 status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW);
1940 if (!(status32 & VIA_INTR_MASK))
1942 #ifdef CONFIG_MIDI_VIA82CXXX
1943 if (card->midi_devc)
1944 uart401intr(irq, card->midi_devc, regs);
1948 DPRINTK ("intr, status32 == 0x%08X\n", status32);
1950 /* synchronize interrupt handling under SMP. this spinlock
1951 * goes away completely on UP
1953 spin_lock (&card->lock);
1955 if (status32 & VIA_INTR_OUT)
1956 via_intr_channel (card, &card->ch_out);
1957 if (status32 & VIA_INTR_IN)
1958 via_intr_channel (card, &card->ch_in);
1959 if (status32 & VIA_INTR_FM)
1960 via_intr_channel (card, &card->ch_fm);
1962 spin_unlock (&card->lock);
1967 static irqreturn_t via_new_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1969 struct via_info *card = dev_id;
1972 /* to minimize interrupt sharing costs, we use the SGD status
1973 * shadow register to check the status of all inputs and
1974 * outputs with a single 32-bit bus read. If no interrupt
1975 * conditions are flagged, we exit immediately
1977 status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW);
1978 if (!(status32 & VIA_NEW_INTR_MASK))
1981 * goes away completely on UP
1983 spin_lock (&card->lock);
1985 via_intr_channel (card, &card->ch_out);
1986 via_intr_channel (card, &card->ch_in);
1987 via_intr_channel (card, &card->ch_fm);
1989 spin_unlock (&card->lock);
1995 * via_interrupt_init - Initialize interrupt handling
1996 * @card: Private info for specified board
1998 * Obtain and reserve IRQ for using in handling audio events.
1999 * Also, disable any IRQ-generating resources, to make sure
2000 * we don't get interrupts before we want them.
2003 static int via_interrupt_init (struct via_info *card)
2007 DPRINTK ("ENTER\n");
2009 assert (card != NULL);
2010 assert (card->pdev != NULL);
2012 /* check for sane IRQ number. can this ever happen? */
2013 if (card->pdev->irq < 2) {
2014 printk (KERN_ERR PFX "insane IRQ %d, aborting\n",
2016 DPRINTK ("EXIT, returning -EIO\n");
2020 /* VIA requires this is done */
2021 pci_write_config_byte(card->pdev, PCI_INTERRUPT_LINE, card->pdev->irq);
2025 /* make sure FM irq is not routed to us */
2026 pci_read_config_byte (card->pdev, VIA_FM_NMI_CTRL, &tmp8);
2027 if ((tmp8 & VIA_CR48_FM_TRAP_TO_NMI) == 0) {
2028 tmp8 |= VIA_CR48_FM_TRAP_TO_NMI;
2029 pci_write_config_byte (card->pdev, VIA_FM_NMI_CTRL, tmp8);
2031 if (request_irq (card->pdev->irq, via_interrupt, SA_SHIRQ, VIA_MODULE_NAME, card)) {
2032 printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n",
2034 DPRINTK ("EXIT, returning -EBUSY\n");
2040 if (request_irq (card->pdev->irq, via_new_interrupt, SA_SHIRQ, VIA_MODULE_NAME, card)) {
2041 printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n",
2043 DPRINTK ("EXIT, returning -EBUSY\n");
2048 DPRINTK ("EXIT, returning 0\n");
2053 /****************************************************************
2059 static struct file_operations via_dsp_fops = {
2062 release: via_dsp_release,
2064 write: via_dsp_write,
2067 ioctl: via_dsp_ioctl,
2072 static int __init via_dsp_init (struct via_info *card)
2076 DPRINTK ("ENTER\n");
2078 assert (card != NULL);
2082 /* turn off legacy features, if not already */
2083 pci_read_config_byte (card->pdev, VIA_FUNC_ENABLE, &tmp8);
2084 if (tmp8 & (VIA_CR42_SB_ENABLE | VIA_CR42_FM_ENABLE)) {
2085 tmp8 &= ~(VIA_CR42_SB_ENABLE | VIA_CR42_FM_ENABLE);
2086 pci_write_config_byte (card->pdev, VIA_FUNC_ENABLE, tmp8);
2090 via_stop_everything (card);
2092 card->dev_dsp = register_sound_dsp (&via_dsp_fops, -1);
2093 if (card->dev_dsp < 0) {
2094 DPRINTK ("EXIT, returning -ENODEV\n");
2097 DPRINTK ("EXIT, returning 0\n");
2102 static void via_dsp_cleanup (struct via_info *card)
2104 DPRINTK ("ENTER\n");
2106 assert (card != NULL);
2107 assert (card->dev_dsp >= 0);
2109 via_stop_everything (card);
2111 unregister_sound_dsp (card->dev_dsp);
2117 static struct page * via_mm_nopage (struct vm_area_struct * vma,
2118 unsigned long address, int write_access)
2120 struct via_info *card = vma->vm_private_data;
2121 struct via_channel *chan = &card->ch_out;
2122 struct page *dmapage;
2123 unsigned long pgoff;
2126 DPRINTK ("ENTER, start %lXh, ofs %lXh, pgoff %ld, addr %lXh, wr %d\n",
2128 address - vma->vm_start,
2129 (address - vma->vm_start) >> PAGE_SHIFT,
2133 if (address > vma->vm_end) {
2134 DPRINTK ("EXIT, returning NOPAGE_SIGBUS\n");
2135 return NOPAGE_SIGBUS; /* Disallow mremap */
2138 DPRINTK ("EXIT, returning NOPAGE_OOM\n");
2139 return NOPAGE_OOM; /* Nothing allocated */
2142 pgoff = vma->vm_pgoff + ((address - vma->vm_start) >> PAGE_SHIFT);
2143 rd = card->ch_in.is_mapped;
2144 wr = card->ch_out.is_mapped;
2148 unsigned long max_bufs = chan->frag_number;
2149 if (rd && wr) max_bufs *= 2;
2150 /* via_dsp_mmap() should ensure this */
2151 assert (pgoff < max_bufs);
2155 /* if full-duplex (read+write) and we have two sets of bufs,
2156 * then the playback buffers come first, sez soundcard.c */
2157 if (pgoff >= chan->page_number) {
2158 pgoff -= chan->page_number;
2159 chan = &card->ch_in;
2161 chan = &card->ch_in;
2163 assert ((((unsigned long)chan->pgtbl[pgoff].cpuaddr) % PAGE_SIZE) == 0);
2165 dmapage = virt_to_page (chan->pgtbl[pgoff].cpuaddr);
2166 DPRINTK ("EXIT, returning page %p for cpuaddr %lXh\n",
2167 dmapage, (unsigned long) chan->pgtbl[pgoff].cpuaddr);
2174 static int via_mm_swapout (struct page *page, struct file *filp)
2178 #endif /* VM_RESERVED */
2181 struct vm_operations_struct via_mm_ops = {
2182 nopage: via_mm_nopage,
2185 swapout: via_mm_swapout,
2190 static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma)
2192 struct via_info *card;
2193 int nonblock = (file->f_flags & O_NONBLOCK);
2194 int rc = -EINVAL, rd=0, wr=0;
2195 unsigned long max_size, size, start, offset;
2197 assert (file != NULL);
2198 assert (vma != NULL);
2199 card = file->private_data;
2200 assert (card != NULL);
2202 DPRINTK ("ENTER, start %lXh, size %ld, pgoff %ld\n",
2204 vma->vm_end - vma->vm_start,
2208 if (vma->vm_flags & VM_READ) {
2210 via_chan_set_buffering(card, &card->ch_in, -1);
2211 via_chan_buffer_init (card, &card->ch_in);
2212 max_size += card->ch_in.page_number << PAGE_SHIFT;
2214 if (vma->vm_flags & VM_WRITE) {
2216 via_chan_set_buffering(card, &card->ch_out, -1);
2217 via_chan_buffer_init (card, &card->ch_out);
2218 max_size += card->ch_out.page_number << PAGE_SHIFT;
2221 start = vma->vm_start;
2222 offset = (vma->vm_pgoff << PAGE_SHIFT);
2223 size = vma->vm_end - vma->vm_start;
2225 /* some basic size/offset sanity checks */
2226 if (size > max_size)
2228 if (offset > max_size - size)
2231 rc = via_syscall_down (card, nonblock);
2234 vma->vm_ops = &via_mm_ops;
2235 vma->vm_private_data = card;
2238 vma->vm_flags |= VM_RESERVED;
2242 card->ch_in.is_mapped = 1;
2244 card->ch_out.is_mapped = 1;
2246 up (&card->syscall_sem);
2250 DPRINTK ("EXIT, returning %d\n", rc);
2255 static ssize_t via_dsp_do_read (struct via_info *card,
2256 char *userbuf, size_t count,
2259 DECLARE_WAITQUEUE(wait, current);
2260 const char *orig_userbuf = userbuf;
2261 struct via_channel *chan = &card->ch_in;
2266 /* if SGD has not yet been started, start it */
2267 via_chan_maybe_start (chan);
2270 /* just to be a nice neighbor */
2272 * But also to ourselves, release semaphore if we do so */
2273 if (need_resched()) {
2274 up(&card->syscall_sem);
2276 ret = via_syscall_down (card, nonblock);
2281 /* grab current channel software pointer. In the case of
2282 * recording, this is pointing to the next buffer that
2283 * will receive data from the audio hardware.
2287 /* n_frags represents the number of fragments waiting
2288 * to be copied to userland. sleep until at least
2289 * one buffer has been read from the audio hardware.
2291 add_wait_queue(&chan->wait, &wait);
2293 __set_current_state(TASK_INTERRUPTIBLE);
2294 tmp = atomic_read (&chan->n_frags);
2296 assert (tmp <= chan->frag_number);
2299 if (nonblock || !chan->is_active) {
2304 up(&card->syscall_sem);
2306 DPRINTK ("Sleeping on block %d\n", n);
2309 ret = via_syscall_down (card, nonblock);
2313 if (signal_pending (current)) {
2318 set_current_state(TASK_RUNNING);
2319 remove_wait_queue(&chan->wait, &wait);
2323 /* Now that we have a buffer we can read from, send
2324 * as much as sample data possible to userspace.
2326 while ((count > 0) && (chan->slop_len < chan->frag_size)) {
2327 size_t slop_left = chan->frag_size - chan->slop_len;
2328 void *base = chan->pgtbl[n / (PAGE_SIZE / chan->frag_size)].cpuaddr;
2329 unsigned ofs = (n % (PAGE_SIZE / chan->frag_size)) * chan->frag_size;
2331 size = (count < slop_left) ? count : slop_left;
2332 if (copy_to_user (userbuf,
2333 base + ofs + chan->slop_len,
2340 chan->slop_len += size;
2344 /* If we didn't copy the buffer completely to userspace,
2347 if (chan->slop_len < chan->frag_size)
2351 * If we get to this point, we copied one buffer completely
2352 * to userspace, give the buffer back to the hardware.
2355 /* advance channel software pointer to point to
2356 * the next buffer from which we will copy
2358 if (chan->sw_ptr == (chan->frag_number - 1))
2363 /* mark one less buffer waiting to be processed */
2364 assert (atomic_read (&chan->n_frags) > 0);
2365 atomic_dec (&chan->n_frags);
2367 /* we are at a block boundary, there is no fragment data */
2370 DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
2371 n, chan->sw_ptr, atomic_read (&chan->n_frags));
2373 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2374 inb (card->baseaddr + 0x00),
2375 inb (card->baseaddr + 0x01),
2376 inb (card->baseaddr + 0x02),
2377 inl (card->baseaddr + 0x04),
2378 inl (card->baseaddr + 0x0C),
2379 inl (card->baseaddr + 0x80),
2380 inl (card->baseaddr + 0x84));
2383 goto handle_one_block;
2386 return (userbuf != orig_userbuf) ? (userbuf - orig_userbuf) : ret;
2390 static ssize_t via_dsp_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
2392 struct via_info *card;
2393 int nonblock = (file->f_flags & O_NONBLOCK);
2396 DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
2397 file, buffer, count, ppos ? ((unsigned long)*ppos) : 0);
2399 assert (file != NULL);
2400 card = file->private_data;
2401 assert (card != NULL);
2403 if (ppos != &file->f_pos) {
2404 DPRINTK ("EXIT, returning -ESPIPE\n");
2408 rc = via_syscall_down (card, nonblock);
2411 if (card->ch_in.is_mapped) {
2416 via_chan_set_buffering(card, &card->ch_in, -1);
2417 rc = via_chan_buffer_init (card, &card->ch_in);
2422 rc = via_dsp_do_read (card, buffer, count, nonblock);
2425 up (&card->syscall_sem);
2427 DPRINTK ("EXIT, returning %ld\n",(long) rc);
2432 static ssize_t via_dsp_do_write (struct via_info *card,
2433 const char *userbuf, size_t count,
2436 DECLARE_WAITQUEUE(wait, current);
2437 const char *orig_userbuf = userbuf;
2438 struct via_channel *chan = &card->ch_out;
2439 volatile struct via_sgd_table *sgtable = chan->sgtable;
2445 /* just to be a nice neighbor */
2447 * But also to ourselves, release semaphore if we do so */
2448 if (need_resched()) {
2449 up(&card->syscall_sem);
2451 ret = via_syscall_down (card, nonblock);
2456 /* grab current channel fragment pointer. In the case of
2457 * playback, this is pointing to the next fragment that
2458 * should receive data from userland.
2462 /* n_frags represents the number of fragments remaining
2463 * to be filled by userspace. Sleep until
2464 * at least one fragment is available for our use.
2466 add_wait_queue(&chan->wait, &wait);
2468 __set_current_state(TASK_INTERRUPTIBLE);
2469 tmp = atomic_read (&chan->n_frags);
2471 assert (tmp <= chan->frag_number);
2474 if (nonblock || !chan->is_active) {
2479 up(&card->syscall_sem);
2481 DPRINTK ("Sleeping on page %d, tmp==%d, ir==%d\n", n, tmp, chan->is_record);
2484 ret = via_syscall_down (card, nonblock);
2488 if (signal_pending (current)) {
2493 set_current_state(TASK_RUNNING);
2494 remove_wait_queue(&chan->wait, &wait);
2498 /* Now that we have at least one fragment we can write to, fill the buffer
2499 * as much as possible with data from userspace.
2501 while ((count > 0) && (chan->slop_len < chan->frag_size)) {
2502 size_t slop_left = chan->frag_size - chan->slop_len;
2504 size = (count < slop_left) ? count : slop_left;
2505 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,
2512 chan->slop_len += size;
2516 /* If we didn't fill up the buffer with data, stop now.
2517 * Put a 'stop' marker in the DMA table too, to tell the
2518 * audio hardware to stop if it gets here.
2520 if (chan->slop_len < chan->frag_size) {
2521 sgtable[n].count = cpu_to_le32 (chan->slop_len | VIA_EOL | VIA_STOP);
2526 * If we get to this point, we have filled a buffer with
2527 * audio data, flush the buffer to audio hardware.
2530 /* Record the true size for the audio hardware to notice */
2531 if (n == (chan->frag_number - 1))
2532 sgtable[n].count = cpu_to_le32 (chan->frag_size | VIA_EOL);
2534 sgtable[n].count = cpu_to_le32 (chan->frag_size | VIA_FLAG);
2536 /* advance channel software pointer to point to
2537 * the next buffer we will fill with data
2539 if (chan->sw_ptr == (chan->frag_number - 1))
2544 /* mark one less buffer as being available for userspace consumption */
2545 assert (atomic_read (&chan->n_frags) > 0);
2546 atomic_dec (&chan->n_frags);
2548 /* we are at a block boundary, there is no fragment data */
2551 /* if SGD has not yet been started, start it */
2552 via_chan_maybe_start (chan);
2554 DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
2555 n, chan->sw_ptr, atomic_read (&chan->n_frags));
2557 DPRINTK ("regs==S=%02X C=%02X TP=%02X BP=%08X RT=%08X SG=%08X CC=%08X SS=%08X\n",
2558 inb (card->baseaddr + 0x00),
2559 inb (card->baseaddr + 0x01),
2560 inb (card->baseaddr + 0x02),
2561 inl (card->baseaddr + 0x04),
2562 inl (card->baseaddr + 0x08),
2563 inl (card->baseaddr + 0x0C),
2564 inl (card->baseaddr + 0x80),
2565 inl (card->baseaddr + 0x84));
2568 goto handle_one_block;
2571 if (userbuf - orig_userbuf)
2572 return userbuf - orig_userbuf;
2578 static ssize_t via_dsp_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
2580 struct via_info *card;
2582 int nonblock = (file->f_flags & O_NONBLOCK);
2584 DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
2585 file, buffer, count, ppos ? ((unsigned long)*ppos) : 0);
2587 assert (file != NULL);
2588 card = file->private_data;
2589 assert (card != NULL);
2591 if (ppos != &file->f_pos) {
2592 DPRINTK ("EXIT, returning -ESPIPE\n");
2596 rc = via_syscall_down (card, nonblock);
2599 if (card->ch_out.is_mapped) {
2604 via_chan_set_buffering(card, &card->ch_out, -1);
2605 rc = via_chan_buffer_init (card, &card->ch_out);
2610 rc = via_dsp_do_write (card, buffer, count, nonblock);
2613 up (&card->syscall_sem);
2615 DPRINTK ("EXIT, returning %ld\n",(long) rc);
2620 static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait)
2622 struct via_info *card;
2623 struct via_channel *chan;
2624 unsigned int mask = 0;
2626 DPRINTK ("ENTER\n");
2628 assert (file != NULL);
2629 card = file->private_data;
2630 assert (card != NULL);
2632 if (file->f_mode & FMODE_READ) {
2633 chan = &card->ch_in;
2634 if (sg_active (chan->iobase))
2635 poll_wait(file, &chan->wait, wait);
2636 if (atomic_read (&chan->n_frags) > 0)
2637 mask |= POLLIN | POLLRDNORM;
2640 if (file->f_mode & FMODE_WRITE) {
2641 chan = &card->ch_out;
2642 if (sg_active (chan->iobase))
2643 poll_wait(file, &chan->wait, wait);
2644 if (atomic_read (&chan->n_frags) > 0)
2645 mask |= POLLOUT | POLLWRNORM;
2648 DPRINTK ("EXIT, returning %u\n", mask);
2654 * via_dsp_drain_playback - sleep until all playback samples are flushed
2655 * @card: Private info for specified board
2656 * @chan: Channel to drain
2657 * @nonblock: boolean, non-zero if O_NONBLOCK is set
2659 * Sleeps until all playback has been flushed to the audio
2662 * Locking: inside card->syscall_sem
2665 static int via_dsp_drain_playback (struct via_info *card,
2666 struct via_channel *chan, int nonblock)
2668 DECLARE_WAITQUEUE(wait, current);
2671 DPRINTK ("ENTER, nonblock = %d\n", nonblock);
2673 if (chan->slop_len > 0)
2674 via_chan_flush_frag (chan);
2676 if (atomic_read (&chan->n_frags) == chan->frag_number)
2679 via_chan_maybe_start (chan);
2681 add_wait_queue(&chan->wait, &wait);
2683 DPRINTK ("FRAGS %d FRAGNUM %d\n", atomic_read(&chan->n_frags), chan->frag_number);
2684 __set_current_state(TASK_INTERRUPTIBLE);
2685 if (atomic_read (&chan->n_frags) >= chan->frag_number)
2689 DPRINTK ("EXIT, returning -EAGAIN\n");
2696 u8 r40,r41,r42,r43,r44,r48;
2697 pci_read_config_byte (card->pdev, 0x40, &r40);
2698 pci_read_config_byte (card->pdev, 0x41, &r41);
2699 pci_read_config_byte (card->pdev, 0x42, &r42);
2700 pci_read_config_byte (card->pdev, 0x43, &r43);
2701 pci_read_config_byte (card->pdev, 0x44, &r44);
2702 pci_read_config_byte (card->pdev, 0x48, &r48);
2703 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
2704 r40,r41,r42,r43,r44,r48);
2706 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2707 inb (card->baseaddr + 0x00),
2708 inb (card->baseaddr + 0x01),
2709 inb (card->baseaddr + 0x02),
2710 inl (card->baseaddr + 0x04),
2711 inl (card->baseaddr + 0x0C),
2712 inl (card->baseaddr + 0x80),
2713 inl (card->baseaddr + 0x84));
2716 if (!chan->is_active)
2717 printk (KERN_ERR "sleeping but not active\n");
2720 up(&card->syscall_sem);
2722 DPRINTK ("sleeping, nbufs=%d\n", atomic_read (&chan->n_frags));
2725 if ((ret = via_syscall_down (card, nonblock)))
2728 if (signal_pending (current)) {
2729 DPRINTK ("EXIT, returning -ERESTARTSYS\n");
2734 set_current_state(TASK_RUNNING);
2735 remove_wait_queue(&chan->wait, &wait);
2739 u8 r40,r41,r42,r43,r44,r48;
2740 pci_read_config_byte (card->pdev, 0x40, &r40);
2741 pci_read_config_byte (card->pdev, 0x41, &r41);
2742 pci_read_config_byte (card->pdev, 0x42, &r42);
2743 pci_read_config_byte (card->pdev, 0x43, &r43);
2744 pci_read_config_byte (card->pdev, 0x44, &r44);
2745 pci_read_config_byte (card->pdev, 0x48, &r48);
2746 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
2747 r40,r41,r42,r43,r44,r48);
2749 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2750 inb (card->baseaddr + 0x00),
2751 inb (card->baseaddr + 0x01),
2752 inb (card->baseaddr + 0x02),
2753 inl (card->baseaddr + 0x04),
2754 inl (card->baseaddr + 0x0C),
2755 inl (card->baseaddr + 0x80),
2756 inl (card->baseaddr + 0x84));
2758 DPRINTK ("final nbufs=%d\n", atomic_read (&chan->n_frags));
2763 DPRINTK ("EXIT, returning %d\n", ret);
2769 * via_dsp_ioctl_space - get information about channel buffering
2770 * @card: Private info for specified board
2771 * @chan: pointer to channel-specific info
2772 * @arg: user buffer for returned information
2774 * Handles SNDCTL_DSP_GETISPACE and SNDCTL_DSP_GETOSPACE.
2776 * Locking: inside card->syscall_sem
2779 static int via_dsp_ioctl_space (struct via_info *card,
2780 struct via_channel *chan,
2783 audio_buf_info info;
2785 via_chan_set_buffering(card, chan, -1);
2787 info.fragstotal = chan->frag_number;
2788 info.fragsize = chan->frag_size;
2790 /* number of full fragments we can read/write without blocking */
2791 info.fragments = atomic_read (&chan->n_frags);
2793 if ((chan->slop_len % chan->frag_size > 0) && (info.fragments > 0))
2796 /* number of bytes that can be read or written immediately
2799 info.bytes = (info.fragments * chan->frag_size);
2800 if (chan->slop_len % chan->frag_size > 0)
2801 info.bytes += chan->frag_size - (chan->slop_len % chan->frag_size);
2803 DPRINTK ("EXIT, returning fragstotal=%d, fragsize=%d, fragments=%d, bytes=%d\n",
2809 return copy_to_user (arg, &info, sizeof (info))?-EFAULT:0;
2814 * via_dsp_ioctl_ptr - get information about hardware buffer ptr
2815 * @card: Private info for specified board
2816 * @chan: pointer to channel-specific info
2817 * @arg: user buffer for returned information
2819 * Handles SNDCTL_DSP_GETIPTR and SNDCTL_DSP_GETOPTR.
2821 * Locking: inside card->syscall_sem
2824 static int via_dsp_ioctl_ptr (struct via_info *card,
2825 struct via_channel *chan,
2830 spin_lock_irq (&card->lock);
2832 info.bytes = chan->bytes;
2833 info.blocks = chan->n_irqs;
2836 spin_unlock_irq (&card->lock);
2838 if (chan->is_active) {
2839 unsigned long extra;
2840 info.ptr = atomic_read (&chan->hw_ptr) * chan->frag_size;
2841 extra = chan->frag_size - via_sg_offset(chan);
2843 info.bytes += extra;
2848 DPRINTK ("EXIT, returning bytes=%d, blocks=%d, ptr=%d\n",
2853 return copy_to_user (arg, &info, sizeof (info))?-EFAULT:0;
2857 static int via_dsp_ioctl_trigger (struct via_channel *chan, int val)
2859 int enable, do_something;
2861 if (chan->is_record)
2862 enable = (val & PCM_ENABLE_INPUT);
2864 enable = (val & PCM_ENABLE_OUTPUT);
2866 if (!chan->is_enabled && enable) {
2868 } else if (chan->is_enabled && !enable) {
2874 DPRINTK ("enable=%d, do_something=%d\n",
2875 enable, do_something);
2877 if (chan->is_active && do_something)
2880 if (do_something == 1) {
2881 chan->is_enabled = 1;
2882 via_chan_maybe_start (chan);
2883 DPRINTK ("Triggering input\n");
2886 else if (do_something == -1) {
2887 chan->is_enabled = 0;
2888 DPRINTK ("Setup input trigger\n");
2895 static int via_dsp_ioctl (struct inode *inode, struct file *file,
2896 unsigned int cmd, unsigned long arg)
2898 int rc, rd=0, wr=0, val=0;
2899 struct via_info *card;
2900 struct via_channel *chan;
2901 int nonblock = (file->f_flags & O_NONBLOCK);
2903 assert (file != NULL);
2904 card = file->private_data;
2905 assert (card != NULL);
2907 if (file->f_mode & FMODE_WRITE)
2909 if (file->f_mode & FMODE_READ)
2912 rc = via_syscall_down (card, nonblock);
2919 /* OSS API version. XXX unverified */
2920 case OSS_GETVERSION:
2921 DPRINTK ("ioctl OSS_GETVERSION, EXIT, returning SOUND_VERSION\n");
2922 rc = put_user (SOUND_VERSION, (int *)arg);
2925 /* list of supported PCM data formats */
2926 case SNDCTL_DSP_GETFMTS:
2927 DPRINTK ("DSP_GETFMTS, EXIT, returning AFMT U8|S16_LE\n");
2928 rc = put_user (AFMT_U8 | AFMT_S16_LE, (int *)arg);
2931 /* query or set current channel's PCM data format */
2932 case SNDCTL_DSP_SETFMT:
2933 if (get_user(val, (int *)arg)) {
2937 DPRINTK ("DSP_SETFMT, val==%d\n", val);
2938 if (val != AFMT_QUERY) {
2942 rc = via_chan_set_fmt (card, &card->ch_in, val);
2945 rc = via_chan_set_fmt (card, &card->ch_out, val);
2952 if ((rd && (card->ch_in.pcm_fmt & VIA_PCM_FMT_16BIT)) ||
2953 (wr && (card->ch_out.pcm_fmt & VIA_PCM_FMT_16BIT)))
2958 DPRINTK ("SETFMT EXIT, returning %d\n", val);
2959 rc = put_user (val, (int *)arg);
2962 /* query or set number of channels (1=mono, 2=stereo, 4/6 for multichannel) */
2963 case SNDCTL_DSP_CHANNELS:
2964 if (get_user(val, (int *)arg)) {
2968 DPRINTK ("DSP_CHANNELS, val==%d\n", val);
2973 rc = via_chan_set_stereo (card, &card->ch_in, val);
2976 rc = via_chan_set_stereo (card, &card->ch_out, val);
2984 val = card->ch_in.channels;
2986 val = card->ch_out.channels;
2988 DPRINTK ("CHANNELS EXIT, returning %d\n", val);
2989 rc = put_user (val, (int *)arg);
2992 /* enable (val is not zero) or disable (val == 0) stereo */
2993 case SNDCTL_DSP_STEREO:
2994 if (get_user(val, (int *)arg)) {
2998 DPRINTK ("DSP_STEREO, val==%d\n", val);
3002 rc = via_chan_set_stereo (card, &card->ch_in, val ? 2 : 1);
3004 rc = via_chan_set_stereo (card, &card->ch_out, val ? 2 : 1);
3011 DPRINTK ("STEREO EXIT, returning %d\n", val);
3012 rc = put_user(val, (int *) arg);
3015 /* query or set sampling rate */
3016 case SNDCTL_DSP_SPEED:
3017 if (get_user(val, (int *)arg)) {
3021 DPRINTK ("DSP_SPEED, val==%d\n", val);
3030 rc = via_chan_set_speed (card, &card->ch_in, val);
3032 rc = via_chan_set_speed (card, &card->ch_out, val);
3040 val = card->ch_in.rate;
3042 val = card->ch_out.rate;
3046 DPRINTK ("SPEED EXIT, returning %d\n", val);
3047 rc = put_user (val, (int *)arg);
3050 /* wait until all buffers have been played, and then stop device */
3051 case SNDCTL_DSP_SYNC:
3052 DPRINTK ("DSP_SYNC\n");
3055 DPRINTK ("SYNC EXIT (after calling via_dsp_drain_playback)\n");
3056 rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
3060 /* stop recording/playback immediately */
3061 case SNDCTL_DSP_RESET:
3062 DPRINTK ("DSP_RESET\n");
3064 via_chan_clear (card, &card->ch_in);
3065 card->ch_in.frag_number = 0;
3066 card->ch_in.frag_size = 0;
3067 atomic_set(&card->ch_in.n_frags, 0);
3071 via_chan_clear (card, &card->ch_out);
3072 card->ch_out.frag_number = 0;
3073 card->ch_out.frag_size = 0;
3074 atomic_set(&card->ch_out.n_frags, 0);
3080 case SNDCTL_DSP_NONBLOCK:
3081 file->f_flags |= O_NONBLOCK;
3085 /* obtain bitmask of device capabilities, such as mmap, full duplex, etc. */
3086 case SNDCTL_DSP_GETCAPS:
3087 DPRINTK ("DSP_GETCAPS\n");
3088 rc = put_user(VIA_DSP_CAP, (int *)arg);
3091 /* obtain buffer fragment size */
3092 case SNDCTL_DSP_GETBLKSIZE:
3093 DPRINTK ("DSP_GETBLKSIZE\n");
3096 via_chan_set_buffering(card, &card->ch_in, -1);
3097 rc = put_user(card->ch_in.frag_size, (int *)arg);
3099 via_chan_set_buffering(card, &card->ch_out, -1);
3100 rc = put_user(card->ch_out.frag_size, (int *)arg);
3104 /* obtain information about input buffering */
3105 case SNDCTL_DSP_GETISPACE:
3106 DPRINTK ("DSP_GETISPACE\n");
3108 rc = via_dsp_ioctl_space (card, &card->ch_in, (void*) arg);
3111 /* obtain information about output buffering */
3112 case SNDCTL_DSP_GETOSPACE:
3113 DPRINTK ("DSP_GETOSPACE\n");
3115 rc = via_dsp_ioctl_space (card, &card->ch_out, (void*) arg);
3118 /* obtain information about input hardware pointer */
3119 case SNDCTL_DSP_GETIPTR:
3120 DPRINTK ("DSP_GETIPTR\n");
3122 rc = via_dsp_ioctl_ptr (card, &card->ch_in, (void*) arg);
3125 /* obtain information about output hardware pointer */
3126 case SNDCTL_DSP_GETOPTR:
3127 DPRINTK ("DSP_GETOPTR\n");
3129 rc = via_dsp_ioctl_ptr (card, &card->ch_out, (void*) arg);
3132 /* return number of bytes remaining to be played by DMA engine */
3133 case SNDCTL_DSP_GETODELAY:
3135 DPRINTK ("DSP_GETODELAY\n");
3137 chan = &card->ch_out;
3142 if (chan->is_active) {
3144 val = chan->frag_number - atomic_read (&chan->n_frags);
3149 val *= chan->frag_size;
3150 val -= chan->frag_size - via_sg_offset(chan);
3152 val += chan->slop_len % chan->frag_size;
3156 assert (val <= (chan->frag_size * chan->frag_number));
3158 DPRINTK ("GETODELAY EXIT, val = %d bytes\n", val);
3159 rc = put_user (val, (int *)arg);
3163 /* handle the quick-start of a channel,
3164 * or the notification that a quick-start will
3165 * occur in the future
3167 case SNDCTL_DSP_SETTRIGGER:
3168 if (get_user(val, (int *)arg)) {
3172 DPRINTK ("DSP_SETTRIGGER, rd=%d, wr=%d, act=%d/%d, en=%d/%d\n",
3173 rd, wr, card->ch_in.is_active, card->ch_out.is_active,
3174 card->ch_in.is_enabled, card->ch_out.is_enabled);
3179 rc = via_dsp_ioctl_trigger (&card->ch_in, val);
3182 rc = via_dsp_ioctl_trigger (&card->ch_out, val);
3186 case SNDCTL_DSP_GETTRIGGER:
3188 if ((file->f_mode & FMODE_READ) && card->ch_in.is_enabled)
3189 val |= PCM_ENABLE_INPUT;
3190 if ((file->f_mode & FMODE_WRITE) && card->ch_out.is_enabled)
3191 val |= PCM_ENABLE_OUTPUT;
3192 rc = put_user(val, (int *)arg);
3195 /* Enable full duplex. Since we do this as soon as we are opened
3196 * with O_RDWR, this is mainly a no-op that always returns success.
3198 case SNDCTL_DSP_SETDUPLEX:
3199 DPRINTK ("DSP_SETDUPLEX\n");
3205 /* set fragment size. implemented as a successful no-op for now */
3206 case SNDCTL_DSP_SETFRAGMENT:
3207 if (get_user(val, (int *)arg)) {
3211 DPRINTK ("DSP_SETFRAGMENT, val==%d\n", val);
3214 rc = via_chan_set_buffering(card, &card->ch_in, val);
3217 rc = via_chan_set_buffering(card, &card->ch_out, val);
3219 DPRINTK ("SNDCTL_DSP_SETFRAGMENT (fragshift==0x%04X (%d), maxfrags==0x%04X (%d))\n",
3222 (val >> 16) & 0xFFFF,
3223 (val >> 16) & 0xFFFF);
3228 /* inform device of an upcoming pause in input (or output). */
3229 case SNDCTL_DSP_POST:
3230 DPRINTK ("DSP_POST\n");
3232 if (card->ch_out.slop_len > 0)
3233 via_chan_flush_frag (&card->ch_out);
3234 via_chan_maybe_start (&card->ch_out);
3240 /* not implemented */
3242 DPRINTK ("unhandled ioctl, cmd==%u, arg==%p\n",
3247 up (&card->syscall_sem);
3248 DPRINTK ("EXIT, returning %d\n", rc);
3253 static int via_dsp_open (struct inode *inode, struct file *file)
3255 int minor = minor(inode->i_rdev);
3256 struct via_info *card;
3257 struct pci_dev *pdev = NULL;
3258 struct via_channel *chan;
3259 struct pci_driver *drvr;
3260 int nonblock = (file->f_flags & O_NONBLOCK);
3262 DPRINTK ("ENTER, minor=%d, file->f_mode=0x%x\n", minor, file->f_mode);
3264 if (!(file->f_mode & (FMODE_READ | FMODE_WRITE))) {
3265 DPRINTK ("EXIT, returning -EINVAL\n");
3270 while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
3271 drvr = pci_dev_driver (pdev);
3272 if (drvr == &via_driver) {
3273 assert (pci_get_drvdata (pdev) != NULL);
3275 card = pci_get_drvdata (pdev);
3276 DPRINTK ("dev_dsp = %d, minor = %d, assn = %d\n",
3277 card->dev_dsp, minor,
3278 (card->dev_dsp ^ minor) & ~0xf);
3280 if (((card->dev_dsp ^ minor) & ~0xf) == 0)
3285 DPRINTK ("no matching %s found\n", card ? "minor" : "driver");
3290 if (down_trylock (&card->open_sem)) {
3291 DPRINTK ("EXIT, returning -EAGAIN\n");
3295 if (down_interruptible (&card->open_sem)) {
3296 DPRINTK ("EXIT, returning -ERESTARTSYS\n");
3297 return -ERESTARTSYS;
3301 file->private_data = card;
3302 DPRINTK ("file->f_mode == 0x%x\n", file->f_mode);
3304 /* handle input from analog source */
3305 if (file->f_mode & FMODE_READ) {
3306 chan = &card->ch_in;
3308 via_chan_init (card, chan);
3310 /* why is this forced to 16-bit stereo in all drivers? */
3311 chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
3314 // TO DO - use FIFO: via_capture_fifo(card, 1);
3315 via_chan_pcm_fmt (chan, 0);
3316 via_set_rate (card->ac97, chan, 44100);
3319 /* handle output to analog source */
3320 if (file->f_mode & FMODE_WRITE) {
3321 chan = &card->ch_out;
3323 via_chan_init (card, chan);
3325 if (file->f_mode & FMODE_READ) {
3326 /* if in duplex mode make the recording and playback channels
3327 have the same settings */
3328 chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
3330 via_chan_pcm_fmt (chan, 0);
3331 via_set_rate (card->ac97, chan, 44100);
3333 if ((minor & 0xf) == SND_DEV_DSP16) {
3334 chan->pcm_fmt = VIA_PCM_FMT_16BIT;
3335 via_chan_pcm_fmt (chan, 0);
3336 via_set_rate (card->ac97, chan, 44100);
3338 via_chan_pcm_fmt (chan, 1);
3339 via_set_rate (card->ac97, chan, 8000);
3344 DPRINTK ("EXIT, returning 0\n");
3349 static int via_dsp_release(struct inode *inode, struct file *file)
3351 struct via_info *card;
3352 int nonblock = (file->f_flags & O_NONBLOCK);
3355 DPRINTK ("ENTER\n");
3357 assert (file != NULL);
3358 card = file->private_data;
3359 assert (card != NULL);
3361 rc = via_syscall_down (card, nonblock);
3363 DPRINTK ("EXIT (syscall_down error), rc=%d\n", rc);
3367 if (file->f_mode & FMODE_WRITE) {
3368 rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
3369 if (rc && rc != ERESTARTSYS) /* Nobody needs to know about ^C */
3370 printk (KERN_DEBUG "via_audio: ignoring drain playback error %d\n", rc);
3372 via_chan_free (card, &card->ch_out);
3373 via_chan_buffer_free(card, &card->ch_out);
3376 if (file->f_mode & FMODE_READ) {
3377 via_chan_free (card, &card->ch_in);
3378 via_chan_buffer_free (card, &card->ch_in);
3381 up (&card->syscall_sem);
3382 up (&card->open_sem);
3384 DPRINTK ("EXIT, returning 0\n");
3389 /****************************************************************
3391 * Chip setup and kernel registration
3396 static int __init via_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
3398 #ifdef CONFIG_MIDI_VIA82CXXX
3402 struct via_info *card;
3403 static int printed_version = 0;
3405 DPRINTK ("ENTER\n");
3407 if (printed_version++ == 0)
3408 printk (KERN_INFO "Via 686a/8233/8235 audio driver " VIA_VERSION "\n");
3410 rc = pci_enable_device (pdev);
3415 rc = pci_request_regions (pdev, "via82cxxx_audio");
3417 goto err_out_disable;
3419 card = kmalloc (sizeof (*card), GFP_KERNEL);
3421 printk (KERN_ERR PFX "out of memory, aborting\n");
3426 pci_set_drvdata (pdev, card);
3428 memset (card, 0, sizeof (*card));
3430 card->baseaddr = pci_resource_start (pdev, 0);
3431 card->card_num = via_num_cards++;
3432 spin_lock_init (&card->lock);
3433 spin_lock_init (&card->ac97_lock);
3434 init_MUTEX (&card->syscall_sem);
3435 init_MUTEX (&card->open_sem);
3437 /* we must init these now, in case the intr handler needs them */
3438 via_chan_init_defaults (card, &card->ch_out);
3439 via_chan_init_defaults (card, &card->ch_in);
3440 via_chan_init_defaults (card, &card->ch_fm);
3442 /* if BAR 2 is present, chip is Rev H or later,
3443 * which means it has a few extra features */
3444 if (pci_resource_start (pdev, 2) > 0)
3447 /* Overkill for now, but more flexible done right */
3449 card->intmask = id->driver_data;
3450 card->legacy = !card->intmask;
3451 card->sixchannel = id->driver_data;
3453 if(card->sixchannel)
3454 printk(KERN_INFO PFX "Six channel audio available\n");
3455 if (pdev->irq < 1) {
3456 printk (KERN_ERR PFX "invalid PCI IRQ %d, aborting\n", pdev->irq);
3461 if (!(pci_resource_flags (pdev, 0) & IORESOURCE_IO)) {
3462 printk (KERN_ERR PFX "unable to locate I/O resources, aborting\n");
3467 pci_set_master(pdev);
3470 * init AC97 mixer and codec
3472 rc = via_ac97_init (card);
3474 printk (KERN_ERR PFX "AC97 init failed, aborting\n");
3481 rc = via_dsp_init (card);
3483 printk (KERN_ERR PFX "DSP device init failed, aborting\n");
3484 goto err_out_have_mixer;
3488 * per-card /proc info
3490 rc = via_card_init_proc (card);
3492 printk (KERN_ERR PFX "card-specific /proc init failed, aborting\n");
3493 goto err_out_have_dsp;
3497 * init and turn on interrupts, as the last thing we do
3499 rc = via_interrupt_init (card);
3501 printk (KERN_ERR PFX "interrupt init failed, aborting\n");
3502 goto err_out_have_proc;
3505 printk (KERN_INFO PFX "board #%d at 0x%04lX, IRQ %d\n",
3506 card->card_num + 1, card->baseaddr, pdev->irq);
3508 #ifdef CONFIG_MIDI_VIA82CXXX
3509 /* Disable by default */
3510 card->midi_info.io_base = 0;
3514 pci_read_config_byte (pdev, 0x42, &r42);
3515 /* Disable MIDI interrupt */
3516 pci_write_config_byte (pdev, 0x42, r42 | VIA_CR42_MIDI_IRQMASK);
3517 if (r42 & VIA_CR42_MIDI_ENABLE)
3519 if (r42 & VIA_CR42_MIDI_PNP) /* Address selected by iobase 2 - not tested */
3520 card->midi_info.io_base = pci_resource_start (pdev, 2);
3521 else /* Address selected by byte 0x43 */
3524 pci_read_config_byte (pdev, 0x43, &r43);
3525 card->midi_info.io_base = 0x300 + ((r43 & 0x0c) << 2);
3528 card->midi_info.irq = -pdev->irq;
3529 if (probe_uart401(& card->midi_info, THIS_MODULE))
3531 card->midi_devc=midi_devs[card->midi_info.slots[4]]->devc;
3532 pci_write_config_byte(pdev, 0x42, r42 & ~VIA_CR42_MIDI_IRQMASK);
3533 printk("Enabled Via MIDI\n");
3539 DPRINTK ("EXIT, returning 0\n");
3543 via_card_cleanup_proc (card);
3546 via_dsp_cleanup (card);
3549 via_ac97_cleanup (card);
3553 memset (card, 0xAB, sizeof (*card)); /* poison memory */
3558 pci_release_regions (pdev);
3561 pci_disable_device (pdev);
3564 pci_set_drvdata (pdev, NULL);
3565 DPRINTK ("EXIT - returning %d\n", rc);
3570 static void __devexit via_remove_one (struct pci_dev *pdev)
3572 struct via_info *card;
3574 DPRINTK ("ENTER\n");
3576 assert (pdev != NULL);
3577 card = pci_get_drvdata (pdev);
3578 assert (card != NULL);
3580 #ifdef CONFIG_MIDI_VIA82CXXX
3581 if (card->midi_info.io_base)
3582 unload_uart401(&card->midi_info);
3585 free_irq (card->pdev->irq, card);
3586 via_card_cleanup_proc (card);
3587 via_dsp_cleanup (card);
3588 via_ac97_cleanup (card);
3591 memset (card, 0xAB, sizeof (*card)); /* poison memory */
3595 pci_set_drvdata (pdev, NULL);
3597 pci_release_regions (pdev);
3598 pci_disable_device (pdev);
3599 pci_set_power_state (pdev, 3); /* ...zzzzzz */
3606 /****************************************************************
3608 * Driver initialization and cleanup
3613 static int __init init_via82cxxx_audio(void)
3617 DPRINTK ("ENTER\n");
3619 rc = via_init_proc ();
3621 DPRINTK ("EXIT, returning %d\n", rc);
3625 rc = pci_register_driver (&via_driver);
3628 pci_unregister_driver (&via_driver);
3629 via_cleanup_proc ();
3630 DPRINTK ("EXIT, returning -ENODEV\n");
3634 DPRINTK ("EXIT, returning 0\n");
3639 static void __exit cleanup_via82cxxx_audio(void)
3641 DPRINTK ("ENTER\n");
3643 pci_unregister_driver (&via_driver);
3644 via_cleanup_proc ();
3650 module_init(init_via82cxxx_audio);
3651 module_exit(cleanup_via82cxxx_audio);
3653 MODULE_AUTHOR("Jeff Garzik");
3654 MODULE_DESCRIPTION("DSP audio and mixer driver for Via 82Cxxx audio devices");
3655 MODULE_LICENSE("GPL");
3660 /****************************************************************
3662 * /proc/driver/via/info
3667 static int via_info_read_proc (char *page, char **start, off_t off,
3668 int count, int *eof, void *data)
3670 #define YN(val,bit) (((val) & (bit)) ? "yes" : "no")
3671 #define ED(val,bit) (((val) & (bit)) ? "enable" : "disable")
3674 u8 r40, r41, r42, r44;
3675 struct via_info *card = data;
3677 DPRINTK ("ENTER\n");
3679 assert (card != NULL);
3681 len += sprintf (page+len, VIA_CARD_NAME "\n\n");
3683 pci_read_config_byte (card->pdev, 0x40, &r40);
3684 pci_read_config_byte (card->pdev, 0x41, &r41);
3685 pci_read_config_byte (card->pdev, 0x42, &r42);
3686 pci_read_config_byte (card->pdev, 0x44, &r44);
3688 len += sprintf (page+len,
3689 "Via 82Cxxx PCI registers:\n"
3691 "40 Codec Ready: %s\n"
3692 " Codec Low-power: %s\n"
3693 " Secondary Codec Ready: %s\n"
3695 "41 Interface Enable: %s\n"
3696 " De-Assert Reset: %s\n"
3697 " Force SYNC high: %s\n"
3698 " Force SDO high: %s\n"
3699 " Variable Sample Rate On-Demand Mode: %s\n"
3700 " SGD Read Channel PCM Data Out: %s\n"
3701 " FM Channel PCM Data Out: %s\n"
3702 " SB PCM Data Out: %s\n"
3704 "42 Game port enabled: %s\n"
3705 " SoundBlaster enabled: %s\n"
3707 " MIDI enabled: %s\n"
3709 "44 AC-Link Interface Access: %s\n"
3710 " Secondary Codec Support: %s\n"
3714 YN (r40, VIA_CR40_AC97_READY),
3715 YN (r40, VIA_CR40_AC97_LOW_POWER),
3716 YN (r40, VIA_CR40_SECONDARY_READY),
3718 ED (r41, VIA_CR41_AC97_ENABLE),
3727 YN (r42, VIA_CR42_GAME_ENABLE),
3728 YN (r42, VIA_CR42_SB_ENABLE),
3729 YN (r42, VIA_CR42_FM_ENABLE),
3730 YN (r42, VIA_CR42_MIDI_ENABLE),
3732 YN (r44, VIA_CR44_AC_LINK_ACCESS),
3733 YN (r44, VIA_CR44_SECOND_CODEC_SUPPORT)
3737 DPRINTK ("EXIT, returning %d\n", len);
3745 /****************************************************************
3747 * /proc/driver/via/... setup and cleanup
3752 static int __init via_init_proc (void)
3754 DPRINTK ("ENTER\n");
3756 if (!proc_mkdir ("driver/via", 0))
3759 DPRINTK ("EXIT, returning 0\n");
3764 static void via_cleanup_proc (void)
3766 DPRINTK ("ENTER\n");
3768 remove_proc_entry ("driver/via", NULL);
3774 static int __init via_card_init_proc (struct via_info *card)
3779 DPRINTK ("ENTER\n");
3781 sprintf (s, "driver/via/%d", card->card_num);
3782 if (!proc_mkdir (s, 0)) {
3787 sprintf (s, "driver/via/%d/info", card->card_num);
3788 if (!create_proc_read_entry (s, 0, 0, via_info_read_proc, card)) {
3793 sprintf (s, "driver/via/%d/ac97", card->card_num);
3794 if (!create_proc_read_entry (s, 0, 0, ac97_read_proc, card->ac97)) {
3799 DPRINTK ("EXIT, returning 0\n");
3803 sprintf (s, "driver/via/%d/info", card->card_num);
3804 remove_proc_entry (s, NULL);
3807 sprintf (s, "driver/via/%d", card->card_num);
3808 remove_proc_entry (s, NULL);
3811 DPRINTK ("EXIT, returning %d\n", rc);
3816 static void via_card_cleanup_proc (struct via_info *card)
3820 DPRINTK ("ENTER\n");
3822 sprintf (s, "driver/via/%d/ac97", card->card_num);
3823 remove_proc_entry (s, NULL);
3825 sprintf (s, "driver/via/%d/info", card->card_num);
3826 remove_proc_entry (s, NULL);
3828 sprintf (s, "driver/via/%d", card->card_num);
3829 remove_proc_entry (s, NULL);
3834 #endif /* VIA_PROC_FS */