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.16 2006/03/24 18:35:32 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_psize_t mcdsize;
208 static d_strategy_t mcdstrategy;
210 static struct cdevsw mcd_cdevsw = {
212 /* maj */ MCD_CDEV_MAJOR,
218 /* close */ mcdclose,
221 /* ioctl */ mcdioctl,
224 /* strategy */ mcdstrategy,
229 #define mcd_put(port,byte) outb(port,byte)
232 #define MCD_RDRETRYS 8
234 #define CLOSE_TRAY_SECS 8
235 #define DISK_SENSE_SECS 3
239 #define RDELAY_WAITSTAT 300
240 #define RDELAY_WAITMODE 300
241 #define RDELAY_WAITREAD 800
244 #define DELAY_GETREPLY 5000000
246 int mcd_attach(struct isa_device *dev)
248 int unit = dev->id_unit;
249 struct mcd_data *cd = mcd_data + unit;
251 dev->id_intr = (inthand2_t *)mcdintr;
252 cd->iobase = dev->id_iobase;
253 cd->flags |= MCDINIT;
254 callout_init(&cd->callout);
255 mcd_soft_reset(unit);
256 bioq_init(&cd->bio_queue);
259 /* wire controller for interrupts and dma */
262 /* name filled in probe */
263 cdevsw_add(&mcd_cdevsw, dkunitmask(), dkmakeunit(unit));
264 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, 0),
265 UID_ROOT, GID_OPERATOR, 0640, "rmcd%da", unit);
266 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
267 UID_ROOT, GID_OPERATOR, 0640, "rmcd%dc", unit);
268 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, 0),
269 UID_ROOT, GID_OPERATOR, 0640, "mcd%da", unit);
270 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
271 UID_ROOT, GID_OPERATOR, 0640, "mcd%dc", unit);
275 int mcdopen(dev_t dev, int flags, int fmt, struct thread *td)
277 int unit,part,phys,r,retry;
280 unit = mcd_unit(dev);
284 cd = mcd_data + unit;
285 part = mcd_part(dev);
286 phys = mcd_phys(dev);
289 if (!(cd->flags & MCDINIT))
292 /* invalidated in the meantime? mark all open part's invalid */
293 if (!(cd->flags & MCDVALID) && cd->openflags)
296 if (mcd_getstat(unit,1) == -1)
299 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
300 || !(cd->status & MCDDSKIN))
301 for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) {
302 (void) tsleep((caddr_t)cd, PCATCH, "mcdsn1", hz/WAIT_FRAC);
303 if ((r = mcd_getstat(unit,1)) == -1)
309 if (( (cd->status & (MCDDOOROPEN|MCDDSKCHNG))
310 || !(cd->status & MCDDSKIN)
312 && major(dev) == MCD_CDEV_MAJOR && part == RAW_PART
314 cd->openflags |= (1<<part);
316 cd->partflags[part] |= MCDREADRAW;
319 if (cd->status & MCDDOOROPEN) {
320 printf("mcd%d: door is open\n", unit);
323 if (!(cd->status & MCDDSKIN)) {
324 printf("mcd%d: no CD inside\n", unit);
327 if (cd->status & MCDDSKCHNG) {
328 printf("mcd%d: CD not sensed\n", unit);
332 if (mcdsize(dev) < 0) {
333 if (major(dev) == MCD_CDEV_MAJOR && part == RAW_PART) {
334 cd->openflags |= (1<<part);
336 cd->partflags[part] |= MCDREADRAW;
339 printf("mcd%d: failed to get disk size\n",unit);
342 cd->flags |= MCDVALID;
344 /* XXX get a default disklabel */
345 mcd_getdisklabel(unit);
347 MCD_TRACE("open: partition=%d, disksize = %ld, blksize=%d\n",
348 part, cd->disksize, cd->blksize);
350 dev->si_bsize_phys = cd->blksize;
352 if (part == RAW_PART ||
353 (part < cd->dlabel.d_npartitions &&
354 cd->dlabel.d_partitions[part].p_fstype != FS_UNUSED)) {
355 cd->openflags |= (1<<part);
356 if (part == RAW_PART && phys)
357 cd->partflags[part] |= MCDREADRAW;
358 (void) mcd_lock_door(unit, MCD_LK_LOCK);
359 if (!(cd->flags & MCDVALID))
367 int mcdclose(dev_t dev, int flags, int fmt, struct thread *td)
372 unit = mcd_unit(dev);
376 cd = mcd_data + unit;
377 part = mcd_part(dev);
379 if (!(cd->flags & MCDINIT) || !(cd->openflags & (1<<part)))
382 MCD_TRACE("close: partition=%d\n", part);
384 (void) mcd_lock_door(unit, MCD_LK_UNLOCK);
385 cd->openflags &= ~(1<<part);
386 cd->partflags[part] &= ~MCDREADRAW;
392 mcdstrategy(dev_t dev, struct bio *bio)
395 struct buf *bp = bio->bio_buf;
397 int unit = mcd_unit(dev);
399 cd = mcd_data + unit;
400 bio->bio_driver_info = dev;
403 /*MCD_TRACE("strategy: buf=0x%lx, unit=%ld, offset=%lld bcount=%ld\n",
404 bp,unit,bio->bio_offset,bp->b_bcount);*/
405 if (unit >= NMCD || bio->bio_offset < 0) {
406 printf("mcdstrategy: unit = %d, offset = %lld, bcount = %ld\n",
407 unit, bio->bio_offset, bp->b_bcount);
408 printf("mcd: mcdstratregy failure");
409 bp->b_error = EINVAL;
410 bp->b_flags |= B_ERROR;
414 /* if device invalidated (e.g. media change, door open), error */
415 if (!(cd->flags & MCDVALID)) {
416 MCD_TRACE("strategy: drive not valid\n");
422 if (!(bp->b_flags & B_READ)) {
427 /* no data to read */
428 if (bp->b_bcount == 0)
431 /* for non raw access, check partition limits */
432 if (mcd_part(dev) != RAW_PART) {
433 if (!(cd->flags & MCDLABEL)) {
437 /* adjust transfer if necessary */
438 nbio = bounds_check_with_label(dev, bio, &cd->dlabel, 1);
448 bioqdisksort(&cd->bio_queue, nbio);
451 /* now check whether we can perform processing */
456 * These cases occur before nbio is set, use bio.
459 bp->b_flags |= B_ERROR;
461 bp->b_resid = bp->b_bcount;
466 static void mcd_start(int unit)
468 struct mcd_data *cd = mcd_data + unit;
475 if (cd->flags & MCDMBXBSY) {
480 bio = bioq_first(&cd->bio_queue);
487 dev = bio->bio_driver_info;
489 /* block found to process, dequeue */
490 /*MCD_TRACE("mcd_start: found block bp=0x%x\n",bp,0,0,0);*/
491 bioq_remove(&cd->bio_queue, bio);
495 if (!(cd->flags & MCDVALID)) {
496 MCD_TRACE("mcd_start: drive not valid\n");
500 p = cd->dlabel.d_partitions + mcd_part(dev);
502 cd->flags |= MCDMBXBSY;
503 if (cd->partflags[mcd_part(dev)] & MCDREADRAW)
504 cd->flags |= MCDREADRAW;
506 cd->mbx.port = cd->iobase;
507 cd->mbx.retry = MCD_RETRYS;
509 cd->mbx.p_offset = p->p_offset;
511 /* calling the read routine */
512 mcd_doread(MCD_S_BEGIN,&(cd->mbx));
513 /* triggers mcd_start, when successful finished */
517 int mcdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
520 int unit,part,retry,r;
522 unit = mcd_unit(dev);
523 part = mcd_part(dev);
524 cd = mcd_data + unit;
526 if (mcd_getstat(unit, 1) == -1) /* detect disk change too */
528 MCD_TRACE("ioctl called 0x%lx\n", cmd);
541 return mcd_eject(unit);
549 return mcd_hard_reset(unit);
551 return mcd_lock_door(unit, MCD_LK_UNLOCK);
553 return mcd_lock_door(unit, MCD_LK_LOCK);
555 return mcd_inject(unit);
558 if (!(cd->flags & MCDVALID)) {
559 if ( major(dev) != MCD_CDEV_MAJOR
561 || !(cd->openflags & (1<<RAW_PART))
564 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
565 || !(cd->status & MCDDSKIN))
566 for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) {
567 (void) tsleep((caddr_t)cd, PCATCH, "mcdsn2", hz/WAIT_FRAC);
568 if ((r = mcd_getstat(unit,1)) == -1)
573 if ( (cd->status & (MCDDOOROPEN|MCDDSKCHNG))
574 || !(cd->status & MCDDSKIN)
578 cd->flags |= MCDVALID;
579 mcd_getdisklabel(unit);
581 cd->partflags[part] |= MCDREADRAW;
582 (void) mcd_lock_door(unit, MCD_LK_LOCK);
583 if (!(cd->flags & MCDVALID))
589 *(struct disklabel *) addr = cd->dlabel;
592 ((struct partinfo *) addr)->disklab = &cd->dlabel;
593 ((struct partinfo *) addr)->part =
594 &cd->dlabel.d_partitions[mcd_part(dev)];
598 * a bit silly, but someone might want to test something on a
603 if ((flags & FWRITE) == 0)
606 return setdisklabel(&cd->dlabel,
607 (struct disklabel *) addr,
612 case CDIOCPLAYTRACKS:
613 return mcd_playtracks(unit, (struct ioc_play_track *) addr);
614 case CDIOCPLAYBLOCKS:
615 return mcd_playblocks(unit, (struct ioc_play_blocks *) addr);
617 return mcd_playmsf(unit, (struct ioc_play_msf *) addr);
618 case CDIOCREADSUBCHANNEL:
619 return mcd_subchan(unit, (struct ioc_read_subchannel *) addr);
620 case CDIOREADTOCHEADER:
621 return mcd_toc_header(unit, (struct ioc_toc_header *) addr);
622 case CDIOREADTOCENTRYS:
623 return mcd_toc_entrys(unit, (struct ioc_read_toc_entry *) addr);
625 return mcd_resume(unit);
627 return mcd_pause(unit);
629 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
633 return mcd_stop(unit);
640 /* this could have been taken from scsi/cd.c, but it is not clear
641 * whether the scsi cd driver is linked in
643 static int mcd_getdisklabel(int unit)
645 struct mcd_data *cd = mcd_data + unit;
647 if (cd->flags & MCDLABEL)
650 bzero(&cd->dlabel,sizeof(struct disklabel));
651 /* filled with spaces first */
652 strncpy(cd->dlabel.d_typename," ",
653 sizeof(cd->dlabel.d_typename));
654 strncpy(cd->dlabel.d_typename, cd->name,
655 min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1));
656 strncpy(cd->dlabel.d_packname,"unknown ",
657 sizeof(cd->dlabel.d_packname));
658 cd->dlabel.d_secsize = cd->blksize;
659 cd->dlabel.d_nsectors = 100;
660 cd->dlabel.d_ntracks = 1;
661 cd->dlabel.d_ncylinders = (cd->disksize/100)+1;
662 cd->dlabel.d_secpercyl = 100;
663 cd->dlabel.d_secperunit = cd->disksize;
664 cd->dlabel.d_rpm = 300;
665 cd->dlabel.d_interleave = 1;
666 cd->dlabel.d_flags = D_REMOVABLE;
667 cd->dlabel.d_npartitions= 1;
668 cd->dlabel.d_partitions[0].p_offset = 0;
669 cd->dlabel.d_partitions[0].p_size = cd->disksize;
670 cd->dlabel.d_partitions[0].p_fstype = 9;
671 cd->dlabel.d_magic = DISKMAGIC;
672 cd->dlabel.d_magic2 = DISKMAGIC;
673 cd->dlabel.d_checksum = dkcksum(&cd->dlabel);
675 cd->flags |= MCDLABEL;
679 int mcdsize(dev_t dev)
682 int unit = mcd_unit(dev);
683 struct mcd_data *cd = mcd_data + unit;
685 if (mcd_volinfo(unit) == 0) {
686 cd->blksize = MCDBLK;
687 size = msf2hsg(cd->volinfo.vol_msf, 0);
688 cd->disksize = size * (MCDBLK/DEV_BSIZE);
694 /***************************************************************
695 * lower level of driver starts here
696 **************************************************************/
701 0x00,0x00,0x10,0x20,0x00,0x30,0x00,0x00,
702 0x00,0x10,0x40,0x50,0x00,0x00,0x00,0x00
707 0x00,0x01,0x00,0x03,0x00,0x05,0x06,0x07,
713 mcd_configure(struct mcd_data *cd)
715 outb(cd->iobase+mcd_config,cd->config);
719 /* Wait for non-busy - return 0 on timeout */
721 twiddle_thumbs(int port, int unit, int count, char *whine)
725 for (i = 0; i < count; i++) {
726 if (!(inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL))
730 printf("mcd%d: timeout %s\n", unit, whine);
734 /* check to see if a Mitsumi CD-ROM is attached to the ISA bus */
737 mcd_probe(struct isa_device *dev)
739 int port = dev->id_iobase;
740 int unit = dev->id_unit;
742 unsigned char stbytes[3];
744 mcd_data[unit].flags = MCDPROBING;
747 /* get irq/drq configuration word */
748 mcd_data[unit].config = irqs[dev->id_irq]; /* | drqs[dev->id_drq];*/
750 mcd_data[unit].config = 0;
754 outb(port+MCD_FLAGS, M_RESET);
757 * delay awhile by getting any pending garbage (old data) and
760 for (i = 1000000; i != 0; i--)
764 outb(port+MCD_DATA, MCD_CMDGETSTAT);
765 if (!twiddle_thumbs(port, unit, 1000000, "getting status"))
766 return 0; /* Timeout */
767 /* Get version information */
768 outb(port+MCD_DATA, MCD_CMDCONTINFO);
769 for (j = 0; j < 3; j++) {
770 if (!twiddle_thumbs(port, unit, 3000, "getting version info"))
772 stbytes[j] = (inb(port+MCD_DATA) & 0xFF);
774 if (stbytes[1] == stbytes[2])
776 if (stbytes[2] >= 4 || stbytes[1] != 'M') {
777 outb(port+MCD_CTRL, M_PICKLE);
778 mcd_data[unit].flags |= MCDNEWMODEL;
780 mcd_data[unit].read_command = MCD_CMDSINGLESPEEDREAD;
781 switch (stbytes[1]) {
783 if (stbytes[2] <= 2) {
784 mcd_data[unit].type = MCD_TYPE_LU002S;
785 mcd_data[unit].name = "Mitsumi LU002S";
786 } else if (stbytes[2] <= 5) {
787 mcd_data[unit].type = MCD_TYPE_LU005S;
788 mcd_data[unit].name = "Mitsumi LU005S";
790 mcd_data[unit].type = MCD_TYPE_LU006S;
791 mcd_data[unit].name = "Mitsumi LU006S";
795 mcd_data[unit].type = MCD_TYPE_FX001;
796 mcd_data[unit].name = "Mitsumi FX001";
799 mcd_data[unit].type = MCD_TYPE_FX001D;
800 mcd_data[unit].name = "Mitsumi FX001D";
801 mcd_data[unit].read_command = MCD_CMDDOUBLESPEEDREAD;
804 mcd_data[unit].type = MCD_TYPE_UNKNOWN;
805 mcd_data[unit].name = "Mitsumi ???";
808 printf("mcd%d: type %s, version info: %c %x\n", unit, mcd_data[unit].name,
809 stbytes[1], stbytes[2]);
816 mcd_waitrdy(int port,int dly)
820 /* wait until flag port senses status ready */
821 for (i=0; i<dly; i+=MIN_DELAY) {
822 if (!(inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL))
830 mcd_getreply(int unit,int dly)
832 struct mcd_data *cd = mcd_data + unit;
833 int port = cd->iobase;
835 /* wait data to become ready */
836 if (mcd_waitrdy(port,dly)<0) {
837 printf("mcd%d: timeout getreply\n",unit);
842 return inb(port+mcd_status) & 0xFF;
846 mcd_getstat(int unit,int sflg)
849 struct mcd_data *cd = mcd_data + unit;
850 int port = cd->iobase;
854 outb(port+mcd_command, MCD_CMDGETSTAT);
855 i = mcd_getreply(unit,DELAY_GETREPLY);
856 if (i<0 || (i & MCD_ST_CMDCHECK)) {
857 cd->curr_mode = MCD_MD_UNKNOWN;
863 if (mcd_setflags(unit,cd) < 0)
869 mcd_setflags(int unit, struct mcd_data *cd)
872 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
873 || !(cd->status & MCDDSKIN)) {
874 MCD_TRACE("setflags: sensed DSKCHNG or DOOROPEN or !DSKIN\n");
875 mcd_soft_reset(unit);
879 if (cd->status & MCDAUDIOBSY)
880 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
881 else if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS)
882 cd->audio_status = CD_AS_PLAY_COMPLETED;
887 mcd_get(int unit, char *buf, int nmax)
891 for (i=0; i<nmax; i++) {
893 if ((k = mcd_getreply(unit,DELAY_GETREPLY)) < 0) {
894 printf("mcd%d: timeout mcd_get\n",unit);
903 mcd_send(int unit, int cmd,int nretrys)
906 int port = mcd_data[unit].iobase;
908 /*MCD_TRACE("mcd_send: command = 0x%02x\n",cmd,0,0,0);*/
909 for (i=0; i<nretrys; i++) {
910 outb(port+mcd_command, cmd);
911 if ((k=mcd_getstat(unit,0)) != -1)
915 printf("mcd%d: media changed\n",unit);
919 printf("mcd%d: mcd_send retry cnt exceeded\n",unit);
922 /*MCD_TRACE("mcd_send: done\n",0,0,0,0);*/
927 hsg2msf(int hsg, bcd_t *msf)
930 F_msf(msf) = bin2bcd(hsg % 75);
932 S_msf(msf) = bin2bcd(hsg % 60);
934 M_msf(msf) = bin2bcd(hsg);
938 msf2hsg(bcd_t *msf, int relative)
940 return (bcd2bin(M_msf(msf)) * 60 + bcd2bin(S_msf(msf))) * 75 +
941 bcd2bin(F_msf(msf)) - (!relative) * 150;
945 mcd_volinfo(int unit)
947 struct mcd_data *cd = mcd_data + unit;
949 /* Just return if we already have it */
950 if (cd->flags & MCDVOLINFO) return 0;
952 /*MCD_TRACE("mcd_volinfo: enter\n",0,0,0,0);*/
954 /* send volume info command */
955 if (mcd_send(unit,MCD_CMDGETVOLINFO,MCD_RETRYS) < 0)
959 if (mcd_get(unit,(char*) &cd->volinfo,sizeof(struct mcd_volinfo)) < 0) {
960 printf("mcd%d: mcd_volinfo: error read data\n",unit);
964 if (cd->volinfo.trk_low > 0 &&
965 cd->volinfo.trk_high >= cd->volinfo.trk_low
967 cd->flags |= MCDVOLINFO; /* volinfo is OK */
977 int unit = (int)dummy;
978 MCD_TRACE("stray interrupt\n");
981 /* state machine to process read requests
982 * initialize with MCD_S_BEGIN: calculate sizes, and read status
983 * MCD_S_WAITSTAT: wait for status reply, set mode
984 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
985 * MCD_S_WAITREAD: wait for read ready, read data
987 static struct mcd_mbx *mbxsave;
990 mcd_timeout(void *arg)
992 mcd_doread((int)arg, mbxsave);
996 mcd_doread(int state, struct mcd_mbx *mbxin)
998 struct mcd_mbx *mbx = (state!=MCD_S_BEGIN) ? mbxsave : mbxin;
999 int unit = mbx->unit;
1000 int port = mbx->port;
1001 int com_port = mbx->port + mcd_command;
1002 int data_port = mbx->port + mcd_rdata;
1003 struct bio *bio = mbx->bio;
1004 struct buf *bp = bio->bio_buf;
1005 struct mcd_data *cd = mcd_data + unit;
1008 struct mcd_read2 rbuf;
1015 mbx = mbxsave = mbxin;
1020 outb(com_port, MCD_CMDGETSTAT);
1021 mbx->count = RDELAY_WAITSTAT;
1022 callout_reset(&cd->callout, hz / 100,
1023 mcd_timeout, (void *)MCD_S_WAITSTAT);
1025 case MCD_S_WAITSTAT:
1026 callout_stop(&cd->callout);
1027 if (mbx->count-- >= 0) {
1028 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) {
1030 callout_reset(&cd->callout, hz / 100,
1031 mcd_timeout, (void *)MCD_S_WAITSTAT);
1034 cd->status = inb(port+mcd_status) & 0xFF;
1035 if (cd->status & MCD_ST_CMDCHECK)
1037 if (mcd_setflags(unit,cd) < 0)
1039 MCD_TRACE("got WAITSTAT delay=%d\n",
1040 RDELAY_WAITSTAT-mbx->count);
1041 /* reject, if audio active */
1042 if (cd->status & MCDAUDIOBSY) {
1043 printf("mcd%d: audio is active\n",unit);
1048 /* to check for raw/cooked mode */
1049 if (cd->flags & MCDREADRAW) {
1054 mbx->sz = cd->blksize;
1057 if (rm == cd->curr_mode)
1060 mbx->count = RDELAY_WAITMODE;
1062 cd->curr_mode = MCD_MD_UNKNOWN;
1064 mcd_put(com_port, MCD_CMDSETMODE);
1065 mcd_put(com_port, rm);
1067 callout_reset(&cd->callout, hz / 100,
1068 mcd_timeout, (void *)MCD_S_WAITMODE);
1071 printf("mcd%d: timeout getstatus\n",unit);
1075 case MCD_S_WAITMODE:
1076 callout_stop(&cd->callout);
1077 if (mbx->count-- < 0) {
1078 printf("mcd%d: timeout set mode\n",unit);
1081 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) {
1082 callout_reset(&cd->callout, hz / 100,
1083 mcd_timeout, (void *)MCD_S_WAITMODE);
1086 cd->status = inb(port+mcd_status) & 0xFF;
1087 if (cd->status & MCD_ST_CMDCHECK) {
1088 cd->curr_mode = MCD_MD_UNKNOWN;
1091 if (mcd_setflags(unit,cd) < 0)
1093 cd->curr_mode = mbx->mode;
1094 MCD_TRACE("got WAITMODE delay=%d\n",
1095 RDELAY_WAITMODE-mbx->count);
1097 /* for first block */
1098 mbx->nblk = (bp->b_bcount + (mbx->sz-1)) / mbx->sz;
1102 blknum = (bio->bio_offset / mbx->sz)
1103 + mbx->p_offset + mbx->skip/mbx->sz;
1105 MCD_TRACE("mcd_doread: read blknum=%d for bp=%p\n",
1108 /* build parameter block */
1109 hsg2msf(blknum,rbuf.start_msf);
1111 /* send the read command */
1113 mcd_put(com_port,cd->read_command);
1114 mcd_put(com_port,rbuf.start_msf[0]);
1115 mcd_put(com_port,rbuf.start_msf[1]);
1116 mcd_put(com_port,rbuf.start_msf[2]);
1117 mcd_put(com_port,0);
1118 mcd_put(com_port,0);
1119 mcd_put(com_port,1);
1122 /* Spin briefly (<= 2ms) to avoid missing next block */
1123 for (i = 0; i < 20; i++) {
1124 k = inb(port+MCD_FLAGS);
1125 if (!(k & MFL_DATA_NOT_AVAIL))
1130 mbx->count = RDELAY_WAITREAD;
1131 callout_reset(&cd->callout, hz / 100,
1132 mcd_timeout, (void *)MCD_S_WAITREAD);
1134 case MCD_S_WAITREAD:
1135 callout_stop(&cd->callout);
1136 if (mbx->count-- > 0) {
1137 k = inb(port+MCD_FLAGS);
1138 if (!(k & MFL_DATA_NOT_AVAIL)) { /* XXX */
1139 MCD_TRACE("got data delay=%d\n",
1140 RDELAY_WAITREAD-mbx->count);
1143 addr = bp->b_data + mbx->skip;
1145 outb(port+mcd_ctl2,0x04); /* XXX */
1146 for (i=0; i<mbx->sz; i++)
1147 *addr++ = inb(data_port);
1148 outb(port+mcd_ctl2,0x0c); /* XXX */
1150 k = inb(port+MCD_FLAGS);
1151 /* If we still have some junk, read it too */
1152 if (!(k & MFL_DATA_NOT_AVAIL)) {
1153 outb(port+mcd_ctl2,0x04); /* XXX */
1154 (void)inb(data_port);
1155 (void)inb(data_port);
1156 outb(port+mcd_ctl2,0x0c); /* XXX */
1159 if (--mbx->nblk > 0) {
1160 mbx->skip += mbx->sz;
1168 cd->flags &= ~(MCDMBXBSY|MCDREADRAW);
1169 mcd_start(mbx->unit);
1172 if (!(k & MFL_STATUS_NOT_AVAIL)) {
1173 cd->status = inb(port+mcd_status) & 0xFF;
1174 if (cd->status & MCD_ST_CMDCHECK)
1176 if (mcd_setflags(unit,cd) < 0)
1179 callout_reset(&cd->callout, hz / 100,
1180 mcd_timeout, (void *)MCD_S_WAITREAD);
1183 printf("mcd%d: timeout read data\n",unit);
1189 if (mbx->retry-- > 0) {
1190 printf("mcd%d: retrying\n",unit);
1191 state = MCD_S_BEGIN1;
1195 /* invalidate the buffer */
1196 bp->b_flags |= B_ERROR;
1197 bp->b_resid = bp->b_bcount;
1200 cd->flags &= ~(MCDMBXBSY|MCDREADRAW);
1201 mcd_start(mbx->unit);
1205 printf("mcd%d: media changed\n", unit);
1209 printf("mcd%d: unit timeout, resetting\n",mbx->unit);
1210 outb(mbx->port+mcd_reset,MCD_CMDRESET);
1212 (void)mcd_getstat(mbx->unit,1);
1213 (void)mcd_getstat(mbx->unit,1);
1214 /*cd->status &= ~MCDDSKCHNG; */
1215 cd->debug = 1; /* preventive set debug mode */
1222 mcd_lock_door(int unit, int lock)
1224 struct mcd_data *cd = mcd_data + unit;
1225 int port = cd->iobase;
1227 outb(port+mcd_command, MCD_CMDLOCKDRV);
1228 outb(port+mcd_command, lock);
1229 if (mcd_getstat(unit,0) == -1)
1235 mcd_close_tray(int unit)
1237 struct mcd_data *cd = mcd_data + unit;
1238 int port = cd->iobase;
1241 if (mcd_getstat(unit,1) == -1)
1243 if (cd->status & MCDDOOROPEN) {
1244 outb(port+mcd_command, MCD_CMDCLOSETRAY);
1245 for (retry = 0; retry < CLOSE_TRAY_SECS * WAIT_FRAC; retry++) {
1246 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL)
1247 (void) tsleep((caddr_t)cd, PCATCH, "mcdcls", hz/WAIT_FRAC);
1249 if ((r = mcd_getstat(unit,0)) == -1)
1262 struct mcd_data *cd = mcd_data + unit;
1263 int port = cd->iobase, r;
1265 if (mcd_getstat(unit,1) == -1) /* detect disk change too */
1267 if (cd->status & MCDDOOROPEN)
1269 if ((r = mcd_stop(unit)) == EIO)
1271 outb(port+mcd_command, MCD_CMDEJECTDISK);
1272 if (mcd_getstat(unit,0) == -1)
1278 mcd_inject(int unit)
1280 struct mcd_data *cd = mcd_data + unit;
1282 if (mcd_getstat(unit,1) == -1) /* detect disk change too */
1284 if (cd->status & MCDDOOROPEN)
1285 return mcd_close_tray(unit);
1290 mcd_hard_reset(int unit)
1292 struct mcd_data *cd = mcd_data + unit;
1293 int port = cd->iobase;
1295 outb(port+mcd_reset,MCD_CMDRESET);
1296 cd->curr_mode = MCD_MD_UNKNOWN;
1297 cd->audio_status = CD_AS_AUDIO_INVALID;
1302 mcd_soft_reset(int unit)
1304 struct mcd_data *cd = mcd_data + unit;
1307 cd->flags &= (MCDINIT|MCDPROBING|MCDNEWMODEL);
1308 cd->curr_mode = MCD_MD_UNKNOWN;
1309 for (i=0; i<MAXPARTITIONS; i++) cd->partflags[i] = 0;
1310 cd->audio_status = CD_AS_AUDIO_INVALID;
1314 mcd_setmode(int unit, int mode)
1316 struct mcd_data *cd = mcd_data + unit;
1317 int port = cd->iobase;
1320 if (cd->curr_mode == mode)
1323 printf("mcd%d: setting mode to %d\n", unit, mode);
1324 for(retry=0; retry<MCD_RETRYS; retry++)
1326 cd->curr_mode = MCD_MD_UNKNOWN;
1327 outb(port+mcd_command, MCD_CMDSETMODE);
1328 outb(port+mcd_command, mode);
1329 if ((st = mcd_getstat(unit, 0)) >= 0) {
1330 cd->curr_mode = mode;
1334 printf("mcd%d: media changed\n", unit);
1343 mcd_toc_header(int unit, struct ioc_toc_header *th)
1345 struct mcd_data *cd = mcd_data + unit;
1348 if ((r = mcd_volinfo(unit)) != 0)
1351 th->starting_track = bcd2bin(cd->volinfo.trk_low);
1352 th->ending_track = bcd2bin(cd->volinfo.trk_high);
1353 th->len = 2 * sizeof(u_char) /* start & end tracks */ +
1354 (th->ending_track + 1 - th->starting_track + 1) *
1355 sizeof(struct cd_toc_entry);
1361 mcd_read_toc(int unit)
1363 struct mcd_data *cd = mcd_data + unit;
1364 struct ioc_toc_header th;
1365 struct mcd_qchninfo q;
1366 int rc, trk, idx, retry;
1368 /* Only read TOC if needed */
1369 if (cd->flags & MCDTOC)
1373 printf("mcd%d: reading toc header\n", unit);
1375 if ((rc = mcd_toc_header(unit, &th)) != 0)
1378 if (mcd_send(unit, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
1381 if (mcd_setmode(unit, MCD_MD_TOC) != 0)
1385 printf("mcd%d: get_toc reading qchannel info\n",unit);
1387 for(trk=th.starting_track; trk<=th.ending_track; trk++)
1388 cd->toc[trk].idx_no = 0;
1389 trk = th.ending_track - th.starting_track + 1;
1390 for(retry=0; retry<600 && trk>0; retry++)
1392 if (mcd_getqchan(unit, &q) < 0) break;
1393 idx = bcd2bin(q.idx_no);
1394 if (idx>=th.starting_track && idx<=th.ending_track && q.trk_no==0) {
1395 if (cd->toc[idx].idx_no == 0) {
1402 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1408 /* add a fake last+1 */
1409 idx = th.ending_track + 1;
1410 cd->toc[idx].control = cd->toc[idx-1].control;
1411 cd->toc[idx].addr_type = cd->toc[idx-1].addr_type;
1412 cd->toc[idx].trk_no = 0;
1413 cd->toc[idx].idx_no = MCD_LASTPLUS1;
1414 cd->toc[idx].hd_pos_msf[0] = cd->volinfo.vol_msf[0];
1415 cd->toc[idx].hd_pos_msf[1] = cd->volinfo.vol_msf[1];
1416 cd->toc[idx].hd_pos_msf[2] = cd->volinfo.vol_msf[2];
1420 for (i = th.starting_track; i <= idx; i++)
1421 printf("mcd%d: trk %d idx %d pos %d %d %d\n",
1423 cd->toc[i].idx_no > 0x99 ? cd->toc[i].idx_no :
1424 bcd2bin(cd->toc[i].idx_no),
1425 bcd2bin(cd->toc[i].hd_pos_msf[0]),
1426 bcd2bin(cd->toc[i].hd_pos_msf[1]),
1427 bcd2bin(cd->toc[i].hd_pos_msf[2]));
1430 cd->flags |= MCDTOC;
1437 mcd_toc_entry(int unit, struct ioc_read_toc_single_entry *te)
1439 struct mcd_data *cd = mcd_data + unit;
1440 struct ioc_toc_header th;
1443 if (te->address_format != CD_MSF_FORMAT
1444 && te->address_format != CD_LBA_FORMAT)
1447 /* Copy the toc header */
1448 if ((rc = mcd_toc_header(unit, &th)) != 0)
1451 /* verify starting track */
1454 trk = th.starting_track;
1455 else if (trk == MCD_LASTPLUS1)
1456 trk = th.ending_track + 1;
1457 else if (trk < th.starting_track || trk > th.ending_track + 1)
1460 /* Make sure we have a valid toc */
1461 if ((rc=mcd_read_toc(unit)) != 0)
1464 /* Copy the TOC data. */
1465 if (cd->toc[trk].idx_no == 0)
1468 te->entry.control = cd->toc[trk].control;
1469 te->entry.addr_type = cd->toc[trk].addr_type;
1471 cd->toc[trk].idx_no > 0x99 ? cd->toc[trk].idx_no :
1472 bcd2bin(cd->toc[trk].idx_no);
1473 switch (te->address_format) {
1475 te->entry.addr.msf.unused = 0;
1476 te->entry.addr.msf.minute = bcd2bin(cd->toc[trk].hd_pos_msf[0]);
1477 te->entry.addr.msf.second = bcd2bin(cd->toc[trk].hd_pos_msf[1]);
1478 te->entry.addr.msf.frame = bcd2bin(cd->toc[trk].hd_pos_msf[2]);
1481 te->entry.addr.lba = htonl(msf2hsg(cd->toc[trk].hd_pos_msf, 0));
1489 mcd_toc_entrys(int unit, struct ioc_read_toc_entry *te)
1491 struct mcd_data *cd = mcd_data + unit;
1492 struct cd_toc_entry entries[MCD_MAXTOCS];
1493 struct ioc_toc_header th;
1494 int rc, n, trk, len;
1496 if ( te->data_len < sizeof(entries[0])
1497 || (te->data_len % sizeof(entries[0])) != 0
1498 || (te->address_format != CD_MSF_FORMAT
1499 && te->address_format != CD_LBA_FORMAT)
1503 /* Copy the toc header */
1504 if ((rc = mcd_toc_header(unit, &th)) != 0)
1507 /* verify starting track */
1508 trk = te->starting_track;
1510 trk = th.starting_track;
1511 else if (trk == MCD_LASTPLUS1)
1512 trk = th.ending_track + 1;
1513 else if (trk < th.starting_track || trk > th.ending_track + 1)
1516 len = ((th.ending_track + 1 - trk) + 1) *
1518 if (te->data_len < len)
1520 if (len > sizeof(entries))
1523 /* Make sure we have a valid toc */
1524 if ((rc=mcd_read_toc(unit)) != 0)
1527 /* Copy the TOC data. */
1528 for (n = 0; len > 0 && trk <= th.ending_track + 1; trk++) {
1529 if (cd->toc[trk].idx_no == 0)
1531 entries[n].control = cd->toc[trk].control;
1532 entries[n].addr_type = cd->toc[trk].addr_type;
1534 cd->toc[trk].idx_no > 0x99 ? cd->toc[trk].idx_no :
1535 bcd2bin(cd->toc[trk].idx_no);
1536 switch (te->address_format) {
1538 entries[n].addr.msf.unused = 0;
1539 entries[n].addr.msf.minute = bcd2bin(cd->toc[trk].hd_pos_msf[0]);
1540 entries[n].addr.msf.second = bcd2bin(cd->toc[trk].hd_pos_msf[1]);
1541 entries[n].addr.msf.frame = bcd2bin(cd->toc[trk].hd_pos_msf[2]);
1544 entries[n].addr.lba = htonl(msf2hsg(cd->toc[trk].hd_pos_msf, 0));
1547 len -= sizeof(struct cd_toc_entry);
1551 /* copy the data back */
1552 return copyout(entries, te->data, n * sizeof(struct cd_toc_entry));
1558 struct mcd_data *cd = mcd_data + unit;
1560 /* Verify current status */
1561 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS &&
1562 cd->audio_status != CD_AS_PLAY_PAUSED &&
1563 cd->audio_status != CD_AS_PLAY_COMPLETED) {
1565 printf("mcd%d: stop attempted when not playing, audio status %d\n",
1566 unit, cd->audio_status);
1569 if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS)
1570 if (mcd_send(unit, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
1572 cd->audio_status = CD_AS_PLAY_COMPLETED;
1577 mcd_getqchan(int unit, struct mcd_qchninfo *q)
1579 struct mcd_data *cd = mcd_data + unit;
1581 if (mcd_send(unit, MCD_CMDGETQCHN, MCD_RETRYS) < 0)
1583 if (mcd_get(unit, (char *) q, sizeof(struct mcd_qchninfo)) < 0)
1586 printf("mcd%d: getqchan control=0x%x addr_type=0x%x trk=%d ind=%d ttm=%d:%d.%d dtm=%d:%d.%d\n",
1588 q->control, q->addr_type, bcd2bin(q->trk_no),
1590 bcd2bin(q->trk_size_msf[0]), bcd2bin(q->trk_size_msf[1]),
1591 bcd2bin(q->trk_size_msf[2]),
1592 bcd2bin(q->hd_pos_msf[0]), bcd2bin(q->hd_pos_msf[1]),
1593 bcd2bin(q->hd_pos_msf[2]));
1599 mcd_subchan(int unit, struct ioc_read_subchannel *sc)
1601 struct mcd_data *cd = mcd_data + unit;
1602 struct mcd_qchninfo q;
1603 struct cd_sub_channel_info data;
1607 printf("mcd%d: subchan af=%d, df=%d\n", unit,
1611 if (sc->address_format != CD_MSF_FORMAT &&
1612 sc->address_format != CD_LBA_FORMAT)
1615 if (sc->data_format != CD_CURRENT_POSITION &&
1616 sc->data_format != CD_MEDIA_CATALOG)
1619 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1622 if (mcd_getqchan(unit, &q) < 0)
1625 data.header.audio_status = cd->audio_status;
1626 data.what.position.data_format = sc->data_format;
1628 switch (sc->data_format) {
1629 case CD_MEDIA_CATALOG:
1630 data.what.media_catalog.mc_valid = 1;
1631 data.what.media_catalog.mc_number[0] = '\0';
1634 case CD_CURRENT_POSITION:
1635 data.what.position.control = q.control;
1636 data.what.position.addr_type = q.addr_type;
1637 data.what.position.track_number = bcd2bin(q.trk_no);
1638 data.what.position.index_number = bcd2bin(q.idx_no);
1639 switch (sc->address_format) {
1641 data.what.position.reladdr.msf.unused = 0;
1642 data.what.position.reladdr.msf.minute = bcd2bin(q.trk_size_msf[0]);
1643 data.what.position.reladdr.msf.second = bcd2bin(q.trk_size_msf[1]);
1644 data.what.position.reladdr.msf.frame = bcd2bin(q.trk_size_msf[2]);
1645 data.what.position.absaddr.msf.unused = 0;
1646 data.what.position.absaddr.msf.minute = bcd2bin(q.hd_pos_msf[0]);
1647 data.what.position.absaddr.msf.second = bcd2bin(q.hd_pos_msf[1]);
1648 data.what.position.absaddr.msf.frame = bcd2bin(q.hd_pos_msf[2]);
1651 lba = msf2hsg(q.trk_size_msf, 1);
1653 * Pre-gap has index number of 0, and decreasing MSF
1654 * address. Must be converted to negative LBA, per
1657 if (data.what.position.index_number == 0)
1659 data.what.position.reladdr.lba = htonl(lba);
1660 data.what.position.absaddr.lba = htonl(msf2hsg(q.hd_pos_msf, 0));
1666 return copyout(&data, sc->data, min(sizeof(struct cd_sub_channel_info), sc->data_len));
1670 mcd_playmsf(int unit, struct ioc_play_msf *p)
1672 struct mcd_data *cd = mcd_data + unit;
1673 struct mcd_read2 pb;
1676 printf("mcd%d: playmsf: from %d:%d.%d to %d:%d.%d\n",
1678 p->start_m, p->start_s, p->start_f,
1679 p->end_m, p->end_s, p->end_f);
1681 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
1682 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) ||
1683 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) >
1684 M_msf(cd->volinfo.vol_msf) * 60 * 75 +
1685 S_msf(cd->volinfo.vol_msf) * 75 +
1686 F_msf(cd->volinfo.vol_msf))
1689 pb.start_msf[0] = bin2bcd(p->start_m);
1690 pb.start_msf[1] = bin2bcd(p->start_s);
1691 pb.start_msf[2] = bin2bcd(p->start_f);
1692 pb.end_msf[0] = bin2bcd(p->end_m);
1693 pb.end_msf[1] = bin2bcd(p->end_s);
1694 pb.end_msf[2] = bin2bcd(p->end_f);
1696 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1699 return mcd_play(unit, &pb);
1703 mcd_playtracks(int unit, struct ioc_play_track *pt)
1705 struct mcd_data *cd = mcd_data + unit;
1706 struct mcd_read2 pb;
1707 int a = pt->start_track;
1708 int z = pt->end_track;
1711 if ((rc = mcd_read_toc(unit)) != 0)
1715 printf("mcd%d: playtracks from %d:%d to %d:%d\n", unit,
1716 a, pt->start_index, z, pt->end_index);
1718 if ( a < bcd2bin(cd->volinfo.trk_low)
1719 || a > bcd2bin(cd->volinfo.trk_high)
1721 || z < bcd2bin(cd->volinfo.trk_low)
1722 || z > bcd2bin(cd->volinfo.trk_high))
1725 for (i = 0; i < 3; i++) {
1726 pb.start_msf[i] = cd->toc[a].hd_pos_msf[i];
1727 pb.end_msf[i] = cd->toc[z+1].hd_pos_msf[i];
1730 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1733 return mcd_play(unit, &pb);
1737 mcd_playblocks(int unit, struct ioc_play_blocks *p)
1739 struct mcd_data *cd = mcd_data + unit;
1740 struct mcd_read2 pb;
1743 printf("mcd%d: playblocks: blkno %d length %d\n",
1744 unit, p->blk, p->len);
1746 if (p->blk > cd->disksize || p->len > cd->disksize ||
1747 p->blk < 0 || p->len < 0 ||
1748 (p->blk + p->len) > cd->disksize)
1751 hsg2msf(p->blk, pb.start_msf);
1752 hsg2msf(p->blk + p->len, pb.end_msf);
1754 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1757 return mcd_play(unit, &pb);
1761 mcd_play(int unit, struct mcd_read2 *pb)
1763 struct mcd_data *cd = mcd_data + unit;
1764 int com_port = cd->iobase + mcd_command;
1765 int retry, st = -1, status;
1768 for(retry=0; retry<MCD_RETRYS; retry++) {
1771 outb(com_port, MCD_CMDSINGLESPEEDREAD);
1772 outb(com_port, pb->start_msf[0]);
1773 outb(com_port, pb->start_msf[1]);
1774 outb(com_port, pb->start_msf[2]);
1775 outb(com_port, pb->end_msf[0]);
1776 outb(com_port, pb->end_msf[1]);
1777 outb(com_port, pb->end_msf[2]);
1780 status=mcd_getstat(unit, 0);
1783 else if (status != -2)
1789 printf("mcd%d: media changed\n", unit);
1793 printf("mcd%d: mcd_play retry=%d, status=0x%02x\n", unit, retry, status);
1796 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
1803 struct mcd_data *cd = mcd_data + unit;
1804 struct mcd_qchninfo q;
1807 /* Verify current status */
1808 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS &&
1809 cd->audio_status != CD_AS_PLAY_PAUSED) {
1811 printf("mcd%d: pause attempted when not playing, audio status %d\n",
1812 unit, cd->audio_status);
1816 /* Get the current position */
1817 if (mcd_getqchan(unit, &q) < 0)
1820 /* Copy it into lastpb */
1821 cd->lastpb.start_msf[0] = q.hd_pos_msf[0];
1822 cd->lastpb.start_msf[1] = q.hd_pos_msf[1];
1823 cd->lastpb.start_msf[2] = q.hd_pos_msf[2];
1826 if ((rc=mcd_stop(unit)) != 0)
1829 /* Set the proper status and exit */
1830 cd->audio_status = CD_AS_PLAY_PAUSED;
1835 mcd_resume(int unit)
1837 struct mcd_data *cd = mcd_data + unit;
1839 if (cd->audio_status != CD_AS_PLAY_PAUSED)
1841 return mcd_play(unit, &cd->lastpb);