From b490bad8bacb99a8cff7430418436b25982014b2 Mon Sep 17 00:00:00 2001 From: Harrison <53527582+HTV04@users.noreply.github.com> Date: Mon, 30 May 2022 02:05:33 -0400 Subject: [PATCH] Fix Hidv5 USB_Halt & Halt all usbkeyboard requests when closing device Based on https://github.com/devkitPro/libogc/commit/151ac5769167419442357b2592342b4d46765fb6 Co-Authored-By: DacoTaco <12701306+DacoTaco@users.noreply.github.com> --- libogc/usb.c | 28 +++++++++++++++++++++++----- libwiikeyboard/usbkeyboard.c | 6 +++--- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/libogc/usb.c b/libogc/usb.c index 211d50ef..059f2c84 100644 --- a/libogc/usb.c +++ b/libogc/usb.c @@ -64,8 +64,10 @@ distribution. #define USBV0_IOCTL_DEVREMOVALHOOK 26 #define USBV0_IOCTL_DEVINSERTHOOK 27 #define USBV0_IOCTL_DEVICECLASSCHANGE 28 +#define USBV0_IOCTL_RESETDEVICE 29 #define USBV4_IOCTL_GETVERSION 6 // returns 0x40001 +#define USBV4_IOCTL_CANCELINTERRUPT 8 #define USBV5_IOCTL_GETVERSION 0 // should return 0x50001 #define USBV5_IOCTL_GETDEVICECHANGE 1 @@ -1430,7 +1432,7 @@ s32 USB_SetAlternativeInterface(s32 fd, u8 interface, u8 alternateSetting) return __usb_control_message(fd, (USB_CTRLTYPE_DIR_HOST2DEVICE | USB_CTRLTYPE_TYPE_STANDARD | USB_CTRLTYPE_REC_INTERFACE), USB_REQ_SETINTERFACE, alternateSetting, interface, 0, NULL, NULL, NULL); } -static s32 USBV5_CancelEndpoint(s32 device_id, u8 endpoint) +static s32 USBV5_CancelEndpoint(s32 device_id) { s32 ret; s32 fd; @@ -1446,19 +1448,35 @@ static s32 USBV5_CancelEndpoint(s32 device_id, u8 endpoint) if (buf==NULL) return IPC_ENOMEM; buf[0] = device_id; - buf[2] = endpoint; + + //Cancel all control messages + buf[2] = 0x00; ret = IOS_Ioctl(fd, USBV5_IOCTL_CANCELENDPOINT, buf, 32, NULL, 0); - iosFree(hId, buf); + if(ret < 0) + goto ret; + + //Cancel all incoming interrupts + buf[2] = 0x01 << 24; + ret = IOS_Ioctl(fd, USBV5_IOCTL_CANCELENDPOINT, buf, 32, NULL, 0); + if(ret < 0) + goto ret; + //Cancel all outgoing interrupts + buf[2] = 0x02 << 24; + ret = IOS_Ioctl(fd, USBV5_IOCTL_CANCELENDPOINT, buf, 32, NULL, 0); + if(ret < 0) + goto ret; + +ret: + iosFree(hId, buf); return ret; } s32 USB_ClearHalt(s32 fd, u8 endpoint) { if (fd>=0x20 || fd<-1) - return USBV5_CancelEndpoint(fd, endpoint); + return USBV5_CancelEndpoint(fd); return __usb_control_message(fd, (USB_CTRLTYPE_DIR_HOST2DEVICE | USB_CTRLTYPE_TYPE_STANDARD | USB_CTRLTYPE_REC_ENDPOINT), USB_REQ_CLEARFEATURE, USB_FEATURE_ENDPOINT_HALT, endpoint, 0, NULL, NULL, NULL); } #endif /* defined(HW_RVL) */ - diff --git a/libwiikeyboard/usbkeyboard.c b/libwiikeyboard/usbkeyboard.c index 1b2d45a6..b18b8724 100644 --- a/libwiikeyboard/usbkeyboard.c +++ b/libwiikeyboard/usbkeyboard.c @@ -387,11 +387,13 @@ void USBKeyboard_Close(void) return; if(_kbd->fd != -1) + { + USB_ClearHalt( _kbd->fd, _kbd->ep ); USB_CloseDevice(&_kbd->fd); + } free(_kbd); _kbd = NULL; - return; } @@ -458,7 +460,6 @@ s32 USBKeyboard_Scan(void) } _kbd->sc_odata = _kbd->sc_ndata; - return 0; } @@ -492,4 +493,3 @@ s32 USBKeyboard_ToggleLed(const USBKeyboard_led led) return 1; } -