9#include "module_debug.h"
18#define MAX_CONNECTIONS 20
19static struct bdm_mounts g_mount[MAX_CONNECTIONS];
21static bdm_cb g_cb = NULL;
22static int bdm_event = -1;
23static int bdm_thread_id = -1;
26#define BDM_EVENT_CB_MOUNT 0x01
27#define BDM_EVENT_CB_UMOUNT 0x02
28#define BDM_EVENT_MOUNT 0x04
30void bdm_RegisterCallback(bdm_cb cb)
34 M_DEBUG(
"%s\n", __func__);
42 for (i = 0; i < MAX_CONNECTIONS; ++i) {
43 if ((g_mount[i].bd != NULL) && (g_mount[i].fs != NULL)) {
44 SetEventFlag(bdm_event, BDM_EVENT_CB_MOUNT);
54 M_PRINTF(
"connecting device %s%dp%d size=%luMB id=0x%x\n", bd->name, bd->devNr, bd->parNr,
55 (u32)(bd->sectorCount / ((1000 * 1000) / bd->sectorSize)), bd->parId);
57 for (i = 0; i < MAX_CONNECTIONS; ++i) {
58 if (g_mount[i].bd == NULL) {
61 g_mount[i].cbd = (bd->parNr == 0) ? bd_cache_create(bd) : NULL;
63 SetEventFlag(bdm_event, BDM_EVENT_MOUNT);
73 M_PRINTF(
"disconnecting device %s%dp%d\n", bd->name, bd->devNr, bd->parNr);
75 for (i = 0; i < MAX_CONNECTIONS; ++i) {
76 if (g_mount[i].bd == bd) {
77 if (g_mount[i].fs != NULL) {
79 g_mount[i].fs->disconnect_bd(g_mount[i].cbd != NULL ? g_mount[i].cbd : g_mount[i].bd);
80 M_PRINTF(
"%s%dp%d unmounted from %s\n", bd->name, bd->devNr, bd->parNr, g_mount[i].fs->name);
84 if (g_mount[i].cbd != NULL) {
85 bd_cache_destroy(g_mount[i].cbd);
86 g_mount[i].cbd = NULL;
92 SetEventFlag(bdm_event, BDM_EVENT_CB_UMOUNT);
101 M_PRINTF(
"connecting fs %s\n", fs->name);
103 for (i = 0; i < MAX_CONNECTIONS; ++i) {
104 if (g_fs[i] == NULL) {
111 SetEventFlag(bdm_event, BDM_EVENT_MOUNT);
118 M_PRINTF(
"disconnecting fs %s\n", fs->name);
121 for (i = 0; i < MAX_CONNECTIONS; ++i) {
122 if (g_mount[i].fs == fs) {
123 g_mount[i].fs = NULL;
125 SetEventFlag(bdm_event, BDM_EVENT_CB_UMOUNT);
130 for (i = 0; i < MAX_CONNECTIONS; ++i) {
142 M_DEBUG(
"%s\n", __func__);
145 for (i = 0; (
unsigned int)i <
count && i < MAX_CONNECTIONS; i++)
146 pbd[i] = g_mount[i].bd;
149static void bdm_try_mount(
struct bdm_mounts *mount)
153 M_DEBUG(
"%s(%s%dp%d)\n", __func__, mount->bd->name, mount->bd->devNr, mount->bd->parNr);
155 for (i = 0; i < MAX_CONNECTIONS; ++i) {
156 if (g_fs[i] != NULL) {
157 if (g_fs[i]->connect_bd(mount->cbd != NULL ? mount->cbd : mount->bd) == 0) {
158 M_PRINTF(
"%s%dp%d mounted to %s\n", mount->bd->name, mount->bd->devNr, mount->bd->parNr, g_fs[i]->name);
161 SetEventFlag(bdm_event, BDM_EVENT_CB_MOUNT);
168static void bdm_thread(
void *arg)
175 M_PRINTF(
"BDM event thread running\n");
178 WaitEventFlag(bdm_event, BDM_EVENT_CB_MOUNT | BDM_EVENT_CB_UMOUNT | BDM_EVENT_MOUNT, WEF_OR | WEF_CLEAR, &EFBits);
180 if (EFBits & BDM_EVENT_MOUNT) {
182 for (i = 0; i < MAX_CONNECTIONS; ++i) {
183 if ((g_mount[i].bd != NULL) && (g_mount[i].fs == NULL))
184 bdm_try_mount(&g_mount[i]);
188 if (EFBits & BDM_EVENT_CB_MOUNT) {
194 if (EFBits & BDM_EVENT_CB_UMOUNT) {
206 iop_thread_t ThreadData;
208 M_DEBUG(
"%s\n", __func__);
210 for (i = 0; i < MAX_CONNECTIONS; ++i) {
211 g_mount[i].bd = NULL;
212 g_mount[i].cbd = NULL;
213 g_mount[i].fs = NULL;
217 EventFlagData.attr = 0;
218 EventFlagData.option = 0;
219 EventFlagData.bits = 0;
220 result = bdm_event = CreateEventFlag(&EventFlagData);
222 M_DEBUG(
"ERROR: CreateEventFlag %d\n", result);
226 ThreadData.attr = TH_C;
227 ThreadData.thread = bdm_thread;
228 ThreadData.option = 0;
229 ThreadData.priority = 0x30;
230 ThreadData.stacksize = 0x1000;
231 result = bdm_thread_id = CreateThread(&ThreadData);
233 M_DEBUG(
"ERROR: CreateThread %d\n", result);
234 DeleteEventFlag(bdm_event);
238 result = StartThread(bdm_thread_id, NULL);
240 M_DEBUG(
"ERROR: StartThread %d\n", result);
241 DeleteThread(bdm_thread_id);
242 DeleteEventFlag(bdm_event);
u32 count
start sector of fragmented bd/file