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