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