GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / media / usb / 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/workqueue.h>
24 #include <linux/atomic.h>
25 #include <media/v4l2-ctrls.h>
26
27 #include "uvcvideo.h"
28
29 #define UVC_CTRL_DATA_CURRENT   0
30 #define UVC_CTRL_DATA_BACKUP    1
31 #define UVC_CTRL_DATA_MIN       2
32 #define UVC_CTRL_DATA_MAX       3
33 #define UVC_CTRL_DATA_RES       4
34 #define UVC_CTRL_DATA_DEF       5
35 #define UVC_CTRL_DATA_LAST      6
36
37 /* ------------------------------------------------------------------------
38  * Controls
39  */
40
41 static struct uvc_control_info uvc_ctrls[] = {
42         {
43                 .entity         = UVC_GUID_UVC_PROCESSING,
44                 .selector       = UVC_PU_BRIGHTNESS_CONTROL,
45                 .index          = 0,
46                 .size           = 2,
47                 .flags          = UVC_CTRL_FLAG_SET_CUR
48                                 | UVC_CTRL_FLAG_GET_RANGE
49                                 | UVC_CTRL_FLAG_RESTORE,
50         },
51         {
52                 .entity         = UVC_GUID_UVC_PROCESSING,
53                 .selector       = UVC_PU_CONTRAST_CONTROL,
54                 .index          = 1,
55                 .size           = 2,
56                 .flags          = UVC_CTRL_FLAG_SET_CUR
57                                 | UVC_CTRL_FLAG_GET_RANGE
58                                 | UVC_CTRL_FLAG_RESTORE,
59         },
60         {
61                 .entity         = UVC_GUID_UVC_PROCESSING,
62                 .selector       = UVC_PU_HUE_CONTROL,
63                 .index          = 2,
64                 .size           = 2,
65                 .flags          = UVC_CTRL_FLAG_SET_CUR
66                                 | UVC_CTRL_FLAG_GET_RANGE
67                                 | UVC_CTRL_FLAG_RESTORE
68                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
69         },
70         {
71                 .entity         = UVC_GUID_UVC_PROCESSING,
72                 .selector       = UVC_PU_SATURATION_CONTROL,
73                 .index          = 3,
74                 .size           = 2,
75                 .flags          = UVC_CTRL_FLAG_SET_CUR
76                                 | UVC_CTRL_FLAG_GET_RANGE
77                                 | UVC_CTRL_FLAG_RESTORE,
78         },
79         {
80                 .entity         = UVC_GUID_UVC_PROCESSING,
81                 .selector       = UVC_PU_SHARPNESS_CONTROL,
82                 .index          = 4,
83                 .size           = 2,
84                 .flags          = UVC_CTRL_FLAG_SET_CUR
85                                 | UVC_CTRL_FLAG_GET_RANGE
86                                 | UVC_CTRL_FLAG_RESTORE,
87         },
88         {
89                 .entity         = UVC_GUID_UVC_PROCESSING,
90                 .selector       = UVC_PU_GAMMA_CONTROL,
91                 .index          = 5,
92                 .size           = 2,
93                 .flags          = UVC_CTRL_FLAG_SET_CUR
94                                 | UVC_CTRL_FLAG_GET_RANGE
95                                 | UVC_CTRL_FLAG_RESTORE,
96         },
97         {
98                 .entity         = UVC_GUID_UVC_PROCESSING,
99                 .selector       = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
100                 .index          = 6,
101                 .size           = 2,
102                 .flags          = UVC_CTRL_FLAG_SET_CUR
103                                 | UVC_CTRL_FLAG_GET_RANGE
104                                 | UVC_CTRL_FLAG_RESTORE
105                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
106         },
107         {
108                 .entity         = UVC_GUID_UVC_PROCESSING,
109                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
110                 .index          = 7,
111                 .size           = 4,
112                 .flags          = UVC_CTRL_FLAG_SET_CUR
113                                 | UVC_CTRL_FLAG_GET_RANGE
114                                 | UVC_CTRL_FLAG_RESTORE
115                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
116         },
117         {
118                 .entity         = UVC_GUID_UVC_PROCESSING,
119                 .selector       = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
120                 .index          = 8,
121                 .size           = 2,
122                 .flags          = UVC_CTRL_FLAG_SET_CUR
123                                 | UVC_CTRL_FLAG_GET_RANGE
124                                 | UVC_CTRL_FLAG_RESTORE,
125         },
126         {
127                 .entity         = UVC_GUID_UVC_PROCESSING,
128                 .selector       = UVC_PU_GAIN_CONTROL,
129                 .index          = 9,
130                 .size           = 2,
131                 .flags          = UVC_CTRL_FLAG_SET_CUR
132                                 | UVC_CTRL_FLAG_GET_RANGE
133                                 | UVC_CTRL_FLAG_RESTORE,
134         },
135         {
136                 .entity         = UVC_GUID_UVC_PROCESSING,
137                 .selector       = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
138                 .index          = 10,
139                 .size           = 1,
140                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
141                                 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
142         },
143         {
144                 .entity         = UVC_GUID_UVC_PROCESSING,
145                 .selector       = UVC_PU_HUE_AUTO_CONTROL,
146                 .index          = 11,
147                 .size           = 1,
148                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
149                                 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
150         },
151         {
152                 .entity         = UVC_GUID_UVC_PROCESSING,
153                 .selector       = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
154                 .index          = 12,
155                 .size           = 1,
156                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
157                                 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
158         },
159         {
160                 .entity         = UVC_GUID_UVC_PROCESSING,
161                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
162                 .index          = 13,
163                 .size           = 1,
164                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
165                                 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
166         },
167         {
168                 .entity         = UVC_GUID_UVC_PROCESSING,
169                 .selector       = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
170                 .index          = 14,
171                 .size           = 2,
172                 .flags          = UVC_CTRL_FLAG_SET_CUR
173                                 | UVC_CTRL_FLAG_GET_RANGE
174                                 | UVC_CTRL_FLAG_RESTORE,
175         },
176         {
177                 .entity         = UVC_GUID_UVC_PROCESSING,
178                 .selector       = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
179                 .index          = 15,
180                 .size           = 2,
181                 .flags          = UVC_CTRL_FLAG_SET_CUR
182                                 | UVC_CTRL_FLAG_GET_RANGE
183                                 | UVC_CTRL_FLAG_RESTORE,
184         },
185         {
186                 .entity         = UVC_GUID_UVC_PROCESSING,
187                 .selector       = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
188                 .index          = 16,
189                 .size           = 1,
190                 .flags          = UVC_CTRL_FLAG_GET_CUR,
191         },
192         {
193                 .entity         = UVC_GUID_UVC_PROCESSING,
194                 .selector       = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
195                 .index          = 17,
196                 .size           = 1,
197                 .flags          = UVC_CTRL_FLAG_GET_CUR,
198         },
199         {
200                 .entity         = UVC_GUID_UVC_CAMERA,
201                 .selector       = UVC_CT_SCANNING_MODE_CONTROL,
202                 .index          = 0,
203                 .size           = 1,
204                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
205                                 | UVC_CTRL_FLAG_RESTORE,
206         },
207         {
208                 .entity         = UVC_GUID_UVC_CAMERA,
209                 .selector       = UVC_CT_AE_MODE_CONTROL,
210                 .index          = 1,
211                 .size           = 1,
212                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
213                                 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
214                                 | UVC_CTRL_FLAG_RESTORE,
215         },
216         {
217                 .entity         = UVC_GUID_UVC_CAMERA,
218                 .selector       = UVC_CT_AE_PRIORITY_CONTROL,
219                 .index          = 2,
220                 .size           = 1,
221                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
222                                 | UVC_CTRL_FLAG_RESTORE,
223         },
224         {
225                 .entity         = UVC_GUID_UVC_CAMERA,
226                 .selector       = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
227                 .index          = 3,
228                 .size           = 4,
229                 .flags          = UVC_CTRL_FLAG_SET_CUR
230                                 | UVC_CTRL_FLAG_GET_RANGE
231                                 | UVC_CTRL_FLAG_RESTORE
232                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
233         },
234         {
235                 .entity         = UVC_GUID_UVC_CAMERA,
236                 .selector       = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
237                 .index          = 4,
238                 .size           = 1,
239                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
240         },
241         {
242                 .entity         = UVC_GUID_UVC_CAMERA,
243                 .selector       = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
244                 .index          = 5,
245                 .size           = 2,
246                 .flags          = UVC_CTRL_FLAG_SET_CUR
247                                 | UVC_CTRL_FLAG_GET_RANGE
248                                 | UVC_CTRL_FLAG_RESTORE
249                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
250         },
251         {
252                 .entity         = UVC_GUID_UVC_CAMERA,
253                 .selector       = UVC_CT_FOCUS_RELATIVE_CONTROL,
254                 .index          = 6,
255                 .size           = 2,
256                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
257                                 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
258                                 | UVC_CTRL_FLAG_GET_DEF
259                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
260         },
261         {
262                 .entity         = UVC_GUID_UVC_CAMERA,
263                 .selector       = UVC_CT_IRIS_ABSOLUTE_CONTROL,
264                 .index          = 7,
265                 .size           = 2,
266                 .flags          = UVC_CTRL_FLAG_SET_CUR
267                                 | UVC_CTRL_FLAG_GET_RANGE
268                                 | UVC_CTRL_FLAG_RESTORE
269                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
270         },
271         {
272                 .entity         = UVC_GUID_UVC_CAMERA,
273                 .selector       = UVC_CT_IRIS_RELATIVE_CONTROL,
274                 .index          = 8,
275                 .size           = 1,
276                 .flags          = UVC_CTRL_FLAG_SET_CUR
277                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
278         },
279         {
280                 .entity         = UVC_GUID_UVC_CAMERA,
281                 .selector       = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
282                 .index          = 9,
283                 .size           = 2,
284                 .flags          = UVC_CTRL_FLAG_SET_CUR
285                                 | UVC_CTRL_FLAG_GET_RANGE
286                                 | UVC_CTRL_FLAG_RESTORE
287                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
288         },
289         {
290                 .entity         = UVC_GUID_UVC_CAMERA,
291                 .selector       = UVC_CT_ZOOM_RELATIVE_CONTROL,
292                 .index          = 10,
293                 .size           = 3,
294                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
295                                 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
296                                 | UVC_CTRL_FLAG_GET_DEF
297                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
298         },
299         {
300                 .entity         = UVC_GUID_UVC_CAMERA,
301                 .selector       = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
302                 .index          = 11,
303                 .size           = 8,
304                 .flags          = UVC_CTRL_FLAG_SET_CUR
305                                 | UVC_CTRL_FLAG_GET_RANGE
306                                 | UVC_CTRL_FLAG_RESTORE
307                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
308         },
309         {
310                 .entity         = UVC_GUID_UVC_CAMERA,
311                 .selector       = UVC_CT_PANTILT_RELATIVE_CONTROL,
312                 .index          = 12,
313                 .size           = 4,
314                 .flags          = UVC_CTRL_FLAG_SET_CUR
315                                 | UVC_CTRL_FLAG_GET_RANGE
316                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
317         },
318         {
319                 .entity         = UVC_GUID_UVC_CAMERA,
320                 .selector       = UVC_CT_ROLL_ABSOLUTE_CONTROL,
321                 .index          = 13,
322                 .size           = 2,
323                 .flags          = UVC_CTRL_FLAG_SET_CUR
324                                 | UVC_CTRL_FLAG_GET_RANGE
325                                 | UVC_CTRL_FLAG_RESTORE
326                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
327         },
328         {
329                 .entity         = UVC_GUID_UVC_CAMERA,
330                 .selector       = UVC_CT_ROLL_RELATIVE_CONTROL,
331                 .index          = 14,
332                 .size           = 2,
333                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
334                                 | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
335                                 | UVC_CTRL_FLAG_GET_DEF
336                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
337         },
338         {
339                 .entity         = UVC_GUID_UVC_CAMERA,
340                 .selector       = UVC_CT_FOCUS_AUTO_CONTROL,
341                 .index          = 17,
342                 .size           = 1,
343                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
344                                 | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
345         },
346         {
347                 .entity         = UVC_GUID_UVC_CAMERA,
348                 .selector       = UVC_CT_PRIVACY_CONTROL,
349                 .index          = 18,
350                 .size           = 1,
351                 .flags          = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
352                                 | UVC_CTRL_FLAG_RESTORE
353                                 | UVC_CTRL_FLAG_AUTO_UPDATE,
354         },
355 };
356
357 static struct uvc_menu_info power_line_frequency_controls[] = {
358         { 0, "Disabled" },
359         { 1, "50 Hz" },
360         { 2, "60 Hz" },
361 };
362
363 static struct uvc_menu_info exposure_auto_controls[] = {
364         { 2, "Auto Mode" },
365         { 1, "Manual Mode" },
366         { 4, "Shutter Priority Mode" },
367         { 8, "Aperture Priority Mode" },
368 };
369
370 static s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
371         u8 query, const u8 *data)
372 {
373         s8 zoom = (s8)data[0];
374
375         switch (query) {
376         case UVC_GET_CUR:
377                 return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
378
379         case UVC_GET_MIN:
380         case UVC_GET_MAX:
381         case UVC_GET_RES:
382         case UVC_GET_DEF:
383         default:
384                 return data[2];
385         }
386 }
387
388 static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
389         s32 value, u8 *data)
390 {
391         data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
392         data[2] = min((int)abs(value), 0xff);
393 }
394
395 static s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
396         u8 query, const u8 *data)
397 {
398         unsigned int first = mapping->offset / 8;
399         s8 rel = (s8)data[first];
400
401         switch (query) {
402         case UVC_GET_CUR:
403                 return (rel == 0) ? 0 : (rel > 0 ? data[first+1]
404                                                  : -data[first+1]);
405         case UVC_GET_MIN:
406                 return -data[first+1];
407         case UVC_GET_MAX:
408         case UVC_GET_RES:
409         case UVC_GET_DEF:
410         default:
411                 return data[first+1];
412         }
413 }
414
415 static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
416         s32 value, u8 *data)
417 {
418         unsigned int first = mapping->offset / 8;
419
420         data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
421         data[first+1] = min_t(int, abs(value), 0xff);
422 }
423
424 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
425         {
426                 .id             = V4L2_CID_BRIGHTNESS,
427                 .name           = "Brightness",
428                 .entity         = UVC_GUID_UVC_PROCESSING,
429                 .selector       = UVC_PU_BRIGHTNESS_CONTROL,
430                 .size           = 16,
431                 .offset         = 0,
432                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
433                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
434         },
435         {
436                 .id             = V4L2_CID_CONTRAST,
437                 .name           = "Contrast",
438                 .entity         = UVC_GUID_UVC_PROCESSING,
439                 .selector       = UVC_PU_CONTRAST_CONTROL,
440                 .size           = 16,
441                 .offset         = 0,
442                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
443                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
444         },
445         {
446                 .id             = V4L2_CID_HUE,
447                 .name           = "Hue",
448                 .entity         = UVC_GUID_UVC_PROCESSING,
449                 .selector       = UVC_PU_HUE_CONTROL,
450                 .size           = 16,
451                 .offset         = 0,
452                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
453                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
454                 .master_id      = V4L2_CID_HUE_AUTO,
455                 .master_manual  = 0,
456         },
457         {
458                 .id             = V4L2_CID_SATURATION,
459                 .name           = "Saturation",
460                 .entity         = UVC_GUID_UVC_PROCESSING,
461                 .selector       = UVC_PU_SATURATION_CONTROL,
462                 .size           = 16,
463                 .offset         = 0,
464                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
465                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
466         },
467         {
468                 .id             = V4L2_CID_SHARPNESS,
469                 .name           = "Sharpness",
470                 .entity         = UVC_GUID_UVC_PROCESSING,
471                 .selector       = UVC_PU_SHARPNESS_CONTROL,
472                 .size           = 16,
473                 .offset         = 0,
474                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
475                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
476         },
477         {
478                 .id             = V4L2_CID_GAMMA,
479                 .name           = "Gamma",
480                 .entity         = UVC_GUID_UVC_PROCESSING,
481                 .selector       = UVC_PU_GAMMA_CONTROL,
482                 .size           = 16,
483                 .offset         = 0,
484                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
485                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
486         },
487         {
488                 .id             = V4L2_CID_BACKLIGHT_COMPENSATION,
489                 .name           = "Backlight Compensation",
490                 .entity         = UVC_GUID_UVC_PROCESSING,
491                 .selector       = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
492                 .size           = 16,
493                 .offset         = 0,
494                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
495                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
496         },
497         {
498                 .id             = V4L2_CID_GAIN,
499                 .name           = "Gain",
500                 .entity         = UVC_GUID_UVC_PROCESSING,
501                 .selector       = UVC_PU_GAIN_CONTROL,
502                 .size           = 16,
503                 .offset         = 0,
504                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
505                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
506         },
507         {
508                 .id             = V4L2_CID_POWER_LINE_FREQUENCY,
509                 .name           = "Power Line Frequency",
510                 .entity         = UVC_GUID_UVC_PROCESSING,
511                 .selector       = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
512                 .size           = 2,
513                 .offset         = 0,
514                 .v4l2_type      = V4L2_CTRL_TYPE_MENU,
515                 .data_type      = UVC_CTRL_DATA_TYPE_ENUM,
516                 .menu_info      = power_line_frequency_controls,
517                 .menu_count     = ARRAY_SIZE(power_line_frequency_controls),
518         },
519         {
520                 .id             = V4L2_CID_HUE_AUTO,
521                 .name           = "Hue, Auto",
522                 .entity         = UVC_GUID_UVC_PROCESSING,
523                 .selector       = UVC_PU_HUE_AUTO_CONTROL,
524                 .size           = 1,
525                 .offset         = 0,
526                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
527                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
528                 .slave_ids      = { V4L2_CID_HUE, },
529         },
530         {
531                 .id             = V4L2_CID_EXPOSURE_AUTO,
532                 .name           = "Exposure, Auto",
533                 .entity         = UVC_GUID_UVC_CAMERA,
534                 .selector       = UVC_CT_AE_MODE_CONTROL,
535                 .size           = 4,
536                 .offset         = 0,
537                 .v4l2_type      = V4L2_CTRL_TYPE_MENU,
538                 .data_type      = UVC_CTRL_DATA_TYPE_BITMASK,
539                 .menu_info      = exposure_auto_controls,
540                 .menu_count     = ARRAY_SIZE(exposure_auto_controls),
541                 .slave_ids      = { V4L2_CID_EXPOSURE_ABSOLUTE, },
542         },
543         {
544                 .id             = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
545                 .name           = "Exposure, Auto Priority",
546                 .entity         = UVC_GUID_UVC_CAMERA,
547                 .selector       = UVC_CT_AE_PRIORITY_CONTROL,
548                 .size           = 1,
549                 .offset         = 0,
550                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
551                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
552         },
553         {
554                 .id             = V4L2_CID_EXPOSURE_ABSOLUTE,
555                 .name           = "Exposure (Absolute)",
556                 .entity         = UVC_GUID_UVC_CAMERA,
557                 .selector       = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
558                 .size           = 32,
559                 .offset         = 0,
560                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
561                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
562                 .master_id      = V4L2_CID_EXPOSURE_AUTO,
563                 .master_manual  = V4L2_EXPOSURE_MANUAL,
564         },
565         {
566                 .id             = V4L2_CID_AUTO_WHITE_BALANCE,
567                 .name           = "White Balance Temperature, Auto",
568                 .entity         = UVC_GUID_UVC_PROCESSING,
569                 .selector       = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
570                 .size           = 1,
571                 .offset         = 0,
572                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
573                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
574                 .slave_ids      = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
575         },
576         {
577                 .id             = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
578                 .name           = "White Balance Temperature",
579                 .entity         = UVC_GUID_UVC_PROCESSING,
580                 .selector       = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
581                 .size           = 16,
582                 .offset         = 0,
583                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
584                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
585                 .master_id      = V4L2_CID_AUTO_WHITE_BALANCE,
586                 .master_manual  = 0,
587         },
588         {
589                 .id             = V4L2_CID_AUTO_WHITE_BALANCE,
590                 .name           = "White Balance Component, Auto",
591                 .entity         = UVC_GUID_UVC_PROCESSING,
592                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
593                 .size           = 1,
594                 .offset         = 0,
595                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
596                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
597                 .slave_ids      = { V4L2_CID_BLUE_BALANCE,
598                                     V4L2_CID_RED_BALANCE },
599         },
600         {
601                 .id             = V4L2_CID_BLUE_BALANCE,
602                 .name           = "White Balance Blue Component",
603                 .entity         = UVC_GUID_UVC_PROCESSING,
604                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
605                 .size           = 16,
606                 .offset         = 0,
607                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
608                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
609                 .master_id      = V4L2_CID_AUTO_WHITE_BALANCE,
610                 .master_manual  = 0,
611         },
612         {
613                 .id             = V4L2_CID_RED_BALANCE,
614                 .name           = "White Balance Red Component",
615                 .entity         = UVC_GUID_UVC_PROCESSING,
616                 .selector       = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
617                 .size           = 16,
618                 .offset         = 16,
619                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
620                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
621                 .master_id      = V4L2_CID_AUTO_WHITE_BALANCE,
622                 .master_manual  = 0,
623         },
624         {
625                 .id             = V4L2_CID_FOCUS_ABSOLUTE,
626                 .name           = "Focus (absolute)",
627                 .entity         = UVC_GUID_UVC_CAMERA,
628                 .selector       = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
629                 .size           = 16,
630                 .offset         = 0,
631                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
632                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
633                 .master_id      = V4L2_CID_FOCUS_AUTO,
634                 .master_manual  = 0,
635         },
636         {
637                 .id             = V4L2_CID_FOCUS_AUTO,
638                 .name           = "Focus, Auto",
639                 .entity         = UVC_GUID_UVC_CAMERA,
640                 .selector       = UVC_CT_FOCUS_AUTO_CONTROL,
641                 .size           = 1,
642                 .offset         = 0,
643                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
644                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
645                 .slave_ids      = { V4L2_CID_FOCUS_ABSOLUTE, },
646         },
647         {
648                 .id             = V4L2_CID_IRIS_ABSOLUTE,
649                 .name           = "Iris, Absolute",
650                 .entity         = UVC_GUID_UVC_CAMERA,
651                 .selector       = UVC_CT_IRIS_ABSOLUTE_CONTROL,
652                 .size           = 16,
653                 .offset         = 0,
654                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
655                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
656         },
657         {
658                 .id             = V4L2_CID_IRIS_RELATIVE,
659                 .name           = "Iris, Relative",
660                 .entity         = UVC_GUID_UVC_CAMERA,
661                 .selector       = UVC_CT_IRIS_RELATIVE_CONTROL,
662                 .size           = 8,
663                 .offset         = 0,
664                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
665                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
666         },
667         {
668                 .id             = V4L2_CID_ZOOM_ABSOLUTE,
669                 .name           = "Zoom, Absolute",
670                 .entity         = UVC_GUID_UVC_CAMERA,
671                 .selector       = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
672                 .size           = 16,
673                 .offset         = 0,
674                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
675                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
676         },
677         {
678                 .id             = V4L2_CID_ZOOM_CONTINUOUS,
679                 .name           = "Zoom, Continuous",
680                 .entity         = UVC_GUID_UVC_CAMERA,
681                 .selector       = UVC_CT_ZOOM_RELATIVE_CONTROL,
682                 .size           = 0,
683                 .offset         = 0,
684                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
685                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
686                 .get            = uvc_ctrl_get_zoom,
687                 .set            = uvc_ctrl_set_zoom,
688         },
689         {
690                 .id             = V4L2_CID_PAN_ABSOLUTE,
691                 .name           = "Pan (Absolute)",
692                 .entity         = UVC_GUID_UVC_CAMERA,
693                 .selector       = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
694                 .size           = 32,
695                 .offset         = 0,
696                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
697                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
698         },
699         {
700                 .id             = V4L2_CID_TILT_ABSOLUTE,
701                 .name           = "Tilt (Absolute)",
702                 .entity         = UVC_GUID_UVC_CAMERA,
703                 .selector       = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
704                 .size           = 32,
705                 .offset         = 32,
706                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
707                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
708         },
709         {
710                 .id             = V4L2_CID_PAN_SPEED,
711                 .name           = "Pan (Speed)",
712                 .entity         = UVC_GUID_UVC_CAMERA,
713                 .selector       = UVC_CT_PANTILT_RELATIVE_CONTROL,
714                 .size           = 16,
715                 .offset         = 0,
716                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
717                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
718                 .get            = uvc_ctrl_get_rel_speed,
719                 .set            = uvc_ctrl_set_rel_speed,
720         },
721         {
722                 .id             = V4L2_CID_TILT_SPEED,
723                 .name           = "Tilt (Speed)",
724                 .entity         = UVC_GUID_UVC_CAMERA,
725                 .selector       = UVC_CT_PANTILT_RELATIVE_CONTROL,
726                 .size           = 16,
727                 .offset         = 16,
728                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
729                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
730                 .get            = uvc_ctrl_get_rel_speed,
731                 .set            = uvc_ctrl_set_rel_speed,
732         },
733         {
734                 .id             = V4L2_CID_PRIVACY,
735                 .name           = "Privacy",
736                 .entity         = UVC_GUID_UVC_CAMERA,
737                 .selector       = UVC_CT_PRIVACY_CONTROL,
738                 .size           = 1,
739                 .offset         = 0,
740                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
741                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
742         },
743 };
744
745 /* ------------------------------------------------------------------------
746  * Utility functions
747  */
748
749 static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
750 {
751         return ctrl->uvc_data + id * ctrl->info.size;
752 }
753
754 static inline int uvc_test_bit(const u8 *data, int bit)
755 {
756         return (data[bit >> 3] >> (bit & 7)) & 1;
757 }
758
759 static inline void uvc_clear_bit(u8 *data, int bit)
760 {
761         data[bit >> 3] &= ~(1 << (bit & 7));
762 }
763
764 /* Extract the bit string specified by mapping->offset and mapping->size
765  * from the little-endian data stored at 'data' and return the result as
766  * a signed 32bit integer. Sign extension will be performed if the mapping
767  * references a signed data type.
768  */
769 static s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
770         u8 query, const u8 *data)
771 {
772         int bits = mapping->size;
773         int offset = mapping->offset;
774         s32 value = 0;
775         u8 mask;
776
777         data += offset / 8;
778         offset &= 7;
779         mask = ((1LL << bits) - 1) << offset;
780
781         while (1) {
782                 u8 byte = *data & mask;
783                 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
784                 bits -= 8 - (offset > 0 ? offset : 0);
785                 if (bits <= 0)
786                         break;
787
788                 offset -= 8;
789                 mask = (1 << bits) - 1;
790                 data++;
791         }
792
793         /* Sign-extend the value if needed. */
794         if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
795                 value |= -(value & (1 << (mapping->size - 1)));
796
797         return value;
798 }
799
800 /* Set the bit string specified by mapping->offset and mapping->size
801  * in the little-endian data stored at 'data' to the value 'value'.
802  */
803 static void uvc_set_le_value(struct uvc_control_mapping *mapping,
804         s32 value, u8 *data)
805 {
806         int bits = mapping->size;
807         int offset = mapping->offset;
808         u8 mask;
809
810         /* According to the v4l2 spec, writing any value to a button control
811          * should result in the action belonging to the button control being
812          * triggered. UVC devices however want to see a 1 written -> override
813          * value.
814          */
815         if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
816                 value = -1;
817
818         data += offset / 8;
819         offset &= 7;
820
821         for (; bits > 0; data++) {
822                 mask = ((1LL << bits) - 1) << offset;
823                 *data = (*data & ~mask) | ((value << offset) & mask);
824                 value >>= offset ? offset : 8;
825                 bits -= 8 - offset;
826                 offset = 0;
827         }
828 }
829
830 /* ------------------------------------------------------------------------
831  * Terminal and unit management
832  */
833
834 static const u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
835 static const u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
836 static const u8 uvc_media_transport_input_guid[16] =
837         UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
838
839 static int uvc_entity_match_guid(const struct uvc_entity *entity,
840         const u8 guid[16])
841 {
842         switch (UVC_ENTITY_TYPE(entity)) {
843         case UVC_ITT_CAMERA:
844                 return memcmp(uvc_camera_guid, guid, 16) == 0;
845
846         case UVC_ITT_MEDIA_TRANSPORT_INPUT:
847                 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
848
849         case UVC_VC_PROCESSING_UNIT:
850                 return memcmp(uvc_processing_guid, guid, 16) == 0;
851
852         case UVC_VC_EXTENSION_UNIT:
853                 return memcmp(entity->extension.guidExtensionCode,
854                               guid, 16) == 0;
855
856         default:
857                 return 0;
858         }
859 }
860
861 /* ------------------------------------------------------------------------
862  * UVC Controls
863  */
864
865 static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id,
866         struct uvc_control_mapping **mapping, struct uvc_control **control,
867         int next)
868 {
869         struct uvc_control *ctrl;
870         struct uvc_control_mapping *map;
871         unsigned int i;
872
873         if (entity == NULL)
874                 return;
875
876         for (i = 0; i < entity->ncontrols; ++i) {
877                 ctrl = &entity->controls[i];
878                 if (!ctrl->initialized)
879                         continue;
880
881                 list_for_each_entry(map, &ctrl->info.mappings, list) {
882                         if ((map->id == v4l2_id) && !next) {
883                                 *control = ctrl;
884                                 *mapping = map;
885                                 return;
886                         }
887
888                         if ((*mapping == NULL || (*mapping)->id > map->id) &&
889                             (map->id > v4l2_id) && next) {
890                                 *control = ctrl;
891                                 *mapping = map;
892                         }
893                 }
894         }
895 }
896
897 static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
898         u32 v4l2_id, struct uvc_control_mapping **mapping)
899 {
900         struct uvc_control *ctrl = NULL;
901         struct uvc_entity *entity;
902         int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
903
904         *mapping = NULL;
905
906         /* Mask the query flags. */
907         v4l2_id &= V4L2_CTRL_ID_MASK;
908
909         /* Find the control. */
910         list_for_each_entry(entity, &chain->entities, chain) {
911                 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
912                 if (ctrl && !next)
913                         return ctrl;
914         }
915
916         if (ctrl == NULL && !next)
917                 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
918                                 v4l2_id);
919
920         return ctrl;
921 }
922
923 static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
924         struct uvc_control *ctrl)
925 {
926         int ret;
927
928         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
929                 ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
930                                      chain->dev->intfnum, ctrl->info.selector,
931                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
932                                      ctrl->info.size);
933                 if (ret < 0)
934                         return ret;
935         }
936
937         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
938                 ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
939                                      chain->dev->intfnum, ctrl->info.selector,
940                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
941                                      ctrl->info.size);
942                 if (ret < 0)
943                         return ret;
944         }
945         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
946                 ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
947                                      chain->dev->intfnum, ctrl->info.selector,
948                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
949                                      ctrl->info.size);
950                 if (ret < 0)
951                         return ret;
952         }
953         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
954                 ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
955                                      chain->dev->intfnum, ctrl->info.selector,
956                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
957                                      ctrl->info.size);
958                 if (ret < 0) {
959                         if (UVC_ENTITY_TYPE(ctrl->entity) !=
960                             UVC_VC_EXTENSION_UNIT)
961                                 return ret;
962
963                         /* GET_RES is mandatory for XU controls, but some
964                          * cameras still choke on it. Ignore errors and set the
965                          * resolution value to zero.
966                          */
967                         uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
968                                       "UVC non compliance - GET_RES failed on "
969                                       "an XU control. Enabling workaround.\n");
970                         memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
971                                ctrl->info.size);
972                 }
973         }
974
975         ctrl->cached = 1;
976         return 0;
977 }
978
979 static s32 __uvc_ctrl_get_value(struct uvc_control_mapping *mapping,
980                                 const u8 *data)
981 {
982         s32 value = mapping->get(mapping, UVC_GET_CUR, data);
983
984         if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
985                 struct uvc_menu_info *menu = mapping->menu_info;
986                 unsigned int i;
987
988                 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
989                         if (menu->value == value) {
990                                 value = i;
991                                 break;
992                         }
993                 }
994         }
995
996         return value;
997 }
998
999 static int __uvc_ctrl_get(struct uvc_video_chain *chain,
1000         struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
1001         s32 *value)
1002 {
1003         int ret;
1004
1005         if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
1006                 return -EACCES;
1007
1008         if (!ctrl->loaded) {
1009                 ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
1010                                 chain->dev->intfnum, ctrl->info.selector,
1011                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1012                                 ctrl->info.size);
1013                 if (ret < 0)
1014                         return ret;
1015
1016                 ctrl->loaded = 1;
1017         }
1018
1019         *value = __uvc_ctrl_get_value(mapping,
1020                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1021
1022         return 0;
1023 }
1024
1025 static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1026         struct uvc_control *ctrl,
1027         struct uvc_control_mapping *mapping,
1028         struct v4l2_queryctrl *v4l2_ctrl)
1029 {
1030         struct uvc_control_mapping *master_map = NULL;
1031         struct uvc_control *master_ctrl = NULL;
1032         struct uvc_menu_info *menu;
1033         unsigned int i;
1034
1035         memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
1036         v4l2_ctrl->id = mapping->id;
1037         v4l2_ctrl->type = mapping->v4l2_type;
1038         strlcpy(v4l2_ctrl->name, mapping->name, sizeof(v4l2_ctrl->name));
1039         v4l2_ctrl->flags = 0;
1040
1041         if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1042                 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
1043         if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1044                 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1045
1046         if (mapping->master_id)
1047                 __uvc_find_control(ctrl->entity, mapping->master_id,
1048                                    &master_map, &master_ctrl, 0);
1049         if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
1050                 s32 val;
1051                 int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
1052                 if (ret < 0)
1053                         return ret;
1054
1055                 if (val != mapping->master_manual)
1056                                 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1057         }
1058
1059         if (!ctrl->cached) {
1060                 int ret = uvc_ctrl_populate_cache(chain, ctrl);
1061                 if (ret < 0)
1062                         return ret;
1063         }
1064
1065         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
1066                 v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
1067                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
1068         }
1069
1070         switch (mapping->v4l2_type) {
1071         case V4L2_CTRL_TYPE_MENU:
1072                 v4l2_ctrl->minimum = 0;
1073                 v4l2_ctrl->maximum = mapping->menu_count - 1;
1074                 v4l2_ctrl->step = 1;
1075
1076                 menu = mapping->menu_info;
1077                 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
1078                         if (menu->value == v4l2_ctrl->default_value) {
1079                                 v4l2_ctrl->default_value = i;
1080                                 break;
1081                         }
1082                 }
1083
1084                 return 0;
1085
1086         case V4L2_CTRL_TYPE_BOOLEAN:
1087                 v4l2_ctrl->minimum = 0;
1088                 v4l2_ctrl->maximum = 1;
1089                 v4l2_ctrl->step = 1;
1090                 return 0;
1091
1092         case V4L2_CTRL_TYPE_BUTTON:
1093                 v4l2_ctrl->minimum = 0;
1094                 v4l2_ctrl->maximum = 0;
1095                 v4l2_ctrl->step = 0;
1096                 return 0;
1097
1098         default:
1099                 break;
1100         }
1101
1102         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
1103                 v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
1104                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1105
1106         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
1107                 v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
1108                                      uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1109
1110         if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
1111                 v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
1112                                   uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1113
1114         return 0;
1115 }
1116
1117 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
1118         struct v4l2_queryctrl *v4l2_ctrl)
1119 {
1120         struct uvc_control *ctrl;
1121         struct uvc_control_mapping *mapping;
1122         int ret;
1123
1124         ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1125         if (ret < 0)
1126                 return -ERESTARTSYS;
1127
1128         ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
1129         if (ctrl == NULL) {
1130                 ret = -EINVAL;
1131                 goto done;
1132         }
1133
1134         ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
1135 done:
1136         mutex_unlock(&chain->ctrl_mutex);
1137         return ret;
1138 }
1139
1140 /*
1141  * Mapping V4L2 controls to UVC controls can be straightforward if done well.
1142  * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
1143  * must be grouped (for instance the Red Balance, Blue Balance and Do White
1144  * Balance V4L2 controls use the White Balance Component UVC control) or
1145  * otherwise translated. The approach we take here is to use a translation
1146  * table for the controls that can be mapped directly, and handle the others
1147  * manually.
1148  */
1149 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
1150         struct v4l2_querymenu *query_menu)
1151 {
1152         struct uvc_menu_info *menu_info;
1153         struct uvc_control_mapping *mapping;
1154         struct uvc_control *ctrl;
1155         u32 index = query_menu->index;
1156         u32 id = query_menu->id;
1157         int ret;
1158
1159         memset(query_menu, 0, sizeof(*query_menu));
1160         query_menu->id = id;
1161         query_menu->index = index;
1162
1163         ret = mutex_lock_interruptible(&chain->ctrl_mutex);
1164         if (ret < 0)
1165                 return -ERESTARTSYS;
1166
1167         ctrl = uvc_find_control(chain, query_menu->id, &mapping);
1168         if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
1169                 ret = -EINVAL;
1170                 goto done;
1171         }
1172
1173         if (query_menu->index >= mapping->menu_count) {
1174                 ret = -EINVAL;
1175                 goto done;
1176         }
1177
1178         menu_info = &mapping->menu_info[query_menu->index];
1179
1180         if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1181             (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1182                 s32 bitmap;
1183
1184                 if (!ctrl->cached) {
1185                         ret = uvc_ctrl_populate_cache(chain, ctrl);
1186                         if (ret < 0)
1187                                 goto done;
1188                 }
1189
1190                 bitmap = mapping->get(mapping, UVC_GET_RES,
1191                                       uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1192                 if (!(bitmap & menu_info->value)) {
1193                         ret = -EINVAL;
1194                         goto done;
1195                 }
1196         }
1197
1198         strlcpy(query_menu->name, menu_info->name, sizeof(query_menu->name));
1199
1200 done:
1201         mutex_unlock(&chain->ctrl_mutex);
1202         return ret;
1203 }
1204
1205 /* --------------------------------------------------------------------------
1206  * Ctrl event handling
1207  */
1208
1209 static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
1210         struct v4l2_event *ev,
1211         struct uvc_control *ctrl,
1212         struct uvc_control_mapping *mapping,
1213         s32 value, u32 changes)
1214 {
1215         struct v4l2_queryctrl v4l2_ctrl;
1216
1217         __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
1218
1219         memset(ev, 0, sizeof(*ev));
1220         ev->type = V4L2_EVENT_CTRL;
1221         ev->id = v4l2_ctrl.id;
1222         ev->u.ctrl.value = value;
1223         ev->u.ctrl.changes = changes;
1224         ev->u.ctrl.type = v4l2_ctrl.type;
1225         ev->u.ctrl.flags = v4l2_ctrl.flags;
1226         ev->u.ctrl.minimum = v4l2_ctrl.minimum;
1227         ev->u.ctrl.maximum = v4l2_ctrl.maximum;
1228         ev->u.ctrl.step = v4l2_ctrl.step;
1229         ev->u.ctrl.default_value = v4l2_ctrl.default_value;
1230 }
1231
1232 /*
1233  * Send control change events to all subscribers for the @ctrl control. By
1234  * default the subscriber that generated the event, as identified by @handle,
1235  * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
1236  * @handle can be NULL for asynchronous events related to auto-update controls,
1237  * in which case all subscribers are notified.
1238  */
1239 static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
1240         struct uvc_fh *handle, struct uvc_control *ctrl,
1241         struct uvc_control_mapping *mapping, s32 value, u32 changes)
1242 {
1243         struct v4l2_fh *originator = handle ? &handle->vfh : NULL;
1244         struct v4l2_subscribed_event *sev;
1245         struct v4l2_event ev;
1246
1247         if (list_empty(&mapping->ev_subs))
1248                 return;
1249
1250         uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes);
1251
1252         list_for_each_entry(sev, &mapping->ev_subs, node) {
1253                 if (sev->fh != originator ||
1254                     (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
1255                     (changes & V4L2_EVENT_CTRL_CH_FLAGS))
1256                         v4l2_event_queue_fh(sev->fh, &ev);
1257         }
1258 }
1259
1260 /*
1261  * Send control change events for the slave of the @master control identified
1262  * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
1263  * generated the event and may be NULL for auto-update events.
1264  */
1265 static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain,
1266         struct uvc_fh *handle, struct uvc_control *master, u32 slave_id)
1267 {
1268         struct uvc_control_mapping *mapping = NULL;
1269         struct uvc_control *ctrl = NULL;
1270         u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1271         s32 val = 0;
1272
1273         __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
1274         if (ctrl == NULL)
1275                 return;
1276
1277         if (__uvc_ctrl_get(chain, ctrl, mapping, &val) == 0)
1278                 changes |= V4L2_EVENT_CTRL_CH_VALUE;
1279
1280         uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes);
1281 }
1282
1283 static void uvc_ctrl_status_event_work(struct work_struct *work)
1284 {
1285         struct uvc_device *dev = container_of(work, struct uvc_device,
1286                                               async_ctrl.work);
1287         struct uvc_ctrl_work *w = &dev->async_ctrl;
1288         struct uvc_video_chain *chain = w->chain;
1289         struct uvc_control_mapping *mapping;
1290         struct uvc_control *ctrl = w->ctrl;
1291         struct uvc_fh *handle;
1292         unsigned int i;
1293         int ret;
1294
1295         mutex_lock(&chain->ctrl_mutex);
1296
1297         handle = ctrl->handle;
1298         ctrl->handle = NULL;
1299
1300         list_for_each_entry(mapping, &ctrl->info.mappings, list) {
1301                 s32 value = __uvc_ctrl_get_value(mapping, w->data);
1302
1303                 /*
1304                  * handle may be NULL here if the device sends auto-update
1305                  * events without a prior related control set from userspace.
1306                  */
1307                 for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
1308                         if (!mapping->slave_ids[i])
1309                                 break;
1310
1311                         uvc_ctrl_send_slave_event(chain, handle, ctrl,
1312                                                   mapping->slave_ids[i]);
1313                 }
1314
1315                 uvc_ctrl_send_event(chain, handle, ctrl, mapping, value,
1316                                     V4L2_EVENT_CTRL_CH_VALUE);
1317         }
1318
1319         mutex_unlock(&chain->ctrl_mutex);
1320
1321         /* Resubmit the URB. */
1322         w->urb->interval = dev->int_ep->desc.bInterval;
1323         ret = usb_submit_urb(w->urb, GFP_KERNEL);
1324         if (ret < 0)
1325                 uvc_printk(KERN_ERR, "Failed to resubmit status URB (%d).\n",
1326                            ret);
1327 }
1328
1329 bool uvc_ctrl_status_event(struct urb *urb, struct uvc_video_chain *chain,
1330                            struct uvc_control *ctrl, const u8 *data)
1331 {
1332         struct uvc_device *dev = chain->dev;
1333         struct uvc_ctrl_work *w = &dev->async_ctrl;
1334
1335         if (list_empty(&ctrl->info.mappings)) {
1336                 ctrl->handle = NULL;
1337                 return false;
1338         }
1339
1340         w->data = data;
1341         w->urb = urb;
1342         w->chain = chain;
1343         w->ctrl = ctrl;
1344
1345         schedule_work(&w->work);
1346
1347         return true;
1348 }
1349
1350 static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls,
1351                                         unsigned int xctrls_count, u32 id)
1352 {
1353         unsigned int i;
1354
1355         for (i = 0; i < xctrls_count; ++i) {
1356                 if (xctrls[i].id == id)
1357                         return true;
1358         }
1359
1360         return false;
1361 }
1362
1363 static void uvc_ctrl_send_events(struct uvc_fh *handle,
1364         const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
1365 {
1366         struct uvc_control_mapping *mapping;
1367         struct uvc_control *ctrl;
1368         u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
1369         unsigned int i;
1370         unsigned int j;
1371
1372         for (i = 0; i < xctrls_count; ++i) {
1373                 ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
1374
1375                 if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1376                         /* Notification will be sent from an Interrupt event. */
1377                         continue;
1378
1379                 for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
1380                         u32 slave_id = mapping->slave_ids[j];
1381
1382                         if (!slave_id)
1383                                 break;
1384
1385                         /*
1386                          * We can skip sending an event for the slave if the
1387                          * slave is being modified in the same transaction.
1388                          */
1389                         if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1390                                                         slave_id))
1391                                 continue;
1392
1393                         uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
1394                                                   slave_id);
1395                 }
1396
1397                 /*
1398                  * If the master is being modified in the same transaction
1399                  * flags may change too.
1400                  */
1401                 if (mapping->master_id &&
1402                     uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
1403                                                 mapping->master_id))
1404                         changes |= V4L2_EVENT_CTRL_CH_FLAGS;
1405
1406                 uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
1407                                     xctrls[i].value, changes);
1408         }
1409 }
1410
1411 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
1412 {
1413         struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1414         struct uvc_control_mapping *mapping;
1415         struct uvc_control *ctrl;
1416         int ret;
1417
1418         ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
1419         if (ret < 0)
1420                 return -ERESTARTSYS;
1421
1422         ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
1423         if (ctrl == NULL) {
1424                 ret = -EINVAL;
1425                 goto done;
1426         }
1427
1428         list_add_tail(&sev->node, &mapping->ev_subs);
1429         if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
1430                 struct v4l2_event ev;
1431                 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
1432                 s32 val = 0;
1433
1434                 if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
1435                         changes |= V4L2_EVENT_CTRL_CH_VALUE;
1436
1437                 uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
1438                                     changes);
1439                 /* Mark the queue as active, allowing this initial
1440                    event to be accepted. */
1441                 sev->elems = elems;
1442                 v4l2_event_queue_fh(sev->fh, &ev);
1443         }
1444
1445 done:
1446         mutex_unlock(&handle->chain->ctrl_mutex);
1447         return ret;
1448 }
1449
1450 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
1451 {
1452         struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
1453
1454         mutex_lock(&handle->chain->ctrl_mutex);
1455         list_del(&sev->node);
1456         mutex_unlock(&handle->chain->ctrl_mutex);
1457 }
1458
1459 const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
1460         .add = uvc_ctrl_add_event,
1461         .del = uvc_ctrl_del_event,
1462         .replace = v4l2_ctrl_replace,
1463         .merge = v4l2_ctrl_merge,
1464 };
1465
1466 /* --------------------------------------------------------------------------
1467  * Control transactions
1468  *
1469  * To make extended set operations as atomic as the hardware allows, controls
1470  * are handled using begin/commit/rollback operations.
1471  *
1472  * At the beginning of a set request, uvc_ctrl_begin should be called to
1473  * initialize the request. This function acquires the control lock.
1474  *
1475  * When setting a control, the new value is stored in the control data field
1476  * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
1477  * later processing. If the UVC and V4L2 control sizes differ, the current
1478  * value is loaded from the hardware before storing the new value in the data
1479  * field.
1480  *
1481  * After processing all controls in the transaction, uvc_ctrl_commit or
1482  * uvc_ctrl_rollback must be called to apply the pending changes to the
1483  * hardware or revert them. When applying changes, all controls marked as
1484  * dirty will be modified in the UVC device, and the dirty flag will be
1485  * cleared. When reverting controls, the control data field
1486  * UVC_CTRL_DATA_CURRENT is reverted to its previous value
1487  * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
1488  * control lock.
1489  */
1490 int uvc_ctrl_begin(struct uvc_video_chain *chain)
1491 {
1492         return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
1493 }
1494
1495 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
1496         struct uvc_entity *entity, int rollback)
1497 {
1498         struct uvc_control *ctrl;
1499         unsigned int i;
1500         int ret;
1501
1502         if (entity == NULL)
1503                 return 0;
1504
1505         for (i = 0; i < entity->ncontrols; ++i) {
1506                 ctrl = &entity->controls[i];
1507                 if (!ctrl->initialized)
1508                         continue;
1509
1510                 /* Reset the loaded flag for auto-update controls that were
1511                  * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
1512                  * uvc_ctrl_get from using the cached value, and for write-only
1513                  * controls to prevent uvc_ctrl_set from setting bits not
1514                  * explicitly set by the user.
1515                  */
1516                 if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
1517                     !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
1518                         ctrl->loaded = 0;
1519
1520                 if (!ctrl->dirty)
1521                         continue;
1522
1523                 if (!rollback)
1524                         ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
1525                                 dev->intfnum, ctrl->info.selector,
1526                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1527                                 ctrl->info.size);
1528                 else
1529                         ret = 0;
1530
1531                 if (rollback || ret < 0)
1532                         memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1533                                uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1534                                ctrl->info.size);
1535
1536                 ctrl->dirty = 0;
1537
1538                 if (ret < 0)
1539                         return ret;
1540         }
1541
1542         return 0;
1543 }
1544
1545 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
1546                       const struct v4l2_ext_control *xctrls,
1547                       unsigned int xctrls_count)
1548 {
1549         struct uvc_video_chain *chain = handle->chain;
1550         struct uvc_entity *entity;
1551         int ret = 0;
1552
1553         /* Find the control. */
1554         list_for_each_entry(entity, &chain->entities, chain) {
1555                 ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
1556                 if (ret < 0)
1557                         goto done;
1558         }
1559
1560         if (!rollback)
1561                 uvc_ctrl_send_events(handle, xctrls, xctrls_count);
1562 done:
1563         mutex_unlock(&chain->ctrl_mutex);
1564         return ret;
1565 }
1566
1567 int uvc_ctrl_get(struct uvc_video_chain *chain,
1568         struct v4l2_ext_control *xctrl)
1569 {
1570         struct uvc_control *ctrl;
1571         struct uvc_control_mapping *mapping;
1572
1573         ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1574         if (ctrl == NULL)
1575                 return -EINVAL;
1576
1577         return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
1578 }
1579
1580 int uvc_ctrl_set(struct uvc_fh *handle,
1581         struct v4l2_ext_control *xctrl)
1582 {
1583         struct uvc_video_chain *chain = handle->chain;
1584         struct uvc_control *ctrl;
1585         struct uvc_control_mapping *mapping;
1586         s32 value;
1587         u32 step;
1588         s32 min;
1589         s32 max;
1590         int ret;
1591
1592         ctrl = uvc_find_control(chain, xctrl->id, &mapping);
1593         if (ctrl == NULL)
1594                 return -EINVAL;
1595         if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
1596                 return -EACCES;
1597
1598         /* Clamp out of range values. */
1599         switch (mapping->v4l2_type) {
1600         case V4L2_CTRL_TYPE_INTEGER:
1601                 if (!ctrl->cached) {
1602                         ret = uvc_ctrl_populate_cache(chain, ctrl);
1603                         if (ret < 0)
1604                                 return ret;
1605                 }
1606
1607                 min = mapping->get(mapping, UVC_GET_MIN,
1608                                    uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
1609                 max = mapping->get(mapping, UVC_GET_MAX,
1610                                    uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
1611                 step = mapping->get(mapping, UVC_GET_RES,
1612                                     uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1613                 if (step == 0)
1614                         step = 1;
1615
1616                 xctrl->value = min + ((u32)(xctrl->value - min) + step / 2)
1617                              / step * step;
1618                 if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
1619                         xctrl->value = clamp(xctrl->value, min, max);
1620                 else
1621                         xctrl->value = clamp_t(u32, xctrl->value, min, max);
1622                 value = xctrl->value;
1623                 break;
1624
1625         case V4L2_CTRL_TYPE_BOOLEAN:
1626                 xctrl->value = clamp(xctrl->value, 0, 1);
1627                 value = xctrl->value;
1628                 break;
1629
1630         case V4L2_CTRL_TYPE_MENU:
1631                 if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
1632                         return -ERANGE;
1633                 value = mapping->menu_info[xctrl->value].value;
1634
1635                 /* Valid menu indices are reported by the GET_RES request for
1636                  * UVC controls that support it.
1637                  */
1638                 if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
1639                     (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
1640                         if (!ctrl->cached) {
1641                                 ret = uvc_ctrl_populate_cache(chain, ctrl);
1642                                 if (ret < 0)
1643                                         return ret;
1644                         }
1645
1646                         step = mapping->get(mapping, UVC_GET_RES,
1647                                         uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
1648                         if (!(step & value))
1649                                 return -EINVAL;
1650                 }
1651
1652                 break;
1653
1654         default:
1655                 value = xctrl->value;
1656                 break;
1657         }
1658
1659         /* If the mapping doesn't span the whole UVC control, the current value
1660          * needs to be loaded from the device to perform the read-modify-write
1661          * operation.
1662          */
1663         if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
1664                 if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
1665                         memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1666                                 0, ctrl->info.size);
1667                 } else {
1668                         ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
1669                                 ctrl->entity->id, chain->dev->intfnum,
1670                                 ctrl->info.selector,
1671                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1672                                 ctrl->info.size);
1673                         if (ret < 0)
1674                                 return ret;
1675                 }
1676
1677                 ctrl->loaded = 1;
1678         }
1679
1680         /* Backup the current value in case we need to rollback later. */
1681         if (!ctrl->dirty) {
1682                 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1683                        uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1684                        ctrl->info.size);
1685         }
1686
1687         mapping->set(mapping, value,
1688                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
1689
1690         if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
1691                 ctrl->handle = handle;
1692
1693         ctrl->dirty = 1;
1694         ctrl->modified = 1;
1695         return 0;
1696 }
1697
1698 /* --------------------------------------------------------------------------
1699  * Dynamic controls
1700  */
1701
1702 /*
1703  * Retrieve flags for a given control
1704  */
1705 static int uvc_ctrl_get_flags(struct uvc_device *dev,
1706                               const struct uvc_control *ctrl,
1707                               struct uvc_control_info *info)
1708 {
1709         u8 *data;
1710         int ret;
1711
1712         data = kmalloc(1, GFP_KERNEL);
1713         if (data == NULL)
1714                 return -ENOMEM;
1715
1716         ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
1717                              info->selector, data, 1);
1718         if (!ret)
1719                 info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
1720                                 UVC_CTRL_FLAG_GET_CUR : 0)
1721                             |  (data[0] & UVC_CONTROL_CAP_SET ?
1722                                 UVC_CTRL_FLAG_SET_CUR : 0)
1723                             |  (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
1724                                 UVC_CTRL_FLAG_AUTO_UPDATE : 0)
1725                             |  (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
1726                                 UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
1727
1728         kfree(data);
1729         return ret;
1730 }
1731
1732 static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
1733         const struct uvc_control *ctrl, struct uvc_control_info *info)
1734 {
1735         struct uvc_ctrl_fixup {
1736                 struct usb_device_id id;
1737                 u8 entity;
1738                 u8 selector;
1739                 u8 flags;
1740         };
1741
1742         static const struct uvc_ctrl_fixup fixups[] = {
1743                 { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
1744                         UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1745                         UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1746                         UVC_CTRL_FLAG_AUTO_UPDATE },
1747                 { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
1748                         UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1749                         UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1750                         UVC_CTRL_FLAG_AUTO_UPDATE },
1751                 { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
1752                         UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
1753                         UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
1754                         UVC_CTRL_FLAG_AUTO_UPDATE },
1755         };
1756
1757         unsigned int i;
1758
1759         for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
1760                 if (!usb_match_one_id(dev->intf, &fixups[i].id))
1761                         continue;
1762
1763                 if (fixups[i].entity == ctrl->entity->id &&
1764                     fixups[i].selector == info->selector) {
1765                         info->flags = fixups[i].flags;
1766                         return;
1767                 }
1768         }
1769 }
1770
1771 /*
1772  * Query control information (size and flags) for XU controls.
1773  */
1774 static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
1775         const struct uvc_control *ctrl, struct uvc_control_info *info)
1776 {
1777         u8 *data;
1778         int ret;
1779
1780         data = kmalloc(2, GFP_KERNEL);
1781         if (data == NULL)
1782                 return -ENOMEM;
1783
1784         memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
1785                sizeof(info->entity));
1786         info->index = ctrl->index;
1787         info->selector = ctrl->index + 1;
1788
1789         /* Query and verify the control length (GET_LEN) */
1790         ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
1791                              info->selector, data, 2);
1792         if (ret < 0) {
1793                 uvc_trace(UVC_TRACE_CONTROL,
1794                           "GET_LEN failed on control %pUl/%u (%d).\n",
1795                            info->entity, info->selector, ret);
1796                 goto done;
1797         }
1798
1799         info->size = le16_to_cpup((__le16 *)data);
1800
1801         info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
1802                     | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
1803
1804         ret = uvc_ctrl_get_flags(dev, ctrl, info);
1805         if (ret < 0) {
1806                 uvc_trace(UVC_TRACE_CONTROL,
1807                           "Failed to get flags for control %pUl/%u (%d).\n",
1808                           info->entity, info->selector, ret);
1809                 goto done;
1810         }
1811
1812         uvc_ctrl_fixup_xu_info(dev, ctrl, info);
1813
1814         uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
1815                   "flags { get %u set %u auto %u }.\n",
1816                   info->entity, info->selector, info->size,
1817                   (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
1818                   (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
1819                   (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
1820
1821 done:
1822         kfree(data);
1823         return ret;
1824 }
1825
1826 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
1827         const struct uvc_control_info *info);
1828
1829 static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
1830         struct uvc_control *ctrl)
1831 {
1832         struct uvc_control_info info;
1833         int ret;
1834
1835         if (ctrl->initialized)
1836                 return 0;
1837
1838         ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
1839         if (ret < 0)
1840                 return ret;
1841
1842         ret = uvc_ctrl_add_info(dev, ctrl, &info);
1843         if (ret < 0)
1844                 uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
1845                           "%pUl/%u on device %s entity %u\n", info.entity,
1846                           info.selector, dev->udev->devpath, ctrl->entity->id);
1847
1848         return ret;
1849 }
1850
1851 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
1852         struct uvc_xu_control_query *xqry)
1853 {
1854         struct uvc_entity *entity;
1855         struct uvc_control *ctrl;
1856         unsigned int i;
1857         bool found;
1858         u32 reqflags;
1859         u16 size;
1860         u8 *data = NULL;
1861         int ret;
1862
1863         /* Find the extension unit. */
1864         found = false;
1865         list_for_each_entry(entity, &chain->entities, chain) {
1866                 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
1867                     entity->id == xqry->unit) {
1868                         found = true;
1869                         break;
1870                 }
1871         }
1872
1873         if (!found) {
1874                 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1875                         xqry->unit);
1876                 return -ENOENT;
1877         }
1878
1879         /* Find the control and perform delayed initialization if needed. */
1880         found = false;
1881         for (i = 0; i < entity->ncontrols; ++i) {
1882                 ctrl = &entity->controls[i];
1883                 if (ctrl->index == xqry->selector - 1) {
1884                         found = true;
1885                         break;
1886                 }
1887         }
1888
1889         if (!found) {
1890                 uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
1891                         entity->extension.guidExtensionCode, xqry->selector);
1892                 return -ENOENT;
1893         }
1894
1895         if (mutex_lock_interruptible(&chain->ctrl_mutex))
1896                 return -ERESTARTSYS;
1897
1898         ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
1899         if (ret < 0) {
1900                 ret = -ENOENT;
1901                 goto done;
1902         }
1903
1904         /* Validate the required buffer size and flags for the request */
1905         reqflags = 0;
1906         size = ctrl->info.size;
1907
1908         switch (xqry->query) {
1909         case UVC_GET_CUR:
1910                 reqflags = UVC_CTRL_FLAG_GET_CUR;
1911                 break;
1912         case UVC_GET_MIN:
1913                 reqflags = UVC_CTRL_FLAG_GET_MIN;
1914                 break;
1915         case UVC_GET_MAX:
1916                 reqflags = UVC_CTRL_FLAG_GET_MAX;
1917                 break;
1918         case UVC_GET_DEF:
1919                 reqflags = UVC_CTRL_FLAG_GET_DEF;
1920                 break;
1921         case UVC_GET_RES:
1922                 reqflags = UVC_CTRL_FLAG_GET_RES;
1923                 break;
1924         case UVC_SET_CUR:
1925                 reqflags = UVC_CTRL_FLAG_SET_CUR;
1926                 break;
1927         case UVC_GET_LEN:
1928                 size = 2;
1929                 break;
1930         case UVC_GET_INFO:
1931                 size = 1;
1932                 break;
1933         default:
1934                 ret = -EINVAL;
1935                 goto done;
1936         }
1937
1938         if (size != xqry->size) {
1939                 ret = -ENOBUFS;
1940                 goto done;
1941         }
1942
1943         if (reqflags && !(ctrl->info.flags & reqflags)) {
1944                 ret = -EBADRQC;
1945                 goto done;
1946         }
1947
1948         data = kmalloc(size, GFP_KERNEL);
1949         if (data == NULL) {
1950                 ret = -ENOMEM;
1951                 goto done;
1952         }
1953
1954         if (xqry->query == UVC_SET_CUR &&
1955             copy_from_user(data, xqry->data, size)) {
1956                 ret = -EFAULT;
1957                 goto done;
1958         }
1959
1960         ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
1961                              chain->dev->intfnum, xqry->selector, data, size);
1962         if (ret < 0)
1963                 goto done;
1964
1965         if (xqry->query != UVC_SET_CUR &&
1966             copy_to_user(xqry->data, data, size))
1967                 ret = -EFAULT;
1968 done:
1969         kfree(data);
1970         mutex_unlock(&chain->ctrl_mutex);
1971         return ret;
1972 }
1973
1974 /* --------------------------------------------------------------------------
1975  * Suspend/resume
1976  */
1977
1978 /*
1979  * Restore control values after resume, skipping controls that haven't been
1980  * changed.
1981  *
1982  * TODO
1983  * - Don't restore modified controls that are back to their default value.
1984  * - Handle restore order (Auto-Exposure Mode should be restored before
1985  *   Exposure Time).
1986  */
1987 int uvc_ctrl_restore_values(struct uvc_device *dev)
1988 {
1989         struct uvc_control *ctrl;
1990         struct uvc_entity *entity;
1991         unsigned int i;
1992         int ret;
1993
1994         /* Walk the entities list and restore controls when possible. */
1995         list_for_each_entry(entity, &dev->entities, list) {
1996
1997                 for (i = 0; i < entity->ncontrols; ++i) {
1998                         ctrl = &entity->controls[i];
1999
2000                         if (!ctrl->initialized || !ctrl->modified ||
2001                             (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
2002                                 continue;
2003
2004                         printk(KERN_INFO "restoring control %pUl/%u/%u\n",
2005                                 ctrl->info.entity, ctrl->info.index,
2006                                 ctrl->info.selector);
2007                         ctrl->dirty = 1;
2008                 }
2009
2010                 ret = uvc_ctrl_commit_entity(dev, entity, 0);
2011                 if (ret < 0)
2012                         return ret;
2013         }
2014
2015         return 0;
2016 }
2017
2018 /* --------------------------------------------------------------------------
2019  * Control and mapping handling
2020  */
2021
2022 /*
2023  * Add control information to a given control.
2024  */
2025 static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
2026         const struct uvc_control_info *info)
2027 {
2028         int ret = 0;
2029
2030         ctrl->info = *info;
2031         INIT_LIST_HEAD(&ctrl->info.mappings);
2032
2033         /* Allocate an array to save control values (cur, def, max, etc.) */
2034         ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
2035                                  GFP_KERNEL);
2036         if (ctrl->uvc_data == NULL) {
2037                 ret = -ENOMEM;
2038                 goto done;
2039         }
2040
2041         ctrl->initialized = 1;
2042
2043         uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
2044                 "entity %u\n", ctrl->info.entity, ctrl->info.selector,
2045                 dev->udev->devpath, ctrl->entity->id);
2046
2047 done:
2048         if (ret < 0)
2049                 kfree(ctrl->uvc_data);
2050         return ret;
2051 }
2052
2053 /*
2054  * Add a control mapping to a given control.
2055  */
2056 static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
2057         struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
2058 {
2059         struct uvc_control_mapping *map;
2060         unsigned int size;
2061
2062         /* Most mappings come from static kernel data and need to be duplicated.
2063          * Mappings that come from userspace will be unnecessarily duplicated,
2064          * this could be optimized.
2065          */
2066         map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
2067         if (map == NULL)
2068                 return -ENOMEM;
2069
2070         INIT_LIST_HEAD(&map->ev_subs);
2071
2072         size = sizeof(*mapping->menu_info) * mapping->menu_count;
2073         map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
2074         if (map->menu_info == NULL) {
2075                 kfree(map);
2076                 return -ENOMEM;
2077         }
2078
2079         if (map->get == NULL)
2080                 map->get = uvc_get_le_value;
2081         if (map->set == NULL)
2082                 map->set = uvc_set_le_value;
2083
2084         list_add_tail(&map->list, &ctrl->info.mappings);
2085         uvc_trace(UVC_TRACE_CONTROL,
2086                 "Adding mapping '%s' to control %pUl/%u.\n",
2087                 map->name, ctrl->info.entity, ctrl->info.selector);
2088
2089         return 0;
2090 }
2091
2092 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
2093         const struct uvc_control_mapping *mapping)
2094 {
2095         struct uvc_device *dev = chain->dev;
2096         struct uvc_control_mapping *map;
2097         struct uvc_entity *entity;
2098         struct uvc_control *ctrl;
2099         int found = 0;
2100         int ret;
2101
2102         if (mapping->id & ~V4L2_CTRL_ID_MASK) {
2103                 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
2104                         "id 0x%08x is invalid.\n", mapping->name,
2105                         mapping->id);
2106                 return -EINVAL;
2107         }
2108
2109         /* Search for the matching (GUID/CS) control on the current chain */
2110         list_for_each_entry(entity, &chain->entities, chain) {
2111                 unsigned int i;
2112
2113                 if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
2114                     !uvc_entity_match_guid(entity, mapping->entity))
2115                         continue;
2116
2117                 for (i = 0; i < entity->ncontrols; ++i) {
2118                         ctrl = &entity->controls[i];
2119                         if (ctrl->index == mapping->selector - 1) {
2120                                 found = 1;
2121                                 break;
2122                         }
2123                 }
2124
2125                 if (found)
2126                         break;
2127         }
2128         if (!found)
2129                 return -ENOENT;
2130
2131         if (mutex_lock_interruptible(&chain->ctrl_mutex))
2132                 return -ERESTARTSYS;
2133
2134         /* Perform delayed initialization of XU controls */
2135         ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
2136         if (ret < 0) {
2137                 ret = -ENOENT;
2138                 goto done;
2139         }
2140
2141         /* Validate the user-provided bit-size and offset */
2142         if (mapping->size > 32 ||
2143             mapping->offset + mapping->size > ctrl->info.size * 8) {
2144                 ret = -EINVAL;
2145                 goto done;
2146         }
2147
2148         list_for_each_entry(map, &ctrl->info.mappings, list) {
2149                 if (mapping->id == map->id) {
2150                         uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
2151                                 "control id 0x%08x already exists.\n",
2152                                 mapping->name, mapping->id);
2153                         ret = -EEXIST;
2154                         goto done;
2155                 }
2156         }
2157
2158         /* Prevent excess memory consumption */
2159         if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
2160                 atomic_dec(&dev->nmappings);
2161                 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
2162                         "mappings count (%u) exceeded.\n", mapping->name,
2163                         UVC_MAX_CONTROL_MAPPINGS);
2164                 ret = -ENOMEM;
2165                 goto done;
2166         }
2167
2168         ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2169         if (ret < 0)
2170                 atomic_dec(&dev->nmappings);
2171
2172 done:
2173         mutex_unlock(&chain->ctrl_mutex);
2174         return ret;
2175 }
2176
2177 /*
2178  * Prune an entity of its bogus controls using a blacklist. Bogus controls
2179  * are currently the ones that crash the camera or unconditionally return an
2180  * error when queried.
2181  */
2182 static void uvc_ctrl_prune_entity(struct uvc_device *dev,
2183         struct uvc_entity *entity)
2184 {
2185         struct uvc_ctrl_blacklist {
2186                 struct usb_device_id id;
2187                 u8 index;
2188         };
2189
2190         static const struct uvc_ctrl_blacklist processing_blacklist[] = {
2191                 { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
2192                 { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
2193                 { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
2194         };
2195         static const struct uvc_ctrl_blacklist camera_blacklist[] = {
2196                 { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
2197         };
2198
2199         const struct uvc_ctrl_blacklist *blacklist;
2200         unsigned int size;
2201         unsigned int count;
2202         unsigned int i;
2203         u8 *controls;
2204
2205         switch (UVC_ENTITY_TYPE(entity)) {
2206         case UVC_VC_PROCESSING_UNIT:
2207                 blacklist = processing_blacklist;
2208                 count = ARRAY_SIZE(processing_blacklist);
2209                 controls = entity->processing.bmControls;
2210                 size = entity->processing.bControlSize;
2211                 break;
2212
2213         case UVC_ITT_CAMERA:
2214                 blacklist = camera_blacklist;
2215                 count = ARRAY_SIZE(camera_blacklist);
2216                 controls = entity->camera.bmControls;
2217                 size = entity->camera.bControlSize;
2218                 break;
2219
2220         default:
2221                 return;
2222         }
2223
2224         for (i = 0; i < count; ++i) {
2225                 if (!usb_match_one_id(dev->intf, &blacklist[i].id))
2226                         continue;
2227
2228                 if (blacklist[i].index >= 8 * size ||
2229                     !uvc_test_bit(controls, blacklist[i].index))
2230                         continue;
2231
2232                 uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
2233                         "removing it.\n", entity->id, blacklist[i].index);
2234
2235                 uvc_clear_bit(controls, blacklist[i].index);
2236         }
2237 }
2238
2239 /*
2240  * Add control information and hardcoded stock control mappings to the given
2241  * device.
2242  */
2243 static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
2244 {
2245         const struct uvc_control_info *info = uvc_ctrls;
2246         const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
2247         const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
2248         const struct uvc_control_mapping *mend =
2249                 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
2250
2251         /* XU controls initialization requires querying the device for control
2252          * information. As some buggy UVC devices will crash when queried
2253          * repeatedly in a tight loop, delay XU controls initialization until
2254          * first use.
2255          */
2256         if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
2257                 return;
2258
2259         for (; info < iend; ++info) {
2260                 if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
2261                     ctrl->index == info->index) {
2262                         uvc_ctrl_add_info(dev, ctrl, info);
2263                         /*
2264                          * Retrieve control flags from the device. Ignore errors
2265                          * and work with default flag values from the uvc_ctrl
2266                          * array when the device doesn't properly implement
2267                          * GET_INFO on standard controls.
2268                          */
2269                         uvc_ctrl_get_flags(dev, ctrl, &ctrl->info);
2270                         break;
2271                  }
2272         }
2273
2274         if (!ctrl->initialized)
2275                 return;
2276
2277         for (; mapping < mend; ++mapping) {
2278                 if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
2279                     ctrl->info.selector == mapping->selector)
2280                         __uvc_ctrl_add_mapping(dev, ctrl, mapping);
2281         }
2282 }
2283
2284 /*
2285  * Initialize device controls.
2286  */
2287 int uvc_ctrl_init_device(struct uvc_device *dev)
2288 {
2289         struct uvc_entity *entity;
2290         unsigned int i;
2291
2292         INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
2293
2294         /* Walk the entities list and instantiate controls */
2295         list_for_each_entry(entity, &dev->entities, list) {
2296                 struct uvc_control *ctrl;
2297                 unsigned int bControlSize = 0, ncontrols;
2298                 u8 *bmControls = NULL;
2299
2300                 if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
2301                         bmControls = entity->extension.bmControls;
2302                         bControlSize = entity->extension.bControlSize;
2303                 } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
2304                         bmControls = entity->processing.bmControls;
2305                         bControlSize = entity->processing.bControlSize;
2306                 } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
2307                         bmControls = entity->camera.bmControls;
2308                         bControlSize = entity->camera.bControlSize;
2309                 }
2310
2311                 /* Remove bogus/blacklisted controls */
2312                 uvc_ctrl_prune_entity(dev, entity);
2313
2314                 /* Count supported controls and allocate the controls array */
2315                 ncontrols = memweight(bmControls, bControlSize);
2316                 if (ncontrols == 0)
2317                         continue;
2318
2319                 entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
2320                                            GFP_KERNEL);
2321                 if (entity->controls == NULL)
2322                         return -ENOMEM;
2323                 entity->ncontrols = ncontrols;
2324
2325                 /* Initialize all supported controls */
2326                 ctrl = entity->controls;
2327                 for (i = 0; i < bControlSize * 8; ++i) {
2328                         if (uvc_test_bit(bmControls, i) == 0)
2329                                 continue;
2330
2331                         ctrl->entity = entity;
2332                         ctrl->index = i;
2333
2334                         uvc_ctrl_init_ctrl(dev, ctrl);
2335                         ctrl++;
2336                 }
2337         }
2338
2339         return 0;
2340 }
2341
2342 /*
2343  * Cleanup device controls.
2344  */
2345 static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
2346         struct uvc_control *ctrl)
2347 {
2348         struct uvc_control_mapping *mapping, *nm;
2349
2350         list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
2351                 list_del(&mapping->list);
2352                 kfree(mapping->menu_info);
2353                 kfree(mapping);
2354         }
2355 }
2356
2357 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
2358 {
2359         struct uvc_entity *entity;
2360         unsigned int i;
2361
2362         /* Can be uninitialized if we are aborting on probe error. */
2363         if (dev->async_ctrl.work.func)
2364                 cancel_work_sync(&dev->async_ctrl.work);
2365
2366         /* Free controls and control mappings for all entities. */
2367         list_for_each_entry(entity, &dev->entities, list) {
2368                 for (i = 0; i < entity->ncontrols; ++i) {
2369                         struct uvc_control *ctrl = &entity->controls[i];
2370
2371                         if (!ctrl->initialized)
2372                                 continue;
2373
2374                         uvc_ctrl_cleanup_mappings(dev, ctrl);
2375                         kfree(ctrl->uvc_data);
2376                 }
2377
2378                 kfree(entity->controls);
2379         }
2380 }