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