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