implement vsync irq-based frame counter
[teensytas.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 use_readinc = 0; // frame increment on read
449   int tas_skip = 0;
450   int enable_sent = 0;
451   int frame_count = 0;
452   int frames_sent = 0;
453   char buf_dbg[64 + 1];
454   struct tas_pkt pkt_in;
455   struct tas_pkt pkt_out;
456   struct timeval *timeout = NULL;
457   struct timeval tout;
458   int i, ret;
459   int fd;
460
461   for (i = 1; i < argc; i++) {
462     if (argv[i][0] == '-') {
463       switch (argv[i][1] | (argv[i][2] << 8)) {
464       case 'm':
465         i++;
466         if (argv[i] == NULL)
467           missing_arg(i);
468         tasfn = argv[i];
469         continue;
470       case 's':
471         i++;
472         if (argv[i] == NULL)
473           missing_arg(i);
474         tas_skip = atoi(argv[i]);
475         continue;
476       case 'r':
477         use_readinc = 1;
478         continue;
479       default:
480         fprintf(stderr, "bad arg: %s\n", argv[i]);
481         return 1;
482       }
483     }
484     if (evdev_fd_cnt >= ARRAY_SIZE(evdev_fds)) {
485       fprintf(stderr, "too many evdevs\n");
486       break;
487     }
488     fd = open(argv[i], O_RDONLY);
489     if (fd == -1) {
490       fprintf(stderr, "open %s: ", argv[i]);
491       perror("");
492       continue;
493     }
494     evdev_support = 0;
495     ret = ioctl(fd, EVIOCGBIT(0, sizeof(evdev_support)),
496                 &evdev_support);
497     if (ret < 0)
498       perror("EVIOCGBIT");
499     if (!(evdev_support & (1 << EV_KEY))) {
500       fprintf(stderr, "%s doesn't have keys\n", argv[i]);
501       close(fd);
502       continue;
503     }
504     evdev_fds[evdev_fd_cnt++] = fd;
505   }
506
507   if (tasfn != NULL) {
508     long size;
509     FILE *f;
510     
511     f = fopen(tasfn, "rb");
512     if (f == NULL) {
513       fprintf(stderr, "fopen %s: ", tasfn);
514       perror("");
515       return 1;
516     }
517
518     fseek(f, 0, SEEK_END);
519     size = ftell(f);
520     fseek(f, 0, SEEK_SET);
521     if (size < (long)sizeof(*gmv)) {
522       fprintf(stderr, "bad gmv size: %ld\n", size);
523       return 1;
524     }
525     gmv = malloc(size);
526     if (gmv == NULL) {
527       fprintf(stderr, "OOM?\n");
528       return 1;
529     }
530     ret = fread(gmv, 1, size, f);
531     if (ret != size) {
532       fprintf(stderr, "fread %d/%ld: ", ret, size);
533       perror("");
534       return 1;
535     }
536     fclose(f);
537     frame_count = (size - sizeof(*gmv)) / sizeof(gmv->data[0]);
538
539     /* check the GMV.. */
540     if (frame_count <= 0 || size != sizeof(*gmv) + frame_count * 3) {
541       fprintf(stderr, "broken gmv? frames=%d\n", frame_count);
542       return 1;
543     }
544
545     if (strncmp(gmv->sig, "Gens Movie TEST", 15) != 0) {
546       fprintf(stderr, "bad GMV sig\n");
547       return 1;
548     }
549     if (gmv->ctrl1 != '3') {
550       fprintf(stderr, "unhandled controlled config: '%c'\n", gmv->ctrl1);
551       //return 1;
552     }
553     if (gmv->ver >= 'A') {
554       if (gmv->flags & 0x40) {
555         fprintf(stderr, "unhandled flag: movie requires a savestate\n");
556         return 1;
557       }
558       if (gmv->flags & 0x20) {
559         fprintf(stderr, "unhandled flag: 3-player movie\n");
560         return 1;
561       }
562       if (gmv->flags & ~0x80) {
563         fprintf(stderr, "unhandled flag(s): %04x\n", gmv->flags);
564         //return 1;
565       }
566     }
567     gmv->name[39] = 0;
568     printf("loaded GMV: %s\n", gmv->name);
569     printf("%d frames, %u rerecords\n",
570            frame_count, gmv->rerecord_count);
571
572     if (tas_skip != 0) {
573       if (tas_skip >= frame_count || tas_skip <= -frame_count) {
574         printf("skip out of range: %d/%d\n", tas_skip, frame_count);
575         return 1;
576       }
577       if (tas_skip > 0) {
578         frame_count -= tas_skip;
579         memmove(&gmv->data[0], &gmv->data[tas_skip],
580           sizeof(gmv->data[0]) * frame_count);
581       }
582       else {
583         gmv = realloc(gmv, sizeof(*gmv)
584                 + (frame_count - tas_skip) * sizeof(gmv->data[0]));
585         if (gmv == NULL) {
586           fprintf(stderr, "OOM?\n");
587           return 1;
588         }
589         memmove(&gmv->data[-tas_skip], &gmv->data[0],
590           sizeof(gmv->data[0]) * frame_count);
591         memset(&gmv->data[0], 0xff, sizeof(gmv->data[0]) * -tas_skip);
592         frame_count -= tas_skip;
593       }
594     }
595   }
596
597   enable_echo(0);
598   signal(SIGINT, signal_handler);
599
600   dev.fd = -1;
601
602   while (1)
603   {
604     if (dev.fd == -1) {
605       ret = find_device(&dev, 0x16C0, 0x0486);
606       if (ret < 0)
607         break;
608
609       if (ret == 0) {
610         if (!wait_device) {
611           printf("waiting for device..\n");
612           wait_device = 1;
613         }
614         usleep(250000);
615         continue;
616       }
617
618       wait_device = 0;
619       data_in_sent = 0;
620       dbg_in_sent = 0;
621       enable_sent = 0;
622       frames_sent = 0;
623
624       /* we wait first, then send commands, but if teensy
625        * is started already, it won't send anything */
626       tout.tv_sec = 1;
627       tout.tv_usec = 0;
628       timeout = &tout;
629     }
630
631     if (!data_in_sent) {
632       memset(&pkt_in, 0, sizeof(pkt_in));
633       ret = submit_urb(dev.fd, &urb[URB_DATA_IN], dev.ifaces[0].ep_in,
634                        &pkt_in, sizeof(pkt_in));
635       if (ret != 0) {
636         perror("USBDEVFS_SUBMITURB URB_DATA_IN");
637         break;
638       }
639
640       data_in_sent = 1;
641     }
642     if (!dbg_in_sent) {
643       ret = submit_urb(dev.fd, &urb[URB_DBG_IN], dev.ifaces[1].ep_in,
644                        buf_dbg, sizeof(buf_dbg) - 1);
645       if (ret != 0) {
646         perror("USBDEVFS_SUBMITURB URB_DBG_IN");
647         break;
648       }
649
650       dbg_in_sent = 1;
651     }
652
653     FD_ZERO(&rfds);
654     for (i = 0; i < evdev_fd_cnt; i++)
655       FD_SET(evdev_fds[i], &rfds);
656
657     FD_ZERO(&wfds);
658     FD_SET(dev.fd, &wfds);
659
660     ret = select(dev.fd + 1, &rfds, &wfds, NULL, timeout);
661     if (ret < 0) {
662       perror("select");
663       break;
664     }
665     timeout = NULL;
666
667     /* something from input devices? */
668     fixed_input_changed = 0;
669     for (i = 0; i < evdev_fd_cnt; i++) {
670       if (FD_ISSET(evdev_fds[i], &rfds)) {
671         fixed_input_changed |=
672           do_evdev_input(evdev_fds[i]);
673       }
674     }
675
676     /* something from USB? */
677     if (FD_ISSET(dev.fd, &wfds))
678     {
679       reaped_urb = NULL;
680       ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb);
681       if (ret != 0) {
682         if (errno == ENODEV)
683           goto dev_close;
684         perror("USBDEVFS_REAPURB");
685         break;
686       }
687
688       if (reaped_urb != NULL && reaped_urb->status != 0) {
689         errno = -reaped_urb->status;
690         if ((unsigned long)(reaped_urb - urb) < ARRAY_SIZE(urb))
691           fprintf(stderr, "urb #%zu: ", reaped_urb - urb);
692         else
693           fprintf(stderr, "unknown urb: ");
694         perror("");
695         if (reaped_urb->status == -EILSEQ) {
696           /* this is usually a sign of disconnect.. */
697           usleep(250000);
698           goto dev_close;
699         }
700       }
701
702       if (reaped_urb == &urb[URB_DATA_IN]) {
703         /* some request from teensy */
704         int count;
705         uint8_t b;
706
707         switch (pkt_in.type) {
708         case PKT_STREAM_REQ:
709           printf("%d/%d/%d\n", pkt_in.req.frame,
710             frames_sent, frame_count);
711
712           for (i = 0; i < sizeof(pkt_out.data); i++) {
713             pkt_out.data[i * 2 + 0] = 0x33;
714             pkt_out.data[i * 2 + 1] = 0x3f;
715           }
716           if (frames_sent < frame_count) {
717             pkt_out.type = PKT_STREAM_DATA;
718
719             count = frame_count - frames_sent;
720             if (count > sizeof(pkt_out.data) / 2)
721               count = sizeof(pkt_out.data) / 2;
722             for (i = 0; i < count; i++) {
723               /* SCBA RLDU */
724               b = gmv->data[frames_sent][0];
725
726               /* ?0SA 00DU, ?1CB RLDU */
727               pkt_out.data[i * 2 + 0] = (b & 0x13) | ((b >> 2) & 0x20);
728               pkt_out.data[i * 2 + 1] = (b & 0x0f) | ((b >> 1) & 0x30);
729
730               if (gmv->data[frames_sent][1] != 0xff
731                   || gmv->data[frames_sent][2] != 0xff)
732               {
733                 fprintf(stderr, "f %d: unhandled byte(s) %02x %02x\n",
734                   frames_sent, gmv->data[frames_sent][1],
735                   gmv->data[frames_sent][2]);
736               }
737
738               frames_sent++;
739             }
740           }
741           else
742             pkt_out.type = PKT_STREAM_END;
743
744           ret = submit_urb(dev.fd, &urb[URB_DATA_OUT],
745                   dev.ifaces[0].ep_out, &pkt_out, sizeof(pkt_out));
746           if (ret != 0)
747             perror("USBDEVFS_SUBMITURB URB_DATA_OUT PKT_STREAM_DATA");
748           break;
749
750         default:
751           printf("host: got unknown pkt type: %04x\n", pkt_in.type);
752           break;
753         }
754
755         data_in_sent = 0;
756       }
757       else if (reaped_urb == &urb[URB_DATA_OUT]) {
758       }
759       else if (reaped_urb == &urb[URB_DBG_IN]) {
760         /* debug text */
761         buf_dbg[reaped_urb->actual_length] = 0;
762         printf("%s", buf_dbg);
763         dbg_in_sent = 0;
764       }
765       else {
766         fprintf(stderr, "reaped unknown urb? %p #%zu\n",
767           reaped_urb, reaped_urb - urb);
768       }
769     }
770
771     /* something to send? */
772     if (gmv != NULL && !enable_sent) {
773       memset(&pkt_out, 0, sizeof(pkt_out));
774       pkt_out.type = PKT_STREAM_ENABLE;
775       pkt_out.start.use_readinc = use_readinc;
776
777       ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
778                        &pkt_out, sizeof(pkt_out));
779       if (ret != 0) {
780         perror("USBDEVFS_SUBMITURB PKT_STREAM_ENABLE");
781         continue;
782       }
783       enable_sent = 1;
784     }
785     if (gmv == NULL && fixed_input_changed) {
786       memset(&pkt_out, 0, sizeof(pkt_out));
787       pkt_out.type = PKT_FIXED_STATE;
788       memcpy(pkt_out.data, fixed_input_state, sizeof(fixed_input_state));
789
790       ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
791                        &pkt_out, sizeof(pkt_out));
792       if (ret != 0) {
793         perror("USBDEVFS_SUBMITURB URB_DATA_OUT");
794         break;
795       }
796     }
797
798     continue;
799
800 dev_close:
801     close(dev.fd);
802     dev.fd = -1;
803   }
804
805   enable_echo(1);
806
807   return ret;
808 }
809
810 // vim: ts=2:sw=2:expandtab