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