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