c28a7c81 |
1 | /* |
2 | * TeensyTAS, TAS input player for MegaDrive |
3 | * Copyright (c) 2014 notaz |
4 | * |
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: |
12 | * |
13 | * The above copyright notice and this permission notice shall be |
14 | * included in all copies or substantial portions of the Software. |
15 | * |
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 |
23 | * SOFTWARE. |
24 | */ |
25 | |
932302ef |
26 | #include <stdio.h> |
61172125 |
27 | #include <stdlib.h> |
932302ef |
28 | #include <string.h> |
29 | #include <stdint.h> |
932302ef |
30 | #include <sys/types.h> |
31 | #include <sys/stat.h> |
32 | #include <fcntl.h> |
33 | #include <sys/ioctl.h> |
34 | #include <sys/select.h> |
35 | #include <unistd.h> |
36 | #include <dirent.h> |
f359b935 |
37 | #include <signal.h> |
38 | #include <termios.h> |
932302ef |
39 | #include <errno.h> |
40 | #include <linux/usbdevice_fs.h> |
41 | #include <linux/usb/ch9.h> |
1cb2822f |
42 | #include <linux/input.h> |
f20de073 |
43 | #include "../pkts.h" |
932302ef |
44 | |
45 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) |
46 | |
932302ef |
47 | struct teensy_dev { |
48 | int fd; |
49 | struct { |
50 | int ep_in; |
51 | int ep_out; |
52 | } ifaces[2]; |
53 | }; |
54 | |
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) |
58 | { |
59 | const char path_root[] = "/dev/bus/usb"; |
60 | union { |
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? */ |
67 | } desc; |
68 | char path_bus[256], path_dev[256]; |
69 | struct dirent *ent, *ent_bus; |
70 | DIR *dir = NULL, *dir_bus = NULL; |
71 | int num, fd = -1; |
72 | int iface = -1; |
73 | int retval = -1; |
74 | int ret; |
75 | |
76 | memset(dev, 0xff, sizeof(*dev)); |
77 | |
78 | dir = opendir(path_root); |
79 | if (dir == NULL) { |
80 | perror("opendir"); |
81 | return -1; |
82 | } |
83 | |
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) |
87 | continue; |
88 | |
89 | snprintf(path_bus, sizeof(path_bus), "%s/%s", |
90 | path_root, ent->d_name); |
91 | |
92 | dir_bus = opendir(path_bus); |
93 | if (dir_bus == NULL) |
94 | continue; |
95 | |
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) |
99 | continue; |
100 | |
101 | snprintf(path_dev, sizeof(path_dev), "%s/%s/%s", |
102 | path_root, ent->d_name, ent_bus->d_name); |
103 | |
104 | fd = open(path_dev, O_RDWR); |
105 | if (fd == -1) |
106 | continue; |
107 | |
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)); |
111 | perror(""); |
112 | goto next; |
113 | } |
114 | |
115 | if (desc.d.bDescriptorType != USB_DT_DEVICE) { |
116 | fprintf(stderr, "%s: bad DT: 0x%02x\n", |
117 | path_dev, desc.d.bDescriptorType); |
118 | goto next; |
119 | } |
120 | |
121 | if (desc.d.idVendor == vendor && desc.d.idProduct == product) |
122 | goto found; |
123 | |
124 | next: |
125 | close(fd); |
126 | fd = -1; |
127 | } |
128 | |
129 | closedir(dir_bus); |
130 | dir_bus = NULL; |
131 | } |
132 | |
133 | /* not found */ |
134 | retval = 0; |
135 | goto out; |
136 | |
137 | found: |
138 | if (desc.d.bNumConfigurations != 1) { |
139 | fprintf(stderr, "unexpected bNumConfigurations: %u\n", |
140 | desc.d.bNumConfigurations); |
141 | goto out; |
142 | } |
143 | |
144 | /* walk through all descriptors */ |
145 | while (1) |
146 | { |
147 | ret = read(fd, &desc.hdr, sizeof(desc.hdr)); |
148 | if (ret == 0) |
149 | break; |
150 | if (ret != sizeof(desc.hdr)) { |
151 | fprintf(stderr, "desc.hdr read: %d/%zd: ", ret, sizeof(desc.hdr)); |
152 | perror(""); |
153 | break; |
154 | } |
155 | |
156 | ret = (int)lseek(fd, -sizeof(desc.hdr), SEEK_CUR); |
157 | if (ret == -1) { |
158 | perror("lseek"); |
159 | break; |
160 | } |
161 | |
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); |
165 | perror(""); |
166 | break; |
167 | } |
168 | |
169 | switch (desc.hdr.bDescriptorType) { |
170 | case USB_DT_CONFIG: |
171 | if (desc.c.bNumInterfaces != 2) { |
172 | fprintf(stderr, "unexpected bNumInterfaces: %u\n", |
173 | desc.c.bNumInterfaces); |
174 | goto out; |
175 | } |
176 | break; |
177 | |
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); |
185 | goto out; |
186 | } |
187 | if (desc.i.bNumEndpoints != 2) { |
188 | fprintf(stderr, "unexpected bNumEndpoints: %u\n", |
189 | desc.i.bNumEndpoints); |
190 | goto out; |
191 | } |
192 | iface++; |
193 | break; |
194 | |
195 | case USB_DT_ENDPOINT: |
196 | if (iface < 0 || iface >= ARRAY_SIZE(dev->ifaces)) { |
197 | fprintf(stderr, "bad iface: %d\n", iface); |
198 | goto out; |
199 | } |
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); |
204 | goto out; |
205 | } |
206 | if (desc.e.bEndpointAddress & 0x80) |
207 | dev->ifaces[iface].ep_in = desc.e.bEndpointAddress; // & 0x7F; |
208 | else |
209 | dev->ifaces[iface].ep_out = desc.e.bEndpointAddress; |
210 | break; |
211 | |
212 | case 0x21: |
213 | /* ignore */ |
214 | break; |
215 | |
216 | default: |
217 | fprintf(stderr, "skipping desc 0x%02x\n", |
218 | desc.hdr.bDescriptorType); |
219 | break; |
220 | } |
221 | } |
222 | |
223 | /* claim interfaces */ |
224 | for (iface = 0; iface < ARRAY_SIZE(dev->ifaces); iface++) { |
225 | struct usbdevfs_ioctl usbio; |
226 | |
227 | if (dev->ifaces[iface].ep_in == -1) { |
228 | fprintf(stderr, "missing ep_in, iface: %d\n", iface); |
229 | goto out; |
230 | } |
231 | if (dev->ifaces[iface].ep_out == -1) { |
232 | fprintf(stderr, "missing ep_out, iface: %d\n", iface); |
233 | goto out; |
234 | } |
235 | |
236 | /* disconnect default driver */ |
237 | memset(&usbio, 0, sizeof(usbio)); |
238 | usbio.ifno = iface; |
239 | usbio.ioctl_code = USBDEVFS_DISCONNECT; |
240 | ret = ioctl(fd, USBDEVFS_IOCTL, &usbio); |
241 | if (ret != 0 && errno != ENODATA) |
242 | perror("USBDEVFS_DISCONNECT"); |
243 | |
244 | ret = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &iface); |
245 | if (ret != 0) |
246 | perror("USBDEVFS_CLAIMINTERFACE"); |
247 | } |
248 | |
249 | dev->fd = fd; |
250 | fd = -1; |
251 | retval = 1; |
252 | |
253 | out: |
254 | if (fd != -1) |
255 | close(fd); |
256 | if (dir_bus != NULL) |
257 | closedir(dir_bus); |
258 | if (dir != NULL) |
259 | closedir(dir); |
260 | |
261 | return retval; |
262 | } |
263 | |
f359b935 |
264 | static int enable_echo(int enable) |
265 | { |
266 | const char *portname = "/dev/tty"; |
267 | struct termios tty; |
268 | int retval = -1; |
269 | int ret; |
270 | int fd; |
271 | |
272 | memset(&tty, 0, sizeof(tty)); |
273 | |
274 | fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC); |
275 | if (fd < 0) { |
276 | fprintf(stderr, "open %s: ", portname); |
277 | perror(""); |
278 | return 1; |
279 | } |
280 | |
281 | ret = tcgetattr(fd, &tty); |
282 | if (ret != 0) { |
283 | perror("tcgetattr"); |
284 | goto out; |
285 | } |
286 | |
287 | // printf("lflag: 0%o\n", tty.c_lflag); |
288 | if (enable) |
de3f807e |
289 | tty.c_lflag |= ECHO | ICANON; |
290 | else { |
291 | tty.c_lflag &= ~(ECHO | ICANON); |
292 | tty.c_cc[VMIN] = tty.c_cc[VTIME] = 0; |
293 | } |
f359b935 |
294 | |
295 | ret = tcsetattr(fd, TCSANOW, &tty); |
296 | if (ret != 0) { |
297 | perror("tcsetattr"); |
298 | goto out; |
299 | } |
300 | |
301 | retval = 0; |
302 | out: |
303 | close(fd); |
304 | |
305 | return retval; |
306 | } |
307 | |
96b9855a |
308 | static int g_exit; |
309 | |
f359b935 |
310 | static void signal_handler(int sig) |
311 | { |
96b9855a |
312 | g_exit = 1; |
f359b935 |
313 | signal(sig, SIG_DFL); |
f359b935 |
314 | } |
315 | |
1cb2822f |
316 | /* ?0SA 00DU, ?1CB RLDU */ |
317 | #define STATE_BYTES 2 |
318 | |
319 | static uint8_t fixed_input_state[STATE_BYTES] = { 0x33, 0x3f }; |
320 | |
321 | enum mdbtn { |
322 | MDBTN_UP = 1, |
323 | MDBTN_DOWN, |
324 | MDBTN_LEFT, |
325 | MDBTN_RIGHT, |
326 | MDBTN_A, |
327 | MDBTN_B, |
328 | MDBTN_C, |
329 | MDBTN_START, |
330 | }; |
331 | |
332 | static const enum mdbtn evdev_md_map[KEY_CNT] = { |
333 | [KEY_UP] = MDBTN_UP, |
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, |
339 | [KEY_END] = MDBTN_C, |
340 | [KEY_LEFTALT] = MDBTN_START, |
341 | }; |
342 | |
343 | int do_evdev_input(int fd) |
344 | { |
345 | uint8_t old_state[STATE_BYTES]; |
346 | uint8_t changed_bits[STATE_BYTES] = { 0, }; |
347 | struct input_event ev; |
348 | enum mdbtn mdbtn; |
349 | int i, ret; |
350 | |
351 | ret = read(fd, &ev, sizeof(ev)); |
352 | if (ret != sizeof(ev)) { |
353 | fprintf(stderr, "evdev read %d/%zd: ", ret, sizeof(ev)); |
354 | perror(""); |
355 | return 0; |
356 | } |
357 | |
358 | if (ev.type != EV_KEY) |
359 | return 0; |
360 | |
361 | if (ev.value != 0 && ev.value != 1) |
362 | return 0; |
363 | |
364 | if ((uint32_t)ev.code >= ARRAY_SIZE(evdev_md_map)) { |
365 | fprintf(stderr, "evdev read bad key: %u\n", ev.code); |
366 | return 0; |
367 | } |
368 | |
369 | mdbtn = evdev_md_map[ev.code]; |
370 | if (mdbtn == 0) |
371 | return 0; |
372 | |
373 | memcpy(old_state, fixed_input_state, STATE_BYTES); |
374 | |
375 | /* ?0SA 00DU, ?1CB RLDU */ |
376 | switch (mdbtn) { |
377 | case MDBTN_UP: |
378 | changed_bits[0] = 0x01; |
379 | changed_bits[1] = 0x01; |
380 | break; |
381 | case MDBTN_DOWN: |
382 | changed_bits[0] = 0x02; |
383 | changed_bits[1] = 0x02; |
384 | break; |
385 | case MDBTN_LEFT: |
386 | changed_bits[0] = 0x00; |
387 | changed_bits[1] = 0x04; |
388 | break; |
389 | case MDBTN_RIGHT: |
390 | changed_bits[0] = 0x00; |
391 | changed_bits[1] = 0x08; |
392 | break; |
393 | case MDBTN_A: |
394 | changed_bits[0] = 0x10; |
395 | changed_bits[1] = 0x00; |
396 | break; |
397 | case MDBTN_B: |
398 | changed_bits[0] = 0x00; |
399 | changed_bits[1] = 0x10; |
400 | break; |
401 | case MDBTN_C: |
402 | changed_bits[0] = 0x00; |
403 | changed_bits[1] = 0x20; |
404 | break; |
405 | case MDBTN_START: |
406 | changed_bits[0] = 0x20; |
407 | changed_bits[1] = 0x00; |
408 | break; |
409 | } |
410 | |
411 | if (ev.value) { |
412 | // key press |
413 | for (i = 0; i < STATE_BYTES; i++) |
414 | fixed_input_state[i] &= ~changed_bits[i]; |
415 | } |
416 | else { |
417 | // key release |
418 | for (i = 0; i < STATE_BYTES; i++) |
419 | fixed_input_state[i] |= changed_bits[i]; |
420 | } |
421 | |
422 | return memcmp(old_state, fixed_input_state, STATE_BYTES) ? 1 : 0; |
423 | } |
424 | |
ec7c7d4a |
425 | #define MAX_INPUT_BYTES 2 |
426 | |
427 | // TODO: 6btn |
428 | static int tas_data_to_teensy(uint16_t b, uint8_t *data, FILE *logf) |
429 | { |
430 | uint8_t t; |
431 | |
432 | /* SCBA RLDU */ |
433 | /* v */ |
434 | /* ?0SA 00DU, ?1CB RLDU */ |
435 | data[0] = (b & 0x13) | ((b >> 2) & 0x20); |
436 | data[1] = (b & 0x0f) | ((b >> 1) & 0x30); |
437 | |
438 | if (logf != NULL) { |
439 | fwrite(&data[0], 1, 1, logf); |
440 | t = data[1] | 0x40; // expected TH |
441 | fwrite(&t, 1, 1, logf); |
442 | } |
443 | |
444 | return 2; |
445 | } |
446 | |
f20de073 |
447 | struct gmv_tas { |
448 | char sig[15]; |
449 | char ver; |
450 | uint32_t rerecord_count; |
451 | char ctrl1; |
452 | char ctrl2; |
453 | uint16_t flags; |
454 | char name[40]; |
455 | uint8_t data[0][3]; |
456 | }; |
457 | |
ec7c7d4a |
458 | static uint8_t *import_gmv(FILE *f, long size, int *byte_count, FILE *logf) |
01f5cc9e |
459 | { |
460 | struct gmv_tas *gmv; |
ec7c7d4a |
461 | int frame_count; |
462 | int count = 0; |
463 | uint16_t val; |
01f5cc9e |
464 | uint8_t *out; |
465 | int ret; |
466 | int i; |
467 | |
ec7c7d4a |
468 | *byte_count = 0; |
469 | |
01f5cc9e |
470 | if (size < (long)sizeof(*gmv)) { |
471 | fprintf(stderr, "bad gmv size: %ld\n", size); |
472 | return NULL; |
473 | } |
474 | |
475 | gmv = malloc(size); |
476 | if (gmv == NULL) { |
477 | fprintf(stderr, "OOM?\n"); |
478 | return NULL; |
479 | } |
480 | ret = fread(gmv, 1, size, f); |
481 | if (ret != size) { |
482 | fprintf(stderr, "fread %d/%ld: ", ret, size); |
483 | perror(""); |
484 | return NULL; |
485 | } |
486 | |
ec7c7d4a |
487 | frame_count = (size - sizeof(*gmv)) / sizeof(gmv->data[0]); |
01f5cc9e |
488 | |
489 | /* check the GMV.. */ |
ec7c7d4a |
490 | if (frame_count <= 0 || size != sizeof(*gmv) + frame_count * 3) { |
491 | fprintf(stderr, "broken gmv? frames=%d\n", frame_count); |
01f5cc9e |
492 | return NULL; |
493 | } |
494 | |
495 | if (strncmp(gmv->sig, "Gens Movie TEST", 15) != 0) { |
496 | fprintf(stderr, "bad GMV sig\n"); |
497 | return NULL; |
498 | } |
499 | if (gmv->ctrl1 != '3') { |
500 | fprintf(stderr, "unhandled controlled config: '%c'\n", gmv->ctrl1); |
501 | //return NULL; |
502 | } |
503 | if (gmv->ver >= 'A') { |
504 | if (gmv->flags & 0x40) { |
505 | fprintf(stderr, "unhandled flag: movie requires a savestate\n"); |
506 | return NULL; |
507 | } |
508 | if (gmv->flags & 0x20) { |
509 | fprintf(stderr, "unhandled flag: 3-player movie\n"); |
510 | return NULL; |
511 | } |
512 | if (gmv->flags & ~0x80) { |
513 | //fprintf(stderr, "unhandled flag(s): %04x\n", gmv->flags); |
514 | //return 1; |
515 | } |
516 | } |
517 | gmv->name[39] = 0; |
518 | printf("loaded GMV: %s\n", gmv->name); |
519 | printf("%d frames, %u rerecords\n", |
ec7c7d4a |
520 | frame_count, gmv->rerecord_count); |
01f5cc9e |
521 | |
ec7c7d4a |
522 | out = malloc(frame_count * MAX_INPUT_BYTES); |
01f5cc9e |
523 | if (out == NULL) { |
524 | fprintf(stderr, "OOM?\n"); |
525 | return NULL; |
526 | } |
527 | |
ec7c7d4a |
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 + count, logf); |
01f5cc9e |
531 | |
532 | if (gmv->data[i][1] != 0xff || gmv->data[i][2] != 0xff) |
533 | { |
534 | fprintf(stderr, "f %d: unhandled byte(s) %02x %02x\n", |
535 | i, gmv->data[i][1], gmv->data[i][2]); |
536 | } |
537 | } |
538 | |
ec7c7d4a |
539 | *byte_count = count; |
01f5cc9e |
540 | return out; |
541 | } |
542 | |
ec7c7d4a |
543 | static int do_bkm_char(char c, char expect, uint16_t *val, int bit) |
01f5cc9e |
544 | { |
545 | if (c == expect) { |
546 | *val &= ~(1 << bit); |
547 | return 0; |
548 | } |
549 | if (c == '.') |
550 | return 0; |
551 | |
552 | fprintf(stderr, "unexpected bkm char: '%c' instead of '%c'\n", |
553 | c, expect); |
554 | return 1; |
555 | } |
556 | |
ec7c7d4a |
557 | static uint8_t *import_bkm(FILE *f, int *byte_count, FILE *logf) |
01f5cc9e |
558 | { |
ec7c7d4a |
559 | uint8_t *out = NULL; |
560 | uint16_t val; |
561 | int frames = 0; |
01f5cc9e |
562 | int count = 0; |
563 | int alloc = 0; |
564 | int line = 0; |
565 | char buf[256]; |
566 | const char *r; |
567 | char *p; |
568 | int i; |
569 | |
ec7c7d4a |
570 | while ((p = fgets(buf, sizeof(buf), f)) != NULL) |
571 | { |
01f5cc9e |
572 | line++; |
573 | if (p[0] != '|') |
574 | continue; |
575 | |
576 | if (strlen(p) < 30) |
577 | goto unhandled_line; |
578 | if (p[30] != '\r' && p[30] != '\n') |
579 | goto unhandled_line; |
580 | p[30] = 0; |
581 | |
ec7c7d4a |
582 | if (count >= alloc - MAX_INPUT_BYTES) { |
01f5cc9e |
583 | alloc = alloc * 2 + 64; |
584 | out = realloc(out, alloc * sizeof(out[0])); |
585 | if (out == NULL) { |
586 | fprintf(stderr, "OOM?\n"); |
587 | return NULL; |
588 | } |
589 | } |
590 | |
ec7c7d4a |
591 | val = 0xfff; |
01f5cc9e |
592 | |
593 | if (strncmp(p, "|.|", 3) != 0) |
594 | goto unhandled_line; |
595 | p += 3; |
596 | |
597 | const char ref[] = "UDLRABCS"; |
598 | for (r = ref, i = 0; *r != 0; p++, r++, i++) { |
599 | if (do_bkm_char(*p, *r, &val, i)) |
600 | goto unhandled_line; |
601 | } |
602 | |
603 | if (strcmp(p, "....|............||") != 0) |
604 | goto unhandled_line; |
605 | |
ec7c7d4a |
606 | count += tas_data_to_teensy(val, out + count, logf); |
607 | frames++; |
01f5cc9e |
608 | continue; |
609 | |
610 | unhandled_line: |
611 | fprintf(stderr, "unhandled bkm line %d: '%s'\n", line, buf); |
612 | return NULL; |
613 | } |
614 | |
ec7c7d4a |
615 | printf("loaded bkm, %d frames, %d bytes\n", frames, count); |
616 | *byte_count = count; |
617 | return out; |
618 | } |
619 | |
620 | static uint8_t *import_raw(FILE *f, int *byte_count, FILE *logf) |
621 | { |
622 | uint8_t *out = NULL, val; |
623 | int count = 0; |
624 | int alloc = 0; |
625 | int line = 0; |
626 | int first = 1; |
627 | char buf[256]; |
628 | char *p; |
629 | int i; |
630 | |
631 | while ((p = fgets(buf, sizeof(buf), f)) != NULL) |
632 | { |
633 | line++; |
634 | if (p[0] == '#') |
635 | continue; |
636 | if (p[0] != 'e') { |
637 | printf("skipping: %s", p); |
638 | continue; |
639 | } |
640 | |
641 | val = 0; |
642 | p++; |
643 | for (i = 6; i >= 0; i--, p++) { |
644 | if (*p != '0' && *p != '1') |
645 | goto bad; |
646 | if (*p == '1') |
647 | val |= 1 << i; |
648 | } |
649 | if (*p != ' ') |
650 | goto bad; |
651 | |
652 | if (first && (val & 0x40)) |
653 | continue; // XXX.. |
654 | first = 0; |
655 | |
656 | if (count >= alloc) { |
657 | alloc = alloc * 2 + 64; |
658 | out = realloc(out, alloc * sizeof(out[0])); |
659 | if (out == NULL) { |
660 | fprintf(stderr, "OOM?\n"); |
661 | return NULL; |
662 | } |
663 | } |
664 | |
665 | if (logf) |
666 | fwrite(&val, 1, 1, logf); |
667 | |
668 | out[count++] = val & 0x3f; |
669 | continue; |
670 | |
671 | bad: |
672 | fprintf(stderr, "bad raw line %d: '%s'\n", line, buf); |
673 | return NULL; |
674 | } |
675 | |
676 | printf("loaded raw, %d bytes\n", count); |
677 | *byte_count = count; |
01f5cc9e |
678 | return out; |
679 | } |
680 | |
e8e66d02 |
681 | static int write_bkm_frame(FILE *f, const uint8_t *data) |
682 | { |
683 | /* ?0SA 00DU, ?1CB RLDU */ |
684 | static const char ref[] = "UDLRABCSXYZM"; |
685 | static const char bits[] = { 0,1,2,3, 12,4,5,13, 16,16,16,16 }; |
686 | uint32_t idata[2]; |
687 | int p, i; |
688 | |
689 | if (f == NULL) { |
690 | fprintf(stderr, "%s called without outfile\n", __func__); |
691 | goto out; |
692 | } |
693 | |
694 | idata[0] = 0x10000 | (data[0] << 8) | data[1]; |
695 | idata[1] = ~0; |
696 | |
697 | fprintf(f, "|.|"); |
698 | for (p = 0; p < 2; p++) { |
699 | for (i = 0; i < 12; i++) |
700 | fprintf(f, "%c", (idata[p] & (1 << bits[i])) ? '.' : ref[i]); |
701 | fprintf(f, "|"); |
702 | } |
703 | fprintf(f, "|\n"); |
704 | |
705 | out: |
706 | return 2; |
707 | } |
708 | |
f20de073 |
709 | static int submit_urb(int fd, struct usbdevfs_urb *urb, int ep, |
710 | void *buf, size_t buf_size) |
711 | { |
712 | memset(urb, 0, sizeof(*urb)); |
713 | urb->type = USBDEVFS_URB_TYPE_INTERRUPT; |
714 | urb->endpoint = ep; |
715 | urb->buffer = buf; |
716 | urb->buffer_length = buf_size; |
717 | |
718 | return ioctl(fd, USBDEVFS_SUBMITURB, urb); |
719 | } |
720 | |
932302ef |
721 | enum my_urbs { |
722 | URB_DATA_IN, |
723 | URB_DATA_OUT, |
724 | URB_DBG_IN, |
725 | URB_CNT |
726 | }; |
727 | |
61172125 |
728 | static void missing_arg(int a) |
729 | { |
730 | fprintf(stderr, "missing arg: %d\n", a); |
731 | exit(1); |
732 | } |
733 | |
932302ef |
734 | int main(int argc, char *argv[]) |
735 | { |
736 | struct teensy_dev dev; |
737 | struct usbdevfs_urb urb[URB_CNT]; |
738 | struct usbdevfs_urb *reaped_urb; |
0172d3f3 |
739 | int fixed_input_changed = 0; |
1cb2822f |
740 | int evdev_fds[16]; |
741 | int evdev_fd_cnt = 0; |
742 | int evdev_support; |
932302ef |
743 | int wait_device = 0; |
96b9855a |
744 | int pending_urbs = 0; |
1cb2822f |
745 | fd_set rfds, wfds; |
61172125 |
746 | const char *tasfn = NULL; |
e8e66d02 |
747 | const char *outfn = NULL; |
32f257c5 |
748 | const char *logfn = NULL; |
01f5cc9e |
749 | uint8_t *tas_data = NULL; |
ec7c7d4a |
750 | int tas_data_size = 0; |
751 | int bytes_sent = 0; |
37495607 |
752 | int use_vsync = 0; // frame increment on vsync |
6d4349fc |
753 | int no_start_seq = 0; |
61172125 |
754 | int tas_skip = 0; |
f20de073 |
755 | int enable_sent = 0; |
e8e66d02 |
756 | int abort_sent = 0; |
f20de073 |
757 | int frame_count = 0; |
f20de073 |
758 | char buf_dbg[64 + 1]; |
759 | struct tas_pkt pkt_in; |
760 | struct tas_pkt pkt_out; |
761 | struct timeval *timeout = NULL; |
762 | struct timeval tout; |
e8e66d02 |
763 | FILE *outf = NULL; |
32f257c5 |
764 | FILE *logf = NULL; |
96b9855a |
765 | int i, ret = -1; |
1cb2822f |
766 | int fd; |
767 | |
768 | for (i = 1; i < argc; i++) { |
f20de073 |
769 | if (argv[i][0] == '-') { |
61172125 |
770 | switch (argv[i][1] | (argv[i][2] << 8)) { |
771 | case 'm': |
772 | i++; |
773 | if (argv[i] == NULL) |
774 | missing_arg(i); |
775 | tasfn = argv[i]; |
776 | continue; |
e8e66d02 |
777 | case 'w': |
778 | i++; |
779 | if (argv[i] == NULL) |
780 | missing_arg(i); |
781 | outfn = argv[i]; |
782 | continue; |
32f257c5 |
783 | case 'l': |
784 | i++; |
785 | if (argv[i] == NULL) |
786 | missing_arg(i); |
787 | logfn = argv[i]; |
788 | continue; |
61172125 |
789 | case 's': |
790 | i++; |
791 | if (argv[i] == NULL) |
792 | missing_arg(i); |
793 | tas_skip = atoi(argv[i]); |
794 | continue; |
37495607 |
795 | case 'v': |
796 | use_vsync = 1; |
4af6d4e5 |
797 | continue; |
6d4349fc |
798 | case 'n': |
799 | no_start_seq = 1; |
800 | continue; |
61172125 |
801 | default: |
f20de073 |
802 | fprintf(stderr, "bad arg: %s\n", argv[i]); |
803 | return 1; |
804 | } |
f20de073 |
805 | } |
e8e66d02 |
806 | |
807 | /* remaining args are evdev filenames */ |
1cb2822f |
808 | if (evdev_fd_cnt >= ARRAY_SIZE(evdev_fds)) { |
809 | fprintf(stderr, "too many evdevs\n"); |
810 | break; |
811 | } |
f20de073 |
812 | fd = open(argv[i], O_RDONLY); |
1cb2822f |
813 | if (fd == -1) { |
814 | fprintf(stderr, "open %s: ", argv[i]); |
815 | perror(""); |
816 | continue; |
817 | } |
818 | evdev_support = 0; |
f20de073 |
819 | ret = ioctl(fd, EVIOCGBIT(0, sizeof(evdev_support)), |
1cb2822f |
820 | &evdev_support); |
821 | if (ret < 0) |
822 | perror("EVIOCGBIT"); |
823 | if (!(evdev_support & (1 << EV_KEY))) { |
824 | fprintf(stderr, "%s doesn't have keys\n", argv[i]); |
825 | close(fd); |
826 | continue; |
827 | } |
828 | evdev_fds[evdev_fd_cnt++] = fd; |
829 | } |
932302ef |
830 | |
ec7c7d4a |
831 | if (logfn != NULL) { |
832 | logf = fopen(logfn, "wb"); |
833 | if (logf == NULL) { |
834 | fprintf(stderr, "fopen %s: ", logfn); |
835 | perror(""); |
836 | return 1; |
837 | } |
838 | } |
839 | |
f20de073 |
840 | if (tasfn != NULL) { |
01f5cc9e |
841 | const char *ext; |
f20de073 |
842 | long size; |
843 | FILE *f; |
01f5cc9e |
844 | |
f20de073 |
845 | f = fopen(tasfn, "rb"); |
846 | if (f == NULL) { |
847 | fprintf(stderr, "fopen %s: ", tasfn); |
848 | perror(""); |
849 | return 1; |
850 | } |
851 | |
852 | fseek(f, 0, SEEK_END); |
853 | size = ftell(f); |
854 | fseek(f, 0, SEEK_SET); |
01f5cc9e |
855 | if (size <= 0) { |
856 | fprintf(stderr, "bad size: %ld\n", size); |
f20de073 |
857 | return 1; |
858 | } |
01f5cc9e |
859 | |
860 | ext = strrchr(tasfn, '.'); |
861 | if (ext == NULL) |
862 | ext = tasfn; |
863 | else |
864 | ext++; |
865 | |
866 | if (strcasecmp(ext, "gmv") == 0) |
ec7c7d4a |
867 | tas_data = import_gmv(f, size, &tas_data_size, logf); |
01f5cc9e |
868 | else if (strcasecmp(ext, "bkm") == 0) |
ec7c7d4a |
869 | tas_data = import_bkm(f, &tas_data_size, logf); |
870 | else if (strcasecmp(ext, "txt") == 0) |
871 | tas_data = import_raw(f, &tas_data_size, logf); |
01f5cc9e |
872 | else { |
873 | fprintf(stderr, "unknown movie type: '%s'\n", ext); |
f20de073 |
874 | return 1; |
875 | } |
876 | fclose(f); |
f20de073 |
877 | |
ec7c7d4a |
878 | if (logf != NULL) { |
879 | rewind(logf); |
880 | logf = NULL; |
881 | } |
882 | |
01f5cc9e |
883 | if (tas_data == NULL) { |
884 | fprintf(stderr, "failed fo parse %s\n", tasfn); |
f20de073 |
885 | return 1; |
886 | } |
887 | |
61172125 |
888 | if (tas_skip != 0) { |
ec7c7d4a |
889 | // FIXME: no longer a byte |
890 | if (tas_skip >= tas_data_size || tas_skip <= -tas_data_size) { |
891 | printf("skip out of range: %d/%d\n", tas_skip, tas_data_size); |
61172125 |
892 | return 1; |
893 | } |
894 | if (tas_skip > 0) { |
ec7c7d4a |
895 | tas_data_size -= tas_skip; |
01f5cc9e |
896 | memmove(&tas_data[0], &tas_data[tas_skip], |
ec7c7d4a |
897 | sizeof(tas_data[0]) * tas_data_size); |
61172125 |
898 | } |
899 | else { |
01f5cc9e |
900 | tas_data = realloc(tas_data, |
ec7c7d4a |
901 | (tas_data_size - tas_skip) * sizeof(tas_data[0])); |
01f5cc9e |
902 | if (tas_data == NULL) { |
61172125 |
903 | fprintf(stderr, "OOM?\n"); |
904 | return 1; |
905 | } |
01f5cc9e |
906 | memmove(&tas_data[-tas_skip], &tas_data[0], |
ec7c7d4a |
907 | sizeof(tas_data[0]) * tas_data_size); |
01f5cc9e |
908 | memset(&tas_data[0], 0xff, sizeof(tas_data[0]) * -tas_skip); |
ec7c7d4a |
909 | tas_data_size -= tas_skip; |
61172125 |
910 | } |
911 | } |
f20de073 |
912 | } |
913 | |
e8e66d02 |
914 | if (outfn != NULL) { |
915 | outf = fopen(outfn, "w"); |
916 | if (outf == NULL) { |
32f257c5 |
917 | fprintf(stderr, "fopen %s: ", outfn); |
918 | perror(""); |
919 | return 1; |
920 | } |
921 | } |
922 | |
f359b935 |
923 | enable_echo(0); |
924 | signal(SIGINT, signal_handler); |
925 | |
932302ef |
926 | dev.fd = -1; |
927 | |
e8e66d02 |
928 | while (!g_exit || (pending_urbs & (1 << URB_DATA_OUT))) |
932302ef |
929 | { |
930 | if (dev.fd == -1) { |
931 | ret = find_device(&dev, 0x16C0, 0x0486); |
932 | if (ret < 0) |
f359b935 |
933 | break; |
932302ef |
934 | |
935 | if (ret == 0) { |
936 | if (!wait_device) { |
937 | printf("waiting for device..\n"); |
938 | wait_device = 1; |
939 | } |
940 | usleep(250000); |
941 | continue; |
942 | } |
943 | |
944 | wait_device = 0; |
96b9855a |
945 | pending_urbs = 0; |
f20de073 |
946 | enable_sent = 0; |
ec7c7d4a |
947 | bytes_sent = 0; |
f20de073 |
948 | |
949 | /* we wait first, then send commands, but if teensy |
950 | * is started already, it won't send anything */ |
951 | tout.tv_sec = 1; |
952 | tout.tv_usec = 0; |
953 | timeout = &tout; |
932302ef |
954 | } |
955 | |
96b9855a |
956 | if (!(pending_urbs & (1 << URB_DATA_IN))) { |
f20de073 |
957 | memset(&pkt_in, 0, sizeof(pkt_in)); |
958 | ret = submit_urb(dev.fd, &urb[URB_DATA_IN], dev.ifaces[0].ep_in, |
959 | &pkt_in, sizeof(pkt_in)); |
932302ef |
960 | if (ret != 0) { |
961 | perror("USBDEVFS_SUBMITURB URB_DATA_IN"); |
f359b935 |
962 | break; |
932302ef |
963 | } |
f20de073 |
964 | |
96b9855a |
965 | pending_urbs |= 1 << URB_DATA_IN; |
932302ef |
966 | } |
96b9855a |
967 | if (!(pending_urbs & (1 << URB_DBG_IN))) { |
f20de073 |
968 | ret = submit_urb(dev.fd, &urb[URB_DBG_IN], dev.ifaces[1].ep_in, |
969 | buf_dbg, sizeof(buf_dbg) - 1); |
932302ef |
970 | if (ret != 0) { |
971 | perror("USBDEVFS_SUBMITURB URB_DBG_IN"); |
f359b935 |
972 | break; |
932302ef |
973 | } |
f20de073 |
974 | |
96b9855a |
975 | pending_urbs |= 1 << URB_DBG_IN; |
932302ef |
976 | } |
977 | |
1cb2822f |
978 | FD_ZERO(&rfds); |
de3f807e |
979 | FD_SET(STDIN_FILENO, &rfds); |
1cb2822f |
980 | for (i = 0; i < evdev_fd_cnt; i++) |
981 | FD_SET(evdev_fds[i], &rfds); |
982 | |
932302ef |
983 | FD_ZERO(&wfds); |
984 | FD_SET(dev.fd, &wfds); |
985 | |
f20de073 |
986 | ret = select(dev.fd + 1, &rfds, &wfds, NULL, timeout); |
932302ef |
987 | if (ret < 0) { |
988 | perror("select"); |
f359b935 |
989 | break; |
932302ef |
990 | } |
f20de073 |
991 | timeout = NULL; |
932302ef |
992 | |
de3f807e |
993 | /* sometihng form stdin? */ |
de3f807e |
994 | if (FD_ISSET(STDIN_FILENO, &rfds)) { |
995 | char c = 0; |
996 | ret = read(STDIN_FILENO, &c, 1); |
997 | if (ret <= 0) { |
998 | perror("read stdin"); |
999 | break; |
1000 | } |
1001 | |
1002 | switch (c) { |
1003 | case 'r': |
1004 | enable_sent = 0; |
1005 | break; |
1006 | } |
1007 | } |
1008 | |
e8e66d02 |
1009 | /* something from input devices? */ |
1cb2822f |
1010 | for (i = 0; i < evdev_fd_cnt; i++) { |
1011 | if (FD_ISSET(evdev_fds[i], &rfds)) { |
1012 | fixed_input_changed |= |
1013 | do_evdev_input(evdev_fds[i]); |
1014 | } |
1015 | } |
1016 | |
1017 | /* something from USB? */ |
f20de073 |
1018 | if (FD_ISSET(dev.fd, &wfds)) |
1019 | { |
96b9855a |
1020 | unsigned int which_urb; |
1021 | |
932302ef |
1022 | reaped_urb = NULL; |
1023 | ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb); |
1024 | if (ret != 0) { |
1025 | if (errno == ENODEV) |
1026 | goto dev_close; |
1027 | perror("USBDEVFS_REAPURB"); |
f359b935 |
1028 | break; |
932302ef |
1029 | } |
96b9855a |
1030 | which_urb = reaped_urb - urb; |
1031 | if (which_urb < ARRAY_SIZE(urb)) |
1032 | pending_urbs &= ~(1 << which_urb); |
1033 | else { |
1034 | fprintf(stderr, "reaped unknown urb: %p #%u", |
1035 | reaped_urb, which_urb); |
1036 | } |
932302ef |
1037 | |
1038 | if (reaped_urb != NULL && reaped_urb->status != 0) { |
1039 | errno = -reaped_urb->status; |
96b9855a |
1040 | fprintf(stderr, "urb #%u: ", which_urb); |
f20de073 |
1041 | perror(""); |
932302ef |
1042 | if (reaped_urb->status == -EILSEQ) { |
1043 | /* this is usually a sign of disconnect.. */ |
1044 | usleep(250000); |
1045 | goto dev_close; |
1046 | } |
1047 | } |
96b9855a |
1048 | else if (reaped_urb == &urb[URB_DATA_IN]) |
1049 | { |
f20de073 |
1050 | /* some request from teensy */ |
f20de073 |
1051 | switch (pkt_in.type) { |
1052 | case PKT_STREAM_REQ: |
ec7c7d4a |
1053 | printf("req: %d/%d/%d\n", pkt_in.req.frame * 2, |
1054 | bytes_sent, tas_data_size); |
f20de073 |
1055 | |
e8e66d02 |
1056 | pkt_out.size = 0; |
ec7c7d4a |
1057 | if (bytes_sent < tas_data_size) { |
e8e66d02 |
1058 | pkt_out.type = PKT_STREAM_DATA_TO; |
1059 | |
ec7c7d4a |
1060 | i = tas_data_size - bytes_sent; |
1061 | if (i > sizeof(pkt_out.data)) |
1062 | i = sizeof(pkt_out.data); |
1063 | memcpy(pkt_out.data, tas_data + bytes_sent, i); |
1064 | bytes_sent += i; |
e8e66d02 |
1065 | pkt_out.size = i; |
f20de073 |
1066 | } |
32f257c5 |
1067 | else { |
f20de073 |
1068 | pkt_out.type = PKT_STREAM_END; |
32f257c5 |
1069 | } |
f20de073 |
1070 | |
1071 | ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], |
1072 | dev.ifaces[0].ep_out, &pkt_out, sizeof(pkt_out)); |
1073 | if (ret != 0) |
e8e66d02 |
1074 | perror("USBDEVFS_SUBMITURB PKT_STREAM_DATA_TO"); |
1075 | break; |
1076 | |
1077 | case PKT_STREAM_DATA_FROM: |
1078 | printf("f: %d\n", frame_count); |
1079 | if (pkt_in.size == 0 || pkt_in.size > sizeof(pkt_out.data)) { |
1080 | printf("host: got bad DATA_FROM size: %u\n", pkt_in.size); |
1081 | break; |
1082 | } |
1083 | for (i = 0; i < pkt_in.size; ) { |
1084 | i += write_bkm_frame(outf, pkt_in.data + i); |
1085 | frame_count++; |
1086 | } |
f20de073 |
1087 | break; |
1088 | |
1089 | default: |
1090 | printf("host: got unknown pkt type: %04x\n", pkt_in.type); |
1091 | break; |
1092 | } |
932302ef |
1093 | } |
96b9855a |
1094 | else if (reaped_urb == &urb[URB_DATA_OUT]) |
1095 | { |
1cb2822f |
1096 | } |
96b9855a |
1097 | else if (reaped_urb == &urb[URB_DBG_IN]) |
1098 | { |
932302ef |
1099 | /* debug text */ |
1100 | buf_dbg[reaped_urb->actual_length] = 0; |
1101 | printf("%s", buf_dbg); |
e4d23548 |
1102 | |
1103 | // continue receiving debug before sending out stuff |
1104 | tout.tv_sec = 0; |
1105 | tout.tv_usec = 1000; |
1106 | timeout = &tout; |
1107 | continue; |
932302ef |
1108 | } |
1109 | else { |
f20de073 |
1110 | fprintf(stderr, "reaped unknown urb? %p #%zu\n", |
1111 | reaped_urb, reaped_urb - urb); |
932302ef |
1112 | } |
1113 | } |
1cb2822f |
1114 | |
1115 | /* something to send? */ |
96b9855a |
1116 | if (pending_urbs & (1 << URB_DATA_OUT)) |
1117 | // can't do that yet |
1118 | continue; |
1119 | |
e8e66d02 |
1120 | if ((tas_data != NULL || outf != NULL) && !enable_sent) { |
f20de073 |
1121 | memset(&pkt_out, 0, sizeof(pkt_out)); |
1122 | pkt_out.type = PKT_STREAM_ENABLE; |
e8e66d02 |
1123 | pkt_out.enable.stream_to = (tas_data != NULL); |
1124 | pkt_out.enable.stream_from = (outf != NULL); |
37495607 |
1125 | pkt_out.enable.use_readinc = !use_vsync; |
6d4349fc |
1126 | pkt_out.enable.no_start_seq = no_start_seq; |
4af6d4e5 |
1127 | |
f20de073 |
1128 | ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out, |
1129 | &pkt_out, sizeof(pkt_out)); |
1130 | if (ret != 0) { |
1131 | perror("USBDEVFS_SUBMITURB PKT_STREAM_ENABLE"); |
1132 | continue; |
1133 | } |
e8e66d02 |
1134 | pending_urbs |= 1 << URB_DATA_OUT; |
f20de073 |
1135 | enable_sent = 1; |
ec7c7d4a |
1136 | bytes_sent = 0; |
e8e66d02 |
1137 | continue; |
f20de073 |
1138 | } |
01f5cc9e |
1139 | if (tas_data == NULL && fixed_input_changed) { |
f20de073 |
1140 | memset(&pkt_out, 0, sizeof(pkt_out)); |
1141 | pkt_out.type = PKT_FIXED_STATE; |
1142 | memcpy(pkt_out.data, fixed_input_state, sizeof(fixed_input_state)); |
1cb2822f |
1143 | |
f20de073 |
1144 | ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out, |
1145 | &pkt_out, sizeof(pkt_out)); |
1cb2822f |
1146 | if (ret != 0) { |
e8e66d02 |
1147 | perror("USBDEVFS_SUBMITURB PKT_FIXED_STATE"); |
1148 | break; |
1149 | } |
0172d3f3 |
1150 | fixed_input_changed = 0; |
e8e66d02 |
1151 | pending_urbs |= 1 << URB_DATA_OUT; |
1152 | continue; |
1153 | } |
1154 | if (g_exit && !abort_sent) { |
1155 | memset(&pkt_out, 0, sizeof(pkt_out)); |
1156 | pkt_out.type = PKT_STREAM_ABORT; |
1157 | |
1158 | ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out, |
1159 | &pkt_out, sizeof(pkt_out)); |
1160 | if (ret != 0) { |
1161 | perror("USBDEVFS_SUBMITURB PKT_STREAM_ABORT"); |
f359b935 |
1162 | break; |
1cb2822f |
1163 | } |
96b9855a |
1164 | pending_urbs |= 1 << URB_DATA_OUT; |
e8e66d02 |
1165 | abort_sent = 1; |
96b9855a |
1166 | continue; |
1cb2822f |
1167 | } |
1168 | |
932302ef |
1169 | continue; |
1170 | |
1171 | dev_close: |
1172 | close(dev.fd); |
1173 | dev.fd = -1; |
1174 | } |
1175 | |
f359b935 |
1176 | enable_echo(1); |
1177 | |
e8e66d02 |
1178 | if (outf != NULL) |
1179 | fclose(outf); |
1180 | |
96b9855a |
1181 | if (dev.fd != -1) { |
1182 | /* deal with pending URBs */ |
1183 | if (pending_urbs & (1 << URB_DATA_IN)) |
1184 | ioctl(dev.fd, USBDEVFS_DISCARDURB, &urb[URB_DATA_IN]); |
1185 | if (pending_urbs & (1 << URB_DBG_IN)) |
1186 | ioctl(dev.fd, USBDEVFS_DISCARDURB, &urb[URB_DBG_IN]); |
1187 | for (i = 0; i < URB_CNT; i++) { |
1188 | if (pending_urbs & (1 << i)) { |
1189 | ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb); |
1190 | if (ret != 0) |
1191 | perror("USBDEVFS_REAPURB"); |
1192 | } |
1193 | } |
1194 | |
1195 | close(dev.fd); |
1196 | } |
1197 | |
f359b935 |
1198 | return ret; |
932302ef |
1199 | } |
1200 | |
1201 | // vim: ts=2:sw=2:expandtab |