Home | History | Annotate | Line # | Download | only in isa
mcd.c revision 1.39
      1 /*	$NetBSD: mcd.c,v 1.39 1995/06/26 05:37:11 cgd 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 <dev/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 	void *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 = isa_intr_establish(ia->ia_irq, ISA_IST_EDGE, ISA_IPL_BIO,
    225 	    mcdintr, sc);
    226 }
    227 
    228 /*
    229  * Wait interruptibly for an exclusive lock.
    230  *
    231  * XXX
    232  * Several drivers do this; it should be abstracted and made MP-safe.
    233  */
    234 int
    235 mcdlock(sc)
    236 	struct mcd_softc *sc;
    237 {
    238 	int error;
    239 
    240 	while ((sc->flags & MCDF_LOCKED) != 0) {
    241 		sc->flags |= MCDF_WANTED;
    242 		if ((error = tsleep(sc, PRIBIO | PCATCH, "mcdlck", 0)) != 0)
    243 			return error;
    244 	}
    245 	sc->flags |= MCDF_LOCKED;
    246 	return 0;
    247 }
    248 
    249 /*
    250  * Unlock and wake up any waiters.
    251  */
    252 void
    253 mcdunlock(sc)
    254 	struct mcd_softc *sc;
    255 {
    256 
    257 	sc->flags &= ~MCDF_LOCKED;
    258 	if ((sc->flags & MCDF_WANTED) != 0) {
    259 		sc->flags &= ~MCDF_WANTED;
    260 		wakeup(sc);
    261 	}
    262 }
    263 
    264 int
    265 mcdopen(dev, flag, fmt, p)
    266 	dev_t dev;
    267 	int flag, fmt;
    268 	struct proc *p;
    269 {
    270 	int error;
    271 	int unit, part;
    272 	struct mcd_softc *sc;
    273 
    274 	unit = MCDUNIT(dev);
    275 	if (unit >= mcdcd.cd_ndevs)
    276 		return ENXIO;
    277 	sc = mcdcd.cd_devs[unit];
    278 	if (!sc)
    279 		return ENXIO;
    280 
    281 	if (error = mcdlock(sc))
    282 		return error;
    283 
    284 	if (sc->sc_dk.dk_openmask != 0) {
    285 		/*
    286 		 * If any partition is open, but the disk has been invalidated,
    287 		 * disallow further opens.
    288 		 */
    289 		if ((sc->flags & MCDF_LOADED) == 0) {
    290 			error = EIO;
    291 			goto bad3;
    292 		}
    293 	} else {
    294 		/*
    295 		 * Lock the drawer.  This will also notice any pending disk
    296 		 * change or door open indicator and clear the MCDF_LOADED bit
    297 		 * if necessary.
    298 		 */
    299 		(void) mcd_setlock(sc, MCD_LK_LOCK);
    300 
    301 		if ((sc->flags & MCDF_LOADED) == 0) {
    302 			/* Partially reset the state. */
    303 			sc->lastmode = MCD_MD_UNKNOWN;
    304 			sc->lastupc = MCD_UPC_UNKNOWN;
    305 
    306 			sc->flags |= MCDF_LOADED;
    307 
    308 			/* Set the mode, causing the disk to spin up. */
    309 			if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
    310 				goto bad2;
    311 
    312 			/* Load the physical device parameters. */
    313 			if (mcd_get_parms(sc) != 0) {
    314 				error = ENXIO;
    315 				goto bad2;
    316 			}
    317 
    318 			/* Read the table of contents. */
    319 			if ((error = mcd_read_toc(sc)) != 0)
    320 				goto bad2;
    321 
    322 			/* Fabricate a disk label. */
    323 			mcdgetdisklabel(sc);
    324 		}
    325 	}
    326 
    327 	MCD_TRACE("open: partition=%d disksize=%d blksize=%d\n", part,
    328 	    sc->disksize, sc->blksize, 0);
    329 
    330 	part = MCDPART(dev);
    331 
    332 	/* Check that the partition exists. */
    333 	if (part != RAW_PART &&
    334 	    (part >= sc->sc_dk.dk_label.d_npartitions ||
    335 	     sc->sc_dk.dk_label.d_partitions[part].p_fstype == FS_UNUSED)) {
    336 		error = ENXIO;
    337 		goto bad;
    338 	}
    339 
    340 	/* Insure only one open at a time. */
    341 	switch (fmt) {
    342 	case S_IFCHR:
    343 		sc->sc_dk.dk_copenmask |= (1 << part);
    344 		break;
    345 	case S_IFBLK:
    346 		sc->sc_dk.dk_bopenmask |= (1 << part);
    347 		break;
    348 	}
    349 	sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    350 
    351 	mcdunlock(sc);
    352 	return 0;
    353 
    354 bad2:
    355 	sc->flags &= ~MCDF_LOADED;
    356 
    357 bad:
    358 	if (sc->sc_dk.dk_openmask == 0) {
    359 #if 0
    360 		(void) mcd_setmode(sc, MCD_MD_SLEEP);
    361 #endif
    362 		(void) mcd_setlock(sc, MCD_LK_UNLOCK);
    363 	}
    364 
    365 bad3:
    366 	mcdunlock(sc);
    367 	return error;
    368 }
    369 
    370 int
    371 mcdclose(dev, flag, fmt)
    372 	dev_t dev;
    373 	int flag, fmt;
    374 {
    375 	struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(dev)];
    376 	int part = MCDPART(dev);
    377 	int error;
    378 
    379 	MCD_TRACE("close: partition=%d\n", part, 0, 0, 0);
    380 
    381 	if (error = mcdlock(sc))
    382 		return error;
    383 
    384 	switch (fmt) {
    385 	case S_IFCHR:
    386 		sc->sc_dk.dk_copenmask &= ~(1 << part);
    387 		break;
    388 	case S_IFBLK:
    389 		sc->sc_dk.dk_bopenmask &= ~(1 << part);
    390 		break;
    391 	}
    392 	sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    393 
    394 	if (sc->sc_dk.dk_openmask == 0) {
    395 		/* XXXX Must wait for I/O to complete! */
    396 
    397 #if 0
    398 		(void) mcd_setmode(sc, MCD_MD_SLEEP);
    399 #endif
    400 		(void) mcd_setlock(sc, MCD_LK_UNLOCK);
    401 	}
    402 
    403 	mcdunlock(sc);
    404 	return 0;
    405 }
    406 
    407 void
    408 mcdstrategy(bp)
    409 	struct buf *bp;
    410 {
    411 	struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(bp->b_dev)];
    412 	int s;
    413 
    414 	/* Test validity. */
    415 	MCD_TRACE("strategy: buf=0x%lx blkno=%ld bcount=%ld\n", bp,
    416 	    bp->b_blkno, bp->b_bcount, 0);
    417 	if (bp->b_blkno < 0 ||
    418 	    (bp->b_bcount % sc->blksize) != 0) {
    419 		printf("%s: strategy: blkno = %d bcount = %d\n",
    420 		    sc->sc_dev.dv_xname, bp->b_blkno, bp->b_bcount);
    421 		bp->b_error = EINVAL;
    422 		goto bad;
    423 	}
    424 
    425 	/* If device invalidated (e.g. media change, door open), error. */
    426 	if ((sc->flags & MCDF_LOADED) == 0) {
    427 		MCD_TRACE("strategy: drive not valid\n", 0, 0, 0, 0);
    428 		bp->b_error = EIO;
    429 		goto bad;
    430 	}
    431 
    432 	/* No data to read. */
    433 	if (bp->b_bcount == 0)
    434 		goto done;
    435 
    436 	/*
    437 	 * Do bounds checking, adjust transfer. if error, process.
    438 	 * If end of partition, just return.
    439 	 */
    440 	if (MCDPART(bp->b_dev) != RAW_PART &&
    441 	    bounds_check_with_label(bp, &sc->sc_dk.dk_label,
    442 	    (sc->flags & (MCDF_WLABEL|MCDF_LABELLING)) != 0) <= 0)
    443 		goto done;
    444 
    445 	/* Queue it. */
    446 	s = splbio();
    447 	disksort(&sc->buf_queue, bp);
    448 	splx(s);
    449 	if (!sc->buf_queue.b_active)
    450 		mcdstart(sc);
    451 	return;
    452 
    453 bad:
    454 	bp->b_flags |= B_ERROR;
    455 done:
    456 	bp->b_resid = bp->b_bcount;
    457 	biodone(bp);
    458 }
    459 
    460 void
    461 mcdstart(sc)
    462 	struct mcd_softc *sc;
    463 {
    464 	struct buf *bp, *dp = &sc->buf_queue;
    465 	int s;
    466 
    467 loop:
    468 	s = splbio();
    469 
    470 	bp = dp->b_actf;
    471 	if (bp == NULL) {
    472 		/* Nothing to do. */
    473 		dp->b_active = 0;
    474 		splx(s);
    475 		return;
    476 	}
    477 
    478 	/* Block found to process; dequeue. */
    479 	MCD_TRACE("start: found block bp=0x%x\n", bp, 0, 0, 0);
    480 	dp->b_actf = bp->b_actf;
    481 	splx(s);
    482 
    483 	/* Changed media? */
    484 	if ((sc->flags & MCDF_LOADED) == 0) {
    485 		MCD_TRACE("start: drive not valid\n", 0, 0, 0, 0);
    486 		bp->b_error = EIO;
    487 		bp->b_flags |= B_ERROR;
    488 		biodone(bp);
    489 		goto loop;
    490 	}
    491 
    492 	dp->b_active = 1;
    493 
    494 	sc->mbx.retry = MCD_RDRETRIES;
    495 	sc->mbx.bp = bp;
    496 	sc->mbx.blkno = bp->b_blkno / (sc->blksize / DEV_BSIZE);
    497 	if (MCDPART(bp->b_dev) != RAW_PART) {
    498 		struct partition *p;
    499 		p = &sc->sc_dk.dk_label.d_partitions[MCDPART(bp->b_dev)];
    500 		sc->mbx.blkno += p->p_offset;
    501 	}
    502 	sc->mbx.nblk = bp->b_bcount / sc->blksize;
    503 	sc->mbx.sz = sc->blksize;
    504 	sc->mbx.skip = 0;
    505 	sc->mbx.state = MCD_S_BEGIN;
    506 	sc->mbx.mode = MCD_MD_COOKED;
    507 
    508 	s = splbio();
    509 	(void) mcdintr(sc);
    510 	splx(s);
    511 }
    512 
    513 int
    514 mcdioctl(dev, cmd, addr, flag, p)
    515 	dev_t dev;
    516 	u_long cmd;
    517 	caddr_t addr;
    518 	int flag;
    519 	struct proc *p;
    520 {
    521 	struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(dev)];
    522 	int error;
    523 
    524 	MCD_TRACE("ioctl: cmd=0x%x\n", cmd, 0, 0, 0);
    525 
    526 	if ((sc->flags & MCDF_LOADED) == 0)
    527 		return EIO;
    528 
    529 	switch (cmd) {
    530 	case DIOCGDINFO:
    531 		*(struct disklabel *)addr = sc->sc_dk.dk_label;
    532 		return 0;
    533 
    534 	case DIOCGPART:
    535 		((struct partinfo *)addr)->disklab = &sc->sc_dk.dk_label;
    536 		((struct partinfo *)addr)->part =
    537 		    &sc->sc_dk.dk_label.d_partitions[MCDPART(dev)];
    538 		return 0;
    539 
    540 	case DIOCWDINFO:
    541 	case DIOCSDINFO:
    542 		if ((flag & FWRITE) == 0)
    543 			return EBADF;
    544 
    545 		if (error = mcdlock(sc))
    546 			return error;
    547 		sc->flags |= MCDF_LABELLING;
    548 
    549 		error = setdisklabel(&sc->sc_dk.dk_label,
    550 		    (struct disklabel *)addr, /*sc->sc_dk.dk_openmask : */0,
    551 		    &sc->sc_dk.dk_cpulabel);
    552 		if (error == 0) {
    553 		}
    554 
    555 		sc->flags &= ~MCDF_LABELLING;
    556 		mcdunlock(sc);
    557 		return error;
    558 
    559 	case DIOCWLABEL:
    560 		return EBADF;
    561 
    562 	case CDIOCPLAYTRACKS:
    563 		return mcd_playtracks(sc, (struct ioc_play_track *)addr);
    564 	case CDIOCPLAYMSF:
    565 		return mcd_playmsf(sc, (struct ioc_play_msf *)addr);
    566 	case CDIOCPLAYBLOCKS:
    567 		return mcd_playblocks(sc, (struct ioc_play_blocks *)addr);
    568 	case CDIOCREADSUBCHANNEL:
    569 		return mcd_read_subchannel(sc, (struct ioc_read_subchannel *)addr);
    570 	case CDIOREADTOCHEADER:
    571 		return mcd_toc_header(sc, (struct ioc_toc_header *)addr);
    572 	case CDIOREADTOCENTRYS:
    573 		return mcd_toc_entries(sc, (struct ioc_read_toc_entry *)addr);
    574 	case CDIOCSETPATCH:
    575 	case CDIOCGETVOL:
    576 	case CDIOCSETVOL:
    577 	case CDIOCSETMONO:
    578 	case CDIOCSETSTEREO:
    579 	case CDIOCSETMUTE:
    580 	case CDIOCSETLEFT:
    581 	case CDIOCSETRIGHT:
    582 		return EINVAL;
    583 	case CDIOCRESUME:
    584 		return mcd_resume(sc);
    585 	case CDIOCPAUSE:
    586 		return mcd_pause(sc);
    587 	case CDIOCSTART:
    588 		return EINVAL;
    589 	case CDIOCSTOP:
    590 		return mcd_stop(sc);
    591 	case CDIOCEJECT:
    592 		return mcd_eject(sc);
    593 	case CDIOCALLOW:
    594 		return mcd_setlock(sc, MCD_LK_UNLOCK);
    595 	case CDIOCPREVENT:
    596 		return mcd_setlock(sc, MCD_LK_LOCK);
    597 	case CDIOCSETDEBUG:
    598 		sc->debug = 1;
    599 		return 0;
    600 	case CDIOCCLRDEBUG:
    601 		sc->debug = 0;
    602 		return 0;
    603 	case CDIOCRESET:
    604 		return mcd_hard_reset(sc);
    605 
    606 	default:
    607 		return ENOTTY;
    608 	}
    609 
    610 #ifdef DIAGNOSTIC
    611 	panic("mcdioctl: impossible");
    612 #endif
    613 }
    614 
    615 /*
    616  * This could have been taken from scsi/cd.c, but it is not clear
    617  * whether the scsi cd driver is linked in.
    618  */
    619 void
    620 mcdgetdisklabel(sc)
    621 	struct mcd_softc *sc;
    622 {
    623 
    624 	bzero(&sc->sc_dk.dk_label, sizeof(struct disklabel));
    625 	bzero(&sc->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
    626 
    627 	sc->sc_dk.dk_label.d_secsize = sc->blksize;
    628 	sc->sc_dk.dk_label.d_ntracks = 1;
    629 	sc->sc_dk.dk_label.d_nsectors = 100;
    630 	sc->sc_dk.dk_label.d_ncylinders = (sc->disksize / 100) + 1;
    631 	sc->sc_dk.dk_label.d_secpercyl =
    632 	    sc->sc_dk.dk_label.d_ntracks * sc->sc_dk.dk_label.d_nsectors;
    633 
    634 	strncpy(sc->sc_dk.dk_label.d_typename, "Mitsumi CD-ROM", 16);
    635 	sc->sc_dk.dk_label.d_type = 0;	/* XXX */
    636 	strncpy(sc->sc_dk.dk_label.d_packname, "fictitious", 16);
    637 	sc->sc_dk.dk_label.d_secperunit = sc->disksize;
    638 	sc->sc_dk.dk_label.d_rpm = 300;
    639 	sc->sc_dk.dk_label.d_interleave = 1;
    640 	sc->sc_dk.dk_label.d_flags = D_REMOVABLE;
    641 
    642 	sc->sc_dk.dk_label.d_partitions[0].p_offset = 0;
    643 	sc->sc_dk.dk_label.d_partitions[0].p_size =
    644 	    sc->sc_dk.dk_label.d_secperunit *
    645 	    (sc->sc_dk.dk_label.d_secsize / DEV_BSIZE);
    646 	sc->sc_dk.dk_label.d_partitions[0].p_fstype = FS_ISO9660;
    647 	sc->sc_dk.dk_label.d_npartitions = 1;
    648 
    649 	sc->sc_dk.dk_label.d_magic = DISKMAGIC;
    650 	sc->sc_dk.dk_label.d_magic2 = DISKMAGIC;
    651 	sc->sc_dk.dk_label.d_checksum = dkcksum(&sc->sc_dk.dk_label);
    652 }
    653 
    654 int
    655 mcd_get_parms(sc)
    656 	struct mcd_softc *sc;
    657 {
    658 	struct mcd_mbox mbx;
    659 	daddr_t size;
    660 	int error;
    661 
    662 	/* Send volume info command. */
    663 	mbx.cmd.opcode = MCD_CMDGETVOLINFO;
    664 	mbx.cmd.length = 0;
    665 	mbx.res.length = sizeof(mbx.res.data.volinfo);
    666 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
    667 		return error;
    668 
    669 	if (mbx.res.data.volinfo.trk_low == 0x00 &&
    670 	    mbx.res.data.volinfo.trk_high == 0x00)
    671 		return EINVAL;
    672 
    673 	/* Volinfo is OK. */
    674 	sc->volinfo = mbx.res.data.volinfo;
    675 	sc->blksize = MCD_BLKSIZE_COOKED;
    676 	size = msf2hsg(sc->volinfo.vol_msf, 0);
    677 	sc->disksize = size * (MCD_BLKSIZE_COOKED / DEV_BSIZE);
    678 	return 0;
    679 }
    680 
    681 int
    682 mcdsize(dev)
    683 	dev_t dev;
    684 {
    685 
    686 	/* CD-ROMs are read-only. */
    687 	return -1;
    688 }
    689 
    690 int
    691 mcddump(dev, blkno, va, size)
    692 	dev_t dev;
    693 	daddr_t blkno;
    694 	caddr_t va;
    695 	size_t size;
    696 {
    697 
    698 	/* Not implemented. */
    699 	return ENXIO;
    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 #ifdef MCDDEBUG
    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