|
| 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 | + |
| 22 | +#include <stdio.h> |
| 23 | +#include <string.h> |
| 24 | +#include <unistd.h> |
| 25 | +#include <fcntl.h> |
| 26 | +#include <errno.h> |
| 27 | + |
| 28 | +#ifndef __WIN32__ |
| 29 | +#include <termios.h> |
| 30 | +#else |
| 31 | +#define WIN32_LEAN_AND_MEAN |
| 32 | +#include <windows.h> |
| 33 | +#endif |
| 34 | + |
| 35 | +#include "gecko.h" |
| 36 | + |
| 37 | +#define FTDI_PACKET_SIZE 3968 |
| 38 | + |
| 39 | +#ifndef __WIN32__ |
| 40 | +static int fd_gecko = -1; |
| 41 | +#else |
| 42 | +static HANDLE handle_gecko = NULL; |
| 43 | +#endif |
| 44 | + |
| 45 | +int gecko_open(const char *dev) { |
| 46 | +#ifndef __WIN32__ |
| 47 | + struct termios newtio; |
| 48 | + |
| 49 | + fd_gecko = open(dev, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK); |
| 50 | + |
| 51 | + if (fd_gecko == -1) { |
| 52 | + perror("gecko_open"); |
| 53 | + return 1; |
| 54 | + } |
| 55 | + |
| 56 | + if (fcntl(fd_gecko, F_SETFL, 0)) { |
| 57 | + perror("F_SETFL on serial port"); |
| 58 | + return 1; |
| 59 | + } |
| 60 | + |
| 61 | + if (tcgetattr(fd_gecko, &newtio)) { |
| 62 | + perror("tcgetattr"); |
| 63 | + return 1; |
| 64 | + } |
| 65 | + |
| 66 | + cfmakeraw(&newtio); |
| 67 | + |
| 68 | + newtio.c_cflag |= CRTSCTS | CS8 | CLOCAL | CREAD; |
| 69 | + |
| 70 | + if (tcsetattr(fd_gecko, TCSANOW, &newtio)) { |
| 71 | + perror("tcsetattr"); |
| 72 | + return 1; |
| 73 | + } |
| 74 | +#else |
| 75 | + COMMTIMEOUTS timeouts; |
| 76 | + |
| 77 | + handle_gecko = CreateFile(dev, GENERIC_READ | GENERIC_WRITE, 0, 0, |
| 78 | + OPEN_EXISTING, 0, 0); |
| 79 | + |
| 80 | + GetCommTimeouts(handle_gecko, &timeouts); |
| 81 | + |
| 82 | + timeouts.ReadIntervalTimeout = MAXDWORD; |
| 83 | + timeouts.ReadTotalTimeoutMultiplier = 0; |
| 84 | + timeouts.ReadTotalTimeoutConstant = 0; |
| 85 | + timeouts.WriteTotalTimeoutMultiplier = 0; |
| 86 | + timeouts.WriteTotalTimeoutConstant = 0; |
| 87 | + |
| 88 | + if (!SetCommTimeouts(handle_gecko, &timeouts)) { |
| 89 | + fprintf(stderr, "error setting timeouts on port\n"); |
| 90 | + return 1; |
| 91 | + } |
| 92 | + |
| 93 | + if (!SetCommMask(handle_gecko, 0)) { |
| 94 | + fprintf(stderr, "error setting communications event mask\n"); |
| 95 | + return 1; |
| 96 | + } |
| 97 | +#endif |
| 98 | + |
| 99 | + gecko_flush(); |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
| 103 | + |
| 104 | +void gecko_close() { |
| 105 | +#ifndef __WIN32__ |
| 106 | + if (fd_gecko > 0) |
| 107 | + close(fd_gecko); |
| 108 | +#else |
| 109 | + CloseHandle(handle_gecko); |
| 110 | +#endif |
| 111 | +} |
| 112 | + |
| 113 | +void gecko_flush() { |
| 114 | +#ifndef __WIN32__ |
| 115 | + tcflush(fd_gecko, TCIOFLUSH); |
| 116 | +#else |
| 117 | + PurgeComm(handle_gecko, PURGE_RXCLEAR | PURGE_TXCLEAR | |
| 118 | + PURGE_TXABORT | PURGE_RXABORT); |
| 119 | + |
| 120 | +#endif |
| 121 | +} |
| 122 | + |
| 123 | +int gecko_read(void *buf, size_t count) { |
| 124 | + size_t left, chunk; |
| 125 | +#ifndef __WIN32__ |
| 126 | + size_t res; |
| 127 | +#else |
| 128 | + DWORD res; |
| 129 | +#endif |
| 130 | + |
| 131 | + left = count; |
| 132 | + while (left) { |
| 133 | + chunk = left; |
| 134 | + if (chunk > FTDI_PACKET_SIZE) |
| 135 | + chunk = FTDI_PACKET_SIZE; |
| 136 | + |
| 137 | +#ifndef __WIN32__ |
| 138 | + res = read(fd_gecko, buf, chunk); |
| 139 | + if (res < 1) { |
| 140 | + perror("gecko_read"); |
| 141 | + return 1; |
| 142 | + } |
| 143 | +#else |
| 144 | + if (!ReadFile(handle_gecko, buf, chunk, &res, NULL)) { |
| 145 | + fprintf(stderr, "gecko_read\n"); |
| 146 | + return 1; |
| 147 | + } |
| 148 | +#endif |
| 149 | + |
| 150 | + left -= res; |
| 151 | + buf += res; |
| 152 | + } |
| 153 | + |
| 154 | + return 0; |
| 155 | +} |
| 156 | + |
| 157 | +int gecko_write(const void *buf, size_t count) { |
| 158 | + size_t left, chunk; |
| 159 | +#ifndef __WIN32__ |
| 160 | + size_t res; |
| 161 | +#else |
| 162 | + DWORD res; |
| 163 | +#endif |
| 164 | + |
| 165 | + left = count; |
| 166 | + |
| 167 | + while (left) { |
| 168 | + chunk = left; |
| 169 | + if (chunk > FTDI_PACKET_SIZE) |
| 170 | + chunk = FTDI_PACKET_SIZE; |
| 171 | + |
| 172 | +#ifndef __WIN32__ |
| 173 | + res = write(fd_gecko, buf, count); |
| 174 | + if (res < 1) { |
| 175 | + perror("gecko_write"); |
| 176 | + return 1; |
| 177 | + } |
| 178 | +#else |
| 179 | + if (!WriteFile(handle_gecko, buf, chunk, &res, NULL)) { |
| 180 | + fprintf (stderr, "gecko_write\n"); |
| 181 | + return 1; |
| 182 | + } |
| 183 | +#endif |
| 184 | + |
| 185 | + left -= res; |
| 186 | + buf += res; |
| 187 | + |
| 188 | +#ifndef __WIN32__ |
| 189 | + // does this work with ftdi-sio? |
| 190 | + if (tcdrain(fd_gecko)) { |
| 191 | + perror ("gecko_drain"); |
| 192 | + return 1; |
| 193 | + } |
| 194 | +#endif |
| 195 | + } |
| 196 | + |
| 197 | + return 0; |
| 198 | +} |
| 199 | + |
0 commit comments