Home | History | Annotate | Line # | Download | only in qbus
rf.c revision 1.3
      1 /*	$NetBSD: rf.c,v 1.3 2003/07/14 15:47:26 lukem Exp $	*/
      2 /*
      3  * Copyright (c) 2002 Jochen Kunz.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of Jochen Kunz may not be used to endorse or promote
     15  *    products derived from this software without specific prior
     16  *    written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY JOCHEN KUNZ
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JOCHEN KUNZ
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32 TODO:
     33 - Better LBN bound checking, block padding for SD disks.
     34 - Formating / "Set Density"
     35 - Better error handling / detaild error reason reportnig.
     36 */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: rf.c,v 1.3 2003/07/14 15:47:26 lukem Exp $");
     40 
     41 /* autoconfig stuff */
     42 #include <sys/param.h>
     43 #include <sys/device.h>
     44 #include <sys/conf.h>
     45 #include "locators.h"
     46 #include "ioconf.h"
     47 
     48 /* bus_space / bus_dma */
     49 #include <machine/bus.h>
     50 
     51 /* UniBus / QBus specific stuff */
     52 #include <dev/qbus/ubavar.h>
     53 
     54 /* disk interface */
     55 #include <sys/types.h>
     56 #include <sys/disklabel.h>
     57 #include <sys/disk.h>
     58 
     59 /* general system data and functions */
     60 #include <sys/systm.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/ioccom.h>
     63 
     64 /* physio / buffer handling */
     65 #include <sys/buf.h>
     66 
     67 /* tsleep / sleep / wakeup */
     68 #include <sys/proc.h>
     69 /* hz for above */
     70 #include <sys/kernel.h>
     71 
     72 /* bitdefinitions for RX211 */
     73 #include <dev/qbus/rfreg.h>
     74 
     75 
     76 #define	RFS_DENS	0x0001		/* single or double density */
     77 #define	RFS_AD		0x0002		/* density auto detect */
     78 #define	RFS_NOTINIT	0x0000		/* not initialized */
     79 #define	RFS_PROBING	0x0010		/* density detect / verify started */
     80 #define	RFS_FBUF	0x0020		/* Fill Buffer */
     81 #define	RFS_EBUF	0x0030		/* Empty Buffer */
     82 #define	RFS_WSEC	0x0040		/* Write Sector */
     83 #define	RFS_RSEC	0x0050		/* Read Sector */
     84 #define	RFS_SMD		0x0060		/* Set Media Density */
     85 #define	RFS_RSTAT	0x0070		/* Read Status */
     86 #define	RFS_WDDS	0x0080		/* Write Deleted Data Sector */
     87 #define	RFS_REC		0x0090		/* Read Error Code */
     88 #define	RFS_IDLE	0x00a0		/* controller is idle */
     89 #define	RFS_CMDS	0x00f0		/* command mask */
     90 #define	RFS_SETCMD(rf, state)	((rf) = ((rf) & ~RFS_CMDS) | (state))
     91 
     92 
     93 
     94 /* autoconfig stuff */
     95 static int rfc_match(struct device *, struct cfdata *, void *);
     96 static void rfc_attach(struct device *, struct device *, void *);
     97 static int rf_match(struct device *, struct cfdata *, void *);
     98 static void rf_attach(struct device *, struct device *, void *);
     99 static int rf_print(void *, const char *);
    100 
    101 /* device interfce functions / interface to disk(9) */
    102 dev_type_open(rfopen);
    103 dev_type_close(rfclose);
    104 dev_type_read(rfread);
    105 dev_type_write(rfwrite);
    106 dev_type_ioctl(rfioctl);
    107 dev_type_strategy(rfstrategy);
    108 dev_type_dump(rfdump);
    109 dev_type_size(rfsize);
    110 
    111 
    112 /* Entries in block and character major device number switch table. */
    113 const struct bdevsw rf_bdevsw = {
    114 	rfopen,
    115 	rfclose,
    116 	rfstrategy,
    117 	rfioctl,
    118 	rfdump,
    119 	rfsize,
    120 	D_DISK
    121 };
    122 
    123 const struct cdevsw rf_cdevsw = {
    124 	rfopen,
    125 	rfclose,
    126 	rfread,
    127 	rfwrite,
    128 	rfioctl,
    129 	nostop,
    130 	notty,
    131 	nopoll,
    132 	nommap,
    133 	nokqfilter,
    134 	D_DISK
    135 };
    136 
    137 
    138 
    139 struct rfc_softc {
    140 	struct device sc_dev;		/* common device data */
    141 	struct device *sc_childs[2];	/* child devices */
    142 	struct evcnt sc_intr_count;	/* Interrupt counter for statistics */
    143 	struct buf *sc_curbuf;		/* buf that is currently in work */
    144 	bus_space_tag_t sc_iot;		/* bus_space IO tag */
    145 	bus_space_handle_t sc_ioh;	/* bus_space IO handle */
    146 	bus_dma_tag_t sc_dmat;		/* bus_dma DMA tag */
    147 	bus_dmamap_t sc_dmam;		/* bus_dma DMA map */
    148 	caddr_t sc_bufidx;		/* current position in buffer data */
    149 	int sc_curchild;		/* child whos bufq is in work */
    150 	int sc_bytesleft;		/* bytes left to transfer */
    151 	u_int8_t type;			/* controller type, 1 or 2 */
    152 };
    153 
    154 
    155 
    156 CFATTACH_DECL(
    157 	rfc,
    158 	sizeof(struct rfc_softc),
    159 	rfc_match,
    160 	rfc_attach,
    161 	NULL,
    162     	NULL
    163 );
    164 
    165 
    166 
    167 struct rf_softc {
    168 	struct device sc_dev;		/* common device data */
    169 	struct disk sc_disk;		/* common disk device data */
    170 	struct bufq_state sc_bufq;	/* queue of pending transfers */
    171 	int sc_state;			/* state of drive */
    172 	int sc_open;			/* simultaneous opens */
    173 	u_int8_t sc_dnum;		/* drive number, 0 or 1 */
    174 };
    175 
    176 
    177 
    178 CFATTACH_DECL(
    179 	rf,
    180 	sizeof(struct rf_softc),
    181 	rf_match,
    182 	rf_attach,
    183 	NULL,
    184 	NULL
    185 );
    186 
    187 
    188 
    189 struct rfc_attach_args {
    190 	u_int8_t type;		/* controller type, 1 or 2 */
    191 	u_int8_t dnum;		/* drive number, 0 or 1 */
    192 };
    193 
    194 
    195 
    196 struct dkdriver rfdkdriver = {
    197 	rfstrategy
    198 };
    199 
    200 
    201 
    202 /* helper functions */
    203 int rfc_sendcmd(struct rfc_softc *, int, int, int);
    204 static void rfc_intr(void *);
    205 
    206 
    207 
    208 /*
    209  * Issue a reset command to the controller and look for the bits in
    210  * RX2CS and RX2ES.
    211  * RX2CS_RX02 and / or RX2CS_DD can be set,
    212  * RX2ES has to be set, all other bits must be 0
    213  */
    214 int
    215 rfc_match(struct device *parent, struct cfdata *match, void *aux)
    216 {
    217 	struct uba_attach_args *ua = aux;
    218 	int i;
    219 
    220 	/* Issue reset command. */
    221 	bus_space_write_2(ua->ua_iot, ua->ua_ioh, RX2CS, RX2CS_INIT);
    222 	/* Wait for the controller to become ready, that is when
    223 	 * RX2CS_DONE, RX2ES_RDY and RX2ES_ID are set. */
    224 	for (i = 0 ; i < 20 ; i++) {
    225 		if ((bus_space_read_2(ua->ua_iot, ua->ua_ioh, RX2CS)
    226 		    & RX2CS_DONE) != 0
    227 		    && (bus_space_read_2(ua->ua_iot, ua->ua_ioh, RX2ES)
    228 		    & (RX2ES_RDY | RX2ES_ID)) != 0)
    229 			break;
    230 		DELAY(100000);	/* wait 100ms */
    231 	}
    232 	/*
    233 	 * Give up if the timeout has elapsed
    234 	 * and the controller is not ready.
    235 	 */
    236 	if (i >= 20)
    237 		return(0);
    238 	/*
    239 	 * Issue a Read Status command with interrupt enabled.
    240 	 * The uba(4) driver wants to catch the interrupt to get the
    241 	 * interrupt vector and level of the device
    242 	 */
    243 	bus_space_write_2(ua->ua_iot, ua->ua_ioh, RX2CS,
    244 	    RX2CS_RSTAT | RX2CS_IE);
    245 	/*
    246 	 * Wait for command to finish, ignore errors and
    247 	 * abort if the controller does not respond within the timeout
    248 	 */
    249 	for (i = 0 ; i < 20 ; i++) {
    250 		if ((bus_space_read_2(ua->ua_iot, ua->ua_ioh, RX2CS)
    251 		    & (RX2CS_DONE | RX2CS_IE)) != 0
    252 		    && (bus_space_read_2(ua->ua_iot, ua->ua_ioh, RX2ES)
    253 		    & RX2ES_RDY) != 0 )
    254 			return(1);
    255 		DELAY(100000);	/* wait 100ms */
    256 	}
    257 	return(0);
    258 }
    259 
    260 
    261 
    262 /* #define RX02_PROBE 1 */
    263 #ifdef RX02_PROBE
    264 /*
    265  * Probe the density of an inserted floppy disk.
    266  * This is done by reading a sector from disk.
    267  * Return -1 on error, 0 on SD and 1 on DD.
    268  */
    269 int rfcprobedens(struct rfc_softc *, int);
    270 int
    271 rfcprobedens(struct rfc_softc *rfc_sc, int dnum)
    272 {
    273 	int dens_flag;
    274 	int i;
    275 
    276 	dens_flag = 0;
    277 	do {
    278 		bus_space_write_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS,
    279 		    RX2CS_RSEC | (dens_flag == 0 ? 0 : RX2CS_DD)
    280 		    | (dnum == 0 ? 0 : RX2CS_US));
    281 		/*
    282 		 * Transfer request set?
    283 		 * Wait 50us, the controller needs this time to setle
    284 		 */
    285 		DELAY(50);
    286 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    287 		    & RX2CS_TR) == 0) {
    288 			printf("%s: did not respond to Read Sector CMD(1)\n",
    289 			    rfc_sc->sc_dev.dv_xname);
    290 			return(-1);
    291 		}
    292 		bus_space_write_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2SA, 1);
    293 		/* Wait 50us, the controller needs this time to setle */
    294 		DELAY(50);
    295 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    296 		    & RX2CS_TR) == 0) {
    297 			printf("%s: did not respond to Read Sector CMD(2)\n",
    298 			    rfc_sc->sc_dev.dv_xname);
    299 			return(-1);
    300 		}
    301 		bus_space_write_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2TA, 1);
    302 		/* Wait for the command to finish */
    303 		for (i = 0 ; i < 200 ; i++) {
    304 			if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh,
    305 			    RX2CS) & RX2CS_DONE) != 0)
    306 				break;
    307 			DELAY(10000);	/* wait 10ms */
    308 		}
    309 		if (i >= 200) {
    310 			printf("%s: did not respond to Read Sector CMD(3)\n",
    311 			    rfc_sc->sc_dev.dv_xname);
    312 			return(-1);
    313 		}
    314 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    315 		    & RX2CS_ERR) == 0)
    316 			return(dens_flag);
    317 	} while (rfc_sc->type == 2 && dens_flag++ == 0);
    318 	return(-1);
    319 }
    320 #endif /* RX02_PROBE */
    321 
    322 
    323 
    324 void
    325 rfc_attach(struct device *parent, struct device *self, void *aux)
    326 {
    327 	struct rfc_softc *rfc_sc = (struct rfc_softc *)self;
    328 	struct uba_attach_args *ua = aux;
    329 	struct rfc_attach_args rfc_aa;
    330 	int i;
    331 
    332 	rfc_sc->sc_iot = ua->ua_iot;
    333 	rfc_sc->sc_ioh = ua->ua_ioh;
    334 	rfc_sc->sc_dmat = ua->ua_dmat;
    335 	rfc_sc->sc_curbuf = NULL;
    336 	/* Tell the QBus busdriver about our interrupt handler. */
    337 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec, rfc_intr, rfc_sc,
    338 	    &rfc_sc->sc_intr_count);
    339 	/* Attach to the interrupt counter, see evcnt(9) */
    340 	evcnt_attach_dynamic(&rfc_sc->sc_intr_count, EVCNT_TYPE_INTR,
    341 	    ua->ua_evcnt, rfc_sc->sc_dev.dv_xname, "intr");
    342 	/* get a bus_dma(9) handle */
    343 	i = bus_dmamap_create(rfc_sc->sc_dmat, RX2_BYTE_DD, 1, RX2_BYTE_DD, 0,
    344 	    BUS_DMA_ALLOCNOW, &rfc_sc->sc_dmam);
    345 	if (i != 0) {
    346 		printf("rfc_attach: Error creating bus DMA map: %d\n", i);
    347 		return;
    348 	}
    349 
    350 	/* Issue reset command. */
    351 	bus_space_write_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS, RX2CS_INIT);
    352 	/*
    353 	 * Wait for the controller to become ready, that is when
    354 	 * RX2CS_DONE, RX2ES_RDY and RX2ES_ID are set.
    355 	 */
    356 	for (i = 0 ; i < 20 ; i++) {
    357 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    358 		    & RX2CS_DONE) != 0
    359 		    && (bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2ES)
    360 		    & (RX2ES_RDY | RX2ES_ID)) != 0)
    361 			break;
    362 		DELAY(100000);	/* wait 100ms */
    363 	}
    364 	/*
    365 	 * Give up if the timeout has elapsed
    366 	 * and the controller is not ready.
    367 	 */
    368 	if (i >= 20) {
    369 		printf(": did not respond to INIT CMD\n");
    370 		return;
    371 	}
    372 	/* Is ths a RX01 or a RX02? */
    373 	if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    374 	    & RX2CS_RX02) != 0) {
    375 		rfc_sc->type = 2;
    376 		rfc_aa.type = 2;
    377 	} else {
    378 		rfc_sc->type = 1;
    379 		rfc_aa.type = 1;
    380 	}
    381 	printf(": RX0%d\n", rfc_sc->type);
    382 
    383 #ifndef RX02_PROBE
    384 	/*
    385 	 * Bouth disk drievs and the controller are one physical unit.
    386 	 * If we found the controller, there will be bouth disk drievs.
    387 	 * So attach them.
    388 	 */
    389 	rfc_aa.dnum = 0;
    390 	rfc_sc->sc_childs[0] = config_found(&rfc_sc->sc_dev, &rfc_aa,rf_print);
    391 	rfc_aa.dnum = 1;
    392 	rfc_sc->sc_childs[1] = config_found(&rfc_sc->sc_dev, &rfc_aa,rf_print);
    393 #else /* RX02_PROBE */
    394 	/*
    395 	 * There are clones of the DEC RX system with standard shugart
    396 	 * interface. In this case we can not be sure that there are
    397 	 * bouth disk drievs. So we want to do a detection of attached
    398 	 * drives. This is done by reading a sector from disk. This means
    399 	 * that there must be a formated disk in the drive at boot time.
    400 	 * This is bad, but I did not find an other way to detect the
    401 	 * (non)existence of a floppy drive.
    402 	 */
    403 	if (rfcprobedens(rfc_sc, 0) >= 0) {
    404 		rfc_aa.dnum = 0;
    405 		rfc_sc->sc_childs[0] = config_found(&rfc_sc->sc_dev, &rfc_aa,
    406 		    rf_print);
    407 	} else
    408 		rfc_sc->sc_childs[0] = NULL;
    409 	if (rfcprobedens(rfc_sc, 1) >= 0) {
    410 		rfc_aa.dnum = 1;
    411 		rfc_sc->sc_childs[1] = config_found(&rfc_sc->sc_dev, &rfc_aa,
    412 		    rf_print);
    413 	} else
    414 		rfc_sc->sc_childs[1] = NULL;
    415 #endif /* RX02_PROBE */
    416 	return;
    417 }
    418 
    419 
    420 
    421 int
    422 rf_match(struct device *parent, struct cfdata *match, void *aux)
    423 {
    424 	struct rfc_attach_args *rfc_aa = aux;
    425 
    426 	/*
    427 	 * Only attach if the locator is wildcarded or
    428 	 * if the specified locator addresses the current device.
    429 	 */
    430 	if (match->cf_loc[RFCCF_DRIVE] == RFCCF_DRIVE_DEFAULT ||
    431 	    match->cf_loc[RFCCF_DRIVE] == rfc_aa->dnum)
    432 		return(1);
    433 	return(0);
    434 }
    435 
    436 
    437 
    438 void
    439 rf_attach(struct device *parent, struct device *self, void *aux)
    440 {
    441 	struct rf_softc *rf_sc = (struct rf_softc *)self;
    442 	struct rfc_attach_args *rfc_aa = (struct rfc_attach_args *)aux;
    443 	struct rfc_softc *rfc_sc;
    444 	struct disklabel *dl;
    445 
    446 	rfc_sc = (struct rfc_softc *)rf_sc->sc_dev.dv_parent;
    447 	rf_sc->sc_dnum = rfc_aa->dnum;
    448 	rf_sc->sc_state = 0;
    449 	rf_sc->sc_open = 0;
    450 	rf_sc->sc_disk.dk_name = rf_sc->sc_dev.dv_xname;
    451 	rf_sc->sc_disk.dk_driver = &rfdkdriver;
    452 	disk_attach(&rf_sc->sc_disk);
    453 	dl = rf_sc->sc_disk.dk_label;
    454 	dl->d_type = DTYPE_FLOPPY;		/* drive type */
    455 	dl->d_magic = DISKMAGIC;		/* the magic number */
    456 	dl->d_magic2 = DISKMAGIC;
    457 	dl->d_typename[0] = 'R';
    458 	dl->d_typename[1] = 'X';
    459 	dl->d_typename[2] = '0';
    460 	dl->d_typename[3] = rfc_sc->type == 1 ? '1' : '2';	/* type name */
    461 	dl->d_typename[4] = '\0';
    462 	dl->d_secsize = DEV_BSIZE;		/* bytes per sector */
    463 	/*
    464 	 * Fill in some values to have a initialized data structure. Some
    465 	 * values will be reset by rfopen() depending on the actual density.
    466 	 */
    467 	dl->d_nsectors = RX2_SECTORS;		/* sectors per track */
    468 	dl->d_ntracks = 1;								/* tracks per cylinder */
    469 	dl->d_ncylinders = RX2_TRACKS;		/* cylinders per unit */
    470 	dl->d_secpercyl = RX2_SECTORS;		/* sectors per cylinder */
    471 	dl->d_secperunit = RX2_SECTORS * RX2_TRACKS;	/* sectors per unit */
    472 	dl->d_rpm = 360;			/* rotational speed */
    473 	dl->d_interleave = 1;			/* hardware sector interleave */
    474 	/* number of partitions in following */
    475 	dl->d_npartitions = MAXPARTITIONS;
    476 	dl->d_bbsize = 0;		/* size of boot area at sn0, bytes */
    477 	dl->d_sbsize = 0;		/* max size of fs superblock, bytes */
    478 	/* number of sectors in partition */
    479 	dl->d_partitions[0].p_size = 501;
    480 	dl->d_partitions[0].p_offset = 0;	/* starting sector */
    481 	dl->d_partitions[0].p_fsize = 0;	/* fs basic fragment size */
    482 	dl->d_partitions[0].p_fstype = 0;	/* fs type */
    483 	dl->d_partitions[0].p_frag = 0;		/* fs fragments per block */
    484 	dl->d_partitions[1].p_size = RX2_SECTORS * RX2_TRACKS / 2;
    485 	dl->d_partitions[1].p_offset = 0;	/* starting sector */
    486 	dl->d_partitions[1].p_fsize = 0;	/* fs basic fragment size */
    487 	dl->d_partitions[1].p_fstype = 0;	/* fs type */
    488 	dl->d_partitions[1].p_frag = 0;		/* fs fragments per block */
    489 	dl->d_partitions[2].p_size = RX2_SECTORS * RX2_TRACKS;
    490 	dl->d_partitions[2].p_offset = 0;	/* starting sector */
    491 	dl->d_partitions[2].p_fsize = 0;	/* fs basic fragment size */
    492 	dl->d_partitions[2].p_fstype = 0;	/* fs type */
    493 	dl->d_partitions[2].p_frag = 0;		/* fs fragments per block */
    494 	bufq_alloc(&rf_sc->sc_bufq, BUFQ_DISKSORT | BUFQ_SORT_CYLINDER);
    495 	printf("\n");
    496 	return;
    497 }
    498 
    499 
    500 
    501 int
    502 rf_print(void *aux, const char *name)
    503 {
    504 	struct rfc_attach_args *rfc_aa = aux;
    505 
    506 	if (name != NULL)
    507 		aprint_normal("RX0%d at %s", rfc_aa->type, name);
    508 	aprint_normal(" drive %d", rfc_aa->dnum);
    509 	return(UNCONF);
    510 }
    511 
    512 
    513 
    514 /* Send a command to the controller */
    515 int
    516 rfc_sendcmd(struct rfc_softc *rfc_sc, int cmd, int data1, int data2)
    517 {
    518 
    519 	/* Write command to CSR. */
    520 	bus_space_write_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS, cmd);
    521 	/* Wait 50us, the controller needs this time to setle. */
    522 	DELAY(50);
    523 	/* Write parameter 1 to DBR */
    524 	if ((cmd & RX2CS_FC) != RX2CS_RSTAT) {
    525 		/* Transfer request set? */
    526 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    527 		    & RX2CS_TR) == 0) {
    528 			printf("%s: did not respond to CMD %x (1)\n",
    529 			    rfc_sc->sc_dev.dv_xname, cmd);
    530 			return(-1);
    531 		}
    532 		bus_space_write_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2DB,
    533 		    data1);
    534 	}
    535 	/* Write parameter 2 to DBR */
    536 	if ((cmd & RX2CS_FC) <= RX2CS_RSEC || (cmd & RX2CS_FC) == RX2CS_WDDS) {
    537 		/* Wait 50us, the controller needs this time to setle. */
    538 		DELAY(50);
    539 		/* Transfer request set? */
    540 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    541 		    & RX2CS_TR) == 0) {
    542 			printf("%s: did not respond to CMD %x (2)\n",
    543 			    rfc_sc->sc_dev.dv_xname, cmd);
    544 			return(-1);
    545 		}
    546 		bus_space_write_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2DB,
    547 		    data2);
    548 	}
    549 	return(1);
    550 }
    551 
    552 
    553 
    554 void
    555 rfstrategy(struct buf *buf)
    556 {
    557 	struct rf_softc *rf_sc;
    558 	struct rfc_softc *rfc_sc;
    559 	int i;
    560 
    561 	i = DISKUNIT(buf->b_dev);
    562 	if (i >= rf_cd.cd_ndevs || (rf_sc = rf_cd.cd_devs[i]) == NULL) {
    563 		buf->b_flags |= B_ERROR;
    564 		buf->b_error = ENXIO;
    565 		biodone(buf);
    566 		return;
    567 	}
    568 	rfc_sc = (struct rfc_softc *)rf_sc->sc_dev.dv_parent;
    569 	/* We are going to operate on a non open dev? PANIC! */
    570 	if (rf_sc->sc_open == 0)
    571 		panic("rfstrategy: can not operate on non-open drive %s (1)",
    572 		    rf_sc->sc_dev.dv_xname);
    573 	if (buf->b_bcount == 0) {
    574 		biodone(buf);
    575 		return;
    576 	}
    577 	/*
    578 	 * BUFQ_PUT() operates on b_rawblkno. rfstrategy() gets
    579 	 * only b_blkno that is partition relative. As a floppy does not
    580 	 * have partitions b_rawblkno == b_blkno.
    581 	 */
    582 	buf->b_rawblkno = buf->b_blkno;
    583 	/*
    584 	 * from sys/kern/subr_disk.c:
    585 	 * Seek sort for disks.  We depend on the driver which calls us using
    586 	 * b_resid as the current cylinder number.
    587 	 */
    588 	i = splbio();
    589 	if (rfc_sc->sc_curbuf == NULL) {
    590 		rfc_sc->sc_curchild = rf_sc->sc_dnum;
    591 		rfc_sc->sc_curbuf = buf;
    592 		rfc_sc->sc_bufidx = buf->b_un.b_addr;
    593 		rfc_sc->sc_bytesleft = buf->b_bcount;
    594 		rfc_intr(rfc_sc);
    595 	} else {
    596 		buf->b_resid = buf->b_blkno / RX2_SECTORS;
    597 		BUFQ_PUT(&rf_sc->sc_bufq, buf);
    598 		rfc_sc->sc_curbuf->b_resid = 0;
    599 	}
    600 	splx(i);
    601 	return;
    602 }
    603 
    604 
    605 
    606 void
    607 rfc_intr(void *intarg)
    608 {
    609 	struct rfc_softc *rfc_sc = intarg;
    610 	struct rf_softc *rf_sc;
    611 	struct rf_softc *other_drive;
    612 	int i;
    613 
    614 	rf_sc = (struct rf_softc *)rfc_sc->sc_childs[rfc_sc->sc_curchild];
    615 	/*
    616 	 * First clean up from previous command...
    617 	 */
    618 	switch (rf_sc->sc_state & RFS_CMDS) {
    619 	case RFS_PROBING:	/* density detect / verify started */
    620 		disk_unbusy(&rf_sc->sc_disk, 0, 1);
    621 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    622 		    & RX2CS_ERR) == 0) {
    623 			RFS_SETCMD(rf_sc->sc_state, RFS_IDLE);
    624 			wakeup(rf_sc);
    625 		} else {
    626 			if (rfc_sc->type == 2
    627 			    && (rf_sc->sc_state & RFS_DENS) == 0
    628 			    && (rf_sc->sc_state & RFS_AD) != 0) {
    629 				/* retry at DD */
    630 				rf_sc->sc_state |= RFS_DENS;
    631 				disk_busy(&rf_sc->sc_disk);
    632 				if (rfc_sendcmd(rfc_sc, RX2CS_RSEC | RX2CS_IE
    633 				    | RX2CS_DD | (rf_sc->sc_dnum == 0
    634 				    ? 0 : RX2CS_US), 1, 1) < 0) {
    635 					rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    636 					RFS_SETCMD(rf_sc->sc_state,
    637 					    RFS_NOTINIT);
    638 					wakeup(rf_sc);
    639 				}
    640 			} else {
    641 				printf("%s: density error.\n",
    642 				    rf_sc->sc_dev.dv_xname);
    643 				rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    644 				RFS_SETCMD(rf_sc->sc_state, RFS_NOTINIT);
    645 				wakeup(rf_sc);
    646 			}
    647 		}
    648 		return;
    649 	case RFS_IDLE:	/* controller is idle */
    650 		if (rfc_sc->sc_curbuf->b_bcount
    651 		    % ((rf_sc->sc_state & RFS_DENS) == 0
    652 		    ? RX2_BYTE_SD : RX2_BYTE_DD) != 0) {
    653 			/*
    654 			 * can only handle blocks that are a multiple of the
    655 			 * physical block size
    656 			 */
    657 			rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    658 			break;
    659 		}
    660 		RFS_SETCMD(rf_sc->sc_state, (rfc_sc->sc_curbuf->b_flags
    661 		    & B_READ) != 0 ? RFS_RSEC : RFS_FBUF);
    662 		break;
    663 	case RFS_RSEC:	/* Read Sector */
    664 		disk_unbusy(&rf_sc->sc_disk, 0, 1);
    665 		/* check for errors */
    666 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    667 		    & RX2CS_ERR) != 0) {
    668 			/* should do more verbose error reporting */
    669 			printf("rfc_intr: Error while reading secotr: %x\n",
    670 			    bus_space_read_2(rfc_sc->sc_iot,
    671 			    rfc_sc->sc_ioh, RX2ES) );
    672 			rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    673 			break;
    674 		}
    675 		RFS_SETCMD(rf_sc->sc_state, RFS_EBUF);
    676 		break;
    677 	case RFS_WSEC:	/* Write Sector */
    678 		i = (rf_sc->sc_state & RFS_DENS) == 0
    679 			? RX2_BYTE_SD : RX2_BYTE_DD;
    680 		disk_unbusy(&rf_sc->sc_disk, i, 0);
    681 		/* check for errors */
    682 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    683 		    & RX2CS_ERR) != 0) {
    684 			/* should do more verbose error reporting */
    685 			printf("rfc_intr: Error while writing secotr: %x\n",
    686 			    bus_space_read_2(rfc_sc->sc_iot,
    687 			    rfc_sc->sc_ioh, RX2ES) );
    688 			rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    689 			break;
    690 		}
    691 		if (rfc_sc->sc_bytesleft > i) {
    692 			rfc_sc->sc_bytesleft -= i;
    693 			rfc_sc->sc_bufidx += i;
    694 		} else {
    695 			biodone(rfc_sc->sc_curbuf);
    696 			rfc_sc->sc_curbuf = BUFQ_GET(&rf_sc->sc_bufq);
    697 			if (rfc_sc->sc_curbuf != NULL) {
    698 				rfc_sc->sc_bufidx =
    699 				    rfc_sc->sc_curbuf->b_un.b_addr;
    700 				rfc_sc->sc_bytesleft =
    701 				    rfc_sc->sc_curbuf->b_bcount;
    702 			} else {
    703 				RFS_SETCMD(rf_sc->sc_state, RFS_IDLE);
    704 				/*
    705 				 * Switch to the other drive if there where
    706 				 * buffers queued while we where working on
    707 				 * the buffer queue of this drive
    708 				 */
    709 				other_drive = (struct rf_softc *)
    710 				    rfc_sc->sc_childs[
    711 				    rfc_sc->sc_curchild == 0 ? 1 : 0];
    712 				if (other_drive != NULL && BUFQ_PEEK(
    713 					&other_drive->sc_bufq) != NULL) {
    714 					rfc_sc->sc_curchild =
    715 					    rfc_sc->sc_curchild == 0 ? 1 : 0;
    716 					rf_sc = other_drive;
    717 					rfc_sc->sc_curbuf =
    718 					    BUFQ_GET(&rf_sc->sc_bufq);
    719 					rfc_sc->sc_bufidx =
    720 					    rfc_sc->sc_curbuf->b_un.b_addr;
    721 					rfc_sc->sc_bytesleft =
    722 					    rfc_sc->sc_curbuf->b_bcount;
    723 				} else
    724 					return;
    725 			}
    726 		}
    727 		RFS_SETCMD(rf_sc->sc_state,
    728 		    (rfc_sc->sc_curbuf->b_flags & B_READ) != 0
    729 		    ? RFS_RSEC : RFS_FBUF);
    730 		break;
    731 	case RFS_FBUF:	/* Fill Buffer */
    732 		disk_unbusy(&rf_sc->sc_disk, 0, 0);
    733 		/* check for errors */
    734 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    735 		    & RX2CS_ERR) != 0) {
    736 			/* should do more verbose error reporting */
    737 			printf("rfc_intr: Error while DMA: %x\n",
    738 			    bus_space_read_2(rfc_sc->sc_iot,
    739 			    rfc_sc->sc_ioh, RX2ES));
    740 			rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    741 		}
    742 		bus_dmamap_unload(rfc_sc->sc_dmat, rfc_sc->sc_dmam);
    743 		RFS_SETCMD(rf_sc->sc_state, RFS_WSEC);
    744 		break;
    745 	case RFS_EBUF:	/* Empty Buffer */
    746 		i = (rf_sc->sc_state & RFS_DENS) == 0
    747 		    ? RX2_BYTE_SD : RX2_BYTE_DD;
    748 		disk_unbusy(&rf_sc->sc_disk, i, 1);
    749 		/* check for errors */
    750 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    751 			& RX2CS_ERR) != 0) {
    752 			/* should do more verbose error reporting */
    753 			printf("rfc_intr: Error while DMA: %x\n",
    754 			    bus_space_read_2(rfc_sc->sc_iot,
    755 			    rfc_sc->sc_ioh, RX2ES));
    756 			rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    757 		}
    758 		bus_dmamap_unload(rfc_sc->sc_dmat, rfc_sc->sc_dmam);
    759 		if (rfc_sc->sc_bytesleft > i) {
    760 			rfc_sc->sc_bytesleft -= i;
    761 			rfc_sc->sc_bufidx += i;
    762 		} else {
    763 			biodone(rfc_sc->sc_curbuf);
    764 			rfc_sc->sc_curbuf = BUFQ_GET(&rf_sc->sc_bufq);
    765 			if (rfc_sc->sc_curbuf != NULL) {
    766 				rfc_sc->sc_bufidx =
    767 				    rfc_sc->sc_curbuf->b_un.b_addr;
    768 				rfc_sc->sc_bytesleft =
    769 				    rfc_sc->sc_curbuf->b_bcount;
    770 			} else {
    771 				RFS_SETCMD(rf_sc->sc_state, RFS_IDLE);
    772 				/*
    773 				 * Switch to the other drive if there where
    774 				 * buffers queued while we where working on
    775 				 * the buffer queue of this drive
    776 				 */
    777 				other_drive = (struct rf_softc *)
    778 				    rfc_sc->sc_childs[
    779 				    rfc_sc->sc_curchild == 0 ? 1 : 0];
    780 				if (other_drive != NULL && BUFQ_PEEK(
    781 					&other_drive->sc_bufq) != NULL) {
    782 					rfc_sc->sc_curchild =
    783 					    rfc_sc->sc_curchild == 0 ? 1 : 0;
    784 					rf_sc = other_drive;
    785 					rfc_sc->sc_curbuf =
    786 					    BUFQ_GET(&rf_sc->sc_bufq);
    787 					rfc_sc->sc_bufidx =
    788 					    rfc_sc->sc_curbuf->b_un.b_addr;
    789 					rfc_sc->sc_bytesleft =
    790 					    rfc_sc->sc_curbuf->b_bcount;
    791 				} else
    792 					return;
    793 			}
    794 		}
    795 		RFS_SETCMD(rf_sc->sc_state,
    796 		    (rfc_sc->sc_curbuf->b_flags & B_READ) != 0
    797 		    ? RFS_RSEC : RFS_FBUF);
    798 		break;
    799 	case RFS_NOTINIT: /* Controller is idle and density is not detected. */
    800 	case RFS_SMD:	/* Set Media Density */
    801 	case RFS_RSTAT:	/* Read Status */
    802 	case RFS_WDDS:	/* Write Deleted Data Sector */
    803 	case RFS_REC:	/* Read Error Code */
    804 	default:
    805 		panic("Impossible state in rfc_intr(1).\n");
    806 	}
    807 
    808 	if ((rfc_sc->sc_curbuf->b_flags & B_ERROR) != 0) {
    809 		rfc_sc->sc_curbuf->b_error = EIO;
    810 		biodone(rfc_sc->sc_curbuf);
    811 		rfc_sc->sc_curbuf = NULL;
    812 		RFS_SETCMD(rf_sc->sc_state, RFS_NOTINIT);
    813 		return;
    814 	}
    815 
    816 	/*
    817 	 * ... then initiate next command.
    818 	 */
    819 	switch (rf_sc->sc_state & RFS_CMDS) {
    820 	case RFS_NOTINIT: /* Controller is idle and density is not detected. */
    821 	case RFS_PROBING: /* density detect / verify started */
    822 	case RFS_IDLE:	/* controller is idle */
    823 		panic("Impossible state in rfc_intr(2).\n");
    824 		break;
    825 	case RFS_EBUF:	/* Empty Buffer */
    826 		i = bus_dmamap_load(rfc_sc->sc_dmat, rfc_sc->sc_dmam,
    827 		    rfc_sc->sc_bufidx, (rf_sc->sc_state & RFS_DENS) == 0
    828 		    ? RX2_BYTE_SD : RX2_BYTE_DD, rfc_sc->sc_curbuf->b_proc,
    829 		    BUS_DMA_NOWAIT);
    830 		if (i != 0) {
    831 			printf("rfc_intr: Error while loading bus DMA map: "
    832 			    "%d\n", i);
    833 			rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    834 			break;
    835 		}
    836 		disk_busy(&rf_sc->sc_disk);
    837 		if (rfc_sendcmd(rfc_sc, RX2CS_EBUF | RX2CS_IE
    838 		    | ((rf_sc->sc_state & RFS_DENS) == 0 ? 0 : RX2CS_DD)
    839 		    | (rf_sc->sc_dnum == 0 ? 0 : RX2CS_US)
    840 		    | ((rfc_sc->sc_dmam->dm_segs[0].ds_addr & 0x30000)>>4),
    841 		    ((rf_sc->sc_state & RFS_DENS) == 0
    842 		    ? RX2_BYTE_SD : RX2_BYTE_DD) / 2,
    843 		    rfc_sc->sc_dmam->dm_segs[0].ds_addr & 0xffff) < 0) {
    844 			rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    845 			bus_dmamap_unload(rfc_sc->sc_dmat, rfc_sc->sc_dmam);
    846 		}
    847 		break;
    848 	case RFS_FBUF:	/* Fill Buffer */
    849 		i = bus_dmamap_load(rfc_sc->sc_dmat, rfc_sc->sc_dmam,
    850 		    rfc_sc->sc_bufidx, (rf_sc->sc_state & RFS_DENS) == 0
    851 		    ? RX2_BYTE_SD : RX2_BYTE_DD,
    852 		    rfc_sc->sc_curbuf->b_proc, BUS_DMA_NOWAIT);
    853 		if (i != 0) {
    854 			printf("rfc_intr: Error while loading bus DMA map: "
    855 			    "%d\n", i);
    856 			rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    857 			break;
    858 		}
    859 		disk_busy(&rf_sc->sc_disk);
    860 		if (rfc_sendcmd(rfc_sc, RX2CS_FBUF | RX2CS_IE
    861 		    | ((rf_sc->sc_state & RFS_DENS) == 0 ? 0 : RX2CS_DD)
    862 		    | (rf_sc->sc_dnum == 0 ? 0 : RX2CS_US)
    863 		    | ((rfc_sc->sc_dmam->dm_segs[0].ds_addr & 0x30000)>>4),
    864 		    ((rf_sc->sc_state & RFS_DENS) == 0
    865 		    ? RX2_BYTE_SD : RX2_BYTE_DD) / 2,
    866 		    rfc_sc->sc_dmam->dm_segs[0].ds_addr & 0xffff) < 0) {
    867 			rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    868 			bus_dmamap_unload(rfc_sc->sc_dmat, rfc_sc->sc_dmam);
    869 		}
    870 		break;
    871 	case RFS_WSEC:	/* Write Sector */
    872 		i = (rfc_sc->sc_curbuf->b_bcount - rfc_sc->sc_bytesleft
    873 		    + rfc_sc->sc_curbuf->b_blkno * DEV_BSIZE) /
    874 		    ((rf_sc->sc_state & RFS_DENS) == 0
    875 		    ? RX2_BYTE_SD : RX2_BYTE_DD);
    876 		if (i > RX2_TRACKS * RX2_SECTORS) {
    877 			rfc_sc->sc_curbuf->b_resid = rfc_sc->sc_bytesleft;
    878 			rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    879 		}
    880 		disk_busy(&rf_sc->sc_disk);
    881 		if (rfc_sendcmd(rfc_sc, RX2CS_WSEC | RX2CS_IE
    882 		    | (rf_sc->sc_dnum == 0 ? 0 : RX2CS_US)
    883 		    | ((rf_sc->sc_state & RFS_DENS) == 0 ? 0 : RX2CS_DD),
    884 		    i % RX2_SECTORS + 1, i / RX2_SECTORS) < 0) {
    885 			rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    886 		}
    887 		break;
    888 	case RFS_RSEC:	/* Read Sector */
    889 		i = (rfc_sc->sc_curbuf->b_bcount - rfc_sc->sc_bytesleft
    890 		    + rfc_sc->sc_curbuf->b_blkno * DEV_BSIZE) /
    891 		    ((rf_sc->sc_state & RFS_DENS) == 0
    892 		    ? RX2_BYTE_SD : RX2_BYTE_DD);
    893 		if (i > RX2_TRACKS * RX2_SECTORS) {
    894 			rfc_sc->sc_curbuf->b_resid = rfc_sc->sc_bytesleft;
    895 			rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    896 		}
    897 		disk_busy(&rf_sc->sc_disk);
    898 		if (rfc_sendcmd(rfc_sc, RX2CS_RSEC | RX2CS_IE
    899 		    | (rf_sc->sc_dnum == 0 ? 0 : RX2CS_US)
    900 		    | ((rf_sc->sc_state & RFS_DENS) == 0 ? 0 : RX2CS_DD),
    901 		    i % RX2_SECTORS + 1, i / RX2_SECTORS) < 0) {
    902 			rfc_sc->sc_curbuf->b_flags |= B_ERROR;
    903 		}
    904 		break;
    905 	case RFS_SMD:	/* Set Media Density */
    906 	case RFS_RSTAT:	/* Read Status */
    907 	case RFS_WDDS:	/* Write Deleted Data Sector */
    908 	case RFS_REC:	/* Read Error Code */
    909 	default:
    910 		panic("Impossible state in rfc_intr(3).\n");
    911 	}
    912 
    913 	if ((rfc_sc->sc_curbuf->b_flags & B_ERROR) != 0) {
    914 		rfc_sc->sc_curbuf->b_error = EIO;
    915 		biodone(rfc_sc->sc_curbuf);
    916 		rfc_sc->sc_curbuf = NULL;
    917 		RFS_SETCMD(rf_sc->sc_state, RFS_NOTINIT);
    918 	}
    919 	return;
    920 }
    921 
    922 
    923 
    924 int
    925 rfdump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
    926 {
    927 
    928 	/* A 0.5MB floppy is much to small to take a system dump... */
    929 	return(ENXIO);
    930 }
    931 
    932 
    933 
    934 int
    935 rfsize(dev_t dev)
    936 {
    937 
    938 	return(-1);
    939 }
    940 
    941 
    942 
    943 int
    944 rfopen(dev_t dev, int oflags, int devtype, struct proc *p)
    945 {
    946 	struct rf_softc *rf_sc;
    947 	struct rfc_softc *rfc_sc;
    948 	struct disklabel *dl;
    949 	int unit;
    950 
    951 	unit = DISKUNIT(dev);
    952 	if (unit >= rf_cd.cd_ndevs || (rf_sc = rf_cd.cd_devs[unit]) == NULL) {
    953 		return(ENXIO);
    954 	}
    955 	rfc_sc = (struct rfc_softc *)rf_sc->sc_dev.dv_parent;
    956 	dl = rf_sc->sc_disk.dk_label;
    957 	switch (DISKPART(dev)) {
    958 		case 0:			/* Part. a is single density. */
    959 			rf_sc->sc_state &= ~RFS_DENS;
    960 			rf_sc->sc_state &= ~RFS_AD;
    961 		break;
    962 		case 1:			/* Part. b is double density. */
    963 			/*
    964 			 * Opening a singe density only drive
    965 			 * in double density is sensless.
    966 			 */
    967 			if (rfc_sc->type == 1) {
    968 				return(ENXIO);
    969 			}
    970 			rf_sc->sc_state |= RFS_DENS;
    971 			rf_sc->sc_state &= ~RFS_AD;
    972 		break;
    973 		case 2:			/* Part. c is auto density. */
    974 			rf_sc->sc_state |= RFS_AD;
    975 		break;
    976 		default:
    977 			return(ENXIO);
    978 		break;
    979 	}
    980 	if ((rf_sc->sc_state & RFS_CMDS) == RFS_NOTINIT) {
    981 		rfc_sc->sc_curchild = rf_sc->sc_dnum;
    982 		/*
    983 		 * Controller is idle and density is not detected.
    984 		 * Start a density probe by issuing a read sector command
    985 		 * and sleep until the density probe finished.
    986 		 * Due to this it is imposible to open unformated media.
    987 		 * As the RX02/02 is not able to format its own media,
    988 		 * media must be purchased preformated. fsck DEC makreting!
    989 		 */
    990 		RFS_SETCMD(rf_sc->sc_state, RFS_PROBING);
    991 		disk_busy(&rf_sc->sc_disk);
    992 		if (rfc_sendcmd(rfc_sc, RX2CS_RSEC | RX2CS_IE
    993 		    | (rf_sc->sc_dnum == 0 ? 0 : RX2CS_US)
    994 		    | ((rf_sc->sc_state & RFS_DENS) == 0 ? 0 : RX2CS_DD),
    995 		    1, 1) < 0) {
    996 			rf_sc->sc_state = 0;
    997 			return(ENXIO);
    998 		}
    999 		/* wait max. 2 sec for density probe to finish */
   1000 		if (tsleep(rf_sc, PRIBIO | PCATCH, "density probe", 2 * hz)
   1001 		    != 0 || (rf_sc->sc_state & RFS_CMDS) == RFS_NOTINIT) {
   1002 			/* timeout elapsed and / or somthing went wrong */
   1003 			rf_sc->sc_state = 0;
   1004 			return(ENXIO);
   1005 		}
   1006 	}
   1007 	/* disklabel. We use different fake geometries for SD and DD. */
   1008 	if ((rf_sc->sc_state & RFS_DENS) == 0) {
   1009 		dl->d_nsectors = 10;		/* sectors per track */
   1010 		dl->d_secpercyl = 10;		/* sectors per cylinder */
   1011 		dl->d_ncylinders = 50;		/* cylinders per unit */
   1012 		dl->d_secperunit = 501; /* sectors per unit */
   1013 		/* number of sectors in partition */
   1014 		dl->d_partitions[2].p_size = 500;
   1015 	} else {
   1016 		dl->d_nsectors = RX2_SECTORS / 2;  /* sectors per track */
   1017 		dl->d_secpercyl = RX2_SECTORS / 2; /* sectors per cylinder */
   1018 		dl->d_ncylinders = RX2_TRACKS;     /* cylinders per unit */
   1019 		/* sectors per unit */
   1020 		dl->d_secperunit = RX2_SECTORS * RX2_TRACKS / 2;
   1021 		/* number of sectors in partition */
   1022 		dl->d_partitions[2].p_size = RX2_SECTORS * RX2_TRACKS / 2;
   1023 	}
   1024 	rf_sc->sc_open++;
   1025 	return(0);
   1026 }
   1027 
   1028 
   1029 
   1030 int
   1031 rfclose(dev_t dev, int fflag, int devtype, struct proc *p)
   1032 {
   1033 	struct rf_softc *rf_sc;
   1034 	int unit;
   1035 
   1036 	unit = DISKUNIT(dev);
   1037 	if (unit >= rf_cd.cd_ndevs || (rf_sc = rf_cd.cd_devs[unit]) == NULL) {
   1038 		return(ENXIO);
   1039 	}
   1040 	if (rf_sc->sc_open == 0) {
   1041 		panic("rfclose: can not close non-open drive %s",
   1042 		    rf_sc->sc_dev.dv_xname);
   1043 	} else {
   1044 		if (--rf_sc->sc_open == 0) {
   1045 			rf_sc->sc_state = 0;
   1046 		}
   1047 	}
   1048 	return(0);
   1049 }
   1050 
   1051 
   1052 
   1053 int
   1054 rfread(dev_t dev, struct uio *uio, int ioflag)
   1055 {
   1056 
   1057 	return(physio(rfstrategy, NULL, dev, B_READ, minphys, uio));
   1058 }
   1059 
   1060 
   1061 
   1062 int
   1063 rfwrite(dev_t dev, struct uio *uio, int ioflag)
   1064 {
   1065 
   1066 	return(physio(rfstrategy, NULL, dev, B_WRITE, minphys, uio));
   1067 }
   1068 
   1069 
   1070 
   1071 int
   1072 rfioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)
   1073 {
   1074 	struct rf_softc *rf_sc;
   1075 	int unit;
   1076 
   1077 	unit = DISKUNIT(dev);
   1078 	if (unit >= rf_cd.cd_ndevs || (rf_sc = rf_cd.cd_devs[unit]) == NULL) {
   1079 		return(ENXIO);
   1080 	}
   1081 	/* We are going to operate on a non open dev? PANIC! */
   1082 	if (rf_sc->sc_open == 0) {
   1083 		panic("rfstrategy: can not operate on non-open drive %s (2)",
   1084 		    rf_sc->sc_dev.dv_xname);
   1085 	}
   1086 	switch (cmd) {
   1087 	/* get and set disklabel; DIOCGPART used internally */
   1088 	case DIOCGDINFO: /* get */
   1089 		memcpy(data, rf_sc->sc_disk.dk_label,
   1090 		    sizeof(struct disklabel));
   1091 		return(0);
   1092 	case DIOCSDINFO: /* set */
   1093 		return(0);
   1094 	case DIOCWDINFO: /* set, update disk */
   1095 		return(0);
   1096 	case DIOCGPART:  /* get partition */
   1097 		((struct partinfo *)data)->disklab = rf_sc->sc_disk.dk_label;
   1098 		((struct partinfo *)data)->part =
   1099 		    &rf_sc->sc_disk.dk_label->d_partitions[DISKPART(dev)];
   1100 		return(0);
   1101 
   1102 	/* do format operation, read or write */
   1103 	case DIOCRFORMAT:
   1104 	break;
   1105 	case DIOCWFORMAT:
   1106 	break;
   1107 
   1108 	case DIOCSSTEP: /* set step rate */
   1109 	break;
   1110 	case DIOCSRETRIES: /* set # of retries */
   1111 	break;
   1112 	case DIOCKLABEL: /* keep/drop label on close? */
   1113 	break;
   1114 	case DIOCWLABEL: /* write en/disable label */
   1115 	break;
   1116 
   1117 /*	case DIOCSBAD: / * set kernel dkbad */
   1118 	break; /* */
   1119 	case DIOCEJECT: /* eject removable disk */
   1120 	break;
   1121 	case ODIOCEJECT: /* eject removable disk */
   1122 	break;
   1123 	case DIOCLOCK: /* lock/unlock pack */
   1124 	break;
   1125 
   1126 	/* get default label, clear label */
   1127 	case DIOCGDEFLABEL:
   1128 	break;
   1129 	case DIOCCLRLABEL:
   1130 	break;
   1131 	default:
   1132 		return(ENOTTY);
   1133 	}
   1134 
   1135 	return(ENOTTY);
   1136 }
   1137 
   1138 
   1139