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