patch-2.4.0-test10 linux/drivers/sound/via82cxxx_audio.c

Next file: linux/drivers/sound/waveartist.c
Previous file: linux/drivers/sound/trident.h
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.4.0-test9/linux/drivers/sound/via82cxxx_audio.c linux/drivers/sound/via82cxxx_audio.c
@@ -13,15 +13,16 @@
  * http://gtf.org/garzik/drivers/via82cxxx/
  *
  */
- 
 
-#define VIA_VERSION	"1.1.8"
+
+#define VIA_VERSION	"1.1.14"
 
 
 #include <linux/config.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/fs.h>
+#include <linux/mm.h>
 #include <linux/pci.h>
 #include <linux/init.h>
 #include <linux/proc_fs.h>
@@ -31,10 +32,13 @@
 #include <linux/soundcard.h>
 #include <linux/ac97_codec.h>
 #include <linux/smp_lock.h>
+#include <linux/ioport.h>
+#include <linux/wrapper.h>
 #include <asm/io.h>
 #include <asm/delay.h>
 #include <asm/uaccess.h>
 #include <asm/hardirq.h>
+#include <asm/semaphore.h>
 
 
 #undef VIA_DEBUG	/* define to enable debugging output and checks */
@@ -45,7 +49,7 @@
 #define DPRINTK(fmt, args...)
 #endif
 
-#define VIA_NDEBUG	/* define to disable lightweight runtime checks */
+#undef VIA_NDEBUG	/* define to disable lightweight runtime checks */
 #ifdef VIA_NDEBUG
 #define assert(expr)
 #else
@@ -56,20 +60,15 @@
         }
 #endif
 
-/* user switch: undefine to exclude /proc data */
+#if defined(CONFIG_PROC_FS) && \
+    defined(CONFIG_SOUND_VIA82CXXX_PROCFS)
 #define VIA_PROC_FS 1
-
-/* don't mess with this */
-#ifndef CONFIG_PROC_FS
-#undef VIA_PROC_FS
 #endif
 
-#define arraysize(x)            (sizeof(x)/sizeof(*(x)))
+#define VIA_SUPPORT_MMAP 1 /* buggy, for now... */
 
 #define MAX_CARDS	1
 
-#define	LINE_SIZE	10
-
 #define VIA_CARD_NAME	"VIA 82Cxxx Audio driver " VIA_VERSION
 #define VIA_MODULE_NAME "via82cxxx"
 #define PFX		VIA_MODULE_NAME ": "
@@ -80,6 +79,10 @@
 #define VIA_DMA_BUFFERS		16
 #define VIA_DMA_BUF_SIZE	PAGE_SIZE
 
+#ifndef AC97_PCM_LR_ADC_RATE
+#  define AC97_PCM_LR_ADC_RATE AC97_PCM_LR_DAC_RATE
+#endif
+
 /* 82C686 function 5 (audio codec) PCI configuration registers */
 #define VIA_ACLINK_CTRL		0x41
 #define VIA_FUNC_ENABLE		0x42
@@ -121,6 +124,10 @@
 #define VIA_BASE0_AC97_CTRL		0x80
 #define VIA_BASE0_SGD_STATUS_SHADOW	0x84
 #define VIA_BASE0_GPI_INT_ENABLE	0x8C
+#define VIA_INTR_OUT			((1<<0) |  (1<<4) |  (1<<8))
+#define VIA_INTR_IN			((1<<1) |  (1<<5) |  (1<<9))
+#define VIA_INTR_FM			((1<<2) |  (1<<6) | (1<<10))
+#define VIA_INTR_MASK		(VIA_INTR_OUT | VIA_INTR_IN | VIA_INTR_FM)
 
 /* VIA_BASE0_AUDIO_xxx_CHAN_TYPE bits */
 #define VIA_IRQ_ON_FLAG			(1<<0)	/* int on each flagged scatter block */
@@ -130,11 +137,10 @@
 #define VIA_INT_SEL_ONE_LINE_LEFT	(1<<3)	/* int at less than one line to send */
 #define VIA_PCM_FMT_STEREO		(1<<4)	/* PCM stereo format (bit clear == mono) */
 #define VIA_PCM_FMT_16BIT		(1<<5)	/* PCM 16-bit format (bit clear == 8-bit) */
-#define VIA_PCM_FIFO			(1<<6)	/* enable FIFO?  documented as "reserved" */
+#define VIA_PCM_REC_FIFO		(1<<6)	/* PCM Recording FIFO */
 #define VIA_RESTART_SGD_ON_EOL		(1<<7)	/* restart scatter-gather at EOL */
 #define VIA_PCM_FMT_MASK		(VIA_PCM_FMT_STEREO|VIA_PCM_FMT_16BIT)
 #define VIA_CHAN_TYPE_MASK		(VIA_RESTART_SGD_ON_EOL | \
-					 VIA_PCM_FIFO | \
 					 VIA_IRQ_ON_FLAG | \
 					 VIA_IRQ_ON_EOL)
 #define VIA_CHAN_TYPE_INT_SELECT	(VIA_INT_SEL_LAST_SAMPLE_SENT)
@@ -191,25 +197,24 @@
 #define VIA_CR80_WRITE_MODE		0
 #define VIA_CR80_REG_IDX(idx)		((((idx) & 0xFF) >> 1) << 16)
 
-/* h r puff n stuff */
-#define VIA_FMT_STEREO	0x01
-#define VIA_FMT_16BIT	0x02
-#define VIA_FMT_MASK	0x03
-#define VIA_DAC_SHIFT	0   
-#define VIA_ADC_SHIFT	4
-
-/* undocumented(?) values for setting rate, from Via's source */
-#define VIA_SET_RATE_IN		0x00320000 /* set input rate */
-#define VIA_SET_RATE_OUT	0x002c0000 /* set output rate */
-
+/* capabilities we announce */
+#ifdef VIA_SUPPORT_MMAP
+#define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | DSP_CAP_MMAP | \
+		     DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
+#else
+#define VIA_DSP_CAP (DSP_CAP_REVISION | DSP_CAP_DUPLEX | \
+		     DSP_CAP_TRIGGER | DSP_CAP_REALTIME)
+#endif
 
 /* scatter-gather DMA table entry, exactly as passed to hardware */
 struct via_sgd_table {
 	u32 addr;
-	u32 count;	/* includes additional bits also */
+	u32 count;	/* includes additional VIA_xxx bits also */
 };
+
 #define VIA_EOL (1 << 31)
 #define VIA_FLAG (1 << 30)
+#define VIA_STOP (1 << 29)
 
 
 enum via_channel_states {
@@ -220,27 +225,36 @@
 
 struct via_sgd_data {
 	dma_addr_t handle;
-	volatile void *cpuaddr;
+	void *cpuaddr;
 };
 
 
 struct via_channel {
-	unsigned rate;		/* sample rate */
+	atomic_t n_bufs;
+	atomic_t hw_ptr;
+	wait_queue_head_t wait;
 
+	unsigned int sw_ptr;
+	unsigned int slop_len;
+	unsigned int n_irqs;
+	int bytes;
+
+	unsigned is_active : 1;
+	unsigned is_record : 1;
+	unsigned is_mapped : 1;
+	unsigned is_enabled : 1;
 	u8 pcm_fmt;		/* VIA_PCM_FMT_xxx */
-	
-	atomic_t state;
-	atomic_t buf_in_use;
-	atomic_t next_buf;
-	
+
+	unsigned rate;		/* sample rate */
+
 	volatile struct via_sgd_table *sgtable;
 	dma_addr_t sgt_handle;
-	
+
 	struct via_sgd_data sgbuf [VIA_DMA_BUFFERS];
-	
-	wait_queue_head_t wait;
-	
+
 	long iobase;
+
+	const char *name;
 };
 
 
@@ -248,20 +262,21 @@
 struct via_info {
 	struct pci_dev *pdev;
 	long baseaddr;
-	
+
 	struct ac97_codec ac97;
 	spinlock_t lock;
 	int card_num;		/* unique card number, from 0 */
 
 	int dev_dsp;		/* /dev/dsp index from register_sound_dsp() */
-	
+
 	unsigned rev_h : 1;
 
-	wait_queue_head_t open_wait;
-	int open_mode;
-	
+	struct semaphore syscall_sem;
+	struct semaphore open_sem;
+
 	struct via_channel ch_in;
 	struct via_channel ch_out;
+	struct via_channel ch_fm;
 };
 
 
@@ -269,6 +284,7 @@
 static unsigned via_num_cards = 0;
 
 
+
 /****************************************************************
  *
  * prototypes
@@ -285,6 +301,9 @@
 static int via_dsp_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
 static int via_dsp_open (struct inode *inode, struct file *file);
 static int via_dsp_release(struct inode *inode, struct file *file);
+#ifdef VIA_SUPPORT_MMAP
+static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma);
+#endif
 
 static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg);
 static void via_ac97_write_reg (struct ac97_codec *codec, u8 reg, u16 value);
@@ -292,8 +311,19 @@
 
 static void via_chan_free (struct via_info *card, struct via_channel *chan);
 static void via_chan_clear (struct via_channel *chan);
-static void via_chan_pcm_fmt (struct via_info *card,
-			      struct via_channel *chan, int reset);
+static void via_chan_pcm_fmt (struct via_channel *chan, int reset);
+
+#ifdef VIA_PROC_FS
+static int via_init_proc (void);
+static void via_cleanup_proc (void);
+static int via_card_init_proc (struct via_info *card);
+static void via_card_cleanup_proc (struct via_info *card);
+#else
+static inline int via_init_proc (void) { return 0; }
+static inline void via_cleanup_proc (void) {}
+static inline int via_card_init_proc (struct via_info *card) { return 0; }
+static inline void via_card_cleanup_proc (struct via_info *card) {}
+#endif
 
 
 /****************************************************************
@@ -338,7 +368,7 @@
  *	one of three PCM channels supported by the chip.
  *
  */
- 
+
 static inline void via_chan_stop (int iobase)
 {
 	if (inb (iobase + VIA_PCM_STATUS) & VIA_SGD_ACTIVE)
@@ -359,11 +389,11 @@
  *	one of three PCM channels supported by the chip.
  *
  */
- 
+
 static inline void via_chan_status_clear (int iobase)
 {
 	u8 tmp = inb (iobase + VIA_PCM_STATUS);
-	
+
 	if (tmp != 0)
 		outb (tmp, iobase + VIA_PCM_STATUS);
 }
@@ -376,70 +406,43 @@
  *	Start scatter-gather DMA for the given channel.
  *
  */
- 
+
 static inline void sg_begin (struct via_channel *chan)
 {
 	outb (VIA_SGD_START, chan->iobase + VIA_PCM_CONTROL);
 }
 
 
-/**
- *	via_chan_bufs_in_use - Number of buffers waiting to be consumed
- *	@chan: Channel for which DMA buffers will be counted
- *
- *	Count the number of buffers waiting to be consumed.  For a
- *	playback operation, this is the number of buffers which have
- *	yet to be sent to the DAC.  For a recording operation, this
- *	is the number of buffers waiting to be consumed by software
- *	calling read() system call.
+/****************************************************************
  *
- */
- 
-static inline int via_chan_bufs_in_use (struct via_channel *chan)
-{
-	return atomic_read(&chan->next_buf) -
-	       atomic_read(&chan->buf_in_use);
-}
-
-
-/**
- *	via_chan_full - Check for no-free-buffers condition
- *	@chan: Channel for which DMA full condition will be checked
+ * Miscellaneous debris
  *
- *	Count the number of buffers waiting to be consumed, and return
- *	true (non-zero) if no buffers are available to be filled on the
- *	given DMA channel.
  *
  */
- 
-static inline int via_chan_full (struct via_channel *chan)
-{
-	return (via_chan_bufs_in_use (chan) == VIA_DMA_BUFFERS);
-}
 
 
 /**
- *	via_chan_empty - Check for no-buffers-in-use condition
- *	@chan: Channel for which DMA empty condition will be checked
+ *	via_syscall_down - down the card-specific syscell semaphore
+ *	@card: Private info for specified board
+ *	@nonblock: boolean, non-zero if O_NONBLOCK is set
  *
- *	Count the number of buffers waiting to be consumed, and return
- *	true (non-zero) if no buffers are currently in use.
+ *	Encapsulates standard method of acquiring the syscall sem.
  *
+ *	Returns negative errno on error, or zero for success.
  */
- 
-static inline int via_chan_empty (struct via_channel *chan)
-{
-	return (atomic_read(&chan->next_buf) ==
-	        atomic_read(&chan->buf_in_use));
-}
 
+static inline int via_syscall_down (struct via_info *card, int nonblock)
+{
+	if (nonblock) {
+		if (down_trylock (&card->syscall_sem))
+			return -EAGAIN;
+	} else {
+		if (down_interruptible (&card->syscall_sem))
+			return -ERESTARTSYS;
+	}
 
-/****************************************************************
- *
- * Miscellaneous debris
- *
- *
- */
+	return 0;
+}
 
 
 /**
@@ -469,101 +472,115 @@
 	via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
 	via_chan_status_clear (card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
 	via_chan_status_clear (card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
-	
+
 	/*
 	 * clear any enabled interrupt bits, reset to 8-bit mono PCM mode
 	 */
-	outb (0, card->baseaddr + VIA_BASE0_PCM_OUT_CHAN);
-	outb (0, card->baseaddr + VIA_BASE0_PCM_IN_CHAN);
-	outb (0, card->baseaddr + VIA_BASE0_FM_OUT_CHAN);
+	outb (0, card->baseaddr + VIA_BASE0_PCM_OUT_CHAN_TYPE);
+	outb (0, card->baseaddr + VIA_BASE0_PCM_IN_CHAN_TYPE);
+	outb (0, card->baseaddr + VIA_BASE0_FM_OUT_CHAN_TYPE);
 	DPRINTK ("EXIT\n");
 }
 
 
 /**
  *	via_set_rate - Set PCM rate for given channel
- *	@card: Private info for specified board
+ *	@ac97: Pointer to generic codec info struct
+ *	@chan: Private info for specified channel
  *	@rate: Desired PCM sample rate, in Khz
- *	@inhale_deeply: Boolean.  If non-zero (true), the recording sample rate
- *			is set.  If zero (false), the playback sample rate
- *			is set.
  *
  *	Sets the PCM sample rate for a channel.
  *
  *	Values for @rate are clamped to a range of 4000 Khz through 48000 Khz,
  *	due to hardware constraints.
- *
- *	FIXME:  @inhale_deeply argument is ignored, and %AC97_PCM_FRONT_DAC_RATE
- *	is the only rate which is really set.  This needs to be fixed when
- *	recording support is added.
  */
 
-static int via_set_rate (struct via_info *card, unsigned rate, int inhale_deeply)
+static int via_set_rate (struct ac97_codec *ac97,
+			 struct via_channel *chan, unsigned rate)
 {
+	int rate_reg;
 
-	DPRINTK ("ENTER, rate = %d, inhale = %s\n",
-		 rate, inhale_deeply ? "yes" : "no");
+	DPRINTK ("ENTER, rate = %d\n", rate);
 
 	if (rate > 48000)		rate = 48000;
 	if (rate < 4000) 		rate = 4000;
-	
-	via_ac97_write_reg (&card->ac97, AC97_POWER_CONTROL,
-		(via_ac97_read_reg (&card->ac97, AC97_POWER_CONTROL) & ~0x0200) |
+
+	rate_reg = chan->is_record ? AC97_PCM_LR_ADC_RATE :
+			    AC97_PCM_FRONT_DAC_RATE;
+
+	via_ac97_write_reg (ac97, AC97_POWER_CONTROL,
+		(via_ac97_read_reg (ac97, AC97_POWER_CONTROL) & ~0x0200) |
 		0x0200);
-	via_ac97_write_reg (&card->ac97, AC97_PCM_FRONT_DAC_RATE, rate);
-	via_ac97_write_reg (&card->ac97, AC97_POWER_CONTROL,
-		via_ac97_read_reg (&card->ac97, AC97_POWER_CONTROL) & ~0x0200);
 
-	DPRINTK ("EXIT, returning 0\n");
-	return rate;
+	via_ac97_write_reg (ac97, rate_reg, rate);
+
+	via_ac97_write_reg (ac97, AC97_POWER_CONTROL,
+		via_ac97_read_reg (ac97, AC97_POWER_CONTROL) & ~0x0200);
+
+	udelay (10);
+
+	/* the hardware might return a value different than what we
+	 * passed to it, so read the rate value back from hardware
+	 * to see what we came up with
+	 */
+	chan->rate = via_ac97_read_reg (ac97, rate_reg);
+
+	DPRINTK ("EXIT, returning rate %d Hz\n", chan->rate);
+	return chan->rate;
 }
 
 
-/**
- *	via_set_adc_rate - Set PCM rate for recording channel
- *	@card: Private info for specified board
- *	@rate: Desired PCM sample rate, in Khz
+/****************************************************************
+ *
+ * Channel-specific operations
  *
- *	Sets the PCM sample rate for a recording channel.
  *
- *	FIXME:  @inhale_deeply argument to via_set_rate is ignored, and %AC97_PCM_FRONT_DAC_RATE
- *	is the only rate which is really set.  Thus, this function will
- *	not work until via_set_rate is fixed.
  */
 
-static inline int via_set_adc_rate (struct via_info *card, int rate)
-{
-	return via_set_rate (card, rate, 1);
-}
-
 
 /**
- *	via_set_dac_rate - Set PCM rate for playback channel
- *	@card: Private info for specified board
- *	@rate: Desired PCM sample rate, in Khz
+ *	via_chan_init_defaults - Initialize a struct via_channel
+ *	@card: Private audio chip info
+ *	@chan: Channel to be initialized
  *
- *	Sets the PCM sample rate for a playback channel.
+ *	Zero @chan, and then set all static defaults for the structure.
  */
 
-static inline int via_set_dac_rate (struct via_info *card, int rate)
+static void via_chan_init_defaults (struct via_info *card, struct via_channel *chan)
 {
-	return via_set_rate (card, rate, 0);
+	memset (chan, 0, sizeof (*chan));
+
+	if (chan == &card->ch_out) {
+		chan->name = "PCM-OUT";
+		chan->iobase = card->baseaddr + VIA_BASE0_PCM_OUT_CHAN;
+	} else if (chan == &card->ch_in) {
+		chan->name = "PCM-IN";
+		chan->iobase = card->baseaddr + VIA_BASE0_PCM_IN_CHAN;
+		chan->is_record = 1;
+	} else if (chan == &card->ch_fm) {
+		chan->name = "PCM-OUT-FM";
+		chan->iobase = card->baseaddr + VIA_BASE0_FM_OUT_CHAN;
+	} else {
+		BUG();
+	}
+
+	init_waitqueue_head (&chan->wait);
+
+	chan->pcm_fmt = VIA_PCM_FMT_MASK;
+	chan->is_enabled = 1;
+
+	if (chan->is_record)
+		atomic_set (&chan->n_bufs, 0);
+	else
+		atomic_set (&chan->n_bufs, VIA_DMA_BUFFERS);
+	atomic_set (&chan->hw_ptr, 0);
 }
 
 
-/****************************************************************
- *
- * Channel-specific operations
- *
- *
- */
- 
 /**
  *	via_chan_init - Initialize PCM channel
  *	@card: Private audio chip info
  *	@chan: Channel to be initialized
- *	@chan_ofs: Offset from PCI address, which determines the
- *		   set of SGD registers to use.
  *
  *	Performs all the preparations necessary to begin
  *	using a PCM channel.
@@ -579,15 +596,14 @@
  *	always "take" the address.
  */
 
-static int via_chan_init (struct via_info *card, 
-			  struct via_channel *chan, long chan_ofs)
+static int via_chan_init (struct via_info *card, struct via_channel *chan)
 {
 	int i;
-	unsigned long flags;
-	
+
 	DPRINTK ("ENTER\n");
 
-	memset (chan, 0, sizeof (*chan));
+	/* bzero channel structure, and init members to defaults */
+	via_chan_init_defaults (card, chan);
 
 	/* alloc DMA-able memory for scatter-gather table */
 	chan->sgtable = pci_alloc_consistent (card->pdev,
@@ -598,8 +614,8 @@
 		DPRINTK ("EXIT\n");
 		return -ENOMEM;
 	}
-	
-	memset ((void*)chan->sgtable, 0, 
+
+	memset ((void*)chan->sgtable, 0,
 		(sizeof (struct via_sgd_table) * VIA_DMA_BUFFERS));
 
 	/* alloc DMA-able memory for scatter-gather buffers */
@@ -628,33 +644,26 @@
 			 virt_to_phys(chan->sgbuf[i].cpuaddr),
 			 chan->sgbuf[i].cpuaddr);
 #endif
-	}
-
-	init_waitqueue_head (&chan->wait);
 
-	chan->pcm_fmt = VIA_PCM_FMT_MASK;
-	chan->iobase = card->baseaddr + chan_ofs;
-	
-	spin_lock_irqsave (&card->lock, flags);
+		assert ((VIA_DMA_BUF_SIZE % PAGE_SIZE) == 0);
+	}
 
 	/* stop any existing channel output */
 	via_chan_clear (chan);
 	via_chan_status_clear (chan->iobase);
-	via_chan_pcm_fmt (card, chan, 1);
-
-	spin_unlock_irqrestore (&card->lock, flags);
+	via_chan_pcm_fmt (chan, 1);
 
 	/* set location of DMA-able scatter-gather info table */
 	DPRINTK("outl (0x%X, 0x%04lX)\n",
 		cpu_to_le32 (chan->sgt_handle),
-		card->baseaddr + chan_ofs + VIA_PCM_TABLE_ADDR);
+		chan->iobase + VIA_PCM_TABLE_ADDR);
 
 	via_ac97_wait_idle (card);
 	outl (cpu_to_le32 (chan->sgt_handle),
-	      card->baseaddr + chan_ofs + VIA_PCM_TABLE_ADDR);
+	      chan->iobase + VIA_PCM_TABLE_ADDR);
 	udelay (20);
 	via_ac97_wait_idle (card);
-	
+
 	DPRINTK("inl (0x%lX) = %x\n",
 		chan->iobase + VIA_PCM_TABLE_ADDR,
 		inl(chan->iobase + VIA_PCM_TABLE_ADDR));
@@ -683,24 +692,23 @@
  *	back to a known state, and releasing any allocated
  *	sound buffers.
  */
- 
+
 static void via_chan_free (struct via_info *card, struct via_channel *chan)
 {
 	int i;
-	unsigned long flags;
-	
+
 	DPRINTK ("ENTER\n");
-	
+
 	synchronize_irq();
 
-	spin_lock_irqsave (&card->lock, flags);
+	spin_lock_irq (&card->lock);
 
 	/* stop any existing channel output */
 	via_chan_stop (chan->iobase);
 	via_chan_status_clear (chan->iobase);
-	via_chan_pcm_fmt (card, chan, 1);
+	via_chan_pcm_fmt (chan, 1);
 
-	spin_unlock_irqrestore (&card->lock, flags);
+	spin_unlock_irq (&card->lock);
 
 	/* zero location of DMA-able scatter-gather info table */
 	via_ac97_wait_idle(card);
@@ -708,15 +716,16 @@
 
 	for (i = 0; i < VIA_DMA_BUFFERS; i++)
 		if (chan->sgbuf[i].cpuaddr) {
+			assert ((VIA_DMA_BUF_SIZE % PAGE_SIZE) == 0);
 			pci_free_consistent (card->pdev, VIA_DMA_BUF_SIZE,
-					     (void*)chan->sgbuf[i].cpuaddr,
+					     chan->sgbuf[i].cpuaddr,
 					     chan->sgbuf[i].handle);
 			chan->sgbuf[i].cpuaddr = NULL;
 			chan->sgbuf[i].handle = 0;
 		}
 
 	if (chan->sgtable) {
-		pci_free_consistent (card->pdev, 
+		pci_free_consistent (card->pdev,
 			(sizeof (struct via_sgd_table) * VIA_DMA_BUFFERS),
 			(void*)chan->sgtable, chan->sgt_handle);
 		chan->sgtable = NULL;
@@ -728,7 +737,6 @@
 
 /**
  *	via_chan_pcm_fmt - Update PCM channel settings
- *	@card: Private audio chip info
  *	@chan: Channel to be updated
  *	@reset: Boolean.  If non-zero, channel will be reset
  *		to 8-bit mono mode.
@@ -741,25 +749,26 @@
  *	is set to the values stored in the channel
  *	information struct @chan.
  */
- 
-static void via_chan_pcm_fmt (struct via_info *card,
-			      struct via_channel *chan, int reset)
+
+static void via_chan_pcm_fmt (struct via_channel *chan, int reset)
 {
 	DPRINTK ("ENTER, pcm_fmt=0x%02X, reset=%s\n",
 		 chan->pcm_fmt, reset ? "yes" : "no");
 
-	assert (card != NULL);
 	assert (chan != NULL);
 
 	if (reset)
 		/* reset to 8-bit mono mode */
 		chan->pcm_fmt = 0;
-	
+
 	/* enable interrupts on FLAG and EOL */
 	chan->pcm_fmt |= VIA_CHAN_TYPE_MASK;
-	
+
+	/* if we are recording, enable recording fifo bit */
+	if (chan->is_record)
+		chan->pcm_fmt |= VIA_PCM_REC_FIFO;
 	/* set interrupt select bits where applicable (PCM & FM out channels) */
-	if (chan == &card->ch_out)
+	if (!chan->is_record)
 		chan->pcm_fmt |= VIA_CHAN_TYPE_INT_SELECT;
 
 	outb (chan->pcm_fmt, chan->iobase + 2);
@@ -780,10 +789,20 @@
 
 static void via_chan_clear (struct via_channel *chan)
 {
+	DPRINTK ("ENTER\n");
 	via_chan_stop (chan->iobase);
-	atomic_set (&chan->state, sgd_stopped);
-	atomic_set (&chan->buf_in_use, 0);
-	atomic_set (&chan->next_buf, 0);
+	chan->is_active = 0;
+	chan->is_mapped = 0;
+	chan->is_enabled = 1;
+	chan->slop_len = 0;
+	chan->sw_ptr = 0;
+	chan->n_irqs = 0;
+	atomic_set (&chan->hw_ptr, 0);
+	if (chan->is_record)
+		atomic_set (&chan->n_bufs, 0);
+	else
+		atomic_set (&chan->n_bufs, VIA_DMA_BUFFERS);
+	DPRINTK ("EXIT\n");
 }
 
 
@@ -809,8 +828,8 @@
 
 	via_chan_clear (chan);
 
-	val = via_set_rate (card, val, chan == &card->ch_in);	
-	
+	val = via_set_rate (&card->ac97, chan, val);
+
 	DPRINTK ("EXIT, returning %d\n", val);
 	return val;
 }
@@ -840,23 +859,30 @@
 		 "unknown");
 
 	via_chan_clear (chan);
-	
+
+	assert (val != AFMT_QUERY); /* this case is handled elsewhere */
+
 	switch (val) {
-	case AFMT_U8:
-		chan->pcm_fmt &= ~VIA_PCM_FMT_16BIT;
-		via_chan_pcm_fmt (card, chan, 0);
-		break;
 	case AFMT_S16_LE:
-		chan->pcm_fmt |= VIA_PCM_FMT_16BIT;
-		via_chan_pcm_fmt (card, chan, 0);
+		if ((chan->pcm_fmt & VIA_PCM_FMT_16BIT) == 0) {
+			chan->pcm_fmt |= VIA_PCM_FMT_16BIT;
+			via_chan_pcm_fmt (chan, 0);
+		}
 		break;
-	default:
-		printk (KERN_WARNING PFX "unknown AFMT\n");
-		val = -EINVAL;
+
+	case AFMT_U8:
+		if (chan->pcm_fmt & VIA_PCM_FMT_16BIT) {
+			chan->pcm_fmt &= ~VIA_PCM_FMT_16BIT;
+			via_chan_pcm_fmt (chan, 0);
+		}
 		break;
+
+	default:
+		DPRINTK ("unknown AFMT: 0x%X\n", val);
+		val = AFMT_S16_LE;
 	}
-	
-	DPRINTK ("EXIT, returning %d\n", val);
+
+	DPRINTK ("EXIT\n");
 	return val;
 }
 
@@ -882,19 +908,19 @@
 	DPRINTK ("ENTER, channels = %d\n", val);
 
 	via_chan_clear (chan);
-	
+
 	switch (val) {
 
 	/* mono */
 	case 1:
 		chan->pcm_fmt &= ~VIA_PCM_FMT_STEREO;
-		via_chan_pcm_fmt (card, chan, 0);
+		via_chan_pcm_fmt (chan, 0);
 		break;
 
 	/* stereo */
 	case 2:
 		chan->pcm_fmt |= VIA_PCM_FMT_STEREO;
-		via_chan_pcm_fmt (card, chan, 0);
+		via_chan_pcm_fmt (chan, 0);
 		break;
 
 	/* unknown */
@@ -903,13 +929,13 @@
 		val = -EINVAL;
 		break;
 	}
-	
+
 	DPRINTK ("EXIT, returning %d\n", val);
 	return val;
 }
 
 
-#if 0
+#ifdef VIA_CHAN_DUMP_BUFS
 /**
  *	via_chan_dump_bufs - Display DMA table contents
  *	@chan: Channel whose DMA table will be displayed
@@ -921,7 +947,7 @@
 static void via_chan_dump_bufs (struct via_channel *chan)
 {
 	int i;
-	
+
 	for (i = 0; i < VIA_DMA_BUFFERS; i++) {
 		DPRINTK ("#%02d: addr=%x, count=%u, flag=%d, eol=%d\n",
 			 i, chan->sgtable[i].addr,
@@ -931,9 +957,55 @@
 	}
 	DPRINTK ("buf_in_use = %d, nextbuf = %d\n",
 		 atomic_read (&chan->buf_in_use),
-		 atomic_read (&chan->next_buf));
+		 atomic_read (&chan->sw_ptr));
+}
+#endif /* VIA_CHAN_DUMP_BUFS */
+
+
+/**
+ *	via_chan_flush_frag - Flush partially-full playback buffer to hardware
+ *	@chan: Channel whose DMA table will be displayed
+ *
+ *	Flushes partially-full playback buffer to hardware.
+ */
+
+static void via_chan_flush_frag (struct via_channel *chan)
+{
+	DPRINTK ("ENTER\n");
+
+	assert (chan->slop_len > 0);
+
+	if (chan->sw_ptr == (VIA_DMA_BUFFERS - 1))
+		chan->sw_ptr = 0;
+	else
+		chan->sw_ptr++;
+
+	chan->slop_len = 0;
+
+	assert (atomic_read (&chan->n_bufs) > 0);
+	atomic_dec (&chan->n_bufs);
+
+	DPRINTK ("EXIT\n");
+}
+
+
+
+/**
+ *	via_chan_maybe_start - Initiate audio hardware DMA operation
+ *	@chan: Channel whose DMA is to be started
+ *
+ *	Initiate DMA operation, if the DMA engine for the given
+ *	channel @chan is not already active.
+ */
+
+static inline void via_chan_maybe_start (struct via_channel *chan)
+{
+	if (!chan->is_active && chan->is_enabled) {
+		chan->is_active = 1;
+		sg_begin (chan);
+		DPRINTK("starting channel %s\n", chan->name);
+	}
 }
-#endif
 
 
 /****************************************************************
@@ -942,7 +1014,7 @@
  *
  *
  */
- 
+
 /**
  *	via_ac97_wait_idle - Wait until AC97 codec is not busy
  *	@card: Private info for specified board
@@ -956,23 +1028,20 @@
 {
 	u8 tmp8;
 	int counter = VIA_COUNTER_LIMIT;
-	
+
 	DPRINTK ("ENTER/EXIT\n");
 
 	assert (card != NULL);
 	assert (card->pdev != NULL);
-	
+
 	do {
-		if (current->need_resched)
-			schedule ();
-		else
-			udelay (10);
+		udelay (15);
 
-		spin_lock_irq (&card->lock);
 		tmp8 = inb (card->baseaddr + 0x83);
-		spin_unlock_irq (&card->lock);
 	} while ((tmp8 & VIA_CR83_BUSY) && (counter-- > 0));
 
+	if (tmp8 & VIA_CR83_BUSY)
+		printk (KERN_WARNING PFX "timeout waiting on AC97 codec\n");
 	return tmp8;
 }
 
@@ -994,10 +1063,10 @@
 
 static u16 via_ac97_read_reg (struct ac97_codec *codec, u8 reg)
 {
-	u32 data;
+	unsigned long data;
 	struct via_info *card;
 	int counter;
-	
+
 	DPRINTK ("ENTER\n");
 
 	assert (codec != NULL);
@@ -1013,26 +1082,25 @@
 	for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
 		if (inl (card->baseaddr + 0x80) & VIA_CR80_VALID)
 			goto out;
-		udelay(10);
-		if (current->need_resched)
-			schedule ();
+
+		udelay (15);
 	}
 
-	printk (KERN_WARNING PFX "timeout while reading AC97 codec\n");
+	printk (KERN_WARNING PFX "timeout while reading AC97 codec (0x%lX)\n", data);
 	goto err_out;
 
 out:
-	data = inl (card->baseaddr + 0x80);
+	data = (unsigned long) inl (card->baseaddr + 0x80);
 	outb (0x02, card->baseaddr + 0x83);
 
 	if (((data & 0x007F0000) >> 16) == reg) {
-		DPRINTK ("EXIT, success, data=0x%x, retval=0x%x\n",
+		DPRINTK ("EXIT, success, data=0x%lx, retval=0x%lx\n",
 			 data, data & 0x0000FFFF);
 		return data & 0x0000FFFF;
 	}
 
-	DPRINTK ("WARNING: not our index: reg=0x%x, newreg=0x%x\n",
-		 reg, ((data & 0x007F0000) >> 16));
+	printk (KERN_WARNING "via82cxxx_audio: not our index: reg=0x%x, newreg=0x%lx\n",
+		reg, ((data & 0x007F0000) >> 16));
 
 err_out:
 	DPRINTK ("EXIT, returning 0\n");
@@ -1058,7 +1126,7 @@
 	u32 data;
 	struct via_info *card;
 	int counter;
-	
+
 	DPRINTK ("ENTER\n");
 
 	assert (codec != NULL);
@@ -1068,17 +1136,16 @@
 
 	data = (reg << 16) + value;
 	outl (data, card->baseaddr + VIA_BASE0_AC97_CTRL);
-	udelay (20);
+	udelay (10);
 
 	for (counter = VIA_COUNTER_LIMIT; counter > 0; counter--) {
 		if ((inb (card->baseaddr + 0x83) & VIA_CR83_BUSY) == 0)
 			goto out;
-		udelay(10);
-		if (current->need_resched)
-			schedule ();
+
+		udelay (15);
 	}
-	
-	printk (KERN_WARNING PFX "timeout after AC97 codec write\n");
+
+	printk (KERN_WARNING PFX "timeout after AC97 codec write (0x%X, 0x%X)\n", reg, value);
 
 out:
 	DPRINTK ("EXIT\n");
@@ -1091,15 +1158,15 @@
 	struct via_info *card;
 	struct pci_dev *pdev;
 	struct pci_driver *drvr;
-	
+
 	DPRINTK ("ENTER\n");
 
 	pci_for_each_dev(pdev) {
 		drvr = pci_dev_driver (pdev);
 		if (drvr == &via_driver) {
-			assert (pdev->driver_data != NULL);
-			
-			card = pdev->driver_data;
+			assert (pci_get_drvdata (pdev) != NULL);
+
+			card = pci_get_drvdata (pdev);
 			if (card->ac97.dev_mixer == minor)
 				goto match;
 		}
@@ -1119,12 +1186,25 @@
 			    unsigned long arg)
 {
 	struct ac97_codec *codec = file->private_data;
+	struct via_info *card;
+	int nonblock = (file->f_flags & O_NONBLOCK);
 	int rc;
 
+	DPRINTK ("ENTER\n");
+
 	assert (codec != NULL);
-	
+	card = codec->private_data;
+	assert (card != NULL);
+
+	rc = via_syscall_down (card, nonblock);
+	if (rc) goto out;
+
 	rc = codec->mixer_ioctl(codec, cmd, arg);
-	
+
+	up (&card->syscall_sem);
+
+out:
+	DPRINTK ("EXIT, returning %d\n", rc);
 	return rc;
 }
 
@@ -1146,39 +1226,16 @@
 };
 
 
-#if 0 /* values reasoned from debugging dumps of via's driver */
-static struct {
-	u8 reg;
-	u16 data;
-} mixer_init_vals[] __devinitdata = {
-	{ 0x2, 0x404 },
-	{ 0x4, 0x404 },
-	{ 0x6, 0x404 },
-	{ 0x18, 0x404 },
-	{ 0x10, 0x404 },
-	{ 0x1a, 0x404 },
-	{ 0x1c, 0x404 },
-	{ 0x1a, 0x404 },
-	{ 0x1c, 0xc0c },
-	{ 0x12, 0x808 },
-	{ 0x10, 0x808 },
-	{ 0xe, 0x2 },
-	{ 0x2, 0x808 },
-	{ 0x18, 0x808 },
-};
-#endif
-
-
 static int __init via_ac97_reset (struct via_info *card)
 {
 	struct pci_dev *pdev = card->pdev;
 	u8 tmp8;
 	u16 tmp16;
-	
+
 	DPRINTK ("ENTER\n");
 
 	assert (pdev != NULL);
-	
+
 #ifndef NDEBUG
 	{
 		u8 r40,r41,r42,r43,r44,r48;
@@ -1201,7 +1258,7 @@
 			 inl (card->baseaddr + 0x80),
 			 inl (card->baseaddr + 0x84));
 		spin_unlock_irq (&card->lock);
-		
+
 	}
 #endif
 
@@ -1212,10 +1269,10 @@
         pci_write_config_byte (pdev, VIA_ACLINK_CTRL, VIA_CR41_AC97_ENABLE |
                                VIA_CR41_AC97_RESET | VIA_CR41_AC97_WAKEUP);
         udelay (100);
- 
+
         pci_write_config_byte (pdev, VIA_ACLINK_CTRL, 0);
         udelay (100);
- 
+
         pci_write_config_byte (pdev, VIA_ACLINK_CTRL,
 			       VIA_CR41_AC97_ENABLE | VIA_CR41_PCM_ENABLE |
                                VIA_CR41_VRA | VIA_CR41_AC97_RESET);
@@ -1230,45 +1287,50 @@
 	/* route FM trap to IRQ, disable FM trap */
 	pci_write_config_byte (pdev, 0x48, 0x05);
 	udelay(10);
-	
+
 	/* disable all codec GPI interrupts */
 	outl (0, pci_resource_start (pdev, 0) + 0x8C);
 
-	/* enable variable rate */
+	/* WARNING: this line is magic.  Remove this
+	 * and things break. */
+	/* enable variable rate, variable rate MIC ADC */
 	tmp16 = via_ac97_read_reg (&card->ac97, 0x2A);
-	via_ac97_write_reg (&card->ac97, 0x2A, tmp16 | 0x01);
-	
-	/* boost headphone vol if disabled */
-	tmp16 = via_ac97_read_reg (&card->ac97, AC97_HEADPHONE_VOL);
-	if (tmp16 == 0)
-		via_ac97_write_reg (&card->ac97, AC97_HEADPHONE_VOL, 0x1F1F);
-	
+	via_ac97_write_reg (&card->ac97, 0x2A, tmp16 | (1<<0));
+
 	pci_read_config_byte (pdev, VIA_ACLINK_CTRL, &tmp8);
 	if ((tmp8 & (VIA_CR41_AC97_ENABLE | VIA_CR41_AC97_RESET)) == 0) {
 		printk (KERN_ERR PFX "cannot enable AC97 controller, aborting\n");
 		DPRINTK ("EXIT, tmp8=%X, returning -ENODEV\n", tmp8);
 		return -ENODEV;
 	}
-	
+
 	DPRINTK ("EXIT, returning 0\n");
 	return 0;
 }
 
 
+static void via_ac97_codec_wait (struct ac97_codec *codec)
+{
+	assert (codec->private_data != NULL);
+	via_ac97_wait_idle (codec->private_data);
+}
+
+
 static int __init via_ac97_init (struct via_info *card)
 {
 	int rc;
 	u16 tmp16;
-	
+
 	DPRINTK ("ENTER\n");
 
 	assert (card != NULL);
 
-	memset (&card->ac97, 0, sizeof (card->ac97));	
+	memset (&card->ac97, 0, sizeof (card->ac97));
 	card->ac97.private_data = card;
 	card->ac97.codec_read = via_ac97_read_reg;
 	card->ac97.codec_write = via_ac97_write_reg;
-	
+	card->ac97.codec_wait = via_ac97_codec_wait;
+
 	card->ac97.dev_mixer = register_sound_mixer (&via_mixer_fops, -1);
 	if (card->ac97.dev_mixer < 0) {
 		printk (KERN_ERR PFX "unable to register AC97 mixer, aborting\n");
@@ -1288,9 +1350,10 @@
 		goto err_out;
 	}
 
+	/* enable variable rate, variable rate MIC ADC */
 	tmp16 = via_ac97_read_reg (&card->ac97, 0x2A);
-	via_ac97_write_reg (&card->ac97, 0x2A, tmp16 | 0x01);
-	
+	via_ac97_write_reg (&card->ac97, 0x2A, tmp16 | (1<<0));
+
 	DPRINTK ("EXIT, returning 0\n");
 	return 0;
 
@@ -1304,10 +1367,10 @@
 static void via_ac97_cleanup (struct via_info *card)
 {
 	DPRINTK("ENTER\n");
-	
+
 	assert (card != NULL);
 	assert (card->ac97.dev_mixer >= 0);
-	
+
 	unregister_sound_mixer (card->ac97.dev_mixer);
 
 	DPRINTK("EXIT\n");
@@ -1320,68 +1383,130 @@
  * Interrupt-related code
  *
  */
- 
-static inline void via_interrupt_write (struct via_channel *chan)
+
+/**
+ *	via_intr_channel - handle an interrupt for a single channel
+ *	@chan: handle interrupt for this channel
+ *
+ *	This is the "meat" of the interrupt handler,
+ *	containing the actions taken each time an interrupt
+ *	occurs.  All communication and coordination with
+ *	userspace takes place here.
+ *
+ *	Locking: inside card->lock
+ */
+
+static void via_intr_channel (struct via_channel *chan)
 {
-	assert (atomic_read(&chan->buf_in_use) <
-		atomic_read(&chan->next_buf));
+	u8 status;
+	int n;
+
+	/* check pertinent bits of status register for action bits */
+	status = inb (chan->iobase) & (VIA_SGD_FLAG | VIA_SGD_EOL | VIA_SGD_STOPPED);
+	if (!status)
+		return;
+
+	/* acknowledge any flagged bits ASAP */
+	outb (status, chan->iobase);
+
+	/* grab current h/w ptr value */
+	n = atomic_read (&chan->hw_ptr);
+
+	/* sanity check: make sure our h/w ptr doesn't have a weird value */
+	assert (n >= 0);
+	assert (n < VIA_DMA_BUFFERS);
+
+	/* reset SGD data structure in memory to reflect a full buffer,
+	 * and advance the h/w ptr, wrapping around to zero if needed
+	 */
+	if (n == (VIA_DMA_BUFFERS - 1)) {
+		chan->sgtable[n].count = (VIA_DMA_BUF_SIZE | VIA_EOL);
+		atomic_set (&chan->hw_ptr, 0);
+	} else {
+		chan->sgtable[n].count = (VIA_DMA_BUF_SIZE | VIA_FLAG);
+		atomic_inc (&chan->hw_ptr);
+	}
 
-	/* XXX sanity check: read h/w counter to 
-	 * ensure no lost frames */
+	/* accounting crap for SNDCTL_DSP_GETxPTR */
+	chan->n_irqs++;
+	chan->bytes += VIA_DMA_BUF_SIZE;
+	if (chan->bytes < 0) /* handle overflow of 31-bit value */
+		chan->bytes = VIA_DMA_BUF_SIZE;
+
+	/* wake up anyone listening to see when interrupts occur */
+	if (waitqueue_active (&chan->wait))
+		wake_up_all (&chan->wait);
 
-	atomic_inc (&chan->buf_in_use);
+	DPRINTK ("%s intr, status=0x%02X, hwptr=0x%lX, chan->hw_ptr=%d\n",
+		 chan->name, status, (long) inl (chan->iobase + 0x04),
+		 atomic_read (&chan->hw_ptr));
+
+	/* all following checks only occur when not in mmap(2) mode */
+	if (chan->is_mapped)
+		return;
+
+	/* If we are recording, then n_bufs represents the number
+	 * of buffers waiting to be handled by userspace.
+	 * If we are playback, then n_bufs represents the number
+	 * of buffers remaining to be filled by userspace.
+	 * We increment here.  If we reach max buffers (VIA_DMA_BUFFERS),
+	 * this indicates an underrun/overrun.  For this case under OSS,
+	 * we stop the record/playback process.
+	 */
+	if (atomic_read (&chan->n_bufs) < VIA_DMA_BUFFERS)
+		atomic_inc (&chan->n_bufs);
+	assert (atomic_read (&chan->n_bufs) <= VIA_DMA_BUFFERS);
 
-	/* if SG ptr catches up with userland ptr, stop playback */
-	if (atomic_read(&chan->buf_in_use) == atomic_read(&chan->next_buf)) {
-		atomic_set (&chan->state, sgd_stopped);
+	if (atomic_read (&chan->n_bufs) == VIA_DMA_BUFFERS) {
+		chan->is_active = 0;
 		via_chan_stop (chan->iobase);
 	}
 
-	/* wake up anybody listening */
-	if (waitqueue_active (&chan->wait))
-		wake_up (&chan->wait);
+	DPRINTK ("%s intr, channel n_bufs == %d\n", chan->name,
+		 atomic_read (&chan->n_bufs));
 }
 
 
-
 static void via_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 {
 	struct via_info *card = dev_id;
-	struct via_channel *chan;
-	u8 status;
+	u32 status32;
 
-	status = inb (card->baseaddr + 0x00);
-	if (status) {
-		assert (card->open_mode & FMODE_WRITE);
-		
-		chan = &card->ch_out;
-		
-		if (status & VIA_SGD_FLAG) {
-			assert ((status & VIA_SGD_EOL) == 0);
-			outb (VIA_SGD_FLAG, chan->iobase + 0x00);
-			DPRINTK("FLAG intr, status=0x%02X\n", status);
-			via_interrupt_write (chan);
-		}
-		
-		if (status & VIA_SGD_EOL) {
-			assert ((status & VIA_SGD_FLAG) == 0);
-			outb (VIA_SGD_EOL, chan->iobase + 0x00);
-			DPRINTK("EOL intr, status=0x%02X\n", status);
-			via_interrupt_write (chan);
-		}
-		
-		if (status & VIA_SGD_STOPPED) {
-			outb (VIA_SGD_STOPPED, chan->iobase + 0x00);
-			DPRINTK("STOPPED intr, status=0x%02X\n", status);
-		}
+	/* to minimize interrupt sharing costs, we use the SGD status
+	 * shadow register to check the status of all inputs and
+	 * outputs with a single 32-bit bus read.  If no interrupt
+	 * conditions are flagged, we exit immediately
+	 */
+	status32 = inl (card->baseaddr + VIA_BASE0_SGD_STATUS_SHADOW);
+	if (!(status32 & VIA_INTR_MASK))
+		return;
 
-#if 0
-		via_chan_dump_bufs (&card->ch_out);
-#endif
-	}
+	DPRINTK ("intr, status32 == 0x%08X\n", status32);
+
+	/* synchronize interrupt handling under SMP.  this spinlock
+	 * goes away completely on UP
+	 */
+	spin_lock (&card->lock);
+
+	if (status32 & VIA_INTR_OUT)
+		via_intr_channel (&card->ch_out);
+	if (status32 & VIA_INTR_IN)
+		via_intr_channel (&card->ch_in);
+	if (status32 & VIA_INTR_FM)
+		via_intr_channel (&card->ch_fm);
+
+	spin_unlock (&card->lock);
 }
 
 
+/**
+ *	via_interrupt_disable - Disable all interrupt-generating sources
+ *	@card: Private info for specified board
+ *
+ *	Disables all interrupt-generation flags in the Via
+ *	audio hardware registers.
+ */
+
 static void via_interrupt_disable (struct via_info *card)
 {
 	u8 tmp8;
@@ -1390,7 +1515,7 @@
 	DPRINTK ("ENTER\n");
 
 	assert (card != NULL);
-	
+
 	spin_lock_irqsave (&card->lock, flags);
 
 	pci_read_config_byte (card->pdev, VIA_FM_NMI_CTRL, &tmp8);
@@ -1415,13 +1540,22 @@
 }
 
 
+/**
+ *	via_interrupt_init - Initialize interrupt handling
+ *	@card: Private info for specified board
+ *
+ *	Obtain and reserve IRQ for using in handling audio events.
+ *	Also, disable any IRQ-generating resources, to make sure
+ *	we don't get interrupts before we want them.
+ */
+
 static int via_interrupt_init (struct via_info *card)
 {
 	DPRINTK ("ENTER\n");
 
 	assert (card != NULL);
 	assert (card->pdev != NULL);
-	
+
 	/* check for sane IRQ number. can this ever happen? */
 	if (card->pdev->irq < 2) {
 		printk (KERN_ERR PFX "insane IRQ %d, aborting\n",
@@ -1429,7 +1563,7 @@
 		DPRINTK ("EXIT, returning -EIO\n");
 		return -EIO;
 	}
-	
+
 	if (request_irq (card->pdev->irq, via_interrupt, SA_SHIRQ, VIA_MODULE_NAME, card)) {
 		printk (KERN_ERR PFX "unable to obtain IRQ %d, aborting\n",
 			card->pdev->irq);
@@ -1445,6 +1579,15 @@
 }
 
 
+/**
+ *	via_interrupt_cleanup - Shutdown driver interrupt handling
+ *	@card: Private info for specified board
+ *
+ *	Disable any potential interrupt sources in the Via audio
+ *	hardware, and then release (un-reserve) the IRQ line
+ *	in the kernel core.
+ */
+
 static void via_interrupt_cleanup (struct via_info *card)
 {
 	DPRINTK ("ENTER\n");
@@ -1465,7 +1608,7 @@
  * OSS DSP device
  *
  */
- 
+
 static struct file_operations via_dsp_fops = {
 	owner:		THIS_MODULE,
 	open:		via_dsp_open,
@@ -1475,22 +1618,28 @@
 	poll:		via_dsp_poll,
 	llseek: 	via_llseek,
 	ioctl:		via_dsp_ioctl,
+#ifdef VIA_SUPPORT_MMAP
+	mmap:		via_dsp_mmap,
+#endif
 };
 
 
 static int __init via_dsp_init (struct via_info *card)
 {
 	u8 tmp8;
-	
+
 	DPRINTK ("ENTER\n");
 
 	assert (card != NULL);
 
 	/* turn off legacy features, if not already */
 	pci_read_config_byte (card->pdev, VIA_FUNC_ENABLE, &tmp8);
-	tmp8 &= ~(VIA_CR42_SB_ENABLE | VIA_CR42_MIDI_ENABLE |
-		  VIA_CR42_FM_ENABLE);
-	pci_write_config_byte (card->pdev, VIA_FUNC_ENABLE, tmp8);
+	if (tmp8 & (VIA_CR42_SB_ENABLE | VIA_CR42_MIDI_ENABLE |
+		    VIA_CR42_FM_ENABLE)) {
+		tmp8 &= ~(VIA_CR42_SB_ENABLE | VIA_CR42_MIDI_ENABLE |
+			  VIA_CR42_FM_ENABLE);
+		pci_write_config_byte (card->pdev, VIA_FUNC_ENABLE, tmp8);
+	}
 
 	via_stop_everything (card);
 
@@ -1519,232 +1668,413 @@
 }
 
 
-static ssize_t via_dsp_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
+#ifdef VIA_SUPPORT_MMAP
+static struct page * via_mm_nopage (struct vm_area_struct * vma,
+				    unsigned long address, int write_access)
+{
+	struct via_info *card = vma->vm_private_data;
+	struct via_channel *chan = &card->ch_out;
+	struct page *dmapage;
+	unsigned long pgoff;
+	int rd, wr;
+
+	DPRINTK ("ENTER, start %lXh, ofs %lXh, pgoff %ld, addr %lXh, wr %d\n",
+		 vma->vm_start,
+		 address - vma->vm_start,
+		 (address - vma->vm_start) >> PAGE_SHIFT,
+		 address,
+		 write_access);
+
+	assert (VIA_DMA_BUF_SIZE == PAGE_SIZE);
+
+        if (address > vma->vm_end) {
+		DPRINTK ("EXIT, returning NOPAGE_SIGBUS\n");
+		return NOPAGE_SIGBUS; /* Disallow mremap */
+	}
+        if (!card) {
+		DPRINTK ("EXIT, returning NOPAGE_OOM\n");
+		return NOPAGE_OOM;	/* Nothing allocated */
+	}
+
+	pgoff = vma->vm_pgoff + ((address - vma->vm_start) >> PAGE_SHIFT);
+	rd = card->ch_in.is_mapped;
+	wr = card->ch_out.is_mapped;
+
+#ifndef VIA_NDEBUG
+	{
+	unsigned long max_bufs = VIA_DMA_BUFFERS;
+	if (rd && wr) max_bufs *= 2;
+	/* via_dsp_mmap() should ensure this */
+	assert (pgoff < max_bufs);
+	}
+#endif
+
+	/* if full-duplex (read+write) and we have two sets of bufs,
+	 * then the playback buffers come first, sez soundcard.c */
+	if (pgoff >= VIA_DMA_BUFFERS) {
+		pgoff -= VIA_DMA_BUFFERS;
+		chan = &card->ch_in;
+	} else if (!wr)
+		chan = &card->ch_in;
+
+	assert ((((unsigned long)chan->sgbuf[pgoff].cpuaddr) % PAGE_SIZE) == 0);
+
+	dmapage = virt_to_page (chan->sgbuf[pgoff].cpuaddr);
+	DPRINTK ("EXIT, returning page %p for cpuaddr %lXh\n",
+		 dmapage, (unsigned long) chan->sgbuf[pgoff].cpuaddr);
+	get_page (dmapage);
+	return dmapage;
+}
+
+
+#ifndef VM_RESERVE
+static int via_mm_swapout (struct page *page, struct file *filp)
+{
+	return 0;
+}
+#endif /* VM_RESERVE */
+
+
+struct vm_operations_struct via_mm_ops = {
+	nopage:		via_mm_nopage,
+
+#ifndef VM_RESERVE
+	swapout:	via_mm_swapout,
+#endif
+};
+
+
+static int via_dsp_mmap(struct file *file, struct vm_area_struct *vma)
 {
 	struct via_info *card;
+	int nonblock = (file->f_flags & O_NONBLOCK);
+	int rc = -EINVAL, rd=0, wr=0;
+	unsigned long max_size, size, start, offset;
 
-	DPRINTK ("ENTER\n");
-	
 	assert (file != NULL);
-	assert (buffer != NULL);
+	assert (vma != NULL);
 	card = file->private_data;
 	assert (card != NULL);
 
-	DPRINTK("EXIT, returning -EINVAL\n");
-	return -EINVAL;
-}
+	DPRINTK ("ENTER, start %lXh, size %ld, pgoff %ld\n",
+		 vma->vm_start,
+		 vma->vm_end - vma->vm_start,
+		 vma->vm_pgoff);
 
+	assert (VIA_DMA_BUF_SIZE == PAGE_SIZE);
 
+	max_size = 0;
+	if (file->f_mode & FMODE_READ) {
+		rd = 1;
+		max_size += (VIA_DMA_BUFFERS * VIA_DMA_BUF_SIZE);
+	}
+	if (file->f_mode & FMODE_WRITE) {
+		wr = 1;
+		max_size += (VIA_DMA_BUFFERS * VIA_DMA_BUF_SIZE);
+	}
 
-#define sgcount(n) (sgtable[(n)].count & 0x00FFFFFF)
-#define NEXTBUF (atomic_read(&chan->next_buf) % VIA_DMA_BUFFERS)
-#define BUF_IN_USE (atomic_read(&chan->buf_in_use) % VIA_DMA_BUFFERS)
-#define STATE_STOPPED (atomic_read (state) == sgd_stopped)
-#define STATE_STARTED (atomic_read (state) == sgd_in_progress)
-static ssize_t via_dsp_do_write (struct via_info *card,
-				 const char *userbuf, size_t count,
-				 int non_blocking)
+	start = vma->vm_start;
+	offset = (vma->vm_pgoff << PAGE_SHIFT);
+	size = vma->vm_end - vma->vm_start;
+
+	/* some basic size/offset sanity checks */
+	if (size > max_size)
+		goto out;
+	if (offset > max_size - size)
+		goto out;
+
+	rc = via_syscall_down (card, nonblock);
+	if (rc) goto out;
+
+	vma->vm_ops = &via_mm_ops;
+	vma->vm_private_data = card;
+
+#ifdef VM_RESERVE
+	vma->vm_flags |= VM_RESERVE;
+#endif
+
+	if (rd)
+		card->ch_in.is_mapped = 1;
+	if (wr)
+		card->ch_out.is_mapped = 1;
+
+	up (&card->syscall_sem);
+	rc = 0;
+
+out:
+	DPRINTK("EXIT, returning %d\n", rc);
+	return rc;
+}
+#endif /* VIA_SUPPORT_MMAP */
+
+
+static ssize_t via_dsp_do_read (struct via_info *card,
+				char *userbuf, size_t count,
+				int nonblock)
 {
 	const char *orig_userbuf = userbuf;
-	struct via_channel *chan = &card->ch_out;
-	volatile struct via_sgd_table *sgtable = chan->sgtable;
-	atomic_t *state = &chan->state;
+	struct via_channel *chan = &card->ch_in;
 	size_t size;
-	int nextbuf, prevbuf, n, realcount;
-	ssize_t rc;
-	
-	while (count > 0) {
-		if (current->need_resched)
-			schedule ();
+	int n, tmp;
 
-		spin_lock_irq (&card->lock);
-		DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
-			 inb (card->baseaddr + 0x00),
-			 inb (card->baseaddr + 0x01),
-			 inb (card->baseaddr + 0x02),
-			 inl (card->baseaddr + 0x04),
-			 inl (card->baseaddr + 0x0C),
-			 inl (card->baseaddr + 0x80),
-			 inl (card->baseaddr + 0x84));
-		spin_unlock_irq (&card->lock);
-		
-		size = (count < VIA_DMA_BUF_SIZE) ? count : VIA_DMA_BUF_SIZE;
+	/* if SGD has not yet been started, start it */
+	via_chan_maybe_start (chan);
 
-		/* case 1: SGD not active, list is ours for the mangling */
+handle_one_block:
+	/* just to be a nice neighbor */
+	if (current->need_resched)
+		schedule ();
+
+	/* grab current channel software pointer.  In the case of
+	 * recording, this is pointing to the next buffer that
+	 * will receive data from the audio hardware.
+	 */
+	n = chan->sw_ptr;
 
-		if (STATE_STOPPED) {
-			DPRINTK ("case 1\n");
+	/* n_bufs represents the number of buffers waiting
+	 * to be copied to userland.  sleep until at least
+	 * one buffer has been read from the audio hardware.
+	 */
+	tmp = atomic_read (&chan->n_bufs);
+	assert (tmp >= 0);
+	assert (tmp <= VIA_DMA_BUFFERS);
+	while (tmp == 0) {
+		if (nonblock || !chan->is_active)
+			return -EAGAIN;
 
-			if (copy_from_user ((void*)chan->sgbuf[0].cpuaddr,
-					     userbuf, size))
-				return -EFAULT;
+		DPRINTK ("Sleeping on block %d\n", n);
+		interruptible_sleep_on (&chan->wait);
 
-			assert (sgtable[0].addr == cpu_to_le32 (chan->sgbuf[0].handle));
-			sgtable[0].count = size | VIA_FLAG;
+		if (signal_pending (current))
+			return -ERESTARTSYS;
 
-			atomic_set (state, sgd_in_progress);
-			atomic_set (&chan->buf_in_use, 0);
-			atomic_set (&chan->next_buf, 1);
-			
-			count -= size;
-			userbuf += size;
+		tmp = atomic_read (&chan->n_bufs);
+	}
 
-			spin_lock_irq (&card->lock);
-			sg_begin (chan);
-			spin_unlock_irq (&card->lock);
+	/* Now that we have a buffer we can read from, send
+	 * as much as sample data possible to userspace.
+	 */
+	while ((count > 0) && (chan->slop_len < VIA_DMA_BUF_SIZE)) {
+		size_t slop_left = VIA_DMA_BUF_SIZE - chan->slop_len;
 
-			continue;
-		}
+		size = (count < slop_left) ? count : slop_left;
+		if (copy_to_user (userbuf,
+				  chan->sgbuf[n].cpuaddr + chan->slop_len,
+				  size))
+			return -EFAULT;
 
-		nextbuf = NEXTBUF;
-		if (nextbuf)
-			prevbuf = nextbuf - 1;
-		else
-			prevbuf = VIA_DMA_BUFFERS - 1;
+		count -= size;
+		chan->slop_len += size;
+		userbuf += size;
+	}
 
-		/* case 2: if final buffer is (a) a fragment, and (b) not
-		 * currently being consumed by the SGD engine, then append
-		 * as much data as possible to the fragment. */
-
-		realcount = sgcount(prevbuf);
-		if (STATE_STARTED && (prevbuf != BUF_IN_USE) &&
-		    (realcount < VIA_DMA_BUF_SIZE)) {
-			DPRINTK ("case 2\n");
-			DPRINTK ("st=%d, fb=%d -- nb=%d, pb=%d, n=%d, rc=%d\n",
-				 atomic_read (state),
-				 BUF_IN_USE,
-				 nextbuf,
-				 prevbuf,
-				 prevbuf /* n */,
-				 realcount);
-
-			n = prevbuf;
-
-			if ((VIA_DMA_BUF_SIZE - realcount) < size)
-				size = VIA_DMA_BUF_SIZE - realcount;
-
-			if (copy_from_user ((void*)(chan->sgbuf[n].cpuaddr +
-					     realcount),
-					     userbuf, size))
-				return -EFAULT;
-
-			/* slack way to try and prevent races */
-			if (prevbuf == BUF_IN_USE || !STATE_STARTED)
-				continue;
-
-			assert (sgtable[n].addr == cpu_to_le32 (chan->sgbuf[n].handle));
-			if (n == (VIA_DMA_BUFFERS - 1))
-				sgtable[n].count = (realcount + size) | VIA_EOL;
-			else
-				sgtable[n].count = (realcount + size) | VIA_FLAG;
+	/* If we didn't copy the buffer completely to userspace,
+	 * stop now.
+	 */
+	if (chan->slop_len < VIA_DMA_BUF_SIZE)
+		goto out;
 
-			count -= size;
-			userbuf += size;
-			continue;
-		}
+	/*
+	 * If we get to this point, we copied one buffer completely
+	 * to userspace, give the buffer back to the hardware.
+	 */
 
-		/* case 3: if there are buffers left, use one
-		 * XXX needs more review for possible races */
+	/* advance channel software pointer to point to
+	 * the next buffer from which we will copy
+	 */
+	if (chan->sw_ptr == (VIA_DMA_BUFFERS - 1))
+		chan->sw_ptr = 0;
+	else
+		chan->sw_ptr++;
+
+	/* mark one less buffer waiting to be processed */
+	assert (atomic_read (&chan->n_bufs) > 0);
+	atomic_dec (&chan->n_bufs);
+
+	/* we are at a block boundary, there is no fragment data */
+	chan->slop_len = 0;
+
+	DPRINTK("Flushed block %u, sw_ptr now %u, n_bufs now %d\n",
+		n, chan->sw_ptr, atomic_read (&chan->n_bufs));
+
+	DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
+		 inb (card->baseaddr + 0x00),
+		 inb (card->baseaddr + 0x01),
+		 inb (card->baseaddr + 0x02),
+		 inl (card->baseaddr + 0x04),
+		 inl (card->baseaddr + 0x0C),
+		 inl (card->baseaddr + 0x80),
+		 inl (card->baseaddr + 0x84));
 
-		else if (STATE_STARTED && !via_chan_full (chan)) {
-			DPRINTK ("case 3\n");
-			DPRINTK ("st=%d, fb=%d -- nb=%d, pb=%d, n=%d\n",
-				 atomic_read (state),
-				 BUF_IN_USE,
-				 nextbuf,
-				 prevbuf,
-				 nextbuf /* n */);
+	if (count > 0)
+		goto handle_one_block;
 
-			n = nextbuf;
+out:
+	return userbuf - orig_userbuf;
+}
 
-			if (copy_from_user ((void*)chan->sgbuf[n].cpuaddr,
-					     userbuf, size))
-				return -EFAULT;
 
-			if (n == (VIA_DMA_BUFFERS - 1))
-				sgtable[n].count = size | VIA_EOL;
-			else
-				sgtable[n].count = size | VIA_FLAG;
+static ssize_t via_dsp_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
+{
+	struct via_info *card;
+	int nonblock = (file->f_flags & O_NONBLOCK);
+	int rc;
 
-			/* if SGD stopped during data copy or SG table update,
-			 * then loop back to the beginning without updating
-			 * any pointers.
-			 * ie. slack way to prevent race */
-			if (!STATE_STARTED)
-				continue;
-
-			atomic_inc (&chan->next_buf);
-
-			count -= size;
-			userbuf += size;
-			continue;
-		}
-
-		/* case 4, final SGT active case: no free buffers, wait for one */
-
-		else {
-			DPRINTK ("case 4\n");
-			DPRINTK ("st=%d, fb=%d -- nb=%d, pb=%d\n",
-				 atomic_read (state),
-				 BUF_IN_USE,
-				 nextbuf,
-				 prevbuf);
-
-			/* if playback stopped, no need to sleep */
-			if (!STATE_STARTED)
-				continue;
-			
-			/* if buffer free, no need to sleep */
-			if (!via_chan_full (chan))
-				continue;
-			
-			if (non_blocking) {
-				rc = userbuf - orig_userbuf;
-				if (rc == 0)
-					rc = -EAGAIN;
-				return rc;
-			}
+	DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
+		 file, buffer, count, ppos ? ((unsigned long)*ppos) : 0);
 
-			DPRINTK ("sleeping\n");
-			interruptible_sleep_on (&chan->wait);
-			if (signal_pending (current))
-				return -ERESTARTSYS;
-		}
+	assert (file != NULL);
+	assert (buffer != NULL);
+	card = file->private_data;
+	assert (card != NULL);
+
+	if (ppos != &file->f_pos) {
+		DPRINTK ("EXIT, returning -ESPIPE\n");
+		return -ESPIPE;
 	}
 
-#if 0
-	{
-		u8 r40,r41,r42,r43,r44,r48;
-		pci_read_config_byte (card->pdev, 0x40, &r40);
-		pci_read_config_byte (card->pdev, 0x41, &r41);
-		pci_read_config_byte (card->pdev, 0x42, &r42);
-		pci_read_config_byte (card->pdev, 0x43, &r43);
-		pci_read_config_byte (card->pdev, 0x44, &r44);
-		pci_read_config_byte (card->pdev, 0x48, &r48);
-		DPRINTK("PCI config: %02X %02X %02X %02X %02X %02X\n",
-			r40,r41,r42,r43,r44,r48);
+	rc = via_syscall_down (card, nonblock);
+	if (rc) goto out;
+
+	if (card->ch_in.is_mapped) {
+		rc = -ENXIO;
+		goto out_up;
 	}
-#endif
 
-	DPRINTK ("EXIT, returning %d\n",
-		 userbuf - orig_userbuf);
+	rc = via_dsp_do_read (card, buffer, count, nonblock);
+
+out_up:
+	up (&card->syscall_sem);
+out:
+	DPRINTK("EXIT, returning %ld\n",(long) rc);
+	return rc;
+}
+
+
+static ssize_t via_dsp_do_write (struct via_info *card,
+				 const char *userbuf, size_t count,
+				 int nonblock)
+{
+	const char *orig_userbuf = userbuf;
+	struct via_channel *chan = &card->ch_out;
+	volatile struct via_sgd_table *sgtable = chan->sgtable;
+	size_t size;
+	int n, tmp;
+
+handle_one_block:
+	/* just to be a nice neighbor */
+	if (current->need_resched)
+		schedule ();
+
+	/* grab current channel software pointer.  In the case of
+	 * playback, this is pointing to the next buffer that
+	 * should receive data from userland.
+	 */
+	n = chan->sw_ptr;
+
+	/* n_bufs represents the number of buffers remaining
+	 * to be filled by userspace.  Sleep until
+	 * at least one buffer is available for our use.
+	 */
+	tmp = atomic_read (&chan->n_bufs);
+	assert (tmp >= 0);
+	assert (tmp <= VIA_DMA_BUFFERS);
+	while (tmp == 0) {
+		if (nonblock || !chan->is_enabled)
+			return -EAGAIN;
+
+		DPRINTK ("Sleeping on block %d, tmp==%d, ir==%d\n", n, tmp, chan->is_record);
+		interruptible_sleep_on (&chan->wait);
+
+		if (signal_pending (current))
+			return -ERESTARTSYS;
+
+		tmp = atomic_read (&chan->n_bufs);
+	}
+
+	/* Now that we have a buffer we can write to, fill it up
+	 * as much as possible with data from userspace.
+	 */
+	while ((count > 0) && (chan->slop_len < VIA_DMA_BUF_SIZE)) {
+		size_t slop_left = VIA_DMA_BUF_SIZE - chan->slop_len;
+
+		size = (count < slop_left) ? count : slop_left;
+		if (copy_from_user (chan->sgbuf[n].cpuaddr + chan->slop_len,
+				    userbuf, size))
+			return -EFAULT;
+
+		count -= size;
+		chan->slop_len += size;
+		userbuf += size;
+	}
+
+	/* If we didn't fill up the buffer with data, stop now.
+	 * Put a 'stop' marker in the DMA table too, to tell the
+	 * audio hardware to stop if it gets here.
+	 */
+	if (chan->slop_len < VIA_DMA_BUF_SIZE) {
+		sgtable[n].count = cpu_to_le32 (chan->slop_len | VIA_EOL | VIA_STOP);
+		goto out;
+	}
+
+	/*
+	 * If we get to this point, we have filled a buffer with
+	 * audio data, flush the buffer to audio hardware.
+	 */
+
+	/* Record the true size for the audio hardware to notice */
+	if (n == (VIA_DMA_BUFFERS - 1))
+		sgtable[n].count = cpu_to_le32 (VIA_DMA_BUF_SIZE | VIA_EOL);
+	else
+		sgtable[n].count = cpu_to_le32 (VIA_DMA_BUF_SIZE | VIA_FLAG);
+
+	/* advance channel software pointer to point to
+	 * the next buffer we will fill with data
+	 */
+	if (chan->sw_ptr == (VIA_DMA_BUFFERS - 1))
+		chan->sw_ptr = 0;
+	else
+		chan->sw_ptr++;
+
+	/* mark one less buffer as being available for userspace consumption */
+	assert (atomic_read (&chan->n_bufs) > 0);
+	atomic_dec (&chan->n_bufs);
+
+	/* we are at a block boundary, there is no fragment data */
+	chan->slop_len = 0;
+
+	/* if SGD has not yet been started, start it */
+	via_chan_maybe_start (chan);
+
+	DPRINTK("Flushed block %u, sw_ptr now %u, n_bufs now %d\n",
+		n, chan->sw_ptr, atomic_read (&chan->n_bufs));
+
+	DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
+		 inb (card->baseaddr + 0x00),
+		 inb (card->baseaddr + 0x01),
+		 inb (card->baseaddr + 0x02),
+		 inl (card->baseaddr + 0x04),
+		 inl (card->baseaddr + 0x0C),
+		 inl (card->baseaddr + 0x80),
+		 inl (card->baseaddr + 0x84));
+
+	if (count > 0)
+		goto handle_one_block;
+
+out:
 	return userbuf - orig_userbuf;
 }
-#undef sgcount
-#undef NEXTBUF
-#undef BUF_IN_USE
-#undef STATE_STOPPED
-#undef STATE_STARTED
 
 
 static ssize_t via_dsp_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
 {
 	struct via_info *card;
 	ssize_t rc;
+	int nonblock = (file->f_flags & O_NONBLOCK);
 
 	DPRINTK ("ENTER, file=%p, buffer=%p, count=%u, ppos=%lu\n",
 		 file, buffer, count, ppos ? ((unsigned long)*ppos) : 0);
-	
+
 	assert (file != NULL);
 	assert (buffer != NULL);
 	card = file->private_data;
@@ -1755,8 +2085,19 @@
 		return -ESPIPE;
 	}
 
-	rc = via_dsp_do_write (card, buffer, count, (file->f_flags & O_NONBLOCK));
+	rc = via_syscall_down (card, nonblock);
+	if (rc) goto out;
+
+	if (card->ch_out.is_mapped) {
+		rc = -ENXIO;
+		goto out_up;
+	}
+
+	rc = via_dsp_do_write (card, buffer, count, nonblock);
 
+out_up:
+	up (&card->syscall_sem);
+out:
 	DPRINTK("EXIT, returning %ld\n",(long) rc);
 	return rc;
 }
@@ -1765,45 +2106,105 @@
 static unsigned int via_dsp_poll(struct file *file, struct poll_table_struct *wait)
 {
 	struct via_info *card;
-	unsigned int mask = 0;
+	unsigned int mask = 0, rd, wr;
 
 	DPRINTK ("ENTER\n");
 
 	assert (file != NULL);
-	assert (wait != NULL);
 	card = file->private_data;
 	assert (card != NULL);
 
-	if ((file->f_mode & FMODE_WRITE) &&
-	    (atomic_read (&card->ch_out.state) != sgd_stopped)) {
-		poll_wait(file, &card->ch_out.wait, wait);
-		
-		/* XXX is this correct */
-		if (atomic_read (&card->ch_out.buf_in_use) <
-		    atomic_read (&card->ch_out.next_buf))
-			mask |= POLLOUT | POLLWRNORM;
-	}
+	rd = (file->f_mode & FMODE_READ);
+	wr = (file->f_mode & FMODE_WRITE);
+
+	if (wr && (atomic_read (&card->ch_out.n_bufs) == 0)) {
+		assert (card->ch_out.is_active);
+                poll_wait(file, &card->ch_out.wait, wait);
+	}
+        if (rd) {
+		/* XXX is it ok, spec-wise, to start DMA here? */
+		via_chan_maybe_start (&card->ch_in);
+		if (atomic_read (&card->ch_in.n_bufs) == 0)
+	                poll_wait(file, &card->ch_in.wait, wait);
+	}
+
+	if (wr && (atomic_read (&card->ch_out.n_bufs) > 0))
+		mask |= POLLOUT | POLLWRNORM;
+	if (rd && (atomic_read (&card->ch_in.n_bufs) > 0))
+		mask |= POLLIN | POLLRDNORM;
 
 	DPRINTK("EXIT, returning %u\n", mask);
 	return mask;
 }
 
 
-static int via_dsp_drain_dac (struct via_info *card, int non_block)
+/**
+ *	via_dsp_drain_playback - sleep until all playback samples are flushed
+ *	@card: Private info for specified board
+ *	@chan: Channel to drain
+ *	@nonblock: boolean, non-zero if O_NONBLOCK is set
+ *
+ *	Sleeps until all playback has been flushed to the audio
+ *	hardware.
+ *
+ *	Locking: inside card->syscall_sem
+ */
+
+static int via_dsp_drain_playback (struct via_info *card,
+				   struct via_channel *chan, int nonblock)
 {
-	DPRINTK ("ENTER, non_block = %d\n", non_block);
+	DPRINTK ("ENTER, nonblock = %d\n", nonblock);
+
+	if (chan->slop_len > 0)
+		via_chan_flush_frag (chan);
+
+	if (atomic_read (&chan->n_bufs) == VIA_DMA_BUFFERS)
+		goto out;
+
+	via_chan_maybe_start (chan);
+
+	while (atomic_read (&chan->n_bufs) < VIA_DMA_BUFFERS) {
+		if (nonblock) {
+			DPRINTK ("EXIT, returning -EAGAIN\n");
+			return -EAGAIN;
+		}
+
+#ifdef VIA_DEBUG
+		{
+		u8 r40,r41,r42,r43,r44,r48;
+		pci_read_config_byte (card->pdev, 0x40, &r40);
+		pci_read_config_byte (card->pdev, 0x41, &r41);
+		pci_read_config_byte (card->pdev, 0x42, &r42);
+		pci_read_config_byte (card->pdev, 0x43, &r43);
+		pci_read_config_byte (card->pdev, 0x44, &r44);
+		pci_read_config_byte (card->pdev, 0x48, &r48);
+		DPRINTK("PCI config: %02X %02X %02X %02X %02X %02X\n",
+			r40,r41,r42,r43,r44,r48);
 
-	while (!via_chan_empty (&card->ch_out)) {
-		if (non_block) {
-			DPRINTK ("EXIT, returning -EBUSY\n");
-			return -EBUSY;
+		DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
+			 inb (card->baseaddr + 0x00),
+			 inb (card->baseaddr + 0x01),
+			 inb (card->baseaddr + 0x02),
+			 inl (card->baseaddr + 0x04),
+			 inl (card->baseaddr + 0x0C),
+			 inl (card->baseaddr + 0x80),
+			 inl (card->baseaddr + 0x84));
 		}
+
+		if (!chan->is_active)
+			printk (KERN_ERR "sleeping but not active\n");
+#endif
+
+		DPRINTK ("sleeping, nbufs=%d\n", atomic_read (&chan->n_bufs));
+		interruptible_sleep_on (&chan->wait);
+
 		if (signal_pending (current)) {
 			DPRINTK ("EXIT, returning -ERESTARTSYS\n");
 			return -ERESTARTSYS;
 		}
+	}
 
-#ifndef NDEBUG
+#ifdef VIA_DEBUG
 	{
 		u8 r40,r41,r42,r43,r44,r48;
 		pci_read_config_byte (card->pdev, 0x40, &r40);
@@ -1815,7 +2216,6 @@
 		DPRINTK("PCI config: %02X %02X %02X %02X %02X %02X\n",
 			r40,r41,r42,r43,r44,r48);
 
-		spin_lock_irq (&card->lock);
 		DPRINTK ("regs==%02X %02X %02X %08X %08X %08X %08X\n",
 			 inb (card->baseaddr + 0x00),
 			 inb (card->baseaddr + 0x01),
@@ -1824,39 +2224,49 @@
 			 inl (card->baseaddr + 0x0C),
 			 inl (card->baseaddr + 0x80),
 			 inl (card->baseaddr + 0x84));
-		spin_unlock_irq (&card->lock);
-		
+
+		DPRINTK ("final nbufs=%d\n", atomic_read (&chan->n_bufs));
 	}
 #endif
 
-		DPRINTK ("sleeping\n");		
-		interruptible_sleep_on (&card->ch_out.wait);
-	}
-	
-	DPRINTK ("EXIT\n");
+out:
+	DPRINTK ("EXIT, returning 0\n");
 	return 0;
 }
 
 
+/**
+ *	via_dsp_ioctl_space - get information about channel buffering
+ *	@card: Private info for specified board
+ *	@chan: pointer to channel-specific info
+ *	@arg: user buffer for returned information
+ *
+ *	Handles SNDCTL_DSP_GETISPACE and SNDCTL_DSP_GETOSPACE.
+ *
+ *	Locking: inside card->syscall_sem
+ */
+
 static int via_dsp_ioctl_space (struct via_info *card,
 				struct via_channel *chan,
 				void *arg)
 {
 	audio_buf_info info;
-	int n;
 
 	info.fragstotal = VIA_DMA_BUFFERS;
 	info.fragsize = VIA_DMA_BUF_SIZE;
 
-	/* number of full fragments we can read without blocking */
-	n = atomic_read (&chan->next_buf) - atomic_read (&chan->buf_in_use);
-	info.fragments = VIA_DMA_BUFFERS - n;
+	/* number of full fragments we can read/write without blocking */
+	info.fragments = atomic_read (&chan->n_bufs);
+
+	if ((chan->slop_len > 0) && (info.fragments > 0))
+		info.fragments--;
 
 	/* number of bytes that can be read or written immediately
-	 * without blocking.  FIXME: we are lazy and ignore partially-full
-	 * buffers.
+	 * without blocking.
 	 */
-	info.bytes = info.fragments * VIA_DMA_BUF_SIZE;
+	info.bytes = (info.fragments * VIA_DMA_BUF_SIZE);
+	if (chan->slop_len > 0)
+		info.bytes += VIA_DMA_BUF_SIZE - chan->slop_len;
 
 	DPRINTK ("EXIT, returning fragstotal=%d, fragsize=%d, fragments=%d, bytes=%d\n",
 		info.fragstotal,
@@ -1868,205 +2278,375 @@
 }
 
 
+/**
+ *	via_dsp_ioctl_ptr - get information about hardware buffer ptr
+ *	@card: Private info for specified board
+ *	@chan: pointer to channel-specific info
+ *	@arg: user buffer for returned information
+ *
+ *	Handles SNDCTL_DSP_GETIPTR and SNDCTL_DSP_GETOPTR.
+ *
+ *	Locking: inside card->syscall_sem
+ */
 
-static int via_dsp_ioctl (struct inode *inode, struct file *file,
-			  unsigned int cmd, unsigned long arg)
+static int via_dsp_ioctl_ptr (struct via_info *card,
+				struct via_channel *chan,
+				void *arg)
 {
-	int rc = -EINVAL, rd=0, wr=0, val=0;
-	struct via_info *card;
+	count_info info;
 
-	DPRINTK ("ENTER, cmd = 0x%08X\n", cmd);
+	spin_lock_irq (&card->lock);
 
-	assert (file != NULL);
-	card = file->private_data;
-	assert (card != NULL);
+	info.bytes = chan->bytes;
+	info.blocks = chan->n_irqs;
+	chan->n_irqs = 0;
+
+	spin_unlock_irq (&card->lock);
+
+	if (chan->is_active) {
+		unsigned long extra;
+		info.ptr = atomic_read (&chan->hw_ptr) * VIA_DMA_BUF_SIZE;
+		extra = VIA_DMA_BUF_SIZE - inl (chan->iobase + VIA_BASE0_PCM_OUT_BLOCK_COUNT);
+		info.ptr += extra;
+		info.bytes += extra;
+	} else {
+		info.ptr = 0;
+	}
 
-	if (file->f_mode & FMODE_WRITE)
-		wr = 1;
-	if (file->f_mode & FMODE_READ)
-		rd = 1;
-	
-	switch (cmd) {
+	DPRINTK ("EXIT, returning bytes=%d, blocks=%d, ptr=%d\n",
+		info.bytes,
+		info.blocks,
+		info.ptr);
 
-	/* OSS API version.  XXX unverified */		
-	case OSS_GETVERSION:
-		DPRINTK("EXIT, returning SOUND_VERSION\n");
-		return put_user (SOUND_VERSION, (int *)arg);
+	return copy_to_user (arg, &info, sizeof (info));
+}
 
-	/* list of supported PCM data formats */
-	case SNDCTL_DSP_GETFMTS:
-		DPRINTK("EXIT, returning AFMT U8|S16_LE\n");
-                return put_user (AFMT_U8 | AFMT_S16_LE, (int *)arg);
 
-	/* query or set current channel's PCM data format */
-	case SNDCTL_DSP_SETFMT:
-		if (get_user(val, (int *)arg))
-			return -EFAULT;
-		if (val != AFMT_QUERY) {
-			rc = 0;
+static int via_dsp_ioctl_trigger (struct via_channel *chan, int val)
+{
+	int enable, do_something;
+
+	if (chan->is_record)
+		enable = (val & PCM_ENABLE_INPUT);
+	else
+		enable = (val & PCM_ENABLE_OUTPUT);
+
+	if (!chan->is_enabled && enable) {
+		do_something = 1;
+	} else if (chan->is_enabled && !enable) {
+		do_something = -1;
+	} else {
+		do_something = 0;
+	}
+
+	DPRINTK ("enable=%d, do_something=%d\n",
+		 enable, do_something);
+
+	if (chan->is_active && do_something)
+		return -EINVAL;
+
+	if (do_something == 1) {
+		chan->is_enabled = 1;
+		via_chan_maybe_start (chan);
+		DPRINTK ("Triggering input\n");
+	}
+
+	else if (do_something == -1) {
+		chan->is_enabled = 0;
+		DPRINTK ("Setup input trigger\n");
+	}
+
+	return 0;
+}
+
+
+static int via_dsp_ioctl (struct inode *inode, struct file *file,
+			  unsigned int cmd, unsigned long arg)
+{
+	int rc, rd=0, wr=0, val=0;
+	struct via_info *card;
+	struct via_channel *chan;
+	int nonblock = (file->f_flags & O_NONBLOCK);
+
+	assert (file != NULL);
+	card = file->private_data;
+	assert (card != NULL);
+
+	if (file->f_mode & FMODE_WRITE)
+		wr = 1;
+	if (file->f_mode & FMODE_READ)
+		rd = 1;
+
+	rc = via_syscall_down (card, nonblock);
+	if (rc)
+		return rc;
+	rc = -EINVAL;
+
+	switch (cmd) {
+
+	/* OSS API version.  XXX unverified */
+	case OSS_GETVERSION:
+		DPRINTK("ioctl OSS_GETVERSION, EXIT, returning SOUND_VERSION\n");
+		rc = put_user (SOUND_VERSION, (int *)arg);
+		break;
+
+	/* list of supported PCM data formats */
+	case SNDCTL_DSP_GETFMTS:
+		DPRINTK("DSP_GETFMTS, EXIT, returning AFMT U8|S16_LE\n");
+                rc = put_user (AFMT_U8 | AFMT_S16_LE, (int *)arg);
+		break;
+
+	/* query or set current channel's PCM data format */
+	case SNDCTL_DSP_SETFMT:
+		if (get_user(val, (int *)arg)) {
+			rc = -EFAULT;
+			break;
+		}
+		DPRINTK("DSP_SETFMT, val==%d\n", val);
+		if (val != AFMT_QUERY) {
+			rc = 0;
 
-			spin_lock_irq (&card->lock);
 			if (rc == 0 && rd)
 				rc = via_chan_set_fmt (card, &card->ch_in, val);
 			if (rc == 0 && wr)
 				rc = via_chan_set_fmt (card, &card->ch_out, val);
-			spin_unlock_irq (&card->lock);
 
-			if (rc <= 0)
-				return rc ? rc : -EINVAL;
+			if (rc <= 0) {
+				if (rc == 0)
+					rc = -EINVAL;
+				break;
+			}
 			val = rc;
 		} else {
-			spin_lock_irq (&card->lock);
 			if ((rd && (card->ch_in.pcm_fmt & VIA_PCM_FMT_16BIT)) ||
 			    (wr && (card->ch_out.pcm_fmt & VIA_PCM_FMT_16BIT)))
 				val = AFMT_S16_LE;
 			else
 				val = AFMT_U8;
-			spin_unlock_irq (&card->lock);
 		}
 		DPRINTK("SETFMT EXIT, returning %d\n", val);
-                return put_user (val, (int *)arg);
+                rc = put_user (val, (int *)arg);
+		break;
 
 	/* query or set number of channels (1=mono, 2=stereo) */
         case SNDCTL_DSP_CHANNELS:
-		if (get_user(val, (int *)arg))
-			return -EFAULT;
+		if (get_user(val, (int *)arg)) {
+			rc = -EFAULT;
+			break;
+		}
+		DPRINTK("DSP_CHANNELS, val==%d\n", val);
 		if (val != 0) {
 			rc = 0;
-			spin_lock_irq (&card->lock);
 			if (rc == 0 && rd)
 				rc = via_chan_set_stereo (card, &card->ch_in, val);
 			if (rc == 0 && wr)
 				rc = via_chan_set_stereo (card, &card->ch_out, val);
-			spin_unlock_irq (&card->lock);
-			if (rc <= 0)
-				return rc ? rc : -EINVAL;
+			if (rc <= 0) {
+				if (rc == 0)
+					rc = -EINVAL;
+				break;
+			}
 			val = rc;
 		} else {
-			spin_lock_irq (&card->lock);
 			if ((rd && (card->ch_in.pcm_fmt & VIA_PCM_FMT_STEREO)) ||
 			    (wr && (card->ch_out.pcm_fmt & VIA_PCM_FMT_STEREO)))
 				val = 2;
 			else
 				val = 1;
-			spin_unlock_irq (&card->lock);
 		}
 		DPRINTK("CHANNELS EXIT, returning %d\n", val);
-                return put_user (val, (int *)arg);
-	
+                rc = put_user (val, (int *)arg);
+		break;
+
 	/* enable (val is not zero) or disable (val == 0) stereo */
         case SNDCTL_DSP_STEREO:
-		if (get_user(val, (int *)arg))
-			return -EFAULT;
+		if (get_user(val, (int *)arg)) {
+			rc = -EFAULT;
+			break;
+		}
+		DPRINTK("DSP_STEREO, val==%d\n", val);
 		rc = 0;
-		spin_lock_irq (&card->lock);
+
 		if (rc == 0 && rd)
 			rc = via_chan_set_stereo (card, &card->ch_in, val ? 2 : 1);
 		if (rc == 0 && wr)
 			rc = via_chan_set_stereo (card, &card->ch_out, val ? 2 : 1);
-		spin_unlock_irq (&card->lock);
-		if (rc <= 0)
-			return rc ? rc : -EINVAL;
+
+		if (rc <= 0) {
+			if (rc == 0)
+				rc = -EINVAL;
+			break;
+		}
 		DPRINTK("STEREO EXIT, returning %d\n", val);
-                return 0;
-	
+                rc = 0;
+		break;
+
 	/* query or set sampling rate */
         case SNDCTL_DSP_SPEED:
-		if (get_user(val, (int *)arg))
-			return -EFAULT;
-		if (val < 0)
-			return -EINVAL;
+		if (get_user(val, (int *)arg)) {
+			rc = -EFAULT;
+			break;
+		}
+		DPRINTK("DSP_SPEED, val==%d\n", val);
+		if (val < 0) {
+			rc = -EINVAL;
+			break;
+		}
 		if (val > 0) {
 			rc = 0;
-			spin_lock_irq (&card->lock);
+
 			if (rc == 0 && rd)
 				rc = via_chan_set_speed (card, &card->ch_in, val);
 			if (rc == 0 && wr)
 				rc = via_chan_set_speed (card, &card->ch_out, val);
-			spin_unlock_irq (&card->lock);
-			if (rc <= 0)
-				return rc ? rc : -EINVAL;
+
+			if (rc <= 0) {
+				if (rc == 0)
+					rc = -EINVAL;
+				break;
+			}
 			val = rc;
 		} else {
-			spin_lock_irq (&card->lock);
 			if (rd)
 				val = card->ch_in.rate;
 			else if (wr)
 				val = card->ch_out.rate;
 			else
 				val = 0;
-			spin_unlock_irq (&card->lock);
 		}
 		DPRINTK("SPEED EXIT, returning %d\n", val);
-                return put_user (val, (int *)arg);
-	
+                rc = put_user (val, (int *)arg);
+		break;
+
 	/* wait until all buffers have been played, and then stop device */
 	case SNDCTL_DSP_SYNC:
+		DPRINTK ("DSP_SYNC\n");
 		if (wr) {
-			DPRINTK("SYNC EXIT (after calling via_dsp_drain_dac)\n");
-			return via_dsp_drain_dac (card, file->f_flags & O_NONBLOCK);
+			DPRINTK("SYNC EXIT (after calling via_dsp_drain_playback)\n");
+			rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
 		}
 		break;
 
 	/* stop recording/playback immediately */
         case SNDCTL_DSP_RESET:
-		spin_lock_irq (&card->lock);
+		DPRINTK ("DSP_RESET\n");
 		if (rd) {
 			via_chan_clear (&card->ch_in);
-			via_chan_pcm_fmt (card, &card->ch_in, 1);
+			via_chan_pcm_fmt (&card->ch_in, 1);
 		}
 		if (wr) {
 			via_chan_clear (&card->ch_out);
-			via_chan_pcm_fmt (card, &card->ch_out, 1);
+			via_chan_pcm_fmt (&card->ch_out, 1);
 		}
-		spin_unlock_irq (&card->lock);
-		DPRINTK("RESET EXIT, returning 0\n");
-		return 0;
+
+		rc = 0;
+		break;
 
 	/* obtain bitmask of device capabilities, such as mmap, full duplex, etc. */
 	case SNDCTL_DSP_GETCAPS:
-		DPRINTK("GETCAPS EXIT\n");
-		return put_user(DSP_CAP_REVISION, (int *)arg);
-		
+		DPRINTK("DSP_GETCAPS\n");
+		rc = put_user(VIA_DSP_CAP, (int *)arg);
+		break;
+
 	/* obtain bitmask of device capabilities, such as mmap, full duplex, etc. */
 	case SNDCTL_DSP_GETBLKSIZE:
-		DPRINTK("GETBLKSIZE EXIT\n");
-		return put_user(VIA_DMA_BUF_SIZE, (int *)arg);
-		
+		DPRINTK("DSP_GETBLKSIZE\n");
+		rc = put_user(VIA_DMA_BUF_SIZE, (int *)arg);
+		break;
+
 	/* obtain information about input buffering */
 	case SNDCTL_DSP_GETISPACE:
-		DPRINTK("GETISPACE EXIT\n");
-		return via_dsp_ioctl_space (card, &card->ch_in, (void*) arg);
-	
+		DPRINTK("DSP_GETISPACE\n");
+		if (rd)
+			rc = via_dsp_ioctl_space (card, &card->ch_in, (void*) arg);
+		break;
+
 	/* obtain information about output buffering */
 	case SNDCTL_DSP_GETOSPACE:
-		DPRINTK("GETOSPACE EXIT\n");
-		return via_dsp_ioctl_space (card, &card->ch_out, (void*) arg);
+		DPRINTK("DSP_GETOSPACE\n");
+		if (wr)
+			rc = via_dsp_ioctl_space (card, &card->ch_out, (void*) arg);
+		break;
+
+	/* obtain information about input hardware pointer */
+	case SNDCTL_DSP_GETIPTR:
+		DPRINTK("DSP_GETIPTR\n");
+		if (rd)
+			rc = via_dsp_ioctl_ptr (card, &card->ch_in, (void*) arg);
+		break;
+
+	/* obtain information about output hardware pointer */
+	case SNDCTL_DSP_GETOPTR:
+		DPRINTK("DSP_GETOPTR\n");
+		if (wr)
+			rc = via_dsp_ioctl_ptr (card, &card->ch_out, (void*) arg);
+		break;
 
 	/* return number of bytes remaining to be played by DMA engine */
 	case SNDCTL_DSP_GETODELAY:
 		{
-		int n;
-		
-		n = atomic_read (&card->ch_out.next_buf) -
-		    atomic_read (&card->ch_out.buf_in_use);
-		assert (n >= 0);
-
-		if (n == 0)
-			val = 0;
-		else {
-			val = (n - 1) * VIA_DMA_BUF_SIZE;
-			val += inl (card->ch_out.iobase + VIA_BASE0_PCM_OUT_BLOCK_COUNT);
+		DPRINTK("DSP_GETODELAY\n");
+
+		chan = &card->ch_out;
+
+		if (!wr)
+			break;
+
+		val = VIA_DMA_BUFFERS - atomic_read (&chan->n_bufs);
+
+		if (val > 0) {
+			val *= VIA_DMA_BUF_SIZE;
+			val -= VIA_DMA_BUF_SIZE -
+			       inl (chan->iobase + VIA_BASE0_PCM_OUT_BLOCK_COUNT);
 		}
-		
+		val += chan->slop_len;
+
+		assert (val <= (VIA_DMA_BUF_SIZE * VIA_DMA_BUFFERS));
+
 		DPRINTK("GETODELAY EXIT, val = %d bytes\n", val);
-                return put_user (val, (int *)arg);
+                rc = put_user (val, (int *)arg);
+		break;
 		}
 
+	/* handle the quick-start of a channel,
+	 * or the notification that a quick-start will
+	 * occur in the future
+	 */
+	case SNDCTL_DSP_SETTRIGGER:
+		if (get_user(val, (int *)arg)) {
+			rc = -EFAULT;
+			break;
+		}
+		DPRINTK("DSP_SETTRIGGER, rd=%d, wr=%d, act=%d/%d, en=%d/%d\n",
+			rd, wr, card->ch_in.is_active, card->ch_out.is_active,
+			card->ch_in.is_enabled, card->ch_out.is_enabled);
+
+		rc = 0;
+
+		if (rd)
+			rc = via_dsp_ioctl_trigger (&card->ch_in, val);
+		if (!rc && wr)
+			rc = via_dsp_ioctl_trigger (&card->ch_out, val);
+
+		break;
+
+	/* Enable full duplex.  Since we do this as soon as we are opened
+	 * with O_RDWR, this is mainly a no-op that always returns success.
+	 */
+	case SNDCTL_DSP_SETDUPLEX:
+		DPRINTK("DSP_SETDUPLEX\n");
+		if (!rd || !wr)
+			break;
+		rc = 0;
+		break;
+
 	/* set fragment size.  implemented as a successful no-op for now */
 	case SNDCTL_DSP_SETFRAGMENT:
-		if (get_user(val, (int *)arg))
-			return -EFAULT;
+		if (get_user(val, (int *)arg)) {
+			rc = -EFAULT;
+			break;
+		}
+		DPRINTK("DSP_SETFRAGMENT, val==%d\n", val);
 
 		DPRINTK ("SNDCTL_DSP_SETFRAGMENT (fragshift==0x%04X (%d), maxfrags==0x%04X (%d))\n",
 			 val & 0xFFFF,
@@ -2075,19 +2655,29 @@
 			 (val >> 16) & 0xFFFF);
 
 		/* just to shut up some programs */
-		return 0;
+		rc = 0;
+		break;
 
-	/* inform device of an upcoming pause in input (or output).  not implemented */
+	/* inform device of an upcoming pause in input (or output). */
 	case SNDCTL_DSP_POST:
-		DPRINTK("POST EXIT (null ioctl, returning -EINVAL)\n");
-                return -EINVAL;
+		DPRINTK("DSP_POST\n");
+		if (wr) {
+			if (card->ch_out.slop_len > 0)
+				via_chan_flush_frag (&card->ch_out);
+			via_chan_maybe_start (&card->ch_out);
+		}
+
+		rc = 0;
+		break;
 
 	/* not implemented */
 	default:
-		DPRINTK ("unhandled ioctl\n");
+		DPRINTK ("unhandled ioctl, cmd==%u, arg==%p\n",
+			 cmd, (void*) arg);
 		break;
 	}
-		
+
+	up (&card->syscall_sem);
 	DPRINTK("EXIT, returning %d\n", rc);
 	return rc;
 }
@@ -2095,25 +2685,28 @@
 
 static int via_dsp_open (struct inode *inode, struct file *file)
 {
-	int open_mode, rc = -EINVAL, minor = MINOR(inode->i_rdev);
-	int got_read_chan = 0, is_busy;
+	int rc, minor = MINOR(inode->i_rdev);
+	int got_read_chan = 0;
 	struct via_info *card;
 	struct pci_dev *pdev;
+	struct via_channel *chan;
 	struct pci_driver *drvr;
-	unsigned long flags;
+	int nonblock = (file->f_flags & O_NONBLOCK);
 
 	DPRINTK ("ENTER, minor=%d, file->f_mode=0x%x\n", minor, file->f_mode);
-	
-	if (file->f_mode & FMODE_READ) /* no input ATM */
-		goto err_out;
+
+	if (!(file->f_mode & (FMODE_READ | FMODE_WRITE))) {
+		DPRINTK ("EXIT, returning -EINVAL\n");
+		return -EINVAL;
+	}
 
 	card = NULL;
 	pci_for_each_dev(pdev) {
 		drvr = pci_dev_driver (pdev);
 		if (drvr == &via_driver) {
-			assert (pdev->driver_data != NULL);
-			
-			card = pdev->driver_data;	
+			assert (pci_get_drvdata (pdev) != NULL);
+
+			card = pci_get_drvdata (pdev);
 			DPRINTK ("dev_dsp = %d, minor = %d, assn = %d\n",
 				 card->dev_dsp, minor,
 				 (card->dev_dsp ^ minor) & ~0xf);
@@ -2122,299 +2715,109 @@
 				goto match;
 		}
 	}
-	
+
 	DPRINTK ("no matching %s found\n", card ? "minor" : "driver");
-	rc = -ENODEV;
-	goto err_out;
+	return -ENODEV;
 
 match:
-	file->private_data = card;
-
-	/* wait for device to become free */
-	spin_lock_irqsave (&card->lock, flags);
-	open_mode = card->open_mode;
-	if (open_mode & file->f_mode)
-		is_busy = 1;
-	else {
-		is_busy = 0;
-		card->open_mode |= file->f_mode;
-		open_mode = card->open_mode;
-	}
-	spin_unlock_irqrestore (&card->lock, flags);
-	if (is_busy) {
-		rc = -EBUSY;
-		goto err_out;
+	if (nonblock) {
+		if (down_trylock (&card->open_sem)) {
+			DPRINTK ("EXIT, returning -EAGAIN\n");
+			return -EAGAIN;
+		}
+	} else {
+		if (down_interruptible (&card->open_sem)) {
+			DPRINTK ("EXIT, returning -ERESTARTSYS\n");
+			return -ERESTARTSYS;
+		}
 	}
 
-	DPRINTK("open_mode now 0x%x\n", open_mode);
+	file->private_data = card;
+	DPRINTK("file->f_mode == 0x%x\n", file->f_mode);
 
 	/* handle input from analog source */
 	if (file->f_mode & FMODE_READ) {
-		rc = via_chan_init (card, &card->ch_in, 0x10);
+		chan = &card->ch_in;
+
+		rc = via_chan_init (card, chan);
 		if (rc)
-			goto err_out_clear_mode;
-			
+			goto err_out;
+
 		got_read_chan = 1;
-		
-		/* why is this forced to 16-bit stereo in all drivers? */
-		card->ch_in.pcm_fmt =
-			VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
 
-		spin_lock_irqsave (&card->lock, flags);
-		via_chan_pcm_fmt (card, &card->ch_out, 0);
-		spin_unlock_irqrestore (&card->lock, flags);
+		/* why is this forced to 16-bit stereo in all drivers? */
+		chan->pcm_fmt = VIA_PCM_FMT_16BIT | VIA_PCM_FMT_STEREO;
 
-		via_set_adc_rate (card, 8000);
+		via_chan_pcm_fmt (chan, 0);
+		via_set_rate (&card->ac97, chan, 44100);
 	}
 
 	/* handle output to analog source */
 	if (file->f_mode & FMODE_WRITE) {
-		rc = via_chan_init (card, &card->ch_out, 0x00);
-		if (rc)
-			goto err_out_clear_mode;
-		
-		if ((minor & 0xf) == SND_DEV_DSP16)
-			card->ch_out.pcm_fmt |= VIA_PCM_FMT_16BIT;
-
-		spin_lock_irqsave (&card->lock, flags);
-		via_chan_pcm_fmt (card, &card->ch_out, 0);
-		spin_unlock_irqrestore (&card->lock, flags);
-
-		via_set_dac_rate (card, 8000);
-	}
-
-	DPRINTK ("EXIT, returning 0\n");
-	return 0;
-
-err_out_clear_mode:
-	if (got_read_chan)
-		via_chan_free (card, &card->ch_in);
-	spin_lock_irqsave (&card->lock, flags);
-	card->open_mode &= ~file->f_mode;
-	spin_unlock_irqrestore (&card->lock, flags);
-err_out:
-	DPRINTK("ERROR EXIT, returning %d\n", rc);
-	return rc;
-}
-
-
-static int via_dsp_release(struct inode *inode, struct file *file)
-{
-	struct via_info *card;
-	unsigned long flags;
-
-	DPRINTK ("ENTER\n");
-	
-	assert (file != NULL);
-	card = file->private_data;
-	assert (card != NULL);
-	
-	lock_kernel();
-	if (file->f_mode & FMODE_READ)
-		via_chan_free (card, &card->ch_in);
-
-	if (file->f_mode & FMODE_WRITE) {
-		via_dsp_drain_dac (card, file->f_flags & O_NONBLOCK);
-		via_chan_free (card, &card->ch_out);
-	}
-			
-	spin_lock_irqsave (&card->lock, flags);
-	card->open_mode &= ~(file->f_mode);
-	spin_unlock_irqrestore (&card->lock, flags);
-
- 	wake_up (&card->open_wait);
-	unlock_kernel();
-
-	DPRINTK("EXIT, returning 0\n");
-	return 0;
-}
-
-
-#ifdef VIA_PROC_FS
-
-/****************************************************************
- *
- * /proc/driver/via/info
- *
- *
- */
-
-static int via_info_read_proc (char *page, char **start, off_t off,
-			       int count, int *eof, void *data)
-{
-#define YN(val,bit) (((val) & (bit)) ? "yes" : "no")
-#define ED(val,bit) (((val) & (bit)) ? "enable" : "disable")
-
-	int len = 0;
-	u8 r40, r41, r42, r44;
-	struct via_info *card = data;
-	
-	DPRINTK ("ENTER\n");
-
-	assert (card != NULL);
-	
-	len += sprintf (page+len, VIA_CARD_NAME "\n\n");
-	
-	pci_read_config_byte (card->pdev, 0x40, &r40);
-	pci_read_config_byte (card->pdev, 0x41, &r41);
-	pci_read_config_byte (card->pdev, 0x42, &r42);
-	pci_read_config_byte (card->pdev, 0x44, &r44);
-	
-	len += sprintf (page+len,
-		"Via 82Cxxx PCI registers:\n"
-		"\n"
-		"40  Codec Ready: %s\n"
-		"    Codec Low-power: %s\n"
-		"    Secondary Codec Ready: %s\n"
-		"\n"
-		"41  Interface Enable: %s\n"
-		"    De-Assert Reset: %s\n"
-		"    Force SYNC high: %s\n"
-		"    Force SDO high: %s\n"
-		"    Variable Sample Rate On-Demand Mode: %s\n"
-		"    SGD Read Channel PCM Data Out: %s\n"
-		"    FM Channel PCM Data Out: %s\n"
-		"    SB PCM Data Out: %s\n"
-		"\n"
-		"42  Game port enabled: %s\n"
-		"    SoundBlaster enabled: %s\n"
-		"    FM enabled: %s\n"
-		"    MIDI enabled: %s\n"
-		"\n"	
-		"44  AC-Link Interface Access: %s\n"
-		"    Secondary Codec Support: %s\n"
-			
-		"\n",
-			
-		YN (r40, VIA_CR40_AC97_READY),
-		YN (r40, VIA_CR40_AC97_LOW_POWER),
-		YN (r40, VIA_CR40_SECONDARY_READY),
-
-		ED (r41, VIA_CR41_AC97_ENABLE),
-		YN (r41, (1 << 6)),
-		YN (r41, (1 << 5)),
-		YN (r41, (1 << 4)),
-		ED (r41, (1 << 3)),
-		ED (r41, (1 << 2)),
-		ED (r41, (1 << 1)),
-		ED (r41, (1 << 0)),
-
-		YN (r42, VIA_CR42_GAME_ENABLE),
-		YN (r42, VIA_CR42_SB_ENABLE),
-		YN (r42, VIA_CR42_FM_ENABLE),
-		YN (r42, VIA_CR42_MIDI_ENABLE),
-
-		YN (r44, VIA_CR44_AC_LINK_ACCESS),
-		YN (r44, VIA_CR44_SECOND_CODEC_SUPPORT)
-			
-		);
-
-	DPRINTK("EXIT, returning %d\n", len);
-	return len;
-
-#undef YN
-#undef ED
-}
-
-
-/****************************************************************
- *
- * /proc/driver/via/... setup and cleanup
- *
- *
- */
-
-static int __init via_init_proc (void)
-{
-	DPRINTK ("ENTER\n");
-
-	if (!proc_mkdir ("driver/via", 0))
-		return -EIO;
-
-	DPRINTK ("EXIT, returning 0\n");
-	return 0;
-}
-
-
-static void via_cleanup_proc (void)
-{
-	DPRINTK ("ENTER\n");
-
-	remove_proc_entry ("driver/via", NULL);
-
-	DPRINTK ("EXIT\n");
-}
-
-
-static int __init via_card_init_proc (struct via_info *card)
-{
-	char s[32];
-	int rc;
-	
-	DPRINTK ("ENTER\n");
-
-	sprintf (s, "driver/via/%d", card->card_num);
-	if (!proc_mkdir (s, 0)) {
-		rc = -EIO;
-		goto err_out_none;
-	}
-	
-	sprintf (s, "driver/via/%d/info", card->card_num);
-	if (!create_proc_read_entry (s, 0, 0, via_info_read_proc, card)) {
-		rc = -EIO;
-		goto err_out_dir;
-	}
+		chan = &card->ch_out;
 
-	sprintf (s, "driver/via/%d/ac97", card->card_num);
-	if (!create_proc_read_entry (s, 0, 0, ac97_read_proc, &card->ac97)) {
-		rc = -EIO;
-		goto err_out_info;
+		rc = via_chan_init (card, chan);
+		if (rc)
+			goto err_out_read_chan;
+
+		if ((minor & 0xf) == SND_DEV_DSP16) {
+			chan->pcm_fmt |= VIA_PCM_FMT_16BIT;
+			via_set_rate (&card->ac97, chan, 44100);
+		} else {
+			via_set_rate (&card->ac97, chan, 8000);
+		}
+
+		via_chan_pcm_fmt (chan, 0);
 	}
 
 	DPRINTK ("EXIT, returning 0\n");
 	return 0;
 
-err_out_info:
-	sprintf (s, "driver/via/%d/info", card->card_num);
-	remove_proc_entry (s, NULL);
-
-err_out_dir:
-	sprintf (s, "driver/via/%d", card->card_num);
-	remove_proc_entry (s, NULL);
-
-err_out_none:
-	DPRINTK ("EXIT, returning %d\n", rc);
+err_out_read_chan:
+	if (got_read_chan)
+		via_chan_free (card, &card->ch_in);
+err_out:
+	up (&card->open_sem);
+	DPRINTK("ERROR EXIT, returning %d\n", rc);
 	return rc;
 }
 
 
-static void via_card_cleanup_proc (struct via_info *card)
+static int via_dsp_release(struct inode *inode, struct file *file)
 {
-	char s[32];
+	struct via_info *card;
+	int nonblock = (file->f_flags & O_NONBLOCK);
+	int rc;
 
 	DPRINTK ("ENTER\n");
 
-	sprintf (s, "driver/via/%d/ac97", card->card_num);
-	remove_proc_entry (s, NULL);
-
-	sprintf (s, "driver/via/%d/info", card->card_num);
-	remove_proc_entry (s, NULL);
+	assert (file != NULL);
+	card = file->private_data;
+	assert (card != NULL);
 
-	sprintf (s, "driver/via/%d", card->card_num);
-	remove_proc_entry (s, NULL);
+	rc = via_syscall_down (card, nonblock);
+	if (rc) {
+		DPRINTK ("EXIT (syscall_down error), rc=%d\n", rc);
+		return rc;
+	}
 
-	DPRINTK ("EXIT\n");
-}
+	if (file->f_mode & FMODE_WRITE) {
+		rc = via_dsp_drain_playback (card, &card->ch_out, nonblock);
+		if (rc)
+			printk (KERN_DEBUG "via_audio: ignoring drain playback error %d\n", rc);
 
+		via_chan_free (card, &card->ch_out);
+	}
 
-#else
+	if (file->f_mode & FMODE_READ)
+		via_chan_free (card, &card->ch_in);
 
-static inline int via_init_proc (void) { return 0; }
-static inline void via_cleanup_proc (void) {}
-static inline int via_card_init_proc (struct via_info *card) { return 0; }
-static inline void via_card_cleanup_proc (struct via_info *card) {}
+	up (&card->syscall_sem);
+	up (&card->open_sem);
 
-#endif /* VIA_PROC_FS */
+	DPRINTK("EXIT, returning 0\n");
+	return 0;
+}
 
 
 /****************************************************************
@@ -2430,9 +2833,9 @@
 	struct via_info *card;
 	u8 tmp;
 	static int printed_version = 0;
-	
+
 	DPRINTK ("ENTER\n");
-	
+
 	if (printed_version++ == 0)
 		printk (KERN_INFO "Via 686a audio driver " VIA_VERSION "\n");
 
@@ -2448,7 +2851,7 @@
 		rc = -EIO;
 		goto err_out_none;
 	}
-	
+
 	card = kmalloc (sizeof (*card), GFP_KERNEL);
 	if (!card) {
 		printk (KERN_ERR PFX "out of memory, aborting\n");
@@ -2456,15 +2859,21 @@
 		goto err_out_none;
 	}
 
-	pdev->driver_data = card;
+	pci_set_drvdata (pdev, card);
 
 	memset (card, 0, sizeof (*card));
 	card->pdev = pdev;
 	card->baseaddr = pci_resource_start (pdev, 0);
 	card->card_num = via_num_cards++;
 	spin_lock_init (&card->lock);
-	init_waitqueue_head(&card->open_wait);
-	
+	init_MUTEX (&card->syscall_sem);
+	init_MUTEX (&card->open_sem);
+
+	/* we must init these now, in case the intr handler needs them */
+	via_chan_init_defaults (card, &card->ch_out);
+	via_chan_init_defaults (card, &card->ch_in);
+	via_chan_init_defaults (card, &card->ch_fm);
+
 	/* if BAR 2 is present, chip is Rev H or later,
 	 * which means it has a few extra features */
 	if (pci_resource_start (pdev, 2) > 0)
@@ -2481,8 +2890,8 @@
 		rc = -ENODEV;
 		goto err_out_kfree;
 	}
-	
-	/* 
+
+	/*
 	 * init AC97 mixer and codec
 	 */
 	rc = via_ac97_init (card);
@@ -2499,10 +2908,10 @@
 		printk (KERN_ERR PFX "DSP device init failed, aborting\n");
 		goto err_out_have_mixer;
 	}
-	
+
 	/*
 	 * per-card /proc info
-	 */	
+	 */
 	rc = via_card_init_proc (card);
 	if (rc) {
 		printk (KERN_ERR PFX "card-specific /proc init failed, aborting\n");
@@ -2517,10 +2926,11 @@
 		printk (KERN_ERR PFX "interrupt init failed, aborting\n");
 		goto err_out_have_proc;
 	}
-	
+
 	pci_read_config_byte (pdev, 0x3C, &tmp);
 	if ((tmp & 0x0F) != pdev->irq) {
 		printk (KERN_WARNING PFX "IRQ fixup, 0x3C==0x%02X\n", tmp);
+		udelay (15);
 		tmp &= 0xF0;
 		tmp |= pdev->irq;
 		pci_write_config_byte (pdev, 0x3C, tmp);
@@ -2532,7 +2942,7 @@
 
 	printk (KERN_INFO PFX "board #%d at 0x%04lX, IRQ %d\n",
 		card->card_num + 1, card->baseaddr, pdev->irq);
-	
+
 	DPRINTK ("EXIT, returning 0\n");
 	return 0;
 
@@ -2554,7 +2964,7 @@
 err_out_none:
 	release_region (pci_resource_start (pdev, 0), pci_resource_len (pdev, 0));
 err_out:
-	pdev->driver_data = NULL;
+	pci_set_drvdata (pdev, NULL);
 	DPRINTK ("EXIT - returning %d\n", rc);
 	return rc;
 }
@@ -2563,13 +2973,13 @@
 static void __exit via_remove_one (struct pci_dev *pdev)
 {
 	struct via_info *card;
-	
+
 	DPRINTK ("ENTER\n");
-	
+
 	assert (pdev != NULL);
-	card = pdev->driver_data;
+	card = pci_get_drvdata (pdev);
 	assert (card != NULL);
-	
+
 	via_interrupt_cleanup (card);
 	via_card_cleanup_proc (card);
 	via_dsp_cleanup (card);
@@ -2582,8 +2992,8 @@
 #endif
 	kfree (card);
 
-	pdev->driver_data = NULL;
-	
+	pci_set_drvdata (pdev, NULL);
+
 	pci_set_power_state (pdev, 3); /* ...zzzzzz */
 
 	DPRINTK ("EXIT\n");
@@ -2623,11 +3033,11 @@
 	return 0;
 }
 
- 
+
 static void __exit cleanup_via82cxxx_audio(void)
 {
 	DPRINTK("ENTER\n");
-	
+
 	pci_unregister_driver (&via_driver);
 	via_cleanup_proc ();
 
@@ -2637,3 +3047,187 @@
 
 module_init(init_via82cxxx_audio);
 module_exit(cleanup_via82cxxx_audio);
+
+MODULE_AUTHOR("Jeff Garzik <jgarzik@mandrakesoft.com>");
+MODULE_DESCRIPTION("DSP audio and mixer driver for Via 82Cxxx audio devices");
+EXPORT_NO_SYMBOLS;
+
+
+
+#ifdef VIA_PROC_FS
+
+/****************************************************************
+ *
+ * /proc/driver/via/info
+ *
+ *
+ */
+
+static int via_info_read_proc (char *page, char **start, off_t off,
+			       int count, int *eof, void *data)
+{
+#define YN(val,bit) (((val) & (bit)) ? "yes" : "no")
+#define ED(val,bit) (((val) & (bit)) ? "enable" : "disable")
+
+	int len = 0;
+	u8 r40, r41, r42, r44;
+	struct via_info *card = data;
+
+	DPRINTK ("ENTER\n");
+
+	assert (card != NULL);
+
+	len += sprintf (page+len, VIA_CARD_NAME "\n\n");
+
+	pci_read_config_byte (card->pdev, 0x40, &r40);
+	pci_read_config_byte (card->pdev, 0x41, &r41);
+	pci_read_config_byte (card->pdev, 0x42, &r42);
+	pci_read_config_byte (card->pdev, 0x44, &r44);
+
+	len += sprintf (page+len,
+		"Via 82Cxxx PCI registers:\n"
+		"\n"
+		"40  Codec Ready: %s\n"
+		"    Codec Low-power: %s\n"
+		"    Secondary Codec Ready: %s\n"
+		"\n"
+		"41  Interface Enable: %s\n"
+		"    De-Assert Reset: %s\n"
+		"    Force SYNC high: %s\n"
+		"    Force SDO high: %s\n"
+		"    Variable Sample Rate On-Demand Mode: %s\n"
+		"    SGD Read Channel PCM Data Out: %s\n"
+		"    FM Channel PCM Data Out: %s\n"
+		"    SB PCM Data Out: %s\n"
+		"\n"
+		"42  Game port enabled: %s\n"
+		"    SoundBlaster enabled: %s\n"
+		"    FM enabled: %s\n"
+		"    MIDI enabled: %s\n"
+		"\n"
+		"44  AC-Link Interface Access: %s\n"
+		"    Secondary Codec Support: %s\n"
+
+		"\n",
+
+		YN (r40, VIA_CR40_AC97_READY),
+		YN (r40, VIA_CR40_AC97_LOW_POWER),
+		YN (r40, VIA_CR40_SECONDARY_READY),
+
+		ED (r41, VIA_CR41_AC97_ENABLE),
+		YN (r41, (1 << 6)),
+		YN (r41, (1 << 5)),
+		YN (r41, (1 << 4)),
+		ED (r41, (1 << 3)),
+		ED (r41, (1 << 2)),
+		ED (r41, (1 << 1)),
+		ED (r41, (1 << 0)),
+
+		YN (r42, VIA_CR42_GAME_ENABLE),
+		YN (r42, VIA_CR42_SB_ENABLE),
+		YN (r42, VIA_CR42_FM_ENABLE),
+		YN (r42, VIA_CR42_MIDI_ENABLE),
+
+		YN (r44, VIA_CR44_AC_LINK_ACCESS),
+		YN (r44, VIA_CR44_SECOND_CODEC_SUPPORT)
+
+		);
+
+	DPRINTK("EXIT, returning %d\n", len);
+	return len;
+
+#undef YN
+#undef ED
+}
+
+
+/****************************************************************
+ *
+ * /proc/driver/via/... setup and cleanup
+ *
+ *
+ */
+
+static int __init via_init_proc (void)
+{
+	DPRINTK ("ENTER\n");
+
+	if (!proc_mkdir ("driver/via", 0))
+		return -EIO;
+
+	DPRINTK ("EXIT, returning 0\n");
+	return 0;
+}
+
+
+static void via_cleanup_proc (void)
+{
+	DPRINTK ("ENTER\n");
+
+	remove_proc_entry ("driver/via", NULL);
+
+	DPRINTK ("EXIT\n");
+}
+
+
+static int __init via_card_init_proc (struct via_info *card)
+{
+	char s[32];
+	int rc;
+
+	DPRINTK ("ENTER\n");
+
+	sprintf (s, "driver/via/%d", card->card_num);
+	if (!proc_mkdir (s, 0)) {
+		rc = -EIO;
+		goto err_out_none;
+	}
+
+	sprintf (s, "driver/via/%d/info", card->card_num);
+	if (!create_proc_read_entry (s, 0, 0, via_info_read_proc, card)) {
+		rc = -EIO;
+		goto err_out_dir;
+	}
+
+	sprintf (s, "driver/via/%d/ac97", card->card_num);
+	if (!create_proc_read_entry (s, 0, 0, ac97_read_proc, &card->ac97)) {
+		rc = -EIO;
+		goto err_out_info;
+	}
+
+	DPRINTK ("EXIT, returning 0\n");
+	return 0;
+
+err_out_info:
+	sprintf (s, "driver/via/%d/info", card->card_num);
+	remove_proc_entry (s, NULL);
+
+err_out_dir:
+	sprintf (s, "driver/via/%d", card->card_num);
+	remove_proc_entry (s, NULL);
+
+err_out_none:
+	DPRINTK ("EXIT, returning %d\n", rc);
+	return rc;
+}
+
+
+static void via_card_cleanup_proc (struct via_info *card)
+{
+	char s[32];
+
+	DPRINTK ("ENTER\n");
+
+	sprintf (s, "driver/via/%d/ac97", card->card_num);
+	remove_proc_entry (s, NULL);
+
+	sprintf (s, "driver/via/%d/info", card->card_num);
+	remove_proc_entry (s, NULL);
+
+	sprintf (s, "driver/via/%d", card->card_num);
+	remove_proc_entry (s, NULL);
+
+	DPRINTK ("EXIT\n");
+}
+
+#endif /* VIA_PROC_FS */

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)