2 * uvc_ctrl.c -- USB Video Class driver - Controls
4 * Copyright (C) 2005-2009
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
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.
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 <asm/atomic.h>
27 #define UVC_CTRL_DATA_CURRENT 0
28 #define UVC_CTRL_DATA_BACKUP 1
29 #define UVC_CTRL_DATA_MIN 2
30 #define UVC_CTRL_DATA_MAX 3
31 #define UVC_CTRL_DATA_RES 4
32 #define UVC_CTRL_DATA_DEF 5
33 #define UVC_CTRL_DATA_LAST 6
35 /* ------------------------------------------------------------------------
39 static struct uvc_control_info uvc_ctrls[] = {
41 .entity = UVC_GUID_UVC_PROCESSING,
42 .selector = UVC_PU_BRIGHTNESS_CONTROL,
45 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
46 | UVC_CONTROL_RESTORE,
49 .entity = UVC_GUID_UVC_PROCESSING,
50 .selector = UVC_PU_CONTRAST_CONTROL,
53 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
54 | UVC_CONTROL_RESTORE,
57 .entity = UVC_GUID_UVC_PROCESSING,
58 .selector = UVC_PU_HUE_CONTROL,
61 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
62 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
65 .entity = UVC_GUID_UVC_PROCESSING,
66 .selector = UVC_PU_SATURATION_CONTROL,
69 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
70 | UVC_CONTROL_RESTORE,
73 .entity = UVC_GUID_UVC_PROCESSING,
74 .selector = UVC_PU_SHARPNESS_CONTROL,
77 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
78 | UVC_CONTROL_RESTORE,
81 .entity = UVC_GUID_UVC_PROCESSING,
82 .selector = UVC_PU_GAMMA_CONTROL,
85 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
86 | UVC_CONTROL_RESTORE,
89 .entity = UVC_GUID_UVC_PROCESSING,
90 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
93 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
94 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
97 .entity = UVC_GUID_UVC_PROCESSING,
98 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
101 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
102 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
105 .entity = UVC_GUID_UVC_PROCESSING,
106 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
109 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
110 | UVC_CONTROL_RESTORE,
113 .entity = UVC_GUID_UVC_PROCESSING,
114 .selector = UVC_PU_GAIN_CONTROL,
117 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
118 | UVC_CONTROL_RESTORE,
121 .entity = UVC_GUID_UVC_PROCESSING,
122 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
125 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
126 | UVC_CONTROL_RESTORE,
129 .entity = UVC_GUID_UVC_PROCESSING,
130 .selector = UVC_PU_HUE_AUTO_CONTROL,
133 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
134 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
137 .entity = UVC_GUID_UVC_PROCESSING,
138 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
141 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
142 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
145 .entity = UVC_GUID_UVC_PROCESSING,
146 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
149 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
150 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
153 .entity = UVC_GUID_UVC_PROCESSING,
154 .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
157 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
158 | UVC_CONTROL_RESTORE,
161 .entity = UVC_GUID_UVC_PROCESSING,
162 .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
165 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
166 | UVC_CONTROL_RESTORE,
169 .entity = UVC_GUID_UVC_PROCESSING,
170 .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
173 .flags = UVC_CONTROL_GET_CUR,
176 .entity = UVC_GUID_UVC_PROCESSING,
177 .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
180 .flags = UVC_CONTROL_GET_CUR,
183 .entity = UVC_GUID_UVC_CAMERA,
184 .selector = UVC_CT_SCANNING_MODE_CONTROL,
187 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
188 | UVC_CONTROL_RESTORE,
191 .entity = UVC_GUID_UVC_CAMERA,
192 .selector = UVC_CT_AE_MODE_CONTROL,
195 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
196 | UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES
197 | UVC_CONTROL_RESTORE,
200 .entity = UVC_GUID_UVC_CAMERA,
201 .selector = UVC_CT_AE_PRIORITY_CONTROL,
204 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
205 | UVC_CONTROL_RESTORE,
208 .entity = UVC_GUID_UVC_CAMERA,
209 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
212 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
213 | UVC_CONTROL_RESTORE,
216 .entity = UVC_GUID_UVC_CAMERA,
217 .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
220 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_RESTORE,
223 .entity = UVC_GUID_UVC_CAMERA,
224 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
227 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
228 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
231 .entity = UVC_GUID_UVC_CAMERA,
232 .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
235 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
236 | UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
237 | UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
240 .entity = UVC_GUID_UVC_CAMERA,
241 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
244 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
245 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
248 .entity = UVC_GUID_UVC_CAMERA,
249 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
252 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_AUTO_UPDATE,
255 .entity = UVC_GUID_UVC_CAMERA,
256 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
259 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
260 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
263 .entity = UVC_GUID_UVC_CAMERA,
264 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
267 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
268 | UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
269 | UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
272 .entity = UVC_GUID_UVC_CAMERA,
273 .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
276 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
277 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
280 .entity = UVC_GUID_UVC_CAMERA,
281 .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
284 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
285 | UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
286 | UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
289 .entity = UVC_GUID_UVC_CAMERA,
290 .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
293 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
294 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
297 .entity = UVC_GUID_UVC_CAMERA,
298 .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
301 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_MIN
302 | UVC_CONTROL_GET_MAX | UVC_CONTROL_GET_RES
303 | UVC_CONTROL_GET_DEF | UVC_CONTROL_AUTO_UPDATE,
306 .entity = UVC_GUID_UVC_CAMERA,
307 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
310 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
311 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
314 .entity = UVC_GUID_UVC_CAMERA,
315 .selector = UVC_CT_PRIVACY_CONTROL,
318 .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
319 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
323 static struct uvc_menu_info power_line_frequency_controls[] = {
329 static struct uvc_menu_info exposure_auto_controls[] = {
331 { 1, "Manual Mode" },
332 { 4, "Shutter Priority Mode" },
333 { 8, "Aperture Priority Mode" },
336 static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
337 __u8 query, const __u8 *data)
339 __s8 zoom = (__s8)data[0];
343 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
354 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
355 __s32 value, __u8 *data)
357 data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
358 data[2] = min((int)abs(value), 0xff);
361 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
363 .id = V4L2_CID_BRIGHTNESS,
364 .name = "Brightness",
365 .entity = UVC_GUID_UVC_PROCESSING,
366 .selector = UVC_PU_BRIGHTNESS_CONTROL,
369 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
370 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
373 .id = V4L2_CID_CONTRAST,
375 .entity = UVC_GUID_UVC_PROCESSING,
376 .selector = UVC_PU_CONTRAST_CONTROL,
379 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
380 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
385 .entity = UVC_GUID_UVC_PROCESSING,
386 .selector = UVC_PU_HUE_CONTROL,
389 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
390 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
393 .id = V4L2_CID_SATURATION,
394 .name = "Saturation",
395 .entity = UVC_GUID_UVC_PROCESSING,
396 .selector = UVC_PU_SATURATION_CONTROL,
399 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
400 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
403 .id = V4L2_CID_SHARPNESS,
405 .entity = UVC_GUID_UVC_PROCESSING,
406 .selector = UVC_PU_SHARPNESS_CONTROL,
409 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
410 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
413 .id = V4L2_CID_GAMMA,
415 .entity = UVC_GUID_UVC_PROCESSING,
416 .selector = UVC_PU_GAMMA_CONTROL,
419 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
420 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
423 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
424 .name = "Backlight Compensation",
425 .entity = UVC_GUID_UVC_PROCESSING,
426 .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
429 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
430 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
435 .entity = UVC_GUID_UVC_PROCESSING,
436 .selector = UVC_PU_GAIN_CONTROL,
439 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
440 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
443 .id = V4L2_CID_POWER_LINE_FREQUENCY,
444 .name = "Power Line Frequency",
445 .entity = UVC_GUID_UVC_PROCESSING,
446 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
449 .v4l2_type = V4L2_CTRL_TYPE_MENU,
450 .data_type = UVC_CTRL_DATA_TYPE_ENUM,
451 .menu_info = power_line_frequency_controls,
452 .menu_count = ARRAY_SIZE(power_line_frequency_controls),
455 .id = V4L2_CID_HUE_AUTO,
457 .entity = UVC_GUID_UVC_PROCESSING,
458 .selector = UVC_PU_HUE_AUTO_CONTROL,
461 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
462 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
465 .id = V4L2_CID_EXPOSURE_AUTO,
466 .name = "Exposure, Auto",
467 .entity = UVC_GUID_UVC_CAMERA,
468 .selector = UVC_CT_AE_MODE_CONTROL,
471 .v4l2_type = V4L2_CTRL_TYPE_MENU,
472 .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
473 .menu_info = exposure_auto_controls,
474 .menu_count = ARRAY_SIZE(exposure_auto_controls),
477 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
478 .name = "Exposure, Auto Priority",
479 .entity = UVC_GUID_UVC_CAMERA,
480 .selector = UVC_CT_AE_PRIORITY_CONTROL,
483 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
484 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
487 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
488 .name = "Exposure (Absolute)",
489 .entity = UVC_GUID_UVC_CAMERA,
490 .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
493 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
494 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
497 .id = V4L2_CID_AUTO_WHITE_BALANCE,
498 .name = "White Balance Temperature, Auto",
499 .entity = UVC_GUID_UVC_PROCESSING,
500 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
503 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
504 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
507 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
508 .name = "White Balance Temperature",
509 .entity = UVC_GUID_UVC_PROCESSING,
510 .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
513 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
514 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
517 .id = V4L2_CID_AUTO_WHITE_BALANCE,
518 .name = "White Balance Component, Auto",
519 .entity = UVC_GUID_UVC_PROCESSING,
520 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
523 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
524 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
527 .id = V4L2_CID_BLUE_BALANCE,
528 .name = "White Balance Blue Component",
529 .entity = UVC_GUID_UVC_PROCESSING,
530 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
533 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
534 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
537 .id = V4L2_CID_RED_BALANCE,
538 .name = "White Balance Red Component",
539 .entity = UVC_GUID_UVC_PROCESSING,
540 .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
543 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
544 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
547 .id = V4L2_CID_FOCUS_ABSOLUTE,
548 .name = "Focus (absolute)",
549 .entity = UVC_GUID_UVC_CAMERA,
550 .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
553 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
554 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
557 .id = V4L2_CID_FOCUS_AUTO,
558 .name = "Focus, Auto",
559 .entity = UVC_GUID_UVC_CAMERA,
560 .selector = UVC_CT_FOCUS_AUTO_CONTROL,
563 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
564 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
567 .id = V4L2_CID_IRIS_ABSOLUTE,
568 .name = "Iris, Absolute",
569 .entity = UVC_GUID_UVC_CAMERA,
570 .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
573 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
574 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
577 .id = V4L2_CID_IRIS_RELATIVE,
578 .name = "Iris, Relative",
579 .entity = UVC_GUID_UVC_CAMERA,
580 .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
583 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
584 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
587 .id = V4L2_CID_ZOOM_ABSOLUTE,
588 .name = "Zoom, Absolute",
589 .entity = UVC_GUID_UVC_CAMERA,
590 .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
593 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
594 .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
597 .id = V4L2_CID_ZOOM_CONTINUOUS,
598 .name = "Zoom, Continuous",
599 .entity = UVC_GUID_UVC_CAMERA,
600 .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
603 .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
604 .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
605 .get = uvc_ctrl_get_zoom,
606 .set = uvc_ctrl_set_zoom,
609 .id = V4L2_CID_PRIVACY,
611 .entity = UVC_GUID_UVC_CAMERA,
612 .selector = UVC_CT_PRIVACY_CONTROL,
615 .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
616 .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
620 /* ------------------------------------------------------------------------
624 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
626 return ctrl->data + id * ctrl->info->size;
629 static inline int uvc_test_bit(const __u8 *data, int bit)
631 return (data[bit >> 3] >> (bit & 7)) & 1;
634 static inline void uvc_clear_bit(__u8 *data, int bit)
636 data[bit >> 3] &= ~(1 << (bit & 7));
639 /* Extract the bit string specified by mapping->offset and mapping->size
640 * from the little-endian data stored at 'data' and return the result as
641 * a signed 32bit integer. Sign extension will be performed if the mapping
642 * references a signed data type.
644 static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
645 __u8 query, const __u8 *data)
647 int bits = mapping->size;
648 int offset = mapping->offset;
654 mask = ((1LL << bits) - 1) << offset;
656 for (; bits > 0; data++) {
657 __u8 byte = *data & mask;
658 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
659 bits -= 8 - (offset > 0 ? offset : 0);
661 mask = (1 << bits) - 1;
664 /* Sign-extend the value if needed. */
665 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
666 value |= -(value & (1 << (mapping->size - 1)));
671 /* Set the bit string specified by mapping->offset and mapping->size
672 * in the little-endian data stored at 'data' to the value 'value'.
674 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
675 __s32 value, __u8 *data)
677 int bits = mapping->size;
678 int offset = mapping->offset;
684 for (; bits > 0; data++) {
685 mask = ((1LL << bits) - 1) << offset;
686 *data = (*data & ~mask) | ((value << offset) & mask);
687 value >>= offset ? offset : 8;
693 /* ------------------------------------------------------------------------
694 * Terminal and unit management
697 static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
698 static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
699 static const __u8 uvc_media_transport_input_guid[16] =
700 UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
702 static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
704 switch (UVC_ENTITY_TYPE(entity)) {
706 return memcmp(uvc_camera_guid, guid, 16) == 0;
708 case UVC_ITT_MEDIA_TRANSPORT_INPUT:
709 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
711 case UVC_VC_PROCESSING_UNIT:
712 return memcmp(uvc_processing_guid, guid, 16) == 0;
714 case UVC_VC_EXTENSION_UNIT:
715 return memcmp(entity->extension.guidExtensionCode,
723 /* ------------------------------------------------------------------------
727 static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
728 struct uvc_control_mapping **mapping, struct uvc_control **control,
731 struct uvc_control *ctrl;
732 struct uvc_control_mapping *map;
738 for (i = 0; i < entity->ncontrols; ++i) {
739 ctrl = &entity->controls[i];
740 if (ctrl->info == NULL)
743 list_for_each_entry(map, &ctrl->info->mappings, list) {
744 if ((map->id == v4l2_id) && !next) {
750 if ((*mapping == NULL || (*mapping)->id > map->id) &&
751 (map->id > v4l2_id) && next) {
759 struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
760 __u32 v4l2_id, struct uvc_control_mapping **mapping)
762 struct uvc_control *ctrl = NULL;
763 struct uvc_entity *entity;
764 int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
768 /* Mask the query flags. */
769 v4l2_id &= V4L2_CTRL_ID_MASK;
771 /* Find the control. */
772 list_for_each_entry(entity, &chain->entities, chain) {
773 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
778 if (ctrl == NULL && !next)
779 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
785 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
786 struct uvc_control *ctrl)
790 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
791 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
792 chain->dev->intfnum, ctrl->info->selector,
793 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
799 if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
800 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
801 chain->dev->intfnum, ctrl->info->selector,
802 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
807 if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
808 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
809 chain->dev->intfnum, ctrl->info->selector,
810 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
815 if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
816 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
817 chain->dev->intfnum, ctrl->info->selector,
818 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
828 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
829 struct v4l2_queryctrl *v4l2_ctrl)
831 struct uvc_control *ctrl;
832 struct uvc_control_mapping *mapping;
833 struct uvc_menu_info *menu;
837 ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
841 memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
842 v4l2_ctrl->id = mapping->id;
843 v4l2_ctrl->type = mapping->v4l2_type;
844 strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
845 v4l2_ctrl->flags = 0;
847 if (!(ctrl->info->flags & UVC_CONTROL_GET_CUR))
848 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
849 if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
850 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
853 ret = uvc_ctrl_populate_cache(chain, ctrl);
858 if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
859 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
860 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
863 switch (mapping->v4l2_type) {
864 case V4L2_CTRL_TYPE_MENU:
865 v4l2_ctrl->minimum = 0;
866 v4l2_ctrl->maximum = mapping->menu_count - 1;
869 menu = mapping->menu_info;
870 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
871 if (menu->value == v4l2_ctrl->default_value) {
872 v4l2_ctrl->default_value = i;
879 case V4L2_CTRL_TYPE_BOOLEAN:
880 v4l2_ctrl->minimum = 0;
881 v4l2_ctrl->maximum = 1;
885 case V4L2_CTRL_TYPE_BUTTON:
886 v4l2_ctrl->minimum = 0;
887 v4l2_ctrl->maximum = 0;
895 if (ctrl->info->flags & UVC_CONTROL_GET_MIN)
896 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
897 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
899 if (ctrl->info->flags & UVC_CONTROL_GET_MAX)
900 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
901 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
903 if (ctrl->info->flags & UVC_CONTROL_GET_RES)
904 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
905 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
911 /* --------------------------------------------------------------------------
912 * Control transactions
914 * To make extended set operations as atomic as the hardware allows, controls
915 * are handled using begin/commit/rollback operations.
917 * At the beginning of a set request, uvc_ctrl_begin should be called to
918 * initialize the request. This function acquires the control lock.
920 * When setting a control, the new value is stored in the control data field
921 * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
922 * later processing. If the UVC and V4L2 control sizes differ, the current
923 * value is loaded from the hardware before storing the new value in the data
926 * After processing all controls in the transaction, uvc_ctrl_commit or
927 * uvc_ctrl_rollback must be called to apply the pending changes to the
928 * hardware or revert them. When applying changes, all controls marked as
929 * dirty will be modified in the UVC device, and the dirty flag will be
930 * cleared. When reverting controls, the control data field
931 * UVC_CTRL_DATA_CURRENT is reverted to its previous value
932 * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
935 int uvc_ctrl_begin(struct uvc_video_chain *chain)
937 return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
940 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
941 struct uvc_entity *entity, int rollback)
943 struct uvc_control *ctrl;
950 for (i = 0; i < entity->ncontrols; ++i) {
951 ctrl = &entity->controls[i];
952 if (ctrl->info == NULL)
955 /* Reset the loaded flag for auto-update controls that were
956 * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
957 * uvc_ctrl_get from using the cached value.
959 if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
966 ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
967 dev->intfnum, ctrl->info->selector,
968 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
973 if (rollback || ret < 0)
974 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
975 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
987 int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
989 struct uvc_entity *entity;
992 /* Find the control. */
993 list_for_each_entry(entity, &chain->entities, chain) {
994 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1000 mutex_unlock(&chain->ctrl_mutex);
1004 int uvc_ctrl_get(struct uvc_video_chain *chain,
1005 struct v4l2_ext_control *xctrl)
1007 struct uvc_control *ctrl;
1008 struct uvc_control_mapping *mapping;
1009 struct uvc_menu_info *menu;
1013 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1014 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
1017 if (!ctrl->loaded) {
1018 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
1019 chain->dev->intfnum, ctrl->info->selector,
1020 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1028 xctrl->value = mapping->get(mapping, UVC_GET_CUR,
1029 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1031 if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1032 menu = mapping->menu_info;
1033 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1034 if (menu->value == xctrl->value) {
1044 int uvc_ctrl_set(struct uvc_video_chain *chain,
1045 struct v4l2_ext_control *xctrl)
1047 struct uvc_control *ctrl;
1048 struct uvc_control_mapping *mapping;
1055 ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1056 if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
1059 /* Clamp out of range values. */
1060 switch (mapping->v4l2_type) {
1061 case V4L2_CTRL_TYPE_INTEGER:
1062 if (!ctrl->cached) {
1063 ret = uvc_ctrl_populate_cache(chain, ctrl);
1068 min = mapping->get(mapping, UVC_GET_MIN,
1069 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1070 max = mapping->get(mapping, UVC_GET_MAX,
1071 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1072 step = mapping->get(mapping, UVC_GET_RES,
1073 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1075 xctrl->value = min + (xctrl->value - min + step/2) / step * step;
1076 xctrl->value = clamp(xctrl->value, min, max);
1077 value = xctrl->value;
1080 case V4L2_CTRL_TYPE_BOOLEAN:
1081 xctrl->value = clamp(xctrl->value, 0, 1);
1082 value = xctrl->value;
1085 case V4L2_CTRL_TYPE_MENU:
1086 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1088 value = mapping->menu_info[xctrl->value].value;
1092 value = xctrl->value;
1096 /* If the mapping doesn't span the whole UVC control, the current value
1097 * needs to be loaded from the device to perform the read-modify-write
1100 if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
1101 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
1102 memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1103 0, ctrl->info->size);
1105 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1106 ctrl->entity->id, chain->dev->intfnum,
1107 ctrl->info->selector,
1108 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1117 /* Backup the current value in case we need to rollback later. */
1119 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1120 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1124 mapping->set(mapping, value,
1125 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1132 /* --------------------------------------------------------------------------
1136 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1137 struct uvc_xu_control *xctrl, int set)
1139 struct uvc_entity *entity;
1140 struct uvc_control *ctrl = NULL;
1141 unsigned int i, found = 0;
1145 /* Find the extension unit. */
1146 list_for_each_entry(entity, &chain->entities, chain) {
1147 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1148 entity->id == xctrl->unit)
1152 if (entity->id != xctrl->unit) {
1153 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1158 /* Find the control. */
1159 for (i = 0; i < entity->ncontrols; ++i) {
1160 ctrl = &entity->controls[i];
1161 if (ctrl->info == NULL)
1164 if (ctrl->info->selector == xctrl->selector) {
1171 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1172 entity->extension.guidExtensionCode, xctrl->selector);
1176 /* Validate control data size. */
1177 if (ctrl->info->size != xctrl->size)
1180 if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
1181 (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
1184 if (mutex_lock_interruptible(&chain->ctrl_mutex))
1185 return -ERESTARTSYS;
1187 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1188 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1190 data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1192 if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1197 ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR,
1198 xctrl->unit, chain->dev->intfnum, xctrl->selector,
1203 if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1210 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1211 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1214 mutex_unlock(&chain->ctrl_mutex);
1218 /* --------------------------------------------------------------------------
1223 * Restore control values after resume, skipping controls that haven't been
1227 * - Don't restore modified controls that are back to their default value.
1228 * - Handle restore order (Auto-Exposure Mode should be restored before
1231 int uvc_ctrl_resume_device(struct uvc_device *dev)
1233 struct uvc_control *ctrl;
1234 struct uvc_entity *entity;
1238 /* Walk the entities list and restore controls when possible. */
1239 list_for_each_entry(entity, &dev->entities, list) {
1241 for (i = 0; i < entity->ncontrols; ++i) {
1242 ctrl = &entity->controls[i];
1244 if (ctrl->info == NULL || !ctrl->modified ||
1245 (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
1248 printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1249 ctrl->info->entity, ctrl->info->index,
1250 ctrl->info->selector);
1254 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1262 /* --------------------------------------------------------------------------
1263 * Control and mapping handling
1266 static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
1267 struct uvc_control_info *info)
1269 struct uvc_entity *entity;
1270 struct uvc_control *ctrl = NULL;
1274 list_for_each_entry(entity, &dev->entities, list) {
1275 if (!uvc_entity_match_guid(entity, info->entity))
1278 for (i = 0; i < entity->ncontrols; ++i) {
1279 ctrl = &entity->controls[i];
1280 if (ctrl->index == info->index) {
1293 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1294 /* Check if the device control information and length match
1295 * the user supplied information.
1301 ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id,
1302 dev->intfnum, info->selector, (__u8 *)&size, 2);
1304 uvc_trace(UVC_TRACE_CONTROL,
1305 "GET_LEN failed on control %pUl/%u (%d).\n",
1306 info->entity, info->selector, ret);
1310 if (info->size != le16_to_cpu(size)) {
1311 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u size "
1312 "doesn't match user supplied value.\n",
1313 info->entity, info->selector);
1317 ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
1318 dev->intfnum, info->selector, &inf, 1);
1320 uvc_trace(UVC_TRACE_CONTROL,
1321 "GET_INFO failed on control %pUl/%u (%d).\n",
1322 info->entity, info->selector, ret);
1326 flags = info->flags;
1327 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1328 ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
1329 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u flags "
1330 "don't match supported operations.\n",
1331 info->entity, info->selector);
1337 ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_DATA_LAST, GFP_KERNEL);
1338 uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1339 "entity %u\n", ctrl->info->entity, ctrl->info->selector,
1340 dev->udev->devpath, entity->id);
1344 * Add an item to the UVC control information list, and instantiate a control
1345 * structure for each device that supports the control.
1347 int uvc_ctrl_add_info(struct uvc_control_info *info)
1349 struct uvc_control_info *ctrl;
1350 struct uvc_device *dev;
1353 /* Find matching controls by walking the devices, entities and
1356 mutex_lock(&uvc_driver.ctrl_mutex);
1358 /* First check if the list contains a control matching the new one.
1359 * Bail out if it does.
1361 list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1362 if (memcmp(ctrl->entity, info->entity, 16))
1365 if (ctrl->selector == info->selector) {
1366 uvc_trace(UVC_TRACE_CONTROL,
1367 "Control %pUl/%u is already defined.\n",
1368 info->entity, info->selector);
1372 if (ctrl->index == info->index) {
1373 uvc_trace(UVC_TRACE_CONTROL,
1374 "Control %pUl/%u would overwrite index %d.\n",
1375 info->entity, info->selector, info->index);
1381 list_for_each_entry(dev, &uvc_driver.devices, list)
1382 uvc_ctrl_add_ctrl(dev, info);
1384 INIT_LIST_HEAD(&info->mappings);
1385 list_add_tail(&info->list, &uvc_driver.controls);
1387 mutex_unlock(&uvc_driver.ctrl_mutex);
1391 int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1393 struct uvc_control_info *info;
1394 struct uvc_control_mapping *map;
1397 if (mapping->get == NULL)
1398 mapping->get = uvc_get_le_value;
1399 if (mapping->set == NULL)
1400 mapping->set = uvc_set_le_value;
1402 if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1403 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1404 "invalid control id 0x%08x\n", mapping->name,
1409 mutex_lock(&uvc_driver.ctrl_mutex);
1410 list_for_each_entry(info, &uvc_driver.controls, list) {
1411 if (memcmp(info->entity, mapping->entity, 16) ||
1412 info->selector != mapping->selector)
1415 if (info->size * 8 < mapping->size + mapping->offset) {
1416 uvc_trace(UVC_TRACE_CONTROL,
1417 "Mapping '%s' would overflow control %pUl/%u\n",
1418 mapping->name, info->entity, info->selector);
1423 /* Check if the list contains a mapping matching the new one.
1424 * Bail out if it does.
1426 list_for_each_entry(map, &info->mappings, list) {
1427 if (map->id == mapping->id) {
1428 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1429 "already defined.\n", mapping->name);
1435 mapping->ctrl = info;
1436 list_add_tail(&mapping->list, &info->mappings);
1437 uvc_trace(UVC_TRACE_CONTROL,
1438 "Adding mapping %s to control %pUl/%u.\n",
1439 mapping->name, info->entity, info->selector);
1445 mutex_unlock(&uvc_driver.ctrl_mutex);
1450 * Prune an entity of its bogus controls using a blacklist. Bogus controls
1451 * are currently the ones that crash the camera or unconditionally return an
1452 * error when queried.
1455 uvc_ctrl_prune_entity(struct uvc_device *dev, struct uvc_entity *entity)
1457 static const struct {
1458 struct usb_device_id id;
1461 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
1462 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1463 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1470 if (UVC_ENTITY_TYPE(entity) != UVC_VC_PROCESSING_UNIT)
1473 controls = entity->processing.bmControls;
1474 size = entity->processing.bControlSize;
1476 for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
1477 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
1480 if (blacklist[i].index >= 8 * size ||
1481 !uvc_test_bit(controls, blacklist[i].index))
1484 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
1485 "removing it.\n", entity->id, blacklist[i].index);
1487 uvc_clear_bit(controls, blacklist[i].index);
1492 * Initialize device controls.
1494 int uvc_ctrl_init_device(struct uvc_device *dev)
1496 struct uvc_control_info *info;
1497 struct uvc_control *ctrl;
1498 struct uvc_entity *entity;
1501 /* Walk the entities list and instantiate controls */
1502 list_for_each_entry(entity, &dev->entities, list) {
1503 unsigned int bControlSize = 0, ncontrols = 0;
1504 __u8 *bmControls = NULL;
1506 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1507 bmControls = entity->extension.bmControls;
1508 bControlSize = entity->extension.bControlSize;
1509 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
1510 bmControls = entity->processing.bmControls;
1511 bControlSize = entity->processing.bControlSize;
1512 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
1513 bmControls = entity->camera.bmControls;
1514 bControlSize = entity->camera.bControlSize;
1517 uvc_ctrl_prune_entity(dev, entity);
1519 for (i = 0; i < bControlSize; ++i)
1520 ncontrols += hweight8(bmControls[i]);
1525 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1526 if (entity->controls == NULL)
1529 entity->ncontrols = ncontrols;
1531 ctrl = entity->controls;
1532 for (i = 0; i < bControlSize * 8; ++i) {
1533 if (uvc_test_bit(bmControls, i) == 0)
1536 ctrl->entity = entity;
1542 /* Walk the controls info list and associate them with the device
1543 * controls, then add the device to the global device list. This has
1544 * to be done while holding the controls lock, to make sure
1545 * uvc_ctrl_add_info() will not get called in-between.
1547 mutex_lock(&uvc_driver.ctrl_mutex);
1548 list_for_each_entry(info, &uvc_driver.controls, list)
1549 uvc_ctrl_add_ctrl(dev, info);
1551 list_add_tail(&dev->list, &uvc_driver.devices);
1552 mutex_unlock(&uvc_driver.ctrl_mutex);
1558 * Cleanup device controls.
1560 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1562 struct uvc_entity *entity;
1565 /* Remove the device from the global devices list */
1566 mutex_lock(&uvc_driver.ctrl_mutex);
1567 if (dev->list.next != NULL)
1568 list_del(&dev->list);
1569 mutex_unlock(&uvc_driver.ctrl_mutex);
1571 list_for_each_entry(entity, &dev->entities, list) {
1572 for (i = 0; i < entity->ncontrols; ++i)
1573 kfree(entity->controls[i].data);
1575 kfree(entity->controls);
1579 void uvc_ctrl_init(void)
1581 struct uvc_control_info *ctrl = uvc_ctrls;
1582 struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1583 struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1584 struct uvc_control_mapping *mend =
1585 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1587 for (; ctrl < cend; ++ctrl)
1588 uvc_ctrl_add_info(ctrl);
1590 for (; mapping < mend; ++mapping)
1591 uvc_ctrl_add_mapping(mapping);