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