2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
4 * Copyright (C) 2005-2009
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/version.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/usb.h>
20 #include <linux/videodev2.h>
21 #include <linux/vmalloc.h>
23 #include <linux/wait.h>
24 #include <asm/atomic.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ioctl.h>
31 /* ------------------------------------------------------------------------
36 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
37 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
38 * must be grouped (for instance the Red Balance, Blue Balance and Do White
39 * Balance V4L2 controls use the White Balance Component UVC control) or
40 * otherwise translated. The approach we take here is to use a translation
41 * table for the controls that can be mapped directly, and handle the others
44 static int uvc_v4l2_query_menu(struct uvc_video_chain *chain,
45 struct v4l2_querymenu *query_menu)
47 struct uvc_menu_info *menu_info;
48 struct uvc_control_mapping *mapping;
49 struct uvc_control *ctrl;
50 u32 index = query_menu->index;
51 u32 id = query_menu->id;
53 ctrl = uvc_find_control(chain, query_menu->id, &mapping);
54 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU)
57 if (query_menu->index >= mapping->menu_count)
60 memset(query_menu, 0, sizeof(*query_menu));
62 query_menu->index = index;
64 menu_info = &mapping->menu_info[query_menu->index];
65 strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
70 * Find the frame interval closest to the requested frame interval for the
71 * given frame format and size. This should be done by the device as part of
72 * the Video Probe and Commit negotiation, but some hardware don't implement
75 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
79 if (frame->bFrameIntervalType) {
80 __u32 best = -1, dist;
82 for (i = 0; i < frame->bFrameIntervalType; ++i) {
83 dist = interval > frame->dwFrameInterval[i]
84 ? interval - frame->dwFrameInterval[i]
85 : frame->dwFrameInterval[i] - interval;
93 interval = frame->dwFrameInterval[i-1];
95 const __u32 min = frame->dwFrameInterval[0];
96 const __u32 max = frame->dwFrameInterval[1];
97 const __u32 step = frame->dwFrameInterval[2];
99 interval = min + (interval - min + step/2) / step * step;
107 static int uvc_v4l2_try_format(struct uvc_streaming *stream,
108 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
109 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
111 struct uvc_format *format = NULL;
112 struct uvc_frame *frame = NULL;
114 unsigned int d, maxd;
120 if (fmt->type != stream->type)
123 fcc = (__u8 *)&fmt->fmt.pix.pixelformat;
124 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
125 fmt->fmt.pix.pixelformat,
126 fcc[0], fcc[1], fcc[2], fcc[3],
127 fmt->fmt.pix.width, fmt->fmt.pix.height);
129 /* Check if the hardware supports the requested format. */
130 for (i = 0; i < stream->nformats; ++i) {
131 format = &stream->format[i];
132 if (format->fcc == fmt->fmt.pix.pixelformat)
136 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
137 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n",
138 fmt->fmt.pix.pixelformat);
142 /* Find the closest image size. The distance between image sizes is
143 * the size in pixels of the non-overlapping regions between the
144 * requested size and the frame-specified size.
146 rw = fmt->fmt.pix.width;
147 rh = fmt->fmt.pix.height;
148 maxd = (unsigned int)-1;
150 for (i = 0; i < format->nframes; ++i) {
151 __u16 w = format->frame[i].wWidth;
152 __u16 h = format->frame[i].wHeight;
154 d = min(w, rw) * min(h, rh);
155 d = w*h + rw*rh - 2*d;
158 frame = &format->frame[i];
166 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n",
167 fmt->fmt.pix.width, fmt->fmt.pix.height);
171 /* Use the default frame interval. */
172 interval = frame->dwDefaultFrameInterval;
173 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us "
174 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval,
175 (100000000/interval)%10);
177 /* Set the format index, frame index and frame interval. */
178 memset(probe, 0, sizeof *probe);
179 probe->bmHint = 1; /* dwFrameInterval */
180 probe->bFormatIndex = format->index;
181 probe->bFrameIndex = frame->bFrameIndex;
182 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval);
183 /* Some webcams stall the probe control set request when the
184 * dwMaxVideoFrameSize field is set to zero. The UVC specification
185 * clearly states that the field is read-only from the host, so this
186 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
187 * the webcam to work around the problem.
189 * The workaround could probably be enabled for all webcams, so the
190 * quirk can be removed if needed. It's currently useful to detect
191 * webcam bugs and fix them before they hit the market (providing
192 * developers test their webcams with the Linux driver as well as with
193 * the Windows driver).
195 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS)
196 probe->dwMaxVideoFrameSize =
197 stream->ctrl.dwMaxVideoFrameSize;
199 /* Probe the device. */
200 ret = uvc_probe_video(stream, probe);
204 fmt->fmt.pix.width = frame->wWidth;
205 fmt->fmt.pix.height = frame->wHeight;
206 fmt->fmt.pix.field = V4L2_FIELD_NONE;
207 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
208 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
209 fmt->fmt.pix.colorspace = format->colorspace;
210 fmt->fmt.pix.priv = 0;
212 if (uvc_format != NULL)
213 *uvc_format = format;
214 if (uvc_frame != NULL)
221 static int uvc_v4l2_get_format(struct uvc_streaming *stream,
222 struct v4l2_format *fmt)
224 struct uvc_format *format = stream->cur_format;
225 struct uvc_frame *frame = stream->cur_frame;
227 if (fmt->type != stream->type)
230 if (format == NULL || frame == NULL)
233 fmt->fmt.pix.pixelformat = format->fcc;
234 fmt->fmt.pix.width = frame->wWidth;
235 fmt->fmt.pix.height = frame->wHeight;
236 fmt->fmt.pix.field = V4L2_FIELD_NONE;
237 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;
238 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
239 fmt->fmt.pix.colorspace = format->colorspace;
240 fmt->fmt.pix.priv = 0;
245 static int uvc_v4l2_set_format(struct uvc_streaming *stream,
246 struct v4l2_format *fmt)
248 struct uvc_streaming_control probe;
249 struct uvc_format *format;
250 struct uvc_frame *frame;
253 if (fmt->type != stream->type)
256 if (uvc_queue_allocated(&stream->queue))
259 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame);
263 memcpy(&stream->ctrl, &probe, sizeof probe);
264 stream->cur_format = format;
265 stream->cur_frame = frame;
270 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream,
271 struct v4l2_streamparm *parm)
273 uint32_t numerator, denominator;
275 if (parm->type != stream->type)
278 numerator = stream->ctrl.dwFrameInterval;
279 denominator = 10000000;
280 uvc_simplify_fraction(&numerator, &denominator, 8, 333);
282 memset(parm, 0, sizeof *parm);
283 parm->type = stream->type;
285 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
286 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
287 parm->parm.capture.capturemode = 0;
288 parm->parm.capture.timeperframe.numerator = numerator;
289 parm->parm.capture.timeperframe.denominator = denominator;
290 parm->parm.capture.extendedmode = 0;
291 parm->parm.capture.readbuffers = 0;
293 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
294 parm->parm.output.outputmode = 0;
295 parm->parm.output.timeperframe.numerator = numerator;
296 parm->parm.output.timeperframe.denominator = denominator;
302 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
303 struct v4l2_streamparm *parm)
305 struct uvc_frame *frame = stream->cur_frame;
306 struct uvc_streaming_control probe;
307 struct v4l2_fract timeperframe;
311 if (parm->type != stream->type)
314 if (uvc_queue_streaming(&stream->queue))
317 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
318 timeperframe = parm->parm.capture.timeperframe;
320 timeperframe = parm->parm.output.timeperframe;
322 memcpy(&probe, &stream->ctrl, sizeof probe);
323 interval = uvc_fraction_to_interval(timeperframe.numerator,
324 timeperframe.denominator);
326 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n",
327 timeperframe.numerator, timeperframe.denominator, interval);
328 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval);
330 /* Probe the device with the new settings. */
331 ret = uvc_probe_video(stream, &probe);
335 memcpy(&stream->ctrl, &probe, sizeof probe);
337 /* Return the actual frame period. */
338 timeperframe.numerator = probe.dwFrameInterval;
339 timeperframe.denominator = 10000000;
340 uvc_simplify_fraction(&timeperframe.numerator,
341 &timeperframe.denominator, 8, 333);
343 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
344 parm->parm.capture.timeperframe = timeperframe;
346 parm->parm.output.timeperframe = timeperframe;
351 /* ------------------------------------------------------------------------
352 * Privilege management
356 * Privilege management is the multiple-open implementation basis. The current
357 * implementation is completely transparent for the end-user and doesn't
358 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
359 * Those ioctls enable finer control on the device (by making possible for a
360 * user to request exclusive access to a device), but are not mature yet.
361 * Switching to the V4L2 priority mechanism might be considered in the future
362 * if this situation changes.
364 * Each open instance of a UVC device can either be in a privileged or
365 * unprivileged state. Only a single instance can be in a privileged state at
366 * a given time. Trying to perform an operation that requires privileges will
367 * automatically acquire the required privileges if possible, or return -EBUSY
368 * otherwise. Privileges are dismissed when closing the instance or when
369 * freeing the video buffers using VIDIOC_REQBUFS.
371 * Operations that require privileges are:
378 static int uvc_acquire_privileges(struct uvc_fh *handle)
380 /* Always succeed if the handle is already privileged. */
381 if (handle->state == UVC_HANDLE_ACTIVE)
384 /* Check if the device already has a privileged handle. */
385 if (atomic_inc_return(&handle->stream->active) != 1) {
386 atomic_dec(&handle->stream->active);
390 handle->state = UVC_HANDLE_ACTIVE;
394 static void uvc_dismiss_privileges(struct uvc_fh *handle)
396 if (handle->state == UVC_HANDLE_ACTIVE)
397 atomic_dec(&handle->stream->active);
399 handle->state = UVC_HANDLE_PASSIVE;
402 static int uvc_has_privileges(struct uvc_fh *handle)
404 return handle->state == UVC_HANDLE_ACTIVE;
407 /* ------------------------------------------------------------------------
408 * V4L2 file operations
411 static int uvc_v4l2_open(struct file *file)
413 struct uvc_streaming *stream;
414 struct uvc_fh *handle;
417 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n");
418 stream = video_drvdata(file);
420 if (stream->dev->state & UVC_DEV_DISCONNECTED)
423 ret = usb_autopm_get_interface(stream->dev->intf);
427 /* Create the device handle. */
428 handle = kzalloc(sizeof *handle, GFP_KERNEL);
429 if (handle == NULL) {
430 usb_autopm_put_interface(stream->dev->intf);
434 if (atomic_inc_return(&stream->dev->users) == 1) {
435 ret = uvc_status_start(stream->dev);
437 usb_autopm_put_interface(stream->dev->intf);
438 atomic_dec(&stream->dev->users);
444 handle->chain = stream->chain;
445 handle->stream = stream;
446 handle->state = UVC_HANDLE_PASSIVE;
447 file->private_data = handle;
452 static int uvc_v4l2_release(struct file *file)
454 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
455 struct uvc_streaming *stream = handle->stream;
457 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n");
459 /* Only free resources if this is a privileged handle. */
460 if (uvc_has_privileges(handle)) {
461 uvc_video_enable(stream, 0);
463 mutex_lock(&stream->queue.mutex);
464 if (uvc_free_buffers(&stream->queue) < 0)
465 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to "
467 mutex_unlock(&stream->queue.mutex);
470 /* Release the file handle. */
471 uvc_dismiss_privileges(handle);
473 file->private_data = NULL;
475 if (atomic_dec_return(&stream->dev->users) == 0)
476 uvc_status_stop(stream->dev);
478 usb_autopm_put_interface(stream->dev->intf);
482 static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
484 struct video_device *vdev = video_devdata(file);
485 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
486 struct uvc_video_chain *chain = handle->chain;
487 struct uvc_streaming *stream = handle->stream;
491 /* Query capabilities */
492 case VIDIOC_QUERYCAP:
494 struct v4l2_capability *cap = arg;
496 memset(cap, 0, sizeof *cap);
497 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);
498 strlcpy(cap->card, vdev->name, sizeof cap->card);
499 usb_make_path(stream->dev->udev,
500 cap->bus_info, sizeof(cap->bus_info));
501 cap->version = DRIVER_VERSION_NUMBER;
502 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
503 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
504 | V4L2_CAP_STREAMING;
506 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
507 | V4L2_CAP_STREAMING;
511 /* Get, Set & Query control */
512 case VIDIOC_QUERYCTRL:
513 return uvc_query_v4l2_ctrl(chain, arg);
517 struct v4l2_control *ctrl = arg;
518 struct v4l2_ext_control xctrl;
520 memset(&xctrl, 0, sizeof xctrl);
523 ret = uvc_ctrl_begin(chain);
527 ret = uvc_ctrl_get(chain, &xctrl);
528 uvc_ctrl_rollback(chain);
530 ctrl->value = xctrl.value;
536 struct v4l2_control *ctrl = arg;
537 struct v4l2_ext_control xctrl;
539 memset(&xctrl, 0, sizeof xctrl);
541 xctrl.value = ctrl->value;
543 ret = uvc_ctrl_begin(chain);
547 ret = uvc_ctrl_set(chain, &xctrl);
549 uvc_ctrl_rollback(chain);
552 ret = uvc_ctrl_commit(chain);
554 ctrl->value = xctrl.value;
558 case VIDIOC_QUERYMENU:
559 return uvc_v4l2_query_menu(chain, arg);
561 case VIDIOC_G_EXT_CTRLS:
563 struct v4l2_ext_controls *ctrls = arg;
564 struct v4l2_ext_control *ctrl = ctrls->controls;
567 ret = uvc_ctrl_begin(chain);
571 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
572 ret = uvc_ctrl_get(chain, ctrl);
574 uvc_ctrl_rollback(chain);
575 ctrls->error_idx = i;
579 ctrls->error_idx = 0;
580 ret = uvc_ctrl_rollback(chain);
584 case VIDIOC_S_EXT_CTRLS:
585 case VIDIOC_TRY_EXT_CTRLS:
587 struct v4l2_ext_controls *ctrls = arg;
588 struct v4l2_ext_control *ctrl = ctrls->controls;
591 ret = uvc_ctrl_begin(chain);
595 for (i = 0; i < ctrls->count; ++ctrl, ++i) {
596 ret = uvc_ctrl_set(chain, ctrl);
598 uvc_ctrl_rollback(chain);
599 ctrls->error_idx = i;
604 ctrls->error_idx = 0;
606 if (cmd == VIDIOC_S_EXT_CTRLS)
607 ret = uvc_ctrl_commit(chain);
609 ret = uvc_ctrl_rollback(chain);
613 /* Get, Set & Enum input */
614 case VIDIOC_ENUMINPUT:
616 const struct uvc_entity *selector = chain->selector;
617 struct v4l2_input *input = arg;
618 struct uvc_entity *iterm = NULL;
619 u32 index = input->index;
622 if (selector == NULL ||
623 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
626 list_for_each_entry(iterm, &chain->entities, chain) {
627 if (UVC_ENTITY_IS_ITERM(iterm))
631 } else if (pin < selector->bNrInPins) {
632 pin = selector->baSourceID[index];
633 list_for_each_entry(iterm, &chain->entities, chain) {
634 if (!UVC_ENTITY_IS_ITERM(iterm))
636 if (iterm->id == pin)
641 if (iterm == NULL || iterm->id != pin)
644 memset(input, 0, sizeof *input);
645 input->index = index;
646 strlcpy(input->name, iterm->name, sizeof input->name);
647 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA)
648 input->type = V4L2_INPUT_TYPE_CAMERA;
656 if (chain->selector == NULL ||
657 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
662 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
663 chain->selector->id, chain->dev->intfnum,
664 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
668 *(int *)arg = input - 1;
674 u32 input = *(u32 *)arg + 1;
676 if ((ret = uvc_acquire_privileges(handle)) < 0)
679 if (chain->selector == NULL ||
680 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) {
686 if (input == 0 || input > chain->selector->bNrInPins)
689 return uvc_query_ctrl(chain->dev, UVC_SET_CUR,
690 chain->selector->id, chain->dev->intfnum,
691 UVC_SU_INPUT_SELECT_CONTROL, &input, 1);
694 /* Try, Get, Set & Enum format */
695 case VIDIOC_ENUM_FMT:
697 struct v4l2_fmtdesc *fmt = arg;
698 struct uvc_format *format;
699 enum v4l2_buf_type type = fmt->type;
700 __u32 index = fmt->index;
702 if (fmt->type != stream->type ||
703 fmt->index >= stream->nformats)
706 memset(fmt, 0, sizeof(*fmt));
710 format = &stream->format[fmt->index];
712 if (format->flags & UVC_FMT_FLAG_COMPRESSED)
713 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
714 strlcpy(fmt->description, format->name,
715 sizeof fmt->description);
716 fmt->description[sizeof fmt->description - 1] = 0;
717 fmt->pixelformat = format->fcc;
723 struct uvc_streaming_control probe;
725 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL);
729 if ((ret = uvc_acquire_privileges(handle)) < 0)
732 return uvc_v4l2_set_format(stream, arg);
735 return uvc_v4l2_get_format(stream, arg);
737 /* Frame size enumeration */
738 case VIDIOC_ENUM_FRAMESIZES:
740 struct v4l2_frmsizeenum *fsize = arg;
741 struct uvc_format *format = NULL;
742 struct uvc_frame *frame;
745 /* Look for the given pixel format */
746 for (i = 0; i < stream->nformats; i++) {
747 if (stream->format[i].fcc ==
748 fsize->pixel_format) {
749 format = &stream->format[i];
756 if (fsize->index >= format->nframes)
759 frame = &format->frame[fsize->index];
760 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
761 fsize->discrete.width = frame->wWidth;
762 fsize->discrete.height = frame->wHeight;
766 /* Frame interval enumeration */
767 case VIDIOC_ENUM_FRAMEINTERVALS:
769 struct v4l2_frmivalenum *fival = arg;
770 struct uvc_format *format = NULL;
771 struct uvc_frame *frame = NULL;
774 /* Look for the given pixel format and frame size */
775 for (i = 0; i < stream->nformats; i++) {
776 if (stream->format[i].fcc ==
777 fival->pixel_format) {
778 format = &stream->format[i];
785 for (i = 0; i < format->nframes; i++) {
786 if (format->frame[i].wWidth == fival->width &&
787 format->frame[i].wHeight == fival->height) {
788 frame = &format->frame[i];
795 if (frame->bFrameIntervalType) {
796 if (fival->index >= frame->bFrameIntervalType)
799 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
800 fival->discrete.numerator =
801 frame->dwFrameInterval[fival->index];
802 fival->discrete.denominator = 10000000;
803 uvc_simplify_fraction(&fival->discrete.numerator,
804 &fival->discrete.denominator, 8, 333);
806 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
807 fival->stepwise.min.numerator =
808 frame->dwFrameInterval[0];
809 fival->stepwise.min.denominator = 10000000;
810 fival->stepwise.max.numerator =
811 frame->dwFrameInterval[1];
812 fival->stepwise.max.denominator = 10000000;
813 fival->stepwise.step.numerator =
814 frame->dwFrameInterval[2];
815 fival->stepwise.step.denominator = 10000000;
816 uvc_simplify_fraction(&fival->stepwise.min.numerator,
817 &fival->stepwise.min.denominator, 8, 333);
818 uvc_simplify_fraction(&fival->stepwise.max.numerator,
819 &fival->stepwise.max.denominator, 8, 333);
820 uvc_simplify_fraction(&fival->stepwise.step.numerator,
821 &fival->stepwise.step.denominator, 8, 333);
826 /* Get & Set streaming parameters */
828 return uvc_v4l2_get_streamparm(stream, arg);
831 if ((ret = uvc_acquire_privileges(handle)) < 0)
834 return uvc_v4l2_set_streamparm(stream, arg);
836 /* Cropping and scaling */
839 struct v4l2_cropcap *ccap = arg;
840 struct uvc_frame *frame = stream->cur_frame;
842 if (ccap->type != stream->type)
845 ccap->bounds.left = 0;
846 ccap->bounds.top = 0;
847 ccap->bounds.width = frame->wWidth;
848 ccap->bounds.height = frame->wHeight;
850 ccap->defrect = ccap->bounds;
852 ccap->pixelaspect.numerator = 1;
853 ccap->pixelaspect.denominator = 1;
861 /* Buffers & streaming */
864 struct v4l2_requestbuffers *rb = arg;
865 unsigned int bufsize =
866 stream->ctrl.dwMaxVideoFrameSize;
868 if (rb->type != stream->type ||
869 rb->memory != V4L2_MEMORY_MMAP)
872 if ((ret = uvc_acquire_privileges(handle)) < 0)
875 ret = uvc_alloc_buffers(&stream->queue, rb->count, bufsize);
880 uvc_dismiss_privileges(handle);
887 case VIDIOC_QUERYBUF:
889 struct v4l2_buffer *buf = arg;
891 if (buf->type != stream->type)
894 if (!uvc_has_privileges(handle))
897 return uvc_query_buffer(&stream->queue, buf);
901 if (!uvc_has_privileges(handle))
904 return uvc_queue_buffer(&stream->queue, arg);
907 if (!uvc_has_privileges(handle))
910 return uvc_dequeue_buffer(&stream->queue, arg,
911 file->f_flags & O_NONBLOCK);
913 case VIDIOC_STREAMON:
917 if (*type != stream->type)
920 if (!uvc_has_privileges(handle))
923 ret = uvc_video_enable(stream, 1);
929 case VIDIOC_STREAMOFF:
933 if (*type != stream->type)
936 if (!uvc_has_privileges(handle))
939 return uvc_video_enable(stream, 0);
942 /* Analog video standards make no sense for digital cameras. */
944 case VIDIOC_QUERYSTD:
950 case VIDIOC_ENUMAUDIO:
951 case VIDIOC_ENUMAUDOUT:
953 case VIDIOC_ENUMOUTPUT:
954 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd);
957 /* Dynamic controls. */
958 case UVCIOC_CTRL_ADD:
960 struct uvc_xu_control_info *xinfo = arg;
961 struct uvc_control_info *info;
963 if (!capable(CAP_SYS_ADMIN))
966 info = kzalloc(sizeof *info, GFP_KERNEL);
970 memcpy(info->entity, xinfo->entity, sizeof info->entity);
971 info->index = xinfo->index;
972 info->selector = xinfo->selector;
973 info->size = xinfo->size;
974 info->flags = xinfo->flags;
976 info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
977 UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF;
979 ret = uvc_ctrl_add_info(info);
985 case UVCIOC_CTRL_MAP:
987 struct uvc_xu_control_mapping *xmap = arg;
988 struct uvc_control_mapping *map;
990 if (!capable(CAP_SYS_ADMIN))
993 map = kzalloc(sizeof *map, GFP_KERNEL);
998 memcpy(map->name, xmap->name, sizeof map->name);
999 memcpy(map->entity, xmap->entity, sizeof map->entity);
1000 map->selector = xmap->selector;
1001 map->size = xmap->size;
1002 map->offset = xmap->offset;
1003 map->v4l2_type = xmap->v4l2_type;
1004 map->data_type = xmap->data_type;
1006 ret = uvc_ctrl_add_mapping(map);
1012 case UVCIOC_CTRL_GET:
1013 return uvc_xu_ctrl_query(chain, arg, 0);
1015 case UVCIOC_CTRL_SET:
1016 return uvc_xu_ctrl_query(chain, arg, 1);
1019 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg,
1020 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD)
1021 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n",
1029 static long uvc_v4l2_ioctl(struct file *file,
1030 unsigned int cmd, unsigned long arg)
1032 if (uvc_trace_param & UVC_TRACE_IOCTL) {
1033 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl(");
1034 v4l_printk_ioctl(cmd);
1038 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
1041 static ssize_t uvc_v4l2_read(struct file *file, char __user *data,
1042 size_t count, loff_t *ppos)
1044 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n");
1051 static void uvc_vm_open(struct vm_area_struct *vma)
1053 struct uvc_buffer *buffer = vma->vm_private_data;
1054 buffer->vma_use_count++;
1057 static void uvc_vm_close(struct vm_area_struct *vma)
1059 struct uvc_buffer *buffer = vma->vm_private_data;
1060 buffer->vma_use_count--;
1063 static const struct vm_operations_struct uvc_vm_ops = {
1064 .open = uvc_vm_open,
1065 .close = uvc_vm_close,
1068 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1070 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
1071 struct uvc_streaming *stream = handle->stream;
1072 struct uvc_video_queue *queue = &stream->queue;
1073 struct uvc_buffer *uninitialized_var(buffer);
1075 unsigned long addr, start, size;
1079 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n");
1081 start = vma->vm_start;
1082 size = vma->vm_end - vma->vm_start;
1084 mutex_lock(&queue->mutex);
1086 for (i = 0; i < queue->count; ++i) {
1087 buffer = &queue->buffer[i];
1088 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1092 if (i == queue->count || size != queue->buf_size) {
1098 * VM_IO marks the area as being an mmaped region for I/O to a
1099 * device. It also prevents the region from being core dumped.
1101 vma->vm_flags |= VM_IO;
1103 addr = (unsigned long)queue->mem + buffer->buf.m.offset;
1105 page = vmalloc_to_page((void *)addr);
1106 if ((ret = vm_insert_page(vma, start, page)) < 0)
1114 vma->vm_ops = &uvc_vm_ops;
1115 vma->vm_private_data = buffer;
1119 mutex_unlock(&queue->mutex);
1123 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait)
1125 struct uvc_fh *handle = (struct uvc_fh *)file->private_data;
1126 struct uvc_streaming *stream = handle->stream;
1128 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n");
1130 return uvc_queue_poll(&stream->queue, file, wait);
1133 const struct v4l2_file_operations uvc_fops = {
1134 .owner = THIS_MODULE,
1135 .open = uvc_v4l2_open,
1136 .release = uvc_v4l2_release,
1137 .ioctl = uvc_v4l2_ioctl,
1138 .read = uvc_v4l2_read,
1139 .mmap = uvc_v4l2_mmap,
1140 .poll = uvc_v4l2_poll,