Home | History | Annotate | Line # | Download | only in isa
mcd.c revision 1.18
      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.18 1994/08/05 22:57:14 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 		splx(s);
    362 		return;
    363 	}
    364 
    365 	if ((bp = qp->b_actf) != 0) {
    366 		/* Block found to process; dequeue. */
    367 		MCD_TRACE("start: found block bp=0x%x\n", bp, 0, 0, 0);
    368 		qp->b_actf = bp->b_actf;
    369 		splx(s);
    370 	} else {
    371 		/* Nothing to do; */
    372 		splx(s);
    373 		return;
    374 	}
    375 
    376 	/* Changed media? */
    377 	if (!(sc->flags	& MCDVALID)) {
    378 		MCD_TRACE("start: drive not valid\n", 0, 0, 0, 0);
    379 		return;
    380 	}
    381 
    382 	p = &sc->dlabel.d_partitions[MCDPART(bp->b_dev)];
    383 
    384 	sc->flags |= MCDMBXBSY;
    385 	sc->mbx.unit = sc->sc_dev.dv_unit;
    386 	sc->mbx.retry = MCD_RETRIES;
    387 	sc->mbx.bp = bp;
    388 	sc->mbx.p_offset = p->p_offset;
    389 
    390 	/* Calling the read routine. */
    391 	sc->mbx.state = MCD_S_BEGIN;
    392 	mcd_doread(&sc->mbx);
    393 	/* triggers mcd_start, when successful finished. */
    394 }
    395 
    396 int
    397 mcdioctl(dev, cmd, addr, flags, p)
    398 	dev_t dev;
    399 	int cmd;
    400 	caddr_t addr;
    401 	int flags;
    402 	struct proc *p;
    403 {
    404 	struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(dev)];
    405 
    406 	if (!(sc->flags & MCDVALID))
    407 		return EIO;
    408 	MCD_TRACE("ioctl: cmd=0x%x\n", cmd, 0, 0, 0);
    409 
    410 	switch (cmd) {
    411 	case DIOCSBAD:
    412 		return EINVAL;
    413 	case DIOCGDINFO:
    414 	case DIOCGPART:
    415 	case DIOCWDINFO:
    416 	case DIOCSDINFO:
    417 	case DIOCWLABEL:
    418 		return ENOTTY;
    419 	case CDIOCPLAYTRACKS:
    420 		return mcd_playtracks(sc, (struct ioc_play_track *) addr);
    421 	case CDIOCPLAYBLOCKS:
    422 		return mcd_play(sc, (struct mcd_read2 *) addr);
    423 	case CDIOCREADSUBCHANNEL:
    424 		return mcd_subchan(sc, (struct ioc_read_subchannel *) addr);
    425 	case CDIOREADTOCHEADER:
    426 		return mcd_toc_header(sc, (struct ioc_toc_header *) addr);
    427 	case CDIOREADTOCENTRYS:
    428 		return mcd_toc_entry(sc, (struct ioc_read_toc_entry *) addr);
    429 	case CDIOCSETPATCH:
    430 	case CDIOCGETVOL:
    431 	case CDIOCSETVOL:
    432 	case CDIOCSETMONO:
    433 	case CDIOCSETSTEREO:
    434 	case CDIOCSETMUTE:
    435 	case CDIOCSETLEFT:
    436 	case CDIOCSETRIGHT:
    437 		return EINVAL;
    438 	case CDIOCRESUME:
    439 		return mcd_resume(sc);
    440 	case CDIOCPAUSE:
    441 		return mcd_pause(sc);
    442 	case CDIOCSTART:
    443 		return EINVAL;
    444 	case CDIOCSTOP:
    445 		return mcd_stop(sc);
    446 	case CDIOCEJECT:
    447 		return EINVAL;
    448 	case CDIOCSETDEBUG:
    449 		sc->debug = 1;
    450 		return 0;
    451 	case CDIOCCLRDEBUG:
    452 		sc->debug = 0;
    453 		return 0;
    454 	case CDIOCRESET:
    455 		return EINVAL;
    456 	default:
    457 		return ENOTTY;
    458 	}
    459 #ifdef DIAGNOSTIC
    460 	panic("mcdioctl: impossible");
    461 #endif
    462 }
    463 
    464 /*
    465  * This could have been taken from scsi/cd.c, but it is not clear
    466  * whether the scsi cd driver is linked in.
    467  */
    468 int
    469 mcd_getdisklabel(sc)
    470 	struct mcd_softc *sc;
    471 {
    472 
    473 	if (sc->flags & MCDLABEL)
    474 		return -1;
    475 
    476 	bzero(&sc->dlabel, sizeof(struct disklabel));
    477 	strncpy(sc->dlabel.d_typename, "Mitsumi CD ROM ", 16);
    478 	strncpy(sc->dlabel.d_packname, "unknown        ", 16);
    479 	sc->dlabel.d_secsize 	= sc->blksize;
    480 	sc->dlabel.d_nsectors	= 100;
    481 	sc->dlabel.d_ntracks	= 1;
    482 	sc->dlabel.d_ncylinders	= (sc->disksize /100) + 1;
    483 	sc->dlabel.d_secpercyl	= 100;
    484 	sc->dlabel.d_secperunit	= sc->disksize;
    485 	sc->dlabel.d_rpm	= 300;
    486 	sc->dlabel.d_interleave	= 1;
    487 	sc->dlabel.d_flags	= D_REMOVABLE;
    488 	sc->dlabel.d_npartitions= 1;
    489 	sc->dlabel.d_partitions[0].p_offset = 0;
    490 	sc->dlabel.d_partitions[0].p_size = sc->disksize;
    491 	sc->dlabel.d_partitions[0].p_fstype = 9;
    492 	sc->dlabel.d_magic	= DISKMAGIC;
    493 	sc->dlabel.d_magic2	= DISKMAGIC;
    494 	sc->dlabel.d_checksum	= dkcksum(&sc->dlabel);
    495 
    496 	sc->flags |= MCDLABEL;
    497 	return 0;
    498 }
    499 
    500 int
    501 mcdsize(dev)
    502 	dev_t dev;
    503 {
    504 	int size;
    505 	struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(dev)];
    506 
    507 	if (mcd_volinfo(sc) >= 0) {
    508 		sc->blksize = MCDBLK;
    509 		size = msf2hsg(sc->volinfo.vol_msf);
    510 		sc->disksize = size * (MCDBLK / DEV_BSIZE);
    511 		return 0;
    512 	}
    513 	return -1;
    514 }
    515 
    516 int
    517 mcddump()
    518 {
    519 
    520 	/* Not implemented. */
    521 	return EINVAL;
    522 }
    523 
    524 /***************************************************************
    525  * lower level of driver starts here
    526  **************************************************************/
    527 
    528 #ifdef notyet
    529 static char irqs[] = {
    530 	0x00, 0x00, 0x10, 0x20, 0x00, 0x30, 0x00, 0x00,
    531 	0x00, 0x10, 0x40, 0x50, 0x00, 0x00, 0x00, 0x00
    532 };
    533 
    534 static char drqs[] = {
    535 	0x00, 0x01, 0x00, 0x03, 0x00, 0x05, 0x06, 0x07
    536 };
    537 #endif
    538 
    539 void
    540 mcd_configure(sc)
    541 	struct mcd_softc *sc;
    542 {
    543 
    544 	outb(sc->iobase + mcd_config, sc->config);
    545 }
    546 
    547 int
    548 mcdprobe(parent, self, aux)
    549 	struct device *parent, *self;
    550 	void *aux;
    551 {
    552 	struct mcd_softc *sc = (void *)self;
    553 	struct isa_attach_args *ia = aux;
    554 	u_short iobase = ia->ia_iobase;
    555 	int i;
    556 	int st, check, version;
    557 
    558 #ifdef notyet
    559 	/* Get irq/drq configuration word. */
    560 	sc->config = irqs[ia->ia_irq];
    561 #endif
    562 	sc->iobase = iobase;
    563 
    564 	/* Send a reset. */
    565 	outb(iobase + mcd_reset, 0);
    566 	delay(1000000);
    567 	/* Get any pending status and throw away. */
    568 	for (i = 10; i; i--)
    569 		inb(iobase + mcd_status);
    570 	delay(1000);
    571 
    572 	/* Send get status command. */
    573 	outb(iobase + mcd_command, MCD_CMDGETSTAT);
    574 	st = mcd_getreply(sc, DELAY_GETREPLY);
    575 
    576 	if (st < 0) {
    577 #ifdef DEBUG
    578 		printf("Mitsumi drive NOT detected\n");
    579 #endif
    580 		return 0;
    581 	}
    582 
    583 /*
    584  * The following code uses the 0xDC command, it returns a M from the
    585  * second byte and a number in the third.
    586  * (I hope you have the right drive for that, most drives don't do!)
    587  * Whole code entirely rewriten by veit (at) gmd.de, the changes accessed
    588  * the drive in an illegal way. Proper way is to use the timeout
    589  * driven routines mcd_getreply etc. rather than arbitrary delays.
    590  */
    591 
    592 	delay(2000);
    593 	outb(iobase + mcd_command, MCD_CMDCONTINFO);
    594 	st = mcd_getreply(sc, DELAY_GETREPLY);
    595 
    596 	if (st < 0) {
    597 #ifdef DEBUG
    598 		printf("Mitsumi drive error\n");
    599 #endif
    600 		return 0;
    601 	}
    602 	check = mcd_getreply(sc, DELAY_GETREPLY);
    603 	if (check < 0)
    604 		return 0;
    605 	version = mcd_getreply(sc, DELAY_GETREPLY);
    606 	if (version < 0)
    607 		return 0;
    608 	/* Flush junk. */
    609 	(void) mcd_getreply(sc, DELAY_GETREPLY);
    610 
    611 	/*
    612 	 * The following is code which is not guaranteed to work for all
    613 	 * drives, because the meaning of the expected 'M' is not clear
    614 	 * (M_itsumi is an obvious assumption, but I don't trust that).
    615 	 * Also, the original hack had a bogus condition that always
    616 	 * returned true.
    617 	 */
    618 	if (check != 'D' && check != 'M') {
    619 		printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n",
    620 		    sc->sc_dev.dv_xname, check, version);
    621 	}
    622 
    623 #ifdef DEBUG
    624 	printf("Mitsumi drive detected\n");
    625 #endif
    626 	ia->ia_iosize = 4;
    627 	ia->ia_msize = 0;
    628 	return 1;
    629 }
    630 
    631 int
    632 mcd_waitrdy(iobase, dly)
    633 	u_short iobase;
    634 	int dly;
    635 {
    636 	int i;
    637 
    638 	/* Wait until xfer port senses data ready. */
    639 	for (i = dly; i; i--) {
    640 		if ((inb(iobase + mcd_xfer) & MCD_ST_BUSY) == 0)
    641 			return 0;
    642 		delay(1);
    643 	}
    644 	return -1;
    645 }
    646 
    647 int
    648 mcd_getreply(sc, dly)
    649 	struct mcd_softc *sc;
    650 	int dly;
    651 {
    652 	u_short iobase = sc->iobase;
    653 
    654 	/* Wait data to become ready. */
    655 	if (mcd_waitrdy(iobase, dly) < 0) {
    656 		printf("%s: timeout in getreply\n", sc->sc_dev.dv_xname);
    657 		return -1;
    658 	}
    659 
    660 	/* Get the data. */
    661 	return inb(iobase + mcd_status);
    662 }
    663 
    664 int
    665 mcd_getstat(sc, sflg)
    666 	struct mcd_softc *sc;
    667 	int sflg;
    668 {
    669 	int i;
    670 	u_short iobase = sc->iobase;
    671 
    672 	/* Get the status. */
    673 	if (sflg)
    674 		outb(iobase + mcd_command, MCD_CMDGETSTAT);
    675 	i = mcd_getreply(sc, DELAY_GETREPLY);
    676 	if (i < 0) {
    677 		printf("%s: timeout in getstat\n", sc->sc_dev.dv_xname);
    678 		return -1;
    679 	}
    680 
    681 	sc->status = i;
    682 
    683 	mcd_setflags(sc);
    684 	return sc->status;
    685 }
    686 
    687 void
    688 mcd_setflags(sc)
    689 	struct mcd_softc *sc;
    690 {
    691 
    692 	/* Check flags. */
    693 	if (sc->status & (MCDDSKCHNG | MCDDOOROPEN)) {
    694 		MCD_TRACE("getstat: sensed DSKCHNG or DOOROPEN\n", 0, 0, 0, 0);
    695 		sc->flags &= ~MCDVALID;
    696 	}
    697 
    698 	if (sc->status & MCDAUDIOBSY)
    699 		sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
    700 	else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS)
    701 		sc->audio_status = CD_AS_PLAY_COMPLETED;
    702 }
    703 
    704 int
    705 mcd_get(sc, buf, nmax)
    706 	struct mcd_softc *sc;
    707 	char *buf;
    708 	int nmax;
    709 {
    710 	int i, k;
    711 
    712 	for (i = 0; i < nmax; i++) {
    713 		/* Wait for data. */
    714 		if ((k = mcd_getreply(sc, DELAY_GETREPLY)) < 0) {
    715 			printf("%s: timeout in get\n", sc->sc_dev.dv_xname);
    716 			return -1;
    717 		}
    718 		buf[i] = k;
    719 	}
    720 	return i;
    721 }
    722 
    723 int
    724 mcd_send(sc, cmd, nretries)
    725 	struct mcd_softc *sc;
    726 	int cmd, nretries;
    727 {
    728 	int i, k;
    729 	u_short iobase = sc->iobase;
    730 
    731 	MCD_TRACE("send: cmd=0x%x\n", cmd, 0, 0, 0);
    732 
    733 	for (i = nretries; i; i--) {
    734 		outb(iobase + mcd_command, cmd);
    735 		if ((k = mcd_getstat(sc, 0)) != -1)
    736 			break;
    737 	}
    738 	if (!i) {
    739 		printf("%s: send: retry count exceeded\n", sc->sc_dev.dv_xname);
    740 		return -1;
    741 	}
    742 
    743 	MCD_TRACE("send: status=0x%x\n", k, 0, 0, 0);
    744 
    745 	return 0;
    746 }
    747 
    748 int
    749 bcd2bin(b)
    750 	bcd_t b;
    751 {
    752 
    753 	return (b >> 4) * 10 + (b & 15);
    754 }
    755 
    756 bcd_t
    757 bin2bcd(b)
    758 	int b;
    759 {
    760 
    761 	return ((b / 10) << 4) | (b % 10);
    762 }
    763 
    764 void
    765 hsg2msf(hsg, msf)
    766 	int hsg;
    767 	bcd_t *msf;
    768 {
    769 
    770 	hsg += 150;
    771 	M_msf(msf) = bin2bcd(hsg / 4500);
    772 	hsg %= 4500;
    773 	S_msf(msf) = bin2bcd(hsg / 75);
    774 	F_msf(msf) = bin2bcd(hsg % 75);
    775 }
    776 
    777 int
    778 msf2hsg(msf)
    779 	bcd_t *msf;
    780 {
    781 
    782 	return (bcd2bin(M_msf(msf)) * 60 +
    783 		bcd2bin(S_msf(msf))) * 75 +
    784 		bcd2bin(F_msf(msf)) - 150;
    785 }
    786 
    787 int
    788 mcd_volinfo(sc)
    789 	struct mcd_softc *sc;
    790 {
    791 
    792 	MCD_TRACE("volinfo: enter\n", 0, 0, 0, 0);
    793 
    794 	/* Get the status, in case the disc has been changed. */
    795 	if (mcd_getstat(sc, 1) < 0)
    796 		return EIO;
    797 
    798 	/* Just return if we already have it. */
    799 	if (sc->flags & MCDVOLINFO)
    800 		return 0;
    801 
    802 	/* Send volume info command. */
    803 	if (mcd_send(sc, MCD_CMDGETVOLINFO, MCD_RETRIES) < 0)
    804 		return -1;
    805 
    806 	/* Get the data. */
    807 	if (mcd_get(sc, (char*) &sc->volinfo, sizeof(struct mcd_volinfo)) < 0) {
    808 		printf("%s: volinfo: error reading data\n",
    809 		    sc->sc_dev.dv_xname);
    810 		return -1;
    811 	}
    812 
    813 	if (sc->volinfo.trk_low != 0 || sc->volinfo.trk_high != 0) {
    814 		/* Volinfo is OK. */
    815 		sc->flags |= MCDVOLINFO;
    816 		return 0;
    817 	}
    818 
    819 	return -1;
    820 }
    821 
    822 int
    823 mcdintr(sc)
    824 	struct mcd_softc *sc;
    825 {
    826 	u_short iobase = sc->iobase;
    827 
    828 	MCD_TRACE("stray interrupt xfer=0x%x\n", inb(iobase + mcd_xfer),
    829 	    0, 0, 0);
    830 
    831 	/* Just read out status and ignore the rest. */
    832 	if (inb(iobase + mcd_xfer) != 0xff)
    833 		(void) inb(iobase + mcd_status);
    834 
    835 	return -1;
    836 }
    837 
    838 /*
    839  * State machine to process read requests.
    840  * Initialize with MCD_S_BEGIN: calculate sizes, and read status
    841  * MCD_S_WAITSTAT: wait for status reply, set mode
    842  * MCD_S_WAITMODE: waits for status reply from set mode, set read command
    843  * MCD_S_WAITREAD: wait for read ready, read data.
    844  */
    845 void
    846 mcd_doread(arg)
    847 	void *arg;
    848 {
    849 	struct mcd_mbx *mbx = arg;
    850 	struct mcd_softc *sc = mcdcd.cd_devs[mbx->unit];
    851 	u_short iobase = sc->iobase;
    852 	struct buf *bp = mbx->bp;
    853 
    854 	int rm, i, k;
    855 	struct mcd_read2 rbuf;
    856 	int blkno;
    857 	caddr_t	addr;
    858 
    859 loop:
    860 	switch (mbx->state) {
    861 	case MCD_S_BEGIN:
    862 		/* Get status. */
    863 		outb(iobase + mcd_command, MCD_CMDGETSTAT);
    864 
    865 		mbx->count = RDELAY_WAITSTAT;
    866 		mbx->state = MCD_S_WAITSTAT;
    867 		timeout(mcd_doread, mbx, hz / 100);
    868 		return;
    869 
    870 	case MCD_S_WAITSTAT:
    871 		untimeout(mcd_doread, mbx);
    872 		if (mbx->count-- < 0) {
    873 			printf("%s: timeout getting status\n",
    874 			    sc->sc_dev.dv_xname);
    875 			goto readerr;
    876 		}
    877 		if (inb(iobase + mcd_xfer) & MCD_ST_BUSY) {
    878 			timeout(mcd_doread, mbx, hz / 100);
    879 			return;
    880 		}
    881 		mcd_setflags(sc);
    882 		MCD_TRACE("doread: got WAITSTAT delay=%d\n",
    883 		    RDELAY_WAITSTAT - mbx->count, 0, 0, 0);
    884 
    885 		/* Reject, if audio active. */
    886 		if (sc->status & MCDAUDIOBSY) {
    887 			printf("%s: audio is active\n",
    888 			    sc->sc_dev.dv_xname);
    889 			goto readerr;
    890 		}
    891 
    892 		/* Check for raw/cooked mode. */
    893 		if (sc->flags & MCDREADRAW) {
    894 			rm = MCD_MD_RAW;
    895 			mbx->sz = MCDRBLK;
    896 		} else {
    897 			rm = MCD_MD_COOKED;
    898 			mbx->sz = sc->blksize;
    899 		}
    900 
    901 		mcd_put(iobase + mcd_command, MCD_CMDSETMODE);
    902 		mcd_put(iobase + mcd_command, rm);
    903 
    904 		mbx->count = RDELAY_WAITMODE;
    905 		mbx->state = MCD_S_WAITMODE;
    906 		timeout(mcd_doread, mbx, hz / 100);
    907 		return;
    908 
    909 	case MCD_S_WAITMODE:
    910 		untimeout(mcd_doread, mbx);
    911 		if (mbx->count-- < 0) {
    912 			printf("%s: timeout setting mode\n",
    913 			    sc->sc_dev.dv_xname);
    914 			goto readerr;
    915 		}
    916 		if (inb(iobase + mcd_xfer) & MCD_ST_BUSY) {
    917 			timeout(mcd_doread, mbx, hz / 100);
    918 			return;
    919 		}
    920 		mcd_setflags(sc);
    921 		MCD_TRACE("doread: got WAITMODE delay=%d\n",
    922 		    RDELAY_WAITMODE - mbx->count, 0, 0, 0);
    923 
    924 		/* For first block. */
    925 		mbx->nblk = (bp->b_bcount + (mbx->sz - 1)) / mbx->sz;
    926 		mbx->skip = 0;
    927 
    928 	nextblock:
    929 		blkno = (bp->b_blkno / (mbx->sz / DEV_BSIZE)) + mbx->p_offset +
    930 		    (mbx->skip / mbx->sz);
    931 
    932 		MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", blkno, bp, 0,
    933 		    0);
    934 
    935 		/* Build parameter block. */
    936 		hsg2msf(blkno, rbuf.start_msf);
    937 
    938 		/* Send the read command. */
    939 		mcd_put(iobase + mcd_command, MCD_CMDREAD2);
    940 		mcd_put(iobase + mcd_command, rbuf.start_msf[0]);
    941 		mcd_put(iobase + mcd_command, rbuf.start_msf[1]);
    942 		mcd_put(iobase + mcd_command, rbuf.start_msf[2]);
    943 		mcd_put(iobase + mcd_command, 0);
    944 		mcd_put(iobase + mcd_command, 0);
    945 		mcd_put(iobase + mcd_command, 1);
    946 
    947 		mbx->count = RDELAY_WAITREAD;
    948 		mbx->state = MCD_S_WAITREAD;
    949 		timeout(mcd_doread, mbx, hz / 100);
    950 		return;
    951 
    952 	case MCD_S_WAITREAD:
    953 		untimeout(mcd_doread, mbx);
    954 		if (mbx->count-- < 0) {
    955 			printf("%s: timeout reading data\n",
    956 			    sc->sc_dev.dv_xname);
    957 			goto readerr;
    958 		}
    959 
    960 		k = inb(iobase + mcd_xfer);
    961 		if ((k & 2) == 0) {	/* XXX MCD_ST_AUDIOBSY? */
    962 			MCD_TRACE("doread: got data delay=%d\n",
    963 			    RDELAY_WAITREAD - mbx->count, 0, 0, 0);
    964 			/* Data is ready. */
    965 			addr = bp->b_data + mbx->skip;
    966 			outb(iobase + mcd_ctl2, 0x04);	/* XXX */
    967 			for (i = 0; i < mbx->sz; i++)
    968 				*addr++	= inb(iobase + mcd_rdata);
    969 			outb(iobase + mcd_ctl2, 0x0c);	/* XXX */
    970 
    971 			if (--mbx->nblk > 0) {
    972 				mbx->skip += mbx->sz;
    973 				goto nextblock;
    974 			}
    975 
    976 			/* Return buffer. */
    977 			bp->b_resid = 0;
    978 			biodone(bp);
    979 
    980 			sc->flags &= ~MCDMBXBSY;
    981 			mcd_start(sc);
    982 			return;
    983 		}
    984 		if ((k & MCD_ST_BUSY) == 0)
    985 			mcd_getstat(sc, 0);
    986 		timeout(mcd_doread, mbx, hz / 100);
    987 		return;
    988 	}
    989 
    990 readerr:
    991 	if (mbx->retry-- > 0) {
    992 		printf("%s: retrying\n", sc->sc_dev.dv_xname);
    993 		mbx->state = MCD_S_BEGIN;
    994 		goto loop;
    995 	}
    996 
    997 	/* Invalidate the buffer. */
    998 	bp->b_flags |= B_ERROR;
    999 	bp->b_resid = bp->b_bcount;
   1000 	biodone(bp);
   1001 	mcd_start(sc);
   1002 
   1003 #ifdef notyet
   1004 	printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname);
   1005 	outb(mbx->iobase + mcd_reset, MCD_CMDRESET);
   1006 	delay(300000);
   1007 	(void)mcd_getstat(sc, 1);
   1008 	(void)mcd_getstat(sc, 1);
   1009 	/*sc->status &= ~MCDDSKCHNG; */
   1010 	sc->debug = 1; /* preventive set debug mode */
   1011 #endif
   1012 }
   1013 
   1014 int
   1015 mcd_setmode(sc, mode)
   1016 	struct mcd_softc *sc;
   1017 	int mode;
   1018 {
   1019 	u_short iobase = sc->iobase;
   1020 	int retry;
   1021 
   1022 	printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode);
   1023 	for (retry = MCD_RETRIES; retry; retry--) {
   1024 		outb(iobase + mcd_command, MCD_CMDSETMODE);
   1025 		outb(iobase + mcd_command, mode);
   1026 		if (mcd_getstat(sc, 0) != -1)
   1027 			return 0;
   1028 	}
   1029 
   1030 	return -1;
   1031 }
   1032 
   1033 int
   1034 mcd_toc_header(sc, th)
   1035 	struct mcd_softc *sc;
   1036 	struct ioc_toc_header *th;
   1037 {
   1038 
   1039 	if (mcd_volinfo(sc) < 0)
   1040 		return ENXIO;
   1041 
   1042 	th->len = msf2hsg(sc->volinfo.vol_msf);
   1043 	th->starting_track = bcd2bin(sc->volinfo.trk_low);
   1044 	th->ending_track = bcd2bin(sc->volinfo.trk_high);
   1045 
   1046 	return 0;
   1047 }
   1048 
   1049 int
   1050 mcd_read_toc(sc)
   1051 	struct mcd_softc *sc;
   1052 {
   1053 	struct ioc_toc_header th;
   1054 	struct mcd_qchninfo q;
   1055 	int rc, trk, idx, retry;
   1056 
   1057 	/* Only read TOC if needed. */
   1058 	if (sc->flags & MCDTOC)
   1059 		return 0;
   1060 
   1061 	if (sc->debug)
   1062 		printf("%s: read_toc: reading toc header\n",
   1063 		    sc->sc_dev.dv_xname);
   1064 	if (mcd_toc_header(sc, &th) != 0)
   1065 		return ENXIO;
   1066 
   1067 	if (sc->debug)
   1068 		printf("%s: read_toc: stopping play\n", sc->sc_dev.dv_xname);
   1069 	if ((rc = mcd_stop(sc)) != 0)
   1070 		return rc;
   1071 
   1072 	/* Try setting the mode twice. */
   1073 	if (mcd_setmode(sc, MCD_MD_TOC) != 0)
   1074 		return EIO;
   1075 	if (mcd_setmode(sc, MCD_MD_TOC) != 0)
   1076 		return EIO;
   1077 
   1078 	if (sc->debug)
   1079 		printf("%s: read_toc: reading qchannel info\n",
   1080 		    sc->sc_dev.dv_xname);
   1081 	for (trk = th.starting_track; trk <= th.ending_track; trk++)
   1082 		sc->toc[trk].idx_no = 0;
   1083 	trk = th.ending_track - th.starting_track + 1;
   1084 	for (retry = 300; retry && trk > 0; retry--) {
   1085 		if (mcd_getqchan(sc, &q) < 0)
   1086 			break;
   1087 		idx = bcd2bin(q.idx_no);
   1088 		if (idx > 0 && idx < MCD_MAXTOCS && q.trk_no == 0 &&
   1089 		    sc->toc[idx].idx_no == 0) {
   1090 			sc->toc[idx] = q;
   1091 			trk--;
   1092 		}
   1093 	}
   1094 
   1095 	if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
   1096 		return EIO;
   1097 
   1098 	if (trk != 0)
   1099 		return ENXIO;
   1100 
   1101 	/* Add a fake last+1. */
   1102 	idx = th.ending_track + 1;
   1103 	sc->toc[idx].ctrl_adr = sc->toc[idx-1].ctrl_adr;
   1104 	sc->toc[idx].trk_no = 0;
   1105 	sc->toc[idx].idx_no = 0xaa;
   1106 	sc->toc[idx].hd_pos_msf[0] = sc->volinfo.vol_msf[0];
   1107 	sc->toc[idx].hd_pos_msf[1] = sc->volinfo.vol_msf[1];
   1108 	sc->toc[idx].hd_pos_msf[2] = sc->volinfo.vol_msf[2];
   1109 
   1110 	sc->flags |= MCDTOC;
   1111 
   1112 	return 0;
   1113 }
   1114 
   1115 int
   1116 mcd_toc_entry(sc, te)
   1117 	struct mcd_softc *sc;
   1118 	struct ioc_read_toc_entry *te;
   1119 {
   1120 	struct ret_toc {
   1121 		struct ioc_toc_header th;
   1122 		struct cd_toc_entry rt;
   1123 	} ret_toc;
   1124 	struct ioc_toc_header th;
   1125 	int rc, i;
   1126 
   1127 	/* Make sure we have a valid TOC. */
   1128 	if ((rc = mcd_read_toc(sc)) != 0)
   1129 		return rc;
   1130 
   1131 	/* Find the TOC to copy. */
   1132 	i = te->starting_track;
   1133 	if (i == MCD_LASTPLUS1)
   1134 		i = bcd2bin(sc->volinfo.trk_high) + 1;
   1135 
   1136 	/* Verify starting track. */
   1137 	if (i < bcd2bin(sc->volinfo.trk_low) ||
   1138 	    i > bcd2bin(sc->volinfo.trk_high) + 1)
   1139 		return EINVAL;
   1140 
   1141 	/* Do we have room? */
   1142 	if (te->data_len < sizeof(struct ioc_toc_header) +
   1143 	    sizeof(struct cd_toc_entry))
   1144 		return EINVAL;
   1145 
   1146 	/* Copy the TOC header. */
   1147 	if (mcd_toc_header(sc, &th) < 0)
   1148 		return EIO;
   1149 	ret_toc.th = th;
   1150 
   1151 	/* Copy the TOC data. */
   1152 	ret_toc.rt.control = sc->toc[i].ctrl_adr;
   1153 	ret_toc.rt.addr_type = te->address_format;
   1154 	ret_toc.rt.track = i;
   1155 	if (te->address_format == CD_MSF_FORMAT) {
   1156 		ret_toc.rt.addr[1] = sc->toc[i].hd_pos_msf[0];
   1157 		ret_toc.rt.addr[2] = sc->toc[i].hd_pos_msf[1];
   1158 		ret_toc.rt.addr[3] = sc->toc[i].hd_pos_msf[2];
   1159 	}
   1160 
   1161 	/* Copy the data back. */
   1162 	copyout(&ret_toc, te->data,
   1163 	    sizeof(struct cd_toc_entry) + sizeof(struct ioc_toc_header));
   1164 
   1165 	return 0;
   1166 }
   1167 
   1168 int
   1169 mcd_stop(sc)
   1170 	struct mcd_softc *sc;
   1171 {
   1172 
   1173 	if (mcd_send(sc, MCD_CMDSTOPAUDIO, MCD_RETRIES) < 0)
   1174 		return ENXIO;
   1175 	sc->audio_status = CD_AS_PLAY_COMPLETED;
   1176 	return 0;
   1177 }
   1178 
   1179 int
   1180 mcd_getqchan(sc, q)
   1181 	struct mcd_softc *sc;
   1182 	struct mcd_qchninfo *q;
   1183 {
   1184 
   1185 	if (mcd_send(sc, MCD_CMDGETQCHN, MCD_RETRIES) < 0)
   1186 		return -1;
   1187 	if (mcd_get(sc, (char *) q, sizeof(struct mcd_qchninfo)) < 0)
   1188 		return -1;
   1189 	if (sc->debug)
   1190 		printf("%s: getqchan: ctl=%d t=%d i=%d ttm=%d:%d.%d dtm=%d:%d.%d\n",
   1191 		    sc->sc_dev.dv_xname, q->ctrl_adr, q->trk_no, q->idx_no,
   1192 		    q->trk_size_msf[0], q->trk_size_msf[1], q->trk_size_msf[2],
   1193 		    q->trk_size_msf[0], q->trk_size_msf[1], q->trk_size_msf[2]);
   1194 	return 0;
   1195 }
   1196 
   1197 int
   1198 mcd_subchan(sc, ch)
   1199 	struct mcd_softc *sc;
   1200 	struct ioc_read_subchannel *ch;
   1201 {
   1202 	struct mcd_qchninfo q;
   1203 	struct cd_sub_channel_info data;
   1204 
   1205 	if (sc->debug)
   1206 		printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname,
   1207 		    ch->address_format, ch->data_format);
   1208 
   1209 	if (ch->address_format != CD_MSF_FORMAT)
   1210 		return EIO;
   1211 	if (ch->data_format != CD_CURRENT_POSITION)
   1212 		return EIO;
   1213 	if (mcd_getqchan(sc, &q) < 0)
   1214 		return EIO;
   1215 
   1216 	data.header.audio_status = sc->audio_status;
   1217 	data.what.position.data_format = CD_MSF_FORMAT;
   1218 	data.what.position.track_number = bcd2bin(q.trk_no);
   1219 
   1220 	if (copyout(&data, ch->data, sizeof(struct cd_sub_channel_info)) != 0)
   1221 		return EFAULT;
   1222 	return 0;
   1223 }
   1224 
   1225 int
   1226 mcd_playtracks(sc, pt)
   1227 	struct mcd_softc *sc;
   1228 	struct ioc_play_track *pt;
   1229 {
   1230 	struct mcd_read2 pb;
   1231 	int a = pt->start_track;
   1232 	int z = pt->end_track;
   1233 	int rc;
   1234 
   1235 	if ((rc = mcd_read_toc(sc)) != 0)
   1236 		return rc;
   1237 
   1238 	printf("%s: playtracks: from %d:%d to %d:%d\n", sc->sc_dev.dv_xname,
   1239 	    a, pt->start_index, z, pt->end_index);
   1240 
   1241 	if (a < sc->volinfo.trk_low || a > sc->volinfo.trk_high || a > z ||
   1242 	    z < sc->volinfo.trk_low || z > sc->volinfo.trk_high)
   1243 		return EINVAL;
   1244 
   1245 	pb.start_msf[0] = sc->toc[a].hd_pos_msf[0];
   1246 	pb.start_msf[1] = sc->toc[a].hd_pos_msf[1];
   1247 	pb.start_msf[2] = sc->toc[a].hd_pos_msf[2];
   1248 	pb.end_msf[0] = sc->toc[z+1].hd_pos_msf[0];
   1249 	pb.end_msf[1] = sc->toc[z+1].hd_pos_msf[1];
   1250 	pb.end_msf[2] = sc->toc[z+1].hd_pos_msf[2];
   1251 
   1252 	return mcd_play(sc, &pb);
   1253 }
   1254 
   1255 int
   1256 mcd_play(sc, pb)
   1257 	struct mcd_softc *sc;
   1258 	struct mcd_read2 *pb;
   1259 {
   1260 	u_short iobase = sc->iobase;
   1261 	int retry, st;
   1262 
   1263 	sc->lastpb = *pb;
   1264 	for (retry = MCD_RETRIES; retry; retry--) {
   1265 		outb(iobase + mcd_command, MCD_CMDREAD2);
   1266 		outb(iobase + mcd_command, pb->start_msf[0]);
   1267 		outb(iobase + mcd_command, pb->start_msf[1]);
   1268 		outb(iobase + mcd_command, pb->start_msf[2]);
   1269 		outb(iobase + mcd_command, pb->end_msf[0]);
   1270 		outb(iobase + mcd_command, pb->end_msf[1]);
   1271 		outb(iobase + mcd_command, pb->end_msf[2]);
   1272 		if ((st = mcd_getstat(sc, 0)) != -1)
   1273 			break;
   1274 	}
   1275 	if (sc->debug)
   1276 		printf("%s: play: retry=%d status=%d\n", sc->sc_dev.dv_xname,
   1277 		    retry, st);
   1278 	if (!retry)
   1279 		return ENXIO;
   1280 
   1281 	sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
   1282 	return 0;
   1283 }
   1284 
   1285 int
   1286 mcd_pause(sc)
   1287 	struct mcd_softc *sc;
   1288 {
   1289 	struct mcd_qchninfo q;
   1290 	int rc;
   1291 
   1292 	/* Verify current status. */
   1293 	if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS)	{
   1294 		printf("%s: pause: attempted when not playing\n",
   1295 		    sc->sc_dev.dv_xname);
   1296 		return EINVAL;
   1297 	}
   1298 
   1299 	/* Get the current position. */
   1300 	if (mcd_getqchan(sc, &q) < 0)
   1301 		return EIO;
   1302 
   1303 	/* Copy it into lastpb. */
   1304 	sc->lastpb.start_msf[0] = q.hd_pos_msf[0];
   1305 	sc->lastpb.start_msf[1] = q.hd_pos_msf[1];
   1306 	sc->lastpb.start_msf[2] = q.hd_pos_msf[2];
   1307 
   1308 	/* Stop playing. */
   1309 	if ((rc = mcd_stop(sc)) != 0)
   1310 		return rc;
   1311 
   1312 	/* Set the proper status and exit. */
   1313 	sc->audio_status = CD_AS_PLAY_PAUSED;
   1314 	return 0;
   1315 }
   1316 
   1317 int
   1318 mcd_resume(sc)
   1319 	struct mcd_softc *sc;
   1320 {
   1321 
   1322 	if (sc->audio_status != CD_AS_PLAY_PAUSED)
   1323 		return EINVAL;
   1324 	return mcd_play(sc, &sc->lastpb);
   1325 }
   1326