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