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