sys/bus/u4b/uvc/uvc_ctrls.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 | /* * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2024 Dell Inc. * * Alvin Chen <weike_chen@dell.com, vico.chern@qq.com> * Zhichao Li <Zhichao1.Li@Dell.com> * Pillar Liang <Pillar.Liang@Dellteam.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * UVC spec:https://www.usb.org/sites/default/files/USB_Video_Class_1_5.zip */ #include <sys/cdefs.h> #include <sys/param.h> #include <sys/kernel.h> #include <sys/systm.h> #include <sys/sysctl.h> #include <sys/proc.h> #include <sys/types.h> #include <sys/bus.h> #include <sys/lock.h> #include <sys/condvar.h> #include <sys/malloc.h> #include <sys/conf.h> #include <sys/fcntl.h> #include <bus/u4b/usb.h> #define USB_DEBUG_VAR uvc_debug #include <bus/u4b/usb_debug.h> #include <bus/u4b/usbdi.h> #include <contrib/v4l/videodev.h> #include <contrib/v4l/videodev2.h> #include "uvc_drv.h" #include "uvc_buf.h" #include "uvc_v4l2.h" #define UVC_LOCK(lkp) lockmgr(lkp, LK_EXCLUSIVE) #define UVC_UNLOCK(lkp) lockmgr(lkp, LK_RELEASE) #define STAILQ_FOREACH_SAFE STAILQ_FOREACH_MUTABLE #define UVC_CTRL_DATA_CURRENT 0 #define UVC_CTRL_DATA_BACKUP 1 #define UVC_CTRL_DATA_MIN 2 #define UVC_CTRL_DATA_MAX 3 #define UVC_CTRL_DATA_RES 4 #define UVC_CTRL_DATA_DEF 5 #define UVC_CTRL_DATA_LAST 6 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define UVC_BITMASK 0x7 #define UVC_BITSHIFT 0x3 #define UVC_VALMASK 0x1 static unsigned int uvc_test_bit(const uint8_t *buf, int b) { return (buf[b >> UVC_BITSHIFT] >> (b & UVC_BITMASK)) & UVC_VALMASK; } static int uvc_ctrl_init_sub_info(struct uvc_ctrl_info *info, struct uvc_ctrl_sub_info *sub_info) { int ret = 0; enum v4l2_ctrl_type type = V4L2_CTRL_TYPE_INTEGER; uint8_t bit_offset = 0; uint8_t bit_size = 0; uint8_t name[32] = { 0 }; uint32_t superior_id = 0; uint32_t superior_manual = 0; uint32_t inferior_ids[2] = { 0 }; uint32_t id = sub_info->v4l2_id; if (info->topo_type == UVC_TOPO_TYPE_PROCESSING_UNIT) { switch (id) { case V4L2_CID_BRIGHTNESS: ksnprintf(name, sizeof(name), "Brightness"); bit_size = 16; break; case V4L2_CID_CONTRAST: ksnprintf(name, sizeof(name), "Contrast"); bit_size = 16; break; case V4L2_CID_HUE: ksnprintf(name, sizeof(name), "Hue"); bit_size = 16; superior_id = V4L2_CID_HUE_AUTO; break; case V4L2_CID_SATURATION: ksnprintf(name, sizeof(name), "Saturation"); bit_size = 16; break; case V4L2_CID_SHARPNESS: ksnprintf(name, sizeof(name), "Sharpness"); bit_size = 16; break; case V4L2_CID_GAMMA: ksnprintf(name, sizeof(name), "Gamma"); bit_size = 16; break; case V4L2_CID_BACKLIGHT_COMPENSATION: ksnprintf(name, sizeof(name), "Backlight Compensation"); bit_size = 16; break; case V4L2_CID_GAIN: ksnprintf(name, sizeof(name), "Gain"); bit_size = 16; break; case V4L2_CID_POWER_LINE_FREQUENCY: ksnprintf(name, sizeof(name), "Power Line Frequency"); type = V4L2_CTRL_TYPE_MENU; bit_size = 2; break; case V4L2_CID_HUE_AUTO: ksnprintf(name, sizeof(name), "Hue, Auto"); type = V4L2_CTRL_TYPE_BOOLEAN; bit_size = 1; sub_info->inferior_v4l2_ids[0] = V4L2_CID_HUE; break; case V4L2_CID_AUTO_WHITE_BALANCE: if (info->selector == UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL) { ksnprintf(name, sizeof(name), "White Balance Temperature, Auto"); type = V4L2_CTRL_TYPE_BOOLEAN; bit_size = 1; inferior_ids[0] = V4L2_CID_WHITE_BALANCE_TEMPERATURE; } else if (info->selector == UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL) { ksnprintf(name, sizeof(name), "White Balance Component, Auto"); type = V4L2_CTRL_TYPE_BOOLEAN; bit_size = 1; inferior_ids[0] = V4L2_CID_BLUE_BALANCE; inferior_ids[1] = V4L2_CID_RED_BALANCE; } else { ret = EINVAL; } break; case V4L2_CID_WHITE_BALANCE_TEMPERATURE: ksnprintf(name, sizeof(name), "White Balance Temperature"); bit_size = 16; superior_id = V4L2_CID_AUTO_WHITE_BALANCE; break; case V4L2_CID_BLUE_BALANCE: ksnprintf(name, sizeof(name), "White Balance Blue Component"); bit_size = 16; superior_id = V4L2_CID_AUTO_WHITE_BALANCE; break; case V4L2_CID_RED_BALANCE: ksnprintf(name, sizeof(name), "White Balance Red Component"); bit_offset = 16; bit_size = 16; superior_id = V4L2_CID_AUTO_WHITE_BALANCE; break; default: ret = EINVAL; goto err_done; break; } } else if (info->topo_type == UVC_TOPO_TYPE_CAMERA_TERMINAL) { switch (id) { case V4L2_CID_EXPOSURE_AUTO: ksnprintf(name, sizeof(name), "Exposure, Auto"); type = V4L2_CTRL_TYPE_MENU; bit_size = 4; inferior_ids[0] = V4L2_CID_EXPOSURE_ABSOLUTE; break; case V4L2_CID_EXPOSURE_AUTO_PRIORITY: ksnprintf(name, sizeof(name), "Exposure, Auto Priority"); type = V4L2_CTRL_TYPE_BOOLEAN; bit_size = 1; break; case V4L2_CID_EXPOSURE_ABSOLUTE: ksnprintf(name, sizeof(name), "Exposure (Absolute)"); bit_size = 32; superior_id = V4L2_CID_EXPOSURE_AUTO; superior_manual = V4L2_EXPOSURE_MANUAL; break; case V4L2_CID_FOCUS_ABSOLUTE: ksnprintf(name, sizeof(name), "Focus (absolute)"); bit_size = 16; superior_id = V4L2_CID_FOCUS_AUTO; break; case V4L2_CID_FOCUS_AUTO: ksnprintf(name, sizeof(name), "Focus, Auto"); type = V4L2_CTRL_TYPE_BOOLEAN; bit_size = 1; inferior_ids[0] = V4L2_CID_FOCUS_ABSOLUTE; break; case V4L2_CID_IRIS_ABSOLUTE: ksnprintf(name, sizeof(name), "Iris, Absolute"); bit_size = 16; break; case V4L2_CID_IRIS_RELATIVE: ksnprintf(name, sizeof(name), "Iris, Relative"); bit_size = 8; break; case V4L2_CID_ZOOM_ABSOLUTE: ksnprintf(name, sizeof(name), "Zoom, Absolute"); bit_size = 16; break; case V4L2_CID_ZOOM_CONTINUOUS: ksnprintf(name, sizeof(name), "Zoom, Continuous"); bit_size = 0; // should use get/set break; case V4L2_CID_PAN_ABSOLUTE: ksnprintf(name, sizeof(name), "Pan (Absolute)"); bit_size = 32; break; case V4L2_CID_TILT_ABSOLUTE: ksnprintf(name, sizeof(name), "Tilt (Absolute)"); bit_offset = 32; bit_size = 32; break; case V4L2_CID_PAN_SPEED: ksnprintf(name, sizeof(name), "Pan (Speed)"); bit_size = 16; break; case V4L2_CID_TILT_SPEED: ksnprintf(name, sizeof(name), "Tilt (Speed)"); bit_offset = 16; bit_size = 16; break; case V4L2_CID_PRIVACY: ksnprintf(name, sizeof(name), "Privacy"); type = V4L2_CTRL_TYPE_BOOLEAN; bit_size = 1; break; default: ret = EINVAL; goto err_done; break; } } strcpy(sub_info->v4l2_name, name); sub_info->v4l2_type = type; sub_info->bit_offset = bit_offset; sub_info->bit_size = bit_size; sub_info->superior_v4l2_id = superior_id; sub_info->superior_manual = superior_manual; sub_info->inferior_v4l2_ids[0] = inferior_ids[0]; sub_info->inferior_v4l2_ids[1] = inferior_ids[1]; return 0; err_done: kprintf("%s:%d Unknown topo 0x%x, selector 0x%x, v4l2 id 0x%x\n", __func__, __LINE__, info->topo_type, info->selector, id); return ret; } static int uvc_ctrl_get_info_ct(uint8_t bit_idx, struct uvc_ctrl_info *info) { int ret = 0; memset(info, 0, sizeof(*info)); info->topo_type = UVC_TOPO_TYPE_CAMERA_TERMINAL; info->index = bit_idx; // Table 3-6 Camera Terminal Descriptor, bitmap bmControls switch (bit_idx) { case 0: // Table 4-9 info->selector = UVC_CT_SCANNING_MODE_CONTROL; info->byte_size = 1; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_CUR | UVC_CTRL_RESTORE; break; case 1: // Table 4-10 info->selector = UVC_CT_AE_MODE_CONTROL; info->byte_size = 1; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_CUR | UVC_CTRL_GET_DEF | UVC_CTRL_GET_RES | UVC_CTRL_RESTORE; info->data_type = UVC_CTRL_DATA_BITMASK; break; case 2: // Table 4-11 info->selector = UVC_CT_AE_PRIORITY_CONTROL; info->byte_size = 1; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_CUR | UVC_CTRL_RESTORE; info->data_type = UVC_CTRL_DATA_BOOLEAN; break; case 3: // Table 4-12 info->selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL; info->byte_size = 4; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE | UVC_CTRL_AUTO_UPDATE; info->data_type = UVC_CTRL_DATA_UNSIGNED; break; case 4: // Table 4-13 info->selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL; info->byte_size = 1; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_RESTORE; break; case 5: // Table 4-14 info->selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE | UVC_CTRL_AUTO_UPDATE; info->data_type = UVC_CTRL_DATA_UNSIGNED; break; case 6: // Table 4-15 info->selector = UVC_CT_FOCUS_RELATIVE_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_MIN | UVC_CTRL_GET_MAX | UVC_CTRL_GET_RES | UVC_CTRL_GET_DEF | UVC_CTRL_AUTO_UPDATE; break; case 7: // Table 4-18 info->selector = UVC_CT_IRIS_ABSOLUTE_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE | UVC_CTRL_AUTO_UPDATE; info->data_type = UVC_CTRL_DATA_UNSIGNED; break; case 8: // Table 4-19 info->selector = UVC_CT_IRIS_RELATIVE_CONTROL; info->byte_size = 1; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_AUTO_UPDATE; info->data_type = UVC_CTRL_DATA_SIGNED; break; case 9: // Table 4-20 info->selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE | UVC_CTRL_AUTO_UPDATE; info->data_type = UVC_CTRL_DATA_UNSIGNED; break; case 10: // Table 4-21 info->selector = UVC_CT_ZOOM_RELATIVE_CONTROL; info->byte_size = 3; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_MIN | UVC_CTRL_GET_MAX | UVC_CTRL_GET_RES | UVC_CTRL_GET_DEF | UVC_CTRL_AUTO_UPDATE; info->data_type = UVC_CTRL_DATA_SIGNED; break; case 11: // Table 4-22 info->selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL; info->byte_size = 8; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE | UVC_CTRL_AUTO_UPDATE; info->data_type = UVC_CTRL_DATA_SIGNED; break; case 12: // Table 4-23 info->selector = UVC_CT_PANTILT_RELATIVE_CONTROL; info->byte_size = 4; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_AUTO_UPDATE; info->data_type = UVC_CTRL_DATA_SIGNED; break; case 13: // Table 4-24 info->selector = UVC_CT_ROLL_ABSOLUTE_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE | UVC_CTRL_AUTO_UPDATE; break; case 14: // Table 4-25 info->selector = UVC_CT_ROLL_RELATIVE_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_MIN | UVC_CTRL_GET_MAX | UVC_CTRL_GET_RES | UVC_CTRL_GET_DEF | UVC_CTRL_AUTO_UPDATE; break; case 17: // Table 4-17 info->selector = UVC_CT_FOCUS_AUTO_CONTROL; info->byte_size = 1; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_CUR | UVC_CTRL_GET_DEF | UVC_CTRL_RESTORE; info->data_type = UVC_CTRL_DATA_BOOLEAN; break; case 18: // Table 4-26 info->selector = UVC_CT_PRIVACY_CONTROL; info->byte_size = 1; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_CUR | UVC_CTRL_RESTORE | UVC_CTRL_AUTO_UPDATE; info->data_type = UVC_CTRL_DATA_BOOLEAN; break; default: // Others to be implemented. ret = EINVAL; break; } if (ret != 0) { memset(info, 0, sizeof(*info)); } return ret; } static int uvc_ctrl_get_info_pu(uint8_t bit_idx, struct uvc_ctrl_info *info) { int ret = 0; memset(info, 0, sizeof(*info)); info->topo_type = UVC_TOPO_TYPE_PROCESSING_UNIT; info->index = bit_idx; // Table 3-8 Processing Unit Descriptor, bitmap bmControls switch (bit_idx) { case 0: // Table 4-31 info->selector = UVC_PU_BRIGHTNESS_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE; info->data_type = UVC_CTRL_DATA_SIGNED; break; case 1: // Table 4-32 info->selector = UVC_PU_CONTRAST_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE; info->data_type = UVC_CTRL_DATA_UNSIGNED; break; case 2: // Table 4-36 info->selector = UVC_PU_HUE_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE | UVC_CTRL_AUTO_UPDATE; info->data_type = UVC_CTRL_DATA_SIGNED; break; case 3: // Table 4-38 info->selector = UVC_PU_SATURATION_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE; info->data_type = UVC_CTRL_DATA_UNSIGNED; break; case 4: // Table 4-39 info->selector = UVC_PU_SHARPNESS_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE; info->data_type = UVC_CTRL_DATA_UNSIGNED; break; case 5: // Table 4-40 info->selector = UVC_PU_GAMMA_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE; info->data_type = UVC_CTRL_DATA_UNSIGNED; break; case 6: // Table 4-41 info->selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE | UVC_CTRL_AUTO_UPDATE; info->data_type = UVC_CTRL_DATA_UNSIGNED; break; case 7: // Table 4-43 info->selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL; info->byte_size = 4; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE | UVC_CTRL_AUTO_UPDATE; info->data_type = UVC_CTRL_DATA_SIGNED; break; case 8: // Table 4-30 info->selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE; info->data_type = UVC_CTRL_DATA_UNSIGNED; break; case 9: // Table 4-34 info->selector = UVC_PU_GAIN_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE; info->data_type = UVC_CTRL_DATA_UNSIGNED; break; case 10: // Table 4-35 info->selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL; info->byte_size = 1; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_CUR | UVC_CTRL_GET_DEF | UVC_CTRL_RESTORE; info->data_type = UVC_CTRL_DATA_ENUM; break; case 11: // Table 4-37 info->selector = UVC_PU_HUE_AUTO_CONTROL; info->byte_size = 1; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_CUR | UVC_CTRL_GET_DEF | UVC_CTRL_RESTORE; info->data_type = UVC_CTRL_DATA_BOOLEAN; break; case 12: // Table 4-42 info->selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL; info->byte_size = 1; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_CUR | UVC_CTRL_GET_DEF | UVC_CTRL_RESTORE; info->data_type = UVC_CTRL_DATA_BOOLEAN; break; case 13: // Table 4-44 info->selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL; info->byte_size = 1; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_CUR | UVC_CTRL_GET_DEF | UVC_CTRL_RESTORE; info->data_type = UVC_CTRL_DATA_BOOLEAN; break; case 14: // Table 4-45 info->selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE; break; case 15: // Table 4-46 info->selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL; info->byte_size = 2; info->flags = UVC_CTRL_SET_CUR | UVC_CTRL_GET_RANGE | UVC_CTRL_RESTORE; break; case 16: // Table 4-47 info->selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL; info->byte_size = 1; info->flags = UVC_CTRL_GET_CUR; break; case 17: // Table 4-48 info->selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL; info->byte_size = 1; info->flags = UVC_CTRL_GET_CUR; break; default: ret = EINVAL; break; } if (ret != 0) { memset(info, 0, sizeof(*info)); } return ret; } static const struct uvc_ctrl_menu_item exposure_mode_menu[] = { { 2, "Auto Mode" }, { 1, "Manual Mode" }, { 4, "Shutter Priority Mode" }, { 8, "Aperture Priority Mode" }, }; static const struct uvc_ctrl_menu_item power_line_freq_menu[] = { { 0, "Disabled" }, { 1, "50 Hz" }, { 2, "60 Hz" }, }; static int uvc_ctrl_init_menu(struct uvc_ctrl_info *info) { const struct uvc_ctrl_menu_item *src_menu_data = NULL; struct uvc_ctrl_menu_item *dst_menu_data = NULL; uint32_t item_num = 0; uint32_t len = 0; if (info->topo_type == UVC_TOPO_TYPE_CAMERA_TERMINAL && info->selector == UVC_CT_AE_MODE_CONTROL) { src_menu_data = exposure_mode_menu; item_num = ARRAY_SIZE(exposure_mode_menu); } else if (info->topo_type == UVC_TOPO_TYPE_PROCESSING_UNIT && info->selector == UVC_PU_POWER_LINE_FREQUENCY_CONTROL) { src_menu_data = power_line_freq_menu; item_num = ARRAY_SIZE(power_line_freq_menu); } else { return 0; } len = item_num * sizeof(struct uvc_ctrl_menu_item); dst_menu_data = kmalloc(len, M_UVC, M_ZERO | M_WAITOK); if (dst_menu_data == NULL) { kprintf("%s:%d fail to alloc mem\n", __func__, __LINE__); return ENOMEM; } memcpy(dst_menu_data, src_menu_data, len); info->menus[0].item_num = item_num; info->menus[0].menu_data = dst_menu_data; return 0; } static void uvc_ctrl_init_sub_info_num(struct uvc_ctrl_info *info) { info->sub_info_num = 1; if (info->topo_type == UVC_TOPO_TYPE_CAMERA_TERMINAL) { switch (info->selector) { case UVC_CT_PANTILT_ABSOLUTE_CONTROL: case UVC_CT_PANTILT_RELATIVE_CONTROL: info->sub_info_num = 2; break; } } else if (info->topo_type == UVC_TOPO_TYPE_PROCESSING_UNIT) { switch (info->selector) { case UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL: info->sub_info_num = 2; break; } } } static int uvc_ctrl_build_mapping_ct(struct uvc_ctrl_info *info) { int ret = 0; switch (info->selector) { case UVC_CT_AE_MODE_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_EXPOSURE_AUTO; break; case UVC_CT_AE_PRIORITY_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_EXPOSURE_AUTO_PRIORITY; break; case UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_EXPOSURE_ABSOLUTE; break; case UVC_CT_FOCUS_ABSOLUTE_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_FOCUS_ABSOLUTE; break; case UVC_CT_FOCUS_AUTO_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_FOCUS_AUTO; break; case UVC_CT_IRIS_ABSOLUTE_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_IRIS_ABSOLUTE; break; case UVC_CT_IRIS_RELATIVE_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_IRIS_RELATIVE; break; case UVC_CT_ZOOM_ABSOLUTE_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_ZOOM_ABSOLUTE; break; case UVC_CT_ZOOM_RELATIVE_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_ZOOM_CONTINUOUS; break; case UVC_CT_PANTILT_ABSOLUTE_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_PAN_ABSOLUTE; info->sub_infos[1]->v4l2_id = V4L2_CID_TILT_ABSOLUTE; break; case UVC_CT_PANTILT_RELATIVE_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_PAN_SPEED; info->sub_infos[1]->v4l2_id = V4L2_CID_TILT_SPEED; break; case UVC_CT_PRIVACY_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_PRIVACY; break; default: ret = EINVAL; break; } return ret; } static int uvc_ctrl_build_mapping_pu(struct uvc_ctrl_info *info) { int ret = 0; switch (info->selector) { case UVC_PU_BRIGHTNESS_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_BRIGHTNESS; break; case UVC_PU_CONTRAST_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_CONTRAST; break; case UVC_PU_HUE_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_HUE; break; case UVC_PU_SATURATION_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_SATURATION; break; case UVC_PU_SHARPNESS_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_SHARPNESS; break; case UVC_PU_GAMMA_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_GAMMA; break; case UVC_PU_BACKLIGHT_COMPENSATION_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_BACKLIGHT_COMPENSATION; break; case UVC_PU_GAIN_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_GAIN; break; case UVC_PU_POWER_LINE_FREQUENCY_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_POWER_LINE_FREQUENCY; break; case UVC_PU_HUE_AUTO_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_HUE_AUTO; break; case UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_AUTO_WHITE_BALANCE; break; case UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_WHITE_BALANCE_TEMPERATURE; break; case UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_AUTO_WHITE_BALANCE; break; case UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL: info->sub_infos[0]->v4l2_id = V4L2_CID_BLUE_BALANCE; info->sub_infos[1]->v4l2_id = V4L2_CID_RED_BALANCE; break; default: ret = EINVAL; } return ret; } static int uvc_ctrl_build_mapping(struct uvc_ctrl_info *info) { int ret = 0; uint8_t i = 0; enum uvc_topo_type topo_type = info->topo_type; uint8_t sub_info_num = info->sub_info_num; struct uvc_ctrl_sub_info *sub_info = NULL; if (sub_info_num == 0) { return 0; } for (i = 0; i < sub_info_num; i++) { sub_info = kmalloc(sizeof(*sub_info), M_UVC, M_ZERO | M_WAITOK); if (sub_info == NULL) { kprintf("%s:%d fail to alloc mem\n", __func__, __LINE__); ret = ENOMEM; goto err_done; } sub_info->uvc_info = info; info->sub_infos[i] = sub_info; } if (topo_type == UVC_TOPO_TYPE_CAMERA_TERMINAL) { ret = uvc_ctrl_build_mapping_ct(info); } else if (topo_type == UVC_TOPO_TYPE_PROCESSING_UNIT) { ret = uvc_ctrl_build_mapping_pu(info); } else { ret = EINVAL; } if (ret != 0) { goto err_done; } for (int i = 0; i < sub_info_num; i++) { uvc_ctrl_init_sub_info(info, info->sub_infos[i]); } return 0; err_done: for (i = 0; i < MAX_MAPPING_NUM; i++) { if (info->sub_infos[i] != NULL) { kfree(info->sub_infos[i], M_UVC); info->sub_infos[i] = NULL; } } return ret; } static int uvc_ctrl_initialize_control(struct uvc_control *ctrl) { int ret = 0; uint8_t bit_idx = ctrl->index; uint16_t node_type = UVC_ENT_TYPE(ctrl->topo_node); struct uvc_ctrl_info tmp_info; switch (node_type) { case UVC_ITT_CAMERA: ret = uvc_ctrl_get_info_ct(bit_idx, &tmp_info); if (ret != 0) { return EINVAL; } break; case UDESCSUB_VC_PROCESSING_UNIT: ret = uvc_ctrl_get_info_pu(bit_idx, &tmp_info); if (ret != 0) { return EINVAL; } break; default: return 0; } ctrl->uvc_data = kmalloc(tmp_info.byte_size * UVC_CTRL_DATA_LAST + 1, M_UVC, M_ZERO | M_WAITOK); if (ctrl->uvc_data == NULL) { kprintf("%s:%d fail to allocate memory\n", __func__, __LINE__); ret = ENOMEM; goto err; } uvc_ctrl_init_sub_info_num(&tmp_info); ret = uvc_ctrl_build_mapping(&tmp_info); if (0 != ret) { goto err; } ret = uvc_ctrl_init_menu(&tmp_info); if (0 != ret) { goto err; } ctrl->info = tmp_info; ctrl->initialized = 1; return 0; err: if (ctrl->uvc_data != NULL) { kfree(ctrl->uvc_data, M_UVC); ctrl->uvc_data = NULL; } return ret; } void uvc_ctrl_destroy_mappings(struct uvc_control *ctrl) { uint8_t i = 0; if (!ctrl) return; for (i = 0; i < MAX_MAPPING_NUM; i++) { if (ctrl->info.sub_infos[i] != NULL) { DPRINTF("removing v4l2 mapping '%s' \n", ctrl->info.sub_infos[i]->v4l2_name); kfree(ctrl->info.sub_infos[i], M_UVC); ctrl->info.sub_infos[i] = NULL; } if (ctrl->info.menus[i].menu_data != NULL) { kfree(ctrl->info.menus[i].menu_data, M_UVC); ctrl->info.menus[i].menu_data = NULL; } } ctrl->info.sub_info_num = 0; return; } static unsigned int uvc_ctrl_count_control(const uint8_t *bmCtrls, uint8_t bCtrlSize) { int i = 0; unsigned int count = 0; const uint8_t *data = bmCtrls; if (data == NULL || bCtrlSize == 0) return 0; for (i = 0; i < bCtrlSize * 8; i++) { if ((data[i >> UVC_BITSHIFT] >> (i & UVC_BITMASK)) & UVC_VALMASK) count++; } return count; } int uvc_ctrl_init_dev(struct uvc_softc *sc, struct uvc_drv_ctrl *ctrls) { struct uvc_topo_node *topo_node, *tmp; struct uvc_control *ctrl = NULL; uint8_t bCtrlSize = 0; uint32_t nctrls = 0; uint8_t *bmCtrls = NULL; uint8_t i = 0; struct uvc_xu_node_info *node_info_xu = NULL; struct uvc_pu_node_info *node_info_pu = NULL; struct uvc_ct_node_info *node_info_ct = NULL; if (!ctrls) { return EINVAL; } STAILQ_FOREACH_SAFE(topo_node, &ctrls->topo_nodes, link, tmp) { if (UVC_ENT_TYPE(topo_node) == UDESCSUB_VC_EXTENSION_UNIT) { node_info_xu = (struct uvc_xu_node_info*)topo_node->node_info; bmCtrls = node_info_xu->bmControls; bCtrlSize = node_info_xu->bControlSize; } else if (UVC_ENT_TYPE(topo_node) == UDESCSUB_VC_PROCESSING_UNIT) { node_info_pu = (struct uvc_pu_node_info*)topo_node->node_info; bmCtrls = node_info_pu->bmControls; bCtrlSize = node_info_pu->bControlSize; } else if (UVC_ENT_TYPE(topo_node) == UVC_ITT_CAMERA) { node_info_ct = (struct uvc_ct_node_info*)topo_node->node_info; bmCtrls = node_info_ct->bmControls; bCtrlSize = node_info_ct->bControlSize; } else continue; nctrls = uvc_ctrl_count_control(bmCtrls, bCtrlSize); if (nctrls == 0) continue; topo_node->controls = kmalloc(nctrls * sizeof(*ctrl), M_UVC, M_ZERO | M_WAITOK); if (!topo_node->controls) return ENOMEM; topo_node->controls_num = nctrls; ctrl = topo_node->controls; for (i = 0; i < bCtrlSize * 8; i++) { if (uvc_test_bit(bmCtrls, i) == 0) continue; ctrl->topo_node = topo_node; ctrl->index = i; uvc_ctrl_initialize_control(ctrl); ctrl++; } } return 0; } static int uvc_query_v4l2_ctrl_sub(struct uvc_control *ctrl, struct v4l2_queryctrl *query) { int i = 0; uint8_t sub_info_idx = ctrl->info.cached_sub_info_idx; struct uvc_ctrl_menu_item *menu_items = ctrl->info.menus[sub_info_idx].menu_data; uint8_t menu_item_num = ctrl->info.menus[sub_info_idx].item_num; struct uvc_ctrl_sub_info *sub_info = ctrl->info.sub_infos[sub_info_idx]; memset(query, 0, sizeof(*query)); if (sub_info == NULL) { return EINVAL; } query->id = sub_info->v4l2_id; query->type = sub_info->v4l2_type; strlcpy(query->name, sub_info->v4l2_name, sizeof(query->name)); query->flags = 0; switch (sub_info->v4l2_type) { case V4L2_CTRL_TYPE_BOOLEAN: query->minimum = 0; query->maximum = 1; query->step = 1; goto end; case V4L2_CTRL_TYPE_BUTTON: query->minimum = 0; query->maximum = 0; query->step = 0; goto end; case V4L2_CTRL_TYPE_MENU: query->minimum = 0; query->maximum = menu_item_num - 1; query->step = 1; for (i = 0; i < menu_item_num; i++) { if (menu_items[i].value == query->default_value) { query->default_value = i; break; } } goto end; default: break; } /*todo*/ DPRINTF("WARNING __TO_BE_IMPLEMENT__ other par %s\n", __func__); end: return 0; } static struct uvc_control * uvc_search_control_sub(struct uvc_topo_node *topo_node, uint32_t v4l2_id) { unsigned int i; unsigned int j; struct uvc_control *ctrl = NULL; if (topo_node == NULL) { return NULL; } for (i = 0; i < topo_node->controls_num; ++i) { ctrl = &topo_node->controls[i]; if (!ctrl->initialized) continue; for (j = 0; j < MAX_MAPPING_NUM; j++) { if (ctrl->info.sub_infos[j] == NULL) { continue; } if (ctrl->info.sub_infos[j]->v4l2_id == v4l2_id) { ctrl->info.cached_sub_info_idx = j; return ctrl; } } } return NULL; } static struct uvc_control * uvc_search_control(struct uvc_drv_ctrl *ctrls, uint32_t v4l2_id) { struct uvc_control *ret_ctrl = NULL; struct uvc_topo_node *topo_node = NULL, *tmp = NULL; if (v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL) { DPRINTF("Not support V4L2_CTRL_FLAG_NEXT_CTRL (0x%08x )\n", v4l2_id); return NULL; } /* Mask the query flags. */ v4l2_id &= V4L2_CTRL_ID_MASK; /* Find the control. */ STAILQ_FOREACH_SAFE(topo_node, &ctrls->topo_nodes, link, tmp) { ret_ctrl = uvc_search_control_sub(topo_node, v4l2_id); if (ret_ctrl != NULL) { return ret_ctrl; } } DPRINTF("Control 0x%08x not found.\n", v4l2_id); return NULL; } int uvc_query_v4l2_ctrl(struct uvc_drv_video *video, struct v4l2_queryctrl *query) { struct uvc_control *ctrl; int ret = EINVAL; UVC_LOCK(&video->ctrl->mtx); ctrl = uvc_search_control(video->ctrl, query->id); if (ctrl == NULL) { ret = EINVAL; goto done; } ret = uvc_query_v4l2_ctrl_sub(ctrl, query); done: UVC_UNLOCK(&video->ctrl->mtx); return ret; } int uvc_query_v4l2_menu(struct uvc_drv_video *video, struct v4l2_querymenu *qm) { struct uvc_control *ctrl; uint8_t sub_info_idx = 0; int ret = 0; int id = qm->id; int index = qm->index; memset(qm, 0, sizeof(*qm)); qm->id = id; qm->index = index; UVC_LOCK(&video->ctrl->mtx); ctrl = uvc_search_control(video->ctrl, qm->id); if (ctrl == NULL) { ret = EINVAL; goto done; } sub_info_idx = ctrl->info.cached_sub_info_idx; if (ctrl->info.menus[sub_info_idx].menu_data == NULL) { ret = EINVAL; goto done; } strlcpy(qm->name, ctrl->info.sub_infos[sub_info_idx]->v4l2_name, sizeof(qm->name)); done: UVC_UNLOCK(&video->ctrl->mtx); return ret; } |