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