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