-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathio.c
executable file
·364 lines (304 loc) · 10.3 KB
/
io.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <errno.h>
#include <kernel.h>
#include <tamtypes.h>
#include <string.h>
#include <malloc.h>
#include <fileXio_rpc.h>
#include <ps2lib_err.h>
#include "io.h"
enum IO_THREAD_CMD {
IO_THREAD_CMD_NONE = 0,
IO_THREAD_CMD_WRITE,
IO_THREAD_CMD_WRITE_END, // Finish writing outstanding data and terminate.
IO_THREAD_CMD_READ,
IO_THREAD_CMD_READ_END
};
struct IOState
{
void *buffer;
struct BuffDesc *bd;
unsigned short int WritePtr, ReadPtr;
unsigned short int bufmax, nbufs;
unsigned char state, command;
unsigned int remaining;
int id, CmdAckSema, inBufSema, outBufSema, ioFD;
};
static struct IOState IOState;
static u8 IOThreadStack[0x600] __attribute__((aligned(16)));
static int IOExecWriteNext(void)
{
void *buffer;
const struct BuffDesc *bd, *bdNext;
unsigned short int nextReadPtr;
int result, toWrite, partitionsCleared, i;
result = 0;
if (PollSema(IOState.outBufSema) == IOState.outBufSema) {
buffer = (u8 *)IOState.buffer + IOState.ReadPtr * (unsigned int)IOState.bufmax * 2048;
bd = &IOState.bd[IOState.ReadPtr];
toWrite = bd->length;
// Determine how much can be written at once, without wrapping around the ring buffer.
for (partitionsCleared = 1, nextReadPtr = IOState.ReadPtr + 1;
nextReadPtr < IOState.nbufs;
partitionsCleared++, nextReadPtr++) {
if (PollSema(IOState.outBufSema) != IOState.outBufSema)
break;
bdNext = &IOState.bd[nextReadPtr];
toWrite += bdNext->length;
}
do {
result = fileXioWrite(IOState.ioFD, buffer, toWrite);
} while (result < 0 && result != -EIO);
if (result >= 0) { // Update state
IOState.ReadPtr = (IOState.ReadPtr + partitionsCleared) % IOState.nbufs;
for (i = 0; i < partitionsCleared; i++)
SignalSema(IOState.inBufSema);
}
}
return result;
}
static int IOExecReadNext(void)
{
void *buffer;
struct BuffDesc *bd, *bdNext;
unsigned short int sectors, nextWritePtr;
unsigned int sectorsTotal;
int result, partitionsCleared, i;
result = 0;
while (IOState.remaining > 0) {
if (PollSema(IOState.inBufSema) == IOState.inBufSema) {
sectors = IOState.remaining < IOState.bufmax ? IOState.remaining : IOState.bufmax;
sectorsTotal = sectors;
buffer = (u8 *)IOState.buffer + IOState.WritePtr * (unsigned int)IOState.bufmax * 2048;
bd = &IOState.bd[IOState.WritePtr];
bd->length = sectors * 2048; // Indicate how many bytes will be in this partition.
partitionsCleared = 1;
// Determine how much can be written at once, without wrapping around the ring buffer.
for (partitionsCleared = 1, nextWritePtr = IOState.WritePtr + 1;
(nextWritePtr < IOState.nbufs) && (IOState.remaining - sectorsTotal > 0);
partitionsCleared++, nextWritePtr++) {
if (PollSema(IOState.inBufSema) != IOState.inBufSema)
break;
bd = &IOState.bd[nextWritePtr];
sectors = IOState.remaining - sectorsTotal < IOState.bufmax ? IOState.remaining - sectorsTotal : IOState.bufmax;
bd->length = sectors * 2048; // Indicate how many bytes will be in this partition.
sectorsTotal += sectors;
}
// Read raw data
do {
result = fileXioRead(IOState.ioFD, buffer, sectorsTotal * 2048);
} while (result < 0 && result != -EIO);
if (result >= 0) {
// Update state
IOState.WritePtr = (IOState.WritePtr + partitionsCleared) % IOState.nbufs;
for (i = 0; i < partitionsCleared; i++)
SignalSema(IOState.outBufSema);
IOState.remaining -= sectorsTotal;
}
} else
break;
}
return result;
}
static void IOThread(void *arg)
{
int result;
while (1) {
SleepThread();
switch (IOState.command) {
case IO_THREAD_CMD_WRITE:
if (IOExecWriteNext() < 0) {
IOState.state = IO_THREAD_STATE_ERROR;
ExitDeleteThread();
}
break;
case IO_THREAD_CMD_WRITE_END:
// Finish writing all data
while ((result = IOExecWriteNext()) > 0) {};
if (result < 0)
IOState.state = IO_THREAD_STATE_ERROR;
SignalSema(IOState.CmdAckSema);
ExitDeleteThread();
break;
case IO_THREAD_CMD_READ:
if (IOExecReadNext() < 0) {
IOState.state = IO_THREAD_STATE_ERROR;
ExitDeleteThread();
}
break;
case IO_THREAD_CMD_READ_END:
SignalSema(IOState.CmdAckSema);
ExitDeleteThread();
break;
}
}
}
static void IOExec(unsigned char cmd)
{
IOState.command = cmd;
WakeupThread(IOState.id);
}
static void IOExecWait(unsigned char cmd)
{
IOState.command = cmd;
WakeupThread(IOState.id);
WaitSema(IOState.CmdAckSema);
}
static int IOInitCommon(int fd, struct BuffDesc *bd, void *buffers, unsigned short int bufmax, unsigned short int nbufs, int prio)
{
ee_sema_t sema;
int result;
sema.init_count = 0;
sema.max_count = 1;
sema.attr = 0;
sema.option = (u32) "io-CmdAck";
if ((IOState.CmdAckSema = CreateSema(&sema)) >= 0) {
sema.init_count = nbufs;
sema.max_count = nbufs;
sema.attr = 0;
sema.option = (u32) "io-inBufSema";
if ((IOState.inBufSema = CreateSema(&sema)) >= 0) {
sema.init_count = 0;
sema.max_count = nbufs;
sema.attr = 0;
sema.option = (u32) "io-outBufSema";
if ((IOState.outBufSema = CreateSema(&sema)) >= 0) {
IOState.command = IO_THREAD_CMD_NONE;
IOState.state = IO_THREAD_STATE_OK;
IOState.bd = bd;
IOState.buffer = buffers;
IOState.bufmax = bufmax;
IOState.nbufs = nbufs;
IOState.WritePtr = 0;
IOState.ReadPtr = 0;
IOState.ioFD = fd;
IOState.id = SysCreateThread(&IOThread, IOThreadStack, sizeof(IOThreadStack), &IOState, prio);
result = IOState.id;
if (IOState.id < 0) {
DeleteSema(IOState.CmdAckSema);
DeleteSema(IOState.inBufSema);
}
} else {
DeleteSema(IOState.inBufSema);
DeleteSema(IOState.CmdAckSema);
result = IOState.outBufSema;
}
} else {
DeleteSema(IOState.CmdAckSema);
result = IOState.inBufSema;
}
} else
result = IOState.CmdAckSema;
return result;
}
int IOWriteInit(int fd, struct BuffDesc *bd, void *buffers, unsigned short int bufmax, unsigned short int nbufs, int prio)
{
return IOInitCommon(fd, bd, buffers, bufmax, nbufs, prio);
}
int IOReadInit(int fd, struct BuffDesc *bd, void *buffers, unsigned short int bufmax, unsigned short int nbufs, unsigned int remaining, int prio)
{
int result;
IOState.remaining = remaining;
if ((result = IOInitCommon(fd, bd, buffers, bufmax, nbufs, prio)) >= 0)
IOExec(IO_THREAD_CMD_READ);
return result;
}
// Note: a call to IOSignalWriteDone() must follow a call to IOGetNextWrBuffer()!
void *IOGetNextWrBuffer(void)
{
WaitSema(IOState.inBufSema);
return ((u8 *)IOState.buffer + IOState.bufmax * (unsigned int)IOState.WritePtr * 2048);
}
void IOSignalWriteDone(int length)
{
struct BuffDesc *bd;
bd = &IOState.bd[IOState.WritePtr];
bd->length = length;
IOState.WritePtr = (IOState.WritePtr + 1) % IOState.nbufs;
SignalSema(IOState.outBufSema);
IOExec(IO_THREAD_CMD_WRITE);
}
int IORead(void *buffer)
{
struct BuffDesc *bd;
const void *pReadData;
int length;
WaitSema(IOState.outBufSema);
bd = &IOState.bd[IOState.ReadPtr];
pReadData = (const void *)((u8 *)IOState.buffer + IOState.ReadPtr * (unsigned int)IOState.bufmax * 2048);
IOState.ReadPtr = (IOState.ReadPtr + 1) % IOState.nbufs;
// Copy out the data read
memcpy(buffer, pReadData, bd->length);
length = (int)bd->length;
SignalSema(IOState.inBufSema);
// Continue reading
IOExec(IO_THREAD_CMD_READ);
return length;
}
int IOReadNext(const void **buffer)
{
struct BuffDesc *bd;
int length;
WaitSema(IOState.outBufSema);
bd = &IOState.bd[IOState.ReadPtr];
*buffer = (const void *)((u8 *)IOState.buffer + IOState.ReadPtr * (unsigned int)IOState.bufmax * 2048);
IOState.ReadPtr = (IOState.ReadPtr + 1) % IOState.nbufs;
length = (int)bd->length;
return length;
}
void IOReadAdvance(void)
{
SignalSema(IOState.inBufSema);
// Continue reading
IOExec(IO_THREAD_CMD_READ);
}
static void IOEnd(void)
{
DeleteSema(IOState.CmdAckSema);
DeleteSema(IOState.inBufSema);
DeleteSema(IOState.outBufSema);
}
int IOEndWrite(void)
{
IOExecWait(IO_THREAD_CMD_WRITE_END);
IOEnd();
return IOState.state;
}
int IOEndRead(void)
{
IOExecWait(IO_THREAD_CMD_READ_END);
IOEnd();
return IOState.state;
}
int IOGetStatus(void)
{
return IOState.state;
}
int IOAlloc(struct BuffDesc **bdOut, void **ioBufferOut)
{
struct BuffDesc *bd;
void *ioBuffer;
int result;
if ((bd = malloc(sizeof(struct BuffDesc) * IO_BANKMAX)) != NULL) {
if ((ioBuffer = memalign(64, IO_BANKMAX * IO_BANKSIZE * 2048)) != NULL) {
*bdOut = bd;
*ioBufferOut = ioBuffer;
result = 0;
} else {
free(bd);
result = -ENOMEM;
}
} else
result = -ENOMEM;
return result;
}
void IOFree(struct BuffDesc **bdOut, void **ioBufferOut)
{
if (*bdOut != NULL) {
free(*bdOut);
*bdOut = NULL;
}
if (*ioBufferOut != NULL) {
free(*ioBufferOut);
*ioBufferOut = NULL;
}
}