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