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
213 static struct cdevsw mcd_cdevsw = {
215 /* close */ mcdclose,
218 /* ioctl */ mcdioctl,
221 /* strategy */ mcdstrategy,
223 /* maj */ CDEV_MAJOR,
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_ointr = mcdintr;
252 cd->iobase = dev->id_iobase;
253 cd->flags |= MCDINIT;
254 mcd_soft_reset(unit);
255 bioq_init(&cd->head);
258 /* wire controller for interrupts and dma */
261 /* name filled in probe */
262 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, 0),
263 UID_ROOT, GID_OPERATOR, 0640, "rmcd%da", unit);
264 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
265 UID_ROOT, GID_OPERATOR, 0640, "rmcd%dc", unit);
266 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, 0),
267 UID_ROOT, GID_OPERATOR, 0640, "mcd%da", unit);
268 make_dev(&mcd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
269 UID_ROOT, GID_OPERATOR, 0640, "mcd%dc", unit);
273 int mcdopen(dev_t dev, int flags, int fmt, struct proc *p)
275 int unit,part,phys,r,retry;
278 unit = mcd_unit(dev);
282 cd = mcd_data + unit;
283 part = mcd_part(dev);
284 phys = mcd_phys(dev);
287 if (!(cd->flags & MCDINIT))
290 /* invalidated in the meantime? mark all open part's invalid */
291 if (!(cd->flags & MCDVALID) && cd->openflags)
294 if (mcd_getstat(unit,1) == -1)
297 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
298 || !(cd->status & MCDDSKIN))
299 for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) {
300 (void) tsleep((caddr_t)cd, PSOCK | PCATCH, "mcdsn1", hz/WAIT_FRAC);
301 if ((r = mcd_getstat(unit,1)) == -1)
307 if (( (cd->status & (MCDDOOROPEN|MCDDSKCHNG))
308 || !(cd->status & MCDDSKIN)
310 && major(dev) == CDEV_MAJOR && part == RAW_PART
312 cd->openflags |= (1<<part);
314 cd->partflags[part] |= MCDREADRAW;
317 if (cd->status & MCDDOOROPEN) {
318 printf("mcd%d: door is open\n", unit);
321 if (!(cd->status & MCDDSKIN)) {
322 printf("mcd%d: no CD inside\n", unit);
325 if (cd->status & MCDDSKCHNG) {
326 printf("mcd%d: CD not sensed\n", unit);
330 if (mcdsize(dev) < 0) {
331 if (major(dev) == CDEV_MAJOR && part == RAW_PART) {
332 cd->openflags |= (1<<part);
334 cd->partflags[part] |= MCDREADRAW;
337 printf("mcd%d: failed to get disk size\n",unit);
340 cd->flags |= MCDVALID;
342 /* XXX get a default disklabel */
343 mcd_getdisklabel(unit);
345 MCD_TRACE("open: partition=%d, disksize = %ld, blksize=%d\n",
346 part, cd->disksize, cd->blksize);
348 dev->si_bsize_phys = cd->blksize;
350 if (part == RAW_PART ||
351 (part < cd->dlabel.d_npartitions &&
352 cd->dlabel.d_partitions[part].p_fstype != FS_UNUSED)) {
353 cd->openflags |= (1<<part);
354 if (part == RAW_PART && phys)
355 cd->partflags[part] |= MCDREADRAW;
356 (void) mcd_lock_door(unit, MCD_LK_LOCK);
357 if (!(cd->flags & MCDVALID))
365 int mcdclose(dev_t dev, int flags, int fmt, struct proc *p)
370 unit = mcd_unit(dev);
374 cd = mcd_data + unit;
375 part = mcd_part(dev);
377 if (!(cd->flags & MCDINIT) || !(cd->openflags & (1<<part)))
380 MCD_TRACE("close: partition=%d\n", part);
382 (void) mcd_lock_door(unit, MCD_LK_UNLOCK);
383 cd->openflags &= ~(1<<part);
384 cd->partflags[part] &= ~MCDREADRAW;
390 mcdstrategy(struct bio *bp)
395 int unit = mcd_unit(bp->bio_dev);
397 cd = mcd_data + unit;
400 /*MCD_TRACE("strategy: buf=0x%lx, unit=%ld, block#=%ld bcount=%ld\n",
401 bp,unit,bp->bio_blkno,bp->bio_bcount);*/
402 if (unit >= NMCD || bp->bio_blkno < 0) {
403 printf("mcdstrategy: unit = %d, blkno = %ld, bcount = %ld\n",
404 unit, (long)bp->bio_blkno, bp->bio_bcount);
405 printf("mcd: mcdstratregy failure");
406 bp->bio_error = EINVAL;
407 bp->bio_flags |= BIO_ERROR;
411 /* if device invalidated (e.g. media change, door open), error */
412 if (!(cd->flags & MCDVALID)) {
413 MCD_TRACE("strategy: drive not valid\n");
419 if (!(bp->bio_cmd == BIO_READ)) {
420 bp->bio_error = EROFS;
424 /* no data to read */
425 if (bp->bio_bcount == 0)
428 /* for non raw access, check partition limits */
429 if (mcd_part(bp->bio_dev) != RAW_PART) {
430 if (!(cd->flags & MCDLABEL)) {
434 /* adjust transfer if necessary */
435 if (bounds_check_with_label(bp,&cd->dlabel,1) <= 0) {
439 bp->bio_pblkno = bp->bio_blkno;
445 bioqdisksort(&cd->head, bp);
448 /* now check whether we can perform processing */
453 bp->bio_flags |= BIO_ERROR;
455 bp->bio_resid = bp->bio_bcount;
460 static void mcd_start(int unit)
462 struct mcd_data *cd = mcd_data + unit;
467 if (cd->flags & MCDMBXBSY) {
472 bp = bioq_first(&cd->head);
474 /* block found to process, dequeue */
475 /*MCD_TRACE("mcd_start: found block bp=0x%x\n",bp,0,0,0);*/
476 bioq_remove(&cd->head, bp);
485 if (!(cd->flags & MCDVALID)) {
486 MCD_TRACE("mcd_start: drive not valid\n");
490 p = cd->dlabel.d_partitions + mcd_part(bp->bio_dev);
492 cd->flags |= MCDMBXBSY;
493 if (cd->partflags[mcd_part(bp->bio_dev)] & MCDREADRAW)
494 cd->flags |= MCDREADRAW;
496 cd->mbx.port = cd->iobase;
497 cd->mbx.retry = MCD_RETRYS;
499 cd->mbx.p_offset = p->p_offset;
501 /* calling the read routine */
502 mcd_doread(MCD_S_BEGIN,&(cd->mbx));
503 /* triggers mcd_start, when successful finished */
507 int mcdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
510 int unit,part,retry,r;
512 unit = mcd_unit(dev);
513 part = mcd_part(dev);
514 cd = mcd_data + unit;
516 if (mcd_getstat(unit, 1) == -1) /* detect disk change too */
518 MCD_TRACE("ioctl called 0x%lx\n", cmd);
531 return mcd_eject(unit);
539 return mcd_hard_reset(unit);
541 return mcd_lock_door(unit, MCD_LK_UNLOCK);
543 return mcd_lock_door(unit, MCD_LK_LOCK);
545 return mcd_inject(unit);
548 if (!(cd->flags & MCDVALID)) {
549 if ( major(dev) != CDEV_MAJOR
551 || !(cd->openflags & (1<<RAW_PART))
554 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
555 || !(cd->status & MCDDSKIN))
556 for (retry = 0; retry < DISK_SENSE_SECS * WAIT_FRAC; retry++) {
557 (void) tsleep((caddr_t)cd, PSOCK | PCATCH, "mcdsn2", hz/WAIT_FRAC);
558 if ((r = mcd_getstat(unit,1)) == -1)
563 if ( (cd->status & (MCDDOOROPEN|MCDDSKCHNG))
564 || !(cd->status & MCDDSKIN)
568 cd->flags |= MCDVALID;
569 mcd_getdisklabel(unit);
571 cd->partflags[part] |= MCDREADRAW;
572 (void) mcd_lock_door(unit, MCD_LK_LOCK);
573 if (!(cd->flags & MCDVALID))
579 *(struct disklabel *) addr = cd->dlabel;
582 ((struct partinfo *) addr)->disklab = &cd->dlabel;
583 ((struct partinfo *) addr)->part =
584 &cd->dlabel.d_partitions[mcd_part(dev)];
588 * a bit silly, but someone might want to test something on a
593 if ((flags & FWRITE) == 0)
596 return setdisklabel(&cd->dlabel,
597 (struct disklabel *) addr,
602 case CDIOCPLAYTRACKS:
603 return mcd_playtracks(unit, (struct ioc_play_track *) addr);
604 case CDIOCPLAYBLOCKS:
605 return mcd_playblocks(unit, (struct ioc_play_blocks *) addr);
607 return mcd_playmsf(unit, (struct ioc_play_msf *) addr);
608 case CDIOCREADSUBCHANNEL:
609 return mcd_subchan(unit, (struct ioc_read_subchannel *) addr);
610 case CDIOREADTOCHEADER:
611 return mcd_toc_header(unit, (struct ioc_toc_header *) addr);
612 case CDIOREADTOCENTRYS:
613 return mcd_toc_entrys(unit, (struct ioc_read_toc_entry *) addr);
615 return mcd_resume(unit);
617 return mcd_pause(unit);
619 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
623 return mcd_stop(unit);
630 /* this could have been taken from scsi/cd.c, but it is not clear
631 * whether the scsi cd driver is linked in
633 static int mcd_getdisklabel(int unit)
635 struct mcd_data *cd = mcd_data + unit;
637 if (cd->flags & MCDLABEL)
640 bzero(&cd->dlabel,sizeof(struct disklabel));
641 /* filled with spaces first */
642 strncpy(cd->dlabel.d_typename," ",
643 sizeof(cd->dlabel.d_typename));
644 strncpy(cd->dlabel.d_typename, cd->name,
645 min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1));
646 strncpy(cd->dlabel.d_packname,"unknown ",
647 sizeof(cd->dlabel.d_packname));
648 cd->dlabel.d_secsize = cd->blksize;
649 cd->dlabel.d_nsectors = 100;
650 cd->dlabel.d_ntracks = 1;
651 cd->dlabel.d_ncylinders = (cd->disksize/100)+1;
652 cd->dlabel.d_secpercyl = 100;
653 cd->dlabel.d_secperunit = cd->disksize;
654 cd->dlabel.d_rpm = 300;
655 cd->dlabel.d_interleave = 1;
656 cd->dlabel.d_flags = D_REMOVABLE;
657 cd->dlabel.d_npartitions= 1;
658 cd->dlabel.d_partitions[0].p_offset = 0;
659 cd->dlabel.d_partitions[0].p_size = cd->disksize;
660 cd->dlabel.d_partitions[0].p_fstype = 9;
661 cd->dlabel.d_magic = DISKMAGIC;
662 cd->dlabel.d_magic2 = DISKMAGIC;
663 cd->dlabel.d_checksum = dkcksum(&cd->dlabel);
665 cd->flags |= MCDLABEL;
669 int mcdsize(dev_t dev)
672 int unit = mcd_unit(dev);
673 struct mcd_data *cd = mcd_data + unit;
675 if (mcd_volinfo(unit) == 0) {
676 cd->blksize = MCDBLK;
677 size = msf2hsg(cd->volinfo.vol_msf, 0);
678 cd->disksize = size * (MCDBLK/DEV_BSIZE);
684 /***************************************************************
685 * lower level of driver starts here
686 **************************************************************/
691 0x00,0x00,0x10,0x20,0x00,0x30,0x00,0x00,
692 0x00,0x10,0x40,0x50,0x00,0x00,0x00,0x00
697 0x00,0x01,0x00,0x03,0x00,0x05,0x06,0x07,
703 mcd_configure(struct mcd_data *cd)
705 outb(cd->iobase+mcd_config,cd->config);
709 /* Wait for non-busy - return 0 on timeout */
711 twiddle_thumbs(int port, int unit, int count, char *whine)
715 for (i = 0; i < count; i++) {
716 if (!(inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL))
720 printf("mcd%d: timeout %s\n", unit, whine);
724 /* check to see if a Mitsumi CD-ROM is attached to the ISA bus */
727 mcd_probe(struct isa_device *dev)
729 int port = dev->id_iobase;
730 int unit = dev->id_unit;
732 unsigned char stbytes[3];
736 cdevsw_add(&mcd_cdevsw);
738 mcd_data[unit].flags = MCDPROBING;
741 /* get irq/drq configuration word */
742 mcd_data[unit].config = irqs[dev->id_irq]; /* | drqs[dev->id_drq];*/
744 mcd_data[unit].config = 0;
748 outb(port+MCD_FLAGS, M_RESET);
751 * delay awhile by getting any pending garbage (old data) and
754 for (i = 1000000; i != 0; i--)
758 outb(port+MCD_DATA, MCD_CMDGETSTAT);
759 if (!twiddle_thumbs(port, unit, 1000000, "getting status"))
760 return 0; /* Timeout */
761 /* Get version information */
762 outb(port+MCD_DATA, MCD_CMDCONTINFO);
763 for (j = 0; j < 3; j++) {
764 if (!twiddle_thumbs(port, unit, 3000, "getting version info"))
766 stbytes[j] = (inb(port+MCD_DATA) & 0xFF);
768 if (stbytes[1] == stbytes[2])
770 if (stbytes[2] >= 4 || stbytes[1] != 'M') {
771 outb(port+MCD_CTRL, M_PICKLE);
772 mcd_data[unit].flags |= MCDNEWMODEL;
774 mcd_data[unit].read_command = MCD_CMDSINGLESPEEDREAD;
775 switch (stbytes[1]) {
777 if (stbytes[2] <= 2) {
778 mcd_data[unit].type = MCD_TYPE_LU002S;
779 mcd_data[unit].name = "Mitsumi LU002S";
780 } else if (stbytes[2] <= 5) {
781 mcd_data[unit].type = MCD_TYPE_LU005S;
782 mcd_data[unit].name = "Mitsumi LU005S";
784 mcd_data[unit].type = MCD_TYPE_LU006S;
785 mcd_data[unit].name = "Mitsumi LU006S";
789 mcd_data[unit].type = MCD_TYPE_FX001;
790 mcd_data[unit].name = "Mitsumi FX001";
793 mcd_data[unit].type = MCD_TYPE_FX001D;
794 mcd_data[unit].name = "Mitsumi FX001D";
795 mcd_data[unit].read_command = MCD_CMDDOUBLESPEEDREAD;
798 mcd_data[unit].type = MCD_TYPE_UNKNOWN;
799 mcd_data[unit].name = "Mitsumi ???";
802 printf("mcd%d: type %s, version info: %c %x\n", unit, mcd_data[unit].name,
803 stbytes[1], stbytes[2]);
810 mcd_waitrdy(int port,int dly)
814 /* wait until flag port senses status ready */
815 for (i=0; i<dly; i+=MIN_DELAY) {
816 if (!(inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL))
824 mcd_getreply(int unit,int dly)
826 struct mcd_data *cd = mcd_data + unit;
827 int port = cd->iobase;
829 /* wait data to become ready */
830 if (mcd_waitrdy(port,dly)<0) {
831 printf("mcd%d: timeout getreply\n",unit);
836 return inb(port+mcd_status) & 0xFF;
840 mcd_getstat(int unit,int sflg)
843 struct mcd_data *cd = mcd_data + unit;
844 int port = cd->iobase;
848 outb(port+mcd_command, MCD_CMDGETSTAT);
849 i = mcd_getreply(unit,DELAY_GETREPLY);
850 if (i<0 || (i & MCD_ST_CMDCHECK)) {
851 cd->curr_mode = MCD_MD_UNKNOWN;
857 if (mcd_setflags(unit,cd) < 0)
863 mcd_setflags(int unit, struct mcd_data *cd)
866 if ( (cd->status & (MCDDSKCHNG|MCDDOOROPEN))
867 || !(cd->status & MCDDSKIN)) {
868 MCD_TRACE("setflags: sensed DSKCHNG or DOOROPEN or !DSKIN\n");
869 mcd_soft_reset(unit);
873 if (cd->status & MCDAUDIOBSY)
874 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
875 else if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS)
876 cd->audio_status = CD_AS_PLAY_COMPLETED;
881 mcd_get(int unit, char *buf, int nmax)
885 for (i=0; i<nmax; i++) {
887 if ((k = mcd_getreply(unit,DELAY_GETREPLY)) < 0) {
888 printf("mcd%d: timeout mcd_get\n",unit);
897 mcd_send(int unit, int cmd,int nretrys)
900 int port = mcd_data[unit].iobase;
902 /*MCD_TRACE("mcd_send: command = 0x%02x\n",cmd,0,0,0);*/
903 for (i=0; i<nretrys; i++) {
904 outb(port+mcd_command, cmd);
905 if ((k=mcd_getstat(unit,0)) != -1)
909 printf("mcd%d: media changed\n",unit);
913 printf("mcd%d: mcd_send retry cnt exceeded\n",unit);
916 /*MCD_TRACE("mcd_send: done\n",0,0,0,0);*/
921 hsg2msf(int hsg, bcd_t *msf)
924 F_msf(msf) = bin2bcd(hsg % 75);
926 S_msf(msf) = bin2bcd(hsg % 60);
928 M_msf(msf) = bin2bcd(hsg);
932 msf2hsg(bcd_t *msf, int relative)
934 return (bcd2bin(M_msf(msf)) * 60 + bcd2bin(S_msf(msf))) * 75 +
935 bcd2bin(F_msf(msf)) - (!relative) * 150;
939 mcd_volinfo(int unit)
941 struct mcd_data *cd = mcd_data + unit;
943 /* Just return if we already have it */
944 if (cd->flags & MCDVOLINFO) return 0;
946 /*MCD_TRACE("mcd_volinfo: enter\n",0,0,0,0);*/
948 /* send volume info command */
949 if (mcd_send(unit,MCD_CMDGETVOLINFO,MCD_RETRYS) < 0)
953 if (mcd_get(unit,(char*) &cd->volinfo,sizeof(struct mcd_volinfo)) < 0) {
954 printf("mcd%d: mcd_volinfo: error read data\n",unit);
958 if (cd->volinfo.trk_low > 0 &&
959 cd->volinfo.trk_high >= cd->volinfo.trk_low
961 cd->flags |= MCDVOLINFO; /* volinfo is OK */
972 MCD_TRACE("stray interrupt\n");
975 /* state machine to process read requests
976 * initialize with MCD_S_BEGIN: calculate sizes, and read status
977 * MCD_S_WAITSTAT: wait for status reply, set mode
978 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
979 * MCD_S_WAITREAD: wait for read ready, read data
981 static struct mcd_mbx *mbxsave;
982 static struct callout_handle tohandle = CALLOUT_HANDLE_INITIALIZER(&tohandle);
985 mcd_timeout(void *arg)
987 mcd_doread((int)arg, mbxsave);
991 mcd_doread(int state, struct mcd_mbx *mbxin)
993 struct mcd_mbx *mbx = (state!=MCD_S_BEGIN) ? mbxsave : mbxin;
994 int unit = mbx->unit;
995 int port = mbx->port;
996 int com_port = mbx->port + mcd_command;
997 int data_port = mbx->port + mcd_rdata;
998 struct bio *bp = mbx->bp;
999 struct mcd_data *cd = mcd_data + unit;
1002 struct mcd_read2 rbuf;
1009 mbx = mbxsave = mbxin;
1014 outb(com_port, MCD_CMDGETSTAT);
1015 mbx->count = RDELAY_WAITSTAT;
1016 tohandle = timeout(mcd_timeout,
1017 (caddr_t)MCD_S_WAITSTAT,hz/100); /* XXX */
1019 case MCD_S_WAITSTAT:
1020 untimeout(mcd_timeout,(caddr_t)MCD_S_WAITSTAT, tohandle);
1021 if (mbx->count-- >= 0) {
1022 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) {
1023 timeout(mcd_timeout,
1024 (caddr_t)MCD_S_WAITSTAT,hz/100); /* XXX */
1027 cd->status = inb(port+mcd_status) & 0xFF;
1028 if (cd->status & MCD_ST_CMDCHECK)
1030 if (mcd_setflags(unit,cd) < 0)
1032 MCD_TRACE("got WAITSTAT delay=%d\n",
1033 RDELAY_WAITSTAT-mbx->count);
1034 /* reject, if audio active */
1035 if (cd->status & MCDAUDIOBSY) {
1036 printf("mcd%d: audio is active\n",unit);
1041 /* to check for raw/cooked mode */
1042 if (cd->flags & MCDREADRAW) {
1047 mbx->sz = cd->blksize;
1050 if (rm == cd->curr_mode)
1053 mbx->count = RDELAY_WAITMODE;
1055 cd->curr_mode = MCD_MD_UNKNOWN;
1057 mcd_put(com_port, MCD_CMDSETMODE);
1058 mcd_put(com_port, rm);
1060 tohandle = timeout(mcd_timeout,
1061 (caddr_t)MCD_S_WAITMODE,hz/100); /* XXX */
1064 printf("mcd%d: timeout getstatus\n",unit);
1068 case MCD_S_WAITMODE:
1069 untimeout(mcd_timeout,(caddr_t)MCD_S_WAITMODE, tohandle);
1070 if (mbx->count-- < 0) {
1071 printf("mcd%d: timeout set mode\n",unit);
1074 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL) {
1075 tohandle = timeout(mcd_timeout,
1076 (caddr_t)MCD_S_WAITMODE,hz/100);
1079 cd->status = inb(port+mcd_status) & 0xFF;
1080 if (cd->status & MCD_ST_CMDCHECK) {
1081 cd->curr_mode = MCD_MD_UNKNOWN;
1084 if (mcd_setflags(unit,cd) < 0)
1086 cd->curr_mode = mbx->mode;
1087 MCD_TRACE("got WAITMODE delay=%d\n",
1088 RDELAY_WAITMODE-mbx->count);
1090 /* for first block */
1091 mbx->nblk = (bp->bio_bcount + (mbx->sz-1)) / mbx->sz;
1095 blknum = (bp->bio_blkno / (mbx->sz/DEV_BSIZE))
1096 + mbx->p_offset + mbx->skip/mbx->sz;
1098 MCD_TRACE("mcd_doread: read blknum=%d for bp=%p\n",
1101 /* build parameter block */
1102 hsg2msf(blknum,rbuf.start_msf);
1104 /* send the read command */
1106 mcd_put(com_port,cd->read_command);
1107 mcd_put(com_port,rbuf.start_msf[0]);
1108 mcd_put(com_port,rbuf.start_msf[1]);
1109 mcd_put(com_port,rbuf.start_msf[2]);
1110 mcd_put(com_port,0);
1111 mcd_put(com_port,0);
1112 mcd_put(com_port,1);
1115 /* Spin briefly (<= 2ms) to avoid missing next block */
1116 for (i = 0; i < 20; i++) {
1117 k = inb(port+MCD_FLAGS);
1118 if (!(k & MFL_DATA_NOT_AVAIL))
1123 mbx->count = RDELAY_WAITREAD;
1124 tohandle = timeout(mcd_timeout,
1125 (caddr_t)MCD_S_WAITREAD,hz/100); /* XXX */
1127 case MCD_S_WAITREAD:
1128 untimeout(mcd_timeout,(caddr_t)MCD_S_WAITREAD, tohandle);
1129 if (mbx->count-- > 0) {
1130 k = inb(port+MCD_FLAGS);
1131 if (!(k & MFL_DATA_NOT_AVAIL)) { /* XXX */
1132 MCD_TRACE("got data delay=%d\n",
1133 RDELAY_WAITREAD-mbx->count);
1136 addr = bp->bio_data + mbx->skip;
1138 outb(port+mcd_ctl2,0x04); /* XXX */
1139 for (i=0; i<mbx->sz; i++)
1140 *addr++ = inb(data_port);
1141 outb(port+mcd_ctl2,0x0c); /* XXX */
1143 k = inb(port+MCD_FLAGS);
1144 /* If we still have some junk, read it too */
1145 if (!(k & MFL_DATA_NOT_AVAIL)) {
1146 outb(port+mcd_ctl2,0x04); /* XXX */
1147 (void)inb(data_port);
1148 (void)inb(data_port);
1149 outb(port+mcd_ctl2,0x0c); /* XXX */
1152 if (--mbx->nblk > 0) {
1153 mbx->skip += mbx->sz;
1161 cd->flags &= ~(MCDMBXBSY|MCDREADRAW);
1162 mcd_start(mbx->unit);
1165 if (!(k & MFL_STATUS_NOT_AVAIL)) {
1166 cd->status = inb(port+mcd_status) & 0xFF;
1167 if (cd->status & MCD_ST_CMDCHECK)
1169 if (mcd_setflags(unit,cd) < 0)
1172 tohandle = timeout(mcd_timeout,
1173 (caddr_t)MCD_S_WAITREAD,hz/100); /* XXX */
1176 printf("mcd%d: timeout read data\n",unit);
1182 if (mbx->retry-- > 0) {
1183 printf("mcd%d: retrying\n",unit);
1184 state = MCD_S_BEGIN1;
1188 /* invalidate the buffer */
1189 bp->bio_flags |= BIO_ERROR;
1190 bp->bio_resid = bp->bio_bcount;
1193 cd->flags &= ~(MCDMBXBSY|MCDREADRAW);
1194 mcd_start(mbx->unit);
1198 printf("mcd%d: media changed\n", unit);
1202 printf("mcd%d: unit timeout, resetting\n",mbx->unit);
1203 outb(mbx->port+mcd_reset,MCD_CMDRESET);
1205 (void)mcd_getstat(mbx->unit,1);
1206 (void)mcd_getstat(mbx->unit,1);
1207 /*cd->status &= ~MCDDSKCHNG; */
1208 cd->debug = 1; /* preventive set debug mode */
1215 mcd_lock_door(int unit, int lock)
1217 struct mcd_data *cd = mcd_data + unit;
1218 int port = cd->iobase;
1220 outb(port+mcd_command, MCD_CMDLOCKDRV);
1221 outb(port+mcd_command, lock);
1222 if (mcd_getstat(unit,0) == -1)
1228 mcd_close_tray(int unit)
1230 struct mcd_data *cd = mcd_data + unit;
1231 int port = cd->iobase;
1234 if (mcd_getstat(unit,1) == -1)
1236 if (cd->status & MCDDOOROPEN) {
1237 outb(port+mcd_command, MCD_CMDCLOSETRAY);
1238 for (retry = 0; retry < CLOSE_TRAY_SECS * WAIT_FRAC; retry++) {
1239 if (inb(port+MCD_FLAGS) & MFL_STATUS_NOT_AVAIL)
1240 (void) tsleep((caddr_t)cd, PSOCK | PCATCH, "mcdcls", hz/WAIT_FRAC);
1242 if ((r = mcd_getstat(unit,0)) == -1)
1255 struct mcd_data *cd = mcd_data + unit;
1256 int port = cd->iobase, r;
1258 if (mcd_getstat(unit,1) == -1) /* detect disk change too */
1260 if (cd->status & MCDDOOROPEN)
1262 if ((r = mcd_stop(unit)) == EIO)
1264 outb(port+mcd_command, MCD_CMDEJECTDISK);
1265 if (mcd_getstat(unit,0) == -1)
1271 mcd_inject(int unit)
1273 struct mcd_data *cd = mcd_data + unit;
1275 if (mcd_getstat(unit,1) == -1) /* detect disk change too */
1277 if (cd->status & MCDDOOROPEN)
1278 return mcd_close_tray(unit);
1283 mcd_hard_reset(int unit)
1285 struct mcd_data *cd = mcd_data + unit;
1286 int port = cd->iobase;
1288 outb(port+mcd_reset,MCD_CMDRESET);
1289 cd->curr_mode = MCD_MD_UNKNOWN;
1290 cd->audio_status = CD_AS_AUDIO_INVALID;
1295 mcd_soft_reset(int unit)
1297 struct mcd_data *cd = mcd_data + unit;
1300 cd->flags &= (MCDINIT|MCDPROBING|MCDNEWMODEL);
1301 cd->curr_mode = MCD_MD_UNKNOWN;
1302 for (i=0; i<MAXPARTITIONS; i++) cd->partflags[i] = 0;
1303 cd->audio_status = CD_AS_AUDIO_INVALID;
1307 mcd_setmode(int unit, int mode)
1309 struct mcd_data *cd = mcd_data + unit;
1310 int port = cd->iobase;
1313 if (cd->curr_mode == mode)
1316 printf("mcd%d: setting mode to %d\n", unit, mode);
1317 for(retry=0; retry<MCD_RETRYS; retry++)
1319 cd->curr_mode = MCD_MD_UNKNOWN;
1320 outb(port+mcd_command, MCD_CMDSETMODE);
1321 outb(port+mcd_command, mode);
1322 if ((st = mcd_getstat(unit, 0)) >= 0) {
1323 cd->curr_mode = mode;
1327 printf("mcd%d: media changed\n", unit);
1336 mcd_toc_header(int unit, struct ioc_toc_header *th)
1338 struct mcd_data *cd = mcd_data + unit;
1341 if ((r = mcd_volinfo(unit)) != 0)
1344 th->starting_track = bcd2bin(cd->volinfo.trk_low);
1345 th->ending_track = bcd2bin(cd->volinfo.trk_high);
1346 th->len = 2 * sizeof(u_char) /* start & end tracks */ +
1347 (th->ending_track + 1 - th->starting_track + 1) *
1348 sizeof(struct cd_toc_entry);
1354 mcd_read_toc(int unit)
1356 struct mcd_data *cd = mcd_data + unit;
1357 struct ioc_toc_header th;
1358 struct mcd_qchninfo q;
1359 int rc, trk, idx, retry;
1361 /* Only read TOC if needed */
1362 if (cd->flags & MCDTOC)
1366 printf("mcd%d: reading toc header\n", unit);
1368 if ((rc = mcd_toc_header(unit, &th)) != 0)
1371 if (mcd_send(unit, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
1374 if (mcd_setmode(unit, MCD_MD_TOC) != 0)
1378 printf("mcd%d: get_toc reading qchannel info\n",unit);
1380 for(trk=th.starting_track; trk<=th.ending_track; trk++)
1381 cd->toc[trk].idx_no = 0;
1382 trk = th.ending_track - th.starting_track + 1;
1383 for(retry=0; retry<600 && trk>0; retry++)
1385 if (mcd_getqchan(unit, &q) < 0) break;
1386 idx = bcd2bin(q.idx_no);
1387 if (idx>=th.starting_track && idx<=th.ending_track && q.trk_no==0) {
1388 if (cd->toc[idx].idx_no == 0) {
1395 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1401 /* add a fake last+1 */
1402 idx = th.ending_track + 1;
1403 cd->toc[idx].control = cd->toc[idx-1].control;
1404 cd->toc[idx].addr_type = cd->toc[idx-1].addr_type;
1405 cd->toc[idx].trk_no = 0;
1406 cd->toc[idx].idx_no = MCD_LASTPLUS1;
1407 cd->toc[idx].hd_pos_msf[0] = cd->volinfo.vol_msf[0];
1408 cd->toc[idx].hd_pos_msf[1] = cd->volinfo.vol_msf[1];
1409 cd->toc[idx].hd_pos_msf[2] = cd->volinfo.vol_msf[2];
1413 for (i = th.starting_track; i <= idx; i++)
1414 printf("mcd%d: trk %d idx %d pos %d %d %d\n",
1416 cd->toc[i].idx_no > 0x99 ? cd->toc[i].idx_no :
1417 bcd2bin(cd->toc[i].idx_no),
1418 bcd2bin(cd->toc[i].hd_pos_msf[0]),
1419 bcd2bin(cd->toc[i].hd_pos_msf[1]),
1420 bcd2bin(cd->toc[i].hd_pos_msf[2]));
1423 cd->flags |= MCDTOC;
1430 mcd_toc_entry(int unit, struct ioc_read_toc_single_entry *te)
1432 struct mcd_data *cd = mcd_data + unit;
1433 struct ioc_toc_header th;
1436 if (te->address_format != CD_MSF_FORMAT
1437 && te->address_format != CD_LBA_FORMAT)
1440 /* Copy the toc header */
1441 if ((rc = mcd_toc_header(unit, &th)) != 0)
1444 /* verify starting track */
1447 trk = th.starting_track;
1448 else if (trk == MCD_LASTPLUS1)
1449 trk = th.ending_track + 1;
1450 else if (trk < th.starting_track || trk > th.ending_track + 1)
1453 /* Make sure we have a valid toc */
1454 if ((rc=mcd_read_toc(unit)) != 0)
1457 /* Copy the TOC data. */
1458 if (cd->toc[trk].idx_no == 0)
1461 te->entry.control = cd->toc[trk].control;
1462 te->entry.addr_type = cd->toc[trk].addr_type;
1464 cd->toc[trk].idx_no > 0x99 ? cd->toc[trk].idx_no :
1465 bcd2bin(cd->toc[trk].idx_no);
1466 switch (te->address_format) {
1468 te->entry.addr.msf.unused = 0;
1469 te->entry.addr.msf.minute = bcd2bin(cd->toc[trk].hd_pos_msf[0]);
1470 te->entry.addr.msf.second = bcd2bin(cd->toc[trk].hd_pos_msf[1]);
1471 te->entry.addr.msf.frame = bcd2bin(cd->toc[trk].hd_pos_msf[2]);
1474 te->entry.addr.lba = htonl(msf2hsg(cd->toc[trk].hd_pos_msf, 0));
1482 mcd_toc_entrys(int unit, struct ioc_read_toc_entry *te)
1484 struct mcd_data *cd = mcd_data + unit;
1485 struct cd_toc_entry entries[MCD_MAXTOCS];
1486 struct ioc_toc_header th;
1487 int rc, n, trk, len;
1489 if ( te->data_len < sizeof(entries[0])
1490 || (te->data_len % sizeof(entries[0])) != 0
1491 || (te->address_format != CD_MSF_FORMAT
1492 && te->address_format != CD_LBA_FORMAT)
1496 /* Copy the toc header */
1497 if ((rc = mcd_toc_header(unit, &th)) != 0)
1500 /* verify starting track */
1501 trk = te->starting_track;
1503 trk = th.starting_track;
1504 else if (trk == MCD_LASTPLUS1)
1505 trk = th.ending_track + 1;
1506 else if (trk < th.starting_track || trk > th.ending_track + 1)
1509 len = ((th.ending_track + 1 - trk) + 1) *
1511 if (te->data_len < len)
1513 if (len > sizeof(entries))
1516 /* Make sure we have a valid toc */
1517 if ((rc=mcd_read_toc(unit)) != 0)
1520 /* Copy the TOC data. */
1521 for (n = 0; len > 0 && trk <= th.ending_track + 1; trk++) {
1522 if (cd->toc[trk].idx_no == 0)
1524 entries[n].control = cd->toc[trk].control;
1525 entries[n].addr_type = cd->toc[trk].addr_type;
1527 cd->toc[trk].idx_no > 0x99 ? cd->toc[trk].idx_no :
1528 bcd2bin(cd->toc[trk].idx_no);
1529 switch (te->address_format) {
1531 entries[n].addr.msf.unused = 0;
1532 entries[n].addr.msf.minute = bcd2bin(cd->toc[trk].hd_pos_msf[0]);
1533 entries[n].addr.msf.second = bcd2bin(cd->toc[trk].hd_pos_msf[1]);
1534 entries[n].addr.msf.frame = bcd2bin(cd->toc[trk].hd_pos_msf[2]);
1537 entries[n].addr.lba = htonl(msf2hsg(cd->toc[trk].hd_pos_msf, 0));
1540 len -= sizeof(struct cd_toc_entry);
1544 /* copy the data back */
1545 return copyout(entries, te->data, n * sizeof(struct cd_toc_entry));
1551 struct mcd_data *cd = mcd_data + unit;
1553 /* Verify current status */
1554 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS &&
1555 cd->audio_status != CD_AS_PLAY_PAUSED &&
1556 cd->audio_status != CD_AS_PLAY_COMPLETED) {
1558 printf("mcd%d: stop attempted when not playing, audio status %d\n",
1559 unit, cd->audio_status);
1562 if (cd->audio_status == CD_AS_PLAY_IN_PROGRESS)
1563 if (mcd_send(unit, MCD_CMDSTOPAUDIO, MCD_RETRYS) < 0)
1565 cd->audio_status = CD_AS_PLAY_COMPLETED;
1570 mcd_getqchan(int unit, struct mcd_qchninfo *q)
1572 struct mcd_data *cd = mcd_data + unit;
1574 if (mcd_send(unit, MCD_CMDGETQCHN, MCD_RETRYS) < 0)
1576 if (mcd_get(unit, (char *) q, sizeof(struct mcd_qchninfo)) < 0)
1579 printf("mcd%d: getqchan control=0x%x addr_type=0x%x trk=%d ind=%d ttm=%d:%d.%d dtm=%d:%d.%d\n",
1581 q->control, q->addr_type, bcd2bin(q->trk_no),
1583 bcd2bin(q->trk_size_msf[0]), bcd2bin(q->trk_size_msf[1]),
1584 bcd2bin(q->trk_size_msf[2]),
1585 bcd2bin(q->hd_pos_msf[0]), bcd2bin(q->hd_pos_msf[1]),
1586 bcd2bin(q->hd_pos_msf[2]));
1592 mcd_subchan(int unit, struct ioc_read_subchannel *sc)
1594 struct mcd_data *cd = mcd_data + unit;
1595 struct mcd_qchninfo q;
1596 struct cd_sub_channel_info data;
1600 printf("mcd%d: subchan af=%d, df=%d\n", unit,
1604 if (sc->address_format != CD_MSF_FORMAT &&
1605 sc->address_format != CD_LBA_FORMAT)
1608 if (sc->data_format != CD_CURRENT_POSITION &&
1609 sc->data_format != CD_MEDIA_CATALOG)
1612 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1615 if (mcd_getqchan(unit, &q) < 0)
1618 data.header.audio_status = cd->audio_status;
1619 data.what.position.data_format = sc->data_format;
1621 switch (sc->data_format) {
1622 case CD_MEDIA_CATALOG:
1623 data.what.media_catalog.mc_valid = 1;
1624 data.what.media_catalog.mc_number[0] = '\0';
1627 case CD_CURRENT_POSITION:
1628 data.what.position.control = q.control;
1629 data.what.position.addr_type = q.addr_type;
1630 data.what.position.track_number = bcd2bin(q.trk_no);
1631 data.what.position.index_number = bcd2bin(q.idx_no);
1632 switch (sc->address_format) {
1634 data.what.position.reladdr.msf.unused = 0;
1635 data.what.position.reladdr.msf.minute = bcd2bin(q.trk_size_msf[0]);
1636 data.what.position.reladdr.msf.second = bcd2bin(q.trk_size_msf[1]);
1637 data.what.position.reladdr.msf.frame = bcd2bin(q.trk_size_msf[2]);
1638 data.what.position.absaddr.msf.unused = 0;
1639 data.what.position.absaddr.msf.minute = bcd2bin(q.hd_pos_msf[0]);
1640 data.what.position.absaddr.msf.second = bcd2bin(q.hd_pos_msf[1]);
1641 data.what.position.absaddr.msf.frame = bcd2bin(q.hd_pos_msf[2]);
1644 lba = msf2hsg(q.trk_size_msf, 1);
1646 * Pre-gap has index number of 0, and decreasing MSF
1647 * address. Must be converted to negative LBA, per
1650 if (data.what.position.index_number == 0)
1652 data.what.position.reladdr.lba = htonl(lba);
1653 data.what.position.absaddr.lba = htonl(msf2hsg(q.hd_pos_msf, 0));
1659 return copyout(&data, sc->data, min(sizeof(struct cd_sub_channel_info), sc->data_len));
1663 mcd_playmsf(int unit, struct ioc_play_msf *p)
1665 struct mcd_data *cd = mcd_data + unit;
1666 struct mcd_read2 pb;
1669 printf("mcd%d: playmsf: from %d:%d.%d to %d:%d.%d\n",
1671 p->start_m, p->start_s, p->start_f,
1672 p->end_m, p->end_s, p->end_f);
1674 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
1675 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) ||
1676 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f) >
1677 M_msf(cd->volinfo.vol_msf) * 60 * 75 +
1678 S_msf(cd->volinfo.vol_msf) * 75 +
1679 F_msf(cd->volinfo.vol_msf))
1682 pb.start_msf[0] = bin2bcd(p->start_m);
1683 pb.start_msf[1] = bin2bcd(p->start_s);
1684 pb.start_msf[2] = bin2bcd(p->start_f);
1685 pb.end_msf[0] = bin2bcd(p->end_m);
1686 pb.end_msf[1] = bin2bcd(p->end_s);
1687 pb.end_msf[2] = bin2bcd(p->end_f);
1689 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1692 return mcd_play(unit, &pb);
1696 mcd_playtracks(int unit, struct ioc_play_track *pt)
1698 struct mcd_data *cd = mcd_data + unit;
1699 struct mcd_read2 pb;
1700 int a = pt->start_track;
1701 int z = pt->end_track;
1704 if ((rc = mcd_read_toc(unit)) != 0)
1708 printf("mcd%d: playtracks from %d:%d to %d:%d\n", unit,
1709 a, pt->start_index, z, pt->end_index);
1711 if ( a < bcd2bin(cd->volinfo.trk_low)
1712 || a > bcd2bin(cd->volinfo.trk_high)
1714 || z < bcd2bin(cd->volinfo.trk_low)
1715 || z > bcd2bin(cd->volinfo.trk_high))
1718 for (i = 0; i < 3; i++) {
1719 pb.start_msf[i] = cd->toc[a].hd_pos_msf[i];
1720 pb.end_msf[i] = cd->toc[z+1].hd_pos_msf[i];
1723 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1726 return mcd_play(unit, &pb);
1730 mcd_playblocks(int unit, struct ioc_play_blocks *p)
1732 struct mcd_data *cd = mcd_data + unit;
1733 struct mcd_read2 pb;
1736 printf("mcd%d: playblocks: blkno %d length %d\n",
1737 unit, p->blk, p->len);
1739 if (p->blk > cd->disksize || p->len > cd->disksize ||
1740 p->blk < 0 || p->len < 0 ||
1741 (p->blk + p->len) > cd->disksize)
1744 hsg2msf(p->blk, pb.start_msf);
1745 hsg2msf(p->blk + p->len, pb.end_msf);
1747 if (mcd_setmode(unit, MCD_MD_COOKED) != 0)
1750 return mcd_play(unit, &pb);
1754 mcd_play(int unit, struct mcd_read2 *pb)
1756 struct mcd_data *cd = mcd_data + unit;
1757 int com_port = cd->iobase + mcd_command;
1758 int retry, st = -1, status;
1761 for(retry=0; retry<MCD_RETRYS; retry++) {
1764 outb(com_port, MCD_CMDSINGLESPEEDREAD);
1765 outb(com_port, pb->start_msf[0]);
1766 outb(com_port, pb->start_msf[1]);
1767 outb(com_port, pb->start_msf[2]);
1768 outb(com_port, pb->end_msf[0]);
1769 outb(com_port, pb->end_msf[1]);
1770 outb(com_port, pb->end_msf[2]);
1773 status=mcd_getstat(unit, 0);
1776 else if (status != -2)
1782 printf("mcd%d: media changed\n", unit);
1786 printf("mcd%d: mcd_play retry=%d, status=0x%02x\n", unit, retry, status);
1789 cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
1796 struct mcd_data *cd = mcd_data + unit;
1797 struct mcd_qchninfo q;
1800 /* Verify current status */
1801 if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS &&
1802 cd->audio_status != CD_AS_PLAY_PAUSED) {
1804 printf("mcd%d: pause attempted when not playing, audio status %d\n",
1805 unit, cd->audio_status);
1809 /* Get the current position */
1810 if (mcd_getqchan(unit, &q) < 0)
1813 /* Copy it into lastpb */
1814 cd->lastpb.start_msf[0] = q.hd_pos_msf[0];
1815 cd->lastpb.start_msf[1] = q.hd_pos_msf[1];
1816 cd->lastpb.start_msf[2] = q.hd_pos_msf[2];
1819 if ((rc=mcd_stop(unit)) != 0)
1822 /* Set the proper status and exit */
1823 cd->audio_status = CD_AS_PLAY_PAUSED;
1828 mcd_resume(int unit)
1830 struct mcd_data *cd = mcd_data + unit;
1832 if (cd->audio_status != CD_AS_PLAY_PAUSED)
1834 return mcd_play(unit, &cd->lastpb);