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