1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * uvc_v4l2.c -- USB Video Class Gadget driver 4 * 5 * Copyright (C) 2009-2010 6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com) 7 */ 8 9 #include <linux/device.h> 10 #include <linux/errno.h> 11 #include <linux/kernel.h> 12 #include <linux/list.h> 13 #include <linux/usb/g_uvc.h> 14 #include <linux/usb/uvc.h> 15 #include <linux/videodev2.h> 16 #include <linux/vmalloc.h> 17 #include <linux/wait.h> 18 19 #include <media/v4l2-dev.h> 20 #include <media/v4l2-event.h> 21 #include <media/v4l2-ioctl.h> 22 23 #include "f_uvc.h" 24 #include "uvc.h" 25 #include "uvc_queue.h" 26 #include "uvc_video.h" 27 #include "uvc_v4l2.h" 28 #include "uvc_configfs.h" 29 30 static const struct uvc_format_desc *to_uvc_format(struct uvcg_format *uformat) 31 { 32 char guid[16] = UVC_GUID_FORMAT_MJPEG; 33 const struct uvc_format_desc *format; 34 struct uvcg_uncompressed *unc; 35 36 if (uformat->type == UVCG_UNCOMPRESSED) { 37 unc = to_uvcg_uncompressed(&uformat->group.cg_item); 38 if (!unc) 39 return ERR_PTR(-EINVAL); 40 41 memcpy(guid, unc->desc.guidFormat, sizeof(guid)); 42 } 43 44 format = uvc_format_by_guid(guid); 45 if (!format) 46 return ERR_PTR(-EINVAL); 47 48 return format; 49 } 50 51 static int uvc_v4l2_get_bytesperline(struct uvcg_format *uformat, 52 struct uvcg_frame *uframe) 53 { 54 struct uvcg_uncompressed *u; 55 56 if (uformat->type == UVCG_UNCOMPRESSED) { 57 u = to_uvcg_uncompressed(&uformat->group.cg_item); 58 if (!u) 59 return 0; 60 61 return u->desc.bBitsPerPixel * uframe->frame.w_width / 8; 62 } 63 64 return 0; 65 } 66 67 static int uvc_get_frame_size(struct uvcg_format *uformat, 68 struct uvcg_frame *uframe) 69 { 70 unsigned int bpl = uvc_v4l2_get_bytesperline(uformat, uframe); 71 72 return bpl ? bpl * uframe->frame.w_height : 73 uframe->frame.dw_max_video_frame_buffer_size; 74 } 75 76 static struct uvcg_format *find_format_by_index(struct uvc_device *uvc, int index) 77 { 78 struct uvcg_format_ptr *format; 79 struct uvcg_format *uformat = NULL; 80 int i = 1; 81 82 list_for_each_entry(format, &uvc->header->formats, entry) { 83 if (index == i) { 84 uformat = format->fmt; 85 break; 86 } 87 i++; 88 } 89 90 return uformat; 91 } 92 93 static struct uvcg_frame *find_frame_by_index(struct uvc_device *uvc, 94 struct uvcg_format *uformat, 95 int index) 96 { 97 struct uvcg_format_ptr *format; 98 struct uvcg_frame_ptr *frame; 99 struct uvcg_frame *uframe = NULL; 100 101 list_for_each_entry(format, &uvc->header->formats, entry) { 102 if (format->fmt->type != uformat->type) 103 continue; 104 list_for_each_entry(frame, &format->fmt->frames, entry) { 105 if (index == frame->frm->frame.b_frame_index) { 106 uframe = frame->frm; 107 break; 108 } 109 } 110 } 111 112 return uframe; 113 } 114 115 static struct uvcg_format *find_format_by_pix(struct uvc_device *uvc, 116 u32 pixelformat) 117 { 118 struct uvcg_format_ptr *format; 119 struct uvcg_format *uformat = NULL; 120 121 list_for_each_entry(format, &uvc->header->formats, entry) { 122 const struct uvc_format_desc *fmtdesc = to_uvc_format(format->fmt); 123 124 if (fmtdesc->fcc == pixelformat) { 125 uformat = format->fmt; 126 break; 127 } 128 } 129 130 return uformat; 131 } 132 133 static struct uvcg_frame *find_closest_frame_by_size(struct uvc_device *uvc, 134 struct uvcg_format *uformat, 135 u16 rw, u16 rh) 136 { 137 struct uvc_video *video = &uvc->video; 138 struct uvcg_format_ptr *format; 139 struct uvcg_frame_ptr *frame; 140 struct uvcg_frame *uframe = NULL; 141 unsigned int d, maxd; 142 143 /* Find the closest image size. The distance between image sizes is 144 * the size in pixels of the non-overlapping regions between the 145 * requested size and the frame-specified size. 146 */ 147 maxd = (unsigned int)-1; 148 149 list_for_each_entry(format, &uvc->header->formats, entry) { 150 if (format->fmt->type != uformat->type) 151 continue; 152 153 list_for_each_entry(frame, &format->fmt->frames, entry) { 154 u16 w, h; 155 156 w = frame->frm->frame.w_width; 157 h = frame->frm->frame.w_height; 158 159 d = min(w, rw) * min(h, rh); 160 d = w*h + rw*rh - 2*d; 161 if (d < maxd) { 162 maxd = d; 163 uframe = frame->frm; 164 } 165 166 if (maxd == 0) 167 break; 168 } 169 } 170 171 if (!uframe) 172 uvcg_dbg(&video->uvc->func, "Unsupported size %ux%u\n", rw, rh); 173 174 return uframe; 175 } 176 177 /* -------------------------------------------------------------------------- 178 * Requests handling 179 */ 180 181 static int 182 uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data) 183 { 184 struct usb_composite_dev *cdev = uvc->func.config->cdev; 185 struct usb_request *req = uvc->control_req; 186 187 if (data->length < 0) 188 return usb_ep_set_halt(cdev->gadget->ep0); 189 190 req->length = min_t(unsigned int, uvc->event_length, data->length); 191 req->zero = data->length < uvc->event_length; 192 193 memcpy(req->buf, data->data, req->length); 194 195 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL); 196 } 197 198 /* -------------------------------------------------------------------------- 199 * V4L2 ioctls 200 */ 201 202 static int 203 uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap) 204 { 205 struct video_device *vdev = video_devdata(file); 206 struct uvc_device *uvc = video_get_drvdata(vdev); 207 struct usb_composite_dev *cdev = uvc->func.config->cdev; 208 209 strscpy(cap->driver, "g_uvc", sizeof(cap->driver)); 210 strscpy(cap->card, cdev->gadget->name, sizeof(cap->card)); 211 strscpy(cap->bus_info, dev_name(&cdev->gadget->dev), 212 sizeof(cap->bus_info)); 213 return 0; 214 } 215 216 static int 217 uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt) 218 { 219 struct video_device *vdev = video_devdata(file); 220 struct uvc_device *uvc = video_get_drvdata(vdev); 221 struct uvc_video *video = &uvc->video; 222 223 fmt->fmt.pix.pixelformat = video->fcc; 224 fmt->fmt.pix.width = video->width; 225 fmt->fmt.pix.height = video->height; 226 fmt->fmt.pix.field = V4L2_FIELD_NONE; 227 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8; 228 fmt->fmt.pix.sizeimage = video->imagesize; 229 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; 230 fmt->fmt.pix.priv = 0; 231 232 return 0; 233 } 234 235 static int 236 uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt) 237 { 238 struct video_device *vdev = video_devdata(file); 239 struct uvc_device *uvc = video_get_drvdata(vdev); 240 struct uvc_video *video = &uvc->video; 241 struct uvcg_format *uformat; 242 struct uvcg_frame *uframe; 243 u8 *fcc; 244 245 if (fmt->type != video->queue.queue.type) 246 return -EINVAL; 247 248 fcc = (u8 *)&fmt->fmt.pix.pixelformat; 249 uvcg_dbg(&uvc->func, "Trying format 0x%08x (%c%c%c%c): %ux%u\n", 250 fmt->fmt.pix.pixelformat, 251 fcc[0], fcc[1], fcc[2], fcc[3], 252 fmt->fmt.pix.width, fmt->fmt.pix.height); 253 254 uformat = find_format_by_pix(uvc, fmt->fmt.pix.pixelformat); 255 if (!uformat) 256 return -EINVAL; 257 258 uframe = find_closest_frame_by_size(uvc, uformat, 259 fmt->fmt.pix.width, fmt->fmt.pix.height); 260 if (!uframe) 261 return -EINVAL; 262 263 if (uformat->type == UVCG_UNCOMPRESSED) { 264 struct uvcg_uncompressed *u = 265 to_uvcg_uncompressed(&uformat->group.cg_item); 266 if (!u) 267 return 0; 268 269 v4l2_fill_pixfmt(&fmt->fmt.pix, fmt->fmt.pix.pixelformat, 270 uframe->frame.w_width, uframe->frame.w_height); 271 272 if (fmt->fmt.pix.sizeimage != (uvc_v4l2_get_bytesperline(uformat, uframe) * 273 uframe->frame.w_height)) 274 return -EINVAL; 275 } else { 276 fmt->fmt.pix.width = uframe->frame.w_width; 277 fmt->fmt.pix.height = uframe->frame.w_height; 278 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(uformat, uframe); 279 fmt->fmt.pix.sizeimage = uvc_get_frame_size(uformat, uframe); 280 fmt->fmt.pix.pixelformat = to_uvc_format(uformat)->fcc; 281 } 282 fmt->fmt.pix.field = V4L2_FIELD_NONE; 283 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; 284 fmt->fmt.pix.priv = 0; 285 286 return 0; 287 } 288 289 static int 290 uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt) 291 { 292 struct video_device *vdev = video_devdata(file); 293 struct uvc_device *uvc = video_get_drvdata(vdev); 294 struct uvc_video *video = &uvc->video; 295 int ret; 296 297 ret = uvc_v4l2_try_format(file, fh, fmt); 298 if (ret) 299 return ret; 300 301 video->fcc = fmt->fmt.pix.pixelformat; 302 video->bpp = fmt->fmt.pix.bytesperline * 8 / video->width; 303 video->width = fmt->fmt.pix.width; 304 video->height = fmt->fmt.pix.height; 305 video->imagesize = fmt->fmt.pix.sizeimage; 306 307 return ret; 308 } 309 310 static int 311 uvc_v4l2_enum_frameintervals(struct file *file, void *fh, 312 struct v4l2_frmivalenum *fival) 313 { 314 struct video_device *vdev = video_devdata(file); 315 struct uvc_device *uvc = video_get_drvdata(vdev); 316 struct uvcg_format *uformat = NULL; 317 struct uvcg_frame *uframe = NULL; 318 struct uvcg_frame_ptr *frame; 319 320 uformat = find_format_by_pix(uvc, fival->pixel_format); 321 if (!uformat) 322 return -EINVAL; 323 324 list_for_each_entry(frame, &uformat->frames, entry) { 325 if (frame->frm->frame.w_width == fival->width && 326 frame->frm->frame.w_height == fival->height) { 327 uframe = frame->frm; 328 break; 329 } 330 } 331 if (!uframe) 332 return -EINVAL; 333 334 if (fival->index >= uframe->frame.b_frame_interval_type) 335 return -EINVAL; 336 337 fival->discrete.numerator = 338 uframe->dw_frame_interval[fival->index]; 339 340 /* TODO: handle V4L2_FRMIVAL_TYPE_STEPWISE */ 341 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; 342 fival->discrete.denominator = 10000000; 343 v4l2_simplify_fraction(&fival->discrete.numerator, 344 &fival->discrete.denominator, 8, 333); 345 346 return 0; 347 } 348 349 static int 350 uvc_v4l2_enum_framesizes(struct file *file, void *fh, 351 struct v4l2_frmsizeenum *fsize) 352 { 353 struct video_device *vdev = video_devdata(file); 354 struct uvc_device *uvc = video_get_drvdata(vdev); 355 struct uvcg_format *uformat = NULL; 356 struct uvcg_frame *uframe = NULL; 357 358 uformat = find_format_by_pix(uvc, fsize->pixel_format); 359 if (!uformat) 360 return -EINVAL; 361 362 if (fsize->index >= uformat->num_frames) 363 return -EINVAL; 364 365 uframe = find_frame_by_index(uvc, uformat, fsize->index + 1); 366 if (!uframe) 367 return -EINVAL; 368 369 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 370 fsize->discrete.width = uframe->frame.w_width; 371 fsize->discrete.height = uframe->frame.w_height; 372 373 return 0; 374 } 375 376 static int 377 uvc_v4l2_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f) 378 { 379 struct video_device *vdev = video_devdata(file); 380 struct uvc_device *uvc = video_get_drvdata(vdev); 381 const struct uvc_format_desc *fmtdesc; 382 struct uvcg_format *uformat; 383 384 if (f->index >= uvc->header->num_fmt) 385 return -EINVAL; 386 387 uformat = find_format_by_index(uvc, f->index + 1); 388 if (!uformat) 389 return -EINVAL; 390 391 fmtdesc = to_uvc_format(uformat); 392 f->pixelformat = fmtdesc->fcc; 393 394 return 0; 395 } 396 397 static int 398 uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b) 399 { 400 struct video_device *vdev = video_devdata(file); 401 struct uvc_device *uvc = video_get_drvdata(vdev); 402 struct uvc_video *video = &uvc->video; 403 404 if (b->type != video->queue.queue.type) 405 return -EINVAL; 406 407 return uvcg_alloc_buffers(&video->queue, b); 408 } 409 410 static int 411 uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b) 412 { 413 struct video_device *vdev = video_devdata(file); 414 struct uvc_device *uvc = video_get_drvdata(vdev); 415 struct uvc_video *video = &uvc->video; 416 417 return uvcg_query_buffer(&video->queue, b); 418 } 419 420 static int 421 uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b) 422 { 423 struct video_device *vdev = video_devdata(file); 424 struct uvc_device *uvc = video_get_drvdata(vdev); 425 struct uvc_video *video = &uvc->video; 426 int ret; 427 428 ret = uvcg_queue_buffer(&video->queue, b); 429 if (ret < 0) 430 return ret; 431 432 if (uvc->state == UVC_STATE_STREAMING) 433 queue_work(video->async_wq, &video->pump); 434 435 return ret; 436 } 437 438 static int 439 uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b) 440 { 441 struct video_device *vdev = video_devdata(file); 442 struct uvc_device *uvc = video_get_drvdata(vdev); 443 struct uvc_video *video = &uvc->video; 444 445 return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK); 446 } 447 448 static int 449 uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type) 450 { 451 struct video_device *vdev = video_devdata(file); 452 struct uvc_device *uvc = video_get_drvdata(vdev); 453 struct uvc_video *video = &uvc->video; 454 int ret; 455 456 if (type != video->queue.queue.type) 457 return -EINVAL; 458 459 /* Enable UVC video. */ 460 ret = uvcg_video_enable(video); 461 if (ret < 0) 462 return ret; 463 464 /* 465 * Complete the alternate setting selection setup phase now that 466 * userspace is ready to provide video frames. 467 */ 468 uvc_function_setup_continue(uvc, 0); 469 uvc->state = UVC_STATE_STREAMING; 470 471 return 0; 472 } 473 474 static int 475 uvc_v4l2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type) 476 { 477 struct video_device *vdev = video_devdata(file); 478 struct uvc_device *uvc = video_get_drvdata(vdev); 479 struct uvc_video *video = &uvc->video; 480 int ret = 0; 481 482 if (type != video->queue.queue.type) 483 return -EINVAL; 484 485 ret = uvcg_video_disable(video); 486 if (ret < 0) 487 return ret; 488 489 uvc->state = UVC_STATE_CONNECTED; 490 uvc_function_setup_continue(uvc, 1); 491 return 0; 492 } 493 494 static int 495 uvc_v4l2_subscribe_event(struct v4l2_fh *fh, 496 const struct v4l2_event_subscription *sub) 497 { 498 struct uvc_device *uvc = video_get_drvdata(fh->vdev); 499 struct uvc_file_handle *handle = to_uvc_file_handle(fh); 500 int ret; 501 502 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST) 503 return -EINVAL; 504 505 if (sub->type == UVC_EVENT_SETUP && uvc->func_connected) 506 return -EBUSY; 507 508 ret = v4l2_event_subscribe(fh, sub, 2, NULL); 509 if (ret < 0) 510 return ret; 511 512 if (sub->type == UVC_EVENT_SETUP) { 513 uvc->func_connected = true; 514 handle->is_uvc_app_handle = true; 515 uvc_function_connect(uvc); 516 } 517 518 return 0; 519 } 520 521 static void uvc_v4l2_disable(struct uvc_device *uvc) 522 { 523 uvc_function_disconnect(uvc); 524 uvcg_video_disable(&uvc->video); 525 uvcg_free_buffers(&uvc->video.queue); 526 uvc->func_connected = false; 527 wake_up_interruptible(&uvc->func_connected_queue); 528 } 529 530 static int 531 uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh, 532 const struct v4l2_event_subscription *sub) 533 { 534 struct uvc_device *uvc = video_get_drvdata(fh->vdev); 535 struct uvc_file_handle *handle = to_uvc_file_handle(fh); 536 int ret; 537 538 ret = v4l2_event_unsubscribe(fh, sub); 539 if (ret < 0) 540 return ret; 541 542 if (sub->type == UVC_EVENT_SETUP && handle->is_uvc_app_handle) { 543 uvc_v4l2_disable(uvc); 544 handle->is_uvc_app_handle = false; 545 } 546 547 return 0; 548 } 549 550 static long 551 uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio, 552 unsigned int cmd, void *arg) 553 { 554 struct video_device *vdev = video_devdata(file); 555 struct uvc_device *uvc = video_get_drvdata(vdev); 556 557 switch (cmd) { 558 case UVCIOC_SEND_RESPONSE: 559 return uvc_send_response(uvc, arg); 560 561 default: 562 return -ENOIOCTLCMD; 563 } 564 } 565 566 const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = { 567 .vidioc_querycap = uvc_v4l2_querycap, 568 .vidioc_try_fmt_vid_out = uvc_v4l2_try_format, 569 .vidioc_g_fmt_vid_out = uvc_v4l2_get_format, 570 .vidioc_s_fmt_vid_out = uvc_v4l2_set_format, 571 .vidioc_enum_frameintervals = uvc_v4l2_enum_frameintervals, 572 .vidioc_enum_framesizes = uvc_v4l2_enum_framesizes, 573 .vidioc_enum_fmt_vid_out = uvc_v4l2_enum_format, 574 .vidioc_reqbufs = uvc_v4l2_reqbufs, 575 .vidioc_querybuf = uvc_v4l2_querybuf, 576 .vidioc_qbuf = uvc_v4l2_qbuf, 577 .vidioc_dqbuf = uvc_v4l2_dqbuf, 578 .vidioc_streamon = uvc_v4l2_streamon, 579 .vidioc_streamoff = uvc_v4l2_streamoff, 580 .vidioc_subscribe_event = uvc_v4l2_subscribe_event, 581 .vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event, 582 .vidioc_default = uvc_v4l2_ioctl_default, 583 }; 584 585 /* -------------------------------------------------------------------------- 586 * V4L2 587 */ 588 589 static int 590 uvc_v4l2_open(struct file *file) 591 { 592 struct video_device *vdev = video_devdata(file); 593 struct uvc_device *uvc = video_get_drvdata(vdev); 594 struct uvc_file_handle *handle; 595 596 handle = kzalloc(sizeof(*handle), GFP_KERNEL); 597 if (handle == NULL) 598 return -ENOMEM; 599 600 v4l2_fh_init(&handle->vfh, vdev); 601 v4l2_fh_add(&handle->vfh); 602 603 handle->device = &uvc->video; 604 file->private_data = &handle->vfh; 605 606 return 0; 607 } 608 609 static int 610 uvc_v4l2_release(struct file *file) 611 { 612 struct video_device *vdev = video_devdata(file); 613 struct uvc_device *uvc = video_get_drvdata(vdev); 614 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data); 615 struct uvc_video *video = handle->device; 616 617 mutex_lock(&video->mutex); 618 if (handle->is_uvc_app_handle) 619 uvc_v4l2_disable(uvc); 620 mutex_unlock(&video->mutex); 621 622 file->private_data = NULL; 623 v4l2_fh_del(&handle->vfh); 624 v4l2_fh_exit(&handle->vfh); 625 kfree(handle); 626 627 return 0; 628 } 629 630 static int 631 uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma) 632 { 633 struct video_device *vdev = video_devdata(file); 634 struct uvc_device *uvc = video_get_drvdata(vdev); 635 636 return uvcg_queue_mmap(&uvc->video.queue, vma); 637 } 638 639 static __poll_t 640 uvc_v4l2_poll(struct file *file, poll_table *wait) 641 { 642 struct video_device *vdev = video_devdata(file); 643 struct uvc_device *uvc = video_get_drvdata(vdev); 644 645 return uvcg_queue_poll(&uvc->video.queue, file, wait); 646 } 647 648 #ifndef CONFIG_MMU 649 static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file, 650 unsigned long addr, unsigned long len, unsigned long pgoff, 651 unsigned long flags) 652 { 653 struct video_device *vdev = video_devdata(file); 654 struct uvc_device *uvc = video_get_drvdata(vdev); 655 656 return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff); 657 } 658 #endif 659 660 const struct v4l2_file_operations uvc_v4l2_fops = { 661 .owner = THIS_MODULE, 662 .open = uvc_v4l2_open, 663 .release = uvc_v4l2_release, 664 .unlocked_ioctl = video_ioctl2, 665 .mmap = uvc_v4l2_mmap, 666 .poll = uvc_v4l2_poll, 667 #ifndef CONFIG_MMU 668 .get_unmapped_area = uvcg_v4l2_get_unmapped_area, 669 #endif 670 }; 671