b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * uvc_v4l2.c -- USB Video Class driver - V4L2 API |
| 4 | * |
| 5 | * Copyright (C) 2005-2010 |
| 6 | * Laurent Pinchart (laurent.pinchart@ideasonboard.com) |
| 7 | */ |
| 8 | |
| 9 | #include <linux/compat.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/list.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/usb.h> |
| 15 | #include <linux/videodev2.h> |
| 16 | #include <linux/vmalloc.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/wait.h> |
| 19 | #include <linux/atomic.h> |
| 20 | |
| 21 | #include <media/v4l2-common.h> |
| 22 | #include <media/v4l2-ctrls.h> |
| 23 | #include <media/v4l2-event.h> |
| 24 | #include <media/v4l2-ioctl.h> |
| 25 | |
| 26 | #include "uvcvideo.h" |
| 27 | |
| 28 | /* ------------------------------------------------------------------------ |
| 29 | * UVC ioctls |
| 30 | */ |
| 31 | static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain, |
| 32 | struct uvc_xu_control_mapping *xmap) |
| 33 | { |
| 34 | struct uvc_control_mapping *map; |
| 35 | unsigned int size; |
| 36 | int ret; |
| 37 | |
| 38 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 39 | if (map == NULL) |
| 40 | return -ENOMEM; |
| 41 | |
| 42 | map->id = xmap->id; |
| 43 | memcpy(map->name, xmap->name, sizeof(map->name)); |
| 44 | memcpy(map->entity, xmap->entity, sizeof(map->entity)); |
| 45 | map->selector = xmap->selector; |
| 46 | map->size = xmap->size; |
| 47 | map->offset = xmap->offset; |
| 48 | map->v4l2_type = xmap->v4l2_type; |
| 49 | map->data_type = xmap->data_type; |
| 50 | |
| 51 | switch (xmap->v4l2_type) { |
| 52 | case V4L2_CTRL_TYPE_INTEGER: |
| 53 | case V4L2_CTRL_TYPE_BOOLEAN: |
| 54 | case V4L2_CTRL_TYPE_BUTTON: |
| 55 | break; |
| 56 | |
| 57 | case V4L2_CTRL_TYPE_MENU: |
| 58 | /* Prevent excessive memory consumption, as well as integer |
| 59 | * overflows. |
| 60 | */ |
| 61 | if (xmap->menu_count == 0 || |
| 62 | xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) { |
| 63 | ret = -EINVAL; |
| 64 | goto free_map; |
| 65 | } |
| 66 | |
| 67 | size = xmap->menu_count * sizeof(*map->menu_info); |
| 68 | map->menu_info = memdup_user(xmap->menu_info, size); |
| 69 | if (IS_ERR(map->menu_info)) { |
| 70 | ret = PTR_ERR(map->menu_info); |
| 71 | goto free_map; |
| 72 | } |
| 73 | |
| 74 | map->menu_count = xmap->menu_count; |
| 75 | break; |
| 76 | |
| 77 | default: |
| 78 | uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type " |
| 79 | "%u.\n", xmap->v4l2_type); |
| 80 | ret = -ENOTTY; |
| 81 | goto free_map; |
| 82 | } |
| 83 | |
| 84 | ret = uvc_ctrl_add_mapping(chain, map); |
| 85 | |
| 86 | kfree(map->menu_info); |
| 87 | free_map: |
| 88 | kfree(map); |
| 89 | |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | /* ------------------------------------------------------------------------ |
| 94 | * V4L2 interface |
| 95 | */ |
| 96 | |
| 97 | /* |
| 98 | * Find the frame interval closest to the requested frame interval for the |
| 99 | * given frame format and size. This should be done by the device as part of |
| 100 | * the Video Probe and Commit negotiation, but some hardware don't implement |
| 101 | * that feature. |
| 102 | */ |
| 103 | static u32 uvc_try_frame_interval(struct uvc_frame *frame, u32 interval) |
| 104 | { |
| 105 | unsigned int i; |
| 106 | |
| 107 | if (frame->bFrameIntervalType) { |
| 108 | u32 best = -1, dist; |
| 109 | |
| 110 | for (i = 0; i < frame->bFrameIntervalType; ++i) { |
| 111 | dist = interval > frame->dwFrameInterval[i] |
| 112 | ? interval - frame->dwFrameInterval[i] |
| 113 | : frame->dwFrameInterval[i] - interval; |
| 114 | |
| 115 | if (dist > best) |
| 116 | break; |
| 117 | |
| 118 | best = dist; |
| 119 | } |
| 120 | |
| 121 | interval = frame->dwFrameInterval[i-1]; |
| 122 | } else { |
| 123 | const u32 min = frame->dwFrameInterval[0]; |
| 124 | const u32 max = frame->dwFrameInterval[1]; |
| 125 | const u32 step = frame->dwFrameInterval[2]; |
| 126 | |
| 127 | interval = min + (interval - min + step/2) / step * step; |
| 128 | if (interval > max) |
| 129 | interval = max; |
| 130 | } |
| 131 | |
| 132 | return interval; |
| 133 | } |
| 134 | |
| 135 | static u32 uvc_v4l2_get_bytesperline(const struct uvc_format *format, |
| 136 | const struct uvc_frame *frame) |
| 137 | { |
| 138 | switch (format->fcc) { |
| 139 | case V4L2_PIX_FMT_NV12: |
| 140 | case V4L2_PIX_FMT_YVU420: |
| 141 | case V4L2_PIX_FMT_YUV420: |
| 142 | case V4L2_PIX_FMT_M420: |
| 143 | return frame->wWidth; |
| 144 | |
| 145 | default: |
| 146 | return format->bpp * frame->wWidth / 8; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | static int uvc_v4l2_try_format(struct uvc_streaming *stream, |
| 151 | struct v4l2_format *fmt, struct uvc_streaming_control *probe, |
| 152 | struct uvc_format **uvc_format, struct uvc_frame **uvc_frame) |
| 153 | { |
| 154 | struct uvc_format *format = NULL; |
| 155 | struct uvc_frame *frame = NULL; |
| 156 | u16 rw, rh; |
| 157 | unsigned int d, maxd; |
| 158 | unsigned int i; |
| 159 | u32 interval; |
| 160 | int ret = 0; |
| 161 | u8 *fcc; |
| 162 | |
| 163 | if (fmt->type != stream->type) |
| 164 | return -EINVAL; |
| 165 | |
| 166 | fcc = (u8 *)&fmt->fmt.pix.pixelformat; |
| 167 | uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n", |
| 168 | fmt->fmt.pix.pixelformat, |
| 169 | fcc[0], fcc[1], fcc[2], fcc[3], |
| 170 | fmt->fmt.pix.width, fmt->fmt.pix.height); |
| 171 | |
| 172 | /* Check if the hardware supports the requested format, use the default |
| 173 | * format otherwise. |
| 174 | */ |
| 175 | for (i = 0; i < stream->nformats; ++i) { |
| 176 | format = &stream->format[i]; |
| 177 | if (format->fcc == fmt->fmt.pix.pixelformat) |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | if (i == stream->nformats) { |
| 182 | format = stream->def_format; |
| 183 | fmt->fmt.pix.pixelformat = format->fcc; |
| 184 | } |
| 185 | |
| 186 | /* Find the closest image size. The distance between image sizes is |
| 187 | * the size in pixels of the non-overlapping regions between the |
| 188 | * requested size and the frame-specified size. |
| 189 | */ |
| 190 | rw = fmt->fmt.pix.width; |
| 191 | rh = fmt->fmt.pix.height; |
| 192 | maxd = (unsigned int)-1; |
| 193 | |
| 194 | for (i = 0; i < format->nframes; ++i) { |
| 195 | u16 w = format->frame[i].wWidth; |
| 196 | u16 h = format->frame[i].wHeight; |
| 197 | |
| 198 | d = min(w, rw) * min(h, rh); |
| 199 | d = w*h + rw*rh - 2*d; |
| 200 | if (d < maxd) { |
| 201 | maxd = d; |
| 202 | frame = &format->frame[i]; |
| 203 | } |
| 204 | |
| 205 | if (maxd == 0) |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | if (frame == NULL) { |
| 210 | uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n", |
| 211 | fmt->fmt.pix.width, fmt->fmt.pix.height); |
| 212 | return -EINVAL; |
| 213 | } |
| 214 | |
| 215 | /* Use the default frame interval. */ |
| 216 | interval = frame->dwDefaultFrameInterval; |
| 217 | uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us " |
| 218 | "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval, |
| 219 | (100000000/interval)%10); |
| 220 | |
| 221 | /* Set the format index, frame index and frame interval. */ |
| 222 | memset(probe, 0, sizeof(*probe)); |
| 223 | probe->bmHint = 1; /* dwFrameInterval */ |
| 224 | probe->bFormatIndex = format->index; |
| 225 | probe->bFrameIndex = frame->bFrameIndex; |
| 226 | probe->dwFrameInterval = uvc_try_frame_interval(frame, interval); |
| 227 | /* Some webcams stall the probe control set request when the |
| 228 | * dwMaxVideoFrameSize field is set to zero. The UVC specification |
| 229 | * clearly states that the field is read-only from the host, so this |
| 230 | * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by |
| 231 | * the webcam to work around the problem. |
| 232 | * |
| 233 | * The workaround could probably be enabled for all webcams, so the |
| 234 | * quirk can be removed if needed. It's currently useful to detect |
| 235 | * webcam bugs and fix them before they hit the market (providing |
| 236 | * developers test their webcams with the Linux driver as well as with |
| 237 | * the Windows driver). |
| 238 | */ |
| 239 | mutex_lock(&stream->mutex); |
| 240 | if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS) |
| 241 | probe->dwMaxVideoFrameSize = |
| 242 | stream->ctrl.dwMaxVideoFrameSize; |
| 243 | |
| 244 | /* Probe the device. */ |
| 245 | ret = uvc_probe_video(stream, probe); |
| 246 | mutex_unlock(&stream->mutex); |
| 247 | if (ret < 0) |
| 248 | goto done; |
| 249 | |
| 250 | /* After the probe, update fmt with the values returned from |
| 251 | * negotiation with the device. Some devices return invalid bFormatIndex |
| 252 | * and bFrameIndex values, in which case we can only assume they have |
| 253 | * accepted the requested format as-is. |
| 254 | */ |
| 255 | for (i = 0; i < stream->nformats; ++i) { |
| 256 | if (probe->bFormatIndex == stream->format[i].index) { |
| 257 | format = &stream->format[i]; |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (i == stream->nformats) |
| 263 | uvc_trace(UVC_TRACE_FORMAT, |
| 264 | "Unknown bFormatIndex %u, using default\n", |
| 265 | probe->bFormatIndex); |
| 266 | |
| 267 | for (i = 0; i < format->nframes; ++i) { |
| 268 | if (probe->bFrameIndex == format->frame[i].bFrameIndex) { |
| 269 | frame = &format->frame[i]; |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (i == format->nframes) |
| 275 | uvc_trace(UVC_TRACE_FORMAT, |
| 276 | "Unknown bFrameIndex %u, using default\n", |
| 277 | probe->bFrameIndex); |
| 278 | |
| 279 | fmt->fmt.pix.width = frame->wWidth; |
| 280 | fmt->fmt.pix.height = frame->wHeight; |
| 281 | fmt->fmt.pix.field = V4L2_FIELD_NONE; |
| 282 | fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame); |
| 283 | fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize; |
| 284 | fmt->fmt.pix.pixelformat = format->fcc; |
| 285 | fmt->fmt.pix.colorspace = format->colorspace; |
| 286 | |
| 287 | if (uvc_format != NULL) |
| 288 | *uvc_format = format; |
| 289 | if (uvc_frame != NULL) |
| 290 | *uvc_frame = frame; |
| 291 | |
| 292 | done: |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | static int uvc_v4l2_get_format(struct uvc_streaming *stream, |
| 297 | struct v4l2_format *fmt) |
| 298 | { |
| 299 | struct uvc_format *format; |
| 300 | struct uvc_frame *frame; |
| 301 | int ret = 0; |
| 302 | |
| 303 | if (fmt->type != stream->type) |
| 304 | return -EINVAL; |
| 305 | |
| 306 | mutex_lock(&stream->mutex); |
| 307 | format = stream->cur_format; |
| 308 | frame = stream->cur_frame; |
| 309 | |
| 310 | if (format == NULL || frame == NULL) { |
| 311 | ret = -EINVAL; |
| 312 | goto done; |
| 313 | } |
| 314 | |
| 315 | fmt->fmt.pix.pixelformat = format->fcc; |
| 316 | fmt->fmt.pix.width = frame->wWidth; |
| 317 | fmt->fmt.pix.height = frame->wHeight; |
| 318 | fmt->fmt.pix.field = V4L2_FIELD_NONE; |
| 319 | fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame); |
| 320 | fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize; |
| 321 | fmt->fmt.pix.colorspace = format->colorspace; |
| 322 | |
| 323 | done: |
| 324 | mutex_unlock(&stream->mutex); |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | static int uvc_v4l2_set_format(struct uvc_streaming *stream, |
| 329 | struct v4l2_format *fmt) |
| 330 | { |
| 331 | struct uvc_streaming_control probe; |
| 332 | struct uvc_format *format; |
| 333 | struct uvc_frame *frame; |
| 334 | int ret; |
| 335 | |
| 336 | if (fmt->type != stream->type) |
| 337 | return -EINVAL; |
| 338 | |
| 339 | ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame); |
| 340 | if (ret < 0) |
| 341 | return ret; |
| 342 | |
| 343 | mutex_lock(&stream->mutex); |
| 344 | |
| 345 | if (uvc_queue_allocated(&stream->queue)) { |
| 346 | ret = -EBUSY; |
| 347 | goto done; |
| 348 | } |
| 349 | |
| 350 | stream->ctrl = probe; |
| 351 | stream->cur_format = format; |
| 352 | stream->cur_frame = frame; |
| 353 | |
| 354 | done: |
| 355 | mutex_unlock(&stream->mutex); |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream, |
| 360 | struct v4l2_streamparm *parm) |
| 361 | { |
| 362 | u32 numerator, denominator; |
| 363 | |
| 364 | if (parm->type != stream->type) |
| 365 | return -EINVAL; |
| 366 | |
| 367 | mutex_lock(&stream->mutex); |
| 368 | numerator = stream->ctrl.dwFrameInterval; |
| 369 | mutex_unlock(&stream->mutex); |
| 370 | |
| 371 | denominator = 10000000; |
| 372 | uvc_simplify_fraction(&numerator, &denominator, 8, 333); |
| 373 | |
| 374 | memset(parm, 0, sizeof(*parm)); |
| 375 | parm->type = stream->type; |
| 376 | |
| 377 | if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 378 | parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; |
| 379 | parm->parm.capture.capturemode = 0; |
| 380 | parm->parm.capture.timeperframe.numerator = numerator; |
| 381 | parm->parm.capture.timeperframe.denominator = denominator; |
| 382 | parm->parm.capture.extendedmode = 0; |
| 383 | parm->parm.capture.readbuffers = 0; |
| 384 | } else { |
| 385 | parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME; |
| 386 | parm->parm.output.outputmode = 0; |
| 387 | parm->parm.output.timeperframe.numerator = numerator; |
| 388 | parm->parm.output.timeperframe.denominator = denominator; |
| 389 | } |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream, |
| 395 | struct v4l2_streamparm *parm) |
| 396 | { |
| 397 | struct uvc_streaming_control probe; |
| 398 | struct v4l2_fract timeperframe; |
| 399 | struct uvc_format *format; |
| 400 | struct uvc_frame *frame; |
| 401 | u32 interval, maxd; |
| 402 | unsigned int i; |
| 403 | int ret; |
| 404 | |
| 405 | if (parm->type != stream->type) |
| 406 | return -EINVAL; |
| 407 | |
| 408 | if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 409 | timeperframe = parm->parm.capture.timeperframe; |
| 410 | else |
| 411 | timeperframe = parm->parm.output.timeperframe; |
| 412 | |
| 413 | interval = uvc_fraction_to_interval(timeperframe.numerator, |
| 414 | timeperframe.denominator); |
| 415 | uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n", |
| 416 | timeperframe.numerator, timeperframe.denominator, interval); |
| 417 | |
| 418 | mutex_lock(&stream->mutex); |
| 419 | |
| 420 | if (uvc_queue_streaming(&stream->queue)) { |
| 421 | mutex_unlock(&stream->mutex); |
| 422 | return -EBUSY; |
| 423 | } |
| 424 | |
| 425 | format = stream->cur_format; |
| 426 | frame = stream->cur_frame; |
| 427 | probe = stream->ctrl; |
| 428 | probe.dwFrameInterval = uvc_try_frame_interval(frame, interval); |
| 429 | maxd = abs((s32)probe.dwFrameInterval - interval); |
| 430 | |
| 431 | /* Try frames with matching size to find the best frame interval. */ |
| 432 | for (i = 0; i < format->nframes && maxd != 0; i++) { |
| 433 | u32 d, ival; |
| 434 | |
| 435 | if (&format->frame[i] == stream->cur_frame) |
| 436 | continue; |
| 437 | |
| 438 | if (format->frame[i].wWidth != stream->cur_frame->wWidth || |
| 439 | format->frame[i].wHeight != stream->cur_frame->wHeight) |
| 440 | continue; |
| 441 | |
| 442 | ival = uvc_try_frame_interval(&format->frame[i], interval); |
| 443 | d = abs((s32)ival - interval); |
| 444 | if (d >= maxd) |
| 445 | continue; |
| 446 | |
| 447 | frame = &format->frame[i]; |
| 448 | probe.bFrameIndex = frame->bFrameIndex; |
| 449 | probe.dwFrameInterval = ival; |
| 450 | maxd = d; |
| 451 | } |
| 452 | |
| 453 | /* Probe the device with the new settings. */ |
| 454 | ret = uvc_probe_video(stream, &probe); |
| 455 | if (ret < 0) { |
| 456 | mutex_unlock(&stream->mutex); |
| 457 | return ret; |
| 458 | } |
| 459 | |
| 460 | stream->ctrl = probe; |
| 461 | stream->cur_frame = frame; |
| 462 | mutex_unlock(&stream->mutex); |
| 463 | |
| 464 | /* Return the actual frame period. */ |
| 465 | timeperframe.numerator = probe.dwFrameInterval; |
| 466 | timeperframe.denominator = 10000000; |
| 467 | uvc_simplify_fraction(&timeperframe.numerator, |
| 468 | &timeperframe.denominator, 8, 333); |
| 469 | |
| 470 | if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 471 | parm->parm.capture.timeperframe = timeperframe; |
| 472 | parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; |
| 473 | } else { |
| 474 | parm->parm.output.timeperframe = timeperframe; |
| 475 | parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME; |
| 476 | } |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | /* ------------------------------------------------------------------------ |
| 482 | * Privilege management |
| 483 | */ |
| 484 | |
| 485 | /* |
| 486 | * Privilege management is the multiple-open implementation basis. The current |
| 487 | * implementation is completely transparent for the end-user and doesn't |
| 488 | * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls. |
| 489 | * Those ioctls enable finer control on the device (by making possible for a |
| 490 | * user to request exclusive access to a device), but are not mature yet. |
| 491 | * Switching to the V4L2 priority mechanism might be considered in the future |
| 492 | * if this situation changes. |
| 493 | * |
| 494 | * Each open instance of a UVC device can either be in a privileged or |
| 495 | * unprivileged state. Only a single instance can be in a privileged state at |
| 496 | * a given time. Trying to perform an operation that requires privileges will |
| 497 | * automatically acquire the required privileges if possible, or return -EBUSY |
| 498 | * otherwise. Privileges are dismissed when closing the instance or when |
| 499 | * freeing the video buffers using VIDIOC_REQBUFS. |
| 500 | * |
| 501 | * Operations that require privileges are: |
| 502 | * |
| 503 | * - VIDIOC_S_INPUT |
| 504 | * - VIDIOC_S_PARM |
| 505 | * - VIDIOC_S_FMT |
| 506 | * - VIDIOC_REQBUFS |
| 507 | */ |
| 508 | static int uvc_acquire_privileges(struct uvc_fh *handle) |
| 509 | { |
| 510 | /* Always succeed if the handle is already privileged. */ |
| 511 | if (handle->state == UVC_HANDLE_ACTIVE) |
| 512 | return 0; |
| 513 | |
| 514 | /* Check if the device already has a privileged handle. */ |
| 515 | if (atomic_inc_return(&handle->stream->active) != 1) { |
| 516 | atomic_dec(&handle->stream->active); |
| 517 | return -EBUSY; |
| 518 | } |
| 519 | |
| 520 | handle->state = UVC_HANDLE_ACTIVE; |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | static void uvc_dismiss_privileges(struct uvc_fh *handle) |
| 525 | { |
| 526 | if (handle->state == UVC_HANDLE_ACTIVE) |
| 527 | atomic_dec(&handle->stream->active); |
| 528 | |
| 529 | handle->state = UVC_HANDLE_PASSIVE; |
| 530 | } |
| 531 | |
| 532 | static int uvc_has_privileges(struct uvc_fh *handle) |
| 533 | { |
| 534 | return handle->state == UVC_HANDLE_ACTIVE; |
| 535 | } |
| 536 | |
| 537 | /* ------------------------------------------------------------------------ |
| 538 | * V4L2 file operations |
| 539 | */ |
| 540 | |
| 541 | static int uvc_v4l2_open(struct file *file) |
| 542 | { |
| 543 | struct uvc_streaming *stream; |
| 544 | struct uvc_fh *handle; |
| 545 | int ret = 0; |
| 546 | |
| 547 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n"); |
| 548 | stream = video_drvdata(file); |
| 549 | |
| 550 | ret = usb_autopm_get_interface(stream->dev->intf); |
| 551 | if (ret < 0) |
| 552 | return ret; |
| 553 | |
| 554 | /* Create the device handle. */ |
| 555 | handle = kzalloc(sizeof(*handle), GFP_KERNEL); |
| 556 | if (handle == NULL) { |
| 557 | usb_autopm_put_interface(stream->dev->intf); |
| 558 | return -ENOMEM; |
| 559 | } |
| 560 | |
| 561 | mutex_lock(&stream->dev->lock); |
| 562 | if (stream->dev->users == 0) { |
| 563 | ret = uvc_status_start(stream->dev, GFP_KERNEL); |
| 564 | if (ret < 0) { |
| 565 | mutex_unlock(&stream->dev->lock); |
| 566 | usb_autopm_put_interface(stream->dev->intf); |
| 567 | kfree(handle); |
| 568 | return ret; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | stream->dev->users++; |
| 573 | mutex_unlock(&stream->dev->lock); |
| 574 | |
| 575 | v4l2_fh_init(&handle->vfh, &stream->vdev); |
| 576 | v4l2_fh_add(&handle->vfh); |
| 577 | handle->chain = stream->chain; |
| 578 | handle->stream = stream; |
| 579 | handle->state = UVC_HANDLE_PASSIVE; |
| 580 | file->private_data = handle; |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | static int uvc_v4l2_release(struct file *file) |
| 586 | { |
| 587 | struct uvc_fh *handle = file->private_data; |
| 588 | struct uvc_streaming *stream = handle->stream; |
| 589 | |
| 590 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n"); |
| 591 | |
| 592 | /* Only free resources if this is a privileged handle. */ |
| 593 | if (uvc_has_privileges(handle)) |
| 594 | uvc_queue_release(&stream->queue); |
| 595 | |
| 596 | /* Release the file handle. */ |
| 597 | uvc_dismiss_privileges(handle); |
| 598 | v4l2_fh_del(&handle->vfh); |
| 599 | v4l2_fh_exit(&handle->vfh); |
| 600 | kfree(handle); |
| 601 | file->private_data = NULL; |
| 602 | |
| 603 | mutex_lock(&stream->dev->lock); |
| 604 | if (--stream->dev->users == 0) |
| 605 | uvc_status_stop(stream->dev); |
| 606 | mutex_unlock(&stream->dev->lock); |
| 607 | |
| 608 | usb_autopm_put_interface(stream->dev->intf); |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | static int uvc_ioctl_querycap(struct file *file, void *fh, |
| 613 | struct v4l2_capability *cap) |
| 614 | { |
| 615 | struct video_device *vdev = video_devdata(file); |
| 616 | struct uvc_fh *handle = file->private_data; |
| 617 | struct uvc_video_chain *chain = handle->chain; |
| 618 | struct uvc_streaming *stream = handle->stream; |
| 619 | |
| 620 | strscpy(cap->driver, "uvcvideo", sizeof(cap->driver)); |
| 621 | strscpy(cap->card, vdev->name, sizeof(cap->card)); |
| 622 | usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info)); |
| 623 | cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING |
| 624 | | chain->caps; |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | static int uvc_ioctl_enum_fmt(struct uvc_streaming *stream, |
| 630 | struct v4l2_fmtdesc *fmt) |
| 631 | { |
| 632 | struct uvc_format *format; |
| 633 | enum v4l2_buf_type type = fmt->type; |
| 634 | u32 index = fmt->index; |
| 635 | |
| 636 | if (fmt->type != stream->type || fmt->index >= stream->nformats) |
| 637 | return -EINVAL; |
| 638 | |
| 639 | memset(fmt, 0, sizeof(*fmt)); |
| 640 | fmt->index = index; |
| 641 | fmt->type = type; |
| 642 | |
| 643 | format = &stream->format[fmt->index]; |
| 644 | fmt->flags = 0; |
| 645 | if (format->flags & UVC_FMT_FLAG_COMPRESSED) |
| 646 | fmt->flags |= V4L2_FMT_FLAG_COMPRESSED; |
| 647 | strscpy(fmt->description, format->name, sizeof(fmt->description)); |
| 648 | fmt->description[sizeof(fmt->description) - 1] = 0; |
| 649 | fmt->pixelformat = format->fcc; |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | static int uvc_ioctl_enum_fmt_vid_cap(struct file *file, void *fh, |
| 654 | struct v4l2_fmtdesc *fmt) |
| 655 | { |
| 656 | struct uvc_fh *handle = fh; |
| 657 | struct uvc_streaming *stream = handle->stream; |
| 658 | |
| 659 | return uvc_ioctl_enum_fmt(stream, fmt); |
| 660 | } |
| 661 | |
| 662 | static int uvc_ioctl_enum_fmt_vid_out(struct file *file, void *fh, |
| 663 | struct v4l2_fmtdesc *fmt) |
| 664 | { |
| 665 | struct uvc_fh *handle = fh; |
| 666 | struct uvc_streaming *stream = handle->stream; |
| 667 | |
| 668 | return uvc_ioctl_enum_fmt(stream, fmt); |
| 669 | } |
| 670 | |
| 671 | static int uvc_ioctl_g_fmt_vid_cap(struct file *file, void *fh, |
| 672 | struct v4l2_format *fmt) |
| 673 | { |
| 674 | struct uvc_fh *handle = fh; |
| 675 | struct uvc_streaming *stream = handle->stream; |
| 676 | |
| 677 | return uvc_v4l2_get_format(stream, fmt); |
| 678 | } |
| 679 | |
| 680 | static int uvc_ioctl_g_fmt_vid_out(struct file *file, void *fh, |
| 681 | struct v4l2_format *fmt) |
| 682 | { |
| 683 | struct uvc_fh *handle = fh; |
| 684 | struct uvc_streaming *stream = handle->stream; |
| 685 | |
| 686 | return uvc_v4l2_get_format(stream, fmt); |
| 687 | } |
| 688 | |
| 689 | static int uvc_ioctl_s_fmt_vid_cap(struct file *file, void *fh, |
| 690 | struct v4l2_format *fmt) |
| 691 | { |
| 692 | struct uvc_fh *handle = fh; |
| 693 | struct uvc_streaming *stream = handle->stream; |
| 694 | int ret; |
| 695 | |
| 696 | ret = uvc_acquire_privileges(handle); |
| 697 | if (ret < 0) |
| 698 | return ret; |
| 699 | |
| 700 | return uvc_v4l2_set_format(stream, fmt); |
| 701 | } |
| 702 | |
| 703 | static int uvc_ioctl_s_fmt_vid_out(struct file *file, void *fh, |
| 704 | struct v4l2_format *fmt) |
| 705 | { |
| 706 | struct uvc_fh *handle = fh; |
| 707 | struct uvc_streaming *stream = handle->stream; |
| 708 | int ret; |
| 709 | |
| 710 | ret = uvc_acquire_privileges(handle); |
| 711 | if (ret < 0) |
| 712 | return ret; |
| 713 | |
| 714 | return uvc_v4l2_set_format(stream, fmt); |
| 715 | } |
| 716 | |
| 717 | static int uvc_ioctl_try_fmt_vid_cap(struct file *file, void *fh, |
| 718 | struct v4l2_format *fmt) |
| 719 | { |
| 720 | struct uvc_fh *handle = fh; |
| 721 | struct uvc_streaming *stream = handle->stream; |
| 722 | struct uvc_streaming_control probe; |
| 723 | |
| 724 | return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL); |
| 725 | } |
| 726 | |
| 727 | static int uvc_ioctl_try_fmt_vid_out(struct file *file, void *fh, |
| 728 | struct v4l2_format *fmt) |
| 729 | { |
| 730 | struct uvc_fh *handle = fh; |
| 731 | struct uvc_streaming *stream = handle->stream; |
| 732 | struct uvc_streaming_control probe; |
| 733 | |
| 734 | return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL); |
| 735 | } |
| 736 | |
| 737 | static int uvc_ioctl_reqbufs(struct file *file, void *fh, |
| 738 | struct v4l2_requestbuffers *rb) |
| 739 | { |
| 740 | struct uvc_fh *handle = fh; |
| 741 | struct uvc_streaming *stream = handle->stream; |
| 742 | int ret; |
| 743 | |
| 744 | ret = uvc_acquire_privileges(handle); |
| 745 | if (ret < 0) |
| 746 | return ret; |
| 747 | |
| 748 | mutex_lock(&stream->mutex); |
| 749 | ret = uvc_request_buffers(&stream->queue, rb); |
| 750 | mutex_unlock(&stream->mutex); |
| 751 | if (ret < 0) |
| 752 | return ret; |
| 753 | |
| 754 | if (ret == 0) |
| 755 | uvc_dismiss_privileges(handle); |
| 756 | |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | static int uvc_ioctl_querybuf(struct file *file, void *fh, |
| 761 | struct v4l2_buffer *buf) |
| 762 | { |
| 763 | struct uvc_fh *handle = fh; |
| 764 | struct uvc_streaming *stream = handle->stream; |
| 765 | |
| 766 | if (!uvc_has_privileges(handle)) |
| 767 | return -EBUSY; |
| 768 | |
| 769 | return uvc_query_buffer(&stream->queue, buf); |
| 770 | } |
| 771 | |
| 772 | static int uvc_ioctl_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf) |
| 773 | { |
| 774 | struct uvc_fh *handle = fh; |
| 775 | struct uvc_streaming *stream = handle->stream; |
| 776 | |
| 777 | if (!uvc_has_privileges(handle)) |
| 778 | return -EBUSY; |
| 779 | |
| 780 | return uvc_queue_buffer(&stream->queue, |
| 781 | stream->vdev.v4l2_dev->mdev, buf); |
| 782 | } |
| 783 | |
| 784 | static int uvc_ioctl_expbuf(struct file *file, void *fh, |
| 785 | struct v4l2_exportbuffer *exp) |
| 786 | { |
| 787 | struct uvc_fh *handle = fh; |
| 788 | struct uvc_streaming *stream = handle->stream; |
| 789 | |
| 790 | if (!uvc_has_privileges(handle)) |
| 791 | return -EBUSY; |
| 792 | |
| 793 | return uvc_export_buffer(&stream->queue, exp); |
| 794 | } |
| 795 | |
| 796 | static int uvc_ioctl_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf) |
| 797 | { |
| 798 | struct uvc_fh *handle = fh; |
| 799 | struct uvc_streaming *stream = handle->stream; |
| 800 | |
| 801 | if (!uvc_has_privileges(handle)) |
| 802 | return -EBUSY; |
| 803 | |
| 804 | return uvc_dequeue_buffer(&stream->queue, buf, |
| 805 | file->f_flags & O_NONBLOCK); |
| 806 | } |
| 807 | |
| 808 | static int uvc_ioctl_create_bufs(struct file *file, void *fh, |
| 809 | struct v4l2_create_buffers *cb) |
| 810 | { |
| 811 | struct uvc_fh *handle = fh; |
| 812 | struct uvc_streaming *stream = handle->stream; |
| 813 | int ret; |
| 814 | |
| 815 | ret = uvc_acquire_privileges(handle); |
| 816 | if (ret < 0) |
| 817 | return ret; |
| 818 | |
| 819 | return uvc_create_buffers(&stream->queue, cb); |
| 820 | } |
| 821 | |
| 822 | static int uvc_ioctl_streamon(struct file *file, void *fh, |
| 823 | enum v4l2_buf_type type) |
| 824 | { |
| 825 | struct uvc_fh *handle = fh; |
| 826 | struct uvc_streaming *stream = handle->stream; |
| 827 | int ret; |
| 828 | |
| 829 | if (!uvc_has_privileges(handle)) |
| 830 | return -EBUSY; |
| 831 | |
| 832 | mutex_lock(&stream->mutex); |
| 833 | ret = uvc_queue_streamon(&stream->queue, type); |
| 834 | mutex_unlock(&stream->mutex); |
| 835 | |
| 836 | return ret; |
| 837 | } |
| 838 | |
| 839 | static int uvc_ioctl_streamoff(struct file *file, void *fh, |
| 840 | enum v4l2_buf_type type) |
| 841 | { |
| 842 | struct uvc_fh *handle = fh; |
| 843 | struct uvc_streaming *stream = handle->stream; |
| 844 | |
| 845 | if (!uvc_has_privileges(handle)) |
| 846 | return -EBUSY; |
| 847 | |
| 848 | mutex_lock(&stream->mutex); |
| 849 | uvc_queue_streamoff(&stream->queue, type); |
| 850 | mutex_unlock(&stream->mutex); |
| 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | static int uvc_ioctl_enum_input(struct file *file, void *fh, |
| 856 | struct v4l2_input *input) |
| 857 | { |
| 858 | struct uvc_fh *handle = fh; |
| 859 | struct uvc_video_chain *chain = handle->chain; |
| 860 | const struct uvc_entity *selector = chain->selector; |
| 861 | struct uvc_entity *iterm = NULL; |
| 862 | struct uvc_entity *it; |
| 863 | u32 index = input->index; |
| 864 | |
| 865 | if (selector == NULL || |
| 866 | (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { |
| 867 | if (index != 0) |
| 868 | return -EINVAL; |
| 869 | list_for_each_entry(it, &chain->entities, chain) { |
| 870 | if (UVC_ENTITY_IS_ITERM(it)) { |
| 871 | iterm = it; |
| 872 | break; |
| 873 | } |
| 874 | } |
| 875 | } else if (index < selector->bNrInPins) { |
| 876 | list_for_each_entry(it, &chain->entities, chain) { |
| 877 | if (!UVC_ENTITY_IS_ITERM(it)) |
| 878 | continue; |
| 879 | if (it->id == selector->baSourceID[index]) { |
| 880 | iterm = it; |
| 881 | break; |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | if (iterm == NULL) |
| 887 | return -EINVAL; |
| 888 | |
| 889 | memset(input, 0, sizeof(*input)); |
| 890 | input->index = index; |
| 891 | strscpy(input->name, iterm->name, sizeof(input->name)); |
| 892 | if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA) |
| 893 | input->type = V4L2_INPUT_TYPE_CAMERA; |
| 894 | |
| 895 | return 0; |
| 896 | } |
| 897 | |
| 898 | static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input) |
| 899 | { |
| 900 | struct uvc_fh *handle = fh; |
| 901 | struct uvc_video_chain *chain = handle->chain; |
| 902 | u8 *buf; |
| 903 | int ret; |
| 904 | |
| 905 | if (chain->selector == NULL || |
| 906 | (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { |
| 907 | *input = 0; |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | buf = kmalloc(1, GFP_KERNEL); |
| 912 | if (!buf) |
| 913 | return -ENOMEM; |
| 914 | |
| 915 | ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id, |
| 916 | chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL, |
| 917 | buf, 1); |
| 918 | if (!ret) |
| 919 | *input = *buf - 1; |
| 920 | |
| 921 | kfree(buf); |
| 922 | |
| 923 | return ret; |
| 924 | } |
| 925 | |
| 926 | static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input) |
| 927 | { |
| 928 | struct uvc_fh *handle = fh; |
| 929 | struct uvc_video_chain *chain = handle->chain; |
| 930 | u8 *buf; |
| 931 | int ret; |
| 932 | |
| 933 | ret = uvc_acquire_privileges(handle); |
| 934 | if (ret < 0) |
| 935 | return ret; |
| 936 | |
| 937 | if (chain->selector == NULL || |
| 938 | (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { |
| 939 | if (input) |
| 940 | return -EINVAL; |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | if (input >= chain->selector->bNrInPins) |
| 945 | return -EINVAL; |
| 946 | |
| 947 | buf = kmalloc(1, GFP_KERNEL); |
| 948 | if (!buf) |
| 949 | return -ENOMEM; |
| 950 | |
| 951 | *buf = input + 1; |
| 952 | ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id, |
| 953 | chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL, |
| 954 | buf, 1); |
| 955 | kfree(buf); |
| 956 | |
| 957 | return ret; |
| 958 | } |
| 959 | |
| 960 | static int uvc_ioctl_queryctrl(struct file *file, void *fh, |
| 961 | struct v4l2_queryctrl *qc) |
| 962 | { |
| 963 | struct uvc_fh *handle = fh; |
| 964 | struct uvc_video_chain *chain = handle->chain; |
| 965 | |
| 966 | return uvc_query_v4l2_ctrl(chain, qc); |
| 967 | } |
| 968 | |
| 969 | static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh, |
| 970 | struct v4l2_query_ext_ctrl *qec) |
| 971 | { |
| 972 | struct uvc_fh *handle = fh; |
| 973 | struct uvc_video_chain *chain = handle->chain; |
| 974 | struct v4l2_queryctrl qc = { qec->id }; |
| 975 | int ret; |
| 976 | |
| 977 | ret = uvc_query_v4l2_ctrl(chain, &qc); |
| 978 | if (ret) |
| 979 | return ret; |
| 980 | |
| 981 | qec->id = qc.id; |
| 982 | qec->type = qc.type; |
| 983 | strscpy(qec->name, qc.name, sizeof(qec->name)); |
| 984 | qec->minimum = qc.minimum; |
| 985 | qec->maximum = qc.maximum; |
| 986 | qec->step = qc.step; |
| 987 | qec->default_value = qc.default_value; |
| 988 | qec->flags = qc.flags; |
| 989 | qec->elem_size = 4; |
| 990 | qec->elems = 1; |
| 991 | qec->nr_of_dims = 0; |
| 992 | memset(qec->dims, 0, sizeof(qec->dims)); |
| 993 | memset(qec->reserved, 0, sizeof(qec->reserved)); |
| 994 | |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | static int uvc_ioctl_g_ctrl(struct file *file, void *fh, |
| 999 | struct v4l2_control *ctrl) |
| 1000 | { |
| 1001 | struct uvc_fh *handle = fh; |
| 1002 | struct uvc_video_chain *chain = handle->chain; |
| 1003 | struct v4l2_ext_control xctrl; |
| 1004 | int ret; |
| 1005 | |
| 1006 | memset(&xctrl, 0, sizeof(xctrl)); |
| 1007 | xctrl.id = ctrl->id; |
| 1008 | |
| 1009 | ret = uvc_ctrl_begin(chain); |
| 1010 | if (ret < 0) |
| 1011 | return ret; |
| 1012 | |
| 1013 | ret = uvc_ctrl_get(chain, &xctrl); |
| 1014 | uvc_ctrl_rollback(handle); |
| 1015 | if (ret < 0) |
| 1016 | return ret; |
| 1017 | |
| 1018 | ctrl->value = xctrl.value; |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | static int uvc_ioctl_s_ctrl(struct file *file, void *fh, |
| 1023 | struct v4l2_control *ctrl) |
| 1024 | { |
| 1025 | struct uvc_fh *handle = fh; |
| 1026 | struct uvc_video_chain *chain = handle->chain; |
| 1027 | struct v4l2_ext_control xctrl; |
| 1028 | int ret; |
| 1029 | |
| 1030 | memset(&xctrl, 0, sizeof(xctrl)); |
| 1031 | xctrl.id = ctrl->id; |
| 1032 | xctrl.value = ctrl->value; |
| 1033 | |
| 1034 | ret = uvc_ctrl_begin(chain); |
| 1035 | if (ret < 0) |
| 1036 | return ret; |
| 1037 | |
| 1038 | ret = uvc_ctrl_set(handle, &xctrl); |
| 1039 | if (ret < 0) { |
| 1040 | uvc_ctrl_rollback(handle); |
| 1041 | return ret; |
| 1042 | } |
| 1043 | |
| 1044 | ret = uvc_ctrl_commit(handle, &xctrl, 1); |
| 1045 | if (ret < 0) |
| 1046 | return ret; |
| 1047 | |
| 1048 | ctrl->value = xctrl.value; |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh, |
| 1053 | struct v4l2_ext_controls *ctrls) |
| 1054 | { |
| 1055 | struct uvc_fh *handle = fh; |
| 1056 | struct uvc_video_chain *chain = handle->chain; |
| 1057 | struct v4l2_ext_control *ctrl = ctrls->controls; |
| 1058 | unsigned int i; |
| 1059 | int ret; |
| 1060 | |
| 1061 | if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) { |
| 1062 | for (i = 0; i < ctrls->count; ++ctrl, ++i) { |
| 1063 | struct v4l2_queryctrl qc = { .id = ctrl->id }; |
| 1064 | |
| 1065 | ret = uvc_query_v4l2_ctrl(chain, &qc); |
| 1066 | if (ret < 0) { |
| 1067 | ctrls->error_idx = i; |
| 1068 | return ret; |
| 1069 | } |
| 1070 | |
| 1071 | ctrl->value = qc.default_value; |
| 1072 | } |
| 1073 | |
| 1074 | return 0; |
| 1075 | } |
| 1076 | |
| 1077 | ret = uvc_ctrl_begin(chain); |
| 1078 | if (ret < 0) |
| 1079 | return ret; |
| 1080 | |
| 1081 | for (i = 0; i < ctrls->count; ++ctrl, ++i) { |
| 1082 | ret = uvc_ctrl_get(chain, ctrl); |
| 1083 | if (ret < 0) { |
| 1084 | uvc_ctrl_rollback(handle); |
| 1085 | ctrls->error_idx = i; |
| 1086 | return ret; |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | ctrls->error_idx = 0; |
| 1091 | |
| 1092 | return uvc_ctrl_rollback(handle); |
| 1093 | } |
| 1094 | |
| 1095 | static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle, |
| 1096 | struct v4l2_ext_controls *ctrls, |
| 1097 | bool commit) |
| 1098 | { |
| 1099 | struct v4l2_ext_control *ctrl = ctrls->controls; |
| 1100 | struct uvc_video_chain *chain = handle->chain; |
| 1101 | unsigned int i; |
| 1102 | int ret; |
| 1103 | |
| 1104 | /* Default value cannot be changed */ |
| 1105 | if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) |
| 1106 | return -EINVAL; |
| 1107 | |
| 1108 | ret = uvc_ctrl_begin(chain); |
| 1109 | if (ret < 0) |
| 1110 | return ret; |
| 1111 | |
| 1112 | for (i = 0; i < ctrls->count; ++ctrl, ++i) { |
| 1113 | ret = uvc_ctrl_set(handle, ctrl); |
| 1114 | if (ret < 0) { |
| 1115 | uvc_ctrl_rollback(handle); |
| 1116 | ctrls->error_idx = commit ? ctrls->count : i; |
| 1117 | return ret; |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | ctrls->error_idx = 0; |
| 1122 | |
| 1123 | if (commit) |
| 1124 | return uvc_ctrl_commit(handle, ctrls->controls, ctrls->count); |
| 1125 | else |
| 1126 | return uvc_ctrl_rollback(handle); |
| 1127 | } |
| 1128 | |
| 1129 | static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh, |
| 1130 | struct v4l2_ext_controls *ctrls) |
| 1131 | { |
| 1132 | struct uvc_fh *handle = fh; |
| 1133 | |
| 1134 | return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, true); |
| 1135 | } |
| 1136 | |
| 1137 | static int uvc_ioctl_try_ext_ctrls(struct file *file, void *fh, |
| 1138 | struct v4l2_ext_controls *ctrls) |
| 1139 | { |
| 1140 | struct uvc_fh *handle = fh; |
| 1141 | |
| 1142 | return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, false); |
| 1143 | } |
| 1144 | |
| 1145 | static int uvc_ioctl_querymenu(struct file *file, void *fh, |
| 1146 | struct v4l2_querymenu *qm) |
| 1147 | { |
| 1148 | struct uvc_fh *handle = fh; |
| 1149 | struct uvc_video_chain *chain = handle->chain; |
| 1150 | |
| 1151 | return uvc_query_v4l2_menu(chain, qm); |
| 1152 | } |
| 1153 | |
| 1154 | static int uvc_ioctl_g_selection(struct file *file, void *fh, |
| 1155 | struct v4l2_selection *sel) |
| 1156 | { |
| 1157 | struct uvc_fh *handle = fh; |
| 1158 | struct uvc_streaming *stream = handle->stream; |
| 1159 | |
| 1160 | if (sel->type != stream->type) |
| 1161 | return -EINVAL; |
| 1162 | |
| 1163 | switch (sel->target) { |
| 1164 | case V4L2_SEL_TGT_CROP_DEFAULT: |
| 1165 | case V4L2_SEL_TGT_CROP_BOUNDS: |
| 1166 | if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1167 | return -EINVAL; |
| 1168 | break; |
| 1169 | case V4L2_SEL_TGT_COMPOSE_DEFAULT: |
| 1170 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
| 1171 | if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) |
| 1172 | return -EINVAL; |
| 1173 | break; |
| 1174 | default: |
| 1175 | return -EINVAL; |
| 1176 | } |
| 1177 | |
| 1178 | sel->r.left = 0; |
| 1179 | sel->r.top = 0; |
| 1180 | mutex_lock(&stream->mutex); |
| 1181 | sel->r.width = stream->cur_frame->wWidth; |
| 1182 | sel->r.height = stream->cur_frame->wHeight; |
| 1183 | mutex_unlock(&stream->mutex); |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | static int uvc_ioctl_g_parm(struct file *file, void *fh, |
| 1189 | struct v4l2_streamparm *parm) |
| 1190 | { |
| 1191 | struct uvc_fh *handle = fh; |
| 1192 | struct uvc_streaming *stream = handle->stream; |
| 1193 | |
| 1194 | return uvc_v4l2_get_streamparm(stream, parm); |
| 1195 | } |
| 1196 | |
| 1197 | static int uvc_ioctl_s_parm(struct file *file, void *fh, |
| 1198 | struct v4l2_streamparm *parm) |
| 1199 | { |
| 1200 | struct uvc_fh *handle = fh; |
| 1201 | struct uvc_streaming *stream = handle->stream; |
| 1202 | int ret; |
| 1203 | |
| 1204 | ret = uvc_acquire_privileges(handle); |
| 1205 | if (ret < 0) |
| 1206 | return ret; |
| 1207 | |
| 1208 | return uvc_v4l2_set_streamparm(stream, parm); |
| 1209 | } |
| 1210 | |
| 1211 | static int uvc_ioctl_enum_framesizes(struct file *file, void *fh, |
| 1212 | struct v4l2_frmsizeenum *fsize) |
| 1213 | { |
| 1214 | struct uvc_fh *handle = fh; |
| 1215 | struct uvc_streaming *stream = handle->stream; |
| 1216 | struct uvc_format *format = NULL; |
| 1217 | struct uvc_frame *frame = NULL; |
| 1218 | unsigned int index; |
| 1219 | unsigned int i; |
| 1220 | |
| 1221 | /* Look for the given pixel format */ |
| 1222 | for (i = 0; i < stream->nformats; i++) { |
| 1223 | if (stream->format[i].fcc == fsize->pixel_format) { |
| 1224 | format = &stream->format[i]; |
| 1225 | break; |
| 1226 | } |
| 1227 | } |
| 1228 | if (format == NULL) |
| 1229 | return -EINVAL; |
| 1230 | |
| 1231 | /* Skip duplicate frame sizes */ |
| 1232 | for (i = 0, index = 0; i < format->nframes; i++) { |
| 1233 | if (frame && frame->wWidth == format->frame[i].wWidth && |
| 1234 | frame->wHeight == format->frame[i].wHeight) |
| 1235 | continue; |
| 1236 | frame = &format->frame[i]; |
| 1237 | if (index == fsize->index) |
| 1238 | break; |
| 1239 | index++; |
| 1240 | } |
| 1241 | |
| 1242 | if (i == format->nframes) |
| 1243 | return -EINVAL; |
| 1244 | |
| 1245 | fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; |
| 1246 | fsize->discrete.width = frame->wWidth; |
| 1247 | fsize->discrete.height = frame->wHeight; |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
| 1251 | static int uvc_ioctl_enum_frameintervals(struct file *file, void *fh, |
| 1252 | struct v4l2_frmivalenum *fival) |
| 1253 | { |
| 1254 | struct uvc_fh *handle = fh; |
| 1255 | struct uvc_streaming *stream = handle->stream; |
| 1256 | struct uvc_format *format = NULL; |
| 1257 | struct uvc_frame *frame = NULL; |
| 1258 | unsigned int nintervals; |
| 1259 | unsigned int index; |
| 1260 | unsigned int i; |
| 1261 | |
| 1262 | /* Look for the given pixel format and frame size */ |
| 1263 | for (i = 0; i < stream->nformats; i++) { |
| 1264 | if (stream->format[i].fcc == fival->pixel_format) { |
| 1265 | format = &stream->format[i]; |
| 1266 | break; |
| 1267 | } |
| 1268 | } |
| 1269 | if (format == NULL) |
| 1270 | return -EINVAL; |
| 1271 | |
| 1272 | index = fival->index; |
| 1273 | for (i = 0; i < format->nframes; i++) { |
| 1274 | if (format->frame[i].wWidth == fival->width && |
| 1275 | format->frame[i].wHeight == fival->height) { |
| 1276 | frame = &format->frame[i]; |
| 1277 | nintervals = frame->bFrameIntervalType ?: 1; |
| 1278 | if (index < nintervals) |
| 1279 | break; |
| 1280 | index -= nintervals; |
| 1281 | } |
| 1282 | } |
| 1283 | if (i == format->nframes) |
| 1284 | return -EINVAL; |
| 1285 | |
| 1286 | if (frame->bFrameIntervalType) { |
| 1287 | fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; |
| 1288 | fival->discrete.numerator = |
| 1289 | frame->dwFrameInterval[index]; |
| 1290 | fival->discrete.denominator = 10000000; |
| 1291 | uvc_simplify_fraction(&fival->discrete.numerator, |
| 1292 | &fival->discrete.denominator, 8, 333); |
| 1293 | } else { |
| 1294 | fival->type = V4L2_FRMIVAL_TYPE_STEPWISE; |
| 1295 | fival->stepwise.min.numerator = frame->dwFrameInterval[0]; |
| 1296 | fival->stepwise.min.denominator = 10000000; |
| 1297 | fival->stepwise.max.numerator = frame->dwFrameInterval[1]; |
| 1298 | fival->stepwise.max.denominator = 10000000; |
| 1299 | fival->stepwise.step.numerator = frame->dwFrameInterval[2]; |
| 1300 | fival->stepwise.step.denominator = 10000000; |
| 1301 | uvc_simplify_fraction(&fival->stepwise.min.numerator, |
| 1302 | &fival->stepwise.min.denominator, 8, 333); |
| 1303 | uvc_simplify_fraction(&fival->stepwise.max.numerator, |
| 1304 | &fival->stepwise.max.denominator, 8, 333); |
| 1305 | uvc_simplify_fraction(&fival->stepwise.step.numerator, |
| 1306 | &fival->stepwise.step.denominator, 8, 333); |
| 1307 | } |
| 1308 | |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh, |
| 1313 | const struct v4l2_event_subscription *sub) |
| 1314 | { |
| 1315 | switch (sub->type) { |
| 1316 | case V4L2_EVENT_CTRL: |
| 1317 | return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops); |
| 1318 | default: |
| 1319 | return -EINVAL; |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | static long uvc_ioctl_default(struct file *file, void *fh, bool valid_prio, |
| 1324 | unsigned int cmd, void *arg) |
| 1325 | { |
| 1326 | struct uvc_fh *handle = fh; |
| 1327 | struct uvc_video_chain *chain = handle->chain; |
| 1328 | |
| 1329 | switch (cmd) { |
| 1330 | /* Dynamic controls. */ |
| 1331 | case UVCIOC_CTRL_MAP: |
| 1332 | return uvc_ioctl_ctrl_map(chain, arg); |
| 1333 | |
| 1334 | case UVCIOC_CTRL_QUERY: |
| 1335 | return uvc_xu_ctrl_query(chain, arg); |
| 1336 | |
| 1337 | default: |
| 1338 | return -ENOTTY; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | #ifdef CONFIG_COMPAT |
| 1343 | struct uvc_xu_control_mapping32 { |
| 1344 | u32 id; |
| 1345 | u8 name[32]; |
| 1346 | u8 entity[16]; |
| 1347 | u8 selector; |
| 1348 | |
| 1349 | u8 size; |
| 1350 | u8 offset; |
| 1351 | u32 v4l2_type; |
| 1352 | u32 data_type; |
| 1353 | |
| 1354 | compat_caddr_t menu_info; |
| 1355 | u32 menu_count; |
| 1356 | |
| 1357 | u32 reserved[4]; |
| 1358 | }; |
| 1359 | |
| 1360 | static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp, |
| 1361 | const struct uvc_xu_control_mapping32 __user *up) |
| 1362 | { |
| 1363 | struct uvc_xu_control_mapping32 *p = (void *)kp; |
| 1364 | compat_caddr_t info; |
| 1365 | u32 count; |
| 1366 | |
| 1367 | if (copy_from_user(p, up, sizeof(*p))) |
| 1368 | return -EFAULT; |
| 1369 | |
| 1370 | count = p->menu_count; |
| 1371 | info = p->menu_info; |
| 1372 | |
| 1373 | memset(kp->reserved, 0, sizeof(kp->reserved)); |
| 1374 | kp->menu_info = count ? compat_ptr(info) : NULL; |
| 1375 | kp->menu_count = count; |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
| 1379 | static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp, |
| 1380 | struct uvc_xu_control_mapping32 __user *up) |
| 1381 | { |
| 1382 | if (copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) || |
| 1383 | put_user(kp->menu_count, &up->menu_count)) |
| 1384 | return -EFAULT; |
| 1385 | |
| 1386 | if (clear_user(up->reserved, sizeof(up->reserved))) |
| 1387 | return -EFAULT; |
| 1388 | |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
| 1392 | struct uvc_xu_control_query32 { |
| 1393 | u8 unit; |
| 1394 | u8 selector; |
| 1395 | u8 query; |
| 1396 | u16 size; |
| 1397 | compat_caddr_t data; |
| 1398 | }; |
| 1399 | |
| 1400 | static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp, |
| 1401 | const struct uvc_xu_control_query32 __user *up) |
| 1402 | { |
| 1403 | struct uvc_xu_control_query32 v; |
| 1404 | |
| 1405 | if (copy_from_user(&v, up, sizeof(v))) |
| 1406 | return -EFAULT; |
| 1407 | |
| 1408 | *kp = (struct uvc_xu_control_query){ |
| 1409 | .unit = v.unit, |
| 1410 | .selector = v.selector, |
| 1411 | .query = v.query, |
| 1412 | .size = v.size, |
| 1413 | .data = v.size ? compat_ptr(v.data) : NULL |
| 1414 | }; |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
| 1418 | static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp, |
| 1419 | struct uvc_xu_control_query32 __user *up) |
| 1420 | { |
| 1421 | if (copy_to_user(up, kp, offsetof(typeof(*up), data))) |
| 1422 | return -EFAULT; |
| 1423 | return 0; |
| 1424 | } |
| 1425 | |
| 1426 | #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32) |
| 1427 | #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32) |
| 1428 | |
| 1429 | static long uvc_v4l2_compat_ioctl32(struct file *file, |
| 1430 | unsigned int cmd, unsigned long arg) |
| 1431 | { |
| 1432 | struct uvc_fh *handle = file->private_data; |
| 1433 | union { |
| 1434 | struct uvc_xu_control_mapping xmap; |
| 1435 | struct uvc_xu_control_query xqry; |
| 1436 | } karg; |
| 1437 | void __user *up = compat_ptr(arg); |
| 1438 | long ret; |
| 1439 | |
| 1440 | switch (cmd) { |
| 1441 | case UVCIOC_CTRL_MAP32: |
| 1442 | ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up); |
| 1443 | if (ret) |
| 1444 | return ret; |
| 1445 | ret = uvc_ioctl_ctrl_map(handle->chain, &karg.xmap); |
| 1446 | if (ret) |
| 1447 | return ret; |
| 1448 | ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up); |
| 1449 | if (ret) |
| 1450 | return ret; |
| 1451 | |
| 1452 | break; |
| 1453 | |
| 1454 | case UVCIOC_CTRL_QUERY32: |
| 1455 | ret = uvc_v4l2_get_xu_query(&karg.xqry, up); |
| 1456 | if (ret) |
| 1457 | return ret; |
| 1458 | ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry); |
| 1459 | if (ret) |
| 1460 | return ret; |
| 1461 | ret = uvc_v4l2_put_xu_query(&karg.xqry, up); |
| 1462 | if (ret) |
| 1463 | return ret; |
| 1464 | break; |
| 1465 | |
| 1466 | default: |
| 1467 | return -ENOIOCTLCMD; |
| 1468 | } |
| 1469 | |
| 1470 | return ret; |
| 1471 | } |
| 1472 | #endif |
| 1473 | |
| 1474 | static ssize_t uvc_v4l2_read(struct file *file, char __user *data, |
| 1475 | size_t count, loff_t *ppos) |
| 1476 | { |
| 1477 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n"); |
| 1478 | return -EINVAL; |
| 1479 | } |
| 1480 | |
| 1481 | static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma) |
| 1482 | { |
| 1483 | struct uvc_fh *handle = file->private_data; |
| 1484 | struct uvc_streaming *stream = handle->stream; |
| 1485 | |
| 1486 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n"); |
| 1487 | |
| 1488 | return uvc_queue_mmap(&stream->queue, vma); |
| 1489 | } |
| 1490 | |
| 1491 | static __poll_t uvc_v4l2_poll(struct file *file, poll_table *wait) |
| 1492 | { |
| 1493 | struct uvc_fh *handle = file->private_data; |
| 1494 | struct uvc_streaming *stream = handle->stream; |
| 1495 | |
| 1496 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n"); |
| 1497 | |
| 1498 | return uvc_queue_poll(&stream->queue, file, wait); |
| 1499 | } |
| 1500 | |
| 1501 | #ifndef CONFIG_MMU |
| 1502 | static unsigned long uvc_v4l2_get_unmapped_area(struct file *file, |
| 1503 | unsigned long addr, unsigned long len, unsigned long pgoff, |
| 1504 | unsigned long flags) |
| 1505 | { |
| 1506 | struct uvc_fh *handle = file->private_data; |
| 1507 | struct uvc_streaming *stream = handle->stream; |
| 1508 | |
| 1509 | uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n"); |
| 1510 | |
| 1511 | return uvc_queue_get_unmapped_area(&stream->queue, pgoff); |
| 1512 | } |
| 1513 | #endif |
| 1514 | |
| 1515 | const struct v4l2_ioctl_ops uvc_ioctl_ops = { |
| 1516 | .vidioc_querycap = uvc_ioctl_querycap, |
| 1517 | .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt_vid_cap, |
| 1518 | .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt_vid_out, |
| 1519 | .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt_vid_cap, |
| 1520 | .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt_vid_out, |
| 1521 | .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt_vid_cap, |
| 1522 | .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt_vid_out, |
| 1523 | .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt_vid_cap, |
| 1524 | .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt_vid_out, |
| 1525 | .vidioc_reqbufs = uvc_ioctl_reqbufs, |
| 1526 | .vidioc_querybuf = uvc_ioctl_querybuf, |
| 1527 | .vidioc_qbuf = uvc_ioctl_qbuf, |
| 1528 | .vidioc_expbuf = uvc_ioctl_expbuf, |
| 1529 | .vidioc_dqbuf = uvc_ioctl_dqbuf, |
| 1530 | .vidioc_create_bufs = uvc_ioctl_create_bufs, |
| 1531 | .vidioc_streamon = uvc_ioctl_streamon, |
| 1532 | .vidioc_streamoff = uvc_ioctl_streamoff, |
| 1533 | .vidioc_enum_input = uvc_ioctl_enum_input, |
| 1534 | .vidioc_g_input = uvc_ioctl_g_input, |
| 1535 | .vidioc_s_input = uvc_ioctl_s_input, |
| 1536 | .vidioc_queryctrl = uvc_ioctl_queryctrl, |
| 1537 | .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl, |
| 1538 | .vidioc_g_ctrl = uvc_ioctl_g_ctrl, |
| 1539 | .vidioc_s_ctrl = uvc_ioctl_s_ctrl, |
| 1540 | .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls, |
| 1541 | .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls, |
| 1542 | .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls, |
| 1543 | .vidioc_querymenu = uvc_ioctl_querymenu, |
| 1544 | .vidioc_g_selection = uvc_ioctl_g_selection, |
| 1545 | .vidioc_g_parm = uvc_ioctl_g_parm, |
| 1546 | .vidioc_s_parm = uvc_ioctl_s_parm, |
| 1547 | .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes, |
| 1548 | .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals, |
| 1549 | .vidioc_subscribe_event = uvc_ioctl_subscribe_event, |
| 1550 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
| 1551 | .vidioc_default = uvc_ioctl_default, |
| 1552 | }; |
| 1553 | |
| 1554 | const struct v4l2_file_operations uvc_fops = { |
| 1555 | .owner = THIS_MODULE, |
| 1556 | .open = uvc_v4l2_open, |
| 1557 | .release = uvc_v4l2_release, |
| 1558 | .unlocked_ioctl = video_ioctl2, |
| 1559 | #ifdef CONFIG_COMPAT |
| 1560 | .compat_ioctl32 = uvc_v4l2_compat_ioctl32, |
| 1561 | #endif |
| 1562 | .read = uvc_v4l2_read, |
| 1563 | .mmap = uvc_v4l2_mmap, |
| 1564 | .poll = uvc_v4l2_poll, |
| 1565 | #ifndef CONFIG_MMU |
| 1566 | .get_unmapped_area = uvc_v4l2_get_unmapped_area, |
| 1567 | #endif |
| 1568 | }; |
| 1569 | |