gecko.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (C) 2008 dhewg, #wiidev efnet
  3. *
  4. * this file is part of wiifuse
  5. * http://wiibrew.org/index.php?title=Wiifuse
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #ifndef __WIN32__
  27. #include <termios.h>
  28. #else
  29. #define WIN32_LEAN_AND_MEAN
  30. #include <windows.h>
  31. #endif
  32. #include "gecko.h"
  33. #ifndef O_SYNC
  34. #define O_SYNC 0
  35. #endif
  36. #define FTDI_PACKET_SIZE 3968
  37. #ifndef __WIN32__
  38. static int fd_gecko = -1;
  39. #else
  40. static HANDLE handle_gecko = NULL;
  41. #endif
  42. #ifdef USE_LIBFTDI
  43. #include "ftdi.h"
  44. #define DEFAULT_FTDI_VID 0x0403
  45. #define DEFAULT_FTDI_PID 0x6001
  46. struct ftdi_context gFTDIContext;
  47. int gUsingFTDI = 0;
  48. #endif
  49. int gecko_open(const char *dev) {
  50. #ifdef USE_LIBFTDI
  51. int err;
  52. ftdi_init(&gFTDIContext);
  53. if((err = ftdi_usb_open(&gFTDIContext, DEFAULT_FTDI_VID, DEFAULT_FTDI_PID) < 0)) {
  54. fprintf(stderr, "ftdi_usb_open_desc failed: %d (%s)\n", err, ftdi_get_error_string(&gFTDIContext));
  55. fprintf(stderr, "Perhaps you need to unload the FTDI kext with e.g.\n");
  56. fprintf(stderr, "\tsudo kextunload -b com.FTDI.driver.FTDIUSBSerialDriver\n");
  57. } else {
  58. gUsingFTDI=1;
  59. fprintf(stderr, "Found libftdi device\n");
  60. goto done;
  61. }
  62. #endif
  63. #ifndef __WIN32__
  64. struct termios newtio;
  65. fd_gecko = open(dev, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK);
  66. if (fd_gecko == -1) {
  67. perror("gecko_open");
  68. return 1;
  69. }
  70. if (fcntl(fd_gecko, F_SETFL, 0)) {
  71. perror("F_SETFL on serial port");
  72. return 1;
  73. }
  74. if (tcgetattr(fd_gecko, &newtio)) {
  75. perror("tcgetattr");
  76. return 1;
  77. }
  78. cfmakeraw(&newtio);
  79. newtio.c_cflag |= CRTSCTS | CS8 | CLOCAL | CREAD;
  80. if (tcsetattr(fd_gecko, TCSANOW, &newtio)) {
  81. perror("tcsetattr");
  82. return 1;
  83. }
  84. #else
  85. COMMTIMEOUTS timeouts;
  86. handle_gecko = CreateFile(dev, GENERIC_READ | GENERIC_WRITE, 0, 0,
  87. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  88. GetCommTimeouts(handle_gecko, &timeouts);
  89. timeouts.ReadIntervalTimeout = MAXDWORD;
  90. timeouts.ReadTotalTimeoutMultiplier = 0;
  91. timeouts.ReadTotalTimeoutConstant = 0;
  92. timeouts.WriteTotalTimeoutMultiplier = 0;
  93. timeouts.WriteTotalTimeoutConstant = 0;
  94. if (!SetCommTimeouts(handle_gecko, &timeouts)) {
  95. fprintf(stderr, "error setting timeouts on port\n");
  96. return 1;
  97. }
  98. if (!SetCommMask(handle_gecko, 0)) {
  99. fprintf(stderr, "error setting communications event mask\n");
  100. return 1;
  101. }
  102. #endif
  103. done:
  104. gecko_flush();
  105. return 0;
  106. }
  107. void gecko_close() {
  108. #ifdef USE_LIBFTDI
  109. if (gUsingFTDI) {
  110. ftdi_usb_close(&gFTDIContext);
  111. return;
  112. }
  113. #endif
  114. #ifndef __WIN32__
  115. if (fd_gecko > 0)
  116. close(fd_gecko);
  117. #else
  118. CloseHandle(handle_gecko);
  119. #endif
  120. }
  121. void gecko_flush() {
  122. #ifdef USE_LIBFTDI
  123. if (gUsingFTDI) {
  124. ftdi_usb_purge_buffers(&gFTDIContext);
  125. return;
  126. }
  127. #endif
  128. #ifndef __WIN32__
  129. tcflush(fd_gecko, TCIOFLUSH);
  130. #else
  131. PurgeComm(handle_gecko, PURGE_RXCLEAR | PURGE_TXCLEAR |
  132. PURGE_TXABORT | PURGE_RXABORT);
  133. #endif
  134. }
  135. int gecko_read(void *buf, size_t count) {
  136. size_t left, chunk;
  137. #ifndef __WIN32__
  138. size_t res;
  139. #else
  140. DWORD res;
  141. #endif
  142. left = count;
  143. while (left) {
  144. chunk = left;
  145. if (chunk > FTDI_PACKET_SIZE)
  146. chunk = FTDI_PACKET_SIZE;
  147. #ifdef USE_LIBFTDI
  148. if (gUsingFTDI)
  149. res = ftdi_read_data(&gFTDIContext, buf, chunk);
  150. else
  151. #endif
  152. #ifndef __WIN32__
  153. res = read(fd_gecko, buf, chunk);
  154. if (res < 1) {
  155. perror("gecko_read");
  156. return 1;
  157. }
  158. #else
  159. if (!ReadFile(handle_gecko, buf, chunk, &res, NULL)) {
  160. fprintf(stderr, "gecko_read\n");
  161. return 1;
  162. }
  163. #endif
  164. left -= res;
  165. buf += res;
  166. }
  167. return 0;
  168. }
  169. int gecko_write(const void *buf, size_t count) {
  170. size_t left, chunk;
  171. #ifndef __WIN32__
  172. size_t res;
  173. #else
  174. DWORD res;
  175. #endif
  176. left = count;
  177. while (left) {
  178. chunk = left;
  179. if (chunk > FTDI_PACKET_SIZE)
  180. chunk = FTDI_PACKET_SIZE;
  181. #ifdef USE_LIBFTDI
  182. if (gUsingFTDI)
  183. res = ftdi_write_data(&gFTDIContext, (unsigned char *)buf, chunk);
  184. else
  185. #endif
  186. #ifndef __WIN32__
  187. res = write(fd_gecko, buf, count);
  188. if (res < 1) {
  189. perror("gecko_write");
  190. return 1;
  191. }
  192. #else
  193. if (!WriteFile(handle_gecko, buf, chunk, &res, NULL)) {
  194. fprintf (stderr, "gecko_write\n");
  195. return 1;
  196. }
  197. #endif
  198. left -= res;
  199. buf += res;
  200. #if !defined(__WIN32__) && !defined(USE_LIBFTDI)
  201. // does this work with ftdi-sio?
  202. if (tcdrain(fd_gecko)) {
  203. perror ("gecko_drain");
  204. return 1;
  205. }
  206. #endif
  207. }
  208. return 0;
  209. }