Home | History | Annotate | Line # | Download | only in dev
fd.c revision 1.65
      1 /*	$NetBSD: fd.c,v 1.65 2009/03/14 15:36:03 dsl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Leo Weppelman.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Leo Weppelman.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * This file contains a driver for the Floppy Disk Controller (FDC)
     35  * on the Atari TT. It uses the WD 1772 chip, modified for steprates.
     36  *
     37  * The ST floppy disk controller shares the access to the DMA circuitry
     38  * with other devices. For this reason the floppy disk controller makes
     39  * use of some special DMA accessing code.
     40  *
     41  * Interrupts from the FDC are in fact DMA interrupts which get their
     42  * first level handling in 'dma.c' . If the floppy driver is currently
     43  * using DMA the interrupt is signalled to 'fdcint'.
     44  *
     45  * TODO:
     46  *   - Test it with 2 drives (I don't have them)
     47  *   - Test it with an HD-drive (Don't have that either)
     48  *   - Finish ioctl's
     49  */
     50 
     51 #include <sys/cdefs.h>
     52 __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.65 2009/03/14 15:36:03 dsl Exp $");
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/callout.h>
     57 #include <sys/kernel.h>
     58 #include <sys/malloc.h>
     59 #include <sys/buf.h>
     60 #include <sys/bufq.h>
     61 #include <sys/proc.h>
     62 #include <sys/device.h>
     63 #include <sys/ioctl.h>
     64 #include <sys/fcntl.h>
     65 #include <sys/conf.h>
     66 #include <sys/disklabel.h>
     67 #include <sys/disk.h>
     68 #include <sys/dkbad.h>
     69 #include <atari/atari/device.h>
     70 #include <atari/atari/stalloc.h>
     71 #include <machine/disklabel.h>
     72 #include <machine/iomap.h>
     73 #include <machine/mfp.h>
     74 #include <machine/dma.h>
     75 #include <machine/video.h>
     76 #include <machine/cpu.h>
     77 #include <atari/dev/ym2149reg.h>
     78 #include <atari/dev/fdreg.h>
     79 
     80 /*
     81  * Be verbose for debugging
     82  */
     83 /*#define FLP_DEBUG	1 */
     84 
     85 #define	FDC_MAX_DMA_AD	0x1000000	/* No DMA possible beyond	*/
     86 
     87 /* Parameters for the disk drive. */
     88 #define SECTOR_SIZE	512	/* physical sector size in bytes	*/
     89 #define NR_DRIVES	2	/* maximum number of drives		*/
     90 #define NR_TYPES	3	/* number of diskette/drive combinations*/
     91 #define MAX_ERRORS	10	/* how often to try rd/wt before quitting*/
     92 #define STEP_DELAY	6000	/* 6ms (6000us) delay after stepping	*/
     93 
     94 
     95 #define	INV_TRK		32000	/* Should fit in unsigned short		*/
     96 #define	INV_PART	NR_TYPES
     97 
     98 /*
     99  * Driver states
    100  */
    101 #define	FLP_IDLE	0x00	/* floppy is idle			*/
    102 #define	FLP_MON		0x01	/* idle with motor on			*/
    103 #define	FLP_STAT	0x02	/* determine floppy status		*/
    104 #define	FLP_XFER	0x04	/* read/write data from floppy		*/
    105 
    106 /*
    107  * Timer delay's
    108  */
    109 #define	FLP_MONDELAY	(3 * hz)	/* motor-on delay		*/
    110 #define	FLP_XFERDELAY	(2 * hz)	/* timeout on transfer		*/
    111 
    112 /*
    113  * The density codes
    114  */
    115 #define	FLP_DD		0		/* Double density		*/
    116 #define	FLP_HD		1		/* High density			*/
    117 
    118 
    119 #define	b_block		b_resid		/* FIXME: this is not the place	*/
    120 
    121 /*
    122  * Global data for all physical floppy devices
    123  */
    124 static short	selected = 0;		/* drive/head currently selected*/
    125 static short	motoron  = 0;		/* motor is spinning		*/
    126 static short	nopens   = 0;		/* Number of opens executed	*/
    127 
    128 static short	fd_state = FLP_IDLE;	/* Current driver state		*/
    129 static int	lock_stat= 0;		/* DMA locking status		*/
    130 static short	fd_cmd   = 0;		/* command being executed	*/
    131 static const char *fd_error= NULL;	/* error from fd_xfer_ok()	*/
    132 
    133 /*
    134  * Private per device data
    135  */
    136 struct fd_softc {
    137 	struct device	sc_dv;		/* generic device info		*/
    138 	struct disk	dkdev;		/* generic disk info		*/
    139 	struct bufq_state *bufq;	/* queue of buf's		*/
    140 	struct callout	sc_motor_ch;
    141 	int		unit;		/* unit for atari controlling hw*/
    142 	int		nheads;		/* number of heads in use	*/
    143 	int		nsectors;	/* number of sectors/track	*/
    144 	int		density;	/* density code			*/
    145 	int		nblocks;	/* number of blocks on disk	*/
    146 	int		curtrk;		/* track head positioned on	*/
    147 	short		flags;		/* misc flags			*/
    148 	short		part;		/* Current open partition	*/
    149 	int		sector;		/* logical sector for I/O	*/
    150 	char		*io_data;	/* KVA for data transfer	*/
    151 	int		io_bytes;	/* bytes left for I/O		*/
    152 	int		io_dir;		/* B_READ/B_WRITE		*/
    153 	int		errcnt;		/* current error count		*/
    154 	u_char		*bounceb;	/* Bounce buffer		*/
    155 
    156 };
    157 
    158 /*
    159  * Flags in fd_softc:
    160  */
    161 #define FLPF_NOTRESP	0x001		/* Unit not responding		*/
    162 #define FLPF_ISOPEN	0x002		/* Unit is open			*/
    163 #define FLPF_SPARE	0x004		/* Not used			*/
    164 #define FLPF_HAVELAB	0x008		/* We have a valid label	*/
    165 #define FLPF_BOUNCE	0x010		/* Now using the bounce buffer	*/
    166 #define FLPF_WRTPROT	0x020		/* Unit is write-protected	*/
    167 #define FLPF_EMPTY	0x040		/* Unit is empty		*/
    168 #define FLPF_INOPEN	0x080		/* Currently being opened	*/
    169 #define FLPF_GETSTAT	0x100		/* Getting unit status		*/
    170 
    171 struct fd_types {
    172 	int		nheads;		/* Heads in use			*/
    173 	int		nsectors;	/* sectors per track		*/
    174 	int		nblocks;	/* number of blocks		*/
    175 	int		density;	/* density code			*/
    176 	const char	*descr;		/* type description		*/
    177 } fdtypes[NR_TYPES] = {
    178 		{ 1,  9,  720 , FLP_DD , "360KB" },	/* 360  Kb	*/
    179 		{ 2,  9, 1440 , FLP_DD , "720KB" },	/* 720  Kb	*/
    180 		{ 2, 18, 2880 , FLP_HD , "1.44MB" },	/* 1.44 Mb	*/
    181 };
    182 
    183 #define	FLP_TYPE_360	0		/* XXX: Please keep these in	*/
    184 #define	FLP_TYPE_720	1		/* sync with the numbering in	*/
    185 #define	FLP_TYPE_144	2		/* 'fdtypes' right above!	*/
    186 
    187 /*
    188  * This is set only once at attach time. The value is determined by reading
    189  * the configuration switches and is one of the FLP_TYPE_*'s.
    190  * This is simular to the way Atari handles the _FLP cookie.
    191  */
    192 static short	def_type = 0;		/* Reflects config-switches	*/
    193 
    194 #define	FLP_DEFTYPE	1		/* 720Kb, reasonable default	*/
    195 #define	FLP_TYPE(dev)	( DISKPART(dev) == 0 ? def_type : DISKPART(dev) - 1 )
    196 
    197 typedef void	(*FPV)(void *);
    198 
    199 dev_type_open(fdopen);
    200 dev_type_close(fdclose);
    201 dev_type_read(fdread);
    202 dev_type_write(fdwrite);
    203 dev_type_ioctl(fdioctl);
    204 dev_type_strategy(fdstrategy);
    205 
    206 /*
    207  * Private drive functions....
    208  */
    209 static void	fdstart(struct fd_softc *);
    210 static void	fddone(struct fd_softc *);
    211 static void	fdstatus(struct fd_softc *);
    212 static void	fd_xfer(struct fd_softc *);
    213 static void	fdcint(struct fd_softc *);
    214 static int	fd_xfer_ok(struct fd_softc *);
    215 static void	fdmotoroff(struct fd_softc *);
    216 static void	fdminphys(struct buf *);
    217 static void	fdtestdrv(struct fd_softc *);
    218 static void	fdgetdefaultlabel(struct fd_softc *, struct disklabel *,
    219 		    int);
    220 static int	fdgetdisklabel(struct fd_softc *, dev_t);
    221 static int	fdselect(int, int, int);
    222 static void	fddeselect(void);
    223 static void	fdmoff(struct fd_softc *);
    224        u_char	read_fdreg(u_short);
    225        void	write_fdreg(u_short, u_short);
    226        u_char	read_dmastat(void);
    227 
    228 extern inline u_char read_fdreg(u_short regno)
    229 {
    230 	DMA->dma_mode = regno;
    231 	return(DMA->dma_data);
    232 }
    233 
    234 extern inline void write_fdreg(u_short regno, u_short val)
    235 {
    236 	DMA->dma_mode = regno;
    237 	DMA->dma_data = val;
    238 }
    239 
    240 extern inline u_char read_dmastat(void)
    241 {
    242 	DMA->dma_mode = FDC_CS | DMA_SCREG;
    243 	return(DMA->dma_stat);
    244 }
    245 
    246 /*
    247  * Config switch stuff. Used only for the floppy type for now. That's
    248  * why it's here...
    249  * XXX: If needed in more places, it should be moved to it's own include file.
    250  * Note: This location _must_ be read as an u_short. Failure to do so
    251  *       will return garbage!
    252  */
    253 static u_short rd_cfg_switch(void);
    254 static u_short rd_cfg_switch(void)
    255 {
    256 	return(*((u_short*)AD_CFG_SWITCH));
    257 }
    258 
    259 /*
    260  * Switch definitions.
    261  * Note: ON reads as a zero bit!
    262  */
    263 #define	CFG_SWITCH_NOHD	0x4000
    264 
    265 /*
    266  * Autoconfig stuff....
    267  */
    268 extern struct cfdriver fd_cd;
    269 
    270 static int	fdcmatch(struct device *, struct cfdata *, void *);
    271 static int	fdcprint(void *, const char *);
    272 static void	fdcattach(struct device *, struct device *, void *);
    273 
    274 CFATTACH_DECL(fdc, sizeof(struct device),
    275     fdcmatch, fdcattach, NULL, NULL);
    276 
    277 const struct bdevsw fd_bdevsw = {
    278 	fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
    279 };
    280 
    281 const struct cdevsw fd_cdevsw = {
    282 	fdopen, fdclose, fdread, fdwrite, fdioctl,
    283 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    284 };
    285 
    286 static int
    287 fdcmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
    288 {
    289 	static int	fdc_matched = 0;
    290 
    291 	/* Match only once */
    292 	if(strcmp("fdc", auxp) || fdc_matched)
    293 		return(0);
    294 	fdc_matched = 1;
    295 	return(1);
    296 }
    297 
    298 static void
    299 fdcattach(pdp, dp, auxp)
    300 struct device	*pdp, *dp;
    301 void		*auxp;
    302 {
    303 	struct fd_softc	fdsoftc;
    304 	int		i, nfound, first_found;
    305 
    306 	nfound = first_found = 0;
    307 	printf("\n");
    308 	fddeselect();
    309 	for(i = 0; i < NR_DRIVES; i++) {
    310 
    311 		/*
    312 		 * Test if unit is present
    313 		 */
    314 		fdsoftc.unit  = i;
    315 		fdsoftc.flags = 0;
    316 		st_dmagrab((dma_farg)fdcint, (dma_farg)fdtestdrv, &fdsoftc,
    317 								&lock_stat, 0);
    318 		st_dmafree(&fdsoftc, &lock_stat);
    319 
    320 		if(!(fdsoftc.flags & FLPF_NOTRESP)) {
    321 			if(!nfound)
    322 				first_found = i;
    323 			nfound++;
    324 			config_found(dp, (void*)i, fdcprint);
    325 		}
    326 	}
    327 
    328 	if(nfound) {
    329 		struct fd_softc *fdsc = getsoftc(fd_cd, first_found);
    330 
    331 		/*
    332 		 * Make sure motor will be turned of when a floppy is
    333 		 * inserted in the first selected drive.
    334 		 */
    335 		fdselect(first_found, 0, FLP_DD);
    336 		fd_state = FLP_MON;
    337 		callout_reset(&fdsc->sc_motor_ch, 0, (FPV)fdmotoroff, fdsc);
    338 
    339 		/*
    340 		 * enable disk related interrupts
    341 		 */
    342 		MFP->mf_ierb |= IB_DINT;
    343 		MFP->mf_iprb  = (u_int8_t)~IB_DINT;
    344 		MFP->mf_imrb |= IB_DINT;
    345 	}
    346 }
    347 
    348 static int
    349 fdcprint(void *auxp, const char *pnp)
    350 {
    351 	if (pnp != NULL)
    352 		aprint_normal("fd%d at %s:", (int)auxp, pnp);
    353 
    354 	return(UNCONF);
    355 }
    356 
    357 static int	fdmatch(struct device *, struct cfdata *, void *);
    358 static void	fdattach(struct device *, struct device *, void *);
    359 
    360 struct dkdriver fddkdriver = { fdstrategy };
    361 
    362 CFATTACH_DECL(fd, sizeof(struct fd_softc),
    363     fdmatch, fdattach, NULL, NULL);
    364 
    365 extern struct cfdriver fd_cd;
    366 
    367 static int
    368 fdmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
    369 {
    370 	return(1);
    371 }
    372 
    373 static void
    374 fdattach(pdp, dp, auxp)
    375 struct device	*pdp, *dp;
    376 void		*auxp;
    377 {
    378 	struct fd_softc	*sc;
    379 	struct fd_types *type;
    380 	u_short		swtch;
    381 
    382 	sc = device_private(dp);
    383 
    384 	callout_init(&sc->sc_motor_ch, 0);
    385 
    386 	/*
    387 	 * Find out if an Ajax chip might be installed. Set the default
    388 	 * floppy type accordingly.
    389 	 */
    390 	swtch    = rd_cfg_switch();
    391 	def_type = (swtch & CFG_SWITCH_NOHD) ? FLP_TYPE_720 : FLP_TYPE_144;
    392 	type     = &fdtypes[def_type];
    393 
    394 	printf(": %s %d cyl, %d head, %d sec\n", type->descr,
    395 		type->nblocks / (type->nsectors * type->nheads), type->nheads,
    396 		type->nsectors);
    397 
    398 	/*
    399 	 * Initialize and attach the disk structure.
    400 	 */
    401 	disk_init(&sc->dkdev, sc->sc_dv.dv_xname, &fddkdriver);
    402 	disk_attach(&sc->dkdev);
    403 }
    404 
    405 int
    406 fdioctl(dev_t dev, u_long cmd, void * addr, int flag, struct lwp *l)
    407 {
    408 	struct fd_softc *sc;
    409 
    410 	sc = getsoftc(fd_cd, DISKUNIT(dev));
    411 
    412 	if((sc->flags & FLPF_HAVELAB) == 0)
    413 		return(EBADF);
    414 
    415 	switch(cmd) {
    416 		case DIOCSBAD:
    417 			return(EINVAL);
    418 		case DIOCGDINFO:
    419 			*(struct disklabel *)addr = *(sc->dkdev.dk_label);
    420 			return(0);
    421 		case DIOCGPART:
    422 			((struct partinfo *)addr)->disklab =
    423 				sc->dkdev.dk_label;
    424 			((struct partinfo *)addr)->part =
    425 			      &sc->dkdev.dk_label->d_partitions[RAW_PART];
    426 			return(0);
    427 #ifdef notyet /* XXX LWP */
    428 		case DIOCSRETRIES:
    429 		case DIOCSSTEP:
    430 		case DIOCSDINFO:
    431 		case DIOCWDINFO:
    432 		case DIOCWLABEL:
    433 			break;
    434 #endif /* notyet */
    435 		case DIOCGDEFLABEL:
    436 			fdgetdefaultlabel(sc, (struct disklabel *)addr,
    437 			    RAW_PART);
    438 			return(0);
    439 	}
    440 	return(ENOTTY);
    441 }
    442 
    443 /*
    444  * Open the device. If this is the first open on both the floppy devices,
    445  * intialize the controller.
    446  * Note that partition info on the floppy device is used to distinguise
    447  * between 780Kb and 360Kb floppy's.
    448  *	partition 0: 360Kb
    449  *	partition 1: 780Kb
    450  */
    451 int
    452 fdopen(dev, flags, devtype, l)
    453 dev_t		dev;
    454 int		flags, devtype;
    455 struct lwp	*l;
    456 {
    457 	struct fd_softc	*sc;
    458 	int		sps;
    459 
    460 #ifdef FLP_DEBUG
    461 	printf("fdopen dev=0x%x\n", dev);
    462 #endif
    463 
    464 	if(FLP_TYPE(dev) >= NR_TYPES)
    465 		return(ENXIO);
    466 
    467 	if((sc = getsoftc(fd_cd, DISKUNIT(dev))) == NULL)
    468 		return(ENXIO);
    469 
    470 	/*
    471 	 * If no floppy currently open, reset the controller and select
    472 	 * floppy type.
    473 	 */
    474 	if(!nopens) {
    475 
    476 #ifdef FLP_DEBUG
    477 		printf("fdopen device not yet open\n");
    478 #endif
    479 		nopens++;
    480 		write_fdreg(FDC_CS, IRUPT);
    481 		delay(40);
    482 	}
    483 
    484 	/*
    485 	 * Sleep while other process is opening the device
    486 	 */
    487 	sps = splbio();
    488 	while(sc->flags & FLPF_INOPEN)
    489 		tsleep((void *)sc, PRIBIO, "fdopen", 0);
    490 	splx(sps);
    491 
    492 	if(!(sc->flags & FLPF_ISOPEN)) {
    493 		/*
    494 		 * Initialise some driver values.
    495 		 */
    496 		int	type;
    497 		void	*addr;
    498 
    499 		type = FLP_TYPE(dev);
    500 
    501 		bufq_alloc(&sc->bufq, "disksort", BUFQ_SORT_RAWBLOCK);
    502 		sc->unit        = DISKUNIT(dev);
    503 		sc->part        = RAW_PART;
    504 		sc->nheads	= fdtypes[type].nheads;
    505 		sc->nsectors	= fdtypes[type].nsectors;
    506 		sc->nblocks     = fdtypes[type].nblocks;
    507 		sc->density	= fdtypes[type].density;
    508 		sc->curtrk	= INV_TRK;
    509 		sc->sector	= 0;
    510 		sc->errcnt	= 0;
    511 		sc->bounceb	= (u_char*)alloc_stmem(SECTOR_SIZE, &addr);
    512 		if(sc->bounceb == NULL)
    513 			return(ENOMEM); /* XXX */
    514 
    515 		/*
    516 		 * Go get write protect + loaded status
    517 		 */
    518 		sc->flags |= FLPF_INOPEN|FLPF_GETSTAT;
    519 		sps = splbio();
    520 		st_dmagrab((dma_farg)fdcint, (dma_farg)fdstatus, sc,
    521 								&lock_stat, 0);
    522 		while(sc->flags & FLPF_GETSTAT)
    523 			tsleep((void *)sc, PRIBIO, "fdopen", 0);
    524 		splx(sps);
    525 		wakeup((void *)sc);
    526 
    527 		if((sc->flags & FLPF_WRTPROT) && (flags & FWRITE)) {
    528 			sc->flags = 0;
    529 			return(EPERM);
    530 		}
    531 		if(sc->flags & FLPF_EMPTY) {
    532 			sc->flags = 0;
    533 			return(ENXIO);
    534 		}
    535 		sc->flags &= ~(FLPF_INOPEN|FLPF_GETSTAT);
    536 		sc->flags |= FLPF_ISOPEN;
    537 	}
    538 	else {
    539 		/*
    540 		 * Multiply opens are granted when accessing the same type of
    541 		 * floppy (eq. the same partition).
    542 		 */
    543 		if(sc->density != fdtypes[DISKPART(dev)].density)
    544 			return(ENXIO);	/* XXX temporarely out of business */
    545 	}
    546 	fdgetdisklabel(sc, dev);
    547 #ifdef FLP_DEBUG
    548 	printf("fdopen open succeeded on type %d\n", sc->part);
    549 #endif
    550 	return (0);
    551 }
    552 
    553 int
    554 fdclose(dev, flags, devtype, l)
    555 dev_t		dev;
    556 int		flags, devtype;
    557 struct lwp	*l;
    558 {
    559 	struct fd_softc	*sc;
    560 
    561 	sc = getsoftc(fd_cd, DISKUNIT(dev));
    562 	free_stmem(sc->bounceb);
    563 	sc->flags = 0;
    564 	nopens--;
    565 
    566 #ifdef FLP_DEBUG
    567 	printf("Closed floppy device -- nopens: %d\n", nopens);
    568 #endif
    569 	return(0);
    570 }
    571 
    572 void
    573 fdstrategy(struct buf *bp)
    574 {
    575 	struct fd_softc	 *sc;
    576 	struct disklabel *lp;
    577 	int		 sps, sz;
    578 
    579 	sc = getsoftc(fd_cd, DISKUNIT(bp->b_dev));
    580 
    581 #ifdef FLP_DEBUG
    582 	printf("fdstrategy: %p, b_bcount: %ld\n", bp, bp->b_bcount);
    583 #endif
    584 
    585 	/*
    586 	 * check for valid partition and bounds
    587 	 */
    588 	lp = sc->dkdev.dk_label;
    589 	if ((sc->flags & FLPF_HAVELAB) == 0) {
    590 		bp->b_error = EIO;
    591 		goto done;
    592 	}
    593 	if (bp->b_blkno < 0 || (bp->b_bcount % SECTOR_SIZE)) {
    594 		bp->b_error = EINVAL;
    595 		goto done;
    596 	}
    597 	if (bp->b_bcount == 0)
    598 		goto done;
    599 
    600 	sz = howmany(bp->b_bcount, SECTOR_SIZE);
    601 
    602 	if (bp->b_blkno + sz > sc->nblocks) {
    603 		sz = sc->nblocks - bp->b_blkno;
    604 		if (sz == 0) /* Exactly at EndOfDisk */
    605 			goto done;
    606 		if (sz < 0) { /* Past EndOfDisk */
    607 			bp->b_error = EINVAL;
    608 			goto done;
    609 		}
    610 		/* Trucate it */
    611 		if (bp->b_flags & B_RAW)
    612 			bp->b_bcount = sz << DEV_BSHIFT;
    613 		else bp->b_bcount = sz * lp->d_secsize;
    614 	}
    615 
    616 	/* No partition translation. */
    617 	bp->b_rawblkno = bp->b_blkno;
    618 
    619 	/*
    620 	 * queue the buf and kick the low level code
    621 	 */
    622 	sps = splbio();
    623 	bufq_put(sc->bufq, bp);	/* XXX disksort_cylinder */
    624 	if (!lock_stat) {
    625 		if (fd_state & FLP_MON)
    626 			callout_stop(&sc->sc_motor_ch);
    627 		fd_state = FLP_IDLE;
    628 		st_dmagrab((dma_farg)fdcint, (dma_farg)fdstart, sc,
    629 							&lock_stat, 0);
    630 	}
    631 	splx(sps);
    632 
    633 	return;
    634 done:
    635 	bp->b_resid = bp->b_bcount;
    636 	biodone(bp);
    637 }
    638 
    639 int
    640 fdread(dev_t dev, struct uio *uio, int flags)
    641 {
    642 	return(physio(fdstrategy, NULL, dev, B_READ, fdminphys, uio));
    643 }
    644 
    645 int
    646 fdwrite(dev_t dev, struct uio *uio, int flags)
    647 {
    648 	return(physio(fdstrategy, NULL, dev, B_WRITE, fdminphys, uio));
    649 }
    650 
    651 /*
    652  * Called through DMA-dispatcher, get status.
    653  */
    654 static void
    655 fdstatus(struct fd_softc *sc)
    656 {
    657 #ifdef FLP_DEBUG
    658 	printf("fdstatus\n");
    659 #endif
    660 	sc->errcnt = 0;
    661 	fd_state   = FLP_STAT;
    662 	fd_xfer(sc);
    663 }
    664 
    665 /*
    666  * Called through the DMA-dispatcher. So we know we are the only ones
    667  * messing with the floppy-controller.
    668  * Initialize some fields in the fdsoftc for the state-machine and get
    669  * it going.
    670  */
    671 static void
    672 fdstart(struct fd_softc *sc)
    673 {
    674 	struct buf	*bp;
    675 
    676 	bp	     = bufq_peek(sc->bufq);
    677 	sc->sector   = bp->b_blkno;	/* Start sector for I/O		*/
    678 	sc->io_data  = bp->b_data;	/* KVA base for I/O		*/
    679 	sc->io_bytes = bp->b_bcount;	/* Transfer size in bytes	*/
    680 	sc->io_dir   = bp->b_flags & B_READ;/* Direction of transfer	*/
    681 	sc->errcnt   = 0;		/* No errors yet		*/
    682 	fd_state     = FLP_XFER;	/* Yes, we're going to transfer	*/
    683 
    684 	/* Instrumentation. */
    685 	disk_busy(&sc->dkdev);
    686 
    687 	fd_xfer(sc);
    688 }
    689 
    690 /*
    691  * The current transaction is finished (for good or bad). Let go of
    692  * the DMA-resources. Call biodone() to finish the transaction.
    693  * Find a new transaction to work on.
    694  */
    695 static void
    696 fddone(register struct fd_softc *sc)
    697 {
    698 	struct buf	*bp;
    699 	struct fd_softc	*sc1;
    700 	int		i, sps;
    701 
    702 	/*
    703 	 * Give others a chance to use the DMA.
    704 	 */
    705 	st_dmafree(sc, &lock_stat);
    706 
    707 
    708 	if(fd_state != FLP_STAT) {
    709 		/*
    710 		 * Finish current transaction.
    711 		 */
    712 		sps = splbio();
    713 		bp = bufq_get(sc->bufq);
    714 		if (bp == NULL)
    715 			panic("fddone");
    716 		splx(sps);
    717 
    718 #ifdef FLP_DEBUG
    719 		printf("fddone: unit: %d, buf: %p, resid: %d\n",sc->unit,bp,
    720 								sc->io_bytes);
    721 #endif
    722 		bp->b_resid = sc->io_bytes;
    723 
    724 		disk_unbusy(&sc->dkdev, (bp->b_bcount - bp->b_resid),
    725 		    (bp->b_flags & B_READ));
    726 
    727 		biodone(bp);
    728 	}
    729 	fd_state = FLP_MON;
    730 
    731 	if(lock_stat)
    732 		return;		/* XXX Is this possible?	*/
    733 
    734 	/*
    735 	 * Find a new transaction on round-robin basis.
    736 	 */
    737 	for(i = sc->unit + 1; ;i++) {
    738 		if(i >= fd_cd.cd_ndevs)
    739 			i = 0;
    740 		if((sc1 = device_lookup_private(&fd_cd, i)) == NULL)
    741 			continue;
    742 		if (bufq_peek(sc1->bufq) != NULL)
    743 			break;
    744 		if(i == sc->unit) {
    745 			callout_reset(&sc->sc_motor_ch, FLP_MONDELAY,
    746 			    (FPV)fdmotoroff, sc);
    747 #ifdef FLP_DEBUG
    748 			printf("fddone: Nothing to do\n");
    749 #endif
    750 			return;	/* No work */
    751 		}
    752 	}
    753 	fd_state = FLP_IDLE;
    754 #ifdef FLP_DEBUG
    755 	printf("fddone: Staring job on unit %d\n", sc1->unit);
    756 #endif
    757 	st_dmagrab((dma_farg)fdcint, (dma_farg)fdstart, sc1, &lock_stat, 0);
    758 }
    759 
    760 static int
    761 fdselect(drive, head, dense)
    762 int	drive, head, dense;
    763 {
    764 	int	i, spinning;
    765 #ifdef FLP_DEBUG
    766 	printf("fdselect: drive=%d, head=%d, dense=%d\n", drive, head, dense);
    767 #endif
    768 	i = ((drive == 1) ? PA_FLOP1 : PA_FLOP0) | head;
    769 	spinning = motoron;
    770 	motoron  = 1;
    771 
    772 	switch(dense) {
    773 		case FLP_DD:
    774 			DMA->dma_drvmode = 0;
    775 			break;
    776 		case FLP_HD:
    777 			DMA->dma_drvmode = (FDC_HDSET|FDC_HDSIG);
    778 			break;
    779 		default:
    780 			panic("fdselect: unknown density code");
    781 	}
    782 	if(i != selected) {
    783 		selected = i;
    784 		ym2149_fd_select((i ^ PA_FDSEL));
    785 	}
    786 	return(spinning);
    787 }
    788 
    789 static void
    790 fddeselect()
    791 {
    792 	ym2149_fd_select(PA_FDSEL);
    793 	motoron = selected = 0;
    794 	DMA->dma_drvmode   = 0;
    795 }
    796 
    797 /****************************************************************************
    798  * The following functions assume to be running as a result of a            *
    799  * disk-interrupt (e.q. spl = splbio).				            *
    800  * They form the finit-state machine, the actual driver.                    *
    801  *                                                                          *
    802  *	fdstart()/ --> fd_xfer() -> activate hardware                       *
    803  *  fdopen()          ^                                                     *
    804  *                    |                                                     *
    805  *                    +-- not ready -<------------+                         *
    806  *                                                |                         *
    807  *  fdmotoroff()/ --> fdcint() -> fd_xfer_ok() ---+                         *
    808  *  h/w interrupt                 |                                         *
    809  *                               \|/                                        *
    810  *                            finished ---> fdone()                         *
    811  *                                                                          *
    812  ****************************************************************************/
    813 static void
    814 fd_xfer(struct fd_softc *sc)
    815 {
    816 	register int	head;
    817 	register int	track, sector, hbit;
    818 		 u_long	phys_addr;
    819 
    820 	head = track = 0;
    821 	switch(fd_state) {
    822 	    case FLP_XFER:
    823 		/*
    824 		 * Calculate head/track values
    825 		 */
    826 		track  = sc->sector / sc->nsectors;
    827 		head   = track % sc->nheads;
    828 		track  = track / sc->nheads;
    829 #ifdef FLP_DEBUG
    830 		printf("fd_xfer: sector:%d,head:%d,track:%d\n", sc->sector,head,
    831 								track);
    832 #endif
    833 		break;
    834 
    835 	    case FLP_STAT:
    836 		/*
    837 		 * FLP_STAT only wants to recalibrate
    838 		 */
    839 		sc->curtrk = INV_TRK;
    840 		break;
    841 	    default:
    842 		panic("fd_xfer: wrong state (0x%x)", fd_state);
    843 	}
    844 
    845 	/*
    846 	 * Select the drive.
    847 	 */
    848 	hbit = fdselect(sc->unit, head, sc->density) ? HBIT : 0;
    849 
    850 	if(sc->curtrk == INV_TRK) {
    851 		/*
    852 		 * Recalibrate, since we lost track of head positioning.
    853 		 * The floppy disk controller has no way of determining its
    854 		 * absolute arm position (track).  Instead, it steps the
    855 		 * arm a track at a time and keeps track of where it
    856 		 * thinks it is (in software).  However, after a SEEK, the
    857 		 * hardware reads information from the diskette telling
    858 		 * where the arm actually is.  If the arm is in the wrong place,
    859 		 * a recalibration is done, which forces the arm to track 0.
    860 		 * This way the controller can get back into sync with reality.
    861 		 */
    862 		fd_cmd = RESTORE;
    863 		write_fdreg(FDC_CS, RESTORE|VBIT|hbit);
    864 		callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
    865 		    (FPV)fdmotoroff, sc);
    866 
    867 #ifdef FLP_DEBUG
    868 		printf("fd_xfer:Recalibrating drive %d\n", sc->unit);
    869 #endif
    870 		return;
    871 	}
    872 
    873 	write_fdreg(FDC_TR, sc->curtrk);
    874 
    875 	/*
    876 	 * Issue a SEEK command on the indicated drive unless the arm is
    877 	 * already positioned on the correct track.
    878 	 */
    879 	if(track != sc->curtrk) {
    880 		sc->curtrk = track;	/* be optimistic */
    881 		write_fdreg(FDC_DR, track);
    882 		write_fdreg(FDC_CS, SEEK|RATE6|VBIT|hbit);
    883 		callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
    884 		    (FPV)fdmotoroff, sc);
    885 		fd_cmd = SEEK;
    886 #ifdef FLP_DEBUG
    887 		printf("fd_xfer:Seek to track %d on drive %d\n",track,sc->unit);
    888 #endif
    889 		return;
    890 	}
    891 
    892 	/*
    893 	 * The drive is now on the proper track. Read or write 1 block.
    894 	 */
    895 	sector = sc->sector % sc->nsectors;
    896 	sector++;	/* start numbering at 1 */
    897 
    898 	write_fdreg(FDC_SR, sector);
    899 
    900 	phys_addr = (u_long)kvtop(sc->io_data);
    901 	if(phys_addr >= FDC_MAX_DMA_AD) {
    902 		/*
    903 		 * We _must_ bounce this address
    904 		 */
    905 		phys_addr = (u_long)kvtop(sc->bounceb);
    906 		if(sc->io_dir == B_WRITE)
    907 			bcopy(sc->io_data, sc->bounceb, SECTOR_SIZE);
    908 		sc->flags |= FLPF_BOUNCE;
    909 	}
    910 	st_dmaaddr_set((void *)phys_addr);	/* DMA address setup */
    911 
    912 #ifdef FLP_DEBUG
    913 	printf("fd_xfer:Start io (io_addr:%lx)\n", (u_long)kvtop(sc->io_data));
    914 #endif
    915 
    916 	if(sc->io_dir == B_READ) {
    917 		/* Issue the command */
    918 		st_dmacomm(DMA_FDC | DMA_SCREG, 1);
    919 		write_fdreg(FDC_CS, F_READ|hbit);
    920 		fd_cmd = F_READ;
    921 	}
    922 	else {
    923 		/* Issue the command */
    924 		st_dmacomm(DMA_WRBIT | DMA_FDC | DMA_SCREG, 1);
    925 		write_fdreg(DMA_WRBIT | FDC_CS, F_WRITE|hbit|EBIT|PBIT);
    926 		fd_cmd = F_WRITE;
    927 	}
    928 	callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY, (FPV)fdmotoroff, sc);
    929 }
    930 
    931 /* return values of fd_xfer_ok(): */
    932 #define X_OK			0
    933 #define X_AGAIN			1
    934 #define X_ERROR			2
    935 #define X_FAIL			3
    936 
    937 /*
    938  * Hardware interrupt function.
    939  */
    940 static void
    941 fdcint(struct fd_softc *sc)
    942 {
    943 	struct	buf	*bp;
    944 
    945 #ifdef FLP_DEBUG
    946 	printf("fdcint: unit = %d\n", sc->unit);
    947 #endif
    948 
    949 	/*
    950 	 * Cancel timeout (we made it, didn't we)
    951 	 */
    952 	callout_stop(&sc->sc_motor_ch);
    953 
    954 	switch(fd_xfer_ok(sc)) {
    955 		case X_ERROR :
    956 			if(++(sc->errcnt) < MAX_ERRORS) {
    957 				/*
    958 				 * Command failed but still retries left.
    959 				 */
    960 				break;
    961 			}
    962 			/* FALL THROUGH */
    963 		case X_FAIL  :
    964 			/*
    965 			 * Non recoverable error. Fall back to motor-on
    966 			 * idle-state.
    967 			 */
    968 			if(fd_error != NULL) {
    969 				printf("Floppy error: %s\n", fd_error);
    970 				fd_error = NULL;
    971 			}
    972 
    973 			if(fd_state == FLP_STAT) {
    974 				sc->flags |= FLPF_EMPTY;
    975 				sc->flags &= ~FLPF_GETSTAT;
    976 				wakeup((void *)sc);
    977 				fddone(sc);
    978 				return;
    979 			}
    980 
    981 			bp = bufq_peek(sc->bufq);
    982 
    983 			bp->b_error  = EIO;
    984 			fd_state     = FLP_MON;
    985 
    986 			break;
    987 		case X_AGAIN:
    988 			/*
    989 			 * Start next part of state machine.
    990 			 */
    991 			break;
    992 		case X_OK:
    993 			/*
    994 			 * Command ok and finished. Reset error-counter.
    995 			 * If there are no more bytes to transfer fall back
    996 			 * to motor-on idle state.
    997 			 */
    998 			sc->errcnt = 0;
    999 
   1000 			if(fd_state == FLP_STAT) {
   1001 				sc->flags &= ~FLPF_GETSTAT;
   1002 				wakeup((void *)sc);
   1003 				fddone(sc);
   1004 				return;
   1005 			}
   1006 
   1007 			if((sc->flags & FLPF_BOUNCE) && (sc->io_dir == B_READ))
   1008 				bcopy(sc->bounceb, sc->io_data, SECTOR_SIZE);
   1009 			sc->flags &= ~FLPF_BOUNCE;
   1010 
   1011 			sc->sector++;
   1012 			sc->io_data  += SECTOR_SIZE;
   1013 			sc->io_bytes -= SECTOR_SIZE;
   1014 			if(sc->io_bytes <= 0)
   1015 				fd_state = FLP_MON;
   1016 	}
   1017 	if(fd_state == FLP_MON)
   1018 		fddone(sc);
   1019 	else fd_xfer(sc);
   1020 }
   1021 
   1022 /*
   1023  * Determine status of last command. Should only be called through
   1024  * 'fdcint()'.
   1025  * Returns:
   1026  *	X_ERROR : Error on command; might succeed next time.
   1027  *	X_FAIL  : Error on command; will never succeed.
   1028  *	X_AGAIN : Part of a command succeeded, call 'fd_xfer()' to complete.
   1029  *	X_OK	: Command succeeded and is complete.
   1030  *
   1031  * This function only affects sc->curtrk.
   1032  */
   1033 static int
   1034 fd_xfer_ok(register struct fd_softc *sc)
   1035 {
   1036 	register int	status;
   1037 
   1038 #ifdef FLP_DEBUG
   1039 	printf("fd_xfer_ok: cmd: 0x%x, state: 0x%x\n", fd_cmd, fd_state);
   1040 #endif
   1041 	switch(fd_cmd) {
   1042 		case IRUPT:
   1043 			/*
   1044 			 * Timeout. Force a recalibrate before we try again.
   1045 			 */
   1046 			status = read_fdreg(FDC_CS);
   1047 
   1048 			fd_error = "Timeout";
   1049 			sc->curtrk = INV_TRK;
   1050 			return(X_ERROR);
   1051 		case F_READ:
   1052 			/*
   1053 			 * Test for DMA error
   1054 			 */
   1055 			status = read_dmastat();
   1056 			if(!(status & DMAOK)) {
   1057 				fd_error = "DMA error";
   1058 				return(X_ERROR);
   1059 			}
   1060 			/*
   1061 			 * Get controller status and check for errors.
   1062 			 */
   1063 			status = read_fdreg(FDC_CS);
   1064 			if(status & (RNF | CRCERR | LD_T00)) {
   1065 				fd_error = "Read error";
   1066 				if(status & RNF)
   1067 					sc->curtrk = INV_TRK;
   1068 				return(X_ERROR);
   1069 			}
   1070 			break;
   1071 		case F_WRITE:
   1072 			/*
   1073 			 * Test for DMA error
   1074 			 */
   1075 			status = read_dmastat();
   1076 			if(!(status & DMAOK)) {
   1077 				fd_error = "DMA error";
   1078 				return(X_ERROR);
   1079 			}
   1080 			/*
   1081 			 * Get controller status and check for errors.
   1082 			 */
   1083 			status = read_fdreg(FDC_CS);
   1084 			if(status & WRI_PRO) {
   1085 				fd_error = "Write protected";
   1086 				return(X_FAIL);
   1087 			}
   1088 			if(status & (RNF | CRCERR | LD_T00)) {
   1089 				fd_error = "Write error";
   1090 				sc->curtrk = INV_TRK;
   1091 				return(X_ERROR);
   1092 			}
   1093 			break;
   1094 		case SEEK:
   1095 			status = read_fdreg(FDC_CS);
   1096 			if(status & (RNF | CRCERR)) {
   1097 				fd_error = "Seek error";
   1098 				sc->curtrk = INV_TRK;
   1099 				return(X_ERROR);
   1100 			}
   1101 			return(X_AGAIN);
   1102 		case RESTORE:
   1103 			/*
   1104 			 * Determine if the recalibration succeeded.
   1105 			 */
   1106 			status = read_fdreg(FDC_CS);
   1107 			if(status & RNF) {
   1108 				fd_error = "Recalibrate error";
   1109 				/* reset controller */
   1110 				write_fdreg(FDC_CS, IRUPT);
   1111 				sc->curtrk = INV_TRK;
   1112 				return(X_ERROR);
   1113 			}
   1114 			sc->curtrk = 0;
   1115 			if(fd_state == FLP_STAT) {
   1116 				if(status & WRI_PRO)
   1117 					sc->flags |= FLPF_WRTPROT;
   1118 				break;
   1119 			}
   1120 			return(X_AGAIN);
   1121 		default:
   1122 			fd_error = "Driver error: fd_xfer_ok : Unknown state";
   1123 			return(X_FAIL);
   1124 	}
   1125 	return(X_OK);
   1126 }
   1127 
   1128 /*
   1129  * All timeouts will call this function.
   1130  */
   1131 static void
   1132 fdmotoroff(struct fd_softc *sc)
   1133 {
   1134 	int	sps;
   1135 
   1136 	/*
   1137 	 * Get at harware interrupt level
   1138 	 */
   1139 	sps = splbio();
   1140 
   1141 #if FLP_DEBUG
   1142 	printf("fdmotoroff, state = 0x%x\n", fd_state);
   1143 #endif
   1144 
   1145 	switch(fd_state) {
   1146 		case FLP_STAT :
   1147 		case FLP_XFER :
   1148 			/*
   1149 			 * Timeout during a transfer; cancel transaction
   1150 			 * set command to 'IRUPT'.
   1151 			 * A drive-interrupt is simulated to trigger the state
   1152 			 * machine.
   1153 			 */
   1154 			/*
   1155 			 * Cancel current transaction
   1156 			 */
   1157 			fd_cmd = IRUPT;
   1158 			write_fdreg(FDC_CS, IRUPT);
   1159 			delay(20);
   1160 			(void)read_fdreg(FDC_CS);
   1161 			write_fdreg(FDC_CS, RESTORE);
   1162 			break;
   1163 
   1164 		case FLP_MON  :
   1165 			/*
   1166 			 * Turn motor off.
   1167 			 */
   1168 			if(selected) {
   1169 				int tmp;
   1170 
   1171 				st_dmagrab((dma_farg)fdcint, (dma_farg)fdmoff,
   1172 								sc, &tmp, 0);
   1173 			}
   1174 			else  fd_state = FLP_IDLE;
   1175 			break;
   1176 	}
   1177 	splx(sps);
   1178 }
   1179 
   1180 /*
   1181  * min byte count to whats left of the track in question
   1182  */
   1183 static void
   1184 fdminphys(struct buf *bp)
   1185 {
   1186 	struct fd_softc	*sc;
   1187 	int		sec, toff, tsz;
   1188 
   1189 	if((sc = getsoftc(fd_cd, DISKUNIT(bp->b_dev))) == NULL)
   1190 		panic("fdminphys: couldn't get softc");
   1191 
   1192 	sec  = bp->b_blkno % (sc->nsectors * sc->nheads);
   1193 	toff = sec * SECTOR_SIZE;
   1194 	tsz  = sc->nsectors * sc->nheads * SECTOR_SIZE;
   1195 
   1196 #ifdef FLP_DEBUG
   1197 	printf("fdminphys: before %ld", bp->b_bcount);
   1198 #endif
   1199 
   1200 	bp->b_bcount = min(bp->b_bcount, tsz - toff);
   1201 
   1202 #ifdef FLP_DEBUG
   1203 	printf(" after %ld\n", bp->b_bcount);
   1204 #endif
   1205 
   1206 	minphys(bp);
   1207 }
   1208 
   1209 /*
   1210  * Called from fdmotoroff to turn the motor actually off....
   1211  * This can't be done in fdmotoroff itself, because exclusive access to the
   1212  * DMA controller is needed to read the FDC-status register. The function
   1213  * 'fdmoff()' always runs as the result of a 'dmagrab()'.
   1214  * We need to test the status-register because we want to be sure that the
   1215  * drive motor is really off before deselecting the drive. The FDC only
   1216  * turns off the drive motor after having seen 10 index-pulses. You only
   1217  * get index-pulses when a drive is selected....This means that if the
   1218  * drive is deselected when the motor is still spinning, it will continue
   1219  * to spin _even_ when you insert a floppy later on...
   1220  */
   1221 static void
   1222 fdmoff(struct fd_softc *fdsoftc)
   1223 {
   1224 	int tmp;
   1225 
   1226 	if ((fd_state == FLP_MON) && selected) {
   1227 		tmp = read_fdreg(FDC_CS);
   1228 		if (!(tmp & MOTORON)) {
   1229 			fddeselect();
   1230 			fd_state = FLP_IDLE;
   1231 		}
   1232 		else
   1233 			callout_reset(&fdsoftc->sc_motor_ch, 10*FLP_MONDELAY,
   1234 			    (FPV)fdmotoroff, fdsoftc);
   1235 	}
   1236 	st_dmafree(fdsoftc, &tmp);
   1237 }
   1238 
   1239 /*
   1240  * Used to find out wich drives are actually connected. We do this by issuing
   1241  * is 'RESTORE' command and check if the 'track-0' bit is set. This also works
   1242  * if the drive is present but no floppy is inserted.
   1243  */
   1244 static void
   1245 fdtestdrv(struct fd_softc *fdsoftc)
   1246 {
   1247 	int	status;
   1248 
   1249 	/*
   1250 	 * Select the right unit and head.
   1251 	 */
   1252 	fdselect(fdsoftc->unit, 0, FLP_DD);
   1253 
   1254 	write_fdreg(FDC_CS, RESTORE|HBIT);
   1255 
   1256 	/*
   1257 	 * Wait for about 2 seconds.
   1258 	 */
   1259 	delay(2000000);
   1260 
   1261 	status = read_fdreg(FDC_CS);
   1262 	if(status & (RNF|BUSY)) {
   1263 		write_fdreg(FDC_CS, IRUPT);	/* reset controller */
   1264 		delay(40);
   1265 	}
   1266 
   1267 	if(!(status & LD_T00))
   1268 		fdsoftc->flags |= FLPF_NOTRESP;
   1269 
   1270 	fddeselect();
   1271 }
   1272 
   1273 static void
   1274 fdgetdefaultlabel(struct fd_softc *sc, struct disklabel *lp, int part)
   1275 {
   1276 
   1277 	bzero(lp, sizeof(struct disklabel));
   1278 
   1279 	lp->d_secsize     = SECTOR_SIZE;
   1280 	lp->d_ntracks     = sc->nheads;
   1281 	lp->d_nsectors    = sc->nsectors;
   1282 	lp->d_secpercyl   = lp->d_ntracks * lp->d_nsectors;
   1283 	lp->d_ncylinders  = sc->nblocks / lp->d_secpercyl;
   1284 	lp->d_secperunit  = sc->nblocks;
   1285 
   1286 	lp->d_type        = DTYPE_FLOPPY;
   1287 	lp->d_rpm         = 300; 	/* good guess I suppose.	*/
   1288 	lp->d_interleave  = 1;		/* FIXME: is this OK?		*/
   1289 	lp->d_bbsize      = 0;
   1290 	lp->d_sbsize      = 0;
   1291 	lp->d_npartitions = part + 1;
   1292 	lp->d_trkseek     = STEP_DELAY;
   1293 	lp->d_magic       = DISKMAGIC;
   1294 	lp->d_magic2      = DISKMAGIC;
   1295 	lp->d_checksum    = dkcksum(lp);
   1296 	lp->d_partitions[part].p_size   = lp->d_secperunit;
   1297 	lp->d_partitions[part].p_fstype = FS_UNUSED;
   1298 	lp->d_partitions[part].p_fsize  = 1024;
   1299 	lp->d_partitions[part].p_frag   = 8;
   1300 }
   1301 
   1302 /*
   1303  * Build disk label. For now we only create a label from what we know
   1304  * from 'sc'.
   1305  */
   1306 static int
   1307 fdgetdisklabel(struct fd_softc *sc, dev_t dev)
   1308 {
   1309 	struct disklabel	*lp;
   1310 	int			part;
   1311 
   1312 	/*
   1313 	 * If we already got one, get out.
   1314 	 */
   1315 	if(sc->flags & FLPF_HAVELAB)
   1316 		return(0);
   1317 
   1318 #ifdef FLP_DEBUG
   1319 	printf("fdgetdisklabel()\n");
   1320 #endif
   1321 
   1322 	part = RAW_PART;
   1323 	lp   = sc->dkdev.dk_label;
   1324 	fdgetdefaultlabel(sc, lp, part);
   1325 	sc->flags        |= FLPF_HAVELAB;
   1326 
   1327 	return(0);
   1328 }
   1329