Home | History | Annotate | Line # | Download | only in isa
mcd.c revision 1.13
      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.13 1994/05/05 05:36:42 cgd 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 	u_short		iobase;
    100 	short		retry;
    101 	short		nblk;
    102 	int		sz;
    103 	u_long		skip;
    104 	struct buf	*bp;
    105 	int		p_offset;
    106 	short		count;
    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((int, struct mcd_mbx *));
    153 void mcd_doreadtimeout __P((void *arg));
    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 	printf("\n");
    210 
    211 	sc->flags = 0;
    212 
    213 	sc->sc_ih.ih_fun = mcdintr;
    214 	sc->sc_ih.ih_arg = sc;
    215 	sc->sc_ih.ih_level = IPL_BIO;
    216 	intr_establish(ia->ia_irq, &sc->sc_ih);
    217 }
    218 
    219 int
    220 mcdopen(dev)
    221 	dev_t dev;
    222 {
    223 	int unit, part, phys;
    224 	struct mcd_softc *sc;
    225 
    226 	unit = MCDUNIT(dev);
    227 	if (unit >= mcdcd.cd_ndevs)
    228 		return ENXIO;
    229 	sc = mcdcd.cd_devs[unit];
    230 	if (!sc)
    231 		return ENXIO;
    232 
    233 	part = MCDPART(dev);
    234 	phys = MCDPHYS(dev);
    235 
    236 	/* Invalidated in the meantime?  Mark all open part's invalid. */
    237 	if (!(sc->flags & MCDVALID) && sc->openflags)
    238 		return ENXIO;
    239 
    240 	if (mcd_getstat(sc, 1) < 0)
    241 		return ENXIO;
    242 
    243 	/* XXX Get a default disklabel. */
    244 	mcd_getdisklabel(sc);
    245 
    246 	if (mcdsize(dev) < 0) {
    247 		printf("%s: failed to get disk size\n", sc->sc_dev.dv_xname);
    248 		return ENXIO;
    249 	} else
    250 		sc->flags |= MCDVALID;
    251 
    252 	MCD_TRACE("open: partition=%d disksize=%d blksize=%d\n", part,
    253 	    sc->disksize, sc->blksize, 0);
    254 
    255 	if (part != RAW_PART &&
    256 	    (part >= sc->dlabel.d_npartitions ||
    257 	    sc->dlabel.d_partitions[part].p_fstype == FS_UNUSED))
    258 		return ENXIO;
    259 
    260 	sc->partflags[part] |= MCDOPEN;
    261 	sc->openflags |= (1 << part);
    262 	if (part == RAW_PART && phys != 0)
    263 		sc->partflags[part] |= MCDREADRAW;
    264 	return 0;
    265 }
    266 
    267 int
    268 mcdclose(dev)
    269 	dev_t dev;
    270 {
    271 	int unit, part;
    272 	struct mcd_softc *sc;
    273 
    274 	unit = MCDUNIT(dev);
    275 	part = MCDPART(dev);
    276 	sc = mcdcd.cd_devs[unit];
    277 
    278 	/* Get status. */
    279 	mcd_getstat(sc, 1);
    280 
    281 	/* Close channel. */
    282 	sc->partflags[part] &= ~(MCDOPEN | MCDREADRAW);
    283 	sc->openflags &= ~(1 << part);
    284 	MCD_TRACE("close: partition=%d\n", part, 0, 0, 0);
    285 
    286 	return 0;
    287 }
    288 
    289 void
    290 mcdstrategy(bp)
    291 	struct buf *bp;
    292 {
    293 	struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(bp->b_dev)];
    294 	struct buf *qp;
    295 	int s;
    296 
    297 	/* Test validity. */
    298 	MCD_TRACE("strategy: buf=0x%lx blkno=%ld bcount=%ld\n", bp,
    299 	    bp->b_blkno, bp->b_bcount, 0);
    300 	if (bp->b_blkno < 0) {
    301 		printf("%s: strategy: blkno=%d bcount=%d\n",
    302 		    sc->sc_dev.dv_xname, bp->b_blkno, bp->b_bcount);
    303 		bp->b_error = EINVAL;
    304 		bp->b_flags |= B_ERROR;
    305 		goto bad;
    306 	}
    307 
    308 	/* If device invalidated (e.g. media change, door open), error. */
    309 	if (!(sc->flags & MCDVALID)) {
    310 		MCD_TRACE("strategy: drive not valid\n", 0, 0, 0, 0);
    311 		bp->b_error = EIO;
    312 		goto bad;
    313 	}
    314 
    315 	/* Check for read only. */
    316 	if (!(bp->b_flags & B_READ)) {
    317 		bp->b_error = EROFS;
    318 		goto bad;
    319 	}
    320 
    321 	/* No data to read. */
    322 	if (bp->b_bcount == 0)
    323 		goto done;
    324 
    325 	/* For non raw access, check partition limits. */
    326 	if (MCDPART(bp->b_dev) != RAW_PART) {
    327 		if (!(sc->flags & MCDLABEL)) {
    328 			bp->b_error = EIO;
    329 			goto bad;
    330 		}
    331 		/* Adjust transfer if necessary. */
    332 		if (bounds_check_with_label(bp, &sc->dlabel, 1) <= 0)
    333 			goto done;
    334 	}
    335 
    336 	/* Queue it. */
    337 	qp = &sc->head;
    338 	s = splbio();
    339 	disksort(qp, bp);
    340 	splx(s);
    341 
    342 	/* Now check whether we can perform processing. */
    343 	mcd_start(sc);
    344 	return;
    345 
    346 bad:
    347 	bp->b_flags |= B_ERROR;
    348 done:
    349 	bp->b_resid = bp->b_bcount;
    350 	biodone(bp);
    351 	return;
    352 }
    353 
    354 int
    355 mcd_start(sc)
    356 	struct mcd_softc *sc;
    357 {
    358 	struct buf *bp, *qp = &sc->head;
    359 	struct partition *p;
    360 	int s = splbio();
    361 
    362 	if (sc->flags & MCDMBXBSY)
    363 		return;
    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.iobase = sc->iobase;
    387 	sc->mbx.retry = MCD_RETRIES;
    388 	sc->mbx.bp = bp;
    389 	sc->mbx.p_offset = p->p_offset;
    390 
    391 	/* Calling the read routine. */
    392 	mcd_doread(MCD_S_BEGIN, &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 CDIOCSETSTERIO:
    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 /***************************************************************
    517  * lower level of driver starts here
    518  **************************************************************/
    519 
    520 #ifdef notyet
    521 static char irqs[] = {
    522 	0x00, 0x00, 0x10, 0x20, 0x00, 0x30, 0x00, 0x00,
    523 	0x00, 0x10, 0x40, 0x50, 0x00, 0x00, 0x00, 0x00
    524 };
    525 
    526 static char drqs[] = {
    527 	0x00, 0x01, 0x00, 0x03, 0x00, 0x05, 0x06, 0x07
    528 };
    529 #endif
    530 
    531 void
    532 mcd_configure(sc)
    533 	struct mcd_softc *sc;
    534 {
    535 
    536 	outb(sc->iobase + mcd_config, sc->config);
    537 }
    538 
    539 int
    540 mcdprobe(parent, self, aux)
    541 	struct device *parent, *self;
    542 	void *aux;
    543 {
    544 	struct mcd_softc *sc = (void *)self;
    545 	struct isa_attach_args *ia = aux;
    546 	u_short iobase = ia->ia_iobase;
    547 	int i;
    548 	int st, check;
    549 
    550 #ifdef notyet
    551 	/* Get irq/drq configuration word. */
    552 	sc->config = irqs[ia->ia_irq];
    553 #endif
    554 	sc->iobase = iobase;
    555 
    556 	/* Send a reset. */
    557 	outb(iobase + mcd_reset, 0);
    558 	delay(300000);
    559 	/* Get any pending status and throw away. */
    560 	for (i = 10; i; i--)
    561 		inb(iobase + mcd_status);
    562 	delay(1000);
    563 
    564 	/* Send get status command. */
    565 	outb(iobase + mcd_command, MCD_CMDGETSTAT);
    566 	i = mcd_getreply(sc, DELAY_GETREPLY);
    567 
    568 	if (i < 0) {
    569 #ifdef DEBUG
    570 		printf("Mitsumi drive NOT detected\n");
    571 #endif
    572 		return 0;
    573 	}
    574 
    575 /*
    576  * The following code uses the 0xDC command, it returns a M from the
    577  * second byte and a number in the third.
    578  * (I hope you have the right drive for that, most drives don't do!)
    579  * Whole code entirely rewriten by veit (at) gmd.de, the changes accessed
    580  * the drive in an illegal way. Proper way is to use the timeout
    581  * driven routines mcd_getreply etc. rather than arbitrary delays.
    582  */
    583 
    584 	delay(2000);
    585 	outb(iobase + mcd_command, MCD_CMDCONTINFO);
    586 	i = mcd_getreply(sc, DELAY_GETREPLY);
    587 
    588 	if (i < 0) {
    589 #ifdef DEBUG
    590 		printf("Mitsumi drive error\n");
    591 #endif
    592 		return 0;
    593 	}
    594 	st = mcd_getreply(sc, DELAY_GETREPLY);
    595 	if (st < 0)
    596 		return 0;
    597 	check = mcd_getreply(sc, DELAY_GETREPLY);
    598 	if (check < 0)
    599 		return 0;
    600 	/* Flush junk. */
    601 	(void) mcd_getreply(sc, DELAY_GETREPLY);
    602 
    603 	/*
    604 	 * The following is code which is not guaranteed to work for all
    605 	 * drives, because the meaning of the expected 'M' is not clear
    606 	 * (M_itsumi is an obvious assumption, but I don't trust that).
    607 	 * Also, the original hack had a bogus condition that always
    608 	 * returned true.
    609 	 */
    610 #ifdef notdef
    611 	if (check != 'M') {
    612 #ifdef DEBUG
    613 		printf("Mitsumi drive NOT detected\n");
    614 #endif
    615 		return 0;
    616 	}
    617 #endif
    618 
    619 #ifdef DEBUG
    620 	printf("Mitsumi drive detected\n");
    621 #endif
    622 	ia->ia_iosize = 4;
    623 	ia->ia_msize = 0;
    624 	return 1;
    625 }
    626 
    627 int
    628 mcd_waitrdy(iobase, dly)
    629 	u_short iobase;
    630 	int dly;
    631 {
    632 	int i;
    633 
    634 	/* Wait until xfer port senses data ready. */
    635 	for (i = dly; i; i--) {
    636 		if ((inb(iobase + mcd_xfer) & MCD_ST_BUSY) == 0)
    637 			return 0;
    638 		delay(1);
    639 	}
    640 	return -1;
    641 }
    642 
    643 int
    644 mcd_getreply(sc, dly)
    645 	struct mcd_softc *sc;
    646 	int dly;
    647 {
    648 	u_short iobase = sc->iobase;
    649 
    650 	/* Wait data to become ready. */
    651 	if (mcd_waitrdy(iobase, dly) < 0) {
    652 		printf("%s: timeout in getreply\n", sc->sc_dev.dv_xname);
    653 		return -1;
    654 	}
    655 
    656 	/* Get the data. */
    657 	return inb(iobase + mcd_status);
    658 }
    659 
    660 int
    661 mcd_getstat(sc, sflg)
    662 	struct mcd_softc *sc;
    663 	int sflg;
    664 {
    665 	int i;
    666 	u_short iobase = sc->iobase;
    667 
    668 	/* Get the status. */
    669 	if (sflg)
    670 		outb(iobase + mcd_command, MCD_CMDGETSTAT);
    671 	i = mcd_getreply(sc, DELAY_GETREPLY);
    672 	if (i < 0) {
    673 		printf("%s: timeout in getstat\n", sc->sc_dev.dv_xname);
    674 		return -1;
    675 	}
    676 
    677 	sc->status = i;
    678 
    679 	mcd_setflags(sc);
    680 	return sc->status;
    681 }
    682 
    683 void
    684 mcd_setflags(sc)
    685 	struct mcd_softc *sc;
    686 {
    687 
    688 	/* Check flags. */
    689 	if (sc->status & (MCDDSKCHNG | MCDDOOROPEN)) {
    690 		MCD_TRACE("getstat: sensed DSKCHNG or DOOROPEN\n", 0, 0, 0, 0);
    691 		sc->flags &= ~MCDVALID;
    692 	}
    693 
    694 	if (sc->status & MCDAUDIOBSY)
    695 		sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
    696 	else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS)
    697 		sc->audio_status = CD_AS_PLAY_COMPLETED;
    698 }
    699 
    700 int
    701 mcd_get(sc, buf, nmax)
    702 	struct mcd_softc *sc;
    703 	char *buf;
    704 	int nmax;
    705 {
    706 	int i, k;
    707 
    708 	for (i = 0; i < nmax; i++) {
    709 		/* Wait for data. */
    710 		if ((k = mcd_getreply(sc, DELAY_GETREPLY)) < 0) {
    711 			printf("%s: timeout in get\n", sc->sc_dev.dv_xname);
    712 			return -1;
    713 		}
    714 		buf[i] = k;
    715 	}
    716 	return i;
    717 }
    718 
    719 int
    720 mcd_send(sc, cmd, nretries)
    721 	struct mcd_softc *sc;
    722 	int cmd, nretries;
    723 {
    724 	int i, k;
    725 	u_short iobase = sc->iobase;
    726 
    727 	MCD_TRACE("send: cmd=0x%x\n", cmd, 0, 0, 0);
    728 
    729 	for (i = nretries; i; i--) {
    730 		outb(iobase + mcd_command, cmd);
    731 		if ((k = mcd_getstat(sc, 0)) != -1)
    732 			break;
    733 	}
    734 	if (!i) {
    735 		printf("%s: send: retry count exceeded\n", sc->sc_dev.dv_xname);
    736 		return -1;
    737 	}
    738 
    739 	MCD_TRACE("send: status=0x%x\n", k, 0, 0, 0);
    740 
    741 	return 0;
    742 }
    743 
    744 int
    745 bcd2bin(b)
    746 	bcd_t b;
    747 {
    748 
    749 	return (b >> 4) * 10 + (b & 15);
    750 }
    751 
    752 bcd_t
    753 bin2bcd(b)
    754 	int b;
    755 {
    756 
    757 	return ((b / 10) << 4) | (b % 10);
    758 }
    759 
    760 void
    761 hsg2msf(hsg, msf)
    762 	int hsg;
    763 	bcd_t *msf;
    764 {
    765 
    766 	hsg += 150;
    767 	M_msf(msf) = bin2bcd(hsg / 4500);
    768 	hsg %= 4500;
    769 	S_msf(msf) = bin2bcd(hsg / 75);
    770 	F_msf(msf) = bin2bcd(hsg % 75);
    771 }
    772 
    773 int
    774 msf2hsg(msf)
    775 	bcd_t *msf;
    776 {
    777 
    778 	return (bcd2bin(M_msf(msf)) * 60 +
    779 		bcd2bin(S_msf(msf))) * 75 +
    780 		bcd2bin(F_msf(msf)) - 150;
    781 }
    782 
    783 int
    784 mcd_volinfo(sc)
    785 	struct mcd_softc *sc;
    786 {
    787 
    788 	MCD_TRACE("volinfo: enter\n", 0, 0, 0, 0);
    789 
    790 	/* Get the status, in case the disc has been changed. */
    791 	if (mcd_getstat(sc, 1) < 0)
    792 		return EIO;
    793 
    794 	/* Just return if we already have it. */
    795 	if (sc->flags & MCDVOLINFO)
    796 		return 0;
    797 
    798 	/* Send volume info command. */
    799 	if (mcd_send(sc, MCD_CMDGETVOLINFO, MCD_RETRIES) < 0)
    800 		return -1;
    801 
    802 	/* Get the data. */
    803 	if (mcd_get(sc, (char*) &sc->volinfo, sizeof(struct mcd_volinfo)) < 0) {
    804 		printf("%s: volinfo: error reading data\n",
    805 		    sc->sc_dev.dv_xname);
    806 		return -1;
    807 	}
    808 
    809 	if (sc->volinfo.trk_low != 0 || sc->volinfo.trk_high != 0) {
    810 		/* Volinfo is OK. */
    811 		sc->flags |= MCDVOLINFO;
    812 		return 0;
    813 	}
    814 
    815 	return -1;
    816 }
    817 
    818 int
    819 mcdintr(sc)
    820 	struct mcd_softc *sc;
    821 {
    822 	u_short iobase = sc->iobase;
    823 
    824 	MCD_TRACE("stray interrupt xfer=0x%x\n", inb(iobase + mcd_xfer),
    825 	    0, 0, 0);
    826 
    827 	/* Just read out status and ignore the rest. */
    828 	if (inb(iobase + mcd_xfer) != 0xff)
    829 		(void) inb(iobase + mcd_status);
    830 
    831 	return -1;
    832 }
    833 
    834 /*
    835  * State machine to process read requests.
    836  * Initialize with MCD_S_BEGIN: calculate sizes, and read status
    837  * MCD_S_WAITSTAT: wait for status reply, set mode
    838  * MCD_S_WAITMODE: waits for status reply from set mode, set read command
    839  * MCD_S_WAITREAD: wait for read ready, read data.
    840  */
    841 struct mcd_mbx *mbxsave;
    842 
    843 void
    844 mcd_doreadtimeout(arg)
    845 	void *arg;
    846 {
    847 	int state = (long)arg;
    848 
    849 	mcd_doread(state, NULL);
    850 }
    851 
    852 void
    853 mcd_doread(state, mbxin)
    854 	int state;
    855 	struct mcd_mbx *mbxin;
    856 {
    857 	struct mcd_mbx *mbx = (state != MCD_S_BEGIN) ? mbxsave : mbxin;
    858 	struct mcd_softc *sc = mcdcd.cd_devs[mbx->unit];
    859 	u_short iobase = mbx->iobase;
    860 	struct buf *bp = mbx->bp;
    861 
    862 	int rm, i, k;
    863 	struct mcd_read2 rbuf;
    864 	int blkno;
    865 	caddr_t	addr;
    866 
    867 loop:
    868 	switch (state) {
    869 	case MCD_S_BEGIN:
    870 		mbx = mbxsave = mbxin;
    871 
    872 	case MCD_S_BEGIN1:
    873 		/* Get status. */
    874 		outb(iobase + mcd_command, MCD_CMDGETSTAT);
    875 		mbx->count = RDELAY_WAITSTAT;
    876 		timeout(mcd_doreadtimeout, (caddr_t)MCD_S_WAITSTAT,
    877 		    hz/100);
    878 		return;
    879 
    880 	case MCD_S_WAITSTAT:
    881 		untimeout(mcd_doreadtimeout, (caddr_t)MCD_S_WAITSTAT);
    882 		if (mbx->count-- >= 0) {
    883 			if (inb(iobase + mcd_xfer) & MCD_ST_BUSY) {
    884 				timeout(mcd_doreadtimeout,
    885 				    (caddr_t)MCD_S_WAITSTAT, hz/100);
    886 				return;
    887 			}
    888 			mcd_setflags(sc);
    889 			MCD_TRACE("doread: got WAITSTAT delay=%d\n",
    890 			    RDELAY_WAITSTAT - mbx->count, 0, 0, 0);
    891 			/* Reject, if audio active. */
    892 			if (sc->status & MCDAUDIOBSY) {
    893 				printf("%s: audio is active\n",
    894 				    sc->sc_dev.dv_xname);
    895 				goto readerr;
    896 			}
    897 
    898 			/* Check for raw/cooked mode. */
    899 			if (sc->flags & MCDREADRAW) {
    900 				rm = MCD_MD_RAW;
    901 				mbx->sz = MCDRBLK;
    902 			} else {
    903 				rm = MCD_MD_COOKED;
    904 				mbx->sz = sc->blksize;
    905 			}
    906 
    907 			mbx->count = RDELAY_WAITMODE;
    908 
    909 			mcd_put(iobase + mcd_command, MCD_CMDSETMODE);
    910 			mcd_put(iobase + mcd_command, rm);
    911 			timeout(mcd_doreadtimeout, (caddr_t)MCD_S_WAITMODE,
    912 			    hz/100);
    913 			return;
    914 		} else {
    915 			printf("%s: timeout getting status\n",
    916 			    sc->sc_dev.dv_xname);
    917 			goto readerr;
    918 		}
    919 
    920 	case MCD_S_WAITMODE:
    921 		untimeout(mcd_doreadtimeout, (caddr_t)MCD_S_WAITMODE);
    922 		if (mbx->count-- < 0) {
    923 			printf("%s: timeout setting mode\n",
    924 			    sc->sc_dev.dv_xname);
    925 			goto readerr;
    926 		}
    927 		if (inb(iobase + mcd_xfer) & MCD_ST_BUSY) {
    928 			timeout(mcd_doreadtimeout, (caddr_t)MCD_S_WAITMODE,
    929 			    hz/100);
    930 			return;
    931 		}
    932 		mcd_setflags(sc);
    933 		MCD_TRACE("doread: got WAITMODE delay=%d\n",
    934 		    RDELAY_WAITMODE - mbx->count, 0, 0, 0);
    935 		/* For first block. */
    936 		mbx->nblk = (bp->b_bcount + (mbx->sz - 1)) / mbx->sz;
    937 		mbx->skip = 0;
    938 
    939 nextblock:
    940 		blkno = (bp->b_blkno / (mbx->sz / DEV_BSIZE)) + mbx->p_offset +
    941 		    (mbx->skip / mbx->sz);
    942 
    943 		MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", blkno, bp, 0,
    944 		    0);
    945 
    946 		/* Build parameter block. */
    947 		hsg2msf(blkno, rbuf.start_msf);
    948 
    949 		/* Send the read command. */
    950 		mcd_put(iobase + mcd_command, MCD_CMDREAD2);
    951 		mcd_put(iobase + mcd_command, rbuf.start_msf[0]);
    952 		mcd_put(iobase + mcd_command, rbuf.start_msf[1]);
    953 		mcd_put(iobase + mcd_command, rbuf.start_msf[2]);
    954 		mcd_put(iobase + mcd_command, 0);
    955 		mcd_put(iobase + mcd_command, 0);
    956 		mcd_put(iobase + mcd_command, 1);
    957 		mbx->count = RDELAY_WAITREAD;
    958 		timeout(mcd_doreadtimeout, (caddr_t)MCD_S_WAITREAD, hz/100);
    959 		return;
    960 
    961 	case MCD_S_WAITREAD:
    962 		untimeout(mcd_doreadtimeout, (caddr_t)MCD_S_WAITREAD);
    963 		if (mbx->count-- > 0) {
    964 			k = inb(iobase + mcd_xfer);
    965 			if ((k & 2) == 0) {	/* XXX MCD_ST_AUDIOBSY? */
    966 				MCD_TRACE("doread: got data delay=%d\n",
    967 				    RDELAY_WAITREAD - mbx->count, 0, 0, 0);
    968 				/* Data is ready. */
    969 				addr = bp->b_un.b_addr + mbx->skip;
    970 				outb(iobase + mcd_ctl2, 0x04);	/* XXX */
    971 				for (i = 0; i < mbx->sz; i++)
    972 					*addr++	= inb(iobase + mcd_rdata);
    973 				outb(iobase + mcd_ctl2, 0x0c);	/* XXX */
    974 
    975 				if (--mbx->nblk > 0) {
    976 					mbx->skip += mbx->sz;
    977 					goto nextblock;
    978 				}
    979 
    980 				/* Return buffer. */
    981 				bp->b_resid = 0;
    982 				biodone(bp);
    983 
    984 				sc->flags &= ~MCDMBXBSY;
    985 				mcd_start(sc);
    986 				return;
    987 			}
    988 			if ((k & MCD_ST_BUSY) == 0)
    989 				mcd_getstat(sc, 0);
    990 			timeout(mcd_doreadtimeout, (caddr_t)MCD_S_WAITREAD,
    991 			    hz/100);
    992 			return;
    993 		} else {
    994 			printf("%s: timeout reading data\n",
    995 			    sc->sc_dev.dv_xname);
    996 			goto readerr;
    997 		}
    998 	}
    999 
   1000 readerr:
   1001 	if (mbx->retry-- > 0) {
   1002 		printf("%s: retrying\n", sc->sc_dev.dv_xname);
   1003 		state = MCD_S_BEGIN1;
   1004 		goto loop;
   1005 	}
   1006 
   1007 	/* Invalidate the buffer. */
   1008 	bp->b_flags |= B_ERROR;
   1009 	bp->b_resid = bp->b_bcount;
   1010 	biodone(bp);
   1011 	mcd_start(sc);
   1012 
   1013 #ifdef notyet
   1014 	printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname);
   1015 	outb(mbx->iobase + mcd_reset, MCD_CMDRESET);
   1016 	delay(300000);
   1017 	(void)mcd_getstat(sc, 1);
   1018 	(void)mcd_getstat(sc, 1);
   1019 	/*sc->status &= ~MCDDSKCHNG; */
   1020 	sc->debug = 1; /* preventive set debug mode */
   1021 #endif
   1022 }
   1023 
   1024 int
   1025 mcd_setmode(sc, mode)
   1026 	struct mcd_softc *sc;
   1027 	int mode;
   1028 {
   1029 	u_short iobase = sc->iobase;
   1030 	int retry;
   1031 
   1032 	printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode);
   1033 	for (retry = MCD_RETRIES; retry; retry--) {
   1034 		outb(iobase + mcd_command, MCD_CMDSETMODE);
   1035 		outb(iobase + mcd_command, mode);
   1036 		if (mcd_getstat(sc, 0) != -1)
   1037 			return 0;
   1038 	}
   1039 
   1040 	return -1;
   1041 }
   1042 
   1043 int
   1044 mcd_toc_header(sc, th)
   1045 	struct mcd_softc *sc;
   1046 	struct ioc_toc_header *th;
   1047 {
   1048 
   1049 	if (mcd_volinfo(sc) < 0)
   1050 		return ENXIO;
   1051 
   1052 	th->len = msf2hsg(sc->volinfo.vol_msf);
   1053 	th->starting_track = bcd2bin(sc->volinfo.trk_low);
   1054 	th->ending_track = bcd2bin(sc->volinfo.trk_high);
   1055 
   1056 	return 0;
   1057 }
   1058 
   1059 int
   1060 mcd_read_toc(sc)
   1061 	struct mcd_softc *sc;
   1062 {
   1063 	struct ioc_toc_header th;
   1064 	struct mcd_qchninfo q;
   1065 	int rc, trk, idx, retry;
   1066 
   1067 	/* Only read TOC if needed. */
   1068 	if (sc->flags & MCDTOC)
   1069 		return 0;
   1070 
   1071 	if (sc->debug)
   1072 		printf("%s: read_toc: reading toc header\n",
   1073 		    sc->sc_dev.dv_xname);
   1074 	if (mcd_toc_header(sc, &th) != 0)
   1075 		return ENXIO;
   1076 
   1077 	if (sc->debug)
   1078 		printf("%s: read_toc: stopping play\n", sc->sc_dev.dv_xname);
   1079 	if ((rc = mcd_stop(sc)) != 0)
   1080 		return rc;
   1081 
   1082 	/* Try setting the mode twice. */
   1083 	if (mcd_setmode(sc, MCD_MD_TOC) != 0)
   1084 		return EIO;
   1085 	if (mcd_setmode(sc, MCD_MD_TOC) != 0)
   1086 		return EIO;
   1087 
   1088 	if (sc->debug)
   1089 		printf("%s: read_toc: reading qchannel info\n",
   1090 		    sc->sc_dev.dv_xname);
   1091 	for (trk = th.starting_track; trk <= th.ending_track; trk++)
   1092 		sc->toc[trk].idx_no = 0;
   1093 	trk = th.ending_track - th.starting_track + 1;
   1094 	for (retry = 300; retry && trk > 0; retry--) {
   1095 		if (mcd_getqchan(sc, &q) < 0)
   1096 			break;
   1097 		idx = bcd2bin(q.idx_no);
   1098 		if (idx > 0 && idx < MCD_MAXTOCS && q.trk_no == 0 &&
   1099 		    sc->toc[idx].idx_no == 0) {
   1100 			sc->toc[idx] = q;
   1101 			trk--;
   1102 		}
   1103 	}
   1104 
   1105 	if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
   1106 		return EIO;
   1107 
   1108 	if (trk != 0)
   1109 		return ENXIO;
   1110 
   1111 	/* Add a fake last+1. */
   1112 	idx = th.ending_track + 1;
   1113 	sc->toc[idx].ctrl_adr = sc->toc[idx-1].ctrl_adr;
   1114 	sc->toc[idx].trk_no = 0;
   1115 	sc->toc[idx].idx_no = 0xaa;
   1116 	sc->toc[idx].hd_pos_msf[0] = sc->volinfo.vol_msf[0];
   1117 	sc->toc[idx].hd_pos_msf[1] = sc->volinfo.vol_msf[1];
   1118 	sc->toc[idx].hd_pos_msf[2] = sc->volinfo.vol_msf[2];
   1119 
   1120 	sc->flags |= MCDTOC;
   1121 
   1122 	return 0;
   1123 }
   1124 
   1125 int
   1126 mcd_toc_entry(sc, te)
   1127 	struct mcd_softc *sc;
   1128 	struct ioc_read_toc_entry *te;
   1129 {
   1130 	struct ret_toc {
   1131 		struct ioc_toc_header th;
   1132 		struct cd_toc_entry rt;
   1133 	} ret_toc;
   1134 	struct ioc_toc_header th;
   1135 	int rc, i;
   1136 
   1137 	/* Make sure we have a valid TOC. */
   1138 	if ((rc = mcd_read_toc(sc)) != 0)
   1139 		return rc;
   1140 
   1141 	/* Find the TOC to copy. */
   1142 	i = te->starting_track;
   1143 	if (i == MCD_LASTPLUS1)
   1144 		i = bcd2bin(sc->volinfo.trk_high) + 1;
   1145 
   1146 	/* Verify starting track. */
   1147 	if (i < bcd2bin(sc->volinfo.trk_low) ||
   1148 	    i > bcd2bin(sc->volinfo.trk_high) + 1)
   1149 		return EINVAL;
   1150 
   1151 	/* Do we have room? */
   1152 	if (te->data_len < sizeof(struct ioc_toc_header) +
   1153 	    sizeof(struct cd_toc_entry))
   1154 		return EINVAL;
   1155 
   1156 	/* Copy the TOC header. */
   1157 	if (mcd_toc_header(sc, &th) < 0)
   1158 		return EIO;
   1159 	ret_toc.th = th;
   1160 
   1161 	/* Copy the TOC data. */
   1162 	ret_toc.rt.control = sc->toc[i].ctrl_adr;
   1163 	ret_toc.rt.addr_type = te->address_format;
   1164 	ret_toc.rt.track = i;
   1165 	if (te->address_format == CD_MSF_FORMAT) {
   1166 		ret_toc.rt.addr[1] = sc->toc[i].hd_pos_msf[0];
   1167 		ret_toc.rt.addr[2] = sc->toc[i].hd_pos_msf[1];
   1168 		ret_toc.rt.addr[3] = sc->toc[i].hd_pos_msf[2];
   1169 	}
   1170 
   1171 	/* Copy the data back. */
   1172 	copyout(&ret_toc, te->data,
   1173 	    sizeof(struct cd_toc_entry) + sizeof(struct ioc_toc_header));
   1174 
   1175 	return 0;
   1176 }
   1177 
   1178 int
   1179 mcd_stop(sc)
   1180 	struct mcd_softc *sc;
   1181 {
   1182 
   1183 	if (mcd_send(sc, MCD_CMDSTOPAUDIO, MCD_RETRIES) < 0)
   1184 		return ENXIO;
   1185 	sc->audio_status = CD_AS_PLAY_COMPLETED;
   1186 	return 0;
   1187 }
   1188 
   1189 int
   1190 mcd_getqchan(sc, q)
   1191 	struct mcd_softc *sc;
   1192 	struct mcd_qchninfo *q;
   1193 {
   1194 
   1195 	if (mcd_send(sc, MCD_CMDGETQCHN, MCD_RETRIES) < 0)
   1196 		return -1;
   1197 	if (mcd_get(sc, (char *) q, sizeof(struct mcd_qchninfo)) < 0)
   1198 		return -1;
   1199 	if (sc->debug)
   1200 		printf("%s: getqchan: ctl=%d t=%d i=%d ttm=%d:%d.%d dtm=%d:%d.%d\n",
   1201 		    sc->sc_dev.dv_xname, q->ctrl_adr, q->trk_no, q->idx_no,
   1202 		    q->trk_size_msf[0], q->trk_size_msf[1], q->trk_size_msf[2],
   1203 		    q->trk_size_msf[0], q->trk_size_msf[1], q->trk_size_msf[2]);
   1204 	return 0;
   1205 }
   1206 
   1207 int
   1208 mcd_subchan(sc, ch)
   1209 	struct mcd_softc *sc;
   1210 	struct ioc_read_subchannel *ch;
   1211 {
   1212 	struct mcd_qchninfo q;
   1213 	struct cd_sub_channel_info data;
   1214 
   1215 	if (sc->debug)
   1216 		printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname,
   1217 		    ch->address_format, ch->data_format);
   1218 
   1219 	if (ch->address_format != CD_MSF_FORMAT)
   1220 		return EIO;
   1221 	if (ch->data_format != CD_CURRENT_POSITION)
   1222 		return EIO;
   1223 	if (mcd_getqchan(sc, &q) < 0)
   1224 		return EIO;
   1225 
   1226 	data.header.audio_status = sc->audio_status;
   1227 	data.what.position.data_format = CD_MSF_FORMAT;
   1228 	data.what.position.track_number = bcd2bin(q.trk_no);
   1229 
   1230 	if (copyout(&data, ch->data, sizeof(struct cd_sub_channel_info)) != 0)
   1231 		return EFAULT;
   1232 	return 0;
   1233 }
   1234 
   1235 int
   1236 mcd_playtracks(sc, pt)
   1237 	struct mcd_softc *sc;
   1238 	struct ioc_play_track *pt;
   1239 {
   1240 	struct mcd_read2 pb;
   1241 	int a = pt->start_track;
   1242 	int z = pt->end_track;
   1243 	int rc;
   1244 
   1245 	if ((rc = mcd_read_toc(sc)) != 0)
   1246 		return rc;
   1247 
   1248 	printf("%s: playtracks: from %d:%d to %d:%d\n", sc->sc_dev.dv_xname,
   1249 	    a, pt->start_index, z, pt->end_index);
   1250 
   1251 	if (a < sc->volinfo.trk_low || a > sc->volinfo.trk_high || a > z ||
   1252 	    z < sc->volinfo.trk_low || z > sc->volinfo.trk_high)
   1253 		return EINVAL;
   1254 
   1255 	pb.start_msf[0] = sc->toc[a].hd_pos_msf[0];
   1256 	pb.start_msf[1] = sc->toc[a].hd_pos_msf[1];
   1257 	pb.start_msf[2] = sc->toc[a].hd_pos_msf[2];
   1258 	pb.end_msf[0] = sc->toc[z+1].hd_pos_msf[0];
   1259 	pb.end_msf[1] = sc->toc[z+1].hd_pos_msf[1];
   1260 	pb.end_msf[2] = sc->toc[z+1].hd_pos_msf[2];
   1261 
   1262 	return mcd_play(sc, &pb);
   1263 }
   1264 
   1265 int
   1266 mcd_play(sc, pb)
   1267 	struct mcd_softc *sc;
   1268 	struct mcd_read2 *pb;
   1269 {
   1270 	u_short iobase = sc->iobase;
   1271 	int retry, st;
   1272 
   1273 	sc->lastpb = *pb;
   1274 	for (retry = MCD_RETRIES; retry; retry--) {
   1275 		outb(iobase + mcd_command, MCD_CMDREAD2);
   1276 		outb(iobase + mcd_command, pb->start_msf[0]);
   1277 		outb(iobase + mcd_command, pb->start_msf[1]);
   1278 		outb(iobase + mcd_command, pb->start_msf[2]);
   1279 		outb(iobase + mcd_command, pb->end_msf[0]);
   1280 		outb(iobase + mcd_command, pb->end_msf[1]);
   1281 		outb(iobase + mcd_command, pb->end_msf[2]);
   1282 		if ((st = mcd_getstat(sc, 0)) != -1)
   1283 			break;
   1284 	}
   1285 	if (sc->debug)
   1286 		printf("%s: play: retry=%d status=%d\n", sc->sc_dev.dv_xname,
   1287 		    retry, st);
   1288 	if (!retry)
   1289 		return ENXIO;
   1290 
   1291 	sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
   1292 	return 0;
   1293 }
   1294 
   1295 int
   1296 mcd_pause(sc)
   1297 	struct mcd_softc *sc;
   1298 {
   1299 	struct mcd_qchninfo q;
   1300 	int rc;
   1301 
   1302 	/* Verify current status. */
   1303 	if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS)	{
   1304 		printf("%s: pause: attempted when not playing\n",
   1305 		    sc->sc_dev.dv_xname);
   1306 		return EINVAL;
   1307 	}
   1308 
   1309 	/* Get the current position. */
   1310 	if (mcd_getqchan(sc, &q) < 0)
   1311 		return EIO;
   1312 
   1313 	/* Copy it into lastpb. */
   1314 	sc->lastpb.start_msf[0] = q.hd_pos_msf[0];
   1315 	sc->lastpb.start_msf[1] = q.hd_pos_msf[1];
   1316 	sc->lastpb.start_msf[2] = q.hd_pos_msf[2];
   1317 
   1318 	/* Stop playing. */
   1319 	if ((rc = mcd_stop(sc)) != 0)
   1320 		return rc;
   1321 
   1322 	/* Set the proper status and exit. */
   1323 	sc->audio_status = CD_AS_PLAY_PAUSED;
   1324 	return 0;
   1325 }
   1326 
   1327 int
   1328 mcd_resume(sc)
   1329 	struct mcd_softc *sc;
   1330 {
   1331 
   1332 	if (sc->audio_status != CD_AS_PLAY_PAUSED)
   1333 		return EINVAL;
   1334 	return mcd_play(sc, &sc->lastpb);
   1335 }
   1336