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