Prefer passing a USB "pipe" structure over a USB endp encoding.
[seabios.git] / src / usb-uhci.c
index 950ec6a3b116f5e649eae65449990249e4049029..d4497bd134546d8dd11cd7317f483846e99dd750 100644 (file)
 #include "pci_regs.h" // PCI_BASE_ADDRESS_4
 #include "usb.h" // struct usb_s
 #include "farptr.h" // GET_FLATPTR
+#include "biosvar.h" // GET_GLOBAL
+
+
+/****************************************************************
+ * Setup
+ ****************************************************************/
 
 static void
 reset_uhci(struct usb_s *cntl)
@@ -37,15 +43,13 @@ configure_uhci(struct usb_s *cntl)
     struct uhci_td *term_td = malloc_high(sizeof(*term_td));
     struct uhci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
     struct uhci_qh *intr_qh = malloc_high(sizeof(*intr_qh));
-    struct uhci_qh *data_qh = malloc_high(sizeof(*data_qh));
     struct uhci_qh *term_qh = malloc_high(sizeof(*term_qh));
-    if (!term_td || !fl || !intr_qh || !data_qh || !term_qh) {
+    if (!term_td || !fl || !intr_qh || !term_qh) {
+        warn_noalloc();
         free(term_td);
         free(fl);
         free(intr_qh);
-        free(data_qh);
         free(term_qh);
-        dprintf(1, "No ram for uhci init\n");
         return;
     }
 
@@ -58,20 +62,16 @@ configure_uhci(struct usb_s *cntl)
     term_qh->element = (u32)term_td;
     term_qh->link = UHCI_PTR_TERM;
 
-    // Setup primary queue head.
-    memset(data_qh, 0, sizeof(*data_qh));
-    data_qh->element = UHCI_PTR_TERM;
-    data_qh->link = (u32)term_qh | UHCI_PTR_QH;
-    cntl->uhci.qh = data_qh;
-
     // Set schedule to point to primary intr queue head
     memset(intr_qh, 0, sizeof(*intr_qh));
     intr_qh->element = UHCI_PTR_TERM;
-    intr_qh->link = (u32)data_qh | UHCI_PTR_QH;
+    intr_qh->link = (u32)term_qh | UHCI_PTR_QH;
     int i;
     for (i=0; i<ARRAY_SIZE(fl->links); i++)
         fl->links[i] = (u32)intr_qh | UHCI_PTR_QH;
     cntl->uhci.framelist = fl;
+    cntl->uhci.control_qh = cntl->uhci.bulk_qh = intr_qh;
+    barrier();
 
     // Set the frame length to the default: 1 ms exactly
     outb(USBSOF_DEFAULT, cntl->uhci.iobase + USBSOF);
@@ -94,6 +94,7 @@ start_uhci(struct usb_s *cntl)
 static int
 check_ports(struct usb_s *cntl)
 {
+    // XXX - if just powered up, need to wait for USB_TIME_SIGATT?
     u16 port1 = inw(cntl->uhci.iobase + USBPORTSC1);
     u16 port2 = inw(cntl->uhci.iobase + USBPORTSC2);
 
@@ -101,29 +102,34 @@ check_ports(struct usb_s *cntl)
         // No devices
         return 0;
 
+    // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
+
     // reset ports
     if (port1 & USBPORTSC_CCS)
         outw(USBPORTSC_PR, cntl->uhci.iobase + USBPORTSC1);
     if (port2 & USBPORTSC_CCS)
         outw(USBPORTSC_PR, cntl->uhci.iobase + USBPORTSC2);
-    msleep(50);
-    outw(0, cntl->uhci.iobase + USBPORTSC1);
-    outw(0, cntl->uhci.iobase + USBPORTSC2);
-    msleep(10);
+    msleep(USB_TIME_DRSTR);
 
     // Configure ports
     int totalcount = 0;
+    outw(0, cntl->uhci.iobase + USBPORTSC1);
+    udelay(6); // 64 high-speed bit times
     port1 = inw(cntl->uhci.iobase + USBPORTSC1);
     if (port1 & USBPORTSC_CCS) {
         outw(USBPORTSC_PE, cntl->uhci.iobase + USBPORTSC1);
+        msleep(USB_TIME_RSTRCY);
         int count = configure_usb_device(cntl, !!(port1 & USBPORTSC_LSDA));
         if (! count)
             outw(0, cntl->uhci.iobase + USBPORTSC1);
         totalcount += count;
     }
+    outw(0, cntl->uhci.iobase + USBPORTSC2);
+    udelay(6);
     port2 = inw(cntl->uhci.iobase + USBPORTSC2);
     if (port2 & USBPORTSC_CCS) {
         outw(USBPORTSC_PE, cntl->uhci.iobase + USBPORTSC2);
+        msleep(USB_TIME_RSTRCY);
         int count = configure_usb_device(cntl, !!(port2 & USBPORTSC_LSDA));
         if (! count)
             outw(0, cntl->uhci.iobase + USBPORTSC2);
@@ -155,11 +161,17 @@ uhci_init(void *data)
     start_uhci(cntl);
 
     int count = check_ports(cntl);
+    free_pipe(cntl->defaultpipe);
     if (! count) {
         // XXX - no devices; free data structures.
     }
 }
 
+
+/****************************************************************
+ * End point communication
+ ****************************************************************/
+
 static int
 wait_qh(struct usb_s *cntl, struct uhci_qh *qh)
 {
@@ -169,6 +181,7 @@ wait_qh(struct usb_s *cntl, struct uhci_qh *qh)
         if (qh->element & UHCI_PTR_TERM)
             return 0;
         if (check_time(end)) {
+            warn_timeout();
             struct uhci_td *td = (void*)(qh->element & ~UHCI_PTR_BITS);
             dprintf(1, "Timeout on wait_qh %p (td=%p s=%x c=%x/%x)\n"
                     , qh, td, td->status
@@ -180,12 +193,102 @@ wait_qh(struct usb_s *cntl, struct uhci_qh *qh)
     }
 }
 
+// Wait for next USB frame to start - for ensuring safe memory release.
+static void
+uhci_waittick(struct usb_s *cntl)
+{
+    barrier();
+    u16 iobase = GET_GLOBAL(cntl->uhci.iobase);
+    u16 startframe = inw(iobase + USBFRNUM);
+    u64 end = calc_future_tsc(1000 * 5);
+    for (;;) {
+        if (inw(iobase + USBFRNUM) != startframe)
+            break;
+        if (check_time(end)) {
+            warn_timeout();
+            return;
+        }
+        yield();
+    }
+}
+
+struct uhci_pipe {
+    struct uhci_qh qh;
+    struct uhci_td *next_td;
+    struct usb_pipe pipe;
+};
+
+void
+uhci_free_pipe(struct usb_pipe *p)
+{
+    if (! CONFIG_USB_UHCI)
+        return;
+    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 uhci_framelist *fl = cntl->uhci.framelist;
+    struct uhci_qh *pos = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
+    for (;;) {
+        u32 link = pos->link;
+        if (link == UHCI_PTR_TERM) {
+            // Not found?!  Exit without freeing.
+            warn_internalerror();
+            return;
+        }
+        struct uhci_qh *next = (void*)(link & ~UHCI_PTR_BITS);
+        if (next == &pipe->qh) {
+            pos->link = next->link;
+            if (cntl->uhci.control_qh == next)
+                cntl->uhci.control_qh = pos;
+            if (cntl->uhci.bulk_qh == next)
+                cntl->uhci.bulk_qh = pos;
+            uhci_waittick(cntl);
+            free(pipe);
+            return;
+        }
+        pos = next;
+    }
+}
+
+struct usb_pipe *
+uhci_alloc_control_pipe(u32 endp)
+{
+    if (! CONFIG_USB_UHCI)
+        return NULL;
+    struct usb_s *cntl = endp2cntl(endp);
+    dprintf(7, "uhci_alloc_control_pipe %x\n", endp);
+
+    // Allocate a queue head.
+    struct uhci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
+    if (!pipe) {
+        warn_noalloc();
+        return NULL;
+    }
+    pipe->qh.element = UHCI_PTR_TERM;
+    pipe->next_td = 0;
+    pipe->pipe.endp = endp;
+
+    // Add queue head to controller list.
+    struct uhci_qh *control_qh = cntl->uhci.control_qh;
+    pipe->qh.link = control_qh->link;
+    barrier();
+    control_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
+    if (cntl->uhci.bulk_qh == control_qh)
+        cntl->uhci.bulk_qh = &pipe->qh;
+    return &pipe->pipe;
+}
+
 int
-uhci_control(u32 endp, int dir, const void *cmd, int cmdsize
+uhci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
              , void *data, int datasize)
 {
+    ASSERT32FLAT();
     if (! CONFIG_USB_UHCI)
         return -1;
+    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);
@@ -225,16 +328,129 @@ uhci_control(u32 endp, int dir, const void *cmd, int cmdsize
     tds[i].buffer = 0;
 
     // Transfer data
-    struct uhci_qh *data_qh = cntl->uhci.qh;
-    data_qh->element = (u32)&tds[0];
-    int ret = wait_qh(cntl, data_qh);
+    barrier();
+    pipe->qh.element = (u32)&tds[0];
+    int ret = wait_qh(cntl, &pipe->qh);
     if (ret) {
-        data_qh->element = UHCI_PTR_TERM;
-        // XXX - leak tds
-        return ret;
+        pipe->qh.element = UHCI_PTR_TERM;
+        uhci_waittick(cntl);
     }
     free(tds);
+    return ret;
+}
+
+struct usb_pipe *
+uhci_alloc_bulk_pipe(u32 endp)
+{
+    if (! CONFIG_USB_UHCI)
+        return NULL;
+    struct usb_s *cntl = endp2cntl(endp);
+    dprintf(7, "uhci_alloc_bulk_pipe %x\n", endp);
+
+    // Allocate a queue head.
+    struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
+    if (!pipe) {
+        warn_noalloc();
+        return NULL;
+    }
+    pipe->qh.element = UHCI_PTR_TERM;
+    pipe->next_td = 0;
+    pipe->pipe.endp = endp;
+
+    // Add queue head to controller list.
+    struct uhci_qh *bulk_qh = cntl->uhci.bulk_qh;
+    pipe->qh.link = bulk_qh->link;
+    barrier();
+    bulk_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
+
+    return &pipe->pipe;
+}
+
+static int
+wait_td(struct uhci_td *td)
+{
+    u64 end = calc_future_tsc(5000); // XXX - lookup real time.
+    u32 status;
+    for (;;) {
+        status = td->status;
+        if (!(status & TD_CTRL_ACTIVE))
+            break;
+        if (check_time(end)) {
+            warn_timeout();
+            return -1;
+        }
+        yield();
+    }
+    if (status & TD_CTRL_ANY_ERROR) {
+        dprintf(1, "wait_td error - status=%x\n", status);
+        return -2;
+    }
+    return 0;
+}
+
+#define STACKTDS 4
+#define TDALIGN 16
+
+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
+
+    // Allocate 4 tds on stack (16byte aligned)
+    u8 tdsbuf[sizeof(struct uhci_td) * STACKTDS + TDALIGN - 1];
+    struct uhci_td *tds = (void*)ALIGN((u32)tdsbuf, TDALIGN);
+    memset(tds, 0, sizeof(*tds) * STACKTDS);
+
+    // Enable tds
+    SET_FLATPTR(pipe->qh.element, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
+
+    int tdpos = 0;
+    while (datasize) {
+        struct uhci_td *td = &tds[tdpos++ % STACKTDS];
+        int ret = wait_td(td);
+        if (ret)
+            goto fail;
+
+        int transfer = datasize;
+        if (transfer > maxpacket)
+            transfer = maxpacket;
+        struct uhci_td *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
+                                                 , &tds[tdpos % STACKTDS]);
+        td->link = (transfer==datasize ? UHCI_PTR_TERM : (u32)nexttd_fl);
+        td->token = (uhci_explen(transfer) | toggle
+                     | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
+                     | (dir ? USB_PID_IN : USB_PID_OUT));
+        td->buffer = data;
+        barrier();
+        td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
+                      | TD_CTRL_ACTIVE);
+        toggle ^= TD_TOKEN_TOGGLE;
+
+        data += transfer;
+        datasize -= transfer;
+    }
+    int i;
+    for (i=0; i<STACKTDS; i++) {
+        struct uhci_td *td = &tds[tdpos++ % STACKTDS];
+        int ret = wait_td(td);
+        if (ret)
+            goto fail;
+    }
+
+    SET_FLATPTR(pipe->next_td, (void*)toggle); // XXX
     return 0;
+fail:
+    dprintf(1, "uhci_send_bulk failed\n");
+    SET_FLATPTR(pipe->qh.element, UHCI_PTR_TERM);
+    uhci_waittick(endp2cntl(endp));
+    return -1;
 }
 
 struct usb_pipe *
@@ -253,14 +469,15 @@ uhci_alloc_intr_pipe(u32 endp, int frameexp)
     // 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);
-    struct uhci_qh *qh = malloc_low(sizeof(*qh));
+    struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
     struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
-    if (!qh || !tds || maxpacket > sizeof(tds[0].data)) {
-        free(qh);
-        free(tds);
-        return NULL;
+    if (!pipe || !tds) {
+        warn_noalloc();
+        goto fail;
     }
-    qh->element = (u32)tds;
+    if (maxpacket > sizeof(tds[0].data))
+        goto fail;
+    pipe->qh.element = (u32)tds;
     int toggle = 0;
     int i;
     for (i=0; i<count; i++) {
@@ -274,35 +491,45 @@ uhci_alloc_intr_pipe(u32 endp, int frameexp)
         toggle ^= TD_TOKEN_TOGGLE;
     }
 
-    qh->next_td = &tds[0];
-    qh->pipe.endp = endp;
+    pipe->next_td = &tds[0];
+    pipe->pipe.endp = endp;
 
     // Add to interrupt schedule.
     struct uhci_framelist *fl = cntl->uhci.framelist;
     if (frameexp == 0) {
         // Add to existing interrupt entry.
         struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
-        qh->link = intr_qh->link;
-        intr_qh->link = (u32)qh | UHCI_PTR_QH;
+        pipe->qh.link = intr_qh->link;
+        barrier();
+        intr_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
+        if (cntl->uhci.control_qh == intr_qh)
+            cntl->uhci.control_qh = &pipe->qh;
+        if (cntl->uhci.bulk_qh == intr_qh)
+            cntl->uhci.bulk_qh = &pipe->qh;
     } else {
         int startpos = 1<<(frameexp-1);
-        qh->link = fl->links[startpos];
+        pipe->qh.link = fl->links[startpos];
+        barrier();
         for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
-            fl->links[i] = (u32)qh | UHCI_PTR_QH;
+            fl->links[i] = (u32)&pipe->qh | UHCI_PTR_QH;
     }
 
-    return &qh->pipe;
+    return &pipe->pipe;
+fail:
+    free(pipe);
+    free(tds);
+    return NULL;
 }
 
 int
-uhci_poll_intr(struct usb_pipe *pipe, void *data)
+uhci_poll_intr(struct usb_pipe *p, void *data)
 {
     ASSERT16();
     if (! CONFIG_USB_UHCI)
         return -1;
 
-    struct uhci_qh *qh = container_of(pipe, struct uhci_qh, pipe);
-    struct uhci_td *td = GET_FLATPTR(qh->next_td);
+    struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
+    struct uhci_td *td = GET_FLATPTR(pipe->next_td);
     u32 status = GET_FLATPTR(td->status);
     u32 token = GET_FLATPTR(td->token);
     if (status & TD_CTRL_ACTIVE)
@@ -317,9 +544,10 @@ uhci_poll_intr(struct usb_pipe *pipe, void *data)
 
     // Reenable this td.
     u32 next = GET_FLATPTR(td->link);
+    barrier();
     SET_FLATPTR(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
                              | TD_CTRL_ACTIVE));
-    SET_FLATPTR(qh->next_td, (void*)(next & ~UHCI_PTR_BITS));
+    SET_FLATPTR(pipe->next_td, (void*)(next & ~UHCI_PTR_BITS));
 
     return 0;
 }