mcd.c revision 1.31 1 /* $NetBSD: mcd.c,v 1.31 1995/03/27 18:08:34 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 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
213
214 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
215 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
216 mbx.cmd.data.config.subcommand = MCD_CF_IRQENABLE;
217 mbx.cmd.data.config.data1 = 0x01;
218 mbx.res.length = 0;
219 (void) mcd_send(sc, &mbx, 0);
220
221 mcd_soft_reset(sc);
222
223 sc->sc_dk.dk_driver = &mcddkdriver;
224
225 sc->sc_ih.ih_fun = mcdintr;
226 sc->sc_ih.ih_arg = sc;
227 sc->sc_ih.ih_level = IPL_BIO;
228 intr_establish(ia->ia_irq, IST_EDGE, &sc->sc_ih);
229 }
230
231 int
232 mcdlockwait(sc)
233 struct mcd_softc *sc;
234 {
235 int error;
236
237 while ((sc->flags & MCDF_LOCKED) != 0) {
238 sc->flags |= MCDF_WANTED;
239 if ((error = tsleep(sc, PRIBIO | PCATCH, "mcdlck", 0)) != 0)
240 return error;
241 }
242 return 0;
243 }
244
245 void
246 mcdunlock(sc)
247 struct mcd_softc *sc;
248 {
249
250 sc->flags &= ~MCDF_LOCKED;
251 if ((sc->flags & MCDF_WANTED) != 0) {
252 sc->flags &= ~MCDF_WANTED;
253 wakeup(sc);
254 }
255 }
256
257 int
258 mcdopen(dev, flag, fmt, p)
259 dev_t dev;
260 int flag, fmt;
261 struct proc *p;
262 {
263 int error;
264 int unit, part;
265 struct mcd_softc *sc;
266
267 unit = MCDUNIT(dev);
268 if (unit >= mcdcd.cd_ndevs)
269 return ENXIO;
270 sc = mcdcd.cd_devs[unit];
271 if (!sc)
272 return ENXIO;
273
274 part = MCDPART(dev);
275
276 if (error = mcdlockwait(sc))
277 return error;
278
279 if (sc->sc_dk.dk_openmask != 0) {
280 /*
281 * If any partition is open, but the disk has been invalidated,
282 * disallow further opens.
283 */
284 if ((sc->flags & MCDF_LOADED) == 0)
285 return ENXIO;
286 } else {
287 sc->flags |= MCDF_LOCKED;
288
289 /*
290 * Lock the drawer. This will also notice any pending disk
291 * change or door open indicator and clear the MCDF_LOADED bit
292 * if necessary.
293 */
294 (void) mcd_setlock(sc, MCD_LK_LOCK);
295
296 if ((sc->flags & MCDF_LOADED) == 0) {
297 /* Partially reset the state. */
298 sc->flags &= ~MCDF_HAVETOC;
299 sc->lastmode = MCD_MD_UNKNOWN;
300 sc->lastupc = MCD_UPC_UNKNOWN;
301
302 sc->flags |= MCDF_LOADED;
303
304 /* Set the mode, causing the disk to spin up. */
305 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
306 goto bad2;
307
308 /* Load the physical device parameters. */
309 if (mcd_get_parms(sc) != 0) {
310 error = ENXIO;
311 goto bad2;
312 }
313
314 /* Fabricate a disk label. */
315 mcdgetdisklabel(sc);
316 }
317
318 mcdunlock(sc);
319 }
320
321 MCD_TRACE("open: partition=%d disksize=%d blksize=%d\n", part,
322 sc->disksize, sc->blksize, 0);
323
324 /* Check that the partition exists. */
325 if (part != RAW_PART &&
326 (part >= sc->sc_dk.dk_label.d_npartitions ||
327 sc->sc_dk.dk_label.d_partitions[part].p_fstype == FS_UNUSED)) {
328 error = ENXIO;
329 goto bad;
330 }
331
332 /* Insure only one open at a time. */
333 switch (fmt) {
334 case S_IFCHR:
335 sc->sc_dk.dk_copenmask |= (1 << part);
336 break;
337 case S_IFBLK:
338 sc->sc_dk.dk_bopenmask |= (1 << part);
339 break;
340 }
341 sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
342
343 return 0;
344
345 bad2:
346 sc->flags &= ~MCDF_LOADED;
347
348 bad:
349 if (sc->sc_dk.dk_openmask == 0) {
350 #if 0
351 (void) mcd_setmode(sc, MCD_MD_SLEEP);
352 #endif
353 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
354
355 mcdunlock(sc);
356 }
357
358 return error;
359 }
360
361 int
362 mcdclose(dev, flag, fmt)
363 dev_t dev;
364 int flag, fmt;
365 {
366 struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(dev)];
367 int part = MCDPART(dev);
368 int s;
369
370 MCD_TRACE("close: partition=%d\n", part, 0, 0, 0);
371
372 switch (fmt) {
373 case S_IFCHR:
374 sc->sc_dk.dk_copenmask &= ~(1 << part);
375 break;
376 case S_IFBLK:
377 sc->sc_dk.dk_bopenmask &= ~(1 << part);
378 break;
379 }
380 sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
381
382 if (sc->sc_dk.dk_openmask == 0) {
383 /*
384 * If we're closing the last partition, nobody else could be
385 * holding the lock, so don't bother to check.
386 */
387 sc->flags |= MCDF_LOCKED;
388
389 #if 0
390 s = splbio();
391 while (...) {
392 sc->flags |= MCDF_WAITING;
393 if ((error = tsleep(sc, PRIBIO | PCATCH, "mcdcls", 0)) != 0)
394 return error;
395 }
396 splx(s);
397 #endif
398
399 #if 0
400 (void) mcd_setmode(sc, MCD_MD_SLEEP);
401 #endif
402 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
403
404 mcdunlock(sc);
405 }
406
407 return 0;
408 }
409
410 void
411 mcdstrategy(bp)
412 struct buf *bp;
413 {
414 struct mcd_softc *sc = mcdcd.cd_devs[MCDUNIT(bp->b_dev)];
415 int s;
416
417 /* Test validity. */
418 MCD_TRACE("strategy: buf=0x%lx blkno=%ld bcount=%ld\n", bp,
419 bp->b_blkno, bp->b_bcount, 0);
420 if (bp->b_blkno < 0 ||
421 (bp->b_bcount % sc->blksize) != 0) {
422 printf("%s: strategy: blkno = %d bcount = %d\n",
423 sc->sc_dev.dv_xname, bp->b_blkno, bp->b_bcount);
424 bp->b_error = EINVAL;
425 goto bad;
426 }
427
428 /* If device invalidated (e.g. media change, door open), error. */
429 if ((sc->flags & MCDF_LOADED) == 0) {
430 MCD_TRACE("strategy: drive not valid\n", 0, 0, 0, 0);
431 bp->b_error = EIO;
432 goto bad;
433 }
434
435 /* No data to read. */
436 if (bp->b_bcount == 0)
437 goto done;
438
439 /*
440 * Do bounds checking, adjust transfer. if error, process.
441 * If end of partition, just return.
442 */
443 if (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 = mcdlockwait(sc))
548 return error;
549 sc->flags |= MCDF_LOCKED | 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 irq;
709 int i;
710 struct mcd_mbox mbx;
711
712 sc->iobase = iobase;
713
714 /* Send a reset. */
715 outb(iobase + MCD_RESET, 0);
716 delay(1000000);
717 /* Get any pending status and throw away. */
718 for (i = 10; i; i--)
719 inb(iobase + MCD_STATUS);
720 delay(1000);
721
722 /* Send get status command. */
723 mbx.cmd.opcode = MCD_CMDGETSTAT;
724 mbx.cmd.length = 0;
725 mbx.res.length = 0;
726 if (mcd_send(sc, &mbx, 0) != 0)
727 return 0;
728
729 /* Get info about the drive. */
730 mbx.cmd.opcode = MCD_CMDCONTINFO;
731 mbx.cmd.length = 0;
732 mbx.res.length = sizeof(mbx.res.data.continfo);
733 if (mcd_send(sc, &mbx, 0) != 0)
734 return 0;
735
736 /*
737 * The following is code which is not guaranteed to work for all
738 * drives, because the meaning of the expected 'M' is not clear
739 * (M_itsumi is an obvious assumption, but I don't trust that).
740 * Also, the original hack had a bogus condition that always
741 * returned true.
742 *
743 * Note: Which models support interrupts? >=LU005S?
744 */
745 switch (mbx.res.data.continfo.code) {
746 case 'M':
747 if (mbx.res.data.continfo.version <= 2)
748 sc->type = "LU002S";
749 else if (mbx.res.data.continfo.version <= 5)
750 sc->type = "LU005S";
751 else
752 sc->type = "LU006S";
753 break;
754 case 'F':
755 sc->type = "FX001";
756 break;
757 case 'D':
758 sc->type = "FX001D";
759 break;
760 default:
761 printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n",
762 sc->sc_dev.dv_xname,
763 mbx.res.data.continfo.code, mbx.res.data.continfo.version);
764 sc->type = 0;
765 break;
766 }
767
768 ia->ia_iosize = 4;
769 ia->ia_msize = 0;
770 return 1;
771 }
772
773 int
774 mcd_getreply(sc)
775 struct mcd_softc *sc;
776 {
777 int iobase = sc->iobase;
778 int i;
779
780 /* Wait until xfer port senses data ready. */
781 for (i = DELAY_GETREPLY; i; i--) {
782 if ((inb(iobase + MCD_XFER) & MCD_XF_STATUSUNAVAIL) == 0)
783 break;
784 delay(DELAY_GRANULARITY);
785 }
786 if (!i)
787 return -1;
788
789 /* Get the data. */
790 return inb(iobase + MCD_STATUS);
791 }
792
793 int
794 mcd_getstat(sc)
795 struct mcd_softc *sc;
796 {
797 struct mcd_mbox mbx;
798
799 mbx.cmd.opcode = MCD_CMDGETSTAT;
800 mbx.cmd.length = 0;
801 mbx.res.length = 0;
802 return mcd_send(sc, &mbx, 1);
803 }
804
805 int
806 mcd_getresult(sc, res)
807 struct mcd_softc *sc;
808 struct mcd_result *res;
809 {
810 int i, x;
811
812 if (sc->debug)
813 printf("%s: mcd_getresult: %d", sc->sc_dev.dv_xname,
814 res->length);
815
816 if ((x = mcd_getreply(sc)) < 0) {
817 if (sc->debug)
818 printf(" timeout\n");
819 else
820 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
821 return EIO;
822 }
823 if (sc->debug)
824 printf(" %02x", (u_int)x);
825 sc->status = x;
826 mcd_setflags(sc);
827
828 if ((sc->status & MCD_ST_CMDCHECK) != 0)
829 return EINVAL;
830
831 for (i = 0; i < res->length; i++) {
832 if ((x = mcd_getreply(sc)) < 0) {
833 if (sc->debug)
834 printf(" timeout\n");
835 else
836 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
837 return EIO;
838 }
839 if (sc->debug)
840 printf(" %02x", (u_int)x);
841 res->data.raw.data[i] = x;
842 }
843
844 if (sc->debug)
845 printf(" succeeded\n");
846
847 #if 1
848 delay(10);
849 while ((inb(sc->iobase + MCD_XFER) & MCD_XF_STATUSUNAVAIL) == 0) {
850 x = inb(sc->iobase + MCD_STATUS);
851 printf("%s: got extra byte %02x during getstatus\n",
852 sc->sc_dev.dv_xname, (u_int)x);
853 delay(10);
854 }
855 #endif
856
857 return 0;
858 }
859
860 void
861 mcd_setflags(sc)
862 struct mcd_softc *sc;
863 {
864
865 /* Check flags. */
866 if ((sc->flags & MCDF_LOADED) != 0 &&
867 (sc->status & (MCD_ST_DSKCHNG | MCD_ST_DSKIN | MCD_ST_DOOROPEN)) !=
868 MCD_ST_DSKIN) {
869 if ((sc->status & MCD_ST_DOOROPEN) != 0)
870 printf("%s: door open\n", sc->sc_dev.dv_xname);
871 else if ((sc->status & MCD_ST_DSKIN) == 0)
872 printf("%s: no disk present\n", sc->sc_dev.dv_xname);
873 else if ((sc->status & MCD_ST_DSKCHNG) != 0)
874 printf("%s: media change\n", sc->sc_dev.dv_xname);
875 sc->flags &= ~MCDF_LOADED;
876 }
877
878 if ((sc->status & MCD_ST_AUDIOBSY) != 0)
879 sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
880 else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS ||
881 sc->audio_status == CD_AS_AUDIO_INVALID)
882 sc->audio_status = CD_AS_PLAY_COMPLETED;
883 }
884
885 int
886 mcd_send(sc, mbx, diskin)
887 struct mcd_softc *sc;
888 struct mcd_mbox *mbx;
889 int diskin;
890 {
891 int iobase = sc->iobase;
892 int retry, i, error;
893
894 if (sc->debug) {
895 printf("%s: mcd_send: %d %02x", sc->sc_dev.dv_xname,
896 mbx->cmd.length, (u_int)mbx->cmd.opcode);
897 for (i = 0; i < mbx->cmd.length; i++)
898 printf(" %02x", (u_int)mbx->cmd.data.raw.data[i]);
899 printf("\n");
900 }
901
902 for (retry = MCD_RETRIES; retry; retry--) {
903 outb(iobase + MCD_COMMAND, mbx->cmd.opcode);
904 for (i = 0; i < mbx->cmd.length; i++)
905 outb(iobase + MCD_COMMAND, mbx->cmd.data.raw.data[i]);
906 if ((error = mcd_getresult(sc, &mbx->res)) == 0)
907 break;
908 if (error == EINVAL)
909 return error;
910 }
911 if (!retry)
912 return error;
913 if (diskin && (sc->flags & MCDF_LOADED) == 0)
914 return EIO;
915
916 return 0;
917 }
918
919 static int
920 bcd2bin(b)
921 bcd_t b;
922 {
923
924 return (b >> 4) * 10 + (b & 15);
925 }
926
927 static bcd_t
928 bin2bcd(b)
929 int b;
930 {
931
932 return ((b / 10) << 4) | (b % 10);
933 }
934
935 static void
936 hsg2msf(hsg, msf)
937 int hsg;
938 bcd_t *msf;
939 {
940
941 hsg += 150;
942 F_msf(msf) = bin2bcd(hsg % 75);
943 hsg /= 75;
944 S_msf(msf) = bin2bcd(hsg % 60);
945 hsg /= 60;
946 M_msf(msf) = bin2bcd(hsg);
947 }
948
949 static daddr_t
950 msf2hsg(msf, relative)
951 bcd_t *msf;
952 int relative;
953 {
954 daddr_t blkno;
955
956 blkno = bcd2bin(M_msf(msf)) * 75 * 60 +
957 bcd2bin(S_msf(msf)) * 75 +
958 bcd2bin(F_msf(msf));
959 if (!relative)
960 blkno -= 150;
961 return blkno;
962 }
963
964 void
965 mcd_pseudointr(sc)
966 struct mcd_softc *sc;
967 {
968 int s;
969
970 s = splbio();
971 (void) mcdintr(sc);
972 splx(s);
973 }
974
975 /*
976 * State machine to process read requests.
977 * Initialize with MCD_S_BEGIN: calculate sizes, and set mode
978 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
979 * MCD_S_WAITREAD: wait for read ready, read data.
980 */
981 int
982 mcdintr(arg)
983 void *arg;
984 {
985 struct mcd_softc *sc = arg;
986 struct mcd_mbx *mbx = &sc->mbx;
987 int iobase = sc->iobase;
988 struct buf *bp = mbx->bp;
989
990 int i;
991 u_char x;
992 bcd_t msf[3];
993
994 switch (mbx->state) {
995 case MCD_S_IDLE:
996 return 0;
997
998 case MCD_S_BEGIN:
999 tryagain:
1000 if (mbx->mode == sc->lastmode)
1001 goto firstblock;
1002
1003 sc->lastmode = MCD_MD_UNKNOWN;
1004 outb(iobase + MCD_COMMAND, MCD_CMDSETMODE);
1005 outb(iobase + MCD_COMMAND, mbx->mode);
1006
1007 mbx->count = RDELAY_WAITMODE;
1008 mbx->state = MCD_S_WAITMODE;
1009
1010 case MCD_S_WAITMODE:
1011 untimeout(mcd_pseudointr, sc);
1012 for (i = 20; i; i--) {
1013 x = inb(iobase + MCD_XFER);
1014 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1015 break;
1016 delay(50);
1017 }
1018 if (i == 0)
1019 goto hold;
1020 sc->status = inb(iobase + MCD_STATUS);
1021 mcd_setflags(sc);
1022 if ((sc->flags & MCDF_LOADED) == 0)
1023 goto changed;
1024 MCD_TRACE("doread: got WAITMODE delay=%d\n",
1025 RDELAY_WAITMODE - mbx->count, 0, 0, 0);
1026
1027 sc->lastmode = mbx->mode;
1028
1029 firstblock:
1030 MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", mbx->blkno,
1031 bp, 0, 0);
1032
1033 /* Build parameter block. */
1034 hsg2msf(mbx->blkno, msf);
1035
1036 /* Send the read command. */
1037 outb(iobase + MCD_COMMAND, MCD_CMDREAD2);
1038 outb(iobase + MCD_COMMAND, msf[0]);
1039 outb(iobase + MCD_COMMAND, msf[1]);
1040 outb(iobase + MCD_COMMAND, msf[2]);
1041 outb(iobase + MCD_COMMAND, 0);
1042 outb(iobase + MCD_COMMAND, 0);
1043 outb(iobase + MCD_COMMAND, mbx->nblk);
1044
1045 mbx->count = RDELAY_WAITREAD;
1046 mbx->state = MCD_S_WAITREAD;
1047
1048 case MCD_S_WAITREAD:
1049 untimeout(mcd_pseudointr, sc);
1050 nextblock:
1051 loop:
1052 for (i = 20; i; i--) {
1053 x = inb(iobase + MCD_XFER);
1054 if ((x & MCD_XF_DATAUNAVAIL) == 0)
1055 goto gotblock;
1056 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1057 break;
1058 delay(50);
1059 }
1060 if (i == 0)
1061 goto hold;
1062 sc->status = inb(iobase + MCD_STATUS);
1063 mcd_setflags(sc);
1064 if ((sc->flags & MCDF_LOADED) == 0)
1065 goto changed;
1066 #if 0
1067 printf("%s: got status byte %02x during read\n",
1068 sc->sc_dev.dv_xname, (u_int)sc->status);
1069 #endif
1070 goto loop;
1071
1072 gotblock:
1073 MCD_TRACE("doread: got data delay=%d\n",
1074 RDELAY_WAITREAD - mbx->count, 0, 0, 0);
1075
1076 /* Data is ready. */
1077 outb(iobase + MCD_CTL2, 0x04); /* XXX */
1078 insb(iobase + MCD_RDATA, bp->b_data + mbx->skip, mbx->sz);
1079 outb(iobase + MCD_CTL2, 0x0c); /* XXX */
1080 mbx->blkno += 1;
1081 mbx->skip += mbx->sz;
1082 if (--mbx->nblk > 0)
1083 goto nextblock;
1084
1085 mbx->state = MCD_S_IDLE;
1086
1087 /* Return buffer. */
1088 bp->b_resid = 0;
1089 biodone(bp);
1090
1091 mcdstart(sc);
1092 return 1;
1093
1094 hold:
1095 if (mbx->count-- < 0) {
1096 printf("%s: timeout in state %d",
1097 sc->sc_dev.dv_xname, mbx->state);
1098 goto readerr;
1099 }
1100
1101 #if 0
1102 printf("%s: sleep in state %d\n", sc->sc_dev.dv_xname,
1103 mbx->state);
1104 #endif
1105 timeout(mcd_pseudointr, sc, hz / 100);
1106 return -1;
1107 }
1108
1109 readerr:
1110 if (mbx->retry-- > 0) {
1111 printf("; retrying\n");
1112 goto tryagain;
1113 } else
1114 printf("; giving up\n");
1115
1116 changed:
1117 harderr:
1118 /* Invalidate the buffer. */
1119 bp->b_flags |= B_ERROR;
1120 bp->b_resid = bp->b_bcount - mbx->skip;
1121 biodone(bp);
1122
1123 mcdstart(sc);
1124 return -1;
1125
1126 #ifdef notyet
1127 printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname);
1128 outb(mbx->iobase + MCD_RESET, MCD_CMDRESET);
1129 delay(300000);
1130 (void) mcd_getstat(sc, 1);
1131 (void) mcd_getstat(sc, 1);
1132 /*sc->status &= ~MCD_ST_DSKCHNG; */
1133 sc->debug = 1; /* preventive set debug mode */
1134 #endif
1135 }
1136
1137 void
1138 mcd_soft_reset(sc)
1139 struct mcd_softc *sc;
1140 {
1141
1142 sc->debug = 0;
1143 sc->flags = 0;
1144 sc->lastmode = MCD_MD_UNKNOWN;
1145 sc->lastupc = MCD_UPC_UNKNOWN;
1146 sc->audio_status = CD_AS_AUDIO_INVALID;
1147 outb(sc->iobase + MCD_CTL2, 0x0c); /* XXX */
1148 }
1149
1150 int
1151 mcd_hard_reset(sc)
1152 struct mcd_softc *sc;
1153 {
1154 struct mcd_mbox mbx;
1155
1156 mcd_soft_reset(sc);
1157
1158 mbx.cmd.opcode = MCD_CMDRESET;
1159 mbx.cmd.length = 0;
1160 mbx.res.length = 0;
1161 return mcd_send(sc, &mbx, 0);
1162 }
1163
1164 int
1165 mcd_setmode(sc, mode)
1166 struct mcd_softc *sc;
1167 int mode;
1168 {
1169 struct mcd_mbox mbx;
1170 int error;
1171
1172 if (sc->lastmode == mode)
1173 return 0;
1174 if (sc->debug)
1175 printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode);
1176 sc->lastmode = MCD_MD_UNKNOWN;
1177
1178 mbx.cmd.opcode = MCD_CMDSETMODE;
1179 mbx.cmd.length = sizeof(mbx.cmd.data.datamode);
1180 mbx.cmd.data.datamode.mode = mode;
1181 mbx.res.length = 0;
1182 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1183 return error;
1184
1185 sc->lastmode = mode;
1186 return 0;
1187 }
1188
1189 int
1190 mcd_setupc(sc, upc)
1191 struct mcd_softc *sc;
1192 int upc;
1193 {
1194 struct mcd_mbox mbx;
1195 int error;
1196
1197 if (sc->lastupc == upc)
1198 return 0;
1199 if (sc->debug)
1200 printf("%s: setting upc to %d\n", sc->sc_dev.dv_xname, upc);
1201 sc->lastupc = MCD_UPC_UNKNOWN;
1202
1203 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
1204 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
1205 mbx.cmd.data.config.subcommand = MCD_CF_READUPC;
1206 mbx.cmd.data.config.data1 = upc;
1207 mbx.res.length = 0;
1208 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1209 return error;
1210
1211 sc->lastupc = upc;
1212 return 0;
1213 }
1214
1215 int
1216 mcd_toc_header(sc, th)
1217 struct mcd_softc *sc;
1218 struct ioc_toc_header *th;
1219 {
1220
1221 if (sc->debug)
1222 printf("%s: mcd_toc_header: reading toc header\n",
1223 sc->sc_dev.dv_xname);
1224
1225 th->len = msf2hsg(sc->volinfo.vol_msf, 0);
1226 th->starting_track = bcd2bin(sc->volinfo.trk_low);
1227 th->ending_track = bcd2bin(sc->volinfo.trk_high);
1228
1229 return 0;
1230 }
1231
1232 int
1233 mcd_read_toc(sc)
1234 struct mcd_softc *sc;
1235 {
1236 struct ioc_toc_header th;
1237 union mcd_qchninfo q;
1238 int error, trk, idx, retry;
1239
1240 /* Only read TOC if needed. */
1241 if ((sc->flags & MCDF_HAVETOC) != 0)
1242 return 0;
1243
1244 if ((error = mcd_toc_header(sc, &th)) != 0)
1245 return error;
1246
1247 if ((error = mcd_stop(sc)) != 0)
1248 return error;
1249
1250 if (sc->debug)
1251 printf("%s: read_toc: reading qchannel info\n",
1252 sc->sc_dev.dv_xname);
1253
1254 for (trk = th.starting_track; trk <= th.ending_track; trk++)
1255 sc->toc[trk].toc.idx_no = 0x00;
1256 trk = th.ending_track - th.starting_track + 1;
1257 for (retry = 300; retry && trk > 0; retry--) {
1258 if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0)
1259 break;
1260 if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00)
1261 continue;
1262 idx = bcd2bin(q.toc.idx_no);
1263 if (idx < MCD_MAXTOCS &&
1264 sc->toc[idx].toc.idx_no == 0x00) {
1265 sc->toc[idx] = q;
1266 trk--;
1267 }
1268 }
1269
1270 if (trk != 0)
1271 return EINVAL;
1272
1273 /* Add a fake last+1 for mcd_playtracks(). */
1274 idx = th.ending_track + 1;
1275 sc->toc[idx].toc.control = sc->toc[idx-1].toc.control;
1276 sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type;
1277 sc->toc[idx].toc.trk_no = 0x00;
1278 sc->toc[idx].toc.idx_no = 0xaa;
1279 sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0];
1280 sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1];
1281 sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2];
1282
1283 sc->flags |= MCDF_HAVETOC;
1284 return 0;
1285 }
1286
1287 int
1288 mcd_toc_entries(sc, te)
1289 struct mcd_softc *sc;
1290 struct ioc_read_toc_entry *te;
1291 {
1292 int len = te->data_len;
1293 struct ret_toc {
1294 struct ioc_toc_header header;
1295 struct cd_toc_entry entries[MCD_MAXTOCS];
1296 } data;
1297 u_char trk;
1298 daddr_t lba;
1299 int error, n;
1300
1301 if (len > sizeof(data.entries) ||
1302 len < sizeof(struct cd_toc_entry))
1303 return EINVAL;
1304 if (te->address_format != CD_MSF_FORMAT &&
1305 te->address_format != CD_LBA_FORMAT)
1306 return EINVAL;
1307
1308 /* Copy the TOC header. */
1309 if ((error = mcd_toc_header(sc, &data.header)) != 0)
1310 return error;
1311
1312 /* Verify starting track. */
1313 trk = te->starting_track;
1314 if (trk == 0x00)
1315 trk = data.header.starting_track;
1316 else if (trk == 0xaa)
1317 trk = data.header.ending_track + 1;
1318 else if (trk < data.header.starting_track ||
1319 trk > data.header.ending_track + 1)
1320 return EINVAL;
1321
1322 /* Make sure we have a valid TOC. */
1323 if ((error = mcd_read_toc(sc)) != 0)
1324 return error;
1325
1326 /* Copy the TOC data. */
1327 for (n = 0; trk <= data.header.ending_track + 1; trk++) {
1328 if (sc->toc[trk].toc.idx_no == 0x00)
1329 continue;
1330 data.entries[n].control = sc->toc[trk].toc.control;
1331 data.entries[n].addr_type = sc->toc[trk].toc.addr_type;
1332 data.entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no);
1333 switch (te->address_format) {
1334 case CD_MSF_FORMAT:
1335 data.entries[n].addr[0] = 0;
1336 data.entries[n].addr[1] = bcd2bin(sc->toc[trk].toc.absolute_pos[0]);
1337 data.entries[n].addr[2] = bcd2bin(sc->toc[trk].toc.absolute_pos[1]);
1338 data.entries[n].addr[3] = bcd2bin(sc->toc[trk].toc.absolute_pos[2]);
1339 break;
1340 case CD_LBA_FORMAT:
1341 lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0);
1342 data.entries[n].addr[0] = lba >> 24;
1343 data.entries[n].addr[1] = lba >> 16;
1344 data.entries[n].addr[2] = lba >> 8;
1345 data.entries[n].addr[3] = lba;
1346 break;
1347 }
1348 n++;
1349 }
1350
1351 len = min(len, n * sizeof(struct cd_toc_entry));
1352
1353 /* Copy the data back. */
1354 return copyout(&data.entries[0], te->data, len);
1355 }
1356
1357 int
1358 mcd_stop(sc)
1359 struct mcd_softc *sc;
1360 {
1361 struct mcd_mbox mbx;
1362 int error;
1363
1364 if (sc->debug)
1365 printf("%s: mcd_stop: stopping play\n", sc->sc_dev.dv_xname);
1366
1367 mbx.cmd.opcode = MCD_CMDSTOPAUDIO;
1368 mbx.cmd.length = 0;
1369 mbx.res.length = 0;
1370 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1371 return error;
1372
1373 sc->audio_status = CD_AS_PLAY_COMPLETED;
1374 return 0;
1375 }
1376
1377 int
1378 mcd_getqchan(sc, q, qchn)
1379 struct mcd_softc *sc;
1380 union mcd_qchninfo *q;
1381 int qchn;
1382 {
1383 struct mcd_mbox mbx;
1384 int error;
1385
1386 if (qchn == CD_TRACK_INFO) {
1387 if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0)
1388 return error;
1389 } else {
1390 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1391 return error;
1392 }
1393 if (qchn == CD_MEDIA_CATALOG) {
1394 if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0)
1395 return error;
1396 } else {
1397 if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0)
1398 return error;
1399 }
1400
1401 mbx.cmd.opcode = MCD_CMDGETQCHN;
1402 mbx.cmd.length = 0;
1403 mbx.res.length = sizeof(mbx.res.data.qchninfo);
1404 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1405 return error;
1406
1407 *q = mbx.res.data.qchninfo;
1408 return 0;
1409 }
1410
1411 int
1412 mcd_read_subchannel(sc, ch)
1413 struct mcd_softc *sc;
1414 struct ioc_read_subchannel *ch;
1415 {
1416 int len = ch->data_len;
1417 union mcd_qchninfo q;
1418 struct cd_sub_channel_info data;
1419 daddr_t lba;
1420 int error;
1421
1422 if (sc->debug)
1423 printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname,
1424 ch->address_format, ch->data_format);
1425
1426 if (len > sizeof(data) ||
1427 len < sizeof(struct cd_sub_channel_header))
1428 return EINVAL;
1429 if (ch->address_format != CD_MSF_FORMAT &&
1430 ch->address_format != CD_LBA_FORMAT)
1431 return EINVAL;
1432 if (ch->data_format != CD_CURRENT_POSITION &&
1433 ch->data_format != CD_MEDIA_CATALOG)
1434 return EINVAL;
1435
1436 if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0)
1437 return error;
1438
1439 data.header.audio_status = sc->audio_status;
1440 data.what.media_catalog.data_format = ch->data_format;
1441
1442 switch (ch->data_format) {
1443 case CD_MEDIA_CATALOG:
1444 data.what.media_catalog.mc_valid = 1;
1445 #if 0
1446 data.what.media_catalog.mc_number =
1447 #endif
1448 break;
1449
1450 case CD_CURRENT_POSITION:
1451 data.what.position.track_number = bcd2bin(q.current.trk_no);
1452 data.what.position.index_number = bcd2bin(q.current.idx_no);
1453 switch (ch->address_format) {
1454 case CD_MSF_FORMAT:
1455 data.what.position.reladdr[0] = 0;
1456 data.what.position.reladdr[1] = bcd2bin(q.current.relative_pos[0]);
1457 data.what.position.reladdr[2] = bcd2bin(q.current.relative_pos[1]);
1458 data.what.position.reladdr[3] = bcd2bin(q.current.relative_pos[2]);
1459 data.what.position.absaddr[0] = 0;
1460 data.what.position.absaddr[1] = bcd2bin(q.current.absolute_pos[0]);
1461 data.what.position.absaddr[2] = bcd2bin(q.current.absolute_pos[1]);
1462 data.what.position.absaddr[3] = bcd2bin(q.current.absolute_pos[2]);
1463 break;
1464 case CD_LBA_FORMAT:
1465 lba = msf2hsg(q.current.relative_pos, 1);
1466 /*
1467 * Pre-gap has index number of 0, and decreasing MSF
1468 * address. Must be converted to negative LBA, per
1469 * SCSI spec.
1470 */
1471 if (data.what.position.index_number == 0x00)
1472 lba = -lba;
1473 data.what.position.reladdr[0] = lba >> 24;
1474 data.what.position.reladdr[1] = lba >> 16;
1475 data.what.position.reladdr[2] = lba >> 8;
1476 data.what.position.reladdr[3] = lba;
1477 lba = msf2hsg(q.current.absolute_pos, 0);
1478 data.what.position.absaddr[0] = lba >> 24;
1479 data.what.position.absaddr[1] = lba >> 16;
1480 data.what.position.absaddr[2] = lba >> 8;
1481 data.what.position.absaddr[3] = lba;
1482 break;
1483 }
1484 break;
1485 }
1486
1487 return copyout(&data, ch->data, len);
1488 }
1489
1490 int
1491 mcd_playtracks(sc, p)
1492 struct mcd_softc *sc;
1493 struct ioc_play_track *p;
1494 {
1495 struct mcd_mbox mbx;
1496 int a = p->start_track;
1497 int z = p->end_track;
1498 int error;
1499
1500 if ((error = mcd_read_toc(sc)) != 0)
1501 return error;
1502
1503 if (sc->debug)
1504 printf("%s: playtracks: from %d:%d to %d:%d\n",
1505 sc->sc_dev.dv_xname,
1506 a, p->start_index, z, p->end_index);
1507
1508 if (a < bcd2bin(sc->volinfo.trk_low) ||
1509 a > bcd2bin(sc->volinfo.trk_high) ||
1510 a > z ||
1511 z < bcd2bin(sc->volinfo.trk_low) ||
1512 z > bcd2bin(sc->volinfo.trk_high))
1513 return EINVAL;
1514
1515 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1516 return error;
1517
1518 mbx.cmd.opcode = MCD_CMDREAD2;
1519 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1520 mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0];
1521 mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1];
1522 mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2];
1523 mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0];
1524 mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1];
1525 mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2];
1526 sc->lastpb = mbx.cmd;
1527 mbx.res.length = 0;
1528 return mcd_send(sc, &mbx, 1);
1529 }
1530
1531 int
1532 mcd_playmsf(sc, p)
1533 struct mcd_softc *sc;
1534 struct ioc_play_msf *p;
1535 {
1536 struct mcd_mbox mbx;
1537 int error;
1538
1539 if ((error = mcd_read_toc(sc)) != 0)
1540 return error;
1541
1542 if (sc->debug)
1543 printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n",
1544 sc->sc_dev.dv_xname,
1545 p->start_m, p->start_s, p->start_f,
1546 p->end_m, p->end_s, p->end_f);
1547
1548 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
1549 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f))
1550 return EINVAL;
1551
1552 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1553 return error;
1554
1555 mbx.cmd.opcode = MCD_CMDREAD2;
1556 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1557 mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m);
1558 mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s);
1559 mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f);
1560 mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m);
1561 mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s);
1562 mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f);
1563 sc->lastpb = mbx.cmd;
1564 mbx.res.length = 0;
1565 return mcd_send(sc, &mbx, 1);
1566 }
1567
1568 int
1569 mcd_playblocks(sc, p)
1570 struct mcd_softc *sc;
1571 struct ioc_play_blocks *p;
1572 {
1573 struct mcd_mbox mbx;
1574 int error;
1575
1576 if ((error = mcd_read_toc(sc)) != 0)
1577 return error;
1578
1579 if (sc->debug)
1580 printf("%s: playblocks: blkno %d length %d\n",
1581 sc->sc_dev.dv_xname, p->blk, p->len);
1582
1583 if (p->blk > sc->disksize || p->len > sc->disksize ||
1584 (p->blk + p->len) > sc->disksize)
1585 return 0;
1586
1587 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1588 return error;
1589
1590 mbx.cmd.opcode = MCD_CMDREAD2;
1591 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1592 hsg2msf(p->blk, mbx.cmd.data.play.start_msf);
1593 hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf);
1594 sc->lastpb = mbx.cmd;
1595 mbx.res.length = 0;
1596 return mcd_send(sc, &mbx, 1);
1597 }
1598
1599 int
1600 mcd_pause(sc)
1601 struct mcd_softc *sc;
1602 {
1603 union mcd_qchninfo q;
1604 int error;
1605
1606 /* Verify current status. */
1607 if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS) {
1608 printf("%s: pause: attempted when not playing\n",
1609 sc->sc_dev.dv_xname);
1610 return EINVAL;
1611 }
1612
1613 /* Get the current position. */
1614 if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0)
1615 return error;
1616
1617 /* Copy it into lastpb. */
1618 sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0];
1619 sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1];
1620 sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2];
1621
1622 /* Stop playing. */
1623 if ((error = mcd_stop(sc)) != 0)
1624 return error;
1625
1626 /* Set the proper status and exit. */
1627 sc->audio_status = CD_AS_PLAY_PAUSED;
1628 return 0;
1629 }
1630
1631 int
1632 mcd_resume(sc)
1633 struct mcd_softc *sc;
1634 {
1635 struct mcd_mbox mbx;
1636 int error;
1637
1638 if (sc->audio_status != CD_AS_PLAY_PAUSED)
1639 return EINVAL;
1640
1641 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1642 return error;
1643
1644 mbx.cmd = sc->lastpb;
1645 mbx.res.length = 0;
1646 return mcd_send(sc, &mbx, 1);
1647 }
1648
1649 int
1650 mcd_eject(sc)
1651 struct mcd_softc *sc;
1652 {
1653 struct mcd_mbox mbx;
1654
1655 mbx.cmd.opcode = MCD_CMDEJECTDISK;
1656 mbx.cmd.length = 0;
1657 mbx.res.length = 0;
1658 return mcd_send(sc, &mbx, 0);
1659 }
1660
1661 int
1662 mcd_setlock(sc, mode)
1663 struct mcd_softc *sc;
1664 int mode;
1665 {
1666 struct mcd_mbox mbx;
1667
1668 mbx.cmd.opcode = MCD_CMDSETLOCK;
1669 mbx.cmd.length = sizeof(mbx.cmd.data.lockmode);
1670 mbx.cmd.data.lockmode.mode = mode;
1671 mbx.res.length = 0;
1672 return mcd_send(sc, &mbx, 1);
1673 }
1674