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