scd.c revision 7332
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 withough 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
44/* $Id: scd.c,v 1.7 1995/02/09 11:30:07 jkh Exp $ */
45
46/* Please send any comments to micke@dynas.se */
47
48#define	SCD_DEBUG	0
49
50#include "scd.h"
51#if NSCD > 0
52#include <sys/types.h>
53#include <sys/param.h>
54#include <sys/systm.h>
55#include <sys/conf.h>
56#include <sys/file.h>
57#include <sys/buf.h>
58#include <sys/stat.h>
59#include <sys/uio.h>
60#include <sys/ioctl.h>
61#include <sys/cdio.h>
62#include <sys/errno.h>
63#include <sys/dkbad.h>
64#include <sys/disklabel.h>
65#include <sys/devconf.h>
66#include <machine/stdarg.h>
67
68#include <i386/isa/isa.h>
69#include <i386/isa/isa_device.h>
70#include <gnu/i386/scdreg.h>
71
72#define scd_part(dev)	((minor(dev)) & 7)
73#define scd_unit(dev)	(((minor(dev)) & 0x38) >> 3)
74#define scd_phys(dev)	(((minor(dev)) & 0x40) >> 6)
75#define RAW_PART        2
76
77/* flags */
78#define SCDOPEN		0x0001	/* device opened */
79#define SCDVALID	0x0002	/* parameters loaded */
80#define SCDINIT		0x0004	/* device is init'd */
81#define	SCDPROBING	0x0020	/* probing */
82#define	SCDTOC		0x0100	/* already read toc */
83#define	SCDMBXBSY	0x0200	/* local mbx is busy */
84#define	SCDSPINNING	0x0400  /* drive is spun up */
85
86#define SCD_S_BEGIN	0
87#define SCD_S_BEGIN1	1
88#define SCD_S_WAITSTAT	2
89#define	SCD_S_WAITFIFO	3
90#define SCD_S_WAITSPIN	4
91#define SCD_S_WAITREAD	5
92#define	SCD_S_WAITPARAM 6
93
94#define RDELAY_WAIT	300
95#define RDELAY_WAITREAD	300
96
97#define	SCDBLKSIZE	2048
98
99#ifdef SCD_DEBUG
100   int scd_debuglevel = SCD_DEBUG;
101#  define XDEBUG(level, data) {if (scd_debuglevel >= level) printf data;}
102#else
103#  define XDEBUG(level, data)
104#endif
105
106struct scd_mbx {
107	short		unit;
108	short		port;
109	short		retry;
110	short		nblk;
111	int		sz;
112	u_long		skip;
113	struct buf	*bp;
114	int		p_offset;
115	short		count;
116};
117
118struct scd_data {
119	int	iobase;
120	char	double_speed;
121	char	*name;
122	short	flags;
123	int	blksize;
124	u_long	disksize;
125	struct disklabel dlabel;
126	int	openflag;
127	struct {
128		unsigned char adr :4;
129		unsigned char ctl :4; /* xcdplayer needs this */
130		unsigned char start_msf[3];
131	} toc[MAX_TRACKS];
132	short	first_track;
133	short	last_track;
134	struct	ioc_play_msf last_play;
135
136	short	audio_status;
137	struct buf head;		/* head of buf queue */
138	struct scd_mbx mbx;
139} scd_data[NSCD];
140
141/* prototypes */
142int	scdopen(dev_t dev);
143int	scdclose(dev_t dev);
144void	scdstrategy(struct buf *bp);
145int	scdioctl(dev_t dev, int cmd, caddr_t addr, int flags);
146int	scdsize(dev_t dev);
147
148static	int	bcd2bin(bcd_t b);
149static	bcd_t	bin2bcd(int b);
150static	void	hsg2msf(int hsg, bcd_t *msf);
151static	int	msf2hsg(bcd_t *msf);
152
153static void process_attention(unsigned unit);
154static inline void write_control(unsigned port, unsigned data);
155static int waitfor_status_bits(int unit, int bits_set, int bits_clear);
156static int waitfor_attention(int unit);
157static int send_cmd(u_int unit, u_char cmd, u_int nargs, ...);
158static void init_drive(unsigned unit);
159static int spin_up(unsigned unit);
160static int read_toc(dev_t dev);
161static int get_result(u_int unit, int result_len, u_char *result);
162static void print_error(int unit, int errcode);
163
164static void scd_start(int unit);
165static void scd_doread(int state, struct scd_mbx *mbxin);
166
167static int scd_eject(int unit);
168static int scd_stop(int unit);
169static int scd_pause(int unit);
170static int scd_resume(int unit);
171static int scd_playtracks(int unit, struct ioc_play_track *pt);
172static int scd_playmsf(int unit, struct ioc_play_msf *msf);
173static int scd_play(int unit, struct ioc_play_msf *msf);
174static int scd_subchan(int unit, struct ioc_read_subchannel *sc);
175static int read_subcode(int unit, struct sony_subchannel_position_data *sc);
176
177/* for xcdplayer */
178static int scd_toc_header(int unit, struct ioc_toc_header *th);
179static int scd_toc_entrys(int unit, struct ioc_read_toc_entry *te);
180#define SCD_LASTPLUS1 170 /* don't ask, xcdplayer passes this in */
181
182extern	int	hz;
183
184int	scd_probe(struct isa_device *dev);
185int	scd_attach(struct isa_device *dev);
186struct	isa_driver	scddriver = { scd_probe, scd_attach, "scd" };
187
188static struct kern_devconf kdc_scd[NSCD] = { {
189	0, 0, 0,		/* filled in by dev_attach */
190	"scd", 0, { MDDT_ISA, 0, "bio" },
191	isa_generic_externalize, 0, 0, ISA_EXTERNALLEN,
192	&kdc_isa0,		/* parent */
193	0,			/* parentdata */
194	DC_IDLE,		/* status */
195	"Sony CD-ROM drive"     /* properly filled later */
196} };
197
198static inline void
199scd_registerdev(struct isa_device *id)
200{
201	if(id->id_unit)
202		kdc_scd[id->id_unit] = kdc_scd[0];
203	kdc_scd[id->id_unit].kdc_unit = id->id_unit;
204	kdc_scd[id->id_unit].kdc_isa = id;
205	dev_attach(&kdc_scd[id->id_unit]);
206}
207
208int scd_attach(struct isa_device *dev)
209{
210	struct scd_data *cd = scd_data + dev->id_unit;
211	int i;
212
213	cd->iobase = dev->id_iobase;	/* Already set by probe, but ... */
214
215	scd_registerdev(dev);
216	/* name filled in probe */
217	kdc_scd[dev->id_unit].kdc_description = scd_data[dev->id_unit].name;
218	printf("scd%d: <%s>\n", dev->id_unit, scd_data[dev->id_unit].name);
219
220	init_drive(dev->id_unit);
221
222	cd->flags = SCDINIT;
223	cd->audio_status = CD_AS_AUDIO_INVALID;
224
225	return 1;
226}
227
228int
229scdopen(dev_t dev)
230{
231	int unit,part,phys;
232	int rc;
233	struct scd_data *cd;
234
235	unit = scd_unit(dev);
236	if (unit >= NSCD)
237		return ENXIO;
238
239	cd = scd_data + unit;
240	part = scd_part(dev);
241	phys = scd_phys(dev);
242
243	/* not initialized*/
244	if (!(cd->flags & SCDINIT))
245		return ENXIO;
246
247	/* invalidated in the meantime? mark all open part's invalid */
248	if (cd->openflag)
249		return ENXIO;
250
251	XDEBUG(1,("scd%d: DEBUG: status = 0x%x\n", unit, inb(cd->iobase+IREG_STATUS)));
252
253	if ((rc = spin_up(unit)) != 0) {
254		print_error(unit, rc);
255		return EIO;
256	}
257	if (!(cd->flags & SCDTOC)) {
258		int loop_count = 3;
259
260		while (loop_count-- > 0 && (rc = read_toc(dev)) != 0) {
261			if (rc == ERR_NOT_SPINNING) {
262				rc = spin_up(unit);
263				if (rc) {
264					print_error(unit, rc);\
265					return EIO;
266				}
267				continue;
268			}
269			printf("scd%d: TOC read error 0x%x\n", unit, rc);
270			return EIO;
271		}
272	}
273
274	cd->openflag = 1;
275	cd->flags |= SCDVALID;
276	kdc_scd[unit].kdc_state = DC_BUSY;
277
278	return 0;
279}
280
281int
282scdclose(dev_t dev)
283{
284	int unit,part,phys;
285	struct scd_data *cd;
286	int rlen;
287	char rdata[10];
288
289	unit = scd_unit(dev);
290	if (unit >= NSCD)
291		return ENXIO;
292
293	cd = scd_data + unit;
294	part = scd_part(dev);
295	phys = scd_phys(dev);
296
297	if (!(cd->flags & SCDINIT) || !cd->openflag)
298		return ENXIO;
299
300	if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS) {
301		(void)send_cmd(unit, CMD_SPIN_DOWN, 0);
302		cd->flags &= ~SCDSPINNING;
303	}
304
305	kdc_scd[unit].kdc_state = DC_IDLE;
306
307	/* close channel */
308	cd->openflag = 0;
309
310	return 0;
311}
312
313void
314scdstrategy(struct buf *bp)
315{
316	struct scd_data *cd;
317	struct buf *qp;
318	int s;
319	int unit = scd_unit(bp->b_dev);
320
321	cd = scd_data + unit;
322
323	XDEBUG(2, ("scd%d: DEBUG: strategy: block=%ld, bcount=%ld\n", unit, bp->b_blkno, bp->b_bcount));
324
325	if (unit >= NSCD || bp->b_blkno < 0 || (bp->b_bcount % SCDBLKSIZE)) {
326		printf("scd%d: strategy failure: blkno = %d, bcount = %d\n",
327			unit, bp->b_blkno, bp->b_bcount);
328		bp->b_error = EINVAL;
329		bp->b_flags |= B_ERROR;
330		goto bad;
331	}
332
333	/* if device invalidated (e.g. media change, door open), error */
334	if (!(cd->flags & SCDVALID)) {
335		printf("scd%d: media changed\n", unit);
336		bp->b_error = EIO;
337		goto bad;
338	}
339
340	/* read only */
341	if (!(bp->b_flags & B_READ)) {
342		bp->b_error = EROFS;
343		goto bad;
344	}
345
346	/* no data to read */
347	if (bp->b_bcount == 0)
348		goto done;
349
350	if (!(cd->flags & SCDTOC)) {
351		bp->b_error = EIO;
352		goto bad;
353	}
354	/* adjust transfer if necessary */
355	if (bounds_check_with_label(bp,&cd->dlabel,1) <= 0)
356		goto done;
357
358	bp->b_pblkno = bp->b_blkno;
359	bp->b_resid = 0;
360
361	/* queue it */
362	qp = &cd->head;
363	s = splbio();
364	disksort(qp,bp);
365	splx(s);
366
367	/* now check whether we can perform processing */
368	scd_start(unit);
369	return;
370
371bad:
372	bp->b_flags |= B_ERROR;
373done:
374	bp->b_resid = bp->b_bcount;
375	biodone(bp);
376	return;
377}
378
379static void
380scd_start(int unit)
381{
382	struct scd_data *cd = scd_data + unit;
383	struct buf *bp, *qp = &cd->head;
384	struct partition *p;
385	int part;
386	register s = splbio();
387
388	if (cd->flags & SCDMBXBSY) {
389		splx(s);
390		return;
391	}
392
393	if ((bp = qp->b_actf) != 0) {
394		/* block found to process, dequeue */
395		qp->b_actf = bp->b_actf;
396		cd->flags |= SCDMBXBSY;
397		splx(s);
398	} else {
399		/* nothing to do */
400		splx(s);
401		return;
402	}
403
404	p = cd->dlabel.d_partitions + scd_part(bp->b_dev);
405
406	cd->mbx.unit = unit;
407	cd->mbx.port = cd->iobase;
408	cd->mbx.retry = 3;
409	cd->mbx.bp = bp;
410	cd->mbx.p_offset = p->p_offset;
411	splx(s);
412
413	scd_doread(SCD_S_BEGIN,&(cd->mbx));
414	return;
415}
416
417int
418scdioctl(dev_t dev, int cmd, caddr_t addr, int flags)
419{
420	struct scd_data *cd;
421	int unit,part;
422
423	unit = scd_unit(dev);
424	part = scd_part(dev);
425	cd = scd_data + unit;
426
427	XDEBUG(1, ("scd%d: ioctl: cmd=0x%lx\n", unit, cmd));
428
429	if (!(cd->flags & SCDVALID))
430		return EIO;
431
432	switch (cmd) {
433	case DIOCSBAD:
434		return EINVAL;
435	case DIOCGDINFO:
436		*(struct disklabel *)addr = cd->dlabel;
437		return 0;
438	case DIOCGPART:
439		((struct partinfo *)addr)->disklab = &cd->dlabel;
440		((struct partinfo *)addr)->part =
441			&cd->dlabel.d_partitions[0];
442		return 0;
443	case CDIOCPLAYTRACKS:
444		return scd_playtracks(unit, (struct ioc_play_track *) addr);
445	case CDIOCPLAYBLOCKS:
446		return EINVAL;
447	case CDIOCPLAYMSF:
448		return scd_playmsf(unit, (struct ioc_play_msf *) addr);
449	case CDIOCREADSUBCHANNEL:
450		return scd_subchan(unit, (struct ioc_read_subchannel *) addr);
451	case CDIOREADTOCHEADER:
452		return scd_toc_header (unit, (struct ioc_toc_header *) addr);
453	case CDIOREADTOCENTRYS:
454		return scd_toc_entrys (unit, (struct ioc_read_toc_entry*) addr);
455	case CDIOCSETPATCH:
456	case CDIOCGETVOL:
457	case CDIOCSETVOL:
458	case CDIOCSETMONO:
459	case CDIOCSETSTERIO:
460	case CDIOCSETMUTE:
461	case CDIOCSETLEFT:
462	case CDIOCSETRIGHT:
463		return EINVAL;
464	case CDIOCRESUME:
465		return scd_resume(unit);
466	case CDIOCPAUSE:
467		return scd_pause(unit);
468	case CDIOCSTART:
469		return EINVAL;
470	case CDIOCSTOP:
471		return scd_stop(unit);
472	case CDIOCEJECT:
473		return scd_eject(unit);
474	case CDIOCALLOW:
475		return 0;
476	case CDIOCSETDEBUG:
477#ifdef SCD_DEBUG
478		scd_debuglevel++;
479#endif
480		return 0;
481	case CDIOCCLRDEBUG:
482#ifdef SCD_DEBUG
483		scd_debuglevel = 0;
484
485#endif
486		return 0;
487	default:
488		printf("scd%d: unsupported ioctl (cmd=0x%lx)\n", unit, cmd);
489		return ENOTTY;
490	}
491}
492
493int
494scdsize(dev_t dev)
495{
496	return -1;
497}
498
499void
500scdintr()
501{
502	return;
503}
504
505/***************************************************************
506 * lower level of driver starts here
507 **************************************************************/
508
509static int
510scd_playtracks(int unit, struct ioc_play_track *pt)
511{
512	struct scd_data *cd = scd_data + unit;
513	struct ioc_play_msf msf;
514	int a = pt->start_track;
515	int z = pt->end_track;
516	int rc, i;
517
518	if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
519		if (rc == -ERR_NOT_SPINNING) {
520			if (spin_up(unit) != 0)
521				return EIO;
522			rc = read_toc(unit);
523		}
524		if (rc != 0) {
525			print_error(unit, rc);
526			return EIO;
527		}
528	}
529
530	XDEBUG(1, ("scd%d: playtracks from %d:%d to %d:%d\n", unit,
531		a, pt->start_index, z, pt->end_index));
532
533	if (   a < cd->first_track
534	    || a > cd->last_track
535	    || a > z
536	    || z > cd->last_track)
537		return EINVAL;
538
539	bcopy(cd->toc[a].start_msf, &msf.start_m, 3);
540	hsg2msf(msf2hsg(cd->toc[z+1].start_msf)-1, &msf.end_m);
541
542	return scd_play(unit, &msf);
543}
544
545/* The start/end msf is expected to be in bin format */
546static int
547scd_playmsf(int unit, struct ioc_play_msf *msfin)
548{
549	struct ioc_play_msf msf;
550
551	msf.start_m = bin2bcd(msfin->start_m);
552	msf.start_s = bin2bcd(msfin->start_s);
553	msf.start_f = bin2bcd(msfin->start_f);
554	msf.end_m = bin2bcd(msfin->end_m);
555	msf.end_s = bin2bcd(msfin->end_s);
556	msf.end_f = bin2bcd(msfin->end_f);
557
558	return scd_play(unit, &msf);
559}
560
561/* The start/end msf is expected to be in bcd format */
562static int
563scd_play(int unit, struct ioc_play_msf *msf)
564{
565	struct scd_data *cd = scd_data + unit;
566	int i, rc;
567
568	XDEBUG(1, ("scd%d: playing: %02x:%02x:%02x -> %02x:%02x:%02x\n", unit,
569		msf->start_m, msf->start_s, msf->start_f,
570		msf->end_m, msf->end_s, msf->end_f));
571
572	for (i = 0; i < 2; i++) {
573		rc = send_cmd(unit, CMD_PLAY_AUDIO, 7,
574			0x03,
575			msf->start_m, msf->start_s, msf->start_f,
576			msf->end_m, msf->end_s, msf->end_f);
577		if (rc == -ERR_NOT_SPINNING) {
578			cd->flags &= ~SCDSPINNING;
579			if (spin_up(unit) != 0)
580				return EIO;
581		} else if (rc < 0) {
582			print_error(unit, rc);
583			return EIO;
584		} else {
585			break;
586		}
587	}
588	cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
589	bcopy((char *)msf, (char *)&cd->last_play, sizeof(struct ioc_play_msf));
590	return 0;
591}
592
593static int
594scd_stop(int unit)
595{
596	struct scd_data *cd = scd_data + unit;
597
598	(void)send_cmd(unit, CMD_STOP_AUDIO, 0);
599	cd->audio_status = CD_AS_PLAY_COMPLETED;
600	return 0;
601}
602
603static int
604scd_pause(int unit)
605{
606	struct scd_data *cd = scd_data + unit;
607	struct sony_subchannel_position_data subpos;
608
609	if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS)
610		return EINVAL;
611
612	if (read_subcode(unit, &subpos) != 0)
613		return EIO;
614
615	if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0)
616		return EIO;
617
618	cd->last_play.start_m = subpos.abs_msf[0];
619	cd->last_play.start_s = subpos.abs_msf[1];
620	cd->last_play.start_f = subpos.abs_msf[2];
621	cd->audio_status = CD_AS_PLAY_PAUSED;
622
623	XDEBUG(1, ("scd%d: pause @ %02x:%02x:%02x\n", unit,
624		cd->last_play.start_m,
625		cd->last_play.start_s,
626		cd->last_play.start_f));
627
628	return 0;
629}
630
631static int
632scd_resume(int unit)
633{
634	if (scd_data[unit].audio_status != CD_AS_PLAY_PAUSED)
635		return EINVAL;
636	return scd_play(unit, &scd_data[unit].last_play);
637}
638
639static int
640scd_eject(int unit)
641{
642	struct scd_data *cd = scd_data + unit;
643	int port = cd->iobase;
644
645	cd->audio_status = CD_AS_AUDIO_INVALID;
646	cd->flags &= ~(SCDSPINNING|SCDTOC);
647
648	if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0 ||
649	    send_cmd(unit, CMD_SPIN_DOWN, 0) != 0 ||
650	    send_cmd(unit, CMD_EJECT, 0) != 0)
651	{
652		return EIO;
653	}
654	return 0;
655}
656
657static int
658scd_subchan(int unit, struct ioc_read_subchannel *sc)
659{
660	struct scd_data *cd = scd_data + unit;
661	struct sony_subchannel_position_data q;
662	struct cd_sub_channel_info data;
663
664	XDEBUG(1, ("scd%d: subchan af=%d, df=%d\n", unit,
665		sc->address_format,
666		sc->data_format));
667
668	if (sc->address_format != CD_MSF_FORMAT)
669		return EINVAL;
670
671	if (sc->data_format != CD_CURRENT_POSITION)
672		return EINVAL;
673
674	if (read_subcode(unit, &q) != 0)
675		return EIO;
676
677	data.header.audio_status = cd->audio_status;
678	data.what.position.data_format = CD_MSF_FORMAT;
679	data.what.position.track_number = bcd2bin(q.track_number);
680	data.what.position.reladdr.msf.unused = 0;
681	data.what.position.reladdr.msf.minute = bcd2bin(q.rel_msf[0]);
682	data.what.position.reladdr.msf.second = bcd2bin(q.rel_msf[1]);
683	data.what.position.reladdr.msf.frame = bcd2bin(q.rel_msf[2]);
684	data.what.position.absaddr.msf.unused = 0;
685	data.what.position.absaddr.msf.minute = bcd2bin(q.abs_msf[0]);
686	data.what.position.absaddr.msf.second = bcd2bin(q.abs_msf[1]);
687	data.what.position.absaddr.msf.frame = bcd2bin(q.abs_msf[2]);
688
689	if (copyout(&data, sc->data, min(sizeof(struct cd_sub_channel_info), sc->data_len))!=0)
690		return EFAULT;
691	return 0;
692}
693
694int
695scd_probe(struct isa_device *dev)
696{
697	struct sony_drive_configuration drive_config;
698	int unit = dev->id_unit;
699	int rc;
700	static char namebuf[8+16+8+3];
701	char *s = namebuf;
702	int loop_count = 0;
703
704	scd_data[unit].flags = SCDPROBING;
705	scd_data[unit].iobase = dev->id_iobase;
706
707	bzero(&drive_config, sizeof(drive_config));
708
709again:
710	/* Reset drive */
711	write_control(dev->id_iobase, CBIT_RESET_DRIVE);
712
713	/* Calm down */
714	DELAY(300000);
715
716	/* Only the ATTENTION bit may be set */
717	if ((inb(dev->id_iobase+IREG_STATUS) & ~1) != 0) {
718		XDEBUG(1, ("scd: too many bits set. probe failed.\n"));
719		return 0;
720	}
721	rc = send_cmd(unit, CMD_GET_DRIVE_CONFIG, 0);
722	if (rc != sizeof(drive_config)) {
723		/* Sometimes if the drive is playing audio I get */
724		/* the bad result 82. Fix by repeating the reset */
725		if (rc > 0 && loop_count++ == 0)
726			goto again;
727		return 0;
728	}
729	if (get_result(unit, rc, (u_char *)&drive_config) != 0)
730		return 0;
731
732	bcopy(drive_config.vendor, namebuf, 8);
733	s = namebuf+8;
734	while (*(s-1) == ' ')	/* Strip trailing spaces */
735		s--;
736	*s++ = ' ';
737	bcopy(drive_config.product, s, 16);
738	s += 16;
739	while (*(s-1) == ' ')
740		s--;
741	*s++ = ' ';
742	bcopy(drive_config.revision, s, 8);
743	s += 8;
744	while (*(s-1) == ' ')
745		s--;
746	*s = 0;
747
748	scd_data[unit].name = namebuf;
749
750	if (drive_config.config & 0x10)
751		scd_data[unit].double_speed = 1;
752	else
753		scd_data[unit].double_speed = 0;
754
755	return 4;
756}
757
758static int
759read_subcode(int unit, struct sony_subchannel_position_data *sc)
760{
761	int rc;
762
763	rc = send_cmd(unit, CMD_GET_SUBCHANNEL_DATA, 0);
764	if (rc < 0 || rc < sizeof(*sc))
765		return EIO;
766	if (get_result(unit, rc, (u_char *)sc) != 0)
767		return EIO;
768	return 0;
769}
770
771/* State machine copied from mcd.c */
772
773/* This (and the code in mcd.c) will not work with more than one drive */
774/* because there is only one mbxsave below. Should fix that some day. */
775/* (mbxsave & state should probably be included in the scd_data struct and */
776/*  the unit number used as first argument to scd_doread().) /Micke */
777
778/* state machine to process read requests
779 * initialize with SCD_S_BEGIN: reset state machine
780 * SCD_S_WAITSTAT:  wait for ready (!busy)
781 * SCD_S_WAITSPIN:  wait for drive to spin up (if not spinning)
782 * SCD_S_WAITFIFO:  wait for param fifo to get ready, them exec. command.
783 * SCD_S_WAITREAD:  wait for data ready, read data
784 * SCD_S_WAITPARAM: wait for command result params, read them, error if bad data read.
785 */
786
787static struct scd_mbx *mbxsave;
788
789static void
790scd_doread(int state, struct scd_mbx *mbxin)
791{
792	struct scd_mbx *mbx = (state!=SCD_S_BEGIN) ? mbxsave : mbxin;
793	int	unit = mbx->unit;
794	int	port = mbx->port;
795	struct	buf *bp = mbx->bp;
796	struct	scd_data *cd = scd_data + unit;
797	int	reg,i,k,c;
798	int	blknum;
799	caddr_t	addr;
800	char	rdata[10];
801	static char sdata[3];	/* Must be preserved between calls to this function */
802
803loop:
804	switch (state) {
805	case SCD_S_BEGIN:
806		mbx = mbxsave = mbxin;
807
808	case SCD_S_BEGIN1:
809		/* get status */
810		mbx->count = RDELAY_WAIT;
811
812		process_attention(unit);
813		goto trystat;
814
815	case SCD_S_WAITSTAT:
816		untimeout((timeout_func_t)scd_doread,(caddr_t)SCD_S_WAITSTAT);
817		if (mbx->count-- <= 0) {
818			printf("scd%d: timeout. drive busy.\n",unit);
819			goto harderr;
820		}
821
822trystat:
823		if (IS_BUSY(port)) {
824			timeout((timeout_func_t)scd_doread,
825			    (caddr_t)SCD_S_WAITSTAT,hz/100); /* XXX */
826			return;
827		}
828
829		process_attention(unit);
830
831		/* reject, if audio active */
832		if (cd->audio_status & CD_AS_PLAY_IN_PROGRESS) {
833			printf("scd%d: audio is active\n",unit);
834			goto harderr;
835		}
836
837		mbx->sz = cd->blksize;
838
839firstblock:
840		/* for first block */
841		mbx->nblk = (bp->b_bcount + (mbx->sz-1)) / mbx->sz;
842		mbx->skip = 0;
843
844nextblock:
845		if (!(cd->flags & SCDVALID))
846			goto changed;
847
848		blknum 	= (bp->b_blkno / (mbx->sz/DEV_BSIZE))
849			+ mbx->p_offset + mbx->skip/mbx->sz;
850
851		XDEBUG(2, ("scd%d: scd_doread: read blknum=%d\n", unit, blknum));
852
853		/* build parameter block */
854		hsg2msf(blknum, sdata);
855
856		write_control(port, CBIT_RESULT_READY_CLEAR);
857		write_control(port, CBIT_RPARAM_CLEAR);
858		write_control(port, CBIT_DATA_READY_CLEAR);
859
860		if (FSTATUS_BIT(port, FBIT_WPARAM_READY))
861			goto writeparam;
862
863		mbx->count = 100;
864		timeout((timeout_func_t)scd_doread,
865			(caddr_t)SCD_S_WAITFIFO,hz/100); /* XXX */
866		return;
867
868	case SCD_S_WAITSPIN:
869		untimeout((timeout_func_t)scd_doread,(caddr_t)SCD_S_WAITSPIN);
870		if (mbx->count-- <= 0) {
871			printf("scd%d: timeout waiting for drive to spin up.\n", unit);
872			goto harderr;
873		}
874		if (!STATUS_BIT(port, SBIT_RESULT_READY)) {
875			timeout((timeout_func_t)scd_doread,
876				(caddr_t)SCD_S_WAITSPIN,hz/100); /* XXX */
877			return;
878		}
879		write_control(port, CBIT_RESULT_READY_CLEAR);
880		switch ((i = inb(port+IREG_RESULT)) & 0xf0) {
881		case 0x20:
882			i = inb(port+IREG_RESULT);
883			print_error(unit, i);
884			goto harderr;
885		case 0x00:
886			(void)inb(port+IREG_RESULT);
887			cd->flags |= SCDSPINNING;
888			break;
889		}
890		XDEBUG(1, ("scd%d: DEBUG: spin up complete\n", unit));
891
892		state = SCD_S_BEGIN1;
893		goto loop;
894
895	case SCD_S_WAITFIFO:
896		untimeout((timeout_func_t)scd_doread,(caddr_t)SCD_S_WAITFIFO);
897		if (mbx->count-- <= 0) {
898			printf("scd%d: timeout. write param not ready.\n",unit);
899			goto harderr;
900		}
901		if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) {
902			timeout((timeout_func_t)scd_doread,
903				(caddr_t)SCD_S_WAITFIFO,hz/100); /* XXX */
904			return;
905		}
906		XDEBUG(1, ("scd%d: mbx->count (writeparamwait) = %d(%d)\n", unit, mbx->count, 100));
907
908writeparam:
909		/* The reason this test isn't done 'till now is to make sure */
910		/* that it is ok to send the SPIN_UP cmd below. */
911		if (!(cd->flags & SCDSPINNING)) {
912			XDEBUG(1, ("scd%d: spinning up drive ...\n", unit));
913			outb(port+OREG_COMMAND, CMD_SPIN_UP);
914			mbx->count = 300;
915			timeout((timeout_func_t)scd_doread,
916				(caddr_t)SCD_S_WAITSPIN,hz/100); /* XXX */
917			return;
918		}
919
920		reg = port + OREG_WPARAMS;
921		/* send the read command */
922		disable_intr();
923		outb(reg, sdata[0]);
924		outb(reg, sdata[1]);
925		outb(reg, sdata[2]);
926		outb(reg, 0);
927		outb(reg, 0);
928		outb(reg, 1);
929		outb(port+OREG_COMMAND, CMD_READ);
930		enable_intr();
931
932		mbx->count = RDELAY_WAITREAD;
933		for (i = 0; i < 50; i++) {
934			if (STATUS_BIT(port, SBIT_DATA_READY))
935				goto got_data;
936			DELAY(100);
937		}
938
939		timeout((timeout_func_t)scd_doread,
940			(caddr_t)SCD_S_WAITREAD,hz/100); /* XXX */
941		return;
942
943	case SCD_S_WAITREAD:
944		untimeout((timeout_func_t)scd_doread,(caddr_t)SCD_S_WAITREAD);
945		if (mbx->count-- <= 0) {
946			if (STATUS_BIT(port, SBIT_RESULT_READY))
947				goto got_param;
948			printf("scd%d: timeout while reading data\n",unit);
949			goto readerr;
950		}
951		if (!STATUS_BIT(port, SBIT_DATA_READY)) {
952			process_attention(unit);
953			if (!(cd->flags & SCDVALID))
954				goto changed;
955			timeout((timeout_func_t)scd_doread,
956				(caddr_t)SCD_S_WAITREAD,hz/100); /* XXX */
957			return;
958		}
959		XDEBUG(2, ("scd%d: mbx->count (after RDY_BIT) = %d(%d)\n", unit, mbx->count, RDELAY_WAITREAD));
960
961got_data:
962		/* data is ready */
963		addr = bp->b_un.b_addr + mbx->skip;
964		write_control(port, CBIT_DATA_READY_CLEAR);
965		insb(port+IREG_DATA, addr, mbx->sz);
966
967		mbx->count = 100;
968		for (i = 0; i < 20; i++) {
969			if (STATUS_BIT(port, SBIT_RESULT_READY))
970				goto waitfor_param;
971			DELAY(100);
972		}
973		goto waitfor_param;
974
975	case SCD_S_WAITPARAM:
976		untimeout((timeout_func_t)scd_doread,(caddr_t)SCD_S_WAITPARAM);
977		if (mbx->count-- <= 0) {
978			printf("scd%d: timeout waiting for params\n",unit);
979			goto readerr;
980		}
981
982waitfor_param:
983		if (!STATUS_BIT(port, SBIT_RESULT_READY)) {
984			timeout((timeout_func_t)scd_doread,
985				(caddr_t)SCD_S_WAITPARAM,hz/100); /* XXX */
986			return;
987		}
988#if SCD_DEBUG
989		if (mbx->count < 100 && scd_debuglevel > 0)
990			printf("scd%d: mbx->count (paramwait) = %d(%d)\n", unit, mbx->count, 100);
991#endif
992
993got_param:
994		write_control(port, CBIT_RESULT_READY_CLEAR);
995		switch ((i = inb(port+IREG_RESULT)) & 0xf0) {
996		case 0x50:
997			switch (i) {
998			case ERR_FATAL_READ_ERROR1:
999			case ERR_FATAL_READ_ERROR2:
1000				printf("scd%d: unrecoverable read error 0x%x\n", unit, i);
1001				goto harderr;
1002			}
1003			break;
1004		case 0x20:
1005			i = inb(port+IREG_RESULT);
1006			switch (i) {
1007			case ERR_NOT_SPINNING:
1008				XDEBUG(1, ("scd%d: read error: drive not spinning\n", unit));
1009				if (mbx->retry-- > 0) {
1010					state = SCD_S_BEGIN1;
1011					cd->flags &= ~SCDSPINNING;
1012					goto loop;
1013				}
1014				goto harderr;
1015			default:
1016				print_error(unit, i);
1017				goto readerr;
1018			}
1019		case 0x00:
1020			i = inb(port+IREG_RESULT);
1021			break;
1022		}
1023
1024		if (--mbx->nblk > 0) {
1025			mbx->skip += mbx->sz;
1026			goto nextblock;
1027		}
1028
1029		/* return buffer */
1030		bp->b_resid = 0;
1031		biodone(bp);
1032
1033		cd->flags &= ~SCDMBXBSY;
1034		scd_start(mbx->unit);
1035		return;
1036	}
1037
1038readerr:
1039	if (mbx->retry-- > 0) {
1040		printf("scd%d: retrying ...\n",unit);
1041		state = SCD_S_BEGIN1;
1042		goto loop;
1043	}
1044harderr:
1045	/* invalidate the buffer */
1046	bp->b_error = EIO;
1047	bp->b_flags |= B_ERROR;
1048	bp->b_resid = bp->b_bcount;
1049	biodone(bp);
1050
1051	cd->flags &= ~SCDMBXBSY;
1052	scd_start(mbx->unit);
1053	return;
1054
1055changed:
1056	printf("scd%d: media changed\n", unit);
1057	goto harderr;
1058}
1059
1060static int
1061bcd2bin(bcd_t b)
1062{
1063	return (b >> 4) * 10 + (b & 15);
1064}
1065
1066static bcd_t
1067bin2bcd(int b)
1068{
1069	return ((b / 10) << 4) | (b % 10);
1070}
1071
1072static void
1073hsg2msf(int hsg, bcd_t *msf)
1074{
1075	hsg += 150;
1076	M_msf(msf) = bin2bcd(hsg / 4500);
1077	hsg %= 4500;
1078	S_msf(msf) = bin2bcd(hsg / 75);
1079	F_msf(msf) = bin2bcd(hsg % 75);
1080}
1081
1082static int
1083msf2hsg(bcd_t *msf)
1084{
1085	return (bcd2bin(M_msf(msf)) * 60 +
1086		bcd2bin(S_msf(msf))) * 75 +
1087		bcd2bin(F_msf(msf)) - 150;
1088}
1089
1090static void
1091process_attention(unsigned unit)
1092{
1093	unsigned port = scd_data[unit].iobase;
1094	unsigned char code;
1095	int count = 0;
1096	int i;
1097
1098	while (IS_ATTENTION(port) && count++ < 30) {
1099		write_control(port, CBIT_ATTENTION_CLEAR);
1100		code = inb(port+IREG_RESULT);
1101
1102#if SCD_DEBUG
1103		if (scd_debuglevel > 0) {
1104			if (count == 1)
1105				printf("scd%d: DEBUG: ATTENTIONS = 0x%x", unit, code);
1106			else
1107				printf(",0x%x", code);
1108		}
1109#endif
1110
1111		switch (code) {
1112		case ATTEN_SPIN_DOWN:
1113			scd_data[unit].flags &= ~SCDSPINNING;
1114			break;
1115
1116		case ATTEN_SPIN_UP_DONE:
1117			scd_data[unit].flags |= SCDSPINNING;
1118			break;
1119
1120		case ATTEN_AUDIO_DONE:
1121			scd_data[unit].audio_status = CD_AS_PLAY_COMPLETED;
1122			break;
1123
1124		case ATTEN_DRIVE_LOADED:
1125			scd_data[unit].flags &= ~(SCDTOC|SCDSPINNING|SCDVALID);
1126			scd_data[unit].audio_status = CD_AS_AUDIO_INVALID;
1127			break;
1128
1129		case ATTEN_EJECT_PUSHED:
1130			scd_data[unit].flags &= ~SCDVALID;
1131			break;
1132		}
1133		DELAY(100);
1134	}
1135#if SCD_DEBUG
1136	if (scd_debuglevel > 0 && count > 0)
1137		printf("\n");
1138#endif
1139}
1140
1141/* Returns 0 OR sony error code */
1142static int
1143spin_up(unsigned unit)
1144{
1145	unsigned char res_reg[12];
1146	unsigned int res_size;
1147	int rc;
1148	int loop_count = 0;
1149
1150again:
1151	rc = send_cmd(unit, CMD_SPIN_UP, NULL, 0, res_reg, &res_size);
1152	if (rc != 0) {
1153		XDEBUG(2, ("scd%d: CMD_SPIN_UP error 0x%x\n", unit, rc));
1154		return rc;
1155	}
1156
1157	if (!(scd_data[unit].flags & SCDTOC)) {
1158		rc = send_cmd(unit, CMD_READ_TOC, 0);
1159		if (rc == ERR_NOT_SPINNING) {
1160			if (loop_count++ < 3)
1161				goto again;
1162			return rc;
1163		}
1164		if (rc != 0)
1165			return rc;
1166	}
1167
1168	scd_data[unit].flags |= SCDSPINNING;
1169
1170	return 0;
1171}
1172
1173static struct sony_tracklist *
1174get_tl(struct sony_toc *toc, int size)
1175{
1176	struct sony_tracklist *tl = &toc->tracks[0];
1177
1178	if (tl->track != 0xb0)
1179		return tl;
1180	(char *)tl += 9;
1181	if (tl->track != 0xb1)
1182		return tl;
1183	(char *)tl += 9;
1184	if (tl->track != 0xb2)
1185		return tl;
1186	(char *)tl += 9;
1187	if (tl->track != 0xb3)
1188		return tl;
1189	(char *)tl += 9;
1190	if (tl->track != 0xb4)
1191		return tl;
1192	(char *)tl += 9;
1193	if (tl->track != 0xc0)
1194		return tl;
1195	(char *)tl += 9;
1196	return tl;
1197}
1198
1199static int
1200read_toc(dev_t dev)
1201{
1202	unsigned unit;
1203	struct scd_data *cd;
1204	unsigned part = 0;	/* For now ... */
1205	struct sony_toc toc;
1206	struct sony_tracklist *tl;
1207	int rc, i, j;
1208	u_long first, last;
1209
1210	unit = scd_unit(dev);
1211	cd = scd_data + unit;
1212
1213	rc = send_cmd(unit, CMD_GET_TOC, 1, part+1);
1214	if (rc < 0)
1215		return rc;
1216	if (rc > sizeof(toc)) {
1217		printf("scd%d: program error: toc too large (%d)\n", unit, rc);
1218		return EIO;
1219	}
1220	if (get_result(unit, rc, (u_char *)&toc) != 0)
1221		return EIO;
1222
1223	XDEBUG(1, ("scd%d: toc read. len = %d, sizeof(toc) = %d\n", unit, rc, sizeof(toc)));
1224
1225	tl = get_tl(&toc, rc);
1226	first = msf2hsg(tl->start_msf);
1227	last = msf2hsg(toc.lead_out_start_msf);
1228	cd->blksize = SCDBLKSIZE;
1229	cd->disksize = last*cd->blksize/DEV_BSIZE;
1230
1231	XDEBUG(1, ("scd%d: firstsector = %d, lastsector = %d", unit,
1232			first, last));
1233
1234	cd->first_track = bcd2bin(toc.first_track);
1235	cd->last_track = bcd2bin(toc.last_track);
1236	if (cd->last_track > (MAX_TRACKS-2))
1237		cd->last_track = MAX_TRACKS-2;
1238	for (j = 0, i = cd->first_track; i <= cd->last_track; i++, j++) {
1239		cd->toc[i].adr = tl[j].adr;
1240		cd->toc[i].ctl = tl[j].ctl; /* for xcdplayer */
1241		bcopy(tl[j].start_msf, cd->toc[i].start_msf, 3);
1242#ifdef SCD_DEBUG
1243		if (scd_debuglevel > 0) {
1244			if ((j % 3) == 0)
1245				printf("\nscd%d: tracks ", unit);
1246			printf("[%03d: %2d %2d %2d]  ", i,
1247				bcd2bin(cd->toc[i].start_msf[0]),
1248				bcd2bin(cd->toc[i].start_msf[1]),
1249				bcd2bin(cd->toc[i].start_msf[2]));
1250		}
1251#endif
1252	}
1253	bcopy(toc.lead_out_start_msf, cd->toc[cd->last_track+1].start_msf, 3);
1254#ifdef SCD_DEBUG
1255	if (scd_debuglevel > 0) {
1256		i = cd->last_track+1;
1257		printf("[END: %2d %2d %2d]\n",
1258			bcd2bin(cd->toc[i].start_msf[0]),
1259			bcd2bin(cd->toc[i].start_msf[1]),
1260			bcd2bin(cd->toc[i].start_msf[2]));
1261	}
1262#endif
1263
1264	bzero(&cd->dlabel,sizeof(struct disklabel));
1265	/* filled with spaces first */
1266	strncpy(cd->dlabel.d_typename,"               ",
1267		sizeof(cd->dlabel.d_typename));
1268	strncpy(cd->dlabel.d_typename, cd->name,
1269		min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1));
1270	strncpy(cd->dlabel.d_packname,"unknown        ",
1271		sizeof(cd->dlabel.d_packname));
1272	cd->dlabel.d_secsize 	= cd->blksize;
1273	cd->dlabel.d_nsectors	= 100;
1274	cd->dlabel.d_ntracks	= 1;
1275	cd->dlabel.d_ncylinders	= (cd->disksize/100)+1;
1276	cd->dlabel.d_secpercyl	= 100;
1277	cd->dlabel.d_secperunit	= cd->disksize;
1278	cd->dlabel.d_rpm	= 300;
1279	cd->dlabel.d_interleave	= 1;
1280	cd->dlabel.d_flags	= D_REMOVABLE;
1281	cd->dlabel.d_npartitions= 1;
1282	cd->dlabel.d_partitions[0].p_offset = 0;
1283	cd->dlabel.d_partitions[0].p_size = cd->disksize;
1284	cd->dlabel.d_partitions[0].p_fstype = 9;
1285	cd->dlabel.d_magic	= DISKMAGIC;
1286	cd->dlabel.d_magic2	= DISKMAGIC;
1287	cd->dlabel.d_checksum	= dkcksum(&cd->dlabel);
1288
1289	cd->flags |= SCDTOC;
1290
1291	return 0;
1292}
1293
1294static inline void
1295write_control(unsigned port, unsigned data)
1296{
1297	outb(port + OREG_CONTROL, data);
1298}
1299
1300static void
1301init_drive(unsigned unit)
1302{
1303	int rc;
1304
1305	rc = send_cmd(unit, CMD_SET_DRIVE_PARAM, 2,
1306		0x05, 0x03 | ((scd_data[unit].double_speed) ? 0x04: 0));
1307	if (rc != 0)
1308		printf("scd%d: Unable to set parameters. Errcode = 0x%x\n", unit, rc);
1309}
1310
1311/* Returns 0 or errno */
1312static int
1313get_result(u_int unit, int result_len, u_char *result)
1314{
1315	unsigned int port = scd_data[unit].iobase;
1316	unsigned int res_reg = port + IREG_RESULT;
1317	unsigned char c;
1318	int loop_index = 2; /* send_cmd() reads two bytes ... */
1319
1320	XDEBUG(1, ("scd%d: DEBUG: get_result: bytes=%d\n", unit, result_len));
1321
1322	while (result_len-- > 0) {
1323		if (loop_index++ >= 10) {
1324			loop_index = 1;
1325			if (waitfor_status_bits(unit, SBIT_RESULT_READY, 0))
1326				return EIO;
1327			write_control(port, CBIT_RESULT_READY_CLEAR);
1328		}
1329		if (result)
1330			*result++ = inb(res_reg);
1331		else
1332			(void)inb(res_reg);
1333	}
1334	return 0;
1335}
1336
1337/* Returns -0x100 for timeout, -(drive error code) OR number of result bytes */
1338static int
1339send_cmd(u_int unit, u_char cmd, u_int nargs, ...)
1340{
1341	va_list ap;
1342	u_int port = scd_data[unit].iobase;
1343	u_int reg;
1344	u_char c;
1345	int rc;
1346	int i;
1347
1348	if (waitfor_status_bits(unit, 0, SBIT_BUSY)) {
1349		printf("scd%d: drive busy\n", unit);
1350		return -0x100;
1351	}
1352
1353	XDEBUG(1,("scd%d: DEBUG: send_cmd: cmd=0x%x nargs=%d", unit, cmd, nargs));
1354
1355	write_control(port, CBIT_RESULT_READY_CLEAR);
1356	write_control(port, CBIT_RPARAM_CLEAR);
1357
1358	for (i = 0; i < 100; i++)
1359		if (FSTATUS_BIT(port, FBIT_WPARAM_READY))
1360			break;
1361	if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) {
1362		XDEBUG(1, ("\nscd%d: wparam timeout\n", unit));
1363		return -EIO;
1364	}
1365
1366	va_start(ap, nargs);
1367	reg = port + OREG_WPARAMS;
1368	for (i = 0; i < nargs; i++) {
1369		c = (u_char)va_arg(ap, int);
1370		outb(reg, c);
1371		XDEBUG(1, (",{0x%x}", c));
1372	}
1373	va_end(ap);
1374	XDEBUG(1, ("\n"));
1375
1376	outb(port+OREG_COMMAND, cmd);
1377
1378	if (rc = waitfor_status_bits(unit, SBIT_RESULT_READY, SBIT_BUSY))
1379		return -0x100;
1380
1381	reg = port + IREG_RESULT;
1382	write_control(port, CBIT_RESULT_READY_CLEAR);
1383	switch ((rc = inb(reg)) & 0xf0) {
1384	case 0x20:
1385		rc = inb(reg);
1386		/* FALL TROUGH */
1387	case 0x50:
1388		XDEBUG(1, ("scd%d: DEBUG: send_cmd: drive_error=0x%x\n", unit, rc));
1389		return -rc;
1390	case 0x00:
1391	default:
1392		rc = inb(reg);
1393		XDEBUG(1, ("scd%d: DEBUG: send_cmd: result_len=%d\n", unit, rc));
1394		return rc;
1395	}
1396}
1397
1398static void
1399print_error(int unit, int errcode)
1400{
1401	switch (errcode) {
1402	case -ERR_CD_NOT_LOADED:
1403		printf("scd%d: door is open\n", unit);
1404		break;
1405	case -ERR_NO_CD_INSIDE:
1406		printf("scd%d: no cd inside\n", unit);
1407		break;
1408	default:
1409		if (errcode == -0x100 || errcode > 0)
1410			printf("scd%d: device timeout\n", unit);
1411		else
1412			printf("scd%d: unexpected error 0x%x\n", unit, -errcode);
1413		break;
1414	}
1415}
1416
1417/* Returns 0 or errno value */
1418static int
1419waitfor_status_bits(int unit, int bits_set, int bits_clear)
1420{
1421	u_int port = scd_data[unit].iobase;
1422	u_int flags = scd_data[unit].flags;
1423	u_int reg = port + IREG_STATUS;
1424	u_int max_loop;
1425	u_char c = 0;
1426
1427	if (flags & SCDPROBING) {
1428		max_loop = 0;
1429		while (max_loop++ < 1000) {
1430			c = inb(reg);
1431			if (c == 0xff)
1432				return EIO;
1433			if (c & SBIT_ATTENTION) {
1434				process_attention(unit);
1435				continue;
1436			}
1437			if ((c & bits_set) == bits_set &&
1438			    (c & bits_clear) == 0)
1439			{
1440				break;
1441			}
1442			DELAY(10000);
1443		}
1444	} else {
1445		max_loop = 100;
1446		while (max_loop-- > 0) {
1447			c = inb(reg);
1448			if (c & SBIT_ATTENTION) {
1449				process_attention(unit);
1450				continue;
1451			}
1452			if ((c & bits_set) == bits_set &&
1453			    (c & bits_clear) == 0)
1454			{
1455				break;
1456			}
1457			tsleep(waitfor_status_bits, PZERO - 1, "waitfor", hz/10);
1458		}
1459	}
1460	if ((c & bits_set) == bits_set &&
1461	    (c & bits_clear) == 0)
1462	{
1463		return 0;
1464	}
1465#ifdef SCD_DEBUG
1466	if (scd_debuglevel > 0)
1467		printf("scd%d: DEBUG: waitfor: TIMEOUT (0x%x,(0x%x,0x%x))\n", unit, c, bits_set, bits_clear);
1468	else
1469#endif
1470		printf("scd%d: timeout.\n", unit);
1471	return EIO;
1472}
1473
1474/* these two routines for xcdplayer - "borrowed" from mcd.c */
1475static int
1476scd_toc_header (int unit, struct ioc_toc_header* th)
1477{
1478	struct scd_data *cd = scd_data + unit;
1479	int rc;
1480
1481	if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
1482		print_error(unit, rc);
1483		return EIO;
1484	}
1485
1486	th->starting_track = cd->first_track;
1487	th->ending_track = cd->last_track;
1488	th->len = 0; /* not used */
1489
1490	return 0;
1491}
1492
1493static int
1494scd_toc_entrys (int unit, struct ioc_read_toc_entry *te)
1495{
1496	struct scd_data *cd = scd_data + unit;
1497	struct cd_toc_entry toc_entry;
1498	int rc, i, len = te->data_len;
1499
1500	if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
1501		print_error(unit, rc);
1502		return EIO;
1503	}
1504
1505	/* find the toc to copy*/
1506	i = te->starting_track;
1507	if (i == SCD_LASTPLUS1)
1508		i = cd->last_track + 1;
1509
1510	/* verify starting track */
1511	if (i < cd->first_track || i > cd->last_track+1)
1512		return EINVAL;
1513
1514	/* valid length ? */
1515	if (len < sizeof(struct cd_toc_entry)
1516	    || (len % sizeof(struct cd_toc_entry)) != 0)
1517		return EINVAL;
1518
1519	/* copy the toc data */
1520	toc_entry.control = cd->toc[i].ctl;
1521	toc_entry.addr_type = te->address_format;
1522	toc_entry.track = i;
1523	if (te->address_format == CD_MSF_FORMAT) {
1524		toc_entry.addr.msf.unused = 0;
1525		toc_entry.addr.msf.minute = bcd2bin(cd->toc[i].start_msf[0]);
1526		toc_entry.addr.msf.second = bcd2bin(cd->toc[i].start_msf[1]);
1527		toc_entry.addr.msf.frame = bcd2bin(cd->toc[i].start_msf[2]);
1528	}
1529
1530	/* copy the data back */
1531	if (copyout(&toc_entry, te->data, sizeof(struct cd_toc_entry)) != 0)
1532		return EFAULT;
1533
1534	return 0;
1535}
1536
1537#endif /* NSCD > 0 */
1538