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