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 /* ------------------------------------------------------------------------
34 static int uvc_ioctl_ctrl_map(struct uvc_xu_control_mapping
*xmap
, int old
)
36 struct uvc_control_mapping
*map
;
40 map
= kzalloc(sizeof *map
, GFP_KERNEL
);
45 memcpy(map
->name
, xmap
->name
, sizeof map
->name
);
46 memcpy(map
->entity
, xmap
->entity
, sizeof map
->entity
);
47 map
->selector
= xmap
->selector
;
48 map
->size
= xmap
->size
;
49 map
->offset
= xmap
->offset
;
50 map
->v4l2_type
= xmap
->v4l2_type
;
51 map
->data_type
= xmap
->data_type
;
53 switch (xmap
->v4l2_type
) {
54 case V4L2_CTRL_TYPE_INTEGER
:
55 case V4L2_CTRL_TYPE_BOOLEAN
:
56 case V4L2_CTRL_TYPE_BUTTON
:
59 case V4L2_CTRL_TYPE_MENU
:
65 size
= xmap
->menu_count
* sizeof(*map
->menu_info
);
66 map
->menu_info
= kmalloc(size
, GFP_KERNEL
);
67 if (map
->menu_info
== NULL
) {
72 if (copy_from_user(map
->menu_info
, xmap
->menu_info
, size
)) {
77 map
->menu_count
= xmap
->menu_count
;
85 ret
= uvc_ctrl_add_mapping(map
);
89 kfree(map
->menu_info
);
96 /* ------------------------------------------------------------------------
101 * Mapping V4L2 controls to UVC controls can be straighforward if done well.
102 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
103 * must be grouped (for instance the Red Balance, Blue Balance and Do White
104 * Balance V4L2 controls use the White Balance Component UVC control) or
105 * otherwise translated. The approach we take here is to use a translation
106 * table for the controls that can be mapped directly, and handle the others
109 static int uvc_v4l2_query_menu(struct uvc_video_chain
*chain
,
110 struct v4l2_querymenu
*query_menu
)
112 struct uvc_menu_info
*menu_info
;
113 struct uvc_control_mapping
*mapping
;
114 struct uvc_control
*ctrl
;
115 u32 index
= query_menu
->index
;
116 u32 id
= query_menu
->id
;
118 ctrl
= uvc_find_control(chain
, query_menu
->id
, &mapping
);
119 if (ctrl
== NULL
|| mapping
->v4l2_type
!= V4L2_CTRL_TYPE_MENU
)
122 if (query_menu
->index
>= mapping
->menu_count
)
125 memset(query_menu
, 0, sizeof(*query_menu
));
127 query_menu
->index
= index
;
129 menu_info
= &mapping
->menu_info
[query_menu
->index
];
130 strlcpy(query_menu
->name
, menu_info
->name
, sizeof query_menu
->name
);
135 * Find the frame interval closest to the requested frame interval for the
136 * given frame format and size. This should be done by the device as part of
137 * the Video Probe and Commit negotiation, but some hardware don't implement
140 static __u32
uvc_try_frame_interval(struct uvc_frame
*frame
, __u32 interval
)
144 if (frame
->bFrameIntervalType
) {
145 __u32 best
= -1, dist
;
147 for (i
= 0; i
< frame
->bFrameIntervalType
; ++i
) {
148 dist
= interval
> frame
->dwFrameInterval
[i
]
149 ? interval
- frame
->dwFrameInterval
[i
]
150 : frame
->dwFrameInterval
[i
] - interval
;
158 interval
= frame
->dwFrameInterval
[i
-1];
160 const __u32 min
= frame
->dwFrameInterval
[0];
161 const __u32 max
= frame
->dwFrameInterval
[1];
162 const __u32 step
= frame
->dwFrameInterval
[2];
164 interval
= min
+ (interval
- min
+ step
/2) / step
* step
;
172 static int uvc_v4l2_try_format(struct uvc_streaming
*stream
,
173 struct v4l2_format
*fmt
, struct uvc_streaming_control
*probe
,
174 struct uvc_format
**uvc_format
, struct uvc_frame
**uvc_frame
)
176 struct uvc_format
*format
= NULL
;
177 struct uvc_frame
*frame
= NULL
;
179 unsigned int d
, maxd
;
185 if (fmt
->type
!= stream
->type
)
188 fcc
= (__u8
*)&fmt
->fmt
.pix
.pixelformat
;
189 uvc_trace(UVC_TRACE_FORMAT
, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n",
190 fmt
->fmt
.pix
.pixelformat
,
191 fcc
[0], fcc
[1], fcc
[2], fcc
[3],
192 fmt
->fmt
.pix
.width
, fmt
->fmt
.pix
.height
);
194 /* Check if the hardware supports the requested format. */
195 for (i
= 0; i
< stream
->nformats
; ++i
) {
196 format
= &stream
->format
[i
];
197 if (format
->fcc
== fmt
->fmt
.pix
.pixelformat
)
201 if (format
== NULL
|| format
->fcc
!= fmt
->fmt
.pix
.pixelformat
) {
202 uvc_trace(UVC_TRACE_FORMAT
, "Unsupported format 0x%08x.\n",
203 fmt
->fmt
.pix
.pixelformat
);
207 /* Find the closest image size. The distance between image sizes is
208 * the size in pixels of the non-overlapping regions between the
209 * requested size and the frame-specified size.
211 rw
= fmt
->fmt
.pix
.width
;
212 rh
= fmt
->fmt
.pix
.height
;
213 maxd
= (unsigned int)-1;
215 for (i
= 0; i
< format
->nframes
; ++i
) {
216 __u16 w
= format
->frame
[i
].wWidth
;
217 __u16 h
= format
->frame
[i
].wHeight
;
219 d
= min(w
, rw
) * min(h
, rh
);
220 d
= w
*h
+ rw
*rh
- 2*d
;
223 frame
= &format
->frame
[i
];
231 uvc_trace(UVC_TRACE_FORMAT
, "Unsupported size %ux%u.\n",
232 fmt
->fmt
.pix
.width
, fmt
->fmt
.pix
.height
);
236 /* Use the default frame interval. */
237 interval
= frame
->dwDefaultFrameInterval
;
238 uvc_trace(UVC_TRACE_FORMAT
, "Using default frame interval %u.%u us "
239 "(%u.%u fps).\n", interval
/10, interval
%10, 10000000/interval
,
240 (100000000/interval
)%10);
242 /* Set the format index, frame index and frame interval. */
243 memset(probe
, 0, sizeof *probe
);
244 probe
->bmHint
= 1; /* dwFrameInterval */
245 probe
->bFormatIndex
= format
->index
;
246 probe
->bFrameIndex
= frame
->bFrameIndex
;
247 probe
->dwFrameInterval
= uvc_try_frame_interval(frame
, interval
);
248 /* Some webcams stall the probe control set request when the
249 * dwMaxVideoFrameSize field is set to zero. The UVC specification
250 * clearly states that the field is read-only from the host, so this
251 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by
252 * the webcam to work around the problem.
254 * The workaround could probably be enabled for all webcams, so the
255 * quirk can be removed if needed. It's currently useful to detect
256 * webcam bugs and fix them before they hit the market (providing
257 * developers test their webcams with the Linux driver as well as with
258 * the Windows driver).
260 if (stream
->dev
->quirks
& UVC_QUIRK_PROBE_EXTRAFIELDS
)
261 probe
->dwMaxVideoFrameSize
=
262 stream
->ctrl
.dwMaxVideoFrameSize
;
264 /* Probe the device. */
265 ret
= uvc_probe_video(stream
, probe
);
269 fmt
->fmt
.pix
.width
= frame
->wWidth
;
270 fmt
->fmt
.pix
.height
= frame
->wHeight
;
271 fmt
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
272 fmt
->fmt
.pix
.bytesperline
= format
->bpp
* frame
->wWidth
/ 8;
273 fmt
->fmt
.pix
.sizeimage
= probe
->dwMaxVideoFrameSize
;
274 fmt
->fmt
.pix
.colorspace
= format
->colorspace
;
275 fmt
->fmt
.pix
.priv
= 0;
277 if (uvc_format
!= NULL
)
278 *uvc_format
= format
;
279 if (uvc_frame
!= NULL
)
286 static int uvc_v4l2_get_format(struct uvc_streaming
*stream
,
287 struct v4l2_format
*fmt
)
289 struct uvc_format
*format
= stream
->cur_format
;
290 struct uvc_frame
*frame
= stream
->cur_frame
;
292 if (fmt
->type
!= stream
->type
)
295 if (format
== NULL
|| frame
== NULL
)
298 fmt
->fmt
.pix
.pixelformat
= format
->fcc
;
299 fmt
->fmt
.pix
.width
= frame
->wWidth
;
300 fmt
->fmt
.pix
.height
= frame
->wHeight
;
301 fmt
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
302 fmt
->fmt
.pix
.bytesperline
= format
->bpp
* frame
->wWidth
/ 8;
303 fmt
->fmt
.pix
.sizeimage
= stream
->ctrl
.dwMaxVideoFrameSize
;
304 fmt
->fmt
.pix
.colorspace
= format
->colorspace
;
305 fmt
->fmt
.pix
.priv
= 0;
310 static int uvc_v4l2_set_format(struct uvc_streaming
*stream
,
311 struct v4l2_format
*fmt
)
313 struct uvc_streaming_control probe
;
314 struct uvc_format
*format
;
315 struct uvc_frame
*frame
;
318 if (fmt
->type
!= stream
->type
)
321 if (uvc_queue_allocated(&stream
->queue
))
324 ret
= uvc_v4l2_try_format(stream
, fmt
, &probe
, &format
, &frame
);
328 memcpy(&stream
->ctrl
, &probe
, sizeof probe
);
329 stream
->cur_format
= format
;
330 stream
->cur_frame
= frame
;
335 static int uvc_v4l2_get_streamparm(struct uvc_streaming
*stream
,
336 struct v4l2_streamparm
*parm
)
338 uint32_t numerator
, denominator
;
340 if (parm
->type
!= stream
->type
)
343 numerator
= stream
->ctrl
.dwFrameInterval
;
344 denominator
= 10000000;
345 uvc_simplify_fraction(&numerator
, &denominator
, 8, 333);
347 memset(parm
, 0, sizeof *parm
);
348 parm
->type
= stream
->type
;
350 if (stream
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
351 parm
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
352 parm
->parm
.capture
.capturemode
= 0;
353 parm
->parm
.capture
.timeperframe
.numerator
= numerator
;
354 parm
->parm
.capture
.timeperframe
.denominator
= denominator
;
355 parm
->parm
.capture
.extendedmode
= 0;
356 parm
->parm
.capture
.readbuffers
= 0;
358 parm
->parm
.output
.capability
= V4L2_CAP_TIMEPERFRAME
;
359 parm
->parm
.output
.outputmode
= 0;
360 parm
->parm
.output
.timeperframe
.numerator
= numerator
;
361 parm
->parm
.output
.timeperframe
.denominator
= denominator
;
367 static int uvc_v4l2_set_streamparm(struct uvc_streaming
*stream
,
368 struct v4l2_streamparm
*parm
)
370 struct uvc_frame
*frame
= stream
->cur_frame
;
371 struct uvc_streaming_control probe
;
372 struct v4l2_fract timeperframe
;
376 if (parm
->type
!= stream
->type
)
379 if (uvc_queue_streaming(&stream
->queue
))
382 if (parm
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
383 timeperframe
= parm
->parm
.capture
.timeperframe
;
385 timeperframe
= parm
->parm
.output
.timeperframe
;
387 memcpy(&probe
, &stream
->ctrl
, sizeof probe
);
388 interval
= uvc_fraction_to_interval(timeperframe
.numerator
,
389 timeperframe
.denominator
);
391 uvc_trace(UVC_TRACE_FORMAT
, "Setting frame interval to %u/%u (%u).\n",
392 timeperframe
.numerator
, timeperframe
.denominator
, interval
);
393 probe
.dwFrameInterval
= uvc_try_frame_interval(frame
, interval
);
395 /* Probe the device with the new settings. */
396 ret
= uvc_probe_video(stream
, &probe
);
400 memcpy(&stream
->ctrl
, &probe
, sizeof probe
);
402 /* Return the actual frame period. */
403 timeperframe
.numerator
= probe
.dwFrameInterval
;
404 timeperframe
.denominator
= 10000000;
405 uvc_simplify_fraction(&timeperframe
.numerator
,
406 &timeperframe
.denominator
, 8, 333);
408 if (parm
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
409 parm
->parm
.capture
.timeperframe
= timeperframe
;
411 parm
->parm
.output
.timeperframe
= timeperframe
;
416 /* ------------------------------------------------------------------------
417 * Privilege management
421 * Privilege management is the multiple-open implementation basis. The current
422 * implementation is completely transparent for the end-user and doesn't
423 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls.
424 * Those ioctls enable finer control on the device (by making possible for a
425 * user to request exclusive access to a device), but are not mature yet.
426 * Switching to the V4L2 priority mechanism might be considered in the future
427 * if this situation changes.
429 * Each open instance of a UVC device can either be in a privileged or
430 * unprivileged state. Only a single instance can be in a privileged state at
431 * a given time. Trying to perform an operation that requires privileges will
432 * automatically acquire the required privileges if possible, or return -EBUSY
433 * otherwise. Privileges are dismissed when closing the instance or when
434 * freeing the video buffers using VIDIOC_REQBUFS.
436 * Operations that require privileges are:
443 static int uvc_acquire_privileges(struct uvc_fh
*handle
)
445 /* Always succeed if the handle is already privileged. */
446 if (handle
->state
== UVC_HANDLE_ACTIVE
)
449 /* Check if the device already has a privileged handle. */
450 if (atomic_inc_return(&handle
->stream
->active
) != 1) {
451 atomic_dec(&handle
->stream
->active
);
455 handle
->state
= UVC_HANDLE_ACTIVE
;
459 static void uvc_dismiss_privileges(struct uvc_fh
*handle
)
461 if (handle
->state
== UVC_HANDLE_ACTIVE
)
462 atomic_dec(&handle
->stream
->active
);
464 handle
->state
= UVC_HANDLE_PASSIVE
;
467 static int uvc_has_privileges(struct uvc_fh
*handle
)
469 return handle
->state
== UVC_HANDLE_ACTIVE
;
472 /* ------------------------------------------------------------------------
473 * V4L2 file operations
476 static int uvc_v4l2_open(struct file
*file
)
478 struct uvc_streaming
*stream
;
479 struct uvc_fh
*handle
;
482 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_open\n");
483 stream
= video_drvdata(file
);
485 if (stream
->dev
->state
& UVC_DEV_DISCONNECTED
)
488 ret
= usb_autopm_get_interface(stream
->dev
->intf
);
492 /* Create the device handle. */
493 handle
= kzalloc(sizeof *handle
, GFP_KERNEL
);
494 if (handle
== NULL
) {
495 usb_autopm_put_interface(stream
->dev
->intf
);
499 if (atomic_inc_return(&stream
->dev
->users
) == 1) {
500 ret
= uvc_status_start(stream
->dev
);
502 usb_autopm_put_interface(stream
->dev
->intf
);
503 atomic_dec(&stream
->dev
->users
);
509 handle
->chain
= stream
->chain
;
510 handle
->stream
= stream
;
511 handle
->state
= UVC_HANDLE_PASSIVE
;
512 file
->private_data
= handle
;
517 static int uvc_v4l2_release(struct file
*file
)
519 struct uvc_fh
*handle
= file
->private_data
;
520 struct uvc_streaming
*stream
= handle
->stream
;
522 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_release\n");
524 /* Only free resources if this is a privileged handle. */
525 if (uvc_has_privileges(handle
)) {
526 uvc_video_enable(stream
, 0);
528 mutex_lock(&stream
->queue
.mutex
);
529 if (uvc_free_buffers(&stream
->queue
) < 0)
530 uvc_printk(KERN_ERR
, "uvc_v4l2_release: Unable to "
532 mutex_unlock(&stream
->queue
.mutex
);
535 /* Release the file handle. */
536 uvc_dismiss_privileges(handle
);
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
= DRIVER_VERSION_NUMBER
;
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(chain
);
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(chain
);
617 ret
= uvc_ctrl_commit(chain
);
619 ctrl
->value
= xctrl
.value
;
623 case VIDIOC_QUERYMENU
:
624 return uvc_v4l2_query_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(chain
);
640 ctrls
->error_idx
= i
;
644 ctrls
->error_idx
= 0;
645 ret
= uvc_ctrl_rollback(chain
);
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(chain
);
664 ctrls
->error_idx
= i
;
669 ctrls
->error_idx
= 0;
671 if (cmd
== VIDIOC_S_EXT_CTRLS
)
672 ret
= uvc_ctrl_commit(chain
);
674 ret
= uvc_ctrl_rollback(chain
);
678 /* Get, Set & Enum input */
679 case VIDIOC_ENUMINPUT
:
681 const struct uvc_entity
*selector
= chain
->selector
;
682 struct v4l2_input
*input
= arg
;
683 struct uvc_entity
*iterm
= NULL
;
684 u32 index
= input
->index
;
687 if (selector
== NULL
||
688 (chain
->dev
->quirks
& UVC_QUIRK_IGNORE_SELECTOR_UNIT
)) {
691 list_for_each_entry(iterm
, &chain
->entities
, chain
) {
692 if (UVC_ENTITY_IS_ITERM(iterm
))
696 } else if (pin
< selector
->bNrInPins
) {
697 pin
= selector
->baSourceID
[index
];
698 list_for_each_entry(iterm
, &chain
->entities
, chain
) {
699 if (!UVC_ENTITY_IS_ITERM(iterm
))
701 if (iterm
->id
== pin
)
706 if (iterm
== NULL
|| iterm
->id
!= pin
)
709 memset(input
, 0, sizeof *input
);
710 input
->index
= index
;
711 strlcpy(input
->name
, iterm
->name
, sizeof input
->name
);
712 if (UVC_ENTITY_TYPE(iterm
) == UVC_ITT_CAMERA
)
713 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
721 if (chain
->selector
== NULL
||
722 (chain
->dev
->quirks
& UVC_QUIRK_IGNORE_SELECTOR_UNIT
)) {
727 ret
= uvc_query_ctrl(chain
->dev
, UVC_GET_CUR
,
728 chain
->selector
->id
, chain
->dev
->intfnum
,
729 UVC_SU_INPUT_SELECT_CONTROL
, &input
, 1);
733 *(int *)arg
= input
- 1;
739 u32 input
= *(u32
*)arg
+ 1;
741 if ((ret
= uvc_acquire_privileges(handle
)) < 0)
744 if (chain
->selector
== NULL
||
745 (chain
->dev
->quirks
& UVC_QUIRK_IGNORE_SELECTOR_UNIT
)) {
751 if (input
== 0 || input
> chain
->selector
->bNrInPins
)
754 return uvc_query_ctrl(chain
->dev
, UVC_SET_CUR
,
755 chain
->selector
->id
, chain
->dev
->intfnum
,
756 UVC_SU_INPUT_SELECT_CONTROL
, &input
, 1);
759 /* Try, Get, Set & Enum format */
760 case VIDIOC_ENUM_FMT
:
762 struct v4l2_fmtdesc
*fmt
= arg
;
763 struct uvc_format
*format
;
764 enum v4l2_buf_type type
= fmt
->type
;
765 __u32 index
= fmt
->index
;
767 if (fmt
->type
!= stream
->type
||
768 fmt
->index
>= stream
->nformats
)
771 memset(fmt
, 0, sizeof(*fmt
));
775 format
= &stream
->format
[fmt
->index
];
777 if (format
->flags
& UVC_FMT_FLAG_COMPRESSED
)
778 fmt
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
779 strlcpy(fmt
->description
, format
->name
,
780 sizeof fmt
->description
);
781 fmt
->description
[sizeof fmt
->description
- 1] = 0;
782 fmt
->pixelformat
= format
->fcc
;
788 struct uvc_streaming_control probe
;
790 return uvc_v4l2_try_format(stream
, arg
, &probe
, NULL
, NULL
);
794 if ((ret
= uvc_acquire_privileges(handle
)) < 0)
797 return uvc_v4l2_set_format(stream
, arg
);
800 return uvc_v4l2_get_format(stream
, arg
);
802 /* Frame size enumeration */
803 case VIDIOC_ENUM_FRAMESIZES
:
805 struct v4l2_frmsizeenum
*fsize
= arg
;
806 struct uvc_format
*format
= NULL
;
807 struct uvc_frame
*frame
;
810 /* Look for the given pixel format */
811 for (i
= 0; i
< stream
->nformats
; i
++) {
812 if (stream
->format
[i
].fcc
==
813 fsize
->pixel_format
) {
814 format
= &stream
->format
[i
];
821 if (fsize
->index
>= format
->nframes
)
824 frame
= &format
->frame
[fsize
->index
];
825 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
826 fsize
->discrete
.width
= frame
->wWidth
;
827 fsize
->discrete
.height
= frame
->wHeight
;
831 /* Frame interval enumeration */
832 case VIDIOC_ENUM_FRAMEINTERVALS
:
834 struct v4l2_frmivalenum
*fival
= arg
;
835 struct uvc_format
*format
= NULL
;
836 struct uvc_frame
*frame
= NULL
;
839 /* Look for the given pixel format and frame size */
840 for (i
= 0; i
< stream
->nformats
; i
++) {
841 if (stream
->format
[i
].fcc
==
842 fival
->pixel_format
) {
843 format
= &stream
->format
[i
];
850 for (i
= 0; i
< format
->nframes
; i
++) {
851 if (format
->frame
[i
].wWidth
== fival
->width
&&
852 format
->frame
[i
].wHeight
== fival
->height
) {
853 frame
= &format
->frame
[i
];
860 if (frame
->bFrameIntervalType
) {
861 if (fival
->index
>= frame
->bFrameIntervalType
)
864 fival
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
865 fival
->discrete
.numerator
=
866 frame
->dwFrameInterval
[fival
->index
];
867 fival
->discrete
.denominator
= 10000000;
868 uvc_simplify_fraction(&fival
->discrete
.numerator
,
869 &fival
->discrete
.denominator
, 8, 333);
871 fival
->type
= V4L2_FRMIVAL_TYPE_STEPWISE
;
872 fival
->stepwise
.min
.numerator
=
873 frame
->dwFrameInterval
[0];
874 fival
->stepwise
.min
.denominator
= 10000000;
875 fival
->stepwise
.max
.numerator
=
876 frame
->dwFrameInterval
[1];
877 fival
->stepwise
.max
.denominator
= 10000000;
878 fival
->stepwise
.step
.numerator
=
879 frame
->dwFrameInterval
[2];
880 fival
->stepwise
.step
.denominator
= 10000000;
881 uvc_simplify_fraction(&fival
->stepwise
.min
.numerator
,
882 &fival
->stepwise
.min
.denominator
, 8, 333);
883 uvc_simplify_fraction(&fival
->stepwise
.max
.numerator
,
884 &fival
->stepwise
.max
.denominator
, 8, 333);
885 uvc_simplify_fraction(&fival
->stepwise
.step
.numerator
,
886 &fival
->stepwise
.step
.denominator
, 8, 333);
891 /* Get & Set streaming parameters */
893 return uvc_v4l2_get_streamparm(stream
, arg
);
896 if ((ret
= uvc_acquire_privileges(handle
)) < 0)
899 return uvc_v4l2_set_streamparm(stream
, arg
);
901 /* Cropping and scaling */
904 struct v4l2_cropcap
*ccap
= arg
;
905 struct uvc_frame
*frame
= stream
->cur_frame
;
907 if (ccap
->type
!= stream
->type
)
910 ccap
->bounds
.left
= 0;
911 ccap
->bounds
.top
= 0;
912 ccap
->bounds
.width
= frame
->wWidth
;
913 ccap
->bounds
.height
= frame
->wHeight
;
915 ccap
->defrect
= ccap
->bounds
;
917 ccap
->pixelaspect
.numerator
= 1;
918 ccap
->pixelaspect
.denominator
= 1;
926 /* Buffers & streaming */
929 struct v4l2_requestbuffers
*rb
= arg
;
930 unsigned int bufsize
=
931 stream
->ctrl
.dwMaxVideoFrameSize
;
933 if (rb
->type
!= stream
->type
||
934 rb
->memory
!= V4L2_MEMORY_MMAP
)
937 if ((ret
= uvc_acquire_privileges(handle
)) < 0)
940 ret
= uvc_alloc_buffers(&stream
->queue
, rb
->count
, bufsize
);
945 uvc_dismiss_privileges(handle
);
952 case VIDIOC_QUERYBUF
:
954 struct v4l2_buffer
*buf
= arg
;
956 if (buf
->type
!= stream
->type
)
959 if (!uvc_has_privileges(handle
))
962 return uvc_query_buffer(&stream
->queue
, buf
);
966 if (!uvc_has_privileges(handle
))
969 return uvc_queue_buffer(&stream
->queue
, arg
);
972 if (!uvc_has_privileges(handle
))
975 return uvc_dequeue_buffer(&stream
->queue
, arg
,
976 file
->f_flags
& O_NONBLOCK
);
978 case VIDIOC_STREAMON
:
982 if (*type
!= stream
->type
)
985 if (!uvc_has_privileges(handle
))
988 ret
= uvc_video_enable(stream
, 1);
994 case VIDIOC_STREAMOFF
:
998 if (*type
!= stream
->type
)
1001 if (!uvc_has_privileges(handle
))
1004 return uvc_video_enable(stream
, 0);
1007 /* Analog video standards make no sense for digital cameras. */
1008 case VIDIOC_ENUMSTD
:
1009 case VIDIOC_QUERYSTD
:
1013 case VIDIOC_OVERLAY
:
1015 case VIDIOC_ENUMAUDIO
:
1016 case VIDIOC_ENUMAUDOUT
:
1018 case VIDIOC_ENUMOUTPUT
:
1019 uvc_trace(UVC_TRACE_IOCTL
, "Unsupported ioctl 0x%08x\n", cmd
);
1022 /* Dynamic controls. */
1023 case UVCIOC_CTRL_ADD
:
1025 struct uvc_xu_control_info
*xinfo
= arg
;
1026 struct uvc_control_info
*info
;
1028 if (!capable(CAP_SYS_ADMIN
))
1031 if (xinfo
->size
== 0)
1034 info
= kzalloc(sizeof *info
, GFP_KERNEL
);
1038 memcpy(info
->entity
, xinfo
->entity
, sizeof info
->entity
);
1039 info
->index
= xinfo
->index
;
1040 info
->selector
= xinfo
->selector
;
1041 info
->size
= xinfo
->size
;
1042 info
->flags
= xinfo
->flags
;
1044 info
->flags
|= UVC_CONTROL_GET_MIN
| UVC_CONTROL_GET_MAX
|
1045 UVC_CONTROL_GET_RES
| UVC_CONTROL_GET_DEF
|
1046 UVC_CONTROL_EXTENSION
;
1048 ret
= uvc_ctrl_add_info(info
);
1054 case UVCIOC_CTRL_MAP_OLD
:
1055 case UVCIOC_CTRL_MAP
:
1056 if (!capable(CAP_SYS_ADMIN
))
1059 return uvc_ioctl_ctrl_map(arg
, cmd
== UVCIOC_CTRL_MAP_OLD
);
1061 case UVCIOC_CTRL_GET
:
1062 return uvc_xu_ctrl_query(chain
, arg
, 0);
1064 case UVCIOC_CTRL_SET
:
1065 return uvc_xu_ctrl_query(chain
, arg
, 1);
1068 if ((ret
= v4l_compat_translate_ioctl(file
, cmd
, arg
,
1069 uvc_v4l2_do_ioctl
)) == -ENOIOCTLCMD
)
1070 uvc_trace(UVC_TRACE_IOCTL
, "Unknown ioctl 0x%08x\n",
1078 static long uvc_v4l2_ioctl(struct file
*file
,
1079 unsigned int cmd
, unsigned long arg
)
1081 if (uvc_trace_param
& UVC_TRACE_IOCTL
) {
1082 uvc_printk(KERN_DEBUG
, "uvc_v4l2_ioctl(");
1083 v4l_printk_ioctl(cmd
);
1087 return video_usercopy(file
, cmd
, arg
, uvc_v4l2_do_ioctl
);
1090 static ssize_t
uvc_v4l2_read(struct file
*file
, char __user
*data
,
1091 size_t count
, loff_t
*ppos
)
1093 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_read: not implemented.\n");
1100 static void uvc_vm_open(struct vm_area_struct
*vma
)
1102 struct uvc_buffer
*buffer
= vma
->vm_private_data
;
1103 buffer
->vma_use_count
++;
1106 static void uvc_vm_close(struct vm_area_struct
*vma
)
1108 struct uvc_buffer
*buffer
= vma
->vm_private_data
;
1109 buffer
->vma_use_count
--;
1112 static struct vm_operations_struct uvc_vm_ops
= {
1113 .open
= uvc_vm_open
,
1114 .close
= uvc_vm_close
,
1117 static int uvc_v4l2_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1119 struct uvc_fh
*handle
= file
->private_data
;
1120 struct uvc_streaming
*stream
= handle
->stream
;
1121 struct uvc_video_queue
*queue
= &stream
->queue
;
1122 struct uvc_buffer
*uninitialized_var(buffer
);
1124 unsigned long addr
, start
, size
;
1128 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_mmap\n");
1130 start
= vma
->vm_start
;
1131 size
= vma
->vm_end
- vma
->vm_start
;
1133 mutex_lock(&queue
->mutex
);
1135 for (i
= 0; i
< queue
->count
; ++i
) {
1136 buffer
= &queue
->buffer
[i
];
1137 if ((buffer
->buf
.m
.offset
>> PAGE_SHIFT
) == vma
->vm_pgoff
)
1141 if (i
== queue
->count
|| size
!= queue
->buf_size
) {
1147 * VM_IO marks the area as being an mmaped region for I/O to a
1148 * device. It also prevents the region from being core dumped.
1150 vma
->vm_flags
|= VM_IO
;
1152 addr
= (unsigned long)queue
->mem
+ buffer
->buf
.m
.offset
;
1154 page
= vmalloc_to_page((void *)addr
);
1155 if ((ret
= vm_insert_page(vma
, start
, page
)) < 0)
1163 vma
->vm_ops
= &uvc_vm_ops
;
1164 vma
->vm_private_data
= buffer
;
1168 mutex_unlock(&queue
->mutex
);
1172 static unsigned int uvc_v4l2_poll(struct file
*file
, poll_table
*wait
)
1174 struct uvc_fh
*handle
= file
->private_data
;
1175 struct uvc_streaming
*stream
= handle
->stream
;
1177 uvc_trace(UVC_TRACE_CALLS
, "uvc_v4l2_poll\n");
1179 return uvc_queue_poll(&stream
->queue
, file
, wait
);
1182 const struct v4l2_file_operations uvc_fops
= {
1183 .owner
= THIS_MODULE
,
1184 .open
= uvc_v4l2_open
,
1185 .release
= uvc_v4l2_release
,
1186 .ioctl
= uvc_v4l2_ioctl
,
1187 .read
= uvc_v4l2_read
,
1188 .mmap
= uvc_v4l2_mmap
,
1189 .poll
= uvc_v4l2_poll
,