## Automatically generated incremental diff
## From:   linux-2.2.13-pre3
## To:     linux-2.2.13-pre4
## Robot:  $Id: make-incremental-diff,v 1.11 2002/02/20 02:59:33 hpa Exp $

diff -urN linux-2.2.13-pre3/Documentation/Configure.help linux-2.2.13-pre4/Documentation/Configure.help
--- linux-2.2.13-pre3/Documentation/Configure.help	2003-08-15 15:07:25.000000000 -0700
+++ linux-2.2.13-pre4/Documentation/Configure.help	2003-08-15 15:07:26.000000000 -0700
@@ -3843,6 +3843,15 @@
   kernel. The default value has been reduced to 5 seconds. If this
   doesn't work with your hardware, try increasing this value.
 
+IBM ServeRAID Support
+CONFIG_SCSI_IPS
+  This is support for the IBM ServeRAID hardware RAID controllers.
+  Consult the SCSI-HOWTO, available via anonymous FTP from
+  ftp://metalab.unc.edu/pub/Linux/docs/HOWTO, and the file
+  README.ips in drivers/scsi for more information.  If this driver
+  does not work correctly without modification please contact the
+  author by email at ipslinux@us.ibm.com.
+
 BusLogic SCSI support
 CONFIG_SCSI_BUSLOGIC
   This is support for BusLogic MultiMaster and FlashPoint SCSI Host
@@ -9540,6 +9549,11 @@
   See also Documentation/sound/Opti and Documentation/sound/MAD16 for
   more information on setting these cards up as modules.
 
+VIA 82Cxxx audio support
+CONFIG_SOUND_VIA82CXXX
+  Answer Y if you have a VIA82C686 chip, typically found built
+  onto a motherboard.
+
 Full support for Turtle Beach WaveFront synth/sound cards
 CONFIG_SOUND_WAVEFRONT
   Answer Y or M if you have a Tropez Plus, Tropez or Maui sound card
@@ -11148,6 +11162,25 @@
   Enter either 0x20c, 0x30c, 0x24c or 0x34c here. The card default is
   0x34c, if you haven't changed the jumper setting on the card.
 
+Trust FM Radio Card
+CONFIG_RADIO_TRUST
+  Choose Y here if you have this FM radio card, and then fill in the 
+  port address below.
+
+  In order to control your radio card, you will need to use programs
+  that are compatible with the Video for Linux API. Information on
+  this API and pointers to "v4l" programs may be found at
+  http://roadrunner.swansea.uk.linux.org/v4l.shtml.
+
+  If you want to compile this driver as a module ( = code which can be
+  inserted in and removed from the running kernel whenever you want),
+  say M here and read Documentation/modules.txt. The module will be
+  called radio-trust.o.
+
+Trust FM Radio I/O port
+CONFIG_RADIO_TRUST_PORT
+  Enter the I/O port of your card (usually 0x350 or 0x358).
+
 BT848 Video For Linux
 CONFIG_VIDEO_BT848
   Support for BT848 based frame grabber/overlay boards. This includes
diff -urN linux-2.2.13-pre3/Documentation/sound/CMI8338 linux-2.2.13-pre4/Documentation/sound/CMI8338
--- linux-2.2.13-pre3/Documentation/sound/CMI8338	1969-12-31 16:00:00.000000000 -0800
+++ linux-2.2.13-pre4/Documentation/sound/CMI8338	2003-08-15 15:07:26.000000000 -0700
@@ -0,0 +1,24 @@
+Audio driver for CM8338/CM8738 chips by Chen-Li Tien
+
+Steps to build cmpci driver
+
+1. Backup the Config.in and Makefile in the sound driver directory
+	(/usr/src/linux/driver/sound).
+
+2. Extract the tar file by tar xvzf source.tar.gz in the above directory.
+
+3. Change directory to /usr/src/linux
+
+4. Config cm8338 driver by 'make menuconfig' or 'make config' command.
+
+5. Please select Sound Card (CONFIG_SOUND=m) support and CMPCI driver (CONFIG_SOUND_CMPCI=m) as modules. Resident mode not tested.
+
+6. Compile the kernel if necessary.
+
+7. Compile the modules by 'make modules'.
+
+8. Install the modules by 'make modules_install'
+
+9. Before first time to run the driver, create module dependency by 'depmod -a'
+
+10. To install the driver, enter 'modprobe cmpci'.
diff -urN linux-2.2.13-pre3/drivers/char/buz.c linux-2.2.13-pre4/drivers/char/buz.c
--- linux-2.2.13-pre3/drivers/char/buz.c	1999-08-25 17:29:47.000000000 -0700
+++ linux-2.2.13-pre4/drivers/char/buz.c	2003-08-15 15:07:26.000000000 -0700
@@ -2901,7 +2901,6 @@
 		{
 			struct zoran_status bs;
 			int norm, input, status;
-			unsigned long timeout;
 
 			if (zr->codec_mode != BUZ_MODE_IDLE) {
 				return -EINVAL;
@@ -3317,7 +3316,6 @@
 	unsigned char latency;
 	struct zoran *zr;
 	struct pci_dev *dev = NULL;
-	int result;
 
 	zoran_num = 0;
 
diff -urN linux-2.2.13-pre3/drivers/char/Config.in linux-2.2.13-pre4/drivers/char/Config.in
--- linux-2.2.13-pre3/drivers/char/Config.in	1999-08-25 17:29:46.000000000 -0700
+++ linux-2.2.13-pre4/drivers/char/Config.in	2003-08-15 15:07:26.000000000 -0700
@@ -134,6 +134,10 @@
   if [ "$CONFIG_RADIO_GEMTEK" = "y" ]; then
     hex '  GemTek i/o port (0x20c, 0x30c, 0x24c or 0x34c)' CONFIG_RADIO_GEMTEK_PORT 34c
   fi
+  dep_tristate 'Trust FM Radio' CONFIG_RADIO_TRUST $CONFIG_VIDEO_DEV
+  if [ "$CONFIG_RADIO_TRUST" = "y" ]; then
+    hex '  Trust FM Radio I/O port (0x350 or 0x358)' CONFIG_RADIO_TRUST_PORT 350
+  fi
   if [ "$CONFIG_PCI" != "n" ]; then
     dep_tristate 'BT848 Video For Linux' CONFIG_VIDEO_BT848 $CONFIG_VIDEO_DEV
   fi
diff -urN linux-2.2.13-pre3/drivers/char/Makefile linux-2.2.13-pre4/drivers/char/Makefile
--- linux-2.2.13-pre3/drivers/char/Makefile	1999-08-25 17:29:46.000000000 -0700
+++ linux-2.2.13-pre4/drivers/char/Makefile	2003-08-15 15:07:26.000000000 -0700
@@ -498,6 +498,14 @@
   endif
 endif                                             
 
+ifeq ($(CONFIG_RADIO_TRUST),y)
+L_OBJS += radio-trust.o
+else
+  ifeq ($(CONFIG_RADIO_TRUST),m)
+  M_OBJS += radio-trust.o
+  endif
+endif                                             
+
 ifeq ($(CONFIG_QIC02_TAPE),y)
 L_OBJS += tpqic02.o
 else
diff -urN linux-2.2.13-pre3/drivers/char/radio-trust.c linux-2.2.13-pre4/drivers/char/radio-trust.c
--- linux-2.2.13-pre3/drivers/char/radio-trust.c	1969-12-31 16:00:00.000000000 -0800
+++ linux-2.2.13-pre4/drivers/char/radio-trust.c	2003-08-15 15:07:26.000000000 -0700
@@ -0,0 +1,356 @@
+/* radio-trust.c - Trust FM Radio card driver for Linux 2.2 
+ * by Eric Lammerts <eric@scintilla.utwente.nl>
+ *
+ * Based on radio-aztech.c. Original notes:
+ *
+ * Adapted to support the Video for Linux API by 
+ * Russell Kroll <rkroll@exploits.org>.  Based on original tuner code by:
+ *
+ * Quay Ly
+ * Donald Song
+ * Jason Lewis      (jlewis@twilight.vtc.vsc.edu) 
+ * Scott McGrath    (smcgrath@twilight.vtc.vsc.edu)
+ * William McGrath  (wmcgrath@twilight.vtc.vsc.edu)
+ *
+ * The basis for this code may be found at http://bigbang.vtc.vsc.edu/fmradio/
+ */
+
+#include <stdarg.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/ioport.h>
+#include <asm/io.h>
+#include <asm/uaccess.h>
+#include <linux/videodev.h>
+#include <linux/config.h>	/* CONFIG_RADIO_TRUST_PORT 	*/
+
+/* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
+
+#ifndef CONFIG_RADIO_TRUST_PORT
+#define CONFIG_RADIO_TRUST_PORT -1
+#endif
+
+static int io = CONFIG_RADIO_TRUST_PORT; 
+static int ioval = 0xf;
+static int users = 0;
+static __u16 curvol;
+static __u16 curbass;
+static __u16 curtreble;
+static unsigned long curfreq;
+static int curstereo;
+static int curmute;
+
+/* i2c addresses */
+#define TDA7318_ADDR 0x88
+#define TSA6060T_ADDR 0xc4
+
+#define TR_DELAY do { inb(io); inb(io); inb(io); } while(0)
+#define TR_SET_SCL outb(ioval |= 2, io)
+#define TR_CLR_SCL outb(ioval &= 0xfd, io)
+#define TR_SET_SDA outb(ioval |= 1, io)
+#define TR_CLR_SDA outb(ioval &= 0xfe, io)
+
+static void write_i2c(int n, ...)
+{
+	unsigned char val, mask;
+	va_list args;
+
+	va_start(args, n);
+
+	/* start condition */
+	TR_SET_SDA;
+	TR_SET_SCL;
+	TR_DELAY;
+	TR_CLR_SDA;
+	TR_CLR_SCL;
+	TR_DELAY;
+
+	for(; n; n--) {
+		val = va_arg(args, unsigned);
+		for(mask = 0x80; mask; mask >>= 1) {
+			if(val & mask)
+				TR_SET_SDA;
+			else
+				TR_CLR_SDA;
+			TR_SET_SCL;
+			TR_DELAY;
+			TR_CLR_SCL;
+			TR_DELAY;
+		}
+		/* acknowledge bit */
+		TR_SET_SDA;
+		TR_SET_SCL;
+		TR_DELAY;
+		TR_CLR_SCL;
+		TR_DELAY;
+	}
+
+	/* stop condition */
+	TR_CLR_SDA;
+	TR_DELAY;
+	TR_SET_SCL;
+	TR_DELAY;
+	TR_SET_SDA;
+	TR_DELAY;
+
+	va_end(args);
+}
+
+static void tr_setvol(__u16 vol)
+{
+	curvol = vol / 2048;
+	write_i2c(2, TDA7318_ADDR, curvol ^ 0x1f);
+}
+
+static int basstreble2chip[15] = {
+	0, 1, 2, 3, 4, 5, 6, 7, 14, 13, 12, 11, 10, 9, 8
+};
+
+static void tr_setbass(__u16 bass)
+{
+	curbass = bass / 4370;
+	write_i2c(2, TDA7318_ADDR, 0x60 | basstreble2chip[curbass]);
+}
+
+static void tr_settreble(__u16 treble)
+{
+	curtreble = treble / 4370;
+	write_i2c(2, TDA7318_ADDR, 0x70 | basstreble2chip[curtreble]);
+}
+
+static void tr_setstereo(int stereo)
+{
+	curstereo = !!stereo;
+	ioval = (ioval & 0xfb) | (!curstereo << 2);
+	outb(ioval, io);
+}
+
+static void tr_setmute(int mute)
+{
+	curmute = !!mute;
+	ioval = (ioval & 0xf7) | (curmute << 3);
+	outb(ioval, io);
+}
+
+static int tr_getsigstr(void)
+{
+	int i, v;
+	
+	for(i = 0, v = 0; i < 100; i++) v |= inb(io);
+	return (v & 1)? 0 : 0xffff;
+}
+
+static int tr_getstereo(void)
+{
+	/* don't know how to determine it, just return the setting */
+	return curstereo;
+}
+
+static void tr_setfreq(unsigned long f)
+{
+	f /= 160;	/* Convert to 10 kHz units	*/
+	f += 1070;	/* Add 10.7 MHz IF			*/
+
+	write_i2c(5, TSA6060T_ADDR, (f << 1) | 1, f >> 7, 0x60 | ((f >> 15) & 1), 0);
+}
+
+static int tr_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
+{
+	switch(cmd)
+	{
+		case VIDIOCGCAP:
+		{
+			struct video_capability v;
+
+			v.type=VID_TYPE_TUNER;
+			v.channels=1;
+			v.audios=1;
+
+			/* No we don't do pictures */
+			v.maxwidth=0;
+			v.maxheight=0;
+			v.minwidth=0;
+			v.minheight=0;
+
+			strcpy(v.name, "Trust FM Radio");
+
+			if(copy_to_user(arg,&v,sizeof(v)))
+				return -EFAULT;
+
+			return 0;
+		}
+		case VIDIOCGTUNER:
+		{
+			struct video_tuner v;
+
+			if(copy_from_user(&v, arg,sizeof(v))!=0) 
+				return -EFAULT;
+
+			if(v.tuner)	/* Only 1 tuner */ 
+				return -EINVAL;
+
+			v.rangelow = 87500 * 16;
+			v.rangehigh = 108000 * 16;
+			v.flags = VIDEO_TUNER_LOW;
+			v.mode = VIDEO_MODE_AUTO;
+
+			v.signal = tr_getsigstr();
+			if(tr_getstereo())
+				v.flags |= VIDEO_TUNER_STEREO_ON;
+
+			strcpy(v.name, "FM");
+
+			if(copy_to_user(arg,&v, sizeof(v)))
+				return -EFAULT;
+
+			return 0;
+		}
+		case VIDIOCSTUNER:
+		{
+			struct video_tuner v;
+
+			if(copy_from_user(&v, arg, sizeof(v)))
+				return -EFAULT;
+			if(v.tuner != 0)
+				return -EINVAL;
+
+			return 0;
+		}
+		case VIDIOCGFREQ:
+			if(copy_to_user(arg, &curfreq, sizeof(curfreq)))
+				return -EFAULT;
+			return 0;
+
+		case VIDIOCSFREQ:
+		{
+			unsigned long f;
+
+			if(copy_from_user(&f, arg, sizeof(curfreq)))
+				return -EFAULT;
+			tr_setfreq(f);
+			return 0;
+		}
+		case VIDIOCGAUDIO:
+		{	
+			struct video_audio v;
+
+			memset(&v,0, sizeof(v));
+			v.flags = VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME |
+			          VIDEO_AUDIO_BASS | VIDEO_AUDIO_TREBLE;
+			v.mode = curstereo? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
+			v.volume = curvol * 2048;
+			v.step = 2048;
+			v.bass = curbass * 4370;
+			v.treble = curtreble * 4370;
+			
+			strcpy(v.name, "Trust FM Radio");
+			if(copy_to_user(arg,&v, sizeof(v)))
+				return -EFAULT;
+			return 0;			
+		}
+		case VIDIOCSAUDIO:
+		{
+			struct video_audio v;
+
+			if(copy_from_user(&v, arg, sizeof(v))) 
+				return -EFAULT;	
+			if(v.audio) 
+				return -EINVAL;
+
+			tr_setvol(v.volume);					
+			tr_setbass(v.bass);
+			tr_settreble(v.treble);
+			tr_setstereo(v.mode & VIDEO_SOUND_STEREO);
+			tr_setmute(v.flags & VIDEO_AUDIO_MUTE);
+			return 0;
+		}
+		default:
+			return -ENOIOCTLCMD;
+	}
+}
+
+static int tr_open(struct video_device *dev, int flags)
+{
+	if(users)
+		return -EBUSY;
+	users++;
+	MOD_INC_USE_COUNT;
+	return 0;
+}
+
+static void tr_close(struct video_device *dev)
+{
+	users--;
+	MOD_DEC_USE_COUNT;
+}
+
+static struct video_device trust_radio=
+{
+	"Trust FM Radio",
+	VID_TYPE_TUNER,
+	VID_HARDWARE_TRUST,
+	tr_open,
+	tr_close,
+	NULL,	/* Can't read  (no capture ability) */
+	NULL,	/* Can't write */
+	NULL,	/* No poll */
+	tr_ioctl,
+	NULL,
+	NULL
+};
+
+__initfunc(int trust_init(struct video_init *v))
+{
+	if(check_region(io, 2)) {
+		printk(KERN_ERR "trust: port 0x%x already in use\n", io);
+		return -EBUSY;
+	}
+	if(video_register_device(&trust_radio, VFL_TYPE_RADIO)==-1)
+		return -EINVAL;
+		
+	request_region(io, 2, "Trust FM Radio");
+
+	printk(KERN_INFO "Trust FM Radio card driver v1.0.\n");
+
+	write_i2c(2, TDA7318_ADDR, 0x80);	/* speaker att. LF = 0 dB */
+	write_i2c(2, TDA7318_ADDR, 0xa0);	/* speaker att. RF = 0 dB */
+	write_i2c(2, TDA7318_ADDR, 0xc0);	/* speaker att. LR = 0 dB */
+	write_i2c(2, TDA7318_ADDR, 0xe0);	/* speaker att. RR = 0 dB */
+	write_i2c(2, TDA7318_ADDR, 0x40);	/* stereo 1 input, gain = 18.75 dB */
+
+	tr_setvol(0x8000);					
+	tr_setbass(0x8000);
+	tr_settreble(0x8000);
+	tr_setstereo(1);
+
+	/* mute card - prevents noisy bootups */
+	tr_setmute(1);
+
+	return 0;
+}
+
+#ifdef MODULE
+
+MODULE_AUTHOR("Eric Lammerts, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
+MODULE_DESCRIPTION("A driver for the Trust FM Radio card.");
+MODULE_PARM(io, "i");
+MODULE_PARM_DESC(io, "I/O address of the Trust FM Radio card (0x350 or 0x358)");
+
+EXPORT_NO_SYMBOLS;
+
+int init_module(void)
+{
+	if(io == -1) {
+		printk(KERN_ERR "You must set an I/O address with io=0x???\n");
+		return -EINVAL;
+	}
+	return trust_init(NULL);
+}
+
+void cleanup_module(void)
+{
+	video_unregister_device(&trust_radio);
+	release_region(io, 2);
+}
+
+#endif
diff -urN linux-2.2.13-pre3/drivers/char/tty_io.c linux-2.2.13-pre4/drivers/char/tty_io.c
--- linux-2.2.13-pre3/drivers/char/tty_io.c	2003-08-15 15:07:25.000000000 -0700
+++ linux-2.2.13-pre4/drivers/char/tty_io.c	2003-08-15 15:07:26.000000000 -0700
@@ -1492,10 +1492,11 @@
 	return 0;
 }
 
-static int tioccons(struct tty_struct *tty, struct tty_struct *real_tty)
+static int tioccons(struct inode *inode,
+	struct tty_struct *tty, struct tty_struct *real_tty)
 {
-	if (tty->driver.type == TTY_DRIVER_TYPE_CONSOLE ||
-	    tty->driver.type == TTY_DRIVER_TYPE_SYSCONS) {
+	if (inode->i_rdev == SYSCONS_DEV ||
+	    inode->i_rdev == CONSOLE_DEV) {
 		if (!suser())
 			return -EPERM;
 		redirect = NULL;
@@ -1705,7 +1706,7 @@
 		case TIOCSWINSZ:
 			return tiocswinsz(tty, real_tty, (struct winsize *) arg);
 		case TIOCCONS:
-			return tioccons(tty, real_tty);
+			return tioccons(inode, tty, real_tty);
 		case FIONBIO:
 			return fionbio(file, (int *) arg);
 		case TIOCEXCL:
diff -urN linux-2.2.13-pre3/drivers/char/videodev.c linux-2.2.13-pre4/drivers/char/videodev.c
--- linux-2.2.13-pre3/drivers/char/videodev.c	1999-08-09 12:04:39.000000000 -0700
+++ linux-2.2.13-pre4/drivers/char/videodev.c	2003-08-15 15:07:26.000000000 -0700
@@ -81,6 +81,9 @@
 #ifdef CONFIG_RADIO_CADET
 extern int cadet_init(struct video_init *);
 #endif
+#ifdef CONFIG_RADIO_TRUST
+extern int trust_init(struct video_init *);
+#endif
 #ifdef CONFIG_VIDEO_PMS
 extern int init_pms_cards(struct video_init *);
 #endif
@@ -135,6 +138,9 @@
 #ifdef CONFIG_VIDEO_ZORAN
 	{"zoran", init_zoran_cards},
 #endif	
+#ifdef CONFIG_RADIO_TRUST
+	{"Trust", trust_init}, 
+#endif	
 	{"end", NULL}
 };
 
diff -urN linux-2.2.13-pre3/drivers/net/atp.c linux-2.2.13-pre4/drivers/net/atp.c
--- linux-2.2.13-pre3/drivers/net/atp.c	1998-11-20 08:44:06.000000000 -0800
+++ linux-2.2.13-pre4/drivers/net/atp.c	2003-08-15 15:07:26.000000000 -0700
@@ -184,7 +184,7 @@
 	/* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */
 	outb(0x04, ioaddr + PAR_CONTROL);
 	write_reg_high(ioaddr, CMR1, CMR1h_RESET);
-	eeprom_delay(2048);
+	udelay(100);
 	status = read_nibble(ioaddr, CMR1);
 
 	if ((status & 0x78) != 0x08) {
@@ -299,12 +299,12 @@
 	while (--num_bits >= 0) {
 		char outval = test_bit(num_bits, &cmd) ? EE_DATA_WRITE : 0;
 		write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
-		eeprom_delay(5);
+		udelay(5);
 		write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
 		eedata_out <<= 1;
 		if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
 			eedata_out++;
-		eeprom_delay(5);
+		udelay(5);
 	}
 	write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
 	return eedata_out;
diff -urN linux-2.2.13-pre3/drivers/net/atp.h linux-2.2.13-pre4/drivers/net/atp.h
--- linux-2.2.13-pre3/drivers/net/atp.h	1997-02-02 05:18:36.000000000 -0800
+++ linux-2.2.13-pre4/drivers/net/atp.h	2003-08-15 15:07:26.000000000 -0700
@@ -261,10 +261,6 @@
 #define EE_DATA_WRITE	0x01	/* EEPROM chip data in. */
 #define EE_DATA_READ	0x08	/* EEPROM chip data out. */
 
-/* Delay between EEPROM clock transitions. */
-#define eeprom_delay(ticks) \
-do { int _i = 40; while (--_i > 0) { __SLOW_DOWN_IO; }} while (0)
-
 /* The EEPROM commands include the alway-set leading bit. */
 #define EE_WRITE_CMD(offset)	(((5 << 6) + (offset)) << 17)
 #define EE_READ(offset) 	(((6 << 6) + (offset)) << 17)
diff -urN linux-2.2.13-pre3/drivers/net/sis900.c linux-2.2.13-pre4/drivers/net/sis900.c
--- linux-2.2.13-pre3/drivers/net/sis900.c	2003-08-15 15:07:25.000000000 -0700
+++ linux-2.2.13-pre4/drivers/net/sis900.c	2003-08-15 15:07:26.000000000 -0700
@@ -969,6 +969,7 @@
         outl(TxATP | (TX_DMA_BURST << 20) | (TX_FIFO_THRESH<<8) | TxDRNT_10,
                                                 ioaddr + txcfg);
         if (sis900_debug > 1)
+        {
         	if (tp->LinkOn) {
         		printk(KERN_INFO"%s: Media Type %s%s-duplex.\n",
                                 dev->name,
@@ -976,8 +977,11 @@
 					"100mbps " : "10mbps ",
                                 tp->full_duplex== FDX_CAPABLE_FULL_SELECTED ?
 					"full" : "half");
-		} else {
+		}
+		else
+		{
 			printk(KERN_INFO"%s: Media Link Off\n", dev->name);
+		}
 	}
         set_rx_mode(dev);
 
diff -urN linux-2.2.13-pre3/drivers/scsi/ChangeLog.ips linux-2.2.13-pre4/drivers/scsi/ChangeLog.ips
--- linux-2.2.13-pre3/drivers/scsi/ChangeLog.ips	1969-12-31 16:00:00.000000000 -0800
+++ linux-2.2.13-pre4/drivers/scsi/ChangeLog.ips	2003-08-15 15:07:26.000000000 -0700
@@ -0,0 +1,25 @@
+Change Log
+~~~~~~~~~~
+
+	1.00.00 - Initial Public Release
+		- Functionally equivalent to 0.99.05
+
+        0.99.05 - Fix an oops on certain passthru commands
+
+	0.99.04 - Fix race condition in the passthru mechanism
+                  -- this required the interface to the utilities to change
+		- Fix error recovery code
+
+	0.99.03 - Make interrupt routine handle all completed request on the
+		  adapter not just the first one
+		- Make sure passthru commands get woken up if we run out of
+		  SCBs
+		- Send all of the commands on the queue at once rather than
+		  one at a time since the card will support it.
+
+	0.99.02 - Added some additional debug statements to print out
+                  errors if an error occurs while trying to read/write
+                  to a logical drive (IPS_DEBUG).
+
+                  Fixed read/write errors when the adapter is using an
+                  8K stripe size.
diff -urN linux-2.2.13-pre3/drivers/scsi/Config.in linux-2.2.13-pre4/drivers/scsi/Config.in
--- linux-2.2.13-pre3/drivers/scsi/Config.in	1999-08-25 17:29:48.000000000 -0700
+++ linux-2.2.13-pre4/drivers/scsi/Config.in	2003-08-15 15:07:26.000000000 -0700
@@ -30,6 +30,7 @@
     bool '   Collect statistics to report in /proc' CONFIG_AIC7XXX_PROC_STATS N
     int  '   Delay in seconds after SCSI bus reset' CONFIG_AIC7XXX_RESET_DELAY 5
 fi
+dep_tristate 'IBM ServeRAID support' CONFIG_SCSI_IPS $CONFIG_SCSI
 dep_tristate 'AdvanSys SCSI support' CONFIG_SCSI_ADVANSYS $CONFIG_SCSI
 dep_tristate 'Always IN2000 SCSI support' CONFIG_SCSI_IN2000 $CONFIG_SCSI
 dep_tristate 'AM53/79C974 PCI SCSI support' CONFIG_SCSI_AM53C974 $CONFIG_SCSI
diff -urN linux-2.2.13-pre3/drivers/scsi/hosts.c linux-2.2.13-pre4/drivers/scsi/hosts.c
--- linux-2.2.13-pre3/drivers/scsi/hosts.c	1999-08-25 17:29:48.000000000 -0700
+++ linux-2.2.13-pre4/drivers/scsi/hosts.c	2003-08-15 15:07:26.000000000 -0700
@@ -131,6 +131,10 @@
 #include "aic7xxx.h"
 #endif
 
+#ifdef CONFIG_SCSI_IPS
+#include "ips.h"
+#endif
+
 #ifdef CONFIG_SCSI_BUSLOGIC
 #include "BusLogic.h"
 #endif
@@ -468,6 +472,9 @@
 #ifdef CONFIG_SCSI_AIC7XXX
     AIC7XXX,
 #endif
+#ifdef CONFIG_SCSI_IPS
+    IPS,
+#endif
 #ifdef CONFIG_SCSI_FD_MCS
    FD_MCS,
 #endif
diff -urN linux-2.2.13-pre3/drivers/scsi/ips.c linux-2.2.13-pre4/drivers/scsi/ips.c
--- linux-2.2.13-pre3/drivers/scsi/ips.c	1969-12-31 16:00:00.000000000 -0800
+++ linux-2.2.13-pre4/drivers/scsi/ips.c	2003-08-15 15:07:26.000000000 -0700
@@ -0,0 +1,3940 @@
+/*****************************************************************************/
+/* ips.c -- driver for the IBM ServeRAID controller                          */
+/*                                                                           */
+/* Written By: Keith Mitchell, IBM Corporation                               */
+/*                                                                           */
+/* Copyright (C) 1999 IBM Corporation                                        */
+/*                                                                           */
+/* 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.                              */
+/*                                                                           */
+/* NO WARRANTY                                                               */
+/* THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR        */
+/* CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT      */
+/* LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,      */
+/* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is    */
+/* solely responsible for determining the appropriateness of using and       */
+/* distributing the Program and assumes all risks associated with its        */
+/* exercise of rights under this Agreement, including but not limited to     */
+/* the risks and costs of program errors, damage to or loss of data,         */
+/* programs or equipment, and unavailability or interruption of operations.  */
+/*                                                                           */
+/* DISCLAIMER OF LIABILITY                                                   */
+/* NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY   */
+/* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL        */
+/* DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND   */
+/* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR     */
+/* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    */
+/* USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED  */
+/* HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES             */
+/*                                                                           */
+/* 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
+/*                                                                           */
+/* Bugs/Comments/Suggestions should be mailed to:                            */
+/*      ipslinux@us.ibm.com                                                  */
+/*                                                                           */
+/*****************************************************************************/
+
+/*****************************************************************************/
+/* Change Log                                                                */
+/*                                                                           */
+/* 0.99.02  - Breakup commands that are bigger than 8 * the stripe size      */
+/* 0.99.03  - Make interrupt routine handle all completed request on the     */
+/*            adapter not just the first one                                 */
+/*          - Make sure passthru commands get woken up if we run out of      */
+/*            SCBs                                                           */
+/*          - Send all of the commands on the queue at once rather than      */
+/*            one at a time since the card will support it.                  */
+/* 0.99.04  - Fix race condition in the passthru mechanism -- this required  */
+/*            the interface to the utilities to change                       */
+/*          - Fix error recovery code                                        */
+/* 0.99.05  - Fix an oops when we get certain passthru commands              */
+/* 1.00.00  - Initial Public Release                                         */
+/*            Functionally equivalent to 0.99.05                             */
+/*                                                                           */
+/*****************************************************************************/
+
+/*
+ * Conditional Compilation directives for this driver:
+ *
+ * NO_IPS_RESET         - Don't reset the controller (no matter what)
+ * IPS_DEBUG            - More verbose error messages
+ * IPS_PCI_PROBE_DEBUG  - Print out more detail on the PCI probe
+ *
+ */
+
+#if defined (MODULE)
+   #include <linux/module.h>
+#endif /* MODULE */
+
+#include <asm/io.h>
+#include <asm/byteorder.h>
+#include <linux/stddef.h>
+#include <linux/version.h>
+#include <linux/string.h>
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/ioport.h>
+#include <linux/malloc.h>
+#include <linux/delay.h>
+#include <linux/sched.h>
+#include <linux/pci.h>
+#include <linux/proc_fs.h>
+
+#include <linux/blk.h>
+#include <linux/types.h>
+
+#ifndef NO_IPS_CMDLINE
+#include <scsi/sg.h>
+#endif
+
+#include "sd.h"
+#include "scsi.h"
+#include "hosts.h"
+#include "ips.h"
+
+#include <linux/stat.h>
+#include <linux/malloc.h>
+#include <linux/config.h>
+#include <asm/spinlock.h>
+#include <linux/smp.h>
+
+/*
+ * DRIVER_VER
+ */
+#define IPS_VERSION_HIGH        "1.00"  /* MUST be 4 chars */
+#define IPS_VERSION_LOW         ".00 "  /* MUST be 4 chars */
+
+struct proc_dir_entry proc_scsi_ips = {
+#if !defined(PROC_SCSI_IPS)
+   0,                                /* Use dynamic inode allocation */
+#else
+   PROC_SCSI_IPS,
+#endif
+   3, "ips",
+   S_IFDIR | S_IRUGO | S_IXUGO, 2
+};
+
+#if LINUX_VERSION_CODE < LinuxVersionCode(2,1,93)
+   #include <linux/bios32.h>
+#endif
+
+#if !defined(__i386__)
+   #error "This driver has only been tested on the x86 platform"
+#endif
+
+#if LINUX_VERSION_CODE < LinuxVersionCode(2,2,0)
+   #error "This driver only works with kernel 2.2.0 and later"
+#endif
+
+#if !defined(NO_IPS_CMDLINE) && ((SG_BIG_BUFF < 8192) || !defined(SG_BIG_BUFF))
+   #error "To use the command-line interface you need to define SG_BIG_BUFF"
+#endif
+
+#if IPS_DEBUG >= 12
+   #define DBG(s)       printk(KERN_NOTICE s "\n"); MDELAY(2*ONE_SEC)
+#elif IPS_DEBUG >= 11
+   #define DBG(s)       printk(KERN_NOTICE s "\n")
+#else
+   #define DBG(s)
+#endif
+
+/*
+ * global variables
+ */
+static const char * ips_name = "ips";
+static struct Scsi_Host * ips_sh[IPS_MAX_ADAPTERS];  /* Array of host controller structures */
+static ips_ha_t * ips_ha[IPS_MAX_ADAPTERS];          /* Array of HA structures */
+static unsigned int ips_num_controllers = 0;
+static int          ips_cmd_timeout = 60;
+static int          ips_reset_timeout = 60 * 5;
+
+#define MAX_ADAPTER_NAME 6
+
+static char ips_adapter_name[][30] = {
+   "ServeRAID",
+   "ServeRAID II",
+   "ServeRAID on motherboard",
+   "ServeRAID on motherboard",
+   "ServeRAID 3H",
+   "ServeRAID 3L"
+};
+
+/*
+ * Function prototypes
+ */
+int ips_detect(Scsi_Host_Template *);
+int ips_release(struct Scsi_Host *);
+int ips_abort(Scsi_Cmnd *);
+int ips_reset(Scsi_Cmnd *, unsigned int);
+int ips_eh_abort(Scsi_Cmnd *);
+int ips_eh_reset(Scsi_Cmnd *);
+int ips_queue(Scsi_Cmnd *, void (*) (Scsi_Cmnd *));
+int ips_biosparam(Disk *, kdev_t, int *);
+const char * ips_info(struct Scsi_Host *);
+void do_ipsintr(int, void *, struct pt_regs *);
+static int ips_hainit(ips_ha_t *);
+static int ips_map_status(ips_scb_t *, ips_stat_t *);
+static int ips_send(ips_ha_t *, ips_scb_t *, scb_callback);
+static int ips_send_wait(ips_ha_t *, ips_scb_t *, int);
+static int ips_send_cmd(ips_ha_t *, ips_scb_t *);
+static int ips_chkstatus(ips_ha_t *);
+static int ips_online(ips_ha_t *, ips_scb_t *);
+static int ips_inquiry(ips_ha_t *, ips_scb_t *);
+static int ips_rdcap(ips_ha_t *, ips_scb_t *);
+static int ips_msense(ips_ha_t *, ips_scb_t *);
+static int ips_reqsen(ips_ha_t *, ips_scb_t *);
+static int ips_allocatescbs(ips_ha_t *);
+static int ips_reset_adapter(ips_ha_t *);
+static int ips_statupd(ips_ha_t *);
+static int ips_issue(ips_ha_t *, ips_scb_t *);
+static int ips_isintr(ips_ha_t *);
+static int ips_wait(ips_ha_t *, int, int);
+static int ips_write_driver_status(ips_ha_t *);
+static int ips_read_adapter_status(ips_ha_t *);
+static int ips_read_subsystem_parameters(ips_ha_t *);
+static int ips_read_config(ips_ha_t *);
+static int ips_clear_adapter(ips_ha_t *);
+static int ips_readwrite_page5(ips_ha_t *, int);
+static void ips_intr(ips_ha_t *);
+static void ips_next(ips_ha_t *);
+static void ipsintr_blocking(ips_ha_t *, struct ips_scb *);
+static void ipsintr_done(ips_ha_t *, struct ips_scb *);
+static void ips_done(ips_ha_t *, ips_scb_t *);
+static void ips_free(ips_ha_t *);
+static void ips_init_scb(ips_ha_t *, ips_scb_t *);
+static void ips_freescb(ips_ha_t *, ips_scb_t *);
+static void ips_statinit(ips_ha_t *);
+static ips_scb_t * ips_getscb(ips_ha_t *);
+static inline void ips_putq_scb_head(ips_scb_queue_t *, ips_scb_t *);
+static inline void ips_putq_scb_tail(ips_scb_queue_t *, ips_scb_t *);
+static inline ips_scb_t * ips_removeq_scb_head(ips_scb_queue_t *);
+static inline ips_scb_t * ips_removeq_scb(ips_scb_queue_t *, ips_scb_t *);
+static inline void ips_putq_wait_head(ips_wait_queue_t *, Scsi_Cmnd *);
+static inline void ips_putq_wait_tail(ips_wait_queue_t *, Scsi_Cmnd *);
+static inline Scsi_Cmnd * ips_removeq_wait_head(ips_wait_queue_t *);
+static inline Scsi_Cmnd * ips_removeq_wait(ips_wait_queue_t *, Scsi_Cmnd *);
+
+#ifndef NO_IPS_CMDLINE
+static int ips_is_passthru(Scsi_Cmnd *);
+static int ips_make_passthru(ips_ha_t *, Scsi_Cmnd *, ips_scb_t *);
+static int ips_usrcmd(ips_ha_t *, ips_passthru_t *, ips_scb_t *);
+#endif
+
+int  ips_proc_info(char *, char **, off_t, int, int, int);
+static int ips_host_info(ips_ha_t *, char *, off_t, int);
+static void copy_mem_info(INFOSTR *, char *, int);
+static int copy_info(INFOSTR *, char *, ...);
+
+/*--------------------------------------------------------------------------*/
+/* Exported Functions                                                       */
+/*--------------------------------------------------------------------------*/
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_detect                                                 */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Detect and initialize the driver                                       */
+/*                                                                          */
+/****************************************************************************/
+int
+ips_detect(Scsi_Host_Template *SHT) {
+   struct Scsi_Host *sh;
+   ips_ha_t         *ha;
+   u32               io_addr;
+   u16               planer;
+   u8                bus;
+   u8                func;
+   u8                irq;
+   int               index;
+   struct pci_dev   *dev = NULL;
+
+   DBG("ips_detect");
+
+   SHT->proc_info = ips_proc_info;
+   SHT->proc_dir = &proc_scsi_ips;
+
+#if defined(CONFIG_PCI)
+
+   /* initalize number of controllers */
+   ips_num_controllers = 0;
+
+   if (!pci_present())
+      return (0);
+
+   for (index = 0; index < IPS_MAX_ADAPTERS; index++) {
+
+      if (!(dev = pci_find_device(IPS_VENDORID, IPS_DEVICEID, dev)))
+         break;
+
+      /* stuff that we get in dev */
+      irq = dev->irq;
+      bus = dev->bus->number;
+      func = dev->devfn;
+      io_addr = dev->base_address[0];
+
+      /* get planer status */
+      if (pci_read_config_word(dev, 0x04, &planer)) {
+         printk(KERN_WARNING "(%s%d) can't get planer status.\n",
+                ips_name, index);
+
+         continue;
+      }
+
+      /* check I/O address */
+      if ((io_addr & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_IO)
+         continue;
+
+      /* check to see if an onboard planer controller is disabled */
+      if (!(planer & 0x000C)) {
+
+   #ifdef IPS_PCI_PROBE_DEBUG
+         printk(KERN_NOTICE "(%s%d) detect, Onboard ServeRAID disabled by BIOS\n",
+                ips_name, index);
+   #endif
+
+         continue;
+      }
+
+      /* get the BASE IO Address */
+      io_addr &= PCI_BASE_ADDRESS_IO_MASK;
+
+   #ifdef IPS_PCI_PROBE_DEBUG
+      printk(KERN_NOTICE "(%s%d) detect bus %d, func %x, irq %d, io %x\n",
+             ips_name, index, bus, func, irq, io_addr);
+   #endif
+
+      /* found a controller */
+      sh = scsi_register(SHT, sizeof(ips_ha_t));
+
+      if (sh == NULL) {
+         printk(KERN_WARNING "(%s%d) Unable to register controller with SCSI subsystem - skipping controller\n",
+                ips_name, index);
+
+         continue;
+      }
+
+      ha = HA(sh);
+      memset(ha, 0, sizeof(ips_ha_t));
+
+      /* Initialize spin lock */
+      spin_lock_init(&ha->scb_lock);
+      spin_lock_init(&ha->copp_lock);
+
+      ips_sh[ips_num_controllers] = sh;
+      ips_ha[ips_num_controllers] = ha;
+      ips_num_controllers++;
+      ha->active = 1;
+
+      ha->enq = kmalloc(sizeof(ENQCMD), GFP_KERNEL|GFP_DMA);
+
+      if (!ha->enq) {
+         printk(KERN_WARNING "(%s%d) Unable to allocate host inquiry structure - skipping contoller\n",
+                ips_name, index);
+
+         ha->active = 0;
+
+         continue;
+      }
+
+      ha->adapt = kmalloc(sizeof(ADAPTER_AREA), GFP_KERNEL|GFP_DMA);
+
+      if (!ha->adapt) {
+         printk(KERN_WARNING "(%s%d) Unable to allocate host adapt structure - skipping controller\n",
+                ips_name, index);
+
+         ha->active = 0;
+
+         continue;
+      }
+
+      ha->conf = kmalloc(sizeof(CONFCMD), GFP_KERNEL|GFP_DMA);
+
+      if (!ha->conf) {
+         printk(KERN_WARNING "(%s%d) Unable to allocate host conf structure - skipping controller\n",
+                ips_name, index);
+
+         ha->active = 0;
+
+         continue;
+      }
+
+      ha->nvram = kmalloc(sizeof(NVRAM_PAGE5), GFP_KERNEL|GFP_DMA);
+
+      if (!ha->nvram) {
+         printk(KERN_WARNING "(%s%d) Unable to allocate host nvram structure - skipping controller\n",
+                ips_name, index);
+
+         ha->active = 0;
+
+         continue;
+      }
+
+      ha->subsys = kmalloc(sizeof(SUBSYS_PARAM), GFP_KERNEL|GFP_DMA);
+
+      if (!ha->subsys) {
+         printk(KERN_WARNING "(%s%d) Unable to allocate host subsystem structure - skipping controller\n",
+                ips_name, index);
+
+         ha->active = 0;
+
+         continue;
+      }
+
+      ha->dummy = kmalloc(sizeof(BASIC_IO_CMD), GFP_KERNEL|GFP_DMA);
+
+      if (!ha->dummy) {
+         printk(KERN_WARNING "(%s%d) Unable to allocate host dummy structure - skipping controller\n",
+                ips_name, index);
+
+         ha->active = 0;
+
+         continue;
+      }
+
+      /* Store away needed values for later use */
+      sh->io_port = io_addr;
+      sh->n_io_port = 255;
+      sh->unique_id = io_addr;
+      sh->irq = irq;
+      sh->select_queue_depths = NULL;
+      sh->sg_tablesize = sh->hostt->sg_tablesize;
+      sh->can_queue = sh->hostt->can_queue;
+      sh->cmd_per_lun = sh->hostt->cmd_per_lun;
+      sh->unchecked_isa_dma = sh->hostt->unchecked_isa_dma;
+      sh->use_clustering = sh->hostt->use_clustering;
+      sh->wish_block = FALSE;
+
+      /* Store info in HA structure */
+      ha->io_addr = io_addr;
+      ha->irq = irq;
+      ha->host_num = index;
+
+      /* install the interrupt handler */
+      if (request_irq(irq, do_ipsintr, SA_SHIRQ, ips_name, ha)) {
+         printk(KERN_WARNING "(%s%d) unable to install interrupt handler - skipping controller\n",
+                ips_name, index);
+
+         ha->active = 0;
+
+         continue;
+      }
+
+      /*
+       * Allocate a temporary SCB for initialization
+       */
+      ha->scbs = (ips_scb_t *) kmalloc(sizeof(ips_scb_t), GFP_KERNEL|GFP_DMA);
+      if (!ha->scbs) {
+         /* couldn't allocate a temp SCB */
+         printk(KERN_WARNING "(%s%d) unable to allocate CCBs - skipping contoller\n",
+                ips_name, index);
+
+         ha->active = 0;
+
+         continue;
+      }
+
+      memset(ha->scbs, 0, sizeof(ips_scb_t));
+      ha->scbs->sg_list = (SG_LIST *) kmalloc(sizeof(SG_LIST) * MAX_SG_ELEMENTS, GFP_KERNEL|GFP_DMA);
+      if (!ha->scbs->sg_list) {
+         /* couldn't allocate a temp SCB S/G list */
+         printk(KERN_WARNING "(%s%d) unable to allocate CCBs - skipping contoller\n",
+                ips_name, index);
+
+         ha->active = 0;
+
+         continue;
+      }
+
+      ha->max_cmds = 1;
+
+      if (!ips_hainit(ha)) {
+         printk(KERN_WARNING "(%s%d) unable to initialize controller - skipping\n",
+                ips_name, index);
+
+         ha->active = 0;
+
+         continue;
+      }
+
+      /*
+       * Free the temporary SCB
+       */
+      kfree(ha->scbs->sg_list);
+      kfree(ha->scbs);
+      ha->scbs = NULL;
+
+      /* allocate CCBs */
+      if (!ips_allocatescbs(ha)) {
+         printk(KERN_WARNING "(%s%d) unable to allocate CCBs - skipping contoller\n",
+                ips_name, index);
+
+         ha->active = 0;
+
+         continue;
+      }
+
+      /* finish setting values */
+      sh->max_id = ha->ntargets;
+      sh->max_lun = ha->nlun;
+      sh->max_channel = ha->nbus;
+      sh->can_queue = ha->max_cmds-1;
+   } /* end for */
+
+   return (ips_num_controllers);
+
+#else
+
+   /* No PCI -- No ServeRAID */
+   return (0);
+#endif /* CONFIG_PCI */
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_release                                                */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Remove a driver                                                        */
+/*                                                                          */
+/****************************************************************************/
+int
+ips_release(struct Scsi_Host *sh) {
+   ips_scb_t *scb;
+   ips_ha_t  *ha;
+   int        i;
+
+   DBG("ips_release");
+
+   for (i = 0; i < IPS_MAX_ADAPTERS && ips_sh[i] != sh; i++);
+
+   if (i == IPS_MAX_ADAPTERS)
+      panic("(%s) release, invalid Scsi_Host pointer.\n",
+            ips_name);
+
+   ha = HA(sh);
+
+   if (!ha)
+      return (FALSE);
+
+   /* flush the cache on the controller */
+   scb = &ha->scbs[ha->max_cmds-1];
+
+   ips_init_scb(ha, scb);
+
+   scb->timeout = ips_cmd_timeout;
+   scb->cdb[0] = FLUSH_CACHE;
+
+   scb->cmd.flush_cache.op_code = FLUSH_CACHE;
+   scb->cmd.flush_cache.command_id = IPS_COMMAND_ID(ha, scb);
+   scb->cmd.flush_cache.state = NORM_STATE;
+   scb->cmd.flush_cache.reserved = 0;
+   scb->cmd.flush_cache.reserved2 = 0;
+   scb->cmd.flush_cache.reserved3 = 0;
+   scb->cmd.flush_cache.reserved4 = 0;
+
+   printk("(%s%d) Flushing Cache.\n", ips_name, ha->host_num);
+
+   /* send command */
+   if (ips_send_wait(ha, scb, ips_cmd_timeout) == IPS_FAILURE)
+      printk("(%s%d) Incomplete Flush.\n", ips_name, ha->host_num);
+
+   printk("(%s%d) Flushing Complete.\n", ips_name, ha->host_num);
+
+   ips_sh[i] = NULL;
+   ips_ha[i] = NULL;
+
+   /* free extra memory */
+   ips_free(ha);
+
+   /* free IRQ */
+   free_irq(ha->irq, ha);
+
+   /* unregister with SCSI sub system */
+   scsi_unregister(sh);
+
+   return (FALSE);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_eh_abort                                               */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Abort a command (using the new error code stuff)                       */
+/*                                                                          */
+/****************************************************************************/
+int
+ips_eh_abort(Scsi_Cmnd *SC) {
+   ips_ha_t         *ha;
+
+   DBG("ips_eh_abort");
+
+   if (!SC)
+      return (FAILED);
+
+   ha = (ips_ha_t *) SC->host->hostdata;
+
+   if (!ha)
+      return (FAILED);
+
+   if (!ha->active)
+      return (FAILED);
+
+   if (SC->serial_number != SC->serial_number_at_timeout) {
+      /* HMM, looks like a bogus command */
+#if IPS_DEBUG >= 1
+      printk(KERN_NOTICE "Abort called with bogus scsi command\n");
+#endif
+
+      return (FAILED);
+   }
+
+   if (test_and_set_bit(IPS_IN_ABORT, &ha->flags))
+      return (FAILED);
+
+   /* See if the command is on the wait queue */
+   if (ips_removeq_wait(&ha->scb_waitlist, SC) ||
+       ips_removeq_wait(&ha->copp_waitlist, SC)) {
+      /* command not sent yet */
+      clear_bit(IPS_IN_ABORT, &ha->flags);
+
+      return (SUCCESS);
+   } else {
+      /* command must have already been sent */
+      clear_bit(IPS_IN_ABORT, &ha->flags);
+
+      return (FAILED);
+   }
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_abort                                                  */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Abort a command                                                        */
+/*                                                                          */
+/****************************************************************************/
+int
+ips_abort(Scsi_Cmnd *SC) {
+   ips_ha_t         *ha;
+
+   DBG("ips_abort");
+
+   if (!SC)
+      return (SCSI_ABORT_SNOOZE);
+
+   ha = (ips_ha_t *) SC->host->hostdata;
+
+   if (!ha)
+      return (SCSI_ABORT_SNOOZE);
+
+   if (!ha->active)
+      return (SCSI_ABORT_SNOOZE);
+
+   if (SC->serial_number != SC->serial_number_at_timeout) {
+      /* HMM, looks like a bogus command */
+#if IPS_DEBUG >= 1
+      printk(KERN_NOTICE "Abort called with bogus scsi command\n");
+#endif
+
+      return (SCSI_ABORT_NOT_RUNNING);
+   }
+
+   if (test_and_set_bit(IPS_IN_ABORT, &ha->flags))
+      return (SCSI_ABORT_SNOOZE);
+
+   /* See if the command is on the wait queue */
+   if (ips_removeq_wait(&ha->scb_waitlist, SC) ||
+       ips_removeq_wait(&ha->copp_waitlist, SC)) {
+      /* command not sent yet */
+      clear_bit(IPS_IN_ABORT, &ha->flags);
+
+      return (SCSI_ABORT_PENDING);
+   } else {
+      /* command must have already been sent */
+      clear_bit(IPS_IN_ABORT, &ha->flags);
+
+      return (SCSI_ABORT_SNOOZE);
+   }
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_eh_reset                                               */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Reset the controller (with new eh error code)                          */
+/*                                                                          */
+/****************************************************************************/
+int
+ips_eh_reset(Scsi_Cmnd *SC) {
+   ips_ha_t         *ha;
+   ips_scb_t        *scb;
+
+   DBG("ips_eh_reset");
+
+#ifdef NO_IPS_RESET
+   return (FAILED);
+#else
+
+   if (!SC) {
+
+#if IPS_DEBUG >= 1
+      printk(KERN_NOTICE "Reset called with NULL scsi command\n");
+#endif
+
+      return (FAILED);
+   }
+
+   ha = (ips_ha_t *) SC->host->hostdata;
+
+   if (!ha) {
+
+#if IPS_DEBUG >= 1
+      printk(KERN_NOTICE "Reset called with NULL ha struct\n");
+#endif
+
+      return (FAILED);
+   }
+
+   if (!ha->active)
+      return (FAILED);
+
+   if (test_and_set_bit(IPS_IN_RESET, &ha->flags))
+      return (FAILED);
+
+   /* See if the command is on the waiting queue */
+   if (ips_removeq_wait(&ha->scb_waitlist, SC) ||
+       ips_removeq_wait(&ha->copp_waitlist, SC)) {
+      /* command not sent yet */
+      clear_bit(IPS_IN_ABORT, &ha->flags);
+
+      return (SUCCESS);
+   }
+
+   /*
+    * command must have already been sent
+    * reset the controller
+    */
+   if (!ips_reset_adapter(ha)) {
+      clear_bit(IPS_IN_RESET, &ha->flags);
+
+      return (FAILED);
+   }
+
+   if (!ips_clear_adapter(ha)) {
+      clear_bit(IPS_IN_RESET, &ha->flags);
+
+      return (FAILED);
+   }
+
+   /* Now fail all of the active commands */
+#if IPS_DEBUG >= 1
+   printk(KERN_WARNING "(%s%d) Failing active commands\n",
+          ips_name, ha->host_num);
+#endif
+   while ((scb = ips_removeq_scb_head(&ha->scb_activelist))) {
+      scb->scsi_cmd->result = DID_RESET << 16;
+      scb->scsi_cmd->scsi_done(scb->scsi_cmd);
+      ips_freescb(ha, scb);
+   }
+
+   /* Reset the number of active IOCTLs */
+   ha->num_ioctl = 0;
+
+   clear_bit(IPS_IN_RESET, &ha->flags);
+
+   if (!test_bit(IPS_IN_INTR, &ha->flags)) {
+      /*
+       * Only execute the next command when
+       * we are not being called from the
+       * interrupt handler.  The interrupt
+       * handler wants to do this and since
+       * interrupts are turned off here....
+       */
+      ips_next(ha);
+   }
+
+   return (SUCCESS);
+
+#endif /* NO_IPS_RESET */
+
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_reset                                                  */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Reset the controller                                                   */
+/*                                                                          */
+/****************************************************************************/
+int
+ips_reset(Scsi_Cmnd *SC, unsigned int flags) {
+   ips_ha_t         *ha;
+   ips_scb_t        *scb;
+
+   DBG("ips_reset");
+
+#ifdef NO_IPS_RESET
+   return (SCSI_RESET_SNOOZE);
+#else
+
+   if (!SC) {
+
+#if IPS_DEBUG >= 1
+      printk(KERN_NOTICE "Reset called with NULL scsi command\n");
+#endif
+
+      return (SCSI_RESET_SNOOZE);
+   }
+
+   ha = (ips_ha_t *) SC->host->hostdata;
+
+   if (!ha) {
+
+#if IPS_DEBUG >= 1
+      printk(KERN_NOTICE "Reset called with NULL ha struct\n");
+#endif
+
+      return (SCSI_RESET_SNOOZE);
+   }
+
+   if (!ha->active)
+      return (SCSI_RESET_SNOOZE);
+
+   if (test_and_set_bit(IPS_IN_RESET, &ha->flags))
+      return (SCSI_RESET_SNOOZE);
+
+   /* See if the command is on the waiting queue */
+   if (ips_removeq_wait(&ha->scb_waitlist, SC) ||
+       ips_removeq_wait(&ha->copp_waitlist, SC)) {
+      /* command not sent yet */
+      clear_bit(IPS_IN_ABORT, &ha->flags);
+
+      return (SCSI_RESET_SNOOZE);
+   }
+
+   /* reset the controller */
+   if (!ips_reset_adapter(ha)) {
+      clear_bit(IPS_IN_RESET, &ha->flags);
+
+      return (SCSI_RESET_ERROR);
+   }
+
+   if (!ips_clear_adapter(ha)) {
+      clear_bit(IPS_IN_RESET, &ha->flags);
+
+      return (SCSI_RESET_ERROR);
+   }
+
+   /* Now fail all of the active commands */
+#if IPS_DEBUG >= 1
+   printk(KERN_WARNING "(%s%d) Failing active commands\n",
+          ips_name, ha->host_num);
+#endif
+   while ((scb = ips_removeq_scb_head(&ha->scb_activelist))) {
+      scb->scsi_cmd->result = DID_RESET << 16;
+      scb->scsi_cmd->scsi_done(scb->scsi_cmd);
+      ips_freescb(ha, scb);
+   }
+
+   /* Reset the number of active IOCTLs */
+   ha->num_ioctl = 0;
+
+   clear_bit(IPS_IN_RESET, &ha->flags);
+
+   if (!test_bit(IPS_IN_INTR, &ha->flags)) {
+      /*
+       * Only execute the next command when
+       * we are not being called from the
+       * interrupt handler.  The interrupt
+       * handler wants to do this and since
+       * interrupts are turned off here....
+       */
+      ips_next(ha);
+   }
+
+   return (SCSI_RESET_SUCCESS);
+
+#endif /* NO_IPS_RESET */
+
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_queue                                                  */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Send a command to the controller                                       */
+/*                                                                          */
+/****************************************************************************/
+int
+ips_queue(Scsi_Cmnd *SC, void (*done) (Scsi_Cmnd *)) {
+   ips_ha_t         *ha;
+
+   DBG("ips_queue");
+
+   ha = (ips_ha_t *) SC->host->hostdata;
+
+   if (!ha)
+      return (1);
+
+   if (!ha->active)
+      return (1);
+
+#ifndef NO_IPS_CMDLINE
+   if (ips_is_passthru(SC)) {
+      if (ha->copp_waitlist.count == IPS_MAX_IOCTL_QUEUE) {
+         SC->result = DID_BUS_BUSY << 16;
+         done(SC);
+
+         return (0);
+      }
+   } else {
+#endif
+      if (ha->scb_waitlist.count == IPS_MAX_QUEUE) {
+         SC->result = DID_BUS_BUSY << 16;
+         done(SC);
+
+         return (0);
+      }
+
+#ifndef NO_IPS_CMDLINE
+   }
+#endif
+
+   SC->scsi_done = done;
+
+#if IPS_DEBUG >= 10
+   printk(KERN_NOTICE "%s: ips_queue: cmd 0x%X (%d %d %d)\n",
+          ips_name,
+          SC->cmnd[0],
+          SC->channel,
+          SC->target,
+          SC->lun);
+#if IPS_DEBUG >= 11
+        MDELAY(2*ONE_SEC);
+#endif
+#endif
+
+#ifndef NO_IPS_CMDLINE
+   if (ips_is_passthru(SC))
+      ips_putq_wait_tail(&ha->copp_waitlist, SC);
+   else
+#endif
+      ips_putq_wait_tail(&ha->scb_waitlist, SC);
+
+   if ((!test_bit(IPS_IN_INTR, &ha->flags)) &&
+       (!test_bit(IPS_IN_ABORT, &ha->flags)) &&
+       (!test_bit(IPS_IN_RESET, &ha->flags)))
+      ips_next(ha);
+
+   return (0);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_biosparam                                              */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Set bios geometry for the controller                                   */
+/*                                                                          */
+/****************************************************************************/
+int
+ips_biosparam(Disk *disk, kdev_t dev, int geom[]) {
+   ips_ha_t         *ha;
+   int               heads;
+   int               sectors;
+   int               cylinders;
+
+   DBG("ips_biosparam");
+
+   ha = (ips_ha_t *) disk->device->host->hostdata;
+
+   if (!ha)
+      /* ?!?! host adater info invalid */
+      return (0);
+
+   if (!ha->active)
+      return (0);
+
+   if (!ips_read_adapter_status(ha))
+      /* ?!?! Enquiry command failed */
+      return (0);
+
+   if ((disk->capacity > 0x400000) &&
+       ((ha->enq->ucMiscFlag & 0x8) == 0)) {
+      heads = NORM_MODE_HEADS;
+      sectors = NORM_MODE_SECTORS;
+   } else {
+      heads = COMP_MODE_HEADS;
+      sectors = COMP_MODE_SECTORS;
+   }
+
+   cylinders = disk->capacity / (heads * sectors);
+
+#if IPS_DEBUG >= 2
+   printk(KERN_NOTICE "Geometry: heads: %d, sectors: %d, cylinders: %d\n",
+          heads, sectors, cylinders);
+#endif
+
+   geom[0] = heads;
+   geom[1] = sectors;
+   geom[2] = cylinders;
+
+   return (0);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: do_ipsintr                                                 */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Wrapper for the interrupt handler                                      */
+/*                                                                          */
+/****************************************************************************/
+void
+do_ipsintr(int irq, void *dev_id, struct pt_regs *regs) {
+   ips_ha_t         *ha;
+   unsigned int      cpu_flags;
+
+   DBG("do_ipsintr");
+
+   ha = (ips_ha_t *) dev_id;
+
+   spin_lock_irqsave(&io_request_lock, cpu_flags);
+
+   if (test_and_set_bit(IPS_IN_INTR, &ha->flags)) {
+      spin_unlock_irqrestore(&io_request_lock, cpu_flags);
+
+      return ;
+   }
+
+   if (!ha) {
+      clear_bit(IPS_IN_INTR, &ha->flags);
+      spin_unlock_irqrestore(&io_request_lock, cpu_flags);
+
+      return;
+   }
+
+   if (!ha->active) {
+      clear_bit(IPS_IN_INTR, &ha->flags);
+      spin_unlock_irqrestore(&io_request_lock, cpu_flags);
+
+      return;
+   }
+
+   ips_intr(ha);
+
+   clear_bit(IPS_IN_INTR, &ha->flags);
+
+   spin_unlock_irqrestore(&io_request_lock, cpu_flags);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_intr                                                   */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Polling interrupt handler                                              */
+/*                                                                          */
+/*   ASSUMES interrupts are disabled                                        */
+/*                                                                          */
+/****************************************************************************/
+void
+ips_intr(ips_ha_t *ha) {
+   ips_stat_t       *sp;
+   ips_scb_t        *scb;
+   int               status;
+
+   DBG("ips_intr");
+
+   if (!ha)
+      return;
+
+   if (!ha->active)
+      return;
+
+   while (ips_isintr(ha)) {
+      sp = &ha->sp;
+
+      if ((status = ips_chkstatus(ha)) < 0) {
+         /* unexpected interrupt - no ccb */
+         printk(KERN_WARNING "(%s%d) Spurious interrupt; no ccb.\n",
+                ips_name, ha->host_num);
+         continue ;
+      }
+
+      scb = (ips_scb_t *) sp->scb_addr;
+
+      /*
+       * use the callback function to finish things up
+       * NOTE: interrupts are OFF for this
+       */
+      (*scb->callback) (ha, scb);
+   }
+
+   clear_bit(IPS_IN_INTR, &ha->flags);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_info                                                   */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Return info about the driver                                           */
+/*                                                                          */
+/****************************************************************************/
+const char *
+ips_info(struct Scsi_Host *SH) {
+   static char  buffer[256];
+   char        *bp;
+   ips_ha_t    *ha;
+
+   DBG("ips_info");
+
+   ha = HA(SH);
+
+   if (!ha)
+      return (NULL);
+
+   bp = &buffer[0];
+   memset(bp, 0, sizeof(buffer));
+
+   strcpy(bp, "IBM PCI ServeRAID ");
+   strcat(bp, IPS_VERSION_HIGH);
+   strcat(bp, IPS_VERSION_LOW);
+
+   if (ha->ad_type > 0 &&
+       ha->ad_type <= MAX_ADAPTER_NAME) {
+      strcat(bp, " <");
+      strcat(bp, ips_adapter_name[ha->ad_type-1]);
+      strcat(bp, ">");
+   }
+
+   return (bp);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_proc_info                                              */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   The passthru interface for the driver                                  */
+/*                                                                          */
+/****************************************************************************/
+int
+ips_proc_info(char *buffer, char **start, off_t offset,
+              int length, int hostno, int func) {
+   int           i;
+   int           ret;
+   ips_ha_t     *ha = NULL;
+
+   DBG("ips_proc_info");
+
+   /* Find our host structure */
+   for (i = 0; i < ips_num_controllers; i++) {
+      if (ips_sh[i] && ips_sh[i]->host_no == hostno) {
+         ha = (ips_ha_t *) ips_sh[i]->hostdata;
+
+         break;
+      }
+   }
+
+   if (!ha)
+      return (-EINVAL);
+
+   if (func) {
+      /* write */
+      return (0);
+   } else {
+      /* read */
+      if (start)
+         *start = buffer;
+
+      ret = ips_host_info(ha, buffer, offset, length);
+
+      return (ret);
+   }
+}
+
+/*--------------------------------------------------------------------------*/
+/* Helper Functions                                                         */
+/*--------------------------------------------------------------------------*/
+
+#ifndef NO_IPS_CMDLINE
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_is_passthru                                            */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Determine if the specified SCSI command is really a passthru command   */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_is_passthru(Scsi_Cmnd *SC) {
+   DBG("ips_is_passthru");
+
+   if (!SC)
+      return (0);
+
+   if ((SC->channel == 0) &&
+       (SC->target == IPS_ADAPTER_ID) &&
+       (SC->lun == 0) &&
+       (SC->cmnd[0] == 0x0d) &&
+       (SC->request_bufflen) &&
+       (!SC->use_sg) &&
+       (((char *) SC->request_buffer)[0] == 'C') &&
+       (((char *) SC->request_buffer)[1] == 'O') &&
+       (((char *) SC->request_buffer)[2] == 'P') &&
+       (((char *) SC->request_buffer)[3] == 'P')) {
+      return (1);
+   } else {
+      return (0);
+   }
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_is_passthru                                            */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Make a passthru command out of the info in the Scsi block              */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_make_passthru(ips_ha_t *ha, Scsi_Cmnd *SC, ips_scb_t *scb) {
+   ips_passthru_t *pt;
+
+   DBG("ips_make_passthru");
+
+   if (!SC->request_bufflen || !SC->request_buffer) {
+      /* no data */
+#if IPS_DEBUG_PT >= 1
+      printk(KERN_NOTICE "(%s%d) No passthru structure\n",
+             ips_name, ha->host_num);
+#endif
+
+      return (IPS_FAILURE);
+   }
+
+   if (SC->request_bufflen < sizeof(ips_passthru_t)) {
+      /* wrong size */
+#if IPS_DEBUG_PT >= 1
+      printk(KERN_NOTICE "(%s%d) Passthru structure wrong size\n",
+             ips_name, ha->host_num);
+#endif
+
+      return (IPS_FAILURE);
+   }
+
+   if ((((char *) SC->request_buffer)[0] != 'C') ||
+       (((char *) SC->request_buffer)[1] != 'O') ||
+       (((char *) SC->request_buffer)[2] != 'P') ||
+       (((char *) SC->request_buffer)[3] != 'P')) {
+      /* signature doesn't match */
+#if IPS_DEBUG_PT >= 1
+      printk(KERN_NOTICE "(%s%d) Wrong signature on passthru structure.\n",
+             ips_name, ha->host_num);
+#endif
+
+      return (IPS_FAILURE);
+   }
+
+   pt = (ips_passthru_t *) SC->request_buffer;
+   scb->scsi_cmd = SC;
+
+   if (SC->request_bufflen < (sizeof(ips_passthru_t) + pt->CmdBSize)) {
+      /* wrong size */
+#if IPS_DEBUG_PT >= 1
+      printk(KERN_NOTICE "(%s%d) Passthru structure wrong size\n",
+             ips_name, ha->host_num);
+#endif
+
+      return (IPS_FAILURE);
+   }
+
+   switch (pt->CoppCmd) {
+   case IPS_NUMCTRLS:
+      memcpy(SC->request_buffer + sizeof(ips_passthru_t),
+             &ips_num_controllers, sizeof(int));
+      SC->result = DID_OK << 16;
+
+      return (IPS_SUCCESS_IMM);
+   case IPS_CTRLINFO:
+      memcpy(SC->request_buffer + sizeof(ips_passthru_t),
+             ha, sizeof(ips_ha_t));
+      SC->result = DID_OK << 16;
+
+      return (IPS_SUCCESS_IMM);
+   case COPPUSRCMD:
+      if (ips_usrcmd(ha, pt, scb))
+         return (IPS_SUCCESS);
+      else
+         return (IPS_FAILURE);
+      break;
+   }
+
+   return (IPS_FAILURE);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_usrcmd                                                 */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Process a user command and make it ready to send                       */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_usrcmd(ips_ha_t *ha, ips_passthru_t *pt, ips_scb_t *scb) {
+   SG_LIST        *sg_list;
+
+   DBG("ips_usrcmd");
+
+   if ((!scb) || (!pt) || (!ha))
+      return (0);
+
+   /* Save the S/G list pointer so it doesn't get clobbered */
+   sg_list = scb->sg_list;
+
+   /* copy in the CP */
+   memcpy(&scb->cmd, &pt->CoppCP.cmd, sizeof(IOCTL_INFO));
+   memcpy(&scb->dcdb, &pt->CoppCP.dcdb, sizeof(DCDB_TABLE));
+
+   /* FIX stuff that might be wrong */
+   scb->sg_list = sg_list;
+   scb->scb_busaddr = VIRT_TO_BUS(scb);
+   scb->bus = 0;
+   scb->target_id = 0;
+   scb->lun = 0;
+   scb->sg_len = 0;
+   scb->data_len = 0;
+   scb->flags = 0;
+   scb->op_code = 0;
+   scb->callback = ipsintr_done;
+   scb->timeout = ips_cmd_timeout;
+   scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
+
+   /* we don't support DCDB/READ/WRITE Scatter Gather */
+   if ((scb->cmd.basic_io.op_code == READ_SCATTER_GATHER) ||
+       (scb->cmd.basic_io.op_code == WRITE_SCATTER_GATHER) ||
+       (scb->cmd.basic_io.op_code == DIRECT_CDB_SCATTER_GATHER))
+      return (0);
+
+   if (pt->CmdBSize && pt->CmdBuffer) {
+      scb->data_busaddr = VIRT_TO_BUS(scb->scsi_cmd->request_buffer + sizeof(ips_passthru_t));
+   } else {
+      scb->data_busaddr = 0L;
+   }
+
+   if (pt->CmdBSize) {
+      if (scb->cmd.dcdb.op_code == DIRECT_CDB) {
+         scb->cmd.dcdb.dcdb_address = VIRT_TO_BUS(&scb->dcdb);
+         scb->dcdb.buffer_pointer = scb->data_busaddr;
+      } else {
+         scb->cmd.basic_io.sg_addr = scb->data_busaddr;
+      }
+   }
+
+   /* set timeouts */
+   if (pt->TimeOut) {
+      scb->timeout = pt->TimeOut;
+
+      if (pt->TimeOut <= 10)
+         scb->dcdb.cmd_attribute |= TIMEOUT_10;
+      else if (pt->TimeOut <= 60)
+         scb->dcdb.cmd_attribute |= TIMEOUT_60;
+      else
+         scb->dcdb.cmd_attribute |= TIMEOUT_20M;
+   }
+
+   /* assume error */
+   scb->scsi_cmd->result = DID_ERROR << 16;
+
+   /* success */
+   return (1);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_cleanup_passthru                                       */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Cleanup after a passthru command                                       */
+/*                                                                          */
+/****************************************************************************/
+static void
+ips_cleanup_passthru(ips_scb_t *scb) {
+   ips_passthru_t *pt;
+
+   DBG("ips_cleanup_passthru");
+
+   if ((!scb) || (!scb->scsi_cmd) || (!scb->scsi_cmd->request_buffer)) {
+#if IPS_DEBUG_PT >= 1
+      printk(KERN_NOTICE "IPS couldn't cleanup\n");
+#endif
+
+      return ;
+   }
+
+   pt = (ips_passthru_t *) scb->scsi_cmd->request_buffer;
+
+   /* Copy data back to the user */
+   pt->BasicStatus = scb->basic_status;
+   pt->ExtendedStatus = scb->extended_status;
+
+   scb->scsi_cmd->result = DID_OK << 16;
+}
+
+#endif
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_host_info                                              */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   The passthru interface for the driver                                  */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_host_info(ips_ha_t *ha, char *ptr, off_t offset, int len) {
+   INFOSTR info;
+
+   DBG("ips_host_info");
+
+   info.buffer = ptr;
+   info.length = len;
+   info.offset = offset;
+   info.pos = 0;
+
+   copy_info(&info, "\nIBM ServeRAID General Information:\n\n");
+
+   if ((ha->nvram->signature == NVRAM_PAGE5_SIGNATURE) &&
+       (ha->nvram->adapter_type != 0))
+      copy_info(&info, "\tController Type                   : %s\n", ips_adapter_name[ha->ad_type-1]);
+   else
+      copy_info(&info, "\tController Type                   : Unknown\n");
+
+   copy_info(&info, "\tIO port address                   : 0x%lx\n", ha->io_addr);
+   copy_info(&info, "\tIRQ number                        : %d\n", ha->irq);
+
+   if (ha->nvram->signature == NVRAM_PAGE5_SIGNATURE)
+      copy_info(&info, "\tBIOS Version                      : %c%c%c%c%c%c%c%c\n",
+                ha->nvram->bios_high[0], ha->nvram->bios_high[1],
+                ha->nvram->bios_high[2], ha->nvram->bios_high[3],
+                ha->nvram->bios_low[0], ha->nvram->bios_low[1],
+                ha->nvram->bios_low[2], ha->nvram->bios_low[3]);
+
+   copy_info(&info, "\tFirmware Version                  : %c%c%c%c%c%c%c%c\n",
+             ha->enq->CodeBlkVersion[0], ha->enq->CodeBlkVersion[1],
+             ha->enq->CodeBlkVersion[2], ha->enq->CodeBlkVersion[3],
+             ha->enq->CodeBlkVersion[4], ha->enq->CodeBlkVersion[5],
+             ha->enq->CodeBlkVersion[6], ha->enq->CodeBlkVersion[7]);
+
+   copy_info(&info, "\tBoot Block Version                : %c%c%c%c%c%c%c%c\n",
+             ha->enq->BootBlkVersion[0], ha->enq->BootBlkVersion[1],
+             ha->enq->BootBlkVersion[2], ha->enq->BootBlkVersion[3],
+             ha->enq->BootBlkVersion[4], ha->enq->BootBlkVersion[5],
+             ha->enq->BootBlkVersion[6], ha->enq->BootBlkVersion[7]);
+
+   copy_info(&info, "\tDriver Version                    : %s%s\n",
+             IPS_VERSION_HIGH, IPS_VERSION_LOW);
+
+   copy_info(&info, "\tMax Physical Devices              : %d\n",
+             ha->enq->ucMaxPhysicalDevices);
+   copy_info(&info, "\tMax Active Commands               : %d\n",
+             ha->max_cmds);
+   copy_info(&info, "\tCurrent Queued Commands           : %d\n",
+             ha->scb_waitlist.count);
+   copy_info(&info, "\tCurrent Active Commands           : %d\n",
+             ha->scb_activelist.count - ha->num_ioctl);
+   copy_info(&info, "\tCurrent Queued PT Commands        : %d\n",
+             ha->copp_waitlist.count);
+   copy_info(&info, "\tCurrent Active PT Commands        : %d\n",
+             ha->num_ioctl);
+
+   copy_info(&info, "\n");
+
+   return (info.pos > info.offset ? info.pos - info.offset : 0);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: copy_mem_info                                              */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Copy data into an INFOSTR structure                                    */
+/*                                                                          */
+/****************************************************************************/
+static void
+copy_mem_info(INFOSTR *info, char *data, int len) {
+   DBG("copy_mem_info");
+
+   if (info->pos + len > info->length)
+      len = info->length - info->pos;
+
+   if (info->pos + len < info->offset) {
+      info->pos += len;
+      return;
+   }
+
+   if (info->pos < info->offset) {
+      data += (info->offset - info->pos);
+      len  -= (info->offset - info->pos);
+   }
+
+   if (len > 0) {
+      memcpy(info->buffer + info->pos, data, len);
+      info->pos += len;
+   }
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: copy_info                                                  */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   printf style wrapper for an info structure                             */
+/*                                                                          */
+/****************************************************************************/
+static int
+copy_info(INFOSTR *info, char *fmt, ...) {
+   va_list args;
+   char buf[81];
+   int len;
+
+   DBG("copy_info");
+
+   va_start(args, fmt);
+   len = vsprintf(buf, fmt, args);
+   va_end(args);
+
+   copy_mem_info(info, buf, len);
+
+   return (len);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_hainit                                                 */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Initialize the controller                                              */
+/*                                                                          */
+/* NOTE: Assumes to be called from with a lock                              */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_hainit(ips_ha_t *ha) {
+   int       i;
+
+   DBG("ips_hainit");
+
+   if (!ha)
+      return (0);
+
+   /* initialize status queue */
+   ips_statinit(ha);
+
+   /* Setup HBA ID's */
+   if (!ips_read_config(ha)) {
+
+#ifndef NO_IPS_RESET
+
+      /* Try to reset the controller and try again */
+      if (!ips_reset_adapter(ha)) {
+         printk(KERN_WARNING "(%s%d) unable to reset controller.\n",
+                ips_name, ha->host_num);
+
+         return (0);
+      }
+
+      if (!ips_clear_adapter(ha)) {
+         printk(KERN_WARNING "(%s%d) unable to initialize controller.\n",
+                ips_name, ha->host_num);
+
+         return (0);
+      }
+
+#endif
+
+      if (!ips_read_config(ha)) {
+         printk(KERN_WARNING "(%s%d) unable to read config from controller.\n",
+                ips_name, ha->host_num);
+
+         return (0);
+      }
+   } /* end if */
+
+   /* write driver version */
+   if (!ips_write_driver_status(ha)) {
+      printk(KERN_WARNING "(%s%d) unable to write driver info to controller.\n",
+             ips_name, ha->host_num);
+
+      return (0);
+   }
+
+   if (!ips_read_adapter_status(ha)) {
+      printk(KERN_WARNING "(%s%d) unable to read controller status.\n",
+             ips_name, ha->host_num);
+
+      return (0);
+   }
+
+   if (!ips_read_subsystem_parameters(ha)) {
+      printk(KERN_WARNING "(%s%d) unable to read subsystem parameters.\n",
+             ips_name, ha->host_num);
+
+      return (0);
+   }
+
+   /* set limits on SID, LUN, BUS */
+   ha->ntargets = MAX_TARGETS + 1;
+   ha->nlun = 1;
+   ha->nbus = (ha->enq->ucMaxPhysicalDevices / MAX_TARGETS);
+
+   switch (ha->conf->logical_drive[0].ucStripeSize) {
+   case 4:
+      ha->max_xfer = 0x10000;
+      break;
+
+   case 5:
+      ha->max_xfer = 0x20000;
+      break;
+
+   case 6:
+      ha->max_xfer = 0x40000;
+      break;
+
+   case 7:
+   default:
+      ha->max_xfer = 0x80000;
+      break;
+   }
+
+   /* setup max concurrent commands */
+   if (ha->subsys->param[4] & 0x1) {
+      /* Use the new method */
+      ha->max_cmds = ha->enq->ucConcurrentCmdCount;
+   } else {
+      /* use the old method */
+      switch (ha->conf->logical_drive[0].ucStripeSize) {
+      case 4:
+         ha->max_cmds = 32;
+         break;
+
+      case 5:
+         ha->max_cmds = 16;
+         break;
+
+      case 6:
+         ha->max_cmds = 8;
+         break;
+
+      case 7:
+      default:
+         ha->max_cmds = 4;
+         break;
+      }
+   }
+
+   /* set controller IDs */
+   ha->ha_id[0] = IPS_ADAPTER_ID;
+   for (i = 1; i < ha->nbus; i++)
+      ha->ha_id[i] = ha->conf->init_id[i-1] & 0x1f;
+
+   return (1);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_next                                                   */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Take the next command off the queue and send it to the controller      */
+/*                                                                          */
+/* ASSUMED to be called from within a lock                                  */
+/*                                                                          */
+/****************************************************************************/
+static void
+ips_next(ips_ha_t *ha) {
+   ips_scb_t    *scb;
+   Scsi_Cmnd    *SC;
+   int           ret;
+
+   DBG("ips_next");
+
+   if (!ha)
+      return ;
+
+#ifndef NO_IPS_CMDLINE
+   /*
+    * Send passthru commands
+    * These have priority over normal I/O
+    * but shouldn't affect performance too much
+    * since we limit the number that can be active
+    * on the card at any one time
+    */
+   while ((ha->num_ioctl < IPS_MAX_IOCTL) &&
+          (ha->copp_waitlist.head) &&
+          (scb = ips_getscb(ha))) {
+      SC = ips_removeq_wait_head(&ha->copp_waitlist);
+
+      ret = ips_make_passthru(ha, SC, scb);
+
+      switch (ret) {
+      case IPS_FAILURE:
+         if (scb->scsi_cmd) {
+            scb->scsi_cmd->result = DID_ERROR << 16;
+            scb->scsi_cmd->scsi_done(scb->scsi_cmd);
+         }
+         ips_freescb(ha, scb);
+         break;
+      case IPS_SUCCESS_IMM:
+         if (scb->scsi_cmd)
+            scb->scsi_cmd->scsi_done(scb->scsi_cmd);
+         ips_freescb(ha, scb);
+         break;
+      default:
+         break;
+      } /* end case */
+
+      if (ret != IPS_SUCCESS)
+         continue;
+
+      ret = ips_send_cmd(ha, scb);
+
+      if (ret == IPS_SUCCESS) {
+         ips_putq_scb_head(&ha->scb_activelist, scb);
+         ha->num_ioctl++;
+      }
+
+      switch(ret) {
+      case IPS_FAILURE:
+         if (scb->scsi_cmd) {
+            scb->scsi_cmd->result = DID_ERROR << 16;
+            scb->scsi_cmd->scsi_done(scb->scsi_cmd);
+         }
+
+         ips_freescb(ha, scb);
+         break;
+      case IPS_SUCCESS_IMM:
+         if (scb->scsi_cmd)
+            scb->scsi_cmd->scsi_done(scb->scsi_cmd);
+         ips_freescb(ha, scb);
+         break;
+      default:
+         break;
+      } /* end case */
+   }
+#endif
+
+   /*
+    * Send "Normal" I/O commands
+    */
+   while ((ha->scb_waitlist.head) &&
+          (scb = ips_getscb(ha))) {
+      SC = ips_removeq_wait_head(&ha->scb_waitlist);
+
+      SC->result = DID_OK;
+      SC->host_scribble = NULL;
+
+      memset(SC->sense_buffer, 0, sizeof(SC->sense_buffer));
+
+      scb->target_id = SC->target;
+      scb->lun = SC->lun;
+      scb->bus = SC->channel;
+      scb->scsi_cmd = SC;
+      scb->breakup = 0;
+      scb->data_len = 0;
+      scb->callback = ipsintr_done;
+      scb->timeout = ips_cmd_timeout;
+      memset(&scb->cmd, 0, 4);
+
+      /* copy in the CDB */
+      memcpy(scb->cdb, SC->cmnd, SC->cmd_len);
+
+      /* Now handle the data buffer */
+      if (SC->use_sg) {
+         struct scatterlist *sg;
+         int                 i;
+
+         sg = SC->request_buffer;
+
+         for (i = 0; i < SC->use_sg; i++) {
+            scb->sg_list[i].address = VIRT_TO_BUS(sg[i].address);
+            scb->sg_list[i].length = sg[i].length;
+
+            if (scb->data_len + sg[i].length > ha->max_xfer) {
+               /*
+                * Data Breakup required
+                */
+               scb->breakup = i;
+               break;
+            }
+
+            scb->data_len += sg[i].length;
+         }
+
+         if (!scb->breakup)
+            scb->sg_len = SC->use_sg;
+         else
+            scb->sg_len = scb->breakup;
+
+         scb->dcdb.transfer_length = scb->data_len;
+         scb->data_busaddr = VIRT_TO_BUS(scb->sg_list);
+      } else {
+         if (SC->request_bufflen) {
+            if (SC->request_bufflen > ha->max_xfer) {
+               /*
+                * Data breakup required
+                */
+               scb->breakup = 1;
+               scb->data_len = ha->max_xfer;
+            } else {
+               scb->data_len = SC->request_bufflen;
+            }
+
+            scb->dcdb.transfer_length = scb->data_len;
+            scb->data_busaddr = VIRT_TO_BUS(SC->request_buffer);
+            scb->sg_len = 0;
+         } else {
+            scb->data_busaddr = 0L;
+            scb->sg_len = 0;
+            scb->data_len = 0;
+            scb->dcdb.transfer_length = 0;
+         }
+
+      }
+
+      if ((scb->scsi_cmd->request.cmd == READ) && (SC->request_bufflen))
+         scb->dcdb.cmd_attribute |= DATA_IN;
+
+      if ((scb->scsi_cmd->request.cmd == WRITE) && (SC->request_bufflen))
+         scb->dcdb.cmd_attribute |= DATA_OUT;
+
+      if (scb->data_len >= IPS_MAX_XFER) {
+         scb->dcdb.cmd_attribute |= TRANSFER_64K;
+         scb->dcdb.transfer_length = 0;
+      }
+
+      ret = ips_send_cmd(ha, scb);
+
+      if (ret == IPS_SUCCESS)
+         ips_putq_scb_head(&ha->scb_activelist, scb);
+
+      switch(ret) {
+      case IPS_FAILURE:
+         if (scb->scsi_cmd) {
+            scb->scsi_cmd->result = DID_ERROR << 16;
+            scb->scsi_cmd->scsi_done(scb->scsi_cmd);
+         }
+
+         ips_freescb(ha, scb);
+         break;
+      case IPS_SUCCESS_IMM:
+         if (scb->scsi_cmd)
+            scb->scsi_cmd->scsi_done(scb->scsi_cmd);
+         ips_freescb(ha, scb);
+         break;
+      default:
+         break;
+      } /* end case */
+   } /* end while */
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_putq_scb_head                                          */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Add an item to the head of the queue                                   */
+/*                                                                          */
+/* ASSUMED to be called from within a lock                                  */
+/*                                                                          */
+/****************************************************************************/
+static inline void
+ips_putq_scb_head(ips_scb_queue_t *queue, ips_scb_t *item) {
+   DBG("ips_putq_scb_head");
+
+   if (!item)
+      return ;
+
+   item->q_next = queue->head;
+   queue->head = item;
+
+   if (!queue->tail)
+      queue->tail = item;
+
+   queue->count++;
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_putq_scb_tail                                          */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Add an item to the tail of the queue                                   */
+/*                                                                          */
+/* ASSUMED to be called from within a lock                                  */
+/*                                                                          */
+/****************************************************************************/
+static inline void
+ips_putq_scb_tail(ips_scb_queue_t *queue, ips_scb_t *item) {
+   DBG("ips_putq_scb_tail");
+
+   if (!item)
+      return ;
+
+   item->q_next = NULL;
+
+   if (queue->tail)
+      queue->tail->q_next = item;
+
+   queue->tail = item;
+
+   if (!queue->head)
+      queue->head = item;
+
+   queue->count++;
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_removeq_scb_head                                       */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Remove the head of the queue                                           */
+/*                                                                          */
+/* ASSUMED to be called from within a lock                                  */
+/*                                                                          */
+/****************************************************************************/
+static inline ips_scb_t *
+ips_removeq_scb_head(ips_scb_queue_t *queue) {
+   ips_scb_t  *item;
+
+   DBG("ips_removeq_scb_head");
+
+   item = queue->head;
+
+   if (!item)
+      return (NULL);
+
+   queue->head = item->q_next;
+   item->q_next = NULL;
+
+   if (queue->tail == item)
+      queue->tail = NULL;
+
+   queue->count--;
+
+   return (item);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_removeq_scb                                            */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Remove an item from a queue                                            */
+/*                                                                          */
+/* ASSUMED to be called from within a lock                                  */
+/*                                                                          */
+/****************************************************************************/
+static inline ips_scb_t *
+ips_removeq_scb(ips_scb_queue_t *queue, ips_scb_t *item) {
+   ips_scb_t  *p;
+
+   DBG("ips_removeq_scb");
+
+   if (!item)
+      return (NULL);
+
+   if (item == queue->head)
+      return (ips_removeq_scb_head(queue));
+
+   p = queue->head;
+
+   while ((p) && (item != p->q_next))
+      p = p->q_next;
+
+   if (p) {
+      /* found a match */
+      p->q_next = item->q_next;
+
+      if (!item->q_next)
+         queue->tail = p;
+
+      item->q_next = NULL;
+      queue->count--;
+
+      return (item);
+   }
+
+   return (NULL);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_putq_wait_head                                         */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Add an item to the head of the queue                                   */
+/*                                                                          */
+/* ASSUMED to be called from within a lock                                  */
+/*                                                                          */
+/****************************************************************************/
+static inline void
+ips_putq_wait_head(ips_wait_queue_t *queue, Scsi_Cmnd *item) {
+   DBG("ips_putq_wait_head");
+
+   if (!item)
+      return ;
+
+   item->host_scribble = (char *) queue->head;
+   queue->head = item;
+
+   if (!queue->tail)
+      queue->tail = item;
+
+   queue->count++;
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_putq_wait_tail                                         */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Add an item to the tail of the queue                                   */
+/*                                                                          */
+/* ASSUMED to be called from within a lock                                  */
+/*                                                                          */
+/****************************************************************************/
+static inline void
+ips_putq_wait_tail(ips_wait_queue_t *queue, Scsi_Cmnd *item) {
+   DBG("ips_putq_wait_tail");
+
+   if (!item)
+      return ;
+
+   item->host_scribble = NULL;
+
+   if (queue->tail)
+      queue->tail->host_scribble = (char *)item;
+
+   queue->tail = item;
+
+   if (!queue->head)
+      queue->head = item;
+
+   queue->count++;
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_removeq_wait_head                                      */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Remove the head of the queue                                           */
+/*                                                                          */
+/* ASSUMED to be called from within a lock                                  */
+/*                                                                          */
+/****************************************************************************/
+static inline Scsi_Cmnd *
+ips_removeq_wait_head(ips_wait_queue_t *queue) {
+   Scsi_Cmnd  *item;
+
+   DBG("ips_removeq_wait_head");
+
+   item = queue->head;
+
+   if (!item)
+      return (NULL);
+
+   queue->head = (Scsi_Cmnd *) item->host_scribble;
+   item->host_scribble = NULL;
+
+   if (queue->tail == item)
+      queue->tail = NULL;
+
+   queue->count--;
+
+   return (item);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_removeq_wait                                           */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Remove an item from a queue                                            */
+/*                                                                          */
+/* ASSUMED to be called from within a lock                                  */
+/*                                                                          */
+/****************************************************************************/
+static inline Scsi_Cmnd *
+ips_removeq_wait(ips_wait_queue_t *queue, Scsi_Cmnd *item) {
+   Scsi_Cmnd  *p;
+
+   DBG("ips_removeq_wait");
+
+   if (!item)
+      return (NULL);
+
+   if (item == queue->head)
+      return (ips_removeq_wait_head(queue));
+
+   p = queue->head;
+
+   while ((p) && (item != (Scsi_Cmnd *) p->host_scribble))
+      p = (Scsi_Cmnd *) p->host_scribble;
+
+   if (p) {
+      /* found a match */
+      p->host_scribble = item->host_scribble;
+
+      if (!item->host_scribble)
+         queue->tail = p;
+
+      item->host_scribble = NULL;
+      queue->count--;
+
+      return (item);
+   }
+
+   return (NULL);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ipsintr_blocking                                           */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Finalize an interrupt for internal commands                            */
+/*                                                                          */
+/****************************************************************************/
+static void
+ipsintr_blocking(ips_ha_t *ha, ips_scb_t *scb) {
+   DBG("ipsintr_blocking");
+
+   if ((ha->waitflag == TRUE) &&
+       (ha->cmd_in_progress == scb->cdb[0])) {
+      ha->waitflag = FALSE;
+
+      return ;
+   }
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ipsintr_done                                               */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Finalize an interrupt for non-internal commands                        */
+/*                                                                          */
+/****************************************************************************/
+static void
+ipsintr_done(ips_ha_t *ha, ips_scb_t *scb) {
+   DBG("ipsintr_done");
+
+   if (scb->scsi_cmd == NULL) {
+      /* unexpected interrupt */
+      printk(KERN_WARNING "(%s%d) Spurious interrupt; scsi_cmd not set.\n",
+             ips_name, ha->host_num);
+
+      return;
+   }
+
+   ips_done(ha, scb);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_done                                                   */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Do housekeeping on completed commands                                  */
+/*                                                                          */
+/****************************************************************************/
+static void
+ips_done(ips_ha_t *ha, ips_scb_t *scb) {
+   int ret;
+
+   DBG("ips_done");
+
+   if (!scb)
+      return ;
+
+#ifndef NO_IPS_CMDLINE
+   if ((scb->scsi_cmd) && (ips_is_passthru(scb->scsi_cmd))) {
+      ips_cleanup_passthru(scb);
+      ha->num_ioctl--;
+   } else {
+#endif
+      /*
+       * Check to see if this command had too much
+       * data and had to be broke up.  If so, queue
+       * the rest of the data and continue.
+       */
+      if (scb->breakup) {
+         /* we had a data breakup */
+         u16 bk_save;
+
+         bk_save = scb->breakup;
+         scb->breakup = 0;
+
+         if (scb->scsi_cmd->use_sg) {
+            /* S/G request */
+            struct scatterlist *sg;
+            int                 i;
+
+            sg = scb->scsi_cmd->request_buffer;
+
+            scb->data_len = 0;
+
+            for (i = bk_save; i < scb->scsi_cmd->use_sg; i++) {
+               scb->sg_list[i - bk_save].address = VIRT_TO_BUS(sg[i].address);
+               scb->sg_list[i - bk_save].length = sg[i].length;
+
+               if (scb->data_len + sg[i].length > ha->max_xfer) {
+                  /*
+                   * Data Breakup required
+                   */
+                  scb->breakup = i;
+                  break;
+               }
+
+               scb->data_len += sg[i].length;
+            }
+
+            if (!scb->breakup)
+               scb->sg_len = scb->scsi_cmd->use_sg - bk_save;
+            else
+               scb->sg_len = scb->breakup - bk_save;
+
+            scb->dcdb.transfer_length = scb->data_len;
+            scb->data_busaddr = VIRT_TO_BUS(scb->sg_list);
+         } else {
+            /* Non S/G Request */
+            if (scb->scsi_cmd->request_bufflen - (bk_save * ha->max_xfer)) {
+               /* Further breakup required */
+               scb->data_len = ha->max_xfer;
+               scb->data_busaddr = VIRT_TO_BUS(scb->scsi_cmd->request_buffer + (bk_save * ha->max_xfer));
+               scb->breakup = bk_save + 1;
+            } else {
+               scb->data_len = scb->scsi_cmd->request_bufflen - (bk_save * ha->max_xfer);
+               scb->data_busaddr = VIRT_TO_BUS(scb->scsi_cmd->request_buffer + (bk_save * ha->max_xfer));
+            }
+
+            scb->dcdb.transfer_length = scb->data_len;
+            scb->sg_len = 0;
+         }
+
+         if ((scb->scsi_cmd->request.cmd == READ) && (scb->data_len))
+            scb->dcdb.cmd_attribute |= DATA_IN;
+
+         if ((scb->scsi_cmd->request.cmd == WRITE) && (scb->data_len))
+            scb->dcdb.cmd_attribute |= DATA_OUT;
+
+         if (scb->data_len >= IPS_MAX_XFER) {
+            scb->dcdb.cmd_attribute |= TRANSFER_64K;
+            scb->dcdb.transfer_length = 0;
+         }
+
+         ret = ips_send_cmd(ha, scb);
+
+         switch(ret) {
+         case IPS_FAILURE:
+            if (scb->scsi_cmd) {
+               scb->scsi_cmd->result = DID_ERROR << 16;
+               scb->scsi_cmd->scsi_done(scb->scsi_cmd);
+            }
+
+            ips_freescb(ha, scb);
+            break;
+         case IPS_SUCCESS_IMM:
+            if (scb->scsi_cmd) {
+               scb->scsi_cmd->result = DID_ERROR << 16;
+               scb->scsi_cmd->scsi_done(scb->scsi_cmd);
+            }
+
+            ips_freescb(ha, scb);
+            break;
+         default:
+            break;
+         } /* end case */
+
+         return ;
+      }
+#ifndef NO_IPS_CMDLINE
+   } /* end if passthru */
+#endif
+
+   /* call back to SCSI layer */
+   scb->scsi_cmd->scsi_done(scb->scsi_cmd);
+   ips_freescb(ha, scb);
+
+   /* do the next command */
+   ips_next(ha);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_map_status                                             */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Map ServeRAID error codes to Linux Error Codes                         */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_map_status(ips_scb_t *scb, ips_stat_t *sp) {
+   int       errcode;
+
+   DBG("ips_map_status");
+
+   if (scb->bus) {
+      /* copy SCSI status and sense data for DCDB commands */
+      memcpy(scb->scsi_cmd->sense_buffer, scb->dcdb.sense_info,
+             sizeof(scb->scsi_cmd->sense_buffer));
+      scb->scsi_cmd->result = scb->dcdb.scsi_status;
+   } else
+      scb->scsi_cmd->result = 0;
+
+   /* default driver error */
+   errcode = DID_ERROR;
+
+   switch (scb->basic_status & GSC_STATUS_MASK) {
+   case CMD_TIMEOUT:
+      errcode = DID_TIME_OUT;
+      break;
+
+   case INVAL_OPCO:
+   case INVAL_CMD_BLK:
+   case INVAL_PARM_BLK:
+   case LOG_DRV_ERROR:
+   case CMD_CMPLT_WERROR:
+      break;
+
+   case PHYS_DRV_ERROR:
+      /*
+       * For physical drive errors that
+       * are not on a logical drive should
+       * be DID_OK.  The SCSI errcode will
+       * show what the real error is.
+       */
+      if (scb->bus)
+         errcode = DID_OK;
+
+      switch (scb->extended_status) {
+      case SELECTION_TIMEOUT:
+         if (scb->bus) {
+            scb->scsi_cmd->result |= DID_TIME_OUT << 16;
+
+            return (0);
+         }
+         break;
+      case DATA_OVER_UNDER_RUN:
+         if ((scb->bus) && (scb->dcdb.transfer_length < scb->data_len)) {
+            if ((scb->scsi_cmd->cmnd[0] == INQUIRY) &&
+                ((((char *) scb->scsi_cmd->buffer)[0] & 0x1f) == TYPE_DISK)) {
+               /* underflow -- no error               */
+               /* restrict access to physical DASD    */
+               errcode = DID_TIME_OUT;
+               break;
+            }
+
+            /* normal underflow Occured */
+            if (scb->dcdb.transfer_length >= scb->scsi_cmd->underflow) {
+               scb->scsi_cmd->result |= DID_OK << 16;
+
+               return (0);
+            }
+         }
+
+         break;
+      case EXT_RECOVERY:
+         /* don't fail recovered errors */
+         if (scb->bus) {
+            scb->scsi_cmd->result |= DID_OK << 16;
+
+            return (0);
+         }
+         break;
+
+      case EXT_HOST_RESET:
+      case EXT_DEVICE_RESET:
+         errcode = DID_RESET;
+         break;
+
+      case EXT_CHECK_CONDITION:
+         break;
+      } /* end switch */
+   } /* end switch */
+
+   scb->scsi_cmd->result |= (errcode << 16);
+
+   return (1);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_send                                                   */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Wrapper for ips_send_cmd                                               */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_send(ips_ha_t *ha, ips_scb_t *scb, scb_callback callback) {
+   int ret;
+
+   DBG("ips_send");
+
+   scb->callback = callback;
+
+   ret = ips_send_cmd(ha, scb);
+
+   return (ret);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_send_wait                                              */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Send a command to the controller and wait for it to return             */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_send_wait(ips_ha_t *ha, ips_scb_t *scb, int timeout) {
+   int       ret;
+
+   DBG("ips_send_wait");
+
+   ha->waitflag = TRUE;
+   ha->cmd_in_progress = scb->cdb[0];
+
+   ret = ips_send(ha, scb, ipsintr_blocking);
+
+   if ((ret == IPS_FAILURE) || (ret == IPS_SUCCESS_IMM))
+      return (ret);
+
+   ret = ips_wait(ha, timeout, IPS_INTR_OFF);
+
+   return (ret);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_send_cmd                                               */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Map SCSI commands to ServeRAID commands for logical drives             */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_send_cmd(ips_ha_t *ha, ips_scb_t *scb) {
+   int       ret;
+
+   DBG("ips_send_cmd");
+
+   ret = IPS_SUCCESS;
+
+   if (!scb->scsi_cmd) {
+      /* internal command */
+
+      if (scb->bus > 0) {
+         /* ServeRAID commands can't be issued */
+         /* to real devices -- fail them       */
+         if ((ha->waitflag == TRUE) &&
+             (ha->cmd_in_progress == scb->cdb[0])) {
+            ha->waitflag = FALSE;
+         }
+
+         return (1);
+      }
+#ifndef NO_IPS_CMDLINE
+   } else if ((scb->bus == 0) && (!ips_is_passthru(scb->scsi_cmd))) {
+#else
+   } else if (scb->bus == 0) {
+#endif
+      /* command to logical bus -- interpret */
+      ret = IPS_SUCCESS_IMM;
+
+      switch (scb->scsi_cmd->cmnd[0]) {
+      case ALLOW_MEDIUM_REMOVAL:
+      case REZERO_UNIT:
+      case ERASE:
+      case WRITE_FILEMARKS:
+      case SPACE:
+         scb->scsi_cmd->result = DID_ERROR << 16;
+         break;
+
+      case START_STOP:
+         scb->scsi_cmd->result = DID_OK << 16;
+
+      case TEST_UNIT_READY:
+      case INQUIRY:
+         if (scb->target_id == IPS_ADAPTER_ID) {
+            /*
+             * Either we have a TUR
+             * or we have a SCSI inquiry
+             */
+            if (scb->scsi_cmd->cmnd[0] == TEST_UNIT_READY)
+               scb->scsi_cmd->result = DID_OK << 16;
+
+            if (scb->scsi_cmd->cmnd[0] == INQUIRY) {
+               INQUIRYDATA inq;
+
+               memset(&inq, 0, sizeof(INQUIRYDATA));
+
+               inq.DeviceType = TYPE_PROCESSOR;
+               inq.DeviceTypeQualifier = 0;
+               inq.RemoveableMedia = 0;
+               inq.Versions = 0x1;  /* SCSI I */
+               inq.AdditionalLength = 31;
+               strncpy(inq.VendorId, "IBM     ", 8);
+               strncpy(inq.ProductId, "SERVERAID       ", 16);
+               strncpy(inq.ProductRevisionLevel, "1.00", 4);
+
+               memcpy(scb->scsi_cmd->request_buffer, &inq, scb->scsi_cmd->request_bufflen);
+
+               scb->scsi_cmd->result = DID_OK << 16;
+            }
+         } else {
+            scb->cmd.logical_info.op_code = GET_LOGICAL_DRIVE_INFO;
+            scb->cmd.logical_info.command_id = IPS_COMMAND_ID(ha, scb);
+            scb->cmd.logical_info.buffer_addr = VIRT_TO_BUS(&ha->adapt->logical_drive_info);
+            scb->cmd.logical_info.reserved = 0;
+            scb->cmd.logical_info.reserved2 = 0;
+            ret = IPS_SUCCESS;
+         }
+
+         break;
+
+      case REQUEST_SENSE:
+         ips_reqsen(ha, scb);
+         scb->scsi_cmd->result = DID_OK << 16;
+         break;
+
+      case READ_6:
+      case WRITE_6:
+         if (!scb->sg_len) {
+            scb->cmd.basic_io.op_code =
+            (scb->scsi_cmd->cmnd[0] == READ_6) ? IPS_READ : IPS_WRITE;
+         } else {
+            scb->cmd.basic_io.op_code =
+            (scb->scsi_cmd->cmnd[0] == READ_6) ? READ_SCATTER_GATHER : WRITE_SCATTER_GATHER;
+         }
+
+         scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
+         scb->cmd.basic_io.log_drv = scb->target_id;
+         scb->cmd.basic_io.sg_count = scb->sg_len;
+         scb->cmd.basic_io.sg_addr = scb->data_busaddr;
+
+         if (scb->cmd.basic_io.lba)
+            scb->cmd.basic_io.lba += scb->cmd.basic_io.sector_count;
+         else
+            scb->cmd.basic_io.lba = (((scb->scsi_cmd->cmnd[1] & 0x1f) << 16) |
+                                     (scb->scsi_cmd->cmnd[2] << 8) |
+                                     (scb->scsi_cmd->cmnd[3]));
+
+         scb->cmd.basic_io.sector_count = scb->data_len / IPS_BLKSIZE;
+
+         if (scb->cmd.basic_io.sector_count == 0)
+            scb->cmd.basic_io.sector_count = 256;
+
+         scb->cmd.basic_io.reserved = 0;
+         ret = IPS_SUCCESS;
+         break;
+
+      case READ_10:
+      case WRITE_10:
+         if (!scb->sg_len) {
+            scb->cmd.basic_io.op_code =
+            (scb->scsi_cmd->cmnd[0] == READ_10) ? IPS_READ : IPS_WRITE;
+         } else {
+            scb->cmd.basic_io.op_code =
+            (scb->scsi_cmd->cmnd[0] == READ_10) ? READ_SCATTER_GATHER : WRITE_SCATTER_GATHER;
+         }
+
+         scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
+         scb->cmd.basic_io.log_drv = scb->target_id;
+         scb->cmd.basic_io.sg_count = scb->sg_len;
+         scb->cmd.basic_io.sg_addr = scb->data_busaddr;
+
+         if (scb->cmd.basic_io.lba)
+            scb->cmd.basic_io.lba += scb->cmd.basic_io.sector_count;
+         else
+            scb->cmd.basic_io.lba = ((scb->scsi_cmd->cmnd[2] << 24) |
+                                     (scb->scsi_cmd->cmnd[3] << 16) |
+                                     (scb->scsi_cmd->cmnd[4] << 8) |
+                                     scb->scsi_cmd->cmnd[5]);
+
+         scb->cmd.basic_io.sector_count = scb->data_len / IPS_BLKSIZE;
+
+         scb->cmd.basic_io.reserved = 0;
+
+         if (scb->cmd.basic_io.sector_count == 0) {
+            /*
+             * This is a null condition
+             * we don't have to do anything
+             * so just return
+             */
+            scb->scsi_cmd->result = DID_OK << 16;
+         } else
+            ret = IPS_SUCCESS;
+
+         break;
+
+      case RESERVE:
+      case RELEASE:
+         scb->scsi_cmd->result = DID_OK << 16;
+         break;
+
+      case MODE_SENSE:
+         scb->cmd.basic_io.op_code = ENQUIRY;
+         scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
+         scb->cmd.basic_io.sg_addr = VIRT_TO_BUS(ha->enq);
+         ret = IPS_SUCCESS;
+         break;
+
+      case READ_CAPACITY:
+         scb->cmd.logical_info.op_code = GET_LOGICAL_DRIVE_INFO;
+         scb->cmd.logical_info.command_id = IPS_COMMAND_ID(ha, scb);
+         scb->cmd.logical_info.buffer_addr = VIRT_TO_BUS(&ha->adapt->logical_drive_info);
+         scb->cmd.logical_info.reserved = 0;
+         scb->cmd.logical_info.reserved2 = 0;
+         scb->cmd.logical_info.reserved3 = 0;
+         ret = IPS_SUCCESS;
+         break;
+
+      case SEND_DIAGNOSTIC:
+      case REASSIGN_BLOCKS:
+      case FORMAT_UNIT:
+      case SEEK_10:
+      case VERIFY:
+      case READ_DEFECT_DATA:
+      case READ_BUFFER:
+      case WRITE_BUFFER:
+         scb->scsi_cmd->result = DID_OK << 16;
+         break;
+
+      default:
+         scb->scsi_cmd->result = DID_ERROR << 16;
+         break;
+      } /* end switch */
+   } /* end if */
+
+   if (ret == IPS_SUCCESS_IMM)
+      return (ret);
+
+   /* setup DCDB */
+   if (scb->bus > 0) {
+      if (!scb->sg_len)
+         scb->cmd.dcdb.op_code = DIRECT_CDB;
+      else
+         scb->cmd.dcdb.op_code = DIRECT_CDB_SCATTER_GATHER;
+
+      scb->cmd.dcdb.command_id = IPS_COMMAND_ID(ha, scb);
+      scb->cmd.dcdb.dcdb_address = VIRT_TO_BUS(&scb->dcdb);
+      scb->cmd.dcdb.reserved = 0;
+      scb->cmd.dcdb.reserved2 = 0;
+      scb->cmd.dcdb.reserved3 = 0;
+
+      scb->dcdb.device_address = ((scb->bus - 1) << 4) | scb->target_id;
+      scb->dcdb.cmd_attribute |= DISCONNECT_ALLOWED;
+
+      if (scb->timeout) {
+         if (scb->timeout <= 10)
+            scb->dcdb.cmd_attribute |= TIMEOUT_10;
+         else if (scb->timeout <= 60)
+            scb->dcdb.cmd_attribute |= TIMEOUT_60;
+         else
+            scb->dcdb.cmd_attribute |= TIMEOUT_20M;
+      }
+
+      if (!(scb->dcdb.cmd_attribute & TIMEOUT_20M))
+         scb->dcdb.cmd_attribute |= TIMEOUT_20M;
+
+      scb->dcdb.sense_length = sizeof(scb->scsi_cmd->sense_buffer);
+      scb->dcdb.buffer_pointer = scb->data_busaddr;
+      scb->dcdb.sg_count = scb->sg_len;
+      scb->dcdb.cdb_length = scb->scsi_cmd->cmd_len;
+      memcpy(scb->dcdb.scsi_cdb, scb->scsi_cmd->cmnd, scb->scsi_cmd->cmd_len);
+   }
+
+   return (ips_issue(ha, scb));
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_chk_status                                             */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Check the status of commands to logical drives                         */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_chkstatus(ips_ha_t *ha) {
+   ips_scb_t  *scb;
+   ips_stat_t *sp = &ha->sp;
+   u8          basic_status;
+   u8          ext_status;
+   int         command_id;
+   int         errcode;
+   int         ret;
+
+   DBG("ips_chkstatus");
+
+   command_id = ips_statupd(ha);
+
+   if (command_id > (MAX_CMDS-1)) {
+      printk(KERN_NOTICE "(%s%d) invalid command id received: %d\n",
+             ips_name, ha->host_num, command_id);
+
+      return (-1);
+   }
+
+   scb = &ha->scbs[command_id];
+   sp->scb_addr = (u32) scb;
+   sp->residue_len = 0;
+   scb->basic_status = basic_status = ha->adapt->p_status_tail->basic_status & BASIC_STATUS_MASK;
+   scb->extended_status = ext_status = ha->adapt->p_status_tail->extended_status;
+
+   /* Remove the item from the active queue */
+   ips_removeq_scb(&ha->scb_activelist, scb);
+
+   if (!scb->scsi_cmd)
+      /* internal commands are handled in do_ipsintr */
+      return (0);
+
+#ifndef NO_IPS_CMDLINE
+   if ((scb->scsi_cmd) && (ips_is_passthru(scb->scsi_cmd)))
+      /* passthru - just returns the raw result */
+      return (0);
+#endif
+
+   errcode = DID_OK;
+   ret = 0;
+
+   if (((basic_status & GSC_STATUS_MASK) == SSUCCESS) ||
+       ((basic_status & GSC_STATUS_MASK) == RECOVERED_ERROR)) {
+
+      if (scb->bus == 0) {
+#if IPS_DEBUG >= 1
+         if ((basic_status & GSC_STATUS_MASK) == RECOVERED_ERROR) {
+            printk(KERN_NOTICE "(%s%d) Recovered Logical Drive Error OpCode: %x, BSB: %x, ESB: %x\n",
+                   ips_name, ha->host_num,
+                   scb->cmd.basic_io.op_code, basic_status, ext_status);
+         }
+#endif
+
+         switch (scb->scsi_cmd->cmnd[0]) {
+         case ALLOW_MEDIUM_REMOVAL:
+         case REZERO_UNIT:
+         case ERASE:
+         case WRITE_FILEMARKS:
+         case SPACE:
+            errcode = DID_ERROR;
+            ret = 1;
+            break;
+
+         case START_STOP:
+            break;
+
+         case TEST_UNIT_READY:
+            if (!ips_online(ha, scb)) {
+               errcode = DID_TIME_OUT;
+               ret = 1;
+            }
+            break;
+
+         case INQUIRY:
+            if (ips_online(ha, scb)) {
+               ips_inquiry(ha, scb);
+            } else {
+               errcode = DID_TIME_OUT;
+               ret = 1;
+            }
+            break;
+
+         case REQUEST_SENSE:
+            ips_reqsen(ha, scb);
+            break;
+
+         case READ_6:
+         case WRITE_6:
+         case READ_10:
+         case WRITE_10:
+         case RESERVE:
+         case RELEASE:
+            break;
+
+         case MODE_SENSE:
+            if (!ips_online(ha, scb) || !ips_msense(ha, scb)) {
+               errcode = DID_ERROR;
+               ret = 1;
+            }
+            break;
+
+         case READ_CAPACITY:
+            if (ips_online(ha, scb))
+               ips_rdcap(ha, scb);
+            else {
+               errcode = DID_TIME_OUT;
+               ret = 1;
+            }
+            break;
+
+         case SEND_DIAGNOSTIC:
+         case REASSIGN_BLOCKS:
+            break;
+
+         case FORMAT_UNIT:
+            errcode = DID_ERROR;
+            ret = 1;
+            break;
+
+         case SEEK_10:
+         case VERIFY:
+         case READ_DEFECT_DATA:
+         case READ_BUFFER:
+         case WRITE_BUFFER:
+            break;
+
+         default:
+            errcode = DID_ERROR;
+            ret = 1;
+         } /* end switch */
+
+         scb->scsi_cmd->result = errcode << 16;
+      } else { /* bus == 0 */
+         /* restrict access to physical drives */
+         if ((scb->scsi_cmd->cmnd[0] == INQUIRY) &&
+             ((((char *) scb->scsi_cmd->buffer)[0] & 0x1f) == TYPE_DISK)) {
+
+            scb->scsi_cmd->result = DID_TIME_OUT << 16;
+
+            ret = 1;
+         }
+      } /* else */
+   } else { /* recovered error / success */
+#if IPS_DEBUG >= 1
+      if (scb->bus == 0) {
+         printk(KERN_NOTICE "(%s%d) Unrecovered Logical Drive Error OpCode: %x, BSB: %x, ESB: %x\n",
+                ips_name, ha->host_num,
+                scb->cmd.basic_io.op_code, basic_status, ext_status);
+      }
+#endif
+
+      ret = ips_map_status(scb, sp);
+   } /* else */
+
+   return (ret);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_online                                                 */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Determine if a logical drive is online                                 */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_online(ips_ha_t *ha, ips_scb_t *scb) {
+   DBG("ips_online");
+
+   if (scb->target_id >= MAX_LOGICAL_DRIVES)
+      return (0);
+
+   if ((scb->basic_status & GSC_STATUS_MASK) > 1) {
+      memset(&ha->adapt->logical_drive_info, 0, sizeof(ha->adapt->logical_drive_info));
+
+      return (0);
+   }
+
+   if (scb->target_id < ha->adapt->logical_drive_info.no_of_log_drive &&
+       ha->adapt->logical_drive_info.drive_info[scb->target_id].state != OFF_LINE &&
+       ha->adapt->logical_drive_info.drive_info[scb->target_id].state != FREE &&
+       ha->adapt->logical_drive_info.drive_info[scb->target_id].state != CRS &&
+       ha->adapt->logical_drive_info.drive_info[scb->target_id].state != SYS)
+      return (1);
+   else
+      return (0);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_inquiry                                                */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Simulate an inquiry command to a logical drive                         */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_inquiry(ips_ha_t *ha, ips_scb_t *scb) {
+   INQUIRYDATA inq;
+
+   DBG("ips_inquiry");
+
+   memset(&inq, 0, sizeof(INQUIRYDATA));
+
+   inq.DeviceType = TYPE_DISK;
+   inq.DeviceTypeQualifier = 0;
+   inq.RemoveableMedia = 0;
+   inq.Versions = 0x1;  /* SCSI I */
+   inq.AdditionalLength = 31;
+   strncpy(inq.VendorId, "IBM     ", 8);
+   strncpy(inq.ProductId, "SERVERAID       ", 16);
+   strncpy(inq.ProductRevisionLevel, "1.00", 4);
+
+   memcpy(scb->scsi_cmd->request_buffer, &inq, scb->scsi_cmd->request_bufflen);
+
+   return (1);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_rdcap                                                  */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Simulate a read capacity command to a logical drive                    */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_rdcap(ips_ha_t *ha, ips_scb_t *scb) {
+   CAPACITY_T *cap;
+
+   DBG("ips_rdcap");
+
+   if (scb->scsi_cmd->bufflen < 8)
+      return (0);
+
+   cap = (CAPACITY_T *) scb->scsi_cmd->request_buffer;
+
+   cap->lba = htonl(ha->adapt->logical_drive_info.drive_info[scb->target_id].sector_count - 1);
+   cap->len = htonl((u32) IPS_BLKSIZE);
+
+   return (1);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_msense                                                 */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Simulate a mode sense command to a logical drive                       */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_msense(ips_ha_t *ha, ips_scb_t *scb) {
+   u16          heads;
+   u16          sectors;
+   u32          cylinders;
+   ips_mdata_t  mdata;
+
+   DBG("ips_msense");
+
+   if (ha->enq->ulDriveSize[scb->target_id] > 0x400000 &&
+       (ha->enq->ucMiscFlag & 0x8) == 0) {
+      heads = NORM_MODE_HEADS;
+      sectors = NORM_MODE_SECTORS;
+   } else {
+      heads = COMP_MODE_HEADS;
+      sectors = COMP_MODE_SECTORS;
+   }
+
+   cylinders = ha->enq->ulDriveSize[scb->target_id] / (heads * sectors);
+
+   mdata.plh.plh_type = 0;
+   mdata.plh.plh_wp = 0;
+   mdata.plh.plh_bdl = 8;
+
+   switch (scb->scsi_cmd->cmnd[2] & 0x3f) {
+   case 0x03: /* page 3 */
+      mdata.pdata.pg3.pg_pc = 0x3;
+      mdata.pdata.pg3.pg_res1 = 0;
+      mdata.pdata.pg3.pg_len = sizeof(DADF_T);
+      mdata.plh.plh_len = 3 + mdata.plh.plh_bdl + mdata.pdata.pg3.pg_len;
+      mdata.pdata.pg3.pg_trk_z = 0;
+      mdata.pdata.pg3.pg_asec_z = 0;
+      mdata.pdata.pg3.pg_atrk_z = 0;
+      mdata.pdata.pg3.pg_atrk_v = 0;
+      mdata.pdata.pg3.pg_sec_t = htons(sectors);
+      mdata.pdata.pg3.pg_bytes_s = htons(IPS_BLKSIZE);
+      mdata.pdata.pg3.pg_intl = htons(1);
+      mdata.pdata.pg3.pg_trkskew = 0;
+      mdata.pdata.pg3.pg_cylskew = 0;
+      mdata.pdata.pg3.pg_res2 = 0;
+      mdata.pdata.pg3.pg_ins = 0;
+      mdata.pdata.pg3.pg_surf = 0;
+      mdata.pdata.pg3.pg_rmb = 0;
+      mdata.pdata.pg3.pg_hsec = 0;
+      mdata.pdata.pg3.pg_ssec = 1;
+      break;
+
+   case 0x4:
+      mdata.pdata.pg4.pg_pc = 0x4;
+      mdata.pdata.pg4.pg_res1 = 0;
+      mdata.pdata.pg4.pg_len = sizeof(RDDG_T);
+      mdata.plh.plh_len = 3 + mdata.plh.plh_bdl + mdata.pdata.pg4.pg_len;
+      mdata.pdata.pg4.pg_cylu = (cylinders >> 8) & 0xffff;
+      mdata.pdata.pg4.pg_cyll = cylinders & 0xff;
+      mdata.pdata.pg4.pg_head = heads;
+      mdata.pdata.pg4.pg_wrpcompu = 0;
+      mdata.pdata.pg4.pg_wrpcompl = 0;
+      mdata.pdata.pg4.pg_redwrcur = 0;
+      mdata.pdata.pg4.pg_drstep = htons(1);
+      mdata.pdata.pg4.pg_landu = 0;
+      mdata.pdata.pg4.pg_landl = 0;
+      mdata.pdata.pg4.pg_res2 = 0;
+      break;
+   default:
+      return (0);
+   } /* end switch */
+
+   memcpy(scb->scsi_cmd->request_buffer, &mdata, scb->scsi_cmd->request_bufflen);
+
+   return (1);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_reqsen                                                 */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Simulate a request sense command to a logical drive                    */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_reqsen(ips_ha_t *ha, ips_scb_t *scb) {
+   char *sp;
+
+   DBG("ips_reqsen");
+
+   sp = (char *) scb->scsi_cmd->sense_buffer;
+   memset(sp, 0, sizeof(scb->scsi_cmd->sense_buffer));
+
+   sp[0] = 0x70;
+   sp[3] = NO_SENSE;
+   sp[7] = 0xe;
+   sp[12] = NO_SENSE;
+
+   return (0);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_free                                                   */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Free any allocated space for this controller                           */
+/*                                                                          */
+/****************************************************************************/
+static void
+ips_free(ips_ha_t *ha) {
+   int i;
+
+   DBG("ips_free");
+
+   if (ha) {
+      if (ha->enq) {
+         kfree(ha->enq);
+         ha->enq = NULL;
+      }
+
+      if (ha->conf) {
+         kfree(ha->conf);
+         ha->conf = NULL;
+      }
+
+      if (ha->adapt) {
+         kfree(ha->adapt);
+         ha->adapt = NULL;
+      }
+
+      if (ha->nvram) {
+         kfree(ha->nvram);
+         ha->nvram = NULL;
+      }
+
+      if (ha->subsys) {
+         kfree(ha->subsys);
+         ha->subsys = NULL;
+      }
+
+      if (ha->dummy) {
+         kfree(ha->dummy);
+         ha->dummy = NULL;
+      }
+
+      if (ha->scbs) {
+         for (i = 0; i < ha->max_cmds; i++) {
+            if (ha->scbs[i].sg_list)
+               kfree(ha->scbs[i].sg_list);
+         }
+
+         kfree(ha->scbs);
+         ha->scbs = NULL;
+      } /* end if */
+   }
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_allocatescbs                                           */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Allocate the command blocks                                            */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_allocatescbs(ips_ha_t *ha) {
+   ips_scb_t *scb_p;
+   int        i;
+
+   DBG("ips_allocatescbs");
+
+   /* Allocate memory for the CCBs */
+   ha->scbs = (ips_scb_t *) kmalloc(ha->max_cmds * sizeof(ips_scb_t), GFP_KERNEL|GFP_DMA);
+
+   memset(ha->scbs, 0, ha->max_cmds * sizeof(ips_scb_t));
+
+   for (i = 0; i < ha->max_cmds; i++) {
+      scb_p = &ha->scbs[i];
+
+      /* allocate S/G list */
+      scb_p->sg_list = (SG_LIST *) kmalloc(sizeof(SG_LIST) * MAX_SG_ELEMENTS, GFP_KERNEL|GFP_DMA);
+
+      if (! scb_p->sg_list)
+         return (0);
+
+      /* add to the free list */
+      if (i < ha->max_cmds - 1) {
+         scb_p->q_next = ha->scb_freelist;
+         ha->scb_freelist = scb_p;
+      }
+   }
+
+   /* success */
+   return (1);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_init_scb                                               */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Initialize a CCB to default values                                     */
+/*                                                                          */
+/****************************************************************************/
+static void
+ips_init_scb(ips_ha_t *ha, ips_scb_t *scb) {
+   SG_LIST *sg_list;
+
+   DBG("ips_init_scb");
+
+   if (scb == NULL)
+      return ;
+
+   sg_list = scb->sg_list;
+
+   /* zero fill */
+   memset(scb, 0, sizeof(ips_scb_t));
+   memset(ha->dummy, 0, sizeof(BASIC_IO_CMD));
+
+   /* Initialize dummy command bucket */
+   ha->dummy->op_code = 0xFF;
+   ha->dummy->ccsar = VIRT_TO_BUS(ha->dummy);
+   ha->dummy->command_id = MAX_CMDS;
+
+   /* set bus address of scb */
+   scb->scb_busaddr = VIRT_TO_BUS(scb);
+   scb->sg_list = sg_list;
+
+   /* Neptune Fix */
+   scb->cmd.basic_io.cccr = ILE;
+   scb->cmd.basic_io.ccsar = VIRT_TO_BUS(ha->dummy);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_get_scb                                                */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Initialize a CCB to default values                                     */
+/*                                                                          */
+/* ASSUMED to be callled from within a lock                                 */
+/*                                                                          */
+/****************************************************************************/
+static ips_scb_t *
+ips_getscb(ips_ha_t *ha) {
+   ips_scb_t     *scb;
+   unsigned int   cpu_flags;
+
+   DBG("ips_getscb");
+
+   spin_lock_irqsave(&ha->scb_lock, cpu_flags);
+   if ((scb = ha->scb_freelist) == NULL) {
+      spin_unlock_irqrestore(&ha->scb_lock, cpu_flags);
+
+      return (NULL);
+   }
+
+   ha->scb_freelist = scb->q_next;
+   scb->q_next = NULL;
+
+   spin_unlock_irqrestore(&ha->scb_lock, cpu_flags);
+
+   ips_init_scb(ha, scb);
+
+   return (scb);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_free_scb                                               */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Return an unused CCB back to the free list                             */
+/*                                                                          */
+/* ASSUMED to be called from within a lock                                  */
+/*                                                                          */
+/****************************************************************************/
+static void
+ips_freescb(ips_ha_t *ha, ips_scb_t *scb) {
+   unsigned int cpu_flags;
+
+   DBG("ips_freescb");
+
+   /* check to make sure this is not our "special" scb */
+   if (IPS_COMMAND_ID(ha, scb) < (ha->max_cmds - 1)) {
+      spin_lock_irqsave(&ha->scb_lock, cpu_flags);
+      scb->q_next = ha->scb_freelist;
+      ha->scb_freelist = scb;
+      spin_unlock_irqrestore(&ha->scb_lock, cpu_flags);
+   }
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_reset_adapter                                          */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Reset the controller                                                   */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_reset_adapter(ips_ha_t *ha) {
+   u8  Isr;
+   u8  Cbsp;
+   u8  PostByte[MAX_POST_BYTES];
+   u8  ConfigByte[MAX_CONFIG_BYTES];
+   int i, j;
+   int reset_counter;
+
+   DBG("ips_reset_adapter");
+
+#if IPS_DEBUG >= 1
+   printk(KERN_WARNING "ips_reset_adapter: io addr: %x, irq: %d\n",
+          ha->io_addr, ha->irq);
+#endif
+
+   reset_counter = 0;
+
+   while (reset_counter < 2) {
+      reset_counter++;
+
+      outb(RST, ha->io_addr + SCPR);
+      MDELAY(ONE_SEC);
+      outb(0, ha->io_addr + SCPR);
+      MDELAY(ONE_SEC);
+
+      for (i = 0; i < MAX_POST_BYTES; i++) {
+         for (j = 0; j < 45; j++) {
+            Isr = inb(ha->io_addr + HISR);
+            if (Isr & GHI)
+               break;
+
+            MDELAY(ONE_SEC);
+         }
+
+         if (j >= 45) {
+            /* error occured */
+            if (reset_counter < 2)
+               continue;
+            else
+               /* reset failed */
+               return (0);
+         }
+
+         PostByte[i] = inb(ha->io_addr + ISPR);
+         outb(Isr, ha->io_addr + HISR);
+      }
+
+      if (PostByte[0] < GOOD_POST_BASIC_STATUS) {
+         printk("(%s%d) reset controller fails (post status %x %x).\n",
+                ips_name, ha->host_num, PostByte[0], PostByte[1]);
+
+         return (0);
+      }
+
+      for (i = 0; i < MAX_CONFIG_BYTES; i++) {
+         for (j = 0; j < 240; j++) {
+            Isr = inb(ha->io_addr + HISR);
+            if (Isr & GHI)
+               break;
+
+            MDELAY(ONE_SEC); /* 100 msec */
+         }
+
+         if (j >= 240) {
+            /* error occured */
+            if (reset_counter < 2)
+               continue;
+            else
+               /* reset failed */
+               return (0);
+         }
+
+         ConfigByte[i] = inb(ha->io_addr + ISPR);
+         outb(Isr, ha->io_addr + HISR);
+      }
+
+      if (ConfigByte[0] == 0 && ConfigByte[1] == 2) {
+         printk("(%s%d) reset controller fails (status %x %x).\n",
+                ips_name, ha->host_num, ConfigByte[0], ConfigByte[1]);
+
+         return (0);
+      }
+
+      for (i = 0; i < 240; i++) {
+         Cbsp = inb(ha->io_addr + CBSP);
+
+         if ((Cbsp & OP) == 0)
+            break;
+
+         MDELAY(ONE_SEC);
+      }
+
+      if (i >= 240) {
+         /* error occured */
+         if (reset_counter < 2)
+            continue;
+         else
+            /* reset failed */
+            return (0);
+      }
+
+      /* setup CCCR */
+      outw(0x1010, ha->io_addr + CCCR);
+
+      /* Enable busmastering */
+      outb(EBM, ha->io_addr + SCPR);
+
+      /* setup status queues */
+      ips_statinit(ha);
+
+      /* Enable interrupts */
+      outb(EI, ha->io_addr + HISR);
+
+      /* if we get here then everything went OK */
+      break;
+   }
+
+   return (1);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_statinit                                               */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Initialize the status queues on the controller                         */
+/*                                                                          */
+/****************************************************************************/
+static void
+ips_statinit(ips_ha_t *ha) {
+   u32        phys_status_start;
+
+   DBG("ips_statinit");
+
+   ha->adapt->p_status_start = ha->adapt->status;
+   ha->adapt->p_status_end = ha->adapt->status + MAX_CMDS;
+   ha->adapt->p_status_tail = ha->adapt->status;
+
+   phys_status_start = VIRT_TO_BUS(ha->adapt->status);
+   outl(phys_status_start, ha->io_addr + SQSR);
+   outl(phys_status_start + STATUS_Q_SIZE, ha->io_addr + SQER);
+   outl(phys_status_start + STATUS_SIZE, ha->io_addr + SQHR);
+   outl(phys_status_start, ha->io_addr + SQTR);
+
+   ha->adapt->hw_status_start = phys_status_start;
+   ha->adapt->hw_status_tail = phys_status_start;
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_statupd                                                */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Remove an element from the status queue                                */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_statupd(ips_ha_t *ha) {
+   int       command_id;
+
+   DBG("ips_statupd");
+
+   if (ha->adapt->p_status_tail != ha->adapt->p_status_end) {
+      ha->adapt->p_status_tail++;
+      ha->adapt->hw_status_tail += sizeof(STATUS);
+   } else {
+      ha->adapt->p_status_tail = ha->adapt->p_status_start;
+      ha->adapt->hw_status_tail = ha->adapt->hw_status_start;
+   }
+
+   outl(ha->adapt->hw_status_tail, ha->io_addr + SQTR);
+
+   command_id = ha->adapt->p_status_tail->command_id;
+
+   return (command_id);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_issue                                                  */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Send a command down to the controller                                  */
+/*                                                                          */
+/* ASSUMED to be called from within a lock                                  */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_issue(ips_ha_t *ha, ips_scb_t *scb) {
+   u32       TimeOut;
+   u16       val;
+
+   DBG("ips_issue");
+
+#if IPS_DEBUG >= 10
+   if (scb->scsi_cmd)
+      printk(KERN_NOTICE "%s: ips_issue: cmd 0x%X id %d (%d %d %d)\n",
+             ips_name,
+             scb->cdb[0],
+             scb->cmd.basic_io.command_id,
+             scb->bus,
+             scb->target_id,
+             scb->lun);
+   else
+      printk(KERN_NOTICE "%s: ips_issue: logical cmd id %d\n",
+             ips_name,
+             scb->cmd.basic_io.command_id);
+#if IPS_DEBUG >= 11
+      MDELAY(ONE_SEC);
+#endif
+#endif
+
+   TimeOut = 0;
+
+   while ((val = inw(ha->io_addr + CCCR)) & SEMAPHORE) {
+      UDELAY(1000);
+
+      if (++TimeOut >= SEMAPHORE_TIMEOUT) {
+         if (!(val & START_STOP_BIT))
+            break;
+
+         printk(KERN_WARNING "(%s%d) ips_issue val [0x%x].\n",
+                ips_name, ha->host_num, val);
+         printk(KERN_WARNING "(%s%d) ips_issue semaphore chk timeout.\n",
+                ips_name, ha->host_num);
+
+         return (IPS_FAILURE);
+      } /* end if */
+   } /* end while */
+
+   outl(scb->scb_busaddr, ha->io_addr + CCSAR);
+   outw(START_COMMAND, ha->io_addr + CCCR);
+
+   return (IPS_SUCCESS);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_isintr                                                 */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Test to see if an interrupt is for us                                  */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_isintr(ips_ha_t *ha) {
+   u8 Isr;
+
+   DBG("ips_isintr");
+
+   Isr = inb(ha->io_addr + HISR);
+
+   if (Isr == 0xFF)
+      /* ?!?! Nothing really there */
+      return (0);
+
+   if (Isr & SCE)
+      return (1);
+   else if (Isr & (SQO | GHI)) {
+      /* status queue overflow or GHI */
+      /* just clear the interrupt */
+      outb(Isr, ha->io_addr + HISR);
+   }
+
+   return (0);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_wait                                                   */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Wait for a command to complete                                         */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_wait(ips_ha_t *ha, int time, int intr) {
+   int        ret;
+
+   DBG("ips_wait");
+
+   ret = IPS_FAILURE;
+
+   time *= ONE_SEC; /* convert seconds to milliseconds */
+
+   while (time > 0) {
+      if (intr == IPS_INTR_OFF) {
+         if (ha->waitflag == FALSE) {
+            /*
+             * controller generated an interupt to
+             * acknowledge completion of the command
+             * and ips_intr() has serviced the interrupt.
+             */
+            ret = IPS_SUCCESS;
+            break;
+         }
+
+         /*
+          * NOTE: Interrupts are disabled here
+          * On an SMP system interrupts will only
+          * be disabled on one processor.
+          * So, ultimately we still need to set the
+          * "I'm in the interrupt handler flag"
+          */
+         while (test_and_set_bit(IPS_IN_INTR, &ha->flags))
+            UDELAY(1000);
+
+         ips_intr(ha);
+
+         clear_bit(IPS_IN_INTR, &ha->flags);
+
+      } else {
+         if (ha->waitflag == FALSE) {
+            ret = IPS_SUCCESS;
+            break;
+         }
+      }
+
+      UDELAY(1000); /* 1 milisecond */
+      time--;
+   }
+
+   return (ret);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_write_driver_status                                    */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Write OS/Driver version to Page 5 of the nvram on the controller       */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_write_driver_status(ips_ha_t *ha) {
+   DBG("ips_write_driver_status");
+
+   if (!ips_readwrite_page5(ha, FALSE)) {
+      printk(KERN_WARNING "(%s%d) unable to read NVRAM page 5.\n",
+             ips_name, ha->host_num);
+
+      return (0);
+   }
+
+   /* check to make sure the page has a valid */
+   /* signature */
+   if (ha->nvram->signature != NVRAM_PAGE5_SIGNATURE) {
+#if IPS_DEBUG >= 1
+      printk("(%s%d) NVRAM page 5 has an invalid signature: %X.\n",
+             ips_name, ha->host_num, ha->nvram->signature);
+#endif
+      return (1);
+   }
+
+#if IPS_DEBUG >= 2
+   printk("(%s%d) Ad Type: %d, Ad Slot: %d, BIOS: %c%c%c%c %c%c%c%c.\n",
+          ips_name, ha->host_num, ha->nvram->adapter_type, ha->nvram->adapter_slot,
+          ha->nvram->bios_high[0], ha->nvram->bios_high[1],
+          ha->nvram->bios_high[2], ha->nvram->bios_high[3],
+          ha->nvram->bios_low[0], ha->nvram->bios_low[1],
+          ha->nvram->bios_low[2], ha->nvram->bios_low[3]);
+#endif
+
+   /* save controller type */
+   ha->ad_type = ha->nvram->adapter_type;
+
+   /* change values (as needed) */
+   ha->nvram->operating_system = OS_LINUX;
+   strncpy((char *) ha->nvram->driver_high, IPS_VERSION_HIGH, 4);
+   strncpy((char *) ha->nvram->driver_low, IPS_VERSION_LOW, 4);
+
+   /* now update the page */
+   if (!ips_readwrite_page5(ha, TRUE)) {
+      printk(KERN_WARNING "(%s%d) unable to write NVRAM page 5.\n",
+             ips_name, ha->host_num);
+
+      return (0);
+   }
+
+   return (1);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_read_adapter_status                                    */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Do an Inquiry command to the adapter                                   */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_read_adapter_status(ips_ha_t *ha) {
+   ips_scb_t *scb;
+   int        ret;
+
+   DBG("ips_read_adapter_status");
+
+   scb = &ha->scbs[ha->max_cmds-1];
+
+   ips_init_scb(ha, scb);
+
+   scb->timeout = ips_cmd_timeout;
+   scb->cdb[0] = ENQUIRY;
+
+   scb->cmd.basic_io.op_code = ENQUIRY;
+   scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
+   scb->cmd.basic_io.sg_count = 0;
+   scb->cmd.basic_io.sg_addr = VIRT_TO_BUS(ha->enq);
+   scb->cmd.basic_io.lba = 0;
+   scb->cmd.basic_io.sector_count = 0;
+   scb->cmd.basic_io.log_drv = 0;
+   scb->cmd.basic_io.reserved = 0;
+
+   /* send command */
+   ret = ips_send_wait(ha, scb, ips_cmd_timeout);
+   if ((ret == IPS_FAILURE) || (ret == IPS_SUCCESS_IMM))
+      return (0);
+
+   return (1);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_read_subsystem_parameters                              */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Read subsystem parameters from the adapter                             */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_read_subsystem_parameters(ips_ha_t *ha) {
+   ips_scb_t *scb;
+   int        ret;
+
+   DBG("ips_read_subsystem_parameters");
+
+   scb = &ha->scbs[ha->max_cmds-1];
+
+   ips_init_scb(ha, scb);
+
+   scb->timeout = ips_cmd_timeout;
+   scb->cdb[0] = GET_SUBSYS_PARAM;
+
+   scb->cmd.basic_io.op_code = GET_SUBSYS_PARAM;
+   scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
+   scb->cmd.basic_io.sg_count = 0;
+   scb->cmd.basic_io.sg_addr = VIRT_TO_BUS(ha->subsys);
+   scb->cmd.basic_io.lba = 0;
+   scb->cmd.basic_io.sector_count = 0;
+   scb->cmd.basic_io.log_drv = 0;
+   scb->cmd.basic_io.reserved = 0;
+
+   /* send command */
+   ret = ips_send_wait(ha, scb, ips_cmd_timeout);
+   if ((ret == IPS_FAILURE) || (ret == IPS_SUCCESS_IMM))
+      return (0);
+
+   return (1);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_read_config                                            */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Read the configuration on the adapter                                  */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_read_config(ips_ha_t *ha) {
+   ips_scb_t *scb;
+   int        i;
+   int        ret;
+
+   DBG("ips_read_config");
+
+   /* set defaults for initiator IDs */
+   ha->conf->init_id[0] = IPS_ADAPTER_ID;
+   for (i = 1; i < 4; i++)
+      ha->conf->init_id[i] = 7;
+
+   scb = &ha->scbs[ha->max_cmds-1];
+
+   ips_init_scb(ha, scb);
+
+   scb->timeout = ips_cmd_timeout;
+   scb->cdb[0] = READ_NVRAM_CONFIGURATION;
+
+   scb->cmd.basic_io.op_code = READ_NVRAM_CONFIGURATION;
+   scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
+   scb->cmd.basic_io.sg_addr = VIRT_TO_BUS(ha->conf);
+
+   /* send command */
+   if (((ret = ips_send_wait(ha, scb, ips_cmd_timeout)) == IPS_FAILURE) ||
+       (ret == IPS_SUCCESS_IMM) ||
+       ((scb->basic_status & GSC_STATUS_MASK) > 1)) {
+
+      memset(ha->conf, 0, sizeof(CONFCMD));
+
+      /* reset initiator IDs */
+      ha->conf->init_id[0] = IPS_ADAPTER_ID;
+      for (i = 1; i < 4; i++)
+         ha->conf->init_id[i] = 7;
+
+      return (0);
+   }
+
+   return (1);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_readwrite_page5                                        */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Read the configuration on the adapter                                  */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_readwrite_page5(ips_ha_t *ha, int write) {
+   ips_scb_t *scb;
+   int        ret;
+
+   DBG("ips_readwrite_page5");
+
+   scb = &ha->scbs[ha->max_cmds-1];
+
+   ips_init_scb(ha, scb);
+
+   scb->timeout = ips_cmd_timeout;
+   scb->cdb[0] = RW_NVRAM_PAGE;
+
+   scb->cmd.nvram.op_code = RW_NVRAM_PAGE;
+   scb->cmd.nvram.command_id = IPS_COMMAND_ID(ha, scb);
+   scb->cmd.nvram.page = 5;
+   scb->cmd.nvram.write = write;
+   scb->cmd.nvram.buffer_addr = VIRT_TO_BUS(ha->nvram);
+   scb->cmd.nvram.reserved = 0;
+   scb->cmd.nvram.reserved2 = 0;
+
+   /* issue the command */
+   if (((ret = ips_send_wait(ha, scb, ips_cmd_timeout)) == IPS_FAILURE) ||
+       (ret == IPS_SUCCESS_IMM) ||
+       ((scb->basic_status & GSC_STATUS_MASK) > 1)) {
+
+      memset(ha->nvram, 0, sizeof(NVRAM_PAGE5));
+
+      return (0);
+   }
+
+   return (1);
+}
+
+/****************************************************************************/
+/*                                                                          */
+/* Routine Name: ips_clear_adapter                                          */
+/*                                                                          */
+/* Routine Description:                                                     */
+/*                                                                          */
+/*   Clear the stripe lock tables                                           */
+/*                                                                          */
+/****************************************************************************/
+static int
+ips_clear_adapter(ips_ha_t *ha) {
+   ips_scb_t *scb;
+   int        ret;
+
+   DBG("ips_clear_adapter");
+
+   scb = &ha->scbs[ha->max_cmds-1];
+
+   ips_init_scb(ha, scb);
+
+   scb->timeout = ips_reset_timeout;
+   scb->cdb[0] = CONFIG_SYNC;
+
+   scb->cmd.config_sync.op_code = CONFIG_SYNC;
+   scb->cmd.config_sync.command_id = IPS_COMMAND_ID(ha, scb);
+   scb->cmd.config_sync.channel = 0;
+   scb->cmd.config_sync.source_target = POCL;
+   scb->cmd.config_sync.reserved = 0;
+   scb->cmd.config_sync.reserved2 = 0;
+   scb->cmd.config_sync.reserved3 = 0;
+
+   /* issue command */
+   ret = ips_send_wait(ha, scb, ips_reset_timeout);
+   if ((ret == IPS_FAILURE) || (ret == IPS_SUCCESS_IMM))
+      return (0);
+
+   /* send unlock stripe command */
+   ips_init_scb(ha, scb);
+
+   scb->cdb[0] = GET_ERASE_ERROR_TABLE;
+   scb->timeout = ips_reset_timeout;
+
+   scb->cmd.unlock_stripe.op_code = GET_ERASE_ERROR_TABLE;
+   scb->cmd.unlock_stripe.command_id = IPS_COMMAND_ID(ha, scb);
+   scb->cmd.unlock_stripe.log_drv = 0;
+   scb->cmd.unlock_stripe.control = CSL;
+   scb->cmd.unlock_stripe.reserved = 0;
+   scb->cmd.unlock_stripe.reserved2 = 0;
+   scb->cmd.unlock_stripe.reserved3 = 0;
+
+   /* issue command */
+   ret = ips_send_wait(ha, scb, ips_reset_timeout);
+   if ((ret == IPS_FAILURE) || (ret == IPS_SUCCESS_IMM))
+      return (0);
+
+   return (1);
+}
+
+#if defined (MODULE)
+
+Scsi_Host_Template driver_template = IPS;
+
+   #include "scsi_module.c"
+
+#endif
+
+
+/*
+ * Overrides for Emacs so that we almost follow Linus's tabbing style.
+ * Emacs will notice this stuff at the end of the file and automatically
+ * adjust the settings for this buffer only.  This must remain at the end
+ * of the file.
+ * ---------------------------------------------------------------------------
+ * Local variables:
+ * c-indent-level: 2
+ * c-brace-imaginary-offset: 0
+ * c-brace-offset: -2
+ * c-argdecl-indent: 2
+ * c-label-offset: -2
+ * c-continued-statement-offset: 2
+ * c-continued-brace-offset: 0
+ * indent-tabs-mode: nil
+ * tab-width: 8
+ * End:
+ */
diff -urN linux-2.2.13-pre3/drivers/scsi/ips.h linux-2.2.13-pre4/drivers/scsi/ips.h
--- linux-2.2.13-pre3/drivers/scsi/ips.h	1969-12-31 16:00:00.000000000 -0800
+++ linux-2.2.13-pre4/drivers/scsi/ips.h	2003-08-15 15:07:26.000000000 -0700
@@ -0,0 +1,837 @@
+/*****************************************************************************/
+/* ips.h -- driver for the IBM ServeRAID adapter                             */
+/*                                                                           */
+/* Written By: Keith Mitchell, IBM Corporation                               */
+/*                                                                           */
+/* Copyright (C) 1999 IBM Corporation                                        */
+/*                                                                           */
+/* 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.                              */
+/*                                                                           */
+/* NO WARRANTY                                                               */
+/* THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR        */
+/* CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT      */
+/* LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,      */
+/* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is    */
+/* solely responsible for determining the appropriateness of using and       */
+/* distributing the Program and assumes all risks associated with its        */
+/* exercise of rights under this Agreement, including but not limited to     */
+/* the risks and costs of program errors, damage to or loss of data,         */
+/* programs or equipment, and unavailability or interruption of operations.  */
+/*                                                                           */
+/* DISCLAIMER OF LIABILITY                                                   */
+/* NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY   */
+/* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL        */
+/* DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND   */
+/* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR     */
+/* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    */
+/* USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED  */
+/* HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES             */
+/*                                                                           */
+/* 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
+/*                                                                           */
+/* Bugs/Comments/Suggestions should be mailed to:                            */
+/*      ipslinux@us.ibm.com                                                  */
+/*                                                                           */
+/*****************************************************************************/
+
+#ifndef _IPS_H_
+   #define _IPS_H_
+
+   #include <asm/uaccess.h>
+   #include <asm/io.h>
+
+   /* Prototypes */
+   extern int ips_detect(Scsi_Host_Template *);
+   extern int ips_release(struct Scsi_Host *);
+   extern int ips_abort(Scsi_Cmnd *);
+   extern int ips_reset(Scsi_Cmnd *, unsigned int);
+   extern int ips_eh_abort(Scsi_Cmnd *);
+   extern int ips_eh_reset(Scsi_Cmnd *);
+   extern int ips_queue(Scsi_Cmnd *, void (*) (Scsi_Cmnd *));
+   extern int ips_biosparam(Disk *, kdev_t, int *);
+   extern const char * ips_info(struct Scsi_Host *);
+   extern void do_ipsintr(int, void *, struct pt_regs *);
+
+   /*
+    * Some handy macros
+    */
+   #ifndef LinuxVersionCode
+      #define LinuxVersionCode(x,y,z)   (((x)<<16)+((y)<<8)+(z))
+   #endif
+
+   #define HA(x)                       ((ips_ha_t *) x->hostdata)
+   #define IPS_COMMAND_ID(ha, scb)     (int) (scb - ha->scbs)
+   #define VIRT_TO_BUS(x)              (unsigned int)virt_to_bus((void *) x)
+
+   #define UDELAY udelay
+   #define MDELAY mdelay
+
+   #define verify_area_20(t,a,sz)               (0) /* success */
+   #define PUT_USER                             put_user
+   #define __PUT_USER                           __put_user
+   #define PUT_USER_RET                         put_user_ret
+   #define GET_USER                             get_user
+   #define __GET_USER                           __get_user
+   #define GET_USER_RET                         get_user_ret
+
+/*
+ * Adapter address map equates
+ */
+   #define HISR                 0x08    /* Host Interrupt Status Reg   */
+   #define CCSAR                0x10    /* Cmd Channel System Addr Reg */
+   #define CCCR                 0x14    /* Cmd Channel Control Reg     */
+   #define SQHR                 0x20    /* Status Q Head Reg           */
+   #define SQTR                 0x24    /* Status Q Tail Reg           */
+   #define SQER                 0x28    /* Status Q End Reg            */
+   #define SQSR                 0x2C    /* Status Q Start Reg          */
+   #define SCPR                 0x05    /* Subsystem control port reg  */
+   #define ISPR                 0x06    /* interrupt status port reg   */
+   #define CBSP                 0x07    /* CBSP register               */
+
+/*
+ * Adapter register bit equates
+ */
+   #define GHI                  0x04    /* HISR General Host Interrupt */
+   #define SQO                  0x02    /* HISR Status Q Overflow      */
+   #define SCE                  0x01    /* HISR Status Channel Enqueue */
+   #define SEMAPHORE            0x08    /* CCCR Semaphore Bit          */
+   #define ILE                  0x10    /* CCCR ILE Bit                */
+   #define START_COMMAND        0x101A  /* CCCR Start Command Channel  */
+   #define START_STOP_BIT       0x0002  /* CCCR Start/Stop Bit         */
+   #define RST                  0x80    /* SCPR Reset Bit              */
+   #define EBM                  0x02    /* SCPR Enable Bus Master      */
+   #define EI                   0x80    /* HISR Enable Interrupts      */
+   #define OP                   0x01    /* OP bit in CBSP              */
+
+/*
+ * Adapter Command ID Equates
+ */
+   #define GET_LOGICAL_DRIVE_INFO               0x19
+   #define GET_SUBSYS_PARAM                     0x40
+   #define READ_NVRAM_CONFIGURATION             0x38
+   #define RW_NVRAM_PAGE                        0xBC
+   #define IPS_READ                             0x02
+   #define IPS_WRITE                            0x03
+   #define ENQUIRY                              0x05
+   #define FLUSH_CACHE                          0x0A
+   #define NORM_STATE                           0x00
+   #define READ_SCATTER_GATHER                  0x82
+   #define WRITE_SCATTER_GATHER                 0x83
+   #define DIRECT_CDB                           0x04
+   #define DIRECT_CDB_SCATTER_GATHER            0x84
+   #define CONFIG_SYNC                          0x58
+   #define POCL                                 0x30
+   #define GET_ERASE_ERROR_TABLE                0x17
+   #define RESET_CHANNEL                        0x1A
+   #define CSL                                  0xFF
+   #define ADAPT_RESET                          0xFF
+
+/*
+ * Adapter Equates
+ */
+   #define IPS_MAX_ADAPTERS                     16
+   #define IPS_MAX_IOCTL                        1
+   #define IPS_MAX_IOCTL_QUEUE                  8
+   #define IPS_MAX_QUEUE                        128
+   #define IPS_BLKSIZE                          512
+   #define MAX_SG_ELEMENTS                      17
+   #define MAX_LOGICAL_DRIVES                   8
+   #define MAX_CHANNELS                         3
+   #define MAX_TARGETS                          15
+   #define MAX_CHUNKS                           16
+   #define MAX_CMDS                             64
+   #define IPS_MAX_XFER                         0x10000
+   #define COMP_MODE_HEADS                      128
+   #define COMP_MODE_SECTORS                    32
+   #define NORM_MODE_HEADS                      254
+   #define NORM_MODE_SECTORS                    63
+   #define NVRAM_PAGE5_SIGNATURE                0xFFDDBB99
+   #define MAX_POST_BYTES                       0x02
+   #define MAX_CONFIG_BYTES                     0x02
+   #define GOOD_POST_BASIC_STATUS               0x80
+   #define SEMAPHORE_TIMEOUT                    2000
+   #define IPS_INTR_OFF                         0
+   #define IPS_INTR_ON                          1
+   #define IPS_ADAPTER_ID                       0xF
+   #define IPS_VENDORID                         0x1014
+   #define IPS_DEVICEID                         0x002E
+   #define TIMEOUT_10                           0x10
+   #define TIMEOUT_60                           0x20
+   #define TIMEOUT_20M                          0x30
+   #define STATUS_SIZE                          4
+   #define STATUS_Q_SIZE                        (MAX_CMDS+1) * STATUS_SIZE
+   #define ONE_MSEC                             1
+   #define ONE_SEC                              1000
+
+/*
+ * Adapter Basic Status Codes
+ */
+   #define BASIC_STATUS_MASK            0xFF
+   #define GSC_STATUS_MASK              0x0F
+   #define SSUCCESS                     0x00
+   #define RECOVERED_ERROR              0x01
+   #define IPS_CHECK_CONDITION          0x02
+   #define INVAL_OPCO                   0x03
+   #define INVAL_CMD_BLK                0x04
+   #define INVAL_PARM_BLK               0x05
+   #define IPS_BUSY                     0x08
+   #define ADAPT_HARDWARE_ERROR         0x09
+   #define ADAPT_FIRMWARE_ERROR         0x0A
+   #define CMD_CMPLT_WERROR             0x0C
+   #define LOG_DRV_ERROR                0x0D
+   #define CMD_TIMEOUT                  0x0E
+   #define PHYS_DRV_ERROR               0x0F
+
+/*
+ * Adapter Extended Status Equates
+ */
+   #define SELECTION_TIMEOUT                    0xF0
+   #define DATA_OVER_UNDER_RUN                  0xF2
+   #define EXT_HOST_RESET                       0xF7
+   #define EXT_DEVICE_RESET                     0xF8
+   #define EXT_RECOVERY                         0xFC
+   #define EXT_CHECK_CONDITION                  0xFF
+
+/*
+ * Operating System Defines
+ */
+   #define OS_WINDOWS_NT                        0x01
+   #define OS_NETWARE                           0x02
+   #define OS_OPENSERVER                        0x03
+   #define OS_UNIXWARE                          0x04
+   #define OS_SOLARIS                           0x05
+   #define OS_OS2                               0x06
+   #define OS_LINUX                             0x07
+   #define OS_FREEBSD                           0x08
+
+/*
+ * Adapter Command/Status Packet Definitions
+ */
+   #define IPS_SUCCESS                  0x01 /* Successfully completed       */
+   #define IPS_SUCCESS_IMM              0x02 /* Success - Immediately        */
+   #define IPS_FAILURE                  0x04 /* Completed with Error         */
+
+/*
+ * Logical Drive Equates
+ */
+   #define OFF_LINE             0x02
+   #define OKAY                 0x03
+   #define FREE                 0x00
+   #define SYS                  0x06
+   #define CRS                  0x24
+
+/*
+ * DCDB Table Equates
+ */
+   #define NO_DISCONNECT                0x00
+   #define DISCONNECT_ALLOWED           0x80
+   #define NO_AUTO_REQUEST_SENSE        0x40
+   #define DATA_IN                      0x01
+   #define DATA_OUT                     0x02
+   #define TRANSFER_64K                 0x08
+   #define NOTIMEOUT                    0x00
+   #define TIMEOUT10                    0x10
+   #define TIMEOUT60                    0x20
+   #define TIMEOUT20M                   0x30
+
+/*
+ * Host adapter Flags (bit numbers)
+ */
+   #define IPS_IN_INTR                  0
+   #define IPS_IN_ABORT                 1
+   #define IPS_IN_RESET                 2
+
+/*
+ * SCB Flags
+ */
+   #define SCB_ACTIVE                   0x00001
+   #define SCB_WAITING                  0x00002
+
+/*
+ * Passthru stuff
+ */
+   #define COPPUSRCMD                  (('C'<<8) | 65)
+   #define IPS_NUMCTRLS                (('C'<<8) | 68)
+   #define IPS_CTRLINFO                (('C'<<8) | 69)
+
+/*
+ * Scsi_Host Template
+ */
+ #define IPS {                            \
+    next : NULL,                          \
+    module : NULL,                        \
+    proc_dir : NULL,                      \
+    proc_info : NULL,                     \
+    name : NULL,                          \
+    detect : ips_detect,                  \
+    release : ips_release,                \
+    info : ips_info,                      \
+    command : NULL,                       \
+    queuecommand : ips_queue,             \
+    eh_strategy_handler : NULL,           \
+    eh_abort_handler : ips_eh_abort,      \
+    eh_device_reset_handler : NULL,       \
+    eh_bus_reset_handler : NULL,          \
+    eh_host_reset_handler : ips_eh_reset, \
+    abort : ips_abort,                    \
+    reset : ips_reset,                    \
+    slave_attach : NULL,                  \
+    bios_param : ips_biosparam,           \
+    can_queue : 0,                        \
+    this_id: -1,                          \
+    sg_tablesize : MAX_SG_ELEMENTS,       \
+    cmd_per_lun: 16,                      \
+    present : 0,                          \
+    unchecked_isa_dma : 0,                \
+    use_clustering : ENABLE_CLUSTERING,   \
+    use_new_eh_code : 1                   \
+ }
+
+/*
+ * IBM PCI Raid Command Formats
+ */
+typedef struct {
+   u8        op_code;
+   u8        command_id;
+   u8        log_drv;
+   u8        sg_count;
+   u32       lba;
+   u32       sg_addr;
+   u16       sector_count;
+   u16       reserved;
+   u32       ccsar;
+   u32       cccr;
+} BASIC_IO_CMD, *PBASIC_IO_CMD;
+
+typedef struct {
+   u8        op_code;
+   u8        command_id;
+   u16       reserved;
+   u32       reserved2;
+   u32       buffer_addr;
+   u32       reserved3;
+   u32       ccsar;
+   u32       cccr;
+} LOGICAL_INFO, *PLOGICAL_INFO;
+
+typedef struct {
+   u8        op_code;
+   u8        command_id;
+   u8        reserved;
+   u8        reserved2;
+   u32       reserved3;
+   u32       buffer_addr;
+   u32       reserved4;
+} IOCTL_INFO, *PIOCTL_INFO;
+
+typedef struct {
+   u8        op_code;
+   u8        command_id;
+   u16       reserved;
+   u32       reserved2;
+   u32       dcdb_address;
+   u32       reserved3;
+   u32       ccsar;
+   u32       cccr;
+} DCDB_CMD, *PDCDB_CMD;
+
+typedef struct {
+   u8        op_code;
+   u8        command_id;
+   u8        channel;
+   u8        source_target;
+   u32       reserved;
+   u32       reserved2;
+   u32       reserved3;
+   u32       ccsar;
+   u32       cccr;
+} CONFIG_SYNC_CMD, *PCONFIG_SYNC_CMD;
+
+typedef struct {
+   u8        op_code;
+   u8        command_id;
+   u8        log_drv;
+   u8        control;
+   u32       reserved;
+   u32       reserved2;
+   u32       reserved3;
+   u32       ccsar;
+   u32       cccr;
+} UNLOCK_STRIPE_CMD, *PUNLOCK_STRIPE_CMD;
+
+typedef struct {
+   u8        op_code;
+   u8        command_id;
+   u8        reserved;
+   u8        state;
+   u32       reserved2;
+   u32       reserved3;
+   u32       reserved4;
+   u32       ccsar;
+   u32       cccr;
+} FLUSH_CACHE_CMD, *PFLUSH_CACHE_CMD;
+
+typedef struct {
+   u8        op_code;
+   u8        command_id;
+   u8        reserved;
+   u8        desc;
+   u32       reserved2;
+   u32       buffer_addr;
+   u32       reserved3;
+   u32       ccsar;
+   u32       cccr;
+} STATUS_CMD, *PSTATUS_CMD;
+
+typedef struct {
+   u8        op_code;
+   u8        command_id;
+   u8        page;
+   u8        write;
+   u32       reserved;
+   u32       buffer_addr;
+   u32       reserved2;
+   u32       ccsar;
+   u32       cccr;
+} NVRAM_CMD, *PNVRAM_CMD;
+
+typedef union {
+   BASIC_IO_CMD      basic_io;
+   LOGICAL_INFO      logical_info;
+   IOCTL_INFO        ioctl_info;
+   DCDB_CMD          dcdb;
+   CONFIG_SYNC_CMD   config_sync;
+   UNLOCK_STRIPE_CMD unlock_stripe;
+   FLUSH_CACHE_CMD   flush_cache;
+   STATUS_CMD        status;
+   NVRAM_CMD         nvram;
+} HOST_COMMAND, *PHOST_COMMAND;
+
+typedef struct {
+   u8         logical_id;
+   u8         reserved;
+   u8         raid_level;
+   u8         state;
+   u32        sector_count;
+} DRIVE_INFO, *PDRIVE_INFO;
+
+typedef struct {
+   u8         no_of_log_drive;
+   u8         reserved[3];
+   DRIVE_INFO drive_info[MAX_LOGICAL_DRIVES];
+} LOGICAL_DRIVE_INFO, *PLOGICAL_DRIVE_INFO;
+
+typedef struct {
+   u8        ha_num;
+   u8        bus_num;
+   u8        id;
+   u8        device_type;
+   u32       data_len;
+   u32       data_ptr;
+   u8        scsi_cdb[12];
+   u32       data_counter;
+   u32       block_size;
+} NON_DISK_DEVICE_INFO, *PNON_DISK_DEVICE_INFO;
+
+typedef struct {
+   u8         device_address;
+   u8         cmd_attribute;
+   u16        transfer_length;
+   u32        buffer_pointer;
+   u8         cdb_length;
+   u8         sense_length;
+   u8         sg_count;
+   u8         reserved;
+   u8         scsi_cdb[12];
+   u8         sense_info[64];
+   u8         scsi_status;
+   u8         reserved2[3];
+} DCDB_TABLE, *PDCDB_TABLE;
+
+typedef struct {
+   volatile u8      reserved;
+   volatile u8      command_id;
+   volatile u8      basic_status;
+   volatile u8      extended_status;
+} STATUS, *PSTATUS;
+
+typedef struct {
+   STATUS               status[MAX_CMDS + 1];
+   volatile PSTATUS     p_status_start;
+   volatile PSTATUS     p_status_end;
+   volatile PSTATUS     p_status_tail;
+   volatile u32         hw_status_start;
+   volatile u32         hw_status_tail;
+   LOGICAL_DRIVE_INFO   logical_drive_info;
+} ADAPTER_AREA, *PADAPTER_AREA;
+
+typedef struct {
+   u8        ucLogDriveCount;
+   u8        ucMiscFlag;
+   u8        ucSLTFlag;
+   u8        ucBSTFlag;
+   u8        ucPwrChgCnt;
+   u8        ucWrongAdrCnt;
+   u8        ucUnidentCnt;
+   u8        ucNVramDevChgCnt;
+   u8        CodeBlkVersion[8];
+   u8        BootBlkVersion[8];
+   u32       ulDriveSize[MAX_LOGICAL_DRIVES];
+   u8        ucConcurrentCmdCount;
+   u8        ucMaxPhysicalDevices;
+   u16       usFlashRepgmCount;
+   u8        ucDefunctDiskCount;
+   u8        ucRebuildFlag;
+   u8        ucOfflineLogDrvCount;
+   u8        ucCriticalDrvCount;
+   u16       usConfigUpdateCount;
+   u8        ucBlkFlag;
+   u8        reserved;
+   u16       usAddrDeadDisk[MAX_CHANNELS * MAX_TARGETS];
+} ENQCMD, *PENQCMD;
+
+typedef struct {
+   u8        ucInitiator;
+   u8        ucParameters;
+   u8        ucMiscFlag;
+   u8        ucState;
+   u32       ulBlockCount;
+   u8        ucDeviceId[28];
+} DEVSTATE, *PDEVSTATE;
+
+typedef struct {
+   u8        ucChn;
+   u8        ucTgt;
+   u16       ucReserved;
+   u32       ulStartSect;
+   u32       ulNoOfSects;
+} CHUNK, *PCHUNK;
+
+typedef struct {
+   u16       ucUserField;
+   u8        ucState;
+   u8        ucRaidCacheParam;
+   u8        ucNoOfChunkUnits;
+   u8        ucStripeSize;
+   u8        ucParams;
+   u8        ucReserved;
+   u32       ulLogDrvSize;
+   CHUNK     chunk[MAX_CHUNKS];
+} LOGICAL_DRIVE, *PLOGICAL_DRIVE;
+
+typedef struct {
+   u8        board_disc[8];
+   u8        processor[8];
+   u8        ucNoChanType;
+   u8        ucNoHostIntType;
+   u8        ucCompression;
+   u8        ucNvramType;
+   u32       ulNvramSize;
+} HARDWARE_DISC, *PHARDWARE_DISC;
+
+typedef struct {
+   u8             ucLogDriveCount;
+   u8             ucDateD;
+   u8             ucDateM;
+   u8             ucDateY;
+   u8             init_id[4];
+   u8             host_id[12];
+   u8             time_sign[8];
+
+   struct {
+      u32         usCfgDrvUpdateCnt:16;
+      u32         ConcurDrvStartCnt:4;
+      u32         StartupDelay:4;
+      u32         auto_rearrange:1;
+      u32         cd_boot:1;
+      u32         cluster:1;
+      u32         reserved:5;
+   } UserOpt;
+
+   u16            user_field;
+   u8             ucRebuildRate;
+   u8             ucReserve;
+   HARDWARE_DISC  hardware_disc;
+   LOGICAL_DRIVE  logical_drive[MAX_LOGICAL_DRIVES];
+   DEVSTATE       dev[MAX_CHANNELS][MAX_TARGETS+1];
+   u8             reserved[512];
+
+} CONFCMD, *PCONFCMD;
+
+typedef struct {
+   u32        signature;
+   u8         reserved;
+   u8         adapter_slot;
+   u16        adapter_type;
+   u8         bios_high[4];
+   u8         bios_low[4];
+   u16        reserved2;
+   u8         reserved3;
+   u8         operating_system;
+   u8         driver_high[4];
+   u8         driver_low[4];
+   u8         reserved4[100];
+} NVRAM_PAGE5, *PNVRAM_PAGE5;
+
+typedef struct _SUBSYS_PARAM {
+   u32        param[128];
+} SUBSYS_PARAM, *PSUBSYS_PARAM;
+
+/*
+ * Inquiry Data Format
+ */
+typedef struct {
+   u8        DeviceType:5;
+   u8        DeviceTypeQualifier:3;
+   u8        DeviceTypeModifier:7;
+   u8        RemoveableMedia:1;
+   u8        Versions;
+   u8        ResponseDataFormat;
+   u8        AdditionalLength;
+   u16       Reserved;
+   u8        SoftReset:1;
+   u8        CommandQueue:1;
+   u8        Reserved2:1;
+   u8        LinkedCommands:1;
+   u8        Synchronous:1;
+   u8        Wide16Bit:1;
+   u8        Wide32Bit:1;
+   u8        RelativeAddressing:1;
+   u8        VendorId[8];
+   u8        ProductId[16];
+   u8        ProductRevisionLevel[4];
+   u8        VendorSpecific[20];
+   u8        Reserved3[40];
+} INQUIRYDATA, *PINQUIRYDATA;
+
+/*
+ * Read Capacity Data Format
+ */
+typedef struct {
+   u32       lba;
+   u32       len;
+} CAPACITY_T;
+
+/*
+ * Sense Data Format
+ */
+typedef struct {
+   u8        pg_pc:6;       /* Page Code                    */
+   u8        pg_res1:2;     /* Reserved                     */
+   u8        pg_len;        /* Page Length                  */
+   u16       pg_trk_z;      /* Tracks per zone              */
+   u16       pg_asec_z;     /* Alternate sectors per zone   */
+   u16       pg_atrk_z;     /* Alternate tracks per zone    */
+   u16       pg_atrk_v;     /* Alternate tracks per volume  */
+   u16       pg_sec_t;      /* Sectors per track            */
+   u16       pg_bytes_s;    /* Bytes per physical sectors   */
+   u16       pg_intl;       /* Interleave                   */
+   u16       pg_trkskew;    /* Track skew factor            */
+   u16       pg_cylskew;    /* Cylinder Skew Factor         */
+   u32       pg_res2:27;    /* Reserved                     */
+   u32       pg_ins:1;      /* Inhibit Slave                */
+   u32       pg_surf:1;     /* Allocate Surface Sectors     */
+   u32       pg_rmb:1;      /* Removeable                   */
+   u32       pg_hsec:1;     /* Hard sector formatting       */
+   u32       pg_ssec:1;     /* Soft sector formatting       */
+} DADF_T;
+
+typedef struct {
+   u8        pg_pc:6;        /* Page Code                     */
+   u8        pg_res1:2;      /* Reserved                      */
+   u8        pg_len;         /* Page Length                   */
+   u16       pg_cylu;        /* Number of cylinders (upper)   */
+   u8        pg_cyll;        /* Number of cylinders (lower)   */
+   u8        pg_head;        /* Number of heads               */
+   u16       pg_wrpcompu;    /* Write precomp (upper)         */
+   u32       pg_wrpcompl:8;  /* Write precomp (lower)         */
+   u32       pg_redwrcur:24; /* Reduced write current         */
+   u32       pg_drstep:16;   /* Drive step rate               */
+   u32       pg_landu:16;    /* Landing zone cylinder (upper) */
+   u32       pg_landl:8;     /* Landing zone cylinder (lower) */
+   u32       pg_res2:24;     /* Reserved                      */
+} RDDG_T;
+
+struct blk_desc {
+   u8       bd_dencode;
+   u8       bd_nblks1;
+   u8       bd_nblks2;
+   u8       bd_nblks3;
+   u8       bd_res;
+   u8       bd_blen1;
+   u8       bd_blen2;
+   u8       bd_blen3;
+};
+
+typedef struct {
+   u8       plh_len;   /* Data length             */
+   u8       plh_type;  /* Medium type             */
+   u8       plh_res:7; /* Reserved                */
+   u8       plh_wp:1;  /* Write protect           */
+   u8       plh_bdl;   /* Block descriptor length */
+} SENSE_PLH_T;
+
+typedef struct {
+   SENSE_PLH_T     plh;
+   struct blk_desc blk_desc;
+
+   union {
+      DADF_T        pg3;
+      RDDG_T        pg4;
+   } pdata;
+} ips_mdata_t;
+
+/*
+ * Scatter Gather list format
+ */
+typedef struct ips_sglist {
+   u32       address;
+   u32       length;
+} SG_LIST, *PSG_LIST;
+
+typedef struct _INFOSTR {
+   char *buffer;
+   int   length;
+   int   offset;
+   int   pos;
+} INFOSTR;
+
+/*
+ * Status Info
+ */
+typedef struct ips_stat {
+   u32       residue_len;
+   u32       scb_addr;
+} ips_stat_t;
+
+/*
+ * SCB Queue Format
+ */
+typedef struct ips_scb_queue {
+   struct ips_scb *head;
+   struct ips_scb *tail;
+   unsigned int    count;
+} ips_scb_queue_t;
+
+/*
+ * Wait queue_format
+ */
+typedef struct ips_wait_queue {
+   Scsi_Cmnd      *head;
+   Scsi_Cmnd      *tail;
+   unsigned int    count;
+} ips_wait_queue_t;
+
+typedef struct ips_ha {
+   u8                 ha_id[MAX_CHANNELS+1];
+   u32                io_addr;            /* Base I/O address           */
+   u8                 irq;                /* IRQ for adapter            */
+   u8                 ntargets;           /* Number of targets          */
+   u8                 nbus;               /* Number of buses            */
+   u8                 nlun;               /* Number of Luns             */
+   u16                ad_type;            /* Adapter type               */
+   u16                host_num;           /* Adapter number             */
+   u32                max_xfer;           /* Maximum Xfer size          */
+   u32                max_cmds;           /* Max concurrent commands    */
+   u32                num_ioctl;          /* Number of Ioctls           */
+   ips_stat_t         sp;                 /* Status packer pointer      */
+   struct ips_scb    *scbs;               /* Array of all CCBS          */
+   struct ips_scb    *scb_freelist;       /* SCB free list              */
+   ips_wait_queue_t   scb_waitlist;       /* Pending SCB list           */
+   ips_wait_queue_t   copp_waitlist;      /* Pending PT list            */
+   ips_scb_queue_t    scb_activelist;     /* Active SCB list            */
+   BASIC_IO_CMD      *dummy;              /* dummy command              */
+   ADAPTER_AREA      *adapt;              /* Adapter status area        */
+   ENQCMD            *enq;                /* Adapter Enquiry data       */
+   CONFCMD           *conf;               /* Adapter config data        */
+   NVRAM_PAGE5       *nvram;              /* NVRAM page 5 data          */
+   SUBSYS_PARAM      *subsys;             /* Subsystem parameters       */
+   u32                cmd_in_progress;    /* Current command in progress*/
+   u32                flags;              /* HA flags                   */
+   u8                 waitflag;           /* are we waiting for cmd     */
+   u8                 active;
+   u32                reserved:16;        /* reserved space             */
+   struct wait_queue *copp_queue;         /* passthru sync queue        */
+
+   #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,0)
+   spinlock_t         scb_lock;
+   spinlock_t         copp_lock;
+   #endif
+} ips_ha_t;
+
+typedef void (*scb_callback) (ips_ha_t *, struct ips_scb *);
+
+/*
+ * SCB Format
+ */
+typedef struct ips_scb {
+   HOST_COMMAND      cmd;
+   DCDB_TABLE        dcdb;
+   u8                target_id;
+   u8                bus;
+   u8                lun;
+   u8                cdb[12];
+   u32               scb_busaddr;
+   u32               data_busaddr;
+   u32               timeout;
+   u8                basic_status;
+   u8                extended_status;
+   u16               breakup;
+   u32               data_len;
+   u32               sg_len;
+   u32               flags;
+   u32               op_code;
+   SG_LIST          *sg_list;
+   Scsi_Cmnd        *scsi_cmd;
+   struct ips_scb   *q_next;
+   scb_callback      callback;
+} ips_scb_t;
+
+/*
+ * Passthru Command Format
+ */
+typedef struct {
+   u8         CoppID[4];
+   u32        CoppCmd;
+   u32        PtBuffer;
+   u8        *CmdBuffer;
+   u32        CmdBSize;
+   ips_scb_t  CoppCP;
+   u32        TimeOut;
+   u8         BasicStatus;
+   u8         ExtendedStatus;
+   u16        reserved;
+} ips_passthru_t;
+
+#endif
+
+
+
+/*
+ * Overrides for Emacs so that we almost follow Linus's tabbing style.
+ * Emacs will notice this stuff at the end of the file and automatically
+ * adjust the settings for this buffer only.  This must remain at the end
+ * of the file.
+ * ---------------------------------------------------------------------------
+ * Local variables:
+ * c-indent-level: 2
+ * c-brace-imaginary-offset: 0
+ * c-brace-offset: -2
+ * c-argdecl-indent: 2
+ * c-label-offset: -2
+ * c-continued-statement-offset: 2
+ * c-continued-brace-offset: 0
+ * indent-tabs-mode: nil
+ * tab-width: 8
+ * End:
+ */
diff -urN linux-2.2.13-pre3/drivers/scsi/Makefile linux-2.2.13-pre4/drivers/scsi/Makefile
--- linux-2.2.13-pre3/drivers/scsi/Makefile	1999-04-29 11:53:41.000000000 -0700
+++ linux-2.2.13-pre4/drivers/scsi/Makefile	2003-08-15 15:07:26.000000000 -0700
@@ -346,6 +346,14 @@
   endif
 endif
 
+ifeq ($(CONFIG_SCSI_IPS),y)
+L_OBJS += ips.o
+else
+  ifeq ($(CONFIG_SCSI_IPS),m)
+  M_OBJS += ips.o
+  endif
+endif
+
 ifeq ($(CONFIG_SCSI_DC390T),y)
 L_OBJS += tmscsim.o
 else
diff -urN linux-2.2.13-pre3/drivers/sound/cmpci.c linux-2.2.13-pre4/drivers/sound/cmpci.c
--- linux-2.2.13-pre3/drivers/sound/cmpci.c	1969-12-31 16:00:00.000000000 -0800
+++ linux-2.2.13-pre4/drivers/sound/cmpci.c	2003-08-15 15:07:26.000000000 -0700
@@ -0,0 +1,2392 @@
+/*****************************************************************************/
+
+/*
+ *      cmpci.c  --  C-Media PCI audio driver.
+ *
+ *      Copyright (C) 1999  ChenLi Tien (cltien@home.com)
+ *
+ *	Based on the PCI drivers by Thomas Sailer (sailer@ife.ee.ethz.ch)
+ *
+ *      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.
+ *
+ * Special thanks to David C. Niemi
+ *
+ *
+ * Module command line parameters:
+ *   none so far
+ *
+ *
+ *  Supported devices:
+ *  /dev/dsp    standard /dev/dsp device, (mostly) OSS compatible
+ *  /dev/mixer  standard /dev/mixer device, (mostly) OSS compatible
+ *  /dev/midi   simple MIDI UART interface, no ioctl
+ *
+ *  The card has both an FM and a Wavetable synth, but I have to figure
+ *  out first how to drive them...
+ *
+ *  Revision history
+ *    06.05.98   0.1   Initial release
+ *    10.05.98   0.2   Fixed many bugs, esp. ADC rate calculation
+ *                     First stab at a simple midi interface (no bells&whistles)
+ *    13.05.98   0.3   Fix stupid cut&paste error: set_adc_rate was called instead of
+ *                     set_dac_rate in the FMODE_WRITE case in cm_open
+ *                     Fix hwptr out of bounds (now mpg123 works)
+ *    14.05.98   0.4   Don't allow excessive interrupt rates
+ *    08.06.98   0.5   First release using Alan Cox' soundcore instead of miscdevice
+ *    03.08.98   0.6   Do not include modversions.h
+ *                     Now mixer behaviour can basically be selected between
+ *                     "OSS documented" and "OSS actual" behaviour
+ *    31.08.98   0.7   Fix realplayer problems - dac.count issues
+ *    10.12.98   0.8   Fix drain_dac trying to wait on not yet initialized DMA
+ *    16.12.98   0.9   Fix a few f_file & FMODE_ bugs
+ *    06.01.99   0.10  remove the silly SA_INTERRUPT flag.
+ *                     hopefully killed the egcs section type conflict
+ *    12.03.99   0.11  cinfo.blocks should be reset after GETxPTR ioctl.
+ *                     reported by Johan Maes <joma@telindus.be>
+ *    22.03.99   0.12  return EAGAIN instead of EBUSY when O_NONBLOCK
+ *                     read/write cannot be executed
+ *
+ */
+
+/*****************************************************************************/
+      
+#include <linux/config.h>
+#include <linux/version.h>
+#include <linux/module.h>
+#include <linux/string.h>
+#include <linux/ioport.h>
+#include <linux/sched.h>
+#include <linux/delay.h>
+#include <linux/sound.h>
+#include <linux/malloc.h>
+#include <linux/soundcard.h>
+#include <linux/pci.h>
+#include <asm/io.h>
+#include <asm/dma.h>
+#include <linux/init.h>
+#include <linux/poll.h>
+#include <asm/spinlock.h>
+#include <asm/uaccess.h>
+#include <asm/hardirq.h>
+
+#include "dm.h"
+
+/* --------------------------------------------------------------------- */
+
+#undef OSS_DOCUMENTED_MIXER_SEMANTICS
+
+/* --------------------------------------------------------------------- */
+
+#ifndef PCI_VENDOR_ID_CMEDIA
+#define PCI_VENDOR_ID_CMEDIA         0x13F6
+#endif
+#ifndef PCI_DEVICE_ID_CMEDIA_CM8338A
+#define PCI_DEVICE_ID_CMEDIA_CM8338A 0x0100
+#endif
+#ifndef PCI_DEVICE_ID_CMEDIA_CM8338B
+#define PCI_DEVICE_ID_CMEDIA_CM8338B 0x0101
+#endif
+#ifndef PCI_DEVICE_ID_CMEDIA_CM8738
+#define PCI_DEVICE_ID_CMEDIA_CM8738  0x0111
+#endif
+
+#define CM_MAGIC  ((PCI_VENDOR_ID_CMEDIA<<16)|PCI_DEVICE_ID_CMEDIA_CM8338A)
+
+/*
+ * CM8338 registers definition
+ */
+
+#define CODEC_CMI_FUNCTRL0      (0x00)
+#define CODEC_CMI_FUNCTRL1      (0x04)
+#define CODEC_CMI_CHFORMAT      (0x08)
+#define CODEC_CMI_INT_HLDCLR    (0x0C)
+#define CODEC_CMI_INT_STATUS    (0x10)
+#define CODEC_CMI_LEGACY_CTRL   (0x14)
+#define CODEC_CMI_MISC_CTRL     (0x18)
+#define CODEC_CMI_TDMA_POS      (0x1C)
+#define CODEC_CMI_MIXER         (0x20)
+#define CODEC_SB16_DATA         (0x22)
+#define CODEC_SB16_ADDR         (0x23)
+#define CODEC_CMI_MIXER1        (0x24)
+#define CODEC_CMI_MIXER2        (0x25)
+#define CODEC_CMI_AUX_VOL       (0x26)
+#define CODEC_CMI_MISC          (0x27)
+#define CODEC_CMI_AC97          (0x28)
+
+#define CODEC_CMI_CH0_FRAME1    (0x80)
+#define CODEC_CMI_CH0_FRAME2    (0x84)
+#define CODEC_CMI_CH1_FRAME1    (0x88)
+#define CODEC_CMI_CH1_FRAME2    (0x8C)
+
+#define CODEC_CMI_EXT_REG       (0xF0)
+#define UCHAR	unsigned char
+/*
+**  Mixer registers for SB16
+*/
+
+#define DSP_MIX_DATARESETIDX    ((UCHAR)(0x00))
+
+#define DSP_MIX_MASTERVOLIDX_L  ((UCHAR)(0x30))
+#define DSP_MIX_MASTERVOLIDX_R  ((UCHAR)(0x31))
+#define DSP_MIX_VOICEVOLIDX_L   ((UCHAR)(0x32))
+#define DSP_MIX_VOICEVOLIDX_R   ((UCHAR)(0x33))
+#define DSP_MIX_FMVOLIDX_L      ((UCHAR)(0x34))
+#define DSP_MIX_FMVOLIDX_R      ((UCHAR)(0x35))
+#define DSP_MIX_CDVOLIDX_L      ((UCHAR)(0x36))
+#define DSP_MIX_CDVOLIDX_R      ((UCHAR)(0x37))
+#define DSP_MIX_LINEVOLIDX_L    ((UCHAR)(0x38))
+#define DSP_MIX_LINEVOLIDX_R    ((UCHAR)(0x39))
+
+#define DSP_MIX_MICVOLIDX       ((UCHAR)(0x3A))
+#define DSP_MIX_SPKRVOLIDX      ((UCHAR)(0x3B))
+
+#define DSP_MIX_OUTMIXIDX       ((UCHAR)(0x3C))
+
+#define DSP_MIX_ADCMIXIDX_L     ((UCHAR)(0x3D))
+#define DSP_MIX_ADCMIXIDX_R     ((UCHAR)(0x3E))
+
+#define DSP_MIX_INGAINIDX_L     ((UCHAR)(0x3F))
+#define DSP_MIX_INGAINIDX_R     ((UCHAR)(0x40))
+#define DSP_MIX_OUTGAINIDX_L    ((UCHAR)(0x41))
+#define DSP_MIX_OUTGAINIDX_R    ((UCHAR)(0x42))
+
+#define DSP_MIX_AGCIDX          ((UCHAR)(0x43))
+
+#define DSP_MIX_TREBLEIDX_L     ((UCHAR)(0x44))
+#define DSP_MIX_TREBLEIDX_R     ((UCHAR)(0x45))
+#define DSP_MIX_BASSIDX_L       ((UCHAR)(0x46))
+#define DSP_MIX_BASSIDX_R       ((UCHAR)(0x47))
+#define CM_CH0_RESET	  0x04
+#define CM_CH1_RESET	  0x08
+#define CM_EXTENT_CODEC	  0x100
+#define CM_EXTENT_MIDI	  0x2
+#define CM_EXTENT_SYNTH	  0x4
+#define CM_INT_CH0	  1
+#define CM_INT_CH1	  2
+
+#define CM_CFMT_STEREO     0x01
+#define CM_CFMT_16BIT      0x02
+#define CM_CFMT_MASK       0x03
+#define CM_CFMT_DACSHIFT   0   
+#define CM_CFMT_ADCSHIFT   2
+
+static const unsigned sample_size[] = { 1, 2, 2, 4 };
+static const unsigned sample_shift[] = { 0, 1, 1, 2 };
+
+#define CM_CENABLE_RE      0x2
+#define CM_CENABLE_PE      0x1
+
+
+/* MIDI buffer sizes */
+
+#define MIDIINBUF  256
+#define MIDIOUTBUF 256
+
+#define FMODE_MIDI_SHIFT 2
+#define FMODE_MIDI_READ  (FMODE_READ << FMODE_MIDI_SHIFT)
+#define FMODE_MIDI_WRITE (FMODE_WRITE << FMODE_MIDI_SHIFT)
+
+#define FMODE_DMFM 0x10
+
+#define SND_DEV_DSP16   5 
+
+/* --------------------------------------------------------------------- */
+
+struct cm_state {
+	/* magic */
+	unsigned int magic;
+
+	/* we keep cm cards in a linked list */
+	struct cm_state *next;
+
+	/* soundcore stuff */
+	int dev_audio;
+	int dev_mixer;
+	int dev_midi;
+	int dev_dmfm;
+
+	/* hardware resources */
+	unsigned int iosb, iobase, iosynth, iomidi, iogame, irq;
+
+        /* mixer stuff */
+        struct {
+                unsigned int modcnt;
+#ifndef OSS_DOCUMENTED_MIXER_SEMANTICS
+		unsigned short vol[13];
+#endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
+        } mix;
+
+	/* wave stuff */
+	unsigned int rateadc, ratedac;
+	unsigned char fmt, enable;
+
+	spinlock_t lock;
+	struct semaphore open_sem;
+	mode_t open_mode;
+	struct wait_queue *open_wait;
+
+	struct dmabuf {
+		void *rawbuf;
+		unsigned buforder;
+		unsigned numfrag;
+		unsigned fragshift;
+		unsigned hwptr, swptr;
+		unsigned total_bytes;
+		int count;
+		unsigned error; /* over/underrun */
+		struct wait_queue *wait;
+		/* redundant, but makes calculations easier */
+		unsigned fragsize;
+		unsigned dmasize;
+		unsigned fragsamples;
+		/* OSS stuff */
+		unsigned mapped:1;
+		unsigned ready:1;
+		unsigned endcleared:1;
+		unsigned ossfragshift;
+		int ossmaxfrags;
+		unsigned subdivision;
+	} dma_dac, dma_adc;
+
+	/* midi stuff */
+	struct {
+		unsigned ird, iwr, icnt;
+		unsigned ord, owr, ocnt;
+		struct wait_queue *iwait;
+		struct wait_queue *owait;
+		struct timer_list timer;
+		unsigned char ibuf[MIDIINBUF];
+		unsigned char obuf[MIDIOUTBUF];
+	} midi;
+};
+
+/* --------------------------------------------------------------------- */
+
+static struct cm_state *devs = NULL;
+static unsigned long wavetable_mem = 0;
+
+/* --------------------------------------------------------------------- */
+
+extern __inline__ unsigned ld2(unsigned int x)
+{
+	unsigned r = 0;
+	
+	if (x >= 0x10000) {
+		x >>= 16;
+		r += 16;
+	}
+	if (x >= 0x100) {
+		x >>= 8;
+		r += 8;
+	}
+	if (x >= 0x10) {
+		x >>= 4;
+		r += 4;
+	}
+	if (x >= 4) {
+		x >>= 2;
+		r += 2;
+	}
+	if (x >= 2)
+		r++;
+	return r;
+}
+
+/*
+ * hweightN: returns the hamming weight (i.e. the number
+ * of bits set) of a N-bit word
+ */
+
+#ifdef hweight32
+#undef hweight32
+#endif
+
+extern __inline__ unsigned int hweight32(unsigned int w)
+{
+        unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
+        res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
+        res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
+        res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
+        return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
+}
+
+/* --------------------------------------------------------------------- */
+
+static void set_dmadac(struct cm_state *s, unsigned int addr, unsigned int count)
+{
+	count--;
+	outl(addr, s->iobase + CODEC_CMI_CH0_FRAME1);
+	outw(count, s->iobase + CODEC_CMI_CH0_FRAME2);
+	outb(inb(s->iobase + CODEC_CMI_FUNCTRL0) & ~1, s->iobase + CODEC_CMI_FUNCTRL0);
+	outb(inb(s->iobase + CODEC_CMI_FUNCTRL0 + 2) | 1, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
+}
+
+static void set_dmaadc(struct cm_state *s, unsigned int addr, unsigned int count)
+{
+	count--;
+	outl(addr, s->iobase + CODEC_CMI_CH1_FRAME1);
+	outw(count, s->iobase + CODEC_CMI_CH1_FRAME2);
+	outb(inb(s->iobase + CODEC_CMI_FUNCTRL0) | 2, s->iobase + CODEC_CMI_FUNCTRL0);
+	outb(inb(s->iobase + CODEC_CMI_FUNCTRL0 + 2) | 2, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
+}
+
+extern __inline__ unsigned get_dmadac(struct cm_state *s)
+{
+	unsigned int curr_addr;
+
+	curr_addr = inl(s->iobase + CODEC_CMI_CH0_FRAME1);
+	curr_addr -= virt_to_bus(s->dma_dac.rawbuf);
+	curr_addr = s->dma_dac.dmasize - curr_addr;
+	curr_addr &= ~(sample_size[(s->fmt >> CM_CFMT_DACSHIFT) & CM_CFMT_MASK]-1);
+	return curr_addr;
+}
+
+extern __inline__ unsigned get_dmaadc(struct cm_state *s)
+{
+	unsigned int curr_addr;
+
+	curr_addr = inl(s->iobase + CODEC_CMI_CH1_FRAME1);
+	curr_addr -= virt_to_bus(s->dma_adc.rawbuf);
+	curr_addr = s->dma_adc.dmasize - curr_addr;
+	curr_addr &= ~(sample_size[(s->fmt >> CM_CFMT_ADCSHIFT) & CM_CFMT_MASK]-1);
+	return curr_addr;
+}
+
+static void wrmixer(struct cm_state *s, unsigned char idx, unsigned char data)
+{
+	outb(idx, s->iobase + CODEC_SB16_ADDR);
+	udelay(10);
+	outb(data, s->iobase + CODEC_SB16_DATA);
+	udelay(10);
+}
+
+static unsigned char rdmixer(struct cm_state *s, unsigned char idx)
+{
+	unsigned char v;
+
+	outb(idx, s->iobase + CODEC_SB16_ADDR);
+	udelay(10);
+	v = inb(s->iobase + CODEC_SB16_DATA);
+	udelay(10);
+	return v;
+}
+
+static void set_fmt(struct cm_state *s, unsigned char mask, unsigned char data)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&s->lock, flags);
+	if (mask) {
+		s->fmt = inb(s->iobase + CODEC_CMI_CHFORMAT);
+		udelay(10);
+	}
+	s->fmt = (s->fmt & mask) | data;
+	outb(s->fmt, s->iobase + CODEC_CMI_CHFORMAT);
+	spin_unlock_irqrestore(&s->lock, flags);
+	udelay(10);
+}
+
+static void frobindir(struct cm_state *s, unsigned char idx, unsigned char mask, unsigned char data)
+{
+	outb(idx, s->iobase + CODEC_SB16_ADDR);
+	udelay(10);
+	outb((inb(s->iobase + CODEC_SB16_DATA) & mask) | data, s->iobase + CODEC_SB16_DATA);
+	udelay(10);
+}
+
+static struct {
+	unsigned	rate;
+	unsigned	lower;
+	unsigned	upper;
+	unsigned char	freq;
+} rate_lookup[] =
+{
+	{ 5512,		(0 + 5512) / 2,		(5512 + 8000) / 2,	0 },
+	{ 8000,		(5512 + 8000) / 2,	(8000 + 11025) / 2,	4 },
+	{ 11025,	(8000 + 11025) / 2,	(11025 + 16000) / 2,	1 },
+	{ 16000,	(11025 + 16000) / 2,	(16000 + 22050) / 2,	5 },
+	{ 22050,	(16000 + 22050) / 2,	(22050 + 32000) / 2,	2 },
+	{ 32000,	(22050 + 32000) / 2,	(32000 + 44100) / 2,	6 },
+	{ 44100,	(32000 + 44100) / 2,	(44100 + 48000) / 2,	3 },
+	{ 48000,	48000,			48000,			7 }
+};
+
+static void set_dac_rate(struct cm_state *s, unsigned rate)
+{
+	unsigned long flags;
+	unsigned char freq = 4, val;
+	int	i;
+
+	if (rate > 48000)
+		rate = 48000;
+	if (rate < 5512)
+		rate = 5512;
+	for (i = 0; i < sizeof(rate_lookup) / sizeof(rate_lookup[0]); i++)
+	{
+		if (rate > rate_lookup[i].lower && rate <= rate_lookup[i].upper)
+		{
+			rate = rate_lookup[i].rate;
+			freq = rate_lookup[i].freq;
+			break;
+	    	}
+	}
+	s->ratedac = rate;
+	freq <<= 2;
+	spin_lock_irqsave(&s->lock, flags);
+	val = inb(s->iobase + CODEC_CMI_FUNCTRL1 + 1) & ~0x1c; 
+	outb(val | freq, s->iobase + CODEC_CMI_FUNCTRL1 + 1);
+	spin_unlock_irqrestore(&s->lock, flags);
+}
+
+static void set_adc_rate(struct cm_state *s, unsigned rate)
+{
+	unsigned long flags;
+	unsigned char freq = 4, val;
+	int	i;
+
+	if (rate > 48000)
+		rate = 48000;
+	if (rate < 5512)
+		rate = 5512;
+	for (i = 0; i < sizeof(rate_lookup) / sizeof(rate_lookup[0]); i++)
+	{
+		if (rate > rate_lookup[i].lower && rate <= rate_lookup[i].upper)
+		{
+			rate = rate_lookup[i].rate;
+			freq = rate_lookup[i].freq;
+			break;
+	    	}
+	}
+	s->rateadc = rate;
+	freq <<= 5;
+	spin_lock_irqsave(&s->lock, flags);
+	val = inb(s->iobase + CODEC_CMI_FUNCTRL1 + 1) & ~0xe0; 
+	outb(val | freq, s->iobase + CODEC_CMI_FUNCTRL1 + 1);
+	spin_unlock_irqrestore(&s->lock, flags);
+}
+
+/* --------------------------------------------------------------------- */
+
+extern inline void stop_adc(struct cm_state *s)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&s->lock, flags);
+	s->enable &= ~CM_CENABLE_RE;
+	/* disable interrupt */
+	outb(inb(s->iobase + CODEC_CMI_INT_HLDCLR + 2) & ~2, s->iobase + CODEC_CMI_INT_HLDCLR + 2);
+	/* disable channel and reset */
+	outb(s->enable | CM_CH1_RESET, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
+	udelay(10);
+	outb(s->enable & ~CM_CH1_RESET, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
+	spin_unlock_irqrestore(&s->lock, flags);
+}	
+
+extern inline void stop_dac(struct cm_state *s)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&s->lock, flags);
+	s->enable &= ~CM_CENABLE_PE;
+	/* disable interrupt */
+	outb(inb(s->iobase + CODEC_CMI_INT_HLDCLR + 2) & ~1, s->iobase + CODEC_CMI_INT_HLDCLR + 2);
+	/* disable channel and reset */
+	outb(s->enable | CM_CH0_RESET, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
+	udelay(10);
+	outb(s->enable & ~CM_CH0_RESET, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
+	spin_unlock_irqrestore(&s->lock, flags);
+}	
+
+static void start_dac(struct cm_state *s)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&s->lock, flags);
+	if ((s->dma_dac.mapped || s->dma_dac.count > 0) && s->dma_dac.ready) {
+		s->enable |= CM_CENABLE_PE;
+		outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
+	}
+	outb(inb(s->iobase + CODEC_CMI_INT_HLDCLR + 2) | 1, s->iobase + CODEC_CMI_INT_HLDCLR + 2);
+	spin_unlock_irqrestore(&s->lock, flags);
+}	
+
+static void start_adc(struct cm_state *s)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&s->lock, flags);
+	if ((s->dma_adc.mapped || s->dma_adc.count < (signed)(s->dma_adc.dmasize - 2*s->dma_adc.fragsize)) 
+	    && s->dma_adc.ready) {
+		s->enable |= CM_CENABLE_RE;
+		outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
+	}
+	outb(inb(s->iobase + CODEC_CMI_INT_HLDCLR + 2) | 2, s->iobase + CODEC_CMI_INT_HLDCLR + 2);
+	spin_unlock_irqrestore(&s->lock, flags);
+}	
+
+/* --------------------------------------------------------------------- */
+
+#define DMABUF_DEFAULTORDER (17-PAGE_SHIFT)
+#define DMABUF_MINORDER 1
+
+static void dealloc_dmabuf(struct dmabuf *db)
+{
+	unsigned long map, mapend;
+
+	if (db->rawbuf) {
+		/* undo marking the pages as reserved */
+		mapend = MAP_NR(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
+		for (map = MAP_NR(db->rawbuf); map <= mapend; map++)
+			clear_bit(PG_reserved, &mem_map[map].flags);	
+		free_pages((unsigned long)db->rawbuf, db->buforder);
+	}
+	db->rawbuf = NULL;
+	db->mapped = db->ready = 0;
+}
+
+
+/* Ch0 is used for playback, Ch1 is used for recording */
+
+static int prog_dmabuf(struct cm_state *s, unsigned rec)
+{
+	struct dmabuf *db = rec ? &s->dma_adc : &s->dma_dac;
+	unsigned rate = rec ? s->rateadc : s->ratedac;
+	int order;
+	unsigned bytepersec;
+	unsigned bufs;
+	unsigned long map, mapend;
+	unsigned char fmt;
+	unsigned long flags;
+
+	spin_lock_irqsave(&s->lock, flags);
+	fmt = s->fmt;
+	if (rec) {
+		s->enable &= ~CM_CENABLE_RE;
+		fmt >>= CM_CFMT_ADCSHIFT;
+	} else {
+		s->enable &= ~CM_CENABLE_PE;
+		fmt >>= CM_CFMT_DACSHIFT;
+	}
+	outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
+	spin_unlock_irqrestore(&s->lock, flags);
+	fmt &= CM_CFMT_MASK;
+	db->hwptr = db->swptr = db->total_bytes = db->count = db->error = db->endcleared = 0;
+	if (!db->rawbuf) {
+		db->ready = db->mapped = 0;
+		for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER && !db->rawbuf; order--)
+			db->rawbuf = (void *)__get_free_pages(GFP_KERNEL | GFP_DMA, order);
+		if (!db->rawbuf)
+			return -ENOMEM;
+		db->buforder = order;
+		if ((virt_to_bus(db->rawbuf) ^ (virt_to_bus(db->rawbuf) + (PAGE_SIZE << db->buforder) - 1)) & ~0xffff)
+			printk(KERN_DEBUG "cm: DMA buffer crosses 64k boundary: busaddr 0x%lx  size %ld\n", 
+			       virt_to_bus(db->rawbuf), PAGE_SIZE << db->buforder);
+		if ((virt_to_bus(db->rawbuf) + (PAGE_SIZE << db->buforder) - 1) & ~0xffffff)
+			printk(KERN_DEBUG "cm: DMA buffer beyond 16MB: busaddr 0x%lx  size %ld\n", 
+			       virt_to_bus(db->rawbuf), PAGE_SIZE << db->buforder);
+		/* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
+		mapend = MAP_NR(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
+		for (map = MAP_NR(db->rawbuf); map <= mapend; map++)
+			set_bit(PG_reserved, &mem_map[map].flags);
+	}
+	bytepersec = rate << sample_shift[fmt];
+	bufs = PAGE_SIZE << db->buforder;
+	if (db->ossfragshift) {
+		if ((1000 << db->ossfragshift) < bytepersec)
+			db->fragshift = ld2(bytepersec/1000);
+		else
+			db->fragshift = db->ossfragshift;
+	} else {
+		db->fragshift = ld2(bytepersec/100/(db->subdivision ? db->subdivision : 1));
+		if (db->fragshift < 3)
+			db->fragshift = 3;
+	}
+	db->numfrag = bufs >> db->fragshift;
+	while (db->numfrag < 4 && db->fragshift > 3) {
+		db->fragshift--;
+		db->numfrag = bufs >> db->fragshift;
+	}
+	db->fragsize = 1 << db->fragshift;
+	if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
+		db->numfrag = db->ossmaxfrags;
+#if 1
+	/* to make fragsize >= 4096 */
+	while (db->fragsize < 4096 && db->numfrag >= 4)
+	{
+		db->fragsize *= 2;
+		db->fragshift++;
+		db->numfrag /= 2;
+	}
+#endif
+	db->fragsamples = db->fragsize >> sample_shift[fmt];
+	db->dmasize = db->numfrag << db->fragshift;
+	memset(db->rawbuf, (fmt & CM_CFMT_16BIT) ? 0 : 0x80, db->dmasize);
+	spin_lock_irqsave(&s->lock, flags);
+	if (rec) {
+		set_dmaadc(s, virt_to_bus(db->rawbuf), db->dmasize >> sample_shift[fmt]);
+		/* program sample counts */
+		outw(db->fragsamples-1, s->iobase + CODEC_CMI_CH1_FRAME2 + 2);
+	} else {
+		set_dmadac(s, virt_to_bus(db->rawbuf), db->dmasize >> sample_shift[fmt]);
+		/* program sample counts */
+		outw(db->fragsamples-1, s->iobase + CODEC_CMI_CH0_FRAME2 + 2);
+	}
+	spin_unlock_irqrestore(&s->lock, flags);
+	db->ready = 1;
+	return 0;
+}
+
+extern __inline__ void clear_advance(struct cm_state *s)
+{
+	unsigned char c = (s->fmt & (CM_CFMT_16BIT << CM_CFMT_DACSHIFT)) ? 0 : 0x80;
+	unsigned char *buf = s->dma_dac.rawbuf;
+	unsigned bsize = s->dma_dac.dmasize;
+	unsigned bptr = s->dma_dac.swptr;
+	unsigned len = s->dma_dac.fragsize;
+
+	if (bptr + len > bsize) {
+		unsigned x = bsize - bptr;
+		memset(buf + bptr, c, x);
+		bptr = 0;
+		len -= x;
+	}
+	memset(buf + bptr, c, len);
+}
+
+/* call with spinlock held! */
+static void cm_update_ptr(struct cm_state *s)
+{
+	unsigned hwptr;
+	int diff;
+
+	/* update ADC pointer */
+	if (s->dma_adc.ready) {
+		hwptr = (s->dma_adc.dmasize - get_dmaadc(s)) % s->dma_adc.dmasize;
+		diff = (s->dma_adc.dmasize + hwptr - s->dma_adc.hwptr) % s->dma_adc.dmasize;
+		s->dma_adc.hwptr = hwptr;
+		s->dma_adc.total_bytes += diff;
+		s->dma_adc.count += diff;
+		if (s->dma_adc.count >= (signed)s->dma_adc.fragsize) 
+			wake_up(&s->dma_adc.wait);
+		if (!s->dma_adc.mapped) {
+			if (s->dma_adc.count > (signed)(s->dma_adc.dmasize - ((3 * s->dma_adc.fragsize) >> 1))) {
+				s->enable &= ~CM_CENABLE_RE;
+				outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
+				s->dma_adc.error++;
+			}
+		}
+	}
+	/* update DAC pointer */
+	if (s->dma_dac.ready) {
+		hwptr = (s->dma_dac.dmasize - get_dmadac(s)) % s->dma_dac.dmasize;
+		diff = (s->dma_dac.dmasize + hwptr - s->dma_dac.hwptr) % s->dma_dac.dmasize;
+		s->dma_dac.hwptr = hwptr;
+		s->dma_dac.total_bytes += diff;
+		if (s->dma_dac.mapped) {
+			s->dma_dac.count += diff;
+			if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
+				wake_up(&s->dma_dac.wait);
+		} else {
+			s->dma_dac.count -= diff;
+			if (s->dma_dac.count <= 0) {
+				s->enable &= ~CM_CENABLE_PE;
+				outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
+				s->dma_dac.error++;
+			} else if (s->dma_dac.count <= (signed)s->dma_dac.fragsize && !s->dma_dac.endcleared) {
+				clear_advance(s);
+				s->dma_dac.endcleared = 1;
+			}
+			if (s->dma_dac.count + (signed)s->dma_dac.fragsize <= (signed)s->dma_dac.dmasize)
+				wake_up(&s->dma_dac.wait);
+		}
+	}
+}
+
+/* hold spinlock for the following! */
+static void cm_handle_midi(struct cm_state *s)
+{
+	unsigned char ch;
+	int wake;
+
+	wake = 0;
+	while (!(inb(s->iomidi+1) & 0x80)) {
+		ch = inb(s->iomidi);
+		if (s->midi.icnt < MIDIINBUF) {
+			s->midi.ibuf[s->midi.iwr] = ch;
+			s->midi.iwr = (s->midi.iwr + 1) % MIDIINBUF;
+			s->midi.icnt++;
+		}
+		wake = 1;
+	}
+	if (wake)
+		wake_up(&s->midi.iwait);
+	wake = 0;
+	while (!(inb(s->iomidi+1) & 0x40) && s->midi.ocnt > 0) {
+		outb(s->midi.obuf[s->midi.ord], s->iomidi);
+		s->midi.ord = (s->midi.ord + 1) % MIDIOUTBUF;
+		s->midi.ocnt--;
+		if (s->midi.ocnt < MIDIOUTBUF-16)
+			wake = 1;
+	}
+	if (wake)
+		wake_up(&s->midi.owait);
+}
+
+static void cm_interrupt(int irq, void *dev_id, struct pt_regs *regs)
+{
+        struct cm_state *s = (struct cm_state *)dev_id;
+	unsigned int intsrc, intstat;
+	
+	/* fastpath out, to ease interrupt sharing */
+	intsrc = inb(s->iobase + CODEC_CMI_INT_STATUS);
+	if (!(intsrc & (CM_INT_CH0 | CM_INT_CH1)))
+		return;
+	spin_lock(&s->lock);
+	intstat = inb(s->iobase + CODEC_CMI_INT_HLDCLR + 2);
+	/* disable interrupt */
+	if (intsrc & CM_INT_CH0)
+		outb(intstat & ~1, s->iobase + CODEC_CMI_INT_HLDCLR + 2);
+	if (intsrc & CM_INT_CH1)
+		outb(intstat & ~2, s->iobase + CODEC_CMI_INT_HLDCLR + 2);
+	cm_update_ptr(s);
+#ifdef SOUND_CONFIG_CMPCI_MIDI
+	cm_handle_midi(s);
+#endif
+	/* enable interrupt */
+	if (intsrc & CM_INT_CH0)
+		outb(intstat | 1, s->iobase + CODEC_CMI_INT_HLDCLR + 2);
+	if (intsrc & CM_INT_CH1)
+		outb(intstat | 2, s->iobase + CODEC_CMI_INT_HLDCLR + 2);
+	spin_unlock(&s->lock);
+}
+
+static void cm_midi_timer(unsigned long data)
+{
+	struct cm_state *s = (struct cm_state *)data;
+	unsigned long flags;
+	
+	spin_lock_irqsave(&s->lock, flags);
+	cm_handle_midi(s);
+	spin_unlock_irqrestore(&s->lock, flags);
+	s->midi.timer.expires = jiffies+1;
+	add_timer(&s->midi.timer);
+}
+
+/* --------------------------------------------------------------------- */
+
+static const char invalid_magic[] = KERN_CRIT "cm: invalid magic value\n";
+
+#ifdef CONFIG_SOUND_CMPCI	/* support multiple chips */
+#define VALIDATE_STATE(s)
+#else
+#define VALIDATE_STATE(s)                         \
+({                                                \
+	if (!(s) || (s)->magic != CM_MAGIC) { \
+		printk(invalid_magic);            \
+		return -ENXIO;                    \
+	}                                         \
+})
+#endif
+
+/* --------------------------------------------------------------------- */
+
+#define MT_4          1
+#define MT_5MUTE      2
+#define MT_4MUTEMONO  3
+#define MT_6MUTE      4
+
+static const struct {
+	unsigned left;
+	unsigned right;
+	unsigned type;
+	unsigned rec;
+	unsigned play;
+} mixtable[SOUND_MIXER_NRDEVICES] = {
+	[SOUND_MIXER_CD]     = { DSP_MIX_CDVOLIDX_L,     DSP_MIX_CDVOLIDX_R,     MT_5MUTE,     0x04, 0x02 },
+	[SOUND_MIXER_LINE]   = { DSP_MIX_LINEVOLIDX_L,   DSP_MIX_LINEVOLIDX_R,   MT_5MUTE,     0x10, 0x08 },
+	[SOUND_MIXER_MIC]    = { DSP_MIX_MICVOLIDX,      CODEC_CMI_MIXER2,       MT_4MUTEMONO, 0x01, 0x01 },
+	[SOUND_MIXER_SYNTH]  = { DSP_MIX_FMVOLIDX_L,  	 DSP_MIX_FMVOLIDX_R,     MT_5MUTE,     0x40, 0x00 },
+	[SOUND_MIXER_VOLUME] = { DSP_MIX_MASTERVOLIDX_L, DSP_MIX_MASTERVOLIDX_R, MT_5MUTE,     0x00, 0x00 },
+	[SOUND_MIXER_PCM]    = { DSP_MIX_VOICEVOLIDX_L,  DSP_MIX_VOICEVOLIDX_R,  MT_5MUTE,     0x00, 0x00 }
+};
+
+#ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
+
+static int return_mixval(struct cm_state *s, unsigned i, int *arg)
+{
+	unsigned long flags;
+	unsigned char l, r, rl, rr;
+
+	spin_lock_irqsave(&s->lock, flags);
+	l = rdmixer(s, mixtable[i].left);
+	r = rdmixer(s, mixtable[i].right);
+	spin_unlock_irqrestore(&s->lock, flags);
+	switch (mixtable[i].type) {
+	case MT_4:
+		r &= 0xf;
+		l &= 0xf;
+		rl = 10 + 6 * (l & 15);
+		rr = 10 + 6 * (r & 15);
+		break;
+
+	case MT_4MUTEMONO:
+		rl = 55 - 3 * (l & 15);
+		if (r & 0x10)
+			rl += 45;
+		rr = rl;
+		r = l;
+		break;
+
+	case MT_5MUTE:
+	default:
+		rl = 100 - 3 * (l & 31);
+		rr = 100 - 3 * (r & 31);
+		break;
+				
+	case MT_6MUTE:
+		rl = 100 - 3 * (l & 63) / 2;
+		rr = 100 - 3 * (r & 63) / 2;
+		break;
+	}
+	if (l & 0x80)
+		rl = 0;
+	if (r & 0x80)
+		rr = 0;
+	return put_user((rr << 8) | rl, arg);
+}
+
+#else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
+
+static const unsigned char volidx[SOUND_MIXER_NRDEVICES] = 
+{
+	[SOUND_MIXER_CD]     = 1,
+	[SOUND_MIXER_LINE]   = 2,
+	[SOUND_MIXER_MIC]    = 3,
+	[SOUND_MIXER_SYNTH]  = 4,
+	[SOUND_MIXER_VOLUME] = 5,
+	[SOUND_MIXER_PCM]    = 6
+};
+
+#endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
+
+static unsigned mixer_recmask(struct cm_state *s)
+{
+	unsigned long flags;
+	int i, j, k;
+
+	spin_lock_irqsave(&s->lock, flags);
+	j = rdmixer(s, DSP_MIX_ADCMIXIDX_L);
+	spin_unlock_irqrestore(&s->lock, flags);
+	j &= 0x7f;
+	for (k = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
+		if (j & mixtable[i].rec)
+			k |= 1 << i;
+	return k;
+}
+
+static int mixer_ioctl(struct cm_state *s, unsigned int cmd, unsigned long arg)
+{
+	unsigned long flags;
+	int i, val, j;
+	unsigned char l, r, rl, rr;
+
+	VALIDATE_STATE(s);
+        if (cmd == SOUND_MIXER_INFO) {
+		mixer_info info;
+		strncpy(info.id, "cmpci", sizeof(info.id));
+		strncpy(info.name, "C-Media PCI", sizeof(info.name));
+		info.modify_counter = s->mix.modcnt;
+		if (copy_to_user((void *)arg, &info, sizeof(info)))
+			return -EFAULT;
+		return 0;
+	}
+	if (cmd == SOUND_OLD_MIXER_INFO) {
+		_old_mixer_info info;
+		strncpy(info.id, "cmpci", sizeof(info.id));
+		strncpy(info.name, "C-Media cmpci", sizeof(info.name));
+		if (copy_to_user((void *)arg, &info, sizeof(info)))
+			return -EFAULT;
+		return 0;
+	}
+	if (cmd == OSS_GETVERSION)
+		return put_user(SOUND_VERSION, (int *)arg);
+	if (_IOC_TYPE(cmd) != 'M' || _IOC_SIZE(cmd) != sizeof(int))
+                return -EINVAL;
+        if (_IOC_DIR(cmd) == _IOC_READ) {
+                switch (_IOC_NR(cmd)) {
+                case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
+			return put_user(mixer_recmask(s), (int *)arg);
+			
+                case SOUND_MIXER_OUTSRC: /* Arg contains a bit for each recording source */
+			return put_user(mixer_recmask(s), (int *)arg);//need fix
+			
+                case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */
+			for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
+				if (mixtable[i].type)
+					val |= 1 << i;
+			return put_user(val, (int *)arg);
+
+                case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
+			for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
+				if (mixtable[i].rec)
+					val |= 1 << i;
+			return put_user(val, (int *)arg);
+			
+                case SOUND_MIXER_OUTMASK: /* Arg contains a bit for each supported recording source */
+			for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
+				if (mixtable[i].play)
+					val |= 1 << i;
+			return put_user(val, (int *)arg);
+			
+                 case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
+			for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
+				if (mixtable[i].type && mixtable[i].type != MT_4MUTEMONO)
+					val |= 1 << i;
+			return put_user(val, (int *)arg);
+			
+                case SOUND_MIXER_CAPS:
+			return put_user(0, (int *)arg);
+
+		default:
+			i = _IOC_NR(cmd);
+                        if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
+                                return -EINVAL;
+#ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
+			return return_mixval(s, i, (int *)arg);
+#else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
+			if (!volidx[i])
+				return -EINVAL;
+			return put_user(s->mix.vol[volidx[i]-1], (int *)arg);
+#endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
+		}
+	}
+        if (_IOC_DIR(cmd) != (_IOC_READ|_IOC_WRITE)) 
+		return -EINVAL;
+	s->mix.modcnt++;
+	switch (_IOC_NR(cmd)) {
+	case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
+		get_user_ret(val, (int *)arg, -EFAULT);
+		i = hweight32(val);
+		for (j = i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
+			if (!(val & (1 << i)))
+				continue;
+			if (!mixtable[i].rec) {
+				val &= ~(1 << i);
+				continue;
+			}
+			j |= mixtable[i].rec;
+		}
+		spin_lock_irqsave(&s->lock, flags);
+		wrmixer(s, DSP_MIX_ADCMIXIDX_L, j);
+		wrmixer(s, DSP_MIX_ADCMIXIDX_R, (j & 1) | j>>1);
+		spin_unlock_irqrestore(&s->lock, flags);
+		return 0;
+
+	case SOUND_MIXER_OUTSRC: /* Arg contains a bit for each recording source */
+		get_user_ret(val, (int *)arg, -EFAULT);
+		for (j = i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
+			if (!(val & (1 << i)))
+				continue;
+			if (!mixtable[i].play) {
+				val &= ~(1 << i);
+				continue;
+			}
+			j |= mixtable[i].play;
+		}
+		spin_lock_irqsave(&s->lock, flags);
+		frobindir(s, DSP_MIX_OUTMIXIDX, 0x1f, j);
+		spin_unlock_irqrestore(&s->lock, flags);
+		return 0;
+
+		default:
+		i = _IOC_NR(cmd);
+		if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
+			return -EINVAL;
+		get_user_ret(val, (int *)arg, -EFAULT);
+		l = val & 0xff;
+		r = (val >> 8) & 0xff;
+		if (l > 100)
+			l = 100;
+		if (r > 100)
+			r = 100;
+		spin_lock_irqsave(&s->lock, flags);
+		switch (mixtable[i].type) {
+		case MT_4:
+			if (l >= 10)
+				l -= 10;
+			if (r >= 10)
+				r -= 10;
+			frobindir(s, mixtable[i].left, 0xf0, l / 6);
+			frobindir(s, mixtable[i].right, 0xf0, l / 6);
+			break;
+
+		case MT_4MUTEMONO:
+			rl = (l < 4 ? 0 : (l - 5) / 3) & 31;
+			rr = (rl >> 2) & 7;
+			wrmixer(s, mixtable[i].left, rl<<3);
+			outb((inb(s->iobase + CODEC_CMI_MIXER2) & ~0x0e) | rr<<1, s->iobase + CODEC_CMI_MIXER2);
+			break;
+			
+		case MT_5MUTE:
+			rl = l < 4 ? 0 : (l - 5) / 3;
+			rr = r < 4 ? 0 : (r - 5) / 3;
+ 			wrmixer(s, mixtable[i].left, rl<<3);
+			wrmixer(s, mixtable[i].right, rr<<3);
+			break;
+				
+		case MT_6MUTE:
+			if (l < 6)
+				rl = 0x00;
+			else
+				rl = l * 2 / 3;
+			if (r < 6)
+				rr = 0x00;
+			else
+				rr = r * 2 / 3;
+			wrmixer(s, mixtable[i].left, rl);
+			wrmixer(s, mixtable[i].right, rr);
+			break;
+		}
+		spin_unlock_irqrestore(&s->lock, flags);
+#ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
+                return return_mixval(s, i, (int *)arg);
+#else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
+		if (!volidx[i])
+			return -EINVAL;
+		s->mix.vol[volidx[i]-1] = val;
+		return put_user(s->mix.vol[volidx[i]-1], (int *)arg);
+#endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
+	}
+}
+
+/* --------------------------------------------------------------------- */
+
+static loff_t cm_llseek(struct file *file, loff_t offset, int origin)
+{
+	return -ESPIPE;
+}
+
+/* --------------------------------------------------------------------- */
+
+static int cm_open_mixdev(struct inode *inode, struct file *file)
+{
+	int minor = MINOR(inode->i_rdev);
+	struct cm_state *s = devs;
+
+	while (s && s->dev_mixer != minor)
+		s = s->next;
+	if (!s)
+		return -ENODEV;
+       	VALIDATE_STATE(s);
+	file->private_data = s;
+	MOD_INC_USE_COUNT;
+	return 0;
+}
+
+static int cm_release_mixdev(struct inode *inode, struct file *file)
+{
+	struct cm_state *s = (struct cm_state *)file->private_data;
+	
+	VALIDATE_STATE(s);
+	MOD_DEC_USE_COUNT;
+	return 0;
+}
+
+static int cm_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
+{
+	return mixer_ioctl((struct cm_state *)file->private_data, cmd, arg);
+}
+
+static /*const*/ struct file_operations cm_mixer_fops = {
+	&cm_llseek,
+	NULL,  /* read */
+	NULL,  /* write */
+	NULL,  /* readdir */
+	NULL,  /* poll */
+	&cm_ioctl_mixdev,
+	NULL,  /* mmap */
+	&cm_open_mixdev,
+	NULL,	/* flush */
+	&cm_release_mixdev,
+	NULL,  /* fsync */
+	NULL,  /* fasync */
+	NULL,  /* check_media_change */
+	NULL,  /* revalidate */
+	NULL,  /* lock */
+};
+
+/* --------------------------------------------------------------------- */
+
+static int drain_dac(struct cm_state *s, int nonblock)
+{
+        struct wait_queue wait = { current, NULL };
+	unsigned long flags;
+	int count, tmo;
+
+	if (s->dma_dac.mapped || !s->dma_dac.ready)
+		return 0;
+        current->state = TASK_INTERRUPTIBLE;
+        add_wait_queue(&s->dma_dac.wait, &wait);
+        for (;;) {
+                spin_lock_irqsave(&s->lock, flags);
+		count = s->dma_dac.count;
+                spin_unlock_irqrestore(&s->lock, flags);
+		if (count <= 0)
+			break;
+		if (signal_pending(current))
+                        break;
+                if (nonblock) {
+                        remove_wait_queue(&s->dma_dac.wait, &wait);
+                        current->state = TASK_RUNNING;
+                        return -EBUSY;
+                }
+		tmo = (count * HZ) / s->ratedac;
+		tmo >>= sample_shift[(s->fmt >> CM_CFMT_DACSHIFT) & CM_CFMT_MASK];
+		if (!schedule_timeout(tmo ? : 1) && tmo)
+			printk(KERN_DEBUG "cm: dma timed out??\n");
+        }
+        remove_wait_queue(&s->dma_dac.wait, &wait);
+        current->state = TASK_RUNNING;
+        if (signal_pending(current))
+                return -ERESTARTSYS;
+        return 0;
+}
+
+/* --------------------------------------------------------------------- */
+
+static ssize_t cm_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
+{
+	struct cm_state *s = (struct cm_state *)file->private_data;
+	ssize_t ret;
+	unsigned long flags;
+	unsigned swptr;
+	int cnt;
+
+	VALIDATE_STATE(s);
+	if (ppos != &file->f_pos)
+		return -ESPIPE;
+	if (s->dma_adc.mapped)
+		return -ENXIO;
+	if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
+		return ret;
+	if (!access_ok(VERIFY_WRITE, buffer, count))
+		return -EFAULT;
+	ret = 0;
+#if 0
+   spin_lock_irqsave(&s->lock, flags);
+   cm_update_ptr(s);
+   spin_unlock_irqrestore(&s->lock, flags);
+#endif
+	while (count > 0) {
+		spin_lock_irqsave(&s->lock, flags);
+		swptr = s->dma_adc.swptr;
+		cnt = s->dma_adc.dmasize-swptr;
+		if (s->dma_adc.count < cnt)
+			cnt = s->dma_adc.count;
+		spin_unlock_irqrestore(&s->lock, flags);
+		if (cnt > count)
+			cnt = count;
+		if (cnt <= 0) {
+			start_adc(s);
+			if (file->f_flags & O_NONBLOCK)
+				return ret ? ret : -EAGAIN;
+			interruptible_sleep_on(&s->dma_adc.wait);
+			if (signal_pending(current))
+				return ret ? ret : -ERESTARTSYS;
+			continue;
+		}
+		if (copy_to_user(buffer, s->dma_adc.rawbuf + swptr, cnt))
+			return ret ? ret : -EFAULT;
+		swptr = (swptr + cnt) % s->dma_adc.dmasize;
+		spin_lock_irqsave(&s->lock, flags);
+		s->dma_adc.swptr = swptr;
+		s->dma_adc.count -= cnt;
+		spin_unlock_irqrestore(&s->lock, flags);
+		count -= cnt;
+		buffer += cnt;
+		ret += cnt;
+		start_adc(s);
+	}
+	return ret;
+}
+
+static ssize_t cm_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
+{
+	struct cm_state *s = (struct cm_state *)file->private_data;
+	ssize_t ret;
+	unsigned long flags;
+	unsigned swptr;
+	int cnt;
+
+	VALIDATE_STATE(s);
+	if (ppos != &file->f_pos)
+		return -ESPIPE;
+	if (s->dma_dac.mapped)
+		return -ENXIO;
+	if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
+		return ret;
+	if (!access_ok(VERIFY_READ, buffer, count))
+		return -EFAULT;
+	ret = 0;
+#if 0
+   spin_lock_irqsave(&s->lock, flags);
+   cm_update_ptr(s);
+   spin_unlock_irqrestore(&s->lock, flags);
+#endif
+	while (count > 0) {
+		spin_lock_irqsave(&s->lock, flags);
+		if (s->dma_dac.count < 0) {
+			s->dma_dac.count = 0;
+			s->dma_dac.swptr = s->dma_dac.hwptr;
+		}
+		swptr = s->dma_dac.swptr;
+		cnt = s->dma_dac.dmasize-swptr;
+		if (s->dma_dac.count + cnt > s->dma_dac.dmasize)
+			cnt = s->dma_dac.dmasize - s->dma_dac.count;
+		spin_unlock_irqrestore(&s->lock, flags);
+		if (cnt > count)
+			cnt = count;
+		if (cnt <= 0) {
+			start_dac(s);
+			if (file->f_flags & O_NONBLOCK)
+				return ret ? ret : -EAGAIN;
+			interruptible_sleep_on(&s->dma_dac.wait);
+			if (signal_pending(current))
+				return ret ? ret : -ERESTARTSYS;
+			continue;
+		}
+		if (copy_from_user(s->dma_dac.rawbuf + swptr, buffer, cnt))
+			return ret ? ret : -EFAULT;
+		swptr = (swptr + cnt) % s->dma_dac.dmasize;
+		spin_lock_irqsave(&s->lock, flags);
+		s->dma_dac.swptr = swptr;
+		s->dma_dac.count += cnt;
+		s->dma_dac.endcleared = 0;
+		spin_unlock_irqrestore(&s->lock, flags);
+		count -= cnt;
+		buffer += cnt;
+		ret += cnt;
+		start_dac(s);
+	}
+	return ret;
+}
+
+static unsigned int cm_poll(struct file *file, struct poll_table_struct *wait)
+{
+	struct cm_state *s = (struct cm_state *)file->private_data;
+	unsigned long flags;
+	unsigned int mask = 0;
+
+	VALIDATE_STATE(s);
+	if (file->f_mode & FMODE_WRITE)
+		poll_wait(file, &s->dma_dac.wait, wait);
+	if (file->f_mode & FMODE_READ)
+		poll_wait(file, &s->dma_adc.wait, wait);
+	spin_lock_irqsave(&s->lock, flags);
+	cm_update_ptr(s);
+	if (file->f_mode & FMODE_READ) {
+		if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
+			mask |= POLLIN | POLLRDNORM;
+	}
+	if (file->f_mode & FMODE_WRITE) {
+		if (s->dma_dac.mapped) {
+			if (s->dma_dac.count >= (signed)s->dma_dac.fragsize) 
+				mask |= POLLOUT | POLLWRNORM;
+		} else {
+			if ((signed)s->dma_dac.dmasize >= s->dma_dac.count + (signed)s->dma_dac.fragsize)
+				mask |= POLLOUT | POLLWRNORM;
+		}
+	}
+	spin_unlock_irqrestore(&s->lock, flags);
+	return mask;
+}
+
+static int cm_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct cm_state *s = (struct cm_state *)file->private_data;
+	struct dmabuf *db;
+	int ret;
+	unsigned long size;
+
+	VALIDATE_STATE(s);
+	if (vma->vm_flags & VM_WRITE) {
+		if ((ret = prog_dmabuf(s, 1)) != 0)
+			return ret;
+		db = &s->dma_dac;
+	} else if (vma->vm_flags & VM_READ) {
+		if ((ret = prog_dmabuf(s, 0)) != 0)
+			return ret;
+		db = &s->dma_adc;
+	} else 
+		return -EINVAL;
+	if (vma->vm_offset != 0)
+		return -EINVAL;
+	size = vma->vm_end - vma->vm_start;
+	if (size > (PAGE_SIZE << db->buforder))
+		return -EINVAL;
+	if (remap_page_range(vma->vm_start, virt_to_phys(db->rawbuf), size, vma->vm_page_prot))
+		return -EAGAIN;
+	db->mapped = 1;
+	return 0;
+}
+
+static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
+{
+	struct cm_state *s = (struct cm_state *)file->private_data;
+	unsigned long flags;
+        audio_buf_info abinfo;
+        count_info cinfo;
+	int val, mapped, ret;
+	unsigned char fmtm, fmtd;
+
+	VALIDATE_STATE(s);
+        mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
+		((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
+	switch (cmd) {
+	case OSS_GETVERSION:
+		return put_user(SOUND_VERSION, (int *)arg);
+
+	case SNDCTL_DSP_SYNC:
+		if (file->f_mode & FMODE_WRITE)
+			return drain_dac(s, 0/*file->f_flags & O_NONBLOCK*/);
+		return 0;
+		
+	case SNDCTL_DSP_SETDUPLEX:
+		return 0;
+
+	case SNDCTL_DSP_GETCAPS:
+		return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER | DSP_CAP_MMAP, (int *)arg);
+		
+        case SNDCTL_DSP_RESET:
+		if (file->f_mode & FMODE_WRITE) {
+			stop_dac(s);
+			synchronize_irq();
+			s->dma_dac.swptr = s->dma_dac.hwptr = s->dma_dac.count = s->dma_dac.total_bytes = 0;
+		}
+		if (file->f_mode & FMODE_READ) {
+			stop_adc(s);
+			synchronize_irq();
+			s->dma_adc.swptr = s->dma_adc.hwptr = s->dma_adc.count = s->dma_adc.total_bytes = 0;
+		}
+		return 0;
+
+        case SNDCTL_DSP_SPEED:
+                get_user_ret(val, (int *)arg, -EFAULT);
+		if (val >= 0) {
+			if (file->f_mode & FMODE_READ) {
+				stop_adc(s);
+				s->dma_adc.ready = 0;
+				set_adc_rate(s, val);
+			}
+			if (file->f_mode & FMODE_WRITE) {
+				stop_dac(s);
+				s->dma_dac.ready = 0;
+				set_dac_rate(s, val);
+			}
+		}
+		return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, (int *)arg);
+		
+        case SNDCTL_DSP_STEREO:
+                get_user_ret(val, (int *)arg, -EFAULT);
+		fmtd = 0;
+		fmtm = ~0;
+		if (file->f_mode & FMODE_READ) {
+			stop_adc(s);
+			s->dma_adc.ready = 0;
+			if (val)
+				fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
+			else
+				fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
+		}
+		if (file->f_mode & FMODE_WRITE) {
+			stop_dac(s);
+			s->dma_dac.ready = 0;
+			if (val)
+				fmtd |= CM_CFMT_STEREO << CM_CFMT_DACSHIFT;
+			else
+				fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_DACSHIFT);
+		}
+		set_fmt(s, fmtm, fmtd);
+		return 0;
+
+        case SNDCTL_DSP_CHANNELS:
+                get_user_ret(val, (int *)arg, -EFAULT);
+		if (val != 0) {
+			fmtd = 0;
+			fmtm = ~0;
+			if (file->f_mode & FMODE_READ) {
+				stop_adc(s);
+				s->dma_adc.ready = 0;
+				if (val >= 2)
+					fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
+				else
+					fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
+			}
+			if (file->f_mode & FMODE_WRITE) {
+				stop_dac(s);
+				s->dma_dac.ready = 0;
+				if (val >= 2)
+					fmtd |= CM_CFMT_STEREO << CM_CFMT_DACSHIFT;
+				else
+					fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_DACSHIFT);
+			}
+			set_fmt(s, fmtm, fmtd);
+		}
+		return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_STEREO << CM_CFMT_ADCSHIFT) 
+					   : (CM_CFMT_STEREO << CM_CFMT_DACSHIFT))) ? 2 : 1, (int *)arg);
+		
+	case SNDCTL_DSP_GETFMTS: /* Returns a mask */
+                return put_user(AFMT_S16_LE|AFMT_U8, (int *)arg);
+		
+	case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
+		get_user_ret(val, (int *)arg, -EFAULT);
+		if (val != AFMT_QUERY) {
+			fmtd = 0;
+			fmtm = ~0;
+			if (file->f_mode & FMODE_READ) {
+				stop_adc(s);
+				s->dma_adc.ready = 0;
+				if (val == AFMT_S16_LE)
+					fmtd |= CM_CFMT_16BIT << CM_CFMT_ADCSHIFT;
+				else
+					fmtm &= ~(CM_CFMT_16BIT << CM_CFMT_ADCSHIFT);
+			}
+			if (file->f_mode & FMODE_WRITE) {
+				stop_dac(s);
+				s->dma_dac.ready = 0;
+				if (val == AFMT_S16_LE)
+					fmtd |= CM_CFMT_16BIT << CM_CFMT_DACSHIFT;
+				else
+					fmtm &= ~(CM_CFMT_16BIT << CM_CFMT_DACSHIFT);
+			}
+			set_fmt(s, fmtm, fmtd);
+		}
+		return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_16BIT << CM_CFMT_ADCSHIFT) 
+					   : (CM_CFMT_16BIT << CM_CFMT_DACSHIFT))) ? AFMT_S16_LE : AFMT_U8, (int *)arg);
+		
+	case SNDCTL_DSP_POST:
+                return 0;
+
+        case SNDCTL_DSP_GETTRIGGER:
+		val = 0;
+		if (file->f_mode & FMODE_READ && s->enable & CM_CENABLE_RE) 
+			val |= PCM_ENABLE_INPUT;
+		if (file->f_mode & FMODE_WRITE && s->enable & CM_CENABLE_PE) 
+			val |= PCM_ENABLE_OUTPUT;
+		return put_user(val, (int *)arg);
+		
+	case SNDCTL_DSP_SETTRIGGER:
+		get_user_ret(val, (int *)arg, -EFAULT);
+		if (file->f_mode & FMODE_READ) {
+			if (val & PCM_ENABLE_INPUT) {
+				if (!s->dma_adc.ready && (ret =  prog_dmabuf(s, 1)))
+					return ret;
+				start_adc(s);
+			} else
+				stop_adc(s);
+		}
+		if (file->f_mode & FMODE_WRITE) {
+			if (val & PCM_ENABLE_OUTPUT) {
+				if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
+					return ret;
+				start_dac(s);
+			} else
+				stop_dac(s);
+		}
+		return 0;
+
+	case SNDCTL_DSP_GETOSPACE:
+		if (!(file->f_mode & FMODE_WRITE))
+			return -EINVAL;
+		if (!(s->enable & CM_CENABLE_PE) && (val = prog_dmabuf(s, 0)) != 0)
+			return val;
+		spin_lock_irqsave(&s->lock, flags);
+		cm_update_ptr(s);
+		abinfo.fragsize = s->dma_dac.fragsize;
+                abinfo.bytes = s->dma_dac.dmasize - s->dma_dac.count;
+                abinfo.fragstotal = s->dma_dac.numfrag;
+                abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;      
+		spin_unlock_irqrestore(&s->lock, flags);
+		return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
+
+	case SNDCTL_DSP_GETISPACE:
+		if (!(file->f_mode & FMODE_READ))
+			return -EINVAL;
+		if (!(s->enable & CM_CENABLE_RE) && (val = prog_dmabuf(s, 1)) != 0)
+			return val;
+		spin_lock_irqsave(&s->lock, flags);
+		cm_update_ptr(s);
+		abinfo.fragsize = s->dma_adc.fragsize;
+                abinfo.bytes = s->dma_adc.count;
+                abinfo.fragstotal = s->dma_adc.numfrag;
+                abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;      
+		spin_unlock_irqrestore(&s->lock, flags);
+		return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
+		
+        case SNDCTL_DSP_NONBLOCK:
+                file->f_flags |= O_NONBLOCK;
+                return 0;
+
+        case SNDCTL_DSP_GETODELAY:
+		if (!(file->f_mode & FMODE_WRITE))
+			return -EINVAL;
+		spin_lock_irqsave(&s->lock, flags);
+		cm_update_ptr(s);
+                val = s->dma_dac.count;
+		spin_unlock_irqrestore(&s->lock, flags);
+		return put_user(val, (int *)arg);
+
+        case SNDCTL_DSP_GETIPTR:
+		if (!(file->f_mode & FMODE_READ))
+			return -EINVAL;
+		spin_lock_irqsave(&s->lock, flags);
+		cm_update_ptr(s);
+                cinfo.bytes = s->dma_adc.total_bytes;
+                cinfo.blocks = s->dma_adc.count >> s->dma_adc.fragshift;
+                cinfo.ptr = s->dma_adc.hwptr;
+		if (s->dma_adc.mapped)
+			s->dma_adc.count &= s->dma_adc.fragsize-1;
+		spin_unlock_irqrestore(&s->lock, flags);
+                return copy_to_user((void *)arg, &cinfo, sizeof(cinfo));
+
+        case SNDCTL_DSP_GETOPTR:
+		if (!(file->f_mode & FMODE_WRITE))
+			return -EINVAL;
+		spin_lock_irqsave(&s->lock, flags);
+		cm_update_ptr(s);
+                cinfo.bytes = s->dma_dac.total_bytes;
+                cinfo.blocks = s->dma_dac.count >> s->dma_dac.fragshift;
+                cinfo.ptr = s->dma_dac.hwptr;
+		if (s->dma_dac.mapped)
+			s->dma_dac.count &= s->dma_dac.fragsize-1;
+		spin_unlock_irqrestore(&s->lock, flags);
+                return copy_to_user((void *)arg, &cinfo, sizeof(cinfo));
+
+        case SNDCTL_DSP_GETBLKSIZE:
+		if (file->f_mode & FMODE_WRITE) {
+			if ((val = prog_dmabuf(s, 0)))
+				return val;
+			return put_user(s->dma_dac.fragsize, (int *)arg);
+		}
+		if ((val = prog_dmabuf(s, 1)))
+			return val;
+		return put_user(s->dma_adc.fragsize, (int *)arg);
+
+        case SNDCTL_DSP_SETFRAGMENT:
+                get_user_ret(val, (int *)arg, -EFAULT);
+		if (file->f_mode & FMODE_READ) {
+			s->dma_adc.ossfragshift = val & 0xffff;
+			s->dma_adc.ossmaxfrags = (val >> 16) & 0xffff;
+			if (s->dma_adc.ossfragshift < 4)
+				s->dma_adc.ossfragshift = 4;
+			if (s->dma_adc.ossfragshift > 15)
+				s->dma_adc.ossfragshift = 15;
+			if (s->dma_adc.ossmaxfrags < 4)
+				s->dma_adc.ossmaxfrags = 4;
+		}
+		if (file->f_mode & FMODE_WRITE) {
+			s->dma_dac.ossfragshift = val & 0xffff;
+			s->dma_dac.ossmaxfrags = (val >> 16) & 0xffff;
+			if (s->dma_dac.ossfragshift < 4)
+				s->dma_dac.ossfragshift = 4;
+			if (s->dma_dac.ossfragshift > 15)
+				s->dma_dac.ossfragshift = 15;
+			if (s->dma_dac.ossmaxfrags < 4)
+				s->dma_dac.ossmaxfrags = 4;
+		}
+		return 0;
+
+        case SNDCTL_DSP_SUBDIVIDE:
+		if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
+		    (file->f_mode & FMODE_WRITE && s->dma_dac.subdivision))
+			return -EINVAL;
+                get_user_ret(val, (int *)arg, -EFAULT);
+		if (val != 1 && val != 2 && val != 4)
+			return -EINVAL;
+		if (file->f_mode & FMODE_READ)
+			s->dma_adc.subdivision = val;
+		if (file->f_mode & FMODE_WRITE)
+			s->dma_dac.subdivision = val;
+		return 0;
+
+        case SOUND_PCM_READ_RATE:
+		return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, (int *)arg);
+
+        case SOUND_PCM_READ_CHANNELS:
+		return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_STEREO << CM_CFMT_ADCSHIFT) : (CM_CFMT_STEREO << CM_CFMT_DACSHIFT))) ? 2 : 1, (int *)arg);
+
+        case SOUND_PCM_READ_BITS:
+		return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_16BIT << CM_CFMT_ADCSHIFT) : (CM_CFMT_16BIT << CM_CFMT_DACSHIFT))) ? 16 : 8, (int *)arg);
+
+        case SOUND_PCM_WRITE_FILTER:
+        case SNDCTL_DSP_SETSYNCRO:
+        case SOUND_PCM_READ_FILTER:
+                return -EINVAL;
+		
+	}
+	return mixer_ioctl(s, cmd, arg);
+}
+
+static int cm_open(struct inode *inode, struct file *file)
+{
+	int minor = MINOR(inode->i_rdev);
+	struct cm_state *s = devs;
+	unsigned char fmtm = ~0, fmts = 0;
+
+	while (s && ((s->dev_audio ^ minor) & ~0xf))
+		s = s->next;
+	if (!s)
+		return -ENODEV;
+       	VALIDATE_STATE(s);
+	file->private_data = s;
+	/* wait for device to become free */
+	down(&s->open_sem);
+	while (s->open_mode & file->f_mode) {
+		if (file->f_flags & O_NONBLOCK) {
+			up(&s->open_sem);
+			return -EBUSY;
+		}
+		up(&s->open_sem);
+		interruptible_sleep_on(&s->open_wait);
+		if (signal_pending(current))
+			return -ERESTARTSYS;
+		down(&s->open_sem);
+	}
+	if (file->f_mode & FMODE_READ) {
+		fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_ADCSHIFT);
+		if ((minor & 0xf) == SND_DEV_DSP16)
+			fmts |= CM_CFMT_16BIT << CM_CFMT_ADCSHIFT;
+		s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags = s->dma_adc.subdivision = 0;
+		set_adc_rate(s, 8000);
+	}
+	if (file->f_mode & FMODE_WRITE) {
+		fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_DACSHIFT);
+		if ((minor & 0xf) == SND_DEV_DSP16)
+			fmts |= CM_CFMT_16BIT << CM_CFMT_DACSHIFT;
+		s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags = s->dma_dac.subdivision = 0;
+		set_dac_rate(s, 8000);
+	}
+	set_fmt(s, fmtm, fmts);
+	s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
+	up(&s->open_sem);
+	MOD_INC_USE_COUNT;
+	return 0;
+}
+
+static int cm_release(struct inode *inode, struct file *file)
+{
+	struct cm_state *s = (struct cm_state *)file->private_data;
+
+	VALIDATE_STATE(s);
+	if (file->f_mode & FMODE_WRITE)
+		drain_dac(s, file->f_flags & O_NONBLOCK);
+	down(&s->open_sem);
+	if (file->f_mode & FMODE_WRITE) {
+		stop_dac(s);
+		dealloc_dmabuf(&s->dma_dac);
+	}
+	if (file->f_mode & FMODE_READ) {
+		stop_adc(s);
+		dealloc_dmabuf(&s->dma_adc);
+	}
+	s->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
+	up(&s->open_sem);
+	wake_up(&s->open_wait);
+	MOD_DEC_USE_COUNT;
+	return 0;
+}
+
+static /*const*/ struct file_operations cm_audio_fops = {
+	&cm_llseek,
+	&cm_read,
+	&cm_write,
+	NULL,  /* readdir */
+	&cm_poll,
+	&cm_ioctl,
+	&cm_mmap,
+	&cm_open,
+	NULL,	/* flush */
+	&cm_release,
+	NULL,  /* fsync */
+	NULL,  /* fasync */
+	NULL,  /* check_media_change */
+	NULL,  /* revalidate */
+	NULL,  /* lock */
+};
+
+#ifdef CONFIG_SOUND_CMPCI_MIDI
+/* --------------------------------------------------------------------- */
+
+static ssize_t cm_midi_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
+{
+	struct cm_state *s = (struct cm_state *)file->private_data;
+	ssize_t ret;
+	unsigned long flags;
+	unsigned ptr;
+	int cnt;
+
+	VALIDATE_STATE(s);
+	if (ppos != &file->f_pos)
+		return -ESPIPE;
+	if (!access_ok(VERIFY_WRITE, buffer, count))
+		return -EFAULT;
+	ret = 0;
+	while (count > 0) {
+		spin_lock_irqsave(&s->lock, flags);
+		ptr = s->midi.ird;
+		cnt = MIDIINBUF - ptr;
+		if (s->midi.icnt < cnt)
+			cnt = s->midi.icnt;
+		spin_unlock_irqrestore(&s->lock, flags);
+		if (cnt > count)
+			cnt = count;
+		if (cnt <= 0) {
+			if (file->f_flags & O_NONBLOCK)
+				return ret ? ret : -EAGAIN;
+			interruptible_sleep_on(&s->midi.iwait);
+			if (signal_pending(current))
+				return ret ? ret : -ERESTARTSYS;
+			continue;
+		}
+		if (copy_to_user(buffer, s->midi.ibuf + ptr, cnt))
+			return ret ? ret : -EFAULT;
+		ptr = (ptr + cnt) % MIDIINBUF;
+		spin_lock_irqsave(&s->lock, flags);
+		s->midi.ird = ptr;
+		s->midi.icnt -= cnt;
+		spin_unlock_irqrestore(&s->lock, flags);
+		count -= cnt;
+		buffer += cnt;
+		ret += cnt;
+	}
+	return ret;
+}
+
+static ssize_t cm_midi_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
+{
+	struct cm_state *s = (struct cm_state *)file->private_data;
+	ssize_t ret;
+	unsigned long flags;
+	unsigned ptr;
+	int cnt;
+
+	VALIDATE_STATE(s);
+	if (ppos != &file->f_pos)
+		return -ESPIPE;
+	if (!access_ok(VERIFY_READ, buffer, count))
+		return -EFAULT;
+	ret = 0;
+	while (count > 0) {
+		spin_lock_irqsave(&s->lock, flags);
+		ptr = s->midi.owr;
+		cnt = MIDIOUTBUF - ptr;
+		if (s->midi.ocnt + cnt > MIDIOUTBUF)
+			cnt = MIDIOUTBUF - s->midi.ocnt;
+		if (cnt <= 0)
+			cm_handle_midi(s);
+		spin_unlock_irqrestore(&s->lock, flags);
+		if (cnt > count)
+			cnt = count;
+		if (cnt <= 0) {
+			if (file->f_flags & O_NONBLOCK)
+				return ret ? ret : -EAGAIN;
+			interruptible_sleep_on(&s->midi.owait);
+			if (signal_pending(current))
+				return ret ? ret : -ERESTARTSYS;
+			continue;
+		}
+		if (copy_from_user(s->midi.obuf + ptr, buffer, cnt))
+			return ret ? ret : -EFAULT;
+		ptr = (ptr + cnt) % MIDIOUTBUF;
+		spin_lock_irqsave(&s->lock, flags);
+		s->midi.owr = ptr;
+		s->midi.ocnt += cnt;
+		spin_unlock_irqrestore(&s->lock, flags);
+		count -= cnt;
+		buffer += cnt;
+		ret += cnt;
+		spin_lock_irqsave(&s->lock, flags);
+		cm_handle_midi(s);
+		spin_unlock_irqrestore(&s->lock, flags);
+	}
+	return ret;
+}
+
+static unsigned int cm_midi_poll(struct file *file, struct poll_table_struct *wait)
+{
+	struct cm_state *s = (struct cm_state *)file->private_data;
+	unsigned long flags;
+	unsigned int mask = 0;
+
+	VALIDATE_STATE(s);
+	if (file->f_mode & FMODE_WRITE)
+		poll_wait(file, &s->midi.owait, wait);
+	if (file->f_mode & FMODE_READ)
+		poll_wait(file, &s->midi.iwait, wait);
+	spin_lock_irqsave(&s->lock, flags);
+	if (file->f_mode & FMODE_READ) {
+		if (s->midi.icnt > 0)
+			mask |= POLLIN | POLLRDNORM;
+	}
+	if (file->f_mode & FMODE_WRITE) {
+		if (s->midi.ocnt < MIDIOUTBUF)
+			mask |= POLLOUT | POLLWRNORM;
+	}
+	spin_unlock_irqrestore(&s->lock, flags);
+	return mask;
+}
+
+static int cm_midi_open(struct inode *inode, struct file *file)
+{
+	int minor = MINOR(inode->i_rdev);
+	struct cm_state *s = devs;
+	unsigned long flags;
+
+	while (s && s->dev_midi != minor)
+		s = s->next;
+	if (!s)
+		return -ENODEV;
+       	VALIDATE_STATE(s);
+	file->private_data = s;
+	/* wait for device to become free */
+	down(&s->open_sem);
+	while (s->open_mode & (file->f_mode << FMODE_MIDI_SHIFT)) {
+		if (file->f_flags & O_NONBLOCK) {
+			up(&s->open_sem);
+			return -EBUSY;
+		}
+		up(&s->open_sem);
+		interruptible_sleep_on(&s->open_wait);
+		if (signal_pending(current))
+			return -ERESTARTSYS;
+		down(&s->open_sem);
+	}
+	spin_lock_irqsave(&s->lock, flags);
+	if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
+		s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
+		s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
+		/* enable MPU-401 */
+		outb(inb(s->iobase + CODEC_CMI_FUNCTRL1) | 4, s->iobase + CODEC_CMI_FUNCTRL1);
+		outb(0xff, s->iomidi+1); /* reset command */
+		if (!(inb(s->iomidi+1) & 0x80))
+			inb(s->iomidi);
+		outb(0x3f, s->iomidi+1); /* uart command */
+		if (!(inb(s->iomidi+1) & 0x80))
+			inb(s->iomidi);
+		s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
+		init_timer(&s->midi.timer);
+		s->midi.timer.expires = jiffies+1;
+		s->midi.timer.data = (unsigned long)s;
+		s->midi.timer.function = cm_midi_timer;
+		add_timer(&s->midi.timer);
+	}
+	if (file->f_mode & FMODE_READ) {
+		s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
+	}
+	if (file->f_mode & FMODE_WRITE) {
+		s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
+	}
+	spin_unlock_irqrestore(&s->lock, flags);
+	s->open_mode |= (file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ | FMODE_MIDI_WRITE);
+	up(&s->open_sem);
+	MOD_INC_USE_COUNT;
+	return 0;
+}
+
+static int cm_midi_release(struct inode *inode, struct file *file)
+{
+	struct cm_state *s = (struct cm_state *)file->private_data;
+        struct wait_queue wait = { current, NULL };
+	unsigned long flags;
+	unsigned count, tmo;
+
+	VALIDATE_STATE(s);
+
+	if (file->f_mode & FMODE_WRITE) {
+		current->state = TASK_INTERRUPTIBLE;
+		add_wait_queue(&s->midi.owait, &wait);
+		for (;;) {
+			spin_lock_irqsave(&s->lock, flags);
+			count = s->midi.ocnt;
+			spin_unlock_irqrestore(&s->lock, flags);
+			if (count <= 0)
+				break;
+			if (signal_pending(current))
+				break;
+			if (file->f_flags & O_NONBLOCK) {
+				remove_wait_queue(&s->midi.owait, &wait);
+				current->state = TASK_RUNNING;
+				return -EBUSY;
+			}
+			tmo = (count * HZ) / 3100;
+			if (!schedule_timeout(tmo ? : 1) && tmo)
+				printk(KERN_DEBUG "cm: midi timed out??\n");
+		}
+		remove_wait_queue(&s->midi.owait, &wait);
+		current->state = TASK_RUNNING;
+	}
+	down(&s->open_sem);
+	s->open_mode &= (~(file->f_mode << FMODE_MIDI_SHIFT)) & (FMODE_MIDI_READ|FMODE_MIDI_WRITE);
+	spin_lock_irqsave(&s->lock, flags);
+	if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
+		del_timer(&s->midi.timer);		
+		outb(0xff, s->iomidi+1); /* reset command */
+		if (!(inb(s->iomidi+1) & 0x80))
+			inb(s->iomidi);
+		/* disable MPU-401 */
+		outb(inb(s->iobase + CODEC_CMI_FUNCTRL1) & ~4, s->iobase + CODEC_CMI_FUNCTRL1);
+	}
+	spin_unlock_irqrestore(&s->lock, flags);
+	up(&s->open_sem);
+	wake_up(&s->open_wait);
+	MOD_DEC_USE_COUNT;
+	return 0;
+}
+
+static /*const*/ struct file_operations cm_midi_fops = {
+	&cm_llseek,
+	&cm_midi_read,
+	&cm_midi_write,
+	NULL,  /* readdir */
+	&cm_midi_poll,
+	NULL,  /* ioctl */
+	NULL,  /* mmap */
+	&cm_midi_open,
+	NULL,	/* flush */
+	&cm_midi_release,
+	NULL,  /* fsync */
+	NULL,  /* fasync */
+	NULL,  /* check_media_change */
+	NULL,  /* revalidate */
+	NULL,  /* lock */
+};
+#endif
+
+/* --------------------------------------------------------------------- */
+
+#ifdef CONFIG_SOUND_CMPCI_FM
+static int cm_dmfm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
+{
+	static const unsigned char op_offset[18] = {
+		0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
+		0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
+		0x10, 0x11, 0x12, 0x13, 0x14, 0x15
+	};
+	struct cm_state *s = (struct cm_state *)file->private_data;
+	struct dm_fm_voice v;
+	struct dm_fm_note n;
+	struct dm_fm_params p;
+	unsigned int io;
+	unsigned int regb;
+
+	switch (cmd) {		
+	case FM_IOCTL_RESET:
+		for (regb = 0xb0; regb < 0xb9; regb++) {
+			outb(regb, s->iosynth);
+			outb(0, s->iosynth+1);
+			outb(regb, s->iosynth+2);
+			outb(0, s->iosynth+3);
+		}
+		return 0;
+
+	case FM_IOCTL_PLAY_NOTE:
+		if (copy_from_user(&n, (void *)arg, sizeof(n)))
+			return -EFAULT;
+		if (n.voice >= 18)
+			return -EINVAL;
+		if (n.voice >= 9) {
+			regb = n.voice - 9;
+			io = s->iosynth+2;
+		} else {
+			regb = n.voice;
+			io = s->iosynth;
+		}
+		outb(0xa0 + regb, io);
+		outb(n.fnum & 0xff, io+1);
+		outb(0xb0 + regb, io);
+		outb(((n.fnum >> 8) & 3) | ((n.octave & 7) << 2) | ((n.key_on & 1) << 5), io+1);
+		return 0;
+
+	case FM_IOCTL_SET_VOICE:
+		if (copy_from_user(&v, (void *)arg, sizeof(v)))
+			return -EFAULT;
+		if (v.voice >= 18)
+			return -EINVAL;
+		regb = op_offset[v.voice];
+		io = s->iosynth + ((v.op & 1) << 1);
+		outb(0x20 + regb, io);
+		outb(((v.am & 1) << 7) | ((v.vibrato & 1) << 6) | ((v.do_sustain & 1) << 5) | 
+		     ((v.kbd_scale & 1) << 4) | (v.harmonic & 0xf), io+1);
+		outb(0x40 + regb, io);
+		outb(((v.scale_level & 0x3) << 6) | (v.volume & 0x3f), io+1);
+		outb(0x60 + regb, io);
+		outb(((v.attack & 0xf) << 4) | (v.decay & 0xf), io+1);
+		outb(0x80 + regb, io);
+		outb(((v.sustain & 0xf) << 4) | (v.release & 0xf), io+1);
+		outb(0xe0 + regb, io);
+		outb(v.waveform & 0x7, io+1);
+		if (n.voice >= 9) {
+			regb = n.voice - 9;
+			io = s->iosynth+2;
+		} else {
+			regb = n.voice;
+			io = s->iosynth;
+		}
+		outb(0xc0 + regb, io);
+		outb(((v.right & 1) << 5) | ((v.left & 1) << 4) | ((v.feedback & 7) << 1) |
+		     (v.connection & 1), io+1);
+		return 0;
+		
+	case FM_IOCTL_SET_PARAMS:
+		if (copy_from_user(&p, (void *)arg, sizeof(p)))
+			return -EFAULT;
+		outb(0x08, s->iosynth);
+		outb((p.kbd_split & 1) << 6, s->iosynth+1);
+		outb(0xbd, s->iosynth);
+		outb(((p.am_depth & 1) << 7) | ((p.vib_depth & 1) << 6) | ((p.rhythm & 1) << 5) | ((p.bass & 1) << 4) |
+		     ((p.snare & 1) << 3) | ((p.tomtom & 1) << 2) | ((p.cymbal & 1) << 1) | (p.hihat & 1), s->iosynth+1);
+		return 0;
+
+	case FM_IOCTL_SET_OPL:
+		outb(4, s->iosynth+2);
+		outb(arg, s->iosynth+3);
+		return 0;
+
+	case FM_IOCTL_SET_MODE:
+		outb(5, s->iosynth+2);
+		outb(arg & 1, s->iosynth+3);
+		return 0;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int cm_dmfm_open(struct inode *inode, struct file *file)
+{
+	int minor = MINOR(inode->i_rdev);
+	struct cm_state *s = devs;
+
+	while (s && s->dev_dmfm != minor)
+		s = s->next;
+	if (!s)
+		return -ENODEV;
+       	VALIDATE_STATE(s);
+	file->private_data = s;
+	/* wait for device to become free */
+	down(&s->open_sem);
+	while (s->open_mode & FMODE_DMFM) {
+		if (file->f_flags & O_NONBLOCK) {
+			up(&s->open_sem);
+			return -EBUSY;
+		}
+		up(&s->open_sem);
+		interruptible_sleep_on(&s->open_wait);
+		if (signal_pending(current))
+			return -ERESTARTSYS;
+		down(&s->open_sem);
+	}
+	/* init the stuff */
+	outb(1, s->iosynth);
+	outb(0x20, s->iosynth+1); /* enable waveforms */
+	outb(4, s->iosynth+2);
+	outb(0, s->iosynth+3);  /* no 4op enabled */
+	outb(5, s->iosynth+2);
+	outb(1, s->iosynth+3);  /* enable OPL3 */
+	s->open_mode |= FMODE_DMFM;
+	up(&s->open_sem);
+	MOD_INC_USE_COUNT;
+	return 0;
+}
+
+static int cm_dmfm_release(struct inode *inode, struct file *file)
+{
+	struct cm_state *s = (struct cm_state *)file->private_data;
+	unsigned int regb;
+
+	VALIDATE_STATE(s);
+	down(&s->open_sem);
+	s->open_mode &= ~FMODE_DMFM;
+	for (regb = 0xb0; regb < 0xb9; regb++) {
+		outb(regb, s->iosynth);
+		outb(0, s->iosynth+1);
+		outb(regb, s->iosynth+2);
+		outb(0, s->iosynth+3);
+	}
+	up(&s->open_sem);
+	wake_up(&s->open_wait);
+	MOD_DEC_USE_COUNT;
+	return 0;
+}
+
+static /*const*/ struct file_operations cm_dmfm_fops = {
+	&cm_llseek,
+	NULL,  /* read */
+	NULL,  /* write */
+	NULL,  /* readdir */
+	NULL,  /* poll */
+	&cm_dmfm_ioctl,
+	NULL,  /* mmap */
+	&cm_dmfm_open,
+	NULL,	/* flush */
+	&cm_dmfm_release,
+	NULL,  /* fsync */
+	NULL,  /* fasync */
+	NULL,  /* check_media_change */
+	NULL,  /* revalidate */
+	NULL,  /* lock */
+};
+#endif /* CONFIG_SOUND_CMPCI_FM */
+
+/* --------------------------------------------------------------------- */
+
+/* maximum number of devices */
+#define NR_DEVICE 5
+
+#if 0
+static int reverb[NR_DEVICE] = { 0, };
+
+static int wavetable[NR_DEVICE] = { 0, };
+#endif
+
+/* --------------------------------------------------------------------- */
+
+static struct initvol {
+	int mixch;
+	int vol;
+} initvol[] __initdata = {
+	{ SOUND_MIXER_WRITE_CD, 0x4040 },
+	{ SOUND_MIXER_WRITE_LINE, 0x4040 },
+	{ SOUND_MIXER_WRITE_MIC, 0x4040 },
+	{ SOUND_MIXER_WRITE_SYNTH, 0x4040 },
+	{ SOUND_MIXER_WRITE_VOLUME, 0x4040 },
+	{ SOUND_MIXER_WRITE_PCM, 0x4040 }
+};
+
+#ifdef MODULE
+__initfunc(int init_module(void))
+#else
+__initfunc(int init_cmpci(void))
+#endif
+{
+	struct cm_state *s;
+	struct pci_dev *pcidev = NULL;
+	mm_segment_t fs;
+	int i, val, index = 0;
+	struct {
+		unsigned short	deviceid;
+		char		*devicename;
+	} devicetable[] =
+	{
+		{ PCI_DEVICE_ID_CMEDIA_CM8338A, "CM8338A" },
+		{ PCI_DEVICE_ID_CMEDIA_CM8338B, "CM8338B" },
+		{ PCI_DEVICE_ID_CMEDIA_CM8738,  "CM8738" },
+	};
+	char	*devicename = "unknown";
+
+#ifdef CONFIG_PCI
+	if (!pci_present())   /* No PCI bus in this machine! */
+#endif
+		return -ENODEV;
+	printk(KERN_INFO "cm: version v1.1 time " __TIME__ " " __DATE__ "\n");
+#if 0
+	if (!(wavetable_mem = __get_free_pages(GFP_KERNEL, 20-PAGE_SHIFT)))
+		printk(KERN_INFO "cm: cannot allocate 1MB of contiguous nonpageable memory for wavetable data\n");
+#endif
+	while (index < NR_DEVICE && pcidev == NULL && (
+ 	       (pcidev = pci_find_device(PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338A, pcidev)) ||
+	       (pcidev = pci_find_device(PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338B, pcidev)) ||
+	       (pcidev = pci_find_device(PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738, pcidev)))) {
+		if (pcidev->irq == 0)
+			continue;
+		if (!(s = kmalloc(sizeof(struct cm_state), GFP_KERNEL))) {
+			printk(KERN_WARNING "cm: out of memory\n");
+			continue;
+		}
+		/* search device name */
+		for (i = 0; i < sizeof(devicetable) / sizeof(devicetable[0]); i++)
+		{
+			if (devicetable[i].deviceid == pcidev->device)
+			{
+				devicename = devicetable[i].devicename;
+				break;
+			}
+		}
+		memset(s, 0, sizeof(struct cm_state));
+		init_waitqueue(&s->dma_adc.wait);
+		init_waitqueue(&s->dma_dac.wait);
+		init_waitqueue(&s->open_wait);
+		init_waitqueue(&s->midi.iwait);
+		init_waitqueue(&s->midi.owait);
+		s->open_sem = MUTEX;
+		s->magic = CM_MAGIC;
+		s->iobase = pcidev->base_address[0] & PCI_BASE_ADDRESS_IO_MASK;
+#ifdef CONFIG_SOUND_CMPCI_FM
+		s->iosynth = 0x388;
+#endif
+#ifdef CONFIG_SOUND_CMPCI_MIDI
+		s->iomidi = 0x330;
+#endif
+		if (s->iobase == 0)
+			continue;
+		s->irq = pcidev->irq;
+
+		if (check_region(s->iobase, CM_EXTENT_CODEC)) {
+			printk(KERN_ERR "cm: io ports %#x-%#x in use\n", s->iobase, s->iobase+CM_EXTENT_CODEC-1);
+			goto err_region5;
+		}
+		request_region(s->iobase, CM_EXTENT_CODEC, "cmpci");
+#ifdef CONFIG_SOUND_CMPCI_MIDI
+		if (check_region(s->iomidi, CM_EXTENT_MIDI)) {
+			printk(KERN_ERR "cm: io ports %#x-%#x in use\n", s->iomidi, s->iomidi+CM_EXTENT_MIDI-1);
+			goto err_region4;
+		}
+		request_region(s->iomidi, CM_EXTENT_MIDI, "cmpci Midi");
+		/* set IO based at 0x330 */
+		outb(inb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3) & ~0x60, s->iobase + CODEC_CMI_LEGACY_CTRL + 3);
+#endif
+#ifdef CONFIG_SOUND_CMPCI_FM
+		if (check_region(s->iosynth, CM_EXTENT_SYNTH)) {
+			printk(KERN_ERR "cm: io ports %#x-%#x in use\n", s->iosynth, s->iosynth+CM_EXTENT_SYNTH-1);
+			goto err_region1;
+		}
+		request_region(s->iosynth, CM_EXTENT_SYNTH, "cmpci FM");
+		/* enable FM */
+		outb(inb(s->iobase + CODEC_CMI_MISC_CTRL + 2) | 8, s->iobase + CODEC_CMI_MISC_CTRL);
+#endif
+		/* initialize codec registers */
+		outb(0, s->iobase + CODEC_CMI_INT_HLDCLR + 2);  /* disable ints */
+		outb(0, s->iobase + CODEC_CMI_FUNCTRL0 + 2); /* reset channels */
+		/* reset mixer */
+		wrmixer(s, DSP_MIX_DATARESETIDX, 0);
+
+		/* request irq */
+		if (request_irq(s->irq, cm_interrupt, SA_SHIRQ, "cmpci", s)) {
+			printk(KERN_ERR "cm: irq %u in use\n", s->irq);
+			goto err_irq;
+		}
+		printk(KERN_INFO "cm: found %s adapter at io %#06x irq %u\n",
+		       devicename, s->iobase, s->irq);
+		/* register devices */
+		if ((s->dev_audio = register_sound_dsp(&cm_audio_fops, -1)) < 0)
+			goto err_dev1;
+		if ((s->dev_mixer = register_sound_mixer(&cm_mixer_fops, -1)) < 0)
+			goto err_dev2;
+#ifdef CONFIG_SOUND_CMPCI_MIDI
+		if ((s->dev_midi = register_sound_midi(&cm_midi_fops, -1)) < 0)
+			goto err_dev3;
+#endif
+#ifdef CONFIG_SOUND_CMPCI_FM
+		if ((s->dev_dmfm = register_sound_special(&cm_dmfm_fops, 15 /* ?? */)) < 0)
+			goto err_dev4;
+#endif
+		/* initialize the chips */
+		fs = get_fs();
+		set_fs(KERNEL_DS);
+		/* set mixer output */
+		frobindir(s, DSP_MIX_OUTMIXIDX, 0x1f, 0x1f);
+		/* set mixer input */
+		val = SOUND_MASK_LINE|SOUND_MASK_SYNTH|SOUND_MASK_CD|SOUND_MASK_MIC;
+		mixer_ioctl(s, SOUND_MIXER_WRITE_RECSRC, (unsigned long)&val);
+		for (i = 0; i < sizeof(initvol)/sizeof(initvol[0]); i++) {
+			val = initvol[i].vol;
+			mixer_ioctl(s, initvol[i].mixch, (unsigned long)&val);
+		}
+		set_fs(fs);
+		/* queue it for later freeing */
+		s->next = devs;
+		devs = s;
+		index++;
+		continue;
+
+	err_dev4:
+		unregister_sound_midi(s->dev_midi);
+	err_dev3:
+		unregister_sound_mixer(s->dev_mixer);
+	err_dev2:
+		unregister_sound_dsp(s->dev_audio);
+	err_dev1:
+		printk(KERN_ERR "cm: cannot register misc device\n");
+		free_irq(s->irq, s);
+	err_irq:
+#ifdef CONFIG_SOUND_CMPCI_FM
+		release_region(s->iosynth, CM_EXTENT_SYNTH);
+	err_region1:
+#endif
+#ifdef CONFIG_SOUND_CMPCI_MIDI
+		release_region(s->iomidi, CM_EXTENT_MIDI);
+#endif
+	err_region4:
+		release_region(s->iobase, CM_EXTENT_CODEC);
+	err_region5:
+		kfree_s(s, sizeof(struct cm_state));
+	}
+	if (!devs) {
+		if (wavetable_mem)
+			free_pages(wavetable_mem, 20-PAGE_SHIFT);
+		return -ENODEV;
+	}
+	return 0;
+}
+
+/* --------------------------------------------------------------------- */
+
+#ifdef MODULE
+
+#if 0
+MODULE_PARM(wavetable, "1-" __MODULE_STRING(NR_DEVICE) "i");
+MODULE_PARM_DESC(wavetable, "if 1 the wavetable synth is enabled");
+#endif
+
+MODULE_AUTHOR("ChenLi Tien, cltien@home.com");
+MODULE_DESCRIPTION("CMPCI Audio Driver");
+
+void cleanup_module(void)
+{
+	struct cm_state *s;
+
+	while ((s = devs)) {
+		devs = devs->next;
+		outb(0, s->iobase + CODEC_CMI_INT_HLDCLR + 2);  /* disable ints */
+		synchronize_irq();
+		outb(0, s->iobase + CODEC_CMI_FUNCTRL0 + 2); /* reset channels */
+		free_irq(s->irq, s);
+
+		/* reset mixer */
+		wrmixer(s, DSP_MIX_DATARESETIDX, 0);
+
+		release_region(s->iobase, CM_EXTENT_CODEC);
+#ifdef CONFIG_SOUND_CMPCI_MIDI
+		release_region(s->iomidi, CM_EXTENT_MIDI);
+#endif
+#ifdef CONFIG_SOUND_CMPCI_FM
+		release_region(s->iosynth, CM_EXTENT_SYNTH);
+#endif
+		unregister_sound_dsp(s->dev_audio);
+		unregister_sound_mixer(s->dev_mixer);
+#ifdef CONFIG_SOUND_CMPCI_MIDI
+		unregister_sound_midi(s->dev_midi);
+#endif
+#ifdef CONFIG_SOUND_CMPCI_FM
+		unregister_sound_special(s->dev_dmfm);
+#endif
+		kfree_s(s, sizeof(struct cm_state));
+	}
+	if (wavetable_mem)
+		free_pages(wavetable_mem, 20-PAGE_SHIFT);
+	printk(KERN_INFO "cm: unloading\n");
+}
+
+#endif /* MODULE */
diff -urN linux-2.2.13-pre3/drivers/sound/Config.in linux-2.2.13-pre4/drivers/sound/Config.in
--- linux-2.2.13-pre3/drivers/sound/Config.in	1999-08-25 17:29:48.000000000 -0700
+++ linux-2.2.13-pre4/drivers/sound/Config.in	2003-08-15 15:07:26.000000000 -0700
@@ -9,9 +9,14 @@
 
 # Prompt user for primary drivers.
 
-if [ "$CONFIG_VISWS" = "y" ]; then
-    dep_tristate 'SGI Visual Workstation Sound' CONFIG_SOUND_VWSND $CONFIG_SOUND
+if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
+  dep_tristate 'Support for C-Media PCI audio chips (Experimental)' CONFIG_SOUND_CMPCI $CONFIG_SOUND
+  if [ "$CONFIG_SOUND_CMPCI" = "y" -o "$CONFIG_SOUND_CMPCI" = "m" ]; then
+     bool 'Enable legacy FM' CONFIG_SOUND_CMPCI_FM
+     bool 'Enable legacy MPU-401' CONFIG_SOUND_CMPCI_MIDI
+  fi
 fi
+
 dep_tristate 'Ensoniq AudioPCI (ES1370)' CONFIG_SOUND_ES1370 $CONFIG_SOUND
 if [ "$CONFIG_SOUND_ES1370" = "y" ]; then
    bool 'Joystick support at boot time' CONFIG_SOUND_ES1370_JOYPORT_BOOT
@@ -29,6 +34,10 @@
 
 dep_tristate 'S3 SonicVibes' CONFIG_SOUND_SONICVIBES $CONFIG_SOUND
 
+if [ "$CONFIG_VISWS" = "y" ]; then
+    dep_tristate 'SGI Visual Workstation Sound' CONFIG_SOUND_VWSND $CONFIG_SOUND
+fi
+
 dep_tristate 'Support for Turtle Beach MultiSound Classic, Tahiti, Monterey' CONFIG_SOUND_MSNDCLAS $CONFIG_SOUND
 if [ "$CONFIG_SOUND_MSNDCLAS" = "y" -o "$CONFIG_SOUND_MSNDCLAS" = "m" ]; then
    if [ "$CONFIG_SOUND_MSNDCLAS" = "y" ]; then
@@ -79,6 +88,7 @@
    int 'MSND buffer size (kB)' CONFIG_MSND_FIFOSIZE 128
 fi
 
+
 dep_tristate 'OSS sound modules' CONFIG_SOUND_OSS $CONFIG_SOUND
 
 if [ "$CONFIG_SOUND_OSS" = "y" -o "$CONFIG_SOUND_OSS" = "m" ]; then
@@ -182,6 +192,8 @@
       fi
   fi
   
+  dep_tristate 'VIA 82C686 Audio Codec' CONFIG_SOUND_VIA82CXXX $CONFIG_SOUND_OSS
+
   dep_tristate 'Support for OPTi MAD16 and/or Mozart based cards' CONFIG_SOUND_MAD16 $CONFIG_SOUND_OSS
   if [ "$CONFIG_SOUND_MAD16" = "y" -o "$CONFIG_SOUND_MAD16" = "m" ]; then
     bool 'Support MIDI in older MAD16 based cards (requires SB)' CONFIG_MAD16_OLDCARD
diff -urN linux-2.2.13-pre3/drivers/sound/Makefile linux-2.2.13-pre4/drivers/sound/Makefile
--- linux-2.2.13-pre3/drivers/sound/Makefile	1999-08-25 17:29:48.000000000 -0700
+++ linux-2.2.13-pre4/drivers/sound/Makefile	2003-08-15 15:07:26.000000000 -0700
@@ -53,6 +53,7 @@
 obj-$(CONFIG_SOUND_CS4232)	+= uart401.o
 obj-$(CONFIG_SOUND_GUS)		+= gus.o ad1848.o
 obj-$(CONFIG_SOUND_MAD16)	+= mad16.o ad1848.o sb.o uart401.o
+obj-$(CONFIG_SOUND_VIA82CXXX)	+= via82cxxx.o sb.o uart401.o
 obj-$(CONFIG_SOUND_MAUI)	+= maui.o mpu401.o
 obj-$(CONFIG_SOUND_MPU401)	+= mpu401.o
 obj-$(CONFIG_SOUND_MSNDCLAS)	+= msnd.o msnd_classic.o
@@ -77,6 +78,7 @@
 obj-$(CONFIG_SOUND_WAVEFRONT)   += wavefront.o
 
 #jnx
+obj-$(CONFIG_SOUND_CMPCI)	+= cmpci.o
 obj-$(CONFIG_SOUND_ES1370)	+= es1370.o
 obj-$(CONFIG_SOUND_ES1371)	+= es1371.o
 obj-$(CONFIG_SOUND_ESSSOLO1)	+= esssolo1.o
diff -urN linux-2.2.13-pre3/drivers/sound/via82cxxx.c linux-2.2.13-pre4/drivers/sound/via82cxxx.c
--- linux-2.2.13-pre3/drivers/sound/via82cxxx.c	1969-12-31 16:00:00.000000000 -0800
+++ linux-2.2.13-pre4/drivers/sound/via82cxxx.c	2003-08-15 15:07:26.000000000 -0700
@@ -0,0 +1,266 @@
+/*
+ * Support for VIA 82Cxxx Audio Codecs
+ * Copyright 1999 Jeff Garzik <jgarzik@pobox.com>
+ *
+ * Distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2.
+ * See the "COPYING" file distributed with this software for more info.
+ *
+ ********************************************************************
+ *
+ * TODO:
+ *
+ *	- Integrate AC'97 support, when AC'97 interface released
+ *
+ */
+ 
+#include <linux/module.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <linux/fs.h>
+#include <linux/kernel.h>
+#include <linux/pci.h>
+#include <linux/init.h>
+
+#include <asm/io.h>
+
+#include "sound_config.h"
+#include "soundmodule.h"
+#include "sb.h"
+
+#ifndef SOUND_LOCK
+#define SOUND_LOCK do {} while (0)
+#define SOUND_LOCK_END do {} while (0)
+#endif
+
+#define MAX_CARDS	2
+
+#define PFX		"via82cxxx: "
+
+#define VIA_VERSION	"1.0.0"
+#define VIA_CARD_NAME	"VIA 82Cxxx Audio driver " VIA_VERSION
+
+#define VIA_FUNC_ENABLE		0x42
+#define VIA_PNP_CONTROL		0x43
+
+#define VIA_CR42_SB_ENABLE	0x01
+#define VIA_CR42_MIDI_ENABLE	0x02
+#define VIA_CR42_FM_ENABLE	0x04
+
+#define via_probe_midi probe_uart401
+#define via_attach_midi attach_uart401
+#define via_unload_midi unload_uart401
+
+static struct address_info	sb_data[MAX_CARDS];
+static struct address_info	opl3_data[MAX_CARDS];
+static unsigned			cards = 0;
+
+
+static void __init via_attach_sb(struct address_info *hw_config)
+{
+	if(!sb_dsp_init(hw_config))
+		hw_config->slots[0] = -1;
+}
+
+
+static int __init via_probe_sb(struct address_info *hw_config)
+{
+	if (check_region(hw_config->io_base, 16))
+	{
+		printk(KERN_DEBUG PFX "SBPro port 0x%x is already in use\n",
+		       hw_config->io_base);
+		return 0;
+	}
+	return sb_dsp_detect(hw_config, 0, 0);
+}
+
+
+static void via_unload_sb(struct address_info *hw_config, int unload_mpu)
+{
+	if(hw_config->slots[0]!=-1)
+		sb_dsp_unload(hw_config, unload_mpu);
+}
+
+
+static int __init via82cxxx_install (struct pci_dev *pcidev)
+{
+	int sb_io_base = 0;
+	int sb_irq = 0;
+	int sb_dma = 0;
+	int midi_base = 0;
+	u8 tmp8;
+	
+	memset (&sb_data[cards], 0, sizeof (struct address_info));
+	memset (&opl3_data[cards], 0, sizeof (struct address_info));
+
+	sb_data[cards].name = opl3_data[cards].name = VIA_CARD_NAME;
+	opl3_data[cards].irq = -1;
+
+	/* turn on features, if not already */
+	pci_read_config_byte (pcidev, VIA_FUNC_ENABLE, &tmp8);
+	tmp8 |= VIA_CR42_SB_ENABLE | VIA_CR42_MIDI_ENABLE |
+		VIA_CR42_FM_ENABLE;
+	pci_write_config_byte (pcidev, VIA_FUNC_ENABLE, tmp8);
+
+	/* read legacy PNP info byte */
+	pci_read_config_byte (pcidev, VIA_PNP_CONTROL, &tmp8);
+	pci_write_config_byte (pcidev, VIA_PNP_CONTROL, tmp8);
+	
+	switch ((tmp8 >> 6) & 0x03) {
+		case 0: sb_irq = 5; break;
+		case 1: sb_irq = 7; break;
+		case 2: sb_irq = 9; break;
+		case 3: sb_irq = 10; break;
+		default: /* do nothing */ break;
+	}
+	switch ((tmp8 >> 4) & 0x03) {
+		case 0: sb_dma = 0; break;
+		case 1: sb_dma = 1; break;
+		case 2: sb_dma = 2; break;
+		case 3: sb_dma = 3; break;
+		default: /* do nothing */ break;
+	}
+	switch ((tmp8 >> 2) & 0x03) {
+		case 0: midi_base = 0x300; break;
+		case 1: midi_base = 0x310; break;
+		case 2: midi_base = 0x320; break;
+		case 3: midi_base = 0x330; break;
+		default: /* do nothing */ break;
+	}
+	switch (tmp8 & 0x03) {
+		case 0: sb_io_base = 0x220; break;
+		case 1: sb_io_base = 0x240; break;
+		case 2: sb_io_base = 0x260; break;
+		case 3: sb_io_base = 0x280; break;
+		default: /* do nothing */ break;
+	}
+
+	udelay(100);
+	
+	printk(KERN_INFO PFX "legacy "
+	       "MIDI: 0x%X, SB: 0x%X / %d IRQ / %d DMA\n",
+		midi_base, sb_io_base, sb_irq, sb_dma);
+		
+	sb_data[cards].card_subtype = MDL_SBPRO;
+	sb_data[cards].io_base = sb_io_base;
+	sb_data[cards].irq = sb_irq;
+	sb_data[cards].dma = sb_dma;
+	
+	opl3_data[cards].io_base = midi_base;
+	
+	/* register legacy SoundBlaster Pro */
+	if (!via_probe_sb (&sb_data[cards])) {
+		printk (KERN_ERR PFX
+			"SB probe @ 0x%X failed, aborting\n",
+			sb_io_base);
+		return -1;
+	}
+	via_attach_sb (&sb_data[cards]);
+
+	/* register legacy MIDI */
+	if (!via_probe_midi (&opl3_data[cards])) {
+		printk (KERN_ERR PFX
+			"MIDI probe @ 0x%X failed, aborting\n",
+			midi_base);
+		via_unload_sb (&sb_data[cards], 0);
+		return -1;
+	}
+	via_attach_midi (&opl3_data[cards]);
+
+	cards++;	
+	return 0;
+}
+
+
+/*
+ * 	This loop walks the PCI configuration database and finds where
+ *	the sound cards are.
+ */
+ 
+static int __init probe_via82cxxx (void)
+{
+	struct pci_dev *pcidev = NULL;
+
+	while ((pcidev = pci_find_device (PCI_VENDOR_ID_VIA,
+					  PCI_DEVICE_ID_VIA_82C686_5,
+					  pcidev)) != NULL) {
+
+		  if (via82cxxx_install (pcidev) != 0) {
+			  printk (KERN_ERR PFX "audio init failed\n");
+			  return -1;
+		  }
+
+		  if (cards == MAX_CARDS) {
+		  	  printk (KERN_DEBUG PFX "maximum number of cards reached\n");
+			  break;
+		  }
+	}
+
+	return 0;
+}
+
+
+/*
+ *	This function is called when the user or kernel loads the 
+ *	module into memory.
+ */
+
+
+static int __init init_via82cxxx_module(void)
+{
+	if (!pci_present ()) {
+		printk (KERN_DEBUG PFX "PCI not present, exiting\n");
+		return -ENODEV;
+	}
+
+	if (probe_via82cxxx() != 0) {
+		printk(KERN_ERR PFX "probe failed, aborting\n");
+		/* XXX unload cards registered so far, if any */
+		return -ENODEV;
+	}
+
+	if (cards == 0) {
+		printk(KERN_DEBUG PFX "No chips found, aborting\n");
+		return -ENODEV;
+	}
+
+	printk (KERN_INFO PFX VIA_CARD_NAME " loaded\n");
+	
+	/*
+	 *	Binds us to the sound subsystem	
+	 */
+	SOUND_LOCK;
+	return 0;
+}
+
+/*
+ *	This is called when it is removed. It will only be removed 
+ *	when its use count is 0. For sound the SOUND_LOCK/SOUND_UNLOCK
+ *	macros hide the entire work for this.
+ */
+ 
+static void cleanup_via82cxxx_module(void)
+{
+	int i;
+	
+	for (i = 0; i < cards; i++)
+		via_unload_sb (&sb_data[i], 1);
+
+	/*
+	 *	Final clean up with the sound layer
+	 */
+	SOUND_LOCK_END;
+}
+
+#ifdef MODULE
+
+int init_module(void)
+{
+	return init_via82cxxx_module();
+}
+
+void cleanup_module(void)
+{
+	cleanup_via82cxxx_module();
+}
+
+#endif
diff -urN linux-2.2.13-pre3/include/linux/pci.h linux-2.2.13-pre4/include/linux/pci.h
--- linux-2.2.13-pre3/include/linux/pci.h	1999-08-25 17:29:53.000000000 -0700
+++ linux-2.2.13-pre4/include/linux/pci.h	2003-08-15 15:07:26.000000000 -0700
@@ -762,6 +762,7 @@
 #define PCI_DEVICE_ID_VIA_82C595_97	0x1595
 #define PCI_DEVICE_ID_VIA_82C586_2	0x3038
 #define PCI_DEVICE_ID_VIA_82C586_3	0x3040
+#define PCI_DEVICE_ID_VIA_82C686_5	0x3058
 #define PCI_DEVICE_ID_VIA_86C100A	0x6100
 #define PCI_DEVICE_ID_VIA_82C597_1	0x8597
 #define PCI_DEVICE_ID_VIA_82C598_1      0x8598
diff -urN linux-2.2.13-pre3/include/linux/videodev.h linux-2.2.13-pre4/include/linux/videodev.h
--- linux-2.2.13-pre3/include/linux/videodev.h	1999-05-07 11:05:30.000000000 -0700
+++ linux-2.2.13-pre4/include/linux/videodev.h	2003-08-15 15:07:26.000000000 -0700
@@ -278,6 +278,7 @@
 #define VID_HARDWARE_TYPHOON	19
 #define VID_HARDWARE_VINO	20	/* Reserved for SGI Indy Vino */
 #define VID_HARDWARE_CADET	21	/* Cadet radio */
+#define VID_HARDWARE_TRUST	22	/* Trust FM Radio */
 
 /*
  *	Initialiser list
diff -urN linux-2.2.13-pre3/MAINTAINERS linux-2.2.13-pre4/MAINTAINERS
--- linux-2.2.13-pre3/MAINTAINERS	1999-08-25 17:29:45.000000000 -0700
+++ linux-2.2.13-pre4/MAINTAINERS	2003-08-15 15:07:26.000000000 -0700
@@ -388,6 +388,12 @@
 W:	http://www.uni-mainz.de/~langm000/linux.html
 S:	Maintained
 
+IBM ServeRAID RAID DRIVER
+P:      Keith Mitchell
+M:      ipslinux@us.ibm.com
+W:      http://www.developer.ibm.com/welcome/netfinity/serveraid_beta.html
+S:      Supported 
+
 IDE DRIVER [GENERAL]
 P:	Andre Hedrick
 M:	hedrick@astro.dyer.vanderbilt.edu
@@ -841,6 +847,11 @@
 W:	http://bmrc.berkeley.edu/people/chaffee
 S:	Maintained
 
+VIA 82Cxxx AUDIO DRIVER
+P:	Jeff Garzik
+M:	jgarzik@pobox.com
+S:	Maintained
+
 VIDEO FOR LINUX
 P:	Alan Cox
 M:	Alan.Cox@linux.org
diff -urN linux-2.2.13-pre3/Makefile linux-2.2.13-pre4/Makefile
--- linux-2.2.13-pre3/Makefile	2003-08-15 15:07:25.000000000 -0700
+++ linux-2.2.13-pre4/Makefile	2003-08-15 15:07:26.000000000 -0700
@@ -1,7 +1,7 @@
 VERSION = 2
 PATCHLEVEL = 2
 SUBLEVEL = 13
-EXTRAVERSION = pre3
+EXTRAVERSION = pre4
 
 ARCH := $(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/)
 
