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