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