2 * Copyright (c) 1995 Mikael Hybsch
5 * Portions of this file are copied from mcd.c
6 * which has the following copyrights:
8 * Copyright 1993 by Holger Veit (data part)
9 * Copyright 1993 by Brian Moore (audio part)
10 * Changes Copyright 1993 by Gary Clark II
11 * Changes Copyright (C) 1994 by Andrew A. Chernov
13 * Rewrote probe routine to work on newer Mitsumi drives.
14 * Additional changes (C) 1994 by Jordan K. Hubbard
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer
23 * in this position and unchanged.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. The name of the author may not be used to endorse or promote products
28 * derived from this software without specific prior written permission
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
53 #include <sys/fcntl.h>
59 #include <machine/stdarg.h>
61 #include <machine/bus.h>
62 #include <machine/resource.h>
65 #include <isa/isavar.h>
67 #include <dev/scd/scdreg.h>
68 #include <dev/scd/scdvar.h>
71 #define SCDOPEN 0x0001 /* device opened */
72 #define SCDVALID 0x0002 /* parameters loaded */
73 #define SCDINIT 0x0004 /* device is init'd */
74 #define SCDPROBING 0x0020 /* probing */
75 #define SCDTOC 0x0100 /* already read toc */
76 #define SCDMBXBSY 0x0200 /* local mbx is busy */
77 #define SCDSPINNING 0x0400 /* drive is spun up */
80 #define SCD_S_BEGIN1 1
81 #define SCD_S_WAITSTAT 2
82 #define SCD_S_WAITFIFO 3
83 #define SCD_S_WAITSPIN 4
84 #define SCD_S_WAITREAD 5
85 #define SCD_S_WAITPARAM 6
87 #define RDELAY_WAIT 300
88 #define RDELAY_WAITREAD 300
90 #define SCDBLKSIZE 2048
93 static int scd_debuglevel = SCD_DEBUG;
94 # define XDEBUG(sc, level, fmt, args...) \
96 if (scd_debuglevel >= level) \
97 device_printf(sc->dev, fmt, ## args); \
100 # define XDEBUG(sc, level, fmt, args...)
103 #define IS_ATTENTION(sc) ((SCD_READ(sc, IREG_STATUS) & SBIT_ATTENTION) != 0)
104 #define IS_BUSY(sc) ((SCD_READ(sc, IREG_STATUS) & SBIT_BUSY) != 0)
105 #define IS_DATA_RDY(sc) ((SCD_READ(sc, IREG_STATUS) & SBIT_DATA_READY) != 0)
106 #define STATUS_BIT(sc, bit) ((SCD_READ(sc, IREG_STATUS) & (bit)) != 0)
107 #define FSTATUS_BIT(sc, bit) ((SCD_READ(sc, IREG_FSTATUS) & (bit)) != 0)
110 static void hsg2msf(int hsg, bcd_t *msf);
111 static int msf2hsg(bcd_t *msf);
113 static void process_attention(struct scd_softc *);
114 static int waitfor_status_bits(struct scd_softc *, int bits_set, int bits_clear);
115 static int send_cmd(struct scd_softc *, u_char cmd, u_int nargs, ...);
116 static void init_drive(struct scd_softc *);
117 static int spin_up(struct scd_softc *);
118 static int read_toc(struct scd_softc *);
119 static int get_result(struct scd_softc *, int result_len, u_char *result);
120 static void print_error(struct scd_softc *, int errcode);
122 static void scd_start(struct scd_softc *);
123 static void scd_timeout(void *);
124 static void scd_doread(struct scd_softc *, int state, struct scd_mbx *mbxin);
126 static int scd_eject(struct scd_softc *);
127 static int scd_stop(struct scd_softc *);
128 static int scd_pause(struct scd_softc *);
129 static int scd_resume(struct scd_softc *);
130 static int scd_playtracks(struct scd_softc *, struct ioc_play_track *pt);
131 static int scd_playmsf(struct scd_softc *, struct ioc_play_msf *msf);
132 static int scd_play(struct scd_softc *, struct ioc_play_msf *msf);
133 static int scd_subchan(struct scd_softc *, struct ioc_read_subchannel *sch);
134 static int read_subcode(struct scd_softc *, struct sony_subchannel_position_data *sch);
137 static int scd_toc_header(struct scd_softc *, struct ioc_toc_header *th);
138 static int scd_toc_entrys(struct scd_softc *, struct ioc_read_toc_entry *te);
139 static int scd_toc_entry(struct scd_softc *, struct ioc_read_toc_single_entry *te);
140 #define SCD_LASTPLUS1 170 /* don't ask, xcdplayer passes this in */
142 static d_open_t scdopen;
143 static d_close_t scdclose;
144 static d_ioctl_t scdioctl;
145 static d_strategy_t scdstrategy;
148 static struct cdevsw scd_cdevsw = {
149 .d_version = D_VERSION,
154 .d_strategy = scdstrategy,
160 scd_attach(struct scd_softc *sc)
164 unit = device_get_unit(sc->dev);
169 sc->data.flags = SCDINIT;
170 sc->data.audio_status = CD_AS_AUDIO_INVALID;
171 bioq_init(&sc->data.head);
174 sc->scd_dev_t = make_dev(&scd_cdevsw, 8 * unit,
175 UID_ROOT, GID_OPERATOR, 0640, "scd%d", unit);
176 sc->scd_dev_t->si_drv1 = (void *)sc;
177 device_printf(sc->dev,
178 "WARNING: This driver is deprecated and will be removed.\n");
184 scdopen(struct cdev *dev, int flags, int fmt, struct thread *td)
186 struct scd_softc *sc;
189 sc = (struct scd_softc *)dev->si_drv1;
191 /* mark all open part's invalid */
193 if (sc->data.openflag) {
198 XDEBUG(sc, 1, "DEBUG: status = 0x%x\n", SCD_READ(sc, IREG_STATUS));
200 if ((rc = spin_up(sc)) != 0) {
205 if (!(sc->data.flags & SCDTOC)) {
208 while (loop_count-- > 0 && (rc = read_toc(sc)) != 0) {
209 if (rc == ERR_NOT_SPINNING) {
218 device_printf(sc->dev, "TOC read error 0x%x\n", rc);
224 sc->data.openflag = 1;
225 sc->data.flags |= SCDVALID;
232 scdclose(struct cdev *dev, int flags, int fmt, struct thread *td)
234 struct scd_softc *sc;
236 sc = (struct scd_softc *)dev->si_drv1;
239 KASSERT(sc->data.openflag, ("device not open"));
241 if (sc->data.audio_status != CD_AS_PLAY_IN_PROGRESS) {
242 (void)send_cmd(sc, CMD_SPIN_DOWN, 0);
243 sc->data.flags &= ~SCDSPINNING;
247 sc->data.openflag = 0;
254 scdstrategy(struct bio *bp)
256 struct scd_softc *sc;
258 sc = (struct scd_softc *)bp->bio_dev->si_drv1;
260 /* if device invalidated (e.g. media change, door open), error */
262 if (!(sc->data.flags & SCDVALID)) {
263 device_printf(sc->dev, "media changed\n");
269 if (!(bp->bio_cmd == BIO_READ)) {
270 bp->bio_error = EROFS;
274 /* no data to read */
275 if (bp->bio_bcount == 0)
278 if (!(sc->data.flags & SCDTOC)) {
286 bioq_disksort(&sc->data.head, bp);
288 /* now check whether we can perform processing */
294 bp->bio_flags |= BIO_ERROR;
297 bp->bio_resid = bp->bio_bcount;
303 scd_start(struct scd_softc *sc)
307 SCD_ASSERT_LOCKED(sc);
308 if (sc->data.flags & SCDMBXBSY)
311 bp = bioq_takefirst(&sc->data.head);
313 /* block found to process, dequeue */
314 sc->data.flags |= SCDMBXBSY;
320 sc->data.mbx.retry = 3;
321 sc->data.mbx.bp = bp;
323 scd_doread(sc, SCD_S_BEGIN, &(sc->data.mbx));
328 scdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
330 struct scd_softc *sc;
333 sc = (struct scd_softc *)dev->si_drv1;
335 XDEBUG(sc, 1, "ioctl: cmd=0x%lx\n", cmd);
338 if (!(sc->data.flags & SCDVALID)) {
346 *(off_t *)addr = (off_t)sc->data.disksize * sc->data.blksize;
348 case DIOCGSECTORSIZE:
349 *(u_int *)addr = sc->data.blksize;
351 case CDIOCPLAYTRACKS:
352 error = scd_playtracks(sc, (struct ioc_play_track *) addr);
354 case CDIOCPLAYBLOCKS:
358 error = scd_playmsf(sc, (struct ioc_play_msf *) addr);
360 case CDIOCREADSUBCHANNEL:
361 return scd_subchan(sc, (struct ioc_read_subchannel *) addr);
362 case CDIOREADTOCHEADER:
363 error = scd_toc_header (sc, (struct ioc_toc_header *) addr);
365 case CDIOREADTOCENTRYS:
366 return scd_toc_entrys (sc, (struct ioc_read_toc_entry*) addr);
367 case CDIOREADTOCENTRY:
368 error = scd_toc_entry (sc, (struct ioc_read_toc_single_entry*) addr);
381 error = scd_resume(sc);
384 error = scd_pause(sc);
390 error = scd_stop(sc);
393 error = scd_eject(sc);
409 device_printf(sc->dev, "unsupported ioctl (cmd=0x%lx)\n", cmd);
417 /***************************************************************
418 * lower level of driver starts here
419 **************************************************************/
422 scd_playtracks(struct scd_softc *sc, struct ioc_play_track *pt)
424 struct ioc_play_msf msf;
425 int a = pt->start_track;
426 int z = pt->end_track;
429 if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) {
430 if (rc == -ERR_NOT_SPINNING) {
431 if (spin_up(sc) != 0)
441 XDEBUG(sc, 1, "playtracks from %d:%d to %d:%d\n",
442 a, pt->start_index, z, pt->end_index);
444 if ( a < sc->data.first_track
445 || a > sc->data.last_track
447 || z > sc->data.last_track)
450 bcopy(sc->data.toc[a].start_msf, &msf.start_m, 3);
451 hsg2msf(msf2hsg(sc->data.toc[z+1].start_msf)-1, &msf.end_m);
453 return scd_play(sc, &msf);
456 /* The start/end msf is expected to be in bin format */
458 scd_playmsf(struct scd_softc *sc, struct ioc_play_msf *msfin)
460 struct ioc_play_msf msf;
462 msf.start_m = bin2bcd(msfin->start_m);
463 msf.start_s = bin2bcd(msfin->start_s);
464 msf.start_f = bin2bcd(msfin->start_f);
465 msf.end_m = bin2bcd(msfin->end_m);
466 msf.end_s = bin2bcd(msfin->end_s);
467 msf.end_f = bin2bcd(msfin->end_f);
469 return scd_play(sc, &msf);
472 /* The start/end msf is expected to be in bcd format */
474 scd_play(struct scd_softc *sc, struct ioc_play_msf *msf)
478 XDEBUG(sc, 1, "playing: %02x:%02x:%02x -> %02x:%02x:%02x\n",
479 msf->start_m, msf->start_s, msf->start_f,
480 msf->end_m, msf->end_s, msf->end_f);
482 for (i = 0; i < 2; i++) {
483 rc = send_cmd(sc, CMD_PLAY_AUDIO, 7,
485 msf->start_m, msf->start_s, msf->start_f,
486 msf->end_m, msf->end_s, msf->end_f);
487 if (rc == -ERR_NOT_SPINNING) {
488 sc->data.flags &= ~SCDSPINNING;
489 if (spin_up(sc) != 0)
498 sc->data.audio_status = CD_AS_PLAY_IN_PROGRESS;
499 bcopy((char *)msf, (char *)&sc->data.last_play, sizeof(struct ioc_play_msf));
504 scd_stop(struct scd_softc *sc)
507 (void)send_cmd(sc, CMD_STOP_AUDIO, 0);
508 sc->data.audio_status = CD_AS_PLAY_COMPLETED;
513 scd_pause(struct scd_softc *sc)
515 struct sony_subchannel_position_data subpos;
517 if (sc->data.audio_status != CD_AS_PLAY_IN_PROGRESS)
520 if (read_subcode(sc, &subpos) != 0)
523 if (send_cmd(sc, CMD_STOP_AUDIO, 0) != 0)
526 sc->data.last_play.start_m = subpos.abs_msf[0];
527 sc->data.last_play.start_s = subpos.abs_msf[1];
528 sc->data.last_play.start_f = subpos.abs_msf[2];
529 sc->data.audio_status = CD_AS_PLAY_PAUSED;
531 XDEBUG(sc, 1, "pause @ %02x:%02x:%02x\n",
532 sc->data.last_play.start_m,
533 sc->data.last_play.start_s,
534 sc->data.last_play.start_f);
540 scd_resume(struct scd_softc *sc)
543 if (sc->data.audio_status != CD_AS_PLAY_PAUSED)
545 return scd_play(sc, &sc->data.last_play);
549 scd_eject(struct scd_softc *sc)
552 sc->data.audio_status = CD_AS_AUDIO_INVALID;
553 sc->data.flags &= ~(SCDSPINNING|SCDTOC);
555 if (send_cmd(sc, CMD_STOP_AUDIO, 0) != 0 ||
556 send_cmd(sc, CMD_SPIN_DOWN, 0) != 0 ||
557 send_cmd(sc, CMD_EJECT, 0) != 0)
565 scd_subchan(struct scd_softc *sc, struct ioc_read_subchannel *sch)
567 struct sony_subchannel_position_data q;
568 struct cd_sub_channel_info data;
570 XDEBUG(sc, 1, "subchan af=%d, df=%d\n",
571 sch->address_format, sch->data_format);
573 if (sch->address_format != CD_MSF_FORMAT)
576 if (sch->data_format != CD_CURRENT_POSITION)
579 if (read_subcode(sc, &q) != 0)
582 data.header.audio_status = sc->data.audio_status;
583 data.what.position.data_format = CD_MSF_FORMAT;
584 data.what.position.track_number = bcd2bin(q.track_number);
585 data.what.position.reladdr.msf.unused = 0;
586 data.what.position.reladdr.msf.minute = bcd2bin(q.rel_msf[0]);
587 data.what.position.reladdr.msf.second = bcd2bin(q.rel_msf[1]);
588 data.what.position.reladdr.msf.frame = bcd2bin(q.rel_msf[2]);
589 data.what.position.absaddr.msf.unused = 0;
590 data.what.position.absaddr.msf.minute = bcd2bin(q.abs_msf[0]);
591 data.what.position.absaddr.msf.second = bcd2bin(q.abs_msf[1]);
592 data.what.position.absaddr.msf.frame = bcd2bin(q.abs_msf[2]);
595 if (copyout(&data, sch->data, min(sizeof(struct cd_sub_channel_info), sch->data_len))!=0)
601 scd_probe(struct scd_softc *sc)
603 struct sony_drive_configuration drive_config;
605 static char namebuf[8+16+8+3];
609 sc->data.flags = SCDPROBING;
611 bzero(&drive_config, sizeof(drive_config));
615 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESET_DRIVE);
620 /* Only the ATTENTION bit may be set */
621 if ((SCD_READ(sc, IREG_STATUS) & ~1) != 0) {
622 XDEBUG(sc, 1, "too many bits set. probe failed.\n");
625 rc = send_cmd(sc, CMD_GET_DRIVE_CONFIG, 0);
626 if (rc != sizeof(drive_config)) {
627 /* Sometimes if the drive is playing audio I get */
628 /* the bad result 82. Fix by repeating the reset */
629 if (rc > 0 && loop_count++ == 0)
633 if (get_result(sc, rc, (u_char *)&drive_config) != 0)
636 bcopy(drive_config.vendor, namebuf, 8);
638 while (*(s-1) == ' ') /* Strip trailing spaces */
641 bcopy(drive_config.product, s, 16);
643 while (*(s-1) == ' ')
646 bcopy(drive_config.revision, s, 8);
648 while (*(s-1) == ' ')
652 sc->data.name = namebuf;
654 if (drive_config.config & 0x10)
655 sc->data.double_speed = 1;
657 sc->data.double_speed = 0;
663 read_subcode(struct scd_softc *sc, struct sony_subchannel_position_data *scp)
667 rc = send_cmd(sc, CMD_GET_SUBCHANNEL_DATA, 0);
668 if (rc < 0 || rc < sizeof(*scp))
670 if (get_result(sc, rc, (u_char *)scp) != 0)
675 /* State machine copied from mcd.c */
677 /* This (and the code in mcd.c) will not work with more than one drive */
678 /* because there is only one sc->ch_mbxsave below. Should fix that some day. */
679 /* (sc->ch_mbxsave & state should probably be included in the scd_data struct and */
680 /* the unit number used as first argument to scd_doread().) /Micke */
682 /* state machine to process read requests
683 * initialize with SCD_S_BEGIN: reset state machine
684 * SCD_S_WAITSTAT: wait for ready (!busy)
685 * SCD_S_WAITSPIN: wait for drive to spin up (if not spinning)
686 * SCD_S_WAITFIFO: wait for param fifo to get ready, them exec. command.
687 * SCD_S_WAITREAD: wait for data ready, read data
688 * SCD_S_WAITPARAM: wait for command result params, read them, error if bad data read.
692 scd_timeout(void *arg)
694 struct scd_softc *sc;
695 sc = (struct scd_softc *)arg;
697 SCD_ASSERT_LOCKED(sc);
698 scd_doread(sc, sc->ch_state, sc->ch_mbxsave);
702 scd_doread(struct scd_softc *sc, int state, struct scd_mbx *mbxin)
704 struct scd_mbx *mbx = (state!=SCD_S_BEGIN) ? sc->ch_mbxsave : mbxin;
705 struct bio *bp = mbx->bp;
709 static char sdata[3]; /* Must be preserved between calls to this function */
711 SCD_ASSERT_LOCKED(sc);
715 mbx = sc->ch_mbxsave = mbxin;
719 mbx->count = RDELAY_WAIT;
721 process_attention(sc);
725 sc->ch_state = SCD_S_WAITSTAT;
726 callout_stop(&sc->timer);
727 if (mbx->count-- <= 0) {
728 device_printf(sc->dev, "timeout. drive busy.\n");
734 sc->ch_state = SCD_S_WAITSTAT;
735 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
739 process_attention(sc);
741 /* reject, if audio active */
742 if (sc->data.audio_status & CD_AS_PLAY_IN_PROGRESS) {
743 device_printf(sc->dev, "audio is active\n");
747 mbx->sz = sc->data.blksize;
749 /* for first block */
750 mbx->nblk = howmany(bp->bio_bcount, mbx->sz);
754 if (!(sc->data.flags & SCDVALID))
757 blknum = bp->bio_offset / mbx->sz + mbx->skip/mbx->sz;
759 XDEBUG(sc, 2, "scd_doread: read blknum=%d\n", blknum);
761 /* build parameter block */
762 hsg2msf(blknum, sdata);
764 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
765 SCD_WRITE(sc, OREG_CONTROL, CBIT_RPARAM_CLEAR);
766 SCD_WRITE(sc, OREG_CONTROL, CBIT_DATA_READY_CLEAR);
768 if (FSTATUS_BIT(sc, FBIT_WPARAM_READY))
772 sc->ch_state = SCD_S_WAITFIFO;
773 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
777 sc->ch_state = SCD_S_WAITSPIN;
778 callout_stop(&sc->timer);
779 if (mbx->count-- <= 0) {
780 device_printf(sc->dev, "timeout waiting for drive to spin up.\n");
783 if (!STATUS_BIT(sc, SBIT_RESULT_READY)) {
784 sc->ch_state = SCD_S_WAITSPIN;
785 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
788 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
789 switch ((i = SCD_READ(sc, IREG_RESULT)) & 0xf0) {
791 i = SCD_READ(sc, IREG_RESULT);
795 (void)SCD_READ(sc, IREG_RESULT);
796 sc->data.flags |= SCDSPINNING;
799 XDEBUG(sc, 1, "DEBUG: spin up complete\n");
801 state = SCD_S_BEGIN1;
805 sc->ch_state = SCD_S_WAITFIFO;
806 callout_stop(&sc->timer);
807 if (mbx->count-- <= 0) {
808 device_printf(sc->dev, "timeout. write param not ready.\n");
811 if (!FSTATUS_BIT(sc, FBIT_WPARAM_READY)) {
812 sc->ch_state = SCD_S_WAITFIFO;
813 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
816 XDEBUG(sc, 1, "mbx->count (writeparamwait) = %d(%d)\n", mbx->count, 100);
819 /* The reason this test isn't done 'till now is to make sure */
820 /* that it is ok to send the SPIN_UP cmd below. */
821 if (!(sc->data.flags & SCDSPINNING)) {
822 XDEBUG(sc, 1, "spinning up drive ...\n");
823 SCD_WRITE(sc, OREG_COMMAND, CMD_SPIN_UP);
825 sc->ch_state = SCD_S_WAITSPIN;
826 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
830 /* send the read command */
831 SCD_WRITE(sc, OREG_WPARAMS, sdata[0]);
832 SCD_WRITE(sc, OREG_WPARAMS, sdata[1]);
833 SCD_WRITE(sc, OREG_WPARAMS, sdata[2]);
834 SCD_WRITE(sc, OREG_WPARAMS, 0);
835 SCD_WRITE(sc, OREG_WPARAMS, 0);
836 SCD_WRITE(sc, OREG_WPARAMS, 1);
837 SCD_WRITE(sc, OREG_COMMAND, CMD_READ);
839 mbx->count = RDELAY_WAITREAD;
840 for (i = 0; i < 50; i++) {
841 if (STATUS_BIT(sc, SBIT_DATA_READY))
846 sc->ch_state = SCD_S_WAITREAD;
847 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
851 sc->ch_state = SCD_S_WAITREAD;
852 callout_stop(&sc->timer);
853 if (mbx->count-- <= 0) {
854 if (STATUS_BIT(sc, SBIT_RESULT_READY))
856 device_printf(sc->dev, "timeout while reading data\n");
859 if (!STATUS_BIT(sc, SBIT_DATA_READY)) {
860 process_attention(sc);
861 if (!(sc->data.flags & SCDVALID))
863 sc->ch_state = SCD_S_WAITREAD;
864 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
867 XDEBUG(sc, 2, "mbx->count (after RDY_BIT) = %d(%d)\n", mbx->count, RDELAY_WAITREAD);
871 addr = bp->bio_data + mbx->skip;
872 SCD_WRITE(sc, OREG_CONTROL, CBIT_DATA_READY_CLEAR);
873 SCD_READ_MULTI(sc, IREG_DATA, addr, mbx->sz);
876 for (i = 0; i < 20; i++) {
877 if (STATUS_BIT(sc, SBIT_RESULT_READY))
883 case SCD_S_WAITPARAM:
884 sc->ch_state = SCD_S_WAITPARAM;
885 callout_stop(&sc->timer);
886 if (mbx->count-- <= 0) {
887 device_printf(sc->dev, "timeout waiting for params\n");
892 if (!STATUS_BIT(sc, SBIT_RESULT_READY)) {
893 sc->ch_state = SCD_S_WAITPARAM;
894 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
898 if (mbx->count < 100 && scd_debuglevel > 0)
899 device_printf(sc->dev, "mbx->count (paramwait) = %d(%d)\n", mbx->count, 100);
903 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
904 switch ((i = SCD_READ(sc, IREG_RESULT)) & 0xf0) {
907 case ERR_FATAL_READ_ERROR1:
908 case ERR_FATAL_READ_ERROR2:
909 device_printf(sc->dev, "unrecoverable read error 0x%x\n", i);
914 i = SCD_READ(sc, IREG_RESULT);
916 case ERR_NOT_SPINNING:
917 XDEBUG(sc, 1, "read error: drive not spinning\n");
918 if (mbx->retry-- > 0) {
919 state = SCD_S_BEGIN1;
920 sc->data.flags &= ~SCDSPINNING;
929 i = SCD_READ(sc, IREG_RESULT);
933 if (--mbx->nblk > 0) {
934 mbx->skip += mbx->sz;
942 sc->data.flags &= ~SCDMBXBSY;
948 if (mbx->retry-- > 0) {
949 device_printf(sc->dev, "retrying ...\n");
950 state = SCD_S_BEGIN1;
954 /* invalidate the buffer */
956 bp->bio_flags |= BIO_ERROR;
957 bp->bio_resid = bp->bio_bcount;
960 sc->data.flags &= ~SCDMBXBSY;
965 device_printf(sc->dev, "media changed\n");
970 hsg2msf(int hsg, bcd_t *msf)
974 M_msf(msf) = bin2bcd(hsg / 4500);
976 S_msf(msf) = bin2bcd(hsg / 75);
977 F_msf(msf) = bin2bcd(hsg % 75);
984 return (bcd2bin(M_msf(msf)) * 60 +
985 bcd2bin(S_msf(msf))) * 75 +
986 bcd2bin(F_msf(msf)) - 150;
990 process_attention(struct scd_softc *sc)
995 while (IS_ATTENTION(sc) && count++ < 30) {
996 SCD_WRITE(sc, OREG_CONTROL, CBIT_ATTENTION_CLEAR);
997 code = SCD_READ(sc, IREG_RESULT);
1000 if (scd_debuglevel > 0) {
1002 device_printf(sc->dev, "DEBUG: ATTENTIONS = 0x%x", code);
1004 printf(",0x%x", code);
1009 case ATTEN_SPIN_DOWN:
1010 sc->data.flags &= ~SCDSPINNING;
1013 case ATTEN_SPIN_UP_DONE:
1014 sc->data.flags |= SCDSPINNING;
1017 case ATTEN_AUDIO_DONE:
1018 sc->data.audio_status = CD_AS_PLAY_COMPLETED;
1021 case ATTEN_DRIVE_LOADED:
1022 sc->data.flags &= ~(SCDTOC|SCDSPINNING|SCDVALID);
1023 sc->data.audio_status = CD_AS_AUDIO_INVALID;
1026 case ATTEN_EJECT_PUSHED:
1027 sc->data.flags &= ~SCDVALID;
1033 if (scd_debuglevel > 0 && count > 0)
1038 /* Returns 0 OR sony error code */
1040 spin_up(struct scd_softc *sc)
1042 unsigned char res_reg[12];
1043 unsigned int res_size;
1048 rc = send_cmd(sc, CMD_SPIN_UP, 0, 0, res_reg, &res_size);
1050 XDEBUG(sc, 2, "CMD_SPIN_UP error 0x%x\n", rc);
1054 if (!(sc->data.flags & SCDTOC)) {
1055 rc = send_cmd(sc, CMD_READ_TOC, 0);
1056 if (rc == ERR_NOT_SPINNING) {
1057 if (loop_count++ < 3)
1065 sc->data.flags |= SCDSPINNING;
1070 static struct sony_tracklist *
1071 get_tl(struct sony_toc *toc, int size)
1073 struct sony_tracklist *tl = &toc->tracks[0];
1075 if (tl->track != 0xb0)
1077 if (tl->track != 0xb1)
1079 tl = (struct sony_tracklist *)((char *)tl + 9);
1080 if (tl->track != 0xb2)
1082 tl = (struct sony_tracklist *)((char *)tl + 9);
1083 if (tl->track != 0xb3)
1085 tl = (struct sony_tracklist *)((char *)tl + 9);
1086 if (tl->track != 0xb4)
1088 tl = (struct sony_tracklist *)((char *)tl + 9);
1089 if (tl->track != 0xc0)
1091 tl = (struct sony_tracklist *)((char *)tl + 9);
1096 read_toc(struct scd_softc *sc)
1098 struct sony_toc toc;
1099 struct sony_tracklist *tl;
1103 rc = send_cmd(sc, CMD_GET_TOC, 1, 1);
1106 if (rc > sizeof(toc)) {
1107 device_printf(sc->dev, "program error: toc too large (%d)\n", rc);
1110 if (get_result(sc, rc, (u_char *)&toc) != 0)
1113 XDEBUG(sc, 1, "toc read. len = %d, sizeof(toc) = %d\n", rc, sizeof(toc));
1115 tl = get_tl(&toc, rc);
1116 first = msf2hsg(tl->start_msf);
1117 last = msf2hsg(toc.lead_out_start_msf);
1118 sc->data.blksize = SCDBLKSIZE;
1119 sc->data.disksize = last*sc->data.blksize/DEV_BSIZE;
1121 XDEBUG(sc, 1, "firstsector = %ld, lastsector = %ld", first, last);
1123 sc->data.first_track = bcd2bin(toc.first_track);
1124 sc->data.last_track = bcd2bin(toc.last_track);
1125 if (sc->data.last_track > (MAX_TRACKS-2))
1126 sc->data.last_track = MAX_TRACKS-2;
1127 for (j = 0, i = sc->data.first_track; i <= sc->data.last_track; i++, j++) {
1128 sc->data.toc[i].adr = tl[j].adr;
1129 sc->data.toc[i].ctl = tl[j].ctl; /* for xcdplayer */
1130 bcopy(tl[j].start_msf, sc->data.toc[i].start_msf, 3);
1132 if (scd_debuglevel > 0) {
1135 device_printf(sc->dev, "tracks ");
1137 printf("[%03d: %2d %2d %2d] ", i,
1138 bcd2bin(sc->data.toc[i].start_msf[0]),
1139 bcd2bin(sc->data.toc[i].start_msf[1]),
1140 bcd2bin(sc->data.toc[i].start_msf[2]));
1144 bcopy(toc.lead_out_start_msf, sc->data.toc[sc->data.last_track+1].start_msf, 3);
1146 if (scd_debuglevel > 0) {
1147 i = sc->data.last_track+1;
1148 printf("[END: %2d %2d %2d]\n",
1149 bcd2bin(sc->data.toc[i].start_msf[0]),
1150 bcd2bin(sc->data.toc[i].start_msf[1]),
1151 bcd2bin(sc->data.toc[i].start_msf[2]));
1155 sc->data.flags |= SCDTOC;
1161 init_drive(struct scd_softc *sc)
1165 rc = send_cmd(sc, CMD_SET_DRIVE_PARAM, 2,
1166 0x05, 0x03 | ((sc->data.double_speed) ? 0x04: 0));
1168 device_printf(sc->dev, "Unable to set parameters. Errcode = 0x%x\n", rc);
1171 /* Returns 0 or errno */
1173 get_result(struct scd_softc *sc, int result_len, u_char *result)
1175 int loop_index = 2; /* send_cmd() reads two bytes ... */
1177 XDEBUG(sc, 1, "DEBUG: get_result: bytes=%d\n", result_len);
1179 while (result_len-- > 0) {
1180 if (loop_index++ >= 10) {
1182 if (waitfor_status_bits(sc, SBIT_RESULT_READY, 0))
1184 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
1187 *result++ = SCD_READ(sc, IREG_RESULT);
1189 (void)SCD_READ(sc, IREG_RESULT);
1194 /* Returns -0x100 for timeout, -(drive error code) OR number of result bytes */
1196 send_cmd(struct scd_softc *sc, u_char cmd, u_int nargs, ...)
1203 if (waitfor_status_bits(sc, 0, SBIT_BUSY)) {
1204 device_printf(sc->dev, "drive busy\n");
1208 XDEBUG(sc, 1, "DEBUG: send_cmd: cmd=0x%x nargs=%d", cmd, nargs);
1210 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
1211 SCD_WRITE(sc, OREG_CONTROL, CBIT_RPARAM_CLEAR);
1213 for (i = 0; i < 100; i++)
1214 if (FSTATUS_BIT(sc, FBIT_WPARAM_READY))
1216 if (!FSTATUS_BIT(sc, FBIT_WPARAM_READY)) {
1217 XDEBUG(sc, 1, "\nwparam timeout\n");
1221 va_start(ap, nargs);
1222 for (i = 0; i < nargs; i++) {
1223 c = (u_char)va_arg(ap, int);
1224 SCD_WRITE(sc, OREG_WPARAMS, c);
1225 XDEBUG(sc, 1, ",{0x%x}", c);
1228 XDEBUG(sc, 1, "\n");
1230 SCD_WRITE(sc, OREG_COMMAND, cmd);
1232 rc = waitfor_status_bits(sc, SBIT_RESULT_READY, SBIT_BUSY);
1236 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
1237 switch ((rc = SCD_READ(sc, IREG_RESULT)) & 0xf0) {
1239 rc = SCD_READ(sc, IREG_RESULT);
1242 XDEBUG(sc, 1, "DEBUG: send_cmd: drive_error=0x%x\n", rc);
1246 rc = SCD_READ(sc, IREG_RESULT);
1247 XDEBUG(sc, 1, "DEBUG: send_cmd: result_len=%d\n", rc);
1253 print_error(struct scd_softc *sc, int errcode)
1257 case -ERR_CD_NOT_LOADED:
1258 device_printf(sc->dev, "door is open\n");
1260 case -ERR_NO_CD_INSIDE:
1261 device_printf(sc->dev, "no cd inside\n");
1264 if (errcode == -0x100 || errcode > 0)
1265 device_printf(sc->dev, "device timeout\n");
1267 device_printf(sc->dev, "unexpected error 0x%x\n", -errcode);
1272 /* Returns 0 or errno value */
1274 waitfor_status_bits(struct scd_softc *sc, int bits_set, int bits_clear)
1276 u_int flags = sc->data.flags;
1280 if (flags & SCDPROBING) {
1282 while (max_loop++ < 1000) {
1283 c = SCD_READ(sc, IREG_STATUS);
1286 if (c & SBIT_ATTENTION) {
1287 process_attention(sc);
1290 if ((c & bits_set) == bits_set &&
1291 (c & bits_clear) == 0)
1299 while (max_loop-- > 0) {
1300 c = SCD_READ(sc, IREG_STATUS);
1301 if (c & SBIT_ATTENTION) {
1302 process_attention(sc);
1305 if ((c & bits_set) == bits_set &&
1306 (c & bits_clear) == 0)
1311 pause("waitfor", hz/10);
1315 if ((c & bits_set) == bits_set &&
1316 (c & bits_clear) == 0)
1321 if (scd_debuglevel > 0)
1322 device_printf(sc->dev, "DEBUG: waitfor: TIMEOUT (0x%x,(0x%x,0x%x))\n", c, bits_set, bits_clear);
1325 device_printf(sc->dev, "timeout.\n");
1329 /* these two routines for xcdplayer - "borrowed" from mcd.c */
1331 scd_toc_header (struct scd_softc *sc, struct ioc_toc_header* th)
1335 if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) {
1336 print_error(sc, rc);
1340 th->starting_track = sc->data.first_track;
1341 th->ending_track = sc->data.last_track;
1342 th->len = 0; /* not used */
1348 scd_toc_entrys (struct scd_softc *sc, struct ioc_read_toc_entry *te)
1350 struct cd_toc_entry toc_entry;
1351 int rc, i, len = te->data_len;
1353 if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) {
1354 print_error(sc, rc);
1358 /* find the toc to copy*/
1359 i = te->starting_track;
1360 if (i == SCD_LASTPLUS1)
1361 i = sc->data.last_track + 1;
1363 /* verify starting track */
1364 if (i < sc->data.first_track || i > sc->data.last_track+1)
1367 /* valid length ? */
1368 if (len < sizeof(struct cd_toc_entry)
1369 || (len % sizeof(struct cd_toc_entry)) != 0)
1372 /* copy the toc data */
1373 toc_entry.control = sc->data.toc[i].ctl;
1374 toc_entry.addr_type = te->address_format;
1375 toc_entry.track = i;
1376 if (te->address_format == CD_MSF_FORMAT) {
1377 toc_entry.addr.msf.unused = 0;
1378 toc_entry.addr.msf.minute = bcd2bin(sc->data.toc[i].start_msf[0]);
1379 toc_entry.addr.msf.second = bcd2bin(sc->data.toc[i].start_msf[1]);
1380 toc_entry.addr.msf.frame = bcd2bin(sc->data.toc[i].start_msf[2]);
1384 /* copy the data back */
1385 if (copyout(&toc_entry, te->data, sizeof(struct cd_toc_entry)) != 0)
1393 scd_toc_entry (struct scd_softc *sc, struct ioc_read_toc_single_entry *te)
1395 struct cd_toc_entry toc_entry;
1398 if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) {
1399 print_error(sc, rc);
1403 /* find the toc to copy*/
1405 if (i == SCD_LASTPLUS1)
1406 i = sc->data.last_track + 1;
1408 /* verify starting track */
1409 if (i < sc->data.first_track || i > sc->data.last_track+1)
1412 /* copy the toc data */
1413 toc_entry.control = sc->data.toc[i].ctl;
1414 toc_entry.addr_type = te->address_format;
1415 toc_entry.track = i;
1416 if (te->address_format == CD_MSF_FORMAT) {
1417 toc_entry.addr.msf.unused = 0;
1418 toc_entry.addr.msf.minute = bcd2bin(sc->data.toc[i].start_msf[0]);
1419 toc_entry.addr.msf.second = bcd2bin(sc->data.toc[i].start_msf[1]);
1420 toc_entry.addr.msf.frame = bcd2bin(sc->data.toc[i].start_msf[2]);
1423 /* copy the data back */
1424 bcopy(&toc_entry, &te->entry, sizeof(struct cd_toc_entry));