scd.c revision 126080
1/*-
2 * Copyright (c) 1995 Mikael Hybsch
3 * All rights reserved.
4 *
5 * Portions of this file are copied from mcd.c
6 * which has the following copyrights:
7 *
8 *	Copyright 1993 by Holger Veit (data part)
9 *	Copyright 1993 by Brian Moore (audio part)
10 *	Changes Copyright 1993 by Gary Clark II
11 *	Changes Copyright (C) 1994 by Andrew A. Chernov
12 *
13 *	Rewrote probe routine to work on newer Mitsumi drives.
14 *	Additional changes (C) 1994 by Jordan K. Hubbard
15 *
16 *	All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer
23 *    in this position and unchanged.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 *    notice, this list of conditions and the following disclaimer in the
26 *    documentation and/or other materials provided with the distribution.
27 * 3. The name of the author may not be used to endorse or promote products
28 *    derived from this software without specific prior written permission
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 */
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: head/sys/dev/scd/scd.c 126080 2004-02-21 21:10:55Z phk $");
45
46
47#undef	SCD_DEBUG
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/kernel.h>
52#include <sys/conf.h>
53#include <sys/fcntl.h>
54#include <sys/bio.h>
55#include <sys/cdio.h>
56#include <sys/disk.h>
57#include <sys/bus.h>
58
59#include <machine/stdarg.h>
60
61#include <machine/bus_pio.h>
62#include <machine/bus.h>
63#include <machine/resource.h>
64#include <sys/rman.h>
65
66#include <isa/isavar.h>
67
68#include <dev/scd/scdreg.h>
69#include <dev/scd/scdvar.h>
70
71/* flags */
72#define SCDOPEN		0x0001	/* device opened */
73#define SCDVALID	0x0002	/* parameters loaded */
74#define SCDINIT		0x0004	/* device is init'd */
75#define	SCDPROBING	0x0020	/* probing */
76#define	SCDTOC		0x0100	/* already read toc */
77#define	SCDMBXBSY	0x0200	/* local mbx is busy */
78#define	SCDSPINNING	0x0400  /* drive is spun up */
79
80#define SCD_S_BEGIN	0
81#define SCD_S_BEGIN1	1
82#define SCD_S_WAITSTAT	2
83#define	SCD_S_WAITFIFO	3
84#define SCD_S_WAITSPIN	4
85#define SCD_S_WAITREAD	5
86#define	SCD_S_WAITPARAM 6
87
88#define RDELAY_WAIT	300
89#define RDELAY_WAITREAD	300
90
91#define	SCDBLKSIZE	2048
92
93#ifdef SCD_DEBUG
94   static int scd_debuglevel = SCD_DEBUG;
95#  define XDEBUG(sc, level, fmt, args...) \
96	do { \
97		if (scd_debuglevel >= level) \
98			device_printf(sc->dev, fmt, ## args); \
99	} while (0)
100#else
101#  define XDEBUG(sc, level, fmt, args...)
102#endif
103
104#define	IS_ATTENTION(sc)	((SCD_READ(sc, IREG_STATUS) & SBIT_ATTENTION) != 0)
105#define	IS_BUSY(sc)		((SCD_READ(sc, IREG_STATUS) & SBIT_BUSY) != 0)
106#define	IS_DATA_RDY(sc)		((SCD_READ(sc, IREG_STATUS) & SBIT_DATA_READY) != 0)
107#define	STATUS_BIT(sc, bit)	((SCD_READ(sc, IREG_STATUS) & (bit)) != 0)
108#define	FSTATUS_BIT(sc, bit)	((SCD_READ(sc, IREG_FSTATUS) & (bit)) != 0)
109
110/* prototypes */
111static	void	hsg2msf(int hsg, bcd_t *msf);
112static	int	msf2hsg(bcd_t *msf);
113
114static void process_attention(struct scd_softc *);
115static int waitfor_status_bits(struct scd_softc *, int bits_set, int bits_clear);
116static int send_cmd(struct scd_softc *, u_char cmd, u_int nargs, ...);
117static void init_drive(struct scd_softc *);
118static int spin_up(struct scd_softc *);
119static int read_toc(struct scd_softc *);
120static int get_result(struct scd_softc *, int result_len, u_char *result);
121static void print_error(struct scd_softc *, int errcode);
122
123static void scd_start(struct scd_softc *);
124static timeout_t scd_timeout;
125static void scd_doread(struct scd_softc *, int state, struct scd_mbx *mbxin);
126
127static int scd_eject(struct scd_softc *);
128static int scd_stop(struct scd_softc *);
129static int scd_pause(struct scd_softc *);
130static int scd_resume(struct scd_softc *);
131static int scd_playtracks(struct scd_softc *, struct ioc_play_track *pt);
132static int scd_playmsf(struct scd_softc *, struct ioc_play_msf *msf);
133static int scd_play(struct scd_softc *, struct ioc_play_msf *msf);
134static int scd_subchan(struct scd_softc *, struct ioc_read_subchannel *sch);
135static int read_subcode(struct scd_softc *, struct sony_subchannel_position_data *sch);
136
137/* for xcdplayer */
138static int scd_toc_header(struct scd_softc *, struct ioc_toc_header *th);
139static int scd_toc_entrys(struct scd_softc *, struct ioc_read_toc_entry *te);
140static int scd_toc_entry(struct scd_softc *, struct ioc_read_toc_single_entry *te);
141#define SCD_LASTPLUS1 170 /* don't ask, xcdplayer passes this in */
142
143static	d_open_t	scdopen;
144static	d_close_t	scdclose;
145static	d_ioctl_t	scdioctl;
146static	d_strategy_t	scdstrategy;
147
148
149static struct cdevsw scd_cdevsw = {
150	.d_version =	D_VERSION,
151	.d_open =	scdopen,
152	.d_close =	scdclose,
153	.d_read =	physread,
154	.d_ioctl =	scdioctl,
155	.d_strategy =	scdstrategy,
156	.d_name =	"scd",
157	.d_flags =	D_DISK | D_NEEDGIANT,
158};
159
160int
161scd_attach(struct scd_softc *sc)
162{
163	int unit;
164
165	unit = device_get_unit(sc->dev);
166
167	init_drive(sc);
168
169	sc->data.flags = SCDINIT;
170	sc->data.audio_status = CD_AS_AUDIO_INVALID;
171	bioq_init(&sc->data.head);
172
173	sc->scd_dev_t = make_dev(&scd_cdevsw, 8 * unit,
174		UID_ROOT, GID_OPERATOR, 0640, "scd%d", unit);
175	sc->scd_dev_t->si_drv1 = (void *)sc;
176
177	return (0);
178}
179
180static	int
181scdopen(dev_t dev, int flags, int fmt, struct thread *td)
182{
183	struct scd_softc *sc;
184	int rc;
185
186	sc = (struct scd_softc *)dev->si_drv1;
187
188	/* not initialized*/
189	if (!(sc->data.flags & SCDINIT))
190		return (ENXIO);
191
192	/* invalidated in the meantime? mark all open part's invalid */
193	if (sc->data.openflag)
194		return (ENXIO);
195
196	XDEBUG(sc, 1, "DEBUG: status = 0x%x\n", SCD_READ(sc, IREG_STATUS));
197
198	if ((rc = spin_up(sc)) != 0) {
199		print_error(sc, rc);
200		return (EIO);
201	}
202	if (!(sc->data.flags & SCDTOC)) {
203		int loop_count = 3;
204
205		while (loop_count-- > 0 && (rc = read_toc(sc)) != 0) {
206			if (rc == ERR_NOT_SPINNING) {
207				rc = spin_up(sc);
208				if (rc) {
209					print_error(sc, rc);\
210					return (EIO);
211				}
212				continue;
213			}
214			device_printf(sc->dev, "TOC read error 0x%x\n", rc);
215			return (EIO);
216		}
217	}
218
219	dev->si_bsize_phys = sc->data.blksize;
220
221	sc->data.openflag = 1;
222	sc->data.flags |= SCDVALID;
223
224	return (0);
225}
226
227static	int
228scdclose(dev_t dev, int flags, int fmt, struct thread *td)
229{
230	struct scd_softc *sc;
231
232	sc = (struct scd_softc *)dev->si_drv1;
233
234	if (!(sc->data.flags & SCDINIT) || !sc->data.openflag)
235		return (ENXIO);
236
237	if (sc->data.audio_status != CD_AS_PLAY_IN_PROGRESS) {
238		(void)send_cmd(sc, CMD_SPIN_DOWN, 0);
239		sc->data.flags &= ~SCDSPINNING;
240	}
241
242
243	/* close channel */
244	sc->data.openflag = 0;
245
246	return (0);
247}
248
249static	void
250scdstrategy(struct bio *bp)
251{
252	int s;
253	struct scd_softc *sc;
254
255	sc = (struct scd_softc *)bp->bio_dev->si_drv1;
256
257	/* if device invalidated (e.g. media change, door open), error */
258	if (!(sc->data.flags & SCDVALID)) {
259		device_printf(sc->dev, "media changed\n");
260		bp->bio_error = EIO;
261		goto bad;
262	}
263
264	/* read only */
265	if (!(bp->bio_cmd == BIO_READ)) {
266		bp->bio_error = EROFS;
267		goto bad;
268	}
269
270	/* no data to read */
271	if (bp->bio_bcount == 0)
272		goto done;
273
274	if (!(sc->data.flags & SCDTOC)) {
275		bp->bio_error = EIO;
276		goto bad;
277	}
278
279	bp->bio_resid = 0;
280
281	/* queue it */
282	s = splbio();
283	bioq_disksort(&sc->data.head, bp);
284	splx(s);
285
286	/* now check whether we can perform processing */
287	scd_start(sc);
288	return;
289
290bad:
291	bp->bio_flags |= BIO_ERROR;
292done:
293	bp->bio_resid = bp->bio_bcount;
294	biodone(bp);
295	return;
296}
297
298static void
299scd_start(struct scd_softc *sc)
300{
301	struct bio *bp;
302	int s = splbio();
303
304	if (sc->data.flags & SCDMBXBSY) {
305		splx(s);
306		return;
307	}
308
309	bp = bioq_first(&sc->data.head);
310	if (bp != 0) {
311		/* block found to process, dequeue */
312		bioq_remove(&sc->data.head, bp);
313		sc->data.flags |= SCDMBXBSY;
314		splx(s);
315	} else {
316		/* nothing to do */
317		splx(s);
318		return;
319	}
320
321	sc->data.mbx.retry = 3;
322	sc->data.mbx.bp = bp;
323	splx(s);
324
325	scd_doread(sc, SCD_S_BEGIN, &(sc->data.mbx));
326	return;
327}
328
329static	int
330scdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
331{
332	struct scd_softc *sc;
333
334	sc = (struct scd_softc *)dev->si_drv1;
335
336	XDEBUG(sc, 1, "ioctl: cmd=0x%lx\n", cmd);
337
338	if (!(sc->data.flags & SCDVALID))
339		return (EIO);
340
341	switch (cmd) {
342	case DIOCGMEDIASIZE:
343		*(off_t *)addr = (off_t)sc->data.disksize * sc->data.blksize;
344		return (0);
345		break;
346	case DIOCGSECTORSIZE:
347		*(u_int *)addr = sc->data.blksize;
348		return (0);
349		break;
350	case CDIOCPLAYTRACKS:
351		return scd_playtracks(sc, (struct ioc_play_track *) addr);
352	case CDIOCPLAYBLOCKS:
353		return (EINVAL);
354	case CDIOCPLAYMSF:
355		return scd_playmsf(sc, (struct ioc_play_msf *) addr);
356	case CDIOCREADSUBCHANNEL:
357		return scd_subchan(sc, (struct ioc_read_subchannel *) addr);
358	case CDIOREADTOCHEADER:
359		return scd_toc_header (sc, (struct ioc_toc_header *) addr);
360	case CDIOREADTOCENTRYS:
361		return scd_toc_entrys (sc, (struct ioc_read_toc_entry*) addr);
362	case CDIOREADTOCENTRY:
363		return scd_toc_entry (sc, (struct ioc_read_toc_single_entry*) addr);
364	case CDIOCSETPATCH:
365	case CDIOCGETVOL:
366	case CDIOCSETVOL:
367	case CDIOCSETMONO:
368	case CDIOCSETSTERIO:
369	case CDIOCSETMUTE:
370	case CDIOCSETLEFT:
371	case CDIOCSETRIGHT:
372		return (EINVAL);
373	case CDIOCRESUME:
374		return scd_resume(sc);
375	case CDIOCPAUSE:
376		return scd_pause(sc);
377	case CDIOCSTART:
378		return (EINVAL);
379	case CDIOCSTOP:
380		return scd_stop(sc);
381	case CDIOCEJECT:
382		return scd_eject(sc);
383	case CDIOCALLOW:
384		return (0);
385	case CDIOCSETDEBUG:
386#ifdef SCD_DEBUG
387		scd_debuglevel++;
388#endif
389		return (0);
390	case CDIOCCLRDEBUG:
391#ifdef SCD_DEBUG
392		scd_debuglevel = 0;
393
394#endif
395		return (0);
396	default:
397		device_printf(sc->dev, "unsupported ioctl (cmd=0x%lx)\n", cmd);
398		return (ENOTTY);
399	}
400}
401
402/***************************************************************
403 * lower level of driver starts here
404 **************************************************************/
405
406static int
407scd_playtracks(struct scd_softc *sc, struct ioc_play_track *pt)
408{
409	struct ioc_play_msf msf;
410	int a = pt->start_track;
411	int z = pt->end_track;
412	int rc;
413
414	if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) {
415		if (rc == -ERR_NOT_SPINNING) {
416			if (spin_up(sc) != 0)
417				return (EIO);
418			rc = read_toc(sc);
419		}
420		if (rc != 0) {
421			print_error(sc, rc);
422			return (EIO);
423		}
424	}
425
426	XDEBUG(sc, 1, "playtracks from %d:%d to %d:%d\n",
427		a, pt->start_index, z, pt->end_index);
428
429	if (   a < sc->data.first_track
430	    || a > sc->data.last_track
431	    || a > z
432	    || z > sc->data.last_track)
433		return (EINVAL);
434
435	bcopy(sc->data.toc[a].start_msf, &msf.start_m, 3);
436	hsg2msf(msf2hsg(sc->data.toc[z+1].start_msf)-1, &msf.end_m);
437
438	return scd_play(sc, &msf);
439}
440
441/* The start/end msf is expected to be in bin format */
442static int
443scd_playmsf(struct scd_softc *sc, struct ioc_play_msf *msfin)
444{
445	struct ioc_play_msf msf;
446
447	msf.start_m = bin2bcd(msfin->start_m);
448	msf.start_s = bin2bcd(msfin->start_s);
449	msf.start_f = bin2bcd(msfin->start_f);
450	msf.end_m = bin2bcd(msfin->end_m);
451	msf.end_s = bin2bcd(msfin->end_s);
452	msf.end_f = bin2bcd(msfin->end_f);
453
454	return scd_play(sc, &msf);
455}
456
457/* The start/end msf is expected to be in bcd format */
458static int
459scd_play(struct scd_softc *sc, struct ioc_play_msf *msf)
460{
461	int i, rc;
462
463	XDEBUG(sc, 1, "playing: %02x:%02x:%02x -> %02x:%02x:%02x\n",
464		msf->start_m, msf->start_s, msf->start_f,
465		msf->end_m, msf->end_s, msf->end_f);
466
467	for (i = 0; i < 2; i++) {
468		rc = send_cmd(sc, CMD_PLAY_AUDIO, 7,
469			0x03,
470			msf->start_m, msf->start_s, msf->start_f,
471			msf->end_m, msf->end_s, msf->end_f);
472		if (rc == -ERR_NOT_SPINNING) {
473			sc->data.flags &= ~SCDSPINNING;
474			if (spin_up(sc) != 0)
475				return (EIO);
476		} else if (rc < 0) {
477			print_error(sc, rc);
478			return (EIO);
479		} else {
480			break;
481		}
482	}
483	sc->data.audio_status = CD_AS_PLAY_IN_PROGRESS;
484	bcopy((char *)msf, (char *)&sc->data.last_play, sizeof(struct ioc_play_msf));
485	return (0);
486}
487
488static int
489scd_stop(struct scd_softc *sc)
490{
491
492	(void)send_cmd(sc, CMD_STOP_AUDIO, 0);
493	sc->data.audio_status = CD_AS_PLAY_COMPLETED;
494	return (0);
495}
496
497static int
498scd_pause(struct scd_softc *sc)
499{
500	struct sony_subchannel_position_data subpos;
501
502	if (sc->data.audio_status != CD_AS_PLAY_IN_PROGRESS)
503		return (EINVAL);
504
505	if (read_subcode(sc, &subpos) != 0)
506		return (EIO);
507
508	if (send_cmd(sc, CMD_STOP_AUDIO, 0) != 0)
509		return (EIO);
510
511	sc->data.last_play.start_m = subpos.abs_msf[0];
512	sc->data.last_play.start_s = subpos.abs_msf[1];
513	sc->data.last_play.start_f = subpos.abs_msf[2];
514	sc->data.audio_status = CD_AS_PLAY_PAUSED;
515
516	XDEBUG(sc, 1, "pause @ %02x:%02x:%02x\n",
517		sc->data.last_play.start_m,
518		sc->data.last_play.start_s,
519		sc->data.last_play.start_f);
520
521	return (0);
522}
523
524static int
525scd_resume(struct scd_softc *sc)
526{
527
528	if (sc->data.audio_status != CD_AS_PLAY_PAUSED)
529		return (EINVAL);
530	return scd_play(sc, &sc->data.last_play);
531}
532
533static int
534scd_eject(struct scd_softc *sc)
535{
536
537	sc->data.audio_status = CD_AS_AUDIO_INVALID;
538	sc->data.flags &= ~(SCDSPINNING|SCDTOC);
539
540	if (send_cmd(sc, CMD_STOP_AUDIO, 0) != 0 ||
541	    send_cmd(sc, CMD_SPIN_DOWN, 0) != 0 ||
542	    send_cmd(sc, CMD_EJECT, 0) != 0)
543	{
544		return (EIO);
545	}
546	return (0);
547}
548
549static int
550scd_subchan(struct scd_softc *sc, struct ioc_read_subchannel *sch)
551{
552	struct sony_subchannel_position_data q;
553	struct cd_sub_channel_info data;
554
555	XDEBUG(sc, 1, "subchan af=%d, df=%d\n",
556		sch->address_format, sch->data_format);
557
558	if (sch->address_format != CD_MSF_FORMAT)
559		return (EINVAL);
560
561	if (sch->data_format != CD_CURRENT_POSITION)
562		return (EINVAL);
563
564	if (read_subcode(sc, &q) != 0)
565		return (EIO);
566
567	data.header.audio_status = sc->data.audio_status;
568	data.what.position.data_format = CD_MSF_FORMAT;
569	data.what.position.track_number = bcd2bin(q.track_number);
570	data.what.position.reladdr.msf.unused = 0;
571	data.what.position.reladdr.msf.minute = bcd2bin(q.rel_msf[0]);
572	data.what.position.reladdr.msf.second = bcd2bin(q.rel_msf[1]);
573	data.what.position.reladdr.msf.frame = bcd2bin(q.rel_msf[2]);
574	data.what.position.absaddr.msf.unused = 0;
575	data.what.position.absaddr.msf.minute = bcd2bin(q.abs_msf[0]);
576	data.what.position.absaddr.msf.second = bcd2bin(q.abs_msf[1]);
577	data.what.position.absaddr.msf.frame = bcd2bin(q.abs_msf[2]);
578
579	if (copyout(&data, sch->data, min(sizeof(struct cd_sub_channel_info), sch->data_len))!=0)
580		return (EFAULT);
581	return (0);
582}
583
584int
585scd_probe(struct scd_softc *sc)
586{
587	struct sony_drive_configuration drive_config;
588	int rc;
589	static char namebuf[8+16+8+3];
590	char *s = namebuf;
591	int loop_count = 0;
592
593	sc->data.flags = SCDPROBING;
594
595	bzero(&drive_config, sizeof(drive_config));
596
597again:
598	/* Reset drive */
599	SCD_WRITE(sc, OREG_CONTROL, CBIT_RESET_DRIVE);
600
601	/* Calm down */
602	DELAY(300000);
603
604	/* Only the ATTENTION bit may be set */
605	if ((SCD_READ(sc, IREG_STATUS) & ~1) != 0) {
606		XDEBUG(sc, 1, "too many bits set. probe failed.\n");
607		return (ENXIO);
608	}
609	rc = send_cmd(sc, CMD_GET_DRIVE_CONFIG, 0);
610	if (rc != sizeof(drive_config)) {
611		/* Sometimes if the drive is playing audio I get */
612		/* the bad result 82. Fix by repeating the reset */
613		if (rc > 0 && loop_count++ == 0)
614			goto again;
615		return (ENXIO);
616	}
617	if (get_result(sc, rc, (u_char *)&drive_config) != 0)
618		return (ENXIO);
619
620	bcopy(drive_config.vendor, namebuf, 8);
621	s = namebuf+8;
622	while (*(s-1) == ' ')	/* Strip trailing spaces */
623		s--;
624	*s++ = ' ';
625	bcopy(drive_config.product, s, 16);
626	s += 16;
627	while (*(s-1) == ' ')
628		s--;
629	*s++ = ' ';
630	bcopy(drive_config.revision, s, 8);
631	s += 8;
632	while (*(s-1) == ' ')
633		s--;
634	*s = 0;
635
636	sc->data.name = namebuf;
637
638	if (drive_config.config & 0x10)
639		sc->data.double_speed = 1;
640	else
641		sc->data.double_speed = 0;
642
643	return (0);
644}
645
646static int
647read_subcode(struct scd_softc *sc, struct sony_subchannel_position_data *scp)
648{
649	int rc;
650
651	rc = send_cmd(sc, CMD_GET_SUBCHANNEL_DATA, 0);
652	if (rc < 0 || rc < sizeof(*scp))
653		return (EIO);
654	if (get_result(sc, rc, (u_char *)scp) != 0)
655		return (EIO);
656	return (0);
657}
658
659/* State machine copied from mcd.c */
660
661/* This (and the code in mcd.c) will not work with more than one drive */
662/* because there is only one sc->ch_mbxsave below. Should fix that some day. */
663/* (sc->ch_mbxsave & state should probably be included in the scd_data struct and */
664/*  the unit number used as first argument to scd_doread().) /Micke */
665
666/* state machine to process read requests
667 * initialize with SCD_S_BEGIN: reset state machine
668 * SCD_S_WAITSTAT:  wait for ready (!busy)
669 * SCD_S_WAITSPIN:  wait for drive to spin up (if not spinning)
670 * SCD_S_WAITFIFO:  wait for param fifo to get ready, them exec. command.
671 * SCD_S_WAITREAD:  wait for data ready, read data
672 * SCD_S_WAITPARAM: wait for command result params, read them, error if bad data read.
673 */
674
675static void
676scd_timeout(void *arg)
677{
678	struct scd_softc *sc;
679	sc = (struct scd_softc *)arg;
680
681	scd_doread(sc, sc->ch_state, sc->ch_mbxsave);
682}
683
684static void
685scd_doread(struct scd_softc *sc, int state, struct scd_mbx *mbxin)
686{
687	struct scd_mbx *mbx = (state!=SCD_S_BEGIN) ? sc->ch_mbxsave : mbxin;
688	struct	bio *bp = mbx->bp;
689	int	i;
690	int	blknum;
691	caddr_t	addr;
692	static char sdata[3];	/* Must be preserved between calls to this function */
693
694loop:
695	switch (state) {
696	case SCD_S_BEGIN:
697		mbx = sc->ch_mbxsave = mbxin;
698
699	case SCD_S_BEGIN1:
700		/* get status */
701		mbx->count = RDELAY_WAIT;
702
703		process_attention(sc);
704		goto trystat;
705
706	case SCD_S_WAITSTAT:
707		sc->ch_state = SCD_S_WAITSTAT;
708		untimeout(scd_timeout, (caddr_t)sc, sc->ch);
709		if (mbx->count-- <= 0) {
710			device_printf(sc->dev, "timeout. drive busy.\n");
711			goto harderr;
712		}
713
714trystat:
715		if (IS_BUSY(sc)) {
716			sc->ch_state = SCD_S_WAITSTAT;
717			sc->ch = timeout(scd_timeout, (caddr_t)sc, hz/100); /* XXX */
718			return;
719		}
720
721		process_attention(sc);
722
723		/* reject, if audio active */
724		if (sc->data.audio_status & CD_AS_PLAY_IN_PROGRESS) {
725			device_printf(sc->dev, "audio is active\n");
726			goto harderr;
727		}
728
729		mbx->sz = sc->data.blksize;
730
731		/* for first block */
732		mbx->nblk = (bp->bio_bcount + (mbx->sz-1)) / mbx->sz;
733		mbx->skip = 0;
734
735nextblock:
736		if (!(sc->data.flags & SCDVALID))
737			goto changed;
738
739		blknum 	= bp->bio_offset / mbx->sz + mbx->skip/mbx->sz;
740
741		XDEBUG(sc, 2, "scd_doread: read blknum=%d\n", blknum);
742
743		/* build parameter block */
744		hsg2msf(blknum, sdata);
745
746		SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
747		SCD_WRITE(sc, OREG_CONTROL, CBIT_RPARAM_CLEAR);
748		SCD_WRITE(sc, OREG_CONTROL, CBIT_DATA_READY_CLEAR);
749
750		if (FSTATUS_BIT(sc, FBIT_WPARAM_READY))
751			goto writeparam;
752
753		mbx->count = 100;
754		sc->ch_state = SCD_S_WAITFIFO;
755		sc->ch = timeout(scd_timeout, (caddr_t)sc, hz/100); /* XXX */
756		return;
757
758	case SCD_S_WAITSPIN:
759		sc->ch_state = SCD_S_WAITSPIN;
760		untimeout(scd_timeout,(caddr_t)sc, sc->ch);
761		if (mbx->count-- <= 0) {
762			device_printf(sc->dev, "timeout waiting for drive to spin up.\n");
763			goto harderr;
764		}
765		if (!STATUS_BIT(sc, SBIT_RESULT_READY)) {
766			sc->ch_state = SCD_S_WAITSPIN;
767			sc->ch = timeout(scd_timeout, (caddr_t)sc, hz/100); /* XXX */
768			return;
769		}
770		SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
771		switch ((i = SCD_READ(sc, IREG_RESULT)) & 0xf0) {
772		case 0x20:
773			i = SCD_READ(sc, IREG_RESULT);
774			print_error(sc, i);
775			goto harderr;
776		case 0x00:
777			(void)SCD_READ(sc, IREG_RESULT);
778			sc->data.flags |= SCDSPINNING;
779			break;
780		}
781		XDEBUG(sc, 1, "DEBUG: spin up complete\n");
782
783		state = SCD_S_BEGIN1;
784		goto loop;
785
786	case SCD_S_WAITFIFO:
787		sc->ch_state = SCD_S_WAITFIFO;
788		untimeout(scd_timeout,(caddr_t)sc, sc->ch);
789		if (mbx->count-- <= 0) {
790			device_printf(sc->dev, "timeout. write param not ready.\n");
791			goto harderr;
792		}
793		if (!FSTATUS_BIT(sc, FBIT_WPARAM_READY)) {
794			sc->ch_state = SCD_S_WAITFIFO;
795			sc->ch = timeout(scd_timeout, (caddr_t)sc,hz/100); /* XXX */
796			return;
797		}
798		XDEBUG(sc, 1, "mbx->count (writeparamwait) = %d(%d)\n", mbx->count, 100);
799
800writeparam:
801		/* The reason this test isn't done 'till now is to make sure */
802		/* that it is ok to send the SPIN_UP cmd below. */
803		if (!(sc->data.flags & SCDSPINNING)) {
804			XDEBUG(sc, 1, "spinning up drive ...\n");
805			SCD_WRITE(sc, OREG_COMMAND, CMD_SPIN_UP);
806			mbx->count = 300;
807			sc->ch_state = SCD_S_WAITSPIN;
808			sc->ch = timeout(scd_timeout, (caddr_t)sc, hz/100); /* XXX */
809			return;
810		}
811
812		/* send the read command */
813		critical_enter();
814		SCD_WRITE(sc, OREG_WPARAMS, sdata[0]);
815		SCD_WRITE(sc, OREG_WPARAMS, sdata[1]);
816		SCD_WRITE(sc, OREG_WPARAMS, sdata[2]);
817		SCD_WRITE(sc, OREG_WPARAMS, 0);
818		SCD_WRITE(sc, OREG_WPARAMS, 0);
819		SCD_WRITE(sc, OREG_WPARAMS, 1);
820		SCD_WRITE(sc, OREG_COMMAND, CMD_READ);
821		critical_exit();
822
823		mbx->count = RDELAY_WAITREAD;
824		for (i = 0; i < 50; i++) {
825			if (STATUS_BIT(sc, SBIT_DATA_READY))
826				goto got_data;
827			DELAY(100);
828		}
829
830		sc->ch_state = SCD_S_WAITREAD;
831		sc->ch = timeout(scd_timeout, (caddr_t)sc, hz/100); /* XXX */
832		return;
833
834	case SCD_S_WAITREAD:
835		sc->ch_state = SCD_S_WAITREAD;
836		untimeout(scd_timeout,(caddr_t)sc, sc->ch);
837		if (mbx->count-- <= 0) {
838			if (STATUS_BIT(sc, SBIT_RESULT_READY))
839				goto got_param;
840			device_printf(sc->dev, "timeout while reading data\n");
841			goto readerr;
842		}
843		if (!STATUS_BIT(sc, SBIT_DATA_READY)) {
844			process_attention(sc);
845			if (!(sc->data.flags & SCDVALID))
846				goto changed;
847			sc->ch_state = SCD_S_WAITREAD;
848			sc->ch = timeout(scd_timeout, (caddr_t)sc, hz/100); /* XXX */
849			return;
850		}
851		XDEBUG(sc, 2, "mbx->count (after RDY_BIT) = %d(%d)\n", mbx->count, RDELAY_WAITREAD);
852
853got_data:
854		/* data is ready */
855		addr = bp->bio_data + mbx->skip;
856		SCD_WRITE(sc, OREG_CONTROL, CBIT_DATA_READY_CLEAR);
857		SCD_READ_MULTI(sc, IREG_DATA, addr, mbx->sz);
858
859		mbx->count = 100;
860		for (i = 0; i < 20; i++) {
861			if (STATUS_BIT(sc, SBIT_RESULT_READY))
862				goto waitfor_param;
863			DELAY(100);
864		}
865		goto waitfor_param;
866
867	case SCD_S_WAITPARAM:
868		sc->ch_state = SCD_S_WAITPARAM;
869		untimeout(scd_timeout,(caddr_t)sc, sc->ch);
870		if (mbx->count-- <= 0) {
871			device_printf(sc->dev, "timeout waiting for params\n");
872			goto readerr;
873		}
874
875waitfor_param:
876		if (!STATUS_BIT(sc, SBIT_RESULT_READY)) {
877			sc->ch_state = SCD_S_WAITPARAM;
878			sc->ch = timeout(scd_timeout, (caddr_t)sc, hz/100); /* XXX */
879			return;
880		}
881#ifdef SCD_DEBUG
882		if (mbx->count < 100 && scd_debuglevel > 0)
883			device_printf(sc->dev, "mbx->count (paramwait) = %d(%d)\n", mbx->count, 100);
884#endif
885
886got_param:
887		SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
888		switch ((i = SCD_READ(sc, IREG_RESULT)) & 0xf0) {
889		case 0x50:
890			switch (i) {
891			case ERR_FATAL_READ_ERROR1:
892			case ERR_FATAL_READ_ERROR2:
893				device_printf(sc->dev, "unrecoverable read error 0x%x\n", i);
894				goto harderr;
895			}
896			break;
897		case 0x20:
898			i = SCD_READ(sc, IREG_RESULT);
899			switch (i) {
900			case ERR_NOT_SPINNING:
901				XDEBUG(sc, 1, "read error: drive not spinning\n");
902				if (mbx->retry-- > 0) {
903					state = SCD_S_BEGIN1;
904					sc->data.flags &= ~SCDSPINNING;
905					goto loop;
906				}
907				goto harderr;
908			default:
909				print_error(sc, i);
910				goto readerr;
911			}
912		case 0x00:
913			i = SCD_READ(sc, IREG_RESULT);
914			break;
915		}
916
917		if (--mbx->nblk > 0) {
918			mbx->skip += mbx->sz;
919			goto nextblock;
920		}
921
922		/* return buffer */
923		bp->bio_resid = 0;
924		biodone(bp);
925
926		sc->data.flags &= ~SCDMBXBSY;
927		scd_start(sc);
928		return;
929	}
930
931readerr:
932	if (mbx->retry-- > 0) {
933		device_printf(sc->dev, "retrying ...\n");
934		state = SCD_S_BEGIN1;
935		goto loop;
936	}
937harderr:
938	/* invalidate the buffer */
939	bp->bio_error = EIO;
940	bp->bio_flags |= BIO_ERROR;
941	bp->bio_resid = bp->bio_bcount;
942	biodone(bp);
943
944	sc->data.flags &= ~SCDMBXBSY;
945	scd_start(sc);
946	return;
947
948changed:
949	device_printf(sc->dev, "media changed\n");
950	goto harderr;
951}
952
953static void
954hsg2msf(int hsg, bcd_t *msf)
955{
956
957	hsg += 150;
958	M_msf(msf) = bin2bcd(hsg / 4500);
959	hsg %= 4500;
960	S_msf(msf) = bin2bcd(hsg / 75);
961	F_msf(msf) = bin2bcd(hsg % 75);
962}
963
964static int
965msf2hsg(bcd_t *msf)
966{
967
968	return (bcd2bin(M_msf(msf)) * 60 +
969		bcd2bin(S_msf(msf))) * 75 +
970		bcd2bin(F_msf(msf)) - 150;
971}
972
973static void
974process_attention(struct scd_softc *sc)
975{
976	unsigned char code;
977	int count = 0;
978
979	while (IS_ATTENTION(sc) && count++ < 30) {
980		SCD_WRITE(sc, OREG_CONTROL, CBIT_ATTENTION_CLEAR);
981		code = SCD_READ(sc, IREG_RESULT);
982
983#ifdef SCD_DEBUG
984		if (scd_debuglevel > 0) {
985			if (count == 1)
986				device_printf(sc->dev, "DEBUG: ATTENTIONS = 0x%x", code);
987			else
988				printf(",0x%x", code);
989		}
990#endif
991
992		switch (code) {
993		case ATTEN_SPIN_DOWN:
994			sc->data.flags &= ~SCDSPINNING;
995			break;
996
997		case ATTEN_SPIN_UP_DONE:
998			sc->data.flags |= SCDSPINNING;
999			break;
1000
1001		case ATTEN_AUDIO_DONE:
1002			sc->data.audio_status = CD_AS_PLAY_COMPLETED;
1003			break;
1004
1005		case ATTEN_DRIVE_LOADED:
1006			sc->data.flags &= ~(SCDTOC|SCDSPINNING|SCDVALID);
1007			sc->data.audio_status = CD_AS_AUDIO_INVALID;
1008			break;
1009
1010		case ATTEN_EJECT_PUSHED:
1011			sc->data.flags &= ~SCDVALID;
1012			break;
1013		}
1014		DELAY(100);
1015	}
1016#ifdef SCD_DEBUG
1017	if (scd_debuglevel > 0 && count > 0)
1018		printf("\n");
1019#endif
1020}
1021
1022/* Returns 0 OR sony error code */
1023static int
1024spin_up(struct scd_softc *sc)
1025{
1026	unsigned char res_reg[12];
1027	unsigned int res_size;
1028	int rc;
1029	int loop_count = 0;
1030
1031again:
1032	rc = send_cmd(sc, CMD_SPIN_UP, 0, 0, res_reg, &res_size);
1033	if (rc != 0) {
1034		XDEBUG(sc, 2, "CMD_SPIN_UP error 0x%x\n", rc);
1035		return (rc);
1036	}
1037
1038	if (!(sc->data.flags & SCDTOC)) {
1039		rc = send_cmd(sc, CMD_READ_TOC, 0);
1040		if (rc == ERR_NOT_SPINNING) {
1041			if (loop_count++ < 3)
1042				goto again;
1043			return (rc);
1044		}
1045		if (rc != 0)
1046			return (rc);
1047	}
1048
1049	sc->data.flags |= SCDSPINNING;
1050
1051	return (0);
1052}
1053
1054static struct sony_tracklist *
1055get_tl(struct sony_toc *toc, int size)
1056{
1057	struct sony_tracklist *tl = &toc->tracks[0];
1058
1059	if (tl->track != 0xb0)
1060		return (tl);
1061	(char *)tl += 9;
1062	if (tl->track != 0xb1)
1063		return (tl);
1064	(char *)tl += 9;
1065	if (tl->track != 0xb2)
1066		return (tl);
1067	(char *)tl += 9;
1068	if (tl->track != 0xb3)
1069		return (tl);
1070	(char *)tl += 9;
1071	if (tl->track != 0xb4)
1072		return (tl);
1073	(char *)tl += 9;
1074	if (tl->track != 0xc0)
1075		return (tl);
1076	(char *)tl += 9;
1077	return (tl);
1078}
1079
1080static int
1081read_toc(struct scd_softc *sc)
1082{
1083	struct sony_toc toc;
1084	struct sony_tracklist *tl;
1085	int rc, i, j;
1086	u_long first, last;
1087
1088	rc = send_cmd(sc, CMD_GET_TOC, 1, 1);
1089	if (rc < 0)
1090		return (rc);
1091	if (rc > sizeof(toc)) {
1092		device_printf(sc->dev, "program error: toc too large (%d)\n", rc);
1093		return (EIO);
1094	}
1095	if (get_result(sc, rc, (u_char *)&toc) != 0)
1096		return (EIO);
1097
1098	XDEBUG(sc, 1, "toc read. len = %d, sizeof(toc) = %d\n", rc, sizeof(toc));
1099
1100	tl = get_tl(&toc, rc);
1101	first = msf2hsg(tl->start_msf);
1102	last = msf2hsg(toc.lead_out_start_msf);
1103	sc->data.blksize = SCDBLKSIZE;
1104	sc->data.disksize = last*sc->data.blksize/DEV_BSIZE;
1105
1106	XDEBUG(sc, 1, "firstsector = %ld, lastsector = %ld", first, last);
1107
1108	sc->data.first_track = bcd2bin(toc.first_track);
1109	sc->data.last_track = bcd2bin(toc.last_track);
1110	if (sc->data.last_track > (MAX_TRACKS-2))
1111		sc->data.last_track = MAX_TRACKS-2;
1112	for (j = 0, i = sc->data.first_track; i <= sc->data.last_track; i++, j++) {
1113		sc->data.toc[i].adr = tl[j].adr;
1114		sc->data.toc[i].ctl = tl[j].ctl; /* for xcdplayer */
1115		bcopy(tl[j].start_msf, sc->data.toc[i].start_msf, 3);
1116#ifdef SCD_DEBUG
1117		if (scd_debuglevel > 0) {
1118			if ((j % 3) == 0) {
1119				printf("\n");
1120				device_printf(sc->dev, "tracks ");
1121			}
1122			printf("[%03d: %2d %2d %2d]  ", i,
1123				bcd2bin(sc->data.toc[i].start_msf[0]),
1124				bcd2bin(sc->data.toc[i].start_msf[1]),
1125				bcd2bin(sc->data.toc[i].start_msf[2]));
1126		}
1127#endif
1128	}
1129	bcopy(toc.lead_out_start_msf, sc->data.toc[sc->data.last_track+1].start_msf, 3);
1130#ifdef SCD_DEBUG
1131	if (scd_debuglevel > 0) {
1132		i = sc->data.last_track+1;
1133		printf("[END: %2d %2d %2d]\n",
1134			bcd2bin(sc->data.toc[i].start_msf[0]),
1135			bcd2bin(sc->data.toc[i].start_msf[1]),
1136			bcd2bin(sc->data.toc[i].start_msf[2]));
1137	}
1138#endif
1139
1140	sc->data.flags |= SCDTOC;
1141
1142	return (0);
1143}
1144
1145static void
1146init_drive(struct scd_softc *sc)
1147{
1148	int rc;
1149
1150	rc = send_cmd(sc, CMD_SET_DRIVE_PARAM, 2,
1151		0x05, 0x03 | ((sc->data.double_speed) ? 0x04: 0));
1152	if (rc != 0)
1153		device_printf(sc->dev, "Unable to set parameters. Errcode = 0x%x\n", rc);
1154}
1155
1156/* Returns 0 or errno */
1157static int
1158get_result(struct scd_softc *sc, int result_len, u_char *result)
1159{
1160	int loop_index = 2; /* send_cmd() reads two bytes ... */
1161
1162	XDEBUG(sc, 1, "DEBUG: get_result: bytes=%d\n", result_len);
1163
1164	while (result_len-- > 0) {
1165		if (loop_index++ >= 10) {
1166			loop_index = 1;
1167			if (waitfor_status_bits(sc, SBIT_RESULT_READY, 0))
1168				return (EIO);
1169			SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
1170		}
1171		if (result)
1172			*result++ = SCD_READ(sc, IREG_RESULT);
1173		else
1174			(void)SCD_READ(sc, IREG_RESULT);
1175	}
1176	return (0);
1177}
1178
1179/* Returns -0x100 for timeout, -(drive error code) OR number of result bytes */
1180static int
1181send_cmd(struct scd_softc *sc, u_char cmd, u_int nargs, ...)
1182{
1183	va_list ap;
1184	u_char c;
1185	int rc;
1186	int i;
1187
1188	if (waitfor_status_bits(sc, 0, SBIT_BUSY)) {
1189		device_printf(sc->dev, "drive busy\n");
1190		return (-0x100);
1191	}
1192
1193	XDEBUG(sc, 1, "DEBUG: send_cmd: cmd=0x%x nargs=%d", cmd, nargs);
1194
1195	SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
1196	SCD_WRITE(sc, OREG_CONTROL, CBIT_RPARAM_CLEAR);
1197
1198	for (i = 0; i < 100; i++)
1199		if (FSTATUS_BIT(sc, FBIT_WPARAM_READY))
1200			break;
1201	if (!FSTATUS_BIT(sc, FBIT_WPARAM_READY)) {
1202		XDEBUG(sc, 1, "\nwparam timeout\n");
1203		return (-EIO);
1204	}
1205
1206	va_start(ap, nargs);
1207	for (i = 0; i < nargs; i++) {
1208		c = (u_char)va_arg(ap, int);
1209		SCD_WRITE(sc, OREG_WPARAMS, c);
1210		XDEBUG(sc, 1, ",{0x%x}", c);
1211	}
1212	va_end(ap);
1213	XDEBUG(sc, 1, "\n");
1214
1215	SCD_WRITE(sc, OREG_COMMAND, cmd);
1216
1217	rc = waitfor_status_bits(sc, SBIT_RESULT_READY, SBIT_BUSY);
1218	if (rc)
1219		return (-0x100);
1220
1221	SCD_WRITE(sc, OREG_CONTROL, CBIT_RESULT_READY_CLEAR);
1222	switch ((rc = SCD_READ(sc, IREG_RESULT)) & 0xf0) {
1223	case 0x20:
1224		rc = SCD_READ(sc, IREG_RESULT);
1225		/* FALLTHROUGH */
1226	case 0x50:
1227		XDEBUG(sc, 1, "DEBUG: send_cmd: drive_error=0x%x\n", rc);
1228		return (-rc);
1229	case 0x00:
1230	default:
1231		rc = SCD_READ(sc, IREG_RESULT);
1232		XDEBUG(sc, 1, "DEBUG: send_cmd: result_len=%d\n", rc);
1233		return (rc);
1234	}
1235}
1236
1237static void
1238print_error(struct scd_softc *sc, int errcode)
1239{
1240
1241	switch (errcode) {
1242	case -ERR_CD_NOT_LOADED:
1243		device_printf(sc->dev, "door is open\n");
1244		break;
1245	case -ERR_NO_CD_INSIDE:
1246		device_printf(sc->dev, "no cd inside\n");
1247		break;
1248	default:
1249		if (errcode == -0x100 || errcode > 0)
1250			device_printf(sc->dev, "device timeout\n");
1251		else
1252			device_printf(sc->dev, "unexpected error 0x%x\n", -errcode);
1253		break;
1254	}
1255}
1256
1257/* Returns 0 or errno value */
1258static int
1259waitfor_status_bits(struct scd_softc *sc, int bits_set, int bits_clear)
1260{
1261	u_int flags = sc->data.flags;
1262	u_int max_loop;
1263	u_char c = 0;
1264
1265	if (flags & SCDPROBING) {
1266		max_loop = 0;
1267		while (max_loop++ < 1000) {
1268			c = SCD_READ(sc, IREG_STATUS);
1269			if (c == 0xff)
1270				return (EIO);
1271			if (c & SBIT_ATTENTION) {
1272				process_attention(sc);
1273				continue;
1274			}
1275			if ((c & bits_set) == bits_set &&
1276			    (c & bits_clear) == 0)
1277			{
1278				break;
1279			}
1280			DELAY(10000);
1281		}
1282	} else {
1283		max_loop = 100;
1284		while (max_loop-- > 0) {
1285			c = SCD_READ(sc, IREG_STATUS);
1286			if (c & SBIT_ATTENTION) {
1287				process_attention(sc);
1288				continue;
1289			}
1290			if ((c & bits_set) == bits_set &&
1291			    (c & bits_clear) == 0)
1292			{
1293				break;
1294			}
1295			tsleep(waitfor_status_bits, PZERO - 1, "waitfor", hz/10);
1296		}
1297	}
1298	if ((c & bits_set) == bits_set &&
1299	    (c & bits_clear) == 0)
1300	{
1301		return (0);
1302	}
1303#ifdef SCD_DEBUG
1304	if (scd_debuglevel > 0)
1305		device_printf(sc->dev, "DEBUG: waitfor: TIMEOUT (0x%x,(0x%x,0x%x))\n", c, bits_set, bits_clear);
1306	else
1307#endif
1308		device_printf(sc->dev, "timeout.\n");
1309	return (EIO);
1310}
1311
1312/* these two routines for xcdplayer - "borrowed" from mcd.c */
1313static int
1314scd_toc_header (struct scd_softc *sc, struct ioc_toc_header* th)
1315{
1316	int rc;
1317
1318	if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) {
1319		print_error(sc, rc);
1320		return (EIO);
1321	}
1322
1323	th->starting_track = sc->data.first_track;
1324	th->ending_track = sc->data.last_track;
1325	th->len = 0; /* not used */
1326
1327	return (0);
1328}
1329
1330static int
1331scd_toc_entrys (struct scd_softc *sc, struct ioc_read_toc_entry *te)
1332{
1333	struct cd_toc_entry toc_entry;
1334	int rc, i, len = te->data_len;
1335
1336	if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) {
1337		print_error(sc, rc);
1338		return (EIO);
1339	}
1340
1341	/* find the toc to copy*/
1342	i = te->starting_track;
1343	if (i == SCD_LASTPLUS1)
1344		i = sc->data.last_track + 1;
1345
1346	/* verify starting track */
1347	if (i < sc->data.first_track || i > sc->data.last_track+1)
1348		return (EINVAL);
1349
1350	/* valid length ? */
1351	if (len < sizeof(struct cd_toc_entry)
1352	    || (len % sizeof(struct cd_toc_entry)) != 0)
1353		return (EINVAL);
1354
1355	/* copy the toc data */
1356	toc_entry.control = sc->data.toc[i].ctl;
1357	toc_entry.addr_type = te->address_format;
1358	toc_entry.track = i;
1359	if (te->address_format == CD_MSF_FORMAT) {
1360		toc_entry.addr.msf.unused = 0;
1361		toc_entry.addr.msf.minute = bcd2bin(sc->data.toc[i].start_msf[0]);
1362		toc_entry.addr.msf.second = bcd2bin(sc->data.toc[i].start_msf[1]);
1363		toc_entry.addr.msf.frame = bcd2bin(sc->data.toc[i].start_msf[2]);
1364	}
1365
1366	/* copy the data back */
1367	if (copyout(&toc_entry, te->data, sizeof(struct cd_toc_entry)) != 0)
1368		return (EFAULT);
1369
1370	return (0);
1371}
1372
1373
1374static int
1375scd_toc_entry (struct scd_softc *sc, struct ioc_read_toc_single_entry *te)
1376{
1377	struct cd_toc_entry toc_entry;
1378	int rc, i;
1379
1380	if (!(sc->data.flags & SCDTOC) && (rc = read_toc(sc)) != 0) {
1381		print_error(sc, rc);
1382		return (EIO);
1383	}
1384
1385	/* find the toc to copy*/
1386	i = te->track;
1387	if (i == SCD_LASTPLUS1)
1388		i = sc->data.last_track + 1;
1389
1390	/* verify starting track */
1391	if (i < sc->data.first_track || i > sc->data.last_track+1)
1392		return (EINVAL);
1393
1394	/* copy the toc data */
1395	toc_entry.control = sc->data.toc[i].ctl;
1396	toc_entry.addr_type = te->address_format;
1397	toc_entry.track = i;
1398	if (te->address_format == CD_MSF_FORMAT) {
1399		toc_entry.addr.msf.unused = 0;
1400		toc_entry.addr.msf.minute = bcd2bin(sc->data.toc[i].start_msf[0]);
1401		toc_entry.addr.msf.second = bcd2bin(sc->data.toc[i].start_msf[1]);
1402		toc_entry.addr.msf.frame = bcd2bin(sc->data.toc[i].start_msf[2]);
1403	}
1404
1405	/* copy the data back */
1406	bcopy(&toc_entry, &te->entry, sizeof(struct cd_toc_entry));
1407
1408	return (0);
1409}
1410