scd.c revision 25460
17332Sjkh/*-
27332Sjkh * Copyright (c) 1995 Mikael Hybsch
37332Sjkh * All rights reserved.
47332Sjkh *
57332Sjkh * Portions of this file are copied from mcd.c
67332Sjkh * which has the following copyrights:
77332Sjkh *
87332Sjkh *	Copyright 1993 by Holger Veit (data part)
97332Sjkh *	Copyright 1993 by Brian Moore (audio part)
107332Sjkh *	Changes Copyright 1993 by Gary Clark II
117332Sjkh *	Changes Copyright (C) 1994 by Andrew A. Chernov
127332Sjkh *
137332Sjkh *	Rewrote probe routine to work on newer Mitsumi drives.
147332Sjkh *	Additional changes (C) 1994 by Jordan K. Hubbard
157332Sjkh *
167332Sjkh *	All rights reserved.
177332Sjkh *
187332Sjkh * Redistribution and use in source and binary forms, with or without
197332Sjkh * modification, are permitted provided that the following conditions
207332Sjkh * are met:
217332Sjkh * 1. Redistributions of source code must retain the above copyright
227332Sjkh *    notice, this list of conditions and the following disclaimer
237332Sjkh *    in this position and unchanged.
247332Sjkh * 2. Redistributions in binary form must reproduce the above copyright
257332Sjkh *    notice, this list of conditions and the following disclaimer in the
267332Sjkh *    documentation and/or other materials provided with the distribution.
277332Sjkh * 3. The name of the author may not be used to endorse or promote products
287332Sjkh *    derived from this software withough specific prior written permission
297332Sjkh *
307332Sjkh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
317332Sjkh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
327332Sjkh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
337332Sjkh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
347332Sjkh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
357332Sjkh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
367332Sjkh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
377332Sjkh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
387332Sjkh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
397332Sjkh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
407332Sjkh *
417332Sjkh */
427332Sjkh
437332Sjkh
4425460Sjoerg/* $Id: scd.c,v 1.29 1997/04/20 17:26:55 bde Exp $ */
457332Sjkh
467332Sjkh/* Please send any comments to micke@dynas.se */
477332Sjkh
487332Sjkh#define	SCD_DEBUG	0
497332Sjkh
507332Sjkh#include "scd.h"
517332Sjkh#if NSCD > 0
527332Sjkh#include <sys/types.h>
537332Sjkh#include <sys/param.h>
547332Sjkh#include <sys/systm.h>
557332Sjkh#include <sys/conf.h>
567332Sjkh#include <sys/file.h>
577332Sjkh#include <sys/buf.h>
587369Sbde#include <sys/proc.h>
597332Sjkh#include <sys/stat.h>
607332Sjkh#include <sys/uio.h>
617332Sjkh#include <sys/cdio.h>
627332Sjkh#include <sys/errno.h>
637332Sjkh#include <sys/dkbad.h>
647332Sjkh#include <sys/disklabel.h>
6512675Sjulian#include <sys/kernel.h>
6612675Sjulian#ifdef DEVFS
6712675Sjulian#include <sys/devfsext.h>
6812675Sjulian#endif /*DEVFS*/
697369Sbde
707369Sbde#include <machine/clock.h>
717332Sjkh#include <machine/stdarg.h>
727332Sjkh
737332Sjkh#include <i386/isa/isa_device.h>
747369Sbde#include <i386/isa/scdreg.h>
757332Sjkh
7612502Sjulian
777332Sjkh#define scd_part(dev)	((minor(dev)) & 7)
787332Sjkh#define scd_unit(dev)	(((minor(dev)) & 0x38) >> 3)
797332Sjkh#define scd_phys(dev)	(((minor(dev)) & 0x40) >> 6)
807332Sjkh#define RAW_PART        2
817332Sjkh
827332Sjkh/* flags */
837332Sjkh#define SCDOPEN		0x0001	/* device opened */
847332Sjkh#define SCDVALID	0x0002	/* parameters loaded */
857332Sjkh#define SCDINIT		0x0004	/* device is init'd */
867332Sjkh#define	SCDPROBING	0x0020	/* probing */
877332Sjkh#define	SCDTOC		0x0100	/* already read toc */
887332Sjkh#define	SCDMBXBSY	0x0200	/* local mbx is busy */
897332Sjkh#define	SCDSPINNING	0x0400  /* drive is spun up */
907332Sjkh
917332Sjkh#define SCD_S_BEGIN	0
927332Sjkh#define SCD_S_BEGIN1	1
937332Sjkh#define SCD_S_WAITSTAT	2
947332Sjkh#define	SCD_S_WAITFIFO	3
957332Sjkh#define SCD_S_WAITSPIN	4
967332Sjkh#define SCD_S_WAITREAD	5
977332Sjkh#define	SCD_S_WAITPARAM 6
987332Sjkh
997332Sjkh#define RDELAY_WAIT	300
1007332Sjkh#define RDELAY_WAITREAD	300
1017332Sjkh
1027332Sjkh#define	SCDBLKSIZE	2048
1037332Sjkh
1047332Sjkh#ifdef SCD_DEBUG
10511872Sphk   static int scd_debuglevel = SCD_DEBUG;
1067332Sjkh#  define XDEBUG(level, data) {if (scd_debuglevel >= level) printf data;}
1077332Sjkh#else
1087332Sjkh#  define XDEBUG(level, data)
1097332Sjkh#endif
1107332Sjkh
1117332Sjkhstruct scd_mbx {
1127332Sjkh	short		unit;
1137332Sjkh	short		port;
1147332Sjkh	short		retry;
1157332Sjkh	short		nblk;
1167332Sjkh	int		sz;
1177332Sjkh	u_long		skip;
1187332Sjkh	struct buf	*bp;
1197332Sjkh	int		p_offset;
1207332Sjkh	short		count;
1217332Sjkh};
1227332Sjkh
12311872Sphkstatic struct scd_data {
1247332Sjkh	int	iobase;
1257332Sjkh	char	double_speed;
1267332Sjkh	char	*name;
1277332Sjkh	short	flags;
1287332Sjkh	int	blksize;
1297332Sjkh	u_long	disksize;
1307332Sjkh	struct disklabel dlabel;
1317332Sjkh	int	openflag;
1327332Sjkh	struct {
1337332Sjkh		unsigned char adr :4;
1347332Sjkh		unsigned char ctl :4; /* xcdplayer needs this */
1357332Sjkh		unsigned char start_msf[3];
1367332Sjkh	} toc[MAX_TRACKS];
1377332Sjkh	short	first_track;
1387332Sjkh	short	last_track;
1397332Sjkh	struct	ioc_play_msf last_play;
1408876Srgrimes
1417332Sjkh	short	audio_status;
14215574Sphk	struct buf_queue_head head;		/* head of buf queue */
1437332Sjkh	struct scd_mbx mbx;
14412675Sjulian#ifdef	DEVFS
14512675Sjulian	void	*ra_devfs_token;
14612675Sjulian	void	*rc_devfs_token;
14712675Sjulian	void	*a_devfs_token;
14812675Sjulian	void	*c_devfs_token;
14912675Sjulian#endif
1507332Sjkh} scd_data[NSCD];
1517332Sjkh
1527332Sjkh/* prototypes */
1537332Sjkhstatic	void	hsg2msf(int hsg, bcd_t *msf);
1547332Sjkhstatic	int	msf2hsg(bcd_t *msf);
1557332Sjkh
1567332Sjkhstatic void process_attention(unsigned unit);
1577332Sjkhstatic inline void write_control(unsigned port, unsigned data);
1587332Sjkhstatic int waitfor_status_bits(int unit, int bits_set, int bits_clear);
1597332Sjkhstatic int send_cmd(u_int unit, u_char cmd, u_int nargs, ...);
1607332Sjkhstatic void init_drive(unsigned unit);
1617332Sjkhstatic int spin_up(unsigned unit);
1627332Sjkhstatic int read_toc(dev_t dev);
1637332Sjkhstatic int get_result(u_int unit, int result_len, u_char *result);
1647332Sjkhstatic void print_error(int unit, int errcode);
1657332Sjkh
1667332Sjkhstatic void scd_start(int unit);
16725056Sbdestatic timeout_t scd_timeout;
1687332Sjkhstatic void scd_doread(int state, struct scd_mbx *mbxin);
1697332Sjkh
1707332Sjkhstatic int scd_eject(int unit);
1717332Sjkhstatic int scd_stop(int unit);
1727332Sjkhstatic int scd_pause(int unit);
1737332Sjkhstatic int scd_resume(int unit);
1747332Sjkhstatic int scd_playtracks(int unit, struct ioc_play_track *pt);
1757332Sjkhstatic int scd_playmsf(int unit, struct ioc_play_msf *msf);
1767332Sjkhstatic int scd_play(int unit, struct ioc_play_msf *msf);
1777332Sjkhstatic int scd_subchan(int unit, struct ioc_read_subchannel *sc);
1787332Sjkhstatic int read_subcode(int unit, struct sony_subchannel_position_data *sc);
1797332Sjkh
1807332Sjkh/* for xcdplayer */
1817332Sjkhstatic int scd_toc_header(int unit, struct ioc_toc_header *th);
1827332Sjkhstatic int scd_toc_entrys(int unit, struct ioc_read_toc_entry *te);
18325460Sjoergstatic int scd_toc_entry(int unit, struct ioc_read_toc_single_entry *te);
1847332Sjkh#define SCD_LASTPLUS1 170 /* don't ask, xcdplayer passes this in */
1857332Sjkh
18611872Sphkstatic int	scd_probe(struct isa_device *dev);
18711872Sphkstatic int	scd_attach(struct isa_device *dev);
1887332Sjkhstruct	isa_driver	scddriver = { scd_probe, scd_attach, "scd" };
1897332Sjkh
19012675Sjulianstatic	d_open_t	scdopen;
19112675Sjulianstatic	d_close_t	scdclose;
19212675Sjulianstatic	d_ioctl_t	scdioctl;
19312675Sjulianstatic	d_strategy_t	scdstrategy;
19412675Sjulian
19512675Sjulian#define CDEV_MAJOR 45
19612675Sjulian#define BDEV_MAJOR 16
19717264Sphkstatic struct cdevsw scd_cdevsw;
19812678Sphkstatic struct bdevsw scd_bdevsw =
19912675Sjulian	{ scdopen,	scdclose,	scdstrategy,	scdioctl,	/*16*/
20012740Sbde	  nodump,	nopsize,	0, "scd",	&scd_cdevsw,	-1 };
20112675Sjulian
2027332Sjkhint scd_attach(struct isa_device *dev)
2037332Sjkh{
20412675Sjulian	int	unit = dev->id_unit;
20512675Sjulian	struct scd_data *cd = scd_data + unit;
2068876Srgrimes
2077332Sjkh	cd->iobase = dev->id_iobase;	/* Already set by probe, but ... */
2087332Sjkh
2097332Sjkh	/* name filled in probe */
2107332Sjkh	printf("scd%d: <%s>\n", dev->id_unit, scd_data[dev->id_unit].name);
2117332Sjkh
2127332Sjkh	init_drive(dev->id_unit);
2137332Sjkh
2147332Sjkh	cd->flags = SCDINIT;
2157332Sjkh	cd->audio_status = CD_AS_AUDIO_INVALID;
21615577Sphk	TAILQ_INIT(&cd->head);
2177332Sjkh
21812675Sjulian#ifdef DEVFS
21914873Sscrappy	cd->ra_devfs_token =
22016210Sbde		devfs_add_devswf(&scd_cdevsw, dkmakeminor(unit, 0, 0),
22116210Sbde				 DV_CHR, UID_ROOT, GID_OPERATOR, 0640,
22216210Sbde				 "rscd%da", unit);
22314873Sscrappy	cd->rc_devfs_token =
22416210Sbde		devfs_add_devswf(&scd_cdevsw, dkmakeminor(unit, 0, RAW_PART),
22516210Sbde				 DV_CHR, UID_ROOT, GID_OPERATOR, 0640,
22616210Sbde				 "rscd%dc", unit);
22714873Sscrappy	cd->a_devfs_token =
22816210Sbde		devfs_add_devswf(&scd_bdevsw, dkmakeminor(unit, 0, 0),
22916210Sbde				 DV_BLK, UID_ROOT, GID_OPERATOR, 0640,
23016210Sbde				 "scd%da", unit);
23114873Sscrappy	cd->c_devfs_token =
23216210Sbde		devfs_add_devswf(&scd_bdevsw, dkmakeminor(unit, 0, RAW_PART),
23316210Sbde				 DV_BLK, UID_ROOT, GID_OPERATOR, 0640,
23416210Sbde				 "scd%dc", unit);
23512675Sjulian#endif
2367332Sjkh	return 1;
2377332Sjkh}
2387332Sjkh
23912675Sjulianstatic	int
24010624Sbdescdopen(dev_t dev, int flags, int fmt, struct proc *p)
2417332Sjkh{
2427332Sjkh	int unit,part,phys;
2437332Sjkh	int rc;
2447332Sjkh	struct scd_data *cd;
2458876Srgrimes
2467332Sjkh	unit = scd_unit(dev);
2477332Sjkh	if (unit >= NSCD)
2487332Sjkh		return ENXIO;
2497332Sjkh
2507332Sjkh	cd = scd_data + unit;
2517332Sjkh	part = scd_part(dev);
2527332Sjkh	phys = scd_phys(dev);
2538876Srgrimes
2547332Sjkh	/* not initialized*/
2557332Sjkh	if (!(cd->flags & SCDINIT))
2567332Sjkh		return ENXIO;
2577332Sjkh
2587332Sjkh	/* invalidated in the meantime? mark all open part's invalid */
2597332Sjkh	if (cd->openflag)
2607332Sjkh		return ENXIO;
2617332Sjkh
2627332Sjkh	XDEBUG(1,("scd%d: DEBUG: status = 0x%x\n", unit, inb(cd->iobase+IREG_STATUS)));
2637332Sjkh
2647332Sjkh	if ((rc = spin_up(unit)) != 0) {
2657332Sjkh		print_error(unit, rc);
2667332Sjkh		return EIO;
2677332Sjkh	}
2687332Sjkh	if (!(cd->flags & SCDTOC)) {
2697332Sjkh		int loop_count = 3;
2707332Sjkh
2717332Sjkh		while (loop_count-- > 0 && (rc = read_toc(dev)) != 0) {
2727332Sjkh			if (rc == ERR_NOT_SPINNING) {
2737332Sjkh				rc = spin_up(unit);
2747332Sjkh				if (rc) {
2757332Sjkh					print_error(unit, rc);\
2767332Sjkh					return EIO;
2777332Sjkh				}
2787332Sjkh				continue;
2797332Sjkh			}
2807332Sjkh			printf("scd%d: TOC read error 0x%x\n", unit, rc);
2817332Sjkh			return EIO;
2827332Sjkh		}
2837332Sjkh	}
2847332Sjkh
2857332Sjkh	cd->openflag = 1;
2867332Sjkh	cd->flags |= SCDVALID;
2877332Sjkh
2887332Sjkh	return 0;
2897332Sjkh}
2907332Sjkh
29112675Sjulianstatic	int
29210624Sbdescdclose(dev_t dev, int flags, int fmt, struct proc *p)
2937332Sjkh{
2947332Sjkh	int unit,part,phys;
2957332Sjkh	struct scd_data *cd;
2968876Srgrimes
2977332Sjkh	unit = scd_unit(dev);
2987332Sjkh	if (unit >= NSCD)
2997332Sjkh		return ENXIO;
3007332Sjkh
3017332Sjkh	cd = scd_data + unit;
3027332Sjkh	part = scd_part(dev);
3037332Sjkh	phys = scd_phys(dev);
3048876Srgrimes
3058876Srgrimes	if (!(cd->flags & SCDINIT) || !cd->openflag)
3067332Sjkh		return ENXIO;
3077332Sjkh
3087332Sjkh	if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS) {
3097332Sjkh		(void)send_cmd(unit, CMD_SPIN_DOWN, 0);
3107332Sjkh		cd->flags &= ~SCDSPINNING;
3117332Sjkh	}
3127332Sjkh
3137332Sjkh
3147332Sjkh	/* close channel */
3157332Sjkh	cd->openflag = 0;
3167332Sjkh
3177332Sjkh	return 0;
3187332Sjkh}
3197332Sjkh
32012675Sjulianstatic	void
3217332Sjkhscdstrategy(struct buf *bp)
3227332Sjkh{
3237332Sjkh	struct scd_data *cd;
3247332Sjkh	int s;
3257332Sjkh	int unit = scd_unit(bp->b_dev);
3267332Sjkh
3277332Sjkh	cd = scd_data + unit;
3287332Sjkh
3297332Sjkh	XDEBUG(2, ("scd%d: DEBUG: strategy: block=%ld, bcount=%ld\n", unit, bp->b_blkno, bp->b_bcount));
3307332Sjkh
3317332Sjkh	if (unit >= NSCD || bp->b_blkno < 0 || (bp->b_bcount % SCDBLKSIZE)) {
3328376Srgrimes		printf("scd%d: strategy failure: blkno = %ld, bcount = %ld\n",
3337332Sjkh			unit, bp->b_blkno, bp->b_bcount);
3347332Sjkh		bp->b_error = EINVAL;
3357332Sjkh		bp->b_flags |= B_ERROR;
3367332Sjkh		goto bad;
3377332Sjkh	}
3387332Sjkh
3397332Sjkh	/* if device invalidated (e.g. media change, door open), error */
3407332Sjkh	if (!(cd->flags & SCDVALID)) {
3417332Sjkh		printf("scd%d: media changed\n", unit);
3427332Sjkh		bp->b_error = EIO;
3437332Sjkh		goto bad;
3447332Sjkh	}
3457332Sjkh
3467332Sjkh	/* read only */
3477332Sjkh	if (!(bp->b_flags & B_READ)) {
3487332Sjkh		bp->b_error = EROFS;
3497332Sjkh		goto bad;
3507332Sjkh	}
3518876Srgrimes
3527332Sjkh	/* no data to read */
3537332Sjkh	if (bp->b_bcount == 0)
3547332Sjkh		goto done;
3558876Srgrimes
3567332Sjkh	if (!(cd->flags & SCDTOC)) {
3577332Sjkh		bp->b_error = EIO;
3587332Sjkh		goto bad;
3597332Sjkh	}
3607332Sjkh	/* adjust transfer if necessary */
3617332Sjkh	if (bounds_check_with_label(bp,&cd->dlabel,1) <= 0)
3627332Sjkh		goto done;
3637332Sjkh
3647332Sjkh	bp->b_pblkno = bp->b_blkno;
3657332Sjkh	bp->b_resid = 0;
3668876Srgrimes
3677332Sjkh	/* queue it */
3687332Sjkh	s = splbio();
36915574Sphk	tqdisksort(&cd->head, bp);
3707332Sjkh	splx(s);
3718876Srgrimes
3727332Sjkh	/* now check whether we can perform processing */
3737332Sjkh	scd_start(unit);
3747332Sjkh	return;
3757332Sjkh
3767332Sjkhbad:
3777332Sjkh	bp->b_flags |= B_ERROR;
3787332Sjkhdone:
3797332Sjkh	bp->b_resid = bp->b_bcount;
3807332Sjkh	biodone(bp);
3817332Sjkh	return;
3827332Sjkh}
3837332Sjkh
3847332Sjkhstatic void
3857332Sjkhscd_start(int unit)
3867332Sjkh{
3877332Sjkh	struct scd_data *cd = scd_data + unit;
38815574Sphk	struct buf *bp;
3897332Sjkh	struct partition *p;
3907332Sjkh	register s = splbio();
3918876Srgrimes
3927332Sjkh	if (cd->flags & SCDMBXBSY) {
3937332Sjkh		splx(s);
3947332Sjkh		return;
3957332Sjkh	}
3967332Sjkh
39715574Sphk	bp = TAILQ_FIRST(&cd->head);
39815574Sphk	if (bp != 0) {
3997332Sjkh		/* block found to process, dequeue */
40015574Sphk		TAILQ_REMOVE(&cd->head, bp, b_act);
4017332Sjkh		cd->flags |= SCDMBXBSY;
4027332Sjkh		splx(s);
4037332Sjkh	} else {
4047332Sjkh		/* nothing to do */
4057332Sjkh		splx(s);
4067332Sjkh		return;
4077332Sjkh	}
4087332Sjkh
4097332Sjkh	p = cd->dlabel.d_partitions + scd_part(bp->b_dev);
4107332Sjkh
4117332Sjkh	cd->mbx.unit = unit;
4127332Sjkh	cd->mbx.port = cd->iobase;
4137332Sjkh	cd->mbx.retry = 3;
4147332Sjkh	cd->mbx.bp = bp;
4157332Sjkh	cd->mbx.p_offset = p->p_offset;
4167332Sjkh	splx(s);
4177332Sjkh
4187332Sjkh	scd_doread(SCD_S_BEGIN,&(cd->mbx));
4197332Sjkh	return;
4207332Sjkh}
4217332Sjkh
42212675Sjulianstatic	int
42310624Sbdescdioctl(dev_t dev, int cmd, caddr_t addr, int flags, struct proc *p)
4247332Sjkh{
4257332Sjkh	struct scd_data *cd;
4267332Sjkh	int unit,part;
4278876Srgrimes
4287332Sjkh	unit = scd_unit(dev);
4297332Sjkh	part = scd_part(dev);
4307332Sjkh	cd = scd_data + unit;
4317332Sjkh
4328376Srgrimes	XDEBUG(1, ("scd%d: ioctl: cmd=0x%x\n", unit, cmd));
4337332Sjkh
4347332Sjkh	if (!(cd->flags & SCDVALID))
4357332Sjkh		return EIO;
4367332Sjkh
4377332Sjkh	switch (cmd) {
4387332Sjkh	case DIOCSBAD:
4397332Sjkh		return EINVAL;
4407332Sjkh	case DIOCGDINFO:
4417332Sjkh		*(struct disklabel *)addr = cd->dlabel;
4427332Sjkh		return 0;
4437332Sjkh	case DIOCGPART:
4447332Sjkh		((struct partinfo *)addr)->disklab = &cd->dlabel;
4457332Sjkh		((struct partinfo *)addr)->part =
4467332Sjkh			&cd->dlabel.d_partitions[0];
4477332Sjkh		return 0;
4487332Sjkh	case CDIOCPLAYTRACKS:
4497332Sjkh		return scd_playtracks(unit, (struct ioc_play_track *) addr);
4507332Sjkh	case CDIOCPLAYBLOCKS:
4517332Sjkh		return EINVAL;
4527332Sjkh	case CDIOCPLAYMSF:
4537332Sjkh		return scd_playmsf(unit, (struct ioc_play_msf *) addr);
4547332Sjkh	case CDIOCREADSUBCHANNEL:
4557332Sjkh		return scd_subchan(unit, (struct ioc_read_subchannel *) addr);
4567332Sjkh	case CDIOREADTOCHEADER:
4577332Sjkh		return scd_toc_header (unit, (struct ioc_toc_header *) addr);
4587332Sjkh	case CDIOREADTOCENTRYS:
4597332Sjkh		return scd_toc_entrys (unit, (struct ioc_read_toc_entry*) addr);
46025460Sjoerg	case CDIOREADTOCENTRY:
46125460Sjoerg		return scd_toc_entry (unit, (struct ioc_read_toc_single_entry*) addr);
4627332Sjkh	case CDIOCSETPATCH:
4637332Sjkh	case CDIOCGETVOL:
4647332Sjkh	case CDIOCSETVOL:
4657332Sjkh	case CDIOCSETMONO:
4667332Sjkh	case CDIOCSETSTERIO:
4677332Sjkh	case CDIOCSETMUTE:
4687332Sjkh	case CDIOCSETLEFT:
4697332Sjkh	case CDIOCSETRIGHT:
4707332Sjkh		return EINVAL;
4717332Sjkh	case CDIOCRESUME:
4727332Sjkh		return scd_resume(unit);
4737332Sjkh	case CDIOCPAUSE:
4747332Sjkh		return scd_pause(unit);
4757332Sjkh	case CDIOCSTART:
4767332Sjkh		return EINVAL;
4777332Sjkh	case CDIOCSTOP:
4787332Sjkh		return scd_stop(unit);
4797332Sjkh	case CDIOCEJECT:
4807332Sjkh		return scd_eject(unit);
4817332Sjkh	case CDIOCALLOW:
4827332Sjkh		return 0;
4837332Sjkh	case CDIOCSETDEBUG:
4847332Sjkh#ifdef SCD_DEBUG
4857332Sjkh		scd_debuglevel++;
4867332Sjkh#endif
4877332Sjkh		return 0;
4887332Sjkh	case CDIOCCLRDEBUG:
4897332Sjkh#ifdef SCD_DEBUG
4907332Sjkh		scd_debuglevel = 0;
4917332Sjkh
4927332Sjkh#endif
4937332Sjkh		return 0;
4947332Sjkh	default:
4958376Srgrimes		printf("scd%d: unsupported ioctl (cmd=0x%x)\n", unit, cmd);
4967332Sjkh		return ENOTTY;
4977332Sjkh	}
4987332Sjkh}
4997332Sjkh
5007332Sjkh/***************************************************************
5017332Sjkh * lower level of driver starts here
5027332Sjkh **************************************************************/
5037332Sjkh
5047332Sjkhstatic int
5057332Sjkhscd_playtracks(int unit, struct ioc_play_track *pt)
5067332Sjkh{
5077332Sjkh	struct scd_data *cd = scd_data + unit;
5087332Sjkh	struct ioc_play_msf msf;
5097332Sjkh	int a = pt->start_track;
5107332Sjkh	int z = pt->end_track;
51111872Sphk	int rc;
5127332Sjkh
5137332Sjkh	if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
5147332Sjkh		if (rc == -ERR_NOT_SPINNING) {
5157332Sjkh			if (spin_up(unit) != 0)
5167332Sjkh				return EIO;
5177332Sjkh			rc = read_toc(unit);
5187332Sjkh		}
5197332Sjkh		if (rc != 0) {
5207332Sjkh			print_error(unit, rc);
5217332Sjkh			return EIO;
5227332Sjkh		}
5237332Sjkh	}
5247332Sjkh
5257332Sjkh	XDEBUG(1, ("scd%d: playtracks from %d:%d to %d:%d\n", unit,
5267332Sjkh		a, pt->start_index, z, pt->end_index));
5277332Sjkh
5287332Sjkh	if (   a < cd->first_track
5297332Sjkh	    || a > cd->last_track
5307332Sjkh	    || a > z
5317332Sjkh	    || z > cd->last_track)
5327332Sjkh		return EINVAL;
5337332Sjkh
5347332Sjkh	bcopy(cd->toc[a].start_msf, &msf.start_m, 3);
5357332Sjkh	hsg2msf(msf2hsg(cd->toc[z+1].start_msf)-1, &msf.end_m);
5367332Sjkh
5377332Sjkh	return scd_play(unit, &msf);
5387332Sjkh}
5397332Sjkh
5407332Sjkh/* The start/end msf is expected to be in bin format */
5417332Sjkhstatic int
5427332Sjkhscd_playmsf(int unit, struct ioc_play_msf *msfin)
5437332Sjkh{
5447332Sjkh	struct ioc_play_msf msf;
5457332Sjkh
5467332Sjkh	msf.start_m = bin2bcd(msfin->start_m);
5477332Sjkh	msf.start_s = bin2bcd(msfin->start_s);
5487332Sjkh	msf.start_f = bin2bcd(msfin->start_f);
5497332Sjkh	msf.end_m = bin2bcd(msfin->end_m);
5507332Sjkh	msf.end_s = bin2bcd(msfin->end_s);
5517332Sjkh	msf.end_f = bin2bcd(msfin->end_f);
5527332Sjkh
5537332Sjkh	return scd_play(unit, &msf);
5547332Sjkh}
5557332Sjkh
5567332Sjkh/* The start/end msf is expected to be in bcd format */
5577332Sjkhstatic int
5587332Sjkhscd_play(int unit, struct ioc_play_msf *msf)
5597332Sjkh{
5607332Sjkh	struct scd_data *cd = scd_data + unit;
5617332Sjkh	int i, rc;
5627332Sjkh
5637332Sjkh	XDEBUG(1, ("scd%d: playing: %02x:%02x:%02x -> %02x:%02x:%02x\n", unit,
5647332Sjkh		msf->start_m, msf->start_s, msf->start_f,
5657332Sjkh		msf->end_m, msf->end_s, msf->end_f));
5667332Sjkh
5677332Sjkh	for (i = 0; i < 2; i++) {
5687332Sjkh		rc = send_cmd(unit, CMD_PLAY_AUDIO, 7,
5697332Sjkh			0x03,
5707332Sjkh			msf->start_m, msf->start_s, msf->start_f,
5717332Sjkh			msf->end_m, msf->end_s, msf->end_f);
5727332Sjkh		if (rc == -ERR_NOT_SPINNING) {
5737332Sjkh			cd->flags &= ~SCDSPINNING;
5747332Sjkh			if (spin_up(unit) != 0)
5757332Sjkh				return EIO;
5767332Sjkh		} else if (rc < 0) {
5777332Sjkh			print_error(unit, rc);
5787332Sjkh			return EIO;
5797332Sjkh		} else {
5807332Sjkh			break;
5817332Sjkh		}
5827332Sjkh	}
5837332Sjkh	cd->audio_status = CD_AS_PLAY_IN_PROGRESS;
5847332Sjkh	bcopy((char *)msf, (char *)&cd->last_play, sizeof(struct ioc_play_msf));
5857332Sjkh	return 0;
5867332Sjkh}
5877332Sjkh
5887332Sjkhstatic int
5897332Sjkhscd_stop(int unit)
5907332Sjkh{
5917332Sjkh	struct scd_data *cd = scd_data + unit;
5927332Sjkh
5937332Sjkh	(void)send_cmd(unit, CMD_STOP_AUDIO, 0);
5947332Sjkh	cd->audio_status = CD_AS_PLAY_COMPLETED;
5957332Sjkh	return 0;
5967332Sjkh}
5977332Sjkh
5987332Sjkhstatic int
5997332Sjkhscd_pause(int unit)
6007332Sjkh{
6017332Sjkh	struct scd_data *cd = scd_data + unit;
6027332Sjkh	struct sony_subchannel_position_data subpos;
6037332Sjkh
6047332Sjkh	if (cd->audio_status != CD_AS_PLAY_IN_PROGRESS)
6057332Sjkh		return EINVAL;
6067332Sjkh
6077332Sjkh	if (read_subcode(unit, &subpos) != 0)
6087332Sjkh		return EIO;
6097332Sjkh
6107332Sjkh	if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0)
6117332Sjkh		return EIO;
6128876Srgrimes
6137332Sjkh	cd->last_play.start_m = subpos.abs_msf[0];
6147332Sjkh	cd->last_play.start_s = subpos.abs_msf[1];
6157332Sjkh	cd->last_play.start_f = subpos.abs_msf[2];
6167332Sjkh	cd->audio_status = CD_AS_PLAY_PAUSED;
6177332Sjkh
6187332Sjkh	XDEBUG(1, ("scd%d: pause @ %02x:%02x:%02x\n", unit,
6197332Sjkh		cd->last_play.start_m,
6207332Sjkh		cd->last_play.start_s,
6217332Sjkh		cd->last_play.start_f));
6227332Sjkh
6237332Sjkh	return 0;
6247332Sjkh}
6257332Sjkh
6267332Sjkhstatic int
6277332Sjkhscd_resume(int unit)
6287332Sjkh{
6297332Sjkh	if (scd_data[unit].audio_status != CD_AS_PLAY_PAUSED)
6307332Sjkh		return EINVAL;
6317332Sjkh	return scd_play(unit, &scd_data[unit].last_play);
6327332Sjkh}
6337332Sjkh
6347332Sjkhstatic int
6357332Sjkhscd_eject(int unit)
6367332Sjkh{
6377332Sjkh	struct scd_data *cd = scd_data + unit;
6387332Sjkh
6397332Sjkh	cd->audio_status = CD_AS_AUDIO_INVALID;
6407332Sjkh	cd->flags &= ~(SCDSPINNING|SCDTOC);
6417332Sjkh
6427332Sjkh	if (send_cmd(unit, CMD_STOP_AUDIO, 0) != 0 ||
6437332Sjkh	    send_cmd(unit, CMD_SPIN_DOWN, 0) != 0 ||
6447332Sjkh	    send_cmd(unit, CMD_EJECT, 0) != 0)
6457332Sjkh	{
6467332Sjkh		return EIO;
6477332Sjkh	}
6487332Sjkh	return 0;
6497332Sjkh}
6507332Sjkh
6517332Sjkhstatic int
6527332Sjkhscd_subchan(int unit, struct ioc_read_subchannel *sc)
6537332Sjkh{
6547332Sjkh	struct scd_data *cd = scd_data + unit;
6557332Sjkh	struct sony_subchannel_position_data q;
6567332Sjkh	struct cd_sub_channel_info data;
6577332Sjkh
6587332Sjkh	XDEBUG(1, ("scd%d: subchan af=%d, df=%d\n", unit,
6597332Sjkh		sc->address_format,
6607332Sjkh		sc->data_format));
6617332Sjkh
6627332Sjkh	if (sc->address_format != CD_MSF_FORMAT)
6637332Sjkh		return EINVAL;
6647332Sjkh
6657332Sjkh	if (sc->data_format != CD_CURRENT_POSITION)
6667332Sjkh		return EINVAL;
6677332Sjkh
6687332Sjkh	if (read_subcode(unit, &q) != 0)
6697332Sjkh		return EIO;
6707332Sjkh
6717332Sjkh	data.header.audio_status = cd->audio_status;
6727332Sjkh	data.what.position.data_format = CD_MSF_FORMAT;
6737332Sjkh	data.what.position.track_number = bcd2bin(q.track_number);
6747332Sjkh	data.what.position.reladdr.msf.unused = 0;
6757332Sjkh	data.what.position.reladdr.msf.minute = bcd2bin(q.rel_msf[0]);
6767332Sjkh	data.what.position.reladdr.msf.second = bcd2bin(q.rel_msf[1]);
6777332Sjkh	data.what.position.reladdr.msf.frame = bcd2bin(q.rel_msf[2]);
6787332Sjkh	data.what.position.absaddr.msf.unused = 0;
6797332Sjkh	data.what.position.absaddr.msf.minute = bcd2bin(q.abs_msf[0]);
6807332Sjkh	data.what.position.absaddr.msf.second = bcd2bin(q.abs_msf[1]);
6817332Sjkh	data.what.position.absaddr.msf.frame = bcd2bin(q.abs_msf[2]);
6827332Sjkh
6837332Sjkh	if (copyout(&data, sc->data, min(sizeof(struct cd_sub_channel_info), sc->data_len))!=0)
6847332Sjkh		return EFAULT;
6857332Sjkh	return 0;
6867332Sjkh}
6877332Sjkh
6887332Sjkhint
6897332Sjkhscd_probe(struct isa_device *dev)
6907332Sjkh{
6917332Sjkh	struct sony_drive_configuration drive_config;
6927332Sjkh	int unit = dev->id_unit;
6937332Sjkh	int rc;
6947332Sjkh	static char namebuf[8+16+8+3];
6957332Sjkh	char *s = namebuf;
6967332Sjkh	int loop_count = 0;
6977332Sjkh
6987332Sjkh	scd_data[unit].flags = SCDPROBING;
6997332Sjkh	scd_data[unit].iobase = dev->id_iobase;
7007332Sjkh
7017332Sjkh	bzero(&drive_config, sizeof(drive_config));
7027332Sjkh
7037332Sjkhagain:
7047332Sjkh	/* Reset drive */
7057332Sjkh	write_control(dev->id_iobase, CBIT_RESET_DRIVE);
7067332Sjkh
7077332Sjkh	/* Calm down */
7087332Sjkh	DELAY(300000);
7097332Sjkh
7107332Sjkh	/* Only the ATTENTION bit may be set */
7117332Sjkh	if ((inb(dev->id_iobase+IREG_STATUS) & ~1) != 0) {
7127332Sjkh		XDEBUG(1, ("scd: too many bits set. probe failed.\n"));
7137332Sjkh		return 0;
7147332Sjkh	}
7157332Sjkh	rc = send_cmd(unit, CMD_GET_DRIVE_CONFIG, 0);
7167332Sjkh	if (rc != sizeof(drive_config)) {
7177332Sjkh		/* Sometimes if the drive is playing audio I get */
7187332Sjkh		/* the bad result 82. Fix by repeating the reset */
7197332Sjkh		if (rc > 0 && loop_count++ == 0)
7207332Sjkh			goto again;
7217332Sjkh		return 0;
7227332Sjkh	}
7237332Sjkh	if (get_result(unit, rc, (u_char *)&drive_config) != 0)
7247332Sjkh		return 0;
7257332Sjkh
7267332Sjkh	bcopy(drive_config.vendor, namebuf, 8);
7277332Sjkh	s = namebuf+8;
7287332Sjkh	while (*(s-1) == ' ')	/* Strip trailing spaces */
7297332Sjkh		s--;
7307332Sjkh	*s++ = ' ';
7317332Sjkh	bcopy(drive_config.product, s, 16);
7327332Sjkh	s += 16;
7337332Sjkh	while (*(s-1) == ' ')
7347332Sjkh		s--;
7357332Sjkh	*s++ = ' ';
7367332Sjkh	bcopy(drive_config.revision, s, 8);
7377332Sjkh	s += 8;
7387332Sjkh	while (*(s-1) == ' ')
7397332Sjkh		s--;
7407332Sjkh	*s = 0;
7417332Sjkh
7427332Sjkh	scd_data[unit].name = namebuf;
7437332Sjkh
7447332Sjkh	if (drive_config.config & 0x10)
7457332Sjkh		scd_data[unit].double_speed = 1;
7467332Sjkh	else
7477332Sjkh		scd_data[unit].double_speed = 0;
7487332Sjkh
7497332Sjkh	return 4;
7507332Sjkh}
7517332Sjkh
7527332Sjkhstatic int
7537332Sjkhread_subcode(int unit, struct sony_subchannel_position_data *sc)
7547332Sjkh{
7557332Sjkh	int rc;
7567332Sjkh
7577332Sjkh	rc = send_cmd(unit, CMD_GET_SUBCHANNEL_DATA, 0);
7587332Sjkh	if (rc < 0 || rc < sizeof(*sc))
7597332Sjkh		return EIO;
7607332Sjkh	if (get_result(unit, rc, (u_char *)sc) != 0)
7617332Sjkh		return EIO;
7627332Sjkh	return 0;
7637332Sjkh}
7647332Sjkh
7657332Sjkh/* State machine copied from mcd.c */
7667332Sjkh
7677332Sjkh/* This (and the code in mcd.c) will not work with more than one drive */
7687332Sjkh/* because there is only one mbxsave below. Should fix that some day. */
7697332Sjkh/* (mbxsave & state should probably be included in the scd_data struct and */
7707332Sjkh/*  the unit number used as first argument to scd_doread().) /Micke */
7717332Sjkh
7727332Sjkh/* state machine to process read requests
7737332Sjkh * initialize with SCD_S_BEGIN: reset state machine
7747332Sjkh * SCD_S_WAITSTAT:  wait for ready (!busy)
7757332Sjkh * SCD_S_WAITSPIN:  wait for drive to spin up (if not spinning)
7767332Sjkh * SCD_S_WAITFIFO:  wait for param fifo to get ready, them exec. command.
7777332Sjkh * SCD_S_WAITREAD:  wait for data ready, read data
7787332Sjkh * SCD_S_WAITPARAM: wait for command result params, read them, error if bad data read.
7797332Sjkh */
7807332Sjkh
7817332Sjkhstatic struct scd_mbx *mbxsave;
7827332Sjkh
7837332Sjkhstatic void
78425056Sbdescd_timeout(void *arg)
78525056Sbde{
78625056Sbde	scd_doread((int)arg, mbxsave);
78725056Sbde}
78825056Sbde
78925056Sbdestatic void
7907332Sjkhscd_doread(int state, struct scd_mbx *mbxin)
7917332Sjkh{
7927332Sjkh	struct scd_mbx *mbx = (state!=SCD_S_BEGIN) ? mbxsave : mbxin;
7937332Sjkh	int	unit = mbx->unit;
7947332Sjkh	int	port = mbx->port;
7957332Sjkh	struct	buf *bp = mbx->bp;
7967332Sjkh	struct	scd_data *cd = scd_data + unit;
79711872Sphk	int	reg,i;
7987332Sjkh	int	blknum;
7997332Sjkh	caddr_t	addr;
8007332Sjkh	static char sdata[3];	/* Must be preserved between calls to this function */
8017332Sjkh
8027332Sjkhloop:
8037332Sjkh	switch (state) {
8047332Sjkh	case SCD_S_BEGIN:
8057332Sjkh		mbx = mbxsave = mbxin;
8067332Sjkh
8077332Sjkh	case SCD_S_BEGIN1:
8087332Sjkh		/* get status */
8097332Sjkh		mbx->count = RDELAY_WAIT;
8107332Sjkh
8117332Sjkh		process_attention(unit);
8127332Sjkh		goto trystat;
8137332Sjkh
8147332Sjkh	case SCD_S_WAITSTAT:
81525056Sbde		untimeout(scd_timeout,(caddr_t)SCD_S_WAITSTAT);
8167332Sjkh		if (mbx->count-- <= 0) {
8177332Sjkh			printf("scd%d: timeout. drive busy.\n",unit);
8187332Sjkh			goto harderr;
8197332Sjkh		}
8207332Sjkh
8217332Sjkhtrystat:
8227332Sjkh		if (IS_BUSY(port)) {
82325056Sbde			timeout(scd_timeout,
8247332Sjkh			    (caddr_t)SCD_S_WAITSTAT,hz/100); /* XXX */
8257332Sjkh			return;
8267332Sjkh		}
8278876Srgrimes
8287332Sjkh		process_attention(unit);
8297332Sjkh
8307332Sjkh		/* reject, if audio active */
8317332Sjkh		if (cd->audio_status & CD_AS_PLAY_IN_PROGRESS) {
8327332Sjkh			printf("scd%d: audio is active\n",unit);
8337332Sjkh			goto harderr;
8347332Sjkh		}
8357332Sjkh
8367332Sjkh		mbx->sz = cd->blksize;
8377332Sjkh
8387332Sjkh		/* for first block */
8397332Sjkh		mbx->nblk = (bp->b_bcount + (mbx->sz-1)) / mbx->sz;
8407332Sjkh		mbx->skip = 0;
8417332Sjkh
8427332Sjkhnextblock:
8437332Sjkh		if (!(cd->flags & SCDVALID))
8447332Sjkh			goto changed;
8457332Sjkh
8467332Sjkh		blknum 	= (bp->b_blkno / (mbx->sz/DEV_BSIZE))
8477332Sjkh			+ mbx->p_offset + mbx->skip/mbx->sz;
8487332Sjkh
8497332Sjkh		XDEBUG(2, ("scd%d: scd_doread: read blknum=%d\n", unit, blknum));
8507332Sjkh
8517332Sjkh		/* build parameter block */
8527332Sjkh		hsg2msf(blknum, sdata);
8537332Sjkh
8547332Sjkh		write_control(port, CBIT_RESULT_READY_CLEAR);
8557332Sjkh		write_control(port, CBIT_RPARAM_CLEAR);
8567332Sjkh		write_control(port, CBIT_DATA_READY_CLEAR);
8577332Sjkh
8587332Sjkh		if (FSTATUS_BIT(port, FBIT_WPARAM_READY))
8597332Sjkh			goto writeparam;
8607332Sjkh
8617332Sjkh		mbx->count = 100;
86225056Sbde		timeout(scd_timeout,
8637332Sjkh			(caddr_t)SCD_S_WAITFIFO,hz/100); /* XXX */
8647332Sjkh		return;
8657332Sjkh
8667332Sjkh	case SCD_S_WAITSPIN:
86725056Sbde		untimeout(scd_timeout,(caddr_t)SCD_S_WAITSPIN);
8687332Sjkh		if (mbx->count-- <= 0) {
8697332Sjkh			printf("scd%d: timeout waiting for drive to spin up.\n", unit);
8707332Sjkh			goto harderr;
8717332Sjkh		}
8727332Sjkh		if (!STATUS_BIT(port, SBIT_RESULT_READY)) {
87325056Sbde			timeout(scd_timeout,
8747332Sjkh				(caddr_t)SCD_S_WAITSPIN,hz/100); /* XXX */
8757332Sjkh			return;
8767332Sjkh		}
8777332Sjkh		write_control(port, CBIT_RESULT_READY_CLEAR);
8787332Sjkh		switch ((i = inb(port+IREG_RESULT)) & 0xf0) {
8797332Sjkh		case 0x20:
8807332Sjkh			i = inb(port+IREG_RESULT);
8817332Sjkh			print_error(unit, i);
8827332Sjkh			goto harderr;
8837332Sjkh		case 0x00:
8847332Sjkh			(void)inb(port+IREG_RESULT);
8857332Sjkh			cd->flags |= SCDSPINNING;
8867332Sjkh			break;
8877332Sjkh		}
8887332Sjkh		XDEBUG(1, ("scd%d: DEBUG: spin up complete\n", unit));
8897332Sjkh
8907332Sjkh		state = SCD_S_BEGIN1;
8917332Sjkh		goto loop;
8927332Sjkh
8937332Sjkh	case SCD_S_WAITFIFO:
89425056Sbde		untimeout(scd_timeout,(caddr_t)SCD_S_WAITFIFO);
8957332Sjkh		if (mbx->count-- <= 0) {
8967332Sjkh			printf("scd%d: timeout. write param not ready.\n",unit);
8977332Sjkh			goto harderr;
8987332Sjkh		}
8997332Sjkh		if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) {
90025056Sbde			timeout(scd_timeout,
9017332Sjkh				(caddr_t)SCD_S_WAITFIFO,hz/100); /* XXX */
9027332Sjkh			return;
9037332Sjkh		}
9047332Sjkh		XDEBUG(1, ("scd%d: mbx->count (writeparamwait) = %d(%d)\n", unit, mbx->count, 100));
9057332Sjkh
9067332Sjkhwriteparam:
9077332Sjkh		/* The reason this test isn't done 'till now is to make sure */
9087332Sjkh		/* that it is ok to send the SPIN_UP cmd below. */
9097332Sjkh		if (!(cd->flags & SCDSPINNING)) {
9107332Sjkh			XDEBUG(1, ("scd%d: spinning up drive ...\n", unit));
9117332Sjkh			outb(port+OREG_COMMAND, CMD_SPIN_UP);
9127332Sjkh			mbx->count = 300;
91325056Sbde			timeout(scd_timeout,
9147332Sjkh				(caddr_t)SCD_S_WAITSPIN,hz/100); /* XXX */
9157332Sjkh			return;
9167332Sjkh		}
9177332Sjkh
9187332Sjkh		reg = port + OREG_WPARAMS;
9197332Sjkh		/* send the read command */
9207332Sjkh		disable_intr();
9217332Sjkh		outb(reg, sdata[0]);
9227332Sjkh		outb(reg, sdata[1]);
9237332Sjkh		outb(reg, sdata[2]);
9247332Sjkh		outb(reg, 0);
9257332Sjkh		outb(reg, 0);
9267332Sjkh		outb(reg, 1);
9277332Sjkh		outb(port+OREG_COMMAND, CMD_READ);
9287332Sjkh		enable_intr();
9297332Sjkh
9307332Sjkh		mbx->count = RDELAY_WAITREAD;
9317332Sjkh		for (i = 0; i < 50; i++) {
9327332Sjkh			if (STATUS_BIT(port, SBIT_DATA_READY))
9337332Sjkh				goto got_data;
9347332Sjkh			DELAY(100);
9357332Sjkh		}
9367332Sjkh
93725056Sbde		timeout(scd_timeout,
9387332Sjkh			(caddr_t)SCD_S_WAITREAD,hz/100); /* XXX */
9397332Sjkh		return;
9407332Sjkh
9417332Sjkh	case SCD_S_WAITREAD:
94225056Sbde		untimeout(scd_timeout,(caddr_t)SCD_S_WAITREAD);
9437332Sjkh		if (mbx->count-- <= 0) {
9447332Sjkh			if (STATUS_BIT(port, SBIT_RESULT_READY))
9457332Sjkh				goto got_param;
9467332Sjkh			printf("scd%d: timeout while reading data\n",unit);
9477332Sjkh			goto readerr;
9487332Sjkh		}
9497332Sjkh		if (!STATUS_BIT(port, SBIT_DATA_READY)) {
9507332Sjkh			process_attention(unit);
9517332Sjkh			if (!(cd->flags & SCDVALID))
9527332Sjkh				goto changed;
95325056Sbde			timeout(scd_timeout,
9547332Sjkh				(caddr_t)SCD_S_WAITREAD,hz/100); /* XXX */
9557332Sjkh			return;
9567332Sjkh		}
9577332Sjkh		XDEBUG(2, ("scd%d: mbx->count (after RDY_BIT) = %d(%d)\n", unit, mbx->count, RDELAY_WAITREAD));
9587332Sjkh
9597332Sjkhgot_data:
9607332Sjkh		/* data is ready */
9617332Sjkh		addr = bp->b_un.b_addr + mbx->skip;
9627332Sjkh		write_control(port, CBIT_DATA_READY_CLEAR);
9637332Sjkh		insb(port+IREG_DATA, addr, mbx->sz);
9647332Sjkh
9657332Sjkh		mbx->count = 100;
9667332Sjkh		for (i = 0; i < 20; i++) {
9677332Sjkh			if (STATUS_BIT(port, SBIT_RESULT_READY))
9687332Sjkh				goto waitfor_param;
9697332Sjkh			DELAY(100);
9707332Sjkh		}
9717332Sjkh		goto waitfor_param;
9727332Sjkh
9737332Sjkh	case SCD_S_WAITPARAM:
97425056Sbde		untimeout(scd_timeout,(caddr_t)SCD_S_WAITPARAM);
9757332Sjkh		if (mbx->count-- <= 0) {
9767332Sjkh			printf("scd%d: timeout waiting for params\n",unit);
9777332Sjkh			goto readerr;
9787332Sjkh		}
9797332Sjkh
9807332Sjkhwaitfor_param:
9817332Sjkh		if (!STATUS_BIT(port, SBIT_RESULT_READY)) {
98225056Sbde			timeout(scd_timeout,
9837332Sjkh				(caddr_t)SCD_S_WAITPARAM,hz/100); /* XXX */
9847332Sjkh			return;
9857332Sjkh		}
9867332Sjkh#if SCD_DEBUG
9877332Sjkh		if (mbx->count < 100 && scd_debuglevel > 0)
9887332Sjkh			printf("scd%d: mbx->count (paramwait) = %d(%d)\n", unit, mbx->count, 100);
9897332Sjkh#endif
9907332Sjkh
9917332Sjkhgot_param:
9927332Sjkh		write_control(port, CBIT_RESULT_READY_CLEAR);
9937332Sjkh		switch ((i = inb(port+IREG_RESULT)) & 0xf0) {
9947332Sjkh		case 0x50:
9957332Sjkh			switch (i) {
9967332Sjkh			case ERR_FATAL_READ_ERROR1:
9977332Sjkh			case ERR_FATAL_READ_ERROR2:
9987332Sjkh				printf("scd%d: unrecoverable read error 0x%x\n", unit, i);
9997332Sjkh				goto harderr;
10007332Sjkh			}
10017332Sjkh			break;
10027332Sjkh		case 0x20:
10037332Sjkh			i = inb(port+IREG_RESULT);
10047332Sjkh			switch (i) {
10057332Sjkh			case ERR_NOT_SPINNING:
10067332Sjkh				XDEBUG(1, ("scd%d: read error: drive not spinning\n", unit));
10077332Sjkh				if (mbx->retry-- > 0) {
10087332Sjkh					state = SCD_S_BEGIN1;
10097332Sjkh					cd->flags &= ~SCDSPINNING;
10107332Sjkh					goto loop;
10117332Sjkh				}
10127332Sjkh				goto harderr;
10137332Sjkh			default:
10147332Sjkh				print_error(unit, i);
10157332Sjkh				goto readerr;
10167332Sjkh			}
10177332Sjkh		case 0x00:
10187332Sjkh			i = inb(port+IREG_RESULT);
10197332Sjkh			break;
10207332Sjkh		}
10217332Sjkh
10227332Sjkh		if (--mbx->nblk > 0) {
10237332Sjkh			mbx->skip += mbx->sz;
10247332Sjkh			goto nextblock;
10257332Sjkh		}
10267332Sjkh
10277332Sjkh		/* return buffer */
10287332Sjkh		bp->b_resid = 0;
10297332Sjkh		biodone(bp);
10307332Sjkh
10317332Sjkh		cd->flags &= ~SCDMBXBSY;
10327332Sjkh		scd_start(mbx->unit);
10337332Sjkh		return;
10347332Sjkh	}
10357332Sjkh
10367332Sjkhreaderr:
10377332Sjkh	if (mbx->retry-- > 0) {
10387332Sjkh		printf("scd%d: retrying ...\n",unit);
10397332Sjkh		state = SCD_S_BEGIN1;
10407332Sjkh		goto loop;
10417332Sjkh	}
10427332Sjkhharderr:
10437332Sjkh	/* invalidate the buffer */
10447332Sjkh	bp->b_error = EIO;
10457332Sjkh	bp->b_flags |= B_ERROR;
10467332Sjkh	bp->b_resid = bp->b_bcount;
10477332Sjkh	biodone(bp);
10487332Sjkh
10497332Sjkh	cd->flags &= ~SCDMBXBSY;
10507332Sjkh	scd_start(mbx->unit);
10517332Sjkh	return;
10527332Sjkh
10537332Sjkhchanged:
10547332Sjkh	printf("scd%d: media changed\n", unit);
10557332Sjkh	goto harderr;
10567332Sjkh}
10577332Sjkh
10587332Sjkhstatic void
10597332Sjkhhsg2msf(int hsg, bcd_t *msf)
10607332Sjkh{
10617332Sjkh	hsg += 150;
10627332Sjkh	M_msf(msf) = bin2bcd(hsg / 4500);
10637332Sjkh	hsg %= 4500;
10647332Sjkh	S_msf(msf) = bin2bcd(hsg / 75);
10657332Sjkh	F_msf(msf) = bin2bcd(hsg % 75);
10667332Sjkh}
10677332Sjkh
10687332Sjkhstatic int
10697332Sjkhmsf2hsg(bcd_t *msf)
10707332Sjkh{
10717332Sjkh	return (bcd2bin(M_msf(msf)) * 60 +
10727332Sjkh		bcd2bin(S_msf(msf))) * 75 +
10737332Sjkh		bcd2bin(F_msf(msf)) - 150;
10747332Sjkh}
10757332Sjkh
10767332Sjkhstatic void
10777332Sjkhprocess_attention(unsigned unit)
10787332Sjkh{
10797332Sjkh	unsigned port = scd_data[unit].iobase;
10807332Sjkh	unsigned char code;
10817332Sjkh	int count = 0;
10827332Sjkh
10837332Sjkh	while (IS_ATTENTION(port) && count++ < 30) {
10847332Sjkh		write_control(port, CBIT_ATTENTION_CLEAR);
10857332Sjkh		code = inb(port+IREG_RESULT);
10867332Sjkh
10877332Sjkh#if SCD_DEBUG
10887332Sjkh		if (scd_debuglevel > 0) {
10897332Sjkh			if (count == 1)
10907332Sjkh				printf("scd%d: DEBUG: ATTENTIONS = 0x%x", unit, code);
10917332Sjkh			else
10927332Sjkh				printf(",0x%x", code);
10937332Sjkh		}
10947332Sjkh#endif
10957332Sjkh
10967332Sjkh		switch (code) {
10977332Sjkh		case ATTEN_SPIN_DOWN:
10987332Sjkh			scd_data[unit].flags &= ~SCDSPINNING;
10997332Sjkh			break;
11007332Sjkh
11017332Sjkh		case ATTEN_SPIN_UP_DONE:
11027332Sjkh			scd_data[unit].flags |= SCDSPINNING;
11037332Sjkh			break;
11047332Sjkh
11057332Sjkh		case ATTEN_AUDIO_DONE:
11067332Sjkh			scd_data[unit].audio_status = CD_AS_PLAY_COMPLETED;
11077332Sjkh			break;
11087332Sjkh
11097332Sjkh		case ATTEN_DRIVE_LOADED:
11107332Sjkh			scd_data[unit].flags &= ~(SCDTOC|SCDSPINNING|SCDVALID);
11117332Sjkh			scd_data[unit].audio_status = CD_AS_AUDIO_INVALID;
11127332Sjkh			break;
11137332Sjkh
11147332Sjkh		case ATTEN_EJECT_PUSHED:
11157332Sjkh			scd_data[unit].flags &= ~SCDVALID;
11167332Sjkh			break;
11177332Sjkh		}
11187332Sjkh		DELAY(100);
11197332Sjkh	}
11207332Sjkh#if SCD_DEBUG
11217332Sjkh	if (scd_debuglevel > 0 && count > 0)
11227332Sjkh		printf("\n");
11237332Sjkh#endif
11247332Sjkh}
11257332Sjkh
11267332Sjkh/* Returns 0 OR sony error code */
11277332Sjkhstatic int
11287332Sjkhspin_up(unsigned unit)
11297332Sjkh{
11307332Sjkh	unsigned char res_reg[12];
11317332Sjkh	unsigned int res_size;
11327332Sjkh	int rc;
11337332Sjkh	int loop_count = 0;
11347332Sjkh
11357332Sjkhagain:
113617108Sbde	rc = send_cmd(unit, CMD_SPIN_UP, 0, 0, res_reg, &res_size);
11377332Sjkh	if (rc != 0) {
11387332Sjkh		XDEBUG(2, ("scd%d: CMD_SPIN_UP error 0x%x\n", unit, rc));
11397332Sjkh		return rc;
11407332Sjkh	}
11417332Sjkh
11427332Sjkh	if (!(scd_data[unit].flags & SCDTOC)) {
11437332Sjkh		rc = send_cmd(unit, CMD_READ_TOC, 0);
11447332Sjkh		if (rc == ERR_NOT_SPINNING) {
11457332Sjkh			if (loop_count++ < 3)
11467332Sjkh				goto again;
11477332Sjkh			return rc;
11487332Sjkh		}
11497332Sjkh		if (rc != 0)
11507332Sjkh			return rc;
11517332Sjkh	}
11527332Sjkh
11537332Sjkh	scd_data[unit].flags |= SCDSPINNING;
11547332Sjkh
11557332Sjkh	return 0;
11567332Sjkh}
11577332Sjkh
11587332Sjkhstatic struct sony_tracklist *
11597332Sjkhget_tl(struct sony_toc *toc, int size)
11607332Sjkh{
11617332Sjkh	struct sony_tracklist *tl = &toc->tracks[0];
11627332Sjkh
11637332Sjkh	if (tl->track != 0xb0)
11647332Sjkh		return tl;
11657332Sjkh	(char *)tl += 9;
11668876Srgrimes	if (tl->track != 0xb1)
11677332Sjkh		return tl;
11687332Sjkh	(char *)tl += 9;
11698876Srgrimes	if (tl->track != 0xb2)
11707332Sjkh		return tl;
11717332Sjkh	(char *)tl += 9;
11728876Srgrimes	if (tl->track != 0xb3)
11737332Sjkh		return tl;
11747332Sjkh	(char *)tl += 9;
11758876Srgrimes	if (tl->track != 0xb4)
11767332Sjkh		return tl;
11777332Sjkh	(char *)tl += 9;
11788876Srgrimes	if (tl->track != 0xc0)
11797332Sjkh		return tl;
11807332Sjkh	(char *)tl += 9;
11817332Sjkh	return tl;
11827332Sjkh}
11837332Sjkh
11847332Sjkhstatic int
11857332Sjkhread_toc(dev_t dev)
11867332Sjkh{
11877332Sjkh	unsigned unit;
11887332Sjkh	struct scd_data *cd;
11897332Sjkh	unsigned part = 0;	/* For now ... */
11907332Sjkh	struct sony_toc toc;
11917332Sjkh	struct sony_tracklist *tl;
11927332Sjkh	int rc, i, j;
11937332Sjkh	u_long first, last;
11947332Sjkh
11957332Sjkh	unit = scd_unit(dev);
11967332Sjkh	cd = scd_data + unit;
11977332Sjkh
11987332Sjkh	rc = send_cmd(unit, CMD_GET_TOC, 1, part+1);
11997332Sjkh	if (rc < 0)
12007332Sjkh		return rc;
12017332Sjkh	if (rc > sizeof(toc)) {
12027332Sjkh		printf("scd%d: program error: toc too large (%d)\n", unit, rc);
12037332Sjkh		return EIO;
12047332Sjkh	}
12057332Sjkh	if (get_result(unit, rc, (u_char *)&toc) != 0)
12067332Sjkh		return EIO;
12077332Sjkh
12087332Sjkh	XDEBUG(1, ("scd%d: toc read. len = %d, sizeof(toc) = %d\n", unit, rc, sizeof(toc)));
12097332Sjkh
12107332Sjkh	tl = get_tl(&toc, rc);
12117332Sjkh	first = msf2hsg(tl->start_msf);
12127332Sjkh	last = msf2hsg(toc.lead_out_start_msf);
12137332Sjkh	cd->blksize = SCDBLKSIZE;
12147332Sjkh	cd->disksize = last*cd->blksize/DEV_BSIZE;
12157332Sjkh
12168376Srgrimes	XDEBUG(1, ("scd%d: firstsector = %ld, lastsector = %ld", unit,
12177332Sjkh			first, last));
12187332Sjkh
12197332Sjkh	cd->first_track = bcd2bin(toc.first_track);
12207332Sjkh	cd->last_track = bcd2bin(toc.last_track);
12217332Sjkh	if (cd->last_track > (MAX_TRACKS-2))
12227332Sjkh		cd->last_track = MAX_TRACKS-2;
12237332Sjkh	for (j = 0, i = cd->first_track; i <= cd->last_track; i++, j++) {
12247332Sjkh		cd->toc[i].adr = tl[j].adr;
12257332Sjkh		cd->toc[i].ctl = tl[j].ctl; /* for xcdplayer */
12267332Sjkh		bcopy(tl[j].start_msf, cd->toc[i].start_msf, 3);
12277332Sjkh#ifdef SCD_DEBUG
12287332Sjkh		if (scd_debuglevel > 0) {
12297332Sjkh			if ((j % 3) == 0)
12307332Sjkh				printf("\nscd%d: tracks ", unit);
12317332Sjkh			printf("[%03d: %2d %2d %2d]  ", i,
12327332Sjkh				bcd2bin(cd->toc[i].start_msf[0]),
12337332Sjkh				bcd2bin(cd->toc[i].start_msf[1]),
12347332Sjkh				bcd2bin(cd->toc[i].start_msf[2]));
12357332Sjkh		}
12367332Sjkh#endif
12377332Sjkh	}
12387332Sjkh	bcopy(toc.lead_out_start_msf, cd->toc[cd->last_track+1].start_msf, 3);
12397332Sjkh#ifdef SCD_DEBUG
12407332Sjkh	if (scd_debuglevel > 0) {
12417332Sjkh		i = cd->last_track+1;
12427332Sjkh		printf("[END: %2d %2d %2d]\n",
12437332Sjkh			bcd2bin(cd->toc[i].start_msf[0]),
12447332Sjkh			bcd2bin(cd->toc[i].start_msf[1]),
12457332Sjkh			bcd2bin(cd->toc[i].start_msf[2]));
12467332Sjkh	}
12477332Sjkh#endif
12487332Sjkh
12497332Sjkh	bzero(&cd->dlabel,sizeof(struct disklabel));
12507332Sjkh	/* filled with spaces first */
12517332Sjkh	strncpy(cd->dlabel.d_typename,"               ",
12527332Sjkh		sizeof(cd->dlabel.d_typename));
12537332Sjkh	strncpy(cd->dlabel.d_typename, cd->name,
12547332Sjkh		min(strlen(cd->name), sizeof(cd->dlabel.d_typename) - 1));
12557332Sjkh	strncpy(cd->dlabel.d_packname,"unknown        ",
12567332Sjkh		sizeof(cd->dlabel.d_packname));
12577332Sjkh	cd->dlabel.d_secsize 	= cd->blksize;
12587332Sjkh	cd->dlabel.d_nsectors	= 100;
12597332Sjkh	cd->dlabel.d_ntracks	= 1;
12607332Sjkh	cd->dlabel.d_ncylinders	= (cd->disksize/100)+1;
12617332Sjkh	cd->dlabel.d_secpercyl	= 100;
12627332Sjkh	cd->dlabel.d_secperunit	= cd->disksize;
12637332Sjkh	cd->dlabel.d_rpm	= 300;
12647332Sjkh	cd->dlabel.d_interleave	= 1;
12657332Sjkh	cd->dlabel.d_flags	= D_REMOVABLE;
12667332Sjkh	cd->dlabel.d_npartitions= 1;
12677332Sjkh	cd->dlabel.d_partitions[0].p_offset = 0;
12687332Sjkh	cd->dlabel.d_partitions[0].p_size = cd->disksize;
12697332Sjkh	cd->dlabel.d_partitions[0].p_fstype = 9;
12707332Sjkh	cd->dlabel.d_magic	= DISKMAGIC;
12717332Sjkh	cd->dlabel.d_magic2	= DISKMAGIC;
12727332Sjkh	cd->dlabel.d_checksum	= dkcksum(&cd->dlabel);
12737332Sjkh
12747332Sjkh	cd->flags |= SCDTOC;
12757332Sjkh
12767332Sjkh	return 0;
12777332Sjkh}
12787332Sjkh
12797332Sjkhstatic inline void
12807332Sjkhwrite_control(unsigned port, unsigned data)
12817332Sjkh{
12827332Sjkh	outb(port + OREG_CONTROL, data);
12837332Sjkh}
12847332Sjkh
12857332Sjkhstatic void
12867332Sjkhinit_drive(unsigned unit)
12877332Sjkh{
12887332Sjkh	int rc;
12897332Sjkh
12907332Sjkh	rc = send_cmd(unit, CMD_SET_DRIVE_PARAM, 2,
12917332Sjkh		0x05, 0x03 | ((scd_data[unit].double_speed) ? 0x04: 0));
12927332Sjkh	if (rc != 0)
12937332Sjkh		printf("scd%d: Unable to set parameters. Errcode = 0x%x\n", unit, rc);
12947332Sjkh}
12957332Sjkh
12967332Sjkh/* Returns 0 or errno */
12977332Sjkhstatic int
12987332Sjkhget_result(u_int unit, int result_len, u_char *result)
12997332Sjkh{
13007332Sjkh	unsigned int port = scd_data[unit].iobase;
13017332Sjkh	unsigned int res_reg = port + IREG_RESULT;
13027332Sjkh	int loop_index = 2; /* send_cmd() reads two bytes ... */
13037332Sjkh
13047332Sjkh	XDEBUG(1, ("scd%d: DEBUG: get_result: bytes=%d\n", unit, result_len));
13057332Sjkh
13067332Sjkh	while (result_len-- > 0) {
13077332Sjkh		if (loop_index++ >= 10) {
13087332Sjkh			loop_index = 1;
13097332Sjkh			if (waitfor_status_bits(unit, SBIT_RESULT_READY, 0))
13107332Sjkh				return EIO;
13117332Sjkh			write_control(port, CBIT_RESULT_READY_CLEAR);
13127332Sjkh		}
13137332Sjkh		if (result)
13147332Sjkh			*result++ = inb(res_reg);
13157332Sjkh		else
13167332Sjkh			(void)inb(res_reg);
13177332Sjkh	}
13187332Sjkh	return 0;
13197332Sjkh}
13207332Sjkh
13217332Sjkh/* Returns -0x100 for timeout, -(drive error code) OR number of result bytes */
13227332Sjkhstatic int
13237332Sjkhsend_cmd(u_int unit, u_char cmd, u_int nargs, ...)
13247332Sjkh{
13257332Sjkh	va_list ap;
13267332Sjkh	u_int port = scd_data[unit].iobase;
13277332Sjkh	u_int reg;
13287332Sjkh	u_char c;
13297332Sjkh	int rc;
13307332Sjkh	int i;
13317332Sjkh
13327332Sjkh	if (waitfor_status_bits(unit, 0, SBIT_BUSY)) {
13337332Sjkh		printf("scd%d: drive busy\n", unit);
13347332Sjkh		return -0x100;
13357332Sjkh	}
13367332Sjkh
13377332Sjkh	XDEBUG(1,("scd%d: DEBUG: send_cmd: cmd=0x%x nargs=%d", unit, cmd, nargs));
13387332Sjkh
13397332Sjkh	write_control(port, CBIT_RESULT_READY_CLEAR);
13407332Sjkh	write_control(port, CBIT_RPARAM_CLEAR);
13417332Sjkh
13427332Sjkh	for (i = 0; i < 100; i++)
13437332Sjkh		if (FSTATUS_BIT(port, FBIT_WPARAM_READY))
13447332Sjkh			break;
13457332Sjkh	if (!FSTATUS_BIT(port, FBIT_WPARAM_READY)) {
13467332Sjkh		XDEBUG(1, ("\nscd%d: wparam timeout\n", unit));
13477332Sjkh		return -EIO;
13487332Sjkh	}
13498876Srgrimes
13507332Sjkh	va_start(ap, nargs);
13517332Sjkh	reg = port + OREG_WPARAMS;
13527332Sjkh	for (i = 0; i < nargs; i++) {
13537332Sjkh		c = (u_char)va_arg(ap, int);
13547332Sjkh		outb(reg, c);
13557332Sjkh		XDEBUG(1, (",{0x%x}", c));
13567332Sjkh	}
13577332Sjkh	va_end(ap);
13587332Sjkh	XDEBUG(1, ("\n"));
13597332Sjkh
13607332Sjkh	outb(port+OREG_COMMAND, cmd);
13617332Sjkh
136211872Sphk	rc = waitfor_status_bits(unit, SBIT_RESULT_READY, SBIT_BUSY);
136311872Sphk	if (rc)
13647332Sjkh		return -0x100;
13657332Sjkh
13667332Sjkh	reg = port + IREG_RESULT;
13677332Sjkh	write_control(port, CBIT_RESULT_READY_CLEAR);
13687332Sjkh	switch ((rc = inb(reg)) & 0xf0) {
13697332Sjkh	case 0x20:
13707332Sjkh		rc = inb(reg);
13717332Sjkh		/* FALL TROUGH */
13727332Sjkh	case 0x50:
13737332Sjkh		XDEBUG(1, ("scd%d: DEBUG: send_cmd: drive_error=0x%x\n", unit, rc));
13747332Sjkh		return -rc;
13757332Sjkh	case 0x00:
13767332Sjkh	default:
13777332Sjkh		rc = inb(reg);
13787332Sjkh		XDEBUG(1, ("scd%d: DEBUG: send_cmd: result_len=%d\n", unit, rc));
13797332Sjkh		return rc;
13807332Sjkh	}
13817332Sjkh}
13827332Sjkh
13837332Sjkhstatic void
13847332Sjkhprint_error(int unit, int errcode)
13857332Sjkh{
13867332Sjkh	switch (errcode) {
13877332Sjkh	case -ERR_CD_NOT_LOADED:
13887332Sjkh		printf("scd%d: door is open\n", unit);
13897332Sjkh		break;
13907332Sjkh	case -ERR_NO_CD_INSIDE:
13917332Sjkh		printf("scd%d: no cd inside\n", unit);
13927332Sjkh		break;
13937332Sjkh	default:
13947332Sjkh		if (errcode == -0x100 || errcode > 0)
13957332Sjkh			printf("scd%d: device timeout\n", unit);
13967332Sjkh		else
13977332Sjkh			printf("scd%d: unexpected error 0x%x\n", unit, -errcode);
13987332Sjkh		break;
13997332Sjkh	}
14007332Sjkh}
14017332Sjkh
14027332Sjkh/* Returns 0 or errno value */
14037332Sjkhstatic int
14047332Sjkhwaitfor_status_bits(int unit, int bits_set, int bits_clear)
14057332Sjkh{
14067332Sjkh	u_int port = scd_data[unit].iobase;
14077332Sjkh	u_int flags = scd_data[unit].flags;
14087332Sjkh	u_int reg = port + IREG_STATUS;
14097332Sjkh	u_int max_loop;
14107332Sjkh	u_char c = 0;
14117332Sjkh
14127332Sjkh	if (flags & SCDPROBING) {
14137332Sjkh		max_loop = 0;
14147332Sjkh		while (max_loop++ < 1000) {
14157332Sjkh			c = inb(reg);
14167332Sjkh			if (c == 0xff)
14177332Sjkh				return EIO;
14187332Sjkh			if (c & SBIT_ATTENTION) {
14197332Sjkh				process_attention(unit);
14207332Sjkh				continue;
14217332Sjkh			}
14227332Sjkh			if ((c & bits_set) == bits_set &&
14237332Sjkh			    (c & bits_clear) == 0)
14247332Sjkh			{
14257332Sjkh				break;
14267332Sjkh			}
14277332Sjkh			DELAY(10000);
14287332Sjkh		}
14297332Sjkh	} else {
14307332Sjkh		max_loop = 100;
14317332Sjkh		while (max_loop-- > 0) {
14327332Sjkh			c = inb(reg);
14337332Sjkh			if (c & SBIT_ATTENTION) {
14347332Sjkh				process_attention(unit);
14357332Sjkh				continue;
14367332Sjkh			}
14377332Sjkh			if ((c & bits_set) == bits_set &&
14387332Sjkh			    (c & bits_clear) == 0)
14397332Sjkh			{
14407332Sjkh				break;
14417332Sjkh			}
14427332Sjkh			tsleep(waitfor_status_bits, PZERO - 1, "waitfor", hz/10);
14437332Sjkh		}
14447332Sjkh	}
14457332Sjkh	if ((c & bits_set) == bits_set &&
14467332Sjkh	    (c & bits_clear) == 0)
14477332Sjkh	{
14487332Sjkh		return 0;
14497332Sjkh	}
14507332Sjkh#ifdef SCD_DEBUG
14517332Sjkh	if (scd_debuglevel > 0)
14527332Sjkh		printf("scd%d: DEBUG: waitfor: TIMEOUT (0x%x,(0x%x,0x%x))\n", unit, c, bits_set, bits_clear);
14537332Sjkh	else
14547332Sjkh#endif
14557332Sjkh		printf("scd%d: timeout.\n", unit);
14567332Sjkh	return EIO;
14577332Sjkh}
14587332Sjkh
14597332Sjkh/* these two routines for xcdplayer - "borrowed" from mcd.c */
14607332Sjkhstatic int
14617332Sjkhscd_toc_header (int unit, struct ioc_toc_header* th)
14627332Sjkh{
14637332Sjkh	struct scd_data *cd = scd_data + unit;
14647332Sjkh	int rc;
14657332Sjkh
14667332Sjkh	if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
14677332Sjkh		print_error(unit, rc);
14687332Sjkh		return EIO;
14697332Sjkh	}
14707332Sjkh
14717332Sjkh	th->starting_track = cd->first_track;
14727332Sjkh	th->ending_track = cd->last_track;
14737332Sjkh	th->len = 0; /* not used */
14747332Sjkh
14757332Sjkh	return 0;
14767332Sjkh}
14777332Sjkh
14787332Sjkhstatic int
14797332Sjkhscd_toc_entrys (int unit, struct ioc_read_toc_entry *te)
14807332Sjkh{
14817332Sjkh	struct scd_data *cd = scd_data + unit;
14827332Sjkh	struct cd_toc_entry toc_entry;
14837332Sjkh	int rc, i, len = te->data_len;
14847332Sjkh
14857332Sjkh	if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
14867332Sjkh		print_error(unit, rc);
14877332Sjkh		return EIO;
14887332Sjkh	}
14897332Sjkh
14907332Sjkh	/* find the toc to copy*/
14917332Sjkh	i = te->starting_track;
14927332Sjkh	if (i == SCD_LASTPLUS1)
14937332Sjkh		i = cd->last_track + 1;
14948876Srgrimes
14957332Sjkh	/* verify starting track */
14967332Sjkh	if (i < cd->first_track || i > cd->last_track+1)
14977332Sjkh		return EINVAL;
14987332Sjkh
14997332Sjkh	/* valid length ? */
15007332Sjkh	if (len < sizeof(struct cd_toc_entry)
15017332Sjkh	    || (len % sizeof(struct cd_toc_entry)) != 0)
15027332Sjkh		return EINVAL;
15037332Sjkh
15047332Sjkh	/* copy the toc data */
15057332Sjkh	toc_entry.control = cd->toc[i].ctl;
15067332Sjkh	toc_entry.addr_type = te->address_format;
15077332Sjkh	toc_entry.track = i;
15087332Sjkh	if (te->address_format == CD_MSF_FORMAT) {
15097332Sjkh		toc_entry.addr.msf.unused = 0;
15107332Sjkh		toc_entry.addr.msf.minute = bcd2bin(cd->toc[i].start_msf[0]);
15117332Sjkh		toc_entry.addr.msf.second = bcd2bin(cd->toc[i].start_msf[1]);
15127332Sjkh		toc_entry.addr.msf.frame = bcd2bin(cd->toc[i].start_msf[2]);
15137332Sjkh	}
15147332Sjkh
15157332Sjkh	/* copy the data back */
15167332Sjkh	if (copyout(&toc_entry, te->data, sizeof(struct cd_toc_entry)) != 0)
15177332Sjkh		return EFAULT;
15187332Sjkh
15197332Sjkh	return 0;
15207332Sjkh}
15217332Sjkh
152212517Sjulian
152325460Sjoergstatic int
152425460Sjoergscd_toc_entry (int unit, struct ioc_read_toc_single_entry *te)
152525460Sjoerg{
152625460Sjoerg	struct scd_data *cd = scd_data + unit;
152725460Sjoerg	struct cd_toc_entry toc_entry;
152825460Sjoerg	int rc, i;
152925460Sjoerg
153025460Sjoerg	if (!(cd->flags & SCDTOC) && (rc = read_toc(unit)) != 0) {
153125460Sjoerg		print_error(unit, rc);
153225460Sjoerg		return EIO;
153325460Sjoerg	}
153425460Sjoerg
153525460Sjoerg	/* find the toc to copy*/
153625460Sjoerg	i = te->track;
153725460Sjoerg	if (i == SCD_LASTPLUS1)
153825460Sjoerg		i = cd->last_track + 1;
153925460Sjoerg
154025460Sjoerg	/* verify starting track */
154125460Sjoerg	if (i < cd->first_track || i > cd->last_track+1)
154225460Sjoerg		return EINVAL;
154325460Sjoerg
154425460Sjoerg	/* copy the toc data */
154525460Sjoerg	toc_entry.control = cd->toc[i].ctl;
154625460Sjoerg	toc_entry.addr_type = te->address_format;
154725460Sjoerg	toc_entry.track = i;
154825460Sjoerg	if (te->address_format == CD_MSF_FORMAT) {
154925460Sjoerg		toc_entry.addr.msf.unused = 0;
155025460Sjoerg		toc_entry.addr.msf.minute = bcd2bin(cd->toc[i].start_msf[0]);
155125460Sjoerg		toc_entry.addr.msf.second = bcd2bin(cd->toc[i].start_msf[1]);
155225460Sjoerg		toc_entry.addr.msf.frame = bcd2bin(cd->toc[i].start_msf[2]);
155325460Sjoerg	}
155425460Sjoerg
155525460Sjoerg	/* copy the data back */
155625460Sjoerg	bcopy(&toc_entry, &te->entry, sizeof(struct cd_toc_entry));
155725460Sjoerg
155825460Sjoerg	return 0;
155925460Sjoerg}
156025460Sjoerg
156125460Sjoerg
156212502Sjulianstatic scd_devsw_installed = 0;
156312502Sjulian
156412517Sjulianstatic void 	scd_drvinit(void *unused)
156512502Sjulian{
156612517Sjulian
156712502Sjulian	if( ! scd_devsw_installed ) {
156817264Sphk		bdevsw_add_generic(BDEV_MAJOR,CDEV_MAJOR, &scd_bdevsw);
156912502Sjulian		scd_devsw_installed = 1;
157012517Sjulian    	}
157112502Sjulian}
157212517Sjulian
157312517SjulianSYSINIT(scddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,scd_drvinit,NULL)
157412517Sjulian
157512502Sjulian
15767332Sjkh#endif /* NSCD > 0 */
1577