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