add ability to insert/remove frames at the beginning of gmv
[megadrive.git] / host / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
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>
12 #include <signal.h>
13 #include <termios.h>
14 #include <errno.h>
15 #include <linux/usbdevice_fs.h>
16 #include <linux/usb/ch9.h>
17 #include <linux/input.h>
18 #include "../pkts.h"
19
20 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
21
22 struct 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 */
31 static 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
99 next:
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
112 found:
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
228 out:
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
239 static 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)
264     tty.c_lflag |= ECHO;
265   else
266     tty.c_lflag &= ~ECHO;
267
268   ret = tcsetattr(fd, TCSANOW, &tty);
269   if (ret != 0) {
270     perror("tcsetattr");
271     goto out;
272   }
273
274   retval = 0;
275 out:
276   close(fd);
277
278   return retval;
279 }
280
281 static void signal_handler(int sig)
282 {
283   enable_echo(1);
284   signal(sig, SIG_DFL);
285   raise(sig);
286 }
287
288 /* ?0SA 00DU, ?1CB RLDU */
289 #define STATE_BYTES 2
290
291 static uint8_t fixed_input_state[STATE_BYTES] = { 0x33, 0x3f };
292
293 enum mdbtn {
294   MDBTN_UP = 1,
295   MDBTN_DOWN,
296   MDBTN_LEFT,
297   MDBTN_RIGHT,
298   MDBTN_A,
299   MDBTN_B,
300   MDBTN_C,
301   MDBTN_START,
302 };
303
304 static const enum mdbtn evdev_md_map[KEY_CNT] = {
305   [KEY_UP]       = MDBTN_UP,
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,
311   [KEY_END]      = MDBTN_C,
312   [KEY_LEFTALT]  = MDBTN_START,
313 };
314
315 int do_evdev_input(int fd)
316 {
317   uint8_t old_state[STATE_BYTES];
318   uint8_t changed_bits[STATE_BYTES] = { 0, };
319   struct input_event ev;
320   enum mdbtn mdbtn;
321   int i, ret;
322
323   ret = read(fd, &ev, sizeof(ev));
324   if (ret != sizeof(ev)) {
325     fprintf(stderr, "evdev read %d/%zd: ", ret, sizeof(ev));
326     perror("");
327     return 0;
328   }
329
330   if (ev.type != EV_KEY)
331     return 0;
332
333   if (ev.value != 0 && ev.value != 1)
334     return 0;
335
336   if ((uint32_t)ev.code >= ARRAY_SIZE(evdev_md_map)) {
337     fprintf(stderr, "evdev read bad key: %u\n", ev.code);
338     return 0;
339   }
340
341   mdbtn = evdev_md_map[ev.code];
342   if (mdbtn == 0)
343     return 0;
344
345   memcpy(old_state, fixed_input_state, STATE_BYTES);
346
347   /* ?0SA 00DU, ?1CB RLDU */
348   switch (mdbtn) {
349   case MDBTN_UP:
350     changed_bits[0] = 0x01;
351     changed_bits[1] = 0x01;
352     break;
353   case MDBTN_DOWN:
354     changed_bits[0] = 0x02;
355     changed_bits[1] = 0x02;
356     break;
357   case MDBTN_LEFT:
358     changed_bits[0] = 0x00;
359     changed_bits[1] = 0x04;
360     break;
361   case MDBTN_RIGHT:
362     changed_bits[0] = 0x00;
363     changed_bits[1] = 0x08;
364     break;
365   case MDBTN_A:
366     changed_bits[0] = 0x10;
367     changed_bits[1] = 0x00;
368     break;
369   case MDBTN_B:
370     changed_bits[0] = 0x00;
371     changed_bits[1] = 0x10;
372     break;
373   case MDBTN_C:
374     changed_bits[0] = 0x00;
375     changed_bits[1] = 0x20;
376     break;
377   case MDBTN_START:
378     changed_bits[0] = 0x20;
379     changed_bits[1] = 0x00;
380     break;
381   }
382
383   if (ev.value) {
384     // key press
385     for (i = 0; i < STATE_BYTES; i++)
386       fixed_input_state[i] &= ~changed_bits[i];
387   }
388   else {
389     // key release
390     for (i = 0; i < STATE_BYTES; i++)
391       fixed_input_state[i] |=  changed_bits[i];
392   }
393
394   return memcmp(old_state, fixed_input_state, STATE_BYTES) ? 1 : 0;
395 }
396
397 struct gmv_tas {
398   char sig[15];
399   char ver;
400   uint32_t rerecord_count;
401   char ctrl1;
402   char ctrl2;
403   uint16_t flags;
404   char name[40];
405   uint8_t data[0][3];
406 };
407
408 static int submit_urb(int fd, struct usbdevfs_urb *urb, int ep,
409   void *buf, size_t buf_size)
410 {
411   memset(urb, 0, sizeof(*urb));
412   urb->type = USBDEVFS_URB_TYPE_INTERRUPT;
413   urb->endpoint = ep;
414   urb->buffer = buf;
415   urb->buffer_length = buf_size;
416
417   return ioctl(fd, USBDEVFS_SUBMITURB, urb);
418 }
419
420 enum my_urbs {
421   URB_DATA_IN,
422   URB_DATA_OUT,
423   URB_DBG_IN,
424   URB_CNT
425 };
426
427 static void missing_arg(int a)
428 {
429   fprintf(stderr, "missing arg: %d\n", a);
430   exit(1);
431 }
432
433 int main(int argc, char *argv[])
434 {
435   struct teensy_dev dev;
436   struct usbdevfs_urb urb[URB_CNT];
437   struct usbdevfs_urb *reaped_urb;
438   int fixed_input_changed;
439   int evdev_fds[16];
440   int evdev_fd_cnt = 0;
441   int evdev_support;
442   int wait_device = 0;
443   int dbg_in_sent = 0;
444   int data_in_sent = 0;
445   fd_set rfds, wfds;
446   struct gmv_tas *gmv = NULL;
447   const char *tasfn = NULL;
448   int tas_skip = 0;
449   int enable_sent = 0;
450   int frame_count = 0;
451   int frames_sent = 0;
452   char buf_dbg[64 + 1];
453   struct tas_pkt pkt_in;
454   struct tas_pkt pkt_out;
455   struct timeval *timeout = NULL;
456   struct timeval tout;
457   int i, ret;
458   int fd;
459
460   for (i = 1; i < argc; i++) {
461     if (argv[i][0] == '-') {
462       switch (argv[i][1] | (argv[i][2] << 8)) {
463       case 'm':
464         i++;
465         if (argv[i] == NULL)
466           missing_arg(i);
467         tasfn = argv[i];
468         continue;
469       case 's':
470         i++;
471         if (argv[i] == NULL)
472           missing_arg(i);
473         tas_skip = atoi(argv[i]);
474         continue;
475       default:
476         fprintf(stderr, "bad arg: %s\n", argv[i]);
477         return 1;
478       }
479     }
480     if (evdev_fd_cnt >= ARRAY_SIZE(evdev_fds)) {
481       fprintf(stderr, "too many evdevs\n");
482       break;
483     }
484     fd = open(argv[i], O_RDONLY);
485     if (fd == -1) {
486       fprintf(stderr, "open %s: ", argv[i]);
487       perror("");
488       continue;
489     }
490     evdev_support = 0;
491     ret = ioctl(fd, EVIOCGBIT(0, sizeof(evdev_support)),
492                 &evdev_support);
493     if (ret < 0)
494       perror("EVIOCGBIT");
495     if (!(evdev_support & (1 << EV_KEY))) {
496       fprintf(stderr, "%s doesn't have keys\n", argv[i]);
497       close(fd);
498       continue;
499     }
500     evdev_fds[evdev_fd_cnt++] = fd;
501   }
502
503   if (tasfn != NULL) {
504     long size;
505     FILE *f;
506     
507     f = fopen(tasfn, "rb");
508     if (f == NULL) {
509       fprintf(stderr, "fopen %s: ", tasfn);
510       perror("");
511       return 1;
512     }
513
514     fseek(f, 0, SEEK_END);
515     size = ftell(f);
516     fseek(f, 0, SEEK_SET);
517     if (size < (long)sizeof(*gmv)) {
518       fprintf(stderr, "bad gmv size: %ld\n", size);
519       return 1;
520     }
521     gmv = malloc(size);
522     if (gmv == NULL) {
523       fprintf(stderr, "OOM?\n");
524       return 1;
525     }
526     ret = fread(gmv, 1, size, f);
527     if (ret != size) {
528       fprintf(stderr, "fread %d/%ld: ", ret, size);
529       perror("");
530       return 1;
531     }
532     fclose(f);
533     frame_count = (size - sizeof(*gmv)) / sizeof(gmv->data[0]);
534
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);
538       return 1;
539     }
540
541     if (strncmp(gmv->sig, "Gens Movie TEST", 15) != 0) {
542       fprintf(stderr, "bad GMV sig\n");
543       return 1;
544     }
545     if (gmv->ctrl1 != '3') {
546       fprintf(stderr, "unhandled controlled config: '%c'\n", gmv->ctrl1);
547       //return 1;
548     }
549     if (gmv->ver >= 'A') {
550       if (gmv->flags & 0x40) {
551         fprintf(stderr, "unhandled flag: movie requires a savestate\n");
552         return 1;
553       }
554       if (gmv->flags & 0x20) {
555         fprintf(stderr, "unhandled flag: 3-player movie\n");
556         return 1;
557       }
558       if (gmv->flags & ~0x80) {
559         fprintf(stderr, "unhandled flag(s): %04x\n", gmv->flags);
560         //return 1;
561       }
562     }
563     gmv->name[39] = 0;
564     printf("loaded GMV: %s\n", gmv->name);
565     printf("%d frames, %u rerecords\n",
566            frame_count, gmv->rerecord_count);
567
568     if (tas_skip != 0) {
569       if (tas_skip >= frame_count || tas_skip <= -frame_count) {
570         printf("skip out of range: %d/%d\n", tas_skip, frame_count);
571         return 1;
572       }
573       if (tas_skip > 0) {
574         frame_count -= tas_skip;
575         memmove(&gmv->data[0], &gmv->data[tas_skip],
576           sizeof(gmv->data[0]) * frame_count);
577       }
578       else {
579         gmv = realloc(gmv, sizeof(*gmv)
580                 + (frame_count - tas_skip) * sizeof(gmv->data[0]));
581         if (gmv == NULL) {
582           fprintf(stderr, "OOM?\n");
583           return 1;
584         }
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;
589       }
590     }
591   }
592
593   enable_echo(0);
594   signal(SIGINT, signal_handler);
595
596   dev.fd = -1;
597
598   while (1)
599   {
600     if (dev.fd == -1) {
601       ret = find_device(&dev, 0x16C0, 0x0486);
602       if (ret < 0)
603         break;
604
605       if (ret == 0) {
606         if (!wait_device) {
607           printf("waiting for device..\n");
608           wait_device = 1;
609         }
610         usleep(250000);
611         continue;
612       }
613
614       wait_device = 0;
615       data_in_sent = 0;
616       dbg_in_sent = 0;
617       enable_sent = 0;
618       frames_sent = 0;
619
620       /* we wait first, then send commands, but if teensy
621        * is started already, it won't send anything */
622       tout.tv_sec = 1;
623       tout.tv_usec = 0;
624       timeout = &tout;
625     }
626
627     if (!data_in_sent) {
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));
631       if (ret != 0) {
632         perror("USBDEVFS_SUBMITURB URB_DATA_IN");
633         break;
634       }
635
636       data_in_sent = 1;
637     }
638     if (!dbg_in_sent) {
639       ret = submit_urb(dev.fd, &urb[URB_DBG_IN], dev.ifaces[1].ep_in,
640                        buf_dbg, sizeof(buf_dbg) - 1);
641       if (ret != 0) {
642         perror("USBDEVFS_SUBMITURB URB_DBG_IN");
643         break;
644       }
645
646       dbg_in_sent = 1;
647     }
648
649     FD_ZERO(&rfds);
650     for (i = 0; i < evdev_fd_cnt; i++)
651       FD_SET(evdev_fds[i], &rfds);
652
653     FD_ZERO(&wfds);
654     FD_SET(dev.fd, &wfds);
655
656     ret = select(dev.fd + 1, &rfds, &wfds, NULL, timeout);
657     if (ret < 0) {
658       perror("select");
659       break;
660     }
661     timeout = NULL;
662
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]);
669       }
670     }
671
672     /* something from USB? */
673     if (FD_ISSET(dev.fd, &wfds))
674     {
675       reaped_urb = NULL;
676       ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb);
677       if (ret != 0) {
678         if (errno == ENODEV)
679           goto dev_close;
680         perror("USBDEVFS_REAPURB");
681         break;
682       }
683
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);
688         else
689           fprintf(stderr, "unknown urb: ");
690         perror("");
691         if (reaped_urb->status == -EILSEQ) {
692           /* this is usually a sign of disconnect.. */
693           usleep(250000);
694           goto dev_close;
695         }
696       }
697
698       if (reaped_urb == &urb[URB_DATA_IN]) {
699         /* some request from teensy */
700         int count;
701         uint8_t b;
702
703         switch (pkt_in.type) {
704         case PKT_STREAM_REQ:
705           printf("%d/%d\n", frames_sent, frame_count);
706
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;
710           }
711           if (frames_sent < frame_count) {
712             pkt_out.type = PKT_STREAM_DATA;
713
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++) {
718               /* SCBA RLDU */
719               b = gmv->data[frames_sent][0];
720
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);
724
725               if (gmv->data[frames_sent][1] != 0xff
726                   || gmv->data[frames_sent][2] != 0xff)
727               {
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]);
731               }
732
733               frames_sent++;
734             }
735           }
736           else
737             pkt_out.type = PKT_STREAM_END;
738
739           ret = submit_urb(dev.fd, &urb[URB_DATA_OUT],
740                   dev.ifaces[0].ep_out, &pkt_out, sizeof(pkt_out));
741           if (ret != 0)
742             perror("USBDEVFS_SUBMITURB URB_DATA_OUT PKT_STREAM_DATA");
743           break;
744
745         default:
746           printf("host: got unknown pkt type: %04x\n", pkt_in.type);
747           break;
748         }
749
750         data_in_sent = 0;
751       }
752       else if (reaped_urb == &urb[URB_DATA_OUT]) {
753       }
754       else if (reaped_urb == &urb[URB_DBG_IN]) {
755         /* debug text */
756         buf_dbg[reaped_urb->actual_length] = 0;
757         printf("%s", buf_dbg);
758         dbg_in_sent = 0;
759       }
760       else {
761         fprintf(stderr, "reaped unknown urb? %p #%zu\n",
762           reaped_urb, reaped_urb - urb);
763       }
764     }
765
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));
772       if (ret != 0) {
773         perror("USBDEVFS_SUBMITURB PKT_STREAM_ENABLE");
774         continue;
775       }
776       enable_sent = 1;
777     }
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));
782
783       ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
784                        &pkt_out, sizeof(pkt_out));
785       if (ret != 0) {
786         perror("USBDEVFS_SUBMITURB URB_DATA_OUT");
787         break;
788       }
789     }
790
791     continue;
792
793 dev_close:
794     close(dev.fd);
795     dev.fd = -1;
796   }
797
798   enable_echo(1);
799
800   return ret;
801 }
802
803 // vim: ts=2:sw=2:expandtab