mcd.c revision 1.63 1 /* $NetBSD: mcd.c,v 1.63 1999/02/08 16:33:17 bouyer 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 int part;
594
595 MCD_TRACE("ioctl: cmd=0x%x\n", cmd, 0, 0, 0);
596
597 if ((sc->flags & MCDF_LOADED) == 0)
598 return EIO;
599
600 part = MCDPART(dev);
601 switch (cmd) {
602 case DIOCGDINFO:
603 *(struct disklabel *)addr = *(sc->sc_dk.dk_label);
604 return 0;
605
606 case DIOCGPART:
607 ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
608 ((struct partinfo *)addr)->part =
609 &sc->sc_dk.dk_label->d_partitions[part];
610 return 0;
611
612 case DIOCWDINFO:
613 case DIOCSDINFO:
614 if ((flag & FWRITE) == 0)
615 return EBADF;
616
617 if ((error = mcdlock(sc)) != 0)
618 return error;
619 sc->flags |= MCDF_LABELLING;
620
621 error = setdisklabel(sc->sc_dk.dk_label,
622 (struct disklabel *)addr, /*sc->sc_dk.dk_openmask : */0,
623 sc->sc_dk.dk_cpulabel);
624 if (error == 0) {
625 }
626
627 sc->flags &= ~MCDF_LABELLING;
628 mcdunlock(sc);
629 return error;
630
631 case DIOCWLABEL:
632 return EBADF;
633
634 case DIOCGDEFLABEL:
635 mcdgetdefaultlabel(sc, (struct disklabel *)addr);
636 return 0;
637
638 case CDIOCPLAYTRACKS:
639 return mcd_playtracks(sc, (struct ioc_play_track *)addr);
640 case CDIOCPLAYMSF:
641 return mcd_playmsf(sc, (struct ioc_play_msf *)addr);
642 case CDIOCPLAYBLOCKS:
643 return mcd_playblocks(sc, (struct ioc_play_blocks *)addr);
644 case CDIOCREADSUBCHANNEL:
645 return mcd_read_subchannel(sc, (struct ioc_read_subchannel *)addr);
646 case CDIOREADTOCHEADER:
647 return mcd_toc_header(sc, (struct ioc_toc_header *)addr);
648 case CDIOREADTOCENTRYS:
649 return mcd_toc_entries(sc, (struct ioc_read_toc_entry *)addr);
650 case CDIOCSETPATCH:
651 case CDIOCGETVOL:
652 case CDIOCSETVOL:
653 case CDIOCSETMONO:
654 case CDIOCSETSTEREO:
655 case CDIOCSETMUTE:
656 case CDIOCSETLEFT:
657 case CDIOCSETRIGHT:
658 return EINVAL;
659 case CDIOCRESUME:
660 return mcd_resume(sc);
661 case CDIOCPAUSE:
662 return mcd_pause(sc);
663 case CDIOCSTART:
664 return EINVAL;
665 case CDIOCSTOP:
666 return mcd_stop(sc);
667 case DIOCEJECT:
668 if (*(int *)addr == 0) {
669 /*
670 * Don't force eject: check that we are the only
671 * partition open. If so, unlock it.
672 */
673 if ((sc->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
674 sc->sc_dk.dk_bopenmask + sc->sc_dk.dk_copenmask ==
675 sc->sc_dk.dk_openmask) {
676 error = mcd_setlock(sc, MCD_LK_UNLOCK);
677 if (error)
678 return (error);
679 } else {
680 return (EBUSY);
681 }
682 }
683 /* FALLTHROUGH */
684 case CDIOCEJECT: /* FALLTHROUGH */
685 case ODIOCEJECT:
686 return mcd_eject(sc);
687 case CDIOCALLOW:
688 return mcd_setlock(sc, MCD_LK_UNLOCK);
689 case CDIOCPREVENT:
690 return mcd_setlock(sc, MCD_LK_LOCK);
691 case DIOCLOCK:
692 return mcd_setlock(sc,
693 (*(int *)addr) ? MCD_LK_LOCK : MCD_LK_UNLOCK);
694 case CDIOCSETDEBUG:
695 sc->debug = 1;
696 return 0;
697 case CDIOCCLRDEBUG:
698 sc->debug = 0;
699 return 0;
700 case CDIOCRESET:
701 return mcd_hard_reset(sc);
702
703 default:
704 return ENOTTY;
705 }
706
707 #ifdef DIAGNOSTIC
708 panic("mcdioctl: impossible");
709 #endif
710 }
711
712 void
713 mcdgetdefaultlabel(sc, lp)
714 struct mcd_softc *sc;
715 struct disklabel *lp;
716 {
717
718 bzero(lp, sizeof(struct disklabel));
719
720 lp->d_secsize = sc->blksize;
721 lp->d_ntracks = 1;
722 lp->d_nsectors = 100;
723 lp->d_ncylinders = (sc->disksize / 100) + 1;
724 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
725
726 strncpy(lp->d_typename, "Mitsumi CD-ROM", 16);
727 lp->d_type = 0; /* XXX */
728 strncpy(lp->d_packname, "fictitious", 16);
729 lp->d_secperunit = sc->disksize;
730 lp->d_rpm = 300;
731 lp->d_interleave = 1;
732 lp->d_flags = D_REMOVABLE;
733
734 lp->d_partitions[0].p_offset = 0;
735 lp->d_partitions[0].p_size =
736 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
737 lp->d_partitions[0].p_fstype = FS_ISO9660;
738 lp->d_partitions[RAW_PART].p_offset = 0;
739 lp->d_partitions[RAW_PART].p_size =
740 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
741 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660;
742 lp->d_npartitions = RAW_PART + 1;
743
744 lp->d_magic = DISKMAGIC;
745 lp->d_magic2 = DISKMAGIC;
746 lp->d_checksum = dkcksum(lp);
747 }
748
749 /*
750 * This could have been taken from scsi/cd.c, but it is not clear
751 * whether the scsi cd driver is linked in.
752 */
753 void
754 mcdgetdisklabel(sc)
755 struct mcd_softc *sc;
756 {
757 struct disklabel *lp = sc->sc_dk.dk_label;
758
759 bzero(sc->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
760
761 mcdgetdefaultlabel(sc, lp);
762 }
763
764 int
765 mcd_get_parms(sc)
766 struct mcd_softc *sc;
767 {
768 struct mcd_mbox mbx;
769 daddr_t size;
770 int error;
771
772 /* Send volume info command. */
773 mbx.cmd.opcode = MCD_CMDGETVOLINFO;
774 mbx.cmd.length = 0;
775 mbx.res.length = sizeof(mbx.res.data.volinfo);
776 if ((error = mcd_send(sc, &mbx, 1)) != 0)
777 return error;
778
779 if (mbx.res.data.volinfo.trk_low == 0x00 &&
780 mbx.res.data.volinfo.trk_high == 0x00)
781 return EINVAL;
782
783 /* Volinfo is OK. */
784 sc->volinfo = mbx.res.data.volinfo;
785 sc->blksize = MCD_BLKSIZE_COOKED;
786 size = msf2hsg(sc->volinfo.vol_msf, 0);
787 sc->disksize = size * (MCD_BLKSIZE_COOKED / DEV_BSIZE);
788 return 0;
789 }
790
791 int
792 mcdsize(dev)
793 dev_t dev;
794 {
795
796 /* CD-ROMs are read-only. */
797 return -1;
798 }
799
800 int
801 mcddump(dev, blkno, va, size)
802 dev_t dev;
803 daddr_t blkno;
804 caddr_t va;
805 size_t size;
806 {
807
808 /* Not implemented. */
809 return ENXIO;
810 }
811
812 /*
813 * Find the board and fill in the softc.
814 */
815 int
816 mcd_find(iot, ioh, sc)
817 bus_space_tag_t iot;
818 bus_space_handle_t ioh;
819 struct mcd_softc *sc;
820 {
821 int i;
822 struct mcd_mbox mbx;
823
824 sc->sc_iot = iot;
825 sc->sc_ioh = ioh;
826
827 /* Send a reset. */
828 bus_space_write_1(iot, ioh, MCD_RESET, 0);
829 delay(1000000);
830 /* Get any pending status and throw away. */
831 for (i = 10; i; i--)
832 bus_space_read_1(iot, ioh, MCD_STATUS);
833 delay(1000);
834
835 /* Send get status command. */
836 mbx.cmd.opcode = MCD_CMDGETSTAT;
837 mbx.cmd.length = 0;
838 mbx.res.length = 0;
839 if (mcd_send(sc, &mbx, 0) != 0)
840 return 0;
841
842 /* Get info about the drive. */
843 mbx.cmd.opcode = MCD_CMDCONTINFO;
844 mbx.cmd.length = 0;
845 mbx.res.length = sizeof(mbx.res.data.continfo);
846 if (mcd_send(sc, &mbx, 0) != 0)
847 return 0;
848
849 /*
850 * The following is code which is not guaranteed to work for all
851 * drives, because the meaning of the expected 'M' is not clear
852 * (M_itsumi is an obvious assumption, but I don't trust that).
853 * Also, the original hack had a bogus condition that always
854 * returned true.
855 *
856 * Note: Which models support interrupts? >=LU005S?
857 */
858 sc->readcmd = MCD_CMDREADSINGLESPEED;
859 switch (mbx.res.data.continfo.code) {
860 case 'M':
861 if (mbx.res.data.continfo.version <= 2)
862 sc->type = "LU002S";
863 else if (mbx.res.data.continfo.version <= 5)
864 sc->type = "LU005S";
865 else
866 sc->type = "LU006S";
867 break;
868 case 'F':
869 sc->type = "FX001";
870 break;
871 case 'D':
872 sc->type = "FX001D";
873 sc->readcmd = MCD_CMDREADDOUBLESPEED;
874 break;
875 default:
876 /*
877 * mcd_send() says the response looked OK but the
878 * drive type is unknown. If mcd_promisc, match anyway.
879 */
880 if (mcd_promisc != 0)
881 return 0;
882
883 #ifdef MCDDEBUG
884 printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n",
885 sc->sc_dev.dv_xname,
886 mbx.res.data.continfo.code, mbx.res.data.continfo.version);
887 #endif
888 sc->type = 0;
889 break;
890 }
891
892 return 1;
893
894 }
895
896 int
897 mcdprobe(parent, match, aux)
898 struct device *parent;
899 struct cfdata *match;
900 void *aux;
901 {
902 struct isa_attach_args *ia = aux;
903 struct mcd_softc sc;
904 bus_space_tag_t iot = ia->ia_iot;
905 bus_space_handle_t ioh;
906 int rv;
907
908 /* Disallow wildcarded i/o address. */
909 if (ia->ia_iobase == ISACF_PORT_DEFAULT)
910 return (0);
911
912 /* Map i/o space */
913 if (bus_space_map(iot, ia->ia_iobase, MCD_NPORT, 0, &ioh))
914 return 0;
915
916 sc.debug = 0;
917 sc.probe = 1;
918
919 rv = mcd_find(iot, ioh, &sc);
920
921 bus_space_unmap(iot, ioh, MCD_NPORT);
922
923 if (rv) {
924 ia->ia_iosize = MCD_NPORT;
925 ia->ia_msize = 0;
926 }
927
928 return (rv);
929 }
930
931 int
932 mcd_getreply(sc)
933 struct mcd_softc *sc;
934 {
935 bus_space_tag_t iot = sc->sc_iot;
936 bus_space_handle_t ioh = sc->sc_ioh;
937 int i;
938
939 /* Wait until xfer port senses data ready. */
940 for (i = DELAY_GETREPLY; i; i--) {
941 if ((bus_space_read_1(iot, ioh, MCD_XFER) &
942 MCD_XF_STATUSUNAVAIL) == 0)
943 break;
944 delay(DELAY_GRANULARITY);
945 }
946 if (!i)
947 return -1;
948
949 /* Get the data. */
950 return bus_space_read_1(iot, ioh, MCD_STATUS);
951 }
952
953 int
954 mcd_getstat(sc)
955 struct mcd_softc *sc;
956 {
957 struct mcd_mbox mbx;
958
959 mbx.cmd.opcode = MCD_CMDGETSTAT;
960 mbx.cmd.length = 0;
961 mbx.res.length = 0;
962 return mcd_send(sc, &mbx, 1);
963 }
964
965 int
966 mcd_getresult(sc, res)
967 struct mcd_softc *sc;
968 struct mcd_result *res;
969 {
970 int i, x;
971
972 if (sc->debug)
973 printf("%s: mcd_getresult: %d", sc->sc_dev.dv_xname,
974 res->length);
975
976 if ((x = mcd_getreply(sc)) < 0) {
977 if (sc->debug)
978 printf(" timeout\n");
979 else if (!sc->probe)
980 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
981 return EIO;
982 }
983 if (sc->debug)
984 printf(" %02x", (u_int)x);
985 sc->status = x;
986 mcd_setflags(sc);
987
988 if ((sc->status & MCD_ST_CMDCHECK) != 0)
989 return EINVAL;
990
991 for (i = 0; i < res->length; i++) {
992 if ((x = mcd_getreply(sc)) < 0) {
993 if (sc->debug)
994 printf(" timeout\n");
995 else
996 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
997 return EIO;
998 }
999 if (sc->debug)
1000 printf(" %02x", (u_int)x);
1001 res->data.raw.data[i] = x;
1002 }
1003
1004 if (sc->debug)
1005 printf(" succeeded\n");
1006
1007 #ifdef MCDDEBUG
1008 delay(10);
1009 while ((bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_XFER) &
1010 MCD_XF_STATUSUNAVAIL) == 0) {
1011 x = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_STATUS);
1012 printf("%s: got extra byte %02x during getstatus\n",
1013 sc->sc_dev.dv_xname, (u_int)x);
1014 delay(10);
1015 }
1016 #endif
1017
1018 return 0;
1019 }
1020
1021 void
1022 mcd_setflags(sc)
1023 struct mcd_softc *sc;
1024 {
1025
1026 /* Check flags. */
1027 if ((sc->flags & MCDF_LOADED) != 0 &&
1028 (sc->status & (MCD_ST_DSKCHNG | MCD_ST_DSKIN | MCD_ST_DOOROPEN)) !=
1029 MCD_ST_DSKIN) {
1030 if ((sc->status & MCD_ST_DOOROPEN) != 0)
1031 printf("%s: door open\n", sc->sc_dev.dv_xname);
1032 else if ((sc->status & MCD_ST_DSKIN) == 0)
1033 printf("%s: no disk present\n", sc->sc_dev.dv_xname);
1034 else if ((sc->status & MCD_ST_DSKCHNG) != 0)
1035 printf("%s: media change\n", sc->sc_dev.dv_xname);
1036 sc->flags &= ~MCDF_LOADED;
1037 }
1038
1039 if ((sc->status & MCD_ST_AUDIOBSY) != 0)
1040 sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
1041 else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS ||
1042 sc->audio_status == CD_AS_AUDIO_INVALID)
1043 sc->audio_status = CD_AS_PLAY_COMPLETED;
1044 }
1045
1046 int
1047 mcd_send(sc, mbx, diskin)
1048 struct mcd_softc *sc;
1049 struct mcd_mbox *mbx;
1050 int diskin;
1051 {
1052 int retry, i, error;
1053 bus_space_tag_t iot = sc->sc_iot;
1054 bus_space_handle_t ioh = sc->sc_ioh;
1055
1056 if (sc->debug) {
1057 printf("%s: mcd_send: %d %02x", sc->sc_dev.dv_xname,
1058 mbx->cmd.length, (u_int)mbx->cmd.opcode);
1059 for (i = 0; i < mbx->cmd.length; i++)
1060 printf(" %02x", (u_int)mbx->cmd.data.raw.data[i]);
1061 printf("\n");
1062 }
1063
1064 for (retry = MCD_RETRIES; retry; retry--) {
1065 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.opcode);
1066 for (i = 0; i < mbx->cmd.length; i++)
1067 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.data.raw.data[i]);
1068 if ((error = mcd_getresult(sc, &mbx->res)) == 0)
1069 break;
1070 if (error == EINVAL)
1071 return error;
1072 }
1073 if (!retry)
1074 return error;
1075 if (diskin && (sc->flags & MCDF_LOADED) == 0)
1076 return EIO;
1077
1078 return 0;
1079 }
1080
1081 static int
1082 bcd2bin(b)
1083 bcd_t b;
1084 {
1085
1086 return (b >> 4) * 10 + (b & 15);
1087 }
1088
1089 static bcd_t
1090 bin2bcd(b)
1091 int b;
1092 {
1093
1094 return ((b / 10) << 4) | (b % 10);
1095 }
1096
1097 static void
1098 hsg2msf(hsg, msf)
1099 int hsg;
1100 bcd_t *msf;
1101 {
1102
1103 hsg += 150;
1104 F_msf(msf) = bin2bcd(hsg % 75);
1105 hsg /= 75;
1106 S_msf(msf) = bin2bcd(hsg % 60);
1107 hsg /= 60;
1108 M_msf(msf) = bin2bcd(hsg);
1109 }
1110
1111 static daddr_t
1112 msf2hsg(msf, relative)
1113 bcd_t *msf;
1114 int relative;
1115 {
1116 daddr_t blkno;
1117
1118 blkno = bcd2bin(M_msf(msf)) * 75 * 60 +
1119 bcd2bin(S_msf(msf)) * 75 +
1120 bcd2bin(F_msf(msf));
1121 if (!relative)
1122 blkno -= 150;
1123 return blkno;
1124 }
1125
1126 void
1127 mcd_pseudointr(v)
1128 void *v;
1129 {
1130 struct mcd_softc *sc = v;
1131 int s;
1132
1133 s = splbio();
1134 (void) mcdintr(sc);
1135 splx(s);
1136 }
1137
1138 /*
1139 * State machine to process read requests.
1140 * Initialize with MCD_S_BEGIN: calculate sizes, and set mode
1141 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
1142 * MCD_S_WAITREAD: wait for read ready, read data.
1143 */
1144 int
1145 mcdintr(arg)
1146 void *arg;
1147 {
1148 struct mcd_softc *sc = arg;
1149 struct mcd_mbx *mbx = &sc->mbx;
1150 struct buf *bp = mbx->bp;
1151 bus_space_tag_t iot = sc->sc_iot;
1152 bus_space_handle_t ioh = sc->sc_ioh;
1153
1154 int i;
1155 u_char x;
1156 bcd_t msf[3];
1157
1158 switch (mbx->state) {
1159 case MCD_S_IDLE:
1160 return 0;
1161
1162 case MCD_S_BEGIN:
1163 tryagain:
1164 if (mbx->mode == sc->lastmode)
1165 goto firstblock;
1166
1167 sc->lastmode = MCD_MD_UNKNOWN;
1168 bus_space_write_1(iot, ioh, MCD_COMMAND, MCD_CMDSETMODE);
1169 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->mode);
1170
1171 mbx->count = RDELAY_WAITMODE;
1172 mbx->state = MCD_S_WAITMODE;
1173
1174 case MCD_S_WAITMODE:
1175 untimeout(mcd_pseudointr, sc);
1176 for (i = 20; i; i--) {
1177 x = bus_space_read_1(iot, ioh, MCD_XFER);
1178 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1179 break;
1180 delay(50);
1181 }
1182 if (i == 0)
1183 goto hold;
1184 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS);
1185 mcd_setflags(sc);
1186 if ((sc->flags & MCDF_LOADED) == 0)
1187 goto changed;
1188 MCD_TRACE("doread: got WAITMODE delay=%d\n",
1189 RDELAY_WAITMODE - mbx->count, 0, 0, 0);
1190
1191 sc->lastmode = mbx->mode;
1192
1193 firstblock:
1194 MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", mbx->blkno,
1195 bp, 0, 0);
1196
1197 /* Build parameter block. */
1198 hsg2msf(mbx->blkno, msf);
1199
1200 /* Send the read command. */
1201 bus_space_write_1(iot, ioh, MCD_COMMAND, sc->readcmd);
1202 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[0]);
1203 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[1]);
1204 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[2]);
1205 bus_space_write_1(iot, ioh, MCD_COMMAND, 0);
1206 bus_space_write_1(iot, ioh, MCD_COMMAND, 0);
1207 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->nblk);
1208
1209 mbx->count = RDELAY_WAITREAD;
1210 mbx->state = MCD_S_WAITREAD;
1211
1212 case MCD_S_WAITREAD:
1213 untimeout(mcd_pseudointr, sc);
1214 nextblock:
1215 loop:
1216 for (i = 20; i; i--) {
1217 x = bus_space_read_1(iot, ioh, MCD_XFER);
1218 if ((x & MCD_XF_DATAUNAVAIL) == 0)
1219 goto gotblock;
1220 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1221 break;
1222 delay(50);
1223 }
1224 if (i == 0)
1225 goto hold;
1226 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS);
1227 mcd_setflags(sc);
1228 if ((sc->flags & MCDF_LOADED) == 0)
1229 goto changed;
1230 #if 0
1231 printf("%s: got status byte %02x during read\n",
1232 sc->sc_dev.dv_xname, (u_int)sc->status);
1233 #endif
1234 goto loop;
1235
1236 gotblock:
1237 MCD_TRACE("doread: got data delay=%d\n",
1238 RDELAY_WAITREAD - mbx->count, 0, 0, 0);
1239
1240 /* Data is ready. */
1241 bus_space_write_1(iot, ioh, MCD_CTL2, 0x04); /* XXX */
1242 bus_space_read_multi_1(iot, ioh, MCD_RDATA,
1243 bp->b_data + mbx->skip, mbx->sz);
1244 bus_space_write_1(iot, ioh, MCD_CTL2, 0x0c); /* XXX */
1245 mbx->blkno += 1;
1246 mbx->skip += mbx->sz;
1247 if (--mbx->nblk > 0)
1248 goto nextblock;
1249
1250 mbx->state = MCD_S_IDLE;
1251
1252 /* Return buffer. */
1253 bp->b_resid = 0;
1254 disk_unbusy(&sc->sc_dk, bp->b_bcount);
1255 biodone(bp);
1256
1257 mcdstart(sc);
1258 return 1;
1259
1260 hold:
1261 if (mbx->count-- < 0) {
1262 printf("%s: timeout in state %d",
1263 sc->sc_dev.dv_xname, mbx->state);
1264 goto readerr;
1265 }
1266
1267 #if 0
1268 printf("%s: sleep in state %d\n", sc->sc_dev.dv_xname,
1269 mbx->state);
1270 #endif
1271 timeout(mcd_pseudointr, sc, hz / 100);
1272 return -1;
1273 }
1274
1275 readerr:
1276 if (mbx->retry-- > 0) {
1277 printf("; retrying\n");
1278 goto tryagain;
1279 } else
1280 printf("; giving up\n");
1281
1282 changed:
1283 /* Invalidate the buffer. */
1284 bp->b_flags |= B_ERROR;
1285 bp->b_resid = bp->b_bcount - mbx->skip;
1286 disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid));
1287 biodone(bp);
1288
1289 mcdstart(sc);
1290 return -1;
1291
1292 #ifdef notyet
1293 printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname);
1294 bus_space_write_1(iot, ioh, MCD_RESET, MCD_CMDRESET);
1295 delay(300000);
1296 (void) mcd_getstat(sc, 1);
1297 (void) mcd_getstat(sc, 1);
1298 /*sc->status &= ~MCD_ST_DSKCHNG; */
1299 sc->debug = 1; /* preventive set debug mode */
1300 #endif
1301 }
1302
1303 void
1304 mcd_soft_reset(sc)
1305 struct mcd_softc *sc;
1306 {
1307
1308 sc->debug = 0;
1309 sc->flags = 0;
1310 sc->lastmode = MCD_MD_UNKNOWN;
1311 sc->lastupc = MCD_UPC_UNKNOWN;
1312 sc->audio_status = CD_AS_AUDIO_INVALID;
1313 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MCD_CTL2, 0x0c); /* XXX */
1314 }
1315
1316 int
1317 mcd_hard_reset(sc)
1318 struct mcd_softc *sc;
1319 {
1320 struct mcd_mbox mbx;
1321
1322 mcd_soft_reset(sc);
1323
1324 mbx.cmd.opcode = MCD_CMDRESET;
1325 mbx.cmd.length = 0;
1326 mbx.res.length = 0;
1327 return mcd_send(sc, &mbx, 0);
1328 }
1329
1330 int
1331 mcd_setmode(sc, mode)
1332 struct mcd_softc *sc;
1333 int mode;
1334 {
1335 struct mcd_mbox mbx;
1336 int error;
1337
1338 if (sc->lastmode == mode)
1339 return 0;
1340 if (sc->debug)
1341 printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode);
1342 sc->lastmode = MCD_MD_UNKNOWN;
1343
1344 mbx.cmd.opcode = MCD_CMDSETMODE;
1345 mbx.cmd.length = sizeof(mbx.cmd.data.datamode);
1346 mbx.cmd.data.datamode.mode = mode;
1347 mbx.res.length = 0;
1348 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1349 return error;
1350
1351 sc->lastmode = mode;
1352 return 0;
1353 }
1354
1355 int
1356 mcd_setupc(sc, upc)
1357 struct mcd_softc *sc;
1358 int upc;
1359 {
1360 struct mcd_mbox mbx;
1361 int error;
1362
1363 if (sc->lastupc == upc)
1364 return 0;
1365 if (sc->debug)
1366 printf("%s: setting upc to %d\n", sc->sc_dev.dv_xname, upc);
1367 sc->lastupc = MCD_UPC_UNKNOWN;
1368
1369 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
1370 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
1371 mbx.cmd.data.config.subcommand = MCD_CF_READUPC;
1372 mbx.cmd.data.config.data1 = upc;
1373 mbx.res.length = 0;
1374 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1375 return error;
1376
1377 sc->lastupc = upc;
1378 return 0;
1379 }
1380
1381 int
1382 mcd_toc_header(sc, th)
1383 struct mcd_softc *sc;
1384 struct ioc_toc_header *th;
1385 {
1386
1387 if (sc->debug)
1388 printf("%s: mcd_toc_header: reading toc header\n",
1389 sc->sc_dev.dv_xname);
1390
1391 th->len = msf2hsg(sc->volinfo.vol_msf, 0);
1392 th->starting_track = bcd2bin(sc->volinfo.trk_low);
1393 th->ending_track = bcd2bin(sc->volinfo.trk_high);
1394
1395 return 0;
1396 }
1397
1398 int
1399 mcd_read_toc(sc)
1400 struct mcd_softc *sc;
1401 {
1402 struct ioc_toc_header th;
1403 union mcd_qchninfo q;
1404 int error, trk, idx, retry;
1405
1406 if ((error = mcd_toc_header(sc, &th)) != 0)
1407 return error;
1408
1409 if ((error = mcd_stop(sc)) != 0)
1410 return error;
1411
1412 if (sc->debug)
1413 printf("%s: read_toc: reading qchannel info\n",
1414 sc->sc_dev.dv_xname);
1415
1416 for (trk = th.starting_track; trk <= th.ending_track; trk++)
1417 sc->toc[trk].toc.idx_no = 0x00;
1418 trk = th.ending_track - th.starting_track + 1;
1419 for (retry = 300; retry && trk > 0; retry--) {
1420 if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0)
1421 break;
1422 if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00)
1423 continue;
1424 idx = bcd2bin(q.toc.idx_no);
1425 if (idx < MCD_MAXTOCS &&
1426 sc->toc[idx].toc.idx_no == 0x00) {
1427 sc->toc[idx] = q;
1428 trk--;
1429 }
1430 }
1431
1432 /* Inform the drive that we're finished so it turns off the light. */
1433 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1434 return error;
1435
1436 if (trk != 0)
1437 return EINVAL;
1438
1439 /* Add a fake last+1 for mcd_playtracks(). */
1440 idx = th.ending_track + 1;
1441 sc->toc[idx].toc.control = sc->toc[idx-1].toc.control;
1442 sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type;
1443 sc->toc[idx].toc.trk_no = 0x00;
1444 sc->toc[idx].toc.idx_no = 0xaa;
1445 sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0];
1446 sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1];
1447 sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2];
1448
1449 return 0;
1450 }
1451
1452 int
1453 mcd_toc_entries(sc, te)
1454 struct mcd_softc *sc;
1455 struct ioc_read_toc_entry *te;
1456 {
1457 int len = te->data_len;
1458 struct ret_toc {
1459 struct ioc_toc_header header;
1460 struct cd_toc_entry entries[MCD_MAXTOCS];
1461 } data;
1462 u_char trk;
1463 daddr_t lba;
1464 int error, n;
1465
1466 if (len > sizeof(data.entries) ||
1467 len < sizeof(struct cd_toc_entry))
1468 return EINVAL;
1469 if (te->address_format != CD_MSF_FORMAT &&
1470 te->address_format != CD_LBA_FORMAT)
1471 return EINVAL;
1472
1473 /* Copy the TOC header. */
1474 if ((error = mcd_toc_header(sc, &data.header)) != 0)
1475 return error;
1476
1477 /* Verify starting track. */
1478 trk = te->starting_track;
1479 if (trk == 0x00)
1480 trk = data.header.starting_track;
1481 else if (trk == 0xaa)
1482 trk = data.header.ending_track + 1;
1483 else if (trk < data.header.starting_track ||
1484 trk > data.header.ending_track + 1)
1485 return EINVAL;
1486
1487 /* Copy the TOC data. */
1488 for (n = 0; trk <= data.header.ending_track + 1; trk++) {
1489 if (sc->toc[trk].toc.idx_no == 0x00)
1490 continue;
1491 data.entries[n].control = sc->toc[trk].toc.control;
1492 data.entries[n].addr_type = sc->toc[trk].toc.addr_type;
1493 data.entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no);
1494 switch (te->address_format) {
1495 case CD_MSF_FORMAT:
1496 data.entries[n].addr.addr[0] = 0;
1497 data.entries[n].addr.addr[1] = bcd2bin(sc->toc[trk].toc.absolute_pos[0]);
1498 data.entries[n].addr.addr[2] = bcd2bin(sc->toc[trk].toc.absolute_pos[1]);
1499 data.entries[n].addr.addr[3] = bcd2bin(sc->toc[trk].toc.absolute_pos[2]);
1500 break;
1501 case CD_LBA_FORMAT:
1502 lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0);
1503 data.entries[n].addr.addr[0] = lba >> 24;
1504 data.entries[n].addr.addr[1] = lba >> 16;
1505 data.entries[n].addr.addr[2] = lba >> 8;
1506 data.entries[n].addr.addr[3] = lba;
1507 break;
1508 }
1509 n++;
1510 }
1511
1512 len = min(len, n * sizeof(struct cd_toc_entry));
1513
1514 /* Copy the data back. */
1515 return copyout(&data.entries[0], te->data, len);
1516 }
1517
1518 int
1519 mcd_stop(sc)
1520 struct mcd_softc *sc;
1521 {
1522 struct mcd_mbox mbx;
1523 int error;
1524
1525 if (sc->debug)
1526 printf("%s: mcd_stop: stopping play\n", sc->sc_dev.dv_xname);
1527
1528 mbx.cmd.opcode = MCD_CMDSTOPAUDIO;
1529 mbx.cmd.length = 0;
1530 mbx.res.length = 0;
1531 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1532 return error;
1533
1534 sc->audio_status = CD_AS_PLAY_COMPLETED;
1535 return 0;
1536 }
1537
1538 int
1539 mcd_getqchan(sc, q, qchn)
1540 struct mcd_softc *sc;
1541 union mcd_qchninfo *q;
1542 int qchn;
1543 {
1544 struct mcd_mbox mbx;
1545 int error;
1546
1547 if (qchn == CD_TRACK_INFO) {
1548 if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0)
1549 return error;
1550 } else {
1551 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1552 return error;
1553 }
1554 if (qchn == CD_MEDIA_CATALOG) {
1555 if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0)
1556 return error;
1557 } else {
1558 if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0)
1559 return error;
1560 }
1561
1562 mbx.cmd.opcode = MCD_CMDGETQCHN;
1563 mbx.cmd.length = 0;
1564 mbx.res.length = sizeof(mbx.res.data.qchninfo);
1565 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1566 return error;
1567
1568 *q = mbx.res.data.qchninfo;
1569 return 0;
1570 }
1571
1572 int
1573 mcd_read_subchannel(sc, ch)
1574 struct mcd_softc *sc;
1575 struct ioc_read_subchannel *ch;
1576 {
1577 int len = ch->data_len;
1578 union mcd_qchninfo q;
1579 struct cd_sub_channel_info data;
1580 daddr_t lba;
1581 int error;
1582
1583 if (sc->debug)
1584 printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname,
1585 ch->address_format, ch->data_format);
1586
1587 if (len > sizeof(data) ||
1588 len < sizeof(struct cd_sub_channel_header))
1589 return EINVAL;
1590 if (ch->address_format != CD_MSF_FORMAT &&
1591 ch->address_format != CD_LBA_FORMAT)
1592 return EINVAL;
1593 if (ch->data_format != CD_CURRENT_POSITION &&
1594 ch->data_format != CD_MEDIA_CATALOG)
1595 return EINVAL;
1596
1597 if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0)
1598 return error;
1599
1600 data.header.audio_status = sc->audio_status;
1601 data.what.media_catalog.data_format = ch->data_format;
1602
1603 switch (ch->data_format) {
1604 case CD_MEDIA_CATALOG:
1605 data.what.media_catalog.mc_valid = 1;
1606 #if 0
1607 data.what.media_catalog.mc_number =
1608 #endif
1609 break;
1610
1611 case CD_CURRENT_POSITION:
1612 data.what.position.track_number = bcd2bin(q.current.trk_no);
1613 data.what.position.index_number = bcd2bin(q.current.idx_no);
1614 switch (ch->address_format) {
1615 case CD_MSF_FORMAT:
1616 data.what.position.reladdr.addr[0] = 0;
1617 data.what.position.reladdr.addr[1] = bcd2bin(q.current.relative_pos[0]);
1618 data.what.position.reladdr.addr[2] = bcd2bin(q.current.relative_pos[1]);
1619 data.what.position.reladdr.addr[3] = bcd2bin(q.current.relative_pos[2]);
1620 data.what.position.absaddr.addr[0] = 0;
1621 data.what.position.absaddr.addr[1] = bcd2bin(q.current.absolute_pos[0]);
1622 data.what.position.absaddr.addr[2] = bcd2bin(q.current.absolute_pos[1]);
1623 data.what.position.absaddr.addr[3] = bcd2bin(q.current.absolute_pos[2]);
1624 break;
1625 case CD_LBA_FORMAT:
1626 lba = msf2hsg(q.current.relative_pos, 1);
1627 /*
1628 * Pre-gap has index number of 0, and decreasing MSF
1629 * address. Must be converted to negative LBA, per
1630 * SCSI spec.
1631 */
1632 if (data.what.position.index_number == 0x00)
1633 lba = -lba;
1634 data.what.position.reladdr.addr[0] = lba >> 24;
1635 data.what.position.reladdr.addr[1] = lba >> 16;
1636 data.what.position.reladdr.addr[2] = lba >> 8;
1637 data.what.position.reladdr.addr[3] = lba;
1638 lba = msf2hsg(q.current.absolute_pos, 0);
1639 data.what.position.absaddr.addr[0] = lba >> 24;
1640 data.what.position.absaddr.addr[1] = lba >> 16;
1641 data.what.position.absaddr.addr[2] = lba >> 8;
1642 data.what.position.absaddr.addr[3] = lba;
1643 break;
1644 }
1645 break;
1646 }
1647
1648 return copyout(&data, ch->data, len);
1649 }
1650
1651 int
1652 mcd_playtracks(sc, p)
1653 struct mcd_softc *sc;
1654 struct ioc_play_track *p;
1655 {
1656 struct mcd_mbox mbx;
1657 int a = p->start_track;
1658 int z = p->end_track;
1659 int error;
1660
1661 if (sc->debug)
1662 printf("%s: playtracks: from %d:%d to %d:%d\n",
1663 sc->sc_dev.dv_xname,
1664 a, p->start_index, z, p->end_index);
1665
1666 if (a < bcd2bin(sc->volinfo.trk_low) ||
1667 a > bcd2bin(sc->volinfo.trk_high) ||
1668 a > z ||
1669 z < bcd2bin(sc->volinfo.trk_low) ||
1670 z > bcd2bin(sc->volinfo.trk_high))
1671 return EINVAL;
1672
1673 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1674 return error;
1675
1676 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1677 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1678 mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0];
1679 mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1];
1680 mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2];
1681 mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0];
1682 mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1];
1683 mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2];
1684 sc->lastpb = mbx.cmd;
1685 mbx.res.length = 0;
1686 return mcd_send(sc, &mbx, 1);
1687 }
1688
1689 int
1690 mcd_playmsf(sc, p)
1691 struct mcd_softc *sc;
1692 struct ioc_play_msf *p;
1693 {
1694 struct mcd_mbox mbx;
1695 int error;
1696
1697 if (sc->debug)
1698 printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n",
1699 sc->sc_dev.dv_xname,
1700 p->start_m, p->start_s, p->start_f,
1701 p->end_m, p->end_s, p->end_f);
1702
1703 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
1704 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f))
1705 return EINVAL;
1706
1707 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1708 return error;
1709
1710 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1711 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1712 mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m);
1713 mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s);
1714 mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f);
1715 mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m);
1716 mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s);
1717 mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f);
1718 sc->lastpb = mbx.cmd;
1719 mbx.res.length = 0;
1720 return mcd_send(sc, &mbx, 1);
1721 }
1722
1723 int
1724 mcd_playblocks(sc, p)
1725 struct mcd_softc *sc;
1726 struct ioc_play_blocks *p;
1727 {
1728 struct mcd_mbox mbx;
1729 int error;
1730
1731 if (sc->debug)
1732 printf("%s: playblocks: blkno %d length %d\n",
1733 sc->sc_dev.dv_xname, p->blk, p->len);
1734
1735 if (p->blk > sc->disksize || p->len > sc->disksize ||
1736 (p->blk + p->len) > sc->disksize)
1737 return 0;
1738
1739 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1740 return error;
1741
1742 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1743 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1744 hsg2msf(p->blk, mbx.cmd.data.play.start_msf);
1745 hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf);
1746 sc->lastpb = mbx.cmd;
1747 mbx.res.length = 0;
1748 return mcd_send(sc, &mbx, 1);
1749 }
1750
1751 int
1752 mcd_pause(sc)
1753 struct mcd_softc *sc;
1754 {
1755 union mcd_qchninfo q;
1756 int error;
1757
1758 /* Verify current status. */
1759 if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS) {
1760 printf("%s: pause: attempted when not playing\n",
1761 sc->sc_dev.dv_xname);
1762 return EINVAL;
1763 }
1764
1765 /* Get the current position. */
1766 if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0)
1767 return error;
1768
1769 /* Copy it into lastpb. */
1770 sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0];
1771 sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1];
1772 sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2];
1773
1774 /* Stop playing. */
1775 if ((error = mcd_stop(sc)) != 0)
1776 return error;
1777
1778 /* Set the proper status and exit. */
1779 sc->audio_status = CD_AS_PLAY_PAUSED;
1780 return 0;
1781 }
1782
1783 int
1784 mcd_resume(sc)
1785 struct mcd_softc *sc;
1786 {
1787 struct mcd_mbox mbx;
1788 int error;
1789
1790 if (sc->audio_status != CD_AS_PLAY_PAUSED)
1791 return EINVAL;
1792
1793 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1794 return error;
1795
1796 mbx.cmd = sc->lastpb;
1797 mbx.res.length = 0;
1798 return mcd_send(sc, &mbx, 1);
1799 }
1800
1801 int
1802 mcd_eject(sc)
1803 struct mcd_softc *sc;
1804 {
1805 struct mcd_mbox mbx;
1806
1807 mbx.cmd.opcode = MCD_CMDEJECTDISK;
1808 mbx.cmd.length = 0;
1809 mbx.res.length = 0;
1810 return mcd_send(sc, &mbx, 0);
1811 }
1812
1813 int
1814 mcd_setlock(sc, mode)
1815 struct mcd_softc *sc;
1816 int mode;
1817 {
1818 struct mcd_mbox mbx;
1819
1820 mbx.cmd.opcode = MCD_CMDSETLOCK;
1821 mbx.cmd.length = sizeof(mbx.cmd.data.lockmode);
1822 mbx.cmd.data.lockmode.mode = mode;
1823 mbx.res.length = 0;
1824 return mcd_send(sc, &mbx, 1);
1825 }
1826