mcd.c revision 1.66 1 /* $NetBSD: mcd.c,v 1.66 2000/03/23 07:01:35 thorpej 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
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
619 case DIOCGPART:
620 ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
621 ((struct partinfo *)addr)->part =
622 &sc->sc_dk.dk_label->d_partitions[part];
623 return 0;
624
625 case DIOCWDINFO:
626 case DIOCSDINFO:
627 if ((flag & FWRITE) == 0)
628 return EBADF;
629
630 if ((error = mcdlock(sc)) != 0)
631 return error;
632 sc->flags |= MCDF_LABELLING;
633
634 error = setdisklabel(sc->sc_dk.dk_label,
635 (struct disklabel *)addr, /*sc->sc_dk.dk_openmask : */0,
636 sc->sc_dk.dk_cpulabel);
637 if (error == 0) {
638 }
639
640 sc->flags &= ~MCDF_LABELLING;
641 mcdunlock(sc);
642 return error;
643
644 case DIOCWLABEL:
645 return EBADF;
646
647 case DIOCGDEFLABEL:
648 mcdgetdefaultlabel(sc, (struct disklabel *)addr);
649 return 0;
650
651 case CDIOCPLAYTRACKS:
652 return mcd_playtracks(sc, (struct ioc_play_track *)addr);
653 case CDIOCPLAYMSF:
654 return mcd_playmsf(sc, (struct ioc_play_msf *)addr);
655 case CDIOCPLAYBLOCKS:
656 return mcd_playblocks(sc, (struct ioc_play_blocks *)addr);
657 case CDIOCREADSUBCHANNEL:
658 return mcd_read_subchannel(sc, (struct ioc_read_subchannel *)addr);
659 case CDIOREADTOCHEADER:
660 return mcd_toc_header(sc, (struct ioc_toc_header *)addr);
661 case CDIOREADTOCENTRYS:
662 return mcd_toc_entries(sc, (struct ioc_read_toc_entry *)addr);
663 case CDIOCSETPATCH:
664 case CDIOCGETVOL:
665 case CDIOCSETVOL:
666 case CDIOCSETMONO:
667 case CDIOCSETSTEREO:
668 case CDIOCSETMUTE:
669 case CDIOCSETLEFT:
670 case CDIOCSETRIGHT:
671 return EINVAL;
672 case CDIOCRESUME:
673 return mcd_resume(sc);
674 case CDIOCPAUSE:
675 return mcd_pause(sc);
676 case CDIOCSTART:
677 return EINVAL;
678 case CDIOCSTOP:
679 return mcd_stop(sc);
680 case DIOCEJECT:
681 if (*(int *)addr == 0) {
682 /*
683 * Don't force eject: check that we are the only
684 * partition open. If so, unlock it.
685 */
686 if ((sc->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
687 sc->sc_dk.dk_bopenmask + sc->sc_dk.dk_copenmask ==
688 sc->sc_dk.dk_openmask) {
689 error = mcd_setlock(sc, MCD_LK_UNLOCK);
690 if (error)
691 return (error);
692 } else {
693 return (EBUSY);
694 }
695 }
696 /* FALLTHROUGH */
697 case CDIOCEJECT: /* FALLTHROUGH */
698 case ODIOCEJECT:
699 return mcd_eject(sc);
700 case CDIOCALLOW:
701 return mcd_setlock(sc, MCD_LK_UNLOCK);
702 case CDIOCPREVENT:
703 return mcd_setlock(sc, MCD_LK_LOCK);
704 case DIOCLOCK:
705 return mcd_setlock(sc,
706 (*(int *)addr) ? MCD_LK_LOCK : MCD_LK_UNLOCK);
707 case CDIOCSETDEBUG:
708 sc->debug = 1;
709 return 0;
710 case CDIOCCLRDEBUG:
711 sc->debug = 0;
712 return 0;
713 case CDIOCRESET:
714 return mcd_hard_reset(sc);
715
716 default:
717 return ENOTTY;
718 }
719
720 #ifdef DIAGNOSTIC
721 panic("mcdioctl: impossible");
722 #endif
723 }
724
725 void
726 mcdgetdefaultlabel(sc, lp)
727 struct mcd_softc *sc;
728 struct disklabel *lp;
729 {
730
731 bzero(lp, sizeof(struct disklabel));
732
733 lp->d_secsize = sc->blksize;
734 lp->d_ntracks = 1;
735 lp->d_nsectors = 100;
736 lp->d_ncylinders = (sc->disksize / 100) + 1;
737 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
738
739 strncpy(lp->d_typename, "Mitsumi CD-ROM", 16);
740 lp->d_type = 0; /* XXX */
741 strncpy(lp->d_packname, "fictitious", 16);
742 lp->d_secperunit = sc->disksize;
743 lp->d_rpm = 300;
744 lp->d_interleave = 1;
745 lp->d_flags = D_REMOVABLE;
746
747 lp->d_partitions[0].p_offset = 0;
748 lp->d_partitions[0].p_size =
749 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
750 lp->d_partitions[0].p_fstype = FS_ISO9660;
751 lp->d_partitions[RAW_PART].p_offset = 0;
752 lp->d_partitions[RAW_PART].p_size =
753 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
754 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660;
755 lp->d_npartitions = RAW_PART + 1;
756
757 lp->d_magic = DISKMAGIC;
758 lp->d_magic2 = DISKMAGIC;
759 lp->d_checksum = dkcksum(lp);
760 }
761
762 /*
763 * This could have been taken from scsi/cd.c, but it is not clear
764 * whether the scsi cd driver is linked in.
765 */
766 void
767 mcdgetdisklabel(sc)
768 struct mcd_softc *sc;
769 {
770 struct disklabel *lp = sc->sc_dk.dk_label;
771
772 bzero(sc->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
773
774 mcdgetdefaultlabel(sc, lp);
775 }
776
777 int
778 mcd_get_parms(sc)
779 struct mcd_softc *sc;
780 {
781 struct mcd_mbox mbx;
782 daddr_t size;
783 int error;
784
785 /* Send volume info command. */
786 mbx.cmd.opcode = MCD_CMDGETVOLINFO;
787 mbx.cmd.length = 0;
788 mbx.res.length = sizeof(mbx.res.data.volinfo);
789 if ((error = mcd_send(sc, &mbx, 1)) != 0)
790 return error;
791
792 if (mbx.res.data.volinfo.trk_low == 0x00 &&
793 mbx.res.data.volinfo.trk_high == 0x00)
794 return EINVAL;
795
796 /* Volinfo is OK. */
797 sc->volinfo = mbx.res.data.volinfo;
798 sc->blksize = MCD_BLKSIZE_COOKED;
799 size = msf2hsg(sc->volinfo.vol_msf, 0);
800 sc->disksize = size * (MCD_BLKSIZE_COOKED / DEV_BSIZE);
801 return 0;
802 }
803
804 int
805 mcdsize(dev)
806 dev_t dev;
807 {
808
809 /* CD-ROMs are read-only. */
810 return -1;
811 }
812
813 int
814 mcddump(dev, blkno, va, size)
815 dev_t dev;
816 daddr_t blkno;
817 caddr_t va;
818 size_t size;
819 {
820
821 /* Not implemented. */
822 return ENXIO;
823 }
824
825 /*
826 * Find the board and fill in the softc.
827 */
828 int
829 mcd_find(iot, ioh, sc)
830 bus_space_tag_t iot;
831 bus_space_handle_t ioh;
832 struct mcd_softc *sc;
833 {
834 int i;
835 struct mcd_mbox mbx;
836
837 sc->sc_iot = iot;
838 sc->sc_ioh = ioh;
839
840 /* Send a reset. */
841 bus_space_write_1(iot, ioh, MCD_RESET, 0);
842 delay(1000000);
843 /* Get any pending status and throw away. */
844 for (i = 10; i; i--)
845 bus_space_read_1(iot, ioh, MCD_STATUS);
846 delay(1000);
847
848 /* Send get status command. */
849 mbx.cmd.opcode = MCD_CMDGETSTAT;
850 mbx.cmd.length = 0;
851 mbx.res.length = 0;
852 if (mcd_send(sc, &mbx, 0) != 0)
853 return 0;
854
855 /* Get info about the drive. */
856 mbx.cmd.opcode = MCD_CMDCONTINFO;
857 mbx.cmd.length = 0;
858 mbx.res.length = sizeof(mbx.res.data.continfo);
859 if (mcd_send(sc, &mbx, 0) != 0)
860 return 0;
861
862 /*
863 * The following is code which is not guaranteed to work for all
864 * drives, because the meaning of the expected 'M' is not clear
865 * (M_itsumi is an obvious assumption, but I don't trust that).
866 * Also, the original hack had a bogus condition that always
867 * returned true.
868 *
869 * Note: Which models support interrupts? >=LU005S?
870 */
871 sc->readcmd = MCD_CMDREADSINGLESPEED;
872 switch (mbx.res.data.continfo.code) {
873 case 'M':
874 if (mbx.res.data.continfo.version <= 2)
875 sc->type = "LU002S";
876 else if (mbx.res.data.continfo.version <= 5)
877 sc->type = "LU005S";
878 else
879 sc->type = "LU006S";
880 break;
881 case 'F':
882 sc->type = "FX001";
883 break;
884 case 'D':
885 sc->type = "FX001D";
886 sc->readcmd = MCD_CMDREADDOUBLESPEED;
887 break;
888 default:
889 /*
890 * mcd_send() says the response looked OK but the
891 * drive type is unknown. If mcd_promisc, match anyway.
892 */
893 if (mcd_promisc != 0)
894 return 0;
895
896 #ifdef MCDDEBUG
897 printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n",
898 sc->sc_dev.dv_xname,
899 mbx.res.data.continfo.code, mbx.res.data.continfo.version);
900 #endif
901 sc->type = 0;
902 break;
903 }
904
905 return 1;
906
907 }
908
909 int
910 mcdprobe(parent, match, aux)
911 struct device *parent;
912 struct cfdata *match;
913 void *aux;
914 {
915 struct isa_attach_args *ia = aux;
916 struct mcd_softc sc;
917 bus_space_tag_t iot = ia->ia_iot;
918 bus_space_handle_t ioh;
919 int rv;
920
921 /* Disallow wildcarded i/o address. */
922 if (ia->ia_iobase == ISACF_PORT_DEFAULT)
923 return (0);
924
925 /* Map i/o space */
926 if (bus_space_map(iot, ia->ia_iobase, MCD_NPORT, 0, &ioh))
927 return 0;
928
929 sc.debug = 0;
930 sc.probe = 1;
931
932 rv = mcd_find(iot, ioh, &sc);
933
934 bus_space_unmap(iot, ioh, MCD_NPORT);
935
936 if (rv) {
937 ia->ia_iosize = MCD_NPORT;
938 ia->ia_msize = 0;
939 }
940
941 return (rv);
942 }
943
944 int
945 mcd_getreply(sc)
946 struct mcd_softc *sc;
947 {
948 bus_space_tag_t iot = sc->sc_iot;
949 bus_space_handle_t ioh = sc->sc_ioh;
950 int i;
951
952 /* Wait until xfer port senses data ready. */
953 for (i = DELAY_GETREPLY; i; i--) {
954 if ((bus_space_read_1(iot, ioh, MCD_XFER) &
955 MCD_XF_STATUSUNAVAIL) == 0)
956 break;
957 delay(DELAY_GRANULARITY);
958 }
959 if (!i)
960 return -1;
961
962 /* Get the data. */
963 return bus_space_read_1(iot, ioh, MCD_STATUS);
964 }
965
966 int
967 mcd_getstat(sc)
968 struct mcd_softc *sc;
969 {
970 struct mcd_mbox mbx;
971
972 mbx.cmd.opcode = MCD_CMDGETSTAT;
973 mbx.cmd.length = 0;
974 mbx.res.length = 0;
975 return mcd_send(sc, &mbx, 1);
976 }
977
978 int
979 mcd_getresult(sc, res)
980 struct mcd_softc *sc;
981 struct mcd_result *res;
982 {
983 int i, x;
984
985 if (sc->debug)
986 printf("%s: mcd_getresult: %d", sc->sc_dev.dv_xname,
987 res->length);
988
989 if ((x = mcd_getreply(sc)) < 0) {
990 if (sc->debug)
991 printf(" timeout\n");
992 else if (!sc->probe)
993 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
994 return EIO;
995 }
996 if (sc->debug)
997 printf(" %02x", (u_int)x);
998 sc->status = x;
999 mcd_setflags(sc);
1000
1001 if ((sc->status & MCD_ST_CMDCHECK) != 0)
1002 return EINVAL;
1003
1004 for (i = 0; i < res->length; i++) {
1005 if ((x = mcd_getreply(sc)) < 0) {
1006 if (sc->debug)
1007 printf(" timeout\n");
1008 else
1009 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
1010 return EIO;
1011 }
1012 if (sc->debug)
1013 printf(" %02x", (u_int)x);
1014 res->data.raw.data[i] = x;
1015 }
1016
1017 if (sc->debug)
1018 printf(" succeeded\n");
1019
1020 #ifdef MCDDEBUG
1021 delay(10);
1022 while ((bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_XFER) &
1023 MCD_XF_STATUSUNAVAIL) == 0) {
1024 x = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_STATUS);
1025 printf("%s: got extra byte %02x during getstatus\n",
1026 sc->sc_dev.dv_xname, (u_int)x);
1027 delay(10);
1028 }
1029 #endif
1030
1031 return 0;
1032 }
1033
1034 void
1035 mcd_setflags(sc)
1036 struct mcd_softc *sc;
1037 {
1038
1039 /* Check flags. */
1040 if ((sc->flags & MCDF_LOADED) != 0 &&
1041 (sc->status & (MCD_ST_DSKCHNG | MCD_ST_DSKIN | MCD_ST_DOOROPEN)) !=
1042 MCD_ST_DSKIN) {
1043 if ((sc->status & MCD_ST_DOOROPEN) != 0)
1044 printf("%s: door open\n", sc->sc_dev.dv_xname);
1045 else if ((sc->status & MCD_ST_DSKIN) == 0)
1046 printf("%s: no disk present\n", sc->sc_dev.dv_xname);
1047 else if ((sc->status & MCD_ST_DSKCHNG) != 0)
1048 printf("%s: media change\n", sc->sc_dev.dv_xname);
1049 sc->flags &= ~MCDF_LOADED;
1050 }
1051
1052 if ((sc->status & MCD_ST_AUDIOBSY) != 0)
1053 sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
1054 else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS ||
1055 sc->audio_status == CD_AS_AUDIO_INVALID)
1056 sc->audio_status = CD_AS_PLAY_COMPLETED;
1057 }
1058
1059 int
1060 mcd_send(sc, mbx, diskin)
1061 struct mcd_softc *sc;
1062 struct mcd_mbox *mbx;
1063 int diskin;
1064 {
1065 int retry, i, error;
1066 bus_space_tag_t iot = sc->sc_iot;
1067 bus_space_handle_t ioh = sc->sc_ioh;
1068
1069 if (sc->debug) {
1070 printf("%s: mcd_send: %d %02x", sc->sc_dev.dv_xname,
1071 mbx->cmd.length, (u_int)mbx->cmd.opcode);
1072 for (i = 0; i < mbx->cmd.length; i++)
1073 printf(" %02x", (u_int)mbx->cmd.data.raw.data[i]);
1074 printf("\n");
1075 }
1076
1077 for (retry = MCD_RETRIES; retry; retry--) {
1078 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.opcode);
1079 for (i = 0; i < mbx->cmd.length; i++)
1080 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.data.raw.data[i]);
1081 if ((error = mcd_getresult(sc, &mbx->res)) == 0)
1082 break;
1083 if (error == EINVAL)
1084 return error;
1085 }
1086 if (!retry)
1087 return error;
1088 if (diskin && (sc->flags & MCDF_LOADED) == 0)
1089 return EIO;
1090
1091 return 0;
1092 }
1093
1094 static int
1095 bcd2bin(b)
1096 bcd_t b;
1097 {
1098
1099 return (b >> 4) * 10 + (b & 15);
1100 }
1101
1102 static bcd_t
1103 bin2bcd(b)
1104 int b;
1105 {
1106
1107 return ((b / 10) << 4) | (b % 10);
1108 }
1109
1110 static void
1111 hsg2msf(hsg, msf)
1112 int hsg;
1113 bcd_t *msf;
1114 {
1115
1116 hsg += 150;
1117 F_msf(msf) = bin2bcd(hsg % 75);
1118 hsg /= 75;
1119 S_msf(msf) = bin2bcd(hsg % 60);
1120 hsg /= 60;
1121 M_msf(msf) = bin2bcd(hsg);
1122 }
1123
1124 static daddr_t
1125 msf2hsg(msf, relative)
1126 bcd_t *msf;
1127 int relative;
1128 {
1129 daddr_t blkno;
1130
1131 blkno = bcd2bin(M_msf(msf)) * 75 * 60 +
1132 bcd2bin(S_msf(msf)) * 75 +
1133 bcd2bin(F_msf(msf));
1134 if (!relative)
1135 blkno -= 150;
1136 return blkno;
1137 }
1138
1139 void
1140 mcd_pseudointr(v)
1141 void *v;
1142 {
1143 struct mcd_softc *sc = v;
1144 int s;
1145
1146 s = splbio();
1147 (void) mcdintr(sc);
1148 splx(s);
1149 }
1150
1151 /*
1152 * State machine to process read requests.
1153 * Initialize with MCD_S_BEGIN: calculate sizes, and set mode
1154 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
1155 * MCD_S_WAITREAD: wait for read ready, read data.
1156 */
1157 int
1158 mcdintr(arg)
1159 void *arg;
1160 {
1161 struct mcd_softc *sc = arg;
1162 struct mcd_mbx *mbx = &sc->mbx;
1163 struct buf *bp = mbx->bp;
1164 bus_space_tag_t iot = sc->sc_iot;
1165 bus_space_handle_t ioh = sc->sc_ioh;
1166
1167 int i;
1168 u_char x;
1169 bcd_t msf[3];
1170
1171 switch (mbx->state) {
1172 case MCD_S_IDLE:
1173 return 0;
1174
1175 case MCD_S_BEGIN:
1176 tryagain:
1177 if (mbx->mode == sc->lastmode)
1178 goto firstblock;
1179
1180 sc->lastmode = MCD_MD_UNKNOWN;
1181 bus_space_write_1(iot, ioh, MCD_COMMAND, MCD_CMDSETMODE);
1182 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->mode);
1183
1184 mbx->count = RDELAY_WAITMODE;
1185 mbx->state = MCD_S_WAITMODE;
1186
1187 case MCD_S_WAITMODE:
1188 callout_stop(&sc->sc_pintr_ch);
1189 for (i = 20; i; i--) {
1190 x = bus_space_read_1(iot, ioh, MCD_XFER);
1191 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1192 break;
1193 delay(50);
1194 }
1195 if (i == 0)
1196 goto hold;
1197 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS);
1198 mcd_setflags(sc);
1199 if ((sc->flags & MCDF_LOADED) == 0)
1200 goto changed;
1201 MCD_TRACE("doread: got WAITMODE delay=%d\n",
1202 RDELAY_WAITMODE - mbx->count, 0, 0, 0);
1203
1204 sc->lastmode = mbx->mode;
1205
1206 firstblock:
1207 MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", mbx->blkno,
1208 bp, 0, 0);
1209
1210 /* Build parameter block. */
1211 hsg2msf(mbx->blkno, msf);
1212
1213 /* Send the read command. */
1214 bus_space_write_1(iot, ioh, MCD_COMMAND, sc->readcmd);
1215 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[0]);
1216 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[1]);
1217 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[2]);
1218 bus_space_write_1(iot, ioh, MCD_COMMAND, 0);
1219 bus_space_write_1(iot, ioh, MCD_COMMAND, 0);
1220 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->nblk);
1221
1222 mbx->count = RDELAY_WAITREAD;
1223 mbx->state = MCD_S_WAITREAD;
1224
1225 case MCD_S_WAITREAD:
1226 callout_stop(&sc->sc_pintr_ch);
1227 nextblock:
1228 loop:
1229 for (i = 20; i; i--) {
1230 x = bus_space_read_1(iot, ioh, MCD_XFER);
1231 if ((x & MCD_XF_DATAUNAVAIL) == 0)
1232 goto gotblock;
1233 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1234 break;
1235 delay(50);
1236 }
1237 if (i == 0)
1238 goto hold;
1239 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS);
1240 mcd_setflags(sc);
1241 if ((sc->flags & MCDF_LOADED) == 0)
1242 goto changed;
1243 #if 0
1244 printf("%s: got status byte %02x during read\n",
1245 sc->sc_dev.dv_xname, (u_int)sc->status);
1246 #endif
1247 goto loop;
1248
1249 gotblock:
1250 MCD_TRACE("doread: got data delay=%d\n",
1251 RDELAY_WAITREAD - mbx->count, 0, 0, 0);
1252
1253 /* Data is ready. */
1254 bus_space_write_1(iot, ioh, MCD_CTL2, 0x04); /* XXX */
1255 bus_space_read_multi_1(iot, ioh, MCD_RDATA,
1256 bp->b_data + mbx->skip, mbx->sz);
1257 bus_space_write_1(iot, ioh, MCD_CTL2, 0x0c); /* XXX */
1258 mbx->blkno += 1;
1259 mbx->skip += mbx->sz;
1260 if (--mbx->nblk > 0)
1261 goto nextblock;
1262
1263 mbx->state = MCD_S_IDLE;
1264
1265 /* Return buffer. */
1266 bp->b_resid = 0;
1267 disk_unbusy(&sc->sc_dk, bp->b_bcount);
1268 biodone(bp);
1269
1270 mcdstart(sc);
1271 return 1;
1272
1273 hold:
1274 if (mbx->count-- < 0) {
1275 printf("%s: timeout in state %d",
1276 sc->sc_dev.dv_xname, mbx->state);
1277 goto readerr;
1278 }
1279
1280 #if 0
1281 printf("%s: sleep in state %d\n", sc->sc_dev.dv_xname,
1282 mbx->state);
1283 #endif
1284 callout_reset(&sc->sc_pintr_ch, hz / 100,
1285 mcd_pseudointr, sc);
1286 return -1;
1287 }
1288
1289 readerr:
1290 if (mbx->retry-- > 0) {
1291 printf("; retrying\n");
1292 goto tryagain;
1293 } else
1294 printf("; giving up\n");
1295
1296 changed:
1297 /* Invalidate the buffer. */
1298 bp->b_flags |= B_ERROR;
1299 bp->b_resid = bp->b_bcount - mbx->skip;
1300 disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid));
1301 biodone(bp);
1302
1303 mcdstart(sc);
1304 return -1;
1305
1306 #ifdef notyet
1307 printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname);
1308 bus_space_write_1(iot, ioh, MCD_RESET, MCD_CMDRESET);
1309 delay(300000);
1310 (void) mcd_getstat(sc, 1);
1311 (void) mcd_getstat(sc, 1);
1312 /*sc->status &= ~MCD_ST_DSKCHNG; */
1313 sc->debug = 1; /* preventive set debug mode */
1314 #endif
1315 }
1316
1317 void
1318 mcd_soft_reset(sc)
1319 struct mcd_softc *sc;
1320 {
1321
1322 sc->debug = 0;
1323 sc->flags = 0;
1324 sc->lastmode = MCD_MD_UNKNOWN;
1325 sc->lastupc = MCD_UPC_UNKNOWN;
1326 sc->audio_status = CD_AS_AUDIO_INVALID;
1327 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MCD_CTL2, 0x0c); /* XXX */
1328 }
1329
1330 int
1331 mcd_hard_reset(sc)
1332 struct mcd_softc *sc;
1333 {
1334 struct mcd_mbox mbx;
1335
1336 mcd_soft_reset(sc);
1337
1338 mbx.cmd.opcode = MCD_CMDRESET;
1339 mbx.cmd.length = 0;
1340 mbx.res.length = 0;
1341 return mcd_send(sc, &mbx, 0);
1342 }
1343
1344 int
1345 mcd_setmode(sc, mode)
1346 struct mcd_softc *sc;
1347 int mode;
1348 {
1349 struct mcd_mbox mbx;
1350 int error;
1351
1352 if (sc->lastmode == mode)
1353 return 0;
1354 if (sc->debug)
1355 printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode);
1356 sc->lastmode = MCD_MD_UNKNOWN;
1357
1358 mbx.cmd.opcode = MCD_CMDSETMODE;
1359 mbx.cmd.length = sizeof(mbx.cmd.data.datamode);
1360 mbx.cmd.data.datamode.mode = mode;
1361 mbx.res.length = 0;
1362 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1363 return error;
1364
1365 sc->lastmode = mode;
1366 return 0;
1367 }
1368
1369 int
1370 mcd_setupc(sc, upc)
1371 struct mcd_softc *sc;
1372 int upc;
1373 {
1374 struct mcd_mbox mbx;
1375 int error;
1376
1377 if (sc->lastupc == upc)
1378 return 0;
1379 if (sc->debug)
1380 printf("%s: setting upc to %d\n", sc->sc_dev.dv_xname, upc);
1381 sc->lastupc = MCD_UPC_UNKNOWN;
1382
1383 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
1384 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
1385 mbx.cmd.data.config.subcommand = MCD_CF_READUPC;
1386 mbx.cmd.data.config.data1 = upc;
1387 mbx.res.length = 0;
1388 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1389 return error;
1390
1391 sc->lastupc = upc;
1392 return 0;
1393 }
1394
1395 int
1396 mcd_toc_header(sc, th)
1397 struct mcd_softc *sc;
1398 struct ioc_toc_header *th;
1399 {
1400
1401 if (sc->debug)
1402 printf("%s: mcd_toc_header: reading toc header\n",
1403 sc->sc_dev.dv_xname);
1404
1405 th->len = msf2hsg(sc->volinfo.vol_msf, 0);
1406 th->starting_track = bcd2bin(sc->volinfo.trk_low);
1407 th->ending_track = bcd2bin(sc->volinfo.trk_high);
1408
1409 return 0;
1410 }
1411
1412 int
1413 mcd_read_toc(sc)
1414 struct mcd_softc *sc;
1415 {
1416 struct ioc_toc_header th;
1417 union mcd_qchninfo q;
1418 int error, trk, idx, retry;
1419
1420 if ((error = mcd_toc_header(sc, &th)) != 0)
1421 return error;
1422
1423 if ((error = mcd_stop(sc)) != 0)
1424 return error;
1425
1426 if (sc->debug)
1427 printf("%s: read_toc: reading qchannel info\n",
1428 sc->sc_dev.dv_xname);
1429
1430 for (trk = th.starting_track; trk <= th.ending_track; trk++)
1431 sc->toc[trk].toc.idx_no = 0x00;
1432 trk = th.ending_track - th.starting_track + 1;
1433 for (retry = 300; retry && trk > 0; retry--) {
1434 if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0)
1435 break;
1436 if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00)
1437 continue;
1438 idx = bcd2bin(q.toc.idx_no);
1439 if (idx < MCD_MAXTOCS &&
1440 sc->toc[idx].toc.idx_no == 0x00) {
1441 sc->toc[idx] = q;
1442 trk--;
1443 }
1444 }
1445
1446 /* Inform the drive that we're finished so it turns off the light. */
1447 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1448 return error;
1449
1450 if (trk != 0)
1451 return EINVAL;
1452
1453 /* Add a fake last+1 for mcd_playtracks(). */
1454 idx = th.ending_track + 1;
1455 sc->toc[idx].toc.control = sc->toc[idx-1].toc.control;
1456 sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type;
1457 sc->toc[idx].toc.trk_no = 0x00;
1458 sc->toc[idx].toc.idx_no = 0xaa;
1459 sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0];
1460 sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1];
1461 sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2];
1462
1463 return 0;
1464 }
1465
1466 int
1467 mcd_toc_entries(sc, te)
1468 struct mcd_softc *sc;
1469 struct ioc_read_toc_entry *te;
1470 {
1471 int len = te->data_len;
1472 struct ret_toc {
1473 struct ioc_toc_header header;
1474 struct cd_toc_entry entries[MCD_MAXTOCS];
1475 } data;
1476 u_char trk;
1477 daddr_t lba;
1478 int error, n;
1479
1480 if (len > sizeof(data.entries) ||
1481 len < sizeof(struct cd_toc_entry))
1482 return EINVAL;
1483 if (te->address_format != CD_MSF_FORMAT &&
1484 te->address_format != CD_LBA_FORMAT)
1485 return EINVAL;
1486
1487 /* Copy the TOC header. */
1488 if ((error = mcd_toc_header(sc, &data.header)) != 0)
1489 return error;
1490
1491 /* Verify starting track. */
1492 trk = te->starting_track;
1493 if (trk == 0x00)
1494 trk = data.header.starting_track;
1495 else if (trk == 0xaa)
1496 trk = data.header.ending_track + 1;
1497 else if (trk < data.header.starting_track ||
1498 trk > data.header.ending_track + 1)
1499 return EINVAL;
1500
1501 /* Copy the TOC data. */
1502 for (n = 0; trk <= data.header.ending_track + 1; trk++) {
1503 if (sc->toc[trk].toc.idx_no == 0x00)
1504 continue;
1505 data.entries[n].control = sc->toc[trk].toc.control;
1506 data.entries[n].addr_type = sc->toc[trk].toc.addr_type;
1507 data.entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no);
1508 switch (te->address_format) {
1509 case CD_MSF_FORMAT:
1510 data.entries[n].addr.addr[0] = 0;
1511 data.entries[n].addr.addr[1] = bcd2bin(sc->toc[trk].toc.absolute_pos[0]);
1512 data.entries[n].addr.addr[2] = bcd2bin(sc->toc[trk].toc.absolute_pos[1]);
1513 data.entries[n].addr.addr[3] = bcd2bin(sc->toc[trk].toc.absolute_pos[2]);
1514 break;
1515 case CD_LBA_FORMAT:
1516 lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0);
1517 data.entries[n].addr.addr[0] = lba >> 24;
1518 data.entries[n].addr.addr[1] = lba >> 16;
1519 data.entries[n].addr.addr[2] = lba >> 8;
1520 data.entries[n].addr.addr[3] = lba;
1521 break;
1522 }
1523 n++;
1524 }
1525
1526 len = min(len, n * sizeof(struct cd_toc_entry));
1527
1528 /* Copy the data back. */
1529 return copyout(&data.entries[0], te->data, len);
1530 }
1531
1532 int
1533 mcd_stop(sc)
1534 struct mcd_softc *sc;
1535 {
1536 struct mcd_mbox mbx;
1537 int error;
1538
1539 if (sc->debug)
1540 printf("%s: mcd_stop: stopping play\n", sc->sc_dev.dv_xname);
1541
1542 mbx.cmd.opcode = MCD_CMDSTOPAUDIO;
1543 mbx.cmd.length = 0;
1544 mbx.res.length = 0;
1545 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1546 return error;
1547
1548 sc->audio_status = CD_AS_PLAY_COMPLETED;
1549 return 0;
1550 }
1551
1552 int
1553 mcd_getqchan(sc, q, qchn)
1554 struct mcd_softc *sc;
1555 union mcd_qchninfo *q;
1556 int qchn;
1557 {
1558 struct mcd_mbox mbx;
1559 int error;
1560
1561 if (qchn == CD_TRACK_INFO) {
1562 if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0)
1563 return error;
1564 } else {
1565 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1566 return error;
1567 }
1568 if (qchn == CD_MEDIA_CATALOG) {
1569 if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0)
1570 return error;
1571 } else {
1572 if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0)
1573 return error;
1574 }
1575
1576 mbx.cmd.opcode = MCD_CMDGETQCHN;
1577 mbx.cmd.length = 0;
1578 mbx.res.length = sizeof(mbx.res.data.qchninfo);
1579 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1580 return error;
1581
1582 *q = mbx.res.data.qchninfo;
1583 return 0;
1584 }
1585
1586 int
1587 mcd_read_subchannel(sc, ch)
1588 struct mcd_softc *sc;
1589 struct ioc_read_subchannel *ch;
1590 {
1591 int len = ch->data_len;
1592 union mcd_qchninfo q;
1593 struct cd_sub_channel_info data;
1594 daddr_t lba;
1595 int error;
1596
1597 if (sc->debug)
1598 printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname,
1599 ch->address_format, ch->data_format);
1600
1601 if (len > sizeof(data) ||
1602 len < sizeof(struct cd_sub_channel_header))
1603 return EINVAL;
1604 if (ch->address_format != CD_MSF_FORMAT &&
1605 ch->address_format != CD_LBA_FORMAT)
1606 return EINVAL;
1607 if (ch->data_format != CD_CURRENT_POSITION &&
1608 ch->data_format != CD_MEDIA_CATALOG)
1609 return EINVAL;
1610
1611 if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0)
1612 return error;
1613
1614 data.header.audio_status = sc->audio_status;
1615 data.what.media_catalog.data_format = ch->data_format;
1616
1617 switch (ch->data_format) {
1618 case CD_MEDIA_CATALOG:
1619 data.what.media_catalog.mc_valid = 1;
1620 #if 0
1621 data.what.media_catalog.mc_number =
1622 #endif
1623 break;
1624
1625 case CD_CURRENT_POSITION:
1626 data.what.position.track_number = bcd2bin(q.current.trk_no);
1627 data.what.position.index_number = bcd2bin(q.current.idx_no);
1628 switch (ch->address_format) {
1629 case CD_MSF_FORMAT:
1630 data.what.position.reladdr.addr[0] = 0;
1631 data.what.position.reladdr.addr[1] = bcd2bin(q.current.relative_pos[0]);
1632 data.what.position.reladdr.addr[2] = bcd2bin(q.current.relative_pos[1]);
1633 data.what.position.reladdr.addr[3] = bcd2bin(q.current.relative_pos[2]);
1634 data.what.position.absaddr.addr[0] = 0;
1635 data.what.position.absaddr.addr[1] = bcd2bin(q.current.absolute_pos[0]);
1636 data.what.position.absaddr.addr[2] = bcd2bin(q.current.absolute_pos[1]);
1637 data.what.position.absaddr.addr[3] = bcd2bin(q.current.absolute_pos[2]);
1638 break;
1639 case CD_LBA_FORMAT:
1640 lba = msf2hsg(q.current.relative_pos, 1);
1641 /*
1642 * Pre-gap has index number of 0, and decreasing MSF
1643 * address. Must be converted to negative LBA, per
1644 * SCSI spec.
1645 */
1646 if (data.what.position.index_number == 0x00)
1647 lba = -lba;
1648 data.what.position.reladdr.addr[0] = lba >> 24;
1649 data.what.position.reladdr.addr[1] = lba >> 16;
1650 data.what.position.reladdr.addr[2] = lba >> 8;
1651 data.what.position.reladdr.addr[3] = lba;
1652 lba = msf2hsg(q.current.absolute_pos, 0);
1653 data.what.position.absaddr.addr[0] = lba >> 24;
1654 data.what.position.absaddr.addr[1] = lba >> 16;
1655 data.what.position.absaddr.addr[2] = lba >> 8;
1656 data.what.position.absaddr.addr[3] = lba;
1657 break;
1658 }
1659 break;
1660 }
1661
1662 return copyout(&data, ch->data, len);
1663 }
1664
1665 int
1666 mcd_playtracks(sc, p)
1667 struct mcd_softc *sc;
1668 struct ioc_play_track *p;
1669 {
1670 struct mcd_mbox mbx;
1671 int a = p->start_track;
1672 int z = p->end_track;
1673 int error;
1674
1675 if (sc->debug)
1676 printf("%s: playtracks: from %d:%d to %d:%d\n",
1677 sc->sc_dev.dv_xname,
1678 a, p->start_index, z, p->end_index);
1679
1680 if (a < bcd2bin(sc->volinfo.trk_low) ||
1681 a > bcd2bin(sc->volinfo.trk_high) ||
1682 a > z ||
1683 z < bcd2bin(sc->volinfo.trk_low) ||
1684 z > bcd2bin(sc->volinfo.trk_high))
1685 return EINVAL;
1686
1687 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1688 return error;
1689
1690 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1691 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1692 mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0];
1693 mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1];
1694 mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2];
1695 mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0];
1696 mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1];
1697 mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2];
1698 sc->lastpb = mbx.cmd;
1699 mbx.res.length = 0;
1700 return mcd_send(sc, &mbx, 1);
1701 }
1702
1703 int
1704 mcd_playmsf(sc, p)
1705 struct mcd_softc *sc;
1706 struct ioc_play_msf *p;
1707 {
1708 struct mcd_mbox mbx;
1709 int error;
1710
1711 if (sc->debug)
1712 printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n",
1713 sc->sc_dev.dv_xname,
1714 p->start_m, p->start_s, p->start_f,
1715 p->end_m, p->end_s, p->end_f);
1716
1717 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
1718 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f))
1719 return EINVAL;
1720
1721 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1722 return error;
1723
1724 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1725 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1726 mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m);
1727 mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s);
1728 mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f);
1729 mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m);
1730 mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s);
1731 mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f);
1732 sc->lastpb = mbx.cmd;
1733 mbx.res.length = 0;
1734 return mcd_send(sc, &mbx, 1);
1735 }
1736
1737 int
1738 mcd_playblocks(sc, p)
1739 struct mcd_softc *sc;
1740 struct ioc_play_blocks *p;
1741 {
1742 struct mcd_mbox mbx;
1743 int error;
1744
1745 if (sc->debug)
1746 printf("%s: playblocks: blkno %d length %d\n",
1747 sc->sc_dev.dv_xname, p->blk, p->len);
1748
1749 if (p->blk > sc->disksize || p->len > sc->disksize ||
1750 (p->blk + p->len) > sc->disksize)
1751 return 0;
1752
1753 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1754 return error;
1755
1756 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1757 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1758 hsg2msf(p->blk, mbx.cmd.data.play.start_msf);
1759 hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf);
1760 sc->lastpb = mbx.cmd;
1761 mbx.res.length = 0;
1762 return mcd_send(sc, &mbx, 1);
1763 }
1764
1765 int
1766 mcd_pause(sc)
1767 struct mcd_softc *sc;
1768 {
1769 union mcd_qchninfo q;
1770 int error;
1771
1772 /* Verify current status. */
1773 if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS) {
1774 printf("%s: pause: attempted when not playing\n",
1775 sc->sc_dev.dv_xname);
1776 return EINVAL;
1777 }
1778
1779 /* Get the current position. */
1780 if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0)
1781 return error;
1782
1783 /* Copy it into lastpb. */
1784 sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0];
1785 sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1];
1786 sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2];
1787
1788 /* Stop playing. */
1789 if ((error = mcd_stop(sc)) != 0)
1790 return error;
1791
1792 /* Set the proper status and exit. */
1793 sc->audio_status = CD_AS_PLAY_PAUSED;
1794 return 0;
1795 }
1796
1797 int
1798 mcd_resume(sc)
1799 struct mcd_softc *sc;
1800 {
1801 struct mcd_mbox mbx;
1802 int error;
1803
1804 if (sc->audio_status != CD_AS_PLAY_PAUSED)
1805 return EINVAL;
1806
1807 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1808 return error;
1809
1810 mbx.cmd = sc->lastpb;
1811 mbx.res.length = 0;
1812 return mcd_send(sc, &mbx, 1);
1813 }
1814
1815 int
1816 mcd_eject(sc)
1817 struct mcd_softc *sc;
1818 {
1819 struct mcd_mbox mbx;
1820
1821 mbx.cmd.opcode = MCD_CMDEJECTDISK;
1822 mbx.cmd.length = 0;
1823 mbx.res.length = 0;
1824 return mcd_send(sc, &mbx, 0);
1825 }
1826
1827 int
1828 mcd_setlock(sc, mode)
1829 struct mcd_softc *sc;
1830 int mode;
1831 {
1832 struct mcd_mbox mbx;
1833
1834 mbx.cmd.opcode = MCD_CMDSETLOCK;
1835 mbx.cmd.length = sizeof(mbx.cmd.data.lockmode);
1836 mbx.cmd.data.lockmode.mode = mode;
1837 mbx.res.length = 0;
1838 return mcd_send(sc, &mbx, 1);
1839 }
1840