xref: /freebsd/sys/dev/fdc/fdc.c (revision 9f02ad60735d2f13650512c3c74bd610f1785772)
187f6c662SJulian Elischer /*
25b81b6b3SRodney W. Grimes  * Copyright (c) 1990 The Regents of the University of California.
35b81b6b3SRodney W. Grimes  * All rights reserved.
45b81b6b3SRodney W. Grimes  *
55b81b6b3SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
65b81b6b3SRodney W. Grimes  * Don Ahn.
75b81b6b3SRodney W. Grimes  *
8dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994 by
93a2f7427SDavid Greenman  *  jc@irbs.UUCP (John Capo)
103a2f7427SDavid Greenman  *  vak@zebub.msk.su (Serge Vakulenko)
113a2f7427SDavid Greenman  *  ache@astral.msk.su (Andrew A. Chernov)
12dc16046fSJoerg Wunsch  *
13dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994, 1995 by
143a2f7427SDavid Greenman  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
15dc5df763SJoerg Wunsch  *  dufault@hda.com (Peter Dufault)
163a2f7427SDavid Greenman  *
175b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
185b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
195b81b6b3SRodney W. Grimes  * are met:
205b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
215b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
225b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
235b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
245b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
255b81b6b3SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
265b81b6b3SRodney W. Grimes  *    must display the following acknowledgement:
275b81b6b3SRodney W. Grimes  *	This product includes software developed by the University of
285b81b6b3SRodney W. Grimes  *	California, Berkeley and its contributors.
295b81b6b3SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
305b81b6b3SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
315b81b6b3SRodney W. Grimes  *    without specific prior written permission.
325b81b6b3SRodney W. Grimes  *
335b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
345b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
355b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
365b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
375b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
385b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
395b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
405b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
415b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
425b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
435b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
445b81b6b3SRodney W. Grimes  *
45dc4ff321SRodney W. Grimes  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
469f02ad60SJustin T. Gibbs  *	$Id: fd.c,v 1.122 1998/09/15 08:15:28 gibbs Exp $
475b81b6b3SRodney W. Grimes  *
485b81b6b3SRodney W. Grimes  */
495b81b6b3SRodney W. Grimes 
50b99f0a4aSAndrew Moore #include "ft.h"
51b99f0a4aSAndrew Moore #if NFT < 1
52b99f0a4aSAndrew Moore #undef NFDC
53b99f0a4aSAndrew Moore #endif
545b81b6b3SRodney W. Grimes #include "fd.h"
557b778b5eSEivind Eklund #include "opt_devfs.h"
56d2fb4892SJoerg Wunsch #include "opt_fdc.h"
575b81b6b3SRodney W. Grimes 
58b99f0a4aSAndrew Moore #if NFDC > 0
59b99f0a4aSAndrew Moore 
60b99f0a4aSAndrew Moore #include <sys/param.h>
61b99f0a4aSAndrew Moore #include <sys/systm.h>
62b99f0a4aSAndrew Moore #include <sys/kernel.h>
63b99f0a4aSAndrew Moore #include <sys/conf.h>
643ac4d1efSBruce Evans #include <sys/fcntl.h>
65671e2ceeSBruce Evans #include <machine/clock.h>
66b99f0a4aSAndrew Moore #include <machine/ioctl_fd.h>
67b99f0a4aSAndrew Moore #include <sys/disklabel.h>
68b99f0a4aSAndrew Moore #include <sys/buf.h>
69b2dfb1f9SJustin T. Gibbs #include <sys/devicestat.h>
70b99f0a4aSAndrew Moore #include <sys/malloc.h>
713a2f7427SDavid Greenman #include <sys/proc.h>
72b99f0a4aSAndrew Moore #include <sys/syslog.h>
73f540b106SGarrett Wollman #include <i386/isa/isa.h>
74f540b106SGarrett Wollman #include <i386/isa/isa_device.h>
75f540b106SGarrett Wollman #include <i386/isa/fdreg.h>
76f540b106SGarrett Wollman #include <i386/isa/fdc.h>
77f540b106SGarrett Wollman #include <i386/isa/rtc.h>
78dc5df763SJoerg Wunsch #include <machine/stdarg.h>
7987eafbcaSPoul-Henning Kamp #if NFT > 0
8087eafbcaSPoul-Henning Kamp #include <sys/ftape.h>
8187eafbcaSPoul-Henning Kamp #include <i386/isa/ftreg.h>
8287eafbcaSPoul-Henning Kamp #endif
838af5d536SJulian Elischer #ifdef	DEVFS
848af5d536SJulian Elischer #include <sys/devfsext.h>
853e425b96SJulian Elischer #endif	/* DEVFS */
865b81b6b3SRodney W. Grimes 
87b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
88b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX
895b81b6b3SRodney W. Grimes 
900722d6abSJoerg Wunsch /* configuration flags */
910722d6abSJoerg Wunsch #define FDC_PRETEND_D0	(1 << 0)	/* pretend drive 0 to be there */
920722d6abSJoerg Wunsch 
930722d6abSJoerg Wunsch /* internally used only, not really from CMOS: */
940722d6abSJoerg Wunsch #define RTCFDT_144M_PRETENDED	0x1000
950722d6abSJoerg Wunsch 
963a2f7427SDavid Greenman /*
973a2f7427SDavid Greenman  * this biotab field doubles as a field for the physical unit number
983a2f7427SDavid Greenman  * on the controller
993a2f7427SDavid Greenman  */
1003a2f7427SDavid Greenman #define id_physid id_scsiid
1013a2f7427SDavid Greenman 
102dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
103dc5df763SJoerg Wunsch #define FD_FAILED -1
104dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
105dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
106dc5df763SJoerg Wunsch 
107b39c878eSAndrey A. Chernov #define NUMTYPES 14
108b39c878eSAndrey A. Chernov #define NUMDENS  (NUMTYPES - 6)
1097ca0641bSAndrey A. Chernov 
1103a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
111b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
112b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
113b99f0a4aSAndrew Moore #define FD_1720         1
114b99f0a4aSAndrew Moore #define FD_1480         2
115b99f0a4aSAndrew Moore #define FD_1440         3
116b99f0a4aSAndrew Moore #define FD_1200         4
117b99f0a4aSAndrew Moore #define FD_820          5
118b99f0a4aSAndrew Moore #define FD_800          6
119b99f0a4aSAndrew Moore #define FD_720          7
120b99f0a4aSAndrew Moore #define FD_360          8
121ed2fa05eSAndrey A. Chernov 
122b99f0a4aSAndrew Moore #define FD_1480in5_25   9
123b99f0a4aSAndrew Moore #define FD_1440in5_25   10
124b99f0a4aSAndrew Moore #define FD_820in5_25    11
125b99f0a4aSAndrew Moore #define FD_800in5_25    12
126b99f0a4aSAndrew Moore #define FD_720in5_25    13
127b99f0a4aSAndrew Moore #define FD_360in5_25    14
128b99f0a4aSAndrew Moore 
1297ca0641bSAndrey A. Chernov 
1306f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] =
1315b81b6b3SRodney W. Grimes {
132126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
133126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
134126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
135126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
136126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
137126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
138126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
139b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
140ed2fa05eSAndrey A. Chernov 
141126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
142126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
143126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
144126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
145126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
146126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
1475b81b6b3SRodney W. Grimes };
1485b81b6b3SRodney W. Grimes 
149b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
150dc16046fSJoerg Wunsch 
1515b81b6b3SRodney W. Grimes /***********************************************************************\
1525b81b6b3SRodney W. Grimes * Per controller structure.						*
1535b81b6b3SRodney W. Grimes \***********************************************************************/
154b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC];
1555b81b6b3SRodney W. Grimes 
1565b81b6b3SRodney W. Grimes /***********************************************************************\
1575b81b6b3SRodney W. Grimes * Per drive structure.							*
158b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
1595b81b6b3SRodney W. Grimes \***********************************************************************/
1606f4e0bebSPoul-Henning Kamp static struct fd_data {
161b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
1625b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
1633a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
1645b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
1655b81b6b3SRodney W. Grimes 	int	flags;
1665b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
1675b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
1685b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
1695b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
1705b81b6b3SRodney W. Grimes 	int	skip;
1715b81b6b3SRodney W. Grimes 	int	hddrv;
172dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
1735b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
1743a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
17502a19910SJustin T. Gibbs 	struct	callout_handle toffhandle;
17602a19910SJustin T. Gibbs 	struct	callout_handle tohandle;
177b2dfb1f9SJustin T. Gibbs 	struct	devstat device_stats;
17887f6c662SJulian Elischer #ifdef DEVFS
17921519754SBruce Evans 	void	*bdevs[1 + NUMDENS + MAXPARTITIONS];
18021519754SBruce Evans 	void	*cdevs[1 + NUMDENS + MAXPARTITIONS];
18187f6c662SJulian Elischer #endif
1825b81b6b3SRodney W. Grimes } fd_data[NFD];
1835b81b6b3SRodney W. Grimes 
1845b81b6b3SRodney W. Grimes /***********************************************************************\
1855b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
1865b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
1875b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
1885b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
1895b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
1905b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
1915b81b6b3SRodney W. Grimes \***********************************************************************/
192b99f0a4aSAndrew Moore 
1933a2f7427SDavid Greenman #if NFT > 0
1943a2f7427SDavid Greenman int ftopen(dev_t, int);
1953a2f7427SDavid Greenman int ftintr(ftu_t ftu);
1963a2f7427SDavid Greenman int ftclose(dev_t, int);
1973a2f7427SDavid Greenman void ftstrategy(struct buf *);
19829d6b96dSBruce Evans int ftioctl(dev_t, unsigned long, caddr_t, int, struct proc *);
1993a2f7427SDavid Greenman int ftdump(dev_t);
2003a2f7427SDavid Greenman int ftsize(dev_t);
20174fa89f4SRodney W. Grimes int ftattach(struct isa_device *, struct isa_device *, int);
2023a2f7427SDavid Greenman #endif
2035b81b6b3SRodney W. Grimes 
2043a2f7427SDavid Greenman /* autoconfig functions */
2053a2f7427SDavid Greenman static int fdprobe(struct isa_device *);
2063a2f7427SDavid Greenman static int fdattach(struct isa_device *);
2073a2f7427SDavid Greenman 
2083a2f7427SDavid Greenman /* needed for ft driver, thus exported */
2093a2f7427SDavid Greenman int in_fdc(fdcu_t);
2103a2f7427SDavid Greenman int out_fdc(fdcu_t, int);
2113a2f7427SDavid Greenman 
2123a2f7427SDavid Greenman /* internal functions */
2133a2f7427SDavid Greenman static void set_motor(fdcu_t, int, int);
2143a2f7427SDavid Greenman #  define TURNON 1
2153a2f7427SDavid Greenman #  define TURNOFF 0
2163a2f7427SDavid Greenman static timeout_t fd_turnoff;
2173a2f7427SDavid Greenman static timeout_t fd_motor_on;
2183a2f7427SDavid Greenman static void fd_turnon(fdu_t);
2193a2f7427SDavid Greenman static void fdc_reset(fdc_p);
220b5e8ce9fSBruce Evans static int fd_in(fdcu_t, int *);
2213a2f7427SDavid Greenman static void fdstart(fdcu_t);
2225c1a1eaeSBruce Evans static timeout_t fd_iotimeout;
2233a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2243a2f7427SDavid Greenman static int fdstate(fdcu_t, fdc_p);
225aaf08d94SGarrett Wollman static int retrier(fdcu_t);
2263a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
2273a2f7427SDavid Greenman 
228d66c374fSTor Egge static int enable_fifo(fdc_p fdc);
229d66c374fSTor Egge 
230d66c374fSTor Egge static int fifo_threshold = 8;	/* XXX: should be accessible via sysctl */
231d66c374fSTor Egge 
232aaf08d94SGarrett Wollman 
2335b81b6b3SRodney W. Grimes #define DEVIDLE		0
2345b81b6b3SRodney W. Grimes #define FINDWORK	1
2355b81b6b3SRodney W. Grimes #define	DOSEEK		2
2365b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 	3
2375b81b6b3SRodney W. Grimes #define	IOCOMPLETE	4
2385b81b6b3SRodney W. Grimes #define RECALCOMPLETE	5
2395b81b6b3SRodney W. Grimes #define	STARTRECAL	6
2405b81b6b3SRodney W. Grimes #define	RESETCTLR	7
2415b81b6b3SRodney W. Grimes #define	SEEKWAIT	8
2425b81b6b3SRodney W. Grimes #define	RECALWAIT	9
2435b81b6b3SRodney W. Grimes #define	MOTORWAIT	10
2445b81b6b3SRodney W. Grimes #define	IOTIMEDOUT	11
2455c1a1eaeSBruce Evans #define	RESETCOMPLETE	12
2465b81b6b3SRodney W. Grimes 
247d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
248cba2a7c6SBruce Evans static char const * const fdstates[] =
2495b81b6b3SRodney W. Grimes {
2505b81b6b3SRodney W. Grimes "DEVIDLE",
2515b81b6b3SRodney W. Grimes "FINDWORK",
2525b81b6b3SRodney W. Grimes "DOSEEK",
2535b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
2545b81b6b3SRodney W. Grimes "IOCOMPLETE",
2555b81b6b3SRodney W. Grimes "RECALCOMPLETE",
2565b81b6b3SRodney W. Grimes "STARTRECAL",
2575b81b6b3SRodney W. Grimes "RESETCTLR",
2585b81b6b3SRodney W. Grimes "SEEKWAIT",
2595b81b6b3SRodney W. Grimes "RECALWAIT",
2605b81b6b3SRodney W. Grimes "MOTORWAIT",
2615c1a1eaeSBruce Evans "IOTIMEDOUT",
2625c1a1eaeSBruce Evans "RESETCOMPLETE",
2635b81b6b3SRodney W. Grimes };
2645b81b6b3SRodney W. Grimes 
2653a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
266cba2a7c6SBruce Evans static int volatile fd_debug = 0;
2675b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
2685b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
269d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */
2705b81b6b3SRodney W. Grimes #define TRACE0(arg)
2715b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
272d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */
2735b81b6b3SRodney W. Grimes 
274dc16046fSJoerg Wunsch /* autoconfig structure */
275dc16046fSJoerg Wunsch 
276dc16046fSJoerg Wunsch struct	isa_driver fdcdriver = {
277dc16046fSJoerg Wunsch 	fdprobe, fdattach, "fdc",
278dc16046fSJoerg Wunsch };
279dc16046fSJoerg Wunsch 
28087f6c662SJulian Elischer static	d_open_t	Fdopen;	/* NOTE, not fdopen */
281f7ea2f55SJulian Elischer static	d_read_t	fdread;
282f7ea2f55SJulian Elischer static	d_write_t	fdwrite;
28387f6c662SJulian Elischer static	d_close_t	fdclose;
28487f6c662SJulian Elischer static	d_ioctl_t	fdioctl;
28587f6c662SJulian Elischer static	d_strategy_t	fdstrategy;
28687f6c662SJulian Elischer 
2873e425b96SJulian Elischer /* even if SLICE defined, these are needed for the ft support. */
28887f6c662SJulian Elischer #define CDEV_MAJOR 9
28987f6c662SJulian Elischer #define BDEV_MAJOR 2
290f7ea2f55SJulian Elischer 
291f7ea2f55SJulian Elischer 
292f7ea2f55SJulian Elischer static struct cdevsw fd_cdevsw = {
293f7ea2f55SJulian Elischer 	  Fdopen,	fdclose,	fdread,	fdwrite,
294f7ea2f55SJulian Elischer 	  fdioctl,	nostop,		nullreset,	nodevtotty,
295f7ea2f55SJulian Elischer 	  seltrue,	nommap,		fdstrategy,	"fd",
296f7ea2f55SJulian Elischer 	  NULL,		-1,		nodump,		nopsize,
297f7ea2f55SJulian Elischer 	  D_DISK,	0,		-1 };
29887f6c662SJulian Elischer 
29987f6c662SJulian Elischer 
3006f4e0bebSPoul-Henning Kamp static struct isa_device *fdcdevs[NFDC];
30192200632SGarrett Wollman 
3023e425b96SJulian Elischer 
303dc5df763SJoerg Wunsch static int
304dc5df763SJoerg Wunsch fdc_err(fdcu_t fdcu, const char *s)
305dc5df763SJoerg Wunsch {
306dc5df763SJoerg Wunsch 	fdc_data[fdcu].fdc_errs++;
30716b04b6aSJoerg Wunsch 	if(s) {
308dc5df763SJoerg Wunsch 		if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX)
3096a0e6f42SRodney W. Grimes 			printf("fdc%d: %s", fdcu, s);
310dc5df763SJoerg Wunsch 		else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX)
311dc5df763SJoerg Wunsch 			printf("fdc%d: too many errors, not logging any more\n",
312dc5df763SJoerg Wunsch 			       fdcu);
31316b04b6aSJoerg Wunsch 	}
314dc5df763SJoerg Wunsch 
315dc5df763SJoerg Wunsch 	return FD_FAILED;
316dc5df763SJoerg Wunsch }
317dc5df763SJoerg Wunsch 
318dc5df763SJoerg Wunsch /*
319dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
320dc5df763SJoerg Wunsch  * Unit number,
321dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
322dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
323dc5df763SJoerg Wunsch  */
324dc5df763SJoerg Wunsch 
3256f4e0bebSPoul-Henning Kamp static int
326dc5df763SJoerg Wunsch fd_cmd(fdcu_t fdcu, int n_out, ...)
327dc5df763SJoerg Wunsch {
328dc5df763SJoerg Wunsch 	u_char cmd;
329dc5df763SJoerg Wunsch 	int n_in;
330dc5df763SJoerg Wunsch 	int n;
331dc5df763SJoerg Wunsch 	va_list ap;
332dc5df763SJoerg Wunsch 
333dc5df763SJoerg Wunsch 	va_start(ap, n_out);
334dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
335dc5df763SJoerg Wunsch 	va_end(ap);
336dc5df763SJoerg Wunsch 	va_start(ap, n_out);
337dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
338dc5df763SJoerg Wunsch 	{
339dc5df763SJoerg Wunsch 		if (out_fdc(fdcu, va_arg(ap, int)) < 0)
340dc5df763SJoerg Wunsch 		{
341dc5df763SJoerg Wunsch 			char msg[50];
342dc5df763SJoerg Wunsch 			sprintf(msg,
343dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
344dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
345dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
346dc5df763SJoerg Wunsch 		}
347dc5df763SJoerg Wunsch 	}
348dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
349dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
350dc5df763SJoerg Wunsch 	{
351dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
352dc5df763SJoerg Wunsch 		if (fd_in(fdcu, ptr) < 0)
353dc5df763SJoerg Wunsch 		{
354dc5df763SJoerg Wunsch 			char msg[50];
355dc5df763SJoerg Wunsch 			sprintf(msg,
356dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
357dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
358dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
359dc5df763SJoerg Wunsch 		}
360dc5df763SJoerg Wunsch 	}
361dc5df763SJoerg Wunsch 
362dc5df763SJoerg Wunsch 	return 0;
363dc5df763SJoerg Wunsch }
364dc5df763SJoerg Wunsch 
3656f4e0bebSPoul-Henning Kamp static int
366d66c374fSTor Egge enable_fifo(fdc_p fdc)
367d66c374fSTor Egge {
368d66c374fSTor Egge 	int i, j;
369d66c374fSTor Egge 
370d66c374fSTor Egge 	if ((fdc->flags & FDC_HAS_FIFO) == 0) {
371d66c374fSTor Egge 
372d66c374fSTor Egge 		/*
373d66c374fSTor Egge 		 * XXX:
374d66c374fSTor Egge 		 * Cannot use fd_cmd the normal way here, since
375d66c374fSTor Egge 		 * this might be an invalid command. Thus we send the
376d66c374fSTor Egge 		 * first byte, and check for an early turn of data directon.
377d66c374fSTor Egge 		 */
378d66c374fSTor Egge 
379d66c374fSTor Egge 		if (out_fdc(fdc->fdcu, I8207X_CONFIGURE) < 0)
380d66c374fSTor Egge 			return fdc_err(fdc->fdcu, "Enable FIFO failed\n");
381d66c374fSTor Egge 
382d66c374fSTor Egge 		/* If command is invalid, return */
383d66c374fSTor Egge 		j = 100000;
384d66c374fSTor Egge 		while ((i = inb(fdc->baseport + FDSTS) & (NE7_DIO | NE7_RQM))
385d66c374fSTor Egge 		       != NE7_RQM && j-- > 0)
386d66c374fSTor Egge 			if (i == (NE7_DIO | NE7_RQM)) {
387d66c374fSTor Egge 				fdc_reset(fdc);
388d66c374fSTor Egge 				return FD_FAILED;
389d66c374fSTor Egge 			}
390d66c374fSTor Egge 		if (j<0 ||
391d66c374fSTor Egge 		    fd_cmd(fdc->fdcu, 3,
392d66c374fSTor Egge 			   0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
393d66c374fSTor Egge 			fdc_reset(fdc);
394d66c374fSTor Egge 			return fdc_err(fdc->fdcu, "Enable FIFO failed\n");
395d66c374fSTor Egge 		}
396d66c374fSTor Egge 		fdc->flags |= FDC_HAS_FIFO;
397d66c374fSTor Egge 		return 0;
398d66c374fSTor Egge 	}
399d66c374fSTor Egge 	if (fd_cmd(fdc->fdcu, 4,
400d66c374fSTor Egge 		   I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
401d66c374fSTor Egge 		return fdc_err(fdc->fdcu, "Re-enable FIFO failed\n");
402d66c374fSTor Egge 	return 0;
403d66c374fSTor Egge }
404d66c374fSTor Egge 
405d66c374fSTor Egge static int
406dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
407dc5df763SJoerg Wunsch {
408dc5df763SJoerg Wunsch 	int st3;
409dc5df763SJoerg Wunsch 
410dc5df763SJoerg Wunsch 	if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
411dc5df763SJoerg Wunsch 	{
4126a0e6f42SRodney W. Grimes 		return fdc_err(fdc->fdcu, "Sense Drive Status failed\n");
413dc5df763SJoerg Wunsch 	}
414dc5df763SJoerg Wunsch 	if (st3p)
415dc5df763SJoerg Wunsch 		*st3p = st3;
416dc5df763SJoerg Wunsch 
417dc5df763SJoerg Wunsch 	return 0;
418dc5df763SJoerg Wunsch }
419dc5df763SJoerg Wunsch 
4206f4e0bebSPoul-Henning Kamp static int
421dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
422dc5df763SJoerg Wunsch {
423dc5df763SJoerg Wunsch 	int st0, cyl;
424dc5df763SJoerg Wunsch 
425dc5df763SJoerg Wunsch 	int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0);
426dc5df763SJoerg Wunsch 
427dc5df763SJoerg Wunsch 	if (ret)
428dc5df763SJoerg Wunsch 	{
429dc5df763SJoerg Wunsch 		(void)fdc_err(fdc->fdcu,
430dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
431dc5df763SJoerg Wunsch 		return ret;
432dc5df763SJoerg Wunsch 	}
433dc5df763SJoerg Wunsch 
434dc5df763SJoerg Wunsch 	if (st0p)
435dc5df763SJoerg Wunsch 		*st0p = st0;
436dc5df763SJoerg Wunsch 
437dc5df763SJoerg Wunsch 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV)
438dc5df763SJoerg Wunsch 	{
439dc5df763SJoerg Wunsch 		/*
440dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
441dc5df763SJoerg Wunsch 		 */
442dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
443dc5df763SJoerg Wunsch 	}
444dc5df763SJoerg Wunsch 
445dc5df763SJoerg Wunsch 	if (fd_in(fdc->fdcu, &cyl) < 0)
446dc5df763SJoerg Wunsch 	{
447dc5df763SJoerg Wunsch 		return fdc_err(fdc->fdcu, "can't get cyl num\n");
448dc5df763SJoerg Wunsch 	}
449dc5df763SJoerg Wunsch 
450dc5df763SJoerg Wunsch 	if (cylp)
451dc5df763SJoerg Wunsch 		*cylp = cyl;
452dc5df763SJoerg Wunsch 
453dc5df763SJoerg Wunsch 	return 0;
454dc5df763SJoerg Wunsch }
455dc5df763SJoerg Wunsch 
456dc5df763SJoerg Wunsch 
4576f4e0bebSPoul-Henning Kamp static int
458dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu)
459dc5df763SJoerg Wunsch {
460dc5df763SJoerg Wunsch 	int i, ret;
461b5e8ce9fSBruce Evans 
462dc5df763SJoerg Wunsch 	for (i = 0; i < 7; i++)
463dc5df763SJoerg Wunsch 	{
464b5e8ce9fSBruce Evans 		/*
465b5e8ce9fSBruce Evans 		 * XXX types are poorly chosen.  Only bytes can by read
466a838d83dSBruce Evans 		 * from the hardware, but fdc->status[] wants u_ints and
467b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
468b5e8ce9fSBruce Evans 		 */
469b5e8ce9fSBruce Evans 		int status;
470b5e8ce9fSBruce Evans 
471b5e8ce9fSBruce Evans 		ret = fd_in(fdc->fdcu, &status);
472b5e8ce9fSBruce Evans 		fdc->status[i] = status;
473b5e8ce9fSBruce Evans 		if (ret != 0)
474dc5df763SJoerg Wunsch 			break;
475dc5df763SJoerg Wunsch 	}
476dc5df763SJoerg Wunsch 
477dc5df763SJoerg Wunsch 	if (ret == 0)
478dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
479dc5df763SJoerg Wunsch 	else
480dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
481dc5df763SJoerg Wunsch 
482dc5df763SJoerg Wunsch 	return ret;
483dc5df763SJoerg Wunsch }
484dc5df763SJoerg Wunsch 
4855b81b6b3SRodney W. Grimes /****************************************************************************/
4865b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
4875b81b6b3SRodney W. Grimes /****************************************************************************/
488dc5df763SJoerg Wunsch 
4895b81b6b3SRodney W. Grimes /*
4905b81b6b3SRodney W. Grimes  * probe for existance of controller
4915b81b6b3SRodney W. Grimes  */
4923a2f7427SDavid Greenman static int
493dc5df763SJoerg Wunsch fdprobe(struct isa_device *dev)
4945b81b6b3SRodney W. Grimes {
4955b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
4965b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].flags & FDC_ATTACHED)
4975b81b6b3SRodney W. Grimes 	{
4986a0e6f42SRodney W. Grimes 		printf("fdc%d: unit used multiple times\n", fdcu);
4995b81b6b3SRodney W. Grimes 		return 0;
5005b81b6b3SRodney W. Grimes 	}
5015b81b6b3SRodney W. Grimes 
50292200632SGarrett Wollman 	fdcdevs[fdcu] = dev;
5035b81b6b3SRodney W. Grimes 	fdc_data[fdcu].baseport = dev->id_iobase;
5045b81b6b3SRodney W. Grimes 
50516111cedSAndrew Moore 	/* First - lets reset the floppy controller */
5063a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, 0);
50716111cedSAndrew Moore 	DELAY(100);
5083a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, FDO_FRST);
50916111cedSAndrew Moore 
5105b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
511dc5df763SJoerg Wunsch 	if (fd_cmd(fdcu,
512dc5df763SJoerg Wunsch 		   3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
513dc5df763SJoerg Wunsch 		   0))
5145b81b6b3SRodney W. Grimes 	{
5155b81b6b3SRodney W. Grimes 		return(0);
5165b81b6b3SRodney W. Grimes 	}
5175b81b6b3SRodney W. Grimes 	return (IO_FDCSIZE);
5185b81b6b3SRodney W. Grimes }
5195b81b6b3SRodney W. Grimes 
5205b81b6b3SRodney W. Grimes /*
5215b81b6b3SRodney W. Grimes  * wire controller into system, look for floppy units
5225b81b6b3SRodney W. Grimes  */
5233a2f7427SDavid Greenman static int
524dc5df763SJoerg Wunsch fdattach(struct isa_device *dev)
5255b81b6b3SRodney W. Grimes {
5263a2f7427SDavid Greenman 	unsigned fdt;
5275b81b6b3SRodney W. Grimes 	fdu_t	fdu;
5285b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5295b81b6b3SRodney W. Grimes 	fdc_p	fdc = fdc_data + fdcu;
5305b81b6b3SRodney W. Grimes 	fd_p	fd;
531cba2a7c6SBruce Evans 	int	fdsu, st0, st3, i;
532cba2a7c6SBruce Evans #if NFT > 0
533cba2a7c6SBruce Evans 	int	unithasfd;
534cba2a7c6SBruce Evans #endif
535b99f0a4aSAndrew Moore 	struct isa_device *fdup;
536dc5df763SJoerg Wunsch 	int ic_type = 0;
537999422d7SJulian Elischer #ifdef DEVFS
538c8f2fe8dSBruce Evans 	int	mynor;
53921519754SBruce Evans 	int	typemynor;
54021519754SBruce Evans 	int	typesize;
54121519754SBruce Evans #endif
54292200632SGarrett Wollman 
5435b81b6b3SRodney W. Grimes 	fdc->fdcu = fdcu;
5445b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
5455b81b6b3SRodney W. Grimes 	fdc->dmachan = dev->id_drq;
546100f78bbSSujal Patel 	/* Acquire the DMA channel forever, The driver will do the rest */
547100f78bbSSujal Patel 	isa_dma_acquire(fdc->dmachan);
548dd87702aSBruce Evans 	isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
5495b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
5503a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
5513a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
55202a19910SJustin T. Gibbs 	bufq_init(&fdc->head);
5535b81b6b3SRodney W. Grimes 
5545b81b6b3SRodney W. Grimes 	/* check for each floppy drive */
555b99f0a4aSAndrew Moore 	for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
556b99f0a4aSAndrew Moore 		if (fdup->id_iobase != dev->id_iobase)
557b99f0a4aSAndrew Moore 			continue;
558b99f0a4aSAndrew Moore 		fdu = fdup->id_unit;
559b99f0a4aSAndrew Moore 		fd = &fd_data[fdu];
560b99f0a4aSAndrew Moore 		if (fdu >= (NFD+NFT))
561b99f0a4aSAndrew Moore 			continue;
562b99f0a4aSAndrew Moore 		fdsu = fdup->id_physid;
563b99f0a4aSAndrew Moore 		/* look up what bios thinks we have */
564b99f0a4aSAndrew Moore 		switch (fdu) {
5650722d6abSJoerg Wunsch 			case 0: if (dev->id_flags & FDC_PRETEND_D0)
5660722d6abSJoerg Wunsch 					fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
5670722d6abSJoerg Wunsch 				else
5680722d6abSJoerg Wunsch 					fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
569b99f0a4aSAndrew Moore 				break;
570b99f0a4aSAndrew Moore 			case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
571b99f0a4aSAndrew Moore 				break;
572b99f0a4aSAndrew Moore 			default: fdt = RTCFDT_NONE;
573b99f0a4aSAndrew Moore 				break;
574b99f0a4aSAndrew Moore 		}
5755b81b6b3SRodney W. Grimes 		/* is there a unit? */
576b99f0a4aSAndrew Moore 		if ((fdt == RTCFDT_NONE)
577b99f0a4aSAndrew Moore #if NFT > 0
578b99f0a4aSAndrew Moore 		    || (fdsu >= DRVS_PER_CTLR)) {
579b99f0a4aSAndrew Moore #else
580b99f0a4aSAndrew Moore 		) {
58156ef0285SAndrew Moore 			fd->type = NO_TYPE;
582b99f0a4aSAndrew Moore #endif
583b99f0a4aSAndrew Moore #if NFT > 0
584b99f0a4aSAndrew Moore 			/* If BIOS says no floppy, or > 2nd device */
585b99f0a4aSAndrew Moore 			/* Probe for and attach a floppy tape.     */
58674fa89f4SRodney W. Grimes 			/* Tell FT if there was already a disk     */
58774fa89f4SRodney W. Grimes 			/* with this unit number found.            */
58874fa89f4SRodney W. Grimes 
58974fa89f4SRodney W. Grimes 			unithasfd = 0;
59074fa89f4SRodney W. Grimes 			if (fdu < NFD && fd->type != NO_TYPE)
59174fa89f4SRodney W. Grimes 				unithasfd = 1;
59285827d9cSJoerg Wunsch 			if (ftattach(dev, fdup, unithasfd))
593b99f0a4aSAndrew Moore 				continue;
59456ef0285SAndrew Moore 			if (fdsu < DRVS_PER_CTLR)
595b99f0a4aSAndrew Moore 				fd->type = NO_TYPE;
59656ef0285SAndrew Moore #endif
5975b81b6b3SRodney W. Grimes 			continue;
598f5f7ba03SJordan K. Hubbard 		}
5995b81b6b3SRodney W. Grimes 
6005b81b6b3SRodney W. Grimes 		/* select it */
6013a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNON);
6026b7bd95bSJoerg Wunsch 		DELAY(1000000);	/* 1 sec */
603dc5df763SJoerg Wunsch 
604dc5df763SJoerg Wunsch 		if (ic_type == 0 &&
605dc5df763SJoerg Wunsch 		    fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0)
606dc5df763SJoerg Wunsch 		{
607d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6086a0e6f42SRodney W. Grimes 			printf("fdc%d: ", fdcu);
609d2fb4892SJoerg Wunsch #endif
610dc5df763SJoerg Wunsch 			ic_type = (u_char)ic_type;
611dc5df763SJoerg Wunsch 			switch( ic_type ) {
612dc5df763SJoerg Wunsch 			case 0x80:
613d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6146a0e6f42SRodney W. Grimes 				printf("NEC 765\n");
615d2fb4892SJoerg Wunsch #endif
616dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE765;
617dc5df763SJoerg Wunsch 				break;
618dc5df763SJoerg Wunsch 			case 0x81:
619d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6206a0e6f42SRodney W. Grimes 				printf("Intel 82077\n");
621d2fb4892SJoerg Wunsch #endif
622dc5df763SJoerg Wunsch 				fdc->fdct = FDC_I82077;
623dc5df763SJoerg Wunsch 				break;
624dc5df763SJoerg Wunsch 			case 0x90:
625d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6266a0e6f42SRodney W. Grimes 				printf("NEC 72065B\n");
627d2fb4892SJoerg Wunsch #endif
628dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE72065;
629dc5df763SJoerg Wunsch 				break;
630dc5df763SJoerg Wunsch 			default:
631d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6326a0e6f42SRodney W. Grimes 				printf("unknown IC type %02x\n", ic_type);
633d2fb4892SJoerg Wunsch #endif
634dc5df763SJoerg Wunsch 				fdc->fdct = FDC_UNKNOWN;
635dc5df763SJoerg Wunsch 				break;
6366b7bd95bSJoerg Wunsch 			}
637d66c374fSTor Egge 			if (fdc->fdct != FDC_NE765 &&
638d66c374fSTor Egge 			    fdc->fdct != FDC_UNKNOWN &&
639d66c374fSTor Egge 			    enable_fifo(fdc) == 0) {
640d66c374fSTor Egge 				printf("fdc%d: FIFO enabled", fdcu);
641d66c374fSTor Egge 				printf(", %d bytes threshold\n",
642d66c374fSTor Egge 				       fifo_threshold);
643d66c374fSTor Egge 			}
644dc5df763SJoerg Wunsch 		}
645dc5df763SJoerg Wunsch 		if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) &&
646dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
647dc5df763SJoerg Wunsch 			/* if at track 0, first seek inwards */
648dc5df763SJoerg Wunsch 			/* seek some steps: */
649dc5df763SJoerg Wunsch 			(void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0);
650dc5df763SJoerg Wunsch 			DELAY(300000); /* ...wait a moment... */
651dc5df763SJoerg Wunsch 			(void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
652dc5df763SJoerg Wunsch 		}
653dc5df763SJoerg Wunsch 
654dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
655dc5df763SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
656dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
657dc5df763SJoerg Wunsch 			/* Seek some steps... */
658dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
659dc5df763SJoerg Wunsch 				/* ...wait a moment... */
660dc5df763SJoerg Wunsch 				DELAY(300000);
661dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
662dc5df763SJoerg Wunsch 				(void)fd_sense_int(fdc, 0, 0);
663dc5df763SJoerg Wunsch 			}
664dc5df763SJoerg Wunsch 		}
665dc5df763SJoerg Wunsch 
6666b7bd95bSJoerg Wunsch 		for(i = 0; i < 2; i++) {
6676b7bd95bSJoerg Wunsch 			/*
6686b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
6696b7bd95bSJoerg Wunsch 			 * heads have been beyond cylinder 76, since most
6706b7bd95bSJoerg Wunsch 			 * FDCs still barf when attempting to recalibrate
6716b7bd95bSJoerg Wunsch 			 * more than 77 steps
6726b7bd95bSJoerg Wunsch 			 */
673dc5df763SJoerg Wunsch 			/* go back to 0: */
674dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
6756b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
6766b7bd95bSJoerg Wunsch 				DELAY(i == 0? 1000000: 300000);
6775b81b6b3SRodney W. Grimes 
6786b7bd95bSJoerg Wunsch 				/* anything responding? */
679dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
680dc5df763SJoerg Wunsch 				(st0 & NE7_ST0_EC) == 0)
6816b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
6826b7bd95bSJoerg Wunsch 			}
683dc5df763SJoerg Wunsch 		}
6846b7bd95bSJoerg Wunsch 
6853a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNOFF);
6863a2f7427SDavid Greenman 
6873a2f7427SDavid Greenman 		if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
6885b81b6b3SRodney W. Grimes 			continue;
6895b81b6b3SRodney W. Grimes 
690dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
691b99f0a4aSAndrew Moore 		fd->fdc = fdc;
692b99f0a4aSAndrew Moore 		fd->fdsu = fdsu;
6933a2f7427SDavid Greenman 		fd->options = 0;
69402a19910SJustin T. Gibbs 		callout_handle_init(&fd->toffhandle);
69502a19910SJustin T. Gibbs 		callout_handle_init(&fd->tohandle);
6962d9d0204SRodney W. Grimes 		printf("fd%d: ", fdu);
6975b81b6b3SRodney W. Grimes 
698b99f0a4aSAndrew Moore 		switch (fdt) {
6997ca0641bSAndrey A. Chernov 		case RTCFDT_12M:
7006a0e6f42SRodney W. Grimes 			printf("1.2MB 5.25in\n");
701b99f0a4aSAndrew Moore 			fd->type = FD_1200;
7027ca0641bSAndrey A. Chernov 			break;
7030722d6abSJoerg Wunsch 		case RTCFDT_144M | RTCFDT_144M_PRETENDED:
7040722d6abSJoerg Wunsch 			printf("config-pretended ");
7050722d6abSJoerg Wunsch 			fdt = RTCFDT_144M;
7060722d6abSJoerg Wunsch 			/* fallthrough */
7077ca0641bSAndrey A. Chernov 		case RTCFDT_144M:
7086a0e6f42SRodney W. Grimes 			printf("1.44MB 3.5in\n");
709b99f0a4aSAndrew Moore 			fd->type = FD_1440;
7107ca0641bSAndrey A. Chernov 			break;
711290dd077SJoerg Wunsch 		case RTCFDT_288M:
71286a727d9SJoerg Wunsch 		case RTCFDT_288M_1:
7136a0e6f42SRodney W. Grimes 			printf("2.88MB 3.5in - 1.44MB mode\n");
714290dd077SJoerg Wunsch 			fd->type = FD_1440;
715290dd077SJoerg Wunsch 			break;
7167ca0641bSAndrey A. Chernov 		case RTCFDT_360K:
7176a0e6f42SRodney W. Grimes 			printf("360KB 5.25in\n");
718b99f0a4aSAndrew Moore 			fd->type = FD_360;
7197ca0641bSAndrey A. Chernov 			break;
720ed2fa05eSAndrey A. Chernov 		case RTCFDT_720K:
7216a0e6f42SRodney W. Grimes 			printf("720KB 3.5in\n");
722b99f0a4aSAndrew Moore 			fd->type = FD_720;
723ed2fa05eSAndrey A. Chernov 			break;
7247ca0641bSAndrey A. Chernov 		default:
7256a0e6f42SRodney W. Grimes 			printf("unknown\n");
726b99f0a4aSAndrew Moore 			fd->type = NO_TYPE;
72721519754SBruce Evans 			continue;
7285b81b6b3SRodney W. Grimes 		}
729999422d7SJulian Elischer #ifdef DEVFS
73021519754SBruce Evans 		mynor = fdu << 6;
731f7ea2f55SJulian Elischer 		fd->bdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_BLK,
732f85120acSBruce Evans 						UID_ROOT, GID_OPERATOR, 0640,
73321519754SBruce Evans 						"fd%d", fdu);
73421519754SBruce Evans 		fd->cdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_CHR,
735f85120acSBruce Evans 						UID_ROOT, GID_OPERATOR, 0640,
73621519754SBruce Evans 						"rfd%d", fdu);
73721519754SBruce Evans 		for (i = 1; i < 1 + NUMDENS; i++) {
73821519754SBruce Evans 			/*
73921519754SBruce Evans 			 * XXX this and the lookup in Fdopen() should be
74021519754SBruce Evans 			 * data driven.
74121519754SBruce Evans 			 */
74221519754SBruce Evans 			switch (fd->type) {
74321519754SBruce Evans 			case FD_360:
74421519754SBruce Evans 				if (i != FD_360)
74521519754SBruce Evans 					continue;
74621519754SBruce Evans 				break;
74721519754SBruce Evans 			case FD_720:
74821519754SBruce Evans 				if (i != FD_720 && i != FD_800 && i != FD_820)
74921519754SBruce Evans 					continue;
75021519754SBruce Evans 				break;
75121519754SBruce Evans 			case FD_1200:
75221519754SBruce Evans 				if (i != FD_360 && i != FD_720 && i != FD_800
75321519754SBruce Evans 				    && i != FD_820 && i != FD_1200
75421519754SBruce Evans 				    && i != FD_1440 && i != FD_1480)
75521519754SBruce Evans 					continue;
75621519754SBruce Evans 				break;
75721519754SBruce Evans 			case FD_1440:
75821519754SBruce Evans 				if (i != FD_720 && i != FD_800 && i != FD_820
75921519754SBruce Evans 				    && i != FD_1200 && i != FD_1440
76021519754SBruce Evans 				    && i != FD_1480 && i != FD_1720)
76121519754SBruce Evans 					continue;
76221519754SBruce Evans 				break;
76321519754SBruce Evans 			}
76421519754SBruce Evans 			typesize = fd_types[i - 1].size / 2;
76521519754SBruce Evans 			/*
76621519754SBruce Evans 			 * XXX all these conversions give bloated code and
76721519754SBruce Evans 			 * confusing names.
76821519754SBruce Evans 			 */
76921519754SBruce Evans 			if (typesize == 1476)
77021519754SBruce Evans 				typesize = 1480;
77121519754SBruce Evans 			if (typesize == 1722)
77221519754SBruce Evans 				typesize = 1720;
7733e425b96SJulian Elischer 			typemynor = mynor | i;
77421519754SBruce Evans 			fd->bdevs[i] =
775f7ea2f55SJulian Elischer 				devfs_add_devswf(&fd_cdevsw, typemynor, DV_BLK,
77621519754SBruce Evans 						 UID_ROOT, GID_OPERATOR, 0640,
77721519754SBruce Evans 						 "fd%d.%d", fdu, typesize);
77821519754SBruce Evans 			fd->cdevs[i] =
77921519754SBruce Evans 				devfs_add_devswf(&fd_cdevsw, typemynor, DV_CHR,
78021519754SBruce Evans 						 UID_ROOT, GID_OPERATOR, 0640,
78121519754SBruce Evans 						 "rfd%d.%d", fdu, typesize);
78221519754SBruce Evans 		}
7833e425b96SJulian Elischer 
78421519754SBruce Evans 		for (i = 0; i < MAXPARTITIONS; i++) {
7853e425b96SJulian Elischer 			fd->bdevs[1 + NUMDENS + i] = devfs_link(fd->bdevs[0],
78621519754SBruce Evans 					   "fd%d%c", fdu, 'a' + i);
78721519754SBruce Evans 			fd->cdevs[1 + NUMDENS + i] =
78821519754SBruce Evans 				devfs_link(fd->cdevs[0],
78921519754SBruce Evans 					   "rfd%d%c", fdu, 'a' + i);
79021519754SBruce Evans 		}
791999422d7SJulian Elischer #endif /* DEVFS */
792671e2ceeSBruce Evans 		/*
793b2dfb1f9SJustin T. Gibbs 		 * Export the drive to the devstat interface.
794671e2ceeSBruce Evans 		 */
795b2dfb1f9SJustin T. Gibbs 		devstat_add_entry(&fd->device_stats, "fd",
796b2dfb1f9SJustin T. Gibbs 				  fdu, 512,
797b2dfb1f9SJustin T. Gibbs 				  DEVSTAT_NO_ORDERED_TAGS,
798b2dfb1f9SJustin T. Gibbs 				  DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER);
799b2dfb1f9SJustin T. Gibbs 
8005b81b6b3SRodney W. Grimes 	}
8015b81b6b3SRodney W. Grimes 
8023a2f7427SDavid Greenman 	return (1);
8035b81b6b3SRodney W. Grimes }
8045b81b6b3SRodney W. Grimes 
8053e425b96SJulian Elischer 
8063e425b96SJulian Elischer 
8075b81b6b3SRodney W. Grimes /****************************************************************************/
8085b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
8095e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
8105b81b6b3SRodney W. Grimes /****************************************************************************/
8113a2f7427SDavid Greenman static void
812dc5df763SJoerg Wunsch set_motor(fdcu_t fdcu, int fdsu, int turnon)
8135b81b6b3SRodney W. Grimes {
8143a2f7427SDavid Greenman 	int fdout = fdc_data[fdcu].fdout;
8153a2f7427SDavid Greenman 	int needspecify = 0;
8163a2f7427SDavid Greenman 
8173a2f7427SDavid Greenman 	if(turnon) {
8183a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
8193a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
8203a2f7427SDavid Greenman 	} else
8213a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
8223a2f7427SDavid Greenman 
8235e235068SJordan K. Hubbard 	if(!turnon
8245e235068SJordan K. Hubbard 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
8255e235068SJordan K. Hubbard 		/* gonna turn off the last drive, put FDC to bed */
8265e235068SJordan K. Hubbard 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
8275e235068SJordan K. Hubbard 	else {
8283a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
8293a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
8303a2f7427SDavid Greenman 			needspecify = 1;
8313a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
8325b81b6b3SRodney W. Grimes 	}
8335b81b6b3SRodney W. Grimes 
8343a2f7427SDavid Greenman 	outb(fdc_data[fdcu].baseport+FDOUT, fdout);
8353a2f7427SDavid Greenman 	fdc_data[fdcu].fdout = fdout;
8363a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
8373a2f7427SDavid Greenman 
8383a2f7427SDavid Greenman 	if(needspecify) {
839dc8603e3SJoerg Wunsch 		/*
840dc5df763SJoerg Wunsch 		 * XXX
841dc8603e3SJoerg Wunsch 		 * special case: since we have just woken up the FDC
842dc8603e3SJoerg Wunsch 		 * from its sleep, we silently assume the command will
843dc8603e3SJoerg Wunsch 		 * be accepted, and do not test for a timeout
844dc8603e3SJoerg Wunsch 		 */
845dc5df763SJoerg Wunsch 		(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
846dc5df763SJoerg Wunsch 			     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
847dc5df763SJoerg Wunsch 			     0);
848d66c374fSTor Egge 		if (fdc_data[fdcu].flags & FDC_HAS_FIFO)
849d66c374fSTor Egge 			(void) enable_fifo(&fdc_data[fdcu]);
8503a2f7427SDavid Greenman 	}
8513a2f7427SDavid Greenman }
8523a2f7427SDavid Greenman 
853381fe1aaSGarrett Wollman static void
854d0917939SPaul Richards fd_turnoff(void *arg1)
8555b81b6b3SRodney W. Grimes {
856381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
857f5f7ba03SJordan K. Hubbard 	int	s;
8585b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
859dc16046fSJoerg Wunsch 
860dc16046fSJoerg Wunsch 	TRACE1("[fd%d: turnoff]", fdu);
8618335c1b8SBruce Evans 
8628335c1b8SBruce Evans 	/*
8638335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
8648335c1b8SBruce Evans 	 * XXX shouldn't even schedule turnoff until drive is inactive
8658335c1b8SBruce Evans 	 * and nothing is queued on it.
8668335c1b8SBruce Evans 	 */
8678335c1b8SBruce Evans 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fdu) {
86802a19910SJustin T. Gibbs 		fd->toffhandle = timeout(fd_turnoff, arg1, 4 * hz);
8698335c1b8SBruce Evans 		return;
8708335c1b8SBruce Evans 	}
8718335c1b8SBruce Evans 
872f5f7ba03SJordan K. Hubbard 	s = splbio();
8735b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
8743a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
875f5f7ba03SJordan K. Hubbard 	splx(s);
8765b81b6b3SRodney W. Grimes }
8775b81b6b3SRodney W. Grimes 
8783a2f7427SDavid Greenman static void
879d0917939SPaul Richards fd_motor_on(void *arg1)
8805b81b6b3SRodney W. Grimes {
881381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
882f5f7ba03SJordan K. Hubbard 	int	s;
883f5f7ba03SJordan K. Hubbard 
8845b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
885f5f7ba03SJordan K. Hubbard 	s = splbio();
8865b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
8875b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
8885b81b6b3SRodney W. Grimes 	{
889f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
8905b81b6b3SRodney W. Grimes 	}
891f5f7ba03SJordan K. Hubbard 	splx(s);
8925b81b6b3SRodney W. Grimes }
8935b81b6b3SRodney W. Grimes 
8943a2f7427SDavid Greenman static void
895dc5df763SJoerg Wunsch fd_turnon(fdu_t fdu)
8965b81b6b3SRodney W. Grimes {
8975b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
8985b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
8995b81b6b3SRodney W. Grimes 	{
9003a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
9013a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
9025e235068SJordan K. Hubbard 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
9035b81b6b3SRodney W. Grimes 	}
9045b81b6b3SRodney W. Grimes }
9055b81b6b3SRodney W. Grimes 
906381fe1aaSGarrett Wollman static void
907dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
9085b81b6b3SRodney W. Grimes {
9093a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
9103a2f7427SDavid Greenman 
9113a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
9123a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
9133a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
9143a2f7427SDavid Greenman 	DELAY(100);
9153a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
9163a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
9173a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
9183a2f7427SDavid Greenman 	DELAY(100);
9193a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
9203a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
9213a2f7427SDavid Greenman 
922dc5df763SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
923dc5df763SJoerg Wunsch 	(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
924dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
925dc5df763SJoerg Wunsch 		     0);
926d66c374fSTor Egge 	if (fdc->flags & FDC_HAS_FIFO)
927d66c374fSTor Egge 		(void) enable_fifo(fdc);
9285b81b6b3SRodney W. Grimes }
9295b81b6b3SRodney W. Grimes 
9305b81b6b3SRodney W. Grimes /****************************************************************************/
9315b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
9325b81b6b3SRodney W. Grimes /****************************************************************************/
9335b81b6b3SRodney W. Grimes int
934dc5df763SJoerg Wunsch in_fdc(fdcu_t fdcu)
9355b81b6b3SRodney W. Grimes {
9365b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
9375b81b6b3SRodney W. Grimes 	int i, j = 100000;
9383a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
9395b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
940dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
9416a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
9425b81b6b3SRodney W. Grimes 	if (j <= 0)
94316b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
944d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
9453a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
9463a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
9475b81b6b3SRodney W. Grimes 	return(i);
948d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
9493a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
950d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
9515b81b6b3SRodney W. Grimes }
9525b81b6b3SRodney W. Grimes 
953dc5df763SJoerg Wunsch /*
954dc5df763SJoerg Wunsch  * fd_in: Like in_fdc, but allows you to see if it worked.
955dc5df763SJoerg Wunsch  */
956b5e8ce9fSBruce Evans static int
957dc5df763SJoerg Wunsch fd_in(fdcu_t fdcu, int *ptr)
958dc5df763SJoerg Wunsch {
959dc5df763SJoerg Wunsch 	int baseport = fdc_data[fdcu].baseport;
960dc5df763SJoerg Wunsch 	int i, j = 100000;
961dc5df763SJoerg Wunsch 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
962dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
963dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
9646a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
965dc5df763SJoerg Wunsch 	if (j <= 0)
96616b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
967d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
968dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
969dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
970dc5df763SJoerg Wunsch 	*ptr = i;
971dc5df763SJoerg Wunsch 	return 0;
972d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
973dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
974dc5df763SJoerg Wunsch 	if (ptr)
975dc5df763SJoerg Wunsch 		*ptr = i;
976dc5df763SJoerg Wunsch 	return 0;
977d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
978dc5df763SJoerg Wunsch }
979dc5df763SJoerg Wunsch 
980dc5df763SJoerg Wunsch int
981dc5df763SJoerg Wunsch out_fdc(fdcu_t fdcu, int x)
9825b81b6b3SRodney W. Grimes {
9835b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
9843b3837dbSRodney W. Grimes 	int i;
9855b81b6b3SRodney W. Grimes 
9863b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
9873b3837dbSRodney W. Grimes 	i = 100000;
9883a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
9896a0e6f42SRodney W. Grimes 	if (i <= 0) return fdc_err(fdcu, "direction bit not set\n");
9903b3837dbSRodney W. Grimes 
9913b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
9923b3837dbSRodney W. Grimes 	i = 100000;
9933a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
99416b04b6aSJoerg Wunsch 	if (i <= 0)
99516b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "output ready timeout\n": 0);
9963b3837dbSRodney W. Grimes 
9973b3837dbSRodney W. Grimes 	/* Send the command and return */
9983a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
9993a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
10005b81b6b3SRodney W. Grimes 	return (0);
10015b81b6b3SRodney W. Grimes }
10025b81b6b3SRodney W. Grimes 
10035b81b6b3SRodney W. Grimes /****************************************************************************/
10045b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
10055b81b6b3SRodney W. Grimes /****************************************************************************/
1006381fe1aaSGarrett Wollman int
1007671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p)
10085b81b6b3SRodney W. Grimes {
10095b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
101020a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
1011b99f0a4aSAndrew Moore 	fdc_p	fdc;
10125b81b6b3SRodney W. Grimes 
1013b99f0a4aSAndrew Moore #if NFT > 0
1014b99f0a4aSAndrew Moore 	/* check for a tape open */
1015b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
1016b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
1017b99f0a4aSAndrew Moore #endif
10185b81b6b3SRodney W. Grimes 	/* check bounds */
1019b99f0a4aSAndrew Moore 	if (fdu >= NFD)
1020b99f0a4aSAndrew Moore 		return(ENXIO);
1021b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
1022b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
1023b99f0a4aSAndrew Moore 		return(ENXIO);
1024b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1025b99f0a4aSAndrew Moore 		return(ENXIO);
10267ca0641bSAndrey A. Chernov 	if (type == 0)
10277ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
10287ca0641bSAndrey A. Chernov 	else {
10293e425b96SJulian Elischer 		/*
10303e425b96SJulian Elischer 		 * For each type of basic drive, make sure we are trying
10313e425b96SJulian Elischer 		 * to open a type it can do,
10323e425b96SJulian Elischer 		 */
10337ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
1034fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
10357ca0641bSAndrey A. Chernov 			case FD_360:
10367ca0641bSAndrey A. Chernov 				return(ENXIO);
1037ed2fa05eSAndrey A. Chernov 			case FD_720:
1038b39c878eSAndrey A. Chernov 				if (   type != FD_820
1039b39c878eSAndrey A. Chernov 				    && type != FD_800
1040ed2fa05eSAndrey A. Chernov 				   )
1041ed2fa05eSAndrey A. Chernov 					return(ENXIO);
1042ed2fa05eSAndrey A. Chernov 				break;
10437ca0641bSAndrey A. Chernov 			case FD_1200:
1044b39c878eSAndrey A. Chernov 				switch (type) {
1045b39c878eSAndrey A. Chernov 				case FD_1480:
1046b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
1047fa4700b4SAndrey A. Chernov 					break;
10487ca0641bSAndrey A. Chernov 				case FD_1440:
1049b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
1050b39c878eSAndrey A. Chernov 					break;
1051b39c878eSAndrey A. Chernov 				case FD_820:
1052b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
1053b39c878eSAndrey A. Chernov 					break;
1054b39c878eSAndrey A. Chernov 				case FD_800:
1055b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
1056b39c878eSAndrey A. Chernov 					break;
1057b39c878eSAndrey A. Chernov 				case FD_720:
1058b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
1059b39c878eSAndrey A. Chernov 					break;
1060b39c878eSAndrey A. Chernov 				case FD_360:
1061b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
1062b39c878eSAndrey A. Chernov 					break;
1063b39c878eSAndrey A. Chernov 				default:
1064b39c878eSAndrey A. Chernov 					return(ENXIO);
1065b39c878eSAndrey A. Chernov 				}
1066b39c878eSAndrey A. Chernov 				break;
1067b39c878eSAndrey A. Chernov 			case FD_1440:
1068b39c878eSAndrey A. Chernov 				if (   type != FD_1720
1069b39c878eSAndrey A. Chernov 				    && type != FD_1480
1070ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
1071b39c878eSAndrey A. Chernov 				    && type != FD_820
1072b39c878eSAndrey A. Chernov 				    && type != FD_800
1073b39c878eSAndrey A. Chernov 				    && type != FD_720
10747ca0641bSAndrey A. Chernov 				    )
1075dffff499SAndrey A. Chernov 					return(ENXIO);
1076fa4700b4SAndrey A. Chernov 				break;
10777ca0641bSAndrey A. Chernov 			}
10787ca0641bSAndrey A. Chernov 		}
1079fa4700b4SAndrey A. Chernov 	}
1080b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
10815b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
10825b81b6b3SRodney W. Grimes 
10835b81b6b3SRodney W. Grimes 	return 0;
10845b81b6b3SRodney W. Grimes }
10855b81b6b3SRodney W. Grimes 
1086381fe1aaSGarrett Wollman int
1087671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p)
10885b81b6b3SRodney W. Grimes {
10895b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
1090b99f0a4aSAndrew Moore 
1091b99f0a4aSAndrew Moore #if NFT > 0
10923a2f7427SDavid Greenman 	int type = FDTYPE(minor(dev));
10933a2f7427SDavid Greenman 
1094b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
10953a2f7427SDavid Greenman 		return ftclose(dev, flags);
1096b99f0a4aSAndrew Moore #endif
10975b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
10983a2f7427SDavid Greenman 	fd_data[fdu].options &= ~FDOPT_NORETRY;
1099dc16046fSJoerg Wunsch 
11005b81b6b3SRodney W. Grimes 	return(0);
11015b81b6b3SRodney W. Grimes }
11025b81b6b3SRodney W. Grimes 
1103f7ea2f55SJulian Elischer static int
1104f7ea2f55SJulian Elischer fdread(dev_t dev, struct uio *uio, int ioflag)
1105f7ea2f55SJulian Elischer {
1106f7ea2f55SJulian Elischer 	return (physio(fdstrategy, NULL, dev, 1, minphys, uio));
1107f7ea2f55SJulian Elischer }
1108f7ea2f55SJulian Elischer 
1109f7ea2f55SJulian Elischer static int
1110f7ea2f55SJulian Elischer fdwrite(dev_t dev, struct uio *uio, int ioflag)
1111f7ea2f55SJulian Elischer {
1112f7ea2f55SJulian Elischer 	return (physio(fdstrategy, NULL, dev, 0, minphys, uio));
1113f7ea2f55SJulian Elischer }
1114f7ea2f55SJulian Elischer 
11155b81b6b3SRodney W. Grimes 
11163a2f7427SDavid Greenman /****************************************************************************/
11173a2f7427SDavid Greenman /*                               fdstrategy                                 */
11183a2f7427SDavid Greenman /****************************************************************************/
11193a2f7427SDavid Greenman void
11203a2f7427SDavid Greenman fdstrategy(struct buf *bp)
11213a2f7427SDavid Greenman {
1122bb6382faSJoerg Wunsch 	unsigned nblocks, blknum, cando;
11233a2f7427SDavid Greenman  	int	s;
11243a2f7427SDavid Greenman  	fdcu_t	fdcu;
11253a2f7427SDavid Greenman  	fdu_t	fdu;
11263a2f7427SDavid Greenman  	fdc_p	fdc;
11273a2f7427SDavid Greenman  	fd_p	fd;
11283a2f7427SDavid Greenman 	size_t	fdblk;
11293a2f7427SDavid Greenman 
11303a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
11313a2f7427SDavid Greenman 	fd = &fd_data[fdu];
11323a2f7427SDavid Greenman 	fdc = fd->fdc;
11333a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
11343a2f7427SDavid Greenman 
11353a2f7427SDavid Greenman #if NFT > 0
11363a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
11373a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
1138d3628763SRodney W. Grimes 		bp->b_error = ENODEV;
11393a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
11403a2f7427SDavid Greenman 		goto bad;
11413a2f7427SDavid Greenman 	}
11423a2f7427SDavid Greenman 	/* check for controller already busy with tape */
11433a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
11443a2f7427SDavid Greenman 		bp->b_error = EBUSY;
11453a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
11463a2f7427SDavid Greenman 		goto bad;
11473a2f7427SDavid Greenman 	}
11483a2f7427SDavid Greenman #endif
1149d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
11503a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
11513a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
1152dc5df763SJoerg Wunsch 			printf(
11536a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1154702c623aSPoul-Henning Kamp 			       fdu, (u_long)bp->b_blkno, bp->b_bcount);
11553a2f7427SDavid Greenman 			bp->b_error = EINVAL;
11563a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11573a2f7427SDavid Greenman 			goto bad;
11583a2f7427SDavid Greenman 		}
11593a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
11603a2f7427SDavid Greenman 			bp->b_error = EINVAL;
11613a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11623a2f7427SDavid Greenman 			goto bad;
11633a2f7427SDavid Greenman 		}
11643a2f7427SDavid Greenman 	}
11653a2f7427SDavid Greenman 
11663a2f7427SDavid Greenman 	/*
11673a2f7427SDavid Greenman 	 * Set up block calculations.
11683a2f7427SDavid Greenman 	 */
1169bb6382faSJoerg Wunsch 	if (bp->b_blkno > 20000000) {
1170bb6382faSJoerg Wunsch 		/*
1171bb6382faSJoerg Wunsch 		 * Reject unreasonably high block number, prevent the
1172bb6382faSJoerg Wunsch 		 * multiplication below from overflowing.
1173bb6382faSJoerg Wunsch 		 */
1174bb6382faSJoerg Wunsch 		bp->b_error = EINVAL;
11753a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
11763a2f7427SDavid Greenman 		goto bad;
11773a2f7427SDavid Greenman 	}
1178bb6382faSJoerg Wunsch 	blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk;
1179bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
1180bb6382faSJoerg Wunsch 	bp->b_resid = 0;
1181bb6382faSJoerg Wunsch 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
1182bb6382faSJoerg Wunsch 		if (blknum <= nblocks) {
1183bb6382faSJoerg Wunsch 			cando = (nblocks - blknum) * fdblk;
1184bb6382faSJoerg Wunsch 			bp->b_resid = bp->b_bcount - cando;
1185bb6382faSJoerg Wunsch 			if (cando == 0)
1186bb6382faSJoerg Wunsch 				goto bad;	/* not actually bad but EOF */
1187bb6382faSJoerg Wunsch 		} else {
1188bb6382faSJoerg Wunsch 			bp->b_error = EINVAL;
1189bb6382faSJoerg Wunsch 			bp->b_flags |= B_ERROR;
1190bb6382faSJoerg Wunsch 			goto bad;
1191bb6382faSJoerg Wunsch 		}
1192bb6382faSJoerg Wunsch 	}
119327513ca7SBruce Evans  	bp->b_pblkno = bp->b_blkno;
11943a2f7427SDavid Greenman 	s = splbio();
119502a19910SJustin T. Gibbs 	bufqdisksort(&fdc->head, bp);
119602a19910SJustin T. Gibbs 	untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle); /* a good idea */
1197b2dfb1f9SJustin T. Gibbs 
1198b2dfb1f9SJustin T. Gibbs 	/* Tell devstat we are starting on the transaction */
1199b2dfb1f9SJustin T. Gibbs 	devstat_start_transaction(&fd->device_stats);
1200b2dfb1f9SJustin T. Gibbs 
12013a2f7427SDavid Greenman 	fdstart(fdcu);
12023a2f7427SDavid Greenman 	splx(s);
12033a2f7427SDavid Greenman 	return;
12043a2f7427SDavid Greenman 
12053a2f7427SDavid Greenman bad:
12063a2f7427SDavid Greenman 	biodone(bp);
12073a2f7427SDavid Greenman }
12083a2f7427SDavid Greenman 
12095b81b6b3SRodney W. Grimes /***************************************************************\
12105b81b6b3SRodney W. Grimes *				fdstart				*
12115b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
12125b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
12135b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
12145b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
12155b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
12165b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
12175b81b6b3SRodney W. Grimes \***************************************************************/
1218381fe1aaSGarrett Wollman static void
1219dc5df763SJoerg Wunsch fdstart(fdcu_t fdcu)
12205b81b6b3SRodney W. Grimes {
12215b81b6b3SRodney W. Grimes 	int s;
12225b81b6b3SRodney W. Grimes 
12235b81b6b3SRodney W. Grimes 	s = splbio();
12245b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
12255b81b6b3SRodney W. Grimes 	{
12265b81b6b3SRodney W. Grimes 		fdintr(fdcu);
12275b81b6b3SRodney W. Grimes 	}
12285b81b6b3SRodney W. Grimes 	splx(s);
12295b81b6b3SRodney W. Grimes }
12305b81b6b3SRodney W. Grimes 
1231381fe1aaSGarrett Wollman static void
12325c1a1eaeSBruce Evans fd_iotimeout(void *arg1)
12335b81b6b3SRodney W. Grimes {
12345c1a1eaeSBruce Evans  	fdc_p fdc;
12355c1a1eaeSBruce Evans 	fdcu_t fdcu;
1236f5f7ba03SJordan K. Hubbard 	int s;
12375b81b6b3SRodney W. Grimes 
12385c1a1eaeSBruce Evans 	fdcu = (fdcu_t)arg1;
12395c1a1eaeSBruce Evans 	fdc = fdc_data + fdcu;
12405c1a1eaeSBruce Evans 	TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
12415b81b6b3SRodney W. Grimes 
12423a2f7427SDavid Greenman 	/*
12433a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
12443a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
12453a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
12463a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
12473a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
12485c1a1eaeSBruce Evans 	 * The FDC must not be reset directly, since that would
12495c1a1eaeSBruce Evans 	 * interfere with the state machine.  Instead, pretend that
12505c1a1eaeSBruce Evans 	 * the command completed but was invalid.  The state machine
12515c1a1eaeSBruce Evans 	 * will reset the FDC and retry once.
12523a2f7427SDavid Greenman 	 */
12533a2f7427SDavid Greenman 	s = splbio();
12545c1a1eaeSBruce Evans 	fdc->status[0] = NE7_ST0_IC_IV;
12555c1a1eaeSBruce Evans 	fdc->flags &= ~FDC_STAT_VALID;
12565c1a1eaeSBruce Evans 	fdc->state = IOTIMEDOUT;
1257f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
1258f5f7ba03SJordan K. Hubbard 	splx(s);
12595b81b6b3SRodney W. Grimes }
12605b81b6b3SRodney W. Grimes 
12615b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
1262381fe1aaSGarrett Wollman static void
1263d0917939SPaul Richards fd_pseudointr(void *arg1)
12645b81b6b3SRodney W. Grimes {
1265381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
12665b81b6b3SRodney W. Grimes 	int	s;
12673a2f7427SDavid Greenman 
12685b81b6b3SRodney W. Grimes 	s = splbio();
12695b81b6b3SRodney W. Grimes 	fdintr(fdcu);
12705b81b6b3SRodney W. Grimes 	splx(s);
12715b81b6b3SRodney W. Grimes }
12725b81b6b3SRodney W. Grimes 
12735b81b6b3SRodney W. Grimes /***********************************************************************\
12745b81b6b3SRodney W. Grimes *                                 fdintr				*
12755b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
12765b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
12775b81b6b3SRodney W. Grimes \***********************************************************************/
1278381fe1aaSGarrett Wollman void
1279381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
12805b81b6b3SRodney W. Grimes {
12815b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
1282b99f0a4aSAndrew Moore #if NFT > 0
1283b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
1284b99f0a4aSAndrew Moore 
1285b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
1286b99f0a4aSAndrew Moore 		(ftintr(fdu));
1287b99f0a4aSAndrew Moore 	else
1288b99f0a4aSAndrew Moore #endif
1289381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
1290381fe1aaSGarrett Wollman 			;
12915b81b6b3SRodney W. Grimes }
12925b81b6b3SRodney W. Grimes 
12935b81b6b3SRodney W. Grimes /***********************************************************************\
12945b81b6b3SRodney W. Grimes * The controller state machine.						*
12955b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
12965b81b6b3SRodney W. Grimes \***********************************************************************/
12973a2f7427SDavid Greenman static int
1298dc5df763SJoerg Wunsch fdstate(fdcu_t fdcu, fdc_p fdc)
12995b81b6b3SRodney W. Grimes {
13003e425b96SJulian Elischer 	struct subdev *sd;
13015c1a1eaeSBruce Evans 	int read, format, head, i, sec = 0, sectrac, st0, cyl, st3;
1302bb6382faSJoerg Wunsch 	unsigned blknum = 0, b_cylinder = 0;
13035b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
13045b81b6b3SRodney W. Grimes 	fd_p fd;
130517542807SPoul-Henning Kamp 	register struct buf *bp;
1306b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
13073a2f7427SDavid Greenman 	size_t fdblk;
13085b81b6b3SRodney W. Grimes 
130902a19910SJustin T. Gibbs 	bp = bufq_first(&fdc->head);
131017542807SPoul-Henning Kamp 	if(!bp) {
13115b81b6b3SRodney W. Grimes 		/***********************************************\
13125b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
13135b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
13145b81b6b3SRodney W. Grimes 		\***********************************************/
13155b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
13165b81b6b3SRodney W. Grimes 		if(fdc->fd)
13175b81b6b3SRodney W. Grimes 		{
13186a0e6f42SRodney W. Grimes 			printf("fd%d: unexpected valid fd pointer\n",
13193a2f7427SDavid Greenman 			       fdc->fdu);
13205b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
13215b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
13225b81b6b3SRodney W. Grimes 		}
13235b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
13245b81b6b3SRodney W. Grimes  		return(0);
13255b81b6b3SRodney W. Grimes 	}
13265b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
13275b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
13283a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
13295b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
13305b81b6b3SRodney W. Grimes 	{
13316a0e6f42SRodney W. Grimes 		printf("fd%d: confused fd pointers\n", fdu);
13325b81b6b3SRodney W. Grimes 	}
13335b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
1334b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
1335bb6382faSJoerg Wunsch 	if(format) {
1336ab3f7469SPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->b_data;
1337bb6382faSJoerg Wunsch 		fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1338bb6382faSJoerg Wunsch 			- (char *)finfo;
1339bb6382faSJoerg Wunsch 	}
1340bb6382faSJoerg Wunsch 	if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
13413e425b96SJulian Elischer 		blknum = (unsigned) bp->b_pblkno * DEV_BSIZE/fdblk +
1342bb6382faSJoerg Wunsch 			fd->skip/fdblk;
1343bb6382faSJoerg Wunsch 		b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1344bb6382faSJoerg Wunsch 	}
13455b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
13465b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
13475b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
134802a19910SJustin T. Gibbs 	untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle);
134927651b7fSJustin T. Gibbs 	fd->toffhandle = timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
13505b81b6b3SRodney W. Grimes 	switch (fdc->state)
13515b81b6b3SRodney W. Grimes 	{
13525b81b6b3SRodney W. Grimes 	case DEVIDLE:
13535b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
13545b81b6b3SRodney W. Grimes 		fdc->retry = 0;
13555b81b6b3SRodney W. Grimes 		fd->skip = 0;
13565b81b6b3SRodney W. Grimes 		fdc->fd = fd;
13575b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
13583a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
13593a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
13605b81b6b3SRodney W. Grimes 		/*******************************************************\
13615b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
1362dc733423SDag-Erling Smørgrav 		* it will start up in its own good time		*
13635b81b6b3SRodney W. Grimes 		\*******************************************************/
13645b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
13655b81b6b3SRodney W. Grimes 		{
13665b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
13675b81b6b3SRodney W. Grimes 			return(0); /* come back later */
13685b81b6b3SRodney W. Grimes 		}
13695b81b6b3SRodney W. Grimes 		/*******************************************************\
13705b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
13715b81b6b3SRodney W. Grimes 		\*******************************************************/
13725b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
13735b81b6b3SRodney W. Grimes 		{
13745b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
13755b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
13765b81b6b3SRodney W. Grimes 			return(0);
13775b81b6b3SRodney W. Grimes 		}
13785b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
13795b81b6b3SRodney W. Grimes 		{
13803a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
13815b81b6b3SRodney W. Grimes 		}
13825c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
13835c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
13845c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
13855c1a1eaeSBruce Evans 		} else
13865e235068SJordan K. Hubbard 			fdc->state = DOSEEK;
13875b81b6b3SRodney W. Grimes 		break;
13885b81b6b3SRodney W. Grimes 	case DOSEEK:
1389bb6382faSJoerg Wunsch 		if (b_cylinder == (unsigned)fd->track)
13905b81b6b3SRodney W. Grimes 		{
13915b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
13925b81b6b3SRodney W. Grimes 			break;
13935b81b6b3SRodney W. Grimes 		}
1394dc5df763SJoerg Wunsch 		if (fd_cmd(fdcu, 3, NE7CMD_SEEK,
1395bb6382faSJoerg Wunsch 			   fd->fdsu, b_cylinder * fd->ft->steptrac,
1396dc5df763SJoerg Wunsch 			   0))
1397dc8603e3SJoerg Wunsch 		{
1398dc8603e3SJoerg Wunsch 			/*
1399dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1400dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1401dc8603e3SJoerg Wunsch 			 */
1402dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
1403dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1404dc8603e3SJoerg Wunsch 		}
1405dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
14065b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
14075b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
14085b81b6b3SRodney W. Grimes 	case SEEKWAIT:
14095b81b6b3SRodney W. Grimes 		/* allow heads to settle */
141004b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16);
14115b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
14125b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
14135b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
14145b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
1415dc5df763SJoerg Wunsch 		if(fd->track == FD_NO_TRACK)
14165b81b6b3SRodney W. Grimes 		{
1417bb6382faSJoerg Wunsch 			int descyl = b_cylinder * fd->ft->steptrac;
14183a2f7427SDavid Greenman 			do {
14193a2f7427SDavid Greenman 				/*
1420dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1421dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1422dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1423dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1424dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1425dc5df763SJoerg Wunsch 				 *
1426dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1427dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1428dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1429dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1430dc5df763SJoerg Wunsch 				 * real interrupt condition.
1431dc5df763SJoerg Wunsch 				 *
1432dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1433dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1434dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1435dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1436dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1437dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1438dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
14393a2f7427SDavid Greenman 				 */
1440dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1441dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1442dc5df763SJoerg Wunsch 					return 0;
1443dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1444dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1445dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
14463a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1447dc5df763SJoerg Wunsch 
14483a2f7427SDavid Greenman 			if (0 == descyl)
14493a2f7427SDavid Greenman 			{
1450dc5df763SJoerg Wunsch 				int failed = 0;
14513a2f7427SDavid Greenman 				/*
14523a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
14533a2f7427SDavid Greenman 				 * really there
14543a2f7427SDavid Greenman 				 */
1455dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1456dc5df763SJoerg Wunsch 					failed = 1;
14573a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
14583a2f7427SDavid Greenman 					printf(
14593a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
14603a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1461dc5df763SJoerg Wunsch 					failed = 1;
1462dc5df763SJoerg Wunsch 				}
1463dc5df763SJoerg Wunsch 
1464dc5df763SJoerg Wunsch 				if (failed)
1465dc5df763SJoerg Wunsch 				{
14663a2f7427SDavid Greenman 					if(fdc->retry < 3)
14673a2f7427SDavid Greenman 						fdc->retry = 3;
14683a2f7427SDavid Greenman 					return(retrier(fdcu));
14693a2f7427SDavid Greenman 				}
14703a2f7427SDavid Greenman 			}
1471dc5df763SJoerg Wunsch 
14725b81b6b3SRodney W. Grimes 			if (cyl != descyl)
14735b81b6b3SRodney W. Grimes 			{
14743a2f7427SDavid Greenman 				printf(
14753a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
14762d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
1477e5d7d243SBruce Evans 				if (fdc->retry < 3)
1478e5d7d243SBruce Evans 					fdc->retry = 3;
14795b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
14805b81b6b3SRodney W. Grimes 			}
14815b81b6b3SRodney W. Grimes 		}
14825b81b6b3SRodney W. Grimes 
1483bb6382faSJoerg Wunsch 		fd->track = b_cylinder;
1484ab3f7469SPoul-Henning Kamp 		isa_dmastart(bp->b_flags, bp->b_data+fd->skip,
14853a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
14865b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
14875b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
14885b81b6b3SRodney W. Grimes 		head = sec / sectrac;
14895b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
14903a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
14913a2f7427SDavid Greenman 
14923a2f7427SDavid Greenman 		if(format || !read)
14933a2f7427SDavid Greenman 		{
14943a2f7427SDavid Greenman 			/* make sure the drive is writable */
1495dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1496dc8603e3SJoerg Wunsch 			{
1497dc8603e3SJoerg Wunsch 				/* stuck controller? */
14985c1a1eaeSBruce Evans 				isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
14995c1a1eaeSBruce Evans 					    format ? bp->b_bcount : fdblk,
15005c1a1eaeSBruce Evans 					    fdc->dmachan);
1501dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
1502dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1503dc8603e3SJoerg Wunsch 			}
15043a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
15053a2f7427SDavid Greenman 			{
15063a2f7427SDavid Greenman 				/*
15073a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
15083a2f7427SDavid Greenman 				 * in order to force the current operation
15093a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
15103a2f7427SDavid Greenman 				 * error - all error handling is done
15113a2f7427SDavid Greenman 				 * by the retrier()
15123a2f7427SDavid Greenman 				 */
15133a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
15143a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
15153a2f7427SDavid Greenman 				fdc->status[2] = 0;
15163a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
15173a2f7427SDavid Greenman 				fdc->status[4] = head;
15183a2f7427SDavid Greenman 				fdc->status[5] = sec;
15193a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
15203a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
15213a2f7427SDavid Greenman 				return (1);
15223a2f7427SDavid Greenman 			}
15233a2f7427SDavid Greenman 		}
15245b81b6b3SRodney W. Grimes 
1525b39c878eSAndrey A. Chernov 		if(format)
1526b39c878eSAndrey A. Chernov 		{
1527b39c878eSAndrey A. Chernov 			/* formatting */
1528dc5df763SJoerg Wunsch 			if(fd_cmd(fdcu, 6,
1529dc5df763SJoerg Wunsch 				  NE7CMD_FORMAT,
1530dc5df763SJoerg Wunsch 				  head << 2 | fdu,
1531dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1532dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1533dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
1534dc5df763SJoerg Wunsch 				  finfo->fd_formb_fillbyte,
1535dc5df763SJoerg Wunsch 				  0))
1536dc8603e3SJoerg Wunsch 			{
1537dc8603e3SJoerg Wunsch 				/* controller fell over */
15385c1a1eaeSBruce Evans 				isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
15395c1a1eaeSBruce Evans 					    format ? bp->b_bcount : fdblk,
15405c1a1eaeSBruce Evans 					    fdc->dmachan);
1541dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1542dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1543dc8603e3SJoerg Wunsch 			}
1544b39c878eSAndrey A. Chernov 		}
1545b39c878eSAndrey A. Chernov 		else
1546b39c878eSAndrey A. Chernov 		{
1547dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 9,
1548dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1549dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1550dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1551dc5df763SJoerg Wunsch 				   head,
1552dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1553dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1554dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1555dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1556dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
1557dc5df763SJoerg Wunsch 				   0))
15585b81b6b3SRodney W. Grimes 			{
1559dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
15605c1a1eaeSBruce Evans 				isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
15615c1a1eaeSBruce Evans 					    format ? bp->b_bcount : fdblk,
15625c1a1eaeSBruce Evans 					    fdc->dmachan);
1563dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1564dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
15655b81b6b3SRodney W. Grimes 			}
1566b39c878eSAndrey A. Chernov 		}
15675b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
15685c1a1eaeSBruce Evans 		fd->tohandle = timeout(fd_iotimeout, (caddr_t)fdcu, hz);
15695b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15705b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
15715c1a1eaeSBruce Evans 		untimeout(fd_iotimeout, (caddr_t)fdcu, fd->tohandle);
1572dc5df763SJoerg Wunsch 
1573dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu))
15745b81b6b3SRodney W. Grimes 		{
15755c1a1eaeSBruce Evans 			isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
15765c1a1eaeSBruce Evans 				    format ? bp->b_bcount : fdblk,
15775c1a1eaeSBruce Evans 				    fdc->dmachan);
1578dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
1579dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
1580dc5df763SJoerg Wunsch 			return retrier(fdcu);
15815b81b6b3SRodney W. Grimes   		}
1582dc5df763SJoerg Wunsch 
15833a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
1584dc5df763SJoerg Wunsch 
15853a2f7427SDavid Greenman 		/* FALLTHROUGH */
1586dc5df763SJoerg Wunsch 
15873a2f7427SDavid Greenman 	case IOTIMEDOUT:
1588ab3f7469SPoul-Henning Kamp 		isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
15893a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
15903a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
15915b81b6b3SRodney W. Grimes 		{
15923a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
15933a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1594b39c878eSAndrey A. Chernov                                 /*
15953a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
15963a2f7427SDavid Greenman 				 * and didn't release it in time for the
15973a2f7427SDavid Greenman 				 * next FDC transfer.
15983a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
15993a2f7427SDavid Greenman 				 * count. (vak)
1600b39c878eSAndrey A. Chernov                                  */
1601b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1602b39c878eSAndrey A. Chernov                                 return (1);
1603b39c878eSAndrey A. Chernov                         }
16043a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
16053a2f7427SDavid Greenman 				&& fdc->retry < 6)
16063a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
16073a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
16083a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
16093a2f7427SDavid Greenman 				&& fdc->retry < 3)
16103a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
16115b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
16125b81b6b3SRodney W. Grimes 		}
16135b81b6b3SRodney W. Grimes 		/* All OK */
16143a2f7427SDavid Greenman 		fd->skip += fdblk;
1615bb6382faSJoerg Wunsch 		if (!format && fd->skip < bp->b_bcount - bp->b_resid)
16165b81b6b3SRodney W. Grimes 		{
16175b81b6b3SRodney W. Grimes 			/* set up next transfer */
16185b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
16195b81b6b3SRodney W. Grimes 		}
16205b81b6b3SRodney W. Grimes 		else
16215b81b6b3SRodney W. Grimes 		{
16225b81b6b3SRodney W. Grimes 			/* ALL DONE */
16235b81b6b3SRodney W. Grimes 			fd->skip = 0;
162402a19910SJustin T. Gibbs 			bufq_remove(&fdc->head, bp);
1625b2dfb1f9SJustin T. Gibbs 			/* Tell devstat we have finished with the transaction */
1626b2dfb1f9SJustin T. Gibbs 			devstat_end_transaction(&fd->device_stats,
1627b2dfb1f9SJustin T. Gibbs 						bp->b_bcount - bp->b_resid,
1628b2dfb1f9SJustin T. Gibbs 						DEVSTAT_TAG_NONE,
1629b2dfb1f9SJustin T. Gibbs 						(bp->b_flags & B_READ) ?
1630b2dfb1f9SJustin T. Gibbs 						DEVSTAT_READ : DEVSTAT_WRITE);
16315b81b6b3SRodney W. Grimes 			biodone(bp);
16325b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
16335b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
16345b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
16355b81b6b3SRodney W. Grimes 		}
16365b81b6b3SRodney W. Grimes 		return(1);
16375b81b6b3SRodney W. Grimes 	case RESETCTLR:
16383a2f7427SDavid Greenman 		fdc_reset(fdc);
16395b81b6b3SRodney W. Grimes 		fdc->retry++;
16405c1a1eaeSBruce Evans 		fdc->state = RESETCOMPLETE;
16415c1a1eaeSBruce Evans 		return (0);
16425c1a1eaeSBruce Evans 	case RESETCOMPLETE:
16435c1a1eaeSBruce Evans 		/*
16445c1a1eaeSBruce Evans 		 * Discard all the results from the reset so that they
16455c1a1eaeSBruce Evans 		 * can't cause an unexpected interrupt later.
16465c1a1eaeSBruce Evans 		 */
16470e317d05SJoerg Wunsch 		for (i = 0; i < 4; i++)
16480e317d05SJoerg Wunsch 			(void)fd_sense_int(fdc, &st0, &cyl);
16495c1a1eaeSBruce Evans 		fdc->state = STARTRECAL;
16505c1a1eaeSBruce Evans 		/* Fall through. */
16515c1a1eaeSBruce Evans 	case STARTRECAL:
1652dc5df763SJoerg Wunsch 		if(fd_cmd(fdcu,
1653dc5df763SJoerg Wunsch 			  2, NE7CMD_RECAL, fdu,
1654dc5df763SJoerg Wunsch 			  0)) /* Recalibrate Function */
1655dc8603e3SJoerg Wunsch 		{
1656dc8603e3SJoerg Wunsch 			/* arrgl */
1657dc8603e3SJoerg Wunsch 			fdc->retry = 6;
1658dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1659dc8603e3SJoerg Wunsch 		}
16605b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
16615b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
16625b81b6b3SRodney W. Grimes 	case RECALWAIT:
16635b81b6b3SRodney W. Grimes 		/* allow heads to settle */
166404b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8);
16655b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
16665b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
16675b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
16683a2f7427SDavid Greenman 		do {
16693a2f7427SDavid Greenman 			/*
1670dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
16713a2f7427SDavid Greenman 			 */
1672dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1673dc5df763SJoerg Wunsch 				return 0;
1674dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
1675dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1676dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
16773a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
16783a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
16795b81b6b3SRodney W. Grimes 		{
1680dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
1681dc8603e3SJoerg Wunsch 				/*
1682dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
1683dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
1684dc8603e3SJoerg Wunsch 				 * since people used to complain much about
1685dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
1686dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
1687dc8603e3SJoerg Wunsch 				 * time in a line
1688dc8603e3SJoerg Wunsch 				 */
1689dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
1690dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
16913a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
16925b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
16935b81b6b3SRodney W. Grimes 		}
16945b81b6b3SRodney W. Grimes 		fd->track = 0;
16955b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
16965b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
16975b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
16985b81b6b3SRodney W. Grimes 	case MOTORWAIT:
16995b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
17005b81b6b3SRodney W. Grimes 		{
17015b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
17025b81b6b3SRodney W. Grimes 		}
17035c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
17045c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
17055c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
17065c1a1eaeSBruce Evans 		} else {
17075e235068SJordan K. Hubbard 			/*
17085c1a1eaeSBruce Evans 			 * If all motors were off, then the controller was
17095c1a1eaeSBruce Evans 			 * reset, so it has lost track of the current
17105c1a1eaeSBruce Evans 			 * cylinder.  Recalibrate to handle this case.
17115e235068SJordan K. Hubbard 			 */
17125e235068SJordan K. Hubbard 			fdc->state = STARTRECAL;
17135c1a1eaeSBruce Evans 		}
17145b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
17155b81b6b3SRodney W. Grimes 	default:
1716dc5df763SJoerg Wunsch 		printf("fdc%d: Unexpected FD int->", fdcu);
1717dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu) == 0)
1718a838d83dSBruce Evans 			printf("FDC status :%x %x %x %x %x %x %x   ",
17195b81b6b3SRodney W. Grimes 			       fdc->status[0],
17205b81b6b3SRodney W. Grimes 			       fdc->status[1],
17215b81b6b3SRodney W. Grimes 			       fdc->status[2],
17225b81b6b3SRodney W. Grimes 			       fdc->status[3],
17235b81b6b3SRodney W. Grimes 			       fdc->status[4],
17245b81b6b3SRodney W. Grimes 			       fdc->status[5],
17255b81b6b3SRodney W. Grimes 			       fdc->status[6] );
17263a2f7427SDavid Greenman 		else
1727dac0f2dbSJoerg Wunsch 			printf("No status available   ");
1728dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
1729dac0f2dbSJoerg Wunsch 		{
1730dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
17315b81b6b3SRodney W. Grimes 			return(0);
17325b81b6b3SRodney W. Grimes 		}
1733dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
1734dac0f2dbSJoerg Wunsch 		return(0);
1735dac0f2dbSJoerg Wunsch 	}
1736dac0f2dbSJoerg Wunsch 	/*XXX confusing: some branches return immediately, others end up here*/
17375b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
17385b81b6b3SRodney W. Grimes }
17395b81b6b3SRodney W. Grimes 
1740aaf08d94SGarrett Wollman static int
1741f5f7ba03SJordan K. Hubbard retrier(fdcu)
1742f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
17435b81b6b3SRodney W. Grimes {
17443e425b96SJulian Elischer 	struct subdev *sd;
17455b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
174617542807SPoul-Henning Kamp 	register struct buf *bp;
1747b2dfb1f9SJustin T. Gibbs 	int fdu;
17485b81b6b3SRodney W. Grimes 
174902a19910SJustin T. Gibbs 	bp = bufq_first(&fdc->head);
17505b81b6b3SRodney W. Grimes 
17513a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
17523a2f7427SDavid Greenman 		goto fail;
17535b81b6b3SRodney W. Grimes 	switch(fdc->retry)
17545b81b6b3SRodney W. Grimes 	{
17555b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
17565b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
17575b81b6b3SRodney W. Grimes 		break;
17585b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
17595b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
17605b81b6b3SRodney W. Grimes 		break;
17615b81b6b3SRodney W. Grimes 	case 6:
17625b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
17635b81b6b3SRodney W. Grimes 		break;
17645b81b6b3SRodney W. Grimes 	case 7:
17655b81b6b3SRodney W. Grimes 		break;
17665b81b6b3SRodney W. Grimes 	default:
17673a2f7427SDavid Greenman 	fail:
17685b81b6b3SRodney W. Grimes 		{
17697ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
17707ca0641bSAndrey A. Chernov 			/* Trick diskerr */
17713a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
17723a2f7427SDavid Greenman 				    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
177392ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
17743a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
17753a2f7427SDavid Greenman 				(struct disklabel *)NULL);
17767ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
1777dc5df763SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID)
1778dc5df763SJoerg Wunsch 			{
1779dc5df763SJoerg Wunsch 				printf(
1780a838d83dSBruce Evans 			" (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
1781dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
1782dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
1783dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
1784dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
1785dc5df763SJoerg Wunsch 				       fdc->status[5]);
1786dc5df763SJoerg Wunsch 			}
1787dc5df763SJoerg Wunsch 			else
1788dc5df763SJoerg Wunsch 				printf(" (No status)\n");
17895b81b6b3SRodney W. Grimes 		}
17905b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
17915b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
1792bb6382faSJoerg Wunsch 		bp->b_resid += bp->b_bcount - fdc->fd->skip;
179302a19910SJustin T. Gibbs 		bufq_remove(&fdc->head, bp);
1794b2dfb1f9SJustin T. Gibbs 
1795b2dfb1f9SJustin T. Gibbs 		/* Tell devstat we have finished with the transaction */
17969f02ad60SJustin T. Gibbs 		devstat_end_transaction(&fdc->fd->device_stats,
1797b2dfb1f9SJustin T. Gibbs 					bp->b_bcount - bp->b_resid,
1798b2dfb1f9SJustin T. Gibbs 					DEVSTAT_TAG_NONE,
1799b2dfb1f9SJustin T. Gibbs 					(bp->b_flags & B_READ) ? DEVSTAT_READ :
1800b2dfb1f9SJustin T. Gibbs 								 DEVSTAT_WRITE);
18015b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
18025b81b6b3SRodney W. Grimes 		biodone(bp);
180392ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
18045c1a1eaeSBruce Evans 		fdc->flags |= FDC_NEEDS_RESET;
18055b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
18065b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
180792ed385aSRodney W. Grimes 		return(1);
18085b81b6b3SRodney W. Grimes 	}
18095b81b6b3SRodney W. Grimes 	fdc->retry++;
18105b81b6b3SRodney W. Grimes 	return(1);
18115b81b6b3SRodney W. Grimes }
18125b81b6b3SRodney W. Grimes 
1813b39c878eSAndrey A. Chernov static int
1814b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1815b39c878eSAndrey A. Chernov 	dev_t dev;
1816b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1817b39c878eSAndrey A. Chernov 	struct proc *p;
1818b39c878eSAndrey A. Chernov {
1819b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1820b39c878eSAndrey A. Chernov  	fd_p	fd;
1821b39c878eSAndrey A. Chernov 
1822b39c878eSAndrey A. Chernov 	struct buf *bp;
1823b39c878eSAndrey A. Chernov 	int rv = 0, s;
18243a2f7427SDavid Greenman 	size_t fdblk;
1825b39c878eSAndrey A. Chernov 
1826b39c878eSAndrey A. Chernov  	fdu	= FDUNIT(minor(dev));
1827b39c878eSAndrey A. Chernov 	fd	= &fd_data[fdu];
18283a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1829b39c878eSAndrey A. Chernov 
1830b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
1831b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1832b39c878eSAndrey A. Chernov 	if(bp == 0)
1833b39c878eSAndrey A. Chernov 		return ENOBUFS;
183482f5379bSJoerg Wunsch 	/*
183582f5379bSJoerg Wunsch 	 * keep the process from being swapped
183682f5379bSJoerg Wunsch 	 */
183782f5379bSJoerg Wunsch 	p->p_flag |= P_PHYSIO;
1838b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
1839b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1840b39c878eSAndrey A. Chernov 	bp->b_proc = p;
1841b39c878eSAndrey A. Chernov 
1842b39c878eSAndrey A. Chernov 	/*
1843b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
1844b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
1845b39c878eSAndrey A. Chernov 	 */
1846b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
18473a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
1848b39c878eSAndrey A. Chernov 
1849b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1850ab3f7469SPoul-Henning Kamp 	bp->b_data = (caddr_t)finfo;
1851b39c878eSAndrey A. Chernov 
1852b39c878eSAndrey A. Chernov 	/* now do the format */
18533e425b96SJulian Elischer 	bp->b_dev = dev;
1854b39c878eSAndrey A. Chernov 	fdstrategy(bp);
1855b39c878eSAndrey A. Chernov 
1856b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
1857b39c878eSAndrey A. Chernov 	s = splbio();
1858b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
1859b39c878eSAndrey A. Chernov 	{
18605e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1861b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
1862b39c878eSAndrey A. Chernov 			break;
1863b39c878eSAndrey A. Chernov 	}
1864b39c878eSAndrey A. Chernov 	splx(s);
1865b39c878eSAndrey A. Chernov 
186682f5379bSJoerg Wunsch 	if(rv == EWOULDBLOCK) {
1867b39c878eSAndrey A. Chernov 		/* timed out */
1868b39c878eSAndrey A. Chernov 		rv = EIO;
186982f5379bSJoerg Wunsch 		biodone(bp);
187082f5379bSJoerg Wunsch 	}
18713a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
18723a2f7427SDavid Greenman 		rv = bp->b_error;
187382f5379bSJoerg Wunsch 	/*
187482f5379bSJoerg Wunsch 	 * allow the process to be swapped
187582f5379bSJoerg Wunsch 	 */
187682f5379bSJoerg Wunsch 	p->p_flag &= ~P_PHYSIO;
1877b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
1878b39c878eSAndrey A. Chernov 	return rv;
1879b39c878eSAndrey A. Chernov }
1880b39c878eSAndrey A. Chernov 
1881f5f7ba03SJordan K. Hubbard /*
1882671e2ceeSBruce Evans  * TODO: don't allocate buffer on stack.
1883f5f7ba03SJordan K. Hubbard  */
18845b81b6b3SRodney W. Grimes 
18853e425b96SJulian Elischer static int
1886b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
1887f5f7ba03SJordan K. Hubbard 	dev_t dev;
1888ecbb00a2SDoug Rabson 	u_long cmd;
1889f5f7ba03SJordan K. Hubbard 	caddr_t addr;
1890f5f7ba03SJordan K. Hubbard 	int flag;
1891b39c878eSAndrey A. Chernov 	struct proc *p;
1892f5f7ba03SJordan K. Hubbard {
18933a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
18943a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
18953a2f7427SDavid Greenman 	size_t fdblk;
18963a2f7427SDavid Greenman 
1897f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
1898f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
1899f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
19003a2f7427SDavid Greenman 	int error = 0;
1901f5f7ba03SJordan K. Hubbard 
1902b99f0a4aSAndrew Moore #if NFT > 0
1903a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
1904a60eff27SNate Williams 
1905a60eff27SNate Williams 	/* check for a tape ioctl */
1906a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
1907b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
1908b99f0a4aSAndrew Moore #endif
1909b99f0a4aSAndrew Moore 
19103a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1911f5f7ba03SJordan K. Hubbard 
1912f5f7ba03SJordan K. Hubbard 	switch (cmd)
1913f5f7ba03SJordan K. Hubbard 	{
1914f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
1915f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
1916f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
19173a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
191892ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
1919f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
1920f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
1921f5f7ba03SJordan K. Hubbard 
1922191e1a59SBruce Evans 		if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl)
1923191e1a59SBruce Evans 		    == NULL)
1924f5f7ba03SJordan K. Hubbard 			error = 0;
1925f5f7ba03SJordan K. Hubbard 		else
1926f5f7ba03SJordan K. Hubbard 			error = EINVAL;
1927f5f7ba03SJordan K. Hubbard 
1928f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
1929f5f7ba03SJordan K. Hubbard 		break;
1930f5f7ba03SJordan K. Hubbard 
1931f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
1932f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1933f5f7ba03SJordan K. Hubbard 			error = EBADF;
1934f5f7ba03SJordan K. Hubbard 		break;
1935f5f7ba03SJordan K. Hubbard 
1936f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
1937f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1938f5f7ba03SJordan K. Hubbard 			error = EBADF;
1939f5f7ba03SJordan K. Hubbard 		break;
1940f5f7ba03SJordan K. Hubbard 
1941f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
1942f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1943f5f7ba03SJordan K. Hubbard 		{
1944f5f7ba03SJordan K. Hubbard 			error = EBADF;
1945f5f7ba03SJordan K. Hubbard 			break;
1946f5f7ba03SJordan K. Hubbard 		}
1947f5f7ba03SJordan K. Hubbard 
1948f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
1949f5f7ba03SJordan K. Hubbard 
1950191e1a59SBruce Evans 		if ((error = setdisklabel((struct disklabel *)buffer, dl,
1951191e1a59SBruce Evans 					  (u_long)0)) != 0)
1952f5f7ba03SJordan K. Hubbard 			break;
1953f5f7ba03SJordan K. Hubbard 
1954b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
195554c7241bSJordan K. Hubbard 				       (struct disklabel *)buffer);
1956b39c878eSAndrey A. Chernov 		break;
1957b39c878eSAndrey A. Chernov 	case FD_FORM:
1958b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
1959b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
1960b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
1961b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
1962b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
1963b39c878eSAndrey A. Chernov 		else
1964b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
1965b39c878eSAndrey A. Chernov 		break;
1966b39c878eSAndrey A. Chernov 
1967b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
19683e425b96SJulian Elischer 		*(struct fd_type *)addr = *fd->ft;
1969f5f7ba03SJordan K. Hubbard 		break;
1970f5f7ba03SJordan K. Hubbard 
19713a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
19723a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
19733a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
19743a2f7427SDavid Greenman 			return EPERM;
19753e425b96SJulian Elischer 		*fd->ft = *(struct fd_type *)addr;
19763a2f7427SDavid Greenman 		break;
19773a2f7427SDavid Greenman 
19783a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
19793e425b96SJulian Elischer 		*(int *)addr = fd->options;
19803a2f7427SDavid Greenman 		break;
19813a2f7427SDavid Greenman 
19823a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
19833e425b96SJulian Elischer 		fd->options = *(int *)addr;
19843a2f7427SDavid Greenman 		break;
19853a2f7427SDavid Greenman 
1986f5f7ba03SJordan K. Hubbard 	default:
19873a2f7427SDavid Greenman 		error = ENOTTY;
1988f5f7ba03SJordan K. Hubbard 		break;
1989f5f7ba03SJordan K. Hubbard 	}
1990f5f7ba03SJordan K. Hubbard 	return (error);
1991f5f7ba03SJordan K. Hubbard }
1992f5f7ba03SJordan K. Hubbard 
19937146c13eSJulian Elischer 
19947146c13eSJulian Elischer static fd_devsw_installed = 0;
19957146c13eSJulian Elischer 
199687f6c662SJulian Elischer static void 	fd_drvinit(void *notused )
19977146c13eSJulian Elischer {
199887f6c662SJulian Elischer 
19997146c13eSJulian Elischer 	if( ! fd_devsw_installed ) {
2000f7ea2f55SJulian Elischer 		cdevsw_add_generic(BDEV_MAJOR,CDEV_MAJOR, &fd_cdevsw);
20017146c13eSJulian Elischer 		fd_devsw_installed = 1;
20027146c13eSJulian Elischer 	}
20037146c13eSJulian Elischer }
200487f6c662SJulian Elischer 
200587f6c662SJulian Elischer SYSINIT(fddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,fd_drvinit,NULL)
200687f6c662SJulian Elischer 
20073e425b96SJulian Elischer 
2008f5f7ba03SJordan K. Hubbard #endif
20093e425b96SJulian Elischer 
20103a2f7427SDavid Greenman /*
20113a2f7427SDavid Greenman  * Hello emacs, these are the
20123a2f7427SDavid Greenman  * Local Variables:
20133a2f7427SDavid Greenman  *  c-indent-level:               8
20143a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
20153a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
20163a2f7427SDavid Greenman  *  c-brace-offset:              -8
20173a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
20183a2f7427SDavid Greenman  *  c-argdecl-indent:             8
20193a2f7427SDavid Greenman  *  c-label-offset:              -8
20203a2f7427SDavid Greenman  *  c++-hanging-braces:           1
20213a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
20223a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
20233a2f7427SDavid Greenman  *  c++-friend-offset:            0
20243a2f7427SDavid Greenman  * End:
20253a2f7427SDavid Greenman  */
2026