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