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