Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1 | /* |
| 2 | * uvc_ctrl.c -- USB Video Class driver - Controls |
| 3 | * |
Laurent Pinchart | 11fc5ba | 2010-09-20 06:10:10 -0300 | [diff] [blame] | 4 | * Copyright (C) 2005-2010 |
| 5 | * Laurent Pinchart (laurent.pinchart@ideasonboard.com) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 15 | #include <linux/list.h> |
| 16 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 18 | #include <linux/uaccess.h> |
| 19 | #include <linux/usb.h> |
| 20 | #include <linux/videodev2.h> |
| 21 | #include <linux/vmalloc.h> |
| 22 | #include <linux/wait.h> |
| 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 | 071c8bb | 2010-09-29 16:00:08 -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 | |
Laurent Pinchart | f9d81df | 2010-09-17 05:24:13 -0300 | [diff] [blame] | 730 | static int uvc_entity_match_guid(const struct uvc_entity *entity, |
| 731 | const __u8 guid[16]) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 732 | { |
| 733 | switch (UVC_ENTITY_TYPE(entity)) { |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 734 | case UVC_ITT_CAMERA: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 735 | return memcmp(uvc_camera_guid, guid, 16) == 0; |
| 736 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 737 | case UVC_ITT_MEDIA_TRANSPORT_INPUT: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 738 | return memcmp(uvc_media_transport_input_guid, guid, 16) == 0; |
| 739 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 740 | case UVC_VC_PROCESSING_UNIT: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 741 | return memcmp(uvc_processing_guid, guid, 16) == 0; |
| 742 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 743 | case UVC_VC_EXTENSION_UNIT: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 744 | return memcmp(entity->extension.guidExtensionCode, |
| 745 | guid, 16) == 0; |
| 746 | |
| 747 | default: |
| 748 | return 0; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /* ------------------------------------------------------------------------ |
| 753 | * UVC Controls |
| 754 | */ |
| 755 | |
| 756 | static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id, |
| 757 | struct uvc_control_mapping **mapping, struct uvc_control **control, |
| 758 | int next) |
| 759 | { |
| 760 | struct uvc_control *ctrl; |
| 761 | struct uvc_control_mapping *map; |
| 762 | unsigned int i; |
| 763 | |
| 764 | if (entity == NULL) |
| 765 | return; |
| 766 | |
| 767 | for (i = 0; i < entity->ncontrols; ++i) { |
| 768 | ctrl = &entity->controls[i]; |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 769 | if (!ctrl->initialized) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 770 | continue; |
| 771 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 772 | list_for_each_entry(map, &ctrl->info.mappings, list) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 773 | if ((map->id == v4l2_id) && !next) { |
| 774 | *control = ctrl; |
| 775 | *mapping = map; |
| 776 | return; |
| 777 | } |
| 778 | |
| 779 | if ((*mapping == NULL || (*mapping)->id > map->id) && |
| 780 | (map->id > v4l2_id) && next) { |
| 781 | *control = ctrl; |
| 782 | *mapping = map; |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 788 | struct uvc_control *uvc_find_control(struct uvc_video_chain *chain, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 789 | __u32 v4l2_id, struct uvc_control_mapping **mapping) |
| 790 | { |
| 791 | struct uvc_control *ctrl = NULL; |
| 792 | struct uvc_entity *entity; |
| 793 | int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL; |
| 794 | |
| 795 | *mapping = NULL; |
| 796 | |
| 797 | /* Mask the query flags. */ |
| 798 | v4l2_id &= V4L2_CTRL_ID_MASK; |
| 799 | |
| 800 | /* Find the control. */ |
Laurent Pinchart | 6241d8c | 2009-11-25 12:00:22 -0300 | [diff] [blame] | 801 | list_for_each_entry(entity, &chain->entities, chain) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 802 | __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next); |
| 803 | if (ctrl && !next) |
| 804 | return ctrl; |
| 805 | } |
| 806 | |
| 807 | if (ctrl == NULL && !next) |
| 808 | uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n", |
| 809 | v4l2_id); |
| 810 | |
| 811 | return ctrl; |
| 812 | } |
| 813 | |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 814 | static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain, |
| 815 | struct uvc_control *ctrl) |
| 816 | { |
| 817 | int ret; |
| 818 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 819 | if (ctrl->info.flags & UVC_CONTROL_GET_DEF) { |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 820 | ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id, |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 821 | chain->dev->intfnum, ctrl->info.selector, |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 822 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF), |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 823 | ctrl->info.size); |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 824 | if (ret < 0) |
| 825 | return ret; |
| 826 | } |
| 827 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 828 | if (ctrl->info.flags & UVC_CONTROL_GET_MIN) { |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 829 | ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id, |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 830 | chain->dev->intfnum, ctrl->info.selector, |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 831 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN), |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 832 | ctrl->info.size); |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 833 | if (ret < 0) |
| 834 | return ret; |
| 835 | } |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 836 | if (ctrl->info.flags & UVC_CONTROL_GET_MAX) { |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 837 | ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id, |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 838 | chain->dev->intfnum, ctrl->info.selector, |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 839 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX), |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 840 | ctrl->info.size); |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 841 | if (ret < 0) |
| 842 | return ret; |
| 843 | } |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 844 | if (ctrl->info.flags & UVC_CONTROL_GET_RES) { |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 845 | ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id, |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 846 | chain->dev->intfnum, ctrl->info.selector, |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 847 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 848 | ctrl->info.size); |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 849 | if (ret < 0) |
| 850 | return ret; |
| 851 | } |
| 852 | |
| 853 | ctrl->cached = 1; |
| 854 | return 0; |
| 855 | } |
| 856 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 857 | int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 858 | struct v4l2_queryctrl *v4l2_ctrl) |
| 859 | { |
| 860 | struct uvc_control *ctrl; |
| 861 | struct uvc_control_mapping *mapping; |
| 862 | struct uvc_menu_info *menu; |
| 863 | unsigned int i; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 864 | int ret; |
| 865 | |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 866 | ret = mutex_lock_interruptible(&chain->ctrl_mutex); |
| 867 | if (ret < 0) |
| 868 | return -ERESTARTSYS; |
| 869 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 870 | ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping); |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 871 | if (ctrl == NULL) { |
| 872 | ret = -EINVAL; |
| 873 | goto done; |
| 874 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 875 | |
Laurent Pinchart | 54812c7 | 2008-07-17 07:37:37 -0300 | [diff] [blame] | 876 | memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 877 | v4l2_ctrl->id = mapping->id; |
| 878 | v4l2_ctrl->type = mapping->v4l2_type; |
Laurent Pinchart | d0ebf30 | 2009-01-08 14:05:11 -0300 | [diff] [blame] | 879 | strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 880 | v4l2_ctrl->flags = 0; |
| 881 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 882 | if (!(ctrl->info.flags & UVC_CONTROL_GET_CUR)) |
Laurent Pinchart | a90ef69 | 2010-04-25 16:23:24 -0300 | [diff] [blame] | 883 | v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY; |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 884 | if (!(ctrl->info.flags & UVC_CONTROL_SET_CUR)) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 885 | v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; |
| 886 | |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 887 | if (!ctrl->cached) { |
| 888 | ret = uvc_ctrl_populate_cache(chain, ctrl); |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 889 | if (ret < 0) |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 890 | goto done; |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 891 | } |
| 892 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 893 | if (ctrl->info.flags & UVC_CONTROL_GET_DEF) { |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 894 | v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF, |
| 895 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF)); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 896 | } |
| 897 | |
Laurent Pinchart | 54812c7 | 2008-07-17 07:37:37 -0300 | [diff] [blame] | 898 | switch (mapping->v4l2_type) { |
| 899 | case V4L2_CTRL_TYPE_MENU: |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 900 | v4l2_ctrl->minimum = 0; |
| 901 | v4l2_ctrl->maximum = mapping->menu_count - 1; |
| 902 | v4l2_ctrl->step = 1; |
| 903 | |
| 904 | menu = mapping->menu_info; |
| 905 | for (i = 0; i < mapping->menu_count; ++i, ++menu) { |
| 906 | if (menu->value == v4l2_ctrl->default_value) { |
| 907 | v4l2_ctrl->default_value = i; |
| 908 | break; |
| 909 | } |
| 910 | } |
| 911 | |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 912 | goto done; |
Laurent Pinchart | 54812c7 | 2008-07-17 07:37:37 -0300 | [diff] [blame] | 913 | |
| 914 | case V4L2_CTRL_TYPE_BOOLEAN: |
| 915 | v4l2_ctrl->minimum = 0; |
| 916 | v4l2_ctrl->maximum = 1; |
| 917 | v4l2_ctrl->step = 1; |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 918 | goto done; |
Laurent Pinchart | 54812c7 | 2008-07-17 07:37:37 -0300 | [diff] [blame] | 919 | |
Laurent Pinchart | f4eabaf | 2009-08-06 06:05:40 -0300 | [diff] [blame] | 920 | case V4L2_CTRL_TYPE_BUTTON: |
| 921 | v4l2_ctrl->minimum = 0; |
| 922 | v4l2_ctrl->maximum = 0; |
| 923 | v4l2_ctrl->step = 0; |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 924 | goto done; |
Laurent Pinchart | f4eabaf | 2009-08-06 06:05:40 -0300 | [diff] [blame] | 925 | |
Laurent Pinchart | 54812c7 | 2008-07-17 07:37:37 -0300 | [diff] [blame] | 926 | default: |
| 927 | break; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 928 | } |
| 929 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 930 | if (ctrl->info.flags & UVC_CONTROL_GET_MIN) |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 931 | v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN, |
| 932 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN)); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 933 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 934 | if (ctrl->info.flags & UVC_CONTROL_GET_MAX) |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 935 | v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX, |
| 936 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX)); |
| 937 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 938 | if (ctrl->info.flags & UVC_CONTROL_GET_RES) |
Laurent Pinchart | 5952908 | 2010-01-23 06:30:20 -0300 | [diff] [blame] | 939 | v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES, |
| 940 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); |
| 941 | |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 942 | done: |
| 943 | mutex_unlock(&chain->ctrl_mutex); |
| 944 | return ret; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | |
| 948 | /* -------------------------------------------------------------------------- |
| 949 | * Control transactions |
| 950 | * |
| 951 | * To make extended set operations as atomic as the hardware allows, controls |
| 952 | * are handled using begin/commit/rollback operations. |
| 953 | * |
| 954 | * At the beginning of a set request, uvc_ctrl_begin should be called to |
| 955 | * initialize the request. This function acquires the control lock. |
| 956 | * |
| 957 | * When setting a control, the new value is stored in the control data field |
| 958 | * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for |
| 959 | * later processing. If the UVC and V4L2 control sizes differ, the current |
| 960 | * value is loaded from the hardware before storing the new value in the data |
| 961 | * field. |
| 962 | * |
| 963 | * After processing all controls in the transaction, uvc_ctrl_commit or |
| 964 | * uvc_ctrl_rollback must be called to apply the pending changes to the |
| 965 | * hardware or revert them. When applying changes, all controls marked as |
| 966 | * dirty will be modified in the UVC device, and the dirty flag will be |
| 967 | * cleared. When reverting controls, the control data field |
| 968 | * UVC_CTRL_DATA_CURRENT is reverted to its previous value |
| 969 | * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the |
| 970 | * control lock. |
| 971 | */ |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 972 | int uvc_ctrl_begin(struct uvc_video_chain *chain) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 973 | { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 974 | return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | static int uvc_ctrl_commit_entity(struct uvc_device *dev, |
| 978 | struct uvc_entity *entity, int rollback) |
| 979 | { |
| 980 | struct uvc_control *ctrl; |
| 981 | unsigned int i; |
| 982 | int ret; |
| 983 | |
| 984 | if (entity == NULL) |
| 985 | return 0; |
| 986 | |
| 987 | for (i = 0; i < entity->ncontrols; ++i) { |
| 988 | ctrl = &entity->controls[i]; |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 989 | if (!ctrl->initialized) |
Laurent Pinchart | b1accfa | 2008-09-27 20:54:02 -0300 | [diff] [blame] | 990 | continue; |
| 991 | |
| 992 | /* Reset the loaded flag for auto-update controls that were |
| 993 | * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent |
| 994 | * uvc_ctrl_get from using the cached value. |
| 995 | */ |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 996 | if (ctrl->info.flags & UVC_CONTROL_AUTO_UPDATE) |
Laurent Pinchart | b1accfa | 2008-09-27 20:54:02 -0300 | [diff] [blame] | 997 | ctrl->loaded = 0; |
| 998 | |
| 999 | if (!ctrl->dirty) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1000 | continue; |
| 1001 | |
| 1002 | if (!rollback) |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1003 | ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id, |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1004 | dev->intfnum, ctrl->info.selector, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1005 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1006 | ctrl->info.size); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1007 | else |
| 1008 | ret = 0; |
| 1009 | |
| 1010 | if (rollback || ret < 0) |
| 1011 | memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
| 1012 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP), |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1013 | ctrl->info.size); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1014 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1015 | ctrl->dirty = 0; |
| 1016 | |
| 1017 | if (ret < 0) |
| 1018 | return ret; |
| 1019 | } |
| 1020 | |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1024 | int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1025 | { |
| 1026 | struct uvc_entity *entity; |
| 1027 | int ret = 0; |
| 1028 | |
| 1029 | /* Find the control. */ |
Laurent Pinchart | 6241d8c | 2009-11-25 12:00:22 -0300 | [diff] [blame] | 1030 | list_for_each_entry(entity, &chain->entities, chain) { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1031 | ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1032 | if (ret < 0) |
| 1033 | goto done; |
| 1034 | } |
| 1035 | |
| 1036 | done: |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1037 | mutex_unlock(&chain->ctrl_mutex); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1038 | return ret; |
| 1039 | } |
| 1040 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1041 | int uvc_ctrl_get(struct uvc_video_chain *chain, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1042 | struct v4l2_ext_control *xctrl) |
| 1043 | { |
| 1044 | struct uvc_control *ctrl; |
| 1045 | struct uvc_control_mapping *mapping; |
| 1046 | struct uvc_menu_info *menu; |
| 1047 | unsigned int i; |
| 1048 | int ret; |
| 1049 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1050 | ctrl = uvc_find_control(chain, xctrl->id, &mapping); |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1051 | if (ctrl == NULL || (ctrl->info.flags & UVC_CONTROL_GET_CUR) == 0) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1052 | return -EINVAL; |
| 1053 | |
| 1054 | if (!ctrl->loaded) { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1055 | ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id, |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1056 | chain->dev->intfnum, ctrl->info.selector, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1057 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1058 | ctrl->info.size); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1059 | if (ret < 0) |
| 1060 | return ret; |
| 1061 | |
Laurent Pinchart | b1accfa | 2008-09-27 20:54:02 -0300 | [diff] [blame] | 1062 | ctrl->loaded = 1; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1063 | } |
| 1064 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1065 | xctrl->value = mapping->get(mapping, UVC_GET_CUR, |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 1066 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1067 | |
| 1068 | if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) { |
| 1069 | menu = mapping->menu_info; |
| 1070 | for (i = 0; i < mapping->menu_count; ++i, ++menu) { |
| 1071 | if (menu->value == xctrl->value) { |
| 1072 | xctrl->value = i; |
| 1073 | break; |
| 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1081 | int uvc_ctrl_set(struct uvc_video_chain *chain, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1082 | struct v4l2_ext_control *xctrl) |
| 1083 | { |
| 1084 | struct uvc_control *ctrl; |
| 1085 | struct uvc_control_mapping *mapping; |
Laurent Pinchart | e54532e | 2010-01-23 07:07:53 -0300 | [diff] [blame] | 1086 | s32 value; |
| 1087 | u32 step; |
| 1088 | s32 min; |
| 1089 | s32 max; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1090 | int ret; |
| 1091 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1092 | ctrl = uvc_find_control(chain, xctrl->id, &mapping); |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1093 | if (ctrl == NULL || (ctrl->info.flags & UVC_CONTROL_SET_CUR) == 0) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1094 | return -EINVAL; |
| 1095 | |
Laurent Pinchart | e54532e | 2010-01-23 07:07:53 -0300 | [diff] [blame] | 1096 | /* Clamp out of range values. */ |
| 1097 | switch (mapping->v4l2_type) { |
| 1098 | case V4L2_CTRL_TYPE_INTEGER: |
| 1099 | if (!ctrl->cached) { |
| 1100 | ret = uvc_ctrl_populate_cache(chain, ctrl); |
| 1101 | if (ret < 0) |
| 1102 | return ret; |
| 1103 | } |
| 1104 | |
| 1105 | min = mapping->get(mapping, UVC_GET_MIN, |
| 1106 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN)); |
| 1107 | max = mapping->get(mapping, UVC_GET_MAX, |
| 1108 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX)); |
| 1109 | step = mapping->get(mapping, UVC_GET_RES, |
| 1110 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); |
Laurent Pinchart | cf7a50e | 2010-04-25 16:27:14 -0300 | [diff] [blame] | 1111 | if (step == 0) |
| 1112 | step = 1; |
Laurent Pinchart | e54532e | 2010-01-23 07:07:53 -0300 | [diff] [blame] | 1113 | |
| 1114 | xctrl->value = min + (xctrl->value - min + step/2) / step * step; |
| 1115 | xctrl->value = clamp(xctrl->value, min, max); |
| 1116 | value = xctrl->value; |
| 1117 | break; |
| 1118 | |
| 1119 | case V4L2_CTRL_TYPE_BOOLEAN: |
| 1120 | xctrl->value = clamp(xctrl->value, 0, 1); |
| 1121 | value = xctrl->value; |
| 1122 | break; |
| 1123 | |
| 1124 | case V4L2_CTRL_TYPE_MENU: |
| 1125 | if (xctrl->value < 0 || xctrl->value >= mapping->menu_count) |
Laurent Pinchart | 8a4e76c | 2010-01-21 16:53:11 -0300 | [diff] [blame] | 1126 | return -ERANGE; |
Laurent Pinchart | e54532e | 2010-01-23 07:07:53 -0300 | [diff] [blame] | 1127 | value = mapping->menu_info[xctrl->value].value; |
| 1128 | break; |
| 1129 | |
| 1130 | default: |
| 1131 | value = xctrl->value; |
| 1132 | break; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1133 | } |
| 1134 | |
Laurent Pinchart | e54532e | 2010-01-23 07:07:53 -0300 | [diff] [blame] | 1135 | /* If the mapping doesn't span the whole UVC control, the current value |
| 1136 | * needs to be loaded from the device to perform the read-modify-write |
| 1137 | * operation. |
| 1138 | */ |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1139 | if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) { |
| 1140 | if ((ctrl->info.flags & UVC_CONTROL_GET_CUR) == 0) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1141 | memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1142 | 0, ctrl->info.size); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1143 | } else { |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1144 | ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, |
| 1145 | ctrl->entity->id, chain->dev->intfnum, |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1146 | ctrl->info.selector, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1147 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1148 | ctrl->info.size); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1149 | if (ret < 0) |
| 1150 | return ret; |
| 1151 | } |
| 1152 | |
Laurent Pinchart | b1accfa | 2008-09-27 20:54:02 -0300 | [diff] [blame] | 1153 | ctrl->loaded = 1; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1154 | } |
| 1155 | |
Laurent Pinchart | e54532e | 2010-01-23 07:07:53 -0300 | [diff] [blame] | 1156 | /* Backup the current value in case we need to rollback later. */ |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1157 | if (!ctrl->dirty) { |
| 1158 | memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP), |
| 1159 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1160 | ctrl->info.size); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1161 | } |
| 1162 | |
Laurent Pinchart | 9768352 | 2008-12-16 06:46:32 -0300 | [diff] [blame] | 1163 | mapping->set(mapping, value, |
| 1164 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1165 | |
| 1166 | ctrl->dirty = 1; |
| 1167 | ctrl->modified = 1; |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
| 1171 | /* -------------------------------------------------------------------------- |
| 1172 | * Dynamic controls |
| 1173 | */ |
| 1174 | |
Laurent Pinchart | b5977a5 | 2010-10-01 15:39:49 -0300 | [diff] [blame] | 1175 | static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev, |
| 1176 | const struct uvc_control *ctrl, struct uvc_control_info *info) |
| 1177 | { |
| 1178 | struct uvc_ctrl_fixup { |
| 1179 | struct usb_device_id id; |
| 1180 | u8 entity; |
| 1181 | u8 selector; |
| 1182 | u8 flags; |
| 1183 | }; |
| 1184 | |
| 1185 | static const struct uvc_ctrl_fixup fixups[] = { |
| 1186 | { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1, |
| 1187 | UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX | |
| 1188 | UVC_CONTROL_GET_DEF | UVC_CONTROL_SET_CUR | |
| 1189 | UVC_CONTROL_AUTO_UPDATE }, |
| 1190 | { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1, |
| 1191 | UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX | |
| 1192 | UVC_CONTROL_GET_DEF | UVC_CONTROL_SET_CUR | |
| 1193 | UVC_CONTROL_AUTO_UPDATE }, |
| 1194 | { { USB_DEVICE(0x046d, 0x0994) }, 9, 1, |
| 1195 | UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX | |
| 1196 | UVC_CONTROL_GET_DEF | UVC_CONTROL_SET_CUR | |
| 1197 | UVC_CONTROL_AUTO_UPDATE }, |
| 1198 | }; |
| 1199 | |
| 1200 | unsigned int i; |
| 1201 | |
| 1202 | for (i = 0; i < ARRAY_SIZE(fixups); ++i) { |
| 1203 | if (!usb_match_one_id(dev->intf, &fixups[i].id)) |
| 1204 | continue; |
| 1205 | |
| 1206 | if (fixups[i].entity == ctrl->entity->id && |
| 1207 | fixups[i].selector == info->selector) { |
| 1208 | info->flags = fixups[i].flags; |
| 1209 | return; |
| 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | |
Laurent Pinchart | 52c58ad | 2010-09-29 16:03:03 -0300 | [diff] [blame] | 1214 | /* |
| 1215 | * Query control information (size and flags) for XU controls. |
| 1216 | */ |
| 1217 | static int uvc_ctrl_fill_xu_info(struct uvc_device *dev, |
| 1218 | const struct uvc_control *ctrl, struct uvc_control_info *info) |
| 1219 | { |
| 1220 | u8 *data; |
| 1221 | int ret; |
| 1222 | |
| 1223 | data = kmalloc(2, GFP_KERNEL); |
| 1224 | if (data == NULL) |
| 1225 | return -ENOMEM; |
| 1226 | |
| 1227 | memcpy(info->entity, ctrl->entity->extension.guidExtensionCode, |
| 1228 | sizeof(info->entity)); |
| 1229 | info->index = ctrl->index; |
| 1230 | info->selector = ctrl->index + 1; |
| 1231 | |
| 1232 | /* Query and verify the control length (GET_LEN) */ |
| 1233 | ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum, |
| 1234 | info->selector, data, 2); |
| 1235 | if (ret < 0) { |
| 1236 | uvc_trace(UVC_TRACE_CONTROL, |
| 1237 | "GET_LEN failed on control %pUl/%u (%d).\n", |
| 1238 | info->entity, info->selector, ret); |
| 1239 | goto done; |
| 1240 | } |
| 1241 | |
| 1242 | info->size = le16_to_cpup((__le16 *)data); |
| 1243 | |
| 1244 | /* Query the control information (GET_INFO) */ |
| 1245 | ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum, |
| 1246 | info->selector, data, 1); |
| 1247 | if (ret < 0) { |
| 1248 | uvc_trace(UVC_TRACE_CONTROL, |
| 1249 | "GET_INFO failed on control %pUl/%u (%d).\n", |
| 1250 | info->entity, info->selector, ret); |
| 1251 | goto done; |
| 1252 | } |
| 1253 | |
| 1254 | info->flags = UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX |
| 1255 | | UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF |
| 1256 | | (data[0] & UVC_CONTROL_CAP_GET ? UVC_CONTROL_GET_CUR : 0) |
| 1257 | | (data[0] & UVC_CONTROL_CAP_SET ? UVC_CONTROL_SET_CUR : 0) |
| 1258 | | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ? |
| 1259 | UVC_CONTROL_AUTO_UPDATE : 0); |
| 1260 | |
Laurent Pinchart | b5977a5 | 2010-10-01 15:39:49 -0300 | [diff] [blame] | 1261 | uvc_ctrl_fixup_xu_info(dev, ctrl, info); |
| 1262 | |
Laurent Pinchart | 52c58ad | 2010-09-29 16:03:03 -0300 | [diff] [blame] | 1263 | uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, " |
| 1264 | "flags { get %u set %u auto %u }.\n", |
| 1265 | info->entity, info->selector, info->size, |
| 1266 | (info->flags & UVC_CONTROL_GET_CUR) ? 1 : 0, |
| 1267 | (info->flags & UVC_CONTROL_SET_CUR) ? 1 : 0, |
| 1268 | (info->flags & UVC_CONTROL_AUTO_UPDATE) ? 1 : 0); |
| 1269 | |
| 1270 | done: |
| 1271 | kfree(data); |
| 1272 | return ret; |
| 1273 | } |
| 1274 | |
| 1275 | static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl, |
| 1276 | const struct uvc_control_info *info); |
| 1277 | |
| 1278 | static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev, |
| 1279 | struct uvc_control *ctrl) |
| 1280 | { |
| 1281 | struct uvc_control_info info; |
| 1282 | int ret; |
| 1283 | |
| 1284 | if (ctrl->initialized) |
| 1285 | return 0; |
| 1286 | |
| 1287 | ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info); |
| 1288 | if (ret < 0) |
| 1289 | return ret; |
| 1290 | |
| 1291 | ret = uvc_ctrl_add_info(dev, ctrl, &info); |
| 1292 | if (ret < 0) |
| 1293 | uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control " |
| 1294 | "%pUl/%u on device %s entity %u\n", info.entity, |
| 1295 | info.selector, dev->udev->devpath, ctrl->entity->id); |
| 1296 | |
| 1297 | return ret; |
| 1298 | } |
| 1299 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1300 | int uvc_xu_ctrl_query(struct uvc_video_chain *chain, |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1301 | struct uvc_xu_control *xctrl, int set) |
| 1302 | { |
| 1303 | struct uvc_entity *entity; |
| 1304 | struct uvc_control *ctrl = NULL; |
| 1305 | unsigned int i, found = 0; |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 1306 | int restore = 0; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1307 | __u8 *data; |
| 1308 | int ret; |
| 1309 | |
| 1310 | /* Find the extension unit. */ |
Laurent Pinchart | 6241d8c | 2009-11-25 12:00:22 -0300 | [diff] [blame] | 1311 | list_for_each_entry(entity, &chain->entities, chain) { |
| 1312 | if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT && |
| 1313 | entity->id == xctrl->unit) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1314 | break; |
| 1315 | } |
| 1316 | |
| 1317 | if (entity->id != xctrl->unit) { |
| 1318 | uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n", |
| 1319 | xctrl->unit); |
| 1320 | return -EINVAL; |
| 1321 | } |
| 1322 | |
Laurent Pinchart | 52c58ad | 2010-09-29 16:03:03 -0300 | [diff] [blame] | 1323 | /* Find the control and perform delayed initialization if needed. */ |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1324 | for (i = 0; i < entity->ncontrols; ++i) { |
| 1325 | ctrl = &entity->controls[i]; |
Laurent Pinchart | 52c58ad | 2010-09-29 16:03:03 -0300 | [diff] [blame] | 1326 | if (ctrl->index == xctrl->selector - 1) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1327 | found = 1; |
| 1328 | break; |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | if (!found) { |
Laurent Pinchart | 36bd883 | 2010-01-19 09:06:22 -0300 | [diff] [blame] | 1333 | uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n", |
| 1334 | entity->extension.guidExtensionCode, xctrl->selector); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1335 | return -EINVAL; |
| 1336 | } |
| 1337 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1338 | if (mutex_lock_interruptible(&chain->ctrl_mutex)) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1339 | return -ERESTARTSYS; |
| 1340 | |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 1341 | ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl); |
| 1342 | if (ret < 0) { |
| 1343 | ret = -ENOENT; |
| 1344 | goto done; |
| 1345 | } |
| 1346 | |
| 1347 | /* Validate control data size. */ |
| 1348 | if (ctrl->info.size != xctrl->size) { |
| 1349 | ret = -EINVAL; |
| 1350 | goto done; |
| 1351 | } |
| 1352 | |
| 1353 | if ((set && !(ctrl->info.flags & UVC_CONTROL_SET_CUR)) || |
| 1354 | (!set && !(ctrl->info.flags & UVC_CONTROL_GET_CUR))) { |
| 1355 | ret = -EINVAL; |
| 1356 | goto done; |
| 1357 | } |
| 1358 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1359 | memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP), |
| 1360 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 1361 | ctrl->info.size); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1362 | data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT); |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 1363 | restore = set; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1364 | |
| 1365 | if (set && copy_from_user(data, xctrl->data, xctrl->size)) { |
| 1366 | ret = -EFAULT; |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 1367 | goto done; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1368 | } |
| 1369 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1370 | ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR, |
| 1371 | xctrl->unit, chain->dev->intfnum, xctrl->selector, |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1372 | data, xctrl->size); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1373 | if (ret < 0) |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 1374 | goto done; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1375 | |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 1376 | if (!set && copy_to_user(xctrl->data, data, xctrl->size)) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1377 | ret = -EFAULT; |
Laurent Pinchart | 27a61c1 | 2010-10-02 09:04:53 -0300 | [diff] [blame^] | 1378 | done: |
| 1379 | if (ret && restore) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1380 | memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), |
| 1381 | uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP), |
| 1382 | xctrl->size); |
| 1383 | |
Laurent Pinchart | 8e11359 | 2009-07-01 20:24:47 -0300 | [diff] [blame] | 1384 | mutex_unlock(&chain->ctrl_mutex); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1385 | return ret; |
| 1386 | } |
| 1387 | |
| 1388 | /* -------------------------------------------------------------------------- |
| 1389 | * Suspend/resume |
| 1390 | */ |
| 1391 | |
| 1392 | /* |
| 1393 | * Restore control values after resume, skipping controls that haven't been |
| 1394 | * changed. |
| 1395 | * |
| 1396 | * TODO |
| 1397 | * - Don't restore modified controls that are back to their default value. |
| 1398 | * - Handle restore order (Auto-Exposure Mode should be restored before |
| 1399 | * Exposure Time). |
| 1400 | */ |
| 1401 | int uvc_ctrl_resume_device(struct uvc_device *dev) |
| 1402 | { |
| 1403 | struct uvc_control *ctrl; |
| 1404 | struct uvc_entity *entity; |
| 1405 | unsigned int i; |
| 1406 | int ret; |
| 1407 | |
| 1408 | /* Walk the entities list and restore controls when possible. */ |
| 1409 | list_for_each_entry(entity, &dev->entities, list) { |
| 1410 | |
| 1411 | for (i = 0; i < entity->ncontrols; ++i) { |
| 1412 | ctrl = &entity->controls[i]; |
| 1413 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1414 | if (!ctrl->initialized || !ctrl->modified || |
| 1415 | (ctrl->info.flags & UVC_CONTROL_RESTORE) == 0) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1416 | continue; |
| 1417 | |
Laurent Pinchart | 36bd883 | 2010-01-19 09:06:22 -0300 | [diff] [blame] | 1418 | printk(KERN_INFO "restoring control %pUl/%u/%u\n", |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1419 | ctrl->info.entity, ctrl->info.index, |
| 1420 | ctrl->info.selector); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1421 | ctrl->dirty = 1; |
| 1422 | } |
| 1423 | |
| 1424 | ret = uvc_ctrl_commit_entity(dev, entity, 0); |
| 1425 | if (ret < 0) |
| 1426 | return ret; |
| 1427 | } |
| 1428 | |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | /* -------------------------------------------------------------------------- |
| 1433 | * Control and mapping handling |
| 1434 | */ |
| 1435 | |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1436 | /* |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1437 | * Add control information to a given control. |
| 1438 | */ |
| 1439 | static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl, |
| 1440 | const struct uvc_control_info *info) |
| 1441 | { |
| 1442 | int ret = 0; |
| 1443 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1444 | memcpy(&ctrl->info, info, sizeof(*info)); |
| 1445 | INIT_LIST_HEAD(&ctrl->info.mappings); |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1446 | |
| 1447 | /* Allocate an array to save control values (cur, def, max, etc.) */ |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1448 | ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1, |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1449 | GFP_KERNEL); |
| 1450 | if (ctrl->uvc_data == NULL) { |
| 1451 | ret = -ENOMEM; |
| 1452 | goto done; |
| 1453 | } |
| 1454 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1455 | ctrl->initialized = 1; |
| 1456 | |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1457 | uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s " |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1458 | "entity %u\n", ctrl->info.entity, ctrl->info.selector, |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1459 | dev->udev->devpath, ctrl->entity->id); |
| 1460 | |
| 1461 | done: |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1462 | if (ret < 0) |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1463 | kfree(ctrl->uvc_data); |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1464 | return ret; |
| 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * Add a control mapping to a given control. |
| 1469 | */ |
| 1470 | static int __uvc_ctrl_add_mapping(struct uvc_device *dev, |
| 1471 | struct uvc_control *ctrl, const struct uvc_control_mapping *mapping) |
| 1472 | { |
| 1473 | struct uvc_control_mapping *map; |
| 1474 | unsigned int size; |
| 1475 | |
| 1476 | /* Most mappings come from static kernel data and need to be duplicated. |
| 1477 | * Mappings that come from userspace will be unnecessarily duplicated, |
| 1478 | * this could be optimized. |
| 1479 | */ |
| 1480 | map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL); |
| 1481 | if (map == NULL) |
| 1482 | return -ENOMEM; |
| 1483 | |
| 1484 | size = sizeof(*mapping->menu_info) * mapping->menu_count; |
| 1485 | map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL); |
| 1486 | if (map->menu_info == NULL) { |
| 1487 | kfree(map); |
| 1488 | return -ENOMEM; |
| 1489 | } |
| 1490 | |
| 1491 | if (map->get == NULL) |
| 1492 | map->get = uvc_get_le_value; |
| 1493 | if (map->set == NULL) |
| 1494 | map->set = uvc_set_le_value; |
| 1495 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1496 | map->ctrl = &ctrl->info; |
| 1497 | list_add_tail(&map->list, &ctrl->info.mappings); |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1498 | uvc_trace(UVC_TRACE_CONTROL, |
| 1499 | "Adding mapping '%s' to control %pUl/%u.\n", |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1500 | map->name, ctrl->info.entity, ctrl->info.selector); |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1501 | |
| 1502 | return 0; |
| 1503 | } |
| 1504 | |
| 1505 | int uvc_ctrl_add_mapping(struct uvc_video_chain *chain, |
| 1506 | const struct uvc_control_mapping *mapping) |
| 1507 | { |
| 1508 | struct uvc_device *dev = chain->dev; |
| 1509 | struct uvc_control_mapping *map; |
| 1510 | struct uvc_entity *entity; |
| 1511 | struct uvc_control *ctrl; |
| 1512 | int found = 0; |
Martin Rubli | 8fb91b3 | 2010-09-08 04:15:23 -0300 | [diff] [blame] | 1513 | int ret; |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1514 | |
| 1515 | if (mapping->id & ~V4L2_CTRL_ID_MASK) { |
| 1516 | uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control " |
Laurent Pinchart | 52c58ad | 2010-09-29 16:03:03 -0300 | [diff] [blame] | 1517 | "id 0x%08x is invalid.\n", mapping->name, |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1518 | mapping->id); |
| 1519 | return -EINVAL; |
| 1520 | } |
| 1521 | |
| 1522 | /* Search for the matching (GUID/CS) control in the given device */ |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1523 | list_for_each_entry(entity, &dev->entities, list) { |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1524 | unsigned int i; |
| 1525 | |
Laurent Pinchart | 52c58ad | 2010-09-29 16:03:03 -0300 | [diff] [blame] | 1526 | if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT || |
| 1527 | !uvc_entity_match_guid(entity, mapping->entity)) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1528 | continue; |
| 1529 | |
| 1530 | for (i = 0; i < entity->ncontrols; ++i) { |
| 1531 | ctrl = &entity->controls[i]; |
Laurent Pinchart | 52c58ad | 2010-09-29 16:03:03 -0300 | [diff] [blame] | 1532 | if (ctrl->index == mapping->selector - 1) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1533 | found = 1; |
| 1534 | break; |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | if (found) |
| 1539 | break; |
| 1540 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1541 | if (!found) |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1542 | return -ENOENT; |
Laurent Pinchart | b30ece5 | 2010-06-18 11:31:24 -0300 | [diff] [blame] | 1543 | |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1544 | if (mutex_lock_interruptible(&chain->ctrl_mutex)) |
| 1545 | return -ERESTARTSYS; |
Laurent Pinchart | b30ece5 | 2010-06-18 11:31:24 -0300 | [diff] [blame] | 1546 | |
Laurent Pinchart | 52c58ad | 2010-09-29 16:03:03 -0300 | [diff] [blame] | 1547 | /* Perform delayed initialization of XU controls */ |
| 1548 | ret = uvc_ctrl_init_xu_ctrl(dev, ctrl); |
| 1549 | if (ret < 0) { |
| 1550 | ret = -ENOENT; |
| 1551 | goto done; |
| 1552 | } |
| 1553 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1554 | list_for_each_entry(map, &ctrl->info.mappings, list) { |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1555 | if (mapping->id == map->id) { |
| 1556 | uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', " |
| 1557 | "control id 0x%08x already exists.\n", |
| 1558 | mapping->name, mapping->id); |
| 1559 | ret = -EEXIST; |
Laurent Pinchart | b30ece5 | 2010-06-18 11:31:24 -0300 | [diff] [blame] | 1560 | goto done; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1561 | } |
| 1562 | } |
| 1563 | |
Martin Rubli | 8fb91b3 | 2010-09-08 04:15:23 -0300 | [diff] [blame] | 1564 | /* Prevent excess memory consumption */ |
| 1565 | if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) { |
| 1566 | atomic_dec(&dev->nmappings); |
| 1567 | uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum " |
| 1568 | "mappings count (%u) exceeded.\n", mapping->name, |
| 1569 | UVC_MAX_CONTROL_MAPPINGS); |
| 1570 | ret = -ENOMEM; |
| 1571 | goto done; |
| 1572 | } |
| 1573 | |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1574 | ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping); |
Martin Rubli | 8fb91b3 | 2010-09-08 04:15:23 -0300 | [diff] [blame] | 1575 | if (ret < 0) |
| 1576 | atomic_dec(&dev->nmappings); |
| 1577 | |
Laurent Pinchart | b30ece5 | 2010-06-18 11:31:24 -0300 | [diff] [blame] | 1578 | done: |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1579 | mutex_unlock(&chain->ctrl_mutex); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1580 | return ret; |
| 1581 | } |
| 1582 | |
| 1583 | /* |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1584 | * Prune an entity of its bogus controls using a blacklist. Bogus controls |
| 1585 | * are currently the ones that crash the camera or unconditionally return an |
| 1586 | * error when queried. |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1587 | */ |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1588 | static void uvc_ctrl_prune_entity(struct uvc_device *dev, |
| 1589 | struct uvc_entity *entity) |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1590 | { |
Laurent Pinchart | fa34168 | 2010-07-23 10:06:08 -0300 | [diff] [blame] | 1591 | struct uvc_ctrl_blacklist { |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1592 | struct usb_device_id id; |
| 1593 | u8 index; |
Laurent Pinchart | fa34168 | 2010-07-23 10:06:08 -0300 | [diff] [blame] | 1594 | }; |
| 1595 | |
| 1596 | static const struct uvc_ctrl_blacklist processing_blacklist[] = { |
Laurent Pinchart | 9405e3c | 2010-02-03 06:49:35 -0300 | [diff] [blame] | 1597 | { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */ |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1598 | { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */ |
| 1599 | { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */ |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1600 | }; |
Laurent Pinchart | fa34168 | 2010-07-23 10:06:08 -0300 | [diff] [blame] | 1601 | static const struct uvc_ctrl_blacklist camera_blacklist[] = { |
| 1602 | { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */ |
| 1603 | }; |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1604 | |
Laurent Pinchart | fa34168 | 2010-07-23 10:06:08 -0300 | [diff] [blame] | 1605 | const struct uvc_ctrl_blacklist *blacklist; |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1606 | unsigned int size; |
Laurent Pinchart | fa34168 | 2010-07-23 10:06:08 -0300 | [diff] [blame] | 1607 | unsigned int count; |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1608 | unsigned int i; |
Laurent Pinchart | fa34168 | 2010-07-23 10:06:08 -0300 | [diff] [blame] | 1609 | u8 *controls; |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1610 | |
Laurent Pinchart | fa34168 | 2010-07-23 10:06:08 -0300 | [diff] [blame] | 1611 | switch (UVC_ENTITY_TYPE(entity)) { |
| 1612 | case UVC_VC_PROCESSING_UNIT: |
| 1613 | blacklist = processing_blacklist; |
| 1614 | count = ARRAY_SIZE(processing_blacklist); |
| 1615 | controls = entity->processing.bmControls; |
| 1616 | size = entity->processing.bControlSize; |
| 1617 | break; |
| 1618 | |
| 1619 | case UVC_ITT_CAMERA: |
| 1620 | blacklist = camera_blacklist; |
| 1621 | count = ARRAY_SIZE(camera_blacklist); |
| 1622 | controls = entity->camera.bmControls; |
| 1623 | size = entity->camera.bControlSize; |
| 1624 | break; |
| 1625 | |
| 1626 | default: |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1627 | return; |
Laurent Pinchart | fa34168 | 2010-07-23 10:06:08 -0300 | [diff] [blame] | 1628 | } |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1629 | |
Laurent Pinchart | fa34168 | 2010-07-23 10:06:08 -0300 | [diff] [blame] | 1630 | for (i = 0; i < count; ++i) { |
Laurent Pinchart | 385097e | 2009-12-09 22:31:21 -0300 | [diff] [blame] | 1631 | if (!usb_match_one_id(dev->intf, &blacklist[i].id)) |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1632 | continue; |
| 1633 | |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1634 | if (blacklist[i].index >= 8 * size || |
| 1635 | !uvc_test_bit(controls, blacklist[i].index)) |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1636 | continue; |
| 1637 | |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1638 | uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, " |
| 1639 | "removing it.\n", entity->id, blacklist[i].index); |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1640 | |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1641 | uvc_clear_bit(controls, blacklist[i].index); |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | /* |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1646 | * Add control information and hardcoded stock control mappings to the given |
| 1647 | * device. |
| 1648 | */ |
| 1649 | static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl) |
| 1650 | { |
| 1651 | const struct uvc_control_info *info = uvc_ctrls; |
| 1652 | const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls); |
| 1653 | const struct uvc_control_mapping *mapping = uvc_ctrl_mappings; |
| 1654 | const struct uvc_control_mapping *mend = |
| 1655 | mapping + ARRAY_SIZE(uvc_ctrl_mappings); |
| 1656 | |
Laurent Pinchart | 52c58ad | 2010-09-29 16:03:03 -0300 | [diff] [blame] | 1657 | /* XU controls initialization requires querying the device for control |
| 1658 | * information. As some buggy UVC devices will crash when queried |
| 1659 | * repeatedly in a tight loop, delay XU controls initialization until |
| 1660 | * first use. |
| 1661 | */ |
| 1662 | if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT) |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1663 | return; |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1664 | |
| 1665 | for (; info < iend; ++info) { |
| 1666 | if (uvc_entity_match_guid(ctrl->entity, info->entity) && |
| 1667 | ctrl->index == info->index) { |
| 1668 | uvc_ctrl_add_info(dev, ctrl, info); |
| 1669 | break; |
| 1670 | } |
| 1671 | } |
| 1672 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1673 | if (!ctrl->initialized) |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1674 | return; |
| 1675 | |
| 1676 | for (; mapping < mend; ++mapping) { |
| 1677 | if (uvc_entity_match_guid(ctrl->entity, mapping->entity) && |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1678 | ctrl->info.selector == mapping->selector) |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1679 | __uvc_ctrl_add_mapping(dev, ctrl, mapping); |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | /* |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1684 | * Initialize device controls. |
| 1685 | */ |
| 1686 | int uvc_ctrl_init_device(struct uvc_device *dev) |
| 1687 | { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1688 | struct uvc_entity *entity; |
| 1689 | unsigned int i; |
| 1690 | |
| 1691 | /* Walk the entities list and instantiate controls */ |
| 1692 | list_for_each_entry(entity, &dev->entities, list) { |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1693 | struct uvc_control *ctrl; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1694 | unsigned int bControlSize = 0, ncontrols = 0; |
| 1695 | __u8 *bmControls = NULL; |
| 1696 | |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1697 | if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1698 | bmControls = entity->extension.bmControls; |
| 1699 | bControlSize = entity->extension.bControlSize; |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1700 | } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1701 | bmControls = entity->processing.bmControls; |
| 1702 | bControlSize = entity->processing.bControlSize; |
Laurent Pinchart | b482d92 | 2009-06-26 11:39:42 -0300 | [diff] [blame] | 1703 | } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) { |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1704 | bmControls = entity->camera.bmControls; |
| 1705 | bControlSize = entity->camera.bControlSize; |
| 1706 | } |
| 1707 | |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1708 | /* Remove bogus/blacklisted controls */ |
Laurent Pinchart | d732c44 | 2009-05-31 17:05:55 -0300 | [diff] [blame] | 1709 | uvc_ctrl_prune_entity(dev, entity); |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1710 | |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1711 | /* Count supported controls and allocate the controls array */ |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1712 | for (i = 0; i < bControlSize; ++i) |
| 1713 | ncontrols += hweight8(bmControls[i]); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1714 | if (ncontrols == 0) |
| 1715 | continue; |
| 1716 | |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1717 | entity->controls = kzalloc(ncontrols * sizeof(*ctrl), |
| 1718 | GFP_KERNEL); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1719 | if (entity->controls == NULL) |
| 1720 | return -ENOMEM; |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1721 | entity->ncontrols = ncontrols; |
| 1722 | |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1723 | /* Initialize all supported controls */ |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1724 | ctrl = entity->controls; |
| 1725 | for (i = 0; i < bControlSize * 8; ++i) { |
Laurent Pinchart | 2bdd29c | 2008-12-06 17:43:40 -0300 | [diff] [blame] | 1726 | if (uvc_test_bit(bmControls, i) == 0) |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1727 | continue; |
| 1728 | |
| 1729 | ctrl->entity = entity; |
| 1730 | ctrl->index = i; |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1731 | |
| 1732 | uvc_ctrl_init_ctrl(dev, ctrl); |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1733 | ctrl++; |
| 1734 | } |
| 1735 | } |
| 1736 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1737 | return 0; |
| 1738 | } |
| 1739 | |
| 1740 | /* |
| 1741 | * Cleanup device controls. |
| 1742 | */ |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1743 | static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev, |
| 1744 | struct uvc_control *ctrl) |
| 1745 | { |
| 1746 | struct uvc_control_mapping *mapping, *nm; |
| 1747 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1748 | list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) { |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1749 | list_del(&mapping->list); |
| 1750 | kfree(mapping->menu_info); |
| 1751 | kfree(mapping); |
| 1752 | } |
| 1753 | } |
| 1754 | |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1755 | void uvc_ctrl_cleanup_device(struct uvc_device *dev) |
| 1756 | { |
| 1757 | struct uvc_entity *entity; |
| 1758 | unsigned int i; |
| 1759 | |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1760 | /* Free controls and control mappings for all entities. */ |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1761 | list_for_each_entry(entity, &dev->entities, list) { |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1762 | for (i = 0; i < entity->ncontrols; ++i) { |
| 1763 | struct uvc_control *ctrl = &entity->controls[i]; |
| 1764 | |
Laurent Pinchart | 071c8bb | 2010-09-29 16:00:08 -0300 | [diff] [blame] | 1765 | if (!ctrl->initialized) |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1766 | continue; |
| 1767 | |
| 1768 | uvc_ctrl_cleanup_mappings(dev, ctrl); |
| 1769 | kfree(ctrl->uvc_data); |
Laurent Pinchart | ba2fa99 | 2010-09-20 05:53:21 -0300 | [diff] [blame] | 1770 | } |
Laurent Pinchart | c0efd23 | 2008-06-30 15:04:50 -0300 | [diff] [blame] | 1771 | |
| 1772 | kfree(entity->controls); |
| 1773 | } |
| 1774 | } |