mcd.c revision 1.52 1 /* $NetBSD: mcd.c,v 1.52 1996/11/05 07:17:25 mikel 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 #ifdef MCDDEBUG
811 printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n",
812 sc->sc_dev.dv_xname,
813 mbx.res.data.continfo.code, mbx.res.data.continfo.version);
814 #endif
815 sc->type = 0;
816 break;
817 }
818
819 ia->ia_iosize = 4;
820 ia->ia_msize = 0;
821 return 1;
822 }
823
824 int
825 mcd_getreply(sc)
826 struct mcd_softc *sc;
827 {
828 int iobase = sc->iobase;
829 int i;
830
831 /* Wait until xfer port senses data ready. */
832 for (i = DELAY_GETREPLY; i; i--) {
833 if ((inb(iobase + MCD_XFER) & MCD_XF_STATUSUNAVAIL) == 0)
834 break;
835 delay(DELAY_GRANULARITY);
836 }
837 if (!i)
838 return -1;
839
840 /* Get the data. */
841 return inb(iobase + MCD_STATUS);
842 }
843
844 int
845 mcd_getstat(sc)
846 struct mcd_softc *sc;
847 {
848 struct mcd_mbox mbx;
849
850 mbx.cmd.opcode = MCD_CMDGETSTAT;
851 mbx.cmd.length = 0;
852 mbx.res.length = 0;
853 return mcd_send(sc, &mbx, 1);
854 }
855
856 int
857 mcd_getresult(sc, res)
858 struct mcd_softc *sc;
859 struct mcd_result *res;
860 {
861 int i, x;
862
863 if (sc->debug)
864 printf("%s: mcd_getresult: %d", sc->sc_dev.dv_xname,
865 res->length);
866
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 sc->status = x;
877 mcd_setflags(sc);
878
879 if ((sc->status & MCD_ST_CMDCHECK) != 0)
880 return EINVAL;
881
882 for (i = 0; i < res->length; i++) {
883 if ((x = mcd_getreply(sc)) < 0) {
884 if (sc->debug)
885 printf(" timeout\n");
886 else
887 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
888 return EIO;
889 }
890 if (sc->debug)
891 printf(" %02x", (u_int)x);
892 res->data.raw.data[i] = x;
893 }
894
895 if (sc->debug)
896 printf(" succeeded\n");
897
898 #ifdef MCDDEBUG
899 delay(10);
900 while ((inb(sc->iobase + MCD_XFER) & MCD_XF_STATUSUNAVAIL) == 0) {
901 x = inb(sc->iobase + MCD_STATUS);
902 printf("%s: got extra byte %02x during getstatus\n",
903 sc->sc_dev.dv_xname, (u_int)x);
904 delay(10);
905 }
906 #endif
907
908 return 0;
909 }
910
911 void
912 mcd_setflags(sc)
913 struct mcd_softc *sc;
914 {
915
916 /* Check flags. */
917 if ((sc->flags & MCDF_LOADED) != 0 &&
918 (sc->status & (MCD_ST_DSKCHNG | MCD_ST_DSKIN | MCD_ST_DOOROPEN)) !=
919 MCD_ST_DSKIN) {
920 if ((sc->status & MCD_ST_DOOROPEN) != 0)
921 printf("%s: door open\n", sc->sc_dev.dv_xname);
922 else if ((sc->status & MCD_ST_DSKIN) == 0)
923 printf("%s: no disk present\n", sc->sc_dev.dv_xname);
924 else if ((sc->status & MCD_ST_DSKCHNG) != 0)
925 printf("%s: media change\n", sc->sc_dev.dv_xname);
926 sc->flags &= ~MCDF_LOADED;
927 }
928
929 if ((sc->status & MCD_ST_AUDIOBSY) != 0)
930 sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
931 else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS ||
932 sc->audio_status == CD_AS_AUDIO_INVALID)
933 sc->audio_status = CD_AS_PLAY_COMPLETED;
934 }
935
936 int
937 mcd_send(sc, mbx, diskin)
938 struct mcd_softc *sc;
939 struct mcd_mbox *mbx;
940 int diskin;
941 {
942 int iobase = sc->iobase;
943 int retry, i, error;
944
945 if (sc->debug) {
946 printf("%s: mcd_send: %d %02x", sc->sc_dev.dv_xname,
947 mbx->cmd.length, (u_int)mbx->cmd.opcode);
948 for (i = 0; i < mbx->cmd.length; i++)
949 printf(" %02x", (u_int)mbx->cmd.data.raw.data[i]);
950 printf("\n");
951 }
952
953 for (retry = MCD_RETRIES; retry; retry--) {
954 outb(iobase + MCD_COMMAND, mbx->cmd.opcode);
955 for (i = 0; i < mbx->cmd.length; i++)
956 outb(iobase + MCD_COMMAND, mbx->cmd.data.raw.data[i]);
957 if ((error = mcd_getresult(sc, &mbx->res)) == 0)
958 break;
959 if (error == EINVAL)
960 return error;
961 }
962 if (!retry)
963 return error;
964 if (diskin && (sc->flags & MCDF_LOADED) == 0)
965 return EIO;
966
967 return 0;
968 }
969
970 static int
971 bcd2bin(b)
972 bcd_t b;
973 {
974
975 return (b >> 4) * 10 + (b & 15);
976 }
977
978 static bcd_t
979 bin2bcd(b)
980 int b;
981 {
982
983 return ((b / 10) << 4) | (b % 10);
984 }
985
986 static void
987 hsg2msf(hsg, msf)
988 int hsg;
989 bcd_t *msf;
990 {
991
992 hsg += 150;
993 F_msf(msf) = bin2bcd(hsg % 75);
994 hsg /= 75;
995 S_msf(msf) = bin2bcd(hsg % 60);
996 hsg /= 60;
997 M_msf(msf) = bin2bcd(hsg);
998 }
999
1000 static daddr_t
1001 msf2hsg(msf, relative)
1002 bcd_t *msf;
1003 int relative;
1004 {
1005 daddr_t blkno;
1006
1007 blkno = bcd2bin(M_msf(msf)) * 75 * 60 +
1008 bcd2bin(S_msf(msf)) * 75 +
1009 bcd2bin(F_msf(msf));
1010 if (!relative)
1011 blkno -= 150;
1012 return blkno;
1013 }
1014
1015 void
1016 mcd_pseudointr(v)
1017 void *v;
1018 {
1019 struct mcd_softc *sc = v;
1020 int s;
1021
1022 s = splbio();
1023 (void) mcdintr(sc);
1024 splx(s);
1025 }
1026
1027 /*
1028 * State machine to process read requests.
1029 * Initialize with MCD_S_BEGIN: calculate sizes, and set mode
1030 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
1031 * MCD_S_WAITREAD: wait for read ready, read data.
1032 */
1033 int
1034 mcdintr(arg)
1035 void *arg;
1036 {
1037 struct mcd_softc *sc = arg;
1038 struct mcd_mbx *mbx = &sc->mbx;
1039 int iobase = sc->iobase;
1040 struct buf *bp = mbx->bp;
1041
1042 int i;
1043 u_char x;
1044 bcd_t msf[3];
1045
1046 switch (mbx->state) {
1047 case MCD_S_IDLE:
1048 return 0;
1049
1050 case MCD_S_BEGIN:
1051 tryagain:
1052 if (mbx->mode == sc->lastmode)
1053 goto firstblock;
1054
1055 sc->lastmode = MCD_MD_UNKNOWN;
1056 outb(iobase + MCD_COMMAND, MCD_CMDSETMODE);
1057 outb(iobase + MCD_COMMAND, mbx->mode);
1058
1059 mbx->count = RDELAY_WAITMODE;
1060 mbx->state = MCD_S_WAITMODE;
1061
1062 case MCD_S_WAITMODE:
1063 untimeout(mcd_pseudointr, sc);
1064 for (i = 20; i; i--) {
1065 x = inb(iobase + MCD_XFER);
1066 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1067 break;
1068 delay(50);
1069 }
1070 if (i == 0)
1071 goto hold;
1072 sc->status = inb(iobase + MCD_STATUS);
1073 mcd_setflags(sc);
1074 if ((sc->flags & MCDF_LOADED) == 0)
1075 goto changed;
1076 MCD_TRACE("doread: got WAITMODE delay=%d\n",
1077 RDELAY_WAITMODE - mbx->count, 0, 0, 0);
1078
1079 sc->lastmode = mbx->mode;
1080
1081 firstblock:
1082 MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", mbx->blkno,
1083 bp, 0, 0);
1084
1085 /* Build parameter block. */
1086 hsg2msf(mbx->blkno, msf);
1087
1088 /* Send the read command. */
1089 outb(iobase + MCD_COMMAND, sc->readcmd);
1090 outb(iobase + MCD_COMMAND, msf[0]);
1091 outb(iobase + MCD_COMMAND, msf[1]);
1092 outb(iobase + MCD_COMMAND, msf[2]);
1093 outb(iobase + MCD_COMMAND, 0);
1094 outb(iobase + MCD_COMMAND, 0);
1095 outb(iobase + MCD_COMMAND, mbx->nblk);
1096
1097 mbx->count = RDELAY_WAITREAD;
1098 mbx->state = MCD_S_WAITREAD;
1099
1100 case MCD_S_WAITREAD:
1101 untimeout(mcd_pseudointr, sc);
1102 nextblock:
1103 loop:
1104 for (i = 20; i; i--) {
1105 x = inb(iobase + MCD_XFER);
1106 if ((x & MCD_XF_DATAUNAVAIL) == 0)
1107 goto gotblock;
1108 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1109 break;
1110 delay(50);
1111 }
1112 if (i == 0)
1113 goto hold;
1114 sc->status = inb(iobase + MCD_STATUS);
1115 mcd_setflags(sc);
1116 if ((sc->flags & MCDF_LOADED) == 0)
1117 goto changed;
1118 #if 0
1119 printf("%s: got status byte %02x during read\n",
1120 sc->sc_dev.dv_xname, (u_int)sc->status);
1121 #endif
1122 goto loop;
1123
1124 gotblock:
1125 MCD_TRACE("doread: got data delay=%d\n",
1126 RDELAY_WAITREAD - mbx->count, 0, 0, 0);
1127
1128 /* Data is ready. */
1129 outb(iobase + MCD_CTL2, 0x04); /* XXX */
1130 insb(iobase + MCD_RDATA, bp->b_data + mbx->skip, mbx->sz);
1131 outb(iobase + MCD_CTL2, 0x0c); /* XXX */
1132 mbx->blkno += 1;
1133 mbx->skip += mbx->sz;
1134 if (--mbx->nblk > 0)
1135 goto nextblock;
1136
1137 mbx->state = MCD_S_IDLE;
1138
1139 /* Return buffer. */
1140 bp->b_resid = 0;
1141 disk_unbusy(&sc->sc_dk, bp->b_bcount);
1142 biodone(bp);
1143
1144 mcdstart(sc);
1145 return 1;
1146
1147 hold:
1148 if (mbx->count-- < 0) {
1149 printf("%s: timeout in state %d",
1150 sc->sc_dev.dv_xname, mbx->state);
1151 goto readerr;
1152 }
1153
1154 #if 0
1155 printf("%s: sleep in state %d\n", sc->sc_dev.dv_xname,
1156 mbx->state);
1157 #endif
1158 timeout(mcd_pseudointr, sc, hz / 100);
1159 return -1;
1160 }
1161
1162 readerr:
1163 if (mbx->retry-- > 0) {
1164 printf("; retrying\n");
1165 goto tryagain;
1166 } else
1167 printf("; giving up\n");
1168
1169 changed:
1170 /* Invalidate the buffer. */
1171 bp->b_flags |= B_ERROR;
1172 bp->b_resid = bp->b_bcount - mbx->skip;
1173 disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid));
1174 biodone(bp);
1175
1176 mcdstart(sc);
1177 return -1;
1178
1179 #ifdef notyet
1180 printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname);
1181 outb(mbx->iobase + MCD_RESET, MCD_CMDRESET);
1182 delay(300000);
1183 (void) mcd_getstat(sc, 1);
1184 (void) mcd_getstat(sc, 1);
1185 /*sc->status &= ~MCD_ST_DSKCHNG; */
1186 sc->debug = 1; /* preventive set debug mode */
1187 #endif
1188 }
1189
1190 void
1191 mcd_soft_reset(sc)
1192 struct mcd_softc *sc;
1193 {
1194
1195 sc->debug = 0;
1196 sc->flags = 0;
1197 sc->lastmode = MCD_MD_UNKNOWN;
1198 sc->lastupc = MCD_UPC_UNKNOWN;
1199 sc->audio_status = CD_AS_AUDIO_INVALID;
1200 outb(sc->iobase + MCD_CTL2, 0x0c); /* XXX */
1201 }
1202
1203 int
1204 mcd_hard_reset(sc)
1205 struct mcd_softc *sc;
1206 {
1207 struct mcd_mbox mbx;
1208
1209 mcd_soft_reset(sc);
1210
1211 mbx.cmd.opcode = MCD_CMDRESET;
1212 mbx.cmd.length = 0;
1213 mbx.res.length = 0;
1214 return mcd_send(sc, &mbx, 0);
1215 }
1216
1217 int
1218 mcd_setmode(sc, mode)
1219 struct mcd_softc *sc;
1220 int mode;
1221 {
1222 struct mcd_mbox mbx;
1223 int error;
1224
1225 if (sc->lastmode == mode)
1226 return 0;
1227 if (sc->debug)
1228 printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode);
1229 sc->lastmode = MCD_MD_UNKNOWN;
1230
1231 mbx.cmd.opcode = MCD_CMDSETMODE;
1232 mbx.cmd.length = sizeof(mbx.cmd.data.datamode);
1233 mbx.cmd.data.datamode.mode = mode;
1234 mbx.res.length = 0;
1235 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1236 return error;
1237
1238 sc->lastmode = mode;
1239 return 0;
1240 }
1241
1242 int
1243 mcd_setupc(sc, upc)
1244 struct mcd_softc *sc;
1245 int upc;
1246 {
1247 struct mcd_mbox mbx;
1248 int error;
1249
1250 if (sc->lastupc == upc)
1251 return 0;
1252 if (sc->debug)
1253 printf("%s: setting upc to %d\n", sc->sc_dev.dv_xname, upc);
1254 sc->lastupc = MCD_UPC_UNKNOWN;
1255
1256 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
1257 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
1258 mbx.cmd.data.config.subcommand = MCD_CF_READUPC;
1259 mbx.cmd.data.config.data1 = upc;
1260 mbx.res.length = 0;
1261 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1262 return error;
1263
1264 sc->lastupc = upc;
1265 return 0;
1266 }
1267
1268 int
1269 mcd_toc_header(sc, th)
1270 struct mcd_softc *sc;
1271 struct ioc_toc_header *th;
1272 {
1273
1274 if (sc->debug)
1275 printf("%s: mcd_toc_header: reading toc header\n",
1276 sc->sc_dev.dv_xname);
1277
1278 th->len = msf2hsg(sc->volinfo.vol_msf, 0);
1279 th->starting_track = bcd2bin(sc->volinfo.trk_low);
1280 th->ending_track = bcd2bin(sc->volinfo.trk_high);
1281
1282 return 0;
1283 }
1284
1285 int
1286 mcd_read_toc(sc)
1287 struct mcd_softc *sc;
1288 {
1289 struct ioc_toc_header th;
1290 union mcd_qchninfo q;
1291 int error, trk, idx, retry;
1292
1293 if ((error = mcd_toc_header(sc, &th)) != 0)
1294 return error;
1295
1296 if ((error = mcd_stop(sc)) != 0)
1297 return error;
1298
1299 if (sc->debug)
1300 printf("%s: read_toc: reading qchannel info\n",
1301 sc->sc_dev.dv_xname);
1302
1303 for (trk = th.starting_track; trk <= th.ending_track; trk++)
1304 sc->toc[trk].toc.idx_no = 0x00;
1305 trk = th.ending_track - th.starting_track + 1;
1306 for (retry = 300; retry && trk > 0; retry--) {
1307 if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0)
1308 break;
1309 if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00)
1310 continue;
1311 idx = bcd2bin(q.toc.idx_no);
1312 if (idx < MCD_MAXTOCS &&
1313 sc->toc[idx].toc.idx_no == 0x00) {
1314 sc->toc[idx] = q;
1315 trk--;
1316 }
1317 }
1318
1319 /* Inform the drive that we're finished so it turns off the light. */
1320 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1321 return error;
1322
1323 if (trk != 0)
1324 return EINVAL;
1325
1326 /* Add a fake last+1 for mcd_playtracks(). */
1327 idx = th.ending_track + 1;
1328 sc->toc[idx].toc.control = sc->toc[idx-1].toc.control;
1329 sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type;
1330 sc->toc[idx].toc.trk_no = 0x00;
1331 sc->toc[idx].toc.idx_no = 0xaa;
1332 sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0];
1333 sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1];
1334 sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2];
1335
1336 return 0;
1337 }
1338
1339 int
1340 mcd_toc_entries(sc, te)
1341 struct mcd_softc *sc;
1342 struct ioc_read_toc_entry *te;
1343 {
1344 int len = te->data_len;
1345 struct ret_toc {
1346 struct ioc_toc_header header;
1347 struct cd_toc_entry entries[MCD_MAXTOCS];
1348 } data;
1349 u_char trk;
1350 daddr_t lba;
1351 int error, n;
1352
1353 if (len > sizeof(data.entries) ||
1354 len < sizeof(struct cd_toc_entry))
1355 return EINVAL;
1356 if (te->address_format != CD_MSF_FORMAT &&
1357 te->address_format != CD_LBA_FORMAT)
1358 return EINVAL;
1359
1360 /* Copy the TOC header. */
1361 if ((error = mcd_toc_header(sc, &data.header)) != 0)
1362 return error;
1363
1364 /* Verify starting track. */
1365 trk = te->starting_track;
1366 if (trk == 0x00)
1367 trk = data.header.starting_track;
1368 else if (trk == 0xaa)
1369 trk = data.header.ending_track + 1;
1370 else if (trk < data.header.starting_track ||
1371 trk > data.header.ending_track + 1)
1372 return EINVAL;
1373
1374 /* Copy the TOC data. */
1375 for (n = 0; trk <= data.header.ending_track + 1; trk++) {
1376 if (sc->toc[trk].toc.idx_no == 0x00)
1377 continue;
1378 data.entries[n].control = sc->toc[trk].toc.control;
1379 data.entries[n].addr_type = sc->toc[trk].toc.addr_type;
1380 data.entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no);
1381 switch (te->address_format) {
1382 case CD_MSF_FORMAT:
1383 data.entries[n].addr[0] = 0;
1384 data.entries[n].addr[1] = bcd2bin(sc->toc[trk].toc.absolute_pos[0]);
1385 data.entries[n].addr[2] = bcd2bin(sc->toc[trk].toc.absolute_pos[1]);
1386 data.entries[n].addr[3] = bcd2bin(sc->toc[trk].toc.absolute_pos[2]);
1387 break;
1388 case CD_LBA_FORMAT:
1389 lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0);
1390 data.entries[n].addr[0] = lba >> 24;
1391 data.entries[n].addr[1] = lba >> 16;
1392 data.entries[n].addr[2] = lba >> 8;
1393 data.entries[n].addr[3] = lba;
1394 break;
1395 }
1396 n++;
1397 }
1398
1399 len = min(len, n * sizeof(struct cd_toc_entry));
1400
1401 /* Copy the data back. */
1402 return copyout(&data.entries[0], te->data, len);
1403 }
1404
1405 int
1406 mcd_stop(sc)
1407 struct mcd_softc *sc;
1408 {
1409 struct mcd_mbox mbx;
1410 int error;
1411
1412 if (sc->debug)
1413 printf("%s: mcd_stop: stopping play\n", sc->sc_dev.dv_xname);
1414
1415 mbx.cmd.opcode = MCD_CMDSTOPAUDIO;
1416 mbx.cmd.length = 0;
1417 mbx.res.length = 0;
1418 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1419 return error;
1420
1421 sc->audio_status = CD_AS_PLAY_COMPLETED;
1422 return 0;
1423 }
1424
1425 int
1426 mcd_getqchan(sc, q, qchn)
1427 struct mcd_softc *sc;
1428 union mcd_qchninfo *q;
1429 int qchn;
1430 {
1431 struct mcd_mbox mbx;
1432 int error;
1433
1434 if (qchn == CD_TRACK_INFO) {
1435 if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0)
1436 return error;
1437 } else {
1438 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1439 return error;
1440 }
1441 if (qchn == CD_MEDIA_CATALOG) {
1442 if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0)
1443 return error;
1444 } else {
1445 if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0)
1446 return error;
1447 }
1448
1449 mbx.cmd.opcode = MCD_CMDGETQCHN;
1450 mbx.cmd.length = 0;
1451 mbx.res.length = sizeof(mbx.res.data.qchninfo);
1452 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1453 return error;
1454
1455 *q = mbx.res.data.qchninfo;
1456 return 0;
1457 }
1458
1459 int
1460 mcd_read_subchannel(sc, ch)
1461 struct mcd_softc *sc;
1462 struct ioc_read_subchannel *ch;
1463 {
1464 int len = ch->data_len;
1465 union mcd_qchninfo q;
1466 struct cd_sub_channel_info data;
1467 daddr_t lba;
1468 int error;
1469
1470 if (sc->debug)
1471 printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname,
1472 ch->address_format, ch->data_format);
1473
1474 if (len > sizeof(data) ||
1475 len < sizeof(struct cd_sub_channel_header))
1476 return EINVAL;
1477 if (ch->address_format != CD_MSF_FORMAT &&
1478 ch->address_format != CD_LBA_FORMAT)
1479 return EINVAL;
1480 if (ch->data_format != CD_CURRENT_POSITION &&
1481 ch->data_format != CD_MEDIA_CATALOG)
1482 return EINVAL;
1483
1484 if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0)
1485 return error;
1486
1487 data.header.audio_status = sc->audio_status;
1488 data.what.media_catalog.data_format = ch->data_format;
1489
1490 switch (ch->data_format) {
1491 case CD_MEDIA_CATALOG:
1492 data.what.media_catalog.mc_valid = 1;
1493 #if 0
1494 data.what.media_catalog.mc_number =
1495 #endif
1496 break;
1497
1498 case CD_CURRENT_POSITION:
1499 data.what.position.track_number = bcd2bin(q.current.trk_no);
1500 data.what.position.index_number = bcd2bin(q.current.idx_no);
1501 switch (ch->address_format) {
1502 case CD_MSF_FORMAT:
1503 data.what.position.reladdr[0] = 0;
1504 data.what.position.reladdr[1] = bcd2bin(q.current.relative_pos[0]);
1505 data.what.position.reladdr[2] = bcd2bin(q.current.relative_pos[1]);
1506 data.what.position.reladdr[3] = bcd2bin(q.current.relative_pos[2]);
1507 data.what.position.absaddr[0] = 0;
1508 data.what.position.absaddr[1] = bcd2bin(q.current.absolute_pos[0]);
1509 data.what.position.absaddr[2] = bcd2bin(q.current.absolute_pos[1]);
1510 data.what.position.absaddr[3] = bcd2bin(q.current.absolute_pos[2]);
1511 break;
1512 case CD_LBA_FORMAT:
1513 lba = msf2hsg(q.current.relative_pos, 1);
1514 /*
1515 * Pre-gap has index number of 0, and decreasing MSF
1516 * address. Must be converted to negative LBA, per
1517 * SCSI spec.
1518 */
1519 if (data.what.position.index_number == 0x00)
1520 lba = -lba;
1521 data.what.position.reladdr[0] = lba >> 24;
1522 data.what.position.reladdr[1] = lba >> 16;
1523 data.what.position.reladdr[2] = lba >> 8;
1524 data.what.position.reladdr[3] = lba;
1525 lba = msf2hsg(q.current.absolute_pos, 0);
1526 data.what.position.absaddr[0] = lba >> 24;
1527 data.what.position.absaddr[1] = lba >> 16;
1528 data.what.position.absaddr[2] = lba >> 8;
1529 data.what.position.absaddr[3] = lba;
1530 break;
1531 }
1532 break;
1533 }
1534
1535 return copyout(&data, ch->data, len);
1536 }
1537
1538 int
1539 mcd_playtracks(sc, p)
1540 struct mcd_softc *sc;
1541 struct ioc_play_track *p;
1542 {
1543 struct mcd_mbox mbx;
1544 int a = p->start_track;
1545 int z = p->end_track;
1546 int error;
1547
1548 if (sc->debug)
1549 printf("%s: playtracks: from %d:%d to %d:%d\n",
1550 sc->sc_dev.dv_xname,
1551 a, p->start_index, z, p->end_index);
1552
1553 if (a < bcd2bin(sc->volinfo.trk_low) ||
1554 a > bcd2bin(sc->volinfo.trk_high) ||
1555 a > z ||
1556 z < bcd2bin(sc->volinfo.trk_low) ||
1557 z > bcd2bin(sc->volinfo.trk_high))
1558 return EINVAL;
1559
1560 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1561 return error;
1562
1563 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1564 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1565 mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0];
1566 mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1];
1567 mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2];
1568 mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0];
1569 mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1];
1570 mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2];
1571 sc->lastpb = mbx.cmd;
1572 mbx.res.length = 0;
1573 return mcd_send(sc, &mbx, 1);
1574 }
1575
1576 int
1577 mcd_playmsf(sc, p)
1578 struct mcd_softc *sc;
1579 struct ioc_play_msf *p;
1580 {
1581 struct mcd_mbox mbx;
1582 int error;
1583
1584 if (sc->debug)
1585 printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n",
1586 sc->sc_dev.dv_xname,
1587 p->start_m, p->start_s, p->start_f,
1588 p->end_m, p->end_s, p->end_f);
1589
1590 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
1591 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f))
1592 return EINVAL;
1593
1594 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1595 return error;
1596
1597 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1598 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1599 mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m);
1600 mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s);
1601 mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f);
1602 mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m);
1603 mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s);
1604 mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f);
1605 sc->lastpb = mbx.cmd;
1606 mbx.res.length = 0;
1607 return mcd_send(sc, &mbx, 1);
1608 }
1609
1610 int
1611 mcd_playblocks(sc, p)
1612 struct mcd_softc *sc;
1613 struct ioc_play_blocks *p;
1614 {
1615 struct mcd_mbox mbx;
1616 int error;
1617
1618 if (sc->debug)
1619 printf("%s: playblocks: blkno %d length %d\n",
1620 sc->sc_dev.dv_xname, p->blk, p->len);
1621
1622 if (p->blk > sc->disksize || p->len > sc->disksize ||
1623 (p->blk + p->len) > sc->disksize)
1624 return 0;
1625
1626 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1627 return error;
1628
1629 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1630 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1631 hsg2msf(p->blk, mbx.cmd.data.play.start_msf);
1632 hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf);
1633 sc->lastpb = mbx.cmd;
1634 mbx.res.length = 0;
1635 return mcd_send(sc, &mbx, 1);
1636 }
1637
1638 int
1639 mcd_pause(sc)
1640 struct mcd_softc *sc;
1641 {
1642 union mcd_qchninfo q;
1643 int error;
1644
1645 /* Verify current status. */
1646 if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS) {
1647 printf("%s: pause: attempted when not playing\n",
1648 sc->sc_dev.dv_xname);
1649 return EINVAL;
1650 }
1651
1652 /* Get the current position. */
1653 if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0)
1654 return error;
1655
1656 /* Copy it into lastpb. */
1657 sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0];
1658 sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1];
1659 sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2];
1660
1661 /* Stop playing. */
1662 if ((error = mcd_stop(sc)) != 0)
1663 return error;
1664
1665 /* Set the proper status and exit. */
1666 sc->audio_status = CD_AS_PLAY_PAUSED;
1667 return 0;
1668 }
1669
1670 int
1671 mcd_resume(sc)
1672 struct mcd_softc *sc;
1673 {
1674 struct mcd_mbox mbx;
1675 int error;
1676
1677 if (sc->audio_status != CD_AS_PLAY_PAUSED)
1678 return EINVAL;
1679
1680 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1681 return error;
1682
1683 mbx.cmd = sc->lastpb;
1684 mbx.res.length = 0;
1685 return mcd_send(sc, &mbx, 1);
1686 }
1687
1688 int
1689 mcd_eject(sc)
1690 struct mcd_softc *sc;
1691 {
1692 struct mcd_mbox mbx;
1693
1694 mbx.cmd.opcode = MCD_CMDEJECTDISK;
1695 mbx.cmd.length = 0;
1696 mbx.res.length = 0;
1697 return mcd_send(sc, &mbx, 0);
1698 }
1699
1700 int
1701 mcd_setlock(sc, mode)
1702 struct mcd_softc *sc;
1703 int mode;
1704 {
1705 struct mcd_mbox mbx;
1706
1707 mbx.cmd.opcode = MCD_CMDSETLOCK;
1708 mbx.cmd.length = sizeof(mbx.cmd.data.lockmode);
1709 mbx.cmd.data.lockmode.mode = mode;
1710 mbx.res.length = 0;
1711 return mcd_send(sc, &mbx, 1);
1712 }
1713