1 /* 2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API 3 * 4 * Copyright (C) 2005-2010 5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com) 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 */ 13 14 #include <linux/compat.h> 15 #include <linux/kernel.h> 16 #include <linux/version.h> 17 #include <linux/list.h> 18 #include <linux/module.h> 19 #include <linux/slab.h> 20 #include <linux/usb.h> 21 #include <linux/videodev2.h> 22 #include <linux/vmalloc.h> 23 #include <linux/mm.h> 24 #include <linux/wait.h> 25 #include <linux/atomic.h> 26 27 #include <media/v4l2-common.h> 28 #include <media/v4l2-ctrls.h> 29 #include <media/v4l2-event.h> 30 #include <media/v4l2-ioctl.h> 31 32 #include "uvcvideo.h" 33 34 /* ------------------------------------------------------------------------ 35 * UVC ioctls 36 */ 37 static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain, 38 struct uvc_xu_control_mapping *xmap) 39 { 40 struct uvc_control_mapping *map; 41 unsigned int size; 42 int ret; 43 44 map = kzalloc(sizeof *map, GFP_KERNEL); 45 if (map == NULL) 46 return -ENOMEM; 47 48 map->id = xmap->id; 49 memcpy(map->name, xmap->name, sizeof map->name); 50 memcpy(map->entity, xmap->entity, sizeof map->entity); 51 map->selector = xmap->selector; 52 map->size = xmap->size; 53 map->offset = xmap->offset; 54 map->v4l2_type = xmap->v4l2_type; 55 map->data_type = xmap->data_type; 56 57 switch (xmap->v4l2_type) { 58 case V4L2_CTRL_TYPE_INTEGER: 59 case V4L2_CTRL_TYPE_BOOLEAN: 60 case V4L2_CTRL_TYPE_BUTTON: 61 break; 62 63 case V4L2_CTRL_TYPE_MENU: 64 /* Prevent excessive memory consumption, as well as integer 65 * overflows. 66 */ 67 if (xmap->menu_count == 0 || 68 xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) { 69 ret = -EINVAL; 70 goto done; 71 } 72 73 size = xmap->menu_count * sizeof(*map->menu_info); 74 map->menu_info = kmalloc(size, GFP_KERNEL); 75 if (map->menu_info == NULL) { 76 ret = -ENOMEM; 77 goto done; 78 } 79 80 if (copy_from_user(map->menu_info, xmap->menu_info, size)) { 81 ret = -EFAULT; 82 goto done; 83 } 84 85 map->menu_count = xmap->menu_count; 86 break; 87 88 default: 89 uvc_trace(UVC_TRACE_CONTROL, "Unsupported V4L2 control type " 90 "%u.\n", xmap->v4l2_type); 91 ret = -ENOTTY; 92 goto done; 93 } 94 95 ret = uvc_ctrl_add_mapping(chain, map); 96 97 done: 98 kfree(map->menu_info); 99 kfree(map); 100 101 return ret; 102 } 103 104 /* ------------------------------------------------------------------------ 105 * V4L2 interface 106 */ 107 108 /* 109 * Find the frame interval closest to the requested frame interval for the 110 * given frame format and size. This should be done by the device as part of 111 * the Video Probe and Commit negotiation, but some hardware don't implement 112 * that feature. 113 */ 114 static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval) 115 { 116 unsigned int i; 117 118 if (frame->bFrameIntervalType) { 119 __u32 best = -1, dist; 120 121 for (i = 0; i < frame->bFrameIntervalType; ++i) { 122 dist = interval > frame->dwFrameInterval[i] 123 ? interval - frame->dwFrameInterval[i] 124 : frame->dwFrameInterval[i] - interval; 125 126 if (dist > best) 127 break; 128 129 best = dist; 130 } 131 132 interval = frame->dwFrameInterval[i-1]; 133 } else { 134 const __u32 min = frame->dwFrameInterval[0]; 135 const __u32 max = frame->dwFrameInterval[1]; 136 const __u32 step = frame->dwFrameInterval[2]; 137 138 interval = min + (interval - min + step/2) / step * step; 139 if (interval > max) 140 interval = max; 141 } 142 143 return interval; 144 } 145 146 static int uvc_v4l2_try_format(struct uvc_streaming *stream, 147 struct v4l2_format *fmt, struct uvc_streaming_control *probe, 148 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame) 149 { 150 struct uvc_format *format = NULL; 151 struct uvc_frame *frame = NULL; 152 __u16 rw, rh; 153 unsigned int d, maxd; 154 unsigned int i; 155 __u32 interval; 156 int ret = 0; 157 __u8 *fcc; 158 159 if (fmt->type != stream->type) 160 return -EINVAL; 161 162 fcc = (__u8 *)&fmt->fmt.pix.pixelformat; 163 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n", 164 fmt->fmt.pix.pixelformat, 165 fcc[0], fcc[1], fcc[2], fcc[3], 166 fmt->fmt.pix.width, fmt->fmt.pix.height); 167 168 /* Check if the hardware supports the requested format, use the default 169 * format otherwise. 170 */ 171 for (i = 0; i < stream->nformats; ++i) { 172 format = &stream->format[i]; 173 if (format->fcc == fmt->fmt.pix.pixelformat) 174 break; 175 } 176 177 if (i == stream->nformats) { 178 format = stream->def_format; 179 fmt->fmt.pix.pixelformat = format->fcc; 180 } 181 182 /* Find the closest image size. The distance between image sizes is 183 * the size in pixels of the non-overlapping regions between the 184 * requested size and the frame-specified size. 185 */ 186 rw = fmt->fmt.pix.width; 187 rh = fmt->fmt.pix.height; 188 maxd = (unsigned int)-1; 189 190 for (i = 0; i < format->nframes; ++i) { 191 __u16 w = format->frame[i].wWidth; 192 __u16 h = format->frame[i].wHeight; 193 194 d = min(w, rw) * min(h, rh); 195 d = w*h + rw*rh - 2*d; 196 if (d < maxd) { 197 maxd = d; 198 frame = &format->frame[i]; 199 } 200 201 if (maxd == 0) 202 break; 203 } 204 205 if (frame == NULL) { 206 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n", 207 fmt->fmt.pix.width, fmt->fmt.pix.height); 208 return -EINVAL; 209 } 210 211 /* Use the default frame interval. */ 212 interval = frame->dwDefaultFrameInterval; 213 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us " 214 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval, 215 (100000000/interval)%10); 216 217 /* Set the format index, frame index and frame interval. */ 218 memset(probe, 0, sizeof *probe); 219 probe->bmHint = 1; /* dwFrameInterval */ 220 probe->bFormatIndex = format->index; 221 probe->bFrameIndex = frame->bFrameIndex; 222 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval); 223 /* Some webcams stall the probe control set request when the 224 * dwMaxVideoFrameSize field is set to zero. The UVC specification 225 * clearly states that the field is read-only from the host, so this 226 * is a webcam bug. Set dwMaxVideoFrameSize to the value reported by 227 * the webcam to work around the problem. 228 * 229 * The workaround could probably be enabled for all webcams, so the 230 * quirk can be removed if needed. It's currently useful to detect 231 * webcam bugs and fix them before they hit the market (providing 232 * developers test their webcams with the Linux driver as well as with 233 * the Windows driver). 234 */ 235 mutex_lock(&stream->mutex); 236 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS) 237 probe->dwMaxVideoFrameSize = 238 stream->ctrl.dwMaxVideoFrameSize; 239 240 /* Probe the device. */ 241 ret = uvc_probe_video(stream, probe); 242 mutex_unlock(&stream->mutex); 243 if (ret < 0) 244 goto done; 245 246 fmt->fmt.pix.width = frame->wWidth; 247 fmt->fmt.pix.height = frame->wHeight; 248 fmt->fmt.pix.field = V4L2_FIELD_NONE; 249 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8; 250 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize; 251 fmt->fmt.pix.colorspace = format->colorspace; 252 fmt->fmt.pix.priv = 0; 253 254 if (uvc_format != NULL) 255 *uvc_format = format; 256 if (uvc_frame != NULL) 257 *uvc_frame = frame; 258 259 done: 260 return ret; 261 } 262 263 static int uvc_v4l2_get_format(struct uvc_streaming *stream, 264 struct v4l2_format *fmt) 265 { 266 struct uvc_format *format; 267 struct uvc_frame *frame; 268 int ret = 0; 269 270 if (fmt->type != stream->type) 271 return -EINVAL; 272 273 mutex_lock(&stream->mutex); 274 format = stream->cur_format; 275 frame = stream->cur_frame; 276 277 if (format == NULL || frame == NULL) { 278 ret = -EINVAL; 279 goto done; 280 } 281 282 fmt->fmt.pix.pixelformat = format->fcc; 283 fmt->fmt.pix.width = frame->wWidth; 284 fmt->fmt.pix.height = frame->wHeight; 285 fmt->fmt.pix.field = V4L2_FIELD_NONE; 286 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8; 287 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize; 288 fmt->fmt.pix.colorspace = format->colorspace; 289 fmt->fmt.pix.priv = 0; 290 291 done: 292 mutex_unlock(&stream->mutex); 293 return ret; 294 } 295 296 static int uvc_v4l2_set_format(struct uvc_streaming *stream, 297 struct v4l2_format *fmt) 298 { 299 struct uvc_streaming_control probe; 300 struct uvc_format *format; 301 struct uvc_frame *frame; 302 int ret; 303 304 if (fmt->type != stream->type) 305 return -EINVAL; 306 307 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame); 308 if (ret < 0) 309 return ret; 310 311 mutex_lock(&stream->mutex); 312 313 if (uvc_queue_allocated(&stream->queue)) { 314 ret = -EBUSY; 315 goto done; 316 } 317 318 stream->ctrl = probe; 319 stream->cur_format = format; 320 stream->cur_frame = frame; 321 stream->frame_size = fmt->fmt.pix.sizeimage; 322 323 done: 324 mutex_unlock(&stream->mutex); 325 return ret; 326 } 327 328 static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream, 329 struct v4l2_streamparm *parm) 330 { 331 uint32_t numerator, denominator; 332 333 if (parm->type != stream->type) 334 return -EINVAL; 335 336 mutex_lock(&stream->mutex); 337 numerator = stream->ctrl.dwFrameInterval; 338 mutex_unlock(&stream->mutex); 339 340 denominator = 10000000; 341 uvc_simplify_fraction(&numerator, &denominator, 8, 333); 342 343 memset(parm, 0, sizeof *parm); 344 parm->type = stream->type; 345 346 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { 347 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 348 parm->parm.capture.capturemode = 0; 349 parm->parm.capture.timeperframe.numerator = numerator; 350 parm->parm.capture.timeperframe.denominator = denominator; 351 parm->parm.capture.extendedmode = 0; 352 parm->parm.capture.readbuffers = 0; 353 } else { 354 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME; 355 parm->parm.output.outputmode = 0; 356 parm->parm.output.timeperframe.numerator = numerator; 357 parm->parm.output.timeperframe.denominator = denominator; 358 } 359 360 return 0; 361 } 362 363 static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream, 364 struct v4l2_streamparm *parm) 365 { 366 struct uvc_streaming_control probe; 367 struct v4l2_fract timeperframe; 368 uint32_t interval; 369 int ret; 370 371 if (parm->type != stream->type) 372 return -EINVAL; 373 374 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 375 timeperframe = parm->parm.capture.timeperframe; 376 else 377 timeperframe = parm->parm.output.timeperframe; 378 379 interval = uvc_fraction_to_interval(timeperframe.numerator, 380 timeperframe.denominator); 381 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n", 382 timeperframe.numerator, timeperframe.denominator, interval); 383 384 mutex_lock(&stream->mutex); 385 386 if (uvc_queue_streaming(&stream->queue)) { 387 mutex_unlock(&stream->mutex); 388 return -EBUSY; 389 } 390 391 probe = stream->ctrl; 392 probe.dwFrameInterval = 393 uvc_try_frame_interval(stream->cur_frame, interval); 394 395 /* Probe the device with the new settings. */ 396 ret = uvc_probe_video(stream, &probe); 397 if (ret < 0) { 398 mutex_unlock(&stream->mutex); 399 return ret; 400 } 401 402 stream->ctrl = probe; 403 mutex_unlock(&stream->mutex); 404 405 /* Return the actual frame period. */ 406 timeperframe.numerator = probe.dwFrameInterval; 407 timeperframe.denominator = 10000000; 408 uvc_simplify_fraction(&timeperframe.numerator, 409 &timeperframe.denominator, 8, 333); 410 411 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 412 parm->parm.capture.timeperframe = timeperframe; 413 else 414 parm->parm.output.timeperframe = timeperframe; 415 416 return 0; 417 } 418 419 /* ------------------------------------------------------------------------ 420 * Privilege management 421 */ 422 423 /* 424 * Privilege management is the multiple-open implementation basis. The current 425 * implementation is completely transparent for the end-user and doesn't 426 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls. 427 * Those ioctls enable finer control on the device (by making possible for a 428 * user to request exclusive access to a device), but are not mature yet. 429 * Switching to the V4L2 priority mechanism might be considered in the future 430 * if this situation changes. 431 * 432 * Each open instance of a UVC device can either be in a privileged or 433 * unprivileged state. Only a single instance can be in a privileged state at 434 * a given time. Trying to perform an operation that requires privileges will 435 * automatically acquire the required privileges if possible, or return -EBUSY 436 * otherwise. Privileges are dismissed when closing the instance or when 437 * freeing the video buffers using VIDIOC_REQBUFS. 438 * 439 * Operations that require privileges are: 440 * 441 * - VIDIOC_S_INPUT 442 * - VIDIOC_S_PARM 443 * - VIDIOC_S_FMT 444 * - VIDIOC_REQBUFS 445 */ 446 static int uvc_acquire_privileges(struct uvc_fh *handle) 447 { 448 /* Always succeed if the handle is already privileged. */ 449 if (handle->state == UVC_HANDLE_ACTIVE) 450 return 0; 451 452 /* Check if the device already has a privileged handle. */ 453 if (atomic_inc_return(&handle->stream->active) != 1) { 454 atomic_dec(&handle->stream->active); 455 return -EBUSY; 456 } 457 458 handle->state = UVC_HANDLE_ACTIVE; 459 return 0; 460 } 461 462 static void uvc_dismiss_privileges(struct uvc_fh *handle) 463 { 464 if (handle->state == UVC_HANDLE_ACTIVE) 465 atomic_dec(&handle->stream->active); 466 467 handle->state = UVC_HANDLE_PASSIVE; 468 } 469 470 static int uvc_has_privileges(struct uvc_fh *handle) 471 { 472 return handle->state == UVC_HANDLE_ACTIVE; 473 } 474 475 /* ------------------------------------------------------------------------ 476 * V4L2 file operations 477 */ 478 479 static int uvc_v4l2_open(struct file *file) 480 { 481 struct uvc_streaming *stream; 482 struct uvc_fh *handle; 483 int ret = 0; 484 485 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n"); 486 stream = video_drvdata(file); 487 488 if (stream->dev->state & UVC_DEV_DISCONNECTED) 489 return -ENODEV; 490 491 ret = usb_autopm_get_interface(stream->dev->intf); 492 if (ret < 0) 493 return ret; 494 495 /* Create the device handle. */ 496 handle = kzalloc(sizeof *handle, GFP_KERNEL); 497 if (handle == NULL) { 498 usb_autopm_put_interface(stream->dev->intf); 499 return -ENOMEM; 500 } 501 502 mutex_lock(&stream->dev->lock); 503 if (stream->dev->users == 0) { 504 ret = uvc_status_start(stream->dev, GFP_KERNEL); 505 if (ret < 0) { 506 mutex_unlock(&stream->dev->lock); 507 usb_autopm_put_interface(stream->dev->intf); 508 kfree(handle); 509 return ret; 510 } 511 } 512 513 stream->dev->users++; 514 mutex_unlock(&stream->dev->lock); 515 516 v4l2_fh_init(&handle->vfh, stream->vdev); 517 v4l2_fh_add(&handle->vfh); 518 handle->chain = stream->chain; 519 handle->stream = stream; 520 handle->state = UVC_HANDLE_PASSIVE; 521 file->private_data = handle; 522 523 return 0; 524 } 525 526 static int uvc_v4l2_release(struct file *file) 527 { 528 struct uvc_fh *handle = file->private_data; 529 struct uvc_streaming *stream = handle->stream; 530 531 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n"); 532 533 /* Only free resources if this is a privileged handle. */ 534 if (uvc_has_privileges(handle)) { 535 uvc_video_enable(stream, 0); 536 uvc_free_buffers(&stream->queue); 537 } 538 539 /* Release the file handle. */ 540 uvc_dismiss_privileges(handle); 541 v4l2_fh_del(&handle->vfh); 542 v4l2_fh_exit(&handle->vfh); 543 kfree(handle); 544 file->private_data = NULL; 545 546 mutex_lock(&stream->dev->lock); 547 if (--stream->dev->users == 0) 548 uvc_status_stop(stream->dev); 549 mutex_unlock(&stream->dev->lock); 550 551 usb_autopm_put_interface(stream->dev->intf); 552 return 0; 553 } 554 555 static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg) 556 { 557 struct video_device *vdev = video_devdata(file); 558 struct uvc_fh *handle = file->private_data; 559 struct uvc_video_chain *chain = handle->chain; 560 struct uvc_streaming *stream = handle->stream; 561 long ret = 0; 562 563 switch (cmd) { 564 /* Query capabilities */ 565 case VIDIOC_QUERYCAP: 566 { 567 struct v4l2_capability *cap = arg; 568 569 memset(cap, 0, sizeof *cap); 570 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver); 571 strlcpy(cap->card, vdev->name, sizeof cap->card); 572 usb_make_path(stream->dev->udev, 573 cap->bus_info, sizeof(cap->bus_info)); 574 cap->version = LINUX_VERSION_CODE; 575 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING 576 | chain->caps; 577 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 578 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE 579 | V4L2_CAP_STREAMING; 580 else 581 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT 582 | V4L2_CAP_STREAMING; 583 break; 584 } 585 586 /* Priority */ 587 case VIDIOC_G_PRIORITY: 588 *(u32 *)arg = v4l2_prio_max(vdev->prio); 589 break; 590 591 case VIDIOC_S_PRIORITY: 592 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio); 593 if (ret < 0) 594 return ret; 595 596 return v4l2_prio_change(vdev->prio, &handle->vfh.prio, 597 *(u32 *)arg); 598 599 /* Get, Set & Query control */ 600 case VIDIOC_QUERYCTRL: 601 return uvc_query_v4l2_ctrl(chain, arg); 602 603 case VIDIOC_G_CTRL: 604 { 605 struct v4l2_control *ctrl = arg; 606 struct v4l2_ext_control xctrl; 607 608 memset(&xctrl, 0, sizeof xctrl); 609 xctrl.id = ctrl->id; 610 611 ret = uvc_ctrl_begin(chain); 612 if (ret < 0) 613 return ret; 614 615 ret = uvc_ctrl_get(chain, &xctrl); 616 uvc_ctrl_rollback(handle); 617 if (ret >= 0) 618 ctrl->value = xctrl.value; 619 break; 620 } 621 622 case VIDIOC_S_CTRL: 623 { 624 struct v4l2_control *ctrl = arg; 625 struct v4l2_ext_control xctrl; 626 627 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio); 628 if (ret < 0) 629 return ret; 630 631 memset(&xctrl, 0, sizeof xctrl); 632 xctrl.id = ctrl->id; 633 xctrl.value = ctrl->value; 634 635 ret = uvc_ctrl_begin(chain); 636 if (ret < 0) 637 return ret; 638 639 ret = uvc_ctrl_set(chain, &xctrl); 640 if (ret < 0) { 641 uvc_ctrl_rollback(handle); 642 return ret; 643 } 644 ret = uvc_ctrl_commit(handle, &xctrl, 1); 645 if (ret == 0) 646 ctrl->value = xctrl.value; 647 break; 648 } 649 650 case VIDIOC_QUERYMENU: 651 return uvc_query_v4l2_menu(chain, arg); 652 653 case VIDIOC_G_EXT_CTRLS: 654 { 655 struct v4l2_ext_controls *ctrls = arg; 656 struct v4l2_ext_control *ctrl = ctrls->controls; 657 unsigned int i; 658 659 ret = uvc_ctrl_begin(chain); 660 if (ret < 0) 661 return ret; 662 663 for (i = 0; i < ctrls->count; ++ctrl, ++i) { 664 ret = uvc_ctrl_get(chain, ctrl); 665 if (ret < 0) { 666 uvc_ctrl_rollback(handle); 667 ctrls->error_idx = i; 668 return ret; 669 } 670 } 671 ctrls->error_idx = 0; 672 ret = uvc_ctrl_rollback(handle); 673 break; 674 } 675 676 case VIDIOC_S_EXT_CTRLS: 677 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio); 678 if (ret < 0) 679 return ret; 680 /* Fall through */ 681 case VIDIOC_TRY_EXT_CTRLS: 682 { 683 struct v4l2_ext_controls *ctrls = arg; 684 struct v4l2_ext_control *ctrl = ctrls->controls; 685 unsigned int i; 686 687 ret = uvc_ctrl_begin(chain); 688 if (ret < 0) 689 return ret; 690 691 for (i = 0; i < ctrls->count; ++ctrl, ++i) { 692 ret = uvc_ctrl_set(chain, ctrl); 693 if (ret < 0) { 694 uvc_ctrl_rollback(handle); 695 ctrls->error_idx = cmd == VIDIOC_S_EXT_CTRLS 696 ? ctrls->count : i; 697 return ret; 698 } 699 } 700 701 ctrls->error_idx = 0; 702 703 if (cmd == VIDIOC_S_EXT_CTRLS) 704 ret = uvc_ctrl_commit(handle, 705 ctrls->controls, ctrls->count); 706 else 707 ret = uvc_ctrl_rollback(handle); 708 break; 709 } 710 711 /* Get, Set & Enum input */ 712 case VIDIOC_ENUMINPUT: 713 { 714 const struct uvc_entity *selector = chain->selector; 715 struct v4l2_input *input = arg; 716 struct uvc_entity *iterm = NULL; 717 u32 index = input->index; 718 int pin = 0; 719 720 if (selector == NULL || 721 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { 722 if (index != 0) 723 return -EINVAL; 724 list_for_each_entry(iterm, &chain->entities, chain) { 725 if (UVC_ENTITY_IS_ITERM(iterm)) 726 break; 727 } 728 pin = iterm->id; 729 } else if (index < selector->bNrInPins) { 730 pin = selector->baSourceID[index]; 731 list_for_each_entry(iterm, &chain->entities, chain) { 732 if (!UVC_ENTITY_IS_ITERM(iterm)) 733 continue; 734 if (iterm->id == pin) 735 break; 736 } 737 } 738 739 if (iterm == NULL || iterm->id != pin) 740 return -EINVAL; 741 742 memset(input, 0, sizeof *input); 743 input->index = index; 744 strlcpy(input->name, iterm->name, sizeof input->name); 745 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA) 746 input->type = V4L2_INPUT_TYPE_CAMERA; 747 break; 748 } 749 750 case VIDIOC_G_INPUT: 751 { 752 u8 input; 753 754 if (chain->selector == NULL || 755 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { 756 *(int *)arg = 0; 757 break; 758 } 759 760 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, 761 chain->selector->id, chain->dev->intfnum, 762 UVC_SU_INPUT_SELECT_CONTROL, &input, 1); 763 if (ret < 0) 764 return ret; 765 766 *(int *)arg = input - 1; 767 break; 768 } 769 770 case VIDIOC_S_INPUT: 771 { 772 u32 input = *(u32 *)arg + 1; 773 774 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio); 775 if (ret < 0) 776 return ret; 777 778 if ((ret = uvc_acquire_privileges(handle)) < 0) 779 return ret; 780 781 if (chain->selector == NULL || 782 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { 783 if (input != 1) 784 return -EINVAL; 785 break; 786 } 787 788 if (input == 0 || input > chain->selector->bNrInPins) 789 return -EINVAL; 790 791 return uvc_query_ctrl(chain->dev, UVC_SET_CUR, 792 chain->selector->id, chain->dev->intfnum, 793 UVC_SU_INPUT_SELECT_CONTROL, &input, 1); 794 } 795 796 /* Try, Get, Set & Enum format */ 797 case VIDIOC_ENUM_FMT: 798 { 799 struct v4l2_fmtdesc *fmt = arg; 800 struct uvc_format *format; 801 enum v4l2_buf_type type = fmt->type; 802 __u32 index = fmt->index; 803 804 if (fmt->type != stream->type || 805 fmt->index >= stream->nformats) 806 return -EINVAL; 807 808 memset(fmt, 0, sizeof(*fmt)); 809 fmt->index = index; 810 fmt->type = type; 811 812 format = &stream->format[fmt->index]; 813 fmt->flags = 0; 814 if (format->flags & UVC_FMT_FLAG_COMPRESSED) 815 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED; 816 strlcpy(fmt->description, format->name, 817 sizeof fmt->description); 818 fmt->description[sizeof fmt->description - 1] = 0; 819 fmt->pixelformat = format->fcc; 820 break; 821 } 822 823 case VIDIOC_TRY_FMT: 824 { 825 struct uvc_streaming_control probe; 826 827 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL); 828 } 829 830 case VIDIOC_S_FMT: 831 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio); 832 if (ret < 0) 833 return ret; 834 835 if ((ret = uvc_acquire_privileges(handle)) < 0) 836 return ret; 837 838 return uvc_v4l2_set_format(stream, arg); 839 840 case VIDIOC_G_FMT: 841 return uvc_v4l2_get_format(stream, arg); 842 843 /* Frame size enumeration */ 844 case VIDIOC_ENUM_FRAMESIZES: 845 { 846 struct v4l2_frmsizeenum *fsize = arg; 847 struct uvc_format *format = NULL; 848 struct uvc_frame *frame; 849 int i; 850 851 /* Look for the given pixel format */ 852 for (i = 0; i < stream->nformats; i++) { 853 if (stream->format[i].fcc == 854 fsize->pixel_format) { 855 format = &stream->format[i]; 856 break; 857 } 858 } 859 if (format == NULL) 860 return -EINVAL; 861 862 if (fsize->index >= format->nframes) 863 return -EINVAL; 864 865 frame = &format->frame[fsize->index]; 866 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 867 fsize->discrete.width = frame->wWidth; 868 fsize->discrete.height = frame->wHeight; 869 break; 870 } 871 872 /* Frame interval enumeration */ 873 case VIDIOC_ENUM_FRAMEINTERVALS: 874 { 875 struct v4l2_frmivalenum *fival = arg; 876 struct uvc_format *format = NULL; 877 struct uvc_frame *frame = NULL; 878 int i; 879 880 /* Look for the given pixel format and frame size */ 881 for (i = 0; i < stream->nformats; i++) { 882 if (stream->format[i].fcc == 883 fival->pixel_format) { 884 format = &stream->format[i]; 885 break; 886 } 887 } 888 if (format == NULL) 889 return -EINVAL; 890 891 for (i = 0; i < format->nframes; i++) { 892 if (format->frame[i].wWidth == fival->width && 893 format->frame[i].wHeight == fival->height) { 894 frame = &format->frame[i]; 895 break; 896 } 897 } 898 if (frame == NULL) 899 return -EINVAL; 900 901 if (frame->bFrameIntervalType) { 902 if (fival->index >= frame->bFrameIntervalType) 903 return -EINVAL; 904 905 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; 906 fival->discrete.numerator = 907 frame->dwFrameInterval[fival->index]; 908 fival->discrete.denominator = 10000000; 909 uvc_simplify_fraction(&fival->discrete.numerator, 910 &fival->discrete.denominator, 8, 333); 911 } else { 912 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE; 913 fival->stepwise.min.numerator = 914 frame->dwFrameInterval[0]; 915 fival->stepwise.min.denominator = 10000000; 916 fival->stepwise.max.numerator = 917 frame->dwFrameInterval[1]; 918 fival->stepwise.max.denominator = 10000000; 919 fival->stepwise.step.numerator = 920 frame->dwFrameInterval[2]; 921 fival->stepwise.step.denominator = 10000000; 922 uvc_simplify_fraction(&fival->stepwise.min.numerator, 923 &fival->stepwise.min.denominator, 8, 333); 924 uvc_simplify_fraction(&fival->stepwise.max.numerator, 925 &fival->stepwise.max.denominator, 8, 333); 926 uvc_simplify_fraction(&fival->stepwise.step.numerator, 927 &fival->stepwise.step.denominator, 8, 333); 928 } 929 break; 930 } 931 932 /* Get & Set streaming parameters */ 933 case VIDIOC_G_PARM: 934 return uvc_v4l2_get_streamparm(stream, arg); 935 936 case VIDIOC_S_PARM: 937 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio); 938 if (ret < 0) 939 return ret; 940 941 if ((ret = uvc_acquire_privileges(handle)) < 0) 942 return ret; 943 944 return uvc_v4l2_set_streamparm(stream, arg); 945 946 /* Cropping and scaling */ 947 case VIDIOC_CROPCAP: 948 { 949 struct v4l2_cropcap *ccap = arg; 950 951 if (ccap->type != stream->type) 952 return -EINVAL; 953 954 ccap->bounds.left = 0; 955 ccap->bounds.top = 0; 956 957 mutex_lock(&stream->mutex); 958 ccap->bounds.width = stream->cur_frame->wWidth; 959 ccap->bounds.height = stream->cur_frame->wHeight; 960 mutex_unlock(&stream->mutex); 961 962 ccap->defrect = ccap->bounds; 963 964 ccap->pixelaspect.numerator = 1; 965 ccap->pixelaspect.denominator = 1; 966 break; 967 } 968 969 case VIDIOC_G_CROP: 970 case VIDIOC_S_CROP: 971 return -ENOTTY; 972 973 /* Buffers & streaming */ 974 case VIDIOC_REQBUFS: 975 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio); 976 if (ret < 0) 977 return ret; 978 979 if ((ret = uvc_acquire_privileges(handle)) < 0) 980 return ret; 981 982 mutex_lock(&stream->mutex); 983 ret = uvc_alloc_buffers(&stream->queue, arg); 984 mutex_unlock(&stream->mutex); 985 if (ret < 0) 986 return ret; 987 988 if (ret == 0) 989 uvc_dismiss_privileges(handle); 990 991 ret = 0; 992 break; 993 994 case VIDIOC_QUERYBUF: 995 { 996 struct v4l2_buffer *buf = arg; 997 998 if (!uvc_has_privileges(handle)) 999 return -EBUSY; 1000 1001 return uvc_query_buffer(&stream->queue, buf); 1002 } 1003 1004 case VIDIOC_CREATE_BUFS: 1005 { 1006 struct v4l2_create_buffers *cb = arg; 1007 1008 ret = uvc_acquire_privileges(handle); 1009 if (ret < 0) 1010 return ret; 1011 1012 return uvc_create_buffers(&stream->queue, cb); 1013 } 1014 1015 case VIDIOC_QBUF: 1016 if (!uvc_has_privileges(handle)) 1017 return -EBUSY; 1018 1019 return uvc_queue_buffer(&stream->queue, arg); 1020 1021 case VIDIOC_DQBUF: 1022 if (!uvc_has_privileges(handle)) 1023 return -EBUSY; 1024 1025 return uvc_dequeue_buffer(&stream->queue, arg, 1026 file->f_flags & O_NONBLOCK); 1027 1028 case VIDIOC_STREAMON: 1029 { 1030 int *type = arg; 1031 1032 if (*type != stream->type) 1033 return -EINVAL; 1034 1035 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio); 1036 if (ret < 0) 1037 return ret; 1038 1039 if (!uvc_has_privileges(handle)) 1040 return -EBUSY; 1041 1042 mutex_lock(&stream->mutex); 1043 ret = uvc_video_enable(stream, 1); 1044 mutex_unlock(&stream->mutex); 1045 if (ret < 0) 1046 return ret; 1047 break; 1048 } 1049 1050 case VIDIOC_STREAMOFF: 1051 { 1052 int *type = arg; 1053 1054 if (*type != stream->type) 1055 return -EINVAL; 1056 1057 ret = v4l2_prio_check(vdev->prio, handle->vfh.prio); 1058 if (ret < 0) 1059 return ret; 1060 1061 if (!uvc_has_privileges(handle)) 1062 return -EBUSY; 1063 1064 return uvc_video_enable(stream, 0); 1065 } 1066 1067 case VIDIOC_SUBSCRIBE_EVENT: 1068 { 1069 struct v4l2_event_subscription *sub = arg; 1070 1071 switch (sub->type) { 1072 case V4L2_EVENT_CTRL: 1073 return v4l2_event_subscribe(&handle->vfh, sub, 0, 1074 &uvc_ctrl_sub_ev_ops); 1075 default: 1076 return -EINVAL; 1077 } 1078 } 1079 1080 case VIDIOC_UNSUBSCRIBE_EVENT: 1081 return v4l2_event_unsubscribe(&handle->vfh, arg); 1082 1083 case VIDIOC_DQEVENT: 1084 return v4l2_event_dequeue(&handle->vfh, arg, 1085 file->f_flags & O_NONBLOCK); 1086 1087 /* Analog video standards make no sense for digital cameras. */ 1088 case VIDIOC_ENUMSTD: 1089 case VIDIOC_QUERYSTD: 1090 case VIDIOC_G_STD: 1091 case VIDIOC_S_STD: 1092 1093 case VIDIOC_OVERLAY: 1094 1095 case VIDIOC_ENUMAUDIO: 1096 case VIDIOC_ENUMAUDOUT: 1097 1098 case VIDIOC_ENUMOUTPUT: 1099 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd); 1100 return -ENOTTY; 1101 1102 case UVCIOC_CTRL_MAP: 1103 return uvc_ioctl_ctrl_map(chain, arg); 1104 1105 case UVCIOC_CTRL_QUERY: 1106 return uvc_xu_ctrl_query(chain, arg); 1107 1108 default: 1109 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", cmd); 1110 return -ENOTTY; 1111 } 1112 1113 return ret; 1114 } 1115 1116 static long uvc_v4l2_ioctl(struct file *file, 1117 unsigned int cmd, unsigned long arg) 1118 { 1119 if (uvc_trace_param & UVC_TRACE_IOCTL) { 1120 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl("); 1121 v4l_printk_ioctl(NULL, cmd); 1122 printk(")\n"); 1123 } 1124 1125 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl); 1126 } 1127 1128 #ifdef CONFIG_COMPAT 1129 struct uvc_xu_control_mapping32 { 1130 __u32 id; 1131 __u8 name[32]; 1132 __u8 entity[16]; 1133 __u8 selector; 1134 1135 __u8 size; 1136 __u8 offset; 1137 __u32 v4l2_type; 1138 __u32 data_type; 1139 1140 compat_caddr_t menu_info; 1141 __u32 menu_count; 1142 1143 __u32 reserved[4]; 1144 }; 1145 1146 static int uvc_v4l2_get_xu_mapping(struct uvc_xu_control_mapping *kp, 1147 const struct uvc_xu_control_mapping32 __user *up) 1148 { 1149 struct uvc_menu_info __user *umenus; 1150 struct uvc_menu_info __user *kmenus; 1151 compat_caddr_t p; 1152 1153 if (!access_ok(VERIFY_READ, up, sizeof(*up)) || 1154 __copy_from_user(kp, up, offsetof(typeof(*up), menu_info)) || 1155 __get_user(kp->menu_count, &up->menu_count)) 1156 return -EFAULT; 1157 1158 memset(kp->reserved, 0, sizeof(kp->reserved)); 1159 1160 if (kp->menu_count == 0) { 1161 kp->menu_info = NULL; 1162 return 0; 1163 } 1164 1165 if (__get_user(p, &up->menu_info)) 1166 return -EFAULT; 1167 umenus = compat_ptr(p); 1168 if (!access_ok(VERIFY_READ, umenus, kp->menu_count * sizeof(*umenus))) 1169 return -EFAULT; 1170 1171 kmenus = compat_alloc_user_space(kp->menu_count * sizeof(*kmenus)); 1172 if (kmenus == NULL) 1173 return -EFAULT; 1174 kp->menu_info = kmenus; 1175 1176 if (copy_in_user(kmenus, umenus, kp->menu_count * sizeof(*umenus))) 1177 return -EFAULT; 1178 1179 return 0; 1180 } 1181 1182 static int uvc_v4l2_put_xu_mapping(const struct uvc_xu_control_mapping *kp, 1183 struct uvc_xu_control_mapping32 __user *up) 1184 { 1185 struct uvc_menu_info __user *umenus; 1186 struct uvc_menu_info __user *kmenus = kp->menu_info; 1187 compat_caddr_t p; 1188 1189 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) || 1190 __copy_to_user(up, kp, offsetof(typeof(*up), menu_info)) || 1191 __put_user(kp->menu_count, &up->menu_count)) 1192 return -EFAULT; 1193 1194 if (__clear_user(up->reserved, sizeof(up->reserved))) 1195 return -EFAULT; 1196 1197 if (kp->menu_count == 0) 1198 return 0; 1199 1200 if (get_user(p, &up->menu_info)) 1201 return -EFAULT; 1202 umenus = compat_ptr(p); 1203 1204 if (copy_in_user(umenus, kmenus, kp->menu_count * sizeof(*umenus))) 1205 return -EFAULT; 1206 1207 return 0; 1208 } 1209 1210 struct uvc_xu_control_query32 { 1211 __u8 unit; 1212 __u8 selector; 1213 __u8 query; 1214 __u16 size; 1215 compat_caddr_t data; 1216 }; 1217 1218 static int uvc_v4l2_get_xu_query(struct uvc_xu_control_query *kp, 1219 const struct uvc_xu_control_query32 __user *up) 1220 { 1221 u8 __user *udata; 1222 u8 __user *kdata; 1223 compat_caddr_t p; 1224 1225 if (!access_ok(VERIFY_READ, up, sizeof(*up)) || 1226 __copy_from_user(kp, up, offsetof(typeof(*up), data))) 1227 return -EFAULT; 1228 1229 if (kp->size == 0) { 1230 kp->data = NULL; 1231 return 0; 1232 } 1233 1234 if (__get_user(p, &up->data)) 1235 return -EFAULT; 1236 udata = compat_ptr(p); 1237 if (!access_ok(VERIFY_READ, udata, kp->size)) 1238 return -EFAULT; 1239 1240 kdata = compat_alloc_user_space(kp->size); 1241 if (kdata == NULL) 1242 return -EFAULT; 1243 kp->data = kdata; 1244 1245 if (copy_in_user(kdata, udata, kp->size)) 1246 return -EFAULT; 1247 1248 return 0; 1249 } 1250 1251 static int uvc_v4l2_put_xu_query(const struct uvc_xu_control_query *kp, 1252 struct uvc_xu_control_query32 __user *up) 1253 { 1254 u8 __user *udata; 1255 u8 __user *kdata = kp->data; 1256 compat_caddr_t p; 1257 1258 if (!access_ok(VERIFY_WRITE, up, sizeof(*up)) || 1259 __copy_to_user(up, kp, offsetof(typeof(*up), data))) 1260 return -EFAULT; 1261 1262 if (kp->size == 0) 1263 return 0; 1264 1265 if (get_user(p, &up->data)) 1266 return -EFAULT; 1267 udata = compat_ptr(p); 1268 if (!access_ok(VERIFY_READ, udata, kp->size)) 1269 return -EFAULT; 1270 1271 if (copy_in_user(udata, kdata, kp->size)) 1272 return -EFAULT; 1273 1274 return 0; 1275 } 1276 1277 #define UVCIOC_CTRL_MAP32 _IOWR('u', 0x20, struct uvc_xu_control_mapping32) 1278 #define UVCIOC_CTRL_QUERY32 _IOWR('u', 0x21, struct uvc_xu_control_query32) 1279 1280 static long uvc_v4l2_compat_ioctl32(struct file *file, 1281 unsigned int cmd, unsigned long arg) 1282 { 1283 union { 1284 struct uvc_xu_control_mapping xmap; 1285 struct uvc_xu_control_query xqry; 1286 } karg; 1287 void __user *up = compat_ptr(arg); 1288 mm_segment_t old_fs; 1289 long ret; 1290 1291 switch (cmd) { 1292 case UVCIOC_CTRL_MAP32: 1293 cmd = UVCIOC_CTRL_MAP; 1294 ret = uvc_v4l2_get_xu_mapping(&karg.xmap, up); 1295 break; 1296 1297 case UVCIOC_CTRL_QUERY32: 1298 cmd = UVCIOC_CTRL_QUERY; 1299 ret = uvc_v4l2_get_xu_query(&karg.xqry, up); 1300 break; 1301 1302 default: 1303 return -ENOIOCTLCMD; 1304 } 1305 1306 old_fs = get_fs(); 1307 set_fs(KERNEL_DS); 1308 ret = uvc_v4l2_ioctl(file, cmd, (unsigned long)&karg); 1309 set_fs(old_fs); 1310 1311 if (ret < 0) 1312 return ret; 1313 1314 switch (cmd) { 1315 case UVCIOC_CTRL_MAP: 1316 ret = uvc_v4l2_put_xu_mapping(&karg.xmap, up); 1317 break; 1318 1319 case UVCIOC_CTRL_QUERY: 1320 ret = uvc_v4l2_put_xu_query(&karg.xqry, up); 1321 break; 1322 } 1323 1324 return ret; 1325 } 1326 #endif 1327 1328 static ssize_t uvc_v4l2_read(struct file *file, char __user *data, 1329 size_t count, loff_t *ppos) 1330 { 1331 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n"); 1332 return -EINVAL; 1333 } 1334 1335 static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma) 1336 { 1337 struct uvc_fh *handle = file->private_data; 1338 struct uvc_streaming *stream = handle->stream; 1339 1340 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n"); 1341 1342 return uvc_queue_mmap(&stream->queue, vma); 1343 } 1344 1345 static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait) 1346 { 1347 struct uvc_fh *handle = file->private_data; 1348 struct uvc_streaming *stream = handle->stream; 1349 1350 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n"); 1351 1352 return uvc_queue_poll(&stream->queue, file, wait); 1353 } 1354 1355 #ifndef CONFIG_MMU 1356 static unsigned long uvc_v4l2_get_unmapped_area(struct file *file, 1357 unsigned long addr, unsigned long len, unsigned long pgoff, 1358 unsigned long flags) 1359 { 1360 struct uvc_fh *handle = file->private_data; 1361 struct uvc_streaming *stream = handle->stream; 1362 1363 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_get_unmapped_area\n"); 1364 1365 return uvc_queue_get_unmapped_area(&stream->queue, pgoff); 1366 } 1367 #endif 1368 1369 const struct v4l2_file_operations uvc_fops = { 1370 .owner = THIS_MODULE, 1371 .open = uvc_v4l2_open, 1372 .release = uvc_v4l2_release, 1373 .unlocked_ioctl = uvc_v4l2_ioctl, 1374 #ifdef CONFIG_COMPAT 1375 .compat_ioctl32 = uvc_v4l2_compat_ioctl32, 1376 #endif 1377 .read = uvc_v4l2_read, 1378 .mmap = uvc_v4l2_mmap, 1379 .poll = uvc_v4l2_poll, 1380 #ifndef CONFIG_MMU 1381 .get_unmapped_area = uvc_v4l2_get_unmapped_area, 1382 #endif 1383 }; 1384 1385