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