9 #include <sys/select.h>
15 #include <linux/usbdevice_fs.h>
16 #include <linux/usb/ch9.h>
17 #include <linux/input.h>
20 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
30 /* return 1 if founf, 0 if not, < 0 on error */
31 static int find_device(struct teensy_dev *dev,
32 uint16_t vendor, uint16_t product)
34 const char path_root[] = "/dev/bus/usb";
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? */
43 char path_bus[256], path_dev[256];
44 struct dirent *ent, *ent_bus;
45 DIR *dir = NULL, *dir_bus = NULL;
51 memset(dev, 0xff, sizeof(*dev));
53 dir = opendir(path_root);
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)
64 snprintf(path_bus, sizeof(path_bus), "%s/%s",
65 path_root, ent->d_name);
67 dir_bus = opendir(path_bus);
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)
76 snprintf(path_dev, sizeof(path_dev), "%s/%s/%s",
77 path_root, ent->d_name, ent_bus->d_name);
79 fd = open(path_dev, O_RDWR);
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));
90 if (desc.d.bDescriptorType != USB_DT_DEVICE) {
91 fprintf(stderr, "%s: bad DT: 0x%02x\n",
92 path_dev, desc.d.bDescriptorType);
96 if (desc.d.idVendor == vendor && desc.d.idProduct == product)
113 if (desc.d.bNumConfigurations != 1) {
114 fprintf(stderr, "unexpected bNumConfigurations: %u\n",
115 desc.d.bNumConfigurations);
119 /* walk through all descriptors */
122 ret = read(fd, &desc.hdr, sizeof(desc.hdr));
125 if (ret != sizeof(desc.hdr)) {
126 fprintf(stderr, "desc.hdr read: %d/%zd: ", ret, sizeof(desc.hdr));
131 ret = (int)lseek(fd, -sizeof(desc.hdr), SEEK_CUR);
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);
144 switch (desc.hdr.bDescriptorType) {
146 if (desc.c.bNumInterfaces != 2) {
147 fprintf(stderr, "unexpected bNumInterfaces: %u\n",
148 desc.c.bNumInterfaces);
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);
162 if (desc.i.bNumEndpoints != 2) {
163 fprintf(stderr, "unexpected bNumEndpoints: %u\n",
164 desc.i.bNumEndpoints);
170 case USB_DT_ENDPOINT:
171 if (iface < 0 || iface >= ARRAY_SIZE(dev->ifaces)) {
172 fprintf(stderr, "bad iface: %d\n", iface);
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);
181 if (desc.e.bEndpointAddress & 0x80)
182 dev->ifaces[iface].ep_in = desc.e.bEndpointAddress; // & 0x7F;
184 dev->ifaces[iface].ep_out = desc.e.bEndpointAddress;
192 fprintf(stderr, "skipping desc 0x%02x\n",
193 desc.hdr.bDescriptorType);
198 /* claim interfaces */
199 for (iface = 0; iface < ARRAY_SIZE(dev->ifaces); iface++) {
200 struct usbdevfs_ioctl usbio;
202 if (dev->ifaces[iface].ep_in == -1) {
203 fprintf(stderr, "missing ep_in, iface: %d\n", iface);
206 if (dev->ifaces[iface].ep_out == -1) {
207 fprintf(stderr, "missing ep_out, iface: %d\n", iface);
211 /* disconnect default driver */
212 memset(&usbio, 0, sizeof(usbio));
214 usbio.ioctl_code = USBDEVFS_DISCONNECT;
215 ret = ioctl(fd, USBDEVFS_IOCTL, &usbio);
216 if (ret != 0 && errno != ENODATA)
217 perror("USBDEVFS_DISCONNECT");
219 ret = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &iface);
221 perror("USBDEVFS_CLAIMINTERFACE");
239 static int enable_echo(int enable)
241 const char *portname = "/dev/tty";
247 memset(&tty, 0, sizeof(tty));
249 fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
251 fprintf(stderr, "open %s: ", portname);
256 ret = tcgetattr(fd, &tty);
262 // printf("lflag: 0%o\n", tty.c_lflag);
264 tty.c_lflag |= ECHO | ICANON;
266 tty.c_lflag &= ~(ECHO | ICANON);
267 tty.c_cc[VMIN] = tty.c_cc[VTIME] = 0;
270 ret = tcsetattr(fd, TCSANOW, &tty);
285 static void signal_handler(int sig)
288 signal(sig, SIG_DFL);
291 /* ?0SA 00DU, ?1CB RLDU */
292 #define STATE_BYTES 2
294 static uint8_t fixed_input_state[STATE_BYTES] = { 0x33, 0x3f };
307 static const enum mdbtn evdev_md_map[KEY_CNT] = {
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,
315 [KEY_LEFTALT] = MDBTN_START,
318 int do_evdev_input(int fd)
320 uint8_t old_state[STATE_BYTES];
321 uint8_t changed_bits[STATE_BYTES] = { 0, };
322 struct input_event ev;
326 ret = read(fd, &ev, sizeof(ev));
327 if (ret != sizeof(ev)) {
328 fprintf(stderr, "evdev read %d/%zd: ", ret, sizeof(ev));
333 if (ev.type != EV_KEY)
336 if (ev.value != 0 && ev.value != 1)
339 if ((uint32_t)ev.code >= ARRAY_SIZE(evdev_md_map)) {
340 fprintf(stderr, "evdev read bad key: %u\n", ev.code);
344 mdbtn = evdev_md_map[ev.code];
348 memcpy(old_state, fixed_input_state, STATE_BYTES);
350 /* ?0SA 00DU, ?1CB RLDU */
353 changed_bits[0] = 0x01;
354 changed_bits[1] = 0x01;
357 changed_bits[0] = 0x02;
358 changed_bits[1] = 0x02;
361 changed_bits[0] = 0x00;
362 changed_bits[1] = 0x04;
365 changed_bits[0] = 0x00;
366 changed_bits[1] = 0x08;
369 changed_bits[0] = 0x10;
370 changed_bits[1] = 0x00;
373 changed_bits[0] = 0x00;
374 changed_bits[1] = 0x10;
377 changed_bits[0] = 0x00;
378 changed_bits[1] = 0x20;
381 changed_bits[0] = 0x20;
382 changed_bits[1] = 0x00;
388 for (i = 0; i < STATE_BYTES; i++)
389 fixed_input_state[i] &= ~changed_bits[i];
393 for (i = 0; i < STATE_BYTES; i++)
394 fixed_input_state[i] |= changed_bits[i];
397 return memcmp(old_state, fixed_input_state, STATE_BYTES) ? 1 : 0;
403 uint32_t rerecord_count;
411 static uint8_t *import_gmv(FILE *f, long size, int *frame_count)
418 if (size < (long)sizeof(*gmv)) {
419 fprintf(stderr, "bad gmv size: %ld\n", size);
425 fprintf(stderr, "OOM?\n");
428 ret = fread(gmv, 1, size, f);
430 fprintf(stderr, "fread %d/%ld: ", ret, size);
435 *frame_count = (size - sizeof(*gmv)) / sizeof(gmv->data[0]);
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);
443 if (strncmp(gmv->sig, "Gens Movie TEST", 15) != 0) {
444 fprintf(stderr, "bad GMV sig\n");
447 if (gmv->ctrl1 != '3') {
448 fprintf(stderr, "unhandled controlled config: '%c'\n", gmv->ctrl1);
451 if (gmv->ver >= 'A') {
452 if (gmv->flags & 0x40) {
453 fprintf(stderr, "unhandled flag: movie requires a savestate\n");
456 if (gmv->flags & 0x20) {
457 fprintf(stderr, "unhandled flag: 3-player movie\n");
460 if (gmv->flags & ~0x80) {
461 //fprintf(stderr, "unhandled flag(s): %04x\n", gmv->flags);
466 printf("loaded GMV: %s\n", gmv->name);
467 printf("%d frames, %u rerecords\n",
468 *frame_count, gmv->rerecord_count);
470 out = malloc(*frame_count * sizeof(out[0]));
472 fprintf(stderr, "OOM?\n");
476 for (i = 0; i < *frame_count; i++) {
477 out[i] = gmv->data[i][0];
479 if (gmv->data[i][1] != 0xff || gmv->data[i][2] != 0xff)
481 fprintf(stderr, "f %d: unhandled byte(s) %02x %02x\n",
482 i, gmv->data[i][1], gmv->data[i][2]);
489 static int do_bkm_char(char c, char expect, uint8_t *val, int bit)
498 fprintf(stderr, "unexpected bkm char: '%c' instead of '%c'\n",
503 static uint8_t *import_bkm(FILE *f, int *frame_count)
505 uint8_t *out = NULL, val;
514 while ((p = fgets(buf, sizeof(buf), f)) != NULL) {
521 if (p[30] != '\r' && p[30] != '\n')
525 if (count >= alloc) {
526 alloc = alloc * 2 + 64;
527 out = realloc(out, alloc * sizeof(out[0]));
529 fprintf(stderr, "OOM?\n");
536 if (strncmp(p, "|.|", 3) != 0)
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))
546 if (strcmp(p, "....|............||") != 0)
553 fprintf(stderr, "unhandled bkm line %d: '%s'\n", line, buf);
557 printf("loaded bkm, %d frames\n", count);
558 *frame_count = count;
562 static int write_bkm_frame(FILE *f, const uint8_t *data)
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 };
571 fprintf(stderr, "%s called without outfile\n", __func__);
575 idata[0] = 0x10000 | (data[0] << 8) | data[1];
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]);
590 static int tas_data_to_teensy(uint8_t b, uint8_t *data)
594 /* ?0SA 00DU, ?1CB RLDU */
595 data[0] = (b & 0x13) | ((b >> 2) & 0x20);
596 data[1] = (b & 0x0f) | ((b >> 1) & 0x30);
600 static int submit_urb(int fd, struct usbdevfs_urb *urb, int ep,
601 void *buf, size_t buf_size)
603 memset(urb, 0, sizeof(*urb));
604 urb->type = USBDEVFS_URB_TYPE_INTERRUPT;
607 urb->buffer_length = buf_size;
609 return ioctl(fd, USBDEVFS_SUBMITURB, urb);
619 static void missing_arg(int a)
621 fprintf(stderr, "missing arg: %d\n", a);
625 int main(int argc, char *argv[])
627 struct teensy_dev dev;
628 struct usbdevfs_urb urb[URB_CNT];
629 struct usbdevfs_urb *reaped_urb;
630 int fixed_input_changed;
632 int evdev_fd_cnt = 0;
635 int pending_urbs = 0;
637 const char *tasfn = NULL;
638 const char *outfn = NULL;
639 uint8_t *tas_data = NULL;
640 int use_vsync = 0; // frame increment on vsync
646 char buf_dbg[64 + 1];
647 struct tas_pkt pkt_in;
648 struct tas_pkt pkt_out;
649 struct timeval *timeout = NULL;
655 for (i = 1; i < argc; i++) {
656 if (argv[i][0] == '-') {
657 switch (argv[i][1] | (argv[i][2] << 8)) {
674 tas_skip = atoi(argv[i]);
680 fprintf(stderr, "bad arg: %s\n", argv[i]);
685 /* remaining args are evdev filenames */
686 if (evdev_fd_cnt >= ARRAY_SIZE(evdev_fds)) {
687 fprintf(stderr, "too many evdevs\n");
690 fd = open(argv[i], O_RDONLY);
692 fprintf(stderr, "open %s: ", argv[i]);
697 ret = ioctl(fd, EVIOCGBIT(0, sizeof(evdev_support)),
701 if (!(evdev_support & (1 << EV_KEY))) {
702 fprintf(stderr, "%s doesn't have keys\n", argv[i]);
706 evdev_fds[evdev_fd_cnt++] = fd;
714 f = fopen(tasfn, "rb");
716 fprintf(stderr, "fopen %s: ", tasfn);
721 fseek(f, 0, SEEK_END);
723 fseek(f, 0, SEEK_SET);
725 fprintf(stderr, "bad size: %ld\n", size);
729 ext = strrchr(tasfn, '.');
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);
740 fprintf(stderr, "unknown movie type: '%s'\n", ext);
745 if (tas_data == NULL) {
746 fprintf(stderr, "failed fo parse %s\n", tasfn);
751 if (tas_skip >= frame_count || tas_skip <= -frame_count) {
752 printf("skip out of range: %d/%d\n", tas_skip, frame_count);
756 frame_count -= tas_skip;
757 memmove(&tas_data[0], &tas_data[tas_skip],
758 sizeof(tas_data[0]) * frame_count);
761 tas_data = realloc(tas_data,
762 (frame_count - tas_skip) * sizeof(tas_data[0]));
763 if (tas_data == NULL) {
764 fprintf(stderr, "OOM?\n");
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);
770 frame_count -= tas_skip;
776 outf = fopen(outfn, "w");
778 fprintf(stderr, "fopen %s: ", tasfn);
785 signal(SIGINT, signal_handler);
789 while (!g_exit || (pending_urbs & (1 << URB_DATA_OUT)))
792 ret = find_device(&dev, 0x16C0, 0x0486);
798 printf("waiting for device..\n");
810 /* we wait first, then send commands, but if teensy
811 * is started already, it won't send anything */
817 if (!(pending_urbs & (1 << URB_DATA_IN))) {
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));
822 perror("USBDEVFS_SUBMITURB URB_DATA_IN");
826 pending_urbs |= 1 << URB_DATA_IN;
828 if (!(pending_urbs & (1 << URB_DBG_IN))) {
829 ret = submit_urb(dev.fd, &urb[URB_DBG_IN], dev.ifaces[1].ep_in,
830 buf_dbg, sizeof(buf_dbg) - 1);
832 perror("USBDEVFS_SUBMITURB URB_DBG_IN");
836 pending_urbs |= 1 << URB_DBG_IN;
840 FD_SET(STDIN_FILENO, &rfds);
841 for (i = 0; i < evdev_fd_cnt; i++)
842 FD_SET(evdev_fds[i], &rfds);
845 FD_SET(dev.fd, &wfds);
847 ret = select(dev.fd + 1, &rfds, &wfds, NULL, timeout);
854 /* sometihng form stdin? */
855 if (FD_ISSET(STDIN_FILENO, &rfds)) {
857 ret = read(STDIN_FILENO, &c, 1);
859 perror("read stdin");
870 /* something from input devices? */
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]);
879 /* something from USB? */
880 if (FD_ISSET(dev.fd, &wfds))
882 unsigned int which_urb;
885 ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb);
889 perror("USBDEVFS_REAPURB");
892 which_urb = reaped_urb - urb;
893 if (which_urb < ARRAY_SIZE(urb))
894 pending_urbs &= ~(1 << which_urb);
896 fprintf(stderr, "reaped unknown urb: %p #%u",
897 reaped_urb, which_urb);
900 if (reaped_urb != NULL && reaped_urb->status != 0) {
901 errno = -reaped_urb->status;
902 fprintf(stderr, "urb #%u: ", which_urb);
904 if (reaped_urb->status == -EILSEQ) {
905 /* this is usually a sign of disconnect.. */
910 else if (reaped_urb == &urb[URB_DATA_IN])
912 /* some request from teensy */
913 switch (pkt_in.type) {
915 printf("req: %d/%d/%d\n", pkt_in.req.frame,
916 frames_sent, frame_count);
919 if (frames_sent < frame_count) {
920 pkt_out.type = PKT_STREAM_DATA_TO;
922 for (i = 0; i < sizeof(pkt_out.data); ) {
923 i += tas_data_to_teensy(tas_data[frames_sent],
927 if (frames_sent >= frame_count)
933 pkt_out.type = PKT_STREAM_END;
935 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT],
936 dev.ifaces[0].ep_out, &pkt_out, sizeof(pkt_out));
938 perror("USBDEVFS_SUBMITURB PKT_STREAM_DATA_TO");
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);
947 for (i = 0; i < pkt_in.size; ) {
948 i += write_bkm_frame(outf, pkt_in.data + i);
954 printf("host: got unknown pkt type: %04x\n", pkt_in.type);
958 else if (reaped_urb == &urb[URB_DATA_OUT])
961 else if (reaped_urb == &urb[URB_DBG_IN])
964 buf_dbg[reaped_urb->actual_length] = 0;
965 printf("%s", buf_dbg);
968 fprintf(stderr, "reaped unknown urb? %p #%zu\n",
969 reaped_urb, reaped_urb - urb);
973 /* something to send? */
974 if (pending_urbs & (1 << URB_DATA_OUT))
978 if ((tas_data != NULL || outf != NULL) && !enable_sent) {
979 memset(&pkt_out, 0, sizeof(pkt_out));
980 pkt_out.type = PKT_STREAM_ENABLE;
981 pkt_out.enable.stream_to = (tas_data != NULL);
982 pkt_out.enable.stream_from = (outf != NULL);
983 pkt_out.enable.use_readinc = !use_vsync;
985 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
986 &pkt_out, sizeof(pkt_out));
988 perror("USBDEVFS_SUBMITURB PKT_STREAM_ENABLE");
991 pending_urbs |= 1 << URB_DATA_OUT;
996 if (tas_data == NULL && fixed_input_changed) {
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));
1001 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
1002 &pkt_out, sizeof(pkt_out));
1004 perror("USBDEVFS_SUBMITURB PKT_FIXED_STATE");
1007 pending_urbs |= 1 << URB_DATA_OUT;
1010 if (g_exit && !abort_sent) {
1011 memset(&pkt_out, 0, sizeof(pkt_out));
1012 pkt_out.type = PKT_STREAM_ABORT;
1014 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
1015 &pkt_out, sizeof(pkt_out));
1017 perror("USBDEVFS_SUBMITURB PKT_STREAM_ABORT");
1020 pending_urbs |= 1 << URB_DATA_OUT;
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);
1047 perror("USBDEVFS_REAPURB");
1057 // vim: ts=2:sw=2:expandtab