Debian Codegraph @

lib/libv4l1/libv4l1.c

raw
1 /*
2 # libv4l1 userspace v4l1 api emulation for v4l2 devices
3
4 # (C) 2008-2010 Hans de Goede <[email protected]>
5
6 # Based in part on the kernels v4l1 ioctl compatibility code which is:
7
8 # (C) 2003-2009 Bill Dirks <[email protected]>, et al.
9
10 # The code taken from the kernel has been relicensed from GPLv2+ to LGPLv2+
11 # with permission from the authors see libv4l1-kernelcode-license.txt
12
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU Lesser General Public License as published by
15 # the Free Software Foundation; either version 2.1 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU Lesser General Public License for more details.
22 #
23 # You should have received a copy of the GNU Lesser General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
26 */
27
28 /* MAKING CHANGES TO THIS FILE?? READ THIS FIRST!!!
29
30 Important note to people making changes to this file: All functions
31 (v4l1_close, v4l1_ioctl, etc.) are designed to function as their regular
32 counterpart when they get passed a fd that is not "registered" by libv4l1,
33 there are 2 reasons for this:
34 1) This allows us to get completely out of the way when dealing with non
35 capture only devices, or non v4l2 devices.
36 2) libv4l1 is the base of the v4l1compat.so wrapper lib, which is a .so
37 which can be LD_PRELOAD-ed and the overrules the libc's open/close/etc,
38 and when opening /dev/videoX or /dev/v4l/ calls v4l1_open. Because we
39 behave as the regular counterpart when the fd is not known (instead of
40 say throwing an error), v4l1compat.so can simply call the v4l1_ prefixed
41 function for all wrapped functions. This way the wrapper does not have
42 to keep track of which fd's are being handled by libv4l1, as libv4l1
43 already keeps track of this itself.
44
45 This also means that libv4l1 may not use any of the regular functions
46 it mimics, as for example open could be a symbol in v4l1compat.so, which
47 in turn will call v4l1_open, so therefor v4l1_open (for example) may not
48 use the regular open()!
49 */
50 #include <errno.h>
51 #include <stdarg.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <fcntl.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <sys/types.h>
58 #include <sys/mman.h>
59 #include "../libv4lconvert/libv4lsyscall-priv.h"
60 #if defined(__OpenBSD__)
61 #include <sys/videoio.h>
62 #else
63 #include <linux/videodev2.h>
64 #endif
65 #include <libv4l2.h>
66 #include "libv4l1.h"
67 #include "libv4l1-priv.h"
68
69 #define V4L1_SUPPORTS_ENUMINPUT 0x01
70 #define V4L1_SUPPORTS_ENUMSTD 0x02
71 #define V4L1_PIX_FMT_TOUCHED 0x04
72 #define V4L1_PIX_SIZE_TOUCHED 0x08
73
74 static pthread_mutex_t v4l1_open_mutex = PTHREAD_MUTEX_INITIALIZER;
75 static struct v4l1_dev_info devices[V4L1_MAX_DEVICES] = {
76 { .fd = -1 },
77 { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 },
78 { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 },
79 { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }
80 };
81 static int devices_used;
82
83 static unsigned int palette_to_pixelformat(unsigned int palette)
84 {
85 switch (palette) {
86 case VIDEO_PALETTE_GREY:
87 return V4L2_PIX_FMT_GREY;
88 case VIDEO_PALETTE_RGB555:
89 return V4L2_PIX_FMT_RGB555;
90 case VIDEO_PALETTE_RGB565:
91 return V4L2_PIX_FMT_RGB565;
92 case VIDEO_PALETTE_RGB24:
93 /* Yes v4l1 RGB24 really is BGR24, this is a weirdness in the
94 V4L1 API. Which unfortunately some V4L1 apps get wrong.
95 If you have an app which has r and b swapped and end up here
96 fix the app, this is not a libv4l1 bug */
97 return V4L2_PIX_FMT_BGR24;
98 case VIDEO_PALETTE_RGB32:
99 return V4L2_PIX_FMT_BGR32;
100 case VIDEO_PALETTE_YUYV:
101 return V4L2_PIX_FMT_YUYV;
102 case VIDEO_PALETTE_YUV422:
103 return V4L2_PIX_FMT_YUYV;
104 case VIDEO_PALETTE_UYVY:
105 return V4L2_PIX_FMT_UYVY;
106 case VIDEO_PALETTE_YUV410P:
107 return V4L2_PIX_FMT_YUV410;
108 case VIDEO_PALETTE_YUV420:
109 case VIDEO_PALETTE_YUV420P:
110 return V4L2_PIX_FMT_YUV420;
111 case VIDEO_PALETTE_YUV411P:
112 return V4L2_PIX_FMT_YUV411P;
113 case VIDEO_PALETTE_YUV422P:
114 return V4L2_PIX_FMT_YUV422P;
115 }
116 return 0;
117 }
118
119 static unsigned int pixelformat_to_palette(unsigned int pixelformat)
120 {
121 switch (pixelformat) {
122 case V4L2_PIX_FMT_GREY:
123 return VIDEO_PALETTE_GREY;
124 case V4L2_PIX_FMT_RGB555:
125 return VIDEO_PALETTE_RGB555;
126 case V4L2_PIX_FMT_RGB565:
127 return VIDEO_PALETTE_RGB565;
128 case V4L2_PIX_FMT_BGR24:
129 /* See the comment in palette_to_pixelformat above */
130 return VIDEO_PALETTE_RGB24;
131 case V4L2_PIX_FMT_BGR32:
132 return VIDEO_PALETTE_RGB32;
133 case V4L2_PIX_FMT_YUYV:
134 return VIDEO_PALETTE_YUYV;
135 case V4L2_PIX_FMT_UYVY:
136 return VIDEO_PALETTE_UYVY;
137 case V4L2_PIX_FMT_YUV410:
138 case V4L2_PIX_FMT_YUV420:
139 return VIDEO_PALETTE_YUV420P;
140 case V4L2_PIX_FMT_YUV411P:
141 return VIDEO_PALETTE_YUV411P;
142 case V4L2_PIX_FMT_YUV422P:
143 return VIDEO_PALETTE_YUV422P;
144 }
145 return 0;
146 }
147
148 static int count_inputs(int fd)
149 {
150 struct v4l2_input input2;
151 int i;
152 for (i = 0;; i++) {
153 memset(&input2, 0, sizeof(input2));
154 input2.index = i;
155 if (0 != v4l2_ioctl(fd, VIDIOC_ENUMINPUT, &input2))
156 break;
157 }
158 return i;
159 }
160
161 static int v4l1_set_format(int index, unsigned int width,
162 unsigned int height, int v4l1_pal, int width_height_may_differ)
163 {
164 int result;
165 unsigned int v4l2_pixfmt;
166 struct v4l2_format fmt2 = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
167
168 if (v4l1_pal != -1) {
169 v4l2_pixfmt = palette_to_pixelformat(v4l1_pal);
170 if (!v4l2_pixfmt) {
171 V4L1_LOG("Unknown v4l1 palette number: %d\n", v4l1_pal);
172 errno = EINVAL;
173 return -1;
174 }
175 } else {
176 v4l2_pixfmt = devices[index].v4l2_pixfmt;
177 v4l1_pal = devices[index].v4l1_pal;
178 }
179
180 /* Do we need to change the resolution / format ? */
181 if (width == devices[index].width && height == devices[index].height &&
182 v4l2_pixfmt == devices[index].v4l2_pixfmt) {
183 devices[index].v4l1_pal = v4l1_pal;
184 return 0;
185 }
186
187 /* Get current settings, apply our changes and try the new setting */
188 result = v4l2_ioctl(devices[index].fd, VIDIOC_G_FMT, &fmt2);
189 if (result) {
190 int saved_err = errno;
191
192 V4L1_LOG_ERR("getting pixformat: %s\n", strerror(errno));
193 errno = saved_err;
194 return result;
195 }
196
197 fmt2.fmt.pix.pixelformat = v4l2_pixfmt;
198 fmt2.fmt.pix.width = width;
199 fmt2.fmt.pix.height = height;
200 result = v4l2_ioctl(devices[index].fd, VIDIOC_TRY_FMT, &fmt2);
201 if (result) {
202 int saved_err = errno;
203
204 V4L1_LOG("error trying pixformat: %s\n", strerror(errno));
205 errno = saved_err;
206 return result;
207 }
208
209 /* Check if we get what we asked for */
210 if (fmt2.fmt.pix.pixelformat != v4l2_pixfmt || (!width_height_may_differ &&
211 (fmt2.fmt.pix.width != width || fmt2.fmt.pix.height != height))) {
212 V4L1_LOG("requested fmt, width, height combo not available\n");
213 errno = EINVAL;
214 return -1;
215 }
216
217 /* Maybe after the TRY_FMT things haven't changed after all ? */
218 if (fmt2.fmt.pix.width == devices[index].width &&
219 fmt2.fmt.pix.height == devices[index].height &&
220 fmt2.fmt.pix.pixelformat == devices[index].v4l2_pixfmt) {
221 devices[index].v4l1_pal = v4l1_pal;
222 return 0;
223 }
224
225 result = v4l2_ioctl(devices[index].fd, VIDIOC_S_FMT, &fmt2);
226 if (result) {
227 int saved_err = errno;
228
229 V4L1_LOG_ERR("setting pixformat: %s\n", strerror(errno));
230 errno = saved_err;
231 return result;
232 }
233
234 devices[index].width = fmt2.fmt.pix.width;
235 devices[index].height = fmt2.fmt.pix.height;
236 devices[index].v4l2_pixfmt = v4l2_pixfmt;
237 devices[index].v4l1_pal = v4l1_pal;
238 devices[index].depth = ((fmt2.fmt.pix.bytesperline << 3) +
239 (fmt2.fmt.pix.width - 1)) / fmt2.fmt.pix.width;
240
241 return result;
242 }
243
244 static void v4l1_find_min_and_max_size(int index, struct v4l2_format *fmt2)
245 {
246 int i;
247 struct v4l2_fmtdesc fmtdesc2 = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
248
249 devices[index].min_width = -1;
250 devices[index].min_height = -1;
251 devices[index].max_width = 0;
252 devices[index].max_height = 0;
253
254 for (i = 0; ; i++) {
255 fmtdesc2.index = i;
256
257 if (v4l2_ioctl(devices[index].fd, VIDIOC_ENUM_FMT, &fmtdesc2))
258 break;
259
260 fmt2->fmt.pix.pixelformat = fmtdesc2.pixelformat;
261 fmt2->fmt.pix.width = 48;
262 fmt2->fmt.pix.height = 32;
263
264 if (v4l2_ioctl(devices[index].fd, VIDIOC_TRY_FMT, fmt2) == 0) {
265 if (fmt2->fmt.pix.width < devices[index].min_width)
266 devices[index].min_width = fmt2->fmt.pix.width;
267 if (fmt2->fmt.pix.height < devices[index].min_height)
268 devices[index].min_height = fmt2->fmt.pix.height;
269 }
270
271 fmt2->fmt.pix.pixelformat = fmtdesc2.pixelformat;
272 fmt2->fmt.pix.width = 100000;
273 fmt2->fmt.pix.height = 100000;
274
275 if (v4l2_ioctl(devices[index].fd, VIDIOC_TRY_FMT, fmt2) == 0) {
276 if (fmt2->fmt.pix.width > devices[index].max_width)
277 devices[index].max_width = fmt2->fmt.pix.width;
278 if (fmt2->fmt.pix.height > devices[index].max_height)
279 devices[index].max_height = fmt2->fmt.pix.height;
280 }
281 }
282 }
283
284
285 int v4l1_open(const char *file, int oflag, ...)
286 {
287 int index, fd;
288 char *lfname;
289 struct v4l2_capability cap2;
290 struct v4l2_format fmt2;
291 struct v4l2_input input2;
292 struct v4l2_standard standard2;
293 int v4l_device = 0;
294
295 /* check if we're opening a video4linux2 device */
296 if (!strncmp(file, "/dev/video", 10) || !strncmp(file, "/dev/v4l/", 9)) {
297 /* Some apps open the device read only, but we need rw rights as the
298 buffers *MUST* be mapped rw */
299 oflag = (oflag & ~O_ACCMODE) | O_RDWR;
300 v4l_device = 1;
301 }
302
303 /* original open code */
304 if (oflag & O_CREAT) {
305 va_list ap;
306 mode_t mode;
307
308 va_start(ap, oflag);
309 mode = va_arg(ap, PROMOTED_MODE_T);
310
311 fd = SYS_OPEN(file, oflag, mode);
312
313 va_end(ap);
314 } else {
315 fd = SYS_OPEN(file, oflag, 0);
316 }
317
318 /* end of original open code */
319
320 if (fd == -1 || !v4l_device)
321 return fd;
322
323 /* check that this is an v4l2 device, no need to emulate v4l1 on
324 a v4l1 device */
325 if (SYS_IOCTL(fd, VIDIOC_QUERYCAP, &cap2))
326 return fd;
327
328 /* If no log file was set by the app, see if one was specified through the
329 environment */
330 if (!v4l1_log_file) {
331 lfname = getenv("LIBV4L1_LOG_FILENAME");
332 if (lfname)
333 v4l1_log_file = fopen(lfname, "w");
334 }
335
336 /* redirect libv4l2 log messages to our logfile if no libv4l2 logfile is
337 specified */
338 if (!v4l2_log_file)
339 v4l2_log_file = v4l1_log_file;
340
341 /* Register with libv4l2, as we use that todo format conversion and read()
342 emulation for us */
343 if (v4l2_fd_open(fd, 0) == -1) {
344 int saved_err = errno;
345
346 SYS_CLOSE(fd);
347 errno = saved_err;
348 return -1;
349 }
350
351 /* Get initial width, height and pixelformat */
352 fmt2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
353 if (v4l2_ioctl(fd, VIDIOC_G_FMT, &fmt2)) {
354 int saved_err = errno;
355
356 SYS_CLOSE(fd);
357 errno = saved_err;
358 return -1;
359 }
360
361 /* So we have a device on which we can (and want to) emulate v4l1, register
362 it in our devices array */
363 pthread_mutex_lock(&v4l1_open_mutex);
364 for (index = 0; index < V4L1_MAX_DEVICES; index++)
365 if (devices[index].fd == -1) {
366 devices[index].fd = fd;
367 break;
368 }
369 pthread_mutex_unlock(&v4l1_open_mutex);
370
371 if (index == V4L1_MAX_DEVICES) {
372 V4L1_LOG_ERR("attempting to open more than %d video devices\n",
373 V4L1_MAX_DEVICES);
374 v4l2_close(fd);
375 errno = EBUSY;
376 return -1;
377 }
378
379 if (index >= devices_used)
380 devices_used = index + 1;
381
382 devices[index].flags = 0;
383 devices[index].open_count = 1;
384 devices[index].v4l1_frame_buf_map_count = 0;
385 devices[index].v4l1_frame_pointer = MAP_FAILED;
386 devices[index].width = fmt2.fmt.pix.width;
387 devices[index].height = fmt2.fmt.pix.height;
388 devices[index].v4l2_pixfmt = fmt2.fmt.pix.pixelformat;
389 devices[index].v4l1_pal = pixelformat_to_palette(fmt2.fmt.pix.pixelformat);
390 devices[index].depth = ((fmt2.fmt.pix.bytesperline << 3) +
391 (fmt2.fmt.pix.width - 1)) / fmt2.fmt.pix.width;
392
393 v4l1_find_min_and_max_size(index, &fmt2);
394
395 /* Check ENUM_INPUT and ENUM_STD support */
396 input2.index = 0;
397 if (v4l2_ioctl(fd, VIDIOC_ENUMINPUT, &input2) == 0)
398 devices[index].flags |= V4L1_SUPPORTS_ENUMINPUT;
399
400 standard2.index = 0;
401 if (v4l2_ioctl(fd, VIDIOC_ENUMSTD, &standard2) == 0)
402 devices[index].flags |= V4L1_SUPPORTS_ENUMSTD;
403
404 V4L1_LOG("open: %d\n", fd);
405
406 return fd;
407 }
408
409 /* Is this an fd for which we are emulating v4l1 ? */
410 static int v4l1_get_index(int fd)
411 {
412 int index;
413
414 /* We never handle fd -1 */
415 if (fd == -1)
416 return -1;
417
418 for (index = 0; index < devices_used; index++)
419 if (devices[index].fd == fd)
420 break;
421
422 if (index == devices_used)
423 return -1;
424
425 return index;
426 }
427
428 int v4l1_close(int fd)
429 {
430 int index, result;
431
432 index = v4l1_get_index(fd);
433 if (index == -1)
434 return SYS_CLOSE(fd);
435
436 /* Abuse stream_lock to stop 2 closes from racing and trying to free the
437 resources twice */
438 pthread_mutex_lock(&devices[index].stream_lock);
439 devices[index].open_count--;
440 result = devices[index].open_count != 0;
441 pthread_mutex_unlock(&devices[index].stream_lock);
442
443 if (result)
444 return v4l2_close(fd);
445
446 /* Free resources */
447 if (devices[index].v4l1_frame_pointer != MAP_FAILED) {
448 if (devices[index].v4l1_frame_buf_map_count)
449 V4L1_LOG("v4l1 capture buffer still mapped: %d times on close()\n",
450 devices[index].v4l1_frame_buf_map_count);
451 else
452 SYS_MUNMAP(devices[index].v4l1_frame_pointer,
453 V4L1_NO_FRAMES * V4L1_FRAME_BUF_SIZE);
454 devices[index].v4l1_frame_pointer = MAP_FAILED;
455 }
456
457 /* Remove the fd from our list of managed fds before closing it, because as
458 soon as we've done the actual close the fd maybe returned by an open in
459 another thread and we don't want to intercept calls to this new fd. */
460 devices[index].fd = -1;
461
462 result = v4l2_close(fd);
463
464 V4L1_LOG("close: %d\n", fd);
465
466 return result;
467 }
468
469 int v4l1_dup(int fd)
470 {
471 int index = v4l1_get_index(fd);
472
473 if (index == -1)
474 return syscall(SYS_dup, fd);
475
476 devices[index].open_count++;
477
478 return v4l2_dup(fd);
479 }
480
481 int v4l1_ioctl(int fd, unsigned long int request, ...)
482 {
483 void *arg;
484 va_list ap;
485 int result, index, saved_err, stream_locked = 0;
486
487 va_start(ap, request);
488 arg = va_arg(ap, void *);
489 va_end(ap);
490
491 index = v4l1_get_index(fd);
492 if (index == -1)
493 return SYS_IOCTL(fd, request, arg);
494
495 /* Appearantly the kernel and / or glibc ignore the 32 most significant bits
496 when long = 64 bits, and some applications pass an int holding the req to
497 ioctl, causing it to get sign extended, depending upon this behavior */
498 request = (unsigned int)request;
499
500 /* do we need to take the stream lock for this ioctl? */
501 switch (request) {
502 case VIDIOCSPICT:
503 case VIDIOCGPICT:
504 case VIDIOCSWIN:
505 case VIDIOCGWIN:
506 case VIDIOCGMBUF:
507 case VIDIOCMCAPTURE:
508 case VIDIOCSYNC:
509 case VIDIOC_S_FMT:
510 pthread_mutex_lock(&devices[index].stream_lock);
511 stream_locked = 1;
512 }
513
514 switch (request) {
515 case VIDIOCGCAP: {
516 struct video_capability *cap = arg;
517 struct v4l2_framebuffer fbuf = { 0, };
518 struct v4l2_capability cap2 = { { 0 }, };
519
520 result = v4l2_ioctl(fd, VIDIOC_QUERYCAP, &cap2);
521 if (result < 0)
522 break;
523
524 if (cap2.capabilities & V4L2_CAP_DEVICE_CAPS)
525 cap2.capabilities = cap2.device_caps;
526 if (cap2.capabilities & V4L2_CAP_VIDEO_OVERLAY) {
527 result = v4l2_ioctl(fd, VIDIOC_G_FBUF, &fbuf);
528 if (result < 0)
529 memset(&fbuf, 0, sizeof(fbuf));
530 result = 0;
531 }
532
533 memcpy(cap->name, cap2.card,
534 min(sizeof(cap->name), sizeof(cap2.card)));
535
536 cap->name[sizeof(cap->name) - 1] = 0;
537
538 if (cap2.capabilities & V4L2_CAP_VIDEO_CAPTURE)
539 cap->type |= VID_TYPE_CAPTURE;
540 if (cap2.capabilities & V4L2_CAP_TUNER)
541 cap->type |= VID_TYPE_TUNER;
542 if (cap2.capabilities & V4L2_CAP_VBI_CAPTURE)
543 cap->type |= VID_TYPE_TELETEXT;
544 if (cap2.capabilities & V4L2_CAP_VIDEO_OVERLAY)
545 cap->type |= VID_TYPE_OVERLAY;
546 if (fbuf.capability & V4L2_FBUF_CAP_LIST_CLIPPING)
547 cap->type |= VID_TYPE_CLIPPING;
548
549 cap->channels = count_inputs(fd);
550 cap->minwidth = devices[index].min_width;
551 cap->minheight = devices[index].min_height;
552 cap->maxwidth = devices[index].max_width;
553 cap->maxheight = devices[index].max_height;
554 break;
555 }
556
557 case VIDIOCSPICT: {
558 struct video_picture *pic = arg;
559
560 devices[index].flags |= V4L1_PIX_FMT_TOUCHED;
561
562 v4l2_set_control(fd, V4L2_CID_BRIGHTNESS, pic->brightness);
563 v4l2_set_control(fd, V4L2_CID_HUE, pic->hue);
564 v4l2_set_control(fd, V4L2_CID_CONTRAST, pic->contrast);
565 v4l2_set_control(fd, V4L2_CID_SATURATION, pic->colour);
566 v4l2_set_control(fd, V4L2_CID_WHITENESS, pic->whiteness);
567
568 result = v4l1_set_format(index, devices[index].width,
569 devices[index].height, pic->palette, 0);
570 break;
571 }
572
573 case VIDIOCGPICT: {
574 struct video_picture *pic = arg;
575 int i;
576
577 /* If our v4l2 pixformat has no corresponding v4l1 palette, and
578 the app has not touched the pixformat sofar, try setting a
579 palette which does (and which we emulate when necessary) so
580 that applications which just query the current format and
581 then take whatever they get will work */
582 if (!(devices[index].flags & V4L1_PIX_FMT_TOUCHED) &&
583 !pixelformat_to_palette(devices[index].v4l2_pixfmt))
584 v4l1_set_format(index, devices[index].width,
585 devices[index].height,
586 VIDEO_PALETTE_RGB24,
587 (devices[index].flags &
588 V4L1_PIX_SIZE_TOUCHED) ? 0 : 1);
589
590 devices[index].flags |= V4L1_PIX_FMT_TOUCHED;
591
592 memset(pic, 0, sizeof(*pic));
593 pic->depth = devices[index].depth;
594 pic->palette = devices[index].v4l1_pal;
595 i = v4l2_get_control(devices[index].fd, V4L2_CID_HUE);
596 if (i >= 0)
597 pic->hue = i;
598 i = v4l2_get_control(devices[index].fd, V4L2_CID_SATURATION);
599 if (i >= 0)
600 pic->colour = i;
601 i = v4l2_get_control(devices[index].fd, V4L2_CID_CONTRAST);
602 if (i >= 0)
603 pic->contrast = i;
604 i = v4l2_get_control(devices[index].fd, V4L2_CID_WHITENESS);
605 if (i >= 0)
606 pic->whiteness = i;
607 i = v4l2_get_control(devices[index].fd, V4L2_CID_BRIGHTNESS);
608 if (i >= 0)
609 pic->brightness = i;
610
611 result = 0;
612 break;
613 }
614
615 case VIDIOCSWIN:
616 case VIDIOCGWIN: {
617 struct video_window *win = arg;
618
619 devices[index].flags |= V4L1_PIX_SIZE_TOUCHED;
620
621 if (request == VIDIOCSWIN)
622 result = v4l1_set_format(index, win->width, win->height, -1, 1);
623 else
624 result = 0;
625
626 if (result == 0) {
627 win->x = 0;
628 win->y = 0;
629 win->width = devices[index].width;
630 win->height = devices[index].height;
631 win->flags = 0;
632 }
633 break;
634 }
635
636 case VIDIOCGCHAN: {
637 struct video_channel *chan = arg;
638
639 /* Set some defaults */
640 chan->tuners = 0;
641 chan->flags = 0;
642 chan->type = VIDEO_TYPE_CAMERA;
643 chan->norm = 0;
644
645 if (devices[index].flags & V4L1_SUPPORTS_ENUMINPUT) {
646 struct v4l2_input input2 = { .index = chan->channel };
647
648 result = v4l2_ioctl(fd, VIDIOC_ENUMINPUT, &input2);
649 if (result < 0)
650 break;
651
652 snprintf(chan->name, sizeof(chan->name), "%s",
653 (char *)input2.name);
654 if (input2.type == V4L2_INPUT_TYPE_TUNER) {
655 chan->tuners = 1;
656 chan->type = VIDEO_TYPE_TV;
657 chan->flags = VIDEO_VC_TUNER;
658 }
659 } else {
660 /* No ENUMINPUT support, fake it. */
661 if (chan->channel == 0) {
662 snprintf(chan->name, sizeof(chan->name),
663 "Camera");
664 result = 0;
665 } else {
666 errno = EINVAL;
667 result = -1;
668 break;
669 }
670 }
671
672 /* In case of no ENUMSTD support, ignore the norm member of the
673 channel struct */
674 if (devices[index].flags & V4L1_SUPPORTS_ENUMSTD) {
675 v4l2_std_id sid;
676
677 result = v4l2_ioctl(fd, VIDIOC_G_STD, &sid);
678 if (result < 0)
679 break;
680
681 if (sid & V4L2_STD_PAL)
682 chan->norm = VIDEO_MODE_PAL;
683 if (sid & V4L2_STD_NTSC)
684 chan->norm = VIDEO_MODE_NTSC;
685 if (sid & V4L2_STD_SECAM)
686 chan->norm = VIDEO_MODE_SECAM;
687 if (sid == V4L2_STD_ALL)
688 chan->norm = VIDEO_MODE_AUTO;
689 }
690 break;
691 }
692
693 case VIDIOCSCHAN: {
694 struct video_channel *chan = arg;
695
696 if (devices[index].flags & V4L1_SUPPORTS_ENUMINPUT) {
697 result = v4l2_ioctl(fd, VIDIOC_S_INPUT, &chan->channel);
698 if (result < 0)
699 break;
700 } else {
701 /* No ENUMINPUT support, assume a single input */
702 if (chan->channel != 0) {
703 errno = EINVAL;
704 result = -1;
705 break;
706 }
707 result = 0;
708 }
709
710 /* In case of no ENUMSTD support, ignore the norm member of the
711 channel struct */
712 if (devices[index].flags & V4L1_SUPPORTS_ENUMSTD) {
713 v4l2_std_id sid = 0;
714
715 switch (chan->norm) {
716 case VIDEO_MODE_PAL:
717 sid = V4L2_STD_PAL;
718 break;
719 case VIDEO_MODE_NTSC:
720 sid = V4L2_STD_NTSC;
721 break;
722 case VIDEO_MODE_SECAM:
723 sid = V4L2_STD_SECAM;
724 break;
725 case VIDEO_MODE_AUTO:
726 sid = V4L2_STD_ALL;
727 break;
728 }
729
730 if (sid)
731 result = v4l2_ioctl(fd, VIDIOC_S_STD, &sid);
732 }
733 break;
734 }
735
736 case VIDIOCGMBUF: {
737 /* When VIDIOCGMBUF is done, we don't necessarrily know the format the
738 application wants yet (with some apps this is passed for the first
739 time through VIDIOCMCAPTURE), so we just create an anonymous mapping
740 that should be large enough to hold any sort of frame. Note this only
741 takes virtual memory, and does not use memory until actually used. */
742 int i;
743 struct video_mbuf *mbuf = arg;
744
745 mbuf->size = V4L1_NO_FRAMES * V4L1_FRAME_BUF_SIZE;
746 mbuf->frames = V4L1_NO_FRAMES;
747 for (i = 0; i < mbuf->frames; i++)
748 mbuf->offsets[i] = i * V4L1_FRAME_BUF_SIZE;
749
750 if (devices[index].v4l1_frame_pointer == MAP_FAILED) {
751 devices[index].v4l1_frame_pointer = (void *)SYS_MMAP(NULL,
752 (size_t)mbuf->size,
753 PROT_READ | PROT_WRITE,
754 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
755 if (devices[index].v4l1_frame_pointer == MAP_FAILED) {
756 saved_err = errno;
757 V4L1_LOG_ERR("allocating v4l1 buffer: %s\n", strerror(errno));
758 errno = saved_err;
759 result = -1;
760 break;
761 }
762 V4L1_LOG("allocated v4l1 buffer @ %p\n",
763 devices[index].v4l1_frame_pointer);
764 }
765 result = 0;
766 break;
767 }
768
769 case VIDIOCMCAPTURE: {
770 struct video_mmap *map = arg;
771
772 devices[index].flags |= V4L1_PIX_FMT_TOUCHED |
773 V4L1_PIX_SIZE_TOUCHED;
774
775 result = v4l1_set_format(index, map->width, map->height,
776 map->format, 0);
777 break;
778 }
779
780 case VIDIOCSYNC: {
781 int *frame_index = arg;
782
783 if (devices[index].v4l1_frame_pointer == MAP_FAILED ||
784 *frame_index < 0 || *frame_index >= V4L1_NO_FRAMES) {
785 errno = EINVAL;
786 result = -1;
787 break;
788 }
789
790 result = v4l2_read(devices[index].fd,
791 devices[index].v4l1_frame_pointer +
792 *frame_index * V4L1_FRAME_BUF_SIZE,
793 V4L1_FRAME_BUF_SIZE);
794 result = (result > 0) ? 0 : result;
795 break;
796 }
797
798 /* We are passing through v4l2 calls to libv4l2 for applications which are
799 using v4l2 through libv4l1 (possible with the v4l1compat.so wrapper).
800
801 So the application could be calling VIDIOC_S_FMT, in this case update
802 our own bookkeeping of the cam's format. Note that this really only is
803 relevant if an application is mixing and matching v4l1 and v4l2 calls,
804 which is crazy, but better safe than sorry. */
805 case VIDIOC_S_FMT: {
806 struct v4l2_format *fmt2 = arg;
807
808 result = v4l2_ioctl(fd, request, arg);
809
810 if (result == 0 && fmt2->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
811 if (devices[index].v4l2_pixfmt != fmt2->fmt.pix.pixelformat) {
812 devices[index].v4l2_pixfmt = fmt2->fmt.pix.pixelformat;
813 devices[index].v4l1_pal =
814 pixelformat_to_palette(fmt2->fmt.pix.pixelformat);
815 }
816 devices[index].width = fmt2->fmt.pix.width;
817 devices[index].height = fmt2->fmt.pix.height;
818 }
819 break;
820 }
821
822 case VIDIOCGFBUF: {
823 struct video_buffer *buffer = arg;
824 struct v4l2_framebuffer fbuf = { 0, };
825
826 result = v4l2_ioctl(fd, VIDIOC_G_FBUF, &fbuf);
827 if (result < 0)
828 break;
829
830 buffer->base = fbuf.base;
831 buffer->height = fbuf.fmt.height;
832 buffer->width = fbuf.fmt.width;
833
834 switch (fbuf.fmt.pixelformat) {
835 case V4L2_PIX_FMT_RGB332:
836 buffer->depth = 8;
837 break;
838 case V4L2_PIX_FMT_RGB555:
839 buffer->depth = 15;
840 break;
841 case V4L2_PIX_FMT_RGB565:
842 buffer->depth = 16;
843 break;
844 case V4L2_PIX_FMT_BGR24:
845 buffer->depth = 24;
846 break;
847 case V4L2_PIX_FMT_BGR32:
848 buffer->depth = 32;
849 break;
850 default:
851 buffer->depth = 0;
852 }
853
854 if (fbuf.fmt.bytesperline) {
855 buffer->bytesperline = fbuf.fmt.bytesperline;
856 if (!buffer->depth && buffer->width)
857 buffer->depth = ((fbuf.fmt.bytesperline << 3)
858 + (buffer->width - 1))
859 / buffer->width;
860 } else {
861 buffer->bytesperline =
862 (buffer->width * buffer->depth + 7) & 7;
863 buffer->bytesperline >>= 3;
864 }
865 break;
866 }
867
868 case VIDIOCSFBUF: {
869 struct video_buffer *buffer = arg;
870 struct v4l2_framebuffer fbuf = { 0, };
871
872 fbuf.base = buffer->base;
873 fbuf.fmt.height = buffer->height;
874 fbuf.fmt.width = buffer->width;
875
876 switch (buffer->depth) {
877 case 8:
878 fbuf.fmt.pixelformat = V4L2_PIX_FMT_RGB332;
879 break;
880 case 15:
881 fbuf.fmt.pixelformat = V4L2_PIX_FMT_RGB555;
882 break;
883 case 16:
884 fbuf.fmt.pixelformat = V4L2_PIX_FMT_RGB565;
885 break;
886 case 24:
887 fbuf.fmt.pixelformat = V4L2_PIX_FMT_BGR24;
888 break;
889 case 32:
890 fbuf.fmt.pixelformat = V4L2_PIX_FMT_BGR32;
891 break;
892 }
893
894 fbuf.fmt.bytesperline = buffer->bytesperline;
895 result = v4l2_ioctl(fd, VIDIOC_S_FBUF, &fbuf);
896 break;
897 }
898
899 case VIDIOCSTUNER: {
900 struct video_tuner *tun = arg;
901 struct v4l2_tuner t = { 0, };
902
903 t.index = tun->tuner;
904 result = v4l2_ioctl(fd, VIDIOC_S_TUNER, &t);
905
906 break;
907 }
908
909 case VIDIOCGTUNER: {
910 int i;
911 struct video_tuner *tun = arg;
912 struct v4l2_tuner tun2 = { 0, };
913 struct v4l2_standard std2 = { 0, };
914 v4l2_std_id sid;
915
916 result = v4l2_ioctl(fd, VIDIOC_G_TUNER, &tun2);
917 if (result < 0)
918 break;
919
920 memcpy(tun->name, tun2.name,
921 min(sizeof(tun->name), sizeof(tun2.name)));
922 tun->name[sizeof(tun->name) - 1] = 0;
923 tun->rangelow = tun2.rangelow;
924 tun->rangehigh = tun2.rangehigh;
925 tun->flags = 0;
926 tun->mode = VIDEO_MODE_AUTO;
927
928 for (i = 0; i < 64; i++) {
929 std2.index = i;
930 if (0 != v4l2_ioctl(fd, VIDIOC_ENUMSTD, &std2))
931 break;
932 if (std2.id & V4L2_STD_PAL)
933 tun->flags |= VIDEO_TUNER_PAL;
934 if (std2.id & V4L2_STD_NTSC)
935 tun->flags |= VIDEO_TUNER_NTSC;
936 if (std2.id & V4L2_STD_SECAM)
937 tun->flags |= VIDEO_TUNER_SECAM;
938 }
939
940 if (v4l2_ioctl(fd, VIDIOC_G_STD, &sid) == 0) {
941 if (sid & V4L2_STD_PAL)
942 tun->mode = VIDEO_MODE_PAL;
943 if (sid & V4L2_STD_NTSC)
944 tun->mode = VIDEO_MODE_NTSC;
945 if (sid & V4L2_STD_SECAM)
946 tun->mode = VIDEO_MODE_SECAM;
947 }
948 if (tun2.capability & V4L2_TUNER_CAP_LOW)
949 tun->flags |= VIDEO_TUNER_LOW;
950 if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
951 tun->flags |= VIDEO_TUNER_STEREO_ON;
952 tun->signal = tun2.signal;
953
954 break;
955 }
956
957 case VIDIOCSFREQ: {
958 unsigned long *freq = arg;
959 struct v4l2_frequency freq2 = { 0, };
960
961 result = v4l2_ioctl(fd, VIDIOC_G_FREQUENCY, &freq2);
962 if (result < 0)
963 break;
964
965 freq2.frequency = *freq;
966
967 result = v4l2_ioctl(fd, VIDIOC_S_FREQUENCY, &freq2);
968
969 break;
970 }
971
972 case VIDIOCGFREQ: {
973 unsigned long *freq = arg;
974 struct v4l2_frequency freq2 = { 0, };
975
976 freq2.tuner = 0;
977 result = v4l2_ioctl(fd, VIDIOC_G_FREQUENCY, &freq2);
978 if (result < 0)
979 break;
980 if (0 == result)
981 *freq = freq2.frequency;
982
983 break;
984 }
985
986 case VIDIOCCAPTURE: {
987 int *on = arg;
988 enum v4l2_buf_type captype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
989
990 if (0 == *on) {
991 /* dirty hack time. But v4l1 has no STREAMOFF
992 * equivalent in the API, and this one at
993 * least comes close ... */
994 v4l2_ioctl(fd, VIDIOC_STREAMOFF, &captype);
995 }
996
997 result = v4l2_ioctl(fd, VIDIOC_OVERLAY, on);
998
999 break;
1000 }
1001
1002 case VIDIOCSAUDIO: {
1003 struct video_audio *aud = arg;
1004 struct v4l2_audio aud2 = { 0, };
1005 struct v4l2_tuner tun2 = { 0, };
1006
1007 aud2.index = aud->audio;
1008 result = v4l2_ioctl(fd, VIDIOC_S_AUDIO, &aud2);
1009 if (result < 0)
1010 break;
1011
1012 v4l2_set_control(fd, V4L2_CID_AUDIO_VOLUME,
1013 aud->volume);
1014 v4l2_set_control(fd, V4L2_CID_AUDIO_BASS,
1015 aud->bass);
1016 v4l2_set_control(fd, V4L2_CID_AUDIO_TREBLE,
1017 aud->treble);
1018 v4l2_set_control(fd, V4L2_CID_AUDIO_BALANCE,
1019 aud->balance);
1020 v4l2_set_control(fd, V4L2_CID_AUDIO_MUTE,
1021 !!(aud->flags & VIDEO_AUDIO_MUTE));
1022
1023 result = v4l2_ioctl(fd, VIDIOC_G_TUNER, &tun2);
1024 if (result == 0) {
1025 switch (aud->mode) {
1026 default:
1027 case VIDEO_SOUND_MONO:
1028 case VIDEO_SOUND_LANG1:
1029 tun2.audmode = V4L2_TUNER_MODE_MONO;
1030 break;
1031 case VIDEO_SOUND_STEREO:
1032 tun2.audmode = V4L2_TUNER_MODE_STEREO;
1033 break;
1034 case VIDEO_SOUND_LANG2:
1035 tun2.audmode = V4L2_TUNER_MODE_LANG2;
1036 break;
1037 }
1038 result = v4l2_ioctl(fd, VIDIOC_S_TUNER, &tun2);
1039 }
1040 /* Ignore errors modifying the tuner settings. */
1041 result = 0;
1042 break;
1043 }
1044
1045 case VIDIOCGAUDIO: {
1046 int i;
1047 struct video_audio *aud = arg;
1048 struct v4l2_queryctrl qctrl2;
1049 struct v4l2_audio aud2 = { 0, };
1050 struct v4l2_tuner tun2;
1051
1052 result = v4l2_ioctl(fd, VIDIOC_G_AUDIO, &aud2);
1053 if (result < 0)
1054 break;
1055
1056 memcpy(aud->name, aud2.name,
1057 min(sizeof(aud->name), sizeof(aud2.name)));
1058 aud->name[sizeof(aud->name) - 1] = 0;
1059 aud->audio = aud2.index;
1060 aud->flags = 0;
1061 i = v4l2_get_control(fd, V4L2_CID_AUDIO_VOLUME);
1062 if (i >= 0) {
1063 aud->volume = i;
1064 aud->flags |= VIDEO_AUDIO_VOLUME;
1065 }
1066 i = v4l2_get_control(fd, V4L2_CID_AUDIO_BASS);
1067 if (i >= 0) {
1068 aud->bass = i;
1069 aud->flags |= VIDEO_AUDIO_BASS;
1070 }
1071 i = v4l2_get_control(fd, V4L2_CID_AUDIO_TREBLE);
1072 if (i >= 0) {
1073 aud->treble = i;
1074 aud->flags |= VIDEO_AUDIO_TREBLE;
1075 }
1076 i = v4l2_get_control(fd, V4L2_CID_AUDIO_BALANCE);
1077 if (i >= 0) {
1078 aud->balance = i;
1079 aud->flags |= VIDEO_AUDIO_BALANCE;
1080 }
1081 i = v4l2_get_control(fd, V4L2_CID_AUDIO_MUTE);
1082 if (i >= 0) {
1083 if (i)
1084 aud->flags |= VIDEO_AUDIO_MUTE;
1085
1086 aud->flags |= VIDEO_AUDIO_MUTABLE;
1087 }
1088 aud->step = 1;
1089 qctrl2.id = V4L2_CID_AUDIO_VOLUME;
1090 if (v4l2_ioctl(fd, VIDIOC_QUERYCTRL, &qctrl2) == 0 &&
1091 !(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
1092 aud->step = qctrl2.step;
1093 aud->mode = 0;
1094
1095 result = v4l2_ioctl(fd, VIDIOC_G_TUNER, &tun2);
1096 if (result < 0) {
1097 result = 0;
1098 break;
1099 }
1100
1101 if (tun2.rxsubchans & V4L2_TUNER_SUB_LANG2)
1102 aud->mode = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
1103 else if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
1104 aud->mode = VIDEO_SOUND_STEREO;
1105 else if (tun2.rxsubchans & V4L2_TUNER_SUB_MONO)
1106 aud->mode = VIDEO_SOUND_MONO;
1107
1108 break;
1109 }
1110
1111 case VIDIOCSVBIFMT: {
1112 struct vbi_format *fmt = arg;
1113 struct v4l2_format fmt2;
1114
1115 if (VIDEO_PALETTE_RAW != fmt->sample_format) {
1116 result = -EINVAL;
1117 break;
1118 }
1119
1120 fmt2.type = V4L2_BUF_TYPE_VBI_CAPTURE;
1121 fmt2.fmt.vbi.samples_per_line = fmt->samples_per_line;
1122 fmt2.fmt.vbi.sampling_rate = fmt->sampling_rate;
1123 fmt2.fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1124 fmt2.fmt.vbi.start[0] = fmt->start[0];
1125 fmt2.fmt.vbi.count[0] = fmt->count[0];
1126 fmt2.fmt.vbi.start[1] = fmt->start[1];
1127 fmt2.fmt.vbi.count[1] = fmt->count[1];
1128 fmt2.fmt.vbi.flags = fmt->flags;
1129
1130 result = v4l2_ioctl(fd, VIDIOC_TRY_FMT, fmt2);
1131 if (result < 0)
1132 break;
1133
1134 if (fmt2.fmt.vbi.samples_per_line != fmt->samples_per_line ||
1135 fmt2.fmt.vbi.sampling_rate != fmt->sampling_rate ||
1136 fmt2.fmt.vbi.sample_format != V4L2_PIX_FMT_GREY ||
1137 fmt2.fmt.vbi.start[0] != fmt->start[0] ||
1138 fmt2.fmt.vbi.count[0] != fmt->count[0] ||
1139 fmt2.fmt.vbi.start[1] != fmt->start[1] ||
1140 fmt2.fmt.vbi.count[1] != fmt->count[1] ||
1141 fmt2.fmt.vbi.flags != fmt->flags) {
1142 result = -EINVAL;
1143 break;
1144 }
1145 result = v4l2_ioctl(fd, VIDIOC_S_FMT, fmt2);
1146 break;
1147 }
1148
1149 case VIDIOCGVBIFMT: {
1150 struct vbi_format *fmt = arg;
1151 struct v4l2_format fmt2 = { 0, };
1152
1153 fmt2.type = V4L2_BUF_TYPE_VBI_CAPTURE;
1154 result = v4l2_ioctl(fd, VIDIOC_G_FMT, &fmt2);
1155
1156 if (result < 0)
1157 break;
1158
1159 if (fmt2.fmt.vbi.sample_format != V4L2_PIX_FMT_GREY) {
1160 result = -EINVAL;
1161 break;
1162 }
1163
1164 fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
1165 fmt->sampling_rate = fmt2.fmt.vbi.sampling_rate;
1166 fmt->sample_format = VIDEO_PALETTE_RAW;
1167 fmt->start[0] = fmt2.fmt.vbi.start[0];
1168 fmt->count[0] = fmt2.fmt.vbi.count[0];
1169 fmt->start[1] = fmt2.fmt.vbi.start[1];
1170 fmt->count[1] = fmt2.fmt.vbi.count[1];
1171 fmt->flags = fmt2.fmt.vbi.flags & 0x03;
1172
1173 break;
1174 }
1175
1176 default:
1177 /* Pass through libv4l2 for applications which are using v4l2 through
1178 libv4l1 (this can happen with the v4l1compat.so wrapper preloaded */
1179 result = v4l2_ioctl(fd, request, arg);
1180 break;
1181 }
1182
1183 if (stream_locked)
1184 pthread_mutex_unlock(&devices[index].stream_lock);
1185
1186 saved_err = errno;
1187 v4l1_log_ioctl(request, arg, result);
1188 errno = saved_err;
1189
1190 return result;
1191 }
1192
1193
1194 ssize_t v4l1_read(int fd, void *buffer, size_t n)
1195 {
1196 int index = v4l1_get_index(fd);
1197 ssize_t result;
1198
1199 if (index == -1)
1200 return SYS_READ(fd, buffer, n);
1201
1202 pthread_mutex_lock(&devices[index].stream_lock);
1203 result = v4l2_read(fd, buffer, n);
1204 pthread_mutex_unlock(&devices[index].stream_lock);
1205
1206 return result;
1207 }
1208
1209
1210 void *v4l1_mmap(void *start, size_t length, int prot, int flags, int fd,
1211 int64_t offset)
1212 {
1213 int index;
1214 void *result;
1215
1216 /* Check if the mmap data matches our answer to VIDIOCGMBUF, if not
1217 pass through libv4l2 for applications which are using v4l2 through
1218 libv4l1 (this can happen with the v4l1compat.so wrapper preloaded */
1219 index = v4l1_get_index(fd);
1220 if (index == -1 || start || offset ||
1221 length != (V4L1_NO_FRAMES * V4L1_FRAME_BUF_SIZE))
1222 return v4l2_mmap(start, length, prot, flags, fd, offset);
1223
1224
1225 pthread_mutex_lock(&devices[index].stream_lock);
1226
1227 /* It could be that we get called with an mmap which seems to match what
1228 we expect, but no VIDIOCGMBUF has been done yet, then it is certainly not
1229 for us so pass it through */
1230 if (devices[index].v4l1_frame_pointer == MAP_FAILED) {
1231 result = v4l2_mmap(start, length, prot, flags, fd, offset);
1232 goto leave;
1233 }
1234
1235 devices[index].v4l1_frame_buf_map_count++;
1236
1237 V4L1_LOG("v4l1 buffer @ %p mapped by application\n",
1238 devices[index].v4l1_frame_pointer);
1239
1240 result = devices[index].v4l1_frame_pointer;
1241
1242 leave:
1243 pthread_mutex_unlock(&devices[index].stream_lock);
1244
1245 return result;
1246 }
1247
1248 int v4l1_munmap(void *_start, size_t length)
1249 {
1250 int index;
1251 unsigned char *start = _start;
1252
1253 /* Is this memory ours? */
1254 if (start != MAP_FAILED &&
1255 length == (V4L1_FRAME_BUF_SIZE * V4L1_NO_FRAMES)) {
1256 for (index = 0; index < devices_used; index++)
1257 if (devices[index].fd != -1 &&
1258 start == devices[index].v4l1_frame_pointer)
1259 break;
1260
1261 if (index != devices_used) {
1262 int unmapped = 0;
1263
1264 pthread_mutex_lock(&devices[index].stream_lock);
1265
1266 /* Redo our checks now that we have the lock, things may have changed */
1267 if (start == devices[index].v4l1_frame_pointer) {
1268 if (devices[index].v4l1_frame_buf_map_count > 0)
1269 devices[index].v4l1_frame_buf_map_count--;
1270
1271 unmapped = 1;
1272 }
1273
1274 pthread_mutex_unlock(&devices[index].stream_lock);
1275
1276 if (unmapped) {
1277 V4L1_LOG("v4l1 buffer munmap %p, %d\n", start, (int)length);
1278 return 0;
1279 }
1280 }
1281 }
1282
1283 V4L1_LOG("v4l1 unknown munmap %p, %d\n", start, (int)length);
1284
1285 /* If not pass through libv4l2 for applications which are using v4l2 through
1286 libv4l1 (this can happen with the v4l1compat.so wrapper preloaded */
1287 return v4l2_munmap(start, length);
1288 }