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