From 7037aa6052b2f23a7240d52c0b98791dcae8fa30 Mon Sep 17 00:00:00 2001 From: Robert Fisk Date: Tue, 16 Jan 2018 00:21:27 +1300 Subject: [PATCH] Better support for multi-interface HID: Logitech unifying receivers - Select mouse interface first - Correctly specify active interface when retrieving HID descriptors --- Downstream/Inc/usbh_config.h | 2 +- .../Class/HID/Src/usbh_hid.c | 50 +++++++------------ .../Core/Inc/usbh_ctlreq.h | 3 +- .../Core/Src/usbh_ctlreq.c | 24 ++++----- 4 files changed, 31 insertions(+), 48 deletions(-) diff --git a/Downstream/Inc/usbh_config.h b/Downstream/Inc/usbh_config.h index c9d4812..a6ffce4 100644 --- a/Downstream/Inc/usbh_config.h +++ b/Downstream/Inc/usbh_config.h @@ -53,7 +53,7 @@ #define USBH_MAX_NUM_ENDPOINTS 2 /*---------- -----------*/ -#define USBH_MAX_NUM_INTERFACES 2 +#define USBH_MAX_NUM_INTERFACES 3 /*---------- -----------*/ #define USBH_MAX_NUM_CONFIGURATION 1 diff --git a/Downstream/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid.c b/Downstream/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid.c index 0f4078a..9062efd 100644 --- a/Downstream/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid.c +++ b/Downstream/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid.c @@ -141,44 +141,30 @@ static USBH_StatusTypeDef USBH_HID_InterfaceInit (USBH_HandleTypeDef *phost) { uint8_t max_ep; uint8_t num = 0; - uint8_t interface; + uint8_t interface = 0xFF; - USBH_StatusTypeDef status = USBH_FAIL ; HID_HandleTypeDef *HID_Handle; + - interface = USBH_FindInterface(phost, phost->pActiveClass->ClassCode, HID_BOOT_CODE, 0xFF); +#ifdef CONFIG_MOUSE_ENABLED + interface = USBH_FindInterface(phost, phost->pActiveClass->ClassCode, HID_BOOT_CODE, HID_MOUSE_BOOT_CODE); //Search for mouse interfaces first +#endif +#ifdef CONFIG_KEYBOARD_ENABLED + if (interface == 0xFF) + { + interface = USBH_FindInterface(phost, phost->pActiveClass->ClassCode, HID_BOOT_CODE, HID_KEYBRD_BOOT_CODE); + } +#endif if(interface == 0xFF) /* No Valid Interface */ { - status = USBH_FAIL; - USBH_DbgLog ("Cannot Find the interface for %s class.", phost->pActiveClass->Name); + USBH_DbgLog ("Cannot Find the interface for %s class.", phost->pActiveClass->Name); + return USBH_FAIL; } - else - { + USBH_SelectInterface (phost, interface); phost->pActiveClass->pData = (HID_HandleTypeDef *)USBH_malloc (sizeof(HID_HandleTypeDef)); HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; - HID_Handle->state = HID_ERROR; - - /*Decode Bootclass Protocol: Mouse or Keyboard*/ -#ifdef CONFIG_KEYBOARD_ENABLED - if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol == HID_KEYBRD_BOOT_CODE) - { - USBH_UsrLog ("KeyBoard device found!"); - } - else -#endif -#ifdef CONFIG_MOUSE_ENABLED - if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol == HID_MOUSE_BOOT_CODE) - { - USBH_UsrLog ("Mouse device found!"); - } - else -#endif - { - USBH_UsrLog ("Protocol not supported."); - return USBH_FAIL; - } HID_Handle->state = HID_INIT; HID_Handle->ctl_state = HID_REQ_INIT; @@ -245,9 +231,7 @@ static USBH_StatusTypeDef USBH_HID_InterfaceInit (USBH_HandleTypeDef *phost) } } - status = USBH_OK; - } - return status; + return USBH_OK; } /** @@ -488,7 +472,8 @@ USBH_StatusTypeDef USBH_HID_GetHIDReportDescriptor (USBH_HandleTypeDef *phost, status = USBH_GetDescriptor(phost, USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD, - USB_DESC_HID_REPORT, + USB_DESC_HID_REPORT, + phost->device.current_interface, phost->device.Data, length); @@ -524,6 +509,7 @@ USBH_StatusTypeDef USBH_HID_GetHIDDescriptor (USBH_HandleTypeDef *phost, status = USBH_GetDescriptor( phost, USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD, USB_DESC_HID, + phost->device.current_interface, phost->device.Data, length); diff --git a/Downstream/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_ctlreq.h b/Downstream/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_ctlreq.h index c86c1a4..46523e0 100644 --- a/Downstream/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_ctlreq.h +++ b/Downstream/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_ctlreq.h @@ -102,7 +102,8 @@ USBH_StatusTypeDef USBH_CtlReq (USBH_HandleTypeDef *phost, USBH_StatusTypeDef USBH_GetDescriptor(USBH_HandleTypeDef *phost, uint8_t req_type, - uint16_t value_idx, + uint16_t type_idx, + uint16_t value, uint8_t* buff, uint16_t length ); diff --git a/Downstream/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_ctlreq.c b/Downstream/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_ctlreq.c index 42d271d..4e42c37 100644 --- a/Downstream/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_ctlreq.c +++ b/Downstream/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_ctlreq.c @@ -123,7 +123,8 @@ USBH_StatusTypeDef USBH_Get_DevDesc(USBH_HandleTypeDef *phost, uint16_t length) if((status = USBH_GetDescriptor(phost, USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD, - USB_DESC_DEVICE, + USB_DESC_DEVICE, + 0, phost->device.Data, length)) == USBH_OK) { @@ -166,7 +167,8 @@ USBH_StatusTypeDef USBH_Get_CfgDesc(USBH_HandleTypeDef *phost, #endif if((status = USBH_GetDescriptor(phost, USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD, - USB_DESC_CONFIGURATION, + USB_DESC_CONFIGURATION, + 0, pData, length)) == USBH_OK) { @@ -204,7 +206,8 @@ USBH_StatusTypeDef USBH_Get_StringDesc(USBH_HandleTypeDef *phost, if((status = USBH_GetDescriptor(phost, USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD, - USB_DESC_STRING | string_index, + USB_DESC_STRING | string_index, + 0x0409, phost->device.Data, length)) == USBH_OK) { @@ -227,7 +230,8 @@ USBH_StatusTypeDef USBH_Get_StringDesc(USBH_HandleTypeDef *phost, */ USBH_StatusTypeDef USBH_GetDescriptor(USBH_HandleTypeDef *phost, uint8_t req_type, - uint16_t value_idx, + uint16_t type_idx, + uint16_t value, uint8_t* buff, uint16_t length ) { @@ -235,16 +239,8 @@ USBH_StatusTypeDef USBH_GetDescriptor(USBH_HandleTypeDef *phost, { phost->Control.setup.b.bmRequestType = USB_D2H | req_type; phost->Control.setup.b.bRequest = USB_REQ_GET_DESCRIPTOR; - phost->Control.setup.b.wValue.w = value_idx; - - if ((value_idx & 0xff00) == USB_DESC_STRING) - { - phost->Control.setup.b.wIndex.w = 0x0409; - } - else - { - phost->Control.setup.b.wIndex.w = 0; - } + phost->Control.setup.b.wValue.w = type_idx; + phost->Control.setup.b.wIndex.w = value; phost->Control.setup.b.wLength.w = length; } return USBH_CtlReq(phost, buff , length );