forked from umlaeute/v4l2loopback
-
Notifications
You must be signed in to change notification settings - Fork 1
/
v4l2loopback.c
2900 lines (2507 loc) · 75.7 KB
/
v4l2loopback.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
/* -*- c-file-style: "linux" -*- */
/*
* v4l2loopback.c -- video4linux2 loopback driver
*
* Copyright (C) 2005-2009 Vasily Levin ([email protected])
* Copyright (C) 2010-2019 IOhannes m zmoelnig ([email protected])
* Copyright (C) 2011 Stefan Diewald ([email protected])
* Copyright (C) 2012 Anton Novikov ([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.
*
*/
#include <linux/version.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>
#include <linux/time.h>
#include <linux/module.h>
#include <linux/videodev2.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/capability.h>
#include <linux/eventpoll.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-common.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
#define HAVE__V4L2_DEVICE
#include <media/v4l2-device.h>
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
#define HAVE__V4L2_CTRLS
#include <media/v4l2-ctrls.h>
#endif
#include <media/v4l2-event.h>
#include <linux/miscdevice.h>
#include "v4l2loopback.h"
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 1)
#define kstrtoul strict_strtoul
#endif
#if defined(timer_setup) && defined(from_timer)
#define HAVE_TIMER_SETUP
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 7, 0)
#define VFL_TYPE_VIDEO VFL_TYPE_GRABBER
#endif
#define V4L2LOOPBACK_VERSION_CODE \
KERNEL_VERSION(V4L2LOOPBACK_VERSION_MAJOR, V4L2LOOPBACK_VERSION_MINOR, \
V4L2LOOPBACK_VERSION_BUGFIX)
MODULE_DESCRIPTION("V4L2 loopback video device");
MODULE_AUTHOR("Vasily Levin, "
"IOhannes m zmoelnig <[email protected]>,"
"Stefan Diewald,"
"Anton Novikov"
"et al.");
MODULE_LICENSE("GPL");
/*
* helpers
*/
#define STRINGIFY(s) #s
#define STRINGIFY2(s) STRINGIFY(s)
#define dprintk(fmt, args...) \
do { \
if (debug > 0) { \
printk(KERN_INFO "v4l2-loopback[" STRINGIFY2( \
__LINE__) "]: " fmt, \
##args); \
} \
} while (0)
#define MARK() \
do { \
if (debug > 1) { \
printk(KERN_INFO "%s:%d[%s]\n", __FILE__, __LINE__, \
__func__); \
} \
} while (0)
#define dprintkrw(fmt, args...) \
do { \
if (debug > 2) { \
printk(KERN_INFO "v4l2-loopback[" STRINGIFY2( \
__LINE__) "]: " fmt, \
##args); \
} \
} while (0)
/*
* compatibility hacks
*/
#ifndef HAVE__V4L2_CTRLS
struct v4l2_ctrl_handler {
int error;
};
struct v4l2_ctrl_config {
void *ops;
u32 id;
const char *name;
int type;
s32 min;
s32 max;
u32 step;
s32 def;
};
int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl,
unsigned nr_of_controls_hint)
{
hdl->error = 0;
return 0;
}
void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
{
}
void *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
const struct v4l2_ctrl_config *conf, void *priv)
{
return NULL;
}
#endif /* HAVE__V4L2_CTRLS */
#ifndef HAVE__V4L2_DEVICE
/* dummy v4l2_device struct/functions */
#define V4L2_DEVICE_NAME_SIZE (20 + 16)
struct v4l2_device {
char name[V4L2_DEVICE_NAME_SIZE];
struct v4l2_ctrl_handler *ctrl_handler;
};
static inline int v4l2_device_register(void *dev, void *v4l2_dev)
{
return 0;
}
static inline void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
{
return;
}
#endif /* HAVE__V4L2_DEVICE */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
#define v4l2_file_operations file_operations
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 37)
void *v4l2l_vzalloc(unsigned long size)
{
void *data = vmalloc(size);
memset(data, 0, size);
return data;
}
#else
#define v4l2l_vzalloc vzalloc
#endif
static inline void v4l2l_get_timestamp(struct v4l2_buffer *b)
{
/* ktime_get_ts is considered deprecated, so use ktime_get_ts64 if possible */
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
struct timespec ts;
ktime_get_ts(&ts);
#else
struct timespec64 ts;
ktime_get_ts64(&ts);
#endif
b->timestamp.tv_sec = ts.tv_sec;
b->timestamp.tv_usec = (ts.tv_nsec / NSEC_PER_USEC);
}
#if !defined(__poll_t)
typedef unsigned __poll_t;
#endif
/* module constants
* can be overridden during he build process using something like
* make KCPPFLAGS="-DMAX_DEVICES=100"
*/
/* maximum number of v4l2loopback devices that can be created */
#ifndef MAX_DEVICES
#define MAX_DEVICES 8
#endif
/* whether the default is to announce capabilities exclusively or not */
#ifndef V4L2LOOPBACK_DEFAULT_EXCLUSIVECAPS
#define V4L2LOOPBACK_DEFAULT_EXCLUSIVECAPS 0
#endif
/* when a producer is considered to have gone stale */
#ifndef MAX_TIMEOUT
#define MAX_TIMEOUT (100 * 1000) /* in msecs */
#endif
/* max buffers that can be mapped, actually they
* are all mapped to max_buffers buffers */
#ifndef MAX_BUFFERS
#define MAX_BUFFERS 32
#endif
/* module parameters */
static int debug = 0;
module_param(debug, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "debugging level (higher values == more verbose)");
#define V4L2LOOPBACK_DEFAULT_MAX_BUFFERS 2
static int max_buffers = V4L2LOOPBACK_DEFAULT_MAX_BUFFERS;
module_param(max_buffers, int, S_IRUGO);
MODULE_PARM_DESC(max_buffers,
"how many buffers should be allocated [DEFAULT: " STRINGIFY2(
V4L2LOOPBACK_DEFAULT_MAX_BUFFERS) "]");
/* how many times a device can be opened
* the per-module default value can be overridden on a per-device basis using
* the /sys/devices interface
*
* note that max_openers should be at least 2 in order to get a working system:
* one opener for the producer and one opener for the consumer
* however, we leave that to the user
*/
#define V4L2LOOPBACK_DEFAULT_MAX_OPENERS 10
static int max_openers = V4L2LOOPBACK_DEFAULT_MAX_OPENERS;
module_param(max_openers, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(
max_openers,
"how many users can open the loopback device [DEFAULT: " STRINGIFY2(
V4L2LOOPBACK_DEFAULT_MAX_OPENERS) "]");
static int devices = -1;
module_param(devices, int, 0);
MODULE_PARM_DESC(devices, "how many devices should be created");
static int video_nr[MAX_DEVICES] = { [0 ...(MAX_DEVICES - 1)] = -1 };
module_param_array(video_nr, int, NULL, 0444);
MODULE_PARM_DESC(video_nr,
"video device numbers (-1=auto, 0=/dev/video0, etc.)");
static char *card_label[MAX_DEVICES];
module_param_array(card_label, charp, NULL, 0000);
MODULE_PARM_DESC(card_label, "card labels for each device");
static bool exclusive_caps[MAX_DEVICES] = {
[0 ...(MAX_DEVICES - 1)] = V4L2LOOPBACK_DEFAULT_EXCLUSIVECAPS
};
module_param_array(exclusive_caps, bool, NULL, 0444);
/* FIXXME: wording */
MODULE_PARM_DESC(
exclusive_caps,
"whether to announce OUTPUT/CAPTURE capabilities exclusively or not [DEFAULT: " STRINGIFY2(
V4L2LOOPBACK_DEFAULT_EXCLUSIVECAPS) "]");
/* format specifications */
#define V4L2LOOPBACK_SIZE_MIN_WIDTH 48
#define V4L2LOOPBACK_SIZE_MIN_HEIGHT 32
#define V4L2LOOPBACK_SIZE_DEFAULT_MAX_WIDTH 8192
#define V4L2LOOPBACK_SIZE_DEFAULT_MAX_HEIGHT 8192
#define V4L2LOOPBACK_SIZE_DEFAULT_WIDTH 640
#define V4L2LOOPBACK_SIZE_DEFAULT_HEIGHT 480
static int max_width = V4L2LOOPBACK_SIZE_DEFAULT_MAX_WIDTH;
module_param(max_width, int, S_IRUGO);
MODULE_PARM_DESC(max_width, "maximum allowed frame width [DEFAULT: " STRINGIFY2(
V4L2LOOPBACK_SIZE_DEFAULT_MAX_WIDTH) "]");
static int max_height = V4L2LOOPBACK_SIZE_DEFAULT_MAX_HEIGHT;
module_param(max_height, int, S_IRUGO);
MODULE_PARM_DESC(max_height,
"maximum allowed frame height [DEFAULT: " STRINGIFY2(
V4L2LOOPBACK_SIZE_DEFAULT_MAX_HEIGHT) "]");
static DEFINE_IDR(v4l2loopback_index_idr);
static DEFINE_MUTEX(v4l2loopback_ctl_mutex);
/* control IDs */
#ifndef HAVE__V4L2_CTRLS
#define V4L2LOOPBACK_CID_BASE (V4L2_CID_PRIVATE_BASE)
#else
#define V4L2LOOPBACK_CID_BASE (V4L2_CID_USER_BASE | 0xf000)
#endif
#define CID_KEEP_FORMAT (V4L2LOOPBACK_CID_BASE + 0)
#define CID_SUSTAIN_FRAMERATE (V4L2LOOPBACK_CID_BASE + 1)
#define CID_TIMEOUT (V4L2LOOPBACK_CID_BASE + 2)
#define CID_TIMEOUT_IMAGE_IO (V4L2LOOPBACK_CID_BASE + 3)
static int v4l2loopback_s_ctrl(struct v4l2_ctrl *ctrl);
static const struct v4l2_ctrl_ops v4l2loopback_ctrl_ops = {
.s_ctrl = v4l2loopback_s_ctrl,
};
static const struct v4l2_ctrl_config v4l2loopback_ctrl_keepformat = {
// clang-format off
.ops = &v4l2loopback_ctrl_ops,
.id = CID_KEEP_FORMAT,
.name = "keep_format",
.type = V4L2_CTRL_TYPE_BOOLEAN,
.min = 0,
.max = 1,
.step = 1,
.def = 0,
// clang-format on
};
static const struct v4l2_ctrl_config v4l2loopback_ctrl_sustainframerate = {
// clang-format off
.ops = &v4l2loopback_ctrl_ops,
.id = CID_SUSTAIN_FRAMERATE,
.name = "sustain_framerate",
.type = V4L2_CTRL_TYPE_BOOLEAN,
.min = 0,
.max = 1,
.step = 1,
.def = 0,
// clang-format on
};
static const struct v4l2_ctrl_config v4l2loopback_ctrl_timeout = {
// clang-format off
.ops = &v4l2loopback_ctrl_ops,
.id = CID_TIMEOUT,
.name = "timeout",
.type = V4L2_CTRL_TYPE_INTEGER,
.min = 0,
.max = MAX_TIMEOUT,
.step = 1,
.def = 0,
// clang-format on
};
static const struct v4l2_ctrl_config v4l2loopback_ctrl_timeoutimageio = {
// clang-format off
.ops = &v4l2loopback_ctrl_ops,
.id = CID_TIMEOUT_IMAGE_IO,
.name = "timeout_image_io",
.type = V4L2_CTRL_TYPE_BOOLEAN,
.min = 0,
.max = 1,
.step = 1,
.def = 0,
// clang-format on
};
/* module structures */
struct v4l2loopback_private {
int device_nr;
};
/* TODO(vasaka) use typenames which are common to kernel, but first find out if
* it is needed */
/* struct keeping state and settings of loopback device */
struct v4l2l_buffer {
struct v4l2_buffer buffer;
struct list_head list_head;
int use_count;
};
struct v4l2_loopback_device {
struct v4l2_device v4l2_dev;
struct v4l2_ctrl_handler ctrl_handler;
struct video_device *vdev;
/* pixel and stream format */
struct v4l2_pix_format pix_format;
struct v4l2_captureparm capture_param;
unsigned long frame_jiffies;
/* ctrls */
int keep_format; /* CID_KEEP_FORMAT; stay ready_for_capture even when all
openers close() the device */
int sustain_framerate; /* CID_SUSTAIN_FRAMERATE; duplicate frames to maintain
(close to) nominal framerate */
/* buffers stuff */
u8 *image; /* pointer to actual buffers data */
unsigned long int imagesize; /* size of buffers data */
int buffers_number; /* should not be big, 4 is a good choice */
struct v4l2l_buffer buffers[MAX_BUFFERS]; /* inner driver buffers */
int used_buffers; /* number of the actually used buffers */
int max_openers; /* how many times can this device be opened */
int write_position; /* number of last written frame + 1 */
struct list_head outbufs_list; /* buffers in output DQBUF order */
int bufpos2index
[MAX_BUFFERS]; /* mapping of (read/write_position % used_buffers)
* to inner buffer index */
long buffer_size;
/* sustain_framerate stuff */
struct timer_list sustain_timer;
unsigned int reread_count;
/* timeout stuff */
unsigned long timeout_jiffies; /* CID_TIMEOUT; 0 means disabled */
int timeout_image_io; /* CID_TIMEOUT_IMAGE_IO; next opener will
* read/write to timeout_image */
u8 *timeout_image; /* copy of it will be captured when timeout passes */
struct v4l2l_buffer timeout_image_buffer;
struct timer_list timeout_timer;
int timeout_happened;
/* sync stuff */
atomic_t open_count;
int ready_for_capture; /* set to the number of writers that opened the
* device and negotiated format. */
int ready_for_output; /* set to true when no writer is currently attached
* this differs slightly from !ready_for_capture,
* e.g. when using fallback images */
int announce_all_caps; /* set to false, if device caps (OUTPUT/CAPTURE)
* should only be announced if the resp. "ready"
* flag is set; default=TRUE */
int max_width;
int max_height;
char card_label[32];
wait_queue_head_t read_event;
spinlock_t lock;
};
/* types of opener shows what opener wants to do with loopback */
enum opener_type {
// clang-format off
UNNEGOTIATED = 0,
READER = 1,
WRITER = 2,
// clang-format on
};
/* struct keeping state and type of opener */
struct v4l2_loopback_opener {
enum opener_type type;
int vidioc_enum_frameintervals_calls;
int read_position; /* number of last processed frame + 1 or
* write_position - 1 if reader went out of sync */
unsigned int reread_count;
struct v4l2_buffer *buffers;
int buffers_number; /* should not be big, 4 is a good choice */
int timeout_image_io;
struct v4l2_fh fh;
};
#define fh_to_opener(ptr) container_of((ptr), struct v4l2_loopback_opener, fh)
/* this is heavily inspired by the bttv driver found in the linux kernel */
struct v4l2l_format {
char *name;
int fourcc; /* video4linux 2 */
int depth; /* bit/pixel */
int flags;
};
/* set the v4l2l_format.flags to PLANAR for non-packed formats */
#define FORMAT_FLAGS_PLANAR 0x01
#define FORMAT_FLAGS_COMPRESSED 0x02
static const struct v4l2l_format formats[] = {
#include "v4l2loopback_formats.h"
};
static const unsigned int FORMATS = ARRAY_SIZE(formats);
static char *fourcc2str(unsigned int fourcc, char buf[4])
{
buf[0] = (fourcc >> 0) & 0xFF;
buf[1] = (fourcc >> 8) & 0xFF;
buf[2] = (fourcc >> 16) & 0xFF;
buf[3] = (fourcc >> 24) & 0xFF;
return buf;
}
static const struct v4l2l_format *format_by_fourcc(int fourcc)
{
unsigned int i;
for (i = 0; i < FORMATS; i++) {
if (formats[i].fourcc == fourcc)
return formats + i;
}
dprintk("unsupported format '%c%c%c%c'\n", (fourcc >> 0) & 0xFF,
(fourcc >> 8) & 0xFF, (fourcc >> 16) & 0xFF,
(fourcc >> 24) & 0xFF);
return NULL;
}
static void pix_format_set_size(struct v4l2_pix_format *f,
const struct v4l2l_format *fmt,
unsigned int width, unsigned int height)
{
f->width = width;
f->height = height;
if (fmt->flags & FORMAT_FLAGS_PLANAR) {
f->bytesperline = width; /* Y plane */
f->sizeimage = (width * height * fmt->depth) >> 3;
} else if (fmt->flags & FORMAT_FLAGS_COMPRESSED) {
/* doesn't make sense for compressed formats */
f->bytesperline = 0;
f->sizeimage = (width * height * fmt->depth) >> 3;
} else {
f->bytesperline = (width * fmt->depth) >> 3;
f->sizeimage = height * f->bytesperline;
}
}
static int set_timeperframe(struct v4l2_loopback_device *dev,
struct v4l2_fract *tpf)
{
if ((tpf->denominator < 1) || (tpf->numerator < 1)) {
return -EINVAL;
}
dev->capture_param.timeperframe = *tpf;
dev->frame_jiffies = max(1UL, msecs_to_jiffies(1000) * tpf->numerator /
tpf->denominator);
return 0;
}
static struct v4l2_loopback_device *v4l2loopback_cd2dev(struct device *cd);
/* device attributes */
/* available via sysfs: /sys/devices/virtual/video4linux/video* */
static ssize_t attr_show_format(struct device *cd,
struct device_attribute *attr, char *buf)
{
/* gets the current format as "FOURCC:WxH@f/s", e.g. "YUYV:320x240@1000/30" */
struct v4l2_loopback_device *dev = v4l2loopback_cd2dev(cd);
const struct v4l2_fract *tpf;
char buf4cc[5], buf_fps[32];
if (!dev || !dev->ready_for_capture)
return 0;
tpf = &dev->capture_param.timeperframe;
fourcc2str(dev->pix_format.pixelformat, buf4cc);
buf4cc[4] = 0;
if (tpf->numerator == 1)
snprintf(buf_fps, sizeof(buf_fps), "%d", tpf->denominator);
else
snprintf(buf_fps, sizeof(buf_fps), "%d/%d", tpf->denominator,
tpf->numerator);
return sprintf(buf, "%4s:%dx%d@%s\n", buf4cc, dev->pix_format.width,
dev->pix_format.height, buf_fps);
}
static ssize_t attr_store_format(struct device *cd,
struct device_attribute *attr, const char *buf,
size_t len)
{
struct v4l2_loopback_device *dev = v4l2loopback_cd2dev(cd);
int fps_num = 0, fps_den = 1;
/* only fps changing is supported */
if (sscanf(buf, "@%d/%d", &fps_num, &fps_den) > 0) {
struct v4l2_fract f = { .numerator = fps_den,
.denominator = fps_num };
int err = 0;
if ((err = set_timeperframe(dev, &f)) < 0)
return err;
return len;
}
return -EINVAL;
}
static DEVICE_ATTR(format, S_IRUGO | S_IWUSR, attr_show_format,
attr_store_format);
static ssize_t attr_show_buffers(struct device *cd,
struct device_attribute *attr, char *buf)
{
struct v4l2_loopback_device *dev = v4l2loopback_cd2dev(cd);
return sprintf(buf, "%d\n", dev->used_buffers);
}
static DEVICE_ATTR(buffers, S_IRUGO, attr_show_buffers, NULL);
static ssize_t attr_show_maxopeners(struct device *cd,
struct device_attribute *attr, char *buf)
{
struct v4l2_loopback_device *dev = v4l2loopback_cd2dev(cd);
return sprintf(buf, "%d\n", dev->max_openers);
}
static ssize_t attr_store_maxopeners(struct device *cd,
struct device_attribute *attr,
const char *buf, size_t len)
{
struct v4l2_loopback_device *dev = NULL;
unsigned long curr = 0;
if (kstrtoul(buf, 0, &curr))
return -EINVAL;
dev = v4l2loopback_cd2dev(cd);
if (dev->max_openers == curr)
return len;
if (dev->open_count.counter > curr) {
/* request to limit to less openers as are currently attached to us */
return -EINVAL;
}
dev->max_openers = (int)curr;
return len;
}
static DEVICE_ATTR(max_openers, S_IRUGO | S_IWUSR, attr_show_maxopeners,
attr_store_maxopeners);
static void v4l2loopback_remove_sysfs(struct video_device *vdev)
{
#define V4L2_SYSFS_DESTROY(x) device_remove_file(&vdev->dev, &dev_attr_##x)
if (vdev) {
V4L2_SYSFS_DESTROY(format);
V4L2_SYSFS_DESTROY(buffers);
V4L2_SYSFS_DESTROY(max_openers);
/* ... */
}
}
static void v4l2loopback_create_sysfs(struct video_device *vdev)
{
int res = 0;
#define V4L2_SYSFS_CREATE(x) \
res = device_create_file(&vdev->dev, &dev_attr_##x); \
if (res < 0) \
break
if (!vdev)
return;
do {
V4L2_SYSFS_CREATE(format);
V4L2_SYSFS_CREATE(buffers);
V4L2_SYSFS_CREATE(max_openers);
/* ... */
} while (0);
if (res >= 0)
return;
dev_err(&vdev->dev, "%s error: %d\n", __func__, res);
}
/* global module data */
/* find a device based on it's device-number (e.g. '3' for /dev/video3) */
struct v4l2loopback_lookup_cb_data {
int device_nr;
struct v4l2_loopback_device *device;
};
static int v4l2loopback_lookup_cb(int id, void *ptr, void *data)
{
struct v4l2_loopback_device *device = ptr;
struct v4l2loopback_lookup_cb_data *cbdata = data;
if (cbdata && device && device->vdev) {
if (device->vdev->num == cbdata->device_nr) {
cbdata->device = device;
cbdata->device_nr = id;
return 1;
}
}
return 0;
}
static int v4l2loopback_lookup(int device_nr,
struct v4l2_loopback_device **device)
{
struct v4l2loopback_lookup_cb_data data = {
.device_nr = device_nr,
.device = NULL,
};
int err = idr_for_each(&v4l2loopback_index_idr, &v4l2loopback_lookup_cb,
&data);
if (1 == err) {
if (device)
*device = data.device;
return data.device_nr;
}
return -ENODEV;
}
static struct v4l2_loopback_device *v4l2loopback_cd2dev(struct device *cd)
{
struct video_device *loopdev = to_video_device(cd);
struct v4l2loopback_private *ptr =
(struct v4l2loopback_private *)video_get_drvdata(loopdev);
int nr = ptr->device_nr;
return idr_find(&v4l2loopback_index_idr, nr);
}
static struct v4l2_loopback_device *v4l2loopback_getdevice(struct file *f)
{
struct video_device *loopdev = video_devdata(f);
struct v4l2loopback_private *ptr =
(struct v4l2loopback_private *)video_get_drvdata(loopdev);
int nr = ptr->device_nr;
return idr_find(&v4l2loopback_index_idr, nr);
}
/* forward declarations */
static void init_buffers(struct v4l2_loopback_device *dev);
static int allocate_buffers(struct v4l2_loopback_device *dev);
static int free_buffers(struct v4l2_loopback_device *dev);
static void try_free_buffers(struct v4l2_loopback_device *dev);
static int allocate_timeout_image(struct v4l2_loopback_device *dev);
static void check_timers(struct v4l2_loopback_device *dev);
static const struct v4l2_file_operations v4l2_loopback_fops;
static const struct v4l2_ioctl_ops v4l2_loopback_ioctl_ops;
/* Queue helpers */
/* next functions sets buffer flags and adjusts counters accordingly */
static inline void set_done(struct v4l2l_buffer *buffer)
{
buffer->buffer.flags &= ~V4L2_BUF_FLAG_QUEUED;
buffer->buffer.flags |= V4L2_BUF_FLAG_DONE;
}
static inline void set_queued(struct v4l2l_buffer *buffer)
{
buffer->buffer.flags &= ~V4L2_BUF_FLAG_DONE;
buffer->buffer.flags |= V4L2_BUF_FLAG_QUEUED;
}
static inline void unset_flags(struct v4l2l_buffer *buffer)
{
buffer->buffer.flags &= ~V4L2_BUF_FLAG_QUEUED;
buffer->buffer.flags &= ~V4L2_BUF_FLAG_DONE;
}
/* V4L2 ioctl caps and params calls */
/* returns device capabilities
* called on VIDIOC_QUERYCAP
*/
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
struct v4l2_loopback_device *dev = v4l2loopback_getdevice(file);
int labellen = (sizeof(cap->card) < sizeof(dev->card_label)) ?
sizeof(cap->card) :
sizeof(dev->card_label);
int device_nr =
((struct v4l2loopback_private *)video_get_drvdata(dev->vdev))
->device_nr;
__u32 capabilities = V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
strlcpy(cap->driver, "v4l2 loopback", sizeof(cap->driver));
snprintf(cap->card, labellen, dev->card_label);
snprintf(cap->bus_info, sizeof(cap->bus_info),
"platform:v4l2loopback-%03d", device_nr);
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
/* since 3.1.0, the v4l2-core system is supposed to set the version */
cap->version = V4L2LOOPBACK_VERSION_CODE;
#endif
#ifdef V4L2_CAP_VIDEO_M2M
capabilities |= V4L2_CAP_VIDEO_M2M;
#endif /* V4L2_CAP_VIDEO_M2M */
if (dev->announce_all_caps) {
capabilities |= V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
} else {
if (dev->ready_for_capture) {
capabilities |= V4L2_CAP_VIDEO_CAPTURE;
}
if (dev->ready_for_output) {
capabilities |= V4L2_CAP_VIDEO_OUTPUT;
}
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
dev->vdev->device_caps =
#endif /* >=linux-4.7.0 */
cap->device_caps = cap->capabilities = capabilities;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)
cap->capabilities |= V4L2_CAP_DEVICE_CAPS;
#endif
memset(cap->reserved, 0, sizeof(cap->reserved));
return 0;
}
static int vidioc_enum_framesizes(struct file *file, void *fh,
struct v4l2_frmsizeenum *argp)
{
struct v4l2_loopback_device *dev;
/* LATER: what does the index really mean?
* if it's about enumerating formats, we can safely ignore it
* (CHECK)
*/
/* there can be only one... */
if (argp->index)
return -EINVAL;
dev = v4l2loopback_getdevice(file);
if (dev->ready_for_capture) {
/* format has already been negotiated
* cannot change during runtime
*/
argp->type = V4L2_FRMSIZE_TYPE_DISCRETE;
argp->discrete.width = dev->pix_format.width;
argp->discrete.height = dev->pix_format.height;
} else {
/* if the format has not been negotiated yet, we accept anything
*/
argp->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
argp->stepwise.min_width = V4L2LOOPBACK_SIZE_MIN_WIDTH;
argp->stepwise.min_height = V4L2LOOPBACK_SIZE_MIN_HEIGHT;
argp->stepwise.max_width = dev->max_width;
argp->stepwise.max_height = dev->max_height;
argp->stepwise.step_width = 1;
argp->stepwise.step_height = 1;
}
return 0;
}
/* returns frameinterval (fps) for the set resolution
* called on VIDIOC_ENUM_FRAMEINTERVALS
*/
static int vidioc_enum_frameintervals(struct file *file, void *fh,
struct v4l2_frmivalenum *argp)
{
struct v4l2_loopback_device *dev = v4l2loopback_getdevice(file);
struct v4l2_loopback_opener *opener = fh_to_opener(fh);
if (dev->ready_for_capture) {
if (opener->vidioc_enum_frameintervals_calls > 0)
return -EINVAL;
if (argp->width == dev->pix_format.width &&
argp->height == dev->pix_format.height) {
argp->type = V4L2_FRMIVAL_TYPE_DISCRETE;
argp->discrete = dev->capture_param.timeperframe;
opener->vidioc_enum_frameintervals_calls++;
return 0;
}
return -EINVAL;
}
return 0;
}
/* ------------------ CAPTURE ----------------------- */
/* returns device formats
* called on VIDIOC_ENUM_FMT, with v4l2_buf_type set to V4L2_BUF_TYPE_VIDEO_CAPTURE
*/
static int vidioc_enum_fmt_cap(struct file *file, void *fh,
struct v4l2_fmtdesc *f)
{
struct v4l2_loopback_device *dev;
MARK();
dev = v4l2loopback_getdevice(file);
if (f->index)
return -EINVAL;
if (dev->ready_for_capture) {
const __u32 format = dev->pix_format.pixelformat;
snprintf(f->description, sizeof(f->description), "[%c%c%c%c]",
(format >> 0) & 0xFF, (format >> 8) & 0xFF,
(format >> 16) & 0xFF, (format >> 24) & 0xFF);
f->pixelformat = dev->pix_format.pixelformat;
} else {
return -EINVAL;
}
f->flags = 0;
MARK();
return 0;
}
/* returns current video format format fmt
* called on VIDIOC_G_FMT, with v4l2_buf_type set to V4L2_BUF_TYPE_VIDEO_CAPTURE
*/
static int vidioc_g_fmt_cap(struct file *file, void *priv,
struct v4l2_format *fmt)
{
struct v4l2_loopback_device *dev;
MARK();
dev = v4l2loopback_getdevice(file);
if (!dev->ready_for_capture)
return -EINVAL;
fmt->fmt.pix = dev->pix_format;
MARK();
return 0;
}
/* checks if it is OK to change to format fmt;
* actual check is done by inner_try_fmt_cap
* just checking that pixelformat is OK and set other parameters, app should
* obey this decision
* called on VIDIOC_TRY_FMT, with v4l2_buf_type set to V4L2_BUF_TYPE_VIDEO_CAPTURE
*/
static int vidioc_try_fmt_cap(struct file *file, void *priv,
struct v4l2_format *fmt)
{
struct v4l2_loopback_device *dev;
char buf[5];
dev = v4l2loopback_getdevice(file);
if (0 == dev->ready_for_capture) {
dprintk("setting fmt_cap not possible yet\n");
return -EBUSY;
}
if (fmt->fmt.pix.pixelformat != dev->pix_format.pixelformat)
return -EINVAL;
fmt->fmt.pix = dev->pix_format;
buf[4] = 0;
dprintk("capFOURCC=%s\n", fourcc2str(dev->pix_format.pixelformat, buf));
return 0;
}
/* sets new output format, if possible
* actually format is set by input and we even do not check it, just return
* current one, but it is possible to set subregions of input TODO(vasaka)
* called on VIDIOC_S_FMT, with v4l2_buf_type set to V4L2_BUF_TYPE_VIDEO_CAPTURE
*/
static int vidioc_s_fmt_cap(struct file *file, void *priv,
struct v4l2_format *fmt)
{
return vidioc_try_fmt_cap(file, priv, fmt);
}
/* ------------------ OUTPUT ----------------------- */
/* returns device formats;
* LATER: allow all formats
* called on VIDIOC_ENUM_FMT, with v4l2_buf_type set to V4L2_BUF_TYPE_VIDEO_OUTPUT
*/
static int vidioc_enum_fmt_out(struct file *file, void *fh,
struct v4l2_fmtdesc *f)
{
struct v4l2_loopback_device *dev;
const struct v4l2l_format *fmt;
dev = v4l2loopback_getdevice(file);
if (dev->ready_for_capture) {
const __u32 format = dev->pix_format.pixelformat;
/* format has been fixed by the writer, so only one single format is supported */
if (f->index)
return -EINVAL;
fmt = format_by_fourcc(format);
if (NULL == fmt)
return -EINVAL;
f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
/* f->flags = ??; */
snprintf(f->description, sizeof(f->description), "%s",
fmt->name);
f->pixelformat = dev->pix_format.pixelformat;
} else {
/* fill in a dummy format */
/* coverity[unsigned_compare] */
if (f->index < 0 || f->index >= FORMATS)
return -EINVAL;
fmt = &formats[f->index];
f->pixelformat = fmt->fourcc;
snprintf(f->description, sizeof(f->description), "%s",
fmt->name);
}
f->flags = 0;
return 0;
}
/* returns current video format format fmt */
/* NOTE: this is called from the producer
* so if format has not been negotiated yet,
* it should return ALL of available formats,
* called on VIDIOC_G_FMT, with v4l2_buf_type set to V4L2_BUF_TYPE_VIDEO_OUTPUT
*/