Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'tags/media-next-uvc-20240617-2' of git://git.kernel.org/pub/scm/linux/kernel/git/pinchartl/linux.git into media_stage

uvcvideo fixes and improvements:

- add/fix quirks
- improve timestamp handling
- improve Power Line Frequency handling

Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>

+261 -271
+108 -85
drivers/media/usb/uvc/uvc_ctrl.c
··· 459 459 data[first+1] = min_t(int, abs(value), 0xff); 460 460 } 461 461 462 + static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited = { 463 + .id = V4L2_CID_POWER_LINE_FREQUENCY, 464 + .entity = UVC_GUID_UVC_PROCESSING, 465 + .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 466 + .size = 2, 467 + .offset = 0, 468 + .v4l2_type = V4L2_CTRL_TYPE_MENU, 469 + .data_type = UVC_CTRL_DATA_TYPE_ENUM, 470 + .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 471 + V4L2_CID_POWER_LINE_FREQUENCY_50HZ), 472 + }; 473 + 474 + static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11 = { 475 + .id = V4L2_CID_POWER_LINE_FREQUENCY, 476 + .entity = UVC_GUID_UVC_PROCESSING, 477 + .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 478 + .size = 2, 479 + .offset = 0, 480 + .v4l2_type = V4L2_CTRL_TYPE_MENU, 481 + .data_type = UVC_CTRL_DATA_TYPE_ENUM, 482 + .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 483 + V4L2_CID_POWER_LINE_FREQUENCY_DISABLED), 484 + }; 485 + 486 + static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc15 = { 487 + .id = V4L2_CID_POWER_LINE_FREQUENCY, 488 + .entity = UVC_GUID_UVC_PROCESSING, 489 + .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 490 + .size = 2, 491 + .offset = 0, 492 + .v4l2_type = V4L2_CTRL_TYPE_MENU, 493 + .data_type = UVC_CTRL_DATA_TYPE_ENUM, 494 + .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 495 + V4L2_CID_POWER_LINE_FREQUENCY_DISABLED), 496 + }; 497 + 498 + static const struct uvc_control_mapping *uvc_ctrl_filter_plf_mapping( 499 + struct uvc_video_chain *chain, struct uvc_control *ctrl) 500 + { 501 + const struct uvc_control_mapping *out_mapping = 502 + &uvc_ctrl_power_line_mapping_uvc11; 503 + u8 *buf __free(kfree) = NULL; 504 + u8 init_val; 505 + int ret; 506 + 507 + buf = kmalloc(sizeof(*buf), GFP_KERNEL); 508 + if (!buf) 509 + return NULL; 510 + 511 + /* Save the current PLF value, so we can restore it. */ 512 + ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id, 513 + chain->dev->intfnum, ctrl->info.selector, 514 + buf, sizeof(*buf)); 515 + /* If we cannot read the control skip it. */ 516 + if (ret) 517 + return NULL; 518 + init_val = *buf; 519 + 520 + /* If PLF value cannot be set to off, it is limited. */ 521 + *buf = V4L2_CID_POWER_LINE_FREQUENCY_DISABLED; 522 + ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id, 523 + chain->dev->intfnum, ctrl->info.selector, 524 + buf, sizeof(*buf)); 525 + if (ret) 526 + return &uvc_ctrl_power_line_mapping_limited; 527 + 528 + /* UVC 1.1 does not define auto, we can exit. */ 529 + if (chain->dev->uvc_version < 0x150) 530 + goto end; 531 + 532 + /* Check if the device supports auto. */ 533 + *buf = V4L2_CID_POWER_LINE_FREQUENCY_AUTO; 534 + ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id, 535 + chain->dev->intfnum, ctrl->info.selector, 536 + buf, sizeof(*buf)); 537 + if (!ret) 538 + out_mapping = &uvc_ctrl_power_line_mapping_uvc15; 539 + 540 + end: 541 + /* Restore initial value and add mapping. */ 542 + *buf = init_val; 543 + uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id, 544 + chain->dev->intfnum, ctrl->info.selector, 545 + buf, sizeof(*buf)); 546 + 547 + return out_mapping; 548 + } 549 + 462 550 static const struct uvc_control_mapping uvc_ctrl_mappings[] = { 463 551 { 464 552 .id = V4L2_CID_BRIGHTNESS, ··· 836 748 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, 837 749 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, 838 750 }, 839 - }; 840 - 841 - const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited = { 842 - .id = V4L2_CID_POWER_LINE_FREQUENCY, 843 - .entity = UVC_GUID_UVC_PROCESSING, 844 - .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 845 - .size = 2, 846 - .offset = 0, 847 - .v4l2_type = V4L2_CTRL_TYPE_MENU, 848 - .data_type = UVC_CTRL_DATA_TYPE_ENUM, 849 - .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 850 - V4L2_CID_POWER_LINE_FREQUENCY_50HZ), 851 - }; 852 - 853 - const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11 = { 854 - .id = V4L2_CID_POWER_LINE_FREQUENCY, 855 - .entity = UVC_GUID_UVC_PROCESSING, 856 - .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 857 - .size = 2, 858 - .offset = 0, 859 - .v4l2_type = V4L2_CTRL_TYPE_MENU, 860 - .data_type = UVC_CTRL_DATA_TYPE_ENUM, 861 - .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 862 - V4L2_CID_POWER_LINE_FREQUENCY_DISABLED), 863 - }; 864 - 865 - static const struct uvc_control_mapping *uvc_ctrl_mappings_uvc11[] = { 866 - &uvc_ctrl_power_line_mapping_uvc11, 867 - NULL, /* Sentinel */ 868 - }; 869 - 870 - static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc15 = { 871 - .id = V4L2_CID_POWER_LINE_FREQUENCY, 872 - .entity = UVC_GUID_UVC_PROCESSING, 873 - .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 874 - .size = 2, 875 - .offset = 0, 876 - .v4l2_type = V4L2_CTRL_TYPE_MENU, 877 - .data_type = UVC_CTRL_DATA_TYPE_ENUM, 878 - .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 879 - V4L2_CID_POWER_LINE_FREQUENCY_DISABLED), 880 - }; 881 - 882 - static const struct uvc_control_mapping *uvc_ctrl_mappings_uvc15[] = { 883 - &uvc_ctrl_power_line_mapping_uvc15, 884 - NULL, /* Sentinel */ 751 + { 752 + .entity = UVC_GUID_UVC_PROCESSING, 753 + .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 754 + .filter_mapping = uvc_ctrl_filter_plf_mapping, 755 + }, 885 756 }; 886 757 887 758 /* ------------------------------------------------------------------------ ··· 2078 2031 else 2079 2032 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, 2080 2033 dev->intfnum, info->selector, data, 1); 2081 - if (!ret) 2034 + 2035 + if (!ret) { 2036 + info->flags &= ~(UVC_CTRL_FLAG_GET_CUR | 2037 + UVC_CTRL_FLAG_SET_CUR | 2038 + UVC_CTRL_FLAG_AUTO_UPDATE | 2039 + UVC_CTRL_FLAG_ASYNCHRONOUS); 2040 + 2082 2041 info->flags |= (data[0] & UVC_CONTROL_CAP_GET ? 2083 2042 UVC_CTRL_FLAG_GET_CUR : 0) 2084 2043 | (data[0] & UVC_CONTROL_CAP_SET ? ··· 2093 2040 UVC_CTRL_FLAG_AUTO_UPDATE : 0) 2094 2041 | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ? 2095 2042 UVC_CTRL_FLAG_ASYNCHRONOUS : 0); 2043 + } 2096 2044 2097 2045 kfree(data); 2098 2046 return ret; ··· 2645 2591 static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain, 2646 2592 struct uvc_control *ctrl) 2647 2593 { 2648 - const struct uvc_control_mapping **mappings; 2649 2594 unsigned int i; 2650 2595 2651 2596 /* ··· 2676 2623 if (!ctrl->initialized) 2677 2624 return; 2678 2625 2679 - /* 2680 - * First check if the device provides a custom mapping for this control, 2681 - * used to override standard mappings for non-conformant devices. Don't 2682 - * process standard mappings if a custom mapping is found. This 2683 - * mechanism doesn't support combining standard and custom mappings for 2684 - * a single control. 2685 - */ 2686 - if (chain->dev->info->mappings) { 2687 - bool custom = false; 2688 - 2689 - for (i = 0; chain->dev->info->mappings[i]; ++i) { 2690 - const struct uvc_control_mapping *mapping = 2691 - chain->dev->info->mappings[i]; 2692 - 2693 - if (uvc_entity_match_guid(ctrl->entity, mapping->entity) && 2694 - ctrl->info.selector == mapping->selector) { 2695 - __uvc_ctrl_add_mapping(chain, ctrl, mapping); 2696 - custom = true; 2697 - } 2698 - } 2699 - 2700 - if (custom) 2701 - return; 2702 - } 2703 - 2704 - /* Process common mappings next. */ 2626 + /* Process common mappings. */ 2705 2627 for (i = 0; i < ARRAY_SIZE(uvc_ctrl_mappings); ++i) { 2706 2628 const struct uvc_control_mapping *mapping = &uvc_ctrl_mappings[i]; 2707 2629 2708 - if (uvc_entity_match_guid(ctrl->entity, mapping->entity) && 2709 - ctrl->info.selector == mapping->selector) 2710 - __uvc_ctrl_add_mapping(chain, ctrl, mapping); 2711 - } 2712 - 2713 - /* Finally process version-specific mappings. */ 2714 - mappings = chain->dev->uvc_version < 0x0150 2715 - ? uvc_ctrl_mappings_uvc11 : uvc_ctrl_mappings_uvc15; 2716 - 2717 - for (i = 0; mappings[i]; ++i) { 2718 - const struct uvc_control_mapping *mapping = mappings[i]; 2630 + /* Let the device provide a custom mapping. */ 2631 + if (mapping->filter_mapping) { 2632 + mapping = mapping->filter_mapping(chain, ctrl); 2633 + if (!mapping) 2634 + continue; 2635 + } 2719 2636 2720 2637 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) && 2721 2638 ctrl->info.selector == mapping->selector)
+30 -130
drivers/media/usb/uvc/uvc_driver.c
··· 687 687 goto error; 688 688 } 689 689 690 - size = nformats * sizeof(*format) + nframes * sizeof(*frame) 690 + /* 691 + * Allocate memory for the formats, the frames and the intervals, 692 + * plus any required padding to guarantee that everything has the 693 + * correct alignment. 694 + */ 695 + size = nformats * sizeof(*format); 696 + size = ALIGN(size, __alignof__(*frame)) + nframes * sizeof(*frame); 697 + size = ALIGN(size, __alignof__(*interval)) 691 698 + nintervals * sizeof(*interval); 699 + 692 700 format = kzalloc(size, GFP_KERNEL); 693 - if (format == NULL) { 701 + if (!format) { 694 702 ret = -ENOMEM; 695 703 goto error; 696 704 } 697 705 698 - frame = (struct uvc_frame *)&format[nformats]; 699 - interval = (u32 *)&frame[nframes]; 706 + frame = (void *)format + nformats * sizeof(*format); 707 + frame = PTR_ALIGN(frame, __alignof__(*frame)); 708 + interval = (void *)frame + nframes * sizeof(*frame); 709 + interval = PTR_ALIGN(interval, __alignof__(*interval)); 700 710 701 711 streaming->formats = format; 702 712 streaming->nformats = 0; ··· 2400 2390 * Driver initialization and cleanup 2401 2391 */ 2402 2392 2403 - static const struct uvc_device_info uvc_ctrl_power_line_limited = { 2404 - .mappings = (const struct uvc_control_mapping *[]) { 2405 - &uvc_ctrl_power_line_mapping_limited, 2406 - NULL, /* Sentinel */ 2407 - }, 2408 - }; 2409 - 2410 - static const struct uvc_device_info uvc_ctrl_power_line_uvc11 = { 2411 - .mappings = (const struct uvc_control_mapping *[]) { 2412 - &uvc_ctrl_power_line_mapping_uvc11, 2413 - NULL, /* Sentinel */ 2414 - }, 2415 - }; 2416 - 2417 2393 static const struct uvc_device_info uvc_quirk_probe_minmax = { 2418 2394 .quirks = UVC_QUIRK_PROBE_MINMAX, 2419 2395 }; ··· 2430 2434 * though they are compliant. 2431 2435 */ 2432 2436 static const struct usb_device_id uvc_ids[] = { 2433 - /* Quanta USB2.0 HD UVC Webcam */ 2437 + /* Quanta ACER HD User Facing */ 2434 2438 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2435 2439 | USB_DEVICE_ID_MATCH_INT_INFO, 2436 2440 .idVendor = 0x0408, 2437 - .idProduct = 0x3090, 2438 - .bInterfaceClass = USB_CLASS_VIDEO, 2439 - .bInterfaceSubClass = 1, 2440 - .bInterfaceProtocol = 0, 2441 - .driver_info = (kernel_ulong_t)&uvc_ctrl_power_line_limited }, 2442 - /* Quanta USB2.0 HD UVC Webcam */ 2443 - { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2444 - | USB_DEVICE_ID_MATCH_INT_INFO, 2445 - .idVendor = 0x0408, 2446 - .idProduct = 0x4030, 2447 - .bInterfaceClass = USB_CLASS_VIDEO, 2448 - .bInterfaceSubClass = 1, 2449 - .bInterfaceProtocol = 0, 2450 - .driver_info = (kernel_ulong_t)&uvc_ctrl_power_line_limited }, 2451 - /* Quanta USB2.0 HD UVC Webcam */ 2452 - { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2453 - | USB_DEVICE_ID_MATCH_INT_INFO, 2454 - .idVendor = 0x0408, 2455 - .idProduct = 0x4034, 2441 + .idProduct = 0x4035, 2456 2442 .bInterfaceClass = USB_CLASS_VIDEO, 2457 2443 .bInterfaceSubClass = 1, 2458 2444 .bInterfaceProtocol = UVC_PC_PROTOCOL_15, 2459 - .driver_info = (kernel_ulong_t)&uvc_ctrl_power_line_limited }, 2445 + .driver_info = (kernel_ulong_t)&(const struct uvc_device_info){ 2446 + .uvc_version = 0x010a, 2447 + } }, 2460 2448 /* LogiLink Wireless Webcam */ 2461 2449 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2462 2450 | USB_DEVICE_ID_MATCH_INT_INFO, ··· 2560 2580 .bInterfaceClass = USB_CLASS_VIDEO, 2561 2581 .bInterfaceSubClass = 1, 2562 2582 .bInterfaceProtocol = 0, 2563 - .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_RESTORE_CTRLS_ON_INIT) }, 2583 + .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_RESTORE_CTRLS_ON_INIT 2584 + | UVC_QUIRK_INVALID_DEVICE_SOF) }, 2585 + /* Logitech HD Pro Webcam C922 */ 2586 + { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2587 + | USB_DEVICE_ID_MATCH_INT_INFO, 2588 + .idVendor = 0x046d, 2589 + .idProduct = 0x085c, 2590 + .bInterfaceClass = USB_CLASS_VIDEO, 2591 + .bInterfaceSubClass = 1, 2592 + .bInterfaceProtocol = 0, 2593 + .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_INVALID_DEVICE_SOF) }, 2564 2594 /* Logitech Rally Bar Huddle */ 2565 2595 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2566 2596 | USB_DEVICE_ID_MATCH_INT_INFO, ··· 2607 2617 .bInterfaceSubClass = 1, 2608 2618 .bInterfaceProtocol = 0, 2609 2619 .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_RESTRICT_FRAME_RATE) }, 2610 - /* Chicony EasyCamera */ 2611 - { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2612 - | USB_DEVICE_ID_MATCH_INT_INFO, 2613 - .idVendor = 0x04f2, 2614 - .idProduct = 0xb5eb, 2615 - .bInterfaceClass = USB_CLASS_VIDEO, 2616 - .bInterfaceSubClass = 1, 2617 - .bInterfaceProtocol = 0, 2618 - .driver_info = (kernel_ulong_t)&uvc_ctrl_power_line_limited }, 2619 - /* Chicony Electronics Co., Ltd Integrated Camera */ 2620 - { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2621 - | USB_DEVICE_ID_MATCH_INT_INFO, 2622 - .idVendor = 0x04f2, 2623 - .idProduct = 0xb67c, 2624 - .bInterfaceClass = USB_CLASS_VIDEO, 2625 - .bInterfaceSubClass = 1, 2626 - .bInterfaceProtocol = UVC_PC_PROTOCOL_15, 2627 - .driver_info = (kernel_ulong_t)&uvc_ctrl_power_line_uvc11 }, 2628 - /* Chicony EasyCamera */ 2629 - { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2630 - | USB_DEVICE_ID_MATCH_INT_INFO, 2631 - .idVendor = 0x04f2, 2632 - .idProduct = 0xb6ba, 2633 - .bInterfaceClass = USB_CLASS_VIDEO, 2634 - .bInterfaceSubClass = 1, 2635 - .bInterfaceProtocol = 0, 2636 - .driver_info = (kernel_ulong_t)&uvc_ctrl_power_line_limited }, 2637 - /* Chicony EasyCamera */ 2638 - { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2639 - | USB_DEVICE_ID_MATCH_INT_INFO, 2640 - .idVendor = 0x04f2, 2641 - .idProduct = 0xb746, 2642 - .bInterfaceClass = USB_CLASS_VIDEO, 2643 - .bInterfaceSubClass = 1, 2644 - .bInterfaceProtocol = 0, 2645 - .driver_info = (kernel_ulong_t)&uvc_ctrl_power_line_limited }, 2646 2620 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */ 2647 2621 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2648 2622 | USB_DEVICE_ID_MATCH_INT_INFO, ··· 2991 3037 .bInterfaceSubClass = 1, 2992 3038 .bInterfaceProtocol = 0, 2993 3039 .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_FORCE_BPP) }, 2994 - /* SunplusIT Inc HD Camera */ 2995 - { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2996 - | USB_DEVICE_ID_MATCH_INT_INFO, 2997 - .idVendor = 0x2b7e, 2998 - .idProduct = 0xb752, 2999 - .bInterfaceClass = USB_CLASS_VIDEO, 3000 - .bInterfaceSubClass = 1, 3001 - .bInterfaceProtocol = UVC_PC_PROTOCOL_15, 3002 - .driver_info = (kernel_ulong_t)&uvc_ctrl_power_line_uvc11 }, 3003 3040 /* Insta360 Link */ 3004 3041 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 3005 3042 | USB_DEVICE_ID_MATCH_INT_INFO, ··· 3000 3055 .bInterfaceSubClass = 1, 3001 3056 .bInterfaceProtocol = 0, 3002 3057 .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_DISABLE_AUTOSUSPEND) }, 3003 - /* Lenovo Integrated Camera */ 3004 - { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 3005 - | USB_DEVICE_ID_MATCH_INT_INFO, 3006 - .idVendor = 0x30c9, 3007 - .idProduct = 0x0093, 3008 - .bInterfaceClass = USB_CLASS_VIDEO, 3009 - .bInterfaceSubClass = 1, 3010 - .bInterfaceProtocol = UVC_PC_PROTOCOL_15, 3011 - .driver_info = (kernel_ulong_t)&uvc_ctrl_power_line_uvc11 }, 3012 - /* Sonix Technology USB 2.0 Camera */ 3013 - { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 3014 - | USB_DEVICE_ID_MATCH_INT_INFO, 3015 - .idVendor = 0x3277, 3016 - .idProduct = 0x0072, 3017 - .bInterfaceClass = USB_CLASS_VIDEO, 3018 - .bInterfaceSubClass = 1, 3019 - .bInterfaceProtocol = 0, 3020 - .driver_info = (kernel_ulong_t)&uvc_ctrl_power_line_limited }, 3021 - /* Shine-Optics Integrated Camera */ 3022 - { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 3023 - | USB_DEVICE_ID_MATCH_INT_INFO, 3024 - .idVendor = 0x3277, 3025 - .idProduct = 0x009e, 3026 - .bInterfaceClass = USB_CLASS_VIDEO, 3027 - .bInterfaceSubClass = 1, 3028 - .bInterfaceProtocol = UVC_PC_PROTOCOL_15, 3029 - .driver_info = (kernel_ulong_t)&uvc_ctrl_power_line_uvc11 }, 3030 - /* Acer EasyCamera */ 3031 - { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 3032 - | USB_DEVICE_ID_MATCH_INT_INFO, 3033 - .idVendor = 0x5986, 3034 - .idProduct = 0x1172, 3035 - .bInterfaceClass = USB_CLASS_VIDEO, 3036 - .bInterfaceSubClass = 1, 3037 - .bInterfaceProtocol = 0, 3038 - .driver_info = (kernel_ulong_t)&uvc_ctrl_power_line_limited }, 3039 - /* Acer EasyCamera */ 3040 - { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 3041 - | USB_DEVICE_ID_MATCH_INT_INFO, 3042 - .idVendor = 0x5986, 3043 - .idProduct = 0x1180, 3044 - .bInterfaceClass = USB_CLASS_VIDEO, 3045 - .bInterfaceSubClass = 1, 3046 - .bInterfaceProtocol = 0, 3047 - .driver_info = (kernel_ulong_t)&uvc_ctrl_power_line_limited }, 3048 3058 /* Intel D410/ASR depth camera */ 3049 3059 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 3050 3060 | USB_DEVICE_ID_MATCH_INT_INFO,
+116 -53
drivers/media/usb/uvc/uvc_video.c
··· 214 214 * Compute a bandwidth estimation by multiplying the frame 215 215 * size by the number of video frames per second, divide the 216 216 * result by the number of USB frames (or micro-frames for 217 - * high-speed devices) per second and add the UVC header size 218 - * (assumed to be 12 bytes long). 217 + * high- and super-speed devices) per second and add the UVC 218 + * header size (assumed to be 12 bytes long). 219 219 */ 220 220 bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp; 221 221 bandwidth *= 10000000 / interval + 1; 222 222 bandwidth /= 1000; 223 - if (stream->dev->udev->speed == USB_SPEED_HIGH) 223 + if (stream->dev->udev->speed >= USB_SPEED_HIGH) 224 224 bandwidth /= 8; 225 225 bandwidth += 12; 226 226 ··· 466 466 return ktime_get_real(); 467 467 } 468 468 469 + static void uvc_video_clock_add_sample(struct uvc_clock *clock, 470 + const struct uvc_clock_sample *sample) 471 + { 472 + unsigned long flags; 473 + 474 + /* 475 + * If we write new data on the position where we had the last 476 + * overflow, remove the overflow pointer. There is no SOF overflow 477 + * in the whole circular buffer. 478 + */ 479 + if (clock->head == clock->last_sof_overflow) 480 + clock->last_sof_overflow = -1; 481 + 482 + spin_lock_irqsave(&clock->lock, flags); 483 + 484 + if (clock->count > 0 && clock->last_sof > sample->dev_sof) { 485 + /* 486 + * Remove data from the circular buffer that is older than the 487 + * last SOF overflow. We only support one SOF overflow per 488 + * circular buffer. 489 + */ 490 + if (clock->last_sof_overflow != -1) 491 + clock->count = (clock->head - clock->last_sof_overflow 492 + + clock->size) % clock->size; 493 + clock->last_sof_overflow = clock->head; 494 + } 495 + 496 + /* Add sample. */ 497 + clock->samples[clock->head] = *sample; 498 + clock->head = (clock->head + 1) % clock->size; 499 + clock->count = min(clock->count + 1, clock->size); 500 + 501 + spin_unlock_irqrestore(&clock->lock, flags); 502 + } 503 + 469 504 static void 470 505 uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, 471 506 const u8 *data, int len) 472 507 { 473 - struct uvc_clock_sample *sample; 508 + struct uvc_clock_sample sample; 474 509 unsigned int header_size; 475 510 bool has_pts = false; 476 511 bool has_scr = false; 477 - unsigned long flags; 478 - ktime_t time; 479 - u16 host_sof; 480 - u16 dev_sof; 481 512 482 513 switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) { 483 514 case UVC_STREAM_PTS | UVC_STREAM_SCR: ··· 553 522 * all the data packets of the same frame contains the same SOF. In that 554 523 * case only the first one will match the host_sof. 555 524 */ 556 - dev_sof = get_unaligned_le16(&data[header_size - 2]); 557 - if (dev_sof == stream->clock.last_sof) 525 + sample.dev_sof = get_unaligned_le16(&data[header_size - 2]); 526 + if (sample.dev_sof == stream->clock.last_sof) 558 527 return; 559 528 560 - stream->clock.last_sof = dev_sof; 529 + sample.dev_stc = get_unaligned_le32(&data[header_size - 6]); 561 530 562 - host_sof = usb_get_current_frame_number(stream->dev->udev); 563 - time = uvc_video_get_time(); 531 + /* 532 + * STC (Source Time Clock) is the clock used by the camera. The UVC 1.5 533 + * standard states that it "must be captured when the first video data 534 + * of a video frame is put on the USB bus". This is generally understood 535 + * as requiring devices to clear the payload header's SCR bit before 536 + * the first packet containing video data. 537 + * 538 + * Most vendors follow that interpretation, but some (namely SunplusIT 539 + * on some devices) always set the `UVC_STREAM_SCR` bit, fill the SCR 540 + * field with 0's,and expect that the driver only processes the SCR if 541 + * there is data in the packet. 542 + * 543 + * Ignore all the hardware timestamp information if we haven't received 544 + * any data for this frame yet, the packet contains no data, and both 545 + * STC and SOF are zero. This heuristics should be safe on compliant 546 + * devices. This should be safe with compliant devices, as in the very 547 + * unlikely case where a UVC 1.1 device would send timing information 548 + * only before the first packet containing data, and both STC and SOF 549 + * happen to be zero for a particular frame, we would only miss one 550 + * clock sample from many and the clock recovery algorithm wouldn't 551 + * suffer from this condition. 552 + */ 553 + if (buf && buf->bytesused == 0 && len == header_size && 554 + sample.dev_stc == 0 && sample.dev_sof == 0) 555 + return; 556 + 557 + sample.host_sof = usb_get_current_frame_number(stream->dev->udev); 558 + 559 + /* 560 + * On some devices, like the Logitech C922, the device SOF does not run 561 + * at a stable rate of 1kHz. For those devices use the host SOF instead. 562 + * In the tests performed so far, this improves the timestamp precision. 563 + * This is probably explained by a small packet handling jitter from the 564 + * host, but the exact reason hasn't been fully determined. 565 + */ 566 + if (stream->dev->quirks & UVC_QUIRK_INVALID_DEVICE_SOF) 567 + sample.dev_sof = sample.host_sof; 568 + 569 + sample.host_time = uvc_video_get_time(); 564 570 565 571 /* 566 572 * The UVC specification allows device implementations that can't obtain ··· 620 552 * the 8 LSBs of the delta are kept. 621 553 */ 622 554 if (stream->clock.sof_offset == (u16)-1) { 623 - u16 delta_sof = (host_sof - dev_sof) & 255; 555 + u16 delta_sof = (sample.host_sof - sample.dev_sof) & 255; 624 556 if (delta_sof >= 10) 625 557 stream->clock.sof_offset = delta_sof; 626 558 else 627 559 stream->clock.sof_offset = 0; 628 560 } 629 561 630 - dev_sof = (dev_sof + stream->clock.sof_offset) & 2047; 631 - 632 - spin_lock_irqsave(&stream->clock.lock, flags); 633 - 634 - sample = &stream->clock.samples[stream->clock.head]; 635 - sample->dev_stc = get_unaligned_le32(&data[header_size - 6]); 636 - sample->dev_sof = dev_sof; 637 - sample->host_sof = host_sof; 638 - sample->host_time = time; 639 - 640 - /* Update the sliding window head and count. */ 641 - stream->clock.head = (stream->clock.head + 1) % stream->clock.size; 642 - 643 - if (stream->clock.count < stream->clock.size) 644 - stream->clock.count++; 645 - 646 - spin_unlock_irqrestore(&stream->clock.lock, flags); 562 + sample.dev_sof = (sample.dev_sof + stream->clock.sof_offset) & 2047; 563 + uvc_video_clock_add_sample(&stream->clock, &sample); 564 + stream->clock.last_sof = sample.dev_sof; 647 565 } 648 566 649 - static void uvc_video_clock_reset(struct uvc_streaming *stream) 567 + static void uvc_video_clock_reset(struct uvc_clock *clock) 650 568 { 651 - struct uvc_clock *clock = &stream->clock; 652 - 653 569 clock->head = 0; 654 570 clock->count = 0; 655 571 clock->last_sof = -1; 572 + clock->last_sof_overflow = -1; 656 573 clock->sof_offset = -1; 657 574 } 658 575 659 - static int uvc_video_clock_init(struct uvc_streaming *stream) 576 + static int uvc_video_clock_init(struct uvc_clock *clock) 660 577 { 661 - struct uvc_clock *clock = &stream->clock; 662 - 663 578 spin_lock_init(&clock->lock); 664 579 clock->size = 32; 665 580 ··· 651 600 if (clock->samples == NULL) 652 601 return -ENOMEM; 653 602 654 - uvc_video_clock_reset(stream); 603 + uvc_video_clock_reset(clock); 655 604 656 605 return 0; 657 606 } 658 607 659 - static void uvc_video_clock_cleanup(struct uvc_streaming *stream) 608 + static void uvc_video_clock_cleanup(struct uvc_clock *clock) 660 609 { 661 - kfree(stream->clock.samples); 662 - stream->clock.samples = NULL; 610 + kfree(clock->samples); 611 + clock->samples = NULL; 663 612 } 664 613 665 614 /* ··· 760 709 unsigned long flags; 761 710 u64 timestamp; 762 711 u32 delta_stc; 763 - u32 y1, y2; 712 + u32 y1; 764 713 u32 x1, x2; 765 714 u32 mean; 766 715 u32 sof; 767 - u64 y; 716 + u64 y, y2; 768 717 769 718 if (!uvc_hw_timestamps_param) 770 719 return; ··· 779 728 780 729 spin_lock_irqsave(&clock->lock, flags); 781 730 782 - if (clock->count < clock->size) 731 + if (clock->count < 2) 783 732 goto done; 784 733 785 - first = &clock->samples[clock->head]; 786 - last = &clock->samples[(clock->head - 1) % clock->size]; 734 + first = &clock->samples[(clock->head - clock->count + clock->size) % clock->size]; 735 + last = &clock->samples[(clock->head - 1 + clock->size) % clock->size]; 787 736 788 737 /* First step, PTS to SOF conversion. */ 789 738 delta_stc = buf->pts - (1UL << 31); ··· 797 746 if (y2 < y1) 798 747 y2 += 2048 << 16; 799 748 749 + /* 750 + * Have at least 1/4 of a second of timestamps before we 751 + * try to do any calculation. Otherwise we do not have enough 752 + * precision. This value was determined by running Android CTS 753 + * on different devices. 754 + * 755 + * dev_sof runs at 1KHz, and we have a fixed point precision of 756 + * 16 bits. 757 + */ 758 + if ((y2 - y1) < ((1000 / 4) << 16)) 759 + goto done; 760 + 800 761 y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2 801 762 - (u64)y2 * (u64)x1; 802 763 y = div_u64(y, x2 - x1); ··· 816 753 sof = y; 817 754 818 755 uvc_dbg(stream->dev, CLOCK, 819 - "%s: PTS %u y %llu.%06llu SOF %u.%06llu (x1 %u x2 %u y1 %u y2 %u SOF offset %u)\n", 756 + "%s: PTS %u y %llu.%06llu SOF %u.%06llu (x1 %u x2 %u y1 %u y2 %llu SOF offset %u)\n", 820 757 stream->dev->name, buf->pts, 821 758 y >> 16, div_u64((y & 0xffff) * 1000000, 65536), 822 759 sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536), ··· 831 768 goto done; 832 769 833 770 y1 = NSEC_PER_SEC; 834 - y2 = (u32)ktime_to_ns(ktime_sub(last->host_time, first->host_time)) + y1; 771 + y2 = ktime_to_ns(ktime_sub(last->host_time, first->host_time)) + y1; 835 772 836 773 /* 837 774 * Interpolated and host SOF timestamps can wrap around at slightly ··· 852 789 timestamp = ktime_to_ns(first->host_time) + y - y1; 853 790 854 791 uvc_dbg(stream->dev, CLOCK, 855 - "%s: SOF %u.%06llu y %llu ts %llu buf ts %llu (x1 %u/%u/%u x2 %u/%u/%u y1 %u y2 %u)\n", 792 + "%s: SOF %u.%06llu y %llu ts %llu buf ts %llu (x1 %u/%u/%u x2 %u/%u/%u y1 %u y2 %llu)\n", 856 793 stream->dev->name, 857 794 sof >> 16, div_u64(((u64)sof & 0xffff) * 1000000LLU, 65536), 858 795 y, timestamp, vbuf->vb2_buf.timestamp, ··· 2134 2071 2135 2072 stream->frozen = 0; 2136 2073 2137 - uvc_video_clock_reset(stream); 2074 + uvc_video_clock_reset(&stream->clock); 2138 2075 2139 2076 if (!uvc_queue_streaming(&stream->queue)) 2140 2077 return 0; ··· 2283 2220 { 2284 2221 int ret; 2285 2222 2286 - ret = uvc_video_clock_init(stream); 2223 + ret = uvc_video_clock_init(&stream->clock); 2287 2224 if (ret < 0) 2288 2225 return ret; 2289 2226 ··· 2301 2238 error_video: 2302 2239 usb_set_interface(stream->dev->udev, stream->intfnum, 0); 2303 2240 error_commit: 2304 - uvc_video_clock_cleanup(stream); 2241 + uvc_video_clock_cleanup(&stream->clock); 2305 2242 2306 2243 return ret; 2307 2244 } ··· 2329 2266 usb_clear_halt(stream->dev->udev, pipe); 2330 2267 } 2331 2268 2332 - uvc_video_clock_cleanup(stream); 2269 + uvc_video_clock_cleanup(&stream->clock); 2333 2270 }
+7 -3
drivers/media/usb/uvc/uvcvideo.h
··· 75 75 #define UVC_QUIRK_WAKE_AUTOSUSPEND 0x00002000 76 76 #define UVC_QUIRK_NO_RESET_RESUME 0x00004000 77 77 #define UVC_QUIRK_DISABLE_AUTOSUSPEND 0x00008000 78 + #define UVC_QUIRK_INVALID_DEVICE_SOF 0x00010000 78 79 79 80 /* Format flags */ 80 81 #define UVC_FMT_FLAG_COMPRESSED 0x00000001 ··· 87 86 88 87 struct gpio_desc; 89 88 struct sg_table; 89 + struct uvc_control; 90 90 struct uvc_device; 91 + struct uvc_video_chain; 91 92 92 93 /* 93 94 * TODO: Put the most frequently accessed fields at the beginning of ··· 128 125 s32 master_manual; 129 126 u32 slave_ids[2]; 130 127 128 + const struct uvc_control_mapping *(*filter_mapping) 129 + (struct uvc_video_chain *chain, 130 + struct uvc_control *ctrl); 131 131 s32 (*get)(struct uvc_control_mapping *mapping, u8 query, 132 132 const u8 *data); 133 133 void (*set)(struct uvc_control_mapping *mapping, s32 value, ··· 506 500 unsigned int head; 507 501 unsigned int count; 508 502 unsigned int size; 503 + unsigned int last_sof_overflow; 509 504 510 505 u16 last_sof; 511 506 u16 sof_offset; ··· 531 524 u32 quirks; 532 525 u32 meta_format; 533 526 u16 uvc_version; 534 - const struct uvc_control_mapping **mappings; 535 527 }; 536 528 537 529 struct uvc_status_streaming { ··· 756 750 void uvc_status_stop(struct uvc_device *dev); 757 751 758 752 /* Controls */ 759 - extern const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited; 760 - extern const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11; 761 753 extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops; 762 754 763 755 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,