bbcinit.c
10.9 KB
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "../bbclocal.h"
bbc_hand *handles[BBC_MAX_DEVS];
BBCMuxParam muxParam;
HANDLE hMuxThread = NULL;
static void __bbc_kill_mux(bbc_hand *hp);
extern int __BBC_SetCardSeqno(bbc_hand *hp);
static int
find_handle()
{
int i;
for (i = 0; i < BBC_MAX_DEVS; i++)
if (handles[i] == NULL)
return i;
return -1;
}
int
next_seqno()
{
static int seqno = 0;
if (++seqno <= 0)
seqno = 1;
return seqno;
}
extern DWORD WINAPI domux_win32(LPVOID);
struct {
int (*init)(bbc_hand *);
} initfunc[] = {
{ NULL /*__bbc_new_reader*/ }, //TODO: support reader interface
{ __bbc_new_direct },
};
BBCHandle
BBCInit(int callType)
{
unsigned int dwMuxId;
int h, type, rv;
int inpipe[2];
int outpipe[2];
bbc_hand *hp;
BBC_LOG(MSG_DIAG, "BBCInit %d\n", callType);
type = BBC_HT_DIRECT; //TODO: detect interface type
/*
* Allocate handle and call the appropriate init routine
*/
if ((h = find_handle()) < 0) {
BBC_LOG(MSG_ERR, "BBCInit: out of handles");
return BBC_TOOMANY;
}
if ((hp = malloc(sizeof (bbc_hand))) == NULL) {
BBC_LOG_SYSERROR("BBCInit: malloc failed");
return BBC_NOMEM;
}
handles[h] = hp;
memset(hp, 0, sizeof (bbc_hand));
hp->bh_type = type;
rv = (initfunc[type].init)(hp);
if (rv != BBC_OK) {
goto err1;
}
/*
* Allocate buffer for FAT blocks
*/
if ((hp->bh_fat = malloc(BB_FL_MAX_FATS*sizeof(BbFat16))) == NULL) {
BBC_LOG_SYSERROR("BBCInit: malloc failed");
rv = BBC_NOMEM;
goto err1;
}
memset(hp->bh_fat, 0, BB_FL_MAX_FATS*sizeof(BbFat16));
if (hp->bh_type != BBC_HT_DIRECT) {
if (!hp->bh_card_present) {
hp->bh_fat_valid = 0;
return h;
}
if (hp->bh_cardsize != 4096 && hp->bh_cardsize != 8192)
BBC_LOG(MSG_WARNING, "BBCInit: invalid card size %d blks\n", hp->bh_cardsize);
/* read fat into allocated buffer */
if ((rv = __bbc_read_fat(hp)) != BBC_OK && rv != BBC_NOFORMAT)
goto err2;
/* Delete the canonically named temp file used in create/atomic rename */
(void) __BBC_FDelete(hp, TEMPFILE);
return h;
}
if (_pipe(inpipe, MAX_PIPE_DATA, O_BINARY) < 0) {
BBC_LOG_PERROR("BBCInit: pipe failed");
rv = BBC_ERROR;
goto err2;
}
if (_pipe(outpipe, MAX_PIPE_DATA, O_BINARY) < 0) {
BBC_LOG_PERROR("BBCInit: pipe failed");
rv = BBC_ERROR;
goto err3;
}
// Create events for thread management
rv = BBC_ERROR;
InitializeCriticalSection( &CriticalSection );
hMuxReadyEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hMuxReadyEvent == NULL) {
BBC_LOG_SYSERROR("BBCInit: Error creating MuxReadyEvent");
goto err4;
}
hDeviceReadyEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hDeviceReadyEvent == NULL) {
BBC_LOG_SYSERROR("BBCInit: Error creating DeviceReadyEvent");
goto err4;
}
hDeviceWriteDoneEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
if (hDeviceWriteDoneEvent == NULL) {
BBC_LOG_SYSERROR("BBCInit: Error creating DeviceWriteDoneEvent");
goto err4;
}
hDeviceErrorEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hDeviceErrorEvent == NULL) {
BBC_LOG_SYSERROR("BBCInit: Error creating DeviceErrorEvent");
goto err4;
}
hDeviceDataSendEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hDeviceDataSendEvent == NULL) {
BBC_LOG_SYSERROR("BBCInit: Error creating DeviceDataSendEvent");
goto err4;
}
hBBCCloseEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hBBCCloseEvent == NULL) {
BBC_LOG_SYSERROR("BBCInit: Error creating BBCCloseEvent");
goto err4;
}
// Make sure both read and write pipes are reset
if (do_usb_reset(hp->bh_urh) == FALSE)
goto err4;
if (do_usb_reset(hp->bh_uwh) == FALSE)
goto err4;
// Start Mux thread
muxParam.devname = hp->bh_readname;
muxParam.rh = hp->bh_urh;
muxParam.wh = hp->bh_uwh;
muxParam.ifd = outpipe[0];
muxParam.ofd = inpipe[1];
hMuxThread = (HANDLE) _beginthreadex(NULL, 0, &domux_win32,
&muxParam, 0, &dwMuxId);
if (hMuxThread == NULL) {
BBC_LOG_SYSERROR("BBCInit: Error starting mux thread");
rv = BBC_ERROR;
goto err4;
}
else {
// Wait for Mux thread to be ready (or for the thread to terminate)
DWORD dwWaitResult;
HANDLE hDeviceEvents[2];
hDeviceEvents[0] = hMuxThread;
hDeviceEvents[1] = hMuxReadyEvent;
dwWaitResult = myWaitForMultipleObjects(
2, /* number of handles in array */
hDeviceEvents, /* array of device-event handles */
FALSE, /* wait until only one is signaled */
WAIT_DEVICE_READY_TIMEOUT); /* 2 second wait */
if (dwWaitResult == WAIT_OBJECT_0 + 1) {
/* Mux is ready! Can go foward with our life now. */
}
else {
/* Hmm, what's wrong with mux? */
BBC_LOG(MSG_ERR, "BBCInit: Error waiting for mux to be ready");
rv = BBC_ERROR;
goto err4;
}
}
// Start USB read thread
if (bb_usb_read_start(&(hp->bh_urh)) < 0) {
BBC_LOG_SYSERROR("BBCInit: Error starting USB thread");
goto err4;
}
// Assign pipes to hp
hp->bh_pifd = inpipe[0];
hp->bh_pofd = outpipe[1];
BBC_LOG(MSG_DIAG, "BBCInit: Ready to communicate with player\n");
/*
* Check for the presence of a card and init sequence number
*/
if ((rv = __BBC_SetCardSeqno(hp)) < 0) {
BBC_LOG(MSG_DEBUG, "BBCInit: SetSeqno ret %d\n", rv);
/*
* It is ok for this to fail if no card is present or
* the card has not yet been formatted
*/
if (rv == BBC_NOCARD || rv == BBC_NOFORMAT) {
return h;
}
goto err4;
}
/* Delete the canonically named temp file used in create/atomic rename */
rv = __BBC_FDelete(hp, TEMPFILE);
if (BBC_IO_ERR(rv)) {
/* Abort if it was an IO error */
goto err4;
}
BBC_LOG(MSG_DIAG, "BBCInit successful\n");
return h;
err4:
__bbc_kill_mux(hp);
close(outpipe[0]);
close(outpipe[1]);
err3:
close(inpipe[0]);
close(inpipe[1]);
err2:
free(hp->bh_fat);
hp->bh_fat = NULL;
err1:
if (hp->bh_flashif != NULL)
(void) free(hp->bh_flashif);
free(hp);
handles[h] = NULL;
BBC_LOG(MSG_DIAG, "BBCInit error %d\n", rv);
return rv;
}
static void
__bbc_kill_mux(bbc_hand *hp)
{
// Tells our threads that we are closing
if (hBBCCloseEvent)
SetEvent(hBBCCloseEvent);
BBC_LOG(MSG_DEBUG, "usb read end\n");
bb_usb_read_end();
if (hMuxThread) {
BBC_LOG(MSG_INFO, "Wait for mux thread to terminate nicely\n");
if (myWaitForSingleObject( hMuxThread, WAIT_THREAD_TERM_TIMEOUT ) != WAIT_OBJECT_0) {
BBC_LOG(MSG_INFO, "Terminating mux thread\n");
if (TerminateThread(hMuxThread, -1) == FALSE) {
BBC_LOG_SYSERROR("Error terminating mux thread");
}
}
else {
BBC_LOG(MSG_INFO, "Mux thread terminated\n");
}
if (CloseHandle(hMuxThread) == FALSE) {
BBC_LOG_SYSERROR("Error closing handle to mux thread");
}
hMuxThread = NULL;
}
BBC_LOG(MSG_DEBUG, "Delete CriticalSection\n");
DeleteCriticalSection( &CriticalSection );
// Close event handles
if (hMuxReadyEvent) {
BBC_LOG(MSG_DEBUG, "Close hMuxReadyEvent\n");
if (CloseHandle(hMuxReadyEvent) == FALSE) {
BBC_LOG_SYSERROR("Error closing handle to MuxReadyEvent");
}
hMuxReadyEvent = NULL;
}
if (hDeviceReadyEvent) {
BBC_LOG(MSG_DEBUG, "Close hDeviceReadyEvent\n");
if (CloseHandle(hDeviceReadyEvent) == FALSE) {
BBC_LOG_SYSERROR("Error closing handle to DeviceReadyEvent");
}
hDeviceReadyEvent = NULL;
}
if (hDeviceWriteDoneEvent) {
BBC_LOG(MSG_DEBUG, "Close hDeviceWriteDoneEvent\n");
if (CloseHandle(hDeviceWriteDoneEvent) == FALSE) {
BBC_LOG_SYSERROR("Error closing handle to DeviceWriteDoneEvent");
}
hDeviceWriteDoneEvent = NULL;
}
if (hDeviceErrorEvent) {
BBC_LOG(MSG_DEBUG, "Close hDeviceErrorEvent\n");
if (CloseHandle(hDeviceErrorEvent) == FALSE) {
BBC_LOG_SYSERROR("Error closing handle to DeviceErrorEvent");
}
hDeviceErrorEvent = NULL;
}
if (hDeviceDataSendEvent) {
BBC_LOG(MSG_DEBUG, "Close hDeviceDataSendEvent\n");
if (CloseHandle(hDeviceDataSendEvent) == FALSE) {
BBC_LOG_SYSERROR("Error closing handle to DeviceDataSendEvent");
}
hDeviceDataSendEvent = NULL;
}
if (hBBCCloseEvent) {
BBC_LOG(MSG_DEBUG, "Close hBBCCloseEvent\n");
if (CloseHandle(hBBCCloseEvent) == FALSE) {
BBC_LOG_SYSERROR("Error closing handle to hBBCCloseEvent");
}
hBBCCloseEvent = NULL;
}
// Close USB handles
// Also send reset to BBPlayer to abort whatever
// it is doing and turn off the LEDs
if (hp->bh_uwh) {
BBC_LOG(MSG_DEBUG, "Close uwh\n");
do_usb_reset(hp->bh_uwh);
if (CloseHandle(hp->bh_uwh) == FALSE) {
BBC_LOG_SYSERROR("Error closing handle to USB Write");
}
hp->bh_uwh = NULL;
}
if (hp->bh_urh) {
BBC_LOG(MSG_DEBUG, "Close urh\n");
do_usb_reset(hp->bh_urh);
if (CloseHandle(hp->bh_urh) == FALSE) {
BBC_LOG_SYSERROR("Error closing handle to USB Read");
}
hp->bh_urh = NULL;
}
}
int
BBCClose(BBCHandle h)
{
bbc_hand *hp;
flashif_t *fif;
BBC_LOG(MSG_INFO, "BBCClose %d\n", h);
if (h < 0 || h >= BBC_MAX_DEVS)
return BBC_BADHANDLE;
if ((hp = handles[h]) == NULL)
return BBC_BADHANDLE;
if (hp->bh_type == BBC_HT_DIRECT
|| hp->bh_type == BBC_HT_MUX_ONLY) {
__bbc_kill_mux(hp);
BBC_LOG(MSG_DEBUG, "Close hp->bh_pofd\n");
(void) close(hp->bh_pofd);
BBC_LOG(MSG_DEBUG, "Close hp->bh_pifd\n");
(void) close(hp->bh_pifd);
//BBC_LOG(MSG_DEBUG, "Close muxParam.ifd\n");
//close(muxParam.ifd);
//BBC_LOG(MSG_DEBUG, "Close muxParam.ofd\n");
//close(muxParam.ofd);
}
else if ((fif = hp->bh_flashif) != NULL) {
(*fif->close)(fif->f);
}
if (hp->bh_fat)
(void) free(hp->bh_fat);
if (hp->bh_flashif)
(void) free(hp->bh_flashif);
(void) free(hp);
handles[h] = NULL;
BBC_LOG(MSG_DEBUG, "BBCClose done\n");
return BBC_OK;
}
char*
BBCGetDevName(BBCHandle h, int type)
{
bbc_hand *hp;
if (h < 0 || h >= BBC_MAX_DEVS)
return NULL;
if ((hp = handles[h]) == NULL)
return NULL;
return type ? hp->bh_writename: hp->bh_readname;
}