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