[media] uvcvideo: Delay initialization of XU controls
[pandora-kernel.git] / drivers / media / video / uvc / uvc_ctrl.c
1 /*
2  *      uvc_ctrl.c  --  USB Video Class driver - Controls
3  *
4  *      Copyright (C) 2005-2010
5  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
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
27 #define UVC_CTRL_DATA_CURRENT   0
28 #define UVC_CTRL_DATA_BACKUP    1
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
34
35 /* ------------------------------------------------------------------------
36  * Controls
37  */
38
39 static struct uvc_control_info uvc_ctrls[] = {
40         {
41                 .entity         = UVC_GUID_UVC_PROCESSING,
42                 .selector       = UVC_PU_BRIGHTNESS_CONTROL,
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,
50                 .selector       = UVC_PU_CONTRAST_CONTROL,
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,
58                 .selector       = UVC_PU_HUE_CONTROL,
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,
66                 .selector       = UVC_PU_SATURATION_CONTROL,
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,
74                 .selector       = UVC_PU_SHARPNESS_CONTROL,
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,
82                 .selector       = UVC_PU_GAMMA_CONTROL,
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,
90                 .selector       = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
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,
98                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
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,
106                 .selector       = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
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,
114                 .selector       = UVC_PU_GAIN_CONTROL,
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,
122                 .selector       = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
123                 .index          = 10,
124                 .size           = 1,
125                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
126                                 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
127         },
128         {
129                 .entity         = UVC_GUID_UVC_PROCESSING,
130                 .selector       = UVC_PU_HUE_AUTO_CONTROL,
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         {
137                 .entity         = UVC_GUID_UVC_PROCESSING,
138                 .selector       = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
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,
146                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
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,
154                 .selector       = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
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,
162                 .selector       = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
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,
170                 .selector       = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
171                 .index          = 16,
172                 .size           = 1,
173                 .flags          = UVC_CONTROL_GET_CUR,
174         },
175         {
176                 .entity         = UVC_GUID_UVC_PROCESSING,
177                 .selector       = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
178                 .index          = 17,
179                 .size           = 1,
180                 .flags          = UVC_CONTROL_GET_CUR,
181         },
182         {
183                 .entity         = UVC_GUID_UVC_CAMERA,
184                 .selector       = UVC_CT_SCANNING_MODE_CONTROL,
185                 .index          = 0,
186                 .size           = 1,
187                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
188                                 | UVC_CONTROL_RESTORE,
189         },
190         {
191                 .entity         = UVC_GUID_UVC_CAMERA,
192                 .selector       = UVC_CT_AE_MODE_CONTROL,
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,
201                 .selector       = UVC_CT_AE_PRIORITY_CONTROL,
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,
209                 .selector       = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
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,
217                 .selector       = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
218                 .index          = 4,
219                 .size           = 1,
220                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_RESTORE,
221         },
222         {
223                 .entity         = UVC_GUID_UVC_CAMERA,
224                 .selector       = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
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,
232                 .selector       = UVC_CT_FOCUS_RELATIVE_CONTROL,
233                 .index          = 6,
234                 .size           = 2,
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,
238         },
239         {
240                 .entity         = UVC_GUID_UVC_CAMERA,
241                 .selector       = UVC_CT_IRIS_ABSOLUTE_CONTROL,
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,
249                 .selector       = UVC_CT_IRIS_RELATIVE_CONTROL,
250                 .index          = 8,
251                 .size           = 1,
252                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_AUTO_UPDATE,
253         },
254         {
255                 .entity         = UVC_GUID_UVC_CAMERA,
256                 .selector       = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
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,
264                 .selector       = UVC_CT_ZOOM_RELATIVE_CONTROL,
265                 .index          = 10,
266                 .size           = 3,
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,
270         },
271         {
272                 .entity         = UVC_GUID_UVC_CAMERA,
273                 .selector       = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
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,
281                 .selector       = UVC_CT_PANTILT_RELATIVE_CONTROL,
282                 .index          = 12,
283                 .size           = 4,
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,
287         },
288         {
289                 .entity         = UVC_GUID_UVC_CAMERA,
290                 .selector       = UVC_CT_ROLL_ABSOLUTE_CONTROL,
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,
298                 .selector       = UVC_CT_ROLL_RELATIVE_CONTROL,
299                 .index          = 14,
300                 .size           = 2,
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,
304         },
305         {
306                 .entity         = UVC_GUID_UVC_CAMERA,
307                 .selector       = UVC_CT_FOCUS_AUTO_CONTROL,
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         {
314                 .entity         = UVC_GUID_UVC_CAMERA,
315                 .selector       = UVC_CT_PRIVACY_CONTROL,
316                 .index          = 18,
317                 .size           = 1,
318                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
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[] = {
330         { 2, "Auto Mode" },
331         { 1, "Manual Mode" },
332         { 4, "Shutter Priority Mode" },
333         { 8, "Aperture Priority Mode" },
334 };
335
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) {
342         case UVC_GET_CUR:
343                 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
344
345         case UVC_GET_MIN:
346         case UVC_GET_MAX:
347         case UVC_GET_RES:
348         case UVC_GET_DEF:
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;
358         data[2] = min((int)abs(value), 0xff);
359 }
360
361 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
362         {
363                 .id             = V4L2_CID_BRIGHTNESS,
364                 .name           = "Brightness",
365                 .entity         = UVC_GUID_UVC_PROCESSING,
366                 .selector       = UVC_PU_BRIGHTNESS_CONTROL,
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,
376                 .selector       = UVC_PU_CONTRAST_CONTROL,
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,
386                 .selector       = UVC_PU_HUE_CONTROL,
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,
396                 .selector       = UVC_PU_SATURATION_CONTROL,
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,
406                 .selector       = UVC_PU_SHARPNESS_CONTROL,
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,
416                 .selector       = UVC_PU_GAMMA_CONTROL,
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,
426                 .selector       = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
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,
436                 .selector       = UVC_PU_GAIN_CONTROL,
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,
446                 .selector       = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
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,
458                 .selector       = UVC_PU_HUE_AUTO_CONTROL,
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,
468                 .selector       = UVC_CT_AE_MODE_CONTROL,
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,
480                 .selector       = UVC_CT_AE_PRIORITY_CONTROL,
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,
490                 .selector       = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
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,
500                 .selector       = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
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,
510                 .selector       = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
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,
520                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
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,
530                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
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,
540                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
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,
550                 .selector       = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
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,
560                 .selector       = UVC_CT_FOCUS_AUTO_CONTROL,
561                 .size           = 1,
562                 .offset         = 0,
563                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
564                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
565         },
566         {
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         {
587                 .id             = V4L2_CID_ZOOM_ABSOLUTE,
588                 .name           = "Zoom, Absolute",
589                 .entity         = UVC_GUID_UVC_CAMERA,
590                 .selector       = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
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,
600                 .selector       = UVC_CT_ZOOM_RELATIVE_CONTROL,
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         {
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         {
629                 .id             = V4L2_CID_PRIVACY,
630                 .name           = "Privacy",
631                 .entity         = UVC_GUID_UVC_CAMERA,
632                 .selector       = UVC_CT_PRIVACY_CONTROL,
633                 .size           = 1,
634                 .offset         = 0,
635                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
636                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
637         },
638 };
639
640 /* ------------------------------------------------------------------------
641  * Utility functions
642  */
643
644 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
645 {
646         return ctrl->uvc_data + id * ctrl->info.size;
647 }
648
649 static inline int uvc_test_bit(const __u8 *data, int bit)
650 {
651         return (data[bit >> 3] >> (bit & 7)) & 1;
652 }
653
654 static inline void uvc_clear_bit(__u8 *data, int bit)
655 {
656         data[bit >> 3] &= ~(1 << (bit & 7));
657 }
658
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  */
664 static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
665         __u8 query, const __u8 *data)
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
684         /* Sign-extend the value if needed. */
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  */
694 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
695         __s32 value, __u8 *data)
696 {
697         int bits = mapping->size;
698         int offset = mapping->offset;
699         __u8 mask;
700
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
709         data += offset / 8;
710         offset &= 7;
711
712         for (; bits > 0; data++) {
713                 mask = ((1LL << bits) - 1) << offset;
714                 *data = (*data & ~mask) | ((value << offset) & mask);
715                 value >>= offset ? offset : 8;
716                 bits -= 8 - offset;
717                 offset = 0;
718         }
719 }
720
721 /* ------------------------------------------------------------------------
722  * Terminal and unit management
723  */
724
725 static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
726 static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
727 static const __u8 uvc_media_transport_input_guid[16] =
728         UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
729
730 static int uvc_entity_match_guid(const struct uvc_entity *entity,
731         const __u8 guid[16])
732 {
733         switch (UVC_ENTITY_TYPE(entity)) {
734         case UVC_ITT_CAMERA:
735                 return memcmp(uvc_camera_guid, guid, 16) == 0;
736
737         case UVC_ITT_MEDIA_TRANSPORT_INPUT:
738                 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
739
740         case UVC_VC_PROCESSING_UNIT:
741                 return memcmp(uvc_processing_guid, guid, 16) == 0;
742
743         case UVC_VC_EXTENSION_UNIT:
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];
769                 if (!ctrl->initialized)
770                         continue;
771
772                 list_for_each_entry(map, &ctrl->info.mappings, list) {
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
788 struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
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. */
801         list_for_each_entry(entity, &chain->entities, chain) {
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
814 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
815         struct uvc_control *ctrl)
816 {
817         int ret;
818
819         if (ctrl->info.flags & UVC_CONTROL_GET_DEF) {
820                 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
821                                      chain->dev->intfnum, ctrl->info.selector,
822                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
823                                      ctrl->info.size);
824                 if (ret < 0)
825                         return ret;
826         }
827
828         if (ctrl->info.flags & UVC_CONTROL_GET_MIN) {
829                 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
830                                      chain->dev->intfnum, ctrl->info.selector,
831                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
832                                      ctrl->info.size);
833                 if (ret < 0)
834                         return ret;
835         }
836         if (ctrl->info.flags & UVC_CONTROL_GET_MAX) {
837                 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
838                                      chain->dev->intfnum, ctrl->info.selector,
839                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
840                                      ctrl->info.size);
841                 if (ret < 0)
842                         return ret;
843         }
844         if (ctrl->info.flags & UVC_CONTROL_GET_RES) {
845                 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
846                                      chain->dev->intfnum, ctrl->info.selector,
847                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
848                                      ctrl->info.size);
849                 if (ret < 0)
850                         return ret;
851         }
852
853         ctrl->cached = 1;
854         return 0;
855 }
856
857 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
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;
864         int ret;
865
866         ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
867         if (ctrl == NULL)
868                 return -EINVAL;
869
870         memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
871         v4l2_ctrl->id = mapping->id;
872         v4l2_ctrl->type = mapping->v4l2_type;
873         strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
874         v4l2_ctrl->flags = 0;
875
876         if (!(ctrl->info.flags & UVC_CONTROL_GET_CUR))
877                 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
878         if (!(ctrl->info.flags & UVC_CONTROL_SET_CUR))
879                 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
880
881         if (!ctrl->cached) {
882                 ret = uvc_ctrl_populate_cache(chain, ctrl);
883                 if (ret < 0)
884                         return ret;
885         }
886
887         if (ctrl->info.flags & UVC_CONTROL_GET_DEF) {
888                 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
889                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
890         }
891
892         switch (mapping->v4l2_type) {
893         case V4L2_CTRL_TYPE_MENU:
894                 v4l2_ctrl->minimum = 0;
895                 v4l2_ctrl->maximum = mapping->menu_count - 1;
896                 v4l2_ctrl->step = 1;
897
898                 menu = mapping->menu_info;
899                 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
900                         if (menu->value == v4l2_ctrl->default_value) {
901                                 v4l2_ctrl->default_value = i;
902                                 break;
903                         }
904                 }
905
906                 return 0;
907
908         case V4L2_CTRL_TYPE_BOOLEAN:
909                 v4l2_ctrl->minimum = 0;
910                 v4l2_ctrl->maximum = 1;
911                 v4l2_ctrl->step = 1;
912                 return 0;
913
914         case V4L2_CTRL_TYPE_BUTTON:
915                 v4l2_ctrl->minimum = 0;
916                 v4l2_ctrl->maximum = 0;
917                 v4l2_ctrl->step = 0;
918                 return 0;
919
920         default:
921                 break;
922         }
923
924         if (ctrl->info.flags & UVC_CONTROL_GET_MIN)
925                 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
926                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
927
928         if (ctrl->info.flags & UVC_CONTROL_GET_MAX)
929                 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
930                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
931
932         if (ctrl->info.flags & UVC_CONTROL_GET_RES)
933                 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
934                                   uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
935
936         return 0;
937 }
938
939
940 /* --------------------------------------------------------------------------
941  * Control transactions
942  *
943  * To make extended set operations as atomic as the hardware allows, controls
944  * are handled using begin/commit/rollback operations.
945  *
946  * At the beginning of a set request, uvc_ctrl_begin should be called to
947  * initialize the request. This function acquires the control lock.
948  *
949  * When setting a control, the new value is stored in the control data field
950  * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
951  * later processing. If the UVC and V4L2 control sizes differ, the current
952  * value is loaded from the hardware before storing the new value in the data
953  * field.
954  *
955  * After processing all controls in the transaction, uvc_ctrl_commit or
956  * uvc_ctrl_rollback must be called to apply the pending changes to the
957  * hardware or revert them. When applying changes, all controls marked as
958  * dirty will be modified in the UVC device, and the dirty flag will be
959  * cleared. When reverting controls, the control data field
960  * UVC_CTRL_DATA_CURRENT is reverted to its previous value
961  * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
962  * control lock.
963  */
964 int uvc_ctrl_begin(struct uvc_video_chain *chain)
965 {
966         return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
967 }
968
969 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
970         struct uvc_entity *entity, int rollback)
971 {
972         struct uvc_control *ctrl;
973         unsigned int i;
974         int ret;
975
976         if (entity == NULL)
977                 return 0;
978
979         for (i = 0; i < entity->ncontrols; ++i) {
980                 ctrl = &entity->controls[i];
981                 if (!ctrl->initialized)
982                         continue;
983
984                 /* Reset the loaded flag for auto-update controls that were
985                  * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
986                  * uvc_ctrl_get from using the cached value.
987                  */
988                 if (ctrl->info.flags & UVC_CONTROL_AUTO_UPDATE)
989                         ctrl->loaded = 0;
990
991                 if (!ctrl->dirty)
992                         continue;
993
994                 if (!rollback)
995                         ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
996                                 dev->intfnum, ctrl->info.selector,
997                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
998                                 ctrl->info.size);
999                 else
1000                         ret = 0;
1001
1002                 if (rollback || ret < 0)
1003                         memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1004                                uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1005                                ctrl->info.size);
1006
1007                 ctrl->dirty = 0;
1008
1009                 if (ret < 0)
1010                         return ret;
1011         }
1012
1013         return 0;
1014 }
1015
1016 int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
1017 {
1018         struct uvc_entity *entity;
1019         int ret = 0;
1020
1021         /* Find the control. */
1022         list_for_each_entry(entity, &chain->entities, chain) {
1023                 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1024                 if (ret < 0)
1025                         goto done;
1026         }
1027
1028 done:
1029         mutex_unlock(&chain->ctrl_mutex);
1030         return ret;
1031 }
1032
1033 int uvc_ctrl_get(struct uvc_video_chain *chain,
1034         struct v4l2_ext_control *xctrl)
1035 {
1036         struct uvc_control *ctrl;
1037         struct uvc_control_mapping *mapping;
1038         struct uvc_menu_info *menu;
1039         unsigned int i;
1040         int ret;
1041
1042         ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1043         if (ctrl == NULL || (ctrl->info.flags & UVC_CONTROL_GET_CUR) == 0)
1044                 return -EINVAL;
1045
1046         if (!ctrl->loaded) {
1047                 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
1048                                 chain->dev->intfnum, ctrl->info.selector,
1049                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1050                                 ctrl->info.size);
1051                 if (ret < 0)
1052                         return ret;
1053
1054                 ctrl->loaded = 1;
1055         }
1056
1057         xctrl->value = mapping->get(mapping, UVC_GET_CUR,
1058                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1059
1060         if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1061                 menu = mapping->menu_info;
1062                 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1063                         if (menu->value == xctrl->value) {
1064                                 xctrl->value = i;
1065                                 break;
1066                         }
1067                 }
1068         }
1069
1070         return 0;
1071 }
1072
1073 int uvc_ctrl_set(struct uvc_video_chain *chain,
1074         struct v4l2_ext_control *xctrl)
1075 {
1076         struct uvc_control *ctrl;
1077         struct uvc_control_mapping *mapping;
1078         s32 value;
1079         u32 step;
1080         s32 min;
1081         s32 max;
1082         int ret;
1083
1084         ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1085         if (ctrl == NULL || (ctrl->info.flags & UVC_CONTROL_SET_CUR) == 0)
1086                 return -EINVAL;
1087
1088         /* Clamp out of range values. */
1089         switch (mapping->v4l2_type) {
1090         case V4L2_CTRL_TYPE_INTEGER:
1091                 if (!ctrl->cached) {
1092                         ret = uvc_ctrl_populate_cache(chain, ctrl);
1093                         if (ret < 0)
1094                                 return ret;
1095                 }
1096
1097                 min = mapping->get(mapping, UVC_GET_MIN,
1098                                    uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1099                 max = mapping->get(mapping, UVC_GET_MAX,
1100                                    uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1101                 step = mapping->get(mapping, UVC_GET_RES,
1102                                     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1103                 if (step == 0)
1104                         step = 1;
1105
1106                 xctrl->value = min + (xctrl->value - min + step/2) / step * step;
1107                 xctrl->value = clamp(xctrl->value, min, max);
1108                 value = xctrl->value;
1109                 break;
1110
1111         case V4L2_CTRL_TYPE_BOOLEAN:
1112                 xctrl->value = clamp(xctrl->value, 0, 1);
1113                 value = xctrl->value;
1114                 break;
1115
1116         case V4L2_CTRL_TYPE_MENU:
1117                 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1118                         return -ERANGE;
1119                 value = mapping->menu_info[xctrl->value].value;
1120                 break;
1121
1122         default:
1123                 value = xctrl->value;
1124                 break;
1125         }
1126
1127         /* If the mapping doesn't span the whole UVC control, the current value
1128          * needs to be loaded from the device to perform the read-modify-write
1129          * operation.
1130          */
1131         if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1132                 if ((ctrl->info.flags & UVC_CONTROL_GET_CUR) == 0) {
1133                         memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1134                                 0, ctrl->info.size);
1135                 } else {
1136                         ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1137                                 ctrl->entity->id, chain->dev->intfnum,
1138                                 ctrl->info.selector,
1139                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1140                                 ctrl->info.size);
1141                         if (ret < 0)
1142                                 return ret;
1143                 }
1144
1145                 ctrl->loaded = 1;
1146         }
1147
1148         /* Backup the current value in case we need to rollback later. */
1149         if (!ctrl->dirty) {
1150                 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1151                        uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1152                        ctrl->info.size);
1153         }
1154
1155         mapping->set(mapping, value,
1156                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1157
1158         ctrl->dirty = 1;
1159         ctrl->modified = 1;
1160         return 0;
1161 }
1162
1163 /* --------------------------------------------------------------------------
1164  * Dynamic controls
1165  */
1166
1167 /*
1168  * Query control information (size and flags) for XU controls.
1169  */
1170 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
1171         const struct uvc_control *ctrl, struct uvc_control_info *info)
1172 {
1173         u8 *data;
1174         int ret;
1175
1176         data = kmalloc(2, GFP_KERNEL);
1177         if (data == NULL)
1178                 return -ENOMEM;
1179
1180         memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
1181                sizeof(info->entity));
1182         info->index = ctrl->index;
1183         info->selector = ctrl->index + 1;
1184
1185         /* Query and verify the control length (GET_LEN) */
1186         ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
1187                              info->selector, data, 2);
1188         if (ret < 0) {
1189                 uvc_trace(UVC_TRACE_CONTROL,
1190                           "GET_LEN failed on control %pUl/%u (%d).\n",
1191                            info->entity, info->selector, ret);
1192                 goto done;
1193         }
1194
1195         info->size = le16_to_cpup((__le16 *)data);
1196
1197         /* Query the control information (GET_INFO) */
1198         ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
1199                              info->selector, data, 1);
1200         if (ret < 0) {
1201                 uvc_trace(UVC_TRACE_CONTROL,
1202                           "GET_INFO failed on control %pUl/%u (%d).\n",
1203                           info->entity, info->selector, ret);
1204                 goto done;
1205         }
1206
1207         info->flags = UVC_CONTROL_GET_MIN | UVC_CONTROL_GET_MAX
1208                     | UVC_CONTROL_GET_RES | UVC_CONTROL_GET_DEF
1209                     | (data[0] & UVC_CONTROL_CAP_GET ? UVC_CONTROL_GET_CUR : 0)
1210                     | (data[0] & UVC_CONTROL_CAP_SET ? UVC_CONTROL_SET_CUR : 0)
1211                     | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1212                        UVC_CONTROL_AUTO_UPDATE : 0);
1213
1214         uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
1215                   "flags { get %u set %u auto %u }.\n",
1216                   info->entity, info->selector, info->size,
1217                   (info->flags & UVC_CONTROL_GET_CUR) ? 1 : 0,
1218                   (info->flags & UVC_CONTROL_SET_CUR) ? 1 : 0,
1219                   (info->flags & UVC_CONTROL_AUTO_UPDATE) ? 1 : 0);
1220
1221 done:
1222         kfree(data);
1223         return ret;
1224 }
1225
1226 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1227         const struct uvc_control_info *info);
1228
1229 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
1230         struct uvc_control *ctrl)
1231 {
1232         struct uvc_control_info info;
1233         int ret;
1234
1235         if (ctrl->initialized)
1236                 return 0;
1237
1238         ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
1239         if (ret < 0)
1240                 return ret;
1241
1242         ret = uvc_ctrl_add_info(dev, ctrl, &info);
1243         if (ret < 0)
1244                 uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
1245                           "%pUl/%u on device %s entity %u\n", info.entity,
1246                           info.selector, dev->udev->devpath, ctrl->entity->id);
1247
1248         return ret;
1249 }
1250
1251 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1252         struct uvc_xu_control *xctrl, int set)
1253 {
1254         struct uvc_entity *entity;
1255         struct uvc_control *ctrl = NULL;
1256         unsigned int i, found = 0;
1257         __u8 *data;
1258         int ret;
1259
1260         /* Find the extension unit. */
1261         list_for_each_entry(entity, &chain->entities, chain) {
1262                 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1263                     entity->id == xctrl->unit)
1264                         break;
1265         }
1266
1267         if (entity->id != xctrl->unit) {
1268                 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1269                         xctrl->unit);
1270                 return -EINVAL;
1271         }
1272
1273         /* Find the control and perform delayed initialization if needed. */
1274         for (i = 0; i < entity->ncontrols; ++i) {
1275                 ctrl = &entity->controls[i];
1276                 if (ctrl->index == xctrl->selector - 1) {
1277                         found = 1;
1278                         break;
1279                 }
1280         }
1281
1282         if (!found) {
1283                 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1284                         entity->extension.guidExtensionCode, xctrl->selector);
1285                 return -EINVAL;
1286         }
1287
1288         ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1289         if (ret < 0)
1290                 return -ENOENT;
1291
1292         /* Validate control data size. */
1293         if (ctrl->info.size != xctrl->size)
1294                 return -EINVAL;
1295
1296         if ((set && !(ctrl->info.flags & UVC_CONTROL_SET_CUR)) ||
1297             (!set && !(ctrl->info.flags & UVC_CONTROL_GET_CUR)))
1298                 return -EINVAL;
1299
1300         if (mutex_lock_interruptible(&chain->ctrl_mutex))
1301                 return -ERESTARTSYS;
1302
1303         memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1304                uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1305                xctrl->size);
1306         data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1307
1308         if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1309                 ret = -EFAULT;
1310                 goto out;
1311         }
1312
1313         ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR,
1314                              xctrl->unit, chain->dev->intfnum, xctrl->selector,
1315                              data, xctrl->size);
1316         if (ret < 0)
1317                 goto out;
1318
1319         if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1320                 ret = -EFAULT;
1321                 goto out;
1322         }
1323
1324 out:
1325         if (ret)
1326                 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1327                        uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1328                        xctrl->size);
1329
1330         mutex_unlock(&chain->ctrl_mutex);
1331         return ret;
1332 }
1333
1334 /* --------------------------------------------------------------------------
1335  * Suspend/resume
1336  */
1337
1338 /*
1339  * Restore control values after resume, skipping controls that haven't been
1340  * changed.
1341  *
1342  * TODO
1343  * - Don't restore modified controls that are back to their default value.
1344  * - Handle restore order (Auto-Exposure Mode should be restored before
1345  *   Exposure Time).
1346  */
1347 int uvc_ctrl_resume_device(struct uvc_device *dev)
1348 {
1349         struct uvc_control *ctrl;
1350         struct uvc_entity *entity;
1351         unsigned int i;
1352         int ret;
1353
1354         /* Walk the entities list and restore controls when possible. */
1355         list_for_each_entry(entity, &dev->entities, list) {
1356
1357                 for (i = 0; i < entity->ncontrols; ++i) {
1358                         ctrl = &entity->controls[i];
1359
1360                         if (!ctrl->initialized || !ctrl->modified ||
1361                             (ctrl->info.flags & UVC_CONTROL_RESTORE) == 0)
1362                                 continue;
1363
1364                         printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1365                                 ctrl->info.entity, ctrl->info.index,
1366                                 ctrl->info.selector);
1367                         ctrl->dirty = 1;
1368                 }
1369
1370                 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1371                 if (ret < 0)
1372                         return ret;
1373         }
1374
1375         return 0;
1376 }
1377
1378 /* --------------------------------------------------------------------------
1379  * Control and mapping handling
1380  */
1381
1382 /*
1383  * Add control information to a given control.
1384  */
1385 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1386         const struct uvc_control_info *info)
1387 {
1388         int ret = 0;
1389
1390         memcpy(&ctrl->info, info, sizeof(*info));
1391         INIT_LIST_HEAD(&ctrl->info.mappings);
1392
1393         /* Allocate an array to save control values (cur, def, max, etc.) */
1394         ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
1395                                  GFP_KERNEL);
1396         if (ctrl->uvc_data == NULL) {
1397                 ret = -ENOMEM;
1398                 goto done;
1399         }
1400
1401         ctrl->initialized = 1;
1402
1403         uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1404                 "entity %u\n", ctrl->info.entity, ctrl->info.selector,
1405                 dev->udev->devpath, ctrl->entity->id);
1406
1407 done:
1408         if (ret < 0)
1409                 kfree(ctrl->uvc_data);
1410         return ret;
1411 }
1412
1413 /*
1414  * Add a control mapping to a given control.
1415  */
1416 static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
1417         struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
1418 {
1419         struct uvc_control_mapping *map;
1420         unsigned int size;
1421
1422         /* Most mappings come from static kernel data and need to be duplicated.
1423          * Mappings that come from userspace will be unnecessarily duplicated,
1424          * this could be optimized.
1425          */
1426         map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
1427         if (map == NULL)
1428                 return -ENOMEM;
1429
1430         size = sizeof(*mapping->menu_info) * mapping->menu_count;
1431         map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
1432         if (map->menu_info == NULL) {
1433                 kfree(map);
1434                 return -ENOMEM;
1435         }
1436
1437         if (map->get == NULL)
1438                 map->get = uvc_get_le_value;
1439         if (map->set == NULL)
1440                 map->set = uvc_set_le_value;
1441
1442         map->ctrl = &ctrl->info;
1443         list_add_tail(&map->list, &ctrl->info.mappings);
1444         uvc_trace(UVC_TRACE_CONTROL,
1445                 "Adding mapping '%s' to control %pUl/%u.\n",
1446                 map->name, ctrl->info.entity, ctrl->info.selector);
1447
1448         return 0;
1449 }
1450
1451 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
1452         const struct uvc_control_mapping *mapping)
1453 {
1454         struct uvc_device *dev = chain->dev;
1455         struct uvc_control_mapping *map;
1456         struct uvc_entity *entity;
1457         struct uvc_control *ctrl;
1458         int found = 0;
1459         int ret;
1460
1461         if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1462                 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
1463                         "id 0x%08x is invalid.\n", mapping->name,
1464                         mapping->id);
1465                 return -EINVAL;
1466         }
1467
1468         /* Search for the matching (GUID/CS) control in the given device */
1469         list_for_each_entry(entity, &dev->entities, list) {
1470                 unsigned int i;
1471
1472                 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
1473                     !uvc_entity_match_guid(entity, mapping->entity))
1474                         continue;
1475
1476                 for (i = 0; i < entity->ncontrols; ++i) {
1477                         ctrl = &entity->controls[i];
1478                         if (ctrl->index == mapping->selector - 1) {
1479                                 found = 1;
1480                                 break;
1481                         }
1482                 }
1483
1484                 if (found)
1485                         break;
1486         }
1487         if (!found)
1488                 return -ENOENT;
1489
1490         if (mutex_lock_interruptible(&chain->ctrl_mutex))
1491                 return -ERESTARTSYS;
1492
1493         /* Perform delayed initialization of XU controls */
1494         ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
1495         if (ret < 0) {
1496                 ret = -ENOENT;
1497                 goto done;
1498         }
1499
1500         list_for_each_entry(map, &ctrl->info.mappings, list) {
1501                 if (mapping->id == map->id) {
1502                         uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
1503                                 "control id 0x%08x already exists.\n",
1504                                 mapping->name, mapping->id);
1505                         ret = -EEXIST;
1506                         goto done;
1507                 }
1508         }
1509
1510         /* Prevent excess memory consumption */
1511         if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
1512                 atomic_dec(&dev->nmappings);
1513                 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
1514                         "mappings count (%u) exceeded.\n", mapping->name,
1515                         UVC_MAX_CONTROL_MAPPINGS);
1516                 ret = -ENOMEM;
1517                 goto done;
1518         }
1519
1520         ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
1521         if (ret < 0)
1522                 atomic_dec(&dev->nmappings);
1523
1524 done:
1525         mutex_unlock(&chain->ctrl_mutex);
1526         return ret;
1527 }
1528
1529 /*
1530  * Prune an entity of its bogus controls using a blacklist. Bogus controls
1531  * are currently the ones that crash the camera or unconditionally return an
1532  * error when queried.
1533  */
1534 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
1535         struct uvc_entity *entity)
1536 {
1537         struct uvc_ctrl_blacklist {
1538                 struct usb_device_id id;
1539                 u8 index;
1540         };
1541
1542         static const struct uvc_ctrl_blacklist processing_blacklist[] = {
1543                 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
1544                 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1545                 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1546         };
1547         static const struct uvc_ctrl_blacklist camera_blacklist[] = {
1548                 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
1549         };
1550
1551         const struct uvc_ctrl_blacklist *blacklist;
1552         unsigned int size;
1553         unsigned int count;
1554         unsigned int i;
1555         u8 *controls;
1556
1557         switch (UVC_ENTITY_TYPE(entity)) {
1558         case UVC_VC_PROCESSING_UNIT:
1559                 blacklist = processing_blacklist;
1560                 count = ARRAY_SIZE(processing_blacklist);
1561                 controls = entity->processing.bmControls;
1562                 size = entity->processing.bControlSize;
1563                 break;
1564
1565         case UVC_ITT_CAMERA:
1566                 blacklist = camera_blacklist;
1567                 count = ARRAY_SIZE(camera_blacklist);
1568                 controls = entity->camera.bmControls;
1569                 size = entity->camera.bControlSize;
1570                 break;
1571
1572         default:
1573                 return;
1574         }
1575
1576         for (i = 0; i < count; ++i) {
1577                 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
1578                         continue;
1579
1580                 if (blacklist[i].index >= 8 * size ||
1581                     !uvc_test_bit(controls, blacklist[i].index))
1582                         continue;
1583
1584                 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
1585                         "removing it.\n", entity->id, blacklist[i].index);
1586
1587                 uvc_clear_bit(controls, blacklist[i].index);
1588         }
1589 }
1590
1591 /*
1592  * Add control information and hardcoded stock control mappings to the given
1593  * device.
1594  */
1595 static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
1596 {
1597         const struct uvc_control_info *info = uvc_ctrls;
1598         const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
1599         const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1600         const struct uvc_control_mapping *mend =
1601                 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1602
1603         /* XU controls initialization requires querying the device for control
1604          * information. As some buggy UVC devices will crash when queried
1605          * repeatedly in a tight loop, delay XU controls initialization until
1606          * first use.
1607          */
1608         if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
1609                 return;
1610
1611         for (; info < iend; ++info) {
1612                 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
1613                     ctrl->index == info->index) {
1614                         uvc_ctrl_add_info(dev, ctrl, info);
1615                         break;
1616                  }
1617         }
1618
1619         if (!ctrl->initialized)
1620                 return;
1621
1622         for (; mapping < mend; ++mapping) {
1623                 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
1624                     ctrl->info.selector == mapping->selector)
1625                         __uvc_ctrl_add_mapping(dev, ctrl, mapping);
1626         }
1627 }
1628
1629 /*
1630  * Initialize device controls.
1631  */
1632 int uvc_ctrl_init_device(struct uvc_device *dev)
1633 {
1634         struct uvc_entity *entity;
1635         unsigned int i;
1636
1637         /* Walk the entities list and instantiate controls */
1638         list_for_each_entry(entity, &dev->entities, list) {
1639                 struct uvc_control *ctrl;
1640                 unsigned int bControlSize = 0, ncontrols = 0;
1641                 __u8 *bmControls = NULL;
1642
1643                 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1644                         bmControls = entity->extension.bmControls;
1645                         bControlSize = entity->extension.bControlSize;
1646                 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
1647                         bmControls = entity->processing.bmControls;
1648                         bControlSize = entity->processing.bControlSize;
1649                 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
1650                         bmControls = entity->camera.bmControls;
1651                         bControlSize = entity->camera.bControlSize;
1652                 }
1653
1654                 /* Remove bogus/blacklisted controls */
1655                 uvc_ctrl_prune_entity(dev, entity);
1656
1657                 /* Count supported controls and allocate the controls array */
1658                 for (i = 0; i < bControlSize; ++i)
1659                         ncontrols += hweight8(bmControls[i]);
1660                 if (ncontrols == 0)
1661                         continue;
1662
1663                 entity->controls = kzalloc(ncontrols * sizeof(*ctrl),
1664                                            GFP_KERNEL);
1665                 if (entity->controls == NULL)
1666                         return -ENOMEM;
1667                 entity->ncontrols = ncontrols;
1668
1669                 /* Initialize all supported controls */
1670                 ctrl = entity->controls;
1671                 for (i = 0; i < bControlSize * 8; ++i) {
1672                         if (uvc_test_bit(bmControls, i) == 0)
1673                                 continue;
1674
1675                         ctrl->entity = entity;
1676                         ctrl->index = i;
1677
1678                         uvc_ctrl_init_ctrl(dev, ctrl);
1679                         ctrl++;
1680                 }
1681         }
1682
1683         return 0;
1684 }
1685
1686 /*
1687  * Cleanup device controls.
1688  */
1689 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
1690         struct uvc_control *ctrl)
1691 {
1692         struct uvc_control_mapping *mapping, *nm;
1693
1694         list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
1695                 list_del(&mapping->list);
1696                 kfree(mapping->menu_info);
1697                 kfree(mapping);
1698         }
1699 }
1700
1701 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1702 {
1703         struct uvc_entity *entity;
1704         unsigned int i;
1705
1706         /* Free controls and control mappings for all entities. */
1707         list_for_each_entry(entity, &dev->entities, list) {
1708                 for (i = 0; i < entity->ncontrols; ++i) {
1709                         struct uvc_control *ctrl = &entity->controls[i];
1710
1711                         if (!ctrl->initialized)
1712                                 continue;
1713
1714                         uvc_ctrl_cleanup_mappings(dev, ctrl);
1715                         kfree(ctrl->uvc_data);
1716                 }
1717
1718                 kfree(entity->controls);
1719         }
1720 }