From 4d9e26b6ef6f205fa071160e5813462a921d323d Mon Sep 17 00:00:00 2001 From: Robert Fisk Date: Wed, 13 Apr 2016 01:07:01 +1200 Subject: [PATCH] Upstream HID bugfixes. --- Downstream/Inc/downstream_hid.h | 3 -- Downstream/Src/downstream_hid.c | 6 ++- Upstream/Inc/upstream_hid.h | 5 +- .../Class/HID/Inc/usbd_hid.h | 10 ++-- .../Class/HID/Src/usbd_hid.c | 46 ++++--------------- .../Core/Src/usbd_core.c | 5 +- Upstream/Src/upstream_hid.c | 37 +++++++++++---- Upstream/Src/upstream_statemachine.c | 3 +- 8 files changed, 58 insertions(+), 57 deletions(-) diff --git a/Downstream/Inc/downstream_hid.h b/Downstream/Inc/downstream_hid.h index 1f80859..e3c3bbd 100644 --- a/Downstream/Inc/downstream_hid.h +++ b/Downstream/Inc/downstream_hid.h @@ -17,9 +17,6 @@ #include "downstream_spi.h" -#define HID_MOUSE_TRANSFER_BYTES 5 -#define HID_KEYBOARD_TRANSFER_BYTES 0 - InterfaceCommandClassTypeDef Downstream_HID_ApproveConnectedDevice(void); diff --git a/Downstream/Src/downstream_hid.c b/Downstream/Src/downstream_hid.c index 27d1843..b941663 100644 --- a/Downstream/Src/downstream_hid.c +++ b/Downstream/Src/downstream_hid.c @@ -17,6 +17,10 @@ +#define HID_MOUSE_DATA_LEN 4 +#define HID_KEYBOARD_DATA_LEN 0 + + extern USBH_HandleTypeDef hUsbHostFS; //Hard-link ourselves to usb_host.c extern InterfaceCommandClassTypeDef ConfiguredDeviceClass; //Do a cheap hard-link to downstream_statemachine.c, rather than keep a duplicate here @@ -61,7 +65,7 @@ void Downstream_HID_InterruptReportCallback(DownstreamPacketTypeDef* packetToSen { if (ConfiguredDeviceClass == COMMAND_CLASS_HID_MOUSE) { - packetToSend->Length16 = (HID_MOUSE_TRANSFER_BYTES / 2) + DOWNSTREAM_PACKET_HEADER_LEN_16; + packetToSend->Length16 = ((HID_MOUSE_DATA_LEN + 1) / 2) + DOWNSTREAM_PACKET_HEADER_LEN_16; } //else if... else diff --git a/Upstream/Inc/upstream_hid.h b/Upstream/Inc/upstream_hid.h index fa5d5fd..c3c0d03 100644 --- a/Upstream/Inc/upstream_hid.h +++ b/Upstream/Inc/upstream_hid.h @@ -20,8 +20,9 @@ #include "upstream_interface_def.h" -typedef uint8_t UpstreamHidSendReportCallback(uint8_t *report, - uint16_t len); +typedef uint8_t (*UpstreamHidSendReportCallback)(uint8_t *report, + uint16_t len); + void Upstream_HID_Init(InterfaceCommandClassTypeDef newClass); diff --git a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Inc/usbd_hid.h b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Inc/usbd_hid.h index e0a1e25..2aef166 100644 --- a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Inc/usbd_hid.h +++ b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Inc/usbd_hid.h @@ -113,8 +113,8 @@ USBD_HID_HandleTypeDef; * @{ */ -extern USBD_ClassTypeDef USBD_HID_Mouse; -extern USBD_ClassTypeDef USBD_HID_Keyboard; +extern USBD_ClassTypeDef USBD_HID; + //#define USBD_HID_CLASS &USBD_HID /** @@ -124,11 +124,13 @@ extern USBD_ClassTypeDef USBD_HID_Keyboard; /** @defgroup USB_CORE_Exported_Functions * @{ */ -uint8_t USBD_HID_SendReport (uint8_t *report, - uint16_t len); uint32_t USBD_HID_GetPollingInterval (USBD_HandleTypeDef *pdev); +void USBD_HID_PreinitMouse(void); +void USBD_HID_PreinitKeyboard(void); + + /** * @} */ diff --git a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c index 3925e7e..c60c011 100644 --- a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c +++ b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c @@ -55,12 +55,6 @@ -static uint8_t USBD_HID_InitMouse(USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_HID_InitKeyboard(USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - static uint8_t USBD_HID_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx); @@ -76,11 +70,14 @@ 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_SendReport (uint8_t *report, + uint16_t len); + -USBD_ClassTypeDef USBD_HID_Mouse = +USBD_ClassTypeDef USBD_HID = { - USBD_HID_InitMouse, + USBD_HID_Init, USBD_HID_DeInit, USBD_HID_Setup, NULL, /*EP0_TxSent*/ @@ -90,26 +87,9 @@ USBD_ClassTypeDef USBD_HID_Mouse = NULL, /*SOF */ NULL, NULL, - USBD_HID_GetCfgDesc, - USBD_HID_GetCfgDesc, - USBD_HID_GetCfgDesc, - USBD_HID_GetDeviceQualifierDesc, -}; - -USBD_ClassTypeDef USBD_HID_Keyboard = -{ - USBD_HID_InitKeyboard, - USBD_HID_DeInit, - USBD_HID_Setup, - NULL, /*EP0_TxSent*/ - NULL, /*EP0_RxReady*/ - USBD_HID_DataIn, /*DataIn*/ - NULL, /*DataOut*/ - NULL, /*SOF */ NULL, - NULL, USBD_HID_GetCfgDesc, - USBD_HID_GetCfgDesc, + USBD_HID_GetCfgDesc, USBD_HID_GetCfgDesc, USBD_HID_GetDeviceQualifierDesc, }; @@ -155,7 +135,7 @@ __ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_ 0x00, /*bCountryCode: Hardware target country*/ 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ 0x22, /*bDescriptorType*/ - HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ + 0x00, //HID_MOUSE_REPORT_DESC_SIZE, /*wItemLength: Total length of Report descriptor*/ 0x00, /******************** Descriptor of Mouse endpoint ********************/ /* 27 */ @@ -180,7 +160,7 @@ __ALIGN_BEGIN static uint8_t USBD_HID_Desc[USB_HID_DESC_SIZ] __ALIGN_END = 0x00, /*bCountryCode: Hardware target country*/ 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ 0x22, /*bDescriptorType*/ - HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ + 0x00, //HID_MOUSE_REPORT_DESC_SIZE, /*wItemLength: Total length of Report descriptor*/ 0x00, }; @@ -264,18 +244,13 @@ uint8_t ActiveReportDescriptorLength; * @param cfgidx: Configuration index * @retval status */ -static uint8_t USBD_HID_InitMouse(USBD_HandleTypeDef *pdev, - uint8_t cfgidx) +void USBD_HID_PreinitMouse(void) { ActiveReportDescriptor = HID_MOUSE_ReportDesc; ActiveReportDescriptorLength = HID_MOUSE_REPORT_DESC_SIZE; USBD_HID_CfgDesc[USB_HID_CFGDESC__HID_REPORT_DESC_SIZE_OFFSET] = HID_MOUSE_REPORT_DESC_SIZE; USBD_HID_Desc[USB_HID_DESC__HID_REPORT_DESC_SIZE_OFFSET] = HID_MOUSE_REPORT_DESC_SIZE; - - Upstream_HID_Init(COMMAND_CLASS_HID_MOUSE); - - return USBD_HID_Init(pdev, cfgidx); } @@ -396,7 +371,7 @@ static uint8_t USBD_HID_Setup (USBD_HandleTypeDef *pdev, if( req->wValue >> 8 == HID_REPORT_DESC) { len = MIN(ActiveReportDescriptorLength , req->wLength); - pbuf = HID_MOUSE_ReportDesc; + pbuf = ActiveReportDescriptor; } else if( req->wValue >> 8 == HID_DESCRIPTOR_TYPE) { @@ -407,7 +382,6 @@ static uint8_t USBD_HID_Setup (USBD_HandleTypeDef *pdev, USBD_CtlSendData (pdev, pbuf, len); - break; case USB_REQ_GET_INTERFACE : diff --git a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c index 6e00101..8353a12 100755 --- a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c +++ b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c @@ -548,7 +548,10 @@ USBD_StatusTypeDef USBD_DevDisconnected(USBD_HandleTypeDef *pdev) USBD_StatusTypeDef USBD_BufferFreed(USBD_HandleTypeDef *pdev) { - pdev->pClass->FreeDataBuffer(pdev); + if(pdev->pClass->FreeDataBuffer != NULL) + { + pdev->pClass->FreeDataBuffer(pdev); + } return USBD_OK; } /** diff --git a/Upstream/Src/upstream_hid.c b/Upstream/Src/upstream_hid.c index 6a48b2d..2de79ad 100644 --- a/Upstream/Src/upstream_hid.c +++ b/Upstream/Src/upstream_hid.c @@ -14,15 +14,17 @@ #include "upstream_hid.h" #include "upstream_spi.h" #include "upstream_interface_def.h" -#include "usbd_hid.h" -#define HID_MOUSE_REPORT_LEN 4 +#define HID_REPORT_DATA_LEN 8 +#define HID_MOUSE_DATA_LEN 4 InterfaceCommandClassTypeDef ActiveHidClass = COMMAND_CLASS_INTERFACE; UpstreamPacketTypeDef* UpstreamHidPacket = NULL; +UpstreamHidSendReportCallback ReportCallback = NULL; + void Upstream_HID_GetNextReportReceiveCallback(UpstreamPacketTypeDef* receivedPacket); @@ -74,6 +76,13 @@ void Upstream_HID_GetNextReport(UpstreamHidSendReportCallback callback) UpstreamHidPacket = NULL; } + if (ReportCallback != NULL) + { + UPSTREAM_SPI_FREAKOUT; + return; + } + ReportCallback = callback; + //Get next packet Upstream_SetExpectedReceivedCommand(ActiveHidClass, COMMAND_HID_REPORT); Upstream_ReceivePacket(Upstream_HID_GetNextReportReceiveCallback); @@ -82,9 +91,12 @@ void Upstream_HID_GetNextReport(UpstreamHidSendReportCallback callback) void Upstream_HID_GetNextReportReceiveCallback(UpstreamPacketTypeDef* receivedPacket) { - uint8_t reportLength = 0; + UpstreamHidSendReportCallback tempReportCallback; + uint8_t dataLength = 0; + uint8_t i; - if (UpstreamHidPacket != NULL) + if ((UpstreamHidPacket != NULL) || + (ReportCallback == NULL)) { UPSTREAM_SPI_FREAKOUT; return; @@ -98,7 +110,7 @@ void Upstream_HID_GetNextReportReceiveCallback(UpstreamPacketTypeDef* receivedPa if (ActiveHidClass == COMMAND_CLASS_HID_MOUSE) { - if (receivedPacket->Length16 != (UPSTREAM_PACKET_HEADER_LEN_16 + ((HID_MOUSE_REPORT_LEN + 1) / 2))) + if (receivedPacket->Length16 != (UPSTREAM_PACKET_HEADER_LEN_16 + ((HID_MOUSE_DATA_LEN + 1) / 2))) { UPSTREAM_SPI_FREAKOUT; return; @@ -106,20 +118,27 @@ void Upstream_HID_GetNextReportReceiveCallback(UpstreamPacketTypeDef* receivedPa //Mouse sanity checks & stuff go here... - reportLength = HID_MOUSE_REPORT_LEN; + dataLength = HID_MOUSE_DATA_LEN; } //Other HID classes go here... - if (reportLength == 0) + if (dataLength == 0) { UPSTREAM_SPI_FREAKOUT; return; } - UpstreamHidPacket = receivedPacket; //Save packet so we can free it when upstream USB transaction is done - USBD_HID_SendReport(receivedPacket->Data, reportLength); + for (i = dataLength; i > HID_REPORT_DATA_LEN; i++) + { + receivedPacket->Data[i] = 0; //Zero out unused bytes before we send report upstream + } + + UpstreamHidPacket = receivedPacket; //Save packet so we can free it when upstream USB transaction is done + tempReportCallback = ReportCallback; + ReportCallback = NULL; + tempReportCallback(receivedPacket->Data, HID_REPORT_DATA_LEN); } diff --git a/Upstream/Src/upstream_statemachine.c b/Upstream/Src/upstream_statemachine.c index 0b38360..d43b6c0 100644 --- a/Upstream/Src/upstream_statemachine.c +++ b/Upstream/Src/upstream_statemachine.c @@ -177,7 +177,8 @@ void Upstream_StateMachine_NotifyDeviceReplyCallback(UpstreamPacketTypeDef* repl case COMMAND_CLASS_HID_MOUSE: newActiveClass = COMMAND_CLASS_HID_MOUSE; - newClassPointer = &USBD_HID_Mouse; + newClassPointer = &USBD_HID; + USBD_HID_PreinitMouse(); break; //Add other supported classes here...