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