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