initial commit with v2.6.9
[linux-2.6.9-moxart.git] / sound / oss / via82cxxx_audio.c
blobdc1c25663b10a412eaa45bdc93847c7e533f9e42
1 /*
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.
10 * NO WARRANTY
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>
24 #include <linux/fs.h>
25 #include <linux/mm.h>
26 #include <linux/pci.h>
27 #include <linux/init.h>
28 #include <linux/interrupt.h>
29 #include <linux/proc_fs.h>
30 #include <linux/spinlock.h>
31 #include <linux/sound.h>
32 #include <linux/poll.h>
33 #include <linux/soundcard.h>
34 #include <linux/ac97_codec.h>
35 #include <linux/smp_lock.h>
36 #include <linux/ioport.h>
37 #include <linux/delay.h>
38 #include <asm/io.h>
39 #include <asm/uaccess.h>
40 #include <asm/semaphore.h>
41 #include "sound_config.h"
42 #include "dev_table.h"
43 #include "mpu401.h"
46 #undef VIA_DEBUG /* define to enable debugging output and checks */
47 #ifdef VIA_DEBUG
48 /* note: prints function name for you */
49 #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
50 #else
51 #define DPRINTK(fmt, args...)
52 #endif
54 #undef VIA_NDEBUG /* define to disable lightweight runtime checks */
55 #ifdef VIA_NDEBUG
56 #define assert(expr)
57 #else
58 #define assert(expr) \
59 if(!(expr)) { \
60 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
61 #expr,__FILE__,__FUNCTION__,__LINE__); \
63 #endif
65 #if defined(CONFIG_PROC_FS) && \
66 defined(CONFIG_SOUND_VIA82CXXX_PROCFS)
67 #define VIA_PROC_FS 1
68 #endif
70 #define VIA_SUPPORT_MMAP 1 /* buggy, for now... */
72 #define MAX_CARDS 1
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 | \
169 VIA_IRQ_ON_FLAG | \
170 VIA_IRQ_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 | \
188 VIA_CR41_AC97_SDO)
189 #define VIA_CR41_RUN_MASK (VIA_CR41_AC97_ENABLE | \
190 VIA_CR41_AC97_RESET | \
191 VIA_CR41_VRA | \
192 VIA_CR41_PCM_ENABLE)
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)
232 #else
233 #define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | \
234 DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
235 #endif
237 /* scatter-gather DMA table entry, exactly as passed to hardware */
238 struct via_sgd_table {
239 u32 addr;
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 {
249 sgd_stopped = 0,
250 sgd_in_progress = 1,
254 struct via_buffer_pgtbl {
255 dma_addr_t handle;
256 void *cpuaddr;
260 struct via_channel {
261 atomic_t n_frags;
262 atomic_t hw_ptr;
263 wait_queue_head_t wait;
265 unsigned int sw_ptr;
266 unsigned int slop_len;
267 unsigned int n_irqs;
268 int bytes;
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];
290 long iobase;
292 const char *name;
296 /* data stored for each chip */
297 struct via_info {
298 struct pci_dev *pdev;
299 long baseaddr;
301 struct ac97_codec *ac97;
302 spinlock_t ac97_lock;
303 spinlock_t lock;
304 int card_num; /* unique card number, from 0 */
306 int dev_dsp; /* /dev/dsp index from register_sound_dsp() */
308 unsigned rev_h : 1;
309 unsigned legacy: 1; /* Has legacy ports */
310 unsigned intmask: 1; /* Needs int bits */
311 unsigned sixchannel: 1; /* 8233/35 with 6 channel support */
312 unsigned volume: 1;
314 int locked_rate : 1;
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
324 moment */
326 struct via_channel ch_in;
327 struct via_channel ch_out;
328 struct via_channel ch_fm;
330 #ifdef CONFIG_MIDI_VIA82CXXX
331 void *midi_devc;
332 struct address_info midi_info;
333 #endif
337 /* number of cards, used for assigning unique numbers to cards */
338 static unsigned via_num_cards;
342 /****************************************************************
344 * prototypes
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);
369 #ifdef VIA_PROC_FS
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);
374 #else
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) {}
379 #endif
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},
395 { 0, }
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);
453 if (tmp != 0)
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");
483 return 0;
485 if (tmp & VIA_SGD_ACTIVE)
486 return 1;
487 return 0;
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)
515 /* Thomas Sailer:
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
521 nonblock = 0;
523 if (nonblock) {
524 if (down_trylock (&card->syscall_sem))
525 return -EAGAIN;
526 } else {
527 if (down_interruptible (&card->syscall_sem))
528 return -ERESTARTSYS;
531 return 0;
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)
545 u8 tmp, new_tmp;
547 DPRINTK ("ENTER\n");
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);
557 if(card->sixchannel)
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);
566 if(card->sixchannel)
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);
574 if (tmp != new_tmp)
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);
579 if (tmp != new_tmp)
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);
584 if (tmp != new_tmp)
585 outb (0, card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
587 if(card->sixchannel)
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);
591 if (tmp != new_tmp)
592 outb (0, card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN_TYPE);
595 udelay(10);
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);
604 DPRINTK ("EXIT\n");
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;
624 int rate_reg;
625 u32 dacp;
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)
632 goto out;
633 if (card->locked_rate) {
634 chan->rate = 48000;
635 goto out;
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);
658 /* Set new rate */
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;
679 chan->rate = 48000;
680 printk (KERN_WARNING PFX "Codec rate locked at 48Khz\n");
683 out:
684 DPRINTK ("EXIT, returning rate %d Hz\n", chan->rate);
685 return 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));
709 if(card->intmask)
710 chan->intmask = 0x23; /* Turn on the IRQ bits */
712 if (chan == &card->ch_out) {
713 chan->name = "PCM-OUT";
714 if(card->sixchannel)
716 chan->iobase = card->baseaddr + VIA_BASE0_MULTI_OUT_CHAN;
717 chan->is_multi = 1;
718 DPRINTK("Using multichannel for pcm out\n");
720 else
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;
725 chan->is_record = 1;
726 } else if (chan == &card->ch_fm) {
727 chan->name = "PCM-OUT-FM";
728 chan->iobase = card->baseaddr + VIA_BASE0_FM_OUT_CHAN;
729 } else {
730 BUG();
733 init_waitqueue_head (&chan->wait);
735 chan->pcm_fmt = VIA_PCM_FMT_MASK;
736 chan->is_enabled = 1;
738 chan->frag_number = 0;
739 chan->frag_size = 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)
760 DPRINTK ("ENTER\n");
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);
770 DPRINTK ("EXIT\n");
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,
783 * and passing the
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)
794 int page, offset;
795 int i;
797 DPRINTK ("ENTER\n");
800 chan->intmask = 0;
801 if(card->intmask)
802 chan->intmask = 0x23; /* Turn on the IRQ bits */
804 if (chan->sgtable != NULL) {
805 DPRINTK ("EXIT\n");
806 return 0;
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),
812 &chan->sgt_handle);
813 if (!chan->sgtable) {
814 printk (KERN_ERR PFX "DMA table alloc fail, aborting\n");
815 DPRINTK ("EXIT\n");
816 return -ENOMEM;
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;
833 goto err_out_nomem;
836 #ifndef VIA_NDEBUG
837 memset (chan->pgtbl[i].cpuaddr, 0xBC, chan->frag_size);
838 #endif
840 #if 1
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);
845 #endif
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);
856 #if 1
857 DPRINTK ("dmabuf #%d (32(h)=%lx)\n",
859 (long)chan->sgtable[i].addr);
860 #endif
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);
872 udelay (20);
873 via_ac97_wait_idle (card);
874 /* load no rate adaption, stereo 16bit, set up ring slots */
875 if(card->sixchannel)
877 if(!chan->is_multi)
879 outl (0xFFFFF | (0x3 << 20) | (chan->frag_number << 24), chan->iobase + VIA_PCM_STOPRATE);
880 udelay (20);
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));
889 DPRINTK ("EXIT\n");
890 return 0;
892 err_out_nomem:
893 printk (KERN_ERR PFX "DMA buffer alloc fail, aborting\n");
894 via_chan_buffer_free (card, chan);
895 DPRINTK ("EXIT\n");
896 return -ENOMEM;
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
911 * sound buffers.
914 static void via_chan_free (struct via_info *card, struct via_channel *chan)
916 DPRINTK ("ENTER\n");
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);
929 DPRINTK ("EXIT\n");
932 static void via_chan_buffer_free (struct via_info *card, struct via_channel *chan)
934 int i;
936 DPRINTK ("ENTER\n");
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;
953 if (chan->sgtable) {
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;
960 DPRINTK ("EXIT\n");
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);
986 if (reset)
988 /* reset to 8-bit mono mode */
989 chan->pcm_fmt = 0;
990 chan->channels = 1;
993 /* enable interrupts on FLAG and EOL */
994 chan->pcm_fmt |= VIA_CHAN_TYPE_MASK;
996 /* if we are recording, enable recording fifo bit */
997 if (chan->is_record)
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);
1005 if(chan->intmask)
1007 u32 m;
1010 * Channel 0x4 is up to 6 x 16bit and has to be
1011 * programmed differently
1014 if(chan->is_multi)
1016 u8 c = 0;
1019 * Load the type bit for num channels
1020 * and 8/16bit
1023 if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
1024 c = 1 << 7;
1025 if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
1026 c |= (2<<4);
1027 else
1028 c |= (1<<4);
1030 outb(c, chan->iobase + VIA_PCM_TYPE);
1033 * Set the channel steering
1034 * Mono
1035 * Channel 0 to slot 3
1036 * Channel 0 to slot 4
1037 * Stereo
1038 * Channel 0 to slot 3
1039 * Channel 1 to slot 4
1042 switch(chan->channels)
1044 case 1:
1045 outl(0xFF000000 | (1<<0) | (1<<4) , chan->iobase + VIA_PCM_STOPRATE);
1046 break;
1047 case 2:
1048 outl(0xFF000000 | (1<<0) | (2<<4) , chan->iobase + VIA_PCM_STOPRATE);
1049 break;
1050 case 4:
1051 outl(0xFF000000 | (1<<0) | (2<<4) | (3<<8) | (4<<12), chan->iobase + VIA_PCM_STOPRATE);
1052 break;
1053 case 6:
1054 outl(0xFF000000 | (1<<0) | (2<<4) | (5<<8) | (6<<12) | (3<<16) | (4<<20), chan->iobase + VIA_PCM_STOPRATE);
1055 break;
1058 else
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);
1068 m &= ~(3<<20);
1069 if(chan->pcm_fmt & VIA_PCM_FMT_STEREO)
1070 m |= (1 << 20);
1071 if(chan->pcm_fmt & VIA_PCM_FMT_16BIT)
1072 m |= (1 << 21);
1073 outl(m, chan->iobase + VIA_PCM_STOPRATE);
1076 else
1077 outb (chan->pcm_fmt, chan->iobase + VIA_PCM_TYPE);
1080 DPRINTK ("EXIT, pcm_fmt = 0x%02X, reg = 0x%02X\n",
1081 chan->pcm_fmt,
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;
1103 chan->slop_len = 0;
1104 chan->sw_ptr = 0;
1105 chan->n_irqs = 0;
1106 atomic_set (&chan->hw_ptr, 0);
1107 DPRINTK ("EXIT\n");
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
1123 * to the new rate.
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);
1136 return 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" :
1161 "unknown");
1163 via_chan_clear (card, chan);
1165 assert (val != AFMT_QUERY); /* this case is handled elsewhere */
1167 switch (val) {
1168 case AFMT_S16_LE:
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);
1173 break;
1175 case AFMT_U8:
1176 if (chan->pcm_fmt & VIA_PCM_FMT_16BIT) {
1177 chan->pcm_fmt &= ~VIA_PCM_FMT_16BIT;
1178 via_chan_pcm_fmt (chan, 0);
1180 break;
1182 default:
1183 DPRINTK ("unknown AFMT: 0x%X\n", val);
1184 val = AFMT_S16_LE;
1187 DPRINTK ("EXIT\n");
1188 return 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);
1214 switch (val) {
1216 /* mono */
1217 case 1:
1218 chan->pcm_fmt &= ~VIA_PCM_FMT_STEREO;
1219 chan->channels = 1;
1220 via_chan_pcm_fmt (chan, 0);
1221 break;
1223 /* stereo */
1224 case 2:
1225 chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
1226 chan->channels = 2;
1227 via_chan_pcm_fmt (chan, 0);
1228 break;
1230 case 4:
1231 case 6:
1232 if(chan->is_multi)
1234 chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
1235 chan->channels = val;
1236 break;
1238 /* unknown */
1239 default:
1240 val = -EINVAL;
1241 break;
1244 DPRINTK ("EXIT, returning %d\n", val);
1245 return val;
1248 static int via_chan_set_buffering (struct via_info *card,
1249 struct via_channel *chan, int val)
1251 int shift;
1253 DPRINTK ("ENTER\n");
1255 /* in both cases the buffer cannot be changed */
1256 if (chan->is_active || chan->is_mapped) {
1257 DPRINTK ("EXIT\n");
1258 return -EINVAL;
1261 /* called outside SETFRAGMENT */
1262 /* set defaults or do nothing */
1263 if (val < 0) {
1265 if (chan->frag_size && chan->frag_number)
1266 goto out;
1268 DPRINTK ("\n");
1270 chan->frag_size = (VIA_DEFAULT_FRAG_TIME * chan->rate * chan->channels
1271 * ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) ? 2 : 1)) / 1000 - 1;
1273 shift = 0;
1274 while (chan->frag_size) {
1275 chan->frag_size >>= 1;
1276 shift++;
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);
1283 } else {
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 */
1291 shift = 0;
1292 while (chan->frag_number) {
1293 chan->frag_number >>= 1;
1294 shift++;
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;
1311 out:
1312 if (chan->is_record)
1313 atomic_set (&chan->n_frags, 0);
1314 else
1315 atomic_set (&chan->n_frags, chan->frag_number);
1317 DPRINTK ("EXIT\n");
1319 return 0;
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)
1333 int i;
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))
1363 chan->sw_ptr = 0;
1364 else
1365 chan->sw_ptr++;
1367 chan->slop_len = 0;
1369 assert (atomic_read (&chan->n_frags) > 0);
1370 atomic_dec (&chan->n_frags);
1372 DPRINTK ("EXIT\n");
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;
1392 sg_begin (chan);
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)
1416 u8 tmp8;
1417 int counter = VIA_COUNTER_LIMIT;
1419 DPRINTK ("ENTER/EXIT\n");
1421 assert (card != NULL);
1422 assert (card->pdev != NULL);
1424 do {
1425 udelay (15);
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");
1432 return tmp8;
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
1448 * register.
1451 static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg)
1453 unsigned long data;
1454 struct via_info *card;
1455 int counter;
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);
1472 udelay (20);
1474 for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
1475 udelay (1);
1476 if ((((data = inl(card->baseaddr + VIA_BASE0_AC97_CTRL)) &
1477 (VIA_CR80_VALID|VIA_CR80_BUSY)) == VIA_CR80_VALID))
1478 goto out;
1481 printk (KERN_WARNING PFX "timeout while reading AC97 codec (0x%lX)\n", data);
1482 goto err_out;
1484 out:
1485 /* Once the valid bit has become set, we must wait a complete AC97
1486 frame before the data has settled. */
1487 udelay(25);
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));
1502 err_out:
1503 spin_unlock(&card->ac97_lock);
1504 DPRINTK ("EXIT, returning 0\n");
1505 return 0;
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)
1524 u32 data;
1525 struct via_info *card;
1526 int counter;
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);
1539 udelay (10);
1541 for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
1542 if ((inb (card->baseaddr + 0x83) & VIA_CR83_BUSY) == 0)
1543 goto out;
1545 udelay (15);
1548 printk (KERN_WARNING PFX "timeout after AC97 codec write (0x%X, 0x%X)\n", reg, value);
1550 out:
1551 spin_unlock(&card->ac97_lock);
1552 DPRINTK ("EXIT\n");
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)
1572 goto match;
1576 DPRINTK ("EXIT, returning -ENODEV\n");
1577 return -ENODEV;
1579 match:
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,
1587 unsigned long arg)
1589 struct ac97_codec *codec = file->private_data;
1590 struct via_info *card;
1591 int nonblock = (file->f_flags & O_NONBLOCK);
1592 int rc;
1594 DPRINTK ("ENTER\n");
1596 assert (codec != NULL);
1597 card = codec->private_data;
1598 assert (card != NULL);
1600 rc = via_syscall_down (card, nonblock);
1601 if (rc) goto out;
1603 #if 0
1605 * Intercept volume control on 8233 and 8235
1607 if(card->volume)
1609 switch(cmd)
1611 case SOUND_MIXER_READ_VOLUME:
1612 return card->mixer_vol;
1613 case SOUND_MIXER_WRITE_VOLUME:
1615 int v;
1616 if(get_user(v, (int *)arg))
1618 rc = -EFAULT;
1619 goto out;
1621 card->mixer_vol = v;
1625 #endif
1626 rc = codec->mixer_ioctl(codec, cmd, arg);
1628 up (&card->syscall_sem);
1630 out:
1631 DPRINTK ("EXIT, returning %d\n", rc);
1632 return 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;
1647 u8 tmp8;
1648 u16 tmp16;
1650 DPRINTK ("ENTER\n");
1652 assert (pdev != NULL);
1654 #ifndef NDEBUG
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);
1678 #endif
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);
1692 udelay (100);
1694 pci_write_config_byte (pdev, VIA_ACLINK_CTRL, 0);
1695 udelay (100);
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);
1701 udelay (100);
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);
1716 udelay (100);
1719 if(card->legacy)
1721 #if 0 /* this breaks on K7M */
1722 /* disable legacy stuff */
1723 pci_write_config_byte (pdev, 0x42, 0x00);
1724 udelay(10);
1725 #endif
1727 /* route FM trap to IRQ, disable FM trap */
1728 pci_write_config_byte (pdev, 0x48, 0x05);
1729 udelay(10);
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");
1743 return 0;
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)
1756 int rc;
1757 u16 tmp16;
1759 DPRINTK ("ENTER\n");
1761 assert (card != NULL);
1763 card->ac97 = ac97_alloc_codec();
1764 if(card->ac97 == NULL)
1765 return -ENOMEM;
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);
1777 return -EIO;
1780 rc = via_ac97_reset (card);
1781 if (rc) {
1782 printk (KERN_ERR PFX "unable to reset AC97 codec, aborting\n");
1783 goto err_out;
1786 mdelay(10);
1788 if (ac97_probe_codec (card->ac97) == 0) {
1789 printk (KERN_ERR PFX "unable to probe AC97 codec, aborting\n");
1790 rc = -EIO;
1791 goto err_out;
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");
1813 return 0;
1815 err_out:
1816 unregister_sound_mixer (card->ac97->dev_mixer);
1817 DPRINTK ("EXIT, returning %d\n", rc);
1818 ac97_release_codec(card->ac97);
1819 return rc;
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);
1833 DPRINTK ("EXIT\n");
1838 /****************************************************************
1840 * Interrupt-related code
1845 * via_intr_channel - handle an interrupt for a single channel
1846 * @card: unused
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)
1859 u8 status;
1860 int n;
1862 /* check pertinent bits of status register for action bits */
1863 status = inb (chan->iobase) & (VIA_SGD_FLAG | VIA_SGD_EOL | VIA_SGD_STOPPED);
1864 if (!status)
1865 return;
1867 /* acknowledge any flagged bits ASAP */
1868 outb (status, chan->iobase);
1870 if (!chan->sgtable) /* XXX: temporary solution */
1871 return;
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 */
1877 assert (n >= 0);
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);
1887 } else {
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 */
1893 chan->n_irqs++;
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;
1932 u32 status32;
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);
1945 #endif
1946 return IRQ_HANDLED;
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);
1964 return IRQ_HANDLED;
1967 static irqreturn_t via_new_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1969 struct via_info *card = dev_id;
1970 u32 status32;
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))
1979 return IRQ_NONE;
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);
1990 return IRQ_HANDLED;
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)
2005 u8 tmp8;
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",
2015 card->pdev->irq);
2016 DPRINTK ("EXIT, returning -EIO\n");
2017 return -EIO;
2020 /* VIA requires this is done */
2021 pci_write_config_byte(card->pdev, PCI_INTERRUPT_LINE, card->pdev->irq);
2023 if(card->legacy)
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",
2033 card->pdev->irq);
2034 DPRINTK ("EXIT, returning -EBUSY\n");
2035 return -EBUSY;
2038 else
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",
2042 card->pdev->irq);
2043 DPRINTK ("EXIT, returning -EBUSY\n");
2044 return -EBUSY;
2048 DPRINTK ("EXIT, returning 0\n");
2049 return 0;
2053 /****************************************************************
2055 * OSS DSP device
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)
2074 u8 tmp8;
2076 DPRINTK ("ENTER\n");
2078 assert (card != NULL);
2080 if(card->legacy)
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");
2095 return -ENODEV;
2097 DPRINTK ("EXIT, returning 0\n");
2098 return 0;
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);
2113 DPRINTK ("EXIT\n");
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;
2124 int rd, wr;
2126 DPRINTK ("ENTER, start %lXh, ofs %lXh, pgoff %ld, addr %lXh\n",
2127 vma->vm_start,
2128 address - vma->vm_start,
2129 (address - vma->vm_start) >> PAGE_SHIFT,
2130 address);
2132 if (address > vma->vm_end) {
2133 DPRINTK ("EXIT, returning NOPAGE_SIGBUS\n");
2134 return NOPAGE_SIGBUS; /* Disallow mremap */
2136 if (!card) {
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;
2145 #ifndef VIA_NDEBUG
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);
2152 #endif
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;
2159 } else if (!wr)
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);
2167 get_page (dmapage);
2168 if (type)
2169 *type = VM_FAULT_MINOR;
2170 return dmapage;
2174 #ifndef VM_RESERVED
2175 static int via_mm_swapout (struct page *page, struct file *filp)
2177 return 0;
2179 #endif /* VM_RESERVED */
2182 struct vm_operations_struct via_mm_ops = {
2183 .nopage = via_mm_nopage,
2185 #ifndef VM_RESERVED
2186 .swapout = via_mm_swapout,
2187 #endif
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",
2204 vma->vm_start,
2205 vma->vm_end - vma->vm_start,
2206 vma->vm_pgoff);
2208 max_size = 0;
2209 if (vma->vm_flags & VM_READ) {
2210 rd = 1;
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) {
2216 wr = 1;
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)
2228 goto out;
2229 if (offset > max_size - size)
2230 goto out;
2232 rc = via_syscall_down (card, nonblock);
2233 if (rc) goto out;
2235 vma->vm_ops = &via_mm_ops;
2236 vma->vm_private_data = card;
2238 #ifdef VM_RESERVED
2239 vma->vm_flags |= VM_RESERVED;
2240 #endif
2242 if (rd)
2243 card->ch_in.is_mapped = 1;
2244 if (wr)
2245 card->ch_out.is_mapped = 1;
2247 up (&card->syscall_sem);
2248 rc = 0;
2250 out:
2251 DPRINTK ("EXIT, returning %d\n", rc);
2252 return rc;
2256 static ssize_t via_dsp_do_read (struct via_info *card,
2257 char __user *userbuf, size_t count,
2258 int nonblock)
2260 DECLARE_WAITQUEUE(wait, current);
2261 const char __user *orig_userbuf = userbuf;
2262 struct via_channel *chan = &card->ch_in;
2263 size_t size;
2264 int n, tmp;
2265 ssize_t ret = 0;
2267 /* if SGD has not yet been started, start it */
2268 via_chan_maybe_start (chan);
2270 handle_one_block:
2271 /* just to be a nice neighbor */
2272 /* Thomas Sailer:
2273 * But also to ourselves, release semaphore if we do so */
2274 if (need_resched()) {
2275 up(&card->syscall_sem);
2276 schedule ();
2277 ret = via_syscall_down (card, nonblock);
2278 if (ret)
2279 goto out;
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.
2286 n = chan->sw_ptr;
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);
2293 for (;;) {
2294 __set_current_state(TASK_INTERRUPTIBLE);
2295 tmp = atomic_read (&chan->n_frags);
2296 assert (tmp >= 0);
2297 assert (tmp <= chan->frag_number);
2298 if (tmp)
2299 break;
2300 if (nonblock || !chan->is_active) {
2301 ret = -EAGAIN;
2302 break;
2305 up(&card->syscall_sem);
2307 DPRINTK ("Sleeping on block %d\n", n);
2308 schedule();
2310 ret = via_syscall_down (card, nonblock);
2311 if (ret)
2312 break;
2314 if (signal_pending (current)) {
2315 ret = -ERESTARTSYS;
2316 break;
2319 set_current_state(TASK_RUNNING);
2320 remove_wait_queue(&chan->wait, &wait);
2321 if (ret)
2322 goto out;
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,
2335 size)) {
2336 ret = -EFAULT;
2337 goto out;
2340 count -= size;
2341 chan->slop_len += size;
2342 userbuf += size;
2345 /* If we didn't copy the buffer completely to userspace,
2346 * stop now.
2348 if (chan->slop_len < chan->frag_size)
2349 goto out;
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))
2360 chan->sw_ptr = 0;
2361 else
2362 chan->sw_ptr++;
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 */
2369 chan->slop_len = 0;
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));
2383 if (count > 0)
2384 goto handle_one_block;
2386 out:
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);
2395 int rc;
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);
2405 if (rc) goto out;
2407 if (card->ch_in.is_mapped) {
2408 rc = -ENXIO;
2409 goto out_up;
2412 via_chan_set_buffering(card, &card->ch_in, -1);
2413 rc = via_chan_buffer_init (card, &card->ch_in);
2415 if (rc)
2416 goto out_up;
2418 rc = via_dsp_do_read (card, buffer, count, nonblock);
2420 out_up:
2421 up (&card->syscall_sem);
2422 out:
2423 DPRINTK ("EXIT, returning %ld\n",(long) rc);
2424 return rc;
2428 static ssize_t via_dsp_do_write (struct via_info *card,
2429 const char __user *userbuf, size_t count,
2430 int nonblock)
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;
2436 size_t size;
2437 int n, tmp;
2438 ssize_t ret = 0;
2440 handle_one_block:
2441 /* just to be a nice neighbor */
2442 /* Thomas Sailer:
2443 * But also to ourselves, release semaphore if we do so */
2444 if (need_resched()) {
2445 up(&card->syscall_sem);
2446 schedule ();
2447 ret = via_syscall_down (card, nonblock);
2448 if (ret)
2449 goto out;
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.
2456 n = chan->sw_ptr;
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);
2463 for (;;) {
2464 __set_current_state(TASK_INTERRUPTIBLE);
2465 tmp = atomic_read (&chan->n_frags);
2466 assert (tmp >= 0);
2467 assert (tmp <= chan->frag_number);
2468 if (tmp)
2469 break;
2470 if (nonblock || !chan->is_active) {
2471 ret = -EAGAIN;
2472 break;
2475 up(&card->syscall_sem);
2477 DPRINTK ("Sleeping on page %d, tmp==%d, ir==%d\n", n, tmp, chan->is_record);
2478 schedule();
2480 ret = via_syscall_down (card, nonblock);
2481 if (ret)
2482 break;
2484 if (signal_pending (current)) {
2485 ret = -ERESTARTSYS;
2486 break;
2489 set_current_state(TASK_RUNNING);
2490 remove_wait_queue(&chan->wait, &wait);
2491 if (ret)
2492 goto out;
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,
2502 userbuf, size)) {
2503 ret = -EFAULT;
2504 goto out;
2507 count -= size;
2508 chan->slop_len += size;
2509 userbuf += 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);
2518 goto out;
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);
2529 else
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))
2536 chan->sw_ptr = 0;
2537 else
2538 chan->sw_ptr++;
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 */
2545 chan->slop_len = 0;
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));
2563 if (count > 0)
2564 goto handle_one_block;
2566 out:
2567 if (userbuf - orig_userbuf)
2568 return userbuf - orig_userbuf;
2569 else
2570 return ret;
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;
2577 ssize_t rc;
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);
2588 if (rc) goto out;
2590 if (card->ch_out.is_mapped) {
2591 rc = -ENXIO;
2592 goto out_up;
2595 via_chan_set_buffering(card, &card->ch_out, -1);
2596 rc = via_chan_buffer_init (card, &card->ch_out);
2598 if (rc)
2599 goto out_up;
2601 rc = via_dsp_do_write (card, buffer, count, nonblock);
2603 out_up:
2604 up (&card->syscall_sem);
2605 out:
2606 DPRINTK ("EXIT, returning %ld\n",(long) rc);
2607 return 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);
2640 return 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
2651 * hardware.
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);
2660 int ret = 0;
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)
2668 goto out;
2670 via_chan_maybe_start (chan);
2672 add_wait_queue(&chan->wait, &wait);
2673 for (;;) {
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)
2677 break;
2679 if (nonblock) {
2680 DPRINTK ("EXIT, returning -EAGAIN\n");
2681 ret = -EAGAIN;
2682 break;
2685 #ifdef VIA_DEBUG
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");
2709 #endif
2711 up(&card->syscall_sem);
2713 DPRINTK ("sleeping, nbufs=%d\n", atomic_read (&chan->n_frags));
2714 schedule();
2716 if ((ret = via_syscall_down (card, nonblock)))
2717 break;
2719 if (signal_pending (current)) {
2720 DPRINTK ("EXIT, returning -ERESTARTSYS\n");
2721 ret = -ERESTARTSYS;
2722 break;
2725 set_current_state(TASK_RUNNING);
2726 remove_wait_queue(&chan->wait, &wait);
2728 #ifdef VIA_DEBUG
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));
2751 #endif
2753 out:
2754 DPRINTK ("EXIT, returning %d\n", ret);
2755 return 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,
2772 void __user *arg)
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))
2785 info.fragments--;
2787 /* number of bytes that can be read or written immediately
2788 * without blocking.
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",
2795 info.fragstotal,
2796 info.fragsize,
2797 info.fragments,
2798 info.bytes);
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,
2817 void __user *arg)
2819 count_info info;
2821 spin_lock_irq (&card->lock);
2823 info.bytes = chan->bytes;
2824 info.blocks = chan->n_irqs;
2825 chan->n_irqs = 0;
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);
2833 info.ptr += extra;
2834 info.bytes += extra;
2835 } else {
2836 info.ptr = 0;
2839 DPRINTK ("EXIT, returning bytes=%d, blocks=%d, ptr=%d\n",
2840 info.bytes,
2841 info.blocks,
2842 info.ptr);
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);
2854 else
2855 enable = (val & PCM_ENABLE_OUTPUT);
2857 if (!chan->is_enabled && enable) {
2858 do_something = 1;
2859 } else if (chan->is_enabled && !enable) {
2860 do_something = -1;
2861 } else {
2862 do_something = 0;
2865 DPRINTK ("enable=%d, do_something=%d\n",
2866 enable, do_something);
2868 if (chan->is_active && do_something)
2869 return -EINVAL;
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");
2882 return 0;
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)
2901 wr = 1;
2902 if (file->f_mode & FMODE_READ)
2903 rd = 1;
2905 rc = via_syscall_down (card, nonblock);
2906 if (rc)
2907 return rc;
2908 rc = -EINVAL;
2910 switch (cmd) {
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);
2916 break;
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);
2922 break;
2924 /* query or set current channel's PCM data format */
2925 case SNDCTL_DSP_SETFMT:
2926 if (get_user(val, ip)) {
2927 rc = -EFAULT;
2928 break;
2930 DPRINTK ("DSP_SETFMT, val==%d\n", val);
2931 if (val != AFMT_QUERY) {
2932 rc = 0;
2934 if (rd)
2935 rc = via_chan_set_fmt (card, &card->ch_in, val);
2937 if (rc >= 0 && wr)
2938 rc = via_chan_set_fmt (card, &card->ch_out, val);
2940 if (rc < 0)
2941 break;
2943 val = rc;
2944 } else {
2945 if ((rd && (card->ch_in.pcm_fmt & VIA_PCM_FMT_16BIT)) ||
2946 (wr && (card->ch_out.pcm_fmt & VIA_PCM_FMT_16BIT)))
2947 val = AFMT_S16_LE;
2948 else
2949 val = AFMT_U8;
2951 DPRINTK ("SETFMT EXIT, returning %d\n", val);
2952 rc = put_user (val, ip);
2953 break;
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)) {
2958 rc = -EFAULT;
2959 break;
2961 DPRINTK ("DSP_CHANNELS, val==%d\n", val);
2962 if (val != 0) {
2963 rc = 0;
2965 if (rd)
2966 rc = via_chan_set_stereo (card, &card->ch_in, val);
2968 if (rc >= 0 && wr)
2969 rc = via_chan_set_stereo (card, &card->ch_out, val);
2971 if (rc < 0)
2972 break;
2974 val = rc;
2975 } else {
2976 if (rd)
2977 val = card->ch_in.channels;
2978 else
2979 val = card->ch_out.channels;
2981 DPRINTK ("CHANNELS EXIT, returning %d\n", val);
2982 rc = put_user (val, ip);
2983 break;
2985 /* enable (val is not zero) or disable (val == 0) stereo */
2986 case SNDCTL_DSP_STEREO:
2987 if (get_user(val, ip)) {
2988 rc = -EFAULT;
2989 break;
2991 DPRINTK ("DSP_STEREO, val==%d\n", val);
2992 rc = 0;
2994 if (rd)
2995 rc = via_chan_set_stereo (card, &card->ch_in, val ? 2 : 1);
2996 if (rc >= 0 && wr)
2997 rc = via_chan_set_stereo (card, &card->ch_out, val ? 2 : 1);
2999 if (rc < 0)
3000 break;
3002 val = rc - 1;
3004 DPRINTK ("STEREO EXIT, returning %d\n", val);
3005 rc = put_user(val, ip);
3006 break;
3008 /* query or set sampling rate */
3009 case SNDCTL_DSP_SPEED:
3010 if (get_user(val, ip)) {
3011 rc = -EFAULT;
3012 break;
3014 DPRINTK ("DSP_SPEED, val==%d\n", val);
3015 if (val < 0) {
3016 rc = -EINVAL;
3017 break;
3019 if (val > 0) {
3020 rc = 0;
3022 if (rd)
3023 rc = via_chan_set_speed (card, &card->ch_in, val);
3024 if (rc >= 0 && wr)
3025 rc = via_chan_set_speed (card, &card->ch_out, val);
3027 if (rc < 0)
3028 break;
3030 val = rc;
3031 } else {
3032 if (rd)
3033 val = card->ch_in.rate;
3034 else if (wr)
3035 val = card->ch_out.rate;
3036 else
3037 val = 0;
3039 DPRINTK ("SPEED EXIT, returning %d\n", val);
3040 rc = put_user (val, ip);
3041 break;
3043 /* wait until all buffers have been played, and then stop device */
3044 case SNDCTL_DSP_SYNC:
3045 DPRINTK ("DSP_SYNC\n");
3046 rc = 0;
3047 if (wr) {
3048 DPRINTK ("SYNC EXIT (after calling via_dsp_drain_playback)\n");
3049 rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
3051 break;
3053 /* stop recording/playback immediately */
3054 case SNDCTL_DSP_RESET:
3055 DPRINTK ("DSP_RESET\n");
3056 if (rd) {
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);
3063 if (wr) {
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);
3070 rc = 0;
3071 break;
3073 case SNDCTL_DSP_NONBLOCK:
3074 file->f_flags |= O_NONBLOCK;
3075 rc = 0;
3076 break;
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);
3082 break;
3084 /* obtain buffer fragment size */
3085 case SNDCTL_DSP_GETBLKSIZE:
3086 DPRINTK ("DSP_GETBLKSIZE\n");
3088 if (rd) {
3089 via_chan_set_buffering(card, &card->ch_in, -1);
3090 rc = put_user(card->ch_in.frag_size, ip);
3091 } else if (wr) {
3092 via_chan_set_buffering(card, &card->ch_out, -1);
3093 rc = put_user(card->ch_out.frag_size, ip);
3095 break;
3097 /* obtain information about input buffering */
3098 case SNDCTL_DSP_GETISPACE:
3099 DPRINTK ("DSP_GETISPACE\n");
3100 if (rd)
3101 rc = via_dsp_ioctl_space (card, &card->ch_in, p);
3102 break;
3104 /* obtain information about output buffering */
3105 case SNDCTL_DSP_GETOSPACE:
3106 DPRINTK ("DSP_GETOSPACE\n");
3107 if (wr)
3108 rc = via_dsp_ioctl_space (card, &card->ch_out, p);
3109 break;
3111 /* obtain information about input hardware pointer */
3112 case SNDCTL_DSP_GETIPTR:
3113 DPRINTK ("DSP_GETIPTR\n");
3114 if (rd)
3115 rc = via_dsp_ioctl_ptr (card, &card->ch_in, p);
3116 break;
3118 /* obtain information about output hardware pointer */
3119 case SNDCTL_DSP_GETOPTR:
3120 DPRINTK ("DSP_GETOPTR\n");
3121 if (wr)
3122 rc = via_dsp_ioctl_ptr (card, &card->ch_out, p);
3123 break;
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;
3132 if (!wr)
3133 break;
3135 if (chan->is_active) {
3137 val = chan->frag_number - atomic_read (&chan->n_frags);
3139 assert(val >= 0);
3141 if (val > 0) {
3142 val *= chan->frag_size;
3143 val -= chan->frag_size - via_sg_offset(chan);
3145 val += chan->slop_len % chan->frag_size;
3146 } else
3147 val = 0;
3149 assert (val <= (chan->frag_size * chan->frag_number));
3151 DPRINTK ("GETODELAY EXIT, val = %d bytes\n", val);
3152 rc = put_user (val, ip);
3153 break;
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)) {
3162 rc = -EFAULT;
3163 break;
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);
3169 rc = 0;
3171 if (rd)
3172 rc = via_dsp_ioctl_trigger (&card->ch_in, val);
3174 if (!rc && wr)
3175 rc = via_dsp_ioctl_trigger (&card->ch_out, val);
3177 break;
3179 case SNDCTL_DSP_GETTRIGGER:
3180 val = 0;
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);
3186 break;
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");
3193 if (!rd || !wr)
3194 break;
3195 rc = 0;
3196 break;
3198 /* set fragment size. implemented as a successful no-op for now */
3199 case SNDCTL_DSP_SETFRAGMENT:
3200 if (get_user(val, ip)) {
3201 rc = -EFAULT;
3202 break;
3204 DPRINTK ("DSP_SETFRAGMENT, val==%d\n", val);
3206 if (rd)
3207 rc = via_chan_set_buffering(card, &card->ch_in, val);
3209 if (wr)
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",
3213 val & 0xFFFF,
3214 val & 0xFFFF,
3215 (val >> 16) & 0xFFFF,
3216 (val >> 16) & 0xFFFF);
3218 rc = 0;
3219 break;
3221 /* inform device of an upcoming pause in input (or output). */
3222 case SNDCTL_DSP_POST:
3223 DPRINTK ("DSP_POST\n");
3224 if (wr) {
3225 if (card->ch_out.slop_len > 0)
3226 via_chan_flush_frag (&card->ch_out);
3227 via_chan_maybe_start (&card->ch_out);
3230 rc = 0;
3231 break;
3233 /* not implemented */
3234 default:
3235 DPRINTK ("unhandled ioctl, cmd==%u, arg==%p\n",
3236 cmd, p);
3237 break;
3240 up (&card->syscall_sem);
3241 DPRINTK ("EXIT, returning %d\n", rc);
3242 return 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");
3259 return -EINVAL;
3262 card = NULL;
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)
3274 goto match;
3278 DPRINTK ("no matching %s found\n", card ? "minor" : "driver");
3279 return -ENODEV;
3281 match:
3282 if (nonblock) {
3283 if (down_trylock (&card->open_sem)) {
3284 DPRINTK ("EXIT, returning -EAGAIN\n");
3285 return -EAGAIN;
3287 } else {
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;
3305 chan->channels = 2;
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;
3322 chan->channels = 2;
3323 via_chan_pcm_fmt (chan, 0);
3324 via_set_rate (card->ac97, chan, 44100);
3325 } else {
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);
3330 } else {
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);
3346 int rc;
3348 DPRINTK ("ENTER\n");
3350 assert (file != NULL);
3351 card = file->private_data;
3352 assert (card != NULL);
3354 rc = via_syscall_down (card, nonblock);
3355 if (rc) {
3356 DPRINTK ("EXIT (syscall_down error), rc=%d\n", rc);
3357 return 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");
3378 return 0;
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
3392 u8 r42;
3393 #endif
3394 int rc;
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);
3404 if (rc)
3405 goto err_out;
3407 rc = pci_request_regions (pdev, "via82cxxx_audio");
3408 if (rc)
3409 goto err_out_disable;
3411 rc = pci_set_dma_mask(pdev, 0xffffffffULL);
3412 if (rc)
3413 goto err_out_res;
3414 rc = pci_set_consistent_dma_mask(pdev, 0xffffffffULL);
3415 if (rc)
3416 goto err_out_res;
3418 card = kmalloc (sizeof (*card), GFP_KERNEL);
3419 if (!card) {
3420 printk (KERN_ERR PFX "out of memory, aborting\n");
3421 rc = -ENOMEM;
3422 goto err_out_res;
3425 pci_set_drvdata (pdev, card);
3427 memset (card, 0, sizeof (*card));
3428 card->pdev = pdev;
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)
3444 card->rev_h = 1;
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);
3456 rc = -ENODEV;
3457 goto err_out_kfree;
3460 if (!(pci_resource_flags (pdev, 0) & IORESOURCE_IO)) {
3461 printk (KERN_ERR PFX "unable to locate I/O resources, aborting\n");
3462 rc = -ENODEV;
3463 goto err_out_kfree;
3466 pci_set_master(pdev);
3469 * init AC97 mixer and codec
3471 rc = via_ac97_init (card);
3472 if (rc) {
3473 printk (KERN_ERR PFX "AC97 init failed, aborting\n");
3474 goto err_out_kfree;
3478 * init DSP device
3480 rc = via_dsp_init (card);
3481 if (rc) {
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);
3490 if (rc) {
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);
3499 if (rc) {
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;
3511 if(card->legacy)
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 */
3522 u8 r43;
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");
3536 #endif
3538 DPRINTK ("EXIT, returning 0\n");
3539 return 0;
3541 err_out_have_proc:
3542 via_card_cleanup_proc (card);
3544 err_out_have_dsp:
3545 via_dsp_cleanup (card);
3547 err_out_have_mixer:
3548 via_ac97_cleanup (card);
3550 err_out_kfree:
3551 #ifndef VIA_NDEBUG
3552 memset (card, 0xAB, sizeof (*card)); /* poison memory */
3553 #endif
3554 kfree (card);
3556 err_out_res:
3557 pci_release_regions (pdev);
3559 err_out_disable:
3560 pci_disable_device (pdev);
3562 err_out:
3563 pci_set_drvdata (pdev, NULL);
3564 DPRINTK ("EXIT - returning %d\n", rc);
3565 return 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);
3582 #endif
3584 free_irq (card->pdev->irq, card);
3585 via_card_cleanup_proc (card);
3586 via_dsp_cleanup (card);
3587 via_ac97_cleanup (card);
3589 #ifndef VIA_NDEBUG
3590 memset (card, 0xAB, sizeof (*card)); /* poison memory */
3591 #endif
3592 kfree (card);
3594 pci_set_drvdata (pdev, NULL);
3596 pci_release_regions (pdev);
3597 pci_disable_device (pdev);
3598 pci_set_power_state (pdev, 3); /* ...zzzzzz */
3600 DPRINTK ("EXIT\n");
3601 return;
3605 /****************************************************************
3607 * Driver initialization and cleanup
3612 static int __init init_via82cxxx_audio(void)
3614 int rc;
3616 DPRINTK ("ENTER\n");
3618 rc = via_init_proc ();
3619 if (rc) {
3620 DPRINTK ("EXIT, returning %d\n", rc);
3621 return rc;
3624 rc = pci_register_driver (&via_driver);
3625 if (rc < 1) {
3626 if (rc == 0)
3627 pci_unregister_driver (&via_driver);
3628 via_cleanup_proc ();
3629 DPRINTK ("EXIT, returning -ENODEV\n");
3630 return -ENODEV;
3633 DPRINTK ("EXIT, returning 0\n");
3634 return 0;
3638 static void __exit cleanup_via82cxxx_audio(void)
3640 DPRINTK ("ENTER\n");
3642 pci_unregister_driver (&via_driver);
3643 via_cleanup_proc ();
3645 DPRINTK ("EXIT\n");
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");
3657 #ifdef VIA_PROC_FS
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")
3672 int len = 0;
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"
3689 "\n"
3690 "40 Codec Ready: %s\n"
3691 " Codec Low-power: %s\n"
3692 " Secondary Codec Ready: %s\n"
3693 "\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"
3702 "\n"
3703 "42 Game port enabled: %s\n"
3704 " SoundBlaster enabled: %s\n"
3705 " FM enabled: %s\n"
3706 " MIDI enabled: %s\n"
3707 "\n"
3708 "44 AC-Link Interface Access: %s\n"
3709 " Secondary Codec Support: %s\n"
3711 "\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),
3718 YN (r41, (1 << 6)),
3719 YN (r41, (1 << 5)),
3720 YN (r41, (1 << 4)),
3721 ED (r41, (1 << 3)),
3722 ED (r41, (1 << 2)),
3723 ED (r41, (1 << 1)),
3724 ED (r41, (1 << 0)),
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);
3737 return len;
3739 #undef YN
3740 #undef ED
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))
3756 return -EIO;
3758 DPRINTK ("EXIT, returning 0\n");
3759 return 0;
3763 static void via_cleanup_proc (void)
3765 DPRINTK ("ENTER\n");
3767 remove_proc_entry ("driver/via", NULL);
3769 DPRINTK ("EXIT\n");
3773 static int __devinit via_card_init_proc (struct via_info *card)
3775 char s[32];
3776 int rc;
3778 DPRINTK ("ENTER\n");
3780 sprintf (s, "driver/via/%d", card->card_num);
3781 if (!proc_mkdir (s, 0)) {
3782 rc = -EIO;
3783 goto err_out_none;
3786 sprintf (s, "driver/via/%d/info", card->card_num);
3787 if (!create_proc_read_entry (s, 0, 0, via_info_read_proc, card)) {
3788 rc = -EIO;
3789 goto err_out_dir;
3792 sprintf (s, "driver/via/%d/ac97", card->card_num);
3793 if (!create_proc_read_entry (s, 0, 0, ac97_read_proc, card->ac97)) {
3794 rc = -EIO;
3795 goto err_out_info;
3798 DPRINTK ("EXIT, returning 0\n");
3799 return 0;
3801 err_out_info:
3802 sprintf (s, "driver/via/%d/info", card->card_num);
3803 remove_proc_entry (s, NULL);
3805 err_out_dir:
3806 sprintf (s, "driver/via/%d", card->card_num);
3807 remove_proc_entry (s, NULL);
3809 err_out_none:
3810 DPRINTK ("EXIT, returning %d\n", rc);
3811 return rc;
3815 static void via_card_cleanup_proc (struct via_info *card)
3817 char s[32];
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);
3830 DPRINTK ("EXIT\n");
3833 #endif /* VIA_PROC_FS */