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