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 #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
;
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 __user
*buffer
, size_t count
, loff_t
*ppos
);
353 static ssize_t
via_dsp_write(struct file
*file
, const char __user
*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
,
403 .probe
= via_init_one
,
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
;
1244 DPRINTK ("EXIT, returning %d\n", val
);
1248 static int via_chan_set_buffering (struct via_info
*card
,
1249 struct via_channel
*chan
, int val
)
1253 DPRINTK ("ENTER\n");
1255 /* in both cases the buffer cannot be changed */
1256 if (chan
->is_active
|| chan
->is_mapped
) {
1261 /* called outside SETFRAGMENT */
1262 /* set defaults or do nothing */
1265 if (chan
->frag_size
&& chan
->frag_number
)
1270 chan
->frag_size
= (VIA_DEFAULT_FRAG_TIME
* chan
->rate
* chan
->channels
1271 * ((chan
->pcm_fmt
& VIA_PCM_FMT_16BIT
) ? 2 : 1)) / 1000 - 1;
1274 while (chan
->frag_size
) {
1275 chan
->frag_size
>>= 1;
1278 chan
->frag_size
= 1 << shift
;
1280 chan
->frag_number
= (VIA_DEFAULT_BUFFER_TIME
/ VIA_DEFAULT_FRAG_TIME
);
1282 DPRINTK ("setting default values %d %d\n", chan
->frag_size
, chan
->frag_number
);
1284 chan
->frag_size
= 1 << (val
& 0xFFFF);
1285 chan
->frag_number
= (val
>> 16) & 0xFFFF;
1287 DPRINTK ("using user values %d %d\n", chan
->frag_size
, chan
->frag_number
);
1290 /* quake3 wants frag_number to be a power of two */
1292 while (chan
->frag_number
) {
1293 chan
->frag_number
>>= 1;
1296 chan
->frag_number
= 1 << shift
;
1298 if (chan
->frag_size
> VIA_MAX_FRAG_SIZE
)
1299 chan
->frag_size
= VIA_MAX_FRAG_SIZE
;
1300 else if (chan
->frag_size
< VIA_MIN_FRAG_SIZE
)
1301 chan
->frag_size
= VIA_MIN_FRAG_SIZE
;
1303 if (chan
->frag_number
< VIA_MIN_FRAG_NUMBER
)
1304 chan
->frag_number
= VIA_MIN_FRAG_NUMBER
;
1305 if (chan
->frag_number
> VIA_MAX_FRAG_NUMBER
)
1306 chan
->frag_number
= VIA_MAX_FRAG_NUMBER
;
1308 if ((chan
->frag_number
* chan
->frag_size
) / PAGE_SIZE
> VIA_MAX_BUFFER_DMA_PAGES
)
1309 chan
->frag_number
= (VIA_MAX_BUFFER_DMA_PAGES
* PAGE_SIZE
) / chan
->frag_size
;
1312 if (chan
->is_record
)
1313 atomic_set (&chan
->n_frags
, 0);
1315 atomic_set (&chan
->n_frags
, chan
->frag_number
);
1322 #ifdef VIA_CHAN_DUMP_BUFS
1324 * via_chan_dump_bufs - Display DMA table contents
1325 * @chan: Channel whose DMA table will be displayed
1327 * Debugging function which displays the contents of the
1328 * scatter-gather DMA table for the given channel @chan.
1331 static void via_chan_dump_bufs (struct via_channel
*chan
)
1335 for (i
= 0; i
< chan
->frag_number
; i
++) {
1336 DPRINTK ("#%02d: addr=%x, count=%u, flag=%d, eol=%d\n",
1337 i
, chan
->sgtable
[i
].addr
,
1338 chan
->sgtable
[i
].count
& 0x00FFFFFF,
1339 chan
->sgtable
[i
].count
& VIA_FLAG
? 1 : 0,
1340 chan
->sgtable
[i
].count
& VIA_EOL
? 1 : 0);
1342 DPRINTK ("buf_in_use = %d, nextbuf = %d\n",
1343 atomic_read (&chan
->buf_in_use
),
1344 atomic_read (&chan
->sw_ptr
));
1346 #endif /* VIA_CHAN_DUMP_BUFS */
1350 * via_chan_flush_frag - Flush partially-full playback buffer to hardware
1351 * @chan: Channel whose DMA table will be flushed
1353 * Flushes partially-full playback buffer to hardware.
1356 static void via_chan_flush_frag (struct via_channel
*chan
)
1358 DPRINTK ("ENTER\n");
1360 assert (chan
->slop_len
> 0);
1362 if (chan
->sw_ptr
== (chan
->frag_number
- 1))
1369 assert (atomic_read (&chan
->n_frags
) > 0);
1370 atomic_dec (&chan
->n_frags
);
1378 * via_chan_maybe_start - Initiate audio hardware DMA operation
1379 * @chan: Channel whose DMA is to be started
1381 * Initiate DMA operation, if the DMA engine for the given
1382 * channel @chan is not already active.
1385 static inline void via_chan_maybe_start (struct via_channel
*chan
)
1387 assert (chan
->is_active
== sg_active(chan
->iobase
));
1389 DPRINTK ("MAYBE START %s\n", chan
->name
);
1390 if (!chan
->is_active
&& chan
->is_enabled
) {
1391 chan
->is_active
= 1;
1393 DPRINTK ("starting channel %s\n", chan
->name
);
1398 /****************************************************************
1400 * Interface to ac97-codec module
1406 * via_ac97_wait_idle - Wait until AC97 codec is not busy
1407 * @card: Private info for specified board
1409 * Sleep until the AC97 codec is no longer busy.
1410 * Returns the final value read from the SGD
1411 * register being polled.
1414 static u8
via_ac97_wait_idle (struct via_info
*card
)
1417 int counter
= VIA_COUNTER_LIMIT
;
1419 DPRINTK ("ENTER/EXIT\n");
1421 assert (card
!= NULL
);
1422 assert (card
->pdev
!= NULL
);
1427 tmp8
= inb (card
->baseaddr
+ 0x83);
1428 } while ((tmp8
& VIA_CR83_BUSY
) && (counter
-- > 0));
1430 if (tmp8
& VIA_CR83_BUSY
)
1431 printk (KERN_WARNING PFX
"timeout waiting on AC97 codec\n");
1437 * via_ac97_read_reg - Read AC97 standard register
1438 * @codec: Pointer to generic AC97 codec info
1439 * @reg: Index of AC97 register to be read
1441 * Read the value of a single AC97 codec register,
1442 * as defined by the Intel AC97 specification.
1444 * Defines the standard AC97 read-register operation
1445 * required by the kernel's ac97_codec interface.
1447 * Returns the 16-bit value stored in the specified
1451 static u16
via_ac97_read_reg (struct ac97_codec
*codec
, u8 reg
)
1454 struct via_info
*card
;
1457 DPRINTK ("ENTER\n");
1459 assert (codec
!= NULL
);
1460 assert (codec
->private_data
!= NULL
);
1462 card
= codec
->private_data
;
1464 spin_lock(&card
->ac97_lock
);
1466 /* Every time we write to register 80 we cause a transaction.
1467 The only safe way to clear the valid bit is to write it at
1468 the same time as the command */
1469 data
= (reg
<< 16) | VIA_CR80_READ
| VIA_CR80_VALID
;
1471 outl (data
, card
->baseaddr
+ VIA_BASE0_AC97_CTRL
);
1474 for (counter
= VIA_COUNTER_LIMIT
; counter
> 0; counter
--) {
1476 if ((((data
= inl(card
->baseaddr
+ VIA_BASE0_AC97_CTRL
)) &
1477 (VIA_CR80_VALID
|VIA_CR80_BUSY
)) == VIA_CR80_VALID
))
1481 printk (KERN_WARNING PFX
"timeout while reading AC97 codec (0x%lX)\n", data
);
1485 /* Once the valid bit has become set, we must wait a complete AC97
1486 frame before the data has settled. */
1488 data
= (unsigned long) inl (card
->baseaddr
+ VIA_BASE0_AC97_CTRL
);
1490 outb (0x02, card
->baseaddr
+ 0x83);
1492 if (((data
& 0x007F0000) >> 16) == reg
) {
1493 DPRINTK ("EXIT, success, data=0x%lx, retval=0x%lx\n",
1494 data
, data
& 0x0000FFFF);
1495 spin_unlock(&card
->ac97_lock
);
1496 return data
& 0x0000FFFF;
1499 printk (KERN_WARNING
"via82cxxx_audio: not our index: reg=0x%x, newreg=0x%lx\n",
1500 reg
, ((data
& 0x007F0000) >> 16));
1503 spin_unlock(&card
->ac97_lock
);
1504 DPRINTK ("EXIT, returning 0\n");
1510 * via_ac97_write_reg - Write AC97 standard register
1511 * @codec: Pointer to generic AC97 codec info
1512 * @reg: Index of AC97 register to be written
1513 * @value: Value to be written to AC97 register
1515 * Write the value of a single AC97 codec register,
1516 * as defined by the Intel AC97 specification.
1518 * Defines the standard AC97 write-register operation
1519 * required by the kernel's ac97_codec interface.
1522 static void via_ac97_write_reg (struct ac97_codec
*codec
, u8 reg
, u16 value
)
1525 struct via_info
*card
;
1528 DPRINTK ("ENTER\n");
1530 assert (codec
!= NULL
);
1531 assert (codec
->private_data
!= NULL
);
1533 card
= codec
->private_data
;
1535 spin_lock(&card
->ac97_lock
);
1537 data
= (reg
<< 16) + value
;
1538 outl (data
, card
->baseaddr
+ VIA_BASE0_AC97_CTRL
);
1541 for (counter
= VIA_COUNTER_LIMIT
; counter
> 0; counter
--) {
1542 if ((inb (card
->baseaddr
+ 0x83) & VIA_CR83_BUSY
) == 0)
1548 printk (KERN_WARNING PFX
"timeout after AC97 codec write (0x%X, 0x%X)\n", reg
, value
);
1551 spin_unlock(&card
->ac97_lock
);
1556 static int via_mixer_open (struct inode
*inode
, struct file
*file
)
1558 int minor
= iminor(inode
);
1559 struct via_info
*card
;
1560 struct pci_dev
*pdev
= NULL
;
1561 struct pci_driver
*drvr
;
1563 DPRINTK ("ENTER\n");
1565 while ((pdev
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pdev
)) != NULL
) {
1566 drvr
= pci_dev_driver (pdev
);
1567 if (drvr
== &via_driver
) {
1568 assert (pci_get_drvdata (pdev
) != NULL
);
1570 card
= pci_get_drvdata (pdev
);
1571 if (card
->ac97
->dev_mixer
== minor
)
1576 DPRINTK ("EXIT, returning -ENODEV\n");
1580 file
->private_data
= card
->ac97
;
1582 DPRINTK ("EXIT, returning 0\n");
1583 return nonseekable_open(inode
, file
);
1586 static int via_mixer_ioctl (struct inode
*inode
, struct file
*file
, unsigned int cmd
,
1589 struct ac97_codec
*codec
= file
->private_data
;
1590 struct via_info
*card
;
1591 int nonblock
= (file
->f_flags
& O_NONBLOCK
);
1594 DPRINTK ("ENTER\n");
1596 assert (codec
!= NULL
);
1597 card
= codec
->private_data
;
1598 assert (card
!= NULL
);
1600 rc
= via_syscall_down (card
, nonblock
);
1605 * Intercept volume control on 8233 and 8235
1611 case SOUND_MIXER_READ_VOLUME
:
1612 return card
->mixer_vol
;
1613 case SOUND_MIXER_WRITE_VOLUME
:
1616 if(get_user(v
, (int *)arg
))
1621 card
->mixer_vol
= v
;
1626 rc
= codec
->mixer_ioctl(codec
, cmd
, arg
);
1628 up (&card
->syscall_sem
);
1631 DPRINTK ("EXIT, returning %d\n", rc
);
1636 static struct file_operations via_mixer_fops
= {
1637 .owner
= THIS_MODULE
,
1638 .open
= via_mixer_open
,
1639 .llseek
= no_llseek
,
1640 .ioctl
= via_mixer_ioctl
,
1644 static int __devinit
via_ac97_reset (struct via_info
*card
)
1646 struct pci_dev
*pdev
= card
->pdev
;
1650 DPRINTK ("ENTER\n");
1652 assert (pdev
!= NULL
);
1656 u8 r40
,r41
,r42
,r43
,r44
,r48
;
1657 pci_read_config_byte (card
->pdev
, 0x40, &r40
);
1658 pci_read_config_byte (card
->pdev
, 0x41, &r41
);
1659 pci_read_config_byte (card
->pdev
, 0x42, &r42
);
1660 pci_read_config_byte (card
->pdev
, 0x43, &r43
);
1661 pci_read_config_byte (card
->pdev
, 0x44, &r44
);
1662 pci_read_config_byte (card
->pdev
, 0x48, &r48
);
1663 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
1664 r40
,r41
,r42
,r43
,r44
,r48
);
1666 spin_lock_irq (&card
->lock
);
1667 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
1668 inb (card
->baseaddr
+ 0x00),
1669 inb (card
->baseaddr
+ 0x01),
1670 inb (card
->baseaddr
+ 0x02),
1671 inl (card
->baseaddr
+ 0x04),
1672 inl (card
->baseaddr
+ 0x0C),
1673 inl (card
->baseaddr
+ 0x80),
1674 inl (card
->baseaddr
+ 0x84));
1675 spin_unlock_irq (&card
->lock
);
1681 * Reset AC97 controller: enable, disable, enable,
1682 * pausing after each command for good luck. Only
1683 * do this if the codec is not ready, because it causes
1684 * loud pops and such due to such a hard codec reset.
1686 pci_read_config_byte (pdev
, VIA_ACLINK_STATUS
, &tmp8
);
1687 if ((tmp8
& VIA_CR40_AC97_READY
) == 0) {
1688 pci_write_config_byte (pdev
, VIA_ACLINK_CTRL
,
1689 VIA_CR41_AC97_ENABLE
|
1690 VIA_CR41_AC97_RESET
|
1691 VIA_CR41_AC97_WAKEUP
);
1694 pci_write_config_byte (pdev
, VIA_ACLINK_CTRL
, 0);
1697 pci_write_config_byte (pdev
, VIA_ACLINK_CTRL
,
1698 VIA_CR41_AC97_ENABLE
|
1699 VIA_CR41_PCM_ENABLE
|
1700 VIA_CR41_VRA
| VIA_CR41_AC97_RESET
);
1704 /* Make sure VRA is enabled, in case we didn't do a
1705 * complete codec reset, above
1707 pci_read_config_byte (pdev
, VIA_ACLINK_CTRL
, &tmp8
);
1708 if (((tmp8
& VIA_CR41_VRA
) == 0) ||
1709 ((tmp8
& VIA_CR41_AC97_ENABLE
) == 0) ||
1710 ((tmp8
& VIA_CR41_PCM_ENABLE
) == 0) ||
1711 ((tmp8
& VIA_CR41_AC97_RESET
) == 0)) {
1712 pci_write_config_byte (pdev
, VIA_ACLINK_CTRL
,
1713 VIA_CR41_AC97_ENABLE
|
1714 VIA_CR41_PCM_ENABLE
|
1715 VIA_CR41_VRA
| VIA_CR41_AC97_RESET
);
1721 #if 0 /* this breaks on K7M */
1722 /* disable legacy stuff */
1723 pci_write_config_byte (pdev
, 0x42, 0x00);
1727 /* route FM trap to IRQ, disable FM trap */
1728 pci_write_config_byte (pdev
, 0x48, 0x05);
1732 /* disable all codec GPI interrupts */
1733 outl (0, pci_resource_start (pdev
, 0) + 0x8C);
1735 /* WARNING: this line is magic. Remove this
1736 * and things break. */
1737 /* enable variable rate */
1738 tmp16
= via_ac97_read_reg (card
->ac97
, AC97_EXTENDED_STATUS
);
1739 if ((tmp16
& 1) == 0)
1740 via_ac97_write_reg (card
->ac97
, AC97_EXTENDED_STATUS
, tmp16
| 1);
1742 DPRINTK ("EXIT, returning 0\n");
1747 static void via_ac97_codec_wait (struct ac97_codec
*codec
)
1749 assert (codec
->private_data
!= NULL
);
1750 via_ac97_wait_idle (codec
->private_data
);
1754 static int __devinit
via_ac97_init (struct via_info
*card
)
1759 DPRINTK ("ENTER\n");
1761 assert (card
!= NULL
);
1763 card
->ac97
= ac97_alloc_codec();
1764 if(card
->ac97
== NULL
)
1767 card
->ac97
->private_data
= card
;
1768 card
->ac97
->codec_read
= via_ac97_read_reg
;
1769 card
->ac97
->codec_write
= via_ac97_write_reg
;
1770 card
->ac97
->codec_wait
= via_ac97_codec_wait
;
1772 card
->ac97
->dev_mixer
= register_sound_mixer (&via_mixer_fops
, -1);
1773 if (card
->ac97
->dev_mixer
< 0) {
1774 printk (KERN_ERR PFX
"unable to register AC97 mixer, aborting\n");
1775 DPRINTK ("EXIT, returning -EIO\n");
1776 ac97_release_codec(card
->ac97
);
1780 rc
= via_ac97_reset (card
);
1782 printk (KERN_ERR PFX
"unable to reset AC97 codec, aborting\n");
1788 if (ac97_probe_codec (card
->ac97
) == 0) {
1789 printk (KERN_ERR PFX
"unable to probe AC97 codec, aborting\n");
1794 /* enable variable rate */
1795 tmp16
= via_ac97_read_reg (card
->ac97
, AC97_EXTENDED_STATUS
);
1796 via_ac97_write_reg (card
->ac97
, AC97_EXTENDED_STATUS
, tmp16
| 1);
1799 * If we cannot enable VRA, we have a locked-rate codec.
1800 * We try again to enable VRA before assuming so, however.
1802 tmp16
= via_ac97_read_reg (card
->ac97
, AC97_EXTENDED_STATUS
);
1803 if ((tmp16
& 1) == 0) {
1804 via_ac97_write_reg (card
->ac97
, AC97_EXTENDED_STATUS
, tmp16
| 1);
1805 tmp16
= via_ac97_read_reg (card
->ac97
, AC97_EXTENDED_STATUS
);
1806 if ((tmp16
& 1) == 0) {
1807 card
->locked_rate
= 1;
1808 printk (KERN_WARNING PFX
"Codec rate locked at 48Khz\n");
1812 DPRINTK ("EXIT, returning 0\n");
1816 unregister_sound_mixer (card
->ac97
->dev_mixer
);
1817 DPRINTK ("EXIT, returning %d\n", rc
);
1818 ac97_release_codec(card
->ac97
);
1823 static void via_ac97_cleanup (struct via_info
*card
)
1825 DPRINTK ("ENTER\n");
1827 assert (card
!= NULL
);
1828 assert (card
->ac97
->dev_mixer
>= 0);
1830 unregister_sound_mixer (card
->ac97
->dev_mixer
);
1831 ac97_release_codec(card
->ac97
);
1838 /****************************************************************
1840 * Interrupt-related code
1845 * 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
= {
2060 .owner
= THIS_MODULE
,
2061 .open
= via_dsp_open
,
2062 .release
= via_dsp_release
,
2063 .read
= via_dsp_read
,
2064 .write
= via_dsp_write
,
2065 .poll
= via_dsp_poll
,
2066 .llseek
= no_llseek
,
2067 .ioctl
= via_dsp_ioctl
,
2068 .mmap
= via_dsp_mmap
,
2072 static int __devinit
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 *type
)
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\n",
2128 address
- vma
->vm_start
,
2129 (address
- vma
->vm_start
) >> PAGE_SHIFT
,
2132 if (address
> vma
->vm_end
) {
2133 DPRINTK ("EXIT, returning NOPAGE_SIGBUS\n");
2134 return NOPAGE_SIGBUS
; /* Disallow mremap */
2137 DPRINTK ("EXIT, returning NOPAGE_OOM\n");
2138 return NOPAGE_OOM
; /* Nothing allocated */
2141 pgoff
= vma
->vm_pgoff
+ ((address
- vma
->vm_start
) >> PAGE_SHIFT
);
2142 rd
= card
->ch_in
.is_mapped
;
2143 wr
= card
->ch_out
.is_mapped
;
2147 unsigned long max_bufs
= chan
->frag_number
;
2148 if (rd
&& wr
) max_bufs
*= 2;
2149 /* via_dsp_mmap() should ensure this */
2150 assert (pgoff
< max_bufs
);
2154 /* if full-duplex (read+write) and we have two sets of bufs,
2155 * then the playback buffers come first, sez soundcard.c */
2156 if (pgoff
>= chan
->page_number
) {
2157 pgoff
-= chan
->page_number
;
2158 chan
= &card
->ch_in
;
2160 chan
= &card
->ch_in
;
2162 assert ((((unsigned long)chan
->pgtbl
[pgoff
].cpuaddr
) % PAGE_SIZE
) == 0);
2164 dmapage
= virt_to_page (chan
->pgtbl
[pgoff
].cpuaddr
);
2165 DPRINTK ("EXIT, returning page %p for cpuaddr %lXh\n",
2166 dmapage
, (unsigned long) chan
->pgtbl
[pgoff
].cpuaddr
);
2169 *type
= VM_FAULT_MINOR
;
2175 static int via_mm_swapout (struct page
*page
, struct file
*filp
)
2179 #endif /* VM_RESERVED */
2182 struct vm_operations_struct via_mm_ops
= {
2183 .nopage
= via_mm_nopage
,
2186 .swapout
= via_mm_swapout
,
2191 static int via_dsp_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2193 struct via_info
*card
;
2194 int nonblock
= (file
->f_flags
& O_NONBLOCK
);
2195 int rc
= -EINVAL
, rd
=0, wr
=0;
2196 unsigned long max_size
, size
, start
, offset
;
2198 assert (file
!= NULL
);
2199 assert (vma
!= NULL
);
2200 card
= file
->private_data
;
2201 assert (card
!= NULL
);
2203 DPRINTK ("ENTER, start %lXh, size %ld, pgoff %ld\n",
2205 vma
->vm_end
- vma
->vm_start
,
2209 if (vma
->vm_flags
& VM_READ
) {
2211 via_chan_set_buffering(card
, &card
->ch_in
, -1);
2212 via_chan_buffer_init (card
, &card
->ch_in
);
2213 max_size
+= card
->ch_in
.page_number
<< PAGE_SHIFT
;
2215 if (vma
->vm_flags
& VM_WRITE
) {
2217 via_chan_set_buffering(card
, &card
->ch_out
, -1);
2218 via_chan_buffer_init (card
, &card
->ch_out
);
2219 max_size
+= card
->ch_out
.page_number
<< PAGE_SHIFT
;
2222 start
= vma
->vm_start
;
2223 offset
= (vma
->vm_pgoff
<< PAGE_SHIFT
);
2224 size
= vma
->vm_end
- vma
->vm_start
;
2226 /* some basic size/offset sanity checks */
2227 if (size
> max_size
)
2229 if (offset
> max_size
- size
)
2232 rc
= via_syscall_down (card
, nonblock
);
2235 vma
->vm_ops
= &via_mm_ops
;
2236 vma
->vm_private_data
= card
;
2239 vma
->vm_flags
|= VM_RESERVED
;
2243 card
->ch_in
.is_mapped
= 1;
2245 card
->ch_out
.is_mapped
= 1;
2247 up (&card
->syscall_sem
);
2251 DPRINTK ("EXIT, returning %d\n", rc
);
2256 static ssize_t
via_dsp_do_read (struct via_info
*card
,
2257 char __user
*userbuf
, size_t count
,
2260 DECLARE_WAITQUEUE(wait
, current
);
2261 const char __user
*orig_userbuf
= userbuf
;
2262 struct via_channel
*chan
= &card
->ch_in
;
2267 /* if SGD has not yet been started, start it */
2268 via_chan_maybe_start (chan
);
2271 /* just to be a nice neighbor */
2273 * But also to ourselves, release semaphore if we do so */
2274 if (need_resched()) {
2275 up(&card
->syscall_sem
);
2277 ret
= via_syscall_down (card
, nonblock
);
2282 /* grab current channel software pointer. In the case of
2283 * recording, this is pointing to the next buffer that
2284 * will receive data from the audio hardware.
2288 /* n_frags represents the number of fragments waiting
2289 * to be copied to userland. sleep until at least
2290 * one buffer has been read from the audio hardware.
2292 add_wait_queue(&chan
->wait
, &wait
);
2294 __set_current_state(TASK_INTERRUPTIBLE
);
2295 tmp
= atomic_read (&chan
->n_frags
);
2297 assert (tmp
<= chan
->frag_number
);
2300 if (nonblock
|| !chan
->is_active
) {
2305 up(&card
->syscall_sem
);
2307 DPRINTK ("Sleeping on block %d\n", n
);
2310 ret
= via_syscall_down (card
, nonblock
);
2314 if (signal_pending (current
)) {
2319 set_current_state(TASK_RUNNING
);
2320 remove_wait_queue(&chan
->wait
, &wait
);
2324 /* Now that we have a buffer we can read from, send
2325 * as much as sample data possible to userspace.
2327 while ((count
> 0) && (chan
->slop_len
< chan
->frag_size
)) {
2328 size_t slop_left
= chan
->frag_size
- chan
->slop_len
;
2329 void *base
= chan
->pgtbl
[n
/ (PAGE_SIZE
/ chan
->frag_size
)].cpuaddr
;
2330 unsigned ofs
= (n
% (PAGE_SIZE
/ chan
->frag_size
)) * chan
->frag_size
;
2332 size
= (count
< slop_left
) ? count
: slop_left
;
2333 if (copy_to_user (userbuf
,
2334 base
+ ofs
+ chan
->slop_len
,
2341 chan
->slop_len
+= size
;
2345 /* If we didn't copy the buffer completely to userspace,
2348 if (chan
->slop_len
< chan
->frag_size
)
2352 * If we get to this point, we copied one buffer completely
2353 * to userspace, give the buffer back to the hardware.
2356 /* advance channel software pointer to point to
2357 * the next buffer from which we will copy
2359 if (chan
->sw_ptr
== (chan
->frag_number
- 1))
2364 /* mark one less buffer waiting to be processed */
2365 assert (atomic_read (&chan
->n_frags
) > 0);
2366 atomic_dec (&chan
->n_frags
);
2368 /* we are at a block boundary, there is no fragment data */
2371 DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
2372 n
, chan
->sw_ptr
, atomic_read (&chan
->n_frags
));
2374 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2375 inb (card
->baseaddr
+ 0x00),
2376 inb (card
->baseaddr
+ 0x01),
2377 inb (card
->baseaddr
+ 0x02),
2378 inl (card
->baseaddr
+ 0x04),
2379 inl (card
->baseaddr
+ 0x0C),
2380 inl (card
->baseaddr
+ 0x80),
2381 inl (card
->baseaddr
+ 0x84));
2384 goto handle_one_block
;
2387 return (userbuf
!= orig_userbuf
) ? (userbuf
- orig_userbuf
) : ret
;
2391 static ssize_t
via_dsp_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
2393 struct via_info
*card
;
2394 int nonblock
= (file
->f_flags
& O_NONBLOCK
);
2397 DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
2398 file
, buffer
, count
, ppos
? ((unsigned long)*ppos
) : 0);
2400 assert (file
!= NULL
);
2401 card
= file
->private_data
;
2402 assert (card
!= NULL
);
2404 rc
= via_syscall_down (card
, nonblock
);
2407 if (card
->ch_in
.is_mapped
) {
2412 via_chan_set_buffering(card
, &card
->ch_in
, -1);
2413 rc
= via_chan_buffer_init (card
, &card
->ch_in
);
2418 rc
= via_dsp_do_read (card
, buffer
, count
, nonblock
);
2421 up (&card
->syscall_sem
);
2423 DPRINTK ("EXIT, returning %ld\n",(long) rc
);
2428 static ssize_t
via_dsp_do_write (struct via_info
*card
,
2429 const char __user
*userbuf
, size_t count
,
2432 DECLARE_WAITQUEUE(wait
, current
);
2433 const char __user
*orig_userbuf
= userbuf
;
2434 struct via_channel
*chan
= &card
->ch_out
;
2435 volatile struct via_sgd_table
*sgtable
= chan
->sgtable
;
2441 /* just to be a nice neighbor */
2443 * But also to ourselves, release semaphore if we do so */
2444 if (need_resched()) {
2445 up(&card
->syscall_sem
);
2447 ret
= via_syscall_down (card
, nonblock
);
2452 /* grab current channel fragment pointer. In the case of
2453 * playback, this is pointing to the next fragment that
2454 * should receive data from userland.
2458 /* n_frags represents the number of fragments remaining
2459 * to be filled by userspace. Sleep until
2460 * at least one fragment is available for our use.
2462 add_wait_queue(&chan
->wait
, &wait
);
2464 __set_current_state(TASK_INTERRUPTIBLE
);
2465 tmp
= atomic_read (&chan
->n_frags
);
2467 assert (tmp
<= chan
->frag_number
);
2470 if (nonblock
|| !chan
->is_active
) {
2475 up(&card
->syscall_sem
);
2477 DPRINTK ("Sleeping on page %d, tmp==%d, ir==%d\n", n
, tmp
, chan
->is_record
);
2480 ret
= via_syscall_down (card
, nonblock
);
2484 if (signal_pending (current
)) {
2489 set_current_state(TASK_RUNNING
);
2490 remove_wait_queue(&chan
->wait
, &wait
);
2494 /* Now that we have at least one fragment we can write to, fill the buffer
2495 * as much as possible with data from userspace.
2497 while ((count
> 0) && (chan
->slop_len
< chan
->frag_size
)) {
2498 size_t slop_left
= chan
->frag_size
- chan
->slop_len
;
2500 size
= (count
< slop_left
) ? count
: slop_left
;
2501 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
,
2508 chan
->slop_len
+= size
;
2512 /* If we didn't fill up the buffer with data, stop now.
2513 * Put a 'stop' marker in the DMA table too, to tell the
2514 * audio hardware to stop if it gets here.
2516 if (chan
->slop_len
< chan
->frag_size
) {
2517 sgtable
[n
].count
= cpu_to_le32 (chan
->slop_len
| VIA_EOL
| VIA_STOP
);
2522 * If we get to this point, we have filled a buffer with
2523 * audio data, flush the buffer to audio hardware.
2526 /* Record the true size for the audio hardware to notice */
2527 if (n
== (chan
->frag_number
- 1))
2528 sgtable
[n
].count
= cpu_to_le32 (chan
->frag_size
| VIA_EOL
);
2530 sgtable
[n
].count
= cpu_to_le32 (chan
->frag_size
| VIA_FLAG
);
2532 /* advance channel software pointer to point to
2533 * the next buffer we will fill with data
2535 if (chan
->sw_ptr
== (chan
->frag_number
- 1))
2540 /* mark one less buffer as being available for userspace consumption */
2541 assert (atomic_read (&chan
->n_frags
) > 0);
2542 atomic_dec (&chan
->n_frags
);
2544 /* we are at a block boundary, there is no fragment data */
2547 /* if SGD has not yet been started, start it */
2548 via_chan_maybe_start (chan
);
2550 DPRINTK ("Flushed block %u, sw_ptr now %u, n_frags now %d\n",
2551 n
, chan
->sw_ptr
, atomic_read (&chan
->n_frags
));
2553 DPRINTK ("regs==S=%02X C=%02X TP=%02X BP=%08X RT=%08X SG=%08X CC=%08X SS=%08X\n",
2554 inb (card
->baseaddr
+ 0x00),
2555 inb (card
->baseaddr
+ 0x01),
2556 inb (card
->baseaddr
+ 0x02),
2557 inl (card
->baseaddr
+ 0x04),
2558 inl (card
->baseaddr
+ 0x08),
2559 inl (card
->baseaddr
+ 0x0C),
2560 inl (card
->baseaddr
+ 0x80),
2561 inl (card
->baseaddr
+ 0x84));
2564 goto handle_one_block
;
2567 if (userbuf
- orig_userbuf
)
2568 return userbuf
- orig_userbuf
;
2574 static ssize_t
via_dsp_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
2576 struct via_info
*card
;
2578 int nonblock
= (file
->f_flags
& O_NONBLOCK
);
2580 DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
2581 file
, buffer
, count
, ppos
? ((unsigned long)*ppos
) : 0);
2583 assert (file
!= NULL
);
2584 card
= file
->private_data
;
2585 assert (card
!= NULL
);
2587 rc
= via_syscall_down (card
, nonblock
);
2590 if (card
->ch_out
.is_mapped
) {
2595 via_chan_set_buffering(card
, &card
->ch_out
, -1);
2596 rc
= via_chan_buffer_init (card
, &card
->ch_out
);
2601 rc
= via_dsp_do_write (card
, buffer
, count
, nonblock
);
2604 up (&card
->syscall_sem
);
2606 DPRINTK ("EXIT, returning %ld\n",(long) rc
);
2611 static unsigned int via_dsp_poll(struct file
*file
, struct poll_table_struct
*wait
)
2613 struct via_info
*card
;
2614 struct via_channel
*chan
;
2615 unsigned int mask
= 0;
2617 DPRINTK ("ENTER\n");
2619 assert (file
!= NULL
);
2620 card
= file
->private_data
;
2621 assert (card
!= NULL
);
2623 if (file
->f_mode
& FMODE_READ
) {
2624 chan
= &card
->ch_in
;
2625 if (sg_active (chan
->iobase
))
2626 poll_wait(file
, &chan
->wait
, wait
);
2627 if (atomic_read (&chan
->n_frags
) > 0)
2628 mask
|= POLLIN
| POLLRDNORM
;
2631 if (file
->f_mode
& FMODE_WRITE
) {
2632 chan
= &card
->ch_out
;
2633 if (sg_active (chan
->iobase
))
2634 poll_wait(file
, &chan
->wait
, wait
);
2635 if (atomic_read (&chan
->n_frags
) > 0)
2636 mask
|= POLLOUT
| POLLWRNORM
;
2639 DPRINTK ("EXIT, returning %u\n", mask
);
2645 * via_dsp_drain_playback - sleep until all playback samples are flushed
2646 * @card: Private info for specified board
2647 * @chan: Channel to drain
2648 * @nonblock: boolean, non-zero if O_NONBLOCK is set
2650 * Sleeps until all playback has been flushed to the audio
2653 * Locking: inside card->syscall_sem
2656 static int via_dsp_drain_playback (struct via_info
*card
,
2657 struct via_channel
*chan
, int nonblock
)
2659 DECLARE_WAITQUEUE(wait
, current
);
2662 DPRINTK ("ENTER, nonblock = %d\n", nonblock
);
2664 if (chan
->slop_len
> 0)
2665 via_chan_flush_frag (chan
);
2667 if (atomic_read (&chan
->n_frags
) == chan
->frag_number
)
2670 via_chan_maybe_start (chan
);
2672 add_wait_queue(&chan
->wait
, &wait
);
2674 DPRINTK ("FRAGS %d FRAGNUM %d\n", atomic_read(&chan
->n_frags
), chan
->frag_number
);
2675 __set_current_state(TASK_INTERRUPTIBLE
);
2676 if (atomic_read (&chan
->n_frags
) >= chan
->frag_number
)
2680 DPRINTK ("EXIT, returning -EAGAIN\n");
2687 u8 r40
,r41
,r42
,r43
,r44
,r48
;
2688 pci_read_config_byte (card
->pdev
, 0x40, &r40
);
2689 pci_read_config_byte (card
->pdev
, 0x41, &r41
);
2690 pci_read_config_byte (card
->pdev
, 0x42, &r42
);
2691 pci_read_config_byte (card
->pdev
, 0x43, &r43
);
2692 pci_read_config_byte (card
->pdev
, 0x44, &r44
);
2693 pci_read_config_byte (card
->pdev
, 0x48, &r48
);
2694 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
2695 r40
,r41
,r42
,r43
,r44
,r48
);
2697 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2698 inb (card
->baseaddr
+ 0x00),
2699 inb (card
->baseaddr
+ 0x01),
2700 inb (card
->baseaddr
+ 0x02),
2701 inl (card
->baseaddr
+ 0x04),
2702 inl (card
->baseaddr
+ 0x0C),
2703 inl (card
->baseaddr
+ 0x80),
2704 inl (card
->baseaddr
+ 0x84));
2707 if (!chan
->is_active
)
2708 printk (KERN_ERR
"sleeping but not active\n");
2711 up(&card
->syscall_sem
);
2713 DPRINTK ("sleeping, nbufs=%d\n", atomic_read (&chan
->n_frags
));
2716 if ((ret
= via_syscall_down (card
, nonblock
)))
2719 if (signal_pending (current
)) {
2720 DPRINTK ("EXIT, returning -ERESTARTSYS\n");
2725 set_current_state(TASK_RUNNING
);
2726 remove_wait_queue(&chan
->wait
, &wait
);
2730 u8 r40
,r41
,r42
,r43
,r44
,r48
;
2731 pci_read_config_byte (card
->pdev
, 0x40, &r40
);
2732 pci_read_config_byte (card
->pdev
, 0x41, &r41
);
2733 pci_read_config_byte (card
->pdev
, 0x42, &r42
);
2734 pci_read_config_byte (card
->pdev
, 0x43, &r43
);
2735 pci_read_config_byte (card
->pdev
, 0x44, &r44
);
2736 pci_read_config_byte (card
->pdev
, 0x48, &r48
);
2737 DPRINTK ("PCI config: %02X %02X %02X %02X %02X %02X\n",
2738 r40
,r41
,r42
,r43
,r44
,r48
);
2740 DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
2741 inb (card
->baseaddr
+ 0x00),
2742 inb (card
->baseaddr
+ 0x01),
2743 inb (card
->baseaddr
+ 0x02),
2744 inl (card
->baseaddr
+ 0x04),
2745 inl (card
->baseaddr
+ 0x0C),
2746 inl (card
->baseaddr
+ 0x80),
2747 inl (card
->baseaddr
+ 0x84));
2749 DPRINTK ("final nbufs=%d\n", atomic_read (&chan
->n_frags
));
2754 DPRINTK ("EXIT, returning %d\n", ret
);
2760 * via_dsp_ioctl_space - get information about channel buffering
2761 * @card: Private info for specified board
2762 * @chan: pointer to channel-specific info
2763 * @arg: user buffer for returned information
2765 * Handles SNDCTL_DSP_GETISPACE and SNDCTL_DSP_GETOSPACE.
2767 * Locking: inside card->syscall_sem
2770 static int via_dsp_ioctl_space (struct via_info
*card
,
2771 struct via_channel
*chan
,
2774 audio_buf_info info
;
2776 via_chan_set_buffering(card
, chan
, -1);
2778 info
.fragstotal
= chan
->frag_number
;
2779 info
.fragsize
= chan
->frag_size
;
2781 /* number of full fragments we can read/write without blocking */
2782 info
.fragments
= atomic_read (&chan
->n_frags
);
2784 if ((chan
->slop_len
% chan
->frag_size
> 0) && (info
.fragments
> 0))
2787 /* number of bytes that can be read or written immediately
2790 info
.bytes
= (info
.fragments
* chan
->frag_size
);
2791 if (chan
->slop_len
% chan
->frag_size
> 0)
2792 info
.bytes
+= chan
->frag_size
- (chan
->slop_len
% chan
->frag_size
);
2794 DPRINTK ("EXIT, returning fragstotal=%d, fragsize=%d, fragments=%d, bytes=%d\n",
2800 return copy_to_user (arg
, &info
, sizeof (info
))?-EFAULT
:0;
2805 * via_dsp_ioctl_ptr - get information about hardware buffer ptr
2806 * @card: Private info for specified board
2807 * @chan: pointer to channel-specific info
2808 * @arg: user buffer for returned information
2810 * Handles SNDCTL_DSP_GETIPTR and SNDCTL_DSP_GETOPTR.
2812 * Locking: inside card->syscall_sem
2815 static int via_dsp_ioctl_ptr (struct via_info
*card
,
2816 struct via_channel
*chan
,
2821 spin_lock_irq (&card
->lock
);
2823 info
.bytes
= chan
->bytes
;
2824 info
.blocks
= chan
->n_irqs
;
2827 spin_unlock_irq (&card
->lock
);
2829 if (chan
->is_active
) {
2830 unsigned long extra
;
2831 info
.ptr
= atomic_read (&chan
->hw_ptr
) * chan
->frag_size
;
2832 extra
= chan
->frag_size
- via_sg_offset(chan
);
2834 info
.bytes
+= extra
;
2839 DPRINTK ("EXIT, returning bytes=%d, blocks=%d, ptr=%d\n",
2844 return copy_to_user (arg
, &info
, sizeof (info
))?-EFAULT
:0;
2848 static int via_dsp_ioctl_trigger (struct via_channel
*chan
, int val
)
2850 int enable
, do_something
;
2852 if (chan
->is_record
)
2853 enable
= (val
& PCM_ENABLE_INPUT
);
2855 enable
= (val
& PCM_ENABLE_OUTPUT
);
2857 if (!chan
->is_enabled
&& enable
) {
2859 } else if (chan
->is_enabled
&& !enable
) {
2865 DPRINTK ("enable=%d, do_something=%d\n",
2866 enable
, do_something
);
2868 if (chan
->is_active
&& do_something
)
2871 if (do_something
== 1) {
2872 chan
->is_enabled
= 1;
2873 via_chan_maybe_start (chan
);
2874 DPRINTK ("Triggering input\n");
2877 else if (do_something
== -1) {
2878 chan
->is_enabled
= 0;
2879 DPRINTK ("Setup input trigger\n");
2886 static int via_dsp_ioctl (struct inode
*inode
, struct file
*file
,
2887 unsigned int cmd
, unsigned long arg
)
2889 int rc
, rd
=0, wr
=0, val
=0;
2890 struct via_info
*card
;
2891 struct via_channel
*chan
;
2892 int nonblock
= (file
->f_flags
& O_NONBLOCK
);
2893 int __user
*ip
= (int __user
*)arg
;
2894 void __user
*p
= (void __user
*)arg
;
2896 assert (file
!= NULL
);
2897 card
= file
->private_data
;
2898 assert (card
!= NULL
);
2900 if (file
->f_mode
& FMODE_WRITE
)
2902 if (file
->f_mode
& FMODE_READ
)
2905 rc
= via_syscall_down (card
, nonblock
);
2912 /* OSS API version. XXX unverified */
2913 case OSS_GETVERSION
:
2914 DPRINTK ("ioctl OSS_GETVERSION, EXIT, returning SOUND_VERSION\n");
2915 rc
= put_user (SOUND_VERSION
, ip
);
2918 /* list of supported PCM data formats */
2919 case SNDCTL_DSP_GETFMTS
:
2920 DPRINTK ("DSP_GETFMTS, EXIT, returning AFMT U8|S16_LE\n");
2921 rc
= put_user (AFMT_U8
| AFMT_S16_LE
, ip
);
2924 /* query or set current channel's PCM data format */
2925 case SNDCTL_DSP_SETFMT
:
2926 if (get_user(val
, ip
)) {
2930 DPRINTK ("DSP_SETFMT, val==%d\n", val
);
2931 if (val
!= AFMT_QUERY
) {
2935 rc
= via_chan_set_fmt (card
, &card
->ch_in
, val
);
2938 rc
= via_chan_set_fmt (card
, &card
->ch_out
, val
);
2945 if ((rd
&& (card
->ch_in
.pcm_fmt
& VIA_PCM_FMT_16BIT
)) ||
2946 (wr
&& (card
->ch_out
.pcm_fmt
& VIA_PCM_FMT_16BIT
)))
2951 DPRINTK ("SETFMT EXIT, returning %d\n", val
);
2952 rc
= put_user (val
, ip
);
2955 /* query or set number of channels (1=mono, 2=stereo, 4/6 for multichannel) */
2956 case SNDCTL_DSP_CHANNELS
:
2957 if (get_user(val
, ip
)) {
2961 DPRINTK ("DSP_CHANNELS, val==%d\n", val
);
2966 rc
= via_chan_set_stereo (card
, &card
->ch_in
, val
);
2969 rc
= via_chan_set_stereo (card
, &card
->ch_out
, val
);
2977 val
= card
->ch_in
.channels
;
2979 val
= card
->ch_out
.channels
;
2981 DPRINTK ("CHANNELS EXIT, returning %d\n", val
);
2982 rc
= put_user (val
, ip
);
2985 /* enable (val is not zero) or disable (val == 0) stereo */
2986 case SNDCTL_DSP_STEREO
:
2987 if (get_user(val
, ip
)) {
2991 DPRINTK ("DSP_STEREO, val==%d\n", val
);
2995 rc
= via_chan_set_stereo (card
, &card
->ch_in
, val
? 2 : 1);
2997 rc
= via_chan_set_stereo (card
, &card
->ch_out
, val
? 2 : 1);
3004 DPRINTK ("STEREO EXIT, returning %d\n", val
);
3005 rc
= put_user(val
, ip
);
3008 /* query or set sampling rate */
3009 case SNDCTL_DSP_SPEED
:
3010 if (get_user(val
, ip
)) {
3014 DPRINTK ("DSP_SPEED, val==%d\n", val
);
3023 rc
= via_chan_set_speed (card
, &card
->ch_in
, val
);
3025 rc
= via_chan_set_speed (card
, &card
->ch_out
, val
);
3033 val
= card
->ch_in
.rate
;
3035 val
= card
->ch_out
.rate
;
3039 DPRINTK ("SPEED EXIT, returning %d\n", val
);
3040 rc
= put_user (val
, ip
);
3043 /* wait until all buffers have been played, and then stop device */
3044 case SNDCTL_DSP_SYNC
:
3045 DPRINTK ("DSP_SYNC\n");
3048 DPRINTK ("SYNC EXIT (after calling via_dsp_drain_playback)\n");
3049 rc
= via_dsp_drain_playback (card
, &card
->ch_out
, nonblock
);
3053 /* stop recording/playback immediately */
3054 case SNDCTL_DSP_RESET
:
3055 DPRINTK ("DSP_RESET\n");
3057 via_chan_clear (card
, &card
->ch_in
);
3058 card
->ch_in
.frag_number
= 0;
3059 card
->ch_in
.frag_size
= 0;
3060 atomic_set(&card
->ch_in
.n_frags
, 0);
3064 via_chan_clear (card
, &card
->ch_out
);
3065 card
->ch_out
.frag_number
= 0;
3066 card
->ch_out
.frag_size
= 0;
3067 atomic_set(&card
->ch_out
.n_frags
, 0);
3073 case SNDCTL_DSP_NONBLOCK
:
3074 file
->f_flags
|= O_NONBLOCK
;
3078 /* obtain bitmask of device capabilities, such as mmap, full duplex, etc. */
3079 case SNDCTL_DSP_GETCAPS
:
3080 DPRINTK ("DSP_GETCAPS\n");
3081 rc
= put_user(VIA_DSP_CAP
, ip
);
3084 /* obtain buffer fragment size */
3085 case SNDCTL_DSP_GETBLKSIZE
:
3086 DPRINTK ("DSP_GETBLKSIZE\n");
3089 via_chan_set_buffering(card
, &card
->ch_in
, -1);
3090 rc
= put_user(card
->ch_in
.frag_size
, ip
);
3092 via_chan_set_buffering(card
, &card
->ch_out
, -1);
3093 rc
= put_user(card
->ch_out
.frag_size
, ip
);
3097 /* obtain information about input buffering */
3098 case SNDCTL_DSP_GETISPACE
:
3099 DPRINTK ("DSP_GETISPACE\n");
3101 rc
= via_dsp_ioctl_space (card
, &card
->ch_in
, p
);
3104 /* obtain information about output buffering */
3105 case SNDCTL_DSP_GETOSPACE
:
3106 DPRINTK ("DSP_GETOSPACE\n");
3108 rc
= via_dsp_ioctl_space (card
, &card
->ch_out
, p
);
3111 /* obtain information about input hardware pointer */
3112 case SNDCTL_DSP_GETIPTR
:
3113 DPRINTK ("DSP_GETIPTR\n");
3115 rc
= via_dsp_ioctl_ptr (card
, &card
->ch_in
, p
);
3118 /* obtain information about output hardware pointer */
3119 case SNDCTL_DSP_GETOPTR
:
3120 DPRINTK ("DSP_GETOPTR\n");
3122 rc
= via_dsp_ioctl_ptr (card
, &card
->ch_out
, p
);
3125 /* return number of bytes remaining to be played by DMA engine */
3126 case SNDCTL_DSP_GETODELAY
:
3128 DPRINTK ("DSP_GETODELAY\n");
3130 chan
= &card
->ch_out
;
3135 if (chan
->is_active
) {
3137 val
= chan
->frag_number
- atomic_read (&chan
->n_frags
);
3142 val
*= chan
->frag_size
;
3143 val
-= chan
->frag_size
- via_sg_offset(chan
);
3145 val
+= chan
->slop_len
% chan
->frag_size
;
3149 assert (val
<= (chan
->frag_size
* chan
->frag_number
));
3151 DPRINTK ("GETODELAY EXIT, val = %d bytes\n", val
);
3152 rc
= put_user (val
, ip
);
3156 /* handle the quick-start of a channel,
3157 * or the notification that a quick-start will
3158 * occur in the future
3160 case SNDCTL_DSP_SETTRIGGER
:
3161 if (get_user(val
, ip
)) {
3165 DPRINTK ("DSP_SETTRIGGER, rd=%d, wr=%d, act=%d/%d, en=%d/%d\n",
3166 rd
, wr
, card
->ch_in
.is_active
, card
->ch_out
.is_active
,
3167 card
->ch_in
.is_enabled
, card
->ch_out
.is_enabled
);
3172 rc
= via_dsp_ioctl_trigger (&card
->ch_in
, val
);
3175 rc
= via_dsp_ioctl_trigger (&card
->ch_out
, val
);
3179 case SNDCTL_DSP_GETTRIGGER
:
3181 if ((file
->f_mode
& FMODE_READ
) && card
->ch_in
.is_enabled
)
3182 val
|= PCM_ENABLE_INPUT
;
3183 if ((file
->f_mode
& FMODE_WRITE
) && card
->ch_out
.is_enabled
)
3184 val
|= PCM_ENABLE_OUTPUT
;
3185 rc
= put_user(val
, ip
);
3188 /* Enable full duplex. Since we do this as soon as we are opened
3189 * with O_RDWR, this is mainly a no-op that always returns success.
3191 case SNDCTL_DSP_SETDUPLEX
:
3192 DPRINTK ("DSP_SETDUPLEX\n");
3198 /* set fragment size. implemented as a successful no-op for now */
3199 case SNDCTL_DSP_SETFRAGMENT
:
3200 if (get_user(val
, ip
)) {
3204 DPRINTK ("DSP_SETFRAGMENT, val==%d\n", val
);
3207 rc
= via_chan_set_buffering(card
, &card
->ch_in
, val
);
3210 rc
= via_chan_set_buffering(card
, &card
->ch_out
, val
);
3212 DPRINTK ("SNDCTL_DSP_SETFRAGMENT (fragshift==0x%04X (%d), maxfrags==0x%04X (%d))\n",
3215 (val
>> 16) & 0xFFFF,
3216 (val
>> 16) & 0xFFFF);
3221 /* inform device of an upcoming pause in input (or output). */
3222 case SNDCTL_DSP_POST
:
3223 DPRINTK ("DSP_POST\n");
3225 if (card
->ch_out
.slop_len
> 0)
3226 via_chan_flush_frag (&card
->ch_out
);
3227 via_chan_maybe_start (&card
->ch_out
);
3233 /* not implemented */
3235 DPRINTK ("unhandled ioctl, cmd==%u, arg==%p\n",
3240 up (&card
->syscall_sem
);
3241 DPRINTK ("EXIT, returning %d\n", rc
);
3246 static int via_dsp_open (struct inode
*inode
, struct file
*file
)
3248 int minor
= iminor(inode
);
3249 struct via_info
*card
;
3250 struct pci_dev
*pdev
= NULL
;
3251 struct via_channel
*chan
;
3252 struct pci_driver
*drvr
;
3253 int nonblock
= (file
->f_flags
& O_NONBLOCK
);
3255 DPRINTK ("ENTER, minor=%d, file->f_mode=0x%x\n", minor
, file
->f_mode
);
3257 if (!(file
->f_mode
& (FMODE_READ
| FMODE_WRITE
))) {
3258 DPRINTK ("EXIT, returning -EINVAL\n");
3263 while ((pdev
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pdev
)) != NULL
) {
3264 drvr
= pci_dev_driver (pdev
);
3265 if (drvr
== &via_driver
) {
3266 assert (pci_get_drvdata (pdev
) != NULL
);
3268 card
= pci_get_drvdata (pdev
);
3269 DPRINTK ("dev_dsp = %d, minor = %d, assn = %d\n",
3270 card
->dev_dsp
, minor
,
3271 (card
->dev_dsp
^ minor
) & ~0xf);
3273 if (((card
->dev_dsp
^ minor
) & ~0xf) == 0)
3278 DPRINTK ("no matching %s found\n", card
? "minor" : "driver");
3283 if (down_trylock (&card
->open_sem
)) {
3284 DPRINTK ("EXIT, returning -EAGAIN\n");
3288 if (down_interruptible (&card
->open_sem
)) {
3289 DPRINTK ("EXIT, returning -ERESTARTSYS\n");
3290 return -ERESTARTSYS
;
3294 file
->private_data
= card
;
3295 DPRINTK ("file->f_mode == 0x%x\n", file
->f_mode
);
3297 /* handle input from analog source */
3298 if (file
->f_mode
& FMODE_READ
) {
3299 chan
= &card
->ch_in
;
3301 via_chan_init (card
, chan
);
3303 /* why is this forced to 16-bit stereo in all drivers? */
3304 chan
->pcm_fmt
= VIA_PCM_FMT_16BIT
| VIA_PCM_FMT_STEREO
;
3307 // TO DO - use FIFO: via_capture_fifo(card, 1);
3308 via_chan_pcm_fmt (chan
, 0);
3309 via_set_rate (card
->ac97
, chan
, 44100);
3312 /* handle output to analog source */
3313 if (file
->f_mode
& FMODE_WRITE
) {
3314 chan
= &card
->ch_out
;
3316 via_chan_init (card
, chan
);
3318 if (file
->f_mode
& FMODE_READ
) {
3319 /* if in duplex mode make the recording and playback channels
3320 have the same settings */
3321 chan
->pcm_fmt
= VIA_PCM_FMT_16BIT
| VIA_PCM_FMT_STEREO
;
3323 via_chan_pcm_fmt (chan
, 0);
3324 via_set_rate (card
->ac97
, chan
, 44100);
3326 if ((minor
& 0xf) == SND_DEV_DSP16
) {
3327 chan
->pcm_fmt
= VIA_PCM_FMT_16BIT
;
3328 via_chan_pcm_fmt (chan
, 0);
3329 via_set_rate (card
->ac97
, chan
, 44100);
3331 via_chan_pcm_fmt (chan
, 1);
3332 via_set_rate (card
->ac97
, chan
, 8000);
3337 DPRINTK ("EXIT, returning 0\n");
3338 return nonseekable_open(inode
, file
);
3342 static int via_dsp_release(struct inode
*inode
, struct file
*file
)
3344 struct via_info
*card
;
3345 int nonblock
= (file
->f_flags
& O_NONBLOCK
);
3348 DPRINTK ("ENTER\n");
3350 assert (file
!= NULL
);
3351 card
= file
->private_data
;
3352 assert (card
!= NULL
);
3354 rc
= via_syscall_down (card
, nonblock
);
3356 DPRINTK ("EXIT (syscall_down error), rc=%d\n", rc
);
3360 if (file
->f_mode
& FMODE_WRITE
) {
3361 rc
= via_dsp_drain_playback (card
, &card
->ch_out
, nonblock
);
3362 if (rc
&& rc
!= -ERESTARTSYS
) /* Nobody needs to know about ^C */
3363 printk (KERN_DEBUG
"via_audio: ignoring drain playback error %d\n", rc
);
3365 via_chan_free (card
, &card
->ch_out
);
3366 via_chan_buffer_free(card
, &card
->ch_out
);
3369 if (file
->f_mode
& FMODE_READ
) {
3370 via_chan_free (card
, &card
->ch_in
);
3371 via_chan_buffer_free (card
, &card
->ch_in
);
3374 up (&card
->syscall_sem
);
3375 up (&card
->open_sem
);
3377 DPRINTK ("EXIT, returning 0\n");
3382 /****************************************************************
3384 * Chip setup and kernel registration
3389 static int __devinit
via_init_one (struct pci_dev
*pdev
, const struct pci_device_id
*id
)
3391 #ifdef CONFIG_MIDI_VIA82CXXX
3395 struct via_info
*card
;
3396 static int printed_version
;
3398 DPRINTK ("ENTER\n");
3400 if (printed_version
++ == 0)
3401 printk (KERN_INFO
"Via 686a/8233/8235 audio driver " VIA_VERSION
"\n");
3403 rc
= pci_enable_device (pdev
);
3407 rc
= pci_request_regions (pdev
, "via82cxxx_audio");
3409 goto err_out_disable
;
3411 rc
= pci_set_dma_mask(pdev
, 0xffffffffULL
);
3414 rc
= pci_set_consistent_dma_mask(pdev
, 0xffffffffULL
);
3418 card
= kmalloc (sizeof (*card
), GFP_KERNEL
);
3420 printk (KERN_ERR PFX
"out of memory, aborting\n");
3425 pci_set_drvdata (pdev
, card
);
3427 memset (card
, 0, sizeof (*card
));
3429 card
->baseaddr
= pci_resource_start (pdev
, 0);
3430 card
->card_num
= via_num_cards
++;
3431 spin_lock_init (&card
->lock
);
3432 spin_lock_init (&card
->ac97_lock
);
3433 init_MUTEX (&card
->syscall_sem
);
3434 init_MUTEX (&card
->open_sem
);
3436 /* we must init these now, in case the intr handler needs them */
3437 via_chan_init_defaults (card
, &card
->ch_out
);
3438 via_chan_init_defaults (card
, &card
->ch_in
);
3439 via_chan_init_defaults (card
, &card
->ch_fm
);
3441 /* if BAR 2 is present, chip is Rev H or later,
3442 * which means it has a few extra features */
3443 if (pci_resource_start (pdev
, 2) > 0)
3446 /* Overkill for now, but more flexible done right */
3448 card
->intmask
= id
->driver_data
;
3449 card
->legacy
= !card
->intmask
;
3450 card
->sixchannel
= id
->driver_data
;
3452 if(card
->sixchannel
)
3453 printk(KERN_INFO PFX
"Six channel audio available\n");
3454 if (pdev
->irq
< 1) {
3455 printk (KERN_ERR PFX
"invalid PCI IRQ %d, aborting\n", pdev
->irq
);
3460 if (!(pci_resource_flags (pdev
, 0) & IORESOURCE_IO
)) {
3461 printk (KERN_ERR PFX
"unable to locate I/O resources, aborting\n");
3466 pci_set_master(pdev
);
3469 * init AC97 mixer and codec
3471 rc
= via_ac97_init (card
);
3473 printk (KERN_ERR PFX
"AC97 init failed, aborting\n");
3480 rc
= via_dsp_init (card
);
3482 printk (KERN_ERR PFX
"DSP device init failed, aborting\n");
3483 goto err_out_have_mixer
;
3487 * per-card /proc info
3489 rc
= via_card_init_proc (card
);
3491 printk (KERN_ERR PFX
"card-specific /proc init failed, aborting\n");
3492 goto err_out_have_dsp
;
3496 * init and turn on interrupts, as the last thing we do
3498 rc
= via_interrupt_init (card
);
3500 printk (KERN_ERR PFX
"interrupt init failed, aborting\n");
3501 goto err_out_have_proc
;
3504 printk (KERN_INFO PFX
"board #%d at 0x%04lX, IRQ %d\n",
3505 card
->card_num
+ 1, card
->baseaddr
, pdev
->irq
);
3507 #ifdef CONFIG_MIDI_VIA82CXXX
3508 /* Disable by default */
3509 card
->midi_info
.io_base
= 0;
3513 pci_read_config_byte (pdev
, 0x42, &r42
);
3514 /* Disable MIDI interrupt */
3515 pci_write_config_byte (pdev
, 0x42, r42
| VIA_CR42_MIDI_IRQMASK
);
3516 if (r42
& VIA_CR42_MIDI_ENABLE
)
3518 if (r42
& VIA_CR42_MIDI_PNP
) /* Address selected by iobase 2 - not tested */
3519 card
->midi_info
.io_base
= pci_resource_start (pdev
, 2);
3520 else /* Address selected by byte 0x43 */
3523 pci_read_config_byte (pdev
, 0x43, &r43
);
3524 card
->midi_info
.io_base
= 0x300 + ((r43
& 0x0c) << 2);
3527 card
->midi_info
.irq
= -pdev
->irq
;
3528 if (probe_uart401(& card
->midi_info
, THIS_MODULE
))
3530 card
->midi_devc
=midi_devs
[card
->midi_info
.slots
[4]]->devc
;
3531 pci_write_config_byte(pdev
, 0x42, r42
& ~VIA_CR42_MIDI_IRQMASK
);
3532 printk("Enabled Via MIDI\n");
3538 DPRINTK ("EXIT, returning 0\n");
3542 via_card_cleanup_proc (card
);
3545 via_dsp_cleanup (card
);
3548 via_ac97_cleanup (card
);
3552 memset (card
, 0xAB, sizeof (*card
)); /* poison memory */
3557 pci_release_regions (pdev
);
3560 pci_disable_device (pdev
);
3563 pci_set_drvdata (pdev
, NULL
);
3564 DPRINTK ("EXIT - returning %d\n", rc
);
3569 static void __devexit
via_remove_one (struct pci_dev
*pdev
)
3571 struct via_info
*card
;
3573 DPRINTK ("ENTER\n");
3575 assert (pdev
!= NULL
);
3576 card
= pci_get_drvdata (pdev
);
3577 assert (card
!= NULL
);
3579 #ifdef CONFIG_MIDI_VIA82CXXX
3580 if (card
->midi_info
.io_base
)
3581 unload_uart401(&card
->midi_info
);
3584 free_irq (card
->pdev
->irq
, card
);
3585 via_card_cleanup_proc (card
);
3586 via_dsp_cleanup (card
);
3587 via_ac97_cleanup (card
);
3590 memset (card
, 0xAB, sizeof (*card
)); /* poison memory */
3594 pci_set_drvdata (pdev
, NULL
);
3596 pci_release_regions (pdev
);
3597 pci_disable_device (pdev
);
3598 pci_set_power_state (pdev
, 3); /* ...zzzzzz */
3605 /****************************************************************
3607 * Driver initialization and cleanup
3612 static int __init
init_via82cxxx_audio(void)
3616 DPRINTK ("ENTER\n");
3618 rc
= via_init_proc ();
3620 DPRINTK ("EXIT, returning %d\n", rc
);
3624 rc
= pci_register_driver (&via_driver
);
3627 pci_unregister_driver (&via_driver
);
3628 via_cleanup_proc ();
3629 DPRINTK ("EXIT, returning -ENODEV\n");
3633 DPRINTK ("EXIT, returning 0\n");
3638 static void __exit
cleanup_via82cxxx_audio(void)
3640 DPRINTK ("ENTER\n");
3642 pci_unregister_driver (&via_driver
);
3643 via_cleanup_proc ();
3649 module_init(init_via82cxxx_audio
);
3650 module_exit(cleanup_via82cxxx_audio
);
3652 MODULE_AUTHOR("Jeff Garzik");
3653 MODULE_DESCRIPTION("DSP audio and mixer driver for Via 82Cxxx audio devices");
3654 MODULE_LICENSE("GPL");
3659 /****************************************************************
3661 * /proc/driver/via/info
3666 static int via_info_read_proc (char *page
, char **start
, off_t off
,
3667 int count
, int *eof
, void *data
)
3669 #define YN(val,bit) (((val) & (bit)) ? "yes" : "no")
3670 #define ED(val,bit) (((val) & (bit)) ? "enable" : "disable")
3673 u8 r40
, r41
, r42
, r44
;
3674 struct via_info
*card
= data
;
3676 DPRINTK ("ENTER\n");
3678 assert (card
!= NULL
);
3680 len
+= sprintf (page
+len
, VIA_CARD_NAME
"\n\n");
3682 pci_read_config_byte (card
->pdev
, 0x40, &r40
);
3683 pci_read_config_byte (card
->pdev
, 0x41, &r41
);
3684 pci_read_config_byte (card
->pdev
, 0x42, &r42
);
3685 pci_read_config_byte (card
->pdev
, 0x44, &r44
);
3687 len
+= sprintf (page
+len
,
3688 "Via 82Cxxx PCI registers:\n"
3690 "40 Codec Ready: %s\n"
3691 " Codec Low-power: %s\n"
3692 " Secondary Codec Ready: %s\n"
3694 "41 Interface Enable: %s\n"
3695 " De-Assert Reset: %s\n"
3696 " Force SYNC high: %s\n"
3697 " Force SDO high: %s\n"
3698 " Variable Sample Rate On-Demand Mode: %s\n"
3699 " SGD Read Channel PCM Data Out: %s\n"
3700 " FM Channel PCM Data Out: %s\n"
3701 " SB PCM Data Out: %s\n"
3703 "42 Game port enabled: %s\n"
3704 " SoundBlaster enabled: %s\n"
3706 " MIDI enabled: %s\n"
3708 "44 AC-Link Interface Access: %s\n"
3709 " Secondary Codec Support: %s\n"
3713 YN (r40
, VIA_CR40_AC97_READY
),
3714 YN (r40
, VIA_CR40_AC97_LOW_POWER
),
3715 YN (r40
, VIA_CR40_SECONDARY_READY
),
3717 ED (r41
, VIA_CR41_AC97_ENABLE
),
3726 YN (r42
, VIA_CR42_GAME_ENABLE
),
3727 YN (r42
, VIA_CR42_SB_ENABLE
),
3728 YN (r42
, VIA_CR42_FM_ENABLE
),
3729 YN (r42
, VIA_CR42_MIDI_ENABLE
),
3731 YN (r44
, VIA_CR44_AC_LINK_ACCESS
),
3732 YN (r44
, VIA_CR44_SECOND_CODEC_SUPPORT
)
3736 DPRINTK ("EXIT, returning %d\n", len
);
3744 /****************************************************************
3746 * /proc/driver/via/... setup and cleanup
3751 static int __init
via_init_proc (void)
3753 DPRINTK ("ENTER\n");
3755 if (!proc_mkdir ("driver/via", 0))
3758 DPRINTK ("EXIT, returning 0\n");
3763 static void via_cleanup_proc (void)
3765 DPRINTK ("ENTER\n");
3767 remove_proc_entry ("driver/via", NULL
);
3773 static int __devinit
via_card_init_proc (struct via_info
*card
)
3778 DPRINTK ("ENTER\n");
3780 sprintf (s
, "driver/via/%d", card
->card_num
);
3781 if (!proc_mkdir (s
, 0)) {
3786 sprintf (s
, "driver/via/%d/info", card
->card_num
);
3787 if (!create_proc_read_entry (s
, 0, 0, via_info_read_proc
, card
)) {
3792 sprintf (s
, "driver/via/%d/ac97", card
->card_num
);
3793 if (!create_proc_read_entry (s
, 0, 0, ac97_read_proc
, card
->ac97
)) {
3798 DPRINTK ("EXIT, returning 0\n");
3802 sprintf (s
, "driver/via/%d/info", card
->card_num
);
3803 remove_proc_entry (s
, NULL
);
3806 sprintf (s
, "driver/via/%d", card
->card_num
);
3807 remove_proc_entry (s
, NULL
);
3810 DPRINTK ("EXIT, returning %d\n", rc
);
3815 static void via_card_cleanup_proc (struct via_info
*card
)
3819 DPRINTK ("ENTER\n");
3821 sprintf (s
, "driver/via/%d/ac97", card
->card_num
);
3822 remove_proc_entry (s
, NULL
);
3824 sprintf (s
, "driver/via/%d/info", card
->card_num
);
3825 remove_proc_entry (s
, NULL
);
3827 sprintf (s
, "driver/via/%d", card
->card_num
);
3828 remove_proc_entry (s
, NULL
);
3833 #endif /* VIA_PROC_FS */