2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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 <linux/atomic.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ioctl.h>
31 /* ------------------------------------------------------------------------
34 static int uvc_ioctl_ctrl_map(struct uvc_video_chain
*chain
,
35 struct uvc_xu_control_mapping
*xmap
)
37 struct uvc_control_mapping
*map
;
41 map
= kzalloc(sizeof *map
, GFP_KERNEL
);
46 memcpy(map
->name
, xmap
->name
, sizeof map
->name
);
47 memcpy(map
->entity
, xmap
->entity
, sizeof map
->entity
);
48 map
->selector
= xmap
->selector
;
49 map
->size
= xmap
->size
;
50 map
->offset
= xmap
->offset
;
51 map
->v4l2_type
= xmap
->v4l2_type
;
52 map
->data_type
= xmap
->data_type
;
54 switch (xmap
->v4l2_type
) {
55 case V4L2_CTRL_TYPE_INTEGER
:
56 case V4L2_CTRL_TYPE_BOOLEAN
:
57 case V4L2_CTRL_TYPE_BUTTON
:
60 case V4L2_CTRL_TYPE_MENU
:
61 /* Prevent excessive memory consumption, as well as integer
64 if (xmap
->menu_count
== 0 ||
65 xmap
->menu_count
> UVC_MAX_CONTROL_MENU_ENTRIES
) {
70 size
= xmap
->menu_count
* sizeof(*map
->menu_info
);
71 map
->menu_info
= kmalloc(size
, GFP_KERNEL
);
72 if (map
->menu_info
== NULL
) {
77 if (copy_from_user(map
->menu_info
, xmap
->menu_info
, size
)) {
82 map
->menu_count
= xmap
->menu_count
;
86 uvc_trace(UVC_TRACE_CONTROL
, "Unsupported V4L2 control type "
87 "%u.\n", xmap
->v4l2_type
);
92 ret
= uvc_ctrl_add_mapping(chain
, map
);
95 kfree(map
->menu_info
);
101 /* ------------------------------------------------------------------------
106 * Find the frame interval closest to the requested frame interval for the
107 * given frame format and size. This should be done by the device as part of
108 * the Video Probe and Commit negotiation, but some hardware don't implement
111 static __u32
uvc_try_frame_interval(struct uvc_frame
*frame
, __u32 interval
)
115 if (frame
->bFrameIntervalType
) {
116 __u32 best
= -1, dist
;
118 for (i
= 0; i
< frame
->bFrameIntervalType
; ++i
) {
119 dist
= interval
> frame
->dwFrameInterval
[i
]
120 ? interval
- frame
->dwFrameInterval
[i
]
121 : frame
->dwFrameInterval
[i
] - interval
;
129 interval
= frame
->dwFrameInterval
[i
-1];
131 const __u32 min
= frame
->dwFrameInterval
[0];
132 const __u32 max
= frame
->dwFrameInterval
[1];
133 const __u32 step
= frame
->dwFrameInterval
[2];
135 interval
= min
+ (interval
- min
+ step
/2) / step
* step
;
143 static int uvc_v4l2_try_format(struct uvc_streaming
*stream
,
144 struct v4l2_format
*fmt
, struct uvc_streaming_control
*probe
,
145 struct uvc_format
**uvc_format
, struct uvc_frame
**uvc_frame
)
147 struct uvc_format
*format
= NULL
;
148 struct uvc_frame
*frame
= NULL
;
150 unsigned int d
, maxd
;
156 if (fmt
->type
!= stream
->type
)
159 fcc
= (__u8
*)&fmt
->fmt
.pix
.pixelformat
;
160 uvc_trace(UVC_TRACE_FORMAT
, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
161 fmt
->fmt
.pix
.pixelformat
,
162 fcc
[0], fcc
[1], fcc
[2], fcc
[3],
163 fmt
->fmt
.pix
.width
, fmt
->fmt
.pix
.height
);
165 /* Check if the hardware supports the requested format. */
166 for (i
= 0; i
< stream
->nformats
; ++i
) {
167 format
= &stream
->format
[i
];
168 if (format
->fcc
== fmt
->fmt
.pix
.pixelformat
)
172 if (format
== NULL
|| format
->fcc
!= fmt
->fmt
.pix
.pixelformat
) {
173 uvc_trace(UVC_TRACE_FORMAT
, "Unsupported format 0x%08x.\n",
174 fmt
->fmt
.pix
.pixelformat
);
178 /* Find the closest image size. The distance between image sizes is
179 * the size in pixels of the non-overlapping regions between the
180 * requested size and the frame-specified size.
182 rw
= fmt
->fmt
.pix
.width
;
183 rh
= fmt
->fmt
.pix
.height
;
184 maxd
= (unsigned int)-1;
186 for (i
= 0; i
< format
->nframes
; ++i
) {
187 __u16 w
= format
->frame
[i
].wWidth
;
188 __u16 h
= format
->frame
[i
].wHeight
;
190 d
= min(w
, rw
) * min(h
, rh
);
191 d
= w
*h
+ rw
*rh
- 2*d
;
194 frame
= &format
->frame
[i
];
202 uvc_trace(UVC_TRACE_FORMAT
, "Unsupported size %ux%u.\n",
203 fmt
->fmt
.pix
.width
, fmt
->fmt
.pix
.height
);
207 /* Use the default frame interval. */
208 interval
= frame
->dwDefaultFrameInterval
;
209 uvc_trace(UVC_TRACE_FORMAT
, "Using default frame interval %u.%u us "
210 "(%u.%u fps).\n", interval
/10, interval
%10, 10000000/interval
,
211 (100000000/interval
)%10);
213 /* Set the format index, frame index and frame interval. */
214 memset(probe
, 0, sizeof *probe
);
215 probe
->bmHint
= 1; /* dwFrameInterval */
216 probe
->bFormatIndex
= format
->index
;
217 probe
->bFrameIndex
= frame
->bFrameIndex
;
218 probe
->dwFrameInterval
= uvc_try_frame_interval(frame
, interval
);
219 /* Some webcams stall the probe control set request when the
220 * dwMaxVideoFrameSize field is set to zero. The UVC specification
221 * clearly states that the field is read-only from the host, so this
222 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
223 * the webcam to work around the problem.
225 * The workaround could probably be enabled for all webcams, so the
226 * quirk can be removed if needed. It's currently useful to detect
227 * webcam bugs and fix them before they hit the market (providing
228 * developers test their webcams with the Linux driver as well as with
229 * the Windows driver).
231 mutex_lock(&stream
->mutex
);
232 if (stream
->dev
->quirks
& UVC_QUIRK_PROBE_EXTRAFIELDS
)
233 probe
->dwMaxVideoFrameSize
=
234 stream
->ctrl
.dwMaxVideoFrameSize
;
236 /* Probe the device. */
237 ret
= uvc_probe_video(stream
, probe
);
238 mutex_unlock(&stream
->mutex
);
242 fmt
->fmt
.pix
.width
= frame
->wWidth
;
243 fmt
->fmt
.pix
.height
= frame
->wHeight
;
244 fmt
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
245 fmt
->fmt
.pix
.bytesperline
= format
->bpp
* frame
->wWidth
/ 8;
246 fmt
->fmt
.pix
.sizeimage
= probe
->dwMaxVideoFrameSize
;
247 fmt
->fmt
.pix
.colorspace
= format
->colorspace
;
248 fmt
->fmt
.pix
.priv
= 0;
250 if (uvc_format
!= NULL
)
251 *uvc_format
= format
;
252 if (uvc_frame
!= NULL
)
259 static int uvc_v4l2_get_format(struct uvc_streaming
*stream
,
260 struct v4l2_format
*fmt
)
262 struct uvc_format
*format
;
263 struct uvc_frame
*frame
;
266 if (fmt
->type
!= stream
->type
)
269 mutex_lock(&stream
->mutex
);
270 format
= stream
->cur_format
;
271 frame
= stream
->cur_frame
;
273 if (format
== NULL
|| frame
== NULL
) {
278 fmt
->fmt
.pix
.pixelformat
= format
->fcc
;
279 fmt
->fmt
.pix
.width
= frame
->wWidth
;
280 fmt
->fmt
.pix
.height
= frame
->wHeight
;
281 fmt
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
282 fmt
->fmt
.pix
.bytesperline
= format
->bpp
* frame
->wWidth
/ 8;
283 fmt
->fmt
.pix
.sizeimage
= stream
->ctrl
.dwMaxVideoFrameSize
;
284 fmt
->fmt
.pix
.colorspace
= format
->colorspace
;
285 fmt
->fmt
.pix
.priv
= 0;
288 mutex_unlock(&stream
->mutex
);
292 static int uvc_v4l2_set_format(struct uvc_streaming
*stream
,
293 struct v4l2_format
*fmt
)
295 struct uvc_streaming_control probe
;
296 struct uvc_format
*format
;
297 struct uvc_frame
*frame
;
300 if (fmt
->type
!= stream
->type
)
303 ret
= uvc_v4l2_try_format(stream
, fmt
, &probe
, &format
, &frame
);
307 mutex_lock(&stream
->mutex
);
309 if (uvc_queue_allocated(&stream
->queue
)) {
314 memcpy(&stream
->ctrl
, &probe
, sizeof probe
);
315 stream
->cur_format
= format
;
316 stream
->cur_frame
= frame
;
319 mutex_unlock(&stream
->mutex
);
323 static int uvc_v4l2_get_streamparm(struct uvc_streaming
*stream
,
324 struct v4l2_streamparm
*parm
)
326 uint32_t numerator
, denominator
;
328 if (parm
->type
!= stream
->type
)
331 mutex_lock(&stream
->mutex
);
332 numerator
= stream
->ctrl
.dwFrameInterval
;
333 mutex_unlock(&stream
->mutex
);
335 denominator
= 10000000;
336 uvc_simplify_fraction(&numerator
, &denominator
, 8, 333);
338 memset(parm
, 0, sizeof *parm
);
339 parm
->type
= stream
->type
;
341 if (stream
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
342 parm
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
343 parm
->parm
.capture
.capturemode
= 0;
344 parm
->parm
.capture
.timeperframe
.numerator
= numerator
;
345 parm
->parm
.capture
.timeperframe
.denominator
= denominator
;
346 parm
->parm
.capture
.extendedmode
= 0;
347 parm
->parm
.capture
.readbuffers
= 0;
349 parm
->parm
.output
.capability
= V4L2_CAP_TIMEPERFRAME
;
350 parm
->parm
.output
.outputmode
= 0;
351 parm
->parm
.output
.timeperframe
.numerator
= numerator
;
352 parm
->parm
.output
.timeperframe
.denominator
= denominator
;
358 static int uvc_v4l2_set_streamparm(struct uvc_streaming
*stream
,
359 struct v4l2_streamparm
*parm
)
361 struct uvc_streaming_control probe
;
362 struct v4l2_fract timeperframe
;
366 if (parm
->type
!= stream
->type
)
369 if (parm
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
370 timeperframe
= parm
->parm
.capture
.timeperframe
;
372 timeperframe
= parm
->parm
.output
.timeperframe
;
374 interval
= uvc_fraction_to_interval(timeperframe
.numerator
,
375 timeperframe
.denominator
);
376 uvc_trace(UVC_TRACE_FORMAT
, "Setting frame interval to %u/%u (%u).\n",
377 timeperframe
.numerator
, timeperframe
.denominator
, interval
);
379 mutex_lock(&stream
->mutex
);
381 if (uvc_queue_streaming(&stream
->queue
)) {
382 mutex_unlock(&stream
->mutex
);
386 memcpy(&probe
, &stream
->ctrl
, sizeof probe
);
387 probe
.dwFrameInterval
=
388 uvc_try_frame_interval(stream
->cur_frame
, interval
);
390 /* Probe the device with the new settings. */
391 ret
= uvc_probe_video(stream
, &probe
);
393 mutex_unlock(&stream
->mutex
);
397 memcpy(&stream
->ctrl
, &probe
, sizeof probe
);
398 mutex_unlock(&stream
->mutex
);
400 /* Return the actual frame period. */
401 timeperframe
.numerator
= probe
.dwFrameInterval
;
402 timeperframe
.denominator
= 10000000;
403 uvc_simplify_fraction(&timeperframe
.numerator
,
404 &timeperframe
.denominator
, 8, 333);
406 if (parm
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
407 parm
->parm
.capture
.timeperframe
= timeperframe
;
409 parm
->parm
.output
.timeperframe
= timeperframe
;
414 /* ------------------------------------------------------------------------
415 * Privilege management
419 * Privilege management is the multiple-open implementation basis. The current
420 * implementation is completely transparent for the end-user and doesn't
421 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
422 * Those ioctls enable finer control on the device (by making possible for a
423 * user to request exclusive access to a device), but are not mature yet.
424 * Switching to the V4L2 priority mechanism might be considered in the future
425 * if this situation changes.
427 * Each open instance of a UVC device can either be in a privileged or
428 * unprivileged state. Only a single instance can be in a privileged state at
429 * a given time. Trying to perform an operation that requires privileges will
430 * automatically acquire the required privileges if possible, or return -EBUSY
431 * otherwise. Privileges are dismissed when closing the instance or when
432 * freeing the video buffers using VIDIOC_REQBUFS.
434 * Operations that require privileges are:
441 static int uvc_acquire_privileges(struct uvc_fh
*handle
)
443 /* Always succeed if the handle is already privileged. */
444 if (handle
->state
== UVC_HANDLE_ACTIVE
)
447 /* Check if the device already has a privileged handle. */
448 if (atomic_inc_return(&handle
->stream
->active
) != 1) {
449 atomic_dec(&handle
->stream
->active
);
453 handle
->state
= UVC_HANDLE_ACTIVE
;
457 static void uvc_dismiss_privileges(struct uvc_fh
*handle
)
459 if (handle
->state
== UVC_HANDLE_ACTIVE
)
460 atomic_dec(&handle
->stream
->active
);
462 handle
->state
= UVC_HANDLE_PASSIVE
;
465 static int uvc_has_privileges(struct uvc_fh
*handle
)
467 return handle
->state
== UVC_HANDLE_ACTIVE
;
470 /* ------------------------------------------------------------------------
471 * V4L2 file operations
474 static int uvc_v4l2_open(struct file
*file
)
476 struct uvc_streaming
*stream
;
477 struct uvc_fh
*handle
;
480 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_open\n");
481 stream
= video_drvdata(file
);
483 if (stream
->dev
->state
& UVC_DEV_DISCONNECTED
)
486 ret
= usb_autopm_get_interface(stream
->dev
->intf
);
490 /* Create the device handle. */
491 handle
= kzalloc(sizeof *handle
, GFP_KERNEL
);
492 if (handle
== NULL
) {
493 usb_autopm_put_interface(stream
->dev
->intf
);
497 if (atomic_inc_return(&stream
->dev
->users
) == 1) {
498 ret
= uvc_status_start(stream
->dev
);
500 usb_autopm_put_interface(stream
->dev
->intf
);
501 atomic_dec(&stream
->dev
->users
);
507 handle
->chain
= stream
->chain
;
508 handle
->stream
= stream
;
509 handle
->state
= UVC_HANDLE_PASSIVE
;
510 file
->private_data
= handle
;
515 static int uvc_v4l2_release(struct file
*file
)
517 struct uvc_fh
*handle
= file
->private_data
;
518 struct uvc_streaming
*stream
= handle
->stream
;
520 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_release\n");
522 /* Only free resources if this is a privileged handle. */
523 if (uvc_has_privileges(handle
)) {
524 uvc_video_enable(stream
, 0);
525 uvc_free_buffers(&stream
->queue
);
528 /* Release the file handle. */
529 uvc_dismiss_privileges(handle
);
531 file
->private_data
= NULL
;
533 if (atomic_dec_return(&stream
->dev
->users
) == 0)
534 uvc_status_stop(stream
->dev
);
536 usb_autopm_put_interface(stream
->dev
->intf
);
540 static long uvc_v4l2_do_ioctl(struct file
*file
, unsigned int cmd
, void *arg
)
542 struct video_device
*vdev
= video_devdata(file
);
543 struct uvc_fh
*handle
= file
->private_data
;
544 struct uvc_video_chain
*chain
= handle
->chain
;
545 struct uvc_streaming
*stream
= handle
->stream
;
549 /* Query capabilities */
550 case VIDIOC_QUERYCAP
:
552 struct v4l2_capability
*cap
= arg
;
554 memset(cap
, 0, sizeof *cap
);
555 strlcpy(cap
->driver
, "uvcvideo", sizeof cap
->driver
);
556 strlcpy(cap
->card
, vdev
->name
, sizeof cap
->card
);
557 usb_make_path(stream
->dev
->udev
,
558 cap
->bus_info
, sizeof(cap
->bus_info
));
559 cap
->version
= LINUX_VERSION_CODE
;
560 if (stream
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
561 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
562 | V4L2_CAP_STREAMING
;
564 cap
->capabilities
= V4L2_CAP_VIDEO_OUTPUT
565 | V4L2_CAP_STREAMING
;
569 /* Get, Set & Query control */
570 case VIDIOC_QUERYCTRL
:
571 return uvc_query_v4l2_ctrl(chain
, arg
);
575 struct v4l2_control
*ctrl
= arg
;
576 struct v4l2_ext_control xctrl
;
578 memset(&xctrl
, 0, sizeof xctrl
);
581 ret
= uvc_ctrl_begin(chain
);
585 ret
= uvc_ctrl_get(chain
, &xctrl
);
586 uvc_ctrl_rollback(chain
);
588 ctrl
->value
= xctrl
.value
;
594 struct v4l2_control
*ctrl
= arg
;
595 struct v4l2_ext_control xctrl
;
597 memset(&xctrl
, 0, sizeof xctrl
);
599 xctrl
.value
= ctrl
->value
;
601 ret
= uvc_ctrl_begin(chain
);
605 ret
= uvc_ctrl_set(chain
, &xctrl
);
607 uvc_ctrl_rollback(chain
);
610 ret
= uvc_ctrl_commit(chain
);
612 ctrl
->value
= xctrl
.value
;
616 case VIDIOC_QUERYMENU
:
617 return uvc_query_v4l2_menu(chain
, arg
);
619 case VIDIOC_G_EXT_CTRLS
:
621 struct v4l2_ext_controls
*ctrls
= arg
;
622 struct v4l2_ext_control
*ctrl
= ctrls
->controls
;
625 ret
= uvc_ctrl_begin(chain
);
629 for (i
= 0; i
< ctrls
->count
; ++ctrl
, ++i
) {
630 ret
= uvc_ctrl_get(chain
, ctrl
);
632 uvc_ctrl_rollback(chain
);
633 ctrls
->error_idx
= i
;
637 ctrls
->error_idx
= 0;
638 ret
= uvc_ctrl_rollback(chain
);
642 case VIDIOC_S_EXT_CTRLS
:
643 case VIDIOC_TRY_EXT_CTRLS
:
645 struct v4l2_ext_controls
*ctrls
= arg
;
646 struct v4l2_ext_control
*ctrl
= ctrls
->controls
;
649 ret
= uvc_ctrl_begin(chain
);
653 for (i
= 0; i
< ctrls
->count
; ++ctrl
, ++i
) {
654 ret
= uvc_ctrl_set(chain
, ctrl
);
656 uvc_ctrl_rollback(chain
);
657 ctrls
->error_idx
= i
;
662 ctrls
->error_idx
= 0;
664 if (cmd
== VIDIOC_S_EXT_CTRLS
)
665 ret
= uvc_ctrl_commit(chain
);
667 ret
= uvc_ctrl_rollback(chain
);
671 /* Get, Set & Enum input */
672 case VIDIOC_ENUMINPUT
:
674 const struct uvc_entity
*selector
= chain
->selector
;
675 struct v4l2_input
*input
= arg
;
676 struct uvc_entity
*iterm
= NULL
;
677 u32 index
= input
->index
;
680 if (selector
== NULL
||
681 (chain
->dev
->quirks
& UVC_QUIRK_IGNORE_SELECTOR_UNIT
)) {
684 list_for_each_entry(iterm
, &chain
->entities
, chain
) {
685 if (UVC_ENTITY_IS_ITERM(iterm
))
689 } else if (pin
< selector
->bNrInPins
) {
690 pin
= selector
->baSourceID
[index
];
691 list_for_each_entry(iterm
, &chain
->entities
, chain
) {
692 if (!UVC_ENTITY_IS_ITERM(iterm
))
694 if (iterm
->id
== pin
)
699 if (iterm
== NULL
|| iterm
->id
!= pin
)
702 memset(input
, 0, sizeof *input
);
703 input
->index
= index
;
704 strlcpy(input
->name
, iterm
->name
, sizeof input
->name
);
705 if (UVC_ENTITY_TYPE(iterm
) == UVC_ITT_CAMERA
)
706 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
714 if (chain
->selector
== NULL
||
715 (chain
->dev
->quirks
& UVC_QUIRK_IGNORE_SELECTOR_UNIT
)) {
720 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
,
721 chain
->selector
->id
, chain
->dev
->intfnum
,
722 UVC_SU_INPUT_SELECT_CONTROL
, &input
, 1);
726 *(int *)arg
= input
- 1;
732 u32 input
= *(u32
*)arg
+ 1;
734 if ((ret
= uvc_acquire_privileges(handle
)) < 0)
737 if (chain
->selector
== NULL
||
738 (chain
->dev
->quirks
& UVC_QUIRK_IGNORE_SELECTOR_UNIT
)) {
744 if (input
== 0 || input
> chain
->selector
->bNrInPins
)
747 return uvc_query_ctrl(chain
->dev
, UVC_SET_CUR
,
748 chain
->selector
->id
, chain
->dev
->intfnum
,
749 UVC_SU_INPUT_SELECT_CONTROL
, &input
, 1);
752 /* Try, Get, Set & Enum format */
753 case VIDIOC_ENUM_FMT
:
755 struct v4l2_fmtdesc
*fmt
= arg
;
756 struct uvc_format
*format
;
757 enum v4l2_buf_type type
= fmt
->type
;
758 __u32 index
= fmt
->index
;
760 if (fmt
->type
!= stream
->type
||
761 fmt
->index
>= stream
->nformats
)
764 memset(fmt
, 0, sizeof(*fmt
));
768 format
= &stream
->format
[fmt
->index
];
770 if (format
->flags
& UVC_FMT_FLAG_COMPRESSED
)
771 fmt
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
772 strlcpy(fmt
->description
, format
->name
,
773 sizeof fmt
->description
);
774 fmt
->description
[sizeof fmt
->description
- 1] = 0;
775 fmt
->pixelformat
= format
->fcc
;
781 struct uvc_streaming_control probe
;
783 return uvc_v4l2_try_format(stream
, arg
, &probe
, NULL
, NULL
);
787 if ((ret
= uvc_acquire_privileges(handle
)) < 0)
790 return uvc_v4l2_set_format(stream
, arg
);
793 return uvc_v4l2_get_format(stream
, arg
);
795 /* Frame size enumeration */
796 case VIDIOC_ENUM_FRAMESIZES
:
798 struct v4l2_frmsizeenum
*fsize
= arg
;
799 struct uvc_format
*format
= NULL
;
800 struct uvc_frame
*frame
;
803 /* Look for the given pixel format */
804 for (i
= 0; i
< stream
->nformats
; i
++) {
805 if (stream
->format
[i
].fcc
==
806 fsize
->pixel_format
) {
807 format
= &stream
->format
[i
];
814 if (fsize
->index
>= format
->nframes
)
817 frame
= &format
->frame
[fsize
->index
];
818 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
819 fsize
->discrete
.width
= frame
->wWidth
;
820 fsize
->discrete
.height
= frame
->wHeight
;
824 /* Frame interval enumeration */
825 case VIDIOC_ENUM_FRAMEINTERVALS
:
827 struct v4l2_frmivalenum
*fival
= arg
;
828 struct uvc_format
*format
= NULL
;
829 struct uvc_frame
*frame
= NULL
;
832 /* Look for the given pixel format and frame size */
833 for (i
= 0; i
< stream
->nformats
; i
++) {
834 if (stream
->format
[i
].fcc
==
835 fival
->pixel_format
) {
836 format
= &stream
->format
[i
];
843 for (i
= 0; i
< format
->nframes
; i
++) {
844 if (format
->frame
[i
].wWidth
== fival
->width
&&
845 format
->frame
[i
].wHeight
== fival
->height
) {
846 frame
= &format
->frame
[i
];
853 if (frame
->bFrameIntervalType
) {
854 if (fival
->index
>= frame
->bFrameIntervalType
)
857 fival
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
858 fival
->discrete
.numerator
=
859 frame
->dwFrameInterval
[fival
->index
];
860 fival
->discrete
.denominator
= 10000000;
861 uvc_simplify_fraction(&fival
->discrete
.numerator
,
862 &fival
->discrete
.denominator
, 8, 333);
864 fival
->type
= V4L2_FRMIVAL_TYPE_STEPWISE
;
865 fival
->stepwise
.min
.numerator
=
866 frame
->dwFrameInterval
[0];
867 fival
->stepwise
.min
.denominator
= 10000000;
868 fival
->stepwise
.max
.numerator
=
869 frame
->dwFrameInterval
[1];
870 fival
->stepwise
.max
.denominator
= 10000000;
871 fival
->stepwise
.step
.numerator
=
872 frame
->dwFrameInterval
[2];
873 fival
->stepwise
.step
.denominator
= 10000000;
874 uvc_simplify_fraction(&fival
->stepwise
.min
.numerator
,
875 &fival
->stepwise
.min
.denominator
, 8, 333);
876 uvc_simplify_fraction(&fival
->stepwise
.max
.numerator
,
877 &fival
->stepwise
.max
.denominator
, 8, 333);
878 uvc_simplify_fraction(&fival
->stepwise
.step
.numerator
,
879 &fival
->stepwise
.step
.denominator
, 8, 333);
884 /* Get & Set streaming parameters */
886 return uvc_v4l2_get_streamparm(stream
, arg
);
889 if ((ret
= uvc_acquire_privileges(handle
)) < 0)
892 return uvc_v4l2_set_streamparm(stream
, arg
);
894 /* Cropping and scaling */
897 struct v4l2_cropcap
*ccap
= arg
;
899 if (ccap
->type
!= stream
->type
)
902 ccap
->bounds
.left
= 0;
903 ccap
->bounds
.top
= 0;
905 mutex_lock(&stream
->mutex
);
906 ccap
->bounds
.width
= stream
->cur_frame
->wWidth
;
907 ccap
->bounds
.height
= stream
->cur_frame
->wHeight
;
908 mutex_unlock(&stream
->mutex
);
910 ccap
->defrect
= ccap
->bounds
;
912 ccap
->pixelaspect
.numerator
= 1;
913 ccap
->pixelaspect
.denominator
= 1;
921 /* Buffers & streaming */
923 if ((ret
= uvc_acquire_privileges(handle
)) < 0)
926 mutex_lock(&stream
->mutex
);
927 ret
= uvc_alloc_buffers(&stream
->queue
, arg
);
928 mutex_unlock(&stream
->mutex
);
933 uvc_dismiss_privileges(handle
);
938 case VIDIOC_QUERYBUF
:
940 struct v4l2_buffer
*buf
= arg
;
942 if (!uvc_has_privileges(handle
))
945 return uvc_query_buffer(&stream
->queue
, buf
);
949 if (!uvc_has_privileges(handle
))
952 return uvc_queue_buffer(&stream
->queue
, arg
);
955 if (!uvc_has_privileges(handle
))
958 return uvc_dequeue_buffer(&stream
->queue
, arg
,
959 file
->f_flags
& O_NONBLOCK
);
961 case VIDIOC_STREAMON
:
965 if (*type
!= stream
->type
)
968 if (!uvc_has_privileges(handle
))
971 mutex_lock(&stream
->mutex
);
972 ret
= uvc_video_enable(stream
, 1);
973 mutex_unlock(&stream
->mutex
);
979 case VIDIOC_STREAMOFF
:
983 if (*type
!= stream
->type
)
986 if (!uvc_has_privileges(handle
))
989 return uvc_video_enable(stream
, 0);
992 /* Analog video standards make no sense for digital cameras. */
994 case VIDIOC_QUERYSTD
:
1000 case VIDIOC_ENUMAUDIO
:
1001 case VIDIOC_ENUMAUDOUT
:
1003 case VIDIOC_ENUMOUTPUT
:
1004 uvc_trace(UVC_TRACE_IOCTL
, "Unsupported ioctl 0x%08x\n", cmd
);
1007 case UVCIOC_CTRL_MAP
:
1008 return uvc_ioctl_ctrl_map(chain
, arg
);
1010 case UVCIOC_CTRL_QUERY
:
1011 return uvc_xu_ctrl_query(chain
, arg
);
1014 uvc_trace(UVC_TRACE_IOCTL
, "Unknown ioctl 0x%08x\n", cmd
);
1021 static long uvc_v4l2_ioctl(struct file
*file
,
1022 unsigned int cmd
, unsigned long arg
)
1024 if (uvc_trace_param
& UVC_TRACE_IOCTL
) {
1025 uvc_printk(KERN_DEBUG
, "uvc_v4l2_ioctl(");
1026 v4l_printk_ioctl(cmd
);
1030 return video_usercopy(file
, cmd
, arg
, uvc_v4l2_do_ioctl
);
1033 static ssize_t
uvc_v4l2_read(struct file
*file
, char __user
*data
,
1034 size_t count
, loff_t
*ppos
)
1036 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_read: not implemented.\n");
1040 static int uvc_v4l2_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1042 struct uvc_fh
*handle
= file
->private_data
;
1043 struct uvc_streaming
*stream
= handle
->stream
;
1045 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_mmap\n");
1047 return uvc_queue_mmap(&stream
->queue
, vma
);
1050 static unsigned int uvc_v4l2_poll(struct file
*file
, poll_table
*wait
)
1052 struct uvc_fh
*handle
= file
->private_data
;
1053 struct uvc_streaming
*stream
= handle
->stream
;
1055 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_poll\n");
1057 return uvc_queue_poll(&stream
->queue
, file
, wait
);
1061 static unsigned long uvc_v4l2_get_unmapped_area(struct file
*file
,
1062 unsigned long addr
, unsigned long len
, unsigned long pgoff
,
1063 unsigned long flags
)
1065 struct uvc_fh
*handle
= file
->private_data
;
1066 struct uvc_streaming
*stream
= handle
->stream
;
1068 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_get_unmapped_area\n");
1070 return uvc_queue_get_unmapped_area(&stream
->queue
, pgoff
);
1074 const struct v4l2_file_operations uvc_fops
= {
1075 .owner
= THIS_MODULE
,
1076 .open
= uvc_v4l2_open
,
1077 .release
= uvc_v4l2_release
,
1078 .unlocked_ioctl
= uvc_v4l2_ioctl
,
1079 .read
= uvc_v4l2_read
,
1080 .mmap
= uvc_v4l2_mmap
,
1081 .poll
= uvc_v4l2_poll
,
1083 .get_unmapped_area
= uvc_v4l2_get_unmapped_area
,