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 | |
Laurent Pinchart | 1a5e4c8 | 2011-12-18 20:45:39 -0300 | [diff] [blame] | 14 | #include <linux/compat.h> |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/version.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 20 | #include <linux/usb.h> |
| 21 | #include <linux/videodev2.h> |
| 22 | #include <linux/vmalloc.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/wait.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 25 | #include <linux/atomic.h> |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 26 | |
| 27 | #include <media/v4l2-common.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 28 | #include <media/v4l2-ioctl.h> |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 29 | |
| 30 | #include "uvcvideo.h" |
| 31 | |
| 32 | /* ------------------------------------------------------------------------ |
Laurent Pinchart | 561474c2 | 2010-02-18 16:38:52 -0300 | [diff] [blame] | 33 | * UVC ioctls |
| 34 | */ |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 35 | static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain, |
Laurent Pinchart | 227bd5b | 2011-07-30 16:19:49 -0300 | [diff] [blame] | 36 | struct uvc_xu_control_mapping *xmap) |
Laurent Pinchart | 561474c2 | 2010-02-18 16:38:52 -0300 | [diff] [blame] | 37 | { |
| 38 | struct uvc_control_mapping *map; |
| 39 | unsigned int size; |
| 40 | int ret; |
| 41 | |
| 42 | map = kzalloc(sizeof *map, GFP_KERNEL); |
| 43 | if (map == NULL) |
| 44 | return -ENOMEM; |
| 45 | |
| 46 | map->id = xmap->id; |
| 47 | memcpy(map->name, xmap->name, sizeof map->name); |
| 48 | memcpy(map->entity, xmap->entity, sizeof map->entity); |
| 49 | map->selector = xmap->selector; |
| 50 | map->size = xmap->size; |
| 51 | map->offset = xmap->offset; |
| 52 | map->v4l2_type = xmap->v4l2_type; |
| 53 | map->data_type = xmap->data_type; |
| 54 | |
| 55 | switch (xmap->v4l2_type) { |
| 56 | case V4L2_CTRL_TYPE_INTEGER: |
| 57 | case V4L2_CTRL_TYPE_BOOLEAN: |
| 58 | case V4L2_CTRL_TYPE_BUTTON: |
| 59 | break; |
| 60 | |
| 61 | case V4L2_CTRL_TYPE_MENU: |
Haogang Chen | 806e23e | 2011-11-29 18:32:25 -0300 | [diff] [blame] | 62 | /* Prevent excessive memory consumption, as well as integer |
| 63 | * overflows. |
| 64 | */ |
| 65 | if (xmap->menu_count == 0 || |
| 66 | xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) { |
| 67 | ret = -EINVAL; |
| 68 | goto done; |
| 69 | } |
| 70 | |
Laurent Pinchart | 561474c2 | 2010-02-18 16:38:52 -0300 | [diff] [blame] | 71 | size = xmap->menu_count * sizeof(*map->menu_info); |
| 72 | map->menu_info = kmalloc(size, GFP_KERNEL); |
| 73 | if (map->menu_info == NULL) { |
| 74 | ret = -ENOMEM; |
| 75 | goto done; |
| 76 | } |
| 77 | |
| 78 | if (copy_from_user(map->menu_info, xmap->menu_info, size)) { |
| 79 | ret = -EFAULT; |
| 80 | goto done; |
| 81 | } |
| 82 | |
| 83 | map->menu_count = xmap->menu_count; |
| 84 | break; |
| 85 | |
| 86 | default: |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 87 | uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type " |
| 88 | "%u.\n", xmap->v4l2_type); |
Mauro Carvalho Chehab | 7a286cc | 2011-06-26 10:18:03 -0300 | [diff] [blame] | 89 | ret = -ENOTTY; |
Laurent Pinchart | 561474c2 | 2010-02-18 16:38:52 -0300 | [diff] [blame] | 90 | goto done; |
| 91 | } |
| 92 | |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 93 | ret = uvc_ctrl_add_mapping(chain, map); |
Laurent Pinchart | 561474c2 | 2010-02-18 16:38:52 -0300 | [diff] [blame] | 94 | |
| 95 | done: |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 96 | kfree(map->menu_info); |
| 97 | kfree(map); |
Laurent Pinchart | 561474c2 | 2010-02-18 16:38:52 -0300 | [diff] [blame] | 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | /* ------------------------------------------------------------------------ |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 103 | * V4L2 interface |
| 104 | */ |
| 105 | |
| 106 | /* |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 107 | * Find the frame interval closest to the requested frame interval for the |
| 108 | * given frame format and size. This should be done by the device as part of |
| 109 | * the Video Probe and Commit negotiation, but some hardware don't implement |
| 110 | * that feature. |
| 111 | */ |
| 112 | static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval) |
| 113 | { |
| 114 | unsigned int i; |
| 115 | |
| 116 | if (frame->bFrameIntervalType) { |
| 117 | __u32 best = -1, dist; |
| 118 | |
| 119 | for (i = 0; i < frame->bFrameIntervalType; ++i) { |
| 120 | dist = interval > frame->dwFrameInterval[i] |
| 121 | ? interval - frame->dwFrameInterval[i] |
| 122 | : frame->dwFrameInterval[i] - interval; |
| 123 | |
| 124 | if (dist > best) |
| 125 | break; |
| 126 | |
| 127 | best = dist; |
| 128 | } |
| 129 | |
| 130 | interval = frame->dwFrameInterval[i-1]; |
| 131 | } else { |
| 132 | const __u32 min = frame->dwFrameInterval[0]; |
| 133 | const __u32 max = frame->dwFrameInterval[1]; |
| 134 | const __u32 step = frame->dwFrameInterval[2]; |
| 135 | |
| 136 | interval = min + (interval - min + step/2) / step * step; |
| 137 | if (interval > max) |
| 138 | interval = max; |
| 139 | } |
| 140 | |
| 141 | return interval; |
| 142 | } |
| 143 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 144 | static int uvc_v4l2_try_format(struct uvc_streaming *stream, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 145 | struct v4l2_format *fmt, struct uvc_streaming_control *probe, |
| 146 | struct uvc_format **uvc_format, struct uvc_frame **uvc_frame) |
| 147 | { |
| 148 | struct uvc_format *format = NULL; |
| 149 | struct uvc_frame *frame = NULL; |
| 150 | __u16 rw, rh; |
| 151 | unsigned int d, maxd; |
| 152 | unsigned int i; |
| 153 | __u32 interval; |
| 154 | int ret = 0; |
| 155 | __u8 *fcc; |
| 156 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 157 | if (fmt->type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 158 | return -EINVAL; |
| 159 | |
| 160 | fcc = (__u8 *)&fmt->fmt.pix.pixelformat; |
| 161 | uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n", |
| 162 | fmt->fmt.pix.pixelformat, |
| 163 | fcc[0], fcc[1], fcc[2], fcc[3], |
| 164 | fmt->fmt.pix.width, fmt->fmt.pix.height); |
| 165 | |
| 166 | /* Check if the hardware supports the requested format. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 167 | for (i = 0; i < stream->nformats; ++i) { |
| 168 | format = &stream->format[i]; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 169 | if (format->fcc == fmt->fmt.pix.pixelformat) |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) { |
| 174 | uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n", |
| 175 | fmt->fmt.pix.pixelformat); |
| 176 | return -EINVAL; |
| 177 | } |
| 178 | |
| 179 | /* Find the closest image size. The distance between image sizes is |
| 180 | * the size in pixels of the non-overlapping regions between the |
| 181 | * requested size and the frame-specified size. |
| 182 | */ |
| 183 | rw = fmt->fmt.pix.width; |
| 184 | rh = fmt->fmt.pix.height; |
| 185 | maxd = (unsigned int)-1; |
| 186 | |
| 187 | for (i = 0; i < format->nframes; ++i) { |
| 188 | __u16 w = format->frame[i].wWidth; |
| 189 | __u16 h = format->frame[i].wHeight; |
| 190 | |
| 191 | d = min(w, rw) * min(h, rh); |
| 192 | d = w*h + rw*rh - 2*d; |
| 193 | if (d < maxd) { |
| 194 | maxd = d; |
| 195 | frame = &format->frame[i]; |
| 196 | } |
| 197 | |
| 198 | if (maxd == 0) |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | if (frame == NULL) { |
| 203 | uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n", |
| 204 | fmt->fmt.pix.width, fmt->fmt.pix.height); |
| 205 | return -EINVAL; |
| 206 | } |
| 207 | |
| 208 | /* Use the default frame interval. */ |
| 209 | interval = frame->dwDefaultFrameInterval; |
| 210 | uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us " |
| 211 | "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval, |
| 212 | (100000000/interval)%10); |
| 213 | |
| 214 | /* Set the format index, frame index and frame interval. */ |
| 215 | memset(probe, 0, sizeof *probe); |
| 216 | probe->bmHint = 1; /* dwFrameInterval */ |
| 217 | probe->bFormatIndex = format->index; |
| 218 | probe->bFrameIndex = frame->bFrameIndex; |
| 219 | probe->dwFrameInterval = uvc_try_frame_interval(frame, interval); |
| 220 | /* Some webcams stall the probe control set request when the |
| 221 | * dwMaxVideoFrameSize field is set to zero. The UVC specification |
| 222 | * clearly states that the field is read-only from the host, so this |
| 223 | * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by |
| 224 | * the webcam to work around the problem. |
| 225 | * |
| 226 | * The workaround could probably be enabled for all webcams, so the |
| 227 | * quirk can be removed if needed. It's currently useful to detect |
| 228 | * webcam bugs and fix them before they hit the market (providing |
| 229 | * developers test their webcams with the Linux driver as well as with |
| 230 | * the Windows driver). |
| 231 | */ |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 232 | mutex_lock(&stream->mutex); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 233 | if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 234 | probe->dwMaxVideoFrameSize = |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 235 | stream->ctrl.dwMaxVideoFrameSize; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 236 | |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 237 | /* Probe the device. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 238 | ret = uvc_probe_video(stream, probe); |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 239 | mutex_unlock(&stream->mutex); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 240 | if (ret < 0) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 241 | goto done; |
| 242 | |
| 243 | fmt->fmt.pix.width = frame->wWidth; |
| 244 | fmt->fmt.pix.height = frame->wHeight; |
| 245 | fmt->fmt.pix.field = V4L2_FIELD_NONE; |
| 246 | fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8; |
| 247 | fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize; |
| 248 | fmt->fmt.pix.colorspace = format->colorspace; |
| 249 | fmt->fmt.pix.priv = 0; |
| 250 | |
| 251 | if (uvc_format != NULL) |
| 252 | *uvc_format = format; |
| 253 | if (uvc_frame != NULL) |
| 254 | *uvc_frame = frame; |
| 255 | |
| 256 | done: |
| 257 | return ret; |
| 258 | } |
| 259 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 260 | static int uvc_v4l2_get_format(struct uvc_streaming *stream, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 261 | struct v4l2_format *fmt) |
| 262 | { |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 263 | struct uvc_format *format; |
| 264 | struct uvc_frame *frame; |
| 265 | int ret = 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 266 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 267 | if (fmt->type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 268 | return -EINVAL; |
| 269 | |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 270 | mutex_lock(&stream->mutex); |
| 271 | format = stream->cur_format; |
| 272 | frame = stream->cur_frame; |
| 273 | |
| 274 | if (format == NULL || frame == NULL) { |
| 275 | ret = -EINVAL; |
| 276 | goto done; |
| 277 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 278 | |
| 279 | fmt->fmt.pix.pixelformat = format->fcc; |
| 280 | fmt->fmt.pix.width = frame->wWidth; |
| 281 | fmt->fmt.pix.height = frame->wHeight; |
| 282 | fmt->fmt.pix.field = V4L2_FIELD_NONE; |
| 283 | fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 284 | fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 285 | fmt->fmt.pix.colorspace = format->colorspace; |
| 286 | fmt->fmt.pix.priv = 0; |
| 287 | |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 288 | done: |
| 289 | mutex_unlock(&stream->mutex); |
| 290 | return ret; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 291 | } |
| 292 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 293 | static int uvc_v4l2_set_format(struct uvc_streaming *stream, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 294 | struct v4l2_format *fmt) |
| 295 | { |
| 296 | struct uvc_streaming_control probe; |
| 297 | struct uvc_format *format; |
| 298 | struct uvc_frame *frame; |
| 299 | int ret; |
| 300 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 301 | if (fmt->type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 302 | return -EINVAL; |
| 303 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 304 | ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 305 | if (ret < 0) |
| 306 | return ret; |
| 307 | |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 308 | mutex_lock(&stream->mutex); |
| 309 | |
| 310 | if (uvc_queue_allocated(&stream->queue)) { |
| 311 | ret = -EBUSY; |
| 312 | goto done; |
| 313 | } |
| 314 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 315 | memcpy(&stream->ctrl, &probe, sizeof probe); |
| 316 | stream->cur_format = format; |
| 317 | stream->cur_frame = frame; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 318 | |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 319 | done: |
| 320 | mutex_unlock(&stream->mutex); |
| 321 | return ret; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 322 | } |
| 323 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 324 | static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 325 | struct v4l2_streamparm *parm) |
| 326 | { |
| 327 | uint32_t numerator, denominator; |
| 328 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 329 | if (parm->type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 330 | return -EINVAL; |
| 331 | |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 332 | mutex_lock(&stream->mutex); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 333 | numerator = stream->ctrl.dwFrameInterval; |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 334 | mutex_unlock(&stream->mutex); |
| 335 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 336 | denominator = 10000000; |
| 337 | uvc_simplify_fraction(&numerator, &denominator, 8, 333); |
| 338 | |
| 339 | memset(parm, 0, sizeof *parm); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 340 | parm->type = stream->type; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 341 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 342 | if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 343 | parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; |
| 344 | parm->parm.capture.capturemode = 0; |
| 345 | parm->parm.capture.timeperframe.numerator = numerator; |
| 346 | parm->parm.capture.timeperframe.denominator = denominator; |
| 347 | parm->parm.capture.extendedmode = 0; |
| 348 | parm->parm.capture.readbuffers = 0; |
| 349 | } else { |
| 350 | parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME; |
| 351 | parm->parm.output.outputmode = 0; |
| 352 | parm->parm.output.timeperframe.numerator = numerator; |
| 353 | parm->parm.output.timeperframe.denominator = denominator; |
| 354 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 359 | static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 360 | struct v4l2_streamparm *parm) |
| 361 | { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 362 | struct uvc_streaming_control probe; |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 363 | struct v4l2_fract timeperframe; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 364 | uint32_t interval; |
| 365 | int ret; |
| 366 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 367 | if (parm->type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 368 | return -EINVAL; |
| 369 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 370 | if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 371 | timeperframe = parm->parm.capture.timeperframe; |
| 372 | else |
| 373 | timeperframe = parm->parm.output.timeperframe; |
| 374 | |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 375 | interval = uvc_fraction_to_interval(timeperframe.numerator, |
| 376 | timeperframe.denominator); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 377 | 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] | 378 | timeperframe.numerator, timeperframe.denominator, interval); |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 379 | |
| 380 | mutex_lock(&stream->mutex); |
| 381 | |
| 382 | if (uvc_queue_streaming(&stream->queue)) { |
| 383 | mutex_unlock(&stream->mutex); |
| 384 | return -EBUSY; |
| 385 | } |
| 386 | |
| 387 | memcpy(&probe, &stream->ctrl, sizeof probe); |
| 388 | probe.dwFrameInterval = |
| 389 | uvc_try_frame_interval(stream->cur_frame, interval); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 390 | |
| 391 | /* Probe the device with the new settings. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 392 | ret = uvc_probe_video(stream, &probe); |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 393 | if (ret < 0) { |
| 394 | mutex_unlock(&stream->mutex); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 395 | return ret; |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 396 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 397 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 398 | memcpy(&stream->ctrl, &probe, sizeof probe); |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 399 | mutex_unlock(&stream->mutex); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 400 | |
| 401 | /* Return the actual frame period. */ |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 402 | timeperframe.numerator = probe.dwFrameInterval; |
| 403 | timeperframe.denominator = 10000000; |
| 404 | uvc_simplify_fraction(&timeperframe.numerator, |
| 405 | &timeperframe.denominator, 8, 333); |
| 406 | |
| 407 | if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 408 | parm->parm.capture.timeperframe = timeperframe; |
| 409 | else |
| 410 | parm->parm.output.timeperframe = timeperframe; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /* ------------------------------------------------------------------------ |
| 416 | * Privilege management |
| 417 | */ |
| 418 | |
| 419 | /* |
| 420 | * Privilege management is the multiple-open implementation basis. The current |
| 421 | * implementation is completely transparent for the end-user and doesn't |
| 422 | * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls. |
| 423 | * Those ioctls enable finer control on the device (by making possible for a |
| 424 | * user to request exclusive access to a device), but are not mature yet. |
| 425 | * Switching to the V4L2 priority mechanism might be considered in the future |
| 426 | * if this situation changes. |
| 427 | * |
| 428 | * Each open instance of a UVC device can either be in a privileged or |
| 429 | * 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] | 430 | * a given time. Trying to perform an operation that requires privileges will |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 431 | * automatically acquire the required privileges if possible, or return -EBUSY |
Laurent Pinchart | 1a969d9 | 2009-09-02 03:12:26 -0300 | [diff] [blame] | 432 | * otherwise. Privileges are dismissed when closing the instance or when |
| 433 | * freeing the video buffers using VIDIOC_REQBUFS. |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 434 | * |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 435 | * Operations that require privileges are: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 436 | * |
| 437 | * - VIDIOC_S_INPUT |
| 438 | * - VIDIOC_S_PARM |
| 439 | * - VIDIOC_S_FMT |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 440 | * - VIDIOC_REQBUFS |
| 441 | */ |
| 442 | static int uvc_acquire_privileges(struct uvc_fh *handle) |
| 443 | { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 444 | /* Always succeed if the handle is already privileged. */ |
| 445 | if (handle->state == UVC_HANDLE_ACTIVE) |
| 446 | return 0; |
| 447 | |
| 448 | /* Check if the device already has a privileged handle. */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 449 | if (atomic_inc_return(&handle->stream->active) != 1) { |
| 450 | atomic_dec(&handle->stream->active); |
Laurent Pinchart | 716fdee | 2009-09-29 21:07:19 -0300 | [diff] [blame] | 451 | return -EBUSY; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | handle->state = UVC_HANDLE_ACTIVE; |
Laurent Pinchart | 716fdee | 2009-09-29 21:07:19 -0300 | [diff] [blame] | 455 | return 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | static void uvc_dismiss_privileges(struct uvc_fh *handle) |
| 459 | { |
| 460 | if (handle->state == UVC_HANDLE_ACTIVE) |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 461 | atomic_dec(&handle->stream->active); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 462 | |
| 463 | handle->state = UVC_HANDLE_PASSIVE; |
| 464 | } |
| 465 | |
| 466 | static int uvc_has_privileges(struct uvc_fh *handle) |
| 467 | { |
| 468 | return handle->state == UVC_HANDLE_ACTIVE; |
| 469 | } |
| 470 | |
| 471 | /* ------------------------------------------------------------------------ |
| 472 | * V4L2 file operations |
| 473 | */ |
| 474 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 475 | static int uvc_v4l2_open(struct file *file) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 476 | { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 477 | struct uvc_streaming *stream; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 478 | struct uvc_fh *handle; |
| 479 | int ret = 0; |
| 480 | |
| 481 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n"); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 482 | stream = video_drvdata(file); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 483 | |
Laurent Pinchart | 716fdee | 2009-09-29 21:07:19 -0300 | [diff] [blame] | 484 | if (stream->dev->state & UVC_DEV_DISCONNECTED) |
| 485 | return -ENODEV; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 486 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 487 | ret = usb_autopm_get_interface(stream->dev->intf); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 488 | if (ret < 0) |
Laurent Pinchart | 716fdee | 2009-09-29 21:07:19 -0300 | [diff] [blame] | 489 | return ret; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 490 | |
| 491 | /* Create the device handle. */ |
| 492 | handle = kzalloc(sizeof *handle, GFP_KERNEL); |
| 493 | if (handle == NULL) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 494 | usb_autopm_put_interface(stream->dev->intf); |
Laurent Pinchart | 716fdee | 2009-09-29 21:07:19 -0300 | [diff] [blame] | 495 | return -ENOMEM; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 496 | } |
| 497 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 498 | if (atomic_inc_return(&stream->dev->users) == 1) { |
| 499 | ret = uvc_status_start(stream->dev); |
| 500 | if (ret < 0) { |
| 501 | usb_autopm_put_interface(stream->dev->intf); |
| 502 | atomic_dec(&stream->dev->users); |
Laurent Pinchart | 04a37e0 | 2009-05-19 10:08:03 -0300 | [diff] [blame] | 503 | kfree(handle); |
Laurent Pinchart | 716fdee | 2009-09-29 21:07:19 -0300 | [diff] [blame] | 504 | return ret; |
Laurent Pinchart | 04a37e0 | 2009-05-19 10:08:03 -0300 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 508 | handle->chain = stream->chain; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 509 | handle->stream = stream; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 510 | handle->state = UVC_HANDLE_PASSIVE; |
| 511 | file->private_data = handle; |
| 512 | |
Laurent Pinchart | 716fdee | 2009-09-29 21:07:19 -0300 | [diff] [blame] | 513 | return 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 514 | } |
| 515 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 516 | static int uvc_v4l2_release(struct file *file) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 517 | { |
Joe Perches | abf8438 | 2010-07-12 17:50:03 -0300 | [diff] [blame] | 518 | struct uvc_fh *handle = file->private_data; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 519 | struct uvc_streaming *stream = handle->stream; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 520 | |
| 521 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n"); |
| 522 | |
| 523 | /* Only free resources if this is a privileged handle. */ |
| 524 | if (uvc_has_privileges(handle)) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 525 | uvc_video_enable(stream, 0); |
Laurent Pinchart | 6998b6f | 2011-10-24 11:53:59 -0300 | [diff] [blame] | 526 | uvc_free_buffers(&stream->queue); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* Release the file handle. */ |
| 530 | uvc_dismiss_privileges(handle); |
| 531 | kfree(handle); |
| 532 | file->private_data = NULL; |
| 533 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 534 | if (atomic_dec_return(&stream->dev->users) == 0) |
| 535 | uvc_status_stop(stream->dev); |
Laurent Pinchart | 04a37e0 | 2009-05-19 10:08:03 -0300 | [diff] [blame] | 536 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 537 | usb_autopm_put_interface(stream->dev->intf); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 538 | return 0; |
| 539 | } |
| 540 | |
Hans Verkuil | 069b747 | 2008-12-30 07:04:34 -0300 | [diff] [blame] | 541 | 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] | 542 | { |
| 543 | struct video_device *vdev = video_devdata(file); |
Joe Perches | abf8438 | 2010-07-12 17:50:03 -0300 | [diff] [blame] | 544 | struct uvc_fh *handle = file->private_data; |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 545 | struct uvc_video_chain *chain = handle->chain; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 546 | struct uvc_streaming *stream = handle->stream; |
Hans Verkuil | 069b747 | 2008-12-30 07:04:34 -0300 | [diff] [blame] | 547 | long ret = 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 548 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 549 | switch (cmd) { |
| 550 | /* Query capabilities */ |
| 551 | case VIDIOC_QUERYCAP: |
| 552 | { |
| 553 | struct v4l2_capability *cap = arg; |
| 554 | |
| 555 | memset(cap, 0, sizeof *cap); |
Laurent Pinchart | d0ebf30 | 2009-01-08 14:05:11 -0300 | [diff] [blame] | 556 | strlcpy(cap->driver, "uvcvideo", sizeof cap->driver); |
| 557 | strlcpy(cap->card, vdev->name, sizeof cap->card); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 558 | usb_make_path(stream->dev->udev, |
Thierry MERLE | b12049a | 2009-01-27 16:53:23 -0300 | [diff] [blame] | 559 | cap->bus_info, sizeof(cap->bus_info)); |
Mauro Carvalho Chehab | fd3e582 | 2011-06-25 13:50:37 -0300 | [diff] [blame] | 560 | cap->version = LINUX_VERSION_CODE; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 561 | if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
Laurent Pinchart | ff92420 | 2008-12-28 22:32:29 -0300 | [diff] [blame] | 562 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
| 563 | | V4L2_CAP_STREAMING; |
| 564 | else |
| 565 | cap->capabilities = V4L2_CAP_VIDEO_OUTPUT |
| 566 | | V4L2_CAP_STREAMING; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 567 | break; |
| 568 | } |
| 569 | |
| 570 | /* Get, Set & Query control */ |
| 571 | case VIDIOC_QUERYCTRL: |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 572 | return uvc_query_v4l2_ctrl(chain, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 573 | |
| 574 | case VIDIOC_G_CTRL: |
| 575 | { |
| 576 | struct v4l2_control *ctrl = arg; |
| 577 | struct v4l2_ext_control xctrl; |
| 578 | |
| 579 | memset(&xctrl, 0, sizeof xctrl); |
| 580 | xctrl.id = ctrl->id; |
| 581 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 582 | ret = uvc_ctrl_begin(chain); |
| 583 | if (ret < 0) |
Robert Krakora | 1fcbcc4 | 2009-06-12 13:51:03 -0300 | [diff] [blame] | 584 | return ret; |
| 585 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 586 | ret = uvc_ctrl_get(chain, &xctrl); |
| 587 | uvc_ctrl_rollback(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 588 | if (ret >= 0) |
| 589 | ctrl->value = xctrl.value; |
| 590 | break; |
| 591 | } |
| 592 | |
| 593 | case VIDIOC_S_CTRL: |
| 594 | { |
| 595 | struct v4l2_control *ctrl = arg; |
| 596 | struct v4l2_ext_control xctrl; |
| 597 | |
| 598 | memset(&xctrl, 0, sizeof xctrl); |
| 599 | xctrl.id = ctrl->id; |
| 600 | xctrl.value = ctrl->value; |
| 601 | |
Laurent Pinchart | 8d55662 | 2010-02-04 21:43:37 -0300 | [diff] [blame] | 602 | ret = uvc_ctrl_begin(chain); |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 603 | if (ret < 0) |
Robert Krakora | 1fcbcc4 | 2009-06-12 13:51:03 -0300 | [diff] [blame] | 604 | return ret; |
| 605 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 606 | ret = uvc_ctrl_set(chain, &xctrl); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 607 | if (ret < 0) { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 608 | uvc_ctrl_rollback(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 609 | return ret; |
| 610 | } |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 611 | ret = uvc_ctrl_commit(chain); |
Laurent Pinchart | e54532e | 2010-01-23 07:07:53 -0300 | [diff] [blame] | 612 | if (ret == 0) |
| 613 | ctrl->value = xctrl.value; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 614 | break; |
| 615 | } |
| 616 | |
| 617 | case VIDIOC_QUERYMENU: |
Laurent Pinchart | 23d9f3e | 2010-11-21 07:58:54 -0300 | [diff] [blame] | 618 | return uvc_query_v4l2_menu(chain, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 619 | |
| 620 | case VIDIOC_G_EXT_CTRLS: |
| 621 | { |
| 622 | struct v4l2_ext_controls *ctrls = arg; |
| 623 | struct v4l2_ext_control *ctrl = ctrls->controls; |
| 624 | unsigned int i; |
| 625 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 626 | ret = uvc_ctrl_begin(chain); |
| 627 | if (ret < 0) |
Robert Krakora | 1fcbcc4 | 2009-06-12 13:51:03 -0300 | [diff] [blame] | 628 | return ret; |
| 629 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 630 | for (i = 0; i < ctrls->count; ++ctrl, ++i) { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 631 | ret = uvc_ctrl_get(chain, ctrl); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 632 | if (ret < 0) { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 633 | uvc_ctrl_rollback(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 634 | ctrls->error_idx = i; |
| 635 | return ret; |
| 636 | } |
| 637 | } |
| 638 | ctrls->error_idx = 0; |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 639 | ret = uvc_ctrl_rollback(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 640 | break; |
| 641 | } |
| 642 | |
| 643 | case VIDIOC_S_EXT_CTRLS: |
| 644 | case VIDIOC_TRY_EXT_CTRLS: |
| 645 | { |
| 646 | struct v4l2_ext_controls *ctrls = arg; |
| 647 | struct v4l2_ext_control *ctrl = ctrls->controls; |
| 648 | unsigned int i; |
| 649 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 650 | ret = uvc_ctrl_begin(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 651 | if (ret < 0) |
| 652 | return ret; |
| 653 | |
| 654 | for (i = 0; i < ctrls->count; ++ctrl, ++i) { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 655 | ret = uvc_ctrl_set(chain, ctrl); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 656 | if (ret < 0) { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 657 | uvc_ctrl_rollback(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 658 | ctrls->error_idx = i; |
| 659 | return ret; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | ctrls->error_idx = 0; |
| 664 | |
| 665 | if (cmd == VIDIOC_S_EXT_CTRLS) |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 666 | ret = uvc_ctrl_commit(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 667 | else |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 668 | ret = uvc_ctrl_rollback(chain); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 669 | break; |
| 670 | } |
| 671 | |
| 672 | /* Get, Set & Enum input */ |
| 673 | case VIDIOC_ENUMINPUT: |
| 674 | { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 675 | const struct uvc_entity *selector = chain->selector; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 676 | struct v4l2_input *input = arg; |
| 677 | struct uvc_entity *iterm = NULL; |
| 678 | u32 index = input->index; |
| 679 | int pin = 0; |
| 680 | |
| 681 | if (selector == NULL || |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 682 | (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 683 | if (index != 0) |
| 684 | return -EINVAL; |
Laurent Pinchart | 6241d8c | 2009-11-25 12:00:22 -0300 | [diff] [blame] | 685 | list_for_each_entry(iterm, &chain->entities, chain) { |
| 686 | if (UVC_ENTITY_IS_ITERM(iterm)) |
| 687 | break; |
| 688 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 689 | pin = iterm->id; |
Laurent Pinchart | 8ca5a63 | 2009-11-25 12:00:30 -0300 | [diff] [blame] | 690 | } else if (pin < selector->bNrInPins) { |
| 691 | pin = selector->baSourceID[index]; |
Laurent Pinchart | 6241d8c | 2009-11-25 12:00:22 -0300 | [diff] [blame] | 692 | list_for_each_entry(iterm, &chain->entities, chain) { |
| 693 | if (!UVC_ENTITY_IS_ITERM(iterm)) |
| 694 | continue; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 695 | if (iterm->id == pin) |
| 696 | break; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | if (iterm == NULL || iterm->id != pin) |
| 701 | return -EINVAL; |
| 702 | |
| 703 | memset(input, 0, sizeof *input); |
| 704 | input->index = index; |
Laurent Pinchart | d0ebf30 | 2009-01-08 14:05:11 -0300 | [diff] [blame] | 705 | strlcpy(input->name, iterm->name, sizeof input->name); |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 706 | if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 707 | input->type = V4L2_INPUT_TYPE_CAMERA; |
| 708 | break; |
| 709 | } |
| 710 | |
| 711 | case VIDIOC_G_INPUT: |
| 712 | { |
| 713 | u8 input; |
| 714 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 715 | if (chain->selector == NULL || |
| 716 | (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 717 | *(int *)arg = 0; |
| 718 | break; |
| 719 | } |
| 720 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 721 | ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, |
| 722 | chain->selector->id, chain->dev->intfnum, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 723 | UVC_SU_INPUT_SELECT_CONTROL, &input, 1); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 724 | if (ret < 0) |
| 725 | return ret; |
| 726 | |
| 727 | *(int *)arg = input - 1; |
| 728 | break; |
| 729 | } |
| 730 | |
| 731 | case VIDIOC_S_INPUT: |
| 732 | { |
Márton Németh | 9086c7b99 | 2009-04-15 09:01:58 -0300 | [diff] [blame] | 733 | u32 input = *(u32 *)arg + 1; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 734 | |
| 735 | if ((ret = uvc_acquire_privileges(handle)) < 0) |
| 736 | return ret; |
| 737 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 738 | if (chain->selector == NULL || |
| 739 | (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 740 | if (input != 1) |
| 741 | return -EINVAL; |
| 742 | break; |
| 743 | } |
| 744 | |
Laurent Pinchart | 8ca5a63 | 2009-11-25 12:00:30 -0300 | [diff] [blame] | 745 | if (input == 0 || input > chain->selector->bNrInPins) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 746 | return -EINVAL; |
| 747 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 748 | return uvc_query_ctrl(chain->dev, UVC_SET_CUR, |
| 749 | chain->selector->id, chain->dev->intfnum, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 750 | UVC_SU_INPUT_SELECT_CONTROL, &input, 1); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /* Try, Get, Set & Enum format */ |
| 754 | case VIDIOC_ENUM_FMT: |
| 755 | { |
| 756 | struct v4l2_fmtdesc *fmt = arg; |
| 757 | struct uvc_format *format; |
Márton Németh | 8bbd90c | 2009-03-27 11:13:57 -0300 | [diff] [blame] | 758 | enum v4l2_buf_type type = fmt->type; |
| 759 | __u32 index = fmt->index; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 760 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 761 | if (fmt->type != stream->type || |
| 762 | fmt->index >= stream->nformats) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 763 | return -EINVAL; |
| 764 | |
Márton Németh | 8bbd90c | 2009-03-27 11:13:57 -0300 | [diff] [blame] | 765 | memset(fmt, 0, sizeof(*fmt)); |
| 766 | fmt->index = index; |
| 767 | fmt->type = type; |
| 768 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 769 | format = &stream->format[fmt->index]; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 770 | fmt->flags = 0; |
| 771 | if (format->flags & UVC_FMT_FLAG_COMPRESSED) |
| 772 | fmt->flags |= V4L2_FMT_FLAG_COMPRESSED; |
Laurent Pinchart | d0ebf30 | 2009-01-08 14:05:11 -0300 | [diff] [blame] | 773 | strlcpy(fmt->description, format->name, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 774 | sizeof fmt->description); |
| 775 | fmt->description[sizeof fmt->description - 1] = 0; |
| 776 | fmt->pixelformat = format->fcc; |
| 777 | break; |
| 778 | } |
| 779 | |
| 780 | case VIDIOC_TRY_FMT: |
| 781 | { |
| 782 | struct uvc_streaming_control probe; |
| 783 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 784 | return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | case VIDIOC_S_FMT: |
| 788 | if ((ret = uvc_acquire_privileges(handle)) < 0) |
| 789 | return ret; |
| 790 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 791 | return uvc_v4l2_set_format(stream, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 792 | |
| 793 | case VIDIOC_G_FMT: |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 794 | return uvc_v4l2_get_format(stream, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 795 | |
| 796 | /* Frame size enumeration */ |
| 797 | case VIDIOC_ENUM_FRAMESIZES: |
| 798 | { |
| 799 | struct v4l2_frmsizeenum *fsize = arg; |
| 800 | struct uvc_format *format = NULL; |
| 801 | struct uvc_frame *frame; |
| 802 | int i; |
| 803 | |
| 804 | /* Look for the given pixel format */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 805 | for (i = 0; i < stream->nformats; i++) { |
| 806 | if (stream->format[i].fcc == |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 807 | fsize->pixel_format) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 808 | format = &stream->format[i]; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 809 | break; |
| 810 | } |
| 811 | } |
| 812 | if (format == NULL) |
| 813 | return -EINVAL; |
| 814 | |
| 815 | if (fsize->index >= format->nframes) |
| 816 | return -EINVAL; |
| 817 | |
| 818 | frame = &format->frame[fsize->index]; |
| 819 | fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; |
| 820 | fsize->discrete.width = frame->wWidth; |
| 821 | fsize->discrete.height = frame->wHeight; |
| 822 | break; |
| 823 | } |
| 824 | |
| 825 | /* Frame interval enumeration */ |
| 826 | case VIDIOC_ENUM_FRAMEINTERVALS: |
| 827 | { |
| 828 | struct v4l2_frmivalenum *fival = arg; |
| 829 | struct uvc_format *format = NULL; |
| 830 | struct uvc_frame *frame = NULL; |
| 831 | int i; |
| 832 | |
| 833 | /* Look for the given pixel format and frame size */ |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 834 | for (i = 0; i < stream->nformats; i++) { |
| 835 | if (stream->format[i].fcc == |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 836 | fival->pixel_format) { |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 837 | format = &stream->format[i]; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 838 | break; |
| 839 | } |
| 840 | } |
| 841 | if (format == NULL) |
| 842 | return -EINVAL; |
| 843 | |
| 844 | for (i = 0; i < format->nframes; i++) { |
| 845 | if (format->frame[i].wWidth == fival->width && |
| 846 | format->frame[i].wHeight == fival->height) { |
| 847 | frame = &format->frame[i]; |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | if (frame == NULL) |
| 852 | return -EINVAL; |
| 853 | |
| 854 | if (frame->bFrameIntervalType) { |
| 855 | if (fival->index >= frame->bFrameIntervalType) |
| 856 | return -EINVAL; |
| 857 | |
| 858 | fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; |
| 859 | fival->discrete.numerator = |
| 860 | frame->dwFrameInterval[fival->index]; |
| 861 | fival->discrete.denominator = 10000000; |
| 862 | uvc_simplify_fraction(&fival->discrete.numerator, |
| 863 | &fival->discrete.denominator, 8, 333); |
| 864 | } else { |
| 865 | fival->type = V4L2_FRMIVAL_TYPE_STEPWISE; |
| 866 | fival->stepwise.min.numerator = |
| 867 | frame->dwFrameInterval[0]; |
| 868 | fival->stepwise.min.denominator = 10000000; |
| 869 | fival->stepwise.max.numerator = |
| 870 | frame->dwFrameInterval[1]; |
| 871 | fival->stepwise.max.denominator = 10000000; |
| 872 | fival->stepwise.step.numerator = |
| 873 | frame->dwFrameInterval[2]; |
| 874 | fival->stepwise.step.denominator = 10000000; |
| 875 | uvc_simplify_fraction(&fival->stepwise.min.numerator, |
| 876 | &fival->stepwise.min.denominator, 8, 333); |
| 877 | uvc_simplify_fraction(&fival->stepwise.max.numerator, |
| 878 | &fival->stepwise.max.denominator, 8, 333); |
| 879 | uvc_simplify_fraction(&fival->stepwise.step.numerator, |
| 880 | &fival->stepwise.step.denominator, 8, 333); |
| 881 | } |
| 882 | break; |
| 883 | } |
| 884 | |
| 885 | /* Get & Set streaming parameters */ |
| 886 | case VIDIOC_G_PARM: |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 887 | return uvc_v4l2_get_streamparm(stream, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 888 | |
| 889 | case VIDIOC_S_PARM: |
| 890 | if ((ret = uvc_acquire_privileges(handle)) < 0) |
| 891 | return ret; |
| 892 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 893 | return uvc_v4l2_set_streamparm(stream, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 894 | |
| 895 | /* Cropping and scaling */ |
| 896 | case VIDIOC_CROPCAP: |
| 897 | { |
| 898 | struct v4l2_cropcap *ccap = arg; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 899 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 900 | if (ccap->type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 901 | return -EINVAL; |
| 902 | |
| 903 | ccap->bounds.left = 0; |
| 904 | ccap->bounds.top = 0; |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 905 | |
| 906 | mutex_lock(&stream->mutex); |
| 907 | ccap->bounds.width = stream->cur_frame->wWidth; |
| 908 | ccap->bounds.height = stream->cur_frame->wHeight; |
| 909 | mutex_unlock(&stream->mutex); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 910 | |
| 911 | ccap->defrect = ccap->bounds; |
| 912 | |
| 913 | ccap->pixelaspect.numerator = 1; |
| 914 | ccap->pixelaspect.denominator = 1; |
| 915 | break; |
| 916 | } |
| 917 | |
| 918 | case VIDIOC_G_CROP: |
| 919 | case VIDIOC_S_CROP: |
| 920 | return -EINVAL; |
| 921 | |
| 922 | /* Buffers & streaming */ |
| 923 | case VIDIOC_REQBUFS: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 924 | if ((ret = uvc_acquire_privileges(handle)) < 0) |
| 925 | return ret; |
| 926 | |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 927 | mutex_lock(&stream->mutex); |
Laurent Pinchart | 6998b6f | 2011-10-24 11:53:59 -0300 | [diff] [blame] | 928 | ret = uvc_alloc_buffers(&stream->queue, arg); |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 929 | mutex_unlock(&stream->mutex); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 930 | if (ret < 0) |
| 931 | return ret; |
| 932 | |
Laurent Pinchart | 1a969d9 | 2009-09-02 03:12:26 -0300 | [diff] [blame] | 933 | if (ret == 0) |
| 934 | uvc_dismiss_privileges(handle); |
| 935 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 936 | ret = 0; |
| 937 | break; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 938 | |
| 939 | case VIDIOC_QUERYBUF: |
| 940 | { |
| 941 | struct v4l2_buffer *buf = arg; |
| 942 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 943 | if (!uvc_has_privileges(handle)) |
| 944 | return -EBUSY; |
| 945 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 946 | return uvc_query_buffer(&stream->queue, buf); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | case VIDIOC_QBUF: |
| 950 | if (!uvc_has_privileges(handle)) |
| 951 | return -EBUSY; |
| 952 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 953 | return uvc_queue_buffer(&stream->queue, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 954 | |
| 955 | case VIDIOC_DQBUF: |
| 956 | if (!uvc_has_privileges(handle)) |
| 957 | return -EBUSY; |
| 958 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 959 | return uvc_dequeue_buffer(&stream->queue, arg, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 960 | file->f_flags & O_NONBLOCK); |
| 961 | |
| 962 | case VIDIOC_STREAMON: |
| 963 | { |
| 964 | int *type = arg; |
| 965 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 966 | if (*type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 967 | return -EINVAL; |
| 968 | |
| 969 | if (!uvc_has_privileges(handle)) |
| 970 | return -EBUSY; |
| 971 | |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 972 | mutex_lock(&stream->mutex); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 973 | ret = uvc_video_enable(stream, 1); |
Laurent Pinchart | 6947756 | 2010-11-21 13:36:34 -0300 | [diff] [blame] | 974 | mutex_unlock(&stream->mutex); |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 975 | if (ret < 0) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 976 | return ret; |
| 977 | break; |
| 978 | } |
| 979 | |
| 980 | case VIDIOC_STREAMOFF: |
| 981 | { |
| 982 | int *type = arg; |
| 983 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 984 | if (*type != stream->type) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 985 | return -EINVAL; |
| 986 | |
| 987 | if (!uvc_has_privileges(handle)) |
| 988 | return -EBUSY; |
| 989 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 990 | return uvc_video_enable(stream, 0); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /* Analog video standards make no sense for digital cameras. */ |
| 994 | case VIDIOC_ENUMSTD: |
| 995 | case VIDIOC_QUERYSTD: |
| 996 | case VIDIOC_G_STD: |
| 997 | case VIDIOC_S_STD: |
| 998 | |
| 999 | case VIDIOC_OVERLAY: |
| 1000 | |
| 1001 | case VIDIOC_ENUMAUDIO: |
| 1002 | case VIDIOC_ENUMAUDOUT: |
| 1003 | |
| 1004 | case VIDIOC_ENUMOUTPUT: |
| 1005 | uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd); |
| 1006 | return -EINVAL; |
| 1007 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1008 | case UVCIOC_CTRL_MAP: |
Laurent Pinchart | 227bd5b | 2011-07-30 16:19:49 -0300 | [diff] [blame] | 1009 | return uvc_ioctl_ctrl_map(chain, arg); |
Martin Rubli | fe78d18 | 2010-10-02 19:10:16 -0300 | [diff] [blame] | 1010 | |
| 1011 | case UVCIOC_CTRL_QUERY: |
| 1012 | return uvc_xu_ctrl_query(chain, arg); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1013 | |
| 1014 | default: |
Hans Verkuil | 08af245 | 2010-12-24 10:33:19 -0300 | [diff] [blame] | 1015 | uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd); |
Laurent Pinchart | a3ec69b | 2011-12-18 20:22:50 -0300 | [diff] [blame] | 1016 | return -ENOTTY; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | return ret; |
| 1020 | } |
| 1021 | |
Hans Verkuil | 069b747 | 2008-12-30 07:04:34 -0300 | [diff] [blame] | 1022 | static long uvc_v4l2_ioctl(struct file *file, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1023 | unsigned int cmd, unsigned long arg) |
| 1024 | { |
Laurent Pinchart | 308bb52 | 2008-11-17 18:32:11 -0300 | [diff] [blame] | 1025 | if (uvc_trace_param & UVC_TRACE_IOCTL) { |
| 1026 | uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl("); |
| 1027 | v4l_printk_ioctl(cmd); |
| 1028 | printk(")\n"); |
| 1029 | } |
| 1030 | |
Hans Verkuil | f473bf7 | 2008-11-01 08:25:11 -0300 | [diff] [blame] | 1031 | return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1032 | } |
| 1033 | |
Laurent Pinchart | 1a5e4c8 | 2011-12-18 20:45:39 -0300 | [diff] [blame] | 1034 | #ifdef CONFIG_COMPAT |
| 1035 | struct uvc_xu_control_mapping32 { |
| 1036 | __u32 id; |
| 1037 | __u8 name[32]; |
| 1038 | __u8 entity[16]; |
| 1039 | __u8 selector; |
| 1040 | |
| 1041 | __u8 size; |
| 1042 | __u8 offset; |
| 1043 | __u32 v4l2_type; |
| 1044 | __u32 data_type; |
| 1045 | |
| 1046 | compat_caddr_t menu_info; |
| 1047 | __u32 menu_count; |
| 1048 | |
| 1049 | __u32 reserved[4]; |
| 1050 | }; |
| 1051 | |
| 1052 | static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp, |
| 1053 | const struct uvc_xu_control_mapping32 __user *up) |
| 1054 | { |
| 1055 | struct uvc_menu_info __user *umenus; |
| 1056 | struct uvc_menu_info __user *kmenus; |
| 1057 | compat_caddr_t p; |
| 1058 | |
| 1059 | if (!access_ok(VERIFY_READ, up, sizeof(*up)) || |
| 1060 | __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) || |
| 1061 | __get_user(kp->menu_count, &up->menu_count)) |
| 1062 | return -EFAULT; |
| 1063 | |
| 1064 | memset(kp->reserved, 0, sizeof(kp->reserved)); |
| 1065 | |
| 1066 | if (kp->menu_count == 0) { |
| 1067 | kp->menu_info = NULL; |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | if (__get_user(p, &up->menu_info)) |
| 1072 | return -EFAULT; |
| 1073 | umenus = compat_ptr(p); |
| 1074 | if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus))) |
| 1075 | return -EFAULT; |
| 1076 | |
| 1077 | kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus)); |
| 1078 | if (kmenus == NULL) |
| 1079 | return -EFAULT; |
| 1080 | kp->menu_info = kmenus; |
| 1081 | |
| 1082 | if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus))) |
| 1083 | return -EFAULT; |
| 1084 | |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp, |
| 1089 | struct uvc_xu_control_mapping32 __user *up) |
| 1090 | { |
| 1091 | struct uvc_menu_info __user *umenus; |
| 1092 | struct uvc_menu_info __user *kmenus = kp->menu_info; |
| 1093 | compat_caddr_t p; |
| 1094 | |
| 1095 | if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) || |
| 1096 | __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) || |
| 1097 | __put_user(kp->menu_count, &up->menu_count)) |
| 1098 | return -EFAULT; |
| 1099 | |
Hans de Goede | 57fb4a4 | 2012-04-08 12:59:48 -0300 | [diff] [blame^] | 1100 | if (__clear_user(up->reserved, sizeof(up->reserved))) |
| 1101 | return -EFAULT; |
Laurent Pinchart | 1a5e4c8 | 2011-12-18 20:45:39 -0300 | [diff] [blame] | 1102 | |
| 1103 | if (kp->menu_count == 0) |
| 1104 | return 0; |
| 1105 | |
| 1106 | if (get_user(p, &up->menu_info)) |
| 1107 | return -EFAULT; |
| 1108 | umenus = compat_ptr(p); |
Laurent Pinchart | 1a5e4c8 | 2011-12-18 20:45:39 -0300 | [diff] [blame] | 1109 | |
| 1110 | if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus))) |
| 1111 | return -EFAULT; |
| 1112 | |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | struct uvc_xu_control_query32 { |
| 1117 | __u8 unit; |
| 1118 | __u8 selector; |
| 1119 | __u8 query; |
| 1120 | __u16 size; |
| 1121 | compat_caddr_t data; |
| 1122 | }; |
| 1123 | |
| 1124 | static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp, |
| 1125 | const struct uvc_xu_control_query32 __user *up) |
| 1126 | { |
| 1127 | u8 __user *udata; |
| 1128 | u8 __user *kdata; |
| 1129 | compat_caddr_t p; |
| 1130 | |
| 1131 | if (!access_ok(VERIFY_READ, up, sizeof(*up)) || |
| 1132 | __copy_from_user(kp, up, offsetof(typeof(*up), data))) |
| 1133 | return -EFAULT; |
| 1134 | |
| 1135 | if (kp->size == 0) { |
| 1136 | kp->data = NULL; |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
| 1140 | if (__get_user(p, &up->data)) |
| 1141 | return -EFAULT; |
| 1142 | udata = compat_ptr(p); |
| 1143 | if (!access_ok(VERIFY_READ, udata, kp->size)) |
| 1144 | return -EFAULT; |
| 1145 | |
| 1146 | kdata = compat_alloc_user_space(kp->size); |
| 1147 | if (kdata == NULL) |
| 1148 | return -EFAULT; |
| 1149 | kp->data = kdata; |
| 1150 | |
| 1151 | if (copy_in_user(kdata, udata, kp->size)) |
| 1152 | return -EFAULT; |
| 1153 | |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
| 1157 | static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp, |
| 1158 | struct uvc_xu_control_query32 __user *up) |
| 1159 | { |
| 1160 | u8 __user *udata; |
| 1161 | u8 __user *kdata = kp->data; |
| 1162 | compat_caddr_t p; |
| 1163 | |
| 1164 | if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) || |
| 1165 | __copy_to_user(up, kp, offsetof(typeof(*up), data))) |
| 1166 | return -EFAULT; |
| 1167 | |
| 1168 | if (kp->size == 0) |
| 1169 | return 0; |
| 1170 | |
| 1171 | if (get_user(p, &up->data)) |
| 1172 | return -EFAULT; |
| 1173 | udata = compat_ptr(p); |
| 1174 | if (!access_ok(VERIFY_READ, udata, kp->size)) |
| 1175 | return -EFAULT; |
| 1176 | |
| 1177 | if (copy_in_user(udata, kdata, kp->size)) |
| 1178 | return -EFAULT; |
| 1179 | |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
| 1183 | #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32) |
| 1184 | #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32) |
| 1185 | |
| 1186 | static long uvc_v4l2_compat_ioctl32(struct file *file, |
| 1187 | unsigned int cmd, unsigned long arg) |
| 1188 | { |
| 1189 | union { |
| 1190 | struct uvc_xu_control_mapping xmap; |
| 1191 | struct uvc_xu_control_query xqry; |
| 1192 | } karg; |
| 1193 | void __user *up = compat_ptr(arg); |
| 1194 | mm_segment_t old_fs; |
| 1195 | long ret; |
| 1196 | |
| 1197 | switch (cmd) { |
| 1198 | case UVCIOC_CTRL_MAP32: |
| 1199 | cmd = UVCIOC_CTRL_MAP; |
| 1200 | ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up); |
| 1201 | break; |
| 1202 | |
| 1203 | case UVCIOC_CTRL_QUERY32: |
| 1204 | cmd = UVCIOC_CTRL_QUERY; |
| 1205 | ret = uvc_v4l2_get_xu_query(&karg.xqry, up); |
| 1206 | break; |
| 1207 | |
| 1208 | default: |
| 1209 | return -ENOIOCTLCMD; |
| 1210 | } |
| 1211 | |
| 1212 | old_fs = get_fs(); |
| 1213 | set_fs(KERNEL_DS); |
| 1214 | ret = uvc_v4l2_ioctl(file, cmd, (unsigned long)&karg); |
| 1215 | set_fs(old_fs); |
| 1216 | |
| 1217 | if (ret < 0) |
| 1218 | return ret; |
| 1219 | |
| 1220 | switch (cmd) { |
| 1221 | case UVCIOC_CTRL_MAP: |
| 1222 | ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up); |
| 1223 | break; |
| 1224 | |
| 1225 | case UVCIOC_CTRL_QUERY: |
| 1226 | ret = uvc_v4l2_put_xu_query(&karg.xqry, up); |
| 1227 | break; |
| 1228 | } |
| 1229 | |
| 1230 | return ret; |
| 1231 | } |
| 1232 | #endif |
| 1233 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1234 | static ssize_t uvc_v4l2_read(struct file *file, char __user *data, |
| 1235 | size_t count, loff_t *ppos) |
| 1236 | { |
| 1237 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n"); |
Laurent Pinchart | 350d640 | 2009-11-04 11:53:49 -0300 | [diff] [blame] | 1238 | return -EINVAL; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1239 | } |
| 1240 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1241 | static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma) |
| 1242 | { |
Joe Perches | abf8438 | 2010-07-12 17:50:03 -0300 | [diff] [blame] | 1243 | struct uvc_fh *handle = file->private_data; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1244 | struct uvc_streaming *stream = handle->stream; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1245 | |
| 1246 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n"); |
| 1247 | |
Laurent Pinchart | 4aa2759 | 2010-11-21 15:18:08 -0300 | [diff] [blame] | 1248 | return uvc_queue_mmap(&stream->queue, vma); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait) |
| 1252 | { |
Joe Perches | abf8438 | 2010-07-12 17:50:03 -0300 | [diff] [blame] | 1253 | struct uvc_fh *handle = file->private_data; |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1254 | struct uvc_streaming *stream = handle->stream; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1255 | |
| 1256 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n"); |
| 1257 | |
Laurent Pinchart | 35f02a6 | 2009-06-28 08:37:50 -0300 | [diff] [blame] | 1258 | return uvc_queue_poll(&stream->queue, file, wait); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1259 | } |
| 1260 | |
Bob Liu | 7296944 | 2011-04-29 07:11:35 -0300 | [diff] [blame] | 1261 | #ifndef CONFIG_MMU |
| 1262 | static unsigned long uvc_v4l2_get_unmapped_area(struct file *file, |
| 1263 | unsigned long addr, unsigned long len, unsigned long pgoff, |
| 1264 | unsigned long flags) |
| 1265 | { |
| 1266 | struct uvc_fh *handle = file->private_data; |
| 1267 | struct uvc_streaming *stream = handle->stream; |
| 1268 | |
| 1269 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n"); |
| 1270 | |
| 1271 | return uvc_queue_get_unmapped_area(&stream->queue, pgoff); |
| 1272 | } |
| 1273 | #endif |
| 1274 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1275 | const struct v4l2_file_operations uvc_fops = { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1276 | .owner = THIS_MODULE, |
| 1277 | .open = uvc_v4l2_open, |
| 1278 | .release = uvc_v4l2_release, |
Laurent Pinchart | 673eb9f | 2010-11-21 16:54:56 -0300 | [diff] [blame] | 1279 | .unlocked_ioctl = uvc_v4l2_ioctl, |
Laurent Pinchart | 1a5e4c8 | 2011-12-18 20:45:39 -0300 | [diff] [blame] | 1280 | #ifdef CONFIG_COMPAT |
| 1281 | .compat_ioctl32 = uvc_v4l2_compat_ioctl32, |
| 1282 | #endif |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1283 | .read = uvc_v4l2_read, |
| 1284 | .mmap = uvc_v4l2_mmap, |
| 1285 | .poll = uvc_v4l2_poll, |
Bob Liu | 7296944 | 2011-04-29 07:11:35 -0300 | [diff] [blame] | 1286 | #ifndef CONFIG_MMU |
| 1287 | .get_unmapped_area = uvc_v4l2_get_unmapped_area, |
| 1288 | #endif |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1289 | }; |
Hans Verkuil | f87086e | 2008-07-18 00:50:58 -0300 | [diff] [blame] | 1290 | |