mcd.c revision 1.8 1 /*
2 * Copyright (c) 1993, 1994 Charles Hannum.
3 * Copyright 1993 by Holger Veit (data part)
4 * Copyright 1993 by Brian Moore (audio part)
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This software was developed by Holger Veit and Brian Moore
18 * for use with "386BSD" and similar operating systems.
19 * "Similar operating systems" includes mainly non-profit oriented
20 * systems for research and education, including but not restricted to
21 * "NetBSD", "FreeBSD", "Mach" (by CMU).
22 * 4. Neither the name of the developer(s) nor the name "386BSD"
23 * may be used to endorse or promote products derived from this
24 * software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY
27 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER(S) BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
32 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
33 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *
38 * $Id: mcd.c,v 1.8 1994/03/29 04:36:11 mycroft Exp $
39 */
40
41 /*static char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";*/
42
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/proc.h>
48 #include <sys/conf.h>
49 #include <sys/file.h>
50 #include <sys/buf.h>
51 #include <sys/stat.h>
52 #include <sys/uio.h>
53 #include <sys/ioctl.h>
54 #include <sys/cdio.h>
55 #include <sys/errno.h>
56 #include <sys/dkbad.h>
57 #include <sys/disklabel.h>
58 #include <sys/device.h>
59
60 #include <machine/cpu.h>
61 #include <machine/pio.h>
62
63 #include <i386/isa/isa.h>
64 #include <i386/isa/isavar.h>
65 #include <i386/isa/mcdreg.h>
66
67 #ifndef MCDDEBUG
68 #define MCD_TRACE(fmt,a,b,c,d)
69 #else
70 #define MCD_TRACE(fmt,a,b,c,d) {if (sc->debug) {printf("%s: st=%02x: ", sc->sc_dev.dv_xname, sc->status); printf(fmt,a,b,c,d);}}
71 #endif
72
73 #define MCDPART(dev) (((minor(dev)) & 0x07) )
74 #define MCDUNIT(dev) (((minor(dev)) & 0x78) >> 3)
75 #define MCDPHYS(dev) (((minor(dev)) & 0x80) >> 7)
76 #define RAW_PART 3
77
78 /* flags */
79 #define MCDOPEN 0x0001 /* device opened */
80 #define MCDVALID 0x0002 /* parameters loaded */
81 #define MCDWAIT 0x0004 /* waiting for something */
82 #define MCDLABEL 0x0008 /* label is read */
83 #define MCDREADRAW 0x0010 /* read raw mode (2352 bytes) */
84 #define MCDVOLINFO 0x0020 /* already read volinfo */
85 #define MCDTOC 0x0040 /* already read toc */
86 #define MCDMBXBSY 0x0080 /* local mbx is busy */
87
88 /* status */
89 #define MCDAUDIOBSY MCD_ST_AUDIOBSY /* playing audio */
90 #define MCDDSKCHNG MCD_ST_DSKCHNG /* sensed change of disk */
91 #define MCDDSKIN MCD_ST_DSKIN /* sensed disk in drive */
92 #define MCDDOOROPEN MCD_ST_DOOROPEN /* sensed door open */
93
94 /* toc */
95 #define MCD_MAXTOCS 104 /* from the Linux driver */
96 #define MCD_LASTPLUS1 170 /* special toc entry */
97
98 struct mcd_mbx {
99 short unit;
100 u_short iobase;
101 short retry;
102 short nblk;
103 int sz;
104 u_long skip;
105 struct buf *bp;
106 int p_offset;
107 short count;
108 };
109
110 struct mcd_softc {
111 struct device sc_dev;
112
113 u_short iobase;
114 short config;
115 short flags;
116 short status;
117 int blksize;
118 u_long disksize;
119 struct disklabel dlabel;
120 int partflags[MAXPARTITIONS];
121 int openflags;
122 struct mcd_volinfo volinfo;
123 struct mcd_qchninfo toc[MCD_MAXTOCS];
124 short audio_status;
125 struct mcd_read2 lastpb;
126 short debug;
127 struct buf head; /* head of buf queue */
128 struct mcd_mbx mbx;
129 };
130
131 /* prototypes */
132 int mcdopen __P((dev_t));
133 int mcdclose __P((dev_t));
134 int mcd_start __P((struct mcd_softc *));
135 int mcdioctl __P((dev_t, int, caddr_t, int, struct proc *));
136 int mcd_getdisklabel __P((struct mcd_softc *));
137 int mcdsize __P((dev_t));
138 void mcd_configure __P((struct mcd_softc *));
139 int mcd_waitrdy __P((u_short, int));
140 int mcd_getreply __P((struct mcd_softc *, int));
141 int mcd_getstat __P((struct mcd_softc *, int));
142 void mcd_setflags __P((struct mcd_softc *));
143 int mcd_get __P((struct mcd_softc *, char *, int));
144 int mcd_send __P((struct mcd_softc *, int, int));
145 int bcd2bin __P((bcd_t));
146 bcd_t bin2bcd __P((int));
147 void hsg2msf __P((int, bcd_t *));
148 int msf2hsg __P((bcd_t *));
149 int mcd_volinfo __P((struct mcd_softc *));
150 int mcdintr __P((int));
151 void mcd_doread __P((int, struct mcd_mbx *));
152 int mcd_setmode __P((struct mcd_softc *, int));
153 int mcd_toc_header __P((struct mcd_softc *, struct ioc_toc_header *));
154 int mcd_read_toc __P((struct mcd_softc *));
155 int mcd_toc_entry __P((struct mcd_softc *, struct ioc_read_toc_entry *));
156 int mcd_stop __P((struct mcd_softc *));
157 int mcd_getqchan __P((struct mcd_softc *, struct mcd_qchninfo *));
158 int mcd_subchan __P((struct mcd_softc *, struct ioc_read_subchannel *));
159 int mcd_playtracks __P((struct mcd_softc *, struct ioc_play_track *));
160 int mcd_play __P((struct mcd_softc *, struct mcd_read2 *));
161 int mcd_pause __P((struct mcd_softc *));
162 int mcd_resume __P((struct mcd_softc *));
163
164 int mcdprobe();
165 void mcdattach();
166
167 struct cfdriver mcdcd = {
168 NULL, "mcd", mcdprobe, mcdattach, DV_DISK, sizeof(struct mcd_softc)
169 };
170
171 #define mcd_put(port,byte) outb(port,byte)
172
173 #define MCD_RETRIES 5
174 #define MCD_RDRETRIES 8
175
176 #define MCDBLK 2048 /* for cooked mode */
177 #define MCDRBLK 2352 /* for raw mode */
178
179 /* several delays */
180 #define RDELAY_WAITSTAT 300
181 #define RDELAY_WAITMODE 300
182 #define RDELAY_WAITREAD 800
183
184 #define DELAY_STATUS 10000l /* 10000 * 1us */
185 #define DELAY_GETREPLY 200000l /* 200000 * 2us */
186 #define DELAY_SEEKREAD 20000l /* 20000 * 1us */
187
188 /* reader state machine */
189 #define MCD_S_BEGIN 0
190 #define MCD_S_BEGIN1 1
191 #define MCD_S_WAITSTAT 2
192 #define MCD_S_WAITMODE 3
193 #define MCD_S_WAITREAD 4
194
195 void
196 mcdattach(parent, self, aux)
197 struct device *parent, *self;
198 void *aux;
199 {
200 struct mcd_softc *sc = (void *)self;
201 struct isa_attach_args *ia = aux;
202
203 #ifdef notyet
204 /* Wire controller for interrupts and DMA. */
205 mcd_configure(sc);
206 #endif
207
208 sc->flags = 0;
209 }
210
211 int
212 mcdopen(dev)
213 dev_t dev;
214 {
215 int unit, part, phys;
216 struct mcd_softc *sc;
217
218 unit = MCDUNIT(dev);
219 if (unit >= mcdcd.cd_ndevs)
220 return ENXIO;
221 sc = mcdcd.cd_devs[unit];
222 if (!sc)
223 return ENXIO;
224
225 part = MCDPART(dev);
226 phys = MCDPHYS(dev);
227
228 /* Invalidated in the meantime? Mark all open part's invalid. */
229 if (!(sc->flags & MCDVALID) && sc->openflags)
230 return ENXIO;
231
232 if (mcd_getstat(sc, 1) < 0)
233 return ENXIO;
234
235 /* XXX Get a default disklabel. */
236 mcd_getdisklabel(sc);
237
238 if (mcdsize(dev) < 0) {
239 printf("%s: failed to get disk size\n", sc->sc_dev.dv_xname);
240 return ENXIO;
241 } else
242 sc->flags |= MCDVALID;
243
244 MCD_TRACE("open: partition=%d disksize=%d blksize=%d\n", part,
245 sc->disksize, sc->blksize, 0);
246
247 if (part != RAW_PART &&
248 (part >= sc->dlabel.d_npartitions ||
249 sc->dlabel.d_partitions[part].p_fstype == FS_UNUSED))
250 return ENXIO;
251
252 sc->partflags[part] |= MCDOPEN;
253 sc->openflags |= (1 << part);
254 if (part == RAW_PART && phys != 0)
255 sc->partflags[part] |= MCDREADRAW;
256 return 0;
257 }
258
259 int
260 mcdclose(dev)
261 dev_t dev;
262 {
263 int unit, part;
264 struct mcd_softc *sc;
265
266 unit = MCDUNIT(dev);
267 part = MCDPART(dev);
268 sc = mcdcd.cd_devs[unit];
269
270 /* Get status. */
271 mcd_getstat(sc, 1);
272
273 /* Close channel. */
274 sc->partflags[part] &= ~(MCDOPEN | MCDREADRAW);
275 sc->openflags &= ~(1 << part);
276 MCD_TRACE("close: partition=%d\n", part, 0, 0, 0);
277
278 return 0;
279 }
280
281 void
282 mcdstrategy(bp)
283 struct buf *bp;
284 {
285 struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(bp->b_dev)];
286 struct buf *qp;
287 int s;
288
289 /* Test validity. */
290 MCD_TRACE("strategy: buf=0x%lx blkno=%ld bcount=%ld\n", bp,
291 bp->b_blkno, bp->b_bcount, 0);
292 if (bp->b_blkno < 0) {
293 printf("%s: strategy: blkno=%d bcount=%d\n",
294 sc->sc_dev.dv_xname, bp->b_blkno, bp->b_bcount);
295 bp->b_error = EINVAL;
296 bp->b_flags |= B_ERROR;
297 goto bad;
298 }
299
300 /* If device invalidated (e.g. media change, door open), error. */
301 if (!(sc->flags & MCDVALID)) {
302 MCD_TRACE("strategy: drive not valid\n", 0, 0, 0, 0);
303 bp->b_error = EIO;
304 goto bad;
305 }
306
307 /* Check for read only. */
308 if (!(bp->b_flags & B_READ)) {
309 bp->b_error = EROFS;
310 goto bad;
311 }
312
313 /* No data to read. */
314 if (bp->b_bcount == 0)
315 goto done;
316
317 /* For non raw access, check partition limits. */
318 if (MCDPART(bp->b_dev) != RAW_PART) {
319 if (!(sc->flags & MCDLABEL)) {
320 bp->b_error = EIO;
321 goto bad;
322 }
323 /* Adjust transfer if necessary. */
324 if (bounds_check_with_label(bp, &sc->dlabel, 1) <= 0)
325 goto done;
326 }
327
328 /* Queue it. */
329 qp = &sc->head;
330 s = splbio();
331 disksort(qp, bp);
332 splx(s);
333
334 /* Now check whether we can perform processing. */
335 mcd_start(sc);
336 return;
337
338 bad:
339 bp->b_flags |= B_ERROR;
340 done:
341 bp->b_resid = bp->b_bcount;
342 biodone(bp);
343 return;
344 }
345
346 int
347 mcd_start(sc)
348 struct mcd_softc *sc;
349 {
350 struct buf *bp, *qp = &sc->head;
351 struct partition *p;
352 int s = splbio();
353
354 if (sc->flags & MCDMBXBSY)
355 return;
356
357 if ((bp = qp->b_actf) != 0) {
358 /* Block found to process; dequeue. */
359 MCD_TRACE("start: found block bp=0x%x\n", bp, 0, 0, 0);
360 qp->b_actf = bp->b_actf;
361 splx(s);
362 } else {
363 /* Nothing to do; */
364 splx(s);
365 return;
366 }
367
368 /* Changed media? */
369 if (!(sc->flags & MCDVALID)) {
370 MCD_TRACE("start: drive not valid\n", 0, 0, 0, 0);
371 return;
372 }
373
374 p = &sc->dlabel.d_partitions[MCDPART(bp->b_dev)];
375
376 sc->flags |= MCDMBXBSY;
377 sc->mbx.unit = sc->sc_dev.dv_unit;
378 sc->mbx.iobase = sc->iobase;
379 sc->mbx.retry = MCD_RETRIES;
380 sc->mbx.bp = bp;
381 sc->mbx.p_offset = p->p_offset;
382
383 /* Calling the read routine. */
384 mcd_doread(MCD_S_BEGIN, &sc->mbx);
385 /* triggers mcd_start, when successful finished. */
386 }
387
388 int
389 mcdioctl(dev, cmd, addr, flags, p)
390 dev_t dev;
391 int cmd;
392 caddr_t addr;
393 int flags;
394 struct proc *p;
395 {
396 struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(dev)];
397
398 if (!(sc->flags & MCDVALID))
399 return EIO;
400 MCD_TRACE("ioctl: cmd=0x%x\n", cmd, 0, 0, 0);
401
402 switch (cmd) {
403 case DIOCSBAD:
404 return EINVAL;
405 case DIOCGDINFO:
406 case DIOCGPART:
407 case DIOCWDINFO:
408 case DIOCSDINFO:
409 case DIOCWLABEL:
410 return ENOTTY;
411 case CDIOCPLAYTRACKS:
412 return mcd_playtracks(sc, (struct ioc_play_track *) addr);
413 case CDIOCPLAYBLOCKS:
414 return mcd_play(sc, (struct mcd_read2 *) addr);
415 case CDIOCREADSUBCHANNEL:
416 return mcd_subchan(sc, (struct ioc_read_subchannel *) addr);
417 case CDIOREADTOCHEADER:
418 return mcd_toc_header(sc, (struct ioc_toc_header *) addr);
419 case CDIOREADTOCENTRYS:
420 return mcd_toc_entry(sc, (struct ioc_read_toc_entry *) addr);
421 case CDIOCSETPATCH:
422 case CDIOCGETVOL:
423 case CDIOCSETVOL:
424 case CDIOCSETMONO:
425 case CDIOCSETSTERIO:
426 case CDIOCSETMUTE:
427 case CDIOCSETLEFT:
428 case CDIOCSETRIGHT:
429 return EINVAL;
430 case CDIOCRESUME:
431 return mcd_resume(sc);
432 case CDIOCPAUSE:
433 return mcd_pause(sc);
434 case CDIOCSTART:
435 return EINVAL;
436 case CDIOCSTOP:
437 return mcd_stop(sc);
438 case CDIOCEJECT:
439 return EINVAL;
440 case CDIOCSETDEBUG:
441 sc->debug = 1;
442 return 0;
443 case CDIOCCLRDEBUG:
444 sc->debug = 0;
445 return 0;
446 case CDIOCRESET:
447 return EINVAL;
448 default:
449 return ENOTTY;
450 }
451 #ifdef DIAGNOSTIC
452 panic("mcdioctl: impossible");
453 #endif
454 }
455
456 /*
457 * This could have been taken from scsi/cd.c, but it is not clear
458 * whether the scsi cd driver is linked in.
459 */
460 int
461 mcd_getdisklabel(sc)
462 struct mcd_softc *sc;
463 {
464
465 if (sc->flags & MCDLABEL)
466 return -1;
467
468 bzero(&sc->dlabel, sizeof(struct disklabel));
469 strncpy(sc->dlabel.d_typename, "Mitsumi CD ROM ", 16);
470 strncpy(sc->dlabel.d_packname, "unknown ", 16);
471 sc->dlabel.d_secsize = sc->blksize;
472 sc->dlabel.d_nsectors = 100;
473 sc->dlabel.d_ntracks = 1;
474 sc->dlabel.d_ncylinders = (sc->disksize /100) + 1;
475 sc->dlabel.d_secpercyl = 100;
476 sc->dlabel.d_secperunit = sc->disksize;
477 sc->dlabel.d_rpm = 300;
478 sc->dlabel.d_interleave = 1;
479 sc->dlabel.d_flags = D_REMOVABLE;
480 sc->dlabel.d_npartitions= 1;
481 sc->dlabel.d_partitions[0].p_offset = 0;
482 sc->dlabel.d_partitions[0].p_size = sc->disksize;
483 sc->dlabel.d_partitions[0].p_fstype = 9;
484 sc->dlabel.d_magic = DISKMAGIC;
485 sc->dlabel.d_magic2 = DISKMAGIC;
486 sc->dlabel.d_checksum = dkcksum(&sc->dlabel);
487
488 sc->flags |= MCDLABEL;
489 return 0;
490 }
491
492 int
493 mcdsize(dev)
494 dev_t dev;
495 {
496 int size;
497 struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(dev)];
498
499 if (mcd_volinfo(sc) >= 0) {
500 sc->blksize = MCDBLK;
501 size = msf2hsg(sc->volinfo.vol_msf);
502 sc->disksize = size * (MCDBLK / DEV_BSIZE);
503 return 0;
504 }
505 return -1;
506 }
507
508 /***************************************************************
509 * lower level of driver starts here
510 **************************************************************/
511
512 #ifdef notyet
513 static char irqs[] = {
514 0x00, 0x00, 0x10, 0x20, 0x00, 0x30, 0x00, 0x00,
515 0x00, 0x10, 0x40, 0x50, 0x00, 0x00, 0x00, 0x00
516 };
517
518 static char drqs[] = {
519 0x00, 0x01, 0x00, 0x03, 0x00, 0x05, 0x06, 0x07
520 };
521 #endif
522
523 void
524 mcd_configure(sc)
525 struct mcd_softc *sc;
526 {
527
528 outb(sc->iobase + mcd_config, sc->config);
529 }
530
531 int
532 mcdprobe(parent, self, aux)
533 struct device *parent, *self;
534 void *aux;
535 {
536 struct mcd_softc *sc = (void *)self;
537 struct isa_attach_args *ia = aux;
538 u_short iobase = ia->ia_iobase;
539 int i;
540 int st, check;
541
542 #ifdef notyet
543 /* Get irq/drq configuration word. */
544 sc->config = irqs[ia->ia_irq];
545 #endif
546 sc->iobase = iobase;
547
548 /* Send a reset. */
549 outb(iobase + mcd_reset, 0);
550 delay(300000);
551 /* Get any pending status and throw away. */
552 for (i = 10; i; i--)
553 inb(iobase + mcd_status);
554 delay(1000);
555
556 /* Send get status command. */
557 outb(iobase + mcd_command, MCD_CMDGETSTAT);
558 i = mcd_getreply(sc, DELAY_GETREPLY);
559
560 if (i < 0) {
561 #ifdef DEBUG
562 printf("Mitsumi drive NOT detected\n");
563 #endif
564 return 0;
565 }
566
567 /*
568 * The following code uses the 0xDC command, it returns a M from the
569 * second byte and a number in the third.
570 * (I hope you have the right drive for that, most drives don't do!)
571 * Whole code entirely rewriten by veit (at) gmd.de, the changes accessed
572 * the drive in an illegal way. Proper way is to use the timeout
573 * driven routines mcd_getreply etc. rather than arbitrary delays.
574 */
575
576 delay(2000);
577 outb(iobase + mcd_command, MCD_CMDCONTINFO);
578 i = mcd_getreply(sc, DELAY_GETREPLY);
579
580 if (i < 0) {
581 #ifdef DEBUG
582 printf("Mitsumi drive error\n");
583 #endif
584 return 0;
585 }
586 st = mcd_getreply(sc, DELAY_GETREPLY);
587 if (st < 0)
588 return 0;
589 check = mcd_getreply(sc, DELAY_GETREPLY);
590 if (check < 0)
591 return 0;
592 /* Flush junk. */
593 (void) mcd_getreply(sc, DELAY_GETREPLY);
594
595 /*
596 * The following is code which is not guaranteed to work for all
597 * drives, because the meaning of the expected 'M' is not clear
598 * (M_itsumi is an obvious assumption, but I don't trust that).
599 * Also, the original hack had a bogus condition that always
600 * returned true.
601 */
602 #ifdef notdef
603 if (check != 'M') {
604 #ifdef DEBUG
605 printf("Mitsumi drive NOT detected\n");
606 #endif
607 return 0;
608 }
609 #endif
610
611 #ifdef DEBUG
612 printf("Mitsumi drive detected\n");
613 #endif
614 ia->ia_iosize = 4;
615 ia->ia_msize = 0;
616 return 1;
617 }
618
619 int
620 mcd_waitrdy(iobase, dly)
621 u_short iobase;
622 int dly;
623 {
624 int i;
625
626 /* Wait until xfer port senses data ready. */
627 for (i = dly; i; i--) {
628 if ((inb(iobase + mcd_xfer) & MCD_ST_BUSY) == 0)
629 return 0;
630 delay(1);
631 }
632 return -1;
633 }
634
635 int
636 mcd_getreply(sc, dly)
637 struct mcd_softc *sc;
638 int dly;
639 {
640 u_short iobase = sc->iobase;
641
642 /* Wait data to become ready. */
643 if (mcd_waitrdy(iobase, dly) < 0) {
644 printf("%s: timeout in getreply\n", sc->sc_dev.dv_xname);
645 return -1;
646 }
647
648 /* Get the data. */
649 return inb(iobase + mcd_status);
650 }
651
652 int
653 mcd_getstat(sc, sflg)
654 struct mcd_softc *sc;
655 int sflg;
656 {
657 int i;
658 u_short iobase = sc->iobase;
659
660 /* Get the status. */
661 if (sflg)
662 outb(iobase + mcd_command, MCD_CMDGETSTAT);
663 i = mcd_getreply(sc, DELAY_GETREPLY);
664 if (i < 0) {
665 printf("%s: timeout in getstat\n", sc->sc_dev.dv_xname);
666 return -1;
667 }
668
669 sc->status = i;
670
671 mcd_setflags(sc);
672 return sc->status;
673 }
674
675 void
676 mcd_setflags(sc)
677 struct mcd_softc *sc;
678 {
679
680 /* Check flags. */
681 if (sc->status & (MCDDSKCHNG | MCDDOOROPEN)) {
682 MCD_TRACE("getstat: sensed DSKCHNG or DOOROPEN\n", 0, 0, 0, 0);
683 sc->flags &= ~MCDVALID;
684 }
685
686 if (sc->status & MCDAUDIOBSY)
687 sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
688 else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS)
689 sc->audio_status = CD_AS_PLAY_COMPLETED;
690 }
691
692 int
693 mcd_get(sc, buf, nmax)
694 struct mcd_softc *sc;
695 char *buf;
696 int nmax;
697 {
698 int i, k;
699
700 for (i = 0; i < nmax; i++) {
701 /* Wait for data. */
702 if ((k = mcd_getreply(sc, DELAY_GETREPLY)) < 0) {
703 printf("%s: timeout in get\n", sc->sc_dev.dv_xname);
704 return -1;
705 }
706 buf[i] = k;
707 }
708 return i;
709 }
710
711 int
712 mcd_send(sc, cmd, nretries)
713 struct mcd_softc *sc;
714 int cmd, nretries;
715 {
716 int i, k;
717 u_short iobase = sc->iobase;
718
719 MCD_TRACE("send: cmd=0x%x\n", cmd, 0, 0, 0);
720
721 for (i = nretries; i; i--) {
722 outb(iobase + mcd_command, cmd);
723 if ((k = mcd_getstat(sc, 0)) != -1)
724 break;
725 }
726 if (!i) {
727 printf("%s: send: retry count exceeded\n", sc->sc_dev.dv_xname);
728 return -1;
729 }
730
731 MCD_TRACE("send: status=0x%x\n", k, 0, 0, 0);
732
733 return 0;
734 }
735
736 int
737 bcd2bin(b)
738 bcd_t b;
739 {
740
741 return (b >> 4) * 10 + (b & 15);
742 }
743
744 bcd_t
745 bin2bcd(b)
746 int b;
747 {
748
749 return ((b / 10) << 4) | (b % 10);
750 }
751
752 void
753 hsg2msf(hsg, msf)
754 int hsg;
755 bcd_t *msf;
756 {
757
758 hsg += 150;
759 M_msf(msf) = bin2bcd(hsg / 4500);
760 hsg %= 4500;
761 S_msf(msf) = bin2bcd(hsg / 75);
762 F_msf(msf) = bin2bcd(hsg % 75);
763 }
764
765 int
766 msf2hsg(msf)
767 bcd_t *msf;
768 {
769
770 return (bcd2bin(M_msf(msf)) * 60 +
771 bcd2bin(S_msf(msf))) * 75 +
772 bcd2bin(F_msf(msf)) - 150;
773 }
774
775 int
776 mcd_volinfo(sc)
777 struct mcd_softc *sc;
778 {
779
780 MCD_TRACE("volinfo: enter\n", 0, 0, 0, 0);
781
782 /* Get the status, in case the disc has been changed. */
783 if (mcd_getstat(sc, 1) < 0)
784 return EIO;
785
786 /* Just return if we already have it. */
787 if (sc->flags & MCDVOLINFO)
788 return 0;
789
790 /* Send volume info command. */
791 if (mcd_send(sc, MCD_CMDGETVOLINFO, MCD_RETRIES) < 0)
792 return -1;
793
794 /* Get the data. */
795 if (mcd_get(sc, (char*) &sc->volinfo, sizeof(struct mcd_volinfo)) < 0) {
796 printf("%s: volinfo: error reading data\n",
797 sc->sc_dev.dv_xname);
798 return -1;
799 }
800
801 if (sc->volinfo.trk_low != 0 || sc->volinfo.trk_high != 0) {
802 /* Volinfo is OK. */
803 sc->flags |= MCDVOLINFO;
804 return 0;
805 }
806
807 return -1;
808 }
809
810 int
811 mcdintr(unit)
812 int unit;
813 {
814 struct mcd_softc *sc = mcdcd.cd_devs[unit];
815 u_short iobase = sc->iobase;
816
817 MCD_TRACE("stray interrupt xfer=0x%x\n", inb(iobase + mcd_xfer),
818 0, 0, 0);
819
820 /* Just read out status and ignore the rest. */
821 if (inb(iobase + mcd_xfer) != 0xff)
822 (void) inb(iobase + mcd_status);
823 }
824
825 /*
826 * State machine to process read requests.
827 * Initialize with MCD_S_BEGIN: calculate sizes, and read status
828 * MCD_S_WAITSTAT: wait for status reply, set mode
829 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
830 * MCD_S_WAITREAD: wait for read ready, read data.
831 */
832 struct mcd_mbx *mbxsave;
833
834 void
835 mcd_doread(state, mbxin)
836 int state;
837 struct mcd_mbx *mbxin;
838 {
839 struct mcd_mbx *mbx = (state != MCD_S_BEGIN) ? mbxsave : mbxin;
840 struct mcd_softc *sc = mcdcd.cd_devs[mbx->unit];
841 u_short iobase = mbx->iobase;
842 struct buf *bp = mbx->bp;
843
844 int rm, i, k;
845 struct mcd_read2 rbuf;
846 int blkno;
847 caddr_t addr;
848
849 loop:
850 switch (state) {
851 case MCD_S_BEGIN:
852 mbx = mbxsave = mbxin;
853
854 case MCD_S_BEGIN1:
855 /* Get status. */
856 outb(iobase + mcd_command, MCD_CMDGETSTAT);
857 mbx->count = RDELAY_WAITSTAT;
858 timeout((timeout_t) mcd_doread, (caddr_t) MCD_S_WAITSTAT,
859 hz/100);
860 return;
861
862 case MCD_S_WAITSTAT:
863 untimeout((timeout_t) mcd_doread, (caddr_t) MCD_S_WAITSTAT);
864 if (mbx->count-- >= 0) {
865 if (inb(iobase + mcd_xfer) & MCD_ST_BUSY) {
866 timeout((timeout_t) mcd_doread,
867 (caddr_t) MCD_S_WAITSTAT, hz/100);
868 return;
869 }
870 mcd_setflags(sc);
871 MCD_TRACE("doread: got WAITSTAT delay=%d\n",
872 RDELAY_WAITSTAT - mbx->count, 0, 0, 0);
873 /* Reject, if audio active. */
874 if (sc->status & MCDAUDIOBSY) {
875 printf("%s: audio is active\n",
876 sc->sc_dev.dv_xname);
877 goto readerr;
878 }
879
880 /* Check for raw/cooked mode. */
881 if (sc->flags & MCDREADRAW) {
882 rm = MCD_MD_RAW;
883 mbx->sz = MCDRBLK;
884 } else {
885 rm = MCD_MD_COOKED;
886 mbx->sz = sc->blksize;
887 }
888
889 mbx->count = RDELAY_WAITMODE;
890
891 mcd_put(iobase + mcd_command, MCD_CMDSETMODE);
892 mcd_put(iobase + mcd_command, rm);
893 timeout((timeout_t) mcd_doread,
894 (caddr_t) MCD_S_WAITMODE, hz/100);
895 return;
896 } else {
897 printf("%s: timeout getting status\n",
898 sc->sc_dev.dv_xname);
899 goto readerr;
900 }
901
902 case MCD_S_WAITMODE:
903 untimeout((timeout_t) mcd_doread, (caddr_t) MCD_S_WAITMODE);
904 if (mbx->count-- < 0) {
905 printf("%s: timeout setting mode\n",
906 sc->sc_dev.dv_xname);
907 goto readerr;
908 }
909 if (inb(iobase + mcd_xfer) & MCD_ST_BUSY) {
910 timeout((timeout_t) mcd_doread,
911 (caddr_t) MCD_S_WAITMODE, hz/100);
912 return;
913 }
914 mcd_setflags(sc);
915 MCD_TRACE("doread: got WAITMODE delay=%d\n",
916 RDELAY_WAITMODE - mbx->count, 0, 0, 0);
917 /* For first block. */
918 mbx->nblk = (bp->b_bcount + (mbx->sz - 1)) / mbx->sz;
919 mbx->skip = 0;
920
921 nextblock:
922 blkno = (bp->b_blkno / (mbx->sz / DEV_BSIZE)) + mbx->p_offset +
923 (mbx->skip / mbx->sz);
924
925 MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", blkno, bp, 0,
926 0);
927
928 /* Build parameter block. */
929 hsg2msf(blkno, rbuf.start_msf);
930
931 /* Send the read command. */
932 mcd_put(iobase + mcd_command, MCD_CMDREAD2);
933 mcd_put(iobase + mcd_command, rbuf.start_msf[0]);
934 mcd_put(iobase + mcd_command, rbuf.start_msf[1]);
935 mcd_put(iobase + mcd_command, rbuf.start_msf[2]);
936 mcd_put(iobase + mcd_command, 0);
937 mcd_put(iobase + mcd_command, 0);
938 mcd_put(iobase + mcd_command, 1);
939 mbx->count = RDELAY_WAITREAD;
940 timeout((timeout_t) mcd_doread, (caddr_t) MCD_S_WAITREAD,
941 hz/100);
942 return;
943
944 case MCD_S_WAITREAD:
945 untimeout((timeout_t) mcd_doread, (caddr_t) MCD_S_WAITREAD);
946 if (mbx->count-- > 0) {
947 k = inb(iobase + mcd_xfer);
948 if ((k & 2) == 0) { /* XXX MCD_ST_AUDIOBSY? */
949 MCD_TRACE("doread: got data delay=%d\n",
950 RDELAY_WAITREAD - mbx->count, 0, 0, 0);
951 /* Data is ready. */
952 addr = bp->b_un.b_addr + mbx->skip;
953 outb(iobase + mcd_ctl2, 0x04); /* XXX */
954 for (i = 0; i < mbx->sz; i++)
955 *addr++ = inb(iobase + mcd_rdata);
956 outb(iobase + mcd_ctl2, 0x0c); /* XXX */
957
958 if (--mbx->nblk > 0) {
959 mbx->skip += mbx->sz;
960 goto nextblock;
961 }
962
963 /* Return buffer. */
964 bp->b_resid = 0;
965 biodone(bp);
966
967 sc->flags &= ~MCDMBXBSY;
968 mcd_start(sc);
969 return;
970 }
971 if ((k & MCD_ST_BUSY) == 0)
972 mcd_getstat(sc, 0);
973 timeout((timeout_t) mcd_doread,
974 (caddr_t) MCD_S_WAITREAD, hz/100);
975 return;
976 } else {
977 printf("%s: timeout reading data\n",
978 sc->sc_dev.dv_xname);
979 goto readerr;
980 }
981 }
982
983 readerr:
984 if (mbx->retry-- > 0) {
985 printf("%s: retrying\n", sc->sc_dev.dv_xname);
986 state = MCD_S_BEGIN1;
987 goto loop;
988 }
989
990 /* Invalidate the buffer. */
991 bp->b_flags |= B_ERROR;
992 bp->b_resid = bp->b_bcount;
993 biodone(bp);
994 mcd_start(sc);
995
996 #ifdef notyet
997 printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname);
998 outb(mbx->iobase + mcd_reset, MCD_CMDRESET);
999 delay(300000);
1000 (void)mcd_getstat(sc, 1);
1001 (void)mcd_getstat(sc, 1);
1002 /*sc->status &= ~MCDDSKCHNG; */
1003 sc->debug = 1; /* preventive set debug mode */
1004 #endif
1005 }
1006
1007 int
1008 mcd_setmode(sc, mode)
1009 struct mcd_softc *sc;
1010 int mode;
1011 {
1012 u_short iobase = sc->iobase;
1013 int retry;
1014
1015 printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode);
1016 for (retry = MCD_RETRIES; retry; retry--) {
1017 outb(iobase + mcd_command, MCD_CMDSETMODE);
1018 outb(iobase + mcd_command, mode);
1019 if (mcd_getstat(sc, 0) != -1)
1020 return 0;
1021 }
1022
1023 return -1;
1024 }
1025
1026 int
1027 mcd_toc_header(sc, th)
1028 struct mcd_softc *sc;
1029 struct ioc_toc_header *th;
1030 {
1031
1032 if (mcd_volinfo(sc) < 0)
1033 return ENXIO;
1034
1035 th->len = msf2hsg(sc->volinfo.vol_msf);
1036 th->starting_track = bcd2bin(sc->volinfo.trk_low);
1037 th->ending_track = bcd2bin(sc->volinfo.trk_high);
1038
1039 return 0;
1040 }
1041
1042 int
1043 mcd_read_toc(sc)
1044 struct mcd_softc *sc;
1045 {
1046 struct ioc_toc_header th;
1047 struct mcd_qchninfo q;
1048 int rc, trk, idx, retry;
1049
1050 /* Only read TOC if needed. */
1051 if (sc->flags & MCDTOC)
1052 return 0;
1053
1054 if (sc->debug)
1055 printf("%s: read_toc: reading toc header\n",
1056 sc->sc_dev.dv_xname);
1057 if (mcd_toc_header(sc, &th) != 0)
1058 return ENXIO;
1059
1060 if (sc->debug)
1061 printf("%s: read_toc: stopping play\n", sc->sc_dev.dv_xname);
1062 if ((rc = mcd_stop(sc)) != 0)
1063 return rc;
1064
1065 /* Try setting the mode twice. */
1066 if (mcd_setmode(sc, MCD_MD_TOC) != 0)
1067 return EIO;
1068 if (mcd_setmode(sc, MCD_MD_TOC) != 0)
1069 return EIO;
1070
1071 if (sc->debug)
1072 printf("%s: read_toc: reading qchannel info\n",
1073 sc->sc_dev.dv_xname);
1074 for (trk = th.starting_track; trk <= th.ending_track; trk++)
1075 sc->toc[trk].idx_no = 0;
1076 trk = th.ending_track - th.starting_track + 1;
1077 for (retry = 300; retry && trk > 0; retry--) {
1078 if (mcd_getqchan(sc, &q) < 0)
1079 break;
1080 idx = bcd2bin(q.idx_no);
1081 if (idx > 0 && idx < MCD_MAXTOCS && q.trk_no == 0 &&
1082 sc->toc[idx].idx_no == 0) {
1083 sc->toc[idx] = q;
1084 trk--;
1085 }
1086 }
1087
1088 if (mcd_setmode(sc, MCD_MD_COOKED) != 0)
1089 return EIO;
1090
1091 if (trk != 0)
1092 return ENXIO;
1093
1094 /* Add a fake last+1. */
1095 idx = th.ending_track + 1;
1096 sc->toc[idx].ctrl_adr = sc->toc[idx-1].ctrl_adr;
1097 sc->toc[idx].trk_no = 0;
1098 sc->toc[idx].idx_no = 0xaa;
1099 sc->toc[idx].hd_pos_msf[0] = sc->volinfo.vol_msf[0];
1100 sc->toc[idx].hd_pos_msf[1] = sc->volinfo.vol_msf[1];
1101 sc->toc[idx].hd_pos_msf[2] = sc->volinfo.vol_msf[2];
1102
1103 sc->flags |= MCDTOC;
1104
1105 return 0;
1106 }
1107
1108 int
1109 mcd_toc_entry(sc, te)
1110 struct mcd_softc *sc;
1111 struct ioc_read_toc_entry *te;
1112 {
1113 struct ret_toc {
1114 struct ioc_toc_header th;
1115 struct cd_toc_entry rt;
1116 } ret_toc;
1117 struct ioc_toc_header th;
1118 int rc, i;
1119
1120 /* Make sure we have a valid TOC. */
1121 if ((rc = mcd_read_toc(sc)) != 0)
1122 return rc;
1123
1124 /* Find the TOC to copy. */
1125 i = te->starting_track;
1126 if (i == MCD_LASTPLUS1)
1127 i = bcd2bin(sc->volinfo.trk_high) + 1;
1128
1129 /* Verify starting track. */
1130 if (i < bcd2bin(sc->volinfo.trk_low) ||
1131 i > bcd2bin(sc->volinfo.trk_high) + 1)
1132 return EINVAL;
1133
1134 /* Do we have room? */
1135 if (te->data_len < sizeof(struct ioc_toc_header) +
1136 sizeof(struct cd_toc_entry))
1137 return EINVAL;
1138
1139 /* Copy the TOC header. */
1140 if (mcd_toc_header(sc, &th) < 0)
1141 return EIO;
1142 ret_toc.th = th;
1143
1144 /* Copy the TOC data. */
1145 ret_toc.rt.control = sc->toc[i].ctrl_adr;
1146 ret_toc.rt.addr_type = te->address_format;
1147 ret_toc.rt.track = i;
1148 if (te->address_format == CD_MSF_FORMAT) {
1149 ret_toc.rt.addr[1] = sc->toc[i].hd_pos_msf[0];
1150 ret_toc.rt.addr[2] = sc->toc[i].hd_pos_msf[1];
1151 ret_toc.rt.addr[3] = sc->toc[i].hd_pos_msf[2];
1152 }
1153
1154 /* Copy the data back. */
1155 copyout(&ret_toc, te->data,
1156 sizeof(struct cd_toc_entry) + sizeof(struct ioc_toc_header));
1157
1158 return 0;
1159 }
1160
1161 int
1162 mcd_stop(sc)
1163 struct mcd_softc *sc;
1164 {
1165
1166 if (mcd_send(sc, MCD_CMDSTOPAUDIO, MCD_RETRIES) < 0)
1167 return ENXIO;
1168 sc->audio_status = CD_AS_PLAY_COMPLETED;
1169 return 0;
1170 }
1171
1172 int
1173 mcd_getqchan(sc, q)
1174 struct mcd_softc *sc;
1175 struct mcd_qchninfo *q;
1176 {
1177
1178 if (mcd_send(sc, MCD_CMDGETQCHN, MCD_RETRIES) < 0)
1179 return -1;
1180 if (mcd_get(sc, (char *) q, sizeof(struct mcd_qchninfo)) < 0)
1181 return -1;
1182 if (sc->debug)
1183 printf("%s: getqchan: ctl=%d t=%d i=%d ttm=%d:%d.%d dtm=%d:%d.%d\n",
1184 sc->sc_dev.dv_xname, q->ctrl_adr, q->trk_no, q->idx_no,
1185 q->trk_size_msf[0], q->trk_size_msf[1], q->trk_size_msf[2],
1186 q->trk_size_msf[0], q->trk_size_msf[1], q->trk_size_msf[2]);
1187 return 0;
1188 }
1189
1190 int
1191 mcd_subchan(sc, ch)
1192 struct mcd_softc *sc;
1193 struct ioc_read_subchannel *ch;
1194 {
1195 struct mcd_qchninfo q;
1196 struct cd_sub_channel_info data;
1197
1198 if (sc->debug)
1199 printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname,
1200 ch->address_format, ch->data_format);
1201
1202 if (ch->address_format != CD_MSF_FORMAT)
1203 return EIO;
1204 if (ch->data_format != CD_CURRENT_POSITION)
1205 return EIO;
1206 if (mcd_getqchan(sc, &q) < 0)
1207 return EIO;
1208
1209 data.header.audio_status = sc->audio_status;
1210 data.what.position.data_format = CD_MSF_FORMAT;
1211 data.what.position.track_number = bcd2bin(q.trk_no);
1212
1213 if (copyout(&data, ch->data, sizeof(struct cd_sub_channel_info)) != 0)
1214 return EFAULT;
1215 return 0;
1216 }
1217
1218 int
1219 mcd_playtracks(sc, pt)
1220 struct mcd_softc *sc;
1221 struct ioc_play_track *pt;
1222 {
1223 struct mcd_read2 pb;
1224 int a = pt->start_track;
1225 int z = pt->end_track;
1226 int rc;
1227
1228 if ((rc = mcd_read_toc(sc)) != 0)
1229 return rc;
1230
1231 printf("%s: playtracks: from %d:%d to %d:%d\n", sc->sc_dev.dv_xname,
1232 a, pt->start_index, z, pt->end_index);
1233
1234 if (a < sc->volinfo.trk_low || a > sc->volinfo.trk_high || a > z ||
1235 z < sc->volinfo.trk_low || z > sc->volinfo.trk_high)
1236 return EINVAL;
1237
1238 pb.start_msf[0] = sc->toc[a].hd_pos_msf[0];
1239 pb.start_msf[1] = sc->toc[a].hd_pos_msf[1];
1240 pb.start_msf[2] = sc->toc[a].hd_pos_msf[2];
1241 pb.end_msf[0] = sc->toc[z+1].hd_pos_msf[0];
1242 pb.end_msf[1] = sc->toc[z+1].hd_pos_msf[1];
1243 pb.end_msf[2] = sc->toc[z+1].hd_pos_msf[2];
1244
1245 return mcd_play(sc, &pb);
1246 }
1247
1248 int
1249 mcd_play(sc, pb)
1250 struct mcd_softc *sc;
1251 struct mcd_read2 *pb;
1252 {
1253 u_short iobase = sc->iobase;
1254 int retry, st;
1255
1256 sc->lastpb = *pb;
1257 for (retry = MCD_RETRIES; retry; retry--) {
1258 outb(iobase + mcd_command, MCD_CMDREAD2);
1259 outb(iobase + mcd_command, pb->start_msf[0]);
1260 outb(iobase + mcd_command, pb->start_msf[1]);
1261 outb(iobase + mcd_command, pb->start_msf[2]);
1262 outb(iobase + mcd_command, pb->end_msf[0]);
1263 outb(iobase + mcd_command, pb->end_msf[1]);
1264 outb(iobase + mcd_command, pb->end_msf[2]);
1265 if ((st = mcd_getstat(sc, 0)) != -1)
1266 break;
1267 }
1268 if (sc->debug)
1269 printf("%s: play: retry=%d status=%d\n", sc->sc_dev.dv_xname,
1270 retry, st);
1271 if (!retry)
1272 return ENXIO;
1273
1274 sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
1275 return 0;
1276 }
1277
1278 int
1279 mcd_pause(sc)
1280 struct mcd_softc *sc;
1281 {
1282 struct mcd_qchninfo q;
1283 int rc;
1284
1285 /* Verify current status. */
1286 if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS) {
1287 printf("%s: pause: attempted when not playing\n",
1288 sc->sc_dev.dv_xname);
1289 return EINVAL;
1290 }
1291
1292 /* Get the current position. */
1293 if (mcd_getqchan(sc, &q) < 0)
1294 return EIO;
1295
1296 /* Copy it into lastpb. */
1297 sc->lastpb.start_msf[0] = q.hd_pos_msf[0];
1298 sc->lastpb.start_msf[1] = q.hd_pos_msf[1];
1299 sc->lastpb.start_msf[2] = q.hd_pos_msf[2];
1300
1301 /* Stop playing. */
1302 if ((rc = mcd_stop(sc)) != 0)
1303 return rc;
1304
1305 /* Set the proper status and exit. */
1306 sc->audio_status = CD_AS_PLAY_PAUSED;
1307 return 0;
1308 }
1309
1310 int
1311 mcd_resume(sc)
1312 struct mcd_softc *sc;
1313 {
1314
1315 if (sc->audio_status != CD_AS_PLAY_PAUSED)
1316 return EINVAL;
1317 return mcd_play(sc, &sc->lastpb);
1318 }
1319