Skip to content

Commit

Permalink
Work in progress USB MSC initiator mode (DO NOT MERGE)
Browse files Browse the repository at this point in the history
  • Loading branch information
PetteriAimonen committed Dec 11, 2024
1 parent 3c35a5c commit 9fdd39b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
24 changes: 15 additions & 9 deletions lib/ZuluSCSI_platform_RP2MCU/ZuluSCSI_platform_msc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
#include "ZuluSCSI_platform.h"
#include "ZuluSCSI_log.h"
#include "ZuluSCSI_msc.h"
#include "ZuluSCSI_msc_initiator.h"
#include "ZuluSCSI_config.h"
#include "ZuluSCSI_settings.h"
#include <class/msc/msc.h>
#include <class/msc/msc_device.h>


#if CFG_TUD_MSC_EP_BUFSIZE < SD_SECTOR_SIZE
#error "CFG_TUD_MSC_EP_BUFSIZE is too small! It needs to be at least 512 (SD_SECTOR_SIZE)"
#endif
Expand Down Expand Up @@ -110,6 +110,8 @@ void __USBInstallMassStorage() { }
extern "C" void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8],
uint8_t product_id[16], uint8_t product_rev[4]) {

if (g_msc_initiator) return init_msc_inquiry_cb(lun, vendor_id, product_id, product_rev);

const char vid[] = "ZuluSCSI";
const char pid[] = PLATFORM_PID;
const char rev[] = "1.0";
Expand All @@ -122,6 +124,8 @@ extern "C" void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8],
// max LUN supported
// we only have the one SD card
extern "C" uint8_t tud_msc_get_maxlun_cb(void) {
if (g_msc_initiator) return init_msc_get_maxlun_cb();

return 1; // number of LUNs supported
}

Expand All @@ -130,15 +134,16 @@ extern "C" uint8_t tud_msc_get_maxlun_cb(void) {
// otherwise this is not actually needed
extern "C" bool tud_msc_is_writable_cb (uint8_t lun)
{
if (g_msc_initiator) return init_msc_is_writable_cb(lun);

(void) lun;
return unitReady;
}

// see https://www.seagate.com/files/staticfiles/support/docs/manual/Interface%20manuals/100293068j.pdf pg 221
extern "C" bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject)
{
(void) lun;
(void) power_condition;
if (g_msc_initiator) return init_msc_start_stop_cb(lun, power_condition, start, load_eject);

if (load_eject) {
if (start) {
Expand All @@ -154,15 +159,15 @@ extern "C" bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool

// return true if we are ready to service reads/writes
extern "C" bool tud_msc_test_unit_ready_cb(uint8_t lun) {
(void) lun;
if (g_msc_initiator) return init_msc_test_unit_ready_cb(lun);

return unitReady;
}

// return size in blocks and block size
extern "C" void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count,
uint16_t *block_size) {
(void) lun;
if (g_msc_initiator) return init_msc_capacity_cb(lun, block_count, block_size);

*block_count = unitReady ? (SD.card()->sectorCount()) : 0;
*block_size = SD_SECTOR_SIZE;
Expand All @@ -172,6 +177,7 @@ extern "C" void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count,
// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE, READ10 and WRITE10
extern "C" int32_t tud_msc_scsi_cb(uint8_t lun, const uint8_t scsi_cmd[16], void *buffer,
uint16_t bufsize) {
if (g_msc_initiator) return init_msc_scsi_cb(lun, scsi_cmd, buffer, bufsize);

const void *response = NULL;
uint16_t resplen = 0;
Expand Down Expand Up @@ -209,7 +215,7 @@ extern "C" int32_t tud_msc_scsi_cb(uint8_t lun, const uint8_t scsi_cmd[16], void
extern "C" int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset,
void* buffer, uint32_t bufsize)
{
(void) lun;
if (g_msc_initiator) return init_msc_read10_cb(lun, lba, offset, buffer, bufsize);

bool rc = SD.card()->readSectors(lba, (uint8_t*) buffer, bufsize/SD_SECTOR_SIZE);

Expand All @@ -224,7 +230,7 @@ extern "C" int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset,
// Process data in buffer to disk's storage and return number of written bytes (must be multiple of block size)
extern "C" int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset,
uint8_t *buffer, uint32_t bufsize) {
(void) lun;
if (g_msc_initiator) return init_msc_read10_cb(lun, lba, offset, buffer, bufsize);

bool rc = SD.card()->writeSectors(lba, buffer, bufsize/SD_SECTOR_SIZE);

Expand All @@ -237,7 +243,7 @@ extern "C" int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
// used to flush any pending cache to storage
extern "C" void tud_msc_write10_complete_cb(uint8_t lun) {
(void) lun;
if (g_msc_initiator) return init_msc_write10_complete_cb(lun);
}

#endif
#endif
16 changes: 11 additions & 5 deletions src/ZuluSCSI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ static void firmware_update()

// Place all the setup code that requires the SD card to be initialized here
// Which is pretty much everything after platform_init and and platform_late_init
static void zuluscsi_setup_sd_card()
static void zuluscsi_setup_sd_card(bool wait_for_card = true)
{

g_sdcard_present = mountSDCard();
Expand All @@ -980,7 +980,7 @@ static void zuluscsi_setup_sd_card()
blinkStatus(BLINK_ERROR_NO_SD_CARD);
platform_reset_watchdog();
g_sdcard_present = mountSDCard();
} while (!g_sdcard_present);
} while (!g_sdcard_present && wait_for_card);
blink_cancel();
LED_OFF();

Expand Down Expand Up @@ -1062,12 +1062,18 @@ extern "C" void zuluscsi_setup(void)
{
platform_init();
platform_late_init();
zuluscsi_setup_sd_card();

bool is_initiator = false;
#ifdef PLATFORM_HAS_INITIATOR_MODE
is_initiator = platform_is_initiator_mode_enabled();
#endif

zuluscsi_setup_sd_card(!is_initiator);

#ifdef PLATFORM_MASS_STORAGE
static bool check_mass_storage = true;
if ((check_mass_storage && g_scsi_settings.getSystem()->enableUSBMassStorage)
|| platform_rebooted_into_mass_storage())
if (((check_mass_storage && g_scsi_settings.getSystem()->enableUSBMassStorage)
|| platform_rebooted_into_mass_storage()) && !is_initiator)
{
check_mass_storage = false;

Expand Down
20 changes: 20 additions & 0 deletions src/ZuluSCSI_initiator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "ZuluSCSI_log.h"
#include "ZuluSCSI_log_trace.h"
#include "ZuluSCSI_initiator.h"
#include "ZuluSCSI_msc_initiator.h"
#include "ZuluSCSI_msc.h"
#include <ZuluSCSI_platform.h>
#include <minIni.h>
#include "SdFat.h"
Expand Down Expand Up @@ -137,6 +139,11 @@ void scsiInitiatorInit()

}

int scsiInitiatorGetOwnID()
{
return g_initiator_state.initiator_id;
}

// Update progress bar LED during transfers
static void scsiInitiatorUpdateLed()
{
Expand Down Expand Up @@ -202,6 +209,19 @@ void scsiInitiatorMainLoop()
scsiHostPhyReset();
}

#ifdef PLATFORM_MASS_STORAGE
if (!g_msc_initiator)
{
logmsg("Entering MSC initiator mode");
platform_enter_msc();
setup_msc_initiator();
}

platform_run_msc();
#endif

return; // FIXME: debug code

if (!g_initiator_state.imaging)
{
// Scan for SCSI drives one at a time
Expand Down
3 changes: 3 additions & 0 deletions src/ZuluSCSI_initiator.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ void scsiInitiatorInit();

void scsiInitiatorMainLoop();

// Get the SCSI ID used by the initiator itself
int scsiInitiatorGetOwnID();

// Select target and execute SCSI command
int scsiInitiatorRunCommand(int target_id,
const uint8_t *command, size_t cmdLen,
Expand Down

0 comments on commit 9fdd39b

Please sign in to comment.