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