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.7 2003/08/07 21:16:53 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 <machine/clock.h>
62 #include <bus/isa/i386/isa_device.h>
65 #define MCD_TRACE(format, args...) \
67 if (mcd_data[unit].debug) { \
68 printf("mcd%d: status=0x%02x: ", \
69 unit, mcd_data[unit].status); \
70 printf(format, ## args); \
74 #define mcd_part(dev) ((minor(dev)) & 7)
75 #define mcd_unit(dev) (((minor(dev)) & 0x38) >> 3)
76 #define mcd_phys(dev) (((minor(dev)) & 0x40) >> 6)
80 #define MCDVALID 0x0001 /* parameters loaded */
81 #define MCDINIT 0x0002 /* device is init'd */
82 #define MCDNEWMODEL 0x0004 /* device is new model */
83 #define MCDLABEL 0x0008 /* label is read */
84 #define MCDPROBING 0x0010 /* probing */
85 #define MCDREADRAW 0x0020 /* read raw mode (2352 bytes) */
86 #define MCDVOLINFO 0x0040 /* already read volinfo */
87 #define MCDTOC 0x0080 /* already read toc */
88 #define MCDMBXBSY 0x0100 /* local mbx is busy */
91 #define MCDAUDIOBSY MCD_ST_AUDIOBSY /* playing audio */
92 #define MCDDSKCHNG MCD_ST_DSKCHNG /* sensed change of disk */
93 #define MCDDSKIN MCD_ST_DSKIN /* sensed disk in drive */
94 #define MCDDOOROPEN MCD_ST_DOOROPEN /* sensed door open */
96 /* These are apparently the different states a mitsumi can get up to */
97 #define MCDCDABSENT 0x0030
98 #define MCDCDPRESENT 0x0020
99 #define MCDSCLOSED 0x0080
100 #define MCDSOPEN 0x00a0
102 #define MCD_MD_UNKNOWN (-1)
105 #define MCD_MAXTOCS 104 /* from the Linux driver */
106 #define MCD_LASTPLUS1 170 /* special toc entry */
108 #define MCD_TYPE_UNKNOWN 0
109 #define MCD_TYPE_LU002S 1
110 #define MCD_TYPE_LU005S 2
111 #define MCD_TYPE_LU006S 3
112 #define MCD_TYPE_FX001 4
113 #define MCD_TYPE_FX001D 5
128 static struct mcd_data {
138 struct disklabel dlabel;
139 int partflags[MAXPARTITIONS];
141 struct mcd_volinfo volinfo;
142 struct mcd_qchninfo toc[MCD_MAXTOCS];
145 struct mcd_read2 lastpb;
147 struct buf_queue_head head; /* head of buf queue */
151 /* reader state machine */
152 #define MCD_S_BEGIN 0
153 #define MCD_S_BEGIN1 1
154 #define MCD_S_WAITSTAT 2
155 #define MCD_S_WAITMODE 3
156 #define MCD_S_WAITREAD 4
159 static void mcd_start(int unit);
160 static int mcd_getdisklabel(int unit);
162 static void mcd_configure(struct mcd_data *cd);
164 static int mcd_get(int unit, char *buf, int nmax);
165 static int mcd_setflags(int unit,struct mcd_data *cd);
166 static int mcd_getstat(int unit,int sflg);
167 static int mcd_send(int unit, int cmd,int nretrys);
168 static void hsg2msf(int hsg, bcd_t *msf);
169 static int msf2hsg(bcd_t *msf, int relative);
170 static int mcd_volinfo(int unit);
171 static ointhand2_t mcdintr;
172 static int mcd_waitrdy(int port,int dly);
173 static timeout_t mcd_timeout;
174 static void mcd_doread(int state, struct mcd_mbx *mbxin);
175 static void mcd_soft_reset(int unit);
176 static int mcd_hard_reset(int unit);
177 static int mcd_setmode(int unit, int mode);
178 static int mcd_getqchan(int unit, struct mcd_qchninfo *q);
179 static int mcd_subchan(int unit, struct ioc_read_subchannel *sc);
180 static int mcd_toc_header(int unit, struct ioc_toc_header *th);
181 static int mcd_read_toc(int unit);
182 static int mcd_toc_entrys(int unit, struct ioc_read_toc_entry *te);
184 static int mcd_toc_entry(int unit, struct ioc_read_toc_single_entry *te);
186 static int mcd_stop(int unit);
187 static int mcd_eject(int unit);
188 static int mcd_inject(int unit);
189 static int mcd_playtracks(int unit, struct ioc_play_track *pt);
190 static int mcd_play(int unit, struct mcd_read2 *pb);
191 static int mcd_playmsf(int unit, struct ioc_play_msf *pt);
192 static int mcd_playblocks(int unit, struct ioc_play_blocks *);
193 static int mcd_pause(int unit);
194 static int mcd_resume(int unit);
195 static int mcd_lock_door(int unit, int lock);
196 static int mcd_close_tray(int unit);
198 static int mcd_probe(struct isa_device *dev);
199 static int mcd_attach(struct isa_device *dev);
200 struct isa_driver mcddriver = { mcd_probe, mcd_attach, "mcd" };
202 static d_open_t mcdopen;
203 static d_close_t mcdclose;
204 static d_ioctl_t mcdioctl;
205 static d_psize_t mcdsize;
206 static d_strategy_t mcdstrategy;
208 static struct cdevsw mcd_cdevsw = {
210 /* maj */ MCD_CDEV_MAJOR,
216 /* close */ mcdclose,
219 /* ioctl */ mcdioctl,
222 /* strategy */ mcdstrategy,
227 #define mcd_put(port,byte) outb(port,byte)
230 #define MCD_RDRETRYS 8
232 #define CLOSE_TRAY_SECS 8
233 #define DISK_SENSE_SECS 3
237 #define RDELAY_WAITSTAT 300
238 #define RDELAY_WAITMODE 300
239 #define RDELAY_WAITREAD 800
242 #define DELAY_GETREPLY 5000000
244 int mcd_attach(struct isa_device *dev)
246 int unit = dev->id_unit;
247 struct mcd_data *cd = mcd_data + unit;
249 dev->id_ointr = mcdintr;
250 cd->iobase = dev->id_iobase;
251 cd->flags |= MCDINIT;
252 mcd_soft_reset(unit);
253 bufq_init(&cd->head);
256 /* wire controller for interrupts and dma */
259 /* name filled in probe */
260 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, 0),
261 UID_ROOT, GID_OPERATOR, 0640, "rmcd%da", unit);
262 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
263 UID_ROOT, GID_OPERATOR, 0640, "rmcd%dc", unit);
264 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, 0),
265 UID_ROOT, GID_OPERATOR, 0640, "mcd%da", unit);
266 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
267 UID_ROOT, GID_OPERATOR, 0640, "mcd%dc", unit);
271 int mcdopen(dev_t dev, int flags, int fmt, struct thread *td)
273 int unit,part,phys,r,retry;
276 unit = mcd_unit(dev);
280 cd = mcd_data + unit;
281 part = mcd_part(dev);
282 phys = mcd_phys(dev);
285 if (!(cd->flags & MCDINIT))
288 /* invalidated in the meantime? mark all open part's invalid */
289 if (!(cd->flags & MCDVALID) && cd->openflags)
292 if (mcd_getstat(unit,1) == -1)
295 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
296 || !(cd->status & MCDDSKIN))
297 for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) {
298 (void) tsleep((caddr_t)cd, PCATCH, "mcdsn1", hz/WAIT_FRAC);
299 if ((r = mcd_getstat(unit,1)) == -1)
305 if (( (cd->status & (MCDDOOROPEN|MCDDSKCHNG))
306 || !(cd->status & MCDDSKIN)
308 && major(dev) == MCD_CDEV_MAJOR && part == RAW_PART
310 cd->openflags |= (1<<part);
312 cd->partflags[part] |= MCDREADRAW;
315 if (cd->status & MCDDOOROPEN) {
316 printf("mcd%d: door is open\n", unit);
319 if (!(cd->status & MCDDSKIN)) {
320 printf("mcd%d: no CD inside\n", unit);
323 if (cd->status & MCDDSKCHNG) {
324 printf("mcd%d: CD not sensed\n", unit);
328 if (mcdsize(dev) < 0) {
329 if (major(dev) == MCD_CDEV_MAJOR && part == RAW_PART) {
330 cd->openflags |= (1<<part);
332 cd->partflags[part] |= MCDREADRAW;
335 printf("mcd%d: failed to get disk size\n",unit);
338 cd->flags |= MCDVALID;
340 /* XXX get a default disklabel */
341 mcd_getdisklabel(unit);
343 MCD_TRACE("open: partition=%d, disksize = %ld, blksize=%d\n",
344 part, cd->disksize, cd->blksize);
346 dev->si_bsize_phys = cd->blksize;
348 if (part == RAW_PART ||
349 (part < cd->dlabel.d_npartitions &&
350 cd->dlabel.d_partitions[part].p_fstype != FS_UNUSED)) {
351 cd->openflags |= (1<<part);
352 if (part == RAW_PART && phys)
353 cd->partflags[part] |= MCDREADRAW;
354 (void) mcd_lock_door(unit, MCD_LK_LOCK);
355 if (!(cd->flags & MCDVALID))
363 int mcdclose(dev_t dev, int flags, int fmt, struct thread *td)
368 unit = mcd_unit(dev);
372 cd = mcd_data + unit;
373 part = mcd_part(dev);
375 if (!(cd->flags & MCDINIT) || !(cd->openflags & (1<<part)))
378 MCD_TRACE("close: partition=%d\n", part);
380 (void) mcd_lock_door(unit, MCD_LK_UNLOCK);
381 cd->openflags &= ~(1<<part);
382 cd->partflags[part] &= ~MCDREADRAW;
388 mcdstrategy(struct buf *bp)
393 int unit = mcd_unit(bp->b_dev);
395 cd = mcd_data + unit;
398 /*MCD_TRACE("strategy: buf=0x%lx, unit=%ld, block#=%ld bcount=%ld\n",
399 bp,unit,bp->b_blkno,bp->b_bcount);*/
400 if (unit >= NMCD || bp->b_blkno < 0) {
401 printf("mcdstrategy: unit = %d, blkno = %ld, bcount = %ld\n",
402 unit, (long)bp->b_blkno, bp->b_bcount);
403 printf("mcd: mcdstratregy failure");
404 bp->b_error = EINVAL;
405 bp->b_flags |= B_ERROR;
409 /* if device invalidated (e.g. media change, door open), error */
410 if (!(cd->flags & MCDVALID)) {
411 MCD_TRACE("strategy: drive not valid\n");
417 if (!(bp->b_flags & B_READ)) {
422 /* no data to read */
423 if (bp->b_bcount == 0)
426 /* for non raw access, check partition limits */
427 if (mcd_part(bp->b_dev) != RAW_PART) {
428 if (!(cd->flags & MCDLABEL)) {
432 /* adjust transfer if necessary */
433 if (bounds_check_with_label(bp,&cd->dlabel,1) <= 0) {
437 bp->b_pblkno = bp->b_blkno;
443 bufqdisksort(&cd->head, bp);
446 /* now check whether we can perform processing */
451 bp->b_flags |= B_ERROR;
453 bp->b_resid = bp->b_bcount;
458 static void mcd_start(int unit)
460 struct mcd_data *cd = mcd_data + unit;
465 if (cd->flags & MCDMBXBSY) {
470 bp = bufq_first(&cd->head);
472 /* block found to process, dequeue */
473 /*MCD_TRACE("mcd_start: found block bp=0x%x\n",bp,0,0,0);*/
474 bufq_remove(&cd->head, bp);
483 if (!(cd->flags & MCDVALID)) {
484 MCD_TRACE("mcd_start: drive not valid\n");
488 p = cd->dlabel.d_partitions + mcd_part(bp->b_dev);
490 cd->flags |= MCDMBXBSY;
491 if (cd->partflags[mcd_part(bp->b_dev)] & MCDREADRAW)
492 cd->flags |= MCDREADRAW;
494 cd->mbx.port = cd->iobase;
495 cd->mbx.retry = MCD_RETRYS;
497 cd->mbx.p_offset = p->p_offset;
499 /* calling the read routine */
500 mcd_doread(MCD_S_BEGIN,&(cd->mbx));
501 /* triggers mcd_start, when successful finished */
505 int mcdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
508 int unit,part,retry,r;
510 unit = mcd_unit(dev);
511 part = mcd_part(dev);
512 cd = mcd_data + unit;
514 if (mcd_getstat(unit, 1) == -1) /* detect disk change too */
516 MCD_TRACE("ioctl called 0x%lx\n", cmd);
529 return mcd_eject(unit);
537 return mcd_hard_reset(unit);
539 return mcd_lock_door(unit, MCD_LK_UNLOCK);
541 return mcd_lock_door(unit, MCD_LK_LOCK);
543 return mcd_inject(unit);
546 if (!(cd->flags & MCDVALID)) {
547 if ( major(dev) != MCD_CDEV_MAJOR
549 || !(cd->openflags & (1<<RAW_PART))
552 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
553 || !(cd->status & MCDDSKIN))
554 for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) {
555 (void) tsleep((caddr_t)cd, PCATCH, "mcdsn2", hz/WAIT_FRAC);
556 if ((r = mcd_getstat(unit,1)) == -1)
561 if ( (cd->status & (MCDDOOROPEN|MCDDSKCHNG))
562 || !(cd->status & MCDDSKIN)
566 cd->flags |= MCDVALID;
567 mcd_getdisklabel(unit);
569 cd->partflags[part] |= MCDREADRAW;
570 (void) mcd_lock_door(unit, MCD_LK_LOCK);
571 if (!(cd->flags & MCDVALID))
577 *(struct disklabel *) addr = cd->dlabel;
580 ((struct partinfo *) addr)->disklab = &cd->dlabel;
581 ((struct partinfo *) addr)->part =
582 &cd->dlabel.d_partitions[mcd_part(dev)];
586 * a bit silly, but someone might want to test something on a
591 if ((flags & FWRITE) == 0)
594 return setdisklabel(&cd->dlabel,
595 (struct disklabel *) addr,
600 case CDIOCPLAYTRACKS:
601 return mcd_playtracks(unit, (struct ioc_play_track *) addr);
602 case CDIOCPLAYBLOCKS:
603 return mcd_playblocks(unit, (struct ioc_play_blocks *) addr);
605 return mcd_playmsf(unit, (struct ioc_play_msf *) addr);
606 case CDIOCREADSUBCHANNEL:
607 return mcd_subchan(unit, (struct ioc_read_subchannel *) addr);
608 case CDIOREADTOCHEADER:
609 return mcd_toc_header(unit, (struct ioc_toc_header *) addr);
610 case CDIOREADTOCENTRYS:
611 return mcd_toc_entrys(unit, (struct ioc_read_toc_entry *) addr);
613 return mcd_resume(unit);
615 return mcd_pause(unit);
617 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
621 return mcd_stop(unit);
628 /* this could have been taken from scsi/cd.c, but it is not clear
629 * whether the scsi cd driver is linked in
631 static int mcd_getdisklabel(int unit)
633 struct mcd_data *cd = mcd_data + unit;
635 if (cd->flags & MCDLABEL)
638 bzero(&cd->dlabel,sizeof(struct disklabel));
639 /* filled with spaces first */
640 strncpy(cd->dlabel.d_typename," ",
641 sizeof(cd->dlabel.d_typename));
642 strncpy(cd->dlabel.d_typename, cd->name,
643 min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1));
644 strncpy(cd->dlabel.d_packname,"unknown ",
645 sizeof(cd->dlabel.d_packname));
646 cd->dlabel.d_secsize = cd->blksize;
647 cd->dlabel.d_nsectors = 100;
648 cd->dlabel.d_ntracks = 1;
649 cd->dlabel.d_ncylinders = (cd->disksize/100)+1;
650 cd->dlabel.d_secpercyl = 100;
651 cd->dlabel.d_secperunit = cd->disksize;
652 cd->dlabel.d_rpm = 300;
653 cd->dlabel.d_interleave = 1;
654 cd->dlabel.d_flags = D_REMOVABLE;
655 cd->dlabel.d_npartitions= 1;
656 cd->dlabel.d_partitions[0].p_offset = 0;
657 cd->dlabel.d_partitions[0].p_size = cd->disksize;
658 cd->dlabel.d_partitions[0].p_fstype = 9;
659 cd->dlabel.d_magic = DISKMAGIC;
660 cd->dlabel.d_magic2 = DISKMAGIC;
661 cd->dlabel.d_checksum = dkcksum(&cd->dlabel);
663 cd->flags |= MCDLABEL;
667 int mcdsize(dev_t dev)
670 int unit = mcd_unit(dev);
671 struct mcd_data *cd = mcd_data + unit;
673 if (mcd_volinfo(unit) == 0) {
674 cd->blksize = MCDBLK;
675 size = msf2hsg(cd->volinfo.vol_msf, 0);
676 cd->disksize = size * (MCDBLK/DEV_BSIZE);
682 /***************************************************************
683 * lower level of driver starts here
684 **************************************************************/
689 0x00,0x00,0x10,0x20,0x00,0x30,0x00,0x00,
690 0x00,0x10,0x40,0x50,0x00,0x00,0x00,0x00
695 0x00,0x01,0x00,0x03,0x00,0x05,0x06,0x07,
701 mcd_configure(struct mcd_data *cd)
703 outb(cd->iobase+mcd_config,cd->config);
707 /* Wait for non-busy - return 0 on timeout */
709 twiddle_thumbs(int port, int unit, int count, char *whine)
713 for (i = 0; i < count; i++) {
714 if (!(inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL))
718 printf("mcd%d: timeout %s\n", unit, whine);
722 /* check to see if a Mitsumi CD-ROM is attached to the ISA bus */
725 mcd_probe(struct isa_device *dev)
727 int port = dev->id_iobase;
728 int unit = dev->id_unit;
730 unsigned char stbytes[3];
734 cdevsw_add(&mcd_cdevsw);
736 mcd_data[unit].flags = MCDPROBING;
739 /* get irq/drq configuration word */
740 mcd_data[unit].config = irqs[dev->id_irq]; /* | drqs[dev->id_drq];*/
742 mcd_data[unit].config = 0;
746 outb(port+MCD_FLAGS, M_RESET);
749 * delay awhile by getting any pending garbage (old data) and
752 for (i = 1000000; i != 0; i--)
756 outb(port+MCD_DATA, MCD_CMDGETSTAT);
757 if (!twiddle_thumbs(port, unit, 1000000, "getting status"))
758 return 0; /* Timeout */
759 /* Get version information */
760 outb(port+MCD_DATA, MCD_CMDCONTINFO);
761 for (j = 0; j < 3; j++) {
762 if (!twiddle_thumbs(port, unit, 3000, "getting version info"))
764 stbytes[j] = (inb(port+MCD_DATA) & 0xFF);
766 if (stbytes[1] == stbytes[2])
768 if (stbytes[2] >= 4 || stbytes[1] != 'M') {
769 outb(port+MCD_CTRL, M_PICKLE);
770 mcd_data[unit].flags |= MCDNEWMODEL;
772 mcd_data[unit].read_command = MCD_CMDSINGLESPEEDREAD;
773 switch (stbytes[1]) {
775 if (stbytes[2] <= 2) {
776 mcd_data[unit].type = MCD_TYPE_LU002S;
777 mcd_data[unit].name = "Mitsumi LU002S";
778 } else if (stbytes[2] <= 5) {
779 mcd_data[unit].type = MCD_TYPE_LU005S;
780 mcd_data[unit].name = "Mitsumi LU005S";
782 mcd_data[unit].type = MCD_TYPE_LU006S;
783 mcd_data[unit].name = "Mitsumi LU006S";
787 mcd_data[unit].type = MCD_TYPE_FX001;
788 mcd_data[unit].name = "Mitsumi FX001";
791 mcd_data[unit].type = MCD_TYPE_FX001D;
792 mcd_data[unit].name = "Mitsumi FX001D";
793 mcd_data[unit].read_command = MCD_CMDDOUBLESPEEDREAD;
796 mcd_data[unit].type = MCD_TYPE_UNKNOWN;
797 mcd_data[unit].name = "Mitsumi ???";
800 printf("mcd%d: type %s, version info: %c %x\n", unit, mcd_data[unit].name,
801 stbytes[1], stbytes[2]);
808 mcd_waitrdy(int port,int dly)
812 /* wait until flag port senses status ready */
813 for (i=0; i<dly; i+=MIN_DELAY) {
814 if (!(inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL))
822 mcd_getreply(int unit,int dly)
824 struct mcd_data *cd = mcd_data + unit;
825 int port = cd->iobase;
827 /* wait data to become ready */
828 if (mcd_waitrdy(port,dly)<0) {
829 printf("mcd%d: timeout getreply\n",unit);
834 return inb(port+mcd_status) & 0xFF;
838 mcd_getstat(int unit,int sflg)
841 struct mcd_data *cd = mcd_data + unit;
842 int port = cd->iobase;
846 outb(port+mcd_command, MCD_CMDGETSTAT);
847 i = mcd_getreply(unit,DELAY_GETREPLY);
848 if (i<0 || (i & MCD_ST_CMDCHECK)) {
849 cd->curr_mode = MCD_MD_UNKNOWN;
855 if (mcd_setflags(unit,cd) < 0)
861 mcd_setflags(int unit, struct mcd_data *cd)
864 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
865 || !(cd->status & MCDDSKIN)) {
866 MCD_TRACE("setflags: sensed DSKCHNG or DOOROPEN or !DSKIN\n");
867 mcd_soft_reset(unit);
871 if (cd->status & MCDAUDIOBSY)
872 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
873 else if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS)
874 cd->audio_status = CD_AS_PLAY_COMPLETED;
879 mcd_get(int unit, char *buf, int nmax)
883 for (i=0; i<nmax; i++) {
885 if ((k = mcd_getreply(unit,DELAY_GETREPLY)) < 0) {
886 printf("mcd%d: timeout mcd_get\n",unit);
895 mcd_send(int unit, int cmd,int nretrys)
898 int port = mcd_data[unit].iobase;
900 /*MCD_TRACE("mcd_send: command = 0x%02x\n",cmd,0,0,0);*/
901 for (i=0; i<nretrys; i++) {
902 outb(port+mcd_command, cmd);
903 if ((k=mcd_getstat(unit,0)) != -1)
907 printf("mcd%d: media changed\n",unit);
911 printf("mcd%d: mcd_send retry cnt exceeded\n",unit);
914 /*MCD_TRACE("mcd_send: done\n",0,0,0,0);*/
919 hsg2msf(int hsg, bcd_t *msf)
922 F_msf(msf) = bin2bcd(hsg % 75);
924 S_msf(msf) = bin2bcd(hsg % 60);
926 M_msf(msf) = bin2bcd(hsg);
930 msf2hsg(bcd_t *msf, int relative)
932 return (bcd2bin(M_msf(msf)) * 60 + bcd2bin(S_msf(msf))) * 75 +
933 bcd2bin(F_msf(msf)) - (!relative) * 150;
937 mcd_volinfo(int unit)
939 struct mcd_data *cd = mcd_data + unit;
941 /* Just return if we already have it */
942 if (cd->flags & MCDVOLINFO) return 0;
944 /*MCD_TRACE("mcd_volinfo: enter\n",0,0,0,0);*/
946 /* send volume info command */
947 if (mcd_send(unit,MCD_CMDGETVOLINFO,MCD_RETRYS) < 0)
951 if (mcd_get(unit,(char*) &cd->volinfo,sizeof(struct mcd_volinfo)) < 0) {
952 printf("mcd%d: mcd_volinfo: error read data\n",unit);
956 if (cd->volinfo.trk_low > 0 &&
957 cd->volinfo.trk_high >= cd->volinfo.trk_low
959 cd->flags |= MCDVOLINFO; /* volinfo is OK */
970 MCD_TRACE("stray interrupt\n");
973 /* state machine to process read requests
974 * initialize with MCD_S_BEGIN: calculate sizes, and read status
975 * MCD_S_WAITSTAT: wait for status reply, set mode
976 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
977 * MCD_S_WAITREAD: wait for read ready, read data
979 static struct mcd_mbx *mbxsave;
980 static struct callout_handle tohandle = CALLOUT_HANDLE_INITIALIZER(&tohandle);
983 mcd_timeout(void *arg)
985 mcd_doread((int)arg, mbxsave);
989 mcd_doread(int state, struct mcd_mbx *mbxin)
991 struct mcd_mbx *mbx = (state!=MCD_S_BEGIN) ? mbxsave : mbxin;
992 int unit = mbx->unit;
993 int port = mbx->port;
994 int com_port = mbx->port + mcd_command;
995 int data_port = mbx->port + mcd_rdata;
996 struct buf *bp = mbx->bp;
997 struct mcd_data *cd = mcd_data + unit;
1000 struct mcd_read2 rbuf;
1007 mbx = mbxsave = mbxin;
1012 outb(com_port, MCD_CMDGETSTAT);
1013 mbx->count = RDELAY_WAITSTAT;
1014 tohandle = timeout(mcd_timeout,
1015 (caddr_t)MCD_S_WAITSTAT,hz/100); /* XXX */
1017 case MCD_S_WAITSTAT:
1018 untimeout(mcd_timeout,(caddr_t)MCD_S_WAITSTAT, tohandle);
1019 if (mbx->count-- >= 0) {
1020 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) {
1021 timeout(mcd_timeout,
1022 (caddr_t)MCD_S_WAITSTAT,hz/100); /* XXX */
1025 cd->status = inb(port+mcd_status) & 0xFF;
1026 if (cd->status & MCD_ST_CMDCHECK)
1028 if (mcd_setflags(unit,cd) < 0)
1030 MCD_TRACE("got WAITSTAT delay=%d\n",
1031 RDELAY_WAITSTAT-mbx->count);
1032 /* reject, if audio active */
1033 if (cd->status & MCDAUDIOBSY) {
1034 printf("mcd%d: audio is active\n",unit);
1039 /* to check for raw/cooked mode */
1040 if (cd->flags & MCDREADRAW) {
1045 mbx->sz = cd->blksize;
1048 if (rm == cd->curr_mode)
1051 mbx->count = RDELAY_WAITMODE;
1053 cd->curr_mode = MCD_MD_UNKNOWN;
1055 mcd_put(com_port, MCD_CMDSETMODE);
1056 mcd_put(com_port, rm);
1058 tohandle = timeout(mcd_timeout,
1059 (caddr_t)MCD_S_WAITMODE,hz/100); /* XXX */
1062 printf("mcd%d: timeout getstatus\n",unit);
1066 case MCD_S_WAITMODE:
1067 untimeout(mcd_timeout,(caddr_t)MCD_S_WAITMODE, tohandle);
1068 if (mbx->count-- < 0) {
1069 printf("mcd%d: timeout set mode\n",unit);
1072 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) {
1073 tohandle = timeout(mcd_timeout,
1074 (caddr_t)MCD_S_WAITMODE,hz/100);
1077 cd->status = inb(port+mcd_status) & 0xFF;
1078 if (cd->status & MCD_ST_CMDCHECK) {
1079 cd->curr_mode = MCD_MD_UNKNOWN;
1082 if (mcd_setflags(unit,cd) < 0)
1084 cd->curr_mode = mbx->mode;
1085 MCD_TRACE("got WAITMODE delay=%d\n",
1086 RDELAY_WAITMODE-mbx->count);
1088 /* for first block */
1089 mbx->nblk = (bp->b_bcount + (mbx->sz-1)) / mbx->sz;
1093 blknum = (bp->b_blkno / (mbx->sz/DEV_BSIZE))
1094 + mbx->p_offset + mbx->skip/mbx->sz;
1096 MCD_TRACE("mcd_doread: read blknum=%d for bp=%p\n",
1099 /* build parameter block */
1100 hsg2msf(blknum,rbuf.start_msf);
1102 /* send the read command */
1104 mcd_put(com_port,cd->read_command);
1105 mcd_put(com_port,rbuf.start_msf[0]);
1106 mcd_put(com_port,rbuf.start_msf[1]);
1107 mcd_put(com_port,rbuf.start_msf[2]);
1108 mcd_put(com_port,0);
1109 mcd_put(com_port,0);
1110 mcd_put(com_port,1);
1113 /* Spin briefly (<= 2ms) to avoid missing next block */
1114 for (i = 0; i < 20; i++) {
1115 k = inb(port+MCD_FLAGS);
1116 if (!(k & MFL_DATA_NOT_AVAIL))
1121 mbx->count = RDELAY_WAITREAD;
1122 tohandle = timeout(mcd_timeout,
1123 (caddr_t)MCD_S_WAITREAD,hz/100); /* XXX */
1125 case MCD_S_WAITREAD:
1126 untimeout(mcd_timeout,(caddr_t)MCD_S_WAITREAD, tohandle);
1127 if (mbx->count-- > 0) {
1128 k = inb(port+MCD_FLAGS);
1129 if (!(k & MFL_DATA_NOT_AVAIL)) { /* XXX */
1130 MCD_TRACE("got data delay=%d\n",
1131 RDELAY_WAITREAD-mbx->count);
1134 addr = bp->b_data + mbx->skip;
1136 outb(port+mcd_ctl2,0x04); /* XXX */
1137 for (i=0; i<mbx->sz; i++)
1138 *addr++ = inb(data_port);
1139 outb(port+mcd_ctl2,0x0c); /* XXX */
1141 k = inb(port+MCD_FLAGS);
1142 /* If we still have some junk, read it too */
1143 if (!(k & MFL_DATA_NOT_AVAIL)) {
1144 outb(port+mcd_ctl2,0x04); /* XXX */
1145 (void)inb(data_port);
1146 (void)inb(data_port);
1147 outb(port+mcd_ctl2,0x0c); /* XXX */
1150 if (--mbx->nblk > 0) {
1151 mbx->skip += mbx->sz;
1159 cd->flags &= ~(MCDMBXBSY|MCDREADRAW);
1160 mcd_start(mbx->unit);
1163 if (!(k & MFL_STATUS_NOT_AVAIL)) {
1164 cd->status = inb(port+mcd_status) & 0xFF;
1165 if (cd->status & MCD_ST_CMDCHECK)
1167 if (mcd_setflags(unit,cd) < 0)
1170 tohandle = timeout(mcd_timeout,
1171 (caddr_t)MCD_S_WAITREAD,hz/100); /* XXX */
1174 printf("mcd%d: timeout read data\n",unit);
1180 if (mbx->retry-- > 0) {
1181 printf("mcd%d: retrying\n",unit);
1182 state = MCD_S_BEGIN1;
1186 /* invalidate the buffer */
1187 bp->b_flags |= B_ERROR;
1188 bp->b_resid = bp->b_bcount;
1191 cd->flags &= ~(MCDMBXBSY|MCDREADRAW);
1192 mcd_start(mbx->unit);
1196 printf("mcd%d: media changed\n", unit);
1200 printf("mcd%d: unit timeout, resetting\n",mbx->unit);
1201 outb(mbx->port+mcd_reset,MCD_CMDRESET);
1203 (void)mcd_getstat(mbx->unit,1);
1204 (void)mcd_getstat(mbx->unit,1);
1205 /*cd->status &= ~MCDDSKCHNG; */
1206 cd->debug = 1; /* preventive set debug mode */
1213 mcd_lock_door(int unit, int lock)
1215 struct mcd_data *cd = mcd_data + unit;
1216 int port = cd->iobase;
1218 outb(port+mcd_command, MCD_CMDLOCKDRV);
1219 outb(port+mcd_command, lock);
1220 if (mcd_getstat(unit,0) == -1)
1226 mcd_close_tray(int unit)
1228 struct mcd_data *cd = mcd_data + unit;
1229 int port = cd->iobase;
1232 if (mcd_getstat(unit,1) == -1)
1234 if (cd->status & MCDDOOROPEN) {
1235 outb(port+mcd_command, MCD_CMDCLOSETRAY);
1236 for (retry = 0; retry < CLOSE_TRAY_SECS * WAIT_FRAC; retry++) {
1237 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL)
1238 (void) tsleep((caddr_t)cd, PCATCH, "mcdcls", hz/WAIT_FRAC);
1240 if ((r = mcd_getstat(unit,0)) == -1)
1253 struct mcd_data *cd = mcd_data + unit;
1254 int port = cd->iobase, r;
1256 if (mcd_getstat(unit,1) == -1) /* detect disk change too */
1258 if (cd->status & MCDDOOROPEN)
1260 if ((r = mcd_stop(unit)) == EIO)
1262 outb(port+mcd_command, MCD_CMDEJECTDISK);
1263 if (mcd_getstat(unit,0) == -1)
1269 mcd_inject(int unit)
1271 struct mcd_data *cd = mcd_data + unit;
1273 if (mcd_getstat(unit,1) == -1) /* detect disk change too */
1275 if (cd->status & MCDDOOROPEN)
1276 return mcd_close_tray(unit);
1281 mcd_hard_reset(int unit)
1283 struct mcd_data *cd = mcd_data + unit;
1284 int port = cd->iobase;
1286 outb(port+mcd_reset,MCD_CMDRESET);
1287 cd->curr_mode = MCD_MD_UNKNOWN;
1288 cd->audio_status = CD_AS_AUDIO_INVALID;
1293 mcd_soft_reset(int unit)
1295 struct mcd_data *cd = mcd_data + unit;
1298 cd->flags &= (MCDINIT|MCDPROBING|MCDNEWMODEL);
1299 cd->curr_mode = MCD_MD_UNKNOWN;
1300 for (i=0; i<MAXPARTITIONS; i++) cd->partflags[i] = 0;
1301 cd->audio_status = CD_AS_AUDIO_INVALID;
1305 mcd_setmode(int unit, int mode)
1307 struct mcd_data *cd = mcd_data + unit;
1308 int port = cd->iobase;
1311 if (cd->curr_mode == mode)
1314 printf("mcd%d: setting mode to %d\n", unit, mode);
1315 for(retry=0; retry<MCD_RETRYS; retry++)
1317 cd->curr_mode = MCD_MD_UNKNOWN;
1318 outb(port+mcd_command, MCD_CMDSETMODE);
1319 outb(port+mcd_command, mode);
1320 if ((st = mcd_getstat(unit, 0)) >= 0) {
1321 cd->curr_mode = mode;
1325 printf("mcd%d: media changed\n", unit);
1334 mcd_toc_header(int unit, struct ioc_toc_header *th)
1336 struct mcd_data *cd = mcd_data + unit;
1339 if ((r = mcd_volinfo(unit)) != 0)
1342 th->starting_track = bcd2bin(cd->volinfo.trk_low);
1343 th->ending_track = bcd2bin(cd->volinfo.trk_high);
1344 th->len = 2 * sizeof(u_char) /* start & end tracks */ +
1345 (th->ending_track + 1 - th->starting_track + 1) *
1346 sizeof(struct cd_toc_entry);
1352 mcd_read_toc(int unit)
1354 struct mcd_data *cd = mcd_data + unit;
1355 struct ioc_toc_header th;
1356 struct mcd_qchninfo q;
1357 int rc, trk, idx, retry;
1359 /* Only read TOC if needed */
1360 if (cd->flags & MCDTOC)
1364 printf("mcd%d: reading toc header\n", unit);
1366 if ((rc = mcd_toc_header(unit, &th)) != 0)
1369 if (mcd_send(unit, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
1372 if (mcd_setmode(unit, MCD_MD_TOC) != 0)
1376 printf("mcd%d: get_toc reading qchannel info\n",unit);
1378 for(trk=th.starting_track; trk<=th.ending_track; trk++)
1379 cd->toc[trk].idx_no = 0;
1380 trk = th.ending_track - th.starting_track + 1;
1381 for(retry=0; retry<600 && trk>0; retry++)
1383 if (mcd_getqchan(unit, &q) < 0) break;
1384 idx = bcd2bin(q.idx_no);
1385 if (idx>=th.starting_track && idx<=th.ending_track && q.trk_no==0) {
1386 if (cd->toc[idx].idx_no == 0) {
1393 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1399 /* add a fake last+1 */
1400 idx = th.ending_track + 1;
1401 cd->toc[idx].control = cd->toc[idx-1].control;
1402 cd->toc[idx].addr_type = cd->toc[idx-1].addr_type;
1403 cd->toc[idx].trk_no = 0;
1404 cd->toc[idx].idx_no = MCD_LASTPLUS1;
1405 cd->toc[idx].hd_pos_msf[0] = cd->volinfo.vol_msf[0];
1406 cd->toc[idx].hd_pos_msf[1] = cd->volinfo.vol_msf[1];
1407 cd->toc[idx].hd_pos_msf[2] = cd->volinfo.vol_msf[2];
1411 for (i = th.starting_track; i <= idx; i++)
1412 printf("mcd%d: trk %d idx %d pos %d %d %d\n",
1414 cd->toc[i].idx_no > 0x99 ? cd->toc[i].idx_no :
1415 bcd2bin(cd->toc[i].idx_no),
1416 bcd2bin(cd->toc[i].hd_pos_msf[0]),
1417 bcd2bin(cd->toc[i].hd_pos_msf[1]),
1418 bcd2bin(cd->toc[i].hd_pos_msf[2]));
1421 cd->flags |= MCDTOC;
1428 mcd_toc_entry(int unit, struct ioc_read_toc_single_entry *te)
1430 struct mcd_data *cd = mcd_data + unit;
1431 struct ioc_toc_header th;
1434 if (te->address_format != CD_MSF_FORMAT
1435 && te->address_format != CD_LBA_FORMAT)
1438 /* Copy the toc header */
1439 if ((rc = mcd_toc_header(unit, &th)) != 0)
1442 /* verify starting track */
1445 trk = th.starting_track;
1446 else if (trk == MCD_LASTPLUS1)
1447 trk = th.ending_track + 1;
1448 else if (trk < th.starting_track || trk > th.ending_track + 1)
1451 /* Make sure we have a valid toc */
1452 if ((rc=mcd_read_toc(unit)) != 0)
1455 /* Copy the TOC data. */
1456 if (cd->toc[trk].idx_no == 0)
1459 te->entry.control = cd->toc[trk].control;
1460 te->entry.addr_type = cd->toc[trk].addr_type;
1462 cd->toc[trk].idx_no > 0x99 ? cd->toc[trk].idx_no :
1463 bcd2bin(cd->toc[trk].idx_no);
1464 switch (te->address_format) {
1466 te->entry.addr.msf.unused = 0;
1467 te->entry.addr.msf.minute = bcd2bin(cd->toc[trk].hd_pos_msf[0]);
1468 te->entry.addr.msf.second = bcd2bin(cd->toc[trk].hd_pos_msf[1]);
1469 te->entry.addr.msf.frame = bcd2bin(cd->toc[trk].hd_pos_msf[2]);
1472 te->entry.addr.lba = htonl(msf2hsg(cd->toc[trk].hd_pos_msf, 0));
1480 mcd_toc_entrys(int unit, struct ioc_read_toc_entry *te)
1482 struct mcd_data *cd = mcd_data + unit;
1483 struct cd_toc_entry entries[MCD_MAXTOCS];
1484 struct ioc_toc_header th;
1485 int rc, n, trk, len;
1487 if ( te->data_len < sizeof(entries[0])
1488 || (te->data_len % sizeof(entries[0])) != 0
1489 || (te->address_format != CD_MSF_FORMAT
1490 && te->address_format != CD_LBA_FORMAT)
1494 /* Copy the toc header */
1495 if ((rc = mcd_toc_header(unit, &th)) != 0)
1498 /* verify starting track */
1499 trk = te->starting_track;
1501 trk = th.starting_track;
1502 else if (trk == MCD_LASTPLUS1)
1503 trk = th.ending_track + 1;
1504 else if (trk < th.starting_track || trk > th.ending_track + 1)
1507 len = ((th.ending_track + 1 - trk) + 1) *
1509 if (te->data_len < len)
1511 if (len > sizeof(entries))
1514 /* Make sure we have a valid toc */
1515 if ((rc=mcd_read_toc(unit)) != 0)
1518 /* Copy the TOC data. */
1519 for (n = 0; len > 0 && trk <= th.ending_track + 1; trk++) {
1520 if (cd->toc[trk].idx_no == 0)
1522 entries[n].control = cd->toc[trk].control;
1523 entries[n].addr_type = cd->toc[trk].addr_type;
1525 cd->toc[trk].idx_no > 0x99 ? cd->toc[trk].idx_no :
1526 bcd2bin(cd->toc[trk].idx_no);
1527 switch (te->address_format) {
1529 entries[n].addr.msf.unused = 0;
1530 entries[n].addr.msf.minute = bcd2bin(cd->toc[trk].hd_pos_msf[0]);
1531 entries[n].addr.msf.second = bcd2bin(cd->toc[trk].hd_pos_msf[1]);
1532 entries[n].addr.msf.frame = bcd2bin(cd->toc[trk].hd_pos_msf[2]);
1535 entries[n].addr.lba = htonl(msf2hsg(cd->toc[trk].hd_pos_msf, 0));
1538 len -= sizeof(struct cd_toc_entry);
1542 /* copy the data back */
1543 return copyout(entries, te->data, n * sizeof(struct cd_toc_entry));
1549 struct mcd_data *cd = mcd_data + unit;
1551 /* Verify current status */
1552 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS &&
1553 cd->audio_status != CD_AS_PLAY_PAUSED &&
1554 cd->audio_status != CD_AS_PLAY_COMPLETED) {
1556 printf("mcd%d: stop attempted when not playing, audio status %d\n",
1557 unit, cd->audio_status);
1560 if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS)
1561 if (mcd_send(unit, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
1563 cd->audio_status = CD_AS_PLAY_COMPLETED;
1568 mcd_getqchan(int unit, struct mcd_qchninfo *q)
1570 struct mcd_data *cd = mcd_data + unit;
1572 if (mcd_send(unit, MCD_CMDGETQCHN, MCD_RETRYS) < 0)
1574 if (mcd_get(unit, (char *) q, sizeof(struct mcd_qchninfo)) < 0)
1577 printf("mcd%d: getqchan control=0x%x addr_type=0x%x trk=%d ind=%d ttm=%d:%d.%d dtm=%d:%d.%d\n",
1579 q->control, q->addr_type, bcd2bin(q->trk_no),
1581 bcd2bin(q->trk_size_msf[0]), bcd2bin(q->trk_size_msf[1]),
1582 bcd2bin(q->trk_size_msf[2]),
1583 bcd2bin(q->hd_pos_msf[0]), bcd2bin(q->hd_pos_msf[1]),
1584 bcd2bin(q->hd_pos_msf[2]));
1590 mcd_subchan(int unit, struct ioc_read_subchannel *sc)
1592 struct mcd_data *cd = mcd_data + unit;
1593 struct mcd_qchninfo q;
1594 struct cd_sub_channel_info data;
1598 printf("mcd%d: subchan af=%d, df=%d\n", unit,
1602 if (sc->address_format != CD_MSF_FORMAT &&
1603 sc->address_format != CD_LBA_FORMAT)
1606 if (sc->data_format != CD_CURRENT_POSITION &&
1607 sc->data_format != CD_MEDIA_CATALOG)
1610 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1613 if (mcd_getqchan(unit, &q) < 0)
1616 data.header.audio_status = cd->audio_status;
1617 data.what.position.data_format = sc->data_format;
1619 switch (sc->data_format) {
1620 case CD_MEDIA_CATALOG:
1621 data.what.media_catalog.mc_valid = 1;
1622 data.what.media_catalog.mc_number[0] = '\0';
1625 case CD_CURRENT_POSITION:
1626 data.what.position.control = q.control;
1627 data.what.position.addr_type = q.addr_type;
1628 data.what.position.track_number = bcd2bin(q.trk_no);
1629 data.what.position.index_number = bcd2bin(q.idx_no);
1630 switch (sc->address_format) {
1632 data.what.position.reladdr.msf.unused = 0;
1633 data.what.position.reladdr.msf.minute = bcd2bin(q.trk_size_msf[0]);
1634 data.what.position.reladdr.msf.second = bcd2bin(q.trk_size_msf[1]);
1635 data.what.position.reladdr.msf.frame = bcd2bin(q.trk_size_msf[2]);
1636 data.what.position.absaddr.msf.unused = 0;
1637 data.what.position.absaddr.msf.minute = bcd2bin(q.hd_pos_msf[0]);
1638 data.what.position.absaddr.msf.second = bcd2bin(q.hd_pos_msf[1]);
1639 data.what.position.absaddr.msf.frame = bcd2bin(q.hd_pos_msf[2]);
1642 lba = msf2hsg(q.trk_size_msf, 1);
1644 * Pre-gap has index number of 0, and decreasing MSF
1645 * address. Must be converted to negative LBA, per
1648 if (data.what.position.index_number == 0)
1650 data.what.position.reladdr.lba = htonl(lba);
1651 data.what.position.absaddr.lba = htonl(msf2hsg(q.hd_pos_msf, 0));
1657 return copyout(&data, sc->data, min(sizeof(struct cd_sub_channel_info), sc->data_len));
1661 mcd_playmsf(int unit, struct ioc_play_msf *p)
1663 struct mcd_data *cd = mcd_data + unit;
1664 struct mcd_read2 pb;
1667 printf("mcd%d: playmsf: from %d:%d.%d to %d:%d.%d\n",
1669 p->start_m, p->start_s, p->start_f,
1670 p->end_m, p->end_s, p->end_f);
1672 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
1673 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) ||
1674 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) >
1675 M_msf(cd->volinfo.vol_msf) * 60 * 75 +
1676 S_msf(cd->volinfo.vol_msf) * 75 +
1677 F_msf(cd->volinfo.vol_msf))
1680 pb.start_msf[0] = bin2bcd(p->start_m);
1681 pb.start_msf[1] = bin2bcd(p->start_s);
1682 pb.start_msf[2] = bin2bcd(p->start_f);
1683 pb.end_msf[0] = bin2bcd(p->end_m);
1684 pb.end_msf[1] = bin2bcd(p->end_s);
1685 pb.end_msf[2] = bin2bcd(p->end_f);
1687 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1690 return mcd_play(unit, &pb);
1694 mcd_playtracks(int unit, struct ioc_play_track *pt)
1696 struct mcd_data *cd = mcd_data + unit;
1697 struct mcd_read2 pb;
1698 int a = pt->start_track;
1699 int z = pt->end_track;
1702 if ((rc = mcd_read_toc(unit)) != 0)
1706 printf("mcd%d: playtracks from %d:%d to %d:%d\n", unit,
1707 a, pt->start_index, z, pt->end_index);
1709 if ( a < bcd2bin(cd->volinfo.trk_low)
1710 || a > bcd2bin(cd->volinfo.trk_high)
1712 || z < bcd2bin(cd->volinfo.trk_low)
1713 || z > bcd2bin(cd->volinfo.trk_high))
1716 for (i = 0; i < 3; i++) {
1717 pb.start_msf[i] = cd->toc[a].hd_pos_msf[i];
1718 pb.end_msf[i] = cd->toc[z+1].hd_pos_msf[i];
1721 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1724 return mcd_play(unit, &pb);
1728 mcd_playblocks(int unit, struct ioc_play_blocks *p)
1730 struct mcd_data *cd = mcd_data + unit;
1731 struct mcd_read2 pb;
1734 printf("mcd%d: playblocks: blkno %d length %d\n",
1735 unit, p->blk, p->len);
1737 if (p->blk > cd->disksize || p->len > cd->disksize ||
1738 p->blk < 0 || p->len < 0 ||
1739 (p->blk + p->len) > cd->disksize)
1742 hsg2msf(p->blk, pb.start_msf);
1743 hsg2msf(p->blk + p->len, pb.end_msf);
1745 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1748 return mcd_play(unit, &pb);
1752 mcd_play(int unit, struct mcd_read2 *pb)
1754 struct mcd_data *cd = mcd_data + unit;
1755 int com_port = cd->iobase + mcd_command;
1756 int retry, st = -1, status;
1759 for(retry=0; retry<MCD_RETRYS; retry++) {
1762 outb(com_port, MCD_CMDSINGLESPEEDREAD);
1763 outb(com_port, pb->start_msf[0]);
1764 outb(com_port, pb->start_msf[1]);
1765 outb(com_port, pb->start_msf[2]);
1766 outb(com_port, pb->end_msf[0]);
1767 outb(com_port, pb->end_msf[1]);
1768 outb(com_port, pb->end_msf[2]);
1771 status=mcd_getstat(unit, 0);
1774 else if (status != -2)
1780 printf("mcd%d: media changed\n", unit);
1784 printf("mcd%d: mcd_play retry=%d, status=0x%02x\n", unit, retry, status);
1787 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
1794 struct mcd_data *cd = mcd_data + unit;
1795 struct mcd_qchninfo q;
1798 /* Verify current status */
1799 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS &&
1800 cd->audio_status != CD_AS_PLAY_PAUSED) {
1802 printf("mcd%d: pause attempted when not playing, audio status %d\n",
1803 unit, cd->audio_status);
1807 /* Get the current position */
1808 if (mcd_getqchan(unit, &q) < 0)
1811 /* Copy it into lastpb */
1812 cd->lastpb.start_msf[0] = q.hd_pos_msf[0];
1813 cd->lastpb.start_msf[1] = q.hd_pos_msf[1];
1814 cd->lastpb.start_msf[2] = q.hd_pos_msf[2];
1817 if ((rc=mcd_stop(unit)) != 0)
1820 /* Set the proper status and exit */
1821 cd->audio_status = CD_AS_PLAY_PAUSED;
1826 mcd_resume(int unit)
1828 struct mcd_data *cd = mcd_data + unit;
1830 if (cd->audio_status != CD_AS_PLAY_PAUSED)
1832 return mcd_play(unit, &cd->lastpb);