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