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