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