Skip to content

Commit

Permalink
Refactor USB storage initialization and deinitialization
Browse files Browse the repository at this point in the history
- Modularized the loading of kernel modules using an array for easier maintenance
- Added safety checks before stopping and unloading modules
- Improved error handling during USB driver startup and capacity setting
- Enhanced code readability with consistent formatting and comments
- Ensured proper file system handling to prevent corruption on memory stick
  • Loading branch information
dogo committed Sep 14, 2024
1 parent 4a14d7f commit 643ab83
Showing 1 changed file with 64 additions and 40 deletions.
104 changes: 64 additions & 40 deletions src/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,95 @@
#include "oslib.h"
#include "usb.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Globals:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Global variable to store module IDs
SceUID modules[8];

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Private functions
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void oslStopUnloadModule(SceUID modID){
int status = 0;
sceKernelStopModule(modID, 0, NULL, &status, NULL);
sceKernelUnloadModule(modID);
// Helper function to stop and unload a module
static void oslStopUnloadModule(SceUID modID) {
if (modID >= 0) {
int status = 0;
sceKernelStopModule(modID, 0, NULL, &status, NULL);
sceKernelUnloadModule(modID);
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int oslInitUsbStorage() {
u32 retVal = 0;

//start necessary drivers
modules[0] = pspSdkLoadStartModule("flash0:/kd/chkreg.prx", PSP_MEMORY_PARTITION_KERNEL);
modules[1] = pspSdkLoadStartModule("flash0:/kd/npdrm.prx", PSP_MEMORY_PARTITION_KERNEL);
modules[2] = pspSdkLoadStartModule("flash0:/kd/semawm.prx", PSP_MEMORY_PARTITION_KERNEL);
modules[3] = pspSdkLoadStartModule("flash0:/kd/usbstor.prx", PSP_MEMORY_PARTITION_KERNEL);
modules[4] = pspSdkLoadStartModule("flash0:/kd/usbstormgr.prx", PSP_MEMORY_PARTITION_KERNEL);
modules[5] = pspSdkLoadStartModule("flash0:/kd/usbstorms.prx", PSP_MEMORY_PARTITION_KERNEL);
modules[6] = pspSdkLoadStartModule("flash0:/kd/usbstorboot.prx", PSP_MEMORY_PARTITION_KERNEL);
modules[7] = pspSdkLoadStartModule("flash0:/kd/usbdevice.prx", PSP_MEMORY_PARTITION_KERNEL);

//setup USB drivers
int oslInitUsbStorage() {
u32 retVal = 0;

// Load and start necessary kernel modules
const char* modulePaths[] = {
"flash0:/kd/chkreg.prx",
"flash0:/kd/npdrm.prx",
"flash0:/kd/semawm.prx",
"flash0:/kd/usbstor.prx",
"flash0:/kd/usbstormgr.prx",
"flash0:/kd/usbstorms.prx",
"flash0:/kd/usbstorboot.prx",
"flash0:/kd/usbdevice.prx"
};

for (int i = 0; i < 8; i++) {
modules[i] = pspSdkLoadStartModule(modulePaths[i], PSP_MEMORY_PARTITION_KERNEL);
if (modules[i] < 0) {
return -1; // Error loading module
}
}

// Start USB bus driver
retVal = sceUsbStart(PSP_USBBUS_DRIVERNAME, 0, 0);
if (retVal != 0)
return -6;
if (retVal != 0) {
return -6; // Error starting USB bus driver
}

// Start USB storage driver
retVal = sceUsbStart(PSP_USBSTOR_DRIVERNAME, 0, 0);
if (retVal != 0)
return -7;
if (retVal != 0) {
return -7; // Error starting USB storage driver
}

// Set the USB storage capacity (e.g., 8 MB)
retVal = sceUsbstorBootSetCapacity(0x800000);
if (retVal != 0)
return -8;
if (retVal != 0) {
return -8; // Error setting USB storage capacity
}

return 0;
}

int oslStartUsbStorage() {
return sceUsbActivate(0x1c8);
int oslStartUsbStorage() {
return sceUsbActivate(0x1c8); // 0x1c8 is the USB PID for storage
}

int oslStopUsbStorage() {
int retVal = sceUsbDeactivate(0x1c8);
sceIoDevctl("fatms0:", 0x0240D81E, NULL, 0, NULL, 0 ); //Avoid corrupted files
return retVal;
int oslStopUsbStorage() {
int retVal = sceUsbDeactivate(0x1c8); // Deactivate USB storage

// Send IOCTL to prevent file corruption on the memory stick
sceIoDevctl("fatms0:", 0x0240D81E, NULL, 0, NULL, 0);

return retVal;
}

int oslDeinitUsbStorage() {
int i;
unsigned long state = oslGetUsbState();
if (state & PSP_USB_ACTIVATED)
int oslDeinitUsbStorage() {
// Stop USB storage if it is still active
unsigned long state = oslGetUsbState();
if (state & PSP_USB_ACTIVATED) {
oslStopUsbStorage();
}

// Stop USB drivers
sceUsbStop(PSP_USBSTOR_DRIVERNAME, 0, 0);
sceUsbStop(PSP_USBBUS_DRIVERNAME, 0, 0);
for (i=7; i>=0; i--)
if (modules[i] >= 0)
oslStopUnloadModule(modules[i]);

// Stop and unload all loaded modules in reverse order
for (int i = 7; i >= 0; i--) {
oslStopUnloadModule(modules[i]);
}

return 0;
}

0 comments on commit 643ab83

Please sign in to comment.