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