9static McCommandHandler_t McCommandHandler;
10static McDevIDHandler_t McDevIDHandler;
13static void _printf4(
const char *format, ...);
18static void _printf4(
const char *format, ...){
24void ResetMcHandlers(
void)
26 McCommandHandler = NULL;
27 McDevIDHandler = NULL;
31void SetMcCommandHandler(McCommandHandler_t handler)
33 McCommandHandler = handler;
37McCommandHandler_t GetMcCommandHandler(
void)
39 return McCommandHandler;
43void SetMcDevIDHandler(McDevIDHandler_t handler)
45 McDevIDHandler = handler;
49McDevIDHandler_t GetMcDevIDHandler(
void)
51 return McDevIDHandler;
55int McDeviceIDToCNum(
int port,
int slot)
57 if (McDevIDHandler == NULL) {
61 return McDevIDHandler(port, slot);
67 return McCommandHandler(port, slot, sio2packet);
71unsigned char calculate_sio2_buffer_checksum(
const void *buffer,
unsigned int length)
73 unsigned int i, checksum;
75 for (checksum = 0, i = 0; i < length; i++)
76 checksum ^= ((
const unsigned char *)buffer)[i];
82int card_auth_write(
int port,
int slot,
const void *buffer,
int arg3,
int command)
86 unsigned char wrbuf[14], rdbuf[14];
88 sio2packet.in = wrbuf;
89 sio2packet.out = rdbuf;
90 memset(sio2packet.port_ctrl1, 0,
sizeof(sio2packet.port_ctrl1));
91 memset(sio2packet.port_ctrl2, 0,
sizeof(sio2packet.port_ctrl2));
92 sio2packet.port_ctrl1[port] = 0xFF060505;
93 sio2packet.port_ctrl2[port] = 0x0003FFFF;
94 sio2packet.regdata[0] = (port & 3) | 0x00380E40;
95 sio2packet.regdata[1] = 0;
96 sio2packet.out_size =
sizeof(rdbuf);
97 sio2packet.in_size =
sizeof(wrbuf);
98 memset(sio2packet.in, 0,
sizeof(wrbuf));
99 sio2packet.in[0] = 0x81;
100 sio2packet.in[1] = arg3;
101 sio2packet.in[2] = command;
102 for (i = 0; i < 8; i++)
103 sio2packet.in[3 + i] = ((
const unsigned char *)buffer)[7 - i];
104 sio2packet.in[11] = calculate_sio2_buffer_checksum(buffer, 8);
105 sio2packet.out_dma.addr = NULL;
106 sio2packet.in_dma.addr = NULL;
108 if (SendMcCommand(port, slot, &sio2packet) == 0) {
109 _printf4(
"card_command error %d\n", (
unsigned char)command);
113 if ((!((sio2packet.stat6c >> 13) & 1) && !((sio2packet.stat6c >> 14) & 3)) == 0) {
114 _printf4(
"sio2 command error %d\n", (
unsigned char)command);
118 if (rdbuf[12] != 0x2B) {
119 _printf4(
"ID error %d\n", (
unsigned char)command);
123 if (rdbuf[13] == 0x66) {
124 _printf4(
"result failed %d\n", (
unsigned char)command);
132int card_auth_read(
int port,
int slot,
void *buffer,
int arg3,
int command)
139 sio2packet.in = wrbuf;
140 sio2packet.out = rdbuf;
141 memset(sio2packet.port_ctrl1, 0,
sizeof(sio2packet.port_ctrl1));
142 memset(sio2packet.port_ctrl2, 0,
sizeof(sio2packet.port_ctrl2));
143 sio2packet.port_ctrl1[port] = 0xFF060505;
144 sio2packet.port_ctrl2[port] = 0x0003FFFF;
145 sio2packet.regdata[0] = (port & 3) | 0x00380E40;
146 sio2packet.regdata[1] = 0;
147 sio2packet.out_size =
sizeof(rdbuf);
148 sio2packet.in_size =
sizeof(wrbuf);
149 memset(sio2packet.in, 0,
sizeof(wrbuf));
150 sio2packet.in[0] = 0x81;
151 sio2packet.in[1] = arg3;
152 sio2packet.in[2] = command;
153 sio2packet.out_dma.addr = NULL;
154 sio2packet.in_dma.addr = NULL;
156 if (SendMcCommand(port, slot, &sio2packet) == 0) {
157 _printf4(
"card_command error %d\n", (
unsigned char)command);
161 if ((!((sio2packet.stat6c >> 13) & 1) && !((sio2packet.stat6c >> 14) & 3)) == 0) {
162 _printf4(
"sio2 command error %d\n", (
unsigned char)command);
166 if (rdbuf[3] != 0x2B) {
167 _printf4(
"ID error %d\n", (
unsigned char)command);
171 if (rdbuf[13] == 0x66) {
172 _printf4(
"result failed %d\n", (
unsigned char)command);
176 if ((
unsigned char)calculate_sio2_buffer_checksum(&rdbuf[4], 8) != rdbuf[12]) {
177 _printf4(
"check sum error %d\n", (
unsigned char)command);
181 for (i = 0; i < 8; i++)
182 ((
unsigned char *)buffer)[7 - i] = rdbuf[4 + i];
188int card_auth_60(
int port,
int slot)
191 unsigned char wrbuf[5], rdbuf[5];
193 sio2packet.in = wrbuf;
194 sio2packet.out = rdbuf;
195 memset(sio2packet.port_ctrl1, 0,
sizeof(sio2packet.port_ctrl1));
196 memset(sio2packet.port_ctrl2, 0,
sizeof(sio2packet.port_ctrl2));
197 sio2packet.port_ctrl1[port] = 0xFF060505;
198 sio2packet.port_ctrl2[port] = 0x0003FFFF;
199 sio2packet.regdata[0] = (port & 3) | 0x00140540;
200 sio2packet.regdata[1] = 0;
201 sio2packet.out_size =
sizeof(rdbuf);
202 sio2packet.in_size =
sizeof(wrbuf);
203 memset(sio2packet.in, 0,
sizeof(wrbuf));
204 sio2packet.in[0] = 0x81;
205 sio2packet.in[1] = 0xF3;
206 sio2packet.out_dma.addr = NULL;
207 sio2packet.in_dma.addr = NULL;
209 if (SendMcCommand(port, slot, &sio2packet) == 0) {
210 _printf4(
"card_command error 0\n");
214 if ((!((sio2packet.stat6c >> 13) & 1) && !((sio2packet.stat6c >> 14) & 3)) == 0) {
215 _printf4(
"sio2 command error 0\n");
219 if (rdbuf[3] != 0x2B) {
220 _printf4(
"ID error 0\n");
224 if (rdbuf[4] == 0x66) {
225 _printf4(
"result failed 0\n");
233int card_auth_key_change(
int port,
int slot,
int command)
236 unsigned char wrbuf[5], rdbuf[5];
238 sio2packet.in = wrbuf;
239 sio2packet.out = rdbuf;
240 memset(sio2packet.port_ctrl1, 0,
sizeof(sio2packet.port_ctrl1));
241 memset(sio2packet.port_ctrl2, 0,
sizeof(sio2packet.port_ctrl2));
242 sio2packet.port_ctrl1[port] = 0xFF060505;
243 sio2packet.port_ctrl2[port] = 0x0003FFFF;
244 sio2packet.regdata[0] = (port & 3) | 0x00140540;
245 sio2packet.regdata[1] = 0;
246 sio2packet.out_size =
sizeof(rdbuf);
247 sio2packet.in_size =
sizeof(wrbuf);
248 memset(sio2packet.in, 0,
sizeof(wrbuf));
249 sio2packet.in[0] = 0x81;
250 sio2packet.in[1] = 0xF7;
251 sio2packet.in[2] = command;
252 sio2packet.out_dma.addr = NULL;
253 sio2packet.in_dma.addr = NULL;
255 if (SendMcCommand(port, slot, &sio2packet) == 0) {
256 _printf4(
"card_command error 0\n");
260 if ((!((sio2packet.stat6c >> 13) & 1) && !((sio2packet.stat6c >> 14) & 3)) == 0) {
261 _printf4(
"sio2 command error 0\n");
265 if (rdbuf[3] != 0x2B) {
266 _printf4(
"ID error 0\n");
270 if (rdbuf[4] == 0x66) {
271 _printf4(
"result failed 0\n");
279int card_auth2(
int port,
int slot,
int arg3,
int command)
282 unsigned char wrbuf[5], rdbuf[5];
284 sio2packet.in = wrbuf;
285 sio2packet.out = rdbuf;
286 memset(sio2packet.port_ctrl1, 0,
sizeof(sio2packet.port_ctrl1));
287 memset(sio2packet.port_ctrl2, 0,
sizeof(sio2packet.port_ctrl2));
288 sio2packet.port_ctrl1[port] = 0xFF060505;
289 sio2packet.port_ctrl2[port] = 0x0003FFFF;
290 sio2packet.regdata[0] = (port & 3) | 0x00140540;
291 sio2packet.regdata[1] = 0;
292 sio2packet.out_size =
sizeof(rdbuf);
293 sio2packet.in_size =
sizeof(wrbuf);
294 memset(sio2packet.in, 0,
sizeof(wrbuf));
295 sio2packet.in[0] = 0x81;
296 sio2packet.in[1] = arg3;
297 sio2packet.in[2] = command;
298 sio2packet.out_dma.addr = NULL;
299 sio2packet.in_dma.addr = NULL;
301 if (SendMcCommand(port, slot, &sio2packet) == 0) {
302 _printf4(
"card_command error %d\n", (
unsigned char)command);
306 if ((!((sio2packet.stat6c >> 13) & 1) && !((sio2packet.stat6c >> 14) & 3)) == 0) {
307 _printf4(
"sio2 command error %d\n", (
unsigned char)command);
311 if (rdbuf[3] != 0x2B) {
312 _printf4(
"ID error %d\n", (
unsigned char)command);
316 if (rdbuf[4] == 0x66) {
317 _printf4(
"result failed %d\n", (
unsigned char)command);
325int card_auth(
int port,
int slot,
int arg3,
int command)
328 unsigned char wrbuf[5], rdbuf[5];
330 sio2packet.in = wrbuf;
331 sio2packet.out = rdbuf;
332 memset(sio2packet.port_ctrl1, 0,
sizeof(sio2packet.port_ctrl1));
333 memset(sio2packet.port_ctrl2, 0,
sizeof(sio2packet.port_ctrl2));
334 sio2packet.port_ctrl1[port] = 0xFF060505;
335 sio2packet.port_ctrl2[port] = 0x0003FFFF;
336 sio2packet.regdata[0] = (port & 3) | 0x00140540;
337 sio2packet.regdata[1] = 0;
338 sio2packet.out_size =
sizeof(rdbuf);
339 sio2packet.in_size =
sizeof(wrbuf);
340 memset(sio2packet.in, 0,
sizeof(wrbuf));
341 sio2packet.in[0] = 0x81;
342 sio2packet.in[1] = arg3;
343 sio2packet.in[2] = command;
344 sio2packet.out_dma.addr = NULL;
345 sio2packet.in_dma.addr = NULL;
347 if (SendMcCommand(port, slot, &sio2packet) == 0) {
348 _printf4(
"card_command error %d\n", (
unsigned char)command);
352 if ((!((sio2packet.stat6c >> 13) & 1) && !((sio2packet.stat6c >> 14) & 3)) == 0) {
353 _printf4(
"sio2 command error %d\n", (
unsigned char)command);
357 if (rdbuf[3] != 0x2B) {
358 _printf4(
"ID error %d\n", (
unsigned char)command);
362 if (rdbuf[4] == 0x66) {
363 _printf4(
"result failed %d\n", (
unsigned char)command);