Replace USB encoded 'u32 endp' scheme with explicit struct fields.
authorKevin O'Connor <kevin@koconnor.net>
Sun, 28 Feb 2010 07:17:28 +0000 (02:17 -0500)
committerKevin O'Connor <kevin@koconnor.net>
Wed, 10 Mar 2010 00:59:10 +0000 (19:59 -0500)
Expand 'struct usb_pipe' with all the fields that are needed from the
encoded 'u32 endp' field.

src/usb-hid.c
src/usb-hub.c
src/usb-msc.c
src/usb-ohci.c
src/usb-ohci.h
src/usb-uhci.c
src/usb-uhci.h
src/usb.c
src/usb.h

index eae68235191893197cb872774bbc5ada025ec4f9..67b5e5464673d37d5a9043250fc74503f85a168f 100644 (file)
@@ -53,7 +53,7 @@ usb_keyboard_init(struct usb_pipe *pipe
     if (keyboard_pipe)
         // XXX - this enables the first found keyboard (could be random)
         return -1;
-    dprintf(2, "usb_keyboard_setup %x\n", pipe->endp);
+    dprintf(2, "usb_keyboard_setup %p\n", pipe);
 
     // Find intr in endpoint.
     struct usb_endpoint_descriptor *epdesc = findEndPointDesc(
@@ -72,8 +72,7 @@ usb_keyboard_init(struct usb_pipe *pipe
     if (ret)
         return -1;
 
-    u32 inendp = mkendpFromDesc(pipe, epdesc);
-    keyboard_pipe = alloc_intr_pipe(inendp, epdesc->bInterval);
+    keyboard_pipe = alloc_intr_pipe(pipe, epdesc);
     if (!keyboard_pipe)
         return -1;
 
index da7d9f8b3fec161074602250ff186497570d239d..ce40099cd32564e505ff6afef7996f2935a9c9d6 100644 (file)
@@ -168,7 +168,7 @@ usb_hub_init(struct usb_pipe *pipe)
     struct usbhub_s hub;
     memset(&hub, 0, sizeof(hub));
     hub.pipe = pipe;
-    hub.cntl = endp2cntl(pipe->endp);
+    hub.cntl = pipe->cntl;
     hub.powerwait = desc.bPwrOn2PwrGood * 2;
 
     // Launch a thread for every port.
index cb60c3e22a4ac84a1caf2dacf3a187e7fea3e3a9..a7047fd253e1cf88e9b0e9daacacf74e1b6e1d98 100644 (file)
@@ -194,10 +194,8 @@ usb_msc_init(struct usb_pipe *pipe
         iface, imax, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
     if (!indesc || !outdesc)
         goto fail;
-    u32 inendp = mkendpFromDesc(pipe, indesc);
-    struct usb_pipe *bulkin = alloc_bulk_pipe(inendp);
-    u32 outendp = mkendpFromDesc(pipe, outdesc);
-    struct usb_pipe *bulkout = alloc_bulk_pipe(outendp);
+    struct usb_pipe *bulkin = alloc_bulk_pipe(pipe, indesc);
+    struct usb_pipe *bulkout = alloc_bulk_pipe(pipe, outdesc);
     if (!bulkin || !bulkout)
         goto fail;
 
index 3e94de6510860284edf06e803c0d616b207d6df1..d825964007aefc2e38046b9c51ec68edc2738fd9 100644 (file)
@@ -300,10 +300,9 @@ ohci_free_pipe(struct usb_pipe *p)
 {
     if (! CONFIG_USB_OHCI)
         return;
+    dprintf(7, "ohci_free_pipe %p\n", p);
     struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
-    u32 endp = pipe->pipe.endp;
-    dprintf(7, "ohci_free_pipe %x\n", endp);
-    struct usb_s *cntl = endp2cntl(endp);
+    struct usb_s *cntl = pipe->pipe.cntl;
 
     u32 *pos = &cntl->ohci.regs->ed_controlhead;
     for (;;) {
@@ -324,12 +323,12 @@ ohci_free_pipe(struct usb_pipe *p)
 }
 
 struct usb_pipe *
-ohci_alloc_control_pipe(u32 endp)
+ohci_alloc_control_pipe(struct usb_pipe *dummy)
 {
     if (! CONFIG_USB_OHCI)
         return NULL;
-    struct usb_s *cntl = endp2cntl(endp);
-    dprintf(7, "ohci_alloc_control_pipe %x\n", endp);
+    struct usb_s *cntl = dummy->cntl;
+    dprintf(7, "ohci_alloc_control_pipe %p\n", cntl);
 
     // Allocate a queue head.
     struct ohci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
@@ -339,7 +338,7 @@ ohci_alloc_control_pipe(u32 endp)
     }
     memset(pipe, 0, sizeof(*pipe));
     pipe->ed.hwINFO = ED_SKIP;
-    pipe->pipe.endp = endp;
+    memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
 
     // Add queue head to controller list.
     pipe->ed.hwNextED = cntl->ohci.regs->ed_controlhead;
@@ -354,18 +353,17 @@ ohci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
 {
     if (! CONFIG_USB_OHCI)
         return -1;
+    dprintf(5, "ohci_control %p\n", p);
     if (datasize > 4096) {
         // XXX - should support larger sizes.
         warn_noalloc();
         return -1;
     }
     struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
-    u32 endp = pipe->pipe.endp;
-    dprintf(5, "ohci_control %x\n", endp);
-    struct usb_s *cntl = endp2cntl(endp);
-    int maxpacket = endp2maxsize(endp);
-    int lowspeed = endp2speed(endp);
-    int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
+    struct usb_s *cntl = pipe->pipe.cntl;
+    int maxpacket = pipe->pipe.maxpacket;
+    int lowspeed = pipe->pipe.lowspeed;
+    int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
 
     // Setup transfer descriptors
     struct ohci_td *tds = malloc_tmphigh(sizeof(*tds) * 3);
@@ -400,18 +398,18 @@ ohci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
 }
 
 struct usb_pipe *
-ohci_alloc_intr_pipe(u32 endp, int frameexp)
+ohci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp)
 {
     if (! CONFIG_USB_OHCI)
         return NULL;
+    struct usb_s *cntl = dummy->cntl;
+    dprintf(7, "ohci_alloc_intr_pipe %p %d\n", cntl, frameexp);
 
-    dprintf(7, "ohci_alloc_intr_pipe %x %d\n", endp, frameexp);
     if (frameexp > 5)
         frameexp = 5;
-    struct usb_s *cntl = endp2cntl(endp);
-    int maxpacket = endp2maxsize(endp);
-    int lowspeed = endp2speed(endp);
-    int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
+    int maxpacket = dummy->maxpacket;
+    int lowspeed = dummy->lowspeed;
+    int devaddr = dummy->devaddr | (dummy->ep << 7);
     // Determine number of entries needed for 2 timer ticks.
     int ms = 1<<frameexp;
     int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
@@ -452,7 +450,7 @@ ohci_alloc_intr_pipe(u32 endp, int frameexp)
     pipe->data = data;
     pipe->count = count;
     pipe->tds = tds;
-    pipe->pipe.endp = endp;
+    memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
     return &pipe->pipe;
 
 err:
@@ -482,8 +480,7 @@ ohci_poll_intr(struct usb_pipe *p, void *data)
     // XXX - check for errors.
 
     // Copy data.
-    u32 endp = GET_FLATPTR(pipe->pipe.endp);
-    int maxpacket = endp2maxsize(endp);
+    int maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
     void *pipedata = GET_FLATPTR(pipe->data);
     void *intrdata = pipedata + maxpacket * pos;
     memcpy_far(GET_SEG(SS), data
index fadd39680cac92e98b75e2a0381f826c7a9212ff..b7b2b29de8a351c8659d69e2324b6971a4d642a7 100644 (file)
@@ -6,11 +6,11 @@ struct usb_s;
 void ohci_init(void *data);
 struct usb_pipe;
 void ohci_free_pipe(struct usb_pipe *p);
-struct usb_pipe *ohci_alloc_control_pipe(u32 endp);
+struct usb_pipe *ohci_alloc_control_pipe(struct usb_pipe *dummy);
 int ohci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
                  , void *data, int datasize);
-struct usb_pipe *ohci_alloc_intr_pipe(u32 endp, int frameexp);
-int ohci_poll_intr(struct usb_pipe *pipe, void *data);
+struct usb_pipe *ohci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp);
+int ohci_poll_intr(struct usb_pipe *p, void *data);
 
 
 /****************************************************************
index 86eeceabd6cef11029085010b7e772f12d34c079..a8709634116dc5340b11793ac5346c277deb07d7 100644 (file)
@@ -251,10 +251,9 @@ uhci_free_pipe(struct usb_pipe *p)
 {
     if (! CONFIG_USB_UHCI)
         return;
+    dprintf(7, "uhci_free_pipe %p\n", p);
     struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
-    u32 endp = pipe->pipe.endp;
-    dprintf(7, "uhci_free_pipe %x\n", endp);
-    struct usb_s *cntl = endp2cntl(endp);
+    struct usb_s *cntl = pipe->pipe.cntl;
 
     struct uhci_framelist *fl = cntl->uhci.framelist;
     struct uhci_qh *pos = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
@@ -281,12 +280,12 @@ uhci_free_pipe(struct usb_pipe *p)
 }
 
 struct usb_pipe *
-uhci_alloc_control_pipe(u32 endp)
+uhci_alloc_control_pipe(struct usb_pipe *dummy)
 {
     if (! CONFIG_USB_UHCI)
         return NULL;
-    struct usb_s *cntl = endp2cntl(endp);
-    dprintf(7, "uhci_alloc_control_pipe %x\n", endp);
+    struct usb_s *cntl = dummy->cntl;
+    dprintf(7, "uhci_alloc_control_pipe %p\n", cntl);
 
     // Allocate a queue head.
     struct uhci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
@@ -294,9 +293,9 @@ uhci_alloc_control_pipe(u32 endp)
         warn_noalloc();
         return NULL;
     }
+    memset(pipe, 0, sizeof(*pipe));
     pipe->qh.element = UHCI_PTR_TERM;
-    pipe->next_td = 0;
-    pipe->pipe.endp = endp;
+    memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
 
     // Add queue head to controller list.
     struct uhci_qh *control_qh = cntl->uhci.control_qh;
@@ -315,14 +314,13 @@ uhci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
     ASSERT32FLAT();
     if (! CONFIG_USB_UHCI)
         return -1;
+    dprintf(5, "uhci_control %p\n", p);
     struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
-    u32 endp = pipe->pipe.endp;
 
-    dprintf(5, "uhci_control %x\n", endp);
-    struct usb_s *cntl = endp2cntl(endp);
-    int maxpacket = endp2maxsize(endp);
-    int lowspeed = endp2speed(endp);
-    int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
+    struct usb_s *cntl = pipe->pipe.cntl;
+    int maxpacket = pipe->pipe.maxpacket;
+    int lowspeed = pipe->pipe.lowspeed;
+    int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
 
     // Setup transfer descriptors
     int count = 2 + DIV_ROUND_UP(datasize, maxpacket);
@@ -368,12 +366,12 @@ uhci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
 }
 
 struct usb_pipe *
-uhci_alloc_bulk_pipe(u32 endp)
+uhci_alloc_bulk_pipe(struct usb_pipe *dummy)
 {
     if (! CONFIG_USB_UHCI)
         return NULL;
-    struct usb_s *cntl = endp2cntl(endp);
-    dprintf(7, "uhci_alloc_bulk_pipe %x\n", endp);
+    struct usb_s *cntl = dummy->cntl;
+    dprintf(7, "uhci_alloc_bulk_pipe %p\n", cntl);
 
     // Allocate a queue head.
     struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
@@ -381,9 +379,9 @@ uhci_alloc_bulk_pipe(u32 endp)
         warn_noalloc();
         return NULL;
     }
+    memset(pipe, 0, sizeof(*pipe));
     pipe->qh.element = UHCI_PTR_TERM;
-    pipe->next_td = 0;
-    pipe->pipe.endp = endp;
+    memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
 
     // Add queue head to controller list.
     struct uhci_qh *bulk_qh = cntl->uhci.bulk_qh;
@@ -423,13 +421,13 @@ int
 uhci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
 {
     struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
-    u32 endp = GET_FLATPTR(pipe->pipe.endp);
-    dprintf(7, "uhci_send_bulk qh=%p endp=%x dir=%d data=%p size=%d\n"
-            , &pipe->qh, endp, dir, data, datasize);
-    int maxpacket = endp2maxsize(endp);
-    int lowspeed = endp2speed(endp);
-    int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
-    int toggle = (u32)GET_FLATPTR(pipe->next_td); // XXX
+    dprintf(7, "uhci_send_bulk qh=%p dir=%d data=%p size=%d\n"
+            , &pipe->qh, dir, data, datasize);
+    int maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
+    int lowspeed = GET_FLATPTR(pipe->pipe.lowspeed);
+    int devaddr = (GET_FLATPTR(pipe->pipe.devaddr)
+                   | (GET_FLATPTR(pipe->pipe.ep) << 7));
+    int toggle = GET_FLATPTR(pipe->pipe.toggle) ? TD_TOKEN_TOGGLE : 0;
 
     // Allocate 4 tds on stack (16byte aligned)
     u8 tdsbuf[sizeof(struct uhci_td) * STACKTDS + TDALIGN - 1];
@@ -472,28 +470,28 @@ uhci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
             goto fail;
     }
 
-    SET_FLATPTR(pipe->next_td, (void*)toggle); // XXX
+    SET_FLATPTR(pipe->pipe.toggle, !!toggle);
     return 0;
 fail:
     dprintf(1, "uhci_send_bulk failed\n");
     SET_FLATPTR(pipe->qh.element, UHCI_PTR_TERM);
-    uhci_waittick(endp2cntl(endp));
+    uhci_waittick(GET_FLATPTR(pipe->pipe.cntl));
     return -1;
 }
 
 struct usb_pipe *
-uhci_alloc_intr_pipe(u32 endp, int frameexp)
+uhci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp)
 {
     if (! CONFIG_USB_UHCI)
         return NULL;
+    struct usb_s *cntl = dummy->cntl;
+    dprintf(7, "uhci_alloc_intr_pipe %p %d\n", cntl, frameexp);
 
-    dprintf(7, "uhci_alloc_intr_pipe %x %d\n", endp, frameexp);
     if (frameexp > 10)
         frameexp = 10;
-    struct usb_s *cntl = endp2cntl(endp);
-    int maxpacket = endp2maxsize(endp);
-    int lowspeed = endp2speed(endp);
-    int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
+    int maxpacket = dummy->maxpacket;
+    int lowspeed = dummy->lowspeed;
+    int devaddr = dummy->devaddr | (dummy->ep << 7);
     // Determine number of entries needed for 2 timer ticks.
     int ms = 1<<frameexp;
     int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
@@ -505,7 +503,11 @@ uhci_alloc_intr_pipe(u32 endp, int frameexp)
     }
     if (maxpacket > sizeof(tds[0].data))
         goto fail;
+    memset(pipe, 0, sizeof(*pipe));
     pipe->qh.element = (u32)tds;
+    pipe->next_td = &tds[0];
+    memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
+
     int toggle = 0;
     int i;
     for (i=0; i<count; i++) {
@@ -519,9 +521,6 @@ uhci_alloc_intr_pipe(u32 endp, int frameexp)
         toggle ^= TD_TOKEN_TOGGLE;
     }
 
-    pipe->next_td = &tds[0];
-    pipe->pipe.endp = endp;
-
     // Add to interrupt schedule.
     struct uhci_framelist *fl = cntl->uhci.framelist;
     if (frameexp == 0) {
index 8dbee9c495a0147271cabd3c2831ac609f96233e..894027b37b778eb30cbb3bb1588169ca4af67c1d 100644 (file)
@@ -4,14 +4,14 @@
 // usb-uhci.c
 void uhci_init(void *data);
 struct usb_pipe;
-void uhci_free_pipe(struct usb_pipe *pipe);
-struct usb_pipe *uhci_alloc_control_pipe(u32 endp);
-int uhci_control(struct usb_pipe *pipe, int dir, const void *cmd, int cmdsize
+void uhci_free_pipe(struct usb_pipe *p);
+struct usb_pipe *uhci_alloc_control_pipe(struct usb_pipe *dummy);
+int uhci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
                  , void *data, int datasize);
-struct usb_pipe *uhci_alloc_bulk_pipe(u32 endp);
-int uhci_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize);
-struct usb_pipe *uhci_alloc_intr_pipe(u32 endp, int frameexp);
-int uhci_poll_intr(struct usb_pipe *pipe, void *data);
+struct usb_pipe *uhci_alloc_bulk_pipe(struct usb_pipe *dummy);
+int uhci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize);
+struct usb_pipe *uhci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp);
+int uhci_poll_intr(struct usb_pipe *p, void *data);
 
 
 /****************************************************************
index ba701819484857a23abc06b2217c925692b0fde6..57d1ad54ddd79f261c7170cad520b348b06c7ca6 100644 (file)
--- a/src/usb.c
+++ b/src/usb.c
@@ -31,8 +31,7 @@ free_pipe(struct usb_pipe *pipe)
     ASSERT32FLAT();
     if (!pipe)
         return;
-    struct usb_s *cntl = endp2cntl(pipe->endp);
-    switch (cntl->type) {
+    switch (pipe->type) {
     default:
     case USB_TYPE_UHCI:
         return uhci_free_pipe(pipe);
@@ -41,17 +40,17 @@ free_pipe(struct usb_pipe *pipe)
     }
 }
 
-// Allocate a control pipe (which can only be used by 32bit code)
+// Allocate a control pipe to a default endpoint (which can only be
+// used by 32bit code)
 static struct usb_pipe *
-alloc_control_pipe(u32 endp)
+alloc_default_control_pipe(struct usb_pipe *dummy)
 {
-    struct usb_s *cntl = endp2cntl(endp);
-    switch (cntl->type) {
+    switch (dummy->type) {
     default:
     case USB_TYPE_UHCI:
-        return uhci_alloc_control_pipe(endp);
+        return uhci_alloc_control_pipe(dummy);
     case USB_TYPE_OHCI:
-        return ohci_alloc_control_pipe(endp);
+        return ohci_alloc_control_pipe(dummy);
     }
 }
 
@@ -61,8 +60,7 @@ send_control(struct usb_pipe *pipe, int dir, const void *cmd, int cmdsize
              , void *data, int datasize)
 {
     ASSERT32FLAT();
-    struct usb_s *cntl = endp2cntl(pipe->endp);
-    switch (cntl->type) {
+    switch (pipe->type) {
     default:
     case USB_TYPE_UHCI:
         return uhci_control(pipe, dir, cmd, cmdsize, data, datasize);
@@ -71,61 +69,70 @@ send_control(struct usb_pipe *pipe, int dir, const void *cmd, int cmdsize
     }
 }
 
+// Fill "pipe" endpoint info from an endpoint descriptor.
+static void
+desc2pipe(struct usb_pipe *newpipe, struct usb_pipe *origpipe
+          , struct usb_endpoint_descriptor *epdesc)
+{
+    memcpy(newpipe, origpipe, sizeof(*newpipe));
+    newpipe->ep = epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
+    newpipe->maxpacket = epdesc->wMaxPacketSize;
+}
+
 struct usb_pipe *
-alloc_bulk_pipe(u32 endp)
+alloc_bulk_pipe(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
 {
-    struct usb_s *cntl = endp2cntl(endp);
-    switch (cntl->type) {
+    struct usb_pipe dummy;
+    desc2pipe(&dummy, pipe, epdesc);
+    switch (pipe->type) {
     default:
     case USB_TYPE_UHCI:
-        return uhci_alloc_bulk_pipe(endp);
+        return uhci_alloc_bulk_pipe(&dummy);
     case USB_TYPE_OHCI:
         return NULL;
     }
 }
 
 int
-usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize)
+usb_send_bulk(struct usb_pipe *pipe_fl, int dir, void *data, int datasize)
 {
-    u32 endp = GET_FLATPTR(pipe->endp);
-    struct usb_s *cntl = endp2cntl(endp);
-    switch (cntl->type) {
+    switch (GET_FLATPTR(pipe_fl->type)) {
     default:
     case USB_TYPE_UHCI:
-        return uhci_send_bulk(pipe, dir, data, datasize);
+        return uhci_send_bulk(pipe_fl, dir, data, datasize);
     case USB_TYPE_OHCI:
         return -1;
     }
 }
 
 struct usb_pipe *
-alloc_intr_pipe(u32 endp, int period)
+alloc_intr_pipe(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
 {
-    struct usb_s *cntl = endp2cntl(endp);
+    struct usb_pipe dummy;
+    desc2pipe(&dummy, pipe, epdesc);
     // Find the exponential period of the requested time.
+    int period = epdesc->bInterval;
     if (period <= 0)
         period = 1;
     int frameexp = __fls(period);
-    switch (cntl->type) {
+    switch (pipe->type) {
     default:
     case USB_TYPE_UHCI:
-        return uhci_alloc_intr_pipe(endp, frameexp);
+        return uhci_alloc_intr_pipe(&dummy, frameexp);
     case USB_TYPE_OHCI:
-        return ohci_alloc_intr_pipe(endp, frameexp);
+        return ohci_alloc_intr_pipe(&dummy, frameexp);
     }
 }
 
 int noinline
-usb_poll_intr(struct usb_pipe *pipe, void *data)
+usb_poll_intr(struct usb_pipe *pipe_fl, void *data)
 {
-    u32 endp = GET_FLATPTR(pipe->endp);
-    struct usb_s *cntl = endp2cntl(endp);
-    switch (GET_GLOBAL(cntl->type)) {
+    switch (GET_FLATPTR(pipe_fl->type)) {
     default:
     case USB_TYPE_UHCI:
-        return uhci_poll_intr(pipe, data);
+        return uhci_poll_intr(pipe_fl, data);
     case USB_TYPE_OHCI:
-        return ohci_poll_intr(pipe, data);
+        return ohci_poll_intr(pipe_fl, data);
     }
 }
 
@@ -153,23 +160,6 @@ findEndPointDesc(struct usb_interface_descriptor *iface, int imax
     }
 }
 
-// Change endpoint characteristics of the default control pipe.
-static void
-usb_alter_control(struct usb_pipe *pipe, u32 endp)
-{
-    pipe->endp = endp;
-}
-
-// Build an encoded "endp" from an endpoint descriptor.
-u32
-mkendpFromDesc(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
-{
-    u32 endp = pipe->endp;
-    return mkendp(endp2cntl(endp), endp2devaddr(endp)
-                  , epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK
-                  , endp2speed(endp), epdesc->wMaxPacketSize);
-}
-
 // Send a message to the default control pipe of a device.
 int
 send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
@@ -246,13 +236,18 @@ usb_set_address(struct usb_s *cntl, int lowspeed)
         return NULL;
 
     struct usb_pipe *defpipe = cntl->defaultpipe;
-    u32 endp = mkendp(cntl, 0, 0, lowspeed, 8);
     if (!defpipe) {
-        cntl->defaultpipe = defpipe = alloc_control_pipe(endp);
+        // Create a pipe for the default address.
+        struct usb_pipe dummy;
+        memset(&dummy, 0, sizeof(dummy));
+        dummy.cntl = cntl;
+        dummy.type = cntl->type;
+        dummy.maxpacket = 8;
+        cntl->defaultpipe = defpipe = alloc_default_control_pipe(&dummy);
         if (!defpipe)
             return NULL;
     }
-    usb_alter_control(defpipe, endp);
+    defpipe->lowspeed = lowspeed;
 
     msleep(USB_TIME_RSTRCY);
 
@@ -269,8 +264,10 @@ usb_set_address(struct usb_s *cntl, int lowspeed)
     msleep(USB_TIME_SETADDR_RECOVERY);
 
     cntl->maxaddr++;
-    endp = mkendp(cntl, cntl->maxaddr, 0, lowspeed, 8);
-    return alloc_control_pipe(endp);
+    defpipe->devaddr = cntl->maxaddr;
+    struct usb_pipe *pipe = alloc_default_control_pipe(defpipe);
+    defpipe->devaddr = 0;
+    return pipe;
 }
 
 // Called for every found device - see if a driver is available for
@@ -279,8 +276,7 @@ int
 configure_usb_device(struct usb_pipe *pipe)
 {
     ASSERT32FLAT();
-    struct usb_s *cntl = endp2cntl(pipe->endp);
-    dprintf(3, "config_usb: %p\n", cntl);
+    dprintf(3, "config_usb: %p\n", pipe);
 
     // Set the max packet size for endpoint 0 of this device.
     struct usb_device_descriptor dinfo;
@@ -292,9 +288,7 @@ configure_usb_device(struct usb_pipe *pipe)
             , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
     if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
         return 0;
-    u32 endp = mkendp(cntl, endp2devaddr(pipe->endp), 0
-                      , endp2speed(pipe->endp), dinfo.bMaxPacketSize0);
-    usb_alter_control(pipe, endp);
+    pipe->maxpacket = dinfo.bMaxPacketSize0;
 
     // Get configuration
     struct usb_config_descriptor *config = get_device_config(pipe);
index 5a6fc0f788fd09374944c65293e9ec3af1439c8f..6bc0cfda411cbf3842bc9c16949396a48fc32b1a 100644 (file)
--- a/src/usb.h
+++ b/src/usb.h
@@ -5,7 +5,13 @@
 #include "util.h" // struct mutex_s
 
 struct usb_pipe {
-    u32 endp;
+    struct usb_s *cntl;
+    u8 type;
+    u8 ep;
+    u8 devaddr;
+    u8 lowspeed;
+    u16 maxpacket;
+    u8 toggle;
 };
 
 // Local information for a usb controller.
@@ -34,54 +40,6 @@ extern struct usb_s USBControllers[];
 
 #define USB_MAXADDR 127
 
-// usb.c
-void usb_setup(void);
-struct usb_pipe *usb_set_address(struct usb_s *cntl, int lowspeed);
-int configure_usb_device(struct usb_pipe *pipe);
-struct usb_ctrlrequest;
-int send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
-                         , void *data);
-int usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize);
-void free_pipe(struct usb_pipe *pipe);
-struct usb_pipe *alloc_bulk_pipe(u32 endp);
-struct usb_pipe *alloc_intr_pipe(u32 endp, int period);
-int usb_poll_intr(struct usb_pipe *pipe, void *data);
-struct usb_interface_descriptor;
-struct usb_endpoint_descriptor *findEndPointDesc(
-    struct usb_interface_descriptor *iface, int imax, int type, int dir);
-u32 mkendpFromDesc(struct usb_pipe *pipe
-                   , struct usb_endpoint_descriptor *epdesc);
-
-
-/****************************************************************
- * endpoint definition
- ****************************************************************/
-
-static inline u32
-mkendp(struct usb_s *cntl, u8 devaddr, u8 ep, u8 lowspeed, u8 maxsize)
-{
-    u8 bus = cntl-USBControllers;
-    u8 size = __ffs(maxsize);
-    return (size<<25) | (lowspeed<<24) | (bus<<16) | (devaddr<<8) | ep;
-}
-
-static inline u8 endp2ep(u32 endp) {
-    return endp;
-}
-static inline u8 endp2devaddr(u32 endp) {
-    return endp>>8;
-}
-static inline struct usb_s *endp2cntl(u32 endp) {
-    u8 bus = endp>>16;
-    return &USBControllers[bus];
-}
-static inline u8 endp2speed(u32 endp) {
-    return (endp>>24) & 1;
-}
-static inline u8 endp2maxsize(u32 endp) {
-    return 1 << (endp>>25);
-}
-
 
 /****************************************************************
  * usb structs and flags
@@ -216,4 +174,27 @@ struct usb_endpoint_descriptor {
 #define USB_ENDPOINT_XFER_INT           3
 #define USB_ENDPOINT_MAX_ADJUSTABLE     0x80
 
+
+/****************************************************************
+ * function defs
+ ****************************************************************/
+
+// usb.c
+void usb_setup(void);
+struct usb_pipe *usb_set_address(struct usb_s *cntl, int lowspeed);
+int configure_usb_device(struct usb_pipe *pipe);
+int send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
+                         , void *data);
+int usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize);
+void free_pipe(struct usb_pipe *pipe);
+struct usb_pipe *alloc_bulk_pipe(struct usb_pipe *pipe
+                                 , struct usb_endpoint_descriptor *epdesc);
+struct usb_pipe *alloc_intr_pipe(struct usb_pipe *pipe
+                                 , struct usb_endpoint_descriptor *epdesc);
+int usb_poll_intr(struct usb_pipe *pipe, void *data);
+struct usb_endpoint_descriptor *findEndPointDesc(
+    struct usb_interface_descriptor *iface, int imax, int type, int dir);
+u32 mkendpFromDesc(struct usb_pipe *pipe
+                   , struct usb_endpoint_descriptor *epdesc);
+
 #endif // usb.h