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 | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 4 | * Copyright (C) 2005-2009 |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 5 | * Laurent Pinchart (laurent.pinchart@skynet.be) |
| 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> |
| 17 | #include <linux/uaccess.h> |
| 18 | #include <linux/usb.h> |
| 19 | #include <linux/videodev2.h> |
| 20 | #include <linux/vmalloc.h> |
| 21 | #include <linux/wait.h> |
| 22 | #include <asm/atomic.h> |
| 23 | |
| 24 | #include "uvcvideo.h" |
| 25 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 26 | #define UVC_CTRL_DATA_CURRENT 0 |
| 27 | #define UVC_CTRL_DATA_BACKUP 1 |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 28 | #define UVC_CTRL_DATA_MIN 2 |
| 29 | #define UVC_CTRL_DATA_MAX 3 |
| 30 | #define UVC_CTRL_DATA_RES 4 |
| 31 | #define UVC_CTRL_DATA_DEF 5 |
| 32 | #define UVC_CTRL_DATA_LAST 6 |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 33 | |
| 34 | /* ------------------------------------------------------------------------ |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 35 | * Controls |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | static struct uvc_control_info uvc_ctrls[] = { |
| 39 | { |
| 40 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 41 | .selector = UVC_PU_BRIGHTNESS_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 42 | .index = 0, |
| 43 | .size = 2, |
| 44 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 45 | | UVC_CONTROL_RESTORE, |
| 46 | }, |
| 47 | { |
| 48 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 49 | .selector = UVC_PU_CONTRAST_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 50 | .index = 1, |
| 51 | .size = 2, |
| 52 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 53 | | UVC_CONTROL_RESTORE, |
| 54 | }, |
| 55 | { |
| 56 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 57 | .selector = UVC_PU_HUE_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 58 | .index = 2, |
| 59 | .size = 2, |
| 60 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 61 | | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE, |
| 62 | }, |
| 63 | { |
| 64 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 65 | .selector = UVC_PU_SATURATION_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 66 | .index = 3, |
| 67 | .size = 2, |
| 68 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 69 | | UVC_CONTROL_RESTORE, |
| 70 | }, |
| 71 | { |
| 72 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 73 | .selector = UVC_PU_SHARPNESS_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 74 | .index = 4, |
| 75 | .size = 2, |
| 76 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 77 | | UVC_CONTROL_RESTORE, |
| 78 | }, |
| 79 | { |
| 80 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 81 | .selector = UVC_PU_GAMMA_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 82 | .index = 5, |
| 83 | .size = 2, |
| 84 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 85 | | UVC_CONTROL_RESTORE, |
| 86 | }, |
| 87 | { |
| 88 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 89 | .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 90 | .index = 6, |
| 91 | .size = 2, |
| 92 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 93 | | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE, |
| 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_COMPONENT_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 98 | .index = 7, |
| 99 | .size = 4, |
| 100 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 101 | | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE, |
| 102 | }, |
| 103 | { |
| 104 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 105 | .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 106 | .index = 8, |
| 107 | .size = 2, |
| 108 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 109 | | UVC_CONTROL_RESTORE, |
| 110 | }, |
| 111 | { |
| 112 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 113 | .selector = UVC_PU_GAIN_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 114 | .index = 9, |
| 115 | .size = 2, |
| 116 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 117 | | UVC_CONTROL_RESTORE, |
| 118 | }, |
| 119 | { |
| 120 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 121 | .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 122 | .index = 10, |
| 123 | .size = 1, |
| 124 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 125 | | UVC_CONTROL_RESTORE, |
| 126 | }, |
| 127 | { |
| 128 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 129 | .selector = UVC_PU_HUE_AUTO_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 130 | .index = 11, |
| 131 | .size = 1, |
| 132 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR |
| 133 | | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE, |
| 134 | }, |
| 135 | { |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 136 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 137 | .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 138 | .index = 12, |
| 139 | .size = 1, |
| 140 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR |
| 141 | | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE, |
| 142 | }, |
| 143 | { |
| 144 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 145 | .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 146 | .index = 13, |
| 147 | .size = 1, |
| 148 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR |
| 149 | | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE, |
| 150 | }, |
| 151 | { |
| 152 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 153 | .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 154 | .index = 14, |
| 155 | .size = 2, |
| 156 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 157 | | UVC_CONTROL_RESTORE, |
| 158 | }, |
| 159 | { |
| 160 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 161 | .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 162 | .index = 15, |
| 163 | .size = 2, |
| 164 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 165 | | UVC_CONTROL_RESTORE, |
| 166 | }, |
| 167 | { |
| 168 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 169 | .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 170 | .index = 16, |
| 171 | .size = 1, |
| 172 | .flags = UVC_CONTROL_GET_CUR, |
| 173 | }, |
| 174 | { |
| 175 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 176 | .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 177 | .index = 17, |
| 178 | .size = 1, |
| 179 | .flags = UVC_CONTROL_GET_CUR, |
| 180 | }, |
| 181 | { |
| 182 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 183 | .selector = UVC_CT_SCANNING_MODE_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 184 | .index = 0, |
| 185 | .size = 1, |
| 186 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR |
| 187 | | UVC_CONTROL_RESTORE, |
| 188 | }, |
| 189 | { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 190 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 191 | .selector = UVC_CT_AE_MODE_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 192 | .index = 1, |
| 193 | .size = 1, |
| 194 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR |
| 195 | | UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES |
| 196 | | UVC_CONTROL_RESTORE, |
| 197 | }, |
| 198 | { |
| 199 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 200 | .selector = UVC_CT_AE_PRIORITY_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 201 | .index = 2, |
| 202 | .size = 1, |
| 203 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR |
| 204 | | UVC_CONTROL_RESTORE, |
| 205 | }, |
| 206 | { |
| 207 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 208 | .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 209 | .index = 3, |
| 210 | .size = 4, |
| 211 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 212 | | UVC_CONTROL_RESTORE, |
| 213 | }, |
| 214 | { |
| 215 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 216 | .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 217 | .index = 4, |
| 218 | .size = 1, |
| 219 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR |
| 220 | | UVC_CONTROL_RESTORE, |
| 221 | }, |
| 222 | { |
| 223 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 224 | .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 225 | .index = 5, |
| 226 | .size = 2, |
| 227 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 228 | | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE, |
| 229 | }, |
| 230 | { |
| 231 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 232 | .selector = UVC_CT_FOCUS_RELATIVE_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 233 | .index = 6, |
| 234 | .size = 2, |
| 235 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 236 | | UVC_CONTROL_AUTO_UPDATE, |
| 237 | }, |
| 238 | { |
| 239 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 240 | .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 241 | .index = 7, |
| 242 | .size = 2, |
| 243 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 244 | | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE, |
| 245 | }, |
| 246 | { |
| 247 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 248 | .selector = UVC_CT_IRIS_RELATIVE_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 249 | .index = 8, |
| 250 | .size = 1, |
| 251 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR |
| 252 | | UVC_CONTROL_AUTO_UPDATE, |
| 253 | }, |
| 254 | { |
| 255 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 256 | .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 257 | .index = 9, |
| 258 | .size = 2, |
| 259 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 260 | | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE, |
| 261 | }, |
| 262 | { |
| 263 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 264 | .selector = UVC_CT_ZOOM_RELATIVE_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 265 | .index = 10, |
| 266 | .size = 3, |
| 267 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 268 | | UVC_CONTROL_AUTO_UPDATE, |
| 269 | }, |
| 270 | { |
| 271 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 272 | .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 273 | .index = 11, |
| 274 | .size = 8, |
| 275 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 276 | | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE, |
| 277 | }, |
| 278 | { |
| 279 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 280 | .selector = UVC_CT_PANTILT_RELATIVE_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 281 | .index = 12, |
| 282 | .size = 4, |
| 283 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 284 | | UVC_CONTROL_AUTO_UPDATE, |
| 285 | }, |
| 286 | { |
| 287 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 288 | .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 289 | .index = 13, |
| 290 | .size = 2, |
| 291 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 292 | | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE, |
| 293 | }, |
| 294 | { |
| 295 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 296 | .selector = UVC_CT_ROLL_RELATIVE_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 297 | .index = 14, |
| 298 | .size = 2, |
| 299 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE |
| 300 | | UVC_CONTROL_AUTO_UPDATE, |
| 301 | }, |
| 302 | { |
| 303 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 304 | .selector = UVC_CT_FOCUS_AUTO_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 305 | .index = 17, |
| 306 | .size = 1, |
| 307 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR |
| 308 | | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE, |
| 309 | }, |
| 310 | { |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 311 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 312 | .selector = UVC_CT_PRIVACY_CONTROL, |
Laurent Pinchart | 5e26d50 | 2008-09-22 13:14:59 -0300 | [diff] [blame] | 313 | .index = 18, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 314 | .size = 1, |
| 315 | .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 316 | | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE, |
| 317 | }, |
| 318 | }; |
| 319 | |
| 320 | static struct uvc_menu_info power_line_frequency_controls[] = { |
| 321 | { 0, "Disabled" }, |
| 322 | { 1, "50 Hz" }, |
| 323 | { 2, "60 Hz" }, |
| 324 | }; |
| 325 | |
| 326 | static struct uvc_menu_info exposure_auto_controls[] = { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 327 | { 2, "Auto Mode" }, |
Laurent Pinchart | 90ac5ea | 2008-07-26 11:42:29 -0300 | [diff] [blame] | 328 | { 1, "Manual Mode" }, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 329 | { 4, "Shutter Priority Mode" }, |
| 330 | { 8, "Aperture Priority Mode" }, |
| 331 | }; |
| 332 | |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 333 | static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping, |
| 334 | __u8 query, const __u8 *data) |
| 335 | { |
| 336 | __s8 zoom = (__s8)data[0]; |
| 337 | |
| 338 | switch (query) { |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 339 | case UVC_GET_CUR: |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 340 | return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]); |
| 341 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 342 | case UVC_GET_MIN: |
| 343 | case UVC_GET_MAX: |
| 344 | case UVC_GET_RES: |
| 345 | case UVC_GET_DEF: |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 346 | default: |
| 347 | return data[2]; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping, |
| 352 | __s32 value, __u8 *data) |
| 353 | { |
| 354 | data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff; |
Laurent Pinchart | 2e89613 | 2009-11-04 13:11:10 -0300 | [diff] [blame] | 355 | data[2] = min((int)abs(value), 0xff); |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 356 | } |
| 357 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 358 | static struct uvc_control_mapping uvc_ctrl_mappings[] = { |
| 359 | { |
| 360 | .id = V4L2_CID_BRIGHTNESS, |
| 361 | .name = "Brightness", |
| 362 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 363 | .selector = UVC_PU_BRIGHTNESS_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 364 | .size = 16, |
| 365 | .offset = 0, |
| 366 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 367 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, |
| 368 | }, |
| 369 | { |
| 370 | .id = V4L2_CID_CONTRAST, |
| 371 | .name = "Contrast", |
| 372 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 373 | .selector = UVC_PU_CONTRAST_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 374 | .size = 16, |
| 375 | .offset = 0, |
| 376 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 377 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, |
| 378 | }, |
| 379 | { |
| 380 | .id = V4L2_CID_HUE, |
| 381 | .name = "Hue", |
| 382 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 383 | .selector = UVC_PU_HUE_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 384 | .size = 16, |
| 385 | .offset = 0, |
| 386 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 387 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, |
| 388 | }, |
| 389 | { |
| 390 | .id = V4L2_CID_SATURATION, |
| 391 | .name = "Saturation", |
| 392 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 393 | .selector = UVC_PU_SATURATION_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 394 | .size = 16, |
| 395 | .offset = 0, |
| 396 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 397 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, |
| 398 | }, |
| 399 | { |
| 400 | .id = V4L2_CID_SHARPNESS, |
| 401 | .name = "Sharpness", |
| 402 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 403 | .selector = UVC_PU_SHARPNESS_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 404 | .size = 16, |
| 405 | .offset = 0, |
| 406 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 407 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, |
| 408 | }, |
| 409 | { |
| 410 | .id = V4L2_CID_GAMMA, |
| 411 | .name = "Gamma", |
| 412 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 413 | .selector = UVC_PU_GAMMA_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 414 | .size = 16, |
| 415 | .offset = 0, |
| 416 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 417 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, |
| 418 | }, |
| 419 | { |
| 420 | .id = V4L2_CID_BACKLIGHT_COMPENSATION, |
| 421 | .name = "Backlight Compensation", |
| 422 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 423 | .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 424 | .size = 16, |
| 425 | .offset = 0, |
| 426 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 427 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, |
| 428 | }, |
| 429 | { |
| 430 | .id = V4L2_CID_GAIN, |
| 431 | .name = "Gain", |
| 432 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 433 | .selector = UVC_PU_GAIN_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 434 | .size = 16, |
| 435 | .offset = 0, |
| 436 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 437 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, |
| 438 | }, |
| 439 | { |
| 440 | .id = V4L2_CID_POWER_LINE_FREQUENCY, |
| 441 | .name = "Power Line Frequency", |
| 442 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 443 | .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 444 | .size = 2, |
| 445 | .offset = 0, |
| 446 | .v4l2_type = V4L2_CTRL_TYPE_MENU, |
| 447 | .data_type = UVC_CTRL_DATA_TYPE_ENUM, |
| 448 | .menu_info = power_line_frequency_controls, |
| 449 | .menu_count = ARRAY_SIZE(power_line_frequency_controls), |
| 450 | }, |
| 451 | { |
| 452 | .id = V4L2_CID_HUE_AUTO, |
| 453 | .name = "Hue, Auto", |
| 454 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 455 | .selector = UVC_PU_HUE_AUTO_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 456 | .size = 1, |
| 457 | .offset = 0, |
| 458 | .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, |
| 459 | .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, |
| 460 | }, |
| 461 | { |
| 462 | .id = V4L2_CID_EXPOSURE_AUTO, |
| 463 | .name = "Exposure, Auto", |
| 464 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 465 | .selector = UVC_CT_AE_MODE_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 466 | .size = 4, |
| 467 | .offset = 0, |
| 468 | .v4l2_type = V4L2_CTRL_TYPE_MENU, |
| 469 | .data_type = UVC_CTRL_DATA_TYPE_BITMASK, |
| 470 | .menu_info = exposure_auto_controls, |
| 471 | .menu_count = ARRAY_SIZE(exposure_auto_controls), |
| 472 | }, |
| 473 | { |
| 474 | .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY, |
| 475 | .name = "Exposure, Auto Priority", |
| 476 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 477 | .selector = UVC_CT_AE_PRIORITY_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 478 | .size = 1, |
| 479 | .offset = 0, |
| 480 | .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, |
| 481 | .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, |
| 482 | }, |
| 483 | { |
| 484 | .id = V4L2_CID_EXPOSURE_ABSOLUTE, |
| 485 | .name = "Exposure (Absolute)", |
| 486 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 487 | .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 488 | .size = 32, |
| 489 | .offset = 0, |
| 490 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 491 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, |
| 492 | }, |
| 493 | { |
| 494 | .id = V4L2_CID_AUTO_WHITE_BALANCE, |
| 495 | .name = "White Balance Temperature, Auto", |
| 496 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 497 | .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 498 | .size = 1, |
| 499 | .offset = 0, |
| 500 | .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, |
| 501 | .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, |
| 502 | }, |
| 503 | { |
| 504 | .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE, |
| 505 | .name = "White Balance Temperature", |
| 506 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 507 | .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 508 | .size = 16, |
| 509 | .offset = 0, |
| 510 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 511 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, |
| 512 | }, |
| 513 | { |
| 514 | .id = V4L2_CID_AUTO_WHITE_BALANCE, |
| 515 | .name = "White Balance Component, Auto", |
| 516 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 517 | .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 518 | .size = 1, |
| 519 | .offset = 0, |
| 520 | .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, |
| 521 | .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, |
| 522 | }, |
| 523 | { |
| 524 | .id = V4L2_CID_BLUE_BALANCE, |
| 525 | .name = "White Balance Blue Component", |
| 526 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 527 | .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 528 | .size = 16, |
| 529 | .offset = 0, |
| 530 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 531 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, |
| 532 | }, |
| 533 | { |
| 534 | .id = V4L2_CID_RED_BALANCE, |
| 535 | .name = "White Balance Red Component", |
| 536 | .entity = UVC_GUID_UVC_PROCESSING, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 537 | .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 538 | .size = 16, |
| 539 | .offset = 16, |
| 540 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 541 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, |
| 542 | }, |
| 543 | { |
| 544 | .id = V4L2_CID_FOCUS_ABSOLUTE, |
| 545 | .name = "Focus (absolute)", |
| 546 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 547 | .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 548 | .size = 16, |
| 549 | .offset = 0, |
| 550 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 551 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, |
| 552 | }, |
| 553 | { |
| 554 | .id = V4L2_CID_FOCUS_AUTO, |
| 555 | .name = "Focus, Auto", |
| 556 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 557 | .selector = UVC_CT_FOCUS_AUTO_CONTROL, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 558 | .size = 1, |
| 559 | .offset = 0, |
| 560 | .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, |
| 561 | .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, |
| 562 | }, |
Laurent Pinchart | 6df126f | 2008-12-16 06:44:11 -0300 | [diff] [blame] | 563 | { |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 564 | .id = V4L2_CID_ZOOM_ABSOLUTE, |
| 565 | .name = "Zoom, Absolute", |
| 566 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 567 | .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL, |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 568 | .size = 16, |
| 569 | .offset = 0, |
| 570 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 571 | .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, |
| 572 | }, |
| 573 | { |
| 574 | .id = V4L2_CID_ZOOM_CONTINUOUS, |
| 575 | .name = "Zoom, Continuous", |
| 576 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 577 | .selector = UVC_CT_ZOOM_RELATIVE_CONTROL, |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 578 | .size = 0, |
| 579 | .offset = 0, |
| 580 | .v4l2_type = V4L2_CTRL_TYPE_INTEGER, |
| 581 | .data_type = UVC_CTRL_DATA_TYPE_SIGNED, |
| 582 | .get = uvc_ctrl_get_zoom, |
| 583 | .set = uvc_ctrl_set_zoom, |
| 584 | }, |
| 585 | { |
Laurent Pinchart | 6df126f | 2008-12-16 06:44:11 -0300 | [diff] [blame] | 586 | .id = V4L2_CID_PRIVACY, |
| 587 | .name = "Privacy", |
| 588 | .entity = UVC_GUID_UVC_CAMERA, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 589 | .selector = UVC_CT_PRIVACY_CONTROL, |
Laurent Pinchart | 6df126f | 2008-12-16 06:44:11 -0300 | [diff] [blame] | 590 | .size = 1, |
| 591 | .offset = 0, |
| 592 | .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, |
| 593 | .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, |
| 594 | }, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 595 | }; |
| 596 | |
| 597 | /* ------------------------------------------------------------------------ |
| 598 | * Utility functions |
| 599 | */ |
| 600 | |
| 601 | static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id) |
| 602 | { |
| 603 | return ctrl->data + id * ctrl->info->size; |
| 604 | } |
| 605 | |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 606 | static inline int uvc_test_bit(const __u8 *data, int bit) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 607 | { |
| 608 | return (data[bit >> 3] >> (bit & 7)) & 1; |
| 609 | } |
| 610 | |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 611 | static inline void uvc_clear_bit(__u8 *data, int bit) |
| 612 | { |
| 613 | data[bit >> 3] &= ~(1 << (bit & 7)); |
| 614 | } |
| 615 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 616 | /* Extract the bit string specified by mapping->offset and mapping->size |
| 617 | * from the little-endian data stored at 'data' and return the result as |
| 618 | * a signed 32bit integer. Sign extension will be performed if the mapping |
| 619 | * references a signed data type. |
| 620 | */ |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 621 | static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping, |
| 622 | __u8 query, const __u8 *data) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 623 | { |
| 624 | int bits = mapping->size; |
| 625 | int offset = mapping->offset; |
| 626 | __s32 value = 0; |
| 627 | __u8 mask; |
| 628 | |
| 629 | data += offset / 8; |
| 630 | offset &= 7; |
| 631 | mask = ((1LL << bits) - 1) << offset; |
| 632 | |
| 633 | for (; bits > 0; data++) { |
| 634 | __u8 byte = *data & mask; |
| 635 | value |= offset > 0 ? (byte >> offset) : (byte << (-offset)); |
| 636 | bits -= 8 - (offset > 0 ? offset : 0); |
| 637 | offset -= 8; |
| 638 | mask = (1 << bits) - 1; |
| 639 | } |
| 640 | |
Laurent Pinchart | 2c2d264 | 2009-01-03 19:12:40 -0300 | [diff] [blame] | 641 | /* Sign-extend the value if needed. */ |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 642 | if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED) |
| 643 | value |= -(value & (1 << (mapping->size - 1))); |
| 644 | |
| 645 | return value; |
| 646 | } |
| 647 | |
| 648 | /* Set the bit string specified by mapping->offset and mapping->size |
| 649 | * in the little-endian data stored at 'data' to the value 'value'. |
| 650 | */ |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 651 | static void uvc_set_le_value(struct uvc_control_mapping *mapping, |
| 652 | __s32 value, __u8 *data) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 653 | { |
| 654 | int bits = mapping->size; |
| 655 | int offset = mapping->offset; |
| 656 | __u8 mask; |
| 657 | |
| 658 | data += offset / 8; |
| 659 | offset &= 7; |
| 660 | |
| 661 | for (; bits > 0; data++) { |
| 662 | mask = ((1LL << bits) - 1) << offset; |
| 663 | *data = (*data & ~mask) | ((value << offset) & mask); |
| 664 | value >>= offset ? offset : 8; |
| 665 | bits -= 8 - offset; |
| 666 | offset = 0; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | /* ------------------------------------------------------------------------ |
| 671 | * Terminal and unit management |
| 672 | */ |
| 673 | |
| 674 | static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING; |
| 675 | static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA; |
| 676 | static const __u8 uvc_media_transport_input_guid[16] = |
| 677 | UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT; |
| 678 | |
| 679 | static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16]) |
| 680 | { |
| 681 | switch (UVC_ENTITY_TYPE(entity)) { |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 682 | case UVC_ITT_CAMERA: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 683 | return memcmp(uvc_camera_guid, guid, 16) == 0; |
| 684 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 685 | case UVC_ITT_MEDIA_TRANSPORT_INPUT: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 686 | return memcmp(uvc_media_transport_input_guid, guid, 16) == 0; |
| 687 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 688 | case UVC_VC_PROCESSING_UNIT: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 689 | return memcmp(uvc_processing_guid, guid, 16) == 0; |
| 690 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 691 | case UVC_VC_EXTENSION_UNIT: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 692 | return memcmp(entity->extension.guidExtensionCode, |
| 693 | guid, 16) == 0; |
| 694 | |
| 695 | default: |
| 696 | return 0; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | /* ------------------------------------------------------------------------ |
| 701 | * UVC Controls |
| 702 | */ |
| 703 | |
| 704 | static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id, |
| 705 | struct uvc_control_mapping **mapping, struct uvc_control **control, |
| 706 | int next) |
| 707 | { |
| 708 | struct uvc_control *ctrl; |
| 709 | struct uvc_control_mapping *map; |
| 710 | unsigned int i; |
| 711 | |
| 712 | if (entity == NULL) |
| 713 | return; |
| 714 | |
| 715 | for (i = 0; i < entity->ncontrols; ++i) { |
| 716 | ctrl = &entity->controls[i]; |
| 717 | if (ctrl->info == NULL) |
| 718 | continue; |
| 719 | |
| 720 | list_for_each_entry(map, &ctrl->info->mappings, list) { |
| 721 | if ((map->id == v4l2_id) && !next) { |
| 722 | *control = ctrl; |
| 723 | *mapping = map; |
| 724 | return; |
| 725 | } |
| 726 | |
| 727 | if ((*mapping == NULL || (*mapping)->id > map->id) && |
| 728 | (map->id > v4l2_id) && next) { |
| 729 | *control = ctrl; |
| 730 | *mapping = map; |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 736 | struct uvc_control *uvc_find_control(struct uvc_video_chain *chain, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 737 | __u32 v4l2_id, struct uvc_control_mapping **mapping) |
| 738 | { |
| 739 | struct uvc_control *ctrl = NULL; |
| 740 | struct uvc_entity *entity; |
| 741 | int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL; |
| 742 | |
| 743 | *mapping = NULL; |
| 744 | |
| 745 | /* Mask the query flags. */ |
| 746 | v4l2_id &= V4L2_CTRL_ID_MASK; |
| 747 | |
| 748 | /* Find the control. */ |
Laurent Pinchart | 6241d8c | 2009-11-25 12:00:22 -0300 | [diff] [blame] | 749 | list_for_each_entry(entity, &chain->entities, chain) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 750 | __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next); |
| 751 | if (ctrl && !next) |
| 752 | return ctrl; |
| 753 | } |
| 754 | |
| 755 | if (ctrl == NULL && !next) |
| 756 | uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n", |
| 757 | v4l2_id); |
| 758 | |
| 759 | return ctrl; |
| 760 | } |
| 761 | |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 762 | static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain, |
| 763 | struct uvc_control *ctrl) |
| 764 | { |
| 765 | int ret; |
| 766 | |
| 767 | if (ctrl->info->flags & UVC_CONTROL_GET_DEF) { |
| 768 | ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id, |
| 769 | chain->dev->intfnum, ctrl->info->selector, |
| 770 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF), |
| 771 | ctrl->info->size); |
| 772 | if (ret < 0) |
| 773 | return ret; |
| 774 | } |
| 775 | |
| 776 | if (ctrl->info->flags & UVC_CONTROL_GET_MIN) { |
| 777 | ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id, |
| 778 | chain->dev->intfnum, ctrl->info->selector, |
| 779 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN), |
| 780 | ctrl->info->size); |
| 781 | if (ret < 0) |
| 782 | return ret; |
| 783 | } |
| 784 | if (ctrl->info->flags & UVC_CONTROL_GET_MAX) { |
| 785 | ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id, |
| 786 | chain->dev->intfnum, ctrl->info->selector, |
| 787 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX), |
| 788 | ctrl->info->size); |
| 789 | if (ret < 0) |
| 790 | return ret; |
| 791 | } |
| 792 | if (ctrl->info->flags & UVC_CONTROL_GET_RES) { |
| 793 | ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id, |
| 794 | chain->dev->intfnum, ctrl->info->selector, |
| 795 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), |
| 796 | ctrl->info->size); |
| 797 | if (ret < 0) |
| 798 | return ret; |
| 799 | } |
| 800 | |
| 801 | ctrl->cached = 1; |
| 802 | return 0; |
| 803 | } |
| 804 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 805 | int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 806 | struct v4l2_queryctrl *v4l2_ctrl) |
| 807 | { |
| 808 | struct uvc_control *ctrl; |
| 809 | struct uvc_control_mapping *mapping; |
| 810 | struct uvc_menu_info *menu; |
| 811 | unsigned int i; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 812 | int ret; |
| 813 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 814 | ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 815 | if (ctrl == NULL) |
| 816 | return -EINVAL; |
| 817 | |
Laurent Pinchart | 54812c7 | 2008-07-17 07:37:37 -0300 | [diff] [blame] | 818 | memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 819 | v4l2_ctrl->id = mapping->id; |
| 820 | v4l2_ctrl->type = mapping->v4l2_type; |
Laurent Pinchart | d0ebf30 | 2009-01-08 14:05:11 -0300 | [diff] [blame] | 821 | strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 822 | v4l2_ctrl->flags = 0; |
| 823 | |
| 824 | if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR)) |
| 825 | v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; |
| 826 | |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 827 | if (!ctrl->cached) { |
| 828 | ret = uvc_ctrl_populate_cache(chain, ctrl); |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 829 | if (ret < 0) |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 830 | return ret; |
| 831 | } |
| 832 | |
| 833 | if (ctrl->info->flags & UVC_CONTROL_GET_DEF) { |
| 834 | v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF, |
| 835 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF)); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 836 | } |
| 837 | |
Laurent Pinchart | 54812c7 | 2008-07-17 07:37:37 -0300 | [diff] [blame] | 838 | switch (mapping->v4l2_type) { |
| 839 | case V4L2_CTRL_TYPE_MENU: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 840 | v4l2_ctrl->minimum = 0; |
| 841 | v4l2_ctrl->maximum = mapping->menu_count - 1; |
| 842 | v4l2_ctrl->step = 1; |
| 843 | |
| 844 | menu = mapping->menu_info; |
| 845 | for (i = 0; i < mapping->menu_count; ++i, ++menu) { |
| 846 | if (menu->value == v4l2_ctrl->default_value) { |
| 847 | v4l2_ctrl->default_value = i; |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 852 | return 0; |
Laurent Pinchart | 54812c7 | 2008-07-17 07:37:37 -0300 | [diff] [blame] | 853 | |
| 854 | case V4L2_CTRL_TYPE_BOOLEAN: |
| 855 | v4l2_ctrl->minimum = 0; |
| 856 | v4l2_ctrl->maximum = 1; |
| 857 | v4l2_ctrl->step = 1; |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 858 | return 0; |
Laurent Pinchart | 54812c7 | 2008-07-17 07:37:37 -0300 | [diff] [blame] | 859 | |
Laurent Pinchart | f4eabaf | 2009-08-06 06:05:40 -0300 | [diff] [blame] | 860 | case V4L2_CTRL_TYPE_BUTTON: |
| 861 | v4l2_ctrl->minimum = 0; |
| 862 | v4l2_ctrl->maximum = 0; |
| 863 | v4l2_ctrl->step = 0; |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 864 | return 0; |
Laurent Pinchart | f4eabaf | 2009-08-06 06:05:40 -0300 | [diff] [blame] | 865 | |
Laurent Pinchart | 54812c7 | 2008-07-17 07:37:37 -0300 | [diff] [blame] | 866 | default: |
| 867 | break; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 868 | } |
| 869 | |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 870 | if (ctrl->info->flags & UVC_CONTROL_GET_MIN) |
| 871 | v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN, |
| 872 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN)); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 873 | |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 874 | if (ctrl->info->flags & UVC_CONTROL_GET_MAX) |
| 875 | v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX, |
| 876 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX)); |
| 877 | |
| 878 | if (ctrl->info->flags & UVC_CONTROL_GET_RES) |
| 879 | v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES, |
| 880 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); |
| 881 | |
| 882 | return 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | |
| 886 | /* -------------------------------------------------------------------------- |
| 887 | * Control transactions |
| 888 | * |
| 889 | * To make extended set operations as atomic as the hardware allows, controls |
| 890 | * are handled using begin/commit/rollback operations. |
| 891 | * |
| 892 | * At the beginning of a set request, uvc_ctrl_begin should be called to |
| 893 | * initialize the request. This function acquires the control lock. |
| 894 | * |
| 895 | * When setting a control, the new value is stored in the control data field |
| 896 | * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for |
| 897 | * later processing. If the UVC and V4L2 control sizes differ, the current |
| 898 | * value is loaded from the hardware before storing the new value in the data |
| 899 | * field. |
| 900 | * |
| 901 | * After processing all controls in the transaction, uvc_ctrl_commit or |
| 902 | * uvc_ctrl_rollback must be called to apply the pending changes to the |
| 903 | * hardware or revert them. When applying changes, all controls marked as |
| 904 | * dirty will be modified in the UVC device, and the dirty flag will be |
| 905 | * cleared. When reverting controls, the control data field |
| 906 | * UVC_CTRL_DATA_CURRENT is reverted to its previous value |
| 907 | * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the |
| 908 | * control lock. |
| 909 | */ |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 910 | int uvc_ctrl_begin(struct uvc_video_chain *chain) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 911 | { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 912 | return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | static int uvc_ctrl_commit_entity(struct uvc_device *dev, |
| 916 | struct uvc_entity *entity, int rollback) |
| 917 | { |
| 918 | struct uvc_control *ctrl; |
| 919 | unsigned int i; |
| 920 | int ret; |
| 921 | |
| 922 | if (entity == NULL) |
| 923 | return 0; |
| 924 | |
| 925 | for (i = 0; i < entity->ncontrols; ++i) { |
| 926 | ctrl = &entity->controls[i]; |
Laurent Pinchart | b1accfa | 2008-09-27 20:54:02 -0300 | [diff] [blame] | 927 | if (ctrl->info == NULL) |
| 928 | continue; |
| 929 | |
| 930 | /* Reset the loaded flag for auto-update controls that were |
| 931 | * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent |
| 932 | * uvc_ctrl_get from using the cached value. |
| 933 | */ |
| 934 | if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE) |
| 935 | ctrl->loaded = 0; |
| 936 | |
| 937 | if (!ctrl->dirty) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 938 | continue; |
| 939 | |
| 940 | if (!rollback) |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 941 | ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 942 | dev->intfnum, ctrl->info->selector, |
| 943 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
| 944 | ctrl->info->size); |
| 945 | else |
| 946 | ret = 0; |
| 947 | |
| 948 | if (rollback || ret < 0) |
| 949 | memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
| 950 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP), |
| 951 | ctrl->info->size); |
| 952 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 953 | ctrl->dirty = 0; |
| 954 | |
| 955 | if (ret < 0) |
| 956 | return ret; |
| 957 | } |
| 958 | |
| 959 | return 0; |
| 960 | } |
| 961 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 962 | int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 963 | { |
| 964 | struct uvc_entity *entity; |
| 965 | int ret = 0; |
| 966 | |
| 967 | /* Find the control. */ |
Laurent Pinchart | 6241d8c | 2009-11-25 12:00:22 -0300 | [diff] [blame] | 968 | list_for_each_entry(entity, &chain->entities, chain) { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 969 | ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 970 | if (ret < 0) |
| 971 | goto done; |
| 972 | } |
| 973 | |
| 974 | done: |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 975 | mutex_unlock(&chain->ctrl_mutex); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 976 | return ret; |
| 977 | } |
| 978 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 979 | int uvc_ctrl_get(struct uvc_video_chain *chain, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 980 | struct v4l2_ext_control *xctrl) |
| 981 | { |
| 982 | struct uvc_control *ctrl; |
| 983 | struct uvc_control_mapping *mapping; |
| 984 | struct uvc_menu_info *menu; |
| 985 | unsigned int i; |
| 986 | int ret; |
| 987 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 988 | ctrl = uvc_find_control(chain, xctrl->id, &mapping); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 989 | if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) |
| 990 | return -EINVAL; |
| 991 | |
| 992 | if (!ctrl->loaded) { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 993 | ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id, |
| 994 | chain->dev->intfnum, ctrl->info->selector, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 995 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
| 996 | ctrl->info->size); |
| 997 | if (ret < 0) |
| 998 | return ret; |
| 999 | |
Laurent Pinchart | b1accfa | 2008-09-27 20:54:02 -0300 | [diff] [blame] | 1000 | ctrl->loaded = 1; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1001 | } |
| 1002 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1003 | xctrl->value = mapping->get(mapping, UVC_GET_CUR, |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 1004 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1005 | |
| 1006 | if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) { |
| 1007 | menu = mapping->menu_info; |
| 1008 | for (i = 0; i < mapping->menu_count; ++i, ++menu) { |
| 1009 | if (menu->value == xctrl->value) { |
| 1010 | xctrl->value = i; |
| 1011 | break; |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1019 | int uvc_ctrl_set(struct uvc_video_chain *chain, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1020 | struct v4l2_ext_control *xctrl) |
| 1021 | { |
| 1022 | struct uvc_control *ctrl; |
| 1023 | struct uvc_control_mapping *mapping; |
Laurent Pinchart | e54532e | 2010-01-23 07:07:53 -0300 | [diff] [blame^] | 1024 | s32 value; |
| 1025 | u32 step; |
| 1026 | s32 min; |
| 1027 | s32 max; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1028 | int ret; |
| 1029 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1030 | ctrl = uvc_find_control(chain, xctrl->id, &mapping); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1031 | if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0) |
| 1032 | return -EINVAL; |
| 1033 | |
Laurent Pinchart | e54532e | 2010-01-23 07:07:53 -0300 | [diff] [blame^] | 1034 | /* Clamp out of range values. */ |
| 1035 | switch (mapping->v4l2_type) { |
| 1036 | case V4L2_CTRL_TYPE_INTEGER: |
| 1037 | if (!ctrl->cached) { |
| 1038 | ret = uvc_ctrl_populate_cache(chain, ctrl); |
| 1039 | if (ret < 0) |
| 1040 | return ret; |
| 1041 | } |
| 1042 | |
| 1043 | min = mapping->get(mapping, UVC_GET_MIN, |
| 1044 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN)); |
| 1045 | max = mapping->get(mapping, UVC_GET_MAX, |
| 1046 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX)); |
| 1047 | step = mapping->get(mapping, UVC_GET_RES, |
| 1048 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); |
| 1049 | |
| 1050 | xctrl->value = min + (xctrl->value - min + step/2) / step * step; |
| 1051 | xctrl->value = clamp(xctrl->value, min, max); |
| 1052 | value = xctrl->value; |
| 1053 | break; |
| 1054 | |
| 1055 | case V4L2_CTRL_TYPE_BOOLEAN: |
| 1056 | xctrl->value = clamp(xctrl->value, 0, 1); |
| 1057 | value = xctrl->value; |
| 1058 | break; |
| 1059 | |
| 1060 | case V4L2_CTRL_TYPE_MENU: |
| 1061 | if (xctrl->value < 0 || xctrl->value >= mapping->menu_count) |
Laurent Pinchart | 8a4e76c | 2010-01-21 16:53:11 -0300 | [diff] [blame] | 1062 | return -ERANGE; |
Laurent Pinchart | e54532e | 2010-01-23 07:07:53 -0300 | [diff] [blame^] | 1063 | value = mapping->menu_info[xctrl->value].value; |
| 1064 | break; |
| 1065 | |
| 1066 | default: |
| 1067 | value = xctrl->value; |
| 1068 | break; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1069 | } |
| 1070 | |
Laurent Pinchart | e54532e | 2010-01-23 07:07:53 -0300 | [diff] [blame^] | 1071 | /* If the mapping doesn't span the whole UVC control, the current value |
| 1072 | * needs to be loaded from the device to perform the read-modify-write |
| 1073 | * operation. |
| 1074 | */ |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1075 | if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) { |
| 1076 | if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) { |
| 1077 | memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
| 1078 | 0, ctrl->info->size); |
| 1079 | } else { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1080 | ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, |
| 1081 | ctrl->entity->id, chain->dev->intfnum, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1082 | ctrl->info->selector, |
| 1083 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
| 1084 | ctrl->info->size); |
| 1085 | if (ret < 0) |
| 1086 | return ret; |
| 1087 | } |
| 1088 | |
Laurent Pinchart | b1accfa | 2008-09-27 20:54:02 -0300 | [diff] [blame] | 1089 | ctrl->loaded = 1; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1090 | } |
| 1091 | |
Laurent Pinchart | e54532e | 2010-01-23 07:07:53 -0300 | [diff] [blame^] | 1092 | /* Backup the current value in case we need to rollback later. */ |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1093 | if (!ctrl->dirty) { |
| 1094 | memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP), |
| 1095 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
| 1096 | ctrl->info->size); |
| 1097 | } |
| 1098 | |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 1099 | mapping->set(mapping, value, |
| 1100 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1101 | |
| 1102 | ctrl->dirty = 1; |
| 1103 | ctrl->modified = 1; |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | /* -------------------------------------------------------------------------- |
| 1108 | * Dynamic controls |
| 1109 | */ |
| 1110 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1111 | int uvc_xu_ctrl_query(struct uvc_video_chain *chain, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1112 | struct uvc_xu_control *xctrl, int set) |
| 1113 | { |
| 1114 | struct uvc_entity *entity; |
| 1115 | struct uvc_control *ctrl = NULL; |
| 1116 | unsigned int i, found = 0; |
| 1117 | __u8 *data; |
| 1118 | int ret; |
| 1119 | |
| 1120 | /* Find the extension unit. */ |
Laurent Pinchart | 6241d8c | 2009-11-25 12:00:22 -0300 | [diff] [blame] | 1121 | list_for_each_entry(entity, &chain->entities, chain) { |
| 1122 | if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT && |
| 1123 | entity->id == xctrl->unit) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1124 | break; |
| 1125 | } |
| 1126 | |
| 1127 | if (entity->id != xctrl->unit) { |
| 1128 | uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n", |
| 1129 | xctrl->unit); |
| 1130 | return -EINVAL; |
| 1131 | } |
| 1132 | |
| 1133 | /* Find the control. */ |
| 1134 | for (i = 0; i < entity->ncontrols; ++i) { |
| 1135 | ctrl = &entity->controls[i]; |
| 1136 | if (ctrl->info == NULL) |
| 1137 | continue; |
| 1138 | |
| 1139 | if (ctrl->info->selector == xctrl->selector) { |
| 1140 | found = 1; |
| 1141 | break; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | if (!found) { |
Laurent Pinchart | 36bd883 | 2010-01-19 09:06:22 -0300 | [diff] [blame] | 1146 | uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n", |
| 1147 | entity->extension.guidExtensionCode, xctrl->selector); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1148 | return -EINVAL; |
| 1149 | } |
| 1150 | |
| 1151 | /* Validate control data size. */ |
| 1152 | if (ctrl->info->size != xctrl->size) |
| 1153 | return -EINVAL; |
| 1154 | |
| 1155 | if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) || |
| 1156 | (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR))) |
| 1157 | return -EINVAL; |
| 1158 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1159 | if (mutex_lock_interruptible(&chain->ctrl_mutex)) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1160 | return -ERESTARTSYS; |
| 1161 | |
| 1162 | memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP), |
| 1163 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
| 1164 | xctrl->size); |
| 1165 | data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT); |
| 1166 | |
| 1167 | if (set && copy_from_user(data, xctrl->data, xctrl->size)) { |
| 1168 | ret = -EFAULT; |
| 1169 | goto out; |
| 1170 | } |
| 1171 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1172 | ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR, |
| 1173 | xctrl->unit, chain->dev->intfnum, xctrl->selector, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1174 | data, xctrl->size); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1175 | if (ret < 0) |
| 1176 | goto out; |
| 1177 | |
| 1178 | if (!set && copy_to_user(xctrl->data, data, xctrl->size)) { |
| 1179 | ret = -EFAULT; |
| 1180 | goto out; |
| 1181 | } |
| 1182 | |
| 1183 | out: |
| 1184 | if (ret) |
| 1185 | memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
| 1186 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP), |
| 1187 | xctrl->size); |
| 1188 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1189 | mutex_unlock(&chain->ctrl_mutex); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1190 | return ret; |
| 1191 | } |
| 1192 | |
| 1193 | /* -------------------------------------------------------------------------- |
| 1194 | * Suspend/resume |
| 1195 | */ |
| 1196 | |
| 1197 | /* |
| 1198 | * Restore control values after resume, skipping controls that haven't been |
| 1199 | * changed. |
| 1200 | * |
| 1201 | * TODO |
| 1202 | * - Don't restore modified controls that are back to their default value. |
| 1203 | * - Handle restore order (Auto-Exposure Mode should be restored before |
| 1204 | * Exposure Time). |
| 1205 | */ |
| 1206 | int uvc_ctrl_resume_device(struct uvc_device *dev) |
| 1207 | { |
| 1208 | struct uvc_control *ctrl; |
| 1209 | struct uvc_entity *entity; |
| 1210 | unsigned int i; |
| 1211 | int ret; |
| 1212 | |
| 1213 | /* Walk the entities list and restore controls when possible. */ |
| 1214 | list_for_each_entry(entity, &dev->entities, list) { |
| 1215 | |
| 1216 | for (i = 0; i < entity->ncontrols; ++i) { |
| 1217 | ctrl = &entity->controls[i]; |
| 1218 | |
| 1219 | if (ctrl->info == NULL || !ctrl->modified || |
| 1220 | (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0) |
| 1221 | continue; |
| 1222 | |
Laurent Pinchart | 36bd883 | 2010-01-19 09:06:22 -0300 | [diff] [blame] | 1223 | printk(KERN_INFO "restoring control %pUl/%u/%u\n", |
| 1224 | ctrl->info->entity, ctrl->info->index, |
| 1225 | ctrl->info->selector); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1226 | ctrl->dirty = 1; |
| 1227 | } |
| 1228 | |
| 1229 | ret = uvc_ctrl_commit_entity(dev, entity, 0); |
| 1230 | if (ret < 0) |
| 1231 | return ret; |
| 1232 | } |
| 1233 | |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | /* -------------------------------------------------------------------------- |
| 1238 | * Control and mapping handling |
| 1239 | */ |
| 1240 | |
| 1241 | static void uvc_ctrl_add_ctrl(struct uvc_device *dev, |
| 1242 | struct uvc_control_info *info) |
| 1243 | { |
| 1244 | struct uvc_entity *entity; |
| 1245 | struct uvc_control *ctrl = NULL; |
| 1246 | int ret, found = 0; |
| 1247 | unsigned int i; |
| 1248 | |
| 1249 | list_for_each_entry(entity, &dev->entities, list) { |
| 1250 | if (!uvc_entity_match_guid(entity, info->entity)) |
| 1251 | continue; |
| 1252 | |
| 1253 | for (i = 0; i < entity->ncontrols; ++i) { |
| 1254 | ctrl = &entity->controls[i]; |
| 1255 | if (ctrl->index == info->index) { |
| 1256 | found = 1; |
| 1257 | break; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | if (found) |
| 1262 | break; |
| 1263 | } |
| 1264 | |
| 1265 | if (!found) |
| 1266 | return; |
| 1267 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1268 | if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1269 | /* Check if the device control information and length match |
| 1270 | * the user supplied information. |
| 1271 | */ |
| 1272 | __u32 flags; |
| 1273 | __le16 size; |
| 1274 | __u8 inf; |
| 1275 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1276 | ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, |
| 1277 | dev->intfnum, info->selector, (__u8 *)&size, 2); |
| 1278 | if (ret < 0) { |
Laurent Pinchart | 36bd883 | 2010-01-19 09:06:22 -0300 | [diff] [blame] | 1279 | uvc_trace(UVC_TRACE_CONTROL, |
| 1280 | "GET_LEN failed on control %pUl/%u (%d).\n", |
| 1281 | info->entity, info->selector, ret); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1282 | return; |
| 1283 | } |
| 1284 | |
| 1285 | if (info->size != le16_to_cpu(size)) { |
Laurent Pinchart | 36bd883 | 2010-01-19 09:06:22 -0300 | [diff] [blame] | 1286 | uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u size " |
| 1287 | "doesn't match user supplied value.\n", |
| 1288 | info->entity, info->selector); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1289 | return; |
| 1290 | } |
| 1291 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1292 | ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, |
| 1293 | dev->intfnum, info->selector, &inf, 1); |
| 1294 | if (ret < 0) { |
Laurent Pinchart | 36bd883 | 2010-01-19 09:06:22 -0300 | [diff] [blame] | 1295 | uvc_trace(UVC_TRACE_CONTROL, |
| 1296 | "GET_INFO failed on control %pUl/%u (%d).\n", |
| 1297 | info->entity, info->selector, ret); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1298 | return; |
| 1299 | } |
| 1300 | |
| 1301 | flags = info->flags; |
| 1302 | if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) || |
| 1303 | ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) { |
Laurent Pinchart | 36bd883 | 2010-01-19 09:06:22 -0300 | [diff] [blame] | 1304 | uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u flags " |
| 1305 | "don't match supported operations.\n", |
| 1306 | info->entity, info->selector); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1307 | return; |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | ctrl->info = info; |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 1312 | ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_DATA_LAST, GFP_KERNEL); |
Laurent Pinchart | 36bd883 | 2010-01-19 09:06:22 -0300 | [diff] [blame] | 1313 | uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s " |
| 1314 | "entity %u\n", ctrl->info->entity, ctrl->info->selector, |
| 1315 | dev->udev->devpath, entity->id); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | * Add an item to the UVC control information list, and instantiate a control |
| 1320 | * structure for each device that supports the control. |
| 1321 | */ |
| 1322 | int uvc_ctrl_add_info(struct uvc_control_info *info) |
| 1323 | { |
| 1324 | struct uvc_control_info *ctrl; |
| 1325 | struct uvc_device *dev; |
| 1326 | int ret = 0; |
| 1327 | |
| 1328 | /* Find matching controls by walking the devices, entities and |
| 1329 | * controls list. |
| 1330 | */ |
| 1331 | mutex_lock(&uvc_driver.ctrl_mutex); |
| 1332 | |
| 1333 | /* First check if the list contains a control matching the new one. |
| 1334 | * Bail out if it does. |
| 1335 | */ |
| 1336 | list_for_each_entry(ctrl, &uvc_driver.controls, list) { |
| 1337 | if (memcmp(ctrl->entity, info->entity, 16)) |
| 1338 | continue; |
| 1339 | |
| 1340 | if (ctrl->selector == info->selector) { |
Laurent Pinchart | 36bd883 | 2010-01-19 09:06:22 -0300 | [diff] [blame] | 1341 | uvc_trace(UVC_TRACE_CONTROL, |
| 1342 | "Control %pUl/%u is already defined.\n", |
| 1343 | info->entity, info->selector); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1344 | ret = -EEXIST; |
| 1345 | goto end; |
| 1346 | } |
| 1347 | if (ctrl->index == info->index) { |
Laurent Pinchart | 36bd883 | 2010-01-19 09:06:22 -0300 | [diff] [blame] | 1348 | uvc_trace(UVC_TRACE_CONTROL, |
| 1349 | "Control %pUl/%u would overwrite index %d.\n", |
| 1350 | info->entity, info->selector, info->index); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1351 | ret = -EEXIST; |
| 1352 | goto end; |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | list_for_each_entry(dev, &uvc_driver.devices, list) |
| 1357 | uvc_ctrl_add_ctrl(dev, info); |
| 1358 | |
| 1359 | INIT_LIST_HEAD(&info->mappings); |
| 1360 | list_add_tail(&info->list, &uvc_driver.controls); |
| 1361 | end: |
| 1362 | mutex_unlock(&uvc_driver.ctrl_mutex); |
| 1363 | return ret; |
| 1364 | } |
| 1365 | |
| 1366 | int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping) |
| 1367 | { |
| 1368 | struct uvc_control_info *info; |
| 1369 | struct uvc_control_mapping *map; |
| 1370 | int ret = -EINVAL; |
| 1371 | |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 1372 | if (mapping->get == NULL) |
| 1373 | mapping->get = uvc_get_le_value; |
| 1374 | if (mapping->set == NULL) |
| 1375 | mapping->set = uvc_set_le_value; |
| 1376 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1377 | if (mapping->id & ~V4L2_CTRL_ID_MASK) { |
| 1378 | uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with " |
| 1379 | "invalid control id 0x%08x\n", mapping->name, |
| 1380 | mapping->id); |
| 1381 | return -EINVAL; |
| 1382 | } |
| 1383 | |
| 1384 | mutex_lock(&uvc_driver.ctrl_mutex); |
| 1385 | list_for_each_entry(info, &uvc_driver.controls, list) { |
| 1386 | if (memcmp(info->entity, mapping->entity, 16) || |
| 1387 | info->selector != mapping->selector) |
| 1388 | continue; |
| 1389 | |
| 1390 | if (info->size * 8 < mapping->size + mapping->offset) { |
Laurent Pinchart | 36bd883 | 2010-01-19 09:06:22 -0300 | [diff] [blame] | 1391 | uvc_trace(UVC_TRACE_CONTROL, |
| 1392 | "Mapping '%s' would overflow control %pUl/%u\n", |
| 1393 | mapping->name, info->entity, info->selector); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1394 | ret = -EOVERFLOW; |
| 1395 | goto end; |
| 1396 | } |
| 1397 | |
| 1398 | /* Check if the list contains a mapping matching the new one. |
| 1399 | * Bail out if it does. |
| 1400 | */ |
| 1401 | list_for_each_entry(map, &info->mappings, list) { |
| 1402 | if (map->id == mapping->id) { |
| 1403 | uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is " |
| 1404 | "already defined.\n", mapping->name); |
| 1405 | ret = -EEXIST; |
| 1406 | goto end; |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | mapping->ctrl = info; |
| 1411 | list_add_tail(&mapping->list, &info->mappings); |
Laurent Pinchart | 36bd883 | 2010-01-19 09:06:22 -0300 | [diff] [blame] | 1412 | uvc_trace(UVC_TRACE_CONTROL, |
| 1413 | "Adding mapping %s to control %pUl/%u.\n", |
| 1414 | mapping->name, info->entity, info->selector); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1415 | |
| 1416 | ret = 0; |
| 1417 | break; |
| 1418 | } |
| 1419 | end: |
| 1420 | mutex_unlock(&uvc_driver.ctrl_mutex); |
| 1421 | return ret; |
| 1422 | } |
| 1423 | |
| 1424 | /* |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1425 | * Prune an entity of its bogus controls using a blacklist. Bogus controls |
| 1426 | * are currently the ones that crash the camera or unconditionally return an |
| 1427 | * error when queried. |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1428 | */ |
| 1429 | static void |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1430 | uvc_ctrl_prune_entity(struct uvc_device *dev, struct uvc_entity *entity) |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1431 | { |
| 1432 | static const struct { |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1433 | struct usb_device_id id; |
| 1434 | u8 index; |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1435 | } blacklist[] = { |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1436 | { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */ |
| 1437 | { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */ |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1438 | }; |
| 1439 | |
| 1440 | u8 *controls; |
| 1441 | unsigned int size; |
| 1442 | unsigned int i; |
| 1443 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1444 | if (UVC_ENTITY_TYPE(entity) != UVC_VC_PROCESSING_UNIT) |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1445 | return; |
| 1446 | |
| 1447 | controls = entity->processing.bmControls; |
| 1448 | size = entity->processing.bControlSize; |
| 1449 | |
| 1450 | for (i = 0; i < ARRAY_SIZE(blacklist); ++i) { |
Laurent Pinchart | 385097e | 2009-12-09 22:31:21 -0300 | [diff] [blame] | 1451 | if (!usb_match_one_id(dev->intf, &blacklist[i].id)) |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1452 | continue; |
| 1453 | |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1454 | if (blacklist[i].index >= 8 * size || |
| 1455 | !uvc_test_bit(controls, blacklist[i].index)) |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1456 | continue; |
| 1457 | |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1458 | uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, " |
| 1459 | "removing it.\n", entity->id, blacklist[i].index); |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1460 | |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1461 | uvc_clear_bit(controls, blacklist[i].index); |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | /* |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1466 | * Initialize device controls. |
| 1467 | */ |
| 1468 | int uvc_ctrl_init_device(struct uvc_device *dev) |
| 1469 | { |
| 1470 | struct uvc_control_info *info; |
| 1471 | struct uvc_control *ctrl; |
| 1472 | struct uvc_entity *entity; |
| 1473 | unsigned int i; |
| 1474 | |
| 1475 | /* Walk the entities list and instantiate controls */ |
| 1476 | list_for_each_entry(entity, &dev->entities, list) { |
| 1477 | unsigned int bControlSize = 0, ncontrols = 0; |
| 1478 | __u8 *bmControls = NULL; |
| 1479 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1480 | if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1481 | bmControls = entity->extension.bmControls; |
| 1482 | bControlSize = entity->extension.bControlSize; |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1483 | } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1484 | bmControls = entity->processing.bmControls; |
| 1485 | bControlSize = entity->processing.bControlSize; |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1486 | } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1487 | bmControls = entity->camera.bmControls; |
| 1488 | bControlSize = entity->camera.bControlSize; |
| 1489 | } |
| 1490 | |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1491 | uvc_ctrl_prune_entity(dev, entity); |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1492 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1493 | for (i = 0; i < bControlSize; ++i) |
| 1494 | ncontrols += hweight8(bmControls[i]); |
| 1495 | |
| 1496 | if (ncontrols == 0) |
| 1497 | continue; |
| 1498 | |
| 1499 | entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL); |
| 1500 | if (entity->controls == NULL) |
| 1501 | return -ENOMEM; |
| 1502 | |
| 1503 | entity->ncontrols = ncontrols; |
| 1504 | |
| 1505 | ctrl = entity->controls; |
| 1506 | for (i = 0; i < bControlSize * 8; ++i) { |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1507 | if (uvc_test_bit(bmControls, i) == 0) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1508 | continue; |
| 1509 | |
| 1510 | ctrl->entity = entity; |
| 1511 | ctrl->index = i; |
| 1512 | ctrl++; |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | /* Walk the controls info list and associate them with the device |
| 1517 | * controls, then add the device to the global device list. This has |
| 1518 | * to be done while holding the controls lock, to make sure |
| 1519 | * uvc_ctrl_add_info() will not get called in-between. |
| 1520 | */ |
| 1521 | mutex_lock(&uvc_driver.ctrl_mutex); |
| 1522 | list_for_each_entry(info, &uvc_driver.controls, list) |
| 1523 | uvc_ctrl_add_ctrl(dev, info); |
| 1524 | |
| 1525 | list_add_tail(&dev->list, &uvc_driver.devices); |
| 1526 | mutex_unlock(&uvc_driver.ctrl_mutex); |
| 1527 | |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
| 1531 | /* |
| 1532 | * Cleanup device controls. |
| 1533 | */ |
| 1534 | void uvc_ctrl_cleanup_device(struct uvc_device *dev) |
| 1535 | { |
| 1536 | struct uvc_entity *entity; |
| 1537 | unsigned int i; |
| 1538 | |
| 1539 | /* Remove the device from the global devices list */ |
| 1540 | mutex_lock(&uvc_driver.ctrl_mutex); |
| 1541 | if (dev->list.next != NULL) |
| 1542 | list_del(&dev->list); |
| 1543 | mutex_unlock(&uvc_driver.ctrl_mutex); |
| 1544 | |
| 1545 | list_for_each_entry(entity, &dev->entities, list) { |
| 1546 | for (i = 0; i < entity->ncontrols; ++i) |
| 1547 | kfree(entity->controls[i].data); |
| 1548 | |
| 1549 | kfree(entity->controls); |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | void uvc_ctrl_init(void) |
| 1554 | { |
| 1555 | struct uvc_control_info *ctrl = uvc_ctrls; |
| 1556 | struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls); |
| 1557 | struct uvc_control_mapping *mapping = uvc_ctrl_mappings; |
| 1558 | struct uvc_control_mapping *mend = |
| 1559 | mapping + ARRAY_SIZE(uvc_ctrl_mappings); |
| 1560 | |
| 1561 | for (; ctrl < cend; ++ctrl) |
| 1562 | uvc_ctrl_add_info(ctrl); |
| 1563 | |
| 1564 | for (; mapping < mend; ++mapping) |
| 1565 | uvc_ctrl_add_mapping(mapping); |
| 1566 | } |
Hans Verkuil | f87086e | 2008-07-18 00:50:58 -0300 | [diff] [blame] | 1567 | |