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