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