## Automatically generated incremental diff
## From:   linux-2.4.8-pre8
## To:     linux-2.4.8
## Robot:  $Id: make-incremental-diff,v 1.8 2001/12/04 06:05:04 hpa Exp $

diff -urN linux-2.4.8-pre8/Makefile linux/Makefile
--- linux-2.4.8-pre8/Makefile	Mon Dec  3 23:00:32 2001
+++ linux/Makefile	Mon Dec  3 23:00:44 2001
@@ -1,7 +1,7 @@
 VERSION = 2
 PATCHLEVEL = 4
 SUBLEVEL = 8
-EXTRAVERSION =-pre8
+EXTRAVERSION =
 
 KERNELRELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
 
diff -urN linux-2.4.8-pre8/drivers/sound/Makefile linux/drivers/sound/Makefile
--- linux-2.4.8-pre8/drivers/sound/Makefile	Mon Jul  2 14:03:04 2001
+++ linux/drivers/sound/Makefile	Mon Dec  3 23:00:45 2001
@@ -73,7 +73,7 @@
 subdir-$(CONFIG_SOUND_CS4281) += cs4281
 
 ifeq ($(CONFIG_SOUND_EMU10K1),y)
-  obj-y += emu10k1/emu10k1.o
+  obj-y += ac97_codec.o emu10k1/emu10k1.o
 endif
 
 ifeq ($(CONFIG_SOUND_CS4281),y)
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/8010.h linux/drivers/sound/emu10k1/8010.h
--- linux-2.4.8-pre8/drivers/sound/emu10k1/8010.h	Mon Dec 11 13:02:20 2000
+++ linux/drivers/sound/emu10k1/8010.h	Mon Dec  3 23:00:45 2001
@@ -39,22 +39,6 @@
 
 #include <linux/types.h>
 
-/* ------------------- DEFINES -------------------- */
-
-#define CMD_WRITEFN0		0x0
-#define CMD_READFN0		0x1
-#define CMD_WRITEPTR		0x2
-#define CMD_READPTR		0x3
-#define CMD_SETRECSRC		0x4
-#define CMD_GETRECSRC		0x5
-#define CMD_GETVOICEPARAM	0x6
-#define CMD_SETVOICEPARAM	0x7
-
-struct mixer_private_ioctl {
-	u32 cmd;
-	u32 val[10];
-};
-
 /************************************************************************************************/
 /* PCI function 0 registers, address = <val> + PCIBASE0						*/
 /************************************************************************************************/
@@ -171,7 +155,10 @@
 #define HCFG_CODECFORMAT_I2S	0x00010000	/* I2S CODEC format -- Secondary (Rear) Output	*/
 #define HCFG_GPINPUT0		0x00004000	/* External pin112				*/
 #define HCFG_GPINPUT1		0x00002000	/* External pin110				*/
+
 #define HCFG_GPOUTPUT_MASK	0x00001c00	/* External pins which may be controlled	*/
+#define HCFG_GPOUT0		0x00001000	/* set to enable digital out on 5.1 cards	*/
+
 #define HCFG_JOYENABLE      	0x00000200	/* Internal joystick enable    			*/
 #define HCFG_PHASETRACKENABLE	0x00000100	/* Phase tracking enable			*/
 						/* 1 = Force all 3 async digital inputs to use	*/
@@ -224,54 +211,6 @@
 #define AC97ADDRESS_READY	0x80		/* Read-only bit, reflects CODEC READY signal	*/
 #define AC97ADDRESS_ADDRESS	0x7f		/* Address of indexed AC97 register		*/
 
-/************************************************************************************************/
-/* PCI function 1 registers, address = <val> + PCIBASE1						*/
-/************************************************************************************************/
-
-#define JOYSTICK1		0x00		/* Analog joystick port register		*/
-#define JOYSTICK2		0x01		/* Analog joystick port register		*/
-#define JOYSTICK3		0x02		/* Analog joystick port register		*/
-#define JOYSTICK4		0x03		/* Analog joystick port register		*/
-#define JOYSTICK5		0x04		/* Analog joystick port register		*/
-#define JOYSTICK6		0x05		/* Analog joystick port register		*/
-#define JOYSTICK7		0x06		/* Analog joystick port register		*/
-#define JOYSTICK8		0x07		/* Analog joystick port register		*/
-
-/* When writing, any write causes JOYSTICK_COMPARATOR output enable to be pulsed on write.	*/
-/* When reading, use these bitfields: */
-#define JOYSTICK_BUTTONS	0x0f		/* Joystick button data				*/
-#define JOYSTICK_COMPARATOR	0xf0		/* Joystick comparator data			*/
-
-
-/********************************************************************************************************/
-/* AC97 pointer-offset register set, accessed through the AC97ADDRESS and AC97DATA registers		*/
-/********************************************************************************************************/
-
-#define AC97_RESET		0x00
-#define AC97_MASTERVOLUME	0x02		/* Master volume					*/
-#define AC97_HEADPHONEVOLUME	0x04		/* Headphone volume					*/
-#define AC97_MASTERVOLUMEMONO	0x06		/* Mast volume mono					*/
-#define AC97_MASTERTONE		0x08
-#define AC97_PCBEEPVOLUME	0x0a		/* PC speaker system beep volume			*/
-#define AC97_PHONEVOLUME	0x0c
-#define AC97_MICVOLUME		0x0e
-#define AC97_LINEINVOLUME	0x10
-#define AC97_CDVOLUME		0x12
-#define AC97_VIDEOVOLUME	0x14
-#define AC97_AUXVOLUME		0x16
-#define AC97_PCMOUTVOLUME	0x18
-#define AC97_RECORDSELECT	0x1a
-#define AC97_RECORDGAIN		0x1c
-#define AC97_RECORDGAINMIC	0x1e
-#define AC97_GENERALPURPOSE	0x20
-#define AC97_3DCONTROL		0x22
-#define AC97_MODEMRATE		0x24
-#define AC97_POWERDOWN		0x26
-#define AC97_VENDORID1		0x7c
-#define AC97_VENDORID2		0x7e
-#define AC97_ZVIDEOVOLUME	0xec
-#define AC97_AC3VOLUME		0xed
-
 /********************************************************************************************************/
 /* Emu10k1 pointer-offset register set, accessed through the PTR and DATA registers			*/
 /********************************************************************************************************/
@@ -568,6 +507,16 @@
 
 #define DBG			0x52		/* DO NOT PROGRAM THIS REGISTER!!! MAY DESTROY CHIP */
 
+/* definitions for debug register - taken from the alsa drivers */
+#define DBG_ZC                  0x80000000      /* zero tram counter */
+#define DBG_SATURATION_OCCURED  0x02000000      /* saturation control */
+#define DBG_SATURATION_ADDR     0x01ff0000      /* saturation address */
+#define DBG_SINGLE_STEP         0x00008000      /* single step mode */
+#define DBG_STEP                0x00004000      /* start single step */
+#define DBG_CONDITION_CODE      0x00003e00      /* condition code */
+#define DBG_SINGLE_STEP_ADDR    0x000001ff      /* single step address */
+
+
 #define REG53			0x53		/* DO NOT PROGRAM THIS REGISTER!!! MAY DESTROY CHIP */
 
 #define SPCS0			0x54		/* SPDIF output Channel Status 0 register	*/
@@ -615,6 +564,10 @@
 
 #define SPBYPASS		0x5e		/* SPDIF BYPASS mode register			*/
 #define SPBYPASS_ENABLE		0x00000001	/* Enable SPDIF bypass mode			*/
+
+#define AC97SLOT		0x5f		/* additional AC97 slots enable bits */
+#define AC97SLOT_CNTR		0x10		/* Center enable */
+#define AC97SLOT_LFE		0x20		/* LFE enable */
 
 #define CDSRCS			0x60		/* CD-ROM Sample Rate Converter status register	*/
 
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/Makefile linux/drivers/sound/emu10k1/Makefile
--- linux-2.4.8-pre8/drivers/sound/emu10k1/Makefile	Fri Dec 29 14:07:23 2000
+++ linux/drivers/sound/emu10k1/Makefile	Mon Dec  3 23:00:45 2001
@@ -5,8 +5,9 @@
 O_TARGET := emu10k1.o
 
 obj-y :=     audio.o cardmi.o cardmo.o cardwi.o cardwo.o ecard.o \
-             emuadxmg.o hwaccess.o irqmgr.o main.o midi.o mixer.o \
-             recmgr.o timer.o voicemgr.o
+             efxmgr.o emuadxmg.o hwaccess.o irqmgr.o joystick.o \
+             main.o midi.o mixer.o passthrough.o recmgr.o timer.o \
+             voicemgr.o
 obj-m := $(O_TARGET)
 
 ifdef DEBUG
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/audio.c linux/drivers/sound/emu10k1/audio.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/audio.c	Mon Jul  2 13:56:41 2001
+++ linux/drivers/sound/emu10k1/audio.c	Mon Dec  3 23:00:45 2001
@@ -48,6 +48,8 @@
 #include "recmgr.h"
 #include "irqmgr.h"
 #include "audio.h"
+#include "8010.h"
+#include "passthrough.h"
 
 static void calculate_ofrag(struct woinst *);
 static void calculate_ifrag(struct wiinst *);
@@ -167,6 +169,18 @@
 		return -ENXIO;
 	}
 
+	if (woinst->format.passthrough) {
+		int r;
+		
+		woinst->buffer.ossfragshift = PT_BLOCKSIZE_LOG2;
+		woinst->buffer.numfrags = PT_BLOCKCOUNT;
+		calculate_ofrag(woinst);
+		
+		r = emu10k1_pt_write(file, buffer, count);
+		spin_unlock_irqrestore(&woinst->lock, flags);
+		return r;
+	}
+
 	if (woinst->state == WAVE_STATE_CLOSED) {
 		calculate_ofrag(woinst);
 
@@ -188,6 +202,11 @@
 	spin_unlock_irqrestore(&woinst->lock, flags);
 
 	ret = 0;
+	if (count % woinst->format.bytespersample)
+                return -EINVAL;
+
+	count /= woinst->num_voices;
+
 	while (count > 0) {
 		u32 bytestocopy;
 
@@ -206,8 +225,8 @@
 			emu10k1_waveout_xferdata(woinst, (u8 *) buffer, &bytestocopy);
 
 			count -= bytestocopy;
-			buffer += bytestocopy;
-			ret += bytestocopy;
+			buffer += bytestocopy * woinst->num_voices;
+			ret += bytestocopy * woinst->num_voices;
 
 			spin_lock_irqsave(&woinst->lock, flags);
 			woinst->total_copied += bytestocopy;
@@ -267,14 +286,6 @@
 			spin_lock_irqsave(&woinst->lock, flags);
 
 			if (woinst->state & WAVE_STATE_OPEN) {
-				if (woinst->mmapped) {
-					int i;
-
-					/* Undo marking the pages as reserved */
-					for (i = 0; i < woinst->buffer.pages; i++)
-						mem_map_unreserve(virt_to_page(woinst->buffer.addr[i]));
-				}
-
 				emu10k1_waveout_close(wave_dev);
 			}
 
@@ -289,8 +300,9 @@
 		if (file->f_mode & FMODE_READ) {
 			spin_lock_irqsave(&wiinst->lock, flags);
 
-			if (wiinst->state & WAVE_STATE_OPEN)
+			if (wiinst->state & WAVE_STATE_OPEN) {
 				emu10k1_wavein_close(wave_dev);
+			}
 
 			wiinst->mmapped = 0;
 			wiinst->total_recorded = 0;
@@ -316,15 +328,6 @@
 						interruptible_sleep_on(&woinst->wait_queue);
 						spin_lock_irqsave(&woinst->lock, flags);
 					}
-
-				if (woinst->mmapped) {
-					int i;
-
-					/* Undo marking the pages as reserved */
-					for (i = 0; i < woinst->buffer.pages; i++)
-						mem_map_unreserve(virt_to_page(woinst->buffer.addr[i]));
-				}
-
 				emu10k1_waveout_close(wave_dev);
 			}
 
@@ -339,8 +342,9 @@
 		if (file->f_mode & FMODE_READ) {
 			spin_lock_irqsave(&wiinst->lock, flags);
 
-			if (wiinst->state & WAVE_STATE_OPEN)
+			if (wiinst->state & WAVE_STATE_OPEN) {
 				emu10k1_wavein_close(wave_dev);
+			}
 
 			wiinst->mmapped = 0;
 			wiinst->total_recorded = 0;
@@ -490,7 +494,6 @@
 					spin_unlock_irqrestore(&wiinst->lock, flags);
 					return -EINVAL;
 				}
-
 				val = wiinst->format.channels;
 
 				spin_unlock_irqrestore(&wiinst->lock, flags);
@@ -532,9 +535,13 @@
 
 		if (file->f_mode & FMODE_READ)
 			val = AFMT_S16_LE;
-		else if (file->f_mode & FMODE_WRITE)
+		else if (file->f_mode & FMODE_WRITE) {
 			val = AFMT_S16_LE | AFMT_U8;
-
+			if (emu10k1_find_control_gpr(&wave_dev->card->mgr,
+			    			     wave_dev->card->pt.patch_name, 
+			    			     wave_dev->card->pt.enable_gpr_name) >= 0)
+				val |= AFMT_AC3;
+		}
 		return put_user(val, (int *) arg);
 
 	case SNDCTL_DSP_SETFMT:	/* Same as SNDCTL_DSP_SAMPLESIZE */
@@ -552,17 +559,17 @@
 				spin_lock_irqsave(&wiinst->lock, flags);
 
 				format = wiinst->format;
-				format.bitsperchannel = val;
+				format.id = val;
 
 				if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
 					spin_unlock_irqrestore(&wiinst->lock, flags);
 					return -EINVAL;
 				}
 
-				val = wiinst->format.bitsperchannel;
+				val = wiinst->format.id;
 
 				spin_unlock_irqrestore(&wiinst->lock, flags);
-				DPD(2, "set recording sample size -> %d\n", val);
+				DPD(2, "set recording format -> %d\n", val);
 			}
 
 			if (file->f_mode & FMODE_WRITE) {
@@ -571,27 +578,27 @@
 				spin_lock_irqsave(&woinst->lock, flags);
 
 				format = woinst->format;
-				format.bitsperchannel = val;
+				format.id = val;
 
 				if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
 					spin_unlock_irqrestore(&woinst->lock, flags);
 					return -EINVAL;
 				}
 
-				val = woinst->format.bitsperchannel;
+				val = woinst->format.id;
 
 				spin_unlock_irqrestore(&woinst->lock, flags);
-				DPD(2, "set playback sample size -> %d\n", val);
+				DPD(2, "set playback format -> %d\n", val);
 			}
 
-			return put_user((val == 16) ? AFMT_S16_LE : AFMT_U8, (int *) arg);
+			return put_user(val, (int *) arg);
 		} else {
 			if (file->f_mode & FMODE_READ)
-				val = wiinst->format.bitsperchannel;
+				val = wiinst->format.id;
 			else if (file->f_mode & FMODE_WRITE)
-				val = woinst->format.bitsperchannel;
+				val = woinst->format.id;
 
-			return put_user((val == 16) ? AFMT_S16_LE : AFMT_U8, (int *) arg);
+			return put_user(val, (int *) arg);
 		}
 		break;
 
@@ -602,7 +609,7 @@
 		else if (file->f_mode & FMODE_WRITE)
 			val = woinst->format.bitsperchannel;
 
-		return put_user((val == 16) ? AFMT_S16_LE : AFMT_U8, (int *) arg);
+		return put_user(val, (int *) arg);
 
 	case SOUND_PCM_READ_RATE:
 
@@ -705,9 +712,10 @@
 			}
 			spin_unlock_irqrestore(&woinst->lock, flags);
 
-			info.fragstotal = woinst->buffer.numfrags;
-			info.fragments = info.bytes / woinst->buffer.fragment_size;
-			info.fragsize = woinst->buffer.fragment_size;
+			info.bytes *= woinst->num_voices;
+			info.fragsize = woinst->buffer.fragment_size * woinst->num_voices;
+			info.fragstotal = woinst->buffer.numfrags * woinst->num_voices;
+			info.fragments = info.bytes / info.fragsize;
 
 			if (copy_to_user((int *) arg, &info, sizeof(info)))
 				return -EFAULT;
@@ -763,6 +771,7 @@
 		} else
 			val = 0;
 
+		val *= woinst->num_voices;
 		spin_unlock_irqrestore(&woinst->lock, flags);
 
 		return put_user(val, (int *) arg);
@@ -790,6 +799,9 @@
 				cinfo.blocks = 0;
 			}
 
+			if(wiinst->mmapped)
+				wiinst->buffer.bytestocopy %= wiinst->buffer.fragment_size;
+
 			spin_unlock_irqrestore(&wiinst->lock, flags);
 
 			if (copy_to_user((void *) arg, &cinfo, sizeof(cinfo)))
@@ -808,19 +820,31 @@
 
 			spin_lock_irqsave(&woinst->lock, flags);
 
-			if (woinst->state & WAVE_STATE_OPEN) {
-				emu10k1_waveout_update(woinst);
+			if (woinst->state & WAVE_STATE_OPEN || 
+			    (woinst->format.passthrough && wave_dev->card->pt.state)) {
+				int num_fragments;
+				if (woinst->format.passthrough) {
+					emu10k1_pt_waveout_update(wave_dev);
+					cinfo.bytes = woinst->total_played;
+				} else {
+					emu10k1_waveout_update(woinst);
+					cinfo.bytes = woinst->total_played;
+				}
 				cinfo.ptr = woinst->buffer.hw_pos;
-				cinfo.bytes = cinfo.ptr + woinst->total_played - woinst->total_played % woinst->buffer.size;
-				cinfo.blocks = cinfo.bytes / woinst->buffer.fragment_size - woinst->blocks;
-				woinst->blocks = cinfo.bytes / woinst->buffer.fragment_size;
+				num_fragments = cinfo.bytes / woinst->buffer.fragment_size;
+				cinfo.blocks = num_fragments - woinst->blocks;
+				woinst->blocks = num_fragments;
+
+				cinfo.bytes *= woinst->num_voices;
+				cinfo.ptr *= woinst->num_voices;
 			} else {
 				cinfo.ptr = 0;
 				cinfo.bytes = 0;
 				cinfo.blocks = 0;
 			}
-			if(woinst->mmapped)
-				woinst->buffer.bytestocopy %= woinst->buffer.fragment_size;
+
+			if (woinst->mmapped)
+				woinst->buffer.free_bytes %= woinst->buffer.fragment_size;
 
 			spin_unlock_irqrestore(&woinst->lock, flags);
 
@@ -836,7 +860,7 @@
 			spin_lock_irqsave(&woinst->lock, flags);
 
 			calculate_ofrag(woinst);
-			val = woinst->buffer.fragment_size;
+			val = woinst->buffer.fragment_size * woinst->num_voices;
 
 			spin_unlock_irqrestore(&woinst->lock, flags);
 		}
@@ -878,13 +902,14 @@
 		if (get_user(val, (int *) arg))
 			return -EFAULT;
 
-		DPD(2, "val is 0x%x\n", val);
+		DPD(2, "val is %#x\n", val);
 
 		if (val == 0)
 			return -EIO;
 
 		if (file->f_mode & FMODE_WRITE) {
-			if (woinst->state & WAVE_STATE_OPEN)
+			/* digital pass-through fragment count and size are fixed values */
+			if (woinst->state & WAVE_STATE_OPEN || woinst->format.passthrough)
 				return -EINVAL;	/* too late to change */
 
 			woinst->buffer.ossfragshift = val & 0xffff;
@@ -905,126 +930,175 @@
 		{
 			copr_buffer *buf;
 			u32 i;
-			int ret = -EFAULT;
 
-			DPF(2, "SNDCTL_COPR_LOAD:\n");
-			
+			DPF(4, "SNDCTL_COPR_LOAD:\n");
+
 			buf = kmalloc(sizeof(copr_buffer), GFP_KERNEL);
-			if(buf == NULL)
+			if (!buf)
 				return -ENOMEM;
 
-			if (copy_from_user(buf, (copr_buffer *) arg, sizeof(buf)))
-				goto fail;
+			if (copy_from_user(buf, (copr_buffer *) arg, sizeof(copr_buffer))) {
+				kfree (buf);
+				return -EFAULT;
+			}
 
-			if ((buf->command != 1) && (buf->command != 2))
-			{
-				ret = -EINVAL;
-				goto fail;
-			}
-
-			if ((buf->offs < 0x100)
-			    || (buf->offs < 0x000)
-			    || (buf->offs + buf->len > 0x800) || (buf->len > 1000))
-			{
-				ret = -EINVAL;
-				goto fail;
+			if ((buf->command != CMD_READ) && (buf->command != CMD_WRITE)) {
+				kfree (buf);
+				return -EINVAL;
+			}
+#ifdef DBGEMU
+			if ( (buf->offs < 0) || (buf->offs + buf->len > 0x800) || (buf->len > 1000)) {
+#else
+			if ( ((buf->offs < 0x100 ) || (buf->offs + buf->len > 0x800) || (buf->len > 1000))
+			     && !( ( buf->offs == DBG) && (buf->len ==1) )){
+#endif	
+				kfree(buf);
+				return -EINVAL;
 			}
-			if (buf->command == 1) {
+
+			if (buf->command == CMD_READ) {
 				for (i = 0; i < buf->len; i++)
 					((u32 *) buf->data)[i] = sblive_readptr(wave_dev->card, buf->offs + i, 0);
 
-				if (copy_to_user((copr_buffer *) arg, buf, sizeof(buf)))
-					goto fail;
+				if (copy_to_user((copr_buffer *) arg, buf, sizeof(copr_buffer))) {
+					kfree(buf);
+					return -EFAULT;
+				}
 			} else {
 				for (i = 0; i < buf->len; i++)
 					sblive_writeptr(wave_dev->card, buf->offs + i, 0, ((u32 *) buf->data)[i]);
 			}
-			ret = 0;
-		fail:			
-			kfree(buf);
-			return ret;
+
+			kfree (buf);
+			break;
 		}
 
 	default:		/* Default is unrecognized command */
-		DPD(2, "default: 0x%x\n", cmd);
+		DPD(2, "default: %#x\n", cmd);
 		return -EINVAL;
 	}
 	return 0;
 }
 
+static struct page *emu10k1_mm_nopage (struct vm_area_struct * vma, unsigned long address, int write_access)
+{
+	struct emu10k1_wavedevice *wave_dev = vma->vm_private_data;
+	struct woinst *woinst = wave_dev->woinst;
+	struct wiinst *wiinst = wave_dev->wiinst;
+	struct page *dmapage;
+	unsigned long pgoff;
+	int rd, wr;
+
+	if (address > vma->vm_end) {
+		DPF(2, "EXIT, returning NOPAGE_SIGBUS\n");
+		return NOPAGE_SIGBUS; /* Disallow mremap */
+	}
+
+	pgoff = vma->vm_pgoff + ((address - vma->vm_start) >> PAGE_SHIFT);
+	if (woinst != NULL)
+		wr = woinst->mmapped;
+	else
+		wr = 0;
+
+	if (wiinst != NULL)
+		rd = wiinst->mmapped;
+	else
+		rd = 0;
+
+	/* if full-duplex (read+write) and we have two sets of bufs,
+	* then the playback buffers come first, sez soundcard.c */
+	if (wr) {
+		if (pgoff >= woinst->buffer.pages) {
+			pgoff -= woinst->buffer.pages;
+			dmapage = virt_to_page ((u8 *) wiinst->buffer.addr + pgoff * PAGE_SIZE);
+		} else
+			dmapage = virt_to_page (woinst->buffer.mem[0].addr[pgoff]);
+	} else {
+		dmapage = virt_to_page ((u8 *) wiinst->buffer.addr + pgoff * PAGE_SIZE);
+	}
+
+	get_page (dmapage);
+	return dmapage;
+}
+
+struct vm_operations_struct emu10k1_mm_ops = {
+	nopage:         emu10k1_mm_nopage,
+};
+
 static int emu10k1_audio_mmap(struct file *file, struct vm_area_struct *vma)
 {
 	struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
+	unsigned long maxsize, size, offset, pgoffset;
+	struct woinst *woinst = NULL;
+	struct wiinst *wiinst = NULL;
+	unsigned long flags;
 
 	DPF(2, "emu10k1_audio_mmap()\n");
 
-	if (vma_get_pgoff(vma) != 0)
-		return -ENXIO;
-
-	lock_kernel();
-
+	maxsize = 0;
 	if (vma->vm_flags & VM_WRITE) {
-		struct woinst *woinst = wave_dev->woinst;
-		u32 size;
-		unsigned long flags;
-		int i;
+		woinst = wave_dev->woinst;
 
 		spin_lock_irqsave(&woinst->lock, flags);
 
+		/* No m'mapping possible for multichannel */
+		if (woinst->num_voices > 1) {
+			spin_unlock_irqrestore(&woinst->lock, flags);
+                	return -EINVAL;
+		}
+
 		if (woinst->state == WAVE_STATE_CLOSED) {
 			calculate_ofrag(woinst);
 
 			if (emu10k1_waveout_open(wave_dev) < 0) {
 				spin_unlock_irqrestore(&woinst->lock, flags);
 				ERROR();
-				unlock_kernel();
 				return -EINVAL;
 			}
-
-			/* Now mark the pages as reserved, otherwise remap_page_range doesn't do what we want */
-			for (i = 0; i < woinst->buffer.pages; i++)
-				mem_map_reserve(virt_to_page(woinst->buffer.addr[i]));
-		}
-
-		size = vma->vm_end - vma->vm_start;
-
-		if (size > (PAGE_SIZE * woinst->buffer.pages)) {
-			spin_unlock_irqrestore(&woinst->lock, flags);
-			unlock_kernel();
-			return -EINVAL;
-		}
-
-		for (i = 0; i < woinst->buffer.pages; i++) {
-			if (remap_page_range(vma->vm_start + (i * PAGE_SIZE), virt_to_phys(woinst->buffer.addr[i]), PAGE_SIZE, vma->vm_page_prot)) {
-				spin_unlock_irqrestore(&woinst->lock, flags);
-				unlock_kernel();
-				return -EAGAIN;
-			}
 		}
 
 		woinst->mmapped = 1;
-
+		maxsize += woinst->buffer.pages * PAGE_SIZE;
 		spin_unlock_irqrestore(&woinst->lock, flags);
 	}
 
 	if (vma->vm_flags & VM_READ) {
-		struct wiinst *wiinst = wave_dev->wiinst;
-		unsigned long flags;
+		wiinst = wave_dev->wiinst;
 
 		spin_lock_irqsave(&wiinst->lock, flags);
+		if (wiinst->state == WAVE_STATE_CLOSED) {
+			calculate_ifrag(wiinst);
+
+			if (emu10k1_wavein_open(wave_dev) < 0) {
+				spin_unlock_irqrestore(&wiinst->lock, flags);
+				ERROR();
+				return -EINVAL;
+			}
+		}
+
 		wiinst->mmapped = 1;
+		maxsize += wiinst->buffer.pages * PAGE_SIZE;
 		spin_unlock_irqrestore(&wiinst->lock, flags);
 	}
 
-	unlock_kernel();
+	size = vma->vm_end - vma->vm_start;
+	pgoffset = vma->vm_pgoff;
+	offset = pgoffset << PAGE_SHIFT;
 
+	if (offset + size > maxsize)
+		return -EINVAL;
+
+	vma->vm_flags |= VM_RESERVED;
+	vma->vm_ops = &emu10k1_mm_ops;
+	vma->vm_private_data = wave_dev;
+	
 	return 0;
 }
 
 static int emu10k1_audio_open(struct inode *inode, struct file *file)
 {
 	int minor = MINOR(inode->i_rdev);
-	struct emu10k1_card *card=NULL;
+	struct emu10k1_card *card = NULL;
 	struct list_head *entry;
 	struct emu10k1_wavedevice *wave_dev;
 
@@ -1035,17 +1109,19 @@
 	list_for_each(entry, &emu10k1_devs) {
 		card = list_entry(entry, struct emu10k1_card, list);
 
-		if (!((card->audio_num ^ minor) & ~0xf) || !((card->audio1_num ^ minor) & ~0xf))
-			break;
+		if (!((card->audio_dev ^ minor) & ~0xf) || !((card->audio_dev1 ^ minor) & ~0xf))
+			goto match;
 	}
 
-	if (entry == &emu10k1_devs)
-		return -ENODEV;
+	return -ENODEV;
+
+match:
 
-	if ((wave_dev = (struct emu10k1_wavedevice *)
-	     kmalloc(sizeof(struct emu10k1_wavedevice), GFP_KERNEL)) == NULL) {
+	wave_dev = (struct emu10k1_wavedevice *) kmalloc(sizeof(struct emu10k1_wavedevice), GFP_KERNEL);
+
+	if (wave_dev == NULL) { 
 		ERROR();
-		return -EINVAL;
+		return -ENOMEM;
 	}
 
 	wave_dev->card = card;
@@ -1067,16 +1143,19 @@
 
 		switch (wiinst->recsrc) {
 		case WAVERECORD_AC97:
+			wiinst->format.id = AFMT_S16_LE;
 			wiinst->format.samplingrate = 8000;
 			wiinst->format.bitsperchannel = 16;
 			wiinst->format.channels = 1;
 			break;
 		case WAVERECORD_MIC:
+			wiinst->format.id = AFMT_S16_LE;
 			wiinst->format.samplingrate = 8000;
 			wiinst->format.bitsperchannel = 16;
 			wiinst->format.channels = 1;
 			break;
 		case WAVERECORD_FX:
+			wiinst->format.id = AFMT_S16_LE;
 			wiinst->format.samplingrate = 48000;
 			wiinst->format.bitsperchannel = 16;
 			wiinst->format.channels = hweight32(wiinst->fxwc);
@@ -1105,6 +1184,7 @@
 
 	if (file->f_mode & FMODE_WRITE) {
 		struct woinst *woinst;
+		int i;
 
 		if ((woinst = (struct woinst *) kmalloc(sizeof(struct woinst), GFP_KERNEL)) == NULL) {
 			ERROR();
@@ -1114,6 +1194,7 @@
 		if (wave_dev->wiinst != NULL) {
 			woinst->format = wave_dev->wiinst->format;
 		} else {
+			woinst->format.id = AFMT_U8;
 			woinst->format.samplingrate = 8000;
 			woinst->format.bitsperchannel = 8;
 			woinst->format.channels = 1;
@@ -1124,7 +1205,13 @@
 		woinst->buffer.fragment_size = 0;
 		woinst->buffer.ossfragshift = 0;
 		woinst->buffer.numfrags = 0;
-		woinst->device = (card->audio1_num == minor);
+		woinst->device = (card->audio_dev1 == minor);
+		woinst->timer.state = TIMER_STATE_UNINSTALLED;
+		woinst->num_voices = 1;
+		for (i = 0; i < WAVEOUT_MAXVOICES; i++) {
+			woinst->voice[i].usage = VOICE_USAGE_FREE;
+			woinst->buffer.mem[i].emupageindex = -1;
+		}
 
 		init_waitqueue_head(&woinst->wait_queue);
 
@@ -1137,45 +1224,6 @@
 		wave_dev->woinst = woinst;
 		emu10k1_waveout_setformat(wave_dev, &woinst->format);
 
-#ifdef PRIVATE_PCM_VOLUME
-		{
-			int i;
-			int j = -1;
-
-			/*
-			 * find out if we've already been in this table
-			 * xmms reopens dsp on every move of slider
-			 * this way we keep the same local pcm for such
-			 * process
-			 */
-			for (i = 0; i < MAX_PCM_CHANNELS; i++) {
-				if (sblive_pcm_volume[i].files == current->files)
-					break;
-				// here we should select last used memeber
-				// improve me in case its not sufficient
-				if (j < 0 && !sblive_pcm_volume[i].opened)
-					j = i;
-			}
-			// current task not found
-			if (i == MAX_PCM_CHANNELS) {
-				// add new entry
-				if (j < 0)
-					printk(KERN_WARNING "emu10k1: too many writters!\n");
-				i = (j >= 0) ? j : 0;
-				DPD(2, "new pcm private %p\n", current->files);
-				sblive_pcm_volume[i].files = current->files;
-				sblive_pcm_volume[i].mixer = pcm_last_mixer;
-				sblive_pcm_volume[i].attn_l = 0;
-				sblive_pcm_volume[i].attn_r = 0;
-				sblive_pcm_volume[i].channel_l = NUM_G;
-				sblive_pcm_volume[i].channel_r = NUM_G;
-			} else
-				DPD(2, "old pcm private %p  0x%x\n", current->files,
-				    sblive_pcm_volume[i].mixer);
-
-			sblive_pcm_volume[i].opened++;
-		}
-#endif
 	}
 
 	file->private_data = (void *) wave_dev;
@@ -1189,7 +1237,6 @@
 	struct emu10k1_card *card;
 	unsigned long flags;
 
-	lock_kernel();
 	card = wave_dev->card;
 
 	DPF(2, "emu10k1_audio_release()\n");
@@ -1199,6 +1246,9 @@
 
 		spin_lock_irqsave(&woinst->lock, flags);
 
+		if (woinst->format.passthrough && card->pt.state != PT_STATE_INACTIVE)
+                        emu10k1_pt_stop(card);
+
 		if (woinst->state & WAVE_STATE_OPEN) {
 			if (woinst->state & WAVE_STATE_STARTED) {
 				if (!(file->f_flags & O_NONBLOCK)) {
@@ -1211,31 +1261,9 @@
 					}
 				}
 			}
-
-			if (woinst->mmapped) {
-				int i;
-
-				/* Undo marking the pages as reserved */
-				for (i = 0; i < woinst->buffer.pages; i++)
-					mem_map_unreserve(virt_to_page(woinst->buffer.addr[i]));
-			}
-
 			emu10k1_waveout_close(wave_dev);
 		}
-#ifdef PRIVATE_PCM_VOLUME
-		{
-			int i;
 
-			/* mark as closed
-			 * NOTE: structure remains unchanged for next reopen */
-			for (i = 0; i < MAX_PCM_CHANNELS; i++) {
-				if (sblive_pcm_volume[i].files == current->files) {
-					sblive_pcm_volume[i].opened--;
-					break;
-				}
-			}
-		}
-#endif
 		spin_unlock_irqrestore(&woinst->lock, flags);
 		/* wait for the tasklet (bottom-half) to finish */
 		tasklet_unlock_wait(&woinst->timer.tasklet);
@@ -1247,8 +1275,9 @@
 
 		spin_lock_irqsave(&wiinst->lock, flags);
 
-		if (wiinst->state & WAVE_STATE_OPEN)
+		if (wiinst->state & WAVE_STATE_OPEN) {
 			emu10k1_wavein_close(wave_dev);
+		}
 
 		spin_unlock_irqrestore(&wiinst->lock, flags);
 		tasklet_unlock_wait(&wiinst->timer.tasklet);
@@ -1257,12 +1286,13 @@
 
 	kfree(wave_dev);
 
-	wake_up_interruptible(&card->open_wait);
-	unlock_kernel();
+	if (waitqueue_active(&card->open_wait))
+		wake_up_interruptible(&card->open_wait);
 
 	return 0;
 }
 
+/* FIXME sort out poll() + mmap() */
 static unsigned int emu10k1_audio_poll(struct file *file, struct poll_table_struct *wait)
 {
 	struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
@@ -1292,11 +1322,6 @@
 		} else
 			mask |= POLLOUT | POLLWRNORM;
 
-		if(woinst->mmapped) {
-			spin_unlock_irqrestore(&woinst->lock, flags);
-			return mask;
-		}
-
 		spin_unlock_irqrestore(&woinst->lock, flags);
 	}
 
@@ -1336,7 +1361,7 @@
 		return;
 
 	if (!buffer->ossfragshift) {
-		fragsize = (woinst->format.bytespersec * WAVEOUT_DEFAULTFRAGLEN) / 1000 - 1;
+		fragsize = (woinst->format.bytespervoicesample * woinst->format.samplingrate * WAVEOUT_DEFAULTFRAGLEN) / 1000 - 1;
 
 		while (fragsize) {
 			fragsize >>= 1;
@@ -1352,7 +1377,8 @@
 	if (!buffer->numfrags) {
 		u32 numfrags;
 
-		numfrags = (woinst->format.bytespersec * WAVEOUT_DEFAULTBUFLEN) / (buffer->fragment_size * 1000) - 1;
+		numfrags = (woinst->format.bytespervoicesample * woinst->format.samplingrate * WAVEOUT_DEFAULTBUFLEN) /
+			   (buffer->fragment_size * 1000) - 1;
 
 		buffer->numfrags = 1;
 
@@ -1452,13 +1478,13 @@
 	}
 
 	buffer->numfrags = buffer->size / buffer->fragment_size;
-
+	buffer->pages =  buffer->size / PAGE_SIZE + ((buffer->size % PAGE_SIZE) ? 1 : 0);
 	if (buffer->size % buffer->fragment_size)
 		BUG();
 
 	DPD(2, " calculated recording fragment_size -> %d\n", buffer->fragment_size);
 	DPD(2, " calculated recording numfrags -> %d\n", buffer->numfrags);
-	DPD(2, " buffer size register -> 0x%2x\n", buffer->sizeregval);
+	DPD(2, " buffer size register -> %#04x\n", buffer->sizeregval);
 
 	return;
 }
@@ -1478,17 +1504,11 @@
 	}
 
 	emu10k1_wavein_update(wave_dev->card, wiinst);
-
-	if (wiinst->mmapped) {
-		spin_unlock_irqrestore(&wiinst->lock, flags);
-		return;
-	}
-
 	emu10k1_wavein_getxfersize(wiinst, &bytestocopy);
 
 	spin_unlock_irqrestore(&wiinst->lock, flags);
 
-	if (bytestocopy >= wiinst->buffer.fragment_size)
+	if (bytestocopy >= wiinst->buffer.fragment_size && waitqueue_active(&wiinst->wait_queue))
 		wake_up_interruptible(&wiinst->wait_queue);
 	else
 		DPD(3, "Not enough transfer size, %d\n", bytestocopy);
@@ -1519,7 +1539,7 @@
 	} else
 		spin_unlock_irqrestore(&woinst->lock, flags);
 
-	if (bytestocopy >= woinst->buffer.fragment_size)
+	if (bytestocopy >= woinst->buffer.fragment_size && waitqueue_active(&woinst->wait_queue))
 		wake_up_interruptible(&woinst->wait_queue);
 	else
 		DPD(3, "Not enough transfer size -> %d\n", bytestocopy);
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/cardmi.c linux/drivers/sound/emu10k1/cardmi.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/cardmi.c	Fri Feb  9 11:30:23 2001
+++ linux/drivers/sound/emu10k1/cardmi.c	Mon Dec  3 23:00:45 2001
@@ -1,4 +1,3 @@
-
 /*
  **********************************************************************
  *     sblive_mi.c - MIDI UART input HAL for emu10k1 driver
@@ -291,7 +290,7 @@
 	if (msg == ICARDMIDI_INDATA || msg == ICARDMIDI_INDATAERROR) {
 		callback_msg[1] = data;
 		callback_msg[2] = bytesvalid;
-		DPD(2, "emu10k1_mpuin_callback: midimsg = %lx\n", data);
+		DPD(2, "emu10k1_mpuin_callback: midimsg = %#lx\n", data);
 	} else {
 		midiq = (struct midi_queue *) data;
 		midihdr = (struct midi_hdr *) midiq->refdata;
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/cardmi.h linux/drivers/sound/emu10k1/cardmi.h
--- linux-2.4.8-pre8/drivers/sound/emu10k1/cardmi.h	Fri Apr 13 20:26:07 2001
+++ linux/drivers/sound/emu10k1/cardmi.h	Mon Dec  3 23:00:45 2001
@@ -34,7 +34,6 @@
 #define _CARDMI_H
 
 #include "icardmid.h"
-#include <linux/sched.h>
 #include <linux/interrupt.h>
 
 typedef enum
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/cardmo.c linux/drivers/sound/emu10k1/cardmo.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/cardmo.c	Fri Feb  9 11:30:23 2001
+++ linux/drivers/sound/emu10k1/cardmo.c	Mon Dec  3 23:00:45 2001
@@ -1,4 +1,3 @@
-
 /*     
  **********************************************************************
  *     cardmo.c - MIDI UART output HAL for emu10k1 driver 
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/cardwi.c linux/drivers/sound/emu10k1/cardwi.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/cardwi.c	Mon Aug 14 08:32:48 2000
+++ linux/drivers/sound/emu10k1/cardwi.c	Mon Dec  3 23:00:45 2001
@@ -36,6 +36,12 @@
 #include "audio.h"
 #include "cardwi.h"
 
+/**
+ * query_format - returns a valid sound format
+ *
+ * This function will return a valid sound format as close
+ * to the requested one as possible. 
+ */
 void query_format(int recsrc, struct wave_format *wave_fmt)
 {
 
@@ -62,8 +68,18 @@
 		else
 			wave_fmt->samplingrate = 0x1F40;
 
-		if ((wave_fmt->bitsperchannel != 8) && (wave_fmt->bitsperchannel != 16))
+		switch (wave_fmt->id) {
+		case AFMT_S16_LE:
 			wave_fmt->bitsperchannel = 16;
+			break;
+		case AFMT_U8:
+			wave_fmt->bitsperchannel = 8;
+			break;
+		default:
+			wave_fmt->id = AFMT_S16_LE;
+			wave_fmt->bitsperchannel = 16;
+			break;
+		}
 
 		break;
 
@@ -80,13 +96,13 @@
 	wave_fmt->bytesperchannel = wave_fmt->bitsperchannel >> 3;
 	wave_fmt->bytespersample = wave_fmt->channels * wave_fmt->bytesperchannel;
 	wave_fmt->bytespersec = wave_fmt->bytespersample * wave_fmt->samplingrate;
-
-	return;
 }
 
 static int alloc_buffer(struct emu10k1_card *card, struct wavein_buffer *buffer)
 {
-	if ((buffer->addr = pci_alloc_consistent(card->pci_dev, buffer->size * buffer->cov, &buffer->dma_handle)) == NULL)
+	buffer->addr = pci_alloc_consistent(card->pci_dev, buffer->size * buffer->cov,
+					    &buffer->dma_handle);
+	if (buffer->addr == NULL)
 		return -1;
 
 	return 0;
@@ -95,9 +111,8 @@
 static void free_buffer(struct emu10k1_card *card, struct wavein_buffer *buffer)
 {
 	if (buffer->addr != NULL)
-		pci_free_consistent(card->pci_dev, buffer->size * buffer->cov, buffer->addr, buffer->dma_handle);
-
-	return;
+		pci_free_consistent(card->pci_dev, buffer->size * buffer->cov,
+				    buffer->addr, buffer->dma_handle);
 }
 
 int emu10k1_wavein_open(struct emu10k1_wavedevice *wave_dev)
@@ -195,8 +210,6 @@
 	spin_unlock_irqrestore(&card->lock, flags);
 
 	wiinst->state = WAVE_STATE_CLOSED;
-
-	return;
 }
 
 void emu10k1_wavein_start(struct emu10k1_wavedevice *wave_dev)
@@ -214,8 +227,6 @@
 	wiinst->buffer.bytestocopy = 0;
 
 	wiinst->state |= WAVE_STATE_STARTED;
-
-	return;
 }
 
 void emu10k1_wavein_stop(struct emu10k1_wavedevice *wave_dev)
@@ -232,8 +243,6 @@
 	emu10k1_stop_record(card, &wiinst->buffer);
 
 	wiinst->state &= ~WAVE_STATE_STARTED;
-
-	return;
 }
 
 int emu10k1_wavein_setformat(struct emu10k1_wavedevice *wave_dev, struct wave_format *format)
@@ -282,20 +291,21 @@
 
 	*size = buffer->bytestocopy;
 
+	if (wiinst->mmapped)
+		return;
+
 	if (*size > buffer->size) {
 		*size = buffer->size;
 		buffer->pos = buffer->hw_pos;
 		buffer->bytestocopy = buffer->size;
 		DPF(1, "buffer overrun\n");
 	}
-
-	return;
 }
 
 static void copy_block(u8 *dst, u8 * src, u32 str, u32 len, u8 cov)
 {
 	if (cov == 1)
-		copy_to_user(dst, src + str, len);
+		__copy_to_user(dst, src + str, len);
 	else {
 		u8 byte;
 		u32 i;
@@ -304,11 +314,9 @@
 
 		for (i = 0; i < len; i++) {
 			byte = src[2 * i] ^ 0x80;
-			copy_to_user(dst + i, &byte, 1);
+			__copy_to_user(dst + i, &byte, 1);
 		}
 	}
-
-	return;
 }
 
 void emu10k1_wavein_xferdata(struct wiinst *wiinst, u8 * data, u32 * size)
@@ -340,8 +348,6 @@
 	} else {
 		copy_block(data, buffer->addr, start, sizetocopy, buffer->cov);
 	}
-
-	return;
 }
 
 void emu10k1_wavein_update(struct emu10k1_card *card, struct wiinst *wiinst)
@@ -362,6 +368,4 @@
 	wiinst->buffer.bytestocopy += diff;
 
 	wiinst->buffer.hw_pos = hw_pos;
-
-	return;
 }
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/cardwi.h linux/drivers/sound/emu10k1/cardwi.h
--- linux-2.4.8-pre8/drivers/sound/emu10k1/cardwi.h	Mon Dec 11 13:02:19 2000
+++ linux/drivers/sound/emu10k1/cardwi.h	Mon Dec  3 23:00:45 2001
@@ -43,6 +43,7 @@
 	u32 pos;		/* software cursor position */
 	u32 bytestocopy;	/* bytes of recorded data available */
 	u32 size;
+	u32 pages;
 	u32 sizereg;
 	u32 sizeregval;
         u32 addrreg;
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/cardwo.c linux/drivers/sound/emu10k1/cardwo.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/cardwo.c	Sat May 19 17:43:07 2001
+++ linux/drivers/sound/emu10k1/cardwo.c	Mon Dec  3 23:00:45 2001
@@ -42,25 +42,91 @@
 	return (samplingrate >> 1) + (samplingrate & 1);
 }
 
-static void query_format(struct wave_format *wave_fmt)
+static void query_format(struct emu10k1_wavedevice *wave_dev, struct wave_format *wave_fmt)
 {
-	if ((wave_fmt->channels != 1) && (wave_fmt->channels != 2))
+	int i, j, do_passthrough = 0, is_ac3 = 0;
+	struct emu10k1_card *card = wave_dev->card;
+	struct woinst *woinst = wave_dev->woinst;
+
+	if ((wave_fmt->channels > 2) && (wave_fmt->id != AFMT_S16_LE) && (wave_fmt->id != AFMT_U8))
+		wave_fmt->channels = 2;
+
+	if ((wave_fmt->channels < 1) || (wave_fmt->channels > WAVEOUT_MAXVOICES))
 		wave_fmt->channels = 2;
 
+	if (wave_fmt->channels == 2)
+		woinst->num_voices = 1;
+	else
+		woinst->num_voices = wave_fmt->channels;
+
 	if (wave_fmt->samplingrate >= 0x2ee00)
 		wave_fmt->samplingrate = 0x2ee00;
 
-	if ((wave_fmt->bitsperchannel != 8) && (wave_fmt->bitsperchannel != 16))
+	wave_fmt->passthrough = 0;
+	do_passthrough = is_ac3 = 0;
+
+	if (card->pt.selected)
+		do_passthrough = 1;
+
+	switch (wave_fmt->id) {
+	case AFMT_S16_LE:
+		wave_fmt->bitsperchannel = 16;
+		break;
+	case AFMT_U8:
+		wave_fmt->bitsperchannel = 8;
+		break;
+	case AFMT_AC3:
+		do_passthrough = 1;
+		is_ac3 = 1;
+		break;
+	default:
+		wave_fmt->id = AFMT_S16_LE;
 		wave_fmt->bitsperchannel = 16;
+		break;
+	}	
+	if (do_passthrough) {
+		i = emu10k1_find_control_gpr(&card->mgr, card->pt.patch_name, card->pt.intr_gpr_name);
+		j = emu10k1_find_control_gpr(&card->mgr, card->pt.patch_name, card->pt.enable_gpr_name);
+		/* currently only one waveout instance may use pass-through */
+		if (i < 0 || j < 0 || woinst->state != WAVE_STATE_CLOSED || 
+		    card->pt.state != PT_STATE_INACTIVE ||
+		    (wave_fmt->samplingrate != 48000 && !is_ac3) ||
+		    (wave_fmt->samplingrate != 48000 && !is_ac3)) {
+			DPF(2, "unable to set pass-through mode\n");
+		} else {
+			wave_fmt->samplingrate = 48000;
+			wave_fmt->channels = 2;
+			wave_fmt->passthrough = 1;
+			card->pt.intr_gpr = i;
+			card->pt.enable_gpr = j;
+			card->pt.state = PT_STATE_INACTIVE;
+			card->pt.pos_gpr = emu10k1_find_control_gpr(&card->mgr, card->pt.patch_name, card->pt.pos_gpr_name);
+			DPD(2, "is_ac3 is %d\n", is_ac3);
+			card->pt.ac3data = is_ac3;
+	                wave_fmt->bitsperchannel = 16;
+		}
+	}
 
 	wave_fmt->bytesperchannel = wave_fmt->bitsperchannel >> 3;
+
+	if (wave_fmt->channels == 2)
+		wave_fmt->bytespervoicesample = wave_fmt->channels * wave_fmt->bytesperchannel;
+	else
+		wave_fmt->bytespervoicesample = wave_fmt->bytesperchannel;
+
 	wave_fmt->bytespersample = wave_fmt->channels * wave_fmt->bytesperchannel;
 	wave_fmt->bytespersec = wave_fmt->bytespersample * wave_fmt->samplingrate;
-
-	return;
 }
 
-static int alloc_buffer(struct emu10k1_card *card, struct waveout_buffer *buffer)
+/**
+ * alloc_buffer -
+ *
+ * allocates the memory buffer for a voice. Two page tables are kept for each buffer.
+ * One (dma_handle) keeps track of the host memory pages used and the other (virtualpagetable)
+ * is passed to the device so that it can do DMA to host memory. 
+ *
+ */
+static int alloc_buffer(struct emu10k1_card *card, struct waveout_buffer *buffer, unsigned int voicenum)
 {
 	u32 pageindex, pagecount;
 	unsigned long busaddx;
@@ -68,59 +134,67 @@
 
 	DPD(2, "requested pages is: %d\n", buffer->pages);
 
-	if ((buffer->emupageindex = emu10k1_addxmgr_alloc(buffer->pages * PAGE_SIZE, card)) < 0)
+	if ((buffer->mem[voicenum].emupageindex =
+	 emu10k1_addxmgr_alloc(buffer->pages * PAGE_SIZE, card)) < 0)
 		return -1;
 
 	/* Fill in virtual memory table */
 	for (pagecount = 0; pagecount < buffer->pages; pagecount++) {
-		if ((buffer->addr[pagecount] = pci_alloc_consistent(card->pci_dev, PAGE_SIZE, &buffer->dma_handle[pagecount])) == NULL) {
+		if ((buffer->mem[voicenum].addr[pagecount] =
+		 pci_alloc_consistent(card->pci_dev, PAGE_SIZE,
+		 &buffer->mem[voicenum].dma_handle[pagecount])) == NULL) {
 			buffer->pages = pagecount;
 			return -1;
 		}
 
-		DPD(2, "Virtual Addx: %p\n", buffer->addr[pagecount]);
+		DPD(2, "Virtual Addx: %p\n", buffer->mem[voicenum].addr[pagecount]);
 
 		for (i = 0; i < PAGE_SIZE / EMUPAGESIZE; i++) {
-			busaddx = buffer->dma_handle[pagecount] + i * EMUPAGESIZE;
+			busaddx = buffer->mem[voicenum].dma_handle[pagecount] + i * EMUPAGESIZE;
 
-			DPD(3, "Bus Addx: %lx\n", busaddx);
+			DPD(3, "Bus Addx: %#lx\n", busaddx);
 
-			pageindex = buffer->emupageindex + pagecount * PAGE_SIZE / EMUPAGESIZE + i;
+			pageindex = buffer->mem[voicenum].emupageindex + pagecount * PAGE_SIZE / EMUPAGESIZE + i;
 
-			((u32 *) card->virtualpagetable.addr)[pageindex] =
-            cpu_to_le32((busaddx * 2) | pageindex);
+			((u32 *) card->virtualpagetable.addr)[pageindex] = cpu_to_le32((busaddx * 2) | pageindex);
 		}
 	}
 
 	return 0;
 }
 
-static void free_buffer(struct emu10k1_card *card, struct waveout_buffer *buffer)
+/**
+ * free_buffer -
+ *
+ * frees the memory buffer for a voice.
+ */
+static void free_buffer(struct emu10k1_card *card, struct waveout_buffer *buffer, unsigned int voicenum)
 {
 	u32 pagecount, pageindex;
 	int i;
 
-	if (buffer->emupageindex < 0)
+	if (buffer->mem[voicenum].emupageindex < 0)
 		return;
 
 	for (pagecount = 0; pagecount < buffer->pages; pagecount++) {
-		pci_free_consistent(card->pci_dev, PAGE_SIZE, buffer->addr[pagecount], buffer->dma_handle[pagecount]);
+		pci_free_consistent(card->pci_dev, PAGE_SIZE,
+		 buffer->mem[voicenum].addr[pagecount],
+		 buffer->mem[voicenum].dma_handle[pagecount]);
 
 		for (i = 0; i < PAGE_SIZE / EMUPAGESIZE; i++) {
-			pageindex = buffer->emupageindex + pagecount * PAGE_SIZE / EMUPAGESIZE + i;
-			((u32 *) card->virtualpagetable.addr)[pageindex] = (card->silentpage.dma_handle * 2) | pageindex;
+			pageindex = buffer->mem[voicenum].emupageindex + pagecount * PAGE_SIZE / EMUPAGESIZE + i;
+			((u32 *) card->virtualpagetable.addr)[pageindex] =
+			 cpu_to_le32((card->silentpage.dma_handle * 2) | pageindex);
 		}
 	}
 
-	emu10k1_addxmgr_free(card, buffer->emupageindex);
-	buffer->emupageindex = -1;
-
-	return;
+	emu10k1_addxmgr_free(card, buffer->mem[voicenum].emupageindex);
+	buffer->mem[voicenum].emupageindex = -1;
 }
 
-static int get_voice(struct emu10k1_card *card, struct woinst *woinst)
+static int get_voice(struct emu10k1_card *card, struct woinst *woinst, unsigned int voicenum)
 {
-	struct emu_voice *voice = &woinst->voice;
+	struct emu_voice *voice = &woinst->voice[voicenum];
 	/* Allocate voices here, if no voices available, return error.
 	 * Init voice_allocdesc first.*/
 
@@ -134,17 +208,20 @@
 	if (woinst->format.bitsperchannel == 16)
 		voice->flags |= VOICE_FLAGS_16BIT;
 
-	if (emu10k1_voice_alloc(card, voice) < 0)
+	if (emu10k1_voice_alloc(card, voice) < 0) {
+		voice->usage = VOICE_USAGE_FREE;
 		return -1;
+	}
 
 	/* Calculate pitch */
 	voice->initial_pitch = (u16) (srToPitch(woinst->format.samplingrate) >> 8);
 	voice->pitch_target = samplerate_to_linearpitch(woinst->format.samplingrate);
 
-	DPD(2, "Initial pitch --> 0x%x\n", voice->initial_pitch);
+	DPD(2, "Initial pitch --> %#x\n", voice->initial_pitch);
 
-	voice->startloop = (woinst->buffer.emupageindex << 12) / woinst->format.bytespersample;
-	voice->endloop = voice->startloop + woinst->buffer.size / woinst->format.bytespersample;
+	voice->startloop = (woinst->buffer.mem[voicenum].emupageindex << 12) /
+	 woinst->format.bytespervoicesample;
+	voice->endloop = voice->startloop + woinst->buffer.size / woinst->format.bytespervoicesample;
 	voice->start = voice->startloop;
 
 	if (voice->flags & VOICE_FLAGS_STEREO) {
@@ -154,7 +231,7 @@
 		voice->params[0].send_d = card->waveout.send_d[1];
 
 		if (woinst->device)
-			voice->params[0].send_routing = 0xd23c;
+			voice->params[0].send_routing = 0x7654;
 		else
 			voice->params[0].send_routing = card->waveout.send_routing[1];
 
@@ -170,7 +247,7 @@
 		voice->params[1].send_d = card->waveout.send_d[2];
 
 		if (woinst->device)
-			voice->params[1].send_routing = 0xd23c;
+			voice->params[1].send_routing = 0x7654;
 		else
 			voice->params[1].send_routing = card->waveout.send_routing[2];
 
@@ -180,15 +257,25 @@
 		voice->params[1].byampl_env_sustain = 0x7f;
 		voice->params[1].byampl_env_decay = 0x7f;
 	} else {
-		voice->params[0].send_a = card->waveout.send_a[0];
-		voice->params[0].send_b = card->waveout.send_b[0];
-		voice->params[0].send_c = card->waveout.send_c[0];
-		voice->params[0].send_d = card->waveout.send_d[0];
-
-		if (woinst->device)
-                        voice->params[0].send_routing = 0xd23c;
-                else
-			voice->params[0].send_routing = card->waveout.send_routing[0];
+		if (woinst->num_voices > 1) {
+			voice->params[0].send_a = 0xff;
+			voice->params[0].send_b = 0;
+			voice->params[0].send_c = 0;
+			voice->params[0].send_d = 0;
+
+			voice->params[0].send_routing =
+			 0xfff0 + card->mchannel_fx + voicenum;
+		} else {
+			voice->params[0].send_a = card->waveout.send_a[0];
+			voice->params[0].send_b = card->waveout.send_b[0];
+			voice->params[0].send_c = card->waveout.send_c[0];
+			voice->params[0].send_d = card->waveout.send_d[0];
+
+			if (woinst->device)
+				voice->params[0].send_routing = 0x7654;
+			else
+				voice->params[0].send_routing = card->waveout.send_routing[0];
+		}	
 
 		voice->params[0].volume_target = 0xffff;
 		voice->params[0].initial_fc = 0xff;
@@ -197,7 +284,7 @@
 		voice->params[0].byampl_env_decay = 0x7f;
 	}
 
-	DPD(2, "voice: startloop=0x%x, endloop=0x%x\n", voice->startloop, voice->endloop);
+	DPD(2, "voice: startloop=%#x, endloop=%#x\n", voice->startloop, voice->endloop);
 
 	emu10k1_voice_playback_setup(voice);
 
@@ -208,29 +295,34 @@
 {
 	struct emu10k1_card *card = wave_dev->card;
 	struct woinst *woinst = wave_dev->woinst;
+	struct waveout_buffer *buffer = &woinst->buffer;
+	unsigned int voicenum;
 	u32 delay;
 
 	DPF(2, "emu10k1_waveout_open()\n");
 
-	if (alloc_buffer(card, &woinst->buffer) < 0) {
-		ERROR();
-		emu10k1_waveout_close(wave_dev);
-		return -1;
-	}
+	for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
+		if (alloc_buffer(card, buffer, voicenum) < 0) {
+			ERROR();
+			emu10k1_waveout_close(wave_dev);
+			return -1;
+		}
 
-	woinst->buffer.fill_silence = 0;
-	woinst->buffer.silence_bytes = 0;
-	woinst->buffer.silence_pos = 0;
-	woinst->buffer.hw_pos = 0;
-	woinst->buffer.bytestocopy = woinst->buffer.size;
-
-	if (get_voice(card, woinst) < 0) {
-		ERROR();
-		emu10k1_waveout_close(wave_dev);
-		return -1;
+		if (get_voice(card, woinst, voicenum) < 0) {
+			ERROR();
+			emu10k1_waveout_close(wave_dev);
+			return -1;
+		}
 	}
 
-	delay = (48000 * woinst->buffer.fragment_size) / woinst->format.bytespersec;
+	buffer->fill_silence = 0;
+	buffer->silence_bytes = 0;
+	buffer->silence_pos = 0;
+	buffer->hw_pos = 0;
+	buffer->free_bytes = woinst->buffer.size;
+
+	delay = (48000 * woinst->buffer.fragment_size) /
+		 (woinst->format.samplingrate * woinst->format.bytespervoicesample);
 
 	emu10k1_timer_install(card, &woinst->timer, delay / 2);
 
@@ -243,6 +335,7 @@
 {
 	struct emu10k1_card *card = wave_dev->card;
 	struct woinst *woinst = wave_dev->woinst;
+	unsigned int voicenum;
 
 	DPF(2, "emu10k1_waveout_close()\n");
 
@@ -250,13 +343,12 @@
 
 	emu10k1_timer_uninstall(card, &woinst->timer);
 
-	emu10k1_voice_free(&woinst->voice);
-
-	free_buffer(card, &woinst->buffer);
+	for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
+		emu10k1_voice_free(&woinst->voice[voicenum]);
+		free_buffer(card, &woinst->buffer, voicenum);
+	}
 
 	woinst->state = WAVE_STATE_CLOSED;
-
-	return;
 }
 
 void emu10k1_waveout_start(struct emu10k1_wavedevice *wave_dev)
@@ -265,21 +357,20 @@
 	struct woinst *woinst = wave_dev->woinst;
 
 	DPF(2, "emu10k1_waveout_start()\n");
-	/* Actual start */
 
-	emu10k1_voice_start(&woinst->voice, woinst->total_played);
+	/* Actual start */
+	emu10k1_voices_start(woinst->voice, woinst->num_voices, woinst->total_played);
 
 	emu10k1_timer_enable(card, &woinst->timer);
 
 	woinst->state |= WAVE_STATE_STARTED;
-
-	return;
 }
 
 int emu10k1_waveout_setformat(struct emu10k1_wavedevice *wave_dev, struct wave_format *format)
 {
 	struct emu10k1_card *card = wave_dev->card;
 	struct woinst *woinst = wave_dev->woinst;
+	unsigned int voicenum;
 	u32 delay;
 
 	DPF(2, "emu10k1_waveout_setformat()\n");
@@ -287,7 +378,7 @@
 	if (woinst->state & WAVE_STATE_STARTED)
 		return -1;
 
-	query_format(format);
+	query_format(wave_dev, format);
 
 	if (woinst->format.samplingrate != format->samplingrate ||
 	    woinst->format.channels != format->channels ||
@@ -299,15 +390,19 @@
 			return 0;
 
 		emu10k1_timer_uninstall(card, &woinst->timer);
-		emu10k1_voice_free(&woinst->voice);
 
-		if (get_voice(card, woinst) < 0) {
-			ERROR();
-			emu10k1_waveout_close(wave_dev);
-			return -1;
+		for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
+			emu10k1_voice_free(&woinst->voice[voicenum]);
+
+			if (get_voice(card, woinst, voicenum) < 0) {
+				ERROR();
+				emu10k1_waveout_close(wave_dev);
+				return -1;
+			}
 		}
 
-		delay = (48000 * woinst->buffer.fragment_size) / woinst->format.bytespersec;
+		delay = (48000 * woinst->buffer.fragment_size) /
+			 (woinst->format.samplingrate * woinst->format.bytespervoicesample);
 
 		emu10k1_timer_install(card, &woinst->timer, delay / 2);
 	}
@@ -327,93 +422,159 @@
 
 	emu10k1_timer_disable(card, &woinst->timer);
 
-	/* Stop actual voice */
-	emu10k1_voice_stop(&woinst->voice);
+	/* Stop actual voices */
+	emu10k1_voices_stop(woinst->voice, woinst->num_voices);
 
 	emu10k1_waveout_update(woinst);
 
 	woinst->state &= ~WAVE_STATE_STARTED;
-
-	return;
 }
 
-void emu10k1_waveout_getxfersize(struct woinst *woinst, u32 * size)
+/**
+ * emu10k1_waveout_getxfersize -
+ *
+ * gives the total free bytes on the voice buffer, including silence bytes
+ * (basically: total_free_bytes = free_bytes + silence_bytes).
+ *
+ */
+void emu10k1_waveout_getxfersize(struct woinst *woinst, u32 *total_free_bytes)
 {
 	struct waveout_buffer *buffer = &woinst->buffer;
-	int pending;
+	int pending_bytes;
 
 	if (woinst->mmapped) {
-		*size = buffer->bytestocopy;
+		*total_free_bytes = buffer->free_bytes;
 		return;
 	}
 
-	pending = buffer->size - buffer->bytestocopy;
+	pending_bytes = buffer->size - buffer->free_bytes;
 
-	buffer->fill_silence = (pending < (signed) buffer->fragment_size) ? 1 : 0;
+	buffer->fill_silence = (pending_bytes < (signed) buffer->fragment_size) ? 1 : 0;
 
-	if (pending > (signed) buffer->silence_bytes) {
-		*size = buffer->bytestocopy + buffer->silence_bytes;
+	if (pending_bytes > (signed) buffer->silence_bytes) {
+		*total_free_bytes = (buffer->free_bytes + buffer->silence_bytes);
 	} else {
-		*size = buffer->size;
-		buffer->silence_bytes = pending;
-		if (pending < 0) {
+		*total_free_bytes = buffer->size;
+		buffer->silence_bytes = pending_bytes;
+		if (pending_bytes < 0) {
 			buffer->silence_pos = buffer->hw_pos;
 			buffer->silence_bytes = 0;
-			buffer->bytestocopy = buffer->size;
+			buffer->free_bytes = buffer->size;
 			DPF(1, "buffer underrun\n");
 		}
 	}
-
-	return;
 }
 
+/**
+ * copy_block -
+ *
+ * copies a block of pcm data to a voice buffer.
+ * Notice that the voice buffer is actually a set of disjointed memory pages.
+ *
+ */
 static void copy_block(void **dst, u32 str, u8 *src, u32 len)
 {
-	int i, j, k;
-
-	i = str / PAGE_SIZE;
-	j = str % PAGE_SIZE;
-
-	if (len > PAGE_SIZE - j) {
-		k = PAGE_SIZE - j;
-		copy_from_user(dst[i] + j, src, k);
+	unsigned int pg;
+	unsigned int pgoff;
+	unsigned int k;
+
+	pg = str / PAGE_SIZE;
+	pgoff = str % PAGE_SIZE;
+
+	if (len > PAGE_SIZE - pgoff) {
+		k = PAGE_SIZE - pgoff;
+		__copy_from_user((u8 *)dst[pg] + pgoff, src, k);
 		len -= k;
 		while (len > PAGE_SIZE) {
-                	copy_from_user(dst[++i], src + k, PAGE_SIZE);
-                	k += PAGE_SIZE;
-                	len -= PAGE_SIZE;
-        	}
-        	copy_from_user(dst[++i], src + k, len);
+			__copy_from_user(dst[++pg], src + k, PAGE_SIZE);
+			k += PAGE_SIZE;
+			len -= PAGE_SIZE;
+		}
+		__copy_from_user(dst[++pg], src + k, len);
 
 	} else
-		copy_from_user(dst[i] + j, src, len);
-
-	return;
+		__copy_from_user((u8 *)dst[pg] + pgoff, src, len);
 }
 
-static void fill_block(void **dst, u32 str, u8 src, u32 len)
-{
-	int i, j, k;
+/**
+ * copy_ilv_block -
+ *
+ * copies a block of pcm data containing n interleaved channels to n mono voice buffers.
+ * Notice that the voice buffer is actually a set of disjointed memory pages.
+ *
+ */
+static void copy_ilv_block(struct woinst *woinst, u32 str, u8 *src, u32 len) 
+{
+        unsigned int pg;
+	unsigned int pgoff;
+	unsigned int voice_num;
+	struct waveout_mem *mem = woinst->buffer.mem;
+
+	pg = str / PAGE_SIZE;
+	pgoff = str % PAGE_SIZE;
+
+	while (len) { 
+		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++) {
+			__copy_from_user((u8 *)(mem[voice_num].addr[pg]) + pgoff, src, woinst->format.bytespervoicesample);
+			src += woinst->format.bytespervoicesample;
+		}
 
-	i = str / PAGE_SIZE;
-	j = str % PAGE_SIZE;
+		len -= woinst->format.bytespervoicesample;
+
+		pgoff += woinst->format.bytespervoicesample;
+		if (pgoff >= PAGE_SIZE) {
+			pgoff = 0;
+			pg++;
+		}
+	}
+}
 
-	if (len > PAGE_SIZE - j) {
-                k = PAGE_SIZE - j;
-                memset(dst[i] + j, src, k);
-                len -= k;
-                while (len > PAGE_SIZE) {
-                        memset(dst[++i], src, PAGE_SIZE);
-                        len -= PAGE_SIZE;
-                }
-                memset(dst[++i], src, len);
+/**
+ * fill_block -
+ *
+ * fills a set voice buffers with a block of a given sample.
+ *
+ */
+static void fill_block(struct woinst *woinst, u32 str, u8 data, u32 len)
+{
+	unsigned int pg;
+	unsigned int pgoff;
+	unsigned int voice_num;
+        struct waveout_mem *mem = woinst->buffer.mem;
+	unsigned int  k;
+
+	pg = str / PAGE_SIZE;
+	pgoff = str % PAGE_SIZE;
+
+	if (len > PAGE_SIZE - pgoff) {
+		k = PAGE_SIZE - pgoff;
+		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
+			memset((u8 *)mem[voice_num].addr[pg] + pgoff, data, k);
+		len -= k;
+		while (len > PAGE_SIZE) {
+			pg++;
+			for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
+				memset(mem[voice_num].addr[pg], data, PAGE_SIZE);
 
-        } else
-                memset(dst[i] + j, src, len);
+			len -= PAGE_SIZE;
+		}
+		pg++;
+		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
+			memset(mem[voice_num].addr[pg], data, len);
 
-	return;
+	} else {
+		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
+			memset((u8 *)mem[voice_num].addr[pg] + pgoff, data, len);
+	}
 }
 
+/**
+ * emu10k1_waveout_xferdata -
+ *
+ * copies pcm data to the voice buffer. Silence samples
+ * previously added to the buffer are overwritten.
+ *
+ */
 void emu10k1_waveout_xferdata(struct woinst *woinst, u8 *data, u32 *size)
 {
 	struct waveout_buffer *buffer = &woinst->buffer;
@@ -425,91 +586,108 @@
 
 	if (!sizetocopy)
 		return;
-
+	
 	spin_lock_irqsave(&woinst->lock, flags);
 	start = (buffer->size + buffer->silence_pos - buffer->silence_bytes) % buffer->size;
 
-	if(sizetocopy > buffer->silence_bytes) {
+	if (sizetocopy > buffer->silence_bytes) {
 		buffer->silence_pos += sizetocopy - buffer->silence_bytes;
-		buffer->bytestocopy -= sizetocopy - buffer->silence_bytes;
+		buffer->free_bytes -= sizetocopy - buffer->silence_bytes;
 		buffer->silence_bytes = 0;
 	} else
 		buffer->silence_bytes -= sizetocopy;
 
-	sizetocopy_now = buffer->size - start;
-
 	spin_unlock_irqrestore(&woinst->lock, flags);
 
+	sizetocopy_now = buffer->size - start;
 	if (sizetocopy > sizetocopy_now) {
 		sizetocopy -= sizetocopy_now;
-		copy_block(buffer->addr, start, data, sizetocopy_now);
-		copy_block(buffer->addr, 0, data + sizetocopy_now, sizetocopy);
+		if (woinst->num_voices > 1) {
+			copy_ilv_block(woinst, start, data, sizetocopy_now);
+			copy_ilv_block(woinst, 0, data + sizetocopy_now * woinst->num_voices, sizetocopy);
+		} else {
+			copy_block(buffer->mem[0].addr, start, data, sizetocopy_now);
+			copy_block(buffer->mem[0].addr, 0, data + sizetocopy_now, sizetocopy);
+		}
 	} else {
-		copy_block(buffer->addr, start, data, sizetocopy);
+		if (woinst->num_voices > 1)
+			copy_ilv_block(woinst, start, data, sizetocopy);
+		else
+			copy_block(buffer->mem[0].addr, start, data, sizetocopy);
 	}
-
-	return;
 }
 
+/**
+ * emu10k1_waveout_fillsilence -
+ *
+ * adds samples of silence to the voice buffer so that we
+ * don't loop over stale pcm data.
+ *
+ */
 void emu10k1_waveout_fillsilence(struct woinst *woinst)
 {
 	struct waveout_buffer *buffer = &woinst->buffer;
-	u16 filldata;
 	u32 sizetocopy, sizetocopy_now, start;
+	u8 filldata;
 	unsigned long flags;
 
-	sizetocopy = woinst->buffer.fragment_size;
+	sizetocopy = buffer->fragment_size;
 
 	if (woinst->format.bitsperchannel == 16)
-		filldata = 0x0000;
+		filldata = 0x00;
 	else
-		filldata = 0x8080;
+		filldata = 0x80;
 
 	spin_lock_irqsave(&woinst->lock, flags);
 	buffer->silence_bytes += sizetocopy;
-	buffer->bytestocopy -= sizetocopy;
+	buffer->free_bytes -= sizetocopy;
 	buffer->silence_pos %= buffer->size;
 	start = buffer->silence_pos;
 	buffer->silence_pos += sizetocopy;
-	sizetocopy_now = buffer->size - start;
-
 	spin_unlock_irqrestore(&woinst->lock, flags);
 
+	sizetocopy_now = buffer->size - start;
+
 	if (sizetocopy > sizetocopy_now) {
 		sizetocopy -= sizetocopy_now;
-		fill_block(buffer->addr, start, filldata, sizetocopy_now);
-		fill_block(buffer->addr, 0, filldata, sizetocopy);
+		fill_block(woinst, start, filldata, sizetocopy_now);
+		fill_block(woinst, 0, filldata, sizetocopy);
 	} else {
-		fill_block(buffer->addr, start, filldata, sizetocopy);
+		fill_block(woinst, start, filldata, sizetocopy);
 	}
-
-	return;
 }
 
+/**
+ * emu10k1_waveout_update -
+ *
+ * updates the position of the voice buffer hardware pointer (hw_pos)
+ * and the number of free bytes on the buffer (free_bytes).
+ * The free bytes _don't_ include silence bytes that may have been
+ * added to the buffer.
+ *
+ */
 void emu10k1_waveout_update(struct woinst *woinst)
 {
 	u32 hw_pos;
 	u32 diff;
-
+	
 	/* There is no actual start yet */
 	if (!(woinst->state & WAVE_STATE_STARTED)) {
 		hw_pos = woinst->buffer.hw_pos;
 	} else {
 		/* hw_pos in sample units */
-		hw_pos = sblive_readptr(woinst->voice.card, CCCA_CURRADDR, woinst->voice.num);
+		hw_pos = sblive_readptr(woinst->voice[0].card, CCCA_CURRADDR, woinst->voice[0].num);
 
-		if(hw_pos < woinst->voice.start)
-			hw_pos += woinst->buffer.size / woinst->format.bytespersample - woinst->voice.start;
+		if(hw_pos < woinst->voice[0].start)
+			hw_pos += woinst->buffer.size / woinst->format.bytespervoicesample - woinst->voice[0].start;
 		else
-			hw_pos -= woinst->voice.start;
+			hw_pos -= woinst->voice[0].start;
 
-		hw_pos *= woinst->format.bytespersample;
+		hw_pos *= woinst->format.bytespervoicesample;
 	}
 
 	diff = (woinst->buffer.size + hw_pos - woinst->buffer.hw_pos) % woinst->buffer.size;
 	woinst->total_played += diff;
-	woinst->buffer.bytestocopy += diff;
+	woinst->buffer.free_bytes += diff;
 	woinst->buffer.hw_pos = hw_pos;
-
-	return;
 }
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/cardwo.h linux/drivers/sound/emu10k1/cardwo.h
--- linux-2.4.8-pre8/drivers/sound/emu10k1/cardwo.h	Mon Dec 11 13:02:19 2000
+++ linux/drivers/sound/emu10k1/cardwo.h	Mon Dec  3 23:00:45 2001
@@ -45,35 +45,42 @@
 #define WAVEOUT_DEFAULTBUFLEN	500 /* Time to play the entire buffer in ms */
 
 #define WAVEOUT_MINFRAGSHIFT	6
+#define WAVEOUT_MAXVOICES 6
+
+/* waveout_mem is cardwo internal */
+struct waveout_mem {
+	int emupageindex;
+	void *addr[BUFMAXPAGES];
+	dma_addr_t dma_handle[BUFMAXPAGES];
+};
 
 struct waveout_buffer {
 	u16 ossfragshift;
-        u32 numfrags;
+	u32 numfrags;
 	u32 fragment_size;	/* in bytes units */
 	u32 size;		/* in bytes units */
 	u32 pages;		/* buffer size in page units*/
-	int emupageindex;
-	void *addr[BUFMAXPAGES];
-	dma_addr_t dma_handle[BUFMAXPAGES];
-        u32 silence_pos;	/* software cursor position (including silence) */
+	struct waveout_mem mem[WAVEOUT_MAXVOICES];
+	u32 silence_pos;	/* software cursor position (including silence bytes) */
 	u32 hw_pos;		/* hardware cursor position */
-	u32 bytestocopy;	/* free space on buffer (including silence) */
+	u32 free_bytes;		/* free bytes available on the buffer (not including silence bytes) */
 	u8 fill_silence;
-	u32 silence_bytes;      /* silence bytes in buffer */
+	u32 silence_bytes;      /* silence bytes on the buffer */
 };
 
 struct woinst 
 {
 	u8 state;
-	struct emu_voice voice;
+	u8 num_voices;
+	struct emu_voice voice[WAVEOUT_MAXVOICES];
 	struct emu_timer timer;
-        struct wave_format format;
+	struct wave_format format;
 	struct waveout_buffer buffer;
-        wait_queue_head_t wait_queue;
-        u8 mmapped;
-        u32 total_copied;	/* total number of bytes written() to the buffer (excluding silence) */
-        u32 total_played;	/* total number of bytes played including silence */
-        u32 blocks;
+	wait_queue_head_t wait_queue;
+	u8 mmapped;
+	u32 total_copied;	/* total number of bytes written() to the buffer (excluding silence) */
+	u32 total_played;	/* total number of bytes played including silence */
+	u32 blocks;
 	u8 device;
 	spinlock_t lock;
 };
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/ecard.c linux/drivers/sound/emu10k1/ecard.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/ecard.c	Mon Aug 14 08:32:48 2000
+++ linux/drivers/sound/emu10k1/ecard.c	Mon Dec  3 23:00:45 2001
@@ -83,10 +83,13 @@
 {
 	u16 count;
 	u32 data, hcvalue;
+	unsigned long flags;
 
-	hcvalue = emu10k1_readfn0(card, HCFG) & ~(HOOKN_BIT|HANDN_BIT|PULSEN_BIT);
+	spin_lock_irqsave(&card->lock, flags);
 
-	emu10k1_writefn0(card, HCFG, hcvalue);
+	hcvalue = inl(card->iobase + HCFG) & ~(HOOKN_BIT|HANDN_BIT|PULSEN_BIT);
+
+	outl(card->iobase + HCFG, hcvalue);
 
 	for (count = 0 ; count < EC_NUM_CONTROL_BITS; count++) {
 	
@@ -94,19 +97,21 @@
 		data = ((value & 0x1) ? PULSEN_BIT : 0);
 		value >>= 1;
 
-		emu10k1_writefn0(card, HCFG, hcvalue | data);
+		outl(card->iobase + HCFG, hcvalue | data);
 
 		/* Clock the shift register */
-		emu10k1_writefn0(card, HCFG, hcvalue | data | HANDN_BIT);
-		emu10k1_writefn0(card, HCFG, hcvalue | data);
+		outl(card->iobase + HCFG, hcvalue | data | HANDN_BIT);
+		outl(card->iobase + HCFG, hcvalue | data);
 	}
 
 	/* Latch the bits */
-	emu10k1_writefn0(card, HCFG, hcvalue | HOOKN_BIT);
-	emu10k1_writefn0(card, HCFG, hcvalue);
+	outl(card->iobase + HCFG, hcvalue | HOOKN_BIT);
+	outl(card->iobase + HCFG, hcvalue);
+
+	spin_unlock_irqrestore(&card->lock, flags);
 }
 
-int __devinit emu10k1_ecard_init(struct emu10k1_card *card)
+void __devinit emu10k1_ecard_init(struct emu10k1_card *card)
 {
 	u32 hcvalue;
 	struct ecard_state ecard;
@@ -125,7 +130,6 @@
 	 * and enable audio output */
 	hcvalue = emu10k1_readfn0(card, HCFG);
 	emu10k1_writefn0(card, HCFG, hcvalue | HCFG_AUDIOENABLE | HCFG_CODECFORMAT_I2S);
-	emu10k1_readfn0(card, HCFG);
 
 	/* Step 1: Turn off the led and deassert TRIM_CS */
 	ecard_write(card, EC_ADCCAL | EC_LEDN | EC_TRIM_CSN);
@@ -148,8 +152,6 @@
 
 	/* Step 5: Set the analog input gain */
 	ecard_setadcgain(card, &ecard, ecard.adc_gain);
-	
-	return 0;
 }
 
 
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/ecard.h linux/drivers/sound/emu10k1/ecard.h
--- linux-2.4.8-pre8/drivers/sound/emu10k1/ecard.h	Sun Feb  4 10:05:29 2001
+++ linux/drivers/sound/emu10k1/ecard.h	Mon Dec  3 23:00:45 2001
@@ -29,7 +29,6 @@
 #include "8010.h"
 #include "hwaccess.h"
 #include <linux/init.h>
-#include <linux/sched.h>
 
 /* In A1 Silicon, these bits are in the HC register */
 #define HOOKN_BIT   (1L << 12)
@@ -109,6 +108,6 @@
 	u16 mux2_setting;
 };
 
-int emu10k1_ecard_init(struct emu10k1_card *) __devinit;
+void emu10k1_ecard_init(struct emu10k1_card *) __devinit;
 
 #endif /* _ECARD_H */
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/efxmgr.c linux/drivers/sound/emu10k1/efxmgr.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/efxmgr.c	Wed Dec 31 16:00:00 1969
+++ linux/drivers/sound/emu10k1/efxmgr.c	Mon Dec  3 23:00:45 2001
@@ -0,0 +1,211 @@
+/*     
+ **********************************************************************
+ *     efxmgr.c
+ *     Copyright 1999, 2000 Creative Labs, Inc. 
+ * 
+ ********************************************************************** 
+ * 
+ *     Date                 Author          Summary of changes 
+ *     ----                 ------          ------------------ 
+ *     October 20, 1999     Bertrand Lee    base code release 
+ * 
+ ********************************************************************** 
+ * 
+ *     This program is free software; you can redistribute it and/or 
+ *     modify it under the terms of the GNU General Public License as 
+ *     published by the Free Software Foundation; either version 2 of 
+ *     the License, or (at your option) any later version. 
+ * 
+ *     This program is distributed in the hope that it will be useful, 
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ *     GNU General Public License for more details. 
+ * 
+ *     You should have received a copy of the GNU General Public 
+ *     License along with this program; if not, write to the Free 
+ *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 
+ *     USA. 
+ * 
+ ********************************************************************** 
+ */
+
+#include <linux/bitops.h>
+#include "hwaccess.h"
+#include "efxmgr.h"
+
+int emu10k1_find_control_gpr(struct patch_manager *mgr, const char *patch_name, const char *gpr_name)
+{
+        struct dsp_patch *patch;
+	struct dsp_rpatch *rpatch;
+	char s[PATCH_NAME_SIZE + 4];
+	u32 *gpr_used;
+	int i;
+
+	DPD(2, "emu10k1_find_control_gpr(): %s %s\n", patch_name, gpr_name);
+
+	rpatch = &mgr->rpatch;
+	if (!strcmp(rpatch->name, patch_name)) {
+		gpr_used = rpatch->gpr_used;
+		goto match;
+	}
+
+	for(i = 0; i < mgr->current_pages * PATCHES_PER_PAGE; i++) {
+		patch = PATCH(mgr, i);
+			sprintf(s,"%s", patch->name);
+
+		if (!strcmp(s, patch_name)) {
+			gpr_used = patch->gpr_used;
+			goto match;
+		}
+	}
+
+	return -1;
+
+  match:
+	for (i = 0; i < NUM_GPRS; i++)
+		if (mgr->gpr[i].type == GPR_TYPE_CONTROL &&
+		    test_bit(i, gpr_used) &&
+		    !strcmp(mgr->gpr[i].name, gpr_name))
+			return i;
+
+	return -1;
+}
+
+void emu10k1_set_control_gpr(struct emu10k1_card *card, int addr, s32 val, int flag)
+{
+	struct patch_manager *mgr = &card->mgr;
+
+	DPD(2, "emu10k1_set_control_gpr(): %d %x\n", addr, val);
+
+	if (addr < 0 || addr >= NUM_GPRS)
+		return;
+
+	if (flag)
+		val += sblive_readptr(card, GPR_BASE + addr, 0);
+
+	if (val > mgr->gpr[addr].max)
+		val = mgr->gpr[addr].max;
+	else if (val < mgr->gpr[addr].min)
+		val = mgr->gpr[addr].min;
+
+	sblive_writeptr(card, GPR_BASE + addr, 0, val);
+}
+
+//TODO: make this configurable:
+#define VOLCTRL_CHANNEL SOUND_MIXER_VOLUME
+#define VOLCTRL_STEP_SIZE        5
+
+//An internal function for setting OSS mixer controls.
+void emu10k1_set_oss_vol(struct emu10k1_card *card, int oss_mixer,
+			 unsigned int left, unsigned int right){
+
+	extern struct oss_scaling volume_params[SOUND_MIXER_NRDEVICES];
+
+	card->ac97.mixer_state[oss_mixer] = (right << 8) | left;
+
+	if (!card->isaps)
+		card->ac97.write_mixer(&card->ac97, oss_mixer, left, right);
+
+	
+	emu10k1_set_volume_gpr(card, card->mgr.ctrl_gpr[oss_mixer][0], left,
+			       volume_params[oss_mixer].scale,
+			       volume_params[oss_mixer].muting);
+	emu10k1_set_volume_gpr(card, card->mgr.ctrl_gpr[oss_mixer][1], right,
+			       volume_params[oss_mixer].scale,
+			       volume_params[oss_mixer].muting);
+
+}
+
+
+//FIXME: mute should unmute when pressed a second time
+void emu10k1_mute_irqhandler(struct emu10k1_card *card)
+{
+	struct patch_manager *mgr = &card->mgr;
+	unsigned long  flags;
+
+	spin_lock_irqsave(&mgr->lock, flags);
+	emu10k1_set_oss_vol(card,VOLCTRL_CHANNEL,0,0);
+	spin_unlock_irqrestore(&mgr->lock, flags);
+}
+
+void emu10k1_volincr_irqhandler(struct emu10k1_card *card)
+{
+	struct patch_manager *mgr = &card->mgr;
+	unsigned long  flags;
+	unsigned int oss_channel=VOLCTRL_CHANNEL, left=0,right=0;
+
+	spin_lock_irqsave(&mgr->lock, flags);
+	left = card->ac97.mixer_state[oss_channel] & 0xff;
+	right = (card->ac97.mixer_state[oss_channel] >> 8) & 0xff;
+
+	if((left+=VOLCTRL_STEP_SIZE )>100)
+		left=100;
+	if((right+=VOLCTRL_STEP_SIZE )>100)
+		right=100;
+	emu10k1_set_oss_vol(card,oss_channel,left,right);
+	spin_unlock_irqrestore(&mgr->lock, flags);	
+}
+void emu10k1_voldecr_irqhandler(struct emu10k1_card *card)
+{
+	struct patch_manager *mgr = &card->mgr;
+	unsigned long  flags;
+	int oss_channel=VOLCTRL_CHANNEL, left=0,right=0;
+
+	spin_lock_irqsave(&mgr->lock, flags);
+	left = card->ac97.mixer_state[oss_channel] & 0xff;
+	right = (card->ac97.mixer_state[oss_channel] >> 8) & 0xff;
+
+	if((left-=VOLCTRL_STEP_SIZE )<0)
+		left=0;
+	if((right-=VOLCTRL_STEP_SIZE )<0)
+		right=0;
+	emu10k1_set_oss_vol(card,oss_channel,left,right);
+	spin_unlock_irqrestore(&mgr->lock, flags);
+}
+
+
+void emu10k1_set_volume_gpr(struct emu10k1_card *card, int addr, s32 vol, int scale, int muting)
+{
+	struct patch_manager *mgr = &card->mgr;
+	unsigned long flags;
+
+	const s32 log2lin[5] ={                  //  attenuation (dB)
+		0x7fffffff,                      //       0.0         
+		0x7fffffff * 0.840896415253715 , //       1.5          
+		0x7fffffff * 0.707106781186548,  //       3.0
+		0x7fffffff * 0.594603557501361 , //       4.5
+	};
+
+	if (addr < 0)
+		return;
+
+	vol = (100 - vol ) * scale / 100;
+
+	// Thanks to the comp.dsp newsgroup for this neat trick:
+	vol = vol >= muting ? 0: log2lin[vol&3]>>(vol>>2);
+
+	spin_lock_irqsave(&mgr->lock, flags);
+	emu10k1_set_control_gpr(card, addr, vol, 0);
+	spin_unlock_irqrestore(&mgr->lock, flags);
+}
+
+void emu10k1_dsp_irqhandler(struct emu10k1_card *card)
+{
+	unsigned long flags;
+
+	if (card->pt.state != PT_STATE_INACTIVE) {
+		u32 bc;
+		bc = sblive_readptr(card, GPR_BASE + card->pt.intr_gpr, 0);
+		if (bc != 0) {
+			spin_lock_irqsave(&card->lock, flags);
+			card->pt.blocks_played = bc;
+			if (card->pt.blocks_played >= card->pt.blocks_copied) {
+				DPF(1, "buffer underrun in passthrough playback\n");
+				emu10k1_pt_stop(card);
+			}
+			wake_up_interruptible(&card->pt.wait);
+			spin_unlock_irqrestore(&card->lock, flags);
+			DPD(3, "pt interrupt, bc = %d\n", bc);
+		}
+	}
+}
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/efxmgr.h linux/drivers/sound/emu10k1/efxmgr.h
--- linux-2.4.8-pre8/drivers/sound/emu10k1/efxmgr.h	Fri Apr 21 16:08:45 2000
+++ linux/drivers/sound/emu10k1/efxmgr.h	Mon Dec  3 23:00:45 2001
@@ -39,5 +39,204 @@
         WRITE_EFX(card, (pc) * 2 + 1, ((op) << 20) | ((z) << 10) | (w)); \
         ++pc; } while (0)
 
+#define NUM_INPUTS 0x20
+#define NUM_OUTPUTS 0x20
+#define NUM_GPRS 0x100
+#define GPR_NAME_SIZE   32
+#define PATCH_NAME_SIZE 32
+
+struct dsp_rpatch {
+	char name[PATCH_NAME_SIZE];
+        u16 code_start;
+        u16 code_size;
+
+        u32 gpr_used[NUM_GPRS / 32];
+        u32 gpr_input[NUM_GPRS / 32];
+        u32 route[NUM_OUTPUTS];
+        u32 route_v[NUM_OUTPUTS];
+};
+
+struct dsp_patch {
+        char name[PATCH_NAME_SIZE];
+        u8 id;
+        u32 input;                      /* bitmap of the lines used as inputs */
+	u32 output;                     /* bitmap of the lines used as outputs */
+        u16 code_start;
+        u16 code_size;
+
+        u32 gpr_used[NUM_GPRS / 32];    /* bitmap of used gprs */
+        u32 gpr_input[NUM_GPRS / 32];
+        u8 traml_istart;  /* starting address of the internal tram lines used */        u8 traml_isize;   /* number of internal tram lines used */
+
+        u8 traml_estart;
+        u8 traml_esize;
+
+        u16 tramb_istart;        /* starting address of the internal tram memory used */
+        u16 tramb_isize;         /* amount of internal memory used */
+        u32 tramb_estart;
+        u32 tramb_esize;
+};
+
+struct dsp_gpr {
+        u8 type;                      /* gpr type, STATIC, DYNAMIC, INPUT, OUTPUT, CONTROL */
+        char name[GPR_NAME_SIZE];       /* gpr value, only valid for control gprs */
+        s32 min, max;         /* value range for this gpr, only valid for control gprs */
+        u8 line;                    /* which input/output line is the gpr attached, only valid for input/output gprs */
+        u8 usage;
+};
+
+enum {
+        GPR_TYPE_NULL = 0,
+        GPR_TYPE_IO,
+        GPR_TYPE_STATIC,
+        GPR_TYPE_DYNAMIC,
+        GPR_TYPE_CONTROL,
+        GPR_TYPE_CONSTANT
+};
+
+#define GPR_BASE 0x100
+#define OUTPUT_BASE 0x20
+
+//We can get this info by looking at the code start
+//#define PATCH_TYPE_INPUT        0x1
+//#define PATCH_TYPE_OUTPUT       0x2
+
+#define MAX_PATCHES_PAGES 32
+
+struct patch_manager {
+        void *patch[MAX_PATCHES_PAGES];
+	int current_pages;
+        struct dsp_rpatch rpatch;
+        struct dsp_gpr gpr[NUM_GPRS];   /* gpr usage table */
+	spinlock_t lock;
+	s16 ctrl_gpr[SOUND_MIXER_NRDEVICES][2];
+};
+
+
+#define PATCHES_PER_PAGE (PAGE_SIZE / sizeof(struct dsp_patch))
+
+#define PATCH(mgr, i) ((struct dsp_patch *) (mgr)->patch[(i) / PATCHES_PER_PAGE] + (i) % PATCHES_PER_PAGE)
+
+/* PCM volume control */
+#define TMP_PCM_L     0x100 //temp PCM L (after the vol control)       
+#define TMP_PCM_R     0x101
+#define VOL_PCM_L     0x102 //vol PCM
+#define VOL_PCM_R     0x103
+
+/* Routing patch */
+#define TMP_AC_L      0x104 //tmp ac97 out
+#define TMP_AC_R      0x105
+#define TMP_REAR_L    0x106 //output - Temp Rear
+#define TMP_REAR_R    0x107
+#define TMP_DIGI_L    0x108 //output - Temp digital
+#define TMP_DIGI_R    0x109
+#define DSP_VOL_L     0x10a // main dsp volume
+#define DSP_VOL_R     0x10b
+
+/* hw inputs */
+#define PCM_IN_L 	0x00
+#define PCM_IN_R 	0x01
+
+#define PCM1_IN_L        0x04
+#define PCM1_IN_R        0x05
+
+#define AC97_IN_L	0x10
+#define AC97_IN_R	0x11
+#define SPDIF_CD_L	0x12
+#define SPDIF_CD_R	0x13
+
+/* hw outputs */
+#define AC97_FRONT_L	0x20
+#define AC97_FRONT_R	0x21
+#define DIGITAL_OUT_L	0x22
+#define DIGITAL_OUT_R	0x23
+#define ANALOG_REAR_L	0x28
+#define ANALOG_REAR_R	0x29
+#define ADC_REC_L	0x2a
+#define ADC_REC_R	0x2b
+
+#define INPUT_PATCH_START(patch, nm, ln, i)		\
+do {							\
+	patch = PATCH(mgr, patch_n);			\
+	strcpy(patch->name, nm);			\
+	patch->code_start = pc * 2;			\
+	patch->input = (1<<(0x1f&ln));			\
+	patch->output= (1<<(0x1f&ln));			\
+	patch->id = i;					\
+} while(0)
+
+#define INPUT_PATCH_END(patch)				\
+do {							\
+	patch->code_size = pc * 2 - patch->code_start;	\
+	patch_n++;					\
+} while(0)
+
+
+#define ROUTING_PATCH_START(patch, nm)	\
+do {					\
+	patch = &mgr->rpatch;		\
+	strcpy(patch->name, nm);	\
+	patch->code_start = pc * 2;	\
+} while(0)
+
+#define ROUTING_PATCH_END(patch)			\
+do {                                                    \
+	patch->code_size = pc * 2 - patch->code_start;      \
+} while(0)
+
+#define CONNECT(input, output) set_bit(input, &rpatch->route[(output) - OUTPUT_BASE]);
+
+#define CONNECT_V(input, output) set_bit(input, &rpatch->route_v[(output) - OUTPUT_BASE]);
+
+#define OUTPUT_PATCH_START(patch, nm, ln, i)		\
+do {							\
+	patch = PATCH(mgr, patch_n);			\
+	strcpy(patch->name, nm);			\
+	patch->code_start = pc * 2;			\
+	patch->input = (1<<(0x1f&ln));			\
+	patch->output= (1<<(0x1f&ln));			\
+	patch->id = i;					\
+} while(0)
+
+#define OUTPUT_PATCH_END(patch)				\
+do {							\
+	patch->code_size = pc * 2 - patch->code_start;	\
+	patch_n++;					\
+} while(0)
+
+#define GET_OUTPUT_GPR(patch, g, ln)			\
+do {							\
+	mgr->gpr[(g) - GPR_BASE].type = GPR_TYPE_IO;	\
+	mgr->gpr[(g) - GPR_BASE].usage++;		\
+	mgr->gpr[(g) - GPR_BASE].line = ln;		\
+	set_bit((g) - GPR_BASE, patch->gpr_used);	\
+} while(0)
+
+#define GET_INPUT_GPR(patch, g, ln)			\
+do {							\
+	mgr->gpr[(g) - GPR_BASE].type = GPR_TYPE_IO;	\
+	mgr->gpr[(g) - GPR_BASE].usage++;		\
+	mgr->gpr[(g) - GPR_BASE].line = ln;		\
+	set_bit((g) - GPR_BASE, patch->gpr_used);	\
+	set_bit((g) - GPR_BASE, patch->gpr_input);	\
+} while(0)
+
+#define GET_DYNAMIC_GPR(patch, g)				\
+do {								\
+	mgr->gpr[(g) - GPR_BASE].type = GPR_TYPE_DYNAMIC;	\
+	mgr->gpr[(g) - GPR_BASE].usage++;			\
+	set_bit((g) - GPR_BASE, patch->gpr_used);          	\
+} while(0)
+
+#define GET_CONTROL_GPR(patch, g, nm, a, b)			\
+do {								\
+	strcpy(mgr->gpr[(g) - GPR_BASE].name, nm);		\
+	mgr->gpr[(g) - GPR_BASE].type = GPR_TYPE_CONTROL;	\
+	mgr->gpr[(g) - GPR_BASE].usage++;			\
+	mgr->gpr[(g) - GPR_BASE].min = a;			\
+	mgr->gpr[(g) - GPR_BASE].max = b;			\
+	sblive_writeptr(card, g, 0, b);				\
+	set_bit((g) - GPR_BASE, patch->gpr_used);		\
+} while(0)
 
 #endif /* _EFXMGR_H */
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/emu_wrapper.h linux/drivers/sound/emu10k1/emu_wrapper.h
--- linux-2.4.8-pre8/drivers/sound/emu10k1/emu_wrapper.h	Tue Nov  7 11:09:19 2000
+++ linux/drivers/sound/emu10k1/emu_wrapper.h	Wed Dec 31 16:00:00 1969
@@ -1,8 +0,0 @@
-#ifndef __EMU_WRAPPER_H
-#define __EMU_WRAPPER_H
-
-#define vma_get_pgoff(v)		((v)->vm_pgoff)
-
-#define PCI_SET_DMA_MASK(pdev,mask)	(((pdev)->dma_mask) = (mask))
-
-#endif
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/hwaccess.c linux/drivers/sound/emu10k1/hwaccess.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/hwaccess.c	Mon Aug 14 08:32:48 2000
+++ linux/drivers/sound/emu10k1/hwaccess.c	Mon Dec  3 23:00:45 2001
@@ -355,51 +355,33 @@
 	}
 }
 
-int sblive_readac97(struct emu10k1_card *card, u8 index, u16 * data)
+u16 emu10k1_ac97_read(struct ac97_codec *codec, u8 reg)
 {
+	struct emu10k1_card *card = codec->private_data;
+	u16 data;
 	unsigned long flags;
 
 	spin_lock_irqsave(&card->lock, flags);
 
-	outb(index, card->iobase + AC97ADDRESS);
-	*data = inw(card->iobase + AC97DATA);
+	outb(reg, card->iobase + AC97ADDRESS);
+	data = inw(card->iobase + AC97DATA);
 
 	spin_unlock_irqrestore(&card->lock, flags);
 
-	return 0;
+	return data;
 }
 
-int sblive_writeac97(struct emu10k1_card *card, u8 index, u16 data)
+void emu10k1_ac97_write(struct ac97_codec *codec, u8 reg, u16 value)
 {
+	struct emu10k1_card *card = codec->private_data;
 	unsigned long flags;
 
 	spin_lock_irqsave(&card->lock, flags);
 
-	outb(index, card->iobase + AC97ADDRESS);
-	outw(data, card->iobase + AC97DATA);
+	outb(reg, card->iobase + AC97ADDRESS);
+	outw(value, card->iobase + AC97DATA);
 
 	spin_unlock_irqrestore(&card->lock, flags);
-
-	return 0;
-}
-
-int sblive_rmwac97(struct emu10k1_card *card, u8 index, u16 data, u16 mask)
-{
-	u16 temp;
-	unsigned long flags;
-
-	spin_lock_irqsave(&card->lock, flags);
-
-	outb(index, card->iobase + AC97ADDRESS);
-	temp = inw(card->iobase + AC97DATA);
-	temp &= ~mask;
-	data &= mask;
-	temp |= data;
-	outw(temp, card->iobase + AC97DATA);
-
-	spin_unlock_irqrestore(&card->lock, flags);
-
-	return 0;
 }
 
 /*********************************************************
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/hwaccess.h linux/drivers/sound/emu10k1/hwaccess.h
--- linux-2.4.8-pre8/drivers/sound/emu10k1/hwaccess.h	Mon Dec 11 13:02:19 2000
+++ linux/drivers/sound/emu10k1/hwaccess.h	Mon Dec  3 23:00:45 2001
@@ -35,9 +35,12 @@
 #include <linux/fs.h>
 #include <linux/sound.h>
 #include <linux/soundcard.h>
+#include <linux/ac97_codec.h>
 #include <linux/pci.h>
 
-#include "emu_wrapper.h"
+#include "passthrough.h"
+#include "efxmgr.h"
+#include "midi.h"
 
 #define EMUPAGESIZE     4096            /* don't change */
 #define NUM_G           64              /* use all channels */
@@ -93,6 +96,38 @@
         u32 fxwc;
 };
 
+#define CMD_READ 1
+#define CMD_WRITE 2
+
+struct mixer_private_ioctl {
+        u32 cmd;
+        u32 val[90];
+};
+
+/* bogus ioctls numbers to escape from OSS mixer limitations */
+#define CMD_WRITEFN0            _IOW('D', 0, struct mixer_private_ioctl)
+#define CMD_READFN0		_IOR('D', 1, struct mixer_private_ioctl) 
+#define CMD_WRITEPTR		_IOW('D', 2, struct mixer_private_ioctl) 
+#define CMD_READPTR		_IOR('D', 3, struct mixer_private_ioctl) 
+#define CMD_SETRECSRC		_IOW('D', 4, struct mixer_private_ioctl) 
+#define CMD_GETRECSRC		_IOR('D', 5, struct mixer_private_ioctl) 
+#define CMD_GETVOICEPARAM	_IOR('D', 6, struct mixer_private_ioctl) 
+#define CMD_SETVOICEPARAM	_IOW('D', 7, struct mixer_private_ioctl) 
+#define CMD_GETPATCH		_IOR('D', 8, struct mixer_private_ioctl) 
+#define CMD_GETGPR		_IOR('D', 9, struct mixer_private_ioctl) 
+#define CMD_GETCTLGPR           _IOR('D', 10, struct mixer_private_ioctl)
+#define CMD_SETPATCH		_IOW('D', 11, struct mixer_private_ioctl) 
+#define CMD_SETGPR		_IOW('D', 12, struct mixer_private_ioctl) 
+#define CMD_SETCTLGPR		_IOW('D', 13, struct mixer_private_ioctl)
+#define CMD_SETGPOUT		_IOW('D', 14, struct mixer_private_ioctl)
+#define CMD_GETGPR2OSS		_IOR('D', 15, struct mixer_private_ioctl)
+#define CMD_SETGPR2OSS		_IOW('D', 16, struct mixer_private_ioctl)
+#define CMD_SETMCH_FX		_IOW('D', 17, struct mixer_private_ioctl)
+#define CMD_SETPASSTHROUGH	_IOW('D', 18, struct mixer_private_ioctl)
+
+struct oss_scaling {
+	char scale, muting;
+}; 
 
 struct emu10k1_card 
 {
@@ -117,20 +152,25 @@
 	unsigned short		model;
 	unsigned int irq; 
 
-	int	audio_num;
-	int	audio1_num;
-	int	mixer_num;
-	int	midi_num;
+	int	audio_dev;
+	int	audio_dev1;
+	int	midi_dev;
+#ifdef EMU10K1_SEQUENCER
+	int seq_dev;
+	struct emu10k1_mididevice *seq_mididev;
+#endif
+
+	struct ac97_codec ac97;
+	int ac97_supported_mixers;
+	int ac97_stereo_mixers;
 
+	/* Number of first fx voice for multichannel output */
+	u8 mchannel_fx;
 	struct emu10k1_waveout	waveout;
 	struct emu10k1_wavein	wavein;
 	struct emu10k1_mpuout	*mpuout;
 	struct emu10k1_mpuin	*mpuin;
 
-	u16			arrwVol[SOUND_MIXER_NRDEVICES + 1];
-	/* array is used from the member 1 to save (-1) operation */
-	u32			digmix[9 * 6 * 2];
-	unsigned int		modcnt;
 	struct semaphore	open_sem;
 	mode_t			open_mode;
 	wait_queue_head_t	open_wait;
@@ -141,26 +181,25 @@
 	u8 chiprev;                    /* Chip revision                */
 
 	int isaps;
+
+	struct patch_manager mgr;
+	struct pt_data pt;
 };
 
 int emu10k1_addxmgr_alloc(u32, struct emu10k1_card *);
 void emu10k1_addxmgr_free(struct emu10k1_card *, int);
 
-#ifdef PRIVATE_PCM_VOLUME
 
-#define MAX_PCM_CHANNELS NUM_G 
-struct sblive_pcm_volume_rec {
-	struct files_struct *files; // identification of the same thread
-	u8 attn_l;		// attenuation for left channel
-	u8 attn_r;		// attenuation for right channel
-	u16 mixer;		// saved mixer value for return
-	u8 channel_l;		// idx of left channel
-	u8 channel_r;		// idx of right channel
-	int opened;		// counter - locks element
-};
-extern struct sblive_pcm_volume_rec sblive_pcm_volume[];
-extern u16 pcm_last_mixer;
-#endif
+
+int emu10k1_find_control_gpr(struct patch_manager *, const char *, const char *);
+void emu10k1_set_control_gpr(struct emu10k1_card *, int , s32, int );
+
+void emu10k1_set_volume_gpr(struct emu10k1_card *, int, s32, int, int);
+
+
+#define VOL_6BIT 0x40,0x40
+#define VOL_5BIT 0x20,0x20
+#define VOL_4BIT 0x10,0x7f
 
 #define TIMEOUT 		    16384
 
@@ -185,10 +224,9 @@
 void emu10k1_set_stop_on_loop(struct emu10k1_card *, u32);
 void emu10k1_clear_stop_on_loop(struct emu10k1_card *, u32);
 
-/* AC97 Mixer access function */
-int sblive_readac97(struct emu10k1_card *, u8, u16 *);
-int sblive_writeac97(struct emu10k1_card *, u8, u16);
-int sblive_rmwac97(struct emu10k1_card *, u8, u16, u16);
+/* AC97 Codec register access function */
+u16 emu10k1_ac97_read(struct ac97_codec *, u8);
+void emu10k1_ac97_write(struct ac97_codec *, u8, u16);
 
 /* MPU access function*/
 int emu10k1_mpu_write_data(struct emu10k1_card *, u8);
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/icardwav.h linux/drivers/sound/emu10k1/icardwav.h
--- linux-2.4.8-pre8/drivers/sound/emu10k1/icardwav.h	Mon Aug 14 08:32:48 2000
+++ linux/drivers/sound/emu10k1/icardwav.h	Mon Dec  3 23:00:45 2001
@@ -34,12 +34,15 @@
 
 struct wave_format 
 {
+	int id;
 	int samplingrate;
 	u8 bitsperchannel;
-	u8 channels;		/* 1 = Mono, 2 = Stereo */
+	u8 channels;		/* 1 = Mono, 2 = Stereo, 3, ... = Multichannel */
 	u8 bytesperchannel;
+	u8 bytespervoicesample;
 	u8 bytespersample;
 	int bytespersec;
+	u8 passthrough;
 };
 
 /* emu10k1_wave states */
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/irqmgr.c linux/drivers/sound/emu10k1/irqmgr.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/irqmgr.c	Mon Aug 14 08:32:48 2000
+++ linux/drivers/sound/emu10k1/irqmgr.c	Mon Dec  3 23:00:45 2001
@@ -41,10 +41,7 @@
 void emu10k1_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 {
 	struct emu10k1_card *card = (struct emu10k1_card *) dev_id;
-	u32 irqstatus, tmp;
-
-	if (!(irqstatus = emu10k1_readfn0(card, IPR)))
-		return;
+	u32 irqstatus;
 
 	DPD(4, "emu10k1_interrupt called, irq =  %u\n", irq);
 
@@ -60,16 +57,22 @@
 	 ** - Eric
 	 */
 
-	do {
-		DPD(4, "irq status %x\n", irqstatus);
+	while ((irqstatus = inl(card->iobase + IPR))) {
+		DPD(4, "irq status %#x\n", irqstatus);
 
-		tmp = irqstatus;
+		/* acknowledge interrupt */
+		outl(irqstatus, card->iobase + IPR);
 
 		if (irqstatus & IRQTYPE_TIMER) {
 			emu10k1_timer_irqhandler(card);
 			irqstatus &= ~IRQTYPE_TIMER;
 		}
 
+		if (irqstatus & IRQTYPE_DSP) {
+			emu10k1_dsp_irqhandler(card);
+			irqstatus &= ~IRQTYPE_DSP;
+		}
+
 		if (irqstatus & IRQTYPE_MPUIN) {
 			emu10k1_mpuin_irqhandler(card);
 			irqstatus &= ~IRQTYPE_MPUIN;
@@ -80,13 +83,22 @@
 			irqstatus &= ~IRQTYPE_MPUOUT;
 		}
 
-		if (irqstatus)
-			emu10k1_irq_disable(card, irqstatus);
+		if (irqstatus & IPR_MUTE) {
+			emu10k1_mute_irqhandler(card);
+			irqstatus &=~IPR_MUTE;
+		}
 
-		emu10k1_writefn0(card, IPR, tmp);
+		if (irqstatus & IPR_VOLINCR) {
+			emu10k1_volincr_irqhandler(card);
+			irqstatus &=~IPR_VOLINCR;
+		}
 
-	} while ((irqstatus = emu10k1_readfn0(card, IPR)));
+		if (irqstatus & IPR_VOLDECR) {
+			emu10k1_voldecr_irqhandler(card);
+			irqstatus &=~IPR_VOLDECR;
+		}
 
-	return;
+		if (irqstatus)
+			emu10k1_irq_disable(card, irqstatus);
+	}
 }
-
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/irqmgr.h linux/drivers/sound/emu10k1/irqmgr.h
--- linux-2.4.8-pre8/drivers/sound/emu10k1/irqmgr.h	Mon Aug 14 08:32:48 2000
+++ linux/drivers/sound/emu10k1/irqmgr.h	Mon Dec  3 23:00:45 2001
@@ -44,5 +44,9 @@
 #define IRQTYPE_DSP                 IPR_FXDSP
 
 void emu10k1_timer_irqhandler(struct emu10k1_card *);
+void emu10k1_dsp_irqhandler(struct emu10k1_card *);
+void emu10k1_mute_irqhandler(struct emu10k1_card *);
+void emu10k1_volincr_irqhandler(struct emu10k1_card *);
+void emu10k1_voldecr_irqhandler(struct emu10k1_card *);
 
 #endif /* _IRQ_H */
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/joystick.c linux/drivers/sound/emu10k1/joystick.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/joystick.c	Wed Dec 31 16:00:00 1969
+++ linux/drivers/sound/emu10k1/joystick.c	Mon Dec  3 23:00:45 2001
@@ -0,0 +1,204 @@
+/*
+ **********************************************************************
+ *     joystick.c - Creative EMU10K1 Joystick port driver
+ *     Copyright 2000 Rui Sousa.
+ *
+ **********************************************************************
+ *
+ *     Date                 Author          Summary of changes
+ *     ----                 ------          ------------------
+ *     April  1, 2000       Rui Sousa       initial version 
+ *     April 28, 2000       Rui Sousa       fixed a kernel oops,
+ *					    make use of kcompat24 for
+ *					    2.2 kernels compatibility.
+ *     May    1, 2000       Rui Sousa       improved kernel compatibility
+ *                                          layer.
+ *
+ **********************************************************************
+ *
+ *     This program is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU General Public License as
+ *     published by the Free Software Foundation; either version 2 of
+ *     the License, or (at your option) any later version.
+ *
+ *     This program is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *     GNU General Public License for more details.
+ *
+ *     You should have received a copy of the GNU General Public
+ *     License along with this program; if not, write to the Free
+ *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
+ *     USA.
+ *
+ **********************************************************************/
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/version.h>
+#include <linux/pci.h>
+#include <linux/slab.h>
+#include <linux/ioport.h>
+#include <linux/list.h>
+
+#define DRIVER_VERSION "0.3.1"
+
+#ifndef PCI_VENDOR_ID_CREATIVE
+#define PCI_VENDOR_ID_CREATIVE 0x1102
+#endif
+
+#ifndef PCI_DEVICE_ID_CREATIVE_EMU10K1_JOYSTICK
+#define PCI_DEVICE_ID_CREATIVE_EMU10K1_JOYSTICK 0x7002
+#endif
+
+/* PCI function 1 registers, address = <val> + PCIBASE1 */
+
+#define JOYSTICK1               0x00            /* Analog joystick port register                */
+#define JOYSTICK2               0x01            /* Analog joystick port register                */
+#define JOYSTICK3               0x02            /* Analog joystick port register                */
+#define JOYSTICK4               0x03            /* Analog joystick port register                */
+#define JOYSTICK5               0x04            /* Analog joystick port register                */
+#define JOYSTICK6               0x05            /* Analog joystick port register                */
+#define JOYSTICK7               0x06            /* Analog joystick port register                */
+#define JOYSTICK8               0x07            /* Analog joystick port register                */
+
+/* When writing, any write causes JOYSTICK_COMPARATOR output enable to be pulsed on write.      */
+/* When reading, use these bitfields: */
+#define JOYSTICK_BUTTONS        0x0f            /* Joystick button data                         */
+#define JOYSTICK_COMPARATOR     0xf0            /* Joystick comparator data                     */
+
+#define NR_DEV 5
+
+static int io[NR_DEV] = { 0, };
+
+enum {
+	EMU10K1_JOYSTICK = 0
+};
+
+static char *card_names[] = {
+	"EMU10K1 Joystick Port"
+};
+
+static struct pci_device_id emu10k1_joy_pci_tbl[] __devinitdata = {
+	{PCI_VENDOR_ID_CREATIVE, PCI_DEVICE_ID_CREATIVE_EMU10K1_JOYSTICK,
+	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, EMU10K1_JOYSTICK},
+	{0,}
+};
+
+MODULE_DEVICE_TABLE(pci, emu10k1_joy_pci_tbl);
+
+struct emu10k1_joy_card {
+	struct list_head list;
+
+	struct pci_dev *pci_dev;
+	unsigned long iobase;
+	unsigned long length;
+	u8 addr_changed;
+};
+
+static LIST_HEAD(emu10k1_joy_devs);
+static unsigned int devindex = 0;
+
+/* Driver initialization routine */
+static int __devinit emu10k1_joy_probe(struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
+{
+	struct emu10k1_joy_card *card;
+	u16 model;
+	u8 chiprev;
+
+	if ((card = kmalloc(sizeof(struct emu10k1_joy_card), GFP_KERNEL)) == NULL) {
+		printk(KERN_ERR "emu10k1-joy: out of memory\n");
+		return -ENOMEM;
+	}
+	memset(card, 0, sizeof(struct emu10k1_joy_card));
+
+	if (pci_enable_device(pci_dev)) {
+		printk(KERN_ERR "emu10k1-joy: couldn't enable device\n");
+		kfree(card);
+		return -ENODEV;
+	}
+
+	card->iobase = pci_resource_start(pci_dev, 0); 
+	card->length = pci_resource_len(pci_dev, 0);
+
+	if (request_region(card->iobase, card->length, card_names[pci_id->driver_data])
+	    == NULL) {
+		printk(KERN_ERR "emu10k1-joy: IO space in use\n");
+		kfree(card);
+		return -ENODEV;
+	}
+
+	pci_set_drvdata(pci_dev, card);
+
+	card->pci_dev = pci_dev;
+	card->addr_changed = 0;
+
+	pci_read_config_byte(pci_dev, PCI_REVISION_ID, &chiprev);
+	pci_read_config_word(pci_dev, PCI_SUBSYSTEM_ID, &model);
+
+	printk(KERN_INFO "emu10k1-joy: %s rev %d model 0x%x found, IO at 0x%04lx-0x%04lx\n",
+		card_names[pci_id->driver_data], chiprev, model, card->iobase,
+		card->iobase + card->length - 1);
+
+	if (io[devindex]) {
+		if ((io[devindex] & ~0x18) != 0x200) {
+			printk(KERN_ERR "emu10k1-joy: invalid io value\n");
+			release_region(card->iobase, card->length);
+			kfree(card);
+			return -ENODEV;
+		}
+
+		card->addr_changed = 1;
+		pci_write_config_dword(pci_dev, PCI_BASE_ADDRESS_0, io[devindex]);
+		printk(KERN_INFO "emu10k1-joy: IO ports mirrored at 0x%03x\n", io[devindex]);
+	}
+
+	list_add(&card->list, &emu10k1_joy_devs);
+	devindex++;
+
+	return 0;
+}
+
+static void __devexit emu10k1_joy_remove(struct pci_dev *pci_dev)
+{
+	struct emu10k1_joy_card *card = pci_get_drvdata(pci_dev);
+
+	if(card->addr_changed)
+		pci_write_config_dword(pci_dev, PCI_BASE_ADDRESS_0, card->iobase);
+
+	release_region(card->iobase, card->length);
+
+	list_del(&card->list);
+	kfree(card);
+	pci_set_drvdata(pci_dev, NULL);
+}
+
+MODULE_PARM(io, "1-" __MODULE_STRING(NR_DEV) "i");
+MODULE_PARM_DESC(io, "sets joystick port address");
+MODULE_AUTHOR("Rui Sousa (Email to: emu10k1-devel@opensource.creative.com)");
+MODULE_DESCRIPTION("Creative EMU10K1 PCI Joystick Port v" DRIVER_VERSION
+		   "\nCopyright (C) 2000 Rui Sousa");
+
+static struct pci_driver emu10k1_joy_pci_driver = {
+	name:"emu10k1 joystick",
+	id_table:emu10k1_joy_pci_tbl,
+	probe:emu10k1_joy_probe,
+	remove:emu10k1_joy_remove,
+};
+
+static int __init emu10k1_joy_init_module(void)
+{
+	printk(KERN_INFO "Creative EMU10K1 PCI Joystick Port, version " DRIVER_VERSION ", " __TIME__
+	       " " __DATE__ "\n");
+
+	return pci_module_init(&emu10k1_joy_pci_driver);
+}
+
+static void __exit emu10k1_joy_cleanup_module(void)
+{
+	pci_unregister_driver(&emu10k1_joy_pci_driver);
+	return;
+}
+
+module_init(emu10k1_joy_init_module);
+module_exit(emu10k1_joy_cleanup_module);
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/main.c linux/drivers/sound/emu10k1/main.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/main.c	Sat May 19 17:43:07 2001
+++ linux/drivers/sound/emu10k1/main.c	Mon Dec  3 23:00:45 2001
@@ -30,9 +30,11 @@
  **********************************************************************
  *
  *      Supported devices:
- *      /dev/dsp:     Standard /dev/dsp device, OSS-compatible
- *      /dev/mixer:   Standard /dev/mixer device, OSS-compatible
- *      /dev/midi:    Raw MIDI UART device, mostly OSS-compatible
+ *      /dev/dsp:        Standard /dev/dsp device, OSS-compatible
+ *      /dev/dsp1:       Routes to rear speakers only	 
+ *      /dev/mixer:      Standard /dev/mixer device, OSS-compatible
+ *      /dev/midi:       Raw MIDI UART device, mostly OSS-compatible
+ *	/dev/sequencer:  Sequencer Interface (requires sound.o)
  *
  *      Revision history:
  *      0.1 beta Initial release
@@ -43,11 +45,29 @@
  *	    moved bh's to tasklets, moved to the new PCI driver initialization style.
  *	0.6 Make use of pci_alloc_consistent, improve compatibility layer for 2.2 kernels,
  *	    code reorganization and cleanup.
- *      0.7 Support for the Emu-APS. Bug fixes for voice cache setup, mmaped sound + poll().
- *	    Support for setting external TRAM size.
- *	
- **********************************************************************
- */
+ *	0.7 Support for the Emu-APS. Bug fixes for voice cache setup, mmaped sound + poll().
+ *          Support for setting external TRAM size.
+ *      0.8 Make use of the kernel ac97 interface. Support for a dsp patch manager.
+ *      0.9 Re-enables rear speakers volume controls
+ *     0.10 Initializes rear speaker volume.
+ *	    Dynamic patch storage allocation.
+ *	    New private ioctls to change control gpr values.
+ *	    Enable volume control interrupts.
+ *	    By default enable dsp routes to digital out. 
+ *     0.11 Fixed fx / 4 problem.
+ *     0.12 Implemented mmaped for recording.
+ *	    Fixed bug: not unreserving mmaped buffer pages.
+ *	    IRQ handler cleanup.
+ *     0.13 Fixed problem with dsp1
+ *          Simplified dsp patch writing (inside the driver)
+ *	    Fixed several bugs found by the Stanford tools
+ *     0.14 New control gpr to oss mixer mapping feature (Chris Purnell)
+ *          Added AC3 Passthrough Support (Juha Yrjola)
+ *          Added Support for 5.1 cards (digital out and the third analog out)
+ *     0.15 Added Sequencer Support (Daniel Mack)
+ *          Support for multichannel pcm playback (Eduard Hasenleithner)
+ *          
+ *********************************************************************/
 
 /* These are only included once per module */
 #include <linux/version.h>
@@ -55,6 +75,7 @@
 #include <linux/slab.h>
 #include <linux/init.h>
 #include <linux/delay.h>
+#include <linux/proc_fs.h>
 
 #include "hwaccess.h"
 #include "8010.h"
@@ -66,7 +87,19 @@
 #include "recmgr.h"
 #include "ecard.h"
 
-#define DRIVER_VERSION "0.7"
+
+#ifdef EMU10K1_SEQUENCER
+#define MIDI_SYNTH_NAME "EMU10K1 MIDI"
+#define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
+ 
+#include "sound_config.h"
+#include "midi_synth.h"
+
+/* this should be in dev_table.h */
+#define SNDCARD_EMU10K1 46
+#endif
+ 
+#define DRIVER_VERSION "0.15"
 
 /* FIXME: is this right? */
 /* does the card support 32 bit bus master?*/
@@ -106,124 +139,164 @@
 extern struct file_operations emu10k1_mixer_fops;
 extern struct file_operations emu10k1_midi_fops;
 
+#ifdef EMU10K1_SEQUENCER
+static struct midi_operations emu10k1_midi_operations;
+#endif
+
 extern void emu10k1_interrupt(int, void *, struct pt_regs *s);
-extern int emu10k1_mixer_wrch(struct emu10k1_card *, unsigned int, int);
 
-static void __devinit audio_init(struct emu10k1_card *card)
+static int __devinit emu10k1_audio_init(struct emu10k1_card *card)
 {
+	card->audio_dev = register_sound_dsp(&emu10k1_audio_fops, -1);
+	if (card->audio_dev < 0) {
+		printk(KERN_ERR "emu10k1: cannot register first audio device!\n");
+		goto err_dev;
+	}
+
+	card->audio_dev1 = register_sound_dsp(&emu10k1_audio_fops, -1);
+	if (card->audio_dev1 < 0) {
+		printk(KERN_ERR "emu10k1: cannot register second audio device!\n");
+		goto err_dev1;
+	}
+
 	/* Assign default playback voice parameters */
+	card->mchannel_fx = 8;
 	/* mono voice */
-	card->waveout.send_a[0] = 0x00;
+	card->waveout.send_a[0] = 0xff;
 	card->waveout.send_b[0] = 0xff;
-	card->waveout.send_c[0] = 0xff;
+	card->waveout.send_c[0] = 0x00;
 	card->waveout.send_d[0] = 0x00;
-	card->waveout.send_routing[0] = 0xd01c;
+	card->waveout.send_routing[0] = 0x3210;
 
 	/* stereo voice */
-	card->waveout.send_a[1] = 0x00;
-        card->waveout.send_b[1] = 0x00;
-        card->waveout.send_c[1] = 0xff;
-        card->waveout.send_d[1] = 0x00;
-        card->waveout.send_routing[1] = 0xd01c;
+	/* left */
+	card->waveout.send_a[1] = 0xff;
+	card->waveout.send_b[1] = 0x00;
+	card->waveout.send_c[1] = 0x00;
+	card->waveout.send_d[1] = 0x00;
+	card->waveout.send_routing[1] = 0x3210;
 
+	/* right */
 	card->waveout.send_a[2] = 0x00;
-        card->waveout.send_b[2] = 0xff;
-        card->waveout.send_c[2] = 0x00;
-        card->waveout.send_d[2] = 0x00;
-        card->waveout.send_routing[2] = 0xd01c;
+	card->waveout.send_b[2] = 0xff;
+	card->waveout.send_c[2] = 0x00;
+	card->waveout.send_d[2] = 0x00;
+	card->waveout.send_routing[2] = 0x3210;
 
 	/* Assign default recording parameters */
+	/* FIXME */
 	if(card->isaps)
 		card->wavein.recsrc = WAVERECORD_FX;
 	else
 		card->wavein.recsrc = WAVERECORD_AC97;
 
 	card->wavein.fxwc = 0x0003;
+	return 0;
 
-	return;
+err_dev1:
+	unregister_sound_dsp(card->audio_dev);
+err_dev:
+	return -ENODEV;
 }
 
-static void __devinit mixer_init(struct emu10k1_card *card)
+static void __devinit emu10k1_audio_cleanup(struct emu10k1_card *card)
 {
-	int count;
-	struct initvol {
-		int mixch;
-		int vol;
-	} initvol[] = {
-		{
-		SOUND_MIXER_VOLUME, 0x5050}, {
-		SOUND_MIXER_OGAIN, 0x3232}, {
-		SOUND_MIXER_SPEAKER, 0x3232}, {
-		SOUND_MIXER_PHONEIN, 0x3232}, {
-		SOUND_MIXER_MIC, 0x0000}, {
-		SOUND_MIXER_LINE, 0x0000}, {
-		SOUND_MIXER_CD, 0x4b4b}, {
-		SOUND_MIXER_LINE1, 0x4b4b}, {
-		SOUND_MIXER_LINE3, 0x3232}, {
-		SOUND_MIXER_DIGITAL1, 0x6464}, {
-		SOUND_MIXER_DIGITAL2, 0x6464}, {
-		SOUND_MIXER_PCM, 0x6464}, {
-		SOUND_MIXER_RECLEV, 0x0404}, {
-		SOUND_MIXER_TREBLE, 0x3232}, {
-		SOUND_MIXER_BASS, 0x3232}, {
-		SOUND_MIXER_LINE2, 0x4b4b}};
-
-	int initdig[] = { 0, 1, 2, 3, 6, 7, 18, 19, 20, 21, 24, 25, 72, 73, 74, 75, 78, 79,
-		94, 95
-	};
-
-	for (count = 0; count < sizeof(card->digmix) / sizeof(card->digmix[0]); count++) {
-		card->digmix[count] = 0x80000000;
-		sblive_writeptr(card, FXGPREGBASE + 0x10 + count, 0, 0);
-	}
-
-	card->modcnt = 0;       // Should this be here or in open() ?
-
-	if (!card->isaps) {
-
-		for (count = 0; count < sizeof(initdig) / sizeof(initdig[0]); count++) {
-			card->digmix[initdig[count]] = 0x7fffffff;
-			sblive_writeptr(card, FXGPREGBASE + 0x10 + initdig[count], 0, 0x7fffffff);
-		}
-
-		/* Reset */
-		sblive_writeac97(card, AC97_RESET, 0);
+	unregister_sound_dsp(card->audio_dev1);
+	unregister_sound_dsp(card->audio_dev);
+}
 
-#if 0
-		/* Check status word */
-		{
-			u16 reg;
-
-			sblive_readac97(card, AC97_RESET, &reg);
-			DPD(2, "RESET 0x%x\n", reg);
-			sblive_readac97(card, AC97_MASTERTONE, &reg);
-			DPD(2, "MASTER_TONE 0x%x\n", reg);
+static int __devinit emu10k1_mixer_init(struct emu10k1_card *card)
+{
+	char s[32];
+	card->ac97.dev_mixer = register_sound_mixer(&emu10k1_mixer_fops, -1);
+	if (card->ac97.dev_mixer < 0) {
+		printk(KERN_ERR "emu10k1: cannot register mixer device\n");
+		return -EIO;
+        }
+
+	card->ac97.private_data = card;
+
+	if(!card->isaps) {
+		card->ac97.id = 0;
+		card->ac97.codec_read = emu10k1_ac97_read;
+        	card->ac97.codec_write = emu10k1_ac97_write;
+
+		if (ac97_probe_codec (&card->ac97) == 0) {
+			printk(KERN_ERR "emu10k1: unable to probe AC97 codec\n");
+			goto err_out;
+		}
+		/* 5.1: Enable the additional AC97 Slots. If the emu10k1 version
+			does not support this, it shouldn't do any harm */
+		sblive_writeptr(card, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE);
+		
+
+		if (!proc_mkdir ("driver/emu10k1", 0)) {
+			printk(KERN_ERR "emu10k1: unable to create proc directory driver/emu10k1\n");
+			goto err_out;
 		}
-#endif
 
-		/* Set default recording source to mic in */
-		sblive_writeac97(card, AC97_RECORDSELECT, 0);
+		sprintf(s, "driver/emu10k1/%s", card->pci_dev->slot_name);
+		if (!proc_mkdir (s, 0)) {
+			printk(KERN_ERR "emu10k1: unable to create proc directory %s\n", s);
+			goto err_emu10k1_proc;
+		}
+	
+		sprintf(s, "driver/emu10k1/%s/ac97", card->pci_dev->slot_name);
+		if (!create_proc_read_entry (s, 0, 0, ac97_read_proc, &card->ac97)) {
+			printk(KERN_ERR "emu10k1: unable to create proc entry %s\n", s);
+			goto err_ac97_proc;
+		}
 
-		/* Set default AC97 "PCM" volume to acceptable max */
-		//sblive_writeac97(card, AC97_PCMOUTVOLUME, 0);
-		//sblive_writeac97(card, AC97_LINE2, 0);
+		card->ac97_supported_mixers = card->ac97.supported_mixers;
+		card->ac97_stereo_mixers = card->ac97.stereo_mixers;
 	}
 
-	/* Set default volumes for all mixer channels */
+	return 0;
+
+ err_ac97_proc:
+	sprintf(s, "driver/emu10k1/%s", card->pci_dev->slot_name);
+	remove_proc_entry(s, NULL);
+
+ err_emu10k1_proc:
+	remove_proc_entry("driver/emu10k1", NULL);
+ err_out:
+	unregister_sound_mixer (card->ac97.dev_mixer);
+	return -EIO;
+}
+
+static void __devinit emu10k1_mixer_cleanup(struct emu10k1_card *card)
+{
+	char s[32];
+
+	if(!card->isaps) {
+		sprintf(s, "driver/emu10k1/%s/ac97", card->pci_dev->slot_name);
+		remove_proc_entry(s, NULL);
 
-	for (count = 0; count < sizeof(initvol) / sizeof(initvol[0]); count++) {
-		emu10k1_mixer_wrch(card, initvol[count].mixch, initvol[count].vol);
+		sprintf(s, "driver/emu10k1/%s", card->pci_dev->slot_name);
+		remove_proc_entry(s, NULL);
+
+		remove_proc_entry("driver/emu10k1", NULL);
 	}
 
-	return;
+	unregister_sound_mixer (card->ac97.dev_mixer);
 }
 
-static int __devinit midi_init(struct emu10k1_card *card)
+static int __devinit emu10k1_midi_init(struct emu10k1_card *card)
 {
-	if ((card->mpuout = kmalloc(sizeof(struct emu10k1_mpuout), GFP_KERNEL))
-	    == NULL) {
+	int ret;
+
+	card->midi_dev = register_sound_midi(&emu10k1_midi_fops, -1);
+	if (card->midi_dev < 0) {
+                printk(KERN_ERR "emu10k1: cannot register midi device!\n");
+		return -ENODEV;
+        }
+
+
+	card->mpuout = kmalloc(sizeof(struct emu10k1_mpuout), GFP_KERNEL);
+	if (card->mpuout == NULL) {
 		printk(KERN_WARNING "emu10k1: Unable to allocate emu10k1_mpuout: out of memory\n");
-		return -1;
+		ret = -ENOMEM;
+		goto err_out1;
 	}
 
 	memset(card->mpuout, 0, sizeof(struct emu10k1_mpuout));
@@ -236,10 +309,11 @@
 
 	spin_lock_init(&card->mpuout->lock);
 
-	if ((card->mpuin = kmalloc(sizeof(struct emu10k1_mpuin), GFP_KERNEL)) == NULL) {
-		kfree(card->mpuout);
+	card->mpuin = kmalloc(sizeof(struct emu10k1_mpuin), GFP_KERNEL);
+	if (card->mpuin == NULL) {
 		printk(KERN_WARNING "emu10k1: Unable to allocate emu10k1_mpuin: out of memory\n");
-		return -1;
+		ret = -ENOMEM;
+                goto err_out2;
 	}
 
 	memset(card->mpuin, 0, sizeof(struct emu10k1_mpuin));
@@ -253,10 +327,65 @@
 	/* Reset the MPU port */
 	if (emu10k1_mpu_reset(card) < 0) {
 		ERROR();
-		return -1;
+		ret = -EIO;
+		goto err_out3;
 	}
 
+#ifdef EMU10K1_SEQUENCER
+	card->seq_dev = sound_alloc_mididev();
+	if(card->seq_dev == -1)
+			printk(KERN_WARNING "emu10k1: unable to register sequencer device!");
+	else {
+			std_midi_synth.midi_dev = card->seq_dev;
+			midi_devs[card->seq_dev] = 
+					(struct midi_operations *)
+					kmalloc(sizeof(struct midi_operations), GFP_KERNEL);
+			
+			if(midi_devs[card->seq_dev] == NULL) {
+					printk(KERN_ERR "emu10k1: unable to allocate memory!");
+					sound_unload_mididev(card->seq_dev);
+					card->seq_dev = -1;
+					return 0;
+			} else {
+					memcpy((char *)midi_devs[card->seq_dev], 
+						   (char *)&emu10k1_midi_operations, 
+						   sizeof(struct midi_operations));
+					midi_devs[card->seq_dev]->devc = card;
+					sequencer_init();
+			}
+	}
+	card->seq_mididev = 0;
+#endif
+
 	return 0;
+
+err_out3:
+	kfree(card->mpuin);
+err_out2:
+	kfree(card->mpuout);
+err_out1:
+	unregister_sound_midi(card->midi_dev);
+	return ret;
+}
+
+static void __devinit emu10k1_midi_cleanup(struct emu10k1_card *card)
+{
+    tasklet_unlock_wait(&card->mpuout->tasklet);
+    kfree(card->mpuout);
+
+    tasklet_unlock_wait(&card->mpuin->tasklet);
+    kfree(card->mpuin);
+
+#ifdef EMU10K1_SEQUENCER
+    if(card->seq_dev > -1) {
+	        kfree(midi_devs[card->seq_dev]);
+			midi_devs[card->seq_dev] = NULL;
+            sound_unload_mididev(card->seq_dev);
+			card->seq_dev = -1;
+    }
+#endif
+
+	unregister_sound_midi(card->midi_dev);
 }
 
 static void __devinit voice_init(struct emu10k1_card *card)
@@ -265,8 +394,6 @@
 
 	for (i = 0; i < NUM_G; i++)
 		card->voicetable[i] = VOICE_USAGE_FREE;
-
-	return;
 }
 
 static void __devinit timer_init(struct emu10k1_card *card)
@@ -274,8 +401,6 @@
 	INIT_LIST_HEAD(&card->timers);
 	card->timer_delay = TIMER_STOPPED;
 	card->timer_lock = SPIN_LOCK_UNLOCKED;
-
-	return;
 }
 
 static void __devinit addxmgr_init(struct emu10k1_card *card)
@@ -289,17 +414,29 @@
 	/* This page is reserved by the driver */
 	card->emupagetable[0] = 0x8001;
 	card->emupagetable[1] = MAXPAGES - 1;
+}
 
-	return;
+static void __devinit fx_cleanup(struct patch_manager *mgr)
+{
+	int i;
+	for(i = 0; i < mgr->current_pages; i++)
+		free_page((unsigned long) mgr->patch[i]);
 }
 
-static void __devinit fx_init(struct emu10k1_card *card)
+static int __devinit fx_init(struct emu10k1_card *card)
 {
-	int i, j, k;
-#ifdef TONE_CONTROL
-	int l;
-#endif	
+	struct patch_manager *mgr = &card->mgr;
+	struct dsp_patch *patch;
+	struct dsp_rpatch *rpatch;
+	s32 left, right;
+	int i;
 	u32 pc = 0;
+	u32 patch_n;
+
+	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
+		mgr->ctrl_gpr[i][0] = -1;
+		mgr->ctrl_gpr[i][1] = -1;
+	}
 
 	for (i = 0; i < 512; i++)
 		OP(6, 0x40, 0x40, 0x40, 0x40);
@@ -310,60 +447,175 @@
 				    TANKMEMADDRREGBASE + i, 0,
 				    TAGLIST_END);
 
+	mgr->current_pages = 5 / PATCHES_PER_PAGE + 1;
+        for(i = 0; i < mgr->current_pages; i++) {
+                mgr->patch[i] = (void *)__get_free_pages(GFP_KERNEL, 1);
+                if (mgr->patch[i] == NULL) {
+                        mgr->current_pages = i;
+                        fx_cleanup(mgr);
+                        return -ENOMEM;
+                }
+                memset(mgr->patch[i], 0, PAGE_SIZE);
+        }
+
 	pc = 0;
+	patch_n = 0;
 
-	for (j = 0; j < 2; j++) {
+	/* FX volume correction */
+	INPUT_PATCH_START(patch, "Pcm L vol correction", 0x0, 0);
+	GET_OUTPUT_GPR(patch, 0x100, 0x0);
+	
+	OP(4, 0x100, 0x40, PCM_IN_L, 0x44);
+	INPUT_PATCH_END(patch);
 
-		OP(4, 0x100, 0x40, j, 0x44);
-		OP(4, 0x101, 0x40, j + 2, 0x44);
 
-		for (i = 0; i < 6; i++) {
-			k = i * 18 + j;
-			OP(0, 0x102, 0x40, 0x110 + k, 0x100);
-			OP(0, 0x102, 0x102, 0x112 + k, 0x101);
-			OP(0, 0x102, 0x102, 0x114 + k, 0x10 + j);
-			OP(0, 0x102, 0x102, 0x116 + k, 0x12 + j);
-			OP(0, 0x102, 0x102, 0x118 + k, 0x14 + j);
-			OP(0, 0x102, 0x102, 0x11a + k, 0x16 + j);
-			OP(0, 0x102, 0x102, 0x11c + k, 0x18 + j);
-			OP(0, 0x102, 0x102, 0x11e + k, 0x1a + j);
-#ifdef TONE_CONTROL
-			OP(0, 0x102, 0x102, 0x120 + k, 0x1c + j);
-
-			k = 0x1a0 + i * 8 + j * 4;
-			OP(0, 0x40, 0x40, 0x102, 0x180 + j);
-			OP(7, k + 1, k, k + 1, 0x184 + j);
-			OP(7, k, 0x102, k, 0x182 + j);
-			OP(7, k + 3, k + 2, k + 3, 0x188 + j);
-			OP(0, k + 2, 0x56, k + 2, 0x186 + j);
-			OP(6, k + 2, k + 2, k + 2, 0x40);
-
-			l = 0x1d0 + i * 8 + j * 4;
-			OP(0, 0x40, 0x40, k + 2, 0x190 + j);
-			OP(7, l + 1, l, l + 1, 0x194 + j);
-			OP(7, l, k + 2, l, 0x192 + j);
-			OP(7, l + 3, l + 2, l + 3, 0x198 + j);
-			OP(0, l + 2, 0x56, l + 2, 0x196 + j);
-			OP(4, l + 2, 0x40, l + 2, 0x46);
-
-			if ((i == 0) && !card->isaps)
-				OP(4, 0x20 + (i * 2) + j, 0x40, l + 2, 0x50);	/* FIXME: Is this really needed? */
-			else
-				OP(6, 0x20 + (i * 2) + j, l + 2, 0x40, 0x40);
-#else
-			OP(0, 0x20 + (i * 2) + j, 0x102, 0x120 + k, 0x1c + j);
-#endif
-		}
-	}
+	INPUT_PATCH_START(patch, "Pcm R vol correction", 0x1, 0);
+	GET_OUTPUT_GPR(patch, 0x101, 0x1);
+
+	OP(4, 0x101, 0x40, PCM_IN_R, 0x44);
+	INPUT_PATCH_END(patch);
+
+
+	ROUTING_PATCH_START(rpatch, "Routing");
+	GET_INPUT_GPR(rpatch, 0x100, 0x0);
+	GET_INPUT_GPR(rpatch, 0x101, 0x1);
+
+        GET_DYNAMIC_GPR(rpatch, 0x102);
+        GET_DYNAMIC_GPR(rpatch, 0x103);
+
+	GET_OUTPUT_GPR(rpatch, 0x104, 0x8);
+	GET_OUTPUT_GPR(rpatch, 0x105, 0x9);
+	GET_OUTPUT_GPR(rpatch, 0x10a, 0x2);
+	GET_OUTPUT_GPR(rpatch, 0x10b, 0x3);
+
+	GET_CONTROL_GPR(rpatch, 0x106, "Vol Pcm L:Rear L", 0, 0x7fffffff);
+        GET_CONTROL_GPR(rpatch, 0x107, "Vol Pcm R:Rear R", 0, 0x7fffffff);
+
+	/* input buffer */
+	OP(6, 0x102, AC97_IN_L, 0x40, 0x40);
+	OP(6, 0x103, AC97_IN_R, 0x40, 0x40);
+
+	/* Digital In + PCM --> AC97 out (front speakers)*/
+	OP(6, AC97_FRONT_L, 0x100, SPDIF_CD_L, 0x40);
+
+	CONNECT(PCM_IN_L, AC97_FRONT_L);
+        CONNECT(SPDIF_CD_L, AC97_FRONT_L);
+
+	OP(6, AC97_FRONT_R, 0x101, SPDIF_CD_R, 0x40);
+
+	CONNECT(PCM_IN_R, AC97_FRONT_R);
+	CONNECT(SPDIF_CD_R, AC97_FRONT_R);
+
+	/* Digital In + PCM + AC97 In + PCM1 --> Rear Channel */ 
+	OP(0, 0x104, PCM1_IN_L, 0x100, 0x106);
+	OP(6, 0x104, 0x104, SPDIF_CD_L, 0x102);
+
+	CONNECT(AC97_IN_L, ANALOG_REAR_L);
+	CONNECT_V(PCM_IN_L, ANALOG_REAR_L);
+	CONNECT(SPDIF_CD_L, ANALOG_REAR_L);
+	CONNECT(PCM1_IN_L, ANALOG_REAR_L);
+
+	OP(0, 0x105, PCM1_IN_R, 0x101, 0x107);
+	OP(6, 0x105, 0x105, SPDIF_CD_R, 0x103);
+
+	CONNECT(AC97_IN_R, ANALOG_REAR_R);
+        CONNECT_V(PCM_IN_R, ANALOG_REAR_R);
+        CONNECT(SPDIF_CD_R, ANALOG_REAR_R);
+	CONNECT(PCM1_IN_R, ANALOG_REAR_R);
+
+	/* Digital In + PCM + AC97 In --> Digital out */
+	OP(6, 0x10b, 0x100, 0x102, SPDIF_CD_L);
+
+	CONNECT(PCM_IN_L, DIGITAL_OUT_L);
+        CONNECT(AC97_IN_L, DIGITAL_OUT_L);
+        CONNECT(SPDIF_CD_L, DIGITAL_OUT_L);
+
+	OP(6, 0x10a, 0x101, 0x103, SPDIF_CD_R);
+
+	CONNECT(PCM_IN_R, DIGITAL_OUT_R);
+	CONNECT(AC97_IN_R, DIGITAL_OUT_R);
+	CONNECT(SPDIF_CD_R, DIGITAL_OUT_R);
+
+	/* AC97 In --> ADC Recording Buffer */
+	OP(6, ADC_REC_L, 0x102, 0x40, 0x40);
+
+	CONNECT(AC97_IN_L, ADC_REC_L);
+
+	OP(6, ADC_REC_R, 0x103, 0x40, 0x40);
+
+	CONNECT(AC97_IN_R, ADC_REC_R);
+
+	ROUTING_PATCH_END(rpatch);
+
+
+	// Master volume control on rear
+	OUTPUT_PATCH_START(patch, "Vol Master L", 0x8, 0);
+	GET_INPUT_GPR(patch, 0x104, 0x8);
+	GET_CONTROL_GPR(patch, 0x108, "Vol", 0, 0x7fffffff);
+
+	OP(0, ANALOG_REAR_L, 0x040, 0x104, 0x108);
+	OUTPUT_PATCH_END(patch);
+
+
+	OUTPUT_PATCH_START(patch, "Vol Master R", 0x9, 0);
+	GET_INPUT_GPR(patch, 0x105, 0x9);
+	GET_CONTROL_GPR(patch, 0x109, "Vol", 0, 0x7fffffff);
+
+	OP(0, ANALOG_REAR_R, 0x040, 0x105, 0x109);
+	OUTPUT_PATCH_END(patch);
+
+
+	//Master volume control on front-digital
+	OUTPUT_PATCH_START(patch, "Vol Master L", 0x2, 1);
+	GET_INPUT_GPR(patch, 0x10a, 0x2);
+	GET_CONTROL_GPR(patch, 0x108, "Vol", 0, 0x7fffffff);
+
+	OP(0, DIGITAL_OUT_L, 0x040, 0x10a, 0x108);
+	OUTPUT_PATCH_END(patch);
+
+	
+	OUTPUT_PATCH_START(patch, "Vol Master R", 0x3, 1);
+	GET_INPUT_GPR(patch, 0x10b, 0x3);
+	GET_CONTROL_GPR(patch, 0x109, "Vol", 0, 0x7fffffff);
+
+	OP(0, DIGITAL_OUT_R, 0x040, 0x10b, 0x109);
+	OUTPUT_PATCH_END(patch);
+
+
+	/* delimiter patch */
+	patch = PATCH(mgr, patch_n);
+	patch->code_size = 0;
+
 	sblive_writeptr(card, DBG, 0, 0);
 
-	return;
+	mgr->lock = SPIN_LOCK_UNLOCKED;
+
+	mgr->ctrl_gpr[SOUND_MIXER_VOLUME][0] = 8;
+	mgr->ctrl_gpr[SOUND_MIXER_VOLUME][1] = 9;
+
+	left = card->ac97.mixer_state[SOUND_MIXER_VOLUME] & 0xff;
+	right = (card->ac97.mixer_state[SOUND_MIXER_VOLUME] >> 8) & 0xff;
+
+	emu10k1_set_volume_gpr(card, 8, left,  VOL_6BIT);
+	emu10k1_set_volume_gpr(card, 9, right, VOL_6BIT);
+
+	mgr->ctrl_gpr[SOUND_MIXER_PCM][0] = 6;
+	mgr->ctrl_gpr[SOUND_MIXER_PCM][1] = 7;
+
+	left = card->ac97.mixer_state[SOUND_MIXER_PCM] & 0xff;
+	right = (card->ac97.mixer_state[SOUND_MIXER_PCM] >> 8) & 0xff;
+
+	emu10k1_set_volume_gpr(card, 6, left,  VOL_5BIT);
+	emu10k1_set_volume_gpr(card, 7, right, VOL_5BIT);
+
+	return 0;
 }
 
 static int __devinit hw_init(struct emu10k1_card *card)
 {
 	int nCh;
 	u32 pagecount; /* tmp */
+	int ret;
 
 	/* Disable audio and lock cache */
 	emu10k1_writefn0(card, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE);
@@ -454,29 +706,32 @@
 
 			    TAGLIST_END);
 
-	fx_init(card);		/* initialize effects engine */
+	ret = fx_init(card);		/* initialize effects engine */
+	if (ret < 0)
+		return ret;
 
 	card->tankmem.size = 0;
 
 	card->virtualpagetable.size = MAXPAGES * sizeof(u32);
 
-	if ((card->virtualpagetable.addr = pci_alloc_consistent(card->pci_dev, card->virtualpagetable.size, &card->virtualpagetable.dma_handle)) ==
-	    NULL) {
+	card->virtualpagetable.addr = pci_alloc_consistent(card->pci_dev, card->virtualpagetable.size, &card->virtualpagetable.dma_handle);
+	if (card->virtualpagetable.addr == NULL) {
 		ERROR();
-		return -1;
+		ret = -ENOMEM;
+		goto err0;
 	}
 
 	card->silentpage.size = EMUPAGESIZE;
 
-	if ((card->silentpage.addr = pci_alloc_consistent(card->pci_dev, card->silentpage.size, &card->silentpage.dma_handle)) == NULL) {
+	card->silentpage.addr = pci_alloc_consistent(card->pci_dev, card->silentpage.size, &card->silentpage.dma_handle);
+	if (card->silentpage.addr == NULL) {
 		ERROR();
-		pci_free_consistent(card->pci_dev, card->virtualpagetable.size, card->virtualpagetable.addr, card->virtualpagetable.dma_handle);
-		return -1;
+		ret = -ENOMEM;
+		goto err1;
 	}
 
 	for (pagecount = 0; pagecount < MAXPAGES; pagecount++)
-		((u32 *) card->virtualpagetable.addr)[pagecount] =
-         cpu_to_le32((card->silentpage.dma_handle * 2) | pagecount);
+		((u32 *) card->virtualpagetable.addr)[pagecount] = cpu_to_le32((card->silentpage.dma_handle * 2) | pagecount);
 
 	/* Init page table & tank memory base register */
 	sblive_writeptr_tag(card, 0,
@@ -499,20 +754,29 @@
 	/* Lock Sound Memory = 0 */
 	/* Auto Mute = 1 */
 
-	sblive_rmwac97(card, AC97_MASTERVOLUME, 0x8000, 0x8000);
-
-	sblive_writeac97(card, AC97_MASTERVOLUME, 0);
-	sblive_writeac97(card, AC97_PCMOUTVOLUME, 0);
-
-	if(card->model == 0x20 || card->model == 0xc400 ||
+	if (card->model == 0x20 || card->model == 0xc400 ||
 	  (card->model == 0x21 && card->chiprev < 6))
 	        emu10k1_writefn0(card, HCFG, HCFG_AUDIOENABLE  | HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE);
 	else
 		emu10k1_writefn0(card, HCFG, HCFG_AUDIOENABLE  | HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE | HCFG_JOYENABLE);
 
+	/* Enable Vol_Ctrl irqs */
+	emu10k1_irq_enable(card, INTE_VOLINCRENABLE | INTE_VOLDECRENABLE | INTE_MUTEENABLE | INTE_FXDSPENABLE);
+
 	/* FIXME: TOSLink detection */
 	card->has_toslink = 0;
 
+	/* Initialize digital passthrough variables */
+	card->pt.pos_gpr = card->pt.intr_gpr = card->pt.enable_gpr = -1;
+	card->pt.selected = 0;
+	card->pt.state = PT_STATE_INACTIVE;
+	card->pt.spcs_to_use = 0x01;
+	card->pt.patch_name = "AC3pass";
+	card->pt.intr_gpr_name = "count";
+	card->pt.enable_gpr_name = "enable";
+	card->pt.pos_gpr_name = "ptr";
+	init_waitqueue_head(&card->pt.wait);
+
 /*	tmp = sblive_readfn0(card, HCFG);
 	if (tmp & (HCFG_GPINPUT0 | HCFG_GPINPUT1)) {
 		sblive_writefn0(card, HCFG, tmp | 0x800);
@@ -526,6 +790,13 @@
 	}
 */
 	return 0;
+
+  err1:
+	pci_free_consistent(card->pci_dev, card->virtualpagetable.size, card->virtualpagetable.addr, card->virtualpagetable.dma_handle);
+  err0:
+	fx_cleanup(&card->mgr);
+
+	return ret;
 }
 
 static int __devinit emu10k1_init(struct emu10k1_card *card)
@@ -538,23 +809,12 @@
 	timer_init(card);
 	addxmgr_init(card);
 
-	DPD(2, "  hw control register -> 0x%x\n", emu10k1_readfn0(card, HCFG));
+	DPD(2, "  hw control register -> %#x\n", emu10k1_readfn0(card, HCFG));
 
 	return 0;
 }
 
-static void __devexit midi_exit(struct emu10k1_card *card)
-{
-	tasklet_unlock_wait(&card->mpuout->tasklet);
-	kfree(card->mpuout);
-
-	tasklet_unlock_wait(&card->mpuin->tasklet);
-	kfree(card->mpuin);
-
-	return;
-}
-
-static void __devinit emu10k1_exit(struct emu10k1_card *card)
+static void __devinit emu10k1_cleanup(struct emu10k1_card *card)
 {
 	int ch;
 
@@ -605,7 +865,8 @@
 	if(card->tankmem.size != 0)
 		pci_free_consistent(card->pci_dev, card->tankmem.size, card->tankmem.addr, card->tankmem.dma_handle);
 
-	return;
+	/* release patch storage memory */
+	fx_cleanup(&card->mgr);
 }
 
 /* Driver initialization routine */
@@ -615,34 +876,32 @@
 	u32 subsysvid;
 	int ret;
 
-	if ((ret=pci_enable_device(pci_dev)))
-		return ret;
-
-	if ((card = kmalloc(sizeof(struct emu10k1_card), GFP_KERNEL)) == NULL) {
-		printk(KERN_ERR "emu10k1: out of memory\n");
-		return -ENOMEM;
-	}
-	memset(card, 0, sizeof(struct emu10k1_card));
-
 	if (pci_set_dma_mask(pci_dev, EMU10K1_DMA_MASK)) {
 		printk(KERN_ERR "emu10k1: architecture does not support 32bit PCI busmaster DMA\n");
-		kfree(card);
 		return -ENODEV;
 	}
 
+	if (pci_enable_device(pci_dev))
+		return -EIO;
+
 	pci_set_master(pci_dev);
 
+	if ((card = kmalloc(sizeof(struct emu10k1_card), GFP_KERNEL)) == NULL) {
+                printk(KERN_ERR "emu10k1: out of memory\n");
+                return -ENOMEM;
+        }
+        memset(card, 0, sizeof(struct emu10k1_card));
+
 	card->iobase = pci_resource_start(pci_dev, 0);
 	card->length = pci_resource_len(pci_dev, 0); 
 
 	if (request_region(card->iobase, card->length, card_names[pci_id->driver_data]) == NULL) {
 		printk(KERN_ERR "emu10k1: IO space in use\n");
-		kfree(card);
-		return -ENODEV;
+		ret = -EBUSY;
+		goto err_region;
 	}
 
 	pci_set_drvdata(pci_dev, card);
-	PCI_SET_DMA_MASK(pci_dev, EMU10K1_DMA_MASK);
 
 	card->irq = pci_dev->irq;
 	card->pci_dev = pci_dev;
@@ -650,6 +909,7 @@
 	/* Reserve IRQ Line */
 	if (request_irq(card->irq, emu10k1_interrupt, SA_SHIRQ, card_names[pci_id->driver_data], card)) {
 		printk(KERN_ERR "emu10k1: IRQ in use\n");
+		ret = -EBUSY;
 		goto err_irq;
 	}
 
@@ -668,40 +928,30 @@
 	card->open_mode = 0;
 	init_waitqueue_head(&card->open_wait);
 
-	/* Register devices */
-	if ((card->audio_num = register_sound_dsp(&emu10k1_audio_fops, -1)) < 0) {
-		printk(KERN_ERR "emu10k1: cannot register first audio device!\n");
-		goto err_dev0;
-	}
-
-	if ((card->audio1_num = register_sound_dsp(&emu10k1_audio_fops, -1)) < 0) {
-		printk(KERN_ERR "emu10k1: cannot register second audio device!\n");
-		goto err_dev1;
-	}
-
-	if ((card->mixer_num = register_sound_mixer(&emu10k1_mixer_fops, -1)) < 0) {
-		printk(KERN_ERR "emu10k1: cannot register mixer device!\n");
-		goto err_dev2;
-	}
-
-	if ((card->midi_num = register_sound_midi(&emu10k1_midi_fops, -1)) < 0) {
-		printk(KERN_ERR "emu10k1: cannot register midi device!\n");
-		goto err_dev3;
-	}
-
-	if (emu10k1_init(card) < 0) {
-		printk(KERN_ERR "emu10k1: cannot initialize device!\n");
+	ret = emu10k1_audio_init(card);
+	if(ret < 0) {
+                printk(KERN_ERR "emu10k1: cannot initialize audio devices\n");
+                goto err_audio;
+        }
+
+	ret = emu10k1_mixer_init(card);
+	if(ret < 0) {
+		printk(KERN_ERR "emu10k1: cannot initialize AC97 codec\n");
+                goto err_mixer;
+	}
+
+	ret = emu10k1_midi_init(card);
+	if (ret < 0) {
+		printk(KERN_ERR "emu10k1: cannot register midi device\n");
+		goto err_midi;
+	}
+
+	ret = emu10k1_init(card);
+	if (ret < 0) {
+		printk(KERN_ERR "emu10k1: cannot initialize device\n");
 		goto err_emu10k1_init;
 	}
 
-	if (midi_init(card) < 0) {
-		printk(KERN_ERR "emu10k1: cannot initialize midi!\n");
-		goto err_midi_init;
-	}
-
-	audio_init(card);
-	mixer_init(card);
-
 	if (card->isaps)
 		emu10k1_ecard_init(card);
 
@@ -709,52 +959,41 @@
 
 	return 0;
 
-      err_midi_init:
-	emu10k1_exit(card);
-
-      err_emu10k1_init:
-	unregister_sound_midi(card->midi_num);
-
-      err_dev3:
-	unregister_sound_mixer(card->mixer_num);
+err_emu10k1_init:
+	emu10k1_midi_cleanup(card);
 
-      err_dev2:
-	unregister_sound_dsp(card->audio1_num);
+err_midi:
+	emu10k1_mixer_cleanup(card);
 
-      err_dev1:
-	unregister_sound_dsp(card->audio_num);
+err_mixer:
+	emu10k1_audio_cleanup(card);
 
-      err_dev0:
+err_audio:
 	free_irq(card->irq, card);
 
-      err_irq:
+err_irq:
 	release_region(card->iobase, card->length);
+	pci_set_drvdata(pci_dev, NULL);
+
+err_region:
 	kfree(card);
 
-	return -ENODEV;
+	return ret;
 }
 
 static void __devexit emu10k1_remove(struct pci_dev *pci_dev)
 {
 	struct emu10k1_card *card = pci_get_drvdata(pci_dev);
 
-	midi_exit(card);
-	emu10k1_exit(card);
-
-	unregister_sound_midi(card->midi_num);
-	
-	unregister_sound_mixer(card->mixer_num);
-
-	unregister_sound_dsp(card->audio1_num);
-	unregister_sound_dsp(card->audio_num);
+	list_del(&card->list);
 
+	emu10k1_cleanup(card);
+	emu10k1_midi_cleanup(card);
+	emu10k1_mixer_cleanup(card);
+	emu10k1_audio_cleanup(card);	
 	free_irq(card->irq, card);
 	release_region(card->iobase, card->length);
-
-	list_del(&card->list);
-
 	kfree(card);
-
 	pci_set_drvdata(pci_dev, NULL);
 }
 
@@ -762,10 +1001,10 @@
 MODULE_DESCRIPTION("Creative EMU10K1 PCI Audio Driver v" DRIVER_VERSION "\nCopyright (C) 1999 Creative Technology Ltd.");
 
 static struct pci_driver emu10k1_pci_driver = {
-	name:"emu10k1",
-	id_table:emu10k1_pci_tbl,
-	probe:emu10k1_probe,
-	remove:emu10k1_remove,
+	name:		"emu10k1",
+	id_table:	emu10k1_pci_tbl,
+	probe:		emu10k1_probe,
+	remove:		emu10k1_remove,
 };
 
 static int __init emu10k1_init_module(void)
@@ -784,3 +1023,36 @@
 
 module_init(emu10k1_init_module);
 module_exit(emu10k1_cleanup_module);
+
+#ifdef EMU10K1_SEQUENCER
+
+/* in midi.c */
+extern int emu10k1_seq_midi_open(int dev, int mode, 
+				void (*input)(int dev, unsigned char midi_byte),
+				void (*output)(int dev));
+extern void emu10k1_seq_midi_close(int dev);
+extern int emu10k1_seq_midi_out(int dev, unsigned char midi_byte);
+extern int emu10k1_seq_midi_start_read(int dev);
+extern int emu10k1_seq_midi_end_read(int dev);
+extern void emu10k1_seq_midi_kick(int dev);
+extern int emu10k1_seq_midi_buffer_status(int dev);
+
+static struct midi_operations emu10k1_midi_operations =
+{
+    {"EMU10K1 MIDI", 0, 0, SNDCARD_EMU10K1},
+	&std_midi_synth,
+	{0},
+	emu10k1_seq_midi_open,
+	emu10k1_seq_midi_close,
+	NULL,
+	emu10k1_seq_midi_out,
+	emu10k1_seq_midi_start_read,
+	emu10k1_seq_midi_end_read,
+	emu10k1_seq_midi_kick,
+	NULL,
+	emu10k1_seq_midi_buffer_status,
+	NULL
+};
+
+#endif
+
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/midi.c linux/drivers/sound/emu10k1/midi.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/midi.c	Sat May 19 17:43:07 2001
+++ linux/drivers/sound/emu10k1/midi.c	Mon Dec  3 23:00:45 2001
@@ -1,4 +1,3 @@
-
 /*
  **********************************************************************
  *     midi.c - /dev/midi interface for emu10k1 driver
@@ -43,6 +42,10 @@
 #include "cardmi.h"
 #include "midi.h"
 
+#ifdef EMU10K1_SEQUENCER
+#include "sound_config.h"
+#endif
+
 static spinlock_t midi_spinlock __attribute((unused)) = SPIN_LOCK_UNLOCKED;
 
 static void init_midi_hdr(struct midi_hdr *midihdr)
@@ -85,22 +88,29 @@
 static int emu10k1_midi_open(struct inode *inode, struct file *file)
 {
 	int minor = MINOR(inode->i_rdev);
-	struct emu10k1_card *card=NULL;
+	struct emu10k1_card *card = NULL;
 	struct emu10k1_mididevice *midi_dev;
 	struct list_head *entry;
 
 	DPF(2, "emu10k1_midi_open()\n");
 
+	
 	/* Check for correct device to open */
 	list_for_each(entry, &emu10k1_devs) {
 		card = list_entry(entry, struct emu10k1_card, list);
 
-		if (card->midi_num == minor)
-			break;
+		if (card->midi_dev == minor)
+			goto match;
 	}
 
-	if (entry == &emu10k1_devs)
-		return -ENODEV;
+	return -ENODEV;
+
+match:
+#ifdef EMU10K1_SEQUENCER
+	if(card->seq_mididev)	/* card is opened by sequencer */
+			return -EBUSY;
+#endif
+	
 
 	/* Wait for device to become free */
 	down(&card->open_sem);
@@ -234,6 +244,7 @@
 	wake_up_interruptible(&card->open_wait);
 
 	unlock_kernel();
+	
 
 	return 0;
 }
@@ -245,7 +256,7 @@
 	u16 cnt;
 	unsigned long flags;
 
-	DPD(4, "emu10k1_midi_read(), count %x\n", (u32) count);
+	DPD(4, "emu10k1_midi_read(), count %#x\n", (u32) count);
 
 	if (pos != &file->f_pos)
 		return -ESPIPE;
@@ -320,7 +331,7 @@
 	ssize_t ret = 0;
 	unsigned long flags;
 
-	DPD(4, "emu10k1_midi_write(), count=%x\n", (u32) count);
+	DPD(4, "emu10k1_midi_write(), count=%#x\n", (u32) count);
 
 	if (pos != &file->f_pos)
 		return -ESPIPE;
@@ -434,10 +445,161 @@
 
 /* MIDI file operations */
 struct file_operations emu10k1_midi_fops = {
-        owner:		THIS_MODULE,
+	owner:		THIS_MODULE,
 	read:		emu10k1_midi_read,
 	write:		emu10k1_midi_write,
 	poll:		emu10k1_midi_poll,
 	open:		emu10k1_midi_open,
 	release:	emu10k1_midi_release,
 };
+
+
+#ifdef EMU10K1_SEQUENCER
+
+/* functions used for sequencer access */
+
+int emu10k1_seq_midi_open(int dev, int mode,
+				void (*input) (int dev, unsigned char data),
+				void (*output) (int dev))
+{
+    struct emu10k1_card *card;
+    struct midi_openinfo dsCardMidiOpenInfo;
+	struct emu10k1_mididevice *midi_dev;
+		
+	if(    midi_devs[dev] == NULL
+		|| midi_devs[dev]->devc == NULL)
+			return -EINVAL;
+
+	card = midi_devs[dev]->devc;
+
+	if(card->open_mode)		/* card is opened native */
+			return -EBUSY;
+			
+	DPF(2, "emu10k1_seq_midi_open()\n");
+	
+	if ((midi_dev = (struct emu10k1_mididevice *) kmalloc(sizeof(*midi_dev), GFP_KERNEL)) == NULL) {
+		return -EINVAL;
+	}
+
+	midi_dev->card = card;
+	midi_dev->mistate = MIDIIN_STATE_STOPPED;
+	init_waitqueue_head(&midi_dev->oWait);
+	init_waitqueue_head(&midi_dev->iWait);
+	midi_dev->ird = 0;
+	midi_dev->iwr = 0;
+	midi_dev->icnt = 0;
+	INIT_LIST_HEAD(&midi_dev->mid_hdrs);
+
+	dsCardMidiOpenInfo.refdata = (unsigned long) midi_dev;
+
+    if (emu10k1_mpuout_open(card, &dsCardMidiOpenInfo) < 0) {
+        ERROR();
+        return -ENODEV;
+    }
+
+	card->seq_mididev = midi_dev;
+		
+	return 0;
+}
+
+void emu10k1_seq_midi_close(int dev)
+{
+	struct emu10k1_card *card;
+
+	DPF(2, "emu10k1_seq_midi_close()\n");
+    if(    midi_devs[dev] == NULL
+        || midi_devs[dev]->devc == NULL)
+			return;
+
+	card = midi_devs[dev]->devc;
+	emu10k1_mpuout_close(card);
+
+	if(card->seq_mididev) {
+			kfree(card->seq_mididev);
+			card->seq_mididev = 0;
+	}
+}
+
+int emu10k1_seq_midi_out(int dev, unsigned char midi_byte)
+{
+
+	struct emu10k1_card *card;
+	struct midi_hdr *midihdr;
+	unsigned long flags;
+
+	if(    midi_devs[dev] == NULL
+        || midi_devs[dev]->devc == NULL)
+			return -EINVAL;
+
+	card = midi_devs[dev]->devc;
+
+	if ((midihdr = (struct midi_hdr *) kmalloc(sizeof(struct midi_hdr), GFP_KERNEL)) == NULL)
+		return -EINVAL;
+
+	midihdr->bufferlength = 1;
+	midihdr->bytesrecorded = 0;
+	midihdr->flags = 0;
+
+	if ((midihdr->data = (u8 *) kmalloc(1, GFP_KERNEL)) == NULL) {
+		ERROR();
+		kfree(midihdr);
+		return -EINVAL;
+	}
+
+	*(midihdr->data) = midi_byte;
+	
+	spin_lock_irqsave(&midi_spinlock, flags);
+
+	if (emu10k1_mpuout_add_buffer(card, midihdr) < 0) {
+		ERROR();
+		kfree(midihdr->data);
+		kfree(midihdr);
+		spin_unlock_irqrestore(&midi_spinlock, flags);
+		return -EINVAL;
+	}
+
+	spin_unlock_irqrestore(&midi_spinlock, flags);
+
+	return 1;
+}
+
+int emu10k1_seq_midi_start_read(int dev)
+{
+	return 0;
+}
+
+int emu10k1_seq_midi_end_read(int dev)
+{
+	return 0;
+}
+
+void emu10k1_seq_midi_kick(int dev)
+{
+}
+
+int emu10k1_seq_midi_buffer_status(int dev)
+{
+	int count;
+	struct midi_queue *queue;
+	struct emu10k1_card *card;
+
+	if(    midi_devs[dev] == NULL
+        || midi_devs[dev]->devc == NULL)
+			return -EINVAL;
+
+	count = 0;
+	
+	card = midi_devs[dev]->devc;
+	queue = card->mpuout->firstmidiq;
+
+	while(queue != NULL) {
+			count++;
+			if(queue == card->mpuout->lastmidiq)
+					break;
+			queue = queue->next;
+	}
+	return count;
+}
+
+#endif
+
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/mixer.c linux/drivers/sound/emu10k1/mixer.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/mixer.c	Thu Sep 21 13:25:09 2000
+++ linux/drivers/sound/emu10k1/mixer.c	Mon Dec  3 23:00:45 2001
@@ -3,9 +3,6 @@
  *     mixer.c - /dev/mixer interface for emu10k1 driver
  *     Copyright 1999, 2000 Creative Labs, Inc.
  *
- *     This program uses some code from es1371.c, Copyright 1998-1999
- *     Thomas Sailer
- *
  **********************************************************************
  *
  *     Date                 Author          Summary of changes
@@ -36,1053 +33,457 @@
 #define __NO_VERSION__		/* Kernel version only defined once */
 #include <linux/module.h>
 #include <linux/version.h>
-#include <linux/bitops.h>
 #include <asm/uaccess.h>
+#include <linux/fs.h>
 
 #include "hwaccess.h"
 #include "8010.h"
 #include "recmgr.h"
 
-#define AC97_PESSIMISTIC
-#undef OSS_DOCUMENTED_MIXER_SEMANTICS
-
-#define vol_to_hw_5(swvol) (31 - (((swvol) * 31) / 100))
-#define vol_to_hw_4(swvol) (15 - (((swvol) * 15) / 100))
-
-#define vol_to_sw_5(hwvol) (((31 - (hwvol)) * 100) / 31)
-#define vol_to_sw_4(hwvol) (((15 - (hwvol)) * 100) / 15)
-
-#define DM_MUTE 0x80000000
-
-#ifdef PRIVATE_PCM_VOLUME
-struct sblive_pcm_volume_rec sblive_pcm_volume[MAX_PCM_CHANNELS];
-u16 pcm_last_mixer = 0x6464;
-#endif
-
-/* Mapping arrays */
-static const unsigned int recsrc[] = {
-	SOUND_MASK_MIC,
-	SOUND_MASK_CD,
-	SOUND_MASK_VIDEO,
-	SOUND_MASK_LINE1,
-	SOUND_MASK_LINE,
-	SOUND_MASK_VOLUME,
-	SOUND_MASK_OGAIN,	/* Used to be PHONEOUT */
-	SOUND_MASK_PHONEIN,
-#ifdef TONE_CONTROL
-	SOUND_MASK_TREBLE,
-	SOUND_MASK_BASS,
-#endif
-};
-
-static const unsigned char volreg[SOUND_MIXER_NRDEVICES] = {
-	/* 5 bit stereo */
-	[SOUND_MIXER_LINE] = AC97_LINEINVOLUME,
-	[SOUND_MIXER_CD] = AC97_CDVOLUME,
-	[SOUND_MIXER_VIDEO] = AC97_VIDEOVOLUME,
-	[SOUND_MIXER_LINE1] = AC97_AUXVOLUME,
-
-/*	[SOUND_MIXER_PCM] = AC97_PCMOUTVOLUME, */
-	/* 5 bit stereo, setting 6th bit equal to maximum attenuation */
-
-/*	[SOUND_MIXER_VOLUME] = AC97_MASTERVOLUME, */
-	[SOUND_MIXER_PHONEOUT] = AC97_HEADPHONEVOLUME,
-	/* 5 bit mono, setting 6th bit equal to maximum attenuation */
-	[SOUND_MIXER_OGAIN] = AC97_MASTERVOLUMEMONO,
-	/* 5 bit mono */
-	[SOUND_MIXER_PHONEIN] = AC97_PHONEVOLUME,
-	/* 4 bit mono but shifted by 1 */
-	[SOUND_MIXER_SPEAKER] = AC97_PCBEEPVOLUME,
-	/* 5 bit mono, 7th bit = preamp */
-	[SOUND_MIXER_MIC] = AC97_MICVOLUME,
-	/* 4 bit stereo */
-	[SOUND_MIXER_RECLEV] = AC97_RECORDGAIN,
-	/* 4 bit mono */
-	[SOUND_MIXER_IGAIN] = AC97_RECORDGAINMIC,
-	/* test code */
-	[SOUND_MIXER_BASS] = AC97_GENERALPURPOSE,
-	[SOUND_MIXER_TREBLE] = AC97_MASTERTONE,
-	[SOUND_MIXER_LINE2] = AC97_PCMOUTVOLUME,
-	[SOUND_MIXER_DIGITAL2] = AC97_MASTERVOLUME
-};
-
-#ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
-
-#define swab(x) ((((x) >> 8) & 0xff) | (((x) << 8) & 0xff00))
-
-/* FIXME: mixer_rdch() is broken. */
-
-static int mixer_rdch(struct emu10k1_card *card, unsigned int ch, int *arg)
-{
-	u16 reg;
-	int j;
-	int nL, nR;
-
-	switch (ch) {
-	case SOUND_MIXER_PCM:
-	case SOUND_MIXER_VOLUME:
-#ifdef TONE_CONTROL
-	case SOUND_MIXER_TREBLE:
-        case SOUND_MIXER_BASS:
-#endif
-                return put_user(0x0000, (int *) arg);
-	default:
-		break;
-	}
-
-	if(card->isaps)
-		return -EINVAL;
-
-	switch (ch) {
-	case SOUND_MIXER_LINE:
-	case SOUND_MIXER_CD:
-	case SOUND_MIXER_VIDEO:
-	case SOUND_MIXER_LINE1:
-		sblive_readac97(card, volreg[ch], &reg);
-		nL = ((~(reg >> 8) & 0x1f) * 100) / 32;
-		nR = (~(reg & 0x1f) * 100) / 32;
-		DPD(2, "mixer_rdch: l=%d, r=%d\n", nL, nR);
-		return put_user(reg & 0x8000 ? 0 : (nL << 8) | nR, (int *) arg);
-
-	case SOUND_MIXER_OGAIN:
-	case SOUND_MIXER_PHONEIN:
-		sblive_readac97(card, volreg[ch], &reg);
-		return put_user(reg & 0x8000 ? 0 : ~(reg & 0x1f) * 0x64 / 0x20 * 0x101, (int *) arg);
-
-	case SOUND_MIXER_SPEAKER:
-		sblive_readac97(card, volreg[ch], &reg);
-		return put_user(reg & 0x8000 ? 0 : ~((reg >> 1) & 0xf) * 0x64 / 0x10 * 0x101, (int *) arg);
-
-	case SOUND_MIXER_MIC:
-		sblive_readac97(card, volreg[ch], &reg);
-		return put_user(reg & 0x8000 ? 0 : ~(reg & 0x1f) * 0x64 / 0x20 * 0x101 + ((reg & 0x40) ? 0x1e1e : 0), (int *) arg);
-
-	case SOUND_MIXER_RECLEV:
-		sblive_readac97(card, volreg[ch], &reg);
-		nL = ((~(reg >> 8) & 0x1f) * 100) / 16;
-		nR = (~(reg & 0x1f) * 100) / 16;
-		return put_user(reg & 0x8000 ? 0 : (nL << 8) | nR, (int *) arg);
-
-	default:
-		return -EINVAL;
-	}
-}
-
-#endif				/* OSS_DOCUMENTED_MIXER_SEMANTICS */
-
-static const unsigned char volidx[SOUND_MIXER_NRDEVICES] = {
-	/* 5 bit stereo */
-	[SOUND_MIXER_LINE] = 1,
-	[SOUND_MIXER_CD] = 2,
-	[SOUND_MIXER_VIDEO] = 3,
-	[SOUND_MIXER_LINE1] = 4,
-	[SOUND_MIXER_PCM] = 5,
-	/* 6 bit stereo */
-	[SOUND_MIXER_VOLUME] = 6,
-	[SOUND_MIXER_PHONEOUT] = 7,
-	/* 6 bit mono */
-	[SOUND_MIXER_OGAIN] = 8,
-	[SOUND_MIXER_PHONEIN] = 9,
-	/* 4 bit mono but shifted by 1 */
-	[SOUND_MIXER_SPEAKER] = 10,
-	/* 6 bit mono + preamp */
-	[SOUND_MIXER_MIC] = 11,
-	/* 4 bit stereo */
-	[SOUND_MIXER_RECLEV] = 12,
-	/* 4 bit mono */
-	[SOUND_MIXER_IGAIN] = 13,
-	[SOUND_MIXER_TREBLE] = 14,
-	[SOUND_MIXER_BASS] = 15,
-	[SOUND_MIXER_LINE2] = 16,
-	[SOUND_MIXER_LINE3] = 17,
-	[SOUND_MIXER_DIGITAL1] = 18,
-	[SOUND_MIXER_DIGITAL2] = 19
-};
-
-#ifdef TONE_CONTROL
-
-static const u32 bass_table[41][5] = {
-	{ 0x3e4f844f, 0x84ed4cc3, 0x3cc69927, 0x7b03553a, 0xc4da8486 },
-	{ 0x3e69a17a, 0x84c280fb, 0x3cd77cd4, 0x7b2f2a6f, 0xc4b08d1d },
-	{ 0x3e82ff42, 0x849991d5, 0x3ce7466b, 0x7b5917c6, 0xc48863ee },
-	{ 0x3e9bab3c, 0x847267f0, 0x3cf5ffe8, 0x7b813560, 0xc461f22c },
-	{ 0x3eb3b275, 0x844ced29, 0x3d03b295, 0x7ba79a1c, 0xc43d223b },
-	{ 0x3ecb2174, 0x84290c8b, 0x3d106714, 0x7bcc5ba3, 0xc419dfa5 },
-	{ 0x3ee2044b, 0x8406b244, 0x3d1c2561, 0x7bef8e77, 0xc3f8170f },
-	{ 0x3ef86698, 0x83e5cb96, 0x3d26f4d8, 0x7c114600, 0xc3d7b625 },
-	{ 0x3f0e5390, 0x83c646c9, 0x3d30dc39, 0x7c319498, 0xc3b8ab97 },
-	{ 0x3f23d60b, 0x83a81321, 0x3d39e1af, 0x7c508b9c, 0xc39ae704 },
-	{ 0x3f38f884, 0x838b20d2, 0x3d420ad2, 0x7c6e3b75, 0xc37e58f1 },
-	{ 0x3f4dc52c, 0x836f60ef, 0x3d495cab, 0x7c8ab3a6, 0xc362f2be },
-	{ 0x3f6245e8, 0x8354c565, 0x3d4fdbb8, 0x7ca602d6, 0xc348a69b },
-	{ 0x3f76845f, 0x833b40ec, 0x3d558bf0, 0x7cc036df, 0xc32f677c },
-	{ 0x3f8a8a03, 0x8322c6fb, 0x3d5a70c4, 0x7cd95cd7, 0xc317290b },
-	{ 0x3f9e6014, 0x830b4bc3, 0x3d5e8d25, 0x7cf1811a, 0xc2ffdfa5 },
-	{ 0x3fb20fae, 0x82f4c420, 0x3d61e37f, 0x7d08af56, 0xc2e9804a },
-	{ 0x3fc5a1cc, 0x82df2592, 0x3d6475c3, 0x7d1ef294, 0xc2d40096 },
-	{ 0x3fd91f55, 0x82ca6632, 0x3d664564, 0x7d345541, 0xc2bf56b9 },
-	{ 0x3fec9120, 0x82b67cac, 0x3d675356, 0x7d48e138, 0xc2ab796e },
-	{ 0x40000000, 0x82a36037, 0x3d67a012, 0x7d5c9fc9, 0xc2985fee },
-	{ 0x401374c7, 0x8291088a, 0x3d672b93, 0x7d6f99c3, 0xc28601f2 },
-	{ 0x4026f857, 0x827f6dd7, 0x3d65f559, 0x7d81d77c, 0xc27457a3 },
-	{ 0x403a939f, 0x826e88c5, 0x3d63fc63, 0x7d9360d4, 0xc2635996 },
-	{ 0x404e4faf, 0x825e5266, 0x3d613f32, 0x7da43d42, 0xc25300c6 },
-	{ 0x406235ba, 0x824ec434, 0x3d5dbbc3, 0x7db473d7, 0xc243468e },
-	{ 0x40764f1f, 0x823fd80c, 0x3d596f8f, 0x7dc40b44, 0xc23424a2 },
-	{ 0x408aa576, 0x82318824, 0x3d545787, 0x7dd309e2, 0xc2259509 },
-	{ 0x409f4296, 0x8223cf0b, 0x3d4e7012, 0x7de175b5, 0xc2179218 },
-	{ 0x40b430a0, 0x8216a7a1, 0x3d47b505, 0x7def5475, 0xc20a1670 },
-	{ 0x40c97a0a, 0x820a0d12, 0x3d4021a1, 0x7dfcab8d, 0xc1fd1cf5 },
-	{ 0x40df29a6, 0x81fdfad6, 0x3d37b08d, 0x7e098028, 0xc1f0a0ca },
-	{ 0x40f54ab1, 0x81f26ca9, 0x3d2e5bd1, 0x7e15d72b, 0xc1e49d52 },
-	{ 0x410be8da, 0x81e75e89, 0x3d241cce, 0x7e21b544, 0xc1d90e24 },
-	{ 0x41231051, 0x81dcccb3, 0x3d18ec37, 0x7e2d1ee6, 0xc1cdef10 },
-	{ 0x413acdd0, 0x81d2b39e, 0x3d0cc20a, 0x7e38184e, 0xc1c33c13 },
-	{ 0x41532ea7, 0x81c90ffb, 0x3cff9585, 0x7e42a58b, 0xc1b8f15a },
-	{ 0x416c40cd, 0x81bfdeb2, 0x3cf15d21, 0x7e4cca7c, 0xc1af0b3f },
-	{ 0x418612ea, 0x81b71cdc, 0x3ce20e85, 0x7e568ad3, 0xc1a58640 },
-	{ 0x41a0b465, 0x81aec7c5, 0x3cd19e7c, 0x7e5fea1e, 0xc19c5f03 },
-	{ 0x41bc3573, 0x81a6dcea, 0x3cc000e9, 0x7e68ebc2, 0xc1939250 }
-};
-
-static const u32 treble_table[41][5] = {
-	{ 0x0125cba9, 0xfed5debd, 0x00599b6c, 0x0d2506da, 0xfa85b354 },
-	{ 0x0142f67e, 0xfeb03163, 0x0066cd0f, 0x0d14c69d, 0xfa914473 },
-	{ 0x016328bd, 0xfe860158, 0x0075b7f2, 0x0d03eb27, 0xfa9d32d2 },
-	{ 0x0186b438, 0xfe56c982, 0x00869234, 0x0cf27048, 0xfaa97fca },
-	{ 0x01adf358, 0xfe21f5fe, 0x00999842, 0x0ce051c2, 0xfab62ca5 },
-	{ 0x01d949fa, 0xfde6e287, 0x00af0d8d, 0x0ccd8b4a, 0xfac33aa7 },
-	{ 0x02092669, 0xfda4d8bf, 0x00c73d4c, 0x0cba1884, 0xfad0ab07 },
-	{ 0x023e0268, 0xfd5b0e4a, 0x00e27b54, 0x0ca5f509, 0xfade7ef2 },
-	{ 0x0278645c, 0xfd08a2b0, 0x01012509, 0x0c911c63, 0xfaecb788 },
-	{ 0x02b8e091, 0xfcac9d1a, 0x0123a262, 0x0c7b8a14, 0xfafb55df },
-	{ 0x03001a9a, 0xfc45e9ce, 0x014a6709, 0x0c65398f, 0xfb0a5aff },
-	{ 0x034ec6d7, 0xfbd3576b, 0x0175f397, 0x0c4e2643, 0xfb19c7e4 },
-	{ 0x03a5ac15, 0xfb5393ee, 0x01a6d6ed, 0x0c364b94, 0xfb299d7c },
-	{ 0x0405a562, 0xfac52968, 0x01ddafae, 0x0c1da4e2, 0xfb39dca5 },
-	{ 0x046fa3fe, 0xfa267a66, 0x021b2ddd, 0x0c042d8d, 0xfb4a8631 },
-	{ 0x04e4b17f, 0xf975be0f, 0x0260149f, 0x0be9e0f2, 0xfb5b9ae0 },
-	{ 0x0565f220, 0xf8b0fbe5, 0x02ad3c29, 0x0bceba73, 0xfb6d1b60 },
-	{ 0x05f4a745, 0xf7d60722, 0x030393d4, 0x0bb2b578, 0xfb7f084d },
-	{ 0x06923236, 0xf6e279bd, 0x03642465, 0x0b95cd75, 0xfb916233 },
-	{ 0x07401713, 0xf5d3aef9, 0x03d01283, 0x0b77fded, 0xfba42984 },
-	{ 0x08000000, 0xf4a6bd88, 0x0448a161, 0x0b594278, 0xfbb75e9f },
-	{ 0x08d3c097, 0xf3587131, 0x04cf35a4, 0x0b3996c9, 0xfbcb01cb },
-	{ 0x09bd59a2, 0xf1e543f9, 0x05655880, 0x0b18f6b2, 0xfbdf1333 },
-	{ 0x0abefd0f, 0xf04956ca, 0x060cbb12, 0x0af75e2c, 0xfbf392e8 },
-	{ 0x0bdb123e, 0xee806984, 0x06c739fe, 0x0ad4c962, 0xfc0880dd },
-	{ 0x0d143a94, 0xec85d287, 0x0796e150, 0x0ab134b0, 0xfc1ddce5 },
-	{ 0x0e6d5664, 0xea547598, 0x087df0a0, 0x0a8c9cb6, 0xfc33a6ad },
-	{ 0x0fe98a2a, 0xe7e6ba35, 0x097edf83, 0x0a66fe5b, 0xfc49ddc2 },
-	{ 0x118c4421, 0xe536813a, 0x0a9c6248, 0x0a4056d7, 0xfc608185 },
-	{ 0x1359422e, 0xe23d19eb, 0x0bd96efb, 0x0a18a3bf, 0xfc77912c },
-	{ 0x1554982b, 0xdef33645, 0x0d3942bd, 0x09efe312, 0xfc8f0bc1 },
-	{ 0x1782b68a, 0xdb50deb1, 0x0ebf676d, 0x09c6133f, 0xfca6f019 },
-	{ 0x19e8715d, 0xd74d64fd, 0x106fb999, 0x099b3337, 0xfcbf3cd6 },
-	{ 0x1c8b07b8, 0xd2df56ab, 0x124e6ec8, 0x096f4274, 0xfcd7f060 },
-	{ 0x1f702b6d, 0xcdfc6e92, 0x14601c10, 0x0942410b, 0xfcf108e5 },
-	{ 0x229e0933, 0xc89985cd, 0x16a9bcfa, 0x09142fb5, 0xfd0a8451 },
-	{ 0x261b5118, 0xc2aa8409, 0x1930bab6, 0x08e50fdc, 0xfd24604d },
-	{ 0x29ef3f5d, 0xbc224f28, 0x1bfaf396, 0x08b4e3aa, 0xfd3e9a3b },
-	{ 0x2e21a59b, 0xb4f2ba46, 0x1f0ec2d6, 0x0883ae15, 0xfd592f33 },
-	{ 0x32baf44b, 0xad0c7429, 0x227308a3, 0x085172eb, 0xfd741bfd },
-	{ 0x37c4448b, 0xa45ef51d, 0x262f3267, 0x081e36dc, 0xfd8f5d14 }
+//FIXME: SOUND_MIXER_VOLUME should be selectable 5 or 6 bit
+const struct oss_scaling volume_params[SOUND_MIXER_NRDEVICES]= {
+/* Used by the ac97 driver */
+	[SOUND_MIXER_VOLUME]	=	{VOL_6BIT},
+	[SOUND_MIXER_BASS]	=	{VOL_4BIT},
+	[SOUND_MIXER_TREBLE]	=	{VOL_4BIT},
+	[SOUND_MIXER_PCM]	=	{VOL_5BIT},
+	[SOUND_MIXER_SPEAKER]	=	{VOL_4BIT},
+	[SOUND_MIXER_LINE]	=	{VOL_5BIT},
+	[SOUND_MIXER_MIC]	=	{VOL_5BIT},
+	[SOUND_MIXER_CD]	=	{VOL_5BIT},
+	[SOUND_MIXER_ALTPCM]	=	{VOL_6BIT},
+	[SOUND_MIXER_IGAIN]	=	{VOL_4BIT},
+	[SOUND_MIXER_LINE1]	=	{VOL_5BIT},
+	[SOUND_MIXER_PHONEIN]	= 	{VOL_5BIT},
+	[SOUND_MIXER_PHONEOUT]	= 	{VOL_6BIT},
+	[SOUND_MIXER_VIDEO]	=	{VOL_5BIT},
+/* Not used by the ac97 driver */
+	[SOUND_MIXER_SYNTH]	=	{VOL_5BIT},
+	[SOUND_MIXER_IMIX]	=	{VOL_5BIT},
+	[SOUND_MIXER_RECLEV]	=	{VOL_5BIT},
+	[SOUND_MIXER_OGAIN]	=	{VOL_5BIT},
+	[SOUND_MIXER_LINE2]	=	{VOL_5BIT},
+	[SOUND_MIXER_LINE3]	=	{VOL_5BIT},
+	[SOUND_MIXER_DIGITAL1]	=	{VOL_5BIT},
+	[SOUND_MIXER_DIGITAL2]	=	{VOL_5BIT},
+	[SOUND_MIXER_DIGITAL3]	=	{VOL_5BIT},
+	[SOUND_MIXER_RADIO]	=	{VOL_5BIT},
+	[SOUND_MIXER_MONITOR]	=	{VOL_5BIT}
 };
-
-static void set_bass(struct emu10k1_card *card, int l, int r)
-{
-	int i;
-
-	l = (l * 40 + 50) / 100;
-	r = (r * 40 + 50) / 100;
-	for (i = 0; i < 5; i++) {
-		sblive_writeptr(card, FXGPREGBASE + 0x80 + (i * 2), 0, bass_table[l][i]);
-		sblive_writeptr(card, FXGPREGBASE + 0x80 + (i * 2) + 1, 0, bass_table[r][i]);
-	}
-}
-
-static void set_treble(struct emu10k1_card *card, int l, int r)
+static loff_t emu10k1_mixer_llseek(struct file *file, loff_t offset, int origin)
 {
-	int i;
-
-	l = (l * 40 + 50) / 100;
-	r = (r * 40 + 50) / 100;
-	for (i = 0; i < 5; i++) {
-		sblive_writeptr(card, FXGPREGBASE + 0x90 + (i * 2), 0, treble_table[l][i]);
-		sblive_writeptr(card, FXGPREGBASE + 0x90 + (i * 2) + 1, 0, treble_table[r][i]);
-	}
+	DPF(2, "sblive_mixer_llseek() called\n");
+	return -ESPIPE;
 }
 
-#endif
-
-static const u32 db_table[101] = {
-	0x00000000, 0x01571f82, 0x01674b41, 0x01783a1b, 0x0189f540,
-	0x019c8651, 0x01aff763, 0x01c45306, 0x01d9a446, 0x01eff6b8,
-	0x0207567a, 0x021fd03d, 0x0239714c, 0x02544792, 0x027061a1,
-	0x028dcebb, 0x02ac9edc, 0x02cce2bf, 0x02eeabe8, 0x03120cb0,
-	0x0337184e, 0x035de2df, 0x03868173, 0x03b10a18, 0x03dd93e9,
-	0x040c3713, 0x043d0cea, 0x04702ff3, 0x04a5bbf2, 0x04ddcdfb,
-	0x0518847f, 0x0555ff62, 0x05966005, 0x05d9c95d, 0x06206005,
-	0x066a4a52, 0x06b7b067, 0x0708bc4c, 0x075d9a01, 0x07b6779d,
-	0x08138561, 0x0874f5d5, 0x08dafde1, 0x0945d4ed, 0x09b5b4fd,
-	0x0a2adad1, 0x0aa58605, 0x0b25f936, 0x0bac7a24, 0x0c3951d8,
-	0x0ccccccc, 0x0d673b17, 0x0e08f093, 0x0eb24510, 0x0f639481,
-	0x101d3f2d, 0x10dfa9e6, 0x11ab3e3f, 0x12806ac3, 0x135fa333,
-	0x144960c5, 0x153e2266, 0x163e6cfe, 0x174acbb7, 0x1863d04d,
-	0x198a1357, 0x1abe349f, 0x1c00db77, 0x1d52b712, 0x1eb47ee6,
-	0x2026f30f, 0x21aadcb6, 0x23410e7e, 0x24ea64f9, 0x26a7c71d,
-	0x287a26c4, 0x2a62812c, 0x2c61df84, 0x2e795779, 0x30aa0bcf,
-	0x32f52cfe, 0x355bf9d8, 0x37dfc033, 0x3a81dda4, 0x3d43c038,
-	0x4026e73c, 0x432ce40f, 0x46575af8, 0x49a8040f, 0x4d20ac2a,
-	0x50c335d3, 0x54919a57, 0x588dead1, 0x5cba514a, 0x611911ea,
-	0x65ac8c2f, 0x6a773c39, 0x6f7bbc23, 0x74bcc56c, 0x7a3d3272,
-	0x7fffffff,
-};
+/* Mixer file operations */
 
-static void aps_update_digital(struct emu10k1_card *card)
+static int emu10k1_private_mixer(struct emu10k1_card *card, unsigned int cmd, unsigned long arg)
 {
-	int i, l1, r1, l2, r2;
-	
-	i = card->arrwVol[volidx[SOUND_MIXER_VOLUME]];
-	l1 = (i & 0xff);
-	r1 = ((i >> 8) & 0xff);
-
-	i = card->arrwVol[volidx[SOUND_MIXER_PCM]];
-	l2 = (i & 0xff);
-	r2 = ((i >> 8) & 0xff);
-	
-	for (i = 0; i < 108; i++) {
-		if (card->digmix[i] != DM_MUTE) {
-			if ((i % 18 >= 0) && (i % 18 < 4))
-				card->digmix[i] = ((i & 1) ? ((u64) db_table[r1] * (u64) db_table[r2]) : ((u64) db_table[l1] * (u64) db_table[l2])) >> 31;
-			else
-				card->digmix[i] = (i & 1) ? db_table[r1] : db_table[l1];
-			sblive_writeptr(card, FXGPREGBASE + 0x10 + i, 0, card->digmix[i]);
-		}
-	}
-}
+	struct mixer_private_ioctl *ctl;
+	struct dsp_patch *patch;
+	u32 size, page;
+	int addr, size_reg, i, ret;
+	unsigned int id, ch;
 
-static void update_digital(struct emu10k1_card *card)
-{
-	int i, k, l1, r1, l2, r2, l3, r3, l4, r4;
-	u64 j;
+	switch (cmd) {
 
-	i = card->arrwVol[volidx[SOUND_MIXER_VOLUME]];
-	l1 = (i & 0xff);
-	r1 = ((i >> 8) & 0xff);
-	i = card->arrwVol[volidx[SOUND_MIXER_LINE3]];
-	l2 = i & 0xff;
-	r2 = (i >> 8) & 0xff;
-
-	i = card->arrwVol[volidx[SOUND_MIXER_PCM]];
-	l3 = i & 0xff;
-	r3 = (i >> 8) & 0xff;
-
-	i = card->arrwVol[volidx[SOUND_MIXER_DIGITAL1]];
-	l4 = i & 0xff;
-	r4 = (i >> 8) & 0xff;
-
-	i = (r1 * r2) / 50;
-	if (r2 > 50)
-		r2 = 2 * r1 - i;
-	else {
-		r2 = r1;
-		r1 = i;
-	}
+	case SOUND_MIXER_PRIVATE3:
 
-	i = (l1 * l2) / 50;
-	if (l2 > 50)
-		l2 = 2 * l1 - i;
-	else {
-		l2 = l1;
-		l1 = i;
-	}
+		ctl = (struct mixer_private_ioctl *) kmalloc(sizeof(struct mixer_private_ioctl), GFP_KERNEL);
+		if (ctl == NULL)
+			return -ENOMEM;
 
-	for (i = 0; i < 36; i++) {
-		if (card->digmix[i] != DM_MUTE) {
-			if (((i >= 0) && (i < 4)) || ((i >= 18) && (i < 22)))
-				j = (i & 1) ? ((u64) db_table[r1] * (u64) db_table[r3]) : ((u64) db_table[l1] * (u64) db_table[l3]);
-			else if ((i == 6) || (i == 7) || (i == 24) || (i == 25))
-				j = (i & 1) ? ((u64) db_table[r1] * (u64) db_table[r4]) : ((u64) db_table[l1] * (u64) db_table[l4]);
-			else
-				j = ((i & 1) ? db_table[r1] : db_table[l1]) << 31;
-			card->digmix[i] = j >> 31;
-			sblive_writeptr(card, FXGPREGBASE + 0x10 + i, 0, card->digmix[i]);
+		if (copy_from_user(ctl, (void *) arg, sizeof(struct mixer_private_ioctl))) {
+			kfree(ctl);
+			return -EFAULT;
 		}
-	}
 
-	for (i = 72; i < 90; i++) {
-		if (card->digmix[i] != DM_MUTE) {
-			if ((i >= 72) && (i < 76))
-				j = (i & 1) ? ((u64) db_table[r2] * (u64) db_table[r3]) : ((u64) db_table[l2] * (u64) db_table[l3]);
-			else if ((i == 78) || (i == 79))
-				j = (i & 1) ? ((u64) db_table[r2] * (u64) db_table[r4]) : ((u64) db_table[l2] * (u64) db_table[l4]);
-			else
-				j = ((i & 1) ? db_table[r2] : db_table[l2]) << 31;
-			card->digmix[i] = j >> 31;
-			sblive_writeptr(card, FXGPREGBASE + 0x10 + i, 0, card->digmix[i]);
-		}
-	}
+		ret = 0;
+		switch (ctl->cmd) {
+#ifdef DBGEMU
+		case CMD_WRITEFN0:
+			emu10k1_writefn0(card, ctl->val[0], ctl->val[1]);
+			break;
 
-	for (i = 36; i <= 90; i += 18) {
-		if (i != 72) {
-			for (k = 0; k < 4; k++)
-				if (card->digmix[i + k] != DM_MUTE) {
-					card->digmix[i + k] = db_table[l3];
-					sblive_writeptr(card, FXGPREGBASE + 0x10 + i + k, 0, card->digmix[i + k]);
-				}
-			if (card->digmix[i + 6] != DM_MUTE) {
-				card->digmix[i + 6] = db_table[l4];
-				sblive_writeptr(card, FXGPREGBASE + 0x10 + i + 6, 0, card->digmix[i + 6]);
-			}
-			if (card->digmix[i + 7] != DM_MUTE) {
-				card->digmix[i + 7] = db_table[r4];
-				sblive_writeptr(card, FXGPREGBASE + 0x10 + i + 7, 0, card->digmix[i + 7]);
+		case CMD_WRITEPTR:
+			if (ctl->val[1] >= 0x40 || ctl->val[0] > 0xff) {
+				ret = -EINVAL;
+				break;
 			}
-		}
-	}
 
-}
+			if ((ctl->val[0] & 0x7ff) > 0x3f)
+				ctl->val[1] = 0x00;
 
-#ifdef PRIVATE_PCM_VOLUME
+			sblive_writeptr(card, ctl->val[0], ctl->val[1], ctl->val[2]);
 
-/* calc & set attenuation factor for given channel */
-static int set_pcm_attn(struct emu10k1_card *card, int ch, int l)
-{
-#ifndef PCMLEVEL
-#define PCMLEVEL 110		/* almost silence */
+			break;
 #endif
-	int vol = IFATN_ATTENUATION_MASK;	/* silence */
+		case CMD_READFN0:
+			ctl->val[2] = emu10k1_readfn0(card, ctl->val[0]);
 
-	if (l > 0)
-		vol = (PCMLEVEL - (l * PCMLEVEL + 50) / 100);
-	sblive_writeptr(card, IFATN, ch, IFATN_FILTERCUTOFF_MASK | vol);
-	DPD(2, "SOUND_MIXER_PCM: channel:%d  level:%d  attn:%d\n", ch, l, vol);
-
-	return vol;
-#undef PCMLEVEL
-}
+			if (copy_to_user((void *) arg, ctl, sizeof(struct mixer_private_ioctl)))
+				ret = -EFAULT;
 
-/* update value of local PCM volume level (using channel attenuation)
- *
- * return 1: in case its local change
- *        0: if the current process doesn't have entry in table
- *	     (it means this process have not opened audio (mixer usually)
- */
-static int update_pcm_attn(struct emu10k1_card *card, unsigned l1, unsigned r1)
-{
-	int i;
-	int mixer = (r1 << 8) | l1;
+			break;
 
-	for (i = 0; i < MAX_PCM_CHANNELS; i++) {
-		if (sblive_pcm_volume[i].files == current->files) {
-			sblive_pcm_volume[i].mixer = pcm_last_mixer = mixer;
-			if (sblive_pcm_volume[i].opened) {
-				if (sblive_pcm_volume[i].channel_r < NUM_G) {
-					sblive_pcm_volume[i].attn_r = set_pcm_attn(card, sblive_pcm_volume[i].channel_r, r1);
-					if (sblive_pcm_volume[i].channel_l < NUM_G)
-						sblive_pcm_volume[i].attn_l = set_pcm_attn(card, sblive_pcm_volume[i].channel_l, l1);
-				} else {
-					/* mono voice */
-					if (sblive_pcm_volume[i].channel_l < NUM_G)
-						sblive_pcm_volume[i].attn_l =
-						    set_pcm_attn(card, sblive_pcm_volume[i].channel_l, (l1 >= r1) ? l1 : r1);
-						/* to correctly handle mono voice here we would need
-						   to go into stereo mode and move the voice to the right & left
-						   looks a bit overcomplicated... */
-				}
+		case CMD_READPTR:
+			if (ctl->val[1] >= 0x40 || (ctl->val[0] & 0x7ff) > 0xff) {
+				ret = -EINVAL;
+				break;
 			}
 
-			return 1;
-
-		}
-	}
-
-	card->arrwVol[volidx[SOUND_MIXER_PCM]] = mixer;
-	return 0;
-}
-#endif
-
-int emu10k1_mixer_wrch(struct emu10k1_card *card, unsigned int ch, int val)
-{
-	int i;
-	unsigned l1, r1;
-	u16 wval;
-
-	l1 = val & 0xff;
-	r1 = (val >> 8) & 0xff;
-	if (l1 > 100)
-		l1 = 100;
-	if (r1 > 100)
-		r1 = 100;
-
-	DPD(4, "emu10k1_mixer_wrch() called: ch=%u, l1=%u, r1=%u\n", ch, l1, r1);
-
-	if (!volidx[ch])
-		return -EINVAL;
-#ifdef PRIVATE_PCM_VOLUME
-	if (ch != SOUND_MIXER_PCM)
-#endif
-		card->arrwVol[volidx[ch]] = (r1 << 8) | l1;
-
-	switch (ch) {
-	case SOUND_MIXER_VOLUME:
-		DPF(4, "SOUND_MIXER_VOLUME:\n");
-		if (card->isaps)
-			aps_update_digital(card);
-		else
-			update_digital(card);
-		return 0;
-	case SOUND_MIXER_PCM:
-		DPF(4, "SOUND_MIXER_PCM\n");
-#ifdef PRIVATE_PCM_VOLUME
-		if (update_pcm_attn(card, l1, r1))
-			return 0;
-#endif
-		if (card->isaps)
-			aps_update_digital(card);
-		else
-			update_digital(card);
-		return 0;
-#ifdef TONE_CONTROL
-	case SOUND_MIXER_TREBLE:
-                DPF(4, "SOUND_MIXER_TREBLE:\n");
-                set_treble(card, l1, r1);
-                return 0;
-
-        case SOUND_MIXER_BASS:
-                DPF(4, "SOUND_MIXER_BASS:\n");
-                set_bass(card, l1, r1);
-		return 0;
-#endif
-	default:
-		break;
-	}
-
-
-	if (card->isaps)
-		return -EINVAL;
-
-	switch (ch) {
-	case SOUND_MIXER_DIGITAL1:
-	case SOUND_MIXER_LINE3:
-		DPD(4, "SOUND_MIXER_%s:\n", (ch == SOUND_MIXER_DIGITAL1) ? "DIGITAL1" : "LINE3");
-		update_digital(card);
-		return 0;
-	case SOUND_MIXER_DIGITAL2:
-	case SOUND_MIXER_LINE2:
-	case SOUND_MIXER_LINE1:
-	case SOUND_MIXER_LINE:
-	case SOUND_MIXER_CD:
-		DPD(4, "SOUND_MIXER_%s:\n",
-		    (ch == SOUND_MIXER_LINE1) ? "LINE1" :
-		    (ch == SOUND_MIXER_LINE2) ? "LINE2" : (ch == SOUND_MIXER_LINE) ? "LINE" : (ch == SOUND_MIXER_DIGITAL2) ? "DIGITAL2" : "CD");
-		wval = ((((100 - l1) * 32 + 50) / 100) << 8) | (((100 - r1) * 32 + 50) / 100);
-		if (wval == 0x2020)
-			wval = 0x8000;
-		else
-			wval -= ((wval & 0x2020) / 0x20);
-		sblive_writeac97(card, volreg[ch], wval);
-		return 0;
-
-	case SOUND_MIXER_OGAIN:
-	case SOUND_MIXER_PHONEIN:
-		DPD(4, "SOUND_MIXER_%s:\n", (ch == SOUND_MIXER_PHONEIN) ? "PHONEIN" : "OGAIN");
-		sblive_writeac97(card, volreg[ch], (l1 < 2) ? 0x8000 : ((100 - l1) * 32 + 50) / 100);
-		return 0;
-
-	case SOUND_MIXER_SPEAKER:
-		DPF(4, "SOUND_MIXER_SPEAKER:\n");
-		sblive_writeac97(card, volreg[ch], (l1 < 4) ? 0x8000 : (((100 - l1) * 16 + 50) / 100) << 1);
-		return 0;
-
-	case SOUND_MIXER_MIC:
-		DPF(4, "SOUND_MIXER_MIC:\n");
-		i = 0;
-		if (l1 >= 30)
-			/* 20dB / (34.5dB + 12dB + 20dB) * 100 = 30 */
-		{
-			l1 -= 30;
-			i = 0x40;
-		}
-		sblive_writeac97(card, volreg[ch], (l1 < 2) ? 0x8000 : ((((70 - l1) * 0x20 + 35) / 70) | i));
-		return 0;
-
-	case SOUND_MIXER_RECLEV:
-		DPF(4, "SOUND_MIXER_RECLEV:\n");
-
-		wval = (((l1 * 16 + 50) / 100) << 8) | ((r1 * 16 + 50) / 100);
-		if (wval == 0)
-			wval = 0x8000;
-		else {
-			if (wval & 0xff)
-				wval--;
-			if (wval & 0xff00)
-				wval -= 0x0100;
-		}
-		sblive_writeac97(card, volreg[ch], wval);
-		return 0;
-
-	default:
-		DPF(2, "Got unknown SOUND_MIXER ioctl\n");
-		return -EINVAL;
-	}
-}
-
-static loff_t emu10k1_mixer_llseek(struct file *file, loff_t offset, int origin)
-{
-	DPF(2, "sblive_mixer_llseek() called\n");
-	return -ESPIPE;
-}
-
-/* Mixer file operations */
-
-/* FIXME: Do we need spinlocks in here? */
-/* WARNING! not all the ioctl's are supported by the emu-APS
-   (anything AC97 related). As a general rule keep the AC97 related ioctls
-   separate from the rest. This will make it easier to rewrite the mixer
-   using the kernel AC97 interface. */ 
-static int emu10k1_mixer_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
-{
-	static const char id[] = "SBLive";
-	static const char name[] = "Creative SBLive";
-	int i, val;
-	struct emu10k1_card *card = (struct emu10k1_card *) file->private_data;
-	u16 reg;
-
-	switch (cmd) {
-
-	case SOUND_MIXER_INFO:{
-			mixer_info info;
+			if ((ctl->val[0] & 0x7ff) > 0x3f)
+				ctl->val[1] = 0x00;
 
-			DPF(4, "SOUND_MIXER_INFO\n");
-
-			strncpy(info.id, id, sizeof(info.id));
-			strncpy(info.name, name, sizeof(info.name));
-
-			info.modify_counter = card->modcnt;
-			if (copy_to_user((void *) arg, &info, sizeof(info)))
-				return -EFAULT;
-
-			return 0;
-		}
-		break;
-	case SOUND_OLD_MIXER_INFO:{
-			_old_mixer_info info;
+			ctl->val[2] = sblive_readptr(card, ctl->val[0], ctl->val[1]);
 
-			DPF(4, "SOUND_OLD_MIXER_INFO\n");
+			if (copy_to_user((void *) arg, ctl, sizeof(struct mixer_private_ioctl)))
+				ret = -EFAULT;
 
-			strncpy(info.id, id, sizeof(info.id));
-			strncpy(info.name, name, sizeof(info.name));
+			break;
 
-			if (copy_to_user((void *) arg, &info, sizeof(info)))
-				return -EFAULT;
+		case CMD_SETRECSRC:
+			switch (ctl->val[0]) {
+			case WAVERECORD_AC97:
+				if (card->isaps) {
+					ret = -EINVAL;
+					break;
+				}
+				card->wavein.recsrc = WAVERECORD_AC97;
+				break;
+			case WAVERECORD_MIC:
+				card->wavein.recsrc = WAVERECORD_MIC;
+				break;
+			case WAVERECORD_FX:
+				card->wavein.recsrc = WAVERECORD_FX;
+				card->wavein.fxwc = ctl->val[1] & 0xffff;
+				if (!card->wavein.fxwc)
+					ret = -EINVAL;
+				break;
+			default:
+				ret = -EINVAL;
+				break;
+			}
+			break;
 
-			return 0;
-		}
-		break;
+		case CMD_GETRECSRC:
+			ctl->val[0] = card->wavein.recsrc;
+			ctl->val[1] = card->wavein.fxwc;
+			if (copy_to_user((void *) arg, ctl, sizeof(struct mixer_private_ioctl)))
+				ret = -EFAULT;
 
-	case OSS_GETVERSION:
-		DPF(4, "OSS_GETVERSION\n");
-		return put_user(SOUND_VERSION, (int *) arg);
-		break;
+			break;
 
-	case SOUND_MIXER_PRIVATE1:
-		DPF(4, "SOUND_MIXER_PRIVATE1");
+		case CMD_GETVOICEPARAM:
+			ctl->val[0] = card->waveout.send_routing[0];
+			ctl->val[1] = card->waveout.send_a[0] | card->waveout.send_b[0] << 8 |
+			    	      card->waveout.send_c[0] << 16 | card->waveout.send_d[0] << 24;
+
+			ctl->val[2] = card->waveout.send_routing[1];
+			ctl->val[3] = card->waveout.send_a[1] | card->waveout.send_b[1] << 8 |
+				      card->waveout.send_c[1] << 16 | card->waveout.send_d[1] << 24;
+
+			ctl->val[4] = card->waveout.send_routing[2];
+			ctl->val[5] = card->waveout.send_a[2] | card->waveout.send_b[2] << 8 |
+				     card->waveout.send_c[2] << 16 | card->waveout.send_d[2] << 24;
 
-		if (copy_to_user((void *) arg, card->digmix, sizeof(card->digmix)))
-			return -EFAULT;
+			if (copy_to_user((void *) arg, ctl, sizeof(struct mixer_private_ioctl)))
+				ret = -EFAULT;
 
-		return 0;
+			break;
 
-		break;
-	case SOUND_MIXER_PRIVATE2:
-		DPF(4, "SOUND_MIXER_PRIVATE2");
+		case CMD_SETVOICEPARAM:
+			card->waveout.send_routing[0] = ctl->val[0] & 0xffff;
+			card->waveout.send_a[0] = ctl->val[1] & 0xff;
+			card->waveout.send_b[0] = (ctl->val[1] >> 8) & 0xff;
+			card->waveout.send_c[0] = (ctl->val[1] >> 16) & 0xff;
+			card->waveout.send_d[0] = (ctl->val[1] >> 24) & 0xff;
+
+			card->waveout.send_routing[1] = ctl->val[2] & 0xffff;
+			card->waveout.send_a[1] = ctl->val[3] & 0xff;
+			card->waveout.send_b[1] = (ctl->val[3] >> 8) & 0xff;
+			card->waveout.send_c[1] = (ctl->val[3] >> 16) & 0xff;
+			card->waveout.send_d[1] = (ctl->val[3] >> 24) & 0xff;
+
+			card->waveout.send_routing[2] = ctl->val[4] & 0xffff;
+			card->waveout.send_a[2] = ctl->val[5] & 0xff;
+			card->waveout.send_b[2] = (ctl->val[5] >> 8) & 0xff;
+			card->waveout.send_c[2] = (ctl->val[5] >> 16) & 0xff;
+			card->waveout.send_d[2] = (ctl->val[5] >> 24) & 0xff;
 
-		if (copy_from_user(card->digmix, (void *) arg, sizeof(card->digmix)))
-			return -EFAULT;
+			break;
+		
+		case CMD_SETMCH_FX:
+			card->mchannel_fx = ctl->val[0] & 0x000f;
+			break;
+		
+		case CMD_GETPATCH:
+			if (ctl->val[0] == 0) {
+				if (copy_to_user((void *) arg, &card->mgr.rpatch, sizeof(struct dsp_rpatch)))
+                                	ret = -EFAULT;
+			} else {
+				if ((ctl->val[0] - 1) / PATCHES_PER_PAGE >= card->mgr.current_pages) {
+					ret = -EINVAL;
+					break;
+				}
 
-		for (i = 0; i < sizeof(card->digmix) / sizeof(card->digmix[0]); i++)
-			sblive_writeptr(card, FXGPREGBASE + 0x10 + i, 0, (card->digmix[i] & DM_MUTE) ? 0 : card->digmix[i]);
-		return 0;
+				if (copy_to_user((void *) arg, PATCH(&card->mgr, ctl->val[0] - 1), sizeof(struct dsp_patch)))
+					ret = -EFAULT;
+			}
 
-		break;
-	case SOUND_MIXER_PRIVATE3: {
-			struct mixer_private_ioctl ctl;
+			break;
 
-			if (copy_from_user(&ctl, (void *) arg, sizeof(struct mixer_private_ioctl)))
-				return -EFAULT;
+		case CMD_GETGPR:
+			id = ctl->val[0];
 
-			switch (ctl.cmd) {
-#ifdef EMU10K1_DEBUG
-			case CMD_WRITEFN0:
-				emu10k1_writefn0(card, ctl.val[0], ctl.val[1]);
-				return 0;
+			if (id > NUM_GPRS) {
+				ret = -EINVAL;
 				break;
+			}
 
-			case CMD_WRITEPTR:
-				if(ctl.val[1] >= 0x40)
-					return -EINVAL;
-
-				if(ctl.val[0] > 0xff)
-					return -EINVAL;
-
-				if((ctl.val[0] & 0x7ff) > 0x3f)
-					ctl.val[1] = 0x00;
-
-				sblive_writeptr(card, ctl.val[0], ctl.val[1], ctl.val[2]);
+			if (copy_to_user((void *) arg, &card->mgr.gpr[id], sizeof(struct dsp_gpr)))
+				ret = -EFAULT;
 
-				return 0;
-				break;
-#endif
-			case CMD_READFN0:
-				ctl.val[2] = emu10k1_readfn0(card, ctl.val[0]);
+			break;
 
-				if (copy_to_user((void *) arg, &ctl, sizeof(struct mixer_private_ioctl)))
-					return -EFAULT;
+		case CMD_GETCTLGPR:
+			addr = emu10k1_find_control_gpr(&card->mgr, (char *) ctl->val, &((char *) ctl->val)[PATCH_NAME_SIZE]);
+			ctl->val[0] = sblive_readptr(card, addr, 0);
 
-				return 0;
-				break;
+			if (copy_to_user((void *) arg, ctl, sizeof(struct mixer_private_ioctl)))
+				ret = -EFAULT;
 
-			case CMD_READPTR:
-				if(ctl.val[1] >= 0x40)
-					return -EINVAL;
+			break;
 
-				if((ctl.val[0] & 0x7ff) > 0xff)
-					return -EINVAL;
+		case CMD_SETPATCH:
+			if (ctl->val[0] == 0)
+				memcpy(&card->mgr.rpatch, &ctl->val[1], sizeof(struct dsp_rpatch));
+			else {
+				page = (ctl->val[0] - 1) / PATCHES_PER_PAGE;
+				if (page > MAX_PATCHES_PAGES) {
+					ret = -EINVAL;
+					break;
+				}
 
-				if((ctl.val[0] & 0x7ff) > 0x3f)
-					ctl.val[1] = 0x00;
+				if (page >= card->mgr.current_pages) {
+					for(i = card->mgr.current_pages; i < page + 1; i++) {
+				                card->mgr.patch[i] = (void *)__get_free_pages(GFP_KERNEL, 1);
+						if(card->mgr.patch[i] == NULL) {
+							card->mgr.current_pages = i;
+							ret = -ENOMEM;
+							break;
+						}
+						memset(card->mgr.patch[i], 0, PAGE_SIZE);
+					}
+					card->mgr.current_pages = page + 1;
+				}
 
-				ctl.val[2] = sblive_readptr(card, ctl.val[0], ctl.val[1]);
+				patch = PATCH(&card->mgr, ctl->val[0] - 1);
 
-				if (copy_to_user((void *) arg, &ctl, sizeof(struct mixer_private_ioctl)))
-					return -EFAULT;
+				memcpy(patch, &ctl->val[1], sizeof(struct dsp_patch));
 
-				return 0;
-				break;
+				if(patch->code_size == 0) {
+					for(i = page + 1; i < card->mgr.current_pages; i++)
+                                                free_page((unsigned long) card->mgr.patch[i]);
 
-			case CMD_SETRECSRC:
-				switch(ctl.val[0]){
-				case WAVERECORD_AC97:
-					if(card->isaps)
-						return -EINVAL;
-					card->wavein.recsrc = WAVERECORD_AC97;
-					break;
-				case WAVERECORD_MIC:	
-					card->wavein.recsrc = WAVERECORD_MIC;
-					break;
-				case WAVERECORD_FX:
-					card->wavein.recsrc = WAVERECORD_FX;
-					card->wavein.fxwc = ctl.val[1] & 0xffff;
-					if(!card->wavein.fxwc)
-						return -EINVAL;
-					break;
-				default:
-					return -EINVAL;
+					card->mgr.current_pages = page + 1;
 				}
-				return 0;
-				break;
-
-			case CMD_GETRECSRC:
-				ctl.val[0] = card->wavein.recsrc;
-				ctl.val[1] = card->wavein.fxwc;
-				if (copy_to_user((void *) arg, &ctl, sizeof(struct mixer_private_ioctl)))
-					return -EFAULT;
+			}
+			break;
 
-				return 0;
+		case CMD_SETGPR:
+			if (ctl->val[0] > NUM_GPRS) {
+				ret = -EINVAL;
 				break;
+			}
 
-			case CMD_GETVOICEPARAM:
-
-				ctl.val[0] = card->waveout.send_routing[0];
-				ctl.val[1] = card->waveout.send_a[0] | card->waveout.send_b[0] << 8 |
-				             card->waveout.send_c[0] << 16 | card->waveout.send_d[0] << 24;
-
-				ctl.val[2] = card->waveout.send_routing[1]; 
-				ctl.val[3] = card->waveout.send_a[1] | card->waveout.send_b[1] << 8 |
-					     card->waveout.send_c[1] << 16 | card->waveout.send_d[1] << 24;
+			memcpy(&card->mgr.gpr[ctl->val[0]], &ctl->val[1], sizeof(struct dsp_gpr));
+			break;
 
-				ctl.val[4] = card->waveout.send_routing[2]; 
-				ctl.val[5] = card->waveout.send_a[2] | card->waveout.send_b[2] << 8 |
-					     card->waveout.send_c[2] << 16 | card->waveout.send_d[2] << 24;
+		case CMD_SETCTLGPR:
+			addr = emu10k1_find_control_gpr(&card->mgr, (char *) ctl->val, (char *) ctl->val + PATCH_NAME_SIZE);
+			emu10k1_set_control_gpr(card, addr, *((s32 *)((char *) ctl->val + 2 * PATCH_NAME_SIZE)), 0);
+			break;
 
-				if (copy_to_user((void *) arg, &ctl, sizeof(struct mixer_private_ioctl)))
-					return -EFAULT;
 
-				return 0;
+		case CMD_SETGPOUT:
+			if( ctl->val[0]>2 || ctl->val[1]>1){
+				ret= -EINVAL;
 				break;
+			}
+			emu10k1_writefn0(card, (1<<24)| (((ctl->val[0])+10)<<16 ) | HCFG ,ctl->val[1]);
+			break;
 
-			case CMD_SETVOICEPARAM:
-				card->waveout.send_routing[0] = ctl.val[0] & 0xffff;
-				card->waveout.send_a[0] = ctl.val[1] & 0xff;
-				card->waveout.send_b[0] = (ctl.val[1] >> 8) & 0xff;
-				card->waveout.send_c[0] = (ctl.val[1] >> 16) & 0xff;
-				card->waveout.send_d[0] = (ctl.val[1] >> 24) & 0xff;
-
-				card->waveout.send_routing[1] = ctl.val[2] & 0xffff;
-				card->waveout.send_a[1] = ctl.val[3] & 0xff;
-				card->waveout.send_b[1] = (ctl.val[3] >> 8) & 0xff;
-				card->waveout.send_c[1] = (ctl.val[3] >> 16) & 0xff;
-				card->waveout.send_d[1] = (ctl.val[3] >> 24) & 0xff;
-
-				card->waveout.send_routing[2] = ctl.val[4] & 0xffff;
-				card->waveout.send_a[2] = ctl.val[5] & 0xff;
-				card->waveout.send_b[2] = (ctl.val[5] >> 8) & 0xff;
-				card->waveout.send_c[2] = (ctl.val[5] >> 16) & 0xff;
-				card->waveout.send_d[2] = (ctl.val[5] >> 24) & 0xff;
-
-				return 0;
-				break;
+		case CMD_GETGPR2OSS:
+			id = ctl->val[0];
+			ch = ctl->val[1];
 
-			default:
-				return -EINVAL;
+			if (id >= SOUND_MIXER_NRDEVICES || ch >= 2) {
+				ret = -EINVAL;
 				break;
 			}
-		}
-		break;
-
-	case SOUND_MIXER_PRIVATE4:{
-			u32 size;
-			int size_reg = 0;
-
-			if (copy_from_user(&size, (void *) arg, sizeof(size)))
-				return -EFAULT;
 
-			DPD(2,"External tram size 0x%x\n", size);
+			ctl->val[2] = card->mgr.ctrl_gpr[id][ch];
 
-			if(size > 0x1fffff)
-				return -EINVAL;
-
-			if (size != 0) {	
-				size = (size - 1) >> 14;
+			if (copy_to_user((void *) arg, ctl, sizeof(struct mixer_private_ioctl)))
+				ret = -EFAULT;
+			break;
 
-        	                while (size) {
-                	                size >>= 1;
-                        	        size_reg++;
-                        	}
+		case CMD_SETGPR2OSS:
+			id = ctl->val[0];
+			ch = ctl->val[1];
+			addr = ctl->val[2];
 
-                        	size = 0x4000 << size_reg;
+			if (id >= SOUND_MIXER_NRDEVICES || ch >= 2) {
+				ret = -EINVAL;
+				break;
 			}
 
-			DPD(2,"External tram size 0x%x 0x%x\n", size, size_reg);
-
-			if (size != card->tankmem.size) {
-				if (card->tankmem.size > 0) {
-					emu10k1_writefn0(card, HCFG_LOCKTANKCACHE, 1);
+			card->mgr.ctrl_gpr[id][ch] = addr;
 
-					sblive_writeptr_tag(card, 0, TCB, 0,
-							    TCBS, 0,
-							    TAGLIST_END);
+			if (card->isaps)
+				break;
 
-					pci_free_consistent(card->pci_dev, card->tankmem.size,
-							    card->tankmem.addr, card->tankmem.dma_handle);
+			if (addr >= 0) {
+				unsigned int state = card->ac97.mixer_state[id];
 
-					card->tankmem.size = 0;
+				if (ch) {
+					state >>= 8;
+					card->ac97.stereo_mixers |= (1<<id);
+				} else {
+					card->ac97.supported_mixers |= (1<<id);
 				}
 
-				if (size != 0) {
-					if ((card->tankmem.addr = pci_alloc_consistent(card->pci_dev, size,
-					     &card->tankmem.dma_handle)) == NULL)
-						return -ENOMEM;
-
-					card->tankmem.size = size;
-
-					sblive_writeptr_tag(card, 0, TCB, card->tankmem.dma_handle,
-                        	                            TCBS, size_reg,
-                                	                    TAGLIST_END);
-
-					emu10k1_writefn0(card, HCFG_LOCKTANKCACHE, 0);
+				emu10k1_set_volume_gpr(card, addr, state & 0xff,
+						       volume_params[id].scale,
+						       volume_params[id].muting);
+			} else {
+				if (ch) {
+					card->ac97.stereo_mixers &= ~(1<<id);
+					card->ac97.stereo_mixers |= card->ac97_stereo_mixers;
+				} else {
+					card->ac97.supported_mixers &= ~(1<<id);
+					card->ac97.supported_mixers |= card->ac97_supported_mixers;
 				}
 			}
-			return 0;
+			break;
+		case CMD_SETPASSTHROUGH:
+			card->pt.selected = ctl->val[0] ? 1 : 0;
+			if (card->pt.state != PT_STATE_INACTIVE)
+				break;
+			card->pt.spcs_to_use = ctl->val[0] & 0x07;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
 		}
-		break;
 
-	default:
+		kfree(ctl);
+		return ret;
 		break;
-	}
 
-	if (_IOC_TYPE(cmd) != 'M' || _IOC_SIZE(cmd) != sizeof(int))
-		return -EINVAL;
+	case SOUND_MIXER_PRIVATE4:
 
-	if (_IOC_DIR(cmd) == _IOC_READ) {
-		switch (_IOC_NR(cmd)) {
-			case SOUND_MIXER_DEVMASK:       /* Arg contains a bit for each supported device */
-                        DPF(4, "SOUND_MIXER_READ_DEVMASK\n");
-			if (card->isaps)
-#ifdef TONE_CONTROL
-				return put_user(SOUND_MASK_PCM | SOUND_MASK_VOLUME |
-						SOUND_MASK_BASS | SOUND_MASK_TREBLE,
-						(int *) arg); 
-#else
-				return put_user(SOUND_MASK_PCM | SOUND_MASK_VOLUME,
-						(int *) arg); 
-#endif
-		
-#ifdef TONE_CONTROL
-			return put_user(SOUND_MASK_LINE | SOUND_MASK_CD |
-                                        SOUND_MASK_OGAIN | SOUND_MASK_LINE1 |
-                                        SOUND_MASK_PCM | SOUND_MASK_VOLUME |
-                                        SOUND_MASK_PHONEIN | SOUND_MASK_MIC |
-                                        SOUND_MASK_BASS | SOUND_MASK_TREBLE |
-                                        SOUND_MASK_RECLEV | SOUND_MASK_SPEAKER |
-                                        SOUND_MASK_LINE3 | SOUND_MASK_DIGITAL1 | 
-                                        SOUND_MASK_DIGITAL2 | SOUND_MASK_LINE2, (int *) arg);
-#else
-			return put_user(SOUND_MASK_LINE | SOUND_MASK_CD |
-                                        SOUND_MASK_OGAIN | SOUND_MASK_LINE1 |
-                                        SOUND_MASK_PCM | SOUND_MASK_VOLUME |
-                                        SOUND_MASK_PHONEIN | SOUND_MASK_MIC |
-                                        SOUND_MASK_RECLEV | SOUND_MASK_SPEAKER |
-                                        SOUND_MASK_LINE3 | SOUND_MASK_DIGITAL1 | 
-                                        SOUND_MASK_DIGITAL2 | SOUND_MASK_LINE2, (int *) arg);
-#endif
+		if (copy_from_user(&size, (void *) arg, sizeof(size)))
+			return -EFAULT;
 
-			case SOUND_MIXER_RECMASK:       /* Arg contains a bit for each supported recording source */
-				DPF(2, "SOUND_MIXER_READ_RECMASK\n");
-				if (card->isaps)
-					return put_user(0, (int *) arg);
-
-				return put_user(SOUND_MASK_MIC | SOUND_MASK_CD |
-					SOUND_MASK_LINE1 | SOUND_MASK_LINE |
-					SOUND_MASK_VOLUME | SOUND_MASK_OGAIN |
-					SOUND_MASK_PHONEIN, (int *) arg);
-
-			case SOUND_MIXER_STEREODEVS:    /* Mixer channels supporting stereo */
-				DPF(2, "SOUND_MIXER_READ_STEREODEVS\n");
-
-				if (card->isaps)
-#ifdef TONE_CONTROL
-					return put_user(SOUND_MASK_PCM | SOUND_MASK_VOLUME |
-                                        		SOUND_MASK_BASS | SOUND_MASK_TREBLE,
-                                        		(int *) arg);
-#else
-					return put_user(SOUND_MASK_PCM | SOUND_MASK_VOLUME,
-                                         		(int *) arg);
-#endif
+		DPD(2, "External tram size %#x\n", size);
 
-#ifdef TONE_CONTROL
-				return put_user(SOUND_MASK_LINE | SOUND_MASK_CD |
-					SOUND_MASK_OGAIN | SOUND_MASK_LINE1 |
-					SOUND_MASK_PCM | SOUND_MASK_VOLUME |
-					SOUND_MASK_BASS | SOUND_MASK_TREBLE |
-					SOUND_MASK_RECLEV | SOUND_MASK_LINE3 |
-					SOUND_MASK_DIGITAL1 | SOUND_MASK_DIGITAL2 |
-					SOUND_MASK_LINE2, (int *) arg);
-#else
-				return put_user(SOUND_MASK_LINE | SOUND_MASK_CD |
-					SOUND_MASK_OGAIN | SOUND_MASK_LINE1 |
-					SOUND_MASK_PCM | SOUND_MASK_VOLUME |
-					SOUND_MASK_RECLEV | SOUND_MASK_LINE3 |
-					SOUND_MASK_DIGITAL1 | SOUND_MASK_DIGITAL2 |
-					SOUND_MASK_LINE2, (int *) arg);
-#endif
+		if (size > 0x1fffff)
+			return -EINVAL;
 
-			case SOUND_MIXER_CAPS:
-				DPF(2, "SOUND_MIXER_READ_CAPS\n");
-				return put_user(SOUND_CAP_EXCL_INPUT, (int *) arg);
-#ifdef PRIVATE_PCM_VOLUME
-                case SOUND_MIXER_PCM:
-                        /* needs to be before default: !!*/
-                        {
-                                int i;
-
-                                for (i = 0; i < MAX_PCM_CHANNELS; i++) {
-                                        if (sblive_pcm_volume[i].files == current->files) {
-                                                return put_user((int) sblive_pcm_volume[i].mixer, (int *) arg);
-                                        }
-                                }
-                        }
-#endif
-		default:
-			break;
+		size_reg = 0;
+
+		if (size != 0) {
+			size = (size - 1) >> 14;
+
+			while (size) {
+				size >>= 1;
+				size_reg++;
+			}
+
+			size = 0x4000 << size_reg;
 		}
 
-		switch (_IOC_NR(cmd)) {
-		case SOUND_MIXER_RECSRC:	/* Arg contains a bit for each recording source */
-			DPF(2, "SOUND_MIXER_READ_RECSRC\n");
-			if (card->isaps)
-				return put_user(0, (int *) arg);
+		DPD(2, "External tram size %#x %#x\n", size, size_reg);
 
-			sblive_readac97(card, AC97_RECORDSELECT, &reg);
-			return put_user(recsrc[reg & 7], (int *) arg);
+		if (size != card->tankmem.size) {
+			if (card->tankmem.size > 0) {
+				emu10k1_writefn0(card, HCFG_LOCKTANKCACHE, 1);
 
-		default:
-			i = _IOC_NR(cmd);
-			DPD(4, "SOUND_MIXER_READ(%d)\n", i);
-			if (i >= SOUND_MIXER_NRDEVICES)
-				return -EINVAL;
-#ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
-			return mixer_rdch(card, i, (int *) arg);
-#else				/* OSS_DOCUMENTED_MIXER_SEMANTICS */
-			if (!volidx[i])
-				return -EINVAL;
-			return put_user(card->arrwVol[volidx[i]], (int *) arg);
+				sblive_writeptr_tag(card, 0, TCB, 0, TCBS, 0, TAGLIST_END);
 
-#endif				/* OSS_DOCUMENTED_MIXER_SEMANTICS */
-		}
-	}
+				pci_free_consistent(card->pci_dev, card->tankmem.size, card->tankmem.addr, card->tankmem.dma_handle);
 
-	/* End of _IOC_READ */
-	if (_IOC_DIR(cmd) != (_IOC_READ | _IOC_WRITE))
-		return -EINVAL;
-
-	/* _IOC_WRITE */
-	card->modcnt++;
-
-	switch (_IOC_NR(cmd)) {
-	case SOUND_MIXER_RECSRC:	/* Arg contains a bit for each recording source */
-		DPF(2, "SOUND_MIXER_WRITE_RECSRC\n");
+				card->tankmem.size = 0;
+			}
 
-		if (card->isaps)
-			return -EINVAL;
+			if (size != 0) {
+				card->tankmem.addr = pci_alloc_consistent(card->pci_dev, size, &card->tankmem.dma_handle);
+				if (card->tankmem.addr == NULL)
+					return -ENOMEM;
 
-		if (get_user(val, (int *) arg))
-			return -EFAULT;
+				card->tankmem.size = size;
 
-		i = hweight32(val);
-		if (i == 0)
-			return 0;	/* val = mixer_recmask(s); */
-		else if (i > 1) {
-			sblive_readac97(card, AC97_RECORDSELECT, &reg);
-			val &= ~recsrc[reg & 7];
-		}
+				sblive_writeptr_tag(card, 0, TCB, card->tankmem.dma_handle, TCBS, size_reg, TAGLIST_END);
 
-		for (i = 0; i < 8; i++) {
-			if (val & recsrc[i]) {
-				DPD(2, "Selecting record source to be 0x%04x\n", 0x0101 * i);
-				sblive_writeac97(card, AC97_RECORDSELECT, 0x0101 * i);
-				return 0;
+				emu10k1_writefn0(card, HCFG_LOCKTANKCACHE, 0);
 			}
 		}
 		return 0;
+		break;
 
 	default:
-		i = _IOC_NR(cmd);
-		DPD(4, "SOUND_MIXER_WRITE(%d)\n", i);
+		break;
+	}
 
-		if (i >= SOUND_MIXER_NRDEVICES)
-			return -EINVAL;
-		if (get_user(val, (int *) arg))
-			return -EFAULT;
+	return -EINVAL;
+}
 
-		if (emu10k1_mixer_wrch(card, i, val))
-			return -EINVAL;
 
-#ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
-		return mixer_rdch(card, i, (int *) arg);
-#else				/* OSS_DOCUMENTED_MIXER_SEMANTICS */
-		return put_user(card->arrwVol[volidx[i]], (int *) arg);
-#endif				/* OSS_DOCUMENTED_MIXER_SEMANTICS */
+	
+static int emu10k1_mixer_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
+{
+	int ret;
+	struct emu10k1_card *card = file->private_data;
 
+	ret = -EINVAL;
+	if (!card->isaps)
+		ret = card->ac97.mixer_ioctl(&card->ac97, cmd, arg);
+
+	if (ret < 0)
+		ret = emu10k1_private_mixer(card, cmd, arg);
+	else{
+		unsigned int oss_mixer, left, right;
+
+		oss_mixer = _IOC_NR(cmd);
+
+		if ((_IOC_DIR(cmd) == (_IOC_WRITE|_IOC_READ)) && oss_mixer<=SOUND_MIXER_NRDEVICES ) {
+
+			left = card->ac97.mixer_state[oss_mixer] & 0xff;
+			right = (card->ac97.mixer_state[oss_mixer] >> 8) & 0xff;
+			if(card->ac97.supported_mixers|(1<<oss_mixer))
+				emu10k1_set_volume_gpr(card, card->mgr.ctrl_gpr[oss_mixer][0], left,
+						       volume_params[oss_mixer].scale,
+						       volume_params[oss_mixer].muting);
+			if(card->ac97.stereo_mixers |(1<<oss_mixer))
+				emu10k1_set_volume_gpr(card, card->mgr.ctrl_gpr[oss_mixer][1], right,
+						       volume_params[oss_mixer].scale,
+						       volume_params[oss_mixer].muting);
+		}
+		
 	}
+	
+	
+		
+	return ret;
 }
 
 static int emu10k1_mixer_open(struct inode *inode, struct file *file)
 {
 	int minor = MINOR(inode->i_rdev);
-	struct emu10k1_card *card;
+	struct emu10k1_card *card = NULL;
 	struct list_head *entry;
 
 	DPF(4, "emu10k1_mixer_open()\n");
@@ -1090,13 +491,13 @@
 	list_for_each(entry, &emu10k1_devs) {
 		card = list_entry(entry, struct emu10k1_card, list);
 
-		if (card->mixer_num == minor)
-			break;
+		if (card->ac97.dev_mixer == minor)
+			goto match;
 	}
 
-	if (entry == &emu10k1_devs)
-		return -ENODEV;
+	return -ENODEV;
 
+      match:
 	file->private_data = card;
 	return 0;
 }
@@ -1108,7 +509,7 @@
 }
 
 struct file_operations emu10k1_mixer_fops = {
-        owner:		THIS_MODULE,
+	owner:		THIS_MODULE,
 	llseek:		emu10k1_mixer_llseek,
 	ioctl:		emu10k1_mixer_ioctl,
 	open:		emu10k1_mixer_open,
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/passthrough.c linux/drivers/sound/emu10k1/passthrough.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/passthrough.c	Wed Dec 31 16:00:00 1969
+++ linux/drivers/sound/emu10k1/passthrough.c	Mon Dec  3 23:00:45 2001
@@ -0,0 +1,243 @@
+/*
+ **********************************************************************
+ *     passthrough.c -- Emu10k1 digital passthrough
+ *     Copyright (C) 2001  Juha Yrjölä <jyrjola@cc.hut.fi>
+ *
+ **********************************************************************
+ *
+ *     Date                 Author          Summary of changes
+ *     ----                 ------          ------------------
+ *     May 15, 2001	    Juha Yrjölä	    base code release
+ *
+ **********************************************************************
+ *
+ *     This program is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU General Public License as
+ *     published by the Free Software Foundation; either version 2 of
+ *     the License, or (at your option) any later version.
+ *
+ *     This program is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *     GNU General Public License for more details.
+ *
+ *     You should have received a copy of the GNU General Public
+ *     License along with this program; if not, write to the Free
+ *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
+ *     USA.
+ *
+ **********************************************************************
+ */
+                       
+#define __NO_VERSION__
+#include <linux/module.h>
+#include <linux/poll.h>
+#include <linux/malloc.h>
+#include <linux/version.h>
+#include <linux/bitops.h>
+#include <asm/io.h>
+#include <linux/sched.h>
+#include <linux/smp_lock.h>
+#include <linux/wrapper.h>
+
+#include "hwaccess.h"
+#include "cardwo.h"
+#include "cardwi.h"
+#include "recmgr.h"
+#include "irqmgr.h"
+#include "audio.h"
+#include "8010.h"
+#include "passthrough.h"
+
+static void pt_putsamples(struct pt_data *pt, u16 *ptr, u16 left, u16 right)
+{
+	unsigned int idx;
+
+	ptr[pt->copyptr] = left;
+	idx = pt->copyptr + PT_SAMPLES/2;
+	idx %= PT_SAMPLES;
+	ptr[idx] = right;
+}
+
+static inline int pt_can_write(struct pt_data *pt)
+{
+	return pt->blocks_copied < pt->blocks_played + 8;
+}
+
+static int pt_wait_for_write(struct emu10k1_wavedevice *wavedev, int nonblock)
+{
+	struct emu10k1_card *card = wavedev->card;
+	struct pt_data *pt = &card->pt;
+
+	if (nonblock && !pt_can_write(pt))
+		return -EAGAIN;
+	while (!pt_can_write(pt) && pt->state != PT_STATE_INACTIVE) {
+		interruptible_sleep_on(&pt->wait);
+		if (signal_pending(current))
+			return -ERESTARTSYS;
+	}
+	if (pt->state == PT_STATE_INACTIVE)
+		return -EAGAIN;
+	
+	return 0;
+}
+
+static int pt_putblock(struct emu10k1_wavedevice *wave_dev, u16 *block, int nonblock)
+{
+	struct woinst *woinst = wave_dev->woinst;
+	struct emu10k1_card *card = wave_dev->card;
+	struct pt_data *pt = &card->pt;
+	u16 *ptr = (u16 *) card->tankmem.addr;
+	int i = 0, r;
+	unsigned long flags;
+
+	r = pt_wait_for_write(wave_dev, nonblock);
+	if (r < 0)
+		return r;
+	spin_lock_irqsave(&card->pt.lock, flags);
+	while (i < PT_BLOCKSAMPLES) {
+		pt_putsamples(pt, ptr, block[2*i], block[2*i+1]);
+		if (pt->copyptr == 0)
+			pt->copyptr = PT_SAMPLES;
+		pt->copyptr--;
+		i++;
+	}
+	woinst->total_copied += PT_BLOCKSIZE;
+	pt->blocks_copied++;
+	if (pt->blocks_copied >= 4 && pt->state != PT_STATE_PLAYING) {
+		DPF(2, "activating digital pass-through playback\n");
+		sblive_writeptr(card, GPR_BASE + pt->enable_gpr, 0, 1);
+		pt->state = PT_STATE_PLAYING;
+	}
+	spin_unlock_irqrestore(&card->pt.lock, flags);
+	return 0;
+}
+
+static int pt_setup(struct emu10k1_wavedevice *wave_dev)
+{
+	u32 bits;
+	struct emu10k1_card *card = wave_dev->card;
+	struct pt_data *pt = &card->pt;
+	int i;
+
+	for (i = 0; i < 3; i++) {
+		pt->old_spcs[i] = sblive_readptr(card, SPCS0 + i, 0);
+		if (pt->spcs_to_use & (1 << i)) {
+			DPD(2, "using S/PDIF port %d\n", i);
+			bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
+				SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
+				0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
+			if (pt->ac3data)
+				bits |= SPCS_NOTAUDIODATA;
+			sblive_writeptr(card, SPCS0 + i, 0, bits);
+		}
+	}
+	return 0;
+}
+
+ssize_t emu10k1_pt_write(struct file *file, const char *buffer, size_t count)
+{
+	struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
+	struct emu10k1_card *card = wave_dev->card;
+	struct pt_data *pt = &card->pt;
+	int nonblock, i, r, blocks, blocks_copied, bytes_copied = 0;
+
+	DPD(3, "emu10k1_pt_write(): %d bytes\n", count);
+	
+	nonblock = file->f_flags & O_NONBLOCK;
+	
+	if (card->tankmem.size < PT_SAMPLES*2)
+		return -EFAULT;
+	if (pt->state == PT_STATE_INACTIVE) {
+		DPF(2, "bufptr init\n");
+		pt->playptr = PT_SAMPLES-1;
+		pt->copyptr = PT_INITPTR;
+		pt->blocks_played = pt->blocks_copied = 0;
+		memset(card->tankmem.addr, 0, card->tankmem.size);
+		pt->state = PT_STATE_ACTIVATED;
+		pt->buf = kmalloc(PT_BLOCKSIZE, GFP_KERNEL);
+		pt->prepend_size = 0;
+		if (pt->buf == NULL)
+			return -ENOMEM;
+		pt_setup(wave_dev);
+	}
+	if (pt->prepend_size) {
+		int needed = PT_BLOCKSIZE - pt->prepend_size;
+
+		DPD(3, "prepend size %d, prepending %d bytes\n", pt->prepend_size, needed);
+		if (count < needed) {
+			copy_from_user(pt->buf + pt->prepend_size, buffer, count);
+			pt->prepend_size += count;
+			DPD(3, "prepend size now %d\n", pt->prepend_size);
+			return count;
+		}
+		copy_from_user(pt->buf + pt->prepend_size, buffer, needed);
+		r = pt_putblock(wave_dev, (u16 *) pt->buf, nonblock);
+		if (r)
+			return r;
+		bytes_copied += needed;
+		pt->prepend_size = 0;
+	}
+	blocks = (count-bytes_copied)/PT_BLOCKSIZE;
+	blocks_copied = 0;
+	while (blocks > 0) {
+		u16 *bufptr = (u16 *) buffer + (bytes_copied/2);
+		copy_from_user(pt->buf, bufptr, PT_BLOCKSIZE);
+		bufptr = (u16 *) pt->buf;
+		r = pt_putblock(wave_dev, bufptr, nonblock);
+		if (r) {
+			if (bytes_copied)
+				return bytes_copied;
+			else
+				return r;
+		}
+		bytes_copied += PT_BLOCKSIZE;
+		blocks--;
+		blocks_copied++;
+	}
+	i = count - bytes_copied;
+	if (i) {
+		pt->prepend_size = i;
+		copy_from_user(pt->buf, buffer + bytes_copied, i);
+		bytes_copied += i;
+		DPD(3, "filling prepend buffer with %d bytes", i);
+	}
+	return bytes_copied;
+}
+
+void emu10k1_pt_stop(struct emu10k1_card *card)
+{
+	struct pt_data *pt = &card->pt;
+	int i;
+	unsigned long flags;
+
+	spin_lock_irqsave(&card->pt.lock, flags);
+	if (pt->state != PT_STATE_INACTIVE) {
+		DPF(2, "digital pass-through stopped\n");
+		sblive_writeptr(card, GPR_BASE + pt->enable_gpr, 0, 0);
+		for (i = 0; i < 3; i++) {
+                        if (pt->spcs_to_use & (1 << i))
+				sblive_writeptr(card, SPCS0 + i, 0, pt->old_spcs[i]);
+		}
+		pt->state = PT_STATE_INACTIVE;
+		kfree(pt->buf);
+	}
+	spin_unlock_irqrestore(&card->pt.lock, flags);
+}
+
+void emu10k1_pt_waveout_update(struct emu10k1_wavedevice *wave_dev)
+{
+	struct woinst *woinst = wave_dev->woinst;
+	struct pt_data *pt = &wave_dev->card->pt;
+	u32 pos;
+
+	if (pt->state == PT_STATE_PLAYING && pt->pos_gpr >= 0) {
+		pos = sblive_readptr(wave_dev->card, GPR_BASE + pt->pos_gpr, 0);
+		if (pos > PT_BLOCKSAMPLES)
+			pos = PT_BLOCKSAMPLES;
+		pos = 4 * (PT_BLOCKSAMPLES - pos);
+	} else
+		pos = 0;
+	woinst->total_played = pt->blocks_played * woinst->buffer.fragment_size + pos;
+	woinst->buffer.hw_pos = pos;
+}
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/passthrough.h linux/drivers/sound/emu10k1/passthrough.h
--- linux-2.4.8-pre8/drivers/sound/emu10k1/passthrough.h	Wed Dec 31 16:00:00 1969
+++ linux/drivers/sound/emu10k1/passthrough.h	Mon Dec  3 23:00:45 2001
@@ -0,0 +1,70 @@
+/*
+ **********************************************************************
+ *     passthrough.h -- Emu10k1 digital passthrough header file
+ *     Copyright (C) 2001  Juha Yrjölä <jyrjola@cc.hut.fi>
+ *
+ **********************************************************************
+ *
+ *     Date                 Author          Summary of changes
+ *     ----                 ------          ------------------
+ *     May 15, 2001	    Juha Yrjölä     base code release
+ *
+ **********************************************************************
+ *
+ *     This program is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU General Public License as
+ *     published by the Free Software Foundation; either version 2 of
+ *     the License, or (at your option) any later version.
+ *
+ *     This program is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *     GNU General Public License for more details.
+ *
+ *     You should have received a copy of the GNU General Public
+ *     License along with this program; if not, write to the Free
+ *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
+ *     USA.
+ *
+ **********************************************************************
+ */
+
+#ifndef _PASSTHROUGH_H
+#define _PASSTHROUGH_H
+
+#include "audio.h"
+
+/* number of 16-bit stereo samples in XTRAM buffer */
+#define PT_SAMPLES 0x8000
+#define PT_BLOCKSAMPLES 0x400
+#define PT_BLOCKSIZE (PT_BLOCKSAMPLES*4)
+#define PT_BLOCKSIZE_LOG2 12
+#define PT_BLOCKCOUNT (PT_SAMPLES/PT_BLOCKSAMPLES)
+#define PT_INITPTR (PT_SAMPLES/2-1)
+
+#define PT_STATE_INACTIVE 0
+#define PT_STATE_ACTIVATED 1
+#define PT_STATE_PLAYING 2
+
+/* passthrough struct */
+struct pt_data
+{
+	u8	selected, state, spcs_to_use;
+	int	intr_gpr, enable_gpr, pos_gpr;
+	u32	blocks_played, blocks_copied, old_spcs[3];
+	u32	playptr, copyptr;
+	u32	prepend_size;
+	u8	*buf;
+	u8	ac3data;
+
+	char	*patch_name, *intr_gpr_name, *enable_gpr_name, *pos_gpr_name;
+
+	wait_queue_head_t wait;
+	spinlock_t lock;
+};
+
+ssize_t emu10k1_pt_write(struct file *file, const char *buf, size_t count);
+void emu10k1_pt_stop(struct emu10k1_card *card);
+void emu10k1_pt_waveout_update(struct emu10k1_wavedevice *wave_dev);
+
+#endif /* _PASSTHROUGH_H */
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/recmgr.c linux/drivers/sound/emu10k1/recmgr.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/recmgr.c	Mon Aug 14 08:32:48 2000
+++ linux/drivers/sound/emu10k1/recmgr.c	Mon Dec  3 23:00:45 2001
@@ -130,7 +130,7 @@
 		break;
 	}
 
-	DPD(2, "bus addx: %x\n", buffer->dma_handle);
+	DPD(2, "bus addx: %#x\n", buffer->dma_handle);
 
 	sblive_writeptr(card, buffer->addrreg, 0, buffer->dma_handle);
 
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/voicemgr.c linux/drivers/sound/emu10k1/voicemgr.c
--- linux-2.4.8-pre8/drivers/sound/emu10k1/voicemgr.c	Mon Aug 14 08:32:48 2000
+++ linux/drivers/sound/emu10k1/voicemgr.c	Mon Dec  3 23:00:45 2001
@@ -65,21 +65,6 @@
 	voice->card = card;
 	voice->num = i;
 
-#ifdef PRIVATE_PCM_VOLUME
-
-	for (i = 0; i < MAX_PCM_CHANNELS; i++) {
-		if (sblive_pcm_volume[i].files == current->files) {
-			sblive_pcm_volume[i].channel_l = voice->num;
-			DPD(2, "preset left: %d\n", voice->num);
-			if (voice->flags & VOICE_FLAGS_STEREO) {
-				sblive_pcm_volume[i].channel_r = voice->num + 1;
-				DPD(2, "preset right: %d\n", voice->num + 1);
-			}
-			break;
-		}
-	}
-#endif
-
 	for (i = 0; i < (voice->flags & VOICE_FLAGS_STEREO ? 2 : 1); i++) {
 		DPD(2, " voice allocated -> %d\n", voice->num + i);
 
@@ -104,20 +89,6 @@
 	if (voice->usage == VOICE_USAGE_FREE)
 		return;
 
-#ifdef PRIVATE_PCM_VOLUME
-	for (i = 0; i < MAX_PCM_CHANNELS; i++) {
-		if (sblive_pcm_volume[i].files == current->files) {
-			if (voice->num == sblive_pcm_volume[i].channel_l)
-				sblive_pcm_volume[i].channel_l = NUM_G;
-			if ((voice->flags & VOICE_FLAGS_STEREO)
-			    && (voice->num + 1) == sblive_pcm_volume[i].channel_r) {
-				sblive_pcm_volume[i].channel_r = NUM_G;
-			}
-			break;
-		}
-	}
-#endif
-
 	for (i = 0; i < (voice->flags & VOICE_FLAGS_STEREO ? 2 : 1); i++) {
 		DPD(2, " voice released -> %d\n", voice->num + i);
 
@@ -139,8 +110,6 @@
 		card->voicetable[voice->num + 1] = VOICE_USAGE_FREE;
 
 	spin_unlock_irqrestore(&card->lock, flags);
-
-	return;
 }
 
 void emu10k1_voice_playback_setup(struct emu_voice *voice)
@@ -203,114 +172,110 @@
 				/* pitch envelope */
 				    PEFE_PITCHAMOUNT, 0, TAGLIST_END);
 
-#ifdef PRIVATE_PCM_VOLUME
-{
-int j;
-        for (j = 0; j < MAX_PCM_CHANNELS; j++) {
-                if (sblive_pcm_volume[j].channel_l == voice->num + i) {
-                        voice->params[i].initial_attn = (sblive_pcm_volume[j].channel_r < NUM_G) ? sblive_pcm_volume[i].attn_l :
-                // test for mono channel (reverse logic is correct here!)
-                                            (sblive_pcm_volume[j].attn_r >
-                                             sblive_pcm_volume[j].attn_l) ? sblive_pcm_volume[j].attn_l : sblive_pcm_volume[j].attn_r;
-                                        DPD(2, "set left volume %d\n", voice->params[i].initial_attn);
-                                        break;
-                                } else if (sblive_pcm_volume[j].channel_r == voice->num + i) {
-                                        voice->params[i].initial_attn = sblive_pcm_volume[j].attn_r;
-                                        DPD(2, "set right volume %d\n", voice->params[i].initial_attn);
-                                        break;
-                                }
-                        }
-                }
-#endif
-
 		voice->params[i].fc_target = 0xffff;
 	}
-
-	return;
 }
 
-void emu10k1_voice_start(struct emu_voice *voice, int set)
+void emu10k1_voices_start(struct emu_voice *first_voice, unsigned int num_voices, int set)
 {
-	struct emu10k1_card *card = voice->card;
-	int i;
+	struct emu10k1_card *card = first_voice->card;
+	struct emu_voice *voice;
+	unsigned int voicenum;
+	int j;
+
+	DPF(2, "emu10k1_voices_start()\n");
+
+	for (voicenum = 0; voicenum < num_voices; voicenum++)
+	{
+		voice = first_voice + voicenum;
 
-	DPF(2, "emu10k1_voice_start()\n");
+		if (!set) {
+			u32 cra, ccis, cs, sample;
+			if (voice->flags & VOICE_FLAGS_STEREO) {
+				cra = 64;
+				ccis = 28;
+				cs = 4;
+			} else {
+				cra = 64;
+				ccis = 30;
+				cs = 2;
+			}
 
-	if (!set) {
-		u32 cra, ccis, cs, sample;
-		if (voice->flags & VOICE_FLAGS_STEREO) {
-			cra = 64;
-			ccis = 28;
-			cs = 4;
-		} else {
-			cra = 64;
-			ccis = 30;
-			cs = 2;
-		}
+			if(voice->flags & VOICE_FLAGS_16BIT) {
+				sample = 0x00000000;
+			} else {
+				sample = 0x80808080;		
+				ccis *= 2;
+			}
 
-		if(voice->flags & VOICE_FLAGS_16BIT) {
-			sample = 0x00000000;
-		} else {
-			sample = 0x80808080;		
-			ccis *= 2;
-		}
+			for(j = 0; j < cs; j++)
+	        	        sblive_writeptr(card, CD0 + j, voice->num, sample);
 
-		for(i = 0; i < cs; i++)
-	                sblive_writeptr(card, CD0 + i, voice->num, sample);
+			/* Reset cache */
+			sblive_writeptr(card, CCR_CACHEINVALIDSIZE, voice->num, 0);
+			if (voice->flags & VOICE_FLAGS_STEREO)
+				sblive_writeptr(card, CCR_CACHEINVALIDSIZE, voice->num + 1, 0);
 
-		/* Reset cache */
-		sblive_writeptr(card, CCR_CACHEINVALIDSIZE, voice->num, 0);
-		if (voice->flags & VOICE_FLAGS_STEREO)
-			sblive_writeptr(card, CCR_CACHEINVALIDSIZE, voice->num + 1, 0);
+			sblive_writeptr(card, CCR_READADDRESS, voice->num, cra);
 
-		sblive_writeptr(card, CCR_READADDRESS, voice->num, cra);
+			if (voice->flags & VOICE_FLAGS_STEREO)
+				sblive_writeptr(card, CCR_READADDRESS, voice->num + 1, cra);
 
-		if (voice->flags & VOICE_FLAGS_STEREO)
-			sblive_writeptr(card, CCR_READADDRESS, voice->num + 1, cra);
+			/* Fill cache */
+			sblive_writeptr(card, CCR_CACHEINVALIDSIZE, voice->num, ccis);
+		}
 
-		/* Fill cache */
-		sblive_writeptr(card, CCR_CACHEINVALIDSIZE, voice->num, ccis);
+		for (j = 0; j < (voice->flags & VOICE_FLAGS_STEREO ? 2 : 1); j++) {
+			sblive_writeptr_tag(card, voice->num + j,
+				    IFATN, (voice->params[j].initial_fc << 8) | voice->params[j].initial_attn,
+				    VTFT, (voice->params[j].volume_target << 16) | voice->params[j].fc_target,
+				    CVCF, (voice->params[j].volume_target << 16) | voice->params[j].fc_target,
+				    DCYSUSV, (voice->params[j].byampl_env_sustain << 8) | voice->params[j].byampl_env_decay,
+				    TAGLIST_END);
+	
+			emu10k1_clear_stop_on_loop(card, voice->num + j);
+		}
 	}
 
-	for (i = 0; i < (voice->flags & VOICE_FLAGS_STEREO ? 2 : 1); i++) {
-		sblive_writeptr_tag(card, voice->num + i,
-				    IFATN, (voice->params[i].initial_fc << 8) | voice->params[i].initial_attn,
-				    VTFT, (voice->params[i].volume_target << 16) | voice->params[i].fc_target,
-				    CVCF, (voice->params[i].volume_target << 16) | voice->params[i].fc_target,
-				    DCYSUSV, (voice->params[i].byampl_env_sustain << 8) | voice->params[i].byampl_env_decay,
-				    TAGLIST_END);
 
-		emu10k1_clear_stop_on_loop(card, voice->num + i);
+        for (voicenum = 0; voicenum < num_voices; voicenum++)
+	{
+		voice = first_voice + voicenum;
 
-		sblive_writeptr(card, PTRX_PITCHTARGET, voice->num + i, voice->pitch_target);
+		for (j = 0; j < (voice->flags & VOICE_FLAGS_STEREO ? 2 : 1); j++) {
+			sblive_writeptr(card, PTRX_PITCHTARGET, voice->num + j, voice->pitch_target);
 
-		if (i == 0)
-			sblive_writeptr(card, CPF_CURRENTPITCH, voice->num, voice->pitch_target);
+			if (j == 0)
+				sblive_writeptr(card, CPF_CURRENTPITCH, voice->num, voice->pitch_target);
 
-		sblive_writeptr(card, IP, voice->num + i, voice->initial_pitch);
+			sblive_writeptr(card, IP, voice->num + j, voice->initial_pitch);
+		}
 	}
-
-	return;
 }
 
-void emu10k1_voice_stop(struct emu_voice *voice)
+void emu10k1_voices_stop(struct emu_voice *first_voice, int num_voices)
 {
-	struct emu10k1_card *card = voice->card;
-	int i;
+	struct emu10k1_card *card = first_voice->card;
+	struct emu_voice *voice;
+	unsigned int voice_num;
+	int j;
 
 	DPF(2, "emu10k1_voice_stop()\n");
 
-	for (i = 0; i < (voice->flags & VOICE_FLAGS_STEREO ? 2 : 1); i++) {
-		sblive_writeptr_tag(card, voice->num + i,
-					PTRX_PITCHTARGET, 0,
-					CPF_CURRENTPITCH, 0,
-					IFATN, 0xffff,
-					VTFT, 0x0000ffff,
-					CVCF, 0x0000ffff,
-					IP, 0,
-					TAGLIST_END);
+        for (voice_num = 0; voice_num < num_voices; voice_num++)
+	{
+		voice = first_voice + voice_num;
+
+		for (j = 0; j < (voice->flags & VOICE_FLAGS_STEREO ? 2 : 1); j++) {
+			sblive_writeptr_tag(card, voice->num + j,
+						PTRX_PITCHTARGET, 0,
+						CPF_CURRENTPITCH, 0,
+						IFATN, 0xffff,
+						VTFT, 0x0000ffff,
+						CVCF, 0x0000ffff,
+						IP, 0,
+						TAGLIST_END);
+		}
 	}
-
-	return;
 }
 
diff -urN linux-2.4.8-pre8/drivers/sound/emu10k1/voicemgr.h linux/drivers/sound/emu10k1/voicemgr.h
--- linux-2.4.8-pre8/drivers/sound/emu10k1/voicemgr.h	Mon Dec 11 13:02:19 2000
+++ linux/drivers/sound/emu10k1/voicemgr.h	Mon Dec  3 23:00:45 2001
@@ -85,7 +85,7 @@
 int emu10k1_voice_alloc(struct emu10k1_card *, struct emu_voice *);
 void emu10k1_voice_free(struct emu_voice *);
 void emu10k1_voice_playback_setup(struct emu_voice *);
-void emu10k1_voice_start(struct emu_voice *, int);
-void emu10k1_voice_stop(struct emu_voice *);
+void emu10k1_voices_start(struct emu_voice *, unsigned int, int);
+void emu10k1_voices_stop(struct emu_voice *, int);
 
 #endif /* _VOICEMGR_H */
diff -urN linux-2.4.8-pre8/fs/buffer.c linux/fs/buffer.c
--- linux-2.4.8-pre8/fs/buffer.c	Mon Dec  3 23:00:33 2001
+++ linux/fs/buffer.c	Mon Dec  3 23:00:45 2001
@@ -113,19 +113,17 @@
  */
 union bdflush_param {
 	struct {
-		int nfract;  /* Percentage of buffer cache dirty to 
-				activate bdflush */
-		int ndirty;  /* Maximum number of dirty blocks to write out per
-				wake-cycle */
-		int nrefill; /* Number of clean buffers to try to obtain
-				each time we call refill */
-		int dummy1;   /* unused */
-		int interval; /* jiffies delay between kupdate flushes */
-		int age_buffer;  /* Time for normal buffer to age before we flush it */
-		int nfract_sync; /* Percentage of buffer cache dirty to 
-				    activate bdflush synchronously */
-		int dummy2;    /* unused */
-		int dummy3;    /* unused */
+		int nfract;	/* Percentage of buffer cache dirty to 
+				   activate bdflush */
+		int dummy1;	/* old "ndirty" */
+		int dummy2;	/* old "nrefill" */
+		int dummy3;	/* unused */
+		int interval;	/* jiffies delay between kupdate flushes */
+		int age_buffer;	/* Time for normal buffer to age before we flush it */
+		int nfract_sync;/* Percentage of buffer cache dirty to 
+				   activate bdflush synchronously */
+		int dummy4;	/* unused */
+		int dummy5;	/* unused */
 	} b_un;
 	unsigned int data[N_PARAM];
 } bdf_prm = {{30, 64, 64, 256, 5*HZ, 30*HZ, 60, 0, 0}};
@@ -1067,7 +1065,6 @@
 	out:
 		write_unlock(&hash_table_lock);
 		spin_unlock(&lru_list_lock);
-		touch_buffer(bh);
 		return bh;
 	}
 
@@ -1234,6 +1231,7 @@
 	struct buffer_head * bh;
 
 	bh = getblk(dev, block, size);
+	touch_buffer(bh);
 	if (buffer_uptodate(bh))
 		return bh;
 	ll_rw_block(READ, 1, &bh);
@@ -2601,20 +2599,19 @@
 	sync_supers(0);
 	unlock_kernel();
 
-	spin_lock(&lru_list_lock);
 	for (;;) {
-		if (write_some_buffers(NODEV)) {
-			struct buffer_head *bh;
+		struct buffer_head *bh;
 
-			spin_lock(&lru_list_lock);
-			bh = lru_list[BUF_DIRTY];
-			if (bh && !time_before(jiffies, bh->b_flushtime))
-				continue;
-			spin_unlock(&lru_list_lock);
-		}
-		run_task_queue(&tq_disk);
+		spin_lock(&lru_list_lock);
+		bh = lru_list[BUF_DIRTY];
+		if (!bh || time_before(jiffies, bh->b_flushtime))
+			break;
+		if (write_some_buffers(NODEV))
+			continue;
 		return 0;
 	}
+	spin_unlock(&lru_list_lock);
+	return 0;
 }
 
 int block_sync_page(struct page *page)
diff -urN linux-2.4.8-pre8/include/linux/swap.h linux/include/linux/swap.h
--- linux-2.4.8-pre8/include/linux/swap.h	Mon Dec  3 23:00:33 2001
+++ linux/include/linux/swap.h	Mon Dec  3 23:00:45 2001
@@ -148,6 +148,7 @@
 extern void free_page_and_swap_cache(struct page *page);
 
 /* linux/mm/swapfile.c */
+extern int vm_swap_full(void);
 extern unsigned int nr_swapfiles;
 extern struct swap_info_struct swap_info[];
 extern int is_swap_partition(kdev_t);
diff -urN linux-2.4.8-pre8/mm/memory.c linux/mm/memory.c
--- linux-2.4.8-pre8/mm/memory.c	Mon Dec  3 23:00:33 2001
+++ linux/mm/memory.c	Mon Dec  3 23:00:45 2001
@@ -1098,9 +1098,10 @@
  */
 static int do_swap_page(struct mm_struct * mm,
 	struct vm_area_struct * vma, unsigned long address,
-	pte_t * page_table, swp_entry_t entry, int write_access)
+	pte_t * page_table, pte_t orig_pte, int write_access)
 {
 	struct page *page;
+	swp_entry_t entry = pte_to_swp_entry(orig_pte);
 	pte_t pte;
 
 	spin_unlock(&mm->page_table_lock);
@@ -1112,7 +1113,11 @@
 		unlock_kernel();
 		if (!page) {
 			spin_lock(&mm->page_table_lock);
-			return -1;
+			/*
+			 * Back out if somebody else faulted in this pte while
+			 * we released the page table lock.
+			 */
+			return pte_same(*page_table, orig_pte) ? -1 : 1;
 		}
 	}
 
@@ -1128,7 +1133,7 @@
 	 * released the page table lock.
 	 */
 	spin_lock(&mm->page_table_lock);
-	if (pte_present(*page_table)) {
+	if (!pte_same(*page_table, orig_pte)) {
 		UnlockPage(page);
 		page_cache_release(page);
 		return 1;
@@ -1139,8 +1144,14 @@
 	pte = mk_pte(page, vma->vm_page_prot);
 
 	swap_free(entry);
-	if (write_access && exclusive_swap_page(page))
-		pte = pte_mkwrite(pte_mkdirty(pte));
+	if (exclusive_swap_page(page)) {	
+		if (write_access)
+			pte = pte_mkwrite(pte_mkdirty(pte));
+		if (vm_swap_full()) {
+			delete_from_swap_cache_nolock(page);
+			pte = pte_mkdirty(pte);
+		}
+	}
 	UnlockPage(page);
 
 	flush_page_to_ram(page);
@@ -1297,7 +1308,7 @@
 		 */
 		if (pte_none(entry))
 			return do_no_page(mm, vma, address, write_access, pte);
-		return do_swap_page(mm, vma, address, pte, pte_to_swp_entry(entry), write_access);
+		return do_swap_page(mm, vma, address, pte, entry, write_access);
 	}
 
 	if (write_access) {
diff -urN linux-2.4.8-pre8/mm/page_alloc.c linux/mm/page_alloc.c
--- linux-2.4.8-pre8/mm/page_alloc.c	Mon Dec  3 23:00:33 2001
+++ linux/mm/page_alloc.c	Mon Dec  3 23:00:45 2001
@@ -589,7 +589,7 @@
 	zonelist_t *zonelist;
 	zone_t **zonep, *zone;
 
-	zonelist = contig_page_data.node_zonelists + (GFP_KERNEL & GFP_ZONEMASK);
+	zonelist = contig_page_data.node_zonelists + (GFP_NOFS & GFP_ZONEMASK);
 	zonep = zonelist->zones;
 
 	for (zone = *zonep++; zone; zone = *zonep++) {
diff -urN linux-2.4.8-pre8/mm/swapfile.c linux/mm/swapfile.c
--- linux-2.4.8-pre8/mm/swapfile.c	Wed Jul  4 11:50:38 2001
+++ linux/mm/swapfile.c	Mon Dec  3 23:00:45 2001
@@ -19,11 +19,31 @@
 
 spinlock_t swaplock = SPIN_LOCK_UNLOCKED;
 unsigned int nr_swapfiles;
+int total_swap_pages;
 
 struct swap_list_t swap_list = {-1, -1};
 
 struct swap_info_struct swap_info[MAX_SWAPFILES];
 
+/*
+ * When swap space gets filled up, we will set this flag.
+ * This will make do_swap_page(), in the page fault path,
+ * free swap entries on swapin so we'll reclaim swap space
+ * in order to be able to swap something out.
+ *
+ * At the moment we start reclaiming when swap usage goes
+ * over 80% of swap space.
+ *
+ * XXX: Random numbers, fixme.
+ */
+#define SWAP_FULL_PCT 80
+int vm_swap_full (void)
+{
+	int swap_used = total_swap_pages - nr_swap_pages;
+
+	return swap_used * 100 > total_swap_pages * SWAP_FULL_PCT;
+}
+
 #define SWAPFILE_CLUSTER 256
 
 static inline int scan_swap_map(struct swap_info_struct *si, unsigned short count)
@@ -329,6 +349,54 @@
 }
 
 /*
+ * this is called when we find a page in the swap list
+ * all the locks have been dropped at this point which
+ * isn't a problem because we rescan the swap map
+ * and we _don't_ clear the refrence count if for 
+ * some reason it isn't 0
+ */
+   
+static inline int free_found_swap_entry(unsigned int type, int i)
+{
+	struct task_struct *p;
+	struct page *page;
+	swp_entry_t entry;
+
+	entry = SWP_ENTRY(type, i);
+
+	/* 
+	 * Get a page for the entry, using the existing swap
+	 * cache page if there is one.  Otherwise, get a clean
+	 * page and read the swap into it. 
+	 */
+	page = read_swap_cache_async(entry);
+	if (!page) {
+		swap_free(entry);
+		return -ENOMEM;
+	}
+	lock_page(page);
+	if (PageSwapCache(page))
+		delete_from_swap_cache_nolock(page);
+	UnlockPage(page);
+	read_lock(&tasklist_lock);
+	for_each_task(p)
+		unuse_process(p->mm, entry, page);
+	read_unlock(&tasklist_lock);
+	shmem_unuse(entry, page);
+	/* 
+	 * Now get rid of the extra reference to the temporary
+	 * page we've been using. 
+	 */
+	page_cache_release(page);
+	/*
+	 * Check for and clear any overflowed swap map counts.
+	 */
+	swap_free(entry);
+	return 0;
+}
+
+
+/*
  * We completely avoid races by reading each swap page in advance,
  * and then search for the process using it.  All the necessary
  * page table adjustments can then be made atomically.
@@ -336,82 +404,79 @@
 static int try_to_unuse(unsigned int type)
 {
 	struct swap_info_struct * si = &swap_info[type];
-	struct task_struct *p;
-	struct page *page;
-	swp_entry_t entry;
-	int i;
+	int ret, foundpage;
+
+	do {
+		int i;
 
-	while (1) {
 		/*
 		 * The algorithm is inefficient but seldomly used
-		 * and probably not worth fixing.
 		 *
-		 * Make sure that we aren't completely killing
-		 * interactive performance.
-		 */
-		if (current->need_resched)
-			schedule();
-			
-		/*
 		 * Find a swap page in use and read it in.
 		 */
+		foundpage = 0;
 		swap_device_lock(si);
 		for (i = 1; i < si->max ; i++) {
-			if (si->swap_map[i] > 0 && si->swap_map[i] != SWAP_MAP_BAD) {
-				/*
-				 * Prevent swaphandle from being completely
-				 * unused by swap_free while we are trying
-				 * to read in the page - this prevents warning
-				 * messages from rw_swap_page_base.
+			int count = si->swap_map[i];
+			if (!count || count == SWAP_MAP_BAD)
+				continue;
+
+			/*
+			 * Prevent swaphandle from being completely
+			 * unused by swap_free while we are trying
+			 * to read in the page - this prevents warning
+			 * messages from rw_swap_page_base.
+			 */
+			foundpage = 1;
+			if (count != SWAP_MAP_MAX)
+				si->swap_map[i] = count + 1;
+
+			swap_device_unlock(si);
+			ret = free_found_swap_entry(type,i);
+			if (ret)
+				return ret;
+
+			/*
+			 * we pick up the swap_list_lock() to guard the nr_swap_pages,
+			 * si->swap_map[] should only be changed if it is SWAP_MAP_MAX
+			 * otherwise ugly stuff can happen with other people who are in
+			 * the middle of a swap operation to this device. This kind of
+			 * operation can sometimes be detected with the undead swap 
+			 * check. Don't worry about these 'undead' entries for now
+			 * they will be caught the next time though the top loop.
+			 * Do worry, about the weak locking that allows this to happen
+			 * because if it happens to a page that is SWAP_MAP_MAX
+			 * then bad stuff can happen.
+			 */
+			swap_list_lock();
+			swap_device_lock(si);
+			if (si->swap_map[i] > 0) {
+				/* normally this would just kill the swap page if
+				 * it still existed, it appears though that the locks 
+				 * are a little fuzzy
 				 */
-				if (si->swap_map[i] != SWAP_MAP_MAX)
-					si->swap_map[i]++;
-				swap_device_unlock(si);
-				goto found_entry;
+				if (si->swap_map[i] != SWAP_MAP_MAX) {
+					printk("VM: Undead swap entry %08lx\n", 
+					       SWP_ENTRY(type, i).val);
+				} else {
+					nr_swap_pages++;
+					si->swap_map[i] = 0;
+				}
 			}
-		}
-		swap_device_unlock(si);
-		break;
+			swap_device_unlock(si);
+			swap_list_unlock();
 
-	found_entry:
-		entry = SWP_ENTRY(type, i);
-
-		/* Get a page for the entry, using the existing swap
-                   cache page if there is one.  Otherwise, get a clean
-                   page and read the swap into it. */
-		page = read_swap_cache_async(entry);
-		if (!page) {
-			swap_free(entry);
-  			return -ENOMEM;
-		}
-		lock_page(page);
-		if (PageSwapCache(page))
-			delete_from_swap_cache_nolock(page);
-		UnlockPage(page);
-		read_lock(&tasklist_lock);
-		for_each_task(p)
-			unuse_process(p->mm, entry, page);
-		read_unlock(&tasklist_lock);
-		shmem_unuse(entry, page);
-		/* Now get rid of the extra reference to the temporary
-                   page we've been using. */
-		page_cache_release(page);
-		/*
-		 * Check for and clear any overflowed swap map counts.
-		 */
-		swap_free(entry);
-		swap_list_lock();
-		swap_device_lock(si);
-		if (si->swap_map[i] > 0) {
-			if (si->swap_map[i] != SWAP_MAP_MAX)
-				printk("VM: Undead swap entry %08lx\n", 
-								entry.val);
-			nr_swap_pages++;
-			si->swap_map[i] = 0;
+			/*
+			 * This lock stuff is ulgy!
+			 * Make sure that we aren't completely killing
+			 * interactive performance.
+			 */
+			if (current->need_resched)
+				schedule();
+			swap_device_lock(si); 
 		}
 		swap_device_unlock(si);
-		swap_list_unlock();
-	}
+	} while (foundpage);
 	return 0;
 }
 
@@ -462,6 +527,7 @@
 		swap_list.next = swap_list.head;
 	}
 	nr_swap_pages -= p->pages;
+	total_swap_pages -= p->pages;
 	swap_list_unlock();
 	p->flags = SWP_USED;
 	err = try_to_unuse(type);
@@ -477,6 +543,7 @@
 		else
 			swap_info[prev].next = p - swap_info;
 		nr_swap_pages += p->pages;
+		total_swap_pages += p->pages;
 		swap_list_unlock();
 		p->flags = SWP_WRITEOK;
 		goto out_dput;
@@ -764,6 +831,7 @@
 	p->pages = nr_good_pages;
 	swap_list_lock();
 	nr_swap_pages += nr_good_pages;
+	total_swap_pages += nr_good_pages;
 	printk(KERN_INFO "Adding Swap: %dk swap-space (priority %d)\n",
 	       nr_good_pages<<(PAGE_SHIFT-10), p->prio);
 
