1 // Intensity Shuttle USB3 prototype capture driver, v0.3
2 // Can download 8-bit and 10-bit UYVY/v210 frames from HDMI, quite stable
3 // (can do captures for hours at a time with no drops), except during startup
4 // 576p60/720p60/1080i60 works, 1080p60 does not work (firmware limitation)
5 // Audio comes out as 8-channel 24-bit raw audio.
10 #include <arpa/inet.h>
21 #include <condition_variable>
29 static int current_register = 0;
31 #define NUM_REGISTERS 60
32 uint8_t register_file[NUM_REGISTERS];
35 #define HEIGHT 750 /* 30 lines ancillary data? */
37 //#define HEIGHT 1125 /* ??? lines ancillary data? */
38 #define HEADER_SIZE 44
39 //#define HEADER_SIZE 0
40 #define AUDIO_HEADER_SIZE 4
42 //#define FRAME_SIZE (WIDTH * HEIGHT * 2 + HEADER_SIZE) // UYVY
43 //#define FRAME_SIZE (WIDTH * HEIGHT * 2 * 4 / 3 + HEADER_SIZE) // v210
44 #define FRAME_SIZE (8 << 20)
48 FrameAllocator::Frame current_video_frame;
49 FrameAllocator::Frame current_audio_frame;
54 FrameAllocator::Frame frame;
58 condition_variable queues_not_empty;
59 deque<QueuedFrame> pending_video_frames;
60 deque<QueuedFrame> pending_audio_frames;
63 atomic<bool> should_quit;
65 FrameAllocator::~FrameAllocator() {}
67 #define NUM_QUEUED_FRAMES 8
68 class MallocFrameAllocator : public FrameAllocator {
70 MallocFrameAllocator(size_t frame_size);
71 Frame alloc_frame() override;
72 void release_frame(Frame frame) override;
78 stack<unique_ptr<uint8_t[]>> freelist; // All of size <frame_size>.
81 MallocFrameAllocator::MallocFrameAllocator(size_t frame_size)
82 : frame_size(frame_size)
84 for (int i = 0; i < NUM_QUEUED_FRAMES; ++i) {
85 freelist.push(unique_ptr<uint8_t[]>(new uint8_t[frame_size]));
89 FrameAllocator::Frame MallocFrameAllocator::alloc_frame()
94 unique_lock<mutex> lock(freelist_mutex); // Meh.
95 if (freelist.empty()) {
96 printf("Frame overrun (no more spare frames of size %ld), dropping frame!\n",
99 vf.data = freelist.top().release();
100 vf.size = frame_size;
101 freelist.pop(); // Meh.
106 void MallocFrameAllocator::release_frame(Frame frame)
108 unique_lock<mutex> lock(freelist_mutex);
109 freelist.push(unique_ptr<uint8_t[]>(frame.data));
112 FrameAllocator *video_frame_allocator = nullptr;
113 FrameAllocator *audio_frame_allocator = nullptr;
114 frame_callback_t frame_callback = nullptr;
116 bool uint16_less_than_with_wraparound(uint16_t a, uint16_t b)
121 return (b - a < 0x8000);
123 int wrap_b = 0x10000 + int(b);
124 return (wrap_b - a < 0x8000);
128 void queue_frame(uint16_t format, uint16_t timecode, FrameAllocator::Frame frame, deque<QueuedFrame> *q)
130 if (!q->empty() && !uint16_less_than_with_wraparound(q->back().timecode, timecode)) {
131 printf("Blocks going backwards: prev=0x%04x, cur=0x%04x (dropped)\n",
132 q->back().timecode, timecode);
133 frame.owner->release_frame(frame);
139 qf.timecode = timecode;
143 unique_lock<mutex> lock(queue_lock);
144 q->push_back(move(qf));
146 queues_not_empty.notify_one(); // might be spurious
149 void dump_frame(const char *filename, uint8_t *frame_start, size_t frame_len)
151 FILE *fp = fopen(filename, "wb");
152 if (fwrite(frame_start + HEADER_SIZE, frame_len - HEADER_SIZE, 1, fp) != 1) {
153 printf("short write!\n");
158 void dump_audio_block(uint8_t *audio_start, size_t audio_len)
160 fwrite(audio_start + AUDIO_HEADER_SIZE, 1, audio_len - AUDIO_HEADER_SIZE, audiofp);
163 void dequeue_thread()
166 unique_lock<mutex> lock(queue_lock);
167 queues_not_empty.wait(lock, []{ return !pending_video_frames.empty() && !pending_audio_frames.empty(); });
169 uint16_t video_timecode = pending_video_frames.front().timecode;
170 uint16_t audio_timecode = pending_audio_frames.front().timecode;
171 if (video_timecode < audio_timecode) {
172 printf("Video block 0x%04x without corresponding audio block, dropping.\n",
174 video_frame_allocator->release_frame(pending_video_frames.front().frame);
175 pending_video_frames.pop_front();
176 } else if (audio_timecode < video_timecode) {
177 printf("Audio block 0x%04x without corresponding video block, dropping.\n",
179 audio_frame_allocator->release_frame(pending_audio_frames.front().frame);
180 pending_audio_frames.pop_front();
182 QueuedFrame video_frame = pending_video_frames.front();
183 QueuedFrame audio_frame = pending_audio_frames.front();
184 pending_audio_frames.pop_front();
185 pending_video_frames.pop_front();
190 snprintf(filename, sizeof(filename), "%04x%04x.uyvy", video_frame.format, video_timecode);
191 dump_frame(filename, video_frame.frame.data, video_frame.data_len);
192 dump_audio_block(audio_frame.frame.data, audio_frame.data_len);
195 frame_callback(video_timecode,
196 video_frame.frame, HEADER_SIZE, video_frame.format,
197 audio_frame.frame, AUDIO_HEADER_SIZE, audio_frame.format);
202 void start_new_frame(const uint8_t *start)
204 uint16_t format = (start[3] << 8) | start[2];
205 uint16_t timecode = (start[1] << 8) | start[0];
207 if (current_video_frame.len > 0) {
209 queue_frame(format, timecode, current_video_frame, &pending_video_frames);
211 //printf("Found frame start, format 0x%04x timecode 0x%04x, previous frame length was %d/%d\n",
213 // //start[7], start[6], start[5], start[4],
214 // read_current_frame, FRAME_SIZE);
216 current_video_frame = video_frame_allocator->alloc_frame();
217 //if (current_video_frame.data == nullptr) {
218 // read_current_frame = -1;
220 // read_current_frame = 0;
224 void start_new_audio_block(const uint8_t *start)
226 uint16_t format = (start[3] << 8) | start[2];
227 uint16_t timecode = (start[1] << 8) | start[0];
228 if (current_audio_frame.len > 0) {
229 //dump_audio_block();
230 queue_frame(format, timecode, current_audio_frame, &pending_audio_frames);
232 //printf("Found audio block start, format 0x%04x timecode 0x%04x, previous block length was %d\n",
233 // format, timecode, read_current_audio_block);
234 current_audio_frame = audio_frame_allocator->alloc_frame();
238 static void dump_pack(const libusb_transfer *xfr, int offset, const libusb_iso_packet_descriptor *pack)
240 // printf("ISO pack%u length:%u, actual_length:%u, offset:%u\n", i, pack->length, pack->actual_length, offset);
241 for (unsigned j = 0; j < pack->actual_length; j++) {
242 //for (int j = 0; j < min(pack->actual_length, 16u); j++) {
243 printf("%02x", xfr->buffer[j + offset]);
246 else if ((j % 8) == 7)
254 void add_to_frame(FrameAllocator::Frame *current_frame, const char *frame_type_name, const uint8_t *start, const uint8_t *end)
256 if (current_frame->data == nullptr ||
257 current_frame->len > current_video_frame.size ||
262 int bytes = end - start;
263 if (current_frame->len + bytes > current_frame->size) {
264 printf("%d bytes overflow after last %s frame\n",
265 int(current_frame->len + bytes - current_frame->size), frame_type_name);
268 memcpy(current_frame->data + current_frame->len, start, bytes);
269 current_frame->len += bytes;
273 void decode_packs(const libusb_transfer *xfr,
274 const char *sync_pattern,
276 FrameAllocator::Frame *current_frame,
277 const char *frame_type_name,
278 function<void(const uint8_t *start)> start_callback)
281 for (int i = 0; i < xfr->num_iso_packets; i++) {
282 const libusb_iso_packet_descriptor *pack = &xfr->iso_packet_desc[i];
284 if (pack->status != LIBUSB_TRANSFER_COMPLETED) {
285 fprintf(stderr, "Error: pack %u/%u status %d\n", i, xfr->num_iso_packets, pack->status);
290 const uint8_t *start = xfr->buffer + offset;
291 const uint8_t *limit = start + pack->actual_length;
292 while (start < limit) { // Usually runs only one iteration.
293 const unsigned char* start_next_frame = (const unsigned char *)memmem(start, limit - start, sync_pattern, sync_length);
294 if (start_next_frame == nullptr) {
295 // add the rest of the buffer
296 add_to_frame(current_frame, frame_type_name, start, limit);
299 add_to_frame(current_frame, frame_type_name, start, start_next_frame);
300 start = start_next_frame + sync_length; // skip sync
301 start_callback(start);
305 dump_pack(xfr, offset, pack);
307 offset += pack->length;
311 static void cb_xfr(struct libusb_transfer *xfr)
313 if (xfr->status != LIBUSB_TRANSFER_COMPLETED) {
314 fprintf(stderr, "transfer status %d\n", xfr->status);
315 libusb_free_transfer(xfr);
319 if (xfr->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) {
320 if (xfr->endpoint == 0x84) {
321 decode_packs(xfr, "DeckLinkAudioResyncT", 20, ¤t_audio_frame, "audio", start_new_audio_block);
323 decode_packs(xfr, "\x00\x00\xff\xff", 4, ¤t_video_frame, "video", start_new_frame);
326 if (xfr->type == LIBUSB_TRANSFER_TYPE_CONTROL) {
327 //const libusb_control_setup *setup = libusb_control_transfer_get_setup(xfr);
328 uint8_t *buf = libusb_control_transfer_get_data(xfr);
330 if (setup->wIndex == 44) {
331 printf("read timer register: 0x%02x%02x%02x%02x\n", buf[0], buf[1], buf[2], buf[3]);
333 printf("read register %2d: 0x%02x%02x%02x%02x\n",
334 setup->wIndex, buf[0], buf[1], buf[2], buf[3]);
337 memcpy(register_file + current_register, buf, 4);
338 current_register = (current_register + 4) % NUM_REGISTERS;
339 if (current_register == 0) {
340 // read through all of them
341 printf("register dump:");
342 for (int i = 0; i < NUM_REGISTERS; i += 4) {
343 printf(" 0x%02x%02x%02x%02x", register_file[i], register_file[i + 1], register_file[i + 2], register_file[i + 3]);
347 libusb_fill_control_setup(xfr->buffer,
348 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN, /*request=*/214, /*value=*/0,
349 /*index=*/current_register, /*length=*/4);
354 printf("length:%u, actual_length:%u\n", xfr->length, xfr->actual_length);
355 for (i = 0; i < xfr->actual_length; i++) {
356 printf("%02x", xfr->buffer[i]);
366 if (libusb_submit_transfer(xfr) < 0) {
367 fprintf(stderr, "error re-submitting URB\n");
372 void usb_thread_func()
374 printf("usb thread started\n");
377 memset(¶m, 0, sizeof(param));
378 param.sched_priority = 1;
379 if (sched_setscheduler(0, SCHED_RR, ¶m) == -1) {
380 printf("couldn't set realtime priority for USB thread: %s\n", strerror(errno));
382 while (!should_quit) {
383 int rc = libusb_handle_events(nullptr);
384 if (rc != LIBUSB_SUCCESS)
389 FrameAllocator *get_video_frame_allocator()
391 return video_frame_allocator;
394 void set_video_frame_allocator(FrameAllocator *allocator)
396 video_frame_allocator = allocator;
399 FrameAllocator *get_audio_frame_allocator()
401 return audio_frame_allocator;
404 void set_audio_frame_allocator(FrameAllocator *allocator)
406 audio_frame_allocator = allocator;
409 void set_frame_callback(frame_callback_t callback)
411 frame_callback = callback;
414 void start_bm_capture()
416 if (video_frame_allocator == nullptr) {
417 set_video_frame_allocator(new MallocFrameAllocator(FRAME_SIZE)); // FIXME: leak.
419 if (audio_frame_allocator == nullptr) {
420 set_audio_frame_allocator(new MallocFrameAllocator(65536)); // FIXME: leak.
422 thread(dequeue_thread).detach();
425 struct libusb_transfer *xfr;
426 vector<libusb_transfer *> iso_xfrs;
428 rc = libusb_init(nullptr);
430 fprintf(stderr, "Error initializing libusb: %s\n", libusb_error_name(rc));
434 struct libusb_device_handle *devh = libusb_open_device_with_vid_pid(nullptr, 0x1edb, 0xbd3b);
436 fprintf(stderr, "Error finding USB device\n");
440 libusb_config_descriptor *config;
441 rc = libusb_get_config_descriptor(libusb_get_device(devh), /*config_index=*/0, &config);
443 fprintf(stderr, "Error getting configuration: %s\n", libusb_error_name(rc));
446 printf("%d interface\n", config->bNumInterfaces);
447 for (int interface_number = 0; interface_number < config->bNumInterfaces; ++interface_number) {
448 printf(" interface %d\n", interface_number);
449 const libusb_interface *interface = &config->interface[interface_number];
450 for (int altsetting = 0; altsetting < interface->num_altsetting; ++altsetting) {
451 printf(" alternate setting %d\n", altsetting);
452 const libusb_interface_descriptor *interface_desc = &interface->altsetting[altsetting];
453 for (int endpoint_number = 0; endpoint_number < interface_desc->bNumEndpoints; ++endpoint_number) {
454 const libusb_endpoint_descriptor *endpoint = &interface_desc->endpoint[endpoint_number];
455 printf(" endpoint address 0x%02x\n", endpoint->bEndpointAddress);
460 rc = libusb_set_configuration(devh, /*configuration=*/1);
462 fprintf(stderr, "Error setting configuration 1: %s\n", libusb_error_name(rc));
466 rc = libusb_claim_interface(devh, 0);
468 fprintf(stderr, "Error claiming interface 0: %s\n", libusb_error_name(rc));
472 // Alternate setting 1 is output, alternate setting 2 is input.
473 // Card is reset when switching alternates, so the driver uses
474 // this “double switch” when it wants to reset.
475 rc = libusb_set_interface_alt_setting(devh, /*interface=*/0, /*alternate_setting=*/1);
477 fprintf(stderr, "Error setting alternate 1: %s\n", libusb_error_name(rc));
480 rc = libusb_set_interface_alt_setting(devh, /*interface=*/0, /*alternate_setting=*/2);
482 fprintf(stderr, "Error setting alternate 1: %s\n", libusb_error_name(rc));
486 rc = libusb_set_interface_alt_setting(devh, /*interface=*/0, /*alternate_setting=*/1);
488 fprintf(stderr, "Error setting alternate 1: %s\n", libusb_error_name(rc));
494 rc = libusb_claim_interface(devh, 3);
496 fprintf(stderr, "Error claiming interface 3: %s\n", libusb_error_name(rc));
502 // 44 is some kind of timer register (first 16 bits count upwards)
503 // 24 is some sort of watchdog?
504 // you can seemingly set it to 0x73c60001 and that bit will eventually disappear
505 // (or will go to 0x73c60010?), also seen 0x73c60100
506 // 12 also changes all the time, unclear why
507 // 16 seems to be autodetected mode somehow
508 // -- this is e00115e0 after reset?
509 // ed0115e0 after mode change [to output?]
510 // 2d0015e0 after more mode change [to input]
511 // ed0115e0 after more mode change
512 // 2d0015e0 after more mode change
514 // 390115e0 seems to indicate we have signal
515 // changes to 200115e0 when resolution changes/we lose signal, driver resets after a while
517 // 200015e0 on startup
518 // changes to 250115e0 when we sync to the signal
520 // so only first 16 bits count, and 0x0100 is a mask for ok/stable signal?
522 // 28 and 32 seems to be analog audio input levels (one byte for each of the eight channels).
523 // however, if setting 32 with HDMI embedded audio, it is immediately overwritten back (to 0xe137002a).
525 // 4, 8, 20 are unclear. seem to be some sort of bitmask, but we can set them to 0 with no apparent effect.
526 // perhaps some of them are related to analog output?
528 // 36 can be set to 0 with no apparent effect (all of this tested on both video and audio),
529 // but the driver sets it to 0x8036802a at some point.
532 // first byte is 0x39 for a stable 576p60 signal, 0x2d for a stable 720p60 signal, 0x20 for no signal
535 // 0x01 - stable signal
537 // 0x08 - unknown (audio??)
547 static const ctrl ctrls[] = {
548 { LIBUSB_ENDPOINT_IN, 214, 16, 0 },
549 { LIBUSB_ENDPOINT_IN, 214, 0, 0 },
550 { LIBUSB_ENDPOINT_IN, 214, 0, 0 },
551 { LIBUSB_ENDPOINT_IN, 214, 4, 0 },
552 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
553 { LIBUSB_ENDPOINT_IN, 214, 16, 0 },
554 { LIBUSB_ENDPOINT_IN, 214, 20, 0 },
555 { LIBUSB_ENDPOINT_IN, 214, 24, 0 },
556 { LIBUSB_ENDPOINT_IN, 214, 28, 0 },
557 { LIBUSB_ENDPOINT_IN, 215, 32, 0 },
558 { LIBUSB_ENDPOINT_IN, 214, 36, 0 },
559 { LIBUSB_ENDPOINT_IN, 214, 40, 0 },
560 { LIBUSB_ENDPOINT_IN, 216, 44, 0 },
561 { LIBUSB_ENDPOINT_IN, 214, 48, 0 },
562 { LIBUSB_ENDPOINT_IN, 214, 52, 0 },
563 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
564 { LIBUSB_ENDPOINT_IN, 214, 40, 0 },
565 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
566 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
567 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
568 { LIBUSB_ENDPOINT_IN, 214, 40, 0 },
569 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
570 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
571 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
572 { LIBUSB_ENDPOINT_IN, 214, 40, 0 },
573 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
574 { LIBUSB_ENDPOINT_IN, 214, 24, 0 },
575 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
576 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
577 { LIBUSB_ENDPOINT_IN, 214, 40, 0 },
578 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
579 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
580 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
581 { LIBUSB_ENDPOINT_IN, 214, 40, 0 },
582 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
583 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
584 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
585 { LIBUSB_ENDPOINT_IN, 214, 40, 0 },
586 { LIBUSB_ENDPOINT_IN, 214, 24, 0 },
587 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
588 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
589 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
590 { LIBUSB_ENDPOINT_IN, 214, 40, 0 },
591 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
592 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
593 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
594 { LIBUSB_ENDPOINT_IN, 214, 40, 0 },
595 { LIBUSB_ENDPOINT_IN, 214, 12, 0 }, // packet 354
596 { LIBUSB_ENDPOINT_IN, 214, 24, 0 },
597 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
598 { LIBUSB_ENDPOINT_IN, 214, 12, 0 },
599 { LIBUSB_ENDPOINT_IN, 214, 40, 0 },
601 //{ LIBUSB_ENDPOINT_OUT, 215, 0, 0x80000100 },
602 //{ LIBUSB_ENDPOINT_OUT, 215, 0, 0x09000000 }, // wow, some kind of mode
604 // seems to capture on HDMI, clearing the 0x20000000 bit seems to activate 10-bit
606 // clearing the 0x08000000 bit seems to change the capture format (other source?)
607 // 0x10000000 = analog audio instead of embedded audio, it seems
608 // 0x3a000000 = component video? (analog audio)
609 // 0x3c000000 = composite video? (analog audio)
610 // 0x3e000000 = s-video? (analog audio)
611 { LIBUSB_ENDPOINT_OUT, 215, 0, 0x29000000 },
612 //{ LIBUSB_ENDPOINT_OUT, 215, 0, 0x09000000 },
614 //{ LIBUSB_ENDPOINT_OUT, 215, 28, 0xffffffff },
615 //{ LIBUSB_ENDPOINT_OUT, 215, 32, 0xffffffff },
616 //{ LIBUSB_ENDPOINT_OUT, 215, 28, 0x40404040 },
617 //{ LIBUSB_ENDPOINT_OUT, 215, 32, 0x40404040 },
618 //{ LIBUSB_ENDPOINT_OUT, 215, 36, 0x8036802a },
619 { LIBUSB_ENDPOINT_OUT, 215, 24, 0x73c60001 }, // latch for frame start?
620 //{ LIBUSB_ENDPOINT_OUT, 215, 24, 0x13370001 }, // latch for frame start?
621 { LIBUSB_ENDPOINT_IN, 214, 24, 0 }, //
622 //{ LIBUSB_ENDPOINT_OUT, 215, 4, 0x00000000 }, // appears to have no e fect
623 //{ LIBUSB_ENDPOINT_OUT, 215, 8, 0x00000000 }, // appears to have no effect
624 //{ LIBUSB_ENDPOINT_OUT, 215, 20, 0x00000000 }, // appears to have no effect
625 //{ LIBUSB_ENDPOINT_OUT, 215, 28, 0x00000000 }, // appears to have no effect
626 //{ LIBUSB_ENDPOINT_OUT, 215, 32, 0x00000000 }, // appears to have no effect
627 //{ LIBUSB_ENDPOINT_OUT, 215, 36, 0x00000000 }, // appears to have no effect
629 { LIBUSB_ENDPOINT_OUT, 215, 0 },
630 { LIBUSB_ENDPOINT_OUT, 215, 0 },
631 { LIBUSB_ENDPOINT_OUT, 215, 28 },
632 { LIBUSB_ENDPOINT_OUT, 215, 32 },
633 { LIBUSB_ENDPOINT_OUT, 215, 36 },
634 { LIBUSB_ENDPOINT_OUT, 215, 24 },
635 { LIBUSB_ENDPOINT_OUT, 215, 24 },
636 { LIBUSB_ENDPOINT_OUT, 215, 24 },
637 { LIBUSB_ENDPOINT_OUT, 215, 24 },
638 { LIBUSB_ENDPOINT_OUT, 215, 24 },
639 { LIBUSB_ENDPOINT_OUT, 215, 24 },
640 { LIBUSB_ENDPOINT_OUT, 215, 24 },
641 { LIBUSB_ENDPOINT_OUT, 215, 24 },
642 { LIBUSB_ENDPOINT_OUT, 215, 24 },
643 { LIBUSB_ENDPOINT_OUT, 215, 24 },
644 { LIBUSB_ENDPOINT_OUT, 215, 0 },
645 { LIBUSB_ENDPOINT_OUT, 215, 24 },
646 { LIBUSB_ENDPOINT_OUT, 215, 24 },
647 { LIBUSB_ENDPOINT_OUT, 215, 24 },
648 { LIBUSB_ENDPOINT_OUT, 215, 24 },
649 { LIBUSB_ENDPOINT_OUT, 215, 24 },
650 { LIBUSB_ENDPOINT_OUT, 215, 24 },
654 for (unsigned req = 0; req < sizeof(ctrls) / sizeof(ctrls[0]); ++req) {
655 uint32_t flipped = htonl(ctrls[req].data);
656 static uint8_t value[4];
657 memcpy(value, &flipped, sizeof(flipped));
658 int size = sizeof(value);
659 //if (ctrls[req].request == 215) size = 0;
660 rc = libusb_control_transfer(devh, LIBUSB_REQUEST_TYPE_VENDOR | ctrls[req].endpoint,
661 /*request=*/ctrls[req].request, /*value=*/0, /*index=*/ctrls[req].index, value, size, /*timeout=*/0);
663 fprintf(stderr, "Error on control %d: %s\n", ctrls[req].index, libusb_error_name(rc));
667 printf("rc=%d: ep=%d@%d %d -> 0x", rc, ctrls[req].endpoint, ctrls[req].request, ctrls[req].index);
668 for (int i = 0; i < rc; ++i) {
669 printf("%02x", value[i]);
677 static int my_index = 0;
678 static uint8_t value[4];
679 int size = sizeof(value);
680 rc = libusb_control_transfer(devh, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN,
681 /*request=*/214, /*value=*/0, /*index=*/my_index, value, size, /*timeout=*/0);
683 fprintf(stderr, "Error on control\n");
686 printf("rc=%d index=%d: 0x", rc, my_index);
687 for (int i = 0; i < rc; ++i) {
688 printf("%02x", value[i]);
695 // set up an asynchronous transfer of the timer register
696 static uint8_t cmdbuf[LIBUSB_CONTROL_SETUP_SIZE + 4];
697 static int completed = 0;
699 xfr = libusb_alloc_transfer(0);
700 libusb_fill_control_setup(cmdbuf,
701 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN, /*request=*/214, /*value=*/0,
702 /*index=*/44, /*length=*/4);
703 libusb_fill_control_transfer(xfr, devh, cmdbuf, cb_xfr, &completed, 0);
704 libusb_submit_transfer(xfr);
706 // set up an asynchronous transfer of register 24
707 static uint8_t cmdbuf2[LIBUSB_CONTROL_SETUP_SIZE + 4];
708 static int completed2 = 0;
710 xfr = libusb_alloc_transfer(0);
711 libusb_fill_control_setup(cmdbuf2,
712 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN, /*request=*/214, /*value=*/0,
713 /*index=*/24, /*length=*/4);
714 libusb_fill_control_transfer(xfr, devh, cmdbuf2, cb_xfr, &completed2, 0);
715 libusb_submit_transfer(xfr);
718 // set up an asynchronous transfer of the register dump
719 static uint8_t cmdbuf3[LIBUSB_CONTROL_SETUP_SIZE + 4];
720 static int completed3 = 0;
722 xfr = libusb_alloc_transfer(0);
723 libusb_fill_control_setup(cmdbuf3,
724 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN, /*request=*/214, /*value=*/0,
725 /*index=*/current_register, /*length=*/4);
726 libusb_fill_control_transfer(xfr, devh, cmdbuf3, cb_xfr, &completed3, 0);
727 //libusb_submit_transfer(xfr);
729 audiofp = fopen("audio.raw", "wb");
731 // set up isochronous transfers for audio and video
732 for (int e = 3; e <= 4; ++e) {
733 //int num_transfers = (e == 3) ? 6 : 6;
734 int num_transfers = 6;
735 for (int i = 0; i < num_transfers; ++i) {
736 int num_iso_pack, size;
738 // Video seems to require isochronous packets scaled with the width;
739 // seemingly six lines is about right, rounded up to the required 1kB
741 size = WIDTH * 2 * 6;
742 // Note that for 10-bit input, you'll need to increase size accordingly.
743 //size = size * 4 / 3;
744 if (size % 1024 != 0) {
748 num_iso_pack = (2 << 20) / size; // 2 MB.
749 printf("Picking %d packets of 0x%x bytes each\n", num_iso_pack, size);
754 int num_bytes = num_iso_pack * size;
755 uint8_t *buf = new uint8_t[num_bytes];
757 xfr = libusb_alloc_transfer(num_iso_pack);
759 fprintf(stderr, "oom\n");
763 int ep = LIBUSB_ENDPOINT_IN | e;
764 libusb_fill_iso_transfer(xfr, devh, ep, buf, num_bytes,
765 num_iso_pack, cb_xfr, nullptr, 0);
766 libusb_set_iso_packet_lengths(xfr, size);
767 iso_xfrs.push_back(xfr);
773 for (libusb_transfer *xfr : iso_xfrs) {
774 rc = libusb_submit_transfer(xfr);
777 //printf("num_bytes=%d\n", num_bytes);
778 fprintf(stderr, "Error submitting iso to endpoint 0x%02x, number %d: %s\n",
779 xfr->endpoint, i, libusb_error_name(rc));
785 usb_thread = thread(usb_thread_func);
789 libusb_release_interface(devh, 0);
793 libusb_exit(nullptr);
798 void stop_bm_capture()