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