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, int nocopyout);
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;
182 scdopen(struct cdev *dev, int flags, int fmt, struct thread *td)
184 struct scd_softc *sc;
187 sc = (struct scd_softc *)dev->si_drv1;
189 /* mark all open part's invalid */
191 if (sc->data.openflag) {
196 XDEBUG(sc, 1, "DEBUG: status = 0x%x\n", SCD_READ(sc, IREG_STATUS));
198 if ((rc = spin_up(sc)) != 0) {
203 if (!(sc->data.flags & SCDTOC)) {
206 while (loop_count-- > 0 && (rc = read_toc(sc)) != 0) {
207 if (rc == ERR_NOT_SPINNING) {
216 device_printf(sc->dev, "TOC read error 0x%x\n", rc);
222 sc->data.openflag = 1;
223 sc->data.flags |= SCDVALID;
230 scdclose(struct cdev *dev, int flags, int fmt, struct thread *td)
232 struct scd_softc *sc;
234 sc = (struct scd_softc *)dev->si_drv1;
237 KASSERT(sc->data.openflag, ("device not open"));
239 if (sc->data.audio_status != CD_AS_PLAY_IN_PROGRESS) {
240 (void)send_cmd(sc, CMD_SPIN_DOWN, 0);
241 sc->data.flags &= ~SCDSPINNING;
245 sc->data.openflag = 0;
252 scdstrategy(struct bio *bp)
254 struct scd_softc *sc;
256 sc = (struct scd_softc *)bp->bio_dev->si_drv1;
258 /* if device invalidated (e.g. media change, door open), error */
260 if (!(sc->data.flags & SCDVALID)) {
261 device_printf(sc->dev, "media changed\n");
267 if (!(bp->bio_cmd == BIO_READ)) {
268 bp->bio_error = EROFS;
272 /* no data to read */
273 if (bp->bio_bcount == 0)
276 if (!(sc->data.flags & SCDTOC)) {
284 bioq_disksort(&sc->data.head, bp);
286 /* now check whether we can perform processing */
292 bp->bio_flags |= BIO_ERROR;
295 bp->bio_resid = bp->bio_bcount;
301 scd_start(struct scd_softc *sc)
305 SCD_ASSERT_LOCKED(sc);
306 if (sc->data.flags & SCDMBXBSY)
309 bp = bioq_takefirst(&sc->data.head);
311 /* block found to process, dequeue */
312 sc->data.flags |= SCDMBXBSY;
318 sc->data.mbx.retry = 3;
319 sc->data.mbx.bp = bp;
321 scd_doread(sc, SCD_S_BEGIN, &(sc->data.mbx));
326 scdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
328 struct scd_softc *sc;
331 sc = (struct scd_softc *)dev->si_drv1;
333 XDEBUG(sc, 1, "ioctl: cmd=0x%lx\n", cmd);
336 if (!(sc->data.flags & SCDVALID)) {
344 *(off_t *)addr = (off_t)sc->data.disksize * sc->data.blksize;
346 case DIOCGSECTORSIZE:
347 *(u_int *)addr = sc->data.blksize;
349 case CDIOCPLAYTRACKS:
350 error = scd_playtracks(sc, (struct ioc_play_track *) addr);
352 case CDIOCPLAYBLOCKS:
356 error = scd_playmsf(sc, (struct ioc_play_msf *) addr);
358 case CDIOCREADSUBCHANNEL_SYSSPACE:
359 return scd_subchan(sc, (struct ioc_read_subchannel *) addr, 1);
360 case CDIOCREADSUBCHANNEL:
361 return scd_subchan(sc, (struct ioc_read_subchannel *) addr, 0);
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, int nocopyout)
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 (nocopyout == 0) {
596 if (copyout(&data, sch->data, min(sizeof(struct cd_sub_channel_info), sch->data_len))!=0)
599 bcopy(&data, sch->data, min(sizeof(struct cd_sub_channel_info), sch->data_len));
605 scd_probe(struct scd_softc *sc)
607 struct sony_drive_configuration drive_config;
609 static char namebuf[8+16+8+3];
613 sc->data.flags = SCDPROBING;
615 bzero(&drive_config, sizeof(drive_config));
619 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESET_DRIVE);
624 /* Only the ATTENTION bit may be set */
625 if ((SCD_READ(sc, IREG_STATUS) & ~1) != 0) {
626 XDEBUG(sc, 1, "too many bits set. probe failed.\n");
629 rc = send_cmd(sc, CMD_GET_DRIVE_CONFIG, 0);
630 if (rc != sizeof(drive_config)) {
631 /* Sometimes if the drive is playing audio I get */
632 /* the bad result 82. Fix by repeating the reset */
633 if (rc > 0 && loop_count++ == 0)
637 if (get_result(sc, rc, (u_char *)&drive_config) != 0)
640 bcopy(drive_config.vendor, namebuf, 8);
642 while (*(s-1) == ' ') /* Strip trailing spaces */
645 bcopy(drive_config.product, s, 16);
647 while (*(s-1) == ' ')
650 bcopy(drive_config.revision, s, 8);
652 while (*(s-1) == ' ')
656 sc->data.name = namebuf;
658 if (drive_config.config & 0x10)
659 sc->data.double_speed = 1;
661 sc->data.double_speed = 0;
667 read_subcode(struct scd_softc *sc, struct sony_subchannel_position_data *scp)
671 rc = send_cmd(sc, CMD_GET_SUBCHANNEL_DATA, 0);
672 if (rc < 0 || rc < sizeof(*scp))
674 if (get_result(sc, rc, (u_char *)scp) != 0)
679 /* State machine copied from mcd.c */
681 /* This (and the code in mcd.c) will not work with more than one drive */
682 /* because there is only one sc->ch_mbxsave below. Should fix that some day. */
683 /* (sc->ch_mbxsave & state should probably be included in the scd_data struct and */
684 /* the unit number used as first argument to scd_doread().) /Micke */
686 /* state machine to process read requests
687 * initialize with SCD_S_BEGIN: reset state machine
688 * SCD_S_WAITSTAT: wait for ready (!busy)
689 * SCD_S_WAITSPIN: wait for drive to spin up (if not spinning)
690 * SCD_S_WAITFIFO: wait for param fifo to get ready, them exec. command.
691 * SCD_S_WAITREAD: wait for data ready, read data
692 * SCD_S_WAITPARAM: wait for command result params, read them, error if bad data read.
696 scd_timeout(void *arg)
698 struct scd_softc *sc;
699 sc = (struct scd_softc *)arg;
701 SCD_ASSERT_LOCKED(sc);
702 scd_doread(sc, sc->ch_state, sc->ch_mbxsave);
706 scd_doread(struct scd_softc *sc, int state, struct scd_mbx *mbxin)
708 struct scd_mbx *mbx = (state!=SCD_S_BEGIN) ? sc->ch_mbxsave : mbxin;
709 struct bio *bp = mbx->bp;
713 static char sdata[3]; /* Must be preserved between calls to this function */
715 SCD_ASSERT_LOCKED(sc);
719 mbx = sc->ch_mbxsave = mbxin;
723 mbx->count = RDELAY_WAIT;
725 process_attention(sc);
729 sc->ch_state = SCD_S_WAITSTAT;
730 callout_stop(&sc->timer);
731 if (mbx->count-- <= 0) {
732 device_printf(sc->dev, "timeout. drive busy.\n");
738 sc->ch_state = SCD_S_WAITSTAT;
739 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
743 process_attention(sc);
745 /* reject, if audio active */
746 if (sc->data.audio_status & CD_AS_PLAY_IN_PROGRESS) {
747 device_printf(sc->dev, "audio is active\n");
751 mbx->sz = sc->data.blksize;
753 /* for first block */
754 mbx->nblk = (bp->bio_bcount + (mbx->sz-1)) / mbx->sz;
758 if (!(sc->data.flags & SCDVALID))
761 blknum = bp->bio_offset / mbx->sz + mbx->skip/mbx->sz;
763 XDEBUG(sc, 2, "scd_doread: read blknum=%d\n", blknum);
765 /* build parameter block */
766 hsg2msf(blknum, sdata);
768 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
769 SCD_WRITE(sc, OREG_CONTROL, CBIT_RPARAM_CLEAR);
770 SCD_WRITE(sc, OREG_CONTROL, CBIT_DATA_READY_CLEAR);
772 if (FSTATUS_BIT(sc, FBIT_WPARAM_READY))
776 sc->ch_state = SCD_S_WAITFIFO;
777 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
781 sc->ch_state = SCD_S_WAITSPIN;
782 callout_stop(&sc->timer);
783 if (mbx->count-- <= 0) {
784 device_printf(sc->dev, "timeout waiting for drive to spin up.\n");
787 if (!STATUS_BIT(sc, SBIT_RESULT_READY)) {
788 sc->ch_state = SCD_S_WAITSPIN;
789 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
792 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
793 switch ((i = SCD_READ(sc, IREG_RESULT)) & 0xf0) {
795 i = SCD_READ(sc, IREG_RESULT);
799 (void)SCD_READ(sc, IREG_RESULT);
800 sc->data.flags |= SCDSPINNING;
803 XDEBUG(sc, 1, "DEBUG: spin up complete\n");
805 state = SCD_S_BEGIN1;
809 sc->ch_state = SCD_S_WAITFIFO;
810 callout_stop(&sc->timer);
811 if (mbx->count-- <= 0) {
812 device_printf(sc->dev, "timeout. write param not ready.\n");
815 if (!FSTATUS_BIT(sc, FBIT_WPARAM_READY)) {
816 sc->ch_state = SCD_S_WAITFIFO;
817 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
820 XDEBUG(sc, 1, "mbx->count (writeparamwait) = %d(%d)\n", mbx->count, 100);
823 /* The reason this test isn't done 'till now is to make sure */
824 /* that it is ok to send the SPIN_UP cmd below. */
825 if (!(sc->data.flags & SCDSPINNING)) {
826 XDEBUG(sc, 1, "spinning up drive ...\n");
827 SCD_WRITE(sc, OREG_COMMAND, CMD_SPIN_UP);
829 sc->ch_state = SCD_S_WAITSPIN;
830 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
834 /* send the read command */
835 SCD_WRITE(sc, OREG_WPARAMS, sdata[0]);
836 SCD_WRITE(sc, OREG_WPARAMS, sdata[1]);
837 SCD_WRITE(sc, OREG_WPARAMS, sdata[2]);
838 SCD_WRITE(sc, OREG_WPARAMS, 0);
839 SCD_WRITE(sc, OREG_WPARAMS, 0);
840 SCD_WRITE(sc, OREG_WPARAMS, 1);
841 SCD_WRITE(sc, OREG_COMMAND, CMD_READ);
843 mbx->count = RDELAY_WAITREAD;
844 for (i = 0; i < 50; i++) {
845 if (STATUS_BIT(sc, SBIT_DATA_READY))
850 sc->ch_state = SCD_S_WAITREAD;
851 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
855 sc->ch_state = SCD_S_WAITREAD;
856 callout_stop(&sc->timer);
857 if (mbx->count-- <= 0) {
858 if (STATUS_BIT(sc, SBIT_RESULT_READY))
860 device_printf(sc->dev, "timeout while reading data\n");
863 if (!STATUS_BIT(sc, SBIT_DATA_READY)) {
864 process_attention(sc);
865 if (!(sc->data.flags & SCDVALID))
867 sc->ch_state = SCD_S_WAITREAD;
868 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
871 XDEBUG(sc, 2, "mbx->count (after RDY_BIT) = %d(%d)\n", mbx->count, RDELAY_WAITREAD);
875 addr = bp->bio_data + mbx->skip;
876 SCD_WRITE(sc, OREG_CONTROL, CBIT_DATA_READY_CLEAR);
877 SCD_READ_MULTI(sc, IREG_DATA, addr, mbx->sz);
880 for (i = 0; i < 20; i++) {
881 if (STATUS_BIT(sc, SBIT_RESULT_READY))
887 case SCD_S_WAITPARAM:
888 sc->ch_state = SCD_S_WAITPARAM;
889 callout_stop(&sc->timer);
890 if (mbx->count-- <= 0) {
891 device_printf(sc->dev, "timeout waiting for params\n");
896 if (!STATUS_BIT(sc, SBIT_RESULT_READY)) {
897 sc->ch_state = SCD_S_WAITPARAM;
898 callout_reset(&sc->timer, hz / 100, scd_timeout, sc); /* XXX */
902 if (mbx->count < 100 && scd_debuglevel > 0)
903 device_printf(sc->dev, "mbx->count (paramwait) = %d(%d)\n", mbx->count, 100);
907 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
908 switch ((i = SCD_READ(sc, IREG_RESULT)) & 0xf0) {
911 case ERR_FATAL_READ_ERROR1:
912 case ERR_FATAL_READ_ERROR2:
913 device_printf(sc->dev, "unrecoverable read error 0x%x\n", i);
918 i = SCD_READ(sc, IREG_RESULT);
920 case ERR_NOT_SPINNING:
921 XDEBUG(sc, 1, "read error: drive not spinning\n");
922 if (mbx->retry-- > 0) {
923 state = SCD_S_BEGIN1;
924 sc->data.flags &= ~SCDSPINNING;
933 i = SCD_READ(sc, IREG_RESULT);
937 if (--mbx->nblk > 0) {
938 mbx->skip += mbx->sz;
946 sc->data.flags &= ~SCDMBXBSY;
952 if (mbx->retry-- > 0) {
953 device_printf(sc->dev, "retrying ...\n");
954 state = SCD_S_BEGIN1;
958 /* invalidate the buffer */
960 bp->bio_flags |= BIO_ERROR;
961 bp->bio_resid = bp->bio_bcount;
964 sc->data.flags &= ~SCDMBXBSY;
969 device_printf(sc->dev, "media changed\n");
974 hsg2msf(int hsg, bcd_t *msf)
978 M_msf(msf) = bin2bcd(hsg / 4500);
980 S_msf(msf) = bin2bcd(hsg / 75);
981 F_msf(msf) = bin2bcd(hsg % 75);
988 return (bcd2bin(M_msf(msf)) * 60 +
989 bcd2bin(S_msf(msf))) * 75 +
990 bcd2bin(F_msf(msf)) - 150;
994 process_attention(struct scd_softc *sc)
999 while (IS_ATTENTION(sc) && count++ < 30) {
1000 SCD_WRITE(sc, OREG_CONTROL, CBIT_ATTENTION_CLEAR);
1001 code = SCD_READ(sc, IREG_RESULT);
1004 if (scd_debuglevel > 0) {
1006 device_printf(sc->dev, "DEBUG: ATTENTIONS = 0x%x", code);
1008 printf(",0x%x", code);
1013 case ATTEN_SPIN_DOWN:
1014 sc->data.flags &= ~SCDSPINNING;
1017 case ATTEN_SPIN_UP_DONE:
1018 sc->data.flags |= SCDSPINNING;
1021 case ATTEN_AUDIO_DONE:
1022 sc->data.audio_status = CD_AS_PLAY_COMPLETED;
1025 case ATTEN_DRIVE_LOADED:
1026 sc->data.flags &= ~(SCDTOC|SCDSPINNING|SCDVALID);
1027 sc->data.audio_status = CD_AS_AUDIO_INVALID;
1030 case ATTEN_EJECT_PUSHED:
1031 sc->data.flags &= ~SCDVALID;
1037 if (scd_debuglevel > 0 && count > 0)
1042 /* Returns 0 OR sony error code */
1044 spin_up(struct scd_softc *sc)
1046 unsigned char res_reg[12];
1047 unsigned int res_size;
1052 rc = send_cmd(sc, CMD_SPIN_UP, 0, 0, res_reg, &res_size);
1054 XDEBUG(sc, 2, "CMD_SPIN_UP error 0x%x\n", rc);
1058 if (!(sc->data.flags & SCDTOC)) {
1059 rc = send_cmd(sc, CMD_READ_TOC, 0);
1060 if (rc == ERR_NOT_SPINNING) {
1061 if (loop_count++ < 3)
1069 sc->data.flags |= SCDSPINNING;
1074 static struct sony_tracklist *
1075 get_tl(struct sony_toc *toc, int size)
1077 struct sony_tracklist *tl = &toc->tracks[0];
1079 if (tl->track != 0xb0)
1081 if (tl->track != 0xb1)
1083 tl = (struct sony_tracklist *)((char *)tl + 9);
1084 if (tl->track != 0xb2)
1086 tl = (struct sony_tracklist *)((char *)tl + 9);
1087 if (tl->track != 0xb3)
1089 tl = (struct sony_tracklist *)((char *)tl + 9);
1090 if (tl->track != 0xb4)
1092 tl = (struct sony_tracklist *)((char *)tl + 9);
1093 if (tl->track != 0xc0)
1095 tl = (struct sony_tracklist *)((char *)tl + 9);
1100 read_toc(struct scd_softc *sc)
1102 struct sony_toc toc;
1103 struct sony_tracklist *tl;
1107 rc = send_cmd(sc, CMD_GET_TOC, 1, 1);
1110 if (rc > sizeof(toc)) {
1111 device_printf(sc->dev, "program error: toc too large (%d)\n", rc);
1114 if (get_result(sc, rc, (u_char *)&toc) != 0)
1117 XDEBUG(sc, 1, "toc read. len = %d, sizeof(toc) = %d\n", rc, sizeof(toc));
1119 tl = get_tl(&toc, rc);
1120 first = msf2hsg(tl->start_msf);
1121 last = msf2hsg(toc.lead_out_start_msf);
1122 sc->data.blksize = SCDBLKSIZE;
1123 sc->data.disksize = last*sc->data.blksize/DEV_BSIZE;
1125 XDEBUG(sc, 1, "firstsector = %ld, lastsector = %ld", first, last);
1127 sc->data.first_track = bcd2bin(toc.first_track);
1128 sc->data.last_track = bcd2bin(toc.last_track);
1129 if (sc->data.last_track > (MAX_TRACKS-2))
1130 sc->data.last_track = MAX_TRACKS-2;
1131 for (j = 0, i = sc->data.first_track; i <= sc->data.last_track; i++, j++) {
1132 sc->data.toc[i].adr = tl[j].adr;
1133 sc->data.toc[i].ctl = tl[j].ctl; /* for xcdplayer */
1134 bcopy(tl[j].start_msf, sc->data.toc[i].start_msf, 3);
1136 if (scd_debuglevel > 0) {
1139 device_printf(sc->dev, "tracks ");
1141 printf("[%03d: %2d %2d %2d] ", i,
1142 bcd2bin(sc->data.toc[i].start_msf[0]),
1143 bcd2bin(sc->data.toc[i].start_msf[1]),
1144 bcd2bin(sc->data.toc[i].start_msf[2]));
1148 bcopy(toc.lead_out_start_msf, sc->data.toc[sc->data.last_track+1].start_msf, 3);
1150 if (scd_debuglevel > 0) {
1151 i = sc->data.last_track+1;
1152 printf("[END: %2d %2d %2d]\n",
1153 bcd2bin(sc->data.toc[i].start_msf[0]),
1154 bcd2bin(sc->data.toc[i].start_msf[1]),
1155 bcd2bin(sc->data.toc[i].start_msf[2]));
1159 sc->data.flags |= SCDTOC;
1165 init_drive(struct scd_softc *sc)
1169 rc = send_cmd(sc, CMD_SET_DRIVE_PARAM, 2,
1170 0x05, 0x03 | ((sc->data.double_speed) ? 0x04: 0));
1172 device_printf(sc->dev, "Unable to set parameters. Errcode = 0x%x\n", rc);
1175 /* Returns 0 or errno */
1177 get_result(struct scd_softc *sc, int result_len, u_char *result)
1179 int loop_index = 2; /* send_cmd() reads two bytes ... */
1181 XDEBUG(sc, 1, "DEBUG: get_result: bytes=%d\n", result_len);
1183 while (result_len-- > 0) {
1184 if (loop_index++ >= 10) {
1186 if (waitfor_status_bits(sc, SBIT_RESULT_READY, 0))
1188 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
1191 *result++ = SCD_READ(sc, IREG_RESULT);
1193 (void)SCD_READ(sc, IREG_RESULT);
1198 /* Returns -0x100 for timeout, -(drive error code) OR number of result bytes */
1200 send_cmd(struct scd_softc *sc, u_char cmd, u_int nargs, ...)
1207 if (waitfor_status_bits(sc, 0, SBIT_BUSY)) {
1208 device_printf(sc->dev, "drive busy\n");
1212 XDEBUG(sc, 1, "DEBUG: send_cmd: cmd=0x%x nargs=%d", cmd, nargs);
1214 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
1215 SCD_WRITE(sc, OREG_CONTROL, CBIT_RPARAM_CLEAR);
1217 for (i = 0; i < 100; i++)
1218 if (FSTATUS_BIT(sc, FBIT_WPARAM_READY))
1220 if (!FSTATUS_BIT(sc, FBIT_WPARAM_READY)) {
1221 XDEBUG(sc, 1, "\nwparam timeout\n");
1225 va_start(ap, nargs);
1226 for (i = 0; i < nargs; i++) {
1227 c = (u_char)va_arg(ap, int);
1228 SCD_WRITE(sc, OREG_WPARAMS, c);
1229 XDEBUG(sc, 1, ",{0x%x}", c);
1232 XDEBUG(sc, 1, "\n");
1234 SCD_WRITE(sc, OREG_COMMAND, cmd);
1236 rc = waitfor_status_bits(sc, SBIT_RESULT_READY, SBIT_BUSY);
1240 SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
1241 switch ((rc = SCD_READ(sc, IREG_RESULT)) & 0xf0) {
1243 rc = SCD_READ(sc, IREG_RESULT);
1246 XDEBUG(sc, 1, "DEBUG: send_cmd: drive_error=0x%x\n", rc);
1250 rc = SCD_READ(sc, IREG_RESULT);
1251 XDEBUG(sc, 1, "DEBUG: send_cmd: result_len=%d\n", rc);
1257 print_error(struct scd_softc *sc, int errcode)
1261 case -ERR_CD_NOT_LOADED:
1262 device_printf(sc->dev, "door is open\n");
1264 case -ERR_NO_CD_INSIDE:
1265 device_printf(sc->dev, "no cd inside\n");
1268 if (errcode == -0x100 || errcode > 0)
1269 device_printf(sc->dev, "device timeout\n");
1271 device_printf(sc->dev, "unexpected error 0x%x\n", -errcode);
1276 /* Returns 0 or errno value */
1278 waitfor_status_bits(struct scd_softc *sc, int bits_set, int bits_clear)
1280 u_int flags = sc->data.flags;
1284 if (flags & SCDPROBING) {
1286 while (max_loop++ < 1000) {
1287 c = SCD_READ(sc, IREG_STATUS);
1290 if (c & SBIT_ATTENTION) {
1291 process_attention(sc);
1294 if ((c & bits_set) == bits_set &&
1295 (c & bits_clear) == 0)
1303 while (max_loop-- > 0) {
1304 c = SCD_READ(sc, IREG_STATUS);
1305 if (c & SBIT_ATTENTION) {
1306 process_attention(sc);
1309 if ((c & bits_set) == bits_set &&
1310 (c & bits_clear) == 0)
1315 pause("waitfor", hz/10);
1319 if ((c & bits_set) == bits_set &&
1320 (c & bits_clear) == 0)
1325 if (scd_debuglevel > 0)
1326 device_printf(sc->dev, "DEBUG: waitfor: TIMEOUT (0x%x,(0x%x,0x%x))\n", c, bits_set, bits_clear);
1329 device_printf(sc->dev, "timeout.\n");
1333 /* these two routines for xcdplayer - "borrowed" from mcd.c */
1335 scd_toc_header (struct scd_softc *sc, struct ioc_toc_header* th)
1339 if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) {
1340 print_error(sc, rc);
1344 th->starting_track = sc->data.first_track;
1345 th->ending_track = sc->data.last_track;
1346 th->len = 0; /* not used */
1352 scd_toc_entrys (struct scd_softc *sc, struct ioc_read_toc_entry *te)
1354 struct cd_toc_entry toc_entry;
1355 int rc, i, len = te->data_len;
1357 if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) {
1358 print_error(sc, rc);
1362 /* find the toc to copy*/
1363 i = te->starting_track;
1364 if (i == SCD_LASTPLUS1)
1365 i = sc->data.last_track + 1;
1367 /* verify starting track */
1368 if (i < sc->data.first_track || i > sc->data.last_track+1)
1371 /* valid length ? */
1372 if (len < sizeof(struct cd_toc_entry)
1373 || (len % sizeof(struct cd_toc_entry)) != 0)
1376 /* copy the toc data */
1377 toc_entry.control = sc->data.toc[i].ctl;
1378 toc_entry.addr_type = te->address_format;
1379 toc_entry.track = i;
1380 if (te->address_format == CD_MSF_FORMAT) {
1381 toc_entry.addr.msf.unused = 0;
1382 toc_entry.addr.msf.minute = bcd2bin(sc->data.toc[i].start_msf[0]);
1383 toc_entry.addr.msf.second = bcd2bin(sc->data.toc[i].start_msf[1]);
1384 toc_entry.addr.msf.frame = bcd2bin(sc->data.toc[i].start_msf[2]);
1388 /* copy the data back */
1389 if (copyout(&toc_entry, te->data, sizeof(struct cd_toc_entry)) != 0)
1397 scd_toc_entry (struct scd_softc *sc, struct ioc_read_toc_single_entry *te)
1399 struct cd_toc_entry toc_entry;
1402 if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) {
1403 print_error(sc, rc);
1407 /* find the toc to copy*/
1409 if (i == SCD_LASTPLUS1)
1410 i = sc->data.last_track + 1;
1412 /* verify starting track */
1413 if (i < sc->data.first_track || i > sc->data.last_track+1)
1416 /* copy the toc data */
1417 toc_entry.control = sc->data.toc[i].ctl;
1418 toc_entry.addr_type = te->address_format;
1419 toc_entry.track = i;
1420 if (te->address_format == CD_MSF_FORMAT) {
1421 toc_entry.addr.msf.unused = 0;
1422 toc_entry.addr.msf.minute = bcd2bin(sc->data.toc[i].start_msf[0]);
1423 toc_entry.addr.msf.second = bcd2bin(sc->data.toc[i].start_msf[1]);
1424 toc_entry.addr.msf.frame = bcd2bin(sc->data.toc[i].start_msf[2]);
1427 /* copy the data back */
1428 bcopy(&toc_entry, &te->entry, sizeof(struct cd_toc_entry));