X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fcpu%2Fcortex-m3%2Fdrv%2Fusb_stm32.c;h=6e97574987b210a840680cbfc1ceb6efae0b7220;hb=7a562649d9bc6e42df079696410fff7091410656;hp=be9ba64014f72033156e788657913d6a61fd000e;hpb=8af7d7531523e1156f699c1252c30e45bca08057;p=bertos.git diff --git a/bertos/cpu/cortex-m3/drv/usb_stm32.c b/bertos/cpu/cortex-m3/drv/usb_stm32.c index be9ba640..6e975749 100644 --- a/bertos/cpu/cortex-m3/drv/usb_stm32.c +++ b/bertos/cpu/cortex-m3/drv/usb_stm32.c @@ -61,6 +61,10 @@ #define ALIGN_UP(value, align) (((value) & ((align) - 1)) ? \ (((value) + ((align) - 1)) & ~((align) - 1)) : \ (value)) + +/* XXX: redefine this to make it usable within C expression */ +#define _MIN(a,b) (((a) < (b)) ? (a) : (b)) + /* STM32 USB registers */ struct stm32_usb { @@ -84,10 +88,10 @@ struct stm32_usb static struct stm32_usb *usb = (struct stm32_usb *)USB_BASE_ADDR; /* Endpoint descriptors: used for handling requests to use with endpoints */ -static stm32_usb_ep_t ep_cnfg[ENP_MAX_NUMB]; +static stm32_UsbEp ep_cnfg[ENP_MAX_NUMB]; /* USB EP0 control descriptor */ -static const usb_endpoint_descriptor_t USB_CtrlEpDescr0 = +static const UsbEndpointDesc USB_CtrlEpDescr0 = { .bLength = sizeof(USB_CtrlEpDescr0), .bDescriptorType = USB_DT_ENDPOINT, @@ -98,7 +102,7 @@ static const usb_endpoint_descriptor_t USB_CtrlEpDescr0 = }; /* USB EP1 control descriptor */ -static const usb_endpoint_descriptor_t USB_CtrlEpDescr1 = +static const UsbEndpointDesc USB_CtrlEpDescr1 = { .bLength = sizeof(USB_CtrlEpDescr1), .bDescriptorType = USB_DT_ENDPOINT, @@ -109,7 +113,7 @@ static const usb_endpoint_descriptor_t USB_CtrlEpDescr1 = }; /* USB setup packet */ -static usb_ctrlrequest_t setup_packet; +static UsbCtrlRequest setup_packet; /* USB device controller: max supported interfaces */ #define USB_MAX_INTERFACE 1 @@ -123,7 +127,7 @@ typedef struct stm32_udc { uint8_t state; uint32_t cfg_id; - const usb_config_descriptor_t *cfg; + const UsbConfigDesc *cfg; uint32_t interfaces; uint32_t alt[USB_MAX_INTERFACE]; uint32_t address; @@ -134,119 +138,119 @@ typedef struct stm32_udc static stm32_udc_t udc; /* Generic USB Device Controller structure */ -static struct usb_device *usb_dev; +static UsbDevice *usb_dev; /* USB packet memory management: list of allocated chunks */ -static pack_mem_slot_t *pPacketMemUse; +static stm32_UsbMemSlot *mem_use; /* USB packet memory management: memory buffer metadata */ #define EP_MAX_SLOTS 16 -static pack_mem_slot_t PacketMemBuff[EP_MAX_SLOTS]; +static stm32_UsbMemSlot memory_buffer[EP_MAX_SLOTS]; /* Allocate a free block of the packet memory */ -static pack_mem_slot_t *usb_malloc(void) +static stm32_UsbMemSlot *usb_malloc(void) { unsigned int i; - for (i = 0; i < countof(PacketMemBuff); i++) - if (PacketMemBuff[i].Size == 0) - return &PacketMemBuff[i]; + for (i = 0; i < countof(memory_buffer); i++) + if (memory_buffer[i].Size == 0) + return &memory_buffer[i]; return NULL; } /* Release a block of the packet memory */ -static void usb_free(pack_mem_slot_t *pPntr) +static void usb_free(stm32_UsbMemSlot *pPntr) { pPntr->Size = 0; } /* Allocate a free chunk of the packet memory (inside a block) */ -static bool USB_AllocateBuffer(uint16_t *pOffset, uint32_t *pPacketSize, +static bool usb_alloc_buffer(uint16_t *pOffset, uint32_t *size, int EndPoint) { - pack_mem_slot_t *pPacketMem = pPacketMemUse, - *pPacketMemNext, *pPacketMemUseNew; - uint32_t MaxPacketSize = *pPacketSize; + stm32_UsbMemSlot *mem = mem_use, + *memNext, *mem_useNew; + uint32_t max_size = *size; /* * Packet size alignment: * - fine-granularity allocation: size alignment by 2; * - coarse-granularity allocation: size alignment by 32. */ - if (MaxPacketSize < 62) - MaxPacketSize = ALIGN_UP(MaxPacketSize, 2); + if (max_size < 62) + max_size = ALIGN_UP(max_size, 2); else - MaxPacketSize = ALIGN_UP(MaxPacketSize, 32); + max_size = ALIGN_UP(max_size, 32); /* * Finding free memory chunks from the allocated blocks of the USB * packet memory. */ *pOffset = 0; - while (pPacketMem != NULL) + while (mem != NULL) { /* Offset alignment by 4 */ - *pOffset = ALIGN_UP(pPacketMem->Start + pPacketMem->Size, 4); - pPacketMemNext = pPacketMem->next; - if ((pPacketMem->next == NULL) || - (pPacketMemNext->Start >= - *pOffset + MaxPacketSize)) + *pOffset = ALIGN_UP(mem->Start + mem->Size, 4); + memNext = mem->next; + if ((mem->next == NULL) || + (memNext->Start >= + *pOffset + max_size)) break; - pPacketMem = pPacketMem->next; + mem = mem->next; } /* Check for out-of-memory condition */ - if ((*pOffset + MaxPacketSize) >= USB_BDT_OFFSET) + if ((*pOffset + max_size) >= USB_BDT_OFFSET) return false; /* * Allocate a new memory block, next to the last allocated block. */ - pPacketMemUseNew = usb_malloc(); - if (pPacketMemUseNew == NULL) + mem_useNew = usb_malloc(); + if (mem_useNew == NULL) return false; /* Insert the block to the list of allocated blocks */ - if (pPacketMemUse == NULL) + if (mem_use == NULL) { - pPacketMemUse = pPacketMemUseNew; - pPacketMemUse->next = NULL; + mem_use = mem_useNew; + mem_use->next = NULL; } else { - pPacketMemUseNew->next = pPacketMem->next; - pPacketMem->next = pPacketMemUseNew; + mem_useNew->next = mem->next; + mem->next = mem_useNew; } /* Update block's metadata */ - pPacketMemUseNew->ep_addr = EndPoint; - pPacketMemUseNew->Start = *pOffset; - pPacketMemUseNew->Size = MaxPacketSize; + mem_useNew->ep_addr = EndPoint; + mem_useNew->Start = *pOffset; + mem_useNew->Size = max_size; - *pPacketSize = MaxPacketSize; + *size = max_size; return true; } /* Release a chunk of the packet memory (inside a block) */ -static void USB_ReleaseBuffer(int EndPoint) +static void usb_free_buffer(int EndPoint) { - pack_mem_slot_t *pPacketMem, *pPacketMemPrev = NULL; - pPacketMem = pPacketMemUse; + stm32_UsbMemSlot *mem, *memPrev = NULL; + mem = mem_use; - while (pPacketMem != NULL) + while (mem != NULL) { - if (pPacketMem->ep_addr == EndPoint) + if (mem->ep_addr == EndPoint) { - if (UNLIKELY(pPacketMemPrev == NULL)) + if (UNLIKELY(memPrev == NULL)) { /* Free the first element of the list */ - pPacketMemUse = pPacketMemUse->next; - usb_free(pPacketMem); - pPacketMem = pPacketMemUse; + mem_use = mem_use->next; + usb_free(mem); + mem = mem_use; continue; } - pPacketMemPrev->next = pPacketMem->next; - usb_free(pPacketMem); + memPrev->next = mem->next; + usb_free(mem); } else - pPacketMemPrev = pPacketMem; - pPacketMem = pPacketMemPrev->next; + memPrev = mem; + mem = memPrev->next; } } @@ -286,14 +290,14 @@ static void usb_resume(void) } /* Convert logical EP address to physical EP address */ -static int USB_EpLogToPhysAdd(uint8_t ep_addr) +static int usb_ep_logical_to_hw(uint8_t ep_addr) { int addr = (ep_addr & 0x0f) << 1; return (ep_addr & 0x80) ? addr + 1 : addr; } /* Set EP address */ -static void EpCtrlSet_EA(reg32_t *reg, uint32_t val) +static void ep_ctrl_set_ea(reg32_t *reg, uint32_t val) { val &= 0x0f; val |= *reg & 0x0700; @@ -302,13 +306,13 @@ static void EpCtrlSet_EA(reg32_t *reg, uint32_t val) } /* Get EP IN status */ -static uint32_t EpCtrlGet_STAT_TX(reg32_t *reg) +static uint32_t ep_ctrl_get_stat_tx(reg32_t *reg) { return (*reg & (0x3UL << 4)) >> 4; } /* Set EP IN state */ -static void EpCtrlSet_STAT_TX(reg32_t *reg, ep_state_t val) +static void ep_ctrl_set_stat_tx(reg32_t *reg, stm32_UsbEpState val) { uint32_t state; int i; @@ -319,7 +323,7 @@ static void EpCtrlSet_STAT_TX(reg32_t *reg, ep_state_t val) */ for (i = 0; i < 2; i++) { - if (EpCtrlGet_STAT_TX(reg) == val) + if (ep_ctrl_get_stat_tx(reg) == val) return; state = val; state <<= 4; @@ -332,7 +336,7 @@ static void EpCtrlSet_STAT_TX(reg32_t *reg, ep_state_t val) } /* Set EP DTOG_TX bit (IN) */ -static void EpCtrlSet_DTOG_TX(reg32_t *reg, uint32_t val) +static void ep_ctrl_set_dtog_tx(reg32_t *reg, uint32_t val) { val = val ? (*reg ^ (1UL << 6)) : *reg; /* Clear the toggle bits without DTOG_TX (6) */ @@ -342,7 +346,7 @@ static void EpCtrlSet_DTOG_TX(reg32_t *reg, uint32_t val) } /* Clear EP CTR_TX bit (IN) */ -static void EpCtrlClr_CTR_TX(reg32_t *reg) +static void ep_ctrl_clr_ctr_tx(reg32_t *reg) { uint32_t val = *reg; @@ -353,7 +357,7 @@ static void EpCtrlClr_CTR_TX(reg32_t *reg) } /* Clear EP CTR_RX bit (OUT) */ -static void EpCtrlClr_CTR_RX(reg32_t *reg) +static void ep_ctrl_clr_ctr_rx(reg32_t *reg) { uint32_t val = *reg; val &= ~(USB_CTRL_TOGGLE_MASK | 1UL << 15); @@ -363,7 +367,7 @@ static void EpCtrlClr_CTR_RX(reg32_t *reg) } /* Set EP KIND bit */ -static void EpCtrlSet_EP_KIND(reg32_t *reg, uint32_t val) +static void ep_ctrl_set_ep_kind(reg32_t *reg, uint32_t val) { val = val ? (1UL << 8) : 0; val |= *reg & ~(USB_CTRL_TOGGLE_MASK | (1UL << 8)); @@ -372,7 +376,7 @@ static void EpCtrlSet_EP_KIND(reg32_t *reg, uint32_t val) } /* Set EP type */ -static int EpCtrlSet_EP_TYPE(reg32_t *reg, uint8_t val) +static int ep_ctrl_set_ep_type(reg32_t *reg, uint8_t val) { uint32_t type; @@ -391,14 +395,14 @@ static int EpCtrlSet_EP_TYPE(reg32_t *reg, uint8_t val) } /* Get EP STAT_RX (OUT) */ -static uint32_t EpCtrlGet_STAT_RX(reg32_t *reg) +static uint32_t ep_ctrl_get_stat_rx(reg32_t *reg) { uint32_t val = *reg & (0x3UL << 12); return val >> 12; } /* Set EP STAT_RX (OUT) */ -static void EpCtrlSet_STAT_RX(reg32_t *reg, ep_state_t val) +static void ep_ctrl_set_stat_rx(reg32_t *reg, stm32_UsbEpState val) { uint32_t state; int i; @@ -409,7 +413,7 @@ static void EpCtrlSet_STAT_RX(reg32_t *reg, ep_state_t val) */ for (i = 0; i < 2; i++) { - if (EpCtrlGet_STAT_RX(reg) == val) + if (ep_ctrl_get_stat_rx(reg) == val) return; state = val; state <<= 12; @@ -422,7 +426,7 @@ static void EpCtrlSet_STAT_RX(reg32_t *reg, ep_state_t val) } /* Set DTOG_RX bit */ -static void EpCtrlSet_DTOG_RX(reg32_t *reg, uint32_t val) +static void ep_ctrl_set_dtog_rx(reg32_t *reg, uint32_t val) { val = val ? (*reg ^ (1UL << 14)) : *reg; /* Clear the toggle bits without DTOG_RX (14) */ @@ -432,7 +436,7 @@ static void EpCtrlSet_DTOG_RX(reg32_t *reg, uint32_t val) } /* Get EP SETUP bit */ -static uint32_t EpCtrlGet_SETUP(reg32_t *reg) +static uint32_t ep_ctrl_get_setup(reg32_t *reg) { uint32_t val = *reg & (1UL << 11); return val ? 1 : 0; @@ -443,10 +447,17 @@ static void __usb_ep_io(int EP) { ssize_t Count, CountHold, Offset; uint32_t *pDst, *pSrc, Data; - bool CurrentBuffer; - stm32_usb_ep_t *epd = &ep_cnfg[EP]; + stm32_UsbEp *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; @@ -472,15 +483,14 @@ static void __usb_ep_io(int EP) epd->flags |= STM32_USB_EP_ZERO_PACKET; Offset = epd->offset; epd->offset += Count; - CurrentBuffer = true; switch (epd->type) { case USB_ENDPOINT_XFER_CONTROL: case USB_ENDPOINT_XFER_INT: - pDst = (uint32_t *)addr2usbmem(ReadEpDTB_AddrTx(EP >> 1)); + pDst = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_TX_OFFSET)); break; case USB_ENDPOINT_XFER_BULK: - pDst = (uint32_t *)addr2usbmem(ReadEpDTB_AddrTx(EP >> 1)); + pDst = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_TX_OFFSET)); break; case USB_ENDPOINT_XFER_ISOC: LOG_ERR("%s: isochronous transfer not supported\n", @@ -503,12 +513,8 @@ static void __usb_ep_io(int EP) *pDst++ = Data; } - if (CurrentBuffer) - WriteEpDTB_CountTx(EP >> 1, CountHold); - else - WriteEpDTB_CountRx(EP >> 1, CountHold); - - EpCtrlSet_STAT_TX(epd->hw, EP_VALID); + EP_DTB_WRITE(EP >> 1, COUNT_TX_OFFSET, CountHold); + ep_ctrl_set_stat_tx(epd->hw, EP_VALID); --ep_cnfg[EP].avail_data; Count = epd->size - epd->offset; @@ -532,15 +538,15 @@ static void __usb_ep_io(int EP) case USB_ENDPOINT_XFER_CONTROL: case USB_ENDPOINT_XFER_INT: /* Get received bytes number */ - Count = ReadEpDTB_CountRx(EP >> 1) & 0x3FF; + Count = EP_DTB_READ(EP >> 1, COUNT_RX_OFFSET) & 0x3FF; /* Get address of the USB packet buffer for corresponding EP */ - pSrc = (uint32_t *)addr2usbmem(ReadEpDTB_AddrRx(EP >> 1)); + pSrc = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_RX_OFFSET)); break; case USB_ENDPOINT_XFER_BULK: /* Get received bytes number */ - Count = ReadEpDTB_CountRx(EP >> 1) & 0x3FF; + Count = EP_DTB_READ(EP >> 1, COUNT_RX_OFFSET) & 0x3FF; /* Get address of the USB packet buffer for corresponding EP */ - pSrc = (uint32_t *)addr2usbmem(ReadEpDTB_AddrRx(EP >> 1)); + pSrc = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_RX_OFFSET)); break; case USB_ENDPOINT_XFER_ISOC: LOG_ERR("%s: isochronous transfer not supported\n", @@ -581,7 +587,7 @@ static void __usb_ep_io(int EP) } } - EpCtrlSet_STAT_RX(epd->hw, EP_VALID); + ep_ctrl_set_stat_rx(epd->hw, EP_VALID); --ep_cnfg[EP].avail_data; @@ -605,11 +611,25 @@ 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) \ ({ \ cpu_flags_t flags; \ - stm32_usb_io_status_t ret; \ + stm32_UsbIoStatus ret; \ \ /* Fill EP descriptor */ \ IRQ_SAVE_DISABLE(flags); \ @@ -652,114 +672,60 @@ out: \ }) /* Configure and endponint and perform a read operation */ -static stm32_usb_io_status_t +static stm32_UsbIoStatus __usb_ep_read(int ep, void *buffer, ssize_t size, void (*complete)(int)) { - if (UNLIKELY(ep >= ENP_MAX_NUMB)) + if (UNLIKELY((ep >= ENP_MAX_NUMB) || (ep & 0x01))) { + LOG_ERR("%s: invalid EP number %d\n", __func__, ep); ASSERT(0); return STALLED; } - ASSERT(!(ep & 0x01)); - return USB_EP_IO(ep, read, buffer, size, complete); -} - -/* Configure and endponint and perform a write operation */ -static stm32_usb_io_status_t -__usb_ep_write(int ep, const void *buffer, ssize_t size, void (*complete)(int)) -{ - if (UNLIKELY(ep >= ENP_MAX_NUMB)) + if (UNLIKELY((size_t)buffer & 0x03)) { + LOG_ERR("%s: unaligned buffer @ %p\n", __func__, buffer); ASSERT(0); return STALLED; } - ASSERT(ep & 0x01); - return USB_EP_IO(ep, write, buffer, size, complete); + return USB_EP_IO(ep, read, buffer, size, complete); } -static bool rx_done; -static size_t rx_size; - -static void usb_ep_read_complete(int ep) +/* Configure and endponint and perform a write operation */ +static stm32_UsbIoStatus +__usb_ep_write(int ep, const void *buffer, ssize_t size, void (*complete)(int)) { - if (UNLIKELY(ep >= ENP_MAX_NUMB)) + if (UNLIKELY((ep >= ENP_MAX_NUMB) || !(ep & 0x01))) { + LOG_ERR("%s: invalid EP number %d\n", __func__, ep); ASSERT(0); - return; + return STALLED; } - 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)) + if (UNLIKELY((size_t)buffer & 0x03)) { + LOG_ERR("%s: unaligned buffer @ %p\n", __func__, buffer); ASSERT(0); - return; + return STALLED; } - 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; + return USB_EP_IO(ep, write, buffer, size, complete); } static void usb_ep_low_level_config(int ep, uint16_t offset, uint16_t size) { - stm32_usb_ep_t *epc = &ep_cnfg[ep]; + stm32_UsbEp *epc = &ep_cnfg[ep]; /* IN EP */ if (ep & 0x01) { /* Disable EP */ - EpCtrlSet_STAT_TX(epc->hw, EP_DISABLED); + ep_ctrl_set_stat_tx(epc->hw, EP_DISABLED); /* Clear Tx toggle */ - EpCtrlSet_DTOG_TX(epc->hw, 0); + ep_ctrl_set_dtog_tx(epc->hw, 0); /* Clear Correct Transfer for transmission flag */ - EpCtrlClr_CTR_TX(epc->hw); + ep_ctrl_clr_ctr_tx(epc->hw); /* Update EP description table */ - WriteEpDTB_AddrTx(ep >> 1, offset); - WriteEpDTB_CountTx(ep >> 1, 0); + EP_DTB_WRITE(ep >> 1, ADDR_TX_OFFSET, offset); + EP_DTB_WRITE(ep >> 1, COUNT_TX_OFFSET, 0); } /* OUT EP */ else @@ -767,32 +733,32 @@ static void usb_ep_low_level_config(int ep, uint16_t offset, uint16_t size) uint16_t rx_count = 0; /* Disable EP */ - EpCtrlSet_STAT_RX(epc->hw, EP_DISABLED); + ep_ctrl_set_stat_rx(epc->hw, EP_DISABLED); /* Clear Rx toggle */ - EpCtrlSet_DTOG_RX(epc->hw, 0); + ep_ctrl_set_dtog_rx(epc->hw, 0); /* Clear Correct Transfer for reception flag */ - EpCtrlClr_CTR_RX(epc->hw); + ep_ctrl_clr_ctr_rx(epc->hw); /* Descriptor block size field */ rx_count |= (size > 62) << 15; /* Descriptor number of blocks field */ rx_count |= (((size > 62) ? (size >> 5) - 1 : size >> 1) & 0x1f) << 10; /* Update EP description table */ - WriteEpDTB_AddrRx(ep >> 1, offset); - WriteEpDTB_CountRx(ep >> 1, rx_count); + EP_DTB_WRITE(ep >> 1, ADDR_RX_OFFSET, offset); + EP_DTB_WRITE(ep >> 1, COUNT_RX_OFFSET, rx_count); } } /* Enable/Disable an endpoint */ -static int usb_ep_configure(const usb_endpoint_descriptor_t *epd, bool enable) +static int usb_ep_configure(const UsbEndpointDesc *epd, bool enable) { int EP; - stm32_usb_ep_t *ep_hw; + stm32_UsbEp *ep_hw; reg32_t *hw; uint16_t Offset; - uint32_t MaxPacketSizeTmp; + uint32_t size; - EP = USB_EpLogToPhysAdd(epd->bEndpointAddress); + EP = usb_ep_logical_to_hw(epd->bEndpointAddress); ep_hw = &ep_cnfg[EP]; if (enable) @@ -801,8 +767,8 @@ static int usb_ep_configure(const usb_endpoint_descriptor_t *epd, bool enable) * Allocate packet memory for EP buffer/s calculate actual size * only for the OUT EPs. */ - MaxPacketSizeTmp = epd->wMaxPacketSize; - if (!USB_AllocateBuffer(&Offset, &MaxPacketSizeTmp, EP)) + size = epd->wMaxPacketSize; + if (!usb_alloc_buffer(&Offset, &size, EP)) return -USB_MEMORY_FULL; /* Set EP status */ @@ -811,7 +777,7 @@ static int usb_ep_configure(const usb_endpoint_descriptor_t *epd, bool enable) ep_hw->flags = 0; /* Set endpoint type */ - ep_hw->type = usb_endpoint_type(epd); + ep_hw->type = usb_endpointType(epd); /* Init EP max packet size */ ep_hw->max_size = epd->wMaxPacketSize; @@ -823,29 +789,31 @@ static int usb_ep_configure(const usb_endpoint_descriptor_t *epd, bool enable) hw += EP >> 1; /* Set Ep Address */ - EpCtrlSet_EA(hw, EP >> 1); + ep_ctrl_set_ea(hw, EP >> 1); ep_hw->hw = hw; + LOG_INFO("%s: EP%d-%s configured\n", + __func__, EP >> 1, EP & 1 ? "IN" : "OUT"); /* Low-level endpoint configuration */ - usb_ep_low_level_config(EP, Offset, MaxPacketSizeTmp); + usb_ep_low_level_config(EP, Offset, size); /* Set EP Kind & enable */ switch (ep_hw->type) { case USB_ENDPOINT_XFER_CONTROL: LOG_INFO("EP%d: CONTROL IN\n", EP >> 1); - EpCtrlSet_EP_TYPE(hw, EP_CTRL); - EpCtrlSet_EP_KIND(hw, 0); + ep_ctrl_set_ep_type(hw, EP_CTRL); + ep_ctrl_set_ep_kind(hw, 0); break; case USB_ENDPOINT_XFER_INT: LOG_INFO("EP%d: INTERRUPT IN\n", EP >> 1); - EpCtrlSet_EP_TYPE(hw, EP_INTERRUPT); - EpCtrlSet_EP_KIND(hw, 0); + ep_ctrl_set_ep_type(hw, EP_INTERRUPT); + ep_ctrl_set_ep_kind(hw, 0); break; case USB_ENDPOINT_XFER_BULK: LOG_INFO("EP%d: BULK IN\n", EP >> 1); - EpCtrlSet_EP_TYPE(hw, EP_BULK); - EpCtrlSet_EP_KIND(hw, 0); + ep_ctrl_set_ep_type(hw, EP_BULK); + ep_ctrl_set_ep_kind(hw, 0); break; case USB_ENDPOINT_XFER_ISOC: LOG_ERR("EP%d: ISOCHRONOUS IN: not supported\n", EP >> 1); @@ -857,14 +825,14 @@ static int usb_ep_configure(const usb_endpoint_descriptor_t *epd, bool enable) if (EP & 0x01) { /* Enable EP */ - EpCtrlSet_STAT_TX(hw, EP_NAK); + ep_ctrl_set_stat_tx(hw, EP_NAK); /* Clear Correct Transfer for transmission flag */ - EpCtrlClr_CTR_TX(hw); + ep_ctrl_clr_ctr_tx(hw); } else { /* Enable EP */ - EpCtrlSet_STAT_RX(hw, EP_VALID); + ep_ctrl_set_stat_rx(hw, EP_VALID); } } else if (ep_cnfg[EP].hw) @@ -876,40 +844,40 @@ static int usb_ep_configure(const usb_endpoint_descriptor_t *epd, bool enable) if (EP & 0x01) { /* Disable IN EP */ - EpCtrlSet_STAT_TX(hw, EP_DISABLED); + ep_ctrl_set_stat_tx(hw, EP_DISABLED); /* Clear Correct Transfer for reception flag */ - EpCtrlClr_CTR_TX(hw); + ep_ctrl_clr_ctr_tx(hw); } /* OUT EP */ else { /* Disable OUT EP */ - EpCtrlSet_STAT_RX(hw, EP_DISABLED); + ep_ctrl_set_stat_rx(hw, EP_DISABLED); /* Clear Correct Transfer for reception flag */ - EpCtrlClr_CTR_RX(hw); + ep_ctrl_clr_ctr_rx(hw); } /* Release buffer */ - USB_ReleaseBuffer(EP); + usb_free_buffer(EP); ep_cnfg[EP].hw = NULL; } return 0; } /* Get EP stall/unstall */ -static int USB_GetStallEP(int EP, bool *pStall) +static int usb_ep_get_stall(int EP, bool *pStall) { if (ep_cnfg[EP].hw == NULL) return -USB_NODEV_ERROR; *pStall = (EP & 0x01) ? - (EpCtrlGet_STAT_TX(ep_cnfg[EP].hw) == EP_STALL): /* IN EP */ - (EpCtrlGet_STAT_RX(ep_cnfg[EP].hw) == EP_STALL); /* OUT EP */ + (ep_ctrl_get_stat_tx(ep_cnfg[EP].hw) == EP_STALL): /* IN EP */ + (ep_ctrl_get_stat_rx(ep_cnfg[EP].hw) == EP_STALL); /* OUT EP */ return USB_OK; } /* Set EP stall/unstall */ -static int USB_SetStallEP(int EP, bool Stall) +static int usb_ep_set_stall(int EP, bool Stall) { if (ep_cnfg[EP].hw == NULL) return -USB_NODEV_ERROR; @@ -920,13 +888,13 @@ static int USB_SetStallEP(int EP, bool Stall) if (EP & 0x01) { /* IN EP */ - EpCtrlSet_STAT_TX(ep_cnfg[EP].hw, EP_STALL); + ep_ctrl_set_stat_tx(ep_cnfg[EP].hw, EP_STALL); ep_cnfg[EP].avail_data = 1; } else { /* OUT EP */ - EpCtrlSet_STAT_RX(ep_cnfg[EP].hw, EP_STALL); + ep_ctrl_set_stat_rx(ep_cnfg[EP].hw, EP_STALL); ep_cnfg[EP].avail_data = 0; } } @@ -938,31 +906,31 @@ static int USB_SetStallEP(int EP, bool Stall) /* IN EP */ ep_cnfg[EP].avail_data = 1; /* reset Data Toggle bit */ - EpCtrlSet_DTOG_TX(ep_cnfg[EP].hw, 0); - EpCtrlSet_STAT_TX(ep_cnfg[EP].hw, EP_NAK); + ep_ctrl_set_dtog_tx(ep_cnfg[EP].hw, 0); + ep_ctrl_set_stat_tx(ep_cnfg[EP].hw, EP_NAK); } else { /* OUT EP */ ep_cnfg[EP].avail_data = 0; /* reset Data Toggle bit */ - EpCtrlSet_DTOG_RX(ep_cnfg[EP].hw, 0); - EpCtrlSet_STAT_RX(ep_cnfg[EP].hw, EP_VALID); + ep_ctrl_set_dtog_rx(ep_cnfg[EP].hw, 0); + ep_ctrl_set_stat_rx(ep_cnfg[EP].hw, EP_VALID); } } return USB_OK; } /* Stall both directions of the control EP */ -static void USB_StallCtrlEP(void) +static void usb_ep_set_stall_ctrl(void) { ep_cnfg[CTRL_ENP_IN].avail_data = 1; ep_cnfg[CTRL_ENP_IN].status = STALLED; ep_cnfg[CTRL_ENP_OUT].avail_data = 0; ep_cnfg[CTRL_ENP_OUT].status = STALLED; - USB_SetStallEP(CTRL_ENP_IN, true); - USB_SetStallEP(CTRL_ENP_OUT, true); + usb_ep_set_stall(CTRL_ENP_IN, true); + usb_ep_set_stall(CTRL_ENP_OUT, true); } /* @@ -971,13 +939,13 @@ static void USB_StallCtrlEP(void) */ static int usb_find_interface(uint32_t num, uint32_t alt) { - usb_interface_descriptor_t *id; + const UsbInterfaceDesc *id; int i; for (i = 0; ; i++) { /* TODO: support more than one configuration per device */ - id = (usb_interface_descriptor_t *)usb_dev->config[i]; + id = (const UsbInterfaceDesc *)usb_dev->config[i]; if (id == NULL) break; if (id->bDescriptorType != USB_DT_INTERFACE) @@ -995,7 +963,7 @@ static int usb_find_interface(uint32_t num, uint32_t alt) static void usb_configure_ep_interface(unsigned int num, unsigned int alt, bool enable) { - usb_endpoint_descriptor_t *epd; + const UsbEndpointDesc *epd; int i, start; /* @@ -1018,9 +986,11 @@ usb_configure_ep_interface(unsigned int num, unsigned int alt, bool enable) */ for (i = start + 1; ; i++) { - epd = (usb_endpoint_descriptor_t *)usb_dev->config[i]; - if ((epd == NULL) || (epd->bDescriptorType != USB_DT_ENDPOINT)) + epd = (const UsbEndpointDesc *)usb_dev->config[i]; + if ((epd == NULL) || (epd->bDescriptorType == USB_DT_INTERFACE)) break; + if (epd->bDescriptorType != USB_DT_ENDPOINT) + continue; if (UNLIKELY(usb_ep_configure(epd, enable) < 0)) { LOG_ERR("%s: out of memory, can't initialize EP\n", @@ -1073,7 +1043,7 @@ static void usb_set_device_state(int state) } /* Setup packet: set address status phase end handler */ -static void USB_AddStatusEndHandler(UNUSED_ARG(int, EP)) +static void usb_add_status_handler_end(UNUSED_ARG(int, EP)) { uint16_t w_value; @@ -1091,76 +1061,197 @@ static void USB_AddStatusEndHandler(UNUSED_ARG(int, EP)) } /* Prepare status phase */ -static void USB_StatusPhase(bool in) +static void usb_status_phase(bool in) { if (in) __usb_ep_write(CTRL_ENP_IN, NULL, 0, NULL); } /* Setup packet: status phase end handler */ -static void USB_StatusEndHandler(UNUSED_ARG(int, EP)) +static void usb_status_handler_end(UNUSED_ARG(int, EP)) { __usb_ep_write(CTRL_ENP_IN, NULL, -1, NULL); __usb_ep_read(CTRL_ENP_OUT, NULL, -1, NULL); } /* Address status handler */ -static void USB_StatusHandler(UNUSED_ARG(int, EP)) +static void usb_status_handler(UNUSED_ARG(int, EP)) { if (setup_packet.mRequestType & USB_DIR_IN) { - USB_StatusPhase(false); - ep_cnfg[CTRL_ENP_OUT].complete = USB_StatusEndHandler; + usb_status_phase(false); + ep_cnfg[CTRL_ENP_OUT].complete = usb_status_handler_end; } else { - USB_StatusPhase(true); + usb_status_phase(true); ep_cnfg[CTRL_ENP_IN].complete = (setup_packet.bRequest == USB_REQ_SET_ADDRESS) ? - USB_AddStatusEndHandler : - USB_StatusEndHandler; + usb_add_status_handler_end : + usb_status_handler_end; + } +} + +static bool rx_done; +static size_t rx_size; +static uint8_t rx_buffer[_MIN(CONFIG_USB_RXBUFSIZE, USB_RX_MAX_SIZE)] + __attribute__ ((__aligned__(4))); + +static void usb_endpointRead_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_endpointRead(int ep, void *buffer, ssize_t size) +{ + int ep_num = usb_ep_logical_to_hw(ep); + ssize_t max_size = sizeof(rx_buffer); + + /* Non-blocking read for EP0 */ + if (ep_num == CTRL_ENP_OUT) + { + size = usb_size(size, usb_le16_to_cpu(setup_packet.wLength)); + if (UNLIKELY(size > max_size)) + { + LOG_ERR("%s: rx_buffer exceeded, try to enlarge CONFIG_USB_RXBUFSIZE\n", + __func__); + ASSERT(0); + return -USB_BUF_OVERFLOW; + } + if (!size) + usb_status_handler(ep_num); + else + { + __usb_ep_read(ep_num, rx_buffer, size, + usb_status_handler); + memcpy(buffer, rx_buffer, size); + } + return size; + } + if (UNLIKELY(!size)) + return 0; + size = MIN(size, max_size); + rx_done = false; + rx_size = 0; + + /* Blocking read */ + __usb_ep_read(ep_num, rx_buffer, size, usb_endpointRead_complete); + while (!rx_done) + cpu_relax(); + memcpy(buffer, rx_buffer, rx_size); + + return rx_size; +} + +static bool tx_done; +static size_t tx_size; +static uint8_t tx_buffer[_MIN(CONFIG_USB_TXBUFSIZE, USB_TX_MAX_SIZE)] + __attribute__ ((__aligned__(4))); + +static void usb_endpointWrite_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_endpointWrite(int ep, const void *buffer, ssize_t size) +{ + int ep_num = usb_ep_logical_to_hw(ep); + ssize_t max_size = sizeof(tx_buffer); + + /* Non-blocking write for EP0 */ + if (ep_num == CTRL_ENP_IN) + { + size = usb_size(size, usb_le16_to_cpu(setup_packet.wLength)); + if (UNLIKELY(size > max_size)) + { + LOG_ERR("%s: tx_buffer exceeded, try to enlarge CONFIG_USB_TXBUFSIZE\n", + __func__); + ASSERT(0); + return -USB_BUF_OVERFLOW; + } + if (!size) + usb_status_handler(ep_num); + else + { + memcpy(tx_buffer, buffer, size); + __usb_ep_write(ep_num, tx_buffer, size, + usb_status_handler); + } + return size; } + if (UNLIKELY(!size)) + return 0; + size = MIN(size, max_size); + tx_done = false; + tx_size = 0; + + /* Blocking write */ + memcpy(tx_buffer, buffer, size); + __usb_ep_write(ep_num, tx_buffer, size, usb_endpointWrite_complete); + while (!tx_done) + cpu_relax(); + + return tx_size; } /* Global variable to handle the following non-blocking I/O operations */ static uint32_t InData; /* Get device status */ -static int UsbDevStatus(uint16_t index) +static int usb_send_device_status(uint16_t index) { if (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(uint16_t), + usb_status_handler); return 0; } /* Get interface status */ -static int UsbInterfaceStatus(UNUSED_ARG(uint16_t, index)) +static int usb_send_interface_status(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(uint16_t), + usb_status_handler); return 0; } /* Get endpoint status */ -static int UsbEpStatus(uint16_t index) +static int usb_send_ep_status(uint16_t index) { if ((index & 0x7F) > 16) return -USB_NODEV_ERROR; InData = 0; - USB_GetStallEP(USB_EpLogToPhysAdd(index), (bool *)&InData); - __usb_ep_write(CTRL_ENP_IN, (uint8_t *)&InData, 2, USB_StatusHandler); - + usb_ep_get_stall(usb_ep_logical_to_hw(index), (bool *)&InData); + __usb_ep_write(CTRL_ENP_IN, + (uint8_t *)&InData, sizeof(uint16_t), + usb_status_handler); return 0; } /* USB setup packet: GET_STATUS request handler */ -static void USB_GetStatusHandler(void) +static void usb_get_status_handler(void) { uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue); uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex); @@ -1200,7 +1291,7 @@ static void USB_GetStatusHandler(void) switch (setup_packet.mRequestType & USB_RECIP_MASK) { case USB_RECIP_DEVICE: - if (UsbDevStatus(w_index) < 0) + if (usb_send_device_status(w_index) < 0) { LOG_WARN("%s: GET_STATUS: invalid UsbRecipientDevice\n", __func__); @@ -1211,7 +1302,7 @@ static void USB_GetStatusHandler(void) __func__, setup_packet.mRequestType); break; case USB_RECIP_INTERFACE: - if (UsbInterfaceStatus(w_index) < 0) + if (usb_send_interface_status(w_index) < 0) { LOG_WARN("%s: GET_STATUS: invalid UsbRecipientInterface\n", __func__); @@ -1222,7 +1313,7 @@ static void USB_GetStatusHandler(void) __func__, setup_packet.mRequestType); break; case USB_RECIP_ENDPOINT: - if (UsbEpStatus(w_index) < 0) + if (usb_send_ep_status(w_index) < 0) { LOG_WARN("%s: GET_STATUS: invalid UsbRecipientEndpoint\n", __func__); @@ -1240,20 +1331,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,18 +1339,25 @@ 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_StatusHandler); + usb_le16_to_cpu(setup_packet.wLength)), + usb_status_handler); return 0; } +/* + * TODO: refactor this part to remove this temporary buffer. + * + * It would be better to define all the USB descriptors in the right order and + * send them as a contiguous buffer directly from the flash / rodata memory. + */ #define USB_BUFSIZE (128) static uint8_t usb_cfg_buffer[USB_BUFSIZE]; +STATIC_ASSERT(USB_BUFSIZE < (1 << (sizeof(uint16_t) * 8))); static int usb_get_configuration_descriptor(int id) { - const usb_config_descriptor_t **config = - (const usb_config_descriptor_t **)usb_dev->config; + const UsbConfigDesc **config = + (const UsbConfigDesc **)usb_dev->config; uint8_t *p = usb_cfg_buffer; int i; @@ -1292,22 +1376,23 @@ static int usb_get_configuration_descriptor(int id) return -USB_BUF_OVERFLOW; } } - ((usb_config_descriptor_t *)usb_cfg_buffer)->wTotalLength = - usb_cpu_to_le16(p - usb_cfg_buffer); + ((UsbConfigDesc *)usb_cfg_buffer)->wTotalLength = + usb_cpu_to_le16((uint16_t)(p - usb_cfg_buffer)); __usb_ep_write(CTRL_ENP_IN, usb_cfg_buffer, usb_size(p - usb_cfg_buffer, - setup_packet.wLength), - USB_StatusHandler); + usb_le16_to_cpu(setup_packet.wLength)), + usb_status_handler); return 0; } static int usb_get_string_descriptor(unsigned int id) { - usb_string_descriptor_t *lang_str; + const UsbStringDesc *lang_str; unsigned int lang_id, str_id; uint16_t w_index_lo = usb_le16_to_cpu(setup_packet.wIndex) & 0x00ff; - uint16_t w_index_hi = (usb_le16_to_cpu(setup_packet.wIndex) & 0xff00) >> 8; + uint16_t w_index_hi = (usb_le16_to_cpu(setup_packet.wIndex) & + 0xff00) >> 8; ASSERT(usb_dev->strings != NULL); ASSERT(usb_dev->strings[0] != NULL); @@ -1318,8 +1403,8 @@ static int usb_get_string_descriptor(unsigned int id) /* Find Language index */ for (lang_id = 0; ; lang_id++) { - usb_string_descriptor_t *str = usb_dev->strings[lang_id]; - + const UsbStringDesc *str = + usb_dev->strings[lang_id]; if (UNLIKELY(str == NULL)) return -USB_NODEV_ERROR; if ((str->data[0] == w_index_lo) && @@ -1336,12 +1421,13 @@ 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_StatusHandler); + usb_size(lang_str->bLength, + usb_le16_to_cpu(setup_packet.wLength)), + usb_status_handler); return 0; } -static void UsbGetDescriptor(void) +static void usb_get_descriptor(void) { uint16_t w_value_lo = usb_le16_to_cpu(setup_packet.wValue) & 0x00ff; uint16_t w_value_hi = (usb_le16_to_cpu(setup_packet.wValue) & 0xff00) >> 8; @@ -1381,21 +1467,35 @@ static void UsbGetDescriptor(void) } } +/* USB setup packet: class/vendor request handler */ +static void usb_event_handler(UsbDevice *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) +static void usb_get_descriptor_handler(void) { LOG_INFO("%s: GET_DESCRIPTOR: RECIP = %d\n", __func__, setup_packet.mRequestType & USB_RECIP_MASK); if ((setup_packet.mRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) - UsbGetDescriptor(); + usb_get_descriptor(); + /* 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; } /* USB setup packet: SET_ADDRESS handler */ -static void USB_SetAddressHandler(void) +static void usb_set_address_handler(void) { uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue); uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex); @@ -1407,13 +1507,13 @@ static void USB_SetAddressHandler(void) ((setup_packet.mRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) && (w_index == 0) && (w_length == 0) && (w_value < 128)) - USB_StatusHandler(CTRL_ENP_IN); + usb_status_handler(CTRL_ENP_IN); else ep_cnfg[CTRL_ENP_OUT].status = STALLED; } /* USB setup packet: GET_CONFIGURATION handler */ -static void USB_GetConfigurationHandler(void) +static void usb_get_config_handler(void) { uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue); uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex); @@ -1423,20 +1523,20 @@ static void USB_GetConfigurationHandler(void) (w_value == 0) && (w_index == 0) && (w_value == 1)) { InData = udc.cfg_id; - __usb_ep_write(CTRL_ENP_IN, (uint8_t *)&InData, 1, USB_StatusHandler); + __usb_ep_write(CTRL_ENP_IN, (uint8_t *)&InData, 1, usb_status_handler); } else ep_cnfg[CTRL_ENP_OUT].status = STALLED; } -static const usb_config_descriptor_t *usb_find_configuration(int num) +static const UsbConfigDesc *usb_find_configuration(int num) { - const usb_config_descriptor_t *cfg; + const UsbConfigDesc *cfg; int i; for (i = 0; ; i++) { - cfg = (const usb_config_descriptor_t *)usb_dev->config[i]; + cfg = (const UsbConfigDesc *)usb_dev->config[i]; if (cfg == NULL) break; if (cfg->bDescriptorType != USB_DT_CONFIG) @@ -1447,15 +1547,15 @@ static const usb_config_descriptor_t *usb_find_configuration(int num) return NULL; } -static int UsbSetConfigurationState(uint32_t Configuration) +static int usb_set_config_state(uint32_t conf) { - const usb_config_descriptor_t *pCnfg; + const UsbConfigDesc *pCnfg; unsigned int i; - if (Configuration) + if (conf) { /* Find configuration descriptor */ - pCnfg = usb_find_configuration(Configuration); + pCnfg = usb_find_configuration(conf); if (pCnfg == NULL) return -USB_NODEV_ERROR; @@ -1465,7 +1565,7 @@ static int UsbSetConfigurationState(uint32_t Configuration) udc.cfg = pCnfg; /* Set Interface and Alternative Setting */ - udc.cfg_id = Configuration; + udc.cfg_id = conf; /* Set self-powered state */ if (pCnfg->bmAttributes & USB_CONFIG_ATT_SELFPOWER) udc.feature |= STM32_UDC_FEATURE_SELFPOWERED; @@ -1477,6 +1577,7 @@ static int UsbSetConfigurationState(uint32_t Configuration) udc.alt[i] = 0; usb_set_device_state(USB_STATE_CONFIGURED); usb_dev->configured = true; + LOG_INFO("%s: device configured\n", __func__); } else { @@ -1487,7 +1588,7 @@ static int UsbSetConfigurationState(uint32_t Configuration) } /* USB setup packet: SET_CONFIGURATION handler */ -static void USB_SetConfigurationHandler(void) +static void usb_set_config_handler(void) { uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue); uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex); @@ -1497,19 +1598,19 @@ static void USB_SetConfigurationHandler(void) __func__, w_value); if ((udc.state >= USB_STATE_ADDRESS) && (w_index == 0) && (w_length == 0) && - (UsbSetConfigurationState(w_value & 0xff) == 0)) - USB_StatusHandler(CTRL_ENP_OUT); + (usb_set_config_state(w_value & 0xff) == 0)) + usb_status_handler(CTRL_ENP_OUT); else ep_cnfg[CTRL_ENP_OUT].status = STALLED; } /* USB setup packet: standard request handler */ -static void USB_StandardRequestHandler(void) +static void usb_standard_request_handler(void) { switch (setup_packet.bRequest) { case USB_REQ_GET_STATUS: - USB_GetStatusHandler(); + usb_get_status_handler(); break; case USB_REQ_CLEAR_FEATURE: LOG_INFO("%s: bRequest=%d (CLEAR_FEATURE)\n", @@ -1520,20 +1621,20 @@ static void USB_StandardRequestHandler(void) __func__, setup_packet.bRequest); break; case USB_REQ_SET_ADDRESS: - USB_SetAddressHandler(); + usb_set_address_handler(); break; case USB_REQ_GET_DESCRIPTOR: - UBS_GetDescriptorHandler(); + usb_get_descriptor_handler(); break; case USB_REQ_SET_DESCRIPTOR: LOG_INFO("%s: bRequest=%d (SET_DESCRIPTOR)\n", __func__, setup_packet.bRequest); break; case USB_REQ_GET_CONFIGURATION: - USB_GetConfigurationHandler(); + usb_get_config_handler(); break; case USB_REQ_SET_CONFIGURATION: - USB_SetConfigurationHandler(); + usb_set_config_handler(); break; case USB_REQ_GET_INTERFACE: LOG_INFO("%s: bRequest=%d (GET_INTERFACE)\n", @@ -1556,7 +1657,7 @@ static void USB_StandardRequestHandler(void) } /* USB setup packet handler */ -static void USB_SetupHandler(void) +static void usb_setup_handler(void) { switch (setup_packet.mRequestType & USB_TYPE_MASK) { @@ -1564,17 +1665,19 @@ static void USB_SetupHandler(void) case USB_TYPE_STANDARD: LOG_INFO("%s: bmRequestType=%02x (Standard)\n", __func__, setup_packet.mRequestType); - USB_StandardRequestHandler(); + usb_standard_request_handler(); break; /* Class */ case USB_TYPE_CLASS: LOG_INFO("%s: bmRequestType=%02x (Class)\n", __func__, setup_packet.mRequestType); + usb_event_handler(usb_dev); break; /* Vendor */ case USB_TYPE_VENDOR: LOG_INFO("%s: bmRequestType=%02x (Vendor)\n", __func__, setup_packet.mRequestType); + usb_event_handler(usb_dev); break; case USB_TYPE_RESERVED: LOG_INFO("%s: bmRequestType=%02x (Reserved)\n", @@ -1589,6 +1692,7 @@ static void USB_SetupHandler(void) } } +/* USB: low-level hardware initialization */ static void usb_hw_reset(void) { unsigned int i; @@ -1599,10 +1703,10 @@ static void usb_hw_reset(void) ep_cnfg[i].hw = NULL; /* Initialize USB memory */ - for (i = 0; i < countof(PacketMemBuff); i++) - PacketMemBuff[i].Size = 0; + for (i = 0; i < countof(memory_buffer); i++) + memory_buffer[i].Size = 0; usb->BTABLE = USB_BDT_OFFSET; - pPacketMemUse = NULL; + mem_use = NULL; /* Endpoint initialization */ ret = usb_ep_configure(&USB_CtrlEpDescr0, true); @@ -1624,13 +1728,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 */ @@ -1647,13 +1746,13 @@ static void usb_isr_correct_transfer(stm32_usb_irq_status_t interrupt) ASSERT(ep_cnfg[EP].hw); /* IN EP */ if (EP & 0x01) - EpCtrlClr_CTR_TX(ep_cnfg[EP].hw); + ep_ctrl_clr_ctr_tx(ep_cnfg[EP].hw); else - EpCtrlClr_CTR_RX(ep_cnfg[EP].hw); + ep_ctrl_clr_ctr_rx(ep_cnfg[EP].hw); if (EP == CTRL_ENP_OUT) { /* Determinate type of packet (only for control EP) */ - bool SetupPacket = EpCtrlGet_SETUP(ep_cnfg[CTRL_ENP_OUT].hw); + bool SetupPacket = ep_ctrl_get_setup(ep_cnfg[CTRL_ENP_OUT].hw); if (SetupPacket) { @@ -1665,10 +1764,10 @@ static void usb_isr_correct_transfer(stm32_usb_irq_status_t interrupt) /* reset EP IO ctrl */ if (setup_packet.mRequestType & USB_DIR_IN) - USB_StatusHandler(CTRL_ENP_OUT); - USB_SetupHandler(); + usb_status_handler(CTRL_ENP_OUT); + usb_setup_handler(); if (ep_cnfg[CTRL_ENP_OUT].status == STALLED) - USB_StallCtrlEP(); + usb_ep_set_stall_ctrl(); } else { @@ -1720,9 +1819,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) @@ -1804,7 +1907,7 @@ static void usb_init(void) } /* Register an upper layer USB device into the driver */ -int usb_device_register(struct usb_device *dev) +int usb_deviceRegister(UsbDevice *dev) { #if CONFIG_KERN MOD_CHECK(proc);