Initial Commit
[Projects.git] / pkgbuilds / wiifuse / src / wiifuse-0.2.0 / gecko.c
blob9c590c265a04a4960a0da482c764b89b777d16c8
1 /*
2 * Copyright (C) 2008 dhewg, #wiidev efnet
4 * this file is part of wiifuse
5 * http://wiibrew.org/index.php?title=Wiifuse
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.
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.
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
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <termios.h>
29 #include "gecko.h"
31 #define FTDI_PACKET_SIZE 3968
33 // TODO
34 // this whole approach blocks, how do i implement a timeout with tty's?
36 static int fd_gecko = -1;
38 int gecko_open (const char *dev) {
39 struct termios newtio;
41 fd_gecko = open (dev, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK);
43 if (fd_gecko == -1) {
44 perror ("gecko_open");
45 return 1;
48 if (fcntl (fd_gecko, F_SETFL, 0)) {
49 perror ("F_SETFL on serial port");
50 return 1;
53 if (tcgetattr(fd_gecko, &newtio)) {
54 perror ("tcgetattr");
55 return 1;
58 cfmakeraw (&newtio);
60 newtio.c_cflag |= CRTSCTS | CS8 | CLOCAL | CREAD;
62 if (tcsetattr (fd_gecko, TCSANOW, &newtio)) {
63 perror ("tcsetattr");
64 return 1;
67 gecko_flush ();
69 return 0;
72 void gecko_close () {
73 if (fd_gecko > 0)
74 close (fd_gecko);
77 void gecko_flush () {
78 // TODO doesnt seem to work with ftdi-sio
79 // i need a way to check if data is actually available
80 tcflush (fd_gecko, TCIOFLUSH);
83 int gecko_read (void *buf, size_t count) {
84 size_t left, chunk, res;
86 left = count;
87 while (left) {
88 chunk = left;
89 if (chunk > FTDI_PACKET_SIZE)
90 chunk = FTDI_PACKET_SIZE;
92 res = read (fd_gecko, buf, chunk);
93 if (res < 1) {
94 perror ("gecko_read");
95 return 1;
98 left -= res;
99 buf += res;
102 return 0;
105 int gecko_write (const void *buf, size_t count) {
106 size_t left, chunk, res;
108 left = count;
110 while (left) {
111 chunk = left;
112 if (chunk > FTDI_PACKET_SIZE)
113 chunk = FTDI_PACKET_SIZE;
115 res = write (fd_gecko, buf, count);
116 if (res < 1) {
117 perror ("gecko_write");
118 return 1;
121 left -= res;
122 buf += res;
124 // does this work with ftdi-sio?
125 if (tcdrain (fd_gecko)) {
126 perror ("gecko_drain");
127 return 1;
132 return 0;