forked from devkitPro/libogc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeckousb.c
More file actions
193 lines (148 loc) · 3.08 KB
/
Copy pathgeckousb.c
File metadata and controls
193 lines (148 loc) · 3.08 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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <gccore.h>
#include "geckousb.h"
static struct dbginterface usb_device;
static __inline__ int __send_command(s32 chn,u16 *cmd)
{
s32 ret;
ret = 0;
if(!EXI_Select(chn,EXI_DEVICE_0,EXI_SPEED32MHZ)) ret |= 0x01;
if(!EXI_Imm(chn,cmd,sizeof(u16),EXI_READWRITE,NULL)) ret |= 0x02;
if(!EXI_Sync(chn)) ret |= 0x04;
if(!EXI_Deselect(chn)) ret |= 0x08;
if(ret) return 0;
return 1;
}
static int __usb_sendbyte(s32 chn,char ch)
{
s32 ret;
u16 val;
val = (0xB000|_SHIFTL(ch,4,8));
ret = __send_command(chn,&val);
if(ret==1 && !(val&0x0400)) ret = 0;
return ret;
}
static int __usb_recvbyte(s32 chn,char *ch)
{
s32 ret;
u16 val;
*ch = 0;
val = 0xA000;
ret = __send_command(chn,&val);
if(ret==1 && !(val&0x0800)) ret = 0;
else if(ret==1) *ch = (val&0xff);
return ret;
}
static int __usb_checksend(s32 chn)
{
s32 ret;
u16 val;
val = 0xC000;
ret = __send_command(chn,&val);
if(ret==1 && !(val&0x0400)) ret = 0;
return ret;
}
static int __usb_checkrecv(s32 chn)
{
s32 ret;
u16 val;
val = 0xD000;
ret = __send_command(chn,&val);
if(ret==1 && !(val&0x0400)) ret = 0;
return ret;
}
static void __usb_flush(s32 chn)
{
char tmp;
if(!EXI_Lock(chn,EXI_DEVICE_0,NULL)) return;
while(__usb_recvbyte(chn,&tmp));
EXI_Unlock(chn);
}
static int __usb_isgeckoalive(s32 chn)
{
s32 ret;
u16 val;
if(!EXI_Lock(chn,EXI_DEVICE_0,NULL)) return 0;
val = 0x9000;
ret = __send_command(chn,&val);
if(ret==1 && !(val&0x0470)) ret = 0;
EXI_Unlock(chn);
return ret;
}
static int __usb_recvbuffer(s32 chn,void *buffer,int size)
{
s32 ret;
s32 left = size;
char *ptr = (char*)buffer;
if(!EXI_Lock(chn,EXI_DEVICE_0,NULL)) return 0;
while(left>0) {
if(__usb_checkrecv(chn)) {
ret = __usb_recvbyte(chn,ptr);
if(ret==0) break;
ptr++;
left--;
}
}
EXI_Unlock(chn);
return (size - left);
}
static int __usb_sendbuffer(s32 chn,const void *buffer,int size)
{
s32 ret;
s32 left = size;
char *ptr = (char*)buffer;
if(!EXI_Lock(chn,EXI_DEVICE_0,NULL)) return 0;
while(left>0) {
if(__usb_checksend(chn)) {
ret = __usb_sendbyte(chn,*ptr);
if(ret==0) break;
ptr++;
left--;
}
}
EXI_Unlock(chn);
return (size - left);
}
static int usbopen(struct dbginterface *device)
{
if(!__usb_isgeckoalive(device->fhndl)) {
return -1;
}
return 0;
}
static int usbclose(struct dbginterface *device)
{
return 0;
}
static int usbwait(struct dbginterface *device)
{
return 0;
}
static int usbread(struct dbginterface *device,void *buffer,int size)
{
int ret;
ret = __usb_recvbuffer(device->fhndl,buffer,size);
return ret;
}
static int usbwrite(struct dbginterface *device,const void *buffer,int size)
{
int ret;
ret = __usb_sendbuffer(device->fhndl,buffer,size);
return ret;
}
struct dbginterface* usb_init(s32 channel)
{
usb_device.fhndl = channel;
if(__usb_isgeckoalive(channel))
__usb_flush(channel);
usb_device.open = usbopen;
usb_device.close = usbclose;
usb_device.wait = usbwait;
usb_device.read = usbread;
usb_device.write = usbwrite;
return &usb_device;
}