Home | History | Annotate | Line # | Download | only in dev
fd.c revision 1.38
      1 /*	$NetBSD: fd.c,v 1.38 1996/10/11 00:46:40 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994, 1995 Charles Hannum.
      5  * Copyright (c) 1995 Paul Kranenburg.
      6  * Copyright (c) 1990 The Regents of the University of California.
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to Berkeley by
     10  * Don Ahn.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/file.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/device.h>
     49 #include <sys/disklabel.h>
     50 #include <sys/dkstat.h>
     51 #include <sys/disk.h>
     52 #include <sys/buf.h>
     53 #include <sys/uio.h>
     54 #include <sys/stat.h>
     55 #include <sys/syslog.h>
     56 #include <sys/queue.h>
     57 #include <sys/conf.h>
     58 
     59 #include <dev/cons.h>
     60 
     61 #include <machine/cpu.h>
     62 #include <machine/autoconf.h>
     63 #include <machine/conf.h>
     64 
     65 #include <sparc/sparc/auxreg.h>
     66 #include <sparc/dev/fdreg.h>
     67 #include <sparc/dev/fdvar.h>
     68 
     69 #define FDUNIT(dev)	(minor(dev) / 8)
     70 #define FDTYPE(dev)	(minor(dev) % 8)
     71 
     72 #define b_cylin b_resid
     73 
     74 #define FD_DEBUG
     75 #ifdef FD_DEBUG
     76 int	fdc_debug = 0;
     77 #endif
     78 
     79 enum fdc_state {
     80 	DEVIDLE = 0,
     81 	MOTORWAIT,
     82 	DOSEEK,
     83 	SEEKWAIT,
     84 	SEEKTIMEDOUT,
     85 	SEEKCOMPLETE,
     86 	DOIO,
     87 	IOCOMPLETE,
     88 	IOTIMEDOUT,
     89 	DORESET,
     90 	RESETCOMPLETE,
     91 	RESETTIMEDOUT,
     92 	DORECAL,
     93 	RECALWAIT,
     94 	RECALTIMEDOUT,
     95 	RECALCOMPLETE,
     96 };
     97 
     98 /* software state, per controller */
     99 struct fdc_softc {
    100 	struct device	sc_dev;		/* boilerplate */
    101 	struct intrhand sc_sih;
    102 	struct intrhand sc_hih;
    103 	caddr_t		sc_reg;
    104 	struct fd_softc *sc_fd[4];	/* pointers to children */
    105 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
    106 	enum fdc_state	sc_state;
    107 	int		sc_flags;
    108 #define FDC_82077		0x01
    109 #define FDC_NEEDHEADSETTLE	0x02
    110 #define FDC_EIS			0x04
    111 	int		sc_errors;		/* number of retries so far */
    112 	int		sc_overruns;		/* number of DMA overruns */
    113 	int		sc_cfg;			/* current configuration */
    114 	struct fdcio	sc_io;
    115 #define sc_reg_msr	sc_io.fdcio_reg_msr
    116 #define sc_reg_fifo	sc_io.fdcio_reg_fifo
    117 #define sc_reg_dor	sc_io.fdcio_reg_dor
    118 #define sc_reg_drs	sc_io.fdcio_reg_msr
    119 #define sc_istate	sc_io.fdcio_istate
    120 #define sc_data		sc_io.fdcio_data
    121 #define sc_tc		sc_io.fdcio_tc
    122 #define sc_nstat	sc_io.fdcio_nstat
    123 #define sc_status	sc_io.fdcio_status
    124 #define sc_intrcnt	sc_io.fdcio_intrcnt
    125 };
    126 
    127 #ifndef FDC_C_HANDLER
    128 extern	struct fdcio	*fdciop;
    129 #endif
    130 
    131 /* controller driver configuration */
    132 int	fdcmatch __P((struct device *, void *, void *));
    133 void	fdcattach __P((struct device *, struct device *, void *));
    134 
    135 struct cfattach fdc_ca = {
    136 	sizeof(struct fdc_softc), fdcmatch, fdcattach
    137 };
    138 
    139 struct cfdriver fdc_cd = {
    140 	NULL, "fdc", DV_DULL
    141 };
    142 
    143 __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
    144 
    145 /*
    146  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    147  * we tell them apart.
    148  */
    149 struct fd_type {
    150 	int	sectrac;	/* sectors per track */
    151 	int	heads;		/* number of heads */
    152 	int	seccyl;		/* sectors per cylinder */
    153 	int	secsize;	/* size code for sectors */
    154 	int	datalen;	/* data len when secsize = 0 */
    155 	int	steprate;	/* step rate and head unload time */
    156 	int	gap1;		/* gap len between sectors */
    157 	int	gap2;		/* formatting gap */
    158 	int	tracks;		/* total num of tracks */
    159 	int	size;		/* size of disk in sectors */
    160 	int	step;		/* steps per cylinder */
    161 	int	rate;		/* transfer speed code */
    162 	char	*name;
    163 };
    164 
    165 /* The order of entries in the following table is important -- BEWARE! */
    166 struct fd_type fd_types[] = {
    167 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB"    }, /* 1.44MB diskette */
    168 	{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,"1.2MB"    }, /* 1.2 MB AT-diskettes */
    169 	{  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,"360KB/AT" }, /* 360kB in 1.2MB drive */
    170 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,"360KB/PC" }, /* 360kB PC diskettes */
    171 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,"720KB"    }, /* 3.5" 720kB diskette */
    172 	{  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,"720KB/x"  }, /* 720kB in 1.2MB drive */
    173 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,"360KB/x"  }, /* 360kB in 720kB drive */
    174 };
    175 
    176 /* software state, per disk (with up to 4 disks per ctlr) */
    177 struct fd_softc {
    178 	struct device	sc_dv;		/* generic device info */
    179 	struct disk	sc_dk;		/* generic disk info */
    180 
    181 	struct fd_type *sc_deftype;	/* default type descriptor */
    182 	struct fd_type *sc_type;	/* current type descriptor */
    183 
    184 	daddr_t	sc_blkno;	/* starting block number */
    185 	int sc_bcount;		/* byte count left */
    186 	int sc_skip;		/* bytes already transferred */
    187 	int sc_nblks;		/* number of blocks currently tranferring */
    188 	int sc_nbytes;		/* number of bytes currently tranferring */
    189 
    190 	int sc_drive;		/* physical unit number */
    191 	int sc_flags;
    192 #define	FD_OPEN		0x01		/* it's open */
    193 #define	FD_MOTOR	0x02		/* motor should be on */
    194 #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
    195 	int sc_cylin;		/* where we think the head is */
    196 
    197 	void	*sc_sdhook;	/* shutdownhook cookie */
    198 
    199 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    200 	int sc_ops;		/* I/O ops since last switch */
    201 	struct buf sc_q;	/* head of buf chain */
    202 };
    203 
    204 /* floppy driver configuration */
    205 int	fdmatch __P((struct device *, void *, void *));
    206 void	fdattach __P((struct device *, struct device *, void *));
    207 
    208 struct cfattach fd_ca = {
    209 	sizeof(struct fd_softc), fdmatch, fdattach
    210 };
    211 
    212 struct cfdriver fd_cd = {
    213 	NULL, "fd", DV_DISK
    214 };
    215 
    216 void fdgetdisklabel __P((dev_t));
    217 int fd_get_parms __P((struct fd_softc *));
    218 void fdstrategy __P((struct buf *));
    219 void fdstart __P((struct fd_softc *));
    220 int fdprint __P((void *, const char *));
    221 
    222 struct dkdriver fddkdriver = { fdstrategy };
    223 
    224 struct	fd_type *fd_nvtotype __P((char *, int, int));
    225 void	fd_set_motor __P((struct fdc_softc *fdc));
    226 void	fd_motor_off __P((void *arg));
    227 void	fd_motor_on __P((void *arg));
    228 int	fdcresult __P((struct fdc_softc *fdc));
    229 int	out_fdc __P((struct fdc_softc *fdc, u_char x));
    230 void	fdcstart __P((struct fdc_softc *fdc));
    231 void	fdcstatus __P((struct device *dv, int n, char *s));
    232 void	fdc_reset __P((struct fdc_softc *fdc));
    233 void	fdctimeout __P((void *arg));
    234 void	fdcpseudointr __P((void *arg));
    235 #ifdef FDC_C_HANDLER
    236 int	fdchwintr __P((struct fdc_softc *));
    237 #else
    238 void	fdchwintr __P((void));
    239 #endif
    240 int	fdcswintr __P((struct fdc_softc *));
    241 void	fdcretry __P((struct fdc_softc *fdc));
    242 void	fdfinish __P((struct fd_softc *fd, struct buf *bp));
    243 void	fd_do_eject __P((void));
    244 void	fd_mountroot_hook __P((struct device *));
    245 static void fdconf __P((struct fdc_softc *));
    246 
    247 #if PIL_FDSOFT == 4
    248 #define IE_FDSOFT	IE_L4
    249 #else
    250 #error 4
    251 #endif
    252 
    253 #define OBP_FDNAME	(CPU_ISSUN4M ? "SUNW,fdtwo" : "fd")
    254 
    255 int
    256 fdcmatch(parent, match, aux)
    257 	struct device *parent;
    258 	void *match, *aux;
    259 {
    260 	register struct confargs *ca = aux;
    261 	register struct romaux *ra = &ca->ca_ra;
    262 
    263 	/*
    264 	 * Floppy doesn't exist on sun4.
    265 	 */
    266 	if (CPU_ISSUN4)
    267 		return (0);
    268 
    269 	/*
    270 	 * Floppy controller is on mainbus on sun4c.
    271 	 */
    272 	if ((CPU_ISSUN4C) && (ca->ca_bustype != BUS_MAIN))
    273 		return (0);
    274 
    275 	/*
    276 	 * Floppy controller is on obio on sun4m.
    277 	 */
    278 	if ((CPU_ISSUN4M) && (ca->ca_bustype != BUS_OBIO))
    279 		return (0);
    280 
    281 	/* Sun PROMs call the controller an "fd" or "SUNW,fdtwo" */
    282 	if (strcmp(OBP_FDNAME, ra->ra_name))
    283 		return (0);
    284 
    285 	if (ca->ca_ra.ra_vaddr &&
    286 	    probeget(ca->ca_ra.ra_vaddr, 1) == -1) {
    287 		return (0);
    288 	}
    289 
    290 	return (1);
    291 }
    292 
    293 /*
    294  * Arguments passed between fdcattach and fdprobe.
    295  */
    296 struct fdc_attach_args {
    297 	int fa_drive;
    298 	struct bootpath *fa_bootpath;
    299 	struct fd_type *fa_deftype;
    300 };
    301 
    302 /*
    303  * Print the location of a disk drive (called just before attaching the
    304  * the drive).  If `fdc' is not NULL, the drive was found but was not
    305  * in the system config file; print the drive name as well.
    306  * Return QUIET (config_find ignores this if the device was configured) to
    307  * avoid printing `fdN not configured' messages.
    308  */
    309 int
    310 fdprint(aux, fdc)
    311 	void *aux;
    312 	const char *fdc;
    313 {
    314 	register struct fdc_attach_args *fa = aux;
    315 
    316 	if (!fdc)
    317 		kprintf(" drive %d", fa->fa_drive);
    318 	return QUIET;
    319 }
    320 
    321 static void
    322 fdconf(fdc)
    323 	struct fdc_softc *fdc;
    324 {
    325 	int	vroom;
    326 
    327 	if (out_fdc(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
    328 		return;
    329 
    330 	/*
    331 	 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
    332 	 * the PROM thinks is appropriate.
    333 	 */
    334 	if ((vroom = fdc->sc_status[7]) == 0)
    335 		vroom = 0x64;
    336 
    337 	/* Configure controller to use FIFO and Implied Seek */
    338 	out_fdc(fdc, NE7CMD_CFG);
    339 	out_fdc(fdc, vroom);
    340 	out_fdc(fdc, fdc->sc_cfg);
    341 	out_fdc(fdc, 0); /* PRETRK */
    342 	/* No result phase */
    343 }
    344 
    345 void
    346 fdcattach(parent, self, aux)
    347 	struct device *parent, *self;
    348 	void *aux;
    349 {
    350 	register struct confargs *ca = aux;
    351 	struct fdc_softc *fdc = (void *)self;
    352 	struct fdc_attach_args fa;
    353 	struct bootpath *bp;
    354 	int pri;
    355 	char code;
    356 
    357 	if (ca->ca_ra.ra_vaddr)
    358 		fdc->sc_reg = (caddr_t)ca->ca_ra.ra_vaddr;
    359 	else
    360 		fdc->sc_reg = (caddr_t)mapiodev(ca->ca_ra.ra_reg, 0,
    361 						ca->ca_ra.ra_len,
    362 						ca->ca_bustype);
    363 
    364 	fdc->sc_state = DEVIDLE;
    365 	fdc->sc_istate = ISTATE_IDLE;
    366 	fdc->sc_flags |= FDC_EIS;
    367 	TAILQ_INIT(&fdc->sc_drives);
    368 
    369 	pri = ca->ca_ra.ra_intr[0].int_pri;
    370 #ifdef FDC_C_HANDLER
    371 	fdc->sc_hih.ih_fun = (void *)fdchwintr;
    372 	fdc->sc_hih.ih_arg = fdc;
    373 	intr_establish(pri, &fdc->sc_hih);
    374 #else
    375 	fdciop = &fdc->sc_io;
    376 	intr_fasttrap(pri, fdchwintr);
    377 #endif
    378 	fdc->sc_sih.ih_fun = (void *)fdcswintr;
    379 	fdc->sc_sih.ih_arg = fdc;
    380 	intr_establish(PIL_FDSOFT, &fdc->sc_sih);
    381 
    382 	/* Assume a 82077 */
    383 	fdc->sc_reg_msr = &((struct fdreg_77 *)fdc->sc_reg)->fd_msr;
    384 	fdc->sc_reg_fifo = &((struct fdreg_77 *)fdc->sc_reg)->fd_fifo;
    385 	fdc->sc_reg_dor = &((struct fdreg_77 *)fdc->sc_reg)->fd_dor;
    386 
    387 	code = '7';
    388 	if (*fdc->sc_reg_dor == NE7_RQM) {
    389 		/*
    390 		 * This hack from Chris Torek: apparently DOR really
    391 		 * addresses MSR/DRS on a 82072.
    392 		 * We used to rely on the VERSION command to tell the
    393 		 * difference (which did not work).
    394 		 */
    395 		*fdc->sc_reg_dor = FDC_250KBPS;
    396 		if (*fdc->sc_reg_dor == NE7_RQM)
    397 			code = '2';
    398 	}
    399 	if (code == '7') {
    400 		fdc->sc_flags |= FDC_82077;
    401 	} else {
    402 		fdc->sc_reg_msr = &((struct fdreg_72 *)fdc->sc_reg)->fd_msr;
    403 		fdc->sc_reg_fifo = &((struct fdreg_72 *)fdc->sc_reg)->fd_fifo;
    404 		fdc->sc_reg_dor = 0;
    405 	}
    406 
    407 #ifdef FD_DEBUG
    408 	if (out_fdc(fdc, NE7CMD_VERSION) == 0 &&
    409 	    fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
    410 		if (fdc_debug)
    411 			kprintf("[version cmd]");
    412 	}
    413 #endif
    414 
    415 	/*
    416 	 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
    417 	 * Note: CFG_EFIFO is active-low, initial threshold value: 8
    418 	 */
    419 	fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
    420 	fdconf(fdc);
    421 
    422 	if (fdc->sc_flags & FDC_82077) {
    423 		/* Lock configuration across soft resets. */
    424 		out_fdc(fdc, NE7CMD_LOCK | CFG_LOCK);
    425 		if (fdcresult(fdc) != 1)
    426 			kprintf(" CFGLOCK: unexpected response");
    427 	}
    428 
    429 	evcnt_attach(&fdc->sc_dev, "intr", &fdc->sc_intrcnt);
    430 
    431 	kprintf(" pri %d, softpri %d: chip 8207%c\n", pri, PIL_FDSOFT, code);
    432 
    433 	/*
    434 	 * Controller and drives are represented by one and the same
    435 	 * Openprom node, so we can as well check for the floppy boots here.
    436 	 */
    437 	fa.fa_bootpath = 0;
    438 	if ((bp = ca->ca_ra.ra_bp) && strcmp(bp->name, OBP_FDNAME) == 0) {
    439 
    440 		switch (ca->ca_bustype) {
    441 		case BUS_MAIN:
    442 			/*
    443 			 * We can get the bootpath in several different
    444 			 * formats! The faked v1 bootpath looks like /fd@0,0.
    445 			 * The v2 bootpath is either just /fd0, in which case
    446 			 * `bp->val[0]' will have been set to -1, or /fd@x,y
    447 			 * where <x,y> is the prom address specifier.
    448 			 */
    449 			if (((bp->val[0] == ca->ca_ra.ra_iospace) &&
    450 			     (bp->val[1] == (int)ca->ca_ra.ra_paddr)) ||
    451 
    452 			    ((bp->val[0] == -1) &&	/* v2: /fd0 */
    453 			     (bp->val[1] == 0)) ||
    454 
    455 			    ((bp->val[0] == 0) &&	/* v1: /fd@0,0 */
    456 			     (bp->val[1] == 0))
    457 			   )
    458 				fa.fa_bootpath = bp;
    459 			break;
    460 
    461 		case BUS_OBIO:
    462 			/*
    463 			 * floppy controller on obio (such as on the sun4m),
    464 			 * e.g.: `/obio0/SUNW,fdtwo@0,700000'.
    465 			 * We use "slot, offset" to determine if this is the
    466 			 * right one.
    467 			 */
    468 			if ((bp->val[0] == ca->ca_slot) &&
    469 			    (bp->val[1] == ca->ca_offset))
    470 				fa.fa_bootpath = bp;
    471 			break;
    472 		}
    473 
    474 	}
    475 
    476 	/* physical limit: four drives per controller. */
    477 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    478 		fa.fa_deftype = NULL;		/* unknown */
    479 	fa.fa_deftype = &fd_types[0];		/* XXX */
    480 		(void)config_found(self, (void *)&fa, fdprint);
    481 	}
    482 
    483 	bootpath_store(1, NULL);
    484 }
    485 
    486 int
    487 fdmatch(parent, match, aux)
    488 	struct device *parent;
    489 	void *match, *aux;
    490 {
    491 	struct fdc_softc *fdc = (void *)parent;
    492 	struct fdc_attach_args *fa = aux;
    493 	int drive = fa->fa_drive;
    494 	int n, ok;
    495 
    496 	if (drive > 0)
    497 		/* XXX - for now, punt > 1 drives */
    498 		return 0;
    499 
    500 	if (fdc->sc_flags & FDC_82077) {
    501 		/* select drive and turn on motor */
    502 		*fdc->sc_reg_dor = drive | FDO_FRST | FDO_MOEN(drive);
    503 		/* wait for motor to spin up */
    504 		delay(250000);
    505 	} else {
    506 		auxregbisc(AUXIO_FDS, 0);
    507 	}
    508 	fdc->sc_nstat = 0;
    509 	out_fdc(fdc, NE7CMD_RECAL);
    510 	out_fdc(fdc, drive);
    511 	/* wait for recalibrate */
    512 	for (n = 0; n < 10000; n++) {
    513 		delay(1000);
    514 		if ((*fdc->sc_reg_msr & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
    515 			/* wait a bit longer till device *really* is ready */
    516 			delay(100000);
    517 			if (out_fdc(fdc, NE7CMD_SENSEI))
    518 				break;
    519 			if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
    520 				/*
    521 				 * Got `invalid command'; we interpret it
    522 				 * to mean that the re-calibrate hasn't in
    523 				 * fact finished yet
    524 				 */
    525 				continue;
    526 			break;
    527 		}
    528 	}
    529 	n = fdc->sc_nstat;
    530 #ifdef FD_DEBUG
    531 	if (fdc_debug) {
    532 		int i;
    533 		kprintf("fdprobe: %d stati:", n);
    534 		for (i = 0; i < n; i++)
    535 			kprintf(" %x", fdc->sc_status[i]);
    536 		kprintf("\n");
    537 	}
    538 #endif
    539 	ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
    540 
    541 	/* turn off motor */
    542 	if (fdc->sc_flags & FDC_82077) {
    543 		/* select drive and turn on motor */
    544 		*fdc->sc_reg_dor = FDO_FRST;
    545 	} else {
    546 		auxregbisc(0, AUXIO_FDS);
    547 	}
    548 
    549 	return ok;
    550 }
    551 
    552 /*
    553  * Controller is working, and drive responded.  Attach it.
    554  */
    555 void
    556 fdattach(parent, self, aux)
    557 	struct device *parent, *self;
    558 	void *aux;
    559 {
    560 	struct fdc_softc *fdc = (void *)parent;
    561 	struct fd_softc *fd = (void *)self;
    562 	struct fdc_attach_args *fa = aux;
    563 	struct fd_type *type = fa->fa_deftype;
    564 	int drive = fa->fa_drive;
    565 
    566 	/* XXX Allow `flags' to override device type? */
    567 
    568 	if (type)
    569 		kprintf(": %s %d cyl, %d head, %d sec\n", type->name,
    570 		    type->tracks, type->heads, type->sectrac);
    571 	else
    572 		kprintf(": density unknown\n");
    573 
    574 	fd->sc_cylin = -1;
    575 	fd->sc_drive = drive;
    576 	fd->sc_deftype = type;
    577 	fdc->sc_fd[drive] = fd;
    578 
    579 	/*
    580 	 * Initialize and attach the disk structure.
    581 	 */
    582 	fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
    583 	fd->sc_dk.dk_driver = &fddkdriver;
    584 	disk_attach(&fd->sc_dk);
    585 
    586 	/*
    587 	 * We're told if we're the boot device in fdcattach().
    588 	 */
    589 	if (fa->fa_bootpath)
    590 		fa->fa_bootpath->dev = &fd->sc_dv;
    591 
    592 	/*
    593 	 * Establish a mountroot_hook anyway in case we booted
    594 	 * with RB_ASKNAME and get selected as the boot device.
    595 	 */
    596 	mountroot_hook_establish(fd_mountroot_hook, &fd->sc_dv);
    597 
    598 	/* Make sure the drive motor gets turned off at shutdown time. */
    599 	fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
    600 
    601 	/* XXX Need to do some more fiddling with sc_dk. */
    602 	dk_establish(&fd->sc_dk, &fd->sc_dv);
    603 }
    604 
    605 __inline struct fd_type *
    606 fd_dev_to_type(fd, dev)
    607 	struct fd_softc *fd;
    608 	dev_t dev;
    609 {
    610 	int type = FDTYPE(dev);
    611 
    612 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
    613 		return NULL;
    614 	return type ? &fd_types[type - 1] : fd->sc_deftype;
    615 }
    616 
    617 void
    618 fdstrategy(bp)
    619 	register struct buf *bp;	/* IO operation to perform */
    620 {
    621 	struct fd_softc *fd;
    622 	int unit = FDUNIT(bp->b_dev);
    623 	int sz;
    624  	int s;
    625 
    626 	/* Valid unit, controller, and request? */
    627 	if (unit >= fd_cd.cd_ndevs ||
    628 	    (fd = fd_cd.cd_devs[unit]) == 0 ||
    629 	    bp->b_blkno < 0 ||
    630 	    (bp->b_bcount % FDC_BSIZE) != 0) {
    631 		bp->b_error = EINVAL;
    632 		goto bad;
    633 	}
    634 
    635 	/* If it's a null transfer, return immediately. */
    636 	if (bp->b_bcount == 0)
    637 		goto done;
    638 
    639 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    640 
    641 	if (bp->b_blkno + sz > fd->sc_type->size) {
    642 		sz = fd->sc_type->size - bp->b_blkno;
    643 		if (sz == 0) {
    644 			/* If exactly at end of disk, return EOF. */
    645 			bp->b_resid = bp->b_bcount;
    646 			goto done;
    647 		}
    648 		if (sz < 0) {
    649 			/* If past end of disk, return EINVAL. */
    650 			bp->b_error = EINVAL;
    651 			goto bad;
    652 		}
    653 		/* Otherwise, truncate request. */
    654 		bp->b_bcount = sz << DEV_BSHIFT;
    655 	}
    656 
    657  	bp->b_cylin = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
    658 
    659 #ifdef FD_DEBUG
    660 	if (fdc_debug > 1)
    661 	    kprintf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld\n",
    662 		    bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylin);
    663 #endif
    664 
    665 	/* Queue transfer on drive, activate drive and controller if idle. */
    666 	s = splbio();
    667 	disksort(&fd->sc_q, bp);
    668 	untimeout(fd_motor_off, fd); /* a good idea */
    669 	if (!fd->sc_q.b_active)
    670 		fdstart(fd);
    671 #ifdef DIAGNOSTIC
    672 	else {
    673 		struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
    674 		if (fdc->sc_state == DEVIDLE) {
    675 			kprintf("fdstrategy: controller inactive\n");
    676 			fdcstart(fdc);
    677 		}
    678 	}
    679 #endif
    680 	splx(s);
    681 	return;
    682 
    683 bad:
    684 	bp->b_flags |= B_ERROR;
    685 done:
    686 	/* Toss transfer; we're done early. */
    687 	biodone(bp);
    688 }
    689 
    690 void
    691 fdstart(fd)
    692 	struct fd_softc *fd;
    693 {
    694 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
    695 	int active = fdc->sc_drives.tqh_first != 0;
    696 
    697 	/* Link into controller queue. */
    698 	fd->sc_q.b_active = 1;
    699 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    700 
    701 	/* If controller not already active, start it. */
    702 	if (!active)
    703 		fdcstart(fdc);
    704 }
    705 
    706 void
    707 fdfinish(fd, bp)
    708 	struct fd_softc *fd;
    709 	struct buf *bp;
    710 {
    711 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
    712 
    713 	/*
    714 	 * Move this drive to the end of the queue to give others a `fair'
    715 	 * chance.  We only force a switch if N operations are completed while
    716 	 * another drive is waiting to be serviced, since there is a long motor
    717 	 * startup delay whenever we switch.
    718 	 */
    719 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
    720 		fd->sc_ops = 0;
    721 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    722 		if (bp->b_actf) {
    723 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    724 		} else
    725 			fd->sc_q.b_active = 0;
    726 	}
    727 	bp->b_resid = fd->sc_bcount;
    728 	fd->sc_skip = 0;
    729 	fd->sc_q.b_actf = bp->b_actf;
    730 
    731 	biodone(bp);
    732 	/* turn off motor 5s from now */
    733 	timeout(fd_motor_off, fd, 5 * hz);
    734 	fdc->sc_state = DEVIDLE;
    735 }
    736 
    737 void
    738 fdc_reset(fdc)
    739 	struct fdc_softc *fdc;
    740 {
    741 	if (fdc->sc_flags & FDC_82077) {
    742 		*fdc->sc_reg_dor = FDO_MOEN(0);
    743 	}
    744 
    745 	*fdc->sc_reg_drs = DRS_RESET;
    746 	delay(10);
    747 	*fdc->sc_reg_drs = 0;
    748 #ifdef FD_DEBUG
    749 	if (fdc_debug)
    750 		kprintf("fdc reset\n");
    751 #endif
    752 }
    753 
    754 void
    755 fd_set_motor(fdc)
    756 	struct fdc_softc *fdc;
    757 {
    758 	struct fd_softc *fd;
    759 	u_char status;
    760 	int n;
    761 
    762 	if (fdc->sc_flags & FDC_82077) {
    763 		status = FDO_FRST | FDO_FDMAEN;
    764 		if ((fd = fdc->sc_drives.tqh_first) != NULL)
    765 			status |= fd->sc_drive;
    766 
    767 		for (n = 0; n < 4; n++)
    768 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    769 				status |= FDO_MOEN(n);
    770 		*fdc->sc_reg_dor = status;
    771 	} else {
    772 		int on = 0;
    773 
    774 		for (n = 0; n < 4; n++)
    775 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    776 				on = 1;
    777 		if (on) {
    778 			auxregbisc(AUXIO_FDS, 0);
    779 		} else {
    780 			auxregbisc(0, AUXIO_FDS);
    781 		}
    782 	}
    783 }
    784 
    785 void
    786 fd_motor_off(arg)
    787 	void *arg;
    788 {
    789 	struct fd_softc *fd = arg;
    790 	int s;
    791 
    792 	s = splbio();
    793 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    794 	fd_set_motor((struct fdc_softc *)fd->sc_dv.dv_parent);
    795 	splx(s);
    796 }
    797 
    798 void
    799 fd_motor_on(arg)
    800 	void *arg;
    801 {
    802 	struct fd_softc *fd = arg;
    803 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
    804 	int s;
    805 
    806 	s = splbio();
    807 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    808 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
    809 		(void) fdcswintr(fdc);
    810 	splx(s);
    811 }
    812 
    813 int
    814 fdcresult(fdc)
    815 	struct fdc_softc *fdc;
    816 {
    817 	u_char i;
    818 	int j = 100000,
    819 	    n = 0;
    820 
    821 	for (; j; j--) {
    822 		i = *fdc->sc_reg_msr & (NE7_DIO | NE7_RQM | NE7_CB);
    823 		if (i == NE7_RQM)
    824 			return (fdc->sc_nstat = n);
    825 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    826 			if (n >= sizeof(fdc->sc_status)) {
    827 				log(LOG_ERR, "fdcresult: overrun\n");
    828 				return -1;
    829 			}
    830 			fdc->sc_status[n++] = *fdc->sc_reg_fifo;
    831 		}
    832 	}
    833 	log(LOG_ERR, "fdcresult: timeout\n");
    834 	return (fdc->sc_nstat = -1);
    835 }
    836 
    837 int
    838 out_fdc(fdc, x)
    839 	struct fdc_softc *fdc;
    840 	u_char x;
    841 {
    842 	int i = 100000;
    843 
    844 	while (((*fdc->sc_reg_msr & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0);
    845 	if (i <= 0)
    846 		return -1;
    847 
    848 	*fdc->sc_reg_fifo = x;
    849 	return 0;
    850 }
    851 
    852 int
    853 fdopen(dev, flags, fmt, p)
    854 	dev_t dev;
    855 	int flags, fmt;
    856 	struct proc *p;
    857 {
    858  	int unit, pmask;
    859 	struct fd_softc *fd;
    860 	struct fd_type *type;
    861 
    862 	unit = FDUNIT(dev);
    863 	if (unit >= fd_cd.cd_ndevs)
    864 		return ENXIO;
    865 	fd = fd_cd.cd_devs[unit];
    866 	if (fd == 0)
    867 		return ENXIO;
    868 	type = fd_dev_to_type(fd, dev);
    869 	if (type == NULL)
    870 		return ENXIO;
    871 
    872 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    873 	    fd->sc_type != type)
    874 		return EBUSY;
    875 
    876 	fd->sc_type = type;
    877 	fd->sc_cylin = -1;
    878 	fd->sc_flags |= FD_OPEN;
    879 
    880 	/*
    881 	 * Only update the disklabel if we're not open anywhere else.
    882 	 */
    883 	if (fd->sc_dk.dk_openmask == 0)
    884 		fdgetdisklabel(dev);
    885 
    886 	pmask = (1 << DISKPART(dev));
    887 
    888 	switch (fmt) {
    889 	case S_IFCHR:
    890 		fd->sc_dk.dk_copenmask |= pmask;
    891 		break;
    892 
    893 	case S_IFBLK:
    894 		fd->sc_dk.dk_bopenmask |= pmask;
    895 		break;
    896 	}
    897 	fd->sc_dk.dk_openmask =
    898 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
    899 
    900 	return 0;
    901 }
    902 
    903 int
    904 fdclose(dev, flags, fmt, p)
    905 	dev_t dev;
    906 	int flags, fmt;
    907 	struct proc *p;
    908 {
    909 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
    910 	int pmask = (1 << DISKPART(dev));
    911 
    912 	fd->sc_flags &= ~FD_OPEN;
    913 
    914 	switch (fmt) {
    915 	case S_IFCHR:
    916 		fd->sc_dk.dk_copenmask &= ~pmask;
    917 		break;
    918 
    919 	case S_IFBLK:
    920 		fd->sc_dk.dk_bopenmask &= ~pmask;
    921 		break;
    922 	}
    923 	fd->sc_dk.dk_openmask =
    924 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
    925 
    926 	return 0;
    927 }
    928 
    929 int
    930 fdread(dev, uio, flag)
    931         dev_t dev;
    932         struct uio *uio;
    933 	int flag;
    934 {
    935 
    936         return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
    937 }
    938 
    939 int
    940 fdwrite(dev, uio, flag)
    941         dev_t dev;
    942         struct uio *uio;
    943 	int flag;
    944 {
    945 
    946         return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
    947 }
    948 
    949 void
    950 fdcstart(fdc)
    951 	struct fdc_softc *fdc;
    952 {
    953 
    954 #ifdef DIAGNOSTIC
    955 	/* only got here if controller's drive queue was inactive; should
    956 	   be in idle state */
    957 	if (fdc->sc_state != DEVIDLE) {
    958 		kprintf("fdcstart: not idle\n");
    959 		return;
    960 	}
    961 #endif
    962 	(void) fdcswintr(fdc);
    963 }
    964 
    965 void
    966 fdcstatus(dv, n, s)
    967 	struct device *dv;
    968 	int n;
    969 	char *s;
    970 {
    971 	struct fdc_softc *fdc = (void *)dv->dv_parent;
    972 #if 0
    973 	/*
    974 	 * A 82072 seems to return <invalid command> on
    975 	 * gratuitous Sense Interrupt commands.
    976 	 */
    977 	if (n == 0 && (fdc->sc_flags & FDC_82077)) {
    978 		out_fdc(fdc, NE7CMD_SENSEI);
    979 		(void) fdcresult(fdc);
    980 		n = 2;
    981 	}
    982 #endif
    983 
    984 	/* Just print last status */
    985 	n = fdc->sc_nstat;
    986 
    987 	kprintf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
    988 
    989 	switch (n) {
    990 	case 0:
    991 		kprintf("\n");
    992 		break;
    993 	case 2:
    994 		kprintf(" (st0 %b cyl %d)\n",
    995 		    fdc->sc_status[0], NE7_ST0BITS,
    996 		    fdc->sc_status[1]);
    997 		break;
    998 	case 7:
    999 		kprintf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
   1000 		    fdc->sc_status[0], NE7_ST0BITS,
   1001 		    fdc->sc_status[1], NE7_ST1BITS,
   1002 		    fdc->sc_status[2], NE7_ST2BITS,
   1003 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
   1004 		break;
   1005 #ifdef DIAGNOSTIC
   1006 	default:
   1007 		kprintf(" fdcstatus: weird size: %d\n", n);
   1008 		break;
   1009 #endif
   1010 	}
   1011 }
   1012 
   1013 void
   1014 fdctimeout(arg)
   1015 	void *arg;
   1016 {
   1017 	struct fdc_softc *fdc = arg;
   1018 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
   1019 	int s;
   1020 
   1021 	s = splbio();
   1022 	fdcstatus(&fd->sc_dv, 0, "timeout");
   1023 
   1024 	if (fd->sc_q.b_actf)
   1025 		fdc->sc_state++;
   1026 	else
   1027 		fdc->sc_state = DEVIDLE;
   1028 
   1029 	(void) fdcswintr(fdc);
   1030 	splx(s);
   1031 }
   1032 
   1033 void
   1034 fdcpseudointr(arg)
   1035 	void *arg;
   1036 {
   1037 	struct fdc_softc *fdc = arg;
   1038 	int s;
   1039 
   1040 	/* Just ensure it has the right spl. */
   1041 	s = splbio();
   1042 	(void) fdcswintr(fdc);
   1043 	splx(s);
   1044 }
   1045 
   1046 
   1047 #ifdef FDC_C_HANDLER
   1048 /*
   1049  * hardware interrupt entry point: must be converted to `fast'
   1050  * (in-window) handler.
   1051  */
   1052 int
   1053 fdchwintr(fdc)
   1054 	struct fdc_softc *fdc;
   1055 {
   1056 	struct buf *bp;
   1057 	int read;
   1058 
   1059 	switch (fdc->sc_istate) {
   1060 	case ISTATE_SENSEI:
   1061 		out_fdc(fdc, NE7CMD_SENSEI);
   1062 		fdcresult(fdc);
   1063 		fdc->sc_istate = ISTATE_IDLE;
   1064 		ienab_bis(IE_FDSOFT);
   1065 		return 1;
   1066 	case ISTATE_IDLE:
   1067 	case ISTATE_SPURIOUS:
   1068 		auxregbisc(0, AUXIO_FDS);	/* Does this help? */
   1069 		fdcresult(fdc);
   1070 		fdc->sc_istate = ISTATE_SPURIOUS;
   1071 		kprintf("fdc: stray hard interrupt... ");
   1072 		ienab_bis(IE_FDSOFT);
   1073 		return 1;
   1074 	case ISTATE_DMA:
   1075 		break;
   1076 	default:
   1077 		kprintf("fdc: goofed ...\n");
   1078 		return 1;
   1079 	}
   1080 
   1081 	read = bp->b_flags & B_READ;
   1082 	for (;;) {
   1083 		register int msr;
   1084 
   1085 		msr = *fdc->sc_reg_msr;
   1086 
   1087 		if ((msr & NE7_RQM) == 0)
   1088 			break;
   1089 
   1090 		if ((msr & NE7_NDM) == 0) {
   1091 			fdcresult(fdc);
   1092 			fdc->sc_istate = ISTATE_IDLE;
   1093 			ienab_bis(IE_FDSOFT);
   1094 			kprintf("fdc: overrun: tc = %d\n", fdc->sc_tc);
   1095 			break;
   1096 		}
   1097 
   1098 		if (msr & NE7_DIO) {
   1099 #ifdef DIAGNOSTIC
   1100 			if (!read)
   1101 				kprintf("fdxfer: false read\n");
   1102 #endif
   1103 			*fdc->sc_data++ = *fdc->sc_reg_fifo;
   1104 		} else {
   1105 #ifdef DIAGNOSTIC
   1106 			if (read)
   1107 				kprintf("fdxfer: false write\n");
   1108 #endif
   1109 			*fdc->sc_reg_fifo = *fdc->sc_data++;
   1110 		}
   1111 		if (--fdc->sc_tc == 0) {
   1112 			auxregbisc(AUXIO_FTC, 0);
   1113 			fdc->sc_istate = ISTATE_IDLE;
   1114 			delay(10);
   1115 			auxregbisc(0, AUXIO_FTC);
   1116 			fdcresult(fdc);
   1117 			ienab_bis(IE_FDSOFT);
   1118 			break;
   1119 		}
   1120 	}
   1121 	return 1;
   1122 }
   1123 #endif
   1124 
   1125 int
   1126 fdcswintr(fdc)
   1127 	struct fdc_softc *fdc;
   1128 {
   1129 #define	st0	fdc->sc_status[0]
   1130 #define	st1	fdc->sc_status[1]
   1131 #define	cyl	fdc->sc_status[1]
   1132 #define OUT_FDC(fdc, c, s) \
   1133     do { if (out_fdc(fdc, (c))) { (fdc)->sc_state = (s); goto loop; } } while(0)
   1134 
   1135 	struct fd_softc *fd;
   1136 	struct buf *bp;
   1137 	int read, head, sec, nblks;
   1138 	struct fd_type *type;
   1139 
   1140 
   1141 	if (fdc->sc_istate != ISTATE_IDLE) {
   1142 		/* Trouble... */
   1143 		kprintf("fdc: spurious interrupt: state %d, istate=%d\n",
   1144 			fdc->sc_state, fdc->sc_istate);
   1145 		fdc->sc_istate = ISTATE_IDLE;
   1146 		if (fdc->sc_state == RESETCOMPLETE ||
   1147 		    fdc->sc_state == RESETTIMEDOUT) {
   1148 			panic("fdcintr: spurious interrupt can't be cleared");
   1149 		}
   1150 		goto doreset;
   1151 	}
   1152 
   1153 loop:
   1154 	/* Is there a drive for the controller to do a transfer with? */
   1155 	fd = fdc->sc_drives.tqh_first;
   1156 	if (fd == NULL) {
   1157 		fdc->sc_state = DEVIDLE;
   1158  		return 0;
   1159 	}
   1160 
   1161 	/* Is there a transfer to this drive?  If not, deactivate drive. */
   1162 	bp = fd->sc_q.b_actf;
   1163 	if (bp == NULL) {
   1164 		fd->sc_ops = 0;
   1165 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
   1166 		fd->sc_q.b_active = 0;
   1167 		goto loop;
   1168 	}
   1169 
   1170 	switch (fdc->sc_state) {
   1171 	case DEVIDLE:
   1172 		fdc->sc_errors = 0;
   1173 		fd->sc_skip = 0;
   1174 		fd->sc_bcount = bp->b_bcount;
   1175 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
   1176 		untimeout(fd_motor_off, fd);
   1177 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
   1178 			fdc->sc_state = MOTORWAIT;
   1179 			return 1;
   1180 		}
   1181 		if ((fd->sc_flags & FD_MOTOR) == 0) {
   1182 			/* Turn on the motor, being careful about pairing. */
   1183 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
   1184 			if (ofd && ofd->sc_flags & FD_MOTOR) {
   1185 				untimeout(fd_motor_off, ofd);
   1186 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1187 			}
   1188 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1189 			fd_set_motor(fdc);
   1190 			fdc->sc_state = MOTORWAIT;
   1191 			if (fdc->sc_flags & FDC_82077) { /* XXX */
   1192 				/* Allow .25s for motor to stabilize. */
   1193 				timeout(fd_motor_on, fd, hz / 4);
   1194 			} else {
   1195 				fd->sc_flags &= ~FD_MOTOR_WAIT;
   1196 				goto loop;
   1197 			}
   1198 			return 1;
   1199 		}
   1200 		/* Make sure the right drive is selected. */
   1201 		fd_set_motor(fdc);
   1202 
   1203 		/* fall through */
   1204 	case DOSEEK:
   1205 	doseek:
   1206 		if (fdc->sc_flags & FDC_EIS) {
   1207 			fd->sc_cylin = bp->b_cylin;
   1208 			/* We use implied seek */
   1209 			goto doio;
   1210 		}
   1211 
   1212 		if (fd->sc_cylin == bp->b_cylin)
   1213 			goto doio;
   1214 
   1215 		/* specify command */
   1216 		OUT_FDC(fdc, NE7CMD_SPECIFY, SEEKTIMEDOUT);
   1217 		OUT_FDC(fdc, fd->sc_type->steprate, SEEKTIMEDOUT);
   1218 		OUT_FDC(fdc, 6, SEEKTIMEDOUT);	/* XXX head load time == 6ms */
   1219 
   1220 		fdc->sc_istate = ISTATE_SENSEI;
   1221 		/* seek function */
   1222 		OUT_FDC(fdc, NE7CMD_SEEK, SEEKTIMEDOUT);
   1223 		OUT_FDC(fdc, fd->sc_drive, SEEKTIMEDOUT); /* drive number */
   1224 		OUT_FDC(fdc, bp->b_cylin * fd->sc_type->step, SEEKTIMEDOUT);
   1225 
   1226 		fd->sc_cylin = -1;
   1227 		fdc->sc_state = SEEKWAIT;
   1228 		fdc->sc_nstat = 0;
   1229 
   1230 		fd->sc_dk.dk_seek++;
   1231 		disk_busy(&fd->sc_dk);
   1232 
   1233 		timeout(fdctimeout, fdc, 4 * hz);
   1234 		return 1;
   1235 
   1236 	case DOIO:
   1237 	doio:
   1238 		type = fd->sc_type;
   1239 		sec = fd->sc_blkno % type->seccyl;
   1240 		nblks = type->seccyl - sec;
   1241 		nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1242 		nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
   1243 		fd->sc_nblks = nblks;
   1244 		fd->sc_nbytes = nblks * FDC_BSIZE;
   1245 		head = sec / type->sectrac;
   1246 		sec -= head * type->sectrac;
   1247 #ifdef DIAGNOSTIC
   1248 		{int block;
   1249 		 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
   1250 		 if (block != fd->sc_blkno) {
   1251 			 kprintf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
   1252 #ifdef DDB
   1253 			 Debugger();
   1254 #endif
   1255 		 }}
   1256 #endif
   1257 		read = bp->b_flags & B_READ;
   1258 
   1259 		/* Setup for pseudo DMA */
   1260 		fdc->sc_data = bp->b_data + fd->sc_skip;
   1261 		fdc->sc_tc = fd->sc_nbytes;
   1262 
   1263 		*fdc->sc_reg_drs = type->rate;
   1264 #ifdef FD_DEBUG
   1265 		if (fdc_debug > 1)
   1266 			kprintf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
   1267 				read ? "read" : "write", fd->sc_drive,
   1268 				fd->sc_cylin, head, sec, nblks);
   1269 #endif
   1270 		fdc->sc_state = IOCOMPLETE;
   1271 		fdc->sc_istate = ISTATE_DMA;
   1272 		fdc->sc_nstat = 0;
   1273 		if (read)
   1274 			OUT_FDC(fdc, NE7CMD_READ, IOTIMEDOUT);	/* READ */
   1275 		else
   1276 			OUT_FDC(fdc, NE7CMD_WRITE, IOTIMEDOUT);	/* WRITE */
   1277 		OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
   1278 		OUT_FDC(fdc, fd->sc_cylin, IOTIMEDOUT);	/* track */
   1279 		OUT_FDC(fdc, head, IOTIMEDOUT);
   1280 		OUT_FDC(fdc, sec + 1, IOTIMEDOUT);	/* sector +1 */
   1281 		OUT_FDC(fdc, type->secsize, IOTIMEDOUT);/* sector size */
   1282 		OUT_FDC(fdc, type->sectrac, IOTIMEDOUT);/* sectors/track */
   1283 		OUT_FDC(fdc, type->gap1, IOTIMEDOUT);	/* gap1 size */
   1284 		OUT_FDC(fdc, type->datalen, IOTIMEDOUT);/* data length */
   1285 
   1286 		disk_busy(&fd->sc_dk);
   1287 
   1288 		/* allow 2 seconds for operation */
   1289 		timeout(fdctimeout, fdc, 2 * hz);
   1290 		return 1;				/* will return later */
   1291 
   1292 	case SEEKWAIT:
   1293 		untimeout(fdctimeout, fdc);
   1294 		fdc->sc_state = SEEKCOMPLETE;
   1295 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1296 			/* allow 1/50 second for heads to settle */
   1297 			timeout(fdcpseudointr, fdc, hz / 50);
   1298 			return 1;		/* will return later */
   1299 		}
   1300 
   1301 	case SEEKCOMPLETE:
   1302 		disk_unbusy(&fd->sc_dk, 0);	/* no data on seek */
   1303 
   1304 		/* Make sure seek really happened. */
   1305 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
   1306 		    cyl != bp->b_cylin * fd->sc_type->step) {
   1307 #ifdef FD_DEBUG
   1308 			if (fdc_debug)
   1309 				fdcstatus(&fd->sc_dv, 2, "seek failed");
   1310 #endif
   1311 			fdcretry(fdc);
   1312 			goto loop;
   1313 		}
   1314 		fd->sc_cylin = bp->b_cylin;
   1315 		goto doio;
   1316 
   1317 	case IOTIMEDOUT:
   1318 		auxregbisc(AUXIO_FTC, 0);
   1319 		delay(10);
   1320 		auxregbisc(0, AUXIO_FTC);
   1321 		(void)fdcresult(fdc);
   1322 	case SEEKTIMEDOUT:
   1323 	case RECALTIMEDOUT:
   1324 	case RESETTIMEDOUT:
   1325 		fdcretry(fdc);
   1326 		goto loop;
   1327 
   1328 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1329 		untimeout(fdctimeout, fdc);
   1330 
   1331 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
   1332 
   1333 		if (fdc->sc_nstat != 7 || (st0 & 0xf8) != 0 || st1 != 0) {
   1334 #ifdef FD_DEBUG
   1335 			if (fdc_debug) {
   1336 				fdcstatus(&fd->sc_dv, 7,
   1337 					bp->b_flags & B_READ
   1338 					? "read failed" : "write failed");
   1339 				kprintf("blkno %d nblks %d tc %d\n",
   1340 				       fd->sc_blkno, fd->sc_nblks, fdc->sc_tc);
   1341 			}
   1342 #endif
   1343 			if (fdc->sc_nstat == 7 &&
   1344 			    (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
   1345 
   1346 				/*
   1347 				 * Silently retry overruns if no other
   1348 				 * error bit is set. Adjust threshold.
   1349 				 */
   1350 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1351 				if (thr < 15) {
   1352 					thr++;
   1353 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1354 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1355 #ifdef FD_DEBUG
   1356 					if (fdc_debug)
   1357 						kprintf("fdc: %d -> threshold\n", thr);
   1358 #endif
   1359 					fdconf(fdc);
   1360 					fdc->sc_overruns = 0;
   1361 				}
   1362 				if (++fdc->sc_overruns < 3) {
   1363 					fdc->sc_state = DOIO;
   1364 					goto loop;
   1365 				}
   1366 			}
   1367 			fdcretry(fdc);
   1368 			goto loop;
   1369 		}
   1370 		if (fdc->sc_errors) {
   1371 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
   1372 			    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1373 			kprintf("\n");
   1374 			fdc->sc_errors = 0;
   1375 		} else {
   1376 			if (--fdc->sc_overruns < -20) {
   1377 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1378 				if (thr > 0) {
   1379 					thr--;
   1380 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1381 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1382 #ifdef FD_DEBUG
   1383 					if (fdc_debug)
   1384 						kprintf("fdc: %d -> threshold\n", thr);
   1385 #endif
   1386 					fdconf(fdc);
   1387 				}
   1388 				fdc->sc_overruns = 0;
   1389 			}
   1390 		}
   1391 		fd->sc_blkno += fd->sc_nblks;
   1392 		fd->sc_skip += fd->sc_nbytes;
   1393 		fd->sc_bcount -= fd->sc_nbytes;
   1394 		if (fd->sc_bcount > 0) {
   1395 			bp->b_cylin = fd->sc_blkno / fd->sc_type->seccyl;
   1396 			goto doseek;
   1397 		}
   1398 		fdfinish(fd, bp);
   1399 		goto loop;
   1400 
   1401 	case DORESET:
   1402 	doreset:
   1403 		/* try a reset, keep motor on */
   1404 		fd_set_motor(fdc);
   1405 		delay(100);
   1406 		fdc_reset(fdc);
   1407 		fdc->sc_nstat = 0;
   1408 		fdc->sc_istate = ISTATE_SENSEI;
   1409 		fdc->sc_state = RESETCOMPLETE;
   1410 		timeout(fdctimeout, fdc, hz / 2);
   1411 		return 1;			/* will return later */
   1412 
   1413 	case RESETCOMPLETE:
   1414 		untimeout(fdctimeout, fdc);
   1415 		fdconf(fdc);
   1416 
   1417 		/* fall through */
   1418 	case DORECAL:
   1419 		fdc->sc_state = RECALWAIT;
   1420 		fdc->sc_istate = ISTATE_SENSEI;
   1421 		fdc->sc_nstat = 0;
   1422 		/* recalibrate function */
   1423 		OUT_FDC(fdc, NE7CMD_RECAL, RECALTIMEDOUT);
   1424 		OUT_FDC(fdc, fd->sc_drive, RECALTIMEDOUT);
   1425 		timeout(fdctimeout, fdc, 5 * hz);
   1426 		return 1;			/* will return later */
   1427 
   1428 	case RECALWAIT:
   1429 		untimeout(fdctimeout, fdc);
   1430 		fdc->sc_state = RECALCOMPLETE;
   1431 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1432 			/* allow 1/30 second for heads to settle */
   1433 			timeout(fdcpseudointr, fdc, hz / 30);
   1434 			return 1;		/* will return later */
   1435 		}
   1436 
   1437 	case RECALCOMPLETE:
   1438 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1439 #ifdef FD_DEBUG
   1440 			if (fdc_debug)
   1441 				fdcstatus(&fd->sc_dv, 2, "recalibrate failed");
   1442 #endif
   1443 			fdcretry(fdc);
   1444 			goto loop;
   1445 		}
   1446 		fd->sc_cylin = 0;
   1447 		goto doseek;
   1448 
   1449 	case MOTORWAIT:
   1450 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1451 			return 1;		/* time's not up yet */
   1452 		goto doseek;
   1453 
   1454 	default:
   1455 		fdcstatus(&fd->sc_dv, 0, "stray interrupt");
   1456 		return 1;
   1457 	}
   1458 #ifdef DIAGNOSTIC
   1459 	panic("fdcintr: impossible");
   1460 #endif
   1461 #undef	st0
   1462 #undef	st1
   1463 #undef	cyl
   1464 }
   1465 
   1466 void
   1467 fdcretry(fdc)
   1468 	struct fdc_softc *fdc;
   1469 {
   1470 	struct fd_softc *fd;
   1471 	struct buf *bp;
   1472 
   1473 	fd = fdc->sc_drives.tqh_first;
   1474 	bp = fd->sc_q.b_actf;
   1475 
   1476 	fdc->sc_overruns = 0;
   1477 
   1478 	switch (fdc->sc_errors) {
   1479 	case 0:
   1480 		/* try again */
   1481 		fdc->sc_state =
   1482 			(fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
   1483 		break;
   1484 
   1485 	case 1: case 2: case 3:
   1486 		/* didn't work; try recalibrating */
   1487 		fdc->sc_state = DORECAL;
   1488 		break;
   1489 
   1490 	case 4:
   1491 		/* still no go; reset the bastard */
   1492 		fdc->sc_state = DORESET;
   1493 		break;
   1494 
   1495 	default:
   1496 		diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1497 		    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1498 
   1499 		kprintf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
   1500 		    fdc->sc_status[0], NE7_ST0BITS,
   1501 		    fdc->sc_status[1], NE7_ST1BITS,
   1502 		    fdc->sc_status[2], NE7_ST2BITS,
   1503 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
   1504 
   1505 		bp->b_flags |= B_ERROR;
   1506 		bp->b_error = EIO;
   1507 		fdfinish(fd, bp);
   1508 	}
   1509 	fdc->sc_errors++;
   1510 }
   1511 
   1512 int
   1513 fdsize(dev)
   1514 	dev_t dev;
   1515 {
   1516 
   1517 	/* Swapping to floppies would not make sense. */
   1518 	return -1;
   1519 }
   1520 
   1521 int
   1522 fddump(dev, blkno, va, size)
   1523 	dev_t dev;
   1524 	daddr_t blkno;
   1525 	caddr_t va;
   1526 	size_t size;
   1527 {
   1528 
   1529 	/* Not implemented. */
   1530 	return EINVAL;
   1531 }
   1532 
   1533 int
   1534 fdioctl(dev, cmd, addr, flag, p)
   1535 	dev_t dev;
   1536 	u_long cmd;
   1537 	caddr_t addr;
   1538 	int flag;
   1539 	struct proc *p;
   1540 {
   1541 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
   1542 	int error;
   1543 
   1544 	switch (cmd) {
   1545 	case DIOCGDINFO:
   1546 		*(struct disklabel *)addr = *(fd->sc_dk.dk_label);
   1547 		return 0;
   1548 
   1549 	case DIOCWLABEL:
   1550 		if ((flag & FWRITE) == 0)
   1551 			return EBADF;
   1552 		/* XXX do something */
   1553 		return 0;
   1554 
   1555 	case DIOCWDINFO:
   1556 		if ((flag & FWRITE) == 0)
   1557 			return EBADF;
   1558 
   1559 		error = setdisklabel(fd->sc_dk.dk_label,
   1560 				    (struct disklabel *)addr, 0,
   1561 				    fd->sc_dk.dk_cpulabel);
   1562 		if (error)
   1563 			return error;
   1564 
   1565 		error = writedisklabel(dev, fdstrategy,
   1566 				       fd->sc_dk.dk_label,
   1567 				       fd->sc_dk.dk_cpulabel);
   1568 		return error;
   1569 
   1570 	case DIOCLOCK:
   1571 		/*
   1572 		 * Nothing to do here, really.
   1573 		 */
   1574 		return 0;
   1575 
   1576 	case DIOCEJECT:
   1577 		fd_do_eject();
   1578 		return 0;
   1579 
   1580 #ifdef DEBUG
   1581 	case _IO('f', 100):
   1582 		{
   1583 		int i;
   1584 		struct fdc_softc *fdc = (struct fdc_softc *)
   1585 					fd->sc_dv.dv_parent;
   1586 
   1587 		out_fdc(fdc, NE7CMD_DUMPREG);
   1588 		fdcresult(fdc);
   1589 		kprintf("dumpreg(%d regs): <", fdc->sc_nstat);
   1590 		for (i = 0; i < fdc->sc_nstat; i++)
   1591 			kprintf(" %x", fdc->sc_status[i]);
   1592 		kprintf(">\n");
   1593 		}
   1594 
   1595 		return 0;
   1596 	case _IOW('f', 101, int):
   1597 		((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg &=
   1598 			~CFG_THRHLD_MASK;
   1599 		((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg |=
   1600 			(*(int *)addr & CFG_THRHLD_MASK);
   1601 		fdconf((struct fdc_softc *) fd->sc_dv.dv_parent);
   1602 		return 0;
   1603 	case _IO('f', 102):
   1604 		{
   1605 		int i;
   1606 		struct fdc_softc *fdc = (struct fdc_softc *)
   1607 					fd->sc_dv.dv_parent;
   1608 		out_fdc(fdc, NE7CMD_SENSEI);
   1609 		fdcresult(fdc);
   1610 		kprintf("sensei(%d regs): <", fdc->sc_nstat);
   1611 		for (i=0; i< fdc->sc_nstat; i++)
   1612 			kprintf(" 0x%x", fdc->sc_status[i]);
   1613 		}
   1614 		kprintf(">\n");
   1615 		return 0;
   1616 #endif
   1617 	default:
   1618 		return ENOTTY;
   1619 	}
   1620 
   1621 #ifdef DIAGNOSTIC
   1622 	panic("fdioctl: impossible");
   1623 #endif
   1624 }
   1625 
   1626 void
   1627 fdgetdisklabel(dev)
   1628 	dev_t dev;
   1629 {
   1630 	int unit = FDUNIT(dev), i;
   1631 	struct fd_softc *fd = fd_cd.cd_devs[unit];
   1632 	struct disklabel *lp = fd->sc_dk.dk_label;
   1633 	struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
   1634 
   1635 	bzero(lp, sizeof(struct disklabel));
   1636 	bzero(lp, sizeof(struct cpu_disklabel));
   1637 
   1638 	lp->d_type = DTYPE_FLOPPY;
   1639 	lp->d_secsize = FDC_BSIZE;
   1640 	lp->d_secpercyl = fd->sc_type->seccyl;
   1641 	lp->d_nsectors = fd->sc_type->sectrac;
   1642 	lp->d_ncylinders = fd->sc_type->tracks;
   1643 	lp->d_ntracks = fd->sc_type->heads;	/* Go figure... */
   1644 	lp->d_rpm = 3600;	/* XXX like it matters... */
   1645 
   1646 	strncpy(lp->d_typename, "floppy", sizeof(lp->d_typename));
   1647 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1648 	lp->d_interleave = 1;
   1649 
   1650 	lp->d_partitions[RAW_PART].p_offset = 0;
   1651 	lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
   1652 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1653 	lp->d_npartitions = RAW_PART + 1;
   1654 
   1655 	lp->d_magic = DISKMAGIC;
   1656 	lp->d_magic2 = DISKMAGIC;
   1657 	lp->d_checksum = dkcksum(lp);
   1658 
   1659 	/*
   1660 	 * Call the generic disklabel extraction routine.  If there's
   1661 	 * not a label there, fake it.
   1662 	 */
   1663 	if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) {
   1664 		strncpy(lp->d_packname, "default label",
   1665 		    sizeof(lp->d_packname));
   1666 		/*
   1667 		 * Reset the partition info; it might have gotten
   1668 		 * trashed in readdisklabel().
   1669 		 *
   1670 		 * XXX Why do we have to do this?  readdisklabel()
   1671 		 * should be safe...
   1672 		 */
   1673 		for (i = 0; i < MAXPARTITIONS; ++i) {
   1674 			lp->d_partitions[i].p_offset = 0;
   1675 			if (i == RAW_PART) {
   1676 				lp->d_partitions[i].p_size =
   1677 				    lp->d_secpercyl * lp->d_ncylinders;
   1678 				lp->d_partitions[i].p_fstype = FS_BSDFFS;
   1679 			} else {
   1680 				lp->d_partitions[i].p_size = 0;
   1681 				lp->d_partitions[i].p_fstype = FS_UNUSED;
   1682 			}
   1683 		}
   1684 		lp->d_npartitions = RAW_PART + 1;
   1685 	}
   1686 }
   1687 
   1688 void
   1689 fd_do_eject()
   1690 {
   1691 
   1692 	auxregbisc(AUXIO_FDS, AUXIO_FEJ);
   1693 	delay(10);
   1694 	auxregbisc(AUXIO_FEJ, AUXIO_FDS);
   1695 }
   1696 
   1697 #ifdef RAMDISK_HOOKS
   1698 int	fd_read_rd_image __P((size_t *, caddr_t *));
   1699 #endif
   1700 
   1701 /* ARGSUSED */
   1702 void
   1703 fd_mountroot_hook(dev)
   1704 	struct device *dev;
   1705 {
   1706 	int c;
   1707 
   1708 	fd_do_eject();
   1709 	kprintf("Insert filesystem floppy and press return.");
   1710 	for (;;) {
   1711 		c = cngetc();
   1712 		if ((c == '\r') || (c == '\n')) {
   1713 			kprintf("\n");
   1714 			break;
   1715 		}
   1716 	}
   1717 #ifdef RAMDISK_HOOKS
   1718 	{
   1719 	extern int (*rd_read_image) __P((size_t *, caddr_t *));
   1720 	rd_read_image = fd_read_rd_image;
   1721 	}
   1722 #endif
   1723 }
   1724 
   1725 #ifdef RAMDISK_HOOKS
   1726 #include <sys/malloc.h>
   1727 #include <sys/proc.h>
   1728 
   1729 #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT)
   1730 
   1731 int
   1732 fd_read_rd_image(sizep, addrp)
   1733 	size_t	*sizep;
   1734 	caddr_t	*addrp;
   1735 {
   1736 	struct buf buf, *bp = &buf;
   1737 	dev_t dev;
   1738 	off_t offset;
   1739 	caddr_t addr;
   1740 
   1741 	dev = makedev(54,0);	/* XXX */
   1742 
   1743 	MALLOC(addr, caddr_t, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK);
   1744 	*addrp = addr;
   1745 
   1746 	if (fdopen(dev, 0, S_IFCHR, NULL))
   1747 		panic("fd: mountroot: fdopen");
   1748 
   1749 	offset = 0;
   1750 
   1751 	for (;;) {
   1752 		bp->b_dev = dev;
   1753 		bp->b_error = 0;
   1754 		bp->b_resid = 0;
   1755 		bp->b_proc = NULL;
   1756 		bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_READ;
   1757 		bp->b_blkno = btodb(offset);
   1758 		bp->b_bcount = DEV_BSIZE;
   1759 		bp->b_data = addr;
   1760 		fdstrategy(bp);
   1761 		while ((bp->b_flags & B_DONE) == 0) {
   1762 			tsleep((caddr_t)bp, PRIBIO + 1, "physio", 0);
   1763 		}
   1764 		if (bp->b_error)
   1765 			panic("fd: mountroot: fdread error %d", bp->b_error);
   1766 
   1767 		if (bp->b_resid != 0)
   1768 			break;
   1769 
   1770 		addr += DEV_BSIZE;
   1771 		offset += DEV_BSIZE;
   1772 		if (offset + DEV_BSIZE > FDMICROROOTSIZE)
   1773 			break;
   1774 	}
   1775 	(void)fdclose(dev, 0, S_IFCHR, NULL);
   1776 	*sizep = offset;
   1777 	fd_do_eject();
   1778 	return 0;
   1779 }
   1780 #endif
   1781