forked from mrneo240/openmenu
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvm2_api.c
More file actions
151 lines (123 loc) · 3.43 KB
/
vm2_api.c
File metadata and controls
151 lines (123 loc) · 3.43 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
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <kos/genwait.h>
#include <dc/maple.h>
#include "inc/vm2_api.h"
static uint8_t recv_buff[196];
static void vbl_allinfo_callback(maple_state_t *state, maple_frame_t * frm)
{
maple_response_t *resp;
(void) state;
/* So.. did we get a response? */
resp = (maple_response_t *)frm->recv_buf;
if (resp->response == MAPLE_RESPONSE_ALLINFO)
{
/* Copy in the new buff */
memcpy(recv_buff, resp, 196);
}
maple_frame_unlock(frm);
genwait_wake_all(frm);
}
/* Send a ALLINFO command for the given port/unit */
static int send_allinfo(maple_device_t * dev)
{
// Reserve access; if we don't get it, forget about it
if (maple_frame_lock(&dev->frame) < 0)
{
return -1;
}
// Setup our autodetect frame to probe at a new device
maple_frame_init(&dev->frame);
dev->frame.cmd = MAPLE_COMMAND_ALLINFO;
dev->frame.dst_port = dev->port;
dev->frame.dst_unit = dev->unit;
dev->frame.callback = vbl_allinfo_callback;
maple_queue_frame(&dev->frame);
/* Wait for the VM2 to accept it */
if(genwait_wait(&dev->frame, "vmu_get_allinfo", 200, NULL) < 0)
{
if(dev->frame.state != MAPLE_FRAME_UNSENT)
{
/* It's probably never coming back, so just unlock the frame */
dev->frame.state = MAPLE_FRAME_VACANT;
/*dbglog(DBG_ERROR, */printf("send_allinfo: timeout to unit %c%c\n", dev->port + 'A', dev->unit + '0');
return MAPLE_ETIMEOUT;
}
}
return MAPLE_EOK;
}
static void vm2_reply(maple_state_t *state, maple_frame_t * frm)
{
maple_response_t *resp;
(void) state;
/* So.. did we get a response? */
resp = (maple_response_t *)frm->recv_buf;
if (resp->response != MAPLE_RESPONSE_OK)
{
printf("maple: bad response %d on device, wait ACK\n",resp->response);
}
memcpy(recv_buff, resp, 4);
maple_frame_unlock(frm);
genwait_wake_all(frm);
}
int vm2_set_id(maple_device_t * dev, const char *ID, const char *name)
{
maple_response_t *resp;
uint32_t *send_buf;
wait_vm2:
if(maple_frame_lock(&dev->frame) < 0)
return 0;
maple_frame_init(&dev->frame);
send_buf = (uint32_t *)dev->frame.recv_buf;
send_buf[0] = MAPLE_FUNC_MEMCARD;
strncpy((char *) &dev->frame.recv_buf[4], ID, 12);
if (name)
{
strncpy((char *) &dev->frame.recv_buf[16], name, 128);
}
dev->frame.cmd = 33;
dev->frame.dst_port = dev->port;
dev->frame.dst_unit = dev->unit;
dev->frame.length = name ? 36 : 4;
dev->frame.callback = vm2_reply;
dev->frame.send_buf = send_buf;
maple_queue_frame(&dev->frame);
/* Wait for the VM2 to accept it */
if(genwait_wait(&dev->frame, "vm2_set_id", 200, NULL) < 0)
{
if(dev->frame.state != MAPLE_FRAME_UNSENT)
{
/* It's probably never coming back, so just unlock the frame */
dev->frame.state = MAPLE_FRAME_VACANT;
dbglog(DBG_ERROR, "vm2_set_id: timeout to unit %c%c\n", dev->port + 'A', dev->unit + '0');
return -1;
}
}
resp = (maple_response_t *) recv_buff;
if (resp->response == MAPLE_RESPONSE_OK)
{
return 0;
}
else if (resp->response == MAPLE_RESPONSE_AGAIN)
{
goto wait_vm2;
}
return -1;
}
int check_vm2_present(maple_device_t * dev)
{
/* Clear the old buffer */
memset(recv_buff, 0, 196);
if (send_allinfo(dev) != MAPLE_EOK)
{
return 0;
}
maple_alldevinfo_t *info = (maple_alldevinfo_t *) &recv_buff[4];
if (!strncasecmp(info->extended, "VM2 by Dreamware", 16) ||
!strncasecmp(info->extended, "USB RP2040 EMU ", 16))
{
return 1;
}
return 0;
}