2 * Support for VIA 82Cxxx Audio Codecs
3 * Copyright 1999,2000 Jeff Garzik
5 * Updated to support the VIA 8233/8235 audio subsystem
6 * Alan Cox <alan@redhat.com> (C) Copyright 2002, 2003 Red Hat Inc
8 * Distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2.
9 * See the "COPYING" file distributed with this software for more info.
12 * For a list of known bugs (errata) and documentation,
13 * see via-audio.pdf in Documentation/DocBook.
14 * If this documentation does not exist, run "make pdfdocs".
18 #define VIA_VERSION "1.9.1-ac4-2.5"
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
26 #include <linux/pci.h>
27 #include <linux/init.h>
28 #include <linux/interrupt.h>
29 #include <linux/proc_fs.h>
30 #include <linux/spinlock.h>
31 #include <linux/sound.h>
32 #include <linux/poll.h>
33 #include <linux/soundcard.h>
34 #include <linux/ac97_codec.h>
35 #include <linux/smp_lock.h>
36 #include <linux/ioport.h>
37 #include <linux/delay.h>
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 #define VIA_SUPPORT_MMAP 1 /* buggy, for now... */
69 #define VIA_CARD_NAME "VIA 82Cxxx Audio driver " VIA_VERSION
70 #define VIA_MODULE_NAME "via82cxxx"
71 #define PFX VIA_MODULE_NAME ": "
73 #define VIA_COUNTER_LIMIT 100000
75 /* size of DMA buffers */
76 #define VIA_MAX_BUFFER_DMA_PAGES 32
78 /* buffering default values in ms */
79 #define VIA_DEFAULT_FRAG_TIME 20
80 #define VIA_DEFAULT_BUFFER_TIME 500
82 /* the hardware has a 256 fragment limit */
83 #define VIA_MIN_FRAG_NUMBER 2
84 #define VIA_MAX_FRAG_NUMBER 128
86 #define VIA_MAX_FRAG_SIZE PAGE_SIZE
87 #define VIA_MIN_FRAG_SIZE (VIA_MAX_BUFFER_DMA_PAGES * PAGE_SIZE / VIA_MAX_FRAG_NUMBER)
90 /* 82C686 function 5 (audio codec) PCI configuration registers */
91 #define VIA_ACLINK_STATUS 0x40
92 #define VIA_ACLINK_CTRL 0x41
93 #define VIA_FUNC_ENABLE 0x42
94 #define VIA_PNP_CONTROL 0x43
95 #define VIA_FM_NMI_CTRL 0x48
98 * controller base 0 (scatter-gather) registers
100 * NOTE: Via datasheet lists first channel as "read"
101 * channel and second channel as "write" channel.
102 * I changed the naming of the constants to be more
103 * clear than I felt the datasheet to be.
106 #define VIA_BASE0_PCM_OUT_CHAN 0x00 /* output PCM to user */
107 #define VIA_BASE0_PCM_OUT_CHAN_STATUS 0x00
108 #define VIA_BASE0_PCM_OUT_CHAN_CTRL 0x01
109 #define VIA_BASE0_PCM_OUT_CHAN_TYPE 0x02
111 #define VIA_BASE0_PCM_IN_CHAN 0x10 /* input PCM from user */
112 #define VIA_BASE0_PCM_IN_CHAN_STATUS 0x10
113 #define VIA_BASE0_PCM_IN_CHAN_CTRL 0x11
114 #define VIA_BASE0_PCM_IN_CHAN_TYPE 0x12
116 /* offsets from base */
117 #define VIA_PCM_STATUS 0x00
118 #define VIA_PCM_CONTROL 0x01
119 #define VIA_PCM_TYPE 0x02
120 #define VIA_PCM_LEFTVOL 0x02
121 #define VIA_PCM_RIGHTVOL 0x03
122 #define VIA_PCM_TABLE_ADDR 0x04
123 #define VIA_PCM_STOPRATE 0x08 /* 8233+ */
124 #define VIA_PCM_BLOCK_COUNT 0x0C
126 /* XXX unused DMA channel for FM PCM data */
127 #define VIA_BASE0_FM_OUT_CHAN 0x20
128 #define VIA_BASE0_FM_OUT_CHAN_STATUS 0x20
129 #define VIA_BASE0_FM_OUT_CHAN_CTRL 0x21
130 #define VIA_BASE0_FM_OUT_CHAN_TYPE 0x22
132 /* Six channel audio output on 8233 */
133 #define VIA_BASE0_MULTI_OUT_CHAN 0x40
134 #define VIA_BASE0_MULTI_OUT_CHAN_STATUS 0x40
135 #define VIA_BASE0_MULTI_OUT_CHAN_CTRL 0x41
136 #define VIA_BASE0_MULTI_OUT_CHAN_TYPE 0x42
138 #define VIA_BASE0_AC97_CTRL 0x80
139 #define VIA_BASE0_SGD_STATUS_SHADOW 0x84
140 #define VIA_BASE0_GPI_INT_ENABLE 0x8C
141 #define VIA_INTR_OUT ((1<<0) | (1<<4) | (1<<8))
142 #define VIA_INTR_IN ((1<<1) | (1<<5) | (1<<9))
143 #define VIA_INTR_FM ((1<<2) | (1<<6) | (1<<10))
144 #define VIA_INTR_MASK (VIA_INTR_OUT | VIA_INTR_IN | VIA_INTR_FM)
146 /* Newer VIA we need to monitor the low 3 bits of each channel. This
147 mask covers the channels we don't yet use as well
150 #define VIA_NEW_INTR_MASK 0x77077777UL
152 /* VIA_BASE0_AUDIO_xxx_CHAN_TYPE bits */
153 #define VIA_IRQ_ON_FLAG (1<<0) /* int on each flagged scatter block */
154 #define VIA_IRQ_ON_EOL (1<<1) /* int at end of scatter list */
155 #define VIA_INT_SEL_PCI_LAST_LINE_READ (0) /* int at PCI read of last line */
156 #define VIA_INT_SEL_LAST_SAMPLE_SENT (1<<2) /* int at last sample sent */
157 #define VIA_INT_SEL_ONE_LINE_LEFT (1<<3) /* int at less than one line to send */
158 #define VIA_PCM_FMT_STEREO (1<<4) /* PCM stereo format (bit clear == mono) */
159 #define VIA_PCM_FMT_16BIT (1<<5) /* PCM 16-bit format (bit clear == 8-bit) */
160 #define VIA_PCM_REC_FIFO (1<<6) /* PCM Recording FIFO */
161 #define VIA_RESTART_SGD_ON_EOL (1<<7) /* restart scatter-gather at EOL */
162 #define VIA_PCM_FMT_MASK (VIA_PCM_FMT_STEREO|VIA_PCM_FMT_16BIT)
163 #define VIA_CHAN_TYPE_MASK (VIA_RESTART_SGD_ON_EOL | \
166 #define VIA_CHAN_TYPE_INT_SELECT (VIA_INT_SEL_LAST_SAMPLE_SENT)
168 /* PCI configuration register bits and masks */
169 #define VIA_CR40_AC97_READY 0x01
170 #define VIA_CR40_AC97_LOW_POWER 0x02
171 #define VIA_CR40_SECONDARY_READY 0x04
173 #define VIA_CR41_AC97_ENABLE 0x80 /* enable AC97 codec */
174 #define VIA_CR41_AC97_RESET 0x40 /* clear bit to reset AC97 */
175 #define VIA_CR41_AC97_WAKEUP 0x20 /* wake up from power-down mode */
176 #define VIA_CR41_AC97_SDO 0x10 /* force Serial Data Out (SDO) high */
177 #define VIA_CR41_VRA 0x08 /* enable variable sample rate */
178 #define VIA_CR41_PCM_ENABLE 0x04 /* AC Link SGD Read Channel PCM Data Output */
179 #define VIA_CR41_FM_PCM_ENABLE 0x02 /* AC Link FM Channel PCM Data Out */
180 #define VIA_CR41_SB_PCM_ENABLE 0x01 /* AC Link SB PCM Data Output */
181 #define VIA_CR41_BOOT_MASK (VIA_CR41_AC97_ENABLE | \
182 VIA_CR41_AC97_WAKEUP | \
184 #define VIA_CR41_RUN_MASK (VIA_CR41_AC97_ENABLE | \
185 VIA_CR41_AC97_RESET | \
189 #define VIA_CR42_SB_ENABLE 0x01
190 #define VIA_CR42_MIDI_ENABLE 0x02
191 #define VIA_CR42_FM_ENABLE 0x04
192 #define VIA_CR42_GAME_ENABLE 0x08
193 #define VIA_CR42_MIDI_IRQMASK 0x40
194 #define VIA_CR42_MIDI_PNP 0x80
196 #define VIA_CR44_SECOND_CODEC_SUPPORT (1 << 6)
197 #define VIA_CR44_AC_LINK_ACCESS (1 << 7)
199 #define VIA_CR48_FM_TRAP_TO_NMI (1 << 2)
201 /* controller base 0 register bitmasks */
202 #define VIA_INT_DISABLE_MASK (~(0x01|0x02))
203 #define VIA_SGD_STOPPED (1 << 2)
204 #define VIA_SGD_PAUSED (1 << 6)
205 #define VIA_SGD_ACTIVE (1 << 7)
206 #define VIA_SGD_TERMINATE (1 << 6)
207 #define VIA_SGD_FLAG (1 << 0)
208 #define VIA_SGD_EOL (1 << 1)
209 #define VIA_SGD_START (1 << 7)
211 #define VIA_CR80_FIRST_CODEC 0
212 #define VIA_CR80_SECOND_CODEC (1 << 30)
213 #define VIA_CR80_FIRST_CODEC_VALID (1 << 25)
214 #define VIA_CR80_VALID (1 << 25)
215 #define VIA_CR80_SECOND_CODEC_VALID (1 << 27)
216 #define VIA_CR80_BUSY (1 << 24)
217 #define VIA_CR83_BUSY (1)
218 #define VIA_CR83_FIRST_CODEC_VALID (1 << 1)
219 #define VIA_CR80_READ (1 << 23)
220 #define VIA_CR80_WRITE_MODE 0
221 #define VIA_CR80_REG_IDX(idx) ((((idx) & 0xFF) >> 1) << 16)
223 /* capabilities we announce */
224 #ifdef VIA_SUPPORT_MMAP
225 #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | DSP_CAP_MMAP | \
226 DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
228 #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | \
229 DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
232 /* scatter-gather DMA table entry, exactly as passed to hardware */
233 struct via_sgd_table
{
235 u32 count
; /* includes additional VIA_xxx bits also */
238 #define VIA_EOL (1 << 31)
239 #define VIA_FLAG (1 << 30)
240 #define VIA_STOP (1 << 29)
243 enum via_channel_states
{
249 struct via_buffer_pgtbl
{
258 wait_queue_head_t wait
;
261 unsigned int slop_len
;
265 unsigned is_active
: 1;
266 unsigned is_record
: 1;
267 unsigned is_mapped
: 1;
268 unsigned is_enabled
: 1;
269 unsigned is_multi
: 1; /* 8233 6 channel */
270 u8 pcm_fmt
; /* VIA_PCM_FMT_xxx */
271 u8 channels
; /* Channel count */
273 unsigned rate
; /* sample rate */
274 unsigned int frag_size
;
275 unsigned int frag_number
;
277 unsigned char intmask
;
279 volatile struct via_sgd_table
*sgtable
;
280 dma_addr_t sgt_handle
;
282 unsigned int page_number
;
283 struct via_buffer_pgtbl pgtbl
[VIA_MAX_BUFFER_DMA_PAGES
];
291 /* data stored for each chip */
293 struct pci_dev
*pdev
;
296 struct ac97_codec
*ac97
;
297 spinlock_t ac97_lock
;
299 int card_num
; /* unique card number, from 0 */
301 int dev_dsp
; /* /dev/dsp index from register_sound_dsp() */
304 unsigned legacy
: 1; /* Has legacy ports */
305 unsigned intmask
: 1; /* Needs int bits */
306 unsigned sixchannel
: 1; /* 8233/35 with 6 channel support */
311 int mixer_vol
; /* 8233/35 volume - not yet implemented */
313 struct semaphore syscall_sem
;
314 struct semaphore open_sem
;
316 /* The 8233/8235 have 4 DX audio channels, two record and
317 one six channel out. We bind ch_in to DX 1, ch_out to multichannel
318 and ch_fm to DX 2. DX 3 and REC0/REC1 are unused at the
321 struct via_channel ch_in
;
322 struct via_channel ch_out
;
323 struct via_channel ch_fm
;
325 #ifdef CONFIG_MIDI_VIA82CXXX
327 struct address_info midi_info
;
332 /* number of cards, used for assigning unique numbers to cards */
333 static unsigned via_num_cards
;
337 /****************************************************************
344 static int via_init_one (struct pci_dev
*dev
, const struct pci_device_id
*id
);
345 static void __devexit
via_remove_one (struct pci_dev
*pdev
);
347 static ssize_t
via_dsp_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
);
348 static ssize_t
via_dsp_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
);
349 static unsigned int via_dsp_poll(struct file
*file
, struct poll_table_struct
*wait
);
350 static int via_dsp_ioctl (struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
);
351 static int via_dsp_open (struct inode
*inode
, struct file
*file
);
352 static int via_dsp_release(struct inode
*inode
, struct file
*file
);
353 static int via_dsp_mmap(struct file
*file
, struct vm_area_struct
*vma
);
355 static u16
via_ac97_read_reg (struct ac97_codec
*codec
, u8 reg
);
356 static void via_ac97_write_reg (struct ac97_codec
*codec
, u8 reg
, u16 value
);
357 static u8
via_ac97_wait_idle (struct via_info
*card
);
359 static void via_chan_free (struct via_info
*card
, struct via_channel
*chan
);
360 static void via_chan_clear (struct via_info
*card
, struct via_channel
*chan
);
361 static void via_chan_pcm_fmt (struct via_channel
*chan
, int reset
);
362 static void via_chan_buffer_free (struct via_info
*card
, struct via_channel
*chan
);
365 /****************************************************************
367 * Various data the driver needs
373 static struct pci_device_id via_pci_tbl
[] = {
374 { PCI_VENDOR_ID_VIA
, PCI_DEVICE_ID_VIA_82C686_5
,
375 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
376 { PCI_VENDOR_ID_VIA
, PCI_DEVICE_ID_VIA_8233_5
,
377 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 1},
380 MODULE_DEVICE_TABLE(pci
,via_pci_tbl
);
383 static struct pci_driver via_driver
= {
384 .name
= VIA_MODULE_NAME
,
385 .id_table
= via_pci_tbl
,
386 .probe
= via_init_one
,
387 .remove
= __devexit_p(via_remove_one
),
391 /****************************************************************
393 * Low-level base 0 register read/write helpers
399 * via_chan_stop - Terminate DMA on specified PCM channel
400 * @iobase: PCI base address for SGD channel registers
402 * Terminate scatter-gather DMA operation for given
403 * channel (derived from @iobase), if DMA is active.
405 * Note that @iobase is not the PCI base address,
406 * but the PCI base address plus an offset to
407 * one of three PCM channels supported by the chip.
411 static inline void via_chan_stop (long iobase
)
413 if (inb (iobase
+ VIA_PCM_STATUS
) & VIA_SGD_ACTIVE
)
414 outb (VIA_SGD_TERMINATE
, iobase
+ VIA_PCM_CONTROL
);
419 * via_chan_status_clear - Clear status flags on specified DMA channel
420 * @iobase: PCI base address for SGD channel registers
422 * Clear any pending status flags for the given
423 * DMA channel (derived from @iobase), if any
424 * flags are asserted.
426 * Note that @iobase is not the PCI base address,
427 * but the PCI base address plus an offset to
428 * one of three PCM channels supported by the chip.
432 static inline void via_chan_status_clear (long iobase
)
434 u8 tmp
= inb (iobase
+ VIA_PCM_STATUS
);
437 outb (tmp
, iobase
+ VIA_PCM_STATUS
);
442 * sg_begin - Begin recording or playback on a PCM channel
443 * @chan: Channel for which DMA operation shall begin
445 * Start scatter-gather DMA for the given channel.
449 static inline void sg_begin (struct via_channel
*chan
)
451 DPRINTK("Start with intmask %d\n", chan
->intmask
);
452 DPRINTK("About to start from %d to %d\n",
453 inl(chan
->iobase
+ VIA_PCM_BLOCK_COUNT
),
454 inb(chan
->iobase
+ VIA_PCM_STOPRATE
+ 3));
455 outb (VIA_SGD_START
|chan
->intmask
, chan
->iobase
+ VIA_PCM_CONTROL
);
456 DPRINTK("Status is now %02X\n", inb(chan
->iobase
+ VIA_PCM_STATUS
));
457 DPRINTK("Control is now %02X\n", inb(chan
->iobase
+ VIA_PCM_CONTROL
));
461 static int sg_active (long iobase
)
463 u8 tmp
= inb (iobase
+ VIA_PCM_STATUS
);
464 if ((tmp
& VIA_SGD_STOPPED
) || (tmp
& VIA_SGD_PAUSED
)) {
465 printk(KERN_WARNING
"via82cxxx warning: SG stopped or paused\n");
468 if (tmp
& VIA_SGD_ACTIVE
)
473 static int via_sg_offset(struct via_channel
*chan
)
475 return inl (chan
->iobase
+ VIA_PCM_BLOCK_COUNT
) & 0x00FFFFFF;
478 /****************************************************************
480 * Miscellaneous debris
487 * via_syscall_down - down the card-specific syscell semaphore
488 * @card: Private info for specified board
489 * @nonblock: boolean, non-zero if O_NONBLOCK is set
491 * Encapsulates standard method of acquiring the syscall sem.
493 * Returns negative errno on error, or zero for success.
496 static inline int via_syscall_down (struct via_info
*card
, int nonblock
)
499 * EAGAIN is supposed to be used if IO is pending,
500 * not if there is contention on some internal
501 * synchronization primitive which should be
502 * held only for a short time anyway
507 if (down_trylock (&card
->syscall_sem
))
510 if (down_interruptible (&card
->syscall_sem
))
519 * via_stop_everything - Stop all audio operations
520 * @card: Private info for specified board
522 * Stops all DMA operations and interrupts, and clear
523 * any pending status bits resulting from those operations.
526 static void via_stop_everything (struct via_info
*card
)
532 assert (card
!= NULL
);
535 * terminate any existing operations on audio read/write channels
537 via_chan_stop (card
->baseaddr
+ VIA_BASE0_PCM_OUT_CHAN
);
538 via_chan_stop (card
->baseaddr
+ VIA_BASE0_PCM_IN_CHAN
);
539 via_chan_stop (card
->baseaddr
+ VIA_BASE0_FM_OUT_CHAN
);
541 via_chan_stop (card
->baseaddr
+ VIA_BASE0_MULTI_OUT_CHAN
);
544 * clear any existing stops / flags (sanity check mainly)
546 via_chan_status_clear (card
->baseaddr
+ VIA_BASE0_PCM_OUT_CHAN
);
547 via_chan_status_clear (card
->baseaddr
+ VIA_BASE0_PCM_IN_CHAN
);
548 via_chan_status_clear (card
->baseaddr
+ VIA_BASE0_FM_OUT_CHAN
);
550 via_chan_status_clear (card
->baseaddr
+ VIA_BASE0_MULTI_OUT_CHAN
);
553 * clear any enabled interrupt bits
555 tmp
= inb (card
->baseaddr
+ VIA_BASE0_PCM_OUT_CHAN_TYPE
);
556 new_tmp
= tmp
& ~(VIA_IRQ_ON_FLAG
|VIA_IRQ_ON_EOL
|VIA_RESTART_SGD_ON_EOL
);
558 outb (0, card
->baseaddr
+ VIA_BASE0_PCM_OUT_CHAN_TYPE
);
560 tmp
= inb (card
->baseaddr
+ VIA_BASE0_PCM_IN_CHAN_TYPE
);
561 new_tmp
= tmp
& ~(VIA_IRQ_ON_FLAG
|VIA_IRQ_ON_EOL
|VIA_RESTART_SGD_ON_EOL
);
563 outb (0, card
->baseaddr
+ VIA_BASE0_PCM_IN_CHAN_TYPE
);
565 tmp
= inb (card
->baseaddr
+ VIA_BASE0_FM_OUT_CHAN_TYPE
);
566 new_tmp
= tmp
& ~(VIA_IRQ_ON_FLAG
|VIA_IRQ_ON_EOL
|VIA_RESTART_SGD_ON_EOL
);
568 outb (0, card
->baseaddr
+ VIA_BASE0_FM_OUT_CHAN_TYPE
);
572 tmp
= inb (card
->baseaddr
+ VIA_BASE0_MULTI_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_MULTI_OUT_CHAN_TYPE
);
581 * clear any existing flags
583 via_chan_status_clear (card
->baseaddr
+ VIA_BASE0_PCM_OUT_CHAN
);
584 via_chan_status_clear (card
->baseaddr
+ VIA_BASE0_PCM_IN_CHAN
);
585 via_chan_status_clear (card
->baseaddr
+ VIA_BASE0_FM_OUT_CHAN
);
592 * via_set_rate - Set PCM rate for given channel
593 * @ac97: Pointer to generic codec info struct
594 * @chan: Private info for specified channel
595 * @rate: Desired PCM sample rate, in Khz
597 * Sets the PCM sample rate for a channel.
599 * Values for @rate are clamped to a range of 4000 Khz through 48000 Khz,
600 * due to hardware constraints.
603 static int via_set_rate (struct ac97_codec
*ac97
,
604 struct via_channel
*chan
, unsigned rate
)
606 struct via_info
*card
= ac97
->private_data
;
609 u32 mast_vol
, phone_vol
, mono_vol
, pcm_vol
;
610 u32 mute_vol
= 0x8000; /* The mute volume? -- Seems to work! */
612 DPRINTK ("ENTER, rate = %d\n", rate
);
614 if (chan
->rate
== rate
)
616 if (card
->locked_rate
) {
621 if (rate
> 48000) rate
= 48000;
622 if (rate
< 4000) rate
= 4000;
624 rate_reg
= chan
->is_record
? AC97_PCM_LR_ADC_RATE
:
625 AC97_PCM_FRONT_DAC_RATE
;
627 /* Save current state */
628 dacp
=via_ac97_read_reg(ac97
, AC97_POWER_CONTROL
);
629 mast_vol
= via_ac97_read_reg(ac97
, AC97_MASTER_VOL_STEREO
);
630 mono_vol
= via_ac97_read_reg(ac97
, AC97_MASTER_VOL_MONO
);
631 phone_vol
= via_ac97_read_reg(ac97
, AC97_HEADPHONE_VOL
);
632 pcm_vol
= via_ac97_read_reg(ac97
, AC97_PCMOUT_VOL
);
633 /* Mute - largely reduces popping */
634 via_ac97_write_reg(ac97
, AC97_MASTER_VOL_STEREO
, mute_vol
);
635 via_ac97_write_reg(ac97
, AC97_MASTER_VOL_MONO
, mute_vol
);
636 via_ac97_write_reg(ac97
, AC97_HEADPHONE_VOL
, mute_vol
);
637 via_ac97_write_reg(ac97
, AC97_PCMOUT_VOL
, mute_vol
);
638 /* Power down the DAC */
639 via_ac97_write_reg(ac97
, AC97_POWER_CONTROL
, dacp
|0x0200);
642 via_ac97_write_reg (ac97
, rate_reg
, rate
);
644 /* Power DAC back up */
645 via_ac97_write_reg(ac97
, AC97_POWER_CONTROL
, dacp
);
646 udelay (200); /* reduces popping */
648 /* Restore volumes */
649 via_ac97_write_reg(ac97
, AC97_MASTER_VOL_STEREO
, mast_vol
);
650 via_ac97_write_reg(ac97
, AC97_MASTER_VOL_MONO
, mono_vol
);
651 via_ac97_write_reg(ac97
, AC97_HEADPHONE_VOL
, phone_vol
);
652 via_ac97_write_reg(ac97
, AC97_PCMOUT_VOL
, pcm_vol
);
654 /* the hardware might return a value different than what we
655 * passed to it, so read the rate value back from hardware
656 * to see what we came up with
658 chan
->rate
= via_ac97_read_reg (ac97
, rate_reg
);
660 if (chan
->rate
== 0) {
661 card
->locked_rate
= 1;
663 printk (KERN_WARNING PFX
"Codec rate locked at 48Khz\n");
667 DPRINTK ("EXIT, returning rate %d Hz\n", chan
->rate
);
672 /****************************************************************
674 * Channel-specific operations
681 * via_chan_init_defaults - Initialize a struct via_channel
682 * @card: Private audio chip info
683 * @chan: Channel to be initialized
685 * Zero @chan, and then set all static defaults for the structure.
688 static void via_chan_init_defaults (struct via_info
*card
, struct via_channel
*chan
)
690 memset (chan
, 0, sizeof (*chan
));
693 chan
->intmask
= 0x23; /* Turn on the IRQ bits */
695 if (chan
== &card
->ch_out
) {
696 chan
->name
= "PCM-OUT";
699 chan
->iobase
= card
->baseaddr
+ VIA_BASE0_MULTI_OUT_CHAN
;
701 DPRINTK("Using multichannel for pcm out\n");
704 chan
->iobase
= card
->baseaddr
+ VIA_BASE0_PCM_OUT_CHAN
;
705 } else if (chan
== &card
->ch_in
) {
706 chan
->name
= "PCM-IN";
707 chan
->iobase
= card
->baseaddr
+ VIA_BASE0_PCM_IN_CHAN
;
709 } else if (chan
== &card
->ch_fm
) {
710 chan
->name
= "PCM-OUT-FM";
711 chan
->iobase
= card
->baseaddr
+ VIA_BASE0_FM_OUT_CHAN
;
716 init_waitqueue_head (&chan
->wait
);
718 chan
->pcm_fmt
= VIA_PCM_FMT_MASK
;
719 chan
->is_enabled
= 1;
721 chan
->frag_number
= 0;
723 atomic_set(&chan
->n_frags
, 0);
724 atomic_set (&chan
->hw_ptr
, 0);
728 * via_chan_init - Initialize PCM channel
729 * @card: Private audio chip info
730 * @chan: Channel to be initialized
732 * Performs some of the preparations necessary to begin
733 * using a PCM channel.
735 * Currently the preparations consist of
736 * setting the PCM channel to a known state.
740 static void via_chan_init (struct via_info
*card
, struct via_channel
*chan
)
745 /* bzero channel structure, and init members to defaults */
746 via_chan_init_defaults (card
, chan
);
748 /* stop any existing channel output */
749 via_chan_clear (card
, chan
);
750 via_chan_status_clear (chan
->iobase
);
751 via_chan_pcm_fmt (chan
, 1);
757 * via_chan_buffer_init - Initialize PCM channel buffer
758 * @card: Private audio chip info
759 * @chan: Channel to be initialized
761 * Performs some of the preparations necessary to begin
762 * using a PCM channel.
764 * Currently the preparations include allocating the
765 * scatter-gather DMA table and buffers,
767 * address of the DMA table to the hardware.
769 * Note that special care is taken when passing the
770 * DMA table address to hardware, because it was found
771 * during driver development that the hardware did not
772 * always "take" the address.
775 static int via_chan_buffer_init (struct via_info
*card
, struct via_channel
*chan
)
785 chan
->intmask
= 0x23; /* Turn on the IRQ bits */
787 if (chan
->sgtable
!= NULL
) {
792 /* alloc DMA-able memory for scatter-gather table */
793 chan
->sgtable
= pci_alloc_consistent (card
->pdev
,
794 (sizeof (struct via_sgd_table
) * chan
->frag_number
),
796 if (!chan
->sgtable
) {
797 printk (KERN_ERR PFX
"DMA table alloc fail, aborting\n");
802 memset ((void*)chan
->sgtable
, 0,
803 (sizeof (struct via_sgd_table
) * chan
->frag_number
));
805 /* alloc DMA-able memory for scatter-gather buffers */
807 chan
->page_number
= (chan
->frag_number
* chan
->frag_size
) / PAGE_SIZE
+
808 (((chan
->frag_number
* chan
->frag_size
) % PAGE_SIZE
) ? 1 : 0);
810 for (i
= 0; i
< chan
->page_number
; i
++) {
811 chan
->pgtbl
[i
].cpuaddr
= pci_alloc_consistent (card
->pdev
, PAGE_SIZE
,
812 &chan
->pgtbl
[i
].handle
);
814 if (!chan
->pgtbl
[i
].cpuaddr
) {
815 chan
->page_number
= i
;
820 memset (chan
->pgtbl
[i
].cpuaddr
, 0xBC, chan
->frag_size
);
824 DPRINTK ("dmabuf_pg #%d (h=%lx, v2p=%lx, a=%p)\n",
825 i
, (long)chan
->pgtbl
[i
].handle
,
826 virt_to_phys(chan
->pgtbl
[i
].cpuaddr
),
827 chan
->pgtbl
[i
].cpuaddr
);
831 for (i
= 0; i
< chan
->frag_number
; i
++) {
833 page
= i
/ (PAGE_SIZE
/ chan
->frag_size
);
834 offset
= (i
% (PAGE_SIZE
/ chan
->frag_size
)) * chan
->frag_size
;
836 chan
->sgtable
[i
].count
= cpu_to_le32 (chan
->frag_size
| VIA_FLAG
);
837 chan
->sgtable
[i
].addr
= cpu_to_le32 (chan
->pgtbl
[page
].handle
+ offset
);
840 DPRINTK ("dmabuf #%d (32(h)=%lx)\n",
842 (long)chan
->sgtable
[i
].addr
);
846 /* overwrite the last buffer information */
847 chan
->sgtable
[chan
->frag_number
- 1].count
= cpu_to_le32 (chan
->frag_size
| VIA_EOL
);
849 /* set location of DMA-able scatter-gather info table */
850 DPRINTK ("outl (0x%X, 0x%04lX)\n",
851 chan
->sgt_handle
, chan
->iobase
+ VIA_PCM_TABLE_ADDR
);
853 via_ac97_wait_idle (card
);
854 outl (chan
->sgt_handle
, chan
->iobase
+ VIA_PCM_TABLE_ADDR
);
856 via_ac97_wait_idle (card
);
857 /* load no rate adaption, stereo 16bit, set up ring slots */
862 outl (0xFFFFF | (0x3 << 20) | (chan
->frag_number
<< 24), chan
->iobase
+ VIA_PCM_STOPRATE
);
864 via_ac97_wait_idle (card
);
868 DPRINTK ("inl (0x%lX) = %x\n",
869 chan
->iobase
+ VIA_PCM_TABLE_ADDR
,
870 inl(chan
->iobase
+ VIA_PCM_TABLE_ADDR
));
876 printk (KERN_ERR PFX
"DMA buffer alloc fail, aborting\n");
877 via_chan_buffer_free (card
, chan
);
884 * via_chan_free - Release a PCM channel
885 * @card: Private audio chip info
886 * @chan: Channel to be released
888 * Performs all the functions necessary to clean up
889 * an initialized channel.
891 * Currently these functions include disabled any
892 * active DMA operations, setting the PCM channel
893 * back to a known state, and releasing any allocated
897 static void via_chan_free (struct via_info
*card
, struct via_channel
*chan
)
901 spin_lock_irq (&card
->lock
);
903 /* stop any existing channel output */
904 via_chan_status_clear (chan
->iobase
);
905 via_chan_stop (chan
->iobase
);
906 via_chan_status_clear (chan
->iobase
);
908 spin_unlock_irq (&card
->lock
);
910 synchronize_irq(card
->pdev
->irq
);
915 static void via_chan_buffer_free (struct via_info
*card
, struct via_channel
*chan
)
921 /* zero location of DMA-able scatter-gather info table */
922 via_ac97_wait_idle(card
);
923 outl (0, chan
->iobase
+ VIA_PCM_TABLE_ADDR
);
925 for (i
= 0; i
< chan
->page_number
; i
++)
926 if (chan
->pgtbl
[i
].cpuaddr
) {
927 pci_free_consistent (card
->pdev
, PAGE_SIZE
,
928 chan
->pgtbl
[i
].cpuaddr
,
929 chan
->pgtbl
[i
].handle
);
930 chan
->pgtbl
[i
].cpuaddr
= NULL
;
931 chan
->pgtbl
[i
].handle
= 0;
934 chan
->page_number
= 0;
937 pci_free_consistent (card
->pdev
,
938 (sizeof (struct via_sgd_table
) * chan
->frag_number
),
939 (void*)chan
->sgtable
, chan
->sgt_handle
);
940 chan
->sgtable
= NULL
;
948 * via_chan_pcm_fmt - Update PCM channel settings
949 * @chan: Channel to be updated
950 * @reset: Boolean. If non-zero, channel will be reset
951 * to 8-bit mono mode.
953 * Stores the settings of the current PCM format,
954 * 8-bit or 16-bit, and mono/stereo, into the
955 * hardware settings for the specified channel.
956 * If @reset is non-zero, the channel is reset
957 * to 8-bit mono mode. Otherwise, the channel
958 * is set to the values stored in the channel
959 * information struct @chan.
962 static void via_chan_pcm_fmt (struct via_channel
*chan
, int reset
)
964 DPRINTK ("ENTER, pcm_fmt=0x%02X, reset=%s\n",
965 chan
->pcm_fmt
, reset
? "yes" : "no");
967 assert (chan
!= NULL
);
971 /* reset to 8-bit mono mode */
976 /* enable interrupts on FLAG and EOL */
977 chan
->pcm_fmt
|= VIA_CHAN_TYPE_MASK
;
979 /* if we are recording, enable recording fifo bit */
981 chan
->pcm_fmt
|= VIA_PCM_REC_FIFO
;
982 /* set interrupt select bits where applicable (PCM in & out channels) */
983 if (!chan
->is_record
)
984 chan
->pcm_fmt
|= VIA_CHAN_TYPE_INT_SELECT
;
986 DPRINTK("SET FMT - %02x %02x\n", chan
->intmask
, chan
->is_multi
);
993 * Channel 0x4 is up to 6 x 16bit and has to be
994 * programmed differently
1002 * Load the type bit for num channels
1006 if(chan
->pcm_fmt
& VIA_PCM_FMT_16BIT
)
1008 if(chan
->pcm_fmt
& VIA_PCM_FMT_STEREO
)
1013 outb(c
, chan
->iobase
+ VIA_PCM_TYPE
);
1016 * Set the channel steering
1018 * Channel 0 to slot 3
1019 * Channel 0 to slot 4
1021 * Channel 0 to slot 3
1022 * Channel 1 to slot 4
1025 switch(chan
->channels
)
1028 outl(0xFF000000 | (1<<0) | (1<<4) , chan
->iobase
+ VIA_PCM_STOPRATE
);
1031 outl(0xFF000000 | (1<<0) | (2<<4) , chan
->iobase
+ VIA_PCM_STOPRATE
);
1034 outl(0xFF000000 | (1<<0) | (2<<4) | (3<<8) | (4<<12), chan
->iobase
+ VIA_PCM_STOPRATE
);
1037 outl(0xFF000000 | (1<<0) | (2<<4) | (5<<8) | (6<<12) | (3<<16) | (4<<20), chan
->iobase
+ VIA_PCM_STOPRATE
);
1044 * New style, turn off channel volume
1045 * control, set bits in the right register
1047 outb(0x0, chan
->iobase
+ VIA_PCM_LEFTVOL
);
1048 outb(0x0, chan
->iobase
+ VIA_PCM_RIGHTVOL
);
1050 m
= inl(chan
->iobase
+ VIA_PCM_STOPRATE
);
1052 if(chan
->pcm_fmt
& VIA_PCM_FMT_STEREO
)
1054 if(chan
->pcm_fmt
& VIA_PCM_FMT_16BIT
)
1056 outl(m
, chan
->iobase
+ VIA_PCM_STOPRATE
);
1060 outb (chan
->pcm_fmt
, chan
->iobase
+ VIA_PCM_TYPE
);
1063 DPRINTK ("EXIT, pcm_fmt = 0x%02X, reg = 0x%02X\n",
1065 inb (chan
->iobase
+ VIA_PCM_TYPE
));
1070 * via_chan_clear - Stop DMA channel operation, and reset pointers
1071 * @card: the chip to accessed
1072 * @chan: Channel to be cleared
1074 * Call via_chan_stop to halt DMA operations, and then resets
1075 * all software pointers which track DMA operation.
1078 static void via_chan_clear (struct via_info
*card
, struct via_channel
*chan
)
1080 DPRINTK ("ENTER\n");
1081 via_chan_stop (chan
->iobase
);
1082 via_chan_buffer_free(card
, chan
);
1083 chan
->is_active
= 0;
1084 chan
->is_mapped
= 0;
1085 chan
->is_enabled
= 1;
1089 atomic_set (&chan
->hw_ptr
, 0);
1095 * via_chan_set_speed - Set PCM sample rate for given channel
1096 * @card: Private info for specified board
1097 * @chan: Channel whose sample rate will be adjusted
1098 * @val: New sample rate, in Khz
1100 * Helper function for the %SNDCTL_DSP_SPEED ioctl. OSS semantics
1101 * demand that all audio operations halt (if they are not already
1102 * halted) when the %SNDCTL_DSP_SPEED is given.
1104 * This function halts all audio operations for the given channel
1105 * @chan, and then calls via_set_rate to set the audio hardware
1109 static int via_chan_set_speed (struct via_info
*card
,
1110 struct via_channel
*chan
, int val
)
1112 DPRINTK ("ENTER, requested rate = %d\n", val
);
1114 via_chan_clear (card
, chan
);
1116 val
= via_set_rate (card
->ac97
, chan
, val
);
1118 DPRINTK ("EXIT, returning %d\n", val
);
1124 * via_chan_set_fmt - Set PCM sample size for given channel
1125 * @card: Private info for specified board
1126 * @chan: Channel whose sample size will be adjusted
1127 * @val: New sample size, use the %AFMT_xxx constants
1129 * Helper function for the %SNDCTL_DSP_SETFMT ioctl. OSS semantics
1130 * demand that all audio operations halt (if they are not already
1131 * halted) when the %SNDCTL_DSP_SETFMT is given.
1133 * This function halts all audio operations for the given channel
1134 * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
1135 * to the new sample size, either 8-bit or 16-bit.
1138 static int via_chan_set_fmt (struct via_info
*card
,
1139 struct via_channel
*chan
, int val
)
1141 DPRINTK ("ENTER, val=%s\n",
1142 val
== AFMT_U8
? "AFMT_U8" :
1143 val
== AFMT_S16_LE
? "AFMT_S16_LE" :
1146 via_chan_clear (card
, chan
);
1148 assert (val
!= AFMT_QUERY
); /* this case is handled elsewhere */
1152 if ((chan
->pcm_fmt
& VIA_PCM_FMT_16BIT
) == 0) {
1153 chan
->pcm_fmt
|= VIA_PCM_FMT_16BIT
;
1154 via_chan_pcm_fmt (chan
, 0);
1159 if (chan
->pcm_fmt
& VIA_PCM_FMT_16BIT
) {
1160 chan
->pcm_fmt
&= ~VIA_PCM_FMT_16BIT
;
1161 via_chan_pcm_fmt (chan
, 0);
1166 DPRINTK ("unknown AFMT: 0x%X\n", val
);
1176 * via_chan_set_stereo - Enable or disable stereo for a DMA channel
1177 * @card: Private info for specified board
1178 * @chan: Channel whose stereo setting will be adjusted
1179 * @val: New sample size, use the %AFMT_xxx constants
1181 * Helper function for the %SNDCTL_DSP_CHANNELS and %SNDCTL_DSP_STEREO ioctls. OSS semantics
1182 * demand that all audio operations halt (if they are not already
1183 * halted) when %SNDCTL_DSP_CHANNELS or SNDCTL_DSP_STEREO is given.
1185 * This function halts all audio operations for the given channel
1186 * @chan, and then calls via_chan_pcm_fmt to set the audio hardware
1187 * to enable or disable stereo.
1190 static int via_chan_set_stereo (struct via_info
*card
,
1191 struct via_channel
*chan
, int val
)
1193 DPRINTK ("ENTER, channels = %d\n", val
);
1195 via_chan_clear (card
, chan
);
1201 chan
->pcm_fmt
&= ~VIA_PCM_FMT_STEREO
;
1203 via_chan_pcm_fmt (chan
, 0);
1208 chan
->pcm_fmt
|= VIA_PCM_FMT_STEREO
;
1210 via_chan_pcm_fmt (chan
, 0);
1217 chan
->pcm_fmt
|= VIA_PCM_FMT_STEREO
;
1218 chan
->channels
= val
;
1227 DPRINTK ("EXIT, returning %d\n", val
);
1231 static int via_chan_set_buffering (struct via_info
*card
,
1232 struct via_channel
*chan
, int val
)
1236 DPRINTK ("ENTER\n");
1238 /* in both cases the buffer cannot be changed */
1239 if (chan
->is_active
|| chan
->is_mapped
) {
1244 /* called outside SETFRAGMENT */
1245 /* set defaults or do nothing */
1248 if (chan
->frag_size
&& chan
->frag_number
)
1253 chan
->frag_size
= (VIA_DEFAULT_FRAG_TIME
* chan
->rate
* chan
->channels
1254 * ((chan
->pcm_fmt
& VIA_PCM_FMT_16BIT
) ? 2 : 1)) / 1000 - 1;
1257 while (chan
->frag_size
) {
1258 chan
->frag_size
>>= 1;
1261 chan
->frag_size
= 1 << shift
;
1263 chan
->frag_number
= (VIA_DEFAULT_BUFFER_TIME
/ VIA_DEFAULT_FRAG_TIME
);
1265 DPRINTK ("setting default values %d %d\n", chan
->frag_size
, chan
->frag_number
);
1267 chan
->frag_size
= 1 << (val
& 0xFFFF);
1268 chan
->frag_number
= (val
>> 16) & 0xFFFF;
1270 DPRINTK ("using user values %d %d\n", chan
->frag_size
, chan
->frag_number
);
1273 /* quake3 wants frag_number to be a power of two */
1275 while (chan
->frag_number
) {
1276 chan
->frag_number
>>= 1;
1279 chan
->frag_number
= 1 << shift
;
1281 if (chan
->frag_size
> VIA_MAX_FRAG_SIZE
)
1282 chan
->frag_size
= VIA_MAX_FRAG_SIZE
;
1283 else if (chan
->frag_size
< VIA_MIN_FRAG_SIZE
)
1284 chan
->frag_size
= VIA_MIN_FRAG_SIZE
;
1286 if (chan
->frag_number
< VIA_MIN_FRAG_NUMBER
)
1287 chan
->frag_number
= VIA_MIN_FRAG_NUMBER
;
1288 if (chan
->frag_number
> VIA_MAX_FRAG_NUMBER
)
1289 chan
->frag_number
= VIA_MAX_FRAG_NUMBER
;
1291 if ((chan
->frag_number
* chan
->frag_size
) / PAGE_SIZE
> VIA_MAX_BUFFER_DMA_PAGES
)
1292 chan
->frag_number
= (VIA_MAX_BUFFER_DMA_PAGES
* PAGE_SIZE
) / chan
->frag_size
;
1295 if (chan
->is_record
)
1296 atomic_set (&chan
->n_frags
, 0);
1298 atomic_set (&chan
->n_frags
, chan
->frag_number
);
1305 #ifdef VIA_CHAN_DUMP_BUFS
1307 * via_chan_dump_bufs - Display DMA table contents
1308 * @chan: Channel whose DMA table will be displayed
1310 * Debugging function which displays the contents of the
1311 * scatter-gather DMA table for the given channel @chan.
1314 static void via_chan_dump_bufs (struct via_channel
*chan
)
1318 for (i
= 0; i
< chan
->frag_number
; i
++) {
1319 DPRINTK ("#%02d: addr=%x, count=%u, flag=%d, eol=%d\n",
1320 i
, chan
->sgtable
[i
].addr
,
1321 chan
->sgtable
[i
].count
& 0x00FFFFFF,
1322 chan
->sgtable
[i
].count
& VIA_FLAG
? 1 : 0,
1323 chan
->sgtable
[i
].count
& VIA_EOL
? 1 : 0);
1325 DPRINTK ("buf_in_use = %d, nextbuf = %d\n",
1326 atomic_read (&chan
->buf_in_use
),
1327 atomic_read (&chan
->sw_ptr
));
1329 #endif /* VIA_CHAN_DUMP_BUFS */
1333 * via_chan_flush_frag - Flush partially-full playback buffer to hardware
1334 * @chan: Channel whose DMA table will be flushed
1336 * Flushes partially-full playback buffer to hardware.
1339 static void via_chan_flush_frag (struct via_channel
*chan
)
1341 DPRINTK ("ENTER\n");
1343 assert (chan
->slop_len
> 0);
1345 if (chan
->sw_ptr
== (chan
->frag_number
- 1))
1352 assert (atomic_read (&chan
->n_frags
) > 0);
1353 atomic_dec (&chan
->n_frags
);
1361 * via_chan_maybe_start - Initiate audio hardware DMA operation
1362 * @chan: Channel whose DMA is to be started
1364 * Initiate DMA operation, if the DMA engine for the given
1365 * channel @chan is not already active.
1368 static inline void via_chan_maybe_start (struct via_channel
*chan
)
1370 assert (chan
->is_active
== sg_active(chan
->iobase
));
1372 DPRINTK ("MAYBE START %s\n", chan
->name
);
1373 if (!chan
->is_active
&& chan
->is_enabled
) {
1374 chan
->is_active
= 1;
1376 DPRINTK ("starting channel %s\n", chan
->name
);
1381 /****************************************************************
1383 * Interface to ac97-codec module
1389 * via_ac97_wait_idle - Wait until AC97 codec is not busy
1390 * @card: Private info for specified board
1392 * Sleep until the AC97 codec is no longer busy.
1393 * Returns the final value read from the SGD
1394 * register being polled.
1397 static u8
via_ac97_wait_idle (struct via_info
*card
)
1400 int counter
= VIA_COUNTER_LIMIT
;
1402 DPRINTK ("ENTER/EXIT\n");
1404 assert (card
!= NULL
);
1405 assert (card
->pdev
!= NULL
);
1410 tmp8
= inb (card
->baseaddr
+ 0x83);
1411 } while ((tmp8
& VIA_CR83_BUSY
) && (counter
-- > 0));
1413 if (tmp8
& VIA_CR83_BUSY
)
1414 printk (KERN_WARNING PFX
"timeout waiting on AC97 codec\n");
1420 * via_ac97_read_reg - Read AC97 standard register
1421 * @codec: Pointer to generic AC97 codec info
1422 * @reg: Index of AC97 register to be read
1424 * Read the value of a single AC97 codec register,
1425 * as defined by the Intel AC97 specification.
1427 * Defines the standard AC97 read-register operation
1428 * required by the kernel's ac97_codec interface.
1430 * Returns the 16-bit value stored in the specified
1434 static u16
via_ac97_read_reg (struct ac97_codec
*codec
, u8 reg
)
1437 struct via_info
*card
;
1440 DPRINTK ("ENTER\n");
1442 assert (codec
!= NULL
);
1443 assert (codec
->private_data
!= NULL
);
1445 card
= codec
->private_data
;
1447 spin_lock(&card
->ac97_lock
);
1449 /* Every time we write to register 80 we cause a transaction.
1450 The only safe way to clear the valid bit is to write it at
1451 the same time as the command */
1452 data
= (reg
<< 16) | VIA_CR80_READ
| VIA_CR80_VALID
;
1454 outl (data
, card
->baseaddr
+ VIA_BASE0_AC97_CTRL
);
1457 for (counter
= VIA_COUNTER_LIMIT
; counter
> 0; counter
--) {
1459 if ((((data
= inl(card
->baseaddr
+ VIA_BASE0_AC97_CTRL
)) &
1460 (VIA_CR80_VALID
|VIA_CR80_BUSY
)) == VIA_CR80_VALID
))
1464 printk (KERN_WARNING PFX
"timeout while reading AC97 codec (0x%lX)\n", data
);
1468 /* Once the valid bit has become set, we must wait a complete AC97
1469 frame before the data has settled. */
1471 data
= (unsigned long) inl (card
->baseaddr
+ VIA_BASE0_AC97_CTRL
);
1473 outb (0x02, card
->baseaddr
+ 0x83);
1475 if (((data
& 0x007F0000) >> 16) == reg
) {
1476 DPRINTK ("EXIT, success, data=0x%lx, retval=0x%lx\n",
1477 data
, data
& 0x0000FFFF);
1478 spin_unlock(&card
->ac97_lock
);
1479 return data
& 0x0000FFFF;
1482 printk (KERN_WARNING
"via82cxxx_audio: not our index: reg=0x%x, newreg=0x%lx\n",
1483 reg
, ((data
& 0x007F0000) >> 16));
1486 spin_unlock(&card
->ac97_lock
);
1487 DPRINTK ("EXIT, returning 0\n");
1493 * via_ac97_write_reg - Write AC97 standard register
1494 * @codec: Pointer to generic AC97 codec info
1495 * @reg: Index of AC97 register to be written
1496 * @value: Value to be written to AC97 register
1498 * Write the value of a single AC97 codec register,
1499 * as defined by the Intel AC97 specification.
1501 * Defines the standard AC97 write-register operation
1502 * required by the kernel's ac97_codec interface.
1505 static void via_ac97_write_reg (struct ac97_codec
*codec
, u8 reg
, u16 value
)
1508 struct via_info
*card
;
1511 DPRINTK ("ENTER\n");
1513 assert (codec
!= NULL
);
1514 assert (codec
->private_data
!= NULL
);
1516 card
= codec
->private_data
;
1518 spin_lock(&card
->ac97_lock
);
1520 data
= (reg
<< 16) + value
;
1521 outl (data
, card
->baseaddr
+ VIA_BASE0_AC97_CTRL
);
1524 for (counter
= VIA_COUNTER_LIMIT
; counter
> 0; counter
--) {
1525 if ((inb (card
->baseaddr
+ 0x83) & VIA_CR83_BUSY
) == 0)
1531 printk (KERN_WARNING PFX
"timeout after AC97 codec write (0x%X, 0x%X)\n", reg
, value
);
1534 spin_unlock(&card
->ac97_lock
);
1539 static int via_mixer_open (struct inode
*inode
, struct file
*file
)
1541 int minor
= iminor(inode
);
1542 struct via_info
*card
;
1543 struct pci_dev
*pdev
= NULL
;
1544 struct pci_driver
*drvr
;
1546 DPRINTK ("ENTER\n");
1548 while ((pdev
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pdev
)) != NULL
) {
1549 drvr
= pci_dev_driver (pdev
);
1550 if (drvr
== &via_driver
) {
1551 assert (pci_get_drvdata (pdev
) != NULL
);
1553 card
= pci_get_drvdata (pdev
);
1554 if (card
->ac97
->dev_mixer
== minor
)
1559 DPRINTK ("EXIT, returning -ENODEV\n");
1563 file
->private_data
= card
->ac97
;
1565 DPRINTK ("EXIT, returning 0\n");
1566 return nonseekable_open(inode
, file
);
1569 static int via_mixer_ioctl (struct inode
*inode
, struct file
*file
, unsigned int cmd
,
1572 struct ac97_codec
*codec
= file
->private_data
;
1573 struct via_info
*card
;
1574 int nonblock
= (file
->f_flags
& O_NONBLOCK
);
1577 DPRINTK ("ENTER\n");
1579 assert (codec
!= NULL
);
1580 card
= codec
->private_data
;
1581 assert (card
!= NULL
);
1583 rc
= via_syscall_down (card
, nonblock
);
1588 * Intercept volume control on 8233 and 8235
1594 case SOUND_MIXER_READ_VOLUME
:
1595 return card
->mixer_vol
;
1596 case SOUND_MIXER_WRITE_VOLUME
:
1599 if(get_user(v
, (int *)arg
))
1604 card
->mixer_vol
= v
;
1609 rc
= codec
->mixer_ioctl(codec
, cmd
, arg
);
1611 up (&card
->syscall_sem
);
1614 DPRINTK ("EXIT, returning %d\n", rc
);
1619 static struct file_operations via_mixer_fops
= {
1620 .owner
= THIS_MODULE
,
1621 .open
= via_mixer_open
,
1622 .llseek
= no_llseek
,
1623 .ioctl
= via_mixer_ioctl
,
1627 static int __devinit
via_ac97_reset (struct via_info
*card
)
1629 struct pci_dev
*pdev
= card
->pdev
;
1633 DPRINTK ("ENTER\n");
1635 assert (pdev
!= NULL
);
1639 u8 r40
,r41
,r42
,r43
,r44
,r48
;
1640 pci_read_config_byte (card
->pdev
, 0x40, &r40
);
1641 pci_read_config_byte (card
->pdev
, 0x41, &r41
);
1642 pci_read_config_byte (card
->pdev
, 0x42, &r42
);
1643 pci_read_config_byte (card
->pdev
, 0x43, &r43
);
1644 pci_read_config_byte (card
->pdev
, 0x44, &r44
);
1645 pci_read_config_byte (card
->pdev
, 0x48, &r48
);
1646 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
1647 r40
,r41
,r42
,r43
,r44
,r48
);
1649 spin_lock_irq (&card
->lock
);
1650 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
1651 inb (card
->baseaddr
+ 0x00),
1652 inb (card
->baseaddr
+ 0x01),
1653 inb (card
->baseaddr
+ 0x02),
1654 inl (card
->baseaddr
+ 0x04),
1655 inl (card
->baseaddr
+ 0x0C),
1656 inl (card
->baseaddr
+ 0x80),
1657 inl (card
->baseaddr
+ 0x84));
1658 spin_unlock_irq (&card
->lock
);
1664 * Reset AC97 controller: enable, disable, enable,
1665 * pausing after each command for good luck. Only
1666 * do this if the codec is not ready, because it causes
1667 * loud pops and such due to such a hard codec reset.
1669 pci_read_config_byte (pdev
, VIA_ACLINK_STATUS
, &tmp8
);
1670 if ((tmp8
& VIA_CR40_AC97_READY
) == 0) {
1671 pci_write_config_byte (pdev
, VIA_ACLINK_CTRL
,
1672 VIA_CR41_AC97_ENABLE
|
1673 VIA_CR41_AC97_RESET
|
1674 VIA_CR41_AC97_WAKEUP
);
1677 pci_write_config_byte (pdev
, VIA_ACLINK_CTRL
, 0);
1680 pci_write_config_byte (pdev
, VIA_ACLINK_CTRL
,
1681 VIA_CR41_AC97_ENABLE
|
1682 VIA_CR41_PCM_ENABLE
|
1683 VIA_CR41_VRA
| VIA_CR41_AC97_RESET
);
1687 /* Make sure VRA is enabled, in case we didn't do a
1688 * complete codec reset, above
1690 pci_read_config_byte (pdev
, VIA_ACLINK_CTRL
, &tmp8
);
1691 if (((tmp8
& VIA_CR41_VRA
) == 0) ||
1692 ((tmp8
& VIA_CR41_AC97_ENABLE
) == 0) ||
1693 ((tmp8
& VIA_CR41_PCM_ENABLE
) == 0) ||
1694 ((tmp8
& VIA_CR41_AC97_RESET
) == 0)) {
1695 pci_write_config_byte (pdev
, VIA_ACLINK_CTRL
,
1696 VIA_CR41_AC97_ENABLE
|
1697 VIA_CR41_PCM_ENABLE
|
1698 VIA_CR41_VRA
| VIA_CR41_AC97_RESET
);
1704 #if 0 /* this breaks on K7M */
1705 /* disable legacy stuff */
1706 pci_write_config_byte (pdev
, 0x42, 0x00);
1710 /* route FM trap to IRQ, disable FM trap */
1711 pci_write_config_byte (pdev
, 0x48, 0x05);
1715 /* disable all codec GPI interrupts */
1716 outl (0, pci_resource_start (pdev
, 0) + 0x8C);
1718 /* WARNING: this line is magic. Remove this
1719 * and things break. */
1720 /* enable variable rate */
1721 tmp16
= via_ac97_read_reg (card
->ac97
, AC97_EXTENDED_STATUS
);
1722 if ((tmp16
& 1) == 0)
1723 via_ac97_write_reg (card
->ac97
, AC97_EXTENDED_STATUS
, tmp16
| 1);
1725 DPRINTK ("EXIT, returning 0\n");
1730 static void via_ac97_codec_wait (struct ac97_codec
*codec
)
1732 assert (codec
->private_data
!= NULL
);
1733 via_ac97_wait_idle (codec
->private_data
);
1737 static int __devinit
via_ac97_init (struct via_info
*card
)
1742 DPRINTK ("ENTER\n");
1744 assert (card
!= NULL
);
1746 card
->ac97
= ac97_alloc_codec();
1747 if(card
->ac97
== NULL
)
1750 card
->ac97
->private_data
= card
;
1751 card
->ac97
->codec_read
= via_ac97_read_reg
;
1752 card
->ac97
->codec_write
= via_ac97_write_reg
;
1753 card
->ac97
->codec_wait
= via_ac97_codec_wait
;
1755 card
->ac97
->dev_mixer
= register_sound_mixer (&via_mixer_fops
, -1);
1756 if (card
->ac97
->dev_mixer
< 0) {
1757 printk (KERN_ERR PFX
"unable to register AC97 mixer, aborting\n");
1758 DPRINTK ("EXIT, returning -EIO\n");
1759 ac97_release_codec(card
->ac97
);
1763 rc
= via_ac97_reset (card
);
1765 printk (KERN_ERR PFX
"unable to reset AC97 codec, aborting\n");
1771 if (ac97_probe_codec (card
->ac97
) == 0) {
1772 printk (KERN_ERR PFX
"unable to probe AC97 codec, aborting\n");
1777 /* enable variable rate */
1778 tmp16
= via_ac97_read_reg (card
->ac97
, AC97_EXTENDED_STATUS
);
1779 via_ac97_write_reg (card
->ac97
, AC97_EXTENDED_STATUS
, tmp16
| 1);
1782 * If we cannot enable VRA, we have a locked-rate codec.
1783 * We try again to enable VRA before assuming so, however.
1785 tmp16
= via_ac97_read_reg (card
->ac97
, AC97_EXTENDED_STATUS
);
1786 if ((tmp16
& 1) == 0) {
1787 via_ac97_write_reg (card
->ac97
, AC97_EXTENDED_STATUS
, tmp16
| 1);
1788 tmp16
= via_ac97_read_reg (card
->ac97
, AC97_EXTENDED_STATUS
);
1789 if ((tmp16
& 1) == 0) {
1790 card
->locked_rate
= 1;
1791 printk (KERN_WARNING PFX
"Codec rate locked at 48Khz\n");
1795 DPRINTK ("EXIT, returning 0\n");
1799 unregister_sound_mixer (card
->ac97
->dev_mixer
);
1800 DPRINTK ("EXIT, returning %d\n", rc
);
1801 ac97_release_codec(card
->ac97
);
1806 static void via_ac97_cleanup (struct via_info
*card
)
1808 DPRINTK ("ENTER\n");
1810 assert (card
!= NULL
);
1811 assert (card
->ac97
->dev_mixer
>= 0);
1813 unregister_sound_mixer (card
->ac97
->dev_mixer
);
1814 ac97_release_codec(card
->ac97
);
1821 /****************************************************************
1823 * Interrupt-related code
1828 * via_intr_channel - handle an interrupt for a single channel
1830 * @chan: handle interrupt for this channel
1832 * This is the "meat" of the interrupt handler,
1833 * containing the actions taken each time an interrupt
1834 * occurs. All communication and coordination with
1835 * userspace takes place here.
1837 * Locking: inside card->lock
1840 static void via_intr_channel (struct via_info
*card
, struct via_channel
*chan
)
1845 /* check pertinent bits of status register for action bits */
1846 status
= inb (chan
->iobase
) & (VIA_SGD_FLAG
| VIA_SGD_EOL
| VIA_SGD_STOPPED
);
1850 /* acknowledge any flagged bits ASAP */
1851 outb (status
, chan
->iobase
);
1853 if (!chan
->sgtable
) /* XXX: temporary solution */
1856 /* grab current h/w ptr value */
1857 n
= atomic_read (&chan
->hw_ptr
);
1859 /* sanity check: make sure our h/w ptr doesn't have a weird value */
1861 assert (n
< chan
->frag_number
);
1864 /* reset SGD data structure in memory to reflect a full buffer,
1865 * and advance the h/w ptr, wrapping around to zero if needed
1867 if (n
== (chan
->frag_number
- 1)) {
1868 chan
->sgtable
[n
].count
= cpu_to_le32(chan
->frag_size
| VIA_EOL
);
1869 atomic_set (&chan
->hw_ptr
, 0);
1871 chan
->sgtable
[n
].count
= cpu_to_le32(chan
->frag_size
| VIA_FLAG
);
1872 atomic_inc (&chan
->hw_ptr
);
1875 /* accounting crap for SNDCTL_DSP_GETxPTR */
1877 chan
->bytes
+= chan
->frag_size
;
1878 /* FIXME - signed overflow is undefined */
1879 if (chan
->bytes
< 0) /* handle overflow of 31-bit value */
1880 chan
->bytes
= chan
->frag_size
;
1881 /* all following checks only occur when not in mmap(2) mode */
1882 if (!chan
->is_mapped
)
1884 /* If we are recording, then n_frags represents the number
1885 * of fragments waiting to be handled by userspace.
1886 * If we are playback, then n_frags represents the number
1887 * of fragments remaining to be filled by userspace.
1888 * We increment here. If we reach max number of fragments,
1889 * this indicates an underrun/overrun. For this case under OSS,
1890 * we stop the record/playback process.
1892 if (atomic_read (&chan
->n_frags
) < chan
->frag_number
)
1893 atomic_inc (&chan
->n_frags
);
1894 assert (atomic_read (&chan
->n_frags
) <= chan
->frag_number
);
1895 if (atomic_read (&chan
->n_frags
) == chan
->frag_number
) {
1896 chan
->is_active
= 0;
1897 via_chan_stop (chan
->iobase
);
1900 /* wake up anyone listening to see when interrupts occur */
1901 wake_up_all (&chan
->wait
);
1903 DPRINTK ("%s intr, status=0x%02X, hwptr=0x%lX, chan->hw_ptr=%d\n",
1904 chan
->name
, status
, (long) inl (chan
->iobase
+ 0x04),
1905 atomic_read (&chan
->hw_ptr
));
1907 DPRINTK ("%s intr, channel n_frags == %d, missed %d\n", chan
->name
,
1908 atomic_read (&chan
->n_frags
), missed
);
1912 static irqreturn_t
via_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
1914 struct via_info
*card
= dev_id
;
1917 /* to minimize interrupt sharing costs, we use the SGD status
1918 * shadow register to check the status of all inputs and
1919 * outputs with a single 32-bit bus read. If no interrupt
1920 * conditions are flagged, we exit immediately
1922 status32
= inl (card
->baseaddr
+ VIA_BASE0_SGD_STATUS_SHADOW
);
1923 if (!(status32
& VIA_INTR_MASK
))
1925 #ifdef CONFIG_MIDI_VIA82CXXX
1926 if (card
->midi_devc
)
1927 uart401intr(irq
, card
->midi_devc
, regs
);
1931 DPRINTK ("intr, status32 == 0x%08X\n", status32
);
1933 /* synchronize interrupt handling under SMP. this spinlock
1934 * goes away completely on UP
1936 spin_lock (&card
->lock
);
1938 if (status32
& VIA_INTR_OUT
)
1939 via_intr_channel (card
, &card
->ch_out
);
1940 if (status32
& VIA_INTR_IN
)
1941 via_intr_channel (card
, &card
->ch_in
);
1942 if (status32
& VIA_INTR_FM
)
1943 via_intr_channel (card
, &card
->ch_fm
);
1945 spin_unlock (&card
->lock
);
1950 static irqreturn_t
via_new_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
1952 struct via_info
*card
= dev_id
;
1955 /* to minimize interrupt sharing costs, we use the SGD status
1956 * shadow register to check the status of all inputs and
1957 * outputs with a single 32-bit bus read. If no interrupt
1958 * conditions are flagged, we exit immediately
1960 status32
= inl (card
->baseaddr
+ VIA_BASE0_SGD_STATUS_SHADOW
);
1961 if (!(status32
& VIA_NEW_INTR_MASK
))
1964 * goes away completely on UP
1966 spin_lock (&card
->lock
);
1968 via_intr_channel (card
, &card
->ch_out
);
1969 via_intr_channel (card
, &card
->ch_in
);
1970 via_intr_channel (card
, &card
->ch_fm
);
1972 spin_unlock (&card
->lock
);
1978 * via_interrupt_init - Initialize interrupt handling
1979 * @card: Private info for specified board
1981 * Obtain and reserve IRQ for using in handling audio events.
1982 * Also, disable any IRQ-generating resources, to make sure
1983 * we don't get interrupts before we want them.
1986 static int via_interrupt_init (struct via_info
*card
)
1990 DPRINTK ("ENTER\n");
1992 assert (card
!= NULL
);
1993 assert (card
->pdev
!= NULL
);
1995 /* check for sane IRQ number. can this ever happen? */
1996 if (card
->pdev
->irq
< 2) {
1997 printk (KERN_ERR PFX
"insane IRQ %d, aborting\n",
1999 DPRINTK ("EXIT, returning -EIO\n");
2003 /* VIA requires this is done */
2004 pci_write_config_byte(card
->pdev
, PCI_INTERRUPT_LINE
, card
->pdev
->irq
);
2008 /* make sure FM irq is not routed to us */
2009 pci_read_config_byte (card
->pdev
, VIA_FM_NMI_CTRL
, &tmp8
);
2010 if ((tmp8
& VIA_CR48_FM_TRAP_TO_NMI
) == 0) {
2011 tmp8
|= VIA_CR48_FM_TRAP_TO_NMI
;
2012 pci_write_config_byte (card
->pdev
, VIA_FM_NMI_CTRL
, tmp8
);
2014 if (request_irq (card
->pdev
->irq
, via_interrupt
, SA_SHIRQ
, VIA_MODULE_NAME
, card
)) {
2015 printk (KERN_ERR PFX
"unable to obtain IRQ %d, aborting\n",
2017 DPRINTK ("EXIT, returning -EBUSY\n");
2023 if (request_irq (card
->pdev
->irq
, via_new_interrupt
, SA_SHIRQ
, VIA_MODULE_NAME
, card
)) {
2024 printk (KERN_ERR PFX
"unable to obtain IRQ %d, aborting\n",
2026 DPRINTK ("EXIT, returning -EBUSY\n");
2031 DPRINTK ("EXIT, returning 0\n");
2036 /****************************************************************
2042 static struct file_operations via_dsp_fops
= {
2043 .owner
= THIS_MODULE
,
2044 .open
= via_dsp_open
,
2045 .release
= via_dsp_release
,
2046 .read
= via_dsp_read
,
2047 .write
= via_dsp_write
,
2048 .poll
= via_dsp_poll
,
2049 .llseek
= no_llseek
,
2050 .ioctl
= via_dsp_ioctl
,
2051 .mmap
= via_dsp_mmap
,
2055 static int __devinit
via_dsp_init (struct via_info
*card
)
2059 DPRINTK ("ENTER\n");
2061 assert (card
!= NULL
);
2065 /* turn off legacy features, if not already */
2066 pci_read_config_byte (card
->pdev
, VIA_FUNC_ENABLE
, &tmp8
);
2067 if (tmp8
& (VIA_CR42_SB_ENABLE
| VIA_CR42_FM_ENABLE
)) {
2068 tmp8
&= ~(VIA_CR42_SB_ENABLE
| VIA_CR42_FM_ENABLE
);
2069 pci_write_config_byte (card
->pdev
, VIA_FUNC_ENABLE
, tmp8
);
2073 via_stop_everything (card
);
2075 card
->dev_dsp
= register_sound_dsp (&via_dsp_fops
, -1);
2076 if (card
->dev_dsp
< 0) {
2077 DPRINTK ("EXIT, returning -ENODEV\n");
2080 DPRINTK ("EXIT, returning 0\n");
2085 static void via_dsp_cleanup (struct via_info
*card
)
2087 DPRINTK ("ENTER\n");
2089 assert (card
!= NULL
);
2090 assert (card
->dev_dsp
>= 0);
2092 via_stop_everything (card
);
2094 unregister_sound_dsp (card
->dev_dsp
);
2100 static struct page
* via_mm_nopage (struct vm_area_struct
* vma
,
2101 unsigned long address
, int *type
)
2103 struct via_info
*card
= vma
->vm_private_data
;
2104 struct via_channel
*chan
= &card
->ch_out
;
2105 struct page
*dmapage
;
2106 unsigned long pgoff
;
2109 DPRINTK ("ENTER, start %lXh, ofs %lXh, pgoff %ld, addr %lXh\n",
2111 address
- vma
->vm_start
,
2112 (address
- vma
->vm_start
) >> PAGE_SHIFT
,
2115 if (address
> vma
->vm_end
) {
2116 DPRINTK ("EXIT, returning NOPAGE_SIGBUS\n");
2117 return NOPAGE_SIGBUS
; /* Disallow mremap */
2120 DPRINTK ("EXIT, returning NOPAGE_OOM\n");
2121 return NOPAGE_OOM
; /* Nothing allocated */
2124 pgoff
= vma
->vm_pgoff
+ ((address
- vma
->vm_start
) >> PAGE_SHIFT
);
2125 rd
= card
->ch_in
.is_mapped
;
2126 wr
= card
->ch_out
.is_mapped
;
2130 unsigned long max_bufs
= chan
->frag_number
;
2131 if (rd
&& wr
) max_bufs
*= 2;
2132 /* via_dsp_mmap() should ensure this */
2133 assert (pgoff
< max_bufs
);
2137 /* if full-duplex (read+write) and we have two sets of bufs,
2138 * then the playback buffers come first, sez soundcard.c */
2139 if (pgoff
>= chan
->page_number
) {
2140 pgoff
-= chan
->page_number
;
2141 chan
= &card
->ch_in
;
2143 chan
= &card
->ch_in
;
2145 assert ((((unsigned long)chan
->pgtbl
[pgoff
].cpuaddr
) % PAGE_SIZE
) == 0);
2147 dmapage
= virt_to_page (chan
->pgtbl
[pgoff
].cpuaddr
);
2148 DPRINTK ("EXIT, returning page %p for cpuaddr %lXh\n",
2149 dmapage
, (unsigned long) chan
->pgtbl
[pgoff
].cpuaddr
);
2152 *type
= VM_FAULT_MINOR
;
2158 static int via_mm_swapout (struct page
*page
, struct file
*filp
)
2162 #endif /* VM_RESERVED */
2165 static struct vm_operations_struct via_mm_ops
= {
2166 .nopage
= via_mm_nopage
,
2169 .swapout
= via_mm_swapout
,
2174 static int via_dsp_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2176 struct via_info
*card
;
2177 int nonblock
= (file
->f_flags
& O_NONBLOCK
);
2178 int rc
= -EINVAL
, rd
=0, wr
=0;
2179 unsigned long max_size
, size
, start
, offset
;
2181 assert (file
!= NULL
);
2182 assert (vma
!= NULL
);
2183 card
= file
->private_data
;
2184 assert (card
!= NULL
);
2186 DPRINTK ("ENTER, start %lXh, size %ld, pgoff %ld\n",
2188 vma
->vm_end
- vma
->vm_start
,
2192 if (vma
->vm_flags
& VM_READ
) {
2194 via_chan_set_buffering(card
, &card
->ch_in
, -1);
2195 via_chan_buffer_init (card
, &card
->ch_in
);
2196 max_size
+= card
->ch_in
.page_number
<< PAGE_SHIFT
;
2198 if (vma
->vm_flags
& VM_WRITE
) {
2200 via_chan_set_buffering(card
, &card
->ch_out
, -1);
2201 via_chan_buffer_init (card
, &card
->ch_out
);
2202 max_size
+= card
->ch_out
.page_number
<< PAGE_SHIFT
;
2205 start
= vma
->vm_start
;
2206 offset
= (vma
->vm_pgoff
<< PAGE_SHIFT
);
2207 size
= vma
->vm_end
- vma
->vm_start
;
2209 /* some basic size/offset sanity checks */
2210 if (size
> max_size
)
2212 if (offset
> max_size
- size
)
2215 rc
= via_syscall_down (card
, nonblock
);
2218 vma
->vm_ops
= &via_mm_ops
;
2219 vma
->vm_private_data
= card
;
2222 vma
->vm_flags
|= VM_RESERVED
;
2226 card
->ch_in
.is_mapped
= 1;
2228 card
->ch_out
.is_mapped
= 1;
2230 up (&card
->syscall_sem
);
2234 DPRINTK ("EXIT, returning %d\n", rc
);
2239 static ssize_t
via_dsp_do_read (struct via_info
*card
,
2240 char __user
*userbuf
, size_t count
,
2243 DECLARE_WAITQUEUE(wait
, current
);
2244 const char __user
*orig_userbuf
= userbuf
;
2245 struct via_channel
*chan
= &card
->ch_in
;
2250 /* if SGD has not yet been started, start it */
2251 via_chan_maybe_start (chan
);
2254 /* just to be a nice neighbor */
2256 * But also to ourselves, release semaphore if we do so */
2257 if (need_resched()) {
2258 up(&card
->syscall_sem
);
2260 ret
= via_syscall_down (card
, nonblock
);
2265 /* grab current channel software pointer. In the case of
2266 * recording, this is pointing to the next buffer that
2267 * will receive data from the audio hardware.
2271 /* n_frags represents the number of fragments waiting
2272 * to be copied to userland. sleep until at least
2273 * one buffer has been read from the audio hardware.
2275 add_wait_queue(&chan
->wait
, &wait
);
2277 __set_current_state(TASK_INTERRUPTIBLE
);
2278 tmp
= atomic_read (&chan
->n_frags
);
2280 assert (tmp
<= chan
->frag_number
);
2283 if (nonblock
|| !chan
->is_active
) {
2288 up(&card
->syscall_sem
);
2290 DPRINTK ("Sleeping on block %d\n", n
);
2293 ret
= via_syscall_down (card
, nonblock
);
2297 if (signal_pending (current
)) {
2302 set_current_state(TASK_RUNNING
);
2303 remove_wait_queue(&chan
->wait
, &wait
);
2307 /* Now that we have a buffer we can read from, send
2308 * as much as sample data possible to userspace.
2310 while ((count
> 0) && (chan
->slop_len
< chan
->frag_size
)) {
2311 size_t slop_left
= chan
->frag_size
- chan
->slop_len
;
2312 void *base
= chan
->pgtbl
[n
/ (PAGE_SIZE
/ chan
->frag_size
)].cpuaddr
;
2313 unsigned ofs
= (n
% (PAGE_SIZE
/ chan
->frag_size
)) * chan
->frag_size
;
2315 size
= (count
< slop_left
) ? count
: slop_left
;
2316 if (copy_to_user (userbuf
,
2317 base
+ ofs
+ chan
->slop_len
,
2324 chan
->slop_len
+= size
;
2328 /* If we didn't copy the buffer completely to userspace,
2331 if (chan
->slop_len
< chan
->frag_size
)
2335 * If we get to this point, we copied one buffer completely
2336 * to userspace, give the buffer back to the hardware.
2339 /* advance channel software pointer to point to
2340 * the next buffer from which we will copy
2342 if (chan
->sw_ptr
== (chan
->frag_number
- 1))
2347 /* mark one less buffer waiting to be processed */
2348 assert (atomic_read (&chan
->n_frags
) > 0);
2349 atomic_dec (&chan
->n_frags
);
2351 /* we are at a block boundary, there is no fragment data */
2354 DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
2355 n
, chan
->sw_ptr
, atomic_read (&chan
->n_frags
));
2357 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2358 inb (card
->baseaddr
+ 0x00),
2359 inb (card
->baseaddr
+ 0x01),
2360 inb (card
->baseaddr
+ 0x02),
2361 inl (card
->baseaddr
+ 0x04),
2362 inl (card
->baseaddr
+ 0x0C),
2363 inl (card
->baseaddr
+ 0x80),
2364 inl (card
->baseaddr
+ 0x84));
2367 goto handle_one_block
;
2370 return (userbuf
!= orig_userbuf
) ? (userbuf
- orig_userbuf
) : ret
;
2374 static ssize_t
via_dsp_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
2376 struct via_info
*card
;
2377 int nonblock
= (file
->f_flags
& O_NONBLOCK
);
2380 DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
2381 file
, buffer
, count
, ppos
? ((unsigned long)*ppos
) : 0);
2383 assert (file
!= NULL
);
2384 card
= file
->private_data
;
2385 assert (card
!= NULL
);
2387 rc
= via_syscall_down (card
, nonblock
);
2390 if (card
->ch_in
.is_mapped
) {
2395 via_chan_set_buffering(card
, &card
->ch_in
, -1);
2396 rc
= via_chan_buffer_init (card
, &card
->ch_in
);
2401 rc
= via_dsp_do_read (card
, buffer
, count
, nonblock
);
2404 up (&card
->syscall_sem
);
2406 DPRINTK ("EXIT, returning %ld\n",(long) rc
);
2411 static ssize_t
via_dsp_do_write (struct via_info
*card
,
2412 const char __user
*userbuf
, size_t count
,
2415 DECLARE_WAITQUEUE(wait
, current
);
2416 const char __user
*orig_userbuf
= userbuf
;
2417 struct via_channel
*chan
= &card
->ch_out
;
2418 volatile struct via_sgd_table
*sgtable
= chan
->sgtable
;
2424 /* just to be a nice neighbor */
2426 * But also to ourselves, release semaphore if we do so */
2427 if (need_resched()) {
2428 up(&card
->syscall_sem
);
2430 ret
= via_syscall_down (card
, nonblock
);
2435 /* grab current channel fragment pointer. In the case of
2436 * playback, this is pointing to the next fragment that
2437 * should receive data from userland.
2441 /* n_frags represents the number of fragments remaining
2442 * to be filled by userspace. Sleep until
2443 * at least one fragment is available for our use.
2445 add_wait_queue(&chan
->wait
, &wait
);
2447 __set_current_state(TASK_INTERRUPTIBLE
);
2448 tmp
= atomic_read (&chan
->n_frags
);
2450 assert (tmp
<= chan
->frag_number
);
2453 if (nonblock
|| !chan
->is_active
) {
2458 up(&card
->syscall_sem
);
2460 DPRINTK ("Sleeping on page %d, tmp==%d, ir==%d\n", n
, tmp
, chan
->is_record
);
2463 ret
= via_syscall_down (card
, nonblock
);
2467 if (signal_pending (current
)) {
2472 set_current_state(TASK_RUNNING
);
2473 remove_wait_queue(&chan
->wait
, &wait
);
2477 /* Now that we have at least one fragment we can write to, fill the buffer
2478 * as much as possible with data from userspace.
2480 while ((count
> 0) && (chan
->slop_len
< chan
->frag_size
)) {
2481 size_t slop_left
= chan
->frag_size
- chan
->slop_len
;
2483 size
= (count
< slop_left
) ? count
: slop_left
;
2484 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
,
2491 chan
->slop_len
+= size
;
2495 /* If we didn't fill up the buffer with data, stop now.
2496 * Put a 'stop' marker in the DMA table too, to tell the
2497 * audio hardware to stop if it gets here.
2499 if (chan
->slop_len
< chan
->frag_size
) {
2500 sgtable
[n
].count
= cpu_to_le32 (chan
->slop_len
| VIA_EOL
| VIA_STOP
);
2505 * If we get to this point, we have filled a buffer with
2506 * audio data, flush the buffer to audio hardware.
2509 /* Record the true size for the audio hardware to notice */
2510 if (n
== (chan
->frag_number
- 1))
2511 sgtable
[n
].count
= cpu_to_le32 (chan
->frag_size
| VIA_EOL
);
2513 sgtable
[n
].count
= cpu_to_le32 (chan
->frag_size
| VIA_FLAG
);
2515 /* advance channel software pointer to point to
2516 * the next buffer we will fill with data
2518 if (chan
->sw_ptr
== (chan
->frag_number
- 1))
2523 /* mark one less buffer as being available for userspace consumption */
2524 assert (atomic_read (&chan
->n_frags
) > 0);
2525 atomic_dec (&chan
->n_frags
);
2527 /* we are at a block boundary, there is no fragment data */
2530 /* if SGD has not yet been started, start it */
2531 via_chan_maybe_start (chan
);
2533 DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
2534 n
, chan
->sw_ptr
, atomic_read (&chan
->n_frags
));
2536 DPRINTK ("regs==S=%02X C=%02X TP=%02X BP=%08X RT=%08X SG=%08X CC=%08X SS=%08X\n",
2537 inb (card
->baseaddr
+ 0x00),
2538 inb (card
->baseaddr
+ 0x01),
2539 inb (card
->baseaddr
+ 0x02),
2540 inl (card
->baseaddr
+ 0x04),
2541 inl (card
->baseaddr
+ 0x08),
2542 inl (card
->baseaddr
+ 0x0C),
2543 inl (card
->baseaddr
+ 0x80),
2544 inl (card
->baseaddr
+ 0x84));
2547 goto handle_one_block
;
2550 if (userbuf
- orig_userbuf
)
2551 return userbuf
- orig_userbuf
;
2557 static ssize_t
via_dsp_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
2559 struct via_info
*card
;
2561 int nonblock
= (file
->f_flags
& O_NONBLOCK
);
2563 DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
2564 file
, buffer
, count
, ppos
? ((unsigned long)*ppos
) : 0);
2566 assert (file
!= NULL
);
2567 card
= file
->private_data
;
2568 assert (card
!= NULL
);
2570 rc
= via_syscall_down (card
, nonblock
);
2573 if (card
->ch_out
.is_mapped
) {
2578 via_chan_set_buffering(card
, &card
->ch_out
, -1);
2579 rc
= via_chan_buffer_init (card
, &card
->ch_out
);
2584 rc
= via_dsp_do_write (card
, buffer
, count
, nonblock
);
2587 up (&card
->syscall_sem
);
2589 DPRINTK ("EXIT, returning %ld\n",(long) rc
);
2594 static unsigned int via_dsp_poll(struct file
*file
, struct poll_table_struct
*wait
)
2596 struct via_info
*card
;
2597 struct via_channel
*chan
;
2598 unsigned int mask
= 0;
2600 DPRINTK ("ENTER\n");
2602 assert (file
!= NULL
);
2603 card
= file
->private_data
;
2604 assert (card
!= NULL
);
2606 if (file
->f_mode
& FMODE_READ
) {
2607 chan
= &card
->ch_in
;
2608 if (sg_active (chan
->iobase
))
2609 poll_wait(file
, &chan
->wait
, wait
);
2610 if (atomic_read (&chan
->n_frags
) > 0)
2611 mask
|= POLLIN
| POLLRDNORM
;
2614 if (file
->f_mode
& FMODE_WRITE
) {
2615 chan
= &card
->ch_out
;
2616 if (sg_active (chan
->iobase
))
2617 poll_wait(file
, &chan
->wait
, wait
);
2618 if (atomic_read (&chan
->n_frags
) > 0)
2619 mask
|= POLLOUT
| POLLWRNORM
;
2622 DPRINTK ("EXIT, returning %u\n", mask
);
2628 * via_dsp_drain_playback - sleep until all playback samples are flushed
2629 * @card: Private info for specified board
2630 * @chan: Channel to drain
2631 * @nonblock: boolean, non-zero if O_NONBLOCK is set
2633 * Sleeps until all playback has been flushed to the audio
2636 * Locking: inside card->syscall_sem
2639 static int via_dsp_drain_playback (struct via_info
*card
,
2640 struct via_channel
*chan
, int nonblock
)
2642 DECLARE_WAITQUEUE(wait
, current
);
2645 DPRINTK ("ENTER, nonblock = %d\n", nonblock
);
2647 if (chan
->slop_len
> 0)
2648 via_chan_flush_frag (chan
);
2650 if (atomic_read (&chan
->n_frags
) == chan
->frag_number
)
2653 via_chan_maybe_start (chan
);
2655 add_wait_queue(&chan
->wait
, &wait
);
2657 DPRINTK ("FRAGS %d FRAGNUM %d\n", atomic_read(&chan
->n_frags
), chan
->frag_number
);
2658 __set_current_state(TASK_INTERRUPTIBLE
);
2659 if (atomic_read (&chan
->n_frags
) >= chan
->frag_number
)
2663 DPRINTK ("EXIT, returning -EAGAIN\n");
2670 u8 r40
,r41
,r42
,r43
,r44
,r48
;
2671 pci_read_config_byte (card
->pdev
, 0x40, &r40
);
2672 pci_read_config_byte (card
->pdev
, 0x41, &r41
);
2673 pci_read_config_byte (card
->pdev
, 0x42, &r42
);
2674 pci_read_config_byte (card
->pdev
, 0x43, &r43
);
2675 pci_read_config_byte (card
->pdev
, 0x44, &r44
);
2676 pci_read_config_byte (card
->pdev
, 0x48, &r48
);
2677 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
2678 r40
,r41
,r42
,r43
,r44
,r48
);
2680 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2681 inb (card
->baseaddr
+ 0x00),
2682 inb (card
->baseaddr
+ 0x01),
2683 inb (card
->baseaddr
+ 0x02),
2684 inl (card
->baseaddr
+ 0x04),
2685 inl (card
->baseaddr
+ 0x0C),
2686 inl (card
->baseaddr
+ 0x80),
2687 inl (card
->baseaddr
+ 0x84));
2690 if (!chan
->is_active
)
2691 printk (KERN_ERR
"sleeping but not active\n");
2694 up(&card
->syscall_sem
);
2696 DPRINTK ("sleeping, nbufs=%d\n", atomic_read (&chan
->n_frags
));
2699 if ((ret
= via_syscall_down (card
, nonblock
)))
2702 if (signal_pending (current
)) {
2703 DPRINTK ("EXIT, returning -ERESTARTSYS\n");
2708 set_current_state(TASK_RUNNING
);
2709 remove_wait_queue(&chan
->wait
, &wait
);
2713 u8 r40
,r41
,r42
,r43
,r44
,r48
;
2714 pci_read_config_byte (card
->pdev
, 0x40, &r40
);
2715 pci_read_config_byte (card
->pdev
, 0x41, &r41
);
2716 pci_read_config_byte (card
->pdev
, 0x42, &r42
);
2717 pci_read_config_byte (card
->pdev
, 0x43, &r43
);
2718 pci_read_config_byte (card
->pdev
, 0x44, &r44
);
2719 pci_read_config_byte (card
->pdev
, 0x48, &r48
);
2720 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
2721 r40
,r41
,r42
,r43
,r44
,r48
);
2723 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2724 inb (card
->baseaddr
+ 0x00),
2725 inb (card
->baseaddr
+ 0x01),
2726 inb (card
->baseaddr
+ 0x02),
2727 inl (card
->baseaddr
+ 0x04),
2728 inl (card
->baseaddr
+ 0x0C),
2729 inl (card
->baseaddr
+ 0x80),
2730 inl (card
->baseaddr
+ 0x84));
2732 DPRINTK ("final nbufs=%d\n", atomic_read (&chan
->n_frags
));
2737 DPRINTK ("EXIT, returning %d\n", ret
);
2743 * via_dsp_ioctl_space - get information about channel buffering
2744 * @card: Private info for specified board
2745 * @chan: pointer to channel-specific info
2746 * @arg: user buffer for returned information
2748 * Handles SNDCTL_DSP_GETISPACE and SNDCTL_DSP_GETOSPACE.
2750 * Locking: inside card->syscall_sem
2753 static int via_dsp_ioctl_space (struct via_info
*card
,
2754 struct via_channel
*chan
,
2757 audio_buf_info info
;
2759 via_chan_set_buffering(card
, chan
, -1);
2761 info
.fragstotal
= chan
->frag_number
;
2762 info
.fragsize
= chan
->frag_size
;
2764 /* number of full fragments we can read/write without blocking */
2765 info
.fragments
= atomic_read (&chan
->n_frags
);
2767 if ((chan
->slop_len
% chan
->frag_size
> 0) && (info
.fragments
> 0))
2770 /* number of bytes that can be read or written immediately
2773 info
.bytes
= (info
.fragments
* chan
->frag_size
);
2774 if (chan
->slop_len
% chan
->frag_size
> 0)
2775 info
.bytes
+= chan
->frag_size
- (chan
->slop_len
% chan
->frag_size
);
2777 DPRINTK ("EXIT, returning fragstotal=%d, fragsize=%d, fragments=%d, bytes=%d\n",
2783 return copy_to_user (arg
, &info
, sizeof (info
))?-EFAULT
:0;
2788 * via_dsp_ioctl_ptr - get information about hardware buffer ptr
2789 * @card: Private info for specified board
2790 * @chan: pointer to channel-specific info
2791 * @arg: user buffer for returned information
2793 * Handles SNDCTL_DSP_GETIPTR and SNDCTL_DSP_GETOPTR.
2795 * Locking: inside card->syscall_sem
2798 static int via_dsp_ioctl_ptr (struct via_info
*card
,
2799 struct via_channel
*chan
,
2804 spin_lock_irq (&card
->lock
);
2806 info
.bytes
= chan
->bytes
;
2807 info
.blocks
= chan
->n_irqs
;
2810 spin_unlock_irq (&card
->lock
);
2812 if (chan
->is_active
) {
2813 unsigned long extra
;
2814 info
.ptr
= atomic_read (&chan
->hw_ptr
) * chan
->frag_size
;
2815 extra
= chan
->frag_size
- via_sg_offset(chan
);
2817 info
.bytes
+= extra
;
2822 DPRINTK ("EXIT, returning bytes=%d, blocks=%d, ptr=%d\n",
2827 return copy_to_user (arg
, &info
, sizeof (info
))?-EFAULT
:0;
2831 static int via_dsp_ioctl_trigger (struct via_channel
*chan
, int val
)
2833 int enable
, do_something
;
2835 if (chan
->is_record
)
2836 enable
= (val
& PCM_ENABLE_INPUT
);
2838 enable
= (val
& PCM_ENABLE_OUTPUT
);
2840 if (!chan
->is_enabled
&& enable
) {
2842 } else if (chan
->is_enabled
&& !enable
) {
2848 DPRINTK ("enable=%d, do_something=%d\n",
2849 enable
, do_something
);
2851 if (chan
->is_active
&& do_something
)
2854 if (do_something
== 1) {
2855 chan
->is_enabled
= 1;
2856 via_chan_maybe_start (chan
);
2857 DPRINTK ("Triggering input\n");
2860 else if (do_something
== -1) {
2861 chan
->is_enabled
= 0;
2862 DPRINTK ("Setup input trigger\n");
2869 static int via_dsp_ioctl (struct inode
*inode
, struct file
*file
,
2870 unsigned int cmd
, unsigned long arg
)
2872 int rc
, rd
=0, wr
=0, val
=0;
2873 struct via_info
*card
;
2874 struct via_channel
*chan
;
2875 int nonblock
= (file
->f_flags
& O_NONBLOCK
);
2876 int __user
*ip
= (int __user
*)arg
;
2877 void __user
*p
= (void __user
*)arg
;
2879 assert (file
!= NULL
);
2880 card
= file
->private_data
;
2881 assert (card
!= NULL
);
2883 if (file
->f_mode
& FMODE_WRITE
)
2885 if (file
->f_mode
& FMODE_READ
)
2888 rc
= via_syscall_down (card
, nonblock
);
2895 /* OSS API version. XXX unverified */
2896 case OSS_GETVERSION
:
2897 DPRINTK ("ioctl OSS_GETVERSION, EXIT, returning SOUND_VERSION\n");
2898 rc
= put_user (SOUND_VERSION
, ip
);
2901 /* list of supported PCM data formats */
2902 case SNDCTL_DSP_GETFMTS
:
2903 DPRINTK ("DSP_GETFMTS, EXIT, returning AFMT U8|S16_LE\n");
2904 rc
= put_user (AFMT_U8
| AFMT_S16_LE
, ip
);
2907 /* query or set current channel's PCM data format */
2908 case SNDCTL_DSP_SETFMT
:
2909 if (get_user(val
, ip
)) {
2913 DPRINTK ("DSP_SETFMT, val==%d\n", val
);
2914 if (val
!= AFMT_QUERY
) {
2918 rc
= via_chan_set_fmt (card
, &card
->ch_in
, val
);
2921 rc
= via_chan_set_fmt (card
, &card
->ch_out
, val
);
2928 if ((rd
&& (card
->ch_in
.pcm_fmt
& VIA_PCM_FMT_16BIT
)) ||
2929 (wr
&& (card
->ch_out
.pcm_fmt
& VIA_PCM_FMT_16BIT
)))
2934 DPRINTK ("SETFMT EXIT, returning %d\n", val
);
2935 rc
= put_user (val
, ip
);
2938 /* query or set number of channels (1=mono, 2=stereo, 4/6 for multichannel) */
2939 case SNDCTL_DSP_CHANNELS
:
2940 if (get_user(val
, ip
)) {
2944 DPRINTK ("DSP_CHANNELS, val==%d\n", val
);
2949 rc
= via_chan_set_stereo (card
, &card
->ch_in
, val
);
2952 rc
= via_chan_set_stereo (card
, &card
->ch_out
, val
);
2960 val
= card
->ch_in
.channels
;
2962 val
= card
->ch_out
.channels
;
2964 DPRINTK ("CHANNELS EXIT, returning %d\n", val
);
2965 rc
= put_user (val
, ip
);
2968 /* enable (val is not zero) or disable (val == 0) stereo */
2969 case SNDCTL_DSP_STEREO
:
2970 if (get_user(val
, ip
)) {
2974 DPRINTK ("DSP_STEREO, val==%d\n", val
);
2978 rc
= via_chan_set_stereo (card
, &card
->ch_in
, val
? 2 : 1);
2980 rc
= via_chan_set_stereo (card
, &card
->ch_out
, val
? 2 : 1);
2987 DPRINTK ("STEREO EXIT, returning %d\n", val
);
2988 rc
= put_user(val
, ip
);
2991 /* query or set sampling rate */
2992 case SNDCTL_DSP_SPEED
:
2993 if (get_user(val
, ip
)) {
2997 DPRINTK ("DSP_SPEED, val==%d\n", val
);
3006 rc
= via_chan_set_speed (card
, &card
->ch_in
, val
);
3008 rc
= via_chan_set_speed (card
, &card
->ch_out
, val
);
3016 val
= card
->ch_in
.rate
;
3018 val
= card
->ch_out
.rate
;
3022 DPRINTK ("SPEED EXIT, returning %d\n", val
);
3023 rc
= put_user (val
, ip
);
3026 /* wait until all buffers have been played, and then stop device */
3027 case SNDCTL_DSP_SYNC
:
3028 DPRINTK ("DSP_SYNC\n");
3031 DPRINTK ("SYNC EXIT (after calling via_dsp_drain_playback)\n");
3032 rc
= via_dsp_drain_playback (card
, &card
->ch_out
, nonblock
);
3036 /* stop recording/playback immediately */
3037 case SNDCTL_DSP_RESET
:
3038 DPRINTK ("DSP_RESET\n");
3040 via_chan_clear (card
, &card
->ch_in
);
3041 card
->ch_in
.frag_number
= 0;
3042 card
->ch_in
.frag_size
= 0;
3043 atomic_set(&card
->ch_in
.n_frags
, 0);
3047 via_chan_clear (card
, &card
->ch_out
);
3048 card
->ch_out
.frag_number
= 0;
3049 card
->ch_out
.frag_size
= 0;
3050 atomic_set(&card
->ch_out
.n_frags
, 0);
3056 case SNDCTL_DSP_NONBLOCK
:
3057 file
->f_flags
|= O_NONBLOCK
;
3061 /* obtain bitmask of device capabilities, such as mmap, full duplex, etc. */
3062 case SNDCTL_DSP_GETCAPS
:
3063 DPRINTK ("DSP_GETCAPS\n");
3064 rc
= put_user(VIA_DSP_CAP
, ip
);
3067 /* obtain buffer fragment size */
3068 case SNDCTL_DSP_GETBLKSIZE
:
3069 DPRINTK ("DSP_GETBLKSIZE\n");
3072 via_chan_set_buffering(card
, &card
->ch_in
, -1);
3073 rc
= put_user(card
->ch_in
.frag_size
, ip
);
3075 via_chan_set_buffering(card
, &card
->ch_out
, -1);
3076 rc
= put_user(card
->ch_out
.frag_size
, ip
);
3080 /* obtain information about input buffering */
3081 case SNDCTL_DSP_GETISPACE
:
3082 DPRINTK ("DSP_GETISPACE\n");
3084 rc
= via_dsp_ioctl_space (card
, &card
->ch_in
, p
);
3087 /* obtain information about output buffering */
3088 case SNDCTL_DSP_GETOSPACE
:
3089 DPRINTK ("DSP_GETOSPACE\n");
3091 rc
= via_dsp_ioctl_space (card
, &card
->ch_out
, p
);
3094 /* obtain information about input hardware pointer */
3095 case SNDCTL_DSP_GETIPTR
:
3096 DPRINTK ("DSP_GETIPTR\n");
3098 rc
= via_dsp_ioctl_ptr (card
, &card
->ch_in
, p
);
3101 /* obtain information about output hardware pointer */
3102 case SNDCTL_DSP_GETOPTR
:
3103 DPRINTK ("DSP_GETOPTR\n");
3105 rc
= via_dsp_ioctl_ptr (card
, &card
->ch_out
, p
);
3108 /* return number of bytes remaining to be played by DMA engine */
3109 case SNDCTL_DSP_GETODELAY
:
3111 DPRINTK ("DSP_GETODELAY\n");
3113 chan
= &card
->ch_out
;
3118 if (chan
->is_active
) {
3120 val
= chan
->frag_number
- atomic_read (&chan
->n_frags
);
3125 val
*= chan
->frag_size
;
3126 val
-= chan
->frag_size
- via_sg_offset(chan
);
3128 val
+= chan
->slop_len
% chan
->frag_size
;
3132 assert (val
<= (chan
->frag_size
* chan
->frag_number
));
3134 DPRINTK ("GETODELAY EXIT, val = %d bytes\n", val
);
3135 rc
= put_user (val
, ip
);
3139 /* handle the quick-start of a channel,
3140 * or the notification that a quick-start will
3141 * occur in the future
3143 case SNDCTL_DSP_SETTRIGGER
:
3144 if (get_user(val
, ip
)) {
3148 DPRINTK ("DSP_SETTRIGGER, rd=%d, wr=%d, act=%d/%d, en=%d/%d\n",
3149 rd
, wr
, card
->ch_in
.is_active
, card
->ch_out
.is_active
,
3150 card
->ch_in
.is_enabled
, card
->ch_out
.is_enabled
);
3155 rc
= via_dsp_ioctl_trigger (&card
->ch_in
, val
);
3158 rc
= via_dsp_ioctl_trigger (&card
->ch_out
, val
);
3162 case SNDCTL_DSP_GETTRIGGER
:
3164 if ((file
->f_mode
& FMODE_READ
) && card
->ch_in
.is_enabled
)
3165 val
|= PCM_ENABLE_INPUT
;
3166 if ((file
->f_mode
& FMODE_WRITE
) && card
->ch_out
.is_enabled
)
3167 val
|= PCM_ENABLE_OUTPUT
;
3168 rc
= put_user(val
, ip
);
3171 /* Enable full duplex. Since we do this as soon as we are opened
3172 * with O_RDWR, this is mainly a no-op that always returns success.
3174 case SNDCTL_DSP_SETDUPLEX
:
3175 DPRINTK ("DSP_SETDUPLEX\n");
3181 /* set fragment size. implemented as a successful no-op for now */
3182 case SNDCTL_DSP_SETFRAGMENT
:
3183 if (get_user(val
, ip
)) {
3187 DPRINTK ("DSP_SETFRAGMENT, val==%d\n", val
);
3190 rc
= via_chan_set_buffering(card
, &card
->ch_in
, val
);
3193 rc
= via_chan_set_buffering(card
, &card
->ch_out
, val
);
3195 DPRINTK ("SNDCTL_DSP_SETFRAGMENT (fragshift==0x%04X (%d), maxfrags==0x%04X (%d))\n",
3198 (val
>> 16) & 0xFFFF,
3199 (val
>> 16) & 0xFFFF);
3204 /* inform device of an upcoming pause in input (or output). */
3205 case SNDCTL_DSP_POST
:
3206 DPRINTK ("DSP_POST\n");
3208 if (card
->ch_out
.slop_len
> 0)
3209 via_chan_flush_frag (&card
->ch_out
);
3210 via_chan_maybe_start (&card
->ch_out
);
3216 /* not implemented */
3218 DPRINTK ("unhandled ioctl, cmd==%u, arg==%p\n",
3223 up (&card
->syscall_sem
);
3224 DPRINTK ("EXIT, returning %d\n", rc
);
3229 static int via_dsp_open (struct inode
*inode
, struct file
*file
)
3231 int minor
= iminor(inode
);
3232 struct via_info
*card
;
3233 struct pci_dev
*pdev
= NULL
;
3234 struct via_channel
*chan
;
3235 struct pci_driver
*drvr
;
3236 int nonblock
= (file
->f_flags
& O_NONBLOCK
);
3238 DPRINTK ("ENTER, minor=%d, file->f_mode=0x%x\n", minor
, file
->f_mode
);
3240 if (!(file
->f_mode
& (FMODE_READ
| FMODE_WRITE
))) {
3241 DPRINTK ("EXIT, returning -EINVAL\n");
3246 while ((pdev
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pdev
)) != NULL
) {
3247 drvr
= pci_dev_driver (pdev
);
3248 if (drvr
== &via_driver
) {
3249 assert (pci_get_drvdata (pdev
) != NULL
);
3251 card
= pci_get_drvdata (pdev
);
3252 DPRINTK ("dev_dsp = %d, minor = %d, assn = %d\n",
3253 card
->dev_dsp
, minor
,
3254 (card
->dev_dsp
^ minor
) & ~0xf);
3256 if (((card
->dev_dsp
^ minor
) & ~0xf) == 0)
3261 DPRINTK ("no matching %s found\n", card
? "minor" : "driver");
3266 if (down_trylock (&card
->open_sem
)) {
3267 DPRINTK ("EXIT, returning -EAGAIN\n");
3271 if (down_interruptible (&card
->open_sem
)) {
3272 DPRINTK ("EXIT, returning -ERESTARTSYS\n");
3273 return -ERESTARTSYS
;
3277 file
->private_data
= card
;
3278 DPRINTK ("file->f_mode == 0x%x\n", file
->f_mode
);
3280 /* handle input from analog source */
3281 if (file
->f_mode
& FMODE_READ
) {
3282 chan
= &card
->ch_in
;
3284 via_chan_init (card
, chan
);
3286 /* why is this forced to 16-bit stereo in all drivers? */
3287 chan
->pcm_fmt
= VIA_PCM_FMT_16BIT
| VIA_PCM_FMT_STEREO
;
3290 // TO DO - use FIFO: via_capture_fifo(card, 1);
3291 via_chan_pcm_fmt (chan
, 0);
3292 via_set_rate (card
->ac97
, chan
, 44100);
3295 /* handle output to analog source */
3296 if (file
->f_mode
& FMODE_WRITE
) {
3297 chan
= &card
->ch_out
;
3299 via_chan_init (card
, chan
);
3301 if (file
->f_mode
& FMODE_READ
) {
3302 /* if in duplex mode make the recording and playback channels
3303 have the same settings */
3304 chan
->pcm_fmt
= VIA_PCM_FMT_16BIT
| VIA_PCM_FMT_STEREO
;
3306 via_chan_pcm_fmt (chan
, 0);
3307 via_set_rate (card
->ac97
, chan
, 44100);
3309 if ((minor
& 0xf) == SND_DEV_DSP16
) {
3310 chan
->pcm_fmt
= VIA_PCM_FMT_16BIT
;
3311 via_chan_pcm_fmt (chan
, 0);
3312 via_set_rate (card
->ac97
, chan
, 44100);
3314 via_chan_pcm_fmt (chan
, 1);
3315 via_set_rate (card
->ac97
, chan
, 8000);
3320 DPRINTK ("EXIT, returning 0\n");
3321 return nonseekable_open(inode
, file
);
3325 static int via_dsp_release(struct inode
*inode
, struct file
*file
)
3327 struct via_info
*card
;
3328 int nonblock
= (file
->f_flags
& O_NONBLOCK
);
3331 DPRINTK ("ENTER\n");
3333 assert (file
!= NULL
);
3334 card
= file
->private_data
;
3335 assert (card
!= NULL
);
3337 rc
= via_syscall_down (card
, nonblock
);
3339 DPRINTK ("EXIT (syscall_down error), rc=%d\n", rc
);
3343 if (file
->f_mode
& FMODE_WRITE
) {
3344 rc
= via_dsp_drain_playback (card
, &card
->ch_out
, nonblock
);
3345 if (rc
&& rc
!= -ERESTARTSYS
) /* Nobody needs to know about ^C */
3346 printk (KERN_DEBUG
"via_audio: ignoring drain playback error %d\n", rc
);
3348 via_chan_free (card
, &card
->ch_out
);
3349 via_chan_buffer_free(card
, &card
->ch_out
);
3352 if (file
->f_mode
& FMODE_READ
) {
3353 via_chan_free (card
, &card
->ch_in
);
3354 via_chan_buffer_free (card
, &card
->ch_in
);
3357 up (&card
->syscall_sem
);
3358 up (&card
->open_sem
);
3360 DPRINTK ("EXIT, returning 0\n");
3365 /****************************************************************
3367 * Chip setup and kernel registration
3372 static int __devinit
via_init_one (struct pci_dev
*pdev
, const struct pci_device_id
*id
)
3374 #ifdef CONFIG_MIDI_VIA82CXXX
3378 struct via_info
*card
;
3379 static int printed_version
;
3381 DPRINTK ("ENTER\n");
3383 if (printed_version
++ == 0)
3384 printk (KERN_INFO
"Via 686a/8233/8235 audio driver " VIA_VERSION
"\n");
3386 rc
= pci_enable_device (pdev
);
3390 rc
= pci_request_regions (pdev
, "via82cxxx_audio");
3392 goto err_out_disable
;
3394 rc
= pci_set_dma_mask(pdev
, 0xffffffffULL
);
3397 rc
= pci_set_consistent_dma_mask(pdev
, 0xffffffffULL
);
3401 card
= kmalloc (sizeof (*card
), GFP_KERNEL
);
3403 printk (KERN_ERR PFX
"out of memory, aborting\n");
3408 pci_set_drvdata (pdev
, card
);
3410 memset (card
, 0, sizeof (*card
));
3412 card
->baseaddr
= pci_resource_start (pdev
, 0);
3413 card
->card_num
= via_num_cards
++;
3414 spin_lock_init (&card
->lock
);
3415 spin_lock_init (&card
->ac97_lock
);
3416 init_MUTEX (&card
->syscall_sem
);
3417 init_MUTEX (&card
->open_sem
);
3419 /* we must init these now, in case the intr handler needs them */
3420 via_chan_init_defaults (card
, &card
->ch_out
);
3421 via_chan_init_defaults (card
, &card
->ch_in
);
3422 via_chan_init_defaults (card
, &card
->ch_fm
);
3424 /* if BAR 2 is present, chip is Rev H or later,
3425 * which means it has a few extra features */
3426 if (pci_resource_start (pdev
, 2) > 0)
3429 /* Overkill for now, but more flexible done right */
3431 card
->intmask
= id
->driver_data
;
3432 card
->legacy
= !card
->intmask
;
3433 card
->sixchannel
= id
->driver_data
;
3435 if(card
->sixchannel
)
3436 printk(KERN_INFO PFX
"Six channel audio available\n");
3437 if (pdev
->irq
< 1) {
3438 printk (KERN_ERR PFX
"invalid PCI IRQ %d, aborting\n", pdev
->irq
);
3443 if (!(pci_resource_flags (pdev
, 0) & IORESOURCE_IO
)) {
3444 printk (KERN_ERR PFX
"unable to locate I/O resources, aborting\n");
3449 pci_set_master(pdev
);
3452 * init AC97 mixer and codec
3454 rc
= via_ac97_init (card
);
3456 printk (KERN_ERR PFX
"AC97 init failed, aborting\n");
3463 rc
= via_dsp_init (card
);
3465 printk (KERN_ERR PFX
"DSP device init failed, aborting\n");
3466 goto err_out_have_mixer
;
3470 * init and turn on interrupts, as the last thing we do
3472 rc
= via_interrupt_init (card
);
3474 printk (KERN_ERR PFX
"interrupt init failed, aborting\n");
3475 goto err_out_have_dsp
;
3478 printk (KERN_INFO PFX
"board #%d at 0x%04lX, IRQ %d\n",
3479 card
->card_num
+ 1, card
->baseaddr
, pdev
->irq
);
3481 #ifdef CONFIG_MIDI_VIA82CXXX
3482 /* Disable by default */
3483 card
->midi_info
.io_base
= 0;
3487 pci_read_config_byte (pdev
, 0x42, &r42
);
3488 /* Disable MIDI interrupt */
3489 pci_write_config_byte (pdev
, 0x42, r42
| VIA_CR42_MIDI_IRQMASK
);
3490 if (r42
& VIA_CR42_MIDI_ENABLE
)
3492 if (r42
& VIA_CR42_MIDI_PNP
) /* Address selected by iobase 2 - not tested */
3493 card
->midi_info
.io_base
= pci_resource_start (pdev
, 2);
3494 else /* Address selected by byte 0x43 */
3497 pci_read_config_byte (pdev
, 0x43, &r43
);
3498 card
->midi_info
.io_base
= 0x300 + ((r43
& 0x0c) << 2);
3501 card
->midi_info
.irq
= -pdev
->irq
;
3502 if (probe_uart401(& card
->midi_info
, THIS_MODULE
))
3504 card
->midi_devc
=midi_devs
[card
->midi_info
.slots
[4]]->devc
;
3505 pci_write_config_byte(pdev
, 0x42, r42
& ~VIA_CR42_MIDI_IRQMASK
);
3506 printk("Enabled Via MIDI\n");
3512 DPRINTK ("EXIT, returning 0\n");
3516 via_dsp_cleanup (card
);
3519 via_ac97_cleanup (card
);
3523 memset (card
, 0xAB, sizeof (*card
)); /* poison memory */
3528 pci_release_regions (pdev
);
3531 pci_disable_device (pdev
);
3534 pci_set_drvdata (pdev
, NULL
);
3535 DPRINTK ("EXIT - returning %d\n", rc
);
3540 static void __devexit
via_remove_one (struct pci_dev
*pdev
)
3542 struct via_info
*card
;
3544 DPRINTK ("ENTER\n");
3546 assert (pdev
!= NULL
);
3547 card
= pci_get_drvdata (pdev
);
3548 assert (card
!= NULL
);
3550 #ifdef CONFIG_MIDI_VIA82CXXX
3551 if (card
->midi_info
.io_base
)
3552 unload_uart401(&card
->midi_info
);
3555 free_irq (card
->pdev
->irq
, card
);
3556 via_dsp_cleanup (card
);
3557 via_ac97_cleanup (card
);
3560 memset (card
, 0xAB, sizeof (*card
)); /* poison memory */
3564 pci_set_drvdata (pdev
, NULL
);
3566 pci_release_regions (pdev
);
3567 pci_disable_device (pdev
);
3568 pci_set_power_state (pdev
, 3); /* ...zzzzzz */
3575 /****************************************************************
3577 * Driver initialization and cleanup
3582 static int __init
init_via82cxxx_audio(void)
3586 DPRINTK ("ENTER\n");
3588 rc
= pci_register_driver (&via_driver
);
3590 DPRINTK ("EXIT, returning %d\n", rc
);
3594 DPRINTK ("EXIT, returning 0\n");
3599 static void __exit
cleanup_via82cxxx_audio(void)
3601 DPRINTK ("ENTER\n");
3603 pci_unregister_driver (&via_driver
);
3609 module_init(init_via82cxxx_audio
);
3610 module_exit(cleanup_via82cxxx_audio
);
3612 MODULE_AUTHOR("Jeff Garzik");
3613 MODULE_DESCRIPTION("DSP audio and mixer driver for Via 82Cxxx audio devices");
3614 MODULE_LICENSE("GPL");