diff --git a/tinyusb/class/hid_device.c b/tinyusb/class/hid_device.c index 52dc8bdc7..09fd24186 100644 --- a/tinyusb/class/hid_device.c +++ b/tinyusb/class/hid_device.c @@ -57,7 +57,6 @@ typedef struct { endpoint_handle_t ept_handle; uint8_t interface_number; - uint8_t idle_rate; // need to be in usb ram hid_keyboard_report_t report; // need to be in usb ram }hidd_interface_t; @@ -103,7 +102,6 @@ bool tusbd_hid_mouse_is_busy(uint8_t coreid) return dcd_pipe_is_busy(moused_data.ept_handle); } - tusb_error_t tusbd_hid_mouse_send(uint8_t coreid, hid_mouse_report_t const *p_report) { //------------- verify data -------------// @@ -134,7 +132,8 @@ tusb_error_t hidd_control_request(uint8_t coreid, tusb_control_request_t const * ASSERT_PTR(p_hid, TUSB_ERROR_FAILED); - if (p_request->bmRequestType_bit.type == TUSB_REQUEST_TYPE_STANDARD) // standard request to hid + //------------- STD Request -------------// + if (p_request->bmRequestType_bit.type == TUSB_REQUEST_TYPE_STANDARD) { uint8_t const desc_type = u16_high_u8(p_request->wValue); uint8_t const desc_index = u16_low_u8 (p_request->wValue); @@ -144,7 +143,7 @@ tusb_error_t hidd_control_request(uint8_t coreid, tusb_control_request_t const * dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, p_hid->p_report_desc, p_hid->report_length); }else { - ASSERT_STATUS(TUSB_ERROR_FAILED); + dcd_pipe_control_stall(coreid); } } //------------- Class Specific Request -------------// @@ -153,9 +152,9 @@ tusb_error_t hidd_control_request(uint8_t coreid, tusb_control_request_t const * switch(p_request->bRequest) { case HID_REQUEST_CONTROL_SET_IDLE: - p_hid->idle_rate = u16_high_u8(p_request->wValue); + // idle_rate = u16_high_u8(p_request->wValue); dcd_pipe_control_xfer(coreid, TUSB_DIR_HOST_TO_DEV, NULL, 0); - break; + break; case HID_REQUEST_CONTROL_SET_REPORT: { @@ -171,12 +170,12 @@ tusb_error_t hidd_control_request(uint8_t coreid, tusb_control_request_t const * case HID_REQUEST_CONTROL_GET_PROTOCOL: case HID_REQUEST_CONTROL_SET_PROTOCOL: default: - ASSERT_STATUS(TUSB_ERROR_NOT_SUPPORTED_YET); + dcd_pipe_control_stall(coreid); return TUSB_ERROR_NOT_SUPPORTED_YET; } }else { - ASSERT_STATUS(TUSB_ERROR_FAILED); + dcd_pipe_control_stall(coreid); } return TUSB_ERROR_NONE; @@ -200,25 +199,26 @@ tusb_error_t hidd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_int { switch(p_interface_desc->bInterfaceProtocol) { - #if TUSB_CFG_DEVICE_HID_KEYBOARD case HID_PROTOCOL_KEYBOARD: -// memclr_(&keyboardd_data, sizeof(hidd_interface_t)); - - keyboardd_data.interface_number = p_interface_desc->bInterfaceNumber; - keyboardd_data.report_length = p_desc_hid->wReportLength; - keyboardd_data.ept_handle = dcd_pipe_open(coreid, p_desc_endpoint); - ASSERT( endpointhandle_is_valid(keyboardd_data.ept_handle), TUSB_ERROR_DCD_FAILED); - break; - #endif - - #if TUSB_CFG_DEVICE_HID_MOUSE case HID_PROTOCOL_MOUSE: - moused_data.interface_number = p_interface_desc->bInterfaceNumber; - moused_data.report_length = p_desc_hid->wReportLength; - moused_data.ept_handle = dcd_pipe_open(coreid, p_desc_endpoint); - ASSERT( endpointhandle_is_valid(moused_data.ept_handle), TUSB_ERROR_DCD_FAILED); + { + hidd_interface_t* p_hid = + #if TUSB_CFG_DEVICE_HID_KEYBOARD + (p_interface_desc->bInterfaceProtocol == HID_PROTOCOL_KEYBOARD) ? &keyboardd_data : + #endif + #if TUSB_CFG_DEVICE_HID_MOUSE + (p_interface_desc->bInterfaceProtocol == HID_PROTOCOL_MOUSE) ? &moused_data : + #endif + NULL; + + ASSERT_PTR(p_hid, TUSB_ERROR_FAILED); + + p_hid->interface_number = p_interface_desc->bInterfaceNumber; + p_hid->report_length = p_desc_hid->wReportLength; + p_hid->ept_handle = dcd_pipe_open(coreid, p_desc_endpoint); + ASSERT( endpointhandle_is_valid(p_hid->ept_handle), TUSB_ERROR_DCD_FAILED); + } break; - #endif default: // TODO unknown, unsupported protocol --> skip this interface return TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE; @@ -327,49 +327,7 @@ tusb_error_t hidd_configured(void) return TUSB_ERROR_NONE; } -tusb_error_t hidd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_interface_desc, uint16_t *p_length) -{ - uint8_t const *p_desc = (uint8_t const *) p_interface_desc; - //------------- HID descriptor -------------// - p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; - tusb_hid_descriptor_hid_t const *p_desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc; - ASSERT_INT(HID_DESC_TYPE_HID, p_desc_hid->bDescriptorType, TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE); - - if (p_interface_desc->bInterfaceSubClass == HID_SUBCLASS_BOOT) - { - switch(p_interface_desc->bInterfaceProtocol) - { - #if TUSB_CFG_DEVICE_HID_KEYBOARD - case HID_PROTOCOL_KEYBOARD: - ASSERT_STATUS( hidd_interface_init(p_interface_desc, - app_tusb_keyboard_desc_report, p_desc_hid->wReportLength, - hidd_keyboard_buffer , sizeof(hidd_keyboard_buffer)) ); - break; - #endif - - #if TUSB_CFG_DEVICE_HID_MOUSE - case HID_PROTOCOL_MOUSE: - ASSERT_STATUS( hidd_interface_init(p_interface_desc, - app_tusb_mouse_desc_report, p_desc_hid->wReportLength, - hidd_mouse_buffer , sizeof(hidd_mouse_buffer)) ); - break; - #endif - - default: // TODO unknown, unsupported protocol --> skip this interface - return TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE; - } - *p_length = sizeof(tusb_descriptor_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + sizeof(tusb_descriptor_endpoint_t); - }else - { - // open generic - *p_length = 0; - return TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE; - } - - - return TUSB_ERROR_NONE; -} tusb_error_t hidd_interface_init(tusb_descriptor_interface_t const *p_interface_desc, uint8_t const * const p_report_desc, uint32_t report_length, uint8_t* mem_base, uint32_t mem_size) diff --git a/tinyusb/device/dcd_lpc43xx.c b/tinyusb/device/dcd_lpc43xx.c index 6ef650b18..c60c60be8 100644 --- a/tinyusb/device/dcd_lpc43xx.c +++ b/tinyusb/device/dcd_lpc43xx.c @@ -328,6 +328,13 @@ endpoint_handle_t dcd_pipe_open(uint8_t coreid, tusb_descriptor_endpoint_t const if (p_endpoint_desc->bmAttributes.xfer == TUSB_XFER_ISOCHRONOUS) return null_handle; // TODO not support ISO yet + tusb_direction_t dir = (p_endpoint_desc->bEndpointAddress & TUSB_DIR_DEV_TO_HOST_MASK) ? TUSB_DIR_DEV_TO_HOST : TUSB_DIR_HOST_TO_DEV; + + //------------- Endpoint Control Register -------------// + volatile uint32_t * reg_control = (&LPC_USB0->ENDPTCTRL0) + (p_endpoint_desc->bEndpointAddress & 0x0f); + + ASSERT_FALSE( (*reg_control) & (ENDPTCTRL_MASK_ENABLE << (dir ? 16 : 0)), null_handle ); // endpoint must not be already enabled + //------------- Prepare Queue Head -------------// uint8_t ep_idx = endpoint_addr2phy(p_endpoint_desc->bEndpointAddress); dcd_qhd_t * p_qhd = &dcd_data.qhd[ep_idx]; @@ -338,9 +345,7 @@ endpoint_handle_t dcd_pipe_open(uint8_t coreid, tusb_descriptor_endpoint_t const p_qhd->max_package_size = p_endpoint_desc->wMaxPacketSize.size; p_qhd->qtd_overlay.next = QTD_INVALID; - //------------- Endpoint Control Register -------------// - volatile uint32_t * reg_control = (&LPC_USB0->ENDPTCTRL0) + (p_endpoint_desc->bEndpointAddress & 0x0f); - (*reg_control) |= ((p_endpoint_desc->bmAttributes.xfer << 2) | ENDPTCTRL_MASK_ENABLE | ENDPTCTRL_MASK_TOGGLE_RESET) << ((p_endpoint_desc->bEndpointAddress & TUSB_DIR_DEV_TO_HOST_MASK) ? 16 : 0); + (*reg_control) |= ((p_endpoint_desc->bmAttributes.xfer << 2) | ENDPTCTRL_MASK_ENABLE | ENDPTCTRL_MASK_TOGGLE_RESET) << (dir ? 16 : 0); return (endpoint_handle_t) { .coreid = coreid, .xfer_type = p_endpoint_desc->bmAttributes.xfer, .index = ep_idx }; } diff --git a/tinyusb/device/usbd.c b/tinyusb/device/usbd.c index 521e1f6b4..2a65b5037 100644 --- a/tinyusb/device/usbd.c +++ b/tinyusb/device/usbd.c @@ -124,27 +124,32 @@ tusb_error_t usbh_set_configure_received(uint8_t coreid, uint8_t config_number) dcd_controller_set_configuration(coreid, config_number); usbd_devices[coreid].state = TUSB_DEVICE_STATE_CONFIGURED; - uint16_t length = 0; + //------------- parse configuration & open drivers -------------// + uint8_t* p_desc_configure = (uint8_t*) &app_tusb_desc_configuration; + uint8_t* p_desc = p_desc_configure + sizeof(tusb_descriptor_configuration_t); - #if TUSB_CFG_DEVICE_HID_KEYBOARD - tusb_descriptor_interface_t const * p_kbd_interface = &app_tusb_desc_configuration.keyboard_interface; - usbd_devices[coreid].interface2class[p_kbd_interface->bInterfaceNumber] = p_kbd_interface->bInterfaceClass; - - if (usbd_class_drivers[p_kbd_interface->bInterfaceClass].open ) + while( p_desc < p_desc_configure + ((tusb_descriptor_configuration_t*)p_desc_configure)->wTotalLength ) { - usbd_class_drivers[p_kbd_interface->bInterfaceClass].open(coreid, p_kbd_interface, &length); - } - #endif + ASSERT( TUSB_DESC_TYPE_INTERFACE == p_desc[DESCRIPTOR_OFFSET_TYPE], TUSB_ERROR_NOT_SUPPORTED_YET ); - #if TUSB_CFG_DEVICE_HID_MOUSE - tusb_descriptor_interface_t const * p_mouse_interface = &app_tusb_desc_configuration.mouse_interface; - usbd_devices[coreid].interface2class[p_mouse_interface->bInterfaceNumber] = p_mouse_interface->bInterfaceClass; + uint8_t class_index; + tusb_descriptor_interface_t* p_desc_interface = (tusb_descriptor_interface_t*) p_desc; - if (usbd_class_drivers[p_mouse_interface->bInterfaceClass].open ) - { - usbd_class_drivers[p_mouse_interface->bInterfaceClass].open(coreid, p_mouse_interface, &length); + class_index = p_desc_interface->bInterfaceClass; + + ASSERT( class_index != 0 && usbd_class_drivers[class_index].open != NULL, TUSB_ERROR_NOT_SUPPORTED_YET ); + ASSERT( 0 == usbd_devices[coreid].interface2class[p_desc_interface->bInterfaceNumber], TUSB_ERROR_FAILED); // duplicate interface number TODO alternate setting + + usbd_devices[coreid].interface2class[p_desc_interface->bInterfaceNumber] = class_index; + + uint16_t length=0; + ASSERT_STATUS( usbd_class_drivers[class_index].open( coreid, p_desc_interface, &length ) ); + + ASSERT( length >= sizeof(tusb_descriptor_interface_t), TUSB_ERROR_FAILED ); + + // usbh_devices[new_addr].flag_supported_class |= BIT_(class_index); + p_desc += length; } - #endif return TUSB_ERROR_NONE; }