Example #1
0
static int v4l2_map_buffers(int index)
{
	int result = 0;
	unsigned int i;
	struct v4l2_buffer buf;

	for (i = 0; i < devices[index].no_frames; i++) {
		if (devices[index].frame_pointers[i] != MAP_FAILED)
			continue;

		buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		buf.memory = V4L2_MEMORY_MMAP;
		buf.index = i;
		result = SYS_IOCTL(devices[index].fd, VIDIOC_QUERYBUF, &buf);
		if (result) {
			int saved_err = errno;

			V4L2_LOG_ERR("querying buffer %u: %s\n", i, strerror(errno));
			errno = saved_err;
			break;
		}

		devices[index].frame_pointers[i] = (void *)SYS_MMAP(NULL,
				(size_t)buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, devices[index].fd,
				buf.m.offset);
		if (devices[index].frame_pointers[i] == MAP_FAILED) {
			int saved_err = errno;

			V4L2_LOG_ERR("mmapping buffer %u: %s\n", i, strerror(errno));
			errno = saved_err;
			result = -1;
			break;
		}
		V4L2_LOG("mapped buffer %u at %p\n", i,
				devices[index].frame_pointers[i]);

		devices[index].frame_sizes[i] = buf.length;
	}

	return result;
}
Example #2
0
int v4l2_ioctl(int fd, unsigned long int request, ...)
{
	void *arg;
	va_list ap;
	int result, index, saved_err;
	int is_capture_request = 0, stream_needs_locking = 0;

	va_start(ap, request);
	arg = va_arg(ap, void *);
	va_end(ap);

	index = v4l2_get_index(fd);
	if (index == -1)
		return SYS_IOCTL(fd, request, arg);

	/* Appearantly the kernel and / or glibc ignore the 32 most significant bits
	   when long = 64 bits, and some applications pass an int holding the req to
	   ioctl, causing it to get sign extended, depending upon this behavior */
	request = (unsigned int)request;

	/* Is this a capture request and do we need to take the stream lock? */
	switch (request) {
	case VIDIOC_QUERYCTRL:
	case VIDIOC_G_CTRL:
	case VIDIOC_S_CTRL:
		if (!(devices[index].flags & V4L2_DISABLE_CONVERSION))
			is_capture_request = 1;
		break;
	case VIDIOC_QUERYCAP:
		is_capture_request = 1;
		break;
	case VIDIOC_ENUM_FMT:
		if (((struct v4l2_fmtdesc *)arg)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
				!(devices[index].flags & V4L2_DISABLE_CONVERSION))
			is_capture_request = 1;
		break;
	case VIDIOC_ENUM_FRAMESIZES:
	case VIDIOC_ENUM_FRAMEINTERVALS:
		if (!(devices[index].flags & V4L2_DISABLE_CONVERSION))
			is_capture_request = 1;
		break;
	case VIDIOC_TRY_FMT:
		if (((struct v4l2_format *)arg)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
				!(devices[index].flags & V4L2_DISABLE_CONVERSION))
			is_capture_request = 1;
		break;
	case VIDIOC_S_FMT:
	case VIDIOC_G_FMT:
		if (((struct v4l2_format *)arg)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
			is_capture_request = 1;
			stream_needs_locking = 1;
		}
		break;
	case VIDIOC_REQBUFS:
		if (((struct v4l2_requestbuffers *)arg)->type ==
				V4L2_BUF_TYPE_VIDEO_CAPTURE) {
			is_capture_request = 1;
			stream_needs_locking = 1;
		}
		break;
	case VIDIOC_QUERYBUF:
	case VIDIOC_QBUF:
	case VIDIOC_DQBUF:
		if (((struct v4l2_buffer *)arg)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
			is_capture_request = 1;
			stream_needs_locking = 1;
		}
		break;
	case VIDIOC_STREAMON:
	case VIDIOC_STREAMOFF:
		if (*((enum v4l2_buf_type *)arg) == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
			is_capture_request = 1;
			stream_needs_locking = 1;
		}
	}

	if (!is_capture_request) {
		result = SYS_IOCTL(fd, request, arg);
		saved_err = errno;
		v4l2_log_ioctl(request, arg, result);
		errno = saved_err;
		return result;
	}


	if (stream_needs_locking) {
		pthread_mutex_lock(&devices[index].stream_lock);
		/* If this is the first stream related ioctl, and we should only allow
		   libv4lconvert supported destination formats (so that it can do flipping,
		   processing, etc.) and the current destination format is not supported,
		   try setting the format to RGB24 (which is a supported dest. format). */
		if (!(devices[index].flags & V4L2_STREAM_TOUCHED) &&
				!(devices[index].flags & V4L2_DISABLE_CONVERSION) &&
				v4lconvert_supported_dst_fmt_only(devices[index].convert) &&
				!v4lconvert_supported_dst_format(
					devices[index].dest_fmt.fmt.pix.pixelformat)) {
			struct v4l2_format fmt = devices[index].dest_fmt;

			V4L2_LOG("Setting pixelformat to RGB24 (supported_dst_fmt_only)");
			devices[index].flags |= V4L2_STREAM_TOUCHED;
			fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
			pthread_mutex_unlock(&devices[index].stream_lock);
			v4l2_ioctl(fd, VIDIOC_S_FMT, &fmt);
			pthread_mutex_lock(&devices[index].stream_lock);
			V4L2_LOG("Done setting pixelformat (supported_dst_fmt_only)");
		}
		devices[index].flags |= V4L2_STREAM_TOUCHED;
	}

	switch (request) {
	case VIDIOC_QUERYCTRL:
		result = v4lconvert_vidioc_queryctrl(devices[index].convert, arg);
		break;

	case VIDIOC_G_CTRL:
		result = v4lconvert_vidioc_g_ctrl(devices[index].convert, arg);
		break;

	case VIDIOC_S_CTRL:
		result = v4lconvert_vidioc_s_ctrl(devices[index].convert, arg);
		break;

	case VIDIOC_QUERYCAP: {
		struct v4l2_capability *cap = arg;

		result = SYS_IOCTL(devices[index].fd, VIDIOC_QUERYCAP, cap);
		if (result == 0)
			/* We always support read() as we fake it using mmap mode */
			cap->capabilities |= V4L2_CAP_READWRITE;
		break;
	}

	case VIDIOC_ENUM_FMT:
		result = v4lconvert_enum_fmt(devices[index].convert, arg);
		break;

	case VIDIOC_ENUM_FRAMESIZES:
		result = v4lconvert_enum_framesizes(devices[index].convert, arg);
		break;

	case VIDIOC_ENUM_FRAMEINTERVALS:
		result = v4lconvert_enum_frameintervals(devices[index].convert, arg);
		if (result)
			V4L2_LOG("ENUM_FRAMEINTERVALS Error: %s",
					v4lconvert_get_error_message(devices[index].convert));
		break;

	case VIDIOC_TRY_FMT:
		result = v4lconvert_try_format(devices[index].convert, arg, NULL);
		break;

	case VIDIOC_S_FMT: {
		struct v4l2_format src_fmt, *dest_fmt = arg;
		struct v4l2_pix_format req_pix_fmt;

		/* Don't be lazy on uvc cams, as this triggers a bug in the uvcvideo
		   driver in kernel <= 2.6.28 (with certain cams) */
		if (!(devices[index].flags & V4L2_IS_UVC) &&
				v4l2_pix_fmt_identical(&devices[index].dest_fmt, dest_fmt)) {
			*dest_fmt = devices[index].dest_fmt;
			result = 0;
			break;
		}

		if (v4l2_log_file) {
			int pixfmt = dest_fmt->fmt.pix.pixelformat;

			fprintf(v4l2_log_file, "VIDIOC_S_FMT app requesting: %c%c%c%c\n",
					pixfmt & 0xff,
					(pixfmt >> 8) & 0xff,
					(pixfmt >> 16) & 0xff,
					pixfmt >> 24);
		}

		if (devices[index].flags & V4L2_DISABLE_CONVERSION) {
			result = SYS_IOCTL(devices[index].fd, VIDIOC_TRY_FMT,
					dest_fmt);
			src_fmt = *dest_fmt;
		} else {
			result = v4lconvert_try_format(devices[index].convert, dest_fmt,
					&src_fmt);
		}

		if (result) {
			saved_err = errno;
			V4L2_LOG("S_FMT error trying format: %s\n", strerror(errno));
			errno = saved_err;
			break;
		}

		if (src_fmt.fmt.pix.pixelformat != dest_fmt->fmt.pix.pixelformat &&
				v4l2_log_file) {
			int pixfmt = src_fmt.fmt.pix.pixelformat;

			fprintf(v4l2_log_file, "VIDIOC_S_FMT converting from: %c%c%c%c\n",
					pixfmt & 0xff,
					(pixfmt >> 8) & 0xff,
					(pixfmt >> 16) & 0xff,
					pixfmt >> 24);
		}

		/* Maybe after try format has adjusted width/height etc, to whats
		   available nothing has changed (on the cam side) ? */
		if (!(devices[index].flags & V4L2_IS_UVC) &&
				v4l2_pix_fmt_identical(&devices[index].src_fmt, &src_fmt)) {
			v4l2_set_src_and_dest_format(index, &devices[index].src_fmt,
					dest_fmt);
			result = 0;
			break;
		}

		result = v4l2_check_buffer_change_ok(index);
		if (result)
			break;

		req_pix_fmt = src_fmt.fmt.pix;
		result = SYS_IOCTL(devices[index].fd, VIDIOC_S_FMT, &src_fmt);
		if (result) {
			saved_err = errno;
			V4L2_LOG_ERR("setting pixformat: %s\n", strerror(errno));
			/* Report to the app dest_fmt has not changed */
			*dest_fmt = devices[index].dest_fmt;
			errno = saved_err;
			break;
		}
		/* See if we've gotten what try_fmt promised us
		   (this check should never fail) */
		if (src_fmt.fmt.pix.width != req_pix_fmt.width ||
				src_fmt.fmt.pix.height != req_pix_fmt.height ||
				src_fmt.fmt.pix.pixelformat != req_pix_fmt.pixelformat) {
			V4L2_LOG_ERR("set_fmt gave us a different result then try_fmt!\n");
			/* Not what we expected / wanted, disable conversion */
			*dest_fmt = src_fmt;
		}

		v4l2_set_src_and_dest_format(index, &src_fmt, dest_fmt);
		break;
	}

	case VIDIOC_G_FMT: {
		struct v4l2_format *fmt = arg;

		*fmt = devices[index].dest_fmt;
		result = 0;
		break;
	}

	case VIDIOC_REQBUFS: {
		struct v4l2_requestbuffers *req = arg;

		/* IMPROVEME (maybe?) add support for userptr's? */
		if (req->memory != V4L2_MEMORY_MMAP) {
			errno = EINVAL;
			result = -1;
			break;
		}

		result = v4l2_check_buffer_change_ok(index);
		if (result)
			break;

		/* No more buffers then we can manage please */
		if (req->count > V4L2_MAX_NO_FRAMES)
			req->count = V4L2_MAX_NO_FRAMES;

		result = SYS_IOCTL(devices[index].fd, VIDIOC_REQBUFS, req);
		if (result < 0)
			break;
		result = 0; /* some drivers return the number of buffers on success */

		devices[index].no_frames = MIN(req->count, V4L2_MAX_NO_FRAMES);
		devices[index].flags &= ~V4L2_BUFFERS_REQUESTED_BY_READ;
		break;
	}

	case VIDIOC_QUERYBUF: {
		struct v4l2_buffer *buf = arg;

		if (devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) {
			result = v4l2_deactivate_read_stream(index);
			if (result)
				break;
		}

		/* Do a real query even when converting to let the driver fill in
		   things like buf->field */
		result = SYS_IOCTL(devices[index].fd, VIDIOC_QUERYBUF, buf);
		if (result || !v4l2_needs_conversion(index))
			break;

		buf->m.offset = V4L2_MMAP_OFFSET_MAGIC | buf->index;
		buf->length = V4L2_FRAME_BUF_SIZE;
		if (devices[index].frame_map_count[buf->index])
			buf->flags |= V4L2_BUF_FLAG_MAPPED;
		else
			buf->flags &= ~V4L2_BUF_FLAG_MAPPED;
		break;
	}

	case VIDIOC_QBUF:
		if (devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) {
			result = v4l2_deactivate_read_stream(index);
			if (result)
				break;
		}

		/* With some drivers the buffers must be mapped before queuing */
		if (v4l2_needs_conversion(index)) {
			result = v4l2_map_buffers(index);
			if (result)
				break;
		}

		result = SYS_IOCTL(devices[index].fd, VIDIOC_QBUF, arg);
		break;

	case VIDIOC_DQBUF: {
		struct v4l2_buffer *buf = arg;

		if (devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) {
			result = v4l2_deactivate_read_stream(index);
			if (result)
				break;
		}

		if (!v4l2_needs_conversion(index)) {
			result = SYS_IOCTL(devices[index].fd, VIDIOC_DQBUF, buf);
			if (result) {
				int saved_err = errno;

				V4L2_LOG_ERR("dequeuing buf: %s\n", strerror(errno));
				errno = saved_err;
			}
			break;
		}

		/* An application can do a DQBUF before mmap-ing in the buffer,
		   but we need the buffer _now_ to write our converted data
		   to it! */
		if (devices[index].convert_mmap_buf == MAP_FAILED) {
			devices[index].convert_mmap_buf = (void *)SYS_MMAP(NULL,
				(size_t)(devices[index].no_frames * V4L2_FRAME_BUF_SIZE),
				PROT_READ | PROT_WRITE,
				MAP_ANONYMOUS | MAP_PRIVATE,
				-1, 0);
			if (devices[index].convert_mmap_buf == MAP_FAILED) {
				saved_err = errno;
				V4L2_LOG_ERR("allocating conversion buffer\n");
				errno = saved_err;
				result = -1;
				break;
			}
		}

		result = v4l2_dequeue_and_convert(index, buf, 0, V4L2_FRAME_BUF_SIZE);
		if (result < 0)
			break;

		buf->bytesused = result;
		buf->m.offset = V4L2_MMAP_OFFSET_MAGIC | buf->index;
		buf->length = V4L2_FRAME_BUF_SIZE;
		if (devices[index].frame_map_count[buf->index])
			buf->flags |= V4L2_BUF_FLAG_MAPPED;
		else
			buf->flags &= ~V4L2_BUF_FLAG_MAPPED;

		result = 0;
		break;
	}

	case VIDIOC_STREAMON:
	case VIDIOC_STREAMOFF:
		if (devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) {
			result = v4l2_deactivate_read_stream(index);
			if (result)
				break;
		}

		if (request == VIDIOC_STREAMON)
			result = v4l2_streamon(index);
		else
			result = v4l2_streamoff(index);
		break;

	default:
		result = SYS_IOCTL(fd, request, arg);
		break;
	}
Example #3
0
int v4l1_ioctl(int fd, unsigned long int request, ...)
{
	void *arg;
	va_list ap;
	int result, index, saved_err, stream_locked = 0;

	va_start(ap, request);
	arg = va_arg(ap, void *);
	va_end(ap);

	index = v4l1_get_index(fd);
	if (index == -1)
		return SYS_IOCTL(fd, request, arg);

	/* Appearantly the kernel and / or glibc ignore the 32 most significant bits
	   when long = 64 bits, and some applications pass an int holding the req to
	   ioctl, causing it to get sign extended, depending upon this behavior */
	request = (unsigned int)request;

	/* do we need to take the stream lock for this ioctl? */
	switch (request) {
	case VIDIOCSPICT:
	case VIDIOCGPICT:
	case VIDIOCSWIN:
	case VIDIOCGWIN:
	case VIDIOCGMBUF:
	case VIDIOCMCAPTURE:
	case VIDIOCSYNC:
	case VIDIOC_S_FMT:
		pthread_mutex_lock(&devices[index].stream_lock);
		stream_locked = 1;
	}

	switch (request) {
	case VIDIOCGCAP: {
		struct video_capability *cap = arg;
		struct v4l2_framebuffer fbuf = { 0, };
		struct v4l2_capability cap2 = { { 0 }, };

		result = v4l2_ioctl(fd, VIDIOC_QUERYCAP, &cap2);
		if (result < 0)
			break;

		if (cap2.capabilities & V4L2_CAP_DEVICE_CAPS)
			cap2.capabilities = cap2.device_caps;
		if (cap2.capabilities & V4L2_CAP_VIDEO_OVERLAY) {
			result = v4l2_ioctl(fd, VIDIOC_G_FBUF, &fbuf);
			if (result < 0)
				memset(&fbuf, 0, sizeof(fbuf));
			result = 0;
		}

		memcpy(cap->name, cap2.card,
		       min(sizeof(cap->name), sizeof(cap2.card)));

		cap->name[sizeof(cap->name) - 1] = 0;

		if (cap2.capabilities & V4L2_CAP_VIDEO_CAPTURE)
			cap->type |= VID_TYPE_CAPTURE;
		if (cap2.capabilities & V4L2_CAP_TUNER)
			cap->type |= VID_TYPE_TUNER;
		if (cap2.capabilities & V4L2_CAP_VBI_CAPTURE)
			cap->type |= VID_TYPE_TELETEXT;
		if (cap2.capabilities & V4L2_CAP_VIDEO_OVERLAY)
			cap->type |= VID_TYPE_OVERLAY;
		if (fbuf.capability & V4L2_FBUF_CAP_LIST_CLIPPING)
			cap->type |= VID_TYPE_CLIPPING;

		cap->channels  = count_inputs(fd);
		cap->minwidth  = devices[index].min_width;
		cap->minheight = devices[index].min_height;
		cap->maxwidth  = devices[index].max_width;
		cap->maxheight = devices[index].max_height;
		break;
	}

	case VIDIOCSPICT: {
		struct video_picture *pic = arg;

		devices[index].flags |= V4L1_PIX_FMT_TOUCHED;

		v4l2_set_control(fd, V4L2_CID_BRIGHTNESS, pic->brightness);
		v4l2_set_control(fd, V4L2_CID_HUE, pic->hue);
		v4l2_set_control(fd, V4L2_CID_CONTRAST, pic->contrast);
		v4l2_set_control(fd, V4L2_CID_SATURATION, pic->colour);
		v4l2_set_control(fd, V4L2_CID_WHITENESS, pic->whiteness);

		result = v4l1_set_format(index, devices[index].width,
				devices[index].height, pic->palette, 0);
		break;
	}

	case VIDIOCGPICT: {
		struct video_picture *pic = arg;
		int i;

		/* If our v4l2 pixformat has no corresponding v4l1 palette, and
		   the app has not touched the pixformat sofar, try setting a
		   palette which does (and which we emulate when necessary) so
		   that applications which just query the current format and
		   then take whatever they get will work */
		if (!(devices[index].flags & V4L1_PIX_FMT_TOUCHED) &&
		    !pixelformat_to_palette(devices[index].v4l2_pixfmt))
			v4l1_set_format(index, devices[index].width,
					devices[index].height,
					VIDEO_PALETTE_RGB24,
					(devices[index].flags &
					 V4L1_PIX_SIZE_TOUCHED) ? 0 : 1);

		devices[index].flags |= V4L1_PIX_FMT_TOUCHED;

		memset(pic, 0, sizeof(*pic));
		pic->depth = devices[index].depth;
		pic->palette = devices[index].v4l1_pal;
		i = v4l2_get_control(devices[index].fd, V4L2_CID_HUE);
		if (i >= 0)
			pic->hue = i;
		i = v4l2_get_control(devices[index].fd, V4L2_CID_SATURATION);
		if (i >= 0)
			pic->colour = i;
		i = v4l2_get_control(devices[index].fd, V4L2_CID_CONTRAST);
		if (i >= 0)
			pic->contrast = i;
		i = v4l2_get_control(devices[index].fd, V4L2_CID_WHITENESS);
		if (i >= 0)
			pic->whiteness = i;
		i = v4l2_get_control(devices[index].fd, V4L2_CID_BRIGHTNESS);
		if (i >= 0)
			pic->brightness = i;

		result = 0;
		break;
	}

	case VIDIOCSWIN:
	case VIDIOCGWIN: {
		struct video_window *win = arg;

		devices[index].flags |= V4L1_PIX_SIZE_TOUCHED;

		if (request == VIDIOCSWIN)
			result = v4l1_set_format(index, win->width, win->height, -1, 1);
		else
			result = 0;

		if (result == 0) {
			win->x = 0;
			win->y = 0;
			win->width  = devices[index].width;
			win->height = devices[index].height;
			win->flags = 0;
		}
		break;
	}

	case VIDIOCGCHAN: {
		struct video_channel *chan = arg;

		/* Set some defaults */
		chan->tuners = 0;
		chan->flags = 0;
		chan->type = VIDEO_TYPE_CAMERA;
		chan->norm = 0;

		if (devices[index].flags & V4L1_SUPPORTS_ENUMINPUT) {
			struct v4l2_input input2 = { .index = chan->channel };

			result = v4l2_ioctl(fd, VIDIOC_ENUMINPUT, &input2);
			if (result < 0)
				break;

			snprintf(chan->name, sizeof(chan->name), "%s",
				 (char *)input2.name);
			if (input2.type == V4L2_INPUT_TYPE_TUNER) {
				chan->tuners = 1;
				chan->type = VIDEO_TYPE_TV;
				chan->flags = VIDEO_VC_TUNER;
			}
		} else {
			/* No ENUMINPUT support, fake it. */
			if (chan->channel == 0) {
				snprintf(chan->name, sizeof(chan->name),
					 "Camera");
				result = 0;
			} else {
				errno  = EINVAL;
				result = -1;
				break;
			}
		}

		/* In case of no ENUMSTD support, ignore the norm member of the
		   channel struct */
		if (devices[index].flags & V4L1_SUPPORTS_ENUMSTD) {
			v4l2_std_id sid;

			result = v4l2_ioctl(fd, VIDIOC_G_STD, &sid);
			if (result < 0)
				break;

			if (sid & V4L2_STD_PAL)
				chan->norm = VIDEO_MODE_PAL;
			if (sid & V4L2_STD_NTSC)
				chan->norm = VIDEO_MODE_NTSC;
			if (sid & V4L2_STD_SECAM)
				chan->norm = VIDEO_MODE_SECAM;
			if (sid == V4L2_STD_ALL)
				chan->norm = VIDEO_MODE_AUTO;
		}
		break;
	}

	case VIDIOCSCHAN: {
		struct video_channel *chan = arg;

		if (devices[index].flags & V4L1_SUPPORTS_ENUMINPUT) {
			result = v4l2_ioctl(fd, VIDIOC_S_INPUT, &chan->channel);
			if (result < 0)
				break;
		} else {
			/* No ENUMINPUT support, assume a single input */
			if (chan->channel != 0) {
				errno  = EINVAL;
				result = -1;
				break;
			}
			result = 0;
		}

		/* In case of no ENUMSTD support, ignore the norm member of the
		   channel struct */
		if (devices[index].flags & V4L1_SUPPORTS_ENUMSTD) {
			v4l2_std_id sid = 0;

			switch (chan->norm) {
			case VIDEO_MODE_PAL:
				sid = V4L2_STD_PAL;
				break;
			case VIDEO_MODE_NTSC:
				sid = V4L2_STD_NTSC;
				break;
			case VIDEO_MODE_SECAM:
				sid = V4L2_STD_SECAM;
				break;
			case VIDEO_MODE_AUTO:
				sid = V4L2_STD_ALL;
				break;
			}

			if (sid)
				result = v4l2_ioctl(fd, VIDIOC_S_STD, &sid);
		}
		break;
	}

	case VIDIOCGMBUF: {
		/* When VIDIOCGMBUF is done, we don't necessarrily know the format the
		   application wants yet (with some apps this is passed for the first
		   time through VIDIOCMCAPTURE), so we just create an anonymous mapping
		   that should be large enough to hold any sort of frame. Note this only
		   takes virtual memory, and does not use memory until actually used. */
		int i;
		struct video_mbuf *mbuf = arg;

		mbuf->size = V4L1_NO_FRAMES * V4L1_FRAME_BUF_SIZE;
		mbuf->frames = V4L1_NO_FRAMES;
		for (i = 0; i < mbuf->frames; i++)
			mbuf->offsets[i] = i * V4L1_FRAME_BUF_SIZE;

		if (devices[index].v4l1_frame_pointer == MAP_FAILED) {
			devices[index].v4l1_frame_pointer = (void *)SYS_MMAP(NULL,
					(size_t)mbuf->size,
					PROT_READ | PROT_WRITE,
					MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
			if (devices[index].v4l1_frame_pointer == MAP_FAILED) {
				saved_err = errno;
				V4L1_LOG_ERR("allocating v4l1 buffer: %s\n", strerror(errno));
				errno = saved_err;
				result = -1;
				break;
			}
			V4L1_LOG("allocated v4l1 buffer @ %p\n",
					devices[index].v4l1_frame_pointer);
		}
		result = 0;
		break;
	}

	case VIDIOCMCAPTURE: {
		struct video_mmap *map = arg;

		devices[index].flags |= V4L1_PIX_FMT_TOUCHED |
			V4L1_PIX_SIZE_TOUCHED;

		result = v4l1_set_format(index, map->width, map->height,
				map->format, 0);
		break;
	}

	case VIDIOCSYNC: {
		int *frame_index = arg;

		if (devices[index].v4l1_frame_pointer == MAP_FAILED ||
				*frame_index < 0 || *frame_index >= V4L1_NO_FRAMES) {
			errno = EINVAL;
			result = -1;
			break;
		}

		result = v4l2_read(devices[index].fd,
				devices[index].v4l1_frame_pointer +
				*frame_index * V4L1_FRAME_BUF_SIZE,
				V4L1_FRAME_BUF_SIZE);
		result = (result > 0) ? 0 : result;
		break;
	}

		/* We are passing through v4l2 calls to libv4l2 for applications which are
		   using v4l2 through libv4l1 (possible with the v4l1compat.so wrapper).

		   So the application could be calling VIDIOC_S_FMT, in this case update
		   our own bookkeeping of the cam's format. Note that this really only is
		   relevant if an application is mixing and matching v4l1 and v4l2 calls,
		   which is crazy, but better safe then sorry. */
	case VIDIOC_S_FMT: {
		struct v4l2_format *fmt2 = arg;

		result = v4l2_ioctl(fd, request, arg);

		if (result == 0 && fmt2->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
			if (devices[index].v4l2_pixfmt != fmt2->fmt.pix.pixelformat) {
				devices[index].v4l2_pixfmt = fmt2->fmt.pix.pixelformat;
				devices[index].v4l1_pal =
					pixelformat_to_palette(fmt2->fmt.pix.pixelformat);
			}
			devices[index].width  = fmt2->fmt.pix.width;
			devices[index].height = fmt2->fmt.pix.height;
		}
		break;
	}

	case VIDIOCGFBUF: {
		struct video_buffer *buffer = arg;
		struct v4l2_framebuffer fbuf = { 0, };

		result = v4l2_ioctl(fd, VIDIOC_G_FBUF, &fbuf);
		if (result < 0)
			break;

		buffer->base = fbuf.base;
		buffer->height = fbuf.fmt.height;
		buffer->width = fbuf.fmt.width;

		switch (fbuf.fmt.pixelformat) {
		case V4L2_PIX_FMT_RGB332:
			buffer->depth = 8;
			break;
		case V4L2_PIX_FMT_RGB555:
			buffer->depth = 15;
			break;
		case V4L2_PIX_FMT_RGB565:
			buffer->depth = 16;
			break;
		case V4L2_PIX_FMT_BGR24:
			buffer->depth = 24;
			break;
		case V4L2_PIX_FMT_BGR32:
			buffer->depth = 32;
			break;
		default:
			buffer->depth = 0;
		}

		if (fbuf.fmt.bytesperline) {
			buffer->bytesperline = fbuf.fmt.bytesperline;
			if (!buffer->depth && buffer->width)
				buffer->depth = ((fbuf.fmt.bytesperline << 3)
						+ (buffer->width - 1))
						/ buffer->width;
		} else {
			buffer->bytesperline =
				(buffer->width * buffer->depth + 7) & 7;
			buffer->bytesperline >>= 3;
		}
		break;
	}

	case VIDIOCSFBUF: {
		struct video_buffer *buffer = arg;
		struct v4l2_framebuffer fbuf = { 0, };

		fbuf.base = buffer->base;
		fbuf.fmt.height = buffer->height;
		fbuf.fmt.width = buffer->width;

		switch (buffer->depth) {
		case 8:
			fbuf.fmt.pixelformat = V4L2_PIX_FMT_RGB332;
			break;
		case 15:
			fbuf.fmt.pixelformat = V4L2_PIX_FMT_RGB555;
			break;
		case 16:
			fbuf.fmt.pixelformat = V4L2_PIX_FMT_RGB565;
			break;
		case 24:
			fbuf.fmt.pixelformat = V4L2_PIX_FMT_BGR24;
			break;
		case 32:
			fbuf.fmt.pixelformat = V4L2_PIX_FMT_BGR32;
			break;
		}

		fbuf.fmt.bytesperline = buffer->bytesperline;
		result = v4l2_ioctl(fd, VIDIOC_S_FBUF, &fbuf);
		break;
	}

	case VIDIOCSTUNER: {
		struct video_tuner *tun = arg;
		struct v4l2_tuner t = { 0, };

		t.index = tun->tuner;
		result = v4l2_ioctl(fd, VIDIOC_S_TUNER, &t);

		break;
	}

	case VIDIOCGTUNER: {
		int i;
		struct video_tuner *tun = arg;
		struct v4l2_tuner tun2 = { 0, };
		struct v4l2_standard std2 = { 0, };
		v4l2_std_id sid;

		result = v4l2_ioctl(fd, VIDIOC_G_TUNER, &tun2);
		if (result < 0)
			break;

		memcpy(tun->name, tun2.name,
			min(sizeof(tun->name), sizeof(tun2.name)));
		tun->name[sizeof(tun->name) - 1] = 0;
		tun->rangelow = tun2.rangelow;
		tun->rangehigh = tun2.rangehigh;
		tun->flags = 0;
		tun->mode = VIDEO_MODE_AUTO;

		for (i = 0; i < 64; i++) {
			std2.index = i;
			if (0 != v4l2_ioctl(fd, VIDIOC_ENUMSTD, &std2))
				break;
			if (std2.id & V4L2_STD_PAL)
				tun->flags |= VIDEO_TUNER_PAL;
			if (std2.id & V4L2_STD_NTSC)
				tun->flags |= VIDEO_TUNER_NTSC;
			if (std2.id & V4L2_STD_SECAM)
				tun->flags |= VIDEO_TUNER_SECAM;
		}

		if (v4l2_ioctl(fd, VIDIOC_G_STD, &sid) == 0) {
			if (sid & V4L2_STD_PAL)
				tun->mode = VIDEO_MODE_PAL;
			if (sid & V4L2_STD_NTSC)
				tun->mode = VIDEO_MODE_NTSC;
			if (sid & V4L2_STD_SECAM)
				tun->mode = VIDEO_MODE_SECAM;
		}
		if (tun2.capability & V4L2_TUNER_CAP_LOW)
			tun->flags |= VIDEO_TUNER_LOW;
		if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
			tun->flags |= VIDEO_TUNER_STEREO_ON;
		tun->signal = tun2.signal;

		break;
	}

	case VIDIOCSFREQ: {
		unsigned long *freq = arg;
		struct v4l2_frequency freq2 = { 0, };

		result = v4l2_ioctl(fd, VIDIOC_G_FREQUENCY, &freq2);
		if (result < 0)
			break;

		freq2.frequency = *freq;

		result = v4l2_ioctl(fd, VIDIOC_S_FREQUENCY, &freq2);

		break;
	}

	case VIDIOCGFREQ: {
		unsigned long *freq = arg;
		struct v4l2_frequency freq2 = { 0, };

		freq2.tuner = 0;
		result = v4l2_ioctl(fd, VIDIOC_G_FREQUENCY, &freq2);
		if (result < 0)
			break;
		if (0 == result)
			*freq = freq2.frequency;

		break;
	}

	case VIDIOCCAPTURE: {
		int *on = arg;
		enum v4l2_buf_type captype = V4L2_BUF_TYPE_VIDEO_CAPTURE;

		if (0 == *on) {
		/* dirty hack time.  But v4l1 has no STREAMOFF
		* equivalent in the API, and this one at
		* least comes close ... */
			v4l2_ioctl(fd, VIDIOC_STREAMOFF, &captype);
		}

		result = v4l2_ioctl(fd, VIDIOC_OVERLAY, on);

		break;
	}

	case VIDIOCSAUDIO: {
		struct video_audio *aud = arg;
		struct v4l2_audio aud2 = { 0, };
		struct v4l2_tuner tun2 = { 0, };

		aud2.index = aud->audio;
		result = v4l2_ioctl(fd, VIDIOC_S_AUDIO, &aud2);
		if (result < 0)
			break;

		v4l2_set_control(fd, V4L2_CID_AUDIO_VOLUME,
			aud->volume);
		v4l2_set_control(fd, V4L2_CID_AUDIO_BASS,
			aud->bass);
		v4l2_set_control(fd, V4L2_CID_AUDIO_TREBLE,
			aud->treble);
		v4l2_set_control(fd, V4L2_CID_AUDIO_BALANCE,
			aud->balance);
		v4l2_set_control(fd, V4L2_CID_AUDIO_MUTE,
			!!(aud->flags & VIDEO_AUDIO_MUTE));

		result = v4l2_ioctl(fd, VIDIOC_G_TUNER, &tun2);
		if (result == 0) {
			switch (aud->mode) {
			default:
			case VIDEO_SOUND_MONO:
			case VIDEO_SOUND_LANG1:
				tun2.audmode = V4L2_TUNER_MODE_MONO;
				break;
			case VIDEO_SOUND_STEREO:
				tun2.audmode = V4L2_TUNER_MODE_STEREO;
				break;
			case VIDEO_SOUND_LANG2:
				tun2.audmode = V4L2_TUNER_MODE_LANG2;
				break;
			}
			result = v4l2_ioctl(fd, VIDIOC_S_TUNER, &tun2);
		}
		/* Ignore errors modifying the tuner settings. */
		result = 0;
		break;
	}

	case VIDIOCGAUDIO: {
		int i;
		struct video_audio *aud = arg;
		struct v4l2_queryctrl qctrl2;
		struct v4l2_audio aud2 = { 0, };
		struct v4l2_tuner tun2;

		result = v4l2_ioctl(fd, VIDIOC_G_AUDIO, &aud2);
		if (result < 0)
			break;

		memcpy(aud->name, aud2.name,
			min(sizeof(aud->name), sizeof(aud2.name)));
		aud->name[sizeof(aud->name) - 1] = 0;
		aud->audio = aud2.index;
		aud->flags = 0;
		i = v4l2_get_control(fd, V4L2_CID_AUDIO_VOLUME);
		if (i >= 0) {
			aud->volume = i;
			aud->flags |= VIDEO_AUDIO_VOLUME;
		}
		i = v4l2_get_control(fd, V4L2_CID_AUDIO_BASS);
		if (i >= 0) {
			aud->bass = i;
			aud->flags |= VIDEO_AUDIO_BASS;
		}
		i = v4l2_get_control(fd, V4L2_CID_AUDIO_TREBLE);
		if (i >= 0) {
			aud->treble = i;
			aud->flags |= VIDEO_AUDIO_TREBLE;
		}
		i = v4l2_get_control(fd, V4L2_CID_AUDIO_BALANCE);
		if (i >= 0) {
			aud->balance = i;
			aud->flags |= VIDEO_AUDIO_BALANCE;
		}
		i = v4l2_get_control(fd, V4L2_CID_AUDIO_MUTE);
		if (i >= 0) {
			if (i)
				aud->flags |= VIDEO_AUDIO_MUTE;

			aud->flags |= VIDEO_AUDIO_MUTABLE;
		}
		aud->step = 1;
		qctrl2.id = V4L2_CID_AUDIO_VOLUME;
		if (v4l2_ioctl(fd, VIDIOC_QUERYCTRL, &qctrl2) == 0 &&
			!(qctrl2.flags & V4L2_CTRL_FLAG_DISABLED))
			aud->step = qctrl2.step;
		aud->mode = 0;

		result = v4l2_ioctl(fd, VIDIOC_G_TUNER, &tun2);
		if (result < 0) {
			result = 0;
			break;
		}

		if (tun2.rxsubchans & V4L2_TUNER_SUB_LANG2)
			aud->mode = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
		else if (tun2.rxsubchans & V4L2_TUNER_SUB_STEREO)
			aud->mode = VIDEO_SOUND_STEREO;
		else if (tun2.rxsubchans & V4L2_TUNER_SUB_MONO)
			aud->mode = VIDEO_SOUND_MONO;

		break;
	}

	case VIDIOCSVBIFMT: {
		struct vbi_format *fmt = arg;
		struct v4l2_format fmt2;

		if (VIDEO_PALETTE_RAW != fmt->sample_format) {
			result = -EINVAL;
			break;
		}

		fmt2.type = V4L2_BUF_TYPE_VBI_CAPTURE;
		fmt2.fmt.vbi.samples_per_line = fmt->samples_per_line;
		fmt2.fmt.vbi.sampling_rate    = fmt->sampling_rate;
		fmt2.fmt.vbi.sample_format    = V4L2_PIX_FMT_GREY;
		fmt2.fmt.vbi.start[0]         = fmt->start[0];
		fmt2.fmt.vbi.count[0]         = fmt->count[0];
		fmt2.fmt.vbi.start[1]         = fmt->start[1];
		fmt2.fmt.vbi.count[1]         = fmt->count[1];
		fmt2.fmt.vbi.flags            = fmt->flags;

		result  = v4l2_ioctl(fd, VIDIOC_TRY_FMT, fmt2);
		if (result < 0)
			break;

		if (fmt2.fmt.vbi.samples_per_line != fmt->samples_per_line ||
		    fmt2.fmt.vbi.sampling_rate    != fmt->sampling_rate    ||
		    fmt2.fmt.vbi.sample_format    != V4L2_PIX_FMT_GREY     ||
		    fmt2.fmt.vbi.start[0]         != fmt->start[0]         ||
		    fmt2.fmt.vbi.count[0]         != fmt->count[0]         ||
		    fmt2.fmt.vbi.start[1]         != fmt->start[1]         ||
		    fmt2.fmt.vbi.count[1]         != fmt->count[1]         ||
		    fmt2.fmt.vbi.flags            != fmt->flags) {
			result = -EINVAL;
			break;
		}
		result = v4l2_ioctl(fd, VIDIOC_S_FMT, fmt2);
		break;
	}

	case VIDIOCGVBIFMT: {
		struct vbi_format *fmt = arg;
		struct v4l2_format fmt2 = { 0, };

		fmt2.type = V4L2_BUF_TYPE_VBI_CAPTURE;
		result = v4l2_ioctl(fd, VIDIOC_G_FMT, &fmt2);

		if (result < 0)
			break;

		if (fmt2.fmt.vbi.sample_format != V4L2_PIX_FMT_GREY) {
			result = -EINVAL;
			break;
		}

		fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
		fmt->sampling_rate    = fmt2.fmt.vbi.sampling_rate;
		fmt->sample_format    = VIDEO_PALETTE_RAW;
		fmt->start[0]         = fmt2.fmt.vbi.start[0];
		fmt->count[0]         = fmt2.fmt.vbi.count[0];
		fmt->start[1]         = fmt2.fmt.vbi.start[1];
		fmt->count[1]         = fmt2.fmt.vbi.count[1];
		fmt->flags            = fmt2.fmt.vbi.flags & 0x03;

		break;
	}

	default:
		/* Pass through libv4l2 for applications which are using v4l2 through
		   libv4l1 (this can happen with the v4l1compat.so wrapper preloaded */
		result = v4l2_ioctl(fd, request, arg);
		break;
	}

	if (stream_locked)
		pthread_mutex_unlock(&devices[index].stream_lock);

	saved_err = errno;
	v4l1_log_ioctl(request, arg, result);
	errno = saved_err;

	return result;
}