Home | History | Annotate | Line # | Download | only in isa
mcd.c revision 1.10
      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.10 1994/04/08 18:22:26 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/isa.h>
     64 #include <i386/isa/isavar.h>
     65 #include <i386/isa/mcdreg.h>
     66 
     67 #ifndef MCDDEBUG
     68 #define MCD_TRACE(fmt,a,b,c,d)
     69 #else
     70 #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);}}
     71 #endif
     72 
     73 #define MCDPART(dev)	(((minor(dev)) & 0x07)     )
     74 #define MCDUNIT(dev)	(((minor(dev)) & 0x78) >> 3)
     75 #define MCDPHYS(dev)	(((minor(dev)) & 0x80) >> 7)
     76 #define RAW_PART	3
     77 
     78 /* flags */
     79 #define MCDOPEN		0x0001	/* device opened */
     80 #define MCDVALID	0x0002	/* parameters loaded */
     81 #define MCDWAIT		0x0004	/* waiting for something */
     82 #define MCDLABEL	0x0008	/* label is read */
     83 #define	MCDREADRAW	0x0010	/* read raw mode (2352 bytes) */
     84 #define	MCDVOLINFO	0x0020	/* already read volinfo */
     85 #define	MCDTOC		0x0040	/* already read toc */
     86 #define	MCDMBXBSY	0x0080	/* local mbx is busy */
     87 
     88 /* status */
     89 #define	MCDAUDIOBSY	MCD_ST_AUDIOBSY		/* playing audio */
     90 #define MCDDSKCHNG	MCD_ST_DSKCHNG		/* sensed change of disk */
     91 #define MCDDSKIN	MCD_ST_DSKIN		/* sensed disk in drive */
     92 #define MCDDOOROPEN	MCD_ST_DOOROPEN		/* sensed door open */
     93 
     94 /* toc */
     95 #define MCD_MAXTOCS	104	/* from the Linux driver */
     96 #define MCD_LASTPLUS1	170	/* special toc entry */
     97 
     98 struct mcd_mbx {
     99 	short		unit;
    100 	u_short		iobase;
    101 	short		retry;
    102 	short		nblk;
    103 	int		sz;
    104 	u_long		skip;
    105 	struct buf	*bp;
    106 	int		p_offset;
    107 	short		count;
    108 };
    109 
    110 struct mcd_softc {
    111 	struct	device sc_dev;
    112 	struct	intrhand sc_ih;
    113 
    114 	u_short	iobase;
    115 	short	config;
    116 	short	flags;
    117 	short	status;
    118 	int	blksize;
    119 	u_long	disksize;
    120 	struct	disklabel dlabel;
    121 	int	partflags[MAXPARTITIONS];
    122 	int	openflags;
    123 	struct	mcd_volinfo volinfo;
    124 	struct	mcd_qchninfo toc[MCD_MAXTOCS];
    125 	short	audio_status;
    126 	struct	mcd_read2 lastpb;
    127 	short	debug;
    128 	struct	buf head;	/* head of buf queue */
    129 	struct	mcd_mbx mbx;
    130 };
    131 
    132 /* prototypes */
    133 int mcdopen __P((dev_t));
    134 int mcdclose __P((dev_t));
    135 int mcd_start __P((struct mcd_softc *));
    136 int mcdioctl __P((dev_t, int, caddr_t, int, struct proc *));
    137 int mcd_getdisklabel __P((struct mcd_softc *));
    138 int mcdsize __P((dev_t));
    139 void mcd_configure __P((struct mcd_softc *));
    140 int mcd_waitrdy __P((u_short, int));
    141 int mcd_getreply __P((struct mcd_softc *, int));
    142 int mcd_getstat __P((struct mcd_softc *, int));
    143 void mcd_setflags __P((struct mcd_softc *));
    144 int mcd_get __P((struct mcd_softc *, char *, int));
    145 int mcd_send __P((struct mcd_softc *, int, int));
    146 int bcd2bin __P((bcd_t));
    147 bcd_t bin2bcd __P((int));
    148 void hsg2msf __P((int, bcd_t *));
    149 int msf2hsg __P((bcd_t *));
    150 int mcd_volinfo __P((struct mcd_softc *));
    151 int mcdintr __P((struct mcd_softc *));
    152 void mcd_doread __P((int, struct mcd_mbx *));
    153 int mcd_setmode __P((struct mcd_softc *, int));
    154 int mcd_toc_header __P((struct mcd_softc *, struct ioc_toc_header *));
    155 int mcd_read_toc __P((struct mcd_softc *));
    156 int mcd_toc_entry __P((struct mcd_softc *, struct ioc_read_toc_entry *));
    157 int mcd_stop __P((struct mcd_softc *));
    158 int mcd_getqchan __P((struct mcd_softc *, struct mcd_qchninfo *));
    159 int mcd_subchan __P((struct mcd_softc *, struct ioc_read_subchannel *));
    160 int mcd_playtracks __P((struct mcd_softc *, struct ioc_play_track *));
    161 int mcd_play __P((struct mcd_softc *, struct mcd_read2 *));
    162 int mcd_pause __P((struct mcd_softc *));
    163 int mcd_resume __P((struct mcd_softc *));
    164 
    165 int mcdprobe();
    166 void mcdattach();
    167 
    168 struct cfdriver mcdcd = {
    169 	NULL, "mcd", mcdprobe, mcdattach, DV_DISK, sizeof(struct mcd_softc)
    170 };
    171 
    172 #define mcd_put(port,byte)	outb(port,byte)
    173 
    174 #define MCD_RETRIES	5
    175 #define MCD_RDRETRIES	8
    176 
    177 #define MCDBLK	2048	/* for cooked mode */
    178 #define MCDRBLK	2352	/* for raw mode */
    179 
    180 /* several delays */
    181 #define RDELAY_WAITSTAT	300
    182 #define RDELAY_WAITMODE	300
    183 #define RDELAY_WAITREAD	800
    184 
    185 #define DELAY_STATUS	10000l		/* 10000 * 1us */
    186 #define DELAY_GETREPLY	200000l		/* 200000 * 2us */
    187 #define DELAY_SEEKREAD	20000l		/* 20000 * 1us */
    188 
    189 /* reader state machine */
    190 #define MCD_S_BEGIN	0
    191 #define MCD_S_BEGIN1	1
    192 #define MCD_S_WAITSTAT	2
    193 #define MCD_S_WAITMODE	3
    194 #define MCD_S_WAITREAD	4
    195 
    196 void
    197 mcdattach(parent, self, aux)
    198 	struct device *parent, *self;
    199 	void *aux;
    200 {
    201 	struct mcd_softc *sc = (void *)self;
    202 	struct isa_attach_args *ia = aux;
    203 
    204 #ifdef notyet
    205 	/* Wire controller for interrupts and DMA. */
    206 	mcd_configure(sc);
    207 #endif
    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.iobase = sc->iobase;
    385 	sc->mbx.retry = MCD_RETRIES;
    386 	sc->mbx.bp = bp;
    387 	sc->mbx.p_offset = p->p_offset;
    388 
    389 	/* Calling the read routine. */
    390 	mcd_doread(MCD_S_BEGIN, &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 struct mcd_mbx *mbxsave;
    840 
    841 void
    842 mcd_doread(state, mbxin)
    843 	int state;
    844 	struct mcd_mbx *mbxin;
    845 {
    846 	struct mcd_mbx *mbx = (state != MCD_S_BEGIN) ? mbxsave : mbxin;
    847 	struct mcd_softc *sc = mcdcd.cd_devs[mbx->unit];
    848 	u_short iobase = mbx->iobase;
    849 	struct buf *bp = mbx->bp;
    850 
    851 	int rm, i, k;
    852 	struct mcd_read2 rbuf;
    853 	int blkno;
    854 	caddr_t	addr;
    855 
    856 loop:
    857 	switch (state) {
    858 	case MCD_S_BEGIN:
    859 		mbx = mbxsave = mbxin;
    860 
    861 	case MCD_S_BEGIN1:
    862 		/* Get status. */
    863 		outb(iobase + mcd_command, MCD_CMDGETSTAT);
    864 		mbx->count = RDELAY_WAITSTAT;
    865 		timeout((timeout_t) mcd_doread, (caddr_t) MCD_S_WAITSTAT,
    866 		    hz/100);
    867 		return;
    868 
    869 	case MCD_S_WAITSTAT:
    870 		untimeout((timeout_t) mcd_doread, (caddr_t) MCD_S_WAITSTAT);
    871 		if (mbx->count-- >= 0) {
    872 			if (inb(iobase + mcd_xfer) & MCD_ST_BUSY) {
    873 				timeout((timeout_t) mcd_doread,
    874 				    (caddr_t) MCD_S_WAITSTAT, hz/100);
    875 				return;
    876 			}
    877 			mcd_setflags(sc);
    878 			MCD_TRACE("doread: got WAITSTAT delay=%d\n",
    879 			    RDELAY_WAITSTAT - mbx->count, 0, 0, 0);
    880 			/* Reject, if audio active. */
    881 			if (sc->status & MCDAUDIOBSY) {
    882 				printf("%s: audio is active\n",
    883 				    sc->sc_dev.dv_xname);
    884 				goto readerr;
    885 			}
    886 
    887 			/* Check for raw/cooked mode. */
    888 			if (sc->flags & MCDREADRAW) {
    889 				rm = MCD_MD_RAW;
    890 				mbx->sz = MCDRBLK;
    891 			} else {
    892 				rm = MCD_MD_COOKED;
    893 				mbx->sz = sc->blksize;
    894 			}
    895 
    896 			mbx->count = RDELAY_WAITMODE;
    897 
    898 			mcd_put(iobase + mcd_command, MCD_CMDSETMODE);
    899 			mcd_put(iobase + mcd_command, rm);
    900 			timeout((timeout_t) mcd_doread,
    901 			    (caddr_t) MCD_S_WAITMODE, hz/100);
    902 			return;
    903 		} else {
    904 			printf("%s: timeout getting status\n",
    905 			    sc->sc_dev.dv_xname);
    906 			goto readerr;
    907 		}
    908 
    909 	case MCD_S_WAITMODE:
    910 		untimeout((timeout_t) mcd_doread, (caddr_t) MCD_S_WAITMODE);
    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((timeout_t) mcd_doread,
    918 			    (caddr_t) MCD_S_WAITMODE, hz/100);
    919 			return;
    920 		}
    921 		mcd_setflags(sc);
    922 		MCD_TRACE("doread: got WAITMODE delay=%d\n",
    923 		    RDELAY_WAITMODE - mbx->count, 0, 0, 0);
    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 		mbx->count = RDELAY_WAITREAD;
    947 		timeout((timeout_t) mcd_doread, (caddr_t) MCD_S_WAITREAD,
    948 		    hz/100);
    949 		return;
    950 
    951 	case MCD_S_WAITREAD:
    952 		untimeout((timeout_t) mcd_doread, (caddr_t) MCD_S_WAITREAD);
    953 		if (mbx->count-- > 0) {
    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((timeout_t) mcd_doread,
    981 			    (caddr_t) MCD_S_WAITREAD, hz/100);
    982 			return;
    983 		} else {
    984 			printf("%s: timeout reading data\n",
    985 			    sc->sc_dev.dv_xname);
    986 			goto readerr;
    987 		}
    988 	}
    989 
    990 readerr:
    991 	if (mbx->retry-- > 0) {
    992 		printf("%s: retrying\n", sc->sc_dev.dv_xname);
    993 		state = MCD_S_BEGIN1;
    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