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/compat.h>
15 #include <linux/kernel.h>
16 #include <linux/version.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/usb.h>
21 #include <linux/videodev2.h>
22 #include <linux/vmalloc.h>
24 #include <linux/wait.h>
25 #include <linux/atomic.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-event.h>
30 #include <media/v4l2-ioctl.h>
34 /* ------------------------------------------------------------------------
37 static int uvc_ioctl_ctrl_map(struct uvc_video_chain
*chain
,
38 struct uvc_xu_control_mapping
*xmap
)
40 struct uvc_control_mapping
*map
;
44 map
= kzalloc(sizeof *map
, GFP_KERNEL
);
49 memcpy(map
->name
, xmap
->name
, sizeof map
->name
);
50 memcpy(map
->entity
, xmap
->entity
, sizeof map
->entity
);
51 map
->selector
= xmap
->selector
;
52 map
->size
= xmap
->size
;
53 map
->offset
= xmap
->offset
;
54 map
->v4l2_type
= xmap
->v4l2_type
;
55 map
->data_type
= xmap
->data_type
;
57 switch (xmap
->v4l2_type
) {
58 case V4L2_CTRL_TYPE_INTEGER
:
59 case V4L2_CTRL_TYPE_BOOLEAN
:
60 case V4L2_CTRL_TYPE_BUTTON
:
63 case V4L2_CTRL_TYPE_MENU
:
64 /* Prevent excessive memory consumption, as well as integer
67 if (xmap
->menu_count
== 0 ||
68 xmap
->menu_count
> UVC_MAX_CONTROL_MENU_ENTRIES
) {
73 size
= xmap
->menu_count
* sizeof(*map
->menu_info
);
74 map
->menu_info
= kmalloc(size
, GFP_KERNEL
);
75 if (map
->menu_info
== NULL
) {
80 if (copy_from_user(map
->menu_info
, xmap
->menu_info
, size
)) {
85 map
->menu_count
= xmap
->menu_count
;
89 uvc_trace(UVC_TRACE_CONTROL
, "Unsupported V4L2 control type "
90 "%u.\n", xmap
->v4l2_type
);
95 ret
= uvc_ctrl_add_mapping(chain
, map
);
98 kfree(map
->menu_info
);
104 /* ------------------------------------------------------------------------
109 * Find the frame interval closest to the requested frame interval for the
110 * given frame format and size. This should be done by the device as part of
111 * the Video Probe and Commit negotiation, but some hardware don't implement
114 static __u32
uvc_try_frame_interval(struct uvc_frame
*frame
, __u32 interval
)
118 if (frame
->bFrameIntervalType
) {
119 __u32 best
= -1, dist
;
121 for (i
= 0; i
< frame
->bFrameIntervalType
; ++i
) {
122 dist
= interval
> frame
->dwFrameInterval
[i
]
123 ? interval
- frame
->dwFrameInterval
[i
]
124 : frame
->dwFrameInterval
[i
] - interval
;
132 interval
= frame
->dwFrameInterval
[i
-1];
134 const __u32 min
= frame
->dwFrameInterval
[0];
135 const __u32 max
= frame
->dwFrameInterval
[1];
136 const __u32 step
= frame
->dwFrameInterval
[2];
138 interval
= min
+ (interval
- min
+ step
/2) / step
* step
;
146 static int uvc_v4l2_try_format(struct uvc_streaming
*stream
,
147 struct v4l2_format
*fmt
, struct uvc_streaming_control
*probe
,
148 struct uvc_format
**uvc_format
, struct uvc_frame
**uvc_frame
)
150 struct uvc_format
*format
= NULL
;
151 struct uvc_frame
*frame
= NULL
;
153 unsigned int d
, maxd
;
159 if (fmt
->type
!= stream
->type
)
162 fcc
= (__u8
*)&fmt
->fmt
.pix
.pixelformat
;
163 uvc_trace(UVC_TRACE_FORMAT
, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
164 fmt
->fmt
.pix
.pixelformat
,
165 fcc
[0], fcc
[1], fcc
[2], fcc
[3],
166 fmt
->fmt
.pix
.width
, fmt
->fmt
.pix
.height
);
168 /* Check if the hardware supports the requested format. */
169 for (i
= 0; i
< stream
->nformats
; ++i
) {
170 format
= &stream
->format
[i
];
171 if (format
->fcc
== fmt
->fmt
.pix
.pixelformat
)
175 if (format
== NULL
|| format
->fcc
!= fmt
->fmt
.pix
.pixelformat
) {
176 uvc_trace(UVC_TRACE_FORMAT
, "Unsupported format 0x%08x.\n",
177 fmt
->fmt
.pix
.pixelformat
);
181 /* Find the closest image size. The distance between image sizes is
182 * the size in pixels of the non-overlapping regions between the
183 * requested size and the frame-specified size.
185 rw
= fmt
->fmt
.pix
.width
;
186 rh
= fmt
->fmt
.pix
.height
;
187 maxd
= (unsigned int)-1;
189 for (i
= 0; i
< format
->nframes
; ++i
) {
190 __u16 w
= format
->frame
[i
].wWidth
;
191 __u16 h
= format
->frame
[i
].wHeight
;
193 d
= min(w
, rw
) * min(h
, rh
);
194 d
= w
*h
+ rw
*rh
- 2*d
;
197 frame
= &format
->frame
[i
];
205 uvc_trace(UVC_TRACE_FORMAT
, "Unsupported size %ux%u.\n",
206 fmt
->fmt
.pix
.width
, fmt
->fmt
.pix
.height
);
210 /* Use the default frame interval. */
211 interval
= frame
->dwDefaultFrameInterval
;
212 uvc_trace(UVC_TRACE_FORMAT
, "Using default frame interval %u.%u us "
213 "(%u.%u fps).\n", interval
/10, interval
%10, 10000000/interval
,
214 (100000000/interval
)%10);
216 /* Set the format index, frame index and frame interval. */
217 memset(probe
, 0, sizeof *probe
);
218 probe
->bmHint
= 1; /* dwFrameInterval */
219 probe
->bFormatIndex
= format
->index
;
220 probe
->bFrameIndex
= frame
->bFrameIndex
;
221 probe
->dwFrameInterval
= uvc_try_frame_interval(frame
, interval
);
222 /* Some webcams stall the probe control set request when the
223 * dwMaxVideoFrameSize field is set to zero. The UVC specification
224 * clearly states that the field is read-only from the host, so this
225 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
226 * the webcam to work around the problem.
228 * The workaround could probably be enabled for all webcams, so the
229 * quirk can be removed if needed. It's currently useful to detect
230 * webcam bugs and fix them before they hit the market (providing
231 * developers test their webcams with the Linux driver as well as with
232 * the Windows driver).
234 mutex_lock(&stream
->mutex
);
235 if (stream
->dev
->quirks
& UVC_QUIRK_PROBE_EXTRAFIELDS
)
236 probe
->dwMaxVideoFrameSize
=
237 stream
->ctrl
.dwMaxVideoFrameSize
;
239 /* Probe the device. */
240 ret
= uvc_probe_video(stream
, probe
);
241 mutex_unlock(&stream
->mutex
);
245 fmt
->fmt
.pix
.width
= frame
->wWidth
;
246 fmt
->fmt
.pix
.height
= frame
->wHeight
;
247 fmt
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
248 fmt
->fmt
.pix
.bytesperline
= format
->bpp
* frame
->wWidth
/ 8;
249 fmt
->fmt
.pix
.sizeimage
= probe
->dwMaxVideoFrameSize
;
250 fmt
->fmt
.pix
.colorspace
= format
->colorspace
;
251 fmt
->fmt
.pix
.priv
= 0;
253 if (uvc_format
!= NULL
)
254 *uvc_format
= format
;
255 if (uvc_frame
!= NULL
)
262 static int uvc_v4l2_get_format(struct uvc_streaming
*stream
,
263 struct v4l2_format
*fmt
)
265 struct uvc_format
*format
;
266 struct uvc_frame
*frame
;
269 if (fmt
->type
!= stream
->type
)
272 mutex_lock(&stream
->mutex
);
273 format
= stream
->cur_format
;
274 frame
= stream
->cur_frame
;
276 if (format
== NULL
|| frame
== NULL
) {
281 fmt
->fmt
.pix
.pixelformat
= format
->fcc
;
282 fmt
->fmt
.pix
.width
= frame
->wWidth
;
283 fmt
->fmt
.pix
.height
= frame
->wHeight
;
284 fmt
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
285 fmt
->fmt
.pix
.bytesperline
= format
->bpp
* frame
->wWidth
/ 8;
286 fmt
->fmt
.pix
.sizeimage
= stream
->ctrl
.dwMaxVideoFrameSize
;
287 fmt
->fmt
.pix
.colorspace
= format
->colorspace
;
288 fmt
->fmt
.pix
.priv
= 0;
291 mutex_unlock(&stream
->mutex
);
295 static int uvc_v4l2_set_format(struct uvc_streaming
*stream
,
296 struct v4l2_format
*fmt
)
298 struct uvc_streaming_control probe
;
299 struct uvc_format
*format
;
300 struct uvc_frame
*frame
;
303 if (fmt
->type
!= stream
->type
)
306 ret
= uvc_v4l2_try_format(stream
, fmt
, &probe
, &format
, &frame
);
310 mutex_lock(&stream
->mutex
);
312 if (uvc_queue_allocated(&stream
->queue
)) {
317 memcpy(&stream
->ctrl
, &probe
, sizeof probe
);
318 stream
->cur_format
= format
;
319 stream
->cur_frame
= frame
;
322 mutex_unlock(&stream
->mutex
);
326 static int uvc_v4l2_get_streamparm(struct uvc_streaming
*stream
,
327 struct v4l2_streamparm
*parm
)
329 uint32_t numerator
, denominator
;
331 if (parm
->type
!= stream
->type
)
334 mutex_lock(&stream
->mutex
);
335 numerator
= stream
->ctrl
.dwFrameInterval
;
336 mutex_unlock(&stream
->mutex
);
338 denominator
= 10000000;
339 uvc_simplify_fraction(&numerator
, &denominator
, 8, 333);
341 memset(parm
, 0, sizeof *parm
);
342 parm
->type
= stream
->type
;
344 if (stream
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
345 parm
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
346 parm
->parm
.capture
.capturemode
= 0;
347 parm
->parm
.capture
.timeperframe
.numerator
= numerator
;
348 parm
->parm
.capture
.timeperframe
.denominator
= denominator
;
349 parm
->parm
.capture
.extendedmode
= 0;
350 parm
->parm
.capture
.readbuffers
= 0;
352 parm
->parm
.output
.capability
= V4L2_CAP_TIMEPERFRAME
;
353 parm
->parm
.output
.outputmode
= 0;
354 parm
->parm
.output
.timeperframe
.numerator
= numerator
;
355 parm
->parm
.output
.timeperframe
.denominator
= denominator
;
361 static int uvc_v4l2_set_streamparm(struct uvc_streaming
*stream
,
362 struct v4l2_streamparm
*parm
)
364 struct uvc_streaming_control probe
;
365 struct v4l2_fract timeperframe
;
369 if (parm
->type
!= stream
->type
)
372 if (parm
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
373 timeperframe
= parm
->parm
.capture
.timeperframe
;
375 timeperframe
= parm
->parm
.output
.timeperframe
;
377 interval
= uvc_fraction_to_interval(timeperframe
.numerator
,
378 timeperframe
.denominator
);
379 uvc_trace(UVC_TRACE_FORMAT
, "Setting frame interval to %u/%u (%u).\n",
380 timeperframe
.numerator
, timeperframe
.denominator
, interval
);
382 mutex_lock(&stream
->mutex
);
384 if (uvc_queue_streaming(&stream
->queue
)) {
385 mutex_unlock(&stream
->mutex
);
389 memcpy(&probe
, &stream
->ctrl
, sizeof probe
);
390 probe
.dwFrameInterval
=
391 uvc_try_frame_interval(stream
->cur_frame
, interval
);
393 /* Probe the device with the new settings. */
394 ret
= uvc_probe_video(stream
, &probe
);
396 mutex_unlock(&stream
->mutex
);
400 memcpy(&stream
->ctrl
, &probe
, sizeof probe
);
401 mutex_unlock(&stream
->mutex
);
403 /* Return the actual frame period. */
404 timeperframe
.numerator
= probe
.dwFrameInterval
;
405 timeperframe
.denominator
= 10000000;
406 uvc_simplify_fraction(&timeperframe
.numerator
,
407 &timeperframe
.denominator
, 8, 333);
409 if (parm
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
410 parm
->parm
.capture
.timeperframe
= timeperframe
;
412 parm
->parm
.output
.timeperframe
= timeperframe
;
417 /* ------------------------------------------------------------------------
418 * Privilege management
422 * Privilege management is the multiple-open implementation basis. The current
423 * implementation is completely transparent for the end-user and doesn't
424 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
425 * Those ioctls enable finer control on the device (by making possible for a
426 * user to request exclusive access to a device), but are not mature yet.
427 * Switching to the V4L2 priority mechanism might be considered in the future
428 * if this situation changes.
430 * Each open instance of a UVC device can either be in a privileged or
431 * unprivileged state. Only a single instance can be in a privileged state at
432 * a given time. Trying to perform an operation that requires privileges will
433 * automatically acquire the required privileges if possible, or return -EBUSY
434 * otherwise. Privileges are dismissed when closing the instance or when
435 * freeing the video buffers using VIDIOC_REQBUFS.
437 * Operations that require privileges are:
444 static int uvc_acquire_privileges(struct uvc_fh
*handle
)
446 /* Always succeed if the handle is already privileged. */
447 if (handle
->state
== UVC_HANDLE_ACTIVE
)
450 /* Check if the device already has a privileged handle. */
451 if (atomic_inc_return(&handle
->stream
->active
) != 1) {
452 atomic_dec(&handle
->stream
->active
);
456 handle
->state
= UVC_HANDLE_ACTIVE
;
460 static void uvc_dismiss_privileges(struct uvc_fh
*handle
)
462 if (handle
->state
== UVC_HANDLE_ACTIVE
)
463 atomic_dec(&handle
->stream
->active
);
465 handle
->state
= UVC_HANDLE_PASSIVE
;
468 static int uvc_has_privileges(struct uvc_fh
*handle
)
470 return handle
->state
== UVC_HANDLE_ACTIVE
;
473 /* ------------------------------------------------------------------------
474 * V4L2 file operations
477 static int uvc_v4l2_open(struct file
*file
)
479 struct uvc_streaming
*stream
;
480 struct uvc_fh
*handle
;
483 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_open\n");
484 stream
= video_drvdata(file
);
486 if (stream
->dev
->state
& UVC_DEV_DISCONNECTED
)
489 ret
= usb_autopm_get_interface(stream
->dev
->intf
);
493 /* Create the device handle. */
494 handle
= kzalloc(sizeof *handle
, GFP_KERNEL
);
495 if (handle
== NULL
) {
496 usb_autopm_put_interface(stream
->dev
->intf
);
500 if (atomic_inc_return(&stream
->dev
->users
) == 1) {
501 ret
= uvc_status_start(stream
->dev
);
503 usb_autopm_put_interface(stream
->dev
->intf
);
504 atomic_dec(&stream
->dev
->users
);
510 v4l2_fh_init(&handle
->vfh
, stream
->vdev
);
511 v4l2_fh_add(&handle
->vfh
);
512 handle
->chain
= stream
->chain
;
513 handle
->stream
= stream
;
514 handle
->state
= UVC_HANDLE_PASSIVE
;
515 file
->private_data
= handle
;
520 static int uvc_v4l2_release(struct file
*file
)
522 struct uvc_fh
*handle
= file
->private_data
;
523 struct uvc_streaming
*stream
= handle
->stream
;
525 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_release\n");
527 /* Only free resources if this is a privileged handle. */
528 if (uvc_has_privileges(handle
)) {
529 uvc_video_enable(stream
, 0);
530 uvc_free_buffers(&stream
->queue
);
533 /* Release the file handle. */
534 uvc_dismiss_privileges(handle
);
535 v4l2_fh_del(&handle
->vfh
);
536 v4l2_fh_exit(&handle
->vfh
);
538 file
->private_data
= NULL
;
540 if (atomic_dec_return(&stream
->dev
->users
) == 0)
541 uvc_status_stop(stream
->dev
);
543 usb_autopm_put_interface(stream
->dev
->intf
);
547 static long uvc_v4l2_do_ioctl(struct file
*file
, unsigned int cmd
, void *arg
)
549 struct video_device
*vdev
= video_devdata(file
);
550 struct uvc_fh
*handle
= file
->private_data
;
551 struct uvc_video_chain
*chain
= handle
->chain
;
552 struct uvc_streaming
*stream
= handle
->stream
;
556 /* Query capabilities */
557 case VIDIOC_QUERYCAP
:
559 struct v4l2_capability
*cap
= arg
;
561 memset(cap
, 0, sizeof *cap
);
562 strlcpy(cap
->driver
, "uvcvideo", sizeof cap
->driver
);
563 strlcpy(cap
->card
, vdev
->name
, sizeof cap
->card
);
564 usb_make_path(stream
->dev
->udev
,
565 cap
->bus_info
, sizeof(cap
->bus_info
));
566 cap
->version
= LINUX_VERSION_CODE
;
567 if (stream
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
568 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
569 | V4L2_CAP_STREAMING
;
571 cap
->capabilities
= V4L2_CAP_VIDEO_OUTPUT
572 | V4L2_CAP_STREAMING
;
576 /* Get, Set & Query control */
577 case VIDIOC_QUERYCTRL
:
578 return uvc_query_v4l2_ctrl(chain
, arg
);
582 struct v4l2_control
*ctrl
= arg
;
583 struct v4l2_ext_control xctrl
;
585 memset(&xctrl
, 0, sizeof xctrl
);
588 ret
= uvc_ctrl_begin(chain
);
592 ret
= uvc_ctrl_get(chain
, &xctrl
);
593 uvc_ctrl_rollback(handle
);
595 ctrl
->value
= xctrl
.value
;
601 struct v4l2_control
*ctrl
= arg
;
602 struct v4l2_ext_control xctrl
;
604 memset(&xctrl
, 0, sizeof xctrl
);
606 xctrl
.value
= ctrl
->value
;
608 ret
= uvc_ctrl_begin(chain
);
612 ret
= uvc_ctrl_set(chain
, &xctrl
);
614 uvc_ctrl_rollback(handle
);
617 ret
= uvc_ctrl_commit(handle
, &xctrl
, 1);
619 ctrl
->value
= xctrl
.value
;
623 case VIDIOC_QUERYMENU
:
624 return uvc_query_v4l2_menu(chain
, arg
);
626 case VIDIOC_G_EXT_CTRLS
:
628 struct v4l2_ext_controls
*ctrls
= arg
;
629 struct v4l2_ext_control
*ctrl
= ctrls
->controls
;
632 ret
= uvc_ctrl_begin(chain
);
636 for (i
= 0; i
< ctrls
->count
; ++ctrl
, ++i
) {
637 ret
= uvc_ctrl_get(chain
, ctrl
);
639 uvc_ctrl_rollback(handle
);
640 ctrls
->error_idx
= i
;
644 ctrls
->error_idx
= 0;
645 ret
= uvc_ctrl_rollback(handle
);
649 case VIDIOC_S_EXT_CTRLS
:
650 case VIDIOC_TRY_EXT_CTRLS
:
652 struct v4l2_ext_controls
*ctrls
= arg
;
653 struct v4l2_ext_control
*ctrl
= ctrls
->controls
;
656 ret
= uvc_ctrl_begin(chain
);
660 for (i
= 0; i
< ctrls
->count
; ++ctrl
, ++i
) {
661 ret
= uvc_ctrl_set(chain
, ctrl
);
663 uvc_ctrl_rollback(handle
);
664 ctrls
->error_idx
= i
;
669 ctrls
->error_idx
= 0;
671 if (cmd
== VIDIOC_S_EXT_CTRLS
)
672 ret
= uvc_ctrl_commit(handle
,
673 ctrls
->controls
, ctrls
->count
);
675 ret
= uvc_ctrl_rollback(handle
);
679 /* Get, Set & Enum input */
680 case VIDIOC_ENUMINPUT
:
682 const struct uvc_entity
*selector
= chain
->selector
;
683 struct v4l2_input
*input
= arg
;
684 struct uvc_entity
*iterm
= NULL
;
685 u32 index
= input
->index
;
688 if (selector
== NULL
||
689 (chain
->dev
->quirks
& UVC_QUIRK_IGNORE_SELECTOR_UNIT
)) {
692 list_for_each_entry(iterm
, &chain
->entities
, chain
) {
693 if (UVC_ENTITY_IS_ITERM(iterm
))
697 } else if (index
< selector
->bNrInPins
) {
698 pin
= selector
->baSourceID
[index
];
699 list_for_each_entry(iterm
, &chain
->entities
, chain
) {
700 if (!UVC_ENTITY_IS_ITERM(iterm
))
702 if (iterm
->id
== pin
)
707 if (iterm
== NULL
|| iterm
->id
!= pin
)
710 memset(input
, 0, sizeof *input
);
711 input
->index
= index
;
712 strlcpy(input
->name
, iterm
->name
, sizeof input
->name
);
713 if (UVC_ENTITY_TYPE(iterm
) == UVC_ITT_CAMERA
)
714 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
722 if (chain
->selector
== NULL
||
723 (chain
->dev
->quirks
& UVC_QUIRK_IGNORE_SELECTOR_UNIT
)) {
728 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
,
729 chain
->selector
->id
, chain
->dev
->intfnum
,
730 UVC_SU_INPUT_SELECT_CONTROL
, &input
, 1);
734 *(int *)arg
= input
- 1;
740 u32 input
= *(u32
*)arg
+ 1;
742 if ((ret
= uvc_acquire_privileges(handle
)) < 0)
745 if (chain
->selector
== NULL
||
746 (chain
->dev
->quirks
& UVC_QUIRK_IGNORE_SELECTOR_UNIT
)) {
752 if (input
== 0 || input
> chain
->selector
->bNrInPins
)
755 return uvc_query_ctrl(chain
->dev
, UVC_SET_CUR
,
756 chain
->selector
->id
, chain
->dev
->intfnum
,
757 UVC_SU_INPUT_SELECT_CONTROL
, &input
, 1);
760 /* Try, Get, Set & Enum format */
761 case VIDIOC_ENUM_FMT
:
763 struct v4l2_fmtdesc
*fmt
= arg
;
764 struct uvc_format
*format
;
765 enum v4l2_buf_type type
= fmt
->type
;
766 __u32 index
= fmt
->index
;
768 if (fmt
->type
!= stream
->type
||
769 fmt
->index
>= stream
->nformats
)
772 memset(fmt
, 0, sizeof(*fmt
));
776 format
= &stream
->format
[fmt
->index
];
778 if (format
->flags
& UVC_FMT_FLAG_COMPRESSED
)
779 fmt
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
780 strlcpy(fmt
->description
, format
->name
,
781 sizeof fmt
->description
);
782 fmt
->description
[sizeof fmt
->description
- 1] = 0;
783 fmt
->pixelformat
= format
->fcc
;
789 struct uvc_streaming_control probe
;
791 return uvc_v4l2_try_format(stream
, arg
, &probe
, NULL
, NULL
);
795 if ((ret
= uvc_acquire_privileges(handle
)) < 0)
798 return uvc_v4l2_set_format(stream
, arg
);
801 return uvc_v4l2_get_format(stream
, arg
);
803 /* Frame size enumeration */
804 case VIDIOC_ENUM_FRAMESIZES
:
806 struct v4l2_frmsizeenum
*fsize
= arg
;
807 struct uvc_format
*format
= NULL
;
808 struct uvc_frame
*frame
;
811 /* Look for the given pixel format */
812 for (i
= 0; i
< stream
->nformats
; i
++) {
813 if (stream
->format
[i
].fcc
==
814 fsize
->pixel_format
) {
815 format
= &stream
->format
[i
];
822 if (fsize
->index
>= format
->nframes
)
825 frame
= &format
->frame
[fsize
->index
];
826 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
827 fsize
->discrete
.width
= frame
->wWidth
;
828 fsize
->discrete
.height
= frame
->wHeight
;
832 /* Frame interval enumeration */
833 case VIDIOC_ENUM_FRAMEINTERVALS
:
835 struct v4l2_frmivalenum
*fival
= arg
;
836 struct uvc_format
*format
= NULL
;
837 struct uvc_frame
*frame
= NULL
;
840 /* Look for the given pixel format and frame size */
841 for (i
= 0; i
< stream
->nformats
; i
++) {
842 if (stream
->format
[i
].fcc
==
843 fival
->pixel_format
) {
844 format
= &stream
->format
[i
];
851 for (i
= 0; i
< format
->nframes
; i
++) {
852 if (format
->frame
[i
].wWidth
== fival
->width
&&
853 format
->frame
[i
].wHeight
== fival
->height
) {
854 frame
= &format
->frame
[i
];
861 if (frame
->bFrameIntervalType
) {
862 if (fival
->index
>= frame
->bFrameIntervalType
)
865 fival
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
866 fival
->discrete
.numerator
=
867 frame
->dwFrameInterval
[fival
->index
];
868 fival
->discrete
.denominator
= 10000000;
869 uvc_simplify_fraction(&fival
->discrete
.numerator
,
870 &fival
->discrete
.denominator
, 8, 333);
872 fival
->type
= V4L2_FRMIVAL_TYPE_STEPWISE
;
873 fival
->stepwise
.min
.numerator
=
874 frame
->dwFrameInterval
[0];
875 fival
->stepwise
.min
.denominator
= 10000000;
876 fival
->stepwise
.max
.numerator
=
877 frame
->dwFrameInterval
[1];
878 fival
->stepwise
.max
.denominator
= 10000000;
879 fival
->stepwise
.step
.numerator
=
880 frame
->dwFrameInterval
[2];
881 fival
->stepwise
.step
.denominator
= 10000000;
882 uvc_simplify_fraction(&fival
->stepwise
.min
.numerator
,
883 &fival
->stepwise
.min
.denominator
, 8, 333);
884 uvc_simplify_fraction(&fival
->stepwise
.max
.numerator
,
885 &fival
->stepwise
.max
.denominator
, 8, 333);
886 uvc_simplify_fraction(&fival
->stepwise
.step
.numerator
,
887 &fival
->stepwise
.step
.denominator
, 8, 333);
892 /* Get & Set streaming parameters */
894 return uvc_v4l2_get_streamparm(stream
, arg
);
897 if ((ret
= uvc_acquire_privileges(handle
)) < 0)
900 return uvc_v4l2_set_streamparm(stream
, arg
);
902 /* Cropping and scaling */
905 struct v4l2_cropcap
*ccap
= arg
;
907 if (ccap
->type
!= stream
->type
)
910 ccap
->bounds
.left
= 0;
911 ccap
->bounds
.top
= 0;
913 mutex_lock(&stream
->mutex
);
914 ccap
->bounds
.width
= stream
->cur_frame
->wWidth
;
915 ccap
->bounds
.height
= stream
->cur_frame
->wHeight
;
916 mutex_unlock(&stream
->mutex
);
918 ccap
->defrect
= ccap
->bounds
;
920 ccap
->pixelaspect
.numerator
= 1;
921 ccap
->pixelaspect
.denominator
= 1;
929 /* Buffers & streaming */
931 if ((ret
= uvc_acquire_privileges(handle
)) < 0)
934 mutex_lock(&stream
->mutex
);
935 ret
= uvc_alloc_buffers(&stream
->queue
, arg
);
936 mutex_unlock(&stream
->mutex
);
941 uvc_dismiss_privileges(handle
);
946 case VIDIOC_QUERYBUF
:
948 struct v4l2_buffer
*buf
= arg
;
950 if (!uvc_has_privileges(handle
))
953 return uvc_query_buffer(&stream
->queue
, buf
);
957 if (!uvc_has_privileges(handle
))
960 return uvc_queue_buffer(&stream
->queue
, arg
);
963 if (!uvc_has_privileges(handle
))
966 return uvc_dequeue_buffer(&stream
->queue
, arg
,
967 file
->f_flags
& O_NONBLOCK
);
969 case VIDIOC_STREAMON
:
973 if (*type
!= stream
->type
)
976 if (!uvc_has_privileges(handle
))
979 mutex_lock(&stream
->mutex
);
980 ret
= uvc_video_enable(stream
, 1);
981 mutex_unlock(&stream
->mutex
);
987 case VIDIOC_STREAMOFF
:
991 if (*type
!= stream
->type
)
994 if (!uvc_has_privileges(handle
))
997 return uvc_video_enable(stream
, 0);
1000 case VIDIOC_SUBSCRIBE_EVENT
:
1002 struct v4l2_event_subscription
*sub
= arg
;
1004 switch (sub
->type
) {
1005 case V4L2_EVENT_CTRL
:
1006 return v4l2_event_subscribe(&handle
->vfh
, sub
, 0,
1007 &uvc_ctrl_sub_ev_ops
);
1013 case VIDIOC_UNSUBSCRIBE_EVENT
:
1014 return v4l2_event_unsubscribe(&handle
->vfh
, arg
);
1016 case VIDIOC_DQEVENT
:
1017 return v4l2_event_dequeue(&handle
->vfh
, arg
,
1018 file
->f_flags
& O_NONBLOCK
);
1020 /* Analog video standards make no sense for digital cameras. */
1021 case VIDIOC_ENUMSTD
:
1022 case VIDIOC_QUERYSTD
:
1026 case VIDIOC_OVERLAY
:
1028 case VIDIOC_ENUMAUDIO
:
1029 case VIDIOC_ENUMAUDOUT
:
1031 case VIDIOC_ENUMOUTPUT
:
1032 uvc_trace(UVC_TRACE_IOCTL
, "Unsupported ioctl 0x%08x\n", cmd
);
1035 case UVCIOC_CTRL_MAP
:
1036 return uvc_ioctl_ctrl_map(chain
, arg
);
1038 case UVCIOC_CTRL_QUERY
:
1039 return uvc_xu_ctrl_query(chain
, arg
);
1042 uvc_trace(UVC_TRACE_IOCTL
, "Unknown ioctl 0x%08x\n", cmd
);
1049 static long uvc_v4l2_ioctl(struct file
*file
,
1050 unsigned int cmd
, unsigned long arg
)
1052 if (uvc_trace_param
& UVC_TRACE_IOCTL
) {
1053 uvc_printk(KERN_DEBUG
, "uvc_v4l2_ioctl(");
1054 v4l_printk_ioctl(NULL
, cmd
);
1058 return video_usercopy(file
, cmd
, arg
, uvc_v4l2_do_ioctl
);
1061 #ifdef CONFIG_COMPAT
1062 struct uvc_xu_control_mapping32
{
1073 compat_caddr_t menu_info
;
1079 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping
*kp
,
1080 const struct uvc_xu_control_mapping32 __user
*up
)
1082 struct uvc_menu_info __user
*umenus
;
1083 struct uvc_menu_info __user
*kmenus
;
1086 if (!access_ok(VERIFY_READ
, up
, sizeof(*up
)) ||
1087 __copy_from_user(kp
, up
, offsetof(typeof(*up
), menu_info
)) ||
1088 __get_user(kp
->menu_count
, &up
->menu_count
))
1091 memset(kp
->reserved
, 0, sizeof(kp
->reserved
));
1093 if (kp
->menu_count
== 0) {
1094 kp
->menu_info
= NULL
;
1098 if (__get_user(p
, &up
->menu_info
))
1100 umenus
= compat_ptr(p
);
1101 if (!access_ok(VERIFY_READ
, umenus
, kp
->menu_count
* sizeof(*umenus
)))
1104 kmenus
= compat_alloc_user_space(kp
->menu_count
* sizeof(*kmenus
));
1107 kp
->menu_info
= kmenus
;
1109 if (copy_in_user(kmenus
, umenus
, kp
->menu_count
* sizeof(*umenus
)))
1115 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping
*kp
,
1116 struct uvc_xu_control_mapping32 __user
*up
)
1118 struct uvc_menu_info __user
*umenus
;
1119 struct uvc_menu_info __user
*kmenus
= kp
->menu_info
;
1122 if (!access_ok(VERIFY_WRITE
, up
, sizeof(*up
)) ||
1123 __copy_to_user(up
, kp
, offsetof(typeof(*up
), menu_info
)) ||
1124 __put_user(kp
->menu_count
, &up
->menu_count
))
1127 if (__clear_user(up
->reserved
, sizeof(up
->reserved
)))
1130 if (kp
->menu_count
== 0)
1133 if (get_user(p
, &up
->menu_info
))
1135 umenus
= compat_ptr(p
);
1137 if (copy_in_user(umenus
, kmenus
, kp
->menu_count
* sizeof(*umenus
)))
1143 struct uvc_xu_control_query32
{
1148 compat_caddr_t data
;
1151 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query
*kp
,
1152 const struct uvc_xu_control_query32 __user
*up
)
1158 if (!access_ok(VERIFY_READ
, up
, sizeof(*up
)) ||
1159 __copy_from_user(kp
, up
, offsetof(typeof(*up
), data
)))
1162 if (kp
->size
== 0) {
1167 if (__get_user(p
, &up
->data
))
1169 udata
= compat_ptr(p
);
1170 if (!access_ok(VERIFY_READ
, udata
, kp
->size
))
1173 kdata
= compat_alloc_user_space(kp
->size
);
1178 if (copy_in_user(kdata
, udata
, kp
->size
))
1184 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query
*kp
,
1185 struct uvc_xu_control_query32 __user
*up
)
1188 u8 __user
*kdata
= kp
->data
;
1191 if (!access_ok(VERIFY_WRITE
, up
, sizeof(*up
)) ||
1192 __copy_to_user(up
, kp
, offsetof(typeof(*up
), data
)))
1198 if (get_user(p
, &up
->data
))
1200 udata
= compat_ptr(p
);
1201 if (!access_ok(VERIFY_READ
, udata
, kp
->size
))
1204 if (copy_in_user(udata
, kdata
, kp
->size
))
1210 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32)
1211 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32)
1213 static long uvc_v4l2_compat_ioctl32(struct file
*file
,
1214 unsigned int cmd
, unsigned long arg
)
1217 struct uvc_xu_control_mapping xmap
;
1218 struct uvc_xu_control_query xqry
;
1220 void __user
*up
= compat_ptr(arg
);
1221 mm_segment_t old_fs
;
1225 case UVCIOC_CTRL_MAP32
:
1226 cmd
= UVCIOC_CTRL_MAP
;
1227 ret
= uvc_v4l2_get_xu_mapping(&karg
.xmap
, up
);
1230 case UVCIOC_CTRL_QUERY32
:
1231 cmd
= UVCIOC_CTRL_QUERY
;
1232 ret
= uvc_v4l2_get_xu_query(&karg
.xqry
, up
);
1236 return -ENOIOCTLCMD
;
1241 ret
= uvc_v4l2_ioctl(file
, cmd
, (unsigned long)&karg
);
1248 case UVCIOC_CTRL_MAP
:
1249 ret
= uvc_v4l2_put_xu_mapping(&karg
.xmap
, up
);
1252 case UVCIOC_CTRL_QUERY
:
1253 ret
= uvc_v4l2_put_xu_query(&karg
.xqry
, up
);
1261 static ssize_t
uvc_v4l2_read(struct file
*file
, char __user
*data
,
1262 size_t count
, loff_t
*ppos
)
1264 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_read: not implemented.\n");
1268 static int uvc_v4l2_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1270 struct uvc_fh
*handle
= file
->private_data
;
1271 struct uvc_streaming
*stream
= handle
->stream
;
1273 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_mmap\n");
1275 return uvc_queue_mmap(&stream
->queue
, vma
);
1278 static unsigned int uvc_v4l2_poll(struct file
*file
, poll_table
*wait
)
1280 struct uvc_fh
*handle
= file
->private_data
;
1281 struct uvc_streaming
*stream
= handle
->stream
;
1283 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_poll\n");
1285 return uvc_queue_poll(&stream
->queue
, file
, wait
);
1289 static unsigned long uvc_v4l2_get_unmapped_area(struct file
*file
,
1290 unsigned long addr
, unsigned long len
, unsigned long pgoff
,
1291 unsigned long flags
)
1293 struct uvc_fh
*handle
= file
->private_data
;
1294 struct uvc_streaming
*stream
= handle
->stream
;
1296 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_get_unmapped_area\n");
1298 return uvc_queue_get_unmapped_area(&stream
->queue
, pgoff
);
1302 const struct v4l2_file_operations uvc_fops
= {
1303 .owner
= THIS_MODULE
,
1304 .open
= uvc_v4l2_open
,
1305 .release
= uvc_v4l2_release
,
1306 .unlocked_ioctl
= uvc_v4l2_ioctl
,
1307 #ifdef CONFIG_COMPAT
1308 .compat_ioctl32
= uvc_v4l2_compat_ioctl32
,
1310 .read
= uvc_v4l2_read
,
1311 .mmap
= uvc_v4l2_mmap
,
1312 .poll
= uvc_v4l2_poll
,
1314 .get_unmapped_area
= uvc_v4l2_get_unmapped_area
,