Skip to content

Commit

Permalink
Finish refactoring the decompiled codebase.
Browse files Browse the repository at this point in the history
  • Loading branch information
moversi authored and Tatsh committed Oct 17, 2023
1 parent 164ef8c commit 37e7ff2
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 265 deletions.
65 changes: 25 additions & 40 deletions ps2-decompile/attach.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
#include <usbd.h>

#ifdef DEBUG
#include <stdio.h>
#endif

#include "callbacks.h"
#include "globals.h"
#include "shared.h"
#include "utils.h"

static const int USB_ID = 0x110;
static const int USB_MAX_POWER = 0xC8;
static const int USB_DESCRIPTOR_TYPE = 0x21;
static const int USB_DEVICE_CLASS = 0x0;
static const int USB_DEVICE_PROTOCOL = 0x22;
static const int USB_DEVICE_PROTOCOL_LENGTH = 0x09;
static const int USB_DEVICE_PROTOCOL_MAX_SIZE = 0x52;
static const int USB_PROTOCOL_TOTAL_LENGTH = 0x2200;
static const int USB_DEVICE_VENDOR = 0x5051;
static const int USB_DEVICE_ENDPOINT_ADDRESS = 0x81;
static const unsigned int USB_PORT_A = 0x02u;
static const unsigned int USB_PORT_B = 0x01u;
static const int USB_MAX_PACKET_LO_BYTE = 0x08;
static const int USB_MAX_PACKET_HI_BYTE = 0x01;

int usbmouse_attach(int devId) {
int i;
int gp0, gp1, gp2, v5; // $gp, $v0
int maxPacketSize, getDeviceLocationRet, transferPipeRet;
UsbConfigDescriptor *cdesc; // [sp+18h] [+18h]
UsbInterfaceDescriptor *idesc; // [sp+1Ch] [+1Ch]
Expand All @@ -33,35 +21,35 @@ int usbmouse_attach(int devId) {

if ((cdesc = sceUsbdScanStaticDescriptor(devId, 0, USB_DT_CONFIG)) == NULL) {
#ifdef DEBUG
printf("sceUsbdScanStaticDescriptor (CONFIGURATION) failed\n");
(void) printf("sceUsbdScanStaticDescriptor (CONFIGURATION) failed\n");
#endif
goto fail;
}

if ((idesc = sceUsbdScanStaticDescriptor(devId, cdesc, USB_DT_INTERFACE)) == NULL) {
#ifdef DEBUG
printf("sceUsbdScanStaticDescriptor (INTERFACE) failed\n");
(void) printf("sceUsbdScanStaticDescriptor (INTERFACE) failed\n");
#endif
goto fail;
}

if (idesc->bNumEndpoints != USB_ENDPOINT_XFER_ISOC) {
#ifdef DEBUG
printf("wrong bNumEndpoints : %d\n", idesc->bNumEndpoints);
(void) printf("wrong bNumEndpoints : %d\n", idesc->bNumEndpoints);
#endif
goto fail;
}

if ((edesc = sceUsbdScanStaticDescriptor(devId, idesc, USB_DT_ENDPOINT)) == NULL) {
#ifdef DEBUG
printf("sceUsbdScanStaticDescriptor (ENDPOINT) failed\n");
(void) printf("sceUsbdScanStaticDescriptor (ENDPOINT) failed\n");
#endif
goto fail;
}

if ((hdesc = sceUsbdScanStaticDescriptor(devId, 0, 33)) == NULL) {
#ifdef DEBUG
printf("sceUsbdScanStaticDescriptor (HID(0x21)) failed\n");
(void) printf("sceUsbdScanStaticDescriptor (HID(0x21)) failed\n");
#endif
goto fail;
}
Expand All @@ -76,31 +64,31 @@ int usbmouse_attach(int devId) {

if (maxPacketSize >= USB_DEVICE_PROTOCOL_LENGTH) {
#ifdef DEBUG
printf("wrong payload size %d\n", maxPacketSize);
(void) printf("wrong payload size %d\n", maxPacketSize);
#endif
goto fail;
}

if ((g = unit_alloc(devId, maxPacketSize,idesc->bInterfaceNumber, idesc->bAlternateSetting)) == NULL) {
#ifdef DEBUG
printf("unit_alloc failed\n");
(void) printf("unit_alloc failed\n");
#endif
goto fail;
}

if ((g->configEndpoint = sceUsbdOpenPipe(devId, 0)) < USB_RC_OK) {
#ifdef DEBUG
printf("sceUsbdOpenPipe failed(1)\n");
(void) printf("sceUsbdOpenPipe failed(1)\n");
#endif
goto fail;
}
#ifdef DEBUG
printf("#### sceUsbdOpenPipe %d\n", g->configEndpoint);
(void) printf("#### sceUsbdOpenPipe %d\n", g->configEndpoint);
#endif

if ((g->dataEndpoint = sceUsbdOpenPipe(devId, edesc)) < USB_RC_OK) {
#ifdef DEBUG
printf("sceUsbdOpenPipe failed(2)\n");
(void) printf("sceUsbdOpenPipe failed(2)\n");
#endif
goto fail;
}
Expand All @@ -116,7 +104,7 @@ int usbmouse_attach(int devId) {
cdesc->maxPower == USB_MAX_POWER;

#ifdef DEBUG
printf("cdesc check %s\n", g->descriptorChecksPassed == 0 ? "failed" : "passed");
(void) printf("cdesc check %s\n", g->descriptorChecksPassed == 0 ? "failed" : "passed");
#endif

g->descriptorChecksPassed = hdesc->bLength == USB_DEVICE_PROTOCOL_LENGTH &&
Expand All @@ -129,7 +117,7 @@ int usbmouse_attach(int devId) {
hdesc->idVendor == USB_DEVICE_VENDOR;

#ifdef DEBUG
printf("hdesc check %s\n", g->descriptorChecksPassed == 0 ? "failed" : "passed");
(void) printf("hdesc check %s\n", g->descriptorChecksPassed == 0 ? "failed" : "passed");
#endif

g->descriptorChecksPassed = edesc->bEndpointAddress == USB_DEVICE_ENDPOINT_ADDRESS &&
Expand All @@ -139,19 +127,16 @@ int usbmouse_attach(int devId) {
edesc->bInterval == USB_INTERVAL;

#ifdef DEBUG
printf("edesc check %s\n", g->descriptorChecksPassed == 0 ? "failed" : "passed");
(void) printf("edesc check %s\n", g->descriptorChecksPassed == 0 ? "failed" : "passed");
#endif

if ((getDeviceLocationRet = sceUsbdGetDeviceLocation(
devId, deviceLocationPath))) {
if ((getDeviceLocationRet = sceUsbdGetDeviceLocation(devId, deviceLocationPath))) {
#ifdef DEBUG
printf("usbmouse%d: sceUsbdGetDeviceLocation -> 0x%x\n", g->port1,
getDeviceLocationRet);
(void) printf("usbmouse%d: sceUsbdGetDeviceLocation -> 0x%x\n", g->port1, getDeviceLocationRet);
#endif
goto fail;
}


switch (deviceLocationPath[0]) {
case 1:
g->port |= USB_PORT_A;
Expand All @@ -162,8 +147,8 @@ int usbmouse_attach(int devId) {
}

#ifdef DEBUG
printf("attached port = %i\n", deviceLocationPath[0] == 1 ? 1 : 2);
printf("c_pipe = %d\n", g->configEndpoint);
(void) printf("attached port = %i\n", deviceLocationPath[0] == 1 ? 1 : 2);
(void) printf("c_pipe = %d\n", g->configEndpoint);
#endif

deviceRequest.request = USB_REQ_SET_CONFIGURATION;
Expand All @@ -175,19 +160,19 @@ int usbmouse_attach(int devId) {
if ((transferPipeRet = sceUsbdTransferPipe(g->configEndpoint, 0, 0,
&deviceRequest, set_config_done, g))) {
#ifdef DEBUG
printf("usbmouse%d: sceUsbdSetConfiguration -> 0x%x\n",
g->port1, transferPipeRet);
(void) printf("usbmouse%d: sceUsbdSetConfiguration -> 0x%x\n", g->port1, transferPipeRet);
#endif
goto fail;
}

#ifdef DEBUG
dump_desc(devId);
#endif

return 0;

fail:
if (g)
unit_free(g);

return -1;
}
Loading

0 comments on commit 37e7ff2

Please sign in to comment.