From 09b0a739ca9109efb197355b7f8044557c9c28ed Mon Sep 17 00:00:00 2001 From: Ivan Epifanov Date: Wed, 18 Jan 2023 19:49:06 +0300 Subject: [PATCH] Initial --- .clang-format | 28 ++ .gitignore | 1 + CMakeLists.txt | 43 +++ LICENSE.md | 7 + README.md | 33 ++ src/controller.c | 67 ++++ src/controller.h | 41 +++ src/controllers/xbox_360_controller.c | 223 ++++++++++++++ src/controllers/xbox_360_controller.h | 10 + src/controllers/xbox_360_report.h | 47 +++ src/controllers/xbox_360w_controller.c | 272 ++++++++++++++++ src/controllers/xbox_360w_controller.h | 12 + src/devicelist.c | 74 +++++ src/devicelist.h | 23 ++ src/main.c | 409 +++++++++++++++++++++++++ src/unpack.c | 6 + src/unpack.h | 8 + vixen.yml | 8 + 18 files changed, 1312 insertions(+) create mode 100644 .clang-format create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 src/controller.c create mode 100644 src/controller.h create mode 100644 src/controllers/xbox_360_controller.c create mode 100644 src/controllers/xbox_360_controller.h create mode 100644 src/controllers/xbox_360_report.h create mode 100644 src/controllers/xbox_360w_controller.c create mode 100644 src/controllers/xbox_360w_controller.h create mode 100644 src/devicelist.c create mode 100644 src/devicelist.h create mode 100644 src/main.c create mode 100644 src/unpack.c create mode 100644 src/unpack.h create mode 100644 vixen.yml diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..736ee8e --- /dev/null +++ b/.clang-format @@ -0,0 +1,28 @@ +--- +BasedOnStyle: Webkit +--- +Language: Cpp +PointerAlignment: Right +BreakBeforeBraces: Allman +AllowShortBlocksOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +Cpp11BracedListStyle: true +Standard: Cpp11 +ColumnLimit: 120 +IndentWidth: 2 +AccessModifierOffset: -2 +FixNamespaceComments: true +NamespaceIndentation: None +IncludeBlocks: Regroup +IndentCaseLabels: true +MaxEmptyLinesToKeep: 1 +UseTab: Never +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: true +AlignOperands: true +AlignTrailingComments: true +AllowShortCaseLabelsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +CommentPragmas: '^ NOLINT' +--- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f77fc6d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,43 @@ +cmake_minimum_required(VERSION 3.2) + +if(NOT DEFINED CMAKE_TOOLCHAIN_FILE) + if(DEFINED ENV{VITASDK}) + set(CMAKE_TOOLCHAIN_FILE "$ENV{VITASDK}/share/vita.toolchain.cmake" CACHE PATH "toolchain file") + else() + message(FATAL_ERROR "Please define VITASDK to point to your SDK path!") + endif() +endif() + +project(vixen) +include("${VITASDK}/share/vita.cmake" REQUIRED) + +add_executable(${PROJECT_NAME} + src/main.c + src/unpack.c + src/devicelist.c + src/controller.c + src/controllers/xbox_360_controller.c + src/controllers/xbox_360w_controller.c +) + +target_link_libraries(${PROJECT_NAME} + SceDebugForDriver_stub + SceSblAIMgrForDriver_stub + SceCtrlForDriver_stub + SceKernelSuspendForDriver_stub + SceSysclibForDriver_stub + SceSysmemForDriver_stub + SceThreadmgrForDriver_stub + SceUsbdForDriver_stub + SceUsbServForDriver_stub + taihenForKernel_stub +) + +set_target_properties(${PROJECT_NAME} + PROPERTIES LINK_FLAGS "-nostdlib" +) + +vita_create_self(${PROJECT_NAME}.skprx ${PROJECT_NAME} + CONFIG vixen.yml + UNSAFE +) diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..3a8d98e --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,7 @@ +Copyright 2022 Cat (Epifanov Ivan) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9213c15 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# ViXEn - **Vi**ta **X**-input **En**abler + +PSVita kernel driver for x-input gamepads (e.g. xbox360) + +## Features + +* Support for up to 4 wired usb x-input devices. +* Support for up to 4 wireless Xbox 360 gamepads via 1 wired usb receiver. +* Auto turn-off wireless controllers on suspend. + +Note: xbox 360 wireless receiver takes over all 4 gamepad ports, so you can't use wired and wireless gamepads simultaniously. + +For full list of supported devices see [here](src/devicelist.c) + +## Installing +* Add vixen.skprx under `*KERNEL` in tai config and reboot. +* For vita you need usb Y-cable and external power. See [this](https://github.com/isage/vita-usb-ether#hardware) for example. + +## Building + +* Install vitausb from https://github.com/isage/vita-packages-extra +* `mkdir build && cmake -DCMAKE_BUILD_TYPE=Release .. && make install` + +## License + +MIT, see LICENSE.md + +## Credits + +* [xboxdrv](https://github.com/xboxdrv/xboxdrv) - for vid/pid pairs and protocol description +* [xerpi](https://github.com/xerpi) - for ds3vita +* **CBPS discord** - for support and stupid ideas +* **rem** - for being lazy/buzy to do the same :P \ No newline at end of file diff --git a/src/controller.c b/src/controller.c new file mode 100644 index 0000000..1b480b4 --- /dev/null +++ b/src/controller.c @@ -0,0 +1,67 @@ +#include "controller.h" + +#include "controllers/xbox_360_controller.h" +#include "controllers/xbox_360w_controller.h" + +#include +#include +#include + +void on_read_data(int32_t result, int32_t count, void *arg) +{ + // process buffer + Controller *c = (Controller *)arg; + if (result == 0 && count > 0 && arg) + { + if (c->inited) + { + if (c->type == PAD_XBOX360) + { + if (Xbox360Controller_processReport(c, count)) + ksceKernelPowerTick(0); // cancel sleep timers. + } + else + { + if (Xbox360WController_processReport(c, count)) + ksceKernelPowerTick(0); // cancel sleep timers. + } + } + } + + usb_read(c); +} + +void on_write_data(int32_t result, int32_t count, void *arg) +{ + // ksceDebugPrintf("write status: %d %d\n", result, count); + // check status + // do nothing? +} + +void usb_read(Controller *c) +{ + int ret; + + if (!c->inited) + return; + + ret = ksceUsbdInterruptTransfer(c->pipe_in, c->buffer, c->buffer_size, on_read_data, c); + + if (ret < 0) + { + ksceDebugPrintf("ksceUsbdInterruptTransfer(in) error: 0x%08x\n", ret); + // error out + } +} + +void usb_write(Controller *c, uint8_t *data, int len) +{ + int ret; + ret = ksceUsbdInterruptTransfer(c->pipe_out, data, len, on_write_data, c); + + if (ret < 0) + { + ksceDebugPrintf("ksceUsbdInterruptTransfer(out) error: 0x%08x\n", ret); + // error out + } +} diff --git a/src/controller.h b/src/controller.h new file mode 100644 index 0000000..9baf53f --- /dev/null +++ b/src/controller.h @@ -0,0 +1,41 @@ +#ifndef __CONTROLLER_H__ +#define __CONTROLLER_H__ + +#include "devicelist.h" + +#include +#include +#include +#include + +typedef struct +{ + uint32_t buttons; + uint8_t leftX; + uint8_t leftY; + uint8_t rightX; + uint8_t rightY; + uint8_t lt; + uint8_t rt; +} ControlData; + +typedef struct +{ + uint8_t type; + uint8_t attached; // actual gamepad attached + uint8_t inited; // usb device attached and inited + uint8_t battery_level; + ControlData controlData; + int device_id; + uint8_t port; + SceUID pipe_in; + SceUID pipe_out; + unsigned char buffer[32] __attribute__((aligned(64))); + size_t buffer_size; + +} Controller; + +void usb_read(Controller *c); +void usb_write(Controller *c, uint8_t *data, int len); + +#endif // __CONTROLLER_H__ diff --git a/src/controllers/xbox_360_controller.c b/src/controllers/xbox_360_controller.c new file mode 100644 index 0000000..e99c5f7 --- /dev/null +++ b/src/controllers/xbox_360_controller.c @@ -0,0 +1,223 @@ +#include "xbox_360_controller.h" + +#include "../unpack.h" +#include "xbox_360_report.h" + +#include +#include + +#define USB_ENDPOINT_OUT 0x02 +#define USB_ENDPOINT_IN 0x81 + +uint8_t Xbox360Controller_probe(Controller *c, int device_id, int port) +{ + c->type = PAD_XBOX360; + c->buffer_size = 32; + c->device_id = device_id; + c->port = port; + c->battery_level = 5; + + // init endpoints and stuff + SceUsbdEndpointDescriptor *endpoint; +#if defined(DEBUG) + ksceDebugPrintf("scanning endpoints for device %d\n", device_id); +#endif + endpoint = (SceUsbdEndpointDescriptor *)ksceUsbdScanStaticDescriptor(device_id, 0, SCE_USBD_DESCRIPTOR_ENDPOINT); + while (endpoint) + { +#if defined(DEBUG) + ksceDebugPrintf("got EP: %02x\n", endpoint->bEndpointAddress); +#endif + if (endpoint->bEndpointAddress == USB_ENDPOINT_IN) + { +#if defined(DEBUG) + ksceDebugPrintf("opening in pipe\n"); +#endif + c->pipe_in = ksceUsbdOpenPipe(device_id, endpoint); +#if defined(DEBUG) + ksceDebugPrintf("= 0x%08x\n", c->pipe_in); + ksceDebugPrintf("bmAttributes = %x\n", endpoint->bmAttributes); +#endif + } + else if (endpoint->bEndpointAddress == USB_ENDPOINT_OUT) + { +#if defined(DEBUG) + ksceDebugPrintf("opening out pipe\n"); +#endif + c->pipe_out = ksceUsbdOpenPipe(device_id, endpoint); +#if defined(DEBUG) + ksceDebugPrintf("= 0x%08x\n", c->pipe_out); + ksceDebugPrintf("bmAttributes = %x\n", endpoint->bmAttributes); +#endif + } + endpoint + = (SceUsbdEndpointDescriptor *)ksceUsbdScanStaticDescriptor(device_id, endpoint, SCE_USBD_DESCRIPTOR_ENDPOINT); + } + + if (c->pipe_in > 0 && c->pipe_out > 0) + { + SceUsbdConfigurationDescriptor *cdesc; + if ((cdesc = (SceUsbdConfigurationDescriptor *)ksceUsbdScanStaticDescriptor(device_id, NULL, + SCE_USBD_DESCRIPTOR_CONFIGURATION)) + == NULL) + return 0; + + SceUID control_pipe_id = ksceUsbdOpenPipe(device_id, NULL); + // set default config + int r = ksceUsbdSetConfiguration(control_pipe_id, cdesc->bConfigurationValue, NULL, NULL); +#if defined(DEBUG) + ksceDebugPrintf("ksceUsbdSetConfiguration = 0x%08x\n", r); +#endif + if (r < 0) + return 0; + c->attached = 1; + c->inited = 1; + } + + usb_read(c); + return 1; +} + +void Xbox360Controller_setRumble(Controller *c, uint8_t small, uint8_t large) +{ + ksceDebugPrintf("setRumble(%d, %d)\n", small, large); + uint8_t rumblecmd[] __attribute__((aligned(64))) = {0x00, 0x08, 0x08, small, large, 0x00, 0x00, 0x00}; + + usb_write(c, rumblecmd, 8); +} + +uint8_t Xbox360Controller_processReport(Controller *c, size_t length) +{ + if (length == 3 && c->buffer[0] == 0x01 && c->buffer[1] == 0x03) + { +#if defined(DEBUG) + ksceDebugPrintf("Xbox360Controller: LED Status: %d\n", (c->buffer[2])); +#endif + } + else if (length == 3 && c->buffer[0] == 0x03 && c->buffer[1] == 0x03) + { +#if defined(DEBUG) + ksceDebugPrintf("rumble status: %d\n", (c->buffer[2])); +#endif + } + else if (length == 3 && c->buffer[0] == 0x08 && c->buffer[1] == 0x03) + { +#if defined(DEBUG) + if (c->buffer[2] == 0x00) + { + ksceDebugPrintf("peripheral: none"); + } + else if (c->buffer[2] == 0x01) + { + ksceDebugPrintf("peripheral: chatpad"); + } + else if (c->buffer[2] == 0x02) + { + ksceDebugPrintf("peripheral: headset"); + } + else if (c->buffer[2] == 0x03) + { + ksceDebugPrintf("peripheral: headset, chatpad"); + } + else + { + ksceDebugPrintf("peripheral: unknown: %d", (c->buffer[2])); + } +#endif + } + else if (length == 20 && c->buffer[0] == 0x00 && c->buffer[1] == 0x14) + { + Xbox360Report report; + + report.dpad_up = bit(c->buffer + 2, 0); + report.dpad_down = bit(c->buffer + 2, 1); + report.dpad_left = bit(c->buffer + 2, 2); + report.dpad_right = bit(c->buffer + 2, 3); + + report.start = bit(c->buffer + 2, 4); + report.back = bit(c->buffer + 2, 5); + report.thumb_l = bit(c->buffer + 2, 6); + report.thumb_r = bit(c->buffer + 2, 7); + + report.lb = bit(c->buffer + 3, 0); + report.rb = bit(c->buffer + 3, 1); + report.guide = bit(c->buffer + 3, 2); + report.dummy1 = bit(c->buffer + 3, 3); + + report.a = bit(c->buffer + 3, 4); + report.b = bit(c->buffer + 3, 5); + report.x = bit(c->buffer + 3, 6); + report.y = bit(c->buffer + 3, 7); + + report.lt = c->buffer[4]; + report.rt = c->buffer[5]; + + report.x1 = *((int16_t *)(c->buffer + 6)); + report.y1 = *((int16_t *)(c->buffer + 8)); + + report.x2 = *((int16_t *)(c->buffer + 10)); + report.y2 = *((int16_t *)(c->buffer + 12)); + + report.dummy2 = *(c->buffer + 14); + report.dummy3 = *((int16_t *)(c->buffer + 18)); + + c->controlData.buttons = 0; + + if (report.a) + c->controlData.buttons |= SCE_CTRL_CROSS; + if (report.b) + c->controlData.buttons |= SCE_CTRL_CIRCLE; + if (report.y) + c->controlData.buttons |= SCE_CTRL_TRIANGLE; + if (report.x) + c->controlData.buttons |= SCE_CTRL_SQUARE; + + if (report.dpad_up) + c->controlData.buttons |= SCE_CTRL_UP; + if (report.dpad_down) + c->controlData.buttons |= SCE_CTRL_DOWN; + if (report.dpad_left) + c->controlData.buttons |= SCE_CTRL_LEFT; + if (report.dpad_right) + c->controlData.buttons |= SCE_CTRL_RIGHT; + + if (report.lb) + c->controlData.buttons |= SCE_CTRL_L1; + if (report.rb) + c->controlData.buttons |= SCE_CTRL_R1; + if (report.thumb_l) + c->controlData.buttons |= SCE_CTRL_L3; + if (report.thumb_r) + c->controlData.buttons |= SCE_CTRL_R3; + + if (report.lt > 0) + c->controlData.buttons |= SCE_CTRL_LTRIGGER; + if (report.rt > 0) + c->controlData.buttons |= SCE_CTRL_RTRIGGER; + + if (report.start) + c->controlData.buttons |= SCE_CTRL_START; + if (report.back) + c->controlData.buttons |= SCE_CTRL_SELECT; + if (report.guide) + c->controlData.buttons |= SCE_CTRL_PSBUTTON; + + c->controlData.leftX = report.x1 / 256 + 128; + c->controlData.leftY = report.y1 / 256 + 128; + c->controlData.rightX = report.x2 / 256 + 128; + c->controlData.rightY = report.y2 / 256 + 128; + + // up and down are reversed + c->controlData.leftY = 255 - c->controlData.leftY; + c->controlData.rightY = 255 - c->controlData.rightY; + + c->controlData.lt = report.lt; + c->controlData.rt = report.rt; + return 1; + } + else + { + // ksceDebugPrintf("unknown report\n"); + } + return 0; +} diff --git a/src/controllers/xbox_360_controller.h b/src/controllers/xbox_360_controller.h new file mode 100644 index 0000000..a2824e6 --- /dev/null +++ b/src/controllers/xbox_360_controller.h @@ -0,0 +1,10 @@ +#ifndef __XBOX_360_CONTROLLER_H__ +#define __XBOX_360_CONTROLLER_H__ + +#include "../controller.h" + +uint8_t Xbox360Controller_probe(Controller *c, int device_id, int port); +uint8_t Xbox360Controller_processReport(Controller *c, size_t length); +void Xbox360Controller_setRumble(Controller *c, uint8_t small, uint8_t large); + +#endif // __XBOX_360_CONTROLLER_H__ diff --git a/src/controllers/xbox_360_report.h b/src/controllers/xbox_360_report.h new file mode 100644 index 0000000..b8b0c1c --- /dev/null +++ b/src/controllers/xbox_360_report.h @@ -0,0 +1,47 @@ +#ifndef __XBOX_360_REPORT_H__ +#define __XBOX_360_REPORT_H__ + +#include "../controller.h" + +typedef struct +{ + unsigned int dpad_up : 1; + unsigned int dpad_down : 1; + unsigned int dpad_left : 1; + unsigned int dpad_right : 1; + + unsigned int start : 1; + unsigned int back : 1; + + unsigned int thumb_l : 1; + unsigned int thumb_r : 1; + + // data[3] ------------------ + unsigned int lb : 1; + unsigned int rb : 1; + unsigned int guide : 1; + unsigned int dummy1 : 1; + + unsigned int a : 1; + unsigned int b : 1; + unsigned int x : 1; + unsigned int y : 1; + + // data[4] ------------------ + unsigned int lt : 8; + unsigned int rt : 8; + + // data[6] ------------------ + int x1 : 16; + int y1 : 16; + + // data[10] ----------------- + int x2 : 16; + int y2 : 16; + + // data[14]; ---------------- + unsigned int dummy2 : 32; + unsigned int dummy3 : 16; +} Xbox360Report; + +#endif // __XBOX_360_REPORT_H__ diff --git a/src/controllers/xbox_360w_controller.c b/src/controllers/xbox_360w_controller.c new file mode 100644 index 0000000..59b3cc4 --- /dev/null +++ b/src/controllers/xbox_360w_controller.c @@ -0,0 +1,272 @@ +#include "xbox_360w_controller.h" + +#include "../unpack.h" +#include "xbox_360_report.h" + +#include +#include + +#define USB_ENDPOINT_OUT 0x01 +#define USB_ENDPOINT_IN 0x81 + +uint8_t Xbox360WController_probe(Controller *c, int device_id, int port) +{ + c->type = PAD_XBOX360W; + c->buffer_size = 32; + c->device_id = device_id; + c->port = port; + c->battery_level = 5; + + // init endpoints and stuff + SceUsbdEndpointDescriptor *endpoint; +#if defined(DEBUG) + ksceDebugPrintf("scanning endpoints\n"); +#endif + endpoint = (SceUsbdEndpointDescriptor *)ksceUsbdScanStaticDescriptor(device_id, 0, SCE_USBD_DESCRIPTOR_ENDPOINT); + while (endpoint) + { +#if defined(DEBUG) + ksceDebugPrintf("got EP: %02x\n", endpoint->bEndpointAddress); +#endif + if (endpoint->bEndpointAddress == USB_ENDPOINT_IN + port) + { +#if defined(DEBUG) + ksceDebugPrintf("opening in pipe\n"); +#endif + c->pipe_in = ksceUsbdOpenPipe(device_id, endpoint); +#if defined(DEBUG) + ksceDebugPrintf("= 0x%08x\n", c->pipe_in); + ksceDebugPrintf("bmAttributes = %x\n", endpoint->bmAttributes); +#endif + } + else if (endpoint->bEndpointAddress == USB_ENDPOINT_OUT + port) + { +#if defined(DEBUG) + ksceDebugPrintf("opening out pipe\n"); +#endif + c->pipe_out = ksceUsbdOpenPipe(device_id, endpoint); +#if defined(DEBUG) + ksceDebugPrintf("= 0x%08x\n", c->pipe_out); + ksceDebugPrintf("bmAttributes = %x\n", endpoint->bmAttributes); +#endif + } + endpoint + = (SceUsbdEndpointDescriptor *)ksceUsbdScanStaticDescriptor(device_id, endpoint, SCE_USBD_DESCRIPTOR_ENDPOINT); + } + + if (c->pipe_in > 0 && c->pipe_out > 0) + { + SceUsbdConfigurationDescriptor *cdesc; + if ((cdesc = (SceUsbdConfigurationDescriptor *)ksceUsbdScanStaticDescriptor(device_id, NULL, + SCE_USBD_DESCRIPTOR_CONFIGURATION)) + == NULL) + return 0; + + SceUID control_pipe_id = ksceUsbdOpenPipe(device_id, NULL); + // set default config + int r = ksceUsbdSetConfiguration(control_pipe_id, cdesc->bConfigurationValue, NULL, NULL); +#if defined(DEBUG) + ksceDebugPrintf("ksceUsbdSetConfiguration = 0x%08x\n", r); +#endif + if (r < 0) + return 0; + c->attached = 0; + c->inited = 1; + } + + usb_read(c); + return 1; +} + +void Xbox360WController_setLed(Controller *c, uint8_t led) +{ + uint8_t ledcmd[] __attribute__((aligned(64))) + = {0x00, 0x00, 0x08, (uint8_t)(0x40 + led), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + usb_write(c, ledcmd, 12); +} + +void Xbox360WController_setRumble(Controller *c, uint8_t small, uint8_t large) +{ + uint8_t rumblecmd[] __attribute__((aligned(64))) + = {0x00, 0x01, 0x0f, 0xc0, 0x00, small, large, 0x00, 0x00, 0x00, 0x00, 0x00}; + + usb_write(c, rumblecmd, 12); +} + +void Xbox360WController_turnOff(Controller *c) +{ + uint8_t powercmd[] __attribute__((aligned(64))) + = {0x00, 0x00, 0x08, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + usb_write(c, powercmd, 12); + c->attached = 0; +} + +uint8_t Xbox360WController_processReport(Controller *c, size_t length) +{ + if (length == 2 && c->buffer[0] == 0x08) + { // Connection Status Message + if (c->buffer[1] == 0x00) + { + ksceDebugPrintf("controler disconnected\n"); + + // reset the controller into neutral position on disconnect + c->controlData.buttons = 0; + c->controlData.leftX = 127; + c->controlData.leftY = 127; + c->controlData.rightX = 127; + c->controlData.rightY = 127; + c->controlData.lt = 0; + c->controlData.rt = 0; + c->attached = 0; + + return 1; + } + else if (c->buffer[1] == 0x80) + { + ksceDebugPrintf("connection status: controller connected\n"); + Xbox360WController_setLed(c, c->port + 2); + c->attached = 1; + return 1; + } + else if (c->buffer[1] == 0x40) + { + ksceDebugPrintf("Connection status: headset connected\n"); + return 1; + } + else if (c->buffer[1] == 0xc0) + { + ksceDebugPrintf("Connection status: controller and headset connected\n"); + Xbox360WController_setLed(c, c->port + 2); + c->attached = 1; + return 1; + } + else + { + ksceDebugPrintf("Connection status: unknown\n"); + return 1; + } + } + else if (length == 29) + { + if (c->buffer[0] == 0x00 && c->buffer[1] == 0x0f && c->buffer[2] == 0x00 && c->buffer[3] == 0xf0) + { + // Initial Announce Message + c->battery_level = c->buffer[17] / 44; // crudely map 0-255 -> 0-5 + } + else if (c->buffer[0] == 0x00 && c->buffer[1] == 0x00 && c->buffer[2] == 0x00 && c->buffer[3] == 0x13) + { + // Battery status + c->battery_level = c->buffer[4] / 44; + } + else if (c->buffer[0] == 0x00 && c->buffer[1] == 0x01 && c->buffer[2] == 0x00 && c->buffer[3] == 0xf0 + && c->buffer[4] == 0x00 && c->buffer[5] == 0x13) + { + // Event message + uint8_t *ptr = c->buffer + 4; + Xbox360Report report; + + report.dpad_up = bit(ptr + 2, 0); + report.dpad_down = bit(ptr + 2, 1); + report.dpad_left = bit(ptr + 2, 2); + report.dpad_right = bit(ptr + 2, 3); + + report.start = bit(ptr + 2, 4); + report.back = bit(ptr + 2, 5); + report.thumb_l = bit(ptr + 2, 6); + report.thumb_r = bit(ptr + 2, 7); + + report.lb = bit(ptr + 3, 0); + report.rb = bit(ptr + 3, 1); + report.guide = bit(ptr + 3, 2); + report.dummy1 = bit(ptr + 3, 3); + + report.a = bit(ptr + 3, 4); + report.b = bit(ptr + 3, 5); + report.x = bit(ptr + 3, 6); + report.y = bit(ptr + 3, 7); + + report.lt = ptr[4]; + report.rt = ptr[5]; + + report.x1 = *((int16_t *)(ptr + 6)); + report.y1 = *((int16_t *)(ptr + 8)); + + report.x2 = *((int16_t *)(ptr + 10)); + report.y2 = *((int16_t *)(ptr + 12)); + + report.dummy2 = *(ptr + 14); + report.dummy3 = *((int16_t *)(ptr + 18)); + + c->controlData.buttons = 0; + + if (report.a) + c->controlData.buttons |= SCE_CTRL_CROSS; + if (report.b) + c->controlData.buttons |= SCE_CTRL_CIRCLE; + if (report.y) + c->controlData.buttons |= SCE_CTRL_TRIANGLE; + if (report.x) + c->controlData.buttons |= SCE_CTRL_SQUARE; + + if (report.dpad_up) + c->controlData.buttons |= SCE_CTRL_UP; + if (report.dpad_down) + c->controlData.buttons |= SCE_CTRL_DOWN; + if (report.dpad_left) + c->controlData.buttons |= SCE_CTRL_LEFT; + if (report.dpad_right) + c->controlData.buttons |= SCE_CTRL_RIGHT; + + if (report.lb) + c->controlData.buttons |= SCE_CTRL_L1; + if (report.rb) + c->controlData.buttons |= SCE_CTRL_R1; + if (report.thumb_l) + c->controlData.buttons |= SCE_CTRL_L3; + if (report.thumb_r) + c->controlData.buttons |= SCE_CTRL_R3; + + if (report.lt > 0) + c->controlData.buttons |= SCE_CTRL_LTRIGGER; + if (report.rt > 0) + c->controlData.buttons |= SCE_CTRL_RTRIGGER; + + if (report.start) + c->controlData.buttons |= SCE_CTRL_START; + if (report.back) + c->controlData.buttons |= SCE_CTRL_SELECT; + if (report.guide) + c->controlData.buttons |= SCE_CTRL_PSBUTTON; + + c->controlData.leftX = report.x1 / 256 + 128; + c->controlData.leftY = report.y1 / 256 + 128; + c->controlData.rightX = report.x2 / 256 + 128; + c->controlData.rightY = report.y2 / 256 + 128; + + // up and down are reversed + c->controlData.leftY = 255 - c->controlData.leftY; + c->controlData.rightY = 255 - c->controlData.rightY; + + c->controlData.lt = report.lt; + c->controlData.rt = report.rt; + + return 1; + } + else + { +#if defined(DEBUG) +// ksceDebugPrintf("unknown packet\n"); +#endif + } + } + else + { +#if defined(DEBUG) +// ksceDebugPrintf("unknown packet\n"); +#endif + } + + return 0; +} diff --git a/src/controllers/xbox_360w_controller.h b/src/controllers/xbox_360w_controller.h new file mode 100644 index 0000000..3fe183e --- /dev/null +++ b/src/controllers/xbox_360w_controller.h @@ -0,0 +1,12 @@ +#ifndef __XBOX_360W_CONTROLLER_H__ +#define __XBOX_360W_CONTROLLER_H__ + +#include "../controller.h" + +uint8_t Xbox360WController_probe(Controller *c, int device_id, int port); +uint8_t Xbox360WController_processReport(Controller *c, size_t length); +void Xbox360WController_setLed(Controller *c, uint8_t led); +void Xbox360WController_setRumble(Controller *c, uint8_t small, uint8_t large); +void Xbox360WController_turnOff(Controller *c); + +#endif // __XBOX_360W_CONTROLLER_H__ diff --git a/src/devicelist.c b/src/devicelist.c new file mode 100644 index 0000000..539e6fe --- /dev/null +++ b/src/devicelist.c @@ -0,0 +1,74 @@ +#include "devicelist.h" + +gamepad_t _devices[] = {{PAD_XBOX360, 0x045e, 0x028e, "Microsoft X-Box 360 pad"}, + {PAD_XBOX360, 0x045e, 0x028f, "Microsoft X-Box 360 pad v2"}, + {PAD_XBOX360, 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller"}, + {PAD_XBOX360, 0x0738, 0x4726, "Mad Catz Xbox 360 Controller"}, + {PAD_XBOX360, 0x0738, 0x4728, "Mad Catz Street Fighter IV FightPad"}, + {PAD_XBOX360, 0x0738, 0x4740, "Mad Catz Beat Pad"}, + {PAD_XBOX360, 0x0738, 0xb726, "Mad Catz Xbox controller - MW2"}, + {PAD_XBOX360, 0x0738, 0xf738, "Super SFIV FightStick TE S"}, + {PAD_XBOX360, 0x0738, 0x4718, "Mad Catz Street Fighter IV FightStick SE"}, + {PAD_XBOX360, 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)"}, + {PAD_XBOX360, 0x0738, 0xbeef, "Mad Catz JOYTECH NEO SE Advanced GamePad"}, + {PAD_XBOX360, 0x0f0d, 0x000a, "Hori Co. DOA4 FightStick"}, + {PAD_XBOX360, 0x0f0d, 0x000d, "Hori Fighting Stick EX2"}, + {PAD_XBOX360, 0x0f0d, 0x0016, "Hori Real Arcade Pro.EX"}, + {PAD_XBOX360, 0x056e, 0x2004, "Elecom JC-U3613M"}, + {PAD_XBOX360, 0x24c6, 0x5501, "Hori Real Arcade Pro VX-SA"}, + {PAD_XBOX360, 0x24c6, 0x5303, "Xbox Airflo wired controller"}, + {PAD_XBOX360, 0x24c6, 0x531a, "PowerA Pro Ex"}, + {PAD_XBOX360, 0x24c6, 0x5397, "FUS1ON Tournament Controller"}, + {PAD_XBOX360, 0x24c6, 0x5503, "Hori Fighting Edge"}, + {PAD_XBOX360, 0x24c6, 0x550d, "Hori GEM Xbox controller"}, + {PAD_XBOX360, 0x24c6, 0x5b03, "Thrustmaster Ferrari 458 Racing Wheel"}, + {PAD_XBOX360, 0x162e, 0xbeef, "Joytech Neo-Se Take2"}, + {PAD_XBOX360, 0x044f, 0xb326, "Thrustmaster Gamepad GP XID"}, + {PAD_XBOX360, 0x046d, 0xc21d, "Logitech Gamepad F310"}, + {PAD_XBOX360, 0x046d, 0xc21e, "Logitech Gamepad F510"}, + {PAD_XBOX360, 0x046d, 0xc21f, "Logitech Gamepad F710"}, + {PAD_XBOX360, 0x046d, 0xc242, "Logitech Chillstream Controller"}, + {PAD_XBOX360, 0x0738, 0xcb03, "Saitek P3200 Rumble Pad - PC/Xbox 360"}, + {PAD_XBOX360, 0x0738, 0xcb02, "Saitek Cyborg Rumble Pad - PC/Xbox 360"}, + {PAD_XBOX360, 0x0e6f, 0x0201, "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller"}, + {PAD_XBOX360, 0x0e6f, 0x0105, "HSM3 Xbox360 dancepad"}, + {PAD_XBOX360, 0x0e6f, 0x0113, "Afterglow AX.1 Gamepad for Xbox 360"}, + {PAD_XBOX360, 0x0e6f, 0x0413, "Afterglow AX.1 Gamepad for Xbox 360"}, + {PAD_XBOX360, 0x0e6f, 0x0213, "Afterglow Gamepad for Xbox 360"}, + {PAD_XBOX360, 0x0e6f, 0x0401, "Logic3 Controller"}, + {PAD_XBOX360, 0x0e6f, 0x0301, "Logic3 Controller"}, + {PAD_XBOX360, 0x12ab, 0x0301, "PDP AFTERGLOW AX.1"}, + {PAD_XBOX360, 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer"}, + {PAD_XBOX360, 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller"}, + {PAD_XBOX360, 0x1bad, 0x0002, "Harmonix Guitar for Xbox 360"}, + {PAD_XBOX360, 0x1bad, 0x0003, "Harmonix Drum Kit for Xbox 360"}, + {PAD_XBOX360, 0x1bad, 0xf016, "Mad Catz Xbox 360 Controller"}, + {PAD_XBOX360, 0x1bad, 0xf018, "Mad Catz Street Fighter IV SE Fighting Stick"}, + {PAD_XBOX360, 0x1bad, 0xf021, "Mad Cats Ghost Recon FS GamePad"}, + {PAD_XBOX360, 0x1bad, 0xf023, "MLG Pro Circuit Controller (Xbox)"}, + {PAD_XBOX360, 0x1bad, 0xf028, "Street Fighter IV FightPad"}, + {PAD_XBOX360, 0x1bad, 0xf02e, "Mad Catz Fightpad"}, + {PAD_XBOX360, 0x1bad, 0xf038, "Street Fighter IV FightStick TE"}, + {PAD_XBOX360, 0x1bad, 0xf03a, "Mad Catz SFxT Fightstick Pro"}, + {PAD_XBOX360, 0x1bad, 0xf900, "Harmonix Xbox 360 Controller"}, + {PAD_XBOX360, 0x1bad, 0xf901, "Gamestop Xbox 360 Controller"}, + {PAD_XBOX360, 0x1bad, 0xf903, "Tron Xbox 360 controller"}, + {PAD_XBOX360, 0x1bad, 0xfa01, "MadCatz GamePad"}, + {PAD_XBOX360, 0x15e4, 0x3f00, "Power A Mini Pro Elite"}, + {PAD_XBOX360, 0x15e4, 0x3f10, "Batarang Xbox 360 controller"}, + {PAD_XBOX360, 0x24c6, 0x5000, "Razer Atrox Arcade Stick"}, + {PAD_XBOX360, 0x1689, 0xfd00, "Razer Onza Tournament Edition"}, + {PAD_XBOX360, 0x1689, 0xfd01, "Razer Onza Classic Edition"}, + {PAD_XBOX360, 0x1532, 0x0037, "Razer Sabertooth"}, + {PAD_XBOX360, 0x12ab, 0x0004, "Honey Bee Xbox360 dancepad"}, + {PAD_XBOX360, 0x15e4, 0x3f0a, "Xbox Airflo wired controller"}, + {PAD_XBOX360, 0x24c6, 0x5300, "PowerA MINI PROEX Controller"}, + {PAD_XBOX360, 0x24c6, 0x5500, "Hori XBOX 360 EX 2 with Turbo"}, + {PAD_XBOX360, 0x24c6, 0x5506, "Hori SOULCALIBUR V Stick"}, + {PAD_XBOX360, 0x24c6, 0x5b02, "Thrustmaster, Inc. GPX Controller"}, + {PAD_XBOX360, 0x24c6, 0x5d04, "Razer Sabertooth"}, + {PAD_XBOX360, 0x0e6f, 0x011f, "Rock Candy Gamepad Wired Controller"}, + {PAD_XBOX360, 0x0e6f, 0x021f, "Rock Candy Gamepad for Xbox 360"}, + {PAD_XBOX360W, 0x045e, 0x0719, "Xbox 360 Wireless Receiver"}, + {PAD_XBOX360W, 0x045e, 0x0291, "Xbox 360 Wireless Receiver (xbox)"}, + {PAD_UNKNOWN, 0x0000, 0x0000, "Null"}}; diff --git a/src/devicelist.h b/src/devicelist.h new file mode 100644 index 0000000..2b88724 --- /dev/null +++ b/src/devicelist.h @@ -0,0 +1,23 @@ +#ifndef __DEVICELIST_H__ +#define __DEVICELIST_H__ + +#include + +typedef enum +{ + PAD_UNKNOWN, + PAD_XBOX360, + PAD_XBOX360W +} vixenPadType; + +typedef struct +{ + vixenPadType type; + uint16_t idVendor; + uint16_t idProduct; + const char *name; +} gamepad_t; + +extern gamepad_t _devices[]; + +#endif // __DEVICELIST_H__ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..5b6ce37 --- /dev/null +++ b/src/main.c @@ -0,0 +1,409 @@ +#include "controller.h" +#include "controllers/xbox_360_controller.h" +#include "controllers/xbox_360w_controller.h" +#include "devicelist.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_CONTROLLERS 4 + +#define DECL_FUNC_HOOK(name, ...) \ + static tai_hook_ref_t name##HookRef; \ + static SceUID name##HookUid = -1; \ + static int name##HookFunc(__VA_ARGS__) + +#define BIND_FUNC_OFFSET_HOOK(name, pid, modid, segidx, offset, thumb) \ + name##HookUid = taiHookFunctionOffsetForKernel((pid), &name##HookRef, (modid), (segidx), (offset), thumb, \ + (const void *)name##HookFunc) + +#define BIND_FUNC_EXPORT_HOOK(name, pid, module, lib_nid, func_nid) \ + name##HookUid = taiHookFunctionExportForKernel((pid), &name##HookRef, (module), (lib_nid), (func_nid), \ + (const void *)name##HookFunc) + +#define UNBIND_FUNC_HOOK(name) \ + ({ \ + if (name##HookUid > 0) \ + taiHookReleaseForKernel(name##HookUid, name##HookRef); \ + }) + +static int started = 0; + +static Controller controllers[MAX_CONTROLLERS]; + +static inline int clamp(int value, int min, int max) +{ + if (value <= min) + return min; + if (value >= max) + return max; + return value; +} + +DECL_FUNC_HOOK(ksceCtrlGetControllerPortInfo, SceCtrlPortInfo *info) +{ + int ret = TAI_CONTINUE(int, ksceCtrlGetControllerPortInfoHookRef, info); + + if (ret >= 0) + { + // Spoof connected controllers to be DualShock 3 controllers + for (int i = 0; i < MAX_CONTROLLERS; i++) + { + if (controllers[i].inited && controllers[i].attached) + info->port[i + 1] = SCE_CTRL_TYPE_DS3; // no touch, so ds3 + } + } + + return ret; +} + +DECL_FUNC_HOOK(sceCtrlGetBatteryInfo, int port, uint8_t *batt) +{ + if (port > 0 && controllers[port - 1].attached && controllers[port - 1].inited) + { + // Override the battery level for connected controllers + uint8_t data; + ksceKernelMemcpyUserToKernel(&data, (void *)batt, sizeof(uint8_t)); + data = controllers[port - 1].battery_level; + ksceKernelMemcpyKernelToUser((void *)batt, &data, sizeof(uint8_t)); + return 0; + } + + return TAI_CONTINUE(int, sceCtrlGetBatteryInfoHookRef, port, batt); +} + +DECL_FUNC_HOOK(sceCtrlSetActuator, int port, const SceCtrlActuator *pState) +{ + if (port > 0 && controllers[port - 1].attached && controllers[port - 1].inited) + { + SceCtrlActuator lpState; + ksceKernelMemcpyUserToKernel(&lpState, (void *)pState, sizeof(SceCtrlActuator)); + if (controllers[port - 1].type == PAD_XBOX360) + Xbox360Controller_setRumble(&controllers[port - 1], lpState.small, lpState.large); + else + Xbox360WController_setRumble(&controllers[port - 1], lpState.small, lpState.large); + return 0; + } + + return TAI_CONTINUE(int, sceCtrlSetActuatorHookRef, port, pState); +} + +static void patchControlData(int port, SceCtrlData *data, int count, uint8_t negative, uint8_t triggers_ext) +{ + // Use controller 1 data for port 0, or controllers 1-4 for ports 1-4 + int cont = (port > 0) ? (port - 1) : 0; + + if (!controllers[cont].inited || !controllers[cont].attached) + return; + + const ControlData *controlData = &(controllers[cont].controlData); + + // Forward PS button presses to the kernel so the system menu receives them + if (controlData->buttons & SCE_CTRL_PSBUTTON) + ksceCtrlSetButtonEmulation(port, 0, 0, SCE_CTRL_PSBUTTON, 16); + + for (int i = 0; i < count; i++) + { + // Reset initial values for controller ports (port 0 is additive) + if (port > 0) + { + data[i].buttons = (negative ? 0xFFFFFFFF : 0x00000000); + data[i].lx = data[i].ly = data[i].rx = data[i].ry = 127; + } + + // Set the button data from the controller, with optional negative logic + if (negative) + data[i].buttons &= ~controlData->buttons; + else + data[i].buttons |= controlData->buttons; + + data[i].lt = clamp(controlData->lt, 0, 255); + data[i].rt = clamp(controlData->rt, 0, 255); + + // Set the stick data from the controller + data[i].lx = clamp(data[i].lx + controlData->leftX - 127, 0, 255); + data[i].ly = clamp(data[i].ly + controlData->leftY - 127, 0, 255); + data[i].rx = clamp(data[i].rx + controlData->rightX - 127, 0, 255); + data[i].ry = clamp(data[i].ry + controlData->rightY - 127, 0, 255); + } +} + +#define DECL_FUNC_HOOK_CTRL(name, negative, triggers) \ + DECL_FUNC_HOOK(name, int port, SceCtrlData *data, int count) \ + { \ + int ret = TAI_CONTINUE(int, name##HookRef, port, data, count); \ + if (ret >= 0) \ + patchControlData(port, data, count, (negative), (triggers)); \ + return ret; \ + } + +DECL_FUNC_HOOK_CTRL(ksceCtrlPeekBufferPositive, 0, 0) +DECL_FUNC_HOOK_CTRL(ksceCtrlReadBufferPositive, 0, 0) +DECL_FUNC_HOOK_CTRL(ksceCtrlPeekBufferNegative, 1, 0) +DECL_FUNC_HOOK_CTRL(ksceCtrlReadBufferNegative, 1, 0) +DECL_FUNC_HOOK_CTRL(ksceCtrlPeekBufferPositiveExt, 0, 0) +DECL_FUNC_HOOK_CTRL(ksceCtrlReadBufferPositiveExt, 0, 0) + +DECL_FUNC_HOOK_CTRL(ksceCtrlPeekBufferPositive2, 0, 1) +DECL_FUNC_HOOK_CTRL(ksceCtrlReadBufferPositive2, 0, 1) +DECL_FUNC_HOOK_CTRL(ksceCtrlPeekBufferNegative2, 1, 1) +DECL_FUNC_HOOK_CTRL(ksceCtrlReadBufferNegative2, 1, 1) +DECL_FUNC_HOOK_CTRL(ksceCtrlPeekBufferPositiveExt2, 0, 1) +DECL_FUNC_HOOK_CTRL(ksceCtrlReadBufferPositiveExt2, 0, 1) + +int libvixen_probe(int device_id); +int libvixen_attach(int device_id); +int libvixen_detach(int device_id); + +static const SceUsbdDriver libvixenDriver = { + .name = "libvixen", + .probe = libvixen_probe, + .attach = libvixen_attach, + .detach = libvixen_detach, +}; + +int libvixen_probe(int device_id) +{ + SceUsbdDeviceDescriptor *device; + ksceDebugPrintf("probing device: %x\n", device_id); + device = (SceUsbdDeviceDescriptor *)ksceUsbdScanStaticDescriptor(device_id, 0, SCE_USBD_DESCRIPTOR_DEVICE); + if (device) + { + ksceDebugPrintf("vendor: %04x\n", device->idVendor); + ksceDebugPrintf("product: %04x\n", device->idProduct); + int i; + for (i = 0; _devices[i].type != PAD_UNKNOWN; i++) + { + if (_devices[i].idVendor == device->idVendor && _devices[i].idProduct == device->idProduct) + break; + } + + if (_devices[i].type == PAD_UNKNOWN) + { + // TODO: try generic? + return SCE_USBD_PROBE_FAILED; + } + return SCE_USBD_PROBE_SUCCEEDED; + } + return SCE_USBD_PROBE_FAILED; +} + +int libvixen_attach(int device_id) +{ + SceUsbdDeviceDescriptor *device; + ksceDebugPrintf("attaching device: %x\n", device_id); + device = (SceUsbdDeviceDescriptor *)ksceUsbdScanStaticDescriptor(device_id, 0, SCE_USBD_DESCRIPTOR_DEVICE); + if (device) + { + ksceDebugPrintf("vendor: %04x\n", device->idVendor); + ksceDebugPrintf("product: %04x\n", device->idProduct); + int i; + for (i = 0; _devices[i].type != PAD_UNKNOWN; i++) + { + if (_devices[i].idVendor == device->idVendor && _devices[i].idProduct == device->idProduct) + { + ksceDebugPrintf("Found %s, attaching\n", _devices[i].name); + break; + } + } + + if (_devices[i].type == PAD_UNKNOWN) + { + // TODO: try generic? + return SCE_USBD_ATTACH_FAILED; + } + + // wireless takes all 4 ports, sorry + if (_devices[i].type == PAD_XBOX360W) + { + + for (int cont = 0; cont < MAX_CONTROLLERS; cont++) + { + Xbox360WController_probe(&controllers[cont], device_id, cont); + if (!controllers[cont].inited) + { + ksceDebugPrintf("Can't init gamepad (wireless)\n"); + return SCE_USBD_ATTACH_FAILED; + } + } + ksceDebugPrintf("Attached!\n"); + return SCE_USBD_ATTACH_SUCCEEDED; + } + else + { + int cont = -1; + + // find free slot + for (int i = 0; i < MAX_CONTROLLERS; i++) + { + if (!controllers[i].attached || !controllers[i].inited) + { + cont = i; + break; + } + } + + if (cont == -1) + return SCE_USBD_ATTACH_FAILED; + + if (!controllers[cont].attached) + { + Xbox360Controller_probe(&controllers[cont], device_id, cont); + // something gone wrong during usb init + if (!controllers[cont].inited) + { + ksceDebugPrintf("Can't init gamepad (wired)\n"); + return SCE_USBD_ATTACH_FAILED; + } + ksceDebugPrintf("Attached!\n"); + return SCE_USBD_ATTACH_SUCCEEDED; + } + } + } + return SCE_USBD_ATTACH_FAILED; +} + +int libvixen_detach(int device_id) +{ + + for (int i = 0; i < MAX_CONTROLLERS; i++) + { + if (controllers[i].inited && controllers[i].device_id == device_id) + { + if (controllers[i].type == PAD_XBOX360W) + { + for (int j = 0; j < MAX_CONTROLLERS; j++) + { + if (controllers[j].inited) + { + controllers[j].attached = 0; + controllers[j].inited = 0; + } + } + } + else + { + controllers[i].attached = 0; + controllers[i].inited = 0; + } + return SCE_USBD_DETACH_SUCCEEDED; + } + } + + return SCE_USBD_DETACH_FAILED; +} + +static int libvixen_sysevent_handler(int resume, int eventid, void *args, void *opt) +{ + if (resume && started) + { + if (ksceSblAimgrIsGenuineVITA()) + ksceUsbServMacSelect(2, 0); // re-set host mode + } + else if (started && eventid == 256) + { + // turn off wireless controllers + for (int i = 0; i < MAX_CONTROLLERS; i++) + { + if (controllers[i].attached && controllers[i].type == PAD_XBOX360W) + { + Xbox360WController_turnOff(&controllers[i]); + } + } + } + return 0; +} + +int module_start(SceSize args, void *argp) +{ + tai_module_info_t modInfo; + modInfo.size = sizeof(tai_module_info_t); + + for (int i = 0; i < MAX_CONTROLLERS; i++) + { + controllers[i].inited = 0; + controllers[i].attached = 0; + } + + // Hook controller info functions + BIND_FUNC_EXPORT_HOOK(ksceCtrlGetControllerPortInfo, KERNEL_PID, "SceCtrl", TAI_ANY_LIBRARY, 0xF11D0D30); + BIND_FUNC_EXPORT_HOOK(sceCtrlGetBatteryInfo, KERNEL_PID, "SceCtrl", TAI_ANY_LIBRARY, 0x8F9B1CE5); + BIND_FUNC_EXPORT_HOOK(sceCtrlSetActuator, KERNEL_PID, "SceCtrl", TAI_ANY_LIBRARY, 0xDBCAA0C9); + ksceDebugPrintf("sceCtrlSetActuatorHookUid = %x\n", sceCtrlSetActuatorHookUid); + + if (taiGetModuleInfoForKernel(KERNEL_PID, "SceCtrl", &modInfo) < 0) + return SCE_KERNEL_START_FAILED; + + // Hook control data functions + BIND_FUNC_EXPORT_HOOK(ksceCtrlPeekBufferPositive, KERNEL_PID, "SceCtrl", TAI_ANY_LIBRARY, 0xEA1D3A34); + BIND_FUNC_EXPORT_HOOK(ksceCtrlReadBufferPositive, KERNEL_PID, "SceCtrl", TAI_ANY_LIBRARY, 0x9B96A1AA); + BIND_FUNC_EXPORT_HOOK(ksceCtrlPeekBufferNegative, KERNEL_PID, "SceCtrl", TAI_ANY_LIBRARY, 0x19895843); + BIND_FUNC_EXPORT_HOOK(ksceCtrlReadBufferNegative, KERNEL_PID, "SceCtrl", TAI_ANY_LIBRARY, 0x8D4E0DD1); + BIND_FUNC_OFFSET_HOOK(ksceCtrlPeekBufferPositiveExt, KERNEL_PID, modInfo.modid, 0, 0x3928 | 1, 1); + BIND_FUNC_OFFSET_HOOK(ksceCtrlReadBufferPositiveExt, KERNEL_PID, modInfo.modid, 0, 0x3BCC | 1, 1); + + // Hook extended control data functions + BIND_FUNC_OFFSET_HOOK(ksceCtrlPeekBufferPositive2, KERNEL_PID, modInfo.modid, 0, 0x3EF8 | 1, 1); + BIND_FUNC_OFFSET_HOOK(ksceCtrlReadBufferPositive2, KERNEL_PID, modInfo.modid, 0, 0x449C | 1, 1); + BIND_FUNC_OFFSET_HOOK(ksceCtrlPeekBufferNegative2, KERNEL_PID, modInfo.modid, 0, 0x41C8 | 1, 1); + BIND_FUNC_OFFSET_HOOK(ksceCtrlReadBufferNegative2, KERNEL_PID, modInfo.modid, 0, 0x47F0 | 1, 1); + BIND_FUNC_OFFSET_HOOK(ksceCtrlPeekBufferPositiveExt2, KERNEL_PID, modInfo.modid, 0, 0x4B48 | 1, 1); + BIND_FUNC_OFFSET_HOOK(ksceCtrlReadBufferPositiveExt2, KERNEL_PID, modInfo.modid, 0, 0x4E14 | 1, 1); + + started = 1; + + if (ksceSblAimgrIsGenuineVITA()) + { + int ret = ksceUsbServMacSelect(2, 0); + ksceDebugPrintf("MAC select = 0x%08x\n", ret); + } + + int ret_drv = ksceUsbdRegisterDriver(&libvixenDriver); + ksceDebugPrintf("ksceUsbdRegisterDriver = 0x%08x\n", ret_drv); + + ksceKernelRegisterSysEventHandler("zvixen_sysevent", libvixen_sysevent_handler, NULL); + + return SCE_KERNEL_START_SUCCESS; +} + +int module_stop(SceSize args, void *argp) +{ + // Unhook controller info functions + UNBIND_FUNC_HOOK(ksceCtrlGetControllerPortInfo); + UNBIND_FUNC_HOOK(sceCtrlGetBatteryInfo); + UNBIND_FUNC_HOOK(sceCtrlSetActuator); + + // Unhook control data functions + UNBIND_FUNC_HOOK(ksceCtrlReadBufferNegative); + UNBIND_FUNC_HOOK(ksceCtrlPeekBufferPositive); + UNBIND_FUNC_HOOK(ksceCtrlReadBufferPositive); + UNBIND_FUNC_HOOK(ksceCtrlPeekBufferNegative); + UNBIND_FUNC_HOOK(ksceCtrlPeekBufferPositiveExt); + UNBIND_FUNC_HOOK(ksceCtrlReadBufferPositiveExt); + + // Unhook extended control data functions + UNBIND_FUNC_HOOK(ksceCtrlPeekBufferPositive2); + UNBIND_FUNC_HOOK(ksceCtrlReadBufferPositive2); + UNBIND_FUNC_HOOK(ksceCtrlPeekBufferNegative2); + UNBIND_FUNC_HOOK(ksceCtrlReadBufferNegative2); + UNBIND_FUNC_HOOK(ksceCtrlPeekBufferPositiveExt2); + UNBIND_FUNC_HOOK(ksceCtrlReadBufferPositiveExt2); + + return SCE_KERNEL_STOP_SUCCESS; +} + +void _start() +{ + module_start(0, NULL); +} diff --git a/src/unpack.c b/src/unpack.c new file mode 100644 index 0000000..0c95dbf --- /dev/null +++ b/src/unpack.c @@ -0,0 +1,6 @@ +#include "unpack.h" + +uint8_t bit(uint8_t *data, int bit) +{ + return (*data >> bit) & 1; +} diff --git a/src/unpack.h b/src/unpack.h new file mode 100644 index 0000000..4a301ed --- /dev/null +++ b/src/unpack.h @@ -0,0 +1,8 @@ +#ifndef __UNPACK_H__ +#define __UNPACK_H__ + +#include + +uint8_t bit(uint8_t *data, int bit); + +#endif // __UNPACK_H__ diff --git a/vixen.yml b/vixen.yml new file mode 100644 index 0000000..d28a0d2 --- /dev/null +++ b/vixen.yml @@ -0,0 +1,8 @@ +vixen: + attributes: 0 + version: + major: 1 + minor: 0 + main: + start: module_start + stop: module_stop