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);
266 tty.c_lflag &= ~ECHO;
268 ret = tcsetattr(fd, TCSANOW, &tty);
281 static void signal_handler(int sig)
284 signal(sig, SIG_DFL);
288 /* ?0SA 00DU, ?1CB RLDU */
289 #define STATE_BYTES 2
291 static uint8_t fixed_input_state[STATE_BYTES] = { 0x33, 0x3f };
304 static const enum mdbtn evdev_md_map[KEY_CNT] = {
306 [KEY_DOWN] = MDBTN_DOWN,
307 [KEY_LEFT] = MDBTN_LEFT,
308 [KEY_RIGHT] = MDBTN_RIGHT,
309 [KEY_HOME] = MDBTN_A,
310 [KEY_PAGEDOWN] = MDBTN_B,
312 [KEY_LEFTALT] = MDBTN_START,
315 int do_evdev_input(int fd)
317 uint8_t old_state[STATE_BYTES];
318 uint8_t changed_bits[STATE_BYTES] = { 0, };
319 struct input_event ev;
323 ret = read(fd, &ev, sizeof(ev));
324 if (ret != sizeof(ev)) {
325 fprintf(stderr, "evdev read %d/%zd: ", ret, sizeof(ev));
330 if (ev.type != EV_KEY)
333 if (ev.value != 0 && ev.value != 1)
336 if ((uint32_t)ev.code >= ARRAY_SIZE(evdev_md_map)) {
337 fprintf(stderr, "evdev read bad key: %u\n", ev.code);
341 mdbtn = evdev_md_map[ev.code];
345 memcpy(old_state, fixed_input_state, STATE_BYTES);
347 /* ?0SA 00DU, ?1CB RLDU */
350 changed_bits[0] = 0x01;
351 changed_bits[1] = 0x01;
354 changed_bits[0] = 0x02;
355 changed_bits[1] = 0x02;
358 changed_bits[0] = 0x00;
359 changed_bits[1] = 0x04;
362 changed_bits[0] = 0x00;
363 changed_bits[1] = 0x08;
366 changed_bits[0] = 0x10;
367 changed_bits[1] = 0x00;
370 changed_bits[0] = 0x00;
371 changed_bits[1] = 0x10;
374 changed_bits[0] = 0x00;
375 changed_bits[1] = 0x20;
378 changed_bits[0] = 0x20;
379 changed_bits[1] = 0x00;
385 for (i = 0; i < STATE_BYTES; i++)
386 fixed_input_state[i] &= ~changed_bits[i];
390 for (i = 0; i < STATE_BYTES; i++)
391 fixed_input_state[i] |= changed_bits[i];
394 return memcmp(old_state, fixed_input_state, STATE_BYTES) ? 1 : 0;
400 uint32_t rerecord_count;
408 static int submit_urb(int fd, struct usbdevfs_urb *urb, int ep,
409 void *buf, size_t buf_size)
411 memset(urb, 0, sizeof(*urb));
412 urb->type = USBDEVFS_URB_TYPE_INTERRUPT;
415 urb->buffer_length = buf_size;
417 return ioctl(fd, USBDEVFS_SUBMITURB, urb);
427 static void missing_arg(int a)
429 fprintf(stderr, "missing arg: %d\n", a);
433 int main(int argc, char *argv[])
435 struct teensy_dev dev;
436 struct usbdevfs_urb urb[URB_CNT];
437 struct usbdevfs_urb *reaped_urb;
438 int fixed_input_changed;
440 int evdev_fd_cnt = 0;
444 int data_in_sent = 0;
446 struct gmv_tas *gmv = NULL;
447 const char *tasfn = NULL;
452 char buf_dbg[64 + 1];
453 struct tas_pkt pkt_in;
454 struct tas_pkt pkt_out;
455 struct timeval *timeout = NULL;
460 for (i = 1; i < argc; i++) {
461 if (argv[i][0] == '-') {
462 switch (argv[i][1] | (argv[i][2] << 8)) {
473 tas_skip = atoi(argv[i]);
476 fprintf(stderr, "bad arg: %s\n", argv[i]);
480 if (evdev_fd_cnt >= ARRAY_SIZE(evdev_fds)) {
481 fprintf(stderr, "too many evdevs\n");
484 fd = open(argv[i], O_RDONLY);
486 fprintf(stderr, "open %s: ", argv[i]);
491 ret = ioctl(fd, EVIOCGBIT(0, sizeof(evdev_support)),
495 if (!(evdev_support & (1 << EV_KEY))) {
496 fprintf(stderr, "%s doesn't have keys\n", argv[i]);
500 evdev_fds[evdev_fd_cnt++] = fd;
507 f = fopen(tasfn, "rb");
509 fprintf(stderr, "fopen %s: ", tasfn);
514 fseek(f, 0, SEEK_END);
516 fseek(f, 0, SEEK_SET);
517 if (size < (long)sizeof(*gmv)) {
518 fprintf(stderr, "bad gmv size: %ld\n", size);
523 fprintf(stderr, "OOM?\n");
526 ret = fread(gmv, 1, size, f);
528 fprintf(stderr, "fread %d/%ld: ", ret, size);
533 frame_count = (size - sizeof(*gmv)) / sizeof(gmv->data[0]);
535 /* check the GMV.. */
536 if (frame_count <= 0 || size != sizeof(*gmv) + frame_count * 3) {
537 fprintf(stderr, "broken gmv? frames=%d\n", frame_count);
541 if (strncmp(gmv->sig, "Gens Movie TEST", 15) != 0) {
542 fprintf(stderr, "bad GMV sig\n");
545 if (gmv->ctrl1 != '3') {
546 fprintf(stderr, "unhandled controlled config: '%c'\n", gmv->ctrl1);
549 if (gmv->ver >= 'A') {
550 if (gmv->flags & 0x40) {
551 fprintf(stderr, "unhandled flag: movie requires a savestate\n");
554 if (gmv->flags & 0x20) {
555 fprintf(stderr, "unhandled flag: 3-player movie\n");
558 if (gmv->flags & ~0x80) {
559 fprintf(stderr, "unhandled flag(s): %04x\n", gmv->flags);
564 printf("loaded GMV: %s\n", gmv->name);
565 printf("%d frames, %u rerecords\n",
566 frame_count, gmv->rerecord_count);
569 if (tas_skip >= frame_count || tas_skip <= -frame_count) {
570 printf("skip out of range: %d/%d\n", tas_skip, frame_count);
574 frame_count -= tas_skip;
575 memmove(&gmv->data[0], &gmv->data[tas_skip],
576 sizeof(gmv->data[0]) * frame_count);
579 gmv = realloc(gmv, sizeof(*gmv)
580 + (frame_count - tas_skip) * sizeof(gmv->data[0]));
582 fprintf(stderr, "OOM?\n");
585 memmove(&gmv->data[-tas_skip], &gmv->data[0],
586 sizeof(gmv->data[0]) * frame_count);
587 memset(&gmv->data[0], 0xff, sizeof(gmv->data[0]) * -tas_skip);
588 frame_count -= tas_skip;
594 signal(SIGINT, signal_handler);
601 ret = find_device(&dev, 0x16C0, 0x0486);
607 printf("waiting for device..\n");
620 /* we wait first, then send commands, but if teensy
621 * is started already, it won't send anything */
628 memset(&pkt_in, 0, sizeof(pkt_in));
629 ret = submit_urb(dev.fd, &urb[URB_DATA_IN], dev.ifaces[0].ep_in,
630 &pkt_in, sizeof(pkt_in));
632 perror("USBDEVFS_SUBMITURB URB_DATA_IN");
639 ret = submit_urb(dev.fd, &urb[URB_DBG_IN], dev.ifaces[1].ep_in,
640 buf_dbg, sizeof(buf_dbg) - 1);
642 perror("USBDEVFS_SUBMITURB URB_DBG_IN");
650 for (i = 0; i < evdev_fd_cnt; i++)
651 FD_SET(evdev_fds[i], &rfds);
654 FD_SET(dev.fd, &wfds);
656 ret = select(dev.fd + 1, &rfds, &wfds, NULL, timeout);
663 /* something from input devices? */
664 fixed_input_changed = 0;
665 for (i = 0; i < evdev_fd_cnt; i++) {
666 if (FD_ISSET(evdev_fds[i], &rfds)) {
667 fixed_input_changed |=
668 do_evdev_input(evdev_fds[i]);
672 /* something from USB? */
673 if (FD_ISSET(dev.fd, &wfds))
676 ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb);
680 perror("USBDEVFS_REAPURB");
684 if (reaped_urb != NULL && reaped_urb->status != 0) {
685 errno = -reaped_urb->status;
686 if ((unsigned long)(reaped_urb - urb) < ARRAY_SIZE(urb))
687 fprintf(stderr, "urb #%zu: ", reaped_urb - urb);
689 fprintf(stderr, "unknown urb: ");
691 if (reaped_urb->status == -EILSEQ) {
692 /* this is usually a sign of disconnect.. */
698 if (reaped_urb == &urb[URB_DATA_IN]) {
699 /* some request from teensy */
703 switch (pkt_in.type) {
705 printf("%d/%d\n", frames_sent, frame_count);
707 for (i = 0; i < sizeof(pkt_out.data); i++) {
708 pkt_out.data[i * 2 + 0] = 0x33;
709 pkt_out.data[i * 2 + 1] = 0x3f;
711 if (frames_sent < frame_count) {
712 pkt_out.type = PKT_STREAM_DATA;
714 count = frame_count - frames_sent;
715 if (count > sizeof(pkt_out.data) / 2)
716 count = sizeof(pkt_out.data) / 2;
717 for (i = 0; i < count; i++) {
719 b = gmv->data[frames_sent][0];
721 /* ?0SA 00DU, ?1CB RLDU */
722 pkt_out.data[i * 2 + 0] = (b & 0x13) | ((b >> 2) & 0x20);
723 pkt_out.data[i * 2 + 1] = (b & 0x0f) | ((b >> 1) & 0x30);
725 if (gmv->data[frames_sent][1] != 0xff
726 || gmv->data[frames_sent][2] != 0xff)
728 fprintf(stderr, "f %d: unhandled byte(s) %02x %02x\n",
729 frames_sent, gmv->data[frames_sent][1],
730 gmv->data[frames_sent][2]);
737 pkt_out.type = PKT_STREAM_END;
739 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT],
740 dev.ifaces[0].ep_out, &pkt_out, sizeof(pkt_out));
742 perror("USBDEVFS_SUBMITURB URB_DATA_OUT PKT_STREAM_DATA");
746 printf("host: got unknown pkt type: %04x\n", pkt_in.type);
752 else if (reaped_urb == &urb[URB_DATA_OUT]) {
754 else if (reaped_urb == &urb[URB_DBG_IN]) {
756 buf_dbg[reaped_urb->actual_length] = 0;
757 printf("%s", buf_dbg);
761 fprintf(stderr, "reaped unknown urb? %p #%zu\n",
762 reaped_urb, reaped_urb - urb);
766 /* something to send? */
767 if (gmv != NULL && !enable_sent) {
768 memset(&pkt_out, 0, sizeof(pkt_out));
769 pkt_out.type = PKT_STREAM_ENABLE;
770 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
771 &pkt_out, sizeof(pkt_out));
773 perror("USBDEVFS_SUBMITURB PKT_STREAM_ENABLE");
778 if (gmv == NULL && fixed_input_changed) {
779 memset(&pkt_out, 0, sizeof(pkt_out));
780 pkt_out.type = PKT_FIXED_STATE;
781 memcpy(pkt_out.data, fixed_input_state, sizeof(fixed_input_state));
783 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
784 &pkt_out, sizeof(pkt_out));
786 perror("USBDEVFS_SUBMITURB URB_DATA_OUT");
803 // vim: ts=2:sw=2:expandtab