X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=host%2Fmain.c;h=f8f74c90e453cae0f24c4963164ac1140e54c273;hb=e4d23548386133037c0f9ccc02e56facd718d2ce;hp=4036c539dd9f9717ecead5af461548a13ea9d316;hpb=de3f807e329bc3e814bce5dac37982062be770c0;p=teensytas.git diff --git a/host/main.c b/host/main.c index 4036c53..f8f74c9 100644 --- a/host/main.c +++ b/host/main.c @@ -280,11 +280,12 @@ out: return retval; } +static int g_exit; + static void signal_handler(int sig) { - enable_echo(1); + g_exit = 1; signal(sig, SIG_DFL); - raise(sig); } /* ?0SA 00DU, ?1CB RLDU */ @@ -558,6 +559,53 @@ unhandled_line: return out; } +static int write_bkm_frame(FILE *f, const uint8_t *data) +{ + /* ?0SA 00DU, ?1CB RLDU */ + static const char ref[] = "UDLRABCSXYZM"; + static const char bits[] = { 0,1,2,3, 12,4,5,13, 16,16,16,16 }; + uint32_t idata[2]; + int p, i; + + if (f == NULL) { + fprintf(stderr, "%s called without outfile\n", __func__); + goto out; + } + + idata[0] = 0x10000 | (data[0] << 8) | data[1]; + idata[1] = ~0; + + fprintf(f, "|.|"); + for (p = 0; p < 2; p++) { + for (i = 0; i < 12; i++) + fprintf(f, "%c", (idata[p] & (1 << bits[i])) ? '.' : ref[i]); + fprintf(f, "|"); + } + fprintf(f, "|\n"); + +out: + return 2; +} + +static int tas_data_to_teensy(uint8_t b, uint8_t *data, FILE *logf) +{ + uint8_t t; + + /* SCBA RLDU */ + /* v */ + /* ?0SA 00DU, ?1CB RLDU */ + data[0] = (b & 0x13) | ((b >> 2) & 0x20); + data[1] = (b & 0x0f) | ((b >> 1) & 0x30); + + if (logf != NULL) { + fwrite(&data[0], 1, 1, logf); + t = data[1] | 0x40; // expected TH + fwrite(&t, 1, 1, logf); + } + + return 2; +} + static int submit_urb(int fd, struct usbdevfs_urb *urb, int ep, void *buf, size_t buf_size) { @@ -593,14 +641,16 @@ int main(int argc, char *argv[]) int evdev_fd_cnt = 0; int evdev_support; int wait_device = 0; - int dbg_in_sent = 0; - int data_in_sent = 0; + int pending_urbs = 0; fd_set rfds, wfds; const char *tasfn = NULL; + const char *outfn = NULL; + const char *logfn = NULL; uint8_t *tas_data = NULL; - int use_readinc = 0; // frame increment on read + int use_vsync = 0; // frame increment on vsync int tas_skip = 0; int enable_sent = 0; + int abort_sent = 0; int frame_count = 0; int frames_sent = 0; char buf_dbg[64 + 1]; @@ -608,7 +658,9 @@ int main(int argc, char *argv[]) struct tas_pkt pkt_out; struct timeval *timeout = NULL; struct timeval tout; - int i, ret; + FILE *outf = NULL; + FILE *logf = NULL; + int i, ret = -1; int fd; for (i = 1; i < argc; i++) { @@ -620,20 +672,34 @@ int main(int argc, char *argv[]) missing_arg(i); tasfn = argv[i]; continue; + case 'w': + i++; + if (argv[i] == NULL) + missing_arg(i); + outfn = argv[i]; + continue; + case 'l': + i++; + if (argv[i] == NULL) + missing_arg(i); + logfn = argv[i]; + continue; case 's': i++; if (argv[i] == NULL) missing_arg(i); tas_skip = atoi(argv[i]); continue; - case 'r': - use_readinc = 1; + case 'v': + use_vsync = 1; continue; default: fprintf(stderr, "bad arg: %s\n", argv[i]); return 1; } } + + /* remaining args are evdev filenames */ if (evdev_fd_cnt >= ARRAY_SIZE(evdev_fds)) { fprintf(stderr, "too many evdevs\n"); break; @@ -723,12 +789,30 @@ int main(int argc, char *argv[]) } } + if (outfn != NULL) { + outf = fopen(outfn, "w"); + if (outf == NULL) { + fprintf(stderr, "fopen %s: ", outfn); + perror(""); + return 1; + } + } + + if (logfn != NULL) { + logf = fopen(logfn, "wb"); + if (logf == NULL) { + fprintf(stderr, "fopen %s: ", logfn); + perror(""); + return 1; + } + } + enable_echo(0); signal(SIGINT, signal_handler); dev.fd = -1; - while (1) + while (!g_exit || (pending_urbs & (1 << URB_DATA_OUT))) { if (dev.fd == -1) { ret = find_device(&dev, 0x16C0, 0x0486); @@ -745,8 +829,7 @@ int main(int argc, char *argv[]) } wait_device = 0; - data_in_sent = 0; - dbg_in_sent = 0; + pending_urbs = 0; enable_sent = 0; frames_sent = 0; @@ -757,7 +840,7 @@ int main(int argc, char *argv[]) timeout = &tout; } - if (!data_in_sent) { + if (!(pending_urbs & (1 << URB_DATA_IN))) { memset(&pkt_in, 0, sizeof(pkt_in)); ret = submit_urb(dev.fd, &urb[URB_DATA_IN], dev.ifaces[0].ep_in, &pkt_in, sizeof(pkt_in)); @@ -766,9 +849,9 @@ int main(int argc, char *argv[]) break; } - data_in_sent = 1; + pending_urbs |= 1 << URB_DATA_IN; } - if (!dbg_in_sent) { + if (!(pending_urbs & (1 << URB_DBG_IN))) { ret = submit_urb(dev.fd, &urb[URB_DBG_IN], dev.ifaces[1].ep_in, buf_dbg, sizeof(buf_dbg) - 1); if (ret != 0) { @@ -776,7 +859,7 @@ int main(int argc, char *argv[]) break; } - dbg_in_sent = 1; + pending_urbs |= 1 << URB_DBG_IN; } FD_ZERO(&rfds); @@ -795,7 +878,6 @@ int main(int argc, char *argv[]) timeout = NULL; /* sometihng form stdin? */ - /* something from input devices? */ if (FD_ISSET(STDIN_FILENO, &rfds)) { char c = 0; ret = read(STDIN_FILENO, &c, 1); @@ -807,10 +889,13 @@ int main(int argc, char *argv[]) switch (c) { case 'r': enable_sent = 0; + if (logf != NULL) + rewind(logf); break; } } + /* something from input devices? */ fixed_input_changed = 0; for (i = 0; i < evdev_fd_cnt; i++) { if (FD_ISSET(evdev_fds[i], &rfds)) { @@ -822,6 +907,8 @@ int main(int argc, char *argv[]) /* something from USB? */ if (FD_ISSET(dev.fd, &wfds)) { + unsigned int which_urb; + reaped_urb = NULL; ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb); if (ret != 0) { @@ -830,13 +917,17 @@ int main(int argc, char *argv[]) perror("USBDEVFS_REAPURB"); break; } + which_urb = reaped_urb - urb; + if (which_urb < ARRAY_SIZE(urb)) + pending_urbs &= ~(1 << which_urb); + else { + fprintf(stderr, "reaped unknown urb: %p #%u", + reaped_urb, which_urb); + } if (reaped_urb != NULL && reaped_urb->status != 0) { errno = -reaped_urb->status; - if ((unsigned long)(reaped_urb - urb) < ARRAY_SIZE(urb)) - fprintf(stderr, "urb #%zu: ", reaped_urb - urb); - else - fprintf(stderr, "unknown urb: "); + fprintf(stderr, "urb #%u: ", which_urb); perror(""); if (reaped_urb->status == -EILSEQ) { /* this is usually a sign of disconnect.. */ @@ -844,60 +935,71 @@ int main(int argc, char *argv[]) goto dev_close; } } - - if (reaped_urb == &urb[URB_DATA_IN]) { + else if (reaped_urb == &urb[URB_DATA_IN]) + { /* some request from teensy */ - int count; - uint8_t b; - switch (pkt_in.type) { case PKT_STREAM_REQ: - printf("%d/%d/%d\n", pkt_in.req.frame, + printf("req: %d/%d/%d\n", pkt_in.req.frame, frames_sent, frame_count); - for (i = 0; i < sizeof(pkt_out.data); i++) { - pkt_out.data[i * 2 + 0] = 0x33; - pkt_out.data[i * 2 + 1] = 0x3f; - } + pkt_out.size = 0; if (frames_sent < frame_count) { - pkt_out.type = PKT_STREAM_DATA; - - count = frame_count - frames_sent; - if (count > sizeof(pkt_out.data) / 2) - count = sizeof(pkt_out.data) / 2; - for (i = 0; i < count; i++) { - /* SCBA RLDU */ - b = tas_data[frames_sent]; - - /* ?0SA 00DU, ?1CB RLDU */ - pkt_out.data[i * 2 + 0] = (b & 0x13) | ((b >> 2) & 0x20); - pkt_out.data[i * 2 + 1] = (b & 0x0f) | ((b >> 1) & 0x30); + pkt_out.type = PKT_STREAM_DATA_TO; + + for (i = 0; i < sizeof(pkt_out.data); ) { + i += tas_data_to_teensy(tas_data[frames_sent], + pkt_out.data + i, logf); + frames_sent++; + if (frames_sent >= frame_count) + break; } + pkt_out.size = i; } - else + else { pkt_out.type = PKT_STREAM_END; + if (logf != NULL) + fflush(logf); + } ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out, &pkt_out, sizeof(pkt_out)); if (ret != 0) - perror("USBDEVFS_SUBMITURB URB_DATA_OUT PKT_STREAM_DATA"); + perror("USBDEVFS_SUBMITURB PKT_STREAM_DATA_TO"); + break; + + case PKT_STREAM_DATA_FROM: + printf("f: %d\n", frame_count); + if (pkt_in.size == 0 || pkt_in.size > sizeof(pkt_out.data)) { + printf("host: got bad DATA_FROM size: %u\n", pkt_in.size); + break; + } + for (i = 0; i < pkt_in.size; ) { + i += write_bkm_frame(outf, pkt_in.data + i); + frame_count++; + } break; default: printf("host: got unknown pkt type: %04x\n", pkt_in.type); break; } - - data_in_sent = 0; } - else if (reaped_urb == &urb[URB_DATA_OUT]) { + else if (reaped_urb == &urb[URB_DATA_OUT]) + { } - else if (reaped_urb == &urb[URB_DBG_IN]) { + else if (reaped_urb == &urb[URB_DBG_IN]) + { /* debug text */ buf_dbg[reaped_urb->actual_length] = 0; printf("%s", buf_dbg); - dbg_in_sent = 0; + + // continue receiving debug before sending out stuff + tout.tv_sec = 0; + tout.tv_usec = 1000; + timeout = &tout; + continue; } else { fprintf(stderr, "reaped unknown urb? %p #%zu\n", @@ -906,10 +1008,16 @@ int main(int argc, char *argv[]) } /* something to send? */ - if (tas_data != NULL && !enable_sent) { + if (pending_urbs & (1 << URB_DATA_OUT)) + // can't do that yet + continue; + + if ((tas_data != NULL || outf != NULL) && !enable_sent) { memset(&pkt_out, 0, sizeof(pkt_out)); pkt_out.type = PKT_STREAM_ENABLE; - pkt_out.start.use_readinc = use_readinc; + pkt_out.enable.stream_to = (tas_data != NULL); + pkt_out.enable.stream_from = (outf != NULL); + pkt_out.enable.use_readinc = !use_vsync; ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out, &pkt_out, sizeof(pkt_out)); @@ -917,8 +1025,10 @@ int main(int argc, char *argv[]) perror("USBDEVFS_SUBMITURB PKT_STREAM_ENABLE"); continue; } + pending_urbs |= 1 << URB_DATA_OUT; enable_sent = 1; frames_sent = 0; + continue; } if (tas_data == NULL && fixed_input_changed) { memset(&pkt_out, 0, sizeof(pkt_out)); @@ -928,9 +1038,25 @@ int main(int argc, char *argv[]) ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out, &pkt_out, sizeof(pkt_out)); if (ret != 0) { - perror("USBDEVFS_SUBMITURB URB_DATA_OUT"); + perror("USBDEVFS_SUBMITURB PKT_FIXED_STATE"); + break; + } + pending_urbs |= 1 << URB_DATA_OUT; + continue; + } + if (g_exit && !abort_sent) { + memset(&pkt_out, 0, sizeof(pkt_out)); + pkt_out.type = PKT_STREAM_ABORT; + + ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out, + &pkt_out, sizeof(pkt_out)); + if (ret != 0) { + perror("USBDEVFS_SUBMITURB PKT_STREAM_ABORT"); break; } + pending_urbs |= 1 << URB_DATA_OUT; + abort_sent = 1; + continue; } continue; @@ -942,6 +1068,28 @@ dev_close: enable_echo(1); + if (outf != NULL) + fclose(outf); + if (logf != NULL) + fclose(logf); + + if (dev.fd != -1) { + /* deal with pending URBs */ + if (pending_urbs & (1 << URB_DATA_IN)) + ioctl(dev.fd, USBDEVFS_DISCARDURB, &urb[URB_DATA_IN]); + if (pending_urbs & (1 << URB_DBG_IN)) + ioctl(dev.fd, USBDEVFS_DISCARDURB, &urb[URB_DBG_IN]); + for (i = 0; i < URB_CNT; i++) { + if (pending_urbs & (1 << i)) { + ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb); + if (ret != 0) + perror("USBDEVFS_REAPURB"); + } + } + + close(dev.fd); + } + return ret; }