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