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