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