/*
* Copyright (c) 1993, 1994, 1995 Charles M. Hannum. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Charles M. Hannum.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* Copyright 1993 by Holger Veit (data part)
* Copyright 1993 by Brian Moore (audio part)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This software was developed by Holger Veit and Brian Moore
* for use with "386BSD" and similar operating systems.
* "Similar operating systems" includes mainly non-profit oriented
* systems for research and education, including but not restricted to
* "NetBSD", "FreeBSD", "Mach" (by CMU).
* 4. Neither the name of the developer(s) nor the name "386BSD"
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER(S) BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*static char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";*/
int
mcdopen(dev_t dev, int flag, int fmt, struct lwp *l)
{
int error, part;
struct mcd_softc *sc;
sc = device_lookup_private(&mcd_cd, MCDUNIT(dev));
if (sc == NULL)
return ENXIO;
mutex_enter(&sc->sc_lock);
if (sc->sc_dk.dk_openmask != 0) {
/*
* If any partition is open, but the disk has been invalidated,
* disallow further opens.
*/
if ((sc->flags & MCDF_LOADED) == 0) {
error = EIO;
goto bad3;
}
} else {
/*
* Lock the drawer. This will also notice any pending disk
* change or door open indicator and clear the MCDF_LOADED bit
* if necessary.
*/
(void) mcd_setlock(sc, MCD_LK_LOCK);
if ((sc->flags & MCDF_LOADED) == 0) {
/* Partially reset the state. */
sc->lastmode = MCD_MD_UNKNOWN;
sc->lastupc = MCD_UPC_UNKNOWN;
sc->flags |= MCDF_LOADED;
/* Set the mode, causing the disk to spin up. */
if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
goto bad2;
/* Load the physical device parameters. */
if (mcd_get_parms(sc) != 0) {
error = ENXIO;
goto bad2;
}
/* Read the table of contents. */
if ((error = mcd_read_toc(sc)) != 0)
goto bad2;
/* Fabricate a disk label. */
mcdgetdisklabel(sc);
}
}
/* Check that the partition exists. */
if (part != RAW_PART &&
(part >= sc->sc_dk.dk_label->d_npartitions ||
sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
error = ENXIO;
goto bad;
}
/* Insure only one open at a time. */
switch (fmt) {
case S_IFCHR:
sc->sc_dk.dk_copenmask |= (1 << part);
break;
case S_IFBLK:
sc->sc_dk.dk_bopenmask |= (1 << part);
break;
}
sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
int
mcdclose(dev_t dev, int flag, int fmt, struct lwp *l)
{
struct mcd_softc *sc = device_lookup_private(&mcd_cd, MCDUNIT(dev));
int part = MCDPART(dev);
/* If device invalidated (e.g. media change, door open), error. */
if ((sc->flags & MCDF_LOADED) == 0) {
MCD_TRACE("strategy: drive not valid%s", "\n");
bp->b_error = EIO;
goto done;
}
/* No data to read. */
if (bp->b_bcount == 0)
goto done;
/*
* Do bounds checking, adjust transfer. if error, process.
* If end of partition, just return.
*/
if (MCDPART(bp->b_dev) != RAW_PART &&
bounds_check_with_label(&sc->sc_dk, bp,
(sc->flags & (MCDF_WLABEL|MCDF_LABELLING)) != 0) <= 0)
goto done;
/*
* Now convert the block number to absolute and put it in
* terms of the device's logical block size.
*/
blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
if (MCDPART(bp->b_dev) != RAW_PART)
blkno += lp->d_partitions[MCDPART(bp->b_dev)].p_offset;
bp->b_rawblkno = blkno;
/* Queue it. */
s = splbio();
bufq_put(sc->buf_queue, bp);
splx(s);
if (!sc->active)
mcdstart(sc);
return;
done:
bp->b_resid = bp->b_bcount;
biodone(bp);
}
void
mcdstart(struct mcd_softc *sc)
{
struct buf *bp;
int s;
loop:
s = splbio();
if ((bp = bufq_get(sc->buf_queue)) == NULL) {
/* Nothing to do. */
sc->active = 0;
splx(s);
return;
}
/* Block found to process. */
MCD_TRACE("start: found block bp=%p\n", bp);
splx(s);
part = MCDPART(dev);
switch (cmd) {
case DIOCWDINFO:
case DIOCSDINFO:
#ifdef __HAVE_OLD_DISKLABEL
case ODIOCWDINFO:
case ODIOCSDINFO:
#endif
{
struct disklabel *lp;
/*
* This could have been taken from scsi/cd.c, but it is not clear
* whether the scsi cd driver is linked in.
*/
void
mcdgetdisklabel(struct mcd_softc *sc)
{
struct disklabel *lp = sc->sc_dk.dk_label;
int
mcddump(dev_t dev, daddr_t blkno, void *va,
size_t size)
{
/* Not implemented. */
return ENXIO;
}
/*
* Find the board and fill in the softc.
*/
int
mcd_find(bus_space_tag_t iot, bus_space_handle_t ioh, struct mcd_softc *sc)
{
int i;
struct mcd_mbox mbx;
sc->sc_iot = iot;
sc->sc_ioh = ioh;
/* Send a reset. */
bus_space_write_1(iot, ioh, MCD_RESET, 0);
delay(1000000);
/* Get any pending status and throw away. */
for (i = 10; i; i--)
bus_space_read_1(iot, ioh, MCD_STATUS);
delay(1000);
/* Send get status command. */
mbx.cmd.opcode = MCD_CMDGETSTAT;
mbx.cmd.length = 0;
mbx.res.length = 0;
if (mcd_send(sc, &mbx, 0) != 0)
return 0;
/* Get info about the drive. */
mbx.cmd.opcode = MCD_CMDCONTINFO;
mbx.cmd.length = 0;
mbx.res.length = sizeof(mbx.res.data.continfo);
if (mcd_send(sc, &mbx, 0) != 0)
return 0;
/*
* The following is code which is not guaranteed to work for all
* drives, because the meaning of the expected 'M' is not clear
* (M_itsumi is an obvious assumption, but I don't trust that).
* Also, the original hack had a bogus condition that always
* returned true.
*
* Note: Which models support interrupts? >=LU005S?
*/
sc->readcmd = MCD_CMDREADSINGLESPEED;
switch (mbx.res.data.continfo.code) {
case 'M':
if (mbx.res.data.continfo.version <= 2)
sc->type = "LU002S";
else if (mbx.res.data.continfo.version <= 5)
sc->type = "LU005S";
else
sc->type = "LU006S";
break;
case 'F':
sc->type = "FX001";
break;
case 'D':
sc->type = "FX001D";
sc->readcmd = MCD_CMDREADDOUBLESPEED;
break;
default:
/*
* mcd_send() says the response looked OK but the
* drive type is unknown. If mcd_promisc, match anyway.
*/
if (mcd_promisc != 0)
return 0;
#ifdef MCDDEBUG
printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n",
device_xname(sc->sc_dev),
mbx.res.data.continfo.code, mbx.res.data.continfo.version);
#endif
sc->type = 0;
break;
}
if (ia->ia_nio < 1)
return (0);
if (ia->ia_nirq < 1)
return (0);
if (ISA_DIRECT_CONFIG(ia))
return (0);
/* Disallow wildcarded i/o address. */
if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
return (0);
if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
return (0);
/* Map i/o space */
if (bus_space_map(iot, ia->ia_io[0].ir_addr, MCD_NPORT, 0, &ioh))
return 0;
sc.debug = 0;
sc.probe = 1;
rv = mcd_find(iot, ioh, &sc);
bus_space_unmap(iot, ioh, MCD_NPORT);
if (rv) {
ia->ia_nio = 1;
ia->ia_io[0].ir_size = MCD_NPORT;
ia->ia_nirq = 1;
ia->ia_niomem = 0;
ia->ia_ndrq = 0;
}
return (rv);
}
int
mcd_getreply(struct mcd_softc *sc)
{
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
int i;
/* Wait until xfer port senses data ready. */
for (i = DELAY_GETREPLY; i; i--) {
if ((bus_space_read_1(iot, ioh, MCD_XFER) &
MCD_XF_STATUSUNAVAIL) == 0)
break;
delay(DELAY_GRANULARITY);
}
if (!i)
return -1;
/* Get the data. */
return bus_space_read_1(iot, ioh, MCD_STATUS);
}
int
mcd_getstat(struct mcd_softc *sc)
{
struct mcd_mbox mbx;
/*
* State machine to process read requests.
* Initialize with MCD_S_BEGIN: calculate sizes, and set mode
* MCD_S_WAITMODE: waits for status reply from set mode, set read command
* MCD_S_WAITREAD: wait for read ready, read data.
*/
int
mcdintr(void *arg)
{
struct mcd_softc *sc = arg;
struct mcd_mbx *mbx = &sc->mbx;
struct buf *bp = mbx->bp;
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
int i;
u_char x;
bcd_t msf[3];
switch (mbx->state) {
case MCD_S_IDLE:
return 0;
case MCD_S_BEGIN:
tryagain:
if (mbx->mode == sc->lastmode)
goto firstblock;
int
mcd_read_subchannel(struct mcd_softc *sc, struct ioc_read_subchannel *ch,
struct cd_sub_channel_info *info)
{
int len = ch->data_len;
union mcd_qchninfo q;
daddr_t lba;
int error;
if (sc->debug)
printf("%s: subchan: af=%d df=%d\n", device_xname(sc->sc_dev),
ch->address_format, ch->data_format);
if (len > sizeof(*info) || len < sizeof(info->header))
return EINVAL;
if (ch->address_format != CD_MSF_FORMAT &&
ch->address_format != CD_LBA_FORMAT)
return EINVAL;
if (ch->data_format != CD_CURRENT_POSITION &&
ch->data_format != CD_MEDIA_CATALOG)
return EINVAL;
if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0)
return error;
case CD_CURRENT_POSITION:
info->what.position.track_number = bcd2bin(q.current.trk_no);
info->what.position.index_number = bcd2bin(q.current.idx_no);
switch (ch->address_format) {
case CD_MSF_FORMAT:
info->what.position.reladdr.addr[0] = 0;
info->what.position.reladdr.addr[1] = bcd2bin(q.current.relative_pos[0]);
info->what.position.reladdr.addr[2] = bcd2bin(q.current.relative_pos[1]);
info->what.position.reladdr.addr[3] = bcd2bin(q.current.relative_pos[2]);
info->what.position.absaddr.addr[0] = 0;
info->what.position.absaddr.addr[1] = bcd2bin(q.current.absolute_pos[0]);
info->what.position.absaddr.addr[2] = bcd2bin(q.current.absolute_pos[1]);
info->what.position.absaddr.addr[3] = bcd2bin(q.current.absolute_pos[2]);
break;
case CD_LBA_FORMAT:
lba = msf2hsg(q.current.relative_pos, 1);
/*
* Pre-gap has index number of 0, and decreasing MSF
* address. Must be converted to negative LBA, per
* SCSI spec.
*/
if (info->what.position.index_number == 0x00)
lba = -lba;
info->what.position.reladdr.addr[0] = lba >> 24;
info->what.position.reladdr.addr[1] = lba >> 16;
info->what.position.reladdr.addr[2] = lba >> 8;
info->what.position.reladdr.addr[3] = lba;
lba = msf2hsg(q.current.absolute_pos, 0);
info->what.position.absaddr.addr[0] = lba >> 24;
info->what.position.absaddr.addr[1] = lba >> 16;
info->what.position.absaddr.addr[2] = lba >> 8;
info->what.position.absaddr.addr[3] = lba;
break;
}
break;
}
return 0;
}
int
mcd_playtracks(struct mcd_softc *sc, struct ioc_play_track *p)
{
struct mcd_mbox mbx;
int a = p->start_track;
int z = p->end_track;
int error;
if (sc->debug)
printf("%s: playtracks: from %d:%d to %d:%d\n",
device_xname(sc->sc_dev),
a, p->start_index, z, p->end_index);
if (a < bcd2bin(sc->volinfo.trk_low) ||
a > bcd2bin(sc->volinfo.trk_high) ||
a > z ||
z < bcd2bin(sc->volinfo.trk_low) ||
z > bcd2bin(sc->volinfo.trk_high))
return EINVAL;
if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
return error;
int
mcd_playmsf(struct mcd_softc *sc, struct ioc_play_msf *p)
{
struct mcd_mbox mbx;
int error;
if (sc->debug)
printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n",
device_xname(sc->sc_dev),
p->start_m, p->start_s, p->start_f,
p->end_m, p->end_s, p->end_f);
int
mcd_pause(struct mcd_softc *sc)
{
union mcd_qchninfo q;
int error;
/* Verify current status. */
if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS) {
printf("%s: pause: attempted when not playing\n",
device_xname(sc->sc_dev));
return EINVAL;
}
/* Get the current position. */
if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0)
return error;
/* Copy it into lastpb. */
sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0];
sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1];
sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2];