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