2 * TeensyTAS, TAS input player for MegaDrive
3 * Copyright (c) 2014 notaz
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/select.h>
40 #include <linux/usbdevice_fs.h>
41 #include <linux/usb/ch9.h>
42 #include <linux/input.h>
45 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
55 /* return 1 if founf, 0 if not, < 0 on error */
56 static int find_device(struct teensy_dev *dev,
57 uint16_t vendor, uint16_t product)
59 const char path_root[] = "/dev/bus/usb";
61 struct usb_descriptor_header hdr;
62 struct usb_device_descriptor d;
63 struct usb_config_descriptor c;
64 struct usb_interface_descriptor i;
65 struct usb_endpoint_descriptor e;
66 char space[0x100]; /* enough? */
68 char path_bus[256], path_dev[256];
69 struct dirent *ent, *ent_bus;
70 DIR *dir = NULL, *dir_bus = NULL;
76 memset(dev, 0xff, sizeof(*dev));
78 dir = opendir(path_root);
84 for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
85 /* should be a number like 000 */
86 if (sscanf(ent->d_name, "%03d", &num) != 1)
89 snprintf(path_bus, sizeof(path_bus), "%s/%s",
90 path_root, ent->d_name);
92 dir_bus = opendir(path_bus);
96 ent_bus = readdir(dir_bus);
97 for (; ent_bus != NULL; ent_bus = readdir(dir_bus)) {
98 if (sscanf(ent->d_name, "%03d", &num) != 1)
101 snprintf(path_dev, sizeof(path_dev), "%s/%s/%s",
102 path_root, ent->d_name, ent_bus->d_name);
104 fd = open(path_dev, O_RDWR);
108 ret = read(fd, &desc.d, sizeof(desc.d));
109 if (ret != sizeof(desc.d)) {
110 fprintf(stderr, "desc read: %d/%zd: ", ret, sizeof(desc.d));
115 if (desc.d.bDescriptorType != USB_DT_DEVICE) {
116 fprintf(stderr, "%s: bad DT: 0x%02x\n",
117 path_dev, desc.d.bDescriptorType);
121 if (desc.d.idVendor == vendor && desc.d.idProduct == product)
138 if (desc.d.bNumConfigurations != 1) {
139 fprintf(stderr, "unexpected bNumConfigurations: %u\n",
140 desc.d.bNumConfigurations);
144 /* walk through all descriptors */
147 ret = read(fd, &desc.hdr, sizeof(desc.hdr));
150 if (ret != sizeof(desc.hdr)) {
151 fprintf(stderr, "desc.hdr read: %d/%zd: ", ret, sizeof(desc.hdr));
156 ret = (int)lseek(fd, -sizeof(desc.hdr), SEEK_CUR);
162 ret = read(fd, &desc, desc.hdr.bLength);
163 if (ret != desc.hdr.bLength) {
164 fprintf(stderr, "desc read: %d/%u: ", ret, desc.hdr.bLength);
169 switch (desc.hdr.bDescriptorType) {
171 if (desc.c.bNumInterfaces != 2) {
172 fprintf(stderr, "unexpected bNumInterfaces: %u\n",
173 desc.c.bNumInterfaces);
178 case USB_DT_INTERFACE:
179 if (desc.i.bInterfaceClass != USB_CLASS_HID
180 || desc.i.bInterfaceSubClass != 0
181 || desc.i.bInterfaceProtocol != 0) {
182 fprintf(stderr, "unexpected interface %x:%x:%x\n",
183 desc.i.bInterfaceClass, desc.i.bInterfaceSubClass,
184 desc.i.bInterfaceProtocol);
187 if (desc.i.bNumEndpoints != 2) {
188 fprintf(stderr, "unexpected bNumEndpoints: %u\n",
189 desc.i.bNumEndpoints);
195 case USB_DT_ENDPOINT:
196 if (iface < 0 || iface >= ARRAY_SIZE(dev->ifaces)) {
197 fprintf(stderr, "bad iface: %d\n", iface);
200 if (desc.e.wMaxPacketSize != 64 && desc.e.wMaxPacketSize != 32) {
201 fprintf(stderr, "iface %d, EP %02x: "
202 "unexpected wMaxPacketSize: %u\n",
203 iface, desc.e.bEndpointAddress, desc.e.wMaxPacketSize);
206 if (desc.e.bEndpointAddress & 0x80)
207 dev->ifaces[iface].ep_in = desc.e.bEndpointAddress; // & 0x7F;
209 dev->ifaces[iface].ep_out = desc.e.bEndpointAddress;
217 fprintf(stderr, "skipping desc 0x%02x\n",
218 desc.hdr.bDescriptorType);
223 /* claim interfaces */
224 for (iface = 0; iface < ARRAY_SIZE(dev->ifaces); iface++) {
225 struct usbdevfs_ioctl usbio;
227 if (dev->ifaces[iface].ep_in == -1) {
228 fprintf(stderr, "missing ep_in, iface: %d\n", iface);
231 if (dev->ifaces[iface].ep_out == -1) {
232 fprintf(stderr, "missing ep_out, iface: %d\n", iface);
236 /* disconnect default driver */
237 memset(&usbio, 0, sizeof(usbio));
239 usbio.ioctl_code = USBDEVFS_DISCONNECT;
240 ret = ioctl(fd, USBDEVFS_IOCTL, &usbio);
241 if (ret != 0 && errno != ENODATA)
242 perror("USBDEVFS_DISCONNECT");
244 ret = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &iface);
246 perror("USBDEVFS_CLAIMINTERFACE");
264 static int enable_echo(int enable)
266 const char *portname = "/dev/tty";
272 memset(&tty, 0, sizeof(tty));
274 fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
276 fprintf(stderr, "open %s: ", portname);
281 ret = tcgetattr(fd, &tty);
287 // printf("lflag: 0%o\n", tty.c_lflag);
289 tty.c_lflag |= ECHO | ICANON;
291 tty.c_lflag &= ~(ECHO | ICANON);
292 tty.c_cc[VMIN] = tty.c_cc[VTIME] = 0;
295 ret = tcsetattr(fd, TCSANOW, &tty);
310 static void signal_handler(int sig)
313 signal(sig, SIG_DFL);
316 /* ?0SA 00DU, ?1CB RLDU */
317 #define STATE_BYTES 2
319 static uint8_t fixed_input_state[STATE_BYTES] = { 0x33, 0x3f };
332 static const enum mdbtn evdev_md_map[KEY_CNT] = {
334 [KEY_DOWN] = MDBTN_DOWN,
335 [KEY_LEFT] = MDBTN_LEFT,
336 [KEY_RIGHT] = MDBTN_RIGHT,
337 [KEY_HOME] = MDBTN_A,
338 [KEY_PAGEDOWN] = MDBTN_B,
340 [KEY_LEFTALT] = MDBTN_START,
343 int do_evdev_input(int fd)
345 uint8_t old_state[STATE_BYTES];
346 uint8_t changed_bits[STATE_BYTES] = { 0, };
347 struct input_event ev;
351 ret = read(fd, &ev, sizeof(ev));
352 if (ret != sizeof(ev)) {
353 fprintf(stderr, "evdev read %d/%zd: ", ret, sizeof(ev));
358 if (ev.type != EV_KEY)
361 if (ev.value != 0 && ev.value != 1)
364 if ((uint32_t)ev.code >= ARRAY_SIZE(evdev_md_map)) {
365 fprintf(stderr, "evdev read bad key: %u\n", ev.code);
369 mdbtn = evdev_md_map[ev.code];
373 memcpy(old_state, fixed_input_state, STATE_BYTES);
375 /* ?0SA 00DU, ?1CB RLDU */
378 changed_bits[0] = 0x01;
379 changed_bits[1] = 0x01;
382 changed_bits[0] = 0x02;
383 changed_bits[1] = 0x02;
386 changed_bits[0] = 0x00;
387 changed_bits[1] = 0x04;
390 changed_bits[0] = 0x00;
391 changed_bits[1] = 0x08;
394 changed_bits[0] = 0x10;
395 changed_bits[1] = 0x00;
398 changed_bits[0] = 0x00;
399 changed_bits[1] = 0x10;
402 changed_bits[0] = 0x00;
403 changed_bits[1] = 0x20;
406 changed_bits[0] = 0x20;
407 changed_bits[1] = 0x00;
413 for (i = 0; i < STATE_BYTES; i++)
414 fixed_input_state[i] &= ~changed_bits[i];
418 for (i = 0; i < STATE_BYTES; i++)
419 fixed_input_state[i] |= changed_bits[i];
422 return memcmp(old_state, fixed_input_state, STATE_BYTES) ? 1 : 0;
425 #define MAX_INPUT_BYTES 2
428 static int tas_data_to_teensy(uint16_t b, uint8_t *data, FILE *logf)
434 /* ?0SA 00DU, ?1CB RLDU */
435 data[0] = (b & 0x13) | ((b >> 2) & 0x20);
436 data[1] = (b & 0x0f) | ((b >> 1) & 0x30);
439 fwrite(&data[0], 1, 1, logf);
440 t = data[1] | 0x40; // expected TH
441 fwrite(&t, 1, 1, logf);
450 uint32_t rerecord_count;
458 static int import_gmv(FILE *f, long size,
459 uint8_t *out[2], int out_byte_count[2], FILE *logf)
468 out_byte_count[0] = out_byte_count[1] = 0;
470 if (size < (long)sizeof(*gmv)) {
471 fprintf(stderr, "bad gmv size: %ld\n", size);
477 fprintf(stderr, "OOM?\n");
480 ret = fread(gmv, 1, size, f);
482 fprintf(stderr, "fread %d/%ld: ", ret, size);
487 frame_count = (size - sizeof(*gmv)) / sizeof(gmv->data[0]);
489 /* check the GMV.. */
490 if (frame_count <= 0 || size != sizeof(*gmv) + frame_count * 3) {
491 fprintf(stderr, "broken gmv? frames=%d\n", frame_count);
495 if (strncmp(gmv->sig, "Gens Movie TEST", 15) != 0) {
496 fprintf(stderr, "bad GMV sig\n");
499 if (gmv->ctrl1 != '3') {
500 fprintf(stderr, "unhandled controlled config: '%c'\n", gmv->ctrl1);
503 if (gmv->ver >= 'A') {
504 if (gmv->flags & 0x40) {
505 fprintf(stderr, "unhandled flag: movie requires a savestate\n");
508 if (gmv->flags & 0x20) {
509 fprintf(stderr, "unhandled flag: 3-player movie\n");
512 if (gmv->flags & ~0x80) {
513 //fprintf(stderr, "unhandled flag(s): %04x\n", gmv->flags);
518 printf("loaded GMV: %s\n", gmv->name);
519 printf("%d frames, %u rerecords\n",
520 frame_count, gmv->rerecord_count);
522 out[0] = malloc(frame_count * MAX_INPUT_BYTES);
523 if (out[0] == NULL) {
524 fprintf(stderr, "OOM?\n");
528 for (i = 0; i < frame_count; i++) {
529 val = gmv->data[i][0] | ((gmv->data[i][2] & 0x0f) << 8);
530 count += tas_data_to_teensy(val, out[0] + count, logf);
532 if (gmv->data[i][1] != 0xff || gmv->data[i][2] != 0xff)
534 fprintf(stderr, "f %d: unhandled byte(s) %02x %02x\n",
535 i, gmv->data[i][1], gmv->data[i][2]);
539 out_byte_count[0] = count;
543 static int do_bkm_char(char c, char expect, uint16_t *val, int bit)
552 fprintf(stderr, "unexpected bkm char: '%c' instead of '%c'\n",
557 static int import_bkm(FILE *f, uint8_t *out[2], int out_byte_count[2],
560 int teensy_bytes = 0;
573 while ((p = fgets(buf, sizeof(buf), f)) != NULL)
581 if (p[30] != '\r' && p[30] != '\n')
585 if (count >= alloc - MAX_INPUT_BYTES) {
586 alloc = alloc * 2 + 64;
587 for (pl = 0; pl < 2; pl++) {
588 out[pl] = realloc(out[pl], alloc * sizeof(out[0][0]));
589 if (out[pl] == NULL) {
590 fprintf(stderr, "OOM?\n");
596 if (strncmp(p, "|.|", 3) != 0)
600 for (pl = 0; pl < 2; pl++) {
601 static const char ref[] = "UDLRABCSXYZM";
604 for (r = ref, i = 0; *r != 0; p++, r++, i++) {
605 if (do_bkm_char(*p, *r, &val, i))
612 teensy_bytes = tas_data_to_teensy(val, out[pl] + count, logf);
614 if ((val & 0xf00) != 0xf00)
617 have_pl2 |= (val != 0xfff);
619 count += teensy_bytes;
621 if (strcmp(p, "|") != 0)
628 fprintf(stderr, "unhandled bkm line %d: '%s'\n", line, buf);
632 printf("loaded bkm, %d players, %d frames, %d bytes, have_xyz=%d\n",
633 have_pl2 ? 2 : 1, frames, count, have_xyz);
634 out_byte_count[0] = count;
636 out_byte_count[1] = count;
645 static int import_raw(FILE *f, uint8_t **out, int *out_byte_count,
659 while ((p = fgets(buf, sizeof(buf), f)) != NULL)
665 printf("skipping: %s", p);
671 for (i = 6; i >= 0; i--, p++) {
672 if (*p != '0' && *p != '1')
680 if (first && (val & 0x40))
684 if (count >= alloc) {
685 alloc = alloc * 2 + 64;
686 *out = realloc(*out, alloc * sizeof((*out)[0]));
688 fprintf(stderr, "OOM?\n");
694 fwrite(&val, 1, 1, logf);
696 (*out)[count++] = val & 0x3f;
700 fprintf(stderr, "bad raw line %d: '%s'\n", line, buf);
704 printf("loaded raw, %d bytes\n", count);
705 *out_byte_count = count;
709 static int write_bkm_frame(FILE *f, const uint8_t *data)
711 /* ?0SA 00DU, ?1CB RLDU */
712 static const char ref[] = "UDLRABCSXYZM";
713 static const char bits[] = { 0,1,2,3, 12,4,5,13, 16,16,16,16 };
718 fprintf(stderr, "%s called without outfile\n", __func__);
722 idata[0] = 0x10000 | (data[0] << 8) | data[1];
726 for (p = 0; p < 2; p++) {
727 for (i = 0; i < 12; i++)
728 fprintf(f, "%c", (idata[p] & (1 << bits[i])) ? '.' : ref[i]);
737 static int submit_urb(int fd, struct usbdevfs_urb *urb, int ep,
738 void *buf, size_t buf_size)
740 memset(urb, 0, sizeof(*urb));
741 urb->type = USBDEVFS_URB_TYPE_INTERRUPT;
744 urb->buffer_length = buf_size;
746 return ioctl(fd, USBDEVFS_SUBMITURB, urb);
756 static void missing_arg(int a)
758 fprintf(stderr, "missing arg: %d\n", a);
762 int main(int argc, char *argv[])
764 struct teensy_dev dev;
765 struct usbdevfs_urb urb[URB_CNT];
766 struct usbdevfs_urb *reaped_urb;
767 int fixed_input_changed = 0;
769 int evdev_fd_cnt = 0;
772 int pending_urbs = 0;
774 const char *tasfn = NULL;
775 const char *tasfn_p2 = NULL;
776 const char *outfn = NULL;
777 const char *logfn = NULL;
778 uint8_t *tas_data[2] = { NULL, NULL };
779 int tas_data_size[2] = { 0, 0 };
780 int bytes_sent[2] = { 0, 0 };
781 int use_vsync = 0; // frame increment on vsync
783 int no_start_seq = 0;
787 char buf_dbg[64 + 1];
788 struct tas_pkt pkt_in;
789 struct tas_pkt pkt_out;
790 struct timeval *timeout = NULL;
797 for (i = 1; i < argc; i++) {
798 if (argv[i][0] == '-') {
799 switch (argv[i][1] | (argv[i][2] << 8)) {
831 fprintf(stderr, "bad arg: %s\n", argv[i]);
836 /* remaining args are evdev filenames */
837 if (evdev_fd_cnt >= ARRAY_SIZE(evdev_fds)) {
838 fprintf(stderr, "too many evdevs\n");
841 fd = open(argv[i], O_RDONLY);
843 fprintf(stderr, "open %s: ", argv[i]);
848 ret = ioctl(fd, EVIOCGBIT(0, sizeof(evdev_support)),
852 if (!(evdev_support & (1 << EV_KEY))) {
853 fprintf(stderr, "%s doesn't have keys\n", argv[i]);
857 evdev_fds[evdev_fd_cnt++] = fd;
861 logf = fopen(logfn, "wb");
863 fprintf(stderr, "fopen %s: ", logfn);
870 FILE *f, *f_p2 = NULL;
874 f = fopen(tasfn, "rb");
876 fprintf(stderr, "fopen %s: ", tasfn);
881 if (tasfn_p2 != NULL) {
882 f_p2 = fopen(tasfn_p2, "rb");
884 fprintf(stderr, "fopen %s: ", tasfn_p2);
890 fseek(f, 0, SEEK_END);
892 fseek(f, 0, SEEK_SET);
894 fprintf(stderr, "bad size: %ld\n", size);
898 ext = strrchr(tasfn, '.');
904 if (strcasecmp(ext, "gmv") == 0)
905 ret = import_gmv(f, size, tas_data, tas_data_size, logf);
906 else if (strcasecmp(ext, "bkm") == 0)
907 ret = import_bkm(f, tas_data, tas_data_size, logf);
908 else if (strcasecmp(ext, "txt") == 0)
909 ret = import_raw(f, &tas_data[0], &tas_data_size[0], logf);
911 fprintf(stderr, "unknown movie type: '%s'\n", ext);
916 if (ret != 0 || tas_data[0] == NULL || tas_data_size[0] <= 0) {
917 fprintf(stderr, "failed fo parse %s\n", tasfn);
921 // separate file with p2 input?
923 ret = import_raw(f_p2, &tas_data[1], &tas_data_size[1], NULL);
924 if (ret != 0 || tas_data[1] == NULL || tas_data_size[1] <= 0) {
925 fprintf(stderr, "failed fo parse %s\n", tasfn_p2);
937 if (tas_data_size[1] != 0 && tas_data[1] == NULL) {
938 fprintf(stderr, "missing tas_data[1]\n");
944 outf = fopen(outfn, "w");
946 fprintf(stderr, "fopen %s: ", outfn);
953 signal(SIGINT, signal_handler);
957 while (!g_exit || (pending_urbs & (1 << URB_DATA_OUT)))
960 ret = find_device(&dev, 0x16C0, 0x0486);
966 printf("waiting for device..\n");
979 /* we wait first, then send commands, but if teensy
980 * is started already, it won't send anything */
986 if (!(pending_urbs & (1 << URB_DATA_IN))) {
987 memset(&pkt_in, 0, sizeof(pkt_in));
988 ret = submit_urb(dev.fd, &urb[URB_DATA_IN], dev.ifaces[0].ep_in,
989 &pkt_in, sizeof(pkt_in));
991 perror("USBDEVFS_SUBMITURB URB_DATA_IN");
995 pending_urbs |= 1 << URB_DATA_IN;
997 if (!(pending_urbs & (1 << URB_DBG_IN))) {
998 ret = submit_urb(dev.fd, &urb[URB_DBG_IN], dev.ifaces[1].ep_in,
999 buf_dbg, sizeof(buf_dbg) - 1);
1001 perror("USBDEVFS_SUBMITURB URB_DBG_IN");
1005 pending_urbs |= 1 << URB_DBG_IN;
1009 FD_SET(STDIN_FILENO, &rfds);
1010 for (i = 0; i < evdev_fd_cnt; i++)
1011 FD_SET(evdev_fds[i], &rfds);
1014 FD_SET(dev.fd, &wfds);
1016 ret = select(dev.fd + 1, &rfds, &wfds, NULL, timeout);
1023 /* sometihng form stdin? */
1024 if (FD_ISSET(STDIN_FILENO, &rfds)) {
1026 ret = read(STDIN_FILENO, &c, 1);
1028 perror("read stdin");
1039 /* something from input devices? */
1040 for (i = 0; i < evdev_fd_cnt; i++) {
1041 if (FD_ISSET(evdev_fds[i], &rfds)) {
1042 fixed_input_changed |=
1043 do_evdev_input(evdev_fds[i]);
1047 /* something from USB? */
1048 if (FD_ISSET(dev.fd, &wfds))
1050 unsigned int which_urb;
1053 ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb);
1055 if (errno == ENODEV)
1057 perror("USBDEVFS_REAPURB");
1060 which_urb = reaped_urb - urb;
1061 if (which_urb < ARRAY_SIZE(urb))
1062 pending_urbs &= ~(1 << which_urb);
1064 fprintf(stderr, "reaped unknown urb: %p #%u",
1065 reaped_urb, which_urb);
1068 if (reaped_urb != NULL && reaped_urb->status != 0) {
1069 errno = -reaped_urb->status;
1070 fprintf(stderr, "urb #%u: ", which_urb);
1072 if (reaped_urb->status == -EILSEQ) {
1073 /* this is usually a sign of disconnect.. */
1078 else if (reaped_urb == &urb[URB_DATA_IN])
1082 /* some request from teensy */
1083 switch (pkt_in.type) {
1084 case PKT_STREAM_REQ:
1085 p = pkt_in.req.is_p2 ? 1 : 0;
1086 printf("req%d: %d/%d/%d\n", pkt_in.req.is_p2,
1087 pkt_in.req.frame * 2, bytes_sent[p], tas_data_size[p]);
1090 if (bytes_sent[p] < tas_data_size[p]) {
1091 pkt_out.type = p ? PKT_STREAM_DATA_TO_P2
1092 : PKT_STREAM_DATA_TO_P1;
1094 i = tas_data_size[p] - bytes_sent[p];
1095 if (i > sizeof(pkt_out.data))
1096 i = sizeof(pkt_out.data);
1097 memcpy(pkt_out.data, tas_data[p] + bytes_sent[p], i);
1102 pkt_out.type = PKT_STREAM_END;
1105 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT],
1106 dev.ifaces[0].ep_out, &pkt_out, sizeof(pkt_out));
1108 perror("USBDEVFS_SUBMITURB PKT_STREAM_DATA_TO");
1111 case PKT_STREAM_DATA_FROM:
1112 printf("f: %d\n", frame_count);
1113 if (pkt_in.size == 0 || pkt_in.size > sizeof(pkt_out.data)) {
1114 printf("host: got bad DATA_FROM size: %u\n", pkt_in.size);
1117 for (i = 0; i < pkt_in.size; ) {
1118 i += write_bkm_frame(outf, pkt_in.data + i);
1124 printf("host: got unknown pkt type: %04x\n", pkt_in.type);
1128 else if (reaped_urb == &urb[URB_DATA_OUT])
1131 else if (reaped_urb == &urb[URB_DBG_IN])
1134 buf_dbg[reaped_urb->actual_length] = 0;
1135 printf("%s", buf_dbg);
1137 // continue receiving debug before sending out stuff
1139 tout.tv_usec = 1000;
1144 fprintf(stderr, "reaped unknown urb? %p #%zu\n",
1145 reaped_urb, reaped_urb - urb);
1149 /* something to send? */
1150 if (pending_urbs & (1 << URB_DATA_OUT))
1151 // can't do that yet
1154 if ((tas_data[0] != NULL || outf != NULL) && !enable_sent) {
1155 memset(&pkt_out, 0, sizeof(pkt_out));
1156 pkt_out.type = PKT_STREAM_ENABLE;
1157 pkt_out.enable.stream_to = (tas_data[0] != NULL);
1158 pkt_out.enable.stream_from = (outf != NULL);
1159 pkt_out.enable.no_start_seq = no_start_seq;
1161 pkt_out.enable.inc_mode = INC_MODE_VSYNC;
1162 else if (tas_data_size[1] != 0 && separate_2p)
1163 pkt_out.enable.inc_mode = INC_MODE_SEPARATE;
1164 else if (tas_data_size[1] != 0)
1165 pkt_out.enable.inc_mode = INC_MODE_SHARED_PL2;
1167 pkt_out.enable.inc_mode = INC_MODE_SHARED_PL1;
1169 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
1170 &pkt_out, sizeof(pkt_out));
1172 perror("USBDEVFS_SUBMITURB PKT_STREAM_ENABLE");
1175 pending_urbs |= 1 << URB_DATA_OUT;
1181 if (tas_data[0] == NULL && fixed_input_changed) {
1182 memset(&pkt_out, 0, sizeof(pkt_out));
1183 pkt_out.type = PKT_FIXED_STATE;
1184 memcpy(pkt_out.data, fixed_input_state, sizeof(fixed_input_state));
1186 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
1187 &pkt_out, sizeof(pkt_out));
1189 perror("USBDEVFS_SUBMITURB PKT_FIXED_STATE");
1192 fixed_input_changed = 0;
1193 pending_urbs |= 1 << URB_DATA_OUT;
1196 if (g_exit && !abort_sent) {
1197 memset(&pkt_out, 0, sizeof(pkt_out));
1198 pkt_out.type = PKT_STREAM_ABORT;
1200 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
1201 &pkt_out, sizeof(pkt_out));
1203 perror("USBDEVFS_SUBMITURB PKT_STREAM_ABORT");
1206 pending_urbs |= 1 << URB_DATA_OUT;
1224 /* deal with pending URBs */
1225 if (pending_urbs & (1 << URB_DATA_IN))
1226 ioctl(dev.fd, USBDEVFS_DISCARDURB, &urb[URB_DATA_IN]);
1227 if (pending_urbs & (1 << URB_DBG_IN))
1228 ioctl(dev.fd, USBDEVFS_DISCARDURB, &urb[URB_DBG_IN]);
1229 for (i = 0; i < URB_CNT; i++) {
1230 if (pending_urbs & (1 << i)) {
1231 ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb);
1233 perror("USBDEVFS_REAPURB");
1243 // vim: ts=2:sw=2:expandtab