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