xref: /freebsd/sys/dev/fdc/fdc.c (revision 20a2916818b397e9d526ec1ac635f0d8691fe327)
15b81b6b3SRodney W. Grimes /*#define DEBUG 1*/
25b81b6b3SRodney W. Grimes /*-
35b81b6b3SRodney W. Grimes  * Copyright (c) 1990 The Regents of the University of California.
45b81b6b3SRodney W. Grimes  * All rights reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
75b81b6b3SRodney W. Grimes  * Don Ahn.
85b81b6b3SRodney W. Grimes  *
95b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
105b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
115b81b6b3SRodney W. Grimes  * are met:
125b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
135b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
145b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
155b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
165b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
175b81b6b3SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
185b81b6b3SRodney W. Grimes  *    must display the following acknowledgement:
195b81b6b3SRodney W. Grimes  *	This product includes software developed by the University of
205b81b6b3SRodney W. Grimes  *	California, Berkeley and its contributors.
215b81b6b3SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
225b81b6b3SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
235b81b6b3SRodney W. Grimes  *    without specific prior written permission.
245b81b6b3SRodney W. Grimes  *
255b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
265b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
275b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
285b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
295b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
305b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
315b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
325b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
335b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
345b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
355b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
365b81b6b3SRodney W. Grimes  *
37dc4ff321SRodney W. Grimes  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
3820a29168SAndrey A. Chernov  *	$Id: fd.c,v 1.9 1993/12/04 16:13:18 ats Exp $
395b81b6b3SRodney W. Grimes  *
405b81b6b3SRodney W. Grimes  */
415b81b6b3SRodney W. Grimes 
425b81b6b3SRodney W. Grimes #include "fd.h"
435b81b6b3SRodney W. Grimes #if NFD > 0
445b81b6b3SRodney W. Grimes 
455b81b6b3SRodney W. Grimes #include "param.h"
465b81b6b3SRodney W. Grimes #include "dkbad.h"
475b81b6b3SRodney W. Grimes #include "systm.h"
48381fe1aaSGarrett Wollman #include "kernel.h"
495b81b6b3SRodney W. Grimes #include "conf.h"
505b81b6b3SRodney W. Grimes #include "file.h"
515b81b6b3SRodney W. Grimes #include "ioctl.h"
52f5f7ba03SJordan K. Hubbard #include "disklabel.h"
535b81b6b3SRodney W. Grimes #include "buf.h"
545b81b6b3SRodney W. Grimes #include "uio.h"
5592ed385aSRodney W. Grimes #include "syslog.h"
565b81b6b3SRodney W. Grimes #include "i386/isa/isa.h"
575b81b6b3SRodney W. Grimes #include "i386/isa/isa_device.h"
585b81b6b3SRodney W. Grimes #include "i386/isa/fdreg.h"
595b81b6b3SRodney W. Grimes #include "i386/isa/icu.h"
605b81b6b3SRodney W. Grimes #include "i386/isa/rtc.h"
615b81b6b3SRodney W. Grimes #undef NFD
625b81b6b3SRodney W. Grimes #define NFD 2
635b81b6b3SRodney W. Grimes 
645b81b6b3SRodney W. Grimes #define	FDUNIT(s)	((s>>3)&1)
655b81b6b3SRodney W. Grimes #define	FDTYPE(s)	((s)&7)
665b81b6b3SRodney W. Grimes 
675b81b6b3SRodney W. Grimes #define b_cylin b_resid
685b81b6b3SRodney W. Grimes #define FDBLK 512
6920a29168SAndrey A. Chernov #define NUMTYPES 5
705b81b6b3SRodney W. Grimes 
715b81b6b3SRodney W. Grimes struct fd_type {
725b81b6b3SRodney W. Grimes 	int	sectrac;		/* sectors per track         */
735b81b6b3SRodney W. Grimes 	int	secsize;		/* size code for sectors     */
745b81b6b3SRodney W. Grimes 	int	datalen;		/* data len when secsize = 0 */
755b81b6b3SRodney W. Grimes 	int	gap;			/* gap len between sectors   */
765b81b6b3SRodney W. Grimes 	int	tracks;			/* total num of tracks       */
775b81b6b3SRodney W. Grimes 	int	size;			/* size of disk in sectors   */
785b81b6b3SRodney W. Grimes 	int	steptrac;		/* steps per cylinder        */
795b81b6b3SRodney W. Grimes 	int	trans;			/* transfer speed code       */
805b81b6b3SRodney W. Grimes 	int	heads;			/* number of heads	     */
815b81b6b3SRodney W. Grimes };
825b81b6b3SRodney W. Grimes 
835b81b6b3SRodney W. Grimes struct fd_type fd_types[NUMTYPES] =
845b81b6b3SRodney W. Grimes {
855b81b6b3SRodney W. Grimes  	{ 18,2,0xFF,0x1B,80,2880,1,0,2 }, /* 1.44 meg HD 3.5in floppy    */
865b81b6b3SRodney W. Grimes 	{ 15,2,0xFF,0x1B,80,2400,1,0,2 }, /* 1.2 meg HD floppy           */
8720a29168SAndrey A. Chernov 	{ 9,2,0xFF,0x20,80,1440,1,1,2 }, /* 720k floppy in HD drive     */
885b81b6b3SRodney W. Grimes 	{ 9,2,0xFF,0x23,40,720,2,1,2 },	/* 360k floppy in 1.2meg drive */
895b81b6b3SRodney W. Grimes 	{ 9,2,0xFF,0x2A,40,720,1,1,2 },	/* 360k floppy in DD drive     */
905b81b6b3SRodney W. Grimes };
915b81b6b3SRodney W. Grimes 
925b81b6b3SRodney W. Grimes #define DRVS_PER_CTLR 2
935b81b6b3SRodney W. Grimes /***********************************************************************\
945b81b6b3SRodney W. Grimes * Per controller structure.						*
955b81b6b3SRodney W. Grimes \***********************************************************************/
965b81b6b3SRodney W. Grimes struct fdc_data
975b81b6b3SRodney W. Grimes {
985b81b6b3SRodney W. Grimes 	int	fdcu;		/* our unit number */
995b81b6b3SRodney W. Grimes 	int	baseport;
1005b81b6b3SRodney W. Grimes 	int	dmachan;
1015b81b6b3SRodney W. Grimes 	int	flags;
1025b81b6b3SRodney W. Grimes #define FDC_ATTACHED	0x01
1035b81b6b3SRodney W. Grimes 	struct	fd_data *fd;
1045b81b6b3SRodney W. Grimes 	int fdu;		/* the active drive	*/
1055b81b6b3SRodney W. Grimes 	struct buf head;	/* Head of buf chain      */
1065b81b6b3SRodney W. Grimes 	struct buf rhead;	/* Raw head of buf chain  */
1075b81b6b3SRodney W. Grimes 	int state;
1085b81b6b3SRodney W. Grimes 	int retry;
1095b81b6b3SRodney W. Grimes 	int status[7];		/* copy of the registers */
1105b81b6b3SRodney W. Grimes }fdc_data[(NFD+1)/DRVS_PER_CTLR];
1115b81b6b3SRodney W. Grimes 
1125b81b6b3SRodney W. Grimes /***********************************************************************\
1135b81b6b3SRodney W. Grimes * Per drive structure.							*
1145b81b6b3SRodney W. Grimes * N per controller (presently 2) (DRVS_PER_CTLR)			*
1155b81b6b3SRodney W. Grimes \***********************************************************************/
1165b81b6b3SRodney W. Grimes struct fd_data {
1175b81b6b3SRodney W. Grimes 	struct	fdc_data *fdc;
1185b81b6b3SRodney W. Grimes 	int	fdu;		/* this unit number */
1195b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
1205b81b6b3SRodney W. Grimes 	int	type;		/* Drive type (HD, DD     */
1215b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
1225b81b6b3SRodney W. Grimes 	int	flags;
1235b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
1245b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
1255b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
1265b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
1275b81b6b3SRodney W. Grimes 	int skip;
1285b81b6b3SRodney W. Grimes 	int hddrv;
1295b81b6b3SRodney W. Grimes 	int track;		/* where we think the head is */
1305b81b6b3SRodney W. Grimes } fd_data[NFD];
1315b81b6b3SRodney W. Grimes 
1325b81b6b3SRodney W. Grimes /***********************************************************************\
1335b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
1345b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
1355b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
1365b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
1375b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
1385b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
1395b81b6b3SRodney W. Grimes \***********************************************************************/
1405b81b6b3SRodney W. Grimes typedef int	fdu_t;
1415b81b6b3SRodney W. Grimes typedef int	fdcu_t;
1425b81b6b3SRodney W. Grimes typedef int	fdsu_t;
1435b81b6b3SRodney W. Grimes typedef	struct fd_data *fd_p;
1445b81b6b3SRodney W. Grimes typedef struct fdc_data *fdc_p;
1455b81b6b3SRodney W. Grimes 
1465b81b6b3SRodney W. Grimes #define DEVIDLE		0
1475b81b6b3SRodney W. Grimes #define FINDWORK	1
1485b81b6b3SRodney W. Grimes #define	DOSEEK		2
1495b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 	3
1505b81b6b3SRodney W. Grimes #define	IOCOMPLETE	4
1515b81b6b3SRodney W. Grimes #define RECALCOMPLETE	5
1525b81b6b3SRodney W. Grimes #define	STARTRECAL	6
1535b81b6b3SRodney W. Grimes #define	RESETCTLR	7
1545b81b6b3SRodney W. Grimes #define	SEEKWAIT	8
1555b81b6b3SRodney W. Grimes #define	RECALWAIT	9
1565b81b6b3SRodney W. Grimes #define	MOTORWAIT	10
1575b81b6b3SRodney W. Grimes #define	IOTIMEDOUT	11
1585b81b6b3SRodney W. Grimes 
1595b81b6b3SRodney W. Grimes #ifdef	DEBUG
1605b81b6b3SRodney W. Grimes char *fdstates[] =
1615b81b6b3SRodney W. Grimes {
1625b81b6b3SRodney W. Grimes "DEVIDLE",
1635b81b6b3SRodney W. Grimes "FINDWORK",
1645b81b6b3SRodney W. Grimes "DOSEEK",
1655b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
1665b81b6b3SRodney W. Grimes "IOCOMPLETE",
1675b81b6b3SRodney W. Grimes "RECALCOMPLETE",
1685b81b6b3SRodney W. Grimes "STARTRECAL",
1695b81b6b3SRodney W. Grimes "RESETCTLR",
1705b81b6b3SRodney W. Grimes "SEEKWAIT",
1715b81b6b3SRodney W. Grimes "RECALWAIT",
1725b81b6b3SRodney W. Grimes "MOTORWAIT",
1735b81b6b3SRodney W. Grimes "IOTIMEDOUT"
1745b81b6b3SRodney W. Grimes };
1755b81b6b3SRodney W. Grimes 
1765b81b6b3SRodney W. Grimes 
1775b81b6b3SRodney W. Grimes int	fd_debug = 1;
1785b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
1795b81b6b3SRodney W. Grimes #define TRACE1(arg1,arg2) if(fd_debug) printf(arg1,arg2)
180381fe1aaSGarrett Wollman #else /* DEBUG */
1815b81b6b3SRodney W. Grimes #define TRACE0(arg)
1825b81b6b3SRodney W. Grimes #define TRACE1(arg1,arg2)
183381fe1aaSGarrett Wollman #endif /* DEBUG */
1845b81b6b3SRodney W. Grimes 
185381fe1aaSGarrett Wollman static void fdstart(fdcu_t);
186381fe1aaSGarrett Wollman void fdintr(fdcu_t);
187381fe1aaSGarrett Wollman static void fd_turnoff(caddr_t, int);
1885b81b6b3SRodney W. Grimes 
1895b81b6b3SRodney W. Grimes /****************************************************************************/
1905b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
1915b81b6b3SRodney W. Grimes /****************************************************************************/
192381fe1aaSGarrett Wollman static int fdprobe(struct isa_device *);
193381fe1aaSGarrett Wollman static int fdattach(struct isa_device *);
1945b81b6b3SRodney W. Grimes 
1955b81b6b3SRodney W. Grimes struct	isa_driver fddriver = {
1965b81b6b3SRodney W. Grimes 	fdprobe, fdattach, "fd",
1975b81b6b3SRodney W. Grimes };
1985b81b6b3SRodney W. Grimes 
1995b81b6b3SRodney W. Grimes /*
2005b81b6b3SRodney W. Grimes  * probe for existance of controller
2015b81b6b3SRodney W. Grimes  */
202381fe1aaSGarrett Wollman int
2035b81b6b3SRodney W. Grimes fdprobe(dev)
2045b81b6b3SRodney W. Grimes 	struct isa_device *dev;
2055b81b6b3SRodney W. Grimes {
2065b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
2075b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].flags & FDC_ATTACHED)
2085b81b6b3SRodney W. Grimes 	{
2095b81b6b3SRodney W. Grimes 		printf("fdc: same unit (%d) used multiple times\n",fdcu);
2105b81b6b3SRodney W. Grimes 		return 0;
2115b81b6b3SRodney W. Grimes 	}
2125b81b6b3SRodney W. Grimes 
2135b81b6b3SRodney W. Grimes 	fdc_data[fdcu].baseport = dev->id_iobase;
2145b81b6b3SRodney W. Grimes 
21516111cedSAndrew Moore 	/* First - lets reset the floppy controller */
21616111cedSAndrew Moore 
21716111cedSAndrew Moore 	outb(dev->id_iobase+fdout,0);
21816111cedSAndrew Moore 	DELAY(100);
21916111cedSAndrew Moore 	outb(dev->id_iobase+fdout,FDO_FRST);
22016111cedSAndrew Moore 
2215b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
2225b81b6b3SRodney W. Grimes 	if (out_fdc(fdcu,NE7CMD_SPECIFY) < 0)
2235b81b6b3SRodney W. Grimes 	{
2245b81b6b3SRodney W. Grimes 		return(0);
2255b81b6b3SRodney W. Grimes 	}
2265b81b6b3SRodney W. Grimes 	out_fdc(fdcu,0xDF);
2275b81b6b3SRodney W. Grimes 	out_fdc(fdcu,2);
2285b81b6b3SRodney W. Grimes 	return (IO_FDCSIZE);
2295b81b6b3SRodney W. Grimes }
2305b81b6b3SRodney W. Grimes 
2315b81b6b3SRodney W. Grimes /*
2325b81b6b3SRodney W. Grimes  * wire controller into system, look for floppy units
2335b81b6b3SRodney W. Grimes  */
234381fe1aaSGarrett Wollman int
2355b81b6b3SRodney W. Grimes fdattach(dev)
2365b81b6b3SRodney W. Grimes 	struct isa_device *dev;
2375b81b6b3SRodney W. Grimes {
2385b81b6b3SRodney W. Grimes 	unsigned fdt,st0, cyl;
2395b81b6b3SRodney W. Grimes 	int	hdr;
2405b81b6b3SRodney W. Grimes 	fdu_t	fdu;
2415b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
2425b81b6b3SRodney W. Grimes 	fdc_p	fdc = fdc_data + fdcu;
2435b81b6b3SRodney W. Grimes 	fd_p	fd;
2445b81b6b3SRodney W. Grimes 	int	fdsu;
2455b81b6b3SRodney W. Grimes 
2465b81b6b3SRodney W. Grimes 	fdc->fdcu = fdcu;
2475b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
2485b81b6b3SRodney W. Grimes 	fdc->dmachan = dev->id_drq;
2495b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
2505b81b6b3SRodney W. Grimes 
2515b81b6b3SRodney W. Grimes 	fdt = rtcin(RTC_FDISKETTE);
2525b81b6b3SRodney W. Grimes 	hdr = 0;
2535b81b6b3SRodney W. Grimes 
2545b81b6b3SRodney W. Grimes 	/* check for each floppy drive */
2555b81b6b3SRodney W. Grimes 	for (fdu = (fdcu * DRVS_PER_CTLR),fdsu = 0;
2565b81b6b3SRodney W. Grimes 	   ((fdu < NFD) && (fdsu < DRVS_PER_CTLR));
2575b81b6b3SRodney W. Grimes 	   fdu++,fdsu++)
2585b81b6b3SRodney W. Grimes 	{
2595b81b6b3SRodney W. Grimes 		/* is there a unit? */
260f5f7ba03SJordan K. Hubbard 		if ((fdt & 0xf0) == RTCFDT_NONE) {
261f5f7ba03SJordan K. Hubbard #define NO_TYPE NUMTYPES
262f5f7ba03SJordan K. Hubbard 			fd_data[fdu].type = NO_TYPE;
2635b81b6b3SRodney W. Grimes 			continue;
264f5f7ba03SJordan K. Hubbard 		}
2655b81b6b3SRodney W. Grimes 
2665b81b6b3SRodney W. Grimes #ifdef notyet
2675b81b6b3SRodney W. Grimes 		/* select it */
2685b81b6b3SRodney W. Grimes 		fd_turnon1(fdu);
2695b81b6b3SRodney W. Grimes 		spinwait(1000);	/* 1 sec */
2705b81b6b3SRodney W. Grimes 		out_fdc(fdcu,NE7CMD_RECAL);	/* Recalibrate Function */
2715b81b6b3SRodney W. Grimes 		out_fdc(fdcu,fdsu);
2725b81b6b3SRodney W. Grimes 		spinwait(1000);	/* 1 sec */
2735b81b6b3SRodney W. Grimes 
2745b81b6b3SRodney W. Grimes 		/* anything responding */
2755b81b6b3SRodney W. Grimes 		out_fdc(fdcu,NE7CMD_SENSEI);
2765b81b6b3SRodney W. Grimes 		st0 = in_fdc(fdcu);
2775b81b6b3SRodney W. Grimes 		cyl = in_fdc(fdcu);
2785b81b6b3SRodney W. Grimes 		if (st0 & 0xd0)
2795b81b6b3SRodney W. Grimes 			continue;
2805b81b6b3SRodney W. Grimes 
2815b81b6b3SRodney W. Grimes #endif
2825b81b6b3SRodney W. Grimes 		fd_data[fdu].track = -2;
2835b81b6b3SRodney W. Grimes 		fd_data[fdu].fdc = fdc;
2845b81b6b3SRodney W. Grimes 		fd_data[fdu].fdsu = fdsu;
2852a6c8980SDavid Greenman 		printf("fd%d: unit %d type ", fdcu, fdu);
2865b81b6b3SRodney W. Grimes 
2875b81b6b3SRodney W. Grimes 		if ((fdt & 0xf0) == RTCFDT_12M) {
2882a6c8980SDavid Greenman 			printf("1.2MB 5.25in\n");
2895b81b6b3SRodney W. Grimes 			fd_data[fdu].type = 1;
2905b81b6b3SRodney W. Grimes 			fd_data[fdu].ft = fd_types + 1;
2915b81b6b3SRodney W. Grimes 
2925b81b6b3SRodney W. Grimes 		}
2935b81b6b3SRodney W. Grimes 		if ((fdt & 0xf0) == RTCFDT_144M) {
2942a6c8980SDavid Greenman 			printf("1.44MB 3.5in\n");
2955b81b6b3SRodney W. Grimes 			fd_data[fdu].type = 0;
2965b81b6b3SRodney W. Grimes 			fd_data[fdu].ft = fd_types + 0;
2975b81b6b3SRodney W. Grimes 		}
2985b81b6b3SRodney W. Grimes 
2995b81b6b3SRodney W. Grimes 		fdt <<= 4;
300381fe1aaSGarrett Wollman 		fd_turnoff((caddr_t)fdu, 0);
3015b81b6b3SRodney W. Grimes 		hdr = 1;
3025b81b6b3SRodney W. Grimes 	}
3035b81b6b3SRodney W. Grimes 
3045b81b6b3SRodney W. Grimes 	/* Set transfer to 500kbps */
3055b81b6b3SRodney W. Grimes 	outb(fdc->baseport+fdctl,0); /*XXX*/
306381fe1aaSGarrett Wollman 	return 1;
3075b81b6b3SRodney W. Grimes }
3085b81b6b3SRodney W. Grimes 
3095b81b6b3SRodney W. Grimes int
3105b81b6b3SRodney W. Grimes fdsize(dev)
3115b81b6b3SRodney W. Grimes 	dev_t	dev;
3125b81b6b3SRodney W. Grimes {
3135b81b6b3SRodney W. Grimes 	return(0);
3145b81b6b3SRodney W. Grimes }
3155b81b6b3SRodney W. Grimes 
3165b81b6b3SRodney W. Grimes /****************************************************************************/
3175b81b6b3SRodney W. Grimes /*                               fdstrategy                                 */
3185b81b6b3SRodney W. Grimes /****************************************************************************/
319381fe1aaSGarrett Wollman void fdstrategy(struct buf *bp)
3205b81b6b3SRodney W. Grimes {
3215b81b6b3SRodney W. Grimes 	register struct buf *dp,*dp0,*dp1;
3225b81b6b3SRodney W. Grimes 	long nblocks,blknum;
3235b81b6b3SRodney W. Grimes  	int	s;
3245b81b6b3SRodney W. Grimes  	fdcu_t	fdcu;
3255b81b6b3SRodney W. Grimes  	fdu_t	fdu;
3265b81b6b3SRodney W. Grimes  	fdc_p	fdc;
3275b81b6b3SRodney W. Grimes  	fd_p	fd;
3285b81b6b3SRodney W. Grimes 
3295b81b6b3SRodney W. Grimes  	fdu = FDUNIT(minor(bp->b_dev));
3305b81b6b3SRodney W. Grimes 	fd = &fd_data[fdu];
3315b81b6b3SRodney W. Grimes 	fdc = fd->fdc;
3325b81b6b3SRodney W. Grimes 	fdcu = fdc->fdcu;
3335b81b6b3SRodney W. Grimes  	/*type = FDTYPE(minor(bp->b_dev));*/
3345b81b6b3SRodney W. Grimes 
3355b81b6b3SRodney W. Grimes 	if ((fdu >= NFD) || (bp->b_blkno < 0)) {
3365b81b6b3SRodney W. Grimes 		printf("fdstrat: fdu = %d, blkno = %d, bcount = %d\n",
3375b81b6b3SRodney W. Grimes 			fdu, bp->b_blkno, bp->b_bcount);
3385b81b6b3SRodney W. Grimes 		pg("fd:error in fdstrategy");
3395b81b6b3SRodney W. Grimes 		bp->b_error = EINVAL;
3405b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
3415b81b6b3SRodney W. Grimes 		goto bad;
3425b81b6b3SRodney W. Grimes 	}
3435b81b6b3SRodney W. Grimes 	/*
3445b81b6b3SRodney W. Grimes 	 * Set up block calculations.
3455b81b6b3SRodney W. Grimes 	 */
3465b81b6b3SRodney W. Grimes 	blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/FDBLK;
3475b81b6b3SRodney W. Grimes  	nblocks = fd->ft->size;
3485b81b6b3SRodney W. Grimes 	if (blknum + (bp->b_bcount / FDBLK) > nblocks) {
3495b81b6b3SRodney W. Grimes 		if (blknum == nblocks) {
3505b81b6b3SRodney W. Grimes 			bp->b_resid = bp->b_bcount;
3515b81b6b3SRodney W. Grimes 		} else {
3525b81b6b3SRodney W. Grimes 			bp->b_error = ENOSPC;
3535b81b6b3SRodney W. Grimes 			bp->b_flags |= B_ERROR;
3545b81b6b3SRodney W. Grimes 		}
3555b81b6b3SRodney W. Grimes 		goto bad;
3565b81b6b3SRodney W. Grimes 	}
3575b81b6b3SRodney W. Grimes  	bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads);
3585b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
3595b81b6b3SRodney W. Grimes 	s = splbio();
3605b81b6b3SRodney W. Grimes 	disksort(dp, bp);
3615b81b6b3SRodney W. Grimes 	untimeout(fd_turnoff,fdu); /* a good idea */
3625b81b6b3SRodney W. Grimes 	fdstart(fdcu);
3635b81b6b3SRodney W. Grimes 	splx(s);
3645b81b6b3SRodney W. Grimes 	return;
3655b81b6b3SRodney W. Grimes 
3665b81b6b3SRodney W. Grimes bad:
3675b81b6b3SRodney W. Grimes 	biodone(bp);
3685b81b6b3SRodney W. Grimes }
3695b81b6b3SRodney W. Grimes 
3705b81b6b3SRodney W. Grimes /****************************************************************************/
3715b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
3725b81b6b3SRodney W. Grimes /*		remember to not deselect the drive we're working on         */
3735b81b6b3SRodney W. Grimes /****************************************************************************/
374381fe1aaSGarrett Wollman void
375f5f7ba03SJordan K. Hubbard set_motor(fdcu, fdu, reset)
376f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
377f5f7ba03SJordan K. Hubbard 	fdu_t fdu;
378f5f7ba03SJordan K. Hubbard 	int reset;
3795b81b6b3SRodney W. Grimes {
3805b81b6b3SRodney W. Grimes 	int m0,m1;
3815b81b6b3SRodney W. Grimes 	int selunit;
3825b81b6b3SRodney W. Grimes 	fd_p fd;
3835b81b6b3SRodney W. Grimes 	if(fd = fdc_data[fdcu].fd)/* yes an assign! */
3845b81b6b3SRodney W. Grimes 	{
3855b81b6b3SRodney W. Grimes 		selunit =  fd->fdsu;
3865b81b6b3SRodney W. Grimes 	}
3875b81b6b3SRodney W. Grimes 	else
3885b81b6b3SRodney W. Grimes 	{
3895b81b6b3SRodney W. Grimes 		selunit = 0;
3905b81b6b3SRodney W. Grimes 	}
3915b81b6b3SRodney W. Grimes 	m0 = fd_data[fdcu * DRVS_PER_CTLR + 0].flags & FD_MOTOR;
3925b81b6b3SRodney W. Grimes 	m1 = fd_data[fdcu * DRVS_PER_CTLR + 1].flags & FD_MOTOR;
3935b81b6b3SRodney W. Grimes 	outb(fdc_data[fdcu].baseport+fdout,
3945b81b6b3SRodney W. Grimes 		selunit
3955b81b6b3SRodney W. Grimes 		| (reset ? 0 : (FDO_FRST|FDO_FDMAEN))
3965b81b6b3SRodney W. Grimes 		| (m0 ? FDO_MOEN0 : 0)
3975b81b6b3SRodney W. Grimes 		| (m1 ? FDO_MOEN1 : 0));
3985b81b6b3SRodney W. Grimes 	TRACE1("[0x%x->fdout]",(
3995b81b6b3SRodney W. Grimes 		selunit
4005b81b6b3SRodney W. Grimes 		| (reset ? 0 : (FDO_FRST|FDO_FDMAEN))
4015b81b6b3SRodney W. Grimes 		| (m0 ? FDO_MOEN0 : 0)
4025b81b6b3SRodney W. Grimes 		| (m1 ? FDO_MOEN1 : 0)));
4035b81b6b3SRodney W. Grimes }
4045b81b6b3SRodney W. Grimes 
405381fe1aaSGarrett Wollman static void
406381fe1aaSGarrett Wollman fd_turnoff(caddr_t arg1, int arg2)
4075b81b6b3SRodney W. Grimes {
408381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
409f5f7ba03SJordan K. Hubbard 	int	s;
410f5f7ba03SJordan K. Hubbard 
4115b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
412f5f7ba03SJordan K. Hubbard 	s = splbio();
4135b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
4145b81b6b3SRodney W. Grimes 	set_motor(fd->fdc->fdcu,fd->fdsu,0);
415f5f7ba03SJordan K. Hubbard 	splx(s);
4165b81b6b3SRodney W. Grimes }
4175b81b6b3SRodney W. Grimes 
418381fe1aaSGarrett Wollman void
419381fe1aaSGarrett Wollman fd_motor_on(caddr_t arg1, int arg2)
4205b81b6b3SRodney W. Grimes {
421381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
422f5f7ba03SJordan K. Hubbard 	int	s;
423f5f7ba03SJordan K. Hubbard 
4245b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
425f5f7ba03SJordan K. Hubbard 	s = splbio();
4265b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
4275b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
4285b81b6b3SRodney W. Grimes 	{
429f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
4305b81b6b3SRodney W. Grimes 	}
431f5f7ba03SJordan K. Hubbard 	splx(s);
4325b81b6b3SRodney W. Grimes }
4335b81b6b3SRodney W. Grimes 
434381fe1aaSGarrett Wollman static void fd_turnon1(fdu_t);
435381fe1aaSGarrett Wollman 
436381fe1aaSGarrett Wollman void
437f5f7ba03SJordan K. Hubbard fd_turnon(fdu)
438f5f7ba03SJordan K. Hubbard 	fdu_t fdu;
4395b81b6b3SRodney W. Grimes {
4405b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
4415b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
4425b81b6b3SRodney W. Grimes 	{
4435b81b6b3SRodney W. Grimes 		fd_turnon1(fdu);
4445b81b6b3SRodney W. Grimes 		fd->flags |= FD_MOTOR_WAIT;
445381fe1aaSGarrett Wollman 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
4465b81b6b3SRodney W. Grimes 	}
4475b81b6b3SRodney W. Grimes }
4485b81b6b3SRodney W. Grimes 
449381fe1aaSGarrett Wollman static void
450381fe1aaSGarrett Wollman fd_turnon1(fdu_t fdu)
4515b81b6b3SRodney W. Grimes {
4525b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
4535b81b6b3SRodney W. Grimes 	fd->flags |= FD_MOTOR;
4545b81b6b3SRodney W. Grimes 	set_motor(fd->fdc->fdcu,fd->fdsu,0);
4555b81b6b3SRodney W. Grimes }
4565b81b6b3SRodney W. Grimes 
4575b81b6b3SRodney W. Grimes /****************************************************************************/
4585b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
4595b81b6b3SRodney W. Grimes /****************************************************************************/
4605b81b6b3SRodney W. Grimes int
461f5f7ba03SJordan K. Hubbard in_fdc(fdcu)
462f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
4635b81b6b3SRodney W. Grimes {
4645b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
4655b81b6b3SRodney W. Grimes 	int i, j = 100000;
4665b81b6b3SRodney W. Grimes 	while ((i = inb(baseport+fdsts) & (NE7_DIO|NE7_RQM))
4675b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
4685b81b6b3SRodney W. Grimes 		if (i == NE7_RQM) return -1;
4695b81b6b3SRodney W. Grimes 	if (j <= 0)
4705b81b6b3SRodney W. Grimes 		return(-1);
4715b81b6b3SRodney W. Grimes #ifdef	DEBUG
4725b81b6b3SRodney W. Grimes 	i = inb(baseport+fddata);
4735b81b6b3SRodney W. Grimes 	TRACE1("[fddata->0x%x]",(unsigned char)i);
4745b81b6b3SRodney W. Grimes 	return(i);
4755b81b6b3SRodney W. Grimes #else
4765b81b6b3SRodney W. Grimes 	return inb(baseport+fddata);
4775b81b6b3SRodney W. Grimes #endif
4785b81b6b3SRodney W. Grimes }
4795b81b6b3SRodney W. Grimes 
480381fe1aaSGarrett Wollman int
481f5f7ba03SJordan K. Hubbard out_fdc(fdcu, x)
482f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
483f5f7ba03SJordan K. Hubbard 	int x;
4845b81b6b3SRodney W. Grimes {
4855b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
4863b3837dbSRodney W. Grimes 	int i;
4875b81b6b3SRodney W. Grimes 
4883b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
4893b3837dbSRodney W. Grimes 	i = 100000;
4905b81b6b3SRodney W. Grimes 	while ((inb(baseport+fdsts) & NE7_DIO) && i-- > 0);
4913b3837dbSRodney W. Grimes 	if (i <= 0) return (-1);	/* Floppy timed out */
4923b3837dbSRodney W. Grimes 
4933b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
4943b3837dbSRodney W. Grimes 	i = 100000;
4955b81b6b3SRodney W. Grimes 	while ((inb(baseport+fdsts) & NE7_RQM) == 0 && i-- > 0);
4963b3837dbSRodney W. Grimes 	if (i <= 0) return (-1);	/* Floppy timed out */
4973b3837dbSRodney W. Grimes 
4983b3837dbSRodney W. Grimes 	/* Send the command and return */
4995b81b6b3SRodney W. Grimes 	outb(baseport+fddata,x);
5005b81b6b3SRodney W. Grimes 	TRACE1("[0x%x->fddata]",x);
5015b81b6b3SRodney W. Grimes 	return (0);
5025b81b6b3SRodney W. Grimes }
5035b81b6b3SRodney W. Grimes 
5045b81b6b3SRodney W. Grimes /****************************************************************************/
5055b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
5065b81b6b3SRodney W. Grimes /****************************************************************************/
507381fe1aaSGarrett Wollman int
5085b81b6b3SRodney W. Grimes Fdopen(dev, flags)
5095b81b6b3SRodney W. Grimes 	dev_t	dev;
5105b81b6b3SRodney W. Grimes 	int	flags;
5115b81b6b3SRodney W. Grimes {
5125b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
51320a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
5145b81b6b3SRodney W. Grimes 	int s;
5155b81b6b3SRodney W. Grimes 
5165b81b6b3SRodney W. Grimes 	/* check bounds */
517fadc21aeSAndreas Schulz 	if (fdu >= NFD || fd_data[fdu].fdc == NULL
518fadc21aeSAndreas Schulz 		|| fd_data[fdu].type == NO_TYPE) return(ENXIO);
51920a29168SAndrey A. Chernov 	if (type >= NUMTYPES) return(ENXIO);
5205b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
5215b81b6b3SRodney W. Grimes 
5225b81b6b3SRodney W. Grimes 	return 0;
5235b81b6b3SRodney W. Grimes }
5245b81b6b3SRodney W. Grimes 
525381fe1aaSGarrett Wollman int
5265b81b6b3SRodney W. Grimes fdclose(dev, flags)
5275b81b6b3SRodney W. Grimes 	dev_t dev;
528381fe1aaSGarrett Wollman 	int flags;
5295b81b6b3SRodney W. Grimes {
5305b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
5315b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
5325b81b6b3SRodney W. Grimes 	return(0);
5335b81b6b3SRodney W. Grimes }
5345b81b6b3SRodney W. Grimes 
5355b81b6b3SRodney W. Grimes 
5365b81b6b3SRodney W. Grimes /***************************************************************\
5375b81b6b3SRodney W. Grimes *				fdstart				*
5385b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
5395b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
5405b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
5415b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
5425b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
5435b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
5445b81b6b3SRodney W. Grimes \***************************************************************/
545381fe1aaSGarrett Wollman static void
546f5f7ba03SJordan K. Hubbard fdstart(fdcu)
547f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
5485b81b6b3SRodney W. Grimes {
5495b81b6b3SRodney W. Grimes 	register struct buf *dp,*bp;
5505b81b6b3SRodney W. Grimes 	int s;
5515b81b6b3SRodney W. Grimes  	fdu_t fdu;
5525b81b6b3SRodney W. Grimes 
5535b81b6b3SRodney W. Grimes 	s = splbio();
5545b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
5555b81b6b3SRodney W. Grimes 	{
5565b81b6b3SRodney W. Grimes 		fdintr(fdcu);
5575b81b6b3SRodney W. Grimes 	}
5585b81b6b3SRodney W. Grimes 	splx(s);
5595b81b6b3SRodney W. Grimes }
5605b81b6b3SRodney W. Grimes 
561381fe1aaSGarrett Wollman static void
562381fe1aaSGarrett Wollman fd_timeout(caddr_t arg1, int arg2)
5635b81b6b3SRodney W. Grimes {
564381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
5655b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
5665b81b6b3SRodney W. Grimes 	int st0, st3, cyl;
5675b81b6b3SRodney W. Grimes 	struct buf *dp,*bp;
568f5f7ba03SJordan K. Hubbard 	int	s;
5695b81b6b3SRodney W. Grimes 
5705b81b6b3SRodney W. Grimes 	dp = &fdc_data[fdcu].head;
571f5f7ba03SJordan K. Hubbard 	s = splbio();
5725b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
5735b81b6b3SRodney W. Grimes 
5745b81b6b3SRodney W. Grimes 	out_fdc(fdcu,NE7CMD_SENSED);
5755b81b6b3SRodney W. Grimes 	out_fdc(fdcu,fd_data[fdu].hddrv);
5765b81b6b3SRodney W. Grimes 	st3 = in_fdc(fdcu);
5775b81b6b3SRodney W. Grimes 
5785b81b6b3SRodney W. Grimes 	out_fdc(fdcu,NE7CMD_SENSEI);
5795b81b6b3SRodney W. Grimes 	st0 = in_fdc(fdcu);
5805b81b6b3SRodney W. Grimes 	cyl = in_fdc(fdcu);
5815b81b6b3SRodney W. Grimes 	printf("fd%d: Operation timeout ST0 %b cyl %d ST3 %b\n",
5825b81b6b3SRodney W. Grimes 			fdu,
5835b81b6b3SRodney W. Grimes 			st0,
5845b81b6b3SRodney W. Grimes 			NE7_ST0BITS,
5855b81b6b3SRodney W. Grimes 			cyl,
5865b81b6b3SRodney W. Grimes 			st3,
5875b81b6b3SRodney W. Grimes 			NE7_ST3BITS);
5885b81b6b3SRodney W. Grimes 
5895b81b6b3SRodney W. Grimes 	if (bp)
5905b81b6b3SRodney W. Grimes 	{
5915b81b6b3SRodney W. Grimes 		retrier(fdcu);
5925b81b6b3SRodney W. Grimes 		fdc_data[fdcu].status[0] = 0xc0;
5935b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
5945b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
5955b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
5965b81b6b3SRodney W. Grimes 	}
5975b81b6b3SRodney W. Grimes 	else
5985b81b6b3SRodney W. Grimes 	{
5995b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
6005b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
6015b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
6025b81b6b3SRodney W. Grimes 	}
603f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
604f5f7ba03SJordan K. Hubbard 	splx(s);
6055b81b6b3SRodney W. Grimes }
6065b81b6b3SRodney W. Grimes 
6075b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
608381fe1aaSGarrett Wollman static void
609381fe1aaSGarrett Wollman fd_pseudointr(caddr_t arg1, int arg2)
6105b81b6b3SRodney W. Grimes {
611381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
6125b81b6b3SRodney W. Grimes 	int	s;
6135b81b6b3SRodney W. Grimes 	s = splbio();
6145b81b6b3SRodney W. Grimes 	fdintr(fdcu);
6155b81b6b3SRodney W. Grimes 	splx(s);
6165b81b6b3SRodney W. Grimes }
6175b81b6b3SRodney W. Grimes 
6185b81b6b3SRodney W. Grimes /***********************************************************************\
6195b81b6b3SRodney W. Grimes *                                 fdintr				*
6205b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
6215b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
6225b81b6b3SRodney W. Grimes \***********************************************************************/
623381fe1aaSGarrett Wollman void
624381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
6255b81b6b3SRodney W. Grimes {
6265b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
627381fe1aaSGarrett Wollman 	while(fdstate(fdcu, fdc))
628381fe1aaSGarrett Wollman 	  ;
6295b81b6b3SRodney W. Grimes }
6305b81b6b3SRodney W. Grimes 
6315b81b6b3SRodney W. Grimes /***********************************************************************\
6325b81b6b3SRodney W. Grimes * The controller state machine.						*
6335b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
6345b81b6b3SRodney W. Grimes \***********************************************************************/
635381fe1aaSGarrett Wollman int
636381fe1aaSGarrett Wollman fdstate(fdcu, fdc)
637f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
638f5f7ba03SJordan K. Hubbard 	fdc_p fdc;
6395b81b6b3SRodney W. Grimes {
640381fe1aaSGarrett Wollman 	int read, head, trac, sec = 0, i = 0, s, sectrac, cyl, st0;
6415b81b6b3SRodney W. Grimes 	unsigned long blknum;
6425b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
6435b81b6b3SRodney W. Grimes 	fd_p fd;
6445b81b6b3SRodney W. Grimes 	register struct buf *dp,*bp;
6455b81b6b3SRodney W. Grimes 
6465b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
6475b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
6485b81b6b3SRodney W. Grimes 	if(!bp)
6495b81b6b3SRodney W. Grimes 	{
6505b81b6b3SRodney W. Grimes 		/***********************************************\
6515b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
6525b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
6535b81b6b3SRodney W. Grimes 		\***********************************************/
6545b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
6555b81b6b3SRodney W. Grimes 		if(fdc->fd)
6565b81b6b3SRodney W. Grimes 		{
6575b81b6b3SRodney W. Grimes 			printf("unexpected valid fd pointer (fdu = %d)\n"
6585b81b6b3SRodney W. Grimes 						,fdc->fdu);
6595b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
6605b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
6615b81b6b3SRodney W. Grimes 		}
6625b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]",fdcu);
6635b81b6b3SRodney W. Grimes  		return(0);
6645b81b6b3SRodney W. Grimes 	}
6655b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
6665b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
6675b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
6685b81b6b3SRodney W. Grimes 	{
6695b81b6b3SRodney W. Grimes 		printf("confused fd pointers\n");
6705b81b6b3SRodney W. Grimes 	}
6715b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
6725b81b6b3SRodney W. Grimes 	TRACE1("fd%d",fdu);
6735b81b6b3SRodney W. Grimes 	TRACE1("[%s]",fdstates[fdc->state]);
6745b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)",fd->flags);
6755b81b6b3SRodney W. Grimes 	untimeout(fd_turnoff, fdu);
676381fe1aaSGarrett Wollman 	timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
6775b81b6b3SRodney W. Grimes 	switch (fdc->state)
6785b81b6b3SRodney W. Grimes 	{
6795b81b6b3SRodney W. Grimes 	case DEVIDLE:
6805b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
6815b81b6b3SRodney W. Grimes 		fdc->retry = 0;
6825b81b6b3SRodney W. Grimes 		fd->skip = 0;
6835b81b6b3SRodney W. Grimes 		fdc->fd = fd;
6845b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
6855b81b6b3SRodney W. Grimes 		/*******************************************************\
6865b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
6875b81b6b3SRodney W. Grimes 		* it will start up in it's own good time		*
6885b81b6b3SRodney W. Grimes 		\*******************************************************/
6895b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
6905b81b6b3SRodney W. Grimes 		{
6915b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
6925b81b6b3SRodney W. Grimes 			return(0); /* come back later */
6935b81b6b3SRodney W. Grimes 		}
6945b81b6b3SRodney W. Grimes 		/*******************************************************\
6955b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
6965b81b6b3SRodney W. Grimes 		\*******************************************************/
6975b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
6985b81b6b3SRodney W. Grimes 		{
6995b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
7005b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
7015b81b6b3SRodney W. Grimes 			return(0);
7025b81b6b3SRodney W. Grimes 		}
7035b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
7045b81b6b3SRodney W. Grimes 		{
7055b81b6b3SRodney W. Grimes 			set_motor(fdcu,fd->fdsu,0);
7065b81b6b3SRodney W. Grimes 		}
7075b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
7085b81b6b3SRodney W. Grimes 		break;
7095b81b6b3SRodney W. Grimes 	case DOSEEK:
7105b81b6b3SRodney W. Grimes 		if (bp->b_cylin == fd->track)
7115b81b6b3SRodney W. Grimes 		{
7125b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
7135b81b6b3SRodney W. Grimes 			break;
7145b81b6b3SRodney W. Grimes 		}
7155b81b6b3SRodney W. Grimes 		out_fdc(fdcu,NE7CMD_SEEK);	/* Seek function */
7165b81b6b3SRodney W. Grimes 		out_fdc(fdcu,fd->fdsu);		/* Drive number */
7175b81b6b3SRodney W. Grimes 		out_fdc(fdcu,bp->b_cylin * fd->ft->steptrac);
7185b81b6b3SRodney W. Grimes 		fd->track = -2;
7195b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
720381fe1aaSGarrett Wollman 		timeout(fd_timeout, (caddr_t)fdcu, 2 * hz);
7215b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
7225b81b6b3SRodney W. Grimes 	case SEEKWAIT:
723f5f7ba03SJordan K. Hubbard 		untimeout(fd_timeout,fdcu);
7245b81b6b3SRodney W. Grimes 		/* allow heads to settle */
725381fe1aaSGarrett Wollman 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 50);
7265b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
7275b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
7285b81b6b3SRodney W. Grimes 		break;
7295b81b6b3SRodney W. Grimes 
7305b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
7315b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
7325b81b6b3SRodney W. Grimes 		if(fd->track == -2)
7335b81b6b3SRodney W. Grimes 		{
7345b81b6b3SRodney W. Grimes 			int descyl = bp->b_cylin * fd->ft->steptrac;
7355b81b6b3SRodney W. Grimes 			out_fdc(fdcu,NE7CMD_SENSEI);
7365b81b6b3SRodney W. Grimes 			i = in_fdc(fdcu);
7375b81b6b3SRodney W. Grimes 			cyl = in_fdc(fdcu);
7385b81b6b3SRodney W. Grimes 			if (cyl != descyl)
7395b81b6b3SRodney W. Grimes 			{
7405b81b6b3SRodney W. Grimes 				printf("fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", fdu,
7415b81b6b3SRodney W. Grimes 				descyl, cyl, i, NE7_ST0BITS);
7425b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
7435b81b6b3SRodney W. Grimes 			}
7445b81b6b3SRodney W. Grimes 		}
7455b81b6b3SRodney W. Grimes 
7465b81b6b3SRodney W. Grimes 		fd->track = bp->b_cylin;
7475b81b6b3SRodney W. Grimes 		isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip,
7485b81b6b3SRodney W. Grimes 			FDBLK, fdc->dmachan);
7495b81b6b3SRodney W. Grimes 		blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/FDBLK
7505b81b6b3SRodney W. Grimes 			+ fd->skip/FDBLK;
7515b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
7525b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
7535b81b6b3SRodney W. Grimes 		head = sec / sectrac;
7545b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
7555b81b6b3SRodney W. Grimes /*XXX*/		fd->hddrv = ((head&1)<<2)+fdu;
7565b81b6b3SRodney W. Grimes 
7575b81b6b3SRodney W. Grimes 		if (read)
7585b81b6b3SRodney W. Grimes 		{
7595b81b6b3SRodney W. Grimes 			out_fdc(fdcu,NE7CMD_READ);	/* READ */
7605b81b6b3SRodney W. Grimes 		}
7615b81b6b3SRodney W. Grimes 		else
7625b81b6b3SRodney W. Grimes 		{
7635b81b6b3SRodney W. Grimes 			out_fdc(fdcu,NE7CMD_WRITE);	/* WRITE */
7645b81b6b3SRodney W. Grimes 		}
7655b81b6b3SRodney W. Grimes 		out_fdc(fdcu,head << 2 | fdu);	/* head & unit */
7665b81b6b3SRodney W. Grimes 		out_fdc(fdcu,fd->track);		/* track */
7675b81b6b3SRodney W. Grimes 		out_fdc(fdcu,head);
7685b81b6b3SRodney W. Grimes 		out_fdc(fdcu,sec);			/* sector XXX +1? */
7695b81b6b3SRodney W. Grimes 		out_fdc(fdcu,fd->ft->secsize);		/* sector size */
7705b81b6b3SRodney W. Grimes 		out_fdc(fdcu,sectrac);		/* sectors/track */
7715b81b6b3SRodney W. Grimes 		out_fdc(fdcu,fd->ft->gap);		/* gap size */
7725b81b6b3SRodney W. Grimes 		out_fdc(fdcu,fd->ft->datalen);		/* data length */
7735b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
774381fe1aaSGarrett Wollman 		timeout(fd_timeout, (caddr_t)fdcu, 2 * hz);
7755b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
7765b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
7775b81b6b3SRodney W. Grimes 		untimeout(fd_timeout,fdcu);
7785b81b6b3SRodney W. Grimes 		for(i=0;i<7;i++)
7795b81b6b3SRodney W. Grimes 		{
7805b81b6b3SRodney W. Grimes 			fdc->status[i] = in_fdc(fdcu);
7815b81b6b3SRodney W. Grimes 		}
7825b81b6b3SRodney W. Grimes 	case IOTIMEDOUT: /*XXX*/
7835b81b6b3SRodney W. Grimes 		isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip,
7845b81b6b3SRodney W. Grimes 			FDBLK, fdc->dmachan);
7855b81b6b3SRodney W. Grimes 		if (fdc->status[0]&0xF8)
7865b81b6b3SRodney W. Grimes 		{
7875b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
7885b81b6b3SRodney W. Grimes 		}
7895b81b6b3SRodney W. Grimes 		/* All OK */
7905b81b6b3SRodney W. Grimes 		fd->skip += FDBLK;
7915b81b6b3SRodney W. Grimes 		if (fd->skip < bp->b_bcount)
7925b81b6b3SRodney W. Grimes 		{
7935b81b6b3SRodney W. Grimes 			/* set up next transfer */
7945b81b6b3SRodney W. Grimes 			blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/FDBLK
7955b81b6b3SRodney W. Grimes 				+ fd->skip/FDBLK;
7965b81b6b3SRodney W. Grimes 			bp->b_cylin = (blknum / (fd->ft->sectrac * fd->ft->heads));
7975b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
7985b81b6b3SRodney W. Grimes 		}
7995b81b6b3SRodney W. Grimes 		else
8005b81b6b3SRodney W. Grimes 		{
8015b81b6b3SRodney W. Grimes 			/* ALL DONE */
8025b81b6b3SRodney W. Grimes 			fd->skip = 0;
8035b81b6b3SRodney W. Grimes 			bp->b_resid = 0;
8045b81b6b3SRodney W. Grimes 			dp->b_actf = bp->av_forw;
8055b81b6b3SRodney W. Grimes 			biodone(bp);
8065b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
8075b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
8085b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
8095b81b6b3SRodney W. Grimes 		}
8105b81b6b3SRodney W. Grimes 		return(1);
8115b81b6b3SRodney W. Grimes 	case RESETCTLR:
8125b81b6b3SRodney W. Grimes 		/* Try a reset, keep motor on */
8135b81b6b3SRodney W. Grimes 		set_motor(fdcu,fd->fdsu,1);
8145b81b6b3SRodney W. Grimes 		DELAY(100);
8155b81b6b3SRodney W. Grimes 		set_motor(fdcu,fd->fdsu,0);
8165b81b6b3SRodney W. Grimes 		outb(fdc->baseport+fdctl,fd->ft->trans);
8175b81b6b3SRodney W. Grimes 		TRACE1("[0x%x->fdctl]",fd->ft->trans);
8185b81b6b3SRodney W. Grimes 		fdc->retry++;
8195b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
8205b81b6b3SRodney W. Grimes 		break;
8215b81b6b3SRodney W. Grimes 	case STARTRECAL:
8225b81b6b3SRodney W. Grimes 		out_fdc(fdcu,NE7CMD_SPECIFY); /* specify command */
8235b81b6b3SRodney W. Grimes 		out_fdc(fdcu,0xDF);
8245b81b6b3SRodney W. Grimes 		out_fdc(fdcu,2);
8255b81b6b3SRodney W. Grimes 		out_fdc(fdcu,NE7CMD_RECAL);	/* Recalibrate Function */
8265b81b6b3SRodney W. Grimes 		out_fdc(fdcu,fdu);
8275b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
8285b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
8295b81b6b3SRodney W. Grimes 	case RECALWAIT:
8305b81b6b3SRodney W. Grimes 		/* allow heads to settle */
831381fe1aaSGarrett Wollman 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 30);
8325b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
8335b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
8345b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
8355b81b6b3SRodney W. Grimes 		out_fdc(fdcu,NE7CMD_SENSEI);
8365b81b6b3SRodney W. Grimes 		st0 = in_fdc(fdcu);
8375b81b6b3SRodney W. Grimes 		cyl = in_fdc(fdcu);
8385b81b6b3SRodney W. Grimes 		if (cyl != 0)
8395b81b6b3SRodney W. Grimes 		{
8405b81b6b3SRodney W. Grimes 			printf("fd%d: recal failed ST0 %b cyl %d\n", fdu,
8415b81b6b3SRodney W. Grimes 				st0, NE7_ST0BITS, cyl);
8425b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
8435b81b6b3SRodney W. Grimes 		}
8445b81b6b3SRodney W. Grimes 		fd->track = 0;
8455b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
8465b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
8475b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
8485b81b6b3SRodney W. Grimes 	case	MOTORWAIT:
8495b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
8505b81b6b3SRodney W. Grimes 		{
8515b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
8525b81b6b3SRodney W. Grimes 		}
8535b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
8545b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
8555b81b6b3SRodney W. Grimes 	default:
8565b81b6b3SRodney W. Grimes 		printf("Unexpected FD int->");
8575b81b6b3SRodney W. Grimes 		out_fdc(fdcu,NE7CMD_SENSEI);
8585b81b6b3SRodney W. Grimes 		st0 = in_fdc(fdcu);
8595b81b6b3SRodney W. Grimes 		cyl = in_fdc(fdcu);
8605b81b6b3SRodney W. Grimes 		printf("ST0 = %lx, PCN = %lx\n",i,sec);
8615b81b6b3SRodney W. Grimes 		out_fdc(fdcu,0x4A);
8625b81b6b3SRodney W. Grimes 		out_fdc(fdcu,fd->fdsu);
8635b81b6b3SRodney W. Grimes 		for(i=0;i<7;i++) {
8645b81b6b3SRodney W. Grimes 			fdc->status[i] = in_fdc(fdcu);
8655b81b6b3SRodney W. Grimes 		}
8665b81b6b3SRodney W. Grimes 	printf("intr status :%lx %lx %lx %lx %lx %lx %lx ",
8675b81b6b3SRodney W. Grimes 		fdc->status[0],
8685b81b6b3SRodney W. Grimes 		fdc->status[1],
8695b81b6b3SRodney W. Grimes 		fdc->status[2],
8705b81b6b3SRodney W. Grimes 		fdc->status[3],
8715b81b6b3SRodney W. Grimes 		fdc->status[4],
8725b81b6b3SRodney W. Grimes 		fdc->status[5],
8735b81b6b3SRodney W. Grimes 		fdc->status[6] );
8745b81b6b3SRodney W. Grimes 		return(0);
8755b81b6b3SRodney W. Grimes 	}
8765b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
8775b81b6b3SRodney W. Grimes }
8785b81b6b3SRodney W. Grimes 
879381fe1aaSGarrett Wollman int
880f5f7ba03SJordan K. Hubbard retrier(fdcu)
881f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
8825b81b6b3SRodney W. Grimes {
8835b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
8845b81b6b3SRodney W. Grimes 	register struct buf *dp,*bp;
8855b81b6b3SRodney W. Grimes 
8865b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
8875b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
8885b81b6b3SRodney W. Grimes 
8895b81b6b3SRodney W. Grimes 	switch(fdc->retry)
8905b81b6b3SRodney W. Grimes 	{
8915b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
8925b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
8935b81b6b3SRodney W. Grimes 		break;
8945b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
8955b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
8965b81b6b3SRodney W. Grimes 		break;
8975b81b6b3SRodney W. Grimes 	case 6:
8985b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
8995b81b6b3SRodney W. Grimes 		break;
9005b81b6b3SRodney W. Grimes 	case 7:
9015b81b6b3SRodney W. Grimes 		break;
9025b81b6b3SRodney W. Grimes 	default:
9035b81b6b3SRodney W. Grimes 		{
90492ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
90592ed385aSRodney W. Grimes 				fdc->fd->skip, (struct disklabel *)NULL);
90692ed385aSRodney W. Grimes 			printf(" (ST0 %b ", fdc->status[0], NE7_ST0BITS);
9075b81b6b3SRodney W. Grimes 			printf(" ST1 %b ", fdc->status[1], NE7_ST1BITS);
9085b81b6b3SRodney W. Grimes 			printf(" ST2 %b ", fdc->status[2], NE7_ST2BITS);
9095b81b6b3SRodney W. Grimes 			printf("cyl %d hd %d sec %d)\n",
91092ed385aSRodney W. Grimes 			       fdc->status[3], fdc->status[4], fdc->status[5]);
9115b81b6b3SRodney W. Grimes 		}
9125b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
9135b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
9145b81b6b3SRodney W. Grimes 		bp->b_resid = bp->b_bcount - fdc->fd->skip;
9155b81b6b3SRodney W. Grimes 		dp->b_actf = bp->av_forw;
9165b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
9175b81b6b3SRodney W. Grimes 		biodone(bp);
91892ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
9195b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
9205b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
921f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
92292ed385aSRodney W. Grimes 		return(1);
9235b81b6b3SRodney W. Grimes 	}
9245b81b6b3SRodney W. Grimes 	fdc->retry++;
9255b81b6b3SRodney W. Grimes 	return(1);
9265b81b6b3SRodney W. Grimes }
9275b81b6b3SRodney W. Grimes 
928f5f7ba03SJordan K. Hubbard /*
929f5f7ba03SJordan K. Hubbard  * fdioctl() from jc@irbs.UUCP (John Capo)
930f5f7ba03SJordan K. Hubbard  * i386/i386/conf.c needs to have fdioctl() declared and remove the line that
931f5f7ba03SJordan K. Hubbard  * defines fdioctl to be enxio.
932f5f7ba03SJordan K. Hubbard  *
933f5f7ba03SJordan K. Hubbard  * TODO: Reformat.
934f5f7ba03SJordan K. Hubbard  *       Think about allocating buffer off stack.
935f5f7ba03SJordan K. Hubbard  *       Don't pass uncast 0's and NULL's to read/write/setdisklabel().
936f5f7ba03SJordan K. Hubbard  *       Watch out for NetBSD's different *disklabel() interface.
937f5f7ba03SJordan K. Hubbard  */
9385b81b6b3SRodney W. Grimes 
939f5f7ba03SJordan K. Hubbard int
940f5f7ba03SJordan K. Hubbard fdioctl (dev, cmd, addr, flag)
941f5f7ba03SJordan K. Hubbard dev_t dev;
942f5f7ba03SJordan K. Hubbard int cmd;
943f5f7ba03SJordan K. Hubbard caddr_t addr;
944f5f7ba03SJordan K. Hubbard int flag;
945f5f7ba03SJordan K. Hubbard {
946f5f7ba03SJordan K. Hubbard     struct fd_type *fdt;
947f5f7ba03SJordan K. Hubbard     struct disklabel *dl;
948f5f7ba03SJordan K. Hubbard     char buffer[DEV_BSIZE];
949f5f7ba03SJordan K. Hubbard     int error;
950f5f7ba03SJordan K. Hubbard 
951f5f7ba03SJordan K. Hubbard     error = 0;
952f5f7ba03SJordan K. Hubbard 
953f5f7ba03SJordan K. Hubbard     switch (cmd)
954f5f7ba03SJordan K. Hubbard     {
955f5f7ba03SJordan K. Hubbard     case DIOCGDINFO:
956f5f7ba03SJordan K. Hubbard         bzero(buffer, sizeof (buffer));
957f5f7ba03SJordan K. Hubbard         dl = (struct disklabel *)buffer;
958f5f7ba03SJordan K. Hubbard         dl->d_secsize = FDBLK;
95992ed385aSRodney W. Grimes         fdt = fd_data[FDUNIT(minor(dev))].ft;
960f5f7ba03SJordan K. Hubbard         dl->d_secpercyl = fdt->size / fdt->tracks;
961f5f7ba03SJordan K. Hubbard         dl->d_type = DTYPE_FLOPPY;
962f5f7ba03SJordan K. Hubbard 
963f5f7ba03SJordan K. Hubbard         if (readdisklabel(dev, fdstrategy, dl, NULL, 0, 0) == NULL)
964f5f7ba03SJordan K. Hubbard             error = 0;
965f5f7ba03SJordan K. Hubbard         else
966f5f7ba03SJordan K. Hubbard             error = EINVAL;
967f5f7ba03SJordan K. Hubbard 
968f5f7ba03SJordan K. Hubbard         *(struct disklabel *)addr = *dl;
969f5f7ba03SJordan K. Hubbard         break;
970f5f7ba03SJordan K. Hubbard 
971f5f7ba03SJordan K. Hubbard     case DIOCSDINFO:
972f5f7ba03SJordan K. Hubbard 
973f5f7ba03SJordan K. Hubbard         if ((flag & FWRITE) == 0)
974f5f7ba03SJordan K. Hubbard             error = EBADF;
975f5f7ba03SJordan K. Hubbard 
976f5f7ba03SJordan K. Hubbard         break;
977f5f7ba03SJordan K. Hubbard 
978f5f7ba03SJordan K. Hubbard     case DIOCWLABEL:
979f5f7ba03SJordan K. Hubbard         if ((flag & FWRITE) == 0)
980f5f7ba03SJordan K. Hubbard             error = EBADF;
981f5f7ba03SJordan K. Hubbard 
982f5f7ba03SJordan K. Hubbard         break;
983f5f7ba03SJordan K. Hubbard 
984f5f7ba03SJordan K. Hubbard     case DIOCWDINFO:
985f5f7ba03SJordan K. Hubbard         if ((flag & FWRITE) == 0)
986f5f7ba03SJordan K. Hubbard         {
987f5f7ba03SJordan K. Hubbard             error = EBADF;
988f5f7ba03SJordan K. Hubbard             break;
989f5f7ba03SJordan K. Hubbard         }
990f5f7ba03SJordan K. Hubbard 
991f5f7ba03SJordan K. Hubbard         dl = (struct disklabel *)addr;
992f5f7ba03SJordan K. Hubbard 
993f5f7ba03SJordan K. Hubbard         if (error = setdisklabel ((struct disklabel *)buffer, dl, 0, NULL))
994f5f7ba03SJordan K. Hubbard             break;
995f5f7ba03SJordan K. Hubbard 
996f5f7ba03SJordan K. Hubbard         error = writedisklabel(dev, fdstrategy, (struct disklabel *)buffer, NULL);
997f5f7ba03SJordan K. Hubbard         break;
998f5f7ba03SJordan K. Hubbard 
999f5f7ba03SJordan K. Hubbard      default:
1000f5f7ba03SJordan K. Hubbard         error = EINVAL;
1001f5f7ba03SJordan K. Hubbard         break;
1002f5f7ba03SJordan K. Hubbard     }
1003f5f7ba03SJordan K. Hubbard     return (error);
1004f5f7ba03SJordan K. Hubbard }
1005f5f7ba03SJordan K. Hubbard 
1006f5f7ba03SJordan K. Hubbard #endif
1007