xref: /netbsd-src/sys/dev/isa/mcd.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: mcd.c,v 1.102 2007/10/19 12:00:20 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.102 2007/10/19 12:00:20 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 <sys/cpu.h>
80 #include <sys/intr.h>
81 #include <sys/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 	disk_init(&sc->sc_dk, sc->sc_dev.dv_xname, &mcddkdriver);
272 	disk_attach(&sc->sc_dk);
273 
274 	printf(": model %s\n", sc->type != 0 ? sc->type : "unknown");
275 
276 	(void) mcd_setlock(sc, MCD_LK_UNLOCK);
277 
278 	mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
279 	mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
280 	mbx.cmd.data.config.subcommand = MCD_CF_IRQENABLE;
281 	mbx.cmd.data.config.data1 = 0x01;
282 	mbx.res.length = 0;
283 	(void) mcd_send(sc, &mbx, 0);
284 
285 	mcd_soft_reset(sc);
286 
287 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
288 	    IST_EDGE, IPL_BIO, mcdintr, sc);
289 }
290 
291 int
292 mcdopen(dev_t dev, int flag, int fmt, struct lwp *l)
293 {
294 	int error, part;
295 	struct mcd_softc *sc;
296 
297 	sc = device_lookup(&mcd_cd, MCDUNIT(dev));
298 	if (sc == NULL)
299 		return ENXIO;
300 
301 	if ((error = lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
302 		return error;
303 
304 	if (sc->sc_dk.dk_openmask != 0) {
305 		/*
306 		 * If any partition is open, but the disk has been invalidated,
307 		 * disallow further opens.
308 		 */
309 		if ((sc->flags & MCDF_LOADED) == 0) {
310 			error = EIO;
311 			goto bad3;
312 		}
313 	} else {
314 		/*
315 		 * Lock the drawer.  This will also notice any pending disk
316 		 * change or door open indicator and clear the MCDF_LOADED bit
317 		 * if necessary.
318 		 */
319 		(void) mcd_setlock(sc, MCD_LK_LOCK);
320 
321 		if ((sc->flags & MCDF_LOADED) == 0) {
322 			/* Partially reset the state. */
323 			sc->lastmode = MCD_MD_UNKNOWN;
324 			sc->lastupc = MCD_UPC_UNKNOWN;
325 
326 			sc->flags |= MCDF_LOADED;
327 
328 			/* Set the mode, causing the disk to spin up. */
329 			if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
330 				goto bad2;
331 
332 			/* Load the physical device parameters. */
333 			if (mcd_get_parms(sc) != 0) {
334 				error = ENXIO;
335 				goto bad2;
336 			}
337 
338 			/* Read the table of contents. */
339 			if ((error = mcd_read_toc(sc)) != 0)
340 				goto bad2;
341 
342 			/* Fabricate a disk label. */
343 			mcdgetdisklabel(sc);
344 		}
345 	}
346 
347 	part = MCDPART(dev);
348 
349 	MCD_TRACE("open: partition=%d disksize=%ld blksize=%d\n", part,
350 	    sc->disksize, sc->blksize);
351 
352 	/* Check that the partition exists. */
353 	if (part != RAW_PART &&
354 	    (part >= sc->sc_dk.dk_label->d_npartitions ||
355 	     sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
356 		error = ENXIO;
357 		goto bad;
358 	}
359 
360 	/* Insure only one open at a time. */
361 	switch (fmt) {
362 	case S_IFCHR:
363 		sc->sc_dk.dk_copenmask |= (1 << part);
364 		break;
365 	case S_IFBLK:
366 		sc->sc_dk.dk_bopenmask |= (1 << part);
367 		break;
368 	}
369 	sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
370 
371 	lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
372 	return 0;
373 
374 bad2:
375 	sc->flags &= ~MCDF_LOADED;
376 
377 bad:
378 	if (sc->sc_dk.dk_openmask == 0) {
379 #if 0
380 		(void) mcd_setmode(sc, MCD_MD_SLEEP);
381 #endif
382 		(void) mcd_setlock(sc, MCD_LK_UNLOCK);
383 	}
384 
385 bad3:
386 	lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
387 	return error;
388 }
389 
390 int
391 mcdclose(dev_t dev, int flag, int fmt, struct lwp *l)
392 {
393 	struct mcd_softc *sc = device_lookup(&mcd_cd, MCDUNIT(dev));
394 	int part = MCDPART(dev);
395 	int error;
396 
397 	MCD_TRACE("close: partition=%d\n", part);
398 
399 	if ((error = lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
400 		return error;
401 
402 	switch (fmt) {
403 	case S_IFCHR:
404 		sc->sc_dk.dk_copenmask &= ~(1 << part);
405 		break;
406 	case S_IFBLK:
407 		sc->sc_dk.dk_bopenmask &= ~(1 << part);
408 		break;
409 	}
410 	sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
411 
412 	if (sc->sc_dk.dk_openmask == 0) {
413 		/* XXXX Must wait for I/O to complete! */
414 
415 #if 0
416 		(void) mcd_setmode(sc, MCD_MD_SLEEP);
417 #endif
418 		(void) mcd_setlock(sc, MCD_LK_UNLOCK);
419 	}
420 
421 	lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
422 	return 0;
423 }
424 
425 void
426 mcdstrategy(bp)
427 	struct buf *bp;
428 {
429 	struct mcd_softc *sc = device_lookup(&mcd_cd, MCDUNIT(bp->b_dev));
430 	struct disklabel *lp = sc->sc_dk.dk_label;
431 	daddr_t blkno;
432 	int s;
433 
434 	/* Test validity. */
435 	MCD_TRACE("strategy: buf=0x%p blkno=%d bcount=%d\n", bp,
436 	    (int) bp->b_blkno, bp->b_bcount);
437 	if (bp->b_blkno < 0 ||
438 	    (bp->b_bcount % sc->blksize) != 0) {
439 		printf("%s: strategy: blkno = %" PRId64 " bcount = %d\n",
440 		    sc->sc_dev.dv_xname, bp->b_blkno, bp->b_bcount);
441 		bp->b_error = EINVAL;
442 		goto done;
443 	}
444 
445 	/* If device invalidated (e.g. media change, door open), error. */
446 	if ((sc->flags & MCDF_LOADED) == 0) {
447 		MCD_TRACE("strategy: drive not valid%s", "\n");
448 		bp->b_error = EIO;
449 		goto done;
450 	}
451 
452 	/* No data to read. */
453 	if (bp->b_bcount == 0)
454 		goto done;
455 
456 	/*
457 	 * Do bounds checking, adjust transfer. if error, process.
458 	 * If end of partition, just return.
459 	 */
460 	if (MCDPART(bp->b_dev) != RAW_PART &&
461 	    bounds_check_with_label(&sc->sc_dk, bp,
462 	    (sc->flags & (MCDF_WLABEL|MCDF_LABELLING)) != 0) <= 0)
463 		goto done;
464 
465 	/*
466 	 * Now convert the block number to absolute and put it in
467 	 * terms of the device's logical block size.
468 	 */
469 	blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
470 	if (MCDPART(bp->b_dev) != RAW_PART)
471 		blkno += lp->d_partitions[MCDPART(bp->b_dev)].p_offset;
472 
473 	bp->b_rawblkno = blkno;
474 
475 	/* Queue it. */
476 	s = splbio();
477 	BUFQ_PUT(sc->buf_queue, bp);
478 	splx(s);
479 	if (!sc->active)
480 		mcdstart(sc);
481 	return;
482 
483 done:
484 	bp->b_resid = bp->b_bcount;
485 	biodone(bp);
486 }
487 
488 void
489 mcdstart(sc)
490 	struct mcd_softc *sc;
491 {
492 	struct buf *bp;
493 	int s;
494 
495 loop:
496 	s = splbio();
497 
498 	if ((bp = BUFQ_GET(sc->buf_queue)) == NULL) {
499 		/* Nothing to do. */
500 		sc->active = 0;
501 		splx(s);
502 		return;
503 	}
504 
505 	/* Block found to process. */
506 	MCD_TRACE("start: found block bp=0x%p\n", bp);
507 	splx(s);
508 
509 	/* Changed media? */
510 	if ((sc->flags & MCDF_LOADED) == 0) {
511 		MCD_TRACE("start: drive not valid%s", "\n");
512 		bp->b_error = EIO;
513 		biodone(bp);
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 		bp->b_resid = 0;
1292 		disk_unbusy(&sc->sc_dk, bp->b_bcount, (bp->b_flags & B_READ));
1293 		biodone(bp);
1294 
1295 		mcdstart(sc);
1296 		return 1;
1297 
1298 	hold:
1299 		if (mbx->count-- < 0) {
1300 			printf("%s: timeout in state %d",
1301 			    sc->sc_dev.dv_xname, mbx->state);
1302 			goto readerr;
1303 		}
1304 
1305 #if 0
1306 		printf("%s: sleep in state %d\n", sc->sc_dev.dv_xname,
1307 		    mbx->state);
1308 #endif
1309 		callout_reset(&sc->sc_pintr_ch, hz / 100,
1310 		    mcd_pseudointr, sc);
1311 		return -1;
1312 	}
1313 
1314 readerr:
1315 	if (mbx->retry-- > 0) {
1316 		printf("; retrying\n");
1317 		goto tryagain;
1318 	} else
1319 		printf("; giving up\n");
1320 
1321 changed:
1322 	/* Invalidate the buffer. */
1323 	bp->b_error = EIO;
1324 	bp->b_resid = bp->b_bcount - mbx->skip;
1325 	disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid),
1326 	    (bp->b_flags & B_READ));
1327 	biodone(bp);
1328 
1329 	mcdstart(sc);
1330 	return -1;
1331 
1332 #ifdef notyet
1333 	printf("%s: unit timeout; resetting\n", sc->sc_dev.dv_xname);
1334 	bus_space_write_1(iot, ioh, MCD_RESET, MCD_CMDRESET);
1335 	delay(300000);
1336 	(void) mcd_getstat(sc, 1);
1337 	(void) mcd_getstat(sc, 1);
1338 	/*sc->status &= ~MCD_ST_DSKCHNG; */
1339 	sc->debug = 1; /* preventive set debug mode */
1340 #endif
1341 }
1342 
1343 void
1344 mcd_soft_reset(sc)
1345 	struct mcd_softc *sc;
1346 {
1347 
1348 	sc->debug = 0;
1349 	sc->flags = 0;
1350 	sc->lastmode = MCD_MD_UNKNOWN;
1351 	sc->lastupc = MCD_UPC_UNKNOWN;
1352 	sc->audio_status = CD_AS_AUDIO_INVALID;
1353 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MCD_CTL2, 0x0c); /* XXX */
1354 }
1355 
1356 int
1357 mcd_hard_reset(sc)
1358 	struct mcd_softc *sc;
1359 {
1360 	struct mcd_mbox mbx;
1361 
1362 	mcd_soft_reset(sc);
1363 
1364 	mbx.cmd.opcode = MCD_CMDRESET;
1365 	mbx.cmd.length = 0;
1366 	mbx.res.length = 0;
1367 	return mcd_send(sc, &mbx, 0);
1368 }
1369 
1370 int
1371 mcd_setmode(sc, mode)
1372 	struct mcd_softc *sc;
1373 	int mode;
1374 {
1375 	struct mcd_mbox mbx;
1376 	int error;
1377 
1378 	if (sc->lastmode == mode)
1379 		return 0;
1380 	if (sc->debug)
1381 		printf("%s: setting mode to %d\n", sc->sc_dev.dv_xname, mode);
1382 	sc->lastmode = MCD_MD_UNKNOWN;
1383 
1384 	mbx.cmd.opcode = MCD_CMDSETMODE;
1385 	mbx.cmd.length = sizeof(mbx.cmd.data.datamode);
1386 	mbx.cmd.data.datamode.mode = mode;
1387 	mbx.res.length = 0;
1388 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
1389 		return error;
1390 
1391 	sc->lastmode = mode;
1392 	return 0;
1393 }
1394 
1395 int
1396 mcd_setupc(sc, upc)
1397 	struct mcd_softc *sc;
1398 	int upc;
1399 {
1400 	struct mcd_mbox mbx;
1401 	int error;
1402 
1403 	if (sc->lastupc == upc)
1404 		return 0;
1405 	if (sc->debug)
1406 		printf("%s: setting upc to %d\n", sc->sc_dev.dv_xname, upc);
1407 	sc->lastupc = MCD_UPC_UNKNOWN;
1408 
1409 	mbx.cmd.opcode = MCD_CMDCONFIGDRIVE;
1410 	mbx.cmd.length = sizeof(mbx.cmd.data.config) - 1;
1411 	mbx.cmd.data.config.subcommand = MCD_CF_READUPC;
1412 	mbx.cmd.data.config.data1 = upc;
1413 	mbx.res.length = 0;
1414 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
1415 		return error;
1416 
1417 	sc->lastupc = upc;
1418 	return 0;
1419 }
1420 
1421 int
1422 mcd_toc_header(sc, th)
1423 	struct mcd_softc *sc;
1424 	struct ioc_toc_header *th;
1425 {
1426 
1427 	if (sc->debug)
1428 		printf("%s: mcd_toc_header: reading toc header\n",
1429 		    sc->sc_dev.dv_xname);
1430 
1431 	th->len = msf2hsg(sc->volinfo.vol_msf, 0);
1432 	th->starting_track = bcd2bin(sc->volinfo.trk_low);
1433 	th->ending_track = bcd2bin(sc->volinfo.trk_high);
1434 
1435 	return 0;
1436 }
1437 
1438 int
1439 mcd_read_toc(sc)
1440 	struct mcd_softc *sc;
1441 {
1442 	struct ioc_toc_header th;
1443 	union mcd_qchninfo q;
1444 	int error, trk, idx, retry;
1445 
1446 	if ((error = mcd_toc_header(sc, &th)) != 0)
1447 		return error;
1448 
1449 	if ((error = mcd_stop(sc)) != 0)
1450 		return error;
1451 
1452 	if (sc->debug)
1453 		printf("%s: read_toc: reading qchannel info\n",
1454 		    sc->sc_dev.dv_xname);
1455 
1456 	for (trk = th.starting_track; trk <= th.ending_track; trk++)
1457 		sc->toc[trk].toc.idx_no = 0x00;
1458 	trk = th.ending_track - th.starting_track + 1;
1459 	for (retry = 300; retry && trk > 0; retry--) {
1460 		if (mcd_getqchan(sc, &q, CD_TRACK_INFO) != 0)
1461 			break;
1462 		if (q.toc.trk_no != 0x00 || q.toc.idx_no == 0x00)
1463 			continue;
1464 		idx = bcd2bin(q.toc.idx_no);
1465 		if (idx < MCD_MAXTOCS &&
1466 		    sc->toc[idx].toc.idx_no == 0x00) {
1467 			sc->toc[idx] = q;
1468 			trk--;
1469 		}
1470 	}
1471 
1472 	/* Inform the drive that we're finished so it turns off the light. */
1473 	if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1474 		return error;
1475 
1476 	if (trk != 0)
1477 		return EINVAL;
1478 
1479 	/* Add a fake last+1 for mcd_playtracks(). */
1480 	idx = th.ending_track + 1;
1481 	sc->toc[idx].toc.control = sc->toc[idx-1].toc.control;
1482 	sc->toc[idx].toc.addr_type = sc->toc[idx-1].toc.addr_type;
1483 	sc->toc[idx].toc.trk_no = 0x00;
1484 	sc->toc[idx].toc.idx_no = 0xaa;
1485 	sc->toc[idx].toc.absolute_pos[0] = sc->volinfo.vol_msf[0];
1486 	sc->toc[idx].toc.absolute_pos[1] = sc->volinfo.vol_msf[1];
1487 	sc->toc[idx].toc.absolute_pos[2] = sc->volinfo.vol_msf[2];
1488 
1489 	return 0;
1490 }
1491 
1492 int
1493 mcd_toc_entries(sc, te, entries, count)
1494 	struct mcd_softc *sc;
1495 	struct ioc_read_toc_entry *te;
1496 	struct cd_toc_entry *entries;
1497 	int *count;
1498 {
1499 	int len = te->data_len;
1500 	struct ioc_toc_header header;
1501 	u_char trk;
1502 	daddr_t lba;
1503 	int error, n;
1504 
1505 	if (len < sizeof(struct cd_toc_entry))
1506 		return EINVAL;
1507 	if (te->address_format != CD_MSF_FORMAT &&
1508 	    te->address_format != CD_LBA_FORMAT)
1509 		return EINVAL;
1510 
1511 	/* Copy the TOC header. */
1512 	if ((error = mcd_toc_header(sc, &header)) != 0)
1513 		return error;
1514 
1515 	/* Verify starting track. */
1516 	trk = te->starting_track;
1517 	if (trk == 0x00)
1518 		trk = header.starting_track;
1519 	else if (trk == 0xaa)
1520 		trk = header.ending_track + 1;
1521 	else if (trk < header.starting_track ||
1522 		 trk > header.ending_track + 1)
1523 		return EINVAL;
1524 
1525 	/* Copy the TOC data. */
1526 	for (n = 0; trk <= header.ending_track + 1; n++, trk++) {
1527 		if (n * sizeof entries[0] > len)
1528 			break;
1529 		if (sc->toc[trk].toc.idx_no == 0x00)
1530 			continue;
1531 		entries[n].control = sc->toc[trk].toc.control;
1532 		entries[n].addr_type = sc->toc[trk].toc.addr_type;
1533 		entries[n].track = bcd2bin(sc->toc[trk].toc.idx_no);
1534 		switch (te->address_format) {
1535 		case CD_MSF_FORMAT:
1536 			entries[n].addr.addr[0] = 0;
1537 			entries[n].addr.addr[1] = bcd2bin(sc->toc[trk].toc.absolute_pos[0]);
1538 			entries[n].addr.addr[2] = bcd2bin(sc->toc[trk].toc.absolute_pos[1]);
1539 			entries[n].addr.addr[3] = bcd2bin(sc->toc[trk].toc.absolute_pos[2]);
1540 			break;
1541 		case CD_LBA_FORMAT:
1542 			lba = msf2hsg(sc->toc[trk].toc.absolute_pos, 0);
1543 			entries[n].addr.addr[0] = lba >> 24;
1544 			entries[n].addr.addr[1] = lba >> 16;
1545 			entries[n].addr.addr[2] = lba >> 8;
1546 			entries[n].addr.addr[3] = lba;
1547 			break;
1548 		}
1549 	}
1550 
1551 	*count = n;
1552 	return 0;
1553 }
1554 
1555 int
1556 mcd_stop(sc)
1557 	struct mcd_softc *sc;
1558 {
1559 	struct mcd_mbox mbx;
1560 	int error;
1561 
1562 	if (sc->debug)
1563 		printf("%s: mcd_stop: stopping play\n", sc->sc_dev.dv_xname);
1564 
1565 	mbx.cmd.opcode = MCD_CMDSTOPAUDIO;
1566 	mbx.cmd.length = 0;
1567 	mbx.res.length = 0;
1568 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
1569 		return error;
1570 
1571 	sc->audio_status = CD_AS_PLAY_COMPLETED;
1572 	return 0;
1573 }
1574 
1575 int
1576 mcd_getqchan(sc, q, qchn)
1577 	struct mcd_softc *sc;
1578 	union mcd_qchninfo *q;
1579 	int qchn;
1580 {
1581 	struct mcd_mbox mbx;
1582 	int error;
1583 
1584 	if (qchn == CD_TRACK_INFO) {
1585 		if ((error = mcd_setmode(sc, MCD_MD_TOC)) != 0)
1586 			return error;
1587 	} else {
1588 		if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1589 			return error;
1590 	}
1591 	if (qchn == CD_MEDIA_CATALOG) {
1592 		if ((error = mcd_setupc(sc, MCD_UPC_ENABLE)) != 0)
1593 			return error;
1594 	} else {
1595 		if ((error = mcd_setupc(sc, MCD_UPC_DISABLE)) != 0)
1596 			return error;
1597 	}
1598 
1599 	mbx.cmd.opcode = MCD_CMDGETQCHN;
1600 	mbx.cmd.length = 0;
1601 	mbx.res.length = sizeof(mbx.res.data.qchninfo);
1602 	if ((error = mcd_send(sc, &mbx, 1)) != 0)
1603 		return error;
1604 
1605 	*q = mbx.res.data.qchninfo;
1606 	return 0;
1607 }
1608 
1609 int
1610 mcd_read_subchannel(sc, ch, info)
1611 	struct mcd_softc *sc;
1612 	struct ioc_read_subchannel *ch;
1613 	struct cd_sub_channel_info *info;
1614 {
1615 	int len = ch->data_len;
1616 	union mcd_qchninfo q;
1617 	daddr_t lba;
1618 	int error;
1619 
1620 	if (sc->debug)
1621 		printf("%s: subchan: af=%d df=%d\n", sc->sc_dev.dv_xname,
1622 		    ch->address_format, ch->data_format);
1623 
1624 	if (len > sizeof(*info) || len < sizeof(info->header))
1625 		return EINVAL;
1626 	if (ch->address_format != CD_MSF_FORMAT &&
1627 	    ch->address_format != CD_LBA_FORMAT)
1628 		return EINVAL;
1629 	if (ch->data_format != CD_CURRENT_POSITION &&
1630 	    ch->data_format != CD_MEDIA_CATALOG)
1631 		return EINVAL;
1632 
1633 	if ((error = mcd_getqchan(sc, &q, ch->data_format)) != 0)
1634 		return error;
1635 
1636 	info->header.audio_status = sc->audio_status;
1637 	info->what.media_catalog.data_format = ch->data_format;
1638 
1639 	switch (ch->data_format) {
1640 	case CD_MEDIA_CATALOG:
1641 		info->what.media_catalog.mc_valid = 1;
1642 #if 0
1643 		info->what.media_catalog.mc_number =
1644 #endif
1645 		break;
1646 
1647 	case CD_CURRENT_POSITION:
1648 		info->what.position.track_number = bcd2bin(q.current.trk_no);
1649 		info->what.position.index_number = bcd2bin(q.current.idx_no);
1650 		switch (ch->address_format) {
1651 		case CD_MSF_FORMAT:
1652 			info->what.position.reladdr.addr[0] = 0;
1653 			info->what.position.reladdr.addr[1] = bcd2bin(q.current.relative_pos[0]);
1654 			info->what.position.reladdr.addr[2] = bcd2bin(q.current.relative_pos[1]);
1655 			info->what.position.reladdr.addr[3] = bcd2bin(q.current.relative_pos[2]);
1656 			info->what.position.absaddr.addr[0] = 0;
1657 			info->what.position.absaddr.addr[1] = bcd2bin(q.current.absolute_pos[0]);
1658 			info->what.position.absaddr.addr[2] = bcd2bin(q.current.absolute_pos[1]);
1659 			info->what.position.absaddr.addr[3] = bcd2bin(q.current.absolute_pos[2]);
1660 			break;
1661 		case CD_LBA_FORMAT:
1662 			lba = msf2hsg(q.current.relative_pos, 1);
1663 			/*
1664 			 * Pre-gap has index number of 0, and decreasing MSF
1665 			 * address.  Must be converted to negative LBA, per
1666 			 * SCSI spec.
1667 			 */
1668 			if (info->what.position.index_number == 0x00)
1669 				lba = -lba;
1670 			info->what.position.reladdr.addr[0] = lba >> 24;
1671 			info->what.position.reladdr.addr[1] = lba >> 16;
1672 			info->what.position.reladdr.addr[2] = lba >> 8;
1673 			info->what.position.reladdr.addr[3] = lba;
1674 			lba = msf2hsg(q.current.absolute_pos, 0);
1675 			info->what.position.absaddr.addr[0] = lba >> 24;
1676 			info->what.position.absaddr.addr[1] = lba >> 16;
1677 			info->what.position.absaddr.addr[2] = lba >> 8;
1678 			info->what.position.absaddr.addr[3] = lba;
1679 			break;
1680 		}
1681 		break;
1682 	}
1683 
1684 	return 0;
1685 }
1686 
1687 int
1688 mcd_playtracks(sc, p)
1689 	struct mcd_softc *sc;
1690 	struct ioc_play_track *p;
1691 {
1692 	struct mcd_mbox mbx;
1693 	int a = p->start_track;
1694 	int z = p->end_track;
1695 	int error;
1696 
1697 	if (sc->debug)
1698 		printf("%s: playtracks: from %d:%d to %d:%d\n",
1699 		    sc->sc_dev.dv_xname,
1700 		    a, p->start_index, z, p->end_index);
1701 
1702 	if (a < bcd2bin(sc->volinfo.trk_low) ||
1703 	    a > bcd2bin(sc->volinfo.trk_high) ||
1704 	    a > z ||
1705 	    z < bcd2bin(sc->volinfo.trk_low) ||
1706 	    z > bcd2bin(sc->volinfo.trk_high))
1707 		return EINVAL;
1708 
1709 	if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1710 		return error;
1711 
1712 	mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1713 	mbx.cmd.length = sizeof(mbx.cmd.data.play);
1714 	mbx.cmd.data.play.start_msf[0] = sc->toc[a].toc.absolute_pos[0];
1715 	mbx.cmd.data.play.start_msf[1] = sc->toc[a].toc.absolute_pos[1];
1716 	mbx.cmd.data.play.start_msf[2] = sc->toc[a].toc.absolute_pos[2];
1717 	mbx.cmd.data.play.end_msf[0] = sc->toc[z+1].toc.absolute_pos[0];
1718 	mbx.cmd.data.play.end_msf[1] = sc->toc[z+1].toc.absolute_pos[1];
1719 	mbx.cmd.data.play.end_msf[2] = sc->toc[z+1].toc.absolute_pos[2];
1720 	sc->lastpb = mbx.cmd;
1721 	mbx.res.length = 0;
1722 	return mcd_send(sc, &mbx, 1);
1723 }
1724 
1725 int
1726 mcd_playmsf(sc, p)
1727 	struct mcd_softc *sc;
1728 	struct ioc_play_msf *p;
1729 {
1730 	struct mcd_mbox mbx;
1731 	int error;
1732 
1733 	if (sc->debug)
1734 		printf("%s: playmsf: from %d:%d.%d to %d:%d.%d\n",
1735 		    sc->sc_dev.dv_xname,
1736 		    p->start_m, p->start_s, p->start_f,
1737 		    p->end_m, p->end_s, p->end_f);
1738 
1739 	if ((p->start_m * 60 * 75 + p->start_s * 75 + p->start_f) >=
1740 	    (p->end_m * 60 * 75 + p->end_s * 75 + p->end_f))
1741 		return EINVAL;
1742 
1743 	if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1744 		return error;
1745 
1746 	mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1747 	mbx.cmd.length = sizeof(mbx.cmd.data.play);
1748 	mbx.cmd.data.play.start_msf[0] = bin2bcd(p->start_m);
1749 	mbx.cmd.data.play.start_msf[1] = bin2bcd(p->start_s);
1750 	mbx.cmd.data.play.start_msf[2] = bin2bcd(p->start_f);
1751 	mbx.cmd.data.play.end_msf[0] = bin2bcd(p->end_m);
1752 	mbx.cmd.data.play.end_msf[1] = bin2bcd(p->end_s);
1753 	mbx.cmd.data.play.end_msf[2] = bin2bcd(p->end_f);
1754 	sc->lastpb = mbx.cmd;
1755 	mbx.res.length = 0;
1756 	return mcd_send(sc, &mbx, 1);
1757 }
1758 
1759 int
1760 mcd_playblocks(sc, p)
1761 	struct mcd_softc *sc;
1762 	struct ioc_play_blocks *p;
1763 {
1764 	struct mcd_mbox mbx;
1765 	int error;
1766 
1767 	if (sc->debug)
1768 		printf("%s: playblocks: blkno %d length %d\n",
1769 		    sc->sc_dev.dv_xname, p->blk, p->len);
1770 
1771 	if (p->blk > sc->disksize || p->len > sc->disksize ||
1772 	    (p->blk + p->len) > sc->disksize)
1773 		return 0;
1774 
1775 	if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1776 		return error;
1777 
1778 	mbx.cmd.opcode = MCD_CMDREADSINGLESPEED;
1779 	mbx.cmd.length = sizeof(mbx.cmd.data.play);
1780 	hsg2msf(p->blk, mbx.cmd.data.play.start_msf);
1781 	hsg2msf(p->blk + p->len, mbx.cmd.data.play.end_msf);
1782 	sc->lastpb = mbx.cmd;
1783 	mbx.res.length = 0;
1784 	return mcd_send(sc, &mbx, 1);
1785 }
1786 
1787 int
1788 mcd_pause(sc)
1789 	struct mcd_softc *sc;
1790 {
1791 	union mcd_qchninfo q;
1792 	int error;
1793 
1794 	/* Verify current status. */
1795 	if (sc->audio_status != CD_AS_PLAY_IN_PROGRESS)	{
1796 		printf("%s: pause: attempted when not playing\n",
1797 		    sc->sc_dev.dv_xname);
1798 		return EINVAL;
1799 	}
1800 
1801 	/* Get the current position. */
1802 	if ((error = mcd_getqchan(sc, &q, CD_CURRENT_POSITION)) != 0)
1803 		return error;
1804 
1805 	/* Copy it into lastpb. */
1806 	sc->lastpb.data.seek.start_msf[0] = q.current.absolute_pos[0];
1807 	sc->lastpb.data.seek.start_msf[1] = q.current.absolute_pos[1];
1808 	sc->lastpb.data.seek.start_msf[2] = q.current.absolute_pos[2];
1809 
1810 	/* Stop playing. */
1811 	if ((error = mcd_stop(sc)) != 0)
1812 		return error;
1813 
1814 	/* Set the proper status and exit. */
1815 	sc->audio_status = CD_AS_PLAY_PAUSED;
1816 	return 0;
1817 }
1818 
1819 int
1820 mcd_resume(sc)
1821 	struct mcd_softc *sc;
1822 {
1823 	struct mcd_mbox mbx;
1824 	int error;
1825 
1826 	if (sc->audio_status != CD_AS_PLAY_PAUSED)
1827 		return EINVAL;
1828 
1829 	if ((error = mcd_setmode(sc, MCD_MD_COOKED)) != 0)
1830 		return error;
1831 
1832 	mbx.cmd = sc->lastpb;
1833 	mbx.res.length = 0;
1834 	return mcd_send(sc, &mbx, 1);
1835 }
1836 
1837 int
1838 mcd_eject(sc)
1839 	struct mcd_softc *sc;
1840 {
1841 	struct mcd_mbox mbx;
1842 
1843 	mbx.cmd.opcode = MCD_CMDEJECTDISK;
1844 	mbx.cmd.length = 0;
1845 	mbx.res.length = 0;
1846 	return mcd_send(sc, &mbx, 0);
1847 }
1848 
1849 int
1850 mcd_setlock(sc, mode)
1851 	struct mcd_softc *sc;
1852 	int mode;
1853 {
1854 	struct mcd_mbox mbx;
1855 
1856 	mbx.cmd.opcode = MCD_CMDSETLOCK;
1857 	mbx.cmd.length = sizeof(mbx.cmd.data.lockmode);
1858 	mbx.cmd.data.lockmode.mode = mode;
1859 	mbx.res.length = 0;
1860 	return mcd_send(sc, &mbx, 1);
1861 }
1862