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