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