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