Home | History | Annotate | Line # | Download | only in isa
mcd.c revision 1.32
      1 /*	$NetBSD: mcd.c,v 1.32 1995/03/27 21:44:11 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Charles M. Hannum.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * Copyright 1993 by Holger Veit (data part)
     21  * Copyright 1993 by Brian Moore (audio part)
     22  * All rights reserved.
     23  *
     24  * Redistribution and use in source and binary forms, with or without
     25  * modification, are permitted provided that the following conditions
     26  * are met:
     27  * 1. Redistributions of source code must retain the above copyright
     28  *    notice, this list of conditions and the following disclaimer.
     29  * 2. Redistributions in binary form must reproduce the above copyright
     30  *    notice, this list of conditions and the following disclaimer in the
     31  *    documentation and/or other materials provided with the distribution.
     32  * 3. All advertising materials mentioning features or use of this software
     33  *    must display the following acknowledgement:
     34  *	This software was developed by Holger Veit and Brian Moore
     35  *      for use with "386BSD" and similar operating systems.
     36  *    "Similar operating systems" includes mainly non-profit oriented
     37  *    systems for research and education, including but not restricted to
     38  *    "NetBSD", "FreeBSD", "Mach" (by CMU).
     39  * 4. Neither the name of the developer(s) nor the name "386BSD"
     40  *    may be used to endorse or promote products derived from this
     41  *    software without specific prior written permission.
     42  *
     43  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY
     44  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     46  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER(S) BE
     47  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     48  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
     49  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     50  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     51  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     52  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     53  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     54  */
     55 
     56 /*static char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";*/
     57 
     58 #include <sys/types.h>
     59 #include <sys/param.h>
     60 #include <sys/systm.h>
     61 #include <sys/kernel.h>
     62 #include <sys/proc.h>
     63 #include <sys/conf.h>
     64 #include <sys/file.h>
     65 #include <sys/buf.h>
     66 #include <sys/stat.h>
     67 #include <sys/uio.h>
     68 #include <sys/ioctl.h>
     69 #include <sys/cdio.h>
     70 #include <sys/errno.h>
     71 #include <sys/disklabel.h>
     72 #include <sys/device.h>
     73 #include <sys/disk.h>
     74 
     75 #include <machine/cpu.h>
     76 #include <machine/pio.h>
     77 
     78 #include <i386/isa/isavar.h>
     79 #include <dev/isa/mcdreg.h>
     80 
     81 #ifndef MCDDEBUG
     82 #define MCD_TRACE(fmt,a,b,c,d)
     83 #else
     84 #define MCD_TRACE(fmt,a,b,c,d)	{if (sc->debug) {printf("%s: st=%02x: ", sc->sc_dev.dv_xname, sc->status); printf(fmt,a,b,c,d);}}
     85 #endif
     86 
     87 #define	MCDPART(dev)	DISKPART(dev)
     88 #define	MCDUNIT(dev)	DISKUNIT(dev)
     89 
     90 /* toc */
     91 #define MCD_MAXTOCS	104	/* from the Linux driver */
     92 
     93 struct mcd_mbx {
     94 	int		retry, count;
     95 	struct buf	*bp;
     96 	daddr_t		blkno;
     97 	int		nblk;
     98 	int		sz;
     99 	u_long		skip;
    100 	int		state;
    101 #define	MCD_S_IDLE	0
    102 #define MCD_S_BEGIN	1
    103 #define MCD_S_WAITMODE	2
    104 #define MCD_S_WAITREAD	3
    105 	int		mode;
    106 };
    107 
    108 struct mcd_softc {
    109 	struct	device sc_dev;
    110 	struct	dkdevice sc_dk;
    111 	struct	intrhand sc_ih;
    112 
    113 	int	iobase;
    114 	int	irq, drq;
    115 
    116 	char	*type;
    117 	int	flags;
    118 #define	MCDF_LOCKED	0x01
    119 #define	MCDF_WANTED	0x02
    120 #define	MCDF_WLABEL	0x04	/* label is writable */
    121 #define	MCDF_LABELLING	0x08	/* writing label */
    122 #define	MCDF_LOADED	0x10	/* parameters loaded */
    123 #define	MCDF_HAVETOC	0x20	/* already read toc */
    124 	short	status;
    125 	short	audio_status;
    126 	int	blksize;
    127 	u_long	disksize;
    128 	struct	mcd_volinfo volinfo;
    129 	union	mcd_qchninfo toc[MCD_MAXTOCS];
    130 	struct	mcd_command lastpb;
    131 	struct	mcd_mbx mbx;
    132 	int	lastmode;
    133 #define	MCD_MD_UNKNOWN	-1
    134 	int	lastupc;
    135 #define	MCD_UPC_UNKNOWN	-1
    136 	int	debug;
    137 	struct	buf buf_queue;
    138 };
    139 
    140 /* prototypes */
    141 int mcdopen __P((dev_t, int, int, struct proc *));
    142 int mcdclose __P((dev_t, int, int));
    143 int mcdioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
    144 int mcdsize __P((dev_t));
    145 
    146 static int bcd2bin __P((bcd_t));
    147 static bcd_t bin2bcd __P((int));
    148 static void hsg2msf __P((int, bcd_t *));
    149 static daddr_t msf2hsg __P((bcd_t *, int));
    150 
    151 int mcd_playtracks __P((struct mcd_softc *, struct ioc_play_track *));
    152 int mcd_playmsf __P((struct mcd_softc *, struct ioc_play_msf *));
    153 int mcd_playblocks __P((struct mcd_softc *, struct ioc_play_blocks *));
    154 int mcd_stop __P((struct mcd_softc *));
    155 int mcd_eject __P((struct mcd_softc *));
    156 int mcd_read_subchannel __P((struct mcd_softc *, struct ioc_read_subchannel *));
    157 int mcd_pause __P((struct mcd_softc *));
    158 int mcd_resume __P((struct mcd_softc *));
    159 int mcd_toc_header __P((struct mcd_softc *, struct ioc_toc_header *));
    160 int mcd_toc_entries __P((struct mcd_softc *, struct ioc_read_toc_entry *));
    161 
    162 int mcd_getreply __P((struct mcd_softc *));
    163 int mcd_getstat __P((struct mcd_softc *));
    164 int mcd_getresult __P((struct mcd_softc *, struct mcd_result *));
    165 void mcd_setflags __P((struct mcd_softc *));
    166 int mcd_get __P((struct mcd_softc *, char *, int));
    167 int mcd_send __P((struct mcd_softc *, struct mcd_mbox *, int));
    168 int mcdintr __P((void *));
    169 void mcd_soft_reset __P((struct mcd_softc *));
    170 int mcd_hard_reset __P((struct mcd_softc *));
    171 int mcd_setmode __P((struct mcd_softc *, int));
    172 int mcd_setupc __P((struct mcd_softc *, int));
    173 int mcd_read_toc __P((struct mcd_softc *));
    174 int mcd_getqchan __P((struct mcd_softc *, union mcd_qchninfo *, int));
    175 int mcd_setlock __P((struct mcd_softc *, int));
    176 
    177 int mcdprobe __P((struct device *, void *, void *));
    178 void mcdattach __P((struct device *, struct device *, void *));
    179 
    180 struct cfdriver mcdcd = {
    181 	NULL, "mcd", mcdprobe, mcdattach, DV_DISK, sizeof(struct mcd_softc)
    182 };
    183 
    184 void mcdgetdisklabel __P((struct mcd_softc *));
    185 int mcd_get_parms __P((struct mcd_softc *));
    186 void mcdstrategy __P((struct buf *));
    187 void mcdstart __P((struct mcd_softc *));
    188 
    189 struct dkdriver mcddkdriver = { mcdstrategy };
    190 
    191 #define MCD_RETRIES	3
    192 #define MCD_RDRETRIES	3
    193 
    194 /* several delays */
    195 #define RDELAY_WAITMODE	300
    196 #define RDELAY_WAITREAD	800
    197 
    198 #define	DELAY_GRANULARITY	25	/* 25us */
    199 #define DELAY_GETREPLY		100000	/* 100000 * 25us */
    200 
    201 void
    202 mcdattach(parent, self, aux)
    203 	struct device *parent, *self;
    204 	void *aux;
    205 {
    206 	struct mcd_softc *sc = (void *)self;
    207 	struct isa_attach_args *ia = aux;
    208 	struct mcd_mbox mbx;
    209 
    210 	printf(": model %s\n", sc->type != 0 ? sc->type : "unknown");
    211 
    212 	(void) mcd_setlock(sc, MCD_LK_UNLOCK);
    213 
    214 	mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
    215 	mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
    216 	mbx.cmd.data.config.subcommand = MCD_CF_IRQENABLE;
    217 	mbx.cmd.data.config.data1 = 0x01;
    218 	mbx.res.length = 0;
    219 	(void) mcd_send(sc, &mbx, 0);
    220 
    221 	mcd_soft_reset(sc);
    222 
    223 	sc->sc_dk.dk_driver = &mcddkdriver;
    224 
    225 	sc->sc_ih.ih_fun = mcdintr;
    226 	sc->sc_ih.ih_arg = sc;
    227 	sc->sc_ih.ih_level = IPL_BIO;
    228 	intr_establish(ia->ia_irq, IST_EDGE, &sc->sc_ih);
    229 }
    230 
    231 int
    232 mcdlockwait(sc)
    233 	struct mcd_softc *sc;
    234 {
    235 	int error;
    236 
    237 	while ((sc->flags & MCDF_LOCKED) != 0) {
    238 		sc->flags |= MCDF_WANTED;
    239 		if ((error = tsleep(sc, PRIBIO | PCATCH, "mcdlck", 0)) != 0)
    240 			return error;
    241 	}
    242 	return 0;
    243 }
    244 
    245 void
    246 mcdunlock(sc)
    247 	struct mcd_softc *sc;
    248 {
    249 
    250 	sc->flags &= ~MCDF_LOCKED;
    251 	if ((sc->flags & MCDF_WANTED) != 0) {
    252 		sc->flags &= ~MCDF_WANTED;
    253 		wakeup(sc);
    254 	}
    255 }
    256 
    257 int
    258 mcdopen(dev, flag, fmt, p)
    259 	dev_t dev;
    260 	int flag, fmt;
    261 	struct proc *p;
    262 {
    263 	int error;
    264 	int unit, part;
    265 	struct mcd_softc *sc;
    266 
    267 	unit = MCDUNIT(dev);
    268 	if (unit >= mcdcd.cd_ndevs)
    269 		return ENXIO;
    270 	sc = mcdcd.cd_devs[unit];
    271 	if (!sc)
    272 		return ENXIO;
    273 
    274 	part = MCDPART(dev);
    275 
    276 	if (error = mcdlockwait(sc))
    277 		return error;
    278 
    279 	if (sc->sc_dk.dk_openmask != 0) {
    280 		/*
    281 		 * If any partition is open, but the disk has been invalidated,
    282 		 * disallow further opens.
    283 		 */
    284 		if ((sc->flags & MCDF_LOADED) == 0)
    285 			return ENXIO;
    286 	} else {
    287 		sc->flags |= MCDF_LOCKED;
    288 
    289 		/*
    290 		 * Lock the drawer.  This will also notice any pending disk
    291 		 * change or door open indicator and clear the MCDF_LOADED bit
    292 		 * if necessary.
    293 		 */
    294 		(void) mcd_setlock(sc, MCD_LK_LOCK);
    295 
    296 		if ((sc->flags & MCDF_LOADED) == 0) {
    297 			/* Partially reset the state. */
    298 			sc->flags &= ~MCDF_HAVETOC;
    299 			sc->lastmode = MCD_MD_UNKNOWN;
    300 			sc->lastupc = MCD_UPC_UNKNOWN;
    301 
    302 			sc->flags |= MCDF_LOADED;
    303 
    304 			/* Set the mode, causing the disk to spin up. */
    305 			if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
    306 				goto bad2;
    307 
    308 			/* Load the physical device parameters. */
    309 			if (mcd_get_parms(sc) != 0) {
    310 				error = ENXIO;
    311 				goto bad2;
    312 			}
    313 
    314 			/* Fabricate a disk label. */
    315 			mcdgetdisklabel(sc);
    316 		}
    317 
    318 		mcdunlock(sc);
    319 	}
    320 
    321 	MCD_TRACE("open: partition=%d disksize=%d blksize=%d\n", part,
    322 	    sc->disksize, sc->blksize, 0);
    323 
    324 	/* Check that the partition exists. */
    325 	if (part != RAW_PART &&
    326 	    (part >= sc->sc_dk.dk_label.d_npartitions ||
    327 	     sc->sc_dk.dk_label.d_partitions[part].p_fstype == FS_UNUSED)) {
    328 		error = ENXIO;
    329 		goto bad;
    330 	}
    331 
    332 	/* Insure only one open at a time. */
    333 	switch (fmt) {
    334 	case S_IFCHR:
    335 		sc->sc_dk.dk_copenmask |= (1 << part);
    336 		break;
    337 	case S_IFBLK:
    338 		sc->sc_dk.dk_bopenmask |= (1 << part);
    339 		break;
    340 	}
    341 	sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    342 
    343 	return 0;
    344 
    345 bad2:
    346 	sc->flags &= ~MCDF_LOADED;
    347 
    348 bad:
    349 	if (sc->sc_dk.dk_openmask == 0) {
    350 #if 0
    351 		(void) mcd_setmode(sc, MCD_MD_SLEEP);
    352 #endif
    353 		(void) mcd_setlock(sc, MCD_LK_UNLOCK);
    354 
    355 		mcdunlock(sc);
    356 	}
    357 
    358 	return error;
    359 }
    360 
    361 int
    362 mcdclose(dev, flag, fmt)
    363 	dev_t dev;
    364 	int flag, fmt;
    365 {
    366 	struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(dev)];
    367 	int part = MCDPART(dev);
    368 	int s;
    369 
    370 	MCD_TRACE("close: partition=%d\n", part, 0, 0, 0);
    371 
    372 	switch (fmt) {
    373 	case S_IFCHR:
    374 		sc->sc_dk.dk_copenmask &= ~(1 << part);
    375 		break;
    376 	case S_IFBLK:
    377 		sc->sc_dk.dk_bopenmask &= ~(1 << part);
    378 		break;
    379 	}
    380 	sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    381 
    382 	if (sc->sc_dk.dk_openmask == 0) {
    383 		/*
    384 		 * If we're closing the last partition, nobody else could be
    385 		 * holding the lock, so don't bother to check.
    386 		 */
    387 		sc->flags |= MCDF_LOCKED;
    388 
    389 #if 0
    390 		s = splbio();
    391 		while (...) {
    392 			sc->flags |= MCDF_WAITING;
    393 			if ((error = tsleep(sc, PRIBIO | PCATCH, "mcdcls", 0)) != 0)
    394 				return error;
    395 		}
    396 		splx(s);
    397 #endif
    398 
    399 #if 0
    400 		(void) mcd_setmode(sc, MCD_MD_SLEEP);
    401 #endif
    402 		(void) mcd_setlock(sc, MCD_LK_UNLOCK);
    403 
    404 		mcdunlock(sc);
    405 	}
    406 
    407 	return 0;
    408 }
    409 
    410 void
    411 mcdstrategy(bp)
    412 	struct buf *bp;
    413 {
    414 	struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(bp->b_dev)];
    415 	int s;
    416 
    417 	/* Test validity. */
    418 	MCD_TRACE("strategy: buf=0x%lx blkno=%ld bcount=%ld\n", bp,
    419 	    bp->b_blkno, bp->b_bcount, 0);
    420 	if (bp->b_blkno < 0 ||
    421 	    (bp->b_bcount % sc->blksize) != 0) {
    422 		printf("%s: strategy: blkno = %d bcount = %d\n",
    423 		    sc->sc_dev.dv_xname, bp->b_blkno, bp->b_bcount);
    424 		bp->b_error = EINVAL;
    425 		goto bad;
    426 	}
    427 
    428 	/* If device invalidated (e.g. media change, door open), error. */
    429 	if ((sc->flags & MCDF_LOADED) == 0) {
    430 		MCD_TRACE("strategy: drive not valid\n", 0, 0, 0, 0);
    431 		bp->b_error = EIO;
    432 		goto bad;
    433 	}
    434 
    435 	/* No data to read. */
    436 	if (bp->b_bcount == 0)
    437 		goto done;
    438 
    439 	/*
    440 	 * Do bounds checking, adjust transfer. if error, process.
    441 	 * If end of partition, just return.
    442 	 */
    443 	if (bounds_check_with_label(bp, &sc->sc_dk.dk_label,
    444 	    (sc->flags & (MCDF_WLABEL|MCDF_LABELLING)) != 0) <= 0)
    445 		goto done;
    446 
    447 	/* Queue it. */
    448 	s = splbio();
    449 	disksort(&sc->buf_queue, bp);
    450 	splx(s);
    451 	if (!sc->buf_queue.b_active)
    452 		mcdstart(sc);
    453 	return;
    454 
    455 bad:
    456 	bp->b_flags |= B_ERROR;
    457 done:
    458 	bp->b_resid = bp->b_bcount;
    459 	biodone(bp);
    460 }
    461 
    462 void
    463 mcdstart(sc)
    464 	struct mcd_softc *sc;
    465 {
    466 	struct buf *bp, *dp = &sc->buf_queue;
    467 	int s;
    468 
    469 loop:
    470 	s = splbio();
    471 
    472 	bp = dp->b_actf;
    473 	if (bp == NULL) {
    474 		/* Nothing to do. */
    475 		dp->b_active = 0;
    476 		splx(s);
    477 		return;
    478 	}
    479 
    480 	/* Block found to process; dequeue. */
    481 	MCD_TRACE("start: found block bp=0x%x\n", bp, 0, 0, 0);
    482 	dp->b_actf = bp->b_actf;
    483 	splx(s);
    484 
    485 	/* Changed media? */
    486 	if ((sc->flags & MCDF_LOADED) == 0) {
    487 		MCD_TRACE("start: drive not valid\n", 0, 0, 0, 0);
    488 		bp->b_error = EIO;
    489 		bp->b_flags |= B_ERROR;
    490 		biodone(bp);
    491 		goto loop;
    492 	}
    493 
    494 	dp->b_active = 1;
    495 
    496 	sc->mbx.retry = MCD_RDRETRIES;
    497 	sc->mbx.bp = bp;
    498 	sc->mbx.blkno = bp->b_blkno / (sc->blksize / DEV_BSIZE);
    499 	if (MCDPART(bp->b_dev) != RAW_PART) {
    500 		struct partition *p;
    501 		p = &sc->sc_dk.dk_label.d_partitions[MCDPART(bp->b_dev)];
    502 		sc->mbx.blkno += p->p_offset;
    503 	}
    504 	sc->mbx.nblk = bp->b_bcount / sc->blksize;
    505 	sc->mbx.sz = sc->blksize;
    506 	sc->mbx.skip = 0;
    507 	sc->mbx.state = MCD_S_BEGIN;
    508 	sc->mbx.mode = MCD_MD_COOKED;
    509 
    510 	s = splbio();
    511 	(void) mcdintr(sc);
    512 	splx(s);
    513 }
    514 
    515 int
    516 mcdioctl(dev, cmd, addr, flag, p)
    517 	dev_t dev;
    518 	u_long cmd;
    519 	caddr_t addr;
    520 	int flag;
    521 	struct proc *p;
    522 {
    523 	struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(dev)];
    524 	int error;
    525 
    526 	MCD_TRACE("ioctl: cmd=0x%x\n", cmd, 0, 0, 0);
    527 
    528 	if ((sc->flags & MCDF_LOADED) == 0)
    529 		return EIO;
    530 
    531 	switch (cmd) {
    532 	case DIOCGDINFO:
    533 		*(struct disklabel *)addr = sc->sc_dk.dk_label;
    534 		return 0;
    535 
    536 	case DIOCGPART:
    537 		((struct partinfo *)addr)->disklab = &sc->sc_dk.dk_label;
    538 		((struct partinfo *)addr)->part =
    539 		    &sc->sc_dk.dk_label.d_partitions[MCDPART(dev)];
    540 		return 0;
    541 
    542 	case DIOCWDINFO:
    543 	case DIOCSDINFO:
    544 		if ((flag & FWRITE) == 0)
    545 			return EBADF;
    546 
    547 		if (error = mcdlockwait(sc))
    548 			return error;
    549 		sc->flags |= MCDF_LOCKED | MCDF_LABELLING;
    550 
    551 		error = setdisklabel(&sc->sc_dk.dk_label,
    552 		    (struct disklabel *)addr, /*sc->sc_dk.dk_openmask : */0,
    553 		    &sc->sc_dk.dk_cpulabel);
    554 		if (error == 0) {
    555 		}
    556 
    557 		sc->flags &= ~MCDF_LABELLING;
    558 		mcdunlock(sc);
    559 		return error;
    560 
    561 	case DIOCWLABEL:
    562 		return EBADF;
    563 
    564 	case CDIOCPLAYTRACKS:
    565 		return mcd_playtracks(sc, (struct ioc_play_track *)addr);
    566 	case CDIOCPLAYMSF:
    567 		return mcd_playmsf(sc, (struct ioc_play_msf *)addr);
    568 	case CDIOCPLAYBLOCKS:
    569 		return mcd_playblocks(sc, (struct ioc_play_blocks *)addr);
    570 	case CDIOCREADSUBCHANNEL:
    571 		return mcd_read_subchannel(sc, (struct ioc_read_subchannel *)addr);
    572 	case CDIOREADTOCHEADER:
    573 		return mcd_toc_header(sc, (struct ioc_toc_header *)addr);
    574 	case CDIOREADTOCENTRYS:
    575 		return mcd_toc_entries(sc, (struct ioc_read_toc_entry *)addr);
    576 	case CDIOCSETPATCH:
    577 	case CDIOCGETVOL:
    578 	case CDIOCSETVOL:
    579 	case CDIOCSETMONO:
    580 	case CDIOCSETSTEREO:
    581 	case CDIOCSETMUTE:
    582 	case CDIOCSETLEFT:
    583 	case CDIOCSETRIGHT:
    584 		return EINVAL;
    585 	case CDIOCRESUME:
    586 		return mcd_resume(sc);
    587 	case CDIOCPAUSE:
    588 		return mcd_pause(sc);
    589 	case CDIOCSTART:
    590 		return EINVAL;
    591 	case CDIOCSTOP:
    592 		return mcd_stop(sc);
    593 	case CDIOCEJECT:
    594 		return mcd_eject(sc);
    595 	case CDIOCALLOW:
    596 		return mcd_setlock(sc, MCD_LK_UNLOCK);
    597 	case CDIOCPREVENT:
    598 		return mcd_setlock(sc, MCD_LK_LOCK);
    599 	case CDIOCSETDEBUG:
    600 		sc->debug = 1;
    601 		return 0;
    602 	case CDIOCCLRDEBUG:
    603 		sc->debug = 0;
    604 		return 0;
    605 	case CDIOCRESET:
    606 		return mcd_hard_reset(sc);
    607 
    608 	default:
    609 		return ENOTTY;
    610 	}
    611 
    612 #ifdef DIAGNOSTIC
    613 	panic("mcdioctl: impossible");
    614 #endif
    615 }
    616 
    617 /*
    618  * This could have been taken from scsi/cd.c, but it is not clear
    619  * whether the scsi cd driver is linked in.
    620  */
    621 void
    622 mcdgetdisklabel(sc)
    623 	struct mcd_softc *sc;
    624 {
    625 
    626 	bzero(&sc->sc_dk.dk_label, sizeof(struct disklabel));
    627 	bzero(&sc->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
    628 
    629 	sc->sc_dk.dk_label.d_secsize = sc->blksize;
    630 	sc->sc_dk.dk_label.d_ntracks = 1;
    631 	sc->sc_dk.dk_label.d_nsectors = 100;
    632 	sc->sc_dk.dk_label.d_ncylinders = (sc->disksize / 100) + 1;
    633 	sc->sc_dk.dk_label.d_secpercyl =
    634 	    sc->sc_dk.dk_label.d_ntracks * sc->sc_dk.dk_label.d_nsectors;
    635 
    636 	strncpy(sc->sc_dk.dk_label.d_typename, "Mitsumi CD-ROM", 16);
    637 	sc->sc_dk.dk_label.d_type = 0;	/* XXX */
    638 	strncpy(sc->sc_dk.dk_label.d_packname, "fictitious", 16);
    639 	sc->sc_dk.dk_label.d_secperunit = sc->disksize;
    640 	sc->sc_dk.dk_label.d_rpm = 300;
    641 	sc->sc_dk.dk_label.d_interleave = 1;
    642 	sc->sc_dk.dk_label.d_flags = D_REMOVABLE;
    643 
    644 	sc->sc_dk.dk_label.d_partitions[0].p_offset = 0;
    645 	sc->sc_dk.dk_label.d_partitions[0].p_size =
    646 	    sc->sc_dk.dk_label.d_secperunit *
    647 	    (sc->sc_dk.dk_label.d_secsize / DEV_BSIZE);
    648 	sc->sc_dk.dk_label.d_partitions[0].p_fstype = FS_ISO9660;
    649 	sc->sc_dk.dk_label.d_npartitions = 1;
    650 
    651 	sc->sc_dk.dk_label.d_magic = DISKMAGIC;
    652 	sc->sc_dk.dk_label.d_magic2 = DISKMAGIC;
    653 	sc->sc_dk.dk_label.d_checksum = dkcksum(&sc->sc_dk.dk_label);
    654 }
    655 
    656 int
    657 mcd_get_parms(sc)
    658 	struct mcd_softc *sc;
    659 {
    660 	struct mcd_mbox mbx;
    661 	daddr_t size;
    662 	int error;
    663 
    664 	/* Send volume info command. */
    665 	mbx.cmd.opcode = MCD_CMDGETVOLINFO;
    666 	mbx.cmd.length = 0;
    667 	mbx.res.length = sizeof(mbx.res.data.volinfo);
    668 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
    669 		return error;
    670 
    671 	if (mbx.res.data.volinfo.trk_low == 0x00 &&
    672 	    mbx.res.data.volinfo.trk_high == 0x00)
    673 		return EINVAL;
    674 
    675 	/* Volinfo is OK. */
    676 	sc->volinfo = mbx.res.data.volinfo;
    677 	sc->blksize = MCD_BLKSIZE_COOKED;
    678 	size = msf2hsg(sc->volinfo.vol_msf, 0);
    679 	sc->disksize = size * (MCD_BLKSIZE_COOKED / DEV_BSIZE);
    680 	return 0;
    681 }
    682 
    683 int
    684 mcdsize(dev)
    685 	dev_t dev;
    686 {
    687 
    688 	/* CD-ROMs are read-only. */
    689 	return -1;
    690 }
    691 
    692 int
    693 mcddump()
    694 {
    695 
    696 	/* Not implemented. */
    697 	return EINVAL;
    698 }
    699 
    700 int
    701 mcdprobe(parent, match, aux)
    702 	struct device *parent;
    703 	void *match, *aux;
    704 {
    705 	struct mcd_softc *sc = match;
    706 	struct isa_attach_args *ia = aux;
    707 	int iobase = ia->ia_iobase;
    708 	int i;
    709 	struct mcd_mbox mbx;
    710 
    711 	sc->iobase = iobase;
    712 
    713 	/* Send a reset. */
    714 	outb(iobase + MCD_RESET, 0);
    715 	delay(1000000);
    716 	/* Get any pending status and throw away. */
    717 	for (i = 10; i; i--)
    718 		inb(iobase + MCD_STATUS);
    719 	delay(1000);
    720 
    721 	/* Send get status command. */
    722 	mbx.cmd.opcode = MCD_CMDGETSTAT;
    723 	mbx.cmd.length = 0;
    724 	mbx.res.length = 0;
    725 	if (mcd_send(sc, &mbx, 0) != 0)
    726 		return 0;
    727 
    728 	/* Get info about the drive. */
    729 	mbx.cmd.opcode = MCD_CMDCONTINFO;
    730 	mbx.cmd.length = 0;
    731 	mbx.res.length = sizeof(mbx.res.data.continfo);
    732 	if (mcd_send(sc, &mbx, 0) != 0)
    733 		return 0;
    734 
    735 	/*
    736 	 * The following is code which is not guaranteed to work for all
    737 	 * drives, because the meaning of the expected 'M' is not clear
    738 	 * (M_itsumi is an obvious assumption, but I don't trust that).
    739 	 * Also, the original hack had a bogus condition that always
    740 	 * returned true.
    741 	 *
    742 	 * Note:  Which models support interrupts?  >=LU005S?
    743 	 */
    744 	switch (mbx.res.data.continfo.code) {
    745 	case 'M':
    746 		if (mbx.res.data.continfo.version <= 2)
    747 			sc->type = "LU002S";
    748 		else if (mbx.res.data.continfo.version <= 5)
    749 			sc->type = "LU005S";
    750 		else
    751 			sc->type = "LU006S";
    752 		break;
    753 	case 'F':
    754 		sc->type = "FX001";
    755 		break;
    756 	case 'D':
    757 		sc->type = "FX001D";
    758 		break;
    759 	default:
    760 		printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n",
    761 		    sc->sc_dev.dv_xname,
    762 		    mbx.res.data.continfo.code, mbx.res.data.continfo.version);
    763 		sc->type = 0;
    764 		break;
    765 	}
    766 
    767 	ia->ia_iosize = 4;
    768 	ia->ia_msize = 0;
    769 	return 1;
    770 }
    771 
    772 int
    773 mcd_getreply(sc)
    774 	struct mcd_softc *sc;
    775 {
    776 	int iobase = sc->iobase;
    777 	int i;
    778 
    779 	/* Wait until xfer port senses data ready. */
    780 	for (i = DELAY_GETREPLY; i; i--) {
    781 		if ((inb(iobase + MCD_XFER) & MCD_XF_STATUSUNAVAIL) == 0)
    782 			break;
    783 		delay(DELAY_GRANULARITY);
    784 	}
    785 	if (!i)
    786 		return -1;
    787 
    788 	/* Get the data. */
    789 	return inb(iobase + MCD_STATUS);
    790 }
    791 
    792 int
    793 mcd_getstat(sc)
    794 	struct mcd_softc *sc;
    795 {
    796 	struct mcd_mbox mbx;
    797 
    798 	mbx.cmd.opcode = MCD_CMDGETSTAT;
    799 	mbx.cmd.length = 0;
    800 	mbx.res.length = 0;
    801 	return mcd_send(sc, &mbx, 1);
    802 }
    803 
    804 int
    805 mcd_getresult(sc, res)
    806 	struct mcd_softc *sc;
    807 	struct mcd_result *res;
    808 {
    809 	int i, x;
    810 
    811 	if (sc->debug)
    812 		printf("%s: mcd_getresult: %d", sc->sc_dev.dv_xname,
    813 		    res->length);
    814 
    815 	if ((x = mcd_getreply(sc)) < 0) {
    816 		if (sc->debug)
    817 			printf(" timeout\n");
    818 		else
    819 			printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
    820 		return EIO;
    821 	}
    822 	if (sc->debug)
    823 		printf(" %02x", (u_int)x);
    824 	sc->status = x;
    825 	mcd_setflags(sc);
    826 
    827 	if ((sc->status & MCD_ST_CMDCHECK) != 0)
    828 		return EINVAL;
    829 
    830 	for (i = 0; i < res->length; i++) {
    831 		if ((x = mcd_getreply(sc)) < 0) {
    832 			if (sc->debug)
    833 				printf(" timeout\n");
    834 			else
    835 				printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
    836 			return EIO;
    837 		}
    838 		if (sc->debug)
    839 			printf(" %02x", (u_int)x);
    840 		res->data.raw.data[i] = x;
    841 	}
    842 
    843 	if (sc->debug)
    844 		printf(" succeeded\n");
    845 
    846 #if 1
    847 	delay(10);
    848 	while ((inb(sc->iobase + MCD_XFER) & MCD_XF_STATUSUNAVAIL) == 0) {
    849 		x = inb(sc->iobase + MCD_STATUS);
    850 		printf("%s: got extra byte %02x during getstatus\n",
    851 		    sc->sc_dev.dv_xname, (u_int)x);
    852 		delay(10);
    853 	}
    854 #endif
    855 
    856 	return 0;
    857 }
    858 
    859 void
    860 mcd_setflags(sc)
    861 	struct mcd_softc *sc;
    862 {
    863 
    864 	/* Check flags. */
    865 	if ((sc->flags & MCDF_LOADED) != 0 &&
    866 	    (sc->status & (MCD_ST_DSKCHNG | MCD_ST_DSKIN | MCD_ST_DOOROPEN)) !=
    867 	    MCD_ST_DSKIN) {
    868 		if ((sc->status & MCD_ST_DOOROPEN) != 0)
    869 			printf("%s: door open\n", sc->sc_dev.dv_xname);
    870 		else if ((sc->status & MCD_ST_DSKIN) == 0)
    871 			printf("%s: no disk present\n", sc->sc_dev.dv_xname);
    872 		else if ((sc->status & MCD_ST_DSKCHNG) != 0)
    873 			printf("%s: media change\n", sc->sc_dev.dv_xname);
    874 		sc->flags &= ~MCDF_LOADED;
    875 	}
    876 
    877 	if ((sc->status & MCD_ST_AUDIOBSY) != 0)
    878 		sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
    879 	else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS ||
    880 		 sc->audio_status == CD_AS_AUDIO_INVALID)
    881 		sc->audio_status = CD_AS_PLAY_COMPLETED;
    882 }
    883 
    884 int
    885 mcd_send(sc, mbx, diskin)
    886 	struct mcd_softc *sc;
    887 	struct mcd_mbox *mbx;
    888 	int diskin;
    889 {
    890 	int iobase = sc->iobase;
    891 	int retry, i, error;
    892 
    893 	if (sc->debug) {
    894 		printf("%s: mcd_send: %d %02x", sc->sc_dev.dv_xname,
    895 		    mbx->cmd.length, (u_int)mbx->cmd.opcode);
    896 		for (i = 0; i < mbx->cmd.length; i++)
    897 			printf(" %02x", (u_int)mbx->cmd.data.raw.data[i]);
    898 		printf("\n");
    899 	}
    900 
    901 	for (retry = MCD_RETRIES; retry; retry--) {
    902 		outb(iobase + MCD_COMMAND, mbx->cmd.opcode);
    903 		for (i = 0; i < mbx->cmd.length; i++)
    904 			outb(iobase + MCD_COMMAND, mbx->cmd.data.raw.data[i]);
    905 		if ((error = mcd_getresult(sc, &mbx->res)) == 0)
    906 			break;
    907 		if (error == EINVAL)
    908 			return error;
    909 	}
    910 	if (!retry)
    911 		return error;
    912 	if (diskin && (sc->flags & MCDF_LOADED) == 0)
    913 		return EIO;
    914 
    915 	return 0;
    916 }
    917 
    918 static int
    919 bcd2bin(b)
    920 	bcd_t b;
    921 {
    922 
    923 	return (b >> 4) * 10 + (b & 15);
    924 }
    925 
    926 static bcd_t
    927 bin2bcd(b)
    928 	int b;
    929 {
    930 
    931 	return ((b / 10) << 4) | (b % 10);
    932 }
    933 
    934 static void
    935 hsg2msf(hsg, msf)
    936 	int hsg;
    937 	bcd_t *msf;
    938 {
    939 
    940 	hsg += 150;
    941 	F_msf(msf) = bin2bcd(hsg % 75);
    942 	hsg /= 75;
    943 	S_msf(msf) = bin2bcd(hsg % 60);
    944 	hsg /= 60;
    945 	M_msf(msf) = bin2bcd(hsg);
    946 }
    947 
    948 static daddr_t
    949 msf2hsg(msf, relative)
    950 	bcd_t *msf;
    951 	int relative;
    952 {
    953 	daddr_t blkno;
    954 
    955 	blkno = bcd2bin(M_msf(msf)) * 75 * 60 +
    956 		bcd2bin(S_msf(msf)) * 75 +
    957 		bcd2bin(F_msf(msf));
    958 	if (!relative)
    959 		blkno -= 150;
    960 	return blkno;
    961 }
    962 
    963 void
    964 mcd_pseudointr(sc)
    965 	struct mcd_softc *sc;
    966 {
    967 	int s;
    968 
    969 	s = splbio();
    970 	(void) mcdintr(sc);
    971 	splx(s);
    972 }
    973 
    974 /*
    975  * State machine to process read requests.
    976  * Initialize with MCD_S_BEGIN: calculate sizes, and set mode
    977  * MCD_S_WAITMODE: waits for status reply from set mode, set read command
    978  * MCD_S_WAITREAD: wait for read ready, read data.
    979  */
    980 int
    981 mcdintr(arg)
    982 	void *arg;
    983 {
    984 	struct mcd_softc *sc = arg;
    985 	struct mcd_mbx *mbx = &sc->mbx;
    986 	int iobase = sc->iobase;
    987 	struct buf *bp = mbx->bp;
    988 
    989 	int i;
    990 	u_char x;
    991 	bcd_t msf[3];
    992 
    993 	switch (mbx->state) {
    994 	case MCD_S_IDLE:
    995 		return 0;
    996 
    997 	case MCD_S_BEGIN:
    998 	tryagain:
    999 		if (mbx->mode == sc->lastmode)
   1000 			goto firstblock;
   1001 
   1002 		sc->lastmode = MCD_MD_UNKNOWN;
   1003 		outb(iobase + MCD_COMMAND, MCD_CMDSETMODE);
   1004 		outb(iobase + MCD_COMMAND, mbx->mode);
   1005 
   1006 		mbx->count = RDELAY_WAITMODE;
   1007 		mbx->state = MCD_S_WAITMODE;
   1008 
   1009 	case MCD_S_WAITMODE:
   1010 		untimeout(mcd_pseudointr, sc);
   1011 		for (i = 20; i; i--) {
   1012 			x = inb(iobase + MCD_XFER);
   1013 			if ((x & MCD_XF_STATUSUNAVAIL) == 0)
   1014 				break;
   1015 			delay(50);
   1016 		}
   1017 		if (i == 0)
   1018 			goto hold;
   1019 		sc->status = inb(iobase + MCD_STATUS);
   1020 		mcd_setflags(sc);
   1021 		if ((sc->flags & MCDF_LOADED) == 0)
   1022 			goto changed;
   1023 		MCD_TRACE("doread: got WAITMODE delay=%d\n",
   1024 		    RDELAY_WAITMODE - mbx->count, 0, 0, 0);
   1025 
   1026 		sc->lastmode = mbx->mode;
   1027 
   1028 	firstblock:
   1029 		MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", mbx->blkno,
   1030 		    bp, 0, 0);
   1031 
   1032 		/* Build parameter block. */
   1033 		hsg2msf(mbx->blkno, msf);
   1034 
   1035 		/* Send the read command. */
   1036 		outb(iobase + MCD_COMMAND, MCD_CMDREAD2);
   1037 		outb(iobase + MCD_COMMAND, msf[0]);
   1038 		outb(iobase + MCD_COMMAND, msf[1]);
   1039 		outb(iobase + MCD_COMMAND, msf[2]);
   1040 		outb(iobase + MCD_COMMAND, 0);
   1041 		outb(iobase + MCD_COMMAND, 0);
   1042 		outb(iobase + MCD_COMMAND, mbx->nblk);
   1043 
   1044 		mbx->count = RDELAY_WAITREAD;
   1045 		mbx->state = MCD_S_WAITREAD;
   1046 
   1047 	case MCD_S_WAITREAD:
   1048 		untimeout(mcd_pseudointr, sc);
   1049 	nextblock:
   1050 	loop:
   1051 		for (i = 20; i; i--) {
   1052 			x = inb(iobase + MCD_XFER);
   1053 			if ((x & MCD_XF_DATAUNAVAIL) == 0)
   1054 				goto gotblock;
   1055 			if ((x & MCD_XF_STATUSUNAVAIL) == 0)
   1056 				break;
   1057 			delay(50);
   1058 		}
   1059 		if (i == 0)
   1060 			goto hold;
   1061 		sc->status = inb(iobase + MCD_STATUS);
   1062 		mcd_setflags(sc);
   1063 		if ((sc->flags & MCDF_LOADED) == 0)
   1064 			goto changed;
   1065 #if 0
   1066 		printf("%s: got status byte %02x during read\n",
   1067 		    sc->sc_dev.dv_xname, (u_int)sc->status);
   1068 #endif
   1069 		goto loop;
   1070 
   1071 	gotblock:
   1072 		MCD_TRACE("doread: got data delay=%d\n",
   1073 		    RDELAY_WAITREAD - mbx->count, 0, 0, 0);
   1074 
   1075 		/* Data is ready. */
   1076 		outb(iobase + MCD_CTL2, 0x04);	/* XXX */
   1077 		insb(iobase + MCD_RDATA, bp->b_data + mbx->skip, mbx->sz);
   1078 		outb(iobase + MCD_CTL2, 0x0c);	/* XXX */
   1079 		mbx->blkno += 1;
   1080 		mbx->skip += mbx->sz;
   1081 		if (--mbx->nblk > 0)
   1082 			goto nextblock;
   1083 
   1084 		mbx->state = MCD_S_IDLE;
   1085 
   1086 		/* Return buffer. */
   1087 		bp->b_resid = 0;
   1088 		biodone(bp);
   1089 
   1090 		mcdstart(sc);
   1091 		return 1;
   1092 
   1093 	hold:
   1094 		if (mbx->count-- < 0) {
   1095 			printf("%s: timeout in state %d",
   1096 			    sc->sc_dev.dv_xname, mbx->state);
   1097 			goto readerr;
   1098 		}
   1099 
   1100 #if 0
   1101 		printf("%s: sleep in state %d\n", sc->sc_dev.dv_xname,
   1102 		    mbx->state);
   1103 #endif
   1104 		timeout(mcd_pseudointr, sc, hz / 100);
   1105 		return -1;
   1106 	}
   1107 
   1108 readerr:
   1109 	if (mbx->retry-- > 0) {
   1110 		printf("; retrying\n");
   1111 		goto tryagain;
   1112 	} else
   1113 		printf("; giving up\n");
   1114 
   1115 changed:
   1116 harderr:
   1117 	/* Invalidate the buffer. */
   1118 	bp->b_flags |= B_ERROR;
   1119 	bp->b_resid = bp->b_bcount - mbx->skip;
   1120 	biodone(bp);
   1121 
   1122 	mcdstart(sc);
   1123 	return -1;
   1124 
   1125 #ifdef notyet
   1126 	printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname);
   1127 	outb(mbx->iobase + MCD_RESET, MCD_CMDRESET);
   1128 	delay(300000);
   1129 	(void) mcd_getstat(sc, 1);
   1130 	(void) mcd_getstat(sc, 1);
   1131 	/*sc->status &= ~MCD_ST_DSKCHNG; */
   1132 	sc->debug = 1; /* preventive set debug mode */
   1133 #endif
   1134 }
   1135 
   1136 void
   1137 mcd_soft_reset(sc)
   1138 	struct mcd_softc *sc;
   1139 {
   1140 
   1141 	sc->debug = 0;
   1142 	sc->flags = 0;
   1143 	sc->lastmode = MCD_MD_UNKNOWN;
   1144 	sc->lastupc = MCD_UPC_UNKNOWN;
   1145 	sc->audio_status = CD_AS_AUDIO_INVALID;
   1146 	outb(sc->iobase + MCD_CTL2, 0x0c);	/* XXX */
   1147 }
   1148 
   1149 int
   1150 mcd_hard_reset(sc)
   1151 	struct mcd_softc *sc;
   1152 {
   1153 	struct mcd_mbox mbx;
   1154 
   1155 	mcd_soft_reset(sc);
   1156 
   1157 	mbx.cmd.opcode = MCD_CMDRESET;
   1158 	mbx.cmd.length = 0;
   1159 	mbx.res.length = 0;
   1160 	return mcd_send(sc, &mbx, 0);
   1161 }
   1162 
   1163 int
   1164 mcd_setmode(sc, mode)
   1165 	struct mcd_softc *sc;
   1166 	int mode;
   1167 {
   1168 	struct mcd_mbox mbx;
   1169 	int error;
   1170 
   1171 	if (sc->lastmode == mode)
   1172 		return 0;
   1173 	if (sc->debug)
   1174 		printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode);
   1175 	sc->lastmode = MCD_MD_UNKNOWN;
   1176 
   1177 	mbx.cmd.opcode = MCD_CMDSETMODE;
   1178 	mbx.cmd.length = sizeof(mbx.cmd.data.datamode);
   1179 	mbx.cmd.data.datamode.mode = mode;
   1180 	mbx.res.length = 0;
   1181 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
   1182 		return error;
   1183 
   1184 	sc->lastmode = mode;
   1185 	return 0;
   1186 }
   1187 
   1188 int
   1189 mcd_setupc(sc, upc)
   1190 	struct mcd_softc *sc;
   1191 	int upc;
   1192 {
   1193 	struct mcd_mbox mbx;
   1194 	int error;
   1195 
   1196 	if (sc->lastupc == upc)
   1197 		return 0;
   1198 	if (sc->debug)
   1199 		printf("%s: setting upc to %d\n", sc->sc_dev.dv_xname, upc);
   1200 	sc->lastupc = MCD_UPC_UNKNOWN;
   1201 
   1202 	mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
   1203 	mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
   1204 	mbx.cmd.data.config.subcommand = MCD_CF_READUPC;
   1205 	mbx.cmd.data.config.data1 = upc;
   1206 	mbx.res.length = 0;
   1207 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
   1208 		return error;
   1209 
   1210 	sc->lastupc = upc;
   1211 	return 0;
   1212 }
   1213 
   1214 int
   1215 mcd_toc_header(sc, th)
   1216 	struct mcd_softc *sc;
   1217 	struct ioc_toc_header *th;
   1218 {
   1219 
   1220 	if (sc->debug)
   1221 		printf("%s: mcd_toc_header: reading toc header\n",
   1222 		    sc->sc_dev.dv_xname);
   1223 
   1224 	th->len = msf2hsg(sc->volinfo.vol_msf, 0);
   1225 	th->starting_track = bcd2bin(sc->volinfo.trk_low);
   1226 	th->ending_track = bcd2bin(sc->volinfo.trk_high);
   1227 
   1228 	return 0;
   1229 }
   1230 
   1231 int
   1232 mcd_read_toc(sc)
   1233 	struct mcd_softc *sc;
   1234 {
   1235 	struct ioc_toc_header th;
   1236 	union mcd_qchninfo q;
   1237 	int error, trk, idx, retry;
   1238 
   1239 	/* Only read TOC if needed. */
   1240 	if ((sc->flags & MCDF_HAVETOC) != 0)
   1241 		return 0;
   1242 
   1243 	if ((error = mcd_toc_header(sc, &th)) != 0)
   1244 		return error;
   1245 
   1246 	if ((error = mcd_stop(sc)) != 0)
   1247 		return error;
   1248 
   1249 	if (sc->debug)
   1250 		printf("%s: read_toc: reading qchannel info\n",
   1251 		    sc->sc_dev.dv_xname);
   1252 
   1253 	for (trk = th.starting_track; trk <= th.ending_track; trk++)
   1254 		sc->toc[trk].toc.idx_no = 0x00;
   1255 	trk = th.ending_track - th.starting_track + 1;
   1256 	for (retry = 300; retry && trk > 0; retry--) {
   1257 		if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0)
   1258 			break;
   1259 		if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00)
   1260 			continue;
   1261 		idx = bcd2bin(q.toc.idx_no);
   1262 		if (idx < MCD_MAXTOCS &&
   1263 		    sc->toc[idx].toc.idx_no == 0x00) {
   1264 			sc->toc[idx] = q;
   1265 			trk--;
   1266 		}
   1267 	}
   1268 
   1269 	if (trk != 0)
   1270 		return EINVAL;
   1271 
   1272 	/* Add a fake last+1 for mcd_playtracks(). */
   1273 	idx = th.ending_track + 1;
   1274 	sc->toc[idx].toc.control = sc->toc[idx-1].toc.control;
   1275 	sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type;
   1276 	sc->toc[idx].toc.trk_no = 0x00;
   1277 	sc->toc[idx].toc.idx_no = 0xaa;
   1278 	sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0];
   1279 	sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1];
   1280 	sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2];
   1281 
   1282 	sc->flags |= MCDF_HAVETOC;
   1283 	return 0;
   1284 }
   1285 
   1286 int
   1287 mcd_toc_entries(sc, te)
   1288 	struct mcd_softc *sc;
   1289 	struct ioc_read_toc_entry *te;
   1290 {
   1291 	int len = te->data_len;
   1292 	struct ret_toc {
   1293 		struct ioc_toc_header header;
   1294 		struct cd_toc_entry entries[MCD_MAXTOCS];
   1295 	} data;
   1296 	u_char trk;
   1297 	daddr_t lba;
   1298 	int error, n;
   1299 
   1300 	if (len > sizeof(data.entries) ||
   1301 	    len < sizeof(struct cd_toc_entry))
   1302 		return EINVAL;
   1303 	if (te->address_format != CD_MSF_FORMAT &&
   1304 	    te->address_format != CD_LBA_FORMAT)
   1305 		return EINVAL;
   1306 
   1307 	/* Copy the TOC header. */
   1308 	if ((error = mcd_toc_header(sc, &data.header)) != 0)
   1309 		return error;
   1310 
   1311 	/* Verify starting track. */
   1312 	trk = te->starting_track;
   1313 	if (trk == 0x00)
   1314 		trk = data.header.starting_track;
   1315 	else if (trk == 0xaa)
   1316 		trk = data.header.ending_track + 1;
   1317 	else if (trk < data.header.starting_track ||
   1318 		 trk > data.header.ending_track + 1)
   1319 		return EINVAL;
   1320 
   1321 	/* Make sure we have a valid TOC. */
   1322 	if ((error = mcd_read_toc(sc)) != 0)
   1323 		return error;
   1324 
   1325 	/* Copy the TOC data. */
   1326 	for (n = 0; trk <= data.header.ending_track + 1; trk++) {
   1327 		if (sc->toc[trk].toc.idx_no == 0x00)
   1328 			continue;
   1329 		data.entries[n].control = sc->toc[trk].toc.control;
   1330 		data.entries[n].addr_type = sc->toc[trk].toc.addr_type;
   1331 		data.entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no);
   1332 		switch (te->address_format) {
   1333 		case CD_MSF_FORMAT:
   1334 			data.entries[n].addr[0] = 0;
   1335 			data.entries[n].addr[1] = bcd2bin(sc->toc[trk].toc.absolute_pos[0]);
   1336 			data.entries[n].addr[2] = bcd2bin(sc->toc[trk].toc.absolute_pos[1]);
   1337 			data.entries[n].addr[3] = bcd2bin(sc->toc[trk].toc.absolute_pos[2]);
   1338 			break;
   1339 		case CD_LBA_FORMAT:
   1340 			lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0);
   1341 			data.entries[n].addr[0] = lba >> 24;
   1342 			data.entries[n].addr[1] = lba >> 16;
   1343 			data.entries[n].addr[2] = lba >> 8;
   1344 			data.entries[n].addr[3] = lba;
   1345 			break;
   1346 		}
   1347 		n++;
   1348 	}
   1349 
   1350 	len = min(len, n * sizeof(struct cd_toc_entry));
   1351 
   1352 	/* Copy the data back. */
   1353 	return copyout(&data.entries[0], te->data, len);
   1354 }
   1355 
   1356 int
   1357 mcd_stop(sc)
   1358 	struct mcd_softc *sc;
   1359 {
   1360 	struct mcd_mbox mbx;
   1361 	int error;
   1362 
   1363 	if (sc->debug)
   1364 		printf("%s: mcd_stop: stopping play\n", sc->sc_dev.dv_xname);
   1365 
   1366 	mbx.cmd.opcode = MCD_CMDSTOPAUDIO;
   1367 	mbx.cmd.length = 0;
   1368 	mbx.res.length = 0;
   1369 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
   1370 		return error;
   1371 
   1372 	sc->audio_status = CD_AS_PLAY_COMPLETED;
   1373 	return 0;
   1374 }
   1375 
   1376 int
   1377 mcd_getqchan(sc, q, qchn)
   1378 	struct mcd_softc *sc;
   1379 	union mcd_qchninfo *q;
   1380 	int qchn;
   1381 {
   1382 	struct mcd_mbox mbx;
   1383 	int error;
   1384 
   1385 	if (qchn == CD_TRACK_INFO) {
   1386 		if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0)
   1387 			return error;
   1388 	} else {
   1389 		if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
   1390 			return error;
   1391 	}
   1392 	if (qchn == CD_MEDIA_CATALOG) {
   1393 		if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0)
   1394 			return error;
   1395 	} else {
   1396 		if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0)
   1397 			return error;
   1398 	}
   1399 
   1400 	mbx.cmd.opcode = MCD_CMDGETQCHN;
   1401 	mbx.cmd.length = 0;
   1402 	mbx.res.length = sizeof(mbx.res.data.qchninfo);
   1403 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
   1404 		return error;
   1405 
   1406 	*q = mbx.res.data.qchninfo;
   1407 	return 0;
   1408 }
   1409 
   1410 int
   1411 mcd_read_subchannel(sc, ch)
   1412 	struct mcd_softc *sc;
   1413 	struct ioc_read_subchannel *ch;
   1414 {
   1415 	int len = ch->data_len;
   1416 	union mcd_qchninfo q;
   1417 	struct cd_sub_channel_info data;
   1418 	daddr_t lba;
   1419 	int error;
   1420 
   1421 	if (sc->debug)
   1422 		printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname,
   1423 		    ch->address_format, ch->data_format);
   1424 
   1425 	if (len > sizeof(data) ||
   1426 	    len < sizeof(struct cd_sub_channel_header))
   1427 		return EINVAL;
   1428 	if (ch->address_format != CD_MSF_FORMAT &&
   1429 	    ch->address_format != CD_LBA_FORMAT)
   1430 		return EINVAL;
   1431 	if (ch->data_format != CD_CURRENT_POSITION &&
   1432 	    ch->data_format != CD_MEDIA_CATALOG)
   1433 		return EINVAL;
   1434 
   1435 	if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0)
   1436 		return error;
   1437 
   1438 	data.header.audio_status = sc->audio_status;
   1439 	data.what.media_catalog.data_format = ch->data_format;
   1440 
   1441 	switch (ch->data_format) {
   1442 	case CD_MEDIA_CATALOG:
   1443 		data.what.media_catalog.mc_valid = 1;
   1444 #if 0
   1445 		data.what.media_catalog.mc_number =
   1446 #endif
   1447 		break;
   1448 
   1449 	case CD_CURRENT_POSITION:
   1450 		data.what.position.track_number = bcd2bin(q.current.trk_no);
   1451 		data.what.position.index_number = bcd2bin(q.current.idx_no);
   1452 		switch (ch->address_format) {
   1453 		case CD_MSF_FORMAT:
   1454 			data.what.position.reladdr[0] = 0;
   1455 			data.what.position.reladdr[1] = bcd2bin(q.current.relative_pos[0]);
   1456 			data.what.position.reladdr[2] = bcd2bin(q.current.relative_pos[1]);
   1457 			data.what.position.reladdr[3] = bcd2bin(q.current.relative_pos[2]);
   1458 			data.what.position.absaddr[0] = 0;
   1459 			data.what.position.absaddr[1] = bcd2bin(q.current.absolute_pos[0]);
   1460 			data.what.position.absaddr[2] = bcd2bin(q.current.absolute_pos[1]);
   1461 			data.what.position.absaddr[3] = bcd2bin(q.current.absolute_pos[2]);
   1462 			break;
   1463 		case CD_LBA_FORMAT:
   1464 			lba = msf2hsg(q.current.relative_pos, 1);
   1465 			/*
   1466 			 * Pre-gap has index number of 0, and decreasing MSF
   1467 			 * address.  Must be converted to negative LBA, per
   1468 			 * SCSI spec.
   1469 			 */
   1470 			if (data.what.position.index_number == 0x00)
   1471 				lba = -lba;
   1472 			data.what.position.reladdr[0] = lba >> 24;
   1473 			data.what.position.reladdr[1] = lba >> 16;
   1474 			data.what.position.reladdr[2] = lba >> 8;
   1475 			data.what.position.reladdr[3] = lba;
   1476 			lba = msf2hsg(q.current.absolute_pos, 0);
   1477 			data.what.position.absaddr[0] = lba >> 24;
   1478 			data.what.position.absaddr[1] = lba >> 16;
   1479 			data.what.position.absaddr[2] = lba >> 8;
   1480 			data.what.position.absaddr[3] = lba;
   1481 			break;
   1482 		}
   1483 		break;
   1484 	}
   1485 
   1486 	return copyout(&data, ch->data, len);
   1487 }
   1488 
   1489 int
   1490 mcd_playtracks(sc, p)
   1491 	struct mcd_softc *sc;
   1492 	struct ioc_play_track *p;
   1493 {
   1494 	struct mcd_mbox mbx;
   1495 	int a = p->start_track;
   1496 	int z = p->end_track;
   1497 	int error;
   1498 
   1499 	if ((error = mcd_read_toc(sc)) != 0)
   1500 		return error;
   1501 
   1502 	if (sc->debug)
   1503 		printf("%s: playtracks: from %d:%d to %d:%d\n",
   1504 		    sc->sc_dev.dv_xname,
   1505 		    a, p->start_index, z, p->end_index);
   1506 
   1507 	if (a < bcd2bin(sc->volinfo.trk_low) ||
   1508 	    a > bcd2bin(sc->volinfo.trk_high) ||
   1509 	    a > z ||
   1510 	    z < bcd2bin(sc->volinfo.trk_low) ||
   1511 	    z > bcd2bin(sc->volinfo.trk_high))
   1512 		return EINVAL;
   1513 
   1514 	if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
   1515 		return error;
   1516 
   1517 	mbx.cmd.opcode = MCD_CMDREAD2;
   1518 	mbx.cmd.length = sizeof(mbx.cmd.data.play);
   1519 	mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0];
   1520 	mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1];
   1521 	mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2];
   1522 	mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0];
   1523 	mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1];
   1524 	mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2];
   1525 	sc->lastpb = mbx.cmd;
   1526 	mbx.res.length = 0;
   1527 	return mcd_send(sc, &mbx, 1);
   1528 }
   1529 
   1530 int
   1531 mcd_playmsf(sc, p)
   1532 	struct mcd_softc *sc;
   1533 	struct ioc_play_msf *p;
   1534 {
   1535 	struct mcd_mbox mbx;
   1536 	int error;
   1537 
   1538 	if ((error = mcd_read_toc(sc)) != 0)
   1539 		return error;
   1540 
   1541 	if (sc->debug)
   1542 		printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n",
   1543 		    sc->sc_dev.dv_xname,
   1544 		    p->start_m, p->start_s, p->start_f,
   1545 		    p->end_m, p->end_s, p->end_f);
   1546 
   1547 	if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
   1548 	    (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f))
   1549 		return EINVAL;
   1550 
   1551 	if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
   1552 		return error;
   1553 
   1554 	mbx.cmd.opcode = MCD_CMDREAD2;
   1555 	mbx.cmd.length = sizeof(mbx.cmd.data.play);
   1556 	mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m);
   1557 	mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s);
   1558 	mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f);
   1559 	mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m);
   1560 	mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s);
   1561 	mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f);
   1562 	sc->lastpb = mbx.cmd;
   1563 	mbx.res.length = 0;
   1564 	return mcd_send(sc, &mbx, 1);
   1565 }
   1566 
   1567 int
   1568 mcd_playblocks(sc, p)
   1569 	struct mcd_softc *sc;
   1570 	struct ioc_play_blocks *p;
   1571 {
   1572 	struct mcd_mbox mbx;
   1573 	int error;
   1574 
   1575 	if ((error = mcd_read_toc(sc)) != 0)
   1576 		return error;
   1577 
   1578 	if (sc->debug)
   1579 		printf("%s: playblocks: blkno %d length %d\n",
   1580 		    sc->sc_dev.dv_xname, p->blk, p->len);
   1581 
   1582 	if (p->blk > sc->disksize || p->len > sc->disksize ||
   1583 	    (p->blk + p->len) > sc->disksize)
   1584 		return 0;
   1585 
   1586 	if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
   1587 		return error;
   1588 
   1589 	mbx.cmd.opcode = MCD_CMDREAD2;
   1590 	mbx.cmd.length = sizeof(mbx.cmd.data.play);
   1591 	hsg2msf(p->blk, mbx.cmd.data.play.start_msf);
   1592 	hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf);
   1593 	sc->lastpb = mbx.cmd;
   1594 	mbx.res.length = 0;
   1595 	return mcd_send(sc, &mbx, 1);
   1596 }
   1597 
   1598 int
   1599 mcd_pause(sc)
   1600 	struct mcd_softc *sc;
   1601 {
   1602 	union mcd_qchninfo q;
   1603 	int error;
   1604 
   1605 	/* Verify current status. */
   1606 	if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS)	{
   1607 		printf("%s: pause: attempted when not playing\n",
   1608 		    sc->sc_dev.dv_xname);
   1609 		return EINVAL;
   1610 	}
   1611 
   1612 	/* Get the current position. */
   1613 	if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0)
   1614 		return error;
   1615 
   1616 	/* Copy it into lastpb. */
   1617 	sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0];
   1618 	sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1];
   1619 	sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2];
   1620 
   1621 	/* Stop playing. */
   1622 	if ((error = mcd_stop(sc)) != 0)
   1623 		return error;
   1624 
   1625 	/* Set the proper status and exit. */
   1626 	sc->audio_status = CD_AS_PLAY_PAUSED;
   1627 	return 0;
   1628 }
   1629 
   1630 int
   1631 mcd_resume(sc)
   1632 	struct mcd_softc *sc;
   1633 {
   1634 	struct mcd_mbox mbx;
   1635 	int error;
   1636 
   1637 	if (sc->audio_status != CD_AS_PLAY_PAUSED)
   1638 		return EINVAL;
   1639 
   1640 	if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
   1641 		return error;
   1642 
   1643 	mbx.cmd = sc->lastpb;
   1644 	mbx.res.length = 0;
   1645 	return mcd_send(sc, &mbx, 1);
   1646 }
   1647 
   1648 int
   1649 mcd_eject(sc)
   1650 	struct mcd_softc *sc;
   1651 {
   1652 	struct mcd_mbox mbx;
   1653 
   1654 	mbx.cmd.opcode = MCD_CMDEJECTDISK;
   1655 	mbx.cmd.length = 0;
   1656 	mbx.res.length = 0;
   1657 	return mcd_send(sc, &mbx, 0);
   1658 }
   1659 
   1660 int
   1661 mcd_setlock(sc, mode)
   1662 	struct mcd_softc *sc;
   1663 	int mode;
   1664 {
   1665 	struct mcd_mbox mbx;
   1666 
   1667 	mbx.cmd.opcode = MCD_CMDSETLOCK;
   1668 	mbx.cmd.length = sizeof(mbx.cmd.data.lockmode);
   1669 	mbx.cmd.data.lockmode.mode = mode;
   1670 	mbx.res.length = 0;
   1671 	return mcd_send(sc, &mbx, 1);
   1672 }
   1673