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