2 * Copyright 1993 by Holger Veit (data part)
3 * Copyright 1993 by Brian Moore (audio part)
4 * Changes Copyright 1993 by Gary Clark II
5 * Changes Copyright (C) 1994-1995 by Andrey A. Chernov, Moscow, Russia
7 * Rewrote probe routine to work on newer Mitsumi drives.
8 * Additional changes (C) 1994 by Jordan K. Hubbard
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This software was developed by Holger Veit and Brian Moore
23 * for use with "386BSD" and similar operating systems.
24 * "Similar operating systems" includes mainly non-profit oriented
25 * systems for research and education, including but not restricted to
26 * "NetBSD", "FreeBSD", "Mach" (by CMU).
27 * 4. Neither the name of the developer(s) nor the name "386BSD"
28 * may be used to endorse or promote products derived from this
29 * software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY
32 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER(S) BE
35 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
36 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
37 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
38 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 * $FreeBSD: src/sys/i386/isa/mcd.c,v 1.115 2000/01/29 16:17:34 peter Exp $
44 * $DragonFly: src/sys/dev/disk/mcd/Attic/mcd.c,v 1.19 2006/07/28 02:17:35 dillon Exp $
46 static const char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/bootmaj.h>
54 #include <sys/fcntl.h>
57 #include <sys/disklabel.h>
58 #include <sys/kernel.h>
60 #include <sys/thread2.h>
61 #include <machine/clock.h>
63 #include <bus/isa/i386/isa_device.h>
66 #define MCD_TRACE(format, args...) \
68 if (mcd_data[unit].debug) { \
69 printf("mcd%d: status=0x%02x: ", \
70 unit, mcd_data[unit].status); \
71 printf(format, ## args); \
75 #define mcd_part(dev) ((minor(dev)) & 7)
76 #define mcd_unit(dev) (((minor(dev)) & 0x38) >> 3)
77 #define mcd_phys(dev) (((minor(dev)) & 0x40) >> 6)
81 #define MCDVALID 0x0001 /* parameters loaded */
82 #define MCDINIT 0x0002 /* device is init'd */
83 #define MCDNEWMODEL 0x0004 /* device is new model */
84 #define MCDLABEL 0x0008 /* label is read */
85 #define MCDPROBING 0x0010 /* probing */
86 #define MCDREADRAW 0x0020 /* read raw mode (2352 bytes) */
87 #define MCDVOLINFO 0x0040 /* already read volinfo */
88 #define MCDTOC 0x0080 /* already read toc */
89 #define MCDMBXBSY 0x0100 /* local mbx is busy */
92 #define MCDAUDIOBSY MCD_ST_AUDIOBSY /* playing audio */
93 #define MCDDSKCHNG MCD_ST_DSKCHNG /* sensed change of disk */
94 #define MCDDSKIN MCD_ST_DSKIN /* sensed disk in drive */
95 #define MCDDOOROPEN MCD_ST_DOOROPEN /* sensed door open */
97 /* These are apparently the different states a mitsumi can get up to */
98 #define MCDCDABSENT 0x0030
99 #define MCDCDPRESENT 0x0020
100 #define MCDSCLOSED 0x0080
101 #define MCDSOPEN 0x00a0
103 #define MCD_MD_UNKNOWN (-1)
106 #define MCD_MAXTOCS 104 /* from the Linux driver */
107 #define MCD_LASTPLUS1 170 /* special toc entry */
109 #define MCD_TYPE_UNKNOWN 0
110 #define MCD_TYPE_LU002S 1
111 #define MCD_TYPE_LU005S 2
112 #define MCD_TYPE_LU006S 3
113 #define MCD_TYPE_FX001 4
114 #define MCD_TYPE_FX001D 5
129 static struct mcd_data {
139 struct disklabel dlabel;
140 int partflags[MAXPARTITIONS];
142 struct mcd_volinfo volinfo;
143 struct mcd_qchninfo toc[MCD_MAXTOCS];
146 struct mcd_read2 lastpb;
148 struct bio_queue_head bio_queue; /* head of buf queue */
150 struct callout callout;
153 /* reader state machine */
154 #define MCD_S_BEGIN 0
155 #define MCD_S_BEGIN1 1
156 #define MCD_S_WAITSTAT 2
157 #define MCD_S_WAITMODE 3
158 #define MCD_S_WAITREAD 4
161 static void mcd_start(int unit);
162 static int mcd_getdisklabel(int unit);
164 static void mcd_configure(struct mcd_data *cd);
166 static int mcd_get(int unit, char *buf, int nmax);
167 static int mcd_setflags(int unit,struct mcd_data *cd);
168 static int mcd_getstat(int unit,int sflg);
169 static int mcd_send(int unit, int cmd,int nretrys);
170 static void hsg2msf(int hsg, bcd_t *msf);
171 static int msf2hsg(bcd_t *msf, int relative);
172 static int mcd_volinfo(int unit);
173 static void mcdintr(void *);
174 static int mcd_waitrdy(int port,int dly);
175 static timeout_t mcd_timeout;
176 static void mcd_doread(int state, struct mcd_mbx *mbxin);
177 static void mcd_soft_reset(int unit);
178 static int mcd_hard_reset(int unit);
179 static int mcd_setmode(int unit, int mode);
180 static int mcd_getqchan(int unit, struct mcd_qchninfo *q);
181 static int mcd_subchan(int unit, struct ioc_read_subchannel *sc);
182 static int mcd_toc_header(int unit, struct ioc_toc_header *th);
183 static int mcd_read_toc(int unit);
184 static int mcd_toc_entrys(int unit, struct ioc_read_toc_entry *te);
186 static int mcd_toc_entry(int unit, struct ioc_read_toc_single_entry *te);
188 static int mcd_stop(int unit);
189 static int mcd_eject(int unit);
190 static int mcd_inject(int unit);
191 static int mcd_playtracks(int unit, struct ioc_play_track *pt);
192 static int mcd_play(int unit, struct mcd_read2 *pb);
193 static int mcd_playmsf(int unit, struct ioc_play_msf *pt);
194 static int mcd_playblocks(int unit, struct ioc_play_blocks *);
195 static int mcd_pause(int unit);
196 static int mcd_resume(int unit);
197 static int mcd_lock_door(int unit, int lock);
198 static int mcd_close_tray(int unit);
200 static int mcd_probe(struct isa_device *dev);
201 static int mcd_attach(struct isa_device *dev);
202 struct isa_driver mcddriver = { mcd_probe, mcd_attach, "mcd" };
204 static d_open_t mcdopen;
205 static d_close_t mcdclose;
206 static d_ioctl_t mcdioctl;
207 static d_strategy_t mcdstrategy;
209 static int mcdsize(dev_t dev);
211 static struct dev_ops mcd_ops = {
212 { "mcd", MCD_CDEV_MAJOR, D_DISK },
217 .d_strategy = mcdstrategy,
220 #define mcd_put(port,byte) outb(port,byte)
223 #define MCD_RDRETRYS 8
225 #define CLOSE_TRAY_SECS 8
226 #define DISK_SENSE_SECS 3
230 #define RDELAY_WAITSTAT 300
231 #define RDELAY_WAITMODE 300
232 #define RDELAY_WAITREAD 800
235 #define DELAY_GETREPLY 5000000
237 int mcd_attach(struct isa_device *dev)
239 int unit = dev->id_unit;
240 struct mcd_data *cd = mcd_data + unit;
242 dev->id_intr = (inthand2_t *)mcdintr;
243 cd->iobase = dev->id_iobase;
244 cd->flags |= MCDINIT;
245 callout_init(&cd->callout);
246 mcd_soft_reset(unit);
247 bioq_init(&cd->bio_queue);
250 /* wire controller for interrupts and dma */
253 /* name filled in probe */
254 dev_ops_add(&mcd_ops, dkunitmask(), dkmakeunit(unit));
255 make_dev(&mcd_ops, dkmakeminor(unit, 0, 0),
256 UID_ROOT, GID_OPERATOR, 0640, "rmcd%da", unit);
257 make_dev(&mcd_ops, dkmakeminor(unit, 0, RAW_PART),
258 UID_ROOT, GID_OPERATOR, 0640, "rmcd%dc", unit);
259 make_dev(&mcd_ops, dkmakeminor(unit, 0, 0),
260 UID_ROOT, GID_OPERATOR, 0640, "mcd%da", unit);
261 make_dev(&mcd_ops, dkmakeminor(unit, 0, RAW_PART),
262 UID_ROOT, GID_OPERATOR, 0640, "mcd%dc", unit);
267 mcdopen(struct dev_open_args *ap)
269 dev_t dev = ap->a_head.a_dev;
270 int unit,part,phys,r,retry;
273 unit = mcd_unit(dev);
277 cd = mcd_data + unit;
278 part = mcd_part(dev);
279 phys = mcd_phys(dev);
282 if (!(cd->flags & MCDINIT))
285 /* invalidated in the meantime? mark all open part's invalid */
286 if (!(cd->flags & MCDVALID) && cd->openflags)
289 if (mcd_getstat(unit,1) == -1)
292 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
293 || !(cd->status & MCDDSKIN))
294 for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) {
295 (void) tsleep((caddr_t)cd, PCATCH, "mcdsn1", hz/WAIT_FRAC);
296 if ((r = mcd_getstat(unit,1)) == -1)
302 if (( (cd->status & (MCDDOOROPEN|MCDDSKCHNG))
303 || !(cd->status & MCDDSKIN)
305 && major(dev) == MCD_CDEV_MAJOR && part == RAW_PART
307 cd->openflags |= (1<<part);
309 cd->partflags[part] |= MCDREADRAW;
312 if (cd->status & MCDDOOROPEN) {
313 printf("mcd%d: door is open\n", unit);
316 if (!(cd->status & MCDDSKIN)) {
317 printf("mcd%d: no CD inside\n", unit);
320 if (cd->status & MCDDSKCHNG) {
321 printf("mcd%d: CD not sensed\n", unit);
325 if (mcdsize(dev) < 0) {
326 if (major(dev) == MCD_CDEV_MAJOR && part == RAW_PART) {
327 cd->openflags |= (1<<part);
329 cd->partflags[part] |= MCDREADRAW;
332 printf("mcd%d: failed to get disk size\n",unit);
335 cd->flags |= MCDVALID;
337 /* XXX get a default disklabel */
338 mcd_getdisklabel(unit);
340 MCD_TRACE("open: partition=%d, disksize = %ld, blksize=%d\n",
341 part, cd->disksize, cd->blksize);
343 dev->si_bsize_phys = cd->blksize;
345 if (part == RAW_PART ||
346 (part < cd->dlabel.d_npartitions &&
347 cd->dlabel.d_partitions[part].p_fstype != FS_UNUSED)) {
348 cd->openflags |= (1<<part);
349 if (part == RAW_PART && phys)
350 cd->partflags[part] |= MCDREADRAW;
351 (void) mcd_lock_door(unit, MCD_LK_LOCK);
352 if (!(cd->flags & MCDVALID))
360 int mcdclose(struct dev_close_args *ap)
362 dev_t dev = ap->a_head.a_dev;
366 unit = mcd_unit(dev);
370 cd = mcd_data + unit;
371 part = mcd_part(dev);
373 if (!(cd->flags & MCDINIT) || !(cd->openflags & (1<<part)))
376 MCD_TRACE("close: partition=%d\n", part);
378 (void) mcd_lock_door(unit, MCD_LK_UNLOCK);
379 cd->openflags &= ~(1<<part);
380 cd->partflags[part] &= ~MCDREADRAW;
386 mcdstrategy(struct dev_strategy_args *ap)
388 dev_t dev = ap->a_head.a_dev;
389 struct bio *bio = ap->a_bio;
391 struct buf *bp = bio->bio_buf;
393 int unit = mcd_unit(dev);
395 cd = mcd_data + unit;
396 bio->bio_driver_info = dev;
399 /*MCD_TRACE("strategy: buf=0x%lx, unit=%ld, offset=%lld bcount=%ld\n",
400 bp,unit,bio->bio_offset,bp->b_bcount);*/
401 if (unit >= NMCD || bio->bio_offset < 0) {
402 printf("mcdstrategy: unit = %d, offset = %lld, bcount = %d\n",
403 unit, bio->bio_offset, bp->b_bcount);
404 printf("mcd: mcdstratregy failure");
405 bp->b_error = EINVAL;
406 bp->b_flags |= B_ERROR;
410 /* if device invalidated (e.g. media change, door open), error */
411 if (!(cd->flags & MCDVALID)) {
412 MCD_TRACE("strategy: drive not valid\n");
418 if (bp->b_cmd != BUF_CMD_READ) {
423 /* no data to read */
424 if (bp->b_bcount == 0)
427 /* for non raw access, check partition limits */
428 if (mcd_part(dev) != RAW_PART) {
429 if (!(cd->flags & MCDLABEL)) {
433 /* adjust transfer if necessary */
434 nbio = bounds_check_with_label(dev, bio, &cd->dlabel, 1);
444 bioqdisksort(&cd->bio_queue, nbio);
447 /* now check whether we can perform processing */
452 * These cases occur before nbio is set, use bio.
455 bp->b_flags |= B_ERROR;
457 bp->b_resid = bp->b_bcount;
462 static void mcd_start(int unit)
464 struct mcd_data *cd = mcd_data + unit;
471 if (cd->flags & MCDMBXBSY) {
476 bio = bioq_first(&cd->bio_queue);
483 dev = bio->bio_driver_info;
485 /* block found to process, dequeue */
486 /*MCD_TRACE("mcd_start: found block bp=0x%x\n",bp,0,0,0);*/
487 bioq_remove(&cd->bio_queue, bio);
491 if (!(cd->flags & MCDVALID)) {
492 MCD_TRACE("mcd_start: drive not valid\n");
496 p = cd->dlabel.d_partitions + mcd_part(dev);
498 cd->flags |= MCDMBXBSY;
499 if (cd->partflags[mcd_part(dev)] & MCDREADRAW)
500 cd->flags |= MCDREADRAW;
502 cd->mbx.port = cd->iobase;
503 cd->mbx.retry = MCD_RETRYS;
505 cd->mbx.p_offset = p->p_offset;
507 /* calling the read routine */
508 mcd_doread(MCD_S_BEGIN,&(cd->mbx));
509 /* triggers mcd_start, when successful finished */
514 mcdioctl(struct dev_ioctl_args *ap)
516 dev_t dev = ap->a_head.a_dev;
517 caddr_t addr = ap->a_data;
519 int unit,part,retry,r;
521 unit = mcd_unit(dev);
522 part = mcd_part(dev);
523 cd = mcd_data + unit;
525 if (mcd_getstat(unit, 1) == -1) /* detect disk change too */
527 MCD_TRACE("ioctl called 0x%lx\n", ap->a_cmd);
540 return mcd_eject(unit);
548 return mcd_hard_reset(unit);
550 return mcd_lock_door(unit, MCD_LK_UNLOCK);
552 return mcd_lock_door(unit, MCD_LK_LOCK);
554 return mcd_inject(unit);
557 if (!(cd->flags & MCDVALID)) {
558 if ( major(dev) != MCD_CDEV_MAJOR
560 || !(cd->openflags & (1<<RAW_PART))
563 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
564 || !(cd->status & MCDDSKIN))
565 for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) {
566 (void) tsleep((caddr_t)cd, PCATCH, "mcdsn2", hz/WAIT_FRAC);
567 if ((r = mcd_getstat(unit,1)) == -1)
572 if ( (cd->status & (MCDDOOROPEN|MCDDSKCHNG))
573 || !(cd->status & MCDDSKIN)
577 cd->flags |= MCDVALID;
578 mcd_getdisklabel(unit);
580 cd->partflags[part] |= MCDREADRAW;
581 (void) mcd_lock_door(unit, MCD_LK_LOCK);
582 if (!(cd->flags & MCDVALID))
588 *(struct disklabel *) addr = cd->dlabel;
591 ((struct partinfo *) addr)->disklab = &cd->dlabel;
592 ((struct partinfo *) addr)->part =
593 &cd->dlabel.d_partitions[mcd_part(dev)];
597 * a bit silly, but someone might want to test something on a
602 if ((ap->a_fflag & FWRITE) == 0)
605 return setdisklabel(&cd->dlabel,
606 (struct disklabel *) addr,
611 case CDIOCPLAYTRACKS:
612 return mcd_playtracks(unit, (struct ioc_play_track *) addr);
613 case CDIOCPLAYBLOCKS:
614 return mcd_playblocks(unit, (struct ioc_play_blocks *) addr);
616 return mcd_playmsf(unit, (struct ioc_play_msf *) addr);
617 case CDIOCREADSUBCHANNEL:
618 return mcd_subchan(unit, (struct ioc_read_subchannel *) addr);
619 case CDIOREADTOCHEADER:
620 return mcd_toc_header(unit, (struct ioc_toc_header *) addr);
621 case CDIOREADTOCENTRYS:
622 return mcd_toc_entrys(unit, (struct ioc_read_toc_entry *) addr);
624 return mcd_resume(unit);
626 return mcd_pause(unit);
628 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
632 return mcd_stop(unit);
639 /* this could have been taken from scsi/cd.c, but it is not clear
640 * whether the scsi cd driver is linked in
642 static int mcd_getdisklabel(int unit)
644 struct mcd_data *cd = mcd_data + unit;
646 if (cd->flags & MCDLABEL)
649 bzero(&cd->dlabel,sizeof(struct disklabel));
650 /* filled with spaces first */
651 strncpy(cd->dlabel.d_typename," ",
652 sizeof(cd->dlabel.d_typename));
653 strncpy(cd->dlabel.d_typename, cd->name,
654 min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1));
655 strncpy(cd->dlabel.d_packname,"unknown ",
656 sizeof(cd->dlabel.d_packname));
657 cd->dlabel.d_secsize = cd->blksize;
658 cd->dlabel.d_nsectors = 100;
659 cd->dlabel.d_ntracks = 1;
660 cd->dlabel.d_ncylinders = (cd->disksize/100)+1;
661 cd->dlabel.d_secpercyl = 100;
662 cd->dlabel.d_secperunit = cd->disksize;
663 cd->dlabel.d_rpm = 300;
664 cd->dlabel.d_interleave = 1;
665 cd->dlabel.d_flags = D_REMOVABLE;
666 cd->dlabel.d_npartitions= 1;
667 cd->dlabel.d_partitions[0].p_offset = 0;
668 cd->dlabel.d_partitions[0].p_size = cd->disksize;
669 cd->dlabel.d_partitions[0].p_fstype = 9;
670 cd->dlabel.d_magic = DISKMAGIC;
671 cd->dlabel.d_magic2 = DISKMAGIC;
672 cd->dlabel.d_checksum = dkcksum(&cd->dlabel);
674 cd->flags |= MCDLABEL;
678 int mcdsize(dev_t dev)
681 int unit = mcd_unit(dev);
682 struct mcd_data *cd = mcd_data + unit;
684 if (mcd_volinfo(unit) == 0) {
685 cd->blksize = MCDBLK;
686 size = msf2hsg(cd->volinfo.vol_msf, 0);
687 cd->disksize = size * (MCDBLK/DEV_BSIZE);
693 /***************************************************************
694 * lower level of driver starts here
695 **************************************************************/
700 0x00,0x00,0x10,0x20,0x00,0x30,0x00,0x00,
701 0x00,0x10,0x40,0x50,0x00,0x00,0x00,0x00
706 0x00,0x01,0x00,0x03,0x00,0x05,0x06,0x07,
712 mcd_configure(struct mcd_data *cd)
714 outb(cd->iobase+mcd_config,cd->config);
718 /* Wait for non-busy - return 0 on timeout */
720 twiddle_thumbs(int port, int unit, int count, char *whine)
724 for (i = 0; i < count; i++) {
725 if (!(inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL))
729 printf("mcd%d: timeout %s\n", unit, whine);
733 /* check to see if a Mitsumi CD-ROM is attached to the ISA bus */
736 mcd_probe(struct isa_device *dev)
738 int port = dev->id_iobase;
739 int unit = dev->id_unit;
741 unsigned char stbytes[3];
743 mcd_data[unit].flags = MCDPROBING;
746 /* get irq/drq configuration word */
747 mcd_data[unit].config = irqs[dev->id_irq]; /* | drqs[dev->id_drq];*/
749 mcd_data[unit].config = 0;
753 outb(port+MCD_FLAGS, M_RESET);
756 * delay awhile by getting any pending garbage (old data) and
759 for (i = 1000000; i != 0; i--)
763 outb(port+MCD_DATA, MCD_CMDGETSTAT);
764 if (!twiddle_thumbs(port, unit, 1000000, "getting status"))
765 return 0; /* Timeout */
766 /* Get version information */
767 outb(port+MCD_DATA, MCD_CMDCONTINFO);
768 for (j = 0; j < 3; j++) {
769 if (!twiddle_thumbs(port, unit, 3000, "getting version info"))
771 stbytes[j] = (inb(port+MCD_DATA) & 0xFF);
773 if (stbytes[1] == stbytes[2])
775 if (stbytes[2] >= 4 || stbytes[1] != 'M') {
776 outb(port+MCD_CTRL, M_PICKLE);
777 mcd_data[unit].flags |= MCDNEWMODEL;
779 mcd_data[unit].read_command = MCD_CMDSINGLESPEEDREAD;
780 switch (stbytes[1]) {
782 if (stbytes[2] <= 2) {
783 mcd_data[unit].type = MCD_TYPE_LU002S;
784 mcd_data[unit].name = "Mitsumi LU002S";
785 } else if (stbytes[2] <= 5) {
786 mcd_data[unit].type = MCD_TYPE_LU005S;
787 mcd_data[unit].name = "Mitsumi LU005S";
789 mcd_data[unit].type = MCD_TYPE_LU006S;
790 mcd_data[unit].name = "Mitsumi LU006S";
794 mcd_data[unit].type = MCD_TYPE_FX001;
795 mcd_data[unit].name = "Mitsumi FX001";
798 mcd_data[unit].type = MCD_TYPE_FX001D;
799 mcd_data[unit].name = "Mitsumi FX001D";
800 mcd_data[unit].read_command = MCD_CMDDOUBLESPEEDREAD;
803 mcd_data[unit].type = MCD_TYPE_UNKNOWN;
804 mcd_data[unit].name = "Mitsumi ???";
807 printf("mcd%d: type %s, version info: %c %x\n", unit, mcd_data[unit].name,
808 stbytes[1], stbytes[2]);
815 mcd_waitrdy(int port,int dly)
819 /* wait until flag port senses status ready */
820 for (i=0; i<dly; i+=MIN_DELAY) {
821 if (!(inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL))
829 mcd_getreply(int unit,int dly)
831 struct mcd_data *cd = mcd_data + unit;
832 int port = cd->iobase;
834 /* wait data to become ready */
835 if (mcd_waitrdy(port,dly)<0) {
836 printf("mcd%d: timeout getreply\n",unit);
841 return inb(port+mcd_status) & 0xFF;
845 mcd_getstat(int unit,int sflg)
848 struct mcd_data *cd = mcd_data + unit;
849 int port = cd->iobase;
853 outb(port+mcd_command, MCD_CMDGETSTAT);
854 i = mcd_getreply(unit,DELAY_GETREPLY);
855 if (i<0 || (i & MCD_ST_CMDCHECK)) {
856 cd->curr_mode = MCD_MD_UNKNOWN;
862 if (mcd_setflags(unit,cd) < 0)
868 mcd_setflags(int unit, struct mcd_data *cd)
871 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
872 || !(cd->status & MCDDSKIN)) {
873 MCD_TRACE("setflags: sensed DSKCHNG or DOOROPEN or !DSKIN\n");
874 mcd_soft_reset(unit);
878 if (cd->status & MCDAUDIOBSY)
879 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
880 else if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS)
881 cd->audio_status = CD_AS_PLAY_COMPLETED;
886 mcd_get(int unit, char *buf, int nmax)
890 for (i=0; i<nmax; i++) {
892 if ((k = mcd_getreply(unit,DELAY_GETREPLY)) < 0) {
893 printf("mcd%d: timeout mcd_get\n",unit);
902 mcd_send(int unit, int cmd,int nretrys)
905 int port = mcd_data[unit].iobase;
907 /*MCD_TRACE("mcd_send: command = 0x%02x\n",cmd,0,0,0);*/
908 for (i=0; i<nretrys; i++) {
909 outb(port+mcd_command, cmd);
910 if ((k=mcd_getstat(unit,0)) != -1)
914 printf("mcd%d: media changed\n",unit);
918 printf("mcd%d: mcd_send retry cnt exceeded\n",unit);
921 /*MCD_TRACE("mcd_send: done\n",0,0,0,0);*/
926 hsg2msf(int hsg, bcd_t *msf)
929 F_msf(msf) = bin2bcd(hsg % 75);
931 S_msf(msf) = bin2bcd(hsg % 60);
933 M_msf(msf) = bin2bcd(hsg);
937 msf2hsg(bcd_t *msf, int relative)
939 return (bcd2bin(M_msf(msf)) * 60 + bcd2bin(S_msf(msf))) * 75 +
940 bcd2bin(F_msf(msf)) - (!relative) * 150;
944 mcd_volinfo(int unit)
946 struct mcd_data *cd = mcd_data + unit;
948 /* Just return if we already have it */
949 if (cd->flags & MCDVOLINFO) return 0;
951 /*MCD_TRACE("mcd_volinfo: enter\n",0,0,0,0);*/
953 /* send volume info command */
954 if (mcd_send(unit,MCD_CMDGETVOLINFO,MCD_RETRYS) < 0)
958 if (mcd_get(unit,(char*) &cd->volinfo,sizeof(struct mcd_volinfo)) < 0) {
959 printf("mcd%d: mcd_volinfo: error read data\n",unit);
963 if (cd->volinfo.trk_low > 0 &&
964 cd->volinfo.trk_high >= cd->volinfo.trk_low
966 cd->flags |= MCDVOLINFO; /* volinfo is OK */
976 int unit = (int)dummy;
977 MCD_TRACE("stray interrupt\n");
980 /* state machine to process read requests
981 * initialize with MCD_S_BEGIN: calculate sizes, and read status
982 * MCD_S_WAITSTAT: wait for status reply, set mode
983 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
984 * MCD_S_WAITREAD: wait for read ready, read data
986 static struct mcd_mbx *mbxsave;
989 mcd_timeout(void *arg)
991 mcd_doread((int)arg, mbxsave);
995 mcd_doread(int state, struct mcd_mbx *mbxin)
997 struct mcd_mbx *mbx = (state!=MCD_S_BEGIN) ? mbxsave : mbxin;
998 int unit = mbx->unit;
999 int port = mbx->port;
1000 int com_port = mbx->port + mcd_command;
1001 int data_port = mbx->port + mcd_rdata;
1002 struct bio *bio = mbx->bio;
1003 struct buf *bp = bio->bio_buf;
1004 struct mcd_data *cd = mcd_data + unit;
1007 struct mcd_read2 rbuf;
1014 mbx = mbxsave = mbxin;
1019 outb(com_port, MCD_CMDGETSTAT);
1020 mbx->count = RDELAY_WAITSTAT;
1021 callout_reset(&cd->callout, hz / 100,
1022 mcd_timeout, (void *)MCD_S_WAITSTAT);
1024 case MCD_S_WAITSTAT:
1025 callout_stop(&cd->callout);
1026 if (mbx->count-- >= 0) {
1027 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) {
1029 callout_reset(&cd->callout, hz / 100,
1030 mcd_timeout, (void *)MCD_S_WAITSTAT);
1033 cd->status = inb(port+mcd_status) & 0xFF;
1034 if (cd->status & MCD_ST_CMDCHECK)
1036 if (mcd_setflags(unit,cd) < 0)
1038 MCD_TRACE("got WAITSTAT delay=%d\n",
1039 RDELAY_WAITSTAT-mbx->count);
1040 /* reject, if audio active */
1041 if (cd->status & MCDAUDIOBSY) {
1042 printf("mcd%d: audio is active\n",unit);
1047 /* to check for raw/cooked mode */
1048 if (cd->flags & MCDREADRAW) {
1053 mbx->sz = cd->blksize;
1056 if (rm == cd->curr_mode)
1059 mbx->count = RDELAY_WAITMODE;
1061 cd->curr_mode = MCD_MD_UNKNOWN;
1063 mcd_put(com_port, MCD_CMDSETMODE);
1064 mcd_put(com_port, rm);
1066 callout_reset(&cd->callout, hz / 100,
1067 mcd_timeout, (void *)MCD_S_WAITMODE);
1070 printf("mcd%d: timeout getstatus\n",unit);
1074 case MCD_S_WAITMODE:
1075 callout_stop(&cd->callout);
1076 if (mbx->count-- < 0) {
1077 printf("mcd%d: timeout set mode\n",unit);
1080 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) {
1081 callout_reset(&cd->callout, hz / 100,
1082 mcd_timeout, (void *)MCD_S_WAITMODE);
1085 cd->status = inb(port+mcd_status) & 0xFF;
1086 if (cd->status & MCD_ST_CMDCHECK) {
1087 cd->curr_mode = MCD_MD_UNKNOWN;
1090 if (mcd_setflags(unit,cd) < 0)
1092 cd->curr_mode = mbx->mode;
1093 MCD_TRACE("got WAITMODE delay=%d\n",
1094 RDELAY_WAITMODE-mbx->count);
1096 /* for first block */
1097 mbx->nblk = (bp->b_bcount + (mbx->sz-1)) / mbx->sz;
1101 blknum = (bio->bio_offset / mbx->sz)
1102 + mbx->p_offset + mbx->skip/mbx->sz;
1104 MCD_TRACE("mcd_doread: read blknum=%d for bp=%p\n",
1107 /* build parameter block */
1108 hsg2msf(blknum,rbuf.start_msf);
1110 /* send the read command */
1112 mcd_put(com_port,cd->read_command);
1113 mcd_put(com_port,rbuf.start_msf[0]);
1114 mcd_put(com_port,rbuf.start_msf[1]);
1115 mcd_put(com_port,rbuf.start_msf[2]);
1116 mcd_put(com_port,0);
1117 mcd_put(com_port,0);
1118 mcd_put(com_port,1);
1121 /* Spin briefly (<= 2ms) to avoid missing next block */
1122 for (i = 0; i < 20; i++) {
1123 k = inb(port+MCD_FLAGS);
1124 if (!(k & MFL_DATA_NOT_AVAIL))
1129 mbx->count = RDELAY_WAITREAD;
1130 callout_reset(&cd->callout, hz / 100,
1131 mcd_timeout, (void *)MCD_S_WAITREAD);
1133 case MCD_S_WAITREAD:
1134 callout_stop(&cd->callout);
1135 if (mbx->count-- > 0) {
1136 k = inb(port+MCD_FLAGS);
1137 if (!(k & MFL_DATA_NOT_AVAIL)) { /* XXX */
1138 MCD_TRACE("got data delay=%d\n",
1139 RDELAY_WAITREAD-mbx->count);
1142 addr = bp->b_data + mbx->skip;
1144 outb(port+mcd_ctl2,0x04); /* XXX */
1145 for (i=0; i<mbx->sz; i++)
1146 *addr++ = inb(data_port);
1147 outb(port+mcd_ctl2,0x0c); /* XXX */
1149 k = inb(port+MCD_FLAGS);
1150 /* If we still have some junk, read it too */
1151 if (!(k & MFL_DATA_NOT_AVAIL)) {
1152 outb(port+mcd_ctl2,0x04); /* XXX */
1153 (void)inb(data_port);
1154 (void)inb(data_port);
1155 outb(port+mcd_ctl2,0x0c); /* XXX */
1158 if (--mbx->nblk > 0) {
1159 mbx->skip += mbx->sz;
1167 cd->flags &= ~(MCDMBXBSY|MCDREADRAW);
1168 mcd_start(mbx->unit);
1171 if (!(k & MFL_STATUS_NOT_AVAIL)) {
1172 cd->status = inb(port+mcd_status) & 0xFF;
1173 if (cd->status & MCD_ST_CMDCHECK)
1175 if (mcd_setflags(unit,cd) < 0)
1178 callout_reset(&cd->callout, hz / 100,
1179 mcd_timeout, (void *)MCD_S_WAITREAD);
1182 printf("mcd%d: timeout read data\n",unit);
1188 if (mbx->retry-- > 0) {
1189 printf("mcd%d: retrying\n",unit);
1190 state = MCD_S_BEGIN1;
1194 /* invalidate the buffer */
1195 bp->b_flags |= B_ERROR;
1196 bp->b_resid = bp->b_bcount;
1199 cd->flags &= ~(MCDMBXBSY|MCDREADRAW);
1200 mcd_start(mbx->unit);
1204 printf("mcd%d: media changed\n", unit);
1208 printf("mcd%d: unit timeout, resetting\n",mbx->unit);
1209 outb(mbx->port+mcd_reset,MCD_CMDRESET);
1211 (void)mcd_getstat(mbx->unit,1);
1212 (void)mcd_getstat(mbx->unit,1);
1213 /*cd->status &= ~MCDDSKCHNG; */
1214 cd->debug = 1; /* preventive set debug mode */
1221 mcd_lock_door(int unit, int lock)
1223 struct mcd_data *cd = mcd_data + unit;
1224 int port = cd->iobase;
1226 outb(port+mcd_command, MCD_CMDLOCKDRV);
1227 outb(port+mcd_command, lock);
1228 if (mcd_getstat(unit,0) == -1)
1234 mcd_close_tray(int unit)
1236 struct mcd_data *cd = mcd_data + unit;
1237 int port = cd->iobase;
1240 if (mcd_getstat(unit,1) == -1)
1242 if (cd->status & MCDDOOROPEN) {
1243 outb(port+mcd_command, MCD_CMDCLOSETRAY);
1244 for (retry = 0; retry < CLOSE_TRAY_SECS * WAIT_FRAC; retry++) {
1245 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL)
1246 (void) tsleep((caddr_t)cd, PCATCH, "mcdcls", hz/WAIT_FRAC);
1248 if ((r = mcd_getstat(unit,0)) == -1)
1261 struct mcd_data *cd = mcd_data + unit;
1262 int port = cd->iobase, r;
1264 if (mcd_getstat(unit,1) == -1) /* detect disk change too */
1266 if (cd->status & MCDDOOROPEN)
1268 if ((r = mcd_stop(unit)) == EIO)
1270 outb(port+mcd_command, MCD_CMDEJECTDISK);
1271 if (mcd_getstat(unit,0) == -1)
1277 mcd_inject(int unit)
1279 struct mcd_data *cd = mcd_data + unit;
1281 if (mcd_getstat(unit,1) == -1) /* detect disk change too */
1283 if (cd->status & MCDDOOROPEN)
1284 return mcd_close_tray(unit);
1289 mcd_hard_reset(int unit)
1291 struct mcd_data *cd = mcd_data + unit;
1292 int port = cd->iobase;
1294 outb(port+mcd_reset,MCD_CMDRESET);
1295 cd->curr_mode = MCD_MD_UNKNOWN;
1296 cd->audio_status = CD_AS_AUDIO_INVALID;
1301 mcd_soft_reset(int unit)
1303 struct mcd_data *cd = mcd_data + unit;
1306 cd->flags &= (MCDINIT|MCDPROBING|MCDNEWMODEL);
1307 cd->curr_mode = MCD_MD_UNKNOWN;
1308 for (i=0; i<MAXPARTITIONS; i++) cd->partflags[i] = 0;
1309 cd->audio_status = CD_AS_AUDIO_INVALID;
1313 mcd_setmode(int unit, int mode)
1315 struct mcd_data *cd = mcd_data + unit;
1316 int port = cd->iobase;
1319 if (cd->curr_mode == mode)
1322 printf("mcd%d: setting mode to %d\n", unit, mode);
1323 for(retry=0; retry<MCD_RETRYS; retry++)
1325 cd->curr_mode = MCD_MD_UNKNOWN;
1326 outb(port+mcd_command, MCD_CMDSETMODE);
1327 outb(port+mcd_command, mode);
1328 if ((st = mcd_getstat(unit, 0)) >= 0) {
1329 cd->curr_mode = mode;
1333 printf("mcd%d: media changed\n", unit);
1342 mcd_toc_header(int unit, struct ioc_toc_header *th)
1344 struct mcd_data *cd = mcd_data + unit;
1347 if ((r = mcd_volinfo(unit)) != 0)
1350 th->starting_track = bcd2bin(cd->volinfo.trk_low);
1351 th->ending_track = bcd2bin(cd->volinfo.trk_high);
1352 th->len = 2 * sizeof(u_char) /* start & end tracks */ +
1353 (th->ending_track + 1 - th->starting_track + 1) *
1354 sizeof(struct cd_toc_entry);
1360 mcd_read_toc(int unit)
1362 struct mcd_data *cd = mcd_data + unit;
1363 struct ioc_toc_header th;
1364 struct mcd_qchninfo q;
1365 int rc, trk, idx, retry;
1367 /* Only read TOC if needed */
1368 if (cd->flags & MCDTOC)
1372 printf("mcd%d: reading toc header\n", unit);
1374 if ((rc = mcd_toc_header(unit, &th)) != 0)
1377 if (mcd_send(unit, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
1380 if (mcd_setmode(unit, MCD_MD_TOC) != 0)
1384 printf("mcd%d: get_toc reading qchannel info\n",unit);
1386 for(trk=th.starting_track; trk<=th.ending_track; trk++)
1387 cd->toc[trk].idx_no = 0;
1388 trk = th.ending_track - th.starting_track + 1;
1389 for(retry=0; retry<600 && trk>0; retry++)
1391 if (mcd_getqchan(unit, &q) < 0) break;
1392 idx = bcd2bin(q.idx_no);
1393 if (idx>=th.starting_track && idx<=th.ending_track && q.trk_no==0) {
1394 if (cd->toc[idx].idx_no == 0) {
1401 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1407 /* add a fake last+1 */
1408 idx = th.ending_track + 1;
1409 cd->toc[idx].control = cd->toc[idx-1].control;
1410 cd->toc[idx].addr_type = cd->toc[idx-1].addr_type;
1411 cd->toc[idx].trk_no = 0;
1412 cd->toc[idx].idx_no = MCD_LASTPLUS1;
1413 cd->toc[idx].hd_pos_msf[0] = cd->volinfo.vol_msf[0];
1414 cd->toc[idx].hd_pos_msf[1] = cd->volinfo.vol_msf[1];
1415 cd->toc[idx].hd_pos_msf[2] = cd->volinfo.vol_msf[2];
1419 for (i = th.starting_track; i <= idx; i++)
1420 printf("mcd%d: trk %d idx %d pos %d %d %d\n",
1422 cd->toc[i].idx_no > 0x99 ? cd->toc[i].idx_no :
1423 bcd2bin(cd->toc[i].idx_no),
1424 bcd2bin(cd->toc[i].hd_pos_msf[0]),
1425 bcd2bin(cd->toc[i].hd_pos_msf[1]),
1426 bcd2bin(cd->toc[i].hd_pos_msf[2]));
1429 cd->flags |= MCDTOC;
1436 mcd_toc_entry(int unit, struct ioc_read_toc_single_entry *te)
1438 struct mcd_data *cd = mcd_data + unit;
1439 struct ioc_toc_header th;
1442 if (te->address_format != CD_MSF_FORMAT
1443 && te->address_format != CD_LBA_FORMAT)
1446 /* Copy the toc header */
1447 if ((rc = mcd_toc_header(unit, &th)) != 0)
1450 /* verify starting track */
1453 trk = th.starting_track;
1454 else if (trk == MCD_LASTPLUS1)
1455 trk = th.ending_track + 1;
1456 else if (trk < th.starting_track || trk > th.ending_track + 1)
1459 /* Make sure we have a valid toc */
1460 if ((rc=mcd_read_toc(unit)) != 0)
1463 /* Copy the TOC data. */
1464 if (cd->toc[trk].idx_no == 0)
1467 te->entry.control = cd->toc[trk].control;
1468 te->entry.addr_type = cd->toc[trk].addr_type;
1470 cd->toc[trk].idx_no > 0x99 ? cd->toc[trk].idx_no :
1471 bcd2bin(cd->toc[trk].idx_no);
1472 switch (te->address_format) {
1474 te->entry.addr.msf.unused = 0;
1475 te->entry.addr.msf.minute = bcd2bin(cd->toc[trk].hd_pos_msf[0]);
1476 te->entry.addr.msf.second = bcd2bin(cd->toc[trk].hd_pos_msf[1]);
1477 te->entry.addr.msf.frame = bcd2bin(cd->toc[trk].hd_pos_msf[2]);
1480 te->entry.addr.lba = htonl(msf2hsg(cd->toc[trk].hd_pos_msf, 0));
1488 mcd_toc_entrys(int unit, struct ioc_read_toc_entry *te)
1490 struct mcd_data *cd = mcd_data + unit;
1491 struct cd_toc_entry entries[MCD_MAXTOCS];
1492 struct ioc_toc_header th;
1493 int rc, n, trk, len;
1495 if ( te->data_len < sizeof(entries[0])
1496 || (te->data_len % sizeof(entries[0])) != 0
1497 || (te->address_format != CD_MSF_FORMAT
1498 && te->address_format != CD_LBA_FORMAT)
1502 /* Copy the toc header */
1503 if ((rc = mcd_toc_header(unit, &th)) != 0)
1506 /* verify starting track */
1507 trk = te->starting_track;
1509 trk = th.starting_track;
1510 else if (trk == MCD_LASTPLUS1)
1511 trk = th.ending_track + 1;
1512 else if (trk < th.starting_track || trk > th.ending_track + 1)
1515 len = ((th.ending_track + 1 - trk) + 1) *
1517 if (te->data_len < len)
1519 if (len > sizeof(entries))
1522 /* Make sure we have a valid toc */
1523 if ((rc=mcd_read_toc(unit)) != 0)
1526 /* Copy the TOC data. */
1527 for (n = 0; len > 0 && trk <= th.ending_track + 1; trk++) {
1528 if (cd->toc[trk].idx_no == 0)
1530 entries[n].control = cd->toc[trk].control;
1531 entries[n].addr_type = cd->toc[trk].addr_type;
1533 cd->toc[trk].idx_no > 0x99 ? cd->toc[trk].idx_no :
1534 bcd2bin(cd->toc[trk].idx_no);
1535 switch (te->address_format) {
1537 entries[n].addr.msf.unused = 0;
1538 entries[n].addr.msf.minute = bcd2bin(cd->toc[trk].hd_pos_msf[0]);
1539 entries[n].addr.msf.second = bcd2bin(cd->toc[trk].hd_pos_msf[1]);
1540 entries[n].addr.msf.frame = bcd2bin(cd->toc[trk].hd_pos_msf[2]);
1543 entries[n].addr.lba = htonl(msf2hsg(cd->toc[trk].hd_pos_msf, 0));
1546 len -= sizeof(struct cd_toc_entry);
1550 /* copy the data back */
1551 return copyout(entries, te->data, n * sizeof(struct cd_toc_entry));
1557 struct mcd_data *cd = mcd_data + unit;
1559 /* Verify current status */
1560 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS &&
1561 cd->audio_status != CD_AS_PLAY_PAUSED &&
1562 cd->audio_status != CD_AS_PLAY_COMPLETED) {
1564 printf("mcd%d: stop attempted when not playing, audio status %d\n",
1565 unit, cd->audio_status);
1568 if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS)
1569 if (mcd_send(unit, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
1571 cd->audio_status = CD_AS_PLAY_COMPLETED;
1576 mcd_getqchan(int unit, struct mcd_qchninfo *q)
1578 struct mcd_data *cd = mcd_data + unit;
1580 if (mcd_send(unit, MCD_CMDGETQCHN, MCD_RETRYS) < 0)
1582 if (mcd_get(unit, (char *) q, sizeof(struct mcd_qchninfo)) < 0)
1585 printf("mcd%d: getqchan control=0x%x addr_type=0x%x trk=%d ind=%d ttm=%d:%d.%d dtm=%d:%d.%d\n",
1587 q->control, q->addr_type, bcd2bin(q->trk_no),
1589 bcd2bin(q->trk_size_msf[0]), bcd2bin(q->trk_size_msf[1]),
1590 bcd2bin(q->trk_size_msf[2]),
1591 bcd2bin(q->hd_pos_msf[0]), bcd2bin(q->hd_pos_msf[1]),
1592 bcd2bin(q->hd_pos_msf[2]));
1598 mcd_subchan(int unit, struct ioc_read_subchannel *sc)
1600 struct mcd_data *cd = mcd_data + unit;
1601 struct mcd_qchninfo q;
1602 struct cd_sub_channel_info data;
1606 printf("mcd%d: subchan af=%d, df=%d\n", unit,
1610 if (sc->address_format != CD_MSF_FORMAT &&
1611 sc->address_format != CD_LBA_FORMAT)
1614 if (sc->data_format != CD_CURRENT_POSITION &&
1615 sc->data_format != CD_MEDIA_CATALOG)
1618 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1621 if (mcd_getqchan(unit, &q) < 0)
1624 data.header.audio_status = cd->audio_status;
1625 data.what.position.data_format = sc->data_format;
1627 switch (sc->data_format) {
1628 case CD_MEDIA_CATALOG:
1629 data.what.media_catalog.mc_valid = 1;
1630 data.what.media_catalog.mc_number[0] = '\0';
1633 case CD_CURRENT_POSITION:
1634 data.what.position.control = q.control;
1635 data.what.position.addr_type = q.addr_type;
1636 data.what.position.track_number = bcd2bin(q.trk_no);
1637 data.what.position.index_number = bcd2bin(q.idx_no);
1638 switch (sc->address_format) {
1640 data.what.position.reladdr.msf.unused = 0;
1641 data.what.position.reladdr.msf.minute = bcd2bin(q.trk_size_msf[0]);
1642 data.what.position.reladdr.msf.second = bcd2bin(q.trk_size_msf[1]);
1643 data.what.position.reladdr.msf.frame = bcd2bin(q.trk_size_msf[2]);
1644 data.what.position.absaddr.msf.unused = 0;
1645 data.what.position.absaddr.msf.minute = bcd2bin(q.hd_pos_msf[0]);
1646 data.what.position.absaddr.msf.second = bcd2bin(q.hd_pos_msf[1]);
1647 data.what.position.absaddr.msf.frame = bcd2bin(q.hd_pos_msf[2]);
1650 lba = msf2hsg(q.trk_size_msf, 1);
1652 * Pre-gap has index number of 0, and decreasing MSF
1653 * address. Must be converted to negative LBA, per
1656 if (data.what.position.index_number == 0)
1658 data.what.position.reladdr.lba = htonl(lba);
1659 data.what.position.absaddr.lba = htonl(msf2hsg(q.hd_pos_msf, 0));
1665 return copyout(&data, sc->data, min(sizeof(struct cd_sub_channel_info), sc->data_len));
1669 mcd_playmsf(int unit, struct ioc_play_msf *p)
1671 struct mcd_data *cd = mcd_data + unit;
1672 struct mcd_read2 pb;
1675 printf("mcd%d: playmsf: from %d:%d.%d to %d:%d.%d\n",
1677 p->start_m, p->start_s, p->start_f,
1678 p->end_m, p->end_s, p->end_f);
1680 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
1681 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) ||
1682 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) >
1683 M_msf(cd->volinfo.vol_msf) * 60 * 75 +
1684 S_msf(cd->volinfo.vol_msf) * 75 +
1685 F_msf(cd->volinfo.vol_msf))
1688 pb.start_msf[0] = bin2bcd(p->start_m);
1689 pb.start_msf[1] = bin2bcd(p->start_s);
1690 pb.start_msf[2] = bin2bcd(p->start_f);
1691 pb.end_msf[0] = bin2bcd(p->end_m);
1692 pb.end_msf[1] = bin2bcd(p->end_s);
1693 pb.end_msf[2] = bin2bcd(p->end_f);
1695 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1698 return mcd_play(unit, &pb);
1702 mcd_playtracks(int unit, struct ioc_play_track *pt)
1704 struct mcd_data *cd = mcd_data + unit;
1705 struct mcd_read2 pb;
1706 int a = pt->start_track;
1707 int z = pt->end_track;
1710 if ((rc = mcd_read_toc(unit)) != 0)
1714 printf("mcd%d: playtracks from %d:%d to %d:%d\n", unit,
1715 a, pt->start_index, z, pt->end_index);
1717 if ( a < bcd2bin(cd->volinfo.trk_low)
1718 || a > bcd2bin(cd->volinfo.trk_high)
1720 || z < bcd2bin(cd->volinfo.trk_low)
1721 || z > bcd2bin(cd->volinfo.trk_high))
1724 for (i = 0; i < 3; i++) {
1725 pb.start_msf[i] = cd->toc[a].hd_pos_msf[i];
1726 pb.end_msf[i] = cd->toc[z+1].hd_pos_msf[i];
1729 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1732 return mcd_play(unit, &pb);
1736 mcd_playblocks(int unit, struct ioc_play_blocks *p)
1738 struct mcd_data *cd = mcd_data + unit;
1739 struct mcd_read2 pb;
1742 printf("mcd%d: playblocks: blkno %d length %d\n",
1743 unit, p->blk, p->len);
1745 if (p->blk > cd->disksize || p->len > cd->disksize ||
1746 p->blk < 0 || p->len < 0 ||
1747 (p->blk + p->len) > cd->disksize)
1750 hsg2msf(p->blk, pb.start_msf);
1751 hsg2msf(p->blk + p->len, pb.end_msf);
1753 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1756 return mcd_play(unit, &pb);
1760 mcd_play(int unit, struct mcd_read2 *pb)
1762 struct mcd_data *cd = mcd_data + unit;
1763 int com_port = cd->iobase + mcd_command;
1764 int retry, st = -1, status;
1767 for(retry=0; retry<MCD_RETRYS; retry++) {
1770 outb(com_port, MCD_CMDSINGLESPEEDREAD);
1771 outb(com_port, pb->start_msf[0]);
1772 outb(com_port, pb->start_msf[1]);
1773 outb(com_port, pb->start_msf[2]);
1774 outb(com_port, pb->end_msf[0]);
1775 outb(com_port, pb->end_msf[1]);
1776 outb(com_port, pb->end_msf[2]);
1779 status=mcd_getstat(unit, 0);
1782 else if (status != -2)
1788 printf("mcd%d: media changed\n", unit);
1792 printf("mcd%d: mcd_play retry=%d, status=0x%02x\n", unit, retry, status);
1795 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
1802 struct mcd_data *cd = mcd_data + unit;
1803 struct mcd_qchninfo q;
1806 /* Verify current status */
1807 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS &&
1808 cd->audio_status != CD_AS_PLAY_PAUSED) {
1810 printf("mcd%d: pause attempted when not playing, audio status %d\n",
1811 unit, cd->audio_status);
1815 /* Get the current position */
1816 if (mcd_getqchan(unit, &q) < 0)
1819 /* Copy it into lastpb */
1820 cd->lastpb.start_msf[0] = q.hd_pos_msf[0];
1821 cd->lastpb.start_msf[1] = q.hd_pos_msf[1];
1822 cd->lastpb.start_msf[2] = q.hd_pos_msf[2];
1825 if ((rc=mcd_stop(unit)) != 0)
1828 /* Set the proper status and exit */
1829 cd->audio_status = CD_AS_PLAY_PAUSED;
1834 mcd_resume(int unit)
1836 struct mcd_data *cd = mcd_data + unit;
1838 if (cd->audio_status != CD_AS_PLAY_PAUSED)
1840 return mcd_play(unit, &cd->lastpb);