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