Index: head/sys/dev/fdc/fdc.c =================================================================== --- head/sys/dev/fdc/fdc.c (revision 12690) +++ head/sys/dev/fdc/fdc.c (revision 12691) @@ -1,1945 +1,1945 @@ /* * Copyright (c) 1990 The Regents of the University of California. * All rights reserved. * * This code is derived from software contributed to Berkeley by * Don Ahn. * * Copyright (c) 1993, 1994 by * jc@irbs.UUCP (John Capo) * vak@zebub.msk.su (Serge Vakulenko) * ache@astral.msk.su (Andrew A. Chernov) * * Copyright (c) 1993, 1994, 1995 by * joerg_wunsch@uriah.sax.de (Joerg Wunsch) * dufault@hda.com (Peter Dufault) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * from: @(#)fd.c 7.4 (Berkeley) 5/25/91 - * $Id: fd.c,v 1.73 1995/12/08 11:14:10 julian Exp $ + * $Id: fd.c,v 1.74 1995/12/08 23:20:26 phk Exp $ * */ #include "ft.h" #if NFT < 1 #undef NFDC #endif #include "fd.h" #if NFDC > 0 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if NFT > 0 #include #include #endif #ifdef DEVFS #include #endif static int fd_goaway(struct kern_devconf *, int); static int fdc_goaway(struct kern_devconf *, int); static int fd_externalize(struct kern_devconf *, struct sysctl_req *); /* * Templates for the kern_devconf structures used when we attach. */ static struct kern_devconf kdc_fd[NFD] = { { 0, 0, 0, /* filled in by kern_devconf.c */ "fd", 0, { MDDT_DISK, 0 }, fd_externalize, 0, fd_goaway, DISK_EXTERNALLEN, 0, /* parent */ 0, /* parentdata */ DC_UNCONFIGURED, /* state */ "floppy disk", DC_CLS_DISK /* class */ } }; struct kern_devconf kdc_fdc[NFDC] = { { 0, 0, 0, /* filled in by kern_devconf.c */ "fdc", 0, { MDDT_ISA, 0, "bio" }, isa_generic_externalize, 0, fdc_goaway, ISA_EXTERNALLEN, 0, /* parent */ 0, /* parentdata */ DC_UNCONFIGURED, /* state */ "floppy disk/tape controller", DC_CLS_MISC /* class */ } }; static inline void fd_registerdev(int ctlr, int unit) { if(unit != 0) kdc_fd[unit] = kdc_fd[0]; kdc_fd[unit].kdc_unit = unit; kdc_fd[unit].kdc_parent = &kdc_fdc[ctlr]; kdc_fd[unit].kdc_parentdata = 0; dev_attach(&kdc_fd[unit]); } static inline void fdc_registerdev(struct isa_device *dvp) { int unit = dvp->id_unit; if(unit != 0) kdc_fdc[unit] = kdc_fdc[0]; kdc_fdc[unit].kdc_unit = unit; kdc_fdc[unit].kdc_parent = &kdc_isa0; kdc_fdc[unit].kdc_parentdata = dvp; dev_attach(&kdc_fdc[unit]); } static int fdc_goaway(struct kern_devconf *kdc, int force) { if(force) { dev_detach(kdc); return 0; } else { return EBUSY; /* XXX fix */ } } static int fd_goaway(struct kern_devconf *kdc, int force) { dev_detach(kdc); return 0; } #define b_cylin b_resid /* XXX now spelled b_cylinder elsewhere */ /* misuse a flag to identify format operation */ #define B_FORMAT B_XXX /* * this biotab field doubles as a field for the physical unit number * on the controller */ #define id_physid id_scsiid /* error returns for fd_cmd() */ #define FD_FAILED -1 #define FD_NOT_VALID -2 #define FDC_ERRMAX 100 /* do not log more */ #define NUMTYPES 14 #define NUMDENS (NUMTYPES - 6) /* These defines (-1) must match index for fd_types */ #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */ #define NO_TYPE 0 /* must match NO_TYPE in ft.c */ #define FD_1720 1 #define FD_1480 2 #define FD_1440 3 #define FD_1200 4 #define FD_820 5 #define FD_800 6 #define FD_720 7 #define FD_360 8 #define FD_1480in5_25 9 #define FD_1440in5_25 10 #define FD_820in5_25 11 #define FD_800in5_25 12 #define FD_720in5_25 13 #define FD_360in5_25 14 struct fd_type fd_types[NUMTYPES] = { { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */ { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */ { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */ { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */ { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */ { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */ { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */ { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */ { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */ { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */ { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */ { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */ { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */ { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */ }; #define DRVS_PER_CTLR 2 /* 2 floppies */ /***********************************************************************\ * Per controller structure. * \***********************************************************************/ struct fdc_data fdc_data[NFDC]; /***********************************************************************\ * Per drive structure. * * N per controller (DRVS_PER_CTLR) * \***********************************************************************/ struct fd_data { struct fdc_data *fdc; /* pointer to controller structure */ int fdsu; /* this units number on this controller */ int type; /* Drive type (FD_1440...) */ struct fd_type *ft; /* pointer to the type descriptor */ int flags; #define FD_OPEN 0x01 /* it's open */ #define FD_ACTIVE 0x02 /* it's active */ #define FD_MOTOR 0x04 /* motor should be on */ #define FD_MOTOR_WAIT 0x08 /* motor coming up */ int skip; int hddrv; #define FD_NO_TRACK -2 int track; /* where we think the head is */ int options; /* user configurable options, see ioctl_fd.h */ int dkunit; /* disk stats unit number */ #ifdef DEVFS void *rfd_devfs_token; void *fd_devfs_token; #endif } fd_data[NFD]; /***********************************************************************\ * Throughout this file the following conventions will be used: * * fd is a pointer to the fd_data struct for the drive in question * * fdc is a pointer to the fdc_data struct for the controller * * fdu is the floppy drive unit number * * fdcu is the floppy controller unit number * * fdsu is the floppy drive unit number on that controller. (sub-unit) * \***********************************************************************/ #if NFT > 0 int ftopen(dev_t, int); int ftintr(ftu_t ftu); int ftclose(dev_t, int); void ftstrategy(struct buf *); int ftioctl(dev_t, int, caddr_t, int, struct proc *); int ftdump(dev_t); int ftsize(dev_t); int ftattach(struct isa_device *, struct isa_device *, int); #endif /* autoconfig functions */ static int fdprobe(struct isa_device *); static int fdattach(struct isa_device *); /* needed for ft driver, thus exported */ int in_fdc(fdcu_t); int out_fdc(fdcu_t, int); /* internal functions */ static void set_motor(fdcu_t, int, int); # define TURNON 1 # define TURNOFF 0 static timeout_t fd_turnoff; static timeout_t fd_motor_on; static void fd_turnon(fdu_t); static void fdc_reset(fdc_p); static int fd_in(fdcu_t, int *); static void fdstart(fdcu_t); static timeout_t fd_timeout; static timeout_t fd_pseudointr; static int fdstate(fdcu_t, fdc_p); static int retrier(fdcu_t); static int fdformat(dev_t, struct fd_formb *, struct proc *); #define DEVIDLE 0 #define FINDWORK 1 #define DOSEEK 2 #define SEEKCOMPLETE 3 #define IOCOMPLETE 4 #define RECALCOMPLETE 5 #define STARTRECAL 6 #define RESETCTLR 7 #define SEEKWAIT 8 #define RECALWAIT 9 #define MOTORWAIT 10 #define IOTIMEDOUT 11 #ifdef DEBUG char *fdstates[] = { "DEVIDLE", "FINDWORK", "DOSEEK", "SEEKCOMPLETE", "IOCOMPLETE", "RECALCOMPLETE", "STARTRECAL", "RESETCTLR", "SEEKWAIT", "RECALWAIT", "MOTORWAIT", "IOTIMEDOUT" }; /* CAUTION: fd_debug causes huge amounts of logging output */ int fd_debug = 0; #define TRACE0(arg) if(fd_debug) printf(arg) #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2) #else /* DEBUG */ #define TRACE0(arg) #define TRACE1(arg1, arg2) #endif /* DEBUG */ /* autoconfig structure */ struct isa_driver fdcdriver = { fdprobe, fdattach, "fdc", }; static d_open_t Fdopen; /* NOTE, not fdopen */ static d_close_t fdclose; static d_ioctl_t fdioctl; static d_strategy_t fdstrategy; #define CDEV_MAJOR 9 #define BDEV_MAJOR 2 extern struct cdevsw fd_cdevsw; static struct bdevsw fd_bdevsw = { Fdopen, fdclose, fdstrategy, fdioctl, /*2*/ nxdump, zerosize, 0, "fd", &fd_cdevsw, -1 }; static struct cdevsw fd_cdevsw = { Fdopen, fdclose, rawread, rawwrite, /*9*/ fdioctl, nostop, nullreset, nodevtotty, seltrue, nommap, fdstrategy, "fd", &fd_bdevsw, -1 }; struct isa_device *fdcdevs[NFDC]; /* * Provide hw.devconf information. */ static int fd_externalize(struct kern_devconf *kdc, struct sysctl_req *req) { return disk_externalize(fd_data[kdc->kdc_unit].fdsu, req); } static int fdc_err(fdcu_t fdcu, const char *s) { fdc_data[fdcu].fdc_errs++; if(s) { if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX) printf("fdc%d: %s", fdcu, s); else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX) printf("fdc%d: too many errors, not logging any more\n", fdcu); } return FD_FAILED; } /* * fd_cmd: Send a command to the chip. Takes a varargs with this structure: * Unit number, * # of output bytes, output bytes as ints ..., * # of input bytes, input bytes as ints ... */ int fd_cmd(fdcu_t fdcu, int n_out, ...) { u_char cmd; int n_in; int n; va_list ap; va_start(ap, n_out); cmd = (u_char)(va_arg(ap, int)); va_end(ap); va_start(ap, n_out); for (n = 0; n < n_out; n++) { if (out_fdc(fdcu, va_arg(ap, int)) < 0) { char msg[50]; sprintf(msg, "cmd %x failed at out byte %d of %d\n", cmd, n + 1, n_out); return fdc_err(fdcu, msg); } } n_in = va_arg(ap, int); for (n = 0; n < n_in; n++) { int *ptr = va_arg(ap, int *); if (fd_in(fdcu, ptr) < 0) { char msg[50]; sprintf(msg, "cmd %02x failed at in byte %d of %d\n", cmd, n + 1, n_in); return fdc_err(fdcu, msg); } } return 0; } int fd_sense_drive_status(fdc_p fdc, int *st3p) { int st3; if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) { return fdc_err(fdc->fdcu, "Sense Drive Status failed\n"); } if (st3p) *st3p = st3; return 0; } int fd_sense_int(fdc_p fdc, int *st0p, int *cylp) { int st0, cyl; int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0); if (ret) { (void)fdc_err(fdc->fdcu, "sense intr err reading stat reg 0\n"); return ret; } if (st0p) *st0p = st0; if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) { /* * There doesn't seem to have been an interrupt. */ return FD_NOT_VALID; } if (fd_in(fdc->fdcu, &cyl) < 0) { return fdc_err(fdc->fdcu, "can't get cyl num\n"); } if (cylp) *cylp = cyl; return 0; } int fd_read_status(fdc_p fdc, int fdsu) { int i, ret; for (i = 0; i < 7; i++) { /* * XXX types are poorly chosen. Only bytes can by read * from the hardware, but fdc_status wants u_longs and * fd_in() gives ints. */ int status; ret = fd_in(fdc->fdcu, &status); fdc->status[i] = status; if (ret != 0) break; } if (ret == 0) fdc->flags |= FDC_STAT_VALID; else fdc->flags &= ~FDC_STAT_VALID; return ret; } /****************************************************************************/ /* autoconfiguration stuff */ /****************************************************************************/ /* * probe for existance of controller */ static int fdprobe(struct isa_device *dev) { fdcu_t fdcu = dev->id_unit; if(fdc_data[fdcu].flags & FDC_ATTACHED) { printf("fdc%d: unit used multiple times\n", fdcu); return 0; } fdcdevs[fdcu] = dev; fdc_data[fdcu].baseport = dev->id_iobase; #ifndef DEV_LKM fdc_registerdev(dev); #endif /* First - lets reset the floppy controller */ outb(dev->id_iobase+FDOUT, 0); DELAY(100); outb(dev->id_iobase+FDOUT, FDO_FRST); /* see if it can handle a command */ if (fd_cmd(fdcu, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 0)) { return(0); } kdc_fdc[fdcu].kdc_state = DC_IDLE; return (IO_FDCSIZE); } /* * wire controller into system, look for floppy units */ static int fdattach(struct isa_device *dev) { unsigned fdt; fdu_t fdu; fdcu_t fdcu = dev->id_unit; fdc_p fdc = fdc_data + fdcu; fd_p fd; int fdsu, st0, st3, i, unithasfd; struct isa_device *fdup; int ic_type = 0; #ifdef DEVFS char name[64]; #endif /* DEVFS */ fdc->fdcu = fdcu; fdc->flags |= FDC_ATTACHED; fdc->dmachan = dev->id_drq; fdc->state = DEVIDLE; /* reset controller, turn motor off, clear fdout mirror reg */ outb(fdc->baseport + FDOUT, ((fdc->fdout = 0))); /* check for each floppy drive */ for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) { if (fdup->id_iobase != dev->id_iobase) continue; fdu = fdup->id_unit; fd = &fd_data[fdu]; if (fdu >= (NFD+NFT)) continue; fdsu = fdup->id_physid; /* look up what bios thinks we have */ switch (fdu) { case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0); break; case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0); break; default: fdt = RTCFDT_NONE; break; } /* is there a unit? */ if ((fdt == RTCFDT_NONE) #if NFT > 0 || (fdsu >= DRVS_PER_CTLR)) { #else ) { fd->type = NO_TYPE; #endif #if NFT > 0 /* If BIOS says no floppy, or > 2nd device */ /* Probe for and attach a floppy tape. */ /* Tell FT if there was already a disk */ /* with this unit number found. */ unithasfd = 0; if (fdu < NFD && fd->type != NO_TYPE) unithasfd = 1; if (ftattach(dev, fdup, unithasfd)) continue; if (fdsu < DRVS_PER_CTLR) fd->type = NO_TYPE; #endif continue; } /* select it */ set_motor(fdcu, fdsu, TURNON); DELAY(1000000); /* 1 sec */ if (ic_type == 0 && fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { printf("fdc%d: ", fdcu); ic_type = (u_char)ic_type; switch( ic_type ) { case 0x80: printf("NEC 765\n"); fdc->fdct = FDC_NE765; kdc_fdc[fdcu].kdc_description = "NEC 765 floppy disk/tape controller"; break; case 0x81: printf("Intel 82077\n"); fdc->fdct = FDC_I82077; kdc_fdc[fdcu].kdc_description = "Intel 82077 floppy disk/tape controller"; break; case 0x90: printf("NEC 72065B\n"); fdc->fdct = FDC_NE72065; kdc_fdc[fdcu].kdc_description = "NEC 72065B floppy disk/tape controller"; break; default: printf("unknown IC type %02x\n", ic_type); fdc->fdct = FDC_UNKNOWN; break; } } if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) && (st3 & NE7_ST3_T0)) { /* if at track 0, first seek inwards */ /* seek some steps: */ (void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0); DELAY(300000); /* ...wait a moment... */ (void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */ } /* If we're at track 0 first seek inwards. */ if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) { /* Seek some steps... */ if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { /* ...wait a moment... */ DELAY(300000); /* make ctrlr happy: */ (void)fd_sense_int(fdc, 0, 0); } } for(i = 0; i < 2; i++) { /* * we must recalibrate twice, just in case the * heads have been beyond cylinder 76, since most * FDCs still barf when attempting to recalibrate * more than 77 steps */ /* go back to 0: */ if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) { /* a second being enough for full stroke seek*/ DELAY(i == 0? 1000000: 300000); /* anything responding? */ if (fd_sense_int(fdc, &st0, 0) == 0 && (st0 & NE7_ST0_EC) == 0) break; /* already probed succesfully */ } } set_motor(fdcu, fdsu, TURNOFF); if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */ continue; fd->track = FD_NO_TRACK; fd->fdc = fdc; fd->fdsu = fdsu; fd->options = 0; printf("fd%d: ", fdu); fd_registerdev(fdcu, fdu); switch (fdt) { case RTCFDT_12M: printf("1.2MB 5.25in\n"); fd->type = FD_1200; kdc_fd[fdu].kdc_description = "1.2MB (1200K) 5.25in floppy disk drive"; #ifdef DEVFS - sprintf(name,"fd%d.1200",fdu); + sprintf(name,"rfd%d.1200",fdu); #endif /* DEVFS */ break; case RTCFDT_144M: printf("1.44MB 3.5in\n"); fd->type = FD_1440; kdc_fd[fdu].kdc_description = "1.44MB (1440K) 3.5in floppy disk drive"; #ifdef DEVFS - sprintf(name,"fd%d.1440",fdu); + sprintf(name,"rfd%d.1440",fdu); #endif /* DEVFS */ break; case RTCFDT_288M: case RTCFDT_288M_1: printf("2.88MB 3.5in - 1.44MB mode\n"); fd->type = FD_1440; kdc_fd[fdu].kdc_description = "2.88MB (2880K) 3.5in floppy disk drive in 1.44 mode"; #ifdef DEVFS - sprintf(name,"fd%d.1440",fdu); + sprintf(name,"rfd%d.1440",fdu); #endif /* DEVFS */ break; case RTCFDT_360K: printf("360KB 5.25in\n"); fd->type = FD_360; kdc_fd[fdu].kdc_description = "360KB 5.25in floppy disk drive"; #ifdef DEVFS - sprintf(name,"fd%d.360",fdu); + sprintf(name,"rfd%d.360",fdu); #endif /* DEVFS */ break; case RTCFDT_720K: printf("720KB 3.5in\n"); fd->type = FD_720; kdc_fd[fdu].kdc_description = "720KB 3.5in floppy disk drive"; #ifdef DEVFS - sprintf(name,"fd%d.720",fdu); + sprintf(name,"rfd%d.720",fdu); #endif /* DEVFS */ break; default: printf("unknown\n"); fd->type = NO_TYPE; #ifdef DEVFS - sprintf(name,"fd%d.xxxx",fdu); + sprintf(name,"rfd%d.xxxx",fdu); #endif /* DEVFS */ break; } kdc_fd[fdu].kdc_state = DC_IDLE; #ifdef DEVFS fd->rfd_devfs_token = devfs_add_devsw( "/",name,&fd_cdevsw, fdu * 8, DV_CHR,0,0,0644); fd->fd_devfs_token = devfs_add_devsw( - "/",name, &fd_bdevsw, fdu * 8, + "/",name+1, &fd_bdevsw, fdu * 8, DV_BLK,0,0,0644); #endif /* DEVFS */ if (dk_ndrive < DK_NDRIVE) { sprintf(dk_names[dk_ndrive], "fd%d", fdu); fd->dkunit = dk_ndrive++; /* * XXX assume rate is FDC_500KBPS. */ dk_wpms[dk_ndrive] = 500000 / 8 / 2; } else { fd->dkunit = -1; } } return (1); } /****************************************************************************/ /* motor control stuff */ /* remember to not deselect the drive we're working on */ /****************************************************************************/ static void set_motor(fdcu_t fdcu, int fdsu, int turnon) { int fdout = fdc_data[fdcu].fdout; int needspecify = 0; if(turnon) { fdout &= ~FDO_FDSEL; fdout |= (FDO_MOEN0 << fdsu) + fdsu; } else fdout &= ~(FDO_MOEN0 << fdsu); if(!turnon && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0) /* gonna turn off the last drive, put FDC to bed */ fdout &= ~ (FDO_FRST|FDO_FDMAEN); else { /* make sure controller is selected and specified */ if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0) needspecify = 1; fdout |= (FDO_FRST|FDO_FDMAEN); } outb(fdc_data[fdcu].baseport+FDOUT, fdout); fdc_data[fdcu].fdout = fdout; kdc_fdc[fdcu].kdc_state = (fdout & FDO_FRST)? DC_BUSY: DC_IDLE; TRACE1("[0x%x->FDOUT]", fdout); if(needspecify) { /* * XXX * special case: since we have just woken up the FDC * from its sleep, we silently assume the command will * be accepted, and do not test for a timeout */ (void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 0); } } static void fd_turnoff(void *arg1) { fdu_t fdu = (fdu_t)arg1; int s; fd_p fd = fd_data + fdu; TRACE1("[fd%d: turnoff]", fdu); s = splbio(); fd->flags &= ~FD_MOTOR; set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF); splx(s); } static void fd_motor_on(void *arg1) { fdu_t fdu = (fdu_t)arg1; int s; fd_p fd = fd_data + fdu; s = splbio(); fd->flags &= ~FD_MOTOR_WAIT; if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) { fdintr(fd->fdc->fdcu); } splx(s); } static void fd_turnon(fdu_t fdu) { fd_p fd = fd_data + fdu; if(!(fd->flags & FD_MOTOR)) { fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); set_motor(fd->fdc->fdcu, fd->fdsu, TURNON); timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */ } } static void fdc_reset(fdc_p fdc) { fdcu_t fdcu = fdc->fdcu; /* Try a reset, keep motor on */ outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); DELAY(100); /* enable FDC, but defer interrupts a moment */ outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN); TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); DELAY(100); outb(fdc->baseport + FDOUT, fdc->fdout); TRACE1("[0x%x->FDOUT]", fdc->fdout); /* XXX after a reset, silently believe the FDC will accept commands */ (void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 0); } /****************************************************************************/ /* fdc in/out */ /****************************************************************************/ int in_fdc(fdcu_t fdcu) { int baseport = fdc_data[fdcu].baseport; int i, j = 100000; while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) && j-- > 0) if (i == NE7_RQM) return fdc_err(fdcu, "ready for output in input\n"); if (j <= 0) return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0); #ifdef DEBUG i = inb(baseport+FDDATA); TRACE1("[FDDATA->0x%x]", (unsigned char)i); return(i); #else return inb(baseport+FDDATA); #endif } /* * fd_in: Like in_fdc, but allows you to see if it worked. */ static int fd_in(fdcu_t fdcu, int *ptr) { int baseport = fdc_data[fdcu].baseport; int i, j = 100000; while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) && j-- > 0) if (i == NE7_RQM) return fdc_err(fdcu, "ready for output in input\n"); if (j <= 0) return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0); #ifdef DEBUG i = inb(baseport+FDDATA); TRACE1("[FDDATA->0x%x]", (unsigned char)i); *ptr = i; return 0; #else i = inb(baseport+FDDATA); if (ptr) *ptr = i; return 0; #endif } int out_fdc(fdcu_t fdcu, int x) { int baseport = fdc_data[fdcu].baseport; int i; /* Check that the direction bit is set */ i = 100000; while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0); if (i <= 0) return fdc_err(fdcu, "direction bit not set\n"); /* Check that the floppy controller is ready for a command */ i = 100000; while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0); if (i <= 0) return fdc_err(fdcu, bootverbose? "output ready timeout\n": 0); /* Send the command and return */ outb(baseport+FDDATA, x); TRACE1("[0x%x->FDDATA]", x); return (0); } /****************************************************************************/ /* fdopen/fdclose */ /****************************************************************************/ int Fdopen(dev_t dev, int flags, int mode, struct proc *p) { fdu_t fdu = FDUNIT(minor(dev)); int type = FDTYPE(minor(dev)); fdc_p fdc; #if NFT > 0 /* check for a tape open */ if (type & F_TAPE_TYPE) return(ftopen(dev, flags)); #endif /* check bounds */ if (fdu >= NFD) return(ENXIO); fdc = fd_data[fdu].fdc; if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE)) return(ENXIO); if (type > NUMDENS) return(ENXIO); if (type == 0) type = fd_data[fdu].type; else { if (type != fd_data[fdu].type) { switch (fd_data[fdu].type) { case FD_360: return(ENXIO); case FD_720: if ( type != FD_820 && type != FD_800 ) return(ENXIO); break; case FD_1200: switch (type) { case FD_1480: type = FD_1480in5_25; break; case FD_1440: type = FD_1440in5_25; break; case FD_820: type = FD_820in5_25; break; case FD_800: type = FD_800in5_25; break; case FD_720: type = FD_720in5_25; break; case FD_360: type = FD_360in5_25; break; default: return(ENXIO); } break; case FD_1440: if ( type != FD_1720 && type != FD_1480 && type != FD_1200 && type != FD_820 && type != FD_800 && type != FD_720 ) return(ENXIO); break; } } } fd_data[fdu].ft = fd_types + type - 1; fd_data[fdu].flags |= FD_OPEN; kdc_fd[fdu].kdc_state = DC_BUSY; return 0; } int fdclose(dev_t dev, int flags, int mode, struct proc *p) { fdu_t fdu = FDUNIT(minor(dev)); #if NFT > 0 int type = FDTYPE(minor(dev)); if (type & F_TAPE_TYPE) return ftclose(dev, flags); #endif fd_data[fdu].flags &= ~FD_OPEN; fd_data[fdu].options &= ~FDOPT_NORETRY; kdc_fd[fdu].kdc_state = DC_IDLE; return(0); } /****************************************************************************/ /* fdstrategy */ /****************************************************************************/ void fdstrategy(struct buf *bp) { register struct buf *dp; long nblocks, blknum; int s; fdcu_t fdcu; fdu_t fdu; fdc_p fdc; fd_p fd; size_t fdblk; fdu = FDUNIT(minor(bp->b_dev)); fd = &fd_data[fdu]; fdc = fd->fdc; fdcu = fdc->fdcu; #if NFT > 0 if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) { /* ft tapes do not (yet) support strategy i/o */ bp->b_error = ENODEV; bp->b_flags |= B_ERROR; goto bad; } /* check for controller already busy with tape */ if (fdc->flags & FDC_TAPE_BUSY) { bp->b_error = EBUSY; bp->b_flags |= B_ERROR; goto bad; } #endif fdblk = 128 << (fd->ft->secsize); if (!(bp->b_flags & B_FORMAT)) { if ((fdu >= NFD) || (bp->b_blkno < 0)) { printf( "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n", fdu, (u_long)bp->b_blkno, bp->b_bcount); bp->b_error = EINVAL; bp->b_flags |= B_ERROR; goto bad; } if ((bp->b_bcount % fdblk) != 0) { bp->b_error = EINVAL; bp->b_flags |= B_ERROR; goto bad; } } /* * Set up block calculations. */ blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk; nblocks = fd->ft->size; if (blknum + (bp->b_bcount / fdblk) > nblocks) { if (blknum == nblocks) { bp->b_resid = bp->b_bcount; } else { bp->b_error = ENOSPC; bp->b_flags |= B_ERROR; } goto bad; } bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads); bp->b_pblkno = bp->b_blkno; dp = &(fdc->head); s = splbio(); disksort(dp, bp); untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */ fdstart(fdcu); splx(s); return; bad: biodone(bp); } /***************************************************************\ * fdstart * * We have just queued something.. if the controller is not busy * * then simulate the case where it has just finished a command * * So that it (the interrupt routine) looks on the queue for more* * work to do and picks up what we just added. * * If the controller is already busy, we need do nothing, as it * * will pick up our work when the present work completes * \***************************************************************/ static void fdstart(fdcu_t fdcu) { int s; s = splbio(); if(fdc_data[fdcu].state == DEVIDLE) { fdintr(fdcu); } splx(s); } static void fd_timeout(void *arg1) { fdcu_t fdcu = (fdcu_t)arg1; fdu_t fdu = fdc_data[fdcu].fdu; int baseport = fdc_data[fdcu].baseport; struct buf *dp, *bp; int s; dp = &fdc_data[fdcu].head; bp = dp->b_actf; /* * Due to IBM's brain-dead design, the FDC has a faked ready * signal, hardwired to ready == true. Thus, any command * issued if there's no diskette in the drive will _never_ * complete, and must be aborted by resetting the FDC. * Many thanks, Big Blue! */ s = splbio(); TRACE1("fd%d[fd_timeout()]", fdu); /* See if the controller is still busy (patiently awaiting data) */ if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB) { TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS)); /* yup, it is; kill it now */ fdc_reset(&fdc_data[fdcu]); printf("fd%d: Operation timeout\n", fdu); } if (bp) { retrier(fdcu); fdc_data[fdcu].status[0] = NE7_ST0_IC_RC; fdc_data[fdcu].state = IOTIMEDOUT; if( fdc_data[fdcu].retry < 6) fdc_data[fdcu].retry = 6; } else { fdc_data[fdcu].fd = (fd_p) 0; fdc_data[fdcu].fdu = -1; fdc_data[fdcu].state = DEVIDLE; } fdintr(fdcu); splx(s); } /* just ensure it has the right spl */ static void fd_pseudointr(void *arg1) { fdcu_t fdcu = (fdcu_t)arg1; int s; s = splbio(); fdintr(fdcu); splx(s); } /***********************************************************************\ * fdintr * * keep calling the state machine until it returns a 0 * * ALWAYS called at SPLBIO * \***********************************************************************/ void fdintr(fdcu_t fdcu) { fdc_p fdc = fdc_data + fdcu; #if NFT > 0 fdu_t fdu = fdc->fdu; if (fdc->flags & FDC_TAPE_BUSY) (ftintr(fdu)); else #endif while(fdstate(fdcu, fdc)) ; } /***********************************************************************\ * The controller state machine. * * if it returns a non zero value, it should be called again immediatly * \***********************************************************************/ static int fdstate(fdcu_t fdcu, fdc_p fdc) { int read, format, head, sec = 0, sectrac, st0, cyl, st3; unsigned long blknum; fdu_t fdu = fdc->fdu; fd_p fd; register struct buf *dp, *bp; struct fd_formb *finfo = NULL; size_t fdblk; dp = &(fdc->head); bp = dp->b_actf; if(!bp) { /***********************************************\ * nothing left for this controller to do * * Force into the IDLE state, * \***********************************************/ fdc->state = DEVIDLE; if(fdc->fd) { printf("fd%d: unexpected valid fd pointer\n", fdc->fdu); fdc->fd = (fd_p) 0; fdc->fdu = -1; } TRACE1("[fdc%d IDLE]", fdcu); return(0); } fdu = FDUNIT(minor(bp->b_dev)); fd = fd_data + fdu; fdblk = 128 << fd->ft->secsize; if (fdc->fd && (fd != fdc->fd)) { printf("fd%d: confused fd pointers\n", fdu); } read = bp->b_flags & B_READ; format = bp->b_flags & B_FORMAT; if(format) finfo = (struct fd_formb *)bp->b_un.b_addr; TRACE1("fd%d", fdu); TRACE1("[%s]", fdstates[fdc->state]); TRACE1("(0x%x)", fd->flags); untimeout(fd_turnoff, (caddr_t)fdu); timeout(fd_turnoff, (caddr_t)fdu, 4 * hz); switch (fdc->state) { case DEVIDLE: case FINDWORK: /* we have found new work */ fdc->retry = 0; fd->skip = 0; fdc->fd = fd; fdc->fdu = fdu; outb(fdc->baseport+FDCTL, fd->ft->trans); TRACE1("[0x%x->FDCTL]", fd->ft->trans); /*******************************************************\ * If the next drive has a motor startup pending, then * * it will start up in it's own good time * \*******************************************************/ if(fd->flags & FD_MOTOR_WAIT) { fdc->state = MOTORWAIT; return(0); /* come back later */ } /*******************************************************\ * Maybe if it's not starting, it SHOULD be starting * \*******************************************************/ if (!(fd->flags & FD_MOTOR)) { fdc->state = MOTORWAIT; fd_turnon(fdu); return(0); } else /* at least make sure we are selected */ { set_motor(fdcu, fd->fdsu, TURNON); } fdc->state = DOSEEK; break; case DOSEEK: if (bp->b_cylin == fd->track) { fdc->state = SEEKCOMPLETE; break; } if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fd->fdsu, bp->b_cylin * fd->ft->steptrac, 0)) { /* * seek command not accepted, looks like * the FDC went off to the Saints... */ fdc->retry = 6; /* try a reset */ return(retrier(fdcu)); } fd->track = FD_NO_TRACK; fdc->state = SEEKWAIT; return(0); /* will return later */ case SEEKWAIT: /* allow heads to settle */ timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16); fdc->state = SEEKCOMPLETE; return(0); /* will return later */ case SEEKCOMPLETE : /* SEEK DONE, START DMA */ /* Make sure seek really happened*/ if(fd->track == FD_NO_TRACK) { int descyl = bp->b_cylin * fd->ft->steptrac; do { /* * This might be a "ready changed" interrupt, * which cannot really happen since the * RDY pin is hardwired to + 5 volts. This * generally indicates a "bouncing" intr * line, so do one of the following: * * When running on an enhanced FDC that is * known to not go stuck after responding * with INVALID, fetch all interrupt states * until seeing either an INVALID or a * real interrupt condition. * * When running on a dumb old NE765, give * up immediately. The controller will * provide up to four dummy RC interrupt * conditions right after reset (for the * corresponding four drives), so this is * our only chance to get notice that it * was not the FDC that caused the interrupt. */ if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) return 0; if(fdc->fdct == FDC_NE765 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) return 0; /* hope for a real intr */ } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); if (0 == descyl) { int failed = 0; /* * seek to cyl 0 requested; make sure we are * really there */ if (fd_sense_drive_status(fdc, &st3)) failed = 1; if ((st3 & NE7_ST3_T0) == 0) { printf( "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", fdu, st3, NE7_ST3BITS); failed = 1; } if (failed) { if(fdc->retry < 3) fdc->retry = 3; return(retrier(fdcu)); } } if (cyl != descyl) { printf( "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", fdu, descyl, cyl, st0); return(retrier(fdcu)); } } fd->track = bp->b_cylin; if(format) fd->skip = (char *)&(finfo->fd_formb_cylno(0)) - (char *)finfo; isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip, format ? bp->b_bcount : fdblk, fdc->dmachan); blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk + fd->skip/fdblk; sectrac = fd->ft->sectrac; sec = blknum % (sectrac * fd->ft->heads); head = sec / sectrac; sec = sec % sectrac + 1; fd->hddrv = ((head&1)<<2)+fdu; if(format || !read) { /* make sure the drive is writable */ if(fd_sense_drive_status(fdc, &st3) != 0) { /* stuck controller? */ fdc->retry = 6; /* reset the beast */ return(retrier(fdcu)); } if(st3 & NE7_ST3_WP) { /* * XXX YES! this is ugly. * in order to force the current operation * to fail, we will have to fake an FDC * error - all error handling is done * by the retrier() */ fdc->status[0] = NE7_ST0_IC_AT; fdc->status[1] = NE7_ST1_NW; fdc->status[2] = 0; fdc->status[3] = fd->track; fdc->status[4] = head; fdc->status[5] = sec; fdc->retry = 8; /* break out immediately */ fdc->state = IOTIMEDOUT; /* not really... */ return (1); } } if(format) { /* formatting */ if(fd_cmd(fdcu, 6, NE7CMD_FORMAT, head << 2 | fdu, finfo->fd_formb_secshift, finfo->fd_formb_nsecs, finfo->fd_formb_gaplen, finfo->fd_formb_fillbyte, 0)) { /* controller fell over */ fdc->retry = 6; return(retrier(fdcu)); } } else { if (fd_cmd(fdcu, 9, (read ? NE7CMD_READ : NE7CMD_WRITE), head << 2 | fdu, /* head & unit */ fd->track, /* track */ head, sec, /* sector + 1 */ fd->ft->secsize, /* sector size */ sectrac, /* sectors/track */ fd->ft->gap, /* gap size */ fd->ft->datalen, /* data length */ 0)) { /* the beast is sleeping again */ fdc->retry = 6; return(retrier(fdcu)); } } fdc->state = IOCOMPLETE; timeout(fd_timeout, (caddr_t)fdcu, hz); return(0); /* will return later */ case IOCOMPLETE: /* IO DONE, post-analyze */ untimeout(fd_timeout, (caddr_t)fdcu); if (fd_read_status(fdc, fd->fdsu)) { if (fdc->retry < 6) fdc->retry = 6; /* force a reset */ return retrier(fdcu); } fdc->state = IOTIMEDOUT; /* FALLTHROUGH */ case IOTIMEDOUT: isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip, format ? bp->b_bcount : fdblk, fdc->dmachan); if (fdc->status[0] & NE7_ST0_IC) { if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) { /* * DMA overrun. Someone hogged the bus * and didn't release it in time for the * next FDC transfer. * Just restart it, don't increment retry * count. (vak) */ fdc->state = SEEKCOMPLETE; return (1); } else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV && fdc->retry < 6) fdc->retry = 6; /* force a reset */ else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC && fdc->retry < 3) fdc->retry = 3; /* force recalibrate */ return(retrier(fdcu)); } /* All OK */ fd->skip += fdblk; if (!format && fd->skip < bp->b_bcount) { /* set up next transfer */ blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk + fd->skip/fdblk; bp->b_cylin = (blknum / (fd->ft->sectrac * fd->ft->heads)); fdc->state = DOSEEK; } else { /* ALL DONE */ fd->skip = 0; bp->b_resid = 0; dp->b_actf = bp->b_actf; biodone(bp); fdc->fd = (fd_p) 0; fdc->fdu = -1; fdc->state = FINDWORK; } return(1); case RESETCTLR: fdc_reset(fdc); fdc->retry++; fdc->state = STARTRECAL; break; case STARTRECAL: if(fd_cmd(fdcu, 2, NE7CMD_RECAL, fdu, 0)) /* Recalibrate Function */ { /* arrgl */ fdc->retry = 6; return(retrier(fdcu)); } fdc->state = RECALWAIT; return(0); /* will return later */ case RECALWAIT: /* allow heads to settle */ timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8); fdc->state = RECALCOMPLETE; return(0); /* will return later */ case RECALCOMPLETE: do { /* * See SEEKCOMPLETE for a comment on this: */ if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) return 0; if(fdc->fdct == FDC_NE765 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) return 0; /* hope for a real intr */ } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) { if(fdc->retry > 3) /* * a recalibrate from beyond cylinder 77 * will "fail" due to the FDC limitations; * since people used to complain much about * the failure message, try not logging * this one if it seems to be the first * time in a line */ printf("fd%d: recal failed ST0 %b cyl %d\n", fdu, st0, NE7_ST0BITS, cyl); if(fdc->retry < 3) fdc->retry = 3; return(retrier(fdcu)); } fd->track = 0; /* Seek (probably) necessary */ fdc->state = DOSEEK; return(1); /* will return immediatly */ case MOTORWAIT: if(fd->flags & FD_MOTOR_WAIT) { return(0); /* time's not up yet */ } /* * since the controller was off, it has lost its * idea about the current track it were; thus, * recalibrate the bastard */ fdc->state = STARTRECAL; return(1); /* will return immediatly */ default: printf("fdc%d: Unexpected FD int->", fdcu); if (fd_read_status(fdc, fd->fdsu) == 0) printf("FDC status :%lx %lx %lx %lx %lx %lx %lx ", fdc->status[0], fdc->status[1], fdc->status[2], fdc->status[3], fdc->status[4], fdc->status[5], fdc->status[6] ); else printf("No status available "); if (fd_sense_int(fdc, &st0, &cyl) != 0) { printf("[controller is dead now]\n"); return(0); } printf("ST0 = %x, PCN = %x\n", st0, cyl); return(0); } /*XXX confusing: some branches return immediately, others end up here*/ return(1); /* Come back immediatly to new state */ } static int retrier(fdcu) fdcu_t fdcu; { fdc_p fdc = fdc_data + fdcu; register struct buf *dp, *bp; dp = &(fdc->head); bp = dp->b_actf; if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY) goto fail; switch(fdc->retry) { case 0: case 1: case 2: fdc->state = SEEKCOMPLETE; break; case 3: case 4: case 5: fdc->state = STARTRECAL; break; case 6: fdc->state = RESETCTLR; break; case 7: break; default: fail: { dev_t sav_b_dev = bp->b_dev; /* Trick diskerr */ bp->b_dev = makedev(major(bp->b_dev), (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART); diskerr(bp, "fd", "hard error", LOG_PRINTF, fdc->fd->skip / DEV_BSIZE, (struct disklabel *)NULL); bp->b_dev = sav_b_dev; if (fdc->flags & FDC_STAT_VALID) { printf( " (ST0 %b ST1 %b ST2 %b cyl %ld hd %ld sec %ld)\n", fdc->status[0], NE7_ST0BITS, fdc->status[1], NE7_ST1BITS, fdc->status[2], NE7_ST2BITS, fdc->status[3], fdc->status[4], fdc->status[5]); } else printf(" (No status)\n"); } bp->b_flags |= B_ERROR; bp->b_error = EIO; bp->b_resid = bp->b_bcount - fdc->fd->skip; dp->b_actf = bp->b_actf; fdc->fd->skip = 0; biodone(bp); fdc->state = FINDWORK; fdc->fd = (fd_p) 0; fdc->fdu = -1; /* XXX abort current command, if any. */ return(1); } fdc->retry++; return(1); } static int fdformat(dev, finfo, p) dev_t dev; struct fd_formb *finfo; struct proc *p; { fdu_t fdu; fd_p fd; struct buf *bp; int rv = 0, s; size_t fdblk; fdu = FDUNIT(minor(dev)); fd = &fd_data[fdu]; fdblk = 128 << fd->ft->secsize; /* set up a buffer header for fdstrategy() */ bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT); if(bp == 0) return ENOBUFS; /* * keep the process from being swapped */ p->p_flag |= P_PHYSIO; bzero((void *)bp, sizeof(struct buf)); bp->b_flags = B_BUSY | B_PHYS | B_FORMAT; bp->b_proc = p; bp->b_dev = dev; /* * calculate a fake blkno, so fdstrategy() would initiate a * seek to the requested cylinder */ bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE; bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs; bp->b_un.b_addr = (caddr_t)finfo; /* now do the format */ fdstrategy(bp); /* ...and wait for it to complete */ s = splbio(); while(!(bp->b_flags & B_DONE)) { rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz); if(rv == EWOULDBLOCK) break; } splx(s); if(rv == EWOULDBLOCK) { /* timed out */ rv = EIO; biodone(bp); } if(bp->b_flags & B_ERROR) rv = bp->b_error; /* * allow the process to be swapped */ p->p_flag &= ~P_PHYSIO; free(bp, M_TEMP); return rv; } /* * TODO: don't allocate buffer on stack. */ int fdioctl(dev, cmd, addr, flag, p) dev_t dev; int cmd; caddr_t addr; int flag; struct proc *p; { fdu_t fdu = FDUNIT(minor(dev)); fd_p fd = &fd_data[fdu]; size_t fdblk; struct fd_type *fdt; struct disklabel *dl; char buffer[DEV_BSIZE]; int error = 0; #if NFT > 0 int type = FDTYPE(minor(dev)); /* check for a tape ioctl */ if (type & F_TAPE_TYPE) return ftioctl(dev, cmd, addr, flag, p); #endif fdblk = 128 << fd->ft->secsize; switch (cmd) { case DIOCGDINFO: bzero(buffer, sizeof (buffer)); dl = (struct disklabel *)buffer; dl->d_secsize = fdblk; fdt = fd_data[FDUNIT(minor(dev))].ft; dl->d_secpercyl = fdt->size / fdt->tracks; dl->d_type = DTYPE_FLOPPY; if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl) == NULL) error = 0; else error = EINVAL; *(struct disklabel *)addr = *dl; break; case DIOCSDINFO: if ((flag & FWRITE) == 0) error = EBADF; break; case DIOCWLABEL: if ((flag & FWRITE) == 0) error = EBADF; break; case DIOCWDINFO: if ((flag & FWRITE) == 0) { error = EBADF; break; } dl = (struct disklabel *)addr; if ((error = setdisklabel((struct disklabel *)buffer, dl, (u_long)0)) != 0) break; error = writedisklabel(dev, fdstrategy, (struct disklabel *)buffer); break; case FD_FORM: if((flag & FWRITE) == 0) error = EBADF; /* must be opened for writing */ else if(((struct fd_formb *)addr)->format_version != FD_FORMAT_VERSION) error = EINVAL; /* wrong version of formatting prog */ else error = fdformat(dev, (struct fd_formb *)addr, p); break; case FD_GTYPE: /* get drive type */ *(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft; break; case FD_STYPE: /* set drive type */ /* this is considered harmful; only allow for superuser */ if(suser(p->p_ucred, &p->p_acflag) != 0) return EPERM; *fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr; break; case FD_GOPTS: /* get drive options */ *(int *)addr = fd_data[FDUNIT(minor(dev))].options; break; case FD_SOPTS: /* set drive options */ fd_data[FDUNIT(minor(dev))].options = *(int *)addr; break; default: error = ENOTTY; break; } return (error); } static fd_devsw_installed = 0; static void fd_drvinit(void *notused ) { dev_t dev; if( ! fd_devsw_installed ) { dev = makedev(CDEV_MAJOR, 0); cdevsw_add(&dev,&fd_cdevsw, NULL); dev = makedev(BDEV_MAJOR, 0); bdevsw_add(&dev,&fd_bdevsw, NULL); fd_devsw_installed = 1; } } SYSINIT(fddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,fd_drvinit,NULL) #endif /* * Hello emacs, these are the * Local Variables: * c-indent-level: 8 * c-continued-statement-offset: 8 * c-continued-brace-offset: 0 * c-brace-offset: -8 * c-brace-imaginary-offset: 0 * c-argdecl-indent: 8 * c-label-offset: -8 * c++-hanging-braces: 1 * c++-access-specifier-offset: -8 * c++-empty-arglist-indent: 8 * c++-friend-offset: 0 * End: */ Index: head/sys/i386/isa/fd.c =================================================================== --- head/sys/i386/isa/fd.c (revision 12690) +++ head/sys/i386/isa/fd.c (revision 12691) @@ -1,1945 +1,1945 @@ /* * Copyright (c) 1990 The Regents of the University of California. * All rights reserved. * * This code is derived from software contributed to Berkeley by * Don Ahn. * * Copyright (c) 1993, 1994 by * jc@irbs.UUCP (John Capo) * vak@zebub.msk.su (Serge Vakulenko) * ache@astral.msk.su (Andrew A. Chernov) * * Copyright (c) 1993, 1994, 1995 by * joerg_wunsch@uriah.sax.de (Joerg Wunsch) * dufault@hda.com (Peter Dufault) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * from: @(#)fd.c 7.4 (Berkeley) 5/25/91 - * $Id: fd.c,v 1.73 1995/12/08 11:14:10 julian Exp $ + * $Id: fd.c,v 1.74 1995/12/08 23:20:26 phk Exp $ * */ #include "ft.h" #if NFT < 1 #undef NFDC #endif #include "fd.h" #if NFDC > 0 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if NFT > 0 #include #include #endif #ifdef DEVFS #include #endif static int fd_goaway(struct kern_devconf *, int); static int fdc_goaway(struct kern_devconf *, int); static int fd_externalize(struct kern_devconf *, struct sysctl_req *); /* * Templates for the kern_devconf structures used when we attach. */ static struct kern_devconf kdc_fd[NFD] = { { 0, 0, 0, /* filled in by kern_devconf.c */ "fd", 0, { MDDT_DISK, 0 }, fd_externalize, 0, fd_goaway, DISK_EXTERNALLEN, 0, /* parent */ 0, /* parentdata */ DC_UNCONFIGURED, /* state */ "floppy disk", DC_CLS_DISK /* class */ } }; struct kern_devconf kdc_fdc[NFDC] = { { 0, 0, 0, /* filled in by kern_devconf.c */ "fdc", 0, { MDDT_ISA, 0, "bio" }, isa_generic_externalize, 0, fdc_goaway, ISA_EXTERNALLEN, 0, /* parent */ 0, /* parentdata */ DC_UNCONFIGURED, /* state */ "floppy disk/tape controller", DC_CLS_MISC /* class */ } }; static inline void fd_registerdev(int ctlr, int unit) { if(unit != 0) kdc_fd[unit] = kdc_fd[0]; kdc_fd[unit].kdc_unit = unit; kdc_fd[unit].kdc_parent = &kdc_fdc[ctlr]; kdc_fd[unit].kdc_parentdata = 0; dev_attach(&kdc_fd[unit]); } static inline void fdc_registerdev(struct isa_device *dvp) { int unit = dvp->id_unit; if(unit != 0) kdc_fdc[unit] = kdc_fdc[0]; kdc_fdc[unit].kdc_unit = unit; kdc_fdc[unit].kdc_parent = &kdc_isa0; kdc_fdc[unit].kdc_parentdata = dvp; dev_attach(&kdc_fdc[unit]); } static int fdc_goaway(struct kern_devconf *kdc, int force) { if(force) { dev_detach(kdc); return 0; } else { return EBUSY; /* XXX fix */ } } static int fd_goaway(struct kern_devconf *kdc, int force) { dev_detach(kdc); return 0; } #define b_cylin b_resid /* XXX now spelled b_cylinder elsewhere */ /* misuse a flag to identify format operation */ #define B_FORMAT B_XXX /* * this biotab field doubles as a field for the physical unit number * on the controller */ #define id_physid id_scsiid /* error returns for fd_cmd() */ #define FD_FAILED -1 #define FD_NOT_VALID -2 #define FDC_ERRMAX 100 /* do not log more */ #define NUMTYPES 14 #define NUMDENS (NUMTYPES - 6) /* These defines (-1) must match index for fd_types */ #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */ #define NO_TYPE 0 /* must match NO_TYPE in ft.c */ #define FD_1720 1 #define FD_1480 2 #define FD_1440 3 #define FD_1200 4 #define FD_820 5 #define FD_800 6 #define FD_720 7 #define FD_360 8 #define FD_1480in5_25 9 #define FD_1440in5_25 10 #define FD_820in5_25 11 #define FD_800in5_25 12 #define FD_720in5_25 13 #define FD_360in5_25 14 struct fd_type fd_types[NUMTYPES] = { { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */ { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */ { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */ { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */ { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */ { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */ { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */ { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */ { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */ { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */ { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */ { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */ { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */ { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */ }; #define DRVS_PER_CTLR 2 /* 2 floppies */ /***********************************************************************\ * Per controller structure. * \***********************************************************************/ struct fdc_data fdc_data[NFDC]; /***********************************************************************\ * Per drive structure. * * N per controller (DRVS_PER_CTLR) * \***********************************************************************/ struct fd_data { struct fdc_data *fdc; /* pointer to controller structure */ int fdsu; /* this units number on this controller */ int type; /* Drive type (FD_1440...) */ struct fd_type *ft; /* pointer to the type descriptor */ int flags; #define FD_OPEN 0x01 /* it's open */ #define FD_ACTIVE 0x02 /* it's active */ #define FD_MOTOR 0x04 /* motor should be on */ #define FD_MOTOR_WAIT 0x08 /* motor coming up */ int skip; int hddrv; #define FD_NO_TRACK -2 int track; /* where we think the head is */ int options; /* user configurable options, see ioctl_fd.h */ int dkunit; /* disk stats unit number */ #ifdef DEVFS void *rfd_devfs_token; void *fd_devfs_token; #endif } fd_data[NFD]; /***********************************************************************\ * Throughout this file the following conventions will be used: * * fd is a pointer to the fd_data struct for the drive in question * * fdc is a pointer to the fdc_data struct for the controller * * fdu is the floppy drive unit number * * fdcu is the floppy controller unit number * * fdsu is the floppy drive unit number on that controller. (sub-unit) * \***********************************************************************/ #if NFT > 0 int ftopen(dev_t, int); int ftintr(ftu_t ftu); int ftclose(dev_t, int); void ftstrategy(struct buf *); int ftioctl(dev_t, int, caddr_t, int, struct proc *); int ftdump(dev_t); int ftsize(dev_t); int ftattach(struct isa_device *, struct isa_device *, int); #endif /* autoconfig functions */ static int fdprobe(struct isa_device *); static int fdattach(struct isa_device *); /* needed for ft driver, thus exported */ int in_fdc(fdcu_t); int out_fdc(fdcu_t, int); /* internal functions */ static void set_motor(fdcu_t, int, int); # define TURNON 1 # define TURNOFF 0 static timeout_t fd_turnoff; static timeout_t fd_motor_on; static void fd_turnon(fdu_t); static void fdc_reset(fdc_p); static int fd_in(fdcu_t, int *); static void fdstart(fdcu_t); static timeout_t fd_timeout; static timeout_t fd_pseudointr; static int fdstate(fdcu_t, fdc_p); static int retrier(fdcu_t); static int fdformat(dev_t, struct fd_formb *, struct proc *); #define DEVIDLE 0 #define FINDWORK 1 #define DOSEEK 2 #define SEEKCOMPLETE 3 #define IOCOMPLETE 4 #define RECALCOMPLETE 5 #define STARTRECAL 6 #define RESETCTLR 7 #define SEEKWAIT 8 #define RECALWAIT 9 #define MOTORWAIT 10 #define IOTIMEDOUT 11 #ifdef DEBUG char *fdstates[] = { "DEVIDLE", "FINDWORK", "DOSEEK", "SEEKCOMPLETE", "IOCOMPLETE", "RECALCOMPLETE", "STARTRECAL", "RESETCTLR", "SEEKWAIT", "RECALWAIT", "MOTORWAIT", "IOTIMEDOUT" }; /* CAUTION: fd_debug causes huge amounts of logging output */ int fd_debug = 0; #define TRACE0(arg) if(fd_debug) printf(arg) #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2) #else /* DEBUG */ #define TRACE0(arg) #define TRACE1(arg1, arg2) #endif /* DEBUG */ /* autoconfig structure */ struct isa_driver fdcdriver = { fdprobe, fdattach, "fdc", }; static d_open_t Fdopen; /* NOTE, not fdopen */ static d_close_t fdclose; static d_ioctl_t fdioctl; static d_strategy_t fdstrategy; #define CDEV_MAJOR 9 #define BDEV_MAJOR 2 extern struct cdevsw fd_cdevsw; static struct bdevsw fd_bdevsw = { Fdopen, fdclose, fdstrategy, fdioctl, /*2*/ nxdump, zerosize, 0, "fd", &fd_cdevsw, -1 }; static struct cdevsw fd_cdevsw = { Fdopen, fdclose, rawread, rawwrite, /*9*/ fdioctl, nostop, nullreset, nodevtotty, seltrue, nommap, fdstrategy, "fd", &fd_bdevsw, -1 }; struct isa_device *fdcdevs[NFDC]; /* * Provide hw.devconf information. */ static int fd_externalize(struct kern_devconf *kdc, struct sysctl_req *req) { return disk_externalize(fd_data[kdc->kdc_unit].fdsu, req); } static int fdc_err(fdcu_t fdcu, const char *s) { fdc_data[fdcu].fdc_errs++; if(s) { if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX) printf("fdc%d: %s", fdcu, s); else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX) printf("fdc%d: too many errors, not logging any more\n", fdcu); } return FD_FAILED; } /* * fd_cmd: Send a command to the chip. Takes a varargs with this structure: * Unit number, * # of output bytes, output bytes as ints ..., * # of input bytes, input bytes as ints ... */ int fd_cmd(fdcu_t fdcu, int n_out, ...) { u_char cmd; int n_in; int n; va_list ap; va_start(ap, n_out); cmd = (u_char)(va_arg(ap, int)); va_end(ap); va_start(ap, n_out); for (n = 0; n < n_out; n++) { if (out_fdc(fdcu, va_arg(ap, int)) < 0) { char msg[50]; sprintf(msg, "cmd %x failed at out byte %d of %d\n", cmd, n + 1, n_out); return fdc_err(fdcu, msg); } } n_in = va_arg(ap, int); for (n = 0; n < n_in; n++) { int *ptr = va_arg(ap, int *); if (fd_in(fdcu, ptr) < 0) { char msg[50]; sprintf(msg, "cmd %02x failed at in byte %d of %d\n", cmd, n + 1, n_in); return fdc_err(fdcu, msg); } } return 0; } int fd_sense_drive_status(fdc_p fdc, int *st3p) { int st3; if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) { return fdc_err(fdc->fdcu, "Sense Drive Status failed\n"); } if (st3p) *st3p = st3; return 0; } int fd_sense_int(fdc_p fdc, int *st0p, int *cylp) { int st0, cyl; int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0); if (ret) { (void)fdc_err(fdc->fdcu, "sense intr err reading stat reg 0\n"); return ret; } if (st0p) *st0p = st0; if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) { /* * There doesn't seem to have been an interrupt. */ return FD_NOT_VALID; } if (fd_in(fdc->fdcu, &cyl) < 0) { return fdc_err(fdc->fdcu, "can't get cyl num\n"); } if (cylp) *cylp = cyl; return 0; } int fd_read_status(fdc_p fdc, int fdsu) { int i, ret; for (i = 0; i < 7; i++) { /* * XXX types are poorly chosen. Only bytes can by read * from the hardware, but fdc_status wants u_longs and * fd_in() gives ints. */ int status; ret = fd_in(fdc->fdcu, &status); fdc->status[i] = status; if (ret != 0) break; } if (ret == 0) fdc->flags |= FDC_STAT_VALID; else fdc->flags &= ~FDC_STAT_VALID; return ret; } /****************************************************************************/ /* autoconfiguration stuff */ /****************************************************************************/ /* * probe for existance of controller */ static int fdprobe(struct isa_device *dev) { fdcu_t fdcu = dev->id_unit; if(fdc_data[fdcu].flags & FDC_ATTACHED) { printf("fdc%d: unit used multiple times\n", fdcu); return 0; } fdcdevs[fdcu] = dev; fdc_data[fdcu].baseport = dev->id_iobase; #ifndef DEV_LKM fdc_registerdev(dev); #endif /* First - lets reset the floppy controller */ outb(dev->id_iobase+FDOUT, 0); DELAY(100); outb(dev->id_iobase+FDOUT, FDO_FRST); /* see if it can handle a command */ if (fd_cmd(fdcu, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 0)) { return(0); } kdc_fdc[fdcu].kdc_state = DC_IDLE; return (IO_FDCSIZE); } /* * wire controller into system, look for floppy units */ static int fdattach(struct isa_device *dev) { unsigned fdt; fdu_t fdu; fdcu_t fdcu = dev->id_unit; fdc_p fdc = fdc_data + fdcu; fd_p fd; int fdsu, st0, st3, i, unithasfd; struct isa_device *fdup; int ic_type = 0; #ifdef DEVFS char name[64]; #endif /* DEVFS */ fdc->fdcu = fdcu; fdc->flags |= FDC_ATTACHED; fdc->dmachan = dev->id_drq; fdc->state = DEVIDLE; /* reset controller, turn motor off, clear fdout mirror reg */ outb(fdc->baseport + FDOUT, ((fdc->fdout = 0))); /* check for each floppy drive */ for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) { if (fdup->id_iobase != dev->id_iobase) continue; fdu = fdup->id_unit; fd = &fd_data[fdu]; if (fdu >= (NFD+NFT)) continue; fdsu = fdup->id_physid; /* look up what bios thinks we have */ switch (fdu) { case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0); break; case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0); break; default: fdt = RTCFDT_NONE; break; } /* is there a unit? */ if ((fdt == RTCFDT_NONE) #if NFT > 0 || (fdsu >= DRVS_PER_CTLR)) { #else ) { fd->type = NO_TYPE; #endif #if NFT > 0 /* If BIOS says no floppy, or > 2nd device */ /* Probe for and attach a floppy tape. */ /* Tell FT if there was already a disk */ /* with this unit number found. */ unithasfd = 0; if (fdu < NFD && fd->type != NO_TYPE) unithasfd = 1; if (ftattach(dev, fdup, unithasfd)) continue; if (fdsu < DRVS_PER_CTLR) fd->type = NO_TYPE; #endif continue; } /* select it */ set_motor(fdcu, fdsu, TURNON); DELAY(1000000); /* 1 sec */ if (ic_type == 0 && fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { printf("fdc%d: ", fdcu); ic_type = (u_char)ic_type; switch( ic_type ) { case 0x80: printf("NEC 765\n"); fdc->fdct = FDC_NE765; kdc_fdc[fdcu].kdc_description = "NEC 765 floppy disk/tape controller"; break; case 0x81: printf("Intel 82077\n"); fdc->fdct = FDC_I82077; kdc_fdc[fdcu].kdc_description = "Intel 82077 floppy disk/tape controller"; break; case 0x90: printf("NEC 72065B\n"); fdc->fdct = FDC_NE72065; kdc_fdc[fdcu].kdc_description = "NEC 72065B floppy disk/tape controller"; break; default: printf("unknown IC type %02x\n", ic_type); fdc->fdct = FDC_UNKNOWN; break; } } if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) && (st3 & NE7_ST3_T0)) { /* if at track 0, first seek inwards */ /* seek some steps: */ (void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0); DELAY(300000); /* ...wait a moment... */ (void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */ } /* If we're at track 0 first seek inwards. */ if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) { /* Seek some steps... */ if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { /* ...wait a moment... */ DELAY(300000); /* make ctrlr happy: */ (void)fd_sense_int(fdc, 0, 0); } } for(i = 0; i < 2; i++) { /* * we must recalibrate twice, just in case the * heads have been beyond cylinder 76, since most * FDCs still barf when attempting to recalibrate * more than 77 steps */ /* go back to 0: */ if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) { /* a second being enough for full stroke seek*/ DELAY(i == 0? 1000000: 300000); /* anything responding? */ if (fd_sense_int(fdc, &st0, 0) == 0 && (st0 & NE7_ST0_EC) == 0) break; /* already probed succesfully */ } } set_motor(fdcu, fdsu, TURNOFF); if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */ continue; fd->track = FD_NO_TRACK; fd->fdc = fdc; fd->fdsu = fdsu; fd->options = 0; printf("fd%d: ", fdu); fd_registerdev(fdcu, fdu); switch (fdt) { case RTCFDT_12M: printf("1.2MB 5.25in\n"); fd->type = FD_1200; kdc_fd[fdu].kdc_description = "1.2MB (1200K) 5.25in floppy disk drive"; #ifdef DEVFS - sprintf(name,"fd%d.1200",fdu); + sprintf(name,"rfd%d.1200",fdu); #endif /* DEVFS */ break; case RTCFDT_144M: printf("1.44MB 3.5in\n"); fd->type = FD_1440; kdc_fd[fdu].kdc_description = "1.44MB (1440K) 3.5in floppy disk drive"; #ifdef DEVFS - sprintf(name,"fd%d.1440",fdu); + sprintf(name,"rfd%d.1440",fdu); #endif /* DEVFS */ break; case RTCFDT_288M: case RTCFDT_288M_1: printf("2.88MB 3.5in - 1.44MB mode\n"); fd->type = FD_1440; kdc_fd[fdu].kdc_description = "2.88MB (2880K) 3.5in floppy disk drive in 1.44 mode"; #ifdef DEVFS - sprintf(name,"fd%d.1440",fdu); + sprintf(name,"rfd%d.1440",fdu); #endif /* DEVFS */ break; case RTCFDT_360K: printf("360KB 5.25in\n"); fd->type = FD_360; kdc_fd[fdu].kdc_description = "360KB 5.25in floppy disk drive"; #ifdef DEVFS - sprintf(name,"fd%d.360",fdu); + sprintf(name,"rfd%d.360",fdu); #endif /* DEVFS */ break; case RTCFDT_720K: printf("720KB 3.5in\n"); fd->type = FD_720; kdc_fd[fdu].kdc_description = "720KB 3.5in floppy disk drive"; #ifdef DEVFS - sprintf(name,"fd%d.720",fdu); + sprintf(name,"rfd%d.720",fdu); #endif /* DEVFS */ break; default: printf("unknown\n"); fd->type = NO_TYPE; #ifdef DEVFS - sprintf(name,"fd%d.xxxx",fdu); + sprintf(name,"rfd%d.xxxx",fdu); #endif /* DEVFS */ break; } kdc_fd[fdu].kdc_state = DC_IDLE; #ifdef DEVFS fd->rfd_devfs_token = devfs_add_devsw( "/",name,&fd_cdevsw, fdu * 8, DV_CHR,0,0,0644); fd->fd_devfs_token = devfs_add_devsw( - "/",name, &fd_bdevsw, fdu * 8, + "/",name+1, &fd_bdevsw, fdu * 8, DV_BLK,0,0,0644); #endif /* DEVFS */ if (dk_ndrive < DK_NDRIVE) { sprintf(dk_names[dk_ndrive], "fd%d", fdu); fd->dkunit = dk_ndrive++; /* * XXX assume rate is FDC_500KBPS. */ dk_wpms[dk_ndrive] = 500000 / 8 / 2; } else { fd->dkunit = -1; } } return (1); } /****************************************************************************/ /* motor control stuff */ /* remember to not deselect the drive we're working on */ /****************************************************************************/ static void set_motor(fdcu_t fdcu, int fdsu, int turnon) { int fdout = fdc_data[fdcu].fdout; int needspecify = 0; if(turnon) { fdout &= ~FDO_FDSEL; fdout |= (FDO_MOEN0 << fdsu) + fdsu; } else fdout &= ~(FDO_MOEN0 << fdsu); if(!turnon && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0) /* gonna turn off the last drive, put FDC to bed */ fdout &= ~ (FDO_FRST|FDO_FDMAEN); else { /* make sure controller is selected and specified */ if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0) needspecify = 1; fdout |= (FDO_FRST|FDO_FDMAEN); } outb(fdc_data[fdcu].baseport+FDOUT, fdout); fdc_data[fdcu].fdout = fdout; kdc_fdc[fdcu].kdc_state = (fdout & FDO_FRST)? DC_BUSY: DC_IDLE; TRACE1("[0x%x->FDOUT]", fdout); if(needspecify) { /* * XXX * special case: since we have just woken up the FDC * from its sleep, we silently assume the command will * be accepted, and do not test for a timeout */ (void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 0); } } static void fd_turnoff(void *arg1) { fdu_t fdu = (fdu_t)arg1; int s; fd_p fd = fd_data + fdu; TRACE1("[fd%d: turnoff]", fdu); s = splbio(); fd->flags &= ~FD_MOTOR; set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF); splx(s); } static void fd_motor_on(void *arg1) { fdu_t fdu = (fdu_t)arg1; int s; fd_p fd = fd_data + fdu; s = splbio(); fd->flags &= ~FD_MOTOR_WAIT; if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) { fdintr(fd->fdc->fdcu); } splx(s); } static void fd_turnon(fdu_t fdu) { fd_p fd = fd_data + fdu; if(!(fd->flags & FD_MOTOR)) { fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); set_motor(fd->fdc->fdcu, fd->fdsu, TURNON); timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */ } } static void fdc_reset(fdc_p fdc) { fdcu_t fdcu = fdc->fdcu; /* Try a reset, keep motor on */ outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); DELAY(100); /* enable FDC, but defer interrupts a moment */ outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN); TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); DELAY(100); outb(fdc->baseport + FDOUT, fdc->fdout); TRACE1("[0x%x->FDOUT]", fdc->fdout); /* XXX after a reset, silently believe the FDC will accept commands */ (void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 0); } /****************************************************************************/ /* fdc in/out */ /****************************************************************************/ int in_fdc(fdcu_t fdcu) { int baseport = fdc_data[fdcu].baseport; int i, j = 100000; while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) && j-- > 0) if (i == NE7_RQM) return fdc_err(fdcu, "ready for output in input\n"); if (j <= 0) return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0); #ifdef DEBUG i = inb(baseport+FDDATA); TRACE1("[FDDATA->0x%x]", (unsigned char)i); return(i); #else return inb(baseport+FDDATA); #endif } /* * fd_in: Like in_fdc, but allows you to see if it worked. */ static int fd_in(fdcu_t fdcu, int *ptr) { int baseport = fdc_data[fdcu].baseport; int i, j = 100000; while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) && j-- > 0) if (i == NE7_RQM) return fdc_err(fdcu, "ready for output in input\n"); if (j <= 0) return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0); #ifdef DEBUG i = inb(baseport+FDDATA); TRACE1("[FDDATA->0x%x]", (unsigned char)i); *ptr = i; return 0; #else i = inb(baseport+FDDATA); if (ptr) *ptr = i; return 0; #endif } int out_fdc(fdcu_t fdcu, int x) { int baseport = fdc_data[fdcu].baseport; int i; /* Check that the direction bit is set */ i = 100000; while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0); if (i <= 0) return fdc_err(fdcu, "direction bit not set\n"); /* Check that the floppy controller is ready for a command */ i = 100000; while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0); if (i <= 0) return fdc_err(fdcu, bootverbose? "output ready timeout\n": 0); /* Send the command and return */ outb(baseport+FDDATA, x); TRACE1("[0x%x->FDDATA]", x); return (0); } /****************************************************************************/ /* fdopen/fdclose */ /****************************************************************************/ int Fdopen(dev_t dev, int flags, int mode, struct proc *p) { fdu_t fdu = FDUNIT(minor(dev)); int type = FDTYPE(minor(dev)); fdc_p fdc; #if NFT > 0 /* check for a tape open */ if (type & F_TAPE_TYPE) return(ftopen(dev, flags)); #endif /* check bounds */ if (fdu >= NFD) return(ENXIO); fdc = fd_data[fdu].fdc; if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE)) return(ENXIO); if (type > NUMDENS) return(ENXIO); if (type == 0) type = fd_data[fdu].type; else { if (type != fd_data[fdu].type) { switch (fd_data[fdu].type) { case FD_360: return(ENXIO); case FD_720: if ( type != FD_820 && type != FD_800 ) return(ENXIO); break; case FD_1200: switch (type) { case FD_1480: type = FD_1480in5_25; break; case FD_1440: type = FD_1440in5_25; break; case FD_820: type = FD_820in5_25; break; case FD_800: type = FD_800in5_25; break; case FD_720: type = FD_720in5_25; break; case FD_360: type = FD_360in5_25; break; default: return(ENXIO); } break; case FD_1440: if ( type != FD_1720 && type != FD_1480 && type != FD_1200 && type != FD_820 && type != FD_800 && type != FD_720 ) return(ENXIO); break; } } } fd_data[fdu].ft = fd_types + type - 1; fd_data[fdu].flags |= FD_OPEN; kdc_fd[fdu].kdc_state = DC_BUSY; return 0; } int fdclose(dev_t dev, int flags, int mode, struct proc *p) { fdu_t fdu = FDUNIT(minor(dev)); #if NFT > 0 int type = FDTYPE(minor(dev)); if (type & F_TAPE_TYPE) return ftclose(dev, flags); #endif fd_data[fdu].flags &= ~FD_OPEN; fd_data[fdu].options &= ~FDOPT_NORETRY; kdc_fd[fdu].kdc_state = DC_IDLE; return(0); } /****************************************************************************/ /* fdstrategy */ /****************************************************************************/ void fdstrategy(struct buf *bp) { register struct buf *dp; long nblocks, blknum; int s; fdcu_t fdcu; fdu_t fdu; fdc_p fdc; fd_p fd; size_t fdblk; fdu = FDUNIT(minor(bp->b_dev)); fd = &fd_data[fdu]; fdc = fd->fdc; fdcu = fdc->fdcu; #if NFT > 0 if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) { /* ft tapes do not (yet) support strategy i/o */ bp->b_error = ENODEV; bp->b_flags |= B_ERROR; goto bad; } /* check for controller already busy with tape */ if (fdc->flags & FDC_TAPE_BUSY) { bp->b_error = EBUSY; bp->b_flags |= B_ERROR; goto bad; } #endif fdblk = 128 << (fd->ft->secsize); if (!(bp->b_flags & B_FORMAT)) { if ((fdu >= NFD) || (bp->b_blkno < 0)) { printf( "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n", fdu, (u_long)bp->b_blkno, bp->b_bcount); bp->b_error = EINVAL; bp->b_flags |= B_ERROR; goto bad; } if ((bp->b_bcount % fdblk) != 0) { bp->b_error = EINVAL; bp->b_flags |= B_ERROR; goto bad; } } /* * Set up block calculations. */ blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk; nblocks = fd->ft->size; if (blknum + (bp->b_bcount / fdblk) > nblocks) { if (blknum == nblocks) { bp->b_resid = bp->b_bcount; } else { bp->b_error = ENOSPC; bp->b_flags |= B_ERROR; } goto bad; } bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads); bp->b_pblkno = bp->b_blkno; dp = &(fdc->head); s = splbio(); disksort(dp, bp); untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */ fdstart(fdcu); splx(s); return; bad: biodone(bp); } /***************************************************************\ * fdstart * * We have just queued something.. if the controller is not busy * * then simulate the case where it has just finished a command * * So that it (the interrupt routine) looks on the queue for more* * work to do and picks up what we just added. * * If the controller is already busy, we need do nothing, as it * * will pick up our work when the present work completes * \***************************************************************/ static void fdstart(fdcu_t fdcu) { int s; s = splbio(); if(fdc_data[fdcu].state == DEVIDLE) { fdintr(fdcu); } splx(s); } static void fd_timeout(void *arg1) { fdcu_t fdcu = (fdcu_t)arg1; fdu_t fdu = fdc_data[fdcu].fdu; int baseport = fdc_data[fdcu].baseport; struct buf *dp, *bp; int s; dp = &fdc_data[fdcu].head; bp = dp->b_actf; /* * Due to IBM's brain-dead design, the FDC has a faked ready * signal, hardwired to ready == true. Thus, any command * issued if there's no diskette in the drive will _never_ * complete, and must be aborted by resetting the FDC. * Many thanks, Big Blue! */ s = splbio(); TRACE1("fd%d[fd_timeout()]", fdu); /* See if the controller is still busy (patiently awaiting data) */ if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB) { TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS)); /* yup, it is; kill it now */ fdc_reset(&fdc_data[fdcu]); printf("fd%d: Operation timeout\n", fdu); } if (bp) { retrier(fdcu); fdc_data[fdcu].status[0] = NE7_ST0_IC_RC; fdc_data[fdcu].state = IOTIMEDOUT; if( fdc_data[fdcu].retry < 6) fdc_data[fdcu].retry = 6; } else { fdc_data[fdcu].fd = (fd_p) 0; fdc_data[fdcu].fdu = -1; fdc_data[fdcu].state = DEVIDLE; } fdintr(fdcu); splx(s); } /* just ensure it has the right spl */ static void fd_pseudointr(void *arg1) { fdcu_t fdcu = (fdcu_t)arg1; int s; s = splbio(); fdintr(fdcu); splx(s); } /***********************************************************************\ * fdintr * * keep calling the state machine until it returns a 0 * * ALWAYS called at SPLBIO * \***********************************************************************/ void fdintr(fdcu_t fdcu) { fdc_p fdc = fdc_data + fdcu; #if NFT > 0 fdu_t fdu = fdc->fdu; if (fdc->flags & FDC_TAPE_BUSY) (ftintr(fdu)); else #endif while(fdstate(fdcu, fdc)) ; } /***********************************************************************\ * The controller state machine. * * if it returns a non zero value, it should be called again immediatly * \***********************************************************************/ static int fdstate(fdcu_t fdcu, fdc_p fdc) { int read, format, head, sec = 0, sectrac, st0, cyl, st3; unsigned long blknum; fdu_t fdu = fdc->fdu; fd_p fd; register struct buf *dp, *bp; struct fd_formb *finfo = NULL; size_t fdblk; dp = &(fdc->head); bp = dp->b_actf; if(!bp) { /***********************************************\ * nothing left for this controller to do * * Force into the IDLE state, * \***********************************************/ fdc->state = DEVIDLE; if(fdc->fd) { printf("fd%d: unexpected valid fd pointer\n", fdc->fdu); fdc->fd = (fd_p) 0; fdc->fdu = -1; } TRACE1("[fdc%d IDLE]", fdcu); return(0); } fdu = FDUNIT(minor(bp->b_dev)); fd = fd_data + fdu; fdblk = 128 << fd->ft->secsize; if (fdc->fd && (fd != fdc->fd)) { printf("fd%d: confused fd pointers\n", fdu); } read = bp->b_flags & B_READ; format = bp->b_flags & B_FORMAT; if(format) finfo = (struct fd_formb *)bp->b_un.b_addr; TRACE1("fd%d", fdu); TRACE1("[%s]", fdstates[fdc->state]); TRACE1("(0x%x)", fd->flags); untimeout(fd_turnoff, (caddr_t)fdu); timeout(fd_turnoff, (caddr_t)fdu, 4 * hz); switch (fdc->state) { case DEVIDLE: case FINDWORK: /* we have found new work */ fdc->retry = 0; fd->skip = 0; fdc->fd = fd; fdc->fdu = fdu; outb(fdc->baseport+FDCTL, fd->ft->trans); TRACE1("[0x%x->FDCTL]", fd->ft->trans); /*******************************************************\ * If the next drive has a motor startup pending, then * * it will start up in it's own good time * \*******************************************************/ if(fd->flags & FD_MOTOR_WAIT) { fdc->state = MOTORWAIT; return(0); /* come back later */ } /*******************************************************\ * Maybe if it's not starting, it SHOULD be starting * \*******************************************************/ if (!(fd->flags & FD_MOTOR)) { fdc->state = MOTORWAIT; fd_turnon(fdu); return(0); } else /* at least make sure we are selected */ { set_motor(fdcu, fd->fdsu, TURNON); } fdc->state = DOSEEK; break; case DOSEEK: if (bp->b_cylin == fd->track) { fdc->state = SEEKCOMPLETE; break; } if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fd->fdsu, bp->b_cylin * fd->ft->steptrac, 0)) { /* * seek command not accepted, looks like * the FDC went off to the Saints... */ fdc->retry = 6; /* try a reset */ return(retrier(fdcu)); } fd->track = FD_NO_TRACK; fdc->state = SEEKWAIT; return(0); /* will return later */ case SEEKWAIT: /* allow heads to settle */ timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16); fdc->state = SEEKCOMPLETE; return(0); /* will return later */ case SEEKCOMPLETE : /* SEEK DONE, START DMA */ /* Make sure seek really happened*/ if(fd->track == FD_NO_TRACK) { int descyl = bp->b_cylin * fd->ft->steptrac; do { /* * This might be a "ready changed" interrupt, * which cannot really happen since the * RDY pin is hardwired to + 5 volts. This * generally indicates a "bouncing" intr * line, so do one of the following: * * When running on an enhanced FDC that is * known to not go stuck after responding * with INVALID, fetch all interrupt states * until seeing either an INVALID or a * real interrupt condition. * * When running on a dumb old NE765, give * up immediately. The controller will * provide up to four dummy RC interrupt * conditions right after reset (for the * corresponding four drives), so this is * our only chance to get notice that it * was not the FDC that caused the interrupt. */ if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) return 0; if(fdc->fdct == FDC_NE765 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) return 0; /* hope for a real intr */ } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); if (0 == descyl) { int failed = 0; /* * seek to cyl 0 requested; make sure we are * really there */ if (fd_sense_drive_status(fdc, &st3)) failed = 1; if ((st3 & NE7_ST3_T0) == 0) { printf( "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", fdu, st3, NE7_ST3BITS); failed = 1; } if (failed) { if(fdc->retry < 3) fdc->retry = 3; return(retrier(fdcu)); } } if (cyl != descyl) { printf( "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", fdu, descyl, cyl, st0); return(retrier(fdcu)); } } fd->track = bp->b_cylin; if(format) fd->skip = (char *)&(finfo->fd_formb_cylno(0)) - (char *)finfo; isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip, format ? bp->b_bcount : fdblk, fdc->dmachan); blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk + fd->skip/fdblk; sectrac = fd->ft->sectrac; sec = blknum % (sectrac * fd->ft->heads); head = sec / sectrac; sec = sec % sectrac + 1; fd->hddrv = ((head&1)<<2)+fdu; if(format || !read) { /* make sure the drive is writable */ if(fd_sense_drive_status(fdc, &st3) != 0) { /* stuck controller? */ fdc->retry = 6; /* reset the beast */ return(retrier(fdcu)); } if(st3 & NE7_ST3_WP) { /* * XXX YES! this is ugly. * in order to force the current operation * to fail, we will have to fake an FDC * error - all error handling is done * by the retrier() */ fdc->status[0] = NE7_ST0_IC_AT; fdc->status[1] = NE7_ST1_NW; fdc->status[2] = 0; fdc->status[3] = fd->track; fdc->status[4] = head; fdc->status[5] = sec; fdc->retry = 8; /* break out immediately */ fdc->state = IOTIMEDOUT; /* not really... */ return (1); } } if(format) { /* formatting */ if(fd_cmd(fdcu, 6, NE7CMD_FORMAT, head << 2 | fdu, finfo->fd_formb_secshift, finfo->fd_formb_nsecs, finfo->fd_formb_gaplen, finfo->fd_formb_fillbyte, 0)) { /* controller fell over */ fdc->retry = 6; return(retrier(fdcu)); } } else { if (fd_cmd(fdcu, 9, (read ? NE7CMD_READ : NE7CMD_WRITE), head << 2 | fdu, /* head & unit */ fd->track, /* track */ head, sec, /* sector + 1 */ fd->ft->secsize, /* sector size */ sectrac, /* sectors/track */ fd->ft->gap, /* gap size */ fd->ft->datalen, /* data length */ 0)) { /* the beast is sleeping again */ fdc->retry = 6; return(retrier(fdcu)); } } fdc->state = IOCOMPLETE; timeout(fd_timeout, (caddr_t)fdcu, hz); return(0); /* will return later */ case IOCOMPLETE: /* IO DONE, post-analyze */ untimeout(fd_timeout, (caddr_t)fdcu); if (fd_read_status(fdc, fd->fdsu)) { if (fdc->retry < 6) fdc->retry = 6; /* force a reset */ return retrier(fdcu); } fdc->state = IOTIMEDOUT; /* FALLTHROUGH */ case IOTIMEDOUT: isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip, format ? bp->b_bcount : fdblk, fdc->dmachan); if (fdc->status[0] & NE7_ST0_IC) { if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) { /* * DMA overrun. Someone hogged the bus * and didn't release it in time for the * next FDC transfer. * Just restart it, don't increment retry * count. (vak) */ fdc->state = SEEKCOMPLETE; return (1); } else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV && fdc->retry < 6) fdc->retry = 6; /* force a reset */ else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC && fdc->retry < 3) fdc->retry = 3; /* force recalibrate */ return(retrier(fdcu)); } /* All OK */ fd->skip += fdblk; if (!format && fd->skip < bp->b_bcount) { /* set up next transfer */ blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk + fd->skip/fdblk; bp->b_cylin = (blknum / (fd->ft->sectrac * fd->ft->heads)); fdc->state = DOSEEK; } else { /* ALL DONE */ fd->skip = 0; bp->b_resid = 0; dp->b_actf = bp->b_actf; biodone(bp); fdc->fd = (fd_p) 0; fdc->fdu = -1; fdc->state = FINDWORK; } return(1); case RESETCTLR: fdc_reset(fdc); fdc->retry++; fdc->state = STARTRECAL; break; case STARTRECAL: if(fd_cmd(fdcu, 2, NE7CMD_RECAL, fdu, 0)) /* Recalibrate Function */ { /* arrgl */ fdc->retry = 6; return(retrier(fdcu)); } fdc->state = RECALWAIT; return(0); /* will return later */ case RECALWAIT: /* allow heads to settle */ timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8); fdc->state = RECALCOMPLETE; return(0); /* will return later */ case RECALCOMPLETE: do { /* * See SEEKCOMPLETE for a comment on this: */ if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) return 0; if(fdc->fdct == FDC_NE765 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) return 0; /* hope for a real intr */ } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) { if(fdc->retry > 3) /* * a recalibrate from beyond cylinder 77 * will "fail" due to the FDC limitations; * since people used to complain much about * the failure message, try not logging * this one if it seems to be the first * time in a line */ printf("fd%d: recal failed ST0 %b cyl %d\n", fdu, st0, NE7_ST0BITS, cyl); if(fdc->retry < 3) fdc->retry = 3; return(retrier(fdcu)); } fd->track = 0; /* Seek (probably) necessary */ fdc->state = DOSEEK; return(1); /* will return immediatly */ case MOTORWAIT: if(fd->flags & FD_MOTOR_WAIT) { return(0); /* time's not up yet */ } /* * since the controller was off, it has lost its * idea about the current track it were; thus, * recalibrate the bastard */ fdc->state = STARTRECAL; return(1); /* will return immediatly */ default: printf("fdc%d: Unexpected FD int->", fdcu); if (fd_read_status(fdc, fd->fdsu) == 0) printf("FDC status :%lx %lx %lx %lx %lx %lx %lx ", fdc->status[0], fdc->status[1], fdc->status[2], fdc->status[3], fdc->status[4], fdc->status[5], fdc->status[6] ); else printf("No status available "); if (fd_sense_int(fdc, &st0, &cyl) != 0) { printf("[controller is dead now]\n"); return(0); } printf("ST0 = %x, PCN = %x\n", st0, cyl); return(0); } /*XXX confusing: some branches return immediately, others end up here*/ return(1); /* Come back immediatly to new state */ } static int retrier(fdcu) fdcu_t fdcu; { fdc_p fdc = fdc_data + fdcu; register struct buf *dp, *bp; dp = &(fdc->head); bp = dp->b_actf; if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY) goto fail; switch(fdc->retry) { case 0: case 1: case 2: fdc->state = SEEKCOMPLETE; break; case 3: case 4: case 5: fdc->state = STARTRECAL; break; case 6: fdc->state = RESETCTLR; break; case 7: break; default: fail: { dev_t sav_b_dev = bp->b_dev; /* Trick diskerr */ bp->b_dev = makedev(major(bp->b_dev), (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART); diskerr(bp, "fd", "hard error", LOG_PRINTF, fdc->fd->skip / DEV_BSIZE, (struct disklabel *)NULL); bp->b_dev = sav_b_dev; if (fdc->flags & FDC_STAT_VALID) { printf( " (ST0 %b ST1 %b ST2 %b cyl %ld hd %ld sec %ld)\n", fdc->status[0], NE7_ST0BITS, fdc->status[1], NE7_ST1BITS, fdc->status[2], NE7_ST2BITS, fdc->status[3], fdc->status[4], fdc->status[5]); } else printf(" (No status)\n"); } bp->b_flags |= B_ERROR; bp->b_error = EIO; bp->b_resid = bp->b_bcount - fdc->fd->skip; dp->b_actf = bp->b_actf; fdc->fd->skip = 0; biodone(bp); fdc->state = FINDWORK; fdc->fd = (fd_p) 0; fdc->fdu = -1; /* XXX abort current command, if any. */ return(1); } fdc->retry++; return(1); } static int fdformat(dev, finfo, p) dev_t dev; struct fd_formb *finfo; struct proc *p; { fdu_t fdu; fd_p fd; struct buf *bp; int rv = 0, s; size_t fdblk; fdu = FDUNIT(minor(dev)); fd = &fd_data[fdu]; fdblk = 128 << fd->ft->secsize; /* set up a buffer header for fdstrategy() */ bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT); if(bp == 0) return ENOBUFS; /* * keep the process from being swapped */ p->p_flag |= P_PHYSIO; bzero((void *)bp, sizeof(struct buf)); bp->b_flags = B_BUSY | B_PHYS | B_FORMAT; bp->b_proc = p; bp->b_dev = dev; /* * calculate a fake blkno, so fdstrategy() would initiate a * seek to the requested cylinder */ bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE; bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs; bp->b_un.b_addr = (caddr_t)finfo; /* now do the format */ fdstrategy(bp); /* ...and wait for it to complete */ s = splbio(); while(!(bp->b_flags & B_DONE)) { rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz); if(rv == EWOULDBLOCK) break; } splx(s); if(rv == EWOULDBLOCK) { /* timed out */ rv = EIO; biodone(bp); } if(bp->b_flags & B_ERROR) rv = bp->b_error; /* * allow the process to be swapped */ p->p_flag &= ~P_PHYSIO; free(bp, M_TEMP); return rv; } /* * TODO: don't allocate buffer on stack. */ int fdioctl(dev, cmd, addr, flag, p) dev_t dev; int cmd; caddr_t addr; int flag; struct proc *p; { fdu_t fdu = FDUNIT(minor(dev)); fd_p fd = &fd_data[fdu]; size_t fdblk; struct fd_type *fdt; struct disklabel *dl; char buffer[DEV_BSIZE]; int error = 0; #if NFT > 0 int type = FDTYPE(minor(dev)); /* check for a tape ioctl */ if (type & F_TAPE_TYPE) return ftioctl(dev, cmd, addr, flag, p); #endif fdblk = 128 << fd->ft->secsize; switch (cmd) { case DIOCGDINFO: bzero(buffer, sizeof (buffer)); dl = (struct disklabel *)buffer; dl->d_secsize = fdblk; fdt = fd_data[FDUNIT(minor(dev))].ft; dl->d_secpercyl = fdt->size / fdt->tracks; dl->d_type = DTYPE_FLOPPY; if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl) == NULL) error = 0; else error = EINVAL; *(struct disklabel *)addr = *dl; break; case DIOCSDINFO: if ((flag & FWRITE) == 0) error = EBADF; break; case DIOCWLABEL: if ((flag & FWRITE) == 0) error = EBADF; break; case DIOCWDINFO: if ((flag & FWRITE) == 0) { error = EBADF; break; } dl = (struct disklabel *)addr; if ((error = setdisklabel((struct disklabel *)buffer, dl, (u_long)0)) != 0) break; error = writedisklabel(dev, fdstrategy, (struct disklabel *)buffer); break; case FD_FORM: if((flag & FWRITE) == 0) error = EBADF; /* must be opened for writing */ else if(((struct fd_formb *)addr)->format_version != FD_FORMAT_VERSION) error = EINVAL; /* wrong version of formatting prog */ else error = fdformat(dev, (struct fd_formb *)addr, p); break; case FD_GTYPE: /* get drive type */ *(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft; break; case FD_STYPE: /* set drive type */ /* this is considered harmful; only allow for superuser */ if(suser(p->p_ucred, &p->p_acflag) != 0) return EPERM; *fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr; break; case FD_GOPTS: /* get drive options */ *(int *)addr = fd_data[FDUNIT(minor(dev))].options; break; case FD_SOPTS: /* set drive options */ fd_data[FDUNIT(minor(dev))].options = *(int *)addr; break; default: error = ENOTTY; break; } return (error); } static fd_devsw_installed = 0; static void fd_drvinit(void *notused ) { dev_t dev; if( ! fd_devsw_installed ) { dev = makedev(CDEV_MAJOR, 0); cdevsw_add(&dev,&fd_cdevsw, NULL); dev = makedev(BDEV_MAJOR, 0); bdevsw_add(&dev,&fd_bdevsw, NULL); fd_devsw_installed = 1; } } SYSINIT(fddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,fd_drvinit,NULL) #endif /* * Hello emacs, these are the * Local Variables: * c-indent-level: 8 * c-continued-statement-offset: 8 * c-continued-brace-offset: 0 * c-brace-offset: -8 * c-brace-imaginary-offset: 0 * c-argdecl-indent: 8 * c-label-offset: -8 * c++-hanging-braces: 1 * c++-access-specifier-offset: -8 * c++-empty-arglist-indent: 8 * c++-friend-offset: 0 * End: */ Index: head/sys/isa/fd.c =================================================================== --- head/sys/isa/fd.c (revision 12690) +++ head/sys/isa/fd.c (revision 12691) @@ -1,1945 +1,1945 @@ /* * Copyright (c) 1990 The Regents of the University of California. * All rights reserved. * * This code is derived from software contributed to Berkeley by * Don Ahn. * * Copyright (c) 1993, 1994 by * jc@irbs.UUCP (John Capo) * vak@zebub.msk.su (Serge Vakulenko) * ache@astral.msk.su (Andrew A. Chernov) * * Copyright (c) 1993, 1994, 1995 by * joerg_wunsch@uriah.sax.de (Joerg Wunsch) * dufault@hda.com (Peter Dufault) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * from: @(#)fd.c 7.4 (Berkeley) 5/25/91 - * $Id: fd.c,v 1.73 1995/12/08 11:14:10 julian Exp $ + * $Id: fd.c,v 1.74 1995/12/08 23:20:26 phk Exp $ * */ #include "ft.h" #if NFT < 1 #undef NFDC #endif #include "fd.h" #if NFDC > 0 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if NFT > 0 #include #include #endif #ifdef DEVFS #include #endif static int fd_goaway(struct kern_devconf *, int); static int fdc_goaway(struct kern_devconf *, int); static int fd_externalize(struct kern_devconf *, struct sysctl_req *); /* * Templates for the kern_devconf structures used when we attach. */ static struct kern_devconf kdc_fd[NFD] = { { 0, 0, 0, /* filled in by kern_devconf.c */ "fd", 0, { MDDT_DISK, 0 }, fd_externalize, 0, fd_goaway, DISK_EXTERNALLEN, 0, /* parent */ 0, /* parentdata */ DC_UNCONFIGURED, /* state */ "floppy disk", DC_CLS_DISK /* class */ } }; struct kern_devconf kdc_fdc[NFDC] = { { 0, 0, 0, /* filled in by kern_devconf.c */ "fdc", 0, { MDDT_ISA, 0, "bio" }, isa_generic_externalize, 0, fdc_goaway, ISA_EXTERNALLEN, 0, /* parent */ 0, /* parentdata */ DC_UNCONFIGURED, /* state */ "floppy disk/tape controller", DC_CLS_MISC /* class */ } }; static inline void fd_registerdev(int ctlr, int unit) { if(unit != 0) kdc_fd[unit] = kdc_fd[0]; kdc_fd[unit].kdc_unit = unit; kdc_fd[unit].kdc_parent = &kdc_fdc[ctlr]; kdc_fd[unit].kdc_parentdata = 0; dev_attach(&kdc_fd[unit]); } static inline void fdc_registerdev(struct isa_device *dvp) { int unit = dvp->id_unit; if(unit != 0) kdc_fdc[unit] = kdc_fdc[0]; kdc_fdc[unit].kdc_unit = unit; kdc_fdc[unit].kdc_parent = &kdc_isa0; kdc_fdc[unit].kdc_parentdata = dvp; dev_attach(&kdc_fdc[unit]); } static int fdc_goaway(struct kern_devconf *kdc, int force) { if(force) { dev_detach(kdc); return 0; } else { return EBUSY; /* XXX fix */ } } static int fd_goaway(struct kern_devconf *kdc, int force) { dev_detach(kdc); return 0; } #define b_cylin b_resid /* XXX now spelled b_cylinder elsewhere */ /* misuse a flag to identify format operation */ #define B_FORMAT B_XXX /* * this biotab field doubles as a field for the physical unit number * on the controller */ #define id_physid id_scsiid /* error returns for fd_cmd() */ #define FD_FAILED -1 #define FD_NOT_VALID -2 #define FDC_ERRMAX 100 /* do not log more */ #define NUMTYPES 14 #define NUMDENS (NUMTYPES - 6) /* These defines (-1) must match index for fd_types */ #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */ #define NO_TYPE 0 /* must match NO_TYPE in ft.c */ #define FD_1720 1 #define FD_1480 2 #define FD_1440 3 #define FD_1200 4 #define FD_820 5 #define FD_800 6 #define FD_720 7 #define FD_360 8 #define FD_1480in5_25 9 #define FD_1440in5_25 10 #define FD_820in5_25 11 #define FD_800in5_25 12 #define FD_720in5_25 13 #define FD_360in5_25 14 struct fd_type fd_types[NUMTYPES] = { { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */ { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */ { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */ { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */ { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */ { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */ { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */ { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */ { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */ { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */ { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */ { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */ { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */ { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */ }; #define DRVS_PER_CTLR 2 /* 2 floppies */ /***********************************************************************\ * Per controller structure. * \***********************************************************************/ struct fdc_data fdc_data[NFDC]; /***********************************************************************\ * Per drive structure. * * N per controller (DRVS_PER_CTLR) * \***********************************************************************/ struct fd_data { struct fdc_data *fdc; /* pointer to controller structure */ int fdsu; /* this units number on this controller */ int type; /* Drive type (FD_1440...) */ struct fd_type *ft; /* pointer to the type descriptor */ int flags; #define FD_OPEN 0x01 /* it's open */ #define FD_ACTIVE 0x02 /* it's active */ #define FD_MOTOR 0x04 /* motor should be on */ #define FD_MOTOR_WAIT 0x08 /* motor coming up */ int skip; int hddrv; #define FD_NO_TRACK -2 int track; /* where we think the head is */ int options; /* user configurable options, see ioctl_fd.h */ int dkunit; /* disk stats unit number */ #ifdef DEVFS void *rfd_devfs_token; void *fd_devfs_token; #endif } fd_data[NFD]; /***********************************************************************\ * Throughout this file the following conventions will be used: * * fd is a pointer to the fd_data struct for the drive in question * * fdc is a pointer to the fdc_data struct for the controller * * fdu is the floppy drive unit number * * fdcu is the floppy controller unit number * * fdsu is the floppy drive unit number on that controller. (sub-unit) * \***********************************************************************/ #if NFT > 0 int ftopen(dev_t, int); int ftintr(ftu_t ftu); int ftclose(dev_t, int); void ftstrategy(struct buf *); int ftioctl(dev_t, int, caddr_t, int, struct proc *); int ftdump(dev_t); int ftsize(dev_t); int ftattach(struct isa_device *, struct isa_device *, int); #endif /* autoconfig functions */ static int fdprobe(struct isa_device *); static int fdattach(struct isa_device *); /* needed for ft driver, thus exported */ int in_fdc(fdcu_t); int out_fdc(fdcu_t, int); /* internal functions */ static void set_motor(fdcu_t, int, int); # define TURNON 1 # define TURNOFF 0 static timeout_t fd_turnoff; static timeout_t fd_motor_on; static void fd_turnon(fdu_t); static void fdc_reset(fdc_p); static int fd_in(fdcu_t, int *); static void fdstart(fdcu_t); static timeout_t fd_timeout; static timeout_t fd_pseudointr; static int fdstate(fdcu_t, fdc_p); static int retrier(fdcu_t); static int fdformat(dev_t, struct fd_formb *, struct proc *); #define DEVIDLE 0 #define FINDWORK 1 #define DOSEEK 2 #define SEEKCOMPLETE 3 #define IOCOMPLETE 4 #define RECALCOMPLETE 5 #define STARTRECAL 6 #define RESETCTLR 7 #define SEEKWAIT 8 #define RECALWAIT 9 #define MOTORWAIT 10 #define IOTIMEDOUT 11 #ifdef DEBUG char *fdstates[] = { "DEVIDLE", "FINDWORK", "DOSEEK", "SEEKCOMPLETE", "IOCOMPLETE", "RECALCOMPLETE", "STARTRECAL", "RESETCTLR", "SEEKWAIT", "RECALWAIT", "MOTORWAIT", "IOTIMEDOUT" }; /* CAUTION: fd_debug causes huge amounts of logging output */ int fd_debug = 0; #define TRACE0(arg) if(fd_debug) printf(arg) #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2) #else /* DEBUG */ #define TRACE0(arg) #define TRACE1(arg1, arg2) #endif /* DEBUG */ /* autoconfig structure */ struct isa_driver fdcdriver = { fdprobe, fdattach, "fdc", }; static d_open_t Fdopen; /* NOTE, not fdopen */ static d_close_t fdclose; static d_ioctl_t fdioctl; static d_strategy_t fdstrategy; #define CDEV_MAJOR 9 #define BDEV_MAJOR 2 extern struct cdevsw fd_cdevsw; static struct bdevsw fd_bdevsw = { Fdopen, fdclose, fdstrategy, fdioctl, /*2*/ nxdump, zerosize, 0, "fd", &fd_cdevsw, -1 }; static struct cdevsw fd_cdevsw = { Fdopen, fdclose, rawread, rawwrite, /*9*/ fdioctl, nostop, nullreset, nodevtotty, seltrue, nommap, fdstrategy, "fd", &fd_bdevsw, -1 }; struct isa_device *fdcdevs[NFDC]; /* * Provide hw.devconf information. */ static int fd_externalize(struct kern_devconf *kdc, struct sysctl_req *req) { return disk_externalize(fd_data[kdc->kdc_unit].fdsu, req); } static int fdc_err(fdcu_t fdcu, const char *s) { fdc_data[fdcu].fdc_errs++; if(s) { if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX) printf("fdc%d: %s", fdcu, s); else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX) printf("fdc%d: too many errors, not logging any more\n", fdcu); } return FD_FAILED; } /* * fd_cmd: Send a command to the chip. Takes a varargs with this structure: * Unit number, * # of output bytes, output bytes as ints ..., * # of input bytes, input bytes as ints ... */ int fd_cmd(fdcu_t fdcu, int n_out, ...) { u_char cmd; int n_in; int n; va_list ap; va_start(ap, n_out); cmd = (u_char)(va_arg(ap, int)); va_end(ap); va_start(ap, n_out); for (n = 0; n < n_out; n++) { if (out_fdc(fdcu, va_arg(ap, int)) < 0) { char msg[50]; sprintf(msg, "cmd %x failed at out byte %d of %d\n", cmd, n + 1, n_out); return fdc_err(fdcu, msg); } } n_in = va_arg(ap, int); for (n = 0; n < n_in; n++) { int *ptr = va_arg(ap, int *); if (fd_in(fdcu, ptr) < 0) { char msg[50]; sprintf(msg, "cmd %02x failed at in byte %d of %d\n", cmd, n + 1, n_in); return fdc_err(fdcu, msg); } } return 0; } int fd_sense_drive_status(fdc_p fdc, int *st3p) { int st3; if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) { return fdc_err(fdc->fdcu, "Sense Drive Status failed\n"); } if (st3p) *st3p = st3; return 0; } int fd_sense_int(fdc_p fdc, int *st0p, int *cylp) { int st0, cyl; int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0); if (ret) { (void)fdc_err(fdc->fdcu, "sense intr err reading stat reg 0\n"); return ret; } if (st0p) *st0p = st0; if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) { /* * There doesn't seem to have been an interrupt. */ return FD_NOT_VALID; } if (fd_in(fdc->fdcu, &cyl) < 0) { return fdc_err(fdc->fdcu, "can't get cyl num\n"); } if (cylp) *cylp = cyl; return 0; } int fd_read_status(fdc_p fdc, int fdsu) { int i, ret; for (i = 0; i < 7; i++) { /* * XXX types are poorly chosen. Only bytes can by read * from the hardware, but fdc_status wants u_longs and * fd_in() gives ints. */ int status; ret = fd_in(fdc->fdcu, &status); fdc->status[i] = status; if (ret != 0) break; } if (ret == 0) fdc->flags |= FDC_STAT_VALID; else fdc->flags &= ~FDC_STAT_VALID; return ret; } /****************************************************************************/ /* autoconfiguration stuff */ /****************************************************************************/ /* * probe for existance of controller */ static int fdprobe(struct isa_device *dev) { fdcu_t fdcu = dev->id_unit; if(fdc_data[fdcu].flags & FDC_ATTACHED) { printf("fdc%d: unit used multiple times\n", fdcu); return 0; } fdcdevs[fdcu] = dev; fdc_data[fdcu].baseport = dev->id_iobase; #ifndef DEV_LKM fdc_registerdev(dev); #endif /* First - lets reset the floppy controller */ outb(dev->id_iobase+FDOUT, 0); DELAY(100); outb(dev->id_iobase+FDOUT, FDO_FRST); /* see if it can handle a command */ if (fd_cmd(fdcu, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 0)) { return(0); } kdc_fdc[fdcu].kdc_state = DC_IDLE; return (IO_FDCSIZE); } /* * wire controller into system, look for floppy units */ static int fdattach(struct isa_device *dev) { unsigned fdt; fdu_t fdu; fdcu_t fdcu = dev->id_unit; fdc_p fdc = fdc_data + fdcu; fd_p fd; int fdsu, st0, st3, i, unithasfd; struct isa_device *fdup; int ic_type = 0; #ifdef DEVFS char name[64]; #endif /* DEVFS */ fdc->fdcu = fdcu; fdc->flags |= FDC_ATTACHED; fdc->dmachan = dev->id_drq; fdc->state = DEVIDLE; /* reset controller, turn motor off, clear fdout mirror reg */ outb(fdc->baseport + FDOUT, ((fdc->fdout = 0))); /* check for each floppy drive */ for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) { if (fdup->id_iobase != dev->id_iobase) continue; fdu = fdup->id_unit; fd = &fd_data[fdu]; if (fdu >= (NFD+NFT)) continue; fdsu = fdup->id_physid; /* look up what bios thinks we have */ switch (fdu) { case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0); break; case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0); break; default: fdt = RTCFDT_NONE; break; } /* is there a unit? */ if ((fdt == RTCFDT_NONE) #if NFT > 0 || (fdsu >= DRVS_PER_CTLR)) { #else ) { fd->type = NO_TYPE; #endif #if NFT > 0 /* If BIOS says no floppy, or > 2nd device */ /* Probe for and attach a floppy tape. */ /* Tell FT if there was already a disk */ /* with this unit number found. */ unithasfd = 0; if (fdu < NFD && fd->type != NO_TYPE) unithasfd = 1; if (ftattach(dev, fdup, unithasfd)) continue; if (fdsu < DRVS_PER_CTLR) fd->type = NO_TYPE; #endif continue; } /* select it */ set_motor(fdcu, fdsu, TURNON); DELAY(1000000); /* 1 sec */ if (ic_type == 0 && fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { printf("fdc%d: ", fdcu); ic_type = (u_char)ic_type; switch( ic_type ) { case 0x80: printf("NEC 765\n"); fdc->fdct = FDC_NE765; kdc_fdc[fdcu].kdc_description = "NEC 765 floppy disk/tape controller"; break; case 0x81: printf("Intel 82077\n"); fdc->fdct = FDC_I82077; kdc_fdc[fdcu].kdc_description = "Intel 82077 floppy disk/tape controller"; break; case 0x90: printf("NEC 72065B\n"); fdc->fdct = FDC_NE72065; kdc_fdc[fdcu].kdc_description = "NEC 72065B floppy disk/tape controller"; break; default: printf("unknown IC type %02x\n", ic_type); fdc->fdct = FDC_UNKNOWN; break; } } if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) && (st3 & NE7_ST3_T0)) { /* if at track 0, first seek inwards */ /* seek some steps: */ (void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0); DELAY(300000); /* ...wait a moment... */ (void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */ } /* If we're at track 0 first seek inwards. */ if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) { /* Seek some steps... */ if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { /* ...wait a moment... */ DELAY(300000); /* make ctrlr happy: */ (void)fd_sense_int(fdc, 0, 0); } } for(i = 0; i < 2; i++) { /* * we must recalibrate twice, just in case the * heads have been beyond cylinder 76, since most * FDCs still barf when attempting to recalibrate * more than 77 steps */ /* go back to 0: */ if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) { /* a second being enough for full stroke seek*/ DELAY(i == 0? 1000000: 300000); /* anything responding? */ if (fd_sense_int(fdc, &st0, 0) == 0 && (st0 & NE7_ST0_EC) == 0) break; /* already probed succesfully */ } } set_motor(fdcu, fdsu, TURNOFF); if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */ continue; fd->track = FD_NO_TRACK; fd->fdc = fdc; fd->fdsu = fdsu; fd->options = 0; printf("fd%d: ", fdu); fd_registerdev(fdcu, fdu); switch (fdt) { case RTCFDT_12M: printf("1.2MB 5.25in\n"); fd->type = FD_1200; kdc_fd[fdu].kdc_description = "1.2MB (1200K) 5.25in floppy disk drive"; #ifdef DEVFS - sprintf(name,"fd%d.1200",fdu); + sprintf(name,"rfd%d.1200",fdu); #endif /* DEVFS */ break; case RTCFDT_144M: printf("1.44MB 3.5in\n"); fd->type = FD_1440; kdc_fd[fdu].kdc_description = "1.44MB (1440K) 3.5in floppy disk drive"; #ifdef DEVFS - sprintf(name,"fd%d.1440",fdu); + sprintf(name,"rfd%d.1440",fdu); #endif /* DEVFS */ break; case RTCFDT_288M: case RTCFDT_288M_1: printf("2.88MB 3.5in - 1.44MB mode\n"); fd->type = FD_1440; kdc_fd[fdu].kdc_description = "2.88MB (2880K) 3.5in floppy disk drive in 1.44 mode"; #ifdef DEVFS - sprintf(name,"fd%d.1440",fdu); + sprintf(name,"rfd%d.1440",fdu); #endif /* DEVFS */ break; case RTCFDT_360K: printf("360KB 5.25in\n"); fd->type = FD_360; kdc_fd[fdu].kdc_description = "360KB 5.25in floppy disk drive"; #ifdef DEVFS - sprintf(name,"fd%d.360",fdu); + sprintf(name,"rfd%d.360",fdu); #endif /* DEVFS */ break; case RTCFDT_720K: printf("720KB 3.5in\n"); fd->type = FD_720; kdc_fd[fdu].kdc_description = "720KB 3.5in floppy disk drive"; #ifdef DEVFS - sprintf(name,"fd%d.720",fdu); + sprintf(name,"rfd%d.720",fdu); #endif /* DEVFS */ break; default: printf("unknown\n"); fd->type = NO_TYPE; #ifdef DEVFS - sprintf(name,"fd%d.xxxx",fdu); + sprintf(name,"rfd%d.xxxx",fdu); #endif /* DEVFS */ break; } kdc_fd[fdu].kdc_state = DC_IDLE; #ifdef DEVFS fd->rfd_devfs_token = devfs_add_devsw( "/",name,&fd_cdevsw, fdu * 8, DV_CHR,0,0,0644); fd->fd_devfs_token = devfs_add_devsw( - "/",name, &fd_bdevsw, fdu * 8, + "/",name+1, &fd_bdevsw, fdu * 8, DV_BLK,0,0,0644); #endif /* DEVFS */ if (dk_ndrive < DK_NDRIVE) { sprintf(dk_names[dk_ndrive], "fd%d", fdu); fd->dkunit = dk_ndrive++; /* * XXX assume rate is FDC_500KBPS. */ dk_wpms[dk_ndrive] = 500000 / 8 / 2; } else { fd->dkunit = -1; } } return (1); } /****************************************************************************/ /* motor control stuff */ /* remember to not deselect the drive we're working on */ /****************************************************************************/ static void set_motor(fdcu_t fdcu, int fdsu, int turnon) { int fdout = fdc_data[fdcu].fdout; int needspecify = 0; if(turnon) { fdout &= ~FDO_FDSEL; fdout |= (FDO_MOEN0 << fdsu) + fdsu; } else fdout &= ~(FDO_MOEN0 << fdsu); if(!turnon && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0) /* gonna turn off the last drive, put FDC to bed */ fdout &= ~ (FDO_FRST|FDO_FDMAEN); else { /* make sure controller is selected and specified */ if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0) needspecify = 1; fdout |= (FDO_FRST|FDO_FDMAEN); } outb(fdc_data[fdcu].baseport+FDOUT, fdout); fdc_data[fdcu].fdout = fdout; kdc_fdc[fdcu].kdc_state = (fdout & FDO_FRST)? DC_BUSY: DC_IDLE; TRACE1("[0x%x->FDOUT]", fdout); if(needspecify) { /* * XXX * special case: since we have just woken up the FDC * from its sleep, we silently assume the command will * be accepted, and do not test for a timeout */ (void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 0); } } static void fd_turnoff(void *arg1) { fdu_t fdu = (fdu_t)arg1; int s; fd_p fd = fd_data + fdu; TRACE1("[fd%d: turnoff]", fdu); s = splbio(); fd->flags &= ~FD_MOTOR; set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF); splx(s); } static void fd_motor_on(void *arg1) { fdu_t fdu = (fdu_t)arg1; int s; fd_p fd = fd_data + fdu; s = splbio(); fd->flags &= ~FD_MOTOR_WAIT; if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) { fdintr(fd->fdc->fdcu); } splx(s); } static void fd_turnon(fdu_t fdu) { fd_p fd = fd_data + fdu; if(!(fd->flags & FD_MOTOR)) { fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); set_motor(fd->fdc->fdcu, fd->fdsu, TURNON); timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */ } } static void fdc_reset(fdc_p fdc) { fdcu_t fdcu = fdc->fdcu; /* Try a reset, keep motor on */ outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); DELAY(100); /* enable FDC, but defer interrupts a moment */ outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN); TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); DELAY(100); outb(fdc->baseport + FDOUT, fdc->fdout); TRACE1("[0x%x->FDOUT]", fdc->fdout); /* XXX after a reset, silently believe the FDC will accept commands */ (void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 0); } /****************************************************************************/ /* fdc in/out */ /****************************************************************************/ int in_fdc(fdcu_t fdcu) { int baseport = fdc_data[fdcu].baseport; int i, j = 100000; while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) && j-- > 0) if (i == NE7_RQM) return fdc_err(fdcu, "ready for output in input\n"); if (j <= 0) return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0); #ifdef DEBUG i = inb(baseport+FDDATA); TRACE1("[FDDATA->0x%x]", (unsigned char)i); return(i); #else return inb(baseport+FDDATA); #endif } /* * fd_in: Like in_fdc, but allows you to see if it worked. */ static int fd_in(fdcu_t fdcu, int *ptr) { int baseport = fdc_data[fdcu].baseport; int i, j = 100000; while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) && j-- > 0) if (i == NE7_RQM) return fdc_err(fdcu, "ready for output in input\n"); if (j <= 0) return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0); #ifdef DEBUG i = inb(baseport+FDDATA); TRACE1("[FDDATA->0x%x]", (unsigned char)i); *ptr = i; return 0; #else i = inb(baseport+FDDATA); if (ptr) *ptr = i; return 0; #endif } int out_fdc(fdcu_t fdcu, int x) { int baseport = fdc_data[fdcu].baseport; int i; /* Check that the direction bit is set */ i = 100000; while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0); if (i <= 0) return fdc_err(fdcu, "direction bit not set\n"); /* Check that the floppy controller is ready for a command */ i = 100000; while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0); if (i <= 0) return fdc_err(fdcu, bootverbose? "output ready timeout\n": 0); /* Send the command and return */ outb(baseport+FDDATA, x); TRACE1("[0x%x->FDDATA]", x); return (0); } /****************************************************************************/ /* fdopen/fdclose */ /****************************************************************************/ int Fdopen(dev_t dev, int flags, int mode, struct proc *p) { fdu_t fdu = FDUNIT(minor(dev)); int type = FDTYPE(minor(dev)); fdc_p fdc; #if NFT > 0 /* check for a tape open */ if (type & F_TAPE_TYPE) return(ftopen(dev, flags)); #endif /* check bounds */ if (fdu >= NFD) return(ENXIO); fdc = fd_data[fdu].fdc; if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE)) return(ENXIO); if (type > NUMDENS) return(ENXIO); if (type == 0) type = fd_data[fdu].type; else { if (type != fd_data[fdu].type) { switch (fd_data[fdu].type) { case FD_360: return(ENXIO); case FD_720: if ( type != FD_820 && type != FD_800 ) return(ENXIO); break; case FD_1200: switch (type) { case FD_1480: type = FD_1480in5_25; break; case FD_1440: type = FD_1440in5_25; break; case FD_820: type = FD_820in5_25; break; case FD_800: type = FD_800in5_25; break; case FD_720: type = FD_720in5_25; break; case FD_360: type = FD_360in5_25; break; default: return(ENXIO); } break; case FD_1440: if ( type != FD_1720 && type != FD_1480 && type != FD_1200 && type != FD_820 && type != FD_800 && type != FD_720 ) return(ENXIO); break; } } } fd_data[fdu].ft = fd_types + type - 1; fd_data[fdu].flags |= FD_OPEN; kdc_fd[fdu].kdc_state = DC_BUSY; return 0; } int fdclose(dev_t dev, int flags, int mode, struct proc *p) { fdu_t fdu = FDUNIT(minor(dev)); #if NFT > 0 int type = FDTYPE(minor(dev)); if (type & F_TAPE_TYPE) return ftclose(dev, flags); #endif fd_data[fdu].flags &= ~FD_OPEN; fd_data[fdu].options &= ~FDOPT_NORETRY; kdc_fd[fdu].kdc_state = DC_IDLE; return(0); } /****************************************************************************/ /* fdstrategy */ /****************************************************************************/ void fdstrategy(struct buf *bp) { register struct buf *dp; long nblocks, blknum; int s; fdcu_t fdcu; fdu_t fdu; fdc_p fdc; fd_p fd; size_t fdblk; fdu = FDUNIT(minor(bp->b_dev)); fd = &fd_data[fdu]; fdc = fd->fdc; fdcu = fdc->fdcu; #if NFT > 0 if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) { /* ft tapes do not (yet) support strategy i/o */ bp->b_error = ENODEV; bp->b_flags |= B_ERROR; goto bad; } /* check for controller already busy with tape */ if (fdc->flags & FDC_TAPE_BUSY) { bp->b_error = EBUSY; bp->b_flags |= B_ERROR; goto bad; } #endif fdblk = 128 << (fd->ft->secsize); if (!(bp->b_flags & B_FORMAT)) { if ((fdu >= NFD) || (bp->b_blkno < 0)) { printf( "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n", fdu, (u_long)bp->b_blkno, bp->b_bcount); bp->b_error = EINVAL; bp->b_flags |= B_ERROR; goto bad; } if ((bp->b_bcount % fdblk) != 0) { bp->b_error = EINVAL; bp->b_flags |= B_ERROR; goto bad; } } /* * Set up block calculations. */ blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk; nblocks = fd->ft->size; if (blknum + (bp->b_bcount / fdblk) > nblocks) { if (blknum == nblocks) { bp->b_resid = bp->b_bcount; } else { bp->b_error = ENOSPC; bp->b_flags |= B_ERROR; } goto bad; } bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads); bp->b_pblkno = bp->b_blkno; dp = &(fdc->head); s = splbio(); disksort(dp, bp); untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */ fdstart(fdcu); splx(s); return; bad: biodone(bp); } /***************************************************************\ * fdstart * * We have just queued something.. if the controller is not busy * * then simulate the case where it has just finished a command * * So that it (the interrupt routine) looks on the queue for more* * work to do and picks up what we just added. * * If the controller is already busy, we need do nothing, as it * * will pick up our work when the present work completes * \***************************************************************/ static void fdstart(fdcu_t fdcu) { int s; s = splbio(); if(fdc_data[fdcu].state == DEVIDLE) { fdintr(fdcu); } splx(s); } static void fd_timeout(void *arg1) { fdcu_t fdcu = (fdcu_t)arg1; fdu_t fdu = fdc_data[fdcu].fdu; int baseport = fdc_data[fdcu].baseport; struct buf *dp, *bp; int s; dp = &fdc_data[fdcu].head; bp = dp->b_actf; /* * Due to IBM's brain-dead design, the FDC has a faked ready * signal, hardwired to ready == true. Thus, any command * issued if there's no diskette in the drive will _never_ * complete, and must be aborted by resetting the FDC. * Many thanks, Big Blue! */ s = splbio(); TRACE1("fd%d[fd_timeout()]", fdu); /* See if the controller is still busy (patiently awaiting data) */ if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB) { TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS)); /* yup, it is; kill it now */ fdc_reset(&fdc_data[fdcu]); printf("fd%d: Operation timeout\n", fdu); } if (bp) { retrier(fdcu); fdc_data[fdcu].status[0] = NE7_ST0_IC_RC; fdc_data[fdcu].state = IOTIMEDOUT; if( fdc_data[fdcu].retry < 6) fdc_data[fdcu].retry = 6; } else { fdc_data[fdcu].fd = (fd_p) 0; fdc_data[fdcu].fdu = -1; fdc_data[fdcu].state = DEVIDLE; } fdintr(fdcu); splx(s); } /* just ensure it has the right spl */ static void fd_pseudointr(void *arg1) { fdcu_t fdcu = (fdcu_t)arg1; int s; s = splbio(); fdintr(fdcu); splx(s); } /***********************************************************************\ * fdintr * * keep calling the state machine until it returns a 0 * * ALWAYS called at SPLBIO * \***********************************************************************/ void fdintr(fdcu_t fdcu) { fdc_p fdc = fdc_data + fdcu; #if NFT > 0 fdu_t fdu = fdc->fdu; if (fdc->flags & FDC_TAPE_BUSY) (ftintr(fdu)); else #endif while(fdstate(fdcu, fdc)) ; } /***********************************************************************\ * The controller state machine. * * if it returns a non zero value, it should be called again immediatly * \***********************************************************************/ static int fdstate(fdcu_t fdcu, fdc_p fdc) { int read, format, head, sec = 0, sectrac, st0, cyl, st3; unsigned long blknum; fdu_t fdu = fdc->fdu; fd_p fd; register struct buf *dp, *bp; struct fd_formb *finfo = NULL; size_t fdblk; dp = &(fdc->head); bp = dp->b_actf; if(!bp) { /***********************************************\ * nothing left for this controller to do * * Force into the IDLE state, * \***********************************************/ fdc->state = DEVIDLE; if(fdc->fd) { printf("fd%d: unexpected valid fd pointer\n", fdc->fdu); fdc->fd = (fd_p) 0; fdc->fdu = -1; } TRACE1("[fdc%d IDLE]", fdcu); return(0); } fdu = FDUNIT(minor(bp->b_dev)); fd = fd_data + fdu; fdblk = 128 << fd->ft->secsize; if (fdc->fd && (fd != fdc->fd)) { printf("fd%d: confused fd pointers\n", fdu); } read = bp->b_flags & B_READ; format = bp->b_flags & B_FORMAT; if(format) finfo = (struct fd_formb *)bp->b_un.b_addr; TRACE1("fd%d", fdu); TRACE1("[%s]", fdstates[fdc->state]); TRACE1("(0x%x)", fd->flags); untimeout(fd_turnoff, (caddr_t)fdu); timeout(fd_turnoff, (caddr_t)fdu, 4 * hz); switch (fdc->state) { case DEVIDLE: case FINDWORK: /* we have found new work */ fdc->retry = 0; fd->skip = 0; fdc->fd = fd; fdc->fdu = fdu; outb(fdc->baseport+FDCTL, fd->ft->trans); TRACE1("[0x%x->FDCTL]", fd->ft->trans); /*******************************************************\ * If the next drive has a motor startup pending, then * * it will start up in it's own good time * \*******************************************************/ if(fd->flags & FD_MOTOR_WAIT) { fdc->state = MOTORWAIT; return(0); /* come back later */ } /*******************************************************\ * Maybe if it's not starting, it SHOULD be starting * \*******************************************************/ if (!(fd->flags & FD_MOTOR)) { fdc->state = MOTORWAIT; fd_turnon(fdu); return(0); } else /* at least make sure we are selected */ { set_motor(fdcu, fd->fdsu, TURNON); } fdc->state = DOSEEK; break; case DOSEEK: if (bp->b_cylin == fd->track) { fdc->state = SEEKCOMPLETE; break; } if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fd->fdsu, bp->b_cylin * fd->ft->steptrac, 0)) { /* * seek command not accepted, looks like * the FDC went off to the Saints... */ fdc->retry = 6; /* try a reset */ return(retrier(fdcu)); } fd->track = FD_NO_TRACK; fdc->state = SEEKWAIT; return(0); /* will return later */ case SEEKWAIT: /* allow heads to settle */ timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16); fdc->state = SEEKCOMPLETE; return(0); /* will return later */ case SEEKCOMPLETE : /* SEEK DONE, START DMA */ /* Make sure seek really happened*/ if(fd->track == FD_NO_TRACK) { int descyl = bp->b_cylin * fd->ft->steptrac; do { /* * This might be a "ready changed" interrupt, * which cannot really happen since the * RDY pin is hardwired to + 5 volts. This * generally indicates a "bouncing" intr * line, so do one of the following: * * When running on an enhanced FDC that is * known to not go stuck after responding * with INVALID, fetch all interrupt states * until seeing either an INVALID or a * real interrupt condition. * * When running on a dumb old NE765, give * up immediately. The controller will * provide up to four dummy RC interrupt * conditions right after reset (for the * corresponding four drives), so this is * our only chance to get notice that it * was not the FDC that caused the interrupt. */ if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) return 0; if(fdc->fdct == FDC_NE765 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) return 0; /* hope for a real intr */ } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); if (0 == descyl) { int failed = 0; /* * seek to cyl 0 requested; make sure we are * really there */ if (fd_sense_drive_status(fdc, &st3)) failed = 1; if ((st3 & NE7_ST3_T0) == 0) { printf( "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", fdu, st3, NE7_ST3BITS); failed = 1; } if (failed) { if(fdc->retry < 3) fdc->retry = 3; return(retrier(fdcu)); } } if (cyl != descyl) { printf( "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", fdu, descyl, cyl, st0); return(retrier(fdcu)); } } fd->track = bp->b_cylin; if(format) fd->skip = (char *)&(finfo->fd_formb_cylno(0)) - (char *)finfo; isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip, format ? bp->b_bcount : fdblk, fdc->dmachan); blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk + fd->skip/fdblk; sectrac = fd->ft->sectrac; sec = blknum % (sectrac * fd->ft->heads); head = sec / sectrac; sec = sec % sectrac + 1; fd->hddrv = ((head&1)<<2)+fdu; if(format || !read) { /* make sure the drive is writable */ if(fd_sense_drive_status(fdc, &st3) != 0) { /* stuck controller? */ fdc->retry = 6; /* reset the beast */ return(retrier(fdcu)); } if(st3 & NE7_ST3_WP) { /* * XXX YES! this is ugly. * in order to force the current operation * to fail, we will have to fake an FDC * error - all error handling is done * by the retrier() */ fdc->status[0] = NE7_ST0_IC_AT; fdc->status[1] = NE7_ST1_NW; fdc->status[2] = 0; fdc->status[3] = fd->track; fdc->status[4] = head; fdc->status[5] = sec; fdc->retry = 8; /* break out immediately */ fdc->state = IOTIMEDOUT; /* not really... */ return (1); } } if(format) { /* formatting */ if(fd_cmd(fdcu, 6, NE7CMD_FORMAT, head << 2 | fdu, finfo->fd_formb_secshift, finfo->fd_formb_nsecs, finfo->fd_formb_gaplen, finfo->fd_formb_fillbyte, 0)) { /* controller fell over */ fdc->retry = 6; return(retrier(fdcu)); } } else { if (fd_cmd(fdcu, 9, (read ? NE7CMD_READ : NE7CMD_WRITE), head << 2 | fdu, /* head & unit */ fd->track, /* track */ head, sec, /* sector + 1 */ fd->ft->secsize, /* sector size */ sectrac, /* sectors/track */ fd->ft->gap, /* gap size */ fd->ft->datalen, /* data length */ 0)) { /* the beast is sleeping again */ fdc->retry = 6; return(retrier(fdcu)); } } fdc->state = IOCOMPLETE; timeout(fd_timeout, (caddr_t)fdcu, hz); return(0); /* will return later */ case IOCOMPLETE: /* IO DONE, post-analyze */ untimeout(fd_timeout, (caddr_t)fdcu); if (fd_read_status(fdc, fd->fdsu)) { if (fdc->retry < 6) fdc->retry = 6; /* force a reset */ return retrier(fdcu); } fdc->state = IOTIMEDOUT; /* FALLTHROUGH */ case IOTIMEDOUT: isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip, format ? bp->b_bcount : fdblk, fdc->dmachan); if (fdc->status[0] & NE7_ST0_IC) { if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) { /* * DMA overrun. Someone hogged the bus * and didn't release it in time for the * next FDC transfer. * Just restart it, don't increment retry * count. (vak) */ fdc->state = SEEKCOMPLETE; return (1); } else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV && fdc->retry < 6) fdc->retry = 6; /* force a reset */ else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC && fdc->retry < 3) fdc->retry = 3; /* force recalibrate */ return(retrier(fdcu)); } /* All OK */ fd->skip += fdblk; if (!format && fd->skip < bp->b_bcount) { /* set up next transfer */ blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk + fd->skip/fdblk; bp->b_cylin = (blknum / (fd->ft->sectrac * fd->ft->heads)); fdc->state = DOSEEK; } else { /* ALL DONE */ fd->skip = 0; bp->b_resid = 0; dp->b_actf = bp->b_actf; biodone(bp); fdc->fd = (fd_p) 0; fdc->fdu = -1; fdc->state = FINDWORK; } return(1); case RESETCTLR: fdc_reset(fdc); fdc->retry++; fdc->state = STARTRECAL; break; case STARTRECAL: if(fd_cmd(fdcu, 2, NE7CMD_RECAL, fdu, 0)) /* Recalibrate Function */ { /* arrgl */ fdc->retry = 6; return(retrier(fdcu)); } fdc->state = RECALWAIT; return(0); /* will return later */ case RECALWAIT: /* allow heads to settle */ timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8); fdc->state = RECALCOMPLETE; return(0); /* will return later */ case RECALCOMPLETE: do { /* * See SEEKCOMPLETE for a comment on this: */ if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) return 0; if(fdc->fdct == FDC_NE765 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) return 0; /* hope for a real intr */ } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) { if(fdc->retry > 3) /* * a recalibrate from beyond cylinder 77 * will "fail" due to the FDC limitations; * since people used to complain much about * the failure message, try not logging * this one if it seems to be the first * time in a line */ printf("fd%d: recal failed ST0 %b cyl %d\n", fdu, st0, NE7_ST0BITS, cyl); if(fdc->retry < 3) fdc->retry = 3; return(retrier(fdcu)); } fd->track = 0; /* Seek (probably) necessary */ fdc->state = DOSEEK; return(1); /* will return immediatly */ case MOTORWAIT: if(fd->flags & FD_MOTOR_WAIT) { return(0); /* time's not up yet */ } /* * since the controller was off, it has lost its * idea about the current track it were; thus, * recalibrate the bastard */ fdc->state = STARTRECAL; return(1); /* will return immediatly */ default: printf("fdc%d: Unexpected FD int->", fdcu); if (fd_read_status(fdc, fd->fdsu) == 0) printf("FDC status :%lx %lx %lx %lx %lx %lx %lx ", fdc->status[0], fdc->status[1], fdc->status[2], fdc->status[3], fdc->status[4], fdc->status[5], fdc->status[6] ); else printf("No status available "); if (fd_sense_int(fdc, &st0, &cyl) != 0) { printf("[controller is dead now]\n"); return(0); } printf("ST0 = %x, PCN = %x\n", st0, cyl); return(0); } /*XXX confusing: some branches return immediately, others end up here*/ return(1); /* Come back immediatly to new state */ } static int retrier(fdcu) fdcu_t fdcu; { fdc_p fdc = fdc_data + fdcu; register struct buf *dp, *bp; dp = &(fdc->head); bp = dp->b_actf; if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY) goto fail; switch(fdc->retry) { case 0: case 1: case 2: fdc->state = SEEKCOMPLETE; break; case 3: case 4: case 5: fdc->state = STARTRECAL; break; case 6: fdc->state = RESETCTLR; break; case 7: break; default: fail: { dev_t sav_b_dev = bp->b_dev; /* Trick diskerr */ bp->b_dev = makedev(major(bp->b_dev), (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART); diskerr(bp, "fd", "hard error", LOG_PRINTF, fdc->fd->skip / DEV_BSIZE, (struct disklabel *)NULL); bp->b_dev = sav_b_dev; if (fdc->flags & FDC_STAT_VALID) { printf( " (ST0 %b ST1 %b ST2 %b cyl %ld hd %ld sec %ld)\n", fdc->status[0], NE7_ST0BITS, fdc->status[1], NE7_ST1BITS, fdc->status[2], NE7_ST2BITS, fdc->status[3], fdc->status[4], fdc->status[5]); } else printf(" (No status)\n"); } bp->b_flags |= B_ERROR; bp->b_error = EIO; bp->b_resid = bp->b_bcount - fdc->fd->skip; dp->b_actf = bp->b_actf; fdc->fd->skip = 0; biodone(bp); fdc->state = FINDWORK; fdc->fd = (fd_p) 0; fdc->fdu = -1; /* XXX abort current command, if any. */ return(1); } fdc->retry++; return(1); } static int fdformat(dev, finfo, p) dev_t dev; struct fd_formb *finfo; struct proc *p; { fdu_t fdu; fd_p fd; struct buf *bp; int rv = 0, s; size_t fdblk; fdu = FDUNIT(minor(dev)); fd = &fd_data[fdu]; fdblk = 128 << fd->ft->secsize; /* set up a buffer header for fdstrategy() */ bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT); if(bp == 0) return ENOBUFS; /* * keep the process from being swapped */ p->p_flag |= P_PHYSIO; bzero((void *)bp, sizeof(struct buf)); bp->b_flags = B_BUSY | B_PHYS | B_FORMAT; bp->b_proc = p; bp->b_dev = dev; /* * calculate a fake blkno, so fdstrategy() would initiate a * seek to the requested cylinder */ bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE; bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs; bp->b_un.b_addr = (caddr_t)finfo; /* now do the format */ fdstrategy(bp); /* ...and wait for it to complete */ s = splbio(); while(!(bp->b_flags & B_DONE)) { rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz); if(rv == EWOULDBLOCK) break; } splx(s); if(rv == EWOULDBLOCK) { /* timed out */ rv = EIO; biodone(bp); } if(bp->b_flags & B_ERROR) rv = bp->b_error; /* * allow the process to be swapped */ p->p_flag &= ~P_PHYSIO; free(bp, M_TEMP); return rv; } /* * TODO: don't allocate buffer on stack. */ int fdioctl(dev, cmd, addr, flag, p) dev_t dev; int cmd; caddr_t addr; int flag; struct proc *p; { fdu_t fdu = FDUNIT(minor(dev)); fd_p fd = &fd_data[fdu]; size_t fdblk; struct fd_type *fdt; struct disklabel *dl; char buffer[DEV_BSIZE]; int error = 0; #if NFT > 0 int type = FDTYPE(minor(dev)); /* check for a tape ioctl */ if (type & F_TAPE_TYPE) return ftioctl(dev, cmd, addr, flag, p); #endif fdblk = 128 << fd->ft->secsize; switch (cmd) { case DIOCGDINFO: bzero(buffer, sizeof (buffer)); dl = (struct disklabel *)buffer; dl->d_secsize = fdblk; fdt = fd_data[FDUNIT(minor(dev))].ft; dl->d_secpercyl = fdt->size / fdt->tracks; dl->d_type = DTYPE_FLOPPY; if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl) == NULL) error = 0; else error = EINVAL; *(struct disklabel *)addr = *dl; break; case DIOCSDINFO: if ((flag & FWRITE) == 0) error = EBADF; break; case DIOCWLABEL: if ((flag & FWRITE) == 0) error = EBADF; break; case DIOCWDINFO: if ((flag & FWRITE) == 0) { error = EBADF; break; } dl = (struct disklabel *)addr; if ((error = setdisklabel((struct disklabel *)buffer, dl, (u_long)0)) != 0) break; error = writedisklabel(dev, fdstrategy, (struct disklabel *)buffer); break; case FD_FORM: if((flag & FWRITE) == 0) error = EBADF; /* must be opened for writing */ else if(((struct fd_formb *)addr)->format_version != FD_FORMAT_VERSION) error = EINVAL; /* wrong version of formatting prog */ else error = fdformat(dev, (struct fd_formb *)addr, p); break; case FD_GTYPE: /* get drive type */ *(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft; break; case FD_STYPE: /* set drive type */ /* this is considered harmful; only allow for superuser */ if(suser(p->p_ucred, &p->p_acflag) != 0) return EPERM; *fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr; break; case FD_GOPTS: /* get drive options */ *(int *)addr = fd_data[FDUNIT(minor(dev))].options; break; case FD_SOPTS: /* set drive options */ fd_data[FDUNIT(minor(dev))].options = *(int *)addr; break; default: error = ENOTTY; break; } return (error); } static fd_devsw_installed = 0; static void fd_drvinit(void *notused ) { dev_t dev; if( ! fd_devsw_installed ) { dev = makedev(CDEV_MAJOR, 0); cdevsw_add(&dev,&fd_cdevsw, NULL); dev = makedev(BDEV_MAJOR, 0); bdevsw_add(&dev,&fd_bdevsw, NULL); fd_devsw_installed = 1; } } SYSINIT(fddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,fd_drvinit,NULL) #endif /* * Hello emacs, these are the * Local Variables: * c-indent-level: 8 * c-continued-statement-offset: 8 * c-continued-brace-offset: 0 * c-brace-offset: -8 * c-brace-imaginary-offset: 0 * c-argdecl-indent: 8 * c-label-offset: -8 * c++-hanging-braces: 1 * c++-access-specifier-offset: -8 * c++-empty-arglist-indent: 8 * c++-friend-offset: 0 * End: */ Index: head/sys/miscfs/devfs/devfs_proto.h =================================================================== --- head/sys/miscfs/devfs/devfs_proto.h (revision 12690) +++ head/sys/miscfs/devfs/devfs_proto.h (revision 12691) @@ -1,72 +1,70 @@ /* THIS FILE PRODUCED AUTOMATICALLY */ void devfs_sinit(void *junk) /*proto*/; devnm_p dev_findname(dn_p dir,char *name) /*proto*/; int dev_finddir(char *orig_path, dn_p dirnode, int create, dn_p *dn_pp) /*proto*/; int dev_add_name(char *name, dn_p dirnode, devnm_p back, dn_p dnp, devnm_p *devnm_pp) /*proto*/; int dev_add_node(int entrytype, union typeinfo *by, dn_p proto, dn_p *dn_pp) /*proto*/; int dev_touch(devnm_p key) /* update the node for this dev */ /*proto*/; void devfs_dn_free(dn_p dnp) /*proto*/; int devfs_add_fronts(devnm_p parent,devnm_p child) /*proto*/; void dev_remove_dev(devnm_p devnmp) /*proto*/; int dev_dup_plane(struct devfsmount *devfs_mp_p) /*proto*/; void devfs_free_plane(struct devfsmount *devfs_mp_p) /*proto*/; int dev_dup_entry(dn_p parent, devnm_p back, devnm_p *dnm_pp, struct devfsmount *dvm) /*proto*/; void dev_free_name(devnm_p devnmp) /*proto*/; int devfs_vntodn(struct vnode *vn_p, dn_p *dn_pp) /*proto*/; int devfs_dntovn(dn_p dnp, struct vnode **vn_pp) /*proto*/; -int get_cdev_major_num(caddr_t addr) /*proto*/; -int get_bdev_major_num(caddr_t addr) /*proto*/; int dev_add_entry(char *name, dn_p parent, int type, union typeinfo *by, devnm_p *nm_pp) /*proto*/ ; int devfs_init(void) /*proto*/; int devfs_mount( struct mount *mp, char *path, caddr_t data, struct nameidata *ndp, struct proc *p) /*proto*/; int mountdevfs( struct mount *mp, struct proc *p) /*proto*/; int devfs_start(struct mount *mp, int flags, struct proc *p) /*proto*/; int devfs_unmount( struct mount *mp, int mntflags, struct proc *p) /*proto*/; int devfs_root(struct mount *mp, struct vnode **vpp) /*proto*/; int devfs_quotactl( struct mount *mp, int cmds, uid_t uid, caddr_t arg, struct proc *p) /*proto*/; int devfs_statfs( struct mount *mp, struct statfs *sbp, struct proc *p) /*proto*/; int devfs_sync(struct mount *mp, int waitfor,struct ucred *cred,struct proc *p) /*proto*/; int devfs_vget(struct mount *mp, ino_t ino,struct vnode **vpp) /*proto*/; int devfs_fhtovp (struct mount *mp, struct fid *fhp, struct mbuf *nam, struct vnode **vpp, int *exflagsp, struct ucred **credanonp) /*proto*/; int devfs_vptofh (struct vnode *vp, struct fid *fhp) /*proto*/; int devfs_lookup(struct vop_lookup_args *ap) /*proto*/; int devfs_create(struct vop_mknod_args *ap) /*proto*/; int devfs_mknod( struct vop_mknod_args *ap) /*proto*/; int devfs_open(struct vop_open_args *ap) /*proto*/; int devfs_close( struct vop_close_args *ap) /*proto*/; int devfs_access(struct vop_access_args *ap) /*proto*/; int devfs_getattr(struct vop_getattr_args *ap) /*proto*/; int devfs_setattr(struct vop_setattr_args *ap) /*proto*/; int devfs_read(struct vop_read_args *ap) /*proto*/; int devfs_write(struct vop_write_args *ap) /*proto*/; int devfs_ioctl(struct vop_ioctl_args *ap) /*proto*/; int devfs_select(struct vop_select_args *ap) /*proto*/; int devfs_mmap(struct vop_mmap_args *ap) /*proto*/; int devfs_fsync(struct vop_fsync_args *ap) /*proto*/; int devfs_seek(struct vop_seek_args *ap) /*proto*/; int devfs_remove(struct vop_remove_args *ap) /*proto*/; int devfs_link(struct vop_link_args *ap) /*proto*/; int devfs_rename(struct vop_rename_args *ap) /*proto*/; int devfs_mkdir(struct vop_mkdir_args *ap) /*proto*/; int devfs_rmdir(struct vop_rmdir_args *ap) /*proto*/; int devfs_symlink(struct vop_symlink_args *ap) /*proto*/; int devfs_readdir(struct vop_readdir_args *ap) /*proto*/; int devfs_readlink(struct vop_readlink_args *ap) /*proto*/; int devfs_abortop(struct vop_abortop_args *ap) /*proto*/; int devfs_inactive(struct vop_inactive_args *ap) /*proto*/; int devfs_lock(struct vop_lock_args *ap) /*proto*/; int devfs_unlock( struct vop_unlock_args *ap) /*proto*/; int devfs_islocked(struct vop_islocked_args *ap) /*proto*/; int devfs_bmap(struct vop_bmap_args *ap) /*proto*/; int devfs_strategy(struct vop_strategy_args *ap) /*proto*/; int devfs_advlock(struct vop_advlock_args *ap) /*proto*/; int devfs_reclaim(struct vop_reclaim_args *ap) /*proto*/; int devfs_pathconf(struct vop_pathconf_args *ap) /*proto*/; int devfs_print(struct vop_print_args *ap) /*proto*/; int devfs_vfree(struct vop_vfree_args *ap) /*proto*/; int devfs_enotsupp(void *junk) /*proto*/; int devfs_badop(void *junk) /*proto*/; int devfs_nullop(void *junk) /*proto*/; void devfs_dropvnode(dn_p dnp) /*proto*/; /* THIS FILE PRODUCED AUTOMATICALLY */ /* DO NOT EDIT (see reproto.sh) */ Index: head/sys/miscfs/devfs/devfs_tree.c =================================================================== --- head/sys/miscfs/devfs/devfs_tree.c (revision 12690) +++ head/sys/miscfs/devfs/devfs_tree.c (revision 12691) @@ -1,1029 +1,954 @@ /* * Written by Julian Elischer (julian@DIALix.oz.au) * - * $Header: /home/ncvs/src/sys/miscfs/devfs/devfs_tree.c,v 1.9 1995/11/29 10:48:36 julian Exp $ + * $Header: /u1/ncvs/src/sys/miscfs/devfs/devfs_tree.c,v 1.10 1995/12/08 11:17:33 julian Exp $ */ #include "param.h" #include "systm.h" #include "types.h" #include "kernel.h" #include "file.h" /* define FWRITE ... */ #include "conf.h" #include "stat.h" #include "mount.h" #include "vnode.h" #include "malloc.h" #include "dir.h" /* defines dirent structure */ #include "devfsdefs.h" #include "sys/devfsext.h" SYSINIT(devfs, SI_SUB_DEVFS, SI_ORDER_FIRST, devfs_sinit, NULL) devnm_p dev_root; /* root of the backing tree */ struct mount *devfs_hidden_mount; int devfs_up_and_going; /* * Set up the root directory node in the backing plane * This is happenning before the vfs system has been * set up yet, so be careful about what we reference.. * Notice that the ops are by indirection.. as they haven't * been set up yet! */ void devfs_sinit(void *junk) /*proto*/ { int retval; /* we will discard this */ devnm_p new; /* * call the right routine at the right time with the right args.... */ retval = dev_add_entry("root", NULL, DEV_DIR, NULL, &dev_root); #ifdef PARANOID if(retval) panic("devfs_sinit: dev_add_entry failed "); #endif devfs_hidden_mount = (struct mount *)malloc(sizeof(struct mount), M_MOUNT,M_NOWAIT); #ifdef PARANOID if(!devfs_hidden_mount) panic("devfs_sinit: malloc failed"); #endif bzero(devfs_hidden_mount,sizeof(struct mount)); devfs_mount(devfs_hidden_mount,"dummy",NULL,NULL,NULL); dev_root->dnp->dvm = (struct devfsmount *)devfs_hidden_mount->mnt_data; devfs_up_and_going = 1; printf("DEVFS: ready for devices\n"); } /***********************************************************************\ ************************************************************************* * Routines used to find our way to a point in the tree * ************************************************************************* \***********************************************************************/ /***************************************************************\ * Search down the linked list off a dir to find "name" * * return the dn_p for that node. \***************************************************************/ devnm_p dev_findname(dn_p dir,char *name) /*proto*/ { devnm_p newfp; DBPRINT((" dev_findname(%s)\n",name)); if(dir->type != DEV_DIR) return 0;/*XXX*/ /* printf?*/ if(name[0] == '.') { if(name[1] == 0) { return dir->by.Dir.myname; } if((name[1] == '.') && (name[2] == 0)) { /* for root, .. == . */ return dir->by.Dir.parent->by.Dir.myname; } } newfp = dir->by.Dir.dirlist; while(newfp) { if(!(strcmp(name,newfp->name))) return newfp; newfp = newfp->next; } return NULL; } /***********************************************************************\ * Given a starting node (0 for root) and a pathname, return the node * * for the end item on the path. It MUST BE A DIRECTORY. If the 'CREATE' * * option is true, then create any missing nodes in the path and create * * and return the final node as well. * * This is used to set up a directory, before making nodes in it.. * * * * Warning: This function is RECURSIVE. * * char *path, find this dir (err if not dir) * * dn_p dirnode, starting point (0 = root) * * int create, create path if not found * * dn_p *dn_pp) where to return the node of the dir * \***********************************************************************/ int dev_finddir(char *orig_path, dn_p dirnode, int create, dn_p *dn_pp) /*proto*/ { devnm_p devnmp; dn_p dnp; char pathbuf[DEVMAXPATHSIZE]; char *path; char *name; register char *cp; int retval; DBPRINT(("dev_finddir\n")); /***************************************\ * If no parent directory is given * * then start at the root of the tree * \***************************************/ if(!dirnode) dirnode = dev_root->dnp; /***************************************\ * Sanity Checks * \***************************************/ if(dirnode->type != DEV_DIR) return ENOTDIR; if(strlen(orig_path) > (DEVMAXPATHSIZE - 1)) return ENAMETOOLONG; path = pathbuf; strcpy(path,orig_path); - while(*path == '/') path++; /* always absolute, skip leading / */ /***************************************\ + * always absolute, skip leading / * + * get rid of / or // or /// etc. * + \***************************************/ + while(*path == '/') path++; + /***************************************\ + * If nothing left, then parent was it.. * + \***************************************/ + if ( *path == '\0' ) { + *dn_pp = dirnode; + return 0; + } + + /***************************************\ * find the next segment of the name * \***************************************/ cp = name = path; while((*cp != '/') && (*cp != 0)) { cp++; } + /***********************************************\ * Check to see if it's the last component * \***********************************************/ if(*cp) { path = cp + 1; /* path refers to the rest */ *cp = 0; /* name is now a separate string */ if(!(*path)) { path = (char *)0; /* was trailing slash */ } } else { path = (char *)0; /* no more to do */ } /***************************************\ * Start scanning along the linked list * \***************************************/ devnmp = dev_findname(dirnode,name); if(devnmp) { /* check it's a directory */ dnp = devnmp->dnp; if(dnp->type != DEV_DIR) return ENOTDIR; } else { /***************************************\ * The required element does not exist * * So we will add it if asked to. * \***************************************/ if(!create) return ENOENT; if(retval = dev_add_entry(name, dirnode, DEV_DIR, NULL, &devnmp)) { return retval; } dnp = devnmp->dnp; } if(path) /* decide whether to recurse more or return */ { return (dev_finddir(path,dnp,create,dn_pp)); } else { *dn_pp = dnp; return 0; } } /***********************************************************************\ * Add a new element to the devfs backing structure. * * If we're creating a root node, then dirname is NULL * * * * Creates a name node, and links it to the supplied node * \***********************************************************************/ int dev_add_name(char *name, dn_p dirnode, devnm_p back, dn_p dnp, devnm_p *devnm_pp) /*proto*/ { devnm_p devnmp; devnm_p realthing; /* needed to create an alias */ int retval; DBPRINT(("dev_add_name\n")); if(dirnode ) { if(dirnode->type != DEV_DIR) return(ENOTDIR); if( dev_findname(dirnode,name)) return(EEXIST); } /* * make sure the name is legal * slightly misleading in the case of NULL */ if( !name || (strlen(name) > (DEVMAXNAMESIZE - 1))) return (ENAMETOOLONG); /* * Allocate and fill out a new directory entry */ if(!(devnmp = (devnm_p)malloc(sizeof(devnm_t), M_DEVFSNAME, M_NOWAIT))) { return ENOMEM; } bzero(devnmp,sizeof(devnm_t)); /* inherrit our parent's mount info */ /*XXX*/ /* a kludge but.... */ if(dirnode && ( dnp->dvm == NULL)) { dnp->dvm = dirnode->dvm; if(!dnp->dvm) printf("parent had null dvm "); } /* * Link the two together * include the implicit link in the count of links to the devnode.. * this stops it from being accidentally freed later. */ devnmp->dnp = dnp; dnp->links++ ; /* implicit from our own name-node */ if(dnp->type == DEV_DIR) { dnp->by.Dir.myname = devnmp; /* * If we are unlinking from an old dir, decrement it's links * as we point our '..' elsewhere */ if(dnp->by.Dir.parent) { dnp->by.Dir.parent->links--; } if(dirnode) { dnp->by.Dir.parent = dirnode; } else { dnp->by.Dir.parent = dnp; } dnp->by.Dir.parent->links++; /* account for the new '..' */ } /* * put in it's name */ strcpy(devnmp->name,name); /* * And set up the 'clones' list (empty if new node) */ if(back) { /*******************************************************\ * Put it in the appropriate back/front list too. * \*******************************************************/ devnmp->next_front = *back->prev_frontp; devnmp->prev_frontp = back->prev_frontp; *back->prev_frontp = devnmp; back->prev_frontp = &(devnmp->next_front); devnmp->as.front.realthing = back; } else { devnmp->prev_frontp = &(devnmp->next_front); devnmp->next_front = NULL; } /******************************************* * Check if we are not making a root node.. * (i.e. have parent) */ if(dirnode) { /* * Put it on the END of the linked list of directory entries */ devnmp->parent = dirnode; /* null for root */ devnmp->prevp = dirnode->by.Dir.dirlast; devnmp->next = *(devnmp->prevp); /* should be NULL */ /*right?*/ *(devnmp->prevp) = devnmp; dirnode->by.Dir.dirlast = &(devnmp->next); dirnode->by.Dir.entrycount++; dirnode->len += strlen(name) + 8;/*ok, ok?*/ } /* * If we have a parent, then maybe we should duplicate * ourselves onto any plane that the parent is on... * Though this may be better handled elsewhere as * it stops this routine from being used for front nodes */ if(dirnode && !back) { if(retval = devfs_add_fronts(dirnode->by.Dir.myname,devnmp)) { /*XXX*//* no idea what to do if it fails... */ return retval; } } *devnm_pp = devnmp; return 0 ; } /***********************************************************************\ * Add a new element to the devfs backing structure. * * * * Creates a new dev_node to go with it * * 'by' gives us info to make our node * * note the 'links' count is 0 (except if a dir) * * but it is only cleared on a transition * * so this is ok till we link it to something * \***********************************************************************/ int dev_add_node(int entrytype, union typeinfo *by, dn_p proto, dn_p *dn_pp) /*proto*/ { dn_p dnp; int retval; DBPRINT(("dev_add_node\n")); if(!(dnp = (dn_p)malloc(sizeof(devnode_t), M_DEVFSNODE, M_NOWAIT))) { return ENOMEM; } if(proto) { /* XXX should check that we are NOT copying a device node */ bcopy(proto, dnp, sizeof(devnode_t)); /* some things you DON'T copy */ dnp->links = 0; dnp->dvm = NULL; dnp->vn = NULL; dnp->len = 0; } else { bzero(dnp,sizeof(devnode_t)); dnp->type = entrytype; TIMEVAL_TO_TIMESPEC(&time,&(dnp->ctime)) dnp->mtime = dnp->ctime; dnp->atime = dnp->ctime; } /* * fill out the dev node according to type */ switch(entrytype) { case DEV_DIR: /* * As it's a directory, make sure * it has a null entries list */ dnp->by.Dir.dirlast = &(dnp->by.Dir.dirlist); dnp->by.Dir.dirlist = (devnm_p)0; dnp->by.Dir.entrycount = 0; /* until we know better, it has a null parent pointer*/ dnp->by.Dir.parent = NULL; dnp->links++; /* for .*/ dnp->by.Dir.myname = NULL; /* * make sure that the ops associated with it are the ops * that we use (by default) for directories */ dnp->ops = &devfs_vnodeop_p; dnp->mode |= 0555; /* default perms */ break; /*******************************************************\ * The rest of these can't happen except in the back plane* \*******************************************************/ case DEV_BDEV: /* * Make sure it has DEVICE type ops * and device specific fields are correct */ dnp->ops = &dev_spec_vnodeop_p; dnp->by.Bdev.bdevsw = by->Bdev.bdevsw; dnp->by.Bdev.dev = by->Bdev.dev; break; case DEV_CDEV: /* * Make sure it has DEVICE type ops * and device specific fields are correct */ dnp->ops = &dev_spec_vnodeop_p; dnp->by.Cdev.cdevsw = by->Cdev.cdevsw; dnp->by.Cdev.dev = by->Cdev.dev; break; case DEV_DDEV: /* * store the address of (the address of) the ops * and the magic cookie to use with them */ dnp->by.Ddev.arg = by->Ddev.arg; dnp->ops = by->Ddev.ops; break; default: return EINVAL; } *dn_pp = dnp; return 0 ; } /***************************************************************\ * DEV_NODE reference count manipulations.. when a ref count * * reaches 0, the node is to be deleted * \***************************************************************/ int dev_touch(devnm_p key) /* update the node for this dev */ /*proto*/ { DBPRINT(("dev_touch\n")); TIMEVAL_TO_TIMESPEC(&time,&(key->dnp->mtime)) return 0; /*XXX*/ } void devfs_dn_free(dn_p dnp) /*proto*/ { if(--dnp->links <= 0 ) /* can be -1 for initial free, on error */ { /*probably need to do other cleanups XXX */ devfs_dropvnode(dnp); free (dnp, M_DEVFSNODE); } } /***********************************************************************\ * Front Node Operations * * Add or delete a chain of front nodes * \***********************************************************************/ /***********************************************************************\ * Given a directory backing node, and a child backing node, add the * * appropriate front nodes to the front nodes of the directory to * * represent the child node to the user * * * * on failure, front nodes will either be correct or not exist for each * * front dir, however dirs completed will not be stripped of completed * * frontnodes on failure of a later frontnode * * * * This allows a new node to be propogated through all mounted planes * * * \***********************************************************************/ int devfs_add_fronts(devnm_p parent,devnm_p child) /*proto*/ { devnm_p newnmp; devnm_p falias; dn_p dnp = child->dnp; int type = child->dnp->type; DBPRINT((" devfs_add_fronts\n")); /***********************************************\ * Find the frontnodes of the parent node * \***********************************************/ for (falias = parent->next_front; falias; falias = falias->next_front) { /* * If a Dir (XXX symlink too one day) * Make the node, using the original as a prototype) */ if(type == DEV_DIR) { if (dev_add_node(type, NULL, dnp, &dnp)) { printf("Device %s: node allocation failed\n", child->name); continue; } } if (dev_add_name(child->name,parent->dnp,child, dnp,&newnmp)) { if( type == DEV_DIR) { devfs_dn_free(dnp); /* 1->0 */ } printf("Device %s: allocation failed\n", child->name); continue; } } return 0; /* for now always succeed */ } /*********************************************************************** * remove all instances of this devicename [for backing nodes..] * note.. if there is another link to the node (non dir nodes only) * then the devfs_node will still exist as the ref count will be non-0 * removing a directory node will remove all sup-nodes on all planes (ZAP) * * Used be device drivers to remove nodes that are no longer relevant ***********************************************************************/ void dev_remove_dev(devnm_p devnmp) /*proto*/ { DBPRINT(("dev_remove_dev\n")); /* * Keep removing the next front node till no more exist */ while(devnmp->next_front) { dev_free_name(devnmp->next_front); } /* * then free the main node */ dev_free_name(devnmp); return ; } /*************************************************************** * duplicate the backing tree into a tree of nodes hung off the * mount point given as the argument. Do this by * calling dev_dup_entry which recurses all the way * up the tree.. * If we are the first plane, just return the base root **************************************************************/ int dev_dup_plane(struct devfsmount *devfs_mp_p) /*proto*/ { devnm_p new; int error = 0; DBPRINT((" dev_dup_plane\n")); if(devfs_up_and_going) { if(error = dev_dup_entry(NULL, dev_root, &new, devfs_mp_p)) { return error; } } else { /* we are doing the dummy mount during initialisation.. */ new = dev_root; } devfs_mp_p->plane_root = new; return error; } /***************************************************************\ * Free a whole plane \***************************************************************/ void devfs_free_plane(struct devfsmount *devfs_mp_p) /*proto*/ { devnm_p devnmp; DBPRINT((" devfs_free_plane\n")); devnmp = devfs_mp_p->plane_root; if(devnmp) dev_free_name(devnmp); devfs_mp_p->plane_root = NULL; } /***************************************************************\ * Create and link in a new front element.. * * Parent can be 0 for a root node * * Not presently usable to make a symlink XXX * * recursively will create subnodes corresponding to equivalent * * child nodes in the base level * \***************************************************************/ int dev_dup_entry(dn_p parent, devnm_p back, devnm_p *dnm_pp, struct devfsmount *dvm) /*proto*/ { devnm_p newnmp; struct devfsmount *dmt; devnm_p newback; devnm_p newfront; int error; dn_p dnp = back->dnp; int type = back->dnp->type; DBPRINT((" dev_dup_entry\n")); /* * go get the node made (if we need to) * use the back one as a prototype */ if ( type == DEV_DIR) { error = dev_add_node( dnp->type, NULL, dnp, &dnp); if(error) { printf("dev_dup_entry: node alloc failed\n"); return error; } } error = dev_add_name(back->name,parent,back,dnp,&newnmp); if ( error ) { if ( type == DEV_DIR) { devfs_dn_free(dnp); /* 1->0 */ } return error; } /* * If we have just made the root, then insert the pointer to the * mount information */ if(dvm) { newnmp->dnp->dvm = dvm; } /* * If it is a directory, then recurse down all the other * subnodes in it.... * note that this time we don't pass on the mount info.. */ if ( newnmp->dnp->type == DEV_DIR) { for(newback = back->dnp->by.Dir.dirlist; newback; newback = newback->next) { if(error = dev_dup_entry(newnmp->dnp, newback, &newfront, NULL)) { break; /* back out with an error */ } } } *dnm_pp = newnmp; return error; } /***************************************************************\ * Free a name node (and any below it of it's a directory node) * * remember that if there are other names pointing to the * * dev_node then it may not get freed yet * * can handle if there is no dnp * \***************************************************************/ void dev_free_name(devnm_p devnmp) /*proto*/ { dn_p parent = devnmp->parent; dn_p dnp = devnmp->dnp; devnm_p back; DBPRINT((" dev_free_name\n")); if(dnp) { if(dnp->type == DEV_DIR) { while(dnp->by.Dir.dirlist) { dev_free_name(dnp->by.Dir.dirlist); } /* * drop the reference counts on our and our parent's * nodes for "." and ".." (root has ".." -> "." ) */ devfs_dn_free(dnp); /* account for '.' */ devfs_dn_free(dnp->by.Dir.parent); /* '..' */ /* should only have one reference left (from name element) */ } devfs_dn_free(dnp); } /* * unlink ourselves from the directory on this plane */ if(parent) /* if not fs root */ { if( *devnmp->prevp = devnmp->next)/* yes, assign */ { devnmp->next->prevp = devnmp->prevp; } else { parent->by.Dir.dirlast = devnmp->prevp; } parent->by.Dir.entrycount--; parent->len -= strlen(devnmp->name) + 8; } /* * If the node has a backing pointer we need to free ourselves * from that.. * Remember that we may not HAVE a backing node. */ if (back = devnmp->as.front.realthing) /* yes an assign */ { if( *devnmp->prev_frontp = devnmp->next_front)/* yes, assign */ { devnmp->next_front->prev_frontp = devnmp->prev_frontp; } else { back->prev_frontp = devnmp->prev_frontp; } } /***************************************************************\ * If the front node has it's own devnode structure, * * then free it. * \***************************************************************/ free(devnmp,M_DEVFSNAME); return; } /*******************************************************\ ********************************************************* * ROUTINES to control the connection between devfs * * nodes and the system's vnodes * ********************************************************* \*******************************************************/ /*******************************************************\ * Theoretically this could be called for any kind of * * vnode, however in practice it must be a DEVFS vnode * \*******************************************************/ int devfs_vntodn(struct vnode *vn_p, dn_p *dn_pp) /*proto*/ { DBPRINT((" vntodn ")); if(vn_p->v_tag != VT_DEVFS) { printf("bad-tag2 "); Debugger("bad-tag "); return(EINVAL); } if(vn_p->v_usecount == 0) { printf("No references! "); } if((vn_p->v_type == VBAD) || (vn_p->v_type == VNON)) { printf("bad-type2 "); return(EINVAL); } *dn_pp = (dn_p)vn_p->v_data; return(0); } /***************************************************************\ * given a dev_node, find the appropriate vnode if one is already* * associated, or get a new one an associate it with the dev_node* * need to check about vnode references.. should we increment it?* \***************************************************************/ int devfs_dntovn(dn_p dnp, struct vnode **vn_pp) /*proto*/ { struct vnode *vn_p, *nvp; int error = 0; vn_p = dnp->vn; DBPRINT(("dntovn ")); if( vn_p) { if(vn_p->v_id != dnp->vn_id) { printf("bad-id "); goto skip; } if(vn_p->v_tag != VT_DEVFS) { printf("bad-tag "); goto skip; } if(vn_p->v_op != *(dnp->ops)) { printf("bad-ops "); goto skip; } if((dn_p)(vn_p->v_data) != dnp) { printf("bad-rev_link "); goto skip; } if(vn_p->v_type != VNON) { vget(vn_p,0/*lockflag ?*/); /*XXX*/ *vn_pp = vn_p; return(0); } else { printf("bad-type"); } skip: vn_p = (struct vnode *) 0; } if(!(error = getnewvnode(VT_DEVFS, dnp->dvm->mount, *(dnp->ops), &vn_p))) { dnp->vn = vn_p; dnp->vn_id = vn_p->v_id; *vn_pp = vn_p; DBPRINT(("(New vnode)")); switch(dnp->type) { case DEV_SLNK: break; case DEV_DIR: if(dnp->by.Dir.parent == dnp) { vn_p->v_flag |= VROOT; } vn_p->v_type = VDIR; break; case DEV_BDEV: vn_p->v_type = VBLK; if (nvp = checkalias(vn_p, dnp->by.Bdev.dev, (struct mount *)0)) { vput(vn_p); vn_p = nvp; } break; case DEV_CDEV: vn_p->v_type = VCHR; if (nvp = checkalias(vn_p, dnp->by.Cdev.dev, (struct mount *)0)) { vput(vn_p); vn_p = nvp; } break; case DEV_DDEV: break; } if ( vn_p) { vn_p->v_mount = dnp->dvm->mount;/* XXX Duplicated */ *vn_pp = vn_p; vn_p->v_data = (void *)dnp; } else { error = EINVAL; } } return error; } /***********************************************************************\ -* UTILITY routine: * -* Return the major number for the cdevsw entry containing the given * -* address. * +* add a whole device, with no prototype.. make name element and node * \***********************************************************************/ -int get_cdev_major_num(caddr_t addr) /*proto*/ -{ - int index = 0; - - DBPRINT(("get_cdev_major_num\n")); - while (index < nchrdev) - { - if(((caddr_t)(cdevsw[index].d_open) == addr) - ||((caddr_t)(cdevsw[index].d_read) == addr) - ||((caddr_t)(cdevsw[index].d_ioctl) == addr)) - { - return index; - } - index++; - } - return -1; -} - -int get_bdev_major_num(caddr_t addr) /*proto*/ -{ - int index = 0; - - DBPRINT(("get_bdev_major_num\n")); - while (index < nblkdev) - { - if(((caddr_t)(bdevsw[index].d_open) == addr) - ||((caddr_t)(bdevsw[index].d_strategy) == addr) - ||((caddr_t)(bdevsw[index].d_ioctl) == addr)) - { - return index; - } - index++; - } - return -1; -} - -/***********************************************************************\ -* add a whole device, with not prototype.. make name element and node * -\***********************************************************************/ int dev_add_entry(char *name, dn_p parent, int type, union typeinfo *by, devnm_p *nm_pp) /*proto*/ { dn_p dnp; int error = 0; DBPRINT((" devfs_add_entry\n")); if (error = dev_add_node(type, by, NULL, &dnp)) { printf("Device %s: base node allocation failed\n", name); return error; } if ( error = dev_add_name(name ,parent ,NULL, dnp, nm_pp)) { devfs_dn_free(dnp); /* 1->0 for dir, 0->(-1) for other */ printf("Device %s: name slot allocation failed\n", name); } return error; } /***********************************************************************\ * Add the named device entry into the given directory, and make it * * The appropriate type... (called (sometimes indirectly) by drivers..) * \***********************************************************************/ void *devfs_add_devsw(char *path, char *name, void *devsw, int minor, int chrblk, uid_t uid, gid_t gid, int perms) { int major; devnm_p new_dev; dn_p dnp; /* devnode for parent directory */ struct cdevsw *cd; struct bdevsw *bd; int retval; union typeinfo by; DBPRINT(("dev_add\n")); retval = dev_finddir(path,NULL,1,&dnp); if (retval) return 0; switch(chrblk) { case DV_CHR: cd = devsw; major = cd->d_maj; if ( major == -1 ) return NULL; by.Cdev.cdevsw = cd; by.Cdev.dev = makedev(major, minor); if( dev_add_entry(name, dnp, DEV_CDEV, &by,&new_dev)) return NULL; break; case DV_BLK: bd = devsw; major = bd->d_maj; if ( major == -1 ) return NULL; by.Bdev.bdevsw = bd; - by.Bdev.dev = makedev(major, minor); - if( dev_add_entry(name, dnp, DEV_BDEV, &by, &new_dev)) - return NULL; - break; - default: - return NULL; - } - new_dev->dnp->gid = gid; - new_dev->dnp->uid = uid; - new_dev->dnp->mode |= perms; - return new_dev; -} - -/***********************************************************************\ -* Add the named device entry into the given directory, and make it * -* The appropriate type... (called (sometimes indirectly) by drivers..) * -\***********************************************************************/ -void *dev_add(char *path, - char *name, - void *funct, - int minor, - int chrblk, - uid_t uid, - gid_t gid, - int perms) -{ - devnm_p new_dev; - dn_p dnp; /* devnode for parent directory */ - int retval; - int major ; - union typeinfo by; - - DBPRINT(("dev_add\n")); - retval = dev_finddir(path,NULL,1,&dnp); - if (retval) return 0; - switch(chrblk) - { - case DV_CHR: - major = get_cdev_major_num(funct); - by.Cdev.cdevsw = cdevsw + major; - by.Cdev.dev = makedev(major, minor); - if( dev_add_entry(name, dnp, DEV_CDEV, &by,&new_dev)) - return NULL; - break; - case DV_BLK: - major = get_bdev_major_num(funct); - by.Bdev.bdevsw = bdevsw + major; by.Bdev.dev = makedev(major, minor); if( dev_add_entry(name, dnp, DEV_BDEV, &by, &new_dev)) return NULL; break; default: return NULL; } new_dev->dnp->gid = gid; new_dev->dnp->uid = uid; new_dev->dnp->mode |= perms; return new_dev; } /***********************************************************************\ * Add the named device entry into the given directory, and make it * * a link to the already created device given as an arg.. * \***********************************************************************/ void *dev_link(char *path, char *name, void *original) { devnm_p new_dev; devnm_p orig = (devnm_p) original; dn_p dirnode; /* devnode for parent directory */ int retval; int major ; union typeinfo by; DBPRINT(("dev_add\n")); retval = dev_finddir(path,NULL,1,&dirnode); if (retval) return 0; /* * The DEV_CDEV below is not used other than it must NOT be DEV_DIR * the correctness of original shuold be checked.. */ if( dev_add_name(name, dirnode, NULL, orig->dnp, &new_dev)) return NULL; return new_dev; } Index: head/sys/sys/devfsext.h =================================================================== --- head/sys/sys/devfsext.h (revision 12690) +++ head/sys/sys/devfsext.h (revision 12691) @@ -1,37 +1,27 @@ /* usual BSD style copyright here */ /* Written by Julian Elischer (julian@dialix.oz.au)*/ /* - * $Id: devfsext.h,v 1.3 1995/11/29 10:49:13 julian Exp $ + * $Id: devfsext.h,v 1.4 1995/12/08 11:19:28 julian Exp $ */ #ifndef _SYS_DEVFSECT_H_ #define _SYS_DEVFSECT_H_ 1 void *devfs_add_devsw(char *path, char *name, void *devsw, - int minor, - int chrblk, - uid_t uid, - gid_t gid, - int perms) ; - -/* deprecated.. don't use.. */ -void *dev_add(char *path, - char *name, - void *funct, int minor, int chrblk, uid_t uid, gid_t gid, int perms) ; void *dev_link(char *path, char *name, void *original); /* the result of a previous dev_link or dev_add operation */ #define DV_CHR 0 #define DV_BLK 1 #define DV_DEV 2 #endif /*_SYS_DEVFSECT_H_*/