grml...
[seabios.git] / src / usb.c
index 57d1ad54ddd79f261c7170cad520b348b06c7ca6..1f69d16aa2b738d732125891d8aff0b50602f45a 100644 (file)
--- a/src/usb.c
+++ b/src/usb.c
 #include "pci_ids.h" // PCI_CLASS_SERIAL_USB_UHCI
 #include "usb-uhci.h" // uhci_init
 #include "usb-ohci.h" // ohci_init
+#include "usb-ehci.h" // ehci_init
 #include "usb-hid.h" // usb_keyboard_setup
 #include "usb-hub.h" // usb_hub_init
 #include "usb-msc.h" // usb_msc_init
 #include "usb.h" // struct usb_s
 #include "biosvar.h" // GET_GLOBAL
 
-struct usb_s USBControllers[16] VAR16VISIBLE;
-
 
 /****************************************************************
  * Controller function wrappers
@@ -37,6 +36,8 @@ free_pipe(struct usb_pipe *pipe)
         return uhci_free_pipe(pipe);
     case USB_TYPE_OHCI:
         return ohci_free_pipe(pipe);
+    case USB_TYPE_EHCI:
+        return ehci_free_pipe(pipe);
     }
 }
 
@@ -51,6 +52,8 @@ alloc_default_control_pipe(struct usb_pipe *dummy)
         return uhci_alloc_control_pipe(dummy);
     case USB_TYPE_OHCI:
         return ohci_alloc_control_pipe(dummy);
+    case USB_TYPE_EHCI:
+        return ehci_alloc_control_pipe(dummy);
     }
 }
 
@@ -66,6 +69,8 @@ send_control(struct usb_pipe *pipe, int dir, const void *cmd, int cmdsize
         return uhci_control(pipe, dir, cmd, cmdsize, data, datasize);
     case USB_TYPE_OHCI:
         return ohci_control(pipe, dir, cmd, cmdsize, data, datasize);
+    case USB_TYPE_EHCI:
+        return ehci_control(pipe, dir, cmd, cmdsize, data, datasize);
     }
 }
 
@@ -89,7 +94,9 @@ alloc_bulk_pipe(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
     case USB_TYPE_UHCI:
         return uhci_alloc_bulk_pipe(&dummy);
     case USB_TYPE_OHCI:
-        return NULL;
+        return ohci_alloc_bulk_pipe(&dummy);
+    case USB_TYPE_EHCI:
+        return ehci_alloc_bulk_pipe(&dummy);
     }
 }
 
@@ -101,7 +108,9 @@ usb_send_bulk(struct usb_pipe *pipe_fl, int dir, void *data, int datasize)
     case USB_TYPE_UHCI:
         return uhci_send_bulk(pipe_fl, dir, data, datasize);
     case USB_TYPE_OHCI:
-        return -1;
+        return ohci_send_bulk(pipe_fl, dir, data, datasize);
+    case USB_TYPE_EHCI:
+        return ehci_send_bulk(pipe_fl, dir, data, datasize);
     }
 }
 
@@ -112,15 +121,19 @@ alloc_intr_pipe(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
     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);
+    int frameexp;
+    if (pipe->speed != USB_HIGHSPEED)
+        frameexp = (period <= 0) ? 0 : __fls(period);
+    else
+        frameexp = (period <= 4) ? 0 : period - 4;
     switch (pipe->type) {
     default:
     case USB_TYPE_UHCI:
         return uhci_alloc_intr_pipe(&dummy, frameexp);
     case USB_TYPE_OHCI:
         return ohci_alloc_intr_pipe(&dummy, frameexp);
+    case USB_TYPE_EHCI:
+        return ehci_alloc_intr_pipe(&dummy, frameexp);
     }
 }
 
@@ -133,6 +146,8 @@ usb_poll_intr(struct usb_pipe *pipe_fl, void *data)
         return uhci_poll_intr(pipe_fl, data);
     case USB_TYPE_OHCI:
         return ohci_poll_intr(pipe_fl, data);
+    case USB_TYPE_EHCI:
+        return ehci_poll_intr(pipe_fl, data);
     }
 }
 
@@ -227,10 +242,11 @@ set_configuration(struct usb_pipe *pipe, u16 val)
 
 // Assign an address to a device in the default state on the given
 // controller.
-struct usb_pipe *
-usb_set_address(struct usb_s *cntl, int lowspeed)
+static struct usb_pipe *
+usb_set_address(struct usbhub_s *hub, int port, int speed)
 {
     ASSERT32FLAT();
+    struct usb_s *cntl = hub->cntl;
     dprintf(3, "set_address %p\n", cntl);
     if (cntl->maxaddr >= USB_MAXADDR)
         return NULL;
@@ -243,11 +259,23 @@ usb_set_address(struct usb_s *cntl, int lowspeed)
         dummy.cntl = cntl;
         dummy.type = cntl->type;
         dummy.maxpacket = 8;
+        dummy.path = (u64)-1;
         cntl->defaultpipe = defpipe = alloc_default_control_pipe(&dummy);
         if (!defpipe)
             return NULL;
     }
-    defpipe->lowspeed = lowspeed;
+    defpipe->speed = speed;
+    if (hub->pipe) {
+        if (hub->pipe->speed == USB_HIGHSPEED) {
+            defpipe->tt_devaddr = hub->pipe->devaddr;
+            defpipe->tt_port = port;
+        } else {
+            defpipe->tt_devaddr = hub->pipe->tt_devaddr;
+            defpipe->tt_port = hub->pipe->tt_port;
+        }
+    } else {
+        defpipe->tt_devaddr = defpipe->tt_port = 0;
+    }
 
     msleep(USB_TIME_RSTRCY);
 
@@ -267,12 +295,15 @@ usb_set_address(struct usb_s *cntl, int lowspeed)
     defpipe->devaddr = cntl->maxaddr;
     struct usb_pipe *pipe = alloc_default_control_pipe(defpipe);
     defpipe->devaddr = 0;
+    if (hub->pipe)
+        pipe->path = hub->pipe->path;
+    pipe->path = (pipe->path << 8) | port;
     return pipe;
 }
 
 // Called for every found device - see if a driver is available for
 // this device and do setup if so.
-int
+static int
 configure_usb_device(struct usb_pipe *pipe)
 {
     ASSERT32FLAT();
@@ -298,11 +329,9 @@ configure_usb_device(struct usb_pipe *pipe)
     // Determine if a driver exists for this device - only look at the
     // first interface of the first configuration.
     struct usb_interface_descriptor *iface = (void*)(&config[1]);
-    if ((iface->bInterfaceClass != USB_CLASS_HID
-         || iface->bInterfaceSubClass != USB_INTERFACE_SUBCLASS_BOOT
-         || iface->bInterfaceProtocol != USB_INTERFACE_PROTOCOL_KEYBOARD)
-        && (iface->bInterfaceClass != USB_CLASS_MASS_STORAGE)
-        && (iface->bInterfaceClass != USB_CLASS_HUB))
+    if (iface->bInterfaceClass != USB_CLASS_HID
+        && iface->bInterfaceClass != USB_CLASS_MASS_STORAGE
+        && iface->bInterfaceClass != USB_CLASS_HUB)
         // Not a supported device.
         goto fail;
 
@@ -318,7 +347,7 @@ configure_usb_device(struct usb_pipe *pipe)
     else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE)
         ret = usb_msc_init(pipe, iface, imax);
     else
-        ret = usb_keyboard_init(pipe, iface, imax);
+        ret = usb_hid_init(pipe, iface, imax);
     if (ret)
         goto fail;
 
@@ -329,6 +358,66 @@ fail:
     return 0;
 }
 
+static void
+usb_init_hub_port(void *data)
+{
+    struct usbhub_s *hub = data;
+    u32 port = hub->port; // XXX - find better way to pass port
+
+    // Detect if device present (and possibly start reset)
+    int ret = hub->op->detect(hub, port);
+    if (ret)
+        // No device present
+        goto done;
+
+    // Reset port and determine device speed
+    mutex_lock(&hub->cntl->resetlock);
+    ret = hub->op->reset(hub, port);
+    if (ret < 0)
+        // Reset failed
+        goto resetfail;
+
+    // Set address of port
+    struct usb_pipe *pipe = usb_set_address(hub, port, ret);
+    if (!pipe) {
+        hub->op->disconnect(hub, port);
+        goto resetfail;
+    }
+    mutex_unlock(&hub->cntl->resetlock);
+
+    // Configure the device
+    int count = configure_usb_device(pipe);
+    free_pipe(pipe);
+    if (!count)
+        hub->op->disconnect(hub, port);
+    hub->devcount += count;
+done:
+    hub->threads--;
+    return;
+
+resetfail:
+    mutex_unlock(&hub->cntl->resetlock);
+    goto done;
+}
+
+void
+usb_enumerate(struct usbhub_s *hub)
+{
+    u32 portcount = hub->portcount;
+    hub->threads = portcount;
+
+    // Launch a thread for every port.
+    int i;
+    for (i=0; i<portcount; i++) {
+        hub->port = i;
+        run_thread(usb_init_hub_port, hub);
+    }
+
+    // Wait for threads to complete.
+    while (hub->threads)
+        yield();
+}
+
 void
 usb_setup(void)
 {
@@ -338,30 +427,42 @@ usb_setup(void)
 
     dprintf(3, "init usb\n");
 
-    memset(&USBControllers, 0, sizeof(USBControllers));
-    usb_keyboard_setup();
-
     // Look for USB controllers
     int count = 0;
-    int bdf, max;
-    foreachpci(bdf, max) {
-        u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;
-
-        if (code >> 8 != PCI_CLASS_SERIAL_USB)
+    struct pci_device *ehcipci = PCIDevices;
+    struct pci_device *pci;
+    foreachpci(pci) {
+        if (pci->class != PCI_CLASS_SERIAL_USB)
             continue;
 
-        struct usb_s *cntl = &USBControllers[count];
-        cntl->bdf = bdf;
-
-        if (code == PCI_CLASS_SERIAL_USB_UHCI)
-            run_thread(uhci_init, cntl);
-        else if (code == PCI_CLASS_SERIAL_USB_OHCI)
-            run_thread(ohci_init, cntl);
-        else
-            continue;
+        if (pci->bdf >= ehcipci->bdf) {
+            // Check to see if this device has an ehci controller
+            int found = 0;
+            ehcipci = pci;
+            for (;;) {
+                if (pci_classprog(ehcipci) == PCI_CLASS_SERIAL_USB_EHCI) {
+                    // Found an ehci controller.
+                    int ret = ehci_init(ehcipci, count++, pci);
+                    if (ret)
+                        // Error
+                        break;
+                    count += found;
+                    pci = ehcipci;
+                    break;
+                }
+                if (ehcipci->class == PCI_CLASS_SERIAL_USB)
+                    found++;
+                ehcipci = ehcipci->next;
+                if (!ehcipci || (pci_bdf_to_busdev(ehcipci->bdf)
+                                 != pci_bdf_to_busdev(pci->bdf)))
+                    // No ehci controller found.
+                    break;
+            }
+        }
 
-        count++;
-        if (count >= ARRAY_SIZE(USBControllers))
-            break;
+        if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_UHCI)
+            uhci_init(pci, count++);
+        else if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_OHCI)
+            ohci_init(pci, count++);
     }
 }