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