#error "CONFIG_ARCH_AT91 must be defined."
 #endif
 
+#define valid_port(index)      ((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
+#define at91_for_each_port(index)      \
+               for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)
+
 /* interface and function clocks; sometimes also an AHB clock */
 static struct clk *iclk, *fclk, *hclk;
 static int clocked;
 
 static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
 {
-       if (port < 0 || port >= 2)
+       if (!valid_port(port))
                return;
 
        if (!gpio_is_valid(pdata->vbus_pin[port]))
 
 static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
 {
-       if (port < 0 || port >= 2)
+       if (!valid_port(port))
                return -EINVAL;
 
        if (!gpio_is_valid(pdata->vbus_pin[port]))
        int length = ohci_hub_status_data(hcd, buf);
        int port;
 
-       for (port = 0; port < ARRAY_SIZE(pdata->overcurrent_pin); port++) {
+       at91_for_each_port(port) {
                if (pdata->overcurrent_changed[port]) {
-                       if (! length)
+                       if (!length)
                                length = 1;
                        buf[0] |= 1 << (port + 1);
                }
                "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
                hcd, typeReq, wValue, wIndex, buf, wLength);
 
+       wIndex--;
+
        switch (typeReq) {
        case SetPortFeature:
                if (wValue == USB_PORT_FEAT_POWER) {
                        dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
-                       ohci_at91_usb_set_power(pdata, wIndex - 1, 1);
+                       if (valid_port(wIndex)) {
+                               ohci_at91_usb_set_power(pdata, wIndex, 1);
+                               ret = 0;
+                       }
+
                        goto out;
                }
                break;
                        dev_dbg(hcd->self.controller,
                                "ClearPortFeature: C_OVER_CURRENT\n");
 
-                       if (wIndex == 1 || wIndex == 2) {
-                               pdata->overcurrent_changed[wIndex-1] = 0;
-                               pdata->overcurrent_status[wIndex-1] = 0;
+                       if (valid_port(wIndex)) {
+                               pdata->overcurrent_changed[wIndex] = 0;
+                               pdata->overcurrent_status[wIndex] = 0;
                        }
 
                        goto out;
                        dev_dbg(hcd->self.controller,
                                "ClearPortFeature: OVER_CURRENT\n");
 
-                       if (wIndex == 1 || wIndex == 2) {
-                               pdata->overcurrent_status[wIndex-1] = 0;
-                       }
+                       if (valid_port(wIndex))
+                               pdata->overcurrent_status[wIndex] = 0;
 
                        goto out;
 
                        dev_dbg(hcd->self.controller,
                                "ClearPortFeature: POWER\n");
 
-                       if (wIndex == 1 || wIndex == 2) {
-                               ohci_at91_usb_set_power(pdata, wIndex - 1, 0);
+                       if (valid_port(wIndex)) {
+                               ohci_at91_usb_set_power(pdata, wIndex, 0);
                                return 0;
                        }
                }
                break;
        }
 
-       ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
+       ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
        if (ret)
                goto out;
 
 
                dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
 
-               if (wIndex == 1 || wIndex == 2) {
-                       if (! ohci_at91_usb_get_power(pdata, wIndex-1)) {
+               if (valid_port(wIndex)) {
+                       if (!ohci_at91_usb_get_power(pdata, wIndex))
                                *data &= ~cpu_to_le32(RH_PS_PPS);
-                       }
 
-                       if (pdata->overcurrent_changed[wIndex-1]) {
+                       if (pdata->overcurrent_changed[wIndex])
                                *data |= cpu_to_le32(RH_PS_OCIC);
-                       }
 
-                       if (pdata->overcurrent_status[wIndex-1]) {
+                       if (pdata->overcurrent_status[wIndex])
                                *data |= cpu_to_le32(RH_PS_POCI);
-                       }
                }
        }
 
 
        /* From the GPIO notifying the over-current situation, find
         * out the corresponding port */
-       for (port = 0; port < ARRAY_SIZE(pdata->overcurrent_pin); port++) {
+       at91_for_each_port(port) {
                if (gpio_to_irq(pdata->overcurrent_pin[port]) == irq) {
                        gpio = pdata->overcurrent_pin[port];
                        break;
                }
        }
 
-       if (port == ARRAY_SIZE(pdata->overcurrent_pin)) {
+       if (port == AT91_MAX_USBH_PORTS) {
                dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
                return IRQ_HANDLED;
        }
        /* When notified of an over-current situation, disable power
           on the corresponding port, and mark this port in
           over-current. */
-       if (! val) {
+       if (!val) {
                ohci_at91_usb_set_power(pdata, port, 0);
                pdata->overcurrent_status[port]  = 1;
                pdata->overcurrent_changed[port] = 1;
        if (!of_property_read_u32(np, "num-ports", &ports))
                pdata->ports = ports;
 
-       for (i = 0; i < 2; i++) {
+       at91_for_each_port(i) {
                gpio = of_get_named_gpio_flags(np, "atmel,vbus-gpio", i, &flags);
                pdata->vbus_pin[i] = gpio;
                if (!gpio_is_valid(gpio))
                pdata->vbus_pin_active_low[i] = flags & OF_GPIO_ACTIVE_LOW;
        }
 
-       for (i = 0; i < 2; i++)
+       at91_for_each_port(i)
                pdata->overcurrent_pin[i] =
                        of_get_named_gpio_flags(np, "atmel,oc-gpio", i, &flags);
 
        pdata = pdev->dev.platform_data;
 
        if (pdata) {
-               for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) {
+               at91_for_each_port(i) {
                        if (!gpio_is_valid(pdata->vbus_pin[i]))
                                continue;
                        gpio = pdata->vbus_pin[i];
                        ohci_at91_usb_set_power(pdata, i, 1);
                }
 
-               for (i = 0; i < ARRAY_SIZE(pdata->overcurrent_pin); i++) {
+               at91_for_each_port(i) {
                        if (!gpio_is_valid(pdata->overcurrent_pin[i]))
                                continue;
                        gpio = pdata->overcurrent_pin[i];
        int                     i;
 
        if (pdata) {
-               for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) {
+               at91_for_each_port(i) {
                        if (!gpio_is_valid(pdata->vbus_pin[i]))
                                continue;
                        ohci_at91_usb_set_power(pdata, i, 0);
                        gpio_free(pdata->vbus_pin[i]);
                }
 
-               for (i = 0; i < ARRAY_SIZE(pdata->overcurrent_pin); i++) {
+               at91_for_each_port(i) {
                        if (!gpio_is_valid(pdata->overcurrent_pin[i]))
                                continue;
                        free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);