Skip to content

Commit

Permalink
Add exports, vpk
Browse files Browse the repository at this point in the history
  • Loading branch information
xerpi committed Apr 11, 2017
1 parent 1e71b40 commit 5d94c71
Show file tree
Hide file tree
Showing 12 changed files with 509 additions and 22 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# vitastick

vitastick is a plugin and an application that lets you use a PSVita as a USB controller.
It uses the UDCD (USB Device Controller Driver) infrastructure in the kernel to simulate such controller, and thus, the host thinks the PSVita is a legit USB gamepad.

**Download**: https://github.com/xerpi/vitastick/releases

**Features:**
* When the VPK is activated, it reduces the clock frequencies to reduce power consumption

**Installation:**

1. Add vitastick.skprx to taiHEN's config (ux0:/tai/config.txt):
```
*KERNEL
ux0:tai/vitastick.skprx
```
2. Install vitastick.vpk

**Usage:**

1. Open the VPK and the Vita should switch to USB controller mode

**Note**: If you use Mai, don't put the plugin inside ux0:/plugins because Mai will load all stuff you put in there...
3 changes: 1 addition & 2 deletions CMakeLists.txt → skprx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
endif()

project(vitastick)
include("${VITASDK}/share/vita.cmake" REQUIRED)
include("$ENV{VITASDK}/share/vita.cmake" REQUIRED)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-q -Wall -O2 -nostdlib")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fno-exceptions")


option(RELEASE "Release build" OFF)

if (RELEASE)
Expand Down
12 changes: 3 additions & 9 deletions log.c → skprx/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ static char log_buf[16 * 1024];
void log_reset()
{
#ifndef RELEASE
SceUID fd = ksceIoOpen(LOG_FILE,
SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 6);
if (fd < 0)
return;

ksceIoClose(fd);

memset(log_buf, 0, sizeof(log_buf));
log_buf_ptr = 0;
#endif
}

Expand All @@ -29,7 +23,7 @@ void log_write(const char *buffer, size_t length)

memcpy(log_buf + log_buf_ptr, buffer, length);

log_buf_ptr = log_buf_ptr + length;
log_buf_ptr += length;
#endif
}

Expand All @@ -39,7 +33,7 @@ void log_flush()
ksceIoMkdir(LOG_PATH, 6);

SceUID fd = ksceIoOpen(LOG_FILE,
SCE_O_WRONLY | SCE_O_CREAT | SCE_O_APPEND, 6);
SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 6);
if (fd < 0)
return;

Expand Down
File renamed without changes.
32 changes: 21 additions & 11 deletions main.c → skprx/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <psp2/touch.h>
#include <psp2/motion.h>
#include <taihen.h>
#include "vitastick_uapi.h"
#include "uapi/vitastick_uapi.h"
#include "usb_descriptors.h"
#include "log.h"

Expand All @@ -27,7 +27,7 @@ struct gamepad_report_t {
#define EVF_DISCONNECTED (1 << 1)
#define EVF_EXIT (1 << 2)
#define EVF_INT_REQ_COMPLETED (1 << 3)
#define EVF_MASK (EVF_INT_REQ_COMPLETED | (EVF_INT_REQ_COMPLETED - 1))
#define EVF_ALL_MASK (EVF_INT_REQ_COMPLETED | (EVF_INT_REQ_COMPLETED - 1))

static SceUID usb_thread_id;
static SceUID usb_event_flag_id;
Expand Down Expand Up @@ -104,7 +104,7 @@ static int send_hid_report_init(uint8_t report_id)
.physicalAddress = NULL
};

return TEST_CALL(ksceUdcdReqSend, &req);
return ksceUdcdReqSend(&req);
}

static void hid_report_on_complete(SceUdcdDeviceRequest *req)
Expand Down Expand Up @@ -178,7 +178,7 @@ static int send_hid_report(uint8_t report_id)
.physicalAddress = NULL
};

return ksceUdcdReqSend(&req);
return TEST_CALL(ksceUdcdReqSend, &req);
}

static int vitastick_udcd_process_request(int recipient, int arg, SceUdcdEP0DeviceRequest *req)
Expand Down Expand Up @@ -226,7 +226,6 @@ static int vitastick_udcd_process_request(int recipient, int arg, SceUdcdEP0Devi

switch (descriptor_type) {
case HID_DESCRIPTOR_REPORT:
//usb_connected = 1;
send_hid_report_desc();
break;
}
Expand Down Expand Up @@ -286,6 +285,8 @@ static int vitastick_udcd_attach(int usb_version)
{
LOG("vitastick_udcd_attach %d\n", usb_version);

ksceUdcdClearFIFO(&endpoints[1]);

return 0;
}

Expand Down Expand Up @@ -349,7 +350,7 @@ static int usb_thread(SceSize args, void *argp)
int ret;
unsigned int evf_out;

ret = ksceKernelWaitEventFlagCB(usb_event_flag_id, EVF_MASK,
ret = ksceKernelWaitEventFlagCB(usb_event_flag_id, EVF_ALL_MASK,
SCE_EVENT_WAITOR | SCE_EVENT_WAITCLEAR_PAT, &evf_out, NULL);
if (ret < 0)
break;
Expand All @@ -359,8 +360,8 @@ static int usb_thread(SceSize args, void *argp)
if (evf_out & EVF_EXIT) {
break;
} else if (evf_out & EVF_CONNECTED) {
connected = 1;
send_hid_report(1);
if (send_hid_report(1) >= 0)
connected = 1;
} else if (evf_out & EVF_DISCONNECTED) {
connected = 0;
} else if (evf_out & EVF_INT_REQ_COMPLETED && connected) {
Expand All @@ -371,7 +372,6 @@ static int usb_thread(SceSize args, void *argp)
return 0;
}


/*
* Exports to userspace
*/
Expand All @@ -382,6 +382,8 @@ int vitastick_start(void)

ENTER_SYSCALL(state);

log_reset();

LOG("vitastick_start\n");

if (!vitastick_driver_registered) {
Expand Down Expand Up @@ -416,6 +418,8 @@ int vitastick_start(void)
goto err;
}

ksceKernelClearEventFlag(usb_event_flag_id, ~EVF_ALL_MASK);

ret = ksceUdcdActivate(VITASTICK_USB_PID);
if (ret < 0) {
LOG("Error activating the " VITASTICK_DRIVER_NAME " driver (0x%08X)\n", ret);
Expand All @@ -440,19 +444,24 @@ int vitastick_stop(void)

ENTER_SYSCALL(state);

if (vitastick_driver_activated) {
if (!vitastick_driver_activated) {
EXIT_SYSCALL(state);
return VITASTICK_ERROR_DRIVER_NOT_ACTIVATED;
}

ksceKernelSetEventFlag(usb_event_flag_id, EVF_DISCONNECTED);

ksceUdcdDeactivate();
ksceUdcdStop(VITASTICK_DRIVER_NAME, 0, NULL);
ksceUdcdStop("USBDeviceControllerDriver", 0, NULL);
ksceUdcdStart("USB_MTP_Driver", 0, 0);
ksceUdcdStart("USBDeviceControllerDriver", 0, NULL);
ksceUdcdStart("USB_MTP_Driver", 0, NULL);
ksceUdcdActivate(0x4E4);

vitastick_driver_activated = 0;

log_flush();

EXIT_SYSCALL(state);
return 0;
}
Expand Down Expand Up @@ -493,6 +502,7 @@ int module_start(SceSize argc, const void *args)
goto err_unregister;
}

vitastick_driver_activated = 0;
vitastick_driver_registered = 1;

LOG("vitastick started successfully!\n");
Expand Down
20 changes: 20 additions & 0 deletions skprx/uapi/vitastick_uapi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef VITASTICK_UAPI_H
#define VITASTICK_UAPI_H

#ifdef __cplusplus
extern "C" {
#endif

#define VITASTICK_ERROR_DRIVER_NOT_REGISTERED 0x81337000
#define VITASTICK_ERROR_DRIVER_NOT_ACTIVATED 0x81337001
#define VITASTICK_ERROR_DRIVER_ALREADY_ACTIVATED 0x81337002

int vitastick_start(void);
int vitastick_stop(void);

#ifdef __cplusplus
}
#endif


#endif
File renamed without changes.
File renamed without changes.
64 changes: 64 additions & 0 deletions vpk/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
cmake_minimum_required(VERSION 2.8)

set(CMAKE_SYSTEM_NAME "Generic")
set(CMAKE_C_COMPILER "arm-vita-eabi-gcc")
set(CMAKE_CXX_COMPILER "arm-vita-eabi-g++")

project(vitatsick)
set(TITLE_ID "VITASTICK")
set(TITLE_NAME "Vitastick")

set(CMAKE_C_FLAGS "-Wl,-q -Wall -O2 -std=gnu99")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11 -fno-rtti -fno-exceptions")

include_directories(
${PROJECT_SOURCE_DIR}/../skprx/uapi
)

link_directories(
${CMAKE_CURRENT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/../skprx/build/stubs
)

if (NOT ${RELEASE})
add_definitions(-DENABLE_LOGGING)
endif()

add_executable(${PROJECT_NAME}.elf
main.c
debugScreenFont.c
)

target_link_libraries(${PROJECT_NAME}.elf
SceDisplay_stub
SceCtrl_stub
ScePower_stub
vitastick_stub
)

add_custom_target(${PROJECT_NAME}.velf ALL
COMMAND vita-elf-create ${PROJECT_NAME}.elf ${PROJECT_NAME}.velf
)

add_custom_target(eboot.bin ALL
COMMAND vita-make-fself ${PROJECT_NAME}.velf eboot.bin
)

add_custom_target(${PROJECT_NAME}.vpk ALL
COMMAND vita-mksfoex -s TITLE_ID=${TITLE_ID} "${TITLE_NAME}" param.sfo
COMMAND vita-pack-vpk -s param.sfo -b eboot.bin ${PROJECT_NAME}.vpk
)

add_custom_target(vpksend
COMMAND curl -T ${PROJECT_NAME}.vpk ftp://$(PSVITAIP):1337/ux0:/
DEPENDS ${PROJECT_NAME}.vpk
)

add_custom_target(send
COMMAND curl -T eboot.bin ftp://$(PSVITAIP):1337/ux0:/app/${TITLE_ID}/
DEPENDS eboot.bin
)

add_dependencies(${PROJECT_NAME}.velf ${PROJECT_NAME}.elf)
add_dependencies(eboot.bin ${PROJECT_NAME}.velf)
add_dependencies(${PROJECT_NAME}.vpk eboot.bin)
Loading

0 comments on commit 5d94c71

Please sign in to comment.