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 withough 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.
44 /* $FreeBSD: src/sys/i386/isa/scd.c,v 1.54 2000/01/29 16:00:30 peter Exp $ */
45 /* $DragonFly: src/sys/dev/disk/scd/Attic/scd.c,v 1.13 2005/06/06 21:48:16 eirikn Exp $ */
47 /* Please send any comments to micke@dynas.se */
52 #include <sys/param.h>
53 #include <sys/systm.h>
57 #include <sys/disklabel.h>
58 #include <sys/kernel.h>
60 #include <sys/thread2.h>
62 #include <machine/clock.h>
63 #include <machine/stdarg.h>
65 #include <bus/isa/i386/isa_device.h>
69 #define scd_part(dev) ((minor(dev)) & 7)
70 #define scd_unit(dev) (((minor(dev)) & 0x38) >> 3)
71 #define scd_phys(dev) (((minor(dev)) & 0x40) >> 6)
75 #define SCDOPEN 0x0001 /* device opened */
76 #define SCDVALID 0x0002 /* parameters loaded */
77 #define SCDINIT 0x0004 /* device is init'd */
78 #define SCDPROBING 0x0020 /* probing */
79 #define SCDTOC 0x0100 /* already read toc */
80 #define SCDMBXBSY 0x0200 /* local mbx is busy */
81 #define SCDSPINNING 0x0400 /* drive is spun up */
84 #define SCD_S_BEGIN1 1
85 #define SCD_S_WAITSTAT 2
86 #define SCD_S_WAITFIFO 3
87 #define SCD_S_WAITSPIN 4
88 #define SCD_S_WAITREAD 5
89 #define SCD_S_WAITPARAM 6
91 #define RDELAY_WAIT 300
92 #define RDELAY_WAITREAD 300
94 #define SCDBLKSIZE 2048
97 static int scd_debuglevel = SCD_DEBUG;
98 # define XDEBUG(level, data) {if (scd_debuglevel >= level) printf data;}
100 # define XDEBUG(level, data)
115 static struct scd_data {
122 struct disklabel dlabel;
126 unsigned int ctl :4; /* xcdplayer needs this */
127 unsigned char start_msf[3];
131 struct ioc_play_msf last_play;
134 struct buf_queue_head head; /* head of buf queue */
136 struct callout callout;
140 static void hsg2msf(int hsg, bcd_t *msf);
141 static int msf2hsg(bcd_t *msf);
143 static void process_attention(unsigned unit);
144 static __inline void write_control(unsigned port, unsigned data);
145 static int waitfor_status_bits(int unit, int bits_set, int bits_clear);
146 static int send_cmd(u_int unit, u_char cmd, u_int nargs, ...);
147 static void init_drive(unsigned unit);
148 static int spin_up(unsigned unit);
149 static int read_toc(unsigned unit);
150 static int get_result(u_int unit, int result_len, u_char *result);
151 static void print_error(int unit, int errcode);
153 static void scd_start(int unit);
154 static timeout_t scd_timeout;
155 static void scd_doread(int state, struct scd_mbx *mbxin);
157 static int scd_eject(int unit);
158 static int scd_stop(int unit);
159 static int scd_pause(int unit);
160 static int scd_resume(int unit);
161 static int scd_playtracks(int unit, struct ioc_play_track *pt);
162 static int scd_playmsf(int unit, struct ioc_play_msf *msf);
163 static int scd_play(int unit, struct ioc_play_msf *msf);
164 static int scd_subchan(int unit, struct ioc_read_subchannel *sc);
165 static int read_subcode(int unit, struct sony_subchannel_position_data *sc);
168 static int scd_toc_header(int unit, struct ioc_toc_header *th);
169 static int scd_toc_entrys(int unit, struct ioc_read_toc_entry *te);
170 static int scd_toc_entry(int unit, struct ioc_read_toc_single_entry *te);
171 #define SCD_LASTPLUS1 170 /* don't ask, xcdplayer passes this in */
173 static int scd_probe(struct isa_device *dev);
174 static int scd_attach(struct isa_device *dev);
175 struct isa_driver scddriver = { scd_probe, scd_attach, "scd" };
177 /* For canceling our timeout */
179 static d_open_t scdopen;
180 static d_close_t scdclose;
181 static d_ioctl_t scdioctl;
182 static d_strategy_t scdstrategy;
184 #define CDEV_MAJOR 45
185 static struct cdevsw scd_cdevsw = {
187 /* maj */ CDEV_MAJOR,
193 /* close */ scdclose,
196 /* ioctl */ scdioctl,
199 /* strategy */ scdstrategy,
206 scd_attach(struct isa_device *dev)
208 int unit = dev->id_unit;
209 struct scd_data *cd = scd_data + unit;
211 cd->iobase = dev->id_iobase; /* Already set by probe, but ... */
213 /* name filled in probe */
214 printf("scd%d: <%s>\n", dev->id_unit, scd_data[dev->id_unit].name);
216 init_drive(dev->id_unit);
219 cd->audio_status = CD_AS_AUDIO_INVALID;
220 bufq_init(&cd->head);
222 cdevsw_add(&scd_cdevsw, dkunitmask(), dkmakeunit(unit));
223 make_dev(&scd_cdevsw, dkmakeminor(unit, 0, 0),
224 UID_ROOT, GID_OPERATOR, 0640, "rscd%da", unit);
225 make_dev(&scd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
226 UID_ROOT, GID_OPERATOR, 0640, "rscd%dc", unit);
227 make_dev(&scd_cdevsw, dkmakeminor(unit, 0, 0),
228 UID_ROOT, GID_OPERATOR, 0640, "scd%da", unit);
229 make_dev(&scd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
230 UID_ROOT, GID_OPERATOR, 0640, "scd%dc", unit);
235 scdopen(dev_t dev, int flags, int fmt, struct thread *td)
241 unit = scd_unit(dev);
245 cd = scd_data + unit;
246 part = scd_part(dev);
247 phys = scd_phys(dev);
250 if (!(cd->flags & SCDINIT))
253 /* invalidated in the meantime? mark all open part's invalid */
257 XDEBUG(1,("scd%d: DEBUG: status = 0x%x\n", unit, inb(cd->iobase+IREG_STATUS)));
259 if ((rc = spin_up(unit)) != 0) {
260 print_error(unit, rc);
263 if (!(cd->flags & SCDTOC)) {
266 while (loop_count-- > 0 && (rc = read_toc(unit)) != 0) {
267 if (rc == ERR_NOT_SPINNING) {
270 print_error(unit, rc);\
275 printf("scd%d: TOC read error 0x%x\n", unit, rc);
280 dev->si_bsize_phys = cd->blksize;
283 cd->flags |= SCDVALID;
289 scdclose(dev_t dev, int flags, int fmt, struct thread *td)
294 unit = scd_unit(dev);
298 cd = scd_data + unit;
299 part = scd_part(dev);
300 phys = scd_phys(dev);
302 if (!(cd->flags & SCDINIT) || !cd->openflag)
305 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS) {
306 (void)send_cmd(unit, CMD_SPIN_DOWN, 0);
307 cd->flags &= ~SCDSPINNING;
318 scdstrategy(struct buf *bp)
321 int unit = scd_unit(bp->b_dev);
323 cd = scd_data + unit;
325 XDEBUG(2, ("scd%d: DEBUG: strategy: block=%ld, bcount=%ld\n",
326 unit, (long)bp->b_blkno, bp->b_bcount));
328 if (unit >= NSCD || bp->b_blkno < 0 || (bp->b_bcount % SCDBLKSIZE)) {
329 printf("scd%d: strategy failure: blkno = %ld, bcount = %ld\n",
330 unit, (long)bp->b_blkno, bp->b_bcount);
331 bp->b_error = EINVAL;
332 bp->b_flags |= B_ERROR;
336 /* if device invalidated (e.g. media change, door open), error */
337 if (!(cd->flags & SCDVALID)) {
338 printf("scd%d: media changed\n", unit);
344 if (!(bp->b_flags & B_READ)) {
349 /* no data to read */
350 if (bp->b_bcount == 0)
353 if (!(cd->flags & SCDTOC)) {
357 /* adjust transfer if necessary */
358 if (bounds_check_with_label(bp,&cd->dlabel,1) <= 0)
361 bp->b_pblkno = bp->b_blkno;
366 bufqdisksort(&cd->head, bp);
369 /* now check whether we can perform processing */
374 bp->b_flags |= B_ERROR;
376 bp->b_resid = bp->b_bcount;
384 struct scd_data *cd = scd_data + unit;
389 if (cd->flags & SCDMBXBSY) {
394 bp = bufq_first(&cd->head);
396 /* block found to process, dequeue */
397 bufq_remove(&cd->head, bp);
398 cd->flags |= SCDMBXBSY;
405 p = cd->dlabel.d_partitions + scd_part(bp->b_dev);
408 cd->mbx.port = cd->iobase;
411 cd->mbx.p_offset = p->p_offset;
414 scd_doread(SCD_S_BEGIN,&(cd->mbx));
419 scdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
424 unit = scd_unit(dev);
425 part = scd_part(dev);
426 cd = scd_data + unit;
428 XDEBUG(1, ("scd%d: ioctl: cmd=0x%lx\n", unit, cmd));
430 if (!(cd->flags & SCDVALID))
435 *(struct disklabel *)addr = cd->dlabel;
438 ((struct partinfo *)addr)->disklab = &cd->dlabel;
439 ((struct partinfo *)addr)->part =
440 &cd->dlabel.d_partitions[0];
442 case CDIOCPLAYTRACKS:
443 return scd_playtracks(unit, (struct ioc_play_track *) addr);
444 case CDIOCPLAYBLOCKS:
447 return scd_playmsf(unit, (struct ioc_play_msf *) addr);
448 case CDIOCREADSUBCHANNEL:
449 return scd_subchan(unit, (struct ioc_read_subchannel *) addr);
450 case CDIOREADTOCHEADER:
451 return scd_toc_header (unit, (struct ioc_toc_header *) addr);
452 case CDIOREADTOCENTRYS:
453 return scd_toc_entrys (unit, (struct ioc_read_toc_entry*) addr);
454 case CDIOREADTOCENTRY:
455 return scd_toc_entry (unit, (struct ioc_read_toc_single_entry*) addr);
466 return scd_resume(unit);
468 return scd_pause(unit);
472 return scd_stop(unit);
474 return scd_eject(unit);
489 printf("scd%d: unsupported ioctl (cmd=0x%lx)\n", unit, cmd);
494 /***************************************************************
495 * lower level of driver starts here
496 **************************************************************/
499 scd_playtracks(int unit, struct ioc_play_track *pt)
501 struct scd_data *cd = scd_data + unit;
502 struct ioc_play_msf msf;
503 int a = pt->start_track;
504 int z = pt->end_track;
507 if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
508 if (rc == -ERR_NOT_SPINNING) {
509 if (spin_up(unit) != 0)
514 print_error(unit, rc);
519 XDEBUG(1, ("scd%d: playtracks from %d:%d to %d:%d\n", unit,
520 a, pt->start_index, z, pt->end_index));
522 if ( a < cd->first_track
523 || a > cd->last_track
525 || z > cd->last_track)
528 bcopy(cd->toc[a].start_msf, &msf.start_m, 3);
529 hsg2msf(msf2hsg(cd->toc[z+1].start_msf)-1, &msf.end_m);
531 return scd_play(unit, &msf);
534 /* The start/end msf is expected to be in bin format */
536 scd_playmsf(int unit, struct ioc_play_msf *msfin)
538 struct ioc_play_msf msf;
540 msf.start_m = bin2bcd(msfin->start_m);
541 msf.start_s = bin2bcd(msfin->start_s);
542 msf.start_f = bin2bcd(msfin->start_f);
543 msf.end_m = bin2bcd(msfin->end_m);
544 msf.end_s = bin2bcd(msfin->end_s);
545 msf.end_f = bin2bcd(msfin->end_f);
547 return scd_play(unit, &msf);
550 /* The start/end msf is expected to be in bcd format */
552 scd_play(int unit, struct ioc_play_msf *msf)
554 struct scd_data *cd = scd_data + unit;
557 XDEBUG(1, ("scd%d: playing: %02x:%02x:%02x -> %02x:%02x:%02x\n", unit,
558 msf->start_m, msf->start_s, msf->start_f,
559 msf->end_m, msf->end_s, msf->end_f));
561 for (i = 0; i < 2; i++) {
562 rc = send_cmd(unit, CMD_PLAY_AUDIO, 7,
564 msf->start_m, msf->start_s, msf->start_f,
565 msf->end_m, msf->end_s, msf->end_f);
566 if (rc == -ERR_NOT_SPINNING) {
567 cd->flags &= ~SCDSPINNING;
568 if (spin_up(unit) != 0)
571 print_error(unit, rc);
577 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
578 bcopy((char *)msf, (char *)&cd->last_play, sizeof(struct ioc_play_msf));
585 struct scd_data *cd = scd_data + unit;
587 (void)send_cmd(unit, CMD_STOP_AUDIO, 0);
588 cd->audio_status = CD_AS_PLAY_COMPLETED;
595 struct scd_data *cd = scd_data + unit;
596 struct sony_subchannel_position_data subpos;
598 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS)
601 if (read_subcode(unit, &subpos) != 0)
604 if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0)
607 cd->last_play.start_m = subpos.abs_msf[0];
608 cd->last_play.start_s = subpos.abs_msf[1];
609 cd->last_play.start_f = subpos.abs_msf[2];
610 cd->audio_status = CD_AS_PLAY_PAUSED;
612 XDEBUG(1, ("scd%d: pause @ %02x:%02x:%02x\n", unit,
613 cd->last_play.start_m,
614 cd->last_play.start_s,
615 cd->last_play.start_f));
623 if (scd_data[unit].audio_status != CD_AS_PLAY_PAUSED)
625 return scd_play(unit, &scd_data[unit].last_play);
631 struct scd_data *cd = scd_data + unit;
633 cd->audio_status = CD_AS_AUDIO_INVALID;
634 cd->flags &= ~(SCDSPINNING|SCDTOC);
636 if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0 ||
637 send_cmd(unit, CMD_SPIN_DOWN, 0) != 0 ||
638 send_cmd(unit, CMD_EJECT, 0) != 0)
646 scd_subchan(int unit, struct ioc_read_subchannel *sc)
648 struct scd_data *cd = scd_data + unit;
649 struct sony_subchannel_position_data q;
650 struct cd_sub_channel_info data;
652 XDEBUG(1, ("scd%d: subchan af=%d, df=%d\n", unit,
656 if (sc->address_format != CD_MSF_FORMAT)
659 if (sc->data_format != CD_CURRENT_POSITION)
662 if (read_subcode(unit, &q) != 0)
665 data.header.audio_status = cd->audio_status;
666 data.what.position.data_format = CD_MSF_FORMAT;
667 data.what.position.track_number = bcd2bin(q.track_number);
668 data.what.position.reladdr.msf.unused = 0;
669 data.what.position.reladdr.msf.minute = bcd2bin(q.rel_msf[0]);
670 data.what.position.reladdr.msf.second = bcd2bin(q.rel_msf[1]);
671 data.what.position.reladdr.msf.frame = bcd2bin(q.rel_msf[2]);
672 data.what.position.absaddr.msf.unused = 0;
673 data.what.position.absaddr.msf.minute = bcd2bin(q.abs_msf[0]);
674 data.what.position.absaddr.msf.second = bcd2bin(q.abs_msf[1]);
675 data.what.position.absaddr.msf.frame = bcd2bin(q.abs_msf[2]);
677 if (copyout(&data, sc->data, min(sizeof(struct cd_sub_channel_info), sc->data_len))!=0)
683 write_control(unsigned port, unsigned data)
685 outb(port + OREG_CONTROL, data);
689 scd_probe(struct isa_device *dev)
691 struct sony_drive_configuration drive_config;
692 int unit = dev->id_unit;
694 static char namebuf[8+16+8+3];
698 scd_data[unit].flags = SCDPROBING;
699 scd_data[unit].iobase = dev->id_iobase;
701 bzero(&drive_config, sizeof(drive_config));
705 write_control(dev->id_iobase, CBIT_RESET_DRIVE);
710 /* Only the ATTENTION bit may be set */
711 if ((inb(dev->id_iobase+IREG_STATUS) & ~1) != 0) {
712 XDEBUG(1, ("scd: too many bits set. probe failed.\n"));
715 rc = send_cmd(unit, CMD_GET_DRIVE_CONFIG, 0);
716 if (rc != sizeof(drive_config)) {
717 /* Sometimes if the drive is playing audio I get */
718 /* the bad result 82. Fix by repeating the reset */
719 if (rc > 0 && loop_count++ == 0)
723 if (get_result(unit, rc, (u_char *)&drive_config) != 0)
726 bcopy(drive_config.vendor, namebuf, 8);
728 while (*(s-1) == ' ') /* Strip trailing spaces */
731 bcopy(drive_config.product, s, 16);
733 while (*(s-1) == ' ')
736 bcopy(drive_config.revision, s, 8);
738 while (*(s-1) == ' ')
742 scd_data[unit].name = namebuf;
744 if (drive_config.config & 0x10)
745 scd_data[unit].double_speed = 1;
747 scd_data[unit].double_speed = 0;
753 read_subcode(int unit, struct sony_subchannel_position_data *sc)
757 rc = send_cmd(unit, CMD_GET_SUBCHANNEL_DATA, 0);
758 if (rc < 0 || rc < sizeof(*sc))
760 if (get_result(unit, rc, (u_char *)sc) != 0)
765 /* State machine copied from mcd.c */
767 /* This (and the code in mcd.c) will not work with more than one drive */
768 /* because there is only one mbxsave below. Should fix that some day. */
769 /* (mbxsave & state should probably be included in the scd_data struct and */
770 /* the unit number used as first argument to scd_doread().) /Micke */
772 /* state machine to process read requests
773 * initialize with SCD_S_BEGIN: reset state machine
774 * SCD_S_WAITSTAT: wait for ready (!busy)
775 * SCD_S_WAITSPIN: wait for drive to spin up (if not spinning)
776 * SCD_S_WAITFIFO: wait for param fifo to get ready, them exec. command.
777 * SCD_S_WAITREAD: wait for data ready, read data
778 * SCD_S_WAITPARAM: wait for command result params, read them, error if bad data read.
781 static struct scd_mbx *mbxsave;
784 scd_timeout(void *arg)
786 scd_doread((int)arg, mbxsave);
790 scd_doread(int state, struct scd_mbx *mbxin)
792 struct scd_mbx *mbx = (state!=SCD_S_BEGIN) ? mbxsave : mbxin;
793 int unit = mbx->unit;
794 int port = mbx->port;
795 struct buf *bp = mbx->bp;
796 struct scd_data *cd = scd_data + unit;
800 static char sdata[3]; /* Must be preserved between calls to this function */
805 mbx = mbxsave = mbxin;
809 mbx->count = RDELAY_WAIT;
811 process_attention(unit);
815 callout_stop(&cd->callout);
816 if (mbx->count-- <= 0) {
817 printf("scd%d: timeout. drive busy.\n",unit);
823 callout_reset(&cd->callout, hz / 100,
824 scd_timeout, (void *)SCD_S_WAITSTAT);
828 process_attention(unit);
830 /* reject, if audio active */
831 if (cd->audio_status & CD_AS_PLAY_IN_PROGRESS) {
832 printf("scd%d: audio is active\n",unit);
836 mbx->sz = cd->blksize;
838 /* for first block */
839 mbx->nblk = (bp->b_bcount + (mbx->sz-1)) / mbx->sz;
843 if (!(cd->flags & SCDVALID))
846 blknum = (bp->b_blkno / (mbx->sz/DEV_BSIZE))
847 + mbx->p_offset + mbx->skip/mbx->sz;
849 XDEBUG(2, ("scd%d: scd_doread: read blknum=%d\n", unit, blknum));
851 /* build parameter block */
852 hsg2msf(blknum, sdata);
854 write_control(port, CBIT_RESULT_READY_CLEAR);
855 write_control(port, CBIT_RPARAM_CLEAR);
856 write_control(port, CBIT_DATA_READY_CLEAR);
858 if (FSTATUS_BIT(port, FBIT_WPARAM_READY))
862 callout_reset(&cd->callout, hz / 100,
863 scd_timeout, (void *)SCD_S_WAITFIFO);
867 callout_stop(&cd->callout);
868 if (mbx->count-- <= 0) {
869 printf("scd%d: timeout waiting for drive to spin up.\n", unit);
872 if (!STATUS_BIT(port, SBIT_RESULT_READY)) {
873 callout_reset(&cd->callout, hz / 100,
874 scd_timeout, (void *)SCD_S_WAITSPIN);
877 write_control(port, CBIT_RESULT_READY_CLEAR);
878 switch ((i = inb(port+IREG_RESULT)) & 0xf0) {
880 i = inb(port+IREG_RESULT);
881 print_error(unit, i);
884 (void)inb(port+IREG_RESULT);
885 cd->flags |= SCDSPINNING;
888 XDEBUG(1, ("scd%d: DEBUG: spin up complete\n", unit));
890 state = SCD_S_BEGIN1;
894 callout_stop(&cd->callout);
895 if (mbx->count-- <= 0) {
896 printf("scd%d: timeout. write param not ready.\n",unit);
899 if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) {
900 callout_reset(&cd->callout, hz / 100,
901 scd_timeout, (void *)SCD_S_WAITFIFO);
904 XDEBUG(1, ("scd%d: mbx->count (writeparamwait) = %d(%d)\n", unit, mbx->count, 100));
907 /* The reason this test isn't done 'till now is to make sure */
908 /* that it is ok to send the SPIN_UP cmd below. */
909 if (!(cd->flags & SCDSPINNING)) {
910 XDEBUG(1, ("scd%d: spinning up drive ...\n", unit));
911 outb(port+OREG_COMMAND, CMD_SPIN_UP);
913 callout_reset(&cd->callout, hz / 100,
914 scd_timeout, (void *)SCD_S_WAITSPIN);
918 reg = port + OREG_WPARAMS;
919 /* send the read command */
927 outb(port+OREG_COMMAND, CMD_READ);
930 mbx->count = RDELAY_WAITREAD;
931 for (i = 0; i < 50; i++) {
932 if (STATUS_BIT(port, SBIT_DATA_READY))
937 callout_reset(&cd->callout, hz / 100,
938 scd_timeout, (void *)SCD_S_WAITREAD);
942 callout_stop(&cd->callout);
943 if (mbx->count-- <= 0) {
944 if (STATUS_BIT(port, SBIT_RESULT_READY))
946 printf("scd%d: timeout while reading data\n",unit);
949 if (!STATUS_BIT(port, SBIT_DATA_READY)) {
950 process_attention(unit);
951 if (!(cd->flags & SCDVALID))
953 callout_reset(&cd->callout, hz / 100,
954 scd_timeout, (void *)SCD_S_WAITREAD);
957 XDEBUG(2, ("scd%d: mbx->count (after RDY_BIT) = %d(%d)\n", unit, mbx->count, RDELAY_WAITREAD));
961 addr = bp->b_data + mbx->skip;
962 write_control(port, CBIT_DATA_READY_CLEAR);
963 insb(port+IREG_DATA, addr, mbx->sz);
966 for (i = 0; i < 20; i++) {
967 if (STATUS_BIT(port, SBIT_RESULT_READY))
973 case SCD_S_WAITPARAM:
974 callout_stop(&cd->callout);
975 if (mbx->count-- <= 0) {
976 printf("scd%d: timeout waiting for params\n",unit);
981 if (!STATUS_BIT(port, SBIT_RESULT_READY)) {
982 callout_reset(&cd->callout, hz / 100,
983 scd_timeout, (void *)SCD_S_WAITPARAM);
987 if (mbx->count < 100 && scd_debuglevel > 0)
988 printf("scd%d: mbx->count (paramwait) = %d(%d)\n", unit, mbx->count, 100);
992 write_control(port, CBIT_RESULT_READY_CLEAR);
993 switch ((i = inb(port+IREG_RESULT)) & 0xf0) {
996 case ERR_FATAL_READ_ERROR1:
997 case ERR_FATAL_READ_ERROR2:
998 printf("scd%d: unrecoverable read error 0x%x\n", unit, i);
1003 i = inb(port+IREG_RESULT);
1005 case ERR_NOT_SPINNING:
1006 XDEBUG(1, ("scd%d: read error: drive not spinning\n", unit));
1007 if (mbx->retry-- > 0) {
1008 state = SCD_S_BEGIN1;
1009 cd->flags &= ~SCDSPINNING;
1014 print_error(unit, i);
1018 i = inb(port+IREG_RESULT);
1022 if (--mbx->nblk > 0) {
1023 mbx->skip += mbx->sz;
1031 cd->flags &= ~SCDMBXBSY;
1032 scd_start(mbx->unit);
1037 if (mbx->retry-- > 0) {
1038 printf("scd%d: retrying ...\n",unit);
1039 state = SCD_S_BEGIN1;
1043 /* invalidate the buffer */
1045 bp->b_flags |= B_ERROR;
1046 bp->b_resid = bp->b_bcount;
1049 cd->flags &= ~SCDMBXBSY;
1050 scd_start(mbx->unit);
1054 printf("scd%d: media changed\n", unit);
1059 hsg2msf(int hsg, bcd_t *msf)
1062 M_msf(msf) = bin2bcd(hsg / 4500);
1064 S_msf(msf) = bin2bcd(hsg / 75);
1065 F_msf(msf) = bin2bcd(hsg % 75);
1071 return (bcd2bin(M_msf(msf)) * 60 +
1072 bcd2bin(S_msf(msf))) * 75 +
1073 bcd2bin(F_msf(msf)) - 150;
1077 process_attention(unsigned unit)
1079 unsigned port = scd_data[unit].iobase;
1083 while (IS_ATTENTION(port) && count++ < 30) {
1084 write_control(port, CBIT_ATTENTION_CLEAR);
1085 code = inb(port+IREG_RESULT);
1088 if (scd_debuglevel > 0) {
1090 printf("scd%d: DEBUG: ATTENTIONS = 0x%x", unit, code);
1092 printf(",0x%x", code);
1097 case ATTEN_SPIN_DOWN:
1098 scd_data[unit].flags &= ~SCDSPINNING;
1101 case ATTEN_SPIN_UP_DONE:
1102 scd_data[unit].flags |= SCDSPINNING;
1105 case ATTEN_AUDIO_DONE:
1106 scd_data[unit].audio_status = CD_AS_PLAY_COMPLETED;
1109 case ATTEN_DRIVE_LOADED:
1110 scd_data[unit].flags &= ~(SCDTOC|SCDSPINNING|SCDVALID);
1111 scd_data[unit].audio_status = CD_AS_AUDIO_INVALID;
1114 case ATTEN_EJECT_PUSHED:
1115 scd_data[unit].flags &= ~SCDVALID;
1121 if (scd_debuglevel > 0 && count > 0)
1126 /* Returns 0 OR sony error code */
1128 spin_up(unsigned unit)
1130 unsigned char res_reg[12];
1131 unsigned int res_size;
1136 rc = send_cmd(unit, CMD_SPIN_UP, 0, 0, res_reg, &res_size);
1138 XDEBUG(2, ("scd%d: CMD_SPIN_UP error 0x%x\n", unit, rc));
1142 if (!(scd_data[unit].flags & SCDTOC)) {
1143 rc = send_cmd(unit, CMD_READ_TOC, 0);
1144 if (rc == ERR_NOT_SPINNING) {
1145 if (loop_count++ < 3)
1153 scd_data[unit].flags |= SCDSPINNING;
1158 static struct sony_tracklist *
1159 get_tl(struct sony_toc *toc, int size)
1161 const char track_list[] = {
1162 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xc0
1164 struct sony_tracklist *tl = &toc->tracks[0];
1167 for (i = 0; i < __arysize(track_list); i++) {
1168 if (tl->track != track_list[i])
1170 tl = (struct sony_tracklist *)((char *)tl + 9);
1176 read_toc(unsigned unit)
1178 struct scd_data *cd;
1179 unsigned part = 0; /* For now ... */
1180 struct sony_toc toc;
1181 struct sony_tracklist *tl;
1185 cd = scd_data + unit;
1187 rc = send_cmd(unit, CMD_GET_TOC, 1, part+1);
1190 if (rc > sizeof(toc)) {
1191 printf("scd%d: program error: toc too large (%d)\n", unit, rc);
1194 if (get_result(unit, rc, (u_char *)&toc) != 0)
1197 XDEBUG(1, ("scd%d: toc read. len = %d, sizeof(toc) = %d\n", unit, rc, sizeof(toc)));
1199 tl = get_tl(&toc, rc);
1200 first = msf2hsg(tl->start_msf);
1201 last = msf2hsg(toc.lead_out_start_msf);
1202 cd->blksize = SCDBLKSIZE;
1203 cd->disksize = last*cd->blksize/DEV_BSIZE;
1205 XDEBUG(1, ("scd%d: firstsector = %ld, lastsector = %ld", unit,
1208 cd->first_track = bcd2bin(toc.first_track);
1209 cd->last_track = bcd2bin(toc.last_track);
1210 if (cd->last_track > (MAX_TRACKS-2))
1211 cd->last_track = MAX_TRACKS-2;
1212 for (j = 0, i = cd->first_track; i <= cd->last_track; i++, j++) {
1213 cd->toc[i].adr = tl[j].adr;
1214 cd->toc[i].ctl = tl[j].ctl; /* for xcdplayer */
1215 bcopy(tl[j].start_msf, cd->toc[i].start_msf, 3);
1217 if (scd_debuglevel > 0) {
1219 printf("\nscd%d: tracks ", unit);
1220 printf("[%03d: %2d %2d %2d] ", i,
1221 bcd2bin(cd->toc[i].start_msf[0]),
1222 bcd2bin(cd->toc[i].start_msf[1]),
1223 bcd2bin(cd->toc[i].start_msf[2]));
1227 bcopy(toc.lead_out_start_msf, cd->toc[cd->last_track+1].start_msf, 3);
1229 if (scd_debuglevel > 0) {
1230 i = cd->last_track+1;
1231 printf("[END: %2d %2d %2d]\n",
1232 bcd2bin(cd->toc[i].start_msf[0]),
1233 bcd2bin(cd->toc[i].start_msf[1]),
1234 bcd2bin(cd->toc[i].start_msf[2]));
1238 bzero(&cd->dlabel,sizeof(struct disklabel));
1239 /* filled with spaces first */
1240 strncpy(cd->dlabel.d_typename," ",
1241 sizeof(cd->dlabel.d_typename));
1242 strncpy(cd->dlabel.d_typename, cd->name,
1243 min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1));
1244 strncpy(cd->dlabel.d_packname,"unknown ",
1245 sizeof(cd->dlabel.d_packname));
1246 cd->dlabel.d_secsize = cd->blksize;
1247 cd->dlabel.d_nsectors = 100;
1248 cd->dlabel.d_ntracks = 1;
1249 cd->dlabel.d_ncylinders = (cd->disksize/100)+1;
1250 cd->dlabel.d_secpercyl = 100;
1251 cd->dlabel.d_secperunit = cd->disksize;
1252 cd->dlabel.d_rpm = 300;
1253 cd->dlabel.d_interleave = 1;
1254 cd->dlabel.d_flags = D_REMOVABLE;
1255 cd->dlabel.d_npartitions= 1;
1256 cd->dlabel.d_partitions[0].p_offset = 0;
1257 cd->dlabel.d_partitions[0].p_size = cd->disksize;
1258 cd->dlabel.d_partitions[0].p_fstype = 9;
1259 cd->dlabel.d_magic = DISKMAGIC;
1260 cd->dlabel.d_magic2 = DISKMAGIC;
1261 cd->dlabel.d_checksum = dkcksum(&cd->dlabel);
1263 cd->flags |= SCDTOC;
1269 init_drive(unsigned unit)
1273 rc = send_cmd(unit, CMD_SET_DRIVE_PARAM, 2,
1274 0x05, 0x03 | ((scd_data[unit].double_speed) ? 0x04: 0));
1276 printf("scd%d: Unable to set parameters. Errcode = 0x%x\n", unit, rc);
1279 /* Returns 0 or errno */
1281 get_result(u_int unit, int result_len, u_char *result)
1283 unsigned int port = scd_data[unit].iobase;
1284 unsigned int res_reg = port + IREG_RESULT;
1285 int loop_index = 2; /* send_cmd() reads two bytes ... */
1287 XDEBUG(1, ("scd%d: DEBUG: get_result: bytes=%d\n", unit, result_len));
1289 while (result_len-- > 0) {
1290 if (loop_index++ >= 10) {
1292 if (waitfor_status_bits(unit, SBIT_RESULT_READY, 0))
1294 write_control(port, CBIT_RESULT_READY_CLEAR);
1297 *result++ = inb(res_reg);
1304 /* Returns -0x100 for timeout, -(drive error code) OR number of result bytes */
1306 send_cmd(u_int unit, u_char cmd, u_int nargs, ...)
1309 u_int port = scd_data[unit].iobase;
1315 if (waitfor_status_bits(unit, 0, SBIT_BUSY)) {
1316 printf("scd%d: drive busy\n", unit);
1320 XDEBUG(1,("scd%d: DEBUG: send_cmd: cmd=0x%x nargs=%d", unit, cmd, nargs));
1322 write_control(port, CBIT_RESULT_READY_CLEAR);
1323 write_control(port, CBIT_RPARAM_CLEAR);
1325 for (i = 0; i < 100; i++)
1326 if (FSTATUS_BIT(port, FBIT_WPARAM_READY))
1328 if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) {
1329 XDEBUG(1, ("\nscd%d: wparam timeout\n", unit));
1333 __va_start(ap, nargs);
1334 reg = port + OREG_WPARAMS;
1335 for (i = 0; i < nargs; i++) {
1336 c = (u_char)__va_arg(ap, int);
1338 XDEBUG(1, (",{0x%x}", c));
1343 outb(port+OREG_COMMAND, cmd);
1345 rc = waitfor_status_bits(unit, SBIT_RESULT_READY, SBIT_BUSY);
1349 reg = port + IREG_RESULT;
1350 write_control(port, CBIT_RESULT_READY_CLEAR);
1351 switch ((rc = inb(reg)) & 0xf0) {
1356 XDEBUG(1, ("scd%d: DEBUG: send_cmd: drive_error=0x%x\n", unit, rc));
1361 XDEBUG(1, ("scd%d: DEBUG: send_cmd: result_len=%d\n", unit, rc));
1367 print_error(int unit, int errcode)
1370 case -ERR_CD_NOT_LOADED:
1371 printf("scd%d: door is open\n", unit);
1373 case -ERR_NO_CD_INSIDE:
1374 printf("scd%d: no cd inside\n", unit);
1377 if (errcode == -0x100 || errcode > 0)
1378 printf("scd%d: device timeout\n", unit);
1380 printf("scd%d: unexpected error 0x%x\n", unit, -errcode);
1385 /* Returns 0 or errno value */
1387 waitfor_status_bits(int unit, int bits_set, int bits_clear)
1389 u_int port = scd_data[unit].iobase;
1390 u_int flags = scd_data[unit].flags;
1391 u_int reg = port + IREG_STATUS;
1395 if (flags & SCDPROBING) {
1397 while (max_loop++ < 1000) {
1401 if (c & SBIT_ATTENTION) {
1402 process_attention(unit);
1405 if ((c & bits_set) == bits_set &&
1406 (c & bits_clear) == 0)
1414 while (max_loop-- > 0) {
1416 if (c & SBIT_ATTENTION) {
1417 process_attention(unit);
1420 if ((c & bits_set) == bits_set &&
1421 (c & bits_clear) == 0)
1425 tsleep(waitfor_status_bits, 0, "waitfor", hz/10);
1428 if ((c & bits_set) == bits_set &&
1429 (c & bits_clear) == 0)
1434 if (scd_debuglevel > 0)
1435 printf("scd%d: DEBUG: waitfor: TIMEOUT (0x%x,(0x%x,0x%x))\n", unit, c, bits_set, bits_clear);
1438 printf("scd%d: timeout.\n", unit);
1442 /* these two routines for xcdplayer - "borrowed" from mcd.c */
1444 scd_toc_header (int unit, struct ioc_toc_header* th)
1446 struct scd_data *cd = scd_data + unit;
1449 if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
1450 print_error(unit, rc);
1454 th->starting_track = cd->first_track;
1455 th->ending_track = cd->last_track;
1456 th->len = 0; /* not used */
1462 scd_toc_entrys (int unit, struct ioc_read_toc_entry *te)
1464 struct scd_data *cd = scd_data + unit;
1465 struct cd_toc_entry toc_entry;
1466 int rc, i, len = te->data_len;
1468 if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
1469 print_error(unit, rc);
1473 /* find the toc to copy*/
1474 i = te->starting_track;
1475 if (i == SCD_LASTPLUS1)
1476 i = cd->last_track + 1;
1478 /* verify starting track */
1479 if (i < cd->first_track || i > cd->last_track+1)
1482 /* valid length ? */
1483 if (len < sizeof(struct cd_toc_entry)
1484 || (len % sizeof(struct cd_toc_entry)) != 0)
1487 /* copy the toc data */
1488 toc_entry.control = cd->toc[i].ctl;
1489 toc_entry.addr_type = te->address_format;
1490 toc_entry.track = i;
1491 if (te->address_format == CD_MSF_FORMAT) {
1492 toc_entry.addr.msf.unused = 0;
1493 toc_entry.addr.msf.minute = bcd2bin(cd->toc[i].start_msf[0]);
1494 toc_entry.addr.msf.second = bcd2bin(cd->toc[i].start_msf[1]);
1495 toc_entry.addr.msf.frame = bcd2bin(cd->toc[i].start_msf[2]);
1498 /* copy the data back */
1499 if (copyout(&toc_entry, te->data, sizeof(struct cd_toc_entry)) != 0)
1507 scd_toc_entry (int unit, struct ioc_read_toc_single_entry *te)
1509 struct scd_data *cd = scd_data + unit;
1510 struct cd_toc_entry toc_entry;
1513 if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
1514 print_error(unit, rc);
1518 /* find the toc to copy*/
1520 if (i == SCD_LASTPLUS1)
1521 i = cd->last_track + 1;
1523 /* verify starting track */
1524 if (i < cd->first_track || i > cd->last_track+1)
1527 /* copy the toc data */
1528 toc_entry.control = cd->toc[i].ctl;
1529 toc_entry.addr_type = te->address_format;
1530 toc_entry.track = i;
1531 if (te->address_format == CD_MSF_FORMAT) {
1532 toc_entry.addr.msf.unused = 0;
1533 toc_entry.addr.msf.minute = bcd2bin(cd->toc[i].start_msf[0]);
1534 toc_entry.addr.msf.second = bcd2bin(cd->toc[i].start_msf[1]);
1535 toc_entry.addr.msf.frame = bcd2bin(cd->toc[i].start_msf[2]);
1538 /* copy the data back */
1539 bcopy(&toc_entry, &te->entry, sizeof(struct cd_toc_entry));