]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/media/video/uvc/uvc_ctrl.c
V4L/DVB (9902): uvcvideo: V4L2 privacy control support
[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                 .id             = V4L2_CID_PRIVACY,
537                 .name           = "Privacy",
538                 .entity         = UVC_GUID_UVC_CAMERA,
539                 .selector       = CT_PRIVACY_CONTROL,
540                 .size           = 1,
541                 .offset         = 0,
542                 .v4l2_type      = V4L2_CTRL_TYPE_BOOLEAN,
543                 .data_type      = UVC_CTRL_DATA_TYPE_BOOLEAN,
544         },
545 };
546
547 /* ------------------------------------------------------------------------
548  * Utility functions
549  */
550
551 static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
552 {
553         return ctrl->data + id * ctrl->info->size;
554 }
555
556 static inline int uvc_test_bit(const __u8 *data, int bit)
557 {
558         return (data[bit >> 3] >> (bit & 7)) & 1;
559 }
560
561 static inline void uvc_clear_bit(__u8 *data, int bit)
562 {
563         data[bit >> 3] &= ~(1 << (bit & 7));
564 }
565
566 /* Extract the bit string specified by mapping->offset and mapping->size
567  * from the little-endian data stored at 'data' and return the result as
568  * a signed 32bit integer. Sign extension will be performed if the mapping
569  * references a signed data type.
570  */
571 static __s32 uvc_get_le_value(const __u8 *data,
572         struct uvc_control_mapping *mapping)
573 {
574         int bits = mapping->size;
575         int offset = mapping->offset;
576         __s32 value = 0;
577         __u8 mask;
578
579         data += offset / 8;
580         offset &= 7;
581         mask = ((1LL << bits) - 1) << offset;
582
583         for (; bits > 0; data++) {
584                 __u8 byte = *data & mask;
585                 value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
586                 bits -= 8 - (offset > 0 ? offset : 0);
587                 offset -= 8;
588                 mask = (1 << bits) - 1;
589         }
590
591         /* Sign-extend the value if needed */
592         if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
593                 value |= -(value & (1 << (mapping->size - 1)));
594
595         return value;
596 }
597
598 /* Set the bit string specified by mapping->offset and mapping->size
599  * in the little-endian data stored at 'data' to the value 'value'.
600  */
601 static void uvc_set_le_value(__s32 value, __u8 *data,
602         struct uvc_control_mapping *mapping)
603 {
604         int bits = mapping->size;
605         int offset = mapping->offset;
606         __u8 mask;
607
608         data += offset / 8;
609         offset &= 7;
610
611         for (; bits > 0; data++) {
612                 mask = ((1LL << bits) - 1) << offset;
613                 *data = (*data & ~mask) | ((value << offset) & mask);
614                 value >>= offset ? offset : 8;
615                 bits -= 8 - offset;
616                 offset = 0;
617         }
618 }
619
620 /* ------------------------------------------------------------------------
621  * Terminal and unit management
622  */
623
624 static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
625 static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
626 static const __u8 uvc_media_transport_input_guid[16] =
627         UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
628
629 static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
630 {
631         switch (UVC_ENTITY_TYPE(entity)) {
632         case ITT_CAMERA:
633                 return memcmp(uvc_camera_guid, guid, 16) == 0;
634
635         case ITT_MEDIA_TRANSPORT_INPUT:
636                 return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
637
638         case VC_PROCESSING_UNIT:
639                 return memcmp(uvc_processing_guid, guid, 16) == 0;
640
641         case VC_EXTENSION_UNIT:
642                 return memcmp(entity->extension.guidExtensionCode,
643                               guid, 16) == 0;
644
645         default:
646                 return 0;
647         }
648 }
649
650 /* ------------------------------------------------------------------------
651  * UVC Controls
652  */
653
654 static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
655         struct uvc_control_mapping **mapping, struct uvc_control **control,
656         int next)
657 {
658         struct uvc_control *ctrl;
659         struct uvc_control_mapping *map;
660         unsigned int i;
661
662         if (entity == NULL)
663                 return;
664
665         for (i = 0; i < entity->ncontrols; ++i) {
666                 ctrl = &entity->controls[i];
667                 if (ctrl->info == NULL)
668                         continue;
669
670                 list_for_each_entry(map, &ctrl->info->mappings, list) {
671                         if ((map->id == v4l2_id) && !next) {
672                                 *control = ctrl;
673                                 *mapping = map;
674                                 return;
675                         }
676
677                         if ((*mapping == NULL || (*mapping)->id > map->id) &&
678                             (map->id > v4l2_id) && next) {
679                                 *control = ctrl;
680                                 *mapping = map;
681                         }
682                 }
683         }
684 }
685
686 struct uvc_control *uvc_find_control(struct uvc_video_device *video,
687         __u32 v4l2_id, struct uvc_control_mapping **mapping)
688 {
689         struct uvc_control *ctrl = NULL;
690         struct uvc_entity *entity;
691         int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
692
693         *mapping = NULL;
694
695         /* Mask the query flags. */
696         v4l2_id &= V4L2_CTRL_ID_MASK;
697
698         /* Find the control. */
699         __uvc_find_control(video->processing, v4l2_id, mapping, &ctrl, next);
700         if (ctrl && !next)
701                 return ctrl;
702
703         list_for_each_entry(entity, &video->iterms, chain) {
704                 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
705                 if (ctrl && !next)
706                         return ctrl;
707         }
708
709         list_for_each_entry(entity, &video->extensions, chain) {
710                 __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
711                 if (ctrl && !next)
712                         return ctrl;
713         }
714
715         if (ctrl == NULL && !next)
716                 uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
717                                 v4l2_id);
718
719         return ctrl;
720 }
721
722 int uvc_query_v4l2_ctrl(struct uvc_video_device *video,
723         struct v4l2_queryctrl *v4l2_ctrl)
724 {
725         struct uvc_control *ctrl;
726         struct uvc_control_mapping *mapping;
727         struct uvc_menu_info *menu;
728         unsigned int i;
729         __u8 *data;
730         int ret;
731
732         ctrl = uvc_find_control(video, v4l2_ctrl->id, &mapping);
733         if (ctrl == NULL)
734                 return -EINVAL;
735
736         data = kmalloc(ctrl->info->size, GFP_KERNEL);
737         if (data == NULL)
738                 return -ENOMEM;
739
740         memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
741         v4l2_ctrl->id = mapping->id;
742         v4l2_ctrl->type = mapping->v4l2_type;
743         strncpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
744         v4l2_ctrl->flags = 0;
745
746         if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
747                 v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
748
749         if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
750                 if ((ret = uvc_query_ctrl(video->dev, GET_DEF, ctrl->entity->id,
751                                 video->dev->intfnum, ctrl->info->selector,
752                                 data, ctrl->info->size)) < 0)
753                         goto out;
754                 v4l2_ctrl->default_value = uvc_get_le_value(data, mapping);
755         }
756
757         switch (mapping->v4l2_type) {
758         case V4L2_CTRL_TYPE_MENU:
759                 v4l2_ctrl->minimum = 0;
760                 v4l2_ctrl->maximum = mapping->menu_count - 1;
761                 v4l2_ctrl->step = 1;
762
763                 menu = mapping->menu_info;
764                 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
765                         if (menu->value == v4l2_ctrl->default_value) {
766                                 v4l2_ctrl->default_value = i;
767                                 break;
768                         }
769                 }
770
771                 ret = 0;
772                 goto out;
773
774         case V4L2_CTRL_TYPE_BOOLEAN:
775                 v4l2_ctrl->minimum = 0;
776                 v4l2_ctrl->maximum = 1;
777                 v4l2_ctrl->step = 1;
778                 ret = 0;
779                 goto out;
780
781         default:
782                 break;
783         }
784
785         if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
786                 if ((ret = uvc_query_ctrl(video->dev, GET_MIN, ctrl->entity->id,
787                                 video->dev->intfnum, ctrl->info->selector,
788                                 data, ctrl->info->size)) < 0)
789                         goto out;
790                 v4l2_ctrl->minimum = uvc_get_le_value(data, mapping);
791         }
792         if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
793                 if ((ret = uvc_query_ctrl(video->dev, GET_MAX, ctrl->entity->id,
794                                 video->dev->intfnum, ctrl->info->selector,
795                                 data, ctrl->info->size)) < 0)
796                         goto out;
797                 v4l2_ctrl->maximum = uvc_get_le_value(data, mapping);
798         }
799         if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
800                 if ((ret = uvc_query_ctrl(video->dev, GET_RES, ctrl->entity->id,
801                                 video->dev->intfnum, ctrl->info->selector,
802                                 data, ctrl->info->size)) < 0)
803                         goto out;
804                 v4l2_ctrl->step = uvc_get_le_value(data, mapping);
805         }
806
807         ret = 0;
808 out:
809         kfree(data);
810         return ret;
811 }
812
813
814 /* --------------------------------------------------------------------------
815  * Control transactions
816  *
817  * To make extended set operations as atomic as the hardware allows, controls
818  * are handled using begin/commit/rollback operations.
819  *
820  * At the beginning of a set request, uvc_ctrl_begin should be called to
821  * initialize the request. This function acquires the control lock.
822  *
823  * When setting a control, the new value is stored in the control data field
824  * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
825  * later processing. If the UVC and V4L2 control sizes differ, the current
826  * value is loaded from the hardware before storing the new value in the data
827  * field.
828  *
829  * After processing all controls in the transaction, uvc_ctrl_commit or
830  * uvc_ctrl_rollback must be called to apply the pending changes to the
831  * hardware or revert them. When applying changes, all controls marked as
832  * dirty will be modified in the UVC device, and the dirty flag will be
833  * cleared. When reverting controls, the control data field
834  * UVC_CTRL_DATA_CURRENT is reverted to its previous value
835  * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
836  * control lock.
837  */
838 int uvc_ctrl_begin(struct uvc_video_device *video)
839 {
840         return mutex_lock_interruptible(&video->ctrl_mutex) ? -ERESTARTSYS : 0;
841 }
842
843 static int uvc_ctrl_commit_entity(struct uvc_device *dev,
844         struct uvc_entity *entity, int rollback)
845 {
846         struct uvc_control *ctrl;
847         unsigned int i;
848         int ret;
849
850         if (entity == NULL)
851                 return 0;
852
853         for (i = 0; i < entity->ncontrols; ++i) {
854                 ctrl = &entity->controls[i];
855                 if (ctrl->info == NULL)
856                         continue;
857
858                 /* Reset the loaded flag for auto-update controls that were
859                  * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
860                  * uvc_ctrl_get from using the cached value.
861                  */
862                 if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
863                         ctrl->loaded = 0;
864
865                 if (!ctrl->dirty)
866                         continue;
867
868                 if (!rollback)
869                         ret = uvc_query_ctrl(dev, SET_CUR, ctrl->entity->id,
870                                 dev->intfnum, ctrl->info->selector,
871                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
872                                 ctrl->info->size);
873                 else
874                         ret = 0;
875
876                 if (rollback || ret < 0)
877                         memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
878                                uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
879                                ctrl->info->size);
880
881                 ctrl->dirty = 0;
882
883                 if (ret < 0)
884                         return ret;
885         }
886
887         return 0;
888 }
889
890 int __uvc_ctrl_commit(struct uvc_video_device *video, int rollback)
891 {
892         struct uvc_entity *entity;
893         int ret = 0;
894
895         /* Find the control. */
896         ret = uvc_ctrl_commit_entity(video->dev, video->processing, rollback);
897         if (ret < 0)
898                 goto done;
899
900         list_for_each_entry(entity, &video->iterms, chain) {
901                 ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
902                 if (ret < 0)
903                         goto done;
904         }
905
906         list_for_each_entry(entity, &video->extensions, chain) {
907                 ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
908                 if (ret < 0)
909                         goto done;
910         }
911
912 done:
913         mutex_unlock(&video->ctrl_mutex);
914         return ret;
915 }
916
917 int uvc_ctrl_get(struct uvc_video_device *video,
918         struct v4l2_ext_control *xctrl)
919 {
920         struct uvc_control *ctrl;
921         struct uvc_control_mapping *mapping;
922         struct uvc_menu_info *menu;
923         unsigned int i;
924         int ret;
925
926         ctrl = uvc_find_control(video, xctrl->id, &mapping);
927         if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
928                 return -EINVAL;
929
930         if (!ctrl->loaded) {
931                 ret = uvc_query_ctrl(video->dev, GET_CUR, ctrl->entity->id,
932                                 video->dev->intfnum, ctrl->info->selector,
933                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
934                                 ctrl->info->size);
935                 if (ret < 0)
936                         return ret;
937
938                 ctrl->loaded = 1;
939         }
940
941         xctrl->value = uvc_get_le_value(
942                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), mapping);
943
944         if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
945                 menu = mapping->menu_info;
946                 for (i = 0; i < mapping->menu_count; ++i, ++menu) {
947                         if (menu->value == xctrl->value) {
948                                 xctrl->value = i;
949                                 break;
950                         }
951                 }
952         }
953
954         return 0;
955 }
956
957 int uvc_ctrl_set(struct uvc_video_device *video,
958         struct v4l2_ext_control *xctrl)
959 {
960         struct uvc_control *ctrl;
961         struct uvc_control_mapping *mapping;
962         s32 value = xctrl->value;
963         int ret;
964
965         ctrl = uvc_find_control(video, xctrl->id, &mapping);
966         if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
967                 return -EINVAL;
968
969         if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
970                 if (value < 0 || value >= mapping->menu_count)
971                         return -EINVAL;
972                 value = mapping->menu_info[value].value;
973         }
974
975         if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
976                 if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
977                         memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
978                                 0, ctrl->info->size);
979                 } else {
980                         ret = uvc_query_ctrl(video->dev, GET_CUR,
981                                 ctrl->entity->id, video->dev->intfnum,
982                                 ctrl->info->selector,
983                                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
984                                 ctrl->info->size);
985                         if (ret < 0)
986                                 return ret;
987                 }
988
989                 ctrl->loaded = 1;
990         }
991
992         if (!ctrl->dirty) {
993                 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
994                        uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
995                        ctrl->info->size);
996         }
997
998         uvc_set_le_value(value,
999                 uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), mapping);
1000
1001         ctrl->dirty = 1;
1002         ctrl->modified = 1;
1003         return 0;
1004 }
1005
1006 /* --------------------------------------------------------------------------
1007  * Dynamic controls
1008  */
1009
1010 int uvc_xu_ctrl_query(struct uvc_video_device *video,
1011         struct uvc_xu_control *xctrl, int set)
1012 {
1013         struct uvc_entity *entity;
1014         struct uvc_control *ctrl = NULL;
1015         unsigned int i, found = 0;
1016         __u8 *data;
1017         int ret;
1018
1019         /* Find the extension unit. */
1020         list_for_each_entry(entity, &video->extensions, chain) {
1021                 if (entity->id == xctrl->unit)
1022                         break;
1023         }
1024
1025         if (entity->id != xctrl->unit) {
1026                 uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
1027                         xctrl->unit);
1028                 return -EINVAL;
1029         }
1030
1031         /* Find the control. */
1032         for (i = 0; i < entity->ncontrols; ++i) {
1033                 ctrl = &entity->controls[i];
1034                 if (ctrl->info == NULL)
1035                         continue;
1036
1037                 if (ctrl->info->selector == xctrl->selector) {
1038                         found = 1;
1039                         break;
1040                 }
1041         }
1042
1043         if (!found) {
1044                 uvc_trace(UVC_TRACE_CONTROL,
1045                         "Control " UVC_GUID_FORMAT "/%u not found.\n",
1046                         UVC_GUID_ARGS(entity->extension.guidExtensionCode),
1047                         xctrl->selector);
1048                 return -EINVAL;
1049         }
1050
1051         /* Validate control data size. */
1052         if (ctrl->info->size != xctrl->size)
1053                 return -EINVAL;
1054
1055         if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
1056             (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
1057                 return -EINVAL;
1058
1059         if (mutex_lock_interruptible(&video->ctrl_mutex))
1060                 return -ERESTARTSYS;
1061
1062         memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1063                uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1064                xctrl->size);
1065         data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
1066
1067         if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
1068                 ret = -EFAULT;
1069                 goto out;
1070         }
1071
1072         ret = uvc_query_ctrl(video->dev, set ? SET_CUR : GET_CUR, xctrl->unit,
1073                              video->dev->intfnum, xctrl->selector, data,
1074                              xctrl->size);
1075         if (ret < 0)
1076                 goto out;
1077
1078         if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
1079                 ret = -EFAULT;
1080                 goto out;
1081         }
1082
1083 out:
1084         if (ret)
1085                 memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
1086                        uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
1087                        xctrl->size);
1088
1089         mutex_unlock(&video->ctrl_mutex);
1090         return ret;
1091 }
1092
1093 /* --------------------------------------------------------------------------
1094  * Suspend/resume
1095  */
1096
1097 /*
1098  * Restore control values after resume, skipping controls that haven't been
1099  * changed.
1100  *
1101  * TODO
1102  * - Don't restore modified controls that are back to their default value.
1103  * - Handle restore order (Auto-Exposure Mode should be restored before
1104  *   Exposure Time).
1105  */
1106 int uvc_ctrl_resume_device(struct uvc_device *dev)
1107 {
1108         struct uvc_control *ctrl;
1109         struct uvc_entity *entity;
1110         unsigned int i;
1111         int ret;
1112
1113         /* Walk the entities list and restore controls when possible. */
1114         list_for_each_entry(entity, &dev->entities, list) {
1115
1116                 for (i = 0; i < entity->ncontrols; ++i) {
1117                         ctrl = &entity->controls[i];
1118
1119                         if (ctrl->info == NULL || !ctrl->modified ||
1120                             (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
1121                                 continue;
1122
1123                         printk(KERN_INFO "restoring control " UVC_GUID_FORMAT
1124                                 "/%u/%u\n", UVC_GUID_ARGS(ctrl->info->entity),
1125                                 ctrl->info->index, ctrl->info->selector);
1126                         ctrl->dirty = 1;
1127                 }
1128
1129                 ret = uvc_ctrl_commit_entity(dev, entity, 0);
1130                 if (ret < 0)
1131                         return ret;
1132         }
1133
1134         return 0;
1135 }
1136
1137 /* --------------------------------------------------------------------------
1138  * Control and mapping handling
1139  */
1140
1141 static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
1142         struct uvc_control_info *info)
1143 {
1144         struct uvc_entity *entity;
1145         struct uvc_control *ctrl = NULL;
1146         int ret, found = 0;
1147         unsigned int i;
1148
1149         list_for_each_entry(entity, &dev->entities, list) {
1150                 if (!uvc_entity_match_guid(entity, info->entity))
1151                         continue;
1152
1153                 for (i = 0; i < entity->ncontrols; ++i) {
1154                         ctrl = &entity->controls[i];
1155                         if (ctrl->index == info->index) {
1156                                 found = 1;
1157                                 break;
1158                         }
1159                 }
1160
1161                 if (found)
1162                         break;
1163         }
1164
1165         if (!found)
1166                 return;
1167
1168         if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
1169                 /* Check if the device control information and length match
1170                  * the user supplied information.
1171                  */
1172                 __u32 flags;
1173                 __le16 size;
1174                 __u8 inf;
1175
1176                 if ((ret = uvc_query_ctrl(dev, GET_LEN, ctrl->entity->id,
1177                         dev->intfnum, info->selector, (__u8 *)&size, 2)) < 0) {
1178                         uvc_trace(UVC_TRACE_CONTROL, "GET_LEN failed on "
1179                                 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1180                                 UVC_GUID_ARGS(info->entity), info->selector,
1181                                 ret);
1182                         return;
1183                 }
1184
1185                 if (info->size != le16_to_cpu(size)) {
1186                         uvc_trace(UVC_TRACE_CONTROL, "Control " UVC_GUID_FORMAT
1187                                 "/%u size doesn't match user supplied "
1188                                 "value.\n", UVC_GUID_ARGS(info->entity),
1189                                 info->selector);
1190                         return;
1191                 }
1192
1193                 if ((ret = uvc_query_ctrl(dev, GET_INFO, ctrl->entity->id,
1194                         dev->intfnum, info->selector, &inf, 1)) < 0) {
1195                         uvc_trace(UVC_TRACE_CONTROL, "GET_INFO failed on "
1196                                 "control " UVC_GUID_FORMAT "/%u (%d).\n",
1197                                 UVC_GUID_ARGS(info->entity), info->selector,
1198                                 ret);
1199                         return;
1200                 }
1201
1202                 flags = info->flags;
1203                 if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
1204                     ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
1205                         uvc_trace(UVC_TRACE_CONTROL, "Control "
1206                                 UVC_GUID_FORMAT "/%u flags don't match "
1207                                 "supported operations.\n",
1208                                 UVC_GUID_ARGS(info->entity), info->selector);
1209                         return;
1210                 }
1211         }
1212
1213         ctrl->info = info;
1214         ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_NDATA, GFP_KERNEL);
1215         uvc_trace(UVC_TRACE_CONTROL, "Added control " UVC_GUID_FORMAT "/%u "
1216                 "to device %s entity %u\n", UVC_GUID_ARGS(ctrl->info->entity),
1217                 ctrl->info->selector, dev->udev->devpath, entity->id);
1218 }
1219
1220 /*
1221  * Add an item to the UVC control information list, and instantiate a control
1222  * structure for each device that supports the control.
1223  */
1224 int uvc_ctrl_add_info(struct uvc_control_info *info)
1225 {
1226         struct uvc_control_info *ctrl;
1227         struct uvc_device *dev;
1228         int ret = 0;
1229
1230         /* Find matching controls by walking the devices, entities and
1231          * controls list.
1232          */
1233         mutex_lock(&uvc_driver.ctrl_mutex);
1234
1235         /* First check if the list contains a control matching the new one.
1236          * Bail out if it does.
1237          */
1238         list_for_each_entry(ctrl, &uvc_driver.controls, list) {
1239                 if (memcmp(ctrl->entity, info->entity, 16))
1240                         continue;
1241
1242                 if (ctrl->selector == info->selector) {
1243                         uvc_trace(UVC_TRACE_CONTROL, "Control "
1244                                 UVC_GUID_FORMAT "/%u is already defined.\n",
1245                                 UVC_GUID_ARGS(info->entity), info->selector);
1246                         ret = -EEXIST;
1247                         goto end;
1248                 }
1249                 if (ctrl->index == info->index) {
1250                         uvc_trace(UVC_TRACE_CONTROL, "Control "
1251                                 UVC_GUID_FORMAT "/%u would overwrite index "
1252                                 "%d.\n", UVC_GUID_ARGS(info->entity),
1253                                 info->selector, info->index);
1254                         ret = -EEXIST;
1255                         goto end;
1256                 }
1257         }
1258
1259         list_for_each_entry(dev, &uvc_driver.devices, list)
1260                 uvc_ctrl_add_ctrl(dev, info);
1261
1262         INIT_LIST_HEAD(&info->mappings);
1263         list_add_tail(&info->list, &uvc_driver.controls);
1264 end:
1265         mutex_unlock(&uvc_driver.ctrl_mutex);
1266         return ret;
1267 }
1268
1269 int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
1270 {
1271         struct uvc_control_info *info;
1272         struct uvc_control_mapping *map;
1273         int ret = -EINVAL;
1274
1275         if (mapping->id & ~V4L2_CTRL_ID_MASK) {
1276                 uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
1277                         "invalid control id 0x%08x\n", mapping->name,
1278                         mapping->id);
1279                 return -EINVAL;
1280         }
1281
1282         mutex_lock(&uvc_driver.ctrl_mutex);
1283         list_for_each_entry(info, &uvc_driver.controls, list) {
1284                 if (memcmp(info->entity, mapping->entity, 16) ||
1285                         info->selector != mapping->selector)
1286                         continue;
1287
1288                 if (info->size * 8 < mapping->size + mapping->offset) {
1289                         uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' would "
1290                                 "overflow control " UVC_GUID_FORMAT "/%u\n",
1291                                 mapping->name, UVC_GUID_ARGS(info->entity),
1292                                 info->selector);
1293                         ret = -EOVERFLOW;
1294                         goto end;
1295                 }
1296
1297                 /* Check if the list contains a mapping matching the new one.
1298                  * Bail out if it does.
1299                  */
1300                 list_for_each_entry(map, &info->mappings, list) {
1301                         if (map->id == mapping->id) {
1302                                 uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
1303                                         "already defined.\n", mapping->name);
1304                                 ret = -EEXIST;
1305                                 goto end;
1306                         }
1307                 }
1308
1309                 mapping->ctrl = info;
1310                 list_add_tail(&mapping->list, &info->mappings);
1311                 uvc_trace(UVC_TRACE_CONTROL, "Adding mapping %s to control "
1312                         UVC_GUID_FORMAT "/%u.\n", mapping->name,
1313                         UVC_GUID_ARGS(info->entity), info->selector);
1314
1315                 ret = 0;
1316                 break;
1317         }
1318 end:
1319         mutex_unlock(&uvc_driver.ctrl_mutex);
1320         return ret;
1321 }
1322
1323 /*
1324  * Prune an entity of its bogus controls. This currently includes processing
1325  * unit auto controls for which no corresponding manual control is available.
1326  * Such auto controls make little sense if any, and are known to crash at
1327  * least the SiGma Micro webcam.
1328  */
1329 static void
1330 uvc_ctrl_prune_entity(struct uvc_entity *entity)
1331 {
1332         static const struct {
1333                 u8 idx_manual;
1334                 u8 idx_auto;
1335         } blacklist[] = {
1336                 { 2, 11 }, /* Hue */
1337                 { 6, 12 }, /* White Balance Temperature */
1338                 { 7, 13 }, /* White Balance Component */
1339         };
1340
1341         u8 *controls;
1342         unsigned int size;
1343         unsigned int i;
1344
1345         if (UVC_ENTITY_TYPE(entity) != VC_PROCESSING_UNIT)
1346                 return;
1347
1348         controls = entity->processing.bmControls;
1349         size = entity->processing.bControlSize;
1350
1351         for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
1352                 if (blacklist[i].idx_auto >= 8 * size ||
1353                     blacklist[i].idx_manual >= 8 * size)
1354                         continue;
1355
1356                 if (!uvc_test_bit(controls, blacklist[i].idx_auto) ||
1357                      uvc_test_bit(controls, blacklist[i].idx_manual))
1358                         continue;
1359
1360                 uvc_trace(UVC_TRACE_CONTROL, "Auto control %u/%u has no "
1361                         "matching manual control, removing it.\n", entity->id,
1362                         blacklist[i].idx_auto);
1363
1364                 uvc_clear_bit(controls, blacklist[i].idx_auto);
1365         }
1366 }
1367
1368 /*
1369  * Initialize device controls.
1370  */
1371 int uvc_ctrl_init_device(struct uvc_device *dev)
1372 {
1373         struct uvc_control_info *info;
1374         struct uvc_control *ctrl;
1375         struct uvc_entity *entity;
1376         unsigned int i;
1377
1378         /* Walk the entities list and instantiate controls */
1379         list_for_each_entry(entity, &dev->entities, list) {
1380                 unsigned int bControlSize = 0, ncontrols = 0;
1381                 __u8 *bmControls = NULL;
1382
1383                 if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
1384                         bmControls = entity->extension.bmControls;
1385                         bControlSize = entity->extension.bControlSize;
1386                 } else if (UVC_ENTITY_TYPE(entity) == VC_PROCESSING_UNIT) {
1387                         bmControls = entity->processing.bmControls;
1388                         bControlSize = entity->processing.bControlSize;
1389                 } else if (UVC_ENTITY_TYPE(entity) == ITT_CAMERA) {
1390                         bmControls = entity->camera.bmControls;
1391                         bControlSize = entity->camera.bControlSize;
1392                 }
1393
1394                 if (dev->quirks & UVC_QUIRK_PRUNE_CONTROLS)
1395                         uvc_ctrl_prune_entity(entity);
1396
1397                 for (i = 0; i < bControlSize; ++i)
1398                         ncontrols += hweight8(bmControls[i]);
1399
1400                 if (ncontrols == 0)
1401                         continue;
1402
1403                 entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
1404                 if (entity->controls == NULL)
1405                         return -ENOMEM;
1406
1407                 entity->ncontrols = ncontrols;
1408
1409                 ctrl = entity->controls;
1410                 for (i = 0; i < bControlSize * 8; ++i) {
1411                         if (uvc_test_bit(bmControls, i) == 0)
1412                                 continue;
1413
1414                         ctrl->entity = entity;
1415                         ctrl->index = i;
1416                         ctrl++;
1417                 }
1418         }
1419
1420         /* Walk the controls info list and associate them with the device
1421          * controls, then add the device to the global device list. This has
1422          * to be done while holding the controls lock, to make sure
1423          * uvc_ctrl_add_info() will not get called in-between.
1424          */
1425         mutex_lock(&uvc_driver.ctrl_mutex);
1426         list_for_each_entry(info, &uvc_driver.controls, list)
1427                 uvc_ctrl_add_ctrl(dev, info);
1428
1429         list_add_tail(&dev->list, &uvc_driver.devices);
1430         mutex_unlock(&uvc_driver.ctrl_mutex);
1431
1432         return 0;
1433 }
1434
1435 /*
1436  * Cleanup device controls.
1437  */
1438 void uvc_ctrl_cleanup_device(struct uvc_device *dev)
1439 {
1440         struct uvc_entity *entity;
1441         unsigned int i;
1442
1443         /* Remove the device from the global devices list */
1444         mutex_lock(&uvc_driver.ctrl_mutex);
1445         if (dev->list.next != NULL)
1446                 list_del(&dev->list);
1447         mutex_unlock(&uvc_driver.ctrl_mutex);
1448
1449         list_for_each_entry(entity, &dev->entities, list) {
1450                 for (i = 0; i < entity->ncontrols; ++i)
1451                         kfree(entity->controls[i].data);
1452
1453                 kfree(entity->controls);
1454         }
1455 }
1456
1457 void uvc_ctrl_init(void)
1458 {
1459         struct uvc_control_info *ctrl = uvc_ctrls;
1460         struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
1461         struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
1462         struct uvc_control_mapping *mend =
1463                 mapping + ARRAY_SIZE(uvc_ctrl_mappings);
1464
1465         for (; ctrl < cend; ++ctrl)
1466                 uvc_ctrl_add_info(ctrl);
1467
1468         for (; mapping < mend; ++mapping)
1469                 uvc_ctrl_add_mapping(mapping);
1470 }
1471