some recording support, abort cmd on exit
[megadrive.git] / host / main.c
CommitLineData
932302ef 1#include <stdio.h>
61172125 2#include <stdlib.h>
932302ef 3#include <string.h>
4#include <stdint.h>
932302ef 5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <sys/ioctl.h>
9#include <sys/select.h>
10#include <unistd.h>
11#include <dirent.h>
f359b935 12#include <signal.h>
13#include <termios.h>
932302ef 14#include <errno.h>
15#include <linux/usbdevice_fs.h>
16#include <linux/usb/ch9.h>
1cb2822f 17#include <linux/input.h>
f20de073 18#include "../pkts.h"
932302ef 19
20#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
21
932302ef 22struct teensy_dev {
23 int fd;
24 struct {
25 int ep_in;
26 int ep_out;
27 } ifaces[2];
28};
29
30/* return 1 if founf, 0 if not, < 0 on error */
31static int find_device(struct teensy_dev *dev,
32 uint16_t vendor, uint16_t product)
33{
34 const char path_root[] = "/dev/bus/usb";
35 union {
36 struct usb_descriptor_header hdr;
37 struct usb_device_descriptor d;
38 struct usb_config_descriptor c;
39 struct usb_interface_descriptor i;
40 struct usb_endpoint_descriptor e;
41 char space[0x100]; /* enough? */
42 } desc;
43 char path_bus[256], path_dev[256];
44 struct dirent *ent, *ent_bus;
45 DIR *dir = NULL, *dir_bus = NULL;
46 int num, fd = -1;
47 int iface = -1;
48 int retval = -1;
49 int ret;
50
51 memset(dev, 0xff, sizeof(*dev));
52
53 dir = opendir(path_root);
54 if (dir == NULL) {
55 perror("opendir");
56 return -1;
57 }
58
59 for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
60 /* should be a number like 000 */
61 if (sscanf(ent->d_name, "%03d", &num) != 1)
62 continue;
63
64 snprintf(path_bus, sizeof(path_bus), "%s/%s",
65 path_root, ent->d_name);
66
67 dir_bus = opendir(path_bus);
68 if (dir_bus == NULL)
69 continue;
70
71 ent_bus = readdir(dir_bus);
72 for (; ent_bus != NULL; ent_bus = readdir(dir_bus)) {
73 if (sscanf(ent->d_name, "%03d", &num) != 1)
74 continue;
75
76 snprintf(path_dev, sizeof(path_dev), "%s/%s/%s",
77 path_root, ent->d_name, ent_bus->d_name);
78
79 fd = open(path_dev, O_RDWR);
80 if (fd == -1)
81 continue;
82
83 ret = read(fd, &desc.d, sizeof(desc.d));
84 if (ret != sizeof(desc.d)) {
85 fprintf(stderr, "desc read: %d/%zd: ", ret, sizeof(desc.d));
86 perror("");
87 goto next;
88 }
89
90 if (desc.d.bDescriptorType != USB_DT_DEVICE) {
91 fprintf(stderr, "%s: bad DT: 0x%02x\n",
92 path_dev, desc.d.bDescriptorType);
93 goto next;
94 }
95
96 if (desc.d.idVendor == vendor && desc.d.idProduct == product)
97 goto found;
98
99next:
100 close(fd);
101 fd = -1;
102 }
103
104 closedir(dir_bus);
105 dir_bus = NULL;
106 }
107
108 /* not found */
109 retval = 0;
110 goto out;
111
112found:
113 if (desc.d.bNumConfigurations != 1) {
114 fprintf(stderr, "unexpected bNumConfigurations: %u\n",
115 desc.d.bNumConfigurations);
116 goto out;
117 }
118
119 /* walk through all descriptors */
120 while (1)
121 {
122 ret = read(fd, &desc.hdr, sizeof(desc.hdr));
123 if (ret == 0)
124 break;
125 if (ret != sizeof(desc.hdr)) {
126 fprintf(stderr, "desc.hdr read: %d/%zd: ", ret, sizeof(desc.hdr));
127 perror("");
128 break;
129 }
130
131 ret = (int)lseek(fd, -sizeof(desc.hdr), SEEK_CUR);
132 if (ret == -1) {
133 perror("lseek");
134 break;
135 }
136
137 ret = read(fd, &desc, desc.hdr.bLength);
138 if (ret != desc.hdr.bLength) {
139 fprintf(stderr, "desc read: %d/%u: ", ret, desc.hdr.bLength);
140 perror("");
141 break;
142 }
143
144 switch (desc.hdr.bDescriptorType) {
145 case USB_DT_CONFIG:
146 if (desc.c.bNumInterfaces != 2) {
147 fprintf(stderr, "unexpected bNumInterfaces: %u\n",
148 desc.c.bNumInterfaces);
149 goto out;
150 }
151 break;
152
153 case USB_DT_INTERFACE:
154 if (desc.i.bInterfaceClass != USB_CLASS_HID
155 || desc.i.bInterfaceSubClass != 0
156 || desc.i.bInterfaceProtocol != 0) {
157 fprintf(stderr, "unexpected interface %x:%x:%x\n",
158 desc.i.bInterfaceClass, desc.i.bInterfaceSubClass,
159 desc.i.bInterfaceProtocol);
160 goto out;
161 }
162 if (desc.i.bNumEndpoints != 2) {
163 fprintf(stderr, "unexpected bNumEndpoints: %u\n",
164 desc.i.bNumEndpoints);
165 goto out;
166 }
167 iface++;
168 break;
169
170 case USB_DT_ENDPOINT:
171 if (iface < 0 || iface >= ARRAY_SIZE(dev->ifaces)) {
172 fprintf(stderr, "bad iface: %d\n", iface);
173 goto out;
174 }
175 if (desc.e.wMaxPacketSize != 64 && desc.e.wMaxPacketSize != 32) {
176 fprintf(stderr, "iface %d, EP %02x: "
177 "unexpected wMaxPacketSize: %u\n",
178 iface, desc.e.bEndpointAddress, desc.e.wMaxPacketSize);
179 goto out;
180 }
181 if (desc.e.bEndpointAddress & 0x80)
182 dev->ifaces[iface].ep_in = desc.e.bEndpointAddress; // & 0x7F;
183 else
184 dev->ifaces[iface].ep_out = desc.e.bEndpointAddress;
185 break;
186
187 case 0x21:
188 /* ignore */
189 break;
190
191 default:
192 fprintf(stderr, "skipping desc 0x%02x\n",
193 desc.hdr.bDescriptorType);
194 break;
195 }
196 }
197
198 /* claim interfaces */
199 for (iface = 0; iface < ARRAY_SIZE(dev->ifaces); iface++) {
200 struct usbdevfs_ioctl usbio;
201
202 if (dev->ifaces[iface].ep_in == -1) {
203 fprintf(stderr, "missing ep_in, iface: %d\n", iface);
204 goto out;
205 }
206 if (dev->ifaces[iface].ep_out == -1) {
207 fprintf(stderr, "missing ep_out, iface: %d\n", iface);
208 goto out;
209 }
210
211 /* disconnect default driver */
212 memset(&usbio, 0, sizeof(usbio));
213 usbio.ifno = iface;
214 usbio.ioctl_code = USBDEVFS_DISCONNECT;
215 ret = ioctl(fd, USBDEVFS_IOCTL, &usbio);
216 if (ret != 0 && errno != ENODATA)
217 perror("USBDEVFS_DISCONNECT");
218
219 ret = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &iface);
220 if (ret != 0)
221 perror("USBDEVFS_CLAIMINTERFACE");
222 }
223
224 dev->fd = fd;
225 fd = -1;
226 retval = 1;
227
228out:
229 if (fd != -1)
230 close(fd);
231 if (dir_bus != NULL)
232 closedir(dir_bus);
233 if (dir != NULL)
234 closedir(dir);
235
236 return retval;
237}
238
f359b935 239static int enable_echo(int enable)
240{
241 const char *portname = "/dev/tty";
242 struct termios tty;
243 int retval = -1;
244 int ret;
245 int fd;
246
247 memset(&tty, 0, sizeof(tty));
248
249 fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
250 if (fd < 0) {
251 fprintf(stderr, "open %s: ", portname);
252 perror("");
253 return 1;
254 }
255
256 ret = tcgetattr(fd, &tty);
257 if (ret != 0) {
258 perror("tcgetattr");
259 goto out;
260 }
261
262 // printf("lflag: 0%o\n", tty.c_lflag);
263 if (enable)
de3f807e 264 tty.c_lflag |= ECHO | ICANON;
265 else {
266 tty.c_lflag &= ~(ECHO | ICANON);
267 tty.c_cc[VMIN] = tty.c_cc[VTIME] = 0;
268 }
f359b935 269
270 ret = tcsetattr(fd, TCSANOW, &tty);
271 if (ret != 0) {
272 perror("tcsetattr");
273 goto out;
274 }
275
276 retval = 0;
277out:
278 close(fd);
279
280 return retval;
281}
282
96b9855a 283static int g_exit;
284
f359b935 285static void signal_handler(int sig)
286{
96b9855a 287 g_exit = 1;
f359b935 288 signal(sig, SIG_DFL);
f359b935 289}
290
1cb2822f 291/* ?0SA 00DU, ?1CB RLDU */
292#define STATE_BYTES 2
293
294static uint8_t fixed_input_state[STATE_BYTES] = { 0x33, 0x3f };
295
296enum mdbtn {
297 MDBTN_UP = 1,
298 MDBTN_DOWN,
299 MDBTN_LEFT,
300 MDBTN_RIGHT,
301 MDBTN_A,
302 MDBTN_B,
303 MDBTN_C,
304 MDBTN_START,
305};
306
307static const enum mdbtn evdev_md_map[KEY_CNT] = {
308 [KEY_UP] = MDBTN_UP,
309 [KEY_DOWN] = MDBTN_DOWN,
310 [KEY_LEFT] = MDBTN_LEFT,
311 [KEY_RIGHT] = MDBTN_RIGHT,
312 [KEY_HOME] = MDBTN_A,
313 [KEY_PAGEDOWN] = MDBTN_B,
314 [KEY_END] = MDBTN_C,
315 [KEY_LEFTALT] = MDBTN_START,
316};
317
318int do_evdev_input(int fd)
319{
320 uint8_t old_state[STATE_BYTES];
321 uint8_t changed_bits[STATE_BYTES] = { 0, };
322 struct input_event ev;
323 enum mdbtn mdbtn;
324 int i, ret;
325
326 ret = read(fd, &ev, sizeof(ev));
327 if (ret != sizeof(ev)) {
328 fprintf(stderr, "evdev read %d/%zd: ", ret, sizeof(ev));
329 perror("");
330 return 0;
331 }
332
333 if (ev.type != EV_KEY)
334 return 0;
335
336 if (ev.value != 0 && ev.value != 1)
337 return 0;
338
339 if ((uint32_t)ev.code >= ARRAY_SIZE(evdev_md_map)) {
340 fprintf(stderr, "evdev read bad key: %u\n", ev.code);
341 return 0;
342 }
343
344 mdbtn = evdev_md_map[ev.code];
345 if (mdbtn == 0)
346 return 0;
347
348 memcpy(old_state, fixed_input_state, STATE_BYTES);
349
350 /* ?0SA 00DU, ?1CB RLDU */
351 switch (mdbtn) {
352 case MDBTN_UP:
353 changed_bits[0] = 0x01;
354 changed_bits[1] = 0x01;
355 break;
356 case MDBTN_DOWN:
357 changed_bits[0] = 0x02;
358 changed_bits[1] = 0x02;
359 break;
360 case MDBTN_LEFT:
361 changed_bits[0] = 0x00;
362 changed_bits[1] = 0x04;
363 break;
364 case MDBTN_RIGHT:
365 changed_bits[0] = 0x00;
366 changed_bits[1] = 0x08;
367 break;
368 case MDBTN_A:
369 changed_bits[0] = 0x10;
370 changed_bits[1] = 0x00;
371 break;
372 case MDBTN_B:
373 changed_bits[0] = 0x00;
374 changed_bits[1] = 0x10;
375 break;
376 case MDBTN_C:
377 changed_bits[0] = 0x00;
378 changed_bits[1] = 0x20;
379 break;
380 case MDBTN_START:
381 changed_bits[0] = 0x20;
382 changed_bits[1] = 0x00;
383 break;
384 }
385
386 if (ev.value) {
387 // key press
388 for (i = 0; i < STATE_BYTES; i++)
389 fixed_input_state[i] &= ~changed_bits[i];
390 }
391 else {
392 // key release
393 for (i = 0; i < STATE_BYTES; i++)
394 fixed_input_state[i] |= changed_bits[i];
395 }
396
397 return memcmp(old_state, fixed_input_state, STATE_BYTES) ? 1 : 0;
398}
399
f20de073 400struct gmv_tas {
401 char sig[15];
402 char ver;
403 uint32_t rerecord_count;
404 char ctrl1;
405 char ctrl2;
406 uint16_t flags;
407 char name[40];
408 uint8_t data[0][3];
409};
410
01f5cc9e 411static uint8_t *import_gmv(FILE *f, long size, int *frame_count)
412{
413 struct gmv_tas *gmv;
414 uint8_t *out;
415 int ret;
416 int i;
417
418 if (size < (long)sizeof(*gmv)) {
419 fprintf(stderr, "bad gmv size: %ld\n", size);
420 return NULL;
421 }
422
423 gmv = malloc(size);
424 if (gmv == NULL) {
425 fprintf(stderr, "OOM?\n");
426 return NULL;
427 }
428 ret = fread(gmv, 1, size, f);
429 if (ret != size) {
430 fprintf(stderr, "fread %d/%ld: ", ret, size);
431 perror("");
432 return NULL;
433 }
434
435 *frame_count = (size - sizeof(*gmv)) / sizeof(gmv->data[0]);
436
437 /* check the GMV.. */
438 if (*frame_count <= 0 || size != sizeof(*gmv) + *frame_count * 3) {
439 fprintf(stderr, "broken gmv? frames=%d\n", *frame_count);
440 return NULL;
441 }
442
443 if (strncmp(gmv->sig, "Gens Movie TEST", 15) != 0) {
444 fprintf(stderr, "bad GMV sig\n");
445 return NULL;
446 }
447 if (gmv->ctrl1 != '3') {
448 fprintf(stderr, "unhandled controlled config: '%c'\n", gmv->ctrl1);
449 //return NULL;
450 }
451 if (gmv->ver >= 'A') {
452 if (gmv->flags & 0x40) {
453 fprintf(stderr, "unhandled flag: movie requires a savestate\n");
454 return NULL;
455 }
456 if (gmv->flags & 0x20) {
457 fprintf(stderr, "unhandled flag: 3-player movie\n");
458 return NULL;
459 }
460 if (gmv->flags & ~0x80) {
461 //fprintf(stderr, "unhandled flag(s): %04x\n", gmv->flags);
462 //return 1;
463 }
464 }
465 gmv->name[39] = 0;
466 printf("loaded GMV: %s\n", gmv->name);
467 printf("%d frames, %u rerecords\n",
468 *frame_count, gmv->rerecord_count);
469
470 out = malloc(*frame_count * sizeof(out[0]));
471 if (out == NULL) {
472 fprintf(stderr, "OOM?\n");
473 return NULL;
474 }
475
476 for (i = 0; i < *frame_count; i++) {
477 out[i] = gmv->data[i][0];
478
479 if (gmv->data[i][1] != 0xff || gmv->data[i][2] != 0xff)
480 {
481 fprintf(stderr, "f %d: unhandled byte(s) %02x %02x\n",
482 i, gmv->data[i][1], gmv->data[i][2]);
483 }
484 }
485
486 return out;
487}
488
489static int do_bkm_char(char c, char expect, uint8_t *val, int bit)
490{
491 if (c == expect) {
492 *val &= ~(1 << bit);
493 return 0;
494 }
495 if (c == '.')
496 return 0;
497
498 fprintf(stderr, "unexpected bkm char: '%c' instead of '%c'\n",
499 c, expect);
500 return 1;
501}
502
503static uint8_t *import_bkm(FILE *f, int *frame_count)
504{
505 uint8_t *out = NULL, val;
506 int count = 0;
507 int alloc = 0;
508 int line = 0;
509 char buf[256];
510 const char *r;
511 char *p;
512 int i;
513
514 while ((p = fgets(buf, sizeof(buf), f)) != NULL) {
515 line++;
516 if (p[0] != '|')
517 continue;
518
519 if (strlen(p) < 30)
520 goto unhandled_line;
521 if (p[30] != '\r' && p[30] != '\n')
522 goto unhandled_line;
523 p[30] = 0;
524
525 if (count >= alloc) {
526 alloc = alloc * 2 + 64;
527 out = realloc(out, alloc * sizeof(out[0]));
528 if (out == NULL) {
529 fprintf(stderr, "OOM?\n");
530 return NULL;
531 }
532 }
533
534 val = 0xff;
535
536 if (strncmp(p, "|.|", 3) != 0)
537 goto unhandled_line;
538 p += 3;
539
540 const char ref[] = "UDLRABCS";
541 for (r = ref, i = 0; *r != 0; p++, r++, i++) {
542 if (do_bkm_char(*p, *r, &val, i))
543 goto unhandled_line;
544 }
545
546 if (strcmp(p, "....|............||") != 0)
547 goto unhandled_line;
548
549 out[count++] = val;
550 continue;
551
552unhandled_line:
553 fprintf(stderr, "unhandled bkm line %d: '%s'\n", line, buf);
554 return NULL;
555 }
556
557 printf("loaded bkm, %d frames\n", count);
558 *frame_count = count;
559 return out;
560}
561
e8e66d02 562static int write_bkm_frame(FILE *f, const uint8_t *data)
563{
564 /* ?0SA 00DU, ?1CB RLDU */
565 static const char ref[] = "UDLRABCSXYZM";
566 static const char bits[] = { 0,1,2,3, 12,4,5,13, 16,16,16,16 };
567 uint32_t idata[2];
568 int p, i;
569
570 if (f == NULL) {
571 fprintf(stderr, "%s called without outfile\n", __func__);
572 goto out;
573 }
574
575 idata[0] = 0x10000 | (data[0] << 8) | data[1];
576 idata[1] = ~0;
577
578 fprintf(f, "|.|");
579 for (p = 0; p < 2; p++) {
580 for (i = 0; i < 12; i++)
581 fprintf(f, "%c", (idata[p] & (1 << bits[i])) ? '.' : ref[i]);
582 fprintf(f, "|");
583 }
584 fprintf(f, "|\n");
585
586out:
587 return 2;
588}
589
590static int tas_data_to_teensy(uint8_t b, uint8_t *data)
591{
592 /* SCBA RLDU */
593 /* v */
594 /* ?0SA 00DU, ?1CB RLDU */
595 data[0] = (b & 0x13) | ((b >> 2) & 0x20);
596 data[1] = (b & 0x0f) | ((b >> 1) & 0x30);
597 return 2;
598}
599
f20de073 600static int submit_urb(int fd, struct usbdevfs_urb *urb, int ep,
601 void *buf, size_t buf_size)
602{
603 memset(urb, 0, sizeof(*urb));
604 urb->type = USBDEVFS_URB_TYPE_INTERRUPT;
605 urb->endpoint = ep;
606 urb->buffer = buf;
607 urb->buffer_length = buf_size;
608
609 return ioctl(fd, USBDEVFS_SUBMITURB, urb);
610}
611
932302ef 612enum my_urbs {
613 URB_DATA_IN,
614 URB_DATA_OUT,
615 URB_DBG_IN,
616 URB_CNT
617};
618
61172125 619static void missing_arg(int a)
620{
621 fprintf(stderr, "missing arg: %d\n", a);
622 exit(1);
623}
624
932302ef 625int main(int argc, char *argv[])
626{
627 struct teensy_dev dev;
628 struct usbdevfs_urb urb[URB_CNT];
629 struct usbdevfs_urb *reaped_urb;
1cb2822f 630 int fixed_input_changed;
631 int evdev_fds[16];
632 int evdev_fd_cnt = 0;
633 int evdev_support;
932302ef 634 int wait_device = 0;
96b9855a 635 int pending_urbs = 0;
1cb2822f 636 fd_set rfds, wfds;
61172125 637 const char *tasfn = NULL;
e8e66d02 638 const char *outfn = NULL;
01f5cc9e 639 uint8_t *tas_data = NULL;
4af6d4e5 640 int use_readinc = 0; // frame increment on read
61172125 641 int tas_skip = 0;
f20de073 642 int enable_sent = 0;
e8e66d02 643 int abort_sent = 0;
f20de073 644 int frame_count = 0;
645 int frames_sent = 0;
646 char buf_dbg[64 + 1];
647 struct tas_pkt pkt_in;
648 struct tas_pkt pkt_out;
649 struct timeval *timeout = NULL;
650 struct timeval tout;
e8e66d02 651 FILE *outf = NULL;
96b9855a 652 int i, ret = -1;
1cb2822f 653 int fd;
654
655 for (i = 1; i < argc; i++) {
f20de073 656 if (argv[i][0] == '-') {
61172125 657 switch (argv[i][1] | (argv[i][2] << 8)) {
658 case 'm':
659 i++;
660 if (argv[i] == NULL)
661 missing_arg(i);
662 tasfn = argv[i];
663 continue;
e8e66d02 664 case 'w':
665 i++;
666 if (argv[i] == NULL)
667 missing_arg(i);
668 outfn = argv[i];
669 continue;
61172125 670 case 's':
671 i++;
672 if (argv[i] == NULL)
673 missing_arg(i);
674 tas_skip = atoi(argv[i]);
675 continue;
4af6d4e5 676 case 'r':
677 use_readinc = 1;
678 continue;
61172125 679 default:
f20de073 680 fprintf(stderr, "bad arg: %s\n", argv[i]);
681 return 1;
682 }
f20de073 683 }
e8e66d02 684
685 /* remaining args are evdev filenames */
1cb2822f 686 if (evdev_fd_cnt >= ARRAY_SIZE(evdev_fds)) {
687 fprintf(stderr, "too many evdevs\n");
688 break;
689 }
f20de073 690 fd = open(argv[i], O_RDONLY);
1cb2822f 691 if (fd == -1) {
692 fprintf(stderr, "open %s: ", argv[i]);
693 perror("");
694 continue;
695 }
696 evdev_support = 0;
f20de073 697 ret = ioctl(fd, EVIOCGBIT(0, sizeof(evdev_support)),
1cb2822f 698 &evdev_support);
699 if (ret < 0)
700 perror("EVIOCGBIT");
701 if (!(evdev_support & (1 << EV_KEY))) {
702 fprintf(stderr, "%s doesn't have keys\n", argv[i]);
703 close(fd);
704 continue;
705 }
706 evdev_fds[evdev_fd_cnt++] = fd;
707 }
932302ef 708
f20de073 709 if (tasfn != NULL) {
01f5cc9e 710 const char *ext;
f20de073 711 long size;
712 FILE *f;
01f5cc9e 713
f20de073 714 f = fopen(tasfn, "rb");
715 if (f == NULL) {
716 fprintf(stderr, "fopen %s: ", tasfn);
717 perror("");
718 return 1;
719 }
720
721 fseek(f, 0, SEEK_END);
722 size = ftell(f);
723 fseek(f, 0, SEEK_SET);
01f5cc9e 724 if (size <= 0) {
725 fprintf(stderr, "bad size: %ld\n", size);
f20de073 726 return 1;
727 }
01f5cc9e 728
729 ext = strrchr(tasfn, '.');
730 if (ext == NULL)
731 ext = tasfn;
732 else
733 ext++;
734
735 if (strcasecmp(ext, "gmv") == 0)
736 tas_data = import_gmv(f, size, &frame_count);
737 else if (strcasecmp(ext, "bkm") == 0)
738 tas_data = import_bkm(f, &frame_count);
739 else {
740 fprintf(stderr, "unknown movie type: '%s'\n", ext);
f20de073 741 return 1;
742 }
743 fclose(f);
f20de073 744
01f5cc9e 745 if (tas_data == NULL) {
746 fprintf(stderr, "failed fo parse %s\n", tasfn);
f20de073 747 return 1;
748 }
749
61172125 750 if (tas_skip != 0) {
751 if (tas_skip >= frame_count || tas_skip <= -frame_count) {
752 printf("skip out of range: %d/%d\n", tas_skip, frame_count);
753 return 1;
754 }
755 if (tas_skip > 0) {
756 frame_count -= tas_skip;
01f5cc9e 757 memmove(&tas_data[0], &tas_data[tas_skip],
758 sizeof(tas_data[0]) * frame_count);
61172125 759 }
760 else {
01f5cc9e 761 tas_data = realloc(tas_data,
762 (frame_count - tas_skip) * sizeof(tas_data[0]));
763 if (tas_data == NULL) {
61172125 764 fprintf(stderr, "OOM?\n");
765 return 1;
766 }
01f5cc9e 767 memmove(&tas_data[-tas_skip], &tas_data[0],
768 sizeof(tas_data[0]) * frame_count);
769 memset(&tas_data[0], 0xff, sizeof(tas_data[0]) * -tas_skip);
61172125 770 frame_count -= tas_skip;
771 }
772 }
f20de073 773 }
774
e8e66d02 775 if (outfn != NULL) {
776 outf = fopen(outfn, "w");
777 if (outf == NULL) {
778 fprintf(stderr, "fopen %s: ", tasfn);
779 perror("");
780 return 1;
781 }
782 }
783
f359b935 784 enable_echo(0);
785 signal(SIGINT, signal_handler);
786
932302ef 787 dev.fd = -1;
788
e8e66d02 789 while (!g_exit || (pending_urbs & (1 << URB_DATA_OUT)))
932302ef 790 {
791 if (dev.fd == -1) {
792 ret = find_device(&dev, 0x16C0, 0x0486);
793 if (ret < 0)
f359b935 794 break;
932302ef 795
796 if (ret == 0) {
797 if (!wait_device) {
798 printf("waiting for device..\n");
799 wait_device = 1;
800 }
801 usleep(250000);
802 continue;
803 }
804
805 wait_device = 0;
96b9855a 806 pending_urbs = 0;
f20de073 807 enable_sent = 0;
808 frames_sent = 0;
809
810 /* we wait first, then send commands, but if teensy
811 * is started already, it won't send anything */
812 tout.tv_sec = 1;
813 tout.tv_usec = 0;
814 timeout = &tout;
932302ef 815 }
816
96b9855a 817 if (!(pending_urbs & (1 << URB_DATA_IN))) {
f20de073 818 memset(&pkt_in, 0, sizeof(pkt_in));
819 ret = submit_urb(dev.fd, &urb[URB_DATA_IN], dev.ifaces[0].ep_in,
820 &pkt_in, sizeof(pkt_in));
932302ef 821 if (ret != 0) {
822 perror("USBDEVFS_SUBMITURB URB_DATA_IN");
f359b935 823 break;
932302ef 824 }
f20de073 825
96b9855a 826 pending_urbs |= 1 << URB_DATA_IN;
932302ef 827 }
96b9855a 828 if (!(pending_urbs & (1 << URB_DBG_IN))) {
f20de073 829 ret = submit_urb(dev.fd, &urb[URB_DBG_IN], dev.ifaces[1].ep_in,
830 buf_dbg, sizeof(buf_dbg) - 1);
932302ef 831 if (ret != 0) {
832 perror("USBDEVFS_SUBMITURB URB_DBG_IN");
f359b935 833 break;
932302ef 834 }
f20de073 835
96b9855a 836 pending_urbs |= 1 << URB_DBG_IN;
932302ef 837 }
838
1cb2822f 839 FD_ZERO(&rfds);
de3f807e 840 FD_SET(STDIN_FILENO, &rfds);
1cb2822f 841 for (i = 0; i < evdev_fd_cnt; i++)
842 FD_SET(evdev_fds[i], &rfds);
843
932302ef 844 FD_ZERO(&wfds);
845 FD_SET(dev.fd, &wfds);
846
f20de073 847 ret = select(dev.fd + 1, &rfds, &wfds, NULL, timeout);
932302ef 848 if (ret < 0) {
849 perror("select");
f359b935 850 break;
932302ef 851 }
f20de073 852 timeout = NULL;
932302ef 853
de3f807e 854 /* sometihng form stdin? */
de3f807e 855 if (FD_ISSET(STDIN_FILENO, &rfds)) {
856 char c = 0;
857 ret = read(STDIN_FILENO, &c, 1);
858 if (ret <= 0) {
859 perror("read stdin");
860 break;
861 }
862
863 switch (c) {
864 case 'r':
865 enable_sent = 0;
866 break;
867 }
868 }
869
e8e66d02 870 /* something from input devices? */
1cb2822f 871 fixed_input_changed = 0;
872 for (i = 0; i < evdev_fd_cnt; i++) {
873 if (FD_ISSET(evdev_fds[i], &rfds)) {
874 fixed_input_changed |=
875 do_evdev_input(evdev_fds[i]);
876 }
877 }
878
879 /* something from USB? */
f20de073 880 if (FD_ISSET(dev.fd, &wfds))
881 {
96b9855a 882 unsigned int which_urb;
883
932302ef 884 reaped_urb = NULL;
885 ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb);
886 if (ret != 0) {
887 if (errno == ENODEV)
888 goto dev_close;
889 perror("USBDEVFS_REAPURB");
f359b935 890 break;
932302ef 891 }
96b9855a 892 which_urb = reaped_urb - urb;
893 if (which_urb < ARRAY_SIZE(urb))
894 pending_urbs &= ~(1 << which_urb);
895 else {
896 fprintf(stderr, "reaped unknown urb: %p #%u",
897 reaped_urb, which_urb);
898 }
932302ef 899
900 if (reaped_urb != NULL && reaped_urb->status != 0) {
901 errno = -reaped_urb->status;
96b9855a 902 fprintf(stderr, "urb #%u: ", which_urb);
f20de073 903 perror("");
932302ef 904 if (reaped_urb->status == -EILSEQ) {
905 /* this is usually a sign of disconnect.. */
906 usleep(250000);
907 goto dev_close;
908 }
909 }
96b9855a 910 else if (reaped_urb == &urb[URB_DATA_IN])
911 {
f20de073 912 /* some request from teensy */
f20de073 913 switch (pkt_in.type) {
914 case PKT_STREAM_REQ:
96b9855a 915 printf("req: %d/%d/%d\n", pkt_in.req.frame,
4af6d4e5 916 frames_sent, frame_count);
f20de073 917
e8e66d02 918 pkt_out.size = 0;
f20de073 919 if (frames_sent < frame_count) {
e8e66d02 920 pkt_out.type = PKT_STREAM_DATA_TO;
921
922 for (i = 0; i < sizeof(pkt_out.data); ) {
923 i += tas_data_to_teensy(tas_data[frames_sent],
924 pkt_out.data + i);
925
f20de073 926 frames_sent++;
e8e66d02 927 if (frames_sent >= frame_count)
928 break;
f20de073 929 }
e8e66d02 930 pkt_out.size = i;
f20de073 931 }
932 else
933 pkt_out.type = PKT_STREAM_END;
934
935 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT],
936 dev.ifaces[0].ep_out, &pkt_out, sizeof(pkt_out));
937 if (ret != 0)
e8e66d02 938 perror("USBDEVFS_SUBMITURB PKT_STREAM_DATA_TO");
939 break;
940
941 case PKT_STREAM_DATA_FROM:
942 printf("f: %d\n", frame_count);
943 if (pkt_in.size == 0 || pkt_in.size > sizeof(pkt_out.data)) {
944 printf("host: got bad DATA_FROM size: %u\n", pkt_in.size);
945 break;
946 }
947 for (i = 0; i < pkt_in.size; ) {
948 i += write_bkm_frame(outf, pkt_in.data + i);
949 frame_count++;
950 }
f20de073 951 break;
952
953 default:
954 printf("host: got unknown pkt type: %04x\n", pkt_in.type);
955 break;
956 }
932302ef 957 }
96b9855a 958 else if (reaped_urb == &urb[URB_DATA_OUT])
959 {
1cb2822f 960 }
96b9855a 961 else if (reaped_urb == &urb[URB_DBG_IN])
962 {
932302ef 963 /* debug text */
964 buf_dbg[reaped_urb->actual_length] = 0;
965 printf("%s", buf_dbg);
932302ef 966 }
967 else {
f20de073 968 fprintf(stderr, "reaped unknown urb? %p #%zu\n",
969 reaped_urb, reaped_urb - urb);
932302ef 970 }
971 }
1cb2822f 972
973 /* something to send? */
96b9855a 974 if (pending_urbs & (1 << URB_DATA_OUT))
975 // can't do that yet
976 continue;
977
e8e66d02 978 if ((tas_data != NULL || outf != NULL) && !enable_sent) {
f20de073 979 memset(&pkt_out, 0, sizeof(pkt_out));
980 pkt_out.type = PKT_STREAM_ENABLE;
e8e66d02 981 pkt_out.enable.stream_to = (tas_data != NULL);
982 pkt_out.enable.stream_from = (outf != NULL);
983 pkt_out.enable.use_readinc = use_readinc;
4af6d4e5 984
f20de073 985 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
986 &pkt_out, sizeof(pkt_out));
987 if (ret != 0) {
988 perror("USBDEVFS_SUBMITURB PKT_STREAM_ENABLE");
989 continue;
990 }
e8e66d02 991 pending_urbs |= 1 << URB_DATA_OUT;
f20de073 992 enable_sent = 1;
de3f807e 993 frames_sent = 0;
e8e66d02 994 continue;
f20de073 995 }
01f5cc9e 996 if (tas_data == NULL && fixed_input_changed) {
f20de073 997 memset(&pkt_out, 0, sizeof(pkt_out));
998 pkt_out.type = PKT_FIXED_STATE;
999 memcpy(pkt_out.data, fixed_input_state, sizeof(fixed_input_state));
1cb2822f 1000
f20de073 1001 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
1002 &pkt_out, sizeof(pkt_out));
1cb2822f 1003 if (ret != 0) {
e8e66d02 1004 perror("USBDEVFS_SUBMITURB PKT_FIXED_STATE");
1005 break;
1006 }
1007 pending_urbs |= 1 << URB_DATA_OUT;
1008 continue;
1009 }
1010 if (g_exit && !abort_sent) {
1011 memset(&pkt_out, 0, sizeof(pkt_out));
1012 pkt_out.type = PKT_STREAM_ABORT;
1013
1014 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
1015 &pkt_out, sizeof(pkt_out));
1016 if (ret != 0) {
1017 perror("USBDEVFS_SUBMITURB PKT_STREAM_ABORT");
f359b935 1018 break;
1cb2822f 1019 }
96b9855a 1020 pending_urbs |= 1 << URB_DATA_OUT;
e8e66d02 1021 abort_sent = 1;
96b9855a 1022 continue;
1cb2822f 1023 }
1024
932302ef 1025 continue;
1026
1027dev_close:
1028 close(dev.fd);
1029 dev.fd = -1;
1030 }
1031
f359b935 1032 enable_echo(1);
1033
e8e66d02 1034 if (outf != NULL)
1035 fclose(outf);
1036
96b9855a 1037 if (dev.fd != -1) {
1038 /* deal with pending URBs */
1039 if (pending_urbs & (1 << URB_DATA_IN))
1040 ioctl(dev.fd, USBDEVFS_DISCARDURB, &urb[URB_DATA_IN]);
1041 if (pending_urbs & (1 << URB_DBG_IN))
1042 ioctl(dev.fd, USBDEVFS_DISCARDURB, &urb[URB_DBG_IN]);
1043 for (i = 0; i < URB_CNT; i++) {
1044 if (pending_urbs & (1 << i)) {
1045 ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb);
1046 if (ret != 0)
1047 perror("USBDEVFS_REAPURB");
1048 }
1049 }
1050
1051 close(dev.fd);
1052 }
1053
f359b935 1054 return ret;
932302ef 1055}
1056
1057// vim: ts=2:sw=2:expandtab