STM32: USB: wLength field of the setup packet is in little-endian format
[bertos.git] / bertos / cpu / cortex-m3 / drv / usb_stm32.c
index 6b0b28e1d31751fa582330cfc33f9d5d922834c4..fb86f47c95be5dd9313515c7fc50f77c88b5269a 100644 (file)
@@ -446,7 +446,15 @@ static void __usb_ep_io(int EP)
        bool CurrentBuffer;
        stm32_usb_ep_t *epd = &ep_cnfg[EP];
 
-       ASSERT(epd->hw);
+       if (UNLIKELY(epd->hw == NULL))
+       {
+               LOG_ERR("%s: invalid endpoint (EP%d-%s)\n",
+                               __func__,
+                               EP >> 1,
+                               (EP & 0x01) ? "IN" : "OUT");
+               ASSERT(0);
+               return;
+       }
        if (epd->status != BEGIN_SERVICED && epd->status != NO_SERVICED)
                return;
 
@@ -605,6 +613,20 @@ static void __usb_ep_io(int EP)
        }
 }
 
+/*
+ * Return the lower value from Host expected size and size and set a flag
+ * STM32_USB_EP_ZERO_POSSIBLE when size is lower that host expected size.
+ */
+static size_t usb_size(size_t size, size_t host_size)
+{
+       if (size < host_size)
+       {
+               ep_cnfg[CTRL_ENP_IN].flags |= STM32_USB_EP_ZERO_POSSIBLE;
+               return size;
+       }
+       return host_size;
+}
+
 /* Configure an EP descriptor before performing a I/O operation */
 #define USB_EP_IO(__EP, __op, __buf, __size, __complete)               \
 ({                                                                     \
@@ -677,72 +699,6 @@ __usb_ep_write(int ep, const void *buffer, ssize_t size, void (*complete)(int))
        return USB_EP_IO(ep, write, buffer, size, complete);
 }
 
-static bool rx_done;
-static size_t rx_size;
-
-static void usb_ep_read_complete(int ep)
-{
-       if (UNLIKELY(ep >= ENP_MAX_NUMB))
-       {
-               ASSERT(0);
-               return;
-       }
-       ASSERT(!(ep & 0x01));
-
-       rx_done = true;
-       rx_size = ep_cnfg[ep].size;
-}
-
-ssize_t usb_ep_read(int ep, void *buffer, ssize_t size)
-{
-       if (UNLIKELY(!size))
-               return 0;
-       size = MIN(size, USB_RX_MAX_SIZE);
-       rx_done = false;
-       rx_size = 0;
-
-       /* Blocking read */
-       __usb_ep_read(USB_EpLogToPhysAdd(ep), buffer, size,
-                               usb_ep_read_complete);
-       while (!rx_done)
-               cpu_relax();
-
-       return rx_size;
-}
-
-static bool tx_done;
-static size_t tx_size;
-
-static void usb_ep_write_complete(int ep)
-{
-       if (UNLIKELY(ep >= ENP_MAX_NUMB))
-       {
-               ASSERT(0);
-               return;
-       }
-       ASSERT(ep & 0x01);
-
-       tx_done = true;
-       tx_size = ep_cnfg[ep].size;
-}
-
-ssize_t usb_ep_write(int ep, const void *buffer, ssize_t size)
-{
-       if (UNLIKELY(!size))
-               return 0;
-       size = MIN(size, USB_TX_MAX_SIZE);
-       tx_done = false;
-       tx_size = 0;
-
-       /* Blocking write */
-       __usb_ep_write(USB_EpLogToPhysAdd(ep), buffer, size,
-                               usb_ep_write_complete);
-       while (!tx_done)
-               cpu_relax();
-
-       return tx_size;
-}
-
 static void usb_ep_low_level_config(int ep, uint16_t offset, uint16_t size)
 {
        stm32_usb_ep_t *epc = &ep_cnfg[ep];
@@ -1122,6 +1078,96 @@ static void USB_StatusHandler(UNUSED_ARG(int, EP))
        }
 }
 
+static bool rx_done;
+static size_t rx_size;
+
+static void usb_ep_read_complete(int ep)
+{
+       if (UNLIKELY(ep >= ENP_MAX_NUMB))
+       {
+               ASSERT(0);
+               return;
+       }
+       ASSERT(!(ep & 0x01));
+
+       rx_done = true;
+       rx_size = ep_cnfg[ep].size;
+}
+
+ssize_t usb_ep_read(int ep, void *buffer, ssize_t size)
+{
+       int ep_num = USB_EpLogToPhysAdd(ep);
+
+       /* Non-blocking read for EP0 */
+       if (ep_num == CTRL_ENP_OUT)
+       {
+               size = usb_size(size, usb_le16_to_cpu(setup_packet.wLength));
+               if (!size)
+                       USB_StatusHandler(ep_num);
+               else
+                       __usb_ep_read(ep_num, buffer, size,
+                                       USB_StatusHandler);
+               return size;
+       }
+       if (UNLIKELY(!size))
+               return 0;
+       size = MIN(size, USB_RX_MAX_SIZE);
+       rx_done = false;
+       rx_size = 0;
+
+       /* Blocking read */
+       __usb_ep_read(ep_num, buffer, size, usb_ep_read_complete);
+       while (!rx_done)
+               cpu_relax();
+
+       return rx_size;
+}
+
+static bool tx_done;
+static size_t tx_size;
+
+static void usb_ep_write_complete(int ep)
+{
+       if (UNLIKELY(ep >= ENP_MAX_NUMB))
+       {
+               ASSERT(0);
+               return;
+       }
+       ASSERT(ep & 0x01);
+
+       tx_done = true;
+       tx_size = ep_cnfg[ep].size;
+}
+
+ssize_t usb_ep_write(int ep, const void *buffer, ssize_t size)
+{
+       int ep_num = USB_EpLogToPhysAdd(ep);
+
+       /* Non-blocking write for EP0 */
+       if (ep_num == CTRL_ENP_IN)
+       {
+               size = usb_size(size, usb_le16_to_cpu(setup_packet.wLength));
+               if (!size)
+                       USB_StatusHandler(ep_num);
+               else
+                       __usb_ep_write(ep_num, buffer, size,
+                                       USB_StatusHandler);
+               return size;
+       }
+       if (UNLIKELY(!size))
+               return 0;
+       size = MIN(size, USB_TX_MAX_SIZE);
+       tx_done = false;
+       tx_size = 0;
+
+       /* Blocking write */
+       __usb_ep_write(ep_num, buffer, size, usb_ep_write_complete);
+       while (!tx_done)
+               cpu_relax();
+
+       return tx_size;
+}
+
 /* Global variable to handle the following non-blocking I/O operations */
 static uint32_t InData;
 
@@ -1132,7 +1178,8 @@ static int UsbDevStatus(uint16_t index)
                return -USB_NODEV_ERROR;
 
        InData = ((uint32_t)udc.feature) & 0xff;
-       __usb_ep_write(CTRL_ENP_IN, (uint8_t *)&InData, 2, USB_StatusHandler);
+       __usb_ep_write(CTRL_ENP_IN,
+                       (uint8_t *)&InData, sizeof(InData), USB_StatusHandler);
 
        return 0;
 }
@@ -1141,7 +1188,8 @@ static int UsbDevStatus(uint16_t index)
 static int UsbInterfaceStatus(UNUSED_ARG(uint16_t, index))
 {
        InData = 0;
-       __usb_ep_write(CTRL_ENP_IN, (uint8_t *)&InData, 2, USB_StatusHandler);
+       __usb_ep_write(CTRL_ENP_IN,
+                       (uint8_t *)&InData, sizeof(InData), USB_StatusHandler);
 
        return 0;
 }
@@ -1154,7 +1202,8 @@ static int UsbEpStatus(uint16_t index)
 
        InData = 0;
        USB_GetStallEP(USB_EpLogToPhysAdd(index), (bool *)&InData);
-       __usb_ep_write(CTRL_ENP_IN, (uint8_t *)&InData, 2, USB_StatusHandler);
+       __usb_ep_write(CTRL_ENP_IN,
+                       (uint8_t *)&InData, sizeof(InData), USB_StatusHandler);
 
        return 0;
 }
@@ -1240,20 +1289,6 @@ static void USB_GetStatusHandler(void)
        }
 }
 
-/*
- * Return the lower value from Host expected size and size and set a flag
- * STM32_USB_EP_ZERO_POSSIBLE when size is lower that host expected size.
- */
-static size_t usb_size(size_t size, size_t host_size)
-{
-       if (size < host_size)
-       {
-               ep_cnfg[CTRL_ENP_IN].flags |= STM32_USB_EP_ZERO_POSSIBLE;
-               return size;
-       }
-       return host_size;
-}
-
 static int usb_get_device_descriptor(int id)
 {
        if (id)
@@ -1262,7 +1297,7 @@ static int usb_get_device_descriptor(int id)
        usb_dev->device->bMaxPacketSize0 = USB_EP0_MAX_SIZE;
        __usb_ep_write(CTRL_ENP_IN, (const uint8_t *)usb_dev->device,
                        usb_size(usb_dev->device->bLength,
-                       setup_packet.wLength),
+                               usb_le16_to_cpu(setup_packet.wLength)),
                        USB_StatusHandler);
        return 0;
 }
@@ -1297,7 +1332,7 @@ static int usb_get_configuration_descriptor(int id)
        __usb_ep_write(CTRL_ENP_IN,
                        usb_cfg_buffer,
                        usb_size(p - usb_cfg_buffer,
-                               setup_packet.wLength),
+                               usb_le16_to_cpu(setup_packet.wLength)),
                        USB_StatusHandler);
        return 0;
 }
@@ -1336,7 +1371,8 @@ static int usb_get_string_descriptor(unsigned int id)
        }
        __usb_ep_write(CTRL_ENP_IN,
                        lang_str,
-                       usb_size(lang_str->bLength, setup_packet.wLength),
+                       usb_size(lang_str->bLength,
+                               usb_le16_to_cpu(setup_packet.wLength)),
                        USB_StatusHandler);
        return 0;
 }
@@ -1381,6 +1417,17 @@ static void UsbGetDescriptor(void)
        }
 }
 
+/* USB setup packet: class/vendor request handler */
+static void usb_event_handler(struct usb_device *dev)
+{
+       /*
+        * TODO: get the appropriate usb_dev in function of the endpoint
+        * address.
+        */
+       if (dev->event_cb)
+               dev->event_cb(&setup_packet);
+}
+
 /* USB setup packet: GET_DESCRIPTOR handler */
 static void UBS_GetDescriptorHandler(void)
 {
@@ -1390,6 +1437,9 @@ static void UBS_GetDescriptorHandler(void)
        if ((setup_packet.mRequestType & USB_RECIP_MASK) ==
                        USB_RECIP_DEVICE)
                UsbGetDescriptor();
+       /* Getting descriptor for a device is a standard request */
+       else if ((setup_packet.mRequestType & USB_DIR_MASK) == USB_DIR_IN)
+               usb_event_handler(usb_dev);
        else
                ep_cnfg[CTRL_ENP_OUT].status = STALLED;
 }
@@ -1555,17 +1605,6 @@ static void USB_StandardRequestHandler(void)
        }
 }
 
-/* USB setup packet: class/vendor request handler */
-static void USB_EventHandler(void)
-{
-       /*
-        * TODO: get the appropriate usb_dev in function of the endpoint
-        * address.
-        */
-       if (usb_dev->event_cb)
-               usb_dev->event_cb(&setup_packet);
-}
-
 /* USB setup packet handler */
 static void USB_SetupHandler(void)
 {
@@ -1581,13 +1620,13 @@ static void USB_SetupHandler(void)
        case USB_TYPE_CLASS:
                LOG_INFO("%s: bmRequestType=%02x (Class)\n",
                                __func__, setup_packet.mRequestType);
-               USB_EventHandler();
+               usb_event_handler(usb_dev);
                break;
        /* Vendor */
        case USB_TYPE_VENDOR:
                LOG_INFO("%s: bmRequestType=%02x (Vendor)\n",
                                __func__, setup_packet.mRequestType);
-               USB_EventHandler();
+               usb_event_handler(usb_dev);
                break;
        case USB_TYPE_RESERVED:
                LOG_INFO("%s: bmRequestType=%02x (Reserved)\n",
@@ -1638,13 +1677,8 @@ static void usb_hw_reset(void)
        usb_set_address(0);
 
        /* Enable all the device interrupts */
-#if 0
        usb->CNTR = bmCTRM | bmRESETM | bmSOFM | bmERRM | bmPMAOVRM |
                        bmSUSPM | bmWKUPM;
-#else
-       /* XXX: disable frame interrupts for now (too much noise!) */
-       usb->CNTR = bmCTRM | bmRESETM | bmERRM | bmPMAOVRM | bmSUSPM | bmWKUPM;
-#endif
 }
 
 /* Handle a correct transfer under ISR */
@@ -1734,9 +1768,13 @@ static void usb_isr(void)
        }
        if (interrupt.SOF)
        {
+#if 0
+               /*
+                * XXX: disable logging of frame interrupts (too much noise!)
+                */
                uint16_t frame_nr = usb->FNR & 0x0fff;
-
                LOG_INFO("%s: frame %#x\n", __func__, frame_nr);
+#endif
                usb->ISTR = ~bmSOFM;
        }
        if (interrupt.WKUP)