1 /* $OpenBSD: mcd.c,v 1.34 2004/05/01 23:23:13 millert Exp $ */
2 /* $NetBSD: mcd.c,v 1.60 1998/01/14 12:14:41 drochner Exp $ */
3
4 /*
5 * Copyright (c) 1993, 1994, 1995 Charles M. Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Charles M. Hannum.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * Copyright 1993 by Holger Veit (data part)
22 * Copyright 1993 by Brian Moore (audio part)
23 * All rights reserved.
24 *
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
27 * are met:
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 * 3. All advertising materials mentioning features or use of this software
34 * must display the following acknowledgement:
35 * This software was developed by Holger Veit and Brian Moore
36 * for use with "386BSD" and similar operating systems.
37 * "Similar operating systems" includes mainly non-profit oriented
38 * systems for research and education, including but not restricted to
39 * "NetBSD", "FreeBSD", "Mach" (by CMU).
40 * 4. Neither the name of the developer(s) nor the name "386BSD"
41 * may be used to endorse or promote products derived from this
42 * software without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER(S) ``AS IS'' AND ANY
45 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER(S) BE
48 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
49 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
50 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
51 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
52 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
53 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
54 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57 /*static char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";*/
58
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/mtio.h>
70 #include <sys/cdio.h>
71 #include <sys/errno.h>
72 #include <sys/disklabel.h>
73 #include <sys/device.h>
74 #include <sys/disk.h>
75 #include <sys/timeout.h>
76
77 #include <machine/cpu.h>
78 #include <machine/intr.h>
79 #include <machine/bus.h>
80
81 #include <dev/isa/isavar.h>
82 #include <dev/isa/mcdreg.h>
83 #include <dev/isa/opti.h>
84
85 #include <sys/slibkern.h>
86
87 #ifndef MCDDEBUG
88 #define MCD_TRACE(fmt,a,b,c,d)
89 #else
90 #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);}}
91 #endif
92
93 #define MCDPART(dev) DISKPART(dev)
94 #define MCDUNIT(dev) DISKUNIT(dev)
95 #define MAKEMCDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
96
97 #define MCDLABELDEV(dev) (MAKEMCDDEV(major(dev), MCDUNIT(dev), RAW_PART))
98
99 /* toc */
100 #define MCD_MAXTOCS 104 /* from the Linux driver */
101
102 struct mcd_mbx {
103 int retry, count;
104 struct buf *bp;
105 daddr_t blkno;
106 int nblk;
107 int sz;
108 u_long skip;
109 int state;
110 #define MCD_S_IDLE 0
111 #define MCD_S_BEGIN 1
112 #define MCD_S_WAITMODE 2
113 #define MCD_S_WAITREAD 3
114 int mode;
115 };
116
117 struct mcd_softc {
118 struct device sc_dev;
119 struct disk sc_dk;
120 void *sc_ih;
121 struct timeout sc_pi_tmo;
122
123 bus_space_tag_t sc_iot;
124 bus_space_handle_t sc_ioh;
125
126 int irq, drq;
127
128 char *type;
129 int flags;
130 #define MCDF_LOCKED 0x01
131 #define MCDF_WANTED 0x02
132 #define MCDF_WLABEL 0x04 /* label is writable */
133 #define MCDF_LABELLING 0x08 /* writing label */
134 #define MCDF_LOADED 0x10 /* parameters loaded */
135 #define MCDF_EJECTING 0x20 /* please eject at close */
136 short status;
137 short audio_status;
138 int blksize;
139 u_long disksize;
140 struct mcd_volinfo volinfo;
141 union mcd_qchninfo toc[MCD_MAXTOCS];
142 struct mcd_command lastpb;
143 struct mcd_mbx mbx;
144 int lastmode;
145 #define MCD_MD_UNKNOWN -1
146 int lastupc;
147 #define MCD_UPC_UNKNOWN -1
148 struct buf buf_queue;
149 u_char readcmd;
150 u_char debug;
151 u_char probe;
152 };
153
154 /* prototypes */
155 /* XXX does not belong here */
156 cdev_decl(mcd);
157 bdev_decl(mcd);
158
159 static void hsg2msf(int, bcd_t *);
160 static daddr_t msf2hsg(bcd_t *, int);
161
162 int mcd_playtracks(struct mcd_softc *, struct ioc_play_track *);
163 int mcd_playmsf(struct mcd_softc *, struct ioc_play_msf *);
164 int mcd_playblocks(struct mcd_softc *, struct ioc_play_blocks *);
165 int mcd_stop(struct mcd_softc *);
166 int mcd_eject(struct mcd_softc *);
167 int mcd_read_subchannel(struct mcd_softc *, struct ioc_read_subchannel *);
168 int mcd_pause(struct mcd_softc *);
169 int mcd_resume(struct mcd_softc *);
170 int mcd_toc_header(struct mcd_softc *, struct ioc_toc_header *);
171 int mcd_toc_entries(struct mcd_softc *, struct ioc_read_toc_entry *);
172
173 int mcd_getreply(struct mcd_softc *);
174 int mcd_getstat(struct mcd_softc *);
175 int mcd_getresult(struct mcd_softc *, struct mcd_result *);
176 void mcd_setflags(struct mcd_softc *);
177 int mcd_get(struct mcd_softc *, char *, int);
178 int mcd_send(struct mcd_softc *, struct mcd_mbox *, int);
179 int mcdintr(void *);
180 void mcd_soft_reset(struct mcd_softc *);
181 int mcd_hard_reset(struct mcd_softc *);
182 int mcd_setmode(struct mcd_softc *, int);
183 int mcd_setupc(struct mcd_softc *, int);
184 int mcd_read_toc(struct mcd_softc *);
185 int mcd_getqchan(struct mcd_softc *, union mcd_qchninfo *, int);
186 int mcd_setlock(struct mcd_softc *, int);
187
188 int mcd_find(bus_space_tag_t, bus_space_handle_t, struct mcd_softc *);
189 #define __BROKEN_INDIRECT_CONFIG
190 #ifdef __BROKEN_INDIRECT_CONFIG
191 int mcdprobe(struct device *, void *, void *);
192 #else
193 int mcdprobe(struct device *, struct cfdata *, void *);
194 #endif
195 void mcdattach(struct device *, struct device *, void *);
196
197 struct cfattach mcd_ca = {
198 sizeof(struct mcd_softc), mcdprobe, mcdattach
199 };
200
201 struct cfdriver mcd_cd = {
202 NULL, "mcd", DV_DISK
203 };
204
205 void mcdgetdisklabel(dev_t, struct mcd_softc *, struct disklabel *,
206 struct cpu_disklabel *, int);
207 int mcd_get_parms(struct mcd_softc *);
208 void mcdstrategy(struct buf *);
209 void mcdstart(struct mcd_softc *);
210 int mcdlock(struct mcd_softc *);
211 void mcdunlock(struct mcd_softc *);
212 void mcd_pseudointr(void *);
213
214 struct dkdriver mcddkdriver = { mcdstrategy };
215
216 #define MCD_RETRIES 3
217 #define MCD_RDRETRIES 3
218
219 /* several delays */
220 #define RDELAY_WAITMODE 300
221 #define RDELAY_WAITREAD 800
222
223 #define DELAY_GRANULARITY 25 /* 25us */
224 #define DELAY_GETREPLY 100000 /* 100000 * 25us */
225
226 void
mcdattach(parent,self,aux)227 mcdattach(parent, self, aux)
228 struct device *parent, *self;
229 void *aux;
230 {
231 struct mcd_softc *sc = (void *)self;
232 struct isa_attach_args *ia = aux;
233 bus_space_tag_t iot = ia->ia_iot;
234 bus_space_handle_t ioh;
235 struct mcd_mbox mbx;
236
237 /* Map i/o space */
238 if (bus_space_map(iot, ia->ia_iobase, MCD_NPORT, 0, &ioh)) {
239 printf(": can't map i/o space\n");
240 return;
241 }
242
243 sc->sc_iot = iot;
244 sc->sc_ioh = ioh;
245
246 sc->probe = 0;
247 sc->debug = 0;
248
249 if (!mcd_find(iot, ioh, sc)) {
250 printf(": mcd_find failed\n");
251 return;
252 }
253
254 timeout_set(&sc->sc_pi_tmo, mcd_pseudointr, sc);
255
256 /*
257 * Initialize and attach the disk structure.
258 */
259 sc->sc_dk.dk_driver = &mcddkdriver;
260 sc->sc_dk.dk_name = sc->sc_dev.dv_xname;
261 disk_attach(&sc->sc_dk);
262
263 dk_establish(&sc->sc_dk, &sc->sc_dev);
264
265 printf(": model %s\n", sc->type != 0 ? sc->type : "unknown");
266
267 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
268
269 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
270 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
271 mbx.cmd.data.config.subcommand = MCD_CF_IRQENABLE;
272 mbx.cmd.data.config.data1 = 0x01;
273 mbx.res.length = 0;
274 (void) mcd_send(sc, &mbx, 0);
275
276 mcd_soft_reset(sc);
277
278 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
279 IPL_BIO, mcdintr, sc, sc->sc_dev.dv_xname);
280 }
281
282 /*
283 * Wait interruptibly for an exclusive lock.
284 *
285 * XXX
286 * Several drivers do this; it should be abstracted and made MP-safe.
287 */
288 int
mcdlock(sc)289 mcdlock(sc)
290 struct mcd_softc *sc;
291 {
292 int error;
293
294 while ((sc->flags & MCDF_LOCKED) != 0) {
295 sc->flags |= MCDF_WANTED;
296 if ((error = tsleep(sc, PRIBIO | PCATCH, "mcdlck", 0)) != 0)
297 return error;
298 }
299 sc->flags |= MCDF_LOCKED;
300 return 0;
301 }
302
303 /*
304 * Unlock and wake up any waiters.
305 */
306 void
mcdunlock(sc)307 mcdunlock(sc)
308 struct mcd_softc *sc;
309 {
310
311 sc->flags &= ~MCDF_LOCKED;
312 if ((sc->flags & MCDF_WANTED) != 0) {
313 sc->flags &= ~MCDF_WANTED;
314 wakeup(sc);
315 }
316 }
317
318 int
mcdopen(dev,flag,fmt,p)319 mcdopen(dev, flag, fmt, p)
320 dev_t dev;
321 int flag, fmt;
322 struct proc *p;
323 {
324 int error;
325 int unit, part;
326 struct mcd_softc *sc;
327
328 unit = MCDUNIT(dev);
329 if (unit >= mcd_cd.cd_ndevs)
330 return ENXIO;
331 sc = mcd_cd.cd_devs[unit];
332 if (!sc)
333 return ENXIO;
334
335 if ((error = mcdlock(sc)) != 0)
336 return error;
337
338 if (sc->sc_dk.dk_openmask != 0) {
339 /*
340 * If any partition is open, but the disk has been invalidated,
341 * disallow further opens.
342 */
343 if ((sc->flags & MCDF_LOADED) == 0) {
344 error = EIO;
345 goto bad3;
346 }
347 } else {
348 /*
349 * Lock the drawer. This will also notice any pending disk
350 * change or door open indicator and clear the MCDF_LOADED bit
351 * if necessary.
352 */
353 (void) mcd_setlock(sc, MCD_LK_LOCK);
354
355 if ((sc->flags & MCDF_LOADED) == 0) {
356 /* Partially reset the state. */
357 sc->lastmode = MCD_MD_UNKNOWN;
358 sc->lastupc = MCD_UPC_UNKNOWN;
359
360 sc->flags |= MCDF_LOADED;
361
362 /* Set the mode, causing the disk to spin up. */
363 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
364 goto bad2;
365
366 /* Load the physical device parameters. */
367 if (mcd_get_parms(sc) != 0) {
368 error = ENXIO;
369 goto bad2;
370 }
371
372 /* Read the table of contents. */
373 if ((error = mcd_read_toc(sc)) != 0)
374 goto bad2;
375
376 /* Fabricate a disk label. */
377 mcdgetdisklabel(dev, sc, sc->sc_dk.dk_label,
378 sc->sc_dk.dk_cpulabel, 0);
379 }
380 }
381
382 MCD_TRACE("open: partition=%d disksize=%d blksize=%d\n", part,
383 sc->disksize, sc->blksize, 0);
384
385 part = MCDPART(dev);
386
387 /* Check that the partition exists. */
388 if (part != RAW_PART &&
389 (part >= sc->sc_dk.dk_label->d_npartitions ||
390 sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
391 error = ENXIO;
392 goto bad;
393 }
394
395 /* Insure only one open at a time. */
396 switch (fmt) {
397 case S_IFCHR:
398 sc->sc_dk.dk_copenmask |= (1 << part);
399 break;
400 case S_IFBLK:
401 sc->sc_dk.dk_bopenmask |= (1 << part);
402 break;
403 }
404 sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
405
406 mcdunlock(sc);
407 return 0;
408
409 bad2:
410 sc->flags &= ~MCDF_LOADED;
411
412 bad:
413 if (sc->sc_dk.dk_openmask == 0) {
414 #if 0
415 (void) mcd_setmode(sc, MCD_MD_SLEEP);
416 #endif
417 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
418 }
419
420 bad3:
421 mcdunlock(sc);
422 return error;
423 }
424
425 int
mcdclose(dev,flag,fmt,p)426 mcdclose(dev, flag, fmt, p)
427 dev_t dev;
428 int flag, fmt;
429 struct proc *p;
430 {
431 struct mcd_softc *sc = mcd_cd.cd_devs[MCDUNIT(dev)];
432 int part = MCDPART(dev);
433 int error;
434
435 MCD_TRACE("close: partition=%d\n", part, 0, 0, 0);
436
437 if ((error = mcdlock(sc)) != 0)
438 return error;
439
440 switch (fmt) {
441 case S_IFCHR:
442 sc->sc_dk.dk_copenmask &= ~(1 << part);
443 break;
444 case S_IFBLK:
445 sc->sc_dk.dk_bopenmask &= ~(1 << part);
446 break;
447 }
448 sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
449
450 if (sc->sc_dk.dk_openmask == 0) {
451 /* XXXX Must wait for I/O to complete! */
452
453 #if 0
454 (void) mcd_setmode(sc, MCD_MD_SLEEP);
455 #endif
456 (void) mcd_setlock(sc, MCD_LK_UNLOCK);
457 if (sc->flags & MCDF_EJECTING) {
458 mcd_eject(sc);
459 sc->flags &= ~MCDF_EJECTING;
460 }
461 }
462 mcdunlock(sc);
463 return 0;
464 }
465
466 void
mcdstrategy(bp)467 mcdstrategy(bp)
468 struct buf *bp;
469 {
470 struct mcd_softc *sc = mcd_cd.cd_devs[MCDUNIT(bp->b_dev)];
471 int s;
472
473 /* Test validity. */
474 MCD_TRACE("strategy: buf=0x%lx blkno=%ld bcount=%ld\n", bp,
475 bp->b_blkno, bp->b_bcount, 0);
476 if (bp->b_blkno < 0 ||
477 (bp->b_bcount % sc->blksize) != 0) {
478 printf("%s: strategy: blkno = %d bcount = %ld\n",
479 sc->sc_dev.dv_xname, bp->b_blkno, bp->b_bcount);
480 bp->b_error = EINVAL;
481 goto bad;
482 }
483
484 /* If device invalidated (e.g. media change, door open), error. */
485 if ((sc->flags & MCDF_LOADED) == 0) {
486 MCD_TRACE("strategy: drive not valid\n", 0, 0, 0, 0);
487 bp->b_error = EIO;
488 goto bad;
489 }
490
491 /* No data to read. */
492 if (bp->b_bcount == 0)
493 goto done;
494
495 /*
496 * Do bounds checking, adjust transfer. if error, process.
497 * If end of partition, just return.
498 */
499 if (MCDPART(bp->b_dev) != RAW_PART &&
500 bounds_check_with_label(bp, sc->sc_dk.dk_label,
501 sc->sc_dk.dk_cpulabel,
502 (sc->flags & (MCDF_WLABEL|MCDF_LABELLING)) != 0) <= 0)
503 goto done;
504
505 /* Queue it. */
506 s = splbio();
507 disksort(&sc->buf_queue, bp);
508 splx(s);
509 if (!sc->buf_queue.b_active)
510 mcdstart(sc);
511 return;
512
513 bad:
514 bp->b_flags |= B_ERROR;
515 done:
516 bp->b_resid = bp->b_bcount;
517 s = splbio();
518 biodone(bp);
519 splx(s);
520 }
521
522 void
mcdstart(sc)523 mcdstart(sc)
524 struct mcd_softc *sc;
525 {
526 struct buf *bp, *dp = &sc->buf_queue;
527 int s;
528
529 loop:
530 s = splbio();
531
532 bp = dp->b_actf;
533 if (bp == NULL) {
534 /* Nothing to do. */
535 dp->b_active = 0;
536 splx(s);
537 return;
538 }
539
540 /* Block found to process; dequeue. */
541 MCD_TRACE("start: found block bp=0x%x\n", bp, 0, 0, 0);
542 dp->b_actf = bp->b_actf;
543 splx(s);
544
545 /* Changed media? */
546 if ((sc->flags & MCDF_LOADED) == 0) {
547 MCD_TRACE("start: drive not valid\n", 0, 0, 0, 0);
548 bp->b_error = EIO;
549 bp->b_flags |= B_ERROR;
550 s = splbio();
551 biodone(bp);
552 splx(s);
553 goto loop;
554 }
555
556 dp->b_active = 1;
557
558 /* Instrumentation. */
559 s = splbio();
560 disk_busy(&sc->sc_dk);
561 splx(s);
562
563 sc->mbx.retry = MCD_RDRETRIES;
564 sc->mbx.bp = bp;
565 sc->mbx.blkno = bp->b_blkno / (sc->blksize / DEV_BSIZE);
566 if (MCDPART(bp->b_dev) != RAW_PART) {
567 struct partition *p;
568 p = &sc->sc_dk.dk_label->d_partitions[MCDPART(bp->b_dev)];
569 sc->mbx.blkno += p->p_offset;
570 }
571 sc->mbx.nblk = bp->b_bcount / sc->blksize;
572 sc->mbx.sz = sc->blksize;
573 sc->mbx.skip = 0;
574 sc->mbx.state = MCD_S_BEGIN;
575 sc->mbx.mode = MCD_MD_COOKED;
576
577 s = splbio();
578 (void) mcdintr(sc);
579 splx(s);
580 }
581
582 int
mcdread(dev,uio,flags)583 mcdread(dev, uio, flags)
584 dev_t dev;
585 struct uio *uio;
586 int flags;
587 {
588
589 return (physio(mcdstrategy, NULL, dev, B_READ, minphys, uio));
590 }
591
592 int
mcdwrite(dev,uio,flags)593 mcdwrite(dev, uio, flags)
594 dev_t dev;
595 struct uio *uio;
596 int flags;
597 {
598
599 return (physio(mcdstrategy, NULL, dev, B_WRITE, minphys, uio));
600 }
601
602 int
mcdioctl(dev,cmd,addr,flag,p)603 mcdioctl(dev, cmd, addr, flag, p)
604 dev_t dev;
605 u_long cmd;
606 caddr_t addr;
607 int flag;
608 struct proc *p;
609 {
610 struct mcd_softc *sc = mcd_cd.cd_devs[MCDUNIT(dev)];
611 int error;
612
613 MCD_TRACE("ioctl: cmd=0x%x\n", cmd, 0, 0, 0);
614
615 if ((sc->flags & MCDF_LOADED) == 0)
616 return EIO;
617
618 switch (cmd) {
619 case DIOCRLDINFO:
620 mcdgetdisklabel(dev, sc, sc->sc_dk.dk_label,
621 sc->sc_dk.dk_cpulabel, 0);
622 return 0;
623 case DIOCGDINFO:
624 case DIOCGPDINFO:
625 *(struct disklabel *)addr = *(sc->sc_dk.dk_label);
626 return 0;
627
628 case DIOCGPART:
629 ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
630 ((struct partinfo *)addr)->part =
631 &sc->sc_dk.dk_label->d_partitions[MCDPART(dev)];
632 return 0;
633
634 case DIOCWDINFO:
635 case DIOCSDINFO:
636 if ((flag & FWRITE) == 0)
637 return EBADF;
638
639 if ((error = mcdlock(sc)) != 0)
640 return error;
641 sc->flags |= MCDF_LABELLING;
642
643 error = setdisklabel(sc->sc_dk.dk_label,
644 (struct disklabel *)addr, /*sc->sc_dk.dk_openmask : */0,
645 sc->sc_dk.dk_cpulabel);
646 if (error == 0) {
647 }
648
649 sc->flags &= ~MCDF_LABELLING;
650 mcdunlock(sc);
651 return error;
652
653 case DIOCWLABEL:
654 return EBADF;
655
656 case CDIOCPLAYTRACKS:
657 return mcd_playtracks(sc, (struct ioc_play_track *)addr);
658 case CDIOCPLAYMSF:
659 return mcd_playmsf(sc, (struct ioc_play_msf *)addr);
660 case CDIOCPLAYBLOCKS:
661 return mcd_playblocks(sc, (struct ioc_play_blocks *)addr);
662 case CDIOCREADSUBCHANNEL:
663 return mcd_read_subchannel(sc, (struct ioc_read_subchannel *)addr);
664 case CDIOREADTOCHEADER:
665 return mcd_toc_header(sc, (struct ioc_toc_header *)addr);
666 case CDIOREADTOCENTRYS:
667 return mcd_toc_entries(sc, (struct ioc_read_toc_entry *)addr);
668 case CDIOCSETPATCH:
669 case CDIOCGETVOL:
670 case CDIOCSETVOL:
671 case CDIOCSETMONO:
672 case CDIOCSETSTEREO:
673 case CDIOCSETMUTE:
674 case CDIOCSETLEFT:
675 case CDIOCSETRIGHT:
676 return EINVAL;
677 case CDIOCRESUME:
678 return mcd_resume(sc);
679 case CDIOCPAUSE:
680 return mcd_pause(sc);
681 case CDIOCSTART:
682 return EINVAL;
683 case CDIOCSTOP:
684 return mcd_stop(sc);
685 case MTIOCTOP:
686 if (((struct mtop *)addr)->mt_op != MTOFFL)
687 return EIO;
688 /* FALLTHROUGH */
689 case CDIOCEJECT: /* FALLTHROUGH */
690 case DIOCEJECT:
691 sc->flags |= MCDF_EJECTING;
692 return (0);
693 case CDIOCALLOW:
694 return mcd_setlock(sc, MCD_LK_UNLOCK);
695 case CDIOCPREVENT:
696 return mcd_setlock(sc, MCD_LK_LOCK);
697 case DIOCLOCK:
698 return mcd_setlock(sc,
699 (*(int *)addr) ? MCD_LK_LOCK : MCD_LK_UNLOCK);
700 case CDIOCSETDEBUG:
701 sc->debug = 1;
702 return 0;
703 case CDIOCCLRDEBUG:
704 sc->debug = 0;
705 return 0;
706 case CDIOCRESET:
707 return mcd_hard_reset(sc);
708
709 default:
710 return ENOTTY;
711 }
712
713 #ifdef DIAGNOSTIC
714 panic("mcdioctl: impossible");
715 #endif
716 }
717
718 void
mcdgetdisklabel(dev,sc,lp,clp,spoofonly)719 mcdgetdisklabel(dev, sc, lp, clp, spoofonly)
720 dev_t dev;
721 struct mcd_softc *sc;
722 struct disklabel *lp;
723 struct cpu_disklabel *clp;
724 int spoofonly;
725 {
726 char *errstring;
727
728 bzero(lp, sizeof(struct disklabel));
729 bzero(clp, sizeof(struct cpu_disklabel));
730
731 lp->d_secsize = sc->blksize;
732 lp->d_ntracks = 1;
733 lp->d_nsectors = 100;
734 lp->d_ncylinders = (sc->disksize / 100) + 1;
735 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
736 if (lp->d_secpercyl == 0) {
737 lp->d_secpercyl = 100;
738 /* as long as it's not 0 - readdisklabel divides by it */
739 }
740
741 strncpy(lp->d_typename, "Mitsumi CD-ROM", sizeof lp->d_typename);
742 lp->d_type = DTYPE_SCSI; /* XXX */
743 strncpy(lp->d_packname, "fictitious", sizeof lp->d_packname);
744 lp->d_secperunit = sc->disksize;
745 lp->d_rpm = 300;
746 lp->d_interleave = 1;
747 lp->d_flags = D_REMOVABLE;
748
749 lp->d_partitions[RAW_PART].p_offset = 0;
750 lp->d_partitions[RAW_PART].p_size =
751 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
752 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
753 lp->d_npartitions = RAW_PART + 1;
754
755 lp->d_magic = DISKMAGIC;
756 lp->d_magic2 = DISKMAGIC;
757 lp->d_checksum = dkcksum(lp);
758
759 /*
760 * Call the generic disklabel extraction routine
761 */
762 errstring = readdisklabel(MCDLABELDEV(dev), mcdstrategy, lp, clp,
763 spoofonly);
764 if (errstring) {
765 /*printf("%s: %s\n", sc->sc_dev.dv_xname, errstring);*/
766 return;
767 }
768 }
769
770 int
mcd_get_parms(sc)771 mcd_get_parms(sc)
772 struct mcd_softc *sc;
773 {
774 struct mcd_mbox mbx;
775 daddr_t size;
776 int error;
777
778 /* Send volume info command. */
779 mbx.cmd.opcode = MCD_CMDGETVOLINFO;
780 mbx.cmd.length = 0;
781 mbx.res.length = sizeof(mbx.res.data.volinfo);
782 if ((error = mcd_send(sc, &mbx, 1)) != 0)
783 return error;
784
785 if (mbx.res.data.volinfo.trk_low == 0x00 &&
786 mbx.res.data.volinfo.trk_high == 0x00)
787 return EINVAL;
788
789 /* Volinfo is OK. */
790 sc->volinfo = mbx.res.data.volinfo;
791 sc->blksize = MCD_BLKSIZE_COOKED;
792 size = msf2hsg(sc->volinfo.vol_msf, 0);
793 sc->disksize = size * (MCD_BLKSIZE_COOKED / DEV_BSIZE);
794 return 0;
795 }
796
797 int
mcdsize(dev)798 mcdsize(dev)
799 dev_t dev;
800 {
801
802 /* CD-ROMs are read-only. */
803 return -1;
804 }
805
806 int
mcddump(dev,blkno,va,size)807 mcddump(dev, blkno, va, size)
808 dev_t dev;
809 daddr_t blkno;
810 caddr_t va;
811 size_t size;
812 {
813
814 /* Not implemented. */
815 return ENXIO;
816 }
817
818 /*
819 * Find the board and fill in the softc.
820 */
821 int
mcd_find(iot,ioh,sc)822 mcd_find(iot, ioh, sc)
823 bus_space_tag_t iot;
824 bus_space_handle_t ioh;
825 struct mcd_softc *sc;
826 {
827 int i;
828 struct mcd_mbox mbx;
829
830 sc->sc_iot = iot;
831 sc->sc_ioh = ioh;
832
833 /* Send a reset. */
834 bus_space_write_1(iot, ioh, MCD_RESET, 0);
835 delay(1000000);
836 /* Get any pending status and throw away. */
837 for (i = 10; i; i--)
838 bus_space_read_1(iot, ioh, MCD_STATUS);
839 delay(1000);
840
841 /* Send get status command. */
842 mbx.cmd.opcode = MCD_CMDGETSTAT;
843 mbx.cmd.length = 0;
844 mbx.res.length = 0;
845 if (mcd_send(sc, &mbx, 0) != 0)
846 return 0;
847
848 /* Get info about the drive. */
849 mbx.cmd.opcode = MCD_CMDCONTINFO;
850 mbx.cmd.length = 0;
851 mbx.res.length = sizeof(mbx.res.data.continfo);
852 if (mcd_send(sc, &mbx, 0) != 0)
853 return 0;
854
855 /*
856 * The following is code which is not guaranteed to work for all
857 * drives, because the meaning of the expected 'M' is not clear
858 * (M_itsumi is an obvious assumption, but I don't trust that).
859 * Also, the original hack had a bogus condition that always
860 * returned true.
861 *
862 * Note: Which models support interrupts? >=LU005S?
863 */
864 sc->readcmd = MCD_CMDREADSINGLESPEED;
865 switch (mbx.res.data.continfo.code) {
866 case 'M':
867 if (mbx.res.data.continfo.version <= 2)
868 sc->type = "LU002S";
869 else if (mbx.res.data.continfo.version <= 5)
870 sc->type = "LU005S";
871 else
872 sc->type = "LU006S";
873 break;
874 case 'F':
875 sc->type = "FX001";
876 break;
877 case 'D':
878 sc->type = "FX001D";
879 sc->readcmd = MCD_CMDREADDOUBLESPEED;
880 break;
881 default:
882 #ifdef MCDDEBUG
883 printf("%s: unrecognized drive version %c%02x; will try to use it anyway\n",
884 sc->sc_dev.dv_xname,
885 mbx.res.data.continfo.code, mbx.res.data.continfo.version);
886 #endif
887 sc->type = 0;
888 break;
889 }
890
891 return 1;
892
893 }
894
895 int
mcdprobe(parent,match,aux)896 mcdprobe(parent, match, aux)
897 struct device *parent;
898 #ifdef __BROKEN_INDIRECT_CONFIG
899 void *match;
900 #else
901 struct cfdata *match;
902 #endif
903 void *aux;
904 {
905 struct isa_attach_args *ia = aux;
906 struct mcd_softc sc;
907 bus_space_tag_t iot = ia->ia_iot;
908 bus_space_handle_t ioh;
909 int rv;
910
911 /* Disallow wildcarded i/o address. */
912 if (ia->ia_iobase == -1 /*ISACF_PORT_DEFAULT*/)
913 return (0);
914
915 /* Map i/o space */
916 if (bus_space_map(iot, ia->ia_iobase, MCD_NPORT, 0, &ioh))
917 return 0;
918
919 if (!opti_cd_setup(OPTI_MITSUMI, ia->ia_iobase, ia->ia_irq, ia->ia_drq))
920 /* printf("mcdprobe: could not setup OPTi chipset.\n") */;
921
922 sc.debug = 0;
923 sc.probe = 1;
924
925 rv = mcd_find(iot, ioh, &sc);
926
927 bus_space_unmap(iot, ioh, MCD_NPORT);
928
929 if (rv) {
930 ia->ia_iosize = MCD_NPORT;
931 ia->ia_msize = 0;
932 }
933
934 return (rv);
935 }
936
937 int
mcd_getreply(sc)938 mcd_getreply(sc)
939 struct mcd_softc *sc;
940 {
941 bus_space_tag_t iot = sc->sc_iot;
942 bus_space_handle_t ioh = sc->sc_ioh;
943 int i;
944
945 /* Wait until xfer port senses data ready. */
946 for (i = DELAY_GETREPLY; i; i--) {
947 if ((bus_space_read_1(iot, ioh, MCD_XFER) &
948 MCD_XF_STATUSUNAVAIL) == 0)
949 break;
950 delay(DELAY_GRANULARITY);
951 }
952 if (!i)
953 return -1;
954
955 /* Get the data. */
956 return bus_space_read_1(iot, ioh, MCD_STATUS);
957 }
958
959 int
mcd_getstat(sc)960 mcd_getstat(sc)
961 struct mcd_softc *sc;
962 {
963 struct mcd_mbox mbx;
964
965 mbx.cmd.opcode = MCD_CMDGETSTAT;
966 mbx.cmd.length = 0;
967 mbx.res.length = 0;
968 return mcd_send(sc, &mbx, 1);
969 }
970
971 int
mcd_getresult(sc,res)972 mcd_getresult(sc, res)
973 struct mcd_softc *sc;
974 struct mcd_result *res;
975 {
976 int i, x;
977
978 if (sc->debug)
979 printf("%s: mcd_getresult: %d", sc->sc_dev.dv_xname,
980 res->length);
981
982 if ((x = mcd_getreply(sc)) < 0) {
983 if (sc->debug)
984 printf(" timeout\n");
985 else if (sc->probe)
986 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
987 return EIO;
988 }
989 if (sc->debug)
990 printf(" %02x", (u_int)x);
991 sc->status = x;
992 mcd_setflags(sc);
993
994 if ((sc->status & MCD_ST_CMDCHECK) != 0)
995 return EINVAL;
996
997 for (i = 0; i < res->length; i++) {
998 if ((x = mcd_getreply(sc)) < 0) {
999 if (sc->debug)
1000 printf(" timeout\n");
1001 else
1002 printf("%s: timeout in getresult\n", sc->sc_dev.dv_xname);
1003 return EIO;
1004 }
1005 if (sc->debug)
1006 printf(" %02x", (u_int)x);
1007 res->data.raw.data[i] = x;
1008 }
1009
1010 if (sc->debug)
1011 printf(" succeeded\n");
1012
1013 #ifdef MCDDEBUG
1014 delay(10);
1015 while ((bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_XFER) &
1016 MCD_XF_STATUSUNAVAIL) == 0) {
1017 x = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MCD_STATUS);
1018 printf("%s: got extra byte %02x during getstatus\n",
1019 sc->sc_dev.dv_xname, (u_int)x);
1020 delay(10);
1021 }
1022 #endif
1023
1024 return 0;
1025 }
1026
1027 void
mcd_setflags(sc)1028 mcd_setflags(sc)
1029 struct mcd_softc *sc;
1030 {
1031
1032 /* Check flags. */
1033 if ((sc->flags & MCDF_LOADED) != 0 &&
1034 (sc->status & (MCD_ST_DSKCHNG | MCD_ST_DSKIN | MCD_ST_DOOROPEN)) !=
1035 MCD_ST_DSKIN) {
1036 if ((sc->status & MCD_ST_DOOROPEN) != 0)
1037 printf("%s: door open\n", sc->sc_dev.dv_xname);
1038 else if ((sc->status & MCD_ST_DSKIN) == 0)
1039 printf("%s: no disk present\n", sc->sc_dev.dv_xname);
1040 else if ((sc->status & MCD_ST_DSKCHNG) != 0)
1041 printf("%s: media change\n", sc->sc_dev.dv_xname);
1042 sc->flags &= ~MCDF_LOADED;
1043 }
1044
1045 if ((sc->status & MCD_ST_AUDIOBSY) != 0)
1046 sc->audio_status = CD_AS_PLAY_IN_PROGRESS;
1047 else if (sc->audio_status == CD_AS_PLAY_IN_PROGRESS ||
1048 sc->audio_status == CD_AS_AUDIO_INVALID)
1049 sc->audio_status = CD_AS_PLAY_COMPLETED;
1050 }
1051
1052 int
mcd_send(sc,mbx,diskin)1053 mcd_send(sc, mbx, diskin)
1054 struct mcd_softc *sc;
1055 struct mcd_mbox *mbx;
1056 int diskin;
1057 {
1058 int retry, i, error;
1059 bus_space_tag_t iot = sc->sc_iot;
1060 bus_space_handle_t ioh = sc->sc_ioh;
1061
1062 if (sc->debug) {
1063 printf("%s: mcd_send: %d %02x", sc->sc_dev.dv_xname,
1064 mbx->cmd.length, (u_int)mbx->cmd.opcode);
1065 for (i = 0; i < mbx->cmd.length; i++)
1066 printf(" %02x", (u_int)mbx->cmd.data.raw.data[i]);
1067 printf("\n");
1068 }
1069
1070 for (retry = MCD_RETRIES; retry; retry--) {
1071 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.opcode);
1072 for (i = 0; i < mbx->cmd.length; i++)
1073 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->cmd.data.raw.data[i]);
1074 if ((error = mcd_getresult(sc, &mbx->res)) == 0)
1075 break;
1076 if (error == EINVAL)
1077 return error;
1078 }
1079 if (!retry)
1080 return error;
1081 if (diskin && (sc->flags & MCDF_LOADED) == 0)
1082 return EIO;
1083
1084 return 0;
1085 }
1086
1087 static void
hsg2msf(hsg,msf)1088 hsg2msf(hsg, msf)
1089 int hsg;
1090 bcd_t *msf;
1091 {
1092
1093 hsg += 150;
1094 F_msf(msf) = bin2bcd(hsg % 75);
1095 hsg /= 75;
1096 S_msf(msf) = bin2bcd(hsg % 60);
1097 hsg /= 60;
1098 M_msf(msf) = bin2bcd(hsg);
1099 }
1100
1101 static daddr_t
msf2hsg(msf,relative)1102 msf2hsg(msf, relative)
1103 bcd_t *msf;
1104 int relative;
1105 {
1106 daddr_t blkno;
1107
1108 blkno = bcd2bin(M_msf(msf)) * 75 * 60 +
1109 bcd2bin(S_msf(msf)) * 75 +
1110 bcd2bin(F_msf(msf));
1111 if (!relative)
1112 blkno -= 150;
1113 return blkno;
1114 }
1115
1116 void
mcd_pseudointr(v)1117 mcd_pseudointr(v)
1118 void *v;
1119 {
1120 struct mcd_softc *sc = v;
1121 int s;
1122
1123 s = splbio();
1124 (void) mcdintr(sc);
1125 splx(s);
1126 }
1127
1128 /*
1129 * State machine to process read requests.
1130 * Initialize with MCD_S_BEGIN: calculate sizes, and set mode
1131 * MCD_S_WAITMODE: waits for status reply from set mode, set read command
1132 * MCD_S_WAITREAD: wait for read ready, read data.
1133 */
1134 int
mcdintr(arg)1135 mcdintr(arg)
1136 void *arg;
1137 {
1138 struct mcd_softc *sc = arg;
1139 struct mcd_mbx *mbx = &sc->mbx;
1140 struct buf *bp = mbx->bp;
1141 bus_space_tag_t iot = sc->sc_iot;
1142 bus_space_handle_t ioh = sc->sc_ioh;
1143
1144 int i;
1145 u_char x;
1146 bcd_t msf[3];
1147
1148 switch (mbx->state) {
1149 case MCD_S_IDLE:
1150 return 0;
1151
1152 case MCD_S_BEGIN:
1153 tryagain:
1154 if (mbx->mode == sc->lastmode)
1155 goto firstblock;
1156
1157 sc->lastmode = MCD_MD_UNKNOWN;
1158 bus_space_write_1(iot, ioh, MCD_COMMAND, MCD_CMDSETMODE);
1159 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->mode);
1160
1161 mbx->count = RDELAY_WAITMODE;
1162 mbx->state = MCD_S_WAITMODE;
1163
1164 case MCD_S_WAITMODE:
1165 timeout_del(&sc->sc_pi_tmo);
1166 for (i = 20; i; i--) {
1167 x = bus_space_read_1(iot, ioh, MCD_XFER);
1168 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1169 break;
1170 delay(50);
1171 }
1172 if (i == 0)
1173 goto hold;
1174 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS);
1175 mcd_setflags(sc);
1176 if ((sc->flags & MCDF_LOADED) == 0)
1177 goto changed;
1178 MCD_TRACE("doread: got WAITMODE delay=%d\n",
1179 RDELAY_WAITMODE - mbx->count, 0, 0, 0);
1180
1181 sc->lastmode = mbx->mode;
1182
1183 firstblock:
1184 MCD_TRACE("doread: read blkno=%d for bp=0x%x\n", mbx->blkno,
1185 bp, 0, 0);
1186
1187 /* Build parameter block. */
1188 hsg2msf(mbx->blkno, msf);
1189
1190 /* Send the read command. */
1191 bus_space_write_1(iot, ioh, MCD_COMMAND, sc->readcmd);
1192 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[0]);
1193 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[1]);
1194 bus_space_write_1(iot, ioh, MCD_COMMAND, msf[2]);
1195 bus_space_write_1(iot, ioh, MCD_COMMAND, 0);
1196 bus_space_write_1(iot, ioh, MCD_COMMAND, 0);
1197 bus_space_write_1(iot, ioh, MCD_COMMAND, mbx->nblk);
1198
1199 mbx->count = RDELAY_WAITREAD;
1200 mbx->state = MCD_S_WAITREAD;
1201
1202 case MCD_S_WAITREAD:
1203 timeout_del(&sc->sc_pi_tmo);
1204 nextblock:
1205 loop:
1206 for (i = 20; i; i--) {
1207 x = bus_space_read_1(iot, ioh, MCD_XFER);
1208 if ((x & MCD_XF_DATAUNAVAIL) == 0)
1209 goto gotblock;
1210 if ((x & MCD_XF_STATUSUNAVAIL) == 0)
1211 break;
1212 delay(50);
1213 }
1214 if (i == 0)
1215 goto hold;
1216 sc->status = bus_space_read_1(iot, ioh, MCD_STATUS);
1217 mcd_setflags(sc);
1218 if ((sc->flags & MCDF_LOADED) == 0)
1219 goto changed;
1220 #if 0
1221 printf("%s: got status byte %02x during read\n",
1222 sc->sc_dev.dv_xname, (u_int)sc->status);
1223 #endif
1224 goto loop;
1225
1226 gotblock:
1227 MCD_TRACE("doread: got data delay=%d\n",
1228 RDELAY_WAITREAD - mbx->count, 0, 0, 0);
1229
1230 /* Data is ready. */
1231 bus_space_write_1(iot, ioh, MCD_CTL2, 0x04); /* XXX */
1232 bus_space_read_multi_1(iot, ioh, MCD_RDATA,
1233 bp->b_data + mbx->skip, mbx->sz);
1234 bus_space_write_1(iot, ioh, MCD_CTL2, 0x0c); /* XXX */
1235 mbx->blkno += 1;
1236 mbx->skip += mbx->sz;
1237 if (--mbx->nblk > 0)
1238 goto nextblock;
1239
1240 mbx->state = MCD_S_IDLE;
1241
1242 /* Return buffer. */
1243 bp->b_resid = 0;
1244 disk_unbusy(&sc->sc_dk, bp->b_bcount, (bp->b_flags & B_READ));
1245 biodone(bp);
1246
1247 mcdstart(sc);
1248 return 1;
1249
1250 hold:
1251 if (mbx->count-- < 0) {
1252 printf("%s: timeout in state %d",
1253 sc->sc_dev.dv_xname, mbx->state);
1254 goto readerr;
1255 }
1256
1257 #if 0
1258 printf("%s: sleep in state %d\n", sc->sc_dev.dv_xname,
1259 mbx->state);
1260 #endif
1261 timeout_add(&sc->sc_pi_tmo, hz / 100);
1262 return -1;
1263 }
1264
1265 readerr:
1266 if (mbx->retry-- > 0) {
1267 printf("; retrying\n");
1268 goto tryagain;
1269 } else
1270 printf("; giving up\n");
1271
1272 changed:
1273 /* Invalidate the buffer. */
1274 bp->b_flags |= B_ERROR;
1275 bp->b_resid = bp->b_bcount - mbx->skip;
1276 disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid),
1277 (bp->b_flags & B_READ));
1278 biodone(bp);
1279
1280 mcdstart(sc);
1281 return -1;
1282
1283 #ifdef notyet
1284 printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname);
1285 bus_space_write_1(iot, ioh, MCD_RESET, MCD_CMDRESET);
1286 delay(300000);
1287 (void) mcd_getstat(sc, 1);
1288 (void) mcd_getstat(sc, 1);
1289 /*sc->status &= ~MCD_ST_DSKCHNG; */
1290 sc->debug = 1; /* preventive set debug mode */
1291 #endif
1292 }
1293
1294 void
mcd_soft_reset(sc)1295 mcd_soft_reset(sc)
1296 struct mcd_softc *sc;
1297 {
1298
1299 sc->debug = 0;
1300 sc->flags = 0;
1301 sc->lastmode = MCD_MD_UNKNOWN;
1302 sc->lastupc = MCD_UPC_UNKNOWN;
1303 sc->audio_status = CD_AS_AUDIO_INVALID;
1304 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MCD_CTL2, 0x0c); /* XXX */
1305 }
1306
1307 int
mcd_hard_reset(sc)1308 mcd_hard_reset(sc)
1309 struct mcd_softc *sc;
1310 {
1311 struct mcd_mbox mbx;
1312
1313 mcd_soft_reset(sc);
1314
1315 mbx.cmd.opcode = MCD_CMDRESET;
1316 mbx.cmd.length = 0;
1317 mbx.res.length = 0;
1318 return mcd_send(sc, &mbx, 0);
1319 }
1320
1321 int
mcd_setmode(sc,mode)1322 mcd_setmode(sc, mode)
1323 struct mcd_softc *sc;
1324 int mode;
1325 {
1326 struct mcd_mbox mbx;
1327 int error;
1328
1329 if (sc->lastmode == mode)
1330 return 0;
1331 if (sc->debug)
1332 printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode);
1333 sc->lastmode = MCD_MD_UNKNOWN;
1334
1335 mbx.cmd.opcode = MCD_CMDSETMODE;
1336 mbx.cmd.length = sizeof(mbx.cmd.data.datamode);
1337 mbx.cmd.data.datamode.mode = mode;
1338 mbx.res.length = 0;
1339 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1340 return error;
1341
1342 sc->lastmode = mode;
1343 return 0;
1344 }
1345
1346 int
mcd_setupc(sc,upc)1347 mcd_setupc(sc, upc)
1348 struct mcd_softc *sc;
1349 int upc;
1350 {
1351 struct mcd_mbox mbx;
1352 int error;
1353
1354 if (sc->lastupc == upc)
1355 return 0;
1356 if (sc->debug)
1357 printf("%s: setting upc to %d\n", sc->sc_dev.dv_xname, upc);
1358 sc->lastupc = MCD_UPC_UNKNOWN;
1359
1360 mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
1361 mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
1362 mbx.cmd.data.config.subcommand = MCD_CF_READUPC;
1363 mbx.cmd.data.config.data1 = upc;
1364 mbx.res.length = 0;
1365 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1366 return error;
1367
1368 sc->lastupc = upc;
1369 return 0;
1370 }
1371
1372 int
mcd_toc_header(sc,th)1373 mcd_toc_header(sc, th)
1374 struct mcd_softc *sc;
1375 struct ioc_toc_header *th;
1376 {
1377
1378 if (sc->debug)
1379 printf("%s: mcd_toc_header: reading toc header\n",
1380 sc->sc_dev.dv_xname);
1381
1382 th->len = msf2hsg(sc->volinfo.vol_msf, 0);
1383 th->starting_track = bcd2bin(sc->volinfo.trk_low);
1384 th->ending_track = bcd2bin(sc->volinfo.trk_high);
1385
1386 return 0;
1387 }
1388
1389 int
mcd_read_toc(sc)1390 mcd_read_toc(sc)
1391 struct mcd_softc *sc;
1392 {
1393 struct ioc_toc_header th;
1394 union mcd_qchninfo q;
1395 int error, trk, idx, retry;
1396
1397 if ((error = mcd_toc_header(sc, &th)) != 0)
1398 return error;
1399
1400 if ((error = mcd_stop(sc)) != 0)
1401 return error;
1402
1403 if (sc->debug)
1404 printf("%s: read_toc: reading qchannel info\n",
1405 sc->sc_dev.dv_xname);
1406
1407 for (trk = th.starting_track; trk <= th.ending_track; trk++)
1408 sc->toc[trk].toc.idx_no = 0x00;
1409 trk = th.ending_track - th.starting_track + 1;
1410 for (retry = 300; retry && trk > 0; retry--) {
1411 if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0)
1412 break;
1413 if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00)
1414 continue;
1415 idx = bcd2bin(q.toc.idx_no);
1416 if (idx < MCD_MAXTOCS &&
1417 sc->toc[idx].toc.idx_no == 0x00) {
1418 sc->toc[idx] = q;
1419 trk--;
1420 }
1421 }
1422
1423 /* Inform the drive that we're finished so it turns off the light. */
1424 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1425 return error;
1426
1427 if (trk != 0)
1428 return EINVAL;
1429
1430 /* Add a fake last+1 for mcd_playtracks(). */
1431 idx = th.ending_track + 1;
1432 sc->toc[idx].toc.control = sc->toc[idx-1].toc.control;
1433 sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type;
1434 sc->toc[idx].toc.trk_no = 0x00;
1435 sc->toc[idx].toc.idx_no = 0xaa;
1436 sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0];
1437 sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1];
1438 sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2];
1439
1440 return 0;
1441 }
1442
1443 int
mcd_toc_entries(sc,te)1444 mcd_toc_entries(sc, te)
1445 struct mcd_softc *sc;
1446 struct ioc_read_toc_entry *te;
1447 {
1448 int len = te->data_len;
1449 struct ret_toc {
1450 struct ioc_toc_header header;
1451 struct cd_toc_entry entries[MCD_MAXTOCS];
1452 } data;
1453 u_char trk;
1454 daddr_t lba;
1455 int error, n;
1456
1457 if (len > sizeof(data.entries) ||
1458 len < sizeof(struct cd_toc_entry))
1459 return EINVAL;
1460 if (te->address_format != CD_MSF_FORMAT &&
1461 te->address_format != CD_LBA_FORMAT)
1462 return EINVAL;
1463
1464 /* Copy the TOC header. */
1465 if ((error = mcd_toc_header(sc, &data.header)) != 0)
1466 return error;
1467
1468 /* Verify starting track. */
1469 trk = te->starting_track;
1470 if (trk == 0x00)
1471 trk = data.header.starting_track;
1472 else if (trk == 0xaa)
1473 trk = data.header.ending_track + 1;
1474 else if (trk < data.header.starting_track ||
1475 trk > data.header.ending_track + 1)
1476 return EINVAL;
1477
1478 /* Copy the TOC data. */
1479 for (n = 0; trk <= data.header.ending_track + 1; trk++) {
1480 if (sc->toc[trk].toc.idx_no == 0x00)
1481 continue;
1482 data.entries[n].control = sc->toc[trk].toc.control;
1483 data.entries[n].addr_type = sc->toc[trk].toc.addr_type;
1484 data.entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no);
1485 switch (te->address_format) {
1486 case CD_MSF_FORMAT:
1487 data.entries[n].addr.addr[0] = 0;
1488 data.entries[n].addr.addr[1] =
1489 bcd2bin(sc->toc[trk].toc.absolute_pos[0]);
1490 data.entries[n].addr.addr[2] =
1491 bcd2bin(sc->toc[trk].toc.absolute_pos[1]);
1492 data.entries[n].addr.addr[3] =
1493 bcd2bin(sc->toc[trk].toc.absolute_pos[2]);
1494 break;
1495 case CD_LBA_FORMAT:
1496 lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0);
1497 data.entries[n].addr.addr[0] = lba >> 24;
1498 data.entries[n].addr.addr[1] = lba >> 16;
1499 data.entries[n].addr.addr[2] = lba >> 8;
1500 data.entries[n].addr.addr[3] = lba;
1501 break;
1502 }
1503 n++;
1504 }
1505
1506 len = min(len, n * sizeof(struct cd_toc_entry));
1507
1508 /* Copy the data back. */
1509 return copyout(&data.entries[0], te->data, len);
1510 }
1511
1512 int
mcd_stop(sc)1513 mcd_stop(sc)
1514 struct mcd_softc *sc;
1515 {
1516 struct mcd_mbox mbx;
1517 int error;
1518
1519 if (sc->debug)
1520 printf("%s: mcd_stop: stopping play\n", sc->sc_dev.dv_xname);
1521
1522 mbx.cmd.opcode = MCD_CMDSTOPAUDIO;
1523 mbx.cmd.length = 0;
1524 mbx.res.length = 0;
1525 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1526 return error;
1527
1528 sc->audio_status = CD_AS_PLAY_COMPLETED;
1529 return 0;
1530 }
1531
1532 int
mcd_getqchan(sc,q,qchn)1533 mcd_getqchan(sc, q, qchn)
1534 struct mcd_softc *sc;
1535 union mcd_qchninfo *q;
1536 int qchn;
1537 {
1538 struct mcd_mbox mbx;
1539 int error;
1540
1541 if (qchn == CD_TRACK_INFO) {
1542 if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0)
1543 return error;
1544 } else {
1545 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1546 return error;
1547 }
1548 if (qchn == CD_MEDIA_CATALOG) {
1549 if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0)
1550 return error;
1551 } else {
1552 if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0)
1553 return error;
1554 }
1555
1556 mbx.cmd.opcode = MCD_CMDGETQCHN;
1557 mbx.cmd.length = 0;
1558 mbx.res.length = sizeof(mbx.res.data.qchninfo);
1559 if ((error = mcd_send(sc, &mbx, 1)) != 0)
1560 return error;
1561
1562 *q = mbx.res.data.qchninfo;
1563 return 0;
1564 }
1565
1566 int
mcd_read_subchannel(sc,ch)1567 mcd_read_subchannel(sc, ch)
1568 struct mcd_softc *sc;
1569 struct ioc_read_subchannel *ch;
1570 {
1571 int len = ch->data_len;
1572 union mcd_qchninfo q;
1573 struct cd_sub_channel_info data;
1574 daddr_t lba;
1575 int error;
1576
1577 if (sc->debug)
1578 printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname,
1579 ch->address_format, ch->data_format);
1580
1581 if (len > sizeof(data) ||
1582 len < sizeof(struct cd_sub_channel_header))
1583 return EINVAL;
1584 if (ch->address_format != CD_MSF_FORMAT &&
1585 ch->address_format != CD_LBA_FORMAT)
1586 return EINVAL;
1587 if (ch->data_format != CD_CURRENT_POSITION &&
1588 ch->data_format != CD_MEDIA_CATALOG)
1589 return EINVAL;
1590
1591 if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0)
1592 return error;
1593
1594 data.header.audio_status = sc->audio_status;
1595 data.what.media_catalog.data_format = ch->data_format;
1596
1597 switch (ch->data_format) {
1598 case CD_MEDIA_CATALOG:
1599 data.what.media_catalog.mc_valid = 1;
1600 #if 0
1601 data.what.media_catalog.mc_number =
1602 #endif
1603 break;
1604
1605 case CD_CURRENT_POSITION:
1606 data.what.position.track_number = bcd2bin(q.current.trk_no);
1607 data.what.position.index_number = bcd2bin(q.current.idx_no);
1608 switch (ch->address_format) {
1609 case CD_MSF_FORMAT:
1610 data.what.position.reladdr.addr[0] = 0;
1611 data.what.position.reladdr.addr[1] =
1612 bcd2bin(q.current.relative_pos[0]);
1613 data.what.position.reladdr.addr[2] =
1614 bcd2bin(q.current.relative_pos[1]);
1615 data.what.position.reladdr.addr[3] =
1616 bcd2bin(q.current.relative_pos[2]);
1617 data.what.position.absaddr.addr[0] = 0;
1618 data.what.position.absaddr.addr[1] =
1619 bcd2bin(q.current.absolute_pos[0]);
1620 data.what.position.absaddr.addr[2] =
1621 bcd2bin(q.current.absolute_pos[1]);
1622 data.what.position.absaddr.addr[3] =
1623 bcd2bin(q.current.absolute_pos[2]);
1624 break;
1625 case CD_LBA_FORMAT:
1626 lba = msf2hsg(q.current.relative_pos, 1);
1627 /*
1628 * Pre-gap has index number of 0, and decreasing MSF
1629 * address. Must be converted to negative LBA, per
1630 * SCSI spec.
1631 */
1632 if (data.what.position.index_number == 0x00)
1633 lba = -lba;
1634 data.what.position.reladdr.addr[0] = lba >> 24;
1635 data.what.position.reladdr.addr[1] = lba >> 16;
1636 data.what.position.reladdr.addr[2] = lba >> 8;
1637 data.what.position.reladdr.addr[3] = lba;
1638 lba = msf2hsg(q.current.absolute_pos, 0);
1639 data.what.position.absaddr.addr[0] = lba >> 24;
1640 data.what.position.absaddr.addr[1] = lba >> 16;
1641 data.what.position.absaddr.addr[2] = lba >> 8;
1642 data.what.position.absaddr.addr[3] = lba;
1643 break;
1644 }
1645 break;
1646 }
1647
1648 return copyout(&data, ch->data, len);
1649 }
1650
1651 int
mcd_playtracks(sc,p)1652 mcd_playtracks(sc, p)
1653 struct mcd_softc *sc;
1654 struct ioc_play_track *p;
1655 {
1656 struct mcd_mbox mbx;
1657 int a = p->start_track;
1658 int z = p->end_track;
1659 int error;
1660
1661 if (sc->debug)
1662 printf("%s: playtracks: from %d:%d to %d:%d\n",
1663 sc->sc_dev.dv_xname,
1664 a, p->start_index, z, p->end_index);
1665
1666 if (a < bcd2bin(sc->volinfo.trk_low) ||
1667 a > bcd2bin(sc->volinfo.trk_high) ||
1668 a > z ||
1669 z < bcd2bin(sc->volinfo.trk_low) ||
1670 z > bcd2bin(sc->volinfo.trk_high))
1671 return EINVAL;
1672
1673 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1674 return error;
1675
1676 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1677 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1678 mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0];
1679 mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1];
1680 mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2];
1681 mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0];
1682 mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1];
1683 mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2];
1684 sc->lastpb = mbx.cmd;
1685 mbx.res.length = 0;
1686 return mcd_send(sc, &mbx, 1);
1687 }
1688
1689 int
mcd_playmsf(sc,p)1690 mcd_playmsf(sc, p)
1691 struct mcd_softc *sc;
1692 struct ioc_play_msf *p;
1693 {
1694 struct mcd_mbox mbx;
1695 int error;
1696
1697 if (sc->debug)
1698 printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n",
1699 sc->sc_dev.dv_xname,
1700 p->start_m, p->start_s, p->start_f,
1701 p->end_m, p->end_s, p->end_f);
1702
1703 if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
1704 (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f))
1705 return EINVAL;
1706
1707 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1708 return error;
1709
1710 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1711 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1712 mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m);
1713 mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s);
1714 mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f);
1715 mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m);
1716 mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s);
1717 mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f);
1718 sc->lastpb = mbx.cmd;
1719 mbx.res.length = 0;
1720 return mcd_send(sc, &mbx, 1);
1721 }
1722
1723 int
mcd_playblocks(sc,p)1724 mcd_playblocks(sc, p)
1725 struct mcd_softc *sc;
1726 struct ioc_play_blocks *p;
1727 {
1728 struct mcd_mbox mbx;
1729 int error;
1730
1731 if (sc->debug)
1732 printf("%s: playblocks: blkno %d length %d\n",
1733 sc->sc_dev.dv_xname, p->blk, p->len);
1734
1735 if (p->blk > sc->disksize || p->len > sc->disksize ||
1736 (p->blk + p->len) > sc->disksize)
1737 return 0;
1738
1739 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1740 return error;
1741
1742 mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1743 mbx.cmd.length = sizeof(mbx.cmd.data.play);
1744 hsg2msf(p->blk, mbx.cmd.data.play.start_msf);
1745 hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf);
1746 sc->lastpb = mbx.cmd;
1747 mbx.res.length = 0;
1748 return mcd_send(sc, &mbx, 1);
1749 }
1750
1751 int
mcd_pause(sc)1752 mcd_pause(sc)
1753 struct mcd_softc *sc;
1754 {
1755 union mcd_qchninfo q;
1756 int error;
1757
1758 /* Verify current status. */
1759 if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS) {
1760 printf("%s: pause: attempted when not playing\n",
1761 sc->sc_dev.dv_xname);
1762 return EINVAL;
1763 }
1764
1765 /* Get the current position. */
1766 if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0)
1767 return error;
1768
1769 /* Copy it into lastpb. */
1770 sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0];
1771 sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1];
1772 sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2];
1773
1774 /* Stop playing. */
1775 if ((error = mcd_stop(sc)) != 0)
1776 return error;
1777
1778 /* Set the proper status and exit. */
1779 sc->audio_status = CD_AS_PLAY_PAUSED;
1780 return 0;
1781 }
1782
1783 int
mcd_resume(sc)1784 mcd_resume(sc)
1785 struct mcd_softc *sc;
1786 {
1787 struct mcd_mbox mbx;
1788 int error;
1789
1790 if (sc->audio_status != CD_AS_PLAY_PAUSED)
1791 return EINVAL;
1792
1793 if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1794 return error;
1795
1796 mbx.cmd = sc->lastpb;
1797 mbx.res.length = 0;
1798 return mcd_send(sc, &mbx, 1);
1799 }
1800
1801 int
mcd_eject(sc)1802 mcd_eject(sc)
1803 struct mcd_softc *sc;
1804 {
1805 struct mcd_mbox mbx;
1806
1807 mbx.cmd.opcode = MCD_CMDEJECTDISK;
1808 mbx.cmd.length = 0;
1809 mbx.res.length = 0;
1810 return mcd_send(sc, &mbx, 0);
1811 }
1812
1813 int
mcd_setlock(sc,mode)1814 mcd_setlock(sc, mode)
1815 struct mcd_softc *sc;
1816 int mode;
1817 {
1818 struct mcd_mbox mbx;
1819
1820 mbx.cmd.opcode = MCD_CMDSETLOCK;
1821 mbx.cmd.length = sizeof(mbx.cmd.data.lockmode);
1822 mbx.cmd.data.lockmode.mode = mode;
1823 mbx.res.length = 0;
1824 return mcd_send(sc, &mbx, 1);
1825 }
1826