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