From 8acfdb97b59c9414b24d2b04b07829c68b5b57dc Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 24 Dec 2020 15:35:50 +0100 Subject: Fix ALL the bugs --- fw/src/usbd_hid.c | 147 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 89 insertions(+), 58 deletions(-) (limited to 'fw/src/usbd_hid.c') diff --git a/fw/src/usbd_hid.c b/fw/src/usbd_hid.c index 4d313b4..eaf7878 100644 --- a/fw/src/usbd_hid.c +++ b/fw/src/usbd_hid.c @@ -10,6 +10,7 @@ static uint8_t *USBD_HID_GetCfgDesc (uint16_t *length); static uint8_t *USBD_HID_GetDeviceQualifierDesc (uint16_t *length); static uint8_t USBD_HID_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum); static uint8_t USBD_HID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum); +static uint8_t USBD_HID_EP0_RxReady(USBD_HandleTypeDef *pdev); USBD_ClassTypeDef USBD_HID = { @@ -17,7 +18,7 @@ USBD_ClassTypeDef USBD_HID = USBD_HID_DeInit, USBD_HID_Setup, NULL, /* EP0_TxSent */ - NULL, /* EP0_RxReady */ + USBD_HID_EP0_RxReady, USBD_HID_DataIn, USBD_HID_DataOut, NULL, /* SOF */ @@ -233,64 +234,73 @@ static uint8_t USBD_HID_Setup (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef * switch (req->bmRequest & USB_REQ_TYPE_MASK) { - case USB_REQ_TYPE_CLASS : - switch (req->bRequest) { - case HID_REQ_SET_PROTOCOL: - hhid->Protocol = (uint8_t)(req->wValue); - break; - - case HID_REQ_GET_PROTOCOL: - USBD_CtlSendData (pdev, - (uint8_t *)&hhid->Protocol, - 1); - break; - - case HID_REQ_SET_IDLE: - hhid->IdleState = (uint8_t)(req->wValue >> 8); - break; - - case HID_REQ_GET_IDLE: - USBD_CtlSendData (pdev, - (uint8_t *)&hhid->IdleState, - 1); - break; - - default: - USBD_CtlError (pdev, req); - return USBD_FAIL; - } - break; - - case USB_REQ_TYPE_STANDARD: - switch (req->bRequest) { - case USB_REQ_GET_DESCRIPTOR: - if( req->wValue >> 8 == HID_REPORT_DESC) - { - len = MIN(HID_REPORT_DESC_SIZE , req->wLength); - pbuf = HID_ReportDesc; + case USB_REQ_TYPE_CLASS : + switch (req->bRequest) { + case HID_REQ_SET_PROTOCOL: + hhid->Protocol = (uint8_t)(req->wValue); + break; + + case HID_REQ_GET_PROTOCOL: + USBD_CtlSendData (pdev, + (uint8_t *)&hhid->Protocol, + 1); + break; + + case HID_REQ_SET_IDLE: + hhid->IdleState = (uint8_t)(req->wValue >> 8); + break; + + case HID_REQ_GET_IDLE: + USBD_CtlSendData (pdev, + (uint8_t *)&hhid->IdleState, + 1); + break; + + case HID_REQ_SET_REPORT: + hhid->IsReportAvailable = 1U; + if (req->wLength > sizeof(hhid->Report_buf)) { + USBD_CtlError (pdev, req); + return USBD_FAIL; + } + (void)USBD_CtlPrepareRx(pdev, hhid->Report_buf, req->wLength); + break; + + default: + USBD_CtlError (pdev, req); + return USBD_FAIL; } - else if( req->wValue >> 8 == HID_DESCRIPTOR_TYPE) - { - pbuf = USBD_HID_Desc; - len = MIN(USB_HID_DESC_SIZ , req->wLength); - } - - USBD_CtlSendData (pdev, - pbuf, - len); - - break; - - case USB_REQ_GET_INTERFACE : - USBD_CtlSendData (pdev, - (uint8_t *)&hhid->AltSetting, - 1); break; - case USB_REQ_SET_INTERFACE : - hhid->AltSetting = (uint8_t)(req->wValue); - break; - } + case USB_REQ_TYPE_STANDARD: + switch (req->bRequest) { + case USB_REQ_GET_DESCRIPTOR: + if( req->wValue >> 8 == HID_REPORT_DESC) + { + len = MIN(HID_REPORT_DESC_SIZE , req->wLength); + pbuf = HID_ReportDesc; + } + else if( req->wValue >> 8 == HID_DESCRIPTOR_TYPE) + { + pbuf = USBD_HID_Desc; + len = MIN(USB_HID_DESC_SIZ , req->wLength); + } + + USBD_CtlSendData (pdev, + pbuf, + len); + + break; + + case USB_REQ_GET_INTERFACE : + USBD_CtlSendData (pdev, + (uint8_t *)&hhid->AltSetting, + 1); + break; + + case USB_REQ_SET_INTERFACE : + hhid->AltSetting = (uint8_t)(req->wValue); + break; + } } return USBD_OK; } @@ -302,8 +312,8 @@ uint8_t USBD_HID_SendReport (USBD_HandleTypeDef *pdev, uint8_t *report, uint16_ if (pdev->dev_state != USBD_STATE_CONFIGURED ) return USBD_OK; - if(hhid->state == HID_IDLE) - return USBD_OK; + if(hhid->state != HID_IDLE) + return USBD_BUSY; hhid->state = HID_BUSY; USBD_LL_Transmit(pdev, HID_EPIN_ADDR, report, len); @@ -371,3 +381,24 @@ static uint8_t *USBD_HID_GetDeviceQualifierDesc (uint16_t *length) return USBD_HID_DeviceQualifierDesc; } +/** + * @brief USBD_CUSTOM_HID_EP0_RxReady + * Handles control request data. + * @param pdev: device instance + * @retval status + */ +static uint8_t USBD_HID_EP0_RxReady(USBD_HandleTypeDef *pdev) +{ + USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef*) pdev->pClassData; + + if (!hhid) + return (uint8_t)USBD_FAIL; + + if (hhid->IsReportAvailable == 1U) { + /* FIXME ignore for now */ + hhid->IsReportAvailable = 0U; + } + + return (uint8_t)USBD_OK; +} + -- cgit