Home | History | Annotate | Line # | Download | only in isa
mcd.c revision 1.14
      1 /*
      2  * Copyright (c) 1993, 1994 Charles Hannum.
      3  * Copyright 1993 by Holger Veit (data part)
      4  * Copyright 1993 by Brian Moore (audio part)
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This software was developed by Holger Veit and Brian Moore
     18  *      for use with "386BSD" and similar operating systems.
     19  *    "Similar operating systems" includes mainly non-profit oriented
     20  *    systems for research and education, including but not restricted to
     21  *    "NetBSD", "FreeBSD", "Mach" (by CMU).
     22  * 4. Neither the name of the developer(s) nor the name "386BSD"
     23  *    may be used to endorse or promote products derived from this
     24  *    software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY
     27  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER(S) BE
     30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     31  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
     32  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     33  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     36  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     37  *
     38  *	$Id: mcd.c,v 1.14 1994/05/05 08:26:13 mycroft Exp $
     39  */
     40 
     41 /*static char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";*/
     42 
     43 #include <sys/types.h>
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/proc.h>
     48 #include <sys/conf.h>
     49 #include <sys/file.h>
     50 #include <sys/buf.h>
     51 #include <sys/stat.h>
     52 #include <sys/uio.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/cdio.h>
     55 #include <sys/errno.h>
     56 #include <sys/dkbad.h>
     57 #include <sys/disklabel.h>
     58 #include <sys/device.h>
     59 
     60 #include <machine/cpu.h>
     61 #include <machine/pio.h>
     62 
     63 #include <i386/isa/isavar.h>
     64 #include <i386/isa/mcdreg.h>
     65 
     66 #ifndef MCDDEBUG
     67 #define MCD_TRACE(fmt,a,b,c,d)
     68 #else
     69 #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);}}
     70 #endif
     71 
     72 #define MCDPART(dev)	(((minor(dev)) & 0x07)     )
     73 #define MCDUNIT(dev)	(((minor(dev)) & 0x78) >> 3)
     74 #define MCDPHYS(dev)	(((minor(dev)) & 0x80) >> 7)
     75 #define RAW_PART	3
     76 
     77 /* flags */
     78 #define MCDOPEN		0x0001	/* device opened */
     79 #define MCDVALID	0x0002	/* parameters loaded */
     80 #define MCDWAIT		0x0004	/* waiting for something */
     81 #define MCDLABEL	0x0008	/* label is read */
     82 #define	MCDREADRAW	0x0010	/* read raw mode (2352 bytes) */
     83 #define	MCDVOLINFO	0x0020	/* already read volinfo */
     84 #define	MCDTOC		0x0040	/* already read toc */
     85 #define	MCDMBXBSY	0x0080	/* local mbx is busy */
     86 
     87 /* status */
     88 #define	MCDAUDIOBSY	MCD_ST_AUDIOBSY		/* playing audio */
     89 #define MCDDSKCHNG	MCD_ST_DSKCHNG		/* sensed change of disk */
     90 #define MCDDSKIN	MCD_ST_DSKIN		/* sensed disk in drive */
     91 #define MCDDOOROPEN	MCD_ST_DOOROPEN		/* sensed door open */
     92 
     93 /* toc */
     94 #define MCD_MAXTOCS	104	/* from the Linux driver */
     95 #define MCD_LASTPLUS1	170	/* special toc entry */
     96 
     97 struct mcd_mbx {
     98 	short		unit;
     99 	short		retry;
    100 	short		nblk;
    101 	int		sz;
    102 	u_long		skip;
    103 	struct buf	*bp;
    104 	int		p_offset;
    105 	short		count;
    106 	short		state;
    107 };
    108 
    109 struct mcd_softc {
    110 	struct	device sc_dev;
    111 	struct	intrhand sc_ih;
    112 
    113 	u_short	iobase;
    114 	short	config;
    115 	short	flags;
    116 	short	status;
    117 	int	blksize;
    118 	u_long	disksize;
    119 	struct	disklabel dlabel;
    120 	int	partflags[MAXPARTITIONS];
    121 	int	openflags;
    122 	struct	mcd_volinfo volinfo;
    123 	struct	mcd_qchninfo toc[MCD_MAXTOCS];
    124 	short	audio_status;
    125 	struct	mcd_read2 lastpb;
    126 	short	debug;
    127 	struct	buf head;	/* head of buf queue */
    128 	struct	mcd_mbx mbx;
    129 };
    130 
    131 /* prototypes */
    132 int mcdopen __P((dev_t));
    133 int mcdclose __P((dev_t));
    134 int mcd_start __P((struct mcd_softc *));
    135 int mcdioctl __P((dev_t, int, caddr_t, int, struct proc *));
    136 int mcd_getdisklabel __P((struct mcd_softc *));
    137 int mcdsize __P((dev_t));
    138 void mcd_configure __P((struct mcd_softc *));
    139 int mcd_waitrdy __P((u_short, int));
    140 int mcd_getreply __P((struct mcd_softc *, int));
    141 int mcd_getstat __P((struct mcd_softc *, int));
    142 void mcd_setflags __P((struct mcd_softc *));
    143 int mcd_get __P((struct mcd_softc *, char *, int));
    144 int mcd_send __P((struct mcd_softc *, int, int));
    145 int bcd2bin __P((bcd_t));
    146 bcd_t bin2bcd __P((int));
    147 void hsg2msf __P((int, bcd_t *));
    148 int msf2hsg __P((bcd_t *));
    149 int mcd_volinfo __P((struct mcd_softc *));
    150 int mcdintr __P((struct mcd_softc *));
    151 int mcd_setmode __P((struct mcd_softc *, int));
    152 void mcd_doread __P((void *));
    153 int mcd_toc_header __P((struct mcd_softc *, struct ioc_toc_header *));
    154 int mcd_read_toc __P((struct mcd_softc *));
    155 int mcd_toc_entry __P((struct mcd_softc *, struct ioc_read_toc_entry *));
    156 int mcd_stop __P((struct mcd_softc *));
    157 int mcd_getqchan __P((struct mcd_softc *, struct mcd_qchninfo *));
    158 int mcd_subchan __P((struct mcd_softc *, struct ioc_read_subchannel *));
    159 int mcd_playtracks __P((struct mcd_softc *, struct ioc_play_track *));
    160 int mcd_play __P((struct mcd_softc *, struct mcd_read2 *));
    161 int mcd_pause __P((struct mcd_softc *));
    162 int mcd_resume __P((struct mcd_softc *));
    163 
    164 int mcdprobe();
    165 void mcdattach();
    166 
    167 struct cfdriver mcdcd = {
    168 	NULL, "mcd", mcdprobe, mcdattach, DV_DISK, sizeof(struct mcd_softc)
    169 };
    170 
    171 #define mcd_put(port,byte)	outb(port,byte)
    172 
    173 #define MCD_RETRIES	5
    174 #define MCD_RDRETRIES	8
    175 
    176 #define MCDBLK	2048	/* for cooked mode */
    177 #define MCDRBLK	2352	/* for raw mode */
    178 
    179 /* several delays */
    180 #define RDELAY_WAITSTAT	300
    181 #define RDELAY_WAITMODE	300
    182 #define RDELAY_WAITREAD	800
    183 
    184 #define DELAY_STATUS	10000l		/* 10000 * 1us */
    185 #define DELAY_GETREPLY	200000l		/* 200000 * 2us */
    186 #define DELAY_SEEKREAD	20000l		/* 20000 * 1us */
    187 
    188 /* reader state machine */
    189 #define MCD_S_BEGIN	0
    190 #define MCD_S_WAITSTAT	1
    191 #define MCD_S_WAITMODE	2
    192 #define MCD_S_WAITREAD	3
    193 
    194 void
    195 mcdattach(parent, self, aux)
    196 	struct device *parent, *self;
    197 	void *aux;
    198 {
    199 	struct mcd_softc *sc = (void *)self;
    200 	struct isa_attach_args *ia = aux;
    201 
    202 #ifdef notyet
    203 	/* Wire controller for interrupts and DMA. */
    204 	mcd_configure(sc);
    205 #endif
    206 
    207 	printf("\n");
    208 
    209 	sc->flags = 0;
    210 
    211 	sc->sc_ih.ih_fun = mcdintr;
    212 	sc->sc_ih.ih_arg = sc;
    213 	sc->sc_ih.ih_level = IPL_BIO;
    214 	intr_establish(ia->ia_irq, &sc->sc_ih);
    215 }
    216 
    217 int
    218 mcdopen(dev)
    219 	dev_t dev;
    220 {
    221 	int unit, part, phys;
    222 	struct mcd_softc *sc;
    223 
    224 	unit = MCDUNIT(dev);
    225 	if (unit >= mcdcd.cd_ndevs)
    226 		return ENXIO;
    227 	sc = mcdcd.cd_devs[unit];
    228 	if (!sc)
    229 		return ENXIO;
    230 
    231 	part = MCDPART(dev);
    232 	phys = MCDPHYS(dev);
    233 
    234 	/* Invalidated in the meantime?  Mark all open part's invalid. */
    235 	if (!(sc->flags & MCDVALID) && sc->openflags)
    236 		return ENXIO;
    237 
    238 	if (mcd_getstat(sc, 1) < 0)
    239 		return ENXIO;
    240 
    241 	/* XXX Get a default disklabel. */
    242 	mcd_getdisklabel(sc);
    243 
    244 	if (mcdsize(dev) < 0) {
    245 		printf("%s: failed to get disk size\n", sc->sc_dev.dv_xname);
    246 		return ENXIO;
    247 	} else
    248 		sc->flags |= MCDVALID;
    249 
    250 	MCD_TRACE("open: partition=%d disksize=%d blksize=%d\n", part,
    251 	    sc->disksize, sc->blksize, 0);
    252 
    253 	if (part != RAW_PART &&
    254 	    (part >= sc->dlabel.d_npartitions ||
    255 	    sc->dlabel.d_partitions[part].p_fstype == FS_UNUSED))
    256 		return ENXIO;
    257 
    258 	sc->partflags[part] |= MCDOPEN;
    259 	sc->openflags |= (1 << part);
    260 	if (part == RAW_PART && phys != 0)
    261 		sc->partflags[part] |= MCDREADRAW;
    262 	return 0;
    263 }
    264 
    265 int
    266 mcdclose(dev)
    267 	dev_t dev;
    268 {
    269 	int unit, part;
    270 	struct mcd_softc *sc;
    271 
    272 	unit = MCDUNIT(dev);
    273 	part = MCDPART(dev);
    274 	sc = mcdcd.cd_devs[unit];
    275 
    276 	/* Get status. */
    277 	mcd_getstat(sc, 1);
    278 
    279 	/* Close channel. */
    280 	sc->partflags[part] &= ~(MCDOPEN | MCDREADRAW);
    281 	sc->openflags &= ~(1 << part);
    282 	MCD_TRACE("close: partition=%d\n", part, 0, 0, 0);
    283 
    284 	return 0;
    285 }
    286 
    287 void
    288 mcdstrategy(bp)
    289 	struct buf *bp;
    290 {
    291 	struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(bp->b_dev)];
    292 	struct buf *qp;
    293 	int s;
    294 
    295 	/* Test validity. */
    296 	MCD_TRACE("strategy: buf=0x%lx blkno=%ld bcount=%ld\n", bp,
    297 	    bp->b_blkno, bp->b_bcount, 0);
    298 	if (bp->b_blkno < 0) {
    299 		printf("%s: strategy: blkno=%d bcount=%d\n",
    300 		    sc->sc_dev.dv_xname, bp->b_blkno, bp->b_bcount);
    301 		bp->b_error = EINVAL;
    302 		bp->b_flags |= B_ERROR;
    303 		goto bad;
    304 	}
    305 
    306 	/* If device invalidated (e.g. media change, door open), error. */
    307 	if (!(sc->flags & MCDVALID)) {
    308 		MCD_TRACE("strategy: drive not valid\n", 0, 0, 0, 0);
    309 		bp->b_error = EIO;
    310 		goto bad;
    311 	}
    312 
    313 	/* Check for read only. */
    314 	if (!(bp->b_flags & B_READ)) {
    315 		bp->b_error = EROFS;
    316 		goto bad;
    317 	}
    318 
    319 	/* No data to read. */
    320 	if (bp->b_bcount == 0)
    321 		goto done;
    322 
    323 	/* For non raw access, check partition limits. */
    324 	if (MCDPART(bp->b_dev) != RAW_PART) {
    325 		if (!(sc->flags & MCDLABEL)) {
    326 			bp->b_error = EIO;
    327 			goto bad;
    328 		}
    329 		/* Adjust transfer if necessary. */
    330 		if (bounds_check_with_label(bp, &sc->dlabel, 1) <= 0)
    331 			goto done;
    332 	}
    333 
    334 	/* Queue it. */
    335 	qp = &sc->head;
    336 	s = splbio();
    337 	disksort(qp, bp);
    338 	splx(s);
    339 
    340 	/* Now check whether we can perform processing. */
    341 	mcd_start(sc);
    342 	return;
    343 
    344 bad:
    345 	bp->b_flags |= B_ERROR;
    346 done:
    347 	bp->b_resid = bp->b_bcount;
    348 	biodone(bp);
    349 	return;
    350 }
    351 
    352 int
    353 mcd_start(sc)
    354 	struct mcd_softc *sc;
    355 {
    356 	struct buf *bp, *qp = &sc->head;
    357 	struct partition *p;
    358 	int s = splbio();
    359 
    360 	if (sc->flags & MCDMBXBSY)
    361 		return;
    362 
    363 	if ((bp = qp->b_actf) != 0) {
    364 		/* Block found to process; dequeue. */
    365 		MCD_TRACE("start: found block bp=0x%x\n", bp, 0, 0, 0);
    366 		qp->b_actf = bp->b_actf;
    367 		splx(s);
    368 	} else {
    369 		/* Nothing to do; */
    370 		splx(s);
    371 		return;
    372 	}
    373 
    374 	/* Changed media? */
    375 	if (!(sc->flags	& MCDVALID)) {
    376 		MCD_TRACE("start: drive not valid\n", 0, 0, 0, 0);
    377 		return;
    378 	}
    379 
    380 	p = &sc->dlabel.d_partitions[MCDPART(bp->b_dev)];
    381 
    382 	sc->flags |= MCDMBXBSY;
    383 	sc->mbx.unit = sc->sc_dev.dv_unit;
    384 	sc->mbx.retry = MCD_RETRIES;
    385 	sc->mbx.bp = bp;
    386 	sc->mbx.p_offset = p->p_offset;
    387 
    388 	/* Calling the read routine. */
    389 	sc->mbx.state = MCD_S_BEGIN;
    390 	mcd_doread(&sc->mbx);
    391 	/* triggers mcd_start, when successful finished. */
    392 }
    393 
    394 int
    395 mcdioctl(dev, cmd, addr, flags, p)
    396 	dev_t dev;
    397 	int cmd;
    398 	caddr_t addr;
    399 	int flags;
    400 	struct proc *p;
    401 {
    402 	struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(dev)];
    403 
    404 	if (!(sc->flags & MCDVALID))
    405 		return EIO;
    406 	MCD_TRACE("ioctl: cmd=0x%x\n", cmd, 0, 0, 0);
    407 
    408 	switch (cmd) {
    409 	case DIOCSBAD:
    410 		return EINVAL;
    411 	case DIOCGDINFO:
    412 	case DIOCGPART:
    413 	case DIOCWDINFO:
    414 	case DIOCSDINFO:
    415 	case DIOCWLABEL:
    416 		return ENOTTY;
    417 	case CDIOCPLAYTRACKS:
    418 		return mcd_playtracks(sc, (struct ioc_play_track *) addr);
    419 	case CDIOCPLAYBLOCKS:
    420 		return mcd_play(sc, (struct mcd_read2 *) addr);
    421 	case CDIOCREADSUBCHANNEL:
    422 		return mcd_subchan(sc, (struct ioc_read_subchannel *) addr);
    423 	case CDIOREADTOCHEADER:
    424 		return mcd_toc_header(sc, (struct ioc_toc_header *) addr);
    425 	case CDIOREADTOCENTRYS:
    426 		return mcd_toc_entry(sc, (struct ioc_read_toc_entry *) addr);
    427 	case CDIOCSETPATCH:
    428 	case CDIOCGETVOL:
    429 	case CDIOCSETVOL:
    430 	case CDIOCSETMONO:
    431 	case CDIOCSETSTERIO:
    432 	case CDIOCSETMUTE:
    433 	case CDIOCSETLEFT:
    434 	case CDIOCSETRIGHT:
    435 		return EINVAL;
    436 	case CDIOCRESUME:
    437 		return mcd_resume(sc);
    438 	case CDIOCPAUSE:
    439 		return mcd_pause(sc);
    440 	case CDIOCSTART:
    441 		return EINVAL;
    442 	case CDIOCSTOP:
    443 		return mcd_stop(sc);
    444 	case CDIOCEJECT:
    445 		return EINVAL;
    446 	case CDIOCSETDEBUG:
    447 		sc->debug = 1;
    448 		return 0;
    449 	case CDIOCCLRDEBUG:
    450 		sc->debug = 0;
    451 		return 0;
    452 	case CDIOCRESET:
    453 		return EINVAL;
    454 	default:
    455 		return ENOTTY;
    456 	}
    457 #ifdef DIAGNOSTIC
    458 	panic("mcdioctl: impossible");
    459 #endif
    460 }
    461 
    462 /*
    463  * This could have been taken from scsi/cd.c, but it is not clear
    464  * whether the scsi cd driver is linked in.
    465  */
    466 int
    467 mcd_getdisklabel(sc)
    468 	struct mcd_softc *sc;
    469 {
    470 
    471 	if (sc->flags & MCDLABEL)
    472 		return -1;
    473 
    474 	bzero(&sc->dlabel, sizeof(struct disklabel));
    475 	strncpy(sc->dlabel.d_typename, "Mitsumi CD ROM ", 16);
    476 	strncpy(sc->dlabel.d_packname, "unknown        ", 16);
    477 	sc->dlabel.d_secsize 	= sc->blksize;
    478 	sc->dlabel.d_nsectors	= 100;
    479 	sc->dlabel.d_ntracks	= 1;
    480 	sc->dlabel.d_ncylinders	= (sc->disksize /100) + 1;
    481 	sc->dlabel.d_secpercyl	= 100;
    482 	sc->dlabel.d_secperunit	= sc->disksize;
    483 	sc->dlabel.d_rpm	= 300;
    484 	sc->dlabel.d_interleave	= 1;
    485 	sc->dlabel.d_flags	= D_REMOVABLE;
    486 	sc->dlabel.d_npartitions= 1;
    487 	sc->dlabel.d_partitions[0].p_offset = 0;
    488 	sc->dlabel.d_partitions[0].p_size = sc->disksize;
    489 	sc->dlabel.d_partitions[0].p_fstype = 9;
    490 	sc->dlabel.d_magic	= DISKMAGIC;
    491 	sc->dlabel.d_magic2	= DISKMAGIC;
    492 	sc->dlabel.d_checksum	= dkcksum(&sc->dlabel);
    493 
    494 	sc->flags |= MCDLABEL;
    495 	return 0;
    496 }
    497 
    498 int
    499 mcdsize(dev)
    500 	dev_t dev;
    501 {
    502 	int size;
    503 	struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(dev)];
    504 
    505 	if (mcd_volinfo(sc) >= 0) {
    506 		sc->blksize = MCDBLK;
    507 		size = msf2hsg(sc->volinfo.vol_msf);
    508 		sc->disksize = size * (MCDBLK / DEV_BSIZE);
    509 		return 0;
    510 	}
    511 	return -1;
    512 }
    513 
    514 /***************************************************************
    515  * lower level of driver starts here
    516  **************************************************************/
    517 
    518 #ifdef notyet
    519 static char irqs[] = {
    520 	0x00, 0x00, 0x10, 0x20, 0x00, 0x30, 0x00, 0x00,
    521 	0x00, 0x10, 0x40, 0x50, 0x00, 0x00, 0x00, 0x00
    522 };
    523 
    524 static char drqs[] = {
    525 	0x00, 0x01, 0x00, 0x03, 0x00, 0x05, 0x06, 0x07
    526 };
    527 #endif
    528 
    529 void
    530 mcd_configure(sc)
    531 	struct mcd_softc *sc;
    532 {
    533 
    534 	outb(sc->iobase + mcd_config, sc->config);
    535 }
    536 
    537 int
    538 mcdprobe(parent, self, aux)
    539 	struct device *parent, *self;
    540 	void *aux;
    541 {
    542 	struct mcd_softc *sc = (void *)self;
    543 	struct isa_attach_args *ia = aux;
    544 	u_short iobase = ia->ia_iobase;
    545 	int i;
    546 	int st, check;
    547 
    548 #ifdef notyet
    549 	/* Get irq/drq configuration word. */
    550 	sc->config = irqs[ia->ia_irq];
    551 #endif
    552 	sc->iobase = iobase;
    553 
    554 	/* Send a reset. */
    555 	outb(iobase + mcd_reset, 0);
    556 	delay(300000);
    557 	/* Get any pending status and throw away. */
    558 	for (i = 10; i; i--)
    559 		inb(iobase + mcd_status);
    560 	delay(1000);
    561 
    562 	/* Send get status command. */
    563 	outb(iobase + mcd_command, MCD_CMDGETSTAT);
    564 	i = mcd_getreply(sc, DELAY_GETREPLY);
    565 
    566 	if (i < 0) {
    567 #ifdef DEBUG
    568 		printf("Mitsumi drive NOT detected\n");
    569 #endif
    570 		return 0;
    571 	}
    572 
    573 /*
    574  * The following code uses the 0xDC command, it returns a M from the
    575  * second byte and a number in the third.
    576  * (I hope you have the right drive for that, most drives don't do!)
    577  * Whole code entirely rewriten by veit (at) gmd.de, the changes accessed
    578  * the drive in an illegal way. Proper way is to use the timeout
    579  * driven routines mcd_getreply etc. rather than arbitrary delays.
    580  */
    581 
    582 	delay(2000);
    583 	outb(iobase + mcd_command, MCD_CMDCONTINFO);
    584 	i = mcd_getreply(sc, DELAY_GETREPLY);
    585 
    586 	if (i < 0) {
    587 #ifdef DEBUG
    588 		printf("Mitsumi drive error\n");
    589 #endif
    590 		return 0;
    591 	}
    592 	st = mcd_getreply(sc, DELAY_GETREPLY);
    593 	if (st < 0)
    594 		return 0;
    595 	check = mcd_getreply(sc, DELAY_GETREPLY);
    596 	if (check < 0)
    597 		return 0;
    598 	/* Flush junk. */
    599 	(void) mcd_getreply(sc, DELAY_GETREPLY);
    600 
    601 	/*
    602 	 * The following is code which is not guaranteed to work for all
    603 	 * drives, because the meaning of the expected 'M' is not clear
    604 	 * (M_itsumi is an obvious assumption, but I don't trust that).
    605 	 * Also, the original hack had a bogus condition that always
    606 	 * returned true.
    607 	 */
    608 #ifdef notdef
    609 	if (check != 'M') {
    610 #ifdef DEBUG
    611 		printf("Mitsumi drive NOT detected\n");
    612 #endif
    613 		return 0;
    614 	}
    615 #endif
    616 
    617 #ifdef DEBUG
    618 	printf("Mitsumi drive detected\n");
    619 #endif
    620 	ia->ia_iosize = 4;
    621 	ia->ia_msize = 0;
    622 	return 1;
    623 }
    624 
    625 int
    626 mcd_waitrdy(iobase, dly)
    627 	u_short iobase;
    628 	int dly;
    629 {
    630 	int i;
    631 
    632 	/* Wait until xfer port senses data ready. */
    633 	for (i = dly; i; i--) {
    634 		if ((inb(iobase + mcd_xfer) & MCD_ST_BUSY) == 0)
    635 			return 0;
    636 		delay(1);
    637 	}
    638 	return -1;
    639 }
    640 
    641 int
    642 mcd_getreply(sc, dly)
    643 	struct mcd_softc *sc;
    644 	int dly;
    645 {
    646 	u_short iobase = sc->iobase;
    647 
    648 	/* Wait data to become ready. */
    649 	if (mcd_waitrdy(iobase, dly) < 0) {
    650 		printf("%s: timeout in getreply\n", sc->sc_dev.dv_xname);
    651 		return -1;
    652 	}
    653 
    654 	/* Get the data. */
    655 	return inb(iobase + mcd_status);
    656 }
    657 
    658 int
    659 mcd_getstat(sc, sflg)
    660 	struct mcd_softc *sc;
    661 	int sflg;
    662 {
    663 	int i;
    664 	u_short iobase = sc->iobase;
    665 
    666 	/* Get the status. */
    667 	if (sflg)
    668 		outb(iobase + mcd_command, MCD_CMDGETSTAT);
    669 	i = mcd_getreply(sc, DELAY_GETREPLY);
    670 	if (i < 0) {
    671 		printf("%s: timeout in getstat\n", sc->sc_dev.dv_xname);
    672 		return -1;
    673 	}
    674 
    675 	sc->status = i;
    676 
    677 	mcd_setflags(sc);
    678 	return sc->status;
    679 }
    680 
    681 void
    682 mcd_setflags(sc)
    683 	struct mcd_softc *sc;
    684 {
    685 
    686 	/* Check flags. */
    687 	if (sc->status & (MCDDSKCHNG | MCDDOOROPEN)) {
    688 		MCD_TRACE("getstat: sensed DSKCHNG or DOOROPEN\n", 0, 0, 0, 0);
    689 		sc->flags &= ~MCDVALID;
    690 	}
    691 
    692 	if (sc->status & MCDAUDIOBSY)
    693 		sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
    694 	else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS)
    695 		sc->audio_status = CD_AS_PLAY_COMPLETED;
    696 }
    697 
    698 int
    699 mcd_get(sc, buf, nmax)
    700 	struct mcd_softc *sc;
    701 	char *buf;
    702 	int nmax;
    703 {
    704 	int i, k;
    705 
    706 	for (i = 0; i < nmax; i++) {
    707 		/* Wait for data. */
    708 		if ((k = mcd_getreply(sc, DELAY_GETREPLY)) < 0) {
    709 			printf("%s: timeout in get\n", sc->sc_dev.dv_xname);
    710 			return -1;
    711 		}
    712 		buf[i] = k;
    713 	}
    714 	return i;
    715 }
    716 
    717 int
    718 mcd_send(sc, cmd, nretries)
    719 	struct mcd_softc *sc;
    720 	int cmd, nretries;
    721 {
    722 	int i, k;
    723 	u_short iobase = sc->iobase;
    724 
    725 	MCD_TRACE("send: cmd=0x%x\n", cmd, 0, 0, 0);
    726 
    727 	for (i = nretries; i; i--) {
    728 		outb(iobase + mcd_command, cmd);
    729 		if ((k = mcd_getstat(sc, 0)) != -1)
    730 			break;
    731 	}
    732 	if (!i) {
    733 		printf("%s: send: retry count exceeded\n", sc->sc_dev.dv_xname);
    734 		return -1;
    735 	}
    736 
    737 	MCD_TRACE("send: status=0x%x\n", k, 0, 0, 0);
    738 
    739 	return 0;
    740 }
    741 
    742 int
    743 bcd2bin(b)
    744 	bcd_t b;
    745 {
    746 
    747 	return (b >> 4) * 10 + (b & 15);
    748 }
    749 
    750 bcd_t
    751 bin2bcd(b)
    752 	int b;
    753 {
    754 
    755 	return ((b / 10) << 4) | (b % 10);
    756 }
    757 
    758 void
    759 hsg2msf(hsg, msf)
    760 	int hsg;
    761 	bcd_t *msf;
    762 {
    763 
    764 	hsg += 150;
    765 	M_msf(msf) = bin2bcd(hsg / 4500);
    766 	hsg %= 4500;
    767 	S_msf(msf) = bin2bcd(hsg / 75);
    768 	F_msf(msf) = bin2bcd(hsg % 75);
    769 }
    770 
    771 int
    772 msf2hsg(msf)
    773 	bcd_t *msf;
    774 {
    775 
    776 	return (bcd2bin(M_msf(msf)) * 60 +
    777 		bcd2bin(S_msf(msf))) * 75 +
    778 		bcd2bin(F_msf(msf)) - 150;
    779 }
    780 
    781 int
    782 mcd_volinfo(sc)
    783 	struct mcd_softc *sc;
    784 {
    785 
    786 	MCD_TRACE("volinfo: enter\n", 0, 0, 0, 0);
    787 
    788 	/* Get the status, in case the disc has been changed. */
    789 	if (mcd_getstat(sc, 1) < 0)
    790 		return EIO;
    791 
    792 	/* Just return if we already have it. */
    793 	if (sc->flags & MCDVOLINFO)
    794 		return 0;
    795 
    796 	/* Send volume info command. */
    797 	if (mcd_send(sc, MCD_CMDGETVOLINFO, MCD_RETRIES) < 0)
    798 		return -1;
    799 
    800 	/* Get the data. */
    801 	if (mcd_get(sc, (char*) &sc->volinfo, sizeof(struct mcd_volinfo)) < 0) {
    802 		printf("%s: volinfo: error reading data\n",
    803 		    sc->sc_dev.dv_xname);
    804 		return -1;
    805 	}
    806 
    807 	if (sc->volinfo.trk_low != 0 || sc->volinfo.trk_high != 0) {
    808 		/* Volinfo is OK. */
    809 		sc->flags |= MCDVOLINFO;
    810 		return 0;
    811 	}
    812 
    813 	return -1;
    814 }
    815 
    816 int
    817 mcdintr(sc)
    818 	struct mcd_softc *sc;
    819 {
    820 	u_short iobase = sc->iobase;
    821 
    822 	MCD_TRACE("stray interrupt xfer=0x%x\n", inb(iobase + mcd_xfer),
    823 	    0, 0, 0);
    824 
    825 	/* Just read out status and ignore the rest. */
    826 	if (inb(iobase + mcd_xfer) != 0xff)
    827 		(void) inb(iobase + mcd_status);
    828 
    829 	return -1;
    830 }
    831 
    832 /*
    833  * State machine to process read requests.
    834  * Initialize with MCD_S_BEGIN: calculate sizes, and read status
    835  * MCD_S_WAITSTAT: wait for status reply, set mode
    836  * MCD_S_WAITMODE: waits for status reply from set mode, set read command
    837  * MCD_S_WAITREAD: wait for read ready, read data.
    838  */
    839 void
    840 mcd_doread(arg)
    841 	void *arg;
    842 {
    843 	struct mcd_mbx *mbx = arg;
    844 	struct mcd_softc *sc = mcdcd.cd_devs[mbx->unit];
    845 	u_short iobase = sc->iobase;
    846 	struct buf *bp = mbx->bp;
    847 
    848 	int rm, i, k;
    849 	struct mcd_read2 rbuf;
    850 	int blkno;
    851 	caddr_t	addr;
    852 
    853 loop:
    854 	switch (mbx->state) {
    855 	case MCD_S_BEGIN:
    856 		/* Get status. */
    857 		outb(iobase + mcd_command, MCD_CMDGETSTAT);
    858 
    859 		mbx->count = RDELAY_WAITSTAT;
    860 		mbx->state = MCD_S_WAITSTAT;
    861 		timeout(mcd_doread, mbx, hz / 100);
    862 		return;
    863 
    864 	case MCD_S_WAITSTAT:
    865 		untimeout(mcd_doread, mbx);
    866 		if (mbx->count-- < 0) {
    867 			printf("%s: timeout getting status\n",
    868 			    sc->sc_dev.dv_xname);
    869 			goto readerr;
    870 		}
    871 		if (inb(iobase + mcd_xfer) & MCD_ST_BUSY) {
    872 			timeout(mcd_doread, mbx, hz / 100);
    873 			return;
    874 		}
    875 		mcd_setflags(sc);
    876 		MCD_TRACE("doread: got WAITSTAT delay=%d\n",
    877 		    RDELAY_WAITSTAT - mbx->count, 0, 0, 0);
    878 
    879 		/* Reject, if audio active. */
    880 		if (sc->status & MCDAUDIOBSY) {
    881 			printf("%s: audio is active\n",
    882 			    sc->sc_dev.dv_xname);
    883 			goto readerr;
    884 		}
    885 
    886 		/* Check for raw/cooked mode. */
    887 		if (sc->flags & MCDREADRAW) {
    888 			rm = MCD_MD_RAW;
    889 			mbx->sz = MCDRBLK;
    890 		} else {
    891 			rm = MCD_MD_COOKED;
    892 			mbx->sz = sc->blksize;
    893 		}
    894 
    895 		mcd_put(iobase + mcd_command, MCD_CMDSETMODE);
    896 		mcd_put(iobase + mcd_command, rm);
    897 
    898 		mbx->count = RDELAY_WAITMODE;
    899 		mbx->state = MCD_S_WAITMODE;
    900 		timeout(mcd_doread, mbx, hz / 100);
    901 		return;
    902 
    903 	case MCD_S_WAITMODE:
    904 		untimeout(mcd_doread, mbx);
    905 		if (mbx->count-- < 0) {
    906 			printf("%s: timeout setting mode\n",
    907 			    sc->sc_dev.dv_xname);
    908 			goto readerr;
    909 		}
    910 		if (inb(iobase + mcd_xfer) & MCD_ST_BUSY) {
    911 			timeout(mcd_doread, mbx, hz / 100);
    912 			return;
    913 		}
    914 		mcd_setflags(sc);
    915 		MCD_TRACE("doread: got WAITMODE delay=%d\n",
    916 		    RDELAY_WAITMODE - mbx->count, 0, 0, 0);
    917 
    918 		/* For first block. */
    919 		mbx->nblk = (bp->b_bcount + (mbx->sz - 1)) / mbx->sz;
    920 		mbx->skip = 0;
    921 
    922 	nextblock:
    923 		blkno = (bp->b_blkno / (mbx->sz / DEV_BSIZE)) + mbx->p_offset +
    924 		    (mbx->skip / mbx->sz);
    925 
    926 		MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", blkno, bp, 0,
    927 		    0);
    928 
    929 		/* Build parameter block. */
    930 		hsg2msf(blkno, rbuf.start_msf);
    931 
    932 		/* Send the read command. */
    933 		mcd_put(iobase + mcd_command, MCD_CMDREAD2);
    934 		mcd_put(iobase + mcd_command, rbuf.start_msf[0]);
    935 		mcd_put(iobase + mcd_command, rbuf.start_msf[1]);
    936 		mcd_put(iobase + mcd_command, rbuf.start_msf[2]);
    937 		mcd_put(iobase + mcd_command, 0);
    938 		mcd_put(iobase + mcd_command, 0);
    939 		mcd_put(iobase + mcd_command, 1);
    940 
    941 		mbx->count = RDELAY_WAITREAD;
    942 		mbx->state = MCD_S_WAITREAD;
    943 		timeout(mcd_doread, mbx, hz / 100);
    944 		return;
    945 
    946 	case MCD_S_WAITREAD:
    947 		untimeout(mcd_doread, mbx);
    948 		if (mbx->count-- < 0) {
    949 			printf("%s: timeout reading data\n",
    950 			    sc->sc_dev.dv_xname);
    951 			goto readerr;
    952 		}
    953 
    954 		k = inb(iobase + mcd_xfer);
    955 		if ((k & 2) == 0) {	/* XXX MCD_ST_AUDIOBSY? */
    956 			MCD_TRACE("doread: got data delay=%d\n",
    957 			    RDELAY_WAITREAD - mbx->count, 0, 0, 0);
    958 			/* Data is ready. */
    959 			addr = bp->b_un.b_addr + mbx->skip;
    960 			outb(iobase + mcd_ctl2, 0x04);	/* XXX */
    961 			for (i = 0; i < mbx->sz; i++)
    962 				*addr++	= inb(iobase + mcd_rdata);
    963 			outb(iobase + mcd_ctl2, 0x0c);	/* XXX */
    964 
    965 			if (--mbx->nblk > 0) {
    966 				mbx->skip += mbx->sz;
    967 				goto nextblock;
    968 			}
    969 
    970 			/* Return buffer. */
    971 			bp->b_resid = 0;
    972 			biodone(bp);
    973 
    974 			sc->flags &= ~MCDMBXBSY;
    975 			mcd_start(sc);
    976 			return;
    977 		}
    978 		if ((k & MCD_ST_BUSY) == 0)
    979 			mcd_getstat(sc, 0);
    980 		timeout(mcd_doread, mbx, hz / 100);
    981 		return;
    982 	}
    983 
    984 readerr:
    985 	if (mbx->retry-- > 0) {
    986 		printf("%s: retrying\n", sc->sc_dev.dv_xname);
    987 		mbx->state = MCD_S_BEGIN;
    988 		goto loop;
    989 	}
    990 
    991 	/* Invalidate the buffer. */
    992 	bp->b_flags |= B_ERROR;
    993 	bp->b_resid = bp->b_bcount;
    994 	biodone(bp);
    995 	mcd_start(sc);
    996 
    997 #ifdef notyet
    998 	printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname);
    999 	outb(mbx->iobase + mcd_reset, MCD_CMDRESET);
   1000 	delay(300000);
   1001 	(void)mcd_getstat(sc, 1);
   1002 	(void)mcd_getstat(sc, 1);
   1003 	/*sc->status &= ~MCDDSKCHNG; */
   1004 	sc->debug = 1; /* preventive set debug mode */
   1005 #endif
   1006 }
   1007 
   1008 int
   1009 mcd_setmode(sc, mode)
   1010 	struct mcd_softc *sc;
   1011 	int mode;
   1012 {
   1013 	u_short iobase = sc->iobase;
   1014 	int retry;
   1015 
   1016 	printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode);
   1017 	for (retry = MCD_RETRIES; retry; retry--) {
   1018 		outb(iobase + mcd_command, MCD_CMDSETMODE);
   1019 		outb(iobase + mcd_command, mode);
   1020 		if (mcd_getstat(sc, 0) != -1)
   1021 			return 0;
   1022 	}
   1023 
   1024 	return -1;
   1025 }
   1026 
   1027 int
   1028 mcd_toc_header(sc, th)
   1029 	struct mcd_softc *sc;
   1030 	struct ioc_toc_header *th;
   1031 {
   1032 
   1033 	if (mcd_volinfo(sc) < 0)
   1034 		return ENXIO;
   1035 
   1036 	th->len = msf2hsg(sc->volinfo.vol_msf);
   1037 	th->starting_track = bcd2bin(sc->volinfo.trk_low);
   1038 	th->ending_track = bcd2bin(sc->volinfo.trk_high);
   1039 
   1040 	return 0;
   1041 }
   1042 
   1043 int
   1044 mcd_read_toc(sc)
   1045 	struct mcd_softc *sc;
   1046 {
   1047 	struct ioc_toc_header th;
   1048 	struct mcd_qchninfo q;
   1049 	int rc, trk, idx, retry;
   1050 
   1051 	/* Only read TOC if needed. */
   1052 	if (sc->flags & MCDTOC)
   1053 		return 0;
   1054 
   1055 	if (sc->debug)
   1056 		printf("%s: read_toc: reading toc header\n",
   1057 		    sc->sc_dev.dv_xname);
   1058 	if (mcd_toc_header(sc, &th) != 0)
   1059 		return ENXIO;
   1060 
   1061 	if (sc->debug)
   1062 		printf("%s: read_toc: stopping play\n", sc->sc_dev.dv_xname);
   1063 	if ((rc = mcd_stop(sc)) != 0)
   1064 		return rc;
   1065 
   1066 	/* Try setting the mode twice. */
   1067 	if (mcd_setmode(sc, MCD_MD_TOC) != 0)
   1068 		return EIO;
   1069 	if (mcd_setmode(sc, MCD_MD_TOC) != 0)
   1070 		return EIO;
   1071 
   1072 	if (sc->debug)
   1073 		printf("%s: read_toc: reading qchannel info\n",
   1074 		    sc->sc_dev.dv_xname);
   1075 	for (trk = th.starting_track; trk <= th.ending_track; trk++)
   1076 		sc->toc[trk].idx_no = 0;
   1077 	trk = th.ending_track - th.starting_track + 1;
   1078 	for (retry = 300; retry && trk > 0; retry--) {
   1079 		if (mcd_getqchan(sc, &q) < 0)
   1080 			break;
   1081 		idx = bcd2bin(q.idx_no);
   1082 		if (idx > 0 && idx < MCD_MAXTOCS && q.trk_no == 0 &&
   1083 		    sc->toc[idx].idx_no == 0) {
   1084 			sc->toc[idx] = q;
   1085 			trk--;
   1086 		}
   1087 	}
   1088 
   1089 	if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
   1090 		return EIO;
   1091 
   1092 	if (trk != 0)
   1093 		return ENXIO;
   1094 
   1095 	/* Add a fake last+1. */
   1096 	idx = th.ending_track + 1;
   1097 	sc->toc[idx].ctrl_adr = sc->toc[idx-1].ctrl_adr;
   1098 	sc->toc[idx].trk_no = 0;
   1099 	sc->toc[idx].idx_no = 0xaa;
   1100 	sc->toc[idx].hd_pos_msf[0] = sc->volinfo.vol_msf[0];
   1101 	sc->toc[idx].hd_pos_msf[1] = sc->volinfo.vol_msf[1];
   1102 	sc->toc[idx].hd_pos_msf[2] = sc->volinfo.vol_msf[2];
   1103 
   1104 	sc->flags |= MCDTOC;
   1105 
   1106 	return 0;
   1107 }
   1108 
   1109 int
   1110 mcd_toc_entry(sc, te)
   1111 	struct mcd_softc *sc;
   1112 	struct ioc_read_toc_entry *te;
   1113 {
   1114 	struct ret_toc {
   1115 		struct ioc_toc_header th;
   1116 		struct cd_toc_entry rt;
   1117 	} ret_toc;
   1118 	struct ioc_toc_header th;
   1119 	int rc, i;
   1120 
   1121 	/* Make sure we have a valid TOC. */
   1122 	if ((rc = mcd_read_toc(sc)) != 0)
   1123 		return rc;
   1124 
   1125 	/* Find the TOC to copy. */
   1126 	i = te->starting_track;
   1127 	if (i == MCD_LASTPLUS1)
   1128 		i = bcd2bin(sc->volinfo.trk_high) + 1;
   1129 
   1130 	/* Verify starting track. */
   1131 	if (i < bcd2bin(sc->volinfo.trk_low) ||
   1132 	    i > bcd2bin(sc->volinfo.trk_high) + 1)
   1133 		return EINVAL;
   1134 
   1135 	/* Do we have room? */
   1136 	if (te->data_len < sizeof(struct ioc_toc_header) +
   1137 	    sizeof(struct cd_toc_entry))
   1138 		return EINVAL;
   1139 
   1140 	/* Copy the TOC header. */
   1141 	if (mcd_toc_header(sc, &th) < 0)
   1142 		return EIO;
   1143 	ret_toc.th = th;
   1144 
   1145 	/* Copy the TOC data. */
   1146 	ret_toc.rt.control = sc->toc[i].ctrl_adr;
   1147 	ret_toc.rt.addr_type = te->address_format;
   1148 	ret_toc.rt.track = i;
   1149 	if (te->address_format == CD_MSF_FORMAT) {
   1150 		ret_toc.rt.addr[1] = sc->toc[i].hd_pos_msf[0];
   1151 		ret_toc.rt.addr[2] = sc->toc[i].hd_pos_msf[1];
   1152 		ret_toc.rt.addr[3] = sc->toc[i].hd_pos_msf[2];
   1153 	}
   1154 
   1155 	/* Copy the data back. */
   1156 	copyout(&ret_toc, te->data,
   1157 	    sizeof(struct cd_toc_entry) + sizeof(struct ioc_toc_header));
   1158 
   1159 	return 0;
   1160 }
   1161 
   1162 int
   1163 mcd_stop(sc)
   1164 	struct mcd_softc *sc;
   1165 {
   1166 
   1167 	if (mcd_send(sc, MCD_CMDSTOPAUDIO, MCD_RETRIES) < 0)
   1168 		return ENXIO;
   1169 	sc->audio_status = CD_AS_PLAY_COMPLETED;
   1170 	return 0;
   1171 }
   1172 
   1173 int
   1174 mcd_getqchan(sc, q)
   1175 	struct mcd_softc *sc;
   1176 	struct mcd_qchninfo *q;
   1177 {
   1178 
   1179 	if (mcd_send(sc, MCD_CMDGETQCHN, MCD_RETRIES) < 0)
   1180 		return -1;
   1181 	if (mcd_get(sc, (char *) q, sizeof(struct mcd_qchninfo)) < 0)
   1182 		return -1;
   1183 	if (sc->debug)
   1184 		printf("%s: getqchan: ctl=%d t=%d i=%d ttm=%d:%d.%d dtm=%d:%d.%d\n",
   1185 		    sc->sc_dev.dv_xname, q->ctrl_adr, q->trk_no, q->idx_no,
   1186 		    q->trk_size_msf[0], q->trk_size_msf[1], q->trk_size_msf[2],
   1187 		    q->trk_size_msf[0], q->trk_size_msf[1], q->trk_size_msf[2]);
   1188 	return 0;
   1189 }
   1190 
   1191 int
   1192 mcd_subchan(sc, ch)
   1193 	struct mcd_softc *sc;
   1194 	struct ioc_read_subchannel *ch;
   1195 {
   1196 	struct mcd_qchninfo q;
   1197 	struct cd_sub_channel_info data;
   1198 
   1199 	if (sc->debug)
   1200 		printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname,
   1201 		    ch->address_format, ch->data_format);
   1202 
   1203 	if (ch->address_format != CD_MSF_FORMAT)
   1204 		return EIO;
   1205 	if (ch->data_format != CD_CURRENT_POSITION)
   1206 		return EIO;
   1207 	if (mcd_getqchan(sc, &q) < 0)
   1208 		return EIO;
   1209 
   1210 	data.header.audio_status = sc->audio_status;
   1211 	data.what.position.data_format = CD_MSF_FORMAT;
   1212 	data.what.position.track_number = bcd2bin(q.trk_no);
   1213 
   1214 	if (copyout(&data, ch->data, sizeof(struct cd_sub_channel_info)) != 0)
   1215 		return EFAULT;
   1216 	return 0;
   1217 }
   1218 
   1219 int
   1220 mcd_playtracks(sc, pt)
   1221 	struct mcd_softc *sc;
   1222 	struct ioc_play_track *pt;
   1223 {
   1224 	struct mcd_read2 pb;
   1225 	int a = pt->start_track;
   1226 	int z = pt->end_track;
   1227 	int rc;
   1228 
   1229 	if ((rc = mcd_read_toc(sc)) != 0)
   1230 		return rc;
   1231 
   1232 	printf("%s: playtracks: from %d:%d to %d:%d\n", sc->sc_dev.dv_xname,
   1233 	    a, pt->start_index, z, pt->end_index);
   1234 
   1235 	if (a < sc->volinfo.trk_low || a > sc->volinfo.trk_high || a > z ||
   1236 	    z < sc->volinfo.trk_low || z > sc->volinfo.trk_high)
   1237 		return EINVAL;
   1238 
   1239 	pb.start_msf[0] = sc->toc[a].hd_pos_msf[0];
   1240 	pb.start_msf[1] = sc->toc[a].hd_pos_msf[1];
   1241 	pb.start_msf[2] = sc->toc[a].hd_pos_msf[2];
   1242 	pb.end_msf[0] = sc->toc[z+1].hd_pos_msf[0];
   1243 	pb.end_msf[1] = sc->toc[z+1].hd_pos_msf[1];
   1244 	pb.end_msf[2] = sc->toc[z+1].hd_pos_msf[2];
   1245 
   1246 	return mcd_play(sc, &pb);
   1247 }
   1248 
   1249 int
   1250 mcd_play(sc, pb)
   1251 	struct mcd_softc *sc;
   1252 	struct mcd_read2 *pb;
   1253 {
   1254 	u_short iobase = sc->iobase;
   1255 	int retry, st;
   1256 
   1257 	sc->lastpb = *pb;
   1258 	for (retry = MCD_RETRIES; retry; retry--) {
   1259 		outb(iobase + mcd_command, MCD_CMDREAD2);
   1260 		outb(iobase + mcd_command, pb->start_msf[0]);
   1261 		outb(iobase + mcd_command, pb->start_msf[1]);
   1262 		outb(iobase + mcd_command, pb->start_msf[2]);
   1263 		outb(iobase + mcd_command, pb->end_msf[0]);
   1264 		outb(iobase + mcd_command, pb->end_msf[1]);
   1265 		outb(iobase + mcd_command, pb->end_msf[2]);
   1266 		if ((st = mcd_getstat(sc, 0)) != -1)
   1267 			break;
   1268 	}
   1269 	if (sc->debug)
   1270 		printf("%s: play: retry=%d status=%d\n", sc->sc_dev.dv_xname,
   1271 		    retry, st);
   1272 	if (!retry)
   1273 		return ENXIO;
   1274 
   1275 	sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
   1276 	return 0;
   1277 }
   1278 
   1279 int
   1280 mcd_pause(sc)
   1281 	struct mcd_softc *sc;
   1282 {
   1283 	struct mcd_qchninfo q;
   1284 	int rc;
   1285 
   1286 	/* Verify current status. */
   1287 	if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS)	{
   1288 		printf("%s: pause: attempted when not playing\n",
   1289 		    sc->sc_dev.dv_xname);
   1290 		return EINVAL;
   1291 	}
   1292 
   1293 	/* Get the current position. */
   1294 	if (mcd_getqchan(sc, &q) < 0)
   1295 		return EIO;
   1296 
   1297 	/* Copy it into lastpb. */
   1298 	sc->lastpb.start_msf[0] = q.hd_pos_msf[0];
   1299 	sc->lastpb.start_msf[1] = q.hd_pos_msf[1];
   1300 	sc->lastpb.start_msf[2] = q.hd_pos_msf[2];
   1301 
   1302 	/* Stop playing. */
   1303 	if ((rc = mcd_stop(sc)) != 0)
   1304 		return rc;
   1305 
   1306 	/* Set the proper status and exit. */
   1307 	sc->audio_status = CD_AS_PLAY_PAUSED;
   1308 	return 0;
   1309 }
   1310 
   1311 int
   1312 mcd_resume(sc)
   1313 	struct mcd_softc *sc;
   1314 {
   1315 
   1316 	if (sc->audio_status != CD_AS_PLAY_PAUSED)
   1317 		return EINVAL;
   1318 	return mcd_play(sc, &sc->lastpb);
   1319 }
   1320