1/* 2 * uvc_v4l2.c -- USB Video Class driver - V4L2 API 3 * 4 * Copyright (C) 2005-2009 5 * Laurent Pinchart (laurent.pinchart@skynet.be) 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/kernel.h> 15#include <linux/version.h> 16#include <linux/list.h> 17#include <linux/module.h> 18#include <linux/slab.h> 19#include <linux/usb.h> 20#include <linux/videodev2.h> 21#include <linux/vmalloc.h> 22#include <linux/mm.h> 23#include <linux/wait.h> 24#include <asm/atomic.h> 25 26#include <media/v4l2-common.h> 27#include <media/v4l2-ioctl.h> 28 29#include "uvcvideo.h" 30 31/* ------------------------------------------------------------------------ 32 * UVC ioctls 33 */ 34static int uvc_ioctl_ctrl_map(struct uvc_xu_control_mapping *xmap, int old) 35{ 36 struct uvc_control_mapping *map; 37 unsigned int size; 38 int ret; 39 40 map = kzalloc(sizeof *map, GFP_KERNEL); 41 if (map == NULL) 42 return -ENOMEM; 43 44 map->id = xmap->id; 45 memcpy(map->name, xmap->name, sizeof map->name); 46 memcpy(map->entity, xmap->entity, sizeof map->entity); 47 map->selector = xmap->selector; 48 map->size = xmap->size; 49 map->offset = xmap->offset; 50 map->v4l2_type = xmap->v4l2_type; 51 map->data_type = xmap->data_type; 52 53 switch (xmap->v4l2_type) { 54 case V4L2_CTRL_TYPE_INTEGER: 55 case V4L2_CTRL_TYPE_BOOLEAN: 56 case V4L2_CTRL_TYPE_BUTTON: 57 break; 58 59 case V4L2_CTRL_TYPE_MENU: 60 if (old) { 61 ret = -EINVAL; 62 goto done; 63 } 64 65 size = xmap->menu_count * sizeof(*map->menu_info); 66 map->menu_info = kmalloc(size, GFP_KERNEL); 67 if (map->menu_info == NULL) { 68 ret = -ENOMEM; 69 goto done; 70 } 71 72 if (copy_from_user(map->menu_info, xmap->menu_info, size)) { 73 ret = -EFAULT; 74 goto done; 75 } 76 77 map->menu_count = xmap->menu_count; 78 break; 79 80 default: 81 ret = -EINVAL; 82 goto done; 83 } 84 85 ret = uvc_ctrl_add_mapping(map); 86 87done: 88 if (ret < 0) { 89 kfree(map->menu_info); 90 kfree(map); 91 } 92 93 return ret; 94} 95 96/* ------------------------------------------------------------------------ 97 * V4L2 interface 98 */ 99 100/* 101 * Mapping V4L2 controls to UVC controls can be straighforward if done well. 102 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some 103 * must be grouped (for instance the Red Balance, Blue Balance and Do White 104 * Balance V4L2 controls use the White Balance Component UVC control) or 105 * otherwise translated. The approach we take here is to use a translation 106 * table for the controls that can be mapped directly, and handle the others 107 * manually. 108 */ 109static int uvc_v4l2_query_menu(struct uvc_video_chain *chain, 110 struct v4l2_querymenu *query_menu) 111{ 112 struct uvc_menu_info *menu_info; 113 struct uvc_control_mapping *mapping; 114 struct uvc_control *ctrl; 115 u32 index = query_menu->index; 116 u32 id = query_menu->id; 117 118 ctrl = uvc_find_control(chain, query_menu->id, &mapping); 119 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) 120 return -EINVAL; 121 122 if (query_menu->index >= mapping->menu_count) 123 return -EINVAL; 124 125 memset(query_menu, 0, sizeof(*query_menu)); 126 query_menu->id = id; 127 query_menu->index = index; 128 129 menu_info = &mapping->menu_info[query_menu->index]; 130 strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name); 131 return 0; 132} 133 134/* 135 * Find the frame interval closest to the requested frame interval for the 136 * given frame format and size. This should be done by the device as part of 137 * the Video Probe and Commit negotiation, but some hardware don't implement 138 * that feature. 139 */ 140static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval) 141{ 142 unsigned int i; 143 144 if (frame->bFrameIntervalType) { 145 __u32 best = -1, dist; 146 147 for (i = 0; i < frame->bFrameIntervalType; ++i) { 148 dist = interval > frame->dwFrameInterval[i] 149 ? interval - frame->dwFrameInterval[i] 150 : frame->dwFrameInterval[i] - interval; 151 152 if (dist > best) 153 break; 154 155 best = dist; 156 } 157 158 interval = frame->dwFrameInterval[i-1]; 159 } else { 160 const __u32 min = frame->dwFrameInterval[0]; 161 const __u32 max = frame->dwFrameInterval[1]; 162 const __u32 step = frame->dwFrameInterval[2]; 163 164 interval = min + (interval - min + step/2) / step * step; 165 if (interval > max) 166 interval = max; 167 } 168 169 return interval; 170} 171 172static int uvc_v4l2_try_format(struct uvc_streaming *stream, 173 struct v4l2_format *fmt, struct uvc_streaming_control *probe, 174 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame) 175{ 176 struct uvc_format *format = NULL; 177 struct uvc_frame *frame = NULL; 178 __u16 rw, rh; 179 unsigned int d, maxd; 180 unsigned int i; 181 __u32 interval; 182 int ret = 0; 183 __u8 *fcc; 184 185 if (fmt->type != stream->type) 186 return -EINVAL; 187 188 fcc = (__u8 *)&fmt->fmt.pix.pixelformat; 189 uvc_trace(UVC_TRACE_FORMAT, "Trying format 0x%08x (%c%c%c%c): %ux%u.\n", 190 fmt->fmt.pix.pixelformat, 191 fcc[0], fcc[1], fcc[2], fcc[3], 192 fmt->fmt.pix.width, fmt->fmt.pix.height); 193 194 /* Check if the hardware supports the requested format. */ 195 for (i = 0; i < stream->nformats; ++i) { 196 format = &stream->format[i]; 197 if (format->fcc == fmt->fmt.pix.pixelformat) 198 break; 199 } 200 201 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) { 202 uvc_trace(UVC_TRACE_FORMAT, "Unsupported format 0x%08x.\n", 203 fmt->fmt.pix.pixelformat); 204 return -EINVAL; 205 } 206 207 /* Find the closest image size. The distance between image sizes is 208 * the size in pixels of the non-overlapping regions between the 209 * requested size and the frame-specified size. 210 */ 211 rw = fmt->fmt.pix.width; 212 rh = fmt->fmt.pix.height; 213 maxd = (unsigned int)-1; 214 215 for (i = 0; i < format->nframes; ++i) { 216 __u16 w = format->frame[i].wWidth; 217 __u16 h = format->frame[i].wHeight; 218 219 d = min(w, rw) * min(h, rh); 220 d = w*h + rw*rh - 2*d; 221 if (d < maxd) { 222 maxd = d; 223 frame = &format->frame[i]; 224 } 225 226 if (maxd == 0) 227 break; 228 } 229 230 if (frame == NULL) { 231 uvc_trace(UVC_TRACE_FORMAT, "Unsupported size %ux%u.\n", 232 fmt->fmt.pix.width, fmt->fmt.pix.height); 233 return -EINVAL; 234 } 235 236 /* Use the default frame interval. */ 237 interval = frame->dwDefaultFrameInterval; 238 uvc_trace(UVC_TRACE_FORMAT, "Using default frame interval %u.%u us " 239 "(%u.%u fps).\n", interval/10, interval%10, 10000000/interval, 240 (100000000/interval)%10); 241 242 /* Set the format index, frame index and frame interval. */ 243 memset(probe, 0, sizeof *probe); 244 probe->bmHint = 1; /* dwFrameInterval */ 245 probe->bFormatIndex = format->index; 246 probe->bFrameIndex = frame->bFrameIndex; 247 probe->dwFrameInterval = uvc_try_frame_interval(frame, interval); 248 if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS) 249 probe->dwMaxVideoFrameSize = 250 stream->ctrl.dwMaxVideoFrameSize; 251 252 /* Probe the device. */ 253 ret = uvc_probe_video(stream, probe); 254 if (ret < 0) 255 goto done; 256 257 fmt->fmt.pix.width = frame->wWidth; 258 fmt->fmt.pix.height = frame->wHeight; 259 fmt->fmt.pix.field = V4L2_FIELD_NONE; 260 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8; 261 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize; 262 fmt->fmt.pix.colorspace = format->colorspace; 263 fmt->fmt.pix.priv = 0; 264 265 if (uvc_format != NULL) 266 *uvc_format = format; 267 if (uvc_frame != NULL) 268 *uvc_frame = frame; 269 270done: 271 return ret; 272} 273 274static int uvc_v4l2_get_format(struct uvc_streaming *stream, 275 struct v4l2_format *fmt) 276{ 277 struct uvc_format *format = stream->cur_format; 278 struct uvc_frame *frame = stream->cur_frame; 279 280 if (fmt->type != stream->type) 281 return -EINVAL; 282 283 if (format == NULL || frame == NULL) 284 return -EINVAL; 285 286 fmt->fmt.pix.pixelformat = format->fcc; 287 fmt->fmt.pix.width = frame->wWidth; 288 fmt->fmt.pix.height = frame->wHeight; 289 fmt->fmt.pix.field = V4L2_FIELD_NONE; 290 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8; 291 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize; 292 fmt->fmt.pix.colorspace = format->colorspace; 293 fmt->fmt.pix.priv = 0; 294 295 return 0; 296} 297 298static int uvc_v4l2_set_format(struct uvc_streaming *stream, 299 struct v4l2_format *fmt) 300{ 301 struct uvc_streaming_control probe; 302 struct uvc_format *format; 303 struct uvc_frame *frame; 304 int ret; 305 306 if (fmt->type != stream->type) 307 return -EINVAL; 308 309 if (uvc_queue_allocated(&stream->queue)) 310 return -EBUSY; 311 312 ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame); 313 if (ret < 0) 314 return ret; 315 316 memcpy(&stream->ctrl, &probe, sizeof probe); 317 stream->cur_format = format; 318 stream->cur_frame = frame; 319 320 return 0; 321} 322 323static int uvc_v4l2_get_streamparm(struct uvc_streaming *stream, 324 struct v4l2_streamparm *parm) 325{ 326 uint32_t numerator, denominator; 327 328 if (parm->type != stream->type) 329 return -EINVAL; 330 331 numerator = stream->ctrl.dwFrameInterval; 332 denominator = 10000000; 333 uvc_simplify_fraction(&numerator, &denominator, 8, 333); 334 335 memset(parm, 0, sizeof *parm); 336 parm->type = stream->type; 337 338 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { 339 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 340 parm->parm.capture.capturemode = 0; 341 parm->parm.capture.timeperframe.numerator = numerator; 342 parm->parm.capture.timeperframe.denominator = denominator; 343 parm->parm.capture.extendedmode = 0; 344 parm->parm.capture.readbuffers = 0; 345 } else { 346 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME; 347 parm->parm.output.outputmode = 0; 348 parm->parm.output.timeperframe.numerator = numerator; 349 parm->parm.output.timeperframe.denominator = denominator; 350 } 351 352 return 0; 353} 354 355static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream, 356 struct v4l2_streamparm *parm) 357{ 358 struct uvc_frame *frame = stream->cur_frame; 359 struct uvc_streaming_control probe; 360 struct v4l2_fract timeperframe; 361 uint32_t interval; 362 int ret; 363 364 if (parm->type != stream->type) 365 return -EINVAL; 366 367 if (uvc_queue_streaming(&stream->queue)) 368 return -EBUSY; 369 370 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 371 timeperframe = parm->parm.capture.timeperframe; 372 else 373 timeperframe = parm->parm.output.timeperframe; 374 375 memcpy(&probe, &stream->ctrl, sizeof probe); 376 interval = uvc_fraction_to_interval(timeperframe.numerator, 377 timeperframe.denominator); 378 379 uvc_trace(UVC_TRACE_FORMAT, "Setting frame interval to %u/%u (%u).\n", 380 timeperframe.numerator, timeperframe.denominator, interval); 381 probe.dwFrameInterval = uvc_try_frame_interval(frame, interval); 382 383 /* Probe the device with the new settings. */ 384 ret = uvc_probe_video(stream, &probe); 385 if (ret < 0) 386 return ret; 387 388 memcpy(&stream->ctrl, &probe, sizeof probe); 389 390 /* Return the actual frame period. */ 391 timeperframe.numerator = probe.dwFrameInterval; 392 timeperframe.denominator = 10000000; 393 uvc_simplify_fraction(&timeperframe.numerator, 394 &timeperframe.denominator, 8, 333); 395 396 if (parm->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 397 parm->parm.capture.timeperframe = timeperframe; 398 else 399 parm->parm.output.timeperframe = timeperframe; 400 401 return 0; 402} 403 404/* ------------------------------------------------------------------------ 405 * Privilege management 406 */ 407 408/* 409 * Privilege management is the multiple-open implementation basis. The current 410 * implementation is completely transparent for the end-user and doesn't 411 * require explicit use of the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls. 412 * Those ioctls enable finer control on the device (by making possible for a 413 * user to request exclusive access to a device), but are not mature yet. 414 * Switching to the V4L2 priority mechanism might be considered in the future 415 * if this situation changes. 416 * 417 * Each open instance of a UVC device can either be in a privileged or 418 * unprivileged state. Only a single instance can be in a privileged state at 419 * a given time. Trying to perform an operation that requires privileges will 420 * automatically acquire the required privileges if possible, or return -EBUSY 421 * otherwise. Privileges are dismissed when closing the instance or when 422 * freeing the video buffers using VIDIOC_REQBUFS. 423 * 424 * Operations that require privileges are: 425 * 426 * - VIDIOC_S_INPUT 427 * - VIDIOC_S_PARM 428 * - VIDIOC_S_FMT 429 * - VIDIOC_REQBUFS 430 */ 431static int uvc_acquire_privileges(struct uvc_fh *handle) 432{ 433 /* Always succeed if the handle is already privileged. */ 434 if (handle->state == UVC_HANDLE_ACTIVE) 435 return 0; 436 437 /* Check if the device already has a privileged handle. */ 438 if (atomic_inc_return(&handle->stream->active) != 1) { 439 atomic_dec(&handle->stream->active); 440 return -EBUSY; 441 } 442 443 handle->state = UVC_HANDLE_ACTIVE; 444 return 0; 445} 446 447static void uvc_dismiss_privileges(struct uvc_fh *handle) 448{ 449 if (handle->state == UVC_HANDLE_ACTIVE) 450 atomic_dec(&handle->stream->active); 451 452 handle->state = UVC_HANDLE_PASSIVE; 453} 454 455static int uvc_has_privileges(struct uvc_fh *handle) 456{ 457 return handle->state == UVC_HANDLE_ACTIVE; 458} 459 460/* ------------------------------------------------------------------------ 461 * V4L2 file operations 462 */ 463 464static int uvc_v4l2_open(struct file *file) 465{ 466 struct uvc_streaming *stream; 467 struct uvc_fh *handle; 468 int ret = 0; 469 470 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_open\n"); 471 stream = video_drvdata(file); 472 473 if (stream->dev->state & UVC_DEV_DISCONNECTED) 474 return -ENODEV; 475 476 ret = usb_autopm_get_interface(stream->dev->intf); 477 if (ret < 0) 478 return ret; 479 480 /* Create the device handle. */ 481 handle = kzalloc(sizeof *handle, GFP_KERNEL); 482 if (handle == NULL) { 483 usb_autopm_put_interface(stream->dev->intf); 484 return -ENOMEM; 485 } 486 487 if (atomic_inc_return(&stream->dev->users) == 1) { 488 ret = uvc_status_start(stream->dev); 489 if (ret < 0) { 490 usb_autopm_put_interface(stream->dev->intf); 491 atomic_dec(&stream->dev->users); 492 kfree(handle); 493 return ret; 494 } 495 } 496 497 handle->chain = stream->chain; 498 handle->stream = stream; 499 handle->state = UVC_HANDLE_PASSIVE; 500 file->private_data = handle; 501 502 return 0; 503} 504 505static int uvc_v4l2_release(struct file *file) 506{ 507 struct uvc_fh *handle = file->private_data; 508 struct uvc_streaming *stream = handle->stream; 509 510 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_release\n"); 511 512 /* Only free resources if this is a privileged handle. */ 513 if (uvc_has_privileges(handle)) { 514 uvc_video_enable(stream, 0); 515 516 mutex_lock(&stream->queue.mutex); 517 if (uvc_free_buffers(&stream->queue) < 0) 518 uvc_printk(KERN_ERR, "uvc_v4l2_release: Unable to " 519 "free buffers.\n"); 520 mutex_unlock(&stream->queue.mutex); 521 } 522 523 /* Release the file handle. */ 524 uvc_dismiss_privileges(handle); 525 kfree(handle); 526 file->private_data = NULL; 527 528 if (atomic_dec_return(&stream->dev->users) == 0) 529 uvc_status_stop(stream->dev); 530 531 usb_autopm_put_interface(stream->dev->intf); 532 return 0; 533} 534 535static long uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg) 536{ 537 struct video_device *vdev = video_devdata(file); 538 struct uvc_fh *handle = file->private_data; 539 struct uvc_video_chain *chain = handle->chain; 540 struct uvc_streaming *stream = handle->stream; 541 long ret = 0; 542 543 switch (cmd) { 544 /* Query capabilities */ 545 case VIDIOC_QUERYCAP: 546 { 547 struct v4l2_capability *cap = arg; 548 549 memset(cap, 0, sizeof *cap); 550 strlcpy(cap->driver, "uvcvideo", sizeof cap->driver); 551 strlcpy(cap->card, vdev->name, sizeof cap->card); 552 usb_make_path(stream->dev->udev, 553 cap->bus_info, sizeof(cap->bus_info)); 554 cap->version = DRIVER_VERSION_NUMBER; 555 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 556 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE 557 | V4L2_CAP_STREAMING; 558 else 559 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT 560 | V4L2_CAP_STREAMING; 561 break; 562 } 563 564 /* Get, Set & Query control */ 565 case VIDIOC_QUERYCTRL: 566 return uvc_query_v4l2_ctrl(chain, arg); 567 568 case VIDIOC_G_CTRL: 569 { 570 struct v4l2_control *ctrl = arg; 571 struct v4l2_ext_control xctrl; 572 573 memset(&xctrl, 0, sizeof xctrl); 574 xctrl.id = ctrl->id; 575 576 ret = uvc_ctrl_begin(chain); 577 if (ret < 0) 578 return ret; 579 580 ret = uvc_ctrl_get(chain, &xctrl); 581 uvc_ctrl_rollback(chain); 582 if (ret >= 0) 583 ctrl->value = xctrl.value; 584 break; 585 } 586 587 case VIDIOC_S_CTRL: 588 { 589 struct v4l2_control *ctrl = arg; 590 struct v4l2_ext_control xctrl; 591 592 memset(&xctrl, 0, sizeof xctrl); 593 xctrl.id = ctrl->id; 594 xctrl.value = ctrl->value; 595 596 ret = uvc_ctrl_begin(chain); 597 if (ret < 0) 598 return ret; 599 600 ret = uvc_ctrl_set(chain, &xctrl); 601 if (ret < 0) { 602 uvc_ctrl_rollback(chain); 603 return ret; 604 } 605 ret = uvc_ctrl_commit(chain); 606 if (ret == 0) 607 ctrl->value = xctrl.value; 608 break; 609 } 610 611 case VIDIOC_QUERYMENU: 612 return uvc_v4l2_query_menu(chain, arg); 613 614 case VIDIOC_G_EXT_CTRLS: 615 { 616 struct v4l2_ext_controls *ctrls = arg; 617 struct v4l2_ext_control *ctrl = ctrls->controls; 618 unsigned int i; 619 620 ret = uvc_ctrl_begin(chain); 621 if (ret < 0) 622 return ret; 623 624 for (i = 0; i < ctrls->count; ++ctrl, ++i) { 625 ret = uvc_ctrl_get(chain, ctrl); 626 if (ret < 0) { 627 uvc_ctrl_rollback(chain); 628 ctrls->error_idx = i; 629 return ret; 630 } 631 } 632 ctrls->error_idx = 0; 633 ret = uvc_ctrl_rollback(chain); 634 break; 635 } 636 637 case VIDIOC_S_EXT_CTRLS: 638 case VIDIOC_TRY_EXT_CTRLS: 639 { 640 struct v4l2_ext_controls *ctrls = arg; 641 struct v4l2_ext_control *ctrl = ctrls->controls; 642 unsigned int i; 643 644 ret = uvc_ctrl_begin(chain); 645 if (ret < 0) 646 return ret; 647 648 for (i = 0; i < ctrls->count; ++ctrl, ++i) { 649 ret = uvc_ctrl_set(chain, ctrl); 650 if (ret < 0) { 651 uvc_ctrl_rollback(chain); 652 ctrls->error_idx = i; 653 return ret; 654 } 655 } 656 657 ctrls->error_idx = 0; 658 659 if (cmd == VIDIOC_S_EXT_CTRLS) 660 ret = uvc_ctrl_commit(chain); 661 else 662 ret = uvc_ctrl_rollback(chain); 663 break; 664 } 665 666 /* Get, Set & Enum input */ 667 case VIDIOC_ENUMINPUT: 668 { 669 const struct uvc_entity *selector = chain->selector; 670 struct v4l2_input *input = arg; 671 struct uvc_entity *iterm = NULL; 672 u32 index = input->index; 673 int pin = 0; 674 675 if (selector == NULL || 676 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { 677 if (index != 0) 678 return -EINVAL; 679 list_for_each_entry(iterm, &chain->entities, chain) { 680 if (UVC_ENTITY_IS_ITERM(iterm)) 681 break; 682 } 683 pin = iterm->id; 684 } else if (pin < selector->bNrInPins) { 685 pin = selector->baSourceID[index]; 686 list_for_each_entry(iterm, &chain->entities, chain) { 687 if (!UVC_ENTITY_IS_ITERM(iterm)) 688 continue; 689 if (iterm->id == pin) 690 break; 691 } 692 } 693 694 if (iterm == NULL || iterm->id != pin) 695 return -EINVAL; 696 697 memset(input, 0, sizeof *input); 698 input->index = index; 699 strlcpy(input->name, iterm->name, sizeof input->name); 700 if (UVC_ENTITY_TYPE(iterm) == UVC_ITT_CAMERA) 701 input->type = V4L2_INPUT_TYPE_CAMERA; 702 break; 703 } 704 705 case VIDIOC_G_INPUT: 706 { 707 u8 input; 708 709 if (chain->selector == NULL || 710 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { 711 *(int *)arg = 0; 712 break; 713 } 714 715 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, 716 chain->selector->id, chain->dev->intfnum, 717 UVC_SU_INPUT_SELECT_CONTROL, &input, 1); 718 if (ret < 0) 719 return ret; 720 721 *(int *)arg = input - 1; 722 break; 723 } 724 725 case VIDIOC_S_INPUT: 726 { 727 u32 input = *(u32 *)arg + 1; 728 729 if ((ret = uvc_acquire_privileges(handle)) < 0) 730 return ret; 731 732 if (chain->selector == NULL || 733 (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { 734 if (input != 1) 735 return -EINVAL; 736 break; 737 } 738 739 if (input == 0 || input > chain->selector->bNrInPins) 740 return -EINVAL; 741 742 return uvc_query_ctrl(chain->dev, UVC_SET_CUR, 743 chain->selector->id, chain->dev->intfnum, 744 UVC_SU_INPUT_SELECT_CONTROL, &input, 1); 745 } 746 747 /* Try, Get, Set & Enum format */ 748 case VIDIOC_ENUM_FMT: 749 { 750 struct v4l2_fmtdesc *fmt = arg; 751 struct uvc_format *format; 752 enum v4l2_buf_type type = fmt->type; 753 __u32 index = fmt->index; 754 755 if (fmt->type != stream->type || 756 fmt->index >= stream->nformats) 757 return -EINVAL; 758 759 memset(fmt, 0, sizeof(*fmt)); 760 fmt->index = index; 761 fmt->type = type; 762 763 format = &stream->format[fmt->index]; 764 fmt->flags = 0; 765 if (format->flags & UVC_FMT_FLAG_COMPRESSED) 766 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED; 767 strlcpy(fmt->description, format->name, 768 sizeof fmt->description); 769 fmt->description[sizeof fmt->description - 1] = 0; 770 fmt->pixelformat = format->fcc; 771 break; 772 } 773 774 case VIDIOC_TRY_FMT: 775 { 776 struct uvc_streaming_control probe; 777 778 return uvc_v4l2_try_format(stream, arg, &probe, NULL, NULL); 779 } 780 781 case VIDIOC_S_FMT: 782 if ((ret = uvc_acquire_privileges(handle)) < 0) 783 return ret; 784 785 return uvc_v4l2_set_format(stream, arg); 786 787 case VIDIOC_G_FMT: 788 return uvc_v4l2_get_format(stream, arg); 789 790 /* Frame size enumeration */ 791 case VIDIOC_ENUM_FRAMESIZES: 792 { 793 struct v4l2_frmsizeenum *fsize = arg; 794 struct uvc_format *format = NULL; 795 struct uvc_frame *frame; 796 int i; 797 798 /* Look for the given pixel format */ 799 for (i = 0; i < stream->nformats; i++) { 800 if (stream->format[i].fcc == 801 fsize->pixel_format) { 802 format = &stream->format[i]; 803 break; 804 } 805 } 806 if (format == NULL) 807 return -EINVAL; 808 809 if (fsize->index >= format->nframes) 810 return -EINVAL; 811 812 frame = &format->frame[fsize->index]; 813 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 814 fsize->discrete.width = frame->wWidth; 815 fsize->discrete.height = frame->wHeight; 816 break; 817 } 818 819 /* Frame interval enumeration */ 820 case VIDIOC_ENUM_FRAMEINTERVALS: 821 { 822 struct v4l2_frmivalenum *fival = arg; 823 struct uvc_format *format = NULL; 824 struct uvc_frame *frame = NULL; 825 int i; 826 827 /* Look for the given pixel format and frame size */ 828 for (i = 0; i < stream->nformats; i++) { 829 if (stream->format[i].fcc == 830 fival->pixel_format) { 831 format = &stream->format[i]; 832 break; 833 } 834 } 835 if (format == NULL) 836 return -EINVAL; 837 838 for (i = 0; i < format->nframes; i++) { 839 if (format->frame[i].wWidth == fival->width && 840 format->frame[i].wHeight == fival->height) { 841 frame = &format->frame[i]; 842 break; 843 } 844 } 845 if (frame == NULL) 846 return -EINVAL; 847 848 if (frame->bFrameIntervalType) { 849 if (fival->index >= frame->bFrameIntervalType) 850 return -EINVAL; 851 852 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; 853 fival->discrete.numerator = 854 frame->dwFrameInterval[fival->index]; 855 fival->discrete.denominator = 10000000; 856 uvc_simplify_fraction(&fival->discrete.numerator, 857 &fival->discrete.denominator, 8, 333); 858 } else { 859 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE; 860 fival->stepwise.min.numerator = 861 frame->dwFrameInterval[0]; 862 fival->stepwise.min.denominator = 10000000; 863 fival->stepwise.max.numerator = 864 frame->dwFrameInterval[1]; 865 fival->stepwise.max.denominator = 10000000; 866 fival->stepwise.step.numerator = 867 frame->dwFrameInterval[2]; 868 fival->stepwise.step.denominator = 10000000; 869 uvc_simplify_fraction(&fival->stepwise.min.numerator, 870 &fival->stepwise.min.denominator, 8, 333); 871 uvc_simplify_fraction(&fival->stepwise.max.numerator, 872 &fival->stepwise.max.denominator, 8, 333); 873 uvc_simplify_fraction(&fival->stepwise.step.numerator, 874 &fival->stepwise.step.denominator, 8, 333); 875 } 876 break; 877 } 878 879 /* Get & Set streaming parameters */ 880 case VIDIOC_G_PARM: 881 return uvc_v4l2_get_streamparm(stream, arg); 882 883 case VIDIOC_S_PARM: 884 if ((ret = uvc_acquire_privileges(handle)) < 0) 885 return ret; 886 887 return uvc_v4l2_set_streamparm(stream, arg); 888 889 /* Cropping and scaling */ 890 case VIDIOC_CROPCAP: 891 { 892 struct v4l2_cropcap *ccap = arg; 893 struct uvc_frame *frame = stream->cur_frame; 894 895 if (ccap->type != stream->type) 896 return -EINVAL; 897 898 ccap->bounds.left = 0; 899 ccap->bounds.top = 0; 900 ccap->bounds.width = frame->wWidth; 901 ccap->bounds.height = frame->wHeight; 902 903 ccap->defrect = ccap->bounds; 904 905 ccap->pixelaspect.numerator = 1; 906 ccap->pixelaspect.denominator = 1; 907 break; 908 } 909 910 case VIDIOC_G_CROP: 911 case VIDIOC_S_CROP: 912 return -EINVAL; 913 914 /* Buffers & streaming */ 915 case VIDIOC_REQBUFS: 916 { 917 struct v4l2_requestbuffers *rb = arg; 918 unsigned int bufsize = 919 stream->ctrl.dwMaxVideoFrameSize; 920 921 if (rb->type != stream->type || 922 rb->memory != V4L2_MEMORY_MMAP) 923 return -EINVAL; 924 925 if ((ret = uvc_acquire_privileges(handle)) < 0) 926 return ret; 927 928 ret = uvc_alloc_buffers(&stream->queue, rb->count, bufsize); 929 if (ret < 0) 930 return ret; 931 932 if (ret == 0) 933 uvc_dismiss_privileges(handle); 934 935 rb->count = ret; 936 ret = 0; 937 break; 938 } 939 940 case VIDIOC_QUERYBUF: 941 { 942 struct v4l2_buffer *buf = arg; 943 944 if (buf->type != stream->type) 945 return -EINVAL; 946 947 if (!uvc_has_privileges(handle)) 948 return -EBUSY; 949 950 return uvc_query_buffer(&stream->queue, buf); 951 } 952 953 case VIDIOC_QBUF: 954 if (!uvc_has_privileges(handle)) 955 return -EBUSY; 956 957 return uvc_queue_buffer(&stream->queue, arg); 958 959 case VIDIOC_DQBUF: 960 if (!uvc_has_privileges(handle)) 961 return -EBUSY; 962 963 return uvc_dequeue_buffer(&stream->queue, arg, 964 file->f_flags & O_NONBLOCK); 965 966 case VIDIOC_STREAMON: 967 { 968 int *type = arg; 969 970 if (*type != stream->type) 971 return -EINVAL; 972 973 if (!uvc_has_privileges(handle)) 974 return -EBUSY; 975 976 ret = uvc_video_enable(stream, 1); 977 if (ret < 0) 978 return ret; 979 break; 980 } 981 982 case VIDIOC_STREAMOFF: 983 { 984 int *type = arg; 985 986 if (*type != stream->type) 987 return -EINVAL; 988 989 if (!uvc_has_privileges(handle)) 990 return -EBUSY; 991 992 return uvc_video_enable(stream, 0); 993 } 994 995 /* Analog video standards make no sense for digital cameras. */ 996 case VIDIOC_ENUMSTD: 997 case VIDIOC_QUERYSTD: 998 case VIDIOC_G_STD: 999 case VIDIOC_S_STD: 1000 1001 case VIDIOC_OVERLAY: 1002 1003 case VIDIOC_ENUMAUDIO: 1004 case VIDIOC_ENUMAUDOUT: 1005 1006 case VIDIOC_ENUMOUTPUT: 1007 uvc_trace(UVC_TRACE_IOCTL, "Unsupported ioctl 0x%08x\n", cmd); 1008 return -EINVAL; 1009 1010 /* Dynamic controls. */ 1011 case UVCIOC_CTRL_ADD: 1012 { 1013 struct uvc_xu_control_info *xinfo = arg; 1014 struct uvc_control_info *info; 1015 1016 if (!capable(CAP_SYS_ADMIN)) 1017 return -EPERM; 1018 1019 if (xinfo->size == 0) 1020 return -EINVAL; 1021 1022 info = kzalloc(sizeof *info, GFP_KERNEL); 1023 if (info == NULL) 1024 return -ENOMEM; 1025 1026 memcpy(info->entity, xinfo->entity, sizeof info->entity); 1027 info->index = xinfo->index; 1028 info->selector = xinfo->selector; 1029 info->size = xinfo->size; 1030 info->flags = xinfo->flags; 1031 1032 info->flags |= UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX | 1033 UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF | 1034 UVC_CONTROL_EXTENSION; 1035 1036 ret = uvc_ctrl_add_info(info); 1037 if (ret < 0) 1038 kfree(info); 1039 break; 1040 } 1041 1042 case UVCIOC_CTRL_MAP_OLD: 1043 case UVCIOC_CTRL_MAP: 1044 if (!capable(CAP_SYS_ADMIN)) 1045 return -EPERM; 1046 1047 return uvc_ioctl_ctrl_map(arg, cmd == UVCIOC_CTRL_MAP_OLD); 1048 1049 case UVCIOC_CTRL_GET: 1050 return uvc_xu_ctrl_query(chain, arg, 0); 1051 1052 case UVCIOC_CTRL_SET: 1053 return uvc_xu_ctrl_query(chain, arg, 1); 1054 1055 default: 1056 if ((ret = v4l_compat_translate_ioctl(file, cmd, arg, 1057 uvc_v4l2_do_ioctl)) == -ENOIOCTLCMD) 1058 uvc_trace(UVC_TRACE_IOCTL, "Unknown ioctl 0x%08x\n", 1059 cmd); 1060 return ret; 1061 } 1062 1063 return ret; 1064} 1065 1066static long uvc_v4l2_ioctl(struct file *file, 1067 unsigned int cmd, unsigned long arg) 1068{ 1069 if (uvc_trace_param & UVC_TRACE_IOCTL) { 1070 uvc_printk(KERN_DEBUG, "uvc_v4l2_ioctl("); 1071 v4l_printk_ioctl(cmd); 1072 printk(")\n"); 1073 } 1074 1075 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl); 1076} 1077 1078static ssize_t uvc_v4l2_read(struct file *file, char __user *data, 1079 size_t count, loff_t *ppos) 1080{ 1081 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_read: not implemented.\n"); 1082 return -EINVAL; 1083} 1084 1085/* 1086 * VMA operations. 1087 */ 1088static void uvc_vm_open(struct vm_area_struct *vma) 1089{ 1090 struct uvc_buffer *buffer = vma->vm_private_data; 1091 buffer->vma_use_count++; 1092} 1093 1094static void uvc_vm_close(struct vm_area_struct *vma) 1095{ 1096 struct uvc_buffer *buffer = vma->vm_private_data; 1097 buffer->vma_use_count--; 1098} 1099 1100static const struct vm_operations_struct uvc_vm_ops = { 1101 .open = uvc_vm_open, 1102 .close = uvc_vm_close, 1103}; 1104 1105static int uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma) 1106{ 1107 struct uvc_fh *handle = file->private_data; 1108 struct uvc_streaming *stream = handle->stream; 1109 struct uvc_video_queue *queue = &stream->queue; 1110 struct uvc_buffer *uninitialized_var(buffer); 1111 struct page *page; 1112 unsigned long addr, start, size; 1113 unsigned int i; 1114 int ret = 0; 1115 1116 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_mmap\n"); 1117 1118 start = vma->vm_start; 1119 size = vma->vm_end - vma->vm_start; 1120 1121 mutex_lock(&queue->mutex); 1122 1123 for (i = 0; i < queue->count; ++i) { 1124 buffer = &queue->buffer[i]; 1125 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff) 1126 break; 1127 } 1128 1129 if (i == queue->count || size != queue->buf_size) { 1130 ret = -EINVAL; 1131 goto done; 1132 } 1133 1134 /* 1135 * VM_IO marks the area as being an mmaped region for I/O to a 1136 * device. It also prevents the region from being core dumped. 1137 */ 1138 vma->vm_flags |= VM_IO; 1139 1140 addr = (unsigned long)queue->mem + buffer->buf.m.offset; 1141 while (size > 0) { 1142 page = vmalloc_to_page((void *)addr); 1143 if ((ret = vm_insert_page(vma, start, page)) < 0) 1144 goto done; 1145 1146 start += PAGE_SIZE; 1147 addr += PAGE_SIZE; 1148 size -= PAGE_SIZE; 1149 } 1150 1151 vma->vm_ops = &uvc_vm_ops; 1152 vma->vm_private_data = buffer; 1153 uvc_vm_open(vma); 1154 1155done: 1156 mutex_unlock(&queue->mutex); 1157 return ret; 1158} 1159 1160static unsigned int uvc_v4l2_poll(struct file *file, poll_table *wait) 1161{ 1162 struct uvc_fh *handle = file->private_data; 1163 struct uvc_streaming *stream = handle->stream; 1164 1165 uvc_trace(UVC_TRACE_CALLS, "uvc_v4l2_poll\n"); 1166 1167 return uvc_queue_poll(&stream->queue, file, wait); 1168} 1169 1170const struct v4l2_file_operations uvc_fops = { 1171 .owner = THIS_MODULE, 1172 .open = uvc_v4l2_open, 1173 .release = uvc_v4l2_release, 1174 .ioctl = uvc_v4l2_ioctl, 1175 .read = uvc_v4l2_read, 1176 .mmap = uvc_v4l2_mmap, 1177 .poll = uvc_v4l2_poll, 1178}; 1179