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