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.18 2006/07/28 02:17:35 dillon 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 bio_queue_head bio_queue; /* head of bio 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 dev_ops scd_ops = {
186 { "scd", CDEV_MAJOR, D_DISK },
191 .d_strategy = scdstrategy,
196 scd_attach(struct isa_device *dev)
198 int unit = dev->id_unit;
199 struct scd_data *cd = scd_data + unit;
201 cd->iobase = dev->id_iobase; /* Already set by probe, but ... */
203 /* name filled in probe */
204 printf("scd%d: <%s>\n", dev->id_unit, scd_data[dev->id_unit].name);
206 init_drive(dev->id_unit);
209 cd->audio_status = CD_AS_AUDIO_INVALID;
210 bioq_init(&cd->bio_queue);
212 dev_ops_add(&scd_ops, dkunitmask(), dkmakeunit(unit));
213 make_dev(&scd_ops, dkmakeminor(unit, 0, 0),
214 UID_ROOT, GID_OPERATOR, 0640, "rscd%da", unit);
215 make_dev(&scd_ops, dkmakeminor(unit, 0, RAW_PART),
216 UID_ROOT, GID_OPERATOR, 0640, "rscd%dc", unit);
217 make_dev(&scd_ops, dkmakeminor(unit, 0, 0),
218 UID_ROOT, GID_OPERATOR, 0640, "scd%da", unit);
219 make_dev(&scd_ops, dkmakeminor(unit, 0, RAW_PART),
220 UID_ROOT, GID_OPERATOR, 0640, "scd%dc", unit);
225 scdopen(struct dev_open_args *ap)
227 dev_t dev = ap->a_head.a_dev;
232 unit = scd_unit(dev);
236 cd = scd_data + unit;
237 part = scd_part(dev);
238 phys = scd_phys(dev);
241 if (!(cd->flags & SCDINIT))
244 /* invalidated in the meantime? mark all open part's invalid */
248 XDEBUG(1,("scd%d: DEBUG: status = 0x%x\n", unit, inb(cd->iobase+IREG_STATUS)));
250 if ((rc = spin_up(unit)) != 0) {
251 print_error(unit, rc);
254 if (!(cd->flags & SCDTOC)) {
257 while (loop_count-- > 0 && (rc = read_toc(unit)) != 0) {
258 if (rc == ERR_NOT_SPINNING) {
261 print_error(unit, rc);\
266 printf("scd%d: TOC read error 0x%x\n", unit, rc);
271 dev->si_bsize_phys = cd->blksize;
274 cd->flags |= SCDVALID;
280 scdclose(struct dev_close_args *ap)
282 dev_t dev = ap->a_head.a_dev;
286 unit = scd_unit(dev);
290 cd = scd_data + unit;
291 part = scd_part(dev);
292 phys = scd_phys(dev);
294 if (!(cd->flags & SCDINIT) || !cd->openflag)
297 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS) {
298 (void)send_cmd(unit, CMD_SPIN_DOWN, 0);
299 cd->flags &= ~SCDSPINNING;
310 scdstrategy(struct dev_strategy_args *ap)
312 dev_t dev = ap->a_head.a_dev;
313 struct bio *bio = ap->a_bio;
314 struct buf *bp = bio->bio_buf;
317 int unit = scd_unit(dev);
319 cd = scd_data + unit;
321 XDEBUG(2, ("scd%d: DEBUG: strategy: offset=%lld, bcount=%d\n",
322 unit, bio->bio_offset, bp->b_bcount));
324 if (unit >= NSCD || bio->bio_offset < 0 || (bp->b_bcount % SCDBLKSIZE)) {
325 printf("scd%d: strategy failure: offset = %lld, bcount = %d\n",
326 unit, bio->bio_offset, bp->b_bcount);
327 bp->b_error = EINVAL;
328 bp->b_flags |= B_ERROR;
332 /* if device invalidated (e.g. media change, door open), error */
333 if (!(cd->flags & SCDVALID)) {
334 printf("scd%d: media changed\n", unit);
340 if (bp->b_cmd != BUF_CMD_READ) {
345 /* no data to read */
346 if (bp->b_bcount == 0)
349 if (!(cd->flags & SCDTOC)) {
353 /* adjust transfer if necessary */
354 nbio = bounds_check_with_label(dev, bio, &cd->dlabel, 1);
358 nbio->bio_driver_info = dev;
363 bioqdisksort(&cd->bio_queue, nbio);
366 /* now check whether we can perform processing */
371 bp->b_flags |= B_ERROR;
373 bp->b_resid = bp->b_bcount;
381 struct scd_data *cd = scd_data + unit;
387 if (cd->flags & SCDMBXBSY) {
392 bio = bioq_first(&cd->bio_queue);
398 /* block found to process, dequeue */
399 bioq_remove(&cd->bio_queue, bio);
400 cd->flags |= SCDMBXBSY;
401 dev = bio->bio_driver_info;
403 p = cd->dlabel.d_partitions + scd_part(dev);
406 cd->mbx.port = cd->iobase;
409 cd->mbx.p_offset = p->p_offset;
412 scd_doread(SCD_S_BEGIN,&(cd->mbx));
417 scdioctl(struct dev_ioctl_args *ap)
419 dev_t dev = ap->a_head.a_dev;
420 caddr_t addr = ap->a_data;
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, ap->a_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",
495 /***************************************************************
496 * lower level of driver starts here
497 **************************************************************/
500 scd_playtracks(int unit, struct ioc_play_track *pt)
502 struct scd_data *cd = scd_data + unit;
503 struct ioc_play_msf msf;
504 int a = pt->start_track;
505 int z = pt->end_track;
508 if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
509 if (rc == -ERR_NOT_SPINNING) {
510 if (spin_up(unit) != 0)
515 print_error(unit, rc);
520 XDEBUG(1, ("scd%d: playtracks from %d:%d to %d:%d\n", unit,
521 a, pt->start_index, z, pt->end_index));
523 if ( a < cd->first_track
524 || a > cd->last_track
526 || z > cd->last_track)
529 bcopy(cd->toc[a].start_msf, &msf.start_m, 3);
530 hsg2msf(msf2hsg(cd->toc[z+1].start_msf)-1, &msf.end_m);
532 return scd_play(unit, &msf);
535 /* The start/end msf is expected to be in bin format */
537 scd_playmsf(int unit, struct ioc_play_msf *msfin)
539 struct ioc_play_msf msf;
541 msf.start_m = bin2bcd(msfin->start_m);
542 msf.start_s = bin2bcd(msfin->start_s);
543 msf.start_f = bin2bcd(msfin->start_f);
544 msf.end_m = bin2bcd(msfin->end_m);
545 msf.end_s = bin2bcd(msfin->end_s);
546 msf.end_f = bin2bcd(msfin->end_f);
548 return scd_play(unit, &msf);
551 /* The start/end msf is expected to be in bcd format */
553 scd_play(int unit, struct ioc_play_msf *msf)
555 struct scd_data *cd = scd_data + unit;
558 XDEBUG(1, ("scd%d: playing: %02x:%02x:%02x -> %02x:%02x:%02x\n", unit,
559 msf->start_m, msf->start_s, msf->start_f,
560 msf->end_m, msf->end_s, msf->end_f));
562 for (i = 0; i < 2; i++) {
563 rc = send_cmd(unit, CMD_PLAY_AUDIO, 7,
565 msf->start_m, msf->start_s, msf->start_f,
566 msf->end_m, msf->end_s, msf->end_f);
567 if (rc == -ERR_NOT_SPINNING) {
568 cd->flags &= ~SCDSPINNING;
569 if (spin_up(unit) != 0)
572 print_error(unit, rc);
578 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
579 bcopy((char *)msf, (char *)&cd->last_play, sizeof(struct ioc_play_msf));
586 struct scd_data *cd = scd_data + unit;
588 (void)send_cmd(unit, CMD_STOP_AUDIO, 0);
589 cd->audio_status = CD_AS_PLAY_COMPLETED;
596 struct scd_data *cd = scd_data + unit;
597 struct sony_subchannel_position_data subpos;
599 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS)
602 if (read_subcode(unit, &subpos) != 0)
605 if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0)
608 cd->last_play.start_m = subpos.abs_msf[0];
609 cd->last_play.start_s = subpos.abs_msf[1];
610 cd->last_play.start_f = subpos.abs_msf[2];
611 cd->audio_status = CD_AS_PLAY_PAUSED;
613 XDEBUG(1, ("scd%d: pause @ %02x:%02x:%02x\n", unit,
614 cd->last_play.start_m,
615 cd->last_play.start_s,
616 cd->last_play.start_f));
624 if (scd_data[unit].audio_status != CD_AS_PLAY_PAUSED)
626 return scd_play(unit, &scd_data[unit].last_play);
632 struct scd_data *cd = scd_data + unit;
634 cd->audio_status = CD_AS_AUDIO_INVALID;
635 cd->flags &= ~(SCDSPINNING|SCDTOC);
637 if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0 ||
638 send_cmd(unit, CMD_SPIN_DOWN, 0) != 0 ||
639 send_cmd(unit, CMD_EJECT, 0) != 0)
647 scd_subchan(int unit, struct ioc_read_subchannel *sc)
649 struct scd_data *cd = scd_data + unit;
650 struct sony_subchannel_position_data q;
651 struct cd_sub_channel_info data;
653 XDEBUG(1, ("scd%d: subchan af=%d, df=%d\n", unit,
657 if (sc->address_format != CD_MSF_FORMAT)
660 if (sc->data_format != CD_CURRENT_POSITION)
663 if (read_subcode(unit, &q) != 0)
666 data.header.audio_status = cd->audio_status;
667 data.what.position.data_format = CD_MSF_FORMAT;
668 data.what.position.track_number = bcd2bin(q.track_number);
669 data.what.position.reladdr.msf.unused = 0;
670 data.what.position.reladdr.msf.minute = bcd2bin(q.rel_msf[0]);
671 data.what.position.reladdr.msf.second = bcd2bin(q.rel_msf[1]);
672 data.what.position.reladdr.msf.frame = bcd2bin(q.rel_msf[2]);
673 data.what.position.absaddr.msf.unused = 0;
674 data.what.position.absaddr.msf.minute = bcd2bin(q.abs_msf[0]);
675 data.what.position.absaddr.msf.second = bcd2bin(q.abs_msf[1]);
676 data.what.position.absaddr.msf.frame = bcd2bin(q.abs_msf[2]);
678 if (copyout(&data, sc->data, min(sizeof(struct cd_sub_channel_info), sc->data_len))!=0)
684 write_control(unsigned port, unsigned data)
686 outb(port + OREG_CONTROL, data);
690 scd_probe(struct isa_device *dev)
692 struct sony_drive_configuration drive_config;
693 int unit = dev->id_unit;
695 static char namebuf[8+16+8+3];
699 scd_data[unit].flags = SCDPROBING;
700 scd_data[unit].iobase = dev->id_iobase;
702 bzero(&drive_config, sizeof(drive_config));
706 write_control(dev->id_iobase, CBIT_RESET_DRIVE);
711 /* Only the ATTENTION bit may be set */
712 if ((inb(dev->id_iobase+IREG_STATUS) & ~1) != 0) {
713 XDEBUG(1, ("scd: too many bits set. probe failed.\n"));
716 rc = send_cmd(unit, CMD_GET_DRIVE_CONFIG, 0);
717 if (rc != sizeof(drive_config)) {
718 /* Sometimes if the drive is playing audio I get */
719 /* the bad result 82. Fix by repeating the reset */
720 if (rc > 0 && loop_count++ == 0)
724 if (get_result(unit, rc, (u_char *)&drive_config) != 0)
727 bcopy(drive_config.vendor, namebuf, 8);
729 while (*(s-1) == ' ') /* Strip trailing spaces */
732 bcopy(drive_config.product, s, 16);
734 while (*(s-1) == ' ')
737 bcopy(drive_config.revision, s, 8);
739 while (*(s-1) == ' ')
743 scd_data[unit].name = namebuf;
745 if (drive_config.config & 0x10)
746 scd_data[unit].double_speed = 1;
748 scd_data[unit].double_speed = 0;
754 read_subcode(int unit, struct sony_subchannel_position_data *sc)
758 rc = send_cmd(unit, CMD_GET_SUBCHANNEL_DATA, 0);
759 if (rc < 0 || rc < sizeof(*sc))
761 if (get_result(unit, rc, (u_char *)sc) != 0)
766 /* State machine copied from mcd.c */
768 /* This (and the code in mcd.c) will not work with more than one drive */
769 /* because there is only one mbxsave below. Should fix that some day. */
770 /* (mbxsave & state should probably be included in the scd_data struct and */
771 /* the unit number used as first argument to scd_doread().) /Micke */
773 /* state machine to process read requests
774 * initialize with SCD_S_BEGIN: reset state machine
775 * SCD_S_WAITSTAT: wait for ready (!busy)
776 * SCD_S_WAITSPIN: wait for drive to spin up (if not spinning)
777 * SCD_S_WAITFIFO: wait for param fifo to get ready, them exec. command.
778 * SCD_S_WAITREAD: wait for data ready, read data
779 * SCD_S_WAITPARAM: wait for command result params, read them, error if bad data read.
782 static struct scd_mbx *mbxsave;
785 scd_timeout(void *arg)
787 scd_doread((int)arg, mbxsave);
791 scd_doread(int state, struct scd_mbx *mbxin)
793 struct scd_mbx *mbx = (state!=SCD_S_BEGIN) ? mbxsave : mbxin;
794 int unit = mbx->unit;
795 int port = mbx->port;
796 struct bio *bio = mbx->bio;
797 struct buf *bp = bio->bio_buf;
798 struct scd_data *cd = scd_data + unit;
802 static char sdata[3]; /* Must be preserved between calls to this function */
807 mbx = mbxsave = mbxin;
811 mbx->count = RDELAY_WAIT;
813 process_attention(unit);
817 callout_stop(&cd->callout);
818 if (mbx->count-- <= 0) {
819 printf("scd%d: timeout. drive busy.\n",unit);
825 callout_reset(&cd->callout, hz / 100,
826 scd_timeout, (void *)SCD_S_WAITSTAT);
830 process_attention(unit);
832 /* reject, if audio active */
833 if (cd->audio_status & CD_AS_PLAY_IN_PROGRESS) {
834 printf("scd%d: audio is active\n",unit);
838 mbx->sz = cd->blksize;
840 /* for first block */
841 mbx->nblk = (bp->b_bcount + (mbx->sz-1)) / mbx->sz;
845 if (!(cd->flags & SCDVALID))
848 blknum = (bio->bio_offset / mbx->sz) +
849 mbx->p_offset + mbx->skip/mbx->sz;
851 XDEBUG(2, ("scd%d: scd_doread: read blknum=%d\n", unit, blknum));
853 /* build parameter block */
854 hsg2msf(blknum, sdata);
856 write_control(port, CBIT_RESULT_READY_CLEAR);
857 write_control(port, CBIT_RPARAM_CLEAR);
858 write_control(port, CBIT_DATA_READY_CLEAR);
860 if (FSTATUS_BIT(port, FBIT_WPARAM_READY))
864 callout_reset(&cd->callout, hz / 100,
865 scd_timeout, (void *)SCD_S_WAITFIFO);
869 callout_stop(&cd->callout);
870 if (mbx->count-- <= 0) {
871 printf("scd%d: timeout waiting for drive to spin up.\n", unit);
874 if (!STATUS_BIT(port, SBIT_RESULT_READY)) {
875 callout_reset(&cd->callout, hz / 100,
876 scd_timeout, (void *)SCD_S_WAITSPIN);
879 write_control(port, CBIT_RESULT_READY_CLEAR);
880 switch ((i = inb(port+IREG_RESULT)) & 0xf0) {
882 i = inb(port+IREG_RESULT);
883 print_error(unit, i);
886 (void)inb(port+IREG_RESULT);
887 cd->flags |= SCDSPINNING;
890 XDEBUG(1, ("scd%d: DEBUG: spin up complete\n", unit));
892 state = SCD_S_BEGIN1;
896 callout_stop(&cd->callout);
897 if (mbx->count-- <= 0) {
898 printf("scd%d: timeout. write param not ready.\n",unit);
901 if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) {
902 callout_reset(&cd->callout, hz / 100,
903 scd_timeout, (void *)SCD_S_WAITFIFO);
906 XDEBUG(1, ("scd%d: mbx->count (writeparamwait) = %d(%d)\n", unit, mbx->count, 100));
909 /* The reason this test isn't done 'till now is to make sure */
910 /* that it is ok to send the SPIN_UP cmd below. */
911 if (!(cd->flags & SCDSPINNING)) {
912 XDEBUG(1, ("scd%d: spinning up drive ...\n", unit));
913 outb(port+OREG_COMMAND, CMD_SPIN_UP);
915 callout_reset(&cd->callout, hz / 100,
916 scd_timeout, (void *)SCD_S_WAITSPIN);
920 reg = port + OREG_WPARAMS;
921 /* send the read command */
929 outb(port+OREG_COMMAND, CMD_READ);
932 mbx->count = RDELAY_WAITREAD;
933 for (i = 0; i < 50; i++) {
934 if (STATUS_BIT(port, SBIT_DATA_READY))
939 callout_reset(&cd->callout, hz / 100,
940 scd_timeout, (void *)SCD_S_WAITREAD);
944 callout_stop(&cd->callout);
945 if (mbx->count-- <= 0) {
946 if (STATUS_BIT(port, SBIT_RESULT_READY))
948 printf("scd%d: timeout while reading data\n",unit);
951 if (!STATUS_BIT(port, SBIT_DATA_READY)) {
952 process_attention(unit);
953 if (!(cd->flags & SCDVALID))
955 callout_reset(&cd->callout, hz / 100,
956 scd_timeout, (void *)SCD_S_WAITREAD);
959 XDEBUG(2, ("scd%d: mbx->count (after RDY_BIT) = %d(%d)\n", unit, mbx->count, RDELAY_WAITREAD));
963 addr = bp->b_data + mbx->skip;
964 write_control(port, CBIT_DATA_READY_CLEAR);
965 insb(port+IREG_DATA, addr, mbx->sz);
968 for (i = 0; i < 20; i++) {
969 if (STATUS_BIT(port, SBIT_RESULT_READY))
975 case SCD_S_WAITPARAM:
976 callout_stop(&cd->callout);
977 if (mbx->count-- <= 0) {
978 printf("scd%d: timeout waiting for params\n",unit);
983 if (!STATUS_BIT(port, SBIT_RESULT_READY)) {
984 callout_reset(&cd->callout, hz / 100,
985 scd_timeout, (void *)SCD_S_WAITPARAM);
989 if (mbx->count < 100 && scd_debuglevel > 0)
990 printf("scd%d: mbx->count (paramwait) = %d(%d)\n", unit, mbx->count, 100);
994 write_control(port, CBIT_RESULT_READY_CLEAR);
995 switch ((i = inb(port+IREG_RESULT)) & 0xf0) {
998 case ERR_FATAL_READ_ERROR1:
999 case ERR_FATAL_READ_ERROR2:
1000 printf("scd%d: unrecoverable read error 0x%x\n", unit, i);
1005 i = inb(port+IREG_RESULT);
1007 case ERR_NOT_SPINNING:
1008 XDEBUG(1, ("scd%d: read error: drive not spinning\n", unit));
1009 if (mbx->retry-- > 0) {
1010 state = SCD_S_BEGIN1;
1011 cd->flags &= ~SCDSPINNING;
1016 print_error(unit, i);
1020 i = inb(port+IREG_RESULT);
1024 if (--mbx->nblk > 0) {
1025 mbx->skip += mbx->sz;
1033 cd->flags &= ~SCDMBXBSY;
1034 scd_start(mbx->unit);
1039 if (mbx->retry-- > 0) {
1040 printf("scd%d: retrying ...\n",unit);
1041 state = SCD_S_BEGIN1;
1045 /* invalidate the buffer */
1047 bp->b_flags |= B_ERROR;
1048 bp->b_resid = bp->b_bcount;
1051 cd->flags &= ~SCDMBXBSY;
1052 scd_start(mbx->unit);
1056 printf("scd%d: media changed\n", unit);
1061 hsg2msf(int hsg, bcd_t *msf)
1064 M_msf(msf) = bin2bcd(hsg / 4500);
1066 S_msf(msf) = bin2bcd(hsg / 75);
1067 F_msf(msf) = bin2bcd(hsg % 75);
1073 return (bcd2bin(M_msf(msf)) * 60 +
1074 bcd2bin(S_msf(msf))) * 75 +
1075 bcd2bin(F_msf(msf)) - 150;
1079 process_attention(unsigned unit)
1081 unsigned port = scd_data[unit].iobase;
1085 while (IS_ATTENTION(port) && count++ < 30) {
1086 write_control(port, CBIT_ATTENTION_CLEAR);
1087 code = inb(port+IREG_RESULT);
1090 if (scd_debuglevel > 0) {
1092 printf("scd%d: DEBUG: ATTENTIONS = 0x%x", unit, code);
1094 printf(",0x%x", code);
1099 case ATTEN_SPIN_DOWN:
1100 scd_data[unit].flags &= ~SCDSPINNING;
1103 case ATTEN_SPIN_UP_DONE:
1104 scd_data[unit].flags |= SCDSPINNING;
1107 case ATTEN_AUDIO_DONE:
1108 scd_data[unit].audio_status = CD_AS_PLAY_COMPLETED;
1111 case ATTEN_DRIVE_LOADED:
1112 scd_data[unit].flags &= ~(SCDTOC|SCDSPINNING|SCDVALID);
1113 scd_data[unit].audio_status = CD_AS_AUDIO_INVALID;
1116 case ATTEN_EJECT_PUSHED:
1117 scd_data[unit].flags &= ~SCDVALID;
1123 if (scd_debuglevel > 0 && count > 0)
1128 /* Returns 0 OR sony error code */
1130 spin_up(unsigned unit)
1132 unsigned char res_reg[12];
1133 unsigned int res_size;
1138 rc = send_cmd(unit, CMD_SPIN_UP, 0, 0, res_reg, &res_size);
1140 XDEBUG(2, ("scd%d: CMD_SPIN_UP error 0x%x\n", unit, rc));
1144 if (!(scd_data[unit].flags & SCDTOC)) {
1145 rc = send_cmd(unit, CMD_READ_TOC, 0);
1146 if (rc == ERR_NOT_SPINNING) {
1147 if (loop_count++ < 3)
1155 scd_data[unit].flags |= SCDSPINNING;
1160 static struct sony_tracklist *
1161 get_tl(struct sony_toc *toc, int size)
1163 const char track_list[] = {
1164 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xc0
1166 struct sony_tracklist *tl = &toc->tracks[0];
1169 for (i = 0; i < __arysize(track_list); i++) {
1170 if (tl->track != track_list[i])
1172 tl = (struct sony_tracklist *)((char *)tl + 9);
1178 read_toc(unsigned unit)
1180 struct scd_data *cd;
1181 unsigned part = 0; /* For now ... */
1182 struct sony_toc toc;
1183 struct sony_tracklist *tl;
1187 cd = scd_data + unit;
1189 rc = send_cmd(unit, CMD_GET_TOC, 1, part+1);
1192 if (rc > sizeof(toc)) {
1193 printf("scd%d: program error: toc too large (%d)\n", unit, rc);
1196 if (get_result(unit, rc, (u_char *)&toc) != 0)
1199 XDEBUG(1, ("scd%d: toc read. len = %d, sizeof(toc) = %d\n", unit, rc, sizeof(toc)));
1201 tl = get_tl(&toc, rc);
1202 first = msf2hsg(tl->start_msf);
1203 last = msf2hsg(toc.lead_out_start_msf);
1204 cd->blksize = SCDBLKSIZE;
1205 cd->disksize = last*cd->blksize/DEV_BSIZE;
1207 XDEBUG(1, ("scd%d: firstsector = %ld, lastsector = %ld", unit,
1210 cd->first_track = bcd2bin(toc.first_track);
1211 cd->last_track = bcd2bin(toc.last_track);
1212 if (cd->last_track > (MAX_TRACKS-2))
1213 cd->last_track = MAX_TRACKS-2;
1214 for (j = 0, i = cd->first_track; i <= cd->last_track; i++, j++) {
1215 cd->toc[i].adr = tl[j].adr;
1216 cd->toc[i].ctl = tl[j].ctl; /* for xcdplayer */
1217 bcopy(tl[j].start_msf, cd->toc[i].start_msf, 3);
1219 if (scd_debuglevel > 0) {
1221 printf("\nscd%d: tracks ", unit);
1222 printf("[%03d: %2d %2d %2d] ", i,
1223 bcd2bin(cd->toc[i].start_msf[0]),
1224 bcd2bin(cd->toc[i].start_msf[1]),
1225 bcd2bin(cd->toc[i].start_msf[2]));
1229 bcopy(toc.lead_out_start_msf, cd->toc[cd->last_track+1].start_msf, 3);
1231 if (scd_debuglevel > 0) {
1232 i = cd->last_track+1;
1233 printf("[END: %2d %2d %2d]\n",
1234 bcd2bin(cd->toc[i].start_msf[0]),
1235 bcd2bin(cd->toc[i].start_msf[1]),
1236 bcd2bin(cd->toc[i].start_msf[2]));
1240 bzero(&cd->dlabel,sizeof(struct disklabel));
1241 /* filled with spaces first */
1242 strncpy(cd->dlabel.d_typename," ",
1243 sizeof(cd->dlabel.d_typename));
1244 strncpy(cd->dlabel.d_typename, cd->name,
1245 min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1));
1246 strncpy(cd->dlabel.d_packname,"unknown ",
1247 sizeof(cd->dlabel.d_packname));
1248 cd->dlabel.d_secsize = cd->blksize;
1249 cd->dlabel.d_nsectors = 100;
1250 cd->dlabel.d_ntracks = 1;
1251 cd->dlabel.d_ncylinders = (cd->disksize/100)+1;
1252 cd->dlabel.d_secpercyl = 100;
1253 cd->dlabel.d_secperunit = cd->disksize;
1254 cd->dlabel.d_rpm = 300;
1255 cd->dlabel.d_interleave = 1;
1256 cd->dlabel.d_flags = D_REMOVABLE;
1257 cd->dlabel.d_npartitions= 1;
1258 cd->dlabel.d_partitions[0].p_offset = 0;
1259 cd->dlabel.d_partitions[0].p_size = cd->disksize;
1260 cd->dlabel.d_partitions[0].p_fstype = 9;
1261 cd->dlabel.d_magic = DISKMAGIC;
1262 cd->dlabel.d_magic2 = DISKMAGIC;
1263 cd->dlabel.d_checksum = dkcksum(&cd->dlabel);
1265 cd->flags |= SCDTOC;
1271 init_drive(unsigned unit)
1275 rc = send_cmd(unit, CMD_SET_DRIVE_PARAM, 2,
1276 0x05, 0x03 | ((scd_data[unit].double_speed) ? 0x04: 0));
1278 printf("scd%d: Unable to set parameters. Errcode = 0x%x\n", unit, rc);
1281 /* Returns 0 or errno */
1283 get_result(u_int unit, int result_len, u_char *result)
1285 unsigned int port = scd_data[unit].iobase;
1286 unsigned int res_reg = port + IREG_RESULT;
1287 int loop_index = 2; /* send_cmd() reads two bytes ... */
1289 XDEBUG(1, ("scd%d: DEBUG: get_result: bytes=%d\n", unit, result_len));
1291 while (result_len-- > 0) {
1292 if (loop_index++ >= 10) {
1294 if (waitfor_status_bits(unit, SBIT_RESULT_READY, 0))
1296 write_control(port, CBIT_RESULT_READY_CLEAR);
1299 *result++ = inb(res_reg);
1306 /* Returns -0x100 for timeout, -(drive error code) OR number of result bytes */
1308 send_cmd(u_int unit, u_char cmd, u_int nargs, ...)
1311 u_int port = scd_data[unit].iobase;
1317 if (waitfor_status_bits(unit, 0, SBIT_BUSY)) {
1318 printf("scd%d: drive busy\n", unit);
1322 XDEBUG(1,("scd%d: DEBUG: send_cmd: cmd=0x%x nargs=%d", unit, cmd, nargs));
1324 write_control(port, CBIT_RESULT_READY_CLEAR);
1325 write_control(port, CBIT_RPARAM_CLEAR);
1327 for (i = 0; i < 100; i++)
1328 if (FSTATUS_BIT(port, FBIT_WPARAM_READY))
1330 if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) {
1331 XDEBUG(1, ("\nscd%d: wparam timeout\n", unit));
1335 __va_start(ap, nargs);
1336 reg = port + OREG_WPARAMS;
1337 for (i = 0; i < nargs; i++) {
1338 c = (u_char)__va_arg(ap, int);
1340 XDEBUG(1, (",{0x%x}", c));
1345 outb(port+OREG_COMMAND, cmd);
1347 rc = waitfor_status_bits(unit, SBIT_RESULT_READY, SBIT_BUSY);
1351 reg = port + IREG_RESULT;
1352 write_control(port, CBIT_RESULT_READY_CLEAR);
1353 switch ((rc = inb(reg)) & 0xf0) {
1358 XDEBUG(1, ("scd%d: DEBUG: send_cmd: drive_error=0x%x\n", unit, rc));
1363 XDEBUG(1, ("scd%d: DEBUG: send_cmd: result_len=%d\n", unit, rc));
1369 print_error(int unit, int errcode)
1372 case -ERR_CD_NOT_LOADED:
1373 printf("scd%d: door is open\n", unit);
1375 case -ERR_NO_CD_INSIDE:
1376 printf("scd%d: no cd inside\n", unit);
1379 if (errcode == -0x100 || errcode > 0)
1380 printf("scd%d: device timeout\n", unit);
1382 printf("scd%d: unexpected error 0x%x\n", unit, -errcode);
1387 /* Returns 0 or errno value */
1389 waitfor_status_bits(int unit, int bits_set, int bits_clear)
1391 u_int port = scd_data[unit].iobase;
1392 u_int flags = scd_data[unit].flags;
1393 u_int reg = port + IREG_STATUS;
1397 if (flags & SCDPROBING) {
1399 while (max_loop++ < 1000) {
1403 if (c & SBIT_ATTENTION) {
1404 process_attention(unit);
1407 if ((c & bits_set) == bits_set &&
1408 (c & bits_clear) == 0)
1416 while (max_loop-- > 0) {
1418 if (c & SBIT_ATTENTION) {
1419 process_attention(unit);
1422 if ((c & bits_set) == bits_set &&
1423 (c & bits_clear) == 0)
1427 tsleep(waitfor_status_bits, 0, "waitfor", hz/10);
1430 if ((c & bits_set) == bits_set &&
1431 (c & bits_clear) == 0)
1436 if (scd_debuglevel > 0)
1437 printf("scd%d: DEBUG: waitfor: TIMEOUT (0x%x,(0x%x,0x%x))\n", unit, c, bits_set, bits_clear);
1440 printf("scd%d: timeout.\n", unit);
1444 /* these two routines for xcdplayer - "borrowed" from mcd.c */
1446 scd_toc_header (int unit, struct ioc_toc_header* th)
1448 struct scd_data *cd = scd_data + unit;
1451 if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
1452 print_error(unit, rc);
1456 th->starting_track = cd->first_track;
1457 th->ending_track = cd->last_track;
1458 th->len = 0; /* not used */
1464 scd_toc_entrys (int unit, struct ioc_read_toc_entry *te)
1466 struct scd_data *cd = scd_data + unit;
1467 struct cd_toc_entry toc_entry;
1468 int rc, i, len = te->data_len;
1470 if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
1471 print_error(unit, rc);
1475 /* find the toc to copy*/
1476 i = te->starting_track;
1477 if (i == SCD_LASTPLUS1)
1478 i = cd->last_track + 1;
1480 /* verify starting track */
1481 if (i < cd->first_track || i > cd->last_track+1)
1484 /* valid length ? */
1485 if (len < sizeof(struct cd_toc_entry)
1486 || (len % sizeof(struct cd_toc_entry)) != 0)
1489 /* copy the toc data */
1490 toc_entry.control = cd->toc[i].ctl;
1491 toc_entry.addr_type = te->address_format;
1492 toc_entry.track = i;
1493 if (te->address_format == CD_MSF_FORMAT) {
1494 toc_entry.addr.msf.unused = 0;
1495 toc_entry.addr.msf.minute = bcd2bin(cd->toc[i].start_msf[0]);
1496 toc_entry.addr.msf.second = bcd2bin(cd->toc[i].start_msf[1]);
1497 toc_entry.addr.msf.frame = bcd2bin(cd->toc[i].start_msf[2]);
1500 /* copy the data back */
1501 if (copyout(&toc_entry, te->data, sizeof(struct cd_toc_entry)) != 0)
1509 scd_toc_entry (int unit, struct ioc_read_toc_single_entry *te)
1511 struct scd_data *cd = scd_data + unit;
1512 struct cd_toc_entry toc_entry;
1515 if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
1516 print_error(unit, rc);
1520 /* find the toc to copy*/
1522 if (i == SCD_LASTPLUS1)
1523 i = cd->last_track + 1;
1525 /* verify starting track */
1526 if (i < cd->first_track || i > cd->last_track+1)
1529 /* copy the toc data */
1530 toc_entry.control = cd->toc[i].ctl;
1531 toc_entry.addr_type = te->address_format;
1532 toc_entry.track = i;
1533 if (te->address_format == CD_MSF_FORMAT) {
1534 toc_entry.addr.msf.unused = 0;
1535 toc_entry.addr.msf.minute = bcd2bin(cd->toc[i].start_msf[0]);
1536 toc_entry.addr.msf.second = bcd2bin(cd->toc[i].start_msf[1]);
1537 toc_entry.addr.msf.frame = bcd2bin(cd->toc[i].start_msf[2]);
1540 /* copy the data back */
1541 bcopy(&toc_entry, &te->entry, sizeof(struct cd_toc_entry));