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 fmt->fmt.pix.width = uframe->frame.w_width; 268 fmt->fmt.pix.height = uframe->frame.w_height; 269 fmt->fmt.pix.field = V4L2_FIELD_NONE; 270 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(uformat, uframe); 271 fmt->fmt.pix.sizeimage = uvc_get_frame_size(uformat, uframe); 272 fmtdesc = to_uvc_format(uformat); 273 if (IS_ERR(fmtdesc)) 274 return PTR_ERR(fmtdesc); 275 fmt->fmt.pix.pixelformat = fmtdesc->fcc; 276 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; 277 fmt->fmt.pix.priv = 0; 278 279 return 0; 280 } 281 282 static int 283 uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt) 284 { 285 struct video_device *vdev = video_devdata(file); 286 struct uvc_device *uvc = video_get_drvdata(vdev); 287 struct uvc_video *video = &uvc->video; 288 int ret; 289 290 ret = uvc_v4l2_try_format(file, fh, fmt); 291 if (ret) 292 return ret; 293 294 video->fcc = fmt->fmt.pix.pixelformat; 295 video->bpp = fmt->fmt.pix.bytesperline * 8 / video->width; 296 video->width = fmt->fmt.pix.width; 297 video->height = fmt->fmt.pix.height; 298 video->imagesize = fmt->fmt.pix.sizeimage; 299 300 return ret; 301 } 302 303 static int 304 uvc_v4l2_enum_frameintervals(struct file *file, void *fh, 305 struct v4l2_frmivalenum *fival) 306 { 307 struct video_device *vdev = video_devdata(file); 308 struct uvc_device *uvc = video_get_drvdata(vdev); 309 struct uvcg_format *uformat = NULL; 310 struct uvcg_frame *uframe = NULL; 311 struct uvcg_frame_ptr *frame; 312 313 uformat = find_format_by_pix(uvc, fival->pixel_format); 314 if (!uformat) 315 return -EINVAL; 316 317 list_for_each_entry(frame, &uformat->frames, entry) { 318 if (frame->frm->frame.w_width == fival->width && 319 frame->frm->frame.w_height == fival->height) { 320 uframe = frame->frm; 321 break; 322 } 323 } 324 if (!uframe) 325 return -EINVAL; 326 327 if (fival->index >= uframe->frame.b_frame_interval_type) 328 return -EINVAL; 329 330 fival->discrete.numerator = 331 uframe->dw_frame_interval[fival->index]; 332 333 /* TODO: handle V4L2_FRMIVAL_TYPE_STEPWISE */ 334 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; 335 fival->discrete.denominator = 10000000; 336 v4l2_simplify_fraction(&fival->discrete.numerator, 337 &fival->discrete.denominator, 8, 333); 338 339 return 0; 340 } 341 342 static int 343 uvc_v4l2_enum_framesizes(struct file *file, void *fh, 344 struct v4l2_frmsizeenum *fsize) 345 { 346 struct video_device *vdev = video_devdata(file); 347 struct uvc_device *uvc = video_get_drvdata(vdev); 348 struct uvcg_format *uformat = NULL; 349 struct uvcg_frame *uframe = NULL; 350 351 uformat = find_format_by_pix(uvc, fsize->pixel_format); 352 if (!uformat) 353 return -EINVAL; 354 355 if (fsize->index >= uformat->num_frames) 356 return -EINVAL; 357 358 uframe = find_frame_by_index(uvc, uformat, fsize->index + 1); 359 if (!uframe) 360 return -EINVAL; 361 362 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 363 fsize->discrete.width = uframe->frame.w_width; 364 fsize->discrete.height = uframe->frame.w_height; 365 366 return 0; 367 } 368 369 static int 370 uvc_v4l2_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f) 371 { 372 struct video_device *vdev = video_devdata(file); 373 struct uvc_device *uvc = video_get_drvdata(vdev); 374 const struct uvc_format_desc *fmtdesc; 375 struct uvcg_format *uformat; 376 377 if (f->index >= uvc->header->num_fmt) 378 return -EINVAL; 379 380 uformat = find_format_by_index(uvc, f->index + 1); 381 if (!uformat) 382 return -EINVAL; 383 384 fmtdesc = to_uvc_format(uformat); 385 if (IS_ERR(fmtdesc)) 386 return PTR_ERR(fmtdesc); 387 388 f->pixelformat = fmtdesc->fcc; 389 390 return 0; 391 } 392 393 static int 394 uvc_v4l2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *b) 395 { 396 struct video_device *vdev = video_devdata(file); 397 struct uvc_device *uvc = video_get_drvdata(vdev); 398 struct uvc_video *video = &uvc->video; 399 400 if (b->type != video->queue.queue.type) 401 return -EINVAL; 402 403 return uvcg_alloc_buffers(&video->queue, b); 404 } 405 406 static int 407 uvc_v4l2_querybuf(struct file *file, void *fh, struct v4l2_buffer *b) 408 { 409 struct video_device *vdev = video_devdata(file); 410 struct uvc_device *uvc = video_get_drvdata(vdev); 411 struct uvc_video *video = &uvc->video; 412 413 return uvcg_query_buffer(&video->queue, b); 414 } 415 416 static int 417 uvc_v4l2_qbuf(struct file *file, void *fh, struct v4l2_buffer *b) 418 { 419 struct video_device *vdev = video_devdata(file); 420 struct uvc_device *uvc = video_get_drvdata(vdev); 421 struct uvc_video *video = &uvc->video; 422 int ret; 423 424 ret = uvcg_queue_buffer(&video->queue, b); 425 if (ret < 0) 426 return ret; 427 428 if (uvc->state == UVC_STATE_STREAMING) 429 queue_work(video->async_wq, &video->pump); 430 431 return ret; 432 } 433 434 static int 435 uvc_v4l2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b) 436 { 437 struct video_device *vdev = video_devdata(file); 438 struct uvc_device *uvc = video_get_drvdata(vdev); 439 struct uvc_video *video = &uvc->video; 440 441 return uvcg_dequeue_buffer(&video->queue, b, file->f_flags & O_NONBLOCK); 442 } 443 444 static int 445 uvc_v4l2_streamon(struct file *file, void *fh, enum v4l2_buf_type type) 446 { 447 struct video_device *vdev = video_devdata(file); 448 struct uvc_device *uvc = video_get_drvdata(vdev); 449 struct uvc_video *video = &uvc->video; 450 int ret; 451 452 if (type != video->queue.queue.type) 453 return -EINVAL; 454 455 /* Enable UVC video. */ 456 ret = uvcg_video_enable(video, 1); 457 if (ret < 0) 458 return ret; 459 460 /* 461 * Complete the alternate setting selection setup phase now that 462 * userspace is ready to provide video frames. 463 */ 464 uvc_function_setup_continue(uvc); 465 uvc->state = UVC_STATE_STREAMING; 466 467 return 0; 468 } 469 470 static int 471 uvc_v4l2_streamoff(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 477 if (type != video->queue.queue.type) 478 return -EINVAL; 479 480 return uvcg_video_enable(video, 0); 481 } 482 483 static int 484 uvc_v4l2_subscribe_event(struct v4l2_fh *fh, 485 const struct v4l2_event_subscription *sub) 486 { 487 struct uvc_device *uvc = video_get_drvdata(fh->vdev); 488 struct uvc_file_handle *handle = to_uvc_file_handle(fh); 489 int ret; 490 491 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST) 492 return -EINVAL; 493 494 if (sub->type == UVC_EVENT_SETUP && uvc->func_connected) 495 return -EBUSY; 496 497 ret = v4l2_event_subscribe(fh, sub, 2, NULL); 498 if (ret < 0) 499 return ret; 500 501 if (sub->type == UVC_EVENT_SETUP) { 502 uvc->func_connected = true; 503 handle->is_uvc_app_handle = true; 504 uvc_function_connect(uvc); 505 } 506 507 return 0; 508 } 509 510 static void uvc_v4l2_disable(struct uvc_device *uvc) 511 { 512 uvc_function_disconnect(uvc); 513 uvcg_video_enable(&uvc->video, 0); 514 uvcg_free_buffers(&uvc->video.queue); 515 uvc->func_connected = false; 516 wake_up_interruptible(&uvc->func_connected_queue); 517 } 518 519 static int 520 uvc_v4l2_unsubscribe_event(struct v4l2_fh *fh, 521 const struct v4l2_event_subscription *sub) 522 { 523 struct uvc_device *uvc = video_get_drvdata(fh->vdev); 524 struct uvc_file_handle *handle = to_uvc_file_handle(fh); 525 int ret; 526 527 ret = v4l2_event_unsubscribe(fh, sub); 528 if (ret < 0) 529 return ret; 530 531 if (sub->type == UVC_EVENT_SETUP && handle->is_uvc_app_handle) { 532 uvc_v4l2_disable(uvc); 533 handle->is_uvc_app_handle = false; 534 } 535 536 return 0; 537 } 538 539 static long 540 uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio, 541 unsigned int cmd, void *arg) 542 { 543 struct video_device *vdev = video_devdata(file); 544 struct uvc_device *uvc = video_get_drvdata(vdev); 545 546 switch (cmd) { 547 case UVCIOC_SEND_RESPONSE: 548 return uvc_send_response(uvc, arg); 549 550 default: 551 return -ENOIOCTLCMD; 552 } 553 } 554 555 const struct v4l2_ioctl_ops uvc_v4l2_ioctl_ops = { 556 .vidioc_querycap = uvc_v4l2_querycap, 557 .vidioc_try_fmt_vid_out = uvc_v4l2_try_format, 558 .vidioc_g_fmt_vid_out = uvc_v4l2_get_format, 559 .vidioc_s_fmt_vid_out = uvc_v4l2_set_format, 560 .vidioc_enum_frameintervals = uvc_v4l2_enum_frameintervals, 561 .vidioc_enum_framesizes = uvc_v4l2_enum_framesizes, 562 .vidioc_enum_fmt_vid_out = uvc_v4l2_enum_format, 563 .vidioc_reqbufs = uvc_v4l2_reqbufs, 564 .vidioc_querybuf = uvc_v4l2_querybuf, 565 .vidioc_qbuf = uvc_v4l2_qbuf, 566 .vidioc_dqbuf = uvc_v4l2_dqbuf, 567 .vidioc_streamon = uvc_v4l2_streamon, 568 .vidioc_streamoff = uvc_v4l2_streamoff, 569 .vidioc_subscribe_event = uvc_v4l2_subscribe_event, 570 .vidioc_unsubscribe_event = uvc_v4l2_unsubscribe_event, 571 .vidioc_default = uvc_v4l2_ioctl_default, 572 }; 573 574 /* -------------------------------------------------------------------------- 575 * V4L2 576 */ 577 578 static int 579 uvc_v4l2_open(struct file *file) 580 { 581 struct video_device *vdev = video_devdata(file); 582 struct uvc_device *uvc = video_get_drvdata(vdev); 583 struct uvc_file_handle *handle; 584 585 handle = kzalloc(sizeof(*handle), GFP_KERNEL); 586 if (handle == NULL) 587 return -ENOMEM; 588 589 v4l2_fh_init(&handle->vfh, vdev); 590 v4l2_fh_add(&handle->vfh); 591 592 handle->device = &uvc->video; 593 file->private_data = &handle->vfh; 594 595 return 0; 596 } 597 598 static int 599 uvc_v4l2_release(struct file *file) 600 { 601 struct video_device *vdev = video_devdata(file); 602 struct uvc_device *uvc = video_get_drvdata(vdev); 603 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data); 604 struct uvc_video *video = handle->device; 605 606 mutex_lock(&video->mutex); 607 if (handle->is_uvc_app_handle) 608 uvc_v4l2_disable(uvc); 609 mutex_unlock(&video->mutex); 610 611 file->private_data = NULL; 612 v4l2_fh_del(&handle->vfh); 613 v4l2_fh_exit(&handle->vfh); 614 kfree(handle); 615 616 return 0; 617 } 618 619 static int 620 uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma) 621 { 622 struct video_device *vdev = video_devdata(file); 623 struct uvc_device *uvc = video_get_drvdata(vdev); 624 625 return uvcg_queue_mmap(&uvc->video.queue, vma); 626 } 627 628 static __poll_t 629 uvc_v4l2_poll(struct file *file, poll_table *wait) 630 { 631 struct video_device *vdev = video_devdata(file); 632 struct uvc_device *uvc = video_get_drvdata(vdev); 633 634 return uvcg_queue_poll(&uvc->video.queue, file, wait); 635 } 636 637 #ifndef CONFIG_MMU 638 static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file, 639 unsigned long addr, unsigned long len, unsigned long pgoff, 640 unsigned long flags) 641 { 642 struct video_device *vdev = video_devdata(file); 643 struct uvc_device *uvc = video_get_drvdata(vdev); 644 645 return uvcg_queue_get_unmapped_area(&uvc->video.queue, pgoff); 646 } 647 #endif 648 649 const struct v4l2_file_operations uvc_v4l2_fops = { 650 .owner = THIS_MODULE, 651 .open = uvc_v4l2_open, 652 .release = uvc_v4l2_release, 653 .unlocked_ioctl = video_ioctl2, 654 .mmap = uvc_v4l2_mmap, 655 .poll = uvc_v4l2_poll, 656 #ifndef CONFIG_MMU 657 .get_unmapped_area = uvcg_v4l2_get_unmapped_area, 658 #endif 659 }; 660 661