aboutsummaryrefslogtreecommitdiffstats
path: root/utils/v4l2-compliance/v4l2-test-io-config.cpp
blob: 63e96253da6576b77e3cd06d23f532a62b3c7ec3 (plain)
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
/*
    V4L2 API compliance input/output configuration ioctl tests.

    Copyright (C) 2011  Hans Verkuil <hverkuil@xs4all.nl>

    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., 51 Franklin Street, Suite 500, Boston, MA  02110-1335  USA
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <sys/ioctl.h>
#include "v4l2-compliance.h"

static int checkStd(struct node *node, bool has_std, v4l2_std_id mask, bool is_input)
{
	v4l2_std_id std_mask = 0;
	v4l2_std_id std = 0;
	struct v4l2_standard enumstd;
	unsigned i;
	int ret;

	ret = doioctl(node, VIDIOC_G_STD, &std);
	if (ret && has_std)
		return fail("STD cap set, but could not get standard\n");
	if (!ret && !has_std)
		return fail("STD cap not set, but could still get a standard\n");
	if (ret != ENOTTY && ret != ENODATA && !has_std)
		return fail("STD cap not set, but got wrong error code (%d)\n", ret);
	if (!ret && has_std) {
		if (std & ~mask)
			warn("current standard is invalid according to the standard mask\n");
		if (std == 0)
			return fail("Standard == 0?!\n");
		if (std & V4L2_STD_ATSC)
			return fail("Current standard contains ATSC standards. This is no longer supported\n");
	}
	ret = doioctl(node, VIDIOC_S_STD, &std);
	if (ret && has_std)
		return fail("STD cap set, but could not set standard\n");
	if (!ret && !has_std)
		return fail("STD cap not set, but could still set a standard\n");
	std = V4L2_STD_ATSC;
	ret = doioctl(node, VIDIOC_S_STD, &std);
	if (ret != ENODATA && ret != EINVAL && ret != ENOTTY)
		return fail("could set standard to ATSC, which is not supported anymore\n");
	for (i = 0; ; i++) {
		memset(&enumstd, 0xff, sizeof(enumstd));

		enumstd.index = i;
		ret = doioctl(node, VIDIOC_ENUMSTD, &enumstd);
		if (ret)
			break;
		std_mask |= enumstd.id;
		if (check_ustring(enumstd.name, sizeof(enumstd.name)))
			return fail("invalid standard name\n");
		if (check_0(enumstd.reserved, sizeof(enumstd.reserved)))
			return fail("reserved not zeroed\n");
		if (enumstd.framelines == 0)
			return fail("framelines not filled\n");
		if (enumstd.framelines != 625 && enumstd.framelines != 525)
			warn("strange framelines value %d for standard %08llx\n",
					enumstd.framelines, enumstd.id);
		if (enumstd.frameperiod.numerator == 0 || enumstd.frameperiod.denominator == 0)
			return fail("frameperiod is not filled\n");
		if (enumstd.index != i)
			return fail("index changed!\n");
		if (enumstd.id == 0)
			return fail("empty standard returned\n");
	}
	if (i == 0 && has_std)
		return fail("STD cap set, but no standards can be enumerated\n");
	if (i && !has_std)
		return fail("STD cap was not set, but standards can be enumerated\n");
	if (ret != ENOTTY && ret != ENODATA && !has_std)
		return fail("STD cap not set, but got wrong error code for enumeration (%d)\n", ret);
	if (std_mask & V4L2_STD_ATSC)
		return fail("STD mask contains ATSC standards. This is no longer supported\n");
	if (has_std && std_mask != mask)
		return fail("the union of ENUMSTD does not match the standard mask (%llx != %llx)\n",
				std_mask, mask);
	ret = doioctl(node, VIDIOC_QUERYSTD, &std);
	if (!ret && !has_std)
		return fail("STD cap was not set, but could still query standard\n");
	if (ret != ENOTTY && ret != ENODATA && !has_std)
		return fail("STD cap not set, but got wrong error code for query (%d)\n", ret);
	if (ret != ENOTTY && !is_input)
		return fail("this is an output, but could still query standard\n");
	if (!ret && is_input && (std & ~std_mask))
		return fail("QUERYSTD gives back an unsupported standard\n");
	return 0;
}

int testStd(struct node *node)
{
	int ret;
	unsigned i, o;
	bool has_std = false;

	for (i = 0; i < node->inputs; i++) {
		struct v4l2_input input;

		input.index = i;
		ret = doioctl(node, VIDIOC_ENUMINPUT, &input);
		if (ret)
			return fail("could not enumerate input %d?!\n", i);
		ret = doioctl(node, VIDIOC_S_INPUT, &input.index);
		if (ret)
			return fail("could not select input %d.\n", i);
		if (input.capabilities & V4L2_IN_CAP_STD)
			has_std = true;
		if (checkStd(node, input.capabilities & V4L2_IN_CAP_STD, input.std, true))
			return fail("STD failed for input %d.\n", i);
	}

	for (o = 0; o < node->outputs; o++) {
		struct v4l2_output output;

		output.index = o;
		ret = doioctl(node, VIDIOC_ENUMOUTPUT, &output);
		if (ret)
			return fail("could not enumerate output %d?!\n", o);
		ret = doioctl(node, VIDIOC_S_OUTPUT, &output.index);
		if (ret)
			return fail("could not select output %d.\n", o);
		if (output.capabilities & V4L2_OUT_CAP_STD)
			has_std = true;
		if (checkStd(node, output.capabilities & V4L2_OUT_CAP_STD, output.std, false))
			return fail("STD failed for output %d.\n", o);
	}
	return has_std ? 0 : ENOTTY;
}

static int checkTimings(struct node *node, bool has_timings, bool is_input)
{
	struct v4l2_enum_dv_timings enumtimings;
	struct v4l2_dv_timings timings;
	struct v4l2_format fmt = { 0 };
	bool is_mplane = node->g_caps() & (V4L2_CAP_VIDEO_CAPTURE_MPLANE |
				       V4L2_CAP_VIDEO_OUTPUT_MPLANE |
				       V4L2_CAP_VIDEO_M2M_MPLANE);
	unsigned type;
	unsigned i;
	int ret;

	memset(&timings, 0xff, sizeof(timings));
	if (node->can_capture)
		type = is_mplane ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE;
	else
		type = is_mplane ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : V4L2_BUF_TYPE_VIDEO_OUTPUT;
	ret = doioctl(node, VIDIOC_G_DV_TIMINGS, &timings);
	if (ret && has_timings)
		return fail("TIMINGS cap set, but could not get current timings\n");
	if (!ret && !has_timings)
		return fail("TIMINGS cap not set, but could still get timings\n");
	if (ret != ENOTTY && ret != ENODATA && !has_timings)
		return fail("TIMINGS cap not set, but got wrong error code (%d)\n", ret);

	if (!ret) {
		fail_on_test(timings.type != V4L2_DV_BT_656_1120);
		fail_on_test(check_0(timings.bt.reserved, sizeof(timings.bt.reserved)));
	}

	for (i = 0; ; i++) {
		memset(&enumtimings, 0xff, sizeof(enumtimings));

		enumtimings.index = i;
		enumtimings.pad = 0;
		ret = doioctl(node, VIDIOC_ENUM_DV_TIMINGS, &enumtimings);
		if (ret)
			break;
		if (check_0(enumtimings.reserved, sizeof(enumtimings.reserved)))
			return fail("reserved not zeroed\n");
		fail_on_test(check_0(enumtimings.timings.bt.reserved,
				     sizeof(enumtimings.timings.bt.reserved)));
		if (enumtimings.index != i)
			return fail("index changed!\n");
		memset(enumtimings.timings.bt.reserved, 0xff,
		       sizeof(enumtimings.timings.bt.reserved));
		fail_on_test(doioctl(node, VIDIOC_S_DV_TIMINGS, &enumtimings.timings));
		fail_on_test(check_0(enumtimings.timings.bt.reserved,
				     sizeof(enumtimings.timings.bt.reserved)));
		
		struct v4l2_dv_timings g_timings;
		fail_on_test(doioctl(node, VIDIOC_G_DV_TIMINGS, &g_timings));
		fail_on_test(g_timings.bt.width != enumtimings.timings.bt.width);
		fail_on_test(g_timings.bt.height != enumtimings.timings.bt.height);

		if (node->is_vbi)
			continue;
		fmt.type = type;
		fail_on_test(doioctl(node, VIDIOC_G_FMT, &fmt));

		unsigned field = is_mplane ? fmt.fmt.pix_mp.field : fmt.fmt.pix.field;

		if (enumtimings.timings.bt.interlaced)
			fail_on_test(field == V4L2_FIELD_NONE);
		else
			fail_on_test(field != V4L2_FIELD_NONE);

		unsigned factor = V4L2_FIELD_HAS_T_OR_B(field) ? 2 : 1;

		// Test if the new format after setting new timings is either too small
		// or more than 1.5 times the width/height. This allows for some
		// adjustments to be made to the format, but still detect if the
		// format isn't updated by the driver.
		if (is_mplane) {
			fail_on_test(fmt.fmt.pix_mp.width < enumtimings.timings.bt.width);
			fail_on_test(fmt.fmt.pix_mp.width >= enumtimings.timings.bt.width * 1.5);
			fail_on_test(fmt.fmt.pix_mp.height * factor < enumtimings.timings.bt.height);
			fail_on_test(fmt.fmt.pix_mp.height * factor >= enumtimings.timings.bt.height * 1.5);
		} else {
			fail_on_test(fmt.fmt.pix.width < enumtimings.timings.bt.width);
			fail_on_test(fmt.fmt.pix.width >= enumtimings.timings.bt.width * 1.5);
			fail_on_test(fmt.fmt.pix.height * factor < enumtimings.timings.bt.height);
			fail_on_test(fmt.fmt.pix.height * factor >= enumtimings.timings.bt.height * 1.5);
		}
	}
	if (i == 0 && has_timings)
		return fail("TIMINGS cap set, but no timings can be enumerated\n");
	if (i && !has_timings)
		return fail("TIMINGS cap was not set, but timings can be enumerated\n");
	if (ret != ENOTTY && ret != ENODATA && !has_timings)
		return fail("TIMINGS cap not set, but got wrong error code for enumeration (%d)\n", ret);
	if (has_timings)
		fail_on_test(doioctl(node, VIDIOC_S_DV_TIMINGS, &timings));
	ret = doioctl(node, VIDIOC_QUERY_DV_TIMINGS, &timings);
	if (!ret)
		fail_on_test(check_0(timings.bt.reserved, sizeof(timings.bt.reserved)));
	if (!ret && !has_timings)
		return fail("TIMINGS cap was not set, but could still query timings\n");
	if (ret != ENOTTY && ret != ENODATA && !has_timings)
		return fail("TIMINGS cap not set, but got wrong error code for query (%d)\n", ret);
	if (ret != ENOTTY && !is_input)
		return fail("this is an output, but could still query timings\n");
	return 0;
}

static int checkSubDevEnumTimings(struct node *node, __u32 pad)
{
	struct v4l2_enum_dv_timings enumtimings;
	struct v4l2_dv_timings timings;
	bool found_fmt_mismatch = false;
	bool has_timings;
	int ret;

	memset(&timings, 0xff, sizeof(timings));
	ret = doioctl(node, VIDIOC_G_DV_TIMINGS, &timings);
	has_timings = ret != ENOTTY;
	fail_on_test(has_timings && ret);
	if (has_timings) {
		fail_on_test(check_0(timings.bt.reserved, sizeof(timings.bt.reserved)));
		memset(timings.bt.reserved, 0xff, sizeof(timings.bt.reserved));
		fail_on_test(doioctl(node, VIDIOC_S_DV_TIMINGS, &timings));
		fail_on_test(check_0(timings.bt.reserved, sizeof(timings.bt.reserved)));
	} else {
		fail_on_test(doioctl(node, VIDIOC_S_DV_TIMINGS, &timings) != ENOTTY);
		fail_on_test(doioctl(node, VIDIOC_QUERY_DV_TIMINGS, &timings) != ENOTTY);
	}

	for (unsigned i = 0; ; i++) {
		memset(&enumtimings, 0xff, sizeof(enumtimings));

		enumtimings.index = i;
		enumtimings.pad = pad;
		ret = doioctl(node, VIDIOC_ENUM_DV_TIMINGS, &enumtimings);
		fail_on_test(ret != ENOTTY && !has_timings);
		fail_on_test(ret == ENOTTY && has_timings);
		if (ret == ENOTTY)
			return ret;
		fail_on_test(ret && ret != EINVAL);
		fail_on_test(ret && !i);
		if (ret)
			break;
		fail_on_test(check_0(enumtimings.reserved, sizeof(enumtimings.reserved)));
		fail_on_test(check_0(enumtimings.timings.bt.reserved,
				     sizeof(enumtimings.timings.bt.reserved)));
		fail_on_test(enumtimings.index != i);
		fail_on_test(enumtimings.pad != pad);
		memset(enumtimings.timings.bt.reserved, 0xff,
		       sizeof(enumtimings.timings.bt.reserved));
		fail_on_test(doioctl(node, VIDIOC_S_DV_TIMINGS, &enumtimings.timings));
		fail_on_test(check_0(enumtimings.timings.bt.reserved,
				     sizeof(enumtimings.timings.bt.reserved)));

		struct v4l2_dv_timings g_timings;

		fail_on_test(doioctl(node, VIDIOC_G_DV_TIMINGS, &g_timings));
		fail_on_test(g_timings.bt.width != enumtimings.timings.bt.width);
		fail_on_test(g_timings.bt.height != enumtimings.timings.bt.height);

		if (found_fmt_mismatch)
			continue;

		struct v4l2_subdev_format fmt;

		memset(&fmt, 0, sizeof(fmt));
		fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;

		// Test if the new format after setting new timings is either too small
		// or more than 1.5 times the width/height. This allows for some
		// adjustments to be made to the format, but still detect if the
		// format isn't updated by the driver.
		if (!doioctl(node, VIDIOC_SUBDEV_G_FMT, &fmt)) {
			if (V4L2_FIELD_HAS_T_OR_B(fmt.format.field))
				fmt.format.height *= 2;

			if (fmt.format.width < enumtimings.timings.bt.width) {
				warn("Active width for pad 0 is %u, which is less than the timings width of %u\n",
				     fmt.format.width, enumtimings.timings.bt.width);
				found_fmt_mismatch = true;
			}
			if (fmt.format.width >= enumtimings.timings.bt.width * 1.5) {
				warn("Active width for pad 0 is %u, which is more than 1.5 * the timings width of %u\n",
				     fmt.format.width, enumtimings.timings.bt.width);
				found_fmt_mismatch = true;
			}
			if (fmt.format.height < enumtimings.timings.bt.height) {
				warn("Active height for pad 0 is %u, which is less than the timings height of %u\n",
				     fmt.format.height, enumtimings.timings.bt.height);
				found_fmt_mismatch = true;
			}
			if (fmt.format.height >= enumtimings.timings.bt.height * 1.5) {
				warn("Active height for pad 0 is %u, which is more than 1.5 * the timings height of %u\n",
				     fmt.format.height, enumtimings.timings.bt.height);
				found_fmt_mismatch = true;
			}
		}
	}
	enumtimings.pad = node->entity.pads;
	enumtimings.index = 0;
	fail_on_test(doioctl(node, VIDIOC_ENUM_DV_TIMINGS, &enumtimings) != EINVAL);

	fail_on_test(doioctl(node, VIDIOC_S_DV_TIMINGS, &timings));
	memset(&timings, 0xff, sizeof(timings));
	ret = doioctl(node, VIDIOC_QUERY_DV_TIMINGS, &timings);
	fail_on_test(ret == ENOTTY);
	fail_on_test(ret == ENODATA);
	if (!ret || ret == ERANGE)
		fail_on_test(check_0(timings.bt.reserved, sizeof(timings.bt.reserved)));
	return 0;
}

int testTimings(struct node *node)
{
	int ret;
	unsigned i, o;
	bool has_timings = false;

	if (node->is_subdev()) {
		for (unsigned pad = 0; pad < node->entity.pads; pad++) {
			ret = checkSubDevEnumTimings(node, pad);
			if (!ret)
				has_timings = true;
			fail_on_test(ret && ret != ENOTTY);
		}
		return has_timings ? 0 : ENOTTY;
	}

	for (i = 0; i < node->inputs; i++) {
		struct v4l2_input input;

		input.index = i;
		ret = doioctl(node, VIDIOC_ENUMINPUT, &input);
		if (ret)
			return fail("could not enumerate input %d?!\n", i);
		ret = doioctl(node, VIDIOC_S_INPUT, &input.index);
		if (ret)
			return fail("could not select input %d.\n", i);
		if (input.capabilities & V4L2_IN_CAP_DV_TIMINGS)
			has_timings = true;
		if (checkTimings(node, input.capabilities & V4L2_IN_CAP_DV_TIMINGS, true))
			return fail("Timings check failed for input %d.\n", i);
	}

	for (o = 0; o < node->outputs; o++) {
		struct v4l2_output output;

		output.index = o;
		ret = doioctl(node, VIDIOC_ENUMOUTPUT, &output);
		if (ret)
			return fail("could not enumerate output %d?!\n", o);
		ret = doioctl(node, VIDIOC_S_OUTPUT, &output.index);
		if (ret)
			return fail("could not select output %d.\n", o);
		if (output.capabilities & V4L2_OUT_CAP_DV_TIMINGS)
			has_timings = true;
		if (checkTimings(node, output.capabilities & V4L2_OUT_CAP_DV_TIMINGS, false))
			return fail("Timings check failed for output %d.\n", o);
	}
	return has_timings ? 0 : ENOTTY;
}

static int checkTimingsCap(struct node *node, unsigned pad, bool has_timings)
{
	struct v4l2_dv_timings_cap timingscap;
	int ret;

	memset(&timingscap, 0xff, sizeof(timingscap));
	timingscap.pad = pad;
	ret = doioctl(node, VIDIOC_DV_TIMINGS_CAP, &timingscap);
	if (node->is_subdev() && ret == ENOTTY)
		return ENOTTY;
	if (ret && has_timings)
		return fail("TIMINGS cap set, but could not get timings caps\n");
	if (!ret && !has_timings)
		return fail("TIMINGS cap not set, but could still get timings caps\n");
	if (ret && !has_timings)
		return 0;
	if (check_0(timingscap.reserved, sizeof(timingscap.reserved)))
		return fail("reserved not zeroed\n");
	fail_on_test(timingscap.pad != pad);
	fail_on_test(timingscap.type != V4L2_DV_BT_656_1120);
	if (check_0(timingscap.bt.reserved, sizeof(timingscap.bt.reserved)))
		return fail("reserved not zeroed\n");
	fail_on_test(timingscap.bt.min_width > timingscap.bt.max_width);
	fail_on_test(timingscap.bt.min_height > timingscap.bt.max_height);
	fail_on_test(timingscap.bt.min_pixelclock > timingscap.bt.max_pixelclock);

	if (!node->is_subdev())
		return 0;

	memset(&timingscap, 0, sizeof(timingscap));
	timingscap.pad = node->is_subdev() ? node->entity.pads : 1;
	fail_on_test(doioctl(node, VIDIOC_DV_TIMINGS_CAP, &timingscap) != EINVAL);
	return 0;
}

int testTimingsCap(struct node *node)
{
	int ret;
	unsigned i, o;
	bool has_timings = false;

	if (node->is_subdev()) {
		for (unsigned pad = 0; pad < node->entity.pads; pad++) {
			bool is_input = node->pads[pad].flags & MEDIA_PAD_FL_SINK;

			ret = checkTimingsCap(node, pad, true);
			if (ret && ret != ENOTTY)
				return fail("EDID check failed for %s pad %u.\n",
					    is_input ? "sink" : "source", pad);
			if (!ret)
				has_timings = true;
		}
		return has_timings ? 0 : ENOTTY;
	}

	for (i = 0; i < node->inputs; i++) {
		struct v4l2_input input;

		input.index = i;
		ret = doioctl(node, VIDIOC_ENUMINPUT, &input);
		if (ret)
			return fail("could not enumerate input %d?!\n", i);
		ret = doioctl(node, VIDIOC_S_INPUT, &input.index);
		if (ret)
			return fail("could not select input %d.\n", i);
		if (input.capabilities & V4L2_IN_CAP_DV_TIMINGS)
			has_timings = true;
		if (checkTimingsCap(node, 0, input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
			return fail("Timings cap failed for input %d.\n", i);
	}

	for (o = 0; o < node->outputs; o++) {
		struct v4l2_output output;

		output.index = o;
		ret = doioctl(node, VIDIOC_ENUMOUTPUT, &output);
		if (ret)
			return fail("could not enumerate output %d?!\n", o);
		ret = doioctl(node, VIDIOC_S_OUTPUT, &output.index);
		if (ret)
			return fail("could not select output %d.\n", o);
		if (output.capabilities & V4L2_OUT_CAP_DV_TIMINGS)
			has_timings = true;
		if (checkTimingsCap(node, 0, output.capabilities & V4L2_OUT_CAP_DV_TIMINGS))
			return fail("Timings cap check failed for output %d.\n", o);
	}
	return has_timings ? 0 : ENOTTY;
}

static int checkEdid(struct node *node, unsigned pad, bool is_input)
{
	struct v4l2_edid edid;
	__u8 data[256 * 128] = { 0 };
	unsigned blocks;
	bool has_edid;
	int ret;

	memset(edid.reserved, 0xff, sizeof(edid.reserved));
	edid.pad = pad;
	edid.start_block = 0;
	edid.blocks = 0;
	edid.edid = (__u8 *)0xdeadbeef;
	ret = doioctl(node, VIDIOC_G_EDID, &edid);
	if (ret == ENOTTY) {
		memset(&edid, 0, sizeof(edid));
		edid.pad = pad;
		fail_on_test(doioctl(node, VIDIOC_S_EDID, &edid) != ENOTTY);
		return ENOTTY;
	}
	has_edid = ret == 0;
	fail_on_test(ret && ret != EINVAL);
	fail_on_test(!ret && check_0(edid.reserved, sizeof(edid.reserved)));
	fail_on_test(edid.start_block);
	fail_on_test(edid.blocks > 256);
	fail_on_test(!has_edid && edid.blocks);
	fail_on_test(edid.pad != pad);
	blocks = edid.blocks;
	edid.edid = data;
	ret = doioctl(node, VIDIOC_G_EDID, &edid);
	if (!has_edid)
		fail_on_test(ret != EINVAL);
	else
		fail_on_test(ret != 0 && ret != ENODATA);
	fail_on_test(edid.blocks != blocks);

	if (has_edid) {
		edid.start_block = edid.blocks ? edid.blocks : 1;
		ret = doioctl(node, VIDIOC_G_EDID, &edid);
		fail_on_test(ret != EINVAL && ret != ENODATA);
		if (blocks > 1) {
			edid.start_block = 1;
			edid.blocks = blocks;
			fail_on_test(doioctl(node, VIDIOC_G_EDID, &edid));
			fail_on_test(edid.blocks != blocks - 1);
		}

		edid.start_block = 0;
		edid.blocks = 256;
		ret = doioctl(node, VIDIOC_G_EDID, &edid);
		fail_on_test(ret && ret != ENODATA);
		if (!ret)
			fail_on_test(edid.blocks != blocks);
		edid.pad = ~0;
		fail_on_test(doioctl(node, VIDIOC_G_EDID, &edid) != EINVAL);
		edid.pad = pad;
	}

	memset(edid.reserved, 0xff, sizeof(edid.reserved));
	edid.blocks = blocks;
	ret = doioctl(node, VIDIOC_S_EDID, &edid);
	if (ret == ENOTTY) {
		fail_on_test(is_input);
		return 0;
	}
	fail_on_test(!is_input);
	if (!has_edid) {
		fail_on_test(ret != EINVAL);
		return 0;
	}
	fail_on_test(ret);
	fail_on_test(check_0(edid.reserved, sizeof(edid.reserved)));
	edid.blocks = blocks;
	edid.pad = ~0;
	ret = doioctl(node, VIDIOC_S_EDID, &edid);
	fail_on_test(ret != EINVAL);
	if (blocks < 256) {
		edid.blocks = 256;
		edid.pad = pad;
		ret = doioctl(node, VIDIOC_S_EDID, &edid);
		fail_on_test(ret != E2BIG);
		fail_on_test(edid.blocks == 0 || edid.blocks >= 256);
		fail_on_test(edid.pad != pad);
	}
	edid.blocks = blocks;
	edid.pad = pad;
	edid.edid = NULL;
	ret = doioctl(node, VIDIOC_S_EDID, &edid);
	fail_on_test(ret != EFAULT);
	edid.edid = (__u8 *)0xdeadbeef;
	ret = doioctl(node, VIDIOC_S_EDID, &edid);
	fail_on_test(ret != EFAULT);
	return 0;
}

int testEdid(struct node *node)
{
	bool has_edid = false;
	unsigned i, o;
	int ret;

	if (node->is_subdev()) {
		for (unsigned pad = 0; pad < node->entity.pads; pad++) {
			bool is_input = node->pads[pad].flags & MEDIA_PAD_FL_SOURCE;

			ret = checkEdid(node, pad, is_input);
			if (ret && ret != ENOTTY)
				return fail("EDID check failed for %s pad %u.\n",
					    is_input ? "source" : "sink", pad);
			if (!ret)
				has_edid = true;
		}
		return has_edid ? 0 : ENOTTY;
	}

	for (i = 0; i < node->inputs; i++) {
		struct v4l2_input input;

		input.index = i;
		ret = doioctl(node, VIDIOC_ENUMINPUT, &input);
		if (ret)
			return fail("could not enumerate input %d?!\n", i);
		ret = doioctl(node, VIDIOC_S_INPUT, &input.index);
		if (ret)
			return fail("could not select input %d.\n", i);
		ret = checkEdid(node, i, true);
		if (ret && ret != ENOTTY)
			return fail("EDID check failed for input %d.\n", i);
		if (input.capabilities & V4L2_IN_CAP_DV_TIMINGS)
			has_edid = !ret ? true : has_edid;
	}

	for (o = 0; o < node->outputs; o++) {
		struct v4l2_output output;

		output.index = o;
		ret = doioctl(node, VIDIOC_ENUMOUTPUT, &output);
		if (ret)
			return fail("could not enumerate output %d?!\n", o);
		ret = doioctl(node, VIDIOC_S_OUTPUT, &output.index);
		if (ret)
			return fail("could not select output %d.\n", o);
		ret = checkEdid(node, o, false);
		if (ret && ret != ENOTTY)
			return fail("EDID check failed for output %d.\n", o);
		if (output.capabilities & V4L2_OUT_CAP_DV_TIMINGS)
			has_edid = !ret ? true : has_edid;
	}
	return has_edid ? 0 : ENOTTY;
}

Privacy Policy