2 * Copyright (c) 1990 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Copyright (c) 1993, 1994 by
9 * jc@irbs.UUCP (John Capo)
10 * vak@zebub.msk.su (Serge Vakulenko)
11 * ache@astral.msk.su (Andrew A. Chernov)
13 * Copyright (c) 1993, 1994, 1995 by
14 * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
15 * dufault@hda.com (Peter Dufault)
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91
46 * $Id: fd.c,v 1.123 1998/09/15 22:07:24 gibbs Exp $
55 #include "opt_devfs.h"
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
64 #include <sys/fcntl.h>
65 #include <machine/clock.h>
66 #include <machine/ioctl_fd.h>
67 #include <sys/disklabel.h>
69 #include <sys/devicestat.h>
70 #include <sys/malloc.h>
72 #include <sys/syslog.h>
73 #include <i386/isa/isa.h>
74 #include <i386/isa/isa_device.h>
75 #include <i386/isa/fdreg.h>
76 #include <i386/isa/fdc.h>
77 #include <i386/isa/rtc.h>
78 #include <machine/stdarg.h>
80 #include <sys/ftape.h>
81 #include <i386/isa/ftreg.h>
84 #include <sys/devfsext.h>
87 /* misuse a flag to identify format operation */
88 #define B_FORMAT B_XXX
90 /* configuration flags */
91 #define FDC_PRETEND_D0 (1 << 0) /* pretend drive 0 to be there */
93 /* internally used only, not really from CMOS: */
94 #define RTCFDT_144M_PRETENDED 0x1000
97 * this biotab field doubles as a field for the physical unit number
100 #define id_physid id_scsiid
102 /* error returns for fd_cmd() */
104 #define FD_NOT_VALID -2
105 #define FDC_ERRMAX 100 /* do not log more */
108 #define NUMDENS (NUMTYPES - 6)
110 /* These defines (-1) must match index for fd_types */
111 #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */
112 #define NO_TYPE 0 /* must match NO_TYPE in ft.c */
122 #define FD_1480in5_25 9
123 #define FD_1440in5_25 10
124 #define FD_820in5_25 11
125 #define FD_800in5_25 12
126 #define FD_720in5_25 13
127 #define FD_360in5_25 14
130 static struct fd_type fd_types[NUMTYPES] =
132 { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
133 { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
134 { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
135 { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */
136 { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */
137 { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */
138 { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */
139 { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */
141 { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
142 { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
143 { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */
144 { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */
145 { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */
146 { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */
149 #define DRVS_PER_CTLR 2 /* 2 floppies */
151 /***********************************************************************\
152 * Per controller structure. *
153 \***********************************************************************/
154 struct fdc_data fdc_data[NFDC];
156 /***********************************************************************\
157 * Per drive structure. *
158 * N per controller (DRVS_PER_CTLR) *
159 \***********************************************************************/
160 static struct fd_data {
161 struct fdc_data *fdc; /* pointer to controller structure */
162 int fdsu; /* this units number on this controller */
163 int type; /* Drive type (FD_1440...) */
164 struct fd_type *ft; /* pointer to the type descriptor */
166 #define FD_OPEN 0x01 /* it's open */
167 #define FD_ACTIVE 0x02 /* it's active */
168 #define FD_MOTOR 0x04 /* motor should be on */
169 #define FD_MOTOR_WAIT 0x08 /* motor coming up */
172 #define FD_NO_TRACK -2
173 int track; /* where we think the head is */
174 int options; /* user configurable options, see ioctl_fd.h */
175 struct callout_handle toffhandle;
176 struct callout_handle tohandle;
177 struct devstat device_stats;
179 void *bdevs[1 + NUMDENS + MAXPARTITIONS];
180 void *cdevs[1 + NUMDENS + MAXPARTITIONS];
184 /***********************************************************************\
185 * Throughout this file the following conventions will be used: *
186 * fd is a pointer to the fd_data struct for the drive in question *
187 * fdc is a pointer to the fdc_data struct for the controller *
188 * fdu is the floppy drive unit number *
189 * fdcu is the floppy controller unit number *
190 * fdsu is the floppy drive unit number on that controller. (sub-unit) *
191 \***********************************************************************/
194 int ftopen(dev_t, int);
195 int ftintr(ftu_t ftu);
196 int ftclose(dev_t, int);
197 void ftstrategy(struct buf *);
198 int ftioctl(dev_t, unsigned long, caddr_t, int, struct proc *);
201 int ftattach(struct isa_device *, struct isa_device *, int);
204 /* autoconfig functions */
205 static int fdprobe(struct isa_device *);
206 static int fdattach(struct isa_device *);
208 /* needed for ft driver, thus exported */
210 int out_fdc(fdcu_t, int);
212 /* internal functions */
213 static void set_motor(fdcu_t, int, int);
216 static timeout_t fd_turnoff;
217 static timeout_t fd_motor_on;
218 static void fd_turnon(fdu_t);
219 static void fdc_reset(fdc_p);
220 static int fd_in(fdcu_t, int *);
221 static void fdstart(fdcu_t);
222 static timeout_t fd_iotimeout;
223 static timeout_t fd_pseudointr;
224 static ointhand2_t fdintr;
225 static int fdstate(fdcu_t, fdc_p);
226 static int retrier(fdcu_t);
227 static int fdformat(dev_t, struct fd_formb *, struct proc *);
229 static int enable_fifo(fdc_p fdc);
231 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */
237 #define SEEKCOMPLETE 3
239 #define RECALCOMPLETE 5
245 #define IOTIMEDOUT 11
246 #define RESETCOMPLETE 12
249 static char const * const fdstates[] =
266 /* CAUTION: fd_debug causes huge amounts of logging output */
267 static int volatile fd_debug = 0;
268 #define TRACE0(arg) if(fd_debug) printf(arg)
269 #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
270 #else /* FDC_DEBUG */
272 #define TRACE1(arg1, arg2)
273 #endif /* FDC_DEBUG */
275 /* autoconfig structure */
277 struct isa_driver fdcdriver = {
278 fdprobe, fdattach, "fdc",
281 static d_open_t Fdopen; /* NOTE, not fdopen */
282 static d_read_t fdread;
283 static d_write_t fdwrite;
284 static d_close_t fdclose;
285 static d_ioctl_t fdioctl;
286 static d_strategy_t fdstrategy;
288 /* even if SLICE defined, these are needed for the ft support. */
293 static struct cdevsw fd_cdevsw = {
294 Fdopen, fdclose, fdread, fdwrite,
295 fdioctl, nostop, nullreset, nodevtotty,
296 seltrue, nommap, fdstrategy, "fd",
297 NULL, -1, nodump, nopsize,
301 static struct isa_device *fdcdevs[NFDC];
305 fdc_err(fdcu_t fdcu, const char *s)
307 fdc_data[fdcu].fdc_errs++;
309 if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX)
310 printf("fdc%d: %s", fdcu, s);
311 else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX)
312 printf("fdc%d: too many errors, not logging any more\n",
320 * fd_cmd: Send a command to the chip. Takes a varargs with this structure:
322 * # of output bytes, output bytes as ints ...,
323 * # of input bytes, input bytes as ints ...
327 fd_cmd(fdcu_t fdcu, int n_out, ...)
335 cmd = (u_char)(va_arg(ap, int));
338 for (n = 0; n < n_out; n++)
340 if (out_fdc(fdcu, va_arg(ap, int)) < 0)
344 "cmd %x failed at out byte %d of %d\n",
346 return fdc_err(fdcu, msg);
349 n_in = va_arg(ap, int);
350 for (n = 0; n < n_in; n++)
352 int *ptr = va_arg(ap, int *);
353 if (fd_in(fdcu, ptr) < 0)
357 "cmd %02x failed at in byte %d of %d\n",
359 return fdc_err(fdcu, msg);
367 enable_fifo(fdc_p fdc)
371 if ((fdc->flags & FDC_HAS_FIFO) == 0) {
375 * Cannot use fd_cmd the normal way here, since
376 * this might be an invalid command. Thus we send the
377 * first byte, and check for an early turn of data directon.
380 if (out_fdc(fdc->fdcu, I8207X_CONFIGURE) < 0)
381 return fdc_err(fdc->fdcu, "Enable FIFO failed\n");
383 /* If command is invalid, return */
385 while ((i = inb(fdc->baseport + FDSTS) & (NE7_DIO | NE7_RQM))
386 != NE7_RQM && j-- > 0)
387 if (i == (NE7_DIO | NE7_RQM)) {
393 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
395 return fdc_err(fdc->fdcu, "Enable FIFO failed\n");
397 fdc->flags |= FDC_HAS_FIFO;
400 if (fd_cmd(fdc->fdcu, 4,
401 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
402 return fdc_err(fdc->fdcu, "Re-enable FIFO failed\n");
407 fd_sense_drive_status(fdc_p fdc, int *st3p)
411 if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
413 return fdc_err(fdc->fdcu, "Sense Drive Status failed\n");
422 fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
426 int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0);
430 (void)fdc_err(fdc->fdcu,
431 "sense intr err reading stat reg 0\n");
438 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV)
441 * There doesn't seem to have been an interrupt.
446 if (fd_in(fdc->fdcu, &cyl) < 0)
448 return fdc_err(fdc->fdcu, "can't get cyl num\n");
459 fd_read_status(fdc_p fdc, int fdsu)
463 for (i = 0; i < 7; i++)
466 * XXX types are poorly chosen. Only bytes can by read
467 * from the hardware, but fdc->status[] wants u_ints and
468 * fd_in() gives ints.
472 ret = fd_in(fdc->fdcu, &status);
473 fdc->status[i] = status;
479 fdc->flags |= FDC_STAT_VALID;
481 fdc->flags &= ~FDC_STAT_VALID;
486 /****************************************************************************/
487 /* autoconfiguration stuff */
488 /****************************************************************************/
491 * probe for existance of controller
494 fdprobe(struct isa_device *dev)
496 fdcu_t fdcu = dev->id_unit;
497 if(fdc_data[fdcu].flags & FDC_ATTACHED)
499 printf("fdc%d: unit used multiple times\n", fdcu);
504 fdc_data[fdcu].baseport = dev->id_iobase;
506 /* First - lets reset the floppy controller */
507 outb(dev->id_iobase+FDOUT, 0);
509 outb(dev->id_iobase+FDOUT, FDO_FRST);
511 /* see if it can handle a command */
513 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
522 * wire controller into system, look for floppy units
525 fdattach(struct isa_device *dev)
529 fdcu_t fdcu = dev->id_unit;
530 fdc_p fdc = fdc_data + fdcu;
532 int fdsu, st0, st3, i;
536 struct isa_device *fdup;
544 dev->id_ointr = fdintr;
546 fdc->flags |= FDC_ATTACHED;
547 fdc->dmachan = dev->id_drq;
548 /* Acquire the DMA channel forever, The driver will do the rest */
549 isa_dma_acquire(fdc->dmachan);
550 isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
551 fdc->state = DEVIDLE;
552 /* reset controller, turn motor off, clear fdout mirror reg */
553 outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
554 bufq_init(&fdc->head);
556 /* check for each floppy drive */
557 for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
558 if (fdup->id_iobase != dev->id_iobase)
562 if (fdu >= (NFD+NFT))
564 fdsu = fdup->id_physid;
565 /* look up what bios thinks we have */
567 case 0: if (dev->id_flags & FDC_PRETEND_D0)
568 fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
570 fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
572 case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
574 default: fdt = RTCFDT_NONE;
577 /* is there a unit? */
578 if ((fdt == RTCFDT_NONE)
580 || (fdsu >= DRVS_PER_CTLR)) {
586 /* If BIOS says no floppy, or > 2nd device */
587 /* Probe for and attach a floppy tape. */
588 /* Tell FT if there was already a disk */
589 /* with this unit number found. */
592 if (fdu < NFD && fd->type != NO_TYPE)
594 if (ftattach(dev, fdup, unithasfd))
596 if (fdsu < DRVS_PER_CTLR)
603 set_motor(fdcu, fdsu, TURNON);
604 DELAY(1000000); /* 1 sec */
607 fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0)
609 #ifdef FDC_PRINT_BOGUS_CHIPTYPE
610 printf("fdc%d: ", fdcu);
612 ic_type = (u_char)ic_type;
615 #ifdef FDC_PRINT_BOGUS_CHIPTYPE
618 fdc->fdct = FDC_NE765;
621 #ifdef FDC_PRINT_BOGUS_CHIPTYPE
622 printf("Intel 82077\n");
624 fdc->fdct = FDC_I82077;
627 #ifdef FDC_PRINT_BOGUS_CHIPTYPE
628 printf("NEC 72065B\n");
630 fdc->fdct = FDC_NE72065;
633 #ifdef FDC_PRINT_BOGUS_CHIPTYPE
634 printf("unknown IC type %02x\n", ic_type);
636 fdc->fdct = FDC_UNKNOWN;
639 if (fdc->fdct != FDC_NE765 &&
640 fdc->fdct != FDC_UNKNOWN &&
641 enable_fifo(fdc) == 0) {
642 printf("fdc%d: FIFO enabled", fdcu);
643 printf(", %d bytes threshold\n",
647 if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) &&
648 (st3 & NE7_ST3_T0)) {
649 /* if at track 0, first seek inwards */
650 /* seek some steps: */
651 (void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0);
652 DELAY(300000); /* ...wait a moment... */
653 (void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
656 /* If we're at track 0 first seek inwards. */
657 if ((fd_sense_drive_status(fdc, &st3) == 0) &&
658 (st3 & NE7_ST3_T0)) {
659 /* Seek some steps... */
660 if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
661 /* ...wait a moment... */
663 /* make ctrlr happy: */
664 (void)fd_sense_int(fdc, 0, 0);
668 for(i = 0; i < 2; i++) {
670 * we must recalibrate twice, just in case the
671 * heads have been beyond cylinder 76, since most
672 * FDCs still barf when attempting to recalibrate
676 if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
677 /* a second being enough for full stroke seek*/
678 DELAY(i == 0? 1000000: 300000);
680 /* anything responding? */
681 if (fd_sense_int(fdc, &st0, 0) == 0 &&
682 (st0 & NE7_ST0_EC) == 0)
683 break; /* already probed succesfully */
687 set_motor(fdcu, fdsu, TURNOFF);
689 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
692 fd->track = FD_NO_TRACK;
696 callout_handle_init(&fd->toffhandle);
697 callout_handle_init(&fd->tohandle);
698 printf("fd%d: ", fdu);
702 printf("1.2MB 5.25in\n");
705 case RTCFDT_144M | RTCFDT_144M_PRETENDED:
706 printf("config-pretended ");
710 printf("1.44MB 3.5in\n");
715 printf("2.88MB 3.5in - 1.44MB mode\n");
719 printf("360KB 5.25in\n");
723 printf("720KB 3.5in\n");
733 fd->bdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_BLK,
734 UID_ROOT, GID_OPERATOR, 0640,
736 fd->cdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_CHR,
737 UID_ROOT, GID_OPERATOR, 0640,
739 for (i = 1; i < 1 + NUMDENS; i++) {
741 * XXX this and the lookup in Fdopen() should be
750 if (i != FD_720 && i != FD_800 && i != FD_820)
754 if (i != FD_360 && i != FD_720 && i != FD_800
755 && i != FD_820 && i != FD_1200
756 && i != FD_1440 && i != FD_1480)
760 if (i != FD_720 && i != FD_800 && i != FD_820
761 && i != FD_1200 && i != FD_1440
762 && i != FD_1480 && i != FD_1720)
766 typesize = fd_types[i - 1].size / 2;
768 * XXX all these conversions give bloated code and
771 if (typesize == 1476)
773 if (typesize == 1722)
775 typemynor = mynor | i;
777 devfs_add_devswf(&fd_cdevsw, typemynor, DV_BLK,
778 UID_ROOT, GID_OPERATOR, 0640,
779 "fd%d.%d", fdu, typesize);
781 devfs_add_devswf(&fd_cdevsw, typemynor, DV_CHR,
782 UID_ROOT, GID_OPERATOR, 0640,
783 "rfd%d.%d", fdu, typesize);
786 for (i = 0; i < MAXPARTITIONS; i++) {
787 fd->bdevs[1 + NUMDENS + i] = devfs_link(fd->bdevs[0],
788 "fd%d%c", fdu, 'a' + i);
789 fd->cdevs[1 + NUMDENS + i] =
790 devfs_link(fd->cdevs[0],
791 "rfd%d%c", fdu, 'a' + i);
795 * Export the drive to the devstat interface.
797 devstat_add_entry(&fd->device_stats, "fd",
799 DEVSTAT_NO_ORDERED_TAGS,
800 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER);
809 /****************************************************************************/
810 /* motor control stuff */
811 /* remember to not deselect the drive we're working on */
812 /****************************************************************************/
814 set_motor(fdcu_t fdcu, int fdsu, int turnon)
816 int fdout = fdc_data[fdcu].fdout;
821 fdout |= (FDO_MOEN0 << fdsu) + fdsu;
823 fdout &= ~(FDO_MOEN0 << fdsu);
826 && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
827 /* gonna turn off the last drive, put FDC to bed */
828 fdout &= ~ (FDO_FRST|FDO_FDMAEN);
830 /* make sure controller is selected and specified */
831 if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
833 fdout |= (FDO_FRST|FDO_FDMAEN);
836 outb(fdc_data[fdcu].baseport+FDOUT, fdout);
837 fdc_data[fdcu].fdout = fdout;
838 TRACE1("[0x%x->FDOUT]", fdout);
843 * special case: since we have just woken up the FDC
844 * from its sleep, we silently assume the command will
845 * be accepted, and do not test for a timeout
847 (void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
848 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
850 if (fdc_data[fdcu].flags & FDC_HAS_FIFO)
851 (void) enable_fifo(&fdc_data[fdcu]);
856 fd_turnoff(void *arg1)
858 fdu_t fdu = (fdu_t)arg1;
860 fd_p fd = fd_data + fdu;
862 TRACE1("[fd%d: turnoff]", fdu);
865 * Don't turn off the motor yet if the drive is active.
866 * XXX shouldn't even schedule turnoff until drive is inactive
867 * and nothing is queued on it.
869 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fdu) {
870 fd->toffhandle = timeout(fd_turnoff, arg1, 4 * hz);
875 fd->flags &= ~FD_MOTOR;
876 set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
881 fd_motor_on(void *arg1)
883 fdu_t fdu = (fdu_t)arg1;
886 fd_p fd = fd_data + fdu;
888 fd->flags &= ~FD_MOTOR_WAIT;
889 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
891 fdintr(fd->fdc->fdcu);
899 fd_p fd = fd_data + fdu;
900 if(!(fd->flags & FD_MOTOR))
902 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
903 set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
904 timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
911 fdcu_t fdcu = fdc->fdcu;
913 /* Try a reset, keep motor on */
914 outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
915 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
917 /* enable FDC, but defer interrupts a moment */
918 outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
919 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
921 outb(fdc->baseport + FDOUT, fdc->fdout);
922 TRACE1("[0x%x->FDOUT]", fdc->fdout);
924 /* XXX after a reset, silently believe the FDC will accept commands */
925 (void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
926 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
928 if (fdc->flags & FDC_HAS_FIFO)
929 (void) enable_fifo(fdc);
932 /****************************************************************************/
934 /****************************************************************************/
938 int baseport = fdc_data[fdcu].baseport;
940 while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
941 != (NE7_DIO|NE7_RQM) && j-- > 0)
943 return fdc_err(fdcu, "ready for output in input\n");
945 return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
947 i = inb(baseport+FDDATA);
948 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
950 #else /* !FDC_DEBUG */
951 return inb(baseport+FDDATA);
952 #endif /* FDC_DEBUG */
956 * fd_in: Like in_fdc, but allows you to see if it worked.
959 fd_in(fdcu_t fdcu, int *ptr)
961 int baseport = fdc_data[fdcu].baseport;
963 while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
964 != (NE7_DIO|NE7_RQM) && j-- > 0)
966 return fdc_err(fdcu, "ready for output in input\n");
968 return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
970 i = inb(baseport+FDDATA);
971 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
974 #else /* !FDC_DEBUG */
975 i = inb(baseport+FDDATA);
979 #endif /* FDC_DEBUG */
983 out_fdc(fdcu_t fdcu, int x)
985 int baseport = fdc_data[fdcu].baseport;
988 /* Check that the direction bit is set */
990 while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
991 if (i <= 0) return fdc_err(fdcu, "direction bit not set\n");
993 /* Check that the floppy controller is ready for a command */
995 while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
997 return fdc_err(fdcu, bootverbose? "output ready timeout\n": 0);
999 /* Send the command and return */
1000 outb(baseport+FDDATA, x);
1001 TRACE1("[0x%x->FDDATA]", x);
1005 /****************************************************************************/
1006 /* fdopen/fdclose */
1007 /****************************************************************************/
1009 Fdopen(dev_t dev, int flags, int mode, struct proc *p)
1011 fdu_t fdu = FDUNIT(minor(dev));
1012 int type = FDTYPE(minor(dev));
1016 /* check for a tape open */
1017 if (type & F_TAPE_TYPE)
1018 return(ftopen(dev, flags));
1023 fdc = fd_data[fdu].fdc;
1024 if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
1029 type = fd_data[fdu].type;
1032 * For each type of basic drive, make sure we are trying
1033 * to open a type it can do,
1035 if (type != fd_data[fdu].type) {
1036 switch (fd_data[fdu].type) {
1048 type = FD_1480in5_25;
1051 type = FD_1440in5_25;
1054 type = FD_820in5_25;
1057 type = FD_800in5_25;
1060 type = FD_720in5_25;
1063 type = FD_360in5_25;
1070 if ( type != FD_1720
1082 fd_data[fdu].ft = fd_types + type - 1;
1083 fd_data[fdu].flags |= FD_OPEN;
1089 fdclose(dev_t dev, int flags, int mode, struct proc *p)
1091 fdu_t fdu = FDUNIT(minor(dev));
1094 int type = FDTYPE(minor(dev));
1096 if (type & F_TAPE_TYPE)
1097 return ftclose(dev, flags);
1099 fd_data[fdu].flags &= ~FD_OPEN;
1100 fd_data[fdu].options &= ~FDOPT_NORETRY;
1106 fdread(dev_t dev, struct uio *uio, int ioflag)
1108 return (physio(fdstrategy, NULL, dev, 1, minphys, uio));
1112 fdwrite(dev_t dev, struct uio *uio, int ioflag)
1114 return (physio(fdstrategy, NULL, dev, 0, minphys, uio));
1118 /****************************************************************************/
1120 /****************************************************************************/
1122 fdstrategy(struct buf *bp)
1124 unsigned nblocks, blknum, cando;
1132 fdu = FDUNIT(minor(bp->b_dev));
1138 if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
1139 /* ft tapes do not (yet) support strategy i/o */
1140 bp->b_error = ENODEV;
1141 bp->b_flags |= B_ERROR;
1144 /* check for controller already busy with tape */
1145 if (fdc->flags & FDC_TAPE_BUSY) {
1146 bp->b_error = EBUSY;
1147 bp->b_flags |= B_ERROR;
1151 fdblk = 128 << (fd->ft->secsize);
1152 if (!(bp->b_flags & B_FORMAT)) {
1153 if ((fdu >= NFD) || (bp->b_blkno < 0)) {
1155 "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1156 fdu, (u_long)bp->b_blkno, bp->b_bcount);
1157 bp->b_error = EINVAL;
1158 bp->b_flags |= B_ERROR;
1161 if ((bp->b_bcount % fdblk) != 0) {
1162 bp->b_error = EINVAL;
1163 bp->b_flags |= B_ERROR;
1169 * Set up block calculations.
1171 if (bp->b_blkno > 20000000) {
1173 * Reject unreasonably high block number, prevent the
1174 * multiplication below from overflowing.
1176 bp->b_error = EINVAL;
1177 bp->b_flags |= B_ERROR;
1180 blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk;
1181 nblocks = fd->ft->size;
1183 if (blknum + (bp->b_bcount / fdblk) > nblocks) {
1184 if (blknum <= nblocks) {
1185 cando = (nblocks - blknum) * fdblk;
1186 bp->b_resid = bp->b_bcount - cando;
1188 goto bad; /* not actually bad but EOF */
1190 bp->b_error = EINVAL;
1191 bp->b_flags |= B_ERROR;
1195 bp->b_pblkno = bp->b_blkno;
1197 bufqdisksort(&fdc->head, bp);
1198 untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle); /* a good idea */
1200 /* Tell devstat we are starting on the transaction */
1201 devstat_start_transaction(&fd->device_stats);
1211 /***************************************************************\
1213 * We have just queued something.. if the controller is not busy *
1214 * then simulate the case where it has just finished a command *
1215 * So that it (the interrupt routine) looks on the queue for more*
1216 * work to do and picks up what we just added. *
1217 * If the controller is already busy, we need do nothing, as it *
1218 * will pick up our work when the present work completes *
1219 \***************************************************************/
1221 fdstart(fdcu_t fdcu)
1226 if(fdc_data[fdcu].state == DEVIDLE)
1234 fd_iotimeout(void *arg1)
1240 fdcu = (fdcu_t)arg1;
1241 fdc = fdc_data + fdcu;
1242 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
1245 * Due to IBM's brain-dead design, the FDC has a faked ready
1246 * signal, hardwired to ready == true. Thus, any command
1247 * issued if there's no diskette in the drive will _never_
1248 * complete, and must be aborted by resetting the FDC.
1249 * Many thanks, Big Blue!
1250 * The FDC must not be reset directly, since that would
1251 * interfere with the state machine. Instead, pretend that
1252 * the command completed but was invalid. The state machine
1253 * will reset the FDC and retry once.
1256 fdc->status[0] = NE7_ST0_IC_IV;
1257 fdc->flags &= ~FDC_STAT_VALID;
1258 fdc->state = IOTIMEDOUT;
1263 /* just ensure it has the right spl */
1265 fd_pseudointr(void *arg1)
1267 fdcu_t fdcu = (fdcu_t)arg1;
1275 /***********************************************************************\
1277 * keep calling the state machine until it returns a 0 *
1278 * ALWAYS called at SPLBIO *
1279 \***********************************************************************/
1283 fdc_p fdc = fdc_data + fdcu;
1285 fdu_t fdu = fdc->fdu;
1287 if (fdc->flags & FDC_TAPE_BUSY)
1291 while(fdstate(fdcu, fdc))
1295 /***********************************************************************\
1296 * The controller state machine. *
1297 * if it returns a non zero value, it should be called again immediatly *
1298 \***********************************************************************/
1300 fdstate(fdcu_t fdcu, fdc_p fdc)
1303 int read, format, head, i, sec = 0, sectrac, st0, cyl, st3;
1304 unsigned blknum = 0, b_cylinder = 0;
1305 fdu_t fdu = fdc->fdu;
1307 register struct buf *bp;
1308 struct fd_formb *finfo = NULL;
1311 bp = bufq_first(&fdc->head);
1313 /***********************************************\
1314 * nothing left for this controller to do *
1315 * Force into the IDLE state, *
1316 \***********************************************/
1317 fdc->state = DEVIDLE;
1320 printf("fd%d: unexpected valid fd pointer\n",
1325 TRACE1("[fdc%d IDLE]", fdcu);
1328 fdu = FDUNIT(minor(bp->b_dev));
1330 fdblk = 128 << fd->ft->secsize;
1331 if (fdc->fd && (fd != fdc->fd))
1333 printf("fd%d: confused fd pointers\n", fdu);
1335 read = bp->b_flags & B_READ;
1336 format = bp->b_flags & B_FORMAT;
1338 finfo = (struct fd_formb *)bp->b_data;
1339 fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1342 if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
1343 blknum = (unsigned) bp->b_pblkno * DEV_BSIZE/fdblk +
1345 b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1347 TRACE1("fd%d", fdu);
1348 TRACE1("[%s]", fdstates[fdc->state]);
1349 TRACE1("(0x%x)", fd->flags);
1350 untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle);
1351 fd->toffhandle = timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
1355 case FINDWORK: /* we have found new work */
1360 outb(fdc->baseport+FDCTL, fd->ft->trans);
1361 TRACE1("[0x%x->FDCTL]", fd->ft->trans);
1362 /*******************************************************\
1363 * If the next drive has a motor startup pending, then *
1364 * it will start up in its own good time *
1365 \*******************************************************/
1366 if(fd->flags & FD_MOTOR_WAIT)
1368 fdc->state = MOTORWAIT;
1369 return(0); /* come back later */
1371 /*******************************************************\
1372 * Maybe if it's not starting, it SHOULD be starting *
1373 \*******************************************************/
1374 if (!(fd->flags & FD_MOTOR))
1376 fdc->state = MOTORWAIT;
1380 else /* at least make sure we are selected */
1382 set_motor(fdcu, fd->fdsu, TURNON);
1384 if (fdc->flags & FDC_NEEDS_RESET) {
1385 fdc->state = RESETCTLR;
1386 fdc->flags &= ~FDC_NEEDS_RESET;
1388 fdc->state = DOSEEK;
1391 if (b_cylinder == (unsigned)fd->track)
1393 fdc->state = SEEKCOMPLETE;
1396 if (fd_cmd(fdcu, 3, NE7CMD_SEEK,
1397 fd->fdsu, b_cylinder * fd->ft->steptrac,
1401 * seek command not accepted, looks like
1402 * the FDC went off to the Saints...
1404 fdc->retry = 6; /* try a reset */
1405 return(retrier(fdcu));
1407 fd->track = FD_NO_TRACK;
1408 fdc->state = SEEKWAIT;
1409 return(0); /* will return later */
1411 /* allow heads to settle */
1412 timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16);
1413 fdc->state = SEEKCOMPLETE;
1414 return(0); /* will return later */
1415 case SEEKCOMPLETE : /* SEEK DONE, START DMA */
1416 /* Make sure seek really happened*/
1417 if(fd->track == FD_NO_TRACK)
1419 int descyl = b_cylinder * fd->ft->steptrac;
1422 * This might be a "ready changed" interrupt,
1423 * which cannot really happen since the
1424 * RDY pin is hardwired to + 5 volts. This
1425 * generally indicates a "bouncing" intr
1426 * line, so do one of the following:
1428 * When running on an enhanced FDC that is
1429 * known to not go stuck after responding
1430 * with INVALID, fetch all interrupt states
1431 * until seeing either an INVALID or a
1432 * real interrupt condition.
1434 * When running on a dumb old NE765, give
1435 * up immediately. The controller will
1436 * provide up to four dummy RC interrupt
1437 * conditions right after reset (for the
1438 * corresponding four drives), so this is
1439 * our only chance to get notice that it
1440 * was not the FDC that caused the interrupt.
1442 if (fd_sense_int(fdc, &st0, &cyl)
1445 if(fdc->fdct == FDC_NE765
1446 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1447 return 0; /* hope for a real intr */
1448 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1454 * seek to cyl 0 requested; make sure we are
1457 if (fd_sense_drive_status(fdc, &st3))
1459 if ((st3 & NE7_ST3_T0) == 0) {
1461 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
1462 fdu, st3, NE7_ST3BITS);
1470 return(retrier(fdcu));
1477 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
1478 fdu, descyl, cyl, st0);
1481 return(retrier(fdcu));
1485 fd->track = b_cylinder;
1486 isa_dmastart(bp->b_flags, bp->b_data+fd->skip,
1487 format ? bp->b_bcount : fdblk, fdc->dmachan);
1488 sectrac = fd->ft->sectrac;
1489 sec = blknum % (sectrac * fd->ft->heads);
1490 head = sec / sectrac;
1491 sec = sec % sectrac + 1;
1492 fd->hddrv = ((head&1)<<2)+fdu;
1496 /* make sure the drive is writable */
1497 if(fd_sense_drive_status(fdc, &st3) != 0)
1499 /* stuck controller? */
1500 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1501 format ? bp->b_bcount : fdblk,
1503 fdc->retry = 6; /* reset the beast */
1504 return(retrier(fdcu));
1506 if(st3 & NE7_ST3_WP)
1509 * XXX YES! this is ugly.
1510 * in order to force the current operation
1511 * to fail, we will have to fake an FDC
1512 * error - all error handling is done
1515 fdc->status[0] = NE7_ST0_IC_AT;
1516 fdc->status[1] = NE7_ST1_NW;
1518 fdc->status[3] = fd->track;
1519 fdc->status[4] = head;
1520 fdc->status[5] = sec;
1521 fdc->retry = 8; /* break out immediately */
1522 fdc->state = IOTIMEDOUT; /* not really... */
1533 finfo->fd_formb_secshift,
1534 finfo->fd_formb_nsecs,
1535 finfo->fd_formb_gaplen,
1536 finfo->fd_formb_fillbyte,
1539 /* controller fell over */
1540 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1541 format ? bp->b_bcount : fdblk,
1544 return(retrier(fdcu));
1550 (read ? NE7CMD_READ : NE7CMD_WRITE),
1551 head << 2 | fdu, /* head & unit */
1552 fd->track, /* track */
1554 sec, /* sector + 1 */
1555 fd->ft->secsize, /* sector size */
1556 sectrac, /* sectors/track */
1557 fd->ft->gap, /* gap size */
1558 fd->ft->datalen, /* data length */
1561 /* the beast is sleeping again */
1562 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1563 format ? bp->b_bcount : fdblk,
1566 return(retrier(fdcu));
1569 fdc->state = IOCOMPLETE;
1570 fd->tohandle = timeout(fd_iotimeout, (caddr_t)fdcu, hz);
1571 return(0); /* will return later */
1572 case IOCOMPLETE: /* IO DONE, post-analyze */
1573 untimeout(fd_iotimeout, (caddr_t)fdcu, fd->tohandle);
1575 if (fd_read_status(fdc, fd->fdsu))
1577 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1578 format ? bp->b_bcount : fdblk,
1581 fdc->retry = 6; /* force a reset */
1582 return retrier(fdcu);
1585 fdc->state = IOTIMEDOUT;
1590 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1591 format ? bp->b_bcount : fdblk, fdc->dmachan);
1592 if (fdc->status[0] & NE7_ST0_IC)
1594 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
1595 && fdc->status[1] & NE7_ST1_OR) {
1597 * DMA overrun. Someone hogged the bus
1598 * and didn't release it in time for the
1599 * next FDC transfer.
1600 * Just restart it, don't increment retry
1603 fdc->state = SEEKCOMPLETE;
1606 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
1608 fdc->retry = 6; /* force a reset */
1609 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
1610 && fdc->status[2] & NE7_ST2_WC
1612 fdc->retry = 3; /* force recalibrate */
1613 return(retrier(fdcu));
1617 if (!format && fd->skip < bp->b_bcount - bp->b_resid)
1619 /* set up next transfer */
1620 fdc->state = DOSEEK;
1626 bufq_remove(&fdc->head, bp);
1627 /* Tell devstat we have finished with the transaction */
1628 devstat_end_transaction(&fd->device_stats,
1629 bp->b_bcount - bp->b_resid,
1631 (bp->b_flags & B_READ) ?
1632 DEVSTAT_READ : DEVSTAT_WRITE);
1636 fdc->state = FINDWORK;
1642 fdc->state = RESETCOMPLETE;
1646 * Discard all the results from the reset so that they
1647 * can't cause an unexpected interrupt later.
1649 for (i = 0; i < 4; i++)
1650 (void)fd_sense_int(fdc, &st0, &cyl);
1651 fdc->state = STARTRECAL;
1655 2, NE7CMD_RECAL, fdu,
1656 0)) /* Recalibrate Function */
1660 return(retrier(fdcu));
1662 fdc->state = RECALWAIT;
1663 return(0); /* will return later */
1665 /* allow heads to settle */
1666 timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8);
1667 fdc->state = RECALCOMPLETE;
1668 return(0); /* will return later */
1672 * See SEEKCOMPLETE for a comment on this:
1674 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1676 if(fdc->fdct == FDC_NE765
1677 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1678 return 0; /* hope for a real intr */
1679 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1680 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
1684 * a recalibrate from beyond cylinder 77
1685 * will "fail" due to the FDC limitations;
1686 * since people used to complain much about
1687 * the failure message, try not logging
1688 * this one if it seems to be the first
1691 printf("fd%d: recal failed ST0 %b cyl %d\n",
1692 fdu, st0, NE7_ST0BITS, cyl);
1693 if(fdc->retry < 3) fdc->retry = 3;
1694 return(retrier(fdcu));
1697 /* Seek (probably) necessary */
1698 fdc->state = DOSEEK;
1699 return(1); /* will return immediatly */
1701 if(fd->flags & FD_MOTOR_WAIT)
1703 return(0); /* time's not up yet */
1705 if (fdc->flags & FDC_NEEDS_RESET) {
1706 fdc->state = RESETCTLR;
1707 fdc->flags &= ~FDC_NEEDS_RESET;
1710 * If all motors were off, then the controller was
1711 * reset, so it has lost track of the current
1712 * cylinder. Recalibrate to handle this case.
1714 fdc->state = STARTRECAL;
1716 return(1); /* will return immediatly */
1718 printf("fdc%d: Unexpected FD int->", fdcu);
1719 if (fd_read_status(fdc, fd->fdsu) == 0)
1720 printf("FDC status :%x %x %x %x %x %x %x ",
1729 printf("No status available ");
1730 if (fd_sense_int(fdc, &st0, &cyl) != 0)
1732 printf("[controller is dead now]\n");
1735 printf("ST0 = %x, PCN = %x\n", st0, cyl);
1738 /*XXX confusing: some branches return immediately, others end up here*/
1739 return(1); /* Come back immediatly to new state */
1747 fdc_p fdc = fdc_data + fdcu;
1748 register struct buf *bp;
1751 bp = bufq_first(&fdc->head);
1753 if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
1757 case 0: case 1: case 2:
1758 fdc->state = SEEKCOMPLETE;
1760 case 3: case 4: case 5:
1761 fdc->state = STARTRECAL;
1764 fdc->state = RESETCTLR;
1771 dev_t sav_b_dev = bp->b_dev;
1773 bp->b_dev = makedev(major(bp->b_dev),
1774 (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
1775 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1776 fdc->fd->skip / DEV_BSIZE,
1777 (struct disklabel *)NULL);
1778 bp->b_dev = sav_b_dev;
1779 if (fdc->flags & FDC_STAT_VALID)
1782 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
1783 fdc->status[0], NE7_ST0BITS,
1784 fdc->status[1], NE7_ST1BITS,
1785 fdc->status[2], NE7_ST2BITS,
1786 fdc->status[3], fdc->status[4],
1790 printf(" (No status)\n");
1792 bp->b_flags |= B_ERROR;
1794 bp->b_resid += bp->b_bcount - fdc->fd->skip;
1795 bufq_remove(&fdc->head, bp);
1797 /* Tell devstat we have finished with the transaction */
1798 devstat_end_transaction(&fdc->fd->device_stats,
1799 bp->b_bcount - bp->b_resid,
1801 (bp->b_flags & B_READ) ? DEVSTAT_READ :
1805 fdc->state = FINDWORK;
1806 fdc->flags |= FDC_NEEDS_RESET;
1816 fdformat(dev, finfo, p)
1818 struct fd_formb *finfo;
1828 fdu = FDUNIT(minor(dev));
1830 fdblk = 128 << fd->ft->secsize;
1832 /* set up a buffer header for fdstrategy() */
1833 bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1837 * keep the process from being swapped
1839 p->p_flag |= P_PHYSIO;
1840 bzero((void *)bp, sizeof(struct buf));
1841 bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1845 * calculate a fake blkno, so fdstrategy() would initiate a
1846 * seek to the requested cylinder
1848 bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
1849 + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
1851 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1852 bp->b_data = (caddr_t)finfo;
1854 /* now do the format */
1858 /* ...and wait for it to complete */
1860 while(!(bp->b_flags & B_DONE))
1862 rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1863 if(rv == EWOULDBLOCK)
1868 if(rv == EWOULDBLOCK) {
1873 if(bp->b_flags & B_ERROR)
1876 * allow the process to be swapped
1878 p->p_flag &= ~P_PHYSIO;
1884 * TODO: don't allocate buffer on stack.
1888 fdioctl(dev, cmd, addr, flag, p)
1895 fdu_t fdu = FDUNIT(minor(dev));
1896 fd_p fd = &fd_data[fdu];
1899 struct fd_type *fdt;
1900 struct disklabel *dl;
1901 char buffer[DEV_BSIZE];
1905 int type = FDTYPE(minor(dev));
1907 /* check for a tape ioctl */
1908 if (type & F_TAPE_TYPE)
1909 return ftioctl(dev, cmd, addr, flag, p);
1912 fdblk = 128 << fd->ft->secsize;
1917 bzero(buffer, sizeof (buffer));
1918 dl = (struct disklabel *)buffer;
1919 dl->d_secsize = fdblk;
1920 fdt = fd_data[FDUNIT(minor(dev))].ft;
1921 dl->d_secpercyl = fdt->size / fdt->tracks;
1922 dl->d_type = DTYPE_FLOPPY;
1924 if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl)
1930 *(struct disklabel *)addr = *dl;
1934 if ((flag & FWRITE) == 0)
1939 if ((flag & FWRITE) == 0)
1944 if ((flag & FWRITE) == 0)
1950 dl = (struct disklabel *)addr;
1952 if ((error = setdisklabel((struct disklabel *)buffer, dl,
1956 error = writedisklabel(dev, fdstrategy,
1957 (struct disklabel *)buffer);
1960 if((flag & FWRITE) == 0)
1961 error = EBADF; /* must be opened for writing */
1962 else if(((struct fd_formb *)addr)->format_version !=
1964 error = EINVAL; /* wrong version of formatting prog */
1966 error = fdformat(dev, (struct fd_formb *)addr, p);
1969 case FD_GTYPE: /* get drive type */
1970 *(struct fd_type *)addr = *fd->ft;
1973 case FD_STYPE: /* set drive type */
1974 /* this is considered harmful; only allow for superuser */
1975 if(suser(p->p_ucred, &p->p_acflag) != 0)
1977 *fd->ft = *(struct fd_type *)addr;
1980 case FD_GOPTS: /* get drive options */
1981 *(int *)addr = fd->options;
1984 case FD_SOPTS: /* set drive options */
1985 fd->options = *(int *)addr;
1996 static fd_devsw_installed = 0;
1998 static void fd_drvinit(void *notused )
2001 if( ! fd_devsw_installed ) {
2002 cdevsw_add_generic(BDEV_MAJOR,CDEV_MAJOR, &fd_cdevsw);
2003 fd_devsw_installed = 1;
2007 SYSINIT(fddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,fd_drvinit,NULL)
2013 * Hello emacs, these are the
2016 * c-continued-statement-offset: 8
2017 * c-continued-brace-offset: 0
2018 * c-brace-offset: -8
2019 * c-brace-imaginary-offset: 0
2020 * c-argdecl-indent: 8
2021 * c-label-offset: -8
2022 * c++-hanging-braces: 1
2023 * c++-access-specifier-offset: -8
2024 * c++-empty-arglist-indent: 8
2025 * c++-friend-offset: 0