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