+static int usb_scan_port(struct usb_device_scan *usb_scan)
+{
+ ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
+ unsigned short portstatus;
+ unsigned short portchange;
+ struct usb_device *dev;
+ struct usb_hub_device *hub;
+ int ret = 0;
+ int i;
+
+ dev = usb_scan->dev;
+ hub = usb_scan->hub;
+ i = usb_scan->port;
+
+ /*
+ * Don't talk to the device before the query delay is expired.
+ * This is needed for voltages to stabalize.
+ */
+ if (get_timer(0) < hub->query_delay)
+ return 0;
+
+ ret = usb_get_port_status(dev, i + 1, portsts);
+ if (ret < 0) {
+ debug("get_port_status failed\n");
+ if (get_timer(0) >= hub->connect_timeout) {
+ debug("devnum=%d port=%d: timeout\n",
+ dev->devnum, i + 1);
+ /* Remove this device from scanning list */
+ list_del(&usb_scan->list);
+ free(usb_scan);
+ return 0;
+ }
+ return 0;
+ }
+
+ portstatus = le16_to_cpu(portsts->wPortStatus);
+ portchange = le16_to_cpu(portsts->wPortChange);
+ debug("Port %d Status %X Change %X\n", i + 1, portstatus, portchange);
+
+ /* No connection change happened, wait a bit more. */
+ if (!(portchange & USB_PORT_STAT_C_CONNECTION)) {
+ if (get_timer(0) >= hub->connect_timeout) {
+ debug("devnum=%d port=%d: timeout\n",
+ dev->devnum, i + 1);
+ /* Remove this device from scanning list */
+ list_del(&usb_scan->list);
+ free(usb_scan);
+ return 0;
+ }
+ return 0;
+ }
+
+ /* Test if the connection came up, and if not exit */
+ if (!(portstatus & USB_PORT_STAT_CONNECTION))
+ return 0;
+
+ /* A new USB device is ready at this point */
+ debug("devnum=%d port=%d: USB dev found\n", dev->devnum, i + 1);
+
+ usb_hub_port_connect_change(dev, i);
+
+ if (portchange & USB_PORT_STAT_C_ENABLE) {
+ debug("port %d enable change, status %x\n", i + 1, portstatus);
+ usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
+ /*
+ * The following hack causes a ghost device problem
+ * to Faraday EHCI
+ */
+#ifndef CONFIG_USB_EHCI_FARADAY
+ /*
+ * EM interference sometimes causes bad shielded USB
+ * devices to be shutdown by the hub, this hack enables
+ * them again. Works at least with mouse driver
+ */
+ if (!(portstatus & USB_PORT_STAT_ENABLE) &&
+ (portstatus & USB_PORT_STAT_CONNECTION) &&
+ usb_device_has_child_on_port(dev, i)) {
+ debug("already running port %i disabled by hub (EMI?), re-enabling...\n",
+ i + 1);
+ usb_hub_port_connect_change(dev, i);
+ }
+#endif
+ }
+
+ if (portstatus & USB_PORT_STAT_SUSPEND) {
+ debug("port %d suspend change\n", i + 1);
+ usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_SUSPEND);
+ }
+
+ if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
+ debug("port %d over-current change\n", i + 1);
+ usb_clear_port_feature(dev, i + 1,
+ USB_PORT_FEAT_C_OVER_CURRENT);
+ /* Only power-on this one port */
+ usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
+ hub->overcurrent_count[i]++;
+
+ /*
+ * If the max-scan-count is not reached, return without removing
+ * the device from scan-list. This will re-issue a new scan.
+ */
+ if (hub->overcurrent_count[i] <=
+ PORT_OVERCURRENT_MAX_SCAN_COUNT)
+ return 0;
+
+ /* Otherwise the device will get removed */
+ printf("Port %d over-current occurred %d times\n", i + 1,
+ hub->overcurrent_count[i]);
+ }
+
+ if (portchange & USB_PORT_STAT_C_RESET) {
+ debug("port %d reset change\n", i + 1);
+ usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
+ }
+
+ /*
+ * We're done with this device, so let's remove this device from
+ * scanning list
+ */
+ list_del(&usb_scan->list);
+ free(usb_scan);
+
+ return 0;
+}
+
+static int usb_device_list_scan(void)
+{
+ struct usb_device_scan *usb_scan;
+ struct usb_device_scan *tmp;
+ static int running;
+ int ret = 0;
+
+ /* Only run this loop once for each controller */
+ if (running)
+ return 0;
+
+ running = 1;
+
+ while (1) {
+ /* We're done, once the list is empty again */
+ if (list_empty(&usb_scan_list))
+ goto out;
+
+ list_for_each_entry_safe(usb_scan, tmp, &usb_scan_list, list) {
+ int ret;
+
+ /* Scan this port */
+ ret = usb_scan_port(usb_scan);
+ if (ret)
+ goto out;
+ }
+ }
+
+out:
+ /*
+ * This USB controller has finished scanning all its connected
+ * USB devices. Set "running" back to 0, so that other USB controllers
+ * will scan their devices too.
+ */
+ running = 0;
+
+ return ret;
+}