Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1 | /* |
| 2 | * uvc_v4l2.c -- USB Video Class driver - V4L2 API |
| 3 | * |
Laurent Pinchart | 11fc5ba | 2010-09-20 06:10:10 -0300 | [diff] [blame] | 4 | * Copyright (C) 2005-2010 |
| 5 | * Laurent Pinchart (laurent.pinchart@ideasonboard.com) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 6 | * |
| 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. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/version.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 19 | #include <linux/usb.h> |
| 20 | #include <linux/videodev2.h> |
| 21 | #include <linux/vmalloc.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/wait.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 24 | #include <linux/atomic.h> |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 25 | |
| 26 | #include <media/v4l2-common.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 27 | #include <media/v4l2-ioctl.h> |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 28 | |
| 29 | #include "uvcvideo.h" |
| 30 | |
| 31 | /* ------------------------------------------------------------------------ |
Laurent Pinchart | 561474c2 | 2010-02-18 16:38:52 -0300 | [diff] [blame] | 32 | * UVC ioctls |
| 33 | */ |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 34 | static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain, |
Laurent Pinchart | 227bd5b | 2011-07-30 16:19:49 -0300 | [diff] [blame] | 35 | struct uvc_xu_control_mapping *xmap) |
Laurent Pinchart | 561474c2 | 2010-02-18 16:38:52 -0300 | [diff] [blame] | 36 | { |
| 37 | struct uvc_control_mapping *map; |
| 38 | unsigned int size; |
| 39 | int ret; |
| 40 | |
| 41 | map = kzalloc(sizeof *map, GFP_KERNEL); |
| 42 | if (map == NULL) |
| 43 | return -ENOMEM; |
| 44 | |
| 45 | map->id = xmap->id; |
| 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; |
| 53 | |
| 54 | switch (xmap->v4l2_type) { |
| 55 | case V4L2_CTRL_TYPE_INTEGER: |
| 56 | case V4L2_CTRL_TYPE_BOOLEAN: |
| 57 | case V4L2_CTRL_TYPE_BUTTON: |
| 58 | break; |
| 59 | |
| 60 | case V4L2_CTRL_TYPE_MENU: |
Haogang Chen | 806e23e | 2011-11-29 18:32:25 -0300 | [diff] [blame] | 61 | /* Prevent excessive memory consumption, as well as integer |
| 62 | * overflows. |
| 63 | */ |
| 64 | if (xmap->menu_count == 0 || |
| 65 | xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) { |
| 66 | ret = -EINVAL; |
| 67 | goto done; |
| 68 | } |
| 69 | |
Laurent Pinchart | 561474c2 | 2010-02-18 16:38:52 -0300 | [diff] [blame] | 70 | size = xmap->menu_count * sizeof(*map->menu_info); |
| 71 | map->menu_info = kmalloc(size, GFP_KERNEL); |
| 72 | if (map->menu_info == NULL) { |
| 73 | ret = -ENOMEM; |
| 74 | goto done; |
| 75 | } |
| 76 | |
| 77 | if (copy_from_user(map->menu_info, xmap->menu_info, size)) { |
| 78 | ret = -EFAULT; |
| 79 | goto done; |
| 80 | } |
| 81 | |
| 82 | map->menu_count = xmap->menu_count; |
| 83 | break; |
| 84 | |
| 85 | default: |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 86 | uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type " |
| 87 | "%u.\n", xmap->v4l2_type); |
Mauro Carvalho Chehab | 7a286cc | 2011-06-26 10:18:03 -0300 | [diff] [blame] | 88 | ret = -ENOTTY; |
Laurent Pinchart | 561474c2 | 2010-02-18 16:38:52 -0300 | [diff] [blame] | 89 | goto done; |
| 90 | } |
| 91 | |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 92 | ret = uvc_ctrl_add_mapping(chain, map); |
Laurent Pinchart | 561474c2 | 2010-02-18 16:38:52 -0300 | [diff] [blame] | 93 | |
| 94 | done: |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 95 | kfree(map->menu_info); |
| 96 | kfree(map); |
Laurent Pinchart | 561474c2 | 2010-02-18 16:38:52 -0300 | [diff] [blame] | 97 | |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | /* ------------------------------------------------------------------------ |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 102 | * V4L2 interface |
| 103 | */ |
| 104 | |
| 105 | /* |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 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 |
| 109 | * that feature. |
| 110 | */ |
| 111 | static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval) |
| 112 | { |
| 113 | unsigned int i; |
| 114 | |
| 115 | if (frame->bFrameIntervalType) { |
| 116 | __u32 best = -1, dist; |
| 117 | |
| 118 | for (i = 0; i < frame->bFrameIntervalType; ++i) { |
| 119 | dist = interval > frame->dwFrameInterval[i] |
| 120 | ? interval - frame->dwFrameInterval[i] |
| 121 | : frame->dwFrameInterval[i] - interval; |
| 122 | |
| 123 | if (dist > best) |
| 124 | break; |
| 125 | |
| 126 | best = dist; |
| 127 | } |
| 128 | |
| 129 | interval = frame->dwFrameInterval[i-1]; |
| 130 | } else { |
| 131 | const __u32 min = frame->dwFrameInterval[0]; |
| 132 | const __u32 max = frame->dwFrameInterval[1]; |
| 133 | const __u32 step = frame->dwFrameInterval[2]; |
| 134 | |
| 135 | interval = min + (interval - min + step/2) / step * step; |
| 136 | if (interval > max) |
| 137 | interval = max; |
| 138 | } |
| 139 | |
| 140 | return interval; |
| 141 | } |
| 142 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 143 | static int uvc_v4l2_try_format(struct uvc_streaming *stream, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 144 | struct v4l2_format *fmt, struct uvc_streaming_control *probe, |
| 145 | struct uvc_format **uvc_format, struct uvc_frame **uvc_frame) |
| 146 | { |
| 147 | struct uvc_format *format = NULL; |
| 148 | struct uvc_frame *frame = NULL; |
| 149 | __u16 rw, rh; |
| 150 | unsigned int d, maxd; |
| 151 | unsigned int i; |
| 152 | __u32 interval; |
| 153 | int ret = 0; |
| 154 | __u8 *fcc; |
| 155 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 156 | if (fmt->type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 157 | return -EINVAL; |
| 158 | |
| 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); |
| 164 | |
| 165 | /* Check if the hardware supports the requested format. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 166 | for (i = 0; i < stream->nformats; ++i) { |
| 167 | format = &stream->format[i]; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 168 | if (format->fcc == fmt->fmt.pix.pixelformat) |
| 169 | break; |
| 170 | } |
| 171 | |
| 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); |
| 175 | return -EINVAL; |
| 176 | } |
| 177 | |
| 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. |
| 181 | */ |
| 182 | rw = fmt->fmt.pix.width; |
| 183 | rh = fmt->fmt.pix.height; |
| 184 | maxd = (unsigned int)-1; |
| 185 | |
| 186 | for (i = 0; i < format->nframes; ++i) { |
| 187 | __u16 w = format->frame[i].wWidth; |
| 188 | __u16 h = format->frame[i].wHeight; |
| 189 | |
| 190 | d = min(w, rw) * min(h, rh); |
| 191 | d = w*h + rw*rh - 2*d; |
| 192 | if (d < maxd) { |
| 193 | maxd = d; |
| 194 | frame = &format->frame[i]; |
| 195 | } |
| 196 | |
| 197 | if (maxd == 0) |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | if (frame == NULL) { |
| 202 | uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n", |
| 203 | fmt->fmt.pix.width, fmt->fmt.pix.height); |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 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); |
| 212 | |
| 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. |
| 224 | * |
| 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). |
| 230 | */ |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 231 | mutex_lock(&stream->mutex); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 232 | if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 233 | probe->dwMaxVideoFrameSize = |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 234 | stream->ctrl.dwMaxVideoFrameSize; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 235 | |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 236 | /* Probe the device. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 237 | ret = uvc_probe_video(stream, probe); |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 238 | mutex_unlock(&stream->mutex); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 239 | if (ret < 0) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 240 | goto done; |
| 241 | |
| 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; |
| 249 | |
| 250 | if (uvc_format != NULL) |
| 251 | *uvc_format = format; |
| 252 | if (uvc_frame != NULL) |
| 253 | *uvc_frame = frame; |
| 254 | |
| 255 | done: |
| 256 | return ret; |
| 257 | } |
| 258 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 259 | static int uvc_v4l2_get_format(struct uvc_streaming *stream, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 260 | struct v4l2_format *fmt) |
| 261 | { |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 262 | struct uvc_format *format; |
| 263 | struct uvc_frame *frame; |
| 264 | int ret = 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 265 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 266 | if (fmt->type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 267 | return -EINVAL; |
| 268 | |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 269 | mutex_lock(&stream->mutex); |
| 270 | format = stream->cur_format; |
| 271 | frame = stream->cur_frame; |
| 272 | |
| 273 | if (format == NULL || frame == NULL) { |
| 274 | ret = -EINVAL; |
| 275 | goto done; |
| 276 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 277 | |
| 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; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 283 | fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 284 | fmt->fmt.pix.colorspace = format->colorspace; |
| 285 | fmt->fmt.pix.priv = 0; |
| 286 | |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 287 | done: |
| 288 | mutex_unlock(&stream->mutex); |
| 289 | return ret; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 290 | } |
| 291 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 292 | static int uvc_v4l2_set_format(struct uvc_streaming *stream, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 293 | struct v4l2_format *fmt) |
| 294 | { |
| 295 | struct uvc_streaming_control probe; |
| 296 | struct uvc_format *format; |
| 297 | struct uvc_frame *frame; |
| 298 | int ret; |
| 299 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 300 | if (fmt->type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 301 | return -EINVAL; |
| 302 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 303 | ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 304 | if (ret < 0) |
| 305 | return ret; |
| 306 | |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 307 | mutex_lock(&stream->mutex); |
| 308 | |
| 309 | if (uvc_queue_allocated(&stream->queue)) { |
| 310 | ret = -EBUSY; |
| 311 | goto done; |
| 312 | } |
| 313 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 314 | memcpy(&stream->ctrl, &probe, sizeof probe); |
| 315 | stream->cur_format = format; |
| 316 | stream->cur_frame = frame; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 317 | |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 318 | done: |
| 319 | mutex_unlock(&stream->mutex); |
| 320 | return ret; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 321 | } |
| 322 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 323 | static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 324 | struct v4l2_streamparm *parm) |
| 325 | { |
| 326 | uint32_t numerator, denominator; |
| 327 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 328 | if (parm->type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 329 | return -EINVAL; |
| 330 | |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 331 | mutex_lock(&stream->mutex); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 332 | numerator = stream->ctrl.dwFrameInterval; |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 333 | mutex_unlock(&stream->mutex); |
| 334 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 335 | denominator = 10000000; |
| 336 | uvc_simplify_fraction(&numerator, &denominator, 8, 333); |
| 337 | |
| 338 | memset(parm, 0, sizeof *parm); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 339 | parm->type = stream->type; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 340 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 341 | if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 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; |
| 348 | } else { |
| 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; |
| 353 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 358 | static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 359 | struct v4l2_streamparm *parm) |
| 360 | { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 361 | struct uvc_streaming_control probe; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 362 | struct v4l2_fract timeperframe; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 363 | uint32_t interval; |
| 364 | int ret; |
| 365 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 366 | if (parm->type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 367 | return -EINVAL; |
| 368 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 369 | if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 370 | timeperframe = parm->parm.capture.timeperframe; |
| 371 | else |
| 372 | timeperframe = parm->parm.output.timeperframe; |
| 373 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 374 | interval = uvc_fraction_to_interval(timeperframe.numerator, |
| 375 | timeperframe.denominator); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 376 | uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n", |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 377 | timeperframe.numerator, timeperframe.denominator, interval); |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 378 | |
| 379 | mutex_lock(&stream->mutex); |
| 380 | |
| 381 | if (uvc_queue_streaming(&stream->queue)) { |
| 382 | mutex_unlock(&stream->mutex); |
| 383 | return -EBUSY; |
| 384 | } |
| 385 | |
| 386 | memcpy(&probe, &stream->ctrl, sizeof probe); |
| 387 | probe.dwFrameInterval = |
| 388 | uvc_try_frame_interval(stream->cur_frame, interval); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 389 | |
| 390 | /* Probe the device with the new settings. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 391 | ret = uvc_probe_video(stream, &probe); |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 392 | if (ret < 0) { |
| 393 | mutex_unlock(&stream->mutex); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 394 | return ret; |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 395 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 396 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 397 | memcpy(&stream->ctrl, &probe, sizeof probe); |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 398 | mutex_unlock(&stream->mutex); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 399 | |
| 400 | /* Return the actual frame period. */ |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 401 | timeperframe.numerator = probe.dwFrameInterval; |
| 402 | timeperframe.denominator = 10000000; |
| 403 | uvc_simplify_fraction(&timeperframe.numerator, |
| 404 | &timeperframe.denominator, 8, 333); |
| 405 | |
| 406 | if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 407 | parm->parm.capture.timeperframe = timeperframe; |
| 408 | else |
| 409 | parm->parm.output.timeperframe = timeperframe; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /* ------------------------------------------------------------------------ |
| 415 | * Privilege management |
| 416 | */ |
| 417 | |
| 418 | /* |
| 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. |
| 426 | * |
| 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 |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 429 | * a given time. Trying to perform an operation that requires privileges will |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 430 | * automatically acquire the required privileges if possible, or return -EBUSY |
Laurent Pinchart | 1a969d9 | 2009-09-02 03:12:26 -0300 | [diff] [blame] | 431 | * otherwise. Privileges are dismissed when closing the instance or when |
| 432 | * freeing the video buffers using VIDIOC_REQBUFS. |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 433 | * |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 434 | * Operations that require privileges are: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 435 | * |
| 436 | * - VIDIOC_S_INPUT |
| 437 | * - VIDIOC_S_PARM |
| 438 | * - VIDIOC_S_FMT |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 439 | * - VIDIOC_REQBUFS |
| 440 | */ |
| 441 | static int uvc_acquire_privileges(struct uvc_fh *handle) |
| 442 | { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 443 | /* Always succeed if the handle is already privileged. */ |
| 444 | if (handle->state == UVC_HANDLE_ACTIVE) |
| 445 | return 0; |
| 446 | |
| 447 | /* Check if the device already has a privileged handle. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 448 | if (atomic_inc_return(&handle->stream->active) != 1) { |
| 449 | atomic_dec(&handle->stream->active); |
Laurent Pinchart | 716fdee | 2009-09-29 21:07:19 -0300 | [diff] [blame] | 450 | return -EBUSY; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | handle->state = UVC_HANDLE_ACTIVE; |
Laurent Pinchart | 716fdee | 2009-09-29 21:07:19 -0300 | [diff] [blame] | 454 | return 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static void uvc_dismiss_privileges(struct uvc_fh *handle) |
| 458 | { |
| 459 | if (handle->state == UVC_HANDLE_ACTIVE) |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 460 | atomic_dec(&handle->stream->active); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 461 | |
| 462 | handle->state = UVC_HANDLE_PASSIVE; |
| 463 | } |
| 464 | |
| 465 | static int uvc_has_privileges(struct uvc_fh *handle) |
| 466 | { |
| 467 | return handle->state == UVC_HANDLE_ACTIVE; |
| 468 | } |
| 469 | |
| 470 | /* ------------------------------------------------------------------------ |
| 471 | * V4L2 file operations |
| 472 | */ |
| 473 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 474 | static int uvc_v4l2_open(struct file *file) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 475 | { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 476 | struct uvc_streaming *stream; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 477 | struct uvc_fh *handle; |
| 478 | int ret = 0; |
| 479 | |
| 480 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n"); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 481 | stream = video_drvdata(file); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 482 | |
Laurent Pinchart | 716fdee | 2009-09-29 21:07:19 -0300 | [diff] [blame] | 483 | if (stream->dev->state & UVC_DEV_DISCONNECTED) |
| 484 | return -ENODEV; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 485 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 486 | ret = usb_autopm_get_interface(stream->dev->intf); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 487 | if (ret < 0) |
Laurent Pinchart | 716fdee | 2009-09-29 21:07:19 -0300 | [diff] [blame] | 488 | return ret; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 489 | |
| 490 | /* Create the device handle. */ |
| 491 | handle = kzalloc(sizeof *handle, GFP_KERNEL); |
| 492 | if (handle == NULL) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 493 | usb_autopm_put_interface(stream->dev->intf); |
Laurent Pinchart | 716fdee | 2009-09-29 21:07:19 -0300 | [diff] [blame] | 494 | return -ENOMEM; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 495 | } |
| 496 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 497 | if (atomic_inc_return(&stream->dev->users) == 1) { |
| 498 | ret = uvc_status_start(stream->dev); |
| 499 | if (ret < 0) { |
| 500 | usb_autopm_put_interface(stream->dev->intf); |
| 501 | atomic_dec(&stream->dev->users); |
Laurent Pinchart | 04a37e0 | 2009-05-19 10:08:03 -0300 | [diff] [blame] | 502 | kfree(handle); |
Laurent Pinchart | 716fdee | 2009-09-29 21:07:19 -0300 | [diff] [blame] | 503 | return ret; |
Laurent Pinchart | 04a37e0 | 2009-05-19 10:08:03 -0300 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 507 | handle->chain = stream->chain; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 508 | handle->stream = stream; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 509 | handle->state = UVC_HANDLE_PASSIVE; |
| 510 | file->private_data = handle; |
| 511 | |
Laurent Pinchart | 716fdee | 2009-09-29 21:07:19 -0300 | [diff] [blame] | 512 | return 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 513 | } |
| 514 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 515 | static int uvc_v4l2_release(struct file *file) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 516 | { |
Joe Perches | abf8438 | 2010-07-12 17:50:03 -0300 | [diff] [blame] | 517 | struct uvc_fh *handle = file->private_data; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 518 | struct uvc_streaming *stream = handle->stream; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 519 | |
| 520 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n"); |
| 521 | |
| 522 | /* Only free resources if this is a privileged handle. */ |
| 523 | if (uvc_has_privileges(handle)) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 524 | uvc_video_enable(stream, 0); |
Laurent Pinchart | 6998b6f | 2011-10-24 11:53:59 -0300 | [diff] [blame] | 525 | uvc_free_buffers(&stream->queue); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /* Release the file handle. */ |
| 529 | uvc_dismiss_privileges(handle); |
| 530 | kfree(handle); |
| 531 | file->private_data = NULL; |
| 532 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 533 | if (atomic_dec_return(&stream->dev->users) == 0) |
| 534 | uvc_status_stop(stream->dev); |
Laurent Pinchart | 04a37e0 | 2009-05-19 10:08:03 -0300 | [diff] [blame] | 535 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 536 | usb_autopm_put_interface(stream->dev->intf); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 537 | return 0; |
| 538 | } |
| 539 | |
Hans Verkuil | 069b747 | 2008-12-30 07:04:34 -0300 | [diff] [blame] | 540 | static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 541 | { |
| 542 | struct video_device *vdev = video_devdata(file); |
Joe Perches | abf8438 | 2010-07-12 17:50:03 -0300 | [diff] [blame] | 543 | struct uvc_fh *handle = file->private_data; |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 544 | struct uvc_video_chain *chain = handle->chain; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 545 | struct uvc_streaming *stream = handle->stream; |
Hans Verkuil | 069b747 | 2008-12-30 07:04:34 -0300 | [diff] [blame] | 546 | long ret = 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 547 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 548 | switch (cmd) { |
| 549 | /* Query capabilities */ |
| 550 | case VIDIOC_QUERYCAP: |
| 551 | { |
| 552 | struct v4l2_capability *cap = arg; |
| 553 | |
| 554 | memset(cap, 0, sizeof *cap); |
Laurent Pinchart | d0ebf30 | 2009-01-08 14:05:11 -0300 | [diff] [blame] | 555 | strlcpy(cap->driver, "uvcvideo", sizeof cap->driver); |
| 556 | strlcpy(cap->card, vdev->name, sizeof cap->card); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 557 | usb_make_path(stream->dev->udev, |
Thierry MERLE | b12049a | 2009-01-27 16:53:23 -0300 | [diff] [blame] | 558 | cap->bus_info, sizeof(cap->bus_info)); |
Mauro Carvalho Chehab | fd3e582 | 2011-06-25 13:50:37 -0300 | [diff] [blame] | 559 | cap->version = LINUX_VERSION_CODE; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 560 | if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 561 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
| 562 | | V4L2_CAP_STREAMING; |
| 563 | else |
| 564 | cap->capabilities = V4L2_CAP_VIDEO_OUTPUT |
| 565 | | V4L2_CAP_STREAMING; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 566 | break; |
| 567 | } |
| 568 | |
| 569 | /* Get, Set & Query control */ |
| 570 | case VIDIOC_QUERYCTRL: |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 571 | return uvc_query_v4l2_ctrl(chain, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 572 | |
| 573 | case VIDIOC_G_CTRL: |
| 574 | { |
| 575 | struct v4l2_control *ctrl = arg; |
| 576 | struct v4l2_ext_control xctrl; |
| 577 | |
| 578 | memset(&xctrl, 0, sizeof xctrl); |
| 579 | xctrl.id = ctrl->id; |
| 580 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 581 | ret = uvc_ctrl_begin(chain); |
| 582 | if (ret < 0) |
Robert Krakora | 1fcbcc4 | 2009-06-12 13:51:03 -0300 | [diff] [blame] | 583 | return ret; |
| 584 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 585 | ret = uvc_ctrl_get(chain, &xctrl); |
| 586 | uvc_ctrl_rollback(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 587 | if (ret >= 0) |
| 588 | ctrl->value = xctrl.value; |
| 589 | break; |
| 590 | } |
| 591 | |
| 592 | case VIDIOC_S_CTRL: |
| 593 | { |
| 594 | struct v4l2_control *ctrl = arg; |
| 595 | struct v4l2_ext_control xctrl; |
| 596 | |
| 597 | memset(&xctrl, 0, sizeof xctrl); |
| 598 | xctrl.id = ctrl->id; |
| 599 | xctrl.value = ctrl->value; |
| 600 | |
Laurent Pinchart | 8d55662 | 2010-02-04 21:43:37 -0300 | [diff] [blame] | 601 | ret = uvc_ctrl_begin(chain); |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 602 | if (ret < 0) |
Robert Krakora | 1fcbcc4 | 2009-06-12 13:51:03 -0300 | [diff] [blame] | 603 | return ret; |
| 604 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 605 | ret = uvc_ctrl_set(chain, &xctrl); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 606 | if (ret < 0) { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 607 | uvc_ctrl_rollback(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 608 | return ret; |
| 609 | } |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 610 | ret = uvc_ctrl_commit(chain); |
Laurent Pinchart | e54532e | 2010-01-23 07:07:53 -0300 | [diff] [blame] | 611 | if (ret == 0) |
| 612 | ctrl->value = xctrl.value; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 613 | break; |
| 614 | } |
| 615 | |
| 616 | case VIDIOC_QUERYMENU: |
Laurent Pinchart | 23d9f3e | 2010-11-21 07:58:54 -0300 | [diff] [blame] | 617 | return uvc_query_v4l2_menu(chain, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 618 | |
| 619 | case VIDIOC_G_EXT_CTRLS: |
| 620 | { |
| 621 | struct v4l2_ext_controls *ctrls = arg; |
| 622 | struct v4l2_ext_control *ctrl = ctrls->controls; |
| 623 | unsigned int i; |
| 624 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 625 | ret = uvc_ctrl_begin(chain); |
| 626 | if (ret < 0) |
Robert Krakora | 1fcbcc4 | 2009-06-12 13:51:03 -0300 | [diff] [blame] | 627 | return ret; |
| 628 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 629 | for (i = 0; i < ctrls->count; ++ctrl, ++i) { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 630 | ret = uvc_ctrl_get(chain, ctrl); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 631 | if (ret < 0) { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 632 | uvc_ctrl_rollback(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 633 | ctrls->error_idx = i; |
| 634 | return ret; |
| 635 | } |
| 636 | } |
| 637 | ctrls->error_idx = 0; |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 638 | ret = uvc_ctrl_rollback(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 639 | break; |
| 640 | } |
| 641 | |
| 642 | case VIDIOC_S_EXT_CTRLS: |
| 643 | case VIDIOC_TRY_EXT_CTRLS: |
| 644 | { |
| 645 | struct v4l2_ext_controls *ctrls = arg; |
| 646 | struct v4l2_ext_control *ctrl = ctrls->controls; |
| 647 | unsigned int i; |
| 648 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 649 | ret = uvc_ctrl_begin(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 650 | if (ret < 0) |
| 651 | return ret; |
| 652 | |
| 653 | for (i = 0; i < ctrls->count; ++ctrl, ++i) { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 654 | ret = uvc_ctrl_set(chain, ctrl); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 655 | if (ret < 0) { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 656 | uvc_ctrl_rollback(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 657 | ctrls->error_idx = i; |
| 658 | return ret; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | ctrls->error_idx = 0; |
| 663 | |
| 664 | if (cmd == VIDIOC_S_EXT_CTRLS) |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 665 | ret = uvc_ctrl_commit(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 666 | else |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 667 | ret = uvc_ctrl_rollback(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 668 | break; |
| 669 | } |
| 670 | |
| 671 | /* Get, Set & Enum input */ |
| 672 | case VIDIOC_ENUMINPUT: |
| 673 | { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 674 | const struct uvc_entity *selector = chain->selector; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 675 | struct v4l2_input *input = arg; |
| 676 | struct uvc_entity *iterm = NULL; |
| 677 | u32 index = input->index; |
| 678 | int pin = 0; |
| 679 | |
| 680 | if (selector == NULL || |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 681 | (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 682 | if (index != 0) |
| 683 | return -EINVAL; |
Laurent Pinchart | 6241d8c | 2009-11-25 12:00:22 -0300 | [diff] [blame] | 684 | list_for_each_entry(iterm, &chain->entities, chain) { |
| 685 | if (UVC_ENTITY_IS_ITERM(iterm)) |
| 686 | break; |
| 687 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 688 | pin = iterm->id; |
Laurent Pinchart | 8ca5a63 | 2009-11-25 12:00:30 -0300 | [diff] [blame] | 689 | } else if (pin < selector->bNrInPins) { |
| 690 | pin = selector->baSourceID[index]; |
Laurent Pinchart | 6241d8c | 2009-11-25 12:00:22 -0300 | [diff] [blame] | 691 | list_for_each_entry(iterm, &chain->entities, chain) { |
| 692 | if (!UVC_ENTITY_IS_ITERM(iterm)) |
| 693 | continue; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 694 | if (iterm->id == pin) |
| 695 | break; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | if (iterm == NULL || iterm->id != pin) |
| 700 | return -EINVAL; |
| 701 | |
| 702 | memset(input, 0, sizeof *input); |
| 703 | input->index = index; |
Laurent Pinchart | d0ebf30 | 2009-01-08 14:05:11 -0300 | [diff] [blame] | 704 | strlcpy(input->name, iterm->name, sizeof input->name); |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 705 | if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 706 | input->type = V4L2_INPUT_TYPE_CAMERA; |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | case VIDIOC_G_INPUT: |
| 711 | { |
| 712 | u8 input; |
| 713 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 714 | if (chain->selector == NULL || |
| 715 | (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 716 | *(int *)arg = 0; |
| 717 | break; |
| 718 | } |
| 719 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 720 | ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, |
| 721 | chain->selector->id, chain->dev->intfnum, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 722 | UVC_SU_INPUT_SELECT_CONTROL, &input, 1); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 723 | if (ret < 0) |
| 724 | return ret; |
| 725 | |
| 726 | *(int *)arg = input - 1; |
| 727 | break; |
| 728 | } |
| 729 | |
| 730 | case VIDIOC_S_INPUT: |
| 731 | { |
Márton Németh | 9086c7b99 | 2009-04-15 09:01:58 -0300 | [diff] [blame] | 732 | u32 input = *(u32 *)arg + 1; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 733 | |
| 734 | if ((ret = uvc_acquire_privileges(handle)) < 0) |
| 735 | return ret; |
| 736 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 737 | if (chain->selector == NULL || |
| 738 | (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 739 | if (input != 1) |
| 740 | return -EINVAL; |
| 741 | break; |
| 742 | } |
| 743 | |
Laurent Pinchart | 8ca5a63 | 2009-11-25 12:00:30 -0300 | [diff] [blame] | 744 | if (input == 0 || input > chain->selector->bNrInPins) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 745 | return -EINVAL; |
| 746 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 747 | return uvc_query_ctrl(chain->dev, UVC_SET_CUR, |
| 748 | chain->selector->id, chain->dev->intfnum, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 749 | UVC_SU_INPUT_SELECT_CONTROL, &input, 1); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* Try, Get, Set & Enum format */ |
| 753 | case VIDIOC_ENUM_FMT: |
| 754 | { |
| 755 | struct v4l2_fmtdesc *fmt = arg; |
| 756 | struct uvc_format *format; |
Márton Németh | 8bbd90c | 2009-03-27 11:13:57 -0300 | [diff] [blame] | 757 | enum v4l2_buf_type type = fmt->type; |
| 758 | __u32 index = fmt->index; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 759 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 760 | if (fmt->type != stream->type || |
| 761 | fmt->index >= stream->nformats) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 762 | return -EINVAL; |
| 763 | |
Márton Németh | 8bbd90c | 2009-03-27 11:13:57 -0300 | [diff] [blame] | 764 | memset(fmt, 0, sizeof(*fmt)); |
| 765 | fmt->index = index; |
| 766 | fmt->type = type; |
| 767 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 768 | format = &stream->format[fmt->index]; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 769 | fmt->flags = 0; |
| 770 | if (format->flags & UVC_FMT_FLAG_COMPRESSED) |
| 771 | fmt->flags |= V4L2_FMT_FLAG_COMPRESSED; |
Laurent Pinchart | d0ebf30 | 2009-01-08 14:05:11 -0300 | [diff] [blame] | 772 | strlcpy(fmt->description, format->name, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 773 | sizeof fmt->description); |
| 774 | fmt->description[sizeof fmt->description - 1] = 0; |
| 775 | fmt->pixelformat = format->fcc; |
| 776 | break; |
| 777 | } |
| 778 | |
| 779 | case VIDIOC_TRY_FMT: |
| 780 | { |
| 781 | struct uvc_streaming_control probe; |
| 782 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 783 | return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | case VIDIOC_S_FMT: |
| 787 | if ((ret = uvc_acquire_privileges(handle)) < 0) |
| 788 | return ret; |
| 789 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 790 | return uvc_v4l2_set_format(stream, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 791 | |
| 792 | case VIDIOC_G_FMT: |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 793 | return uvc_v4l2_get_format(stream, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 794 | |
| 795 | /* Frame size enumeration */ |
| 796 | case VIDIOC_ENUM_FRAMESIZES: |
| 797 | { |
| 798 | struct v4l2_frmsizeenum *fsize = arg; |
| 799 | struct uvc_format *format = NULL; |
| 800 | struct uvc_frame *frame; |
| 801 | int i; |
| 802 | |
| 803 | /* Look for the given pixel format */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 804 | for (i = 0; i < stream->nformats; i++) { |
| 805 | if (stream->format[i].fcc == |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 806 | fsize->pixel_format) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 807 | format = &stream->format[i]; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 808 | break; |
| 809 | } |
| 810 | } |
| 811 | if (format == NULL) |
| 812 | return -EINVAL; |
| 813 | |
| 814 | if (fsize->index >= format->nframes) |
| 815 | return -EINVAL; |
| 816 | |
| 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; |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | /* Frame interval enumeration */ |
| 825 | case VIDIOC_ENUM_FRAMEINTERVALS: |
| 826 | { |
| 827 | struct v4l2_frmivalenum *fival = arg; |
| 828 | struct uvc_format *format = NULL; |
| 829 | struct uvc_frame *frame = NULL; |
| 830 | int i; |
| 831 | |
| 832 | /* Look for the given pixel format and frame size */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 833 | for (i = 0; i < stream->nformats; i++) { |
| 834 | if (stream->format[i].fcc == |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 835 | fival->pixel_format) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 836 | format = &stream->format[i]; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 837 | break; |
| 838 | } |
| 839 | } |
| 840 | if (format == NULL) |
| 841 | return -EINVAL; |
| 842 | |
| 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]; |
| 847 | break; |
| 848 | } |
| 849 | } |
| 850 | if (frame == NULL) |
| 851 | return -EINVAL; |
| 852 | |
| 853 | if (frame->bFrameIntervalType) { |
| 854 | if (fival->index >= frame->bFrameIntervalType) |
| 855 | return -EINVAL; |
| 856 | |
| 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); |
| 863 | } else { |
| 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); |
| 880 | } |
| 881 | break; |
| 882 | } |
| 883 | |
| 884 | /* Get & Set streaming parameters */ |
| 885 | case VIDIOC_G_PARM: |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 886 | return uvc_v4l2_get_streamparm(stream, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 887 | |
| 888 | case VIDIOC_S_PARM: |
| 889 | if ((ret = uvc_acquire_privileges(handle)) < 0) |
| 890 | return ret; |
| 891 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 892 | return uvc_v4l2_set_streamparm(stream, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 893 | |
| 894 | /* Cropping and scaling */ |
| 895 | case VIDIOC_CROPCAP: |
| 896 | { |
| 897 | struct v4l2_cropcap *ccap = arg; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 898 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 899 | if (ccap->type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 900 | return -EINVAL; |
| 901 | |
| 902 | ccap->bounds.left = 0; |
| 903 | ccap->bounds.top = 0; |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 904 | |
| 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); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 909 | |
| 910 | ccap->defrect = ccap->bounds; |
| 911 | |
| 912 | ccap->pixelaspect.numerator = 1; |
| 913 | ccap->pixelaspect.denominator = 1; |
| 914 | break; |
| 915 | } |
| 916 | |
| 917 | case VIDIOC_G_CROP: |
| 918 | case VIDIOC_S_CROP: |
| 919 | return -EINVAL; |
| 920 | |
| 921 | /* Buffers & streaming */ |
| 922 | case VIDIOC_REQBUFS: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 923 | if ((ret = uvc_acquire_privileges(handle)) < 0) |
| 924 | return ret; |
| 925 | |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 926 | mutex_lock(&stream->mutex); |
Laurent Pinchart | 6998b6f | 2011-10-24 11:53:59 -0300 | [diff] [blame] | 927 | ret = uvc_alloc_buffers(&stream->queue, arg); |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 928 | mutex_unlock(&stream->mutex); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 929 | if (ret < 0) |
| 930 | return ret; |
| 931 | |
Laurent Pinchart | 1a969d9 | 2009-09-02 03:12:26 -0300 | [diff] [blame] | 932 | if (ret == 0) |
| 933 | uvc_dismiss_privileges(handle); |
| 934 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 935 | ret = 0; |
| 936 | break; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 937 | |
| 938 | case VIDIOC_QUERYBUF: |
| 939 | { |
| 940 | struct v4l2_buffer *buf = arg; |
| 941 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 942 | if (!uvc_has_privileges(handle)) |
| 943 | return -EBUSY; |
| 944 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 945 | return uvc_query_buffer(&stream->queue, buf); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | case VIDIOC_QBUF: |
| 949 | if (!uvc_has_privileges(handle)) |
| 950 | return -EBUSY; |
| 951 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 952 | return uvc_queue_buffer(&stream->queue, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 953 | |
| 954 | case VIDIOC_DQBUF: |
| 955 | if (!uvc_has_privileges(handle)) |
| 956 | return -EBUSY; |
| 957 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 958 | return uvc_dequeue_buffer(&stream->queue, arg, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 959 | file->f_flags & O_NONBLOCK); |
| 960 | |
| 961 | case VIDIOC_STREAMON: |
| 962 | { |
| 963 | int *type = arg; |
| 964 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 965 | if (*type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 966 | return -EINVAL; |
| 967 | |
| 968 | if (!uvc_has_privileges(handle)) |
| 969 | return -EBUSY; |
| 970 | |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 971 | mutex_lock(&stream->mutex); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 972 | ret = uvc_video_enable(stream, 1); |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 973 | mutex_unlock(&stream->mutex); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 974 | if (ret < 0) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 975 | return ret; |
| 976 | break; |
| 977 | } |
| 978 | |
| 979 | case VIDIOC_STREAMOFF: |
| 980 | { |
| 981 | int *type = arg; |
| 982 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 983 | if (*type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 984 | return -EINVAL; |
| 985 | |
| 986 | if (!uvc_has_privileges(handle)) |
| 987 | return -EBUSY; |
| 988 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 989 | return uvc_video_enable(stream, 0); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /* Analog video standards make no sense for digital cameras. */ |
| 993 | case VIDIOC_ENUMSTD: |
| 994 | case VIDIOC_QUERYSTD: |
| 995 | case VIDIOC_G_STD: |
| 996 | case VIDIOC_S_STD: |
| 997 | |
| 998 | case VIDIOC_OVERLAY: |
| 999 | |
| 1000 | case VIDIOC_ENUMAUDIO: |
| 1001 | case VIDIOC_ENUMAUDOUT: |
| 1002 | |
| 1003 | case VIDIOC_ENUMOUTPUT: |
| 1004 | uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd); |
| 1005 | return -EINVAL; |
| 1006 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1007 | case UVCIOC_CTRL_MAP: |
Laurent Pinchart | 227bd5b | 2011-07-30 16:19:49 -0300 | [diff] [blame] | 1008 | return uvc_ioctl_ctrl_map(chain, arg); |
Martin Rubli | fe78d18 | 2010-10-02 19:10:16 -0300 | [diff] [blame] | 1009 | |
| 1010 | case UVCIOC_CTRL_QUERY: |
| 1011 | return uvc_xu_ctrl_query(chain, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1012 | |
| 1013 | default: |
Hans Verkuil | 08af245 | 2010-12-24 10:33:19 -0300 | [diff] [blame] | 1014 | uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd); |
Laurent Pinchart | a3ec69b | 2011-12-18 20:22:50 -0300 | [diff] [blame^] | 1015 | return -ENOTTY; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | return ret; |
| 1019 | } |
| 1020 | |
Hans Verkuil | 069b747 | 2008-12-30 07:04:34 -0300 | [diff] [blame] | 1021 | static long uvc_v4l2_ioctl(struct file *file, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1022 | unsigned int cmd, unsigned long arg) |
| 1023 | { |
Laurent Pinchart | 308bb52 | 2008-11-17 18:32:11 -0300 | [diff] [blame] | 1024 | if (uvc_trace_param & UVC_TRACE_IOCTL) { |
| 1025 | uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl("); |
| 1026 | v4l_printk_ioctl(cmd); |
| 1027 | printk(")\n"); |
| 1028 | } |
| 1029 | |
Hans Verkuil | f473bf7 | 2008-11-01 08:25:11 -0300 | [diff] [blame] | 1030 | return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static ssize_t uvc_v4l2_read(struct file *file, char __user *data, |
| 1034 | size_t count, loff_t *ppos) |
| 1035 | { |
| 1036 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n"); |
Laurent Pinchart | 350d640 | 2009-11-04 11:53:49 -0300 | [diff] [blame] | 1037 | return -EINVAL; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1038 | } |
| 1039 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1040 | static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma) |
| 1041 | { |
Joe Perches | abf8438 | 2010-07-12 17:50:03 -0300 | [diff] [blame] | 1042 | struct uvc_fh *handle = file->private_data; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1043 | struct uvc_streaming *stream = handle->stream; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1044 | |
| 1045 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n"); |
| 1046 | |
Laurent Pinchart | 4aa2759 | 2010-11-21 15:18:08 -0300 | [diff] [blame] | 1047 | return uvc_queue_mmap(&stream->queue, vma); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait) |
| 1051 | { |
Joe Perches | abf8438 | 2010-07-12 17:50:03 -0300 | [diff] [blame] | 1052 | struct uvc_fh *handle = file->private_data; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1053 | struct uvc_streaming *stream = handle->stream; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1054 | |
| 1055 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n"); |
| 1056 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1057 | return uvc_queue_poll(&stream->queue, file, wait); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1058 | } |
| 1059 | |
Bob Liu | 7296944 | 2011-04-29 07:11:35 -0300 | [diff] [blame] | 1060 | #ifndef CONFIG_MMU |
| 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) |
| 1064 | { |
| 1065 | struct uvc_fh *handle = file->private_data; |
| 1066 | struct uvc_streaming *stream = handle->stream; |
| 1067 | |
| 1068 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n"); |
| 1069 | |
| 1070 | return uvc_queue_get_unmapped_area(&stream->queue, pgoff); |
| 1071 | } |
| 1072 | #endif |
| 1073 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1074 | const struct v4l2_file_operations uvc_fops = { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1075 | .owner = THIS_MODULE, |
| 1076 | .open = uvc_v4l2_open, |
| 1077 | .release = uvc_v4l2_release, |
Laurent Pinchart | 673eb9f | 2010-11-21 16:54:56 -0300 | [diff] [blame] | 1078 | .unlocked_ioctl = uvc_v4l2_ioctl, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1079 | .read = uvc_v4l2_read, |
| 1080 | .mmap = uvc_v4l2_mmap, |
| 1081 | .poll = uvc_v4l2_poll, |
Bob Liu | 7296944 | 2011-04-29 07:11:35 -0300 | [diff] [blame] | 1082 | #ifndef CONFIG_MMU |
| 1083 | .get_unmapped_area = uvc_v4l2_get_unmapped_area, |
| 1084 | #endif |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1085 | }; |
Hans Verkuil | f87086e | 2008-07-18 00:50:58 -0300 | [diff] [blame] | 1086 | |