]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/media/video/uvc/uvc_ctrl.c
V4L/DVB (9810): uvcvideo: Add a device quirk to prune bogus controls.
[net-next-2.6.git] / drivers / media / video / uvc / uvc_ctrl.c
1 /*
2  *      uvc_ctrl.c  --  USB Video Class driver - Controls
3  *
4  *      Copyright (C) 2005-2008
5  *          Laurent Pinchart (laurent.pinchart@skynet.be)
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License as published by
9  *      the Free Software Foundation; either version 2 of the License, or
10  *      (at your option) any later version.
11  *
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/version.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/uaccess.h>
19 #include <linux/usb.h>
20 #include <linux/videodev2.h>
21 #include <linux/vmalloc.h>
22 #include <linux/wait.h>
23 #include <asm/atomic.h>
24
25 #include "uvcvideo.h"
26
27 #define UVC_CTRL_NDATA          2
28 #define UVC_CTRL_DATA_CURRENT   0
29 #define UVC_CTRL_DATA_BACKUP    1
30
31 /* ------------------------------------------------------------------------
32  * Control, formats, ...
33  */
34
35 static struct uvc_control_info uvc_ctrls[] = {
36         {
37                 .entity         = UVC_GUID_UVC_PROCESSING,
38                 .selector       = PU_BRIGHTNESS_CONTROL,
39                 .index          = 0,
40                 .size           = 2,
41                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
42                                 | UVC_CONTROL_RESTORE,
43         },
44         {
45                 .entity         = UVC_GUID_UVC_PROCESSING,
46                 .selector       = PU_CONTRAST_CONTROL,
47                 .index          = 1,
48                 .size           = 2,
49                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
50                                 | UVC_CONTROL_RESTORE,
51         },
52         {
53                 .entity         = UVC_GUID_UVC_PROCESSING,
54                 .selector       = PU_HUE_CONTROL,
55                 .index          = 2,
56                 .size           = 2,
57                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
58                                 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
59         },
60         {
61                 .entity         = UVC_GUID_UVC_PROCESSING,
62                 .selector       = PU_SATURATION_CONTROL,
63                 .index          = 3,
64                 .size           = 2,
65                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
66                                 | UVC_CONTROL_RESTORE,
67         },
68         {
69                 .entity         = UVC_GUID_UVC_PROCESSING,
70                 .selector       = PU_SHARPNESS_CONTROL,
71                 .index          = 4,
72                 .size           = 2,
73                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
74                                 | UVC_CONTROL_RESTORE,
75         },
76         {
77                 .entity         = UVC_GUID_UVC_PROCESSING,
78                 .selector       = PU_GAMMA_CONTROL,
79                 .index          = 5,
80                 .size           = 2,
81                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
82                                 | UVC_CONTROL_RESTORE,
83         },
84         {
85                 .entity         = UVC_GUID_UVC_PROCESSING,
86                 .selector       = PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
87                 .index          = 6,
88                 .size           = 2,
89                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
90                                 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
91         },
92         {
93                 .entity         = UVC_GUID_UVC_PROCESSING,
94                 .selector       = PU_WHITE_BALANCE_COMPONENT_CONTROL,
95                 .index          = 7,
96                 .size           = 4,
97                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
98                                 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
99         },
100         {
101                 .entity         = UVC_GUID_UVC_PROCESSING,
102                 .selector       = PU_BACKLIGHT_COMPENSATION_CONTROL,
103                 .index          = 8,
104                 .size           = 2,
105                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
106                                 | UVC_CONTROL_RESTORE,
107         },
108         {
109                 .entity         = UVC_GUID_UVC_PROCESSING,
110                 .selector       = PU_GAIN_CONTROL,
111                 .index          = 9,
112                 .size           = 2,
113                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
114                                 | UVC_CONTROL_RESTORE,
115         },
116         {
117                 .entity         = UVC_GUID_UVC_PROCESSING,
118                 .selector       = PU_POWER_LINE_FREQUENCY_CONTROL,
119                 .index          = 10,
120                 .size           = 1,
121                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
122                                 | UVC_CONTROL_RESTORE,
123         },
124         {
125                 .entity         = UVC_GUID_UVC_PROCESSING,
126                 .selector       = PU_HUE_AUTO_CONTROL,
127                 .index          = 11,
128                 .size           = 1,
129                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
130                                 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
131         },
132         {
133                 .entity         = UVC_GUID_UVC_PROCESSING,
134                 .selector       = PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
135                 .index          = 12,
136                 .size           = 1,
137                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
138                                 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
139         },
140         {
141                 .entity         = UVC_GUID_UVC_PROCESSING,
142                 .selector       = PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
143                 .index          = 13,
144                 .size           = 1,
145                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
146                                 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
147         },
148         {
149                 .entity         = UVC_GUID_UVC_PROCESSING,
150                 .selector       = PU_DIGITAL_MULTIPLIER_CONTROL,
151                 .index          = 14,
152                 .size           = 2,
153                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
154                                 | UVC_CONTROL_RESTORE,
155         },
156         {
157                 .entity         = UVC_GUID_UVC_PROCESSING,
158                 .selector       = PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
159                 .index          = 15,
160                 .size           = 2,
161                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
162                                 | UVC_CONTROL_RESTORE,
163         },
164         {
165                 .entity         = UVC_GUID_UVC_PROCESSING,
166                 .selector       = PU_ANALOG_VIDEO_STANDARD_CONTROL,
167                 .index          = 16,
168                 .size           = 1,
169                 .flags          = UVC_CONTROL_GET_CUR,
170         },
171         {
172                 .entity         = UVC_GUID_UVC_PROCESSING,
173                 .selector       = PU_ANALOG_LOCK_STATUS_CONTROL,
174                 .index          = 17,
175                 .size           = 1,
176                 .flags          = UVC_CONTROL_GET_CUR,
177         },
178         {
179                 .entity         = UVC_GUID_UVC_CAMERA,
180                 .selector       = CT_SCANNING_MODE_CONTROL,
181                 .index          = 0,
182                 .size           = 1,
183                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
184                                 | UVC_CONTROL_RESTORE,
185         },
186         {
187                 .entity         = UVC_GUID_UVC_CAMERA,
188                 .selector       = CT_AE_MODE_CONTROL,
189                 .index          = 1,
190                 .size           = 1,
191                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
192                                 | UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES
193                                 | UVC_CONTROL_RESTORE,
194         },
195         {
196                 .entity         = UVC_GUID_UVC_CAMERA,
197                 .selector       = CT_AE_PRIORITY_CONTROL,
198                 .index          = 2,
199                 .size           = 1,
200                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
201                                 | UVC_CONTROL_RESTORE,
202         },
203         {
204                 .entity         = UVC_GUID_UVC_CAMERA,
205                 .selector       = CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
206                 .index          = 3,
207                 .size           = 4,
208                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
209                                 | UVC_CONTROL_RESTORE,
210         },
211         {
212                 .entity         = UVC_GUID_UVC_CAMERA,
213                 .selector       = CT_EXPOSURE_TIME_RELATIVE_CONTROL,
214                 .index          = 4,
215                 .size           = 1,
216                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
217                                 | UVC_CONTROL_RESTORE,
218         },
219         {
220                 .entity         = UVC_GUID_UVC_CAMERA,
221                 .selector       = CT_FOCUS_ABSOLUTE_CONTROL,
222                 .index          = 5,
223                 .size           = 2,
224                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
225                                 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
226         },
227         {
228                 .entity         = UVC_GUID_UVC_CAMERA,
229                 .selector       = CT_FOCUS_RELATIVE_CONTROL,
230                 .index          = 6,
231                 .size           = 2,
232                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
233                                 | UVC_CONTROL_AUTO_UPDATE,
234         },
235         {
236                 .entity         = UVC_GUID_UVC_CAMERA,
237                 .selector       = CT_IRIS_ABSOLUTE_CONTROL,
238                 .index          = 7,
239                 .size           = 2,
240                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
241                                 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
242         },
243         {
244                 .entity         = UVC_GUID_UVC_CAMERA,
245                 .selector       = CT_IRIS_RELATIVE_CONTROL,
246                 .index          = 8,
247                 .size           = 1,
248                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
249                                 | UVC_CONTROL_AUTO_UPDATE,
250         },
251         {
252                 .entity         = UVC_GUID_UVC_CAMERA,
253                 .selector       = CT_ZOOM_ABSOLUTE_CONTROL,
254                 .index          = 9,
255                 .size           = 2,
256                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
257                                 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
258         },
259         {
260                 .entity         = UVC_GUID_UVC_CAMERA,
261                 .selector       = CT_ZOOM_RELATIVE_CONTROL,
262                 .index          = 10,
263                 .size           = 3,
264                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
265                                 | UVC_CONTROL_AUTO_UPDATE,
266         },
267         {
268                 .entity         = UVC_GUID_UVC_CAMERA,
269                 .selector       = CT_PANTILT_ABSOLUTE_CONTROL,
270                 .index          = 11,
271                 .size           = 8,
272                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
273                                 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
274         },
275         {
276                 .entity         = UVC_GUID_UVC_CAMERA,
277                 .selector       = CT_PANTILT_RELATIVE_CONTROL,
278                 .index          = 12,
279                 .size           = 4,
280                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
281                                 | UVC_CONTROL_AUTO_UPDATE,
282         },
283         {
284                 .entity         = UVC_GUID_UVC_CAMERA,
285                 .selector       = CT_ROLL_ABSOLUTE_CONTROL,
286                 .index          = 13,
287                 .size           = 2,
288                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
289                                 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
290         },
291         {
292                 .entity         = UVC_GUID_UVC_CAMERA,
293                 .selector       = CT_ROLL_RELATIVE_CONTROL,
294                 .index          = 14,
295                 .size           = 2,
296                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
297                                 | UVC_CONTROL_AUTO_UPDATE,
298         },
299         {
300                 .entity         = UVC_GUID_UVC_CAMERA,
301                 .selector       = CT_FOCUS_AUTO_CONTROL,
302                 .index          = 17,
303                 .size           = 1,
304                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
305                                 | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
306         },
307         {
308                 .entity         = UVC_GUID_UVC_CAMERA,
309                 .selector       = CT_PRIVACY_CONTROL,
310                 .index          = 18,
311                 .size           = 1,
312                 .flags          = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
313                                 | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
314         },
315 };
316
317 static struct uvc_menu_info power_line_frequency_controls[] = {
318         { 0, "Disabled" },
319         { 1, "50 Hz" },
320         { 2, "60 Hz" },
321 };
322
323 static struct uvc_menu_info exposure_auto_controls[] = {
324         { 2, "Auto Mode" },
325         { 1, "Manual Mode" },
326         { 4, "Shutter Priority Mode" },
327         { 8, "Aperture Priority Mode" },
328 };
329
330 static struct uvc_control_mapping uvc_ctrl_mappings[] = {
331         {
332                 .id             = V4L2_CID_BRIGHTNESS,
333                 .name           = "Brightness",
334                 .entity         = UVC_GUID_UVC_PROCESSING,
335                 .selector       = PU_BRIGHTNESS_CONTROL,
336                 .size           = 16,
337                 .offset         = 0,
338                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
339                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
340         },
341         {
342                 .id             = V4L2_CID_CONTRAST,
343                 .name           = "Contrast",
344                 .entity         = UVC_GUID_UVC_PROCESSING,
345                 .selector       = PU_CONTRAST_CONTROL,
346                 .size           = 16,
347                 .offset         = 0,
348                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
349                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
350         },
351         {
352                 .id             = V4L2_CID_HUE,
353                 .name           = "Hue",
354                 .entity         = UVC_GUID_UVC_PROCESSING,
355                 .selector       = PU_HUE_CONTROL,
356                 .size           = 16,
357                 .offset         = 0,
358                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
359                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
360         },
361         {
362                 .id             = V4L2_CID_SATURATION,
363                 .name           = "Saturation",
364                 .entity         = UVC_GUID_UVC_PROCESSING,
365                 .selector       = PU_SATURATION_CONTROL,
366                 .size           = 16,
367                 .offset         = 0,
368                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
369                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
370         },
371         {
372                 .id             = V4L2_CID_SHARPNESS,
373                 .name           = "Sharpness",
374                 .entity         = UVC_GUID_UVC_PROCESSING,
375                 .selector       = PU_SHARPNESS_CONTROL,
376                 .size           = 16,
377                 .offset         = 0,
378                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
379                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
380         },
381         {
382                 .id             = V4L2_CID_GAMMA,
383                 .name           = "Gamma",
384                 .entity         = UVC_GUID_UVC_PROCESSING,
385                 .selector       = PU_GAMMA_CONTROL,
386                 .size           = 16,
387                 .offset         = 0,
388                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
389                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
390         },
391         {
392                 .id             = V4L2_CID_BACKLIGHT_COMPENSATION,
393                 .name           = "Backlight Compensation",
394                 .entity         = UVC_GUID_UVC_PROCESSING,
395                 .selector       = PU_BACKLIGHT_COMPENSATION_CONTROL,
396                 .size           = 16,
397                 .offset         = 0,
398                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
399                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
400         },
401         {
402                 .id             = V4L2_CID_GAIN,
403                 .name           = "Gain",
404                 .entity         = UVC_GUID_UVC_PROCESSING,
405                 .selector       = PU_GAIN_CONTROL,
406                 .size           = 16,
407                 .offset         = 0,
408                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
409                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
410         },
411         {
412                 .id             = V4L2_CID_POWER_LINE_FREQUENCY,
413                 .name           = "Power Line Frequency",
414                 .entity         = UVC_GUID_UVC_PROCESSING,
415                 .selector       = PU_POWER_LINE_FREQUENCY_CONTROL,
416                 .size           = 2,
417                 .offset         = 0,
418                 .v4l2_type      = V4L2_CTRL_TYPE_MENU,
419                 .data_type      = UVC_CTRL_DATA_TYPE_ENUM,
420                 .menu_info      = power_line_frequency_controls,
421                 .menu_count     = ARRAY_SIZE(power_line_frequency_controls),
422         },
423         {
424                 .id             = V4L2_CID_HUE_AUTO,
425                 .name           = "Hue, Auto",
426                 .entity         = UVC_GUID_UVC_PROCESSING,
427                 .selector       = PU_HUE_AUTO_CONTROL,
428                 .size           = 1,
429                 .offset         = 0,
430                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
431                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
432         },
433         {
434                 .id             = V4L2_CID_EXPOSURE_AUTO,
435                 .name           = "Exposure, Auto",
436                 .entity         = UVC_GUID_UVC_CAMERA,
437                 .selector       = CT_AE_MODE_CONTROL,
438                 .size           = 4,
439                 .offset         = 0,
440                 .v4l2_type      = V4L2_CTRL_TYPE_MENU,
441                 .data_type      = UVC_CTRL_DATA_TYPE_BITMASK,
442                 .menu_info      = exposure_auto_controls,
443                 .menu_count     = ARRAY_SIZE(exposure_auto_controls),
444         },
445         {
446                 .id             = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
447                 .name           = "Exposure, Auto Priority",
448                 .entity         = UVC_GUID_UVC_CAMERA,
449                 .selector       = CT_AE_PRIORITY_CONTROL,
450                 .size           = 1,
451                 .offset         = 0,
452                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
453                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
454         },
455         {
456                 .id             = V4L2_CID_EXPOSURE_ABSOLUTE,
457                 .name           = "Exposure (Absolute)",
458                 .entity         = UVC_GUID_UVC_CAMERA,
459                 .selector       = CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
460                 .size           = 32,
461                 .offset         = 0,
462                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
463                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
464         },
465         {
466                 .id             = V4L2_CID_AUTO_WHITE_BALANCE,
467                 .name           = "White Balance Temperature, Auto",
468                 .entity         = UVC_GUID_UVC_PROCESSING,
469                 .selector       = PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
470                 .size           = 1,
471                 .offset         = 0,
472                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
473                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
474         },
475         {
476                 .id             = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
477                 .name           = "White Balance Temperature",
478                 .entity         = UVC_GUID_UVC_PROCESSING,
479                 .selector       = PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
480                 .size           = 16,
481                 .offset         = 0,
482                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
483                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
484         },
485         {
486                 .id             = V4L2_CID_AUTO_WHITE_BALANCE,
487                 .name           = "White Balance Component, Auto",
488                 .entity         = UVC_GUID_UVC_PROCESSING,
489                 .selector       = PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
490                 .size           = 1,
491                 .offset         = 0,
492                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
493                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
494         },
495         {
496                 .id             = V4L2_CID_BLUE_BALANCE,
497                 .name           = "White Balance Blue Component",
498                 .entity         = UVC_GUID_UVC_PROCESSING,
499                 .selector       = PU_WHITE_BALANCE_COMPONENT_CONTROL,
500                 .size           = 16,
501                 .offset         = 0,
502                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
503                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
504         },
505         {
506                 .id             = V4L2_CID_RED_BALANCE,
507                 .name           = "White Balance Red Component",
508                 .entity         = UVC_GUID_UVC_PROCESSING,
509                 .selector       = PU_WHITE_BALANCE_COMPONENT_CONTROL,
510                 .size           = 16,
511                 .offset         = 16,
512                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
513                 .data_type      = UVC_CTRL_DATA_TYPE_SIGNED,
514         },
515         {
516                 .id             = V4L2_CID_FOCUS_ABSOLUTE,
517                 .name           = "Focus (absolute)",
518                 .entity         = UVC_GUID_UVC_CAMERA,
519                 .selector       = CT_FOCUS_ABSOLUTE_CONTROL,
520                 .size           = 16,
521                 .offset         = 0,
522                 .v4l2_type      = V4L2_CTRL_TYPE_INTEGER,
523                 .data_type      = UVC_CTRL_DATA_TYPE_UNSIGNED,
524         },
525         {
526                 .id             = V4L2_CID_FOCUS_AUTO,
527                 .name           = "Focus, Auto",
528                 .entity         = UVC_GUID_UVC_CAMERA,
529                 .selector       = CT_FOCUS_AUTO_CONTROL,
530                 .size           = 1,
531                 .offset         = 0,
532                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
533                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
534         },
535 };
536
537 /* ------------------------------------------------------------------------
538  * Utility functions
539  */
540
541 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
542 {
543         return ctrl->data + id * ctrl->info->size;
544 }
545
546 static inline int uvc_test_bit(const __u8 *data, int bit)
547 {
548         return (data[bit >> 3] >> (bit & 7)) & 1;
549 }
550
551 static inline void uvc_clear_bit(__u8 *data, int bit)
552 {
553         data[bit >> 3] &= ~(1 << (bit & 7));
554 }
555
556 /* Extract the bit string specified by mapping->offset and mapping->size
557  * from the little-endian data stored at 'data' and return the result as
558  * a signed 32bit integer. Sign extension will be performed if the mapping
559  * references a signed data type.
560  */
561 static __s32 uvc_get_le_value(const __u8 *data,
562         struct uvc_control_mapping *mapping)
563 {
564         int bits = mapping->size;
565         int offset = mapping->offset;
566         __s32 value = 0;
567         __u8 mask;
568
569         data += offset / 8;
570         offset &= 7;
571         mask = ((1LL << bits) - 1) << offset;
572
573         for (; bits > 0; data++) {
574                 __u8 byte = *data & mask;
575                 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
576                 bits -= 8 - (offset > 0 ? offset : 0);
577                 offset -= 8;
578                 mask = (1 << bits) - 1;
579         }
580
581         /* Sign-extend the value if needed */
582         if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
583                 value |= -(value & (1 << (mapping->size - 1)));
584
585         return value;
586 }
587
588 /* Set the bit string specified by mapping->offset and mapping->size
589  * in the little-endian data stored at 'data' to the value 'value'.
590  */
591 static void uvc_set_le_value(__s32 value, __u8 *data,
592         struct uvc_control_mapping *mapping)
593 {
594         int bits = mapping->size;
595         int offset = mapping->offset;
596         __u8 mask;
597
598         data += offset / 8;
599         offset &= 7;
600
601         for (; bits > 0; data++) {
602                 mask = ((1LL << bits) - 1) << offset;
603                 *data = (*data & ~mask) | ((value << offset) & mask);
604                 value >>= offset ? offset : 8;
605                 bits -= 8 - offset;
606                 offset = 0;
607         }
608 }
609
610 /* ------------------------------------------------------------------------
611  * Terminal and unit management
612  */
613
614 static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
615 static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
616 static const __u8 uvc_media_transport_input_guid[16] =
617         UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
618
619 static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
620 {
621         switch (UVC_ENTITY_TYPE(entity)) {
622         case ITT_CAMERA:
623                 return memcmp(uvc_camera_guid, guid, 16) == 0;
624
625         case ITT_MEDIA_TRANSPORT_INPUT:
626                 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
627
628         case VC_PROCESSING_UNIT:
629                 return memcmp(uvc_processing_guid, guid, 16) == 0;
630
631         case VC_EXTENSION_UNIT:
632                 return memcmp(entity->extension.guidExtensionCode,
633                               guid, 16) == 0;
634
635         default:
636                 return 0;
637         }
638 }
639
640 /* ------------------------------------------------------------------------
641  * UVC Controls
642  */
643
644 static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
645         struct uvc_control_mapping **mapping, struct uvc_control **control,
646         int next)
647 {
648         struct uvc_control *ctrl;
649         struct uvc_control_mapping *map;
650         unsigned int i;
651
652         if (entity == NULL)
653                 return;
654
655         for (i = 0; i < entity->ncontrols; ++i) {
656                 ctrl = &entity->controls[i];
657                 if (ctrl->info == NULL)
658                         continue;
659
660                 list_for_each_entry(map, &ctrl->info->mappings, list) {
661                         if ((map->id == v4l2_id) && !next) {
662                                 *control = ctrl;
663                                 *mapping = map;
664                                 return;
665                         }
666
667                         if ((*mapping == NULL || (*mapping)->id > map->id) &&
668                             (map->id > v4l2_id) && next) {
669                                 *control = ctrl;
670                                 *mapping = map;
671                         }
672                 }
673         }
674 }
675
676 struct uvc_control *uvc_find_control(struct uvc_video_device *video,
677         __u32 v4l2_id, struct uvc_control_mapping **mapping)
678 {
679         struct uvc_control *ctrl = NULL;
680         struct uvc_entity *entity;
681         int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
682
683         *mapping = NULL;
684
685         /* Mask the query flags. */
686         v4l2_id &= V4L2_CTRL_ID_MASK;
687
688         /* Find the control. */
689         __uvc_find_control(video->processing, v4l2_id, mapping, &ctrl, next);
690         if (ctrl && !next)
691                 return ctrl;
692
693         list_for_each_entry(entity, &video->iterms, chain) {
694                 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
695                 if (ctrl && !next)
696                         return ctrl;
697         }
698
699         list_for_each_entry(entity, &video->extensions, chain) {
700                 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
701                 if (ctrl && !next)
702                         return ctrl;
703         }
704
705         if (ctrl == NULL && !next)
706                 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
707                                 v4l2_id);
708
709         return ctrl;
710 }
711
712 int uvc_query_v4l2_ctrl(struct uvc_video_device *video,
713         struct v4l2_queryctrl *v4l2_ctrl)
714 {
715         struct uvc_control *ctrl;
716         struct uvc_control_mapping *mapping;
717         struct uvc_menu_info *menu;
718         unsigned int i;
719         __u8 *data;
720         int ret;
721
722         ctrl = uvc_find_control(video, v4l2_ctrl->id, &mapping);
723         if (ctrl == NULL)
724                 return -EINVAL;
725
726         data = kmalloc(ctrl->info->size, GFP_KERNEL);
727         if (data == NULL)
728                 return -ENOMEM;
729
730         memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
731         v4l2_ctrl->id = mapping->id;
732         v4l2_ctrl->type = mapping->v4l2_type;
733         strncpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
734         v4l2_ctrl->flags = 0;
735
736         if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
737                 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
738
739         if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
740                 if ((ret = uvc_query_ctrl(video->dev, GET_DEF, ctrl->entity->id,
741                                 video->dev->intfnum, ctrl->info->selector,
742                                 data, ctrl->info->size)) < 0)
743                         goto out;
744                 v4l2_ctrl->default_value = uvc_get_le_value(data, mapping);
745         }
746
747         switch (mapping->v4l2_type) {
748         case V4L2_CTRL_TYPE_MENU:
749                 v4l2_ctrl->minimum = 0;
750                 v4l2_ctrl->maximum = mapping->menu_count - 1;
751                 v4l2_ctrl->step = 1;
752
753                 menu = mapping->menu_info;
754                 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
755                         if (menu->value == v4l2_ctrl->default_value) {
756                                 v4l2_ctrl->default_value = i;
757                                 break;
758                         }
759                 }
760
761                 ret = 0;
762                 goto out;
763
764         case V4L2_CTRL_TYPE_BOOLEAN:
765                 v4l2_ctrl->minimum = 0;
766                 v4l2_ctrl->maximum = 1;
767                 v4l2_ctrl->step = 1;
768                 ret = 0;
769                 goto out;
770
771         default:
772                 break;
773         }
774
775         if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
776                 if ((ret = uvc_query_ctrl(video->dev, GET_MIN, ctrl->entity->id,
777                                 video->dev->intfnum, ctrl->info->selector,
778                                 data, ctrl->info->size)) < 0)
779                         goto out;
780                 v4l2_ctrl->minimum = uvc_get_le_value(data, mapping);
781         }
782         if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
783                 if ((ret = uvc_query_ctrl(video->dev, GET_MAX, ctrl->entity->id,
784                                 video->dev->intfnum, ctrl->info->selector,
785                                 data, ctrl->info->size)) < 0)
786                         goto out;
787                 v4l2_ctrl->maximum = uvc_get_le_value(data, mapping);
788         }
789         if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
790                 if ((ret = uvc_query_ctrl(video->dev, GET_RES, ctrl->entity->id,
791                                 video->dev->intfnum, ctrl->info->selector,
792                                 data, ctrl->info->size)) < 0)
793                         goto out;
794                 v4l2_ctrl->step = uvc_get_le_value(data, mapping);
795         }
796
797         ret = 0;
798 out:
799         kfree(data);
800         return ret;
801 }
802
803
804 /* --------------------------------------------------------------------------
805  * Control transactions
806  *
807  * To make extended set operations as atomic as the hardware allows, controls
808  * are handled using begin/commit/rollback operations.
809  *
810  * At the beginning of a set request, uvc_ctrl_begin should be called to
811  * initialize the request. This function acquires the control lock.
812  *
813  * When setting a control, the new value is stored in the control data field
814  * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
815  * later processing. If the UVC and V4L2 control sizes differ, the current
816  * value is loaded from the hardware before storing the new value in the data
817  * field.
818  *
819  * After processing all controls in the transaction, uvc_ctrl_commit or
820  * uvc_ctrl_rollback must be called to apply the pending changes to the
821  * hardware or revert them. When applying changes, all controls marked as
822  * dirty will be modified in the UVC device, and the dirty flag will be
823  * cleared. When reverting controls, the control data field
824  * UVC_CTRL_DATA_CURRENT is reverted to its previous value
825  * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
826  * control lock.
827  */
828 int uvc_ctrl_begin(struct uvc_video_device *video)
829 {
830         return mutex_lock_interruptible(&video->ctrl_mutex) ? -ERESTARTSYS : 0;
831 }
832
833 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
834         struct uvc_entity *entity, int rollback)
835 {
836         struct uvc_control *ctrl;
837         unsigned int i;
838         int ret;
839
840         if (entity == NULL)
841                 return 0;
842
843         for (i = 0; i < entity->ncontrols; ++i) {
844                 ctrl = &entity->controls[i];
845                 if (ctrl->info == NULL)
846                         continue;
847
848                 /* Reset the loaded flag for auto-update controls that were
849                  * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
850                  * uvc_ctrl_get from using the cached value.
851                  */
852                 if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
853                         ctrl->loaded = 0;
854
855                 if (!ctrl->dirty)
856                         continue;
857
858                 if (!rollback)
859                         ret = uvc_query_ctrl(dev, SET_CUR, ctrl->entity->id,
860                                 dev->intfnum, ctrl->info->selector,
861                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
862                                 ctrl->info->size);
863                 else
864                         ret = 0;
865
866                 if (rollback || ret < 0)
867                         memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
868                                uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
869                                ctrl->info->size);
870
871                 ctrl->dirty = 0;
872
873                 if (ret < 0)
874                         return ret;
875         }
876
877         return 0;
878 }
879
880 int __uvc_ctrl_commit(struct uvc_video_device *video, int rollback)
881 {
882         struct uvc_entity *entity;
883         int ret = 0;
884
885         /* Find the control. */
886         ret = uvc_ctrl_commit_entity(video->dev, video->processing, rollback);
887         if (ret < 0)
888                 goto done;
889
890         list_for_each_entry(entity, &video->iterms, chain) {
891                 ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
892                 if (ret < 0)
893                         goto done;
894         }
895
896         list_for_each_entry(entity, &video->extensions, chain) {
897                 ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
898                 if (ret < 0)
899                         goto done;
900         }
901
902 done:
903         mutex_unlock(&video->ctrl_mutex);
904         return ret;
905 }
906
907 int uvc_ctrl_get(struct uvc_video_device *video,
908         struct v4l2_ext_control *xctrl)
909 {
910         struct uvc_control *ctrl;
911         struct uvc_control_mapping *mapping;
912         struct uvc_menu_info *menu;
913         unsigned int i;
914         int ret;
915
916         ctrl = uvc_find_control(video, xctrl->id, &mapping);
917         if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
918                 return -EINVAL;
919
920         if (!ctrl->loaded) {
921                 ret = uvc_query_ctrl(video->dev, GET_CUR, ctrl->entity->id,
922                                 video->dev->intfnum, ctrl->info->selector,
923                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
924                                 ctrl->info->size);
925                 if (ret < 0)
926                         return ret;
927
928                 ctrl->loaded = 1;
929         }
930
931         xctrl->value = uvc_get_le_value(
932                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), mapping);
933
934         if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
935                 menu = mapping->menu_info;
936                 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
937                         if (menu->value == xctrl->value) {
938                                 xctrl->value = i;
939                                 break;
940                         }
941                 }
942         }
943
944         return 0;
945 }
946
947 int uvc_ctrl_set(struct uvc_video_device *video,
948         struct v4l2_ext_control *xctrl)
949 {
950         struct uvc_control *ctrl;
951         struct uvc_control_mapping *mapping;
952         s32 value = xctrl->value;
953         int ret;
954
955         ctrl = uvc_find_control(video, xctrl->id, &mapping);
956         if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
957                 return -EINVAL;
958
959         if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
960                 if (value < 0 || value >= mapping->menu_count)
961                         return -EINVAL;
962                 value = mapping->menu_info[value].value;
963         }
964
965         if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
966                 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
967                         memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
968                                 0, ctrl->info->size);
969                 } else {
970                         ret = uvc_query_ctrl(video->dev, GET_CUR,
971                                 ctrl->entity->id, video->dev->intfnum,
972                                 ctrl->info->selector,
973                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
974                                 ctrl->info->size);
975                         if (ret < 0)
976                                 return ret;
977                 }
978
979                 ctrl->loaded = 1;
980         }
981
982         if (!ctrl->dirty) {
983                 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
984                        uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
985                        ctrl->info->size);
986         }
987
988         uvc_set_le_value(value,
989                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), mapping);
990
991         ctrl->dirty = 1;
992         ctrl->modified = 1;
993         return 0;
994 }
995
996 /* --------------------------------------------------------------------------
997  * Dynamic controls
998  */
999
1000 int uvc_xu_ctrl_query(struct uvc_video_device *video,
1001         struct uvc_xu_control *xctrl, int set)
1002 {
1003         struct uvc_entity *entity;
1004         struct uvc_control *ctrl = NULL;
1005         unsigned int i, found = 0;
1006         __u8 *data;
1007         int ret;
1008
1009         /* Find the extension unit. */
1010         list_for_each_entry(entity, &video->extensions, chain) {
1011                 if (entity->id == xctrl->unit)
1012                         break;
1013         }
1014
1015         if (entity->id != xctrl->unit) {
1016                 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1017                         xctrl->unit);
1018                 return -EINVAL;
1019         }
1020
1021         /* Find the control. */
1022         for (i = 0; i < entity->ncontrols; ++i) {
1023                 ctrl = &entity->controls[i];
1024                 if (ctrl->info == NULL)
1025                         continue;
1026
1027                 if (ctrl->info->selector == xctrl->selector) {
1028                         found = 1;
1029                         break;
1030                 }
1031         }
1032
1033         if (!found) {
1034                 uvc_trace(UVC_TRACE_CONTROL,
1035                         "Control " UVC_GUID_FORMAT "/%u not found.\n",
1036                         UVC_GUID_ARGS(entity->extension.guidExtensionCode),
1037                         xctrl->selector);
1038                 return -EINVAL;
1039         }
1040
1041         /* Validate control data size. */
1042         if (ctrl->info->size != xctrl->size)
1043                 return -EINVAL;
1044
1045         if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
1046             (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
1047                 return -EINVAL;
1048
1049         if (mutex_lock_interruptible(&video->ctrl_mutex))
1050                 return -ERESTARTSYS;
1051
1052         memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1053                uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1054                xctrl->size);
1055         data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1056
1057         if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1058                 ret = -EFAULT;
1059                 goto out;
1060         }
1061
1062         ret = uvc_query_ctrl(video->dev, set ? SET_CUR : GET_CUR, xctrl->unit,
1063                              video->dev->intfnum, xctrl->selector, data,
1064                              xctrl->size);
1065         if (ret < 0)
1066                 goto out;
1067
1068         if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1069                 ret = -EFAULT;
1070                 goto out;
1071         }
1072
1073 out:
1074         if (ret)
1075                 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1076                        uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1077                        xctrl->size);
1078
1079         mutex_unlock(&video->ctrl_mutex);
1080         return ret;
1081 }
1082
1083 /* --------------------------------------------------------------------------
1084  * Suspend/resume
1085  */
1086
1087 /*
1088  * Restore control values after resume, skipping controls that haven't been
1089  * changed.
1090  *
1091  * TODO
1092  * - Don't restore modified controls that are back to their default value.
1093  * - Handle restore order (Auto-Exposure Mode should be restored before
1094  *   Exposure Time).
1095  */
1096 int uvc_ctrl_resume_device(struct uvc_device *dev)
1097 {
1098         struct uvc_control *ctrl;
1099         struct uvc_entity *entity;
1100         unsigned int i;
1101         int ret;
1102
1103         /* Walk the entities list and restore controls when possible. */
1104         list_for_each_entry(entity, &dev->entities, list) {
1105
1106                 for (i = 0; i < entity->ncontrols; ++i) {
1107                         ctrl = &entity->controls[i];
1108
1109                         if (ctrl->info == NULL || !ctrl->modified ||
1110                             (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
1111                                 continue;
1112
1113                         printk(KERN_INFO "restoring control " UVC_GUID_FORMAT
1114                                 "/%u/%u\n", UVC_GUID_ARGS(ctrl->info->entity),
1115                                 ctrl->info->index, ctrl->info->selector);
1116                         ctrl->dirty = 1;
1117                 }
1118
1119                 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1120                 if (ret < 0)
1121                         return ret;
1122         }
1123
1124         return 0;
1125 }
1126
1127 /* --------------------------------------------------------------------------
1128  * Control and mapping handling
1129  */
1130
1131 static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
1132         struct uvc_control_info *info)
1133 {
1134         struct uvc_entity *entity;
1135         struct uvc_control *ctrl = NULL;
1136         int ret, found = 0;
1137         unsigned int i;
1138
1139         list_for_each_entry(entity, &dev->entities, list) {
1140                 if (!uvc_entity_match_guid(entity, info->entity))
1141                         continue;
1142
1143                 for (i = 0; i < entity->ncontrols; ++i) {
1144                         ctrl = &entity->controls[i];
1145                         if (ctrl->index == info->index) {
1146                                 found = 1;
1147                                 break;
1148                         }
1149                 }
1150
1151                 if (found)
1152                         break;
1153         }
1154
1155         if (!found)
1156                 return;
1157
1158         if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
1159                 /* Check if the device control information and length match
1160                  * the user supplied information.
1161                  */
1162                 __u32 flags;
1163                 __le16 size;
1164                 __u8 inf;
1165
1166                 if ((ret = uvc_query_ctrl(dev, GET_LEN, ctrl->entity->id,
1167                         dev->intfnum, info->selector, (__u8 *)&size, 2)) < 0) {
1168                         uvc_trace(UVC_TRACE_CONTROL, "GET_LEN failed on "
1169                                 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1170                                 UVC_GUID_ARGS(info->entity), info->selector,
1171                                 ret);
1172                         return;
1173                 }
1174
1175                 if (info->size != le16_to_cpu(size)) {
1176                         uvc_trace(UVC_TRACE_CONTROL, "Control " UVC_GUID_FORMAT
1177                                 "/%u size doesn't match user supplied "
1178                                 "value.\n", UVC_GUID_ARGS(info->entity),
1179                                 info->selector);
1180                         return;
1181                 }
1182
1183                 if ((ret = uvc_query_ctrl(dev, GET_INFO, ctrl->entity->id,
1184                         dev->intfnum, info->selector, &inf, 1)) < 0) {
1185                         uvc_trace(UVC_TRACE_CONTROL, "GET_INFO failed on "
1186                                 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1187                                 UVC_GUID_ARGS(info->entity), info->selector,
1188                                 ret);
1189                         return;
1190                 }
1191
1192                 flags = info->flags;
1193                 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1194                     ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
1195                         uvc_trace(UVC_TRACE_CONTROL, "Control "
1196                                 UVC_GUID_FORMAT "/%u flags don't match "
1197                                 "supported operations.\n",
1198                                 UVC_GUID_ARGS(info->entity), info->selector);
1199                         return;
1200                 }
1201         }
1202
1203         ctrl->info = info;
1204         ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_NDATA, GFP_KERNEL);
1205         uvc_trace(UVC_TRACE_CONTROL, "Added control " UVC_GUID_FORMAT "/%u "
1206                 "to device %s entity %u\n", UVC_GUID_ARGS(ctrl->info->entity),
1207                 ctrl->info->selector, dev->udev->devpath, entity->id);
1208 }
1209
1210 /*
1211  * Add an item to the UVC control information list, and instantiate a control
1212  * structure for each device that supports the control.
1213  */
1214 int uvc_ctrl_add_info(struct uvc_control_info *info)
1215 {
1216         struct uvc_control_info *ctrl;
1217         struct uvc_device *dev;
1218         int ret = 0;
1219
1220         /* Find matching controls by walking the devices, entities and
1221          * controls list.
1222          */
1223         mutex_lock(&uvc_driver.ctrl_mutex);
1224
1225         /* First check if the list contains a control matching the new one.
1226          * Bail out if it does.
1227          */
1228         list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1229                 if (memcmp(ctrl->entity, info->entity, 16))
1230                         continue;
1231
1232                 if (ctrl->selector == info->selector) {
1233                         uvc_trace(UVC_TRACE_CONTROL, "Control "
1234                                 UVC_GUID_FORMAT "/%u is already defined.\n",
1235                                 UVC_GUID_ARGS(info->entity), info->selector);
1236                         ret = -EEXIST;
1237                         goto end;
1238                 }
1239                 if (ctrl->index == info->index) {
1240                         uvc_trace(UVC_TRACE_CONTROL, "Control "
1241                                 UVC_GUID_FORMAT "/%u would overwrite index "
1242                                 "%d.\n", UVC_GUID_ARGS(info->entity),
1243                                 info->selector, info->index);
1244                         ret = -EEXIST;
1245                         goto end;
1246                 }
1247         }
1248
1249         list_for_each_entry(dev, &uvc_driver.devices, list)
1250                 uvc_ctrl_add_ctrl(dev, info);
1251
1252         INIT_LIST_HEAD(&info->mappings);
1253         list_add_tail(&info->list, &uvc_driver.controls);
1254 end:
1255         mutex_unlock(&uvc_driver.ctrl_mutex);
1256         return ret;
1257 }
1258
1259 int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1260 {
1261         struct uvc_control_info *info;
1262         struct uvc_control_mapping *map;
1263         int ret = -EINVAL;
1264
1265         if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1266                 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1267                         "invalid control id 0x%08x\n", mapping->name,
1268                         mapping->id);
1269                 return -EINVAL;
1270         }
1271
1272         mutex_lock(&uvc_driver.ctrl_mutex);
1273         list_for_each_entry(info, &uvc_driver.controls, list) {
1274                 if (memcmp(info->entity, mapping->entity, 16) ||
1275                         info->selector != mapping->selector)
1276                         continue;
1277
1278                 if (info->size * 8 < mapping->size + mapping->offset) {
1279                         uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' would "
1280                                 "overflow control " UVC_GUID_FORMAT "/%u\n",
1281                                 mapping->name, UVC_GUID_ARGS(info->entity),
1282                                 info->selector);
1283                         ret = -EOVERFLOW;
1284                         goto end;
1285                 }
1286
1287                 /* Check if the list contains a mapping matching the new one.
1288                  * Bail out if it does.
1289                  */
1290                 list_for_each_entry(map, &info->mappings, list) {
1291                         if (map->id == mapping->id) {
1292                                 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1293                                         "already defined.\n", mapping->name);
1294                                 ret = -EEXIST;
1295                                 goto end;
1296                         }
1297                 }
1298
1299                 mapping->ctrl = info;
1300                 list_add_tail(&mapping->list, &info->mappings);
1301                 uvc_trace(UVC_TRACE_CONTROL, "Adding mapping %s to control "
1302                         UVC_GUID_FORMAT "/%u.\n", mapping->name,
1303                         UVC_GUID_ARGS(info->entity), info->selector);
1304
1305                 ret = 0;
1306                 break;
1307         }
1308 end:
1309         mutex_unlock(&uvc_driver.ctrl_mutex);
1310         return ret;
1311 }
1312
1313 /*
1314  * Prune an entity of its bogus controls. This currently includes processing
1315  * unit auto controls for which no corresponding manual control is available.
1316  * Such auto controls make little sense if any, and are known to crash at
1317  * least the SiGma Micro webcam.
1318  */
1319 static void
1320 uvc_ctrl_prune_entity(struct uvc_entity *entity)
1321 {
1322         static const struct {
1323                 u8 idx_manual;
1324                 u8 idx_auto;
1325         } blacklist[] = {
1326                 { 2, 11 }, /* Hue */
1327                 { 6, 12 }, /* White Balance Temperature */
1328                 { 7, 13 }, /* White Balance Component */
1329         };
1330
1331         u8 *controls;
1332         unsigned int size;
1333         unsigned int i;
1334
1335         if (UVC_ENTITY_TYPE(entity) != VC_PROCESSING_UNIT)
1336                 return;
1337
1338         controls = entity->processing.bmControls;
1339         size = entity->processing.bControlSize;
1340
1341         for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
1342                 if (blacklist[i].idx_auto >= 8 * size ||
1343                     blacklist[i].idx_manual >= 8 * size)
1344                         continue;
1345
1346                 if (!uvc_test_bit(controls, blacklist[i].idx_auto) ||
1347                      uvc_test_bit(controls, blacklist[i].idx_manual))
1348                         continue;
1349
1350                 uvc_trace(UVC_TRACE_CONTROL, "Auto control %u/%u has no "
1351                         "matching manual control, removing it.\n", entity->id,
1352                         blacklist[i].idx_auto);
1353
1354                 uvc_clear_bit(controls, blacklist[i].idx_auto);
1355         }
1356 }
1357
1358 /*
1359  * Initialize device controls.
1360  */
1361 int uvc_ctrl_init_device(struct uvc_device *dev)
1362 {
1363         struct uvc_control_info *info;
1364         struct uvc_control *ctrl;
1365         struct uvc_entity *entity;
1366         unsigned int i;
1367
1368         /* Walk the entities list and instantiate controls */
1369         list_for_each_entry(entity, &dev->entities, list) {
1370                 unsigned int bControlSize = 0, ncontrols = 0;
1371                 __u8 *bmControls = NULL;
1372
1373                 if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
1374                         bmControls = entity->extension.bmControls;
1375                         bControlSize = entity->extension.bControlSize;
1376                 } else if (UVC_ENTITY_TYPE(entity) == VC_PROCESSING_UNIT) {
1377                         bmControls = entity->processing.bmControls;
1378                         bControlSize = entity->processing.bControlSize;
1379                 } else if (UVC_ENTITY_TYPE(entity) == ITT_CAMERA) {
1380                         bmControls = entity->camera.bmControls;
1381                         bControlSize = entity->camera.bControlSize;
1382                 }
1383
1384                 if (dev->quirks & UVC_QUIRK_PRUNE_CONTROLS)
1385                         uvc_ctrl_prune_entity(entity);
1386
1387                 for (i = 0; i < bControlSize; ++i)
1388                         ncontrols += hweight8(bmControls[i]);
1389
1390                 if (ncontrols == 0)
1391                         continue;
1392
1393                 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1394                 if (entity->controls == NULL)
1395                         return -ENOMEM;
1396
1397                 entity->ncontrols = ncontrols;
1398
1399                 ctrl = entity->controls;
1400                 for (i = 0; i < bControlSize * 8; ++i) {
1401                         if (uvc_test_bit(bmControls, i) == 0)
1402                                 continue;
1403
1404                         ctrl->entity = entity;
1405                         ctrl->index = i;
1406                         ctrl++;
1407                 }
1408         }
1409
1410         /* Walk the controls info list and associate them with the device
1411          * controls, then add the device to the global device list. This has
1412          * to be done while holding the controls lock, to make sure
1413          * uvc_ctrl_add_info() will not get called in-between.
1414          */
1415         mutex_lock(&uvc_driver.ctrl_mutex);
1416         list_for_each_entry(info, &uvc_driver.controls, list)
1417                 uvc_ctrl_add_ctrl(dev, info);
1418
1419         list_add_tail(&dev->list, &uvc_driver.devices);
1420         mutex_unlock(&uvc_driver.ctrl_mutex);
1421
1422         return 0;
1423 }
1424
1425 /*
1426  * Cleanup device controls.
1427  */
1428 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1429 {
1430         struct uvc_entity *entity;
1431         unsigned int i;
1432
1433         /* Remove the device from the global devices list */
1434         mutex_lock(&uvc_driver.ctrl_mutex);
1435         if (dev->list.next != NULL)
1436                 list_del(&dev->list);
1437         mutex_unlock(&uvc_driver.ctrl_mutex);
1438
1439         list_for_each_entry(entity, &dev->entities, list) {
1440                 for (i = 0; i < entity->ncontrols; ++i)
1441                         kfree(entity->controls[i].data);
1442
1443                 kfree(entity->controls);
1444         }
1445 }
1446
1447 void uvc_ctrl_init(void)
1448 {
1449         struct uvc_control_info *ctrl = uvc_ctrls;
1450         struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1451         struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1452         struct uvc_control_mapping *mend =
1453                 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1454
1455         for (; ctrl < cend; ++ctrl)
1456                 uvc_ctrl_add_info(ctrl);
1457
1458         for (; mapping < mend; ++mapping)
1459                 uvc_ctrl_add_mapping(mapping);
1460 }
1461