sdk-world v2026.11.05
PS2 SDK - world tier (copyleft-allowed, non-essential packages)
Loading...
Searching...
No Matches
mmce_cmds.c
1#include <stdio.h>
2#include <sysclib.h>
3#include <tamtypes.h>
4
5#include "mmce_sio2.h"
6#include "mmce_cmds.h"
7
8#include "module_debug.h"
9
10int mmce_cmd_ping(void)
11{
12 int res;
13
14 u8 wrbuf[0x7];
15 u8 rdbuf[0x7];
16
17 wrbuf[0x0] = MMCE_ID; //identifier
18 wrbuf[0x1] = MMCE_CMD_PING; //command
19 wrbuf[0x2] = MMCE_RESERVED; //reserved byte
20 wrbuf[0x3] = 0;
21 wrbuf[0x4] = 0;
22 wrbuf[0x5] = 0;
23 wrbuf[0x6] = 0;
24
25 mmce_sio2_lock();
26 res = mmce_sio2_tx_rx_pio(sizeof(wrbuf), sizeof(rdbuf), wrbuf, rdbuf, &timeout_200ms);
27 mmce_sio2_unlock();
28 if (res == -1) {
29 DPRINTF("%s ERROR: Timedout waiting for /ACK\n", __func__);
30 return -1;
31 }
32
33 //bits 24-16: protocol ver
34 //bits 16-8: product id
35 //bits 8-0: revision id
36 if (rdbuf[0x1] == MMCE_REPLY_CONST) {
37 res = rdbuf[0x3] << 16 | rdbuf[0x4] << 8 | rdbuf[0x5];
38 } else {
39 DPRINTF("%s ERROR: Invalid response from card. Got 0x%x, Expected 0x%x\n", __func__, rdbuf[0x1], MMCE_REPLY_CONST);
40 res = -1;
41 }
42
43 return res;
44}
45
46//TODO:
47int mmce_cmd_get_status(void)
48{
49 int res;
50
51 u8 wrbuf[0x3];
52 u8 rdbuf[0x6];
53
54 wrbuf[0x0] = MMCE_ID; //identifier
55 wrbuf[0x1] = MMCE_CMD_GET_STATUS; //command
56 wrbuf[0x2] = MMCE_RESERVED; //reserved byte
57
58 mmce_sio2_lock();
59 res = mmce_sio2_tx_rx_pio(sizeof(wrbuf), sizeof(rdbuf), wrbuf, rdbuf, &timeout_1s);
60 mmce_sio2_unlock();
61 if (res == -1) {
62 DPRINTF("%s ERROR: Timedout waiting for /ACK\n", __func__);
63 return -1;
64 }
65
66 if (rdbuf[0x1] == MMCE_REPLY_CONST) {
67 res = rdbuf[0x3] << 8 | rdbuf[0x4];
68 } else {
69 DPRINTF("%s ERROR: Invalid response from card. Got 0x%x, Expected 0x%x\n", __func__, rdbuf[0x1], MMCE_REPLY_CONST);
70 res = -1;
71 }
72
73 return res;
74}
75
76int mmce_cmd_get_card(void)
77{
78 int res;
79
80 u8 wrbuf[0x3];
81 u8 rdbuf[0x6];
82
83 wrbuf[0x0] = MMCE_ID; //identifier
84 wrbuf[0x1] = MMCE_CMD_GET_CARD; //command
85 wrbuf[0x2] = MMCE_RESERVED; //reserved byte
86
87 mmce_sio2_lock();
88 res = mmce_sio2_tx_rx_pio(sizeof(wrbuf), sizeof(rdbuf), wrbuf, rdbuf, &timeout_1s);
89 mmce_sio2_unlock();
90 if (res == -1) {
91 DPRINTF("%s ERROR: Timedout waiting for /ACK\n", __func__);
92 return -1;
93 }
94
95 if (rdbuf[0x1] == MMCE_REPLY_CONST) {
96 res = rdbuf[0x3] << 8 | rdbuf[0x4];
97 } else {
98 DPRINTF("%s ERROR: Invalid response from card. Got 0x%x, Expected 0x%x\n", __func__, rdbuf[0x1], MMCE_REPLY_CONST);
99 res = -1;
100 }
101
102 return res;
103}
104
105int mmce_cmd_set_card(u8 type, u8 mode, u16 num)
106{
107 int res;
108
109 u8 wrbuf[0x8];
110 u8 rdbuf[0x2];
111
112 wrbuf[0x0] = MMCE_ID; //identifier
113 wrbuf[0x1] = MMCE_CMD_SET_CARD; //command
114 wrbuf[0x2] = MMCE_RESERVED; //reserved byte
115 wrbuf[0x3] = type; //card type (0 = regular, 1 = boot)
116 wrbuf[0x4] = mode; //set mode (num, next, prev)
117 wrbuf[0x5] = num >> 8; //card number upper 8 bits
118 wrbuf[0x6] = num & 0xFF; //card number lower 8 bits
119
120 mmce_sio2_lock();
121 res = mmce_sio2_tx_rx_pio(sizeof(wrbuf), sizeof(rdbuf), wrbuf, rdbuf, &timeout_1s);
122 mmce_sio2_unlock();
123 if (res == -1) {
124 DPRINTF("%s ERROR: Timedout waiting for /ACK\n", __func__);
125 return -1;
126 }
127
128 if (rdbuf[0x1] != MMCE_REPLY_CONST) {
129 DPRINTF("%s ERROR: Invalid response from card. Got 0x%x, Expected 0x%x\n", __func__, rdbuf[0x1], MMCE_REPLY_CONST);
130 return -1;
131 }
132
133 return 0;
134}
135
136int mmce_cmd_get_channel(void)
137{
138 int res;
139
140 u8 wrbuf[0x3];
141 u8 rdbuf[0x6];
142
143 wrbuf[0x0] = MMCE_ID; //identifier
144 wrbuf[0x1] = MMCE_CMD_GET_CHANNEL; //command
145 wrbuf[0x2] = MMCE_RESERVED; //reserved byte
146
147 mmce_sio2_lock();
148 res = mmce_sio2_tx_rx_pio(sizeof(wrbuf), sizeof(rdbuf), wrbuf, rdbuf, &timeout_1s);
149 mmce_sio2_unlock();
150 if (res == -1) {
151 DPRINTF("%s ERROR: Timedout waiting for /ACK\n", __func__);
152 return -1;
153 }
154
155 if (rdbuf[0x1] == MMCE_REPLY_CONST) {
156 res = rdbuf[0x3] << 8 | rdbuf[0x4];
157 } else {
158 DPRINTF("%s ERROR: Invalid response from card. Got 0x%x, Expected 0x%x\n", __func__, rdbuf[0x1], MMCE_REPLY_CONST);
159 res = -1;
160 }
161
162 return res;
163}
164
165int mmce_cmd_set_channel(u8 mode, u16 num)
166{
167 int res;
168
169 u8 wrbuf[0x7];
170 u8 rdbuf[0x2];
171
172 wrbuf[0x0] = MMCE_ID; //identifier
173 wrbuf[0x1] = MMCE_CMD_SET_CHANNEL; //command
174 wrbuf[0x2] = MMCE_RESERVED; //reserved byte
175 wrbuf[0x3] = mode; //set mode (num, next, prev)
176 wrbuf[0x4] = num >> 8; //channel number upper 8 bits
177 wrbuf[0x5] = num & 0xFF; //channel number lower 8 bits
178
179 mmce_sio2_lock();
180 res = mmce_sio2_tx_rx_pio(sizeof(wrbuf), sizeof(rdbuf), wrbuf, rdbuf, &timeout_1s);
181 mmce_sio2_unlock();
182 if (res == -1) {
183 DPRINTF("%s ERROR: Timedout waiting for /ACK\n", __func__);
184 return -1;
185 }
186
187 if (rdbuf[0x1] != MMCE_REPLY_CONST) {
188 DPRINTF("%s ERROR: Invalid response from card. Got 0x%x, Expected 0x%x\n", __func__, rdbuf[0x1], MMCE_REPLY_CONST);
189 return -1;
190 }
191
192 return 0;
193}
194
195int mmce_cmd_get_gameid(void *ptr)
196{
197 int res;
198
199 u8 wrbuf[0x3];
200 u8 rdbuf[0xFF]; //fixed packet size of 255 bytes
201
202 wrbuf[0x0] = MMCE_ID; //identifier
203 wrbuf[0x1] = MMCE_CMD_GET_GAMEID; //command
204 wrbuf[0x2] = MMCE_RESERVED; //reserved byte
205
206 mmce_sio2_lock();
207 res = mmce_sio2_tx_rx_pio(sizeof(wrbuf), sizeof(rdbuf), wrbuf, rdbuf, &timeout_1s);
208 mmce_sio2_unlock();
209 if (res == -1) {
210 DPRINTF("%s ERROR: Timedout waiting for /ACK\n", __func__);
211 return -1;
212 }
213
214 if (rdbuf[0x1] == MMCE_REPLY_CONST) {
215 char* str = &rdbuf[0x4];
216 strcpy(ptr, str);
217 res = 0;
218 } else {
219 DPRINTF("%s ERROR: Invalid response from card. Got 0x%x, Expected 0x%x\n", __func__, rdbuf[0x1], MMCE_REPLY_CONST);
220 res = -1;
221 }
222
223 return res;
224}
225
226int mmce_cmd_set_gameid(void *ptr)
227{
228 int res;
229
230 u8 len = strlen(ptr) + 1;
231
232 u8 wrbuf[0xFF];
233 u8 rdbuf[0x2];
234
235 wrbuf[0x0] = MMCE_ID; //identifier
236 wrbuf[0x1] = MMCE_CMD_SET_GAMEID; //command
237 wrbuf[0x2] = MMCE_RESERVED; //reserved byte
238 wrbuf[0x3] = len; //gameid length
239
240 char *str = &wrbuf[0x4];
241 strcpy(str, ptr);
242
243 mmce_sio2_lock();
244 res = mmce_sio2_tx_rx_pio(len + 5, sizeof(rdbuf), wrbuf, rdbuf, &timeout_1s);
245 mmce_sio2_unlock();
246 if (res == -1) {
247 DPRINTF("%s ERROR: Timedout waiting for /ACK\n", __func__);
248 return -1;
249 }
250
251 if (rdbuf[0x1] != MMCE_REPLY_CONST) {
252 DPRINTF("%s ERROR: Invalid response from card. Got 0x%x, Expected 0x%x\n", __func__, rdbuf[0x1], MMCE_REPLY_CONST);
253 return -1;
254 }
255
256 return 0;
257}
258
259int mmce_cmd_reset(void)
260{
261 int res;
262
263 u8 wrbuf[0x5];
264 u8 rdbuf[0x5];
265
266 wrbuf[0x0] = MMCE_ID; //identifier
267 wrbuf[0x1] = MMCE_CMD_RESET; //command
268 wrbuf[0x2] = MMCE_RESERVED; //reserved byte
269
270 mmce_sio2_lock();
271 res = mmce_sio2_tx_rx_pio(sizeof(wrbuf), sizeof(rdbuf), wrbuf, rdbuf, &timeout_1s);
272 mmce_sio2_unlock();
273 if (res == -1) {
274 DPRINTF("%s ERROR: Timedout waiting for /ACK\n", __func__);
275 return -1;
276 }
277
278 if (rdbuf[0x1] != MMCE_REPLY_CONST) {
279 DPRINTF("%s ERROR: Invalid response from card. Got 0x%x, Expected 0x%x\n", __func__, rdbuf[0x1], MMCE_REPLY_CONST);
280 return -1;
281 }
282
283 return 0;
284}