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