Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[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 <linux/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_CTRL_FLAG_SET_CUR
46                                 | UVC_CTRL_FLAG_GET_RANGE
47                                 | UVC_CTRL_FLAG_RESTORE,
48         },
49         {
50                 .entity         = UVC_GUID_UVC_PROCESSING,
51                 .selector       = UVC_PU_CONTRAST_CONTROL,
52                 .index          = 1,
53                 .size           = 2,
54                 .flags          = UVC_CTRL_FLAG_SET_CUR
55                                 | UVC_CTRL_FLAG_GET_RANGE
56                                 | UVC_CTRL_FLAG_RESTORE,
57         },
58         {
59                 .entity         = UVC_GUID_UVC_PROCESSING,
60                 .selector       = UVC_PU_HUE_CONTROL,
61                 .index          = 2,
62                 .size           = 2,
63                 .flags          = UVC_CTRL_FLAG_SET_CUR
64                                 | UVC_CTRL_FLAG_GET_RANGE
65                                 | UVC_CTRL_FLAG_RESTORE
66                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
67         },
68         {
69                 .entity         = UVC_GUID_UVC_PROCESSING,
70                 .selector       = UVC_PU_SATURATION_CONTROL,
71                 .index          = 3,
72                 .size           = 2,
73                 .flags          = UVC_CTRL_FLAG_SET_CUR
74                                 | UVC_CTRL_FLAG_GET_RANGE
75                                 | UVC_CTRL_FLAG_RESTORE,
76         },
77         {
78                 .entity         = UVC_GUID_UVC_PROCESSING,
79                 .selector       = UVC_PU_SHARPNESS_CONTROL,
80                 .index          = 4,
81                 .size           = 2,
82                 .flags          = UVC_CTRL_FLAG_SET_CUR
83                                 | UVC_CTRL_FLAG_GET_RANGE
84                                 | UVC_CTRL_FLAG_RESTORE,
85         },
86         {
87                 .entity         = UVC_GUID_UVC_PROCESSING,
88                 .selector       = UVC_PU_GAMMA_CONTROL,
89                 .index          = 5,
90                 .size           = 2,
91                 .flags          = UVC_CTRL_FLAG_SET_CUR
92                                 | UVC_CTRL_FLAG_GET_RANGE
93                                 | UVC_CTRL_FLAG_RESTORE,
94         },
95         {
96                 .entity         = UVC_GUID_UVC_PROCESSING,
97                 .selector       = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
98                 .index          = 6,
99                 .size           = 2,
100                 .flags          = UVC_CTRL_FLAG_SET_CUR
101                                 | UVC_CTRL_FLAG_GET_RANGE
102                                 | UVC_CTRL_FLAG_RESTORE
103                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
104         },
105         {
106                 .entity         = UVC_GUID_UVC_PROCESSING,
107                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
108                 .index          = 7,
109                 .size           = 4,
110                 .flags          = UVC_CTRL_FLAG_SET_CUR
111                                 | UVC_CTRL_FLAG_GET_RANGE
112                                 | UVC_CTRL_FLAG_RESTORE
113                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
114         },
115         {
116                 .entity         = UVC_GUID_UVC_PROCESSING,
117                 .selector       = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
118                 .index          = 8,
119                 .size           = 2,
120                 .flags          = UVC_CTRL_FLAG_SET_CUR
121                                 | UVC_CTRL_FLAG_GET_RANGE
122                                 | UVC_CTRL_FLAG_RESTORE,
123         },
124         {
125                 .entity         = UVC_GUID_UVC_PROCESSING,
126                 .selector       = UVC_PU_GAIN_CONTROL,
127                 .index          = 9,
128                 .size           = 2,
129                 .flags          = UVC_CTRL_FLAG_SET_CUR
130                                 | UVC_CTRL_FLAG_GET_RANGE
131                                 | UVC_CTRL_FLAG_RESTORE,
132         },
133         {
134                 .entity         = UVC_GUID_UVC_PROCESSING,
135                 .selector       = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
136                 .index          = 10,
137                 .size           = 1,
138                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
139                                 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
140         },
141         {
142                 .entity         = UVC_GUID_UVC_PROCESSING,
143                 .selector       = UVC_PU_HUE_AUTO_CONTROL,
144                 .index          = 11,
145                 .size           = 1,
146                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
147                                 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
148         },
149         {
150                 .entity         = UVC_GUID_UVC_PROCESSING,
151                 .selector       = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
152                 .index          = 12,
153                 .size           = 1,
154                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
155                                 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
156         },
157         {
158                 .entity         = UVC_GUID_UVC_PROCESSING,
159                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
160                 .index          = 13,
161                 .size           = 1,
162                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
163                                 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
164         },
165         {
166                 .entity         = UVC_GUID_UVC_PROCESSING,
167                 .selector       = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
168                 .index          = 14,
169                 .size           = 2,
170                 .flags          = UVC_CTRL_FLAG_SET_CUR
171                                 | UVC_CTRL_FLAG_GET_RANGE
172                                 | UVC_CTRL_FLAG_RESTORE,
173         },
174         {
175                 .entity         = UVC_GUID_UVC_PROCESSING,
176                 .selector       = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
177                 .index          = 15,
178                 .size           = 2,
179                 .flags          = UVC_CTRL_FLAG_SET_CUR
180                                 | UVC_CTRL_FLAG_GET_RANGE
181                                 | UVC_CTRL_FLAG_RESTORE,
182         },
183         {
184                 .entity         = UVC_GUID_UVC_PROCESSING,
185                 .selector       = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
186                 .index          = 16,
187                 .size           = 1,
188                 .flags          = UVC_CTRL_FLAG_GET_CUR,
189         },
190         {
191                 .entity         = UVC_GUID_UVC_PROCESSING,
192                 .selector       = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
193                 .index          = 17,
194                 .size           = 1,
195                 .flags          = UVC_CTRL_FLAG_GET_CUR,
196         },
197         {
198                 .entity         = UVC_GUID_UVC_CAMERA,
199                 .selector       = UVC_CT_SCANNING_MODE_CONTROL,
200                 .index          = 0,
201                 .size           = 1,
202                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
203                                 | UVC_CTRL_FLAG_RESTORE,
204         },
205         {
206                 .entity         = UVC_GUID_UVC_CAMERA,
207                 .selector       = UVC_CT_AE_MODE_CONTROL,
208                 .index          = 1,
209                 .size           = 1,
210                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
211                                 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
212                                 | UVC_CTRL_FLAG_RESTORE,
213         },
214         {
215                 .entity         = UVC_GUID_UVC_CAMERA,
216                 .selector       = UVC_CT_AE_PRIORITY_CONTROL,
217                 .index          = 2,
218                 .size           = 1,
219                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
220                                 | UVC_CTRL_FLAG_RESTORE,
221         },
222         {
223                 .entity         = UVC_GUID_UVC_CAMERA,
224                 .selector       = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
225                 .index          = 3,
226                 .size           = 4,
227                 .flags          = UVC_CTRL_FLAG_SET_CUR
228                                 | UVC_CTRL_FLAG_GET_RANGE
229                                 | UVC_CTRL_FLAG_RESTORE,
230         },
231         {
232                 .entity         = UVC_GUID_UVC_CAMERA,
233                 .selector       = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
234                 .index          = 4,
235                 .size           = 1,
236                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
237         },
238         {
239                 .entity         = UVC_GUID_UVC_CAMERA,
240                 .selector       = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
241                 .index          = 5,
242                 .size           = 2,
243                 .flags          = UVC_CTRL_FLAG_SET_CUR
244                                 | UVC_CTRL_FLAG_GET_RANGE
245                                 | UVC_CTRL_FLAG_RESTORE
246                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
247         },
248         {
249                 .entity         = UVC_GUID_UVC_CAMERA,
250                 .selector       = UVC_CT_FOCUS_RELATIVE_CONTROL,
251                 .index          = 6,
252                 .size           = 2,
253                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
254                                 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
255                                 | UVC_CTRL_FLAG_GET_DEF
256                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
257         },
258         {
259                 .entity         = UVC_GUID_UVC_CAMERA,
260                 .selector       = UVC_CT_IRIS_ABSOLUTE_CONTROL,
261                 .index          = 7,
262                 .size           = 2,
263                 .flags          = UVC_CTRL_FLAG_SET_CUR
264                                 | UVC_CTRL_FLAG_GET_RANGE
265                                 | UVC_CTRL_FLAG_RESTORE
266                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
267         },
268         {
269                 .entity         = UVC_GUID_UVC_CAMERA,
270                 .selector       = UVC_CT_IRIS_RELATIVE_CONTROL,
271                 .index          = 8,
272                 .size           = 1,
273                 .flags          = UVC_CTRL_FLAG_SET_CUR
274                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
275         },
276         {
277                 .entity         = UVC_GUID_UVC_CAMERA,
278                 .selector       = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
279                 .index          = 9,
280                 .size           = 2,
281                 .flags          = UVC_CTRL_FLAG_SET_CUR
282                                 | UVC_CTRL_FLAG_GET_RANGE
283                                 | UVC_CTRL_FLAG_RESTORE
284                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
285         },
286         {
287                 .entity         = UVC_GUID_UVC_CAMERA,
288                 .selector       = UVC_CT_ZOOM_RELATIVE_CONTROL,
289                 .index          = 10,
290                 .size           = 3,
291                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
292                                 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
293                                 | UVC_CTRL_FLAG_GET_DEF
294                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
295         },
296         {
297                 .entity         = UVC_GUID_UVC_CAMERA,
298                 .selector       = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
299                 .index          = 11,
300                 .size           = 8,
301                 .flags          = UVC_CTRL_FLAG_SET_CUR
302                                 | UVC_CTRL_FLAG_GET_RANGE
303                                 | UVC_CTRL_FLAG_RESTORE
304                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
305         },
306         {
307                 .entity         = UVC_GUID_UVC_CAMERA,
308                 .selector       = UVC_CT_PANTILT_RELATIVE_CONTROL,
309                 .index          = 12,
310                 .size           = 4,
311                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
312                                 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
313                                 | UVC_CTRL_FLAG_GET_DEF
314                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
315         },
316         {
317                 .entity         = UVC_GUID_UVC_CAMERA,
318                 .selector       = UVC_CT_ROLL_ABSOLUTE_CONTROL,
319                 .index          = 13,
320                 .size           = 2,
321                 .flags          = UVC_CTRL_FLAG_SET_CUR
322                                 | UVC_CTRL_FLAG_GET_RANGE
323                                 | UVC_CTRL_FLAG_RESTORE
324                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
325         },
326         {
327                 .entity         = UVC_GUID_UVC_CAMERA,
328                 .selector       = UVC_CT_ROLL_RELATIVE_CONTROL,
329                 .index          = 14,
330                 .size           = 2,
331                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
332                                 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
333                                 | UVC_CTRL_FLAG_GET_DEF
334                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
335         },
336         {
337                 .entity         = UVC_GUID_UVC_CAMERA,
338                 .selector       = UVC_CT_FOCUS_AUTO_CONTROL,
339                 .index          = 17,
340                 .size           = 1,
341                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
342                                 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
343         },
344         {
345                 .entity         = UVC_GUID_UVC_CAMERA,
346                 .selector       = UVC_CT_PRIVACY_CONTROL,
347                 .index          = 18,
348                 .size           = 1,
349                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
350                                 | UVC_CTRL_FLAG_RESTORE
351                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
352         },
353 };
354
355 static struct uvc_menu_info power_line_frequency_controls[] = {
356         { 0, "Disabled" },
357         { 1, "50 Hz" },
358         { 2, "60 Hz" },
359 };
360
361 static struct uvc_menu_info exposure_auto_controls[] = {
362         { 2, "Auto Mode" },
363         { 1, "Manual Mode" },
364         { 4, "Shutter Priority Mode" },
365         { 8, "Aperture Priority Mode" },
366 };
367
368 static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
369         __u8 query, const __u8 *data)
370 {
371         __s8 zoom = (__s8)data[0];
372
373         switch (query) {
374         case UVC_GET_CUR:
375                 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
376
377         case UVC_GET_MIN:
378         case UVC_GET_MAX:
379         case UVC_GET_RES:
380         case UVC_GET_DEF:
381         default:
382                 return data[2];
383         }
384 }
385
386 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
387         __s32 value, __u8 *data)
388 {
389         data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
390         data[2] = min((int)abs(value), 0xff);
391 }
392
393 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
394         {
395                 .id             = V4L2_CID_BRIGHTNESS,
396                 .name           = "Brightness",
397                 .entity         = UVC_GUID_UVC_PROCESSING,
398                 .selector       = UVC_PU_BRIGHTNESS_CONTROL,
399                 .size           = 16,
400                 .offset         = 0,
401                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
402                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
403         },
404         {
405                 .id             = V4L2_CID_CONTRAST,
406                 .name           = "Contrast",
407                 .entity         = UVC_GUID_UVC_PROCESSING,
408                 .selector       = UVC_PU_CONTRAST_CONTROL,
409                 .size           = 16,
410                 .offset         = 0,
411                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
412                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
413         },
414         {
415                 .id             = V4L2_CID_HUE,
416                 .name           = "Hue",
417                 .entity         = UVC_GUID_UVC_PROCESSING,
418                 .selector       = UVC_PU_HUE_CONTROL,
419                 .size           = 16,
420                 .offset         = 0,
421                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
422                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
423         },
424         {
425                 .id             = V4L2_CID_SATURATION,
426                 .name           = "Saturation",
427                 .entity         = UVC_GUID_UVC_PROCESSING,
428                 .selector       = UVC_PU_SATURATION_CONTROL,
429                 .size           = 16,
430                 .offset         = 0,
431                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
432                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
433         },
434         {
435                 .id             = V4L2_CID_SHARPNESS,
436                 .name           = "Sharpness",
437                 .entity         = UVC_GUID_UVC_PROCESSING,
438                 .selector       = UVC_PU_SHARPNESS_CONTROL,
439                 .size           = 16,
440                 .offset         = 0,
441                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
442                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
443         },
444         {
445                 .id             = V4L2_CID_GAMMA,
446                 .name           = "Gamma",
447                 .entity         = UVC_GUID_UVC_PROCESSING,
448                 .selector       = UVC_PU_GAMMA_CONTROL,
449                 .size           = 16,
450                 .offset         = 0,
451                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
452                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
453         },
454         {
455                 .id             = V4L2_CID_BACKLIGHT_COMPENSATION,
456                 .name           = "Backlight Compensation",
457                 .entity         = UVC_GUID_UVC_PROCESSING,
458                 .selector       = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
459                 .size           = 16,
460                 .offset         = 0,
461                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
462                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
463         },
464         {
465                 .id             = V4L2_CID_GAIN,
466                 .name           = "Gain",
467                 .entity         = UVC_GUID_UVC_PROCESSING,
468                 .selector       = UVC_PU_GAIN_CONTROL,
469                 .size           = 16,
470                 .offset         = 0,
471                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
472                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
473         },
474         {
475                 .id             = V4L2_CID_POWER_LINE_FREQUENCY,
476                 .name           = "Power Line Frequency",
477                 .entity         = UVC_GUID_UVC_PROCESSING,
478                 .selector       = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
479                 .size           = 2,
480                 .offset         = 0,
481                 .v4l2_type      = V4L2_CTRL_TYPE_MENU,
482                 .data_type      = UVC_CTRL_DATA_TYPE_ENUM,
483                 .menu_info      = power_line_frequency_controls,
484                 .menu_count     = ARRAY_SIZE(power_line_frequency_controls),
485         },
486         {
487                 .id             = V4L2_CID_HUE_AUTO,
488                 .name           = "Hue, Auto",
489                 .entity         = UVC_GUID_UVC_PROCESSING,
490                 .selector       = UVC_PU_HUE_AUTO_CONTROL,
491                 .size           = 1,
492                 .offset         = 0,
493                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
494                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
495         },
496         {
497                 .id             = V4L2_CID_EXPOSURE_AUTO,
498                 .name           = "Exposure, Auto",
499                 .entity         = UVC_GUID_UVC_CAMERA,
500                 .selector       = UVC_CT_AE_MODE_CONTROL,
501                 .size           = 4,
502                 .offset         = 0,
503                 .v4l2_type      = V4L2_CTRL_TYPE_MENU,
504                 .data_type      = UVC_CTRL_DATA_TYPE_BITMASK,
505                 .menu_info      = exposure_auto_controls,
506                 .menu_count     = ARRAY_SIZE(exposure_auto_controls),
507         },
508         {
509                 .id             = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
510                 .name           = "Exposure, Auto Priority",
511                 .entity         = UVC_GUID_UVC_CAMERA,
512                 .selector       = UVC_CT_AE_PRIORITY_CONTROL,
513                 .size           = 1,
514                 .offset         = 0,
515                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
516                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
517         },
518         {
519                 .id             = V4L2_CID_EXPOSURE_ABSOLUTE,
520                 .name           = "Exposure (Absolute)",
521                 .entity         = UVC_GUID_UVC_CAMERA,
522                 .selector       = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
523                 .size           = 32,
524                 .offset         = 0,
525                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
526                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
527         },
528         {
529                 .id             = V4L2_CID_AUTO_WHITE_BALANCE,
530                 .name           = "White Balance Temperature, Auto",
531                 .entity         = UVC_GUID_UVC_PROCESSING,
532                 .selector       = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
533                 .size           = 1,
534                 .offset         = 0,
535                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
536                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
537         },
538         {
539                 .id             = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
540                 .name           = "White Balance Temperature",
541                 .entity         = UVC_GUID_UVC_PROCESSING,
542                 .selector       = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
543                 .size           = 16,
544                 .offset         = 0,
545                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
546                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
547         },
548         {
549                 .id             = V4L2_CID_AUTO_WHITE_BALANCE,
550                 .name           = "White Balance Component, Auto",
551                 .entity         = UVC_GUID_UVC_PROCESSING,
552                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
553                 .size           = 1,
554                 .offset         = 0,
555                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
556                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
557         },
558         {
559                 .id             = V4L2_CID_BLUE_BALANCE,
560                 .name           = "White Balance Blue Component",
561                 .entity         = UVC_GUID_UVC_PROCESSING,
562                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
563                 .size           = 16,
564                 .offset         = 0,
565                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
566                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
567         },
568         {
569                 .id             = V4L2_CID_RED_BALANCE,
570                 .name           = "White Balance Red Component",
571                 .entity         = UVC_GUID_UVC_PROCESSING,
572                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
573                 .size           = 16,
574                 .offset         = 16,
575                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
576                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
577         },
578         {
579                 .id             = V4L2_CID_FOCUS_ABSOLUTE,
580                 .name           = "Focus (absolute)",
581                 .entity         = UVC_GUID_UVC_CAMERA,
582                 .selector       = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
583                 .size           = 16,
584                 .offset         = 0,
585                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
586                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
587         },
588         {
589                 .id             = V4L2_CID_FOCUS_AUTO,
590                 .name           = "Focus, Auto",
591                 .entity         = UVC_GUID_UVC_CAMERA,
592                 .selector       = UVC_CT_FOCUS_AUTO_CONTROL,
593                 .size           = 1,
594                 .offset         = 0,
595                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
596                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
597         },
598         {
599                 .id             = V4L2_CID_IRIS_ABSOLUTE,
600                 .name           = "Iris, Absolute",
601                 .entity         = UVC_GUID_UVC_CAMERA,
602                 .selector       = UVC_CT_IRIS_ABSOLUTE_CONTROL,
603                 .size           = 16,
604                 .offset         = 0,
605                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
606                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
607         },
608         {
609                 .id             = V4L2_CID_IRIS_RELATIVE,
610                 .name           = "Iris, Relative",
611                 .entity         = UVC_GUID_UVC_CAMERA,
612                 .selector       = UVC_CT_IRIS_RELATIVE_CONTROL,
613                 .size           = 8,
614                 .offset         = 0,
615                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
616                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
617         },
618         {
619                 .id             = V4L2_CID_ZOOM_ABSOLUTE,
620                 .name           = "Zoom, Absolute",
621                 .entity         = UVC_GUID_UVC_CAMERA,
622                 .selector       = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
623                 .size           = 16,
624                 .offset         = 0,
625                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
626                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
627         },
628         {
629                 .id             = V4L2_CID_ZOOM_CONTINUOUS,
630                 .name           = "Zoom, Continuous",
631                 .entity         = UVC_GUID_UVC_CAMERA,
632                 .selector       = UVC_CT_ZOOM_RELATIVE_CONTROL,
633                 .size           = 0,
634                 .offset         = 0,
635                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
636                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
637                 .get            = uvc_ctrl_get_zoom,
638                 .set            = uvc_ctrl_set_zoom,
639         },
640         {
641                 .id             = V4L2_CID_PAN_ABSOLUTE,
642                 .name           = "Pan (Absolute)",
643                 .entity         = UVC_GUID_UVC_CAMERA,
644                 .selector       = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
645                 .size           = 32,
646                 .offset         = 0,
647                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
648                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
649         },
650         {
651                 .id             = V4L2_CID_TILT_ABSOLUTE,
652                 .name           = "Tilt (Absolute)",
653                 .entity         = UVC_GUID_UVC_CAMERA,
654                 .selector       = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
655                 .size           = 32,
656                 .offset         = 32,
657                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
658                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
659         },
660         {
661                 .id             = V4L2_CID_PRIVACY,
662                 .name           = "Privacy",
663                 .entity         = UVC_GUID_UVC_CAMERA,
664                 .selector       = UVC_CT_PRIVACY_CONTROL,
665                 .size           = 1,
666                 .offset         = 0,
667                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
668                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
669         },
670 };
671
672 /* ------------------------------------------------------------------------
673  * Utility functions
674  */
675
676 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
677 {
678         return ctrl->uvc_data + id * ctrl->info.size;
679 }
680
681 static inline int uvc_test_bit(const __u8 *data, int bit)
682 {
683         return (data[bit >> 3] >> (bit & 7)) & 1;
684 }
685
686 static inline void uvc_clear_bit(__u8 *data, int bit)
687 {
688         data[bit >> 3] &= ~(1 << (bit & 7));
689 }
690
691 /* Extract the bit string specified by mapping->offset and mapping->size
692  * from the little-endian data stored at 'data' and return the result as
693  * a signed 32bit integer. Sign extension will be performed if the mapping
694  * references a signed data type.
695  */
696 static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
697         __u8 query, const __u8 *data)
698 {
699         int bits = mapping->size;
700         int offset = mapping->offset;
701         __s32 value = 0;
702         __u8 mask;
703
704         data += offset / 8;
705         offset &= 7;
706         mask = ((1LL << bits) - 1) << offset;
707
708         for (; bits > 0; data++) {
709                 __u8 byte = *data & mask;
710                 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
711                 bits -= 8 - (offset > 0 ? offset : 0);
712                 offset -= 8;
713                 mask = (1 << bits) - 1;
714         }
715
716         /* Sign-extend the value if needed. */
717         if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
718                 value |= -(value & (1 << (mapping->size - 1)));
719
720         return value;
721 }
722
723 /* Set the bit string specified by mapping->offset and mapping->size
724  * in the little-endian data stored at 'data' to the value 'value'.
725  */
726 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
727         __s32 value, __u8 *data)
728 {
729         int bits = mapping->size;
730         int offset = mapping->offset;
731         __u8 mask;
732
733         /* According to the v4l2 spec, writing any value to a button control
734          * should result in the action belonging to the button control being
735          * triggered. UVC devices however want to see a 1 written -> override
736          * value.
737          */
738         if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
739                 value = -1;
740
741         data += offset / 8;
742         offset &= 7;
743
744         for (; bits > 0; data++) {
745                 mask = ((1LL << bits) - 1) << offset;
746                 *data = (*data & ~mask) | ((value << offset) & mask);
747                 value >>= offset ? offset : 8;
748                 bits -= 8 - offset;
749                 offset = 0;
750         }
751 }
752
753 /* ------------------------------------------------------------------------
754  * Terminal and unit management
755  */
756
757 static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
758 static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
759 static const __u8 uvc_media_transport_input_guid[16] =
760         UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
761
762 static int uvc_entity_match_guid(const struct uvc_entity *entity,
763         const __u8 guid[16])
764 {
765         switch (UVC_ENTITY_TYPE(entity)) {
766         case UVC_ITT_CAMERA:
767                 return memcmp(uvc_camera_guid, guid, 16) == 0;
768
769         case UVC_ITT_MEDIA_TRANSPORT_INPUT:
770                 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
771
772         case UVC_VC_PROCESSING_UNIT:
773                 return memcmp(uvc_processing_guid, guid, 16) == 0;
774
775         case UVC_VC_EXTENSION_UNIT:
776                 return memcmp(entity->extension.guidExtensionCode,
777                               guid, 16) == 0;
778
779         default:
780                 return 0;
781         }
782 }
783
784 /* ------------------------------------------------------------------------
785  * UVC Controls
786  */
787
788 static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
789         struct uvc_control_mapping **mapping, struct uvc_control **control,
790         int next)
791 {
792         struct uvc_control *ctrl;
793         struct uvc_control_mapping *map;
794         unsigned int i;
795
796         if (entity == NULL)
797                 return;
798
799         for (i = 0; i < entity->ncontrols; ++i) {
800                 ctrl = &entity->controls[i];
801                 if (!ctrl->initialized)
802                         continue;
803
804                 list_for_each_entry(map, &ctrl->info.mappings, list) {
805                         if ((map->id == v4l2_id) && !next) {
806                                 *control = ctrl;
807                                 *mapping = map;
808                                 return;
809                         }
810
811                         if ((*mapping == NULL || (*mapping)->id > map->id) &&
812                             (map->id > v4l2_id) && next) {
813                                 *control = ctrl;
814                                 *mapping = map;
815                         }
816                 }
817         }
818 }
819
820 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
821         __u32 v4l2_id, struct uvc_control_mapping **mapping)
822 {
823         struct uvc_control *ctrl = NULL;
824         struct uvc_entity *entity;
825         int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
826
827         *mapping = NULL;
828
829         /* Mask the query flags. */
830         v4l2_id &= V4L2_CTRL_ID_MASK;
831
832         /* Find the control. */
833         list_for_each_entry(entity, &chain->entities, chain) {
834                 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
835                 if (ctrl && !next)
836                         return ctrl;
837         }
838
839         if (ctrl == NULL && !next)
840                 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
841                                 v4l2_id);
842
843         return ctrl;
844 }
845
846 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
847         struct uvc_control *ctrl)
848 {
849         int ret;
850
851         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
852                 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
853                                      chain->dev->intfnum, ctrl->info.selector,
854                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
855                                      ctrl->info.size);
856                 if (ret < 0)
857                         return ret;
858         }
859
860         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
861                 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
862                                      chain->dev->intfnum, ctrl->info.selector,
863                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
864                                      ctrl->info.size);
865                 if (ret < 0)
866                         return ret;
867         }
868         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
869                 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
870                                      chain->dev->intfnum, ctrl->info.selector,
871                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
872                                      ctrl->info.size);
873                 if (ret < 0)
874                         return ret;
875         }
876         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
877                 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
878                                      chain->dev->intfnum, ctrl->info.selector,
879                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
880                                      ctrl->info.size);
881                 if (ret < 0)
882                         return ret;
883         }
884
885         ctrl->cached = 1;
886         return 0;
887 }
888
889 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
890         struct v4l2_queryctrl *v4l2_ctrl)
891 {
892         struct uvc_control *ctrl;
893         struct uvc_control_mapping *mapping;
894         struct uvc_menu_info *menu;
895         unsigned int i;
896         int ret;
897
898         ret = mutex_lock_interruptible(&chain->ctrl_mutex);
899         if (ret < 0)
900                 return -ERESTARTSYS;
901
902         ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
903         if (ctrl == NULL) {
904                 ret = -EINVAL;
905                 goto done;
906         }
907
908         memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
909         v4l2_ctrl->id = mapping->id;
910         v4l2_ctrl->type = mapping->v4l2_type;
911         strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
912         v4l2_ctrl->flags = 0;
913
914         if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
915                 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
916         if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
917                 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
918
919         if (!ctrl->cached) {
920                 ret = uvc_ctrl_populate_cache(chain, ctrl);
921                 if (ret < 0)
922                         goto done;
923         }
924
925         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
926                 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
927                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
928         }
929
930         switch (mapping->v4l2_type) {
931         case V4L2_CTRL_TYPE_MENU:
932                 v4l2_ctrl->minimum = 0;
933                 v4l2_ctrl->maximum = mapping->menu_count - 1;
934                 v4l2_ctrl->step = 1;
935
936                 menu = mapping->menu_info;
937                 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
938                         if (menu->value == v4l2_ctrl->default_value) {
939                                 v4l2_ctrl->default_value = i;
940                                 break;
941                         }
942                 }
943
944                 goto done;
945
946         case V4L2_CTRL_TYPE_BOOLEAN:
947                 v4l2_ctrl->minimum = 0;
948                 v4l2_ctrl->maximum = 1;
949                 v4l2_ctrl->step = 1;
950                 goto done;
951
952         case V4L2_CTRL_TYPE_BUTTON:
953                 v4l2_ctrl->minimum = 0;
954                 v4l2_ctrl->maximum = 0;
955                 v4l2_ctrl->step = 0;
956                 goto done;
957
958         default:
959                 break;
960         }
961
962         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
963                 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
964                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
965
966         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
967                 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
968                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
969
970         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
971                 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
972                                   uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
973
974 done:
975         mutex_unlock(&chain->ctrl_mutex);
976         return ret;
977 }
978
979 /*
980  * Mapping V4L2 controls to UVC controls can be straighforward if done well.
981  * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
982  * must be grouped (for instance the Red Balance, Blue Balance and Do White
983  * Balance V4L2 controls use the White Balance Component UVC control) or
984  * otherwise translated. The approach we take here is to use a translation
985  * table for the controls that can be mapped directly, and handle the others
986  * manually.
987  */
988 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
989         struct v4l2_querymenu *query_menu)
990 {
991         struct uvc_menu_info *menu_info;
992         struct uvc_control_mapping *mapping;
993         struct uvc_control *ctrl;
994         u32 index = query_menu->index;
995         u32 id = query_menu->id;
996         int ret;
997
998         memset(query_menu, 0, sizeof(*query_menu));
999         query_menu->id = id;
1000         query_menu->index = index;
1001
1002         ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1003         if (ret < 0)
1004                 return -ERESTARTSYS;
1005
1006         ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1007         if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1008                 ret = -EINVAL;
1009                 goto done;
1010         }
1011
1012         if (query_menu->index >= mapping->menu_count) {
1013                 ret = -EINVAL;
1014                 goto done;
1015         }
1016
1017         menu_info = &mapping->menu_info[query_menu->index];
1018
1019         if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1020             (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1021                 s32 bitmap;
1022
1023                 if (!ctrl->cached) {
1024                         ret = uvc_ctrl_populate_cache(chain, ctrl);
1025                         if (ret < 0)
1026                                 goto done;
1027                 }
1028
1029                 bitmap = mapping->get(mapping, UVC_GET_RES,
1030                                       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1031                 if (!(bitmap & menu_info->value)) {
1032                         ret = -EINVAL;
1033                         goto done;
1034                 }
1035         }
1036
1037         strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
1038
1039 done:
1040         mutex_unlock(&chain->ctrl_mutex);
1041         return ret;
1042 }
1043
1044
1045 /* --------------------------------------------------------------------------
1046  * Control transactions
1047  *
1048  * To make extended set operations as atomic as the hardware allows, controls
1049  * are handled using begin/commit/rollback operations.
1050  *
1051  * At the beginning of a set request, uvc_ctrl_begin should be called to
1052  * initialize the request. This function acquires the control lock.
1053  *
1054  * When setting a control, the new value is stored in the control data field
1055  * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1056  * later processing. If the UVC and V4L2 control sizes differ, the current
1057  * value is loaded from the hardware before storing the new value in the data
1058  * field.
1059  *
1060  * After processing all controls in the transaction, uvc_ctrl_commit or
1061  * uvc_ctrl_rollback must be called to apply the pending changes to the
1062  * hardware or revert them. When applying changes, all controls marked as
1063  * dirty will be modified in the UVC device, and the dirty flag will be
1064  * cleared. When reverting controls, the control data field
1065  * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1066  * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1067  * control lock.
1068  */
1069 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1070 {
1071         return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1072 }
1073
1074 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1075         struct uvc_entity *entity, int rollback)
1076 {
1077         struct uvc_control *ctrl;
1078         unsigned int i;
1079         int ret;
1080
1081         if (entity == NULL)
1082                 return 0;
1083
1084         for (i = 0; i < entity->ncontrols; ++i) {
1085                 ctrl = &entity->controls[i];
1086                 if (!ctrl->initialized)
1087                         continue;
1088
1089                 /* Reset the loaded flag for auto-update controls that were
1090                  * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1091                  * uvc_ctrl_get from using the cached value.
1092                  */
1093                 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE)
1094                         ctrl->loaded = 0;
1095
1096                 if (!ctrl->dirty)
1097                         continue;
1098
1099                 if (!rollback)
1100                         ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1101                                 dev->intfnum, ctrl->info.selector,
1102                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1103                                 ctrl->info.size);
1104                 else
1105                         ret = 0;
1106
1107                 if (rollback || ret < 0)
1108                         memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1109                                uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1110                                ctrl->info.size);
1111
1112                 ctrl->dirty = 0;
1113
1114                 if (ret < 0)
1115                         return ret;
1116         }
1117
1118         return 0;
1119 }
1120
1121 int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
1122 {
1123         struct uvc_entity *entity;
1124         int ret = 0;
1125
1126         /* Find the control. */
1127         list_for_each_entry(entity, &chain->entities, chain) {
1128                 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1129                 if (ret < 0)
1130                         goto done;
1131         }
1132
1133 done:
1134         mutex_unlock(&chain->ctrl_mutex);
1135         return ret;
1136 }
1137
1138 int uvc_ctrl_get(struct uvc_video_chain *chain,
1139         struct v4l2_ext_control *xctrl)
1140 {
1141         struct uvc_control *ctrl;
1142         struct uvc_control_mapping *mapping;
1143         struct uvc_menu_info *menu;
1144         unsigned int i;
1145         int ret;
1146
1147         ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1148         if (ctrl == NULL || (ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
1149                 return -EINVAL;
1150
1151         if (!ctrl->loaded) {
1152                 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
1153                                 chain->dev->intfnum, ctrl->info.selector,
1154                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1155                                 ctrl->info.size);
1156                 if (ret < 0)
1157                         return ret;
1158
1159                 ctrl->loaded = 1;
1160         }
1161
1162         xctrl->value = mapping->get(mapping, UVC_GET_CUR,
1163                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1164
1165         if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
1166                 menu = mapping->menu_info;
1167                 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1168                         if (menu->value == xctrl->value) {
1169                                 xctrl->value = i;
1170                                 break;
1171                         }
1172                 }
1173         }
1174
1175         return 0;
1176 }
1177
1178 int uvc_ctrl_set(struct uvc_video_chain *chain,
1179         struct v4l2_ext_control *xctrl)
1180 {
1181         struct uvc_control *ctrl;
1182         struct uvc_control_mapping *mapping;
1183         s32 value;
1184         u32 step;
1185         s32 min;
1186         s32 max;
1187         int ret;
1188
1189         ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1190         if (ctrl == NULL || (ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) == 0)
1191                 return -EINVAL;
1192
1193         /* Clamp out of range values. */
1194         switch (mapping->v4l2_type) {
1195         case V4L2_CTRL_TYPE_INTEGER:
1196                 if (!ctrl->cached) {
1197                         ret = uvc_ctrl_populate_cache(chain, ctrl);
1198                         if (ret < 0)
1199                                 return ret;
1200                 }
1201
1202                 min = mapping->get(mapping, UVC_GET_MIN,
1203                                    uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1204                 max = mapping->get(mapping, UVC_GET_MAX,
1205                                    uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1206                 step = mapping->get(mapping, UVC_GET_RES,
1207                                     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1208                 if (step == 0)
1209                         step = 1;
1210
1211                 xctrl->value = min + (xctrl->value - min + step/2) / step * step;
1212                 xctrl->value = clamp(xctrl->value, min, max);
1213                 value = xctrl->value;
1214                 break;
1215
1216         case V4L2_CTRL_TYPE_BOOLEAN:
1217                 xctrl->value = clamp(xctrl->value, 0, 1);
1218                 value = xctrl->value;
1219                 break;
1220
1221         case V4L2_CTRL_TYPE_MENU:
1222                 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1223                         return -ERANGE;
1224                 value = mapping->menu_info[xctrl->value].value;
1225
1226                 /* Valid menu indices are reported by the GET_RES request for
1227                  * UVC controls that support it.
1228                  */
1229                 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1230                     (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1231                         if (!ctrl->cached) {
1232                                 ret = uvc_ctrl_populate_cache(chain, ctrl);
1233                                 if (ret < 0)
1234                                         return ret;
1235                         }
1236
1237                         step = mapping->get(mapping, UVC_GET_RES,
1238                                         uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1239                         if (!(step & value))
1240                                 return -ERANGE;
1241                 }
1242
1243                 break;
1244
1245         default:
1246                 value = xctrl->value;
1247                 break;
1248         }
1249
1250         /* If the mapping doesn't span the whole UVC control, the current value
1251          * needs to be loaded from the device to perform the read-modify-write
1252          * operation.
1253          */
1254         if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1255                 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1256                         memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1257                                 0, ctrl->info.size);
1258                 } else {
1259                         ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1260                                 ctrl->entity->id, chain->dev->intfnum,
1261                                 ctrl->info.selector,
1262                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1263                                 ctrl->info.size);
1264                         if (ret < 0)
1265                                 return ret;
1266                 }
1267
1268                 ctrl->loaded = 1;
1269         }
1270
1271         /* Backup the current value in case we need to rollback later. */
1272         if (!ctrl->dirty) {
1273                 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1274                        uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1275                        ctrl->info.size);
1276         }
1277
1278         mapping->set(mapping, value,
1279                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1280
1281         ctrl->dirty = 1;
1282         ctrl->modified = 1;
1283         return 0;
1284 }
1285
1286 /* --------------------------------------------------------------------------
1287  * Dynamic controls
1288  */
1289
1290 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
1291         const struct uvc_control *ctrl, struct uvc_control_info *info)
1292 {
1293         struct uvc_ctrl_fixup {
1294                 struct usb_device_id id;
1295                 u8 entity;
1296                 u8 selector;
1297                 u8 flags;
1298         };
1299
1300         static const struct uvc_ctrl_fixup fixups[] = {
1301                 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1302                         UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1303                         UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1304                         UVC_CTRL_FLAG_AUTO_UPDATE },
1305                 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1306                         UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1307                         UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1308                         UVC_CTRL_FLAG_AUTO_UPDATE },
1309                 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1310                         UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1311                         UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1312                         UVC_CTRL_FLAG_AUTO_UPDATE },
1313         };
1314
1315         unsigned int i;
1316
1317         for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
1318                 if (!usb_match_one_id(dev->intf, &fixups[i].id))
1319                         continue;
1320
1321                 if (fixups[i].entity == ctrl->entity->id &&
1322                     fixups[i].selector == info->selector) {
1323                         info->flags = fixups[i].flags;
1324                         return;
1325                 }
1326         }
1327 }
1328
1329 /*
1330  * Query control information (size and flags) for XU controls.
1331  */
1332 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
1333         const struct uvc_control *ctrl, struct uvc_control_info *info)
1334 {
1335         u8 *data;
1336         int ret;
1337
1338         data = kmalloc(2, GFP_KERNEL);
1339         if (data == NULL)
1340                 return -ENOMEM;
1341
1342         memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
1343                sizeof(info->entity));
1344         info->index = ctrl->index;
1345         info->selector = ctrl->index + 1;
1346
1347         /* Query and verify the control length (GET_LEN) */
1348         ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
1349                              info->selector, data, 2);
1350         if (ret < 0) {
1351                 uvc_trace(UVC_TRACE_CONTROL,
1352                           "GET_LEN failed on control %pUl/%u (%d).\n",
1353                            info->entity, info->selector, ret);
1354                 goto done;
1355         }
1356
1357         info->size = le16_to_cpup((__le16 *)data);
1358
1359         /* Query the control information (GET_INFO) */
1360         ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
1361                              info->selector, data, 1);
1362         if (ret < 0) {
1363                 uvc_trace(UVC_TRACE_CONTROL,
1364                           "GET_INFO failed on control %pUl/%u (%d).\n",
1365                           info->entity, info->selector, ret);
1366                 goto done;
1367         }
1368
1369         info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
1370                     | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF
1371                     | (data[0] & UVC_CONTROL_CAP_GET ?
1372                        UVC_CTRL_FLAG_GET_CUR : 0)
1373                     | (data[0] & UVC_CONTROL_CAP_SET ?
1374                        UVC_CTRL_FLAG_SET_CUR : 0)
1375                     | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1376                        UVC_CTRL_FLAG_AUTO_UPDATE : 0);
1377
1378         uvc_ctrl_fixup_xu_info(dev, ctrl, info);
1379
1380         uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
1381                   "flags { get %u set %u auto %u }.\n",
1382                   info->entity, info->selector, info->size,
1383                   (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
1384                   (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
1385                   (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
1386
1387 done:
1388         kfree(data);
1389         return ret;
1390 }
1391
1392 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1393         const struct uvc_control_info *info);
1394
1395 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
1396         struct uvc_control *ctrl)
1397 {
1398         struct uvc_control_info info;
1399         int ret;
1400
1401         if (ctrl->initialized)
1402                 return 0;
1403
1404         ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
1405         if (ret < 0)
1406                 return ret;
1407
1408         ret = uvc_ctrl_add_info(dev, ctrl, &info);
1409         if (ret < 0)
1410                 uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
1411                           "%pUl/%u on device %s entity %u\n", info.entity,
1412                           info.selector, dev->udev->devpath, ctrl->entity->id);
1413
1414         return ret;
1415 }
1416
1417 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1418         struct uvc_xu_control_query *xqry)
1419 {
1420         struct uvc_entity *entity;
1421         struct uvc_control *ctrl;
1422         unsigned int i, found = 0;
1423         __u32 reqflags;
1424         __u16 size;
1425         __u8 *data = NULL;
1426         int ret;
1427
1428         /* Find the extension unit. */
1429         list_for_each_entry(entity, &chain->entities, chain) {
1430                 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1431                     entity->id == xqry->unit)
1432                         break;
1433         }
1434
1435         if (entity->id != xqry->unit) {
1436                 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1437                         xqry->unit);
1438                 return -ENOENT;
1439         }
1440
1441         /* Find the control and perform delayed initialization if needed. */
1442         for (i = 0; i < entity->ncontrols; ++i) {
1443                 ctrl = &entity->controls[i];
1444                 if (ctrl->index == xqry->selector - 1) {
1445                         found = 1;
1446                         break;
1447                 }
1448         }
1449
1450         if (!found) {
1451                 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1452                         entity->extension.guidExtensionCode, xqry->selector);
1453                 return -ENOENT;
1454         }
1455
1456         if (mutex_lock_interruptible(&chain->ctrl_mutex))
1457                 return -ERESTARTSYS;
1458
1459         ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1460         if (ret < 0) {
1461                 ret = -ENOENT;
1462                 goto done;
1463         }
1464
1465         /* Validate the required buffer size and flags for the request */
1466         reqflags = 0;
1467         size = ctrl->info.size;
1468
1469         switch (xqry->query) {
1470         case UVC_GET_CUR:
1471                 reqflags = UVC_CTRL_FLAG_GET_CUR;
1472                 break;
1473         case UVC_GET_MIN:
1474                 reqflags = UVC_CTRL_FLAG_GET_MIN;
1475                 break;
1476         case UVC_GET_MAX:
1477                 reqflags = UVC_CTRL_FLAG_GET_MAX;
1478                 break;
1479         case UVC_GET_DEF:
1480                 reqflags = UVC_CTRL_FLAG_GET_DEF;
1481                 break;
1482         case UVC_GET_RES:
1483                 reqflags = UVC_CTRL_FLAG_GET_RES;
1484                 break;
1485         case UVC_SET_CUR:
1486                 reqflags = UVC_CTRL_FLAG_SET_CUR;
1487                 break;
1488         case UVC_GET_LEN:
1489                 size = 2;
1490                 break;
1491         case UVC_GET_INFO:
1492                 size = 1;
1493                 break;
1494         default:
1495                 ret = -EINVAL;
1496                 goto done;
1497         }
1498
1499         if (size != xqry->size) {
1500                 ret = -ENOBUFS;
1501                 goto done;
1502         }
1503
1504         if (reqflags && !(ctrl->info.flags & reqflags)) {
1505                 ret = -EBADRQC;
1506                 goto done;
1507         }
1508
1509         data = kmalloc(size, GFP_KERNEL);
1510         if (data == NULL) {
1511                 ret = -ENOMEM;
1512                 goto done;
1513         }
1514
1515         if (xqry->query == UVC_SET_CUR &&
1516             copy_from_user(data, xqry->data, size)) {
1517                 ret = -EFAULT;
1518                 goto done;
1519         }
1520
1521         ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
1522                              chain->dev->intfnum, xqry->selector, data, size);
1523         if (ret < 0)
1524                 goto done;
1525
1526         if (xqry->query != UVC_SET_CUR &&
1527             copy_to_user(xqry->data, data, size))
1528                 ret = -EFAULT;
1529 done:
1530         kfree(data);
1531         mutex_unlock(&chain->ctrl_mutex);
1532         return ret;
1533 }
1534
1535 /* --------------------------------------------------------------------------
1536  * Suspend/resume
1537  */
1538
1539 /*
1540  * Restore control values after resume, skipping controls that haven't been
1541  * changed.
1542  *
1543  * TODO
1544  * - Don't restore modified controls that are back to their default value.
1545  * - Handle restore order (Auto-Exposure Mode should be restored before
1546  *   Exposure Time).
1547  */
1548 int uvc_ctrl_resume_device(struct uvc_device *dev)
1549 {
1550         struct uvc_control *ctrl;
1551         struct uvc_entity *entity;
1552         unsigned int i;
1553         int ret;
1554
1555         /* Walk the entities list and restore controls when possible. */
1556         list_for_each_entry(entity, &dev->entities, list) {
1557
1558                 for (i = 0; i < entity->ncontrols; ++i) {
1559                         ctrl = &entity->controls[i];
1560
1561                         if (!ctrl->initialized || !ctrl->modified ||
1562                             (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
1563                                 continue;
1564
1565                         printk(KERN_INFO "restoring control %pUl/%u/%u\n",
1566                                 ctrl->info.entity, ctrl->info.index,
1567                                 ctrl->info.selector);
1568                         ctrl->dirty = 1;
1569                 }
1570
1571                 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1572                 if (ret < 0)
1573                         return ret;
1574         }
1575
1576         return 0;
1577 }
1578
1579 /* --------------------------------------------------------------------------
1580  * Control and mapping handling
1581  */
1582
1583 /*
1584  * Add control information to a given control.
1585  */
1586 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1587         const struct uvc_control_info *info)
1588 {
1589         int ret = 0;
1590
1591         memcpy(&ctrl->info, info, sizeof(*info));
1592         INIT_LIST_HEAD(&ctrl->info.mappings);
1593
1594         /* Allocate an array to save control values (cur, def, max, etc.) */
1595         ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
1596                                  GFP_KERNEL);
1597         if (ctrl->uvc_data == NULL) {
1598                 ret = -ENOMEM;
1599                 goto done;
1600         }
1601
1602         ctrl->initialized = 1;
1603
1604         uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
1605                 "entity %u\n", ctrl->info.entity, ctrl->info.selector,
1606                 dev->udev->devpath, ctrl->entity->id);
1607
1608 done:
1609         if (ret < 0)
1610                 kfree(ctrl->uvc_data);
1611         return ret;
1612 }
1613
1614 /*
1615  * Add a control mapping to a given control.
1616  */
1617 static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
1618         struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
1619 {
1620         struct uvc_control_mapping *map;
1621         unsigned int size;
1622
1623         /* Most mappings come from static kernel data and need to be duplicated.
1624          * Mappings that come from userspace will be unnecessarily duplicated,
1625          * this could be optimized.
1626          */
1627         map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
1628         if (map == NULL)
1629                 return -ENOMEM;
1630
1631         size = sizeof(*mapping->menu_info) * mapping->menu_count;
1632         map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
1633         if (map->menu_info == NULL) {
1634                 kfree(map);
1635                 return -ENOMEM;
1636         }
1637
1638         if (map->get == NULL)
1639                 map->get = uvc_get_le_value;
1640         if (map->set == NULL)
1641                 map->set = uvc_set_le_value;
1642
1643         map->ctrl = &ctrl->info;
1644         list_add_tail(&map->list, &ctrl->info.mappings);
1645         uvc_trace(UVC_TRACE_CONTROL,
1646                 "Adding mapping '%s' to control %pUl/%u.\n",
1647                 map->name, ctrl->info.entity, ctrl->info.selector);
1648
1649         return 0;
1650 }
1651
1652 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
1653         const struct uvc_control_mapping *mapping)
1654 {
1655         struct uvc_device *dev = chain->dev;
1656         struct uvc_control_mapping *map;
1657         struct uvc_entity *entity;
1658         struct uvc_control *ctrl;
1659         int found = 0;
1660         int ret;
1661
1662         if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1663                 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
1664                         "id 0x%08x is invalid.\n", mapping->name,
1665                         mapping->id);
1666                 return -EINVAL;
1667         }
1668
1669         /* Search for the matching (GUID/CS) control on the current chain */
1670         list_for_each_entry(entity, &chain->entities, chain) {
1671                 unsigned int i;
1672
1673                 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
1674                     !uvc_entity_match_guid(entity, mapping->entity))
1675                         continue;
1676
1677                 for (i = 0; i < entity->ncontrols; ++i) {
1678                         ctrl = &entity->controls[i];
1679                         if (ctrl->index == mapping->selector - 1) {
1680                                 found = 1;
1681                                 break;
1682                         }
1683                 }
1684
1685                 if (found)
1686                         break;
1687         }
1688         if (!found)
1689                 return -ENOENT;
1690
1691         if (mutex_lock_interruptible(&chain->ctrl_mutex))
1692                 return -ERESTARTSYS;
1693
1694         /* Perform delayed initialization of XU controls */
1695         ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
1696         if (ret < 0) {
1697                 ret = -ENOENT;
1698                 goto done;
1699         }
1700
1701         list_for_each_entry(map, &ctrl->info.mappings, list) {
1702                 if (mapping->id == map->id) {
1703                         uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
1704                                 "control id 0x%08x already exists.\n",
1705                                 mapping->name, mapping->id);
1706                         ret = -EEXIST;
1707                         goto done;
1708                 }
1709         }
1710
1711         /* Prevent excess memory consumption */
1712         if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
1713                 atomic_dec(&dev->nmappings);
1714                 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
1715                         "mappings count (%u) exceeded.\n", mapping->name,
1716                         UVC_MAX_CONTROL_MAPPINGS);
1717                 ret = -ENOMEM;
1718                 goto done;
1719         }
1720
1721         ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
1722         if (ret < 0)
1723                 atomic_dec(&dev->nmappings);
1724
1725 done:
1726         mutex_unlock(&chain->ctrl_mutex);
1727         return ret;
1728 }
1729
1730 /*
1731  * Prune an entity of its bogus controls using a blacklist. Bogus controls
1732  * are currently the ones that crash the camera or unconditionally return an
1733  * error when queried.
1734  */
1735 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
1736         struct uvc_entity *entity)
1737 {
1738         struct uvc_ctrl_blacklist {
1739                 struct usb_device_id id;
1740                 u8 index;
1741         };
1742
1743         static const struct uvc_ctrl_blacklist processing_blacklist[] = {
1744                 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
1745                 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
1746                 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
1747         };
1748         static const struct uvc_ctrl_blacklist camera_blacklist[] = {
1749                 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
1750         };
1751
1752         const struct uvc_ctrl_blacklist *blacklist;
1753         unsigned int size;
1754         unsigned int count;
1755         unsigned int i;
1756         u8 *controls;
1757
1758         switch (UVC_ENTITY_TYPE(entity)) {
1759         case UVC_VC_PROCESSING_UNIT:
1760                 blacklist = processing_blacklist;
1761                 count = ARRAY_SIZE(processing_blacklist);
1762                 controls = entity->processing.bmControls;
1763                 size = entity->processing.bControlSize;
1764                 break;
1765
1766         case UVC_ITT_CAMERA:
1767                 blacklist = camera_blacklist;
1768                 count = ARRAY_SIZE(camera_blacklist);
1769                 controls = entity->camera.bmControls;
1770                 size = entity->camera.bControlSize;
1771                 break;
1772
1773         default:
1774                 return;
1775         }
1776
1777         for (i = 0; i < count; ++i) {
1778                 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
1779                         continue;
1780
1781                 if (blacklist[i].index >= 8 * size ||
1782                     !uvc_test_bit(controls, blacklist[i].index))
1783                         continue;
1784
1785                 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
1786                         "removing it.\n", entity->id, blacklist[i].index);
1787
1788                 uvc_clear_bit(controls, blacklist[i].index);
1789         }
1790 }
1791
1792 /*
1793  * Add control information and hardcoded stock control mappings to the given
1794  * device.
1795  */
1796 static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
1797 {
1798         const struct uvc_control_info *info = uvc_ctrls;
1799         const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
1800         const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1801         const struct uvc_control_mapping *mend =
1802                 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1803
1804         /* XU controls initialization requires querying the device for control
1805          * information. As some buggy UVC devices will crash when queried
1806          * repeatedly in a tight loop, delay XU controls initialization until
1807          * first use.
1808          */
1809         if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
1810                 return;
1811
1812         for (; info < iend; ++info) {
1813                 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
1814                     ctrl->index == info->index) {
1815                         uvc_ctrl_add_info(dev, ctrl, info);
1816                         break;
1817                  }
1818         }
1819
1820         if (!ctrl->initialized)
1821                 return;
1822
1823         for (; mapping < mend; ++mapping) {
1824                 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
1825                     ctrl->info.selector == mapping->selector)
1826                         __uvc_ctrl_add_mapping(dev, ctrl, mapping);
1827         }
1828 }
1829
1830 /*
1831  * Initialize device controls.
1832  */
1833 int uvc_ctrl_init_device(struct uvc_device *dev)
1834 {
1835         struct uvc_entity *entity;
1836         unsigned int i;
1837
1838         /* Walk the entities list and instantiate controls */
1839         list_for_each_entry(entity, &dev->entities, list) {
1840                 struct uvc_control *ctrl;
1841                 unsigned int bControlSize = 0, ncontrols = 0;
1842                 __u8 *bmControls = NULL;
1843
1844                 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
1845                         bmControls = entity->extension.bmControls;
1846                         bControlSize = entity->extension.bControlSize;
1847                 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
1848                         bmControls = entity->processing.bmControls;
1849                         bControlSize = entity->processing.bControlSize;
1850                 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
1851                         bmControls = entity->camera.bmControls;
1852                         bControlSize = entity->camera.bControlSize;
1853                 }
1854
1855                 /* Remove bogus/blacklisted controls */
1856                 uvc_ctrl_prune_entity(dev, entity);
1857
1858                 /* Count supported controls and allocate the controls array */
1859                 for (i = 0; i < bControlSize; ++i)
1860                         ncontrols += hweight8(bmControls[i]);
1861                 if (ncontrols == 0)
1862                         continue;
1863
1864                 entity->controls = kzalloc(ncontrols * sizeof(*ctrl),
1865                                            GFP_KERNEL);
1866                 if (entity->controls == NULL)
1867                         return -ENOMEM;
1868                 entity->ncontrols = ncontrols;
1869
1870                 /* Initialize all supported controls */
1871                 ctrl = entity->controls;
1872                 for (i = 0; i < bControlSize * 8; ++i) {
1873                         if (uvc_test_bit(bmControls, i) == 0)
1874                                 continue;
1875
1876                         ctrl->entity = entity;
1877                         ctrl->index = i;
1878
1879                         uvc_ctrl_init_ctrl(dev, ctrl);
1880                         ctrl++;
1881                 }
1882         }
1883
1884         return 0;
1885 }
1886
1887 /*
1888  * Cleanup device controls.
1889  */
1890 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
1891         struct uvc_control *ctrl)
1892 {
1893         struct uvc_control_mapping *mapping, *nm;
1894
1895         list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
1896                 list_del(&mapping->list);
1897                 kfree(mapping->menu_info);
1898                 kfree(mapping);
1899         }
1900 }
1901
1902 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1903 {
1904         struct uvc_entity *entity;
1905         unsigned int i;
1906
1907         /* Free controls and control mappings for all entities. */
1908         list_for_each_entry(entity, &dev->entities, list) {
1909                 for (i = 0; i < entity->ncontrols; ++i) {
1910                         struct uvc_control *ctrl = &entity->controls[i];
1911
1912                         if (!ctrl->initialized)
1913                                 continue;
1914
1915                         uvc_ctrl_cleanup_mappings(dev, ctrl);
1916                         kfree(ctrl->uvc_data);
1917                 }
1918
1919                 kfree(entity->controls);
1920         }
1921 }