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