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