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