Skip to content

Commit

Permalink
add ready cb to bdm
Browse files Browse the repository at this point in the history
  • Loading branch information
KrahJohlito committed Oct 8, 2024
1 parent d1818f4 commit 5974ac7
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 11 deletions.
8 changes: 5 additions & 3 deletions common/include/usbhdfsd-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ typedef struct bd_fragment {

// Device status bits.
/** CONNected */
#define USBMASS_DEV_STAT_CONN 0x01
#define USBMASS_DEV_STAT_CONN 0x01
/** CONFigured */
#define USBMASS_DEV_STAT_CONF 0x02
#define USBMASS_DEV_STAT_CONF 0x02
/** READY for I/O */
#define USBMASS_DEV_STAT_READY 0x04
/** ERRor */
#define USBMASS_DEV_STAT_ERR 0x80
#define USBMASS_DEV_STAT_ERR 0x80

// Device events
enum USBMASS_DEV_EV {
Expand Down
14 changes: 12 additions & 2 deletions iop/fs/bdm/include/bdm.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
#include <irx.h>
#include <types.h>

/* Event flag bits */
#define BDM_EVENT_CB_MOUNT 0x01
#define BDM_EVENT_CB_UMOUNT 0x02
#define BDM_EVENT_MOUNT 0x04
#define BDM_EVENT_DEVICE_READY 0x08

typedef void (*bdm_cb)(int event);

struct block_device
{
// Private driver data
Expand All @@ -39,6 +47,8 @@ struct block_device
int (*write)(struct block_device *bd, u64 sector, const void *buffer, u16 count);
void (*flush)(struct block_device *bd);
int (*stop)(struct block_device *bd);

bdm_cb device_ready_callback;
};

struct file_system
Expand All @@ -54,15 +64,14 @@ struct file_system
void (*disconnect_bd)(struct block_device *bd);
};

typedef void (*bdm_cb)(int event);

// Exported functions
void bdm_connect_bd(struct block_device *bd);
void bdm_disconnect_bd(struct block_device *bd);
void bdm_connect_fs(struct file_system *fs);
void bdm_disconnect_fs(struct file_system *fs);
void bdm_get_bd(struct block_device **pbd, unsigned int count);
void bdm_RegisterCallback(bdm_cb cb);
void bdm_on_device_ready(int event);

#define bdm_IMPORTS_start DECLARE_IMPORT_TABLE(bdm, 1, 1)
#define bdm_IMPORTS_end END_IMPORT_TABLE
Expand All @@ -73,5 +82,6 @@ void bdm_RegisterCallback(bdm_cb cb);
#define I_bdm_disconnect_fs DECLARE_IMPORT(7, bdm_disconnect_fs)
#define I_bdm_get_bd DECLARE_IMPORT(8, bdm_get_bd)
#define I_bdm_RegisterCallback DECLARE_IMPORT(9, bdm_RegisterCallback)
#define I_bdm_on_device_ready DECLARE_IMPORT(10, bdm_on_device_ready)

#endif
10 changes: 5 additions & 5 deletions iop/fs/bdm/src/bdm.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ static bdm_cb g_cb = NULL;
static int bdm_event = -1;
static int bdm_thread_id = -1;

/* Event flag bits */
#define BDM_EVENT_CB_MOUNT 0x01
#define BDM_EVENT_CB_UMOUNT 0x02
#define BDM_EVENT_MOUNT 0x04

void bdm_RegisterCallback(bdm_cb cb)
{
int i;
Expand All @@ -47,6 +42,11 @@ void bdm_RegisterCallback(bdm_cb cb)
}
}

void bdm_on_device_ready(int event)
{
// do something with your device.. unlock io etc
}

void bdm_connect_bd(struct block_device *bd)
{
int i;
Expand Down
1 change: 1 addition & 0 deletions iop/fs/bdm/src/exports.tab
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ DECLARE_EXPORT_TABLE(bdm, 1, 1)
DECLARE_EXPORT(bdm_disconnect_fs)
DECLARE_EXPORT(bdm_get_bd)
DECLARE_EXPORT(bdm_RegisterCallback)
DECLARE_EXPORT(bdm_on_device_ready)
END_EXPORT_TABLE

void _retonly() {}
1 change: 1 addition & 0 deletions iop/usb/usbmass_bd/src/include/scsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ struct scsi_interface
int scsi_init(void);
void scsi_connect(struct scsi_interface *scsi);
void scsi_disconnect(struct scsi_interface *scsi);
void scsi_set_ready(struct scsi_interface *scsi);

#endif
18 changes: 18 additions & 0 deletions iop/usb/usbmass_bd/src/scsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,22 @@ void scsi_disconnect(struct scsi_interface *scsi)
}
}

void scsi_set_ready(struct scsi_interface *scsi)
{
int i;
M_DEBUG("%s\n", __func__);
for (i = 0; i < NUM_DEVICES; ++i) {
if (g_scsi_bd[i].priv == scsi) {
M_DEBUG("Device %s%d is now READY\n", g_scsi_bd[i].name, g_scsi_bd[i].devNr);
if (g_scsi_bd[i].device_ready_callback) {
g_scsi_bd[i].device_ready_callback(BDM_EVENT_DEVICE_READY);
M_DEBUG("Device is ready, invoking device ready callback\n");
}
break;
}
}
}

int scsi_init(void)
{
int i;
Expand All @@ -339,6 +355,8 @@ int scsi_init(void)
g_scsi_bd[i].write = scsi_write;
g_scsi_bd[i].flush = scsi_flush;
g_scsi_bd[i].stop = scsi_stop;

g_scsi_bd[i].device_ready_callback = NULL;
}

return 0;
Expand Down
47 changes: 46 additions & 1 deletion iop/usb/usbmass_bd/src/usb_mass.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <usbhdfsd-common.h>

// #define ASYNC
// #define DEBUG //comment out this line when not debugging
#define DEBUG //comment out this line when not debugging
#include "module_debug.h"

#define USB_SUBCLASS_MASS_RBC 0x01
Expand Down Expand Up @@ -613,6 +613,8 @@ static int usb_mass_probe(int devId)
return 1;
}

static int devices_initialising = 0;

static int usb_mass_connect(int devId)
{
int i;
Expand All @@ -638,6 +640,8 @@ static int usb_mass_connect(int devId)
return 1;
}

devices_initialising++;

dev->status = 0;
dev->bulkEpI = -1;
dev->bulkEpO = -1;
Expand Down Expand Up @@ -676,6 +680,7 @@ static int usb_mass_connect(int devId)
SemaData.option = 0;
SemaData.attr = 0;
if ((dev->ioSema = CreateSema(&SemaData)) < 0) {
usb_mass_release(dev);
M_PRINTF("ERROR: Failed to allocate I/O semaphore\n");
return -1;
}
Expand Down Expand Up @@ -705,6 +710,9 @@ static void usb_mass_release(mass_dev *dev)
dev->bulkEpO = -1;
dev->controlEp = -1;
dev->status = 0;

if (devices_initialising > 0) // should handle all errors?
devices_initialising--;
}

static int usb_mass_disconnect(int devId)
Expand Down Expand Up @@ -790,6 +798,43 @@ static void usb_mass_update(void *arg)
// This is the same wait amount as done in fat_getData in usbhdfsd.
// This is a workaround to avoid incorrect initialization when attaching multiple drives at the same time.
DelayThread(5000);
devices_initialising--;
}
}

// Determine how many devices are connected
int total_devs = 0;
for (i = 0; i < NUM_DEVICES; i++) {
mass_dev *dev = &g_mass_device[i];
if (dev->devId != -1 && (dev->status & USBMASS_DEV_STAT_CONN) && (dev->status & USBMASS_DEV_STAT_CONF))
total_devs++;
}

// Allow time for more devices to start initialising if there is a possibility of more devices
if (total_devs != NUM_DEVICES) {
int wait_cycles = 0;
while (wait_cycles < 5) {
// If another device is initialising.. break and wait for it
if (devices_initialising > 0) {
M_DEBUG("Detected another device initialising. Breaking wait.\n");
break;
}

wait_cycles++;
DelayThread(1000 * 1000);
M_DEBUG("Waiting... devices_initialising: %d\n", devices_initialising);
}
}

// Now devices are ready.. mark them as ready and perform the final actions
if (devices_initialising == 0) {
for (i = 0; i < NUM_DEVICES; i++) {
mass_dev *dev = &g_mass_device[i];
if (dev->devId != -1 && (dev->status & USBMASS_DEV_STAT_CONF) && !(dev->status & USBMASS_DEV_STAT_READY)) {
dev->status |= USBMASS_DEV_STAT_READY;
scsi_set_ready(&dev->scsi);
M_PRINTF("Device %d is now ready\n", dev->devId);
}
}
}
}
Expand Down

0 comments on commit 5974ac7

Please sign in to comment.