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