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