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