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_control_add_xu_mapping(struct uvc_video_chain *chain, 51 struct uvc_control_mapping *map, 52 const struct uvc_xu_control_mapping *xmap) 53 { 54 unsigned int i; 55 size_t size; 56 int ret; 57 58 /* 59 * Prevent excessive memory consumption, as well as integer 60 * overflows. 61 */ 62 if (xmap->menu_count == 0 || 63 xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) 64 return -EINVAL; 65 66 map->menu_names = NULL; 67 map->menu_mapping = NULL; 68 69 map->menu_mask = GENMASK(xmap->menu_count - 1, 0); 70 71 size = xmap->menu_count * sizeof(*map->menu_mapping); 72 map->menu_mapping = kzalloc(size, GFP_KERNEL); 73 if (!map->menu_mapping) { 74 ret = -ENOMEM; 75 goto done; 76 } 77 78 for (i = 0; i < xmap->menu_count ; i++) { 79 if (copy_from_user((u32 *)&map->menu_mapping[i], 80 &xmap->menu_info[i].value, 81 sizeof(map->menu_mapping[i]))) { 82 ret = -EACCES; 83 goto done; 84 } 85 } 86 87 /* 88 * Always use the standard naming if available, otherwise copy the 89 * names supplied by userspace. 90 */ 91 if (!v4l2_ctrl_get_menu(map->id)) { 92 size = xmap->menu_count * sizeof(map->menu_names[0]); 93 map->menu_names = kzalloc(size, GFP_KERNEL); 94 if (!map->menu_names) { 95 ret = -ENOMEM; 96 goto done; 97 } 98 99 for (i = 0; i < xmap->menu_count ; i++) { 100 /* sizeof(names[i]) - 1: to take care of \0 */ 101 if (copy_from_user((char *)map->menu_names[i], 102 xmap->menu_info[i].name, 103 sizeof(map->menu_names[i]) - 1)) { 104 ret = -EACCES; 105 goto done; 106 } 107 } 108 } 109 110 ret = uvc_ctrl_add_mapping(chain, map); 111 112 done: 113 kfree(map->menu_names); 114 map->menu_names = NULL; 115 kfree(map->menu_mapping); 116 map->menu_mapping = NULL; 117 118 return ret; 119 } 120 121 /* ------------------------------------------------------------------------ 122 * UVC ioctls 123 */ 124 static int uvc_ioctl_xu_ctrl_map(struct uvc_video_chain *chain, 125 struct uvc_xu_control_mapping *xmap) 126 { 127 struct uvc_control_mapping *map; 128 int ret; 129 130 if (xmap->data_type > UVC_CTRL_DATA_TYPE_BITMASK) { 131 uvc_dbg(chain->dev, CONTROL, 132 "Unsupported UVC data type %u\n", xmap->data_type); 133 return -EINVAL; 134 } 135 136 map = kzalloc_obj(*map); 137 if (map == NULL) 138 return -ENOMEM; 139 140 map->id = xmap->id; 141 /* Non standard control id. */ 142 if (v4l2_ctrl_get_name(map->id) == NULL) { 143 if (xmap->name[0] == '\0') { 144 ret = -EINVAL; 145 goto free_map; 146 } 147 xmap->name[sizeof(xmap->name) - 1] = '\0'; 148 map->name = xmap->name; 149 } 150 memcpy(map->entity, xmap->entity, sizeof(map->entity)); 151 map->selector = xmap->selector; 152 map->size = xmap->size; 153 map->offset = xmap->offset; 154 map->v4l2_type = xmap->v4l2_type; 155 map->data_type = xmap->data_type; 156 157 switch (xmap->v4l2_type) { 158 case V4L2_CTRL_TYPE_INTEGER: 159 case V4L2_CTRL_TYPE_BOOLEAN: 160 case V4L2_CTRL_TYPE_BUTTON: 161 ret = uvc_ctrl_add_mapping(chain, map); 162 break; 163 164 case V4L2_CTRL_TYPE_MENU: 165 ret = uvc_control_add_xu_mapping(chain, map, xmap); 166 break; 167 168 default: 169 uvc_dbg(chain->dev, CONTROL, 170 "Unsupported V4L2 control type %u\n", xmap->v4l2_type); 171 ret = -ENOTTY; 172 break; 173 } 174 175 free_map: 176 kfree(map); 177 178 return ret; 179 } 180 181 /* ------------------------------------------------------------------------ 182 * V4L2 interface 183 */ 184 185 /* 186 * Find the frame interval closest to the requested frame interval for the 187 * given frame format and size. This should be done by the device as part of 188 * the Video Probe and Commit negotiation, but some hardware don't implement 189 * that feature. 190 */ 191 static u32 uvc_try_frame_interval(const struct uvc_frame *frame, u32 interval) 192 { 193 unsigned int i; 194 195 if (frame->bFrameIntervalType) { 196 u32 best = -1, dist; 197 198 for (i = 0; i < frame->bFrameIntervalType; ++i) { 199 dist = interval > frame->dwFrameInterval[i] 200 ? interval - frame->dwFrameInterval[i] 201 : frame->dwFrameInterval[i] - interval; 202 203 if (dist > best) 204 break; 205 206 best = dist; 207 } 208 209 interval = frame->dwFrameInterval[i-1]; 210 } else { 211 const u32 min = frame->dwFrameInterval[0]; 212 const u32 max = frame->dwFrameInterval[1]; 213 const u32 step = frame->dwFrameInterval[2]; 214 215 interval = min + (interval - min + step/2) / step * step; 216 if (interval > max) 217 interval = max; 218 } 219 220 return interval; 221 } 222 223 static u32 uvc_v4l2_get_bytesperline(const struct uvc_format *format, 224 const struct uvc_frame *frame) 225 { 226 switch (format->fcc) { 227 case V4L2_PIX_FMT_NV12: 228 case V4L2_PIX_FMT_YVU420: 229 case V4L2_PIX_FMT_YUV420: 230 case V4L2_PIX_FMT_M420: 231 return frame->wWidth; 232 233 case V4L2_PIX_FMT_P010: 234 return frame->wWidth * 2; 235 236 default: 237 return format->bpp * frame->wWidth / 8; 238 } 239 } 240 241 static int uvc_v4l2_try_format(struct uvc_streaming *stream, 242 struct v4l2_format *fmt, struct uvc_streaming_control *probe, 243 const struct uvc_format **uvc_format, 244 const struct uvc_frame **uvc_frame) 245 { 246 const struct uvc_format *format = NULL; 247 const struct uvc_frame *frame = NULL; 248 u16 rw, rh; 249 unsigned int d, maxd; 250 unsigned int i; 251 u32 interval; 252 int ret = 0; 253 u8 *fcc; 254 255 if (fmt->type != stream->type) 256 return -EINVAL; 257 258 fcc = (u8 *)&fmt->fmt.pix.pixelformat; 259 uvc_dbg(stream->dev, FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u\n", 260 fmt->fmt.pix.pixelformat, 261 fcc[0], fcc[1], fcc[2], fcc[3], 262 fmt->fmt.pix.width, fmt->fmt.pix.height); 263 264 /* 265 * Check if the hardware supports the requested format, use the default 266 * format otherwise. 267 */ 268 for (i = 0; i < stream->nformats; ++i) { 269 format = &stream->formats[i]; 270 if (format->fcc == fmt->fmt.pix.pixelformat) 271 break; 272 } 273 274 if (i == stream->nformats) { 275 format = stream->def_format; 276 fmt->fmt.pix.pixelformat = format->fcc; 277 } 278 279 /* 280 * Find the closest image size. The distance between image sizes is 281 * the size in pixels of the non-overlapping regions between the 282 * requested size and the frame-specified size. 283 */ 284 rw = fmt->fmt.pix.width; 285 rh = fmt->fmt.pix.height; 286 maxd = (unsigned int)-1; 287 288 for (i = 0; i < format->nframes; ++i) { 289 u16 w = format->frames[i].wWidth; 290 u16 h = format->frames[i].wHeight; 291 292 d = min(w, rw) * min(h, rh); 293 d = w*h + rw*rh - 2*d; 294 if (d < maxd) { 295 maxd = d; 296 frame = &format->frames[i]; 297 } 298 299 if (maxd == 0) 300 break; 301 } 302 303 if (frame == NULL) { 304 uvc_dbg(stream->dev, FORMAT, "Unsupported size %ux%u\n", 305 fmt->fmt.pix.width, fmt->fmt.pix.height); 306 return -EINVAL; 307 } 308 309 /* Use the default frame interval. */ 310 interval = frame->dwDefaultFrameInterval; 311 uvc_dbg(stream->dev, FORMAT, 312 "Using default frame interval %u.%u us (%u.%u fps)\n", 313 interval / 10, interval % 10, 10000000 / interval, 314 (100000000 / interval) % 10); 315 316 /* Set the format index, frame index and frame interval. */ 317 memset(probe, 0, sizeof(*probe)); 318 probe->bmHint = 1; /* dwFrameInterval */ 319 probe->bFormatIndex = format->index; 320 probe->bFrameIndex = frame->bFrameIndex; 321 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval); 322 /* 323 * Some webcams stall the probe control set request when the 324 * dwMaxVideoFrameSize field is set to zero. The UVC specification 325 * clearly states that the field is read-only from the host, so this 326 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by 327 * the webcam to work around the problem. 328 * 329 * The workaround could probably be enabled for all webcams, so the 330 * quirk can be removed if needed. It's currently useful to detect 331 * webcam bugs and fix them before they hit the market (providing 332 * developers test their webcams with the Linux driver as well as with 333 * the Windows driver). 334 */ 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 if (ret < 0) 342 return ret; 343 344 /* 345 * After the probe, update fmt with the values returned from 346 * negotiation with the device. Some devices return invalid bFormatIndex 347 * and bFrameIndex values, in which case we can only assume they have 348 * accepted the requested format as-is. 349 */ 350 for (i = 0; i < stream->nformats; ++i) { 351 if (probe->bFormatIndex == stream->formats[i].index) { 352 format = &stream->formats[i]; 353 break; 354 } 355 } 356 357 if (i == stream->nformats) 358 uvc_dbg(stream->dev, FORMAT, 359 "Unknown bFormatIndex %u, using default\n", 360 probe->bFormatIndex); 361 362 for (i = 0; i < format->nframes; ++i) { 363 if (probe->bFrameIndex == format->frames[i].bFrameIndex) { 364 frame = &format->frames[i]; 365 break; 366 } 367 } 368 369 if (i == format->nframes) 370 uvc_dbg(stream->dev, FORMAT, 371 "Unknown bFrameIndex %u, using default\n", 372 probe->bFrameIndex); 373 374 fmt->fmt.pix.width = frame->wWidth; 375 fmt->fmt.pix.height = frame->wHeight; 376 fmt->fmt.pix.field = V4L2_FIELD_NONE; 377 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame); 378 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize; 379 fmt->fmt.pix.pixelformat = format->fcc; 380 fmt->fmt.pix.colorspace = format->colorspace; 381 fmt->fmt.pix.xfer_func = format->xfer_func; 382 fmt->fmt.pix.ycbcr_enc = format->ycbcr_enc; 383 384 if (uvc_format != NULL) 385 *uvc_format = format; 386 if (uvc_frame != NULL) 387 *uvc_frame = frame; 388 389 return ret; 390 } 391 392 static int uvc_ioctl_g_fmt(struct file *file, void *priv, 393 struct v4l2_format *fmt) 394 { 395 struct uvc_fh *handle = to_uvc_fh(file); 396 struct uvc_streaming *stream = handle->stream; 397 const struct uvc_format *format; 398 const struct uvc_frame *frame; 399 400 if (fmt->type != stream->type) 401 return -EINVAL; 402 403 format = stream->cur_format; 404 frame = stream->cur_frame; 405 406 if (!format || !frame) 407 return -EINVAL; 408 409 fmt->fmt.pix.pixelformat = format->fcc; 410 fmt->fmt.pix.width = frame->wWidth; 411 fmt->fmt.pix.height = frame->wHeight; 412 fmt->fmt.pix.field = V4L2_FIELD_NONE; 413 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame); 414 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize; 415 fmt->fmt.pix.colorspace = format->colorspace; 416 fmt->fmt.pix.xfer_func = format->xfer_func; 417 fmt->fmt.pix.ycbcr_enc = format->ycbcr_enc; 418 419 return 0; 420 } 421 422 static int uvc_ioctl_s_fmt(struct file *file, void *priv, 423 struct v4l2_format *fmt) 424 { 425 struct uvc_fh *handle = to_uvc_fh(file); 426 struct uvc_streaming *stream = handle->stream; 427 struct uvc_streaming_control probe; 428 const struct uvc_format *format; 429 const struct uvc_frame *frame; 430 int ret; 431 432 if (fmt->type != stream->type) 433 return -EINVAL; 434 435 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame); 436 if (ret < 0) 437 return ret; 438 439 if (vb2_is_busy(&stream->queue.queue)) 440 return -EBUSY; 441 442 stream->ctrl = probe; 443 stream->cur_format = format; 444 stream->cur_frame = frame; 445 446 return 0; 447 } 448 449 static int uvc_ioctl_g_parm(struct file *file, void *priv, 450 struct v4l2_streamparm *parm) 451 { 452 u32 numerator, denominator; 453 struct uvc_fh *handle = to_uvc_fh(file); 454 struct uvc_streaming *stream = handle->stream; 455 456 if (parm->type != stream->type) 457 return -EINVAL; 458 459 numerator = stream->ctrl.dwFrameInterval; 460 denominator = 10000000; 461 v4l2_simplify_fraction(&numerator, &denominator, 8, 333); 462 463 memset(parm, 0, sizeof(*parm)); 464 parm->type = stream->type; 465 466 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { 467 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 468 parm->parm.capture.capturemode = 0; 469 parm->parm.capture.timeperframe.numerator = numerator; 470 parm->parm.capture.timeperframe.denominator = denominator; 471 parm->parm.capture.extendedmode = 0; 472 parm->parm.capture.readbuffers = 0; 473 } else { 474 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME; 475 parm->parm.output.outputmode = 0; 476 parm->parm.output.timeperframe.numerator = numerator; 477 parm->parm.output.timeperframe.denominator = denominator; 478 } 479 480 return 0; 481 } 482 483 static int uvc_ioctl_s_parm(struct file *file, void *priv, 484 struct v4l2_streamparm *parm) 485 { 486 struct uvc_fh *handle = to_uvc_fh(file); 487 struct uvc_streaming *stream = handle->stream; 488 struct uvc_streaming_control probe; 489 struct v4l2_fract timeperframe; 490 const struct uvc_format *format; 491 const struct uvc_frame *frame; 492 u32 interval, maxd; 493 unsigned int i; 494 int ret; 495 496 if (parm->type != stream->type) 497 return -EINVAL; 498 499 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 500 timeperframe = parm->parm.capture.timeperframe; 501 else 502 timeperframe = parm->parm.output.timeperframe; 503 504 interval = v4l2_fraction_to_interval(timeperframe.numerator, 505 timeperframe.denominator); 506 uvc_dbg(stream->dev, FORMAT, "Setting frame interval to %u/%u (%u)\n", 507 timeperframe.numerator, timeperframe.denominator, interval); 508 509 if (uvc_queue_streaming(&stream->queue)) 510 return -EBUSY; 511 512 format = stream->cur_format; 513 frame = stream->cur_frame; 514 probe = stream->ctrl; 515 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval); 516 maxd = abs((s32)probe.dwFrameInterval - interval); 517 518 /* Try frames with matching size to find the best frame interval. */ 519 for (i = 0; i < format->nframes && maxd != 0; i++) { 520 u32 d, ival; 521 522 if (&format->frames[i] == stream->cur_frame) 523 continue; 524 525 if (format->frames[i].wWidth != stream->cur_frame->wWidth || 526 format->frames[i].wHeight != stream->cur_frame->wHeight) 527 continue; 528 529 ival = uvc_try_frame_interval(&format->frames[i], interval); 530 d = abs((s32)ival - interval); 531 if (d >= maxd) 532 continue; 533 534 frame = &format->frames[i]; 535 probe.bFrameIndex = frame->bFrameIndex; 536 probe.dwFrameInterval = ival; 537 maxd = d; 538 } 539 540 /* Probe the device with the new settings. */ 541 ret = uvc_probe_video(stream, &probe); 542 if (ret < 0) 543 return ret; 544 545 stream->ctrl = probe; 546 stream->cur_frame = frame; 547 548 /* Return the actual frame period. */ 549 timeperframe.numerator = probe.dwFrameInterval; 550 timeperframe.denominator = 10000000; 551 v4l2_simplify_fraction(&timeperframe.numerator, 552 &timeperframe.denominator, 8, 333); 553 554 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { 555 parm->parm.capture.timeperframe = timeperframe; 556 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 557 } else { 558 parm->parm.output.timeperframe = timeperframe; 559 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME; 560 } 561 562 return 0; 563 } 564 565 /* ------------------------------------------------------------------------ 566 * V4L2 file operations 567 */ 568 569 static int uvc_v4l2_open(struct file *file) 570 { 571 struct uvc_streaming *stream; 572 struct uvc_fh *handle; 573 574 stream = video_drvdata(file); 575 uvc_dbg(stream->dev, CALLS, "%s\n", __func__); 576 577 /* Create the device handle. */ 578 handle = kzalloc_obj(*handle); 579 if (!handle) 580 return -ENOMEM; 581 582 v4l2_fh_init(&handle->vfh, &stream->queue.vdev); 583 v4l2_fh_add(&handle->vfh, file); 584 handle->chain = stream->chain; 585 handle->stream = stream; 586 587 return 0; 588 } 589 590 static int uvc_v4l2_release(struct file *file) 591 { 592 struct uvc_fh *handle = to_uvc_fh(file); 593 struct uvc_streaming *stream = handle->stream; 594 595 uvc_dbg(stream->dev, CALLS, "%s\n", __func__); 596 597 uvc_ctrl_cleanup_fh(handle); 598 599 /* Release the file handle. */ 600 vb2_fop_release(file); 601 602 return 0; 603 } 604 605 static int uvc_ioctl_querycap(struct file *file, void *priv, 606 struct v4l2_capability *cap) 607 { 608 struct uvc_fh *handle = to_uvc_fh(file); 609 struct uvc_video_chain *chain = handle->chain; 610 struct uvc_streaming *stream = handle->stream; 611 612 strscpy(cap->driver, "uvcvideo", sizeof(cap->driver)); 613 strscpy(cap->card, handle->stream->dev->name, sizeof(cap->card)); 614 usb_make_path(stream->dev->udev, cap->bus_info, sizeof(cap->bus_info)); 615 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING 616 | chain->caps; 617 618 return 0; 619 } 620 621 static int uvc_ioctl_enum_fmt(struct file *file, void *priv, 622 struct v4l2_fmtdesc *fmt) 623 { 624 struct uvc_fh *handle = to_uvc_fh(file); 625 struct uvc_streaming *stream = handle->stream; 626 enum v4l2_buf_type type = fmt->type; 627 const struct uvc_format *format; 628 u32 index = fmt->index; 629 630 if (fmt->type != stream->type || fmt->index >= stream->nformats) 631 return -EINVAL; 632 633 memset(fmt, 0, sizeof(*fmt)); 634 fmt->index = index; 635 fmt->type = type; 636 637 format = &stream->formats[fmt->index]; 638 fmt->flags = 0; 639 if (format->flags & UVC_FMT_FLAG_COMPRESSED) 640 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED; 641 fmt->pixelformat = format->fcc; 642 return 0; 643 } 644 645 static int uvc_ioctl_try_fmt(struct file *file, void *priv, 646 struct v4l2_format *fmt) 647 { 648 struct uvc_fh *handle = to_uvc_fh(file); 649 struct uvc_streaming *stream = handle->stream; 650 struct uvc_streaming_control probe; 651 652 return uvc_v4l2_try_format(stream, fmt, &probe, NULL, NULL); 653 } 654 655 static int uvc_ioctl_enum_input(struct file *file, void *priv, 656 struct v4l2_input *input) 657 { 658 struct uvc_fh *handle = to_uvc_fh(file); 659 struct uvc_video_chain *chain = handle->chain; 660 const struct uvc_entity *selector = chain->selector; 661 struct uvc_entity *iterm = NULL; 662 struct uvc_entity *it; 663 u32 index = input->index; 664 665 if (selector == NULL || 666 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { 667 if (index != 0) 668 return -EINVAL; 669 list_for_each_entry(it, &chain->entities, chain) { 670 if (UVC_ENTITY_IS_ITERM(it)) { 671 iterm = it; 672 break; 673 } 674 } 675 } else if (index < selector->bNrInPins) { 676 list_for_each_entry(it, &chain->entities, chain) { 677 if (!UVC_ENTITY_IS_ITERM(it)) 678 continue; 679 if (it->id == selector->baSourceID[index]) { 680 iterm = it; 681 break; 682 } 683 } 684 } 685 686 if (iterm == NULL) 687 return -EINVAL; 688 689 memset(input, 0, sizeof(*input)); 690 input->index = index; 691 strscpy(input->name, iterm->name, sizeof(input->name)); 692 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA) 693 input->type = V4L2_INPUT_TYPE_CAMERA; 694 695 return 0; 696 } 697 698 static int uvc_ioctl_g_input(struct file *file, void *priv, unsigned int *input) 699 { 700 struct uvc_fh *handle = to_uvc_fh(file); 701 struct uvc_video_chain *chain = handle->chain; 702 u8 *buf; 703 int ret; 704 705 if (chain->selector == NULL || 706 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { 707 *input = 0; 708 return 0; 709 } 710 711 buf = kmalloc(1, GFP_KERNEL); 712 if (!buf) 713 return -ENOMEM; 714 715 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id, 716 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL, 717 buf, 1); 718 if (!ret) 719 *input = *buf - 1; 720 721 kfree(buf); 722 723 return ret; 724 } 725 726 static int uvc_ioctl_s_input(struct file *file, void *priv, unsigned int input) 727 { 728 struct uvc_fh *handle = to_uvc_fh(file); 729 struct uvc_streaming *stream = handle->stream; 730 struct uvc_video_chain *chain = handle->chain; 731 u8 *buf; 732 int ret; 733 734 if (vb2_is_busy(&stream->queue.queue)) 735 return -EBUSY; 736 737 if (chain->selector == NULL || 738 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { 739 if (input) 740 return -EINVAL; 741 return 0; 742 } 743 744 if (input >= chain->selector->bNrInPins) 745 return -EINVAL; 746 747 buf = kmalloc(1, GFP_KERNEL); 748 if (!buf) 749 return -ENOMEM; 750 751 *buf = input + 1; 752 ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id, 753 chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL, 754 buf, 1); 755 kfree(buf); 756 757 return ret; 758 } 759 760 static int uvc_ioctl_query_ext_ctrl(struct file *file, void *priv, 761 struct v4l2_query_ext_ctrl *qec) 762 { 763 struct uvc_fh *handle = to_uvc_fh(file); 764 struct uvc_video_chain *chain = handle->chain; 765 766 return uvc_query_v4l2_ctrl(chain, qec); 767 } 768 769 static int uvc_ctrl_check_access(struct uvc_video_chain *chain, 770 struct v4l2_ext_controls *ctrls, 771 u32 which, unsigned long ioctl) 772 { 773 struct v4l2_ext_control *ctrl = ctrls->controls; 774 unsigned int i; 775 int ret = 0; 776 777 for (i = 0; i < ctrls->count; ++ctrl, ++i) { 778 ret = uvc_ctrl_is_accessible(chain, ctrl->id, ctrls, which, 779 ioctl); 780 if (ret) 781 break; 782 } 783 784 ctrls->error_idx = ioctl == VIDIOC_TRY_EXT_CTRLS ? i : ctrls->count; 785 786 return ret; 787 } 788 789 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *priv, 790 struct v4l2_ext_controls *ctrls) 791 { 792 struct uvc_fh *handle = to_uvc_fh(file); 793 struct uvc_video_chain *chain = handle->chain; 794 struct v4l2_ext_control *ctrl = ctrls->controls; 795 unsigned int i; 796 u32 which; 797 int ret; 798 799 if (!ctrls->count) 800 return 0; 801 802 switch (ctrls->which) { 803 case V4L2_CTRL_WHICH_DEF_VAL: 804 case V4L2_CTRL_WHICH_CUR_VAL: 805 case V4L2_CTRL_WHICH_MAX_VAL: 806 case V4L2_CTRL_WHICH_MIN_VAL: 807 which = ctrls->which; 808 break; 809 default: 810 which = V4L2_CTRL_WHICH_CUR_VAL; 811 } 812 813 ret = uvc_ctrl_check_access(chain, ctrls, which, VIDIOC_G_EXT_CTRLS); 814 if (ret < 0) 815 return ret; 816 817 ret = uvc_ctrl_begin(chain); 818 if (ret < 0) 819 return ret; 820 821 for (i = 0; i < ctrls->count; ++ctrl, ++i) { 822 ret = uvc_ctrl_get(chain, which, ctrl); 823 if (ret < 0) { 824 uvc_ctrl_rollback(handle); 825 ctrls->error_idx = i; 826 return ret; 827 } 828 } 829 830 ctrls->error_idx = 0; 831 832 return uvc_ctrl_rollback(handle); 833 } 834 835 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle, 836 struct v4l2_ext_controls *ctrls, 837 unsigned long ioctl) 838 { 839 struct v4l2_ext_control *ctrl = ctrls->controls; 840 struct uvc_video_chain *chain = handle->chain; 841 unsigned int i; 842 int ret; 843 844 if (!ctrls->count) 845 return 0; 846 847 ret = uvc_ctrl_check_access(chain, ctrls, V4L2_CTRL_WHICH_CUR_VAL, 848 ioctl); 849 if (ret < 0) 850 return ret; 851 852 ret = uvc_ctrl_begin(chain); 853 if (ret < 0) 854 return ret; 855 856 for (i = 0; i < ctrls->count; ++ctrl, ++i) { 857 ret = uvc_ctrl_set(handle, ctrl); 858 if (ret < 0) { 859 uvc_ctrl_rollback(handle); 860 ctrls->error_idx = ioctl == VIDIOC_S_EXT_CTRLS ? 861 ctrls->count : i; 862 return ret; 863 } 864 } 865 866 ctrls->error_idx = 0; 867 868 if (ioctl == VIDIOC_S_EXT_CTRLS) 869 return uvc_ctrl_commit(handle, ctrls); 870 else 871 return uvc_ctrl_rollback(handle); 872 } 873 874 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *priv, 875 struct v4l2_ext_controls *ctrls) 876 { 877 struct uvc_fh *handle = to_uvc_fh(file); 878 879 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, VIDIOC_S_EXT_CTRLS); 880 } 881 882 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *priv, 883 struct v4l2_ext_controls *ctrls) 884 { 885 struct uvc_fh *handle = to_uvc_fh(file); 886 887 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, VIDIOC_TRY_EXT_CTRLS); 888 } 889 890 static int uvc_ioctl_querymenu(struct file *file, void *priv, 891 struct v4l2_querymenu *qm) 892 { 893 struct uvc_fh *handle = to_uvc_fh(file); 894 struct uvc_video_chain *chain = handle->chain; 895 896 return uvc_query_v4l2_menu(chain, qm); 897 } 898 899 static int uvc_ioctl_g_selection(struct file *file, void *priv, 900 struct v4l2_selection *sel) 901 { 902 struct uvc_fh *handle = to_uvc_fh(file); 903 struct uvc_streaming *stream = handle->stream; 904 905 if (sel->type != stream->type) 906 return -EINVAL; 907 908 switch (sel->target) { 909 case V4L2_SEL_TGT_CROP_DEFAULT: 910 case V4L2_SEL_TGT_CROP_BOUNDS: 911 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 912 return -EINVAL; 913 break; 914 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 915 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 916 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 917 return -EINVAL; 918 break; 919 default: 920 return -EINVAL; 921 } 922 923 sel->r.left = 0; 924 sel->r.top = 0; 925 sel->r.width = stream->cur_frame->wWidth; 926 sel->r.height = stream->cur_frame->wHeight; 927 928 return 0; 929 } 930 931 static int uvc_ioctl_enum_framesizes(struct file *file, void *priv, 932 struct v4l2_frmsizeenum *fsize) 933 { 934 struct uvc_fh *handle = to_uvc_fh(file); 935 struct uvc_streaming *stream = handle->stream; 936 const struct uvc_format *format = NULL; 937 const struct uvc_frame *frame = NULL; 938 unsigned int index; 939 unsigned int i; 940 941 /* Look for the given pixel format */ 942 for (i = 0; i < stream->nformats; i++) { 943 if (stream->formats[i].fcc == fsize->pixel_format) { 944 format = &stream->formats[i]; 945 break; 946 } 947 } 948 if (format == NULL) 949 return -EINVAL; 950 951 /* Skip duplicate frame sizes */ 952 for (i = 0, index = 0; i < format->nframes; i++) { 953 if (frame && frame->wWidth == format->frames[i].wWidth && 954 frame->wHeight == format->frames[i].wHeight) 955 continue; 956 frame = &format->frames[i]; 957 if (index == fsize->index) 958 break; 959 index++; 960 } 961 962 if (i == format->nframes) 963 return -EINVAL; 964 965 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 966 fsize->discrete.width = frame->wWidth; 967 fsize->discrete.height = frame->wHeight; 968 return 0; 969 } 970 971 static int uvc_ioctl_enum_frameintervals(struct file *file, void *priv, 972 struct v4l2_frmivalenum *fival) 973 { 974 struct uvc_fh *handle = to_uvc_fh(file); 975 struct uvc_streaming *stream = handle->stream; 976 const struct uvc_format *format = NULL; 977 const struct uvc_frame *frame = NULL; 978 unsigned int nintervals; 979 unsigned int index; 980 unsigned int i; 981 982 /* Look for the given pixel format and frame size */ 983 for (i = 0; i < stream->nformats; i++) { 984 if (stream->formats[i].fcc == fival->pixel_format) { 985 format = &stream->formats[i]; 986 break; 987 } 988 } 989 if (format == NULL) 990 return -EINVAL; 991 992 index = fival->index; 993 for (i = 0; i < format->nframes; i++) { 994 if (format->frames[i].wWidth == fival->width && 995 format->frames[i].wHeight == fival->height) { 996 frame = &format->frames[i]; 997 nintervals = frame->bFrameIntervalType ?: 1; 998 if (index < nintervals) 999 break; 1000 index -= nintervals; 1001 } 1002 } 1003 if (i == format->nframes) 1004 return -EINVAL; 1005 1006 if (frame->bFrameIntervalType) { 1007 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; 1008 fival->discrete.numerator = 1009 frame->dwFrameInterval[index]; 1010 fival->discrete.denominator = 10000000; 1011 v4l2_simplify_fraction(&fival->discrete.numerator, 1012 &fival->discrete.denominator, 8, 333); 1013 } else { 1014 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE; 1015 fival->stepwise.min.numerator = frame->dwFrameInterval[0]; 1016 fival->stepwise.min.denominator = 10000000; 1017 fival->stepwise.max.numerator = frame->dwFrameInterval[1]; 1018 fival->stepwise.max.denominator = 10000000; 1019 fival->stepwise.step.numerator = frame->dwFrameInterval[2]; 1020 fival->stepwise.step.denominator = 10000000; 1021 v4l2_simplify_fraction(&fival->stepwise.min.numerator, 1022 &fival->stepwise.min.denominator, 8, 333); 1023 v4l2_simplify_fraction(&fival->stepwise.max.numerator, 1024 &fival->stepwise.max.denominator, 8, 333); 1025 v4l2_simplify_fraction(&fival->stepwise.step.numerator, 1026 &fival->stepwise.step.denominator, 8, 333); 1027 } 1028 1029 return 0; 1030 } 1031 1032 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh, 1033 const struct v4l2_event_subscription *sub) 1034 { 1035 switch (sub->type) { 1036 case V4L2_EVENT_CTRL: 1037 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops); 1038 default: 1039 return -EINVAL; 1040 } 1041 } 1042 1043 static long uvc_ioctl_default(struct file *file, void *priv, bool valid_prio, 1044 unsigned int cmd, void *arg) 1045 { 1046 struct uvc_fh *handle = to_uvc_fh(file); 1047 struct uvc_video_chain *chain = handle->chain; 1048 1049 switch (cmd) { 1050 /* Dynamic controls. */ 1051 case UVCIOC_CTRL_MAP: 1052 return uvc_ioctl_xu_ctrl_map(chain, arg); 1053 1054 case UVCIOC_CTRL_QUERY: 1055 return uvc_xu_ctrl_query(chain, arg); 1056 1057 default: 1058 return -ENOTTY; 1059 } 1060 } 1061 1062 #ifdef CONFIG_COMPAT 1063 struct uvc_xu_control_mapping32 { 1064 u32 id; 1065 u8 name[32]; 1066 u8 entity[16]; 1067 u8 selector; 1068 1069 u8 size; 1070 u8 offset; 1071 u32 v4l2_type; 1072 u32 data_type; 1073 1074 compat_caddr_t menu_info; 1075 u32 menu_count; 1076 1077 u32 reserved[4]; 1078 }; 1079 1080 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp, 1081 const struct uvc_xu_control_mapping32 __user *up) 1082 { 1083 struct uvc_xu_control_mapping32 *p = (void *)kp; 1084 compat_caddr_t info; 1085 u32 count; 1086 1087 if (copy_from_user(p, up, sizeof(*p))) 1088 return -EFAULT; 1089 1090 count = p->menu_count; 1091 info = p->menu_info; 1092 1093 memset(kp->reserved, 0, sizeof(kp->reserved)); 1094 kp->menu_info = count ? compat_ptr(info) : NULL; 1095 kp->menu_count = count; 1096 return 0; 1097 } 1098 1099 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp, 1100 struct uvc_xu_control_mapping32 __user *up) 1101 { 1102 if (copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) || 1103 put_user(kp->menu_count, &up->menu_count)) 1104 return -EFAULT; 1105 1106 if (clear_user(up->reserved, sizeof(up->reserved))) 1107 return -EFAULT; 1108 1109 return 0; 1110 } 1111 1112 struct uvc_xu_control_query32 { 1113 u8 unit; 1114 u8 selector; 1115 u8 query; 1116 u16 size; 1117 compat_caddr_t data; 1118 }; 1119 1120 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp, 1121 const struct uvc_xu_control_query32 __user *up) 1122 { 1123 struct uvc_xu_control_query32 v; 1124 1125 if (copy_from_user(&v, up, sizeof(v))) 1126 return -EFAULT; 1127 1128 *kp = (struct uvc_xu_control_query){ 1129 .unit = v.unit, 1130 .selector = v.selector, 1131 .query = v.query, 1132 .size = v.size, 1133 .data = v.size ? compat_ptr(v.data) : NULL 1134 }; 1135 return 0; 1136 } 1137 1138 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp, 1139 struct uvc_xu_control_query32 __user *up) 1140 { 1141 if (copy_to_user(up, kp, offsetof(typeof(*up), data))) 1142 return -EFAULT; 1143 return 0; 1144 } 1145 1146 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32) 1147 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32) 1148 1149 static long uvc_v4l2_compat_ioctl32(struct file *file, 1150 unsigned int cmd, unsigned long arg) 1151 { 1152 struct uvc_fh *handle = to_uvc_fh(file); 1153 union { 1154 struct uvc_xu_control_mapping xmap; 1155 struct uvc_xu_control_query xqry; 1156 } karg; 1157 void __user *up = compat_ptr(arg); 1158 long ret; 1159 1160 ret = uvc_pm_get(handle->stream->dev); 1161 if (ret) 1162 return ret; 1163 1164 switch (cmd) { 1165 case UVCIOC_CTRL_MAP32: 1166 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up); 1167 if (ret) 1168 break; 1169 ret = uvc_ioctl_xu_ctrl_map(handle->chain, &karg.xmap); 1170 if (ret) 1171 break; 1172 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up); 1173 if (ret) 1174 break; 1175 break; 1176 1177 case UVCIOC_CTRL_QUERY32: 1178 ret = uvc_v4l2_get_xu_query(&karg.xqry, up); 1179 if (ret) 1180 break; 1181 ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry); 1182 if (ret) 1183 break; 1184 ret = uvc_v4l2_put_xu_query(&karg.xqry, up); 1185 if (ret) 1186 break; 1187 break; 1188 1189 default: 1190 ret = -ENOIOCTLCMD; 1191 break; 1192 } 1193 1194 uvc_pm_put(handle->stream->dev); 1195 1196 return ret; 1197 } 1198 #endif 1199 1200 static long uvc_v4l2_unlocked_ioctl(struct file *file, 1201 unsigned int cmd, unsigned long arg) 1202 { 1203 struct uvc_fh *handle = to_uvc_fh(file); 1204 unsigned int converted_cmd = v4l2_translate_cmd(cmd); 1205 int ret; 1206 1207 /* The following IOCTLs need to turn on the camera. */ 1208 switch (converted_cmd) { 1209 case UVCIOC_CTRL_MAP: 1210 case UVCIOC_CTRL_QUERY: 1211 case VIDIOC_G_CTRL: 1212 case VIDIOC_G_EXT_CTRLS: 1213 case VIDIOC_G_INPUT: 1214 case VIDIOC_QUERYCTRL: 1215 case VIDIOC_QUERYMENU: 1216 case VIDIOC_QUERY_EXT_CTRL: 1217 case VIDIOC_S_CTRL: 1218 case VIDIOC_S_EXT_CTRLS: 1219 case VIDIOC_S_FMT: 1220 case VIDIOC_S_INPUT: 1221 case VIDIOC_S_PARM: 1222 case VIDIOC_TRY_EXT_CTRLS: 1223 case VIDIOC_TRY_FMT: 1224 ret = uvc_pm_get(handle->stream->dev); 1225 if (ret) 1226 return ret; 1227 ret = video_ioctl2(file, cmd, arg); 1228 uvc_pm_put(handle->stream->dev); 1229 return ret; 1230 } 1231 1232 /* The other IOCTLs can run with the camera off. */ 1233 return video_ioctl2(file, cmd, arg); 1234 } 1235 1236 const struct v4l2_ioctl_ops uvc_ioctl_ops = { 1237 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt, 1238 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt, 1239 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt, 1240 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt, 1241 .vidioc_g_parm = uvc_ioctl_g_parm, 1242 .vidioc_s_parm = uvc_ioctl_s_parm, 1243 .vidioc_querycap = uvc_ioctl_querycap, 1244 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt, 1245 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt, 1246 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt, 1247 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt, 1248 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1249 .vidioc_querybuf = vb2_ioctl_querybuf, 1250 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 1251 .vidioc_qbuf = vb2_ioctl_qbuf, 1252 .vidioc_expbuf = vb2_ioctl_expbuf, 1253 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1254 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1255 .vidioc_streamon = vb2_ioctl_streamon, 1256 .vidioc_streamoff = vb2_ioctl_streamoff, 1257 .vidioc_enum_input = uvc_ioctl_enum_input, 1258 .vidioc_g_input = uvc_ioctl_g_input, 1259 .vidioc_s_input = uvc_ioctl_s_input, 1260 .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl, 1261 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls, 1262 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls, 1263 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls, 1264 .vidioc_querymenu = uvc_ioctl_querymenu, 1265 .vidioc_g_selection = uvc_ioctl_g_selection, 1266 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes, 1267 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals, 1268 .vidioc_subscribe_event = uvc_ioctl_subscribe_event, 1269 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1270 .vidioc_default = uvc_ioctl_default, 1271 }; 1272 1273 const struct v4l2_file_operations uvc_fops = { 1274 .owner = THIS_MODULE, 1275 .open = uvc_v4l2_open, 1276 .release = uvc_v4l2_release, 1277 .unlocked_ioctl = uvc_v4l2_unlocked_ioctl, 1278 #ifdef CONFIG_COMPAT 1279 .compat_ioctl32 = uvc_v4l2_compat_ioctl32, 1280 #endif 1281 .mmap = vb2_fop_mmap, 1282 .poll = vb2_fop_poll, 1283 #ifndef CONFIG_MMU 1284 .get_unmapped_area = vb2_fop_get_unmapped_area, 1285 #endif 1286 }; 1287 1288