1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * uvc_ctrl.c -- USB Video Class driver - Controls 4 * 5 * Copyright (C) 2005-2010 6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com) 7 */ 8 9 #include <asm/barrier.h> 10 #include <linux/bitops.h> 11 #include <linux/kernel.h> 12 #include <linux/list.h> 13 #include <linux/module.h> 14 #include <linux/slab.h> 15 #include <linux/uaccess.h> 16 #include <linux/usb.h> 17 #include <linux/usb/uvc.h> 18 #include <linux/videodev2.h> 19 #include <linux/vmalloc.h> 20 #include <linux/wait.h> 21 #include <linux/workqueue.h> 22 #include <linux/atomic.h> 23 #include <media/v4l2-ctrls.h> 24 25 #include "uvcvideo.h" 26 27 #define UVC_CTRL_DATA_CURRENT 0 28 #define UVC_CTRL_DATA_BACKUP 1 29 #define UVC_CTRL_DATA_MIN 2 30 #define UVC_CTRL_DATA_MAX 3 31 #define UVC_CTRL_DATA_RES 4 32 #define UVC_CTRL_DATA_DEF 5 33 #define UVC_CTRL_DATA_LAST 6 34 35 /* ------------------------------------------------------------------------ 36 * Controls 37 */ 38 39 static const struct uvc_control_info uvc_ctrls[] = { 40 { 41 .entity = UVC_GUID_UVC_PROCESSING, 42 .selector = UVC_PU_BRIGHTNESS_CONTROL, 43 .index = 0, 44 .size = 2, 45 .flags = UVC_CTRL_FLAG_SET_CUR 46 | UVC_CTRL_FLAG_GET_RANGE 47 | UVC_CTRL_FLAG_RESTORE, 48 }, 49 { 50 .entity = UVC_GUID_UVC_PROCESSING, 51 .selector = UVC_PU_CONTRAST_CONTROL, 52 .index = 1, 53 .size = 2, 54 .flags = UVC_CTRL_FLAG_SET_CUR 55 | UVC_CTRL_FLAG_GET_RANGE 56 | UVC_CTRL_FLAG_RESTORE, 57 }, 58 { 59 .entity = UVC_GUID_UVC_PROCESSING, 60 .selector = UVC_PU_HUE_CONTROL, 61 .index = 2, 62 .size = 2, 63 .flags = UVC_CTRL_FLAG_SET_CUR 64 | UVC_CTRL_FLAG_GET_RANGE 65 | UVC_CTRL_FLAG_RESTORE 66 | UVC_CTRL_FLAG_AUTO_UPDATE, 67 }, 68 { 69 .entity = UVC_GUID_UVC_PROCESSING, 70 .selector = UVC_PU_SATURATION_CONTROL, 71 .index = 3, 72 .size = 2, 73 .flags = UVC_CTRL_FLAG_SET_CUR 74 | UVC_CTRL_FLAG_GET_RANGE 75 | UVC_CTRL_FLAG_RESTORE, 76 }, 77 { 78 .entity = UVC_GUID_UVC_PROCESSING, 79 .selector = UVC_PU_SHARPNESS_CONTROL, 80 .index = 4, 81 .size = 2, 82 .flags = UVC_CTRL_FLAG_SET_CUR 83 | UVC_CTRL_FLAG_GET_RANGE 84 | UVC_CTRL_FLAG_RESTORE, 85 }, 86 { 87 .entity = UVC_GUID_UVC_PROCESSING, 88 .selector = UVC_PU_GAMMA_CONTROL, 89 .index = 5, 90 .size = 2, 91 .flags = UVC_CTRL_FLAG_SET_CUR 92 | UVC_CTRL_FLAG_GET_RANGE 93 | UVC_CTRL_FLAG_RESTORE, 94 }, 95 { 96 .entity = UVC_GUID_UVC_PROCESSING, 97 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL, 98 .index = 6, 99 .size = 2, 100 .flags = UVC_CTRL_FLAG_SET_CUR 101 | UVC_CTRL_FLAG_GET_RANGE 102 | UVC_CTRL_FLAG_RESTORE 103 | UVC_CTRL_FLAG_AUTO_UPDATE, 104 }, 105 { 106 .entity = UVC_GUID_UVC_PROCESSING, 107 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL, 108 .index = 7, 109 .size = 4, 110 .flags = UVC_CTRL_FLAG_SET_CUR 111 | UVC_CTRL_FLAG_GET_RANGE 112 | UVC_CTRL_FLAG_RESTORE 113 | UVC_CTRL_FLAG_AUTO_UPDATE, 114 }, 115 { 116 .entity = UVC_GUID_UVC_PROCESSING, 117 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL, 118 .index = 8, 119 .size = 2, 120 .flags = UVC_CTRL_FLAG_SET_CUR 121 | UVC_CTRL_FLAG_GET_RANGE 122 | UVC_CTRL_FLAG_RESTORE, 123 }, 124 { 125 .entity = UVC_GUID_UVC_PROCESSING, 126 .selector = UVC_PU_GAIN_CONTROL, 127 .index = 9, 128 .size = 2, 129 .flags = UVC_CTRL_FLAG_SET_CUR 130 | UVC_CTRL_FLAG_GET_RANGE 131 | UVC_CTRL_FLAG_RESTORE, 132 }, 133 { 134 .entity = UVC_GUID_UVC_PROCESSING, 135 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 136 .index = 10, 137 .size = 1, 138 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 139 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, 140 }, 141 { 142 .entity = UVC_GUID_UVC_PROCESSING, 143 .selector = UVC_PU_HUE_AUTO_CONTROL, 144 .index = 11, 145 .size = 1, 146 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 147 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, 148 }, 149 { 150 .entity = UVC_GUID_UVC_PROCESSING, 151 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL, 152 .index = 12, 153 .size = 1, 154 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 155 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, 156 }, 157 { 158 .entity = UVC_GUID_UVC_PROCESSING, 159 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL, 160 .index = 13, 161 .size = 1, 162 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 163 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, 164 }, 165 { 166 .entity = UVC_GUID_UVC_PROCESSING, 167 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL, 168 .index = 14, 169 .size = 2, 170 .flags = UVC_CTRL_FLAG_SET_CUR 171 | UVC_CTRL_FLAG_GET_RANGE 172 | UVC_CTRL_FLAG_RESTORE, 173 }, 174 { 175 .entity = UVC_GUID_UVC_PROCESSING, 176 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL, 177 .index = 15, 178 .size = 2, 179 .flags = UVC_CTRL_FLAG_SET_CUR 180 | UVC_CTRL_FLAG_GET_RANGE 181 | UVC_CTRL_FLAG_RESTORE, 182 }, 183 { 184 .entity = UVC_GUID_UVC_PROCESSING, 185 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL, 186 .index = 16, 187 .size = 1, 188 .flags = UVC_CTRL_FLAG_GET_CUR, 189 }, 190 { 191 .entity = UVC_GUID_UVC_PROCESSING, 192 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL, 193 .index = 17, 194 .size = 1, 195 .flags = UVC_CTRL_FLAG_GET_CUR, 196 }, 197 { 198 .entity = UVC_GUID_UVC_CAMERA, 199 .selector = UVC_CT_SCANNING_MODE_CONTROL, 200 .index = 0, 201 .size = 1, 202 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 203 | UVC_CTRL_FLAG_RESTORE, 204 }, 205 { 206 .entity = UVC_GUID_UVC_CAMERA, 207 .selector = UVC_CT_AE_MODE_CONTROL, 208 .index = 1, 209 .size = 1, 210 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 211 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES 212 | UVC_CTRL_FLAG_RESTORE, 213 }, 214 { 215 .entity = UVC_GUID_UVC_CAMERA, 216 .selector = UVC_CT_AE_PRIORITY_CONTROL, 217 .index = 2, 218 .size = 1, 219 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 220 | UVC_CTRL_FLAG_RESTORE, 221 }, 222 { 223 .entity = UVC_GUID_UVC_CAMERA, 224 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL, 225 .index = 3, 226 .size = 4, 227 .flags = UVC_CTRL_FLAG_SET_CUR 228 | UVC_CTRL_FLAG_GET_RANGE 229 | UVC_CTRL_FLAG_RESTORE 230 | UVC_CTRL_FLAG_AUTO_UPDATE, 231 }, 232 { 233 .entity = UVC_GUID_UVC_CAMERA, 234 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL, 235 .index = 4, 236 .size = 1, 237 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE, 238 }, 239 { 240 .entity = UVC_GUID_UVC_CAMERA, 241 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL, 242 .index = 5, 243 .size = 2, 244 .flags = UVC_CTRL_FLAG_SET_CUR 245 | UVC_CTRL_FLAG_GET_RANGE 246 | UVC_CTRL_FLAG_RESTORE 247 | UVC_CTRL_FLAG_AUTO_UPDATE, 248 }, 249 { 250 .entity = UVC_GUID_UVC_CAMERA, 251 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL, 252 .index = 6, 253 .size = 2, 254 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN 255 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES 256 | UVC_CTRL_FLAG_GET_DEF 257 | UVC_CTRL_FLAG_AUTO_UPDATE, 258 }, 259 { 260 .entity = UVC_GUID_UVC_CAMERA, 261 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL, 262 .index = 7, 263 .size = 2, 264 .flags = UVC_CTRL_FLAG_SET_CUR 265 | UVC_CTRL_FLAG_GET_RANGE 266 | UVC_CTRL_FLAG_RESTORE 267 | UVC_CTRL_FLAG_AUTO_UPDATE, 268 }, 269 { 270 .entity = UVC_GUID_UVC_CAMERA, 271 .selector = UVC_CT_IRIS_RELATIVE_CONTROL, 272 .index = 8, 273 .size = 1, 274 .flags = UVC_CTRL_FLAG_SET_CUR 275 | UVC_CTRL_FLAG_AUTO_UPDATE, 276 }, 277 { 278 .entity = UVC_GUID_UVC_CAMERA, 279 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL, 280 .index = 9, 281 .size = 2, 282 .flags = UVC_CTRL_FLAG_SET_CUR 283 | UVC_CTRL_FLAG_GET_RANGE 284 | UVC_CTRL_FLAG_RESTORE 285 | UVC_CTRL_FLAG_AUTO_UPDATE, 286 }, 287 { 288 .entity = UVC_GUID_UVC_CAMERA, 289 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL, 290 .index = 10, 291 .size = 3, 292 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN 293 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES 294 | UVC_CTRL_FLAG_GET_DEF 295 | UVC_CTRL_FLAG_AUTO_UPDATE, 296 }, 297 { 298 .entity = UVC_GUID_UVC_CAMERA, 299 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL, 300 .index = 11, 301 .size = 8, 302 .flags = UVC_CTRL_FLAG_SET_CUR 303 | UVC_CTRL_FLAG_GET_RANGE 304 | UVC_CTRL_FLAG_RESTORE 305 | UVC_CTRL_FLAG_AUTO_UPDATE, 306 }, 307 { 308 .entity = UVC_GUID_UVC_CAMERA, 309 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL, 310 .index = 12, 311 .size = 4, 312 .flags = UVC_CTRL_FLAG_SET_CUR 313 | UVC_CTRL_FLAG_GET_RANGE 314 | UVC_CTRL_FLAG_AUTO_UPDATE, 315 }, 316 { 317 .entity = UVC_GUID_UVC_CAMERA, 318 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL, 319 .index = 13, 320 .size = 2, 321 .flags = UVC_CTRL_FLAG_SET_CUR 322 | UVC_CTRL_FLAG_GET_RANGE 323 | UVC_CTRL_FLAG_RESTORE 324 | UVC_CTRL_FLAG_AUTO_UPDATE, 325 }, 326 { 327 .entity = UVC_GUID_UVC_CAMERA, 328 .selector = UVC_CT_ROLL_RELATIVE_CONTROL, 329 .index = 14, 330 .size = 2, 331 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN 332 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES 333 | UVC_CTRL_FLAG_GET_DEF 334 | UVC_CTRL_FLAG_AUTO_UPDATE, 335 }, 336 { 337 .entity = UVC_GUID_UVC_CAMERA, 338 .selector = UVC_CT_FOCUS_AUTO_CONTROL, 339 .index = 17, 340 .size = 1, 341 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 342 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, 343 }, 344 { 345 .entity = UVC_GUID_UVC_CAMERA, 346 .selector = UVC_CT_PRIVACY_CONTROL, 347 .index = 18, 348 .size = 1, 349 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 350 | UVC_CTRL_FLAG_RESTORE 351 | UVC_CTRL_FLAG_AUTO_UPDATE, 352 }, 353 { 354 .entity = UVC_GUID_EXT_GPIO_CONTROLLER, 355 .selector = UVC_CT_PRIVACY_CONTROL, 356 .index = 0, 357 .size = 1, 358 .flags = UVC_CTRL_FLAG_GET_CUR 359 | UVC_CTRL_FLAG_AUTO_UPDATE, 360 }, 361 /* 362 * UVC_CTRL_FLAG_AUTO_UPDATE is needed because the RoI may get updated 363 * by sensors. 364 * "This RoI should be the same as specified in most recent SET_CUR 365 * except in the case where the ‘Auto Detect and Track’ and/or 366 * ‘Image Stabilization’ bit have been set." 367 * 4.2.2.1.20 Digital Region of Interest (ROI) Control 368 */ 369 { 370 .entity = UVC_GUID_UVC_CAMERA, 371 .selector = UVC_CT_REGION_OF_INTEREST_CONTROL, 372 .index = 21, 373 .size = 10, 374 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 375 | UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX 376 | UVC_CTRL_FLAG_GET_DEF 377 | UVC_CTRL_FLAG_AUTO_UPDATE, 378 }, 379 { 380 .entity = UVC_GUID_CHROMEOS_XU, 381 .selector = UVC_CROSXU_CONTROL_IQ_PROFILE, 382 .index = 3, 383 .size = 1, 384 .flags = UVC_CTRL_FLAG_SET_CUR 385 | UVC_CTRL_FLAG_GET_RANGE 386 | UVC_CTRL_FLAG_RESTORE, 387 }, 388 }; 389 390 static const u32 uvc_control_classes[] = { 391 V4L2_CID_CAMERA_CLASS, 392 V4L2_CID_USER_CLASS, 393 }; 394 395 static const int exposure_auto_mapping[] = { 2, 1, 4, 8 }; 396 static const int cros_colorfx_mapping[] = { 397 1, /* V4L2_COLORFX_NONE */ 398 -1, /* V4L2_COLORFX_BW */ 399 -1, /* V4L2_COLORFX_SEPIA */ 400 -1, /* V4L2_COLORFX_NEGATIVE */ 401 -1, /* V4L2_COLORFX_EMBOSS */ 402 -1, /* V4L2_COLORFX_SKETCH */ 403 -1, /* V4L2_COLORFX_SKY_BLUE */ 404 -1, /* V4L2_COLORFX_GRASS_GREEN */ 405 -1, /* V4L2_COLORFX_SKIN_WHITEN */ 406 0, /* V4L2_COLORFX_VIVID */ 407 }; 408 409 410 static bool uvc_ctrl_mapping_is_compound(struct uvc_control_mapping *mapping) 411 { 412 return mapping->v4l2_type >= V4L2_CTRL_COMPOUND_TYPES; 413 } 414 415 static s32 uvc_mapping_get_s32(struct uvc_control_mapping *mapping, 416 u8 query, const void *data_in) 417 { 418 s32 data_out = 0; 419 420 mapping->get(mapping, query, data_in, sizeof(data_out), &data_out); 421 422 return data_out; 423 } 424 425 static void uvc_mapping_set_s32(struct uvc_control_mapping *mapping, 426 s32 data_in, void *data_out) 427 { 428 mapping->set(mapping, sizeof(data_in), &data_in, data_out); 429 } 430 431 /* 432 * This function translates the V4L2 menu index @idx, as exposed to userspace as 433 * the V4L2 control value, to the corresponding UVC control value used by the 434 * device. The custom menu_mapping in the control @mapping is used when 435 * available, otherwise the function assumes that the V4L2 and UVC values are 436 * identical. 437 * 438 * For controls of type UVC_CTRL_DATA_TYPE_BITMASK, the UVC control value is 439 * expressed as a bitmask and is thus guaranteed to have a single bit set. 440 * 441 * The function returns -EINVAL if the V4L2 menu index @idx isn't valid for the 442 * control, which includes all controls whose type isn't UVC_CTRL_DATA_TYPE_ENUM 443 * or UVC_CTRL_DATA_TYPE_BITMASK. 444 */ 445 static int uvc_mapping_get_menu_value(const struct uvc_control_mapping *mapping, 446 u32 idx) 447 { 448 if (!test_bit(idx, &mapping->menu_mask)) 449 return -EINVAL; 450 451 if (mapping->menu_mapping) 452 return mapping->menu_mapping[idx]; 453 454 return idx; 455 } 456 457 static const char * 458 uvc_mapping_get_menu_name(const struct uvc_control_mapping *mapping, u32 idx) 459 { 460 if (!test_bit(idx, &mapping->menu_mask)) 461 return NULL; 462 463 if (mapping->menu_names) 464 return mapping->menu_names[idx]; 465 466 return v4l2_ctrl_get_menu(mapping->id)[idx]; 467 } 468 469 static int uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping, u8 query, 470 const void *uvc_in, size_t v4l2_size, 471 void *v4l2_out) 472 { 473 u8 value = ((u8 *)uvc_in)[2]; 474 s8 sign = ((s8 *)uvc_in)[0]; 475 s32 *out = v4l2_out; 476 477 if (WARN_ON(v4l2_size != sizeof(s32))) 478 return -EINVAL; 479 480 switch (query) { 481 case UVC_GET_CUR: 482 *out = (sign == 0) ? 0 : (sign > 0 ? value : -value); 483 return 0; 484 485 case UVC_GET_MIN: 486 /* Not used, we use -UVC_GET_MAX */ 487 case UVC_GET_MAX: 488 case UVC_GET_RES: 489 case UVC_GET_DEF: 490 default: 491 *out = value; 492 return 0; 493 } 494 } 495 496 static int uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping, 497 size_t v4l2_size, const void *v4l2_in, 498 void *uvc_out) 499 { 500 u8 *out = uvc_out; 501 s32 value; 502 503 if (WARN_ON(v4l2_size != sizeof(s32))) 504 return -EINVAL; 505 506 value = *(u32 *)v4l2_in; 507 out[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff; 508 out[2] = min_t(int, abs(value), 0xff); 509 510 return 0; 511 } 512 513 static int uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping, 514 u8 query, const void *uvc_in, 515 size_t v4l2_size, void *v4l2_out) 516 { 517 unsigned int first = mapping->offset / 8; 518 u8 value = ((u8 *)uvc_in)[first + 1]; 519 s8 sign = ((s8 *)uvc_in)[first]; 520 s32 *out = v4l2_out; 521 522 if (WARN_ON(v4l2_size != sizeof(s32))) 523 return -EINVAL; 524 525 switch (query) { 526 case UVC_GET_CUR: 527 *out = (sign == 0) ? 0 : (sign > 0 ? value : -value); 528 return 0; 529 case UVC_GET_MIN: 530 /* Not used, we use -UVC_GET_MAX */ 531 case UVC_GET_MAX: 532 case UVC_GET_RES: 533 case UVC_GET_DEF: 534 default: 535 *out = value; 536 return 0; 537 } 538 } 539 540 static int uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping, 541 size_t v4l2_size, const void *v4l2_in, 542 void *uvc_out) 543 { 544 unsigned int first = mapping->offset / 8; 545 u8 *out = uvc_out; 546 s32 value; 547 548 if (WARN_ON(v4l2_size != sizeof(s32))) 549 return -EINVAL; 550 551 value = *(u32 *)v4l2_in; 552 out[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff; 553 out[first + 1] = min_t(int, abs(value), 0xff); 554 555 return 0; 556 } 557 558 static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited = { 559 .id = V4L2_CID_POWER_LINE_FREQUENCY, 560 .entity = UVC_GUID_UVC_PROCESSING, 561 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 562 .size = 2, 563 .offset = 0, 564 .v4l2_type = V4L2_CTRL_TYPE_MENU, 565 .data_type = UVC_CTRL_DATA_TYPE_ENUM, 566 .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 567 V4L2_CID_POWER_LINE_FREQUENCY_50HZ), 568 }; 569 570 static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11 = { 571 .id = V4L2_CID_POWER_LINE_FREQUENCY, 572 .entity = UVC_GUID_UVC_PROCESSING, 573 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 574 .size = 2, 575 .offset = 0, 576 .v4l2_type = V4L2_CTRL_TYPE_MENU, 577 .data_type = UVC_CTRL_DATA_TYPE_ENUM, 578 .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 579 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED), 580 }; 581 582 static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc15 = { 583 .id = V4L2_CID_POWER_LINE_FREQUENCY, 584 .entity = UVC_GUID_UVC_PROCESSING, 585 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 586 .size = 2, 587 .offset = 0, 588 .v4l2_type = V4L2_CTRL_TYPE_MENU, 589 .data_type = UVC_CTRL_DATA_TYPE_ENUM, 590 .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 591 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED), 592 }; 593 594 static const struct uvc_control_mapping *uvc_ctrl_filter_plf_mapping( 595 struct uvc_video_chain *chain, struct uvc_control *ctrl) 596 { 597 const struct uvc_control_mapping *out_mapping = 598 &uvc_ctrl_power_line_mapping_uvc11; 599 u8 *buf __free(kfree) = NULL; 600 u8 init_val; 601 int ret; 602 603 buf = kmalloc_obj(*buf); 604 if (!buf) 605 return NULL; 606 607 /* Save the current PLF value, so we can restore it. */ 608 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id, 609 chain->dev->intfnum, ctrl->info.selector, 610 buf, sizeof(*buf)); 611 /* If we cannot read the control skip it. */ 612 if (ret) 613 return NULL; 614 init_val = *buf; 615 616 /* If PLF value cannot be set to off, it is limited. */ 617 *buf = V4L2_CID_POWER_LINE_FREQUENCY_DISABLED; 618 ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id, 619 chain->dev->intfnum, ctrl->info.selector, 620 buf, sizeof(*buf)); 621 if (ret) 622 return &uvc_ctrl_power_line_mapping_limited; 623 624 /* UVC 1.1 does not define auto, we can exit. */ 625 if (chain->dev->uvc_version < 0x150) 626 goto end; 627 628 /* Check if the device supports auto. */ 629 *buf = V4L2_CID_POWER_LINE_FREQUENCY_AUTO; 630 ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id, 631 chain->dev->intfnum, ctrl->info.selector, 632 buf, sizeof(*buf)); 633 if (!ret) 634 out_mapping = &uvc_ctrl_power_line_mapping_uvc15; 635 636 end: 637 /* Restore initial value and add mapping. */ 638 *buf = init_val; 639 uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id, 640 chain->dev->intfnum, ctrl->info.selector, 641 buf, sizeof(*buf)); 642 643 return out_mapping; 644 } 645 646 static int uvc_get_rect(struct uvc_control_mapping *mapping, u8 query, 647 const void *uvc_in, size_t v4l2_size, void *v4l2_out) 648 { 649 const struct uvc_rect *uvc_rect = uvc_in; 650 struct v4l2_rect *v4l2_rect = v4l2_out; 651 652 if (WARN_ON(v4l2_size != sizeof(struct v4l2_rect))) 653 return -EINVAL; 654 655 if (uvc_rect->left > uvc_rect->right || 656 uvc_rect->top > uvc_rect->bottom) 657 return -EIO; 658 659 v4l2_rect->top = uvc_rect->top; 660 v4l2_rect->left = uvc_rect->left; 661 v4l2_rect->height = uvc_rect->bottom - uvc_rect->top + 1; 662 v4l2_rect->width = uvc_rect->right - uvc_rect->left + 1; 663 664 return 0; 665 } 666 667 static int uvc_set_rect(struct uvc_control_mapping *mapping, size_t v4l2_size, 668 const void *v4l2_in, void *uvc_out) 669 { 670 struct uvc_rect *uvc_rect = uvc_out; 671 const struct v4l2_rect *v4l2_rect = v4l2_in; 672 673 if (WARN_ON(v4l2_size != sizeof(struct v4l2_rect))) 674 return -EINVAL; 675 676 uvc_rect->top = min(0xffff, v4l2_rect->top); 677 uvc_rect->left = min(0xffff, v4l2_rect->left); 678 uvc_rect->bottom = min(0xffff, v4l2_rect->top + v4l2_rect->height - 1); 679 uvc_rect->right = min(0xffff, v4l2_rect->left + v4l2_rect->width - 1); 680 681 return 0; 682 } 683 684 static const struct uvc_control_mapping uvc_ctrl_mappings[] = { 685 { 686 .id = V4L2_CID_BRIGHTNESS, 687 .entity = UVC_GUID_UVC_PROCESSING, 688 .selector = UVC_PU_BRIGHTNESS_CONTROL, 689 .size = 16, 690 .offset = 0, 691 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 692 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 693 }, 694 { 695 .id = V4L2_CID_CONTRAST, 696 .entity = UVC_GUID_UVC_PROCESSING, 697 .selector = UVC_PU_CONTRAST_CONTROL, 698 .size = 16, 699 .offset = 0, 700 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 701 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 702 }, 703 { 704 .id = V4L2_CID_HUE, 705 .entity = UVC_GUID_UVC_PROCESSING, 706 .selector = UVC_PU_HUE_CONTROL, 707 .size = 16, 708 .offset = 0, 709 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 710 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 711 .master_id = V4L2_CID_HUE_AUTO, 712 .master_manual = 0, 713 }, 714 { 715 .id = V4L2_CID_SATURATION, 716 .entity = UVC_GUID_UVC_PROCESSING, 717 .selector = UVC_PU_SATURATION_CONTROL, 718 .size = 16, 719 .offset = 0, 720 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 721 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 722 }, 723 { 724 .id = V4L2_CID_SHARPNESS, 725 .entity = UVC_GUID_UVC_PROCESSING, 726 .selector = UVC_PU_SHARPNESS_CONTROL, 727 .size = 16, 728 .offset = 0, 729 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 730 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 731 }, 732 { 733 .id = V4L2_CID_GAMMA, 734 .entity = UVC_GUID_UVC_PROCESSING, 735 .selector = UVC_PU_GAMMA_CONTROL, 736 .size = 16, 737 .offset = 0, 738 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 739 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 740 }, 741 { 742 .id = V4L2_CID_BACKLIGHT_COMPENSATION, 743 .entity = UVC_GUID_UVC_PROCESSING, 744 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL, 745 .size = 16, 746 .offset = 0, 747 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 748 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 749 }, 750 { 751 .id = V4L2_CID_GAIN, 752 .entity = UVC_GUID_UVC_PROCESSING, 753 .selector = UVC_PU_GAIN_CONTROL, 754 .size = 16, 755 .offset = 0, 756 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 757 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 758 }, 759 { 760 .id = V4L2_CID_HUE_AUTO, 761 .entity = UVC_GUID_UVC_PROCESSING, 762 .selector = UVC_PU_HUE_AUTO_CONTROL, 763 .size = 1, 764 .offset = 0, 765 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, 766 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, 767 .slave_ids = { V4L2_CID_HUE, }, 768 }, 769 { 770 .id = V4L2_CID_EXPOSURE_AUTO, 771 .entity = UVC_GUID_UVC_CAMERA, 772 .selector = UVC_CT_AE_MODE_CONTROL, 773 .size = 4, 774 .offset = 0, 775 .v4l2_type = V4L2_CTRL_TYPE_MENU, 776 .data_type = UVC_CTRL_DATA_TYPE_BITMASK, 777 .menu_mapping = exposure_auto_mapping, 778 .menu_mask = GENMASK(V4L2_EXPOSURE_APERTURE_PRIORITY, 779 V4L2_EXPOSURE_AUTO), 780 .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, }, 781 }, 782 { 783 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY, 784 .entity = UVC_GUID_UVC_CAMERA, 785 .selector = UVC_CT_AE_PRIORITY_CONTROL, 786 .size = 1, 787 .offset = 0, 788 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, 789 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, 790 }, 791 { 792 .id = V4L2_CID_EXPOSURE_ABSOLUTE, 793 .entity = UVC_GUID_UVC_CAMERA, 794 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL, 795 .size = 32, 796 .offset = 0, 797 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 798 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 799 .master_id = V4L2_CID_EXPOSURE_AUTO, 800 .master_manual = V4L2_EXPOSURE_MANUAL, 801 }, 802 { 803 .id = V4L2_CID_AUTO_WHITE_BALANCE, 804 .entity = UVC_GUID_UVC_PROCESSING, 805 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL, 806 .size = 1, 807 .offset = 0, 808 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, 809 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, 810 .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, }, 811 }, 812 { 813 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE, 814 .entity = UVC_GUID_UVC_PROCESSING, 815 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL, 816 .size = 16, 817 .offset = 0, 818 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 819 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 820 .master_id = V4L2_CID_AUTO_WHITE_BALANCE, 821 .master_manual = 0, 822 }, 823 { 824 .id = V4L2_CID_AUTO_WHITE_BALANCE, 825 .entity = UVC_GUID_UVC_PROCESSING, 826 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL, 827 .size = 1, 828 .offset = 0, 829 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, 830 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, 831 .slave_ids = { V4L2_CID_BLUE_BALANCE, 832 V4L2_CID_RED_BALANCE }, 833 }, 834 { 835 .id = V4L2_CID_BLUE_BALANCE, 836 .entity = UVC_GUID_UVC_PROCESSING, 837 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL, 838 .size = 16, 839 .offset = 0, 840 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 841 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 842 .master_id = V4L2_CID_AUTO_WHITE_BALANCE, 843 .master_manual = 0, 844 }, 845 { 846 .id = V4L2_CID_RED_BALANCE, 847 .entity = UVC_GUID_UVC_PROCESSING, 848 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL, 849 .size = 16, 850 .offset = 16, 851 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 852 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 853 .master_id = V4L2_CID_AUTO_WHITE_BALANCE, 854 .master_manual = 0, 855 }, 856 { 857 .id = V4L2_CID_FOCUS_ABSOLUTE, 858 .entity = UVC_GUID_UVC_CAMERA, 859 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL, 860 .size = 16, 861 .offset = 0, 862 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 863 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 864 .master_id = V4L2_CID_FOCUS_AUTO, 865 .master_manual = 0, 866 }, 867 { 868 .id = V4L2_CID_FOCUS_AUTO, 869 .entity = UVC_GUID_UVC_CAMERA, 870 .selector = UVC_CT_FOCUS_AUTO_CONTROL, 871 .size = 1, 872 .offset = 0, 873 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, 874 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, 875 .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, }, 876 }, 877 { 878 .id = V4L2_CID_IRIS_ABSOLUTE, 879 .entity = UVC_GUID_UVC_CAMERA, 880 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL, 881 .size = 16, 882 .offset = 0, 883 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 884 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 885 }, 886 { 887 .id = V4L2_CID_IRIS_RELATIVE, 888 .entity = UVC_GUID_UVC_CAMERA, 889 .selector = UVC_CT_IRIS_RELATIVE_CONTROL, 890 .size = 8, 891 .offset = 0, 892 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 893 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 894 }, 895 { 896 .id = V4L2_CID_ZOOM_ABSOLUTE, 897 .entity = UVC_GUID_UVC_CAMERA, 898 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL, 899 .size = 16, 900 .offset = 0, 901 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 902 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 903 }, 904 { 905 .id = V4L2_CID_ZOOM_CONTINUOUS, 906 .entity = UVC_GUID_UVC_CAMERA, 907 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL, 908 .size = 0, 909 .offset = 0, 910 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 911 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 912 .get = uvc_ctrl_get_zoom, 913 .set = uvc_ctrl_set_zoom, 914 }, 915 { 916 .id = V4L2_CID_PAN_ABSOLUTE, 917 .entity = UVC_GUID_UVC_CAMERA, 918 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL, 919 .size = 32, 920 .offset = 0, 921 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 922 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 923 }, 924 { 925 .id = V4L2_CID_TILT_ABSOLUTE, 926 .entity = UVC_GUID_UVC_CAMERA, 927 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL, 928 .size = 32, 929 .offset = 32, 930 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 931 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 932 }, 933 { 934 .id = V4L2_CID_PAN_SPEED, 935 .entity = UVC_GUID_UVC_CAMERA, 936 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL, 937 .size = 16, 938 .offset = 0, 939 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 940 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 941 .get = uvc_ctrl_get_rel_speed, 942 .set = uvc_ctrl_set_rel_speed, 943 }, 944 { 945 .id = V4L2_CID_TILT_SPEED, 946 .entity = UVC_GUID_UVC_CAMERA, 947 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL, 948 .size = 16, 949 .offset = 16, 950 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 951 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 952 .get = uvc_ctrl_get_rel_speed, 953 .set = uvc_ctrl_set_rel_speed, 954 }, 955 { 956 .id = V4L2_CID_PRIVACY, 957 .entity = UVC_GUID_UVC_CAMERA, 958 .selector = UVC_CT_PRIVACY_CONTROL, 959 .size = 1, 960 .offset = 0, 961 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, 962 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, 963 }, 964 { 965 .id = V4L2_CID_PRIVACY, 966 .entity = UVC_GUID_EXT_GPIO_CONTROLLER, 967 .selector = UVC_CT_PRIVACY_CONTROL, 968 .size = 1, 969 .offset = 0, 970 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, 971 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, 972 }, 973 { 974 .entity = UVC_GUID_UVC_PROCESSING, 975 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 976 .filter_mapping = uvc_ctrl_filter_plf_mapping, 977 }, 978 { 979 .id = V4L2_CID_UVC_REGION_OF_INTEREST_RECT, 980 .entity = UVC_GUID_UVC_CAMERA, 981 .selector = UVC_CT_REGION_OF_INTEREST_CONTROL, 982 .size = sizeof(struct uvc_rect) * 8, 983 .offset = 0, 984 .v4l2_type = V4L2_CTRL_TYPE_RECT, 985 .data_type = UVC_CTRL_DATA_TYPE_RECT, 986 .get = uvc_get_rect, 987 .set = uvc_set_rect, 988 .name = "Region of Interest Rectangle", 989 }, 990 { 991 .id = V4L2_CID_UVC_REGION_OF_INTEREST_AUTO, 992 .entity = UVC_GUID_UVC_CAMERA, 993 .selector = UVC_CT_REGION_OF_INTEREST_CONTROL, 994 .size = 16, 995 .offset = 64, 996 .v4l2_type = V4L2_CTRL_TYPE_BITMASK, 997 .data_type = UVC_CTRL_DATA_TYPE_BITMASK, 998 .name = "Region of Interest Auto Ctrls", 999 }, 1000 { 1001 .id = V4L2_CID_COLORFX, 1002 .entity = UVC_GUID_CHROMEOS_XU, 1003 .selector = UVC_CROSXU_CONTROL_IQ_PROFILE, 1004 .size = 8, 1005 .offset = 0, 1006 .v4l2_type = V4L2_CTRL_TYPE_MENU, 1007 .data_type = UVC_CTRL_DATA_TYPE_ENUM, 1008 .menu_mapping = cros_colorfx_mapping, 1009 .menu_mask = BIT(V4L2_COLORFX_VIVID) | 1010 BIT(V4L2_COLORFX_NONE), 1011 }, 1012 }; 1013 1014 /* ------------------------------------------------------------------------ 1015 * Utility functions 1016 */ 1017 1018 static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id) 1019 { 1020 return ctrl->uvc_data + id * ctrl->info.size; 1021 } 1022 1023 static inline int uvc_test_bit(const u8 *data, int bit) 1024 { 1025 return (data[bit >> 3] >> (bit & 7)) & 1; 1026 } 1027 1028 static inline void uvc_clear_bit(u8 *data, int bit) 1029 { 1030 data[bit >> 3] &= ~(1 << (bit & 7)); 1031 } 1032 1033 static s32 uvc_menu_to_v4l2_menu(struct uvc_control_mapping *mapping, s32 val) 1034 { 1035 unsigned int i; 1036 1037 for (i = 0; BIT(i) <= mapping->menu_mask; ++i) { 1038 u32 menu_value; 1039 1040 if (!test_bit(i, &mapping->menu_mask)) 1041 continue; 1042 1043 menu_value = uvc_mapping_get_menu_value(mapping, i); 1044 1045 if (menu_value == val) 1046 return i; 1047 } 1048 1049 return val; 1050 } 1051 1052 /* 1053 * Extract the bit string specified by mapping->offset and mapping->size 1054 * from the little-endian data stored at 'data' and return the result as 1055 * a signed 32bit integer. Sign extension will be performed if the mapping 1056 * references a signed data type. 1057 */ 1058 static int uvc_get_le_value(struct uvc_control_mapping *mapping, 1059 u8 query, const void *uvc_in, size_t v4l2_size, 1060 void *v4l2_out) 1061 { 1062 int offset = mapping->offset; 1063 int bits = mapping->size; 1064 const u8 *data = uvc_in; 1065 s32 *out = v4l2_out; 1066 s32 value = 0; 1067 u8 mask; 1068 1069 if (WARN_ON(v4l2_size != sizeof(s32))) 1070 return -EINVAL; 1071 1072 data += offset / 8; 1073 offset &= 7; 1074 mask = ((1LL << bits) - 1) << offset; 1075 1076 while (1) { 1077 u8 byte = *data & mask; 1078 value |= offset > 0 ? (byte >> offset) : (byte << (-offset)); 1079 bits -= 8 - max(offset, 0); 1080 if (bits <= 0) 1081 break; 1082 1083 offset -= 8; 1084 mask = (1 << bits) - 1; 1085 data++; 1086 } 1087 1088 /* Sign-extend the value if needed. */ 1089 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED) 1090 value |= -(value & (1 << (mapping->size - 1))); 1091 1092 /* If it is a menu, convert from uvc to v4l2. */ 1093 if (mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) { 1094 *out = value; 1095 return 0; 1096 } 1097 1098 switch (query) { 1099 case UVC_GET_CUR: 1100 case UVC_GET_DEF: 1101 *out = uvc_menu_to_v4l2_menu(mapping, value); 1102 return 0; 1103 } 1104 1105 *out = value; 1106 return 0; 1107 } 1108 1109 /* 1110 * Set the bit string specified by mapping->offset and mapping->size 1111 * in the little-endian data stored at 'data' to the value 'value'. 1112 */ 1113 static int uvc_set_le_value(struct uvc_control_mapping *mapping, 1114 size_t v4l2_size, const void *v4l2_in, 1115 void *uvc_out) 1116 { 1117 int offset = mapping->offset; 1118 int bits = mapping->size; 1119 u8 *data = uvc_out; 1120 s32 value; 1121 u8 mask; 1122 1123 if (WARN_ON(v4l2_size != sizeof(s32))) 1124 return -EINVAL; 1125 1126 value = *(s32 *)v4l2_in; 1127 1128 switch (mapping->v4l2_type) { 1129 case V4L2_CTRL_TYPE_MENU: 1130 value = uvc_mapping_get_menu_value(mapping, value); 1131 break; 1132 case V4L2_CTRL_TYPE_BUTTON: 1133 /* 1134 * According to the v4l2 spec, writing any value to a button 1135 * control should result in the action belonging to the button 1136 * control being triggered. UVC devices however want to see a 1 1137 * written -> override value. 1138 */ 1139 value = -1; 1140 break; 1141 default: 1142 break; 1143 } 1144 1145 data += offset / 8; 1146 offset &= 7; 1147 1148 for (; bits > 0; data++) { 1149 mask = ((1LL << bits) - 1) << offset; 1150 *data = (*data & ~mask) | ((value << offset) & mask); 1151 value >>= offset ? offset : 8; 1152 bits -= 8 - offset; 1153 offset = 0; 1154 } 1155 1156 return 0; 1157 } 1158 1159 /* ------------------------------------------------------------------------ 1160 * Terminal and unit management 1161 */ 1162 1163 static int uvc_entity_match_guid(const struct uvc_entity *entity, 1164 const u8 guid[16]) 1165 { 1166 return memcmp(entity->guid, guid, sizeof(entity->guid)) == 0; 1167 } 1168 1169 /* ------------------------------------------------------------------------ 1170 * UVC Controls 1171 */ 1172 1173 static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id, 1174 struct uvc_control_mapping **mapping, struct uvc_control **control, 1175 int next, int next_compound) 1176 { 1177 struct uvc_control *ctrl; 1178 struct uvc_control_mapping *map; 1179 unsigned int i; 1180 1181 if (entity == NULL) 1182 return; 1183 1184 for (i = 0; i < entity->ncontrols; ++i) { 1185 ctrl = &entity->controls[i]; 1186 if (!ctrl->initialized) 1187 continue; 1188 1189 list_for_each_entry(map, &ctrl->info.mappings, list) { 1190 if (map->id == v4l2_id && !next && !next_compound) { 1191 *control = ctrl; 1192 *mapping = map; 1193 return; 1194 } 1195 1196 if ((*mapping == NULL || (*mapping)->id > map->id) && 1197 (map->id > v4l2_id) && 1198 (uvc_ctrl_mapping_is_compound(map) ? 1199 next_compound : next)) { 1200 *control = ctrl; 1201 *mapping = map; 1202 } 1203 } 1204 } 1205 } 1206 1207 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain, 1208 u32 v4l2_id, struct uvc_control_mapping **mapping) 1209 { 1210 struct uvc_control *ctrl = NULL; 1211 struct uvc_entity *entity; 1212 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL; 1213 int next_compound = v4l2_id & V4L2_CTRL_FLAG_NEXT_COMPOUND; 1214 1215 *mapping = NULL; 1216 1217 /* Mask the query flags. */ 1218 v4l2_id &= V4L2_CTRL_ID_MASK; 1219 1220 /* Find the control. */ 1221 list_for_each_entry(entity, &chain->entities, chain) { 1222 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next, 1223 next_compound); 1224 if (ctrl && !next && !next_compound) 1225 return ctrl; 1226 } 1227 1228 if (!ctrl && !next && !next_compound) 1229 uvc_dbg(chain->dev, CONTROL, "Control 0x%08x not found\n", 1230 v4l2_id); 1231 1232 return ctrl; 1233 } 1234 1235 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain, 1236 struct uvc_control *ctrl) 1237 { 1238 int ret; 1239 1240 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) { 1241 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id, 1242 chain->dev->intfnum, ctrl->info.selector, 1243 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF), 1244 ctrl->info.size); 1245 if (ret < 0) 1246 return ret; 1247 } 1248 1249 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) { 1250 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id, 1251 chain->dev->intfnum, ctrl->info.selector, 1252 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN), 1253 ctrl->info.size); 1254 if (ret < 0) 1255 return ret; 1256 } 1257 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) { 1258 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id, 1259 chain->dev->intfnum, ctrl->info.selector, 1260 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX), 1261 ctrl->info.size); 1262 if (ret < 0) 1263 return ret; 1264 } 1265 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) { 1266 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id, 1267 chain->dev->intfnum, ctrl->info.selector, 1268 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 1269 ctrl->info.size); 1270 if (ret < 0) { 1271 if (UVC_ENTITY_TYPE(ctrl->entity) != 1272 UVC_VC_EXTENSION_UNIT) 1273 return ret; 1274 1275 /* 1276 * GET_RES is mandatory for XU controls, but some 1277 * cameras still choke on it. Ignore errors and set the 1278 * resolution value to zero. 1279 */ 1280 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES, 1281 "UVC non compliance - GET_RES failed on " 1282 "an XU control. Enabling workaround.\n"); 1283 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0, 1284 ctrl->info.size); 1285 } 1286 } 1287 1288 ctrl->cached = 1; 1289 return 0; 1290 } 1291 1292 static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain, 1293 struct uvc_control *ctrl) 1294 { 1295 u8 *data; 1296 int ret; 1297 1298 if (ctrl->loaded) 1299 return 0; 1300 1301 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT); 1302 1303 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) { 1304 memset(data, 0, ctrl->info.size); 1305 ctrl->loaded = 1; 1306 1307 return 0; 1308 } 1309 1310 if (ctrl->entity->get_cur) 1311 ret = ctrl->entity->get_cur(chain->dev, ctrl->entity, 1312 ctrl->info.selector, data, 1313 ctrl->info.size); 1314 else 1315 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, 1316 ctrl->entity->id, chain->dev->intfnum, 1317 ctrl->info.selector, data, 1318 ctrl->info.size); 1319 1320 if (ret < 0) 1321 return ret; 1322 1323 ctrl->loaded = 1; 1324 1325 return ret; 1326 } 1327 1328 static int __uvc_ctrl_get(struct uvc_video_chain *chain, 1329 struct uvc_control *ctrl, 1330 struct uvc_control_mapping *mapping, 1331 s32 *value) 1332 { 1333 int ret; 1334 1335 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) 1336 return -EACCES; 1337 1338 ret = __uvc_ctrl_load_cur(chain, ctrl); 1339 if (ret < 0) 1340 return ret; 1341 1342 *value = uvc_mapping_get_s32(mapping, UVC_GET_CUR, 1343 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); 1344 1345 return 0; 1346 } 1347 1348 static int __uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id, 1349 u32 found_id) 1350 { 1351 bool find_next = req_id & 1352 (V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND); 1353 unsigned int i; 1354 1355 req_id &= V4L2_CTRL_ID_MASK; 1356 1357 for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) { 1358 if (!(chain->ctrl_class_bitmap & BIT(i))) 1359 continue; 1360 if (!find_next) { 1361 if (uvc_control_classes[i] == req_id) 1362 return i; 1363 continue; 1364 } 1365 if (uvc_control_classes[i] > req_id && 1366 uvc_control_classes[i] < found_id) 1367 return i; 1368 } 1369 1370 return -ENODEV; 1371 } 1372 1373 static int uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id, 1374 u32 found_id, 1375 struct v4l2_query_ext_ctrl *v4l2_ctrl) 1376 { 1377 int idx; 1378 1379 idx = __uvc_query_v4l2_class(chain, req_id, found_id); 1380 if (idx < 0) 1381 return -ENODEV; 1382 1383 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl)); 1384 v4l2_ctrl->id = uvc_control_classes[idx]; 1385 strscpy(v4l2_ctrl->name, v4l2_ctrl_get_name(v4l2_ctrl->id), 1386 sizeof(v4l2_ctrl->name)); 1387 v4l2_ctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS; 1388 v4l2_ctrl->flags = V4L2_CTRL_FLAG_WRITE_ONLY 1389 | V4L2_CTRL_FLAG_READ_ONLY; 1390 return 0; 1391 } 1392 1393 static bool uvc_ctrl_is_readable(u32 which, struct uvc_control *ctrl, 1394 struct uvc_control_mapping *mapping) 1395 { 1396 if (which == V4L2_CTRL_WHICH_CUR_VAL) 1397 return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR); 1398 1399 if (which == V4L2_CTRL_WHICH_DEF_VAL) 1400 return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF); 1401 1402 /* Types with implicit boundaries. */ 1403 switch (mapping->v4l2_type) { 1404 case V4L2_CTRL_TYPE_MENU: 1405 case V4L2_CTRL_TYPE_BOOLEAN: 1406 case V4L2_CTRL_TYPE_BUTTON: 1407 return true; 1408 case V4L2_CTRL_TYPE_BITMASK: 1409 return (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) || 1410 (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX); 1411 default: 1412 break; 1413 } 1414 1415 if (which == V4L2_CTRL_WHICH_MIN_VAL) 1416 return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN); 1417 1418 if (which == V4L2_CTRL_WHICH_MAX_VAL) 1419 return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX); 1420 1421 return false; 1422 } 1423 1424 /* 1425 * Check if control @v4l2_id can be accessed by the given control @ioctl 1426 * (VIDIOC_G_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS or VIDIOC_S_EXT_CTRLS). 1427 * 1428 * For set operations on slave controls, check if the master's value is set to 1429 * manual, either in the others controls set in the same ioctl call, or from 1430 * the master's current value. This catches VIDIOC_S_EXT_CTRLS calls that set 1431 * both the master and slave control, such as for instance setting 1432 * auto_exposure=1, exposure_time_absolute=251. 1433 */ 1434 int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id, 1435 const struct v4l2_ext_controls *ctrls, u32 which, 1436 unsigned long ioctl) 1437 { 1438 struct uvc_control_mapping *master_map = NULL; 1439 struct uvc_control *master_ctrl = NULL; 1440 struct uvc_control_mapping *mapping; 1441 struct uvc_control *ctrl; 1442 s32 val; 1443 int ret; 1444 int i; 1445 /* 1446 * There is no need to check the ioctl, all the ioctls except 1447 * VIDIOC_G_EXT_CTRLS use which=V4L2_CTRL_WHICH_CUR_VAL. 1448 */ 1449 bool is_which_min_max = which == V4L2_CTRL_WHICH_MIN_VAL || 1450 which == V4L2_CTRL_WHICH_MAX_VAL; 1451 1452 if (__uvc_query_v4l2_class(chain, v4l2_id, 0) >= 0) 1453 return is_which_min_max ? -EINVAL : -EACCES; 1454 1455 ctrl = uvc_find_control(chain, v4l2_id, &mapping); 1456 if (!ctrl) 1457 return -EINVAL; 1458 1459 if ((!(ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) || 1460 !(ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)) && is_which_min_max) 1461 return -EINVAL; 1462 1463 if (ioctl == VIDIOC_G_EXT_CTRLS) 1464 return uvc_ctrl_is_readable(ctrls->which, ctrl, mapping); 1465 1466 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR)) 1467 return -EACCES; 1468 1469 if (ioctl != VIDIOC_S_EXT_CTRLS || !mapping->master_id) 1470 return 0; 1471 1472 /* 1473 * Iterate backwards in cases where the master control is accessed 1474 * multiple times in the same ioctl. We want the last value. 1475 */ 1476 for (i = ctrls->count - 1; i >= 0; i--) { 1477 if (ctrls->controls[i].id == mapping->master_id) 1478 return ctrls->controls[i].value == 1479 mapping->master_manual ? 0 : -EACCES; 1480 } 1481 1482 __uvc_find_control(ctrl->entity, mapping->master_id, &master_map, 1483 &master_ctrl, 0, 0); 1484 1485 if (!master_ctrl || !(master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) 1486 return 0; 1487 if (WARN_ON(uvc_ctrl_mapping_is_compound(master_map))) 1488 return -EIO; 1489 1490 ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val); 1491 if (ret >= 0 && val != mapping->master_manual) 1492 return -EACCES; 1493 1494 return 0; 1495 } 1496 1497 static const char *uvc_map_get_name(const struct uvc_control_mapping *map) 1498 { 1499 const char *name; 1500 1501 if (map->name) 1502 return map->name; 1503 1504 name = v4l2_ctrl_get_name(map->id); 1505 if (name) 1506 return name; 1507 1508 return "Unknown Control"; 1509 } 1510 1511 static u32 uvc_get_ctrl_bitmap(struct uvc_control *ctrl, 1512 struct uvc_control_mapping *mapping) 1513 { 1514 /* 1515 * Some controls, like CT_AE_MODE_CONTROL, use GET_RES to represent 1516 * the number of bits supported. Those controls do not list GET_MAX 1517 * as supported. 1518 */ 1519 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) 1520 return uvc_mapping_get_s32(mapping, UVC_GET_RES, 1521 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); 1522 1523 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) 1524 return uvc_mapping_get_s32(mapping, UVC_GET_MAX, 1525 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX)); 1526 1527 return ~0; 1528 } 1529 1530 static bool uvc_ctrl_is_relative_ptz(__u32 ctrl_id) 1531 { 1532 switch (ctrl_id) { 1533 case V4L2_CID_ZOOM_CONTINUOUS: 1534 case V4L2_CID_PAN_SPEED: 1535 case V4L2_CID_TILT_SPEED: 1536 return true; 1537 } 1538 return false; 1539 } 1540 1541 /* 1542 * Maximum retry count to avoid spurious errors with controls. Increasing this 1543 * value does no seem to produce better results in the tested hardware. 1544 */ 1545 #define MAX_QUERY_RETRIES 2 1546 1547 static int __uvc_queryctrl_boundaries(struct uvc_video_chain *chain, 1548 struct uvc_control *ctrl, 1549 struct uvc_control_mapping *mapping, 1550 struct v4l2_query_ext_ctrl *v4l2_ctrl) 1551 { 1552 if (!ctrl->cached) { 1553 unsigned int retries; 1554 int ret; 1555 1556 for (retries = 0; retries < MAX_QUERY_RETRIES; retries++) { 1557 ret = uvc_ctrl_populate_cache(chain, ctrl); 1558 if (ret != -EIO) 1559 break; 1560 } 1561 1562 if (ret) 1563 return ret; 1564 } 1565 1566 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) { 1567 v4l2_ctrl->default_value = uvc_mapping_get_s32(mapping, 1568 UVC_GET_DEF, uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF)); 1569 } 1570 1571 switch (mapping->v4l2_type) { 1572 case V4L2_CTRL_TYPE_MENU: 1573 v4l2_ctrl->minimum = ffs(mapping->menu_mask) - 1; 1574 v4l2_ctrl->maximum = fls(mapping->menu_mask) - 1; 1575 v4l2_ctrl->step = 1; 1576 return 0; 1577 1578 case V4L2_CTRL_TYPE_BOOLEAN: 1579 v4l2_ctrl->minimum = 0; 1580 v4l2_ctrl->maximum = 1; 1581 v4l2_ctrl->step = 1; 1582 return 0; 1583 1584 case V4L2_CTRL_TYPE_BUTTON: 1585 v4l2_ctrl->minimum = 0; 1586 v4l2_ctrl->maximum = 0; 1587 v4l2_ctrl->step = 0; 1588 return 0; 1589 1590 case V4L2_CTRL_TYPE_BITMASK: 1591 v4l2_ctrl->minimum = 0; 1592 v4l2_ctrl->maximum = uvc_get_ctrl_bitmap(ctrl, mapping); 1593 v4l2_ctrl->step = 0; 1594 return 0; 1595 1596 default: 1597 break; 1598 } 1599 1600 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) 1601 v4l2_ctrl->maximum = uvc_mapping_get_s32(mapping, UVC_GET_MAX, 1602 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX)); 1603 else 1604 v4l2_ctrl->maximum = 0; 1605 1606 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) { 1607 /* 1608 * For relative PTZ controls, UVC_GET_MIN for 1609 * b(Pan|Tilt|Zoom)Speed returns the minimum speed of the 1610 * movement in direction specified in the sign field. 1611 * See in USB Device Class Definition for Video Devices: 1612 * 4.2.2.1.13 Zoom (Relative) Control 1613 * 4.2.2.1.15 PanTilt (Relative) Control 1614 * 1615 * For minimum value, use maximum speed but in negative direction. 1616 */ 1617 if (uvc_ctrl_is_relative_ptz(v4l2_ctrl->id)) 1618 v4l2_ctrl->minimum = -v4l2_ctrl->maximum; 1619 else 1620 v4l2_ctrl->minimum = uvc_mapping_get_s32(mapping, 1621 UVC_GET_MIN, uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN)); 1622 } else { 1623 v4l2_ctrl->minimum = 0; 1624 } 1625 1626 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) 1627 v4l2_ctrl->step = uvc_mapping_get_s32(mapping, UVC_GET_RES, 1628 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); 1629 else 1630 v4l2_ctrl->step = 0; 1631 1632 return 0; 1633 } 1634 1635 static size_t uvc_mapping_v4l2_size(struct uvc_control_mapping *mapping) 1636 { 1637 if (mapping->v4l2_type == V4L2_CTRL_TYPE_RECT) 1638 return sizeof(struct v4l2_rect); 1639 1640 if (uvc_ctrl_mapping_is_compound(mapping)) 1641 return DIV_ROUND_UP(mapping->size, 8); 1642 1643 return sizeof(s32); 1644 } 1645 1646 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, 1647 struct uvc_control *ctrl, 1648 struct uvc_control_mapping *mapping, 1649 struct v4l2_query_ext_ctrl *v4l2_ctrl) 1650 { 1651 struct uvc_control_mapping *master_map = NULL; 1652 struct uvc_control *master_ctrl = NULL; 1653 int ret; 1654 1655 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl)); 1656 v4l2_ctrl->id = mapping->id; 1657 v4l2_ctrl->type = mapping->v4l2_type; 1658 strscpy(v4l2_ctrl->name, uvc_map_get_name(mapping), 1659 sizeof(v4l2_ctrl->name)); 1660 v4l2_ctrl->flags = 0; 1661 1662 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) 1663 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY; 1664 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR)) 1665 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; 1666 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) && 1667 (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)) 1668 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_HAS_WHICH_MIN_MAX; 1669 1670 if (mapping->master_id) 1671 __uvc_find_control(ctrl->entity, mapping->master_id, 1672 &master_map, &master_ctrl, 0, 0); 1673 if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) { 1674 unsigned int retries; 1675 s32 val; 1676 int ret; 1677 1678 if (WARN_ON(uvc_ctrl_mapping_is_compound(master_map))) 1679 return -EIO; 1680 1681 for (retries = 0; retries < MAX_QUERY_RETRIES; retries++) { 1682 ret = __uvc_ctrl_get(chain, master_ctrl, master_map, 1683 &val); 1684 if (!ret) 1685 break; 1686 if (ret < 0 && ret != -EIO) 1687 return ret; 1688 } 1689 1690 if (ret == -EIO) { 1691 dev_warn_ratelimited(&chain->dev->intf->dev, 1692 "UVC non compliance: Error %d querying master control %x (%s)\n", 1693 ret, master_map->id, 1694 uvc_map_get_name(master_map)); 1695 } else { 1696 if (val != mapping->master_manual) 1697 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 1698 } 1699 } 1700 1701 v4l2_ctrl->elem_size = uvc_mapping_v4l2_size(mapping); 1702 v4l2_ctrl->elems = 1; 1703 1704 if (v4l2_ctrl->type >= V4L2_CTRL_COMPOUND_TYPES) { 1705 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_HAS_PAYLOAD; 1706 v4l2_ctrl->default_value = 0; 1707 v4l2_ctrl->minimum = 0; 1708 v4l2_ctrl->maximum = 0; 1709 v4l2_ctrl->step = 0; 1710 return 0; 1711 } 1712 1713 ret = __uvc_queryctrl_boundaries(chain, ctrl, mapping, v4l2_ctrl); 1714 if (ret && !mapping->disabled) { 1715 dev_warn(&chain->dev->intf->dev, 1716 "UVC non compliance: permanently disabling control %x (%s), due to error %d\n", 1717 mapping->id, uvc_map_get_name(mapping), ret); 1718 mapping->disabled = true; 1719 } 1720 1721 if (mapping->disabled) 1722 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED; 1723 1724 return 0; 1725 } 1726 1727 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, 1728 struct v4l2_query_ext_ctrl *v4l2_ctrl) 1729 { 1730 struct uvc_control *ctrl; 1731 struct uvc_control_mapping *mapping; 1732 int ret; 1733 1734 ret = mutex_lock_interruptible(&chain->ctrl_mutex); 1735 if (ret < 0) 1736 return -ERESTARTSYS; 1737 1738 /* Check if the ctrl is a know class */ 1739 if (!(v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL)) { 1740 ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, 0, v4l2_ctrl); 1741 if (!ret) 1742 goto done; 1743 } 1744 1745 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping); 1746 if (ctrl == NULL) { 1747 ret = -EINVAL; 1748 goto done; 1749 } 1750 1751 /* 1752 * If we're enumerating control with V4L2_CTRL_FLAG_NEXT_CTRL, check if 1753 * a class should be inserted between the previous control and the one 1754 * we have just found. 1755 */ 1756 if (v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL) { 1757 ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, mapping->id, 1758 v4l2_ctrl); 1759 if (!ret) 1760 goto done; 1761 } 1762 1763 ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl); 1764 done: 1765 mutex_unlock(&chain->ctrl_mutex); 1766 return ret; 1767 } 1768 1769 /* 1770 * Mapping V4L2 controls to UVC controls can be straightforward if done well. 1771 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some 1772 * must be grouped (for instance the Red Balance, Blue Balance and Do White 1773 * Balance V4L2 controls use the White Balance Component UVC control) or 1774 * otherwise translated. The approach we take here is to use a translation 1775 * table for the controls that can be mapped directly, and handle the others 1776 * manually. 1777 */ 1778 int uvc_query_v4l2_menu(struct uvc_video_chain *chain, 1779 struct v4l2_querymenu *query_menu) 1780 { 1781 struct uvc_control_mapping *mapping; 1782 struct uvc_control *ctrl; 1783 u32 index = query_menu->index; 1784 u32 id = query_menu->id; 1785 const char *name; 1786 int ret; 1787 1788 memset(query_menu, 0, sizeof(*query_menu)); 1789 query_menu->id = id; 1790 query_menu->index = index; 1791 1792 if (index >= BITS_PER_TYPE(mapping->menu_mask)) 1793 return -EINVAL; 1794 1795 ret = mutex_lock_interruptible(&chain->ctrl_mutex); 1796 if (ret < 0) 1797 return -ERESTARTSYS; 1798 1799 ctrl = uvc_find_control(chain, query_menu->id, &mapping); 1800 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) { 1801 ret = -EINVAL; 1802 goto done; 1803 } 1804 1805 if (!test_bit(query_menu->index, &mapping->menu_mask)) { 1806 ret = -EINVAL; 1807 goto done; 1808 } 1809 1810 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) { 1811 int mask; 1812 1813 if (!ctrl->cached) { 1814 ret = uvc_ctrl_populate_cache(chain, ctrl); 1815 if (ret < 0) 1816 goto done; 1817 } 1818 1819 mask = uvc_mapping_get_menu_value(mapping, query_menu->index); 1820 if (mask < 0) { 1821 ret = mask; 1822 goto done; 1823 } 1824 1825 if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & mask)) { 1826 ret = -EINVAL; 1827 goto done; 1828 } 1829 } 1830 1831 name = uvc_mapping_get_menu_name(mapping, query_menu->index); 1832 if (!name) { 1833 ret = -EINVAL; 1834 goto done; 1835 } 1836 1837 strscpy(query_menu->name, name, sizeof(query_menu->name)); 1838 1839 done: 1840 mutex_unlock(&chain->ctrl_mutex); 1841 return ret; 1842 } 1843 1844 /* -------------------------------------------------------------------------- 1845 * Ctrl event handling 1846 */ 1847 1848 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain, 1849 struct v4l2_event *ev, 1850 struct uvc_control *ctrl, 1851 struct uvc_control_mapping *mapping, 1852 s32 value, u32 changes) 1853 { 1854 struct v4l2_query_ext_ctrl v4l2_ctrl; 1855 1856 __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl); 1857 1858 memset(ev, 0, sizeof(*ev)); 1859 ev->type = V4L2_EVENT_CTRL; 1860 ev->id = v4l2_ctrl.id; 1861 ev->u.ctrl.value = value; 1862 ev->u.ctrl.changes = changes; 1863 ev->u.ctrl.type = v4l2_ctrl.type; 1864 ev->u.ctrl.flags = v4l2_ctrl.flags; 1865 ev->u.ctrl.minimum = v4l2_ctrl.minimum; 1866 ev->u.ctrl.maximum = v4l2_ctrl.maximum; 1867 ev->u.ctrl.step = v4l2_ctrl.step; 1868 ev->u.ctrl.default_value = v4l2_ctrl.default_value; 1869 } 1870 1871 /* 1872 * Send control change events to all subscribers for the @ctrl control. By 1873 * default the subscriber that generated the event, as identified by @handle, 1874 * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag. 1875 * @handle can be NULL for asynchronous events related to auto-update controls, 1876 * in which case all subscribers are notified. 1877 */ 1878 static void uvc_ctrl_send_event(struct uvc_video_chain *chain, 1879 struct uvc_fh *handle, struct uvc_control *ctrl, 1880 struct uvc_control_mapping *mapping, s32 value, u32 changes) 1881 { 1882 struct v4l2_fh *originator = handle ? &handle->vfh : NULL; 1883 struct v4l2_subscribed_event *sev; 1884 struct v4l2_event ev; 1885 1886 if (list_empty(&mapping->ev_subs)) 1887 return; 1888 1889 uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes); 1890 1891 list_for_each_entry(sev, &mapping->ev_subs, node) { 1892 if (sev->fh != originator || 1893 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) || 1894 (changes & V4L2_EVENT_CTRL_CH_FLAGS)) 1895 v4l2_event_queue_fh(sev->fh, &ev); 1896 } 1897 } 1898 1899 /* 1900 * Send control change events for the slave of the @master control identified 1901 * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that 1902 * generated the event and may be NULL for auto-update events. 1903 */ 1904 static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain, 1905 struct uvc_fh *handle, struct uvc_control *master, u32 slave_id) 1906 { 1907 struct uvc_control_mapping *mapping = NULL; 1908 struct uvc_control *ctrl = NULL; 1909 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS; 1910 s32 val = 0; 1911 1912 __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0, 0); 1913 if (ctrl == NULL) 1914 return; 1915 1916 if (uvc_ctrl_mapping_is_compound(mapping) || 1917 __uvc_ctrl_get(chain, ctrl, mapping, &val) == 0) 1918 changes |= V4L2_EVENT_CTRL_CH_VALUE; 1919 1920 uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes); 1921 } 1922 1923 static int uvc_ctrl_set_handle(struct uvc_control *ctrl, struct uvc_fh *handle) 1924 { 1925 int ret; 1926 1927 lockdep_assert_held(&handle->chain->ctrl_mutex); 1928 1929 if (ctrl->handle) { 1930 dev_warn_ratelimited(&handle->stream->dev->intf->dev, 1931 "UVC non compliance: Setting an async control with a pending operation."); 1932 1933 if (ctrl->handle == handle) 1934 return 0; 1935 1936 WARN_ON(!ctrl->handle->pending_async_ctrls); 1937 if (ctrl->handle->pending_async_ctrls) 1938 ctrl->handle->pending_async_ctrls--; 1939 ctrl->handle = handle; 1940 ctrl->handle->pending_async_ctrls++; 1941 return 0; 1942 } 1943 1944 ret = uvc_pm_get(handle->chain->dev); 1945 if (ret) 1946 return ret; 1947 1948 ctrl->handle = handle; 1949 ctrl->handle->pending_async_ctrls++; 1950 return 0; 1951 } 1952 1953 static int uvc_ctrl_clear_handle(struct uvc_control *ctrl) 1954 { 1955 lockdep_assert_held(&ctrl->handle->chain->ctrl_mutex); 1956 1957 if (WARN_ON(!ctrl->handle->pending_async_ctrls)) { 1958 ctrl->handle = NULL; 1959 return -EINVAL; 1960 } 1961 1962 ctrl->handle->pending_async_ctrls--; 1963 uvc_pm_put(ctrl->handle->chain->dev); 1964 ctrl->handle = NULL; 1965 return 0; 1966 } 1967 1968 void uvc_ctrl_status_event(struct uvc_video_chain *chain, 1969 struct uvc_control *ctrl, const u8 *data) 1970 { 1971 struct uvc_control_mapping *mapping; 1972 struct uvc_fh *handle; 1973 unsigned int i; 1974 1975 mutex_lock(&chain->ctrl_mutex); 1976 1977 /* Flush the control cache, the data might have changed. */ 1978 ctrl->loaded = 0; 1979 1980 handle = ctrl->handle; 1981 if (handle) 1982 uvc_ctrl_clear_handle(ctrl); 1983 1984 list_for_each_entry(mapping, &ctrl->info.mappings, list) { 1985 s32 value; 1986 1987 if (uvc_ctrl_mapping_is_compound(mapping)) 1988 value = 0; 1989 else 1990 value = uvc_mapping_get_s32(mapping, UVC_GET_CUR, data); 1991 1992 /* 1993 * handle may be NULL here if the device sends auto-update 1994 * events without a prior related control set from userspace. 1995 */ 1996 for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) { 1997 if (!mapping->slave_ids[i]) 1998 break; 1999 2000 uvc_ctrl_send_slave_event(chain, handle, ctrl, 2001 mapping->slave_ids[i]); 2002 } 2003 2004 uvc_ctrl_send_event(chain, handle, ctrl, mapping, value, 2005 V4L2_EVENT_CTRL_CH_VALUE); 2006 } 2007 2008 mutex_unlock(&chain->ctrl_mutex); 2009 } 2010 2011 static void uvc_ctrl_status_event_work(struct work_struct *work) 2012 { 2013 struct uvc_device *dev = container_of(work, struct uvc_device, 2014 async_ctrl.work); 2015 struct uvc_ctrl_work *w = &dev->async_ctrl; 2016 int ret; 2017 2018 uvc_ctrl_status_event(w->chain, w->ctrl, w->data); 2019 2020 /* The barrier is needed to synchronize with uvc_status_stop(). */ 2021 if (smp_load_acquire(&dev->flush_status)) 2022 return; 2023 2024 /* Resubmit the URB. */ 2025 w->urb->interval = dev->int_ep->desc.bInterval; 2026 ret = usb_submit_urb(w->urb, GFP_KERNEL); 2027 if (ret < 0) 2028 dev_err(&dev->intf->dev, 2029 "Failed to resubmit status URB (%d).\n", ret); 2030 } 2031 2032 bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain, 2033 struct uvc_control *ctrl, const u8 *data) 2034 { 2035 struct uvc_device *dev = chain->dev; 2036 struct uvc_ctrl_work *w = &dev->async_ctrl; 2037 2038 if (list_empty(&ctrl->info.mappings)) 2039 return false; 2040 2041 w->data = data; 2042 w->urb = urb; 2043 w->chain = chain; 2044 w->ctrl = ctrl; 2045 2046 schedule_work(&w->work); 2047 2048 return true; 2049 } 2050 2051 static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls, 2052 unsigned int xctrls_count, u32 id) 2053 { 2054 unsigned int i; 2055 2056 for (i = 0; i < xctrls_count; ++i) { 2057 if (xctrls[i].id == id) 2058 return true; 2059 } 2060 2061 return false; 2062 } 2063 2064 static void uvc_ctrl_send_events(struct uvc_fh *handle, 2065 struct uvc_entity *entity, 2066 const struct v4l2_ext_control *xctrls, 2067 unsigned int xctrls_count) 2068 { 2069 struct uvc_control_mapping *mapping; 2070 struct uvc_control *ctrl; 2071 unsigned int i; 2072 unsigned int j; 2073 2074 for (i = 0; i < xctrls_count; ++i) { 2075 u32 changes = V4L2_EVENT_CTRL_CH_VALUE; 2076 s32 value; 2077 2078 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping); 2079 if (ctrl->entity != entity) 2080 continue; 2081 2082 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS) 2083 /* Notification will be sent from an Interrupt event. */ 2084 continue; 2085 2086 for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) { 2087 u32 slave_id = mapping->slave_ids[j]; 2088 2089 if (!slave_id) 2090 break; 2091 2092 /* 2093 * We can skip sending an event for the slave if the 2094 * slave is being modified in the same transaction. 2095 */ 2096 if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count, 2097 slave_id)) 2098 continue; 2099 2100 uvc_ctrl_send_slave_event(handle->chain, handle, ctrl, 2101 slave_id); 2102 } 2103 2104 if (uvc_ctrl_mapping_is_compound(mapping)) 2105 value = 0; 2106 else 2107 value = xctrls[i].value; 2108 /* 2109 * If the master is being modified in the same transaction 2110 * flags may change too. 2111 */ 2112 if (mapping->master_id && 2113 uvc_ctrl_xctrls_has_control(xctrls, xctrls_count, 2114 mapping->master_id)) 2115 changes |= V4L2_EVENT_CTRL_CH_FLAGS; 2116 2117 uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping, 2118 value, changes); 2119 } 2120 } 2121 2122 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems) 2123 { 2124 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh); 2125 struct uvc_control_mapping *mapping; 2126 struct uvc_control *ctrl; 2127 int ret; 2128 2129 ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex); 2130 if (ret < 0) 2131 return -ERESTARTSYS; 2132 2133 if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) { 2134 ret = 0; 2135 goto done; 2136 } 2137 2138 ctrl = uvc_find_control(handle->chain, sev->id, &mapping); 2139 if (ctrl == NULL) { 2140 ret = -EINVAL; 2141 goto done; 2142 } 2143 2144 if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) { 2145 struct v4l2_event ev; 2146 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS; 2147 s32 val = 0; 2148 2149 ret = uvc_pm_get(handle->chain->dev); 2150 if (ret) 2151 goto done; 2152 2153 if (uvc_ctrl_mapping_is_compound(mapping) || 2154 __uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0) 2155 changes |= V4L2_EVENT_CTRL_CH_VALUE; 2156 2157 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val, 2158 changes); 2159 2160 uvc_pm_put(handle->chain->dev); 2161 2162 /* 2163 * Mark the queue as active, allowing this initial event to be 2164 * accepted. 2165 */ 2166 sev->elems = elems; 2167 v4l2_event_queue_fh(sev->fh, &ev); 2168 } 2169 2170 list_add_tail(&sev->node, &mapping->ev_subs); 2171 2172 done: 2173 mutex_unlock(&handle->chain->ctrl_mutex); 2174 return ret; 2175 } 2176 2177 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev) 2178 { 2179 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh); 2180 2181 mutex_lock(&handle->chain->ctrl_mutex); 2182 if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) 2183 goto done; 2184 list_del(&sev->node); 2185 done: 2186 mutex_unlock(&handle->chain->ctrl_mutex); 2187 } 2188 2189 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = { 2190 .add = uvc_ctrl_add_event, 2191 .del = uvc_ctrl_del_event, 2192 .replace = v4l2_ctrl_replace, 2193 .merge = v4l2_ctrl_merge, 2194 }; 2195 2196 /* -------------------------------------------------------------------------- 2197 * Control transactions 2198 * 2199 * To make extended set operations as atomic as the hardware allows, controls 2200 * are handled using begin/commit/rollback operations. 2201 * 2202 * At the beginning of a set request, uvc_ctrl_begin should be called to 2203 * initialize the request. This function acquires the control lock. 2204 * 2205 * When setting a control, the new value is stored in the control data field 2206 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for 2207 * later processing. If the UVC and V4L2 control sizes differ, the current 2208 * value is loaded from the hardware before storing the new value in the data 2209 * field. 2210 * 2211 * After processing all controls in the transaction, uvc_ctrl_commit or 2212 * uvc_ctrl_rollback must be called to apply the pending changes to the 2213 * hardware or revert them. When applying changes, all controls marked as 2214 * dirty will be modified in the UVC device, and the dirty flag will be 2215 * cleared. When reverting controls, the control data field 2216 * UVC_CTRL_DATA_CURRENT is reverted to its previous value 2217 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the 2218 * control lock. 2219 */ 2220 int uvc_ctrl_begin(struct uvc_video_chain *chain) 2221 { 2222 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0; 2223 } 2224 2225 /* 2226 * Returns the number of uvc controls that have been correctly set, or a 2227 * negative number if there has been an error. 2228 */ 2229 static int uvc_ctrl_commit_entity(struct uvc_device *dev, 2230 struct uvc_fh *handle, 2231 struct uvc_entity *entity, 2232 int rollback, 2233 struct uvc_control **err_ctrl) 2234 { 2235 unsigned int processed_ctrls = 0; 2236 struct uvc_control *ctrl; 2237 unsigned int i; 2238 int ret = 0; 2239 2240 if (entity == NULL) 2241 return 0; 2242 2243 for (i = 0; i < entity->ncontrols; ++i) { 2244 ctrl = &entity->controls[i]; 2245 if (!ctrl->initialized) 2246 continue; 2247 2248 /* 2249 * Reset the loaded flag for auto-update controls that were 2250 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent 2251 * uvc_ctrl_get from using the cached value, and for write-only 2252 * controls to prevent uvc_ctrl_set from setting bits not 2253 * explicitly set by the user. 2254 */ 2255 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE || 2256 !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) 2257 ctrl->loaded = 0; 2258 2259 if (!ctrl->dirty) 2260 continue; 2261 2262 if (!rollback) 2263 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id, 2264 dev->intfnum, ctrl->info.selector, 2265 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), 2266 ctrl->info.size); 2267 2268 if (!ret) 2269 processed_ctrls++; 2270 2271 if (rollback || ret < 0) 2272 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), 2273 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP), 2274 ctrl->info.size); 2275 2276 ctrl->dirty = 0; 2277 2278 if (!rollback && handle && !ret && 2279 ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS) 2280 ret = uvc_ctrl_set_handle(ctrl, handle); 2281 2282 if (ret < 0 && !rollback) { 2283 if (err_ctrl) 2284 *err_ctrl = ctrl; 2285 /* 2286 * If we fail to set a control, we need to rollback 2287 * the next ones. 2288 */ 2289 rollback = 1; 2290 } 2291 } 2292 2293 if (ret) 2294 return ret; 2295 2296 return processed_ctrls; 2297 } 2298 2299 static int uvc_ctrl_find_ctrl_idx(struct uvc_entity *entity, 2300 struct v4l2_ext_controls *ctrls, 2301 struct uvc_control *uvc_control) 2302 { 2303 struct uvc_control_mapping *mapping = NULL; 2304 struct uvc_control *ctrl_found = NULL; 2305 unsigned int i; 2306 2307 if (!entity) 2308 return ctrls->count; 2309 2310 for (i = 0; i < ctrls->count; i++) { 2311 __uvc_find_control(entity, ctrls->controls[i].id, &mapping, 2312 &ctrl_found, 0, 0); 2313 if (uvc_control == ctrl_found) 2314 return i; 2315 } 2316 2317 return ctrls->count; 2318 } 2319 2320 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback, 2321 struct v4l2_ext_controls *ctrls) 2322 { 2323 struct uvc_video_chain *chain = handle->chain; 2324 struct uvc_control *err_ctrl; 2325 struct uvc_entity *entity; 2326 int ret_out = 0; 2327 int ret; 2328 2329 /* Find the control. */ 2330 list_for_each_entry(entity, &chain->entities, chain) { 2331 ret = uvc_ctrl_commit_entity(chain->dev, handle, entity, 2332 rollback, &err_ctrl); 2333 if (ret < 0) { 2334 if (ctrls) 2335 ctrls->error_idx = 2336 uvc_ctrl_find_ctrl_idx(entity, ctrls, 2337 err_ctrl); 2338 /* 2339 * When we fail to commit an entity, we need to 2340 * restore the UVC_CTRL_DATA_BACKUP for all the 2341 * controls in the other entities, otherwise our cache 2342 * and the hardware will be out of sync. 2343 */ 2344 rollback = 1; 2345 2346 ret_out = ret; 2347 } else if (ret > 0 && !rollback) { 2348 uvc_ctrl_send_events(handle, entity, 2349 ctrls->controls, ctrls->count); 2350 } 2351 } 2352 2353 mutex_unlock(&chain->ctrl_mutex); 2354 return ret_out; 2355 } 2356 2357 static int uvc_mapping_get_xctrl_compound(struct uvc_video_chain *chain, 2358 struct uvc_control *ctrl, 2359 struct uvc_control_mapping *mapping, 2360 u32 which, 2361 struct v4l2_ext_control *xctrl) 2362 { 2363 u8 *data __free(kfree) = NULL; 2364 size_t size; 2365 u8 query; 2366 int ret; 2367 int id; 2368 2369 switch (which) { 2370 case V4L2_CTRL_WHICH_CUR_VAL: 2371 id = UVC_CTRL_DATA_CURRENT; 2372 query = UVC_GET_CUR; 2373 break; 2374 case V4L2_CTRL_WHICH_MIN_VAL: 2375 id = UVC_CTRL_DATA_MIN; 2376 query = UVC_GET_MIN; 2377 break; 2378 case V4L2_CTRL_WHICH_MAX_VAL: 2379 id = UVC_CTRL_DATA_MAX; 2380 query = UVC_GET_MAX; 2381 break; 2382 case V4L2_CTRL_WHICH_DEF_VAL: 2383 id = UVC_CTRL_DATA_DEF; 2384 query = UVC_GET_DEF; 2385 break; 2386 default: 2387 return -EINVAL; 2388 } 2389 2390 size = uvc_mapping_v4l2_size(mapping); 2391 if (xctrl->size < size) { 2392 xctrl->size = size; 2393 return -ENOSPC; 2394 } 2395 2396 data = kmalloc(size, GFP_KERNEL); 2397 if (!data) 2398 return -ENOMEM; 2399 2400 if (which == V4L2_CTRL_WHICH_CUR_VAL) 2401 ret = __uvc_ctrl_load_cur(chain, ctrl); 2402 else 2403 ret = uvc_ctrl_populate_cache(chain, ctrl); 2404 2405 if (ret < 0) 2406 return ret; 2407 2408 ret = mapping->get(mapping, query, uvc_ctrl_data(ctrl, id), size, data); 2409 if (ret < 0) 2410 return ret; 2411 2412 /* 2413 * v4l2_ext_control does not have enough room to fit a compound control. 2414 * Instead, the value is in the user memory at xctrl->ptr. The v4l2 2415 * ioctl helper does not copy it for us. 2416 */ 2417 return copy_to_user(xctrl->ptr, data, size) ? -EFAULT : 0; 2418 } 2419 2420 static int uvc_mapping_get_xctrl_std(struct uvc_video_chain *chain, 2421 struct uvc_control *ctrl, 2422 struct uvc_control_mapping *mapping, 2423 u32 which, struct v4l2_ext_control *xctrl) 2424 { 2425 struct v4l2_query_ext_ctrl qec; 2426 int ret; 2427 2428 switch (which) { 2429 case V4L2_CTRL_WHICH_CUR_VAL: 2430 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value); 2431 case V4L2_CTRL_WHICH_DEF_VAL: 2432 case V4L2_CTRL_WHICH_MIN_VAL: 2433 case V4L2_CTRL_WHICH_MAX_VAL: 2434 break; 2435 default: 2436 return -EINVAL; 2437 } 2438 2439 ret = __uvc_queryctrl_boundaries(chain, ctrl, mapping, &qec); 2440 if (ret < 0) 2441 return ret; 2442 2443 switch (which) { 2444 case V4L2_CTRL_WHICH_DEF_VAL: 2445 xctrl->value = qec.default_value; 2446 break; 2447 case V4L2_CTRL_WHICH_MIN_VAL: 2448 xctrl->value = qec.minimum; 2449 break; 2450 case V4L2_CTRL_WHICH_MAX_VAL: 2451 xctrl->value = qec.maximum; 2452 break; 2453 } 2454 2455 return 0; 2456 } 2457 2458 static int uvc_mapping_get_xctrl(struct uvc_video_chain *chain, 2459 struct uvc_control *ctrl, 2460 struct uvc_control_mapping *mapping, 2461 u32 which, struct v4l2_ext_control *xctrl) 2462 { 2463 if (uvc_ctrl_mapping_is_compound(mapping)) 2464 return uvc_mapping_get_xctrl_compound(chain, ctrl, mapping, 2465 which, xctrl); 2466 return uvc_mapping_get_xctrl_std(chain, ctrl, mapping, which, xctrl); 2467 } 2468 2469 int uvc_ctrl_get(struct uvc_video_chain *chain, u32 which, 2470 struct v4l2_ext_control *xctrl) 2471 { 2472 struct uvc_control *ctrl; 2473 struct uvc_control_mapping *mapping; 2474 2475 if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0) 2476 return -EACCES; 2477 2478 ctrl = uvc_find_control(chain, xctrl->id, &mapping); 2479 if (!ctrl) 2480 return -EINVAL; 2481 2482 return uvc_mapping_get_xctrl(chain, ctrl, mapping, which, xctrl); 2483 } 2484 2485 static int uvc_ctrl_clamp(struct uvc_video_chain *chain, 2486 struct uvc_control *ctrl, 2487 u32 v4l2_id, 2488 struct uvc_control_mapping *mapping, 2489 s32 *value_in_out) 2490 { 2491 s32 value = *value_in_out; 2492 u32 step; 2493 s32 min; 2494 s32 max; 2495 int ret; 2496 2497 switch (mapping->v4l2_type) { 2498 case V4L2_CTRL_TYPE_INTEGER: 2499 if (!ctrl->cached) { 2500 ret = uvc_ctrl_populate_cache(chain, ctrl); 2501 if (ret < 0) 2502 return ret; 2503 } 2504 2505 max = uvc_mapping_get_s32(mapping, UVC_GET_MAX, 2506 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX)); 2507 /* 2508 * For relative PTZ controls, UVC_GET_MIN for 2509 * b(Pan|Tilt|Zoom)Speed returns the minimum speed of the 2510 * movement in direction specified in the sign field. 2511 * See in USB Device Class Definition for Video Devices: 2512 * 4.2.2.1.13 Zoom (Relative) Control 2513 * 4.2.2.1.15 PanTilt (Relative) Control 2514 * 2515 * For minimum value, use maximum speed but in negative direction. 2516 */ 2517 if (uvc_ctrl_is_relative_ptz(v4l2_id)) 2518 min = -max; 2519 else 2520 min = uvc_mapping_get_s32(mapping, UVC_GET_MIN, 2521 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN)); 2522 2523 step = uvc_mapping_get_s32(mapping, UVC_GET_RES, 2524 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); 2525 if (step == 0) 2526 step = 1; 2527 2528 value = min + DIV_ROUND_CLOSEST((u32)(value - min), step) * step; 2529 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED) 2530 value = clamp(value, min, max); 2531 else 2532 value = clamp_t(u32, value, min, max); 2533 *value_in_out = value; 2534 return 0; 2535 2536 case V4L2_CTRL_TYPE_BITMASK: 2537 if (!ctrl->cached) { 2538 ret = uvc_ctrl_populate_cache(chain, ctrl); 2539 if (ret < 0) 2540 return ret; 2541 } 2542 2543 value &= uvc_get_ctrl_bitmap(ctrl, mapping); 2544 *value_in_out = value; 2545 return 0; 2546 2547 case V4L2_CTRL_TYPE_BOOLEAN: 2548 *value_in_out = clamp(value, 0, 1); 2549 return 0; 2550 2551 case V4L2_CTRL_TYPE_MENU: 2552 if (value < (ffs(mapping->menu_mask) - 1) || 2553 value > (fls(mapping->menu_mask) - 1)) 2554 return -ERANGE; 2555 2556 if (!test_bit(value, &mapping->menu_mask)) 2557 return -EINVAL; 2558 2559 /* 2560 * Valid menu indices are reported by the GET_RES request for 2561 * UVC controls that support it. 2562 */ 2563 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) { 2564 int val = uvc_mapping_get_menu_value(mapping, value); 2565 if (!ctrl->cached) { 2566 ret = uvc_ctrl_populate_cache(chain, ctrl); 2567 if (ret < 0) 2568 return ret; 2569 } 2570 2571 if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & val)) 2572 return -EINVAL; 2573 } 2574 return 0; 2575 2576 default: 2577 return 0; 2578 } 2579 2580 return 0; 2581 } 2582 2583 static int uvc_mapping_set_xctrl_compound(struct uvc_control *ctrl, 2584 struct uvc_control_mapping *mapping, 2585 struct v4l2_ext_control *xctrl) 2586 { 2587 u8 *data __free(kfree) = NULL; 2588 size_t size = uvc_mapping_v4l2_size(mapping); 2589 2590 if (xctrl->size != size) 2591 return -EINVAL; 2592 2593 /* 2594 * v4l2_ext_control does not have enough room to fit a compound control. 2595 * Instead, the value is in the user memory at xctrl->ptr. The v4l2 2596 * ioctl helper does not copy it for us. 2597 */ 2598 data = memdup_user(xctrl->ptr, size); 2599 if (IS_ERR(data)) 2600 return PTR_ERR(data); 2601 2602 return mapping->set(mapping, size, data, 2603 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); 2604 } 2605 2606 static int uvc_mapping_set_xctrl(struct uvc_control *ctrl, 2607 struct uvc_control_mapping *mapping, 2608 struct v4l2_ext_control *xctrl) 2609 { 2610 if (uvc_ctrl_mapping_is_compound(mapping)) 2611 return uvc_mapping_set_xctrl_compound(ctrl, mapping, xctrl); 2612 2613 uvc_mapping_set_s32(mapping, xctrl->value, 2614 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); 2615 return 0; 2616 } 2617 2618 int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl) 2619 { 2620 struct uvc_video_chain *chain = handle->chain; 2621 struct uvc_control_mapping *mapping; 2622 struct uvc_control *ctrl; 2623 int ret; 2624 2625 lockdep_assert_held(&chain->ctrl_mutex); 2626 2627 if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0) 2628 return -EACCES; 2629 2630 ctrl = uvc_find_control(chain, xctrl->id, &mapping); 2631 if (!ctrl) 2632 return -EINVAL; 2633 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR)) 2634 return -EACCES; 2635 2636 ret = uvc_ctrl_clamp(chain, ctrl, xctrl->id, mapping, &xctrl->value); 2637 if (ret) 2638 return ret; 2639 /* 2640 * If the mapping doesn't span the whole UVC control, the current value 2641 * needs to be loaded from the device to perform the read-modify-write 2642 * operation. 2643 */ 2644 if ((ctrl->info.size * 8) != mapping->size) { 2645 ret = __uvc_ctrl_load_cur(chain, ctrl); 2646 if (ret < 0) 2647 return ret; 2648 } 2649 2650 /* Backup the current value in case we need to rollback later. */ 2651 if (!ctrl->dirty) { 2652 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP), 2653 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), 2654 ctrl->info.size); 2655 } 2656 2657 ret = uvc_mapping_set_xctrl(ctrl, mapping, xctrl); 2658 if (ret) 2659 return ret; 2660 2661 ctrl->dirty = 1; 2662 ctrl->modified = 1; 2663 return 0; 2664 } 2665 2666 /* -------------------------------------------------------------------------- 2667 * Dynamic controls 2668 */ 2669 2670 /* 2671 * Retrieve flags for a given control 2672 */ 2673 static int uvc_ctrl_get_flags(struct uvc_device *dev, 2674 const struct uvc_control *ctrl, 2675 struct uvc_control_info *info) 2676 { 2677 u8 *data; 2678 int ret; 2679 2680 data = kmalloc(1, GFP_KERNEL); 2681 if (data == NULL) 2682 return -ENOMEM; 2683 2684 if (ctrl->entity->get_info) 2685 ret = ctrl->entity->get_info(dev, ctrl->entity, 2686 ctrl->info.selector, data); 2687 else 2688 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, 2689 dev->intfnum, info->selector, data, 1); 2690 2691 if (!ret) { 2692 info->flags &= ~(UVC_CTRL_FLAG_GET_CUR | 2693 UVC_CTRL_FLAG_SET_CUR | 2694 UVC_CTRL_FLAG_AUTO_UPDATE | 2695 UVC_CTRL_FLAG_ASYNCHRONOUS); 2696 2697 info->flags |= (data[0] & UVC_CONTROL_CAP_GET ? 2698 UVC_CTRL_FLAG_GET_CUR : 0) 2699 | (data[0] & UVC_CONTROL_CAP_SET ? 2700 UVC_CTRL_FLAG_SET_CUR : 0) 2701 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ? 2702 UVC_CTRL_FLAG_AUTO_UPDATE : 0) 2703 | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ? 2704 UVC_CTRL_FLAG_ASYNCHRONOUS : 0); 2705 } 2706 2707 kfree(data); 2708 return ret; 2709 } 2710 2711 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev, 2712 const struct uvc_control *ctrl, struct uvc_control_info *info) 2713 { 2714 struct uvc_ctrl_fixup { 2715 struct usb_device_id id; 2716 u8 entity; 2717 u8 selector; 2718 u8 flags; 2719 }; 2720 2721 static const struct uvc_ctrl_fixup fixups[] = { 2722 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1, 2723 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX | 2724 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR | 2725 UVC_CTRL_FLAG_AUTO_UPDATE }, 2726 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1, 2727 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX | 2728 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR | 2729 UVC_CTRL_FLAG_AUTO_UPDATE }, 2730 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1, 2731 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX | 2732 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR | 2733 UVC_CTRL_FLAG_AUTO_UPDATE }, 2734 }; 2735 2736 unsigned int i; 2737 2738 for (i = 0; i < ARRAY_SIZE(fixups); ++i) { 2739 if (!usb_match_one_id(dev->intf, &fixups[i].id)) 2740 continue; 2741 2742 if (fixups[i].entity == ctrl->entity->id && 2743 fixups[i].selector == info->selector) { 2744 info->flags = fixups[i].flags; 2745 return; 2746 } 2747 } 2748 } 2749 2750 /* 2751 * Query control information (size and flags) for XU controls. 2752 */ 2753 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev, 2754 const struct uvc_control *ctrl, struct uvc_control_info *info) 2755 { 2756 u8 *data; 2757 int ret; 2758 2759 data = kmalloc(2, GFP_KERNEL); 2760 if (data == NULL) 2761 return -ENOMEM; 2762 2763 memcpy(info->entity, ctrl->entity->guid, sizeof(info->entity)); 2764 info->index = ctrl->index; 2765 info->selector = ctrl->index + 1; 2766 2767 /* Query and verify the control length (GET_LEN) */ 2768 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum, 2769 info->selector, data, 2); 2770 if (ret < 0) { 2771 uvc_dbg(dev, CONTROL, 2772 "GET_LEN failed on control %pUl/%u (%d)\n", 2773 info->entity, info->selector, ret); 2774 goto done; 2775 } 2776 2777 info->size = le16_to_cpup((__le16 *)data); 2778 2779 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX 2780 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF; 2781 2782 ret = uvc_ctrl_get_flags(dev, ctrl, info); 2783 if (ret < 0) { 2784 uvc_dbg(dev, CONTROL, 2785 "Failed to get flags for control %pUl/%u (%d)\n", 2786 info->entity, info->selector, ret); 2787 goto done; 2788 } 2789 2790 uvc_ctrl_fixup_xu_info(dev, ctrl, info); 2791 2792 uvc_dbg(dev, CONTROL, 2793 "XU control %pUl/%u queried: len %u, flags { get %u set %u auto %u }\n", 2794 info->entity, info->selector, info->size, 2795 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0, 2796 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0, 2797 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0); 2798 2799 done: 2800 kfree(data); 2801 return ret; 2802 } 2803 2804 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl, 2805 const struct uvc_control_info *info); 2806 2807 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev, 2808 struct uvc_control *ctrl) 2809 { 2810 struct uvc_control_info info; 2811 int ret; 2812 2813 if (ctrl->initialized) 2814 return 0; 2815 2816 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info); 2817 if (ret < 0) 2818 return ret; 2819 2820 ret = uvc_ctrl_add_info(dev, ctrl, &info); 2821 if (ret < 0) 2822 uvc_dbg(dev, CONTROL, 2823 "Failed to initialize control %pUl/%u on device %s entity %u\n", 2824 info.entity, info.selector, dev->udev->devpath, 2825 ctrl->entity->id); 2826 2827 return ret; 2828 } 2829 2830 int uvc_xu_ctrl_query(struct uvc_video_chain *chain, 2831 struct uvc_xu_control_query *xqry) 2832 { 2833 struct uvc_entity *entity, *iter; 2834 struct uvc_control *ctrl; 2835 unsigned int i; 2836 bool found; 2837 u32 reqflags; 2838 u16 size; 2839 u8 *data = NULL; 2840 int ret; 2841 2842 /* Find the extension unit. */ 2843 entity = NULL; 2844 list_for_each_entry(iter, &chain->entities, chain) { 2845 if (UVC_ENTITY_TYPE(iter) == UVC_VC_EXTENSION_UNIT && 2846 iter->id == xqry->unit) { 2847 entity = iter; 2848 break; 2849 } 2850 } 2851 2852 if (!entity) { 2853 uvc_dbg(chain->dev, CONTROL, "Extension unit %u not found\n", 2854 xqry->unit); 2855 return -ENOENT; 2856 } 2857 2858 /* Find the control and perform delayed initialization if needed. */ 2859 found = false; 2860 for (i = 0; i < entity->ncontrols; ++i) { 2861 ctrl = &entity->controls[i]; 2862 if (ctrl->index == xqry->selector - 1) { 2863 found = true; 2864 break; 2865 } 2866 } 2867 2868 if (!found) { 2869 uvc_dbg(chain->dev, CONTROL, "Control %pUl/%u not found\n", 2870 entity->guid, xqry->selector); 2871 return -ENOENT; 2872 } 2873 2874 if (mutex_lock_interruptible(&chain->ctrl_mutex)) 2875 return -ERESTARTSYS; 2876 2877 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl); 2878 if (ret < 0) { 2879 ret = -ENOENT; 2880 goto done; 2881 } 2882 2883 /* Validate the required buffer size and flags for the request */ 2884 reqflags = 0; 2885 size = ctrl->info.size; 2886 2887 switch (xqry->query) { 2888 case UVC_GET_CUR: 2889 reqflags = UVC_CTRL_FLAG_GET_CUR; 2890 break; 2891 case UVC_GET_MIN: 2892 reqflags = UVC_CTRL_FLAG_GET_MIN; 2893 break; 2894 case UVC_GET_MAX: 2895 reqflags = UVC_CTRL_FLAG_GET_MAX; 2896 break; 2897 case UVC_GET_DEF: 2898 reqflags = UVC_CTRL_FLAG_GET_DEF; 2899 break; 2900 case UVC_GET_RES: 2901 reqflags = UVC_CTRL_FLAG_GET_RES; 2902 break; 2903 case UVC_SET_CUR: 2904 reqflags = UVC_CTRL_FLAG_SET_CUR; 2905 break; 2906 case UVC_GET_LEN: 2907 size = 2; 2908 break; 2909 case UVC_GET_INFO: 2910 size = 1; 2911 break; 2912 default: 2913 ret = -EINVAL; 2914 goto done; 2915 } 2916 2917 if (size != xqry->size) { 2918 ret = -ENOBUFS; 2919 goto done; 2920 } 2921 2922 if (reqflags && !(ctrl->info.flags & reqflags)) { 2923 ret = -EBADRQC; 2924 goto done; 2925 } 2926 2927 data = kmalloc(size, GFP_KERNEL); 2928 if (data == NULL) { 2929 ret = -ENOMEM; 2930 goto done; 2931 } 2932 2933 if (xqry->query == UVC_SET_CUR && 2934 copy_from_user(data, xqry->data, size)) { 2935 ret = -EFAULT; 2936 goto done; 2937 } 2938 2939 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit, 2940 chain->dev->intfnum, xqry->selector, data, size); 2941 if (ret < 0) 2942 goto done; 2943 2944 if (xqry->query != UVC_SET_CUR && 2945 copy_to_user(xqry->data, data, size)) 2946 ret = -EFAULT; 2947 done: 2948 kfree(data); 2949 mutex_unlock(&chain->ctrl_mutex); 2950 return ret; 2951 } 2952 2953 /* -------------------------------------------------------------------------- 2954 * Suspend/resume 2955 */ 2956 2957 /* 2958 * Restore control values after resume, skipping controls that haven't been 2959 * changed. 2960 * 2961 * TODO 2962 * - Don't restore modified controls that are back to their default value. 2963 * - Handle restore order (Auto-Exposure Mode should be restored before 2964 * Exposure Time). 2965 */ 2966 int uvc_ctrl_restore_values(struct uvc_device *dev) 2967 { 2968 struct uvc_control *ctrl; 2969 struct uvc_entity *entity; 2970 unsigned int i; 2971 int ret; 2972 2973 /* Walk the entities list and restore controls when possible. */ 2974 list_for_each_entry(entity, &dev->entities, list) { 2975 2976 for (i = 0; i < entity->ncontrols; ++i) { 2977 ctrl = &entity->controls[i]; 2978 2979 if (!ctrl->initialized || !ctrl->modified || 2980 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0) 2981 continue; 2982 uvc_dbg(dev, CONTROL, "restoring control %pUl/%u/%u\n", 2983 ctrl->info.entity, ctrl->info.index, 2984 ctrl->info.selector); 2985 ctrl->dirty = 1; 2986 } 2987 2988 ret = uvc_ctrl_commit_entity(dev, NULL, entity, 0, NULL); 2989 if (ret < 0) 2990 return ret; 2991 } 2992 2993 return 0; 2994 } 2995 2996 /* -------------------------------------------------------------------------- 2997 * Control and mapping handling 2998 */ 2999 3000 /* 3001 * Add control information to a given control. 3002 */ 3003 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl, 3004 const struct uvc_control_info *info) 3005 { 3006 ctrl->info = *info; 3007 INIT_LIST_HEAD(&ctrl->info.mappings); 3008 3009 /* Allocate an array to save control values (cur, def, max, etc.) */ 3010 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1, 3011 GFP_KERNEL); 3012 if (!ctrl->uvc_data) 3013 return -ENOMEM; 3014 3015 ctrl->initialized = 1; 3016 3017 uvc_dbg(dev, CONTROL, "Added control %pUl/%u to device %s entity %u\n", 3018 ctrl->info.entity, ctrl->info.selector, dev->udev->devpath, 3019 ctrl->entity->id); 3020 3021 return 0; 3022 } 3023 3024 /* 3025 * Add a control mapping to a given control. 3026 */ 3027 static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain, 3028 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping) 3029 { 3030 struct uvc_control_mapping *map; 3031 unsigned int size; 3032 unsigned int i; 3033 int ret; 3034 3035 /* 3036 * Most mappings come from static kernel data, and need to be duplicated. 3037 * Mappings that come from userspace will be unnecessarily duplicated, 3038 * this could be optimized. 3039 */ 3040 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL); 3041 if (!map) 3042 return -ENOMEM; 3043 3044 map->name = NULL; 3045 map->menu_names = NULL; 3046 map->menu_mapping = NULL; 3047 3048 /* For UVCIOC_CTRL_MAP custom control */ 3049 if (mapping->name) { 3050 map->name = kstrdup(mapping->name, GFP_KERNEL); 3051 if (!map->name) 3052 goto err_nomem; 3053 } 3054 3055 INIT_LIST_HEAD(&map->ev_subs); 3056 3057 if (mapping->menu_mapping && mapping->menu_mask) { 3058 size = sizeof(mapping->menu_mapping[0]) 3059 * fls(mapping->menu_mask); 3060 map->menu_mapping = kmemdup(mapping->menu_mapping, size, 3061 GFP_KERNEL); 3062 if (!map->menu_mapping) 3063 goto err_nomem; 3064 } 3065 if (mapping->menu_names && mapping->menu_mask) { 3066 size = sizeof(mapping->menu_names[0]) 3067 * fls(mapping->menu_mask); 3068 map->menu_names = kmemdup(mapping->menu_names, size, 3069 GFP_KERNEL); 3070 if (!map->menu_names) 3071 goto err_nomem; 3072 } 3073 3074 if (uvc_ctrl_mapping_is_compound(map)) 3075 if (WARN_ON(!map->set || !map->get)) { 3076 ret = -EIO; 3077 goto free_mem; 3078 } 3079 3080 if (map->get == NULL) 3081 map->get = uvc_get_le_value; 3082 if (map->set == NULL) 3083 map->set = uvc_set_le_value; 3084 3085 for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) { 3086 if (V4L2_CTRL_ID2WHICH(uvc_control_classes[i]) == 3087 V4L2_CTRL_ID2WHICH(map->id)) { 3088 chain->ctrl_class_bitmap |= BIT(i); 3089 break; 3090 } 3091 } 3092 3093 list_add_tail(&map->list, &ctrl->info.mappings); 3094 uvc_dbg(chain->dev, CONTROL, "Adding mapping '%s' to control %pUl/%u\n", 3095 uvc_map_get_name(map), ctrl->info.entity, 3096 ctrl->info.selector); 3097 3098 return 0; 3099 3100 err_nomem: 3101 ret = -ENOMEM; 3102 free_mem: 3103 kfree(map->menu_names); 3104 kfree(map->menu_mapping); 3105 kfree(map->name); 3106 kfree(map); 3107 return ret; 3108 } 3109 3110 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain, 3111 const struct uvc_control_mapping *mapping) 3112 { 3113 struct uvc_device *dev = chain->dev; 3114 struct uvc_control_mapping *map; 3115 struct uvc_entity *entity; 3116 struct uvc_control *ctrl; 3117 int found = 0; 3118 int ret; 3119 3120 if (mapping->id & ~V4L2_CTRL_ID_MASK) { 3121 uvc_dbg(dev, CONTROL, 3122 "Can't add mapping '%s', control id 0x%08x is invalid\n", 3123 uvc_map_get_name(mapping), mapping->id); 3124 return -EINVAL; 3125 } 3126 3127 /* Search for the matching (GUID/CS) control on the current chain */ 3128 list_for_each_entry(entity, &chain->entities, chain) { 3129 unsigned int i; 3130 3131 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT || 3132 !uvc_entity_match_guid(entity, mapping->entity)) 3133 continue; 3134 3135 for (i = 0; i < entity->ncontrols; ++i) { 3136 ctrl = &entity->controls[i]; 3137 if (ctrl->index == mapping->selector - 1) { 3138 found = 1; 3139 break; 3140 } 3141 } 3142 3143 if (found) 3144 break; 3145 } 3146 if (!found) 3147 return -ENOENT; 3148 3149 if (mutex_lock_interruptible(&chain->ctrl_mutex)) 3150 return -ERESTARTSYS; 3151 3152 /* Perform delayed initialization of XU controls */ 3153 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl); 3154 if (ret < 0) { 3155 ret = -ENOENT; 3156 goto done; 3157 } 3158 3159 /* Validate the user-provided bit-size and offset */ 3160 if (mapping->size > 32 || 3161 mapping->offset + mapping->size > ctrl->info.size * 8) { 3162 ret = -EINVAL; 3163 goto done; 3164 } 3165 3166 list_for_each_entry(map, &ctrl->info.mappings, list) { 3167 if (mapping->id == map->id) { 3168 uvc_dbg(dev, CONTROL, 3169 "Can't add mapping '%s', control id 0x%08x already exists\n", 3170 uvc_map_get_name(mapping), mapping->id); 3171 ret = -EEXIST; 3172 goto done; 3173 } 3174 } 3175 3176 /* Prevent excess memory consumption */ 3177 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) { 3178 atomic_dec(&dev->nmappings); 3179 uvc_dbg(dev, CONTROL, 3180 "Can't add mapping '%s', maximum mappings count (%u) exceeded\n", 3181 uvc_map_get_name(mapping), UVC_MAX_CONTROL_MAPPINGS); 3182 ret = -ENOMEM; 3183 goto done; 3184 } 3185 3186 ret = __uvc_ctrl_add_mapping(chain, ctrl, mapping); 3187 if (ret < 0) 3188 atomic_dec(&dev->nmappings); 3189 3190 done: 3191 mutex_unlock(&chain->ctrl_mutex); 3192 return ret; 3193 } 3194 3195 /* 3196 * Prune an entity of its bogus controls using a blacklist. Bogus controls 3197 * are currently the ones that crash the camera or unconditionally return an 3198 * error when queried. 3199 */ 3200 static void uvc_ctrl_prune_entity(struct uvc_device *dev, 3201 struct uvc_entity *entity) 3202 { 3203 struct uvc_ctrl_blacklist { 3204 struct usb_device_id id; 3205 u8 index; 3206 }; 3207 3208 static const struct uvc_ctrl_blacklist processing_blacklist[] = { 3209 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */ 3210 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */ 3211 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */ 3212 }; 3213 static const struct uvc_ctrl_blacklist camera_blacklist[] = { 3214 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */ 3215 }; 3216 3217 const struct uvc_ctrl_blacklist *blacklist; 3218 unsigned int size; 3219 unsigned int count; 3220 unsigned int i; 3221 u8 *controls; 3222 3223 switch (UVC_ENTITY_TYPE(entity)) { 3224 case UVC_VC_PROCESSING_UNIT: 3225 blacklist = processing_blacklist; 3226 count = ARRAY_SIZE(processing_blacklist); 3227 controls = entity->processing.bmControls; 3228 size = entity->processing.bControlSize; 3229 break; 3230 3231 case UVC_ITT_CAMERA: 3232 blacklist = camera_blacklist; 3233 count = ARRAY_SIZE(camera_blacklist); 3234 controls = entity->camera.bmControls; 3235 size = entity->camera.bControlSize; 3236 break; 3237 3238 default: 3239 return; 3240 } 3241 3242 for (i = 0; i < count; ++i) { 3243 if (!usb_match_one_id(dev->intf, &blacklist[i].id)) 3244 continue; 3245 3246 if (blacklist[i].index >= 8 * size || 3247 !uvc_test_bit(controls, blacklist[i].index)) 3248 continue; 3249 3250 uvc_dbg(dev, CONTROL, 3251 "%u/%u control is black listed, removing it\n", 3252 entity->id, blacklist[i].index); 3253 3254 uvc_clear_bit(controls, blacklist[i].index); 3255 } 3256 } 3257 3258 /* 3259 * Add control information and hardcoded stock control mappings to the given 3260 * device. 3261 */ 3262 static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain, 3263 struct uvc_control *ctrl) 3264 { 3265 unsigned int i; 3266 3267 for (i = 0; i < ARRAY_SIZE(uvc_ctrls); ++i) { 3268 const struct uvc_control_info *info = &uvc_ctrls[i]; 3269 3270 if (uvc_entity_match_guid(ctrl->entity, info->entity) && 3271 ctrl->index == info->index) { 3272 uvc_ctrl_add_info(chain->dev, ctrl, info); 3273 /* 3274 * Retrieve control flags from the device. Ignore errors 3275 * and work with default flag values from the uvc_ctrl 3276 * array when the device doesn't properly implement 3277 * GET_INFO on standard controls. 3278 */ 3279 uvc_ctrl_get_flags(chain->dev, ctrl, &ctrl->info); 3280 break; 3281 } 3282 } 3283 3284 if (!ctrl->initialized) 3285 return; 3286 3287 /* Process common mappings. */ 3288 for (i = 0; i < ARRAY_SIZE(uvc_ctrl_mappings); ++i) { 3289 const struct uvc_control_mapping *mapping = &uvc_ctrl_mappings[i]; 3290 3291 if (!uvc_entity_match_guid(ctrl->entity, mapping->entity) || 3292 ctrl->info.selector != mapping->selector) 3293 continue; 3294 3295 /* Let the device provide a custom mapping. */ 3296 if (mapping->filter_mapping) { 3297 mapping = mapping->filter_mapping(chain, ctrl); 3298 if (!mapping) 3299 continue; 3300 } 3301 3302 __uvc_ctrl_add_mapping(chain, ctrl, mapping); 3303 } 3304 } 3305 3306 /* 3307 * Initialize device controls. 3308 */ 3309 static int uvc_ctrl_init_chain(struct uvc_video_chain *chain) 3310 { 3311 struct uvc_entity *entity; 3312 unsigned int i; 3313 3314 /* Walk the entities list and instantiate controls */ 3315 list_for_each_entry(entity, &chain->entities, chain) { 3316 struct uvc_control *ctrl; 3317 unsigned int bControlSize = 0, ncontrols; 3318 u8 *bmControls = NULL; 3319 3320 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) { 3321 bmControls = entity->extension.bmControls; 3322 bControlSize = entity->extension.bControlSize; 3323 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) { 3324 bmControls = entity->processing.bmControls; 3325 bControlSize = entity->processing.bControlSize; 3326 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) { 3327 bmControls = entity->camera.bmControls; 3328 bControlSize = entity->camera.bControlSize; 3329 } else if (UVC_ENTITY_TYPE(entity) == UVC_EXT_GPIO_UNIT) { 3330 bmControls = entity->gpio.bmControls; 3331 bControlSize = entity->gpio.bControlSize; 3332 } 3333 3334 /* Remove bogus/blacklisted controls */ 3335 uvc_ctrl_prune_entity(chain->dev, entity); 3336 3337 /* Count supported controls and allocate the controls array */ 3338 ncontrols = memweight(bmControls, bControlSize); 3339 if (ncontrols == 0) 3340 continue; 3341 3342 entity->controls = kzalloc_objs(*ctrl, ncontrols); 3343 if (entity->controls == NULL) 3344 return -ENOMEM; 3345 entity->ncontrols = ncontrols; 3346 3347 /* Initialize all supported controls */ 3348 ctrl = entity->controls; 3349 for (i = 0; i < bControlSize * 8; ++i) { 3350 if (uvc_test_bit(bmControls, i) == 0) 3351 continue; 3352 3353 ctrl->entity = entity; 3354 ctrl->index = i; 3355 3356 uvc_ctrl_init_ctrl(chain, ctrl); 3357 ctrl++; 3358 } 3359 } 3360 3361 return 0; 3362 } 3363 3364 int uvc_ctrl_init_device(struct uvc_device *dev) 3365 { 3366 struct uvc_video_chain *chain; 3367 int ret; 3368 3369 INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work); 3370 3371 list_for_each_entry(chain, &dev->chains, list) { 3372 ret = uvc_ctrl_init_chain(chain); 3373 if (ret) 3374 return ret; 3375 } 3376 3377 return 0; 3378 } 3379 3380 void uvc_ctrl_cleanup_fh(struct uvc_fh *handle) 3381 { 3382 struct uvc_entity *entity; 3383 3384 guard(mutex)(&handle->chain->ctrl_mutex); 3385 3386 if (!handle->pending_async_ctrls) 3387 return; 3388 3389 list_for_each_entry(entity, &handle->chain->dev->entities, list) { 3390 for (unsigned int i = 0; i < entity->ncontrols; ++i) { 3391 if (entity->controls[i].handle != handle) 3392 continue; 3393 uvc_ctrl_clear_handle(&entity->controls[i]); 3394 } 3395 } 3396 3397 if (!WARN_ON(handle->pending_async_ctrls)) 3398 return; 3399 3400 for (unsigned int i = 0; i < handle->pending_async_ctrls; i++) 3401 uvc_pm_put(handle->stream->dev); 3402 } 3403 3404 /* 3405 * Cleanup device controls. 3406 */ 3407 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev, 3408 struct uvc_control *ctrl) 3409 { 3410 struct uvc_control_mapping *mapping, *nm; 3411 3412 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) { 3413 list_del(&mapping->list); 3414 kfree(mapping->menu_names); 3415 kfree(mapping->menu_mapping); 3416 kfree(mapping->name); 3417 kfree(mapping); 3418 } 3419 } 3420 3421 void uvc_ctrl_cleanup_device(struct uvc_device *dev) 3422 { 3423 struct uvc_entity *entity; 3424 unsigned int i; 3425 3426 /* Can be uninitialized if we are aborting on probe error. */ 3427 if (dev->async_ctrl.work.func) 3428 cancel_work_sync(&dev->async_ctrl.work); 3429 3430 /* Free controls and control mappings for all entities. */ 3431 list_for_each_entry(entity, &dev->entities, list) { 3432 for (i = 0; i < entity->ncontrols; ++i) { 3433 struct uvc_control *ctrl = &entity->controls[i]; 3434 3435 if (!ctrl->initialized) 3436 continue; 3437 3438 uvc_ctrl_cleanup_mappings(dev, ctrl); 3439 kfree(ctrl->uvc_data); 3440 } 3441 3442 kfree(entity->controls); 3443 } 3444 } 3445