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