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.
45 static const char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/fcntl.h>
55 #include <sys/disklabel.h>
59 #include <i386/isa/isa_device.h>
60 #include <i386/isa/mcdreg.h>
62 #define MCD_TRACE(format, args...) \
64 if (mcd_data[unit].debug) { \
65 printf("mcd%d: status=0x%02x: ", \
66 unit, mcd_data[unit].status); \
67 printf(format, ## args); \
71 #define mcd_part(dev) ((minor(dev)) & 7)
72 #define mcd_unit(dev) (((minor(dev)) & 0x38) >> 3)
73 #define mcd_phys(dev) (((minor(dev)) & 0x40) >> 6)
77 #define MCDVALID 0x0001 /* parameters loaded */
78 #define MCDINIT 0x0002 /* device is init'd */
79 #define MCDNEWMODEL 0x0004 /* device is new model */
80 #define MCDLABEL 0x0008 /* label is read */
81 #define MCDPROBING 0x0010 /* probing */
82 #define MCDREADRAW 0x0020 /* read raw mode (2352 bytes) */
83 #define MCDVOLINFO 0x0040 /* already read volinfo */
84 #define MCDTOC 0x0080 /* already read toc */
85 #define MCDMBXBSY 0x0100 /* local mbx is busy */
88 #define MCDAUDIOBSY MCD_ST_AUDIOBSY /* playing audio */
89 #define MCDDSKCHNG MCD_ST_DSKCHNG /* sensed change of disk */
90 #define MCDDSKIN MCD_ST_DSKIN /* sensed disk in drive */
91 #define MCDDOOROPEN MCD_ST_DOOROPEN /* sensed door open */
93 /* These are apparently the different states a mitsumi can get up to */
94 #define MCDCDABSENT 0x0030
95 #define MCDCDPRESENT 0x0020
96 #define MCDSCLOSED 0x0080
97 #define MCDSOPEN 0x00a0
99 #define MCD_MD_UNKNOWN (-1)
102 #define MCD_MAXTOCS 104 /* from the Linux driver */
103 #define MCD_LASTPLUS1 170 /* special toc entry */
105 #define MCD_TYPE_UNKNOWN 0
106 #define MCD_TYPE_LU002S 1
107 #define MCD_TYPE_LU005S 2
108 #define MCD_TYPE_LU006S 3
109 #define MCD_TYPE_FX001 4
110 #define MCD_TYPE_FX001D 5
125 static struct mcd_data {
135 struct disklabel dlabel;
136 int partflags[MAXPARTITIONS];
138 struct mcd_volinfo volinfo;
139 struct mcd_qchninfo toc[MCD_MAXTOCS];
142 struct mcd_read2 lastpb;
144 struct bio_queue_head head; /* head of bio queue */
148 /* reader state machine */
149 #define MCD_S_BEGIN 0
150 #define MCD_S_BEGIN1 1
151 #define MCD_S_WAITSTAT 2
152 #define MCD_S_WAITMODE 3
153 #define MCD_S_WAITREAD 4
156 static void mcd_start(int unit);
157 static int mcd_getdisklabel(int unit);
159 static void mcd_configure(struct mcd_data *cd);
161 static int mcd_get(int unit, char *buf, int nmax);
162 static int mcd_setflags(int unit,struct mcd_data *cd);
163 static int mcd_getstat(int unit,int sflg);
164 static int mcd_send(int unit, int cmd,int nretrys);
165 static void hsg2msf(int hsg, bcd_t *msf);
166 static int msf2hsg(bcd_t *msf, int relative);
167 static int mcd_volinfo(int unit);
168 static ointhand2_t mcdintr;
169 static int mcd_waitrdy(int port,int dly);
170 static timeout_t mcd_timeout;
171 static void mcd_doread(int state, struct mcd_mbx *mbxin);
172 static void mcd_soft_reset(int unit);
173 static int mcd_hard_reset(int unit);
174 static int mcd_setmode(int unit, int mode);
175 static int mcd_getqchan(int unit, struct mcd_qchninfo *q);
176 static int mcd_subchan(int unit, struct ioc_read_subchannel *sc);
177 static int mcd_toc_header(int unit, struct ioc_toc_header *th);
178 static int mcd_read_toc(int unit);
179 static int mcd_toc_entrys(int unit, struct ioc_read_toc_entry *te);
181 static int mcd_toc_entry(int unit, struct ioc_read_toc_single_entry *te);
183 static int mcd_stop(int unit);
184 static int mcd_eject(int unit);
185 static int mcd_inject(int unit);
186 static int mcd_playtracks(int unit, struct ioc_play_track *pt);
187 static int mcd_play(int unit, struct mcd_read2 *pb);
188 static int mcd_playmsf(int unit, struct ioc_play_msf *pt);
189 static int mcd_playblocks(int unit, struct ioc_play_blocks *);
190 static int mcd_pause(int unit);
191 static int mcd_resume(int unit);
192 static int mcd_lock_door(int unit, int lock);
193 static int mcd_close_tray(int unit);
195 static int mcd_probe(struct isa_device *dev);
196 static int mcd_attach(struct isa_device *dev);
197 struct isa_driver mcddriver = {
203 COMPAT_ISA_DRIVER(mcd, mcddriver);
205 static d_open_t mcdopen;
206 static d_close_t mcdclose;
207 static d_ioctl_t mcdioctl;
208 static d_psize_t mcdsize;
209 static d_strategy_t mcdstrategy;
211 #define CDEV_MAJOR 29
216 static struct cdevsw mcd_cdevsw = {
218 /* close */ mcdclose,
221 /* ioctl */ mcdioctl,
224 /* strategy */ mcdstrategy,
226 /* maj */ CDEV_MAJOR,
230 /* bmaj */ BDEV_MAJOR
233 #define mcd_put(port,byte) outb(port,byte)
236 #define MCD_RDRETRYS 8
238 #define CLOSE_TRAY_SECS 8
239 #define DISK_SENSE_SECS 3
243 #define RDELAY_WAITSTAT 300
244 #define RDELAY_WAITMODE 300
245 #define RDELAY_WAITREAD 800
248 #define DELAY_GETREPLY 5000000
250 int mcd_attach(struct isa_device *dev)
252 int unit = dev->id_unit;
253 struct mcd_data *cd = mcd_data + unit;
255 dev->id_ointr = mcdintr;
256 cd->iobase = dev->id_iobase;
257 cd->flags |= MCDINIT;
258 mcd_soft_reset(unit);
259 bioq_init(&cd->head);
262 /* wire controller for interrupts and dma */
265 /* name filled in probe */
266 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, 0),
267 UID_ROOT, GID_OPERATOR, 0640, "rmcd%da", unit);
268 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
269 UID_ROOT, GID_OPERATOR, 0640, "rmcd%dc", unit);
270 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, 0),
271 UID_ROOT, GID_OPERATOR, 0640, "mcd%da", unit);
272 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
273 UID_ROOT, GID_OPERATOR, 0640, "mcd%dc", unit);
277 int mcdopen(dev_t dev, int flags, int fmt, struct proc *p)
279 int unit,part,phys,r,retry;
282 unit = mcd_unit(dev);
286 cd = mcd_data + unit;
287 part = mcd_part(dev);
288 phys = mcd_phys(dev);
291 if (!(cd->flags & MCDINIT))
294 /* invalidated in the meantime? mark all open part's invalid */
295 if (!(cd->flags & MCDVALID) && cd->openflags)
298 if (mcd_getstat(unit,1) == -1)
301 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
302 || !(cd->status & MCDDSKIN))
303 for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) {
304 (void) tsleep((caddr_t)cd, PSOCK | PCATCH, "mcdsn1", hz/WAIT_FRAC);
305 if ((r = mcd_getstat(unit,1)) == -1)
311 if (( (cd->status & (MCDDOOROPEN|MCDDSKCHNG))
312 || !(cd->status & MCDDSKIN)
314 && major(dev) == CDEV_MAJOR && part == RAW_PART
316 cd->openflags |= (1<<part);
318 cd->partflags[part] |= MCDREADRAW;
321 if (cd->status & MCDDOOROPEN) {
322 printf("mcd%d: door is open\n", unit);
325 if (!(cd->status & MCDDSKIN)) {
326 printf("mcd%d: no CD inside\n", unit);
329 if (cd->status & MCDDSKCHNG) {
330 printf("mcd%d: CD not sensed\n", unit);
334 if (mcdsize(dev) < 0) {
335 if (major(dev) == CDEV_MAJOR && part == RAW_PART) {
336 cd->openflags |= (1<<part);
338 cd->partflags[part] |= MCDREADRAW;
341 printf("mcd%d: failed to get disk size\n",unit);
344 cd->flags |= MCDVALID;
346 /* XXX get a default disklabel */
347 mcd_getdisklabel(unit);
349 MCD_TRACE("open: partition=%d, disksize = %ld, blksize=%d\n",
350 part, cd->disksize, cd->blksize);
352 dev->si_bsize_phys = cd->blksize;
354 if (part == RAW_PART ||
355 (part < cd->dlabel.d_npartitions &&
356 cd->dlabel.d_partitions[part].p_fstype != FS_UNUSED)) {
357 cd->openflags |= (1<<part);
358 if (part == RAW_PART && phys)
359 cd->partflags[part] |= MCDREADRAW;
360 (void) mcd_lock_door(unit, MCD_LK_LOCK);
361 if (!(cd->flags & MCDVALID))
369 int mcdclose(dev_t dev, int flags, int fmt, struct proc *p)
374 unit = mcd_unit(dev);
378 cd = mcd_data + unit;
379 part = mcd_part(dev);
381 if (!(cd->flags & MCDINIT) || !(cd->openflags & (1<<part)))
384 MCD_TRACE("close: partition=%d\n", part);
386 (void) mcd_lock_door(unit, MCD_LK_UNLOCK);
387 cd->openflags &= ~(1<<part);
388 cd->partflags[part] &= ~MCDREADRAW;
394 mcdstrategy(struct bio *bp)
399 int unit = mcd_unit(bp->bio_dev);
401 cd = mcd_data + unit;
404 /*MCD_TRACE("strategy: buf=0x%lx, unit=%ld, block#=%ld bcount=%ld\n",
405 bp,unit,bp->bio_blkno,bp->bio_bcount);*/
406 if (unit >= NMCD || bp->bio_blkno < 0) {
407 printf("mcdstrategy: unit = %d, blkno = %ld, bcount = %ld\n",
408 unit, (long)bp->bio_blkno, bp->bio_bcount);
409 printf("mcd: mcdstratregy failure");
410 bp->bio_error = EINVAL;
411 bp->bio_flags |= BIO_ERROR;
415 /* if device invalidated (e.g. media change, door open), error */
416 if (!(cd->flags & MCDVALID)) {
417 MCD_TRACE("strategy: drive not valid\n");
423 if (!(bp->bio_cmd == BIO_READ)) {
424 bp->bio_error = EROFS;
428 /* no data to read */
429 if (bp->bio_bcount == 0)
432 /* for non raw access, check partition limits */
433 if (mcd_part(bp->bio_dev) != RAW_PART) {
434 if (!(cd->flags & MCDLABEL)) {
438 /* adjust transfer if necessary */
439 if (bounds_check_with_label(bp,&cd->dlabel,1) <= 0) {
443 bp->bio_pblkno = bp->bio_blkno;
449 bioqdisksort(&cd->head, bp);
452 /* now check whether we can perform processing */
457 bp->bio_flags |= BIO_ERROR;
459 bp->bio_resid = bp->bio_bcount;
464 static void mcd_start(int unit)
466 struct mcd_data *cd = mcd_data + unit;
471 if (cd->flags & MCDMBXBSY) {
476 bp = bioq_first(&cd->head);
478 /* block found to process, dequeue */
479 /*MCD_TRACE("mcd_start: found block bp=0x%x\n",bp,0,0,0);*/
480 bioq_remove(&cd->head, bp);
489 if (!(cd->flags & MCDVALID)) {
490 MCD_TRACE("mcd_start: drive not valid\n");
494 p = cd->dlabel.d_partitions + mcd_part(bp->bio_dev);
496 cd->flags |= MCDMBXBSY;
497 if (cd->partflags[mcd_part(bp->bio_dev)] & MCDREADRAW)
498 cd->flags |= MCDREADRAW;
500 cd->mbx.port = cd->iobase;
501 cd->mbx.retry = MCD_RETRYS;
503 cd->mbx.p_offset = p->p_offset;
505 /* calling the read routine */
506 mcd_doread(MCD_S_BEGIN,&(cd->mbx));
507 /* triggers mcd_start, when successful finished */
511 int mcdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
514 int unit,part,retry,r;
516 unit = mcd_unit(dev);
517 part = mcd_part(dev);
518 cd = mcd_data + unit;
520 if (mcd_getstat(unit, 1) == -1) /* detect disk change too */
522 MCD_TRACE("ioctl called 0x%lx\n", cmd);
535 return mcd_eject(unit);
543 return mcd_hard_reset(unit);
545 return mcd_lock_door(unit, MCD_LK_UNLOCK);
547 return mcd_lock_door(unit, MCD_LK_LOCK);
549 return mcd_inject(unit);
552 if (!(cd->flags & MCDVALID)) {
553 if ( major(dev) != CDEV_MAJOR
555 || !(cd->openflags & (1<<RAW_PART))
558 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
559 || !(cd->status & MCDDSKIN))
560 for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) {
561 (void) tsleep((caddr_t)cd, PSOCK | PCATCH, "mcdsn2", hz/WAIT_FRAC);
562 if ((r = mcd_getstat(unit,1)) == -1)
567 if ( (cd->status & (MCDDOOROPEN|MCDDSKCHNG))
568 || !(cd->status & MCDDSKIN)
572 cd->flags |= MCDVALID;
573 mcd_getdisklabel(unit);
575 cd->partflags[part] |= MCDREADRAW;
576 (void) mcd_lock_door(unit, MCD_LK_LOCK);
577 if (!(cd->flags & MCDVALID))
583 *(struct disklabel *) addr = cd->dlabel;
586 ((struct partinfo *) addr)->disklab = &cd->dlabel;
587 ((struct partinfo *) addr)->part =
588 &cd->dlabel.d_partitions[mcd_part(dev)];
592 * a bit silly, but someone might want to test something on a
597 if ((flags & FWRITE) == 0)
600 return setdisklabel(&cd->dlabel,
601 (struct disklabel *) addr,
606 case CDIOCPLAYTRACKS:
607 return mcd_playtracks(unit, (struct ioc_play_track *) addr);
608 case CDIOCPLAYBLOCKS:
609 return mcd_playblocks(unit, (struct ioc_play_blocks *) addr);
611 return mcd_playmsf(unit, (struct ioc_play_msf *) addr);
612 case CDIOCREADSUBCHANNEL:
613 return mcd_subchan(unit, (struct ioc_read_subchannel *) addr);
614 case CDIOREADTOCHEADER:
615 return mcd_toc_header(unit, (struct ioc_toc_header *) addr);
616 case CDIOREADTOCENTRYS:
617 return mcd_toc_entrys(unit, (struct ioc_read_toc_entry *) addr);
619 return mcd_resume(unit);
621 return mcd_pause(unit);
623 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
627 return mcd_stop(unit);
634 /* this could have been taken from scsi/cd.c, but it is not clear
635 * whether the scsi cd driver is linked in
637 static int mcd_getdisklabel(int unit)
639 struct mcd_data *cd = mcd_data + unit;
641 if (cd->flags & MCDLABEL)
644 bzero(&cd->dlabel,sizeof(struct disklabel));
645 /* filled with spaces first */
646 strncpy(cd->dlabel.d_typename," ",
647 sizeof(cd->dlabel.d_typename));
648 strncpy(cd->dlabel.d_typename, cd->name,
649 min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1));
650 strncpy(cd->dlabel.d_packname,"unknown ",
651 sizeof(cd->dlabel.d_packname));
652 cd->dlabel.d_secsize = cd->blksize;
653 cd->dlabel.d_nsectors = 100;
654 cd->dlabel.d_ntracks = 1;
655 cd->dlabel.d_ncylinders = (cd->disksize/100)+1;
656 cd->dlabel.d_secpercyl = 100;
657 cd->dlabel.d_secperunit = cd->disksize;
658 cd->dlabel.d_rpm = 300;
659 cd->dlabel.d_interleave = 1;
660 cd->dlabel.d_flags = D_REMOVABLE;
661 cd->dlabel.d_npartitions= 1;
662 cd->dlabel.d_partitions[0].p_offset = 0;
663 cd->dlabel.d_partitions[0].p_size = cd->disksize;
664 cd->dlabel.d_partitions[0].p_fstype = 9;
665 cd->dlabel.d_magic = DISKMAGIC;
666 cd->dlabel.d_magic2 = DISKMAGIC;
667 cd->dlabel.d_checksum = dkcksum(&cd->dlabel);
669 cd->flags |= MCDLABEL;
673 int mcdsize(dev_t dev)
676 int unit = mcd_unit(dev);
677 struct mcd_data *cd = mcd_data + unit;
679 if (mcd_volinfo(unit) == 0) {
680 cd->blksize = MCDBLK;
681 size = msf2hsg(cd->volinfo.vol_msf, 0);
682 cd->disksize = size * (MCDBLK/DEV_BSIZE);
688 /***************************************************************
689 * lower level of driver starts here
690 **************************************************************/
695 0x00,0x00,0x10,0x20,0x00,0x30,0x00,0x00,
696 0x00,0x10,0x40,0x50,0x00,0x00,0x00,0x00
701 0x00,0x01,0x00,0x03,0x00,0x05,0x06,0x07,
707 mcd_configure(struct mcd_data *cd)
709 outb(cd->iobase+mcd_config,cd->config);
713 /* Wait for non-busy - return 0 on timeout */
715 twiddle_thumbs(int port, int unit, int count, char *whine)
719 for (i = 0; i < count; i++) {
720 if (!(inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL))
724 printf("mcd%d: timeout %s\n", unit, whine);
728 /* check to see if a Mitsumi CD-ROM is attached to the ISA bus */
731 mcd_probe(struct isa_device *dev)
733 int port = dev->id_iobase;
734 int unit = dev->id_unit;
736 unsigned char stbytes[3];
740 cdevsw_add(&mcd_cdevsw);
742 mcd_data[unit].flags = MCDPROBING;
745 /* get irq/drq configuration word */
746 mcd_data[unit].config = irqs[dev->id_irq]; /* | drqs[dev->id_drq];*/
748 mcd_data[unit].config = 0;
752 outb(port+MCD_FLAGS, M_RESET);
755 * delay awhile by getting any pending garbage (old data) and
758 for (i = 1000000; i != 0; i--)
762 outb(port+MCD_DATA, MCD_CMDGETSTAT);
763 if (!twiddle_thumbs(port, unit, 1000000, "getting status"))
764 return 0; /* Timeout */
765 /* Get version information */
766 outb(port+MCD_DATA, MCD_CMDCONTINFO);
767 for (j = 0; j < 3; j++) {
768 if (!twiddle_thumbs(port, unit, 3000, "getting version info"))
770 stbytes[j] = (inb(port+MCD_DATA) & 0xFF);
772 if (stbytes[1] == stbytes[2])
774 if (stbytes[2] >= 4 || stbytes[1] != 'M') {
775 outb(port+MCD_CTRL, M_PICKLE);
776 mcd_data[unit].flags |= MCDNEWMODEL;
778 mcd_data[unit].read_command = MCD_CMDSINGLESPEEDREAD;
779 switch (stbytes[1]) {
781 if (stbytes[2] <= 2) {
782 mcd_data[unit].type = MCD_TYPE_LU002S;
783 mcd_data[unit].name = "Mitsumi LU002S";
784 } else if (stbytes[2] <= 5) {
785 mcd_data[unit].type = MCD_TYPE_LU005S;
786 mcd_data[unit].name = "Mitsumi LU005S";
788 mcd_data[unit].type = MCD_TYPE_LU006S;
789 mcd_data[unit].name = "Mitsumi LU006S";
793 mcd_data[unit].type = MCD_TYPE_FX001;
794 mcd_data[unit].name = "Mitsumi FX001";
797 mcd_data[unit].type = MCD_TYPE_FX001D;
798 mcd_data[unit].name = "Mitsumi FX001D";
799 mcd_data[unit].read_command = MCD_CMDDOUBLESPEEDREAD;
802 mcd_data[unit].type = MCD_TYPE_UNKNOWN;
803 mcd_data[unit].name = "Mitsumi ???";
806 printf("mcd%d: type %s, version info: %c %x\n", unit, mcd_data[unit].name,
807 stbytes[1], stbytes[2]);
814 mcd_waitrdy(int port,int dly)
818 /* wait until flag port senses status ready */
819 for (i=0; i<dly; i+=MIN_DELAY) {
820 if (!(inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL))
828 mcd_getreply(int unit,int dly)
830 struct mcd_data *cd = mcd_data + unit;
831 int port = cd->iobase;
833 /* wait data to become ready */
834 if (mcd_waitrdy(port,dly)<0) {
835 printf("mcd%d: timeout getreply\n",unit);
840 return inb(port+mcd_status) & 0xFF;
844 mcd_getstat(int unit,int sflg)
847 struct mcd_data *cd = mcd_data + unit;
848 int port = cd->iobase;
852 outb(port+mcd_command, MCD_CMDGETSTAT);
853 i = mcd_getreply(unit,DELAY_GETREPLY);
854 if (i<0 || (i & MCD_ST_CMDCHECK)) {
855 cd->curr_mode = MCD_MD_UNKNOWN;
861 if (mcd_setflags(unit,cd) < 0)
867 mcd_setflags(int unit, struct mcd_data *cd)
870 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
871 || !(cd->status & MCDDSKIN)) {
872 MCD_TRACE("setflags: sensed DSKCHNG or DOOROPEN or !DSKIN\n");
873 mcd_soft_reset(unit);
877 if (cd->status & MCDAUDIOBSY)
878 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
879 else if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS)
880 cd->audio_status = CD_AS_PLAY_COMPLETED;
885 mcd_get(int unit, char *buf, int nmax)
889 for (i=0; i<nmax; i++) {
891 if ((k = mcd_getreply(unit,DELAY_GETREPLY)) < 0) {
892 printf("mcd%d: timeout mcd_get\n",unit);
901 mcd_send(int unit, int cmd,int nretrys)
904 int port = mcd_data[unit].iobase;
906 /*MCD_TRACE("mcd_send: command = 0x%02x\n",cmd,0,0,0);*/
907 for (i=0; i<nretrys; i++) {
908 outb(port+mcd_command, cmd);
909 if ((k=mcd_getstat(unit,0)) != -1)
913 printf("mcd%d: media changed\n",unit);
917 printf("mcd%d: mcd_send retry cnt exceeded\n",unit);
920 /*MCD_TRACE("mcd_send: done\n",0,0,0,0);*/
925 hsg2msf(int hsg, bcd_t *msf)
928 F_msf(msf) = bin2bcd(hsg % 75);
930 S_msf(msf) = bin2bcd(hsg % 60);
932 M_msf(msf) = bin2bcd(hsg);
936 msf2hsg(bcd_t *msf, int relative)
938 return (bcd2bin(M_msf(msf)) * 60 + bcd2bin(S_msf(msf))) * 75 +
939 bcd2bin(F_msf(msf)) - (!relative) * 150;
943 mcd_volinfo(int unit)
945 struct mcd_data *cd = mcd_data + unit;
947 /* Just return if we already have it */
948 if (cd->flags & MCDVOLINFO) return 0;
950 /*MCD_TRACE("mcd_volinfo: enter\n",0,0,0,0);*/
952 /* send volume info command */
953 if (mcd_send(unit,MCD_CMDGETVOLINFO,MCD_RETRYS) < 0)
957 if (mcd_get(unit,(char*) &cd->volinfo,sizeof(struct mcd_volinfo)) < 0) {
958 printf("mcd%d: mcd_volinfo: error read data\n",unit);
962 if (cd->volinfo.trk_low > 0 &&
963 cd->volinfo.trk_high >= cd->volinfo.trk_low
965 cd->flags |= MCDVOLINFO; /* volinfo is OK */
976 MCD_TRACE("stray interrupt\n");
979 /* state machine to process read requests
980 * initialize with MCD_S_BEGIN: calculate sizes, and read status
981 * MCD_S_WAITSTAT: wait for status reply, set mode
982 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
983 * MCD_S_WAITREAD: wait for read ready, read data
985 static struct mcd_mbx *mbxsave;
986 static struct callout_handle tohandle = CALLOUT_HANDLE_INITIALIZER(&tohandle);
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 *bp = mbx->bp;
1003 struct mcd_data *cd = mcd_data + unit;
1006 struct mcd_read2 rbuf;
1013 mbx = mbxsave = mbxin;
1018 outb(com_port, MCD_CMDGETSTAT);
1019 mbx->count = RDELAY_WAITSTAT;
1020 tohandle = timeout(mcd_timeout,
1021 (caddr_t)MCD_S_WAITSTAT,hz/100); /* XXX */
1023 case MCD_S_WAITSTAT:
1024 untimeout(mcd_timeout,(caddr_t)MCD_S_WAITSTAT, tohandle);
1025 if (mbx->count-- >= 0) {
1026 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) {
1027 timeout(mcd_timeout,
1028 (caddr_t)MCD_S_WAITSTAT,hz/100); /* XXX */
1031 cd->status = inb(port+mcd_status) & 0xFF;
1032 if (cd->status & MCD_ST_CMDCHECK)
1034 if (mcd_setflags(unit,cd) < 0)
1036 MCD_TRACE("got WAITSTAT delay=%d\n",
1037 RDELAY_WAITSTAT-mbx->count);
1038 /* reject, if audio active */
1039 if (cd->status & MCDAUDIOBSY) {
1040 printf("mcd%d: audio is active\n",unit);
1045 /* to check for raw/cooked mode */
1046 if (cd->flags & MCDREADRAW) {
1051 mbx->sz = cd->blksize;
1054 if (rm == cd->curr_mode)
1057 mbx->count = RDELAY_WAITMODE;
1059 cd->curr_mode = MCD_MD_UNKNOWN;
1061 mcd_put(com_port, MCD_CMDSETMODE);
1062 mcd_put(com_port, rm);
1064 tohandle = timeout(mcd_timeout,
1065 (caddr_t)MCD_S_WAITMODE,hz/100); /* XXX */
1068 printf("mcd%d: timeout getstatus\n",unit);
1072 case MCD_S_WAITMODE:
1073 untimeout(mcd_timeout,(caddr_t)MCD_S_WAITMODE, tohandle);
1074 if (mbx->count-- < 0) {
1075 printf("mcd%d: timeout set mode\n",unit);
1078 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) {
1079 tohandle = timeout(mcd_timeout,
1080 (caddr_t)MCD_S_WAITMODE,hz/100);
1083 cd->status = inb(port+mcd_status) & 0xFF;
1084 if (cd->status & MCD_ST_CMDCHECK) {
1085 cd->curr_mode = MCD_MD_UNKNOWN;
1088 if (mcd_setflags(unit,cd) < 0)
1090 cd->curr_mode = mbx->mode;
1091 MCD_TRACE("got WAITMODE delay=%d\n",
1092 RDELAY_WAITMODE-mbx->count);
1094 /* for first block */
1095 mbx->nblk = (bp->bio_bcount + (mbx->sz-1)) / mbx->sz;
1099 blknum = (bp->bio_blkno / (mbx->sz/DEV_BSIZE))
1100 + mbx->p_offset + mbx->skip/mbx->sz;
1102 MCD_TRACE("mcd_doread: read blknum=%d for bp=%p\n",
1105 /* build parameter block */
1106 hsg2msf(blknum,rbuf.start_msf);
1108 /* send the read command */
1110 mcd_put(com_port,cd->read_command);
1111 mcd_put(com_port,rbuf.start_msf[0]);
1112 mcd_put(com_port,rbuf.start_msf[1]);
1113 mcd_put(com_port,rbuf.start_msf[2]);
1114 mcd_put(com_port,0);
1115 mcd_put(com_port,0);
1116 mcd_put(com_port,1);
1119 /* Spin briefly (<= 2ms) to avoid missing next block */
1120 for (i = 0; i < 20; i++) {
1121 k = inb(port+MCD_FLAGS);
1122 if (!(k & MFL_DATA_NOT_AVAIL))
1127 mbx->count = RDELAY_WAITREAD;
1128 tohandle = timeout(mcd_timeout,
1129 (caddr_t)MCD_S_WAITREAD,hz/100); /* XXX */
1131 case MCD_S_WAITREAD:
1132 untimeout(mcd_timeout,(caddr_t)MCD_S_WAITREAD, tohandle);
1133 if (mbx->count-- > 0) {
1134 k = inb(port+MCD_FLAGS);
1135 if (!(k & MFL_DATA_NOT_AVAIL)) { /* XXX */
1136 MCD_TRACE("got data delay=%d\n",
1137 RDELAY_WAITREAD-mbx->count);
1140 addr = bp->bio_data + mbx->skip;
1142 outb(port+mcd_ctl2,0x04); /* XXX */
1143 for (i=0; i<mbx->sz; i++)
1144 *addr++ = inb(data_port);
1145 outb(port+mcd_ctl2,0x0c); /* XXX */
1147 k = inb(port+MCD_FLAGS);
1148 /* If we still have some junk, read it too */
1149 if (!(k & MFL_DATA_NOT_AVAIL)) {
1150 outb(port+mcd_ctl2,0x04); /* XXX */
1151 (void)inb(data_port);
1152 (void)inb(data_port);
1153 outb(port+mcd_ctl2,0x0c); /* XXX */
1156 if (--mbx->nblk > 0) {
1157 mbx->skip += mbx->sz;
1165 cd->flags &= ~(MCDMBXBSY|MCDREADRAW);
1166 mcd_start(mbx->unit);
1169 if (!(k & MFL_STATUS_NOT_AVAIL)) {
1170 cd->status = inb(port+mcd_status) & 0xFF;
1171 if (cd->status & MCD_ST_CMDCHECK)
1173 if (mcd_setflags(unit,cd) < 0)
1176 tohandle = timeout(mcd_timeout,
1177 (caddr_t)MCD_S_WAITREAD,hz/100); /* XXX */
1180 printf("mcd%d: timeout read data\n",unit);
1186 if (mbx->retry-- > 0) {
1187 printf("mcd%d: retrying\n",unit);
1188 state = MCD_S_BEGIN1;
1192 /* invalidate the buffer */
1193 bp->bio_flags |= BIO_ERROR;
1194 bp->bio_resid = bp->bio_bcount;
1197 cd->flags &= ~(MCDMBXBSY|MCDREADRAW);
1198 mcd_start(mbx->unit);
1202 printf("mcd%d: media changed\n", unit);
1206 printf("mcd%d: unit timeout, resetting\n",mbx->unit);
1207 outb(mbx->port+mcd_reset,MCD_CMDRESET);
1209 (void)mcd_getstat(mbx->unit,1);
1210 (void)mcd_getstat(mbx->unit,1);
1211 /*cd->status &= ~MCDDSKCHNG; */
1212 cd->debug = 1; /* preventive set debug mode */
1219 mcd_lock_door(int unit, int lock)
1221 struct mcd_data *cd = mcd_data + unit;
1222 int port = cd->iobase;
1224 outb(port+mcd_command, MCD_CMDLOCKDRV);
1225 outb(port+mcd_command, lock);
1226 if (mcd_getstat(unit,0) == -1)
1232 mcd_close_tray(int unit)
1234 struct mcd_data *cd = mcd_data + unit;
1235 int port = cd->iobase;
1238 if (mcd_getstat(unit,1) == -1)
1240 if (cd->status & MCDDOOROPEN) {
1241 outb(port+mcd_command, MCD_CMDCLOSETRAY);
1242 for (retry = 0; retry < CLOSE_TRAY_SECS * WAIT_FRAC; retry++) {
1243 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL)
1244 (void) tsleep((caddr_t)cd, PSOCK | PCATCH, "mcdcls", hz/WAIT_FRAC);
1246 if ((r = mcd_getstat(unit,0)) == -1)
1259 struct mcd_data *cd = mcd_data + unit;
1260 int port = cd->iobase, r;
1262 if (mcd_getstat(unit,1) == -1) /* detect disk change too */
1264 if (cd->status & MCDDOOROPEN)
1266 if ((r = mcd_stop(unit)) == EIO)
1268 outb(port+mcd_command, MCD_CMDEJECTDISK);
1269 if (mcd_getstat(unit,0) == -1)
1275 mcd_inject(int unit)
1277 struct mcd_data *cd = mcd_data + unit;
1279 if (mcd_getstat(unit,1) == -1) /* detect disk change too */
1281 if (cd->status & MCDDOOROPEN)
1282 return mcd_close_tray(unit);
1287 mcd_hard_reset(int unit)
1289 struct mcd_data *cd = mcd_data + unit;
1290 int port = cd->iobase;
1292 outb(port+mcd_reset,MCD_CMDRESET);
1293 cd->curr_mode = MCD_MD_UNKNOWN;
1294 cd->audio_status = CD_AS_AUDIO_INVALID;
1299 mcd_soft_reset(int unit)
1301 struct mcd_data *cd = mcd_data + unit;
1304 cd->flags &= (MCDINIT|MCDPROBING|MCDNEWMODEL);
1305 cd->curr_mode = MCD_MD_UNKNOWN;
1306 for (i=0; i<MAXPARTITIONS; i++) cd->partflags[i] = 0;
1307 cd->audio_status = CD_AS_AUDIO_INVALID;
1311 mcd_setmode(int unit, int mode)
1313 struct mcd_data *cd = mcd_data + unit;
1314 int port = cd->iobase;
1317 if (cd->curr_mode == mode)
1320 printf("mcd%d: setting mode to %d\n", unit, mode);
1321 for(retry=0; retry<MCD_RETRYS; retry++)
1323 cd->curr_mode = MCD_MD_UNKNOWN;
1324 outb(port+mcd_command, MCD_CMDSETMODE);
1325 outb(port+mcd_command, mode);
1326 if ((st = mcd_getstat(unit, 0)) >= 0) {
1327 cd->curr_mode = mode;
1331 printf("mcd%d: media changed\n", unit);
1340 mcd_toc_header(int unit, struct ioc_toc_header *th)
1342 struct mcd_data *cd = mcd_data + unit;
1345 if ((r = mcd_volinfo(unit)) != 0)
1348 th->starting_track = bcd2bin(cd->volinfo.trk_low);
1349 th->ending_track = bcd2bin(cd->volinfo.trk_high);
1350 th->len = 2 * sizeof(u_char) /* start & end tracks */ +
1351 (th->ending_track + 1 - th->starting_track + 1) *
1352 sizeof(struct cd_toc_entry);
1358 mcd_read_toc(int unit)
1360 struct mcd_data *cd = mcd_data + unit;
1361 struct ioc_toc_header th;
1362 struct mcd_qchninfo q;
1363 int rc, trk, idx, retry;
1365 /* Only read TOC if needed */
1366 if (cd->flags & MCDTOC)
1370 printf("mcd%d: reading toc header\n", unit);
1372 if ((rc = mcd_toc_header(unit, &th)) != 0)
1375 if (mcd_send(unit, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
1378 if (mcd_setmode(unit, MCD_MD_TOC) != 0)
1382 printf("mcd%d: get_toc reading qchannel info\n",unit);
1384 for(trk=th.starting_track; trk<=th.ending_track; trk++)
1385 cd->toc[trk].idx_no = 0;
1386 trk = th.ending_track - th.starting_track + 1;
1387 for(retry=0; retry<600 && trk>0; retry++)
1389 if (mcd_getqchan(unit, &q) < 0) break;
1390 idx = bcd2bin(q.idx_no);
1391 if (idx>=th.starting_track && idx<=th.ending_track && q.trk_no==0) {
1392 if (cd->toc[idx].idx_no == 0) {
1399 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1405 /* add a fake last+1 */
1406 idx = th.ending_track + 1;
1407 cd->toc[idx].control = cd->toc[idx-1].control;
1408 cd->toc[idx].addr_type = cd->toc[idx-1].addr_type;
1409 cd->toc[idx].trk_no = 0;
1410 cd->toc[idx].idx_no = MCD_LASTPLUS1;
1411 cd->toc[idx].hd_pos_msf[0] = cd->volinfo.vol_msf[0];
1412 cd->toc[idx].hd_pos_msf[1] = cd->volinfo.vol_msf[1];
1413 cd->toc[idx].hd_pos_msf[2] = cd->volinfo.vol_msf[2];
1417 for (i = th.starting_track; i <= idx; i++)
1418 printf("mcd%d: trk %d idx %d pos %d %d %d\n",
1420 cd->toc[i].idx_no > 0x99 ? cd->toc[i].idx_no :
1421 bcd2bin(cd->toc[i].idx_no),
1422 bcd2bin(cd->toc[i].hd_pos_msf[0]),
1423 bcd2bin(cd->toc[i].hd_pos_msf[1]),
1424 bcd2bin(cd->toc[i].hd_pos_msf[2]));
1427 cd->flags |= MCDTOC;
1434 mcd_toc_entry(int unit, struct ioc_read_toc_single_entry *te)
1436 struct mcd_data *cd = mcd_data + unit;
1437 struct ioc_toc_header th;
1440 if (te->address_format != CD_MSF_FORMAT
1441 && te->address_format != CD_LBA_FORMAT)
1444 /* Copy the toc header */
1445 if ((rc = mcd_toc_header(unit, &th)) != 0)
1448 /* verify starting track */
1451 trk = th.starting_track;
1452 else if (trk == MCD_LASTPLUS1)
1453 trk = th.ending_track + 1;
1454 else if (trk < th.starting_track || trk > th.ending_track + 1)
1457 /* Make sure we have a valid toc */
1458 if ((rc=mcd_read_toc(unit)) != 0)
1461 /* Copy the TOC data. */
1462 if (cd->toc[trk].idx_no == 0)
1465 te->entry.control = cd->toc[trk].control;
1466 te->entry.addr_type = cd->toc[trk].addr_type;
1468 cd->toc[trk].idx_no > 0x99 ? cd->toc[trk].idx_no :
1469 bcd2bin(cd->toc[trk].idx_no);
1470 switch (te->address_format) {
1472 te->entry.addr.msf.unused = 0;
1473 te->entry.addr.msf.minute = bcd2bin(cd->toc[trk].hd_pos_msf[0]);
1474 te->entry.addr.msf.second = bcd2bin(cd->toc[trk].hd_pos_msf[1]);
1475 te->entry.addr.msf.frame = bcd2bin(cd->toc[trk].hd_pos_msf[2]);
1478 te->entry.addr.lba = htonl(msf2hsg(cd->toc[trk].hd_pos_msf, 0));
1486 mcd_toc_entrys(int unit, struct ioc_read_toc_entry *te)
1488 struct mcd_data *cd = mcd_data + unit;
1489 struct cd_toc_entry entries[MCD_MAXTOCS];
1490 struct ioc_toc_header th;
1491 int rc, n, trk, len;
1493 if ( te->data_len < sizeof(entries[0])
1494 || (te->data_len % sizeof(entries[0])) != 0
1495 || (te->address_format != CD_MSF_FORMAT
1496 && te->address_format != CD_LBA_FORMAT)
1500 /* Copy the toc header */
1501 if ((rc = mcd_toc_header(unit, &th)) != 0)
1504 /* verify starting track */
1505 trk = te->starting_track;
1507 trk = th.starting_track;
1508 else if (trk == MCD_LASTPLUS1)
1509 trk = th.ending_track + 1;
1510 else if (trk < th.starting_track || trk > th.ending_track + 1)
1513 len = ((th.ending_track + 1 - trk) + 1) *
1515 if (te->data_len < len)
1517 if (len > sizeof(entries))
1520 /* Make sure we have a valid toc */
1521 if ((rc=mcd_read_toc(unit)) != 0)
1524 /* Copy the TOC data. */
1525 for (n = 0; len > 0 && trk <= th.ending_track + 1; trk++) {
1526 if (cd->toc[trk].idx_no == 0)
1528 entries[n].control = cd->toc[trk].control;
1529 entries[n].addr_type = cd->toc[trk].addr_type;
1531 cd->toc[trk].idx_no > 0x99 ? cd->toc[trk].idx_no :
1532 bcd2bin(cd->toc[trk].idx_no);
1533 switch (te->address_format) {
1535 entries[n].addr.msf.unused = 0;
1536 entries[n].addr.msf.minute = bcd2bin(cd->toc[trk].hd_pos_msf[0]);
1537 entries[n].addr.msf.second = bcd2bin(cd->toc[trk].hd_pos_msf[1]);
1538 entries[n].addr.msf.frame = bcd2bin(cd->toc[trk].hd_pos_msf[2]);
1541 entries[n].addr.lba = htonl(msf2hsg(cd->toc[trk].hd_pos_msf, 0));
1544 len -= sizeof(struct cd_toc_entry);
1548 /* copy the data back */
1549 return copyout(entries, te->data, n * sizeof(struct cd_toc_entry));
1555 struct mcd_data *cd = mcd_data + unit;
1557 /* Verify current status */
1558 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS &&
1559 cd->audio_status != CD_AS_PLAY_PAUSED &&
1560 cd->audio_status != CD_AS_PLAY_COMPLETED) {
1562 printf("mcd%d: stop attempted when not playing, audio status %d\n",
1563 unit, cd->audio_status);
1566 if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS)
1567 if (mcd_send(unit, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
1569 cd->audio_status = CD_AS_PLAY_COMPLETED;
1574 mcd_getqchan(int unit, struct mcd_qchninfo *q)
1576 struct mcd_data *cd = mcd_data + unit;
1578 if (mcd_send(unit, MCD_CMDGETQCHN, MCD_RETRYS) < 0)
1580 if (mcd_get(unit, (char *) q, sizeof(struct mcd_qchninfo)) < 0)
1583 printf("mcd%d: getqchan control=0x%x addr_type=0x%x trk=%d ind=%d ttm=%d:%d.%d dtm=%d:%d.%d\n",
1585 q->control, q->addr_type, bcd2bin(q->trk_no),
1587 bcd2bin(q->trk_size_msf[0]), bcd2bin(q->trk_size_msf[1]),
1588 bcd2bin(q->trk_size_msf[2]),
1589 bcd2bin(q->hd_pos_msf[0]), bcd2bin(q->hd_pos_msf[1]),
1590 bcd2bin(q->hd_pos_msf[2]));
1596 mcd_subchan(int unit, struct ioc_read_subchannel *sc)
1598 struct mcd_data *cd = mcd_data + unit;
1599 struct mcd_qchninfo q;
1600 struct cd_sub_channel_info data;
1604 printf("mcd%d: subchan af=%d, df=%d\n", unit,
1608 if (sc->address_format != CD_MSF_FORMAT &&
1609 sc->address_format != CD_LBA_FORMAT)
1612 if (sc->data_format != CD_CURRENT_POSITION &&
1613 sc->data_format != CD_MEDIA_CATALOG)
1616 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1619 if (mcd_getqchan(unit, &q) < 0)
1622 data.header.audio_status = cd->audio_status;
1623 data.what.position.data_format = sc->data_format;
1625 switch (sc->data_format) {
1626 case CD_MEDIA_CATALOG:
1627 data.what.media_catalog.mc_valid = 1;
1628 data.what.media_catalog.mc_number[0] = '\0';
1631 case CD_CURRENT_POSITION:
1632 data.what.position.control = q.control;
1633 data.what.position.addr_type = q.addr_type;
1634 data.what.position.track_number = bcd2bin(q.trk_no);
1635 data.what.position.index_number = bcd2bin(q.idx_no);
1636 switch (sc->address_format) {
1638 data.what.position.reladdr.msf.unused = 0;
1639 data.what.position.reladdr.msf.minute = bcd2bin(q.trk_size_msf[0]);
1640 data.what.position.reladdr.msf.second = bcd2bin(q.trk_size_msf[1]);
1641 data.what.position.reladdr.msf.frame = bcd2bin(q.trk_size_msf[2]);
1642 data.what.position.absaddr.msf.unused = 0;
1643 data.what.position.absaddr.msf.minute = bcd2bin(q.hd_pos_msf[0]);
1644 data.what.position.absaddr.msf.second = bcd2bin(q.hd_pos_msf[1]);
1645 data.what.position.absaddr.msf.frame = bcd2bin(q.hd_pos_msf[2]);
1648 lba = msf2hsg(q.trk_size_msf, 1);
1650 * Pre-gap has index number of 0, and decreasing MSF
1651 * address. Must be converted to negative LBA, per
1654 if (data.what.position.index_number == 0)
1656 data.what.position.reladdr.lba = htonl(lba);
1657 data.what.position.absaddr.lba = htonl(msf2hsg(q.hd_pos_msf, 0));
1663 return copyout(&data, sc->data, min(sizeof(struct cd_sub_channel_info), sc->data_len));
1667 mcd_playmsf(int unit, struct ioc_play_msf *p)
1669 struct mcd_data *cd = mcd_data + unit;
1670 struct mcd_read2 pb;
1673 printf("mcd%d: playmsf: from %d:%d.%d to %d:%d.%d\n",
1675 p->start_m, p->start_s, p->start_f,
1676 p->end_m, p->end_s, p->end_f);
1678 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
1679 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) ||
1680 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) >
1681 M_msf(cd->volinfo.vol_msf) * 60 * 75 +
1682 S_msf(cd->volinfo.vol_msf) * 75 +
1683 F_msf(cd->volinfo.vol_msf))
1686 pb.start_msf[0] = bin2bcd(p->start_m);
1687 pb.start_msf[1] = bin2bcd(p->start_s);
1688 pb.start_msf[2] = bin2bcd(p->start_f);
1689 pb.end_msf[0] = bin2bcd(p->end_m);
1690 pb.end_msf[1] = bin2bcd(p->end_s);
1691 pb.end_msf[2] = bin2bcd(p->end_f);
1693 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1696 return mcd_play(unit, &pb);
1700 mcd_playtracks(int unit, struct ioc_play_track *pt)
1702 struct mcd_data *cd = mcd_data + unit;
1703 struct mcd_read2 pb;
1704 int a = pt->start_track;
1705 int z = pt->end_track;
1708 if ((rc = mcd_read_toc(unit)) != 0)
1712 printf("mcd%d: playtracks from %d:%d to %d:%d\n", unit,
1713 a, pt->start_index, z, pt->end_index);
1715 if ( a < bcd2bin(cd->volinfo.trk_low)
1716 || a > bcd2bin(cd->volinfo.trk_high)
1718 || z < bcd2bin(cd->volinfo.trk_low)
1719 || z > bcd2bin(cd->volinfo.trk_high))
1722 for (i = 0; i < 3; i++) {
1723 pb.start_msf[i] = cd->toc[a].hd_pos_msf[i];
1724 pb.end_msf[i] = cd->toc[z+1].hd_pos_msf[i];
1727 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1730 return mcd_play(unit, &pb);
1734 mcd_playblocks(int unit, struct ioc_play_blocks *p)
1736 struct mcd_data *cd = mcd_data + unit;
1737 struct mcd_read2 pb;
1740 printf("mcd%d: playblocks: blkno %d length %d\n",
1741 unit, p->blk, p->len);
1743 if (p->blk > cd->disksize || p->len > cd->disksize ||
1744 p->blk < 0 || p->len < 0 ||
1745 (p->blk + p->len) > cd->disksize)
1748 hsg2msf(p->blk, pb.start_msf);
1749 hsg2msf(p->blk + p->len, pb.end_msf);
1751 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1754 return mcd_play(unit, &pb);
1758 mcd_play(int unit, struct mcd_read2 *pb)
1760 struct mcd_data *cd = mcd_data + unit;
1761 int com_port = cd->iobase + mcd_command;
1762 int retry, st = -1, status;
1765 for(retry=0; retry<MCD_RETRYS; retry++) {
1768 outb(com_port, MCD_CMDSINGLESPEEDREAD);
1769 outb(com_port, pb->start_msf[0]);
1770 outb(com_port, pb->start_msf[1]);
1771 outb(com_port, pb->start_msf[2]);
1772 outb(com_port, pb->end_msf[0]);
1773 outb(com_port, pb->end_msf[1]);
1774 outb(com_port, pb->end_msf[2]);
1777 status=mcd_getstat(unit, 0);
1780 else if (status != -2)
1786 printf("mcd%d: media changed\n", unit);
1790 printf("mcd%d: mcd_play retry=%d, status=0x%02x\n", unit, retry, status);
1793 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
1800 struct mcd_data *cd = mcd_data + unit;
1801 struct mcd_qchninfo q;
1804 /* Verify current status */
1805 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS &&
1806 cd->audio_status != CD_AS_PLAY_PAUSED) {
1808 printf("mcd%d: pause attempted when not playing, audio status %d\n",
1809 unit, cd->audio_status);
1813 /* Get the current position */
1814 if (mcd_getqchan(unit, &q) < 0)
1817 /* Copy it into lastpb */
1818 cd->lastpb.start_msf[0] = q.hd_pos_msf[0];
1819 cd->lastpb.start_msf[1] = q.hd_pos_msf[1];
1820 cd->lastpb.start_msf[2] = q.hd_pos_msf[2];
1823 if ((rc=mcd_stop(unit)) != 0)
1826 /* Set the proper status and exit */
1827 cd->audio_status = CD_AS_PLAY_PAUSED;
1832 mcd_resume(int unit)
1834 struct mcd_data *cd = mcd_data + unit;
1836 if (cd->audio_status != CD_AS_PLAY_PAUSED)
1838 return mcd_play(unit, &cd->lastpb);