Skip to content

Commit 151ac57

Browse files
authored
Fix Hidv5 USB_Halt & Halt all usbkeyboard requests when closing device (#94)
1 parent f0742da commit 151ac57

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

libogc/usb.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ distribution.
6464
#define USBV0_IOCTL_DEVREMOVALHOOK 26
6565
#define USBV0_IOCTL_DEVINSERTHOOK 27
6666
#define USBV0_IOCTL_DEVICECLASSCHANGE 28
67+
#define USBV0_IOCTL_RESETDEVICE 29
6768

6869
#define USBV4_IOCTL_GETVERSION 6 // returns 0x40001
70+
#define USBV4_IOCTL_CANCELINTERRUPT 8
6971

7072
#define USBV5_IOCTL_GETVERSION 0 // should return 0x50001
7173
#define USBV5_IOCTL_GETDEVICECHANGE 1
@@ -1430,7 +1432,7 @@ s32 USB_SetAlternativeInterface(s32 fd, u8 interface, u8 alternateSetting)
14301432
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);
14311433
}
14321434

1433-
static s32 USBV5_CancelEndpoint(s32 device_id, u8 endpoint)
1435+
static s32 USBV5_CancelEndpoint(s32 device_id)
14341436
{
14351437
s32 ret;
14361438
s32 fd;
@@ -1446,17 +1448,34 @@ static s32 USBV5_CancelEndpoint(s32 device_id, u8 endpoint)
14461448
if (buf==NULL) return IPC_ENOMEM;
14471449

14481450
buf[0] = device_id;
1449-
buf[2] = endpoint;
1451+
1452+
//Cancel all control messages
1453+
buf[2] = 0x00;
14501454
ret = IOS_Ioctl(fd, USBV5_IOCTL_CANCELENDPOINT, buf, 32, NULL, 0);
1455+
if(ret < 0)
1456+
goto ret;
1457+
1458+
//Cancel all incoming interrupts
1459+
buf[2] = 0x01 << 24;
1460+
ret = IOS_Ioctl(fd, USBV5_IOCTL_CANCELENDPOINT, buf, 32, NULL, 0);
1461+
if(ret < 0)
1462+
goto ret;
1463+
1464+
//Cancel all outgoing interrupts
1465+
buf[2] = 0x02 << 24;
1466+
ret = IOS_Ioctl(fd, USBV5_IOCTL_CANCELENDPOINT, buf, 32, NULL, 0);
1467+
if(ret < 0)
1468+
goto ret;
1469+
1470+
ret:
14511471
iosFree(hId, buf);
1452-
14531472
return ret;
14541473
}
14551474

14561475
s32 USB_ClearHalt(s32 fd, u8 endpoint)
14571476
{
14581477
if (fd>=0x20 || fd<-1)
1459-
return USBV5_CancelEndpoint(fd, endpoint);
1478+
return USBV5_CancelEndpoint(fd);
14601479
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);
14611480
}
14621481

libwiikeyboard/usbkeyboard.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,13 @@ void USBKeyboard_Close(void)
387387
return;
388388

389389
if(_kbd->fd != -1)
390+
{
391+
USB_ClearHalt( _kbd->fd, _kbd->ep );
390392
USB_CloseDevice(&_kbd->fd);
393+
}
391394

392395
free(_kbd);
393396
_kbd = NULL;
394-
395397
return;
396398
}
397399

@@ -457,8 +459,7 @@ s32 USBKeyboard_Scan(void)
457459
}
458460
}
459461

460-
_kbd->sc_odata = _kbd->sc_ndata;
461-
462+
_kbd->sc_odata = _kbd->sc_ndata;
462463
return 0;
463464
}
464465

0 commit comments

Comments
 (0)