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