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 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_obj(*handle); 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 u32 which, 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, which, 776 ioctl); 777 if (ret) 778 break; 779 } 780 781 ctrls->error_idx = ioctl == VIDIOC_TRY_EXT_CTRLS ? i : ctrls->count; 782 783 return ret; 784 } 785 786 static int uvc_ioctl_g_ext_ctrls(struct file *file, void *priv, 787 struct v4l2_ext_controls *ctrls) 788 { 789 struct uvc_fh *handle = to_uvc_fh(file); 790 struct uvc_video_chain *chain = handle->chain; 791 struct v4l2_ext_control *ctrl = ctrls->controls; 792 unsigned int i; 793 u32 which; 794 int ret; 795 796 if (!ctrls->count) 797 return 0; 798 799 switch (ctrls->which) { 800 case V4L2_CTRL_WHICH_DEF_VAL: 801 case V4L2_CTRL_WHICH_CUR_VAL: 802 case V4L2_CTRL_WHICH_MAX_VAL: 803 case V4L2_CTRL_WHICH_MIN_VAL: 804 which = ctrls->which; 805 break; 806 default: 807 which = V4L2_CTRL_WHICH_CUR_VAL; 808 } 809 810 ret = uvc_ctrl_check_access(chain, ctrls, which, VIDIOC_G_EXT_CTRLS); 811 if (ret < 0) 812 return ret; 813 814 ret = uvc_ctrl_begin(chain); 815 if (ret < 0) 816 return ret; 817 818 for (i = 0; i < ctrls->count; ++ctrl, ++i) { 819 ret = uvc_ctrl_get(chain, which, ctrl); 820 if (ret < 0) { 821 uvc_ctrl_rollback(handle); 822 ctrls->error_idx = i; 823 return ret; 824 } 825 } 826 827 ctrls->error_idx = 0; 828 829 return uvc_ctrl_rollback(handle); 830 } 831 832 static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle, 833 struct v4l2_ext_controls *ctrls, 834 unsigned long ioctl) 835 { 836 struct v4l2_ext_control *ctrl = ctrls->controls; 837 struct uvc_video_chain *chain = handle->chain; 838 unsigned int i; 839 int ret; 840 841 if (!ctrls->count) 842 return 0; 843 844 ret = uvc_ctrl_check_access(chain, ctrls, V4L2_CTRL_WHICH_CUR_VAL, 845 ioctl); 846 if (ret < 0) 847 return ret; 848 849 ret = uvc_ctrl_begin(chain); 850 if (ret < 0) 851 return ret; 852 853 for (i = 0; i < ctrls->count; ++ctrl, ++i) { 854 ret = uvc_ctrl_set(handle, ctrl); 855 if (ret < 0) { 856 uvc_ctrl_rollback(handle); 857 ctrls->error_idx = ioctl == VIDIOC_S_EXT_CTRLS ? 858 ctrls->count : i; 859 return ret; 860 } 861 } 862 863 ctrls->error_idx = 0; 864 865 if (ioctl == VIDIOC_S_EXT_CTRLS) 866 return uvc_ctrl_commit(handle, ctrls); 867 else 868 return uvc_ctrl_rollback(handle); 869 } 870 871 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *priv, 872 struct v4l2_ext_controls *ctrls) 873 { 874 struct uvc_fh *handle = to_uvc_fh(file); 875 876 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, VIDIOC_S_EXT_CTRLS); 877 } 878 879 static int uvc_ioctl_try_ext_ctrls(struct file *file, void *priv, 880 struct v4l2_ext_controls *ctrls) 881 { 882 struct uvc_fh *handle = to_uvc_fh(file); 883 884 return uvc_ioctl_s_try_ext_ctrls(handle, ctrls, VIDIOC_TRY_EXT_CTRLS); 885 } 886 887 static int uvc_ioctl_querymenu(struct file *file, void *priv, 888 struct v4l2_querymenu *qm) 889 { 890 struct uvc_fh *handle = to_uvc_fh(file); 891 struct uvc_video_chain *chain = handle->chain; 892 893 return uvc_query_v4l2_menu(chain, qm); 894 } 895 896 static int uvc_ioctl_g_selection(struct file *file, void *priv, 897 struct v4l2_selection *sel) 898 { 899 struct uvc_fh *handle = to_uvc_fh(file); 900 struct uvc_streaming *stream = handle->stream; 901 902 if (sel->type != stream->type) 903 return -EINVAL; 904 905 switch (sel->target) { 906 case V4L2_SEL_TGT_CROP_DEFAULT: 907 case V4L2_SEL_TGT_CROP_BOUNDS: 908 if (stream->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 909 return -EINVAL; 910 break; 911 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 912 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 913 if (stream->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 914 return -EINVAL; 915 break; 916 default: 917 return -EINVAL; 918 } 919 920 sel->r.left = 0; 921 sel->r.top = 0; 922 sel->r.width = stream->cur_frame->wWidth; 923 sel->r.height = stream->cur_frame->wHeight; 924 925 return 0; 926 } 927 928 static int uvc_ioctl_enum_framesizes(struct file *file, void *priv, 929 struct v4l2_frmsizeenum *fsize) 930 { 931 struct uvc_fh *handle = to_uvc_fh(file); 932 struct uvc_streaming *stream = handle->stream; 933 const struct uvc_format *format = NULL; 934 const struct uvc_frame *frame = NULL; 935 unsigned int index; 936 unsigned int i; 937 938 /* Look for the given pixel format */ 939 for (i = 0; i < stream->nformats; i++) { 940 if (stream->formats[i].fcc == fsize->pixel_format) { 941 format = &stream->formats[i]; 942 break; 943 } 944 } 945 if (format == NULL) 946 return -EINVAL; 947 948 /* Skip duplicate frame sizes */ 949 for (i = 0, index = 0; i < format->nframes; i++) { 950 if (frame && frame->wWidth == format->frames[i].wWidth && 951 frame->wHeight == format->frames[i].wHeight) 952 continue; 953 frame = &format->frames[i]; 954 if (index == fsize->index) 955 break; 956 index++; 957 } 958 959 if (i == format->nframes) 960 return -EINVAL; 961 962 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 963 fsize->discrete.width = frame->wWidth; 964 fsize->discrete.height = frame->wHeight; 965 return 0; 966 } 967 968 static int uvc_ioctl_enum_frameintervals(struct file *file, void *priv, 969 struct v4l2_frmivalenum *fival) 970 { 971 struct uvc_fh *handle = to_uvc_fh(file); 972 struct uvc_streaming *stream = handle->stream; 973 const struct uvc_format *format = NULL; 974 const struct uvc_frame *frame = NULL; 975 unsigned int nintervals; 976 unsigned int index; 977 unsigned int i; 978 979 /* Look for the given pixel format and frame size */ 980 for (i = 0; i < stream->nformats; i++) { 981 if (stream->formats[i].fcc == fival->pixel_format) { 982 format = &stream->formats[i]; 983 break; 984 } 985 } 986 if (format == NULL) 987 return -EINVAL; 988 989 index = fival->index; 990 for (i = 0; i < format->nframes; i++) { 991 if (format->frames[i].wWidth == fival->width && 992 format->frames[i].wHeight == fival->height) { 993 frame = &format->frames[i]; 994 nintervals = frame->bFrameIntervalType ?: 1; 995 if (index < nintervals) 996 break; 997 index -= nintervals; 998 } 999 } 1000 if (i == format->nframes) 1001 return -EINVAL; 1002 1003 if (frame->bFrameIntervalType) { 1004 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; 1005 fival->discrete.numerator = 1006 frame->dwFrameInterval[index]; 1007 fival->discrete.denominator = 10000000; 1008 v4l2_simplify_fraction(&fival->discrete.numerator, 1009 &fival->discrete.denominator, 8, 333); 1010 } else { 1011 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE; 1012 fival->stepwise.min.numerator = frame->dwFrameInterval[0]; 1013 fival->stepwise.min.denominator = 10000000; 1014 fival->stepwise.max.numerator = frame->dwFrameInterval[1]; 1015 fival->stepwise.max.denominator = 10000000; 1016 fival->stepwise.step.numerator = frame->dwFrameInterval[2]; 1017 fival->stepwise.step.denominator = 10000000; 1018 v4l2_simplify_fraction(&fival->stepwise.min.numerator, 1019 &fival->stepwise.min.denominator, 8, 333); 1020 v4l2_simplify_fraction(&fival->stepwise.max.numerator, 1021 &fival->stepwise.max.denominator, 8, 333); 1022 v4l2_simplify_fraction(&fival->stepwise.step.numerator, 1023 &fival->stepwise.step.denominator, 8, 333); 1024 } 1025 1026 return 0; 1027 } 1028 1029 static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh, 1030 const struct v4l2_event_subscription *sub) 1031 { 1032 switch (sub->type) { 1033 case V4L2_EVENT_CTRL: 1034 return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops); 1035 default: 1036 return -EINVAL; 1037 } 1038 } 1039 1040 static long uvc_ioctl_default(struct file *file, void *priv, bool valid_prio, 1041 unsigned int cmd, void *arg) 1042 { 1043 struct uvc_fh *handle = to_uvc_fh(file); 1044 struct uvc_video_chain *chain = handle->chain; 1045 1046 switch (cmd) { 1047 /* Dynamic controls. */ 1048 case UVCIOC_CTRL_MAP: 1049 return uvc_ioctl_xu_ctrl_map(chain, arg); 1050 1051 case UVCIOC_CTRL_QUERY: 1052 return uvc_xu_ctrl_query(chain, arg); 1053 1054 default: 1055 return -ENOTTY; 1056 } 1057 } 1058 1059 #ifdef CONFIG_COMPAT 1060 struct uvc_xu_control_mapping32 { 1061 u32 id; 1062 u8 name[32]; 1063 u8 entity[16]; 1064 u8 selector; 1065 1066 u8 size; 1067 u8 offset; 1068 u32 v4l2_type; 1069 u32 data_type; 1070 1071 compat_caddr_t menu_info; 1072 u32 menu_count; 1073 1074 u32 reserved[4]; 1075 }; 1076 1077 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp, 1078 const struct uvc_xu_control_mapping32 __user *up) 1079 { 1080 struct uvc_xu_control_mapping32 *p = (void *)kp; 1081 compat_caddr_t info; 1082 u32 count; 1083 1084 if (copy_from_user(p, up, sizeof(*p))) 1085 return -EFAULT; 1086 1087 count = p->menu_count; 1088 info = p->menu_info; 1089 1090 memset(kp->reserved, 0, sizeof(kp->reserved)); 1091 kp->menu_info = count ? compat_ptr(info) : NULL; 1092 kp->menu_count = count; 1093 return 0; 1094 } 1095 1096 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp, 1097 struct uvc_xu_control_mapping32 __user *up) 1098 { 1099 if (copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) || 1100 put_user(kp->menu_count, &up->menu_count)) 1101 return -EFAULT; 1102 1103 if (clear_user(up->reserved, sizeof(up->reserved))) 1104 return -EFAULT; 1105 1106 return 0; 1107 } 1108 1109 struct uvc_xu_control_query32 { 1110 u8 unit; 1111 u8 selector; 1112 u8 query; 1113 u16 size; 1114 compat_caddr_t data; 1115 }; 1116 1117 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp, 1118 const struct uvc_xu_control_query32 __user *up) 1119 { 1120 struct uvc_xu_control_query32 v; 1121 1122 if (copy_from_user(&v, up, sizeof(v))) 1123 return -EFAULT; 1124 1125 *kp = (struct uvc_xu_control_query){ 1126 .unit = v.unit, 1127 .selector = v.selector, 1128 .query = v.query, 1129 .size = v.size, 1130 .data = v.size ? compat_ptr(v.data) : NULL 1131 }; 1132 return 0; 1133 } 1134 1135 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp, 1136 struct uvc_xu_control_query32 __user *up) 1137 { 1138 if (copy_to_user(up, kp, offsetof(typeof(*up), data))) 1139 return -EFAULT; 1140 return 0; 1141 } 1142 1143 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32) 1144 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32) 1145 1146 static long uvc_v4l2_compat_ioctl32(struct file *file, 1147 unsigned int cmd, unsigned long arg) 1148 { 1149 struct uvc_fh *handle = to_uvc_fh(file); 1150 union { 1151 struct uvc_xu_control_mapping xmap; 1152 struct uvc_xu_control_query xqry; 1153 } karg; 1154 void __user *up = compat_ptr(arg); 1155 long ret; 1156 1157 ret = uvc_pm_get(handle->stream->dev); 1158 if (ret) 1159 return ret; 1160 1161 switch (cmd) { 1162 case UVCIOC_CTRL_MAP32: 1163 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up); 1164 if (ret) 1165 break; 1166 ret = uvc_ioctl_xu_ctrl_map(handle->chain, &karg.xmap); 1167 if (ret) 1168 break; 1169 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up); 1170 if (ret) 1171 break; 1172 break; 1173 1174 case UVCIOC_CTRL_QUERY32: 1175 ret = uvc_v4l2_get_xu_query(&karg.xqry, up); 1176 if (ret) 1177 break; 1178 ret = uvc_xu_ctrl_query(handle->chain, &karg.xqry); 1179 if (ret) 1180 break; 1181 ret = uvc_v4l2_put_xu_query(&karg.xqry, up); 1182 if (ret) 1183 break; 1184 break; 1185 1186 default: 1187 ret = -ENOIOCTLCMD; 1188 break; 1189 } 1190 1191 uvc_pm_put(handle->stream->dev); 1192 1193 return ret; 1194 } 1195 #endif 1196 1197 static long uvc_v4l2_unlocked_ioctl(struct file *file, 1198 unsigned int cmd, unsigned long arg) 1199 { 1200 struct uvc_fh *handle = to_uvc_fh(file); 1201 unsigned int converted_cmd = v4l2_translate_cmd(cmd); 1202 int ret; 1203 1204 /* The following IOCTLs need to turn on the camera. */ 1205 switch (converted_cmd) { 1206 case UVCIOC_CTRL_MAP: 1207 case UVCIOC_CTRL_QUERY: 1208 case VIDIOC_G_CTRL: 1209 case VIDIOC_G_EXT_CTRLS: 1210 case VIDIOC_G_INPUT: 1211 case VIDIOC_QUERYCTRL: 1212 case VIDIOC_QUERYMENU: 1213 case VIDIOC_QUERY_EXT_CTRL: 1214 case VIDIOC_S_CTRL: 1215 case VIDIOC_S_EXT_CTRLS: 1216 case VIDIOC_S_FMT: 1217 case VIDIOC_S_INPUT: 1218 case VIDIOC_S_PARM: 1219 case VIDIOC_TRY_EXT_CTRLS: 1220 case VIDIOC_TRY_FMT: 1221 ret = uvc_pm_get(handle->stream->dev); 1222 if (ret) 1223 return ret; 1224 ret = video_ioctl2(file, cmd, arg); 1225 uvc_pm_put(handle->stream->dev); 1226 return ret; 1227 } 1228 1229 /* The other IOCTLs can run with the camera off. */ 1230 return video_ioctl2(file, cmd, arg); 1231 } 1232 1233 const struct v4l2_ioctl_ops uvc_ioctl_ops = { 1234 .vidioc_g_fmt_vid_cap = uvc_ioctl_g_fmt, 1235 .vidioc_g_fmt_vid_out = uvc_ioctl_g_fmt, 1236 .vidioc_s_fmt_vid_cap = uvc_ioctl_s_fmt, 1237 .vidioc_s_fmt_vid_out = uvc_ioctl_s_fmt, 1238 .vidioc_g_parm = uvc_ioctl_g_parm, 1239 .vidioc_s_parm = uvc_ioctl_s_parm, 1240 .vidioc_querycap = uvc_ioctl_querycap, 1241 .vidioc_enum_fmt_vid_cap = uvc_ioctl_enum_fmt, 1242 .vidioc_enum_fmt_vid_out = uvc_ioctl_enum_fmt, 1243 .vidioc_try_fmt_vid_cap = uvc_ioctl_try_fmt, 1244 .vidioc_try_fmt_vid_out = uvc_ioctl_try_fmt, 1245 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1246 .vidioc_querybuf = vb2_ioctl_querybuf, 1247 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 1248 .vidioc_qbuf = vb2_ioctl_qbuf, 1249 .vidioc_expbuf = vb2_ioctl_expbuf, 1250 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1251 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1252 .vidioc_streamon = vb2_ioctl_streamon, 1253 .vidioc_streamoff = vb2_ioctl_streamoff, 1254 .vidioc_enum_input = uvc_ioctl_enum_input, 1255 .vidioc_g_input = uvc_ioctl_g_input, 1256 .vidioc_s_input = uvc_ioctl_s_input, 1257 .vidioc_query_ext_ctrl = uvc_ioctl_query_ext_ctrl, 1258 .vidioc_g_ext_ctrls = uvc_ioctl_g_ext_ctrls, 1259 .vidioc_s_ext_ctrls = uvc_ioctl_s_ext_ctrls, 1260 .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls, 1261 .vidioc_querymenu = uvc_ioctl_querymenu, 1262 .vidioc_g_selection = uvc_ioctl_g_selection, 1263 .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes, 1264 .vidioc_enum_frameintervals = uvc_ioctl_enum_frameintervals, 1265 .vidioc_subscribe_event = uvc_ioctl_subscribe_event, 1266 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1267 .vidioc_default = uvc_ioctl_default, 1268 }; 1269 1270 const struct v4l2_file_operations uvc_fops = { 1271 .owner = THIS_MODULE, 1272 .open = uvc_v4l2_open, 1273 .release = uvc_v4l2_release, 1274 .unlocked_ioctl = uvc_v4l2_unlocked_ioctl, 1275 #ifdef CONFIG_COMPAT 1276 .compat_ioctl32 = uvc_v4l2_compat_ioctl32, 1277 #endif 1278 .mmap = vb2_fop_mmap, 1279 .poll = vb2_fop_poll, 1280 #ifndef CONFIG_MMU 1281 .get_unmapped_area = vb2_fop_get_unmapped_area, 1282 #endif 1283 }; 1284 1285