1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "tests.h"
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/ioctl.h>
32 #include <linux/types.h>
33 #include <linux/videodev2.h>
34 
35 #if WORDS_BIGENDIAN
36 # define cc0(arg) ((unsigned int) (unsigned char) ((unsigned int) (arg) >> 24))
37 # define cc1(arg) ((unsigned int) (unsigned char) ((unsigned int) (arg) >> 16))
38 # define cc2(arg) ((unsigned int) (unsigned char) ((unsigned int) (arg) >> 8))
39 # define cc3(arg) ((unsigned int) (unsigned char) (arg))
40 # define fourcc(a0, a1, a2, a3) \
41 	((unsigned int)(a3) | \
42 	 ((unsigned int)(a2) << 8) | \
43 	 ((unsigned int)(a1) << 16) | \
44 	 ((unsigned int)(a0) << 24))
45 #else
46 # define cc0(arg) ((unsigned int) (unsigned char) (arg))
47 # define cc1(arg) ((unsigned int) (unsigned char) ((unsigned int) (arg) >> 8))
48 # define cc2(arg) ((unsigned int) (unsigned char) ((unsigned int) (arg) >> 16))
49 # define cc3(arg) ((unsigned int) (unsigned char) ((unsigned int) (arg) >> 24))
50 # define fourcc(a0, a1, a2, a3) \
51 	((unsigned int)(a0) | \
52 	 ((unsigned int)(a1) << 8) | \
53 	 ((unsigned int)(a2) << 16) | \
54 	 ((unsigned int)(a3) << 24))
55 #endif
56 
57 static const unsigned int magic = 0xdeadbeef;
58 
59 int
main(void)60 main(void )
61 {
62 	const unsigned int size = get_page_size();
63 	void *const page = tail_alloc(size);
64 	fill_memory(page, size);
65 
66 	unsigned char cc[sizeof(int)] = { 'A', '\'', '\\', '\xfa' };
67 
68 	/* VIDIOC_QUERYCAP */
69 	ioctl(-1, VIDIOC_QUERYCAP, 0);
70 	printf("ioctl(-1, VIDIOC_QUERYCAP, NULL) = -1 EBADF (%m)\n");
71 
72 	ioctl(-1, VIDIOC_QUERYCAP, page);
73 	printf("ioctl(-1, VIDIOC_QUERYCAP, %p) = -1 EBADF (%m)\n", page);
74 
75 	/* VIDIOC_ENUM_FMT */
76 	ioctl(-1, VIDIOC_ENUM_FMT, 0);
77 	printf("ioctl(-1, VIDIOC_ENUM_FMT, NULL) = -1 EBADF (%m)\n");
78 
79 	struct v4l2_fmtdesc *const p_fmtdesc = tail_alloc(sizeof(*p_fmtdesc));
80 	p_fmtdesc->index = magic;
81 	p_fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
82 	ioctl(-1, VIDIOC_ENUM_FMT, p_fmtdesc);
83 	printf("ioctl(-1, VIDIOC_ENUM_FMT, {index=%u"
84 	       ", type=V4L2_BUF_TYPE_VIDEO_CAPTURE}) = -1 EBADF (%m)\n",
85 	       p_fmtdesc->index);
86 
87 	/* VIDIOC_G_FMT */
88 	ioctl(-1, VIDIOC_G_FMT, 0);
89 	printf("ioctl(-1, VIDIOC_G_FMT, NULL) = -1 EBADF (%m)\n");
90 
91 	struct v4l2_format *const p_format = tail_alloc(sizeof(*p_format));
92 	p_format->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
93 
94 	ioctl(-1, VIDIOC_G_FMT, p_format);
95 	printf("ioctl(-1, VIDIOC_G_FMT"
96 	       ", {type=V4L2_BUF_TYPE_VIDEO_CAPTURE}) = -1 EBADF (%m)\n");
97 
98 	/* VIDIOC_S_FMT */
99 	ioctl(-1, VIDIOC_S_FMT, 0);
100 	printf("ioctl(-1, VIDIOC_S_FMT, NULL) = -1 EBADF (%m)\n");
101 
102 	p_format->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
103 	p_format->fmt.pix.width = 0xdad1beaf;
104 	p_format->fmt.pix.height = 0xdad2beaf;
105 	p_format->fmt.pix.pixelformat = magic;
106 	p_format->fmt.pix.field = V4L2_FIELD_NONE;
107 	p_format->fmt.pix.bytesperline = 0xdad3beaf;
108 	p_format->fmt.pix.sizeimage = 0xdad4beaf;
109 	p_format->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
110 
111 	ioctl(-1, VIDIOC_S_FMT, p_format);
112 	printf("ioctl(-1, VIDIOC_S_FMT, {type=V4L2_BUF_TYPE_VIDEO_OUTPUT"
113 	       ", fmt.pix={width=%u, height=%u, pixelformat="
114 	       "v4l2_fourcc('\\x%x', '\\x%x', '\\x%x', '\\x%x')"
115 	       ", field=V4L2_FIELD_NONE, bytesperline=%u, sizeimage=%u"
116 	       ", colorspace=V4L2_COLORSPACE_JPEG}}) = -1 EBADF (%m)\n",
117 	       p_format->fmt.pix.width, p_format->fmt.pix.height,
118 	       cc0(magic), cc1(magic), cc2(magic), cc3(magic),
119 	       p_format->fmt.pix.bytesperline, p_format->fmt.pix.sizeimage);
120 
121 	/* VIDIOC_TRY_FMT */
122 	ioctl(-1, VIDIOC_TRY_FMT, 0);
123 	printf("ioctl(-1, VIDIOC_TRY_FMT, NULL) = -1 EBADF (%m)\n");
124 
125 #if HAVE_DECL_V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
126 	memset(p_format, -1, sizeof(*p_format));
127 	p_format->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
128 	p_format->fmt.pix_mp.width = 0xdad1beaf;
129 	p_format->fmt.pix_mp.height = 0xdad2beaf;
130 	p_format->fmt.pix_mp.pixelformat = magic;
131 	p_format->fmt.pix_mp.field = V4L2_FIELD_NONE;
132 	p_format->fmt.pix_mp.colorspace = V4L2_COLORSPACE_JPEG;
133 	unsigned int i;
134 	for (i = 0; i < ARRAY_SIZE(p_format->fmt.pix_mp.plane_fmt); ++i) {
135 		p_format->fmt.pix_mp.plane_fmt[i].sizeimage = 0xbadc0de0 | i;
136 		p_format->fmt.pix_mp.plane_fmt[i].bytesperline = 0xdadbeaf0 | i;
137 	}
138 	ioctl(-1, VIDIOC_TRY_FMT, p_format);
139 	printf("ioctl(-1, VIDIOC_TRY_FMT"
140 	       ", {type=V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE"
141 	       ", fmt.pix_mp={width=%u, height=%u, pixelformat="
142 	       "v4l2_fourcc('\\x%x', '\\x%x', '\\x%x', '\\x%x')"
143 	       ", field=V4L2_FIELD_NONE, colorspace=V4L2_COLORSPACE_JPEG"
144 	       ", plane_fmt=[",
145 	       p_format->fmt.pix_mp.width, p_format->fmt.pix_mp.height,
146 	       cc0(magic), cc1(magic), cc2(magic), cc3(magic));
147 	for (i = 0; i < ARRAY_SIZE(p_format->fmt.pix_mp.plane_fmt); ++i) {
148 		if (i)
149 			printf(", ");
150 		printf("{sizeimage=%u, bytesperline=%u}",
151 		       p_format->fmt.pix_mp.plane_fmt[i].sizeimage,
152 		       p_format->fmt.pix_mp.plane_fmt[i].bytesperline);
153 	}
154 	printf("], num_planes=%u}}) = -1 EBADF (%m)\n",
155 	       p_format->fmt.pix_mp.num_planes);
156 #else
157 	struct v4l2_format *const p_v4l2_format =
158 		page + size - sizeof(*p_v4l2_format);
159 	ioctl(-1, VIDIOC_TRY_FMT, p_v4l2_format);
160 	printf("ioctl(-1, VIDIOC_TRY_FMT, {type=%#x /* V4L2_BUF_TYPE_??? */})"
161 	       " = -1 EBADF (%m)\n", p_v4l2_format->type);
162 #endif
163 
164 	/* VIDIOC_REQBUFS */
165 	ioctl(-1, VIDIOC_REQBUFS, 0);
166 	printf("ioctl(-1, VIDIOC_REQBUFS, NULL) = -1 EBADF (%m)\n");
167 
168 	struct v4l2_requestbuffers *const p_v4l2_requestbuffers =
169 		page + size - sizeof(*p_v4l2_requestbuffers);
170 	ioctl(-1, VIDIOC_REQBUFS, p_v4l2_requestbuffers);
171 	printf("ioctl(-1, VIDIOC_REQBUFS, {count=%u, type=%#x"
172 	       " /* V4L2_BUF_TYPE_??? */, memory=%#x /* V4L2_MEMORY_??? */})"
173 	       " = -1 EBADF (%m)\n",
174 	       p_v4l2_requestbuffers->count,
175 	       p_v4l2_requestbuffers->type,
176 	       p_v4l2_requestbuffers->memory);
177 
178 	/* VIDIOC_QUERYBUF */
179 	ioctl(-1, VIDIOC_QUERYBUF, 0);
180 	printf("ioctl(-1, VIDIOC_QUERYBUF, NULL) = -1 EBADF (%m)\n");
181 
182 	struct v4l2_buffer *const p_v4l2_buffer =
183 		page + size - sizeof(*p_v4l2_buffer);
184 	ioctl(-1, VIDIOC_QUERYBUF, p_v4l2_buffer);
185 	printf("ioctl(-1, VIDIOC_QUERYBUF, {type=%#x /* V4L2_BUF_TYPE_??? */"
186 	       ", index=%u}) = -1 EBADF (%m)\n",
187 	       p_v4l2_buffer->type, p_v4l2_buffer->index);
188 
189 	/* VIDIOC_QBUF */
190 	ioctl(-1, VIDIOC_QBUF, 0);
191 	printf("ioctl(-1, VIDIOC_QBUF, NULL) = -1 EBADF (%m)\n");
192 
193 	ioctl(-1, VIDIOC_QBUF, p_v4l2_buffer);
194 	printf("ioctl(-1, VIDIOC_QBUF, {type=%#x /* V4L2_BUF_TYPE_??? */"
195 	       ", index=%u}) = -1 EBADF (%m)\n",
196 	       p_v4l2_buffer->type, p_v4l2_buffer->index);
197 
198 	/* VIDIOC_DQBUF */
199 	ioctl(-1, VIDIOC_DQBUF, 0);
200 	printf("ioctl(-1, VIDIOC_DQBUF, NULL) = -1 EBADF (%m)\n");
201 
202 	ioctl(-1, VIDIOC_DQBUF, p_v4l2_buffer);
203 	printf("ioctl(-1, VIDIOC_DQBUF, {type=%#x"
204 	       " /* V4L2_BUF_TYPE_??? */}) = -1 EBADF (%m)\n",
205 	       p_v4l2_buffer->type);
206 
207 	/* VIDIOC_G_FBUF */
208 	ioctl(-1, VIDIOC_G_FBUF, 0);
209 	printf("ioctl(-1, VIDIOC_G_FBUF, NULL) = -1 EBADF (%m)\n");
210 
211 	ioctl(-1, VIDIOC_G_FBUF, page);
212 	printf("ioctl(-1, VIDIOC_G_FBUF, %p) = -1 EBADF (%m)\n", page);
213 
214 	/* VIDIOC_S_FBUF */
215 	ioctl(-1, VIDIOC_S_FBUF, 0);
216 	printf("ioctl(-1, VIDIOC_S_FBUF, NULL) = -1 EBADF (%m)\n");
217 
218 	struct v4l2_framebuffer *const p_v4l2_framebuffer =
219 		page + size - sizeof(*p_v4l2_framebuffer);
220 	ioctl(-1, VIDIOC_S_FBUF, p_v4l2_framebuffer);
221 	printf("ioctl(-1, VIDIOC_S_FBUF, {capability=%#x"
222 	       ", flags=%#x, base=%p}) = -1 EBADF (%m)\n",
223 	       p_v4l2_framebuffer->capability,
224 	       p_v4l2_framebuffer->flags,
225 	       p_v4l2_framebuffer->base);
226 
227 	/* VIDIOC_STREAMON */
228 	ioctl(-1, VIDIOC_STREAMON, 0);
229 	printf("ioctl(-1, VIDIOC_STREAMON, NULL) = -1 EBADF (%m)\n");
230 
231 	int *const p_int = page + size - sizeof(int);
232 	ioctl(-1, VIDIOC_STREAMON, p_int);
233 	printf("ioctl(-1, VIDIOC_STREAMON, [%#x /* V4L2_BUF_TYPE_??? */])"
234 	       " = -1 EBADF (%m)\n", *p_int);
235 
236 	/* VIDIOC_STREAMOFF */
237 	ioctl(-1, VIDIOC_STREAMOFF, 0);
238 	printf("ioctl(-1, VIDIOC_STREAMOFF, NULL) = -1 EBADF (%m)\n");
239 
240 	ioctl(-1, VIDIOC_STREAMOFF, p_int);
241 	printf("ioctl(-1, VIDIOC_STREAMOFF, [%#x /* V4L2_BUF_TYPE_??? */])"
242 	       " = -1 EBADF (%m)\n", *p_int);
243 
244 	/* VIDIOC_G_PARM */
245 	ioctl(-1, VIDIOC_G_PARM, 0);
246 	printf("ioctl(-1, VIDIOC_G_PARM, NULL) = -1 EBADF (%m)\n");
247 
248 	struct v4l2_streamparm *const p_v4l2_streamparm =
249 		page + size - sizeof(*p_v4l2_streamparm);
250 	ioctl(-1, VIDIOC_G_PARM, p_v4l2_streamparm);
251 	printf("ioctl(-1, VIDIOC_G_PARM, {type=%#x /* V4L2_BUF_TYPE_??? */})"
252 	       " = -1 EBADF (%m)\n", p_v4l2_streamparm->type);
253 
254 	/* VIDIOC_S_PARM */
255 	ioctl(-1, VIDIOC_S_PARM, 0);
256 	printf("ioctl(-1, VIDIOC_S_PARM, NULL) = -1 EBADF (%m)\n");
257 
258 	ioctl(-1, VIDIOC_S_PARM, p_v4l2_streamparm);
259 	printf("ioctl(-1, VIDIOC_S_PARM, {type=%#x /* V4L2_BUF_TYPE_??? */})"
260 	       " = -1 EBADF (%m)\n", p_v4l2_streamparm->type);
261 
262 	struct v4l2_streamparm *const p_streamparm =
263 		tail_alloc(sizeof(*p_streamparm));
264 	p_streamparm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
265 	p_streamparm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
266 	p_streamparm->parm.capture.capturemode = V4L2_MODE_HIGHQUALITY;
267 	p_streamparm->parm.capture.timeperframe.numerator = 0xdeadbeef;
268 	p_streamparm->parm.capture.timeperframe.denominator = 0xbadc0ded;
269 	ioctl(-1, VIDIOC_S_PARM, p_streamparm);
270 	printf("ioctl(-1, VIDIOC_S_PARM, {type=V4L2_BUF_TYPE_VIDEO_CAPTURE"
271 	       ", parm.capture={capability=V4L2_CAP_TIMEPERFRAME"
272 	       ", capturemode=V4L2_MODE_HIGHQUALITY, timeperframe=%u/%u"
273 	       ", extendedmode=%u, readbuffers=%u}}) = -1 EBADF (%m)\n",
274 	       p_streamparm->parm.capture.timeperframe.numerator,
275 	       p_streamparm->parm.capture.timeperframe.denominator, -1U, -1U);
276 
277 	p_streamparm->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
278 	p_streamparm->parm.output.outputmode = 0;
279 	ioctl(-1, VIDIOC_S_PARM, p_streamparm);
280 	printf("ioctl(-1, VIDIOC_S_PARM, {type=V4L2_BUF_TYPE_VIDEO_OUTPUT"
281 	       ", parm.output={capability=V4L2_CAP_TIMEPERFRAME"
282 	       ", outputmode=0, timeperframe=%u/%u"
283 	       ", extendedmode=%u, writebuffers=%u}}) = -1 EBADF (%m)\n",
284 	       p_streamparm->parm.output.timeperframe.numerator,
285 	       p_streamparm->parm.output.timeperframe.denominator, -1U, -1U);
286 
287 	/* VIDIOC_G_STD */
288 	ioctl(-1, VIDIOC_G_STD, 0);
289 	printf("ioctl(-1, VIDIOC_G_STD, NULL) = -1 EBADF (%m)\n");
290 
291 	ioctl(-1, VIDIOC_G_STD, page);
292 	printf("ioctl(-1, VIDIOC_G_STD, %p) = -1 EBADF (%m)\n", page);
293 
294 	/* VIDIOC_S_STD */
295 	ioctl(-1, VIDIOC_S_STD, 0);
296 	printf("ioctl(-1, VIDIOC_S_STD, NULL) = -1 EBADF (%m)\n");
297 
298 	long long *const p_longlong = page + size - sizeof(*p_longlong);
299 	ioctl(-1, VIDIOC_S_STD, p_longlong);
300 	printf("ioctl(-1, VIDIOC_S_STD, [%#llx]) = -1 EBADF (%m)\n",
301 	       *p_longlong);
302 
303 	/* VIDIOC_ENUMSTD */
304 	ioctl(-1, VIDIOC_ENUMSTD, 0);
305 	printf("ioctl(-1, VIDIOC_ENUMSTD, NULL) = -1 EBADF (%m)\n");
306 
307 	struct v4l2_standard *const p_v4l2_standard =
308 		page + size - sizeof(*p_v4l2_standard);
309 	ioctl(-1, VIDIOC_ENUMSTD, p_v4l2_standard);
310 	printf("ioctl(-1, VIDIOC_ENUMSTD, {index=%u}) = -1 EBADF (%m)\n",
311 	       p_v4l2_standard->index);
312 
313 	/* VIDIOC_ENUMINPUT */
314 	ioctl(-1, VIDIOC_ENUMINPUT, 0);
315 	printf("ioctl(-1, VIDIOC_ENUMINPUT, NULL) = -1 EBADF (%m)\n");
316 
317 	struct v4l2_input *const p_v4l2_input =
318 		page + size - sizeof(*p_v4l2_input);
319 	ioctl(-1, VIDIOC_ENUMINPUT, p_v4l2_input);
320 	printf("ioctl(-1, VIDIOC_ENUMINPUT, {index=%u}) = -1 EBADF (%m)\n",
321 	       p_v4l2_input->index);
322 
323 	/* VIDIOC_G_CTRL */
324 	ioctl(-1, VIDIOC_G_CTRL, 0);
325 	printf("ioctl(-1, VIDIOC_G_CTRL, NULL) = -1 EBADF (%m)\n");
326 
327 	struct v4l2_control *const p_v4l2_control =
328 		page + size - sizeof(*p_v4l2_control);
329 	ioctl(-1, VIDIOC_G_CTRL, p_v4l2_control);
330 	printf("ioctl(-1, VIDIOC_G_CTRL, {id=%#x /* V4L2_CID_??? */})"
331 	       " = -1 EBADF (%m)\n", p_v4l2_control->id);
332 
333 	/* VIDIOC_S_CTRL */
334 	ioctl(-1, VIDIOC_S_CTRL, 0);
335 	printf("ioctl(-1, VIDIOC_S_CTRL, NULL) = -1 EBADF (%m)\n");
336 
337 	ioctl(-1, VIDIOC_S_CTRL, p_v4l2_control);
338 	printf("ioctl(-1, VIDIOC_S_CTRL, {id=%#x /* V4L2_CID_??? */"
339 	       ", value=%d}) = -1 EBADF (%m)\n",
340 	       p_v4l2_control->id, p_v4l2_control->value);
341 
342 	/* VIDIOC_QUERYCTRL */
343 	ioctl(-1, VIDIOC_QUERYCTRL, 0);
344 	printf("ioctl(-1, VIDIOC_QUERYCTRL, NULL) = -1 EBADF (%m)\n");
345 
346 	struct v4l2_queryctrl *const p_v4l2_queryctrl =
347 		page + size - sizeof(*p_v4l2_queryctrl);
348 	ioctl(-1, VIDIOC_QUERYCTRL, p_v4l2_queryctrl);
349 # ifdef V4L2_CTRL_FLAG_NEXT_CTRL
350 	printf("ioctl(-1, VIDIOC_QUERYCTRL, {id=V4L2_CTRL_FLAG_NEXT_CTRL"
351 	       "|%#x /* V4L2_CID_??? */}) = -1 EBADF (%m)\n",
352 	       p_v4l2_queryctrl->id & ~V4L2_CTRL_FLAG_NEXT_CTRL);
353 # else
354 	printf("ioctl(-1, VIDIOC_QUERYCTRL, {id=%#x /* V4L2_CID_??? */})"
355 	       " = -1 EBADF (%m)\n", p_v4l2_queryctrl->id);
356 # endif
357 
358 	struct v4l2_queryctrl *const p_queryctrl =
359 		tail_alloc(sizeof(*p_queryctrl));
360 	p_queryctrl->id = V4L2_CID_SATURATION;
361 	ioctl(-1, VIDIOC_QUERYCTRL, p_queryctrl);
362 	printf("ioctl(-1, VIDIOC_QUERYCTRL, {id=V4L2_CID_SATURATION})"
363 	       " = -1 EBADF (%m)\n");
364 
365 	/* VIDIOC_G_INPUT */
366 	ioctl(-1, VIDIOC_G_INPUT, 0);
367 	printf("ioctl(-1, VIDIOC_G_INPUT, NULL) = -1 EBADF (%m)\n");
368 
369 	ioctl(-1, VIDIOC_G_INPUT, page);
370 	printf("ioctl(-1, VIDIOC_G_INPUT, %p) = -1 EBADF (%m)\n", page);
371 
372 	/* VIDIOC_S_INPUT */
373 	ioctl(-1, VIDIOC_S_INPUT, 0);
374 	printf("ioctl(-1, VIDIOC_S_INPUT, NULL) = -1 EBADF (%m)\n");
375 
376 	ioctl(-1, VIDIOC_S_INPUT, p_int);
377 	printf("ioctl(-1, VIDIOC_S_INPUT, [%u]) = -1 EBADF (%m)\n", *p_int);
378 
379 	/* VIDIOC_CROPCAP */
380 	ioctl(-1, VIDIOC_CROPCAP, 0);
381 	printf("ioctl(-1, VIDIOC_CROPCAP, NULL) = -1 EBADF (%m)\n");
382 
383 	struct v4l2_cropcap *const p_v4l2_cropcap =
384 		page + size - sizeof(*p_v4l2_cropcap);
385 	ioctl(-1, VIDIOC_CROPCAP, p_v4l2_cropcap);
386 	printf("ioctl(-1, VIDIOC_CROPCAP, {type=%#x /* V4L2_BUF_TYPE_??? */})"
387 	       " = -1 EBADF (%m)\n", p_v4l2_cropcap->type);
388 
389 	/* VIDIOC_G_CROP */
390 	ioctl(-1, VIDIOC_G_CROP, 0);
391 	printf("ioctl(-1, VIDIOC_G_CROP, NULL) = -1 EBADF (%m)\n");
392 
393 	struct v4l2_crop *const p_v4l2_crop =
394 		page + size - sizeof(*p_v4l2_crop);
395 	ioctl(-1, VIDIOC_G_CROP, p_v4l2_crop);
396 	printf("ioctl(-1, VIDIOC_G_CROP, {type=%#x /* V4L2_BUF_TYPE_??? */})"
397 	       " = -1 EBADF (%m)\n", p_v4l2_crop->type);
398 
399 	/* VIDIOC_S_CROP */
400 	ioctl(-1, VIDIOC_S_CROP, 0);
401 	printf("ioctl(-1, VIDIOC_S_CROP, NULL) = -1 EBADF (%m)\n");
402 
403 	ioctl(-1, VIDIOC_S_CROP, p_v4l2_crop);
404 	printf("ioctl(-1, VIDIOC_S_CROP, {type=%#x /* V4L2_BUF_TYPE_??? */"
405 	       ", c={left=%d, top=%d, width=%u, height=%u}}) = -1 EBADF (%m)\n",
406 	       p_v4l2_crop->type,
407 	       p_v4l2_crop->c.left,
408 	       p_v4l2_crop->c.top,
409 	       p_v4l2_crop->c.width,
410 	       p_v4l2_crop->c.height);
411 
412 #ifdef VIDIOC_S_EXT_CTRLS
413 	/* VIDIOC_S_EXT_CTRLS */
414 	ioctl(-1, VIDIOC_S_EXT_CTRLS, 0);
415 	printf("ioctl(-1, VIDIOC_S_EXT_CTRLS, NULL) = -1 EBADF (%m)\n");
416 
417 	struct v4l2_ext_controls *const p_ext_controls =
418 		tail_alloc(sizeof(*p_ext_controls));
419 	p_ext_controls->ctrl_class = V4L2_CTRL_CLASS_USER;
420 	p_ext_controls->count = 0;
421 	p_ext_controls->controls = (void *) -2UL;
422 	ioctl(-1, VIDIOC_S_EXT_CTRLS, p_ext_controls);
423 	printf("ioctl(-1, VIDIOC_S_EXT_CTRLS, {ctrl_class=V4L2_CTRL_CLASS_USER"
424 	       ", count=%u}) = -1 EBADF (%m)\n", p_ext_controls->count);
425 
426 	p_ext_controls->ctrl_class = V4L2_CTRL_CLASS_MPEG;
427 	p_ext_controls->count = magic;
428 	ioctl(-1, VIDIOC_S_EXT_CTRLS, p_ext_controls);
429 	printf("ioctl(-1, VIDIOC_S_EXT_CTRLS, {ctrl_class=V4L2_CTRL_CLASS_MPEG"
430 	       ", count=%u, controls=%p}) = -1 EBADF (%m)\n",
431 	       p_ext_controls->count, p_ext_controls->controls);
432 
433 # if HAVE_DECL_V4L2_CTRL_TYPE_STRING
434 	p_ext_controls->count = 2;
435 	p_ext_controls->controls =
436 		tail_alloc(sizeof(*p_ext_controls->controls) * p_ext_controls->count);
437 	p_ext_controls->controls[0].id = V4L2_CID_BRIGHTNESS;
438 	p_ext_controls->controls[0].size = 0;
439 	p_ext_controls->controls[0].value64 = 0xfacefeeddeadbeefULL;
440 	p_ext_controls->controls[1].id = V4L2_CID_CONTRAST;
441 	p_ext_controls->controls[1].size = 2;
442 	p_ext_controls->controls[1].string =
443 		tail_alloc(p_ext_controls->controls[1].size);
444 
445 	ioctl(-1, VIDIOC_S_EXT_CTRLS, p_ext_controls);
446 	printf("ioctl(-1, VIDIOC_S_EXT_CTRLS"
447 	       ", {ctrl_class=V4L2_CTRL_CLASS_MPEG, count=%u, controls="
448 	       "[{id=V4L2_CID_BRIGHTNESS, size=0, value=%d, value64=%lld}"
449 	       ", {id=V4L2_CID_CONTRAST, size=2, string=\"\\377\\377\"}"
450 	       "] => controls="
451 	       "[{id=V4L2_CID_BRIGHTNESS, size=0, value=%d, value64=%lld}"
452 	       ", {id=V4L2_CID_CONTRAST, size=2, string=\"\\377\\377\"}"
453 	       "], error_idx=%u}) = -1 EBADF (%m)\n",
454 	       p_ext_controls->count,
455 	       p_ext_controls->controls[0].value,
456 	       (long long) p_ext_controls->controls[0].value64,
457 	       p_ext_controls->controls[0].value,
458 	       (long long) p_ext_controls->controls[0].value64,
459 	       p_ext_controls->error_idx);
460 
461 	++p_ext_controls->count;
462 	ioctl(-1, VIDIOC_S_EXT_CTRLS, p_ext_controls);
463 	printf("ioctl(-1, VIDIOC_S_EXT_CTRLS"
464 	       ", {ctrl_class=V4L2_CTRL_CLASS_MPEG, count=%u, controls="
465 	       "[{id=V4L2_CID_BRIGHTNESS, size=0, value=%d, value64=%lld}"
466 	       ", {id=V4L2_CID_CONTRAST, size=2, string=\"\\377\\377\"}"
467 	       ", %p]}) = -1 EBADF (%m)\n",
468 	       p_ext_controls->count,
469 	       p_ext_controls->controls[0].value,
470 	       (long long) p_ext_controls->controls[0].value64,
471 	       p_ext_controls->controls + 2);
472 # endif /* HAVE_DECL_V4L2_CTRL_TYPE_STRING */
473 
474 	/* VIDIOC_TRY_EXT_CTRLS */
475 	ioctl(-1, VIDIOC_TRY_EXT_CTRLS, 0);
476 	printf("ioctl(-1, VIDIOC_TRY_EXT_CTRLS, NULL) = -1 EBADF (%m)\n");
477 
478 	p_ext_controls->ctrl_class = V4L2_CTRL_CLASS_USER;
479 	p_ext_controls->count = magic;
480 	p_ext_controls->controls = (void *) -2UL;
481 	ioctl(-1, VIDIOC_TRY_EXT_CTRLS, p_ext_controls);
482 	printf("ioctl(-1, VIDIOC_TRY_EXT_CTRLS"
483 	       ", {ctrl_class=V4L2_CTRL_CLASS_USER, count=%u, controls=%p})"
484 	       " = -1 EBADF (%m)\n",
485 	       p_ext_controls->count, p_ext_controls->controls);
486 
487 	/* VIDIOC_G_EXT_CTRLS */
488 	ioctl(-1, VIDIOC_G_EXT_CTRLS, 0);
489 	printf("ioctl(-1, VIDIOC_G_EXT_CTRLS, NULL) = -1 EBADF (%m)\n");
490 
491 	ioctl(-1, VIDIOC_G_EXT_CTRLS, p_ext_controls);
492 	printf("ioctl(-1, VIDIOC_G_EXT_CTRLS"
493 	       ", {ctrl_class=V4L2_CTRL_CLASS_USER, count=%u, controls=%p"
494 	       ", error_idx=%u}) = -1 EBADF (%m)\n",
495 	       p_ext_controls->count, p_ext_controls->controls,
496 	       p_ext_controls->error_idx);
497 #endif /* VIDIOC_S_EXT_CTRLS */
498 
499 #ifdef VIDIOC_ENUM_FRAMESIZES
500 	ioctl(-1, VIDIOC_ENUM_FRAMESIZES, 0);
501 	printf("ioctl(-1, VIDIOC_ENUM_FRAMESIZES, NULL) = -1 EBADF (%m)\n");
502 
503 	struct v4l2_frmsizeenum *const p_frmsizeenum =
504 		tail_alloc(sizeof(*p_frmsizeenum));
505 	p_frmsizeenum->index = magic;
506 	p_frmsizeenum->pixel_format = fourcc(cc[0], cc[1], cc[2], cc[3]);
507 
508 	ioctl(-1, VIDIOC_ENUM_FRAMESIZES, p_frmsizeenum);
509 	printf("ioctl(-1, VIDIOC_ENUM_FRAMESIZES, {index=%u"
510 	       ", pixel_format=v4l2_fourcc('%c', '\\%c', '\\%c', '\\x%x')})"
511 	       " = -1 EBADF (%m)\n", p_frmsizeenum->index,
512 	       cc[0], cc[1], cc[2], cc[3]);
513 #endif /* VIDIOC_ENUM_FRAMESIZES */
514 
515 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
516 	ioctl(-1, VIDIOC_ENUM_FRAMEINTERVALS, 0);
517 	printf("ioctl(-1, VIDIOC_ENUM_FRAMEINTERVALS, NULL) = -1 EBADF (%m)\n");
518 
519 	struct v4l2_frmivalenum *const p_v4l2_frmivalenum =
520 		page + size - sizeof(*p_v4l2_frmivalenum);
521 	ioctl(-1, VIDIOC_ENUM_FRAMEINTERVALS, p_v4l2_frmivalenum);
522 	printf("ioctl(-1, VIDIOC_ENUM_FRAMEINTERVALS, {index=%u"
523 	       ", pixel_format=v4l2_fourcc('\\x%x', '\\x%x', '\\x%x', '\\x%x')"
524 	       ", width=%u, height=%u}) = -1 EBADF (%m)\n",
525 	       p_v4l2_frmivalenum->index,
526 	       cc0(p_v4l2_frmivalenum->pixel_format),
527 	       cc1(p_v4l2_frmivalenum->pixel_format),
528 	       cc2(p_v4l2_frmivalenum->pixel_format),
529 	       cc3(p_v4l2_frmivalenum->pixel_format),
530 	       p_v4l2_frmivalenum->width,
531 	       p_v4l2_frmivalenum->height);
532 #endif /* VIDIOC_ENUM_FRAMEINTERVALS */
533 
534 #ifdef VIDIOC_CREATE_BUFS
535 	ioctl(-1, VIDIOC_CREATE_BUFS, 0);
536 	printf("ioctl(-1, VIDIOC_CREATE_BUFS, NULL) = -1 EBADF (%m)\n");
537 
538 	struct v4l2_create_buffers *const p_v4l2_create_buffers =
539 		page + size - sizeof(*p_v4l2_create_buffers);
540 	ioctl(-1, VIDIOC_CREATE_BUFS, p_v4l2_create_buffers);
541 	printf("ioctl(-1, VIDIOC_CREATE_BUFS, {count=%u, memory=%#x"
542 	       " /* V4L2_MEMORY_??? */, format={type=%#x"
543 	       " /* V4L2_BUF_TYPE_??? */}}) = -1 EBADF (%m)\n",
544 	       p_v4l2_create_buffers->count,
545 	       p_v4l2_create_buffers->memory,
546 	       p_v4l2_create_buffers->format.type);
547 #endif /* VIDIOC_CREATE_BUFS */
548 
549 	puts("+++ exited with 0 +++");
550 	return 0;
551 }
552