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