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