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