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