patch-pre2.0.3 linux/drivers/cdrom/mcdx.c

Next file: linux/drivers/cdrom/sbpcd.c
Previous file: linux/drivers/cdrom/cm206.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file pre2.0.2/linux/drivers/cdrom/mcdx.c linux/drivers/cdrom/mcdx.c
@@ -1,16 +1,7 @@
 /*
  * The Mitsumi CDROM interface
  * Copyright (C) 1995 Heiko Schlittermann <heiko@lotte.sax.de>
- * VERSION: 1.9
- *
- ****************** H E L P *********************************
- * If you ever plan to update your CD ROM drive and perhaps
- * want to sell or simply give away your Mitsumi FX-001[DS] 
- * -- Please, Please --
- * mail me (heiko@lotte.sax.de).  When my last drive goes 
- * ballistic no more driver support will be available from me !!!
- *************************************************************
- *
+ * VERSION: 2.2
  * 
  * 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
@@ -38,14 +29,14 @@
  *  Nils Faerber and Roger E. Wolff (extensively tested the LU portion)
  *  Andreas Kies (testing the mysterious hang up's)
  *  ... somebody forgotten?
- *  
+ * 
+ * 2.1  1996/04/29 Marcin Dalecki <dalecki@namu03.gwdg.de>
+ *      Far too many bugfixes/changes to mention them all separately.
+ * 2.2  1996/05/06 Marcin Dalecki <dalecki@namu03.gwdg.de>
+ *      Mostly fixes to some silly bugs in the previous release :-).
+ *      (Hi Michael Thimm! Thank's for lending me Your's double speed drive.)
  */
-
-
-#if RCS
-static const char *mcdx_c_version
-		= "mcdx.c,v 1.2 1996/03/22 01:14:59 heiko Exp";
-#endif
+#define VERSION "2.2"
 
 #include <linux/version.h>
 #include <linux/module.h>
@@ -63,202 +54,152 @@
 #include <asm/io.h>
 #include <asm/segment.h>
 
-
 #include <linux/major.h>
 #define MAJOR_NR MITSUMI_X_CDROM_MAJOR
 #include <linux/blk.h>
 
-/* for compatible parameter passing with "insmod" */ 
-#define	mcdx_drive_map mcdx    
+/* 
+ * for compatible parameter passing with "insmod" 
+ */
+#define	mcdx_drive_map mcdx
 #include <linux/mcdx.h>
 
-#ifndef HZ 
-#error HZ not defined
-#endif
-
-/* CONSTANTS *******************************************************/
+#define REQUEST_SIZE	200
+#define DIRECT_SIZE	200
 
-const int REQUEST_SIZE = 200;
-const int DIRECT_SIZE = 200;
-const unsigned long ACLOSE_INHIBIT = 800;  /* 1/100 s of autoclose inhibit */
-
-enum drivemodes { TOC, DATA, RAW, COOKED };
-enum datamodes { MODE0, MODE1, MODE2 };
-enum resetmodes { SOFT, HARD };
-
-const int SINGLE = 0x01;		/* single speed drive (FX001S, LU) */
-const int DOUBLE = 0x02;		/* double speed drive (FX001D, ..? */
-const int DOOR   = 0x04;		/* door locking capability */
-const int MULTI  = 0x08;		/* multi session capability */
-
-const unsigned char READ1X = 0xc0;
-const unsigned char READ2X = 0xc1;
-
-
-/* DECLARATIONS ****************************************************/ 
-struct s_msf {
-	unsigned char minute;
-	unsigned char second;
-	unsigned char frame;
+enum drivemodes {
+	TOC, DATA, RAW, COOKED
 };
 
+#define	MODE0 		0x00
+#define MODE1 		0x01
+#define MODE2 		0x02
+
+#define DOOR_LOCK 	0x01
+#define DOOR_UNLOCK 	0x00
+
+/*
+ * Structs used to gather info reported by the drive.
+ */
+
 struct s_subqcode {
-	unsigned char control;
-	unsigned char tno;
-	unsigned char index;
-	struct s_msf tt;
-	struct s_msf dt;
+	u_char adr:4;
+	u_char ctrl:4;
+	u_char tno;
+	u_char index;
+	struct cdrom_msf0 tt;
+	u_char dummy;		/* padding for matching the returned struct */
+	struct cdrom_msf0 dt;
 };
 
-struct s_diskinfo {
-	unsigned int n_first;
-	unsigned int n_last;
-	struct s_msf msf_leadout;
-	struct s_msf msf_first;
-};
-	
 struct s_multi {
 	unsigned char multi;
-	struct s_msf msf_last;
+	struct cdrom_msf0 msf_last;
 };
 
-struct s_version {
-	unsigned char code;
-	unsigned char ver;
+struct s_play {
+	struct cdrom_msf0 start;
+	struct cdrom_msf0 stop;
 };
 
-/* Per drive/controller stuff **************************************/
+/* 
+ * Per drive/controller stuff.
+ */
 
 struct s_drive_stuff {
-	/* waitqueues */
-    struct wait_queue *busyq;
-    struct wait_queue *lockq;
-    struct wait_queue *sleepq;
-
- 	/* flags */
-    volatile int introk;	/* status of last irq operation */
-    volatile int busy;		/* drive performs an operation */
-    volatile int lock;		/* exclusive usage */
-    int eject_sw;           /* 1 - eject on last close (default 0) */
-    int autoclose;          /* 1 - close the door on open (default 1) */
-    
+	struct wait_queue *busyq;
+	
+	/* flags */
+	u_char introk:1;	/* status of last irq operation */
+	u_char busy:1;		/* drive performs an operation */
+	u_char eject_sw:1;	/* 1 - eject on last close (default 0) */
+	u_char autoclose:1;	/* 1 - close the door on open (default 1) */
+	u_char xxx:1;		/* set if changed, reset while open */
+	u_char xa:1;		/* 1 if xa disk */
+	u_char audio:1;		/* 1 if audio disk */
+	u_char eom:1;		/* end of media reached during read request */
+
+	/* drives capabilities */
+	u_char door:1;		/* can close/lock tray */
+	u_char multi_cap:1;	/* multisession capable */
+	u_char double_speed:1;	/* double speed drive */
+
 	/* cd infos */
-	struct s_diskinfo di;
+	unsigned int n_first;
+	unsigned int n_last;
+	struct cdrom_msf0 msf_leadout;
 	struct s_multi multi;
-	struct s_subqcode* toc;	/* first entry of the toc array */
-	struct s_subqcode start;
-    struct s_subqcode stop;
-	int xa;					/* 1 if xa disk */
-	int audio;				/* 1 if audio disk */
-	int audiostatus;			
+
+	struct s_subqcode *toc;	/* first entry of the toc array */
+	struct s_play resume;	/* where to resume after pause */
+
+	int audiostatus;
 
 	/* `buffer' control */
-    volatile int valid;
-    volatile int pending;
-    volatile int off_direct;
-    volatile int off_requested;
-
-	/* adds and odds */
-	void* wreg_data;	/* w data */
-	void* wreg_reset;	/* w hardware reset */
-	void* wreg_hcon;	/* w hardware conf */
-	void* wreg_chn;		/* w channel */
-	void* rreg_data;	/* r data */
-	void* rreg_status;	/* r status */
-
-    int irq;			/* irq used by this drive */
-    int minor;			/* minor number of this drive */
-    int present;	    /* drive present and its capabilities */
-    unsigned char readcmd;	/* read cmd depends on single/double speed */
-    unsigned char playcmd;  /* play should always be single speed */
-    unsigned int xxx;      /* set if changed, reset while open */
-    unsigned int yyy;      /* set if changed, reset by media_changed */
-    unsigned long ejected;  /* time we called the eject function */
-    int users;				/* keeps track of open/close */
-    int lastsector;			/* last block accessible */
-    int status;				/* last operation's error / status */
-	int readerrs;			/* # of blocks read w/o error */
+	unsigned int valid:1;
+	int pending;
+	int off_direct;
+	int off_requested;
+
+	int irq;		/* irq used by this drive */
+	unsigned int base;	/* base for all registers of the drive */
+	int users;		/* keeps track of open/close */
+	int lastsector;		/* last accessible blocks */
 };
 
+/*
+ * Macros for accessing interface registers
+ */
 
-/* Prototypes ******************************************************/ 
-
-/*	The following prototypes are already declared elsewhere.  They are
- 	repeated here to show what's going on.  And to sense, if they're
-	changed elsewhere. */
+#define DATA_REG	(stuffp->base)
+#define RESET_REG	(stuffp->base+1)
+#define STAT_REG	(stuffp->base+1)
+#define CHAN_REG	(stuffp->base+3)
 
-/* declared in blk.h */
+/* 
+ * declared in blk.h 
+ */
 int mcdx_init(void);
 void do_mcdx_request(void);
-int check_mcdx_media_change(kdev_t);
 
-/* already declared in init/main */
+/* 
+ * already declared in init/main 
+ */
 void mcdx_setup(char *, int *);
 
-/*	Indirect exported functions. These functions are exported by their
-	addresses, such as mcdx_open and mcdx_close in the 
-	structure fops. */
-
-/* ???  exported by the mcdx_sigaction struct */
-static void mcdx_intr(int, void *, struct pt_regs*);
-
-/* exported by file_ops */
-static int mcdx_open(struct inode*, struct file*);
-static void mcdx_close(struct inode*, struct file*);
-static int mcdx_ioctl(struct inode*, struct file*, unsigned int, unsigned long);
-
-/* misc internal support functions */
-static void log2msf(unsigned int, struct s_msf*);
-static unsigned int msf2log(const struct s_msf*);
-static unsigned int uint2bcd(unsigned int);
-static unsigned int bcd2uint(unsigned char);
-#if MCDX_DEBUG
-static void TRACE((int level, const char* fmt, ...));
-#endif
-static void warn(const char* fmt, ...);
-static char *port(int*);
-static int irq(int*);
-static void mcdx_delay(struct s_drive_stuff*, long jifs);
-static int mcdx_transfer(struct s_drive_stuff*, char* buf, int sector, int nr_sectors);
-static int mcdx_xfer(struct s_drive_stuff*, char* buf, int sector, int nr_sectors);
-
-static int mcdx_config(struct s_drive_stuff*, int);
-static int mcdx_closedoor(struct s_drive_stuff*, int);
-static int mcdx_requestversion(struct s_drive_stuff*, struct s_version*, int);
-static int mcdx_lockdoor(struct s_drive_stuff*, int, int);
-static int mcdx_stop(struct s_drive_stuff*, int);
-static int mcdx_hold(struct s_drive_stuff*, int);
-static int mcdx_reset(struct s_drive_stuff*, enum resetmodes, int);
-static int mcdx_eject(struct s_drive_stuff*, int);
-static int mcdx_setdrivemode(struct s_drive_stuff*, enum drivemodes, int);
-static int mcdx_setdatamode(struct s_drive_stuff*, enum datamodes, int);
-static int mcdx_requestsubqcode(struct s_drive_stuff*, struct s_subqcode*, int);
-static int mcdx_requestmultidiskinfo(struct s_drive_stuff*, struct s_multi*, int);
-static int mcdx_requesttocdata(struct s_drive_stuff*, struct s_diskinfo*, int);
-static int mcdx_getstatus(struct s_drive_stuff*, int);
-static int mcdx_getval(struct s_drive_stuff*, int to, int delay, char*);
-static int mcdx_talk(struct s_drive_stuff*, 
-		const unsigned char* cmd, size_t, 
-        void *buffer, size_t size, 
-        unsigned int timeout, int);
-static int mcdx_readtoc(struct s_drive_stuff*);
-static int mcdx_playtrk(struct s_drive_stuff*, const struct cdrom_ti*);
-static int mcdx_playmsf(struct s_drive_stuff*, const struct cdrom_msf*);
-static int mcdx_setattentuator(struct s_drive_stuff*, struct cdrom_volctrl*, int);
+/*      
+ * Indirect exported functions. These functions are exported by their
+ * addresses, such as mcdx_open and mcdx_close in the 
+ *  structure fops. 
+ */
+
+/* 
+ * ???  exported by the mcdx_sigaction struct 
+ */
+static void mcdx_intr(int, void *, struct pt_regs *);
+
+/* 
+   * exported by file_ops 
+ */
+static int mcdx_open(struct inode *, struct file *);
+static void mcdx_close(struct inode *, struct file *);
+static int mcdx_ioctl(struct inode *, struct file *,
+		      unsigned int, unsigned long);
+static int mcdx_media_change(kdev_t);
 
-/* static variables ************************************************/
 
 static int mcdx_blocksizes[MCDX_NDRIVES];
 static int mcdx_drive_map[][2] = MCDX_DRIVEMAP;
-static struct s_drive_stuff* mcdx_stuffp[MCDX_NDRIVES];
-static struct s_drive_stuff* mcdx_irq_map[16] =
-		{0, 0, 0, 0, 0, 0, 0, 0,
-		0, 0, 0, 0, 0, 0, 0, 0};
+static struct s_drive_stuff *mcdx_stuffp[MCDX_NDRIVES];
+static struct s_drive_stuff *mcdx_irq_map[16] =
+{0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0};
 
-static struct file_operations mcdx_fops = {
+static struct file_operations mcdx_fops =
+{
 	NULL,			/* lseek - use kernel default */
 	block_read,		/* read - general block-dev read */
-	block_write,	/* write - general block-dev write */
+	block_write,		/* write - general block-dev write */
 	NULL,			/* no readdir */
 	NULL,			/* no select */
 	mcdx_ioctl,		/* ioctl() */
@@ -266,1591 +207,1191 @@
 	mcdx_open,		/* open() */
 	mcdx_close,		/* close() */
 	NULL,			/* fsync */
-	NULL,                   /* fasync */
-	check_mcdx_media_change, /* media_change */
-	NULL                    /* revalidate */
+	NULL,			/* fasync */
+	mcdx_media_change,	/* media_change */
+	NULL			/* revalidate */
 };
 
-/* KERNEL INTERFACE FUNCTIONS **************************************/ 
+/*     
+ * Misc number converters 
+ */
 
-static int 
-mcdx_ioctl(
-	struct inode* ip, struct file* fp, 
-	unsigned int cmd, unsigned long arg)
-{ 
-	struct s_drive_stuff *stuffp = mcdx_stuffp[MINOR(ip->i_rdev)];
+static unsigned int bcd2uint(unsigned char c)
+{
+	return (c >> 4) * 10 + (c & 0x0f);
+}
 
-	if (!stuffp->present) return -ENXIO;
-	if (!ip) return -EINVAL;
+static unsigned int uint2bcd(unsigned int ival)
+{
+	return ((ival / 10) << 4) | (ival % 10);
+}
 
-	switch (cmd) {
-		case CDROMSTART: {
-			TRACE((IOCTL, "ioctl() START\n"));
-			return 0;
-		}
+static unsigned int msf2log(const struct cdrom_msf0 *pmsf)
+{
+	return bcd2uint(pmsf->frame)
+	    + bcd2uint(pmsf->second) * 75
+	    + bcd2uint(pmsf->minute) * 4500
+	    - CD_BLOCK_OFFSET;
+}
 
-		case CDROMSTOP: {
-			TRACE((IOCTL, "ioctl() STOP\n"));
-            stuffp->audiostatus = CDROM_AUDIO_INVALID;
-			if (-1 == mcdx_stop(stuffp, 1))
-				return -EIO;
-			return 0;
-		}
+/*      
+ * Access to elements of the mcdx_drive_map members 
+ */
+static inline unsigned int port(int *ip)
+{
+	return (unsigned int) ip[0];
+}
 
-		case CDROMPLAYTRKIND: {
-			int ans;
-			struct cdrom_ti ti;
+static inline int irq(int *ip)
+{
+	return ip[1];
+}
 
-			TRACE((IOCTL, "ioctl() PLAYTRKIND\n"));
-			if ((ans = verify_area(VERIFY_READ, (void*) arg, sizeof(ti))))
-				return ans;
-			memcpy_fromfs(&ti, (void*) arg, sizeof(ti));
-			if ((ti.cdti_trk0 < stuffp->di.n_first)
-					|| (ti.cdti_trk0 > stuffp->di.n_last)
-					|| (ti.cdti_trk1 < stuffp->di.n_first))
-				return -EINVAL;
-			if (ti.cdti_trk1 > stuffp->di.n_last) ti.cdti_trk1 = stuffp->di.n_last;
-            TRACE((PLAYTRK, "ioctl() track %d to %d\n", ti.cdti_trk0, ti.cdti_trk1));
-
-            return mcdx_playtrk(stuffp, &ti);
-        }
-
-        case CDROMPLAYMSF: {
-            int ans;
-            struct cdrom_msf msf;
-
-            TRACE((IOCTL, "ioctl() PLAYMSF\n"));
-
-            if ((stuffp->audiostatus == CDROM_AUDIO_PLAY)
-                && (-1 == mcdx_hold(stuffp, 1))) return -EIO;
-
-            if ((ans = verify_area(
-                    VERIFY_READ, (void*) arg, sizeof(struct cdrom_msf)))) 
-                return ans;
-
-            memcpy_fromfs(&msf, (void*) arg, sizeof msf);
-
-            msf.cdmsf_min0 = uint2bcd(msf.cdmsf_min0);
-            msf.cdmsf_sec0 = uint2bcd(msf.cdmsf_sec0);
-            msf.cdmsf_frame0 = uint2bcd(msf.cdmsf_frame0);
-
-            msf.cdmsf_min1 = uint2bcd(msf.cdmsf_min1);
-            msf.cdmsf_sec1 = uint2bcd(msf.cdmsf_sec1);
-            msf.cdmsf_frame1 = uint2bcd(msf.cdmsf_frame1);
-
-            return mcdx_playmsf(stuffp, &msf);
-        }
-
-        case CDROMRESUME: {
-            TRACE((IOCTL, "ioctl() RESUME\n"));
-            return mcdx_playtrk(stuffp, NULL);
-        }
-
-		case CDROMREADTOCENTRY: {
-			struct cdrom_tocentry entry;
-			struct s_subqcode *tp = NULL;
-			int ans;
 
-			TRACE((IOCTL, "ioctl() READTOCENTRY\n"));
+/*
+ * Low level hardware related functions.
+ */
+
+/*
+ * Return drives status in case of success, -1 otherwise.
+ *
+ * First we try to get the status information quickly.
+ * Then we sleep repeatedly for about 10 usecs, befor we finally reach the 
+ * timeout. For this reason this command must be called with the drive beeing 
+ * locked!
+ */
+static int get_status(struct s_drive_stuff *stuffp,
+		      unsigned long timeout)
+{
+	unsigned long bang = jiffies + 2;
+	timeout += jiffies;
 
-            if (-1 == mcdx_readtoc(stuffp)) return -1;
+	do {
+		if (!(inb(STAT_REG) & MCDX_RBIT_STEN)) {
+			return (inb(DATA_REG) & 0xff);
+		}
+	} while (jiffies < bang);
 
-			if ((ans = verify_area(VERIFY_READ, (void *) arg, sizeof(entry)))) return ans;
-			memcpy_fromfs(&entry, (void *) arg, sizeof(entry));
+	while (inb(STAT_REG) & MCDX_RBIT_STEN) {
+		if (jiffies > timeout)
+			return -1;
+		current->state = TASK_INTERRUPTIBLE;
+		current->timeout = jiffies + HZ / 2;
+		schedule();
+	}
+	return (inb(DATA_REG) & 0xff);
+}
 
-			if (entry.cdte_track == CDROM_LEADOUT) 
-				tp = &stuffp->toc[stuffp->di.n_last - stuffp->di.n_first + 1];
-			else if (entry.cdte_track > stuffp->di.n_last 
-					|| entry.cdte_track < stuffp->di.n_first) return -EINVAL;
-			else tp = &stuffp->toc[entry.cdte_track - stuffp->di.n_first];
-
-			if (NULL == tp) WARN(("FATAL.\n"));
-
-			entry.cdte_adr = tp->control;
-			entry.cdte_ctrl = tp->control >> 4;
-
-			if (entry.cdte_format == CDROM_MSF) {
-				entry.cdte_addr.msf.minute = bcd2uint(tp->dt.minute);
-				entry.cdte_addr.msf.second = bcd2uint(tp->dt.second);
-				entry.cdte_addr.msf.frame = bcd2uint(tp->dt.frame);
-			} else if (entry.cdte_format == CDROM_LBA)
-				entry.cdte_addr.lba = msf2log(&tp->dt);
-			else return -EINVAL;
+/* Send a command to the drive, wait for the result.
+ * returns -1 on timeout, drive status otherwise.
+ * If buffer is not zero, the result (length size) is stored there.
+ * If buffer is zero the size should be the number of bytes to read
+ * from the drive.  These bytes are discarded.
+ */
+static int talk(struct s_drive_stuff *stuffp,
+		const unsigned char command,
+		void *pars, size_t parslen,
+		void *buffer, size_t size,
+		unsigned int timeout)
+{
+	int st;
 
-			if ((ans = verify_area(VERIFY_WRITE, (void*) arg, sizeof(entry)))) return ans;
-			memcpy_tofs((void*) arg, &entry, sizeof(entry));
+	while (stuffp->busy) {
+		interruptible_sleep_on(&stuffp->busyq);
+	}
+	stuffp->busy = 1;
+	stuffp->valid = 0;
+	outb(command, DATA_REG);
+	if (parslen)
+		outsb(DATA_REG, pars, parslen);
 
-			return 0;
+	if (-1 == (st = get_status(stuffp, timeout))) {
+		goto end_talk;
+	}
+	if (st & MCDX_RBIT_CMDERR) {
+		printk(KERN_ERR MCDX ": error in command 0x%2x\n", command);
+		st = -1;
+		goto end_talk;
+	}
+	/* audio status? */
+	if (stuffp->audiostatus == CDROM_AUDIO_INVALID) {
+		stuffp->audiostatus =
+		    (st & MCDX_RBIT_AUDIOBS) ? CDROM_AUDIO_PLAY : CDROM_AUDIO_NO_STATUS;
+	} else if (stuffp->audiostatus == CDROM_AUDIO_PLAY
+		   && !(st & MCDX_RBIT_AUDIOBS)) {
+		stuffp->audiostatus = CDROM_AUDIO_COMPLETED;
+	}
+	/* media change? */
+	if (st & MCDX_RBIT_CHANGED) {
+		stuffp->xxx = 1;
+		if (stuffp->toc) {
+			kfree(stuffp->toc);
+			stuffp->toc = 0;
+		}
+	}
+	/* now actually get the data */
+	while (size--) {
+		if (-1 == (st = get_status(stuffp, timeout))) {
+			break;
 		}
+		*((char *) buffer) = st;
+		buffer++;
+	}
 
-		case CDROMSUBCHNL: {
-			int ans;
-			struct cdrom_subchnl sub;
-			struct s_subqcode q;
+      end_talk:
+	stuffp->busy = 0;
+	wake_up_interruptible(&stuffp->busyq);
 
-			TRACE((IOCTL, "ioctl() SUBCHNL\n"));
+	return st;
+}
 
-			if ((ans = verify_area(VERIFY_READ, 
-                    (void*) arg, sizeof(sub)))) return ans;
+static int issue_command(struct s_drive_stuff *stuffp,
+			 unsigned char command,
+			 unsigned int timeout)
+{
+	return talk(stuffp, command, 0, 0, NULL, 0, timeout);
+}
 
-			memcpy_fromfs(&sub, (void*) arg, sizeof(sub));
-
-			if (-1 == mcdx_requestsubqcode(stuffp, &q, 2)) return -EIO;
-
-            TRACE((SUBCHNL, "audiostatus: %x\n", stuffp->audiostatus));
-			sub.cdsc_audiostatus = stuffp->audiostatus;
-			sub.cdsc_adr = q.control;
-			sub.cdsc_ctrl = q.control >> 4;
-			sub.cdsc_trk = bcd2uint(q.tno);
-			sub.cdsc_ind = bcd2uint(q.index);
-
-            TRACE((SUBCHNL, "trk %d, ind %d\n", 
-                    sub.cdsc_trk, sub.cdsc_ind));
-
-			if (sub.cdsc_format == CDROM_LBA) {
-				sub.cdsc_absaddr.lba = msf2log(&q.dt);
-				sub.cdsc_reladdr.lba = msf2log(&q.tt);
-                TRACE((SUBCHNL, "lba: abs %d, rel %d\n",
-                    sub.cdsc_absaddr.lba,
-                    sub.cdsc_reladdr.lba));
-			} else if (sub.cdsc_format == CDROM_MSF) {
-				sub.cdsc_absaddr.msf.minute = bcd2uint(q.dt.minute);
-				sub.cdsc_absaddr.msf.second = bcd2uint(q.dt.second);
-				sub.cdsc_absaddr.msf.frame = bcd2uint(q.dt.frame);
-				sub.cdsc_reladdr.msf.minute = bcd2uint(q.tt.minute);
-				sub.cdsc_reladdr.msf.second = bcd2uint(q.tt.second);
-				sub.cdsc_reladdr.msf.frame = bcd2uint(q.tt.frame);
-                TRACE((SUBCHNL,
-                        "msf: abs %02d:%02d:%02d, rel %02d:%02d:%02d\n",
-                        sub.cdsc_absaddr.msf.minute,
-                        sub.cdsc_absaddr.msf.second,
-                        sub.cdsc_absaddr.msf.frame,
-                        sub.cdsc_reladdr.msf.minute,
-                        sub.cdsc_reladdr.msf.second,
-                        sub.cdsc_reladdr.msf.frame));
-			} else return -EINVAL;
-
-			if ((ans = verify_area(VERIFY_WRITE, (void*) arg, sizeof(sub))))
-				return ans;
-			memcpy_tofs((void*) arg, &sub, sizeof(sub));
+static inline int set_command(struct s_drive_stuff *stuffp,
+			      const unsigned char command,
+			      void *pars, size_t parlen,
+			      unsigned int timeout)
+{
+	return talk(stuffp, command, pars, parlen, NULL, 0, timeout);
+}
 
-			return 0;
-		}
+static inline int get_command(struct s_drive_stuff *stuffp,
+			      const unsigned char command,
+			      void *buffer, size_t size,
+			      unsigned int timeout)
+{
+	return talk(stuffp, command, NULL, 0, buffer, size, timeout);
+}
 
-		case CDROMREADTOCHDR: {
-			struct cdrom_tochdr toc;
-			int ans;
+static int request_subq_code(struct s_drive_stuff *stuffp,
+			     struct s_subqcode *sub)
+{
+	return get_command(stuffp, MCDX_CMD_GET_SUBQ_CODE,
+			   sub, sizeof(struct s_subqcode), 2 * HZ);
+}
 
-			TRACE((IOCTL, "ioctl() READTOCHDR\n"));
-			if ((ans = verify_area(VERIFY_WRITE, (void*) arg, sizeof toc)))
-				return ans;
-			toc.cdth_trk0 = stuffp->di.n_first;
-			toc.cdth_trk1 = stuffp->di.n_last;
-			memcpy_tofs((void*) arg, &toc, sizeof toc);
-			TRACE((TOCHDR, "ioctl() track0 = %d, track1 = %d\n",
-					stuffp->di.n_first, stuffp->di.n_last));
-			return 0;
-		}
+static int request_toc_data(struct s_drive_stuff *stuffp)
+{
+	char buf[8];
+	int ans;
 
-		case CDROMPAUSE: {
-			TRACE((IOCTL, "ioctl() PAUSE\n"));
-			if (stuffp->audiostatus != CDROM_AUDIO_PLAY) return -EINVAL;
-			if (-1 == mcdx_stop(stuffp, 1)) return -EIO;
-            stuffp->audiostatus = CDROM_AUDIO_PAUSED;
-			if (-1 == mcdx_requestsubqcode(stuffp, &stuffp->start, 1))
-				return -EIO;
-			return 0;
-		}
+	ans = get_command(stuffp, MCDX_CMD_GET_TOC, buf, sizeof(buf), 2 * HZ);
+	if (ans == -1) {
+		stuffp->n_first = 0;
+		stuffp->n_last = 0;
+	} else {
+		stuffp->n_first = bcd2uint(buf[0]);
+		stuffp->n_last = bcd2uint(buf[1]);
+		memcpy(&(stuffp->msf_leadout), buf + 2, 3);
+	}
+	return ans;
+}
 
-		case CDROMMULTISESSION: {
-			int ans;
-			struct cdrom_multisession ms;
-			TRACE((IOCTL, "ioctl() MULTISESSION\n"));
-			if (0 != (ans = verify_area(VERIFY_READ, (void*) arg, 
-					sizeof(struct cdrom_multisession))))
-				return ans;
-				
-			memcpy_fromfs(&ms, (void*) arg, sizeof(struct cdrom_multisession));
-			if (ms.addr_format == CDROM_MSF) {
-				ms.addr.msf.minute = bcd2uint(stuffp->multi.msf_last.minute);
-				ms.addr.msf.second = bcd2uint(stuffp->multi.msf_last.second);
-				ms.addr.msf.frame = bcd2uint(stuffp->multi.msf_last.frame);
-			} else if (ms.addr_format == CDROM_LBA)
-				ms.addr.lba = msf2log(&stuffp->multi.msf_last);
-			else
-				return -EINVAL;
-			ms.xa_flag = !!stuffp->multi.multi;
-
-			if (0 != (ans = verify_area(VERIFY_WRITE, (void*) arg,
-					sizeof(struct cdrom_multisession))))
-				return ans;
-
-			memcpy_tofs((void*) arg, &ms, sizeof(struct cdrom_multisession));
-			if (ms.addr_format == CDROM_MSF) 
-				TRACE((MS, 
-						"ioctl() (%d, %02x:%02x.%02x [%02x:%02x.%02x])\n",
-						ms.xa_flag, 
-						ms.addr.msf.minute,
-						ms.addr.msf.second,
-						ms.addr.msf.frame,
-						stuffp->multi.msf_last.minute,
-						stuffp->multi.msf_last.second,
-						stuffp->multi.msf_last.frame));
-			else
-			  {
-			    TRACE((MS, 
-					"ioctl() (%d, 0x%08x [%02x:%02x.%02x])\n",
-					ms.xa_flag,
-					ms.addr.lba,
-					stuffp->multi.msf_last.minute,
-					stuffp->multi.msf_last.second,
-					stuffp->multi.msf_last.frame));
-			  }
-			return 0;
-		}
+static int set_drive_mode(struct s_drive_stuff *stuffp, enum drivemodes mode)
+{
+	char value;
+	if (-1 == get_command(stuffp, MCDX_CMD_GET_DRIVE_MODE,
+			      &value, 1, 5 * HZ))
+		return -1;
+	switch (mode) {
+	case TOC:
+		value |= 0x04;
+		break;
+	case DATA:
+		value &= ~0x04;
+		break;
+	case RAW:
+		value |= 0x40;
+		break;
+	case COOKED:
+		value &= ~0x40;
+		break;
+	default:
+		break;
+	}
+	return set_command(stuffp, MCDX_CMD_SET_DRIVE_MODE, &value, 1, 5 * HZ);
+}
 
-		case CDROMEJECT: {
-			TRACE((IOCTL, "ioctl() EJECT\n"));
-			if (stuffp->users > 1) return -EBUSY;
-			if (-1 == mcdx_eject(stuffp, 1)) return -EIO;
-			return 0;
-		}
+static int config_drive(struct s_drive_stuff *stuffp)
+{
+	unsigned char buf[2];
+	buf[0] = 0x10;		/* irq enable */
+	buf[1] = 0x05;		/* pre, err irq enable */
 
-        case CDROMEJECT_SW: {
-            stuffp->eject_sw = arg;
-            return 0;
-        }
-
-        case CDROMVOLCTRL: {
-            int ans;
-            struct cdrom_volctrl volctrl;
-
-            TRACE((IOCTL, "ioctl() VOLCTRL\n"));
-            if ((ans = verify_area(
-                    VERIFY_READ, 
-                    (void*) arg,
-                    sizeof(volctrl))))
-                return ans;
-
-            memcpy_fromfs(&volctrl, (char *) arg, sizeof(volctrl));
-            return mcdx_setattentuator(stuffp, &volctrl, 1);
-        }
+	if (-1 == set_command(stuffp, MCDX_CMD_CONFIG, buf,
+			      sizeof(buf), 1 * HZ))
+		return -1;
 
-		default:
-			WARN(("ioctl(): unknown request 0x%04x\n", cmd));
-	    	return -EINVAL;
-	}
+	buf[0] = 0x02;		/* dma select */
+	buf[1] = 0x00;		/* no dma */
+
+	return set_command(stuffp, MCDX_CMD_CONFIG, buf, sizeof(buf), 1 * HZ);
 }
 
-void do_mcdx_request()
+/*  
+ * Read the toc entries from the CD.
+ * Return: -1 on failure, else 0 
+ */
+int read_toc(struct s_drive_stuff *stuffp)
 {
-    int dev;
-    struct s_drive_stuff *stuffp;
+	int trk;
+	int retries;
+
+	if (stuffp->toc)
+		return 0;
+	if (-1 == issue_command(stuffp, MCDX_CMD_HOLD, 2 * HZ))
+		return -1;
+	if (-1 == set_drive_mode(stuffp, TOC))
+		return -EIO;
 
-  again:
+	/* all seems to be ok so far ... malloc */
+	stuffp->toc = kmalloc(sizeof(struct s_subqcode) *
+		     (stuffp->n_last - stuffp->n_first + 2), GFP_KERNEL);
+	if (!stuffp->toc) {
+		printk(KERN_ERR MCDX ": malloc for toc failed\n");
+		set_drive_mode(stuffp, DATA);
+		return -EIO;
+	}
+	/* now read actually the index tarcks */
+	for (trk = 0;
+	     trk < (stuffp->n_last - stuffp->n_first + 1);
+	     trk++)
+		stuffp->toc[trk].index = 0;
+
+	for (retries = 300; retries; retries--) {	/* why 300? */
+		struct s_subqcode q;
+		unsigned int idx;
 
-	TRACE((REQUEST, "do_request()\n"));
+		if (-1 == request_subq_code(stuffp, &q)) {
+			set_drive_mode(stuffp, DATA);
+			return -EIO;
+		}
+		idx = bcd2uint(q.index);
 
-	if ((CURRENT == NULL) || (CURRENT->rq_status == RQ_INACTIVE))  {
-		TRACE((REQUEST, "do_request() done\n"));
-		return;
+		if ((idx > 0)
+		    && (idx <= stuffp->n_last)
+		    && (q.tno == 0)
+		    && (stuffp->toc[idx - stuffp->n_first].index == 0)) {
+			stuffp->toc[idx - stuffp->n_first] = q;
+			trk--;
+		}
+		if (trk == 0)
+			break;
 	}
+	memset(&stuffp->toc[stuffp->n_last - stuffp->n_first + 1],
+	       0, sizeof(stuffp->toc[0]));
+	stuffp->toc[stuffp->n_last - stuffp->n_first + 1].dt
+	    = stuffp->msf_leadout;
 
-    stuffp = mcdx_stuffp[MINOR(CURRENT->rq_dev)];
-	TRACE((REQUEST, "do_request() stuffp = %p\n", stuffp));
+	/* unset toc mode */
+	if (-1 == set_drive_mode(stuffp, DATA))
+		return -EIO;
 
-    INIT_REQUEST;
-    dev = MINOR(CURRENT->rq_dev);
+	return 0;
+}
 
-	if ((dev < 0) || (dev >= MCDX_NDRIVES) || (!stuffp->present)) {
-		WARN(("do_request(): bad device: %s\n", 
-            kdevname(CURRENT->rq_dev)));
-		end_request(0);
-		goto again;
-    }
+/*
+ * Return 0 on success, error value -1 otherwise.
+ */
+static int play_track(struct s_drive_stuff *stuffp, const struct cdrom_ti *ti)
+{
+	struct s_play times;
 
-	if (stuffp->audio) {
-		WARN(("do_request() attempt to read from audio cd\n"));
-		end_request(0);
-		goto again;
+	if (ti) {
+		if (-1 == read_toc(stuffp)) {
+			stuffp->audiostatus = CDROM_AUDIO_ERROR;
+			return -EIO;
+		}
+		times.start = stuffp->toc[ti->cdti_trk0 - stuffp->n_first].dt;
+		times.stop = stuffp->resume.stop =
+		    stuffp->toc[ti->cdti_trk1 - stuffp->n_first + 1].dt;
+	} else {
+		times = stuffp->resume;
 	}
+	if (-1 == set_command(stuffp, MCDX_CMD_PLAY,
+			      &times, sizeof(times), 5 * HZ)) {
+		printk(KERN_WARNING MCDX ": play track timeout\n");
+		stuffp->audiostatus = CDROM_AUDIO_ERROR;
+		return -EIO;
+	}
+	stuffp->audiostatus = CDROM_AUDIO_PLAY;
 
-    switch (CURRENT->cmd) {
-      case WRITE:
-	  WARN(("do_request(): attempt to write to cd!!\n"));
-	  end_request(0);
-	  break;
-
-      case READ:
-	  stuffp->status = 0;
-	  while (CURRENT->nr_sectors) {
-	      int i;
-
-	      if (-1 == (i = mcdx_transfer(
-				      stuffp,
-				      CURRENT->buffer,
-				      CURRENT->sector,
-				      CURRENT->nr_sectors))) {
-              /*INFO(("do_request() read error\n"));*/
-			  xwarn("do_request() read error\n");
-              if (stuffp->status & MCDX_ST_EOM) {
-                  CURRENT->sector += CURRENT->nr_sectors;
-                  CURRENT->nr_sectors = 0;
-              } 
-			  invalidate_buffers(CURRENT->rq_dev);
-              end_request(0);
-              goto again;
-	      }
-	      CURRENT->sector += i;
-	      CURRENT->nr_sectors -= i;
-	      CURRENT->buffer += (i * 512);
-
-	  }
-
-	  end_request(1);
-	  break;
-
-      default:
-	  panic(MCDX "do_request: unknown command.\n");
-	  break;
-    }
-
-    goto again;
-}
-
-static int 
-mcdx_open(struct inode *ip, struct file *fp)
-/*  actions done on open:
- *  1)  get the drives status */
-{
-    struct s_drive_stuff *stuffp;
-
-	TRACE((OPENCLOSE, "open()\n"));
-    stuffp = mcdx_stuffp[MINOR(ip->i_rdev)];
-    if (!stuffp->present) return -ENXIO;
+	return 0;
+}
 
-    /* this is only done to test if the drive talks with us */
-    if (-1 == mcdx_getstatus(stuffp, 1)) return -EIO;
+static int lock_door(struct s_drive_stuff *stuffp, u_char lock)
+{
+	if (stuffp->door)
+		return set_command(stuffp, MCDX_CMD_LOCK_DOOR,
+				   &lock, sizeof(lock), 5 * HZ);
+	return 0;
+}				/* 
+				 * KERNEL INTERFACE FUNCTIONS
+				 */
+static int mcdx_ioctl(struct inode *ip, struct file *fp,
+		      unsigned int command, unsigned long arg)
+{
+	int ans;
+	struct cdrom_ti ti;
+	struct cdrom_msf msf;
+	struct cdrom_tocentry entry;
+	struct s_subqcode *tp = NULL;
+	struct cdrom_subchnl sub;
+	struct s_subqcode q;
+	struct cdrom_tochdr toc;
+	struct cdrom_multisession ms;
+	struct cdrom_volctrl volctrl;
+	struct s_drive_stuff *stuffp = mcdx_stuffp[MINOR(ip->i_rdev)];
 
-	/* close the door, if necessary (get the door information
-    from the hardware status register). 
-    If the last eject is too recent, an autoclose wouldn't probably
-    be what we want ..., if we can't read the CD after an autoclose
-    no further autocloses will be tried */
-	if (inb((unsigned int) stuffp->rreg_status) & MCDX_RBIT_DOOR) {
-        if (jiffies - stuffp->ejected < ACLOSE_INHIBIT) return -EIO;
-        if (stuffp->autoclose) mcdx_closedoor(stuffp, 1);
-		else return -EIO;
-    }
+	MCDX_TRACE_IOCTL(("mcdx_ioctl():\n"));
 
-	/* if the media changed we will have to do a little more */
-	if (stuffp->xxx) {
+	if (!stuffp)
+		return -ENXIO;
+	if (!ip)
+		return -EINVAL;
+
+	/*
+	 * Update disk information, when necessary.
+	 * This part will only work, when the new disk is of the same type as 
+	 * the one which was previousley there, esp. also for audio diks.
+	 * This doesn't hurt us, since otherwise the mouting/unmounting scheme 
+	 * will ensure correct operation.
+	 */
+	if (stuffp->xxx) {	/* disk changed */
+		if ((-1 == request_toc_data(stuffp)) ||
+		    (-1 == read_toc(stuffp)))
+			return -EIO;
+		stuffp->xxx = 0;
+	}
+	switch (command) {
+	case CDROMSTART:	/* spin up the drive */
+		MCDX_TRACE_IOCTL(("CDROMSTART\n"));
+		/* Don't think we can do this.  Even if we could,
+		 * I think the drive times out and stops after a while
+		 * anyway.  For now, ignore it.
+		 */
+		return 0;
 
-		TRACE((OPENCLOSE, "open() media changed\n"));
-        /* but wait - the time of media change will be set at the 
-           very last of this block - it seems, some of the following
-           talk() will detect a media change ... (I think, config()
-           is the reason. */
+	case CDROMSTOP:
+		MCDX_TRACE_IOCTL(("CDROMSTOP\n"));
+		stuffp->audiostatus = CDROM_AUDIO_INVALID;
+		if (-1 == issue_command(stuffp, MCDX_CMD_STOP, 2 * HZ))
+			return -EIO;
+		return 0;
 
-        stuffp->audiostatus = CDROM_AUDIO_INVALID;
+	case CDROMPLAYTRKIND:
+		MCDX_TRACE_IOCTL(("CDROMPLAYTRKIND\n"));
 
-		/* get the multisession information */
-		TRACE((OPENCLOSE, "open() Request multisession info\n"));
-		if (-1 == mcdx_requestmultidiskinfo(
-				stuffp, &stuffp->multi, 6)) {
-			INFO(("No multidiskinfo\n"));
-			stuffp->autoclose = 0;
-		} else {
-			/* we succeeded, so on next open(2) we could try auto close
-			again */
-			stuffp->autoclose = 1;
-		
-#if !MCDX_QUIET
-			if (stuffp->multi.multi > 2)
-				INFO(("open() unknown multisession value (%d)\n", 
-						stuffp->multi.multi));
-#endif
+		if ((ans = verify_area(VERIFY_READ, (void *) arg, sizeof(ti))))
+			return ans;
+		memcpy_fromfs(&ti, (void *) arg, sizeof(ti));
+		if ((ti.cdti_trk0 < stuffp->n_first)
+		    || (ti.cdti_trk0 > stuffp->n_last)
+		    || (ti.cdti_trk1 < stuffp->n_first))
+			return -EINVAL;
+		if (ti.cdti_trk1 > stuffp->n_last)
+			ti.cdti_trk1 = stuffp->n_last;
+		return play_track(stuffp, &ti);
+
+	case CDROMPLAYMSF:
+		MCDX_TRACE_IOCTL(("CDROMPLAYMSF "));
+
+		if ((ans = verify_area(VERIFY_READ, (void *) arg,
+				       sizeof(struct cdrom_msf))))
+			 return ans;
+		memcpy_fromfs(&msf, (void *) arg, sizeof msf);
+		msf.cdmsf_min0 = uint2bcd(msf.cdmsf_min0);
+		msf.cdmsf_sec0 = uint2bcd(msf.cdmsf_sec0);
+		msf.cdmsf_frame0 = uint2bcd(msf.cdmsf_frame0);
+		msf.cdmsf_min1 = uint2bcd(msf.cdmsf_min1);
+		msf.cdmsf_sec1 = uint2bcd(msf.cdmsf_sec1);
+		msf.cdmsf_frame1 = uint2bcd(msf.cdmsf_frame1);
+		stuffp->resume.stop.minute = msf.cdmsf_min1;
+		stuffp->resume.stop.second = msf.cdmsf_sec1;
+		stuffp->resume.stop.frame = msf.cdmsf_frame1;
+		if (-1 == set_command(stuffp, MCDX_CMD_PLAY,
+				      &msf, sizeof(msf), 3 * HZ)) {
+			return -1;
+		}
+		stuffp->audiostatus = CDROM_AUDIO_PLAY;
+		return 0;
 
-			/* multisession ? */
-			if (!stuffp->multi.multi)
-				stuffp->multi.msf_last.second = 2;
+	case CDROMPAUSE:
+		MCDX_TRACE_IOCTL(("CDROMPAUSE\n"));
 
-			TRACE((OPENCLOSE, "open() MS: %d, last @ %02x:%02x.%02x\n",
-					stuffp->multi.multi,
-					stuffp->multi.msf_last.minute,
-					stuffp->multi.msf_last.second,
-					stuffp->multi.msf_last.frame));
+		if (stuffp->audiostatus != CDROM_AUDIO_PLAY)
+			return -EINVAL;
 
-		} /* got multisession information */
+		if (-1 == issue_command(stuffp, MCDX_CMD_STOP, 2 * HZ))
+			return -EIO;
+		if (-1 == request_subq_code(stuffp, &q)) {
+			stuffp->audiostatus = CDROM_AUDIO_NO_STATUS;
+			return 0;
+		}
+		stuffp->resume.start = q.dt;
+		stuffp->audiostatus = CDROM_AUDIO_PAUSED;
+		return 0;
 
-		/* request the disks table of contents (aka diskinfo) */
-		if (-1 == mcdx_requesttocdata(stuffp, &stuffp->di, 1)) {
+	case CDROMRESUME:
+		MCDX_TRACE_IOCTL(("CDROMRESUME\n"));
 
-			stuffp->lastsector = -1;
+		if (stuffp->audiostatus != CDROM_AUDIO_PAUSED)
+			return -EINVAL;
+		return play_track(stuffp, NULL);
+	case CDROMREADTOCENTRY:
+		MCDX_TRACE_IOCTL(("CDROMREADTOCENTRY\n"));
 
-		} else {
+		if (-1 == read_toc(stuffp))
+			return -1;
+		if ((ans = verify_area(VERIFY_READ, (void *) arg, sizeof(entry))))
+			return ans;
+		memcpy_fromfs(&entry, (void *) arg, sizeof(entry));
+
+		if (entry.cdte_track == CDROM_LEADOUT)
+			tp = &stuffp->toc[stuffp->n_last - stuffp->n_first + 1];
+		else if (entry.cdte_track > stuffp->n_last
+			 || entry.cdte_track < stuffp->n_first)
+			return -EINVAL;
+		else
+			tp = &stuffp->toc[entry.cdte_track - stuffp->n_first];
+
+		if (NULL == tp)
+			printk(KERN_ERR MCDX ": FATAL.\n");
+
+		entry.cdte_adr = tp->adr;
+		entry.cdte_ctrl = tp->ctrl;
+
+		if (entry.cdte_format == CDROM_MSF) {
+			entry.cdte_addr.msf.minute = bcd2uint(tp->dt.minute);
+			entry.cdte_addr.msf.second = bcd2uint(tp->dt.second);
+			entry.cdte_addr.msf.frame = bcd2uint(tp->dt.frame);
+		} else if (entry.cdte_format == CDROM_LBA)
+			entry.cdte_addr.lba = msf2log(&tp->dt);
+		else
+			return -EINVAL;
+
+		if ((ans = verify_area(VERIFY_WRITE, (void *) arg, sizeof(entry))))
+			return ans;
+		memcpy_tofs((void *) arg, &entry, sizeof(entry));
 
-			stuffp->lastsector = (CD_FRAMESIZE / 512) 
-					* msf2log(&stuffp->di.msf_leadout) - 1;
+		return 0;
 
-			TRACE((OPENCLOSE, "open() start %d (%02x:%02x.%02x) %d\n",
-					stuffp->di.n_first,
-					stuffp->di.msf_first.minute,
-					stuffp->di.msf_first.second,
-					stuffp->di.msf_first.frame,
-					msf2log(&stuffp->di.msf_first)));
-			TRACE((OPENCLOSE, "open() last %d (%02x:%02x.%02x) %d\n",
-					stuffp->di.n_last,
-					stuffp->di.msf_leadout.minute,
-					stuffp->di.msf_leadout.second,
-					stuffp->di.msf_leadout.frame,
-					msf2log(&stuffp->di.msf_leadout)));
-		}
+	case CDROMSUBCHNL:
+		MCDX_TRACE_IOCTL(("CDROMSUBCHNL\n"));
 
-		if (stuffp->toc) {
-			TRACE((MALLOC, "open() free old toc @ %p\n", stuffp->toc));
-			kfree(stuffp->toc);
-		
-			stuffp->toc = NULL;
-		}
+		if ((ans = verify_area(VERIFY_READ,
+				       (void *) arg, sizeof(sub))))
+			return ans;
 
-		TRACE((OPENCLOSE, "open() init irq generation\n"));
-		if (-1 == mcdx_config(stuffp, 1)) return -EIO;
+		memcpy_fromfs(&sub, (void *) arg, sizeof(sub));
 
-#if FALLBACK
-		/* Set the read speed */
-		WARN(("AAA %x AAA\n", stuffp->readcmd));
-		if (stuffp->readerrs) stuffp->readcmd = READ1X;
-		else stuffp->readcmd = 
-				stuffp->present | SINGLE ? READ1X : READ2X;
-		WARN(("XXX %x XXX\n", stuffp->readcmd));
-#endif
+		if (-1 == request_subq_code(stuffp, &q))
+			return -EIO;
 
-		/* try to get the first sector, iff any ... */
-		if (stuffp->lastsector >= 0) {
-			char buf[512];
-			int ans;
-			int tries;
+		sub.cdsc_audiostatus = stuffp->audiostatus;
+		sub.cdsc_adr = q.adr;
+		sub.cdsc_ctrl = q.ctrl;
+		sub.cdsc_trk = bcd2uint(q.tno);
+		sub.cdsc_ind = bcd2uint(q.index);
+
+		if (sub.cdsc_format == CDROM_LBA) {
+			sub.cdsc_absaddr.lba = msf2log(&q.dt);
+			sub.cdsc_reladdr.lba = msf2log(&q.tt);
+		} else if (sub.cdsc_format == CDROM_MSF) {
+			sub.cdsc_absaddr.msf.minute = bcd2uint(q.dt.minute);
+			sub.cdsc_absaddr.msf.second = bcd2uint(q.dt.second);
+			sub.cdsc_absaddr.msf.frame = bcd2uint(q.dt.frame);
+			sub.cdsc_reladdr.msf.minute = bcd2uint(q.tt.minute);
+			sub.cdsc_reladdr.msf.second = bcd2uint(q.tt.second);
+			sub.cdsc_reladdr.msf.frame = bcd2uint(q.tt.frame);
+		} else
+			return -EINVAL;
+
+		if ((ans = verify_area(VERIFY_WRITE,
+				       (void *) arg, sizeof(sub))))
+			return ans;
+		memcpy_tofs((void *) arg, &sub, sizeof(sub));
 
-			stuffp->xa = 0;
-			stuffp->audio = 0;
+		return 0;
 
-			for (tries = 6; tries; tries--) {
+	case CDROMREADTOCHDR:
+		MCDX_TRACE_IOCTL(("CDROMREADTOCHDR\n"));
 
-				stuffp->introk = 1;
+		if ((ans = verify_area(VERIFY_WRITE, (void *) arg, sizeof toc)))
+			return ans;
 
-				TRACE((OPENCLOSE, "open() try as %s\n",
-					stuffp->xa ? "XA" : "normal"));
+		toc.cdth_trk0 = stuffp->n_first;
+		toc.cdth_trk1 = stuffp->n_last;
+		memcpy_tofs((void *) arg, &toc, sizeof toc);
+		return 0;
 
-				/* set data mode */
-				if (-1 == (ans = mcdx_setdatamode(stuffp, 
-						stuffp->xa ? MODE2 : MODE1, 1))) {
-					/* return -EIO; */
-					stuffp->xa = 0;
-					break;
-				}
+	case CDROMMULTISESSION:
+		MCDX_TRACE_IOCTL(("CDROMMULTISESSION\n"));
 
-				if ((stuffp->audio = e_audio(ans))) break; 
+		if (0 != (ans = verify_area(VERIFY_READ, (void *) arg,
+				     sizeof(struct cdrom_multisession))))
+			 return ans;
+
+		memcpy_fromfs(&ms,
+			(void *) arg, sizeof(struct cdrom_multisession));
+		if (ms.addr_format == CDROM_MSF) {
+			ms.addr.msf.minute =
+			    bcd2uint(stuffp->multi.msf_last.minute);
+			ms.addr.msf.second =
+			    bcd2uint(stuffp->multi.msf_last.second);
+			ms.addr.msf.frame =
+			    bcd2uint(stuffp->multi.msf_last.frame);
+		} else if (ms.addr_format == CDROM_LBA)
+			ms.addr.lba = msf2log(&stuffp->multi.msf_last);
+		else
+			return -EINVAL;
+		ms.xa_flag = !!stuffp->multi.multi;
+
+		if (0 != (ans = verify_area(VERIFY_WRITE, (void *) arg,
+				     sizeof(struct cdrom_multisession))))
+			 return ans;
 
-				while (0 == (ans = mcdx_transfer(stuffp, buf, 0, 1))) 
-					;
+		memcpy_tofs((void *) arg,
+			    &ms, sizeof(struct cdrom_multisession));
+		return 0;
 
-				if (ans == 1) break;
-				stuffp->xa = !stuffp->xa; 
-			}
-			/* if (!tries) return -EIO; */
+	case CDROMEJECT:
+		MCDX_TRACE_IOCTL(("CDROMEJECT\n"));
+		if (stuffp->users > 1)
+			return -EBUSY;
+		if (stuffp->door) {
+			if (-1 == issue_command(stuffp, MCDX_CMD_EJECT, 5 * HZ))
+				return -EIO;
 		}
+		/*
+		 * Force rereading of toc next time the disk gets accessed!
+		 */
+		if (stuffp->toc) {
+			kfree(stuffp->toc);
+			stuffp->toc = 0;
+		}
+		return 0;
 
-		/* xa disks will be read in raw mode, others not */
-		if (-1 == mcdx_setdrivemode(stuffp, 
-				stuffp->xa ? RAW : COOKED, 1))
-			return -EIO;
-
-		if (stuffp->audio) {
-			INFO(("open() audio disk found\n"));
-		} else if (stuffp->lastsector >= 0) {
-			INFO(("open() %s%s disk found\n",
-					stuffp->xa ? "XA / " : "",
-					stuffp->multi.multi ? "Multi Session" : "Single Session"));
-		} 
+	case CDROMEJECT_SW:
+		MCDX_TRACE_IOCTL(("CDROMEJECT_SW\n"));
 
-        stuffp->xxx = 0;
-	}
+		stuffp->eject_sw = !!arg;
+		return 0;
 
-    /* lock the door if not already done */
-    if (0 == stuffp->users && (-1 == mcdx_lockdoor(stuffp, 1, 1))) 
-        return -EIO;
+	case CDROMVOLCTRL:
+		MCDX_TRACE_IOCTL(("CDROMVOLCTRL\n"));
 
-    stuffp->users++;
-    MOD_INC_USE_COUNT;
-    return 0;
+		if ((ans = verify_area(VERIFY_READ,
+				       (void *) arg,
+				       sizeof(volctrl))))
+			return ans;
+
+		memcpy_fromfs(&volctrl, (char *) arg, sizeof(volctrl));
+		/* Adjust for the wiredness of workman. */
+		volctrl.channel2 = volctrl.channel1;
+		volctrl.channel1 = volctrl.channel3 = 0xff;
+		return talk(stuffp, MCDX_CMD_SET_ATTENATOR,
+			    &volctrl, sizeof(volctrl),
+			    &volctrl, sizeof(volctrl), 2 * HZ);
+
+	default:
+		printk(KERN_WARNING MCDX
+		       ": unknown ioctl request 0x%04x\n", command);
+		return -EINVAL;
+	}
 }
 
-static void 
-mcdx_close(struct inode *ip, struct file *fp)
+/*   
+ * This does actually the transfer from the drive.
+ * Return:      -1 on timeout or other error
+ * else status byte (as in stuff->st) 
+ * FIXME: the excessive jumping throught wait queues degrades the
+ * performance significantly.
+ */
+static int transfer_data(struct s_drive_stuff *stuffp,
+			 char *p, int sector, int nr_sectors)
 {
-    struct s_drive_stuff *stuffp;
+	int off;
+	int done = 0;
 
-    TRACE((OPENCLOSE, "close()\n"));
+	if (stuffp->valid
+	    && (sector >= stuffp->pending)
+	    && (sector < stuffp->off_direct)) {
+		off = stuffp->off_requested < (off = sector + nr_sectors)
+		    ? stuffp->off_requested : off;
+
+		do {
+			/* wait for the drive become idle, but first
+			 * check for possible occurred errors --- the drive
+			 * seems to report them asynchronously
+			 */
+			current->timeout = jiffies + 5 * HZ;
+			while (stuffp->introk && stuffp->busy
+			       && current->timeout) {
+				interruptible_sleep_on(&stuffp->busyq);
+			}
 
-    stuffp = mcdx_stuffp[MINOR(ip->i_rdev)];
+			/* test for possible errors */
+			if (current->timeout == 0 || !stuffp->introk) {
+				if (current->timeout == 0) {
+					printk(KERN_ERR MCDX ": transfer timeout\n");
+				} else if (!stuffp->introk) {
+					printk(KERN_ERR MCDX
+					       ": error via irq in transfer reported\n");
+				}
 
-    if (0 == --stuffp->users) {
-		sync_dev(ip->i_rdev);	/* needed for r/o device? */
+				stuffp->busy = 0;
+				stuffp->valid = 0;
+				stuffp->introk = 1;
+				return -1;
+			}
+			/* test if it's the first sector of a block,
+			 * there we have to skip some bytes as we read raw data 
+			 */
+			if (stuffp->xa && (0 == (stuffp->pending & 3))) {
+				insb(DATA_REG, p,
+				     CD_FRAMESIZE_RAW - CD_XA_TAIL - CD_FRAMESIZE);
+			}
+			/* now actually read the data */
+			insb(DATA_REG, p, 512);
 
-		/* invalidate_inodes(ip->i_rdev); */
-		invalidate_buffers(ip->i_rdev);
+			/* test if it's the last sector of a block,
+			 * if so, we have to expect an interrupt and to skip 
+			 * some data too 
+			 */
+			if ((stuffp->busy = (3 == (stuffp->pending & 3)))
+			    && stuffp->xa) {
+				int i;
+				for (i = 0; i < CD_XA_TAIL; ++i)
+					inb(DATA_REG);
+			}
+			if (stuffp->pending == sector) {
+				p += 512;
+				done++;
+				sector++;
+			}
+		} while (++(stuffp->pending) < off);
+	} else {
+		unsigned char cmd[6];
+		stuffp->valid = 1;
+		stuffp->pending = sector & ~3;
 
+		/* do some sanity checks */
+		if (stuffp->pending > stuffp->lastsector) {
+			printk(KERN_ERR MCDX
+			": sector %d transfer from nirvana requested.\n",
+			       stuffp->pending);
+			stuffp->eom = 1;
+			stuffp->valid = 0;
+			return -1;
+		}
+		if ((stuffp->off_direct = stuffp->pending + DIRECT_SIZE)
+		    > stuffp->lastsector + 1)
+			stuffp->off_direct = stuffp->lastsector + 1;
+		if ((stuffp->off_requested = stuffp->pending + REQUEST_SIZE)
+		    > stuffp->lastsector + 1)
+			stuffp->off_requested = stuffp->lastsector + 1;
+		{
+			unsigned int l = (stuffp->pending / 4)
+			+ CD_BLOCK_OFFSET;
 
-#if !MCDX_QUIET
-		if (-1 == mcdx_lockdoor(stuffp, 0, 3))
-				INFO(("close() Cannot unlock the door\n"));
-#else
-		mcdx_lockdoor(stuffp, 0, 3);
-#endif
+			cmd[0] = uint2bcd(l / 4500), l %= 4500;
+			/* minute */
+			cmd[1] = uint2bcd(l / 75);	/* second */
+			cmd[2] = uint2bcd(l % 75);	/* frame */
+		}
 
-        /* eject if wished */
-        if (stuffp->eject_sw) mcdx_eject(stuffp, 1);
+		stuffp->busy = 1;
+		/*
+		 * FIXME: What about the ominous frame length?!
+		 */
+		cmd[3] = ~0;
+		cmd[4] = ~0;
+		cmd[5] = ~0;
+
+		outb(stuffp->double_speed ? MCDX_CMD_PLAY_2X : MCDX_CMD_PLAY,
+		     DATA_REG);
+		outsb(DATA_REG, cmd, 6);
+	}
 
-    }
-    MOD_DEC_USE_COUNT;
+	stuffp->off_direct =
+	    (stuffp->off_direct += done) < stuffp->off_requested
+	    ? stuffp->off_direct
+	    : stuffp->off_requested;
 
-    return;
+	return done;
 }
 
-int check_mcdx_media_change(kdev_t full_dev)
-/*	Return: 1 if media changed since last call to this function
-			0 otherwise */
+void do_mcdx_request()
 {
-    struct s_drive_stuff *stuffp; 
+	int dev;
+	struct s_drive_stuff *stuffp;
 
-    INFO(("check_mcdx_media_change called for device %s\n",
-	  kdevname(full_dev)));
+      again:
 
-	stuffp = mcdx_stuffp[MINOR(full_dev)];
-	mcdx_getstatus(stuffp, 1);
-
-	if (stuffp->yyy == 0) return 0;
+	if ((CURRENT == NULL) || (CURRENT->rq_status == RQ_INACTIVE)) {
+		return;
+	}
+	stuffp = mcdx_stuffp[MINOR(CURRENT->rq_dev)];
 
-	stuffp->yyy = 0;
-	return 1;
-}
+	INIT_REQUEST;
+	dev = MINOR(CURRENT->rq_dev);
 
-void mcdx_setup(char *str, int *pi)
-{
-	if (pi[0] > 0) mcdx_drive_map[0][0] = pi[1];
-	if (pi[0] > 1) mcdx_drive_map[0][1] = pi[2];
-}
-
-/* DIRTY PART ******************************************************/ 
+	if ((dev < 0) || (dev >= MCDX_NDRIVES) || (!stuffp)) {
+		printk(KERN_WARNING MCDX ": bad device requested: %s\n",
+		       kdevname(CURRENT->rq_dev));
+		end_request(0);
+		goto again;
+	}
+	if (stuffp->audio) {
+		printk(KERN_WARNING MCDX ": attempt to read from audio cd\n");
+		end_request(0);
+		goto again;
+	}
+	switch (CURRENT->cmd) {
+	case WRITE:
+		printk(KERN_ERR MCDX ": attempt to write to cd!!\n");
+		end_request(0);
+		break;
 
-static void mcdx_delay(struct s_drive_stuff *stuff, long jifs)
-/*	This routine is used for sleeping.
-	May be we could use a simple count loop w/ jumps to itself, but 
-	I wanna make this independent of cpu speed. [1 jiffy is 1/HZ] sec */
-{
-    unsigned long tout = jiffies + jifs;
-    if (jifs < 0) return;
+	case READ:
+		stuffp->eom = 0;	/* clear end of media flag */
+		while (CURRENT->nr_sectors) {
+			int i;
 
-    /* TRACE((INIT, "mcdx_delay %d\n", jifs)); */
+			if (-1 == (i = transfer_data(stuffp,
+						     CURRENT->buffer,
+						     CURRENT->sector,
+						 CURRENT->nr_sectors))) {
+				if (stuffp->eom) {
+					CURRENT->sector += CURRENT->nr_sectors;
+					CURRENT->nr_sectors = 0;
+				} else {
+					/*
+					 * FIXME: TRY SOME ERROR RECOVERY HERE!
+					 */
+				}
+				end_request(0);
+				goto again;
+			}
+			CURRENT->sector += i;
+			CURRENT->nr_sectors -= i;
+			CURRENT->buffer += (i * 512);
+		}
+		end_request(1);
+		break;
+
+	default:
+		panic(MCDX "do_request: unknown command.\n");
+		break;
+	}
 
-    if (current->pid == 0) {        /* no sleep allowed */
-		while (jiffies < tout) {
-            current->timeout = jiffies;
-            schedule();
-        }
-    } else {                        /* sleeping is allowed */
-        current->timeout = tout;
-        /* current->state = TASK_INTERRUPTIBLE; */
-		/* And perhaps we should remove the while() { ... } */
-        while (current->timeout) {
-            interruptible_sleep_on(&stuff->sleepq);
-			TRACE((SLEEP, "delay: to is %d\n", current->timeout));
-        }
-    }
+	goto again;
 }
 
-static void 
-mcdx_intr(int irq, void *dev_id, struct pt_regs* regs)
+/*  
+ * actions done on open:
+ * 1)   get the drives status 
+ */
+static int mcdx_open(struct inode *ip, struct file *fp)
 {
-    struct s_drive_stuff *stuffp;
-	unsigned char b;
+	struct s_drive_stuff *stuffp = mcdx_stuffp[MINOR(ip->i_rdev)];
+	int st = 0;
+	unsigned long bang;
 
-    stuffp = mcdx_irq_map[irq];
+	MCDX_TRACE(("mcdx_open()\n"));
 
-    if (stuffp == NULL ) {
-		xwarn("mcdx: no device for intr %d\n", irq);
-		return;
-    }
-
-	/* get the interrupt status */
-	b = inb((unsigned int) stuffp->rreg_status);
-	stuffp->introk = ~b & MCDX_RBIT_DTEN;
+	if (!stuffp)
+		return -ENXIO;
 
-	/* NOTE: We only should get interrupts if data were requested.
-	   But the drive seems to generate ``asynchronous'' interrupts
-	   on several error conditions too.  (Despite the err int enable
-	   setting during initialisation) */
-
-	/* if not ok, read the next byte as the drives status */
-	if (!stuffp->introk) {
-		TRACE((IRQ, "intr() irq %d hw status 0x%02x\n", irq, b));
-		if (~b & MCDX_RBIT_STEN) {
-			xinfo(  "intr() irq %d    status 0x%02x\n", 
-					irq, inb((unsigned int) stuffp->rreg_data));
-		} else {
-			xinfo(  "intr() irq %d ambiguous hw status\n", irq);
-		}
-	} else {
-		TRACE((IRQ, "irq() irq %d ok, status %02x\n", irq, b));
-    }
+	/* close the door, if necessary (get the door information
+	 * from the hardware status register). 
+	 * If we can't read the CD after an autoclose
+	 * no further autocloses will be tried 
+	 */
+	if (inb(STAT_REG) & MCDX_RBIT_DOOR) {
+		if (stuffp->autoclose && (stuffp->door))
+			issue_command(stuffp, MCDX_CMD_CLOSE_DOOR, 10 * HZ);
+		else
+			return -EIO;
+	}
+	/*
+	 * Check if a disk is in.
+	 */ bang = jiffies + 10 * HZ;
+	while (jiffies < bang) {
+		st = issue_command(stuffp, MCDX_CMD_GET_STATUS, 5 * HZ);
+		if (st != -1 && (st & MCDX_RBIT_DISKSET))
+			break;
+		current->state = TASK_INTERRUPTIBLE;
+		current->timeout = jiffies + 1 * HZ;
+		schedule();
+	}
+	if (st == -1 || (st & MCDX_RBIT_DISKSET) == 0) {
+		printk(KERN_INFO MCDX ": no disk in drive\n");
+		return -EIO;
+	}
+	/* if the media changed we will have to do a little more 
+	 * FIXME: after removing of the mcdx_requestmultisession() it is showed
+	 * that the logics of this may be broken.
+	 */
+	if (stuffp->xxx) {
+		/* but wait - the time of media change will be set at the 
+		 * very last of this block.
+		 */
 
-    stuffp->busy = 0;
-    wake_up_interruptible(&stuffp->busyq);
-}
+		stuffp->audiostatus = CDROM_AUDIO_INVALID;
+		stuffp->autoclose = 1;
 
+		/* get the multisession information */
 
-static int 
-mcdx_talk (
-		struct s_drive_stuff *stuffp, 
-		const unsigned char *cmd, size_t cmdlen,
-		void *buffer, size_t size, 
-		unsigned int timeout, int tries)
-/* Send a command to the drive, wait for the result.
- * returns -1 on timeout, drive status otherwise
- * If buffer is not zero, the result (length size) is stored there.
- * If buffer is zero the size should be the number of bytes to read
- * from the drive.  These bytes are discarded.
- */
-{
-	int st;
-    char c;
-    int discard;
+		if (stuffp->multi_cap) {
+			int i = 6;	/* number of retries */
+			while (i && (-1 == get_command(stuffp,
+						 MCDX_CMD_GET_MDISK_INFO,
+			&stuffp->multi, sizeof(struct s_multi), 2 * HZ)))
+				--i;
+			if (!i) {
+				stuffp->autoclose = 0;
+				/*
+				 * No multidisk info
+				 */
+			}
+		} else
+			stuffp->multi.multi = 0;
 
-    if ((discard = (buffer == NULL))) buffer = &c;
+		if (stuffp->autoclose) {
+			/* we succeeded, so on next open(2) we could try  
+			 * auto close again 
+			 */
 
-    while (stuffp->lock) {
-		interruptible_sleep_on(&stuffp->lockq); 
-		TRACE((SLEEP, "talk: lock = %d\n",
-				stuffp->lock));
-	}
-
-    stuffp->lock = 1;
-    stuffp->valid = 0;	
-
-#if MCDX_DEBUG & TALK
-	{ 
-		unsigned char i;
-		TRACE((TALK, "talk() %d / %d tries, res.size %d, command 0x%02x", 
-				tries, timeout, size, (unsigned char) cmd[0]));
-		for (i = 1; i < cmdlen; i++) printk(" 0x%02x", cmd[i]);
-		printk("\n");
-	}
-#endif
+			/* multisession ? */
+			if (!stuffp->multi.multi)
+				stuffp->multi.msf_last.second = 2;
+		}		/* got multisession information */
+		/* request the disks table of contents (aka diskinfo) */
+		if (-1 == request_toc_data(stuffp)) {
+			stuffp->lastsector = -1;
+		} else {
+			stuffp->lastsector = (CD_FRAMESIZE / 512)
+			    * msf2log(&stuffp->msf_leadout) - 1;
+		}
 
-    /*  give up if all tries are done (bad) or if the status
-     *  st != -1 (good) */
-	for (st = -1; st == -1 && tries; tries--) {
-
-		char *bp = (char*) buffer;
-		size_t sz = size;
-
-		outsb((unsigned int) stuffp->wreg_data, cmd, cmdlen);
-        TRACE((TALK, "talk() command sent\n"));
-
-        /* get the status byte */
-        if (-1 == mcdx_getval(stuffp, timeout, 0, bp)) {
-            INFO(("talk() %02x timed out (status), %d tr%s left\n", 
-                    cmd[0], tries - 1, tries == 2 ? "y" : "ies"));
-                continue; 
-        }
-        st = *bp;
-        sz--;
-        if (!discard) bp++;
-
-        TRACE((TALK, "talk() got status 0x%02x\n", st));
-
-        /* command error? */
-        if (e_cmderr(st)) {
-            WARN(("command error cmd = %02x %s \n", 
-                    cmd[0], cmdlen > 1 ? "..." : ""));
-            st = -1;
-            continue;
-		}
-
-        /* audio status? */
-        if (stuffp->audiostatus == CDROM_AUDIO_INVALID)
-            stuffp->audiostatus = 
-                    e_audiobusy(st) ? CDROM_AUDIO_PLAY : CDROM_AUDIO_NO_STATUS;
-        else if (stuffp->audiostatus == CDROM_AUDIO_PLAY 
-                && e_audiobusy(st) == 0)
-            stuffp->audiostatus = CDROM_AUDIO_COMPLETED;
-
-        /* media change? */
-        if (e_changed(st)) {
-            INFO(("talk() media changed\n"));
-            stuffp->xxx = stuffp->yyy = 1;
-        }
-
-        /* now actually get the data */
-        while (sz--) {
-            if (-1 == mcdx_getval(stuffp, timeout, 0, bp)) {
-                INFO(("talk() %02x timed out (data), %d tr%s left\n", 
-                        cmd[0], tries - 1, tries == 2 ? "y" : "ies"));
-                st = -1; break;
-            }
-            if (!discard) bp++;
-            TRACE((TALK, "talk() got 0x%02x\n", *(bp - 1)));
-        }
-    }
+		if (stuffp->toc) {
+			kfree(stuffp->toc);
+			stuffp->toc = 0;
+		}
+		if (-1 == config_drive(stuffp))
+			return -EIO;
 
-#if !MCDX_QUIET
-    if (!tries && st == -1) INFO(("talk() giving up\n"));
-#endif
+		/* try to get the first sector, iff any ... */
+		if (stuffp->lastsector >= 0) {
+			char buf[512];
+			int ans;
+			int tries;
 
-    stuffp->lock = 0;
-    wake_up_interruptible(&stuffp->lockq);
+			stuffp->xa = 0;
+			stuffp->audio = 0;
 
-	TRACE((TALK, "talk() done with 0x%02x\n", st));
-    return st;
-}
+			for (tries = 6; tries; tries--) {
+				unsigned char c;
+				stuffp->introk = 1;
 
-/* MODULE STUFF ***********************************************************/
-#ifdef MODULE
+				/* set data mode */
+				c = stuffp->xa ? MODE2 : MODE1;
+				ans = set_command(stuffp,
+						  MCDX_CMD_SET_DATA_MODE,
+						  &c, sizeof(c), 5 * HZ);
 
-int init_module(void)
-{
-	int i;
-	int drives = 0;
+				if (-1 == ans) {
+					/* return -EIO; */
+					stuffp->xa = 0;
+					break;
+				} else if (ans & MCDX_RBIT_AUDIOTR) {
+					stuffp->audio = 1;
+					break;
+				}
+				 
+				while (0 == (ans = transfer_data(stuffp, buf,
+								 0, 1)));
 
-	mcdx_init();
-	for (i = 0; i < MCDX_NDRIVES; i++)  {
-		if (mcdx_stuffp[i]) {
-		TRACE((INIT, "init_module() drive %d stuff @ %p\n",
-				i, mcdx_stuffp[i]));
-			drives++;
+				if (ans == 1)
+					break;
+				stuffp->xa = !stuffp->xa;
+			}
+			/* if (!tries) return -EIO; */
 		}
+		/* xa disks will be read in raw mode, others not */
+		if (-1 == set_drive_mode(stuffp, stuffp->xa ? RAW : COOKED))
+			return -EIO;
+		stuffp->xxx = 0;
 	}
-
-    if (!drives) 
+	/* lock the door if not already done */
+	if (0 == stuffp->users && (-1 == lock_door(stuffp, DOOR_LOCK)))
 		return -EIO;
 
-    register_symtab(0);
-    return 0;
+	stuffp->users++;
+	MOD_INC_USE_COUNT;
+	return 0;
 }
 
-void cleanup_module(void)
+static void mcdx_close(struct inode *ip, struct file *fp)
 {
-    int i;
+	struct s_drive_stuff *stuffp = mcdx_stuffp[MINOR(ip->i_rdev)];
 
-	INFO(("cleanup_module called\n"));
-	
-    for (i = 0; i < MCDX_NDRIVES; i++) {
-		struct s_drive_stuff *stuffp;
-		stuffp = mcdx_stuffp[i];
-		if (!stuffp) continue;
-		release_region((unsigned long) stuffp->wreg_data, MCDX_IO_SIZE);
-		free_irq(stuffp->irq, NULL);
-		if (stuffp->toc) {
-			TRACE((MALLOC, "cleanup_module() free toc @ %p\n", stuffp->toc));
-			kfree(stuffp->toc);
+	MCDX_TRACE(("mcdx_close()\n"));
+
+	if (0 == --stuffp->users) {
+		sync_dev(ip->i_rdev);	/* needed for r/o device? */
+
+		/* invalidate_inodes(ip->i_rdev); */
+		invalidate_buffers(ip->i_rdev);
+		lock_door(stuffp, DOOR_UNLOCK);
+
+		/* eject if wished and possible */
+		if (stuffp->eject_sw && (stuffp->door)) {
+			issue_command(stuffp, MCDX_CMD_EJECT, 5 * HZ);
 		}
-		TRACE((MALLOC, "cleanup_module() free stuffp @ %p\n", stuffp));
-		mcdx_stuffp[i] = NULL;
-		kfree(stuffp);
-    }
+	}
+	MOD_DEC_USE_COUNT;
 
-    if (unregister_blkdev(MAJOR_NR, DEVICE_NAME) != 0) {
-        WARN(("cleanup() unregister_blkdev() failed\n"));
-    } 
-#if !MCDX_QUIET
-	else INFO(("cleanup() succeeded\n"));
-#endif
+	return;
 }
 
-#endif MODULE
+/*      
+ * Return: 1 if media changed since last call to this function
+ * 0 otherwise 
+ */
+static int mcdx_media_change(kdev_t full_dev)
+{
+	struct s_drive_stuff *stuffp;
 
-/* Support functions ************************************************/
+	MCDX_TRACE(("mcdx_media_change()\n"));
 
-#if MCDX_DEBUG
-void trace(int level, const char* fmt, ...)
-{
-	char s[255];
-	va_list args;
-	if (level < 1) return;
-	va_start(args, fmt);
-	if (sizeof(s) < vsprintf(s, fmt, args))
-		printk(MCDX ":: dprintf exceeds limit!!\n");
-	else printk(MCDX ":: %s", s);
-	va_end(args);
+	/*
+	 * FIXME: propably this is unneded or should be simplified!
+	 */
+	issue_command(stuffp = mcdx_stuffp[MINOR(full_dev)],
+		      MCDX_CMD_GET_STATUS, 5 * HZ);
+
+	return stuffp->xxx;
 }
-#endif
 
-void warn(const char* fmt, ...)
+/* Interrupt handler routine.
+ * This function is called, when during transfer the end of a physical 2048
+ * byte block is reached.
+ */
+static void mcdx_intr(int irq, void *dev_id, struct pt_regs *regs)
 {
-	char s[255];
-	va_list args;
-	va_start(args, fmt);
-	if (sizeof(s) < vsprintf(s, fmt, args))
-		printk(MCDX ":: dprintf exceeds limit!!\n");
-	else printk(MCDX ": %s", s);
-	va_end(args);
+	struct s_drive_stuff *stuffp;
+	u_char b;
+
+	if (!(stuffp = mcdx_irq_map[irq])) {
+		return;		/* hugh? */
+	}
+	
+	/* NOTE: We only should get interrupts if data were requested.
+	 * But the drive seems to generate ``asynchronous'' interrupts
+	 * on several error conditions too.  (Despite the err int enable
+	 * setting during initialisation) 
+	 */
+
+	/* get the interrupt status */
+	b = inb(STAT_REG);	
+	if (!(b & MCDX_RBIT_DTEN)) {
+		stuffp->introk = 1;
+	} else {
+		stuffp->introk = 0;
+		if (!(b & MCDX_RBIT_STEN)) {
+			printk(KERN_DEBUG MCDX ": irq %d status 0x%02x\n",
+			       irq, inb(DATA_REG));
+		} else {
+			MCDX_TRACE(("irq %d ambiguous hw status\n", irq));
+		}
+	}
+	stuffp->busy = 0;
+	wake_up_interruptible(&stuffp->busyq);
 }
 
+/*
+ * FIXME!
+ * This seems to hang badly, when the driver is loaded with inappriopriate
+ * port/irq settings!
+ */
 int mcdx_init(void)
 {
 	int drive;
 
 #ifdef MODULE
-	WARN(("Version 1.9 for %s\n", kernel_version));
+	printk(KERN_INFO "Mitsumi driver version " VERSION " for %s\n",
+	       kernel_version);
 #else
-	WARN(("Version 1.9\n"));
+	printk(KERN_INFO "Mitsumi driver version " VERSION "\n");
 #endif
-
-	WARN(("mcdx.c,v 1.2 1996/03/22 01:14:59 heiko Exp\n"));
-
-	/* zero the pointer array */
-	for (drive = 0; drive < MCDX_NDRIVES; drive++)
-		mcdx_stuffp[drive] = NULL;
-
-	/* do the initialisation */
-	for (drive = 0; drive < MCDX_NDRIVES; drive++) { 
-		struct s_version version;
-		struct s_drive_stuff* stuffp;
-        int size;
+	printk("%d\n", MCDX_NDRIVES);
+	for (drive = 0; drive < MCDX_NDRIVES; drive++) {
+		struct {
+			u_char code;
+			u_char version;
+		} firmware;
+		int i;
+		struct s_drive_stuff *stuffp;
+		int size;
 
 		mcdx_blocksizes[drive] = 0;
+		mcdx_stuffp[drive] = 0;
 
-        size = sizeof(*stuffp);
-		
-		TRACE((INIT, "init() try drive %d\n", drive));
+		size = sizeof(*stuffp);
 
-        TRACE((INIT, "kmalloc space for stuffpt's\n"));
-		TRACE((MALLOC, "init() malloc %d bytes\n", size));
 		if (!(stuffp = kmalloc(size, GFP_KERNEL))) {
-			WARN(("init() malloc failed\n"));
-			break; 
+			printk(KERN_ERR MCDX
+			       ": malloc of drives data failed!\n");
+			break;
 		}
+		/* set default values */ memset(stuffp, 0, sizeof(*stuffp));
+		stuffp->autoclose = 1;	/* close the door on open(2) */
 
-		TRACE((INIT, "init() got %d bytes for drive stuff @ %p\n", sizeof(*stuffp), stuffp));
-
-		/* set default values */
-		memset(stuffp, 0, sizeof(*stuffp));
-        stuffp->autoclose = 1;      /* close the door on open(2) */
-
-		stuffp->present = 0;		/* this should be 0 already */
-		stuffp->toc = NULL;			/* this should be NULL already */
-
-		/* setup our irq and i/o addresses */
 		stuffp->irq = irq(mcdx_drive_map[drive]);
-		stuffp->wreg_data = stuffp->rreg_data = port(mcdx_drive_map[drive]);
-		stuffp->wreg_reset = stuffp->rreg_status = stuffp->wreg_data + 1;
-		stuffp->wreg_hcon = stuffp->wreg_reset + 1;
-		stuffp->wreg_chn = stuffp->wreg_hcon + 1;
+		stuffp->base = port(mcdx_drive_map[drive]);
 
 		/* check if i/o addresses are available */
-		if (0 != check_region((unsigned int) stuffp->wreg_data, MCDX_IO_SIZE)) {
-            WARN(("0x%3p,%d: "
-                    "Init failed. I/O ports (0x%3p..0x%3p) already in use.\n",
-                    stuffp->wreg_data, stuffp->irq,
-                    stuffp->wreg_data, 
-                    stuffp->wreg_data + MCDX_IO_SIZE - 1));
-			TRACE((MALLOC, "init() free stuffp @ %p\n", stuffp));
-            kfree(stuffp);
-			TRACE((INIT, "init() continue at next drive\n"));
-			continue; /* next drive */
+		if (check_region(stuffp->base, MCDX_IO_SIZE)) {
+			printk(KERN_WARNING
+			       "Init failed. I/O ports (0x%3x..0x%3x) "
+			       "already in use.\n",
+			  stuffp->base, stuffp->base + MCDX_IO_SIZE - 1);
+			kfree(stuffp);
+			continue;	/* next drive */
 		}
-
-		TRACE((INIT, "init() i/o port is available at 0x%3p\n", stuffp->wreg_data));
-
-		TRACE((INIT, "init() hardware reset\n"));
-		mcdx_reset(stuffp, HARD, 1);
-
-		TRACE((INIT, "init() get version\n"));
-		if (-1 == mcdx_requestversion(stuffp, &version, 4)) {
+		/*
+		 * Hardware reset.
+		 */
+		outb(0, CHAN_REG);	/* no dma, no irq -> hardware */
+		outb(0, RESET_REG);	/* hw reset */
+
+		i = 10;		/* number of retries */
+		while (-1 == get_command(stuffp, MCDX_CMD_GET_FIRMWARE,
+				    &firmware, sizeof(firmware), 2 * HZ))
+			--i;
+		if (!i) {
 			/* failed, next drive */
-            WARN(("%s=0x%3p,%d: Init failed. Can't get version.\n",
-                    MCDX,
-                    stuffp->wreg_data, stuffp->irq));
-			TRACE((MALLOC, "init() free stuffp @ %p\n", stuffp));
-            kfree(stuffp);
-			TRACE((INIT, "init() continue at next drive\n"));
+			printk(KERN_WARNING
+			"%s=0x%3x,%d: Init failed. Can't get version.\n",
+			       MCDX, stuffp->base, stuffp->irq);
+			kfree(stuffp);
 			continue;
 		}
-
-		switch (version.code) {
-		case 'D': 
-                stuffp->readcmd = READ2X; 
-                stuffp->present = DOUBLE | DOOR | MULTI; 
-                break;
-		case 'F': 
-                stuffp->readcmd = READ1X; 
-                stuffp->present = SINGLE | DOOR | MULTI;
-                break;
-		case 'M': 
-                stuffp->readcmd = READ1X;
-                stuffp->present = SINGLE;
-                break;
-		default: 
-                stuffp->present = 0; break;
-		}
-
-        stuffp->playcmd = READ1X;
-
-
-		if (!stuffp->present) {
-            WARN(("%s=0x%3p,%d: Init failed. No Mitsumi CD-ROM?.\n",
-                    MCDX, stuffp->wreg_data, stuffp->irq));
+		switch (firmware.code) {
+		case 'D':
+			stuffp->double_speed = stuffp->door =
+			    stuffp->multi_cap = 1;
+			break;
+		case 'F':
+			stuffp->door = stuffp->multi_cap = 1;
+			break;
+		case 'M':
+			break;
+		default:
 			kfree(stuffp);
-			continue; /* next drive */
 		}
 
-		TRACE((INIT, "init() register blkdev\n"));
-		if (register_blkdev(MAJOR_NR, DEVICE_NAME, &mcdx_fops) != 0) {
-            WARN(("%s=0x%3p,%d: Init failed. Can't get major %d.\n",
-                    MCDX,
-                    stuffp->wreg_data, stuffp->irq, MAJOR_NR));
+		if (!stuffp)
+			continue;	/* next drive */
+
+		if (register_blkdev(MAJOR_NR, DEVICE_NAME, &mcdx_fops)) {
 			kfree(stuffp);
-			continue; /* next drive */
+			continue;	/* next drive */
 		}
-
 		blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
 		read_ahead[MAJOR_NR] = READ_AHEAD;
 
 		blksize_size[MAJOR_NR] = mcdx_blocksizes;
 
-		TRACE((INIT, "init() subscribe irq and i/o\n"));
 		mcdx_irq_map[stuffp->irq] = stuffp;
-		if (request_irq(stuffp->irq, mcdx_intr, SA_INTERRUPT, DEVICE_NAME, NULL)) {
-            WARN(("%s=0x%3p,%d: Init failed. Can't get irq (%d).\n",
-                    MCDX,
-                    stuffp->wreg_data, stuffp->irq, stuffp->irq));
+		if (request_irq(stuffp->irq, mcdx_intr,
+				SA_INTERRUPT, DEVICE_NAME, NULL)) {
 			stuffp->irq = 0;
 			kfree(stuffp);
-			continue;
-		}
-		request_region((unsigned int) stuffp->wreg_data, 
-                MCDX_IO_SIZE, 
-                DEVICE_NAME); 
-
-		TRACE((INIT, "init() get garbage\n"));
-		{
-			int i;
-			mcdx_delay(stuffp, HZ/2);
-			for (i = 100; i; i--) (void) inb((unsigned int) stuffp->rreg_status);
+			continue;	/* next drive */
 		}
+		request_region(stuffp->base, MCDX_IO_SIZE, DEVICE_NAME);
 
+		/* get junk after some delay.
+		 */
+		current->state = TASK_INTERRUPTIBLE;
+		current->timeout = jiffies + HZ / 2;
+		schedule();
+		for (i = 100; i; i--)
+			(void) inb(STAT_REG);
 
 #if WE_KNOW_WHY
-			outb(0x50, (unsigned int) stuffp->wreg_chn);	/* irq 11 -> channel register */
+		outb(0x50, CHAN_REG);	/* irq 11 -> channel register */
 #endif
 
-		TRACE((INIT, "init() set non dma but irq mode\n"));
-		mcdx_config(stuffp, 1);
-
-		stuffp->minor = drive;
+		config_drive(stuffp);
 
-		WARN((DEVICE_NAME " installed at 0x%3p, irq %d."
-			   " (Firmware version %c %x)\n",
-			   stuffp->wreg_data, stuffp->irq, version.code,
-               version.ver));
+		printk(KERN_INFO MCDX "%d: at 0x%3x, irq %d, firmware: %c %x\n",
+		       drive, stuffp->base, stuffp->irq,
+		       firmware.code, firmware.version);
 		mcdx_stuffp[drive] = stuffp;
-		TRACE((INIT, "init() mcdx_stuffp[%d] = %p\n", drive, stuffp));
 	}
 
 	return 0;
 }
 
-static int 
-mcdx_transfer(struct s_drive_stuff *stuffp,
-		char *p, int sector, int nr_sectors)
-/*	This seems to do the actually transfer.  But it does more.  It
-	keeps track of errors occurred and will (if possible) fall back
-	to single speed on error. 
-	Return:	-1 on timeout or other error
-			else status byte (as in stuff->st) */
-{
-	int ans;
-
-	ans = mcdx_xfer(stuffp, p, sector, nr_sectors);
-	return ans;
-#if FALLBACK
-	if (-1 == ans) stuffp->readerrs++;
-	else return ans;
-
-	if (stuffp->readerrs && stuffp->readcmd == READ1X) {
-		WARN(("XXX Already reading 1x -- no chance\n"));
-		return -1;
-	}
-
-	WARN(("XXX Fallback to 1x\n"));
-
-	stuffp->readcmd = READ1X;
-	return mcdx_transfer(stuffp, p, sector, nr_sectors);
-#endif
-	
-}
-
-
-static int mcdx_xfer(struct s_drive_stuff *stuffp,
-		char *p, int sector, int nr_sectors)
-/*	This does actually the transfer from the drive.
-	Return:	-1 on timeout or other error
-			else status byte (as in stuff->st) */
-{
-    int off;
-    int done = 0;
-
-	TRACE((TRANSFER, "transfer() %d sectors at sector %d\n",
-			nr_sectors, sector));
-
-	if (stuffp->audio) {
-		WARN(("attempt to read from audio cd\n"));
-		return -1;
-	}
-
-    while (stuffp->lock) {
-		interruptible_sleep_on(&stuffp->lockq);
-		TRACE((SLEEP, "xfer: lock = %d\n",
-			stuffp->lock));
-	}
-
-    if (stuffp->valid
-			&& (sector >= stuffp->pending)
-			&& (sector < stuffp->off_direct)) {
-
-
-	off = stuffp->off_requested < (off = sector + nr_sectors)
-			? stuffp->off_requested : off;
-
-	stuffp->lock = current->pid;
-
-	do {
-		/* wait for the drive become idle, but first
-		   check for possible occurred errors --- the drive
-		   seems to report them asynchronously */
-
-	    current->timeout = jiffies + 5 * HZ;
-	    while (stuffp->introk && stuffp->busy && current->timeout) {
-			interruptible_sleep_on(&stuffp->busyq);
-			TRACE((SLEEP, "request: busy = %d, timeout = %d\n",
-				stuffp->busy, current->timeout));
-		}
-
-		/* test for possible errors */
-		if (current->timeout == 0 || !stuffp->introk) {
-			if (current->timeout == 0)  {
-				WARN(("mcdx_transfer() timeout\n"));
-			} else if (!stuffp->introk) { 
-				WARN(("mcdx_transfer() error via irq reported\n"));
-			} else {
-				WARN(("mcdx_transfer() unknown failure in data request\n"));
-			}
-
-			stuffp->lock = 0;
-			stuffp->busy = 0;
-			stuffp->valid = 0;
-
-			wake_up_interruptible(&stuffp->lockq);
-			TRACE((TRANSFER, "transfer() done (-1)\n"));
-			return -1;
-	    }
-
-		/* test if it's the first sector of a block,
-		 * there we have to skip some bytes as we read raw data */
-		if (stuffp->xa && (0 == (stuffp->pending & 3))) {
-			const int HEAD = CD_FRAMESIZE_RAW - CD_XA_TAIL - CD_FRAMESIZE;
-			TRACE((TRANSFER, "transfer() sector %d, skip %d header bytes\n",
-				stuffp->pending, HEAD));
-			insb((unsigned int) stuffp->rreg_data, p, HEAD);
-		}
-
-		/* now actually read the data */
-
-		TRACE((TRANSFER, "transfer() read sector %d\n", stuffp->pending));
-	    insb((unsigned int) stuffp->rreg_data, p, 512); 
-
-		/* test if it's the last sector of a block,
-		 * if so, we have to expect an interrupt and to skip some
-		 * data too */
-		if ((stuffp->busy = (3 == (stuffp->pending & 3))) && stuffp->xa) {
-			char dummy[CD_XA_TAIL];
-			TRACE((TRANSFER, "transfer() sector %d, skip %d trailer bytes\n",
-					stuffp->pending, CD_XA_TAIL));
-			insb((unsigned int) stuffp->rreg_data, &dummy[0], CD_XA_TAIL);
-		}
-
-	    if (stuffp->pending == sector) {
-			p += 512;
-			done++;
-			sector++;
-	    }
-	} while (++(stuffp->pending) < off);
-
-	stuffp->lock = 0;
-	wake_up_interruptible(&stuffp->lockq);
-
-    } else {
-
-		static unsigned char cmd[] = {
-			0,
-			0, 0, 0,
-			0, 0, 0
-		};
-
-		cmd[0] = stuffp->readcmd;
-
-		stuffp->valid = 1;
-		stuffp->pending = sector & ~3;
-
-		/* do some sanity checks */
-		TRACE((TRANSFER, "transfer() request sector %d\n", stuffp->pending));
-		if (stuffp->pending > stuffp->lastsector) {
-			WARN(("transfer() sector %d from nirvana requested.\n",
-				stuffp->pending));
-			stuffp->status = MCDX_ST_EOM;
-			stuffp->valid = 0;
-			TRACE((TRANSFER, "transfer() done (-1)\n"));
-			return -1;
-		}
-
-		if ((stuffp->off_direct = stuffp->pending + DIRECT_SIZE)
-			> stuffp->lastsector + 1)
-			stuffp->off_direct = stuffp->lastsector + 1;
-		if ((stuffp->off_requested = stuffp->pending + REQUEST_SIZE)
-			> stuffp->lastsector + 1)
-			stuffp->off_requested = stuffp->lastsector + 1;
-
-		TRACE((TRANSFER, "transfer() pending %d\n", stuffp->pending));
-		TRACE((TRANSFER, "transfer() off_dir %d\n", stuffp->off_direct));
-		TRACE((TRANSFER, "transfer() off_req %d\n", stuffp->off_requested));
-
-		{
-			struct s_msf pending;
-			log2msf(stuffp->pending / 4, &pending);
-			cmd[1] = pending.minute;
-			cmd[2] = pending.second;
-			cmd[3] = pending.frame;
-		}
-
-		stuffp->busy = 1;
-		cmd[6] = (unsigned char) (stuffp->off_requested - stuffp->pending) / 4;
-
-		outsb((unsigned int) stuffp->wreg_data, cmd, sizeof cmd);
-
-    }
-
-    stuffp->off_direct = (stuffp->off_direct += done) < stuffp->off_requested
-	    ? stuffp->off_direct : stuffp->off_requested;
-
-	TRACE((TRANSFER, "transfer() done (%d)\n", done));
-    return done;
-}
-
-
-/*	Access to elements of the mcdx_drive_map members */
-
-static char* port(int *ip) { return (char*) ip[0]; }
-static int irq(int *ip) { return ip[1]; }
-
-/*	Misc number converters */
-
-static unsigned int bcd2uint(unsigned char c)
-{ return (c >> 4) * 10 + (c & 0x0f); }
-
-static unsigned int uint2bcd(unsigned int ival)
-{ return ((ival / 10) << 4) | (ival % 10); }
-
-static void log2msf(unsigned int l, struct s_msf* pmsf)
-{
-    l += CD_BLOCK_OFFSET;
-    pmsf->minute = uint2bcd(l / 4500), l %= 4500;
-    pmsf->second = uint2bcd(l / 75);
-    pmsf->frame = uint2bcd(l % 75);
-}
+#ifdef MODULE
 
-static unsigned int msf2log(const struct s_msf* pmsf)
-{
-    return bcd2uint(pmsf->frame)
-    + bcd2uint(pmsf->second) * 75
-    + bcd2uint(pmsf->minute) * 4500
-    - CD_BLOCK_OFFSET;
-}
-	
-int mcdx_readtoc(struct s_drive_stuff* stuffp)
-/*  Read the toc entries from the CD,
- *  Return: -1 on failure, else 0 */
+int init_module(void)
 {
+	int i;
 
-	if (stuffp->toc) {
-		TRACE((READTOC, "ioctl() toc already read\n"));
-		return 0;
-	}
-
-	TRACE((READTOC, "ioctl() readtoc for %d tracks\n",
-			stuffp->di.n_last - stuffp->di.n_first + 1));
-
-    if (-1 == mcdx_hold(stuffp, 1)) return -1;
-
-	TRACE((READTOC, "ioctl() tocmode\n"));
-	if (-1 == mcdx_setdrivemode(stuffp, TOC, 1)) return -EIO;
-
-	/* all seems to be ok so far ... malloc */
-	{
-		int size;
-		size = sizeof(struct s_subqcode) * (stuffp->di.n_last - stuffp->di.n_first + 2);
-
-		TRACE((MALLOC, "ioctl() malloc %d bytes\n", size));
-		stuffp->toc = kmalloc(size, GFP_KERNEL);
-		if (!stuffp->toc) {
-			WARN(("Cannot malloc %s bytes for toc\n", size));
-			mcdx_setdrivemode(stuffp, DATA, 1);
-			return -EIO;
-		}
-	}
-
-	/* now read actually the index */
-	{
-		int trk;
-		int retries;
-
-		for (trk = 0; 
-				trk < (stuffp->di.n_last - stuffp->di.n_first + 1); 
-				trk++)
-			stuffp->toc[trk].index = 0;
-
-		for (retries = 300; retries; retries--) { /* why 300? */
-			struct s_subqcode q;
-			unsigned int idx;
-		
-			if (-1 == mcdx_requestsubqcode(stuffp, &q, 1)) {
-				mcdx_setdrivemode(stuffp, DATA, 1);
-				return -EIO;
-			}
-
-			idx = bcd2uint(q.index);
-
-			if ((idx > 0) 
-					&& (idx <= stuffp->di.n_last) 
-					&& (q.tno == 0)
-					&& (stuffp->toc[idx - stuffp->di.n_first].index == 0)) {
-				stuffp->toc[idx - stuffp->di.n_first] = q;
-				TRACE((READTOC, "ioctl() toc idx %d (trk %d)\n", idx, trk));
-				trk--;
-			}
-			if (trk == 0) break;
+	mcdx_init();
+	for (i = 0; i < MCDX_NDRIVES; i++) {
+		if (mcdx_stuffp[i]) {
+			register_symtab(0);
+			return 0;
 		}
-		memset(&stuffp->toc[stuffp->di.n_last - stuffp->di.n_first + 1], 
-				0, sizeof(stuffp->toc[0]));
-		stuffp->toc[stuffp->di.n_last - stuffp->di.n_first + 1].dt
-				= stuffp->di.msf_leadout;
-	}
-
-	/* unset toc mode */
-	TRACE((READTOC, "ioctl() undo toc mode\n"));
-	if (-1 == mcdx_setdrivemode(stuffp, DATA, 2))
-		return -EIO;
-
-#if MCDX_DEBUG && READTOC
-	{ int trk;
-	for (trk = 0; 
-			trk < (stuffp->di.n_last - stuffp->di.n_first + 2); 
-			trk++)
-		TRACE((READTOC, "ioctl() %d readtoc %02x %02x %02x"
-				"  %02x:%02x.%02x  %02x:%02x.%02x\n",
-				trk + stuffp->di.n_first,
-				stuffp->toc[trk].control, stuffp->toc[trk].tno, stuffp->toc[trk].index,
-				stuffp->toc[trk].tt.minute, stuffp->toc[trk].tt.second, stuffp->toc[trk].tt.frame,
-				stuffp->toc[trk].dt.minute, stuffp->toc[trk].dt.second, stuffp->toc[trk].dt.frame));
-	}
-#endif
-
-	return 0;
-}
-
-static int
-mcdx_playmsf(struct s_drive_stuff* stuffp, const struct cdrom_msf* msf)
-{
-    unsigned char cmd[7] = {
-        0, 0, 0, 0, 0, 0, 0
-    };
-
-    cmd[0] = stuffp->playcmd;
-    
-    cmd[1] = msf->cdmsf_min0;
-    cmd[2] = msf->cdmsf_sec0;
-    cmd[3] = msf->cdmsf_frame0;
-    cmd[4] = msf->cdmsf_min1;
-    cmd[5] = msf->cdmsf_sec1;
-    cmd[6] = msf->cdmsf_frame1;
-
-    TRACE((PLAYMSF, "ioctl(): play %x "
-            "%02x:%02x:%02x -- %02x:%02x:%02x\n",
-            cmd[0], cmd[1], cmd[2], cmd[3],
-            cmd[4], cmd[5], cmd[6])); 
-
-    outsb((unsigned int) stuffp->wreg_data, cmd, sizeof cmd);
-
-    if (-1 == mcdx_getval(stuffp, 3 * HZ, 0, NULL)) {
-        WARN(("playmsf() timeout\n")); 
-        return -1;
-    }
-
-    stuffp->audiostatus = CDROM_AUDIO_PLAY;
-    return 0;
-}
-
-static int 
-mcdx_playtrk(struct s_drive_stuff* stuffp, const struct cdrom_ti* ti)
-{
-    struct s_subqcode* p;
-    struct cdrom_msf msf;
-
-    if (-1 == mcdx_readtoc(stuffp)) return -1;
-
-    if (ti) p = &stuffp->toc[ti->cdti_trk0 - stuffp->di.n_first];
-    else p = &stuffp->start;
-
-    msf.cdmsf_min0 = p->dt.minute;
-    msf.cdmsf_sec0 = p->dt.second;
-    msf.cdmsf_frame0 = p->dt.frame;
-
-    if (ti) {
-        p = &stuffp->toc[ti->cdti_trk1 - stuffp->di.n_first + 1];
-        stuffp->stop = *p;
-    } else p = &stuffp->stop;
-
-    msf.cdmsf_min1 = p->dt.minute;
-    msf.cdmsf_sec1 = p->dt.second;
-    msf.cdmsf_frame1 = p->dt.frame;
-
-    return mcdx_playmsf(stuffp, &msf);
-}
-
-
-/* Drive functions ************************************************/
-
-static int 
-mcdx_closedoor(struct s_drive_stuff *stuffp, int tries)
-{
-	if (stuffp->present & DOOR)
-		return mcdx_talk(stuffp, "\xf8", 1, NULL, 1, 5 * HZ, tries);
-	else
-		return 0;
-}
-
-static int 
-mcdx_stop(struct s_drive_stuff *stuffp, int tries)
-{ return mcdx_talk(stuffp, "\xf0", 1, NULL, 1, 2 * HZ, tries); }
-
-static int
-mcdx_hold(struct s_drive_stuff *stuffp, int tries)
-{ return mcdx_talk(stuffp, "\x70", 1, NULL, 1, 2 * HZ, tries); }
-
-static int
-mcdx_eject(struct s_drive_stuff *stuffp, int tries)
-{
-	if (stuffp->present & DOOR) {
-        stuffp->ejected = jiffies;
-		return mcdx_talk(stuffp, "\xf6", 1, NULL, 1, 5 * HZ, tries);
-    } else return 0;
-}
-
-static int
-mcdx_requestsubqcode(struct s_drive_stuff *stuffp, 
-        struct s_subqcode *sub, 
-        int tries)
-{
-	char buf[11];
-	int ans;
-
-	if (-1 == (ans = mcdx_talk(
-            stuffp, "\x20", 1, buf, sizeof(buf),
-            2 * HZ, tries))) 
-        return -1;
-	sub->control = buf[1];
-	sub->tno = buf[2];
-	sub->index = buf[3];
-	sub->tt.minute = buf[4];
-	sub->tt.second = buf[5];
-	sub->tt.frame = buf[6];
-	sub->dt.minute = buf[8];
-	sub->dt.second = buf[9];
-	sub->dt.frame = buf[10];
-
-	return ans;
-}
-
-static int
-mcdx_requestmultidiskinfo(struct s_drive_stuff *stuffp, struct s_multi *multi, int tries)
-{
-	char buf[5];
-	int ans;
-
-    if (stuffp->present & MULTI) {
-		ans = mcdx_talk(stuffp, "\x11", 1, buf, sizeof(buf), 2 * HZ, tries);
-		multi->multi = buf[1];
-        multi->msf_last.minute = buf[2];
-        multi->msf_last.second = buf[3];
-        multi->msf_last.frame = buf[4];
-        return ans;
-    } else {
-        multi->multi = 0;
-        return 0;
-    }
-}
-
-static int 
-mcdx_requesttocdata(struct s_drive_stuff *stuffp, struct s_diskinfo *info, int tries)
-{
-	char buf[9];
-	int ans;
-	ans = mcdx_talk(stuffp, "\x10", 1, buf, sizeof(buf), 2 * HZ, tries);
-	if (ans == -1) {
-		info->n_first = 0;
-		info->n_last = 0;
-	} else {
-		info->n_first = bcd2uint(buf[1]);
-		info->n_last = bcd2uint(buf[2]);
-		info->msf_leadout.minute = buf[3];
-		info->msf_leadout.second = buf[4];
-		info->msf_leadout.frame = buf[5];
-		info->msf_first.minute = buf[6];
-		info->msf_first.second = buf[7];
-		info->msf_first.frame = buf[8];
 	}
-	return ans;
+	return -EIO;
 }
 
-static int
-mcdx_setdrivemode(struct s_drive_stuff *stuffp, enum drivemodes mode, int tries)
+void cleanup_module(void)
 {
-	char cmd[2];
-	int ans;
-
-	TRACE((HW, "setdrivemode() %d\n", mode));
+	int i;
 
-	if (-1 == (ans = mcdx_talk(stuffp, "\xc2", 1, cmd, sizeof(cmd), 5 * HZ, tries)))
-		return -1;
+	unregister_blkdev(MAJOR_NR, DEVICE_NAME);
 
-	switch (mode) {
-	  case TOC: cmd[1] |= 0x04; break;
-	  case DATA: cmd[1] &= ~0x04; break;
-	  case RAW: cmd[1] |= 0x40; break;
-	  case COOKED: cmd[1] &= ~0x40; break;
-	  default: break;
-	}
-	cmd[0] = 0x50;
-	return mcdx_talk(stuffp, cmd, 2, NULL, 1, 5 * HZ, tries);
-}
-
-static int
-mcdx_setdatamode(struct s_drive_stuff *stuffp, enum datamodes mode, int tries)
-{
-	unsigned char cmd[2] = { 0xa0 };
-	TRACE((HW, "setdatamode() %d\n", mode));
-	switch (mode) {
-	  case MODE0: cmd[1] = 0x00; break;
-	  case MODE1: cmd[1] = 0x01; break;
-	  case MODE2: cmd[1] = 0x02; break;
-	  default: return -EINVAL;
+	for (i = 0; i < MCDX_NDRIVES; i++) {
+		struct s_drive_stuff *stuffp;
+		stuffp = mcdx_stuffp[i];
+		if (!stuffp)
+			continue;
+		release_region(stuffp->base, MCDX_IO_SIZE);
+		free_irq(stuffp->irq, NULL);
+		if (stuffp->toc) {
+			kfree(stuffp->toc);
+		}
+		mcdx_stuffp[i] = NULL;
+		kfree(stuffp);
 	}
-	return mcdx_talk(stuffp, cmd, 2, NULL, 1, 5 * HZ, tries);
-}
-
-static int
-mcdx_config(struct s_drive_stuff *stuffp, int tries)
-{
-	char cmd[4];
-
-	TRACE((HW, "config()\n"));
-
-	cmd[0] = 0x90;
-
-	cmd[1] = 0x10;		/* irq enable */
-	cmd[2] = 0x05;		/* pre, err irq enable */
-
-	if (-1 == mcdx_talk(stuffp, cmd, 3, NULL, 1, 1 * HZ, tries))
-		return -1;
-
-	cmd[1] = 0x02;		/* dma select */
-	cmd[2] = 0x00;		/* no dma */
-
-	return mcdx_talk(stuffp, cmd, 3, NULL, 1, 1 * HZ, tries);
-}
-
-static int
-mcdx_requestversion(struct s_drive_stuff *stuffp, struct s_version *ver, int tries)
-{
-	char buf[3];
-	int ans;
-
-	if (-1 == (ans = mcdx_talk(stuffp, "\xdc", 
-			1, buf, sizeof(buf), 2 * HZ, tries)))
-		return ans;
-
-	ver->code = buf[1];
-	ver->ver = buf[2];
-
-	return ans;
-}
-
-static int
-mcdx_reset(struct s_drive_stuff *stuffp, enum resetmodes mode, int tries)
-{ 
-	if (mode == HARD) {
-		outb(0, (unsigned int) stuffp->wreg_chn);		/* no dma, no irq -> hardware */
-		outb(0, (unsigned int) stuffp->wreg_reset);		/* hw reset */
-		return 0;
-	} else return mcdx_talk(stuffp, "\x60", 1, NULL, 1, 5 * HZ, tries);
-}
-
-static int
-mcdx_lockdoor(struct s_drive_stuff *stuffp, int lock, int tries)
-{
-	char cmd[2] = { 0xfe };
-    if (stuffp->present & DOOR) {
-        cmd[1] = lock ? 0x01 : 0x00;
-        return mcdx_talk(stuffp, cmd, sizeof(cmd), NULL, 1, 5 * HZ, tries);
-    } else return 0;
+	blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
 }
 
-static int
-mcdx_getstatus(struct s_drive_stuff *stuffp, int tries)
-{ return mcdx_talk(stuffp, "\x40", 1, NULL, 1, 5 * HZ, tries); }
+#else				/* MODULE */
 
-static int
-mcdx_getval(struct s_drive_stuff *stuffp, int to, int delay, char* buf)
-{
-    unsigned long timeout = to + jiffies;
-    char c;
-
-    if (!buf) buf = &c;
-
-    while (inb((unsigned int) stuffp->rreg_status) & MCDX_RBIT_STEN) {
-        if (jiffies > timeout) return -1;
-        mcdx_delay(stuffp, delay);
-    }
-
-    *buf = (unsigned char) inb((unsigned int) stuffp->rreg_data) & 0xff;
-
-    return 0;
-}
-
-static int
-mcdx_setattentuator(
-        struct s_drive_stuff* stuffp, 
-        struct cdrom_volctrl* vol, 
-        int tries)
+/*
+ * This function is used by the kernel in init/main.c to provide semantics
+ * for the corresponding kernel options. It's unused otherwise.
+ */
+void mcdx_setup(char *str, int *pi)
 {
-    char cmd[5];
-    cmd[0] = 0xae;
-    cmd[1] = vol->channel0;
-    cmd[2] = 0;
-    cmd[3] = vol->channel1;
-    cmd[4] = 0;
-
-    return mcdx_talk(stuffp, cmd, sizeof(cmd), NULL, 5, 200, tries);
+	if (pi[0] > 0)
+		mcdx_drive_map[0][0] = pi[1];
+	if (pi[0] > 1)
+		mcdx_drive_map[0][1] = pi[2];
 }
-
-/* ex:set ts=4 sw=4: */
+#endif				/* MODULE */

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen, slshen@lbl.gov with Sam's (original) version
of this