mcd.c revision 1.44 1 /* $NetBSD: mcd.c,v 1.44 1996/01/07 22:03:37 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1993, 1994, 1995 Charles M. Hannum. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Charles M. Hannum.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * Copyright 1993 by Holger Veit (data part)
21 * Copyright 1993 by Brian Moore (audio part)
22 * All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This software was developed by Holger Veit and Brian Moore
35 * for use with "386BSD" and similar operating systems.
36 * "Similar operating systems" includes mainly non-profit oriented
37 * systems for research and education, including but not restricted to
38 * "NetBSD", "FreeBSD", "Mach" (by CMU).
39 * 4. Neither the name of the developer(s) nor the name "386BSD"
40 * may be used to endorse or promote products derived from this
41 * software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY
44 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER(S) BE
47 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
48 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
49 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
50 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
51 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
52 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
53 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55
56 /*static char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";*/
57
58 #include <sys/types.h>
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/proc.h>
63 #include <sys/conf.h>
64 #include <sys/file.h>
65 #include <sys/buf.h>
66 #include <sys/stat.h>
67 #include <sys/uio.h>
68 #include <sys/ioctl.h>
69 #include <sys/cdio.h>
70 #include <sys/errno.h>
71 #include <sys/disklabel.h>
72 #include <sys/device.h>
73 #include <sys/disk.h>
74
75 #include <machine/cpu.h>
76 #include <machine/pio.h>
77
78 #include <dev/isa/isavar.h>
79 #include <dev/isa/mcdreg.h>
80
81 #ifndef MCDDEBUG
82 #define MCD_TRACE(fmt,a,b,c,d)
83 #else
84 #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);}}
85 #endif
86
87 #define MCDPART(dev) DISKPART(dev)
88 #define MCDUNIT(dev) DISKUNIT(dev)
89
90 /* toc */
91 #define MCD_MAXTOCS 104 /* from the Linux driver */
92
93 struct mcd_mbx {
94 int retry, count;
95 struct buf *bp;
96 daddr_t blkno;
97 int nblk;
98 int sz;
99 u_long skip;
100 int state;
101 #define MCD_S_IDLE 0
102 #define MCD_S_BEGIN 1
103 #define MCD_S_WAITMODE 2
104 #define MCD_S_WAITREAD 3
105 int mode;
106 };
107
108 struct mcd_softc {
109 struct device sc_dev;
110 struct disk sc_dk;
111 void *sc_ih;
112
113 int iobase;
114 int irq, drq;
115
116 char *type;
117 u_char readcmd;
118 int flags;
119 #define MCDF_LOCKED 0x01
120 #define MCDF_WANTED 0x02
121 #define MCDF_WLABEL 0x04 /* label is writable */
122 #define MCDF_LABELLING 0x08 /* writing label */
123 #define MCDF_LOADED 0x10 /* parameters loaded */
124 short status;
125 short audio_status;
126 int blksize;
127 u_long disksize;
128 struct mcd_volinfo volinfo;
129 union mcd_qchninfo toc[MCD_MAXTOCS];
130 struct mcd_command lastpb;
131 struct mcd_mbx mbx;
132 int lastmode;
133 #define MCD_MD_UNKNOWN -1
134 int lastupc;
135 #define MCD_UPC_UNKNOWN -1
136 int debug;
137 struct buf buf_queue;
138 };
139
140 /* prototypes */
141 int mcdopen __P((dev_t, int, int, struct proc *));
142 int mcdclose __P((dev_t, int, int));
143 int mcdioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
144 int mcdsize __P((dev_t));
145
146 static int bcd2bin __P((bcd_t));
147 static bcd_t bin2bcd __P((int));
148 static void hsg2msf __P((int, bcd_t *));
149 static daddr_t msf2hsg __P((bcd_t *, int));
150
151 int mcd_playtracks __P((struct mcd_softc *, struct ioc_play_track *));
152 int mcd_playmsf __P((struct mcd_softc *, struct ioc_play_msf *));
153 int mcd_playblocks __P((struct mcd_softc *, struct ioc_play_blocks *));
154 int mcd_stop __P((struct mcd_softc *));
155 int mcd_eject __P((struct mcd_softc *));
156 int mcd_read_subchannel __P((struct mcd_softc *, struct ioc_read_subchannel *));
157 int mcd_pause __P((struct mcd_softc *));
158 int mcd_resume __P((struct mcd_softc *));
159 int mcd_toc_header __P((struct mcd_softc *, struct ioc_toc_header *));
160 int mcd_toc_entries __P((struct mcd_softc *, struct ioc_read_toc_entry *));
161
162 int mcd_getreply __P((struct mcd_softc *));
163 int mcd_getstat __P((struct mcd_softc *));
164 int mcd_getresult __P((struct mcd_softc *, struct mcd_result *));
165 void mcd_setflags __P((struct mcd_softc *));
166 int mcd_get __P((struct mcd_softc *, char *, int));
167 int mcd_send __P((struct mcd_softc *, struct mcd_mbox *, int));
168 int mcdintr __P((void *));
169 void mcd_soft_reset __P((struct mcd_softc *));
170 int mcd_hard_reset __P((struct mcd_softc *));
171 int mcd_setmode __P((struct mcd_softc *, int));
172 int mcd_setupc __P((struct mcd_softc *, int));
173 int mcd_read_toc __P((struct mcd_softc *));
174 int mcd_getqchan __P((struct mcd_softc *, union mcd_qchninfo *, int));
175 int mcd_setlock __P((struct mcd_softc *, int));
176
177 int mcdprobe __P((struct device *, void *, void *));
178 void mcdattach __P((struct device *, struct device *, void *));
179
180 struct cfdriver mcdcd = {
181 NULL, "mcd", mcdprobe, mcdattach, DV_DISK, sizeof(struct mcd_softc)
182 };
183
184 void mcdgetdisklabel __P((struct mcd_softc *));
185 int mcd_get_parms __P((struct mcd_softc *));
186 void mcdstrategy __P((struct buf *));
187 void mcdstart __P((struct mcd_softc *));
188
189 struct dkdriver mcddkdriver = { mcdstrategy };
190
191 #define MCD_RETRIES 3
192 #define MCD_RDRETRIES 3
193
194 /* several delays */
195 #define RDELAY_WAITMODE 300
196 #define RDELAY_WAITREAD 800
197
198 #define DELAY_GRANULARITY 25 /* 25us */
199 #define DELAY_GETREPLY 100000 /* 100000 * 25us */
200
201 void
202 mcdattach(parent, self, aux)
203 struct device *parent, *self;
204 void *aux;
205 {
206 struct mcd_softc *sc = (void *)self;
207 struct isa_attach_args *ia = aux;
208 struct mcd_mbox mbx;
209
210 /*
211 * Initialize and attach the disk structure.
212 */
213 sc->sc_dk.dk_driver = &mcddkdriver;
214 sc->sc_dk.dk_name = sc->sc_dev.dv_xname;
215 disk_attach(&sc->sc_dk);
216
217 printf(": model %s\n", sc->type != 0 ? sc->type : "unknown");
218
219 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
220
221 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
222 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
223 mbx.cmd.data.config.subcommand = MCD_CF_IRQENABLE;
224 mbx.cmd.data.config.data1 = 0x01;
225 mbx.res.length = 0;
226 (void) mcd_send(sc, &mbx, 0);
227
228 mcd_soft_reset(sc);
229
230 sc->sc_ih = isa_intr_establish(ia->ia_irq, IST_EDGE, IPL_BIO, mcdintr,
231 sc);
232 }
233
234 /*
235 * Wait interruptibly for an exclusive lock.
236 *
237 * XXX
238 * Several drivers do this; it should be abstracted and made MP-safe.
239 */
240 int
241 mcdlock(sc)
242 struct mcd_softc *sc;
243 {
244 int error;
245
246 while ((sc->flags & MCDF_LOCKED) != 0) {
247 sc->flags |= MCDF_WANTED;
248 if ((error = tsleep(sc, PRIBIO | PCATCH, "mcdlck", 0)) != 0)
249 return error;
250 }
251 sc->flags |= MCDF_LOCKED;
252 return 0;
253 }
254
255 /*
256 * Unlock and wake up any waiters.
257 */
258 void
259 mcdunlock(sc)
260 struct mcd_softc *sc;
261 {
262
263 sc->flags &= ~MCDF_LOCKED;
264 if ((sc->flags & MCDF_WANTED) != 0) {
265 sc->flags &= ~MCDF_WANTED;
266 wakeup(sc);
267 }
268 }
269
270 int
271 mcdopen(dev, flag, fmt, p)
272 dev_t dev;
273 int flag, fmt;
274 struct proc *p;
275 {
276 int error;
277 int unit, part;
278 struct mcd_softc *sc;
279
280 unit = MCDUNIT(dev);
281 if (unit >= mcdcd.cd_ndevs)
282 return ENXIO;
283 sc = mcdcd.cd_devs[unit];
284 if (!sc)
285 return ENXIO;
286
287 if (error = mcdlock(sc))
288 return error;
289
290 if (sc->sc_dk.dk_openmask != 0) {
291 /*
292 * If any partition is open, but the disk has been invalidated,
293 * disallow further opens.
294 */
295 if ((sc->flags & MCDF_LOADED) == 0) {
296 error = EIO;
297 goto bad3;
298 }
299 } else {
300 /*
301 * Lock the drawer. This will also notice any pending disk
302 * change or door open indicator and clear the MCDF_LOADED bit
303 * if necessary.
304 */
305 (void) mcd_setlock(sc, MCD_LK_LOCK);
306
307 if ((sc->flags & MCDF_LOADED) == 0) {
308 /* Partially reset the state. */
309 sc->lastmode = MCD_MD_UNKNOWN;
310 sc->lastupc = MCD_UPC_UNKNOWN;
311
312 sc->flags |= MCDF_LOADED;
313
314 /* Set the mode, causing the disk to spin up. */
315 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
316 goto bad2;
317
318 /* Load the physical device parameters. */
319 if (mcd_get_parms(sc) != 0) {
320 error = ENXIO;
321 goto bad2;
322 }
323
324 /* Read the table of contents. */
325 if ((error = mcd_read_toc(sc)) != 0)
326 goto bad2;
327
328 /* Fabricate a disk label. */
329 mcdgetdisklabel(sc);
330 }
331 }
332
333 MCD_TRACE("open: partition=%d disksize=%d blksize=%d\n", part,
334 sc->disksize, sc->blksize, 0);
335
336 part = MCDPART(dev);
337
338 /* Check that the partition exists. */
339 if (part != RAW_PART &&
340 (part >= sc->sc_dk.dk_label->d_npartitions ||
341 sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
342 error = ENXIO;
343 goto bad;
344 }
345
346 /* Insure only one open at a time. */
347 switch (fmt) {
348 case S_IFCHR:
349 sc->sc_dk.dk_copenmask |= (1 << part);
350 break;
351 case S_IFBLK:
352 sc->sc_dk.dk_bopenmask |= (1 << part);
353 break;
354 }
355 sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
356
357 mcdunlock(sc);
358 return 0;
359
360 bad2:
361 sc->flags &= ~MCDF_LOADED;
362
363 bad:
364 if (sc->sc_dk.dk_openmask == 0) {
365 #if 0
366 (void) mcd_setmode(sc, MCD_MD_SLEEP);
367 #endif
368 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
369 }
370
371 bad3:
372 mcdunlock(sc);
373 return error;
374 }
375
376 int
377 mcdclose(dev, flag, fmt)
378 dev_t dev;
379 int flag, fmt;
380 {
381 struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(dev)];
382 int part = MCDPART(dev);
383 int error;
384
385 MCD_TRACE("close: partition=%d\n", part, 0, 0, 0);
386
387 if (error = mcdlock(sc))
388 return error;
389
390 switch (fmt) {
391 case S_IFCHR:
392 sc->sc_dk.dk_copenmask &= ~(1 << part);
393 break;
394 case S_IFBLK:
395 sc->sc_dk.dk_bopenmask &= ~(1 << part);
396 break;
397 }
398 sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
399
400 if (sc->sc_dk.dk_openmask == 0) {
401 /* XXXX Must wait for I/O to complete! */
402
403 #if 0
404 (void) mcd_setmode(sc, MCD_MD_SLEEP);
405 #endif
406 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
407 }
408
409 mcdunlock(sc);
410 return 0;
411 }
412
413 void
414 mcdstrategy(bp)
415 struct buf *bp;
416 {
417 struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(bp->b_dev)];
418 int s;
419
420 /* Test validity. */
421 MCD_TRACE("strategy: buf=0x%lx blkno=%ld bcount=%ld\n", bp,
422 bp->b_blkno, bp->b_bcount, 0);
423 if (bp->b_blkno < 0 ||
424 (bp->b_bcount % sc->blksize) != 0) {
425 printf("%s: strategy: blkno = %d bcount = %d\n",
426 sc->sc_dev.dv_xname, bp->b_blkno, bp->b_bcount);
427 bp->b_error = EINVAL;
428 goto bad;
429 }
430
431 /* If device invalidated (e.g. media change, door open), error. */
432 if ((sc->flags & MCDF_LOADED) == 0) {
433 MCD_TRACE("strategy: drive not valid\n", 0, 0, 0, 0);
434 bp->b_error = EIO;
435 goto bad;
436 }
437
438 /* No data to read. */
439 if (bp->b_bcount == 0)
440 goto done;
441
442 /*
443 * Do bounds checking, adjust transfer. if error, process.
444 * If end of partition, just return.
445 */
446 if (MCDPART(bp->b_dev) != RAW_PART &&
447 bounds_check_with_label(bp, sc->sc_dk.dk_label,
448 (sc->flags & (MCDF_WLABEL|MCDF_LABELLING)) != 0) <= 0)
449 goto done;
450
451 /* Queue it. */
452 s = splbio();
453 disksort(&sc->buf_queue, bp);
454 splx(s);
455 if (!sc->buf_queue.b_active)
456 mcdstart(sc);
457 return;
458
459 bad:
460 bp->b_flags |= B_ERROR;
461 done:
462 bp->b_resid = bp->b_bcount;
463 biodone(bp);
464 }
465
466 void
467 mcdstart(sc)
468 struct mcd_softc *sc;
469 {
470 struct buf *bp, *dp = &sc->buf_queue;
471 int s;
472
473 loop:
474 s = splbio();
475
476 bp = dp->b_actf;
477 if (bp == NULL) {
478 /* Nothing to do. */
479 dp->b_active = 0;
480 splx(s);
481 return;
482 }
483
484 /* Block found to process; dequeue. */
485 MCD_TRACE("start: found block bp=0x%x\n", bp, 0, 0, 0);
486 dp->b_actf = bp->b_actf;
487 splx(s);
488
489 /* Changed media? */
490 if ((sc->flags & MCDF_LOADED) == 0) {
491 MCD_TRACE("start: drive not valid\n", 0, 0, 0, 0);
492 bp->b_error = EIO;
493 bp->b_flags |= B_ERROR;
494 biodone(bp);
495 goto loop;
496 }
497
498 dp->b_active = 1;
499
500 /* Instrumentation. */
501 s = splbio();
502 disk_busy(&sc->sc_dk);
503 splx(s);
504
505 sc->mbx.retry = MCD_RDRETRIES;
506 sc->mbx.bp = bp;
507 sc->mbx.blkno = bp->b_blkno / (sc->blksize / DEV_BSIZE);
508 if (MCDPART(bp->b_dev) != RAW_PART) {
509 struct partition *p;
510 p = &sc->sc_dk.dk_label->d_partitions[MCDPART(bp->b_dev)];
511 sc->mbx.blkno += p->p_offset;
512 }
513 sc->mbx.nblk = bp->b_bcount / sc->blksize;
514 sc->mbx.sz = sc->blksize;
515 sc->mbx.skip = 0;
516 sc->mbx.state = MCD_S_BEGIN;
517 sc->mbx.mode = MCD_MD_COOKED;
518
519 s = splbio();
520 (void) mcdintr(sc);
521 splx(s);
522 }
523
524 int
525 mcdread(dev, uio)
526 dev_t dev;
527 struct uio *uio;
528 {
529
530 return (physio(mcdstrategy, NULL, dev, B_READ, minphys, uio));
531 }
532
533 int
534 mcdwrite(dev, uio)
535 dev_t dev;
536 struct uio *uio;
537 {
538
539 return (physio(mcdstrategy, NULL, dev, B_WRITE, minphys, uio));
540 }
541
542 int
543 mcdioctl(dev, cmd, addr, flag, p)
544 dev_t dev;
545 u_long cmd;
546 caddr_t addr;
547 int flag;
548 struct proc *p;
549 {
550 struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(dev)];
551 int error;
552
553 MCD_TRACE("ioctl: cmd=0x%x\n", cmd, 0, 0, 0);
554
555 if ((sc->flags & MCDF_LOADED) == 0)
556 return EIO;
557
558 switch (cmd) {
559 case DIOCGDINFO:
560 *(struct disklabel *)addr = *(sc->sc_dk.dk_label);
561 return 0;
562
563 case DIOCGPART:
564 ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
565 ((struct partinfo *)addr)->part =
566 &sc->sc_dk.dk_label->d_partitions[MCDPART(dev)];
567 return 0;
568
569 case DIOCWDINFO:
570 case DIOCSDINFO:
571 if ((flag & FWRITE) == 0)
572 return EBADF;
573
574 if (error = mcdlock(sc))
575 return error;
576 sc->flags |= MCDF_LABELLING;
577
578 error = setdisklabel(sc->sc_dk.dk_label,
579 (struct disklabel *)addr, /*sc->sc_dk.dk_openmask : */0,
580 sc->sc_dk.dk_cpulabel);
581 if (error == 0) {
582 }
583
584 sc->flags &= ~MCDF_LABELLING;
585 mcdunlock(sc);
586 return error;
587
588 case DIOCWLABEL:
589 return EBADF;
590
591 case CDIOCPLAYTRACKS:
592 return mcd_playtracks(sc, (struct ioc_play_track *)addr);
593 case CDIOCPLAYMSF:
594 return mcd_playmsf(sc, (struct ioc_play_msf *)addr);
595 case CDIOCPLAYBLOCKS:
596 return mcd_playblocks(sc, (struct ioc_play_blocks *)addr);
597 case CDIOCREADSUBCHANNEL:
598 return mcd_read_subchannel(sc, (struct ioc_read_subchannel *)addr);
599 case CDIOREADTOCHEADER:
600 return mcd_toc_header(sc, (struct ioc_toc_header *)addr);
601 case CDIOREADTOCENTRYS:
602 return mcd_toc_entries(sc, (struct ioc_read_toc_entry *)addr);
603 case CDIOCSETPATCH:
604 case CDIOCGETVOL:
605 case CDIOCSETVOL:
606 case CDIOCSETMONO:
607 case CDIOCSETSTEREO:
608 case CDIOCSETMUTE:
609 case CDIOCSETLEFT:
610 case CDIOCSETRIGHT:
611 return EINVAL;
612 case CDIOCRESUME:
613 return mcd_resume(sc);
614 case CDIOCPAUSE:
615 return mcd_pause(sc);
616 case CDIOCSTART:
617 return EINVAL;
618 case CDIOCSTOP:
619 return mcd_stop(sc);
620 case CDIOCEJECT:
621 return mcd_eject(sc);
622 case CDIOCALLOW:
623 return mcd_setlock(sc, MCD_LK_UNLOCK);
624 case CDIOCPREVENT:
625 return mcd_setlock(sc, MCD_LK_LOCK);
626 case CDIOCSETDEBUG:
627 sc->debug = 1;
628 return 0;
629 case CDIOCCLRDEBUG:
630 sc->debug = 0;
631 return 0;
632 case CDIOCRESET:
633 return mcd_hard_reset(sc);
634
635 default:
636 return ENOTTY;
637 }
638
639 #ifdef DIAGNOSTIC
640 panic("mcdioctl: impossible");
641 #endif
642 }
643
644 /*
645 * This could have been taken from scsi/cd.c, but it is not clear
646 * whether the scsi cd driver is linked in.
647 */
648 void
649 mcdgetdisklabel(sc)
650 struct mcd_softc *sc;
651 {
652 struct disklabel *lp = sc->sc_dk.dk_label;
653
654 bzero(lp, sizeof(struct disklabel));
655 bzero(sc->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
656
657 lp->d_secsize = sc->blksize;
658 lp->d_ntracks = 1;
659 lp->d_nsectors = 100;
660 lp->d_ncylinders = (sc->disksize / 100) + 1;
661 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
662
663 strncpy(lp->d_typename, "Mitsumi CD-ROM", 16);
664 lp->d_type = 0; /* XXX */
665 strncpy(lp->d_packname, "fictitious", 16);
666 lp->d_secperunit = sc->disksize;
667 lp->d_rpm = 300;
668 lp->d_interleave = 1;
669 lp->d_flags = D_REMOVABLE;
670
671 lp->d_partitions[0].p_offset = 0;
672 lp->d_partitions[0].p_size =
673 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
674 lp->d_partitions[0].p_fstype = FS_ISO9660;
675 lp->d_partitions[RAW_PART].p_offset = 0;
676 lp->d_partitions[RAW_PART].p_size =
677 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
678 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660;
679 lp->d_npartitions = RAW_PART + 1;
680
681 lp->d_magic = DISKMAGIC;
682 lp->d_magic2 = DISKMAGIC;
683 lp->d_checksum = dkcksum(lp);
684 }
685
686 int
687 mcd_get_parms(sc)
688 struct mcd_softc *sc;
689 {
690 struct mcd_mbox mbx;
691 daddr_t size;
692 int error;
693
694 /* Send volume info command. */
695 mbx.cmd.opcode = MCD_CMDGETVOLINFO;
696 mbx.cmd.length = 0;
697 mbx.res.length = sizeof(mbx.res.data.volinfo);
698 if ((error = mcd_send(sc, &mbx, 1)) != 0)
699 return error;
700
701 if (mbx.res.data.volinfo.trk_low == 0x00 &&
702 mbx.res.data.volinfo.trk_high == 0x00)
703 return EINVAL;
704
705 /* Volinfo is OK. */
706 sc->volinfo = mbx.res.data.volinfo;
707 sc->blksize = MCD_BLKSIZE_COOKED;
708 size = msf2hsg(sc->volinfo.vol_msf, 0);
709 sc->disksize = size * (MCD_BLKSIZE_COOKED / DEV_BSIZE);
710 return 0;
711 }
712
713 int
714 mcdsize(dev)
715 dev_t dev;
716 {
717
718 /* CD-ROMs are read-only. */
719 return -1;
720 }
721
722 int
723 mcddump(dev, blkno, va, size)
724 dev_t dev;
725 daddr_t blkno;
726 caddr_t va;
727 size_t size;
728 {
729
730 /* Not implemented. */
731 return ENXIO;
732 }
733
734 int
735 mcdprobe(parent, match, aux)
736 struct device *parent;
737 void *match, *aux;
738 {
739 struct mcd_softc *sc = match;
740 struct isa_attach_args *ia = aux;
741 int iobase = ia->ia_iobase;
742 int i;
743 struct mcd_mbox mbx;
744
745 sc->iobase = iobase;
746
747 /* Send a reset. */
748 outb(iobase + MCD_RESET, 0);
749 delay(1000000);
750 /* Get any pending status and throw away. */
751 for (i = 10; i; i--)
752 inb(iobase + MCD_STATUS);
753 delay(1000);
754
755 /* Send get status command. */
756 mbx.cmd.opcode = MCD_CMDGETSTAT;
757 mbx.cmd.length = 0;
758 mbx.res.length = 0;
759 if (mcd_send(sc, &mbx, 0) != 0)
760 return 0;
761
762 /* Get info about the drive. */
763 mbx.cmd.opcode = MCD_CMDCONTINFO;
764 mbx.cmd.length = 0;
765 mbx.res.length = sizeof(mbx.res.data.continfo);
766 if (mcd_send(sc, &mbx, 0) != 0)
767 return 0;
768
769 /*
770 * The following is code which is not guaranteed to work for all
771 * drives, because the meaning of the expected 'M' is not clear
772 * (M_itsumi is an obvious assumption, but I don't trust that).
773 * Also, the original hack had a bogus condition that always
774 * returned true.
775 *
776 * Note: Which models support interrupts? >=LU005S?
777 */
778 sc->readcmd = MCD_CMDREADSINGLESPEED;
779 switch (mbx.res.data.continfo.code) {
780 case 'M':
781 if (mbx.res.data.continfo.version <= 2)
782 sc->type = "LU002S";
783 else if (mbx.res.data.continfo.version <= 5)
784 sc->type = "LU005S";
785 else
786 sc->type = "LU006S";
787 break;
788 case 'F':
789 sc->type = "FX001";
790 break;
791 case 'D':
792 sc->type = "FX001D";
793 sc->readcmd = MCD_CMDREADDOUBLESPEED;
794 break;
795 default:
796 printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n",
797 sc->sc_dev.dv_xname,
798 mbx.res.data.continfo.code, mbx.res.data.continfo.version);
799 sc->type = 0;
800 break;
801 }
802
803 ia->ia_iosize = 4;
804 ia->ia_msize = 0;
805 return 1;
806 }
807
808 int
809 mcd_getreply(sc)
810 struct mcd_softc *sc;
811 {
812 int iobase = sc->iobase;
813 int i;
814
815 /* Wait until xfer port senses data ready. */
816 for (i = DELAY_GETREPLY; i; i--) {
817 if ((inb(iobase + MCD_XFER) & MCD_XF_STATUSUNAVAIL) == 0)
818 break;
819 delay(DELAY_GRANULARITY);
820 }
821 if (!i)
822 return -1;
823
824 /* Get the data. */
825 return inb(iobase + MCD_STATUS);
826 }
827
828 int
829 mcd_getstat(sc)
830 struct mcd_softc *sc;
831 {
832 struct mcd_mbox mbx;
833
834 mbx.cmd.opcode = MCD_CMDGETSTAT;
835 mbx.cmd.length = 0;
836 mbx.res.length = 0;
837 return mcd_send(sc, &mbx, 1);
838 }
839
840 int
841 mcd_getresult(sc, res)
842 struct mcd_softc *sc;
843 struct mcd_result *res;
844 {
845 int i, x;
846
847 if (sc->debug)
848 printf("%s: mcd_getresult: %d", sc->sc_dev.dv_xname,
849 res->length);
850
851 if ((x = mcd_getreply(sc)) < 0) {
852 if (sc->debug)
853 printf(" timeout\n");
854 else
855 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
856 return EIO;
857 }
858 if (sc->debug)
859 printf(" %02x", (u_int)x);
860 sc->status = x;
861 mcd_setflags(sc);
862
863 if ((sc->status & MCD_ST_CMDCHECK) != 0)
864 return EINVAL;
865
866 for (i = 0; i < res->length; i++) {
867 if ((x = mcd_getreply(sc)) < 0) {
868 if (sc->debug)
869 printf(" timeout\n");
870 else
871 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
872 return EIO;
873 }
874 if (sc->debug)
875 printf(" %02x", (u_int)x);
876 res->data.raw.data[i] = x;
877 }
878
879 if (sc->debug)
880 printf(" succeeded\n");
881
882 #ifdef MCDDEBUG
883 delay(10);
884 while ((inb(sc->iobase + MCD_XFER) & MCD_XF_STATUSUNAVAIL) == 0) {
885 x = inb(sc->iobase + MCD_STATUS);
886 printf("%s: got extra byte %02x during getstatus\n",
887 sc->sc_dev.dv_xname, (u_int)x);
888 delay(10);
889 }
890 #endif
891
892 return 0;
893 }
894
895 void
896 mcd_setflags(sc)
897 struct mcd_softc *sc;
898 {
899
900 /* Check flags. */
901 if ((sc->flags & MCDF_LOADED) != 0 &&
902 (sc->status & (MCD_ST_DSKCHNG | MCD_ST_DSKIN | MCD_ST_DOOROPEN)) !=
903 MCD_ST_DSKIN) {
904 if ((sc->status & MCD_ST_DOOROPEN) != 0)
905 printf("%s: door open\n", sc->sc_dev.dv_xname);
906 else if ((sc->status & MCD_ST_DSKIN) == 0)
907 printf("%s: no disk present\n", sc->sc_dev.dv_xname);
908 else if ((sc->status & MCD_ST_DSKCHNG) != 0)
909 printf("%s: media change\n", sc->sc_dev.dv_xname);
910 sc->flags &= ~MCDF_LOADED;
911 }
912
913 if ((sc->status & MCD_ST_AUDIOBSY) != 0)
914 sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
915 else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS ||
916 sc->audio_status == CD_AS_AUDIO_INVALID)
917 sc->audio_status = CD_AS_PLAY_COMPLETED;
918 }
919
920 int
921 mcd_send(sc, mbx, diskin)
922 struct mcd_softc *sc;
923 struct mcd_mbox *mbx;
924 int diskin;
925 {
926 int iobase = sc->iobase;
927 int retry, i, error;
928
929 if (sc->debug) {
930 printf("%s: mcd_send: %d %02x", sc->sc_dev.dv_xname,
931 mbx->cmd.length, (u_int)mbx->cmd.opcode);
932 for (i = 0; i < mbx->cmd.length; i++)
933 printf(" %02x", (u_int)mbx->cmd.data.raw.data[i]);
934 printf("\n");
935 }
936
937 for (retry = MCD_RETRIES; retry; retry--) {
938 outb(iobase + MCD_COMMAND, mbx->cmd.opcode);
939 for (i = 0; i < mbx->cmd.length; i++)
940 outb(iobase + MCD_COMMAND, mbx->cmd.data.raw.data[i]);
941 if ((error = mcd_getresult(sc, &mbx->res)) == 0)
942 break;
943 if (error == EINVAL)
944 return error;
945 }
946 if (!retry)
947 return error;
948 if (diskin && (sc->flags & MCDF_LOADED) == 0)
949 return EIO;
950
951 return 0;
952 }
953
954 static int
955 bcd2bin(b)
956 bcd_t b;
957 {
958
959 return (b >> 4) * 10 + (b & 15);
960 }
961
962 static bcd_t
963 bin2bcd(b)
964 int b;
965 {
966
967 return ((b / 10) << 4) | (b % 10);
968 }
969
970 static void
971 hsg2msf(hsg, msf)
972 int hsg;
973 bcd_t *msf;
974 {
975
976 hsg += 150;
977 F_msf(msf) = bin2bcd(hsg % 75);
978 hsg /= 75;
979 S_msf(msf) = bin2bcd(hsg % 60);
980 hsg /= 60;
981 M_msf(msf) = bin2bcd(hsg);
982 }
983
984 static daddr_t
985 msf2hsg(msf, relative)
986 bcd_t *msf;
987 int relative;
988 {
989 daddr_t blkno;
990
991 blkno = bcd2bin(M_msf(msf)) * 75 * 60 +
992 bcd2bin(S_msf(msf)) * 75 +
993 bcd2bin(F_msf(msf));
994 if (!relative)
995 blkno -= 150;
996 return blkno;
997 }
998
999 void
1000 mcd_pseudointr(sc)
1001 struct mcd_softc *sc;
1002 {
1003 int s;
1004
1005 s = splbio();
1006 (void) mcdintr(sc);
1007 splx(s);
1008 }
1009
1010 /*
1011 * State machine to process read requests.
1012 * Initialize with MCD_S_BEGIN: calculate sizes, and set mode
1013 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
1014 * MCD_S_WAITREAD: wait for read ready, read data.
1015 */
1016 int
1017 mcdintr(arg)
1018 void *arg;
1019 {
1020 struct mcd_softc *sc = arg;
1021 struct mcd_mbx *mbx = &sc->mbx;
1022 int iobase = sc->iobase;
1023 struct buf *bp = mbx->bp;
1024
1025 int i;
1026 u_char x;
1027 bcd_t msf[3];
1028
1029 switch (mbx->state) {
1030 case MCD_S_IDLE:
1031 return 0;
1032
1033 case MCD_S_BEGIN:
1034 tryagain:
1035 if (mbx->mode == sc->lastmode)
1036 goto firstblock;
1037
1038 sc->lastmode = MCD_MD_UNKNOWN;
1039 outb(iobase + MCD_COMMAND, MCD_CMDSETMODE);
1040 outb(iobase + MCD_COMMAND, mbx->mode);
1041
1042 mbx->count = RDELAY_WAITMODE;
1043 mbx->state = MCD_S_WAITMODE;
1044
1045 case MCD_S_WAITMODE:
1046 untimeout(mcd_pseudointr, sc);
1047 for (i = 20; i; i--) {
1048 x = inb(iobase + MCD_XFER);
1049 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1050 break;
1051 delay(50);
1052 }
1053 if (i == 0)
1054 goto hold;
1055 sc->status = inb(iobase + MCD_STATUS);
1056 mcd_setflags(sc);
1057 if ((sc->flags & MCDF_LOADED) == 0)
1058 goto changed;
1059 MCD_TRACE("doread: got WAITMODE delay=%d\n",
1060 RDELAY_WAITMODE - mbx->count, 0, 0, 0);
1061
1062 sc->lastmode = mbx->mode;
1063
1064 firstblock:
1065 MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", mbx->blkno,
1066 bp, 0, 0);
1067
1068 /* Build parameter block. */
1069 hsg2msf(mbx->blkno, msf);
1070
1071 /* Send the read command. */
1072 outb(iobase + MCD_COMMAND, sc->readcmd);
1073 outb(iobase + MCD_COMMAND, msf[0]);
1074 outb(iobase + MCD_COMMAND, msf[1]);
1075 outb(iobase + MCD_COMMAND, msf[2]);
1076 outb(iobase + MCD_COMMAND, 0);
1077 outb(iobase + MCD_COMMAND, 0);
1078 outb(iobase + MCD_COMMAND, mbx->nblk);
1079
1080 mbx->count = RDELAY_WAITREAD;
1081 mbx->state = MCD_S_WAITREAD;
1082
1083 case MCD_S_WAITREAD:
1084 untimeout(mcd_pseudointr, sc);
1085 nextblock:
1086 loop:
1087 for (i = 20; i; i--) {
1088 x = inb(iobase + MCD_XFER);
1089 if ((x & MCD_XF_DATAUNAVAIL) == 0)
1090 goto gotblock;
1091 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1092 break;
1093 delay(50);
1094 }
1095 if (i == 0)
1096 goto hold;
1097 sc->status = inb(iobase + MCD_STATUS);
1098 mcd_setflags(sc);
1099 if ((sc->flags & MCDF_LOADED) == 0)
1100 goto changed;
1101 #if 0
1102 printf("%s: got status byte %02x during read\n",
1103 sc->sc_dev.dv_xname, (u_int)sc->status);
1104 #endif
1105 goto loop;
1106
1107 gotblock:
1108 MCD_TRACE("doread: got data delay=%d\n",
1109 RDELAY_WAITREAD - mbx->count, 0, 0, 0);
1110
1111 /* Data is ready. */
1112 outb(iobase + MCD_CTL2, 0x04); /* XXX */
1113 insb(iobase + MCD_RDATA, bp->b_data + mbx->skip, mbx->sz);
1114 outb(iobase + MCD_CTL2, 0x0c); /* XXX */
1115 mbx->blkno += 1;
1116 mbx->skip += mbx->sz;
1117 if (--mbx->nblk > 0)
1118 goto nextblock;
1119
1120 mbx->state = MCD_S_IDLE;
1121
1122 /* Return buffer. */
1123 bp->b_resid = 0;
1124 disk_unbusy(&sc->sc_dk, bp->b_bcount);
1125 biodone(bp);
1126
1127 mcdstart(sc);
1128 return 1;
1129
1130 hold:
1131 if (mbx->count-- < 0) {
1132 printf("%s: timeout in state %d",
1133 sc->sc_dev.dv_xname, mbx->state);
1134 goto readerr;
1135 }
1136
1137 #if 0
1138 printf("%s: sleep in state %d\n", sc->sc_dev.dv_xname,
1139 mbx->state);
1140 #endif
1141 timeout(mcd_pseudointr, sc, hz / 100);
1142 return -1;
1143 }
1144
1145 readerr:
1146 if (mbx->retry-- > 0) {
1147 printf("; retrying\n");
1148 goto tryagain;
1149 } else
1150 printf("; giving up\n");
1151
1152 changed:
1153 harderr:
1154 /* Invalidate the buffer. */
1155 bp->b_flags |= B_ERROR;
1156 bp->b_resid = bp->b_bcount - mbx->skip;
1157 disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid));
1158 biodone(bp);
1159
1160 mcdstart(sc);
1161 return -1;
1162
1163 #ifdef notyet
1164 printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname);
1165 outb(mbx->iobase + MCD_RESET, MCD_CMDRESET);
1166 delay(300000);
1167 (void) mcd_getstat(sc, 1);
1168 (void) mcd_getstat(sc, 1);
1169 /*sc->status &= ~MCD_ST_DSKCHNG; */
1170 sc->debug = 1; /* preventive set debug mode */
1171 #endif
1172 }
1173
1174 void
1175 mcd_soft_reset(sc)
1176 struct mcd_softc *sc;
1177 {
1178
1179 sc->debug = 0;
1180 sc->flags = 0;
1181 sc->lastmode = MCD_MD_UNKNOWN;
1182 sc->lastupc = MCD_UPC_UNKNOWN;
1183 sc->audio_status = CD_AS_AUDIO_INVALID;
1184 outb(sc->iobase + MCD_CTL2, 0x0c); /* XXX */
1185 }
1186
1187 int
1188 mcd_hard_reset(sc)
1189 struct mcd_softc *sc;
1190 {
1191 struct mcd_mbox mbx;
1192
1193 mcd_soft_reset(sc);
1194
1195 mbx.cmd.opcode = MCD_CMDRESET;
1196 mbx.cmd.length = 0;
1197 mbx.res.length = 0;
1198 return mcd_send(sc, &mbx, 0);
1199 }
1200
1201 int
1202 mcd_setmode(sc, mode)
1203 struct mcd_softc *sc;
1204 int mode;
1205 {
1206 struct mcd_mbox mbx;
1207 int error;
1208
1209 if (sc->lastmode == mode)
1210 return 0;
1211 if (sc->debug)
1212 printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode);
1213 sc->lastmode = MCD_MD_UNKNOWN;
1214
1215 mbx.cmd.opcode = MCD_CMDSETMODE;
1216 mbx.cmd.length = sizeof(mbx.cmd.data.datamode);
1217 mbx.cmd.data.datamode.mode = mode;
1218 mbx.res.length = 0;
1219 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1220 return error;
1221
1222 sc->lastmode = mode;
1223 return 0;
1224 }
1225
1226 int
1227 mcd_setupc(sc, upc)
1228 struct mcd_softc *sc;
1229 int upc;
1230 {
1231 struct mcd_mbox mbx;
1232 int error;
1233
1234 if (sc->lastupc == upc)
1235 return 0;
1236 if (sc->debug)
1237 printf("%s: setting upc to %d\n", sc->sc_dev.dv_xname, upc);
1238 sc->lastupc = MCD_UPC_UNKNOWN;
1239
1240 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
1241 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
1242 mbx.cmd.data.config.subcommand = MCD_CF_READUPC;
1243 mbx.cmd.data.config.data1 = upc;
1244 mbx.res.length = 0;
1245 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1246 return error;
1247
1248 sc->lastupc = upc;
1249 return 0;
1250 }
1251
1252 int
1253 mcd_toc_header(sc, th)
1254 struct mcd_softc *sc;
1255 struct ioc_toc_header *th;
1256 {
1257
1258 if (sc->debug)
1259 printf("%s: mcd_toc_header: reading toc header\n",
1260 sc->sc_dev.dv_xname);
1261
1262 th->len = msf2hsg(sc->volinfo.vol_msf, 0);
1263 th->starting_track = bcd2bin(sc->volinfo.trk_low);
1264 th->ending_track = bcd2bin(sc->volinfo.trk_high);
1265
1266 return 0;
1267 }
1268
1269 int
1270 mcd_read_toc(sc)
1271 struct mcd_softc *sc;
1272 {
1273 struct ioc_toc_header th;
1274 union mcd_qchninfo q;
1275 int error, trk, idx, retry;
1276
1277 if ((error = mcd_toc_header(sc, &th)) != 0)
1278 return error;
1279
1280 if ((error = mcd_stop(sc)) != 0)
1281 return error;
1282
1283 if (sc->debug)
1284 printf("%s: read_toc: reading qchannel info\n",
1285 sc->sc_dev.dv_xname);
1286
1287 for (trk = th.starting_track; trk <= th.ending_track; trk++)
1288 sc->toc[trk].toc.idx_no = 0x00;
1289 trk = th.ending_track - th.starting_track + 1;
1290 for (retry = 300; retry && trk > 0; retry--) {
1291 if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0)
1292 break;
1293 if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00)
1294 continue;
1295 idx = bcd2bin(q.toc.idx_no);
1296 if (idx < MCD_MAXTOCS &&
1297 sc->toc[idx].toc.idx_no == 0x00) {
1298 sc->toc[idx] = q;
1299 trk--;
1300 }
1301 }
1302
1303 /* Inform the drive that we're finished so it turns off the light. */
1304 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1305 return error;
1306
1307 if (trk != 0)
1308 return EINVAL;
1309
1310 /* Add a fake last+1 for mcd_playtracks(). */
1311 idx = th.ending_track + 1;
1312 sc->toc[idx].toc.control = sc->toc[idx-1].toc.control;
1313 sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type;
1314 sc->toc[idx].toc.trk_no = 0x00;
1315 sc->toc[idx].toc.idx_no = 0xaa;
1316 sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0];
1317 sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1];
1318 sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2];
1319
1320 return 0;
1321 }
1322
1323 int
1324 mcd_toc_entries(sc, te)
1325 struct mcd_softc *sc;
1326 struct ioc_read_toc_entry *te;
1327 {
1328 int len = te->data_len;
1329 struct ret_toc {
1330 struct ioc_toc_header header;
1331 struct cd_toc_entry entries[MCD_MAXTOCS];
1332 } data;
1333 u_char trk;
1334 daddr_t lba;
1335 int error, n;
1336
1337 if (len > sizeof(data.entries) ||
1338 len < sizeof(struct cd_toc_entry))
1339 return EINVAL;
1340 if (te->address_format != CD_MSF_FORMAT &&
1341 te->address_format != CD_LBA_FORMAT)
1342 return EINVAL;
1343
1344 /* Copy the TOC header. */
1345 if ((error = mcd_toc_header(sc, &data.header)) != 0)
1346 return error;
1347
1348 /* Verify starting track. */
1349 trk = te->starting_track;
1350 if (trk == 0x00)
1351 trk = data.header.starting_track;
1352 else if (trk == 0xaa)
1353 trk = data.header.ending_track + 1;
1354 else if (trk < data.header.starting_track ||
1355 trk > data.header.ending_track + 1)
1356 return EINVAL;
1357
1358 /* Copy the TOC data. */
1359 for (n = 0; trk <= data.header.ending_track + 1; trk++) {
1360 if (sc->toc[trk].toc.idx_no == 0x00)
1361 continue;
1362 data.entries[n].control = sc->toc[trk].toc.control;
1363 data.entries[n].addr_type = sc->toc[trk].toc.addr_type;
1364 data.entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no);
1365 switch (te->address_format) {
1366 case CD_MSF_FORMAT:
1367 data.entries[n].addr[0] = 0;
1368 data.entries[n].addr[1] = bcd2bin(sc->toc[trk].toc.absolute_pos[0]);
1369 data.entries[n].addr[2] = bcd2bin(sc->toc[trk].toc.absolute_pos[1]);
1370 data.entries[n].addr[3] = bcd2bin(sc->toc[trk].toc.absolute_pos[2]);
1371 break;
1372 case CD_LBA_FORMAT:
1373 lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0);
1374 data.entries[n].addr[0] = lba >> 24;
1375 data.entries[n].addr[1] = lba >> 16;
1376 data.entries[n].addr[2] = lba >> 8;
1377 data.entries[n].addr[3] = lba;
1378 break;
1379 }
1380 n++;
1381 }
1382
1383 len = min(len, n * sizeof(struct cd_toc_entry));
1384
1385 /* Copy the data back. */
1386 return copyout(&data.entries[0], te->data, len);
1387 }
1388
1389 int
1390 mcd_stop(sc)
1391 struct mcd_softc *sc;
1392 {
1393 struct mcd_mbox mbx;
1394 int error;
1395
1396 if (sc->debug)
1397 printf("%s: mcd_stop: stopping play\n", sc->sc_dev.dv_xname);
1398
1399 mbx.cmd.opcode = MCD_CMDSTOPAUDIO;
1400 mbx.cmd.length = 0;
1401 mbx.res.length = 0;
1402 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1403 return error;
1404
1405 sc->audio_status = CD_AS_PLAY_COMPLETED;
1406 return 0;
1407 }
1408
1409 int
1410 mcd_getqchan(sc, q, qchn)
1411 struct mcd_softc *sc;
1412 union mcd_qchninfo *q;
1413 int qchn;
1414 {
1415 struct mcd_mbox mbx;
1416 int error;
1417
1418 if (qchn == CD_TRACK_INFO) {
1419 if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0)
1420 return error;
1421 } else {
1422 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1423 return error;
1424 }
1425 if (qchn == CD_MEDIA_CATALOG) {
1426 if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0)
1427 return error;
1428 } else {
1429 if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0)
1430 return error;
1431 }
1432
1433 mbx.cmd.opcode = MCD_CMDGETQCHN;
1434 mbx.cmd.length = 0;
1435 mbx.res.length = sizeof(mbx.res.data.qchninfo);
1436 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1437 return error;
1438
1439 *q = mbx.res.data.qchninfo;
1440 return 0;
1441 }
1442
1443 int
1444 mcd_read_subchannel(sc, ch)
1445 struct mcd_softc *sc;
1446 struct ioc_read_subchannel *ch;
1447 {
1448 int len = ch->data_len;
1449 union mcd_qchninfo q;
1450 struct cd_sub_channel_info data;
1451 daddr_t lba;
1452 int error;
1453
1454 if (sc->debug)
1455 printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname,
1456 ch->address_format, ch->data_format);
1457
1458 if (len > sizeof(data) ||
1459 len < sizeof(struct cd_sub_channel_header))
1460 return EINVAL;
1461 if (ch->address_format != CD_MSF_FORMAT &&
1462 ch->address_format != CD_LBA_FORMAT)
1463 return EINVAL;
1464 if (ch->data_format != CD_CURRENT_POSITION &&
1465 ch->data_format != CD_MEDIA_CATALOG)
1466 return EINVAL;
1467
1468 if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0)
1469 return error;
1470
1471 data.header.audio_status = sc->audio_status;
1472 data.what.media_catalog.data_format = ch->data_format;
1473
1474 switch (ch->data_format) {
1475 case CD_MEDIA_CATALOG:
1476 data.what.media_catalog.mc_valid = 1;
1477 #if 0
1478 data.what.media_catalog.mc_number =
1479 #endif
1480 break;
1481
1482 case CD_CURRENT_POSITION:
1483 data.what.position.track_number = bcd2bin(q.current.trk_no);
1484 data.what.position.index_number = bcd2bin(q.current.idx_no);
1485 switch (ch->address_format) {
1486 case CD_MSF_FORMAT:
1487 data.what.position.reladdr[0] = 0;
1488 data.what.position.reladdr[1] = bcd2bin(q.current.relative_pos[0]);
1489 data.what.position.reladdr[2] = bcd2bin(q.current.relative_pos[1]);
1490 data.what.position.reladdr[3] = bcd2bin(q.current.relative_pos[2]);
1491 data.what.position.absaddr[0] = 0;
1492 data.what.position.absaddr[1] = bcd2bin(q.current.absolute_pos[0]);
1493 data.what.position.absaddr[2] = bcd2bin(q.current.absolute_pos[1]);
1494 data.what.position.absaddr[3] = bcd2bin(q.current.absolute_pos[2]);
1495 break;
1496 case CD_LBA_FORMAT:
1497 lba = msf2hsg(q.current.relative_pos, 1);
1498 /*
1499 * Pre-gap has index number of 0, and decreasing MSF
1500 * address. Must be converted to negative LBA, per
1501 * SCSI spec.
1502 */
1503 if (data.what.position.index_number == 0x00)
1504 lba = -lba;
1505 data.what.position.reladdr[0] = lba >> 24;
1506 data.what.position.reladdr[1] = lba >> 16;
1507 data.what.position.reladdr[2] = lba >> 8;
1508 data.what.position.reladdr[3] = lba;
1509 lba = msf2hsg(q.current.absolute_pos, 0);
1510 data.what.position.absaddr[0] = lba >> 24;
1511 data.what.position.absaddr[1] = lba >> 16;
1512 data.what.position.absaddr[2] = lba >> 8;
1513 data.what.position.absaddr[3] = lba;
1514 break;
1515 }
1516 break;
1517 }
1518
1519 return copyout(&data, ch->data, len);
1520 }
1521
1522 int
1523 mcd_playtracks(sc, p)
1524 struct mcd_softc *sc;
1525 struct ioc_play_track *p;
1526 {
1527 struct mcd_mbox mbx;
1528 int a = p->start_track;
1529 int z = p->end_track;
1530 int error;
1531
1532 if (sc->debug)
1533 printf("%s: playtracks: from %d:%d to %d:%d\n",
1534 sc->sc_dev.dv_xname,
1535 a, p->start_index, z, p->end_index);
1536
1537 if (a < bcd2bin(sc->volinfo.trk_low) ||
1538 a > bcd2bin(sc->volinfo.trk_high) ||
1539 a > z ||
1540 z < bcd2bin(sc->volinfo.trk_low) ||
1541 z > bcd2bin(sc->volinfo.trk_high))
1542 return EINVAL;
1543
1544 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1545 return error;
1546
1547 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1548 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1549 mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0];
1550 mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1];
1551 mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2];
1552 mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0];
1553 mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1];
1554 mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2];
1555 sc->lastpb = mbx.cmd;
1556 mbx.res.length = 0;
1557 return mcd_send(sc, &mbx, 1);
1558 }
1559
1560 int
1561 mcd_playmsf(sc, p)
1562 struct mcd_softc *sc;
1563 struct ioc_play_msf *p;
1564 {
1565 struct mcd_mbox mbx;
1566 int error;
1567
1568 if (sc->debug)
1569 printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n",
1570 sc->sc_dev.dv_xname,
1571 p->start_m, p->start_s, p->start_f,
1572 p->end_m, p->end_s, p->end_f);
1573
1574 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
1575 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f))
1576 return EINVAL;
1577
1578 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1579 return error;
1580
1581 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1582 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1583 mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m);
1584 mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s);
1585 mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f);
1586 mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m);
1587 mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s);
1588 mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f);
1589 sc->lastpb = mbx.cmd;
1590 mbx.res.length = 0;
1591 return mcd_send(sc, &mbx, 1);
1592 }
1593
1594 int
1595 mcd_playblocks(sc, p)
1596 struct mcd_softc *sc;
1597 struct ioc_play_blocks *p;
1598 {
1599 struct mcd_mbox mbx;
1600 int error;
1601
1602 if (sc->debug)
1603 printf("%s: playblocks: blkno %d length %d\n",
1604 sc->sc_dev.dv_xname, p->blk, p->len);
1605
1606 if (p->blk > sc->disksize || p->len > sc->disksize ||
1607 (p->blk + p->len) > sc->disksize)
1608 return 0;
1609
1610 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1611 return error;
1612
1613 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1614 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1615 hsg2msf(p->blk, mbx.cmd.data.play.start_msf);
1616 hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf);
1617 sc->lastpb = mbx.cmd;
1618 mbx.res.length = 0;
1619 return mcd_send(sc, &mbx, 1);
1620 }
1621
1622 int
1623 mcd_pause(sc)
1624 struct mcd_softc *sc;
1625 {
1626 union mcd_qchninfo q;
1627 int error;
1628
1629 /* Verify current status. */
1630 if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS) {
1631 printf("%s: pause: attempted when not playing\n",
1632 sc->sc_dev.dv_xname);
1633 return EINVAL;
1634 }
1635
1636 /* Get the current position. */
1637 if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0)
1638 return error;
1639
1640 /* Copy it into lastpb. */
1641 sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0];
1642 sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1];
1643 sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2];
1644
1645 /* Stop playing. */
1646 if ((error = mcd_stop(sc)) != 0)
1647 return error;
1648
1649 /* Set the proper status and exit. */
1650 sc->audio_status = CD_AS_PLAY_PAUSED;
1651 return 0;
1652 }
1653
1654 int
1655 mcd_resume(sc)
1656 struct mcd_softc *sc;
1657 {
1658 struct mcd_mbox mbx;
1659 int error;
1660
1661 if (sc->audio_status != CD_AS_PLAY_PAUSED)
1662 return EINVAL;
1663
1664 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1665 return error;
1666
1667 mbx.cmd = sc->lastpb;
1668 mbx.res.length = 0;
1669 return mcd_send(sc, &mbx, 1);
1670 }
1671
1672 int
1673 mcd_eject(sc)
1674 struct mcd_softc *sc;
1675 {
1676 struct mcd_mbox mbx;
1677
1678 mbx.cmd.opcode = MCD_CMDEJECTDISK;
1679 mbx.cmd.length = 0;
1680 mbx.res.length = 0;
1681 return mcd_send(sc, &mbx, 0);
1682 }
1683
1684 int
1685 mcd_setlock(sc, mode)
1686 struct mcd_softc *sc;
1687 int mode;
1688 {
1689 struct mcd_mbox mbx;
1690
1691 mbx.cmd.opcode = MCD_CMDSETLOCK;
1692 mbx.cmd.length = sizeof(mbx.cmd.data.lockmode);
1693 mbx.cmd.data.lockmode.mode = mode;
1694 mbx.res.length = 0;
1695 return mcd_send(sc, &mbx, 1);
1696 }
1697