altosdroid: Auto tab changing
[fw/altos] / ao-tools / lib / cc-usb.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <stdarg.h>
23 #include <poll.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <termios.h>
29 #include "ccdbg-debug.h"
30 #include "cc-usb.h"
31
32
33 #define CC_NUM_HEX_READ         64
34 /*
35  * AltOS has different buffer sizes for in/out packets
36  */
37 #define CC_IN_BUF               65536
38 #define CC_OUT_BUF              64
39 #define DEFAULT_TTY             "/dev/ttyACM0"
40
41 struct cc_hex_read {
42         uint8_t *buf;
43         int     len;
44 };
45
46 struct cc_usb {
47         int                     fd;
48         uint8_t                 in_buf[CC_IN_BUF];
49         int                     in_pos;
50         int                     in_count;
51         uint8_t                 out_buf[CC_OUT_BUF];
52         int                     out_count;
53
54         struct cc_hex_read      hex_buf[CC_NUM_HEX_READ];
55         int                     hex_count;
56
57         int                     remote;
58 };
59
60 #define NOT_HEX 0xff
61
62 static uint8_t
63 cc_hex_nibble(uint8_t c)
64 {
65         if ('0' <= c && c <= '9')
66                 return c - '0';
67         if ('a' <= c && c <= 'f')
68                 return c - 'a' + 10;
69         if ('A' <= c && c <= 'F')
70                 return c - 'A' + 10;
71         return NOT_HEX;
72 }
73
74 /*
75  * Take raw input bytes, parse them as hex
76  * and write them to the waiting buffer
77  */
78 static void
79 cc_handle_hex_read(struct cc_usb *cc)
80 {
81         uint8_t h, l;
82         int     hex_pos;
83
84         hex_pos = 0;
85         while (hex_pos < cc->hex_count && cc->in_pos < cc->in_count) {
86                 /*
87                  * Skip to next hex character
88                  */
89                 while (cc->in_pos < cc->in_count &&
90                        cc_hex_nibble(cc->in_buf[cc->in_pos]) == NOT_HEX)
91                         cc->in_pos++;
92                 /*
93                  * Make sure we have two characters left
94                  */
95                 if (cc->in_count - cc->in_pos < 2)
96                         break;
97                 /*
98                  * Parse hex number
99                  */
100                 h = cc_hex_nibble(cc->in_buf[cc->in_pos]);
101                 l = cc_hex_nibble(cc->in_buf[cc->in_pos+1]);
102                 if (h == NOT_HEX || l == NOT_HEX) {
103                         fprintf(stderr, "hex read error\n");
104                         break;
105                 }
106                 cc->in_pos += 2;
107                 /*
108                  * Store hex number
109                  */
110                 *cc->hex_buf[hex_pos].buf++ = (h << 4) | l;
111                 if (--cc->hex_buf[hex_pos].len <= 0)
112                         hex_pos++;
113         }
114
115         /* Move pending hex reads to the start of the array */
116         if (hex_pos) {
117                 memmove(cc->hex_buf, cc->hex_buf + hex_pos,
118                         (cc->hex_count - hex_pos) * sizeof (cc->hex_buf[0]));
119                 cc->hex_count -= hex_pos;
120         }
121 }
122
123 static void
124 cc_usb_dbg(int indent, uint8_t *bytes, int len)
125 {
126         int     eol = 1;
127         int     i;
128         uint8_t c;
129         while (len--) {
130                 c = *bytes++;
131                 if (eol) {
132                         for (i = 0; i < indent; i++)
133                                 ccdbg_debug(CC_DEBUG_BITBANG, " ");
134                         eol = 0;
135                 }
136                 switch (c) {
137                 case '\r':
138                         ccdbg_debug(CC_DEBUG_BITBANG, "^M");
139                         break;
140                 case '\n':
141                         eol = 1;
142                 default:
143                         ccdbg_debug(CC_DEBUG_BITBANG, "%c", c);
144                 }
145         }
146 }
147
148 /*
149  * Flush pending writes, fill pending reads
150  */
151
152 static int
153 _cc_usb_sync(struct cc_usb *cc, int wait_for_input)
154 {
155         int             ret;
156         struct pollfd   fds;
157         int             timeout;
158
159         fds.fd = cc->fd;
160         for (;;) {
161                 if (cc->hex_count || cc->out_count)
162                         timeout = 5000;
163                 else if (wait_for_input && cc->in_pos == cc->in_count)
164                         timeout = wait_for_input;
165                 else
166                         timeout = 0;
167                 fds.events = 0;
168                 /* Move remaining bytes to the start of the input buffer */
169                 if (cc->in_pos) {
170                         memmove(cc->in_buf, cc->in_buf + cc->in_pos,
171                                 cc->in_count - cc->in_pos);
172                         cc->in_count -= cc->in_pos;
173                         cc->in_pos = 0;
174                 }
175                 if (cc->in_count < CC_IN_BUF)
176                         fds.events |= POLLIN;
177                 if (cc->out_count)
178                         fds.events |= POLLOUT;
179                 ret = poll(&fds, 1, timeout);
180                 if (ret == 0) {
181                         if (timeout)
182                                 return -1;
183                         break;
184                 }
185                 if (ret < 0) {
186                         perror("poll");
187                         return -1;
188                 }
189                 if (fds.revents & POLLIN) {
190                         ret = read(cc->fd, cc->in_buf + cc->in_count,
191                                    CC_IN_BUF - cc->in_count);
192                         if (ret > 0) {
193                                 cc_usb_dbg(24, cc->in_buf + cc->in_count, ret);
194                                 cc->in_count += ret;
195                                 if (cc->hex_count)
196                                         cc_handle_hex_read(cc);
197                         } else if (ret < 0)
198                                 perror("read");
199                 }
200                 if (fds.revents & POLLOUT) {
201                         ret = write(cc->fd, cc->out_buf,
202                                     cc->out_count);
203                         if (ret > 0) {
204                                 cc_usb_dbg(0, cc->out_buf, ret);
205                                 memmove(cc->out_buf,
206                                         cc->out_buf + ret,
207                                         cc->out_count - ret);
208                                 cc->out_count -= ret;
209                         } else if (ret < 0)
210                                 perror("write");
211                 }
212         }
213         return 0;
214 }
215
216 void
217 cc_usb_sync(struct cc_usb *cc)
218 {
219         if (_cc_usb_sync(cc, 0) < 0) {
220                 fprintf(stderr, "USB link timeout\n");
221                 exit(1);
222         }
223 }
224
225 void
226 cc_usb_printf(struct cc_usb *cc, char *format, ...)
227 {
228         char    buf[1024], *b;
229         va_list ap;
230         int     ret, this_time;
231
232         /* sprintf to a local buffer */
233         va_start(ap, format);
234         ret = vsnprintf(buf, sizeof(buf), format, ap);
235         va_end(ap);
236         if (ret > sizeof(buf)) {
237                 fprintf(stderr, "printf overflow for format %s\n",
238                         format);
239         }
240
241         /* flush local buffer to the wire */
242         b = buf;
243         while (ret > 0) {
244                 this_time = ret;
245                 if (this_time > CC_OUT_BUF - cc->out_count)
246                         this_time = CC_OUT_BUF - cc->out_count;
247                 memcpy(cc->out_buf + cc->out_count, b, this_time);
248                 cc->out_count += this_time;
249                 ret -= this_time;
250                 b += this_time;
251                 while (cc->out_count >= CC_OUT_BUF)
252                         cc_usb_sync(cc);
253         }
254 }
255
256 int
257 cc_usb_getchar(struct cc_usb *cc)
258 {
259         while (cc->in_pos == cc->in_count) {
260                 if (_cc_usb_sync(cc, 5000) < 0) {
261                         fprintf(stderr, "USB link timeout\n");
262                         exit(1);
263                 }
264         }
265         return cc->in_buf[cc->in_pos++];
266 }
267
268 void
269 cc_usb_getline(struct cc_usb *cc, char *line, int max)
270 {
271         int     c;
272
273         while ((c = cc_usb_getchar(cc)) != '\n') {
274                 switch (c) {
275                 case '\r':
276                         break;
277                 default:
278                         if (max > 1) {
279                                 *line++ = c;
280                                 max--;
281                         }
282                         break;
283                 }
284         }
285         *line++ = '\0';
286 }
287
288 int
289 cc_usb_send_bytes(struct cc_usb *cc, uint8_t *bytes, int len)
290 {
291         int     this_len;
292         int     ret = len;
293
294         while (len) {
295                 this_len = len;
296                 if (this_len > 8)
297                         this_len = 8;
298                 len -= this_len;
299                 cc_usb_printf(cc, "P");
300                 while (this_len--)
301                         cc_usb_printf (cc, " %02x", (*bytes++) & 0xff);
302                 cc_usb_printf(cc, "\n");
303         }
304         return ret;
305 }
306
307 void
308 cc_queue_read(struct cc_usb *cc, uint8_t *buf, int len)
309 {
310         struct cc_hex_read      *hex_buf;
311
312         /* At the start of a command sequence, flush any pending input */
313         if (cc->hex_count == 0) {
314                 cc_usb_sync(cc);
315                 cc->in_count = 0;
316         }
317         while (cc->hex_count >= CC_NUM_HEX_READ)
318                 cc_usb_sync(cc);
319         hex_buf = &cc->hex_buf[cc->hex_count++];
320         hex_buf->buf = buf;
321         hex_buf->len = len;
322 }
323
324 int
325 cc_usb_recv_bytes(struct cc_usb *cc, uint8_t *buf, int len)
326 {
327         cc_queue_read(cc, buf, len);
328         cc_usb_printf(cc, "G %x\n", len);
329         return len;
330 }
331
332 int
333 cc_usb_write_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
334 {
335         cc_usb_printf(cc, "O %x %x\n", len, addr);
336         while (len--)
337                 cc_usb_printf(cc, "%02x", *bytes++);
338         cc_usb_sync(cc);
339         return 0;
340 }
341
342 int
343 cc_usb_read_memory(struct cc_usb *cc, uint16_t addr, uint8_t *bytes, int len)
344 {
345         int     i;
346         cc_queue_read(cc, bytes, len);
347         cc_usb_printf(cc, "I %x %x\n", len, addr);
348         cc_usb_sync(cc);
349         for (i = 0; i < len; i++) {
350                 if ((i & 15) == 0) {
351                         if (i)
352                                 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
353                         ccdbg_debug(CC_DEBUG_MEMORY, "\t%04x", addr + i);
354                 }
355                 ccdbg_debug(CC_DEBUG_MEMORY, " %02x", bytes[i]);
356         }
357         ccdbg_debug(CC_DEBUG_MEMORY, "\n");
358         return 0;
359 }
360
361 int
362 cc_usb_debug_mode(struct cc_usb *cc)
363 {
364         cc_usb_sync(cc);
365         cc_usb_printf(cc, "D\n");
366         return 1;
367 }
368
369 int
370 cc_usb_reset(struct cc_usb *cc)
371 {
372         cc_usb_sync(cc);
373         cc_usb_printf(cc, "R\n");
374         return 1;
375 }
376
377 void
378 cc_usb_open_remote(struct cc_usb *cc, int channel)
379 {
380         if (!cc->remote) {
381                 printf ("channel %d\n", channel);
382                 cc_usb_printf(cc, "\nc r %d\np\nE 0\n", channel);
383                 do {
384                         cc->in_count = cc->in_pos = 0;
385                         _cc_usb_sync(cc, 100);
386                 } while (cc->in_count > 0);
387                 cc->remote = 1;
388         }
389 }
390
391 void
392 cc_usb_close_remote(struct cc_usb *cc)
393 {
394         if (cc->remote) {
395                 cc_usb_printf(cc, "~");
396                 cc->remote = 0;
397         }
398 }
399
400 static struct termios   save_termios;
401
402 struct cc_usb *
403 cc_usb_open(char *tty)
404 {
405         struct cc_usb   *cc;
406         struct termios  termios;
407
408         if (!tty)
409                 tty = DEFAULT_TTY;
410         cc = calloc (sizeof (struct cc_usb), 1);
411         if (!cc)
412                 return NULL;
413         cc->fd = open(tty, O_RDWR | O_NONBLOCK);
414         if (cc->fd < 0) {
415                 perror(tty);
416                 free (cc);
417                 return NULL;
418         }
419         tcgetattr(cc->fd, &termios);
420         save_termios = termios;
421         cfmakeraw(&termios);
422         tcsetattr(cc->fd, TCSAFLUSH, &termios);
423         cc_usb_printf(cc, "\nE 0\nm 0\n");
424         do {
425                 cc->in_count = cc->in_pos = 0;
426                 _cc_usb_sync(cc, 100);
427         } while (cc->in_count > 0);
428         return cc;
429 }
430
431 void
432 cc_usb_close(struct cc_usb *cc)
433 {
434         cc_usb_close_remote(cc);
435         cc_usb_sync(cc);
436         tcsetattr(cc->fd, TCSAFLUSH, &save_termios);
437         close (cc->fd);
438         free (cc);
439 }