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