Home | History | Annotate | Line # | Download | only in isa
mcd.c revision 1.49
      1  1.49   mycroft /*	$NetBSD: mcd.c,v 1.49 1996/05/12 23:53:11 mycroft Exp $	*/
      2  1.20       cgd 
      3   1.1       cgd /*
      4  1.28   mycroft  * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.  All rights reserved.
      5  1.28   mycroft  *
      6  1.28   mycroft  * Redistribution and use in source and binary forms, with or without
      7  1.28   mycroft  * modification, are permitted provided that the following conditions
      8  1.28   mycroft  * are met:
      9  1.28   mycroft  * 1. Redistributions of source code must retain the above copyright
     10  1.28   mycroft  *    notice, this list of conditions and the following disclaimer.
     11  1.28   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.28   mycroft  *    notice, this list of conditions and the following disclaimer in the
     13  1.28   mycroft  *    documentation and/or other materials provided with the distribution.
     14  1.28   mycroft  * 3. All advertising materials mentioning features or use of this software
     15  1.28   mycroft  *    must display the following acknowledgement:
     16  1.28   mycroft  *	This product includes software developed by Charles M. Hannum.
     17  1.28   mycroft  * 4. The name of the author may not be used to endorse or promote products
     18  1.28   mycroft  *    derived from this software without specific prior written permission.
     19  1.28   mycroft  *
     20   1.1       cgd  * Copyright 1993 by Holger Veit (data part)
     21   1.1       cgd  * Copyright 1993 by Brian Moore (audio part)
     22   1.1       cgd  * All rights reserved.
     23   1.1       cgd  *
     24   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     25   1.1       cgd  * modification, are permitted provided that the following conditions
     26   1.1       cgd  * are met:
     27   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     28   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     29   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     30   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     31   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     32   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     33   1.1       cgd  *    must display the following acknowledgement:
     34   1.1       cgd  *	This software was developed by Holger Veit and Brian Moore
     35   1.1       cgd  *      for use with "386BSD" and similar operating systems.
     36   1.1       cgd  *    "Similar operating systems" includes mainly non-profit oriented
     37   1.1       cgd  *    systems for research and education, including but not restricted to
     38   1.1       cgd  *    "NetBSD", "FreeBSD", "Mach" (by CMU).
     39   1.1       cgd  * 4. Neither the name of the developer(s) nor the name "386BSD"
     40   1.1       cgd  *    may be used to endorse or promote products derived from this
     41   1.1       cgd  *    software without specific prior written permission.
     42   1.1       cgd  *
     43   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY
     44   1.1       cgd  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     45   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     46   1.1       cgd  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER(S) BE
     47   1.1       cgd  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     48   1.1       cgd  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
     49   1.1       cgd  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     50   1.1       cgd  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     51   1.1       cgd  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     52   1.1       cgd  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     53   1.1       cgd  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     54   1.1       cgd  */
     55   1.2       cgd 
     56   1.6   mycroft /*static char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";*/
     57   1.1       cgd 
     58   1.3   mycroft #include <sys/types.h>
     59   1.3   mycroft #include <sys/param.h>
     60   1.3   mycroft #include <sys/systm.h>
     61   1.6   mycroft #include <sys/kernel.h>
     62   1.6   mycroft #include <sys/proc.h>
     63   1.3   mycroft #include <sys/conf.h>
     64   1.3   mycroft #include <sys/file.h>
     65   1.3   mycroft #include <sys/buf.h>
     66   1.3   mycroft #include <sys/stat.h>
     67   1.3   mycroft #include <sys/uio.h>
     68   1.3   mycroft #include <sys/ioctl.h>
     69   1.3   mycroft #include <sys/cdio.h>
     70   1.3   mycroft #include <sys/errno.h>
     71   1.3   mycroft #include <sys/disklabel.h>
     72   1.6   mycroft #include <sys/device.h>
     73  1.21   mycroft #include <sys/disk.h>
     74   1.3   mycroft 
     75   1.6   mycroft #include <machine/cpu.h>
     76  1.49   mycroft #include <machine/intr.h>
     77   1.3   mycroft #include <machine/pio.h>
     78   1.3   mycroft 
     79  1.37       cgd #include <dev/isa/isavar.h>
     80  1.27       cgd #include <dev/isa/mcdreg.h>
     81   1.1       cgd 
     82   1.6   mycroft #ifndef MCDDEBUG
     83   1.1       cgd #define MCD_TRACE(fmt,a,b,c,d)
     84   1.1       cgd #else
     85   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);}}
     86   1.1       cgd #endif
     87   1.1       cgd 
     88  1.21   mycroft #define	MCDPART(dev)	DISKPART(dev)
     89  1.21   mycroft #define	MCDUNIT(dev)	DISKUNIT(dev)
     90   1.1       cgd 
     91   1.1       cgd /* toc */
     92   1.1       cgd #define MCD_MAXTOCS	104	/* from the Linux driver */
     93   1.1       cgd 
     94   1.1       cgd struct mcd_mbx {
     95  1.28   mycroft 	int		retry, count;
     96  1.28   mycroft 	struct buf	*bp;
     97  1.28   mycroft 	daddr_t		blkno;
     98  1.28   mycroft 	int		nblk;
     99   1.1       cgd 	int		sz;
    100   1.1       cgd 	u_long		skip;
    101  1.28   mycroft 	int		state;
    102  1.28   mycroft #define	MCD_S_IDLE	0
    103  1.28   mycroft #define MCD_S_BEGIN	1
    104  1.30   mycroft #define MCD_S_WAITMODE	2
    105  1.30   mycroft #define MCD_S_WAITREAD	3
    106  1.28   mycroft 	int		mode;
    107   1.1       cgd };
    108   1.1       cgd 
    109   1.6   mycroft struct mcd_softc {
    110   1.6   mycroft 	struct	device sc_dev;
    111  1.44   thorpej 	struct	disk sc_dk;
    112  1.37       cgd 	void *sc_ih;
    113   1.6   mycroft 
    114  1.24   mycroft 	int	iobase;
    115  1.28   mycroft 	int	irq, drq;
    116  1.28   mycroft 
    117  1.28   mycroft 	char	*type;
    118  1.41       cgd 	u_char	readcmd;
    119  1.28   mycroft 	int	flags;
    120  1.28   mycroft #define	MCDF_LOCKED	0x01
    121  1.28   mycroft #define	MCDF_WANTED	0x02
    122  1.28   mycroft #define	MCDF_WLABEL	0x04	/* label is writable */
    123  1.28   mycroft #define	MCDF_LABELLING	0x08	/* writing label */
    124  1.28   mycroft #define	MCDF_LOADED	0x10	/* parameters loaded */
    125   1.1       cgd 	short	status;
    126  1.28   mycroft 	short	audio_status;
    127   1.1       cgd 	int	blksize;
    128   1.1       cgd 	u_long	disksize;
    129   1.6   mycroft 	struct	mcd_volinfo volinfo;
    130  1.28   mycroft 	union	mcd_qchninfo toc[MCD_MAXTOCS];
    131  1.28   mycroft 	struct	mcd_command lastpb;
    132  1.28   mycroft 	struct	mcd_mbx mbx;
    133  1.28   mycroft 	int	lastmode;
    134  1.28   mycroft #define	MCD_MD_UNKNOWN	-1
    135  1.28   mycroft 	int	lastupc;
    136  1.28   mycroft #define	MCD_UPC_UNKNOWN	-1
    137  1.28   mycroft 	int	debug;
    138  1.21   mycroft 	struct	buf buf_queue;
    139   1.8   mycroft };
    140   1.1       cgd 
    141   1.1       cgd /* prototypes */
    142  1.48  christos /* XXX does not belong here */
    143  1.48  christos cdev_decl(mcd);
    144  1.48  christos bdev_decl(mcd);
    145  1.28   mycroft 
    146  1.28   mycroft static int bcd2bin __P((bcd_t));
    147  1.28   mycroft static bcd_t bin2bcd __P((int));
    148  1.28   mycroft static void hsg2msf __P((int, bcd_t *));
    149  1.28   mycroft static daddr_t msf2hsg __P((bcd_t *, int));
    150  1.28   mycroft 
    151  1.28   mycroft int mcd_playtracks __P((struct mcd_softc *, struct ioc_play_track *));
    152  1.28   mycroft int mcd_playmsf __P((struct mcd_softc *, struct ioc_play_msf *));
    153  1.28   mycroft int mcd_playblocks __P((struct mcd_softc *, struct ioc_play_blocks *));
    154  1.28   mycroft int mcd_stop __P((struct mcd_softc *));
    155  1.28   mycroft int mcd_eject __P((struct mcd_softc *));
    156  1.28   mycroft int mcd_read_subchannel __P((struct mcd_softc *, struct ioc_read_subchannel *));
    157  1.28   mycroft int mcd_pause __P((struct mcd_softc *));
    158  1.28   mycroft int mcd_resume __P((struct mcd_softc *));
    159  1.28   mycroft int mcd_toc_header __P((struct mcd_softc *, struct ioc_toc_header *));
    160  1.28   mycroft int mcd_toc_entries __P((struct mcd_softc *, struct ioc_read_toc_entry *));
    161  1.28   mycroft 
    162  1.28   mycroft int mcd_getreply __P((struct mcd_softc *));
    163  1.28   mycroft int mcd_getstat __P((struct mcd_softc *));
    164  1.28   mycroft int mcd_getresult __P((struct mcd_softc *, struct mcd_result *));
    165   1.6   mycroft void mcd_setflags __P((struct mcd_softc *));
    166   1.6   mycroft int mcd_get __P((struct mcd_softc *, char *, int));
    167  1.28   mycroft int mcd_send __P((struct mcd_softc *, struct mcd_mbox *, int));
    168  1.29   mycroft int mcdintr __P((void *));
    169  1.28   mycroft void mcd_soft_reset __P((struct mcd_softc *));
    170  1.28   mycroft int mcd_hard_reset __P((struct mcd_softc *));
    171  1.13       cgd int mcd_setmode __P((struct mcd_softc *, int));
    172  1.28   mycroft int mcd_setupc __P((struct mcd_softc *, int));
    173   1.6   mycroft int mcd_read_toc __P((struct mcd_softc *));
    174  1.28   mycroft int mcd_getqchan __P((struct mcd_softc *, union mcd_qchninfo *, int));
    175  1.28   mycroft int mcd_setlock __P((struct mcd_softc *, int));
    176   1.1       cgd 
    177  1.23   mycroft int mcdprobe __P((struct device *, void *, void *));
    178  1.23   mycroft void mcdattach __P((struct device *, struct device *, void *));
    179   1.8   mycroft 
    180  1.46   thorpej struct cfattach mcd_ca = {
    181  1.46   thorpej 	sizeof(struct mcd_softc), mcdprobe, mcdattach
    182  1.46   thorpej };
    183  1.46   thorpej 
    184  1.46   thorpej struct cfdriver mcd_cd = {
    185  1.46   thorpej 	NULL, "mcd", DV_DISK
    186   1.6   mycroft };
    187   1.1       cgd 
    188  1.48  christos void	mcdgetdisklabel __P((struct mcd_softc *));
    189  1.48  christos int	mcd_get_parms __P((struct mcd_softc *));
    190  1.48  christos void	mcdstrategy __P((struct buf *));
    191  1.48  christos void	mcdstart __P((struct mcd_softc *));
    192  1.48  christos int	mcdlock __P((struct mcd_softc *));
    193  1.48  christos void	mcdunlock __P((struct mcd_softc *));
    194  1.48  christos void	mcd_pseudointr __P((void *));
    195   1.1       cgd 
    196  1.28   mycroft struct dkdriver mcddkdriver = { mcdstrategy };
    197   1.1       cgd 
    198  1.28   mycroft #define MCD_RETRIES	3
    199  1.28   mycroft #define MCD_RDRETRIES	3
    200   1.1       cgd 
    201   1.1       cgd /* several delays */
    202   1.1       cgd #define RDELAY_WAITMODE	300
    203   1.1       cgd #define RDELAY_WAITREAD	800
    204   1.1       cgd 
    205  1.28   mycroft #define	DELAY_GRANULARITY	25	/* 25us */
    206  1.28   mycroft #define DELAY_GETREPLY		100000	/* 100000 * 25us */
    207   1.1       cgd 
    208   1.8   mycroft void
    209   1.8   mycroft mcdattach(parent, self, aux)
    210   1.8   mycroft 	struct device *parent, *self;
    211   1.8   mycroft 	void *aux;
    212   1.1       cgd {
    213   1.8   mycroft 	struct mcd_softc *sc = (void *)self;
    214   1.8   mycroft 	struct isa_attach_args *ia = aux;
    215  1.28   mycroft 	struct mcd_mbox mbx;
    216  1.28   mycroft 
    217  1.44   thorpej 	/*
    218  1.44   thorpej 	 * Initialize and attach the disk structure.
    219  1.44   thorpej 	 */
    220  1.44   thorpej 	sc->sc_dk.dk_driver = &mcddkdriver;
    221  1.44   thorpej 	sc->sc_dk.dk_name = sc->sc_dev.dv_xname;
    222  1.44   thorpej 	disk_attach(&sc->sc_dk);
    223  1.44   thorpej 
    224  1.28   mycroft 	printf(": model %s\n", sc->type != 0 ? sc->type : "unknown");
    225  1.28   mycroft 
    226  1.31   mycroft 	(void) mcd_setlock(sc, MCD_LK_UNLOCK);
    227  1.28   mycroft 
    228  1.28   mycroft 	mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
    229  1.28   mycroft 	mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
    230  1.28   mycroft 	mbx.cmd.data.config.subcommand = MCD_CF_IRQENABLE;
    231  1.28   mycroft 	mbx.cmd.data.config.data1 = 0x01;
    232  1.28   mycroft 	mbx.res.length = 0;
    233  1.28   mycroft 	(void) mcd_send(sc, &mbx, 0);
    234   1.1       cgd 
    235  1.28   mycroft 	mcd_soft_reset(sc);
    236  1.11   mycroft 
    237  1.47       cgd 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    238  1.47       cgd 	    IPL_BIO, mcdintr, sc);
    239   1.1       cgd }
    240   1.1       cgd 
    241  1.34   mycroft /*
    242  1.34   mycroft  * Wait interruptibly for an exclusive lock.
    243  1.34   mycroft  *
    244  1.34   mycroft  * XXX
    245  1.34   mycroft  * Several drivers do this; it should be abstracted and made MP-safe.
    246  1.34   mycroft  */
    247   1.6   mycroft int
    248  1.34   mycroft mcdlock(sc)
    249  1.28   mycroft 	struct mcd_softc *sc;
    250  1.28   mycroft {
    251  1.28   mycroft 	int error;
    252  1.28   mycroft 
    253  1.28   mycroft 	while ((sc->flags & MCDF_LOCKED) != 0) {
    254  1.28   mycroft 		sc->flags |= MCDF_WANTED;
    255  1.28   mycroft 		if ((error = tsleep(sc, PRIBIO | PCATCH, "mcdlck", 0)) != 0)
    256  1.28   mycroft 			return error;
    257  1.28   mycroft 	}
    258  1.34   mycroft 	sc->flags |= MCDF_LOCKED;
    259  1.28   mycroft 	return 0;
    260  1.28   mycroft }
    261  1.28   mycroft 
    262  1.34   mycroft /*
    263  1.34   mycroft  * Unlock and wake up any waiters.
    264  1.34   mycroft  */
    265  1.28   mycroft void
    266  1.28   mycroft mcdunlock(sc)
    267  1.28   mycroft 	struct mcd_softc *sc;
    268  1.28   mycroft {
    269  1.28   mycroft 
    270  1.28   mycroft 	sc->flags &= ~MCDF_LOCKED;
    271  1.28   mycroft 	if ((sc->flags & MCDF_WANTED) != 0) {
    272  1.28   mycroft 		sc->flags &= ~MCDF_WANTED;
    273  1.28   mycroft 		wakeup(sc);
    274  1.28   mycroft 	}
    275  1.28   mycroft }
    276  1.28   mycroft 
    277  1.28   mycroft int
    278  1.21   mycroft mcdopen(dev, flag, fmt, p)
    279   1.6   mycroft 	dev_t dev;
    280  1.21   mycroft 	int flag, fmt;
    281  1.21   mycroft 	struct proc *p;
    282   1.1       cgd {
    283  1.28   mycroft 	int error;
    284  1.21   mycroft 	int unit, part;
    285   1.6   mycroft 	struct mcd_softc *sc;
    286  1.28   mycroft 
    287   1.6   mycroft 	unit = MCDUNIT(dev);
    288  1.46   thorpej 	if (unit >= mcd_cd.cd_ndevs)
    289   1.1       cgd 		return ENXIO;
    290  1.46   thorpej 	sc = mcd_cd.cd_devs[unit];
    291   1.8   mycroft 	if (!sc)
    292   1.6   mycroft 		return ENXIO;
    293   1.1       cgd 
    294  1.48  christos 	if ((error = mcdlock(sc)) != 0)
    295  1.28   mycroft 		return error;
    296  1.21   mycroft 
    297  1.28   mycroft 	if (sc->sc_dk.dk_openmask != 0) {
    298  1.28   mycroft 		/*
    299  1.28   mycroft 		 * If any partition is open, but the disk has been invalidated,
    300  1.28   mycroft 		 * disallow further opens.
    301  1.28   mycroft 		 */
    302  1.35   mycroft 		if ((sc->flags & MCDF_LOADED) == 0) {
    303  1.35   mycroft 			error = EIO;
    304  1.35   mycroft 			goto bad3;
    305  1.35   mycroft 		}
    306  1.28   mycroft 	} else {
    307  1.28   mycroft 		/*
    308  1.28   mycroft 		 * Lock the drawer.  This will also notice any pending disk
    309  1.28   mycroft 		 * change or door open indicator and clear the MCDF_LOADED bit
    310  1.28   mycroft 		 * if necessary.
    311  1.28   mycroft 		 */
    312  1.28   mycroft 		(void) mcd_setlock(sc, MCD_LK_LOCK);
    313  1.28   mycroft 
    314  1.28   mycroft 		if ((sc->flags & MCDF_LOADED) == 0) {
    315  1.28   mycroft 			/* Partially reset the state. */
    316  1.28   mycroft 			sc->lastmode = MCD_MD_UNKNOWN;
    317  1.28   mycroft 			sc->lastupc = MCD_UPC_UNKNOWN;
    318  1.28   mycroft 
    319  1.28   mycroft 			sc->flags |= MCDF_LOADED;
    320  1.28   mycroft 
    321  1.28   mycroft 			/* Set the mode, causing the disk to spin up. */
    322  1.28   mycroft 			if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
    323  1.28   mycroft 				goto bad2;
    324  1.28   mycroft 
    325  1.28   mycroft 			/* Load the physical device parameters. */
    326  1.28   mycroft 			if (mcd_get_parms(sc) != 0) {
    327  1.28   mycroft 				error = ENXIO;
    328  1.28   mycroft 				goto bad2;
    329  1.28   mycroft 			}
    330   1.1       cgd 
    331  1.33   mycroft 			/* Read the table of contents. */
    332  1.33   mycroft 			if ((error = mcd_read_toc(sc)) != 0)
    333  1.33   mycroft 				goto bad2;
    334  1.33   mycroft 
    335  1.28   mycroft 			/* Fabricate a disk label. */
    336  1.28   mycroft 			mcdgetdisklabel(sc);
    337  1.28   mycroft 		}
    338  1.21   mycroft 	}
    339  1.21   mycroft 
    340   1.6   mycroft 	MCD_TRACE("open: partition=%d disksize=%d blksize=%d\n", part,
    341   1.6   mycroft 	    sc->disksize, sc->blksize, 0);
    342   1.1       cgd 
    343  1.34   mycroft 	part = MCDPART(dev);
    344  1.34   mycroft 
    345  1.28   mycroft 	/* Check that the partition exists. */
    346   1.6   mycroft 	if (part != RAW_PART &&
    347  1.44   thorpej 	    (part >= sc->sc_dk.dk_label->d_npartitions ||
    348  1.44   thorpej 	     sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    349  1.28   mycroft 		error = ENXIO;
    350  1.28   mycroft 		goto bad;
    351  1.28   mycroft 	}
    352   1.1       cgd 
    353  1.21   mycroft 	/* Insure only one open at a time. */
    354  1.21   mycroft 	switch (fmt) {
    355  1.21   mycroft 	case S_IFCHR:
    356  1.21   mycroft 		sc->sc_dk.dk_copenmask |= (1 << part);
    357  1.21   mycroft 		break;
    358  1.21   mycroft 	case S_IFBLK:
    359  1.21   mycroft 		sc->sc_dk.dk_bopenmask |= (1 << part);
    360  1.21   mycroft 		break;
    361  1.21   mycroft 	}
    362  1.21   mycroft 	sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    363  1.21   mycroft 
    364  1.34   mycroft 	mcdunlock(sc);
    365   1.6   mycroft 	return 0;
    366  1.28   mycroft 
    367  1.28   mycroft bad2:
    368  1.28   mycroft 	sc->flags &= ~MCDF_LOADED;
    369  1.28   mycroft 
    370  1.28   mycroft bad:
    371  1.28   mycroft 	if (sc->sc_dk.dk_openmask == 0) {
    372  1.28   mycroft #if 0
    373  1.28   mycroft 		(void) mcd_setmode(sc, MCD_MD_SLEEP);
    374  1.28   mycroft #endif
    375  1.28   mycroft 		(void) mcd_setlock(sc, MCD_LK_UNLOCK);
    376  1.28   mycroft 	}
    377  1.28   mycroft 
    378  1.35   mycroft bad3:
    379  1.34   mycroft 	mcdunlock(sc);
    380  1.28   mycroft 	return error;
    381   1.1       cgd }
    382   1.1       cgd 
    383   1.6   mycroft int
    384  1.48  christos mcdclose(dev, flag, fmt, p)
    385   1.6   mycroft 	dev_t dev;
    386  1.21   mycroft 	int flag, fmt;
    387  1.48  christos 	struct proc *p;
    388   1.1       cgd {
    389  1.46   thorpej 	struct mcd_softc *sc = mcd_cd.cd_devs[MCDUNIT(dev)];
    390  1.21   mycroft 	int part = MCDPART(dev);
    391  1.34   mycroft 	int error;
    392   1.1       cgd 
    393  1.21   mycroft 	MCD_TRACE("close: partition=%d\n", part, 0, 0, 0);
    394   1.6   mycroft 
    395  1.48  christos 	if ((error = mcdlock(sc)) != 0)
    396  1.34   mycroft 		return error;
    397  1.34   mycroft 
    398  1.21   mycroft 	switch (fmt) {
    399  1.21   mycroft 	case S_IFCHR:
    400  1.21   mycroft 		sc->sc_dk.dk_copenmask &= ~(1 << part);
    401  1.21   mycroft 		break;
    402  1.21   mycroft 	case S_IFBLK:
    403  1.21   mycroft 		sc->sc_dk.dk_bopenmask &= ~(1 << part);
    404  1.21   mycroft 		break;
    405  1.21   mycroft 	}
    406  1.21   mycroft 	sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    407   1.1       cgd 
    408  1.28   mycroft 	if (sc->sc_dk.dk_openmask == 0) {
    409  1.34   mycroft 		/* XXXX Must wait for I/O to complete! */
    410  1.28   mycroft 
    411  1.28   mycroft #if 0
    412  1.28   mycroft 		(void) mcd_setmode(sc, MCD_MD_SLEEP);
    413  1.28   mycroft #endif
    414  1.28   mycroft 		(void) mcd_setlock(sc, MCD_LK_UNLOCK);
    415  1.28   mycroft 	}
    416  1.28   mycroft 
    417  1.34   mycroft 	mcdunlock(sc);
    418   1.1       cgd 	return 0;
    419   1.1       cgd }
    420   1.1       cgd 
    421   1.6   mycroft void
    422   1.6   mycroft mcdstrategy(bp)
    423   1.6   mycroft 	struct buf *bp;
    424   1.1       cgd {
    425  1.46   thorpej 	struct mcd_softc *sc = mcd_cd.cd_devs[MCDUNIT(bp->b_dev)];
    426   1.1       cgd 	int s;
    427   1.1       cgd 
    428   1.6   mycroft 	/* Test validity. */
    429   1.6   mycroft 	MCD_TRACE("strategy: buf=0x%lx blkno=%ld bcount=%ld\n", bp,
    430   1.6   mycroft 	    bp->b_blkno, bp->b_bcount, 0);
    431  1.28   mycroft 	if (bp->b_blkno < 0 ||
    432  1.28   mycroft 	    (bp->b_bcount % sc->blksize) != 0) {
    433  1.48  christos 		printf("%s: strategy: blkno = %d bcount = %ld\n",
    434   1.6   mycroft 		    sc->sc_dev.dv_xname, bp->b_blkno, bp->b_bcount);
    435   1.1       cgd 		bp->b_error = EINVAL;
    436   1.1       cgd 		goto bad;
    437   1.1       cgd 	}
    438   1.1       cgd 
    439   1.6   mycroft 	/* If device invalidated (e.g. media change, door open), error. */
    440  1.28   mycroft 	if ((sc->flags & MCDF_LOADED) == 0) {
    441   1.6   mycroft 		MCD_TRACE("strategy: drive not valid\n", 0, 0, 0, 0);
    442   1.1       cgd 		bp->b_error = EIO;
    443   1.1       cgd 		goto bad;
    444   1.1       cgd 	}
    445   1.1       cgd 
    446   1.6   mycroft 	/* No data to read. */
    447   1.1       cgd 	if (bp->b_bcount == 0)
    448   1.1       cgd 		goto done;
    449   1.1       cgd 
    450  1.28   mycroft 	/*
    451  1.28   mycroft 	 * Do bounds checking, adjust transfer. if error, process.
    452  1.28   mycroft 	 * If end of partition, just return.
    453  1.28   mycroft 	 */
    454  1.36   mycroft 	if (MCDPART(bp->b_dev) != RAW_PART &&
    455  1.44   thorpej 	    bounds_check_with_label(bp, sc->sc_dk.dk_label,
    456  1.28   mycroft 	    (sc->flags & (MCDF_WLABEL|MCDF_LABELLING)) != 0) <= 0)
    457  1.28   mycroft 		goto done;
    458   1.1       cgd 
    459   1.6   mycroft 	/* Queue it. */
    460   1.1       cgd 	s = splbio();
    461  1.28   mycroft 	disksort(&sc->buf_queue, bp);
    462   1.1       cgd 	splx(s);
    463  1.28   mycroft 	if (!sc->buf_queue.b_active)
    464  1.28   mycroft 		mcdstart(sc);
    465   1.1       cgd 	return;
    466   1.1       cgd 
    467   1.1       cgd bad:
    468   1.1       cgd 	bp->b_flags |= B_ERROR;
    469   1.1       cgd done:
    470   1.1       cgd 	bp->b_resid = bp->b_bcount;
    471   1.1       cgd 	biodone(bp);
    472   1.1       cgd }
    473   1.1       cgd 
    474  1.28   mycroft void
    475  1.28   mycroft mcdstart(sc)
    476   1.6   mycroft 	struct mcd_softc *sc;
    477   1.1       cgd {
    478  1.28   mycroft 	struct buf *bp, *dp = &sc->buf_queue;
    479  1.21   mycroft 	int s;
    480   1.1       cgd 
    481  1.21   mycroft loop:
    482  1.21   mycroft 	s = splbio();
    483  1.21   mycroft 
    484  1.28   mycroft 	bp = dp->b_actf;
    485  1.28   mycroft 	if (bp == NULL) {
    486  1.28   mycroft 		/* Nothing to do. */
    487  1.28   mycroft 		dp->b_active = 0;
    488   1.1       cgd 		splx(s);
    489   1.1       cgd 		return;
    490   1.1       cgd 	}
    491   1.1       cgd 
    492  1.21   mycroft 	/* Block found to process; dequeue. */
    493  1.21   mycroft 	MCD_TRACE("start: found block bp=0x%x\n", bp, 0, 0, 0);
    494  1.28   mycroft 	dp->b_actf = bp->b_actf;
    495  1.21   mycroft 	splx(s);
    496  1.21   mycroft 
    497   1.6   mycroft 	/* Changed media? */
    498  1.28   mycroft 	if ((sc->flags & MCDF_LOADED) == 0) {
    499   1.6   mycroft 		MCD_TRACE("start: drive not valid\n", 0, 0, 0, 0);
    500  1.21   mycroft 		bp->b_error = EIO;
    501  1.21   mycroft 		bp->b_flags |= B_ERROR;
    502  1.21   mycroft 		biodone(bp);
    503  1.21   mycroft 		goto loop;
    504   1.1       cgd 	}
    505   1.1       cgd 
    506  1.28   mycroft 	dp->b_active = 1;
    507  1.28   mycroft 
    508  1.44   thorpej 	/* Instrumentation. */
    509  1.44   thorpej 	s = splbio();
    510  1.44   thorpej 	disk_busy(&sc->sc_dk);
    511  1.44   thorpej 	splx(s);
    512  1.44   thorpej 
    513  1.28   mycroft 	sc->mbx.retry = MCD_RDRETRIES;
    514   1.6   mycroft 	sc->mbx.bp = bp;
    515  1.28   mycroft 	sc->mbx.blkno = bp->b_blkno / (sc->blksize / DEV_BSIZE);
    516  1.28   mycroft 	if (MCDPART(bp->b_dev) != RAW_PART) {
    517  1.28   mycroft 		struct partition *p;
    518  1.44   thorpej 		p = &sc->sc_dk.dk_label->d_partitions[MCDPART(bp->b_dev)];
    519  1.28   mycroft 		sc->mbx.blkno += p->p_offset;
    520  1.28   mycroft 	}
    521  1.28   mycroft 	sc->mbx.nblk = bp->b_bcount / sc->blksize;
    522  1.28   mycroft 	sc->mbx.sz = sc->blksize;
    523  1.28   mycroft 	sc->mbx.skip = 0;
    524  1.28   mycroft 	sc->mbx.state = MCD_S_BEGIN;
    525  1.28   mycroft 	sc->mbx.mode = MCD_MD_COOKED;
    526   1.6   mycroft 
    527  1.28   mycroft 	s = splbio();
    528  1.29   mycroft 	(void) mcdintr(sc);
    529  1.28   mycroft 	splx(s);
    530  1.40   mycroft }
    531  1.40   mycroft 
    532  1.40   mycroft int
    533  1.48  christos mcdread(dev, uio, flags)
    534  1.40   mycroft 	dev_t dev;
    535  1.40   mycroft 	struct uio *uio;
    536  1.48  christos 	int flags;
    537  1.40   mycroft {
    538  1.40   mycroft 
    539  1.40   mycroft 	return (physio(mcdstrategy, NULL, dev, B_READ, minphys, uio));
    540  1.40   mycroft }
    541  1.40   mycroft 
    542  1.40   mycroft int
    543  1.48  christos mcdwrite(dev, uio, flags)
    544  1.40   mycroft 	dev_t dev;
    545  1.40   mycroft 	struct uio *uio;
    546  1.48  christos 	int flags;
    547  1.40   mycroft {
    548  1.40   mycroft 
    549  1.40   mycroft 	return (physio(mcdstrategy, NULL, dev, B_WRITE, minphys, uio));
    550   1.6   mycroft }
    551   1.6   mycroft 
    552   1.6   mycroft int
    553  1.28   mycroft mcdioctl(dev, cmd, addr, flag, p)
    554   1.6   mycroft 	dev_t dev;
    555  1.22       cgd 	u_long cmd;
    556   1.6   mycroft 	caddr_t addr;
    557  1.28   mycroft 	int flag;
    558   1.6   mycroft 	struct proc *p;
    559   1.1       cgd {
    560  1.46   thorpej 	struct mcd_softc *sc = mcd_cd.cd_devs[MCDUNIT(dev)];
    561  1.28   mycroft 	int error;
    562   1.1       cgd 
    563  1.28   mycroft 	MCD_TRACE("ioctl: cmd=0x%x\n", cmd, 0, 0, 0);
    564  1.28   mycroft 
    565  1.28   mycroft 	if ((sc->flags & MCDF_LOADED) == 0)
    566   1.1       cgd 		return EIO;
    567  1.21   mycroft 
    568   1.1       cgd 	switch (cmd) {
    569  1.28   mycroft 	case DIOCGDINFO:
    570  1.44   thorpej 		*(struct disklabel *)addr = *(sc->sc_dk.dk_label);
    571  1.28   mycroft 		return 0;
    572  1.28   mycroft 
    573  1.28   mycroft 	case DIOCGPART:
    574  1.44   thorpej 		((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
    575  1.28   mycroft 		((struct partinfo *)addr)->part =
    576  1.44   thorpej 		    &sc->sc_dk.dk_label->d_partitions[MCDPART(dev)];
    577  1.28   mycroft 		return 0;
    578  1.28   mycroft 
    579  1.28   mycroft 	case DIOCWDINFO:
    580  1.28   mycroft 	case DIOCSDINFO:
    581  1.28   mycroft 		if ((flag & FWRITE) == 0)
    582  1.28   mycroft 			return EBADF;
    583  1.28   mycroft 
    584  1.48  christos 		if ((error = mcdlock(sc)) != 0)
    585  1.28   mycroft 			return error;
    586  1.34   mycroft 		sc->flags |= MCDF_LABELLING;
    587  1.28   mycroft 
    588  1.44   thorpej 		error = setdisklabel(sc->sc_dk.dk_label,
    589  1.28   mycroft 		    (struct disklabel *)addr, /*sc->sc_dk.dk_openmask : */0,
    590  1.44   thorpej 		    sc->sc_dk.dk_cpulabel);
    591  1.28   mycroft 		if (error == 0) {
    592  1.28   mycroft 		}
    593  1.28   mycroft 
    594  1.28   mycroft 		sc->flags &= ~MCDF_LABELLING;
    595  1.28   mycroft 		mcdunlock(sc);
    596  1.28   mycroft 		return error;
    597  1.28   mycroft 
    598  1.28   mycroft 	case DIOCWLABEL:
    599  1.28   mycroft 		return EBADF;
    600  1.28   mycroft 
    601   1.1       cgd 	case CDIOCPLAYTRACKS:
    602  1.21   mycroft 		return mcd_playtracks(sc, (struct ioc_play_track *)addr);
    603  1.28   mycroft 	case CDIOCPLAYMSF:
    604  1.28   mycroft 		return mcd_playmsf(sc, (struct ioc_play_msf *)addr);
    605   1.1       cgd 	case CDIOCPLAYBLOCKS:
    606  1.28   mycroft 		return mcd_playblocks(sc, (struct ioc_play_blocks *)addr);
    607   1.1       cgd 	case CDIOCREADSUBCHANNEL:
    608  1.28   mycroft 		return mcd_read_subchannel(sc, (struct ioc_read_subchannel *)addr);
    609   1.1       cgd 	case CDIOREADTOCHEADER:
    610  1.21   mycroft 		return mcd_toc_header(sc, (struct ioc_toc_header *)addr);
    611   1.1       cgd 	case CDIOREADTOCENTRYS:
    612  1.28   mycroft 		return mcd_toc_entries(sc, (struct ioc_read_toc_entry *)addr);
    613   1.1       cgd 	case CDIOCSETPATCH:
    614   1.1       cgd 	case CDIOCGETVOL:
    615   1.1       cgd 	case CDIOCSETVOL:
    616   1.1       cgd 	case CDIOCSETMONO:
    617  1.18   mycroft 	case CDIOCSETSTEREO:
    618   1.1       cgd 	case CDIOCSETMUTE:
    619   1.1       cgd 	case CDIOCSETLEFT:
    620   1.1       cgd 	case CDIOCSETRIGHT:
    621   1.1       cgd 		return EINVAL;
    622   1.1       cgd 	case CDIOCRESUME:
    623   1.6   mycroft 		return mcd_resume(sc);
    624   1.1       cgd 	case CDIOCPAUSE:
    625   1.6   mycroft 		return mcd_pause(sc);
    626   1.1       cgd 	case CDIOCSTART:
    627   1.1       cgd 		return EINVAL;
    628   1.1       cgd 	case CDIOCSTOP:
    629   1.6   mycroft 		return mcd_stop(sc);
    630  1.45   thorpej 	case CDIOCEJECT: /* FALLTHROUGH */
    631  1.45   thorpej 	case DIOCEJECT:
    632  1.28   mycroft 		return mcd_eject(sc);
    633  1.28   mycroft 	case CDIOCALLOW:
    634  1.28   mycroft 		return mcd_setlock(sc, MCD_LK_UNLOCK);
    635  1.28   mycroft 	case CDIOCPREVENT:
    636  1.28   mycroft 		return mcd_setlock(sc, MCD_LK_LOCK);
    637  1.45   thorpej 	case DIOCLOCK:
    638  1.45   thorpej 		return mcd_setlock(sc,
    639  1.45   thorpej 		    (*(int *)addr) ? MCD_LK_LOCK : MCD_LK_UNLOCK);
    640   1.1       cgd 	case CDIOCSETDEBUG:
    641   1.6   mycroft 		sc->debug = 1;
    642   1.1       cgd 		return 0;
    643   1.1       cgd 	case CDIOCCLRDEBUG:
    644   1.6   mycroft 		sc->debug = 0;
    645   1.1       cgd 		return 0;
    646   1.1       cgd 	case CDIOCRESET:
    647  1.28   mycroft 		return mcd_hard_reset(sc);
    648  1.29   mycroft 
    649   1.1       cgd 	default:
    650   1.1       cgd 		return ENOTTY;
    651   1.1       cgd 	}
    652  1.28   mycroft 
    653   1.6   mycroft #ifdef DIAGNOSTIC
    654   1.6   mycroft 	panic("mcdioctl: impossible");
    655   1.6   mycroft #endif
    656   1.1       cgd }
    657   1.1       cgd 
    658   1.6   mycroft /*
    659   1.6   mycroft  * This could have been taken from scsi/cd.c, but it is not clear
    660   1.6   mycroft  * whether the scsi cd driver is linked in.
    661   1.1       cgd  */
    662  1.28   mycroft void
    663  1.28   mycroft mcdgetdisklabel(sc)
    664   1.6   mycroft 	struct mcd_softc *sc;
    665   1.1       cgd {
    666  1.44   thorpej 	struct disklabel *lp = sc->sc_dk.dk_label;
    667   1.1       cgd 
    668  1.42   mycroft 	bzero(lp, sizeof(struct disklabel));
    669  1.44   thorpej 	bzero(sc->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
    670  1.28   mycroft 
    671  1.42   mycroft 	lp->d_secsize = sc->blksize;
    672  1.42   mycroft 	lp->d_ntracks = 1;
    673  1.42   mycroft 	lp->d_nsectors = 100;
    674  1.42   mycroft 	lp->d_ncylinders = (sc->disksize / 100) + 1;
    675  1.42   mycroft 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    676  1.42   mycroft 
    677  1.42   mycroft 	strncpy(lp->d_typename, "Mitsumi CD-ROM", 16);
    678  1.42   mycroft 	lp->d_type = 0;	/* XXX */
    679  1.42   mycroft 	strncpy(lp->d_packname, "fictitious", 16);
    680  1.42   mycroft 	lp->d_secperunit = sc->disksize;
    681  1.42   mycroft 	lp->d_rpm = 300;
    682  1.42   mycroft 	lp->d_interleave = 1;
    683  1.42   mycroft 	lp->d_flags = D_REMOVABLE;
    684  1.42   mycroft 
    685  1.42   mycroft 	lp->d_partitions[0].p_offset = 0;
    686  1.42   mycroft 	lp->d_partitions[0].p_size =
    687  1.42   mycroft 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    688  1.42   mycroft 	lp->d_partitions[0].p_fstype = FS_ISO9660;
    689  1.42   mycroft 	lp->d_partitions[RAW_PART].p_offset = 0;
    690  1.42   mycroft 	lp->d_partitions[RAW_PART].p_size =
    691  1.42   mycroft 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    692  1.42   mycroft 	lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660;
    693  1.42   mycroft 	lp->d_npartitions = RAW_PART + 1;
    694   1.1       cgd 
    695  1.42   mycroft 	lp->d_magic = DISKMAGIC;
    696  1.42   mycroft 	lp->d_magic2 = DISKMAGIC;
    697  1.42   mycroft 	lp->d_checksum = dkcksum(lp);
    698  1.28   mycroft }
    699  1.28   mycroft 
    700  1.28   mycroft int
    701  1.28   mycroft mcd_get_parms(sc)
    702  1.28   mycroft 	struct mcd_softc *sc;
    703  1.28   mycroft {
    704  1.28   mycroft 	struct mcd_mbox mbx;
    705  1.28   mycroft 	daddr_t size;
    706  1.28   mycroft 	int error;
    707  1.28   mycroft 
    708  1.28   mycroft 	/* Send volume info command. */
    709  1.28   mycroft 	mbx.cmd.opcode = MCD_CMDGETVOLINFO;
    710  1.28   mycroft 	mbx.cmd.length = 0;
    711  1.28   mycroft 	mbx.res.length = sizeof(mbx.res.data.volinfo);
    712  1.28   mycroft 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
    713  1.28   mycroft 		return error;
    714  1.28   mycroft 
    715  1.28   mycroft 	if (mbx.res.data.volinfo.trk_low == 0x00 &&
    716  1.28   mycroft 	    mbx.res.data.volinfo.trk_high == 0x00)
    717  1.28   mycroft 		return EINVAL;
    718  1.28   mycroft 
    719  1.28   mycroft 	/* Volinfo is OK. */
    720  1.28   mycroft 	sc->volinfo = mbx.res.data.volinfo;
    721  1.28   mycroft 	sc->blksize = MCD_BLKSIZE_COOKED;
    722  1.28   mycroft 	size = msf2hsg(sc->volinfo.vol_msf, 0);
    723  1.28   mycroft 	sc->disksize = size * (MCD_BLKSIZE_COOKED / DEV_BSIZE);
    724   1.1       cgd 	return 0;
    725   1.1       cgd }
    726   1.1       cgd 
    727   1.6   mycroft int
    728   1.6   mycroft mcdsize(dev)
    729   1.6   mycroft 	dev_t dev;
    730   1.1       cgd {
    731   1.1       cgd 
    732  1.28   mycroft 	/* CD-ROMs are read-only. */
    733   1.1       cgd 	return -1;
    734  1.15   mycroft }
    735  1.15   mycroft 
    736  1.15   mycroft int
    737  1.39       cgd mcddump(dev, blkno, va, size)
    738  1.39       cgd 	dev_t dev;
    739  1.39       cgd 	daddr_t blkno;
    740  1.39       cgd 	caddr_t va;
    741  1.39       cgd 	size_t size;
    742  1.15   mycroft {
    743  1.15   mycroft 
    744  1.15   mycroft 	/* Not implemented. */
    745  1.39       cgd 	return ENXIO;
    746   1.1       cgd }
    747   1.1       cgd 
    748   1.6   mycroft int
    749  1.23   mycroft mcdprobe(parent, match, aux)
    750  1.23   mycroft 	struct device *parent;
    751  1.23   mycroft 	void *match, *aux;
    752   1.8   mycroft {
    753  1.23   mycroft 	struct mcd_softc *sc = match;
    754   1.8   mycroft 	struct isa_attach_args *ia = aux;
    755  1.24   mycroft 	int iobase = ia->ia_iobase;
    756   1.1       cgd 	int i;
    757  1.28   mycroft 	struct mcd_mbox mbx;
    758   1.6   mycroft 
    759   1.6   mycroft 	sc->iobase = iobase;
    760   1.1       cgd 
    761   1.6   mycroft 	/* Send a reset. */
    762  1.28   mycroft 	outb(iobase + MCD_RESET, 0);
    763  1.17   mycroft 	delay(1000000);
    764   1.6   mycroft 	/* Get any pending status and throw away. */
    765   1.6   mycroft 	for (i = 10; i; i--)
    766  1.28   mycroft 		inb(iobase + MCD_STATUS);
    767   1.7   mycroft 	delay(1000);
    768   1.1       cgd 
    769   1.6   mycroft 	/* Send get status command. */
    770  1.28   mycroft 	mbx.cmd.opcode = MCD_CMDGETSTAT;
    771  1.28   mycroft 	mbx.cmd.length = 0;
    772  1.28   mycroft 	mbx.res.length = 0;
    773  1.28   mycroft 	if (mcd_send(sc, &mbx, 0) != 0)
    774   1.6   mycroft 		return 0;
    775   1.1       cgd 
    776  1.28   mycroft 	/* Get info about the drive. */
    777  1.28   mycroft 	mbx.cmd.opcode = MCD_CMDCONTINFO;
    778  1.28   mycroft 	mbx.cmd.length = 0;
    779  1.28   mycroft 	mbx.res.length = sizeof(mbx.res.data.continfo);
    780  1.28   mycroft 	if (mcd_send(sc, &mbx, 0) != 0)
    781   1.6   mycroft 		return 0;
    782   1.1       cgd 
    783   1.6   mycroft 	/*
    784   1.6   mycroft 	 * The following is code which is not guaranteed to work for all
    785   1.6   mycroft 	 * drives, because the meaning of the expected 'M' is not clear
    786   1.6   mycroft 	 * (M_itsumi is an obvious assumption, but I don't trust that).
    787   1.6   mycroft 	 * Also, the original hack had a bogus condition that always
    788   1.6   mycroft 	 * returned true.
    789  1.28   mycroft 	 *
    790  1.28   mycroft 	 * Note:  Which models support interrupts?  >=LU005S?
    791   1.6   mycroft 	 */
    792  1.41       cgd 	sc->readcmd = MCD_CMDREADSINGLESPEED;
    793  1.28   mycroft 	switch (mbx.res.data.continfo.code) {
    794  1.28   mycroft 	case 'M':
    795  1.28   mycroft 		if (mbx.res.data.continfo.version <= 2)
    796  1.28   mycroft 			sc->type = "LU002S";
    797  1.28   mycroft 		else if (mbx.res.data.continfo.version <= 5)
    798  1.28   mycroft 			sc->type = "LU005S";
    799  1.28   mycroft 		else
    800  1.28   mycroft 			sc->type = "LU006S";
    801  1.28   mycroft 		break;
    802  1.28   mycroft 	case 'F':
    803  1.28   mycroft 		sc->type = "FX001";
    804  1.28   mycroft 		break;
    805  1.28   mycroft 	case 'D':
    806  1.28   mycroft 		sc->type = "FX001D";
    807  1.41       cgd 		sc->readcmd = MCD_CMDREADDOUBLESPEED;
    808  1.28   mycroft 		break;
    809  1.28   mycroft 	default:
    810  1.17   mycroft 		printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n",
    811  1.28   mycroft 		    sc->sc_dev.dv_xname,
    812  1.28   mycroft 		    mbx.res.data.continfo.code, mbx.res.data.continfo.version);
    813  1.28   mycroft 		sc->type = 0;
    814  1.28   mycroft 		break;
    815   1.1       cgd 	}
    816   1.6   mycroft 
    817   1.8   mycroft 	ia->ia_iosize = 4;
    818   1.8   mycroft 	ia->ia_msize = 0;
    819   1.8   mycroft 	return 1;
    820   1.1       cgd }
    821   1.1       cgd 
    822   1.6   mycroft int
    823  1.28   mycroft mcd_getreply(sc)
    824  1.28   mycroft 	struct mcd_softc *sc;
    825   1.1       cgd {
    826  1.28   mycroft 	int iobase = sc->iobase;
    827   1.1       cgd 	int i;
    828   1.1       cgd 
    829   1.6   mycroft 	/* Wait until xfer port senses data ready. */
    830  1.28   mycroft 	for (i = DELAY_GETREPLY; i; i--) {
    831  1.28   mycroft 		if ((inb(iobase + MCD_XFER) & MCD_XF_STATUSUNAVAIL) == 0)
    832  1.28   mycroft 			break;
    833  1.28   mycroft 		delay(DELAY_GRANULARITY);
    834   1.1       cgd 	}
    835  1.28   mycroft 	if (!i)
    836  1.28   mycroft 		return -1;
    837  1.28   mycroft 
    838  1.28   mycroft 	/* Get the data. */
    839  1.28   mycroft 	return inb(iobase + MCD_STATUS);
    840   1.1       cgd }
    841   1.1       cgd 
    842   1.6   mycroft int
    843  1.28   mycroft mcd_getstat(sc)
    844   1.6   mycroft 	struct mcd_softc *sc;
    845   1.6   mycroft {
    846  1.28   mycroft 	struct mcd_mbox mbx;
    847   1.1       cgd 
    848  1.28   mycroft 	mbx.cmd.opcode = MCD_CMDGETSTAT;
    849  1.28   mycroft 	mbx.cmd.length = 0;
    850  1.28   mycroft 	mbx.res.length = 0;
    851  1.28   mycroft 	return mcd_send(sc, &mbx, 1);
    852   1.1       cgd }
    853   1.1       cgd 
    854   1.6   mycroft int
    855  1.28   mycroft mcd_getresult(sc, res)
    856   1.6   mycroft 	struct mcd_softc *sc;
    857  1.28   mycroft 	struct mcd_result *res;
    858   1.1       cgd {
    859  1.28   mycroft 	int i, x;
    860  1.28   mycroft 
    861  1.28   mycroft 	if (sc->debug)
    862  1.28   mycroft 		printf("%s: mcd_getresult: %d", sc->sc_dev.dv_xname,
    863  1.28   mycroft 		    res->length);
    864  1.28   mycroft 
    865  1.28   mycroft 	if ((x = mcd_getreply(sc)) < 0) {
    866  1.28   mycroft 		if (sc->debug)
    867  1.28   mycroft 			printf(" timeout\n");
    868  1.28   mycroft 		else
    869  1.28   mycroft 			printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
    870  1.28   mycroft 		return EIO;
    871  1.28   mycroft 	}
    872  1.28   mycroft 	if (sc->debug)
    873  1.28   mycroft 		printf(" %02x", (u_int)x);
    874  1.28   mycroft 	sc->status = x;
    875  1.28   mycroft 	mcd_setflags(sc);
    876  1.28   mycroft 
    877  1.28   mycroft 	if ((sc->status & MCD_ST_CMDCHECK) != 0)
    878  1.28   mycroft 		return EINVAL;
    879   1.1       cgd 
    880  1.28   mycroft 	for (i = 0; i < res->length; i++) {
    881  1.28   mycroft 		if ((x = mcd_getreply(sc)) < 0) {
    882  1.28   mycroft 			if (sc->debug)
    883  1.28   mycroft 				printf(" timeout\n");
    884  1.28   mycroft 			else
    885  1.28   mycroft 				printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
    886  1.28   mycroft 			return EIO;
    887  1.28   mycroft 		}
    888  1.28   mycroft 		if (sc->debug)
    889  1.28   mycroft 			printf(" %02x", (u_int)x);
    890  1.28   mycroft 		res->data.raw.data[i] = x;
    891   1.6   mycroft 	}
    892   1.1       cgd 
    893  1.28   mycroft 	if (sc->debug)
    894  1.28   mycroft 		printf(" succeeded\n");
    895  1.28   mycroft 
    896  1.38   mycroft #ifdef MCDDEBUG
    897  1.28   mycroft 	delay(10);
    898  1.28   mycroft 	while ((inb(sc->iobase + MCD_XFER) & MCD_XF_STATUSUNAVAIL) == 0) {
    899  1.28   mycroft 		x = inb(sc->iobase + MCD_STATUS);
    900  1.28   mycroft 		printf("%s: got extra byte %02x during getstatus\n",
    901  1.28   mycroft 		    sc->sc_dev.dv_xname, (u_int)x);
    902  1.28   mycroft 		delay(10);
    903  1.28   mycroft 	}
    904  1.28   mycroft #endif
    905   1.1       cgd 
    906  1.28   mycroft 	return 0;
    907   1.1       cgd }
    908   1.1       cgd 
    909   1.6   mycroft void
    910   1.6   mycroft mcd_setflags(sc)
    911   1.6   mycroft 	struct mcd_softc *sc;
    912   1.1       cgd {
    913   1.6   mycroft 
    914   1.6   mycroft 	/* Check flags. */
    915  1.28   mycroft 	if ((sc->flags & MCDF_LOADED) != 0 &&
    916  1.28   mycroft 	    (sc->status & (MCD_ST_DSKCHNG | MCD_ST_DSKIN | MCD_ST_DOOROPEN)) !=
    917  1.28   mycroft 	    MCD_ST_DSKIN) {
    918  1.28   mycroft 		if ((sc->status & MCD_ST_DOOROPEN) != 0)
    919  1.28   mycroft 			printf("%s: door open\n", sc->sc_dev.dv_xname);
    920  1.28   mycroft 		else if ((sc->status & MCD_ST_DSKIN) == 0)
    921  1.28   mycroft 			printf("%s: no disk present\n", sc->sc_dev.dv_xname);
    922  1.28   mycroft 		else if ((sc->status & MCD_ST_DSKCHNG) != 0)
    923  1.28   mycroft 			printf("%s: media change\n", sc->sc_dev.dv_xname);
    924  1.28   mycroft 		sc->flags &= ~MCDF_LOADED;
    925   1.1       cgd 	}
    926   1.1       cgd 
    927  1.28   mycroft 	if ((sc->status & MCD_ST_AUDIOBSY) != 0)
    928   1.6   mycroft 		sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
    929  1.28   mycroft 	else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS ||
    930  1.28   mycroft 		 sc->audio_status == CD_AS_AUDIO_INVALID)
    931   1.6   mycroft 		sc->audio_status = CD_AS_PLAY_COMPLETED;
    932   1.1       cgd }
    933   1.1       cgd 
    934   1.6   mycroft int
    935  1.28   mycroft mcd_send(sc, mbx, diskin)
    936   1.6   mycroft 	struct mcd_softc *sc;
    937  1.28   mycroft 	struct mcd_mbox *mbx;
    938  1.28   mycroft 	int diskin;
    939   1.1       cgd {
    940  1.28   mycroft 	int iobase = sc->iobase;
    941  1.28   mycroft 	int retry, i, error;
    942   1.1       cgd 
    943  1.28   mycroft 	if (sc->debug) {
    944  1.28   mycroft 		printf("%s: mcd_send: %d %02x", sc->sc_dev.dv_xname,
    945  1.28   mycroft 		    mbx->cmd.length, (u_int)mbx->cmd.opcode);
    946  1.28   mycroft 		for (i = 0; i < mbx->cmd.length; i++)
    947  1.28   mycroft 			printf(" %02x", (u_int)mbx->cmd.data.raw.data[i]);
    948  1.28   mycroft 		printf("\n");
    949   1.1       cgd 	}
    950   1.1       cgd 
    951  1.28   mycroft 	for (retry = MCD_RETRIES; retry; retry--) {
    952  1.28   mycroft 		outb(iobase + MCD_COMMAND, mbx->cmd.opcode);
    953  1.28   mycroft 		for (i = 0; i < mbx->cmd.length; i++)
    954  1.28   mycroft 			outb(iobase + MCD_COMMAND, mbx->cmd.data.raw.data[i]);
    955  1.28   mycroft 		if ((error = mcd_getresult(sc, &mbx->res)) == 0)
    956   1.1       cgd 			break;
    957  1.28   mycroft 		if (error == EINVAL)
    958  1.28   mycroft 			return error;
    959   1.1       cgd 	}
    960  1.28   mycroft 	if (!retry)
    961  1.28   mycroft 		return error;
    962  1.28   mycroft 	if (diskin && (sc->flags & MCDF_LOADED) == 0)
    963  1.28   mycroft 		return EIO;
    964   1.6   mycroft 
    965   1.1       cgd 	return 0;
    966   1.1       cgd }
    967   1.1       cgd 
    968  1.28   mycroft static int
    969   1.6   mycroft bcd2bin(b)
    970   1.6   mycroft 	bcd_t b;
    971   1.1       cgd {
    972   1.6   mycroft 
    973   1.1       cgd 	return (b >> 4) * 10 + (b & 15);
    974   1.1       cgd }
    975   1.1       cgd 
    976  1.28   mycroft static bcd_t
    977   1.6   mycroft bin2bcd(b)
    978   1.6   mycroft 	int b;
    979   1.1       cgd {
    980   1.6   mycroft 
    981   1.1       cgd 	return ((b / 10) << 4) | (b % 10);
    982   1.1       cgd }
    983   1.1       cgd 
    984  1.28   mycroft static void
    985   1.6   mycroft hsg2msf(hsg, msf)
    986   1.6   mycroft 	int hsg;
    987   1.6   mycroft 	bcd_t *msf;
    988   1.1       cgd {
    989   1.6   mycroft 
    990   1.1       cgd 	hsg += 150;
    991   1.1       cgd 	F_msf(msf) = bin2bcd(hsg % 75);
    992  1.28   mycroft 	hsg /= 75;
    993  1.28   mycroft 	S_msf(msf) = bin2bcd(hsg % 60);
    994  1.28   mycroft 	hsg /= 60;
    995  1.28   mycroft 	M_msf(msf) = bin2bcd(hsg);
    996   1.1       cgd }
    997   1.1       cgd 
    998  1.28   mycroft static daddr_t
    999  1.28   mycroft msf2hsg(msf, relative)
   1000   1.6   mycroft 	bcd_t *msf;
   1001  1.28   mycroft 	int relative;
   1002   1.1       cgd {
   1003  1.28   mycroft 	daddr_t blkno;
   1004   1.6   mycroft 
   1005  1.28   mycroft 	blkno = bcd2bin(M_msf(msf)) * 75 * 60 +
   1006  1.28   mycroft 		bcd2bin(S_msf(msf)) * 75 +
   1007  1.28   mycroft 		bcd2bin(F_msf(msf));
   1008  1.28   mycroft 	if (!relative)
   1009  1.28   mycroft 		blkno -= 150;
   1010  1.28   mycroft 	return blkno;
   1011   1.1       cgd }
   1012   1.1       cgd 
   1013  1.28   mycroft void
   1014  1.48  christos mcd_pseudointr(v)
   1015  1.48  christos 	void *v;
   1016   1.1       cgd {
   1017  1.48  christos 	struct mcd_softc *sc = v;
   1018  1.28   mycroft 	int s;
   1019   1.1       cgd 
   1020  1.28   mycroft 	s = splbio();
   1021  1.29   mycroft 	(void) mcdintr(sc);
   1022  1.28   mycroft 	splx(s);
   1023   1.1       cgd }
   1024   1.1       cgd 
   1025   1.6   mycroft /*
   1026   1.6   mycroft  * State machine to process read requests.
   1027  1.30   mycroft  * Initialize with MCD_S_BEGIN: calculate sizes, and set mode
   1028   1.1       cgd  * MCD_S_WAITMODE: waits for status reply from set mode, set read command
   1029   1.6   mycroft  * MCD_S_WAITREAD: wait for read ready, read data.
   1030   1.1       cgd  */
   1031  1.29   mycroft int
   1032  1.29   mycroft mcdintr(arg)
   1033  1.13       cgd 	void *arg;
   1034  1.13       cgd {
   1035  1.28   mycroft 	struct mcd_softc *sc = arg;
   1036  1.28   mycroft 	struct mcd_mbx *mbx = &sc->mbx;
   1037  1.24   mycroft 	int iobase = sc->iobase;
   1038   1.6   mycroft 	struct buf *bp = mbx->bp;
   1039   1.1       cgd 
   1040  1.28   mycroft 	int i;
   1041  1.28   mycroft 	u_char x;
   1042  1.28   mycroft 	bcd_t msf[3];
   1043   1.1       cgd 
   1044  1.14   mycroft 	switch (mbx->state) {
   1045  1.28   mycroft 	case MCD_S_IDLE:
   1046  1.29   mycroft 		return 0;
   1047  1.28   mycroft 
   1048   1.1       cgd 	case MCD_S_BEGIN:
   1049  1.28   mycroft 	tryagain:
   1050  1.28   mycroft 		if (mbx->mode == sc->lastmode)
   1051  1.28   mycroft 			goto firstblock;
   1052  1.28   mycroft 
   1053  1.28   mycroft 		sc->lastmode = MCD_MD_UNKNOWN;
   1054  1.28   mycroft 		outb(iobase + MCD_COMMAND, MCD_CMDSETMODE);
   1055  1.28   mycroft 		outb(iobase + MCD_COMMAND, mbx->mode);
   1056  1.14   mycroft 
   1057  1.14   mycroft 		mbx->count = RDELAY_WAITMODE;
   1058  1.14   mycroft 		mbx->state = MCD_S_WAITMODE;
   1059  1.14   mycroft 
   1060   1.1       cgd 	case MCD_S_WAITMODE:
   1061  1.28   mycroft 		untimeout(mcd_pseudointr, sc);
   1062  1.28   mycroft 		for (i = 20; i; i--) {
   1063  1.28   mycroft 			x = inb(iobase + MCD_XFER);
   1064  1.28   mycroft 			if ((x & MCD_XF_STATUSUNAVAIL) == 0)
   1065  1.28   mycroft 				break;
   1066  1.28   mycroft 			delay(50);
   1067   1.1       cgd 		}
   1068  1.28   mycroft 		if (i == 0)
   1069  1.28   mycroft 			goto hold;
   1070  1.28   mycroft 		sc->status = inb(iobase + MCD_STATUS);
   1071   1.6   mycroft 		mcd_setflags(sc);
   1072  1.28   mycroft 		if ((sc->flags & MCDF_LOADED) == 0)
   1073  1.28   mycroft 			goto changed;
   1074   1.6   mycroft 		MCD_TRACE("doread: got WAITMODE delay=%d\n",
   1075   1.6   mycroft 		    RDELAY_WAITMODE - mbx->count, 0, 0, 0);
   1076  1.14   mycroft 
   1077  1.28   mycroft 		sc->lastmode = mbx->mode;
   1078   1.1       cgd 
   1079  1.28   mycroft 	firstblock:
   1080  1.28   mycroft 		MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", mbx->blkno,
   1081  1.28   mycroft 		    bp, 0, 0);
   1082   1.1       cgd 
   1083   1.6   mycroft 		/* Build parameter block. */
   1084  1.28   mycroft 		hsg2msf(mbx->blkno, msf);
   1085   1.1       cgd 
   1086   1.6   mycroft 		/* Send the read command. */
   1087  1.41       cgd 		outb(iobase + MCD_COMMAND, sc->readcmd);
   1088  1.28   mycroft 		outb(iobase + MCD_COMMAND, msf[0]);
   1089  1.28   mycroft 		outb(iobase + MCD_COMMAND, msf[1]);
   1090  1.28   mycroft 		outb(iobase + MCD_COMMAND, msf[2]);
   1091  1.28   mycroft 		outb(iobase + MCD_COMMAND, 0);
   1092  1.28   mycroft 		outb(iobase + MCD_COMMAND, 0);
   1093  1.28   mycroft 		outb(iobase + MCD_COMMAND, mbx->nblk);
   1094  1.14   mycroft 
   1095   1.1       cgd 		mbx->count = RDELAY_WAITREAD;
   1096  1.14   mycroft 		mbx->state = MCD_S_WAITREAD;
   1097  1.28   mycroft 
   1098  1.28   mycroft 	case MCD_S_WAITREAD:
   1099  1.28   mycroft 		untimeout(mcd_pseudointr, sc);
   1100  1.28   mycroft 	nextblock:
   1101  1.28   mycroft 	loop:
   1102  1.28   mycroft 		for (i = 20; i; i--) {
   1103  1.28   mycroft 			x = inb(iobase + MCD_XFER);
   1104  1.28   mycroft 			if ((x & MCD_XF_DATAUNAVAIL) == 0)
   1105  1.28   mycroft 				goto gotblock;
   1106  1.28   mycroft 			if ((x & MCD_XF_STATUSUNAVAIL) == 0)
   1107  1.28   mycroft 				break;
   1108  1.28   mycroft 			delay(50);
   1109  1.28   mycroft 		}
   1110  1.28   mycroft 		if (i == 0)
   1111  1.28   mycroft 			goto hold;
   1112  1.28   mycroft 		sc->status = inb(iobase + MCD_STATUS);
   1113  1.28   mycroft 		mcd_setflags(sc);
   1114  1.28   mycroft 		if ((sc->flags & MCDF_LOADED) == 0)
   1115  1.28   mycroft 			goto changed;
   1116  1.28   mycroft #if 0
   1117  1.28   mycroft 		printf("%s: got status byte %02x during read\n",
   1118  1.28   mycroft 		    sc->sc_dev.dv_xname, (u_int)sc->status);
   1119  1.28   mycroft #endif
   1120  1.28   mycroft 		goto loop;
   1121  1.28   mycroft 
   1122  1.28   mycroft 	gotblock:
   1123  1.28   mycroft 		MCD_TRACE("doread: got data delay=%d\n",
   1124  1.28   mycroft 		    RDELAY_WAITREAD - mbx->count, 0, 0, 0);
   1125  1.28   mycroft 
   1126  1.28   mycroft 		/* Data is ready. */
   1127  1.28   mycroft 		outb(iobase + MCD_CTL2, 0x04);	/* XXX */
   1128  1.28   mycroft 		insb(iobase + MCD_RDATA, bp->b_data + mbx->skip, mbx->sz);
   1129  1.28   mycroft 		outb(iobase + MCD_CTL2, 0x0c);	/* XXX */
   1130  1.28   mycroft 		mbx->blkno += 1;
   1131  1.28   mycroft 		mbx->skip += mbx->sz;
   1132  1.28   mycroft 		if (--mbx->nblk > 0)
   1133  1.28   mycroft 			goto nextblock;
   1134  1.28   mycroft 
   1135  1.28   mycroft 		mbx->state = MCD_S_IDLE;
   1136  1.28   mycroft 
   1137  1.28   mycroft 		/* Return buffer. */
   1138  1.28   mycroft 		bp->b_resid = 0;
   1139  1.44   thorpej 		disk_unbusy(&sc->sc_dk, bp->b_bcount);
   1140  1.28   mycroft 		biodone(bp);
   1141  1.28   mycroft 
   1142  1.28   mycroft 		mcdstart(sc);
   1143  1.29   mycroft 		return 1;
   1144   1.6   mycroft 
   1145  1.28   mycroft 	hold:
   1146  1.14   mycroft 		if (mbx->count-- < 0) {
   1147  1.28   mycroft 			printf("%s: timeout in state %d",
   1148  1.28   mycroft 			    sc->sc_dev.dv_xname, mbx->state);
   1149   1.1       cgd 			goto readerr;
   1150   1.1       cgd 		}
   1151  1.14   mycroft 
   1152  1.28   mycroft #if 0
   1153  1.28   mycroft 		printf("%s: sleep in state %d\n", sc->sc_dev.dv_xname,
   1154  1.28   mycroft 		    mbx->state);
   1155  1.28   mycroft #endif
   1156  1.28   mycroft 		timeout(mcd_pseudointr, sc, hz / 100);
   1157  1.29   mycroft 		return -1;
   1158   1.1       cgd 	}
   1159   1.1       cgd 
   1160   1.1       cgd readerr:
   1161   1.1       cgd 	if (mbx->retry-- > 0) {
   1162  1.28   mycroft 		printf("; retrying\n");
   1163  1.28   mycroft 		goto tryagain;
   1164  1.28   mycroft 	} else
   1165  1.28   mycroft 		printf("; giving up\n");
   1166   1.1       cgd 
   1167  1.28   mycroft changed:
   1168   1.6   mycroft 	/* Invalidate the buffer. */
   1169   1.1       cgd 	bp->b_flags |= B_ERROR;
   1170  1.28   mycroft 	bp->b_resid = bp->b_bcount - mbx->skip;
   1171  1.44   thorpej 	disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid));
   1172   1.1       cgd 	biodone(bp);
   1173  1.28   mycroft 
   1174  1.28   mycroft 	mcdstart(sc);
   1175  1.29   mycroft 	return -1;
   1176   1.1       cgd 
   1177   1.6   mycroft #ifdef notyet
   1178   1.6   mycroft 	printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname);
   1179  1.28   mycroft 	outb(mbx->iobase + MCD_RESET, MCD_CMDRESET);
   1180   1.7   mycroft 	delay(300000);
   1181  1.28   mycroft 	(void) mcd_getstat(sc, 1);
   1182  1.28   mycroft 	(void) mcd_getstat(sc, 1);
   1183  1.28   mycroft 	/*sc->status &= ~MCD_ST_DSKCHNG; */
   1184   1.6   mycroft 	sc->debug = 1; /* preventive set debug mode */
   1185   1.1       cgd #endif
   1186   1.1       cgd }
   1187   1.1       cgd 
   1188  1.28   mycroft void
   1189  1.28   mycroft mcd_soft_reset(sc)
   1190  1.28   mycroft 	struct mcd_softc *sc;
   1191  1.28   mycroft {
   1192  1.28   mycroft 
   1193  1.29   mycroft 	sc->debug = 0;
   1194  1.28   mycroft 	sc->flags = 0;
   1195  1.28   mycroft 	sc->lastmode = MCD_MD_UNKNOWN;
   1196  1.28   mycroft 	sc->lastupc = MCD_UPC_UNKNOWN;
   1197  1.28   mycroft 	sc->audio_status = CD_AS_AUDIO_INVALID;
   1198  1.28   mycroft 	outb(sc->iobase + MCD_CTL2, 0x0c);	/* XXX */
   1199  1.28   mycroft }
   1200  1.28   mycroft 
   1201  1.28   mycroft int
   1202  1.28   mycroft mcd_hard_reset(sc)
   1203  1.28   mycroft 	struct mcd_softc *sc;
   1204  1.28   mycroft {
   1205  1.28   mycroft 	struct mcd_mbox mbx;
   1206  1.28   mycroft 
   1207  1.28   mycroft 	mcd_soft_reset(sc);
   1208  1.28   mycroft 
   1209  1.28   mycroft 	mbx.cmd.opcode = MCD_CMDRESET;
   1210  1.28   mycroft 	mbx.cmd.length = 0;
   1211  1.28   mycroft 	mbx.res.length = 0;
   1212  1.28   mycroft 	return mcd_send(sc, &mbx, 0);
   1213  1.28   mycroft }
   1214  1.28   mycroft 
   1215   1.6   mycroft int
   1216   1.6   mycroft mcd_setmode(sc, mode)
   1217   1.6   mycroft 	struct mcd_softc *sc;
   1218   1.6   mycroft 	int mode;
   1219   1.1       cgd {
   1220  1.28   mycroft 	struct mcd_mbox mbx;
   1221  1.28   mycroft 	int error;
   1222  1.28   mycroft 
   1223  1.28   mycroft 	if (sc->lastmode == mode)
   1224  1.28   mycroft 		return 0;
   1225  1.28   mycroft 	if (sc->debug)
   1226  1.28   mycroft 		printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode);
   1227  1.28   mycroft 	sc->lastmode = MCD_MD_UNKNOWN;
   1228  1.28   mycroft 
   1229  1.28   mycroft 	mbx.cmd.opcode = MCD_CMDSETMODE;
   1230  1.28   mycroft 	mbx.cmd.length = sizeof(mbx.cmd.data.datamode);
   1231  1.28   mycroft 	mbx.cmd.data.datamode.mode = mode;
   1232  1.28   mycroft 	mbx.res.length = 0;
   1233  1.28   mycroft 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
   1234  1.28   mycroft 		return error;
   1235  1.28   mycroft 
   1236  1.28   mycroft 	sc->lastmode = mode;
   1237  1.28   mycroft 	return 0;
   1238  1.28   mycroft }
   1239  1.28   mycroft 
   1240  1.28   mycroft int
   1241  1.28   mycroft mcd_setupc(sc, upc)
   1242  1.28   mycroft 	struct mcd_softc *sc;
   1243  1.28   mycroft 	int upc;
   1244  1.28   mycroft {
   1245  1.28   mycroft 	struct mcd_mbox mbx;
   1246  1.28   mycroft 	int error;
   1247  1.28   mycroft 
   1248  1.28   mycroft 	if (sc->lastupc == upc)
   1249  1.28   mycroft 		return 0;
   1250  1.28   mycroft 	if (sc->debug)
   1251  1.28   mycroft 		printf("%s: setting upc to %d\n", sc->sc_dev.dv_xname, upc);
   1252  1.28   mycroft 	sc->lastupc = MCD_UPC_UNKNOWN;
   1253   1.1       cgd 
   1254  1.28   mycroft 	mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
   1255  1.28   mycroft 	mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
   1256  1.28   mycroft 	mbx.cmd.data.config.subcommand = MCD_CF_READUPC;
   1257  1.28   mycroft 	mbx.cmd.data.config.data1 = upc;
   1258  1.28   mycroft 	mbx.res.length = 0;
   1259  1.28   mycroft 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
   1260  1.28   mycroft 		return error;
   1261   1.1       cgd 
   1262  1.28   mycroft 	sc->lastupc = upc;
   1263  1.28   mycroft 	return 0;
   1264   1.1       cgd }
   1265   1.1       cgd 
   1266   1.6   mycroft int
   1267   1.6   mycroft mcd_toc_header(sc, th)
   1268   1.6   mycroft 	struct mcd_softc *sc;
   1269   1.6   mycroft 	struct ioc_toc_header *th;
   1270   1.1       cgd {
   1271   1.1       cgd 
   1272  1.28   mycroft 	if (sc->debug)
   1273  1.28   mycroft 		printf("%s: mcd_toc_header: reading toc header\n",
   1274  1.28   mycroft 		    sc->sc_dev.dv_xname);
   1275   1.1       cgd 
   1276  1.28   mycroft 	th->len = msf2hsg(sc->volinfo.vol_msf, 0);
   1277   1.6   mycroft 	th->starting_track = bcd2bin(sc->volinfo.trk_low);
   1278   1.6   mycroft 	th->ending_track = bcd2bin(sc->volinfo.trk_high);
   1279   1.1       cgd 
   1280   1.1       cgd 	return 0;
   1281   1.1       cgd }
   1282   1.1       cgd 
   1283   1.6   mycroft int
   1284   1.6   mycroft mcd_read_toc(sc)
   1285   1.6   mycroft 	struct mcd_softc *sc;
   1286   1.1       cgd {
   1287   1.1       cgd 	struct ioc_toc_header th;
   1288  1.28   mycroft 	union mcd_qchninfo q;
   1289  1.28   mycroft 	int error, trk, idx, retry;
   1290   1.1       cgd 
   1291  1.28   mycroft 	if ((error = mcd_toc_header(sc, &th)) != 0)
   1292  1.28   mycroft 		return error;
   1293   1.1       cgd 
   1294  1.28   mycroft 	if ((error = mcd_stop(sc)) != 0)
   1295  1.28   mycroft 		return error;
   1296   1.1       cgd 
   1297   1.6   mycroft 	if (sc->debug)
   1298   1.6   mycroft 		printf("%s: read_toc: reading qchannel info\n",
   1299   1.6   mycroft 		    sc->sc_dev.dv_xname);
   1300  1.28   mycroft 
   1301   1.6   mycroft 	for (trk = th.starting_track; trk <= th.ending_track; trk++)
   1302  1.28   mycroft 		sc->toc[trk].toc.idx_no = 0x00;
   1303   1.1       cgd 	trk = th.ending_track - th.starting_track + 1;
   1304   1.6   mycroft 	for (retry = 300; retry && trk > 0; retry--) {
   1305  1.28   mycroft 		if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0)
   1306   1.6   mycroft 			break;
   1307  1.28   mycroft 		if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00)
   1308  1.28   mycroft 			continue;
   1309  1.28   mycroft 		idx = bcd2bin(q.toc.idx_no);
   1310  1.28   mycroft 		if (idx < MCD_MAXTOCS &&
   1311  1.28   mycroft 		    sc->toc[idx].toc.idx_no == 0x00) {
   1312   1.6   mycroft 			sc->toc[idx] = q;
   1313   1.6   mycroft 			trk--;
   1314   1.6   mycroft 		}
   1315   1.1       cgd 	}
   1316   1.1       cgd 
   1317  1.33   mycroft 	/* Inform the drive that we're finished so it turns off the light. */
   1318  1.33   mycroft 	if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
   1319  1.33   mycroft 		return error;
   1320  1.33   mycroft 
   1321   1.6   mycroft 	if (trk != 0)
   1322  1.28   mycroft 		return EINVAL;
   1323   1.1       cgd 
   1324  1.28   mycroft 	/* Add a fake last+1 for mcd_playtracks(). */
   1325   1.1       cgd 	idx = th.ending_track + 1;
   1326  1.28   mycroft 	sc->toc[idx].toc.control = sc->toc[idx-1].toc.control;
   1327  1.28   mycroft 	sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type;
   1328  1.28   mycroft 	sc->toc[idx].toc.trk_no = 0x00;
   1329  1.28   mycroft 	sc->toc[idx].toc.idx_no = 0xaa;
   1330  1.28   mycroft 	sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0];
   1331  1.28   mycroft 	sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1];
   1332  1.28   mycroft 	sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2];
   1333   1.1       cgd 
   1334   1.1       cgd 	return 0;
   1335   1.1       cgd }
   1336   1.1       cgd 
   1337   1.6   mycroft int
   1338  1.28   mycroft mcd_toc_entries(sc, te)
   1339   1.6   mycroft 	struct mcd_softc *sc;
   1340   1.6   mycroft 	struct ioc_read_toc_entry *te;
   1341   1.1       cgd {
   1342  1.28   mycroft 	int len = te->data_len;
   1343   1.6   mycroft 	struct ret_toc {
   1344  1.28   mycroft 		struct ioc_toc_header header;
   1345  1.28   mycroft 		struct cd_toc_entry entries[MCD_MAXTOCS];
   1346  1.28   mycroft 	} data;
   1347  1.28   mycroft 	u_char trk;
   1348  1.28   mycroft 	daddr_t lba;
   1349  1.28   mycroft 	int error, n;
   1350  1.28   mycroft 
   1351  1.28   mycroft 	if (len > sizeof(data.entries) ||
   1352  1.28   mycroft 	    len < sizeof(struct cd_toc_entry))
   1353  1.28   mycroft 		return EINVAL;
   1354  1.28   mycroft 	if (te->address_format != CD_MSF_FORMAT &&
   1355  1.28   mycroft 	    te->address_format != CD_LBA_FORMAT)
   1356  1.28   mycroft 		return EINVAL;
   1357   1.1       cgd 
   1358  1.28   mycroft 	/* Copy the TOC header. */
   1359  1.28   mycroft 	if ((error = mcd_toc_header(sc, &data.header)) != 0)
   1360  1.28   mycroft 		return error;
   1361   1.1       cgd 
   1362   1.6   mycroft 	/* Verify starting track. */
   1363  1.28   mycroft 	trk = te->starting_track;
   1364  1.28   mycroft 	if (trk == 0x00)
   1365  1.28   mycroft 		trk = data.header.starting_track;
   1366  1.28   mycroft 	else if (trk == 0xaa)
   1367  1.28   mycroft 		trk = data.header.ending_track + 1;
   1368  1.28   mycroft 	else if (trk < data.header.starting_track ||
   1369  1.28   mycroft 		 trk > data.header.ending_track + 1)
   1370   1.1       cgd 		return EINVAL;
   1371   1.1       cgd 
   1372   1.6   mycroft 	/* Copy the TOC data. */
   1373  1.28   mycroft 	for (n = 0; trk <= data.header.ending_track + 1; trk++) {
   1374  1.28   mycroft 		if (sc->toc[trk].toc.idx_no == 0x00)
   1375  1.28   mycroft 			continue;
   1376  1.28   mycroft 		data.entries[n].control = sc->toc[trk].toc.control;
   1377  1.28   mycroft 		data.entries[n].addr_type = sc->toc[trk].toc.addr_type;
   1378  1.28   mycroft 		data.entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no);
   1379  1.28   mycroft 		switch (te->address_format) {
   1380  1.28   mycroft 		case CD_MSF_FORMAT:
   1381  1.28   mycroft 			data.entries[n].addr[0] = 0;
   1382  1.28   mycroft 			data.entries[n].addr[1] = bcd2bin(sc->toc[trk].toc.absolute_pos[0]);
   1383  1.28   mycroft 			data.entries[n].addr[2] = bcd2bin(sc->toc[trk].toc.absolute_pos[1]);
   1384  1.28   mycroft 			data.entries[n].addr[3] = bcd2bin(sc->toc[trk].toc.absolute_pos[2]);
   1385  1.28   mycroft 			break;
   1386  1.28   mycroft 		case CD_LBA_FORMAT:
   1387  1.28   mycroft 			lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0);
   1388  1.28   mycroft 			data.entries[n].addr[0] = lba >> 24;
   1389  1.28   mycroft 			data.entries[n].addr[1] = lba >> 16;
   1390  1.28   mycroft 			data.entries[n].addr[2] = lba >> 8;
   1391  1.28   mycroft 			data.entries[n].addr[3] = lba;
   1392  1.28   mycroft 			break;
   1393  1.28   mycroft 		}
   1394  1.28   mycroft 		n++;
   1395   1.1       cgd 	}
   1396   1.1       cgd 
   1397  1.28   mycroft 	len = min(len, n * sizeof(struct cd_toc_entry));
   1398  1.28   mycroft 
   1399   1.6   mycroft 	/* Copy the data back. */
   1400  1.28   mycroft 	return copyout(&data.entries[0], te->data, len);
   1401   1.1       cgd }
   1402   1.1       cgd 
   1403   1.6   mycroft int
   1404   1.6   mycroft mcd_stop(sc)
   1405   1.6   mycroft 	struct mcd_softc *sc;
   1406   1.1       cgd {
   1407  1.28   mycroft 	struct mcd_mbox mbx;
   1408  1.28   mycroft 	int error;
   1409  1.28   mycroft 
   1410  1.28   mycroft 	if (sc->debug)
   1411  1.28   mycroft 		printf("%s: mcd_stop: stopping play\n", sc->sc_dev.dv_xname);
   1412  1.28   mycroft 
   1413  1.28   mycroft 	mbx.cmd.opcode = MCD_CMDSTOPAUDIO;
   1414  1.28   mycroft 	mbx.cmd.length = 0;
   1415  1.28   mycroft 	mbx.res.length = 0;
   1416  1.28   mycroft 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
   1417  1.28   mycroft 		return error;
   1418   1.1       cgd 
   1419   1.6   mycroft 	sc->audio_status = CD_AS_PLAY_COMPLETED;
   1420   1.1       cgd 	return 0;
   1421   1.1       cgd }
   1422   1.1       cgd 
   1423   1.6   mycroft int
   1424  1.28   mycroft mcd_getqchan(sc, q, qchn)
   1425   1.6   mycroft 	struct mcd_softc *sc;
   1426  1.28   mycroft 	union mcd_qchninfo *q;
   1427  1.28   mycroft 	int qchn;
   1428   1.1       cgd {
   1429  1.28   mycroft 	struct mcd_mbox mbx;
   1430  1.28   mycroft 	int error;
   1431  1.28   mycroft 
   1432  1.28   mycroft 	if (qchn == CD_TRACK_INFO) {
   1433  1.28   mycroft 		if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0)
   1434  1.28   mycroft 			return error;
   1435  1.28   mycroft 	} else {
   1436  1.28   mycroft 		if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
   1437  1.28   mycroft 			return error;
   1438  1.28   mycroft 	}
   1439  1.28   mycroft 	if (qchn == CD_MEDIA_CATALOG) {
   1440  1.28   mycroft 		if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0)
   1441  1.28   mycroft 			return error;
   1442  1.28   mycroft 	} else {
   1443  1.28   mycroft 		if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0)
   1444  1.28   mycroft 			return error;
   1445  1.28   mycroft 	}
   1446  1.28   mycroft 
   1447  1.28   mycroft 	mbx.cmd.opcode = MCD_CMDGETQCHN;
   1448  1.28   mycroft 	mbx.cmd.length = 0;
   1449  1.28   mycroft 	mbx.res.length = sizeof(mbx.res.data.qchninfo);
   1450  1.28   mycroft 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
   1451  1.28   mycroft 		return error;
   1452   1.1       cgd 
   1453  1.28   mycroft 	*q = mbx.res.data.qchninfo;
   1454   1.1       cgd 	return 0;
   1455   1.1       cgd }
   1456   1.1       cgd 
   1457   1.6   mycroft int
   1458  1.28   mycroft mcd_read_subchannel(sc, ch)
   1459   1.6   mycroft 	struct mcd_softc *sc;
   1460   1.6   mycroft 	struct ioc_read_subchannel *ch;
   1461   1.1       cgd {
   1462  1.28   mycroft 	int len = ch->data_len;
   1463  1.28   mycroft 	union mcd_qchninfo q;
   1464   1.1       cgd 	struct cd_sub_channel_info data;
   1465  1.28   mycroft 	daddr_t lba;
   1466  1.28   mycroft 	int error;
   1467   1.1       cgd 
   1468   1.6   mycroft 	if (sc->debug)
   1469   1.6   mycroft 		printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname,
   1470   1.6   mycroft 		    ch->address_format, ch->data_format);
   1471   1.1       cgd 
   1472  1.28   mycroft 	if (len > sizeof(data) ||
   1473  1.28   mycroft 	    len < sizeof(struct cd_sub_channel_header))
   1474  1.28   mycroft 		return EINVAL;
   1475  1.28   mycroft 	if (ch->address_format != CD_MSF_FORMAT &&
   1476  1.28   mycroft 	    ch->address_format != CD_LBA_FORMAT)
   1477  1.28   mycroft 		return EINVAL;
   1478  1.28   mycroft 	if (ch->data_format != CD_CURRENT_POSITION &&
   1479  1.28   mycroft 	    ch->data_format != CD_MEDIA_CATALOG)
   1480  1.28   mycroft 		return EINVAL;
   1481  1.28   mycroft 
   1482  1.28   mycroft 	if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0)
   1483  1.28   mycroft 		return error;
   1484   1.1       cgd 
   1485   1.6   mycroft 	data.header.audio_status = sc->audio_status;
   1486  1.28   mycroft 	data.what.media_catalog.data_format = ch->data_format;
   1487  1.28   mycroft 
   1488  1.28   mycroft 	switch (ch->data_format) {
   1489  1.28   mycroft 	case CD_MEDIA_CATALOG:
   1490  1.28   mycroft 		data.what.media_catalog.mc_valid = 1;
   1491  1.28   mycroft #if 0
   1492  1.28   mycroft 		data.what.media_catalog.mc_number =
   1493  1.28   mycroft #endif
   1494  1.28   mycroft 		break;
   1495  1.28   mycroft 
   1496  1.28   mycroft 	case CD_CURRENT_POSITION:
   1497  1.28   mycroft 		data.what.position.track_number = bcd2bin(q.current.trk_no);
   1498  1.28   mycroft 		data.what.position.index_number = bcd2bin(q.current.idx_no);
   1499  1.28   mycroft 		switch (ch->address_format) {
   1500  1.28   mycroft 		case CD_MSF_FORMAT:
   1501  1.28   mycroft 			data.what.position.reladdr[0] = 0;
   1502  1.28   mycroft 			data.what.position.reladdr[1] = bcd2bin(q.current.relative_pos[0]);
   1503  1.28   mycroft 			data.what.position.reladdr[2] = bcd2bin(q.current.relative_pos[1]);
   1504  1.28   mycroft 			data.what.position.reladdr[3] = bcd2bin(q.current.relative_pos[2]);
   1505  1.28   mycroft 			data.what.position.absaddr[0] = 0;
   1506  1.28   mycroft 			data.what.position.absaddr[1] = bcd2bin(q.current.absolute_pos[0]);
   1507  1.28   mycroft 			data.what.position.absaddr[2] = bcd2bin(q.current.absolute_pos[1]);
   1508  1.28   mycroft 			data.what.position.absaddr[3] = bcd2bin(q.current.absolute_pos[2]);
   1509  1.28   mycroft 			break;
   1510  1.28   mycroft 		case CD_LBA_FORMAT:
   1511  1.28   mycroft 			lba = msf2hsg(q.current.relative_pos, 1);
   1512  1.28   mycroft 			/*
   1513  1.28   mycroft 			 * Pre-gap has index number of 0, and decreasing MSF
   1514  1.28   mycroft 			 * address.  Must be converted to negative LBA, per
   1515  1.28   mycroft 			 * SCSI spec.
   1516  1.28   mycroft 			 */
   1517  1.28   mycroft 			if (data.what.position.index_number == 0x00)
   1518  1.28   mycroft 				lba = -lba;
   1519  1.28   mycroft 			data.what.position.reladdr[0] = lba >> 24;
   1520  1.28   mycroft 			data.what.position.reladdr[1] = lba >> 16;
   1521  1.28   mycroft 			data.what.position.reladdr[2] = lba >> 8;
   1522  1.28   mycroft 			data.what.position.reladdr[3] = lba;
   1523  1.28   mycroft 			lba = msf2hsg(q.current.absolute_pos, 0);
   1524  1.28   mycroft 			data.what.position.absaddr[0] = lba >> 24;
   1525  1.28   mycroft 			data.what.position.absaddr[1] = lba >> 16;
   1526  1.28   mycroft 			data.what.position.absaddr[2] = lba >> 8;
   1527  1.28   mycroft 			data.what.position.absaddr[3] = lba;
   1528  1.28   mycroft 			break;
   1529  1.28   mycroft 		}
   1530  1.28   mycroft 		break;
   1531  1.28   mycroft 	}
   1532  1.28   mycroft 
   1533  1.28   mycroft 	return copyout(&data, ch->data, len);
   1534  1.28   mycroft }
   1535  1.28   mycroft 
   1536  1.28   mycroft int
   1537  1.28   mycroft mcd_playtracks(sc, p)
   1538  1.28   mycroft 	struct mcd_softc *sc;
   1539  1.28   mycroft 	struct ioc_play_track *p;
   1540  1.28   mycroft {
   1541  1.28   mycroft 	struct mcd_mbox mbx;
   1542  1.28   mycroft 	int a = p->start_track;
   1543  1.28   mycroft 	int z = p->end_track;
   1544  1.28   mycroft 	int error;
   1545   1.1       cgd 
   1546  1.28   mycroft 	if (sc->debug)
   1547  1.28   mycroft 		printf("%s: playtracks: from %d:%d to %d:%d\n",
   1548  1.28   mycroft 		    sc->sc_dev.dv_xname,
   1549  1.28   mycroft 		    a, p->start_index, z, p->end_index);
   1550  1.28   mycroft 
   1551  1.28   mycroft 	if (a < bcd2bin(sc->volinfo.trk_low) ||
   1552  1.28   mycroft 	    a > bcd2bin(sc->volinfo.trk_high) ||
   1553  1.28   mycroft 	    a > z ||
   1554  1.28   mycroft 	    z < bcd2bin(sc->volinfo.trk_low) ||
   1555  1.28   mycroft 	    z > bcd2bin(sc->volinfo.trk_high))
   1556  1.28   mycroft 		return EINVAL;
   1557  1.28   mycroft 
   1558  1.28   mycroft 	if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
   1559  1.28   mycroft 		return error;
   1560  1.28   mycroft 
   1561  1.41       cgd 	mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
   1562  1.28   mycroft 	mbx.cmd.length = sizeof(mbx.cmd.data.play);
   1563  1.28   mycroft 	mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0];
   1564  1.28   mycroft 	mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1];
   1565  1.28   mycroft 	mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2];
   1566  1.28   mycroft 	mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0];
   1567  1.28   mycroft 	mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1];
   1568  1.28   mycroft 	mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2];
   1569  1.28   mycroft 	sc->lastpb = mbx.cmd;
   1570  1.28   mycroft 	mbx.res.length = 0;
   1571  1.28   mycroft 	return mcd_send(sc, &mbx, 1);
   1572   1.1       cgd }
   1573   1.1       cgd 
   1574   1.6   mycroft int
   1575  1.28   mycroft mcd_playmsf(sc, p)
   1576   1.6   mycroft 	struct mcd_softc *sc;
   1577  1.28   mycroft 	struct ioc_play_msf *p;
   1578   1.1       cgd {
   1579  1.28   mycroft 	struct mcd_mbox mbx;
   1580  1.28   mycroft 	int error;
   1581   1.1       cgd 
   1582  1.28   mycroft 	if (sc->debug)
   1583  1.28   mycroft 		printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n",
   1584  1.28   mycroft 		    sc->sc_dev.dv_xname,
   1585  1.28   mycroft 		    p->start_m, p->start_s, p->start_f,
   1586  1.28   mycroft 		    p->end_m, p->end_s, p->end_f);
   1587   1.1       cgd 
   1588  1.28   mycroft 	if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
   1589  1.28   mycroft 	    (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f))
   1590   1.1       cgd 		return EINVAL;
   1591   1.1       cgd 
   1592  1.28   mycroft 	if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
   1593  1.28   mycroft 		return error;
   1594   1.6   mycroft 
   1595  1.41       cgd 	mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
   1596  1.28   mycroft 	mbx.cmd.length = sizeof(mbx.cmd.data.play);
   1597  1.28   mycroft 	mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m);
   1598  1.28   mycroft 	mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s);
   1599  1.28   mycroft 	mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f);
   1600  1.28   mycroft 	mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m);
   1601  1.28   mycroft 	mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s);
   1602  1.28   mycroft 	mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f);
   1603  1.28   mycroft 	sc->lastpb = mbx.cmd;
   1604  1.28   mycroft 	mbx.res.length = 0;
   1605  1.28   mycroft 	return mcd_send(sc, &mbx, 1);
   1606   1.6   mycroft }
   1607   1.6   mycroft 
   1608   1.6   mycroft int
   1609  1.28   mycroft mcd_playblocks(sc, p)
   1610   1.6   mycroft 	struct mcd_softc *sc;
   1611  1.28   mycroft 	struct ioc_play_blocks *p;
   1612   1.1       cgd {
   1613  1.28   mycroft 	struct mcd_mbox mbx;
   1614  1.28   mycroft 	int error;
   1615   1.1       cgd 
   1616   1.6   mycroft 	if (sc->debug)
   1617  1.28   mycroft 		printf("%s: playblocks: blkno %d length %d\n",
   1618  1.28   mycroft 		    sc->sc_dev.dv_xname, p->blk, p->len);
   1619  1.28   mycroft 
   1620  1.28   mycroft 	if (p->blk > sc->disksize || p->len > sc->disksize ||
   1621  1.28   mycroft 	    (p->blk + p->len) > sc->disksize)
   1622  1.28   mycroft 		return 0;
   1623  1.28   mycroft 
   1624  1.28   mycroft 	if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
   1625  1.28   mycroft 		return error;
   1626   1.6   mycroft 
   1627  1.41       cgd 	mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
   1628  1.28   mycroft 	mbx.cmd.length = sizeof(mbx.cmd.data.play);
   1629  1.28   mycroft 	hsg2msf(p->blk, mbx.cmd.data.play.start_msf);
   1630  1.28   mycroft 	hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf);
   1631  1.28   mycroft 	sc->lastpb = mbx.cmd;
   1632  1.28   mycroft 	mbx.res.length = 0;
   1633  1.28   mycroft 	return mcd_send(sc, &mbx, 1);
   1634   1.1       cgd }
   1635   1.1       cgd 
   1636   1.6   mycroft int
   1637   1.6   mycroft mcd_pause(sc)
   1638   1.6   mycroft 	struct mcd_softc *sc;
   1639   1.1       cgd {
   1640  1.28   mycroft 	union mcd_qchninfo q;
   1641  1.28   mycroft 	int error;
   1642   1.1       cgd 
   1643   1.6   mycroft 	/* Verify current status. */
   1644   1.6   mycroft 	if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS)	{
   1645   1.6   mycroft 		printf("%s: pause: attempted when not playing\n",
   1646   1.6   mycroft 		    sc->sc_dev.dv_xname);
   1647   1.1       cgd 		return EINVAL;
   1648   1.1       cgd 	}
   1649   1.1       cgd 
   1650   1.6   mycroft 	/* Get the current position. */
   1651  1.28   mycroft 	if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0)
   1652  1.28   mycroft 		return error;
   1653   1.1       cgd 
   1654   1.6   mycroft 	/* Copy it into lastpb. */
   1655  1.28   mycroft 	sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0];
   1656  1.28   mycroft 	sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1];
   1657  1.28   mycroft 	sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2];
   1658   1.1       cgd 
   1659   1.6   mycroft 	/* Stop playing. */
   1660  1.28   mycroft 	if ((error = mcd_stop(sc)) != 0)
   1661  1.28   mycroft 		return error;
   1662   1.1       cgd 
   1663   1.6   mycroft 	/* Set the proper status and exit. */
   1664   1.6   mycroft 	sc->audio_status = CD_AS_PLAY_PAUSED;
   1665   1.1       cgd 	return 0;
   1666   1.1       cgd }
   1667   1.1       cgd 
   1668   1.6   mycroft int
   1669   1.6   mycroft mcd_resume(sc)
   1670   1.6   mycroft 	struct mcd_softc *sc;
   1671   1.1       cgd {
   1672  1.28   mycroft 	struct mcd_mbox mbx;
   1673  1.28   mycroft 	int error;
   1674   1.1       cgd 
   1675   1.6   mycroft 	if (sc->audio_status != CD_AS_PLAY_PAUSED)
   1676   1.6   mycroft 		return EINVAL;
   1677  1.28   mycroft 
   1678  1.28   mycroft 	if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
   1679  1.28   mycroft 		return error;
   1680  1.28   mycroft 
   1681  1.28   mycroft 	mbx.cmd = sc->lastpb;
   1682  1.28   mycroft 	mbx.res.length = 0;
   1683  1.28   mycroft 	return mcd_send(sc, &mbx, 1);
   1684  1.28   mycroft }
   1685  1.28   mycroft 
   1686  1.28   mycroft int
   1687  1.28   mycroft mcd_eject(sc)
   1688  1.28   mycroft 	struct mcd_softc *sc;
   1689  1.28   mycroft {
   1690  1.28   mycroft 	struct mcd_mbox mbx;
   1691  1.28   mycroft 
   1692  1.28   mycroft 	mbx.cmd.opcode = MCD_CMDEJECTDISK;
   1693  1.28   mycroft 	mbx.cmd.length = 0;
   1694  1.28   mycroft 	mbx.res.length = 0;
   1695  1.28   mycroft 	return mcd_send(sc, &mbx, 0);
   1696  1.28   mycroft }
   1697  1.28   mycroft 
   1698  1.28   mycroft int
   1699  1.28   mycroft mcd_setlock(sc, mode)
   1700  1.28   mycroft 	struct mcd_softc *sc;
   1701  1.28   mycroft 	int mode;
   1702  1.28   mycroft {
   1703  1.28   mycroft 	struct mcd_mbox mbx;
   1704  1.28   mycroft 
   1705  1.28   mycroft 	mbx.cmd.opcode = MCD_CMDSETLOCK;
   1706  1.28   mycroft 	mbx.cmd.length = sizeof(mbx.cmd.data.lockmode);
   1707  1.28   mycroft 	mbx.cmd.data.lockmode.mode = mode;
   1708  1.28   mycroft 	mbx.res.length = 0;
   1709  1.28   mycroft 	return mcd_send(sc, &mbx, 1);
   1710   1.1       cgd }
   1711