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