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