Skip to content

Commit

Permalink
Get logical unit number for USB
Browse files Browse the repository at this point in the history
  • Loading branch information
McCaulay committed Mar 12, 2023
1 parent ebc374f commit 4c8e4fa
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
5 changes: 5 additions & 0 deletions sdk/include/ps/sce/usb/mass-store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#define MASS_STORE_BULK_ONLY_DEFAULT_ENDPOINT_IN 0x81
#define MASS_STORE_BULK_ONLY_DEFAULT_ENDPOINT_OUT 0x02

#define MASS_STORE_BOMS_RESET 0xFF
#define MASS_STORE_BOMS_GET_MAX_LUN 0xFE

namespace PS
{
class MassStore
Expand All @@ -27,6 +30,7 @@ namespace PS
MassStore();
MassStore(PS::Sce::Usbd* usbd, uint8_t endpointIn = 0, uint8_t endpointOut = 0);

uint8_t updateLUN();
int32_t inquiry(SCSI::Inquiry* inquiry);
int32_t readCapacity(SCSI::Capacity* capacity);
int32_t readBlock(uint32_t blockAddress, uint16_t blocks, uint16_t blockSize, uint8_t* buffer);
Expand All @@ -39,6 +43,7 @@ namespace PS
private:
static uint32_t tag;
private:
uint8_t lun;
PS::Sce::Usbd* usbd;
uint8_t endpointIn;
uint8_t endpointOut;
Expand Down
2 changes: 2 additions & 0 deletions sdk/include/ps/sce/usb/usb/usb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Usb
uint16_t pid;
uint8_t endpointIn;
uint8_t endpointOut;
uint16_t endpointInMaxPacket;
uint16_t endpointOutMaxPacket;
PS::Sce::Usbd usbd;
PS::MassStore massStore;
MasterBootRecord partition;
Expand Down
5 changes: 5 additions & 0 deletions sdk/include/ps/sce/usb/usbd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
#define LIBUSB_TRANSFER_TYPE_INTERRUPT 3
#define LIBUSB_TRANSFER_TYPE_BULK_STREAM 4

#define LIBUSB_REQUEST_TYPE_STANDARD (0x00 << 5)
#define LIBUSB_REQUEST_TYPE_CLASS (0x01 << 5)
#define LIBUSB_REQUEST_TYPE_VENDOR (0x02 << 5)
#define LIBUSB_REQUEST_TYPE_RESERVED (0x03 << 5)

#define LIBUSB_REQUEST_GET_STATUS 0x00
#define LIBUSB_REQUEST_CLEAR_FEATURE 0x01
#define LIBUSB_REQUEST_SET_FEATURE 0x03
Expand Down
18 changes: 15 additions & 3 deletions sdk/src/ps/sce/usb/mass-store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ PS::MassStore::MassStore()
this->usbd = nullptr;
this->endpointIn = 0;
this->endpointOut = 0;
this->lun = 0;
}

PS::MassStore::MassStore(PS::Sce::Usbd* usbd, uint8_t endpointIn, uint8_t endpointOut)
{
this->usbd = usbd;
this->endpointIn = endpointIn;
this->endpointOut = endpointOut;
this->lun = 0;
}

uint8_t PS::MassStore::updateLUN()
{
if (this->usbd == nullptr)
return this->lun;

int32_t err = this->usbd->ControlTransfer(MASS_STORE_COMMAND_DIRECTION_DATA_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, MASS_STORE_BOMS_GET_MAX_LUN, 0, 0, &this->lun, sizeof(lun), SCE_USBD_CONTROL_TIMEOUT);
this->lun = err == SCE_OK ? lun : 0;
return this->lun;
}

int32_t PS::MassStore::inquiry(SCSI::Inquiry* inquiry)
Expand All @@ -27,7 +39,7 @@ int32_t PS::MassStore::inquiry(SCSI::Inquiry* inquiry)
.signature = MASS_STORE_CBW_SIGNATURE,
.dataTransferLength = sizeof(SCSI::Inquiry),
.flags = MASS_STORE_COMMAND_DIRECTION_DATA_IN,
.lun = 0,
.lun = this->lun,
.scsiCommandLength = 6,
.scsiCommandData = { SCSI_CMD_INQUIRY, 0x00, 0x00, 0x00, sizeof(SCSI::Inquiry), 0x00 }
};
Expand All @@ -46,7 +58,7 @@ int32_t PS::MassStore::readCapacity(SCSI::Capacity* capacity)
.signature = MASS_STORE_CBW_SIGNATURE,
.dataTransferLength = sizeof(SCSI::Capacity),
.flags = MASS_STORE_COMMAND_DIRECTION_DATA_IN,
.lun = 0,
.lun = this->lun,
.scsiCommandLength = 10,
.scsiCommandData = { SCSI_CMD_READ_CAPACITY_10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
};
Expand All @@ -69,7 +81,7 @@ int32_t PS::MassStore::readBlock(uint32_t blockAddress, uint16_t blocks, uint16_
.signature = MASS_STORE_CBW_SIGNATURE,
.dataTransferLength = (uint32_t)blocks * (uint32_t)blockSize,
.flags = MASS_STORE_COMMAND_DIRECTION_DATA_IN,
.lun = 0,
.lun = this->lun,
.scsiCommandLength = 10,
.scsiCommandData = {
SCSI_CMD_READ_10,
Expand Down
19 changes: 19 additions & 0 deletions sdk/src/ps/sce/usb/usb/usb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Usb::Usb()
this->pid = 0;
this->endpointIn = 0;
this->endpointOut = 0;
this->endpointInMaxPacket = 0;
this->endpointOutMaxPacket = 0;
this->usbd = PS::Sce::Usbd();
this->massStore = PS::MassStore(&this->usbd);
this->mounted = false;
Expand All @@ -55,6 +57,8 @@ Usb::Usb(uint64_t device)
this->pid = 0;
this->endpointIn = 0;
this->endpointOut = 0;
this->endpointInMaxPacket = 0;
this->endpointOutMaxPacket = 0;
this->usbd = PS::Sce::Usbd();
this->massStore = PS::MassStore(&this->usbd);
this->mounted = false;
Expand All @@ -74,6 +78,8 @@ Usb::Usb(uint16_t vid, uint16_t pid)
this->pid = pid;
this->endpointIn = 0;
this->endpointOut = 0;
this->endpointInMaxPacket = 0;
this->endpointOutMaxPacket = 0;
this->usbd = PS::Sce::Usbd(this->vid, this->pid);
this->massStore = PS::MassStore(&this->usbd);
this->mounted = false;
Expand Down Expand Up @@ -190,13 +196,22 @@ bool Usb::updateDescriptors()
PS::Debug.printf(" Type: 0x%02x\n", endpoint.descriptorType);
PS::Debug.printf(" Address: 0x%02x\n", endpoint.endpointAddress);
PS::Debug.printf(" Attributes: 0x%02x\n", endpoint.attributes);
PS::Debug.printf(" Max Packet Size: 0x%04x\n", endpoint.maxPacketSize);
PS::Debug.printf(" Polling Interval: 0x%02x\n", endpoint.interval);
PS::Debug.printf(" Refresh: 0x%02x\n", endpoint.refresh);

if ((endpoint.attributes & LIBUSB_TRANSFER_TYPE_MASK) & (LIBUSB_TRANSFER_TYPE_BULK | LIBUSB_TRANSFER_TYPE_INTERRUPT))
{
if (endpoint.endpointAddress & LIBUSB_ENDPOINT_IN)
{
this->endpointIn = this->endpointIn ? this->endpointIn : endpoint.endpointAddress;
this->endpointInMaxPacket = this->endpointInMaxPacket ? this->endpointInMaxPacket : endpoint.maxPacketSize;
}
else
{
this->endpointOut = this->endpointOut ? this->endpointOut : endpoint.endpointAddress;
this->endpointOutMaxPacket = this->endpointOutMaxPacket ? this->endpointOutMaxPacket : endpoint.maxPacketSize;
}
}
}
}
Expand Down Expand Up @@ -230,6 +245,10 @@ bool Usb::mount()

PS::Debug.printf("Mounting USB: VID=%04x, PID=%04x\n", this->vid, this->pid);

// Get max logical unit number (LUN)
uint8_t lun = this->massStore.updateLUN();
PS::Debug.printf("LUN: %i\n", lun);

SCSI::Inquiry inquiry;
if (this->massStore.inquiry(&inquiry) != SCE_OK)
return false;
Expand Down

0 comments on commit 4c8e4fa

Please sign in to comment.