mcd.c revision 1.84 1 /* $NetBSD: mcd.c,v 1.84 2003/06/29 22:30:20 fvdl 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.84 2003/06/29 22:30:20 fvdl 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 CFATTACH_DECL(mcd, sizeof(struct mcd_softc),
195 mcdprobe, mcdattach, NULL, NULL);
196
197 extern struct cfdriver mcd_cd;
198
199 dev_type_open(mcdopen);
200 dev_type_close(mcdclose);
201 dev_type_read(mcdread);
202 dev_type_write(mcdwrite);
203 dev_type_ioctl(mcdioctl);
204 dev_type_strategy(mcdstrategy);
205 dev_type_dump(mcddump);
206 dev_type_size(mcdsize);
207
208 const struct bdevsw mcd_bdevsw = {
209 mcdopen, mcdclose, mcdstrategy, mcdioctl, mcddump, mcdsize, D_DISK
210 };
211
212 const struct cdevsw mcd_cdevsw = {
213 mcdopen, mcdclose, mcdread, mcdwrite, mcdioctl,
214 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
215 };
216
217 void mcdgetdefaultlabel __P((struct mcd_softc *, struct disklabel *));
218 void mcdgetdisklabel __P((struct mcd_softc *));
219 int mcd_get_parms __P((struct mcd_softc *));
220 void mcdstart __P((struct mcd_softc *));
221 int mcdlock __P((struct mcd_softc *));
222 void mcdunlock __P((struct mcd_softc *));
223 void mcd_pseudointr __P((void *));
224
225 struct dkdriver mcddkdriver = { mcdstrategy };
226
227 #define MCD_RETRIES 3
228 #define MCD_RDRETRIES 3
229
230 /* several delays */
231 #define RDELAY_WAITMODE 300
232 #define RDELAY_WAITREAD 800
233
234 #define DELAY_GRANULARITY 25 /* 25us */
235 #define DELAY_GETREPLY 100000 /* 100000 * 25us */
236
237 void
238 mcdattach(parent, self, aux)
239 struct device *parent, *self;
240 void *aux;
241 {
242 struct mcd_softc *sc = (void *)self;
243 struct isa_attach_args *ia = aux;
244 bus_space_tag_t iot = ia->ia_iot;
245 bus_space_handle_t ioh;
246 struct mcd_mbox mbx;
247
248 /* Map i/o space */
249 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MCD_NPORT, 0, &ioh)) {
250 printf(": can't map i/o space\n");
251 return;
252 }
253
254 sc->sc_iot = iot;
255 sc->sc_ioh = ioh;
256
257 sc->probe = 0;
258 sc->debug = 0;
259
260 if (!mcd_find(iot, ioh, sc)) {
261 printf(": mcd_find failed\n");
262 return;
263 }
264
265 bufq_alloc(&sc->buf_queue, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
266 callout_init(&sc->sc_pintr_ch);
267
268 /*
269 * Initialize and attach the disk structure.
270 */
271 sc->sc_dk.dk_driver = &mcddkdriver;
272 sc->sc_dk.dk_name = sc->sc_dev.dv_xname;
273 disk_attach(&sc->sc_dk);
274
275 printf(": model %s\n", sc->type != 0 ? sc->type : "unknown");
276
277 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
278
279 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
280 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
281 mbx.cmd.data.config.subcommand = MCD_CF_IRQENABLE;
282 mbx.cmd.data.config.data1 = 0x01;
283 mbx.res.length = 0;
284 (void) mcd_send(sc, &mbx, 0);
285
286 mcd_soft_reset(sc);
287
288 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
289 IST_EDGE, IPL_BIO, mcdintr, sc);
290 }
291
292 /*
293 * Wait interruptibly for an exclusive lock.
294 *
295 * XXX
296 * Several drivers do this; it should be abstracted and made MP-safe.
297 */
298 int
299 mcdlock(sc)
300 struct mcd_softc *sc;
301 {
302 int error;
303
304 while ((sc->flags & MCDF_LOCKED) != 0) {
305 sc->flags |= MCDF_WANTED;
306 if ((error = tsleep(sc, PRIBIO | PCATCH, "mcdlck", 0)) != 0)
307 return error;
308 }
309 sc->flags |= MCDF_LOCKED;
310 return 0;
311 }
312
313 /*
314 * Unlock and wake up any waiters.
315 */
316 void
317 mcdunlock(sc)
318 struct mcd_softc *sc;
319 {
320
321 sc->flags &= ~MCDF_LOCKED;
322 if ((sc->flags & MCDF_WANTED) != 0) {
323 sc->flags &= ~MCDF_WANTED;
324 wakeup(sc);
325 }
326 }
327
328 int
329 mcdopen(dev, flag, fmt, p)
330 dev_t dev;
331 int flag, fmt;
332 struct proc *p;
333 {
334 int error, part;
335 struct mcd_softc *sc;
336
337 sc = device_lookup(&mcd_cd, MCDUNIT(dev));
338 if (sc == NULL)
339 return ENXIO;
340
341 if ((error = mcdlock(sc)) != 0)
342 return error;
343
344 if (sc->sc_dk.dk_openmask != 0) {
345 /*
346 * If any partition is open, but the disk has been invalidated,
347 * disallow further opens.
348 */
349 if ((sc->flags & MCDF_LOADED) == 0) {
350 error = EIO;
351 goto bad3;
352 }
353 } else {
354 /*
355 * Lock the drawer. This will also notice any pending disk
356 * change or door open indicator and clear the MCDF_LOADED bit
357 * if necessary.
358 */
359 (void) mcd_setlock(sc, MCD_LK_LOCK);
360
361 if ((sc->flags & MCDF_LOADED) == 0) {
362 /* Partially reset the state. */
363 sc->lastmode = MCD_MD_UNKNOWN;
364 sc->lastupc = MCD_UPC_UNKNOWN;
365
366 sc->flags |= MCDF_LOADED;
367
368 /* Set the mode, causing the disk to spin up. */
369 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
370 goto bad2;
371
372 /* Load the physical device parameters. */
373 if (mcd_get_parms(sc) != 0) {
374 error = ENXIO;
375 goto bad2;
376 }
377
378 /* Read the table of contents. */
379 if ((error = mcd_read_toc(sc)) != 0)
380 goto bad2;
381
382 /* Fabricate a disk label. */
383 mcdgetdisklabel(sc);
384 }
385 }
386
387 MCD_TRACE("open: partition=%d disksize=%d blksize=%d\n", part,
388 sc->disksize, sc->blksize, 0);
389
390 part = MCDPART(dev);
391
392 /* Check that the partition exists. */
393 if (part != RAW_PART &&
394 (part >= sc->sc_dk.dk_label->d_npartitions ||
395 sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
396 error = ENXIO;
397 goto bad;
398 }
399
400 /* Insure only one open at a time. */
401 switch (fmt) {
402 case S_IFCHR:
403 sc->sc_dk.dk_copenmask |= (1 << part);
404 break;
405 case S_IFBLK:
406 sc->sc_dk.dk_bopenmask |= (1 << part);
407 break;
408 }
409 sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
410
411 mcdunlock(sc);
412 return 0;
413
414 bad2:
415 sc->flags &= ~MCDF_LOADED;
416
417 bad:
418 if (sc->sc_dk.dk_openmask == 0) {
419 #if 0
420 (void) mcd_setmode(sc, MCD_MD_SLEEP);
421 #endif
422 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
423 }
424
425 bad3:
426 mcdunlock(sc);
427 return error;
428 }
429
430 int
431 mcdclose(dev, flag, fmt, p)
432 dev_t dev;
433 int flag, fmt;
434 struct proc *p;
435 {
436 struct mcd_softc *sc = device_lookup(&mcd_cd, MCDUNIT(dev));
437 int part = MCDPART(dev);
438 int error;
439
440 MCD_TRACE("close: partition=%d\n", part, 0, 0, 0);
441
442 if ((error = mcdlock(sc)) != 0)
443 return error;
444
445 switch (fmt) {
446 case S_IFCHR:
447 sc->sc_dk.dk_copenmask &= ~(1 << part);
448 break;
449 case S_IFBLK:
450 sc->sc_dk.dk_bopenmask &= ~(1 << part);
451 break;
452 }
453 sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
454
455 if (sc->sc_dk.dk_openmask == 0) {
456 /* XXXX Must wait for I/O to complete! */
457
458 #if 0
459 (void) mcd_setmode(sc, MCD_MD_SLEEP);
460 #endif
461 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
462 }
463
464 mcdunlock(sc);
465 return 0;
466 }
467
468 void
469 mcdstrategy(bp)
470 struct buf *bp;
471 {
472 struct mcd_softc *sc = device_lookup(&mcd_cd, MCDUNIT(bp->b_dev));
473 struct disklabel *lp = sc->sc_dk.dk_label;
474 daddr_t blkno;
475 int s;
476
477 /* Test validity. */
478 MCD_TRACE("strategy: buf=0x%lx blkno=%ld bcount=%ld\n", bp,
479 bp->b_blkno, bp->b_bcount, 0);
480 if (bp->b_blkno < 0 ||
481 (bp->b_bcount % sc->blksize) != 0) {
482 printf("%s: strategy: blkno = %d bcount = %ld\n",
483 sc->sc_dev.dv_xname, bp->b_blkno, bp->b_bcount);
484 bp->b_error = EINVAL;
485 goto bad;
486 }
487
488 /* If device invalidated (e.g. media change, door open), error. */
489 if ((sc->flags & MCDF_LOADED) == 0) {
490 MCD_TRACE("strategy: drive not valid\n", 0, 0, 0, 0);
491 bp->b_error = EIO;
492 goto bad;
493 }
494
495 /* No data to read. */
496 if (bp->b_bcount == 0)
497 goto done;
498
499 /*
500 * Do bounds checking, adjust transfer. if error, process.
501 * If end of partition, just return.
502 */
503 if (MCDPART(bp->b_dev) != RAW_PART &&
504 bounds_check_with_label(&sc->sc_dk, bp,
505 (sc->flags & (MCDF_WLABEL|MCDF_LABELLING)) != 0) <= 0)
506 goto done;
507
508 /*
509 * Now convert the block number to absolute and put it in
510 * terms of the device's logical block size.
511 */
512 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
513 if (MCDPART(bp->b_dev) != RAW_PART)
514 blkno += lp->d_partitions[MCDPART(bp->b_dev)].p_offset;
515
516 bp->b_rawblkno = blkno;
517
518 /* Queue it. */
519 s = splbio();
520 BUFQ_PUT(&sc->buf_queue, bp);
521 splx(s);
522 if (!sc->active)
523 mcdstart(sc);
524 return;
525
526 bad:
527 bp->b_flags |= B_ERROR;
528 done:
529 bp->b_resid = bp->b_bcount;
530 biodone(bp);
531 }
532
533 void
534 mcdstart(sc)
535 struct mcd_softc *sc;
536 {
537 struct buf *bp;
538 int s;
539
540 loop:
541 s = splbio();
542
543 if ((bp = BUFQ_GET(&sc->buf_queue)) == NULL) {
544 /* Nothing to do. */
545 sc->active = 0;
546 splx(s);
547 return;
548 }
549
550 /* Block found to process. */
551 MCD_TRACE("start: found block bp=0x%x\n", bp, 0, 0, 0);
552 splx(s);
553
554 /* Changed media? */
555 if ((sc->flags & MCDF_LOADED) == 0) {
556 MCD_TRACE("start: drive not valid\n", 0, 0, 0, 0);
557 bp->b_error = EIO;
558 bp->b_flags |= B_ERROR;
559 biodone(bp);
560 goto loop;
561 }
562
563 sc->active = 1;
564
565 /* Instrumentation. */
566 s = splbio();
567 disk_busy(&sc->sc_dk);
568 splx(s);
569
570 sc->mbx.retry = MCD_RDRETRIES;
571 sc->mbx.bp = bp;
572 sc->mbx.blkno = bp->b_rawblkno;
573 sc->mbx.nblk = bp->b_bcount / sc->blksize;
574 sc->mbx.sz = sc->blksize;
575 sc->mbx.skip = 0;
576 sc->mbx.state = MCD_S_BEGIN;
577 sc->mbx.mode = MCD_MD_COOKED;
578
579 s = splbio();
580 (void) mcdintr(sc);
581 splx(s);
582 }
583
584 int
585 mcdread(dev, uio, flags)
586 dev_t dev;
587 struct uio *uio;
588 int flags;
589 {
590
591 return (physio(mcdstrategy, NULL, dev, B_READ, minphys, uio));
592 }
593
594 int
595 mcdwrite(dev, uio, flags)
596 dev_t dev;
597 struct uio *uio;
598 int flags;
599 {
600
601 return (physio(mcdstrategy, NULL, dev, B_WRITE, minphys, uio));
602 }
603
604 int
605 mcdioctl(dev, cmd, addr, flag, p)
606 dev_t dev;
607 u_long cmd;
608 caddr_t addr;
609 int flag;
610 struct proc *p;
611 {
612 struct mcd_softc *sc = device_lookup(&mcd_cd, MCDUNIT(dev));
613 int error;
614 int part;
615 #ifdef __HAVE_OLD_DISKLABEL
616 struct disklabel newlabel;
617 #endif
618
619 MCD_TRACE("ioctl: cmd=0x%x\n", cmd, 0, 0, 0);
620
621 if ((sc->flags & MCDF_LOADED) == 0)
622 return EIO;
623
624 part = MCDPART(dev);
625 switch (cmd) {
626 case DIOCGDINFO:
627 *(struct disklabel *)addr = *(sc->sc_dk.dk_label);
628 return 0;
629 #ifdef __HAVE_OLD_DISKLABEL
630 case ODIOCGDINFO:
631 newlabel = *(sc->sc_dk.dk_label);
632 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
633 return ENOTTY;
634 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
635 return 0;
636 #endif
637
638 case DIOCGPART:
639 ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
640 ((struct partinfo *)addr)->part =
641 &sc->sc_dk.dk_label->d_partitions[part];
642 return 0;
643
644 case DIOCWDINFO:
645 case DIOCSDINFO:
646 #ifdef __HAVE_OLD_DISKLABEL
647 case ODIOCWDINFO:
648 case ODIOCSDINFO:
649 #endif
650 {
651 struct disklabel *lp;
652
653 if ((flag & FWRITE) == 0)
654 return EBADF;
655
656 #ifdef __HAVE_OLD_DISKLABEL
657 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
658 memset(&newlabel, 0, sizeof newlabel);
659 memcpy(&newlabel, addr, sizeof (struct olddisklabel));
660 lp = &newlabel;
661 } else
662 #endif
663 lp = (struct disklabel *)addr;
664
665 if ((error = mcdlock(sc)) != 0)
666 return error;
667 sc->flags |= MCDF_LABELLING;
668
669 error = setdisklabel(sc->sc_dk.dk_label,
670 lp, /*sc->sc_dk.dk_openmask : */0,
671 sc->sc_dk.dk_cpulabel);
672 if (error == 0) {
673 }
674
675 sc->flags &= ~MCDF_LABELLING;
676 mcdunlock(sc);
677 return error;
678 }
679
680 case DIOCWLABEL:
681 return EBADF;
682
683 case DIOCGDEFLABEL:
684 mcdgetdefaultlabel(sc, (struct disklabel *)addr);
685 return 0;
686
687 #ifdef __HAVE_OLD_DISKLABEL
688 case ODIOCGDEFLABEL:
689 mcdgetdefaultlabel(sc, &newlabel);
690 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
691 return ENOTTY;
692 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
693 return 0;
694 #endif
695
696 case CDIOCPLAYTRACKS:
697 return mcd_playtracks(sc, (struct ioc_play_track *)addr);
698 case CDIOCPLAYMSF:
699 return mcd_playmsf(sc, (struct ioc_play_msf *)addr);
700 case CDIOCPLAYBLOCKS:
701 return mcd_playblocks(sc, (struct ioc_play_blocks *)addr);
702 case CDIOCREADSUBCHANNEL:
703 return mcd_read_subchannel(sc, (struct ioc_read_subchannel *)addr);
704 case CDIOREADTOCHEADER:
705 return mcd_toc_header(sc, (struct ioc_toc_header *)addr);
706 case CDIOREADTOCENTRYS:
707 return mcd_toc_entries(sc, (struct ioc_read_toc_entry *)addr);
708 case CDIOCSETPATCH:
709 case CDIOCGETVOL:
710 case CDIOCSETVOL:
711 case CDIOCSETMONO:
712 case CDIOCSETSTEREO:
713 case CDIOCSETMUTE:
714 case CDIOCSETLEFT:
715 case CDIOCSETRIGHT:
716 return EINVAL;
717 case CDIOCRESUME:
718 return mcd_resume(sc);
719 case CDIOCPAUSE:
720 return mcd_pause(sc);
721 case CDIOCSTART:
722 return EINVAL;
723 case CDIOCSTOP:
724 return mcd_stop(sc);
725 case DIOCEJECT:
726 if (*(int *)addr == 0) {
727 /*
728 * Don't force eject: check that we are the only
729 * partition open. If so, unlock it.
730 */
731 if ((sc->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
732 sc->sc_dk.dk_bopenmask + sc->sc_dk.dk_copenmask ==
733 sc->sc_dk.dk_openmask) {
734 error = mcd_setlock(sc, MCD_LK_UNLOCK);
735 if (error)
736 return (error);
737 } else {
738 return (EBUSY);
739 }
740 }
741 /* FALLTHROUGH */
742 case CDIOCEJECT: /* FALLTHROUGH */
743 case ODIOCEJECT:
744 return mcd_eject(sc);
745 case CDIOCALLOW:
746 return mcd_setlock(sc, MCD_LK_UNLOCK);
747 case CDIOCPREVENT:
748 return mcd_setlock(sc, MCD_LK_LOCK);
749 case DIOCLOCK:
750 return mcd_setlock(sc,
751 (*(int *)addr) ? MCD_LK_LOCK : MCD_LK_UNLOCK);
752 case CDIOCSETDEBUG:
753 sc->debug = 1;
754 return 0;
755 case CDIOCCLRDEBUG:
756 sc->debug = 0;
757 return 0;
758 case CDIOCRESET:
759 return mcd_hard_reset(sc);
760
761 default:
762 return ENOTTY;
763 }
764
765 #ifdef DIAGNOSTIC
766 panic("mcdioctl: impossible");
767 #endif
768 }
769
770 void
771 mcdgetdefaultlabel(sc, lp)
772 struct mcd_softc *sc;
773 struct disklabel *lp;
774 {
775
776 memset(lp, 0, sizeof(struct disklabel));
777
778 lp->d_secsize = sc->blksize;
779 lp->d_ntracks = 1;
780 lp->d_nsectors = 100;
781 lp->d_ncylinders = (sc->disksize / 100) + 1;
782 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
783
784 strncpy(lp->d_typename, "Mitsumi CD-ROM", 16);
785 lp->d_type = 0; /* XXX */
786 strncpy(lp->d_packname, "fictitious", 16);
787 lp->d_secperunit = sc->disksize;
788 lp->d_rpm = 300;
789 lp->d_interleave = 1;
790 lp->d_flags = D_REMOVABLE;
791
792 lp->d_partitions[0].p_offset = 0;
793 lp->d_partitions[0].p_size =
794 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
795 lp->d_partitions[0].p_fstype = FS_ISO9660;
796 lp->d_partitions[RAW_PART].p_offset = 0;
797 lp->d_partitions[RAW_PART].p_size =
798 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
799 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660;
800 lp->d_npartitions = RAW_PART + 1;
801
802 lp->d_magic = DISKMAGIC;
803 lp->d_magic2 = DISKMAGIC;
804 lp->d_checksum = dkcksum(lp);
805 }
806
807 /*
808 * This could have been taken from scsi/cd.c, but it is not clear
809 * whether the scsi cd driver is linked in.
810 */
811 void
812 mcdgetdisklabel(sc)
813 struct mcd_softc *sc;
814 {
815 struct disklabel *lp = sc->sc_dk.dk_label;
816
817 memset(sc->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
818
819 mcdgetdefaultlabel(sc, lp);
820 }
821
822 int
823 mcd_get_parms(sc)
824 struct mcd_softc *sc;
825 {
826 struct mcd_mbox mbx;
827 daddr_t size;
828 int error;
829
830 /* Send volume info command. */
831 mbx.cmd.opcode = MCD_CMDGETVOLINFO;
832 mbx.cmd.length = 0;
833 mbx.res.length = sizeof(mbx.res.data.volinfo);
834 if ((error = mcd_send(sc, &mbx, 1)) != 0)
835 return error;
836
837 if (mbx.res.data.volinfo.trk_low == 0x00 &&
838 mbx.res.data.volinfo.trk_high == 0x00)
839 return EINVAL;
840
841 /* Volinfo is OK. */
842 sc->volinfo = mbx.res.data.volinfo;
843 sc->blksize = MCD_BLKSIZE_COOKED;
844 size = msf2hsg(sc->volinfo.vol_msf, 0);
845 sc->disksize = size * (MCD_BLKSIZE_COOKED / DEV_BSIZE);
846 return 0;
847 }
848
849 int
850 mcdsize(dev)
851 dev_t dev;
852 {
853
854 /* CD-ROMs are read-only. */
855 return -1;
856 }
857
858 int
859 mcddump(dev, blkno, va, size)
860 dev_t dev;
861 daddr_t blkno;
862 caddr_t va;
863 size_t size;
864 {
865
866 /* Not implemented. */
867 return ENXIO;
868 }
869
870 /*
871 * Find the board and fill in the softc.
872 */
873 int
874 mcd_find(iot, ioh, sc)
875 bus_space_tag_t iot;
876 bus_space_handle_t ioh;
877 struct mcd_softc *sc;
878 {
879 int i;
880 struct mcd_mbox mbx;
881
882 sc->sc_iot = iot;
883 sc->sc_ioh = ioh;
884
885 /* Send a reset. */
886 bus_space_write_1(iot, ioh, MCD_RESET, 0);
887 delay(1000000);
888 /* Get any pending status and throw away. */
889 for (i = 10; i; i--)
890 bus_space_read_1(iot, ioh, MCD_STATUS);
891 delay(1000);
892
893 /* Send get status command. */
894 mbx.cmd.opcode = MCD_CMDGETSTAT;
895 mbx.cmd.length = 0;
896 mbx.res.length = 0;
897 if (mcd_send(sc, &mbx, 0) != 0)
898 return 0;
899
900 /* Get info about the drive. */
901 mbx.cmd.opcode = MCD_CMDCONTINFO;
902 mbx.cmd.length = 0;
903 mbx.res.length = sizeof(mbx.res.data.continfo);
904 if (mcd_send(sc, &mbx, 0) != 0)
905 return 0;
906
907 /*
908 * The following is code which is not guaranteed to work for all
909 * drives, because the meaning of the expected 'M' is not clear
910 * (M_itsumi is an obvious assumption, but I don't trust that).
911 * Also, the original hack had a bogus condition that always
912 * returned true.
913 *
914 * Note: Which models support interrupts? >=LU005S?
915 */
916 sc->readcmd = MCD_CMDREADSINGLESPEED;
917 switch (mbx.res.data.continfo.code) {
918 case 'M':
919 if (mbx.res.data.continfo.version <= 2)
920 sc->type = "LU002S";
921 else if (mbx.res.data.continfo.version <= 5)
922 sc->type = "LU005S";
923 else
924 sc->type = "LU006S";
925 break;
926 case 'F':
927 sc->type = "FX001";
928 break;
929 case 'D':
930 sc->type = "FX001D";
931 sc->readcmd = MCD_CMDREADDOUBLESPEED;
932 break;
933 default:
934 /*
935 * mcd_send() says the response looked OK but the
936 * drive type is unknown. If mcd_promisc, match anyway.
937 */
938 if (mcd_promisc != 0)
939 return 0;
940
941 #ifdef MCDDEBUG
942 printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n",
943 sc->sc_dev.dv_xname,
944 mbx.res.data.continfo.code, mbx.res.data.continfo.version);
945 #endif
946 sc->type = 0;
947 break;
948 }
949
950 return 1;
951
952 }
953
954 int
955 mcdprobe(parent, match, aux)
956 struct device *parent;
957 struct cfdata *match;
958 void *aux;
959 {
960 struct isa_attach_args *ia = aux;
961 struct mcd_softc sc;
962 bus_space_tag_t iot = ia->ia_iot;
963 bus_space_handle_t ioh;
964 int rv;
965
966 if (ia->ia_nio < 1)
967 return (0);
968 if (ia->ia_nirq < 1)
969 return (0);
970
971 if (ISA_DIRECT_CONFIG(ia))
972 return (0);
973
974 /* Disallow wildcarded i/o address. */
975 if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
976 return (0);
977 if (ia->ia_irq[0].ir_irq == ISACF_IRQ_DEFAULT)
978 return (0);
979
980 /* Map i/o space */
981 if (bus_space_map(iot, ia->ia_io[0].ir_addr, MCD_NPORT, 0, &ioh))
982 return 0;
983
984 sc.debug = 0;
985 sc.probe = 1;
986
987 rv = mcd_find(iot, ioh, &sc);
988
989 bus_space_unmap(iot, ioh, MCD_NPORT);
990
991 if (rv) {
992 ia->ia_nio = 1;
993 ia->ia_io[0].ir_addr = MCD_NPORT;
994
995 ia->ia_nirq = 1;
996
997 ia->ia_niomem = 0;
998 ia->ia_ndrq = 0;
999 }
1000
1001 return (rv);
1002 }
1003
1004 int
1005 mcd_getreply(sc)
1006 struct mcd_softc *sc;
1007 {
1008 bus_space_tag_t iot = sc->sc_iot;
1009 bus_space_handle_t ioh = sc->sc_ioh;
1010 int i;
1011
1012 /* Wait until xfer port senses data ready. */
1013 for (i = DELAY_GETREPLY; i; i--) {
1014 if ((bus_space_read_1(iot, ioh, MCD_XFER) &
1015 MCD_XF_STATUSUNAVAIL) == 0)
1016 break;
1017 delay(DELAY_GRANULARITY);
1018 }
1019 if (!i)
1020 return -1;
1021
1022 /* Get the data. */
1023 return bus_space_read_1(iot, ioh, MCD_STATUS);
1024 }
1025
1026 int
1027 mcd_getstat(sc)
1028 struct mcd_softc *sc;
1029 {
1030 struct mcd_mbox mbx;
1031
1032 mbx.cmd.opcode = MCD_CMDGETSTAT;
1033 mbx.cmd.length = 0;
1034 mbx.res.length = 0;
1035 return mcd_send(sc, &mbx, 1);
1036 }
1037
1038 int
1039 mcd_getresult(sc, res)
1040 struct mcd_softc *sc;
1041 struct mcd_result *res;
1042 {
1043 int i, x;
1044
1045 if (sc->debug)
1046 printf("%s: mcd_getresult: %d", sc->sc_dev.dv_xname,
1047 res->length);
1048
1049 if ((x = mcd_getreply(sc)) < 0) {
1050 if (sc->debug)
1051 printf(" timeout\n");
1052 else if (!sc->probe)
1053 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
1054 return EIO;
1055 }
1056 if (sc->debug)
1057 printf(" %02x", (u_int)x);
1058 sc->status = x;
1059 mcd_setflags(sc);
1060
1061 if ((sc->status & MCD_ST_CMDCHECK) != 0)
1062 return EINVAL;
1063
1064 for (i = 0; i < res->length; i++) {
1065 if ((x = mcd_getreply(sc)) < 0) {
1066 if (sc->debug)
1067 printf(" timeout\n");
1068 else
1069 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
1070 return EIO;
1071 }
1072 if (sc->debug)
1073 printf(" %02x", (u_int)x);
1074 res->data.raw.data[i] = x;
1075 }
1076
1077 if (sc->debug)
1078 printf(" succeeded\n");
1079
1080 #ifdef MCDDEBUG
1081 delay(10);
1082 while ((bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_XFER) &
1083 MCD_XF_STATUSUNAVAIL) == 0) {
1084 x = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_STATUS);
1085 printf("%s: got extra byte %02x during getstatus\n",
1086 sc->sc_dev.dv_xname, (u_int)x);
1087 delay(10);
1088 }
1089 #endif
1090
1091 return 0;
1092 }
1093
1094 void
1095 mcd_setflags(sc)
1096 struct mcd_softc *sc;
1097 {
1098
1099 /* Check flags. */
1100 if ((sc->flags & MCDF_LOADED) != 0 &&
1101 (sc->status & (MCD_ST_DSKCHNG | MCD_ST_DSKIN | MCD_ST_DOOROPEN)) !=
1102 MCD_ST_DSKIN) {
1103 if ((sc->status & MCD_ST_DOOROPEN) != 0)
1104 printf("%s: door open\n", sc->sc_dev.dv_xname);
1105 else if ((sc->status & MCD_ST_DSKIN) == 0)
1106 printf("%s: no disk present\n", sc->sc_dev.dv_xname);
1107 else if ((sc->status & MCD_ST_DSKCHNG) != 0)
1108 printf("%s: media change\n", sc->sc_dev.dv_xname);
1109 sc->flags &= ~MCDF_LOADED;
1110 }
1111
1112 if ((sc->status & MCD_ST_AUDIOBSY) != 0)
1113 sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
1114 else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS ||
1115 sc->audio_status == CD_AS_AUDIO_INVALID)
1116 sc->audio_status = CD_AS_PLAY_COMPLETED;
1117 }
1118
1119 int
1120 mcd_send(sc, mbx, diskin)
1121 struct mcd_softc *sc;
1122 struct mcd_mbox *mbx;
1123 int diskin;
1124 {
1125 int retry, i, error;
1126 bus_space_tag_t iot = sc->sc_iot;
1127 bus_space_handle_t ioh = sc->sc_ioh;
1128
1129 if (sc->debug) {
1130 printf("%s: mcd_send: %d %02x", sc->sc_dev.dv_xname,
1131 mbx->cmd.length, (u_int)mbx->cmd.opcode);
1132 for (i = 0; i < mbx->cmd.length; i++)
1133 printf(" %02x", (u_int)mbx->cmd.data.raw.data[i]);
1134 printf("\n");
1135 }
1136
1137 for (retry = MCD_RETRIES; retry; retry--) {
1138 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.opcode);
1139 for (i = 0; i < mbx->cmd.length; i++)
1140 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.data.raw.data[i]);
1141 if ((error = mcd_getresult(sc, &mbx->res)) == 0)
1142 break;
1143 if (error == EINVAL)
1144 return error;
1145 }
1146 if (!retry)
1147 return error;
1148 if (diskin && (sc->flags & MCDF_LOADED) == 0)
1149 return EIO;
1150
1151 return 0;
1152 }
1153
1154 static int
1155 bcd2bin(b)
1156 bcd_t b;
1157 {
1158
1159 return (b >> 4) * 10 + (b & 15);
1160 }
1161
1162 static bcd_t
1163 bin2bcd(b)
1164 int b;
1165 {
1166
1167 return ((b / 10) << 4) | (b % 10);
1168 }
1169
1170 static void
1171 hsg2msf(hsg, msf)
1172 int hsg;
1173 bcd_t *msf;
1174 {
1175
1176 hsg += 150;
1177 F_msf(msf) = bin2bcd(hsg % 75);
1178 hsg /= 75;
1179 S_msf(msf) = bin2bcd(hsg % 60);
1180 hsg /= 60;
1181 M_msf(msf) = bin2bcd(hsg);
1182 }
1183
1184 static daddr_t
1185 msf2hsg(msf, relative)
1186 bcd_t *msf;
1187 int relative;
1188 {
1189 daddr_t blkno;
1190
1191 blkno = bcd2bin(M_msf(msf)) * 75 * 60 +
1192 bcd2bin(S_msf(msf)) * 75 +
1193 bcd2bin(F_msf(msf));
1194 if (!relative)
1195 blkno -= 150;
1196 return blkno;
1197 }
1198
1199 void
1200 mcd_pseudointr(v)
1201 void *v;
1202 {
1203 struct mcd_softc *sc = v;
1204 int s;
1205
1206 s = splbio();
1207 (void) mcdintr(sc);
1208 splx(s);
1209 }
1210
1211 /*
1212 * State machine to process read requests.
1213 * Initialize with MCD_S_BEGIN: calculate sizes, and set mode
1214 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
1215 * MCD_S_WAITREAD: wait for read ready, read data.
1216 */
1217 int
1218 mcdintr(arg)
1219 void *arg;
1220 {
1221 struct mcd_softc *sc = arg;
1222 struct mcd_mbx *mbx = &sc->mbx;
1223 struct buf *bp = mbx->bp;
1224 bus_space_tag_t iot = sc->sc_iot;
1225 bus_space_handle_t ioh = sc->sc_ioh;
1226
1227 int i;
1228 u_char x;
1229 bcd_t msf[3];
1230
1231 switch (mbx->state) {
1232 case MCD_S_IDLE:
1233 return 0;
1234
1235 case MCD_S_BEGIN:
1236 tryagain:
1237 if (mbx->mode == sc->lastmode)
1238 goto firstblock;
1239
1240 sc->lastmode = MCD_MD_UNKNOWN;
1241 bus_space_write_1(iot, ioh, MCD_COMMAND, MCD_CMDSETMODE);
1242 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->mode);
1243
1244 mbx->count = RDELAY_WAITMODE;
1245 mbx->state = MCD_S_WAITMODE;
1246
1247 case MCD_S_WAITMODE:
1248 callout_stop(&sc->sc_pintr_ch);
1249 for (i = 20; i; i--) {
1250 x = bus_space_read_1(iot, ioh, MCD_XFER);
1251 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1252 break;
1253 delay(50);
1254 }
1255 if (i == 0)
1256 goto hold;
1257 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS);
1258 mcd_setflags(sc);
1259 if ((sc->flags & MCDF_LOADED) == 0)
1260 goto changed;
1261 MCD_TRACE("doread: got WAITMODE delay=%d\n",
1262 RDELAY_WAITMODE - mbx->count, 0, 0, 0);
1263
1264 sc->lastmode = mbx->mode;
1265
1266 firstblock:
1267 MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", mbx->blkno,
1268 bp, 0, 0);
1269
1270 /* Build parameter block. */
1271 hsg2msf(mbx->blkno, msf);
1272
1273 /* Send the read command. */
1274 bus_space_write_1(iot, ioh, MCD_COMMAND, sc->readcmd);
1275 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[0]);
1276 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[1]);
1277 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[2]);
1278 bus_space_write_1(iot, ioh, MCD_COMMAND, 0);
1279 bus_space_write_1(iot, ioh, MCD_COMMAND, 0);
1280 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->nblk);
1281
1282 mbx->count = RDELAY_WAITREAD;
1283 mbx->state = MCD_S_WAITREAD;
1284
1285 case MCD_S_WAITREAD:
1286 callout_stop(&sc->sc_pintr_ch);
1287 nextblock:
1288 loop:
1289 for (i = 20; i; i--) {
1290 x = bus_space_read_1(iot, ioh, MCD_XFER);
1291 if ((x & MCD_XF_DATAUNAVAIL) == 0)
1292 goto gotblock;
1293 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1294 break;
1295 delay(50);
1296 }
1297 if (i == 0)
1298 goto hold;
1299 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS);
1300 mcd_setflags(sc);
1301 if ((sc->flags & MCDF_LOADED) == 0)
1302 goto changed;
1303 #if 0
1304 printf("%s: got status byte %02x during read\n",
1305 sc->sc_dev.dv_xname, (u_int)sc->status);
1306 #endif
1307 goto loop;
1308
1309 gotblock:
1310 MCD_TRACE("doread: got data delay=%d\n",
1311 RDELAY_WAITREAD - mbx->count, 0, 0, 0);
1312
1313 /* Data is ready. */
1314 bus_space_write_1(iot, ioh, MCD_CTL2, 0x04); /* XXX */
1315 bus_space_read_multi_1(iot, ioh, MCD_RDATA,
1316 bp->b_data + mbx->skip, mbx->sz);
1317 bus_space_write_1(iot, ioh, MCD_CTL2, 0x0c); /* XXX */
1318 mbx->blkno += 1;
1319 mbx->skip += mbx->sz;
1320 if (--mbx->nblk > 0)
1321 goto nextblock;
1322
1323 mbx->state = MCD_S_IDLE;
1324
1325 /* Return buffer. */
1326 bp->b_resid = 0;
1327 disk_unbusy(&sc->sc_dk, bp->b_bcount, (bp->b_flags & B_READ));
1328 biodone(bp);
1329
1330 mcdstart(sc);
1331 return 1;
1332
1333 hold:
1334 if (mbx->count-- < 0) {
1335 printf("%s: timeout in state %d",
1336 sc->sc_dev.dv_xname, mbx->state);
1337 goto readerr;
1338 }
1339
1340 #if 0
1341 printf("%s: sleep in state %d\n", sc->sc_dev.dv_xname,
1342 mbx->state);
1343 #endif
1344 callout_reset(&sc->sc_pintr_ch, hz / 100,
1345 mcd_pseudointr, sc);
1346 return -1;
1347 }
1348
1349 readerr:
1350 if (mbx->retry-- > 0) {
1351 printf("; retrying\n");
1352 goto tryagain;
1353 } else
1354 printf("; giving up\n");
1355
1356 changed:
1357 /* Invalidate the buffer. */
1358 bp->b_flags |= B_ERROR;
1359 bp->b_resid = bp->b_bcount - mbx->skip;
1360 disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid),
1361 (bp->b_flags & B_READ));
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