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