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