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