]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/9/pc/usbohci.c
kernel: cleanup makefile for $CONF.$O target
[plan9front.git] / sys / src / 9 / pc / usbohci.c
index 93428c3d2c8c945dee77356dc8d6da3e2cc8d328..d24f2388e463ca2ffa944d066054035bc7f746be 100644 (file)
@@ -37,8 +37,8 @@ enum
 {
        Incr            = 64,           /* for Td and Ed pools */
 
-       Align           = 0x20,         /* OHCI only requires 0x10 */
-                                       /* use always a power of 2 */
+       Edalign         = 0x10,
+       Tdalign         = 0x20, 
 
        Abortdelay      = 1,            /* delay after cancelling Tds (ms) */
        Tdatomic                = 8,            /* max nb. of Tds per bulk I/O op. */
@@ -350,6 +350,7 @@ struct Ctlr
        Qtree*  tree;           /* tree for t Ep i/o */
        int     ntree;          /* number of dummy Eds in tree */
        Pcidev* pcidev;
+       uintptr base;
 };
 
 #define dqprint                if(debug || io && io->debug)print
@@ -581,19 +582,20 @@ tdtok(Td *td)
 static Td*
 tdalloc(void)
 {
+       uchar *pool;
        Td *td;
-       Td *pool;
        int i;
 
        lock(&tdpool);
        if(tdpool.free == nil){
                ddprint("ohci: tdalloc %d Tds\n", Incr);
-               pool = xspanalloc(Incr*sizeof(Td), Align, 0);
+               pool = xspanalloc(Incr*ROUND(sizeof(Td), Tdalign), Tdalign, 0);
                if(pool == nil)
                        panic("ohci: tdalloc");
                for(i=Incr; --i>=0;){
-                       pool[i].next = tdpool.free;
-                       tdpool.free = &pool[i];
+                       td = (Td*)(pool + i*ROUND(sizeof(Td), Tdalign));
+                       td->next = tdpool.free;
+                       tdpool.free = td;
                }
                tdpool.nalloc += Incr;
                tdpool.nfree += Incr;
@@ -602,10 +604,9 @@ tdalloc(void)
        tdpool.nfree--;
        td = tdpool.free;
        tdpool.free = td->next;
-       memset(td, 0, sizeof(Td));
        unlock(&tdpool);
-
-       assert(((uintptr)td & 0xF) == 0);
+       assert(((uintptr)td & 0x1F) == 0);
+       memset(td, 0, sizeof(Td));
        return td;
 }
 
@@ -630,18 +631,20 @@ tdfree(Td *td)
 static Ed*
 edalloc(void)
 {
-       Ed *ed, *pool;
+       uchar *pool;
+       Ed *ed;
        int i;
 
        lock(&edpool);
        if(edpool.free == nil){
                ddprint("ohci: edalloc %d Eds\n", Incr);
-               pool = xspanalloc(Incr*sizeof(Ed), Align, 0);
+               pool = xspanalloc(Incr*ROUND(sizeof(Ed), Edalign), Edalign, 0);
                if(pool == nil)
                        panic("ohci: edalloc");
                for(i=Incr; --i>=0;){
-                       pool[i].next = edpool.free;
-                       edpool.free = &pool[i];
+                       ed = (Ed*)(pool + i*ROUND(sizeof(Ed), Edalign));
+                       ed->next = edpool.free;
+                       edpool.free = ed;
                }
                edpool.nalloc += Incr;
                edpool.nfree += Incr;
@@ -650,9 +653,9 @@ edalloc(void)
        edpool.nfree--;
        ed = edpool.free;
        edpool.free = ed->next;
-       memset(ed, 0, sizeof(Ed));
        unlock(&edpool);
-
+       assert(((uintptr)ed & 0xF) == 0);
+       memset(ed, 0, sizeof(Ed));
        return ed;
 }
 
@@ -821,7 +824,7 @@ seprinttd(char *s, char *e, Td *td, int iso)
 
        if(td == nil)
                return seprint(s, e, "<nil td>\n");
-       s = seprint(s, e, "%#p ep %#p ctrl %#p", td, td->ep, td->ctrl);
+       s = seprint(s, e, "%#p ep %#p ctrl %#lux", td, td->ep, td->ctrl);
        s = seprint(s, e, " cc=%#ulx", (td->ctrl >> Tdccshift) & Tdccmask);
        if(iso == 0){
                if((td->ctrl & Tdround) != 0)
@@ -836,7 +839,7 @@ seprinttd(char *s, char *e, Td *td, int iso)
                s = seprint(s, e, " fc=%uld", (td->ctrl >> Tdfcshift) & Tdfcmask);
                s = seprint(s, e, " sf=%uld", td->ctrl & Tdsfmask);
        }
-       s = seprint(s, e, " cbp0 %#p cbp %#p next %#p be %#p %s",
+       s = seprint(s, e, " cbp0 %#lux cbp %#lux next %#lux be %#lux %s",
                td->cbp0, td->cbp, td->nexttd, td->be, td->last ? "last" : "");
        s = seprint(s, e, "\n\t\t%ld bytes", td->nbytes);
        if((bp = td->bp) != nil){
@@ -898,7 +901,7 @@ dumped(Ed *ed)
        if(buf == nil)
                return;
        e = buf+512;
-       s = seprint(buf, e, "\ted %#p: ctrl %#p", ed, ed->ctrl);
+       s = seprint(buf, e, "\ted %#p: ctrl %#lux", ed, ed->ctrl);
        if((ed->ctrl & Edskip) != 0)
                s = seprint(s, e, " skip");
        if((ed->ctrl & Ediso) != 0)
@@ -914,7 +917,7 @@ dumped(Ed *ed)
                s = seprint(s, e, " hlt");
        s = seprint(s, e, " ep%uld.%uld", (ed->ctrl>>7)&Epmax, ed->ctrl&0x7f);
        s = seprint(s, e, " maxpkt %uld", (ed->ctrl>>Edmpsshift)&Edmpsmask);
-       seprint(s, e, " tail %#p head %#p next %#p\n",ed->tail,ed->head,ed->nexted);
+       seprint(s, e, " tail %#lux head %#lux next %#lux\n",ed->tail,ed->head,ed->nexted);
        print("%s", buf);
        free(buf);
        if(ed->tds != nil && (ed->ctrl & Ediso) == 0)
@@ -947,7 +950,7 @@ seprintep(char* s, char* e, Ep *ep)
        case Tctl:
                cio = ep->aux;
                s = seprintio(s, e, cio, "c");
-               s = seprint(s, e, "\trepl %d ndata %d\n", ep->rhrepl, cio->ndata);
+               s = seprint(s, e, "\trepl %llux ndata %d\n", ep->rhrepl, cio->ndata);
                break;
        case Tbulk:
        case Tintr:
@@ -1147,7 +1150,6 @@ qhinterrupt(Ctlr *, Ep *ep, Qio *io, Td *td, int)
 
        switch(err){
        case Tddataovr:                 /* Overrun is not an error */
-               break;
        case Tdok:
                /* virtualbox doesn't always report underflow on short packets */
                if(td->cbp == 0)
@@ -1720,7 +1722,7 @@ epctlio(Ep *ep, Ctlio *cio, void *a, long count)
        /* set the address if unset and out of configuration state */
        if(ep->dev->state != Dconfig && ep->dev->state != Dreset)
                if(cio->usbid == 0){
-                       cio->usbid = (ep->nb<<7)|(ep->dev->nb & Devmax);
+                       cio->usbid = (ep->nb&Epmax)<<7 | (ep->dev->nb&Devmax);
                        edsetaddr(cio->ed, cio->usbid);
                }
        /* adjust maxpkt if the user has learned a different one */
@@ -1987,7 +1989,7 @@ isoopen(Ctlr *ctlr, Ep *ep)
        int i;
 
        iso = ep->aux;
-       iso->usbid = (ep->nb<<7)|(ep->dev->nb & Devmax);
+       iso->usbid = (ep->nb&Epmax)<<7 | (ep->dev->nb&Devmax);
        iso->bw = ep->hz * ep->samplesz;        /* bytes/sec */
        if(ep->mode != OWRITE){
                print("ohci: bug: iso input streams not implemented\n");
@@ -2068,7 +2070,7 @@ epopen(Ep *ep)
        case Tintr:
                io = ep->aux = smalloc(sizeof(Qio)*2);
                io[OREAD].debug = io[OWRITE].debug = ep->debug;
-               usbid = (ep->nb<<7)|(ep->dev->nb & Devmax);
+               usbid = (ep->nb&Epmax)<<7 | (ep->dev->nb&Devmax);
                if(ep->mode != OREAD){
                        if(ep->toggle[OWRITE] != 0)
                                io[OWRITE].toggle = Tddata1;
@@ -2377,7 +2379,7 @@ init(Hci *hp)
 static void
 scanpci(void)
 {
-       ulong mem;
+       uintptr io;
        Ctlr *ctlr;
        Pcidev *p;
        int i;
@@ -2391,27 +2393,26 @@ scanpci(void)
                /*
                 * Find Ohci controllers (Programming Interface = 0x10).
                 */
-               if(p->ccrb != Pcibcserial || p->ccru != Pciscusb ||
-                   p->ccrp != 0x10)
+               if(p->ccrb != Pcibcserial || p->ccru != Pciscusb || p->ccrp != 0x10)
                        continue;
-               mem = p->mem[0].bar & ~0x0F;
-               dprint("ohci: %x/%x port 0x%lux size 0x%x irq %d\n",
-                       p->vid, p->did, mem, p->mem[0].size, p->intl);
-               if(mem == 0){
-                       print("ohci: failed to map registers\n");
+               io = p->mem[0].bar & ~0x0F;
+               if(io == 0)
                        continue;
-               }
-
+               print("usbohci: %#x %#x: port %#p size %#x irq %d\n",
+                       p->vid, p->did, io, p->mem[0].size, p->intl);
                ctlr = malloc(sizeof(Ctlr));
                if(ctlr == nil){
                        print("ohci: no memory\n");
                        continue;
                }
+               if((ctlr->ohci = vmap(io, p->mem[0].size)) == nil){
+                       print("ohci: can't map ohci\n");
+                       free(ctlr);
+                       continue;
+               }
                ctlr->pcidev = p;
-               ctlr->ohci = vmap(mem, p->mem[0].size);
+               ctlr->base = io;
                dprint("scanpci: ctlr %#p, ohci %#p\n", ctlr, ctlr->ohci);
-               pcisetbme(p);
-               pcisetpms(p, 0);
                for(i = 0; i < Nhcis; i++)
                        if(ctlrs[i] == nil){
                                ctlrs[i] = ctlr;
@@ -2570,7 +2571,7 @@ reset(Hci *hp)
        for(i = 0; i < Nhcis && ctlrs[i] != nil; i++){
                ctlr = ctlrs[i];
                if(ctlr->active == 0)
-               if(hp->port == 0 || hp->port == (uintptr)ctlr->ohci){
+               if(hp->port == 0 || hp->port == ctlr->base){
                        ctlr->active = 1;
                        break;
                }
@@ -2578,13 +2579,17 @@ reset(Hci *hp)
        iunlock(&resetlck);
        if(ctlrs[i] == nil || i == Nhcis)
                return -1;
-       if(ctlr->ohci->control == ~0)
-               return -1;
-
 
        p = ctlr->pcidev;
+       pcienable(p);
+
+       if(ctlr->ohci->control == ~0){
+               pcidisable(p);
+               return -1;
+       }
+
        hp->aux = ctlr;
-       hp->port = (uintptr)ctlr->ohci;
+       hp->port = ctlr->base;
        hp->irq = p->intl;
        hp->tbdf = p->tbdf;
        ctlr->nports = hp->nports = ctlr->ohci->rhdesca & 0xff;
@@ -2592,6 +2597,8 @@ reset(Hci *hp)
        ohcireset(ctlr);
        ohcimeminit(ctlr);
 
+       pcisetbme(p);
+
        /*
         * Linkage to the generic HCI driver.
         */
@@ -2609,6 +2616,8 @@ reset(Hci *hp)
        hp->shutdown = shutdown;
        hp->debug = usbdebug;
        hp->type = "ohci";
+       intrenable(hp->irq, hp->interrupt, hp, hp->tbdf, hp->type);
+
        return 0;
 }