forked from climberhunt/uvc-gadget
-
Notifications
You must be signed in to change notification settings - Fork 24
/
uvc-gadget.c
2369 lines (1932 loc) · 65.3 KB
/
uvc-gadget.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* UVC gadget application
*
* Modified by Petr Vavrin (peterbay) <[email protected]>
*
* Github: https://github.com/peterbay/uvc-gadget
*
* Added: video controls
* resolution changing
* framebuffer as source device
* code refactoring
* enhanced logging
* etc.
*
* Source tree:
* Primary source - wlhe - https://github.com/wlhe/uvc-gadget
* Forked source - climberhunt (Dave Hunt) - https://github.com/climberhunt/uvc-gadget
* Forked source - peterbay (Petr Vavrin) - https://github.com/peterbay/uvc-gadget
*
* Original author:
* Copyright (C) 2010 Ideas on board SPRL <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdbool.h>
#include <time.h>
#include <ftw.h>
#include <linux/usb/ch9.h>
#include <linux/usb/video.h>
#include <linux/videodev2.h>
#include <linux/fb.h>
#include "uvc-gadget.h"
volatile sig_atomic_t terminate = 0;
void term(int signum)
{
(void)(signum); /* avoid warning: unused parameter 'signum' */
terminate = 1;
}
static int sys_gpio_write(unsigned int type, char pin[], char value[])
{
FILE * sys_file;
char path[255];
strcpy(path, "/sys/class/gpio/");
switch(type) {
case GPIO_EXPORT:
strcat(path, "export");
value = pin;
break;
case GPIO_DIRECTION:
strcat(path, "gpio");
strcat(path, pin);
strcat(path, "/direction");
break;
case GPIO_VALUE:
strcat(path, "gpio");
strcat(path, pin);
strcat(path, "/value");
break;
}
printf("GPIO WRITE: Path: %s, Value: %s\n", path, value);
sys_file = fopen(path, "w");
if (!sys_file) {
printf("GPIO ERROR: File write failed: %s (%d).\n", strerror(errno), errno);
return -1;
}
fwrite(value, 1, strlen(value), sys_file);
fclose(sys_file);
return 0;
}
static int sys_led_write(unsigned int type, char value[])
{
FILE * sys_file;
char path[255];
strcpy(path, "/sys/class/leds/led0/");
switch(type) {
case LED_TRIGGER:
strcat(path, "trigger");
break;
case LED_BRIGHTNESS:
strcat(path, "brightness");
break;
}
printf("LED WRITE: Path: %s, Value: %s\n", path, value);
sys_file = fopen(path, "w");
if (!sys_file) {
printf("LED ERROR: File write failed: %s (%d).\n", strerror(errno), errno);
return -1;
}
fwrite(value, 1, strlen(value), sys_file);
fclose(sys_file);
return 0;
}
static void streaming_status_enable()
{
int ret;
if (!settings.streaming_status_enabled && settings.streaming_status_pin) {
ret = sys_gpio_write(GPIO_EXPORT, settings.streaming_status_pin, NULL);
if (ret < 0) {
return;
}
ret = sys_gpio_write(GPIO_DIRECTION, settings.streaming_status_pin, GPIO_DIRECTION_OUT);
if (ret < 0) {
return;
}
ret = sys_gpio_write(GPIO_VALUE, settings.streaming_status_pin, GPIO_VALUE_OFF);
if (ret < 0) {
return;
}
settings.streaming_status_enabled = true;
}
if (settings.streaming_status_onboard) {
ret = sys_led_write(LED_TRIGGER, LED_TRIGGER_NONE);
if (ret < 0) {
return;
}
ret = sys_led_write(LED_BRIGHTNESS, LED_BRIGHTNESS_LOW);
if (ret < 0) {
return;
}
settings.streaming_status_onboard_enabled = true;
}
return;
}
static void streaming_status_value(bool state)
{
char * gpio_value = (state) ? GPIO_VALUE_ON : GPIO_VALUE_OFF;
char * led_value = (state) ? LED_BRIGHTNESS_HIGH : LED_BRIGHTNESS_LOW;
if (settings.streaming_status_enabled) {
sys_gpio_write(GPIO_VALUE, settings.streaming_status_pin, gpio_value);
}
if (settings.streaming_status_onboard_enabled) {
sys_led_write(LED_BRIGHTNESS, led_value);
}
}
static char * uvc_request_code_name(unsigned int uvc_control)
{
switch (uvc_control) {
case UVC_RC_UNDEFINED:
return "RC_UNDEFINED";
case UVC_SET_CUR:
return "SET_CUR";
case UVC_GET_CUR:
return "GET_CUR";
case UVC_GET_MIN:
return "GET_MIN";
case UVC_GET_MAX:
return "GET_MAX";
case UVC_GET_RES:
return "GET_RES";
case UVC_GET_LEN:
return "GET_LEN";
case UVC_GET_INFO:
return "GET_INFO";
case UVC_GET_DEF:
return "GET_DEF";
default:
return "UNKNOWN";
}
}
static char * uvc_vs_interface_control_name(unsigned int interface)
{
switch (interface) {
case UVC_VS_CONTROL_UNDEFINED:
return "CONTROL_UNDEFINED";
case UVC_VS_PROBE_CONTROL:
return "PROBE";
case UVC_VS_COMMIT_CONTROL:
return "COMMIT";
case UVC_VS_STILL_PROBE_CONTROL:
return "STILL_PROBE";
case UVC_VS_STILL_COMMIT_CONTROL:
return "STILL_COMMIT";
case UVC_VS_STILL_IMAGE_TRIGGER_CONTROL:
return "STILL_IMAGE_TRIGGER";
case UVC_VS_STREAM_ERROR_CODE_CONTROL:
return "STREAM_ERROR_CODE";
case UVC_VS_GENERATE_KEY_FRAME_CONTROL:
return "GENERATE_KEY_FRAME";
case UVC_VS_UPDATE_FRAME_SEGMENT_CONTROL:
return "UPDATE_FRAME_SEGMENT";
case UVC_VS_SYNC_DELAY_CONTROL:
return "SYNC_DELAY";
default:
return "UNKNOWN";
}
}
static unsigned int get_frame_size(int pixelformat, int width, int height)
{
switch (pixelformat) {
case V4L2_PIX_FMT_YUYV:
return width * height * 2;
case V4L2_PIX_FMT_MJPEG:
return width * height;
break;
}
return width * height;
}
static int v4l2_open(char * devname, unsigned int nbufs)
{
struct v4l2_capability cap;
const char * type_name = "DEVICE_V4L2";
printf("%s: Opening %s device\n", type_name, devname);
v4l2_dev.fd = open(devname, O_RDWR | O_NONBLOCK, 0);
if (v4l2_dev.fd == -1) {
printf("%s: Device open failed: %s (%d).\n", type_name, strerror(errno), errno);
return -EINVAL;
}
if (ioctl(v4l2_dev.fd, VIDIOC_QUERYCAP, &cap) < 0) {
printf("%s: VIDIOC_QUERYCAP failed: %s (%d).\n", type_name, strerror(errno), errno);
goto err;
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
printf("%s: %s is no video capture device\n", type_name, devname);
goto err;
}
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
printf("%s: %s does not support streaming i/o\n", type_name, devname);
goto err;
}
printf("%s: Device is %s on bus %s\n", type_name, cap.card, cap.bus_info);
v4l2_dev.device_type = DEVICE_TYPE_UVC;
v4l2_dev.device_type_name = type_name;
v4l2_dev.buffer_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
v4l2_dev.memory_type = V4L2_MEMORY_MMAP;
v4l2_dev.nbufs = nbufs;
return 1;
err:
close(v4l2_dev.fd);
v4l2_dev.fd = -1;
return -EINVAL;
}
static int uvc_open( char * devname, unsigned int nbufs)
{
struct v4l2_capability cap;
const char * type_name = "DEVICE_UVC";
printf("%s: Opening %s device\n", type_name, devname);
uvc_dev.fd = open(devname, O_RDWR | O_NONBLOCK, 0);
if (uvc_dev.fd == -1) {
printf("%s: Device open failed: %s (%d).\n", type_name, strerror(errno), errno);
return -EINVAL;
}
if (ioctl(uvc_dev.fd, VIDIOC_QUERYCAP, &cap) < 0) {
printf("%s: VIDIOC_QUERYCAP failed: %s (%d).\n", type_name, strerror(errno), errno);
goto err;
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)) {
printf("%s: %s is no video output device\n", type_name, devname);
goto err;
}
printf("%s: Device is %s on bus %s\n", type_name, cap.card, cap.bus_info);
uvc_dev.device_type = DEVICE_TYPE_UVC;
uvc_dev.device_type_name = type_name;
uvc_dev.buffer_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
uvc_dev.memory_type = V4L2_MEMORY_USERPTR;
uvc_dev.nbufs = nbufs;
return 1;
err:
close(uvc_dev.fd);
uvc_dev.fd = -1;
return -EINVAL;
}
static void fb_show_info()
{
printf("FB: Resolution: %dx%d\n", fb_dev.fb_width, fb_dev.fb_height);
printf("FB: Bits per pixel: %d\n", fb_dev.fb_bpp);
printf("FB: Line length: %d\n", fb_dev.fb_line_length);
printf("FB: Memory size: %d\n", fb_dev.fb_mem_size);
}
static int fb_get_settings()
{
struct fb_var_screeninfo fb_info;
struct fb_fix_screeninfo mode_info;
if (ioctl(fb_dev.fd, FBIOGET_VSCREENINFO, &fb_info) < 0) {
printf("FB: Can't get framebuffer info: %s (%d).\n", strerror(errno), errno);
return -EINVAL;
}
if (ioctl(fb_dev.fd, FBIOGET_FSCREENINFO, &mode_info)) {
printf("FB: Can't get framebuffer screen info: %s (%d).\n", strerror(errno), errno);
return -EINVAL;
}
fb_dev.fb_screen_size = fb_info.xres * fb_info.yres * fb_info.bits_per_pixel / 8;
fb_dev.fb_mem_size = mode_info.smem_len;
fb_dev.fb_bpp = fb_info.bits_per_pixel;
fb_dev.fb_line_length = mode_info.line_length;
fb_dev.fb_width = fb_info.xres;
fb_dev.fb_height = fb_info.yres;
fb_show_info();
return 1;
}
static int fb_open(char * devname)
{
printf("FB: Opening %s device\n", devname);
fb_dev.fd = open(devname, O_RDWR);
if (fb_dev.fd < 0) {
printf("FB: Device open failed: %s (%d).\n", strerror(errno), errno);
goto err;
}
fb_dev.device_type = DEVICE_TYPE_FRAMEBUFFER;
if (fb_get_settings() < 0) {
goto err;
}
return 1;
err:
close(fb_dev.fd);
fb_dev.fd = -1;
return -EINVAL;
}
static int fb_mmap_open()
{
fb_dev.fb_memory = mmap(0,
fb_dev.fb_mem_size, PROT_READ | PROT_WRITE,
MAP_SHARED,
fb_dev.fd,
0
);
if (fb_dev.fb_memory == MAP_FAILED) {
printf("FB: Can't get framebuffer mmap: %s (%d).\n", strerror(errno), errno);
fb_dev.fb_memory = NULL;
return -EINVAL;
}
return 1;
}
static void fb_mmap_close()
{
if (fb_dev.fb_memory) {
munmap(fb_dev.fb_memory, 0);
fb_dev.fb_memory = NULL;
}
}
/* ---------------------------------------------------------------------------
* V4L2 streaming related
*/
static void v4l2_uninit_device()
{
unsigned int i;
if (!v4l2_dev.mem) {
return;
}
printf("%s: Uninit device\n", v4l2_dev.device_type_name);
for (i = 0; i < v4l2_dev.nbufs; ++i) {
if (munmap(v4l2_dev.mem[i].start, v4l2_dev.mem[i].length) < 0) {
printf("%s: munmap failed\n", v4l2_dev.device_type_name);
return;
}
}
free(v4l2_dev.mem);
v4l2_dev.mem = NULL;
}
static void uvc_uninit_device()
{
unsigned int i;
if (settings.source_device == DEVICE_TYPE_FRAMEBUFFER && uvc_dev.dummy_buf) {
printf("%s: Uninit device\n", uvc_dev.device_type_name);
for (i = 0; i < uvc_dev.nbufs; ++i) {
free(uvc_dev.dummy_buf[i].start);
uvc_dev.dummy_buf[i].start = NULL;
}
free(uvc_dev.dummy_buf);
uvc_dev.dummy_buf = NULL;
}
}
static int v4l2_video_stream_control(struct v4l2_device * dev, enum video_stream_action action)
{
int type = dev->buffer_type;
int ret;
if (action == STREAM_ON) {
ret = ioctl(dev->fd, VIDIOC_STREAMON, &type);
if (ret < 0) {
printf("%s: STREAM ON failed: %s (%d).\n", dev->device_type_name, strerror(errno), errno);
return ret;
}
printf("%s: STREAM ON success\n", dev->device_type_name);
dev->is_streaming = 1;
uvc_shutdown_requested = false;
} else if (dev->is_streaming) {
ret = ioctl(dev->fd, VIDIOC_STREAMOFF, &type);
if (ret < 0) {
printf("%s: STREAM OFF failed: %s (%d).\n", dev->device_type_name, strerror(errno), errno);
return ret;
}
printf("%s: STREAM OFF success\n", dev->device_type_name);
dev->is_streaming = 0;
}
return 0;
}
static int v4l2_video_stream(enum video_stream_action action)
{
return v4l2_video_stream_control(&v4l2_dev, action);
}
static int uvc_video_stream(enum video_stream_action action)
{
return v4l2_video_stream_control(&uvc_dev, action);
}
static int v4l2_init_buffers(struct v4l2_device * dev, struct v4l2_requestbuffers * req,
unsigned int count)
{
int ret;
req->count = count;
req->type = dev->buffer_type;
req->memory = dev->memory_type;
ret = ioctl(dev->fd, VIDIOC_REQBUFS, req);
if (ret < 0) {
if (ret == -EINVAL) {
printf("%s: Does not support %s\n", dev->device_type_name,
(dev->memory_type == V4L2_MEMORY_USERPTR) ? "user pointer i/o" : "memory mapping");
} else {
printf("%s: VIDIOC_REQBUFS error: %s (%d).\n",
dev->device_type_name, strerror(errno), errno);
}
return ret;
}
return count;
}
static int v4l2_reqbufs_mmap(struct v4l2_device * dev, struct v4l2_requestbuffers req)
{
int ret;
unsigned int i = 0;
/* Map the buffers. */
dev->mem = calloc(req.count, sizeof dev->mem[0]);
if (!dev->mem) {
printf("%s: Out of memory\n", dev->device_type_name);
ret = -ENOMEM;
goto err;
}
for (i = 0; i < req.count; ++i) {
CLEAR(dev->mem[i].buf);
dev->mem[i].buf.type = dev->buffer_type;
dev->mem[i].buf.memory = V4L2_MEMORY_MMAP;
dev->mem[i].buf.index = i;
ret = ioctl(dev->fd, VIDIOC_QUERYBUF, &(dev->mem[i].buf));
if (ret < 0) {
printf("%s: VIDIOC_QUERYBUF failed for buf %d: %s (%d).\n",
dev->device_type_name, i, strerror(errno), errno);
ret = -EINVAL;
goto err_free;
}
dev->mem[i].start =
mmap(NULL /* start anywhere */,
dev->mem[i].buf.length,
PROT_READ | PROT_WRITE /* required */,
MAP_SHARED /* recommended */,
dev->fd, dev->mem[i].buf.m.offset
);
if (MAP_FAILED == dev->mem[i].start) {
printf("%s: Unable to map buffer %u: %s (%d).\n",
dev->device_type_name, i, strerror(errno), errno);
dev->mem[i].length = 0;
ret = -EINVAL;
goto err_free;
}
dev->mem[i].length = dev->mem[i].buf.length;
printf("%s: Buffer %u mapped at address %p, length %d.\n",
dev->device_type_name, i, dev->mem[i].start, dev->mem[i].length);
}
return 0;
err_free:
free(dev->mem);
err:
return ret;
}
static int v4l2_reqbufs_userptr(struct v4l2_device * dev, struct v4l2_requestbuffers req)
{
unsigned int payload_size;
unsigned int i;
if (dev->device_type == DEVICE_TYPE_UVC && settings.source_device == DEVICE_TYPE_FRAMEBUFFER) {
/* Allocate buffers to hold dummy data pattern. */
dev->dummy_buf = calloc(req.count, sizeof dev->dummy_buf[0]);
if (!dev->dummy_buf) {
printf("%s: Out of memory\n", dev->device_type_name);
return -ENOMEM;
}
payload_size = fb_dev.fb_width * fb_dev.fb_height * 2;
for (i = 0; i < req.count; ++i) {
dev->dummy_buf[i].length = payload_size;
dev->dummy_buf[i].start = malloc(payload_size);
if (!dev->dummy_buf[i].start) {
printf("%s: Out of memory\n", dev->device_type_name);
return -ENOMEM;
}
}
dev->mem = dev->dummy_buf;
}
return 0;
}
static int v4l2_reqbufs(struct v4l2_device * dev, int nbufs)
{
int ret = 0;
struct v4l2_requestbuffers req;
CLEAR(req);
dev->dqbuf_count = 0;
dev->qbuf_count = 0;
ret = v4l2_init_buffers(dev, &req, nbufs);
if (ret < 1) {
return ret;
}
if (!req.count) {
return 0;
}
if (dev->memory_type == V4L2_MEMORY_MMAP) {
if (req.count < 2) {
printf("%s: Insufficient buffer memory.\n", dev->device_type_name);
return -EINVAL;
}
ret = v4l2_reqbufs_mmap(dev, req);
if (ret < 0) {
return -EINVAL;
}
}
if (dev->memory_type == V4L2_MEMORY_USERPTR && settings.source_device == DEVICE_TYPE_FRAMEBUFFER) {
if (req.count < 2) {
printf("%s: Insufficient buffer memory.\n", dev->device_type_name);
return -EINVAL;
}
ret = v4l2_reqbufs_userptr(dev, req);
if (ret < 0) {
return -EINVAL;
}
}
dev->nbufs = req.count;
printf("%s: %u buffers allocated.\n", dev->device_type_name, req.count);
return ret;
}
static int v4l2_request_bufs(int nbufs)
{
return v4l2_reqbufs(&v4l2_dev, nbufs);
}
static int uvc_request_bufs(int nbufs)
{
return v4l2_reqbufs(&uvc_dev, nbufs);
}
static int uvc_video_qbuf()
{
unsigned int i;
int ret;
if (settings.source_device == DEVICE_TYPE_FRAMEBUFFER) {
for (i = 0; i < uvc_dev.nbufs; ++i) {
struct v4l2_buffer buf;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
buf.memory = V4L2_MEMORY_USERPTR;
buf.m.userptr = (unsigned long) uvc_dev.dummy_buf[i].start;
buf.length = uvc_dev.dummy_buf[i].length;
buf.index = i;
ret = ioctl(uvc_dev.fd, VIDIOC_QBUF, &buf);
if (ret < 0) {
printf("UVC: VIDIOC_QBUF failed : %s (%d).\n", strerror(errno), errno);
return ret;
}
uvc_dev.qbuf_count++;
}
}
return 0;
}
static int v4l2_qbuf_mmap(struct v4l2_device * dev)
{
unsigned int i;
int ret;
for (i = 0; i < dev->nbufs; ++i) {
CLEAR(dev->mem[i].buf);
dev->mem[i].buf.type = dev->buffer_type;
dev->mem[i].buf.memory = V4L2_MEMORY_MMAP;
dev->mem[i].buf.index = i;
ret = ioctl(dev->fd, VIDIOC_QBUF, &(dev->mem[i].buf));
if (ret < 0) {
printf("%s: VIDIOC_QBUF failed : %s (%d).\n",
dev->device_type_name, strerror(errno), errno);
return ret;
}
dev->qbuf_count++;
}
return 0;
}
static void v4l2_uvc_video_process()
{
struct v4l2_buffer vbuf;
struct v4l2_buffer ubuf;
if (uvc_dev.is_streaming && v4l2_dev.dqbuf_count >= v4l2_dev.qbuf_count) {
return;
}
/* Dequeue spent buffer from V4L2 domain. */
CLEAR(vbuf);
vbuf.type = v4l2_dev.buffer_type;
vbuf.memory = v4l2_dev.memory_type;
if (ioctl(v4l2_dev.fd, VIDIOC_DQBUF, &vbuf) < 0) {
printf("%s: Unable to dequeue buffer: %s (%d).\n",
v4l2_dev.device_type_name, strerror(errno), errno);
return;
}
v4l2_dev.dqbuf_count++;
/* Queue video buffer to UVC domain. */
CLEAR(ubuf);
ubuf.type = uvc_dev.buffer_type;
ubuf.memory = uvc_dev.memory_type;
ubuf.m.userptr = (unsigned long) v4l2_dev.mem[vbuf.index].start;
ubuf.length = v4l2_dev.mem[vbuf.index].length;
ubuf.index = vbuf.index;
ubuf.bytesused = vbuf.bytesused;
if (ioctl(uvc_dev.fd, VIDIOC_QBUF, &ubuf) < 0) {
/* Check for a USB disconnect/shutdown event. */
if (errno == ENODEV) {
uvc_shutdown_requested = true;
printf("UVC: Possible USB shutdown requested from Host, seen during VIDIOC_QBUF\n");
}
return;
}
uvc_dev.qbuf_count++;
if (!uvc_dev.is_streaming) {
uvc_video_stream(STREAM_ON);
settings.blink_on_startup = 0;
streaming_status_value(uvc_dev.is_streaming);
}
}
/* ---------------------------------------------------------------------------
* V4L2 generic stuff
*/
static int v4l2_get_format(struct v4l2_device * dev)
{
struct v4l2_format fmt;
int ret;
CLEAR(fmt);
fmt.type = dev->buffer_type;
ret = ioctl(dev->fd, VIDIOC_G_FMT, &fmt);
if (ret < 0) {
return ret;
}
printf("%s: Getting current format: %c%c%c%c %ux%u\n",
dev->device_type_name, pixfmtstr(fmt.fmt.pix.pixelformat),
fmt.fmt.pix.width, fmt.fmt.pix.height);
return 0;
}
static int v4l2_set_format(struct v4l2_device * dev, struct v4l2_format * fmt)
{
int ret;
ret = ioctl(dev->fd, VIDIOC_S_FMT, fmt);
if (ret < 0) {
printf("%s: Unable to set format %s (%d).\n",
dev->device_type_name, strerror(errno), errno);
return ret;
}
printf("%s: Setting format to: %c%c%c%c %ux%u\n",
dev->device_type_name, pixfmtstr(fmt->fmt.pix.pixelformat),
fmt->fmt.pix.width, fmt->fmt.pix.height);
return 0;
}
static int v4l2_apply_format(struct v4l2_device * dev, unsigned int pixelformat,
unsigned int width, unsigned int height)
{
struct v4l2_format fmt;
int ret = -EINVAL;
if (dev->is_streaming || !dev->fd) {
return ret;
}
CLEAR(fmt);
fmt.type = dev->buffer_type;
fmt.fmt.pix.width = width;
fmt.fmt.pix.height = height;
fmt.fmt.pix.sizeimage = get_frame_size(pixelformat, width, height);
fmt.fmt.pix.pixelformat = pixelformat;
fmt.fmt.pix.field = V4L2_FIELD_ANY;
ret = v4l2_set_format(dev, &fmt);
if (ret < 0) {
return ret;
}
return v4l2_get_format(dev);
}
static void v4l2_set_ctrl_value(struct control_mapping_pair ctrl, unsigned int ctrl_v4l2, int v4l2_ctrl_value)
{
struct v4l2_queryctrl queryctrl;
struct v4l2_control control;
CLEAR(queryctrl);
queryctrl.id = ctrl_v4l2;
if (ioctl(v4l2_dev.fd, VIDIOC_QUERYCTRL, &queryctrl) == -1) {
if (errno != EINVAL) {
printf("%s: %s VIDIOC_QUERYCTRL failed: %s (%d).\n",
uvc_dev.device_type_name, ctrl.v4l2_name, strerror(errno), errno);
} else {
printf("%s: %s is not supported: %s (%d).\n",
uvc_dev.device_type_name, ctrl.v4l2_name, strerror(errno), errno);
}
} else if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
printf("%s: %s is disabled.\n", uvc_dev.device_type_name, ctrl.v4l2_name);
} else {
CLEAR(control);
control.id = ctrl.v4l2;
control.value = v4l2_ctrl_value;
if (ioctl(v4l2_dev.fd, VIDIOC_S_CTRL, &control) == -1) {
printf("%s: %s VIDIOC_S_CTRL failed: %s (%d).\n",
uvc_dev.device_type_name, ctrl.v4l2_name, strerror(errno), errno);
return;
}
printf("%s: %s changed value (V4L2: %d, UVC: %d)\n",
uvc_dev.device_type_name, ctrl.v4l2_name, v4l2_ctrl_value, ctrl.value);
}
}
static void v4l2_set_ctrl(struct control_mapping_pair ctrl)
{
int v4l2_ctrl_value = 0;
int v4l2_diff = ctrl.v4l2_maximum - ctrl.v4l2_minimum;
int ctrl_diff = ctrl.maximum - ctrl.minimum;
if (ctrl.value < ctrl.minimum) {
ctrl.value = ctrl.minimum;
}
if (ctrl.value > ctrl.maximum) {
ctrl.value = ctrl.maximum;
}
v4l2_ctrl_value = (ctrl.value - ctrl.minimum) * v4l2_diff / ctrl_diff + ctrl.v4l2_minimum;
v4l2_set_ctrl_value(ctrl, ctrl.v4l2, v4l2_ctrl_value);
if (ctrl.v4l2 == V4L2_CID_RED_BALANCE) {
v4l2_set_ctrl_value(ctrl, V4L2_CID_BLUE_BALANCE, v4l2_ctrl_value);
}
}
static void v4l2_apply_camera_control(struct control_mapping_pair * mapping,
struct v4l2_queryctrl queryctrl, struct v4l2_control control)
{
mapping->enabled = true;
mapping->control_type = queryctrl.type;
mapping->v4l2_minimum = queryctrl.minimum;
mapping->v4l2_maximum = queryctrl.maximum;
mapping->minimum = 0;
mapping->maximum = (0 - queryctrl.minimum) + queryctrl.maximum;
mapping->step = queryctrl.step;
mapping->default_value = (0 - queryctrl.minimum) + queryctrl.default_value;
mapping->value = (0 - queryctrl.minimum) + control.value;
printf("V4L2: Supported control %s (%s = %s)\n", queryctrl.name,
mapping->v4l2_name, mapping->uvc_name);
printf("V4L2: V4L2: min: %d, max: %d, step: %d, default: %d, value: %d\n",
queryctrl.minimum,
queryctrl.maximum,
queryctrl.step,
queryctrl.default_value,
control.value
);
printf("V4L2: UVC: min: %d, max: %d, step: %d, default: %d, value: %d\n",
mapping->minimum,
mapping->maximum,
queryctrl.step,
mapping->default_value,
mapping->value
);
}
static void v4l2_get_controls()
{
int i;
struct v4l2_queryctrl queryctrl;
struct v4l2_control control;
unsigned int id;
const unsigned next_fl = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
CLEAR(queryctrl);
queryctrl.id = next_fl;
while (0 == ioctl (v4l2_dev.fd, VIDIOC_QUERYCTRL, &queryctrl)) {
id = queryctrl.id;
queryctrl.id |= next_fl;
if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
continue;
}
for (i = 0; i < control_mapping_size; i++) {
if (control_mapping[i].v4l2 == id) {
control.id = queryctrl.id;
if (0 == ioctl (v4l2_dev.fd, VIDIOC_G_CTRL, &control)) {
v4l2_apply_camera_control(&control_mapping[i], queryctrl, control);
}
}
}
}
}
static void v4l2_close()
{
if (v4l2_dev.fd) {
close(v4l2_dev.fd);
v4l2_dev.fd = -1;
}
}
static void uvc_close()
{
if (uvc_dev.fd) {
close(uvc_dev.fd);
uvc_dev.fd = -1;
}
}
static void fb_close()
{