Initial Commit
[Projects.git] / pkgbuilds / wiifuse / src / wiifuse-0.2.0 / server / source / usb.c
blobfe046abee5b3a10b8a0b50358cc03c4cca30b587
1 /*---------------------------------------------------------------------------------------------
2 * USB Gecko Development Kit - http://www.usbgecko.com
3 * --------------------------------------------------------------------------------------------
4 *
6 * usb.c - V1.2 functions for the USB Gecko adapter (www.usbgecko.com).
7 * Now works for Wii Mode - use WIIMODE define in usb.h to set
8 * Copyright (c) 2008 - Nuke - <wiinuke@gmail.com>
9 *
10 *---------------------------------------------------------------------------------------------*/
13 #include <gccore.h> // If not using libogc need types
14 #include "usb.h"
16 /*---------------------------------------------------------------------------------------------*
17 Name: usb_sendbyte
18 Description: Send byte to Gamecube/Wii over EXI memory card port
19 *----------------------------------------------------------------------------------------------*/
21 static int __usb_sendbyte (char sendbyte)
23 s32 i;
25 exi_chan1sr = 0x000000D0;
26 exi_chan1data = 0xB0000000 | (sendbyte<<20);
27 exi_chan1cr = 0x19;
28 while((exi_chan1cr)&1);
29 i = exi_chan1data;
30 exi_chan1sr = 0;
31 if (i&0x04000000){
32 return 1;
34 return 0;
37 /*---------------------------------------------------------------------------------------------*
38 Name: usb_receivebyte
39 Description: Receive byte from Gamecube/Wii over EXI memory card port
40 *----------------------------------------------------------------------------------------------*/
42 static int __usb_receivebyte (char *receivebyte)
44 s32 i = 0;
46 exi_chan1sr = 0x000000D0;
47 exi_chan1data = 0xA0000000;
48 exi_chan1cr = 0x19;
49 while((exi_chan1cr)&1);
50 i = exi_chan1data;
51 exi_chan1sr = 0;
52 if (i&0x08000000){
53 *receivebyte=(i>>16)&0xff;
54 return 1;
56 return 0;
59 /*---------------------------------------------------------------------------------------------*
60 Name: usb_checksendstatus
61 Description: Chesk the FIFO is ready to send
62 *----------------------------------------------------------------------------------------------*/
64 static int __usb_checksendstatus()
66 s32 i = 0;
68 exi_chan1sr = 0x000000D0;
69 exi_chan1data = 0xC0000000;
70 exi_chan1cr = 0x19;
71 while((exi_chan1cr)&1);
72 i = exi_chan1data;
73 exi_chan1sr = 0x0;
74 if (i&0x04000000){
75 return 1;
77 return 0;
80 /*---------------------------------------------------------------------------------------------*
81 Name: usb_checkreceivestatus
82 Description: Check the FIFO is ready to receive
83 *----------------------------------------------------------------------------------------------*/
85 static int __usb_checkreceivestatus()
87 s32 i = 0;
88 exi_chan1sr = 0x000000D0;
89 exi_chan1data = 0xD0000000;
90 exi_chan1cr = 0x19;
91 while((exi_chan1cr)&1);
92 i = exi_chan1data;
93 exi_chan1sr = 0x0;
94 if (i&0x04000000){
95 return 1;
97 return 0;
100 /*---------------------------------------------------------------------------------------------*
101 Name: usb_sendbuffer
102 Description: Simple buffer send routine
103 *----------------------------------------------------------------------------------------------*/
105 void usb_sendbuffer (const void *buffer, int size)
107 char *sendbyte = (char*) buffer;
108 s32 bytesleft = size;
109 s32 returnvalue;
111 while (bytesleft > 0)
113 returnvalue = __usb_sendbyte(*sendbyte);
114 if(returnvalue) {
115 sendbyte++;
116 bytesleft--;
121 /*---------------------------------------------------------------------------------------------*
122 Name: usb_receivebuffer
123 Description: Simple buffer receive routine
124 *----------------------------------------------------------------------------------------------*/
126 void usb_receivebuffer (void *buffer, int size)
128 char *receivebyte = (char*)buffer;
129 s32 bytesleft = size;
130 s32 returnvalue;
132 while (bytesleft > 0)
134 returnvalue = __usb_receivebyte(receivebyte);
135 if(returnvalue) {
136 receivebyte++;
137 bytesleft--;
142 /*---------------------------------------------------------------------------------------------*
143 Name: usb_sendbuffersafe
144 Description: Simple buffer send routine with fifo check (use for large transfers)
145 *----------------------------------------------------------------------------------------------*/
147 void usb_sendbuffersafe (const void *buffer, int size)
149 char *sendbyte = (char*) buffer;
150 s32 bytesleft = size;
151 s32 returnvalue;
153 while (bytesleft > 0)
155 if(__usb_checksendstatus()){
156 returnvalue = __usb_sendbyte(*sendbyte);
157 if(returnvalue) {
158 sendbyte++;
159 bytesleft--;
165 /*---------------------------------------------------------------------------------------------*
166 Name: usb_receivebuffersafe
167 Description: Simple buffer receive routine with fifo check (use for large transfers)
168 *----------------------------------------------------------------------------------------------*/
170 void usb_receivebuffersafe (void *buffer, int size)
172 char *receivebyte = (char*)buffer;
173 s32 bytesleft = size;
174 s32 returnvalue;
176 while (bytesleft > 0)
178 if(__usb_checkreceivestatus()){
179 returnvalue = __usb_receivebyte(receivebyte);
180 if(returnvalue) {
181 receivebyte++;
182 bytesleft--;
188 /*---------------------------------------------------------------------------------------------*
189 Name: usb_checkgecko
190 Description: Chesk the Gecko is connected
191 *----------------------------------------------------------------------------------------------*/
192 int usb_checkgecko()
194 s32 i = 0;
196 exi_chan1sr = 0x000000D0;
197 exi_chan1data = 0x90000000;
198 exi_chan1cr = 0x19;
199 while((exi_chan1cr)&1);
200 i = exi_chan1data;
201 exi_chan1sr = 0x0;
202 if (i==0x04700000){
203 return 1;
205 return 0;
208 /*---------------------------------------------------------------------------------------------*
209 Name: usb_flush
210 Description: Flushes the FIFO, Use at the start of your program to avoid trash
211 *----------------------------------------------------------------------------------------------*/
213 void usb_flush()
215 char tempbyte;
217 while (__usb_receivebyte(&tempbyte));