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