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