1 /* 2 * uvc_ctrl.c -- USB Video Class driver - Controls 3 * 4 * Copyright (C) 2005-2010 5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com) 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 */ 13 14 #include <linux/kernel.h> 15 #include <linux/list.h> 16 #include <linux/module.h> 17 #include <linux/slab.h> 18 #include <linux/uaccess.h> 19 #include <linux/usb.h> 20 #include <linux/videodev2.h> 21 #include <linux/vmalloc.h> 22 #include <linux/wait.h> 23 #include <linux/atomic.h> 24 #include <media/v4l2-ctrls.h> 25 26 #include "uvcvideo.h" 27 28 #define UVC_CTRL_DATA_CURRENT 0 29 #define UVC_CTRL_DATA_BACKUP 1 30 #define UVC_CTRL_DATA_MIN 2 31 #define UVC_CTRL_DATA_MAX 3 32 #define UVC_CTRL_DATA_RES 4 33 #define UVC_CTRL_DATA_DEF 5 34 #define UVC_CTRL_DATA_LAST 6 35 36 /* ------------------------------------------------------------------------ 37 * Controls 38 */ 39 40 static struct uvc_control_info uvc_ctrls[] = { 41 { 42 .entity = UVC_GUID_UVC_PROCESSING, 43 .selector = UVC_PU_BRIGHTNESS_CONTROL, 44 .index = 0, 45 .size = 2, 46 .flags = UVC_CTRL_FLAG_SET_CUR 47 | UVC_CTRL_FLAG_GET_RANGE 48 | UVC_CTRL_FLAG_RESTORE, 49 }, 50 { 51 .entity = UVC_GUID_UVC_PROCESSING, 52 .selector = UVC_PU_CONTRAST_CONTROL, 53 .index = 1, 54 .size = 2, 55 .flags = UVC_CTRL_FLAG_SET_CUR 56 | UVC_CTRL_FLAG_GET_RANGE 57 | UVC_CTRL_FLAG_RESTORE, 58 }, 59 { 60 .entity = UVC_GUID_UVC_PROCESSING, 61 .selector = UVC_PU_HUE_CONTROL, 62 .index = 2, 63 .size = 2, 64 .flags = UVC_CTRL_FLAG_SET_CUR 65 | UVC_CTRL_FLAG_GET_RANGE 66 | UVC_CTRL_FLAG_RESTORE 67 | UVC_CTRL_FLAG_AUTO_UPDATE, 68 }, 69 { 70 .entity = UVC_GUID_UVC_PROCESSING, 71 .selector = UVC_PU_SATURATION_CONTROL, 72 .index = 3, 73 .size = 2, 74 .flags = UVC_CTRL_FLAG_SET_CUR 75 | UVC_CTRL_FLAG_GET_RANGE 76 | UVC_CTRL_FLAG_RESTORE, 77 }, 78 { 79 .entity = UVC_GUID_UVC_PROCESSING, 80 .selector = UVC_PU_SHARPNESS_CONTROL, 81 .index = 4, 82 .size = 2, 83 .flags = UVC_CTRL_FLAG_SET_CUR 84 | UVC_CTRL_FLAG_GET_RANGE 85 | UVC_CTRL_FLAG_RESTORE, 86 }, 87 { 88 .entity = UVC_GUID_UVC_PROCESSING, 89 .selector = UVC_PU_GAMMA_CONTROL, 90 .index = 5, 91 .size = 2, 92 .flags = UVC_CTRL_FLAG_SET_CUR 93 | UVC_CTRL_FLAG_GET_RANGE 94 | UVC_CTRL_FLAG_RESTORE, 95 }, 96 { 97 .entity = UVC_GUID_UVC_PROCESSING, 98 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL, 99 .index = 6, 100 .size = 2, 101 .flags = UVC_CTRL_FLAG_SET_CUR 102 | UVC_CTRL_FLAG_GET_RANGE 103 | UVC_CTRL_FLAG_RESTORE 104 | UVC_CTRL_FLAG_AUTO_UPDATE, 105 }, 106 { 107 .entity = UVC_GUID_UVC_PROCESSING, 108 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL, 109 .index = 7, 110 .size = 4, 111 .flags = UVC_CTRL_FLAG_SET_CUR 112 | UVC_CTRL_FLAG_GET_RANGE 113 | UVC_CTRL_FLAG_RESTORE 114 | UVC_CTRL_FLAG_AUTO_UPDATE, 115 }, 116 { 117 .entity = UVC_GUID_UVC_PROCESSING, 118 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL, 119 .index = 8, 120 .size = 2, 121 .flags = UVC_CTRL_FLAG_SET_CUR 122 | UVC_CTRL_FLAG_GET_RANGE 123 | UVC_CTRL_FLAG_RESTORE, 124 }, 125 { 126 .entity = UVC_GUID_UVC_PROCESSING, 127 .selector = UVC_PU_GAIN_CONTROL, 128 .index = 9, 129 .size = 2, 130 .flags = UVC_CTRL_FLAG_SET_CUR 131 | UVC_CTRL_FLAG_GET_RANGE 132 | UVC_CTRL_FLAG_RESTORE, 133 }, 134 { 135 .entity = UVC_GUID_UVC_PROCESSING, 136 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 137 .index = 10, 138 .size = 1, 139 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 140 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, 141 }, 142 { 143 .entity = UVC_GUID_UVC_PROCESSING, 144 .selector = UVC_PU_HUE_AUTO_CONTROL, 145 .index = 11, 146 .size = 1, 147 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 148 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, 149 }, 150 { 151 .entity = UVC_GUID_UVC_PROCESSING, 152 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL, 153 .index = 12, 154 .size = 1, 155 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 156 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, 157 }, 158 { 159 .entity = UVC_GUID_UVC_PROCESSING, 160 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL, 161 .index = 13, 162 .size = 1, 163 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 164 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, 165 }, 166 { 167 .entity = UVC_GUID_UVC_PROCESSING, 168 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL, 169 .index = 14, 170 .size = 2, 171 .flags = UVC_CTRL_FLAG_SET_CUR 172 | UVC_CTRL_FLAG_GET_RANGE 173 | UVC_CTRL_FLAG_RESTORE, 174 }, 175 { 176 .entity = UVC_GUID_UVC_PROCESSING, 177 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL, 178 .index = 15, 179 .size = 2, 180 .flags = UVC_CTRL_FLAG_SET_CUR 181 | UVC_CTRL_FLAG_GET_RANGE 182 | UVC_CTRL_FLAG_RESTORE, 183 }, 184 { 185 .entity = UVC_GUID_UVC_PROCESSING, 186 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL, 187 .index = 16, 188 .size = 1, 189 .flags = UVC_CTRL_FLAG_GET_CUR, 190 }, 191 { 192 .entity = UVC_GUID_UVC_PROCESSING, 193 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL, 194 .index = 17, 195 .size = 1, 196 .flags = UVC_CTRL_FLAG_GET_CUR, 197 }, 198 { 199 .entity = UVC_GUID_UVC_CAMERA, 200 .selector = UVC_CT_SCANNING_MODE_CONTROL, 201 .index = 0, 202 .size = 1, 203 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 204 | UVC_CTRL_FLAG_RESTORE, 205 }, 206 { 207 .entity = UVC_GUID_UVC_CAMERA, 208 .selector = UVC_CT_AE_MODE_CONTROL, 209 .index = 1, 210 .size = 1, 211 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 212 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES 213 | UVC_CTRL_FLAG_RESTORE, 214 }, 215 { 216 .entity = UVC_GUID_UVC_CAMERA, 217 .selector = UVC_CT_AE_PRIORITY_CONTROL, 218 .index = 2, 219 .size = 1, 220 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 221 | UVC_CTRL_FLAG_RESTORE, 222 }, 223 { 224 .entity = UVC_GUID_UVC_CAMERA, 225 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL, 226 .index = 3, 227 .size = 4, 228 .flags = UVC_CTRL_FLAG_SET_CUR 229 | UVC_CTRL_FLAG_GET_RANGE 230 | UVC_CTRL_FLAG_RESTORE 231 | UVC_CTRL_FLAG_AUTO_UPDATE, 232 }, 233 { 234 .entity = UVC_GUID_UVC_CAMERA, 235 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL, 236 .index = 4, 237 .size = 1, 238 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE, 239 }, 240 { 241 .entity = UVC_GUID_UVC_CAMERA, 242 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL, 243 .index = 5, 244 .size = 2, 245 .flags = UVC_CTRL_FLAG_SET_CUR 246 | UVC_CTRL_FLAG_GET_RANGE 247 | UVC_CTRL_FLAG_RESTORE 248 | UVC_CTRL_FLAG_AUTO_UPDATE, 249 }, 250 { 251 .entity = UVC_GUID_UVC_CAMERA, 252 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL, 253 .index = 6, 254 .size = 2, 255 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN 256 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES 257 | UVC_CTRL_FLAG_GET_DEF 258 | UVC_CTRL_FLAG_AUTO_UPDATE, 259 }, 260 { 261 .entity = UVC_GUID_UVC_CAMERA, 262 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL, 263 .index = 7, 264 .size = 2, 265 .flags = UVC_CTRL_FLAG_SET_CUR 266 | UVC_CTRL_FLAG_GET_RANGE 267 | UVC_CTRL_FLAG_RESTORE 268 | UVC_CTRL_FLAG_AUTO_UPDATE, 269 }, 270 { 271 .entity = UVC_GUID_UVC_CAMERA, 272 .selector = UVC_CT_IRIS_RELATIVE_CONTROL, 273 .index = 8, 274 .size = 1, 275 .flags = UVC_CTRL_FLAG_SET_CUR 276 | UVC_CTRL_FLAG_AUTO_UPDATE, 277 }, 278 { 279 .entity = UVC_GUID_UVC_CAMERA, 280 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL, 281 .index = 9, 282 .size = 2, 283 .flags = UVC_CTRL_FLAG_SET_CUR 284 | UVC_CTRL_FLAG_GET_RANGE 285 | UVC_CTRL_FLAG_RESTORE 286 | UVC_CTRL_FLAG_AUTO_UPDATE, 287 }, 288 { 289 .entity = UVC_GUID_UVC_CAMERA, 290 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL, 291 .index = 10, 292 .size = 3, 293 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN 294 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES 295 | UVC_CTRL_FLAG_GET_DEF 296 | UVC_CTRL_FLAG_AUTO_UPDATE, 297 }, 298 { 299 .entity = UVC_GUID_UVC_CAMERA, 300 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL, 301 .index = 11, 302 .size = 8, 303 .flags = UVC_CTRL_FLAG_SET_CUR 304 | UVC_CTRL_FLAG_GET_RANGE 305 | UVC_CTRL_FLAG_RESTORE 306 | UVC_CTRL_FLAG_AUTO_UPDATE, 307 }, 308 { 309 .entity = UVC_GUID_UVC_CAMERA, 310 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL, 311 .index = 12, 312 .size = 4, 313 .flags = UVC_CTRL_FLAG_SET_CUR 314 | UVC_CTRL_FLAG_GET_RANGE 315 | UVC_CTRL_FLAG_AUTO_UPDATE, 316 }, 317 { 318 .entity = UVC_GUID_UVC_CAMERA, 319 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL, 320 .index = 13, 321 .size = 2, 322 .flags = UVC_CTRL_FLAG_SET_CUR 323 | UVC_CTRL_FLAG_GET_RANGE 324 | UVC_CTRL_FLAG_RESTORE 325 | UVC_CTRL_FLAG_AUTO_UPDATE, 326 }, 327 { 328 .entity = UVC_GUID_UVC_CAMERA, 329 .selector = UVC_CT_ROLL_RELATIVE_CONTROL, 330 .index = 14, 331 .size = 2, 332 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN 333 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES 334 | UVC_CTRL_FLAG_GET_DEF 335 | UVC_CTRL_FLAG_AUTO_UPDATE, 336 }, 337 { 338 .entity = UVC_GUID_UVC_CAMERA, 339 .selector = UVC_CT_FOCUS_AUTO_CONTROL, 340 .index = 17, 341 .size = 1, 342 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 343 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE, 344 }, 345 { 346 .entity = UVC_GUID_UVC_CAMERA, 347 .selector = UVC_CT_PRIVACY_CONTROL, 348 .index = 18, 349 .size = 1, 350 .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 351 | UVC_CTRL_FLAG_RESTORE 352 | UVC_CTRL_FLAG_AUTO_UPDATE, 353 }, 354 }; 355 356 static struct uvc_menu_info power_line_frequency_controls[] = { 357 { 0, "Disabled" }, 358 { 1, "50 Hz" }, 359 { 2, "60 Hz" }, 360 }; 361 362 static struct uvc_menu_info exposure_auto_controls[] = { 363 { 2, "Auto Mode" }, 364 { 1, "Manual Mode" }, 365 { 4, "Shutter Priority Mode" }, 366 { 8, "Aperture Priority Mode" }, 367 }; 368 369 static s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping, 370 u8 query, const u8 *data) 371 { 372 s8 zoom = (s8)data[0]; 373 374 switch (query) { 375 case UVC_GET_CUR: 376 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]); 377 378 case UVC_GET_MIN: 379 case UVC_GET_MAX: 380 case UVC_GET_RES: 381 case UVC_GET_DEF: 382 default: 383 return data[2]; 384 } 385 } 386 387 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping, 388 s32 value, u8 *data) 389 { 390 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff; 391 data[2] = min((int)abs(value), 0xff); 392 } 393 394 static s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping, 395 u8 query, const u8 *data) 396 { 397 unsigned int first = mapping->offset / 8; 398 s8 rel = (s8)data[first]; 399 400 switch (query) { 401 case UVC_GET_CUR: 402 return (rel == 0) ? 0 : (rel > 0 ? data[first+1] 403 : -data[first+1]); 404 case UVC_GET_MIN: 405 return -data[first+1]; 406 case UVC_GET_MAX: 407 case UVC_GET_RES: 408 case UVC_GET_DEF: 409 default: 410 return data[first+1]; 411 } 412 } 413 414 static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping, 415 s32 value, u8 *data) 416 { 417 unsigned int first = mapping->offset / 8; 418 419 data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff; 420 data[first+1] = min_t(int, abs(value), 0xff); 421 } 422 423 static struct uvc_control_mapping uvc_ctrl_mappings[] = { 424 { 425 .id = V4L2_CID_BRIGHTNESS, 426 .name = "Brightness", 427 .entity = UVC_GUID_UVC_PROCESSING, 428 .selector = UVC_PU_BRIGHTNESS_CONTROL, 429 .size = 16, 430 .offset = 0, 431 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 432 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 433 }, 434 { 435 .id = V4L2_CID_CONTRAST, 436 .name = "Contrast", 437 .entity = UVC_GUID_UVC_PROCESSING, 438 .selector = UVC_PU_CONTRAST_CONTROL, 439 .size = 16, 440 .offset = 0, 441 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 442 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 443 }, 444 { 445 .id = V4L2_CID_HUE, 446 .name = "Hue", 447 .entity = UVC_GUID_UVC_PROCESSING, 448 .selector = UVC_PU_HUE_CONTROL, 449 .size = 16, 450 .offset = 0, 451 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 452 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 453 .master_id = V4L2_CID_HUE_AUTO, 454 .master_manual = 0, 455 }, 456 { 457 .id = V4L2_CID_SATURATION, 458 .name = "Saturation", 459 .entity = UVC_GUID_UVC_PROCESSING, 460 .selector = UVC_PU_SATURATION_CONTROL, 461 .size = 16, 462 .offset = 0, 463 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 464 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 465 }, 466 { 467 .id = V4L2_CID_SHARPNESS, 468 .name = "Sharpness", 469 .entity = UVC_GUID_UVC_PROCESSING, 470 .selector = UVC_PU_SHARPNESS_CONTROL, 471 .size = 16, 472 .offset = 0, 473 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 474 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 475 }, 476 { 477 .id = V4L2_CID_GAMMA, 478 .name = "Gamma", 479 .entity = UVC_GUID_UVC_PROCESSING, 480 .selector = UVC_PU_GAMMA_CONTROL, 481 .size = 16, 482 .offset = 0, 483 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 484 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 485 }, 486 { 487 .id = V4L2_CID_BACKLIGHT_COMPENSATION, 488 .name = "Backlight Compensation", 489 .entity = UVC_GUID_UVC_PROCESSING, 490 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL, 491 .size = 16, 492 .offset = 0, 493 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 494 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 495 }, 496 { 497 .id = V4L2_CID_GAIN, 498 .name = "Gain", 499 .entity = UVC_GUID_UVC_PROCESSING, 500 .selector = UVC_PU_GAIN_CONTROL, 501 .size = 16, 502 .offset = 0, 503 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 504 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 505 }, 506 { 507 .id = V4L2_CID_POWER_LINE_FREQUENCY, 508 .name = "Power Line Frequency", 509 .entity = UVC_GUID_UVC_PROCESSING, 510 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 511 .size = 2, 512 .offset = 0, 513 .v4l2_type = V4L2_CTRL_TYPE_MENU, 514 .data_type = UVC_CTRL_DATA_TYPE_ENUM, 515 .menu_info = power_line_frequency_controls, 516 .menu_count = ARRAY_SIZE(power_line_frequency_controls), 517 }, 518 { 519 .id = V4L2_CID_HUE_AUTO, 520 .name = "Hue, Auto", 521 .entity = UVC_GUID_UVC_PROCESSING, 522 .selector = UVC_PU_HUE_AUTO_CONTROL, 523 .size = 1, 524 .offset = 0, 525 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, 526 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, 527 .slave_ids = { V4L2_CID_HUE, }, 528 }, 529 { 530 .id = V4L2_CID_EXPOSURE_AUTO, 531 .name = "Exposure, Auto", 532 .entity = UVC_GUID_UVC_CAMERA, 533 .selector = UVC_CT_AE_MODE_CONTROL, 534 .size = 4, 535 .offset = 0, 536 .v4l2_type = V4L2_CTRL_TYPE_MENU, 537 .data_type = UVC_CTRL_DATA_TYPE_BITMASK, 538 .menu_info = exposure_auto_controls, 539 .menu_count = ARRAY_SIZE(exposure_auto_controls), 540 .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, }, 541 }, 542 { 543 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY, 544 .name = "Exposure, Auto Priority", 545 .entity = UVC_GUID_UVC_CAMERA, 546 .selector = UVC_CT_AE_PRIORITY_CONTROL, 547 .size = 1, 548 .offset = 0, 549 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, 550 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, 551 }, 552 { 553 .id = V4L2_CID_EXPOSURE_ABSOLUTE, 554 .name = "Exposure (Absolute)", 555 .entity = UVC_GUID_UVC_CAMERA, 556 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL, 557 .size = 32, 558 .offset = 0, 559 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 560 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 561 .master_id = V4L2_CID_EXPOSURE_AUTO, 562 .master_manual = V4L2_EXPOSURE_MANUAL, 563 }, 564 { 565 .id = V4L2_CID_AUTO_WHITE_BALANCE, 566 .name = "White Balance Temperature, Auto", 567 .entity = UVC_GUID_UVC_PROCESSING, 568 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL, 569 .size = 1, 570 .offset = 0, 571 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, 572 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, 573 .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, }, 574 }, 575 { 576 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE, 577 .name = "White Balance Temperature", 578 .entity = UVC_GUID_UVC_PROCESSING, 579 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL, 580 .size = 16, 581 .offset = 0, 582 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 583 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 584 .master_id = V4L2_CID_AUTO_WHITE_BALANCE, 585 .master_manual = 0, 586 }, 587 { 588 .id = V4L2_CID_AUTO_WHITE_BALANCE, 589 .name = "White Balance Component, Auto", 590 .entity = UVC_GUID_UVC_PROCESSING, 591 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL, 592 .size = 1, 593 .offset = 0, 594 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, 595 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, 596 .slave_ids = { V4L2_CID_BLUE_BALANCE, 597 V4L2_CID_RED_BALANCE }, 598 }, 599 { 600 .id = V4L2_CID_BLUE_BALANCE, 601 .name = "White Balance Blue Component", 602 .entity = UVC_GUID_UVC_PROCESSING, 603 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL, 604 .size = 16, 605 .offset = 0, 606 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 607 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 608 .master_id = V4L2_CID_AUTO_WHITE_BALANCE, 609 .master_manual = 0, 610 }, 611 { 612 .id = V4L2_CID_RED_BALANCE, 613 .name = "White Balance Red Component", 614 .entity = UVC_GUID_UVC_PROCESSING, 615 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL, 616 .size = 16, 617 .offset = 16, 618 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 619 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 620 .master_id = V4L2_CID_AUTO_WHITE_BALANCE, 621 .master_manual = 0, 622 }, 623 { 624 .id = V4L2_CID_FOCUS_ABSOLUTE, 625 .name = "Focus (absolute)", 626 .entity = UVC_GUID_UVC_CAMERA, 627 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL, 628 .size = 16, 629 .offset = 0, 630 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 631 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 632 .master_id = V4L2_CID_FOCUS_AUTO, 633 .master_manual = 0, 634 }, 635 { 636 .id = V4L2_CID_FOCUS_AUTO, 637 .name = "Focus, Auto", 638 .entity = UVC_GUID_UVC_CAMERA, 639 .selector = UVC_CT_FOCUS_AUTO_CONTROL, 640 .size = 1, 641 .offset = 0, 642 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, 643 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, 644 .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, }, 645 }, 646 { 647 .id = V4L2_CID_IRIS_ABSOLUTE, 648 .name = "Iris, Absolute", 649 .entity = UVC_GUID_UVC_CAMERA, 650 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL, 651 .size = 16, 652 .offset = 0, 653 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 654 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 655 }, 656 { 657 .id = V4L2_CID_IRIS_RELATIVE, 658 .name = "Iris, Relative", 659 .entity = UVC_GUID_UVC_CAMERA, 660 .selector = UVC_CT_IRIS_RELATIVE_CONTROL, 661 .size = 8, 662 .offset = 0, 663 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 664 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 665 }, 666 { 667 .id = V4L2_CID_ZOOM_ABSOLUTE, 668 .name = "Zoom, Absolute", 669 .entity = UVC_GUID_UVC_CAMERA, 670 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL, 671 .size = 16, 672 .offset = 0, 673 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 674 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, 675 }, 676 { 677 .id = V4L2_CID_ZOOM_CONTINUOUS, 678 .name = "Zoom, Continuous", 679 .entity = UVC_GUID_UVC_CAMERA, 680 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL, 681 .size = 0, 682 .offset = 0, 683 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 684 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 685 .get = uvc_ctrl_get_zoom, 686 .set = uvc_ctrl_set_zoom, 687 }, 688 { 689 .id = V4L2_CID_PAN_ABSOLUTE, 690 .name = "Pan (Absolute)", 691 .entity = UVC_GUID_UVC_CAMERA, 692 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL, 693 .size = 32, 694 .offset = 0, 695 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 696 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 697 }, 698 { 699 .id = V4L2_CID_TILT_ABSOLUTE, 700 .name = "Tilt (Absolute)", 701 .entity = UVC_GUID_UVC_CAMERA, 702 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL, 703 .size = 32, 704 .offset = 32, 705 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 706 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 707 }, 708 { 709 .id = V4L2_CID_PAN_SPEED, 710 .name = "Pan (Speed)", 711 .entity = UVC_GUID_UVC_CAMERA, 712 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL, 713 .size = 16, 714 .offset = 0, 715 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 716 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 717 .get = uvc_ctrl_get_rel_speed, 718 .set = uvc_ctrl_set_rel_speed, 719 }, 720 { 721 .id = V4L2_CID_TILT_SPEED, 722 .name = "Tilt (Speed)", 723 .entity = UVC_GUID_UVC_CAMERA, 724 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL, 725 .size = 16, 726 .offset = 16, 727 .v4l2_type = V4L2_CTRL_TYPE_INTEGER, 728 .data_type = UVC_CTRL_DATA_TYPE_SIGNED, 729 .get = uvc_ctrl_get_rel_speed, 730 .set = uvc_ctrl_set_rel_speed, 731 }, 732 { 733 .id = V4L2_CID_PRIVACY, 734 .name = "Privacy", 735 .entity = UVC_GUID_UVC_CAMERA, 736 .selector = UVC_CT_PRIVACY_CONTROL, 737 .size = 1, 738 .offset = 0, 739 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, 740 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, 741 }, 742 }; 743 744 /* ------------------------------------------------------------------------ 745 * Utility functions 746 */ 747 748 static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id) 749 { 750 return ctrl->uvc_data + id * ctrl->info.size; 751 } 752 753 static inline int uvc_test_bit(const u8 *data, int bit) 754 { 755 return (data[bit >> 3] >> (bit & 7)) & 1; 756 } 757 758 static inline void uvc_clear_bit(u8 *data, int bit) 759 { 760 data[bit >> 3] &= ~(1 << (bit & 7)); 761 } 762 763 /* Extract the bit string specified by mapping->offset and mapping->size 764 * from the little-endian data stored at 'data' and return the result as 765 * a signed 32bit integer. Sign extension will be performed if the mapping 766 * references a signed data type. 767 */ 768 static s32 uvc_get_le_value(struct uvc_control_mapping *mapping, 769 u8 query, const u8 *data) 770 { 771 int bits = mapping->size; 772 int offset = mapping->offset; 773 s32 value = 0; 774 u8 mask; 775 776 data += offset / 8; 777 offset &= 7; 778 mask = ((1LL << bits) - 1) << offset; 779 780 for (; bits > 0; data++) { 781 u8 byte = *data & mask; 782 value |= offset > 0 ? (byte >> offset) : (byte << (-offset)); 783 bits -= 8 - (offset > 0 ? offset : 0); 784 offset -= 8; 785 mask = (1 << bits) - 1; 786 } 787 788 /* Sign-extend the value if needed. */ 789 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED) 790 value |= -(value & (1 << (mapping->size - 1))); 791 792 return value; 793 } 794 795 /* Set the bit string specified by mapping->offset and mapping->size 796 * in the little-endian data stored at 'data' to the value 'value'. 797 */ 798 static void uvc_set_le_value(struct uvc_control_mapping *mapping, 799 s32 value, u8 *data) 800 { 801 int bits = mapping->size; 802 int offset = mapping->offset; 803 u8 mask; 804 805 /* According to the v4l2 spec, writing any value to a button control 806 * should result in the action belonging to the button control being 807 * triggered. UVC devices however want to see a 1 written -> override 808 * value. 809 */ 810 if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON) 811 value = -1; 812 813 data += offset / 8; 814 offset &= 7; 815 816 for (; bits > 0; data++) { 817 mask = ((1LL << bits) - 1) << offset; 818 *data = (*data & ~mask) | ((value << offset) & mask); 819 value >>= offset ? offset : 8; 820 bits -= 8 - offset; 821 offset = 0; 822 } 823 } 824 825 /* ------------------------------------------------------------------------ 826 * Terminal and unit management 827 */ 828 829 static const u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING; 830 static const u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA; 831 static const u8 uvc_media_transport_input_guid[16] = 832 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT; 833 834 static int uvc_entity_match_guid(const struct uvc_entity *entity, 835 const u8 guid[16]) 836 { 837 switch (UVC_ENTITY_TYPE(entity)) { 838 case UVC_ITT_CAMERA: 839 return memcmp(uvc_camera_guid, guid, 16) == 0; 840 841 case UVC_ITT_MEDIA_TRANSPORT_INPUT: 842 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0; 843 844 case UVC_VC_PROCESSING_UNIT: 845 return memcmp(uvc_processing_guid, guid, 16) == 0; 846 847 case UVC_VC_EXTENSION_UNIT: 848 return memcmp(entity->extension.guidExtensionCode, 849 guid, 16) == 0; 850 851 default: 852 return 0; 853 } 854 } 855 856 /* ------------------------------------------------------------------------ 857 * UVC Controls 858 */ 859 860 static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id, 861 struct uvc_control_mapping **mapping, struct uvc_control **control, 862 int next) 863 { 864 struct uvc_control *ctrl; 865 struct uvc_control_mapping *map; 866 unsigned int i; 867 868 if (entity == NULL) 869 return; 870 871 for (i = 0; i < entity->ncontrols; ++i) { 872 ctrl = &entity->controls[i]; 873 if (!ctrl->initialized) 874 continue; 875 876 list_for_each_entry(map, &ctrl->info.mappings, list) { 877 if ((map->id == v4l2_id) && !next) { 878 *control = ctrl; 879 *mapping = map; 880 return; 881 } 882 883 if ((*mapping == NULL || (*mapping)->id > map->id) && 884 (map->id > v4l2_id) && next) { 885 *control = ctrl; 886 *mapping = map; 887 } 888 } 889 } 890 } 891 892 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain, 893 u32 v4l2_id, struct uvc_control_mapping **mapping) 894 { 895 struct uvc_control *ctrl = NULL; 896 struct uvc_entity *entity; 897 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL; 898 899 *mapping = NULL; 900 901 /* Mask the query flags. */ 902 v4l2_id &= V4L2_CTRL_ID_MASK; 903 904 /* Find the control. */ 905 list_for_each_entry(entity, &chain->entities, chain) { 906 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next); 907 if (ctrl && !next) 908 return ctrl; 909 } 910 911 if (ctrl == NULL && !next) 912 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n", 913 v4l2_id); 914 915 return ctrl; 916 } 917 918 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain, 919 struct uvc_control *ctrl) 920 { 921 int ret; 922 923 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) { 924 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id, 925 chain->dev->intfnum, ctrl->info.selector, 926 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF), 927 ctrl->info.size); 928 if (ret < 0) 929 return ret; 930 } 931 932 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) { 933 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id, 934 chain->dev->intfnum, ctrl->info.selector, 935 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN), 936 ctrl->info.size); 937 if (ret < 0) 938 return ret; 939 } 940 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) { 941 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id, 942 chain->dev->intfnum, ctrl->info.selector, 943 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX), 944 ctrl->info.size); 945 if (ret < 0) 946 return ret; 947 } 948 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) { 949 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id, 950 chain->dev->intfnum, ctrl->info.selector, 951 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 952 ctrl->info.size); 953 if (ret < 0) { 954 if (UVC_ENTITY_TYPE(ctrl->entity) != 955 UVC_VC_EXTENSION_UNIT) 956 return ret; 957 958 /* GET_RES is mandatory for XU controls, but some 959 * cameras still choke on it. Ignore errors and set the 960 * resolution value to zero. 961 */ 962 uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES, 963 "UVC non compliance - GET_RES failed on " 964 "an XU control. Enabling workaround.\n"); 965 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0, 966 ctrl->info.size); 967 } 968 } 969 970 ctrl->cached = 1; 971 return 0; 972 } 973 974 static int __uvc_ctrl_get(struct uvc_video_chain *chain, 975 struct uvc_control *ctrl, struct uvc_control_mapping *mapping, 976 s32 *value) 977 { 978 struct uvc_menu_info *menu; 979 unsigned int i; 980 int ret; 981 982 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) 983 return -EACCES; 984 985 if (!ctrl->loaded) { 986 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id, 987 chain->dev->intfnum, ctrl->info.selector, 988 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), 989 ctrl->info.size); 990 if (ret < 0) 991 return ret; 992 993 ctrl->loaded = 1; 994 } 995 996 *value = mapping->get(mapping, UVC_GET_CUR, 997 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); 998 999 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) { 1000 menu = mapping->menu_info; 1001 for (i = 0; i < mapping->menu_count; ++i, ++menu) { 1002 if (menu->value == *value) { 1003 *value = i; 1004 break; 1005 } 1006 } 1007 } 1008 1009 return 0; 1010 } 1011 1012 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, 1013 struct uvc_control *ctrl, 1014 struct uvc_control_mapping *mapping, 1015 struct v4l2_queryctrl *v4l2_ctrl) 1016 { 1017 struct uvc_control_mapping *master_map = NULL; 1018 struct uvc_control *master_ctrl = NULL; 1019 struct uvc_menu_info *menu; 1020 unsigned int i; 1021 1022 memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl)); 1023 v4l2_ctrl->id = mapping->id; 1024 v4l2_ctrl->type = mapping->v4l2_type; 1025 strlcpy(v4l2_ctrl->name, mapping->name, sizeof(v4l2_ctrl->name)); 1026 v4l2_ctrl->flags = 0; 1027 1028 if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) 1029 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY; 1030 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR)) 1031 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; 1032 1033 if (mapping->master_id) 1034 __uvc_find_control(ctrl->entity, mapping->master_id, 1035 &master_map, &master_ctrl, 0); 1036 if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) { 1037 s32 val; 1038 int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val); 1039 if (ret < 0) 1040 return ret; 1041 1042 if (val != mapping->master_manual) 1043 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 1044 } 1045 1046 if (!ctrl->cached) { 1047 int ret = uvc_ctrl_populate_cache(chain, ctrl); 1048 if (ret < 0) 1049 return ret; 1050 } 1051 1052 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) { 1053 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF, 1054 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF)); 1055 } 1056 1057 switch (mapping->v4l2_type) { 1058 case V4L2_CTRL_TYPE_MENU: 1059 v4l2_ctrl->minimum = 0; 1060 v4l2_ctrl->maximum = mapping->menu_count - 1; 1061 v4l2_ctrl->step = 1; 1062 1063 menu = mapping->menu_info; 1064 for (i = 0; i < mapping->menu_count; ++i, ++menu) { 1065 if (menu->value == v4l2_ctrl->default_value) { 1066 v4l2_ctrl->default_value = i; 1067 break; 1068 } 1069 } 1070 1071 return 0; 1072 1073 case V4L2_CTRL_TYPE_BOOLEAN: 1074 v4l2_ctrl->minimum = 0; 1075 v4l2_ctrl->maximum = 1; 1076 v4l2_ctrl->step = 1; 1077 return 0; 1078 1079 case V4L2_CTRL_TYPE_BUTTON: 1080 v4l2_ctrl->minimum = 0; 1081 v4l2_ctrl->maximum = 0; 1082 v4l2_ctrl->step = 0; 1083 return 0; 1084 1085 default: 1086 break; 1087 } 1088 1089 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) 1090 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN, 1091 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN)); 1092 1093 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) 1094 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX, 1095 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX)); 1096 1097 if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) 1098 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES, 1099 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); 1100 1101 return 0; 1102 } 1103 1104 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, 1105 struct v4l2_queryctrl *v4l2_ctrl) 1106 { 1107 struct uvc_control *ctrl; 1108 struct uvc_control_mapping *mapping; 1109 int ret; 1110 1111 ret = mutex_lock_interruptible(&chain->ctrl_mutex); 1112 if (ret < 0) 1113 return -ERESTARTSYS; 1114 1115 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping); 1116 if (ctrl == NULL) { 1117 ret = -EINVAL; 1118 goto done; 1119 } 1120 1121 ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl); 1122 done: 1123 mutex_unlock(&chain->ctrl_mutex); 1124 return ret; 1125 } 1126 1127 /* 1128 * Mapping V4L2 controls to UVC controls can be straighforward if done well. 1129 * Most of the UVC controls exist in V4L2, and can be mapped directly. Some 1130 * must be grouped (for instance the Red Balance, Blue Balance and Do White 1131 * Balance V4L2 controls use the White Balance Component UVC control) or 1132 * otherwise translated. The approach we take here is to use a translation 1133 * table for the controls that can be mapped directly, and handle the others 1134 * manually. 1135 */ 1136 int uvc_query_v4l2_menu(struct uvc_video_chain *chain, 1137 struct v4l2_querymenu *query_menu) 1138 { 1139 struct uvc_menu_info *menu_info; 1140 struct uvc_control_mapping *mapping; 1141 struct uvc_control *ctrl; 1142 u32 index = query_menu->index; 1143 u32 id = query_menu->id; 1144 int ret; 1145 1146 memset(query_menu, 0, sizeof(*query_menu)); 1147 query_menu->id = id; 1148 query_menu->index = index; 1149 1150 ret = mutex_lock_interruptible(&chain->ctrl_mutex); 1151 if (ret < 0) 1152 return -ERESTARTSYS; 1153 1154 ctrl = uvc_find_control(chain, query_menu->id, &mapping); 1155 if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) { 1156 ret = -EINVAL; 1157 goto done; 1158 } 1159 1160 if (query_menu->index >= mapping->menu_count) { 1161 ret = -EINVAL; 1162 goto done; 1163 } 1164 1165 menu_info = &mapping->menu_info[query_menu->index]; 1166 1167 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK && 1168 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) { 1169 s32 bitmap; 1170 1171 if (!ctrl->cached) { 1172 ret = uvc_ctrl_populate_cache(chain, ctrl); 1173 if (ret < 0) 1174 goto done; 1175 } 1176 1177 bitmap = mapping->get(mapping, UVC_GET_RES, 1178 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); 1179 if (!(bitmap & menu_info->value)) { 1180 ret = -EINVAL; 1181 goto done; 1182 } 1183 } 1184 1185 strlcpy(query_menu->name, menu_info->name, sizeof(query_menu->name)); 1186 1187 done: 1188 mutex_unlock(&chain->ctrl_mutex); 1189 return ret; 1190 } 1191 1192 /* -------------------------------------------------------------------------- 1193 * Ctrl event handling 1194 */ 1195 1196 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain, 1197 struct v4l2_event *ev, 1198 struct uvc_control *ctrl, 1199 struct uvc_control_mapping *mapping, 1200 s32 value, u32 changes) 1201 { 1202 struct v4l2_queryctrl v4l2_ctrl; 1203 1204 __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl); 1205 1206 memset(ev->reserved, 0, sizeof(ev->reserved)); 1207 ev->type = V4L2_EVENT_CTRL; 1208 ev->id = v4l2_ctrl.id; 1209 ev->u.ctrl.value = value; 1210 ev->u.ctrl.changes = changes; 1211 ev->u.ctrl.type = v4l2_ctrl.type; 1212 ev->u.ctrl.flags = v4l2_ctrl.flags; 1213 ev->u.ctrl.minimum = v4l2_ctrl.minimum; 1214 ev->u.ctrl.maximum = v4l2_ctrl.maximum; 1215 ev->u.ctrl.step = v4l2_ctrl.step; 1216 ev->u.ctrl.default_value = v4l2_ctrl.default_value; 1217 } 1218 1219 static void uvc_ctrl_send_event(struct uvc_fh *handle, 1220 struct uvc_control *ctrl, struct uvc_control_mapping *mapping, 1221 s32 value, u32 changes) 1222 { 1223 struct v4l2_subscribed_event *sev; 1224 struct v4l2_event ev; 1225 1226 if (list_empty(&mapping->ev_subs)) 1227 return; 1228 1229 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, value, changes); 1230 1231 list_for_each_entry(sev, &mapping->ev_subs, node) { 1232 if (sev->fh && (sev->fh != &handle->vfh || 1233 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) || 1234 (changes & V4L2_EVENT_CTRL_CH_FLAGS))) 1235 v4l2_event_queue_fh(sev->fh, &ev); 1236 } 1237 } 1238 1239 static void uvc_ctrl_send_slave_event(struct uvc_fh *handle, 1240 struct uvc_control *master, u32 slave_id, 1241 const struct v4l2_ext_control *xctrls, unsigned int xctrls_count) 1242 { 1243 struct uvc_control_mapping *mapping = NULL; 1244 struct uvc_control *ctrl = NULL; 1245 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS; 1246 unsigned int i; 1247 s32 val = 0; 1248 1249 /* 1250 * We can skip sending an event for the slave if the slave 1251 * is being modified in the same transaction. 1252 */ 1253 for (i = 0; i < xctrls_count; i++) { 1254 if (xctrls[i].id == slave_id) 1255 return; 1256 } 1257 1258 __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0); 1259 if (ctrl == NULL) 1260 return; 1261 1262 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0) 1263 changes |= V4L2_EVENT_CTRL_CH_VALUE; 1264 1265 uvc_ctrl_send_event(handle, ctrl, mapping, val, changes); 1266 } 1267 1268 static void uvc_ctrl_send_events(struct uvc_fh *handle, 1269 const struct v4l2_ext_control *xctrls, unsigned int xctrls_count) 1270 { 1271 struct uvc_control_mapping *mapping; 1272 struct uvc_control *ctrl; 1273 u32 changes = V4L2_EVENT_CTRL_CH_VALUE; 1274 unsigned int i; 1275 unsigned int j; 1276 1277 for (i = 0; i < xctrls_count; ++i) { 1278 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping); 1279 1280 for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) { 1281 if (!mapping->slave_ids[j]) 1282 break; 1283 uvc_ctrl_send_slave_event(handle, ctrl, 1284 mapping->slave_ids[j], 1285 xctrls, xctrls_count); 1286 } 1287 1288 /* 1289 * If the master is being modified in the same transaction 1290 * flags may change too. 1291 */ 1292 if (mapping->master_id) { 1293 for (j = 0; j < xctrls_count; j++) { 1294 if (xctrls[j].id == mapping->master_id) { 1295 changes |= V4L2_EVENT_CTRL_CH_FLAGS; 1296 break; 1297 } 1298 } 1299 } 1300 1301 uvc_ctrl_send_event(handle, ctrl, mapping, xctrls[i].value, 1302 changes); 1303 } 1304 } 1305 1306 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems) 1307 { 1308 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh); 1309 struct uvc_control_mapping *mapping; 1310 struct uvc_control *ctrl; 1311 int ret; 1312 1313 ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex); 1314 if (ret < 0) 1315 return -ERESTARTSYS; 1316 1317 ctrl = uvc_find_control(handle->chain, sev->id, &mapping); 1318 if (ctrl == NULL) { 1319 ret = -EINVAL; 1320 goto done; 1321 } 1322 1323 list_add_tail(&sev->node, &mapping->ev_subs); 1324 if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) { 1325 struct v4l2_event ev; 1326 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS; 1327 s32 val = 0; 1328 1329 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0) 1330 changes |= V4L2_EVENT_CTRL_CH_VALUE; 1331 1332 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val, 1333 changes); 1334 /* Mark the queue as active, allowing this initial 1335 event to be accepted. */ 1336 sev->elems = elems; 1337 v4l2_event_queue_fh(sev->fh, &ev); 1338 } 1339 1340 done: 1341 mutex_unlock(&handle->chain->ctrl_mutex); 1342 return ret; 1343 } 1344 1345 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev) 1346 { 1347 struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh); 1348 1349 mutex_lock(&handle->chain->ctrl_mutex); 1350 list_del(&sev->node); 1351 mutex_unlock(&handle->chain->ctrl_mutex); 1352 } 1353 1354 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = { 1355 .add = uvc_ctrl_add_event, 1356 .del = uvc_ctrl_del_event, 1357 .replace = v4l2_ctrl_replace, 1358 .merge = v4l2_ctrl_merge, 1359 }; 1360 1361 /* -------------------------------------------------------------------------- 1362 * Control transactions 1363 * 1364 * To make extended set operations as atomic as the hardware allows, controls 1365 * are handled using begin/commit/rollback operations. 1366 * 1367 * At the beginning of a set request, uvc_ctrl_begin should be called to 1368 * initialize the request. This function acquires the control lock. 1369 * 1370 * When setting a control, the new value is stored in the control data field 1371 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for 1372 * later processing. If the UVC and V4L2 control sizes differ, the current 1373 * value is loaded from the hardware before storing the new value in the data 1374 * field. 1375 * 1376 * After processing all controls in the transaction, uvc_ctrl_commit or 1377 * uvc_ctrl_rollback must be called to apply the pending changes to the 1378 * hardware or revert them. When applying changes, all controls marked as 1379 * dirty will be modified in the UVC device, and the dirty flag will be 1380 * cleared. When reverting controls, the control data field 1381 * UVC_CTRL_DATA_CURRENT is reverted to its previous value 1382 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the 1383 * control lock. 1384 */ 1385 int uvc_ctrl_begin(struct uvc_video_chain *chain) 1386 { 1387 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0; 1388 } 1389 1390 static int uvc_ctrl_commit_entity(struct uvc_device *dev, 1391 struct uvc_entity *entity, int rollback) 1392 { 1393 struct uvc_control *ctrl; 1394 unsigned int i; 1395 int ret; 1396 1397 if (entity == NULL) 1398 return 0; 1399 1400 for (i = 0; i < entity->ncontrols; ++i) { 1401 ctrl = &entity->controls[i]; 1402 if (!ctrl->initialized) 1403 continue; 1404 1405 /* Reset the loaded flag for auto-update controls that were 1406 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent 1407 * uvc_ctrl_get from using the cached value, and for write-only 1408 * controls to prevent uvc_ctrl_set from setting bits not 1409 * explicitly set by the user. 1410 */ 1411 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE || 1412 !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) 1413 ctrl->loaded = 0; 1414 1415 if (!ctrl->dirty) 1416 continue; 1417 1418 if (!rollback) 1419 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id, 1420 dev->intfnum, ctrl->info.selector, 1421 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), 1422 ctrl->info.size); 1423 else 1424 ret = 0; 1425 1426 if (rollback || ret < 0) 1427 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), 1428 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP), 1429 ctrl->info.size); 1430 1431 ctrl->dirty = 0; 1432 1433 if (ret < 0) 1434 return ret; 1435 } 1436 1437 return 0; 1438 } 1439 1440 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback, 1441 const struct v4l2_ext_control *xctrls, 1442 unsigned int xctrls_count) 1443 { 1444 struct uvc_video_chain *chain = handle->chain; 1445 struct uvc_entity *entity; 1446 int ret = 0; 1447 1448 /* Find the control. */ 1449 list_for_each_entry(entity, &chain->entities, chain) { 1450 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback); 1451 if (ret < 0) 1452 goto done; 1453 } 1454 1455 if (!rollback) 1456 uvc_ctrl_send_events(handle, xctrls, xctrls_count); 1457 done: 1458 mutex_unlock(&chain->ctrl_mutex); 1459 return ret; 1460 } 1461 1462 int uvc_ctrl_get(struct uvc_video_chain *chain, 1463 struct v4l2_ext_control *xctrl) 1464 { 1465 struct uvc_control *ctrl; 1466 struct uvc_control_mapping *mapping; 1467 1468 ctrl = uvc_find_control(chain, xctrl->id, &mapping); 1469 if (ctrl == NULL) 1470 return -EINVAL; 1471 1472 return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value); 1473 } 1474 1475 int uvc_ctrl_set(struct uvc_video_chain *chain, 1476 struct v4l2_ext_control *xctrl) 1477 { 1478 struct uvc_control *ctrl; 1479 struct uvc_control_mapping *mapping; 1480 s32 value; 1481 u32 step; 1482 s32 min; 1483 s32 max; 1484 int ret; 1485 1486 ctrl = uvc_find_control(chain, xctrl->id, &mapping); 1487 if (ctrl == NULL) 1488 return -EINVAL; 1489 if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR)) 1490 return -EACCES; 1491 1492 /* Clamp out of range values. */ 1493 switch (mapping->v4l2_type) { 1494 case V4L2_CTRL_TYPE_INTEGER: 1495 if (!ctrl->cached) { 1496 ret = uvc_ctrl_populate_cache(chain, ctrl); 1497 if (ret < 0) 1498 return ret; 1499 } 1500 1501 min = mapping->get(mapping, UVC_GET_MIN, 1502 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN)); 1503 max = mapping->get(mapping, UVC_GET_MAX, 1504 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX)); 1505 step = mapping->get(mapping, UVC_GET_RES, 1506 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); 1507 if (step == 0) 1508 step = 1; 1509 1510 xctrl->value = min + ((u32)(xctrl->value - min) + step / 2) 1511 / step * step; 1512 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED) 1513 xctrl->value = clamp(xctrl->value, min, max); 1514 else 1515 xctrl->value = clamp_t(u32, xctrl->value, min, max); 1516 value = xctrl->value; 1517 break; 1518 1519 case V4L2_CTRL_TYPE_BOOLEAN: 1520 xctrl->value = clamp(xctrl->value, 0, 1); 1521 value = xctrl->value; 1522 break; 1523 1524 case V4L2_CTRL_TYPE_MENU: 1525 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count) 1526 return -ERANGE; 1527 value = mapping->menu_info[xctrl->value].value; 1528 1529 /* Valid menu indices are reported by the GET_RES request for 1530 * UVC controls that support it. 1531 */ 1532 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK && 1533 (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) { 1534 if (!ctrl->cached) { 1535 ret = uvc_ctrl_populate_cache(chain, ctrl); 1536 if (ret < 0) 1537 return ret; 1538 } 1539 1540 step = mapping->get(mapping, UVC_GET_RES, 1541 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); 1542 if (!(step & value)) 1543 return -EINVAL; 1544 } 1545 1546 break; 1547 1548 default: 1549 value = xctrl->value; 1550 break; 1551 } 1552 1553 /* If the mapping doesn't span the whole UVC control, the current value 1554 * needs to be loaded from the device to perform the read-modify-write 1555 * operation. 1556 */ 1557 if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) { 1558 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) { 1559 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), 1560 0, ctrl->info.size); 1561 } else { 1562 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, 1563 ctrl->entity->id, chain->dev->intfnum, 1564 ctrl->info.selector, 1565 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), 1566 ctrl->info.size); 1567 if (ret < 0) 1568 return ret; 1569 } 1570 1571 ctrl->loaded = 1; 1572 } 1573 1574 /* Backup the current value in case we need to rollback later. */ 1575 if (!ctrl->dirty) { 1576 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP), 1577 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), 1578 ctrl->info.size); 1579 } 1580 1581 mapping->set(mapping, value, 1582 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); 1583 1584 ctrl->dirty = 1; 1585 ctrl->modified = 1; 1586 return 0; 1587 } 1588 1589 /* -------------------------------------------------------------------------- 1590 * Dynamic controls 1591 */ 1592 1593 /* 1594 * Retrieve flags for a given control 1595 */ 1596 static int uvc_ctrl_get_flags(struct uvc_device *dev, 1597 const struct uvc_control *ctrl, 1598 struct uvc_control_info *info) 1599 { 1600 u8 *data; 1601 int ret; 1602 1603 data = kmalloc(1, GFP_KERNEL); 1604 if (data == NULL) 1605 return -ENOMEM; 1606 1607 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum, 1608 info->selector, data, 1); 1609 if (!ret) 1610 info->flags |= (data[0] & UVC_CONTROL_CAP_GET ? 1611 UVC_CTRL_FLAG_GET_CUR : 0) 1612 | (data[0] & UVC_CONTROL_CAP_SET ? 1613 UVC_CTRL_FLAG_SET_CUR : 0) 1614 | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ? 1615 UVC_CTRL_FLAG_AUTO_UPDATE : 0); 1616 1617 kfree(data); 1618 return ret; 1619 } 1620 1621 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev, 1622 const struct uvc_control *ctrl, struct uvc_control_info *info) 1623 { 1624 struct uvc_ctrl_fixup { 1625 struct usb_device_id id; 1626 u8 entity; 1627 u8 selector; 1628 u8 flags; 1629 }; 1630 1631 static const struct uvc_ctrl_fixup fixups[] = { 1632 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1, 1633 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX | 1634 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR | 1635 UVC_CTRL_FLAG_AUTO_UPDATE }, 1636 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1, 1637 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX | 1638 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR | 1639 UVC_CTRL_FLAG_AUTO_UPDATE }, 1640 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1, 1641 UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX | 1642 UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR | 1643 UVC_CTRL_FLAG_AUTO_UPDATE }, 1644 }; 1645 1646 unsigned int i; 1647 1648 for (i = 0; i < ARRAY_SIZE(fixups); ++i) { 1649 if (!usb_match_one_id(dev->intf, &fixups[i].id)) 1650 continue; 1651 1652 if (fixups[i].entity == ctrl->entity->id && 1653 fixups[i].selector == info->selector) { 1654 info->flags = fixups[i].flags; 1655 return; 1656 } 1657 } 1658 } 1659 1660 /* 1661 * Query control information (size and flags) for XU controls. 1662 */ 1663 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev, 1664 const struct uvc_control *ctrl, struct uvc_control_info *info) 1665 { 1666 u8 *data; 1667 int ret; 1668 1669 data = kmalloc(2, GFP_KERNEL); 1670 if (data == NULL) 1671 return -ENOMEM; 1672 1673 memcpy(info->entity, ctrl->entity->extension.guidExtensionCode, 1674 sizeof(info->entity)); 1675 info->index = ctrl->index; 1676 info->selector = ctrl->index + 1; 1677 1678 /* Query and verify the control length (GET_LEN) */ 1679 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum, 1680 info->selector, data, 2); 1681 if (ret < 0) { 1682 uvc_trace(UVC_TRACE_CONTROL, 1683 "GET_LEN failed on control %pUl/%u (%d).\n", 1684 info->entity, info->selector, ret); 1685 goto done; 1686 } 1687 1688 info->size = le16_to_cpup((__le16 *)data); 1689 1690 info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX 1691 | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF; 1692 1693 ret = uvc_ctrl_get_flags(dev, ctrl, info); 1694 if (ret < 0) { 1695 uvc_trace(UVC_TRACE_CONTROL, 1696 "Failed to get flags for control %pUl/%u (%d).\n", 1697 info->entity, info->selector, ret); 1698 goto done; 1699 } 1700 1701 uvc_ctrl_fixup_xu_info(dev, ctrl, info); 1702 1703 uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, " 1704 "flags { get %u set %u auto %u }.\n", 1705 info->entity, info->selector, info->size, 1706 (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0, 1707 (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0, 1708 (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0); 1709 1710 done: 1711 kfree(data); 1712 return ret; 1713 } 1714 1715 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl, 1716 const struct uvc_control_info *info); 1717 1718 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev, 1719 struct uvc_control *ctrl) 1720 { 1721 struct uvc_control_info info; 1722 int ret; 1723 1724 if (ctrl->initialized) 1725 return 0; 1726 1727 ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info); 1728 if (ret < 0) 1729 return ret; 1730 1731 ret = uvc_ctrl_add_info(dev, ctrl, &info); 1732 if (ret < 0) 1733 uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control " 1734 "%pUl/%u on device %s entity %u\n", info.entity, 1735 info.selector, dev->udev->devpath, ctrl->entity->id); 1736 1737 return ret; 1738 } 1739 1740 int uvc_xu_ctrl_query(struct uvc_video_chain *chain, 1741 struct uvc_xu_control_query *xqry) 1742 { 1743 struct uvc_entity *entity; 1744 struct uvc_control *ctrl; 1745 unsigned int i, found = 0; 1746 u32 reqflags; 1747 u16 size; 1748 u8 *data = NULL; 1749 int ret; 1750 1751 /* Find the extension unit. */ 1752 list_for_each_entry(entity, &chain->entities, chain) { 1753 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT && 1754 entity->id == xqry->unit) 1755 break; 1756 } 1757 1758 if (entity->id != xqry->unit) { 1759 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n", 1760 xqry->unit); 1761 return -ENOENT; 1762 } 1763 1764 /* Find the control and perform delayed initialization if needed. */ 1765 for (i = 0; i < entity->ncontrols; ++i) { 1766 ctrl = &entity->controls[i]; 1767 if (ctrl->index == xqry->selector - 1) { 1768 found = 1; 1769 break; 1770 } 1771 } 1772 1773 if (!found) { 1774 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n", 1775 entity->extension.guidExtensionCode, xqry->selector); 1776 return -ENOENT; 1777 } 1778 1779 if (mutex_lock_interruptible(&chain->ctrl_mutex)) 1780 return -ERESTARTSYS; 1781 1782 ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl); 1783 if (ret < 0) { 1784 ret = -ENOENT; 1785 goto done; 1786 } 1787 1788 /* Validate the required buffer size and flags for the request */ 1789 reqflags = 0; 1790 size = ctrl->info.size; 1791 1792 switch (xqry->query) { 1793 case UVC_GET_CUR: 1794 reqflags = UVC_CTRL_FLAG_GET_CUR; 1795 break; 1796 case UVC_GET_MIN: 1797 reqflags = UVC_CTRL_FLAG_GET_MIN; 1798 break; 1799 case UVC_GET_MAX: 1800 reqflags = UVC_CTRL_FLAG_GET_MAX; 1801 break; 1802 case UVC_GET_DEF: 1803 reqflags = UVC_CTRL_FLAG_GET_DEF; 1804 break; 1805 case UVC_GET_RES: 1806 reqflags = UVC_CTRL_FLAG_GET_RES; 1807 break; 1808 case UVC_SET_CUR: 1809 reqflags = UVC_CTRL_FLAG_SET_CUR; 1810 break; 1811 case UVC_GET_LEN: 1812 size = 2; 1813 break; 1814 case UVC_GET_INFO: 1815 size = 1; 1816 break; 1817 default: 1818 ret = -EINVAL; 1819 goto done; 1820 } 1821 1822 if (size != xqry->size) { 1823 ret = -ENOBUFS; 1824 goto done; 1825 } 1826 1827 if (reqflags && !(ctrl->info.flags & reqflags)) { 1828 ret = -EBADRQC; 1829 goto done; 1830 } 1831 1832 data = kmalloc(size, GFP_KERNEL); 1833 if (data == NULL) { 1834 ret = -ENOMEM; 1835 goto done; 1836 } 1837 1838 if (xqry->query == UVC_SET_CUR && 1839 copy_from_user(data, xqry->data, size)) { 1840 ret = -EFAULT; 1841 goto done; 1842 } 1843 1844 ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit, 1845 chain->dev->intfnum, xqry->selector, data, size); 1846 if (ret < 0) 1847 goto done; 1848 1849 if (xqry->query != UVC_SET_CUR && 1850 copy_to_user(xqry->data, data, size)) 1851 ret = -EFAULT; 1852 done: 1853 kfree(data); 1854 mutex_unlock(&chain->ctrl_mutex); 1855 return ret; 1856 } 1857 1858 /* -------------------------------------------------------------------------- 1859 * Suspend/resume 1860 */ 1861 1862 /* 1863 * Restore control values after resume, skipping controls that haven't been 1864 * changed. 1865 * 1866 * TODO 1867 * - Don't restore modified controls that are back to their default value. 1868 * - Handle restore order (Auto-Exposure Mode should be restored before 1869 * Exposure Time). 1870 */ 1871 int uvc_ctrl_restore_values(struct uvc_device *dev) 1872 { 1873 struct uvc_control *ctrl; 1874 struct uvc_entity *entity; 1875 unsigned int i; 1876 int ret; 1877 1878 /* Walk the entities list and restore controls when possible. */ 1879 list_for_each_entry(entity, &dev->entities, list) { 1880 1881 for (i = 0; i < entity->ncontrols; ++i) { 1882 ctrl = &entity->controls[i]; 1883 1884 if (!ctrl->initialized || !ctrl->modified || 1885 (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0) 1886 continue; 1887 1888 printk(KERN_INFO "restoring control %pUl/%u/%u\n", 1889 ctrl->info.entity, ctrl->info.index, 1890 ctrl->info.selector); 1891 ctrl->dirty = 1; 1892 } 1893 1894 ret = uvc_ctrl_commit_entity(dev, entity, 0); 1895 if (ret < 0) 1896 return ret; 1897 } 1898 1899 return 0; 1900 } 1901 1902 /* -------------------------------------------------------------------------- 1903 * Control and mapping handling 1904 */ 1905 1906 /* 1907 * Add control information to a given control. 1908 */ 1909 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl, 1910 const struct uvc_control_info *info) 1911 { 1912 int ret = 0; 1913 1914 ctrl->info = *info; 1915 INIT_LIST_HEAD(&ctrl->info.mappings); 1916 1917 /* Allocate an array to save control values (cur, def, max, etc.) */ 1918 ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1, 1919 GFP_KERNEL); 1920 if (ctrl->uvc_data == NULL) { 1921 ret = -ENOMEM; 1922 goto done; 1923 } 1924 1925 /* 1926 * Retrieve control flags from the device. Ignore errors and work with 1927 * default flag values from the uvc_ctrl array when the device doesn't 1928 * properly implement GET_INFO on standard controls. 1929 */ 1930 uvc_ctrl_get_flags(dev, ctrl, &ctrl->info); 1931 1932 ctrl->initialized = 1; 1933 1934 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s " 1935 "entity %u\n", ctrl->info.entity, ctrl->info.selector, 1936 dev->udev->devpath, ctrl->entity->id); 1937 1938 done: 1939 if (ret < 0) 1940 kfree(ctrl->uvc_data); 1941 return ret; 1942 } 1943 1944 /* 1945 * Add a control mapping to a given control. 1946 */ 1947 static int __uvc_ctrl_add_mapping(struct uvc_device *dev, 1948 struct uvc_control *ctrl, const struct uvc_control_mapping *mapping) 1949 { 1950 struct uvc_control_mapping *map; 1951 unsigned int size; 1952 1953 /* Most mappings come from static kernel data and need to be duplicated. 1954 * Mappings that come from userspace will be unnecessarily duplicated, 1955 * this could be optimized. 1956 */ 1957 map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL); 1958 if (map == NULL) 1959 return -ENOMEM; 1960 1961 INIT_LIST_HEAD(&map->ev_subs); 1962 1963 size = sizeof(*mapping->menu_info) * mapping->menu_count; 1964 map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL); 1965 if (map->menu_info == NULL) { 1966 kfree(map); 1967 return -ENOMEM; 1968 } 1969 1970 if (map->get == NULL) 1971 map->get = uvc_get_le_value; 1972 if (map->set == NULL) 1973 map->set = uvc_set_le_value; 1974 1975 list_add_tail(&map->list, &ctrl->info.mappings); 1976 uvc_trace(UVC_TRACE_CONTROL, 1977 "Adding mapping '%s' to control %pUl/%u.\n", 1978 map->name, ctrl->info.entity, ctrl->info.selector); 1979 1980 return 0; 1981 } 1982 1983 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain, 1984 const struct uvc_control_mapping *mapping) 1985 { 1986 struct uvc_device *dev = chain->dev; 1987 struct uvc_control_mapping *map; 1988 struct uvc_entity *entity; 1989 struct uvc_control *ctrl; 1990 int found = 0; 1991 int ret; 1992 1993 if (mapping->id & ~V4L2_CTRL_ID_MASK) { 1994 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control " 1995 "id 0x%08x is invalid.\n", mapping->name, 1996 mapping->id); 1997 return -EINVAL; 1998 } 1999 2000 /* Search for the matching (GUID/CS) control on the current chain */ 2001 list_for_each_entry(entity, &chain->entities, chain) { 2002 unsigned int i; 2003 2004 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT || 2005 !uvc_entity_match_guid(entity, mapping->entity)) 2006 continue; 2007 2008 for (i = 0; i < entity->ncontrols; ++i) { 2009 ctrl = &entity->controls[i]; 2010 if (ctrl->index == mapping->selector - 1) { 2011 found = 1; 2012 break; 2013 } 2014 } 2015 2016 if (found) 2017 break; 2018 } 2019 if (!found) 2020 return -ENOENT; 2021 2022 if (mutex_lock_interruptible(&chain->ctrl_mutex)) 2023 return -ERESTARTSYS; 2024 2025 /* Perform delayed initialization of XU controls */ 2026 ret = uvc_ctrl_init_xu_ctrl(dev, ctrl); 2027 if (ret < 0) { 2028 ret = -ENOENT; 2029 goto done; 2030 } 2031 2032 /* Validate the user-provided bit-size and offset */ 2033 if (mapping->size > 32 || 2034 mapping->offset + mapping->size > ctrl->info.size * 8) { 2035 ret = -EINVAL; 2036 goto done; 2037 } 2038 2039 list_for_each_entry(map, &ctrl->info.mappings, list) { 2040 if (mapping->id == map->id) { 2041 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', " 2042 "control id 0x%08x already exists.\n", 2043 mapping->name, mapping->id); 2044 ret = -EEXIST; 2045 goto done; 2046 } 2047 } 2048 2049 /* Prevent excess memory consumption */ 2050 if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) { 2051 atomic_dec(&dev->nmappings); 2052 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum " 2053 "mappings count (%u) exceeded.\n", mapping->name, 2054 UVC_MAX_CONTROL_MAPPINGS); 2055 ret = -ENOMEM; 2056 goto done; 2057 } 2058 2059 ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping); 2060 if (ret < 0) 2061 atomic_dec(&dev->nmappings); 2062 2063 done: 2064 mutex_unlock(&chain->ctrl_mutex); 2065 return ret; 2066 } 2067 2068 /* 2069 * Prune an entity of its bogus controls using a blacklist. Bogus controls 2070 * are currently the ones that crash the camera or unconditionally return an 2071 * error when queried. 2072 */ 2073 static void uvc_ctrl_prune_entity(struct uvc_device *dev, 2074 struct uvc_entity *entity) 2075 { 2076 struct uvc_ctrl_blacklist { 2077 struct usb_device_id id; 2078 u8 index; 2079 }; 2080 2081 static const struct uvc_ctrl_blacklist processing_blacklist[] = { 2082 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */ 2083 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */ 2084 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */ 2085 }; 2086 static const struct uvc_ctrl_blacklist camera_blacklist[] = { 2087 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */ 2088 }; 2089 2090 const struct uvc_ctrl_blacklist *blacklist; 2091 unsigned int size; 2092 unsigned int count; 2093 unsigned int i; 2094 u8 *controls; 2095 2096 switch (UVC_ENTITY_TYPE(entity)) { 2097 case UVC_VC_PROCESSING_UNIT: 2098 blacklist = processing_blacklist; 2099 count = ARRAY_SIZE(processing_blacklist); 2100 controls = entity->processing.bmControls; 2101 size = entity->processing.bControlSize; 2102 break; 2103 2104 case UVC_ITT_CAMERA: 2105 blacklist = camera_blacklist; 2106 count = ARRAY_SIZE(camera_blacklist); 2107 controls = entity->camera.bmControls; 2108 size = entity->camera.bControlSize; 2109 break; 2110 2111 default: 2112 return; 2113 } 2114 2115 for (i = 0; i < count; ++i) { 2116 if (!usb_match_one_id(dev->intf, &blacklist[i].id)) 2117 continue; 2118 2119 if (blacklist[i].index >= 8 * size || 2120 !uvc_test_bit(controls, blacklist[i].index)) 2121 continue; 2122 2123 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, " 2124 "removing it.\n", entity->id, blacklist[i].index); 2125 2126 uvc_clear_bit(controls, blacklist[i].index); 2127 } 2128 } 2129 2130 /* 2131 * Add control information and hardcoded stock control mappings to the given 2132 * device. 2133 */ 2134 static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl) 2135 { 2136 const struct uvc_control_info *info = uvc_ctrls; 2137 const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls); 2138 const struct uvc_control_mapping *mapping = uvc_ctrl_mappings; 2139 const struct uvc_control_mapping *mend = 2140 mapping + ARRAY_SIZE(uvc_ctrl_mappings); 2141 2142 /* XU controls initialization requires querying the device for control 2143 * information. As some buggy UVC devices will crash when queried 2144 * repeatedly in a tight loop, delay XU controls initialization until 2145 * first use. 2146 */ 2147 if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT) 2148 return; 2149 2150 for (; info < iend; ++info) { 2151 if (uvc_entity_match_guid(ctrl->entity, info->entity) && 2152 ctrl->index == info->index) { 2153 uvc_ctrl_add_info(dev, ctrl, info); 2154 break; 2155 } 2156 } 2157 2158 if (!ctrl->initialized) 2159 return; 2160 2161 for (; mapping < mend; ++mapping) { 2162 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) && 2163 ctrl->info.selector == mapping->selector) 2164 __uvc_ctrl_add_mapping(dev, ctrl, mapping); 2165 } 2166 } 2167 2168 /* 2169 * Initialize device controls. 2170 */ 2171 int uvc_ctrl_init_device(struct uvc_device *dev) 2172 { 2173 struct uvc_entity *entity; 2174 unsigned int i; 2175 2176 /* Walk the entities list and instantiate controls */ 2177 list_for_each_entry(entity, &dev->entities, list) { 2178 struct uvc_control *ctrl; 2179 unsigned int bControlSize = 0, ncontrols; 2180 u8 *bmControls = NULL; 2181 2182 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) { 2183 bmControls = entity->extension.bmControls; 2184 bControlSize = entity->extension.bControlSize; 2185 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) { 2186 bmControls = entity->processing.bmControls; 2187 bControlSize = entity->processing.bControlSize; 2188 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) { 2189 bmControls = entity->camera.bmControls; 2190 bControlSize = entity->camera.bControlSize; 2191 } 2192 2193 /* Remove bogus/blacklisted controls */ 2194 uvc_ctrl_prune_entity(dev, entity); 2195 2196 /* Count supported controls and allocate the controls array */ 2197 ncontrols = memweight(bmControls, bControlSize); 2198 if (ncontrols == 0) 2199 continue; 2200 2201 entity->controls = kcalloc(ncontrols, sizeof(*ctrl), 2202 GFP_KERNEL); 2203 if (entity->controls == NULL) 2204 return -ENOMEM; 2205 entity->ncontrols = ncontrols; 2206 2207 /* Initialize all supported controls */ 2208 ctrl = entity->controls; 2209 for (i = 0; i < bControlSize * 8; ++i) { 2210 if (uvc_test_bit(bmControls, i) == 0) 2211 continue; 2212 2213 ctrl->entity = entity; 2214 ctrl->index = i; 2215 2216 uvc_ctrl_init_ctrl(dev, ctrl); 2217 ctrl++; 2218 } 2219 } 2220 2221 return 0; 2222 } 2223 2224 /* 2225 * Cleanup device controls. 2226 */ 2227 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev, 2228 struct uvc_control *ctrl) 2229 { 2230 struct uvc_control_mapping *mapping, *nm; 2231 2232 list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) { 2233 list_del(&mapping->list); 2234 kfree(mapping->menu_info); 2235 kfree(mapping); 2236 } 2237 } 2238 2239 void uvc_ctrl_cleanup_device(struct uvc_device *dev) 2240 { 2241 struct uvc_entity *entity; 2242 unsigned int i; 2243 2244 /* Free controls and control mappings for all entities. */ 2245 list_for_each_entry(entity, &dev->entities, list) { 2246 for (i = 0; i < entity->ncontrols; ++i) { 2247 struct uvc_control *ctrl = &entity->controls[i]; 2248 2249 if (!ctrl->initialized) 2250 continue; 2251 2252 uvc_ctrl_cleanup_mappings(dev, ctrl); 2253 kfree(ctrl->uvc_data); 2254 } 2255 2256 kfree(entity->controls); 2257 } 2258 } 2259