/* * This file is part of the UCB release of Plan 9. It is subject to the license * terms in the LICENSE file found in the top-level directory of this * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No * part of the UCB release of Plan 9, including this file, may be copied, * modified, propagated, or distributed except according to the terms contained * in the LICENSE file. */ #include "u.h"#include "../port/lib.h"#include "mem.h"#include "dat.h"#include "fns.h"#include "io.h"#include "../port/error.h" #include "floppy.h" /* Intel 82077A (8272A compatible) floppy controller */ /* This module expects the following functions to be defined * elsewhere: * * inb() * outb() * floppyexec() * floppyeject() * floppysetup0() * floppysetup1() * dmainit() * dmasetup() * dmaend() * * On DMA systems, floppyexec() should be an empty function; * on non-DMA systems, dmaend() should be an empty function; * dmasetup() may enforce maximum transfer sizes. */ enum { /* file types */ Qdir= 0, Qdata= (1<<2), Qctl= (2<<2), Qmask= (3<<2), DMAchan= 2, /* floppy dma channel */}; #define DPRINT if(floppydebug)printint floppydebug = 0; /* * types of drive (from PC equipment byte) */enum{ Tnone= 0, T360kb= 1, T1200kb= 2, T720kb= 3, T1440kb= 4,}; FType floppytype[] ={ { "3½HD", T1440kb, 512, 18, 2, 1, 80, 0x1B, 0x54, 0, }, { "3½DD", T1440kb, 512, 9, 2, 1, 80, 0x1B, 0x54, 2, }, { "3½DD", T720kb, 512, 9, 2, 1, 80, 0x1B, 0x54, 2, }, { "5¼HD", T1200kb, 512, 15, 2, 1, 80, 0x2A, 0x50, 0, }, { "5¼DD", T1200kb, 512, 9, 2, 2, 40, 0x2A, 0x50, 1, }, { "ATT3B1", T1200kb, 512, 8, 2, 2, 48, 0x2A, 0x50, 1, }, { "5¼DD", T360kb, 512, 9, 2, 1, 40, 0x2A, 0x50, 2, },}; /* * bytes per sector encoding for the controller. * - index for b2c is is (bytes per sector/128). * - index for c2b is code from b2c */static int b2c[] ={[1] 0,[2] 1,[4] 2,[8] 3,};static int c2b[] ={ 128, 256, 512, 1024,}; FController fl; #define MOTORBIT(i) (1<<((i)+4)) /* * predeclared */static int cmddone(void*);static void floppyformat(FDrive*, Cmdbuf*);static void floppykproc(void*);static void floppypos(FDrive*,long);static int floppyrecal(FDrive*);static int floppyresult(void);static void floppyrevive(void);static long floppyseek(FDrive*, long);static int floppysense(void);static void floppywait(int);static long floppyxfer(FDrive*, int, void*, long, long); Dirtab floppydir[]={ ".", {Qdir, 0, QTDIR}, 0, 0550, "fd0disk", {Qdata + 0}, 0, 0660, "fd0ctl", {Qctl + 0}, 0, 0660, "fd1disk", {Qdata + 1}, 0, 0660, "fd1ctl", {Qctl + 1}, 0, 0660, "fd2disk", {Qdata + 2}, 0, 0660, "fd2ctl", {Qctl + 2}, 0, 0660, "fd3disk", {Qdata + 3}, 0, 0660, "fd3ctl", {Qctl + 3}, 0, 0660,};#define NFDIR 2 /* directory entries/drive */ enum{ CMdebug, CMnodebug, CMeject, CMformat, CMreset,}; static Cmdtab floppyctlmsg[] ={ CMdebug, "debug", 1, CMnodebug, "nodebug", 1, CMeject, "eject", 1, CMformat, "format", 0, CMreset, "reset", 1,}; static voidfldump(void){ DPRINT("sra %ux srb %ux dor %ux msr %ux dir %ux\n", inb(Psra), inb(Psrb), inb(Pdor), inb(Pmsr), inb(Pdir));} /* * set floppy drive to its default type */static voidfloppysetdef(FDrive *dp){ FType *t; for(t = floppytype; t < &floppytype[nelem(floppytype)]; t++) if(dp->dt == t->dt){ dp->t = t; floppydir[1+NFDIR*dp->dev].length = dp->t->cap; break; }} static voidfloppyreset(void){ FDrive *dp; FType *t; ulong maxtsize; floppysetup0(&fl); if(fl.ndrive == 0) return; /* * init dependent parameters */ maxtsize = 0; for(t = floppytype; t < &floppytype[nelem(floppytype)]; t++){ t->cap = t->bytes * t->heads * t->sectors * t->tracks; t->bcode = b2c[t->bytes/128]; t->tsize = t->bytes * t->sectors; if(maxtsize < t->tsize) maxtsize = t->tsize; } /* * Should check if this fails. Can do so * if there is no space <= 16MB for the DMA * bounce buffer. */ dmainit(DMAchan, maxtsize); /* * allocate the drive storage */ fl.d = xalloc(fl.ndrive*sizeof(FDrive)); fl.selected = fl.d; /* * stop the motors */ fl.motor = 0; delay(10); outb(Pdor, fl.motor | Fintena | Fena); delay(10); /* * init drives */ for(dp = fl.d; dp < &fl.d[fl.ndrive]; dp++){ dp->dev = dp - fl.d; dp->dt = T1440kb; floppysetdef(dp); dp->cyl = -1; /* because we don't know */ dp->cache = (uchar*)xspanalloc(maxtsize, BY2PG, 64*1024); dp->ccyl = -1; dp->vers = 0; } /* * first operation will recalibrate */ fl.confused = 1; floppysetup1(&fl);} static Chan*floppyattach(char *spec){ static int kstarted; if(fl.ndrive == 0) error(Enodev); if(kstarted == 0){ /* * watchdog to turn off the motors */ kstarted = 1; kproc("floppy", floppykproc, 0); } return devattach('f', spec);} static Walkqid*floppywalk(Chan *c, Chan *nc, char **name, int nname){ return devwalk(c, nc, name, nname, floppydir, 1+fl.ndrive*NFDIR, devgen);} static intfloppystat(Chan *c, uchar *dp, int n){ return devstat(c, dp, n, floppydir, 1+fl.ndrive*NFDIR, devgen);} static Chan*floppyopen(Chan *c, int omode){ return devopen(c, omode, floppydir, 1+fl.ndrive*NFDIR, devgen);} static voidfloppyclose(Chan *){} static voidislegal(ulong offset, long n, FDrive *dp){ if(offset % dp->t->bytes) error(Ebadarg); if(n % dp->t->bytes) error(Ebadarg);} /* * check if the floppy has been replaced under foot. cause * an error if it has. * * a seek and a read clears the condition. this was determined * experimentally, there has to be a better way. * * if the read fails, cycle through the possible floppy * density till one works or we've cycled through all * possibilities for this drive. */static voidchanged(Chan *c, FDrive *dp){ ulong old; FType *start; /* * if floppy has changed or first time through */ if((inb(Pdir)&Fchange) || dp->vers == 0){ DPRINT("changed\n"); fldump(); dp->vers++; start = dp->t; dp->maxtries = 3; /* limit it when we're probing */ /* floppyon will fail if there's a controller but no drive */ dp->confused = 1; /* make floppyon recal */ if(floppyon(dp) < 0) error(Eio); /* seek to the first track */ floppyseek(dp, dp->t->heads*dp->t->tsize); while(waserror()){ /* * if first attempt doesn't reset changed bit, there's * no floppy there */ if(inb(Pdir)&Fchange) nexterror(); while(++dp->t){ if(dp->t == &floppytype[nelem(floppytype)]) dp->t = floppytype; if(dp->dt == dp->t->dt) break; } floppydir[1+NFDIR*dp->dev].length = dp->t->cap; /* floppyon will fail if there's a controller but no drive */ if(floppyon(dp) < 0) error(Eio); DPRINT("changed: trying %s\n", dp->t->name); fldump(); if(dp->t == start) nexterror(); } /* if the read succeeds, we've got the density right */ floppyxfer(dp, Fread, dp->cache, 0, dp->t->tsize); poperror(); dp->maxtries = 20; } old = c->qid.vers; c->qid.vers = dp->vers; if(old && old != dp->vers) error(Eio);} static intreadtrack(FDrive *dp, int cyl, int head){ int i, nn, sofar; ulong pos; nn = dp->t->tsize; if(dp->ccyl==cyl && dp->chead==head) return nn; pos = (cyl*dp->t->heads+head) * nn; for(sofar = 0; sofar < nn; sofar += i){ dp->ccyl = -1; i = floppyxfer(dp, Fread, dp->cache + sofar, pos + sofar, nn - sofar); if(i <= 0) return -1; } dp->ccyl = cyl; dp->chead = head; return nn;} static longfloppyread(Chan *c, void *a, long n, vlong off){ FDrive *dp; long rv; int sec, head, cyl; long len; uchar *aa; ulong offset = off; if(c->qid.type & QTDIR) return devdirread(c, a, n, floppydir, 1+fl.ndrive*NFDIR, devgen); rv = 0; dp = &fl.d[c->qid.path & ~Qmask]; switch ((int)(c->qid.path & Qmask)) { case Qdata: islegal(offset, n, dp); aa = a; qlock(&fl); if(waserror()){ qunlock(&fl); nexterror(); } floppyon(dp); changed(c, dp); for(rv = 0; rv < n; rv += len){ /* * all xfers come out of the track cache */ dp->len = n - rv; floppypos(dp, offset+rv); cyl = dp->tcyl; head = dp->thead; len = dp->len; sec = dp->tsec; if(readtrack(dp, cyl, head) < 0) break; memmove(aa+rv, dp->cache + (sec-1)*dp->t->bytes, len); } qunlock(&fl); poperror(); break; case Qctl: return readstr(offset, a, n, dp->t->name); default: panic("floppyread: bad qid"); } return rv;} static longfloppywrite(Chan *c, void *a, long n, vlong off){ FDrive *dp; long rv, i; char *aa = a; Cmdbuf *cb; Cmdtab *ct; ulong offset = off; rv = 0; dp = &fl.d[c->qid.path & ~Qmask]; switch ((int)(c->qid.path & Qmask)) { case Qdata: islegal(offset, n, dp); qlock(&fl); if(waserror()){ qunlock(&fl); nexterror(); } floppyon(dp); changed(c, dp); for(rv = 0; rv < n; rv += i){ floppypos(dp, offset+rv); if(dp->tcyl == dp->ccyl) dp->ccyl = -1; i = floppyxfer(dp, Fwrite, aa+rv, offset+rv, n-rv); if(i < 0) break; if(i == 0) error(Eio); } qunlock(&fl); poperror(); break; case Qctl: rv = n; cb = parsecmd(a, n); if(waserror()){ free(cb); nexterror(); } qlock(&fl); if(waserror()){ qunlock(&fl); nexterror(); } ct = lookupcmd(cb, floppyctlmsg, nelem(floppyctlmsg)); switch(ct->index){ case CMeject: floppyeject(dp); break; case CMformat: floppyformat(dp, cb); break; case CMreset: fl.confused = 1; floppyon(dp); break; case CMdebug: floppydebug = 1; break; case CMnodebug: floppydebug = 0; break; } poperror(); qunlock(&fl); poperror(); free(cb);Lines 1–500 of 1096. Load more
/* * This file is part of the UCB release of Plan 9. It is subject to the license * terms in the LICENSE file found in the top-level directory of this * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No * part of the UCB release of Plan 9, including this file, may be copied, * modified, propagated, or distributed except according to the terms contained * in the LICENSE file. */ #include "u.h" #include "../port/lib.h" #include "mem.h" #include "dat.h" #include "fns.h" #include "io.h" #include "../port/error.h" #include "floppy.h" /* Intel 82077A (8272A compatible) floppy controller */ /* This module expects the following functions to be defined * elsewhere: * * inb() * outb() * floppyexec() * floppyeject() * floppysetup0() * floppysetup1() * dmainit() * dmasetup() * dmaend() * * On DMA systems, floppyexec() should be an empty function; * on non-DMA systems, dmaend() should be an empty function; * dmasetup() may enforce maximum transfer sizes. */ enum { /* file types */ Qdir= 0, Qdata= (1<<2), Qctl= (2<<2), Qmask= (3<<2), DMAchan= 2, /* floppy dma channel */ }; #define DPRINT if(floppydebug)print int floppydebug = 0; /* * types of drive (from PC equipment byte) */ enum { Tnone= 0, T360kb= 1, T1200kb= 2, T720kb= 3, T1440kb= 4, }; FType floppytype[] = { { "3½HD", T1440kb, 512, 18, 2, 1, 80, 0x1B, 0x54, 0, }, { "3½DD", T1440kb, 512, 9, 2, 1, 80, 0x1B, 0x54, 2, }, { "3½DD", T720kb, 512, 9, 2, 1, 80, 0x1B, 0x54, 2, }, { "5¼HD", T1200kb, 512, 15, 2, 1, 80, 0x2A, 0x50, 0, }, { "5¼DD", T1200kb, 512, 9, 2, 2, 40, 0x2A, 0x50, 1, }, { "ATT3B1", T1200kb, 512, 8, 2, 2, 48, 0x2A, 0x50, 1, }, { "5¼DD", T360kb, 512, 9, 2, 1, 40, 0x2A, 0x50, 2, }, }; /* * bytes per sector encoding for the controller. * - index for b2c is is (bytes per sector/128). * - index for c2b is code from b2c */ static int b2c[] = { [1] 0, [2] 1, [4] 2, [8] 3, }; static int c2b[] = { 128, 256, 512, 1024, }; FController fl; #define MOTORBIT(i) (1<<((i)+4)) /* * predeclared */ static int cmddone(void*); static void floppyformat(FDrive*, Cmdbuf*); static void floppykproc(void*); static void floppypos(FDrive*,long); static int floppyrecal(FDrive*); static int floppyresult(void); static void floppyrevive(void); static long floppyseek(FDrive*, long); static int floppysense(void); static void floppywait(int); static long floppyxfer(FDrive*, int, void*, long, long); Dirtab floppydir[]={ ".", {Qdir, 0, QTDIR}, 0, 0550, "fd0disk", {Qdata + 0}, 0, 0660, "fd0ctl", {Qctl + 0}, 0, 0660, "fd1disk", {Qdata + 1}, 0, 0660, "fd1ctl", {Qctl + 1}, 0, 0660, "fd2disk", {Qdata + 2}, 0, 0660, "fd2ctl", {Qctl + 2}, 0, 0660, "fd3disk", {Qdata + 3}, 0, 0660, "fd3ctl", {Qctl + 3}, 0, 0660, }; #define NFDIR 2 /* directory entries/drive */ enum { CMdebug, CMnodebug, CMeject, CMformat, CMreset, }; static Cmdtab floppyctlmsg[] = { CMdebug, "debug", 1, CMnodebug, "nodebug", 1, CMeject, "eject", 1, CMformat, "format", 0, CMreset, "reset", 1, }; static void fldump(void) { DPRINT("sra %ux srb %ux dor %ux msr %ux dir %ux\n", inb(Psra), inb(Psrb), inb(Pdor), inb(Pmsr), inb(Pdir)); } /* * set floppy drive to its default type */ static void floppysetdef(FDrive *dp) { FType *t; for(t = floppytype; t < &floppytype[nelem(floppytype)]; t++) if(dp->dt == t->dt){ dp->t = t; floppydir[1+NFDIR*dp->dev].length = dp->t->cap; break; } } static void floppyreset(void) { FDrive *dp; FType *t; ulong maxtsize; floppysetup0(&fl); if(fl.ndrive == 0) return; /* * init dependent parameters */ maxtsize = 0; for(t = floppytype; t < &floppytype[nelem(floppytype)]; t++){ t->cap = t->bytes * t->heads * t->sectors * t->tracks; t->bcode = b2c[t->bytes/128]; t->tsize = t->bytes * t->sectors; if(maxtsize < t->tsize) maxtsize = t->tsize; } /* * Should check if this fails. Can do so * if there is no space <= 16MB for the DMA * bounce buffer. */ dmainit(DMAchan, maxtsize); /* * allocate the drive storage */ fl.d = xalloc(fl.ndrive*sizeof(FDrive)); fl.selected = fl.d; /* * stop the motors */ fl.motor = 0; delay(10); outb(Pdor, fl.motor | Fintena | Fena); delay(10); /* * init drives */ for(dp = fl.d; dp < &fl.d[fl.ndrive]; dp++){ dp->dev = dp - fl.d; dp->dt = T1440kb; floppysetdef(dp); dp->cyl = -1; /* because we don't know */ dp->cache = (uchar*)xspanalloc(maxtsize, BY2PG, 64*1024); dp->ccyl = -1; dp->vers = 0; } /* * first operation will recalibrate */ fl.confused = 1; floppysetup1(&fl); } static Chan* floppyattach(char *spec) { static int kstarted; if(fl.ndrive == 0) error(Enodev); if(kstarted == 0){ /* * watchdog to turn off the motors */ kstarted = 1; kproc("floppy", floppykproc, 0); } return devattach('f', spec); } static Walkqid* floppywalk(Chan *c, Chan *nc, char **name, int nname) { return devwalk(c, nc, name, nname, floppydir, 1+fl.ndrive*NFDIR, devgen); } static int floppystat(Chan *c, uchar *dp, int n) { return devstat(c, dp, n, floppydir, 1+fl.ndrive*NFDIR, devgen); } static Chan* floppyopen(Chan *c, int omode) { return devopen(c, omode, floppydir, 1+fl.ndrive*NFDIR, devgen); } static void floppyclose(Chan *) { } static void islegal(ulong offset, long n, FDrive *dp) { if(offset % dp->t->bytes) error(Ebadarg); if(n % dp->t->bytes) error(Ebadarg); } /* * check if the floppy has been replaced under foot. cause * an error if it has. * * a seek and a read clears the condition. this was determined * experimentally, there has to be a better way. * * if the read fails, cycle through the possible floppy * density till one works or we've cycled through all * possibilities for this drive. */ static void changed(Chan *c, FDrive *dp) { ulong old; FType *start; /* * if floppy has changed or first time through */ if((inb(Pdir)&Fchange) || dp->vers == 0){ DPRINT("changed\n"); fldump(); dp->vers++; start = dp->t; dp->maxtries = 3; /* limit it when we're probing */ /* floppyon will fail if there's a controller but no drive */ dp->confused = 1; /* make floppyon recal */ if(floppyon(dp) < 0) error(Eio); /* seek to the first track */ floppyseek(dp, dp->t->heads*dp->t->tsize); while(waserror()){ /* * if first attempt doesn't reset changed bit, there's * no floppy there */ if(inb(Pdir)&Fchange) nexterror(); while(++dp->t){ if(dp->t == &floppytype[nelem(floppytype)]) dp->t = floppytype; if(dp->dt == dp->t->dt) break; } floppydir[1+NFDIR*dp->dev].length = dp->t->cap; /* floppyon will fail if there's a controller but no drive */ if(floppyon(dp) < 0) error(Eio); DPRINT("changed: trying %s\n", dp->t->name); fldump(); if(dp->t == start) nexterror(); } /* if the read succeeds, we've got the density right */ floppyxfer(dp, Fread, dp->cache, 0, dp->t->tsize); poperror(); dp->maxtries = 20; } old = c->qid.vers; c->qid.vers = dp->vers; if(old && old != dp->vers) error(Eio); } static int readtrack(FDrive *dp, int cyl, int head) { int i, nn, sofar; ulong pos; nn = dp->t->tsize; if(dp->ccyl==cyl && dp->chead==head) return nn; pos = (cyl*dp->t->heads+head) * nn; for(sofar = 0; sofar < nn; sofar += i){ dp->ccyl = -1; i = floppyxfer(dp, Fread, dp->cache + sofar, pos + sofar, nn - sofar); if(i <= 0) return -1; } dp->ccyl = cyl; dp->chead = head; return nn; } static long floppyread(Chan *c, void *a, long n, vlong off) { FDrive *dp; long rv; int sec, head, cyl; long len; uchar *aa; ulong offset = off; if(c->qid.type & QTDIR) return devdirread(c, a, n, floppydir, 1+fl.ndrive*NFDIR, devgen); rv = 0; dp = &fl.d[c->qid.path & ~Qmask]; switch ((int)(c->qid.path & Qmask)) { case Qdata: islegal(offset, n, dp); aa = a; qlock(&fl); if(waserror()){ qunlock(&fl); nexterror(); } floppyon(dp); changed(c, dp); for(rv = 0; rv < n; rv += len){ /* * all xfers come out of the track cache */ dp->len = n - rv; floppypos(dp, offset+rv); cyl = dp->tcyl; head = dp->thead; len = dp->len; sec = dp->tsec; if(readtrack(dp, cyl, head) < 0) break; memmove(aa+rv, dp->cache + (sec-1)*dp->t->bytes, len); } qunlock(&fl); poperror(); break; case Qctl: return readstr(offset, a, n, dp->t->name); default: panic("floppyread: bad qid"); } return rv; } static long floppywrite(Chan *c, void *a, long n, vlong off) { FDrive *dp; long rv, i; char *aa = a; Cmdbuf *cb; Cmdtab *ct; ulong offset = off; rv = 0; dp = &fl.d[c->qid.path & ~Qmask]; switch ((int)(c->qid.path & Qmask)) { case Qdata: islegal(offset, n, dp); qlock(&fl); if(waserror()){ qunlock(&fl); nexterror(); } floppyon(dp); changed(c, dp); for(rv = 0; rv < n; rv += i){ floppypos(dp, offset+rv); if(dp->tcyl == dp->ccyl) dp->ccyl = -1; i = floppyxfer(dp, Fwrite, aa+rv, offset+rv, n-rv); if(i < 0) break; if(i == 0) error(Eio); } qunlock(&fl); poperror(); break; case Qctl: rv = n; cb = parsecmd(a, n); if(waserror()){ free(cb); nexterror(); } qlock(&fl); if(waserror()){ qunlock(&fl); nexterror(); } ct = lookupcmd(cb, floppyctlmsg, nelem(floppyctlmsg)); switch(ct->index){ case CMeject: floppyeject(dp); break; case CMformat: floppyformat(dp, cb); break; case CMreset: fl.confused = 1; floppyon(dp); break; case CMdebug: floppydebug = 1; break; case CMnodebug: floppydebug = 0; break; } poperror(); qunlock(&fl); poperror(); free(cb);