diff options
-rw-r--r-- | sys/conf/NOTES | 6 | ||||
-rw-r--r-- | sys/conf/files | 2 | ||||
-rw-r--r-- | sys/conf/files.i386 | 1 | ||||
-rw-r--r-- | sys/conf/files.pc98 | 1 | ||||
-rw-r--r-- | sys/dev/scd/scd.c | 725 | ||||
-rw-r--r-- | sys/dev/scd/scd_isa.c | 183 | ||||
-rw-r--r-- | sys/dev/scd/scdreg.h | 6 | ||||
-rw-r--r-- | sys/dev/scd/scdvar.h | 68 | ||||
-rw-r--r-- | sys/i386/conf/NOTES | 5 | ||||
-rw-r--r-- | sys/i386/isa/scd.c | 1549 | ||||
-rw-r--r-- | sys/i386/isa/scdreg.h | 151 | ||||
-rw-r--r-- | sys/modules/scd/Makefile | 9 |
12 files changed, 605 insertions, 2101 deletions
diff --git a/sys/conf/NOTES b/sys/conf/NOTES index 9379a59..262a716 100644 --- a/sys/conf/NOTES +++ b/sys/conf/NOTES @@ -1793,6 +1793,7 @@ hint.gusc.0.flags="0x13" # # Miscellaneous hardware: # +# scd: Sony CD-ROM using proprietary (non-ATAPI) interface # mcd: Mitsumi CD-ROM using proprietary (non-ATAPI) interface # meteor: Matrox Meteor video capture board # bktr: Brooktree bt848/848a/849a/878/879 video capture and TV Tuner board @@ -1844,7 +1845,10 @@ hint.gusc.0.flags="0x13" device mcd hint.mcd.0.at="isa" hint.mcd.0.port="0x300" - +# for the Sony CDU31/33A CDROM +device scd +hint.scd.0.at="isa" +hint.scd.0.port="0x230" device joy # PnP aware, hints for nonpnp only hint.joy.0.at="isa" hint.joy.0.port="0x201" diff --git a/sys/conf/files b/sys/conf/files index 8aec70d..8b8d15f 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -621,6 +621,8 @@ dev/rp/rp.c optional rp dev/rp/rp_isa.c optional rp isa dev/rp/rp_pci.c optional rp pci dev/sab/sab.c optional sab ebus +dev/scd/scd.c optional scd isa +dev/scd/scd_isa.c optional scd isa dev/si/si.c optional si dev/si/si2_z280.c optional si dev/si/si3_t225.c optional si diff --git a/sys/conf/files.i386 b/sys/conf/files.i386 index 9985160..ce3c012 100644 --- a/sys/conf/files.i386 +++ b/sys/conf/files.i386 @@ -286,7 +286,6 @@ i386/isa/pcvt/pcvt_sup.c optional vt i386/isa/pcvt/pcvt_vtf.c optional vt i386/isa/pmtimer.c optional pmtimer i386/isa/prof_machdep.c optional profiling-routine -i386/isa/scd.c count scd nowerror i386/isa/spic.c optional spic i386/isa/spigot.c count spigot i386/isa/spkr.c optional speaker diff --git a/sys/conf/files.pc98 b/sys/conf/files.pc98 index 90819a6..b0993b2 100644 --- a/sys/conf/files.pc98 +++ b/sys/conf/files.pc98 @@ -255,7 +255,6 @@ i386/isa/npx.c optional npx i386/isa/pcf.c optional pcf i386/isa/pmtimer.c optional pmtimer i386/isa/prof_machdep.c optional profiling-routine -i386/isa/scd.c count scd i386/isa/spigot.c count spigot i386/isa/stallion.c optional stl nowerror i386/isa/tw.c count tw diff --git a/sys/dev/scd/scd.c b/sys/dev/scd/scd.c index b758035..84e2ce8 100644 --- a/sys/dev/scd/scd.c +++ b/sys/dev/scd/scd.c @@ -49,7 +49,6 @@ #define SCD_DEBUG 0 -#include "scd.h" #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> @@ -61,9 +60,15 @@ #include <machine/stdarg.h> -#include <i386/isa/isa_device.h> -#include <i386/isa/scdreg.h> +#include <machine/bus_pio.h> +#include <machine/bus.h> +#include <machine/resource.h> +#include <sys/rman.h> +#include <isa/isavar.h> + +#include <dev/scd/scdreg.h> +#include <dev/scd/scdvar.h> #define scd_part(dev) ((minor(dev)) & 7) #define scd_unit(dev) (((minor(dev)) & 0x38) >> 3) @@ -94,93 +99,54 @@ #ifdef SCD_DEBUG static int scd_debuglevel = SCD_DEBUG; -# define XDEBUG(level, data) {if (scd_debuglevel >= level) printf data;} +# define XDEBUG(sc, level, fmt, args...) \ + do { \ + if (scd_debuglevel >= level) \ + device_printf(sc->dev, fmt, ## args); \ + } while (0) #else -# define XDEBUG(level, data) +# define XDEBUG(sc, level, fmt, args...) #endif -struct scd_mbx { - short unit; - short port; - short retry; - short nblk; - int sz; - u_long skip; - struct bio *bp; - int p_offset; - short count; -}; - -static struct scd_data { - int iobase; - char double_speed; - char *name; - short flags; - int blksize; - u_long disksize; - struct disklabel dlabel; - int openflag; - struct { - unsigned int adr :4; - unsigned int ctl :4; /* xcdplayer needs this */ - unsigned char start_msf[3]; - } toc[MAX_TRACKS]; - short first_track; - short last_track; - struct ioc_play_msf last_play; - - short audio_status; - struct bio_queue_head head; /* head of bio queue */ - struct scd_mbx mbx; -} scd_data[NSCD]; +#define IS_ATTENTION(sc) ((SCD_READ(sc, IREG_STATUS) & SBIT_ATTENTION) != 0) +#define IS_BUSY(sc) ((SCD_READ(sc, IREG_STATUS) & SBIT_BUSY) != 0) +#define IS_DATA_RDY(sc) ((SCD_READ(sc, IREG_STATUS) & SBIT_DATA_READY) != 0) +#define STATUS_BIT(sc, bit) ((SCD_READ(sc, IREG_STATUS) & (bit)) != 0) +#define FSTATUS_BIT(sc, bit) ((SCD_READ(sc, IREG_FSTATUS) & (bit)) != 0) /* prototypes */ static void hsg2msf(int hsg, bcd_t *msf); static int msf2hsg(bcd_t *msf); -static void process_attention(unsigned unit); -static __inline void write_control(unsigned port, unsigned data); -static int waitfor_status_bits(int unit, int bits_set, int bits_clear); -static int send_cmd(u_int unit, u_char cmd, u_int nargs, ...); -static void init_drive(unsigned unit); -static int spin_up(unsigned unit); -static int read_toc(unsigned unit); -static int get_result(u_int unit, int result_len, u_char *result); -static void print_error(int unit, int errcode); - -static void scd_start(int unit); +static void process_attention(struct scd_softc *); +static int waitfor_status_bits(struct scd_softc *, int bits_set, int bits_clear); +static int send_cmd(struct scd_softc *, u_char cmd, u_int nargs, ...); +static void init_drive(struct scd_softc *); +static int spin_up(struct scd_softc *); +static int read_toc(struct scd_softc *); +static int get_result(struct scd_softc *, int result_len, u_char *result); +static void print_error(struct scd_softc *, int errcode); + +static void scd_start(struct scd_softc *); static timeout_t scd_timeout; -static void scd_doread(int state, struct scd_mbx *mbxin); - -static int scd_eject(int unit); -static int scd_stop(int unit); -static int scd_pause(int unit); -static int scd_resume(int unit); -static int scd_playtracks(int unit, struct ioc_play_track *pt); -static int scd_playmsf(int unit, struct ioc_play_msf *msf); -static int scd_play(int unit, struct ioc_play_msf *msf); -static int scd_subchan(int unit, struct ioc_read_subchannel *sc); -static int read_subcode(int unit, struct sony_subchannel_position_data *sc); +static void scd_doread(struct scd_softc *, int state, struct scd_mbx *mbxin); + +static int scd_eject(struct scd_softc *); +static int scd_stop(struct scd_softc *); +static int scd_pause(struct scd_softc *); +static int scd_resume(struct scd_softc *); +static int scd_playtracks(struct scd_softc *, struct ioc_play_track *pt); +static int scd_playmsf(struct scd_softc *, struct ioc_play_msf *msf); +static int scd_play(struct scd_softc *, struct ioc_play_msf *msf); +static int scd_subchan(struct scd_softc *, struct ioc_read_subchannel *sch); +static int read_subcode(struct scd_softc *, struct sony_subchannel_position_data *sch); /* for xcdplayer */ -static int scd_toc_header(int unit, struct ioc_toc_header *th); -static int scd_toc_entrys(int unit, struct ioc_read_toc_entry *te); -static int scd_toc_entry(int unit, struct ioc_read_toc_single_entry *te); +static int scd_toc_header(struct scd_softc *, struct ioc_toc_header *th); +static int scd_toc_entrys(struct scd_softc *, struct ioc_read_toc_entry *te); +static int scd_toc_entry(struct scd_softc *, struct ioc_read_toc_single_entry *te); #define SCD_LASTPLUS1 170 /* don't ask, xcdplayer passes this in */ -static int scd_probe(struct isa_device *dev); -static int scd_attach(struct isa_device *dev); -struct isa_driver scddriver = { - INTR_TYPE_BIO, - scd_probe, - scd_attach, - "scd" -}; -COMPAT_ISA_DRIVER(scd, scddriver); - -/* For canceling our timeout */ -static struct callout_handle tohandle = CALLOUT_HANDLE_INITIALIZER(&tohanle); - static d_open_t scdopen; static d_close_t scdclose; static d_ioctl_t scdioctl; @@ -205,42 +171,39 @@ static struct cdevsw scd_cdevsw = { }; -static int -scd_attach(struct isa_device *dev) +int +scd_attach(struct scd_softc *sc) { - int unit = dev->id_unit; - struct scd_data *cd = scd_data + unit; - - cd->iobase = dev->id_iobase; /* Already set by probe, but ... */ + struct scd_data *cd; + int unit; - /* name filled in probe */ - printf("scd%d: <%s>\n", dev->id_unit, scd_data[dev->id_unit].name); + cd = &sc->data; + unit = device_get_unit(sc->dev); - init_drive(dev->id_unit); + init_drive(sc); cd->flags = SCDINIT; cd->audio_status = CD_AS_AUDIO_INVALID; bioq_init(&cd->head); - make_dev(&scd_cdevsw, dkmakeminor(unit, 0, 0), - UID_ROOT, GID_OPERATOR, 0640, "scd%da", unit); - make_dev(&scd_cdevsw, dkmakeminor(unit, 0, RAW_PART), - UID_ROOT, GID_OPERATOR, 0640, "scd%dc", unit); - return 1; + sc->scd_dev_t = make_dev(&scd_cdevsw, 8 * unit, + UID_ROOT, GID_OPERATOR, 0640, "scd%d", unit); + sc->scd_dev_t->si_drv1 = (void *)sc; + + return 0; } static int scdopen(dev_t dev, int flags, int fmt, struct thread *td) { - int unit,part,phys; + struct scd_softc *sc; + int part,phys; int rc; struct scd_data *cd; - unit = scd_unit(dev); - if (unit >= NSCD) - return ENXIO; + sc = (struct scd_softc *)dev->si_drv1; - cd = scd_data + unit; + cd = &sc->data; part = scd_part(dev); phys = scd_phys(dev); @@ -252,25 +215,25 @@ scdopen(dev_t dev, int flags, int fmt, struct thread *td) if (cd->openflag) return ENXIO; - XDEBUG(1,("scd%d: DEBUG: status = 0x%x\n", unit, inb(cd->iobase+IREG_STATUS))); + XDEBUG(sc, 1, "DEBUG: status = 0x%x\n", SCD_READ(sc, IREG_STATUS)); - if ((rc = spin_up(unit)) != 0) { - print_error(unit, rc); + if ((rc = spin_up(sc)) != 0) { + print_error(sc, rc); return EIO; } if (!(cd->flags & SCDTOC)) { int loop_count = 3; - while (loop_count-- > 0 && (rc = read_toc(unit)) != 0) { + while (loop_count-- > 0 && (rc = read_toc(sc)) != 0) { if (rc == ERR_NOT_SPINNING) { - rc = spin_up(unit); + rc = spin_up(sc); if (rc) { - print_error(unit, rc);\ + print_error(sc, rc);\ return EIO; } continue; } - printf("scd%d: TOC read error 0x%x\n", unit, rc); + device_printf(sc->dev, "TOC read error 0x%x\n", rc); return EIO; } } @@ -286,14 +249,13 @@ scdopen(dev_t dev, int flags, int fmt, struct thread *td) static int scdclose(dev_t dev, int flags, int fmt, struct thread *td) { - int unit,part,phys; + struct scd_softc *sc; + int part,phys; struct scd_data *cd; - unit = scd_unit(dev); - if (unit >= NSCD) - return ENXIO; + sc = (struct scd_softc *)dev->si_drv1; - cd = scd_data + unit; + cd = &sc->data; part = scd_part(dev); phys = scd_phys(dev); @@ -301,7 +263,7 @@ scdclose(dev_t dev, int flags, int fmt, struct thread *td) return ENXIO; if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS) { - (void)send_cmd(unit, CMD_SPIN_DOWN, 0); + (void)send_cmd(sc, CMD_SPIN_DOWN, 0); cd->flags &= ~SCDSPINNING; } @@ -317,16 +279,17 @@ scdstrategy(struct bio *bp) { struct scd_data *cd; int s; - int unit = scd_unit(bp->bio_dev); + struct scd_softc *sc; - cd = scd_data + unit; + sc = (struct scd_softc *)bp->bio_dev->si_drv1; + cd = &sc->data; - XDEBUG(2, ("scd%d: DEBUG: strategy: block=%ld, bcount=%ld\n", - unit, (long)bp->bio_blkno, bp->bio_bcount)); + XDEBUG(sc, 2, "DEBUG: strategy: block=%ld, bcount=%ld\n", + (long)bp->bio_blkno, bp->bio_bcount); - if (unit >= NSCD || bp->bio_blkno < 0 || (bp->bio_bcount % SCDBLKSIZE)) { - printf("scd%d: strategy failure: blkno = %ld, bcount = %ld\n", - unit, (long)bp->bio_blkno, bp->bio_bcount); + if (bp->bio_blkno < 0 || (bp->bio_bcount % SCDBLKSIZE)) { + device_printf(sc->dev, "strategy failure: blkno = %ld, bcount = %ld\n", + (long)bp->bio_blkno, bp->bio_bcount); bp->bio_error = EINVAL; bp->bio_flags |= BIO_ERROR; goto bad; @@ -334,7 +297,7 @@ scdstrategy(struct bio *bp) /* if device invalidated (e.g. media change, door open), error */ if (!(cd->flags & SCDVALID)) { - printf("scd%d: media changed\n", unit); + device_printf(sc->dev, "media changed\n"); bp->bio_error = EIO; goto bad; } @@ -366,7 +329,7 @@ scdstrategy(struct bio *bp) splx(s); /* now check whether we can perform processing */ - scd_start(unit); + scd_start(sc); return; bad: @@ -378,9 +341,9 @@ done: } static void -scd_start(int unit) +scd_start(struct scd_softc *sc) { - struct scd_data *cd = scd_data + unit; + struct scd_data *cd = &sc->data; struct bio *bp; struct partition *p; int s = splbio(); @@ -404,14 +367,12 @@ scd_start(int unit) p = cd->dlabel.d_partitions + scd_part(bp->bio_dev); - cd->mbx.unit = unit; - cd->mbx.port = cd->iobase; cd->mbx.retry = 3; cd->mbx.bp = bp; cd->mbx.p_offset = p->p_offset; splx(s); - scd_doread(SCD_S_BEGIN,&(cd->mbx)); + scd_doread(sc, SCD_S_BEGIN, &(cd->mbx)); return; } @@ -419,13 +380,14 @@ static int scdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td) { struct scd_data *cd; - int unit,part; + struct scd_softc *sc; + int part; - unit = scd_unit(dev); + sc = (struct scd_softc *)dev->si_drv1; part = scd_part(dev); - cd = scd_data + unit; + cd = &sc->data; - XDEBUG(1, ("scd%d: ioctl: cmd=0x%lx\n", unit, cmd)); + XDEBUG(sc, 1, "ioctl: cmd=0x%lx\n", cmd); if (!(cd->flags & SCDVALID)) return EIO; @@ -435,19 +397,19 @@ scdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td) *(struct disklabel *)addr = cd->dlabel; return 0; case CDIOCPLAYTRACKS: - return scd_playtracks(unit, (struct ioc_play_track *) addr); + return scd_playtracks(sc, (struct ioc_play_track *) addr); case CDIOCPLAYBLOCKS: return EINVAL; case CDIOCPLAYMSF: - return scd_playmsf(unit, (struct ioc_play_msf *) addr); + return scd_playmsf(sc, (struct ioc_play_msf *) addr); case CDIOCREADSUBCHANNEL: - return scd_subchan(unit, (struct ioc_read_subchannel *) addr); + return scd_subchan(sc, (struct ioc_read_subchannel *) addr); case CDIOREADTOCHEADER: - return scd_toc_header (unit, (struct ioc_toc_header *) addr); + return scd_toc_header (sc, (struct ioc_toc_header *) addr); case CDIOREADTOCENTRYS: - return scd_toc_entrys (unit, (struct ioc_read_toc_entry*) addr); + return scd_toc_entrys (sc, (struct ioc_read_toc_entry*) addr); case CDIOREADTOCENTRY: - return scd_toc_entry (unit, (struct ioc_read_toc_single_entry*) addr); + return scd_toc_entry (sc, (struct ioc_read_toc_single_entry*) addr); case CDIOCSETPATCH: case CDIOCGETVOL: case CDIOCSETVOL: @@ -458,15 +420,15 @@ scdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td) case CDIOCSETRIGHT: return EINVAL; case CDIOCRESUME: - return scd_resume(unit); + return scd_resume(sc); case CDIOCPAUSE: - return scd_pause(unit); + return scd_pause(sc); case CDIOCSTART: return EINVAL; case CDIOCSTOP: - return scd_stop(unit); + return scd_stop(sc); case CDIOCEJECT: - return scd_eject(unit); + return scd_eject(sc); case CDIOCALLOW: return 0; case CDIOCSETDEBUG: @@ -481,7 +443,7 @@ scdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td) #endif return 0; default: - printf("scd%d: unsupported ioctl (cmd=0x%lx)\n", unit, cmd); + device_printf(sc->dev, "unsupported ioctl (cmd=0x%lx)\n", cmd); return ENOTTY; } } @@ -491,28 +453,28 @@ scdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td) **************************************************************/ static int -scd_playtracks(int unit, struct ioc_play_track *pt) +scd_playtracks(struct scd_softc *sc, struct ioc_play_track *pt) { - struct scd_data *cd = scd_data + unit; + struct scd_data *cd = &sc->data; struct ioc_play_msf msf; int a = pt->start_track; int z = pt->end_track; int rc; - if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) { + if (!(cd->flags & SCDTOC) && (rc = read_toc(sc)) != 0) { if (rc == -ERR_NOT_SPINNING) { - if (spin_up(unit) != 0) + if (spin_up(sc) != 0) return EIO; - rc = read_toc(unit); + rc = read_toc(sc); } if (rc != 0) { - print_error(unit, rc); + print_error(sc, rc); return EIO; } } - XDEBUG(1, ("scd%d: playtracks from %d:%d to %d:%d\n", unit, - a, pt->start_index, z, pt->end_index)); + XDEBUG(sc, 1, "playtracks from %d:%d to %d:%d\n", + a, pt->start_index, z, pt->end_index); if ( a < cd->first_track || a > cd->last_track @@ -523,12 +485,12 @@ scd_playtracks(int unit, struct ioc_play_track *pt) bcopy(cd->toc[a].start_msf, &msf.start_m, 3); hsg2msf(msf2hsg(cd->toc[z+1].start_msf)-1, &msf.end_m); - return scd_play(unit, &msf); + return scd_play(sc, &msf); } /* The start/end msf is expected to be in bin format */ static int -scd_playmsf(int unit, struct ioc_play_msf *msfin) +scd_playmsf(struct scd_softc *sc, struct ioc_play_msf *msfin) { struct ioc_play_msf msf; @@ -539,31 +501,31 @@ scd_playmsf(int unit, struct ioc_play_msf *msfin) msf.end_s = bin2bcd(msfin->end_s); msf.end_f = bin2bcd(msfin->end_f); - return scd_play(unit, &msf); + return scd_play(sc, &msf); } /* The start/end msf is expected to be in bcd format */ static int -scd_play(int unit, struct ioc_play_msf *msf) +scd_play(struct scd_softc *sc, struct ioc_play_msf *msf) { - struct scd_data *cd = scd_data + unit; + struct scd_data *cd = &sc->data; int i, rc; - XDEBUG(1, ("scd%d: playing: %02x:%02x:%02x -> %02x:%02x:%02x\n", unit, + XDEBUG(sc, 1, "playing: %02x:%02x:%02x -> %02x:%02x:%02x\n", msf->start_m, msf->start_s, msf->start_f, - msf->end_m, msf->end_s, msf->end_f)); + msf->end_m, msf->end_s, msf->end_f); for (i = 0; i < 2; i++) { - rc = send_cmd(unit, CMD_PLAY_AUDIO, 7, + rc = send_cmd(sc, CMD_PLAY_AUDIO, 7, 0x03, msf->start_m, msf->start_s, msf->start_f, msf->end_m, msf->end_s, msf->end_f); if (rc == -ERR_NOT_SPINNING) { cd->flags &= ~SCDSPINNING; - if (spin_up(unit) != 0) + if (spin_up(sc) != 0) return EIO; } else if (rc < 0) { - print_error(unit, rc); + print_error(sc, rc); return EIO; } else { break; @@ -575,28 +537,28 @@ scd_play(int unit, struct ioc_play_msf *msf) } static int -scd_stop(int unit) +scd_stop(struct scd_softc *sc) { - struct scd_data *cd = scd_data + unit; + struct scd_data *cd = &sc->data; - (void)send_cmd(unit, CMD_STOP_AUDIO, 0); + (void)send_cmd(sc, CMD_STOP_AUDIO, 0); cd->audio_status = CD_AS_PLAY_COMPLETED; return 0; } static int -scd_pause(int unit) +scd_pause(struct scd_softc *sc) { - struct scd_data *cd = scd_data + unit; + struct scd_data *cd = &sc->data; struct sony_subchannel_position_data subpos; if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS) return EINVAL; - if (read_subcode(unit, &subpos) != 0) + if (read_subcode(sc, &subpos) != 0) return EIO; - if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0) + if (send_cmd(sc, CMD_STOP_AUDIO, 0) != 0) return EIO; cd->last_play.start_m = subpos.abs_msf[0]; @@ -604,33 +566,34 @@ scd_pause(int unit) cd->last_play.start_f = subpos.abs_msf[2]; cd->audio_status = CD_AS_PLAY_PAUSED; - XDEBUG(1, ("scd%d: pause @ %02x:%02x:%02x\n", unit, + XDEBUG(sc, 1, "pause @ %02x:%02x:%02x\n", cd->last_play.start_m, cd->last_play.start_s, - cd->last_play.start_f)); + cd->last_play.start_f); return 0; } static int -scd_resume(int unit) +scd_resume(struct scd_softc *sc) { - if (scd_data[unit].audio_status != CD_AS_PLAY_PAUSED) + + if (sc->data.audio_status != CD_AS_PLAY_PAUSED) return EINVAL; - return scd_play(unit, &scd_data[unit].last_play); + return scd_play(sc, &sc->data.last_play); } static int -scd_eject(int unit) +scd_eject(struct scd_softc *sc) { - struct scd_data *cd = scd_data + unit; + struct scd_data *cd = &sc->data; cd->audio_status = CD_AS_AUDIO_INVALID; cd->flags &= ~(SCDSPINNING|SCDTOC); - if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0 || - send_cmd(unit, CMD_SPIN_DOWN, 0) != 0 || - send_cmd(unit, CMD_EJECT, 0) != 0) + if (send_cmd(sc, CMD_STOP_AUDIO, 0) != 0 || + send_cmd(sc, CMD_SPIN_DOWN, 0) != 0 || + send_cmd(sc, CMD_EJECT, 0) != 0) { return EIO; } @@ -638,23 +601,22 @@ scd_eject(int unit) } static int -scd_subchan(int unit, struct ioc_read_subchannel *sc) +scd_subchan(struct scd_softc *sc, struct ioc_read_subchannel *sch) { - struct scd_data *cd = scd_data + unit; + struct scd_data *cd = &sc->data; struct sony_subchannel_position_data q; struct cd_sub_channel_info data; - XDEBUG(1, ("scd%d: subchan af=%d, df=%d\n", unit, - sc->address_format, - sc->data_format)); + XDEBUG(sc, 1, "subchan af=%d, df=%d\n", + sch->address_format, sch->data_format); - if (sc->address_format != CD_MSF_FORMAT) + if (sch->address_format != CD_MSF_FORMAT) return EINVAL; - if (sc->data_format != CD_CURRENT_POSITION) + if (sch->data_format != CD_CURRENT_POSITION) return EINVAL; - if (read_subcode(unit, &q) != 0) + if (read_subcode(sc, &q) != 0) return EIO; data.header.audio_status = cd->audio_status; @@ -669,54 +631,48 @@ scd_subchan(int unit, struct ioc_read_subchannel *sc) data.what.position.absaddr.msf.second = bcd2bin(q.abs_msf[1]); data.what.position.absaddr.msf.frame = bcd2bin(q.abs_msf[2]); - if (copyout(&data, sc->data, min(sizeof(struct cd_sub_channel_info), sc->data_len))!=0) + if (copyout(&data, sch->data, min(sizeof(struct cd_sub_channel_info), sch->data_len))!=0) return EFAULT; return 0; } -static __inline void -write_control(unsigned port, unsigned data) -{ - outb(port + OREG_CONTROL, data); -} - -static int -scd_probe(struct isa_device *dev) +int +scd_probe(struct scd_softc *sc) { struct sony_drive_configuration drive_config; - int unit = dev->id_unit; + struct scd_data *cd; int rc; static char namebuf[8+16+8+3]; char *s = namebuf; int loop_count = 0; - scd_data[unit].flags = SCDPROBING; - scd_data[unit].iobase = dev->id_iobase; + cd = &sc->data; + cd->flags = SCDPROBING; bzero(&drive_config, sizeof(drive_config)); again: /* Reset drive */ - write_control(dev->id_iobase, CBIT_RESET_DRIVE); + SCD_WRITE(sc, OREG_CONTROL, CBIT_RESET_DRIVE); /* Calm down */ DELAY(300000); /* Only the ATTENTION bit may be set */ - if ((inb(dev->id_iobase+IREG_STATUS) & ~1) != 0) { - XDEBUG(1, ("scd: too many bits set. probe failed.\n")); - return 0; + if ((SCD_READ(sc, IREG_STATUS) & ~1) != 0) { + XDEBUG(sc, 1, "too many bits set. probe failed.\n"); + return (ENXIO); } - rc = send_cmd(unit, CMD_GET_DRIVE_CONFIG, 0); + rc = send_cmd(sc, CMD_GET_DRIVE_CONFIG, 0); if (rc != sizeof(drive_config)) { /* Sometimes if the drive is playing audio I get */ /* the bad result 82. Fix by repeating the reset */ if (rc > 0 && loop_count++ == 0) goto again; - return 0; + return (ENXIO); } - if (get_result(unit, rc, (u_char *)&drive_config) != 0) - return 0; + if (get_result(sc, rc, (u_char *)&drive_config) != 0) + return (ENXIO); bcopy(drive_config.vendor, namebuf, 8); s = namebuf+8; @@ -734,25 +690,25 @@ again: s--; *s = 0; - scd_data[unit].name = namebuf; + cd->name = namebuf; if (drive_config.config & 0x10) - scd_data[unit].double_speed = 1; + cd->double_speed = 1; else - scd_data[unit].double_speed = 0; + cd->double_speed = 0; - return 4; + return (0); } static int -read_subcode(int unit, struct sony_subchannel_position_data *sc) +read_subcode(struct scd_softc *sc, struct sony_subchannel_position_data *scp) { int rc; - rc = send_cmd(unit, CMD_GET_SUBCHANNEL_DATA, 0); - if (rc < 0 || rc < sizeof(*sc)) + rc = send_cmd(sc, CMD_GET_SUBCHANNEL_DATA, 0); + if (rc < 0 || rc < sizeof(*scp)) return EIO; - if (get_result(unit, rc, (u_char *)sc) != 0) + if (get_result(sc, rc, (u_char *)scp) != 0) return EIO; return 0; } @@ -760,8 +716,8 @@ read_subcode(int unit, struct sony_subchannel_position_data *sc) /* State machine copied from mcd.c */ /* This (and the code in mcd.c) will not work with more than one drive */ -/* because there is only one mbxsave below. Should fix that some day. */ -/* (mbxsave & state should probably be included in the scd_data struct and */ +/* because there is only one sc->ch_mbxsave below. Should fix that some day. */ +/* (sc->ch_mbxsave & state should probably be included in the scd_data struct and */ /* the unit number used as first argument to scd_doread().) /Micke */ /* state machine to process read requests @@ -773,23 +729,22 @@ read_subcode(int unit, struct sony_subchannel_position_data *sc) * SCD_S_WAITPARAM: wait for command result params, read them, error if bad data read. */ -static struct scd_mbx *mbxsave; - static void scd_timeout(void *arg) { - scd_doread((int)arg, mbxsave); + struct scd_softc *sc; + sc = (struct scd_softc *)arg; + + scd_doread(sc, sc->ch_state, sc->ch_mbxsave); } static void -scd_doread(int state, struct scd_mbx *mbxin) +scd_doread(struct scd_softc *sc, int state, struct scd_mbx *mbxin) { - struct scd_mbx *mbx = (state!=SCD_S_BEGIN) ? mbxsave : mbxin; - int unit = mbx->unit; - int port = mbx->port; + struct scd_mbx *mbx = (state!=SCD_S_BEGIN) ? sc->ch_mbxsave : mbxin; struct bio *bp = mbx->bp; - struct scd_data *cd = scd_data + unit; - int reg,i; + struct scd_data *cd = &sc->data; + int i; int blknum; caddr_t addr; static char sdata[3]; /* Must be preserved between calls to this function */ @@ -797,34 +752,35 @@ scd_doread(int state, struct scd_mbx *mbxin) loop: switch (state) { case SCD_S_BEGIN: - mbx = mbxsave = mbxin; + mbx = sc->ch_mbxsave = mbxin; case SCD_S_BEGIN1: /* get status */ mbx->count = RDELAY_WAIT; - process_attention(unit); + process_attention(sc); goto trystat; case SCD_S_WAITSTAT: - untimeout(scd_timeout,(caddr_t)SCD_S_WAITSTAT, tohandle); + sc->ch_state = SCD_S_WAITSTAT; + untimeout(scd_timeout, (caddr_t)sc, sc->ch); if (mbx->count-- <= 0) { - printf("scd%d: timeout. drive busy.\n",unit); + device_printf(sc->dev, "timeout. drive busy.\n"); goto harderr; } trystat: - if (IS_BUSY(port)) { - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITSTAT,hz/100); /* XXX */ + if (IS_BUSY(sc)) { + sc->ch_state = SCD_S_WAITSTAT; + sc->ch = timeout(scd_timeout, (caddr_t)sc, hz/100); /* XXX */ return; } - process_attention(unit); + process_attention(sc); /* reject, if audio active */ if (cd->audio_status & CD_AS_PLAY_IN_PROGRESS) { - printf("scd%d: audio is active\n",unit); + device_printf(sc->dev, "audio is active\n"); goto harderr; } @@ -841,164 +797,167 @@ nextblock: blknum = (bp->bio_blkno / (mbx->sz/DEV_BSIZE)) + mbx->p_offset + mbx->skip/mbx->sz; - XDEBUG(2, ("scd%d: scd_doread: read blknum=%d\n", unit, blknum)); + XDEBUG(sc, 2, "scd_doread: read blknum=%d\n", blknum); /* build parameter block */ hsg2msf(blknum, sdata); - write_control(port, CBIT_RESULT_READY_CLEAR); - write_control(port, CBIT_RPARAM_CLEAR); - write_control(port, CBIT_DATA_READY_CLEAR); + SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR); + SCD_WRITE(sc, OREG_CONTROL, CBIT_RPARAM_CLEAR); + SCD_WRITE(sc, OREG_CONTROL, CBIT_DATA_READY_CLEAR); - if (FSTATUS_BIT(port, FBIT_WPARAM_READY)) + if (FSTATUS_BIT(sc, FBIT_WPARAM_READY)) goto writeparam; mbx->count = 100; - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITFIFO,hz/100); /* XXX */ + sc->ch_state = SCD_S_WAITFIFO; + sc->ch = timeout(scd_timeout, (caddr_t)sc, hz/100); /* XXX */ return; case SCD_S_WAITSPIN: - untimeout(scd_timeout,(caddr_t)SCD_S_WAITSPIN, tohandle); + sc->ch_state = SCD_S_WAITSPIN; + untimeout(scd_timeout,(caddr_t)sc, sc->ch); if (mbx->count-- <= 0) { - printf("scd%d: timeout waiting for drive to spin up.\n", unit); + device_printf(sc->dev, "timeout waiting for drive to spin up.\n"); goto harderr; } - if (!STATUS_BIT(port, SBIT_RESULT_READY)) { - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITSPIN,hz/100); /* XXX */ + if (!STATUS_BIT(sc, SBIT_RESULT_READY)) { + sc->ch_state = SCD_S_WAITSPIN; + sc->ch = timeout(scd_timeout, (caddr_t)sc, hz/100); /* XXX */ return; } - write_control(port, CBIT_RESULT_READY_CLEAR); - switch ((i = inb(port+IREG_RESULT)) & 0xf0) { + SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR); + switch ((i = SCD_READ(sc, IREG_RESULT)) & 0xf0) { case 0x20: - i = inb(port+IREG_RESULT); - print_error(unit, i); + i = SCD_READ(sc, IREG_RESULT); + print_error(sc, i); goto harderr; case 0x00: - (void)inb(port+IREG_RESULT); + (void)SCD_READ(sc, IREG_RESULT); cd->flags |= SCDSPINNING; break; } - XDEBUG(1, ("scd%d: DEBUG: spin up complete\n", unit)); + XDEBUG(sc, 1, "DEBUG: spin up complete\n"); state = SCD_S_BEGIN1; goto loop; case SCD_S_WAITFIFO: - untimeout(scd_timeout,(caddr_t)SCD_S_WAITFIFO, tohandle); + sc->ch_state = SCD_S_WAITFIFO; + untimeout(scd_timeout,(caddr_t)sc, sc->ch); if (mbx->count-- <= 0) { - printf("scd%d: timeout. write param not ready.\n",unit); + device_printf(sc->dev, "timeout. write param not ready.\n"); goto harderr; } - if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) { - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITFIFO,hz/100); /* XXX */ + if (!FSTATUS_BIT(sc, FBIT_WPARAM_READY)) { + sc->ch_state = SCD_S_WAITFIFO; + sc->ch = timeout(scd_timeout, (caddr_t)sc,hz/100); /* XXX */ return; } - XDEBUG(1, ("scd%d: mbx->count (writeparamwait) = %d(%d)\n", unit, mbx->count, 100)); + XDEBUG(sc, 1, "mbx->count (writeparamwait) = %d(%d)\n", mbx->count, 100); writeparam: /* The reason this test isn't done 'till now is to make sure */ /* that it is ok to send the SPIN_UP cmd below. */ if (!(cd->flags & SCDSPINNING)) { - XDEBUG(1, ("scd%d: spinning up drive ...\n", unit)); - outb(port+OREG_COMMAND, CMD_SPIN_UP); + XDEBUG(sc, 1, "spinning up drive ...\n"); + SCD_WRITE(sc, OREG_COMMAND, CMD_SPIN_UP); mbx->count = 300; - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITSPIN,hz/100); /* XXX */ + sc->ch_state = SCD_S_WAITSPIN; + sc->ch = timeout(scd_timeout, (caddr_t)sc, hz/100); /* XXX */ return; } - reg = port + OREG_WPARAMS; /* send the read command */ disable_intr(); - outb(reg, sdata[0]); - outb(reg, sdata[1]); - outb(reg, sdata[2]); - outb(reg, 0); - outb(reg, 0); - outb(reg, 1); - outb(port+OREG_COMMAND, CMD_READ); + SCD_WRITE(sc, OREG_WPARAMS, sdata[0]); + SCD_WRITE(sc, OREG_WPARAMS, sdata[1]); + SCD_WRITE(sc, OREG_WPARAMS, sdata[2]); + SCD_WRITE(sc, OREG_WPARAMS, 0); + SCD_WRITE(sc, OREG_WPARAMS, 0); + SCD_WRITE(sc, OREG_WPARAMS, 1); + SCD_WRITE(sc, OREG_COMMAND, CMD_READ); enable_intr(); mbx->count = RDELAY_WAITREAD; for (i = 0; i < 50; i++) { - if (STATUS_BIT(port, SBIT_DATA_READY)) + if (STATUS_BIT(sc, SBIT_DATA_READY)) goto got_data; DELAY(100); } - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITREAD,hz/100); /* XXX */ + sc->ch_state = SCD_S_WAITREAD; + sc->ch = timeout(scd_timeout, (caddr_t)sc, hz/100); /* XXX */ return; case SCD_S_WAITREAD: - untimeout(scd_timeout,(caddr_t)SCD_S_WAITREAD, tohandle); + sc->ch_state = SCD_S_WAITREAD; + untimeout(scd_timeout,(caddr_t)sc, sc->ch); if (mbx->count-- <= 0) { - if (STATUS_BIT(port, SBIT_RESULT_READY)) + if (STATUS_BIT(sc, SBIT_RESULT_READY)) goto got_param; - printf("scd%d: timeout while reading data\n",unit); + device_printf(sc->dev, "timeout while reading data\n"); goto readerr; } - if (!STATUS_BIT(port, SBIT_DATA_READY)) { - process_attention(unit); + if (!STATUS_BIT(sc, SBIT_DATA_READY)) { + process_attention(sc); if (!(cd->flags & SCDVALID)) goto changed; - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITREAD,hz/100); /* XXX */ + sc->ch_state = SCD_S_WAITREAD; + sc->ch = timeout(scd_timeout, (caddr_t)sc, hz/100); /* XXX */ return; } - XDEBUG(2, ("scd%d: mbx->count (after RDY_BIT) = %d(%d)\n", unit, mbx->count, RDELAY_WAITREAD)); + XDEBUG(sc, 2, "mbx->count (after RDY_BIT) = %d(%d)\n", mbx->count, RDELAY_WAITREAD); got_data: /* data is ready */ addr = bp->bio_data + mbx->skip; - write_control(port, CBIT_DATA_READY_CLEAR); - insb(port+IREG_DATA, addr, mbx->sz); + SCD_WRITE(sc, OREG_CONTROL, CBIT_DATA_READY_CLEAR); + SCD_READ_MULTI(sc, IREG_DATA, addr, mbx->sz); mbx->count = 100; for (i = 0; i < 20; i++) { - if (STATUS_BIT(port, SBIT_RESULT_READY)) + if (STATUS_BIT(sc, SBIT_RESULT_READY)) goto waitfor_param; DELAY(100); } goto waitfor_param; case SCD_S_WAITPARAM: - untimeout(scd_timeout,(caddr_t)SCD_S_WAITPARAM, tohandle); + sc->ch_state = SCD_S_WAITPARAM; + untimeout(scd_timeout,(caddr_t)sc, sc->ch); if (mbx->count-- <= 0) { - printf("scd%d: timeout waiting for params\n",unit); + device_printf(sc->dev, "timeout waiting for params\n"); goto readerr; } waitfor_param: - if (!STATUS_BIT(port, SBIT_RESULT_READY)) { - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITPARAM,hz/100); /* XXX */ + if (!STATUS_BIT(sc, SBIT_RESULT_READY)) { + sc->ch_state = SCD_S_WAITPARAM; + sc->ch = timeout(scd_timeout, (caddr_t)sc, hz/100); /* XXX */ return; } #if SCD_DEBUG if (mbx->count < 100 && scd_debuglevel > 0) - printf("scd%d: mbx->count (paramwait) = %d(%d)\n", unit, mbx->count, 100); + device_printf(sc->dev, "mbx->count (paramwait) = %d(%d)\n", mbx->count, 100); #endif got_param: - write_control(port, CBIT_RESULT_READY_CLEAR); - switch ((i = inb(port+IREG_RESULT)) & 0xf0) { + SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR); + switch ((i = SCD_READ(sc, IREG_RESULT)) & 0xf0) { case 0x50: switch (i) { case ERR_FATAL_READ_ERROR1: case ERR_FATAL_READ_ERROR2: - printf("scd%d: unrecoverable read error 0x%x\n", unit, i); + device_printf(sc->dev, "unrecoverable read error 0x%x\n", i); goto harderr; } break; case 0x20: - i = inb(port+IREG_RESULT); + i = SCD_READ(sc, IREG_RESULT); switch (i) { case ERR_NOT_SPINNING: - XDEBUG(1, ("scd%d: read error: drive not spinning\n", unit)); + XDEBUG(sc, 1, "read error: drive not spinning\n"); if (mbx->retry-- > 0) { state = SCD_S_BEGIN1; cd->flags &= ~SCDSPINNING; @@ -1006,11 +965,11 @@ got_param: } goto harderr; default: - print_error(unit, i); + print_error(sc, i); goto readerr; } case 0x00: - i = inb(port+IREG_RESULT); + i = SCD_READ(sc, IREG_RESULT); break; } @@ -1024,13 +983,13 @@ got_param: biodone(bp); cd->flags &= ~SCDMBXBSY; - scd_start(mbx->unit); + scd_start(sc); return; } readerr: if (mbx->retry-- > 0) { - printf("scd%d: retrying ...\n",unit); + device_printf(sc->dev, "retrying ...\n"); state = SCD_S_BEGIN1; goto loop; } @@ -1042,11 +1001,11 @@ harderr: biodone(bp); cd->flags &= ~SCDMBXBSY; - scd_start(mbx->unit); + scd_start(sc); return; changed: - printf("scd%d: media changed\n", unit); + device_printf(sc->dev, "media changed\n"); goto harderr; } @@ -1069,20 +1028,19 @@ msf2hsg(bcd_t *msf) } static void -process_attention(unsigned unit) +process_attention(struct scd_softc *sc) { - unsigned port = scd_data[unit].iobase; unsigned char code; int count = 0; - while (IS_ATTENTION(port) && count++ < 30) { - write_control(port, CBIT_ATTENTION_CLEAR); - code = inb(port+IREG_RESULT); + while (IS_ATTENTION(sc) && count++ < 30) { + SCD_WRITE(sc, OREG_CONTROL, CBIT_ATTENTION_CLEAR); + code = SCD_READ(sc, IREG_RESULT); #if SCD_DEBUG if (scd_debuglevel > 0) { if (count == 1) - printf("scd%d: DEBUG: ATTENTIONS = 0x%x", unit, code); + device_printf(sc->dev, "DEBUG: ATTENTIONS = 0x%x", code); else printf(",0x%x", code); } @@ -1090,24 +1048,24 @@ process_attention(unsigned unit) switch (code) { case ATTEN_SPIN_DOWN: - scd_data[unit].flags &= ~SCDSPINNING; + sc->data.flags &= ~SCDSPINNING; break; case ATTEN_SPIN_UP_DONE: - scd_data[unit].flags |= SCDSPINNING; + sc->data.flags |= SCDSPINNING; break; case ATTEN_AUDIO_DONE: - scd_data[unit].audio_status = CD_AS_PLAY_COMPLETED; + sc->data.audio_status = CD_AS_PLAY_COMPLETED; break; case ATTEN_DRIVE_LOADED: - scd_data[unit].flags &= ~(SCDTOC|SCDSPINNING|SCDVALID); - scd_data[unit].audio_status = CD_AS_AUDIO_INVALID; + sc->data.flags &= ~(SCDTOC|SCDSPINNING|SCDVALID); + sc->data.audio_status = CD_AS_AUDIO_INVALID; break; case ATTEN_EJECT_PUSHED: - scd_data[unit].flags &= ~SCDVALID; + sc->data.flags &= ~SCDVALID; break; } DELAY(100); @@ -1120,7 +1078,7 @@ process_attention(unsigned unit) /* Returns 0 OR sony error code */ static int -spin_up(unsigned unit) +spin_up(struct scd_softc *sc) { unsigned char res_reg[12]; unsigned int res_size; @@ -1128,14 +1086,14 @@ spin_up(unsigned unit) int loop_count = 0; again: - rc = send_cmd(unit, CMD_SPIN_UP, 0, 0, res_reg, &res_size); + rc = send_cmd(sc, CMD_SPIN_UP, 0, 0, res_reg, &res_size); if (rc != 0) { - XDEBUG(2, ("scd%d: CMD_SPIN_UP error 0x%x\n", unit, rc)); + XDEBUG(sc, 2, "CMD_SPIN_UP error 0x%x\n", rc); return rc; } - if (!(scd_data[unit].flags & SCDTOC)) { - rc = send_cmd(unit, CMD_READ_TOC, 0); + if (!(sc->data.flags & SCDTOC)) { + rc = send_cmd(sc, CMD_READ_TOC, 0); if (rc == ERR_NOT_SPINNING) { if (loop_count++ < 3) goto again; @@ -1145,7 +1103,7 @@ again: return rc; } - scd_data[unit].flags |= SCDSPINNING; + sc->data.flags |= SCDSPINNING; return 0; } @@ -1177,7 +1135,7 @@ get_tl(struct sony_toc *toc, int size) } static int -read_toc(unsigned unit) +read_toc(struct scd_softc *sc) { struct scd_data *cd; unsigned part = 0; /* For now ... */ @@ -1186,19 +1144,19 @@ read_toc(unsigned unit) int rc, i, j; u_long first, last; - cd = scd_data + unit; + cd = &sc->data; - rc = send_cmd(unit, CMD_GET_TOC, 1, part+1); + rc = send_cmd(sc, CMD_GET_TOC, 1, part+1); if (rc < 0) return rc; if (rc > sizeof(toc)) { - printf("scd%d: program error: toc too large (%d)\n", unit, rc); + device_printf(sc->dev, "program error: toc too large (%d)\n", rc); return EIO; } - if (get_result(unit, rc, (u_char *)&toc) != 0) + if (get_result(sc, rc, (u_char *)&toc) != 0) return EIO; - XDEBUG(1, ("scd%d: toc read. len = %d, sizeof(toc) = %d\n", unit, rc, sizeof(toc))); + XDEBUG(sc, 1, "toc read. len = %d, sizeof(toc) = %d\n", rc, sizeof(toc)); tl = get_tl(&toc, rc); first = msf2hsg(tl->start_msf); @@ -1206,8 +1164,7 @@ read_toc(unsigned unit) cd->blksize = SCDBLKSIZE; cd->disksize = last*cd->blksize/DEV_BSIZE; - XDEBUG(1, ("scd%d: firstsector = %ld, lastsector = %ld", unit, - first, last)); + XDEBUG(sc, 1, "firstsector = %ld, lastsector = %ld", first, last); cd->first_track = bcd2bin(toc.first_track); cd->last_track = bcd2bin(toc.last_track); @@ -1219,8 +1176,10 @@ read_toc(unsigned unit) bcopy(tl[j].start_msf, cd->toc[i].start_msf, 3); #ifdef SCD_DEBUG if (scd_debuglevel > 0) { - if ((j % 3) == 0) - printf("\nscd%d: tracks ", unit); + if ((j % 3) == 0) { + printf("\n"); + device_printf(sc->dev, "tracks "); + } printf("[%03d: %2d %2d %2d] ", i, bcd2bin(cd->toc[i].start_msf[0]), bcd2bin(cd->toc[i].start_msf[1]), @@ -1270,140 +1229,132 @@ read_toc(unsigned unit) } static void -init_drive(unsigned unit) +init_drive(struct scd_softc *sc) { int rc; - rc = send_cmd(unit, CMD_SET_DRIVE_PARAM, 2, - 0x05, 0x03 | ((scd_data[unit].double_speed) ? 0x04: 0)); + rc = send_cmd(sc, CMD_SET_DRIVE_PARAM, 2, + 0x05, 0x03 | ((sc->data.double_speed) ? 0x04: 0)); if (rc != 0) - printf("scd%d: Unable to set parameters. Errcode = 0x%x\n", unit, rc); + device_printf(sc->dev, "Unable to set parameters. Errcode = 0x%x\n", rc); } /* Returns 0 or errno */ static int -get_result(u_int unit, int result_len, u_char *result) +get_result(struct scd_softc *sc, int result_len, u_char *result) { - unsigned int port = scd_data[unit].iobase; - unsigned int res_reg = port + IREG_RESULT; int loop_index = 2; /* send_cmd() reads two bytes ... */ - XDEBUG(1, ("scd%d: DEBUG: get_result: bytes=%d\n", unit, result_len)); + XDEBUG(sc, 1, "DEBUG: get_result: bytes=%d\n", result_len); while (result_len-- > 0) { if (loop_index++ >= 10) { loop_index = 1; - if (waitfor_status_bits(unit, SBIT_RESULT_READY, 0)) + if (waitfor_status_bits(sc, SBIT_RESULT_READY, 0)) return EIO; - write_control(port, CBIT_RESULT_READY_CLEAR); + SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR); } if (result) - *result++ = inb(res_reg); + *result++ = SCD_READ(sc, IREG_RESULT); else - (void)inb(res_reg); + (void)SCD_READ(sc, IREG_RESULT); } return 0; } /* Returns -0x100 for timeout, -(drive error code) OR number of result bytes */ static int -send_cmd(u_int unit, u_char cmd, u_int nargs, ...) +send_cmd(struct scd_softc *sc, u_char cmd, u_int nargs, ...) { va_list ap; - u_int port = scd_data[unit].iobase; - u_int reg; u_char c; int rc; int i; - if (waitfor_status_bits(unit, 0, SBIT_BUSY)) { - printf("scd%d: drive busy\n", unit); + if (waitfor_status_bits(sc, 0, SBIT_BUSY)) { + device_printf(sc->dev, "drive busy\n"); return -0x100; } - XDEBUG(1,("scd%d: DEBUG: send_cmd: cmd=0x%x nargs=%d", unit, cmd, nargs)); + XDEBUG(sc, 1, "DEBUG: send_cmd: cmd=0x%x nargs=%d", cmd, nargs); - write_control(port, CBIT_RESULT_READY_CLEAR); - write_control(port, CBIT_RPARAM_CLEAR); + SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR); + SCD_WRITE(sc, OREG_CONTROL, CBIT_RPARAM_CLEAR); for (i = 0; i < 100; i++) - if (FSTATUS_BIT(port, FBIT_WPARAM_READY)) + if (FSTATUS_BIT(sc, FBIT_WPARAM_READY)) break; - if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) { - XDEBUG(1, ("\nscd%d: wparam timeout\n", unit)); + if (!FSTATUS_BIT(sc, FBIT_WPARAM_READY)) { + XDEBUG(sc, 1, "\nwparam timeout\n"); return -EIO; } va_start(ap, nargs); - reg = port + OREG_WPARAMS; for (i = 0; i < nargs; i++) { c = (u_char)va_arg(ap, int); - outb(reg, c); - XDEBUG(1, (",{0x%x}", c)); + SCD_WRITE(sc, OREG_WPARAMS, c); + XDEBUG(sc, 1, ",{0x%x}", c); } va_end(ap); - XDEBUG(1, ("\n")); + XDEBUG(sc, 1, "\n"); - outb(port+OREG_COMMAND, cmd); + SCD_WRITE(sc, OREG_COMMAND, cmd); - rc = waitfor_status_bits(unit, SBIT_RESULT_READY, SBIT_BUSY); + rc = waitfor_status_bits(sc, SBIT_RESULT_READY, SBIT_BUSY); if (rc) return -0x100; - reg = port + IREG_RESULT; - write_control(port, CBIT_RESULT_READY_CLEAR); - switch ((rc = inb(reg)) & 0xf0) { + SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR); + switch ((rc = SCD_READ(sc, IREG_RESULT)) & 0xf0) { case 0x20: - rc = inb(reg); + rc = SCD_READ(sc, IREG_RESULT); /* FALLTHROUGH */ case 0x50: - XDEBUG(1, ("scd%d: DEBUG: send_cmd: drive_error=0x%x\n", unit, rc)); + XDEBUG(sc, 1, "DEBUG: send_cmd: drive_error=0x%x\n", rc); return -rc; case 0x00: default: - rc = inb(reg); - XDEBUG(1, ("scd%d: DEBUG: send_cmd: result_len=%d\n", unit, rc)); + rc = SCD_READ(sc, IREG_RESULT); + XDEBUG(sc, 1, "DEBUG: send_cmd: result_len=%d\n", rc); return rc; } } static void -print_error(int unit, int errcode) +print_error(struct scd_softc *sc, int errcode) { switch (errcode) { case -ERR_CD_NOT_LOADED: - printf("scd%d: door is open\n", unit); + device_printf(sc->dev, "door is open\n"); break; case -ERR_NO_CD_INSIDE: - printf("scd%d: no cd inside\n", unit); + device_printf(sc->dev, "no cd inside\n"); break; default: if (errcode == -0x100 || errcode > 0) - printf("scd%d: device timeout\n", unit); + device_printf(sc->dev, "device timeout\n"); else - printf("scd%d: unexpected error 0x%x\n", unit, -errcode); + device_printf(sc->dev, "unexpected error 0x%x\n", -errcode); break; } } /* Returns 0 or errno value */ static int -waitfor_status_bits(int unit, int bits_set, int bits_clear) +waitfor_status_bits(struct scd_softc *sc, int bits_set, int bits_clear) { - u_int port = scd_data[unit].iobase; - u_int flags = scd_data[unit].flags; - u_int reg = port + IREG_STATUS; + u_int flags = sc->data.flags; u_int max_loop; u_char c = 0; if (flags & SCDPROBING) { max_loop = 0; while (max_loop++ < 1000) { - c = inb(reg); + c = SCD_READ(sc, IREG_STATUS); if (c == 0xff) return EIO; if (c & SBIT_ATTENTION) { - process_attention(unit); + process_attention(sc); continue; } if ((c & bits_set) == bits_set && @@ -1416,9 +1367,9 @@ waitfor_status_bits(int unit, int bits_set, int bits_clear) } else { max_loop = 100; while (max_loop-- > 0) { - c = inb(reg); + c = SCD_READ(sc, IREG_STATUS); if (c & SBIT_ATTENTION) { - process_attention(unit); + process_attention(sc); continue; } if ((c & bits_set) == bits_set && @@ -1436,22 +1387,22 @@ waitfor_status_bits(int unit, int bits_set, int bits_clear) } #ifdef SCD_DEBUG if (scd_debuglevel > 0) - printf("scd%d: DEBUG: waitfor: TIMEOUT (0x%x,(0x%x,0x%x))\n", unit, c, bits_set, bits_clear); + device_printf(sc->dev, "DEBUG: waitfor: TIMEOUT (0x%x,(0x%x,0x%x))\n", c, bits_set, bits_clear); else #endif - printf("scd%d: timeout.\n", unit); + device_printf(sc->dev, "timeout.\n"); return EIO; } /* these two routines for xcdplayer - "borrowed" from mcd.c */ static int -scd_toc_header (int unit, struct ioc_toc_header* th) +scd_toc_header (struct scd_softc *sc, struct ioc_toc_header* th) { - struct scd_data *cd = scd_data + unit; + struct scd_data *cd = &sc->data; int rc; - if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) { - print_error(unit, rc); + if (!(cd->flags & SCDTOC) && (rc = read_toc(sc)) != 0) { + print_error(sc, rc); return EIO; } @@ -1463,14 +1414,14 @@ scd_toc_header (int unit, struct ioc_toc_header* th) } static int -scd_toc_entrys (int unit, struct ioc_read_toc_entry *te) +scd_toc_entrys (struct scd_softc *sc, struct ioc_read_toc_entry *te) { - struct scd_data *cd = scd_data + unit; + struct scd_data *cd = &sc->data; struct cd_toc_entry toc_entry; int rc, i, len = te->data_len; - if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) { - print_error(unit, rc); + if (!(cd->flags & SCDTOC) && (rc = read_toc(sc)) != 0) { + print_error(sc, rc); return EIO; } @@ -1508,14 +1459,14 @@ scd_toc_entrys (int unit, struct ioc_read_toc_entry *te) static int -scd_toc_entry (int unit, struct ioc_read_toc_single_entry *te) +scd_toc_entry (struct scd_softc *sc, struct ioc_read_toc_single_entry *te) { - struct scd_data *cd = scd_data + unit; + struct scd_data *cd = &sc->data; struct cd_toc_entry toc_entry; int rc, i; - if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) { - print_error(unit, rc); + if (!(cd->flags & SCDTOC) && (rc = read_toc(sc)) != 0) { + print_error(sc, rc); return EIO; } diff --git a/sys/dev/scd/scd_isa.c b/sys/dev/scd/scd_isa.c new file mode 100644 index 0000000..3df26f2 --- /dev/null +++ b/sys/dev/scd/scd_isa.c @@ -0,0 +1,183 @@ +/* + * $FreeBSD$ + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/module.h> +#include <sys/conf.h> +#include <sys/fcntl.h> +#include <sys/bio.h> +#include <sys/cdio.h> +#include <sys/disklabel.h> +#include <sys/bus.h> + +#include <sys/mutex.h> + +#include <machine/bus_pio.h> +#include <machine/bus.h> +#include <machine/resource.h> +#include <sys/rman.h> + +#include <isa/isavar.h> + +#include <dev/scd/scdreg.h> +#include <dev/scd/scdvar.h> + +static int scd_isa_probe (device_t); +static int scd_isa_attach (device_t); +static int scd_isa_detach (device_t); + +static int scd_alloc_resources (device_t); +static void scd_release_resources (device_t); + +static int +scd_isa_probe (device_t dev) +{ + struct scd_softc * sc; + int error; + + /* No pnp support */ + if (isa_get_vendorid(dev)) + return (ENXIO); + + /* IO port must be configured. */ + if (bus_get_resource_start(dev, SYS_RES_IOPORT, 0) == 0) + return (ENXIO); + + sc = device_get_softc(dev); + sc->dev = dev; + sc->port_rid = 0; + sc->port_type = SYS_RES_IOPORT; + error = scd_alloc_resources(dev); + if (error) + goto fail; + + error = scd_probe(sc); + if (error) { + device_printf(dev, "Probe failed.\n"); + goto fail; + } + + device_set_desc(dev, sc->data.name); + +fail: + scd_release_resources(dev); + return (error); +} + +static int +scd_isa_attach (device_t dev) +{ + struct scd_softc * sc; + int error; + + sc = device_get_softc(dev); + error = 0; + + sc->dev = dev; + sc->port_rid = 0; + sc->port_type = SYS_RES_IOPORT; + error = scd_alloc_resources(dev); + if (error) + goto fail; + + error = scd_probe(sc); + if (error) { + device_printf(dev, "Re-Probe failed.\n"); + goto fail; + } + + error = scd_attach(sc); + if (error) { + device_printf(dev, "Attach failed.\n"); + goto fail; + } + + return (0); +fail: + scd_release_resources(dev); + return (error); +} + +static int +scd_isa_detach (device_t dev) +{ + struct scd_softc * sc; + int error; + + sc = device_get_softc(dev); + error = 0; + + destroy_dev(sc->scd_dev_t); + + scd_release_resources(dev); + + return (error); +} + +static int +scd_alloc_resources (device_t dev) +{ + struct scd_softc * sc; + int error; + + sc = device_get_softc(dev); + error = 0; + + if (sc->port_type) { + sc->port = bus_alloc_resource(dev, sc->port_type, &sc->port_rid, + 0, ~0, 1, RF_ACTIVE); + if (sc->port == NULL) { + device_printf(dev, "Unable to allocate PORT resource.\n"); + error = ENOMEM; + goto bad; + } + sc->port_bst = rman_get_bustag(sc->port); + sc->port_bsh = rman_get_bushandle(sc->port); + } + + mtx_init(&sc->mtx, device_get_nameunit(dev), + "Interrupt lock", MTX_DEF | MTX_RECURSE); + +bad: + return (error); +} + +void +scd_release_resources (device_t dev) +{ + struct scd_softc * sc; + + sc = device_get_softc(dev); + + if (sc->port) { + bus_release_resource(dev, sc->port_type, sc->port_rid, sc->port); + sc->port_bst = 0; + sc->port_bsh = 0; + } + + if (mtx_initialized(&sc->mtx) != 0) + mtx_destroy(&sc->mtx); + + return; +} + +static device_method_t scd_isa_methods[] = { + DEVMETHOD(device_probe, scd_isa_probe), + DEVMETHOD(device_attach, scd_isa_attach), + DEVMETHOD(device_detach, scd_isa_detach), + + { 0, 0 } +}; + +static driver_t scd_isa_driver = { + "scd", + scd_isa_methods, + sizeof(struct scd_softc) +}; + +static devclass_t scd_devclass; + +DRIVER_MODULE(scd, isa, scd_isa_driver, scd_devclass, NULL, 0); diff --git a/sys/dev/scd/scdreg.h b/sys/dev/scd/scdreg.h index e9ce3b1..bbfeefd 100644 --- a/sys/dev/scd/scdreg.h +++ b/sys/dev/scd/scdreg.h @@ -43,12 +43,6 @@ typedef unsigned char bcd_t; #define S_msf(msf) msf[1] #define F_msf(msf) msf[2] -#define IS_ATTENTION(port) ((inb(port+IREG_STATUS) & SBIT_ATTENTION) != 0) -#define IS_BUSY(port) ((inb(port+IREG_STATUS) & SBIT_BUSY) != 0) -#define IS_DATA_RDY(port) ((inb(port+IREG_STATUS) & SBIT_DATA_READY) != 0) -#define STATUS_BIT(port, bit) ((inb(port+IREG_STATUS) & (bit)) != 0) -#define FSTATUS_BIT(port, bit) ((inb(port+IREG_FSTATUS) & (bit)) != 0) - #define OREG_COMMAND 0 #define OREG_WPARAMS 1 #define OREG_CONTROL 3 diff --git a/sys/dev/scd/scdvar.h b/sys/dev/scd/scdvar.h new file mode 100644 index 0000000..d7faa88 --- /dev/null +++ b/sys/dev/scd/scdvar.h @@ -0,0 +1,68 @@ +/* + * $FreeBSD$ + */ + +struct scd_mbx { + short retry; + short nblk; + int sz; + u_long skip; + struct bio * bp; + int p_offset; + short count; +}; + +struct scd_data { + char double_speed; + char * name; + short flags; + int blksize; + u_long disksize; + struct disklabel dlabel; + int openflag; + struct { + unsigned int adr :4; + unsigned int ctl :4; /* xcdplayer needs this */ + unsigned char start_msf[3]; + } toc[MAX_TRACKS]; + short first_track; + short last_track; + struct ioc_play_msf last_play; + + short audio_status; + struct bio_queue_head head; /* head of bio queue */ + struct scd_mbx mbx; +}; + +struct scd_softc { + device_t dev; + dev_t scd_dev_t; + int debug; + + struct resource * port; + int port_rid; + int port_type; + bus_space_tag_t port_bst; + bus_space_handle_t port_bsh; + + struct mtx mtx; + + struct callout_handle ch; + int ch_state; + struct scd_mbx * ch_mbxsave; + + struct scd_data data; +}; + +#define SCD_LOCK(_sc) splx(&(_sc)->mtx +#define SCD_UNLOCK(_sc) splx(&(_sc)->mtx + +#define SCD_READ(_sc, _reg) \ + bus_space_read_1(_sc->port_bst, _sc->port_bsh, _reg) +#define SCD_READ_MULTI(_sc, _reg, _addr, _count) \ + bus_space_read_multi_1(_sc->port_bst, _sc->port_bsh, _reg, _addr, _count) +#define SCD_WRITE(_sc, _reg, _val) \ + bus_space_write_1(_sc->port_bst, _sc->port_bsh, _reg, _val) + +int scd_probe (struct scd_softc *); +int scd_attach (struct scd_softc *); diff --git a/sys/i386/conf/NOTES b/sys/i386/conf/NOTES index bff66f5..55ac2bf 100644 --- a/sys/i386/conf/NOTES +++ b/sys/i386/conf/NOTES @@ -512,7 +512,6 @@ device aacp # SCSI Passthrough interface (optional, CAM required) # # Miscellaneous hardware: # -# scd: Sony CD-ROM using proprietary (non-ATAPI) interface # wt: Wangtek and Archive QIC-02/QIC-36 tape drives # ctx: Cortex-I frame grabber # apm: Laptop Advanced Power Management (experimental) @@ -583,10 +582,6 @@ device aacp # SCSI Passthrough interface (optional, CAM required) # The NDGBPORTS option specifies the number of ports controlled by the # dgb(4) driver. The default value is 16 ports per device. -# for the Sony CDU31/33A CDROM -device scd 1 -hint.scd.0.at="isa" -hint.scd.0.port="0x230" device wt 1 hint.wt.0.at="isa" hint.wt.0.port="0x300" diff --git a/sys/i386/isa/scd.c b/sys/i386/isa/scd.c deleted file mode 100644 index 5f859e1..0000000 --- a/sys/i386/isa/scd.c +++ /dev/null @@ -1,1549 +0,0 @@ -#include "opt_geom.h" -#ifndef NO_GEOM -#warning "The scd driver is currently incompatible with GEOM" -#else -/*- - * Copyright (c) 1995 Mikael Hybsch - * All rights reserved. - * - * Portions of this file are copied from mcd.c - * which has the following copyrights: - * - * Copyright 1993 by Holger Veit (data part) - * Copyright 1993 by Brian Moore (audio part) - * Changes Copyright 1993 by Gary Clark II - * Changes Copyright (C) 1994 by Andrew A. Chernov - * - * Rewrote probe routine to work on newer Mitsumi drives. - * Additional changes (C) 1994 by Jordan K. Hubbard - * - * 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 - * in this position and unchanged. - * 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. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. - * - */ - - -/* $FreeBSD$ */ - -/* Please send any comments to micke@dynas.se */ - -#define SCD_DEBUG 0 - -#include "scd.h" -#include <sys/param.h> -#include <sys/systm.h> -#include <sys/kernel.h> -#include <sys/conf.h> -#include <sys/bio.h> -#include <sys/cdio.h> -#include <sys/disklabel.h> -#include <sys/bus.h> - -#include <machine/stdarg.h> - -#include <i386/isa/isa_device.h> -#include <i386/isa/scdreg.h> - - -#define scd_part(dev) ((minor(dev)) & 7) -#define scd_unit(dev) (((minor(dev)) & 0x38) >> 3) -#define scd_phys(dev) (((minor(dev)) & 0x40) >> 6) -#define RAW_PART 2 - -/* flags */ -#define SCDOPEN 0x0001 /* device opened */ -#define SCDVALID 0x0002 /* parameters loaded */ -#define SCDINIT 0x0004 /* device is init'd */ -#define SCDPROBING 0x0020 /* probing */ -#define SCDTOC 0x0100 /* already read toc */ -#define SCDMBXBSY 0x0200 /* local mbx is busy */ -#define SCDSPINNING 0x0400 /* drive is spun up */ - -#define SCD_S_BEGIN 0 -#define SCD_S_BEGIN1 1 -#define SCD_S_WAITSTAT 2 -#define SCD_S_WAITFIFO 3 -#define SCD_S_WAITSPIN 4 -#define SCD_S_WAITREAD 5 -#define SCD_S_WAITPARAM 6 - -#define RDELAY_WAIT 300 -#define RDELAY_WAITREAD 300 - -#define SCDBLKSIZE 2048 - -#ifdef SCD_DEBUG - static int scd_debuglevel = SCD_DEBUG; -# define XDEBUG(level, data) {if (scd_debuglevel >= level) printf data;} -#else -# define XDEBUG(level, data) -#endif - -struct scd_mbx { - short unit; - short port; - short retry; - short nblk; - int sz; - u_long skip; - struct bio *bp; - int p_offset; - short count; -}; - -static struct scd_data { - int iobase; - char double_speed; - char *name; - short flags; - int blksize; - u_long disksize; - struct disklabel dlabel; - int openflag; - struct { - unsigned int adr :4; - unsigned int ctl :4; /* xcdplayer needs this */ - unsigned char start_msf[3]; - } toc[MAX_TRACKS]; - short first_track; - short last_track; - struct ioc_play_msf last_play; - - short audio_status; - struct bio_queue_head head; /* head of bio queue */ - struct scd_mbx mbx; -} scd_data[NSCD]; - -/* prototypes */ -static void hsg2msf(int hsg, bcd_t *msf); -static int msf2hsg(bcd_t *msf); - -static void process_attention(unsigned unit); -static __inline void write_control(unsigned port, unsigned data); -static int waitfor_status_bits(int unit, int bits_set, int bits_clear); -static int send_cmd(u_int unit, u_char cmd, u_int nargs, ...); -static void init_drive(unsigned unit); -static int spin_up(unsigned unit); -static int read_toc(unsigned unit); -static int get_result(u_int unit, int result_len, u_char *result); -static void print_error(int unit, int errcode); - -static void scd_start(int unit); -static timeout_t scd_timeout; -static void scd_doread(int state, struct scd_mbx *mbxin); - -static int scd_eject(int unit); -static int scd_stop(int unit); -static int scd_pause(int unit); -static int scd_resume(int unit); -static int scd_playtracks(int unit, struct ioc_play_track *pt); -static int scd_playmsf(int unit, struct ioc_play_msf *msf); -static int scd_play(int unit, struct ioc_play_msf *msf); -static int scd_subchan(int unit, struct ioc_read_subchannel *sc); -static int read_subcode(int unit, struct sony_subchannel_position_data *sc); - -/* for xcdplayer */ -static int scd_toc_header(int unit, struct ioc_toc_header *th); -static int scd_toc_entrys(int unit, struct ioc_read_toc_entry *te); -static int scd_toc_entry(int unit, struct ioc_read_toc_single_entry *te); -#define SCD_LASTPLUS1 170 /* don't ask, xcdplayer passes this in */ - -static int scd_probe(struct isa_device *dev); -static int scd_attach(struct isa_device *dev); -struct isa_driver scddriver = { - INTR_TYPE_BIO, - scd_probe, - scd_attach, - "scd" -}; -COMPAT_ISA_DRIVER(scd, scddriver); - -/* For canceling our timeout */ -static struct callout_handle tohandle = CALLOUT_HANDLE_INITIALIZER(&tohanle); - -static d_open_t scdopen; -static d_close_t scdclose; -static d_ioctl_t scdioctl; -static d_strategy_t scdstrategy; - -#define CDEV_MAJOR 45 - -static struct cdevsw scd_cdevsw = { - /* open */ scdopen, - /* close */ scdclose, - /* read */ physread, - /* write */ nowrite, - /* ioctl */ scdioctl, - /* poll */ nopoll, - /* mmap */ nommap, - /* strategy */ scdstrategy, - /* name */ "scd", - /* maj */ CDEV_MAJOR, - /* dump */ nodump, - /* psize */ nopsize, - /* flags */ D_DISK, -}; - - -static int -scd_attach(struct isa_device *dev) -{ - int unit = dev->id_unit; - struct scd_data *cd = scd_data + unit; - - cd->iobase = dev->id_iobase; /* Already set by probe, but ... */ - - /* name filled in probe */ - printf("scd%d: <%s>\n", dev->id_unit, scd_data[dev->id_unit].name); - - init_drive(dev->id_unit); - - cd->flags = SCDINIT; - cd->audio_status = CD_AS_AUDIO_INVALID; - bioq_init(&cd->head); - - make_dev(&scd_cdevsw, dkmakeminor(unit, 0, 0), - UID_ROOT, GID_OPERATOR, 0640, "scd%da", unit); - make_dev(&scd_cdevsw, dkmakeminor(unit, 0, RAW_PART), - UID_ROOT, GID_OPERATOR, 0640, "scd%dc", unit); - return 1; -} - -static int -scdopen(dev_t dev, int flags, int fmt, struct thread *td) -{ - int unit,part,phys; - int rc; - struct scd_data *cd; - - unit = scd_unit(dev); - if (unit >= NSCD) - return ENXIO; - - cd = scd_data + unit; - part = scd_part(dev); - phys = scd_phys(dev); - - /* not initialized*/ - if (!(cd->flags & SCDINIT)) - return ENXIO; - - /* invalidated in the meantime? mark all open part's invalid */ - if (cd->openflag) - return ENXIO; - - XDEBUG(1,("scd%d: DEBUG: status = 0x%x\n", unit, inb(cd->iobase+IREG_STATUS))); - - if ((rc = spin_up(unit)) != 0) { - print_error(unit, rc); - return EIO; - } - if (!(cd->flags & SCDTOC)) { - int loop_count = 3; - - while (loop_count-- > 0 && (rc = read_toc(unit)) != 0) { - if (rc == ERR_NOT_SPINNING) { - rc = spin_up(unit); - if (rc) { - print_error(unit, rc);\ - return EIO; - } - continue; - } - printf("scd%d: TOC read error 0x%x\n", unit, rc); - return EIO; - } - } - - dev->si_bsize_phys = cd->blksize; - - cd->openflag = 1; - cd->flags |= SCDVALID; - - return 0; -} - -static int -scdclose(dev_t dev, int flags, int fmt, struct thread *td) -{ - int unit,part,phys; - struct scd_data *cd; - - unit = scd_unit(dev); - if (unit >= NSCD) - return ENXIO; - - cd = scd_data + unit; - part = scd_part(dev); - phys = scd_phys(dev); - - if (!(cd->flags & SCDINIT) || !cd->openflag) - return ENXIO; - - if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS) { - (void)send_cmd(unit, CMD_SPIN_DOWN, 0); - cd->flags &= ~SCDSPINNING; - } - - - /* close channel */ - cd->openflag = 0; - - return 0; -} - -static void -scdstrategy(struct bio *bp) -{ - struct scd_data *cd; - int s; - int unit = scd_unit(bp->bio_dev); - - cd = scd_data + unit; - - XDEBUG(2, ("scd%d: DEBUG: strategy: block=%ld, bcount=%ld\n", - unit, (long)bp->bio_blkno, bp->bio_bcount)); - - if (unit >= NSCD || bp->bio_blkno < 0 || (bp->bio_bcount % SCDBLKSIZE)) { - printf("scd%d: strategy failure: blkno = %ld, bcount = %ld\n", - unit, (long)bp->bio_blkno, bp->bio_bcount); - bp->bio_error = EINVAL; - bp->bio_flags |= BIO_ERROR; - goto bad; - } - - /* if device invalidated (e.g. media change, door open), error */ - if (!(cd->flags & SCDVALID)) { - printf("scd%d: media changed\n", unit); - bp->bio_error = EIO; - goto bad; - } - - /* read only */ - if (!(bp->bio_cmd == BIO_READ)) { - bp->bio_error = EROFS; - goto bad; - } - - /* no data to read */ - if (bp->bio_bcount == 0) - goto done; - - if (!(cd->flags & SCDTOC)) { - bp->bio_error = EIO; - goto bad; - } - /* adjust transfer if necessary */ - if (bounds_check_with_label(bp,&cd->dlabel,1) <= 0) - goto done; - - bp->bio_pblkno = bp->bio_blkno; - bp->bio_resid = 0; - - /* queue it */ - s = splbio(); - bioqdisksort(&cd->head, bp); - splx(s); - - /* now check whether we can perform processing */ - scd_start(unit); - return; - -bad: - bp->bio_flags |= BIO_ERROR; -done: - bp->bio_resid = bp->bio_bcount; - biodone(bp); - return; -} - -static void -scd_start(int unit) -{ - struct scd_data *cd = scd_data + unit; - struct bio *bp; - struct partition *p; - int s = splbio(); - - if (cd->flags & SCDMBXBSY) { - splx(s); - return; - } - - bp = bioq_first(&cd->head); - if (bp != 0) { - /* block found to process, dequeue */ - bioq_remove(&cd->head, bp); - cd->flags |= SCDMBXBSY; - splx(s); - } else { - /* nothing to do */ - splx(s); - return; - } - - p = cd->dlabel.d_partitions + scd_part(bp->bio_dev); - - cd->mbx.unit = unit; - cd->mbx.port = cd->iobase; - cd->mbx.retry = 3; - cd->mbx.bp = bp; - cd->mbx.p_offset = p->p_offset; - splx(s); - - scd_doread(SCD_S_BEGIN,&(cd->mbx)); - return; -} - -static int -scdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td) -{ - struct scd_data *cd; - int unit,part; - - unit = scd_unit(dev); - part = scd_part(dev); - cd = scd_data + unit; - - XDEBUG(1, ("scd%d: ioctl: cmd=0x%lx\n", unit, cmd)); - - if (!(cd->flags & SCDVALID)) - return EIO; - - switch (cmd) { - case DIOCGDINFO: - *(struct disklabel *)addr = cd->dlabel; - return 0; - case CDIOCPLAYTRACKS: - return scd_playtracks(unit, (struct ioc_play_track *) addr); - case CDIOCPLAYBLOCKS: - return EINVAL; - case CDIOCPLAYMSF: - return scd_playmsf(unit, (struct ioc_play_msf *) addr); - case CDIOCREADSUBCHANNEL: - return scd_subchan(unit, (struct ioc_read_subchannel *) addr); - case CDIOREADTOCHEADER: - return scd_toc_header (unit, (struct ioc_toc_header *) addr); - case CDIOREADTOCENTRYS: - return scd_toc_entrys (unit, (struct ioc_read_toc_entry*) addr); - case CDIOREADTOCENTRY: - return scd_toc_entry (unit, (struct ioc_read_toc_single_entry*) addr); - case CDIOCSETPATCH: - case CDIOCGETVOL: - case CDIOCSETVOL: - case CDIOCSETMONO: - case CDIOCSETSTERIO: - case CDIOCSETMUTE: - case CDIOCSETLEFT: - case CDIOCSETRIGHT: - return EINVAL; - case CDIOCRESUME: - return scd_resume(unit); - case CDIOCPAUSE: - return scd_pause(unit); - case CDIOCSTART: - return EINVAL; - case CDIOCSTOP: - return scd_stop(unit); - case CDIOCEJECT: - return scd_eject(unit); - case CDIOCALLOW: - return 0; - case CDIOCSETDEBUG: -#ifdef SCD_DEBUG - scd_debuglevel++; -#endif - return 0; - case CDIOCCLRDEBUG: -#ifdef SCD_DEBUG - scd_debuglevel = 0; - -#endif - return 0; - default: - printf("scd%d: unsupported ioctl (cmd=0x%lx)\n", unit, cmd); - return ENOTTY; - } -} - -/*************************************************************** - * lower level of driver starts here - **************************************************************/ - -static int -scd_playtracks(int unit, struct ioc_play_track *pt) -{ - struct scd_data *cd = scd_data + unit; - struct ioc_play_msf msf; - int a = pt->start_track; - int z = pt->end_track; - int rc; - - if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) { - if (rc == -ERR_NOT_SPINNING) { - if (spin_up(unit) != 0) - return EIO; - rc = read_toc(unit); - } - if (rc != 0) { - print_error(unit, rc); - return EIO; - } - } - - XDEBUG(1, ("scd%d: playtracks from %d:%d to %d:%d\n", unit, - a, pt->start_index, z, pt->end_index)); - - if ( a < cd->first_track - || a > cd->last_track - || a > z - || z > cd->last_track) - return EINVAL; - - bcopy(cd->toc[a].start_msf, &msf.start_m, 3); - hsg2msf(msf2hsg(cd->toc[z+1].start_msf)-1, &msf.end_m); - - return scd_play(unit, &msf); -} - -/* The start/end msf is expected to be in bin format */ -static int -scd_playmsf(int unit, struct ioc_play_msf *msfin) -{ - struct ioc_play_msf msf; - - msf.start_m = bin2bcd(msfin->start_m); - msf.start_s = bin2bcd(msfin->start_s); - msf.start_f = bin2bcd(msfin->start_f); - msf.end_m = bin2bcd(msfin->end_m); - msf.end_s = bin2bcd(msfin->end_s); - msf.end_f = bin2bcd(msfin->end_f); - - return scd_play(unit, &msf); -} - -/* The start/end msf is expected to be in bcd format */ -static int -scd_play(int unit, struct ioc_play_msf *msf) -{ - struct scd_data *cd = scd_data + unit; - int i, rc; - - XDEBUG(1, ("scd%d: playing: %02x:%02x:%02x -> %02x:%02x:%02x\n", unit, - msf->start_m, msf->start_s, msf->start_f, - msf->end_m, msf->end_s, msf->end_f)); - - for (i = 0; i < 2; i++) { - rc = send_cmd(unit, CMD_PLAY_AUDIO, 7, - 0x03, - msf->start_m, msf->start_s, msf->start_f, - msf->end_m, msf->end_s, msf->end_f); - if (rc == -ERR_NOT_SPINNING) { - cd->flags &= ~SCDSPINNING; - if (spin_up(unit) != 0) - return EIO; - } else if (rc < 0) { - print_error(unit, rc); - return EIO; - } else { - break; - } - } - cd->audio_status = CD_AS_PLAY_IN_PROGRESS; - bcopy((char *)msf, (char *)&cd->last_play, sizeof(struct ioc_play_msf)); - return 0; -} - -static int -scd_stop(int unit) -{ - struct scd_data *cd = scd_data + unit; - - (void)send_cmd(unit, CMD_STOP_AUDIO, 0); - cd->audio_status = CD_AS_PLAY_COMPLETED; - return 0; -} - -static int -scd_pause(int unit) -{ - struct scd_data *cd = scd_data + unit; - struct sony_subchannel_position_data subpos; - - if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS) - return EINVAL; - - if (read_subcode(unit, &subpos) != 0) - return EIO; - - if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0) - return EIO; - - cd->last_play.start_m = subpos.abs_msf[0]; - cd->last_play.start_s = subpos.abs_msf[1]; - cd->last_play.start_f = subpos.abs_msf[2]; - cd->audio_status = CD_AS_PLAY_PAUSED; - - XDEBUG(1, ("scd%d: pause @ %02x:%02x:%02x\n", unit, - cd->last_play.start_m, - cd->last_play.start_s, - cd->last_play.start_f)); - - return 0; -} - -static int -scd_resume(int unit) -{ - if (scd_data[unit].audio_status != CD_AS_PLAY_PAUSED) - return EINVAL; - return scd_play(unit, &scd_data[unit].last_play); -} - -static int -scd_eject(int unit) -{ - struct scd_data *cd = scd_data + unit; - - cd->audio_status = CD_AS_AUDIO_INVALID; - cd->flags &= ~(SCDSPINNING|SCDTOC); - - if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0 || - send_cmd(unit, CMD_SPIN_DOWN, 0) != 0 || - send_cmd(unit, CMD_EJECT, 0) != 0) - { - return EIO; - } - return 0; -} - -static int -scd_subchan(int unit, struct ioc_read_subchannel *sc) -{ - struct scd_data *cd = scd_data + unit; - struct sony_subchannel_position_data q; - struct cd_sub_channel_info data; - - XDEBUG(1, ("scd%d: subchan af=%d, df=%d\n", unit, - sc->address_format, - sc->data_format)); - - if (sc->address_format != CD_MSF_FORMAT) - return EINVAL; - - if (sc->data_format != CD_CURRENT_POSITION) - return EINVAL; - - if (read_subcode(unit, &q) != 0) - return EIO; - - data.header.audio_status = cd->audio_status; - data.what.position.data_format = CD_MSF_FORMAT; - data.what.position.track_number = bcd2bin(q.track_number); - data.what.position.reladdr.msf.unused = 0; - data.what.position.reladdr.msf.minute = bcd2bin(q.rel_msf[0]); - data.what.position.reladdr.msf.second = bcd2bin(q.rel_msf[1]); - data.what.position.reladdr.msf.frame = bcd2bin(q.rel_msf[2]); - data.what.position.absaddr.msf.unused = 0; - data.what.position.absaddr.msf.minute = bcd2bin(q.abs_msf[0]); - data.what.position.absaddr.msf.second = bcd2bin(q.abs_msf[1]); - data.what.position.absaddr.msf.frame = bcd2bin(q.abs_msf[2]); - - if (copyout(&data, sc->data, min(sizeof(struct cd_sub_channel_info), sc->data_len))!=0) - return EFAULT; - return 0; -} - -static __inline void -write_control(unsigned port, unsigned data) -{ - outb(port + OREG_CONTROL, data); -} - -static int -scd_probe(struct isa_device *dev) -{ - struct sony_drive_configuration drive_config; - int unit = dev->id_unit; - int rc; - static char namebuf[8+16+8+3]; - char *s = namebuf; - int loop_count = 0; - - scd_data[unit].flags = SCDPROBING; - scd_data[unit].iobase = dev->id_iobase; - - bzero(&drive_config, sizeof(drive_config)); - -again: - /* Reset drive */ - write_control(dev->id_iobase, CBIT_RESET_DRIVE); - - /* Calm down */ - DELAY(300000); - - /* Only the ATTENTION bit may be set */ - if ((inb(dev->id_iobase+IREG_STATUS) & ~1) != 0) { - XDEBUG(1, ("scd: too many bits set. probe failed.\n")); - return 0; - } - rc = send_cmd(unit, CMD_GET_DRIVE_CONFIG, 0); - if (rc != sizeof(drive_config)) { - /* Sometimes if the drive is playing audio I get */ - /* the bad result 82. Fix by repeating the reset */ - if (rc > 0 && loop_count++ == 0) - goto again; - return 0; - } - if (get_result(unit, rc, (u_char *)&drive_config) != 0) - return 0; - - bcopy(drive_config.vendor, namebuf, 8); - s = namebuf+8; - while (*(s-1) == ' ') /* Strip trailing spaces */ - s--; - *s++ = ' '; - bcopy(drive_config.product, s, 16); - s += 16; - while (*(s-1) == ' ') - s--; - *s++ = ' '; - bcopy(drive_config.revision, s, 8); - s += 8; - while (*(s-1) == ' ') - s--; - *s = 0; - - scd_data[unit].name = namebuf; - - if (drive_config.config & 0x10) - scd_data[unit].double_speed = 1; - else - scd_data[unit].double_speed = 0; - - return 4; -} - -static int -read_subcode(int unit, struct sony_subchannel_position_data *sc) -{ - int rc; - - rc = send_cmd(unit, CMD_GET_SUBCHANNEL_DATA, 0); - if (rc < 0 || rc < sizeof(*sc)) - return EIO; - if (get_result(unit, rc, (u_char *)sc) != 0) - return EIO; - return 0; -} - -/* State machine copied from mcd.c */ - -/* This (and the code in mcd.c) will not work with more than one drive */ -/* because there is only one mbxsave below. Should fix that some day. */ -/* (mbxsave & state should probably be included in the scd_data struct and */ -/* the unit number used as first argument to scd_doread().) /Micke */ - -/* state machine to process read requests - * initialize with SCD_S_BEGIN: reset state machine - * SCD_S_WAITSTAT: wait for ready (!busy) - * SCD_S_WAITSPIN: wait for drive to spin up (if not spinning) - * SCD_S_WAITFIFO: wait for param fifo to get ready, them exec. command. - * SCD_S_WAITREAD: wait for data ready, read data - * SCD_S_WAITPARAM: wait for command result params, read them, error if bad data read. - */ - -static struct scd_mbx *mbxsave; - -static void -scd_timeout(void *arg) -{ - scd_doread((int)arg, mbxsave); -} - -static void -scd_doread(int state, struct scd_mbx *mbxin) -{ - struct scd_mbx *mbx = (state!=SCD_S_BEGIN) ? mbxsave : mbxin; - int unit = mbx->unit; - int port = mbx->port; - struct bio *bp = mbx->bp; - struct scd_data *cd = scd_data + unit; - int reg,i; - int blknum; - caddr_t addr; - static char sdata[3]; /* Must be preserved between calls to this function */ - -loop: - switch (state) { - case SCD_S_BEGIN: - mbx = mbxsave = mbxin; - - case SCD_S_BEGIN1: - /* get status */ - mbx->count = RDELAY_WAIT; - - process_attention(unit); - goto trystat; - - case SCD_S_WAITSTAT: - untimeout(scd_timeout,(caddr_t)SCD_S_WAITSTAT, tohandle); - if (mbx->count-- <= 0) { - printf("scd%d: timeout. drive busy.\n",unit); - goto harderr; - } - -trystat: - if (IS_BUSY(port)) { - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITSTAT,hz/100); /* XXX */ - return; - } - - process_attention(unit); - - /* reject, if audio active */ - if (cd->audio_status & CD_AS_PLAY_IN_PROGRESS) { - printf("scd%d: audio is active\n",unit); - goto harderr; - } - - mbx->sz = cd->blksize; - - /* for first block */ - mbx->nblk = (bp->bio_bcount + (mbx->sz-1)) / mbx->sz; - mbx->skip = 0; - -nextblock: - if (!(cd->flags & SCDVALID)) - goto changed; - - blknum = (bp->bio_blkno / (mbx->sz/DEV_BSIZE)) - + mbx->p_offset + mbx->skip/mbx->sz; - - XDEBUG(2, ("scd%d: scd_doread: read blknum=%d\n", unit, blknum)); - - /* build parameter block */ - hsg2msf(blknum, sdata); - - write_control(port, CBIT_RESULT_READY_CLEAR); - write_control(port, CBIT_RPARAM_CLEAR); - write_control(port, CBIT_DATA_READY_CLEAR); - - if (FSTATUS_BIT(port, FBIT_WPARAM_READY)) - goto writeparam; - - mbx->count = 100; - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITFIFO,hz/100); /* XXX */ - return; - - case SCD_S_WAITSPIN: - untimeout(scd_timeout,(caddr_t)SCD_S_WAITSPIN, tohandle); - if (mbx->count-- <= 0) { - printf("scd%d: timeout waiting for drive to spin up.\n", unit); - goto harderr; - } - if (!STATUS_BIT(port, SBIT_RESULT_READY)) { - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITSPIN,hz/100); /* XXX */ - return; - } - write_control(port, CBIT_RESULT_READY_CLEAR); - switch ((i = inb(port+IREG_RESULT)) & 0xf0) { - case 0x20: - i = inb(port+IREG_RESULT); - print_error(unit, i); - goto harderr; - case 0x00: - (void)inb(port+IREG_RESULT); - cd->flags |= SCDSPINNING; - break; - } - XDEBUG(1, ("scd%d: DEBUG: spin up complete\n", unit)); - - state = SCD_S_BEGIN1; - goto loop; - - case SCD_S_WAITFIFO: - untimeout(scd_timeout,(caddr_t)SCD_S_WAITFIFO, tohandle); - if (mbx->count-- <= 0) { - printf("scd%d: timeout. write param not ready.\n",unit); - goto harderr; - } - if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) { - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITFIFO,hz/100); /* XXX */ - return; - } - XDEBUG(1, ("scd%d: mbx->count (writeparamwait) = %d(%d)\n", unit, mbx->count, 100)); - -writeparam: - /* The reason this test isn't done 'till now is to make sure */ - /* that it is ok to send the SPIN_UP cmd below. */ - if (!(cd->flags & SCDSPINNING)) { - XDEBUG(1, ("scd%d: spinning up drive ...\n", unit)); - outb(port+OREG_COMMAND, CMD_SPIN_UP); - mbx->count = 300; - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITSPIN,hz/100); /* XXX */ - return; - } - - reg = port + OREG_WPARAMS; - /* send the read command */ - disable_intr(); - outb(reg, sdata[0]); - outb(reg, sdata[1]); - outb(reg, sdata[2]); - outb(reg, 0); - outb(reg, 0); - outb(reg, 1); - outb(port+OREG_COMMAND, CMD_READ); - enable_intr(); - - mbx->count = RDELAY_WAITREAD; - for (i = 0; i < 50; i++) { - if (STATUS_BIT(port, SBIT_DATA_READY)) - goto got_data; - DELAY(100); - } - - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITREAD,hz/100); /* XXX */ - return; - - case SCD_S_WAITREAD: - untimeout(scd_timeout,(caddr_t)SCD_S_WAITREAD, tohandle); - if (mbx->count-- <= 0) { - if (STATUS_BIT(port, SBIT_RESULT_READY)) - goto got_param; - printf("scd%d: timeout while reading data\n",unit); - goto readerr; - } - if (!STATUS_BIT(port, SBIT_DATA_READY)) { - process_attention(unit); - if (!(cd->flags & SCDVALID)) - goto changed; - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITREAD,hz/100); /* XXX */ - return; - } - XDEBUG(2, ("scd%d: mbx->count (after RDY_BIT) = %d(%d)\n", unit, mbx->count, RDELAY_WAITREAD)); - -got_data: - /* data is ready */ - addr = bp->bio_data + mbx->skip; - write_control(port, CBIT_DATA_READY_CLEAR); - insb(port+IREG_DATA, addr, mbx->sz); - - mbx->count = 100; - for (i = 0; i < 20; i++) { - if (STATUS_BIT(port, SBIT_RESULT_READY)) - goto waitfor_param; - DELAY(100); - } - goto waitfor_param; - - case SCD_S_WAITPARAM: - untimeout(scd_timeout,(caddr_t)SCD_S_WAITPARAM, tohandle); - if (mbx->count-- <= 0) { - printf("scd%d: timeout waiting for params\n",unit); - goto readerr; - } - -waitfor_param: - if (!STATUS_BIT(port, SBIT_RESULT_READY)) { - tohandle = timeout(scd_timeout, - (caddr_t)SCD_S_WAITPARAM,hz/100); /* XXX */ - return; - } -#if SCD_DEBUG - if (mbx->count < 100 && scd_debuglevel > 0) - printf("scd%d: mbx->count (paramwait) = %d(%d)\n", unit, mbx->count, 100); -#endif - -got_param: - write_control(port, CBIT_RESULT_READY_CLEAR); - switch ((i = inb(port+IREG_RESULT)) & 0xf0) { - case 0x50: - switch (i) { - case ERR_FATAL_READ_ERROR1: - case ERR_FATAL_READ_ERROR2: - printf("scd%d: unrecoverable read error 0x%x\n", unit, i); - goto harderr; - } - break; - case 0x20: - i = inb(port+IREG_RESULT); - switch (i) { - case ERR_NOT_SPINNING: - XDEBUG(1, ("scd%d: read error: drive not spinning\n", unit)); - if (mbx->retry-- > 0) { - state = SCD_S_BEGIN1; - cd->flags &= ~SCDSPINNING; - goto loop; - } - goto harderr; - default: - print_error(unit, i); - goto readerr; - } - case 0x00: - i = inb(port+IREG_RESULT); - break; - } - - if (--mbx->nblk > 0) { - mbx->skip += mbx->sz; - goto nextblock; - } - - /* return buffer */ - bp->bio_resid = 0; - biodone(bp); - - cd->flags &= ~SCDMBXBSY; - scd_start(mbx->unit); - return; - } - -readerr: - if (mbx->retry-- > 0) { - printf("scd%d: retrying ...\n",unit); - state = SCD_S_BEGIN1; - goto loop; - } -harderr: - /* invalidate the buffer */ - bp->bio_error = EIO; - bp->bio_flags |= BIO_ERROR; - bp->bio_resid = bp->bio_bcount; - biodone(bp); - - cd->flags &= ~SCDMBXBSY; - scd_start(mbx->unit); - return; - -changed: - printf("scd%d: media changed\n", unit); - goto harderr; -} - -static void -hsg2msf(int hsg, bcd_t *msf) -{ - hsg += 150; - M_msf(msf) = bin2bcd(hsg / 4500); - hsg %= 4500; - S_msf(msf) = bin2bcd(hsg / 75); - F_msf(msf) = bin2bcd(hsg % 75); -} - -static int -msf2hsg(bcd_t *msf) -{ - return (bcd2bin(M_msf(msf)) * 60 + - bcd2bin(S_msf(msf))) * 75 + - bcd2bin(F_msf(msf)) - 150; -} - -static void -process_attention(unsigned unit) -{ - unsigned port = scd_data[unit].iobase; - unsigned char code; - int count = 0; - - while (IS_ATTENTION(port) && count++ < 30) { - write_control(port, CBIT_ATTENTION_CLEAR); - code = inb(port+IREG_RESULT); - -#if SCD_DEBUG - if (scd_debuglevel > 0) { - if (count == 1) - printf("scd%d: DEBUG: ATTENTIONS = 0x%x", unit, code); - else - printf(",0x%x", code); - } -#endif - - switch (code) { - case ATTEN_SPIN_DOWN: - scd_data[unit].flags &= ~SCDSPINNING; - break; - - case ATTEN_SPIN_UP_DONE: - scd_data[unit].flags |= SCDSPINNING; - break; - - case ATTEN_AUDIO_DONE: - scd_data[unit].audio_status = CD_AS_PLAY_COMPLETED; - break; - - case ATTEN_DRIVE_LOADED: - scd_data[unit].flags &= ~(SCDTOC|SCDSPINNING|SCDVALID); - scd_data[unit].audio_status = CD_AS_AUDIO_INVALID; - break; - - case ATTEN_EJECT_PUSHED: - scd_data[unit].flags &= ~SCDVALID; - break; - } - DELAY(100); - } -#if SCD_DEBUG - if (scd_debuglevel > 0 && count > 0) - printf("\n"); -#endif -} - -/* Returns 0 OR sony error code */ -static int -spin_up(unsigned unit) -{ - unsigned char res_reg[12]; - unsigned int res_size; - int rc; - int loop_count = 0; - -again: - rc = send_cmd(unit, CMD_SPIN_UP, 0, 0, res_reg, &res_size); - if (rc != 0) { - XDEBUG(2, ("scd%d: CMD_SPIN_UP error 0x%x\n", unit, rc)); - return rc; - } - - if (!(scd_data[unit].flags & SCDTOC)) { - rc = send_cmd(unit, CMD_READ_TOC, 0); - if (rc == ERR_NOT_SPINNING) { - if (loop_count++ < 3) - goto again; - return rc; - } - if (rc != 0) - return rc; - } - - scd_data[unit].flags |= SCDSPINNING; - - return 0; -} - -static struct sony_tracklist * -get_tl(struct sony_toc *toc, int size) -{ - struct sony_tracklist *tl = &toc->tracks[0]; - - if (tl->track != 0xb0) - return tl; - (char *)tl += 9; - if (tl->track != 0xb1) - return tl; - (char *)tl += 9; - if (tl->track != 0xb2) - return tl; - (char *)tl += 9; - if (tl->track != 0xb3) - return tl; - (char *)tl += 9; - if (tl->track != 0xb4) - return tl; - (char *)tl += 9; - if (tl->track != 0xc0) - return tl; - (char *)tl += 9; - return tl; -} - -static int -read_toc(unsigned unit) -{ - struct scd_data *cd; - unsigned part = 0; /* For now ... */ - struct sony_toc toc; - struct sony_tracklist *tl; - int rc, i, j; - u_long first, last; - - cd = scd_data + unit; - - rc = send_cmd(unit, CMD_GET_TOC, 1, part+1); - if (rc < 0) - return rc; - if (rc > sizeof(toc)) { - printf("scd%d: program error: toc too large (%d)\n", unit, rc); - return EIO; - } - if (get_result(unit, rc, (u_char *)&toc) != 0) - return EIO; - - XDEBUG(1, ("scd%d: toc read. len = %d, sizeof(toc) = %d\n", unit, rc, sizeof(toc))); - - tl = get_tl(&toc, rc); - first = msf2hsg(tl->start_msf); - last = msf2hsg(toc.lead_out_start_msf); - cd->blksize = SCDBLKSIZE; - cd->disksize = last*cd->blksize/DEV_BSIZE; - - XDEBUG(1, ("scd%d: firstsector = %ld, lastsector = %ld", unit, - first, last)); - - cd->first_track = bcd2bin(toc.first_track); - cd->last_track = bcd2bin(toc.last_track); - if (cd->last_track > (MAX_TRACKS-2)) - cd->last_track = MAX_TRACKS-2; - for (j = 0, i = cd->first_track; i <= cd->last_track; i++, j++) { - cd->toc[i].adr = tl[j].adr; - cd->toc[i].ctl = tl[j].ctl; /* for xcdplayer */ - bcopy(tl[j].start_msf, cd->toc[i].start_msf, 3); -#ifdef SCD_DEBUG - if (scd_debuglevel > 0) { - if ((j % 3) == 0) - printf("\nscd%d: tracks ", unit); - printf("[%03d: %2d %2d %2d] ", i, - bcd2bin(cd->toc[i].start_msf[0]), - bcd2bin(cd->toc[i].start_msf[1]), - bcd2bin(cd->toc[i].start_msf[2])); - } -#endif - } - bcopy(toc.lead_out_start_msf, cd->toc[cd->last_track+1].start_msf, 3); -#ifdef SCD_DEBUG - if (scd_debuglevel > 0) { - i = cd->last_track+1; - printf("[END: %2d %2d %2d]\n", - bcd2bin(cd->toc[i].start_msf[0]), - bcd2bin(cd->toc[i].start_msf[1]), - bcd2bin(cd->toc[i].start_msf[2])); - } -#endif - - bzero(&cd->dlabel,sizeof(struct disklabel)); - /* filled with spaces first */ - strncpy(cd->dlabel.d_typename," ", - sizeof(cd->dlabel.d_typename)); - strncpy(cd->dlabel.d_typename, cd->name, - min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1)); - strncpy(cd->dlabel.d_packname,"unknown ", - sizeof(cd->dlabel.d_packname)); - cd->dlabel.d_secsize = cd->blksize; - cd->dlabel.d_nsectors = 100; - cd->dlabel.d_ntracks = 1; - cd->dlabel.d_ncylinders = (cd->disksize/100)+1; - cd->dlabel.d_secpercyl = 100; - cd->dlabel.d_secperunit = cd->disksize; - cd->dlabel.d_rpm = 300; - cd->dlabel.d_interleave = 1; - cd->dlabel.d_flags = D_REMOVABLE; - cd->dlabel.d_npartitions= 1; - cd->dlabel.d_partitions[0].p_offset = 0; - cd->dlabel.d_partitions[0].p_size = cd->disksize; - cd->dlabel.d_partitions[0].p_fstype = 9; - cd->dlabel.d_magic = DISKMAGIC; - cd->dlabel.d_magic2 = DISKMAGIC; - cd->dlabel.d_checksum = dkcksum(&cd->dlabel); - - cd->flags |= SCDTOC; - - return 0; -} - -static void -init_drive(unsigned unit) -{ - int rc; - - rc = send_cmd(unit, CMD_SET_DRIVE_PARAM, 2, - 0x05, 0x03 | ((scd_data[unit].double_speed) ? 0x04: 0)); - if (rc != 0) - printf("scd%d: Unable to set parameters. Errcode = 0x%x\n", unit, rc); -} - -/* Returns 0 or errno */ -static int -get_result(u_int unit, int result_len, u_char *result) -{ - unsigned int port = scd_data[unit].iobase; - unsigned int res_reg = port + IREG_RESULT; - int loop_index = 2; /* send_cmd() reads two bytes ... */ - - XDEBUG(1, ("scd%d: DEBUG: get_result: bytes=%d\n", unit, result_len)); - - while (result_len-- > 0) { - if (loop_index++ >= 10) { - loop_index = 1; - if (waitfor_status_bits(unit, SBIT_RESULT_READY, 0)) - return EIO; - write_control(port, CBIT_RESULT_READY_CLEAR); - } - if (result) - *result++ = inb(res_reg); - else - (void)inb(res_reg); - } - return 0; -} - -/* Returns -0x100 for timeout, -(drive error code) OR number of result bytes */ -static int -send_cmd(u_int unit, u_char cmd, u_int nargs, ...) -{ - va_list ap; - u_int port = scd_data[unit].iobase; - u_int reg; - u_char c; - int rc; - int i; - - if (waitfor_status_bits(unit, 0, SBIT_BUSY)) { - printf("scd%d: drive busy\n", unit); - return -0x100; - } - - XDEBUG(1,("scd%d: DEBUG: send_cmd: cmd=0x%x nargs=%d", unit, cmd, nargs)); - - write_control(port, CBIT_RESULT_READY_CLEAR); - write_control(port, CBIT_RPARAM_CLEAR); - - for (i = 0; i < 100; i++) - if (FSTATUS_BIT(port, FBIT_WPARAM_READY)) - break; - if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) { - XDEBUG(1, ("\nscd%d: wparam timeout\n", unit)); - return -EIO; - } - - va_start(ap, nargs); - reg = port + OREG_WPARAMS; - for (i = 0; i < nargs; i++) { - c = (u_char)va_arg(ap, int); - outb(reg, c); - XDEBUG(1, (",{0x%x}", c)); - } - va_end(ap); - XDEBUG(1, ("\n")); - - outb(port+OREG_COMMAND, cmd); - - rc = waitfor_status_bits(unit, SBIT_RESULT_READY, SBIT_BUSY); - if (rc) - return -0x100; - - reg = port + IREG_RESULT; - write_control(port, CBIT_RESULT_READY_CLEAR); - switch ((rc = inb(reg)) & 0xf0) { - case 0x20: - rc = inb(reg); - /* FALLTHROUGH */ - case 0x50: - XDEBUG(1, ("scd%d: DEBUG: send_cmd: drive_error=0x%x\n", unit, rc)); - return -rc; - case 0x00: - default: - rc = inb(reg); - XDEBUG(1, ("scd%d: DEBUG: send_cmd: result_len=%d\n", unit, rc)); - return rc; - } -} - -static void -print_error(int unit, int errcode) -{ - switch (errcode) { - case -ERR_CD_NOT_LOADED: - printf("scd%d: door is open\n", unit); - break; - case -ERR_NO_CD_INSIDE: - printf("scd%d: no cd inside\n", unit); - break; - default: - if (errcode == -0x100 || errcode > 0) - printf("scd%d: device timeout\n", unit); - else - printf("scd%d: unexpected error 0x%x\n", unit, -errcode); - break; - } -} - -/* Returns 0 or errno value */ -static int -waitfor_status_bits(int unit, int bits_set, int bits_clear) -{ - u_int port = scd_data[unit].iobase; - u_int flags = scd_data[unit].flags; - u_int reg = port + IREG_STATUS; - u_int max_loop; - u_char c = 0; - - if (flags & SCDPROBING) { - max_loop = 0; - while (max_loop++ < 1000) { - c = inb(reg); - if (c == 0xff) - return EIO; - if (c & SBIT_ATTENTION) { - process_attention(unit); - continue; - } - if ((c & bits_set) == bits_set && - (c & bits_clear) == 0) - { - break; - } - DELAY(10000); - } - } else { - max_loop = 100; - while (max_loop-- > 0) { - c = inb(reg); - if (c & SBIT_ATTENTION) { - process_attention(unit); - continue; - } - if ((c & bits_set) == bits_set && - (c & bits_clear) == 0) - { - break; - } - tsleep(waitfor_status_bits, PZERO - 1, "waitfor", hz/10); - } - } - if ((c & bits_set) == bits_set && - (c & bits_clear) == 0) - { - return 0; - } -#ifdef SCD_DEBUG - if (scd_debuglevel > 0) - printf("scd%d: DEBUG: waitfor: TIMEOUT (0x%x,(0x%x,0x%x))\n", unit, c, bits_set, bits_clear); - else -#endif - printf("scd%d: timeout.\n", unit); - return EIO; -} - -/* these two routines for xcdplayer - "borrowed" from mcd.c */ -static int -scd_toc_header (int unit, struct ioc_toc_header* th) -{ - struct scd_data *cd = scd_data + unit; - int rc; - - if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) { - print_error(unit, rc); - return EIO; - } - - th->starting_track = cd->first_track; - th->ending_track = cd->last_track; - th->len = 0; /* not used */ - - return 0; -} - -static int -scd_toc_entrys (int unit, struct ioc_read_toc_entry *te) -{ - struct scd_data *cd = scd_data + unit; - struct cd_toc_entry toc_entry; - int rc, i, len = te->data_len; - - if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) { - print_error(unit, rc); - return EIO; - } - - /* find the toc to copy*/ - i = te->starting_track; - if (i == SCD_LASTPLUS1) - i = cd->last_track + 1; - - /* verify starting track */ - if (i < cd->first_track || i > cd->last_track+1) - return EINVAL; - - /* valid length ? */ - if (len < sizeof(struct cd_toc_entry) - || (len % sizeof(struct cd_toc_entry)) != 0) - return EINVAL; - - /* copy the toc data */ - toc_entry.control = cd->toc[i].ctl; - toc_entry.addr_type = te->address_format; - toc_entry.track = i; - if (te->address_format == CD_MSF_FORMAT) { - toc_entry.addr.msf.unused = 0; - toc_entry.addr.msf.minute = bcd2bin(cd->toc[i].start_msf[0]); - toc_entry.addr.msf.second = bcd2bin(cd->toc[i].start_msf[1]); - toc_entry.addr.msf.frame = bcd2bin(cd->toc[i].start_msf[2]); - } - - /* copy the data back */ - if (copyout(&toc_entry, te->data, sizeof(struct cd_toc_entry)) != 0) - return EFAULT; - - return 0; -} - - -static int -scd_toc_entry (int unit, struct ioc_read_toc_single_entry *te) -{ - struct scd_data *cd = scd_data + unit; - struct cd_toc_entry toc_entry; - int rc, i; - - if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) { - print_error(unit, rc); - return EIO; - } - - /* find the toc to copy*/ - i = te->track; - if (i == SCD_LASTPLUS1) - i = cd->last_track + 1; - - /* verify starting track */ - if (i < cd->first_track || i > cd->last_track+1) - return EINVAL; - - /* copy the toc data */ - toc_entry.control = cd->toc[i].ctl; - toc_entry.addr_type = te->address_format; - toc_entry.track = i; - if (te->address_format == CD_MSF_FORMAT) { - toc_entry.addr.msf.unused = 0; - toc_entry.addr.msf.minute = bcd2bin(cd->toc[i].start_msf[0]); - toc_entry.addr.msf.second = bcd2bin(cd->toc[i].start_msf[1]); - toc_entry.addr.msf.frame = bcd2bin(cd->toc[i].start_msf[2]); - } - - /* copy the data back */ - bcopy(&toc_entry, &te->entry, sizeof(struct cd_toc_entry)); - - return 0; -} -#endif diff --git a/sys/i386/isa/scdreg.h b/sys/i386/isa/scdreg.h deleted file mode 100644 index e9ce3b1..0000000 --- a/sys/i386/isa/scdreg.h +++ /dev/null @@ -1,151 +0,0 @@ -/*- - * Copyright (c) 1995 Mikael Hybsch - * 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 - * in this position and unchanged. - * 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. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. - * - * $FreeBSD$ - * - */ - -#ifndef SCD_H -#define SCD_H - -#ifdef __GNUC__ -#if __GNUC__ >= 2 -#pragma pack(1) -#endif -#endif - -typedef unsigned char bcd_t; -#define M_msf(msf) msf[0] -#define S_msf(msf) msf[1] -#define F_msf(msf) msf[2] - -#define IS_ATTENTION(port) ((inb(port+IREG_STATUS) & SBIT_ATTENTION) != 0) -#define IS_BUSY(port) ((inb(port+IREG_STATUS) & SBIT_BUSY) != 0) -#define IS_DATA_RDY(port) ((inb(port+IREG_STATUS) & SBIT_DATA_READY) != 0) -#define STATUS_BIT(port, bit) ((inb(port+IREG_STATUS) & (bit)) != 0) -#define FSTATUS_BIT(port, bit) ((inb(port+IREG_FSTATUS) & (bit)) != 0) - -#define OREG_COMMAND 0 -#define OREG_WPARAMS 1 -#define OREG_CONTROL 3 -#define CBIT_ATTENTION_CLEAR 0x01 -#define CBIT_RESULT_READY_CLEAR 0x02 -#define CBIT_DATA_READY_CLEAR 0x04 -#define CBIT_RPARAM_CLEAR 0x40 -#define CBIT_RESET_DRIVE 0x80 - -#define IREG_STATUS 0 -#define SBIT_ATTENTION 0x01 -#define SBIT_RESULT_READY 0x02 -#define SBIT_DATA_READY 0x04 -#define SBIT_BUSY 0x80 - -#define IREG_RESULT 1 -#define IREG_DATA 2 -#define IREG_FSTATUS 3 -#define FBIT_WPARAM_READY 0x01 - -#define CMD_GET_DRIVE_CONFIG 0x00 -#define CMD_SET_DRIVE_PARAM 0x10 -#define CMD_GET_SUBCHANNEL_DATA 0x21 -#define CMD_GET_TOC 0x24 -#define CMD_READ_TOC 0x30 -#define CMD_READ 0x34 -#define CMD_PLAY_AUDIO 0x40 -#define CMD_STOP_AUDIO 0x41 -#define CMD_EJECT 0x50 -#define CMD_SPIN_UP 0x51 -#define CMD_SPIN_DOWN 0x52 - -#define ERR_CD_NOT_LOADED 0x20 -#define ERR_NO_CD_INSIDE 0x21 -#define ERR_NOT_SPINNING 0x22 -#define ERR_FATAL_READ_ERROR1 0x53 -#define ERR_FATAL_READ_ERROR2 0x57 - -#define ATTEN_DRIVE_LOADED 0x80 -#define ATTEN_EJECT_PUSHED 0x81 -#define ATTEN_AUDIO_DONE 0x90 -#define ATTEN_SPIN_UP_DONE 0x24 -#define ATTEN_SPIN_DOWN 0x27 -#define ATTEN_EJECT_DONE 0x28 - - -struct sony_drive_configuration { - char vendor[8]; - char product[16]; - char revision[8]; - u_short config; -}; - -/* Almost same as cd_sub_channel_position_data */ -struct sony_subchannel_position_data { - u_char control:4; - u_char addr_type:4; - u_char track_number; - u_char index_number; - u_char rel_msf[3]; - u_char dummy; - u_char abs_msf[3]; -}; - -struct sony_tracklist { - u_char adr :4; /* xcdplayer needs these two values */ - u_char ctl :4; - u_char track; - u_char start_msf[3]; -}; - -#define MAX_TRACKS 100 - -struct sony_toc { - u_char session_number; - - u_char :8; - u_char :8; - u_char first_track; - u_char :8; - u_char :8; - - u_char :8; - u_char :8; - u_char last_track; - u_char :8; - u_char :8; - - u_char :8; - u_char :8; - u_char lead_out_start_msf[3]; - - struct sony_tracklist tracks[MAX_TRACKS]; - - /* The rest is just to take space in case all data is returned */ - - u_char dummy[6*9]; -}; - -#endif /* SCD_H */ diff --git a/sys/modules/scd/Makefile b/sys/modules/scd/Makefile new file mode 100644 index 0000000..4cf4145 --- /dev/null +++ b/sys/modules/scd/Makefile @@ -0,0 +1,9 @@ +# $FreeBSD$ + +.PATH: ${.CURDIR}/../../dev/scd + +KMOD= scd +SRCS= scd.c scd_isa.c +SRCS+= bus_if.h device_if.h isa_if.h + +.include <bsd.kmod.mk> |