-
Notifications
You must be signed in to change notification settings - Fork 7
/
mse_adapter_v4l2.c
2027 lines (1638 loc) · 50.8 KB
/
mse_adapter_v4l2.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
/*************************************************************************/ /*
avb-mse
Copyright (C) 2015-2017,2021 Renesas Electronics Corporation
License Dual MIT/GPLv2
The contents of this file are subject to the MIT license as set out below.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 ("GPL") in which case the provisions
of GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
GPL, and not to allow others to use your version of this file under the terms
of the MIT license, indicate your decision by deleting the provisions above
and replace them with the notice and other provisions required by GPL as set
out in the file called "GPL-COPYING" included in this distribution. If you do
not delete the provisions above, a recipient may use your version of this file
under the terms of either the MIT license or GPL.
This License is also included in this distribution in the file called
"MIT-COPYING".
EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
GPLv2:
If you wish to use this file under the terms of GPL, following terms are
effective.
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; version 2 of the License.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ /*************************************************************************/
#undef pr_fmt
#define pr_fmt(fmt) KBUILD_MODNAME "/" fmt
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kmod.h>
#include <linux/mutex.h>
#include <linux/fs.h>
#include <linux/platform_device.h>
#include <linux/videodev2.h>
#include <media/v4l2-device.h>
#include <media/v4l2-dev.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
#include <media/videobuf2-core.h>
#include <media/videobuf2-vmalloc.h>
#include <media/videobuf2-memops.h>
#include "ravb_mse_kernel.h"
#include "jpeg.h"
/*********************/
/* Name of driver */
/*********************/
#define MSE_ADAPTER_V4L2_NAME_BASE "ravb_mse"
#define MSE_ADAPTER_V4L2_DRIVER_NAME "ravb_mse.v4l2"
/*********************/
/* Number of devices */
/*********************/
#define MSE_ADAPTER_V4L2_DEVICE_MAX MSE_ADAPTER_MEDIA_MAX
#define MSE_ADAPTER_V4L2_DEVICE_VIDEO_DEFAULT 2
#define MSE_ADAPTER_V4L2_DEVICE_MPEG2TS_DEFAULT 2
#define NUM_BUFFERS 2
#define NUM_PLANES 1
#define MSE_ADAPTER_V4L2_MIN_VIDEO_SIZEIMAGE (512 * 1024)
#define MSE_ADAPTER_V4L2_MPEG2TS_BYTESPERLINE (188 * 192)
#define MSE_ADAPTER_V4L2_MPEG2TS_SIZEIMAGE \
(MSE_ADAPTER_V4L2_MPEG2TS_BYTESPERLINE * 14)
#define MSE_ADAPTER_V4L2_TEMP_BUF_MAXSIZE (2048 * 1024)
/*************/
/* Structure */
/*************/
/* Buffer information */
struct v4l2_adapter_buffer {
struct vb2_v4l2_buffer vb;
struct list_head list;
};
struct v4l2_adapter_temp_buffer {
size_t bytesused;
size_t length;
unsigned char *buf;
bool prepared;
};
/* File handle information */
struct v4l2_adapter_fh {
struct v4l2_fh fh;
struct v4l2_adapter_device *dev;
};
/* Device information */
struct v4l2_adapter_device {
struct v4l2_adapter_fh *owner;
struct v4l2_device v4l2_dev;
struct video_device vdev;
/* mutex lock */
struct mutex mutex_vb2; /* lock for vb2_queue */
struct v4l2_pix_format format;
struct v4l2_fract frameintervals;
struct vb2_queue q_cap;
struct vb2_queue q_out;
/* spin lock */
spinlock_t lock_buf_list; /* lock for buf_list */
/* list of buffer queued from v4l2 core */
struct list_head buf_list;
/* list of buffer prepared */
struct list_head prepared_buf_list;
/* list of buffer mse processing */
struct list_head stream_buf_list;
unsigned int sequence;
/* for buffer management debug */
unsigned int queued;
unsigned int dequeued;
/* index for register */
int index_mse;
enum MSE_TYPE type;
/* index for MSE instance */
int index_instance;
bool f_mse_open;
/* previous trans buffer for checking buffer overwite */
void *prev;
/* temp video buffer */
bool use_temp_buffer;
struct v4l2_adapter_temp_buffer temp_buf[NUM_BUFFERS];
void *temp_buf_base;
int temp_w;
int temp_r;
/* mpeg2ts stream type */
enum MSE_MPEG2TS_TYPE mpeg2ts_type;
};
/* Format information */
struct v4l2_adapter_fmt {
u32 fourcc;
unsigned int min_width;
unsigned int max_width;
unsigned int step_width;
unsigned int min_height;
unsigned int max_height;
unsigned int step_height;
};
static const struct v4l2_fmtdesc g_formats_video[] = {
{
.description = "H264 with start codes",
.pixelformat = V4L2_PIX_FMT_H264,
},
{
.description = "H264 without start codes",
.pixelformat = V4L2_PIX_FMT_H264_NO_SC,
},
{
.description = "Motion-JPEG",
.pixelformat = V4L2_PIX_FMT_MJPEG,
},
};
static const struct v4l2_fmtdesc g_formats_mpeg[] = {
{
.description = "MPEG-1/2/4 Multiplexed",
.pixelformat = V4L2_PIX_FMT_MPEG,
},
};
/* Playback, capture video format sizes */
static const struct v4l2_adapter_fmt g_mse_adapter_v4l2_fmt_sizes_video[] = {
/* limited H.264 picture size to range of
* R-Car Hardware video decoder (VCP4).
*/
{
.fourcc = V4L2_PIX_FMT_H264,
.min_width = 80,
.max_width = 3840,
.step_width = 2,
.min_height = 80,
.max_height = 2160,
.step_height = 2,
},
/* limited H.264 picture size to range of
* R-Car Hardware video decoder (VCP4).
*/
{
.fourcc = V4L2_PIX_FMT_H264_NO_SC,
.min_width = 80,
.max_width = 3840,
.step_width = 2,
.min_height = 80,
.max_height = 2160,
.step_height = 2,
},
/* limited MJPEG picture size to range of
* AVTP format(same as RTP), see RFC 2435 3.1.5,3.1.6
*/
{
.fourcc = V4L2_PIX_FMT_MJPEG,
.min_width = 8,
.max_width = 2040,
.step_width = 8,
.min_height = 8,
.max_height = 2040,
.step_height = 8,
},
};
static const struct v4l2_adapter_fmt g_mse_adapter_v4l2_fmt_sizes_mpeg[] = {
/* limited MPEG2-TS picture size to range of
* R-Car Hardware video decoder (VCP4).
*/
{
.fourcc = V4L2_PIX_FMT_MPEG,
.min_width = 80,
.max_width = 3840,
.step_width = 2,
.min_height = 80,
.max_height = 2160,
.step_height = 2,
},
};
/*******************/
/* global variable */
/*******************/
static int v4l2_video_devices = MSE_ADAPTER_V4L2_DEVICE_VIDEO_DEFAULT;
module_param(v4l2_video_devices, int, 0440);
static int v4l2_mpeg2ts_devices = MSE_ADAPTER_V4L2_DEVICE_MPEG2TS_DEFAULT;
module_param(v4l2_mpeg2ts_devices, int, 0440);
static int v4l2_devices;
/************/
/* Function */
/************/
#define vadp_buffer_done(vadp_dev, vadp_buf, state) \
do { \
struct v4l2_adapter_device *__dev = (vadp_dev); \
struct v4l2_adapter_buffer *__buf = (vadp_buf); \
mse_debug("queued=%u dequeued=%u\n", \
__dev->queued, ++(__dev->dequeued)); \
list_del(&__buf->list); \
vb2_buffer_done(&__buf->vb.vb2_buf, (state)); \
} while (0)
#define v4l2_type_stringfy(type) \
(V4L2_TYPE_IS_OUTPUT(type) ? "output" : "capture")
/*
* temp_buffer related functions
*/
static void temp_buffer_free(struct v4l2_adapter_device *vadp_dev)
{
int i;
struct v4l2_adapter_temp_buffer *temp;
/* NOT allocated, skip */
if (!vadp_dev->temp_buf_base)
return;
for (i = 0; i < NUM_BUFFERS; i++) {
temp = &vadp_dev->temp_buf[i];
temp->buf = NULL;
temp->length = 0;
}
kfree(vadp_dev->temp_buf_base);
vadp_dev->temp_buf_base = NULL;
}
static int temp_buffer_alloc(struct v4l2_adapter_device *vadp_dev)
{
int i;
struct v4l2_adapter_temp_buffer *temp;
u32 length;
u8 *buf;
/* already allocated, skip allocate memory */
if (vadp_dev->temp_buf_base)
return 0;
length = min_t(u32, vadp_dev->format.sizeimage,
MSE_ADAPTER_V4L2_TEMP_BUF_MAXSIZE);
buf = kmalloc_array((size_t)length, NUM_BUFFERS, GFP_KERNEL);
if (!buf)
return -ENOMEM;
vadp_dev->temp_buf_base = buf;
vadp_dev->temp_w = 0;
vadp_dev->temp_r = 0;
for (i = 0; i < NUM_BUFFERS; i++) {
temp = &vadp_dev->temp_buf[i];
temp->length = (size_t)length;
temp->buf = buf + (temp->length * i);
temp->prepared = false;
temp->bytesused = 0;
}
return 0;
}
static struct v4l2_adapter_temp_buffer *temp_buffer_get_prepared(
struct v4l2_adapter_device *vadp_dev)
{
struct v4l2_adapter_temp_buffer *temp;
temp = &vadp_dev->temp_buf[vadp_dev->temp_r];
if (!temp->prepared)
return NULL;
return temp;
}
#define MPEG2TS_TS_SIZE (188)
#define MPEG2TS_SYNC (0x47)
#define MPEG2TS_M2TS_OFFSET (4)
#define MPEG2TS_M2TS_SIZE (MPEG2TS_M2TS_OFFSET + MPEG2TS_TS_SIZE)
#define MPEG2TS_MIN_LEN_REQUIRED (MPEG2TS_M2TS_OFFSET + MPEG2TS_M2TS_SIZE * 2)
/* Check at least 2 adjacent packets.
* Optionally 3 if enough data are available.
*/
#define MPEG2TS_MIN_PKT_CHECK 2
#define MPEG2TS_MAX_PKT_CHECK 3
static bool is_mpeg2ts_ts(unsigned char *tsp, size_t len)
{
size_t offs = 0;
unsigned int succ = 0;
while ((offs < len) && (succ < MPEG2TS_MAX_PKT_CHECK)) {
if (tsp[offs] != MPEG2TS_SYNC)
return false;
succ++;
offs += MPEG2TS_TS_SIZE;
}
return (succ >= MPEG2TS_MIN_PKT_CHECK) ? true : false;
}
static bool is_mpeg2ts_m2ts(unsigned char *tsp, size_t len)
{
size_t offs = MPEG2TS_M2TS_OFFSET;
unsigned int succ = 0;
while ((offs < len) && (succ < MPEG2TS_MAX_PKT_CHECK)) {
if (tsp[offs] != MPEG2TS_SYNC)
return false;
succ++;
offs += MPEG2TS_M2TS_SIZE;
}
return (succ >= MPEG2TS_MIN_PKT_CHECK) ? true : false;
}
static int get_mpeg2ts_format(unsigned char *buf, size_t size)
{
if (is_mpeg2ts_ts(buf, size))
return MSE_MPEG2TS_TYPE_TS;
else if (is_mpeg2ts_m2ts(buf, size))
return MSE_MPEG2TS_TYPE_M2TS;
else
return -1;
}
static bool tspacket_size_is_valid(enum MSE_MPEG2TS_TYPE type,
size_t size)
{
if (type == MSE_MPEG2TS_TYPE_M2TS)
return !(size % MPEG2TS_M2TS_SIZE);
else if (type == MSE_MPEG2TS_TYPE_TS)
return !(size % MPEG2TS_TS_SIZE);
else
return false;
}
static bool jpeg_frame_is_valid(unsigned char *buf, size_t size)
{
size_t offset = 0;
if ((buf[offset] != JPEG_MARKER) ||
(buf[offset + 1] != JPEG_MARKER_KIND_SOI))
return false;
offset = size - JPEG_MARKER_SIZE_LENGTH;
if ((buf[offset] != JPEG_MARKER) ||
(buf[offset + 1] != JPEG_MARKER_KIND_EOI))
return false;
return true;
}
static int temp_buffer_check_type(struct v4l2_adapter_device *vadp_dev,
unsigned char *buf,
size_t size, size_t size_fmtchk)
{
int ret;
u8 mk;
size_t offset = 0;
switch (vadp_dev->format.pixelformat) {
case V4L2_PIX_FMT_MPEG:
ret = get_mpeg2ts_format(buf, size_fmtchk);
vadp_dev->mpeg2ts_type = ret;
vadp_dev->use_temp_buffer = !tspacket_size_is_valid(ret, size);
break;
case V4L2_PIX_FMT_MJPEG:
mk = jpeg_get_marker(buf, size, &offset);
ret = (mk == JPEG_MARKER_KIND_SOI) ? 0 : -1;
vadp_dev->use_temp_buffer = !jpeg_frame_is_valid(buf, size);
break;
default:
ret = 0;
vadp_dev->use_temp_buffer = false;
break;
}
return ret;
}
static int temp_buffer_write_mpeg2ts(struct v4l2_adapter_device *vadp_dev,
unsigned char *buf,
size_t size)
{
struct v4l2_adapter_temp_buffer *temp;
unsigned char *copy_from;
size_t bytesused, length, pos;
size_t copy_size;
size_t psize;
int temp_w = vadp_dev->temp_w;
int temp_r = vadp_dev->temp_r;
temp = &vadp_dev->temp_buf[temp_w];
if (temp->prepared)
return -EAGAIN;
bytesused = temp->bytesused;
length = temp->length;
mse_debug("temp_w=%d bytesused=%zu buf=%p size=%zu\n",
temp_w, bytesused, buf, size);
if (vadp_dev->mpeg2ts_type == MSE_MPEG2TS_TYPE_M2TS)
psize = MPEG2TS_M2TS_SIZE;
else
psize = MPEG2TS_TS_SIZE;
if (!((temp_w + NUM_BUFFERS - temp_r - 1) % NUM_BUFFERS > 0)) {
if (bytesused + size % psize) {
mse_debug("temp buffer has no enough area\n");
return -EAGAIN;
}
}
copy_from = buf;
copy_size = size;
if (bytesused + copy_size > length) {
mse_debug("temp buffer overrun %zu/%zu\n",
bytesused + copy_size, temp->length);
return -EAGAIN;
}
/* copy stream */
memcpy(temp->buf + bytesused, copy_from, copy_size);
bytesused += copy_size;
/* adjust bytesused */
pos = bytesused - bytesused % psize;
temp->bytesused = pos;
temp->prepared = true;
vadp_dev->temp_w = (temp_w + 1) % NUM_BUFFERS;
if (pos == bytesused)
return 0;
/* copy remain data */
copy_from = temp->buf + pos;
copy_size = bytesused - pos;
temp = &vadp_dev->temp_buf[vadp_dev->temp_w];
memcpy(temp->buf + temp->bytesused, copy_from, copy_size);
temp->bytesused += copy_size;
return 0;
}
static int temp_buffer_write_mjpeg(struct v4l2_adapter_device *vadp_dev,
unsigned char *buf,
size_t size)
{
struct v4l2_adapter_temp_buffer *temp;
unsigned char *copy_from;
size_t bytesused, length, pos;
size_t copy_size = size;
int temp_w = vadp_dev->temp_w;
int temp_r = vadp_dev->temp_r;
temp = &vadp_dev->temp_buf[temp_w];
if (temp->prepared)
return -EAGAIN;
bytesused = temp->bytesused;
length = temp->length;
mse_debug("temp_w=%d bytesused=%zu buf=%p size=%zu\n",
temp_w, bytesused, buf, size);
pos = jpeg_search_eoi(buf, size, 0);
if (!((temp_w + NUM_BUFFERS - temp_r - 1) % NUM_BUFFERS > 0)) {
if (pos != size) {
mse_debug("temp buffer has no enough area\n");
return -EAGAIN;
}
}
copy_from = buf;
copy_size = pos;
if (bytesused + copy_size > length) {
mse_debug("temp buffer overrun %zu/%zu\n",
bytesused + copy_size, temp->length);
return -EAGAIN;
}
/* copy stream until period position */
memcpy(temp->buf + bytesused, copy_from, copy_size);
temp->bytesused += copy_size;
if (jpeg_frame_is_valid(temp->buf, temp->bytesused)) {
temp->prepared = true;
vadp_dev->temp_w = (temp_w + 1) % NUM_BUFFERS;
}
if (size == pos)
return 0;
/* copy remain data */
copy_from = buf + pos;
copy_size = size - pos;
temp = &vadp_dev->temp_buf[vadp_dev->temp_w];
memcpy(temp->buf + temp->bytesused, copy_from, copy_size);
temp->bytesused += copy_size;
return 0;
}
static int temp_buffer_write(struct v4l2_adapter_device *vadp_dev,
unsigned char *buf,
size_t size)
{
if (vadp_dev->format.pixelformat == V4L2_PIX_FMT_MPEG)
return temp_buffer_write_mpeg2ts(vadp_dev, buf, size);
else if (vadp_dev->format.pixelformat == V4L2_PIX_FMT_MJPEG)
return temp_buffer_write_mjpeg(vadp_dev, buf, size);
else
return 0;
}
static inline const char *convert_type_to_str(enum MSE_TYPE type)
{
switch (type) {
case MSE_TYPE_ADAPTER_VIDEO:
return "video";
case MSE_TYPE_ADAPTER_MPEG2TS:
return "mpeg2ts";
default:
return "";
}
}
static inline struct v4l2_adapter_fh *to_v4l2_adapter_fh(struct file *filp)
{
return container_of(filp->private_data, struct v4l2_adapter_fh, fh);
}
static inline struct v4l2_adapter_buffer *to_v4l2_adapter_buffer(
struct vb2_v4l2_buffer *vbuf)
{
return container_of(vbuf, struct v4l2_adapter_buffer, vb);
}
/* Get V4L2 Adapter device ownership */
static int vadp_owner_get(struct v4l2_adapter_fh *vadp_fh)
{
unsigned long flags;
int ret = 0;
struct v4l2_adapter_device *vadp_dev = vadp_fh->dev;
spin_lock_irqsave(&vadp_dev->lock_buf_list, flags);
if (!vadp_dev->owner)
vadp_dev->owner = vadp_fh;
else if (vadp_dev->owner != vadp_fh)
ret = -EBUSY;
spin_unlock_irqrestore(&vadp_dev->lock_buf_list, flags);
return ret;
}
/* Put V4L2 Adapter device ownership */
static void vadp_owner_put(struct v4l2_adapter_fh *vadp_fh)
{
unsigned long flags;
struct v4l2_adapter_device *vadp_dev = vadp_fh->dev;
spin_lock_irqsave(&vadp_dev->lock_buf_list, flags);
vadp_dev->owner = NULL;
spin_unlock_irqrestore(&vadp_dev->lock_buf_list, flags);
}
/* Test V4L2 Adapter device ownership */
static int vadp_owner_is(struct v4l2_adapter_fh *vadp_fh)
{
unsigned long flags;
int ret;
struct v4l2_adapter_device *vadp_dev = vadp_fh->dev;
spin_lock_irqsave(&vadp_dev->lock_buf_list, flags);
ret = (vadp_dev->owner == vadp_fh);
spin_unlock_irqrestore(&vadp_dev->lock_buf_list, flags);
return ret;
}
static int try_mse_open(struct v4l2_adapter_device *vadp_dev,
enum v4l2_buf_type i)
{
bool tx = V4L2_TYPE_IS_OUTPUT(i);
int index;
if (vadp_dev->f_mse_open)
return 0;
/* probe is not finish yet */
if (vadp_dev->index_mse == MSE_INDEX_UNDEFINED) {
mse_info("probe is not finish yet\n");
return 0;
}
index = mse_open(vadp_dev->index_mse, tx);
if (index < 0) {
mse_err("failed to open mse, err=%d\n", index);
return index;
}
vadp_dev->index_instance = index;
vadp_dev->f_mse_open = true;
return 0;
}
static int try_mse_close(struct v4l2_adapter_device *vadp_dev)
{
int err;
if (!vadp_dev->f_mse_open)
return 0;
/* probe is not finish yet */
if (vadp_dev->index_mse == MSE_INDEX_UNDEFINED) {
mse_info("probe is not finish yet\n");
return 0;
}
if (vadp_dev->index_instance == MSE_INDEX_UNDEFINED) {
mse_info("mse_start is not finish yet\n");
return 0;
}
err = mse_close(vadp_dev->index_instance);
if (err < 0)
return err;
vadp_dev->f_mse_open = false;
return 0;
}
static int mse_adapter_v4l2_fop_open(struct file *filp)
{
struct v4l2_adapter_device *vadp_dev = video_drvdata(filp);
struct v4l2_adapter_fh *vadp_fh;
mse_debug("START\n");
vadp_fh = kzalloc(sizeof(*vadp_fh), GFP_KERNEL);
if (!vadp_fh)
return -ENOMEM;
v4l2_fh_init(&vadp_fh->fh, &vadp_dev->vdev);
v4l2_fh_add(&vadp_fh->fh);
vadp_fh->dev = vadp_dev;
filp->private_data = &vadp_fh->fh;
mse_debug("END\n");
return 0;
}
static int mse_adapter_v4l2_fop_release(struct file *filp)
{
int err;
struct v4l2_adapter_fh *vadp_fh = to_v4l2_adapter_fh(filp);
struct v4l2_adapter_device *vadp_dev = vadp_fh->dev;
struct v4l2_requestbuffers req;
mse_debug("START\n");
if (vadp_owner_is(vadp_fh)) {
if (vb2_is_busy(vadp_dev->vdev.queue)) {
mse_debug("vb2 is busy, try queue free\n");
vb2_ioctl_streamoff(filp, filp->private_data,
vadp_dev->vdev.queue->type);
req.count = 0;
req.type = vadp_dev->vdev.queue->type;
req.memory = V4L2_MEMORY_MMAP;
err = vb2_ioctl_reqbufs(filp,
filp->private_data,
&req);
if (err < 0) {
mse_err("Failed vb2_ioctl_reqbufs()\n");
return err;
}
}
if (vadp_dev->f_mse_open) {
err = try_mse_close(vadp_dev);
if (err < 0) {
mse_err("Failed mse_close()\n");
return err;
}
}
vadp_owner_put(vadp_fh);
}
filp->private_data = NULL;
v4l2_fh_del(&vadp_fh->fh);
v4l2_fh_exit(&vadp_fh->fh);
kfree(vadp_fh);
mse_debug("END\n");
return 0;
}
static int mse_adapter_v4l2_querycap(struct file *filp,
void *priv,
struct v4l2_capability *vcap)
{
struct v4l2_adapter_fh *vadp_fh = to_v4l2_adapter_fh(filp);
struct v4l2_adapter_device *vadp_dev = vadp_fh->dev;
mse_debug("START\n");
strlcpy((char *)vcap->driver, MSE_ADAPTER_V4L2_DRIVER_NAME,
sizeof(vcap->driver));
strlcpy((char *)vcap->card, vadp_dev->vdev.name, sizeof(vcap->card));
snprintf((char *)vcap->bus_info, sizeof(vcap->bus_info), "platform:%s",
vadp_dev->v4l2_dev.name);
vcap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
V4L2_CAP_STREAMING;
vcap->capabilities = vcap->device_caps | V4L2_CAP_DEVICE_CAPS;
mse_debug("END\n");
return 0;
}
static int mse_adapter_v4l2_enum_fmt_vid(struct file *filp,
void *priv,
struct v4l2_fmtdesc *fmt)
{
unsigned int index;
const struct v4l2_fmtdesc *fmtdesc;
const struct v4l2_fmtdesc *fmtbase;
struct v4l2_adapter_fh *vadp_fh = to_v4l2_adapter_fh(filp);
struct v4l2_adapter_device *vadp_dev = vadp_fh->dev;
enum v4l2_buf_type buf_type;
int fmt_size;
mse_debug("START fmt->index=%d\n", fmt->index);
if (vadp_dev->type == MSE_TYPE_ADAPTER_VIDEO) {
fmtbase = g_formats_video;
fmt_size = ARRAY_SIZE(g_formats_video);
} else if (vadp_dev->type == MSE_TYPE_ADAPTER_MPEG2TS) {
fmtbase = g_formats_mpeg;
fmt_size = ARRAY_SIZE(g_formats_mpeg);
} else {
mse_err("Failed vdev type=%d\n", vadp_dev->type);
return -EINVAL;
}
if (fmt->index >= fmt_size) {
mse_debug("fmt->index(%d) is equal or bigger than %d\n",
fmt->index, fmt_size);
return -EINVAL;
}
buf_type = fmt->type;
index = fmt->index;
memset(fmt, 0, sizeof(*fmt));
fmtdesc = &fmtbase[index];
fmt->index = index;
fmt->type = buf_type;
strcpy((char *)fmt->description, (char *)fmtdesc->description);
fmt->pixelformat = fmtdesc->pixelformat;
mse_debug("END format: %s\n", fmtdesc->description);
return 0;
}
static const struct v4l2_fmtdesc *get_fmtdesc(
const struct v4l2_fmtdesc *fmtdescs,
int arr_size,
struct v4l2_format *fmt)
{
int i;
const struct v4l2_fmtdesc *fmtdesc;
struct v4l2_pix_format *pix = &fmt->fmt.pix;
for (i = 0; i < arr_size; i++) {
fmtdesc = &fmtdescs[i];
if (fmtdesc->pixelformat == pix->pixelformat)
return fmtdesc;
}
return NULL;
}
static void get_fmt_sizes(const struct v4l2_adapter_fmt *dflt_fmts,
int arr_size,
struct v4l2_format *fmt)
{
int i;
struct v4l2_pix_format *pix = &fmt->fmt.pix;
for (i = 0; i < arr_size; i++) {
if (pix->pixelformat != dflt_fmts[i].fourcc)
continue;
if (pix->width > dflt_fmts[i].max_width)
pix->width = dflt_fmts[i].max_width;
else if (pix->width < dflt_fmts[i].min_width)
pix->width = dflt_fmts[i].min_width;
else
pix->width -= (pix->width % dflt_fmts[i].step_width);
if (pix->height > dflt_fmts[i].max_height)
pix->height = dflt_fmts[i].max_height;
else if (pix->height < dflt_fmts[i].min_height)
pix->height = dflt_fmts[i].min_height;
else
pix->height -= (pix->height % dflt_fmts[i].step_height);
break;
}
}
static int mse_adapter_v4l2_try_fmt_vid(struct file *filp,
void *priv,
struct v4l2_format *fmt)
{
const struct v4l2_fmtdesc *fmtdesc;
const struct v4l2_fmtdesc *fmtbase;
const struct v4l2_adapter_fmt *vadp_fmt;
struct v4l2_adapter_fh *vadp_fh = to_v4l2_adapter_fh(filp);
struct v4l2_adapter_device *vadp_dev = vadp_fh->dev;
struct v4l2_pix_format *pix = &fmt->fmt.pix;
int fmt_size, vadp_fmt_size;
mse_debug("START\n");
if (vadp_owner_get(vadp_fh))
return -EBUSY;
if (vadp_dev->type == MSE_TYPE_ADAPTER_VIDEO) {
fmtbase = g_formats_video;
fmt_size = ARRAY_SIZE(g_formats_video);
vadp_fmt = g_mse_adapter_v4l2_fmt_sizes_video;
vadp_fmt_size = ARRAY_SIZE(g_mse_adapter_v4l2_fmt_sizes_video);
} else if (vadp_dev->type == MSE_TYPE_ADAPTER_MPEG2TS) {
fmtbase = g_formats_mpeg;
fmt_size = ARRAY_SIZE(g_formats_mpeg);
vadp_fmt = g_mse_adapter_v4l2_fmt_sizes_mpeg;
vadp_fmt_size = ARRAY_SIZE(g_mse_adapter_v4l2_fmt_sizes_mpeg);
} else {
mse_err("Failed vdev type=%d\n", vadp_dev->type);
return -EPERM;
}
fmtdesc = get_fmtdesc(fmtbase, fmt_size, fmt);
if (!fmtdesc) {
mse_info("Unknown fourcc format=(0x%08x)\n", pix->pixelformat);
fmtdesc = &fmtbase[0];
pix->pixelformat = fmtdesc->pixelformat;
}
if (pix->field != V4L2_FIELD_NONE &&
pix->field != V4L2_FIELD_INTERLACED)
pix->field = V4L2_FIELD_NONE;
get_fmt_sizes(vadp_fmt, vadp_fmt_size, fmt);
if (vadp_dev->type == MSE_TYPE_ADAPTER_VIDEO) {
pix->bytesperline = pix->width * 2;
pix->sizeimage = pix->bytesperline * pix->height;
if (pix->sizeimage < MSE_ADAPTER_V4L2_MIN_VIDEO_SIZEIMAGE) {
pix->sizeimage = rounddown(
MSE_ADAPTER_V4L2_MIN_VIDEO_SIZEIMAGE,
pix->bytesperline);
}
} else {
pix->bytesperline = MSE_ADAPTER_V4L2_MPEG2TS_BYTESPERLINE;
pix->sizeimage = MSE_ADAPTER_V4L2_MPEG2TS_SIZEIMAGE;
}
mse_debug("END\n");
return 0;
}
static int mse_adapter_v4l2_g_fmt_vid(struct file *filp,
void *priv,
struct v4l2_format *fmt)
{
struct v4l2_adapter_fh *vadp_fh = to_v4l2_adapter_fh(filp);
struct v4l2_adapter_device *vadp_dev = vadp_fh->dev;
struct v4l2_pix_format *pix = &fmt->fmt.pix;
mse_debug("START\n");
*pix = vadp_dev->format;
mse_debug("END\n");
return 0;
}
static int mse_adapter_v4l2_s_fmt_vid(struct file *filp,
void *priv,
struct v4l2_format *fmt)
{