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