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