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