diff --git a/ps2-decompile/attach.c b/ps2-decompile/attach.c index 5ec097a..f7d1ba7 100644 --- a/ps2-decompile/attach.c +++ b/ps2-decompile/attach.c @@ -1,27 +1,15 @@ #include +#ifdef DEBUG +#include +#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] @@ -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; } @@ -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; } @@ -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 && @@ -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 && @@ -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; @@ -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; @@ -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; } diff --git a/ps2-decompile/callbacks.c b/ps2-decompile/callbacks.c index d38a3eb..b0dac64 100644 --- a/ps2-decompile/callbacks.c +++ b/ps2-decompile/callbacks.c @@ -1,194 +1,220 @@ -#include +#include "callbacks.h" -#include "globals.h" -#include "shared.h" +static char report[0x400]; +static const char string_desc[] = {0}; +static const int off_2C30 = 0x2C30; +static char kb[0x10]; int data_transfer_done(int result, int count, shared_t *g) { - int usbmouse_count; // $a2 - int v4; // $gp - int i; // [sp+14h] [+14h] + int i, offset; + (void) count; +#ifdef DEBUG if (result) - printf("usbmouse%d: %s -> 0x%x\n", g->port1, "sceUsbdInterruptTransfer", result); + (void) printf("usbmouse%d: %s -> 0x%x\n", g->port1, "sceUsbdInterruptTransfer", result); +#endif + if (!result && g->descriptorChecksPassed) { - usbmouse_count = g->count + 1; - g->count = usbmouse_count; - printf("usbmouse%d: count=%d data=(", g->port1, usbmouse_count); + g->count += 1; + +#ifdef DEBUG + (void) printf("usbmouse%d: count=%d data=(", g->port1, g->count); +#endif + for (i = 0; i < 8; ++i) { - // v4 - 32752 = 2CCC - printf((const char *)(v4 + 0xFFFF8010), g->unk8[i + 0x7804]); + offset = i + 0x7804; + +#ifdef DEBUG + (void) printf("data at offset %i byte=%c", offset, g->data[offset]); +#endif + if (g->port) { - *(char *)(8 * (g->port - 1) + i + 0x2D20) = g->unk8[i + 0x7804]; + // TODO figure out what the driver is doing here. + // it appears to be a memcpy. + *(char *)(8 * (g->port - 1) + i + 0x2D20) = g->data[offset]; kb[8 * g->port - 1] = 63; } } - // v4 - 32744 = 2CD4 - printf((const char *)(v4 + 0xFFFF8018)); } + return data_transfer(g); } int get_report_done(int result, int count, shared_t *g) { - int gp0; // $gp - int transferPipeRet; // $v0 - int i; // [sp+20h] [+20h] - int j; // [sp+20h] [+20h] - UsbDeviceRequest req; // [sp+28h] [+28h] BYREF + int transferPipeRet; + (void) count; +#ifdef DEBUG if (result) - printf("usbmouse%d: %s -> 0x%x\n", g->port1, "dump_report", result); + (void) printf("usbmouse%d: %s -> 0x%x\n", g->port1, "dump_report", result); +#else + (void) result; +#endif + +#ifdef DEBUG + int i; + // TODO Figure out which values are being compared to detect a descriptor check failure. for (i = 0; i < count; ++i) { if (*(unsigned char *)(i + 11316) != *(unsigned char *)(i + 0x2D30)) { - printf("report desc error\n"); + (void) printf("report desc error\n"); g->descriptorChecksPassed = 0; break; } } - printf("--Report Descriptor(%d)--\n", count); +#endif + +#ifdef DEBUG + int gp0, j; + // TODO figure out what its attempting to print out. + (void) printf("--Report Descriptor(%d)--\n", count); for (j = 0; j < count; ++j) { - printf((const char *)(gp0 + 0xFFFF802C), *(unsigned char *)(j + 0x2D30)); + (void) printf((const char *)(gp0 + 0xFFFF802C), *(unsigned char *)(j + 0x2D30)); if (j % 2 == 1) - printf((const char *)(gp0 + 0xFFFF8034)); + (void) printf((const char *)(gp0 + 0xFFFF8034)); if (j % 16 == 15) - printf((const char *)(gp0 + 0xFFFF8020)); + (void) printf((const char *)(gp0 + 0xFFFF8020)); + } +#endif + + // sceUsbdControlTransfer(g->configEndpoint, 0x80, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) | index, 0x800, ..., g->buf, get_string_done, g) + if ((transferPipeRet = sceUsbdTransferPipe(g->configEndpoint, g->buf, 0x800, &REQ_GET_REPORT, get_string_done, g))) { +#ifdef DEBUG + (void) printf("usbmouse%d: sceUsbdSetInterface -> 0x%x\n", g->port1, transferPipeRet) +#endif } - printf((const char *)(gp0 + 0xFFFF8020)); - req.requesttype = 0x80; // USB_DIR_IN - req.request = 6; // USB_REQ_GET_DESCRIPTOR - req.value = 768; - req.index = 0; - req.length = 0x800; - transferPipeRet = sceUsbdTransferPipe( - g->configEndpoint, - g->buf, - 0x800, - &req, - get_string_done, - g); // sceUsbdControlTransfer(g->configEndpoint, 0x80, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) | index, 0x800, ..., g->buf, get_string_done, g) - if (transferPipeRet) - transferPipeRet = - printf("usbmouse%d: %s -> 0x%x\n", g->port1, "sceUsbdSetInterface", transferPipeRet); + return transferPipeRet; } -const char string_desc[] = {0}; -const int off_2C30 = 0x2C30; - int get_string_done(int result, int count, shared_t *g) { - int v3; // $gp - int v4; // $gp - int setInterfaceRet; // $v0 + int setInterfaceRet; unsigned char *v7; // [sp+1Ch] [+1Ch] - int i; // [sp+20h] [+20h] - int j; // [sp+20h] [+20h] - int k; // [sp+20h] [+20h] short *v11; // [sp+28h] [+28h] UsbDeviceRequest req; // [sp+30h] [+30h] BYREF + (void) count; v7 = (unsigned char *)&g->buf[0x2000 * g->unk7]; if (result) { req.requesttype = 1; - req.request = 0xB; // USB_REQ_SET_PROTOCOL + req.request = USB_REQ_SET_PROTOCOL; req.value = g->alternateSetting; req.index = g->interfaceNumber; req.length = 0; - setInterfaceRet = sceUsbdTransferPipe( - g->configEndpoint, - 0, - 0, - &req, - set_interface_done, - g); // sceUsbdSetInterface(g->c_pipe, g->interfaceNumber, g->alternateSetting, set_interface_done, g) - if (setInterfaceRet) - setInterfaceRet = printf( - "usbmouse%d: %s -> 0x%x\n", g->port1, "sceUsbdSetInterface", setInterfaceRet); - } else { - printf("---StringDesc---\n"); - printf("bDescriptorType 0x%02X\n", v7[1]); - printf("bString "); - for (i = 0; i < *v7 - 2; ++i) - printf((const char *)(v3 + 0xFFFF801C), v7[i + 2]); - printf((const char *)(v3 + 0xFFFF8020)); - for (j = 0; j < *v7 - 2; ++j) { - printf((const char *)(v4 + 0xFFFF8024), v7[j + 2]); - if (j % 16 == 15) - printf((const char *)(v4 + 0xFFFF8020)); - } - printf((const char *)(v4 + 0xFFFF8020)); - if (!g->unk7) { - *(int *)&g->unk8[0x7800] = v7[2] | (v7[3] << 8); - printf("lang_id = 0x%04x\n", *(unsigned short *)&g->unk8[0x7800]); + + // sceUsbdSetInterface(g->c_pipe, g->interfaceNumber, g->alternateSetting, set_interface_done, g) + if ((setInterfaceRet = sceUsbdTransferPipe(g->configEndpoint, 0, 0, &req, set_interface_done, g))) { +#ifdef DEBUG + (void) printf("usbmouse%d: sceUsbdSetInterface -> 0x%x\n", g->port1, setInterfaceRet) +#endif } - if (g->unk7 < 3) - v11 = (short *)*(&string_desc + g->unk7); - else - v11 = off_2C30; - for (k = 0; k < *v7 - 2; ++k) { - if (*(unsigned char *)v11 != v7[k + 2]) { - g->descriptorChecksPassed = 0; - printf("String Desc Error\n"); - } - v11 = (short *)((char *)v11 + 1); + + return setInterfaceRet; + } + +#ifdef DEBUG + int i; + (void) printf("---StringDesc---\n"); + (void) printf("bDescriptorType 0x%02X\n", v7[1]); + (void) printf("bString "); + + for (i = 0; i < *v7 - 2; ++i) + (void) printf("current byte=(%c)\n", v7[i + 2]); +#endif + + switch (g->unk7) { + case 0: { + g->data[0x7800] = v7[2] | (v7[3] << 8); +#ifdef DEBUG + (void) printf("lang_id = 0x%04x\n", *(unsigned short *)&g->data[0x7800]); +#endif + break; + } + case 1: + case 2: { + v11 = (short *)*(&string_desc + g->unk7); + break; + } + default: { + v11 = off_2C30; + break; + } + } + +#ifdef DEBUG + int k; + // TODO figure out the value being compared for a descriptor failure. + for (k = 0; k < *v7 - 2; ++k) { + if (*(unsigned char *)v11 != v7[k + 2]) { + g->descriptorChecksPassed = 0; + (void) printf("String Desc Error\n"); } - ++g->unk7; - req.requesttype = 0x80; // USB_DIR_IN - req.request = 6; // USB_REQ_GET_DESCRIPTOR - req.value = LOWORD(g->unk7) | 0x300; - req.index = *(int *)&g->unk8[0x7800]; - req.length = 2048; - setInterfaceRet = sceUsbdTransferPipe( - g->configEndpoint, &g->buf[0x2000 * g->unk7], 2048, &req, get_string_done, g); + v11 = (short *)((char *)v11 + 1); } - return setInterfaceRet; +#endif + + req.requesttype = USB_DIR_IN; + req.request = USB_REQ_GET_DESCRIPTOR; + req.value = LOWORD(g->unk7++) | 0x300; + req.index = g->data[0x7800]; + req.length = 2048; + return sceUsbdTransferPipe(g->configEndpoint, &g->buf[0x2000 * g->unk7], 2048, &req, get_string_done, g); } int led_transfer_done(int result, int count, shared_t *g) { +#ifdef DEBUG if (result) - printf("usbmouse%d: %s -> 0x%x\n", g->port1, "led_transfer_done", result); + (void) printf("usbmouse%d: %s -> 0x%x\n", g->port1, "led_transfer_done", result); +#else + (void) result; +#endif + (void) count; return data_transfer(g); } -char report[0x400]; - int set_config_done(int result, int count, shared_t *g) { - int transferPipeRet; // $v0 - int gp0; // $gp - UsbDeviceRequest req; // [sp+20h] [+20h] BYREF + int transferPipeRet; + (void) count; +#ifdef DEBUG if (result) - printf("usbmouse%d: %s -> 0x%x\n", g->port1, "sceUsbdSetConfiguration", result); - req.requesttype = 0x81; // USB_DIR_OUT | USB_RECIP_DEVICE - req.request = 6; // USB_REQ_GET_DESCRIPTOR - req.value = 0x2200; - req.index = 0; - req.length = 0x400; - transferPipeRet = - sceUsbdTransferPipe(g->configEndpoint, report, 0x400, &req, get_report_done, g); - if (transferPipeRet) - // v4 - 32712 = 2CF4 - transferPipeRet = printf("usbmouse%d: %s -> 0x%x\n", - g->port1, - (const char *)(gp0 + 0xFFFF8038), - transferPipeRet); + (void) printf("usbmouse%d: %s -> 0x%x\n", g->port1, "sceUsbdSetConfiguration", result); +#else + (void) result; +#endif + + if ((transferPipeRet = sceUsbdTransferPipe(g->configEndpoint, report, 0x400, &req, get_report_done, g))) { +#ifdef DEBUG + (void) printf("usbmouse%d: sceUsbdTransferPipe -> 0x%x\n", g->port1, transferPipeRet) +#endif + } + return transferPipeRet; } int set_interface_done(int result, int count, shared_t *g) { - int transferPipeRet; // $v0 - UsbDeviceRequest req; // [sp+20h] [+20h] BYREF + UsbDeviceRequest req; + int transferPipeRet; + (void) count; +#ifdef DEBUG if (result) - printf("usbmouse%d: %s -> 0x%x\n", g->port1, "sceUsbdSetInterface", result); + (void) printf("usbmouse%d: %s -> 0x%x\n", g->port1, "sceUsbdSetInterface", result); +#else + (void) result; +#endif + data_transfer(g); - req.requesttype = 0x21; // USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE - req.request = 9; // SET_REPORT - req.value = 0x300; // 3 << 8 + req.requesttype = USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE; + req.request = SET_REPORT; + req.value = 3 << 8; req.index = g->interfaceNumber; req.length = 1; - transferPipeRet = - sceUsbdTransferPipe(g->configEndpoint, &g->unk5, 1, &req, led_transfer_done, g); - if (transferPipeRet) - transferPipeRet = - printf("usbmouse%d: %s -> 0x%x\n", g->port1, "req-feature", transferPipeRet); + + if ((transferPipeRet = sceUsbdTransferPipe(g->configEndpoint, &g->unk5, 1, &req, led_transfer_done, g))) { +#ifdef DEBUG + (void) printf("usbmouse%d: req-feature -> 0x%x\n", g->port1, transferPipeRet); +#endif + } + return transferPipeRet; } diff --git a/ps2-decompile/callbacks.h b/ps2-decompile/callbacks.h index d20702c..665f7e1 100644 --- a/ps2-decompile/callbacks.h +++ b/ps2-decompile/callbacks.h @@ -1,9 +1,36 @@ #ifndef CALLBACKS_H #define CALLBACKS_H +#include + +#ifdef DEBUG +#include +#endif + +#include "globals.h" #include "shared.h" +static UsbDeviceRequest REQ_GET_REPORT = { + .requesttype = USB_DIR_IN, + .request = USB_REQ_GET_DESCRIPTOR, + .value = 768, + .index = 0, + .length = 0x800 +}; + +static UsbDeviceRequest REQ_SET_CONFIG_DONE = { + .requesttype = USB_DIR_OUT | USB_RECIP_DEVICE, + .request = USB_REQ_GET_DESCRIPTOR, + .value = 0x2200, + .index = 0, + .length = 0x400 +}; + int data_transfer_done(int result, int count, shared_t *g); +int get_report_done(int result, int count, shared_t *g); +int get_string_done(int result, int count, shared_t *g); +int led_transfer_done(int result, int count, shared_t *g); int set_config_done(int result, int count, shared_t *g); +int set_interface_done(int result, int count, shared_t *g); #endif // CALLBACKS_H diff --git a/ps2-decompile/detach.c b/ps2-decompile/detach.c index 13d804d..0daed85 100644 --- a/ps2-decompile/detach.c +++ b/ps2-decompile/detach.c @@ -1,18 +1,27 @@ #include +#ifdef DEBUG +#include +#endif + #include "shared.h" -char dword_2D18[2048]; +static char dword_2D18[2048]; int usbmouse_detach(int devId) { - shared_t *p; // [sp+10h] [+10h] + shared_t *p; - p = (shared_t *)sceUsbdGetPrivateData(devId); - if (!p) + if ((p = sceUsbdGetPrivateData(devId)) == NULL) return -1; + if (p->port > 0) memset(&dword_2D18[2 * p->port], 0, 8u); + unit_free(p); - printf("usbmouse%d: detached : port %d\n", p->port1, p->port); + +#ifdef DEBUG + (void) printf("usbmouse%d: detached : port %d\n", p->port1, p->port); +#endif + return 0; } diff --git a/ps2-decompile/driver.h b/ps2-decompile/driver.h index 4cdbb64..4ee250b 100644 --- a/ps2-decompile/driver.h +++ b/ps2-decompile/driver.h @@ -1,8 +1,8 @@ -#ifndef ATTACH_H -#define ATTACH_H +#ifndef DRIVER_H +#define DRIVER_H int usbmouse_attach(int devId); int usbmouse_detach(int devId); int usbmouse_probe(int devId); -#endif // ATTACH_H +#endif // DRIVER_H diff --git a/ps2-decompile/globals.c b/ps2-decompile/globals.c new file mode 100644 index 0000000..3478b41 --- /dev/null +++ b/ps2-decompile/globals.c @@ -0,0 +1,22 @@ + #include "globals.h" + +const int THREAD_RPC = -0x7ffffdf0; +const int THREAD_ATTRIBUTE = 0x2000000; +const int THREAD_PRIORITY = 0x15; +const int THREAD_STACK_SIZE = 0x1000; +const int THREAD_KB_SIZE = 0x10; + +const int USB_ID = 0x0110; +const int USB_MAX_POWER = 0xC8; +const int USB_DESCRIPTOR_TYPE = 0x21; +const int USB_DEVICE_CLASS = 0x00; +const int USB_DEVICE_PROTOCOL = 0x22; +const int USB_DEVICE_PROTOCOL_LENGTH = 0x09; +const int USB_DEVICE_PROTOCOL_MAX_SIZE = 0x52; +const int USB_PROTOCOL_TOTAL_LENGTH = 0x2200; +const int USB_DEVICE_VENDOR = 0x5051; +const int USB_DEVICE_ENDPOINT_ADDRESS = 0x81; +const unsigned int USB_PORT_A = 0x02u; +const unsigned int USB_PORT_B = 0x01u; +const int USB_MAX_PACKET_LO_BYTE = 0x08; +const int USB_MAX_PACKET_HI_BYTE = 0x01; diff --git a/ps2-decompile/globals.h b/ps2-decompile/globals.h index 5dbdb94..0954d73 100644 --- a/ps2-decompile/globals.h +++ b/ps2-decompile/globals.h @@ -4,4 +4,24 @@ extern char *buffer; extern char kb[16]; +extern const int THREAD_RPC; +extern const int THREAD_ATTRIBUTE; +extern const int THREAD_PRIORITY; +extern const int THREAD_STACK_SIZE; +extern const int THREAD_KB_SIZE; +extern const int USB_ID; +extern const int USB_MAX_POWER; +extern const int USB_DESCRIPTOR_TYPE; +extern const int USB_DEVICE_CLASS; +extern const int USB_DEVICE_PROTOCOL; +extern const int USB_DEVICE_PROTOCOL_LENGTH; +extern const int USB_DEVICE_PROTOCOL_MAX_SIZE; +extern const int USB_PROTOCOL_TOTAL_LENGTH; +extern const int USB_DEVICE_VENDOR; +extern const int USB_DEVICE_ENDPOINT_ADDRESS; +extern const unsigned int USB_PORT_A; +extern const unsigned int USB_PORT_B; +extern const int USB_MAX_PACKET_LO_BYTE; +extern const int USB_MAX_PACKET_HI_BYTE; + #endif // GLOBALS_H diff --git a/ps2-decompile/main.c b/ps2-decompile/main.c index 3edcb7e..cdfe1f8 100644 --- a/ps2-decompile/main.c +++ b/ps2-decompile/main.c @@ -2,50 +2,63 @@ #include #include +#ifdef DEBUG +#include +#endif + #include "driver.h" +#include "globals.h" char *buffer; char kb[16]; -sceUsbdLddOps usbmouse_ops = {.name = "usbmouse", - .connect = usbmouse_attach, - .disconnect = usbmouse_detach, - .probe = usbmouse_probe}; + +static sceUsbdLddOps usbmouse_ops = { + .name = "usbmouse", + .connect = usbmouse_attach, + .disconnect = usbmouse_detach, + .probe = usbmouse_probe +}; static void *get_data(int fno, void *buffer, int len) { - memcpy(buffer, kb, 0x10u); + (void) fno; + (void) len; + + memcpy(buffer, kb, THREAD_KB_SIZE); return buffer; } -static void sifrpc_thread() { - int thid; // $v0 - SifRpcDataQueue_t qd; // [sp+20h] [+20h] BYREF - SifRpcServerData_t *v2; // [sp+38h] [+38h] BYREF +static void sifrpc_thread(void) { + int thid; + SifRpcDataQueue_t qd; + SifRpcServerData_t *sd; sceSifInitRpc(0); thid = GetThreadId(); sceSifSetRpcQueue(&qd, thid); - sceSifRegisterRpc(v2, -2147483120, get_data, &buffer, 0, 0, &qd); + sceSifRegisterRpc(sd, THREAD_RPC, get_data, &buffer, 0, 0, &qd); sceSifRpcLoop(&qd); } /** Entry point. */ -int start() { - int gp0; // $gp - iop_thread_t thread; // [sp+10h] [+10h] BYREF - int registerLddRet; // [sp+28h] [+28h] - int thid; // [sp+2Ch] [+2Ch] - - registerLddRet = sceUsbdRegisterLdd(&usbmouse_ops); - if (registerLddRet) - printf("usb : sceUsbdRegisterLdd -> 0x%x\n", registerLddRet); - memset(kb, 0, 16); - thread.attr = 0x2000000; +int start(void) { + iop_thread_t thread; + int registerLddRet, thid; + + if ((registerLddRet = sceUsbdRegisterLdd(&usbmouse_ops))) { +#ifdef DEBUG + (void) printf("usb : sceUsbdRegisterLdd -> 0x%x\n", registerLddRet); +#endif + } + + memset(kb, 0, THREAD_KB_SIZE); + thread.attr = THREAD_ATTRIBUTE; thread.thread = sifrpc_thread; - thread.priority = 0x15; - thread.stacksize = 0x1000; + thread.priority = THREAD_PRIORITY; + thread.stacksize = THREAD_STACK_SIZE; thread.option = 0; - thid = CreateThread(&thread); - if (thid <= 0) + + if ((thid = CreateThread(&thread)) <= 0) return 1; + StartThread(thid, 0); return 0; } diff --git a/ps2-decompile/probe.c b/ps2-decompile/probe.c index 8fc202e..500ae47 100644 --- a/ps2-decompile/probe.c +++ b/ps2-decompile/probe.c @@ -1,24 +1,27 @@ -#include #include +#ifdef DEBUG +#include +#endif + int usbmouse_probe(int devId) { UsbDeviceDescriptor *hdesc; // [sp+10h] [+10h] UsbInterfaceDescriptor *idesc; // [sp+14h] [+14h] #ifdef DEBUG - printf("-------- dev_id = %d\n", devId); + (void) printf("-------- dev_id = %d\n", devId); #endif if ((hdesc = sceUsbdScanStaticDescriptor(devId, 0, 1)) == NULL) { #ifdef DEBUG - printf("#### USB : ScanStaticDescriptor Failed(0)\n"); + (void) printf("#### USB : ScanStaticDescriptor Failed(0)\n"); #endif goto fail; } if ((idesc = sceUsbdScanStaticDescriptor(devId, hdesc, 4)) == NULL) { #ifdef DEBUG - printf("#### USB : ScanStaticDescriptor Failed(1)\n"); + (void) printf("#### USB : ScanStaticDescriptor Failed(1)\n"); #endif goto fail; } @@ -29,7 +32,7 @@ int usbmouse_probe(int devId) { hdesc->iManufacturer != 1 || hdesc->iProduct != 2 || hdesc->iSerialNumber >= 1 || hdesc->bNumConfigurations != 1)) { #ifdef DEBUG - printf("Wrong Device Desc.\n"); + (void) printf("Wrong Device Desc.\n"); #endif goto fail; } @@ -40,13 +43,13 @@ int usbmouse_probe(int devId) { idesc->bInterfaceSubClass != 0 || idesc->bInterfaceProtocol != 0 || idesc->iInterface != 2) { #ifdef DEBUG - printf("Wrong Interface Desc.\n"); + (void) printf("Wrong Interface Desc.\n"); #endif goto fail; } #ifdef DEBUG - printf("USB_PROBE END\n"); + (void) printf("USB_PROBE END\n"); #endif return 1; diff --git a/ps2-decompile/shared.h b/ps2-decompile/shared.h index 033e69b..7cd0274 100644 --- a/ps2-decompile/shared.h +++ b/ps2-decompile/shared.h @@ -15,7 +15,7 @@ typedef struct t_shared { int descriptorChecksPassed; int unk7; char buf[2048]; - char unk8[30732]; + char data[30732]; } shared_t; #endif // SHARED_H diff --git a/ps2-decompile/utils.c b/ps2-decompile/utils.c index 59ad95a..cfbf3cd 100644 --- a/ps2-decompile/utils.c +++ b/ps2-decompile/utils.c @@ -1,13 +1,13 @@ #include "utils.h" int data_transfer(shared_t *g) { - int result; // $v0 - result = sceUsbdInterruptTransfer(g->dataEndpoint, &g->unk8[0x7804], g->maxPacketSize, 0, data_transfer_done, g); + int result; + if ((result = sceUsbdInterruptTransfer(g->dataEndpoint, &g->data[0x7804], g->maxPacketSize, 0, data_transfer_done, g))) { #ifdef DEBUG - if (result) - result = printf("usbmouse%d: %s -> 0x%x\n", g->port1, "sceUsbdInterruptTransfer", result); + (void) printf("usbmouse%d: sceUsbdInterruptTransfer -> 0x%x\n", g->port1, result) #endif + } return result; } @@ -20,7 +20,8 @@ UsbDeviceDescriptor *dump_desc(int devId) { UsbEndpointDescriptor *edesc; UsbStringDescriptor *sdesc; - printf("\n\nUSBKB : static Descriptor DUMP\n"); + (void) printf("\n\nUSBKB : static Descriptor DUMP\n"); + while (1) { // 0 means go through all descriptors if ((result = sceUsbdScanStaticDescriptor(devId, cdesc, 0)) == NULL) @@ -28,57 +29,57 @@ UsbDeviceDescriptor *dump_desc(int devId) { switch (result->bDescriptorType) { case USB_DT_DEVICE: - printf("--Device--\n"); - printf("bLength 0x%02X\n", result->bLength); - printf("bDescriptorType 0x%02X\n", result->bDescriptorType); - printf("bcdUSB 0x%02X\n", result->bcdUSB); - printf("bDeviceClass 0x%02X\n", result->bDeviceClass); - printf("bDeviceSubClass 0x%02X\n", result->bDeviceSubClass); - printf("bDeviceProtocol 0x%02X\n", result->bDeviceProtocol); - printf("bMaxPacketSize0 0x%02X\n", result->bMaxPacketSize0); - printf("idVendor 0x%02X\n", result->idVendor); - printf("idProduct 0x%02X\n", result->idProduct); - printf("bcdDevice 0x%02X\n", result->bcdDevice); - printf("iManufacturer 0x%02X\n", result->iManufacturer); - printf("iProduct 0x%02X\n", result->iProduct); - printf("iSerialNumber 0x%02X\n", result->iSerialNumber); - printf("bNumConfigurations 0x%02X\n", result->bNumConfigurations); + (void) printf("--Device--\n"); + (void) printf("bLength 0x%02X\n", result->bLength); + (void) printf("bDescriptorType 0x%02X\n", result->bDescriptorType); + (void) printf("bcdUSB 0x%02X\n", result->bcdUSB); + (void) printf("bDeviceClass 0x%02X\n", result->bDeviceClass); + (void) printf("bDeviceSubClass 0x%02X\n", result->bDeviceSubClass); + (void) printf("bDeviceProtocol 0x%02X\n", result->bDeviceProtocol); + (void) printf("bMaxPacketSize0 0x%02X\n", result->bMaxPacketSize0); + (void) printf("idVendor 0x%02X\n", result->idVendor); + (void) printf("idProduct 0x%02X\n", result->idProduct); + (void) printf("bcdDevice 0x%02X\n", result->bcdDevice); + (void) printf("iManufacturer 0x%02X\n", result->iManufacturer); + (void) printf("iProduct 0x%02X\n", result->iProduct); + (void) printf("iSerialNumber 0x%02X\n", result->iSerialNumber); + (void) printf("bNumConfigurations 0x%02X\n", result->bNumConfigurations); break; case USB_DT_CONFIG: cdesc = result; - printf("---ConfigDesc---\n"); - printf("bLength 0x%02X\n", cdesc->bLength); - printf("wTotalLength0 0x%02X\n", LOBYTE(cdesc->wTotalLength)); - printf("wTotalLength1 0x%02X\n", HIBYTE(cdesc->wTotalLength)); - printf("bNumInterfaces 0x%02X\n", cdesc->bNumInterfaces); - printf("bConfigurationValue 0x%02X\n", cdesc->bConfigurationValue); - printf("iConfiguration 0x%02X\n", cdesc->iConfiguration); - printf("bmAttribute 0x%02X\n", cdesc->bmAttributes); - printf("MaxPower 0x%02X\n", cdesc->maxPower); + (void) printf("---ConfigDesc---\n"); + (void) printf("bLength 0x%02X\n", cdesc->bLength); + (void) printf("wTotalLength0 0x%02X\n", LOBYTE(cdesc->wTotalLength)); + (void) printf("wTotalLength1 0x%02X\n", HIBYTE(cdesc->wTotalLength)); + (void) printf("bNumInterfaces 0x%02X\n", cdesc->bNumInterfaces); + (void) printf("bConfigurationValue 0x%02X\n", cdesc->bConfigurationValue); + (void) printf("iConfiguration 0x%02X\n", cdesc->iConfiguration); + (void) printf("bmAttribute 0x%02X\n", cdesc->bmAttributes); + (void) printf("MaxPower 0x%02X\n", cdesc->maxPower); break; case USB_DT_STRING: sdesc = result; - printf("---StringDesc---\n"); - printf("bDescriptorType 0x%02X\n", sdesc->bDescriptorType); - printf("bString "); - printf("wData[0] = 0x%04x", sdesc->wData[0]); + (void) printf("---StringDesc---\n"); + (void) printf("bDescriptorType 0x%02X\n", sdesc->bDescriptorType); + (void) printf("bString "); + (void) printf("wData[0] = 0x%04x", sdesc->wData[0]); break; case USB_DT_INTERFACE: idesc = result; - printf("--Interface--\n"); - printf("bLength 0x%02X\n", idesc->bLength); - printf("bDescriptorType 0x%02X\n", idesc->bDescriptorType); - printf("bInterfaceNumber 0x%02X\n", idesc->bInterfaceNumber); - printf("bAlternateSetting 0x%02X\n", idesc->bAlternateSetting); - printf("bNumEndpoints 0x%02X\n", idesc->bNumEndpoints); - printf("bInterfaceClass 0x%02X\n", idesc->bInterfaceClass); - printf("bInterfaceSubClass 0x%02X\n", idesc->bInterfaceSubClass); - printf("bInterfaceProtocol 0x%02X\n", idesc->bInterfaceProtocol); - printf("iInterface 0x%02X\n", idesc->iInterface); + (void) printf("--Interface--\n"); + (void) printf("bLength 0x%02X\n", idesc->bLength); + (void) printf("bDescriptorType 0x%02X\n", idesc->bDescriptorType); + (void) printf("bInterfaceNumber 0x%02X\n", idesc->bInterfaceNumber); + (void) printf("bAlternateSetting 0x%02X\n", idesc->bAlternateSetting); + (void) printf("bNumEndpoints 0x%02X\n", idesc->bNumEndpoints); + (void) printf("bInterfaceClass 0x%02X\n", idesc->bInterfaceClass); + (void) printf("bInterfaceSubClass 0x%02X\n", idesc->bInterfaceSubClass); + (void) printf("bInterfaceProtocol 0x%02X\n", idesc->bInterfaceProtocol); + (void) printf("iInterface 0x%02X\n", idesc->iInterface); break; case USB_DT_ENDPOINT: edesc = result; - printf("ENDPOINT ADR 0x%02X ATR 0x%02X SIZE 0x%02X INTR 0x%02X\n", + (void) printf("ENDPOINT ADR 0x%02X ATR 0x%02X SIZE 0x%02X INTR 0x%02X\n", LOBYTE(edesc->bEndpointAddress), HIBYTE(edesc->bEndpointAddress), edesc->wMaxPacketSizeHB | (edesc->wMaxPacketSizeLB << 8), @@ -86,20 +87,20 @@ UsbDeviceDescriptor *dump_desc(int devId) { break; default: // if (result->bDescriptorType == 33) { - // printf("--HID--\n"); - // printf("bLength 0x%02X\n", cdesc->bLength); - // printf("bDescriptorType 0x%02X\n", cdesc->bDescriptorType); - // printf("bcdUSB 0x%02X\n", + // (void) printf("--HID--\n"); + // (void) printf("bLength 0x%02X\n", cdesc->bLength); + // (void) printf("bDescriptorType 0x%02X\n", cdesc->bDescriptorType); + // (void) printf("bcdUSB 0x%02X\n", // (HIBYTE(cdesc->wTotalLength) << 8) | LOBYTE(cdesc->wTotalLength)); - // printf("bCountryCode 0x%02X\n", cdesc->bNumInterfaces); - // printf("bNumDescriptors 0x%02X\n", cdesc->bConfigurationValue); - // printf("bDescriptorType2 0x%02X\n", cdesc->iConfiguration); - // printf("wDescriptorLength0 0x%02X\n", cdesc->bmAttributes); - // printf("wDescriptorLength1 0x%02X\n", cdesc->maxPower); + // (void) printf("bCountryCode 0x%02X\n", cdesc->bNumInterfaces); + // (void) printf("bNumDescriptors 0x%02X\n", cdesc->bConfigurationValue); + // (void) printf("bDescriptorType2 0x%02X\n", cdesc->iConfiguration); + // (void) printf("wDescriptorLength0 0x%02X\n", cdesc->bmAttributes); + // (void) printf("wDescriptorLength1 0x%02X\n", cdesc->maxPower); // } else { - // printf("--Unknown--\n"); - // printf("bLength 0x%02X\n", cdesc->bLength); - // printf("bDescriptorType 0x%02X\n", cdesc->bDescriptorType); + // (void) printf("--Unknown--\n"); + // (void) printf("bLength 0x%02X\n", cdesc->bLength); + // (void) printf("bDescriptorType 0x%02X\n", cdesc->bDescriptorType); // } break; }