-
Notifications
You must be signed in to change notification settings - Fork 9
/
DeviceSupport.c
203 lines (168 loc) · 5 KB
/
DeviceSupport.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <errno.h>
#include <kernel.h>
#include <libmc.h>
#include <malloc.h>
#include <string.h>
#include <stdio.h>
#include <usbhdfsd-common.h>
#include "sysman/sysinfo.h"
#include "main.h"
#include "ident.h"
#include "system.h"
#include "DeviceSupport.h"
static int DevicePollingThreadID = -1, MainThreadID = -1;
static volatile int DevicePollingThreadCommand;
static void *DevicePollingThreadStack = NULL;
int UsbReadyStatus = 0;
enum DEVICE_POLLING_THREAD_COMMANDS
{
DEVICE_POLLING_THREAD_CMD_NONE = 0,
DEVICE_POLLING_THREAD_CMD_STOP
};
static int McUnitStatus[2];
static int MassUnitStatus[1];
static int IsMcUnitReady(int unit)
{
int type, free, format, result;
type = MC_TYPE_NONE;
if (mcGetInfo(unit, 0, &type, &free, &format) == 0)
{
mcSync(0, NULL, &result);
result = (type == MC_TYPE_PS2) ? 1 : 0;
}
else
result = 0;
return result;
}
static int IsMassUnitReady(int unit)
{
return UsbReadyStatus;
}
int GetHasDeviceSufficientSpace(const char *device, int unit, const struct RequiredFileSpaceStat *RequiredSpaceStats, unsigned int NumFileEntries)
{
int result;
if (!strcmp(device, "mc"))
{
result = HasMcUnitSufficientSpace(unit, RequiredSpaceStats, NumFileEntries);
}
else if (!strcmp(device, "mass"))
{
result = HasMassUnitSufficientSpace(unit, RequiredSpaceStats, NumFileEntries);
}
else if (!strcmp(device, "host"))
{
result = HasHostUnitSufficientSpace(unit, RequiredSpaceStats, NumFileEntries);
}
else
result = -ENODEV;
return result;
}
int HasMcUnitSufficientSpace(int unit, const struct RequiredFileSpaceStat *RequiredSpaceStats, unsigned int NumFileEntries)
{
unsigned int TotalRequiredSpace, AvailableSpace, NumFiles, NumDirectories, i;
int type, free, format, result;
type = MC_TYPE_NONE;
if (mcGetInfo(unit, 0, &type, &free, &format) == 0)
{
mcSync(0, NULL, &result);
if (type == MC_TYPE_PS2)
{
NumFiles = NumDirectories = 0;
TotalRequiredSpace = 0;
for (i = 0; i < NumFileEntries; i++)
{
if (RequiredSpaceStats[i].IsFile)
{
NumFiles++;
TotalRequiredSpace += RequiredSpaceStats[i].length;
}
else
NumDirectories++;
}
/* I do not like this, but the Sony documentation implies that we can (And have to?) assume that the memory card has a cluster size of 2. */
AvailableSpace = free * 1024;
TotalRequiredSpace += (NumFiles + NumDirectories + 1) / 2;
TotalRequiredSpace += NumDirectories * 2;
result = AvailableSpace >= TotalRequiredSpace;
}
else
result = -ENXIO;
}
else
result = 0;
return result;
}
int HasMassUnitSufficientSpace(int unit, const struct RequiredFileSpaceStat *RequiredSpaceStats, unsigned int NumFileEntries)
{
return 1;
}
int HasHostUnitSufficientSpace(int unit, const struct RequiredFileSpaceStat *RequiredSpaceStats, unsigned int NumFileEntries)
{
return 1;
}
static void DevicePollingThread(void)
{
int done;
done = 0;
while (!done)
{
//Process commands.
if (DevicePollingThreadCommand != DEVICE_POLLING_THREAD_CMD_NONE)
{
if (DevicePollingThreadCommand == DEVICE_POLLING_THREAD_CMD_STOP)
{
WakeupThread(MainThreadID);
done = 1;
continue;
}
DevicePollingThreadCommand = DEVICE_POLLING_THREAD_CMD_NONE;
}
//Update the status of all units of all devices.
MassUnitStatus[0] = IsMassUnitReady(0);
McUnitStatus[0] = IsMcUnitReady(0);
McUnitStatus[1] = IsMcUnitReady(1);
}
}
int StartDevicePollingThread(void)
{
DevicePollingThreadCommand = DEVICE_POLLING_THREAD_CMD_NONE;
MainThreadID = GetThreadId();
DevicePollingThreadStack = memalign(64, 0x800);
return (DevicePollingThreadID = SysCreateThread(&DevicePollingThread, DevicePollingThreadStack, 0x800, NULL, 0x78));
}
int StopDevicePollingThread(void)
{
DevicePollingThreadCommand = DEVICE_POLLING_THREAD_CMD_STOP;
SleepThread(); //Wait for acknowledgement.
if (DevicePollingThreadID >= 0)
{
TerminateThread(DevicePollingThreadID);
DeleteThread(DevicePollingThreadID);
DevicePollingThreadID = -1;
}
if (DevicePollingThreadStack != NULL)
{
free(DevicePollingThreadStack);
DevicePollingThreadStack = NULL;
}
return 0;
}
int GetIsDeviceUnitReady(const char *device, int unit)
{
int result;
if (strcmp(device, "mc") == 0)
{
result = McUnitStatus[unit];
}
else if (strcmp(device, "mass") == 0)
{
result = MassUnitStatus[unit];
}
else if (strcmp(device, "host") == 0)
{
result = 1;
}
else
result = -ENODEV;
return result;
}