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