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