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