1 /*
2 * v4l-test: Test environment for Video For Linux Two API
3 *
4 * 7 May 2009 0.2 show_v4l2_*() function extracted to v4l2_show.c
5 * 29 Apr 2009 0.1 First release
6 *
7 * Written by M�rton N�meth <nm127@freemail.hu>
8 * Released under GPL
9 */
10
11 #include <stdio.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <sys/ioctl.h>
17 #include <errno.h>
18 #include <string.h>
19
20 #include <linux/videodev2.h>
21 #include <linux/errno.h>
22
23 #include <CUnit/CUnit.h>
24
25 #include "v4l2_test.h"
26 #include "v4l2_show.h"
27 #include "dev_video.h"
28 #include "video_limits.h"
29
30 #include "test_VIDIOC_REQBUFS.h"
31
do_VIDIOC_REQBUFS_capture_mmap(__u32 count)32 static void do_VIDIOC_REQBUFS_capture_mmap(__u32 count)
33 {
34 int ret_cap, errno_cap;
35 int ret_req, errno_req;
36 struct v4l2_capability cap;
37 struct v4l2_requestbuffers reqbuf;
38 struct v4l2_requestbuffers reqbuf2;
39
40 memset(&cap, 0, sizeof(cap));
41
42 ret_cap = ioctl(get_video_fd(), VIDIOC_QUERYCAP, &cap);
43 errno_cap = errno;
44
45 memset(&reqbuf, 0xff, sizeof(reqbuf));
46 reqbuf.count = count;
47 reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
48 reqbuf.memory = V4L2_MEMORY_MMAP;
49
50 ret_req = ioctl(get_video_fd(), VIDIOC_REQBUFS, &reqbuf);
51 errno_req = errno;
52
53 dprintf("\t%s:%u: VIDIOC_REQBUF, count=%u, ret_req=%i, errno_req=%i\n",
54 __FILE__, __LINE__, count, ret_req, errno_req);
55
56 if (ret_cap == 0 &&
57 (cap.capabilities & V4L2_CAP_STREAMING) &&
58 (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
59
60 CU_ASSERT_EQUAL(ret_cap, 0);
61 CU_ASSERT(cap.capabilities & V4L2_CAP_STREAMING);
62
63 CU_ASSERT_EQUAL(ret_req, 0);
64 //CU_ASSERT_EQUAL(reqbuf.count, ???);
65 CU_ASSERT_EQUAL(reqbuf.type, V4L2_BUF_TYPE_VIDEO_CAPTURE);
66 CU_ASSERT_EQUAL(reqbuf.memory, V4L2_MEMORY_MMAP);
67 CU_ASSERT_EQUAL(reqbuf.reserved[0], 0);
68 CU_ASSERT_EQUAL(reqbuf.reserved[1], 0);
69
70 } else {
71 CU_ASSERT_EQUAL(ret_req, -1);
72 CU_ASSERT_EQUAL(errno_req, EINVAL);
73
74 memset(&reqbuf2, 0xff, sizeof(reqbuf2));
75 reqbuf2.count = count;
76 reqbuf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
77 reqbuf2.memory = V4L2_MEMORY_MMAP;
78
79 CU_ASSERT_EQUAL(memcmp(&reqbuf, &reqbuf2, sizeof(reqbuf)), 0);
80 }
81
82 if (ret_req == 0) {
83 show_v4l2_requestbuffers(&reqbuf);
84 }
85
86 }
87
test_VIDIOC_REQBUFS_capture_mmap()88 void test_VIDIOC_REQBUFS_capture_mmap()
89 {
90 do_VIDIOC_REQBUFS_capture_mmap(0);
91 do_VIDIOC_REQBUFS_capture_mmap(1);
92 do_VIDIOC_REQBUFS_capture_mmap(2);
93 do_VIDIOC_REQBUFS_capture_mmap(3);
94 do_VIDIOC_REQBUFS_capture_mmap(4);
95 do_VIDIOC_REQBUFS_capture_mmap((__u32) S16_MIN);
96 do_VIDIOC_REQBUFS_capture_mmap((__u32) S16_MAX);
97 do_VIDIOC_REQBUFS_capture_mmap(U32_MAX);
98 do_VIDIOC_REQBUFS_capture_mmap(0);
99 }
100
do_VIDIOC_REQBUFS_capture_userptr(__u32 count)101 static void do_VIDIOC_REQBUFS_capture_userptr(__u32 count)
102 {
103 int ret_cap, errno_cap;
104 int ret_req, errno_req;
105 struct v4l2_capability cap;
106 struct v4l2_requestbuffers reqbuf;
107 struct v4l2_requestbuffers reqbuf2;
108
109 memset(&cap, 0, sizeof(cap));
110
111 ret_cap = ioctl(get_video_fd(), VIDIOC_QUERYCAP, &cap);
112 errno_cap = errno;
113
114 memset(&reqbuf, 0xff, sizeof(reqbuf));
115 reqbuf.count = count;
116 reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
117 reqbuf.memory = V4L2_MEMORY_USERPTR;
118
119 ret_req = ioctl(get_video_fd(), VIDIOC_REQBUFS, &reqbuf);
120 errno_req = errno;
121
122 dprintf("\t%s:%u: VIDIOC_REQBUF, count=%u, ret_req=%i, errno_req=%i\n",
123 __FILE__, __LINE__, count, ret_req, errno_req);
124
125 if (ret_cap == 0 &&
126 (cap.capabilities & V4L2_CAP_STREAMING) &&
127 (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) && ret_req == 0) {
128 CU_ASSERT_EQUAL(ret_cap, 0);
129 CU_ASSERT(cap.capabilities & V4L2_CAP_STREAMING);
130
131 CU_ASSERT_EQUAL(ret_req, 0);
132 //CU_ASSERT_EQUAL(reqbuf.count, ???);
133 CU_ASSERT_EQUAL(reqbuf.type, V4L2_BUF_TYPE_VIDEO_CAPTURE);
134 CU_ASSERT_EQUAL(reqbuf.memory, V4L2_MEMORY_USERPTR);
135 CU_ASSERT_EQUAL(reqbuf.reserved[0], 0);
136 CU_ASSERT_EQUAL(reqbuf.reserved[1], 0);
137
138 } else {
139 CU_ASSERT_EQUAL(ret_req, -1);
140 CU_ASSERT_EQUAL(errno_req, EINVAL);
141
142 memset(&reqbuf2, 0xff, sizeof(reqbuf2));
143 reqbuf2.count = count;
144 reqbuf2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
145 reqbuf2.memory = V4L2_MEMORY_USERPTR;
146
147 CU_ASSERT_EQUAL(memcmp(&reqbuf, &reqbuf2, sizeof(reqbuf)), 0);
148 }
149
150 if (ret_req == 0) {
151 show_v4l2_requestbuffers(&reqbuf);
152 }
153
154 }
155
test_VIDIOC_REQBUFS_capture_userptr()156 void test_VIDIOC_REQBUFS_capture_userptr()
157 {
158 do_VIDIOC_REQBUFS_capture_userptr(0);
159 do_VIDIOC_REQBUFS_capture_userptr(1);
160 do_VIDIOC_REQBUFS_capture_userptr((__u32) S16_MIN);
161 do_VIDIOC_REQBUFS_capture_userptr((__u32) S16_MAX);
162 do_VIDIOC_REQBUFS_capture_userptr(U32_MAX);
163 do_VIDIOC_REQBUFS_capture_userptr(0);
164 }
165
do_VIDIOC_REQBUFS_output_mmap(__u32 count)166 static void do_VIDIOC_REQBUFS_output_mmap(__u32 count)
167 {
168 int ret_cap, errno_cap;
169 int ret_req, errno_req;
170 struct v4l2_capability cap;
171 struct v4l2_requestbuffers reqbuf;
172 struct v4l2_requestbuffers reqbuf2;
173
174 memset(&cap, 0, sizeof(cap));
175
176 ret_cap = ioctl(get_video_fd(), VIDIOC_QUERYCAP, &cap);
177 errno_cap = errno;
178
179 memset(&reqbuf, 0xff, sizeof(reqbuf));
180 reqbuf.count = count;
181 reqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
182 reqbuf.memory = V4L2_MEMORY_MMAP;
183
184 ret_req = ioctl(get_video_fd(), VIDIOC_REQBUFS, &reqbuf);
185 errno_req = errno;
186
187 dprintf("\t%s:%u: VIDIOC_REQBUF, count=%u, ret_req=%i, errno_req=%i\n",
188 __FILE__, __LINE__, count, ret_req, errno_req);
189
190 if (ret_cap == 0 &&
191 (cap.capabilities & V4L2_CAP_STREAMING) &&
192 (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)) {
193
194 CU_ASSERT_EQUAL(ret_cap, 0);
195 CU_ASSERT(cap.capabilities & V4L2_CAP_STREAMING);
196
197 CU_ASSERT_EQUAL(ret_req, 0);
198 //CU_ASSERT_EQUAL(reqbuf.count, ???);
199 CU_ASSERT_EQUAL(reqbuf.type, V4L2_BUF_TYPE_VIDEO_OUTPUT);
200 CU_ASSERT_EQUAL(reqbuf.memory, V4L2_MEMORY_MMAP);
201 CU_ASSERT_EQUAL(reqbuf.reserved[0], 0);
202 CU_ASSERT_EQUAL(reqbuf.reserved[1], 0);
203
204 } else {
205 CU_ASSERT_EQUAL(ret_req, -1);
206 CU_ASSERT_EQUAL(errno_req, EINVAL);
207
208 memset(&reqbuf2, 0xff, sizeof(reqbuf2));
209 reqbuf2.count = count;
210 reqbuf2.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
211 reqbuf2.memory = V4L2_MEMORY_MMAP;
212
213 CU_ASSERT_EQUAL(memcmp(&reqbuf, &reqbuf2, sizeof(reqbuf)), 0);
214 }
215
216 if (ret_req == 0) {
217 show_v4l2_requestbuffers(&reqbuf);
218 }
219
220 }
221
test_VIDIOC_REQBUFS_output_mmap()222 void test_VIDIOC_REQBUFS_output_mmap()
223 {
224 do_VIDIOC_REQBUFS_output_mmap(0);
225 do_VIDIOC_REQBUFS_output_mmap(1);
226 do_VIDIOC_REQBUFS_output_mmap(2);
227 do_VIDIOC_REQBUFS_output_mmap(3);
228 do_VIDIOC_REQBUFS_output_mmap(4);
229 do_VIDIOC_REQBUFS_output_mmap((__u32) S16_MIN);
230 do_VIDIOC_REQBUFS_output_mmap((__u32) S16_MAX);
231 do_VIDIOC_REQBUFS_output_mmap(U32_MAX);
232 do_VIDIOC_REQBUFS_output_mmap(0);
233 }
234
do_VIDIOC_REQBUFS_output_userptr(__u32 count)235 static void do_VIDIOC_REQBUFS_output_userptr(__u32 count)
236 {
237 int ret_cap, errno_cap;
238 int ret_req, errno_req;
239 struct v4l2_capability cap;
240 struct v4l2_requestbuffers reqbuf;
241 struct v4l2_requestbuffers reqbuf2;
242
243 memset(&cap, 0, sizeof(cap));
244
245 ret_cap = ioctl(get_video_fd(), VIDIOC_QUERYCAP, &cap);
246 errno_cap = errno;
247
248 memset(&reqbuf, 0xff, sizeof(reqbuf));
249 reqbuf.count = count;
250 reqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
251 reqbuf.memory = V4L2_MEMORY_USERPTR;
252
253 ret_req = ioctl(get_video_fd(), VIDIOC_REQBUFS, &reqbuf);
254 errno_req = errno;
255
256 dprintf("\t%s:%u: VIDIOC_REQBUF, count=%u, ret_req=%i, errno_req=%i\n",
257 __FILE__, __LINE__, count, ret_req, errno_req);
258
259 if (ret_cap == 0 &&
260 (cap.capabilities & V4L2_CAP_STREAMING) &&
261 (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT) && ret_req == 0) {
262 CU_ASSERT_EQUAL(ret_cap, 0);
263 CU_ASSERT(cap.capabilities & V4L2_CAP_STREAMING);
264
265 CU_ASSERT_EQUAL(ret_req, 0);
266 //CU_ASSERT_EQUAL(reqbuf.count, ???);
267 CU_ASSERT_EQUAL(reqbuf.type, V4L2_BUF_TYPE_VIDEO_OUTPUT);
268 CU_ASSERT_EQUAL(reqbuf.memory, V4L2_MEMORY_USERPTR);
269 CU_ASSERT_EQUAL(reqbuf.reserved[0], 0);
270 CU_ASSERT_EQUAL(reqbuf.reserved[1], 0);
271
272 } else {
273 CU_ASSERT_EQUAL(ret_req, -1);
274 CU_ASSERT_EQUAL(errno_req, EINVAL);
275
276 memset(&reqbuf2, 0xff, sizeof(reqbuf2));
277 reqbuf2.count = count;
278 reqbuf2.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
279 reqbuf2.memory = V4L2_MEMORY_USERPTR;
280
281 CU_ASSERT_EQUAL(memcmp(&reqbuf, &reqbuf2, sizeof(reqbuf)), 0);
282 }
283
284 if (ret_req == 0) {
285 show_v4l2_requestbuffers(&reqbuf);
286 }
287
288 }
289
test_VIDIOC_REQBUFS_output_userptr()290 void test_VIDIOC_REQBUFS_output_userptr()
291 {
292 do_VIDIOC_REQBUFS_output_userptr(0);
293 do_VIDIOC_REQBUFS_output_userptr(1);
294 do_VIDIOC_REQBUFS_output_userptr((__u32) S16_MIN);
295 do_VIDIOC_REQBUFS_output_userptr((__u32) S16_MAX);
296 do_VIDIOC_REQBUFS_output_userptr(U32_MAX);
297 do_VIDIOC_REQBUFS_output_userptr(0);
298 }
299
do_VIDIOC_REQBUFS_invalid_memory(enum v4l2_buf_type type,enum v4l2_memory memory)300 static void do_VIDIOC_REQBUFS_invalid_memory(enum v4l2_buf_type type,
301 enum v4l2_memory memory)
302 {
303 int ret_req, errno_req;
304 struct v4l2_requestbuffers reqbuf;
305 struct v4l2_requestbuffers reqbuf2;
306
307 memset(&reqbuf, 0xff, sizeof(reqbuf));
308 reqbuf.count = 0;
309 reqbuf.type = type;
310 reqbuf.memory = memory;
311
312 ret_req = ioctl(get_video_fd(), VIDIOC_REQBUFS, &reqbuf);
313 errno_req = errno;
314
315 dprintf("\t%s:%u: VIDIOC_REQBUF, type=0%x, ret_req=%i, errno_req=%i\n",
316 __FILE__, __LINE__, type, ret_req, errno_req);
317
318 CU_ASSERT_EQUAL(ret_req, -1);
319 CU_ASSERT_EQUAL(errno_req, EINVAL);
320
321 memset(&reqbuf2, 0xff, sizeof(reqbuf2));
322 reqbuf2.count = 0;
323 reqbuf2.type = type;
324 reqbuf2.memory = memory;
325
326 CU_ASSERT_EQUAL(memcmp(&reqbuf, &reqbuf2, sizeof(reqbuf)), 0);
327
328 if (ret_req == 0) {
329 show_v4l2_requestbuffers(&reqbuf);
330 }
331
332 }
333
test_VIDIOC_REQBUFS_invalid_memory_capture()334 void test_VIDIOC_REQBUFS_invalid_memory_capture()
335 {
336 do_VIDIOC_REQBUFS_invalid_memory(V4L2_BUF_TYPE_VIDEO_CAPTURE, SINT_MIN);
337 do_VIDIOC_REQBUFS_invalid_memory(V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
338 do_VIDIOC_REQBUFS_invalid_memory(V4L2_BUF_TYPE_VIDEO_CAPTURE,
339 V4L2_MEMORY_OVERLAY);
340 do_VIDIOC_REQBUFS_invalid_memory(V4L2_BUF_TYPE_VIDEO_CAPTURE,
341 V4L2_MEMORY_OVERLAY + 1);
342 do_VIDIOC_REQBUFS_invalid_memory(V4L2_BUF_TYPE_VIDEO_CAPTURE, SINT_MAX);
343 }
344
test_VIDIOC_REQBUFS_invalid_memory_output()345 void test_VIDIOC_REQBUFS_invalid_memory_output()
346 {
347 do_VIDIOC_REQBUFS_invalid_memory(V4L2_BUF_TYPE_VIDEO_OUTPUT, SINT_MIN);
348 do_VIDIOC_REQBUFS_invalid_memory(V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
349 do_VIDIOC_REQBUFS_invalid_memory(V4L2_BUF_TYPE_VIDEO_OUTPUT,
350 V4L2_MEMORY_OVERLAY);
351 do_VIDIOC_REQBUFS_invalid_memory(V4L2_BUF_TYPE_VIDEO_OUTPUT,
352 V4L2_MEMORY_OVERLAY + 1);
353 do_VIDIOC_REQBUFS_invalid_memory(V4L2_BUF_TYPE_VIDEO_OUTPUT, SINT_MAX);
354 }
355
do_VIDIOC_REQBUFS_invalid_type(enum v4l2_memory memory,enum v4l2_buf_type type)356 static void do_VIDIOC_REQBUFS_invalid_type(enum v4l2_memory memory,
357 enum v4l2_buf_type type)
358 {
359 int ret_req, errno_req;
360 struct v4l2_requestbuffers reqbuf;
361 struct v4l2_requestbuffers reqbuf2;
362 __u32 count;
363
364 count = 1;
365
366 memset(&reqbuf, 0xff, sizeof(reqbuf));
367 reqbuf.count = count;
368 reqbuf.type = type;
369 reqbuf.memory = memory;
370
371 ret_req = ioctl(get_video_fd(), VIDIOC_REQBUFS, &reqbuf);
372 errno_req = errno;
373
374 dprintf
375 ("\t%s:%u: VIDIOC_REQBUF, type=0x%x, memory=%i, ret_req=%i, errno_req=%i\n",
376 __FILE__, __LINE__, type, memory, ret_req, errno_req);
377
378 CU_ASSERT_EQUAL(ret_req, -1);
379 CU_ASSERT_EQUAL(errno_req, EINVAL);
380
381 memset(&reqbuf2, 0xff, sizeof(reqbuf2));
382 reqbuf2.count = count;
383 reqbuf2.type = type;
384 reqbuf2.memory = memory;
385
386 CU_ASSERT_EQUAL(memcmp(&reqbuf, &reqbuf2, sizeof(reqbuf)), 0);
387
388 if (ret_req == 0) {
389 show_v4l2_requestbuffers(&reqbuf);
390 }
391 }
392
test_VIDIOC_REQUBUFS_invalid_type_mmap()393 void test_VIDIOC_REQUBUFS_invalid_type_mmap()
394 {
395 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_MMAP, 0);
396 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_MMAP,
397 V4L2_BUF_TYPE_VIDEO_OVERLAY);
398 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_MMAP,
399 V4L2_BUF_TYPE_VBI_CAPTURE);
400 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_MMAP,
401 V4L2_BUF_TYPE_VBI_OUTPUT);
402 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_MMAP,
403 V4L2_BUF_TYPE_SLICED_VBI_CAPTURE);
404 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_MMAP,
405 V4L2_BUF_TYPE_SLICED_VBI_OUTPUT);
406 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_MMAP,
407 V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY);
408 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_MMAP,
409 V4L2_BUF_TYPE_PRIVATE - 1);
410 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_MMAP, V4L2_BUF_TYPE_PRIVATE);
411 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_MMAP,
412 V4L2_BUF_TYPE_PRIVATE + 1);
413 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_MMAP, S32_MAX);
414 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_MMAP,
415 (__s32) ((__u32) S32_MAX + 1));
416 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_MMAP, U32_MAX - 1);
417 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_MMAP, U32_MAX);
418
419 }
420
test_VIDIOC_REQUBUFS_invalid_type_userptr()421 void test_VIDIOC_REQUBUFS_invalid_type_userptr()
422 {
423 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_USERPTR, 0);
424 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_USERPTR,
425 V4L2_BUF_TYPE_VIDEO_OVERLAY);
426 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_USERPTR,
427 V4L2_BUF_TYPE_VBI_CAPTURE);
428 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_USERPTR,
429 V4L2_BUF_TYPE_VBI_OUTPUT);
430 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_USERPTR,
431 V4L2_BUF_TYPE_SLICED_VBI_CAPTURE);
432 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_USERPTR,
433 V4L2_BUF_TYPE_SLICED_VBI_OUTPUT);
434 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_USERPTR,
435 V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY);
436 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_USERPTR,
437 V4L2_BUF_TYPE_PRIVATE - 1);
438 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_USERPTR,
439 V4L2_BUF_TYPE_PRIVATE);
440 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_USERPTR,
441 V4L2_BUF_TYPE_PRIVATE + 1);
442 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_USERPTR, S32_MAX);
443 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_USERPTR,
444 (__s32) ((__u32) S32_MAX + 1));
445 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_USERPTR, U32_MAX - 1);
446 do_VIDIOC_REQBUFS_invalid_type(V4L2_MEMORY_USERPTR, U32_MAX);
447
448 }
449