Subject: Mitsumi CD users wanted
To: None <current-users@NetBSD.ORG, port-i386@NetBSD.ORG>
From: Perry E. Metzger <perry@piermont.com>
List: current-users
Date: 03/25/1997 17:46:13
I'd like to find out from anyone out there who might be using the
Mitsumi CD-ROM driver as to whether or not the enclosed re-hash of the
driver works. If it does, we should soon be able to eliminate the
"mcd wrecks SMC Ethernet" screw thanks to io space accounting.
BTW, if you do have a Mitsumi and the driver doesn't work, I'd
appreciate finding out why, since I don't have one. :)
(BTW, thanks to Christos for doing the hard part of this.)
# This is a shell archive. Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file". Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
# mcd.c
# mcdreg.h
#
echo x - mcd.c
sed 's/^X//' >mcd.c << 'END-of-mcd.c'
X/* $NetBSD: mcd.c,v 1.52 1996/11/05 07:17:25 mikel Exp $ */
X
X/*
X * Copyright (c) 1993, 1994, 1995 Charles M. Hannum. All rights reserved.
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X * notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X * notice, this list of conditions and the following disclaimer in the
X * documentation and/or other materials provided with the distribution.
X * 3. All advertising materials mentioning features or use of this software
X * must display the following acknowledgement:
X * This product includes software developed by Charles M. Hannum.
X * 4. The name of the author may not be used to endorse or promote products
X * derived from this software without specific prior written permission.
X *
X * Copyright 1993 by Holger Veit (data part)
X * Copyright 1993 by Brian Moore (audio part)
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X * notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X * notice, this list of conditions and the following disclaimer in the
X * documentation and/or other materials provided with the distribution.
X * 3. All advertising materials mentioning features or use of this software
X * must display the following acknowledgement:
X * This software was developed by Holger Veit and Brian Moore
X * for use with "386BSD" and similar operating systems.
X * "Similar operating systems" includes mainly non-profit oriented
X * systems for research and education, including but not restricted to
X * "NetBSD", "FreeBSD", "Mach" (by CMU).
X * 4. Neither the name of the developer(s) nor the name "386BSD"
X * may be used to endorse or promote products derived from this
X * software without specific prior written permission.
X *
X * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY
X * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
X * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER(S) BE
X * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
X * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
X * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
X * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
X * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
X * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
X * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
X */
X
X/*static char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";*/
X
X#include <sys/types.h>
X#include <sys/param.h>
X#include <sys/systm.h>
X#include <sys/kernel.h>
X#include <sys/proc.h>
X#include <sys/conf.h>
X#include <sys/file.h>
X#include <sys/buf.h>
X#include <sys/stat.h>
X#include <sys/uio.h>
X#include <sys/ioctl.h>
X#include <sys/cdio.h>
X#include <sys/errno.h>
X#include <sys/disklabel.h>
X#include <sys/device.h>
X#include <sys/disk.h>
X
X#include <machine/cpu.h>
X#include <machine/intr.h>
X#include <machine/bus.h>
X
X#include <dev/isa/isavar.h>
X#include <dev/isa/mcdreg.h>
X
X#ifndef MCDDEBUG
X#define MCD_TRACE(fmt,a,b,c,d)
X#else
X#define MCD_TRACE(fmt,a,b,c,d) {if (sc->debug) {printf("%s: st=%02x: ", sc->sc_dev.dv_xname, sc->status); printf(fmt,a,b,c,d);}}
X#endif
X
X#define MCDPART(dev) DISKPART(dev)
X#define MCDUNIT(dev) DISKUNIT(dev)
X
X/* toc */
X#define MCD_MAXTOCS 104 /* from the Linux driver */
X
Xstruct mcd_mbx {
X int retry, count;
X struct buf *bp;
X daddr_t blkno;
X int nblk;
X int sz;
X u_long skip;
X int state;
X#define MCD_S_IDLE 0
X#define MCD_S_BEGIN 1
X#define MCD_S_WAITMODE 2
X#define MCD_S_WAITREAD 3
X int mode;
X};
X
Xstruct mcd_softc {
X struct device sc_dev;
X struct disk sc_dk;
X void *sc_ih;
X
X bus_space_tag_t sc_iot;
X bus_space_handle_t sc_ioh;
X
X int irq, drq;
X
X char *type;
X u_char readcmd;
X int flags;
X#define MCDF_LOCKED 0x01
X#define MCDF_WANTED 0x02
X#define MCDF_WLABEL 0x04 /* label is writable */
X#define MCDF_LABELLING 0x08 /* writing label */
X#define MCDF_LOADED 0x10 /* parameters loaded */
X short status;
X short audio_status;
X int blksize;
X u_long disksize;
X struct mcd_volinfo volinfo;
X union mcd_qchninfo toc[MCD_MAXTOCS];
X struct mcd_command lastpb;
X struct mcd_mbx mbx;
X int lastmode;
X#define MCD_MD_UNKNOWN -1
X int lastupc;
X#define MCD_UPC_UNKNOWN -1
X int debug;
X struct buf buf_queue;
X};
X
X/* prototypes */
X/* XXX does not belong here */
Xcdev_decl(mcd);
Xbdev_decl(mcd);
X
Xstatic int bcd2bin __P((bcd_t));
Xstatic bcd_t bin2bcd __P((int));
Xstatic void hsg2msf __P((int, bcd_t *));
Xstatic daddr_t msf2hsg __P((bcd_t *, int));
X
Xint mcd_playtracks __P((struct mcd_softc *, struct ioc_play_track *));
Xint mcd_playmsf __P((struct mcd_softc *, struct ioc_play_msf *));
Xint mcd_playblocks __P((struct mcd_softc *, struct ioc_play_blocks *));
Xint mcd_stop __P((struct mcd_softc *));
Xint mcd_eject __P((struct mcd_softc *));
Xint mcd_read_subchannel __P((struct mcd_softc *, struct ioc_read_subchannel *));
Xint mcd_pause __P((struct mcd_softc *));
Xint mcd_resume __P((struct mcd_softc *));
Xint mcd_toc_header __P((struct mcd_softc *, struct ioc_toc_header *));
Xint mcd_toc_entries __P((struct mcd_softc *, struct ioc_read_toc_entry *));
X
Xint mcd_getreply __P((struct mcd_softc *));
Xint mcd_getstat __P((struct mcd_softc *));
Xint mcd_getresult __P((struct mcd_softc *, struct mcd_result *));
Xvoid mcd_setflags __P((struct mcd_softc *));
Xint mcd_get __P((struct mcd_softc *, char *, int));
Xint mcd_send __P((struct mcd_softc *, struct mcd_mbox *, int));
Xint mcdintr __P((void *));
Xvoid mcd_soft_reset __P((struct mcd_softc *));
Xint mcd_hard_reset __P((struct mcd_softc *));
Xint mcd_setmode __P((struct mcd_softc *, int));
Xint mcd_setupc __P((struct mcd_softc *, int));
Xint mcd_read_toc __P((struct mcd_softc *));
Xint mcd_getqchan __P((struct mcd_softc *, union mcd_qchninfo *, int));
Xint mcd_setlock __P((struct mcd_softc *, int));
X
Xint mcd_find __P((bus_space_tag_t, bus_space_handle_t, struct mcd_softc *));
Xint mcdprobe __P((struct device *, void *, void *));
Xvoid mcdattach __P((struct device *, struct device *, void *));
X
Xstruct cfattach mcd_ca = {
X sizeof(struct mcd_softc), mcdprobe, mcdattach
X};
X
Xstruct cfdriver mcd_cd = {
X NULL, "mcd", DV_DISK
X};
X
Xvoid mcdgetdisklabel __P((struct mcd_softc *));
Xint mcd_get_parms __P((struct mcd_softc *));
Xvoid mcdstrategy __P((struct buf *));
Xvoid mcdstart __P((struct mcd_softc *));
Xint mcdlock __P((struct mcd_softc *));
Xvoid mcdunlock __P((struct mcd_softc *));
Xvoid mcd_pseudointr __P((void *));
X
Xstruct dkdriver mcddkdriver = { mcdstrategy };
X
X#define MCD_RETRIES 3
X#define MCD_RDRETRIES 3
X
X/* several delays */
X#define RDELAY_WAITMODE 300
X#define RDELAY_WAITREAD 800
X
X#define DELAY_GRANULARITY 25 /* 25us */
X#define DELAY_GETREPLY 100000 /* 100000 * 25us */
X
Xvoid
Xmcdattach(parent, self, aux)
X struct device *parent, *self;
X void *aux;
X{
X struct mcd_softc *sc = (void *)self;
X struct isa_attach_args *ia = aux;
X bus_space_tag_t iot = ia->ia_iot;
X bus_space_handle_t ioh;
X struct mcd_mbox mbx;
X
X /* Map i/o space */
X if (bus_space_map(iot, ia->ia_iobase, MCD_NPORT, 0, &ioh))
X panic("mcdattach: bus_space_map failed!");
X
X sc->sc_iot = iot;
X sc->sc_ioh = ioh;
X
X if (!mcd_find(iot, ioh, sc))
X panic("mcdattach: mcd_find failed!");
X
X /*
X * Initialize and attach the disk structure.
X */
X sc->sc_dk.dk_driver = &mcddkdriver;
X sc->sc_dk.dk_name = sc->sc_dev.dv_xname;
X disk_attach(&sc->sc_dk);
X
X printf(": model %s\n", sc->type != 0 ? sc->type : "unknown");
X
X (void) mcd_setlock(sc, MCD_LK_UNLOCK);
X
X mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
X mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
X mbx.cmd.data.config.subcommand = MCD_CF_IRQENABLE;
X mbx.cmd.data.config.data1 = 0x01;
X mbx.res.length = 0;
X (void) mcd_send(sc, &mbx, 0);
X
X mcd_soft_reset(sc);
X
X sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
X IPL_BIO, mcdintr, sc);
X}
X
X/*
X * Wait interruptibly for an exclusive lock.
X *
X * XXX
X * Several drivers do this; it should be abstracted and made MP-safe.
X */
Xint
Xmcdlock(sc)
X struct mcd_softc *sc;
X{
X int error;
X
X while ((sc->flags & MCDF_LOCKED) != 0) {
X sc->flags |= MCDF_WANTED;
X if ((error = tsleep(sc, PRIBIO | PCATCH, "mcdlck", 0)) != 0)
X return error;
X }
X sc->flags |= MCDF_LOCKED;
X return 0;
X}
X
X/*
X * Unlock and wake up any waiters.
X */
Xvoid
Xmcdunlock(sc)
X struct mcd_softc *sc;
X{
X
X sc->flags &= ~MCDF_LOCKED;
X if ((sc->flags & MCDF_WANTED) != 0) {
X sc->flags &= ~MCDF_WANTED;
X wakeup(sc);
X }
X}
X
Xint
Xmcdopen(dev, flag, fmt, p)
X dev_t dev;
X int flag, fmt;
X struct proc *p;
X{
X int error;
X int unit, part;
X struct mcd_softc *sc;
X
X unit = MCDUNIT(dev);
X if (unit >= mcd_cd.cd_ndevs)
X return ENXIO;
X sc = mcd_cd.cd_devs[unit];
X if (!sc)
X return ENXIO;
X
X if ((error = mcdlock(sc)) != 0)
X return error;
X
X if (sc->sc_dk.dk_openmask != 0) {
X /*
X * If any partition is open, but the disk has been invalidated,
X * disallow further opens.
X */
X if ((sc->flags & MCDF_LOADED) == 0) {
X error = EIO;
X goto bad3;
X }
X } else {
X /*
X * Lock the drawer. This will also notice any pending disk
X * change or door open indicator and clear the MCDF_LOADED bit
X * if necessary.
X */
X (void) mcd_setlock(sc, MCD_LK_LOCK);
X
X if ((sc->flags & MCDF_LOADED) == 0) {
X /* Partially reset the state. */
X sc->lastmode = MCD_MD_UNKNOWN;
X sc->lastupc = MCD_UPC_UNKNOWN;
X
X sc->flags |= MCDF_LOADED;
X
X /* Set the mode, causing the disk to spin up. */
X if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
X goto bad2;
X
X /* Load the physical device parameters. */
X if (mcd_get_parms(sc) != 0) {
X error = ENXIO;
X goto bad2;
X }
X
X /* Read the table of contents. */
X if ((error = mcd_read_toc(sc)) != 0)
X goto bad2;
X
X /* Fabricate a disk label. */
X mcdgetdisklabel(sc);
X }
X }
X
X MCD_TRACE("open: partition=%d disksize=%d blksize=%d\n", part,
X sc->disksize, sc->blksize, 0);
X
X part = MCDPART(dev);
X
X /* Check that the partition exists. */
X if (part != RAW_PART &&
X (part >= sc->sc_dk.dk_label->d_npartitions ||
X sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
X error = ENXIO;
X goto bad;
X }
X
X /* Insure only one open at a time. */
X switch (fmt) {
X case S_IFCHR:
X sc->sc_dk.dk_copenmask |= (1 << part);
X break;
X case S_IFBLK:
X sc->sc_dk.dk_bopenmask |= (1 << part);
X break;
X }
X sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
X
X mcdunlock(sc);
X return 0;
X
Xbad2:
X sc->flags &= ~MCDF_LOADED;
X
Xbad:
X if (sc->sc_dk.dk_openmask == 0) {
X#if 0
X (void) mcd_setmode(sc, MCD_MD_SLEEP);
X#endif
X (void) mcd_setlock(sc, MCD_LK_UNLOCK);
X }
X
Xbad3:
X mcdunlock(sc);
X return error;
X}
X
Xint
Xmcdclose(dev, flag, fmt, p)
X dev_t dev;
X int flag, fmt;
X struct proc *p;
X{
X struct mcd_softc *sc = mcd_cd.cd_devs[MCDUNIT(dev)];
X int part = MCDPART(dev);
X int error;
X
X MCD_TRACE("close: partition=%d\n", part, 0, 0, 0);
X
X if ((error = mcdlock(sc)) != 0)
X return error;
X
X switch (fmt) {
X case S_IFCHR:
X sc->sc_dk.dk_copenmask &= ~(1 << part);
X break;
X case S_IFBLK:
X sc->sc_dk.dk_bopenmask &= ~(1 << part);
X break;
X }
X sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
X
X if (sc->sc_dk.dk_openmask == 0) {
X /* XXXX Must wait for I/O to complete! */
X
X#if 0
X (void) mcd_setmode(sc, MCD_MD_SLEEP);
X#endif
X (void) mcd_setlock(sc, MCD_LK_UNLOCK);
X }
X
X mcdunlock(sc);
X return 0;
X}
X
Xvoid
Xmcdstrategy(bp)
X struct buf *bp;
X{
X struct mcd_softc *sc = mcd_cd.cd_devs[MCDUNIT(bp->b_dev)];
X int s;
X
X /* Test validity. */
X MCD_TRACE("strategy: buf=0x%lx blkno=%ld bcount=%ld\n", bp,
X bp->b_blkno, bp->b_bcount, 0);
X if (bp->b_blkno < 0 ||
X (bp->b_bcount % sc->blksize) != 0) {
X printf("%s: strategy: blkno = %d bcount = %ld\n",
X sc->sc_dev.dv_xname, bp->b_blkno, bp->b_bcount);
X bp->b_error = EINVAL;
X goto bad;
X }
X
X /* If device invalidated (e.g. media change, door open), error. */
X if ((sc->flags & MCDF_LOADED) == 0) {
X MCD_TRACE("strategy: drive not valid\n", 0, 0, 0, 0);
X bp->b_error = EIO;
X goto bad;
X }
X
X /* No data to read. */
X if (bp->b_bcount == 0)
X goto done;
X
X /*
X * Do bounds checking, adjust transfer. if error, process.
X * If end of partition, just return.
X */
X if (MCDPART(bp->b_dev) != RAW_PART &&
X bounds_check_with_label(bp, sc->sc_dk.dk_label,
X (sc->flags & (MCDF_WLABEL|MCDF_LABELLING)) != 0) <= 0)
X goto done;
X
X /* Queue it. */
X s = splbio();
X disksort(&sc->buf_queue, bp);
X splx(s);
X if (!sc->buf_queue.b_active)
X mcdstart(sc);
X return;
X
Xbad:
X bp->b_flags |= B_ERROR;
Xdone:
X bp->b_resid = bp->b_bcount;
X biodone(bp);
X}
X
Xvoid
Xmcdstart(sc)
X struct mcd_softc *sc;
X{
X struct buf *bp, *dp = &sc->buf_queue;
X int s;
X
Xloop:
X s = splbio();
X
X bp = dp->b_actf;
X if (bp == NULL) {
X /* Nothing to do. */
X dp->b_active = 0;
X splx(s);
X return;
X }
X
X /* Block found to process; dequeue. */
X MCD_TRACE("start: found block bp=0x%x\n", bp, 0, 0, 0);
X dp->b_actf = bp->b_actf;
X splx(s);
X
X /* Changed media? */
X if ((sc->flags & MCDF_LOADED) == 0) {
X MCD_TRACE("start: drive not valid\n", 0, 0, 0, 0);
X bp->b_error = EIO;
X bp->b_flags |= B_ERROR;
X biodone(bp);
X goto loop;
X }
X
X dp->b_active = 1;
X
X /* Instrumentation. */
X s = splbio();
X disk_busy(&sc->sc_dk);
X splx(s);
X
X sc->mbx.retry = MCD_RDRETRIES;
X sc->mbx.bp = bp;
X sc->mbx.blkno = bp->b_blkno / (sc->blksize / DEV_BSIZE);
X if (MCDPART(bp->b_dev) != RAW_PART) {
X struct partition *p;
X p = &sc->sc_dk.dk_label->d_partitions[MCDPART(bp->b_dev)];
X sc->mbx.blkno += p->p_offset;
X }
X sc->mbx.nblk = bp->b_bcount / sc->blksize;
X sc->mbx.sz = sc->blksize;
X sc->mbx.skip = 0;
X sc->mbx.state = MCD_S_BEGIN;
X sc->mbx.mode = MCD_MD_COOKED;
X
X s = splbio();
X (void) mcdintr(sc);
X splx(s);
X}
X
Xint
Xmcdread(dev, uio, flags)
X dev_t dev;
X struct uio *uio;
X int flags;
X{
X
X return (physio(mcdstrategy, NULL, dev, B_READ, minphys, uio));
X}
X
Xint
Xmcdwrite(dev, uio, flags)
X dev_t dev;
X struct uio *uio;
X int flags;
X{
X
X return (physio(mcdstrategy, NULL, dev, B_WRITE, minphys, uio));
X}
X
Xint
Xmcdioctl(dev, cmd, addr, flag, p)
X dev_t dev;
X u_long cmd;
X caddr_t addr;
X int flag;
X struct proc *p;
X{
X struct mcd_softc *sc = mcd_cd.cd_devs[MCDUNIT(dev)];
X int error;
X
X MCD_TRACE("ioctl: cmd=0x%x\n", cmd, 0, 0, 0);
X
X if ((sc->flags & MCDF_LOADED) == 0)
X return EIO;
X
X switch (cmd) {
X case DIOCGDINFO:
X *(struct disklabel *)addr = *(sc->sc_dk.dk_label);
X return 0;
X
X case DIOCGPART:
X ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
X ((struct partinfo *)addr)->part =
X &sc->sc_dk.dk_label->d_partitions[MCDPART(dev)];
X return 0;
X
X case DIOCWDINFO:
X case DIOCSDINFO:
X if ((flag & FWRITE) == 0)
X return EBADF;
X
X if ((error = mcdlock(sc)) != 0)
X return error;
X sc->flags |= MCDF_LABELLING;
X
X error = setdisklabel(sc->sc_dk.dk_label,
X (struct disklabel *)addr, /*sc->sc_dk.dk_openmask : */0,
X sc->sc_dk.dk_cpulabel);
X if (error == 0) {
X }
X
X sc->flags &= ~MCDF_LABELLING;
X mcdunlock(sc);
X return error;
X
X case DIOCWLABEL:
X return EBADF;
X
X case CDIOCPLAYTRACKS:
X return mcd_playtracks(sc, (struct ioc_play_track *)addr);
X case CDIOCPLAYMSF:
X return mcd_playmsf(sc, (struct ioc_play_msf *)addr);
X case CDIOCPLAYBLOCKS:
X return mcd_playblocks(sc, (struct ioc_play_blocks *)addr);
X case CDIOCREADSUBCHANNEL:
X return mcd_read_subchannel(sc, (struct ioc_read_subchannel *)addr);
X case CDIOREADTOCHEADER:
X return mcd_toc_header(sc, (struct ioc_toc_header *)addr);
X case CDIOREADTOCENTRYS:
X return mcd_toc_entries(sc, (struct ioc_read_toc_entry *)addr);
X case CDIOCSETPATCH:
X case CDIOCGETVOL:
X case CDIOCSETVOL:
X case CDIOCSETMONO:
X case CDIOCSETSTEREO:
X case CDIOCSETMUTE:
X case CDIOCSETLEFT:
X case CDIOCSETRIGHT:
X return EINVAL;
X case CDIOCRESUME:
X return mcd_resume(sc);
X case CDIOCPAUSE:
X return mcd_pause(sc);
X case CDIOCSTART:
X return EINVAL;
X case CDIOCSTOP:
X return mcd_stop(sc);
X case CDIOCEJECT: /* FALLTHROUGH */
X case DIOCEJECT:
X return mcd_eject(sc);
X case CDIOCALLOW:
X return mcd_setlock(sc, MCD_LK_UNLOCK);
X case CDIOCPREVENT:
X return mcd_setlock(sc, MCD_LK_LOCK);
X case DIOCLOCK:
X return mcd_setlock(sc,
X (*(int *)addr) ? MCD_LK_LOCK : MCD_LK_UNLOCK);
X case CDIOCSETDEBUG:
X sc->debug = 1;
X return 0;
X case CDIOCCLRDEBUG:
X sc->debug = 0;
X return 0;
X case CDIOCRESET:
X return mcd_hard_reset(sc);
X
X default:
X return ENOTTY;
X }
X
X#ifdef DIAGNOSTIC
X panic("mcdioctl: impossible");
X#endif
X}
X
X/*
X * This could have been taken from scsi/cd.c, but it is not clear
X * whether the scsi cd driver is linked in.
X */
Xvoid
Xmcdgetdisklabel(sc)
X struct mcd_softc *sc;
X{
X struct disklabel *lp = sc->sc_dk.dk_label;
X
X bzero(lp, sizeof(struct disklabel));
X bzero(sc->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
X
X lp->d_secsize = sc->blksize;
X lp->d_ntracks = 1;
X lp->d_nsectors = 100;
X lp->d_ncylinders = (sc->disksize / 100) + 1;
X lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
X
X strncpy(lp->d_typename, "Mitsumi CD-ROM", 16);
X lp->d_type = 0; /* XXX */
X strncpy(lp->d_packname, "fictitious", 16);
X lp->d_secperunit = sc->disksize;
X lp->d_rpm = 300;
X lp->d_interleave = 1;
X lp->d_flags = D_REMOVABLE;
X
X lp->d_partitions[0].p_offset = 0;
X lp->d_partitions[0].p_size =
X lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
X lp->d_partitions[0].p_fstype = FS_ISO9660;
X lp->d_partitions[RAW_PART].p_offset = 0;
X lp->d_partitions[RAW_PART].p_size =
X lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
X lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660;
X lp->d_npartitions = RAW_PART + 1;
X
X lp->d_magic = DISKMAGIC;
X lp->d_magic2 = DISKMAGIC;
X lp->d_checksum = dkcksum(lp);
X}
X
Xint
Xmcd_get_parms(sc)
X struct mcd_softc *sc;
X{
X struct mcd_mbox mbx;
X daddr_t size;
X int error;
X
X /* Send volume info command. */
X mbx.cmd.opcode = MCD_CMDGETVOLINFO;
X mbx.cmd.length = 0;
X mbx.res.length = sizeof(mbx.res.data.volinfo);
X if ((error = mcd_send(sc, &mbx, 1)) != 0)
X return error;
X
X if (mbx.res.data.volinfo.trk_low == 0x00 &&
X mbx.res.data.volinfo.trk_high == 0x00)
X return EINVAL;
X
X /* Volinfo is OK. */
X sc->volinfo = mbx.res.data.volinfo;
X sc->blksize = MCD_BLKSIZE_COOKED;
X size = msf2hsg(sc->volinfo.vol_msf, 0);
X sc->disksize = size * (MCD_BLKSIZE_COOKED / DEV_BSIZE);
X return 0;
X}
X
Xint
Xmcdsize(dev)
X dev_t dev;
X{
X
X /* CD-ROMs are read-only. */
X return -1;
X}
X
Xint
Xmcddump(dev, blkno, va, size)
X dev_t dev;
X daddr_t blkno;
X caddr_t va;
X size_t size;
X{
X
X /* Not implemented. */
X return ENXIO;
X}
X
X/*
X * Find the board and fill in the softc.
X */
Xint
Xmcd_find(iot, ioh, sc)
X bus_space_tag_t iot;
X bus_space_handle_t ioh;
X struct mcd_softc *sc;
X{
X int i;
X struct mcd_mbox mbx;
X
X sc->sc_iot = iot;
X sc->sc_ioh = ioh;
X
X /* Send a reset. */
X bus_space_write_1(iot, ioh, MCD_RESET, 0);
X delay(1000000);
X /* Get any pending status and throw away. */
X for (i = 10; i; i--)
X bus_space_read_1(iot, ioh, MCD_STATUS);
X delay(1000);
X
X /* Send get status command. */
X mbx.cmd.opcode = MCD_CMDGETSTAT;
X mbx.cmd.length = 0;
X mbx.res.length = 0;
X if (mcd_send(sc, &mbx, 0) != 0)
X return 0;
X
X /* Get info about the drive. */
X mbx.cmd.opcode = MCD_CMDCONTINFO;
X mbx.cmd.length = 0;
X mbx.res.length = sizeof(mbx.res.data.continfo);
X if (mcd_send(sc, &mbx, 0) != 0)
X return 0;
X
X /*
X * The following is code which is not guaranteed to work for all
X * drives, because the meaning of the expected 'M' is not clear
X * (M_itsumi is an obvious assumption, but I don't trust that).
X * Also, the original hack had a bogus condition that always
X * returned true.
X *
X * Note: Which models support interrupts? >=LU005S?
X */
X sc->readcmd = MCD_CMDREADSINGLESPEED;
X switch (mbx.res.data.continfo.code) {
X case 'M':
X if (mbx.res.data.continfo.version <= 2)
X sc->type = "LU002S";
X else if (mbx.res.data.continfo.version <= 5)
X sc->type = "LU005S";
X else
X sc->type = "LU006S";
X break;
X case 'F':
X sc->type = "FX001";
X break;
X case 'D':
X sc->type = "FX001D";
X sc->readcmd = MCD_CMDREADDOUBLESPEED;
X break;
X default:
X#ifdef MCDDEBUG
X printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n",
X sc->sc_dev.dv_xname,
X mbx.res.data.continfo.code, mbx.res.data.continfo.version);
X#endif
X sc->type = 0;
X break;
X }
X
X return 1;
X
X}
X
Xint
Xmcdprobe(parent, match, aux)
X struct device *parent;
X void *match, *aux;
X{
X struct isa_attach_args *ia = aux;
X struct mcd_softc sc;
X bus_space_tag_t iot = ia->ia_iot;
X bus_space_handle_t ioh;
X int rv;
X
X /* Map i/o space */
X if (bus_space_map(iot, ia->ia_iobase, MCD_NPORT, 0, &ioh))
X return 0;
X
X rv = mcd_find(iot, ioh, &sc);
X
X bus_space_unmap(iot, ioh, MCD_NPORT);
X
X if (rv)
X {
X ia->ia_iosize = MCD_NPORT;
X ia->ia_msize = 0;
X }
X
X return(rv);
X}
X
Xint
Xmcd_getreply(sc)
X struct mcd_softc *sc;
X{
X bus_space_tag_t iot = sc->sc_iot;
X bus_space_handle_t ioh = sc->sc_ioh;
X int i;
X
X /* Wait until xfer port senses data ready. */
X for (i = DELAY_GETREPLY; i; i--) {
X if ((bus_space_read_1(iot, ioh, MCD_XFER) &
X MCD_XF_STATUSUNAVAIL) == 0)
X break;
X delay(DELAY_GRANULARITY);
X }
X if (!i)
X return -1;
X
X /* Get the data. */
X return bus_space_read_1(iot, ioh, MCD_STATUS);
X}
X
Xint
Xmcd_getstat(sc)
X struct mcd_softc *sc;
X{
X struct mcd_mbox mbx;
X
X mbx.cmd.opcode = MCD_CMDGETSTAT;
X mbx.cmd.length = 0;
X mbx.res.length = 0;
X return mcd_send(sc, &mbx, 1);
X}
X
Xint
Xmcd_getresult(sc, res)
X struct mcd_softc *sc;
X struct mcd_result *res;
X{
X int i, x;
X
X if (sc->debug)
X printf("%s: mcd_getresult: %d", sc->sc_dev.dv_xname,
X res->length);
X
X if ((x = mcd_getreply(sc)) < 0) {
X if (sc->debug)
X printf(" timeout\n");
X else
X printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
X return EIO;
X }
X if (sc->debug)
X printf(" %02x", (u_int)x);
X sc->status = x;
X mcd_setflags(sc);
X
X if ((sc->status & MCD_ST_CMDCHECK) != 0)
X return EINVAL;
X
X for (i = 0; i < res->length; i++) {
X if ((x = mcd_getreply(sc)) < 0) {
X if (sc->debug)
X printf(" timeout\n");
X else
X printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
X return EIO;
X }
X if (sc->debug)
X printf(" %02x", (u_int)x);
X res->data.raw.data[i] = x;
X }
X
X if (sc->debug)
X printf(" succeeded\n");
X
X#ifdef MCDDEBUG
X delay(10);
X while ((bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_XFER) &
X MCD_XF_STATUSUNAVAIL) == 0) {
X x = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_STATUS);
X printf("%s: got extra byte %02x during getstatus\n",
X sc->sc_dev.dv_xname, (u_int)x);
X delay(10);
X }
X#endif
X
X return 0;
X}
X
Xvoid
Xmcd_setflags(sc)
X struct mcd_softc *sc;
X{
X
X /* Check flags. */
X if ((sc->flags & MCDF_LOADED) != 0 &&
X (sc->status & (MCD_ST_DSKCHNG | MCD_ST_DSKIN | MCD_ST_DOOROPEN)) !=
X MCD_ST_DSKIN) {
X if ((sc->status & MCD_ST_DOOROPEN) != 0)
X printf("%s: door open\n", sc->sc_dev.dv_xname);
X else if ((sc->status & MCD_ST_DSKIN) == 0)
X printf("%s: no disk present\n", sc->sc_dev.dv_xname);
X else if ((sc->status & MCD_ST_DSKCHNG) != 0)
X printf("%s: media change\n", sc->sc_dev.dv_xname);
X sc->flags &= ~MCDF_LOADED;
X }
X
X if ((sc->status & MCD_ST_AUDIOBSY) != 0)
X sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
X else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS ||
X sc->audio_status == CD_AS_AUDIO_INVALID)
X sc->audio_status = CD_AS_PLAY_COMPLETED;
X}
X
Xint
Xmcd_send(sc, mbx, diskin)
X struct mcd_softc *sc;
X struct mcd_mbox *mbx;
X int diskin;
X{
X int retry, i, error;
X bus_space_tag_t iot = sc->sc_iot;
X bus_space_handle_t ioh = sc->sc_ioh;
X
X if (sc->debug) {
X printf("%s: mcd_send: %d %02x", sc->sc_dev.dv_xname,
X mbx->cmd.length, (u_int)mbx->cmd.opcode);
X for (i = 0; i < mbx->cmd.length; i++)
X printf(" %02x", (u_int)mbx->cmd.data.raw.data[i]);
X printf("\n");
X }
X
X for (retry = MCD_RETRIES; retry; retry--) {
X bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.opcode);
X for (i = 0; i < mbx->cmd.length; i++)
X bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.data.raw.data[i]);
X if ((error = mcd_getresult(sc, &mbx->res)) == 0)
X break;
X if (error == EINVAL)
X return error;
X }
X if (!retry)
X return error;
X if (diskin && (sc->flags & MCDF_LOADED) == 0)
X return EIO;
X
X return 0;
X}
X
Xstatic int
Xbcd2bin(b)
X bcd_t b;
X{
X
X return (b >> 4) * 10 + (b & 15);
X}
X
Xstatic bcd_t
Xbin2bcd(b)
X int b;
X{
X
X return ((b / 10) << 4) | (b % 10);
X}
X
Xstatic void
Xhsg2msf(hsg, msf)
X int hsg;
X bcd_t *msf;
X{
X
X hsg += 150;
X F_msf(msf) = bin2bcd(hsg % 75);
X hsg /= 75;
X S_msf(msf) = bin2bcd(hsg % 60);
X hsg /= 60;
X M_msf(msf) = bin2bcd(hsg);
X}
X
Xstatic daddr_t
Xmsf2hsg(msf, relative)
X bcd_t *msf;
X int relative;
X{
X daddr_t blkno;
X
X blkno = bcd2bin(M_msf(msf)) * 75 * 60 +
X bcd2bin(S_msf(msf)) * 75 +
X bcd2bin(F_msf(msf));
X if (!relative)
X blkno -= 150;
X return blkno;
X}
X
Xvoid
Xmcd_pseudointr(v)
X void *v;
X{
X struct mcd_softc *sc = v;
X int s;
X
X s = splbio();
X (void) mcdintr(sc);
X splx(s);
X}
X
X/*
X * State machine to process read requests.
X * Initialize with MCD_S_BEGIN: calculate sizes, and set mode
X * MCD_S_WAITMODE: waits for status reply from set mode, set read command
X * MCD_S_WAITREAD: wait for read ready, read data.
X */
Xint
Xmcdintr(arg)
X void *arg;
X{
X struct mcd_softc *sc = arg;
X struct mcd_mbx *mbx = &sc->mbx;
X struct buf *bp = mbx->bp;
X bus_space_tag_t iot = sc->sc_iot;
X bus_space_handle_t ioh = sc->sc_ioh;
X
X int i;
X u_char x;
X bcd_t msf[3];
X
X switch (mbx->state) {
X case MCD_S_IDLE:
X return 0;
X
X case MCD_S_BEGIN:
X tryagain:
X if (mbx->mode == sc->lastmode)
X goto firstblock;
X
X sc->lastmode = MCD_MD_UNKNOWN;
X bus_space_write_1(iot, ioh, MCD_COMMAND, MCD_CMDSETMODE);
X bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->mode);
X
X mbx->count = RDELAY_WAITMODE;
X mbx->state = MCD_S_WAITMODE;
X
X case MCD_S_WAITMODE:
X untimeout(mcd_pseudointr, sc);
X for (i = 20; i; i--) {
X x = bus_space_read_1(iot, ioh, MCD_XFER);
X if ((x & MCD_XF_STATUSUNAVAIL) == 0)
X break;
X delay(50);
X }
X if (i == 0)
X goto hold;
X sc->status = bus_space_read_1(iot, ioh, MCD_STATUS);
X mcd_setflags(sc);
X if ((sc->flags & MCDF_LOADED) == 0)
X goto changed;
X MCD_TRACE("doread: got WAITMODE delay=%d\n",
X RDELAY_WAITMODE - mbx->count, 0, 0, 0);
X
X sc->lastmode = mbx->mode;
X
X firstblock:
X MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", mbx->blkno,
X bp, 0, 0);
X
X /* Build parameter block. */
X hsg2msf(mbx->blkno, msf);
X
X /* Send the read command. */
X bus_space_write_1(iot, ioh, MCD_COMMAND, sc->readcmd);
X bus_space_write_1(iot, ioh, MCD_COMMAND, msf[0]);
X bus_space_write_1(iot, ioh, MCD_COMMAND, msf[1]);
X bus_space_write_1(iot, ioh, MCD_COMMAND, msf[2]);
X bus_space_write_1(iot, ioh, MCD_COMMAND, 0);
X bus_space_write_1(iot, ioh, MCD_COMMAND, 0);
X bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->nblk);
X
X mbx->count = RDELAY_WAITREAD;
X mbx->state = MCD_S_WAITREAD;
X
X case MCD_S_WAITREAD:
X untimeout(mcd_pseudointr, sc);
X nextblock:
X loop:
X for (i = 20; i; i--) {
X x = bus_space_read_1(iot, ioh, MCD_XFER);
X if ((x & MCD_XF_DATAUNAVAIL) == 0)
X goto gotblock;
X if ((x & MCD_XF_STATUSUNAVAIL) == 0)
X break;
X delay(50);
X }
X if (i == 0)
X goto hold;
X sc->status = bus_space_read_1(iot, ioh, MCD_STATUS);
X mcd_setflags(sc);
X if ((sc->flags & MCDF_LOADED) == 0)
X goto changed;
X#if 0
X printf("%s: got status byte %02x during read\n",
X sc->sc_dev.dv_xname, (u_int)sc->status);
X#endif
X goto loop;
X
X gotblock:
X MCD_TRACE("doread: got data delay=%d\n",
X RDELAY_WAITREAD - mbx->count, 0, 0, 0);
X
X /* Data is ready. */
X bus_space_write_1(iot, ioh, MCD_CTL2, 0x04); /* XXX */
X bus_space_read_multi_1(iot, ioh, MCD_RDATA,
X bp->b_data + mbx->skip, mbx->sz);
X bus_space_write_1(iot, ioh, MCD_CTL2, 0x0c); /* XXX */
X mbx->blkno += 1;
X mbx->skip += mbx->sz;
X if (--mbx->nblk > 0)
X goto nextblock;
X
X mbx->state = MCD_S_IDLE;
X
X /* Return buffer. */
X bp->b_resid = 0;
X disk_unbusy(&sc->sc_dk, bp->b_bcount);
X biodone(bp);
X
X mcdstart(sc);
X return 1;
X
X hold:
X if (mbx->count-- < 0) {
X printf("%s: timeout in state %d",
X sc->sc_dev.dv_xname, mbx->state);
X goto readerr;
X }
X
X#if 0
X printf("%s: sleep in state %d\n", sc->sc_dev.dv_xname,
X mbx->state);
X#endif
X timeout(mcd_pseudointr, sc, hz / 100);
X return -1;
X }
X
Xreaderr:
X if (mbx->retry-- > 0) {
X printf("; retrying\n");
X goto tryagain;
X } else
X printf("; giving up\n");
X
Xchanged:
X /* Invalidate the buffer. */
X bp->b_flags |= B_ERROR;
X bp->b_resid = bp->b_bcount - mbx->skip;
X disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid));
X biodone(bp);
X
X mcdstart(sc);
X return -1;
X
X#ifdef notyet
X printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname);
X bus_space_write_1(iot, ioh, MCD_RESET, MCD_CMDRESET);
X delay(300000);
X (void) mcd_getstat(sc, 1);
X (void) mcd_getstat(sc, 1);
X /*sc->status &= ~MCD_ST_DSKCHNG; */
X sc->debug = 1; /* preventive set debug mode */
X#endif
X}
X
Xvoid
Xmcd_soft_reset(sc)
X struct mcd_softc *sc;
X{
X
X sc->debug = 0;
X sc->flags = 0;
X sc->lastmode = MCD_MD_UNKNOWN;
X sc->lastupc = MCD_UPC_UNKNOWN;
X sc->audio_status = CD_AS_AUDIO_INVALID;
X bus_space_write_1(sc->sc_iot, sc->sc_ioh, MCD_CTL2, 0x0c); /* XXX */
X}
X
Xint
Xmcd_hard_reset(sc)
X struct mcd_softc *sc;
X{
X struct mcd_mbox mbx;
X
X mcd_soft_reset(sc);
X
X mbx.cmd.opcode = MCD_CMDRESET;
X mbx.cmd.length = 0;
X mbx.res.length = 0;
X return mcd_send(sc, &mbx, 0);
X}
X
Xint
Xmcd_setmode(sc, mode)
X struct mcd_softc *sc;
X int mode;
X{
X struct mcd_mbox mbx;
X int error;
X
X if (sc->lastmode == mode)
X return 0;
X if (sc->debug)
X printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode);
X sc->lastmode = MCD_MD_UNKNOWN;
X
X mbx.cmd.opcode = MCD_CMDSETMODE;
X mbx.cmd.length = sizeof(mbx.cmd.data.datamode);
X mbx.cmd.data.datamode.mode = mode;
X mbx.res.length = 0;
X if ((error = mcd_send(sc, &mbx, 1)) != 0)
X return error;
X
X sc->lastmode = mode;
X return 0;
X}
X
Xint
Xmcd_setupc(sc, upc)
X struct mcd_softc *sc;
X int upc;
X{
X struct mcd_mbox mbx;
X int error;
X
X if (sc->lastupc == upc)
X return 0;
X if (sc->debug)
X printf("%s: setting upc to %d\n", sc->sc_dev.dv_xname, upc);
X sc->lastupc = MCD_UPC_UNKNOWN;
X
X mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
X mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
X mbx.cmd.data.config.subcommand = MCD_CF_READUPC;
X mbx.cmd.data.config.data1 = upc;
X mbx.res.length = 0;
X if ((error = mcd_send(sc, &mbx, 1)) != 0)
X return error;
X
X sc->lastupc = upc;
X return 0;
X}
X
Xint
Xmcd_toc_header(sc, th)
X struct mcd_softc *sc;
X struct ioc_toc_header *th;
X{
X
X if (sc->debug)
X printf("%s: mcd_toc_header: reading toc header\n",
X sc->sc_dev.dv_xname);
X
X th->len = msf2hsg(sc->volinfo.vol_msf, 0);
X th->starting_track = bcd2bin(sc->volinfo.trk_low);
X th->ending_track = bcd2bin(sc->volinfo.trk_high);
X
X return 0;
X}
X
Xint
Xmcd_read_toc(sc)
X struct mcd_softc *sc;
X{
X struct ioc_toc_header th;
X union mcd_qchninfo q;
X int error, trk, idx, retry;
X
X if ((error = mcd_toc_header(sc, &th)) != 0)
X return error;
X
X if ((error = mcd_stop(sc)) != 0)
X return error;
X
X if (sc->debug)
X printf("%s: read_toc: reading qchannel info\n",
X sc->sc_dev.dv_xname);
X
X for (trk = th.starting_track; trk <= th.ending_track; trk++)
X sc->toc[trk].toc.idx_no = 0x00;
X trk = th.ending_track - th.starting_track + 1;
X for (retry = 300; retry && trk > 0; retry--) {
X if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0)
X break;
X if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00)
X continue;
X idx = bcd2bin(q.toc.idx_no);
X if (idx < MCD_MAXTOCS &&
X sc->toc[idx].toc.idx_no == 0x00) {
X sc->toc[idx] = q;
X trk--;
X }
X }
X
X /* Inform the drive that we're finished so it turns off the light. */
X if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
X return error;
X
X if (trk != 0)
X return EINVAL;
X
X /* Add a fake last+1 for mcd_playtracks(). */
X idx = th.ending_track + 1;
X sc->toc[idx].toc.control = sc->toc[idx-1].toc.control;
X sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type;
X sc->toc[idx].toc.trk_no = 0x00;
X sc->toc[idx].toc.idx_no = 0xaa;
X sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0];
X sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1];
X sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2];
X
X return 0;
X}
X
Xint
Xmcd_toc_entries(sc, te)
X struct mcd_softc *sc;
X struct ioc_read_toc_entry *te;
X{
X int len = te->data_len;
X struct ret_toc {
X struct ioc_toc_header header;
X struct cd_toc_entry entries[MCD_MAXTOCS];
X } data;
X u_char trk;
X daddr_t lba;
X int error, n;
X
X if (len > sizeof(data.entries) ||
X len < sizeof(struct cd_toc_entry))
X return EINVAL;
X if (te->address_format != CD_MSF_FORMAT &&
X te->address_format != CD_LBA_FORMAT)
X return EINVAL;
X
X /* Copy the TOC header. */
X if ((error = mcd_toc_header(sc, &data.header)) != 0)
X return error;
X
X /* Verify starting track. */
X trk = te->starting_track;
X if (trk == 0x00)
X trk = data.header.starting_track;
X else if (trk == 0xaa)
X trk = data.header.ending_track + 1;
X else if (trk < data.header.starting_track ||
X trk > data.header.ending_track + 1)
X return EINVAL;
X
X /* Copy the TOC data. */
X for (n = 0; trk <= data.header.ending_track + 1; trk++) {
X if (sc->toc[trk].toc.idx_no == 0x00)
X continue;
X data.entries[n].control = sc->toc[trk].toc.control;
X data.entries[n].addr_type = sc->toc[trk].toc.addr_type;
X data.entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no);
X switch (te->address_format) {
X case CD_MSF_FORMAT:
X data.entries[n].addr[0] = 0;
X data.entries[n].addr[1] = bcd2bin(sc->toc[trk].toc.absolute_pos[0]);
X data.entries[n].addr[2] = bcd2bin(sc->toc[trk].toc.absolute_pos[1]);
X data.entries[n].addr[3] = bcd2bin(sc->toc[trk].toc.absolute_pos[2]);
X break;
X case CD_LBA_FORMAT:
X lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0);
X data.entries[n].addr[0] = lba >> 24;
X data.entries[n].addr[1] = lba >> 16;
X data.entries[n].addr[2] = lba >> 8;
X data.entries[n].addr[3] = lba;
X break;
X }
X n++;
X }
X
X len = min(len, n * sizeof(struct cd_toc_entry));
X
X /* Copy the data back. */
X return copyout(&data.entries[0], te->data, len);
X}
X
Xint
Xmcd_stop(sc)
X struct mcd_softc *sc;
X{
X struct mcd_mbox mbx;
X int error;
X
X if (sc->debug)
X printf("%s: mcd_stop: stopping play\n", sc->sc_dev.dv_xname);
X
X mbx.cmd.opcode = MCD_CMDSTOPAUDIO;
X mbx.cmd.length = 0;
X mbx.res.length = 0;
X if ((error = mcd_send(sc, &mbx, 1)) != 0)
X return error;
X
X sc->audio_status = CD_AS_PLAY_COMPLETED;
X return 0;
X}
X
Xint
Xmcd_getqchan(sc, q, qchn)
X struct mcd_softc *sc;
X union mcd_qchninfo *q;
X int qchn;
X{
X struct mcd_mbox mbx;
X int error;
X
X if (qchn == CD_TRACK_INFO) {
X if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0)
X return error;
X } else {
X if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
X return error;
X }
X if (qchn == CD_MEDIA_CATALOG) {
X if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0)
X return error;
X } else {
X if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0)
X return error;
X }
X
X mbx.cmd.opcode = MCD_CMDGETQCHN;
X mbx.cmd.length = 0;
X mbx.res.length = sizeof(mbx.res.data.qchninfo);
X if ((error = mcd_send(sc, &mbx, 1)) != 0)
X return error;
X
X *q = mbx.res.data.qchninfo;
X return 0;
X}
X
Xint
Xmcd_read_subchannel(sc, ch)
X struct mcd_softc *sc;
X struct ioc_read_subchannel *ch;
X{
X int len = ch->data_len;
X union mcd_qchninfo q;
X struct cd_sub_channel_info data;
X daddr_t lba;
X int error;
X
X if (sc->debug)
X printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname,
X ch->address_format, ch->data_format);
X
X if (len > sizeof(data) ||
X len < sizeof(struct cd_sub_channel_header))
X return EINVAL;
X if (ch->address_format != CD_MSF_FORMAT &&
X ch->address_format != CD_LBA_FORMAT)
X return EINVAL;
X if (ch->data_format != CD_CURRENT_POSITION &&
X ch->data_format != CD_MEDIA_CATALOG)
X return EINVAL;
X
X if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0)
X return error;
X
X data.header.audio_status = sc->audio_status;
X data.what.media_catalog.data_format = ch->data_format;
X
X switch (ch->data_format) {
X case CD_MEDIA_CATALOG:
X data.what.media_catalog.mc_valid = 1;
X#if 0
X data.what.media_catalog.mc_number =
X#endif
X break;
X
X case CD_CURRENT_POSITION:
X data.what.position.track_number = bcd2bin(q.current.trk_no);
X data.what.position.index_number = bcd2bin(q.current.idx_no);
X switch (ch->address_format) {
X case CD_MSF_FORMAT:
X data.what.position.reladdr[0] = 0;
X data.what.position.reladdr[1] = bcd2bin(q.current.relative_pos[0]);
X data.what.position.reladdr[2] = bcd2bin(q.current.relative_pos[1]);
X data.what.position.reladdr[3] = bcd2bin(q.current.relative_pos[2]);
X data.what.position.absaddr[0] = 0;
X data.what.position.absaddr[1] = bcd2bin(q.current.absolute_pos[0]);
X data.what.position.absaddr[2] = bcd2bin(q.current.absolute_pos[1]);
X data.what.position.absaddr[3] = bcd2bin(q.current.absolute_pos[2]);
X break;
X case CD_LBA_FORMAT:
X lba = msf2hsg(q.current.relative_pos, 1);
X /*
X * Pre-gap has index number of 0, and decreasing MSF
X * address. Must be converted to negative LBA, per
X * SCSI spec.
X */
X if (data.what.position.index_number == 0x00)
X lba = -lba;
X data.what.position.reladdr[0] = lba >> 24;
X data.what.position.reladdr[1] = lba >> 16;
X data.what.position.reladdr[2] = lba >> 8;
X data.what.position.reladdr[3] = lba;
X lba = msf2hsg(q.current.absolute_pos, 0);
X data.what.position.absaddr[0] = lba >> 24;
X data.what.position.absaddr[1] = lba >> 16;
X data.what.position.absaddr[2] = lba >> 8;
X data.what.position.absaddr[3] = lba;
X break;
X }
X break;
X }
X
X return copyout(&data, ch->data, len);
X}
X
Xint
Xmcd_playtracks(sc, p)
X struct mcd_softc *sc;
X struct ioc_play_track *p;
X{
X struct mcd_mbox mbx;
X int a = p->start_track;
X int z = p->end_track;
X int error;
X
X if (sc->debug)
X printf("%s: playtracks: from %d:%d to %d:%d\n",
X sc->sc_dev.dv_xname,
X a, p->start_index, z, p->end_index);
X
X if (a < bcd2bin(sc->volinfo.trk_low) ||
X a > bcd2bin(sc->volinfo.trk_high) ||
X a > z ||
X z < bcd2bin(sc->volinfo.trk_low) ||
X z > bcd2bin(sc->volinfo.trk_high))
X return EINVAL;
X
X if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
X return error;
X
X mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
X mbx.cmd.length = sizeof(mbx.cmd.data.play);
X mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0];
X mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1];
X mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2];
X mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0];
X mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1];
X mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2];
X sc->lastpb = mbx.cmd;
X mbx.res.length = 0;
X return mcd_send(sc, &mbx, 1);
X}
X
Xint
Xmcd_playmsf(sc, p)
X struct mcd_softc *sc;
X struct ioc_play_msf *p;
X{
X struct mcd_mbox mbx;
X int error;
X
X if (sc->debug)
X printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n",
X sc->sc_dev.dv_xname,
X p->start_m, p->start_s, p->start_f,
X p->end_m, p->end_s, p->end_f);
X
X if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
X (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f))
X return EINVAL;
X
X if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
X return error;
X
X mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
X mbx.cmd.length = sizeof(mbx.cmd.data.play);
X mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m);
X mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s);
X mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f);
X mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m);
X mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s);
X mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f);
X sc->lastpb = mbx.cmd;
X mbx.res.length = 0;
X return mcd_send(sc, &mbx, 1);
X}
X
Xint
Xmcd_playblocks(sc, p)
X struct mcd_softc *sc;
X struct ioc_play_blocks *p;
X{
X struct mcd_mbox mbx;
X int error;
X
X if (sc->debug)
X printf("%s: playblocks: blkno %d length %d\n",
X sc->sc_dev.dv_xname, p->blk, p->len);
X
X if (p->blk > sc->disksize || p->len > sc->disksize ||
X (p->blk + p->len) > sc->disksize)
X return 0;
X
X if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
X return error;
X
X mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
X mbx.cmd.length = sizeof(mbx.cmd.data.play);
X hsg2msf(p->blk, mbx.cmd.data.play.start_msf);
X hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf);
X sc->lastpb = mbx.cmd;
X mbx.res.length = 0;
X return mcd_send(sc, &mbx, 1);
X}
X
Xint
Xmcd_pause(sc)
X struct mcd_softc *sc;
X{
X union mcd_qchninfo q;
X int error;
X
X /* Verify current status. */
X if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS) {
X printf("%s: pause: attempted when not playing\n",
X sc->sc_dev.dv_xname);
X return EINVAL;
X }
X
X /* Get the current position. */
X if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0)
X return error;
X
X /* Copy it into lastpb. */
X sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0];
X sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1];
X sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2];
X
X /* Stop playing. */
X if ((error = mcd_stop(sc)) != 0)
X return error;
X
X /* Set the proper status and exit. */
X sc->audio_status = CD_AS_PLAY_PAUSED;
X return 0;
X}
X
Xint
Xmcd_resume(sc)
X struct mcd_softc *sc;
X{
X struct mcd_mbox mbx;
X int error;
X
X if (sc->audio_status != CD_AS_PLAY_PAUSED)
X return EINVAL;
X
X if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
X return error;
X
X mbx.cmd = sc->lastpb;
X mbx.res.length = 0;
X return mcd_send(sc, &mbx, 1);
X}
X
Xint
Xmcd_eject(sc)
X struct mcd_softc *sc;
X{
X struct mcd_mbox mbx;
X
X mbx.cmd.opcode = MCD_CMDEJECTDISK;
X mbx.cmd.length = 0;
X mbx.res.length = 0;
X return mcd_send(sc, &mbx, 0);
X}
X
Xint
Xmcd_setlock(sc, mode)
X struct mcd_softc *sc;
X int mode;
X{
X struct mcd_mbox mbx;
X
X mbx.cmd.opcode = MCD_CMDSETLOCK;
X mbx.cmd.length = sizeof(mbx.cmd.data.lockmode);
X mbx.cmd.data.lockmode.mode = mode;
X mbx.res.length = 0;
X return mcd_send(sc, &mbx, 1);
X}
END-of-mcd.c
echo x - mcdreg.h
sed 's/^X//' >mcdreg.h << 'END-of-mcdreg.h'
X/* $NetBSD: mcdreg.h,v 1.7 1995/07/10 01:27:27 cgd Exp $ */
X
X/*
X * Copyright 1993 by Holger Veit (data part)
X * Copyright 1993 by Brian Moore (audio part)
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X * notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X * notice, this list of conditions and the following disclaimer in the
X * documentation and/or other materials provided with the distribution.
X * 3. All advertising materials mentioning features or use of this software
X * must display the following acknowledgement:
X * This software was developed by Holger Veit and Brian Moore
X * for use with "386BSD" and similar operating systems.
X * "Similar operating systems" includes mainly non-profit oriented
X * systems for research and education, including but not restricted to
X * "NetBSD", "FreeBSD", "Mach" (by CMU).
X * 4. Neither the name of the developer(s) nor the name "386BSD"
X * may be used to endorse or promote products derived from this
X * software without specific prior written permission.
X *
X * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY
X * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
X * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER(S) BE
X * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
X * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
X * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
X * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
X * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
X * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
X * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
X *
X * This file contains definitions for some cdrom control commands
X * and status codes. This info was "inherited" from the DOS MTMCDE.SYS
X * driver, and is thus not complete (and may even be wrong). Some day
X * the manufacturer or anyone else might provide better documentation,
X * so this file (and the driver) will then have a better quality.
X */
X
X#if __GNUC__ >= 2
X#pragma pack(1)
X#endif
X
Xtypedef unsigned char bcd_t;
X#define M_msf(msf) msf[0]
X#define S_msf(msf) msf[1]
X#define F_msf(msf) msf[2]
X
X#define MCD_COMMAND 0
X#define MCD_STATUS 0
X#define MCD_RDATA 0
X#define MCD_RESET 1
X#define MCD_XFER 1
X#define MCD_CTL2 2 /* XXX Is this right? */
X#define MCD_CONFIG 3
X#define MCD_NPORT 4
X
X#define MCD_MASK_DMA 0x07 /* bits 2-0 = DMA channel */
X#define MCD_MASK_IRQ 0x70 /* bits 6-4 = INT number */
X /* 001 = int 2,9 */
X /* 010 = int 3 */
X /* 011 = int 5 */
X /* 100 = int 10 */
X /* 101 = int 11 */
X
X/* Status bits */
X#define MCD_ST_DOOROPEN 0x80
X#define MCD_ST_DSKIN 0x40
X#define MCD_ST_DSKCHNG 0x20
X#define MCD_ST_SPINNING 0x10
X#define MCD_ST_AUDIODISK 0x08 /* audio disk is in */
X#define MCD_ST_READERR 0x04
X#define MCD_ST_AUDIOBSY 0x02 /* audio disk is playing */
X#define MCD_ST_CMDCHECK 0x01 /* command error */
X
X/* Xfer bits */
X#define MCD_XF_STATUSUNAVAIL 0x04
X#define MCD_XF_DATAUNAVAIL 0x02
X
X/* Modes */
X#define MCD_MD_TESTMODE 0x80 /* 0 = DATALENGTH is valid */
X#define MCD_MD_DATALENGTH 0x40 /* 1 = read ECC data also */
X#define MCD_MD_ECCMODE 0x20 /* 1 = disable secondary ECC */
X#define MCD_MD_SPINDOWN 0x08 /* 1 = spin down */
X#define MCD_MD_READTOC 0x04 /* 1 = read TOC on GETQCHN */
X#define MCD_MD_PLAYAUDIO 0x01 /* 1 = play audio through headphones */
X
X#define MCD_MD_RAW (MCD_MD_PLAYAUDIO|MCD_MD_ECCMODE|MCD_MD_DATALENGTH)
X#define MCD_MD_COOKED (MCD_MD_PLAYAUDIO)
X#define MCD_MD_TOC (MCD_MD_PLAYAUDIO|MCD_MD_READTOC)
X#define MCD_MD_SLEEP (MCD_MD_PLAYAUDIO|MCD_MD_SPINDOWN)
X
X#define MCD_BLKSIZE_RAW sizeof(struct mcd_rawsector)
X#define MCD_BLKSIZE_COOKED 2048
X
X/* Lock states */
X#define MCD_LK_UNLOCK 0x00
X#define MCD_LK_LOCK 0x01
X#define MCD_LK_TEST 0x02
X
X/* Config commands */
X#define MCD_CF_IRQENABLE 0x10
X#define MCD_CF_DMATIMEOUT 0x08
X#define MCD_CF_READUPC 0x04
X#define MCD_CF_DMAENABLE 0x02
X#define MCD_CF_BLOCKSIZE 0x01
X
X/* UPC subcommands */
X#define MCD_UPC_DISABLE 0x00
X#define MCD_UPC_ENABLE 0x01
X
X/* commands known by the controller */
X#define MCD_CMDRESET 0x00
X#define MCD_CMDGETVOLINFO 0x10 /* gets mcd_volinfo */
X#define MCD_CMDGETDISKINFO 0x11 /* gets mcd_disk */
X#define MCD_CMDGETQCHN 0x20 /* gets mcd_qchninfo */
X#define MCD_CMDGETSENSE 0x30 /* gets sense info */
X#define MCD_CMDGETSTAT 0x40 /* gets a byte of status */
X#define MCD_CMDSETMODE 0x50 /* set transmission mode, needs byte */
X#define MCD_CMDSTOPAUDIO 0x70
X#define MCD_CMDSTOPAUDIOTIME 0x80
X#define MCD_CMDGETVOLUME 0x8E /* gets mcd_volume */
X#define MCD_CMDCONFIGDRIVE 0x90
X#define MCD_CMDSETDRIVEMODE 0xa0 /* set drive mode */
X#define MCD_CMDSETVOLUME 0xae /* sets mcd_volume */
X#define MCD_CMDREAD1 0xb0 /* read n sectors */
X#define MCD_CMDREADSINGLESPEED 0xc0 /* read (single speed) */
X#define MCD_CMDREADDOUBLESPEED 0xc1 /* read (double speed) */
X#define MCD_CMDGETDRIVEMODE 0xc2 /* get drive mode */
X#define MCD_CMDREAD3 0xc3 /* ? */
X#define MCD_CMDSETINTERLEAVE 0xc8 /* set interleave for read */
X#define MCD_CMDCONTINFO 0xdc /* get controller info */
X#define MCD_CMDSTOP 0xf0 /* stop everything */
X#define MCD_CMDEJECTDISK 0xf6
X#define MCD_CMDCLOSETRAY 0xf8
X#define MCD_CMDSETLOCK 0xfe /* needs byte */
X
Xunion mcd_qchninfo {
X struct {
X u_char control:4;
X u_char addr_type:4;
X u_char trk_no;
X u_char idx_no;
X bcd_t track_size[3];
X u_char :8;
X bcd_t absolute_pos[3];
X } toc;
X struct {
X u_char control:4;
X u_char addr_type:4;
X u_char trk_no;
X u_char idx_no;
X bcd_t relative_pos[3];
X u_char :8;
X bcd_t absolute_pos[3];
X } current;
X struct {
X u_char control:4;
X u_char addr_type:4;
X u_char upccode[7];
X u_char junk[2];
X } upc;
X};
X
Xstruct mcd_volinfo {
X bcd_t trk_low;
X bcd_t trk_high;
X bcd_t vol_msf[3];
X bcd_t trk1_msf[3];
X};
X
Xstruct mcd_result {
X u_char length;
X union {
X struct {
X u_char data[1];
X } raw;
X struct {
X u_char code;
X u_char version;
X } continfo;
X union mcd_qchninfo qchninfo;
X struct mcd_volinfo volinfo;
X } data;
X};
X
Xstruct mcd_command {
X u_char opcode;
X u_char length;
X union {
X struct {
X u_char data[1];
X } raw;
X struct {
X bcd_t start_msf[3];
X bcd_t reserved[3];
X } seek;
X struct {
X bcd_t start_msf[3];
X bcd_t length[3];
X } read;
X struct {
X bcd_t start_msf[3];
X bcd_t end_msf[3];
X } play;
X struct {
X u_char mode;
X } datamode;
X struct {
X u_char time;
X } hold;
X struct {
X u_char mode;
X } drivemode;
X struct {
X u_char mode;
X } lockmode;
X struct {
X u_char subcommand;
X u_char data1, data2;
X } config;
X } data;
X};
X
Xstruct mcd_mbox {
X struct mcd_command cmd;
X struct mcd_result res;
X};
X
Xstruct mcd_volume {
X u_char v0l;
X u_char v0rs;
X u_char v0r;
X u_char v0ls;
X};
X
Xstruct mcd_rawsector {
X u_char sync1[12];
X u_char header[4];
X u_char subheader1[4];
X u_char subheader2[4];
X u_char data[MCD_BLKSIZE_COOKED];
X u_char ecc_bits[280];
X};
X
X#if __GNUC__ >= 2
X#pragma pack(4)
X#endif
END-of-mcdreg.h
exit