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