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