]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/gspca/sonixb.c
V4L/DVB (8456): gspca_sonixb remove non working ovXXXX contrast, hue and saturation...
[linux-2.6-omap-h63xx.git] / drivers / media / video / gspca / sonixb.c
1 /*
2  *              sonix sn9c102 (bayer) library
3  *              Copyright (C) 2003 2004 Michel Xhaard mxhaard@magic.fr
4  * Add Pas106 Stefano Mozzi (C) 2004
5  *
6  * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */
22
23 #define MODULE_NAME "sonixb"
24
25 #include "gspca.h"
26
27 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
28 MODULE_DESCRIPTION("GSPCA/SN9C102 USB Camera Driver");
29 MODULE_LICENSE("GPL");
30
31 /* specific webcam descriptor */
32 struct sd {
33         struct gspca_dev gspca_dev;     /* !! must be the first item */
34
35         struct sd_desc sd_desc;         /* our nctrls differ dependend upon the
36                                            sensor, so we use a per cam copy */
37         atomic_t avg_lum;
38
39         unsigned char gain;
40         unsigned char exposure;
41         unsigned char brightness;
42         unsigned char autogain;
43         unsigned char autogain_ignore_frames;
44         unsigned char frames_to_drop;
45         unsigned char freq;             /* light freq filter setting */
46
47         unsigned char fr_h_sz;          /* size of frame header */
48         char sensor;                    /* Type of image sensor chip */
49 #define SENSOR_HV7131R 0
50 #define SENSOR_OV6650 1
51 #define SENSOR_OV7630 2
52 #define SENSOR_PAS106 3
53 #define SENSOR_PAS202 4
54 #define SENSOR_TAS5110 5
55 #define SENSOR_TAS5130CXX 6
56         char sensor_has_gain;
57         __u8 sensor_addr;
58         __u8 reg11;
59 };
60
61 #define COMP2 0x8f
62 #define COMP 0xc7               /* 0x87 //0x07 */
63 #define COMP1 0xc9              /* 0x89 //0x09 */
64
65 #define MCK_INIT 0x63
66 #define MCK_INIT1 0x20          /*fixme: Bayer - 0x50 for JPEG ??*/
67
68 #define SYS_CLK 0x04
69
70 /* We calculate the autogain at the end of the transfer of a frame, at this
71    moment a frame with the old settings is being transmitted, and a frame is
72    being captured with the old settings. So if we adjust the autogain we must
73    ignore atleast the 2 next frames for the new settings to come into effect
74    before doing any other adjustments */
75 #define AUTOGAIN_IGNORE_FRAMES 3
76 #define AUTOGAIN_DEADZONE 1000
77 #define DESIRED_AVG_LUM 7000
78
79 /* V4L2 controls supported by the driver */
80 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
81 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
82 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
83 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
84 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
85 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
86 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
87 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
88 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
89 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
90
91 static struct ctrl sd_ctrls[] = {
92         {
93             {
94                 .id      = V4L2_CID_BRIGHTNESS,
95                 .type    = V4L2_CTRL_TYPE_INTEGER,
96                 .name    = "Brightness",
97                 .minimum = 0,
98                 .maximum = 255,
99                 .step    = 1,
100 #define BRIGHTNESS_DEF 127
101                 .default_value = BRIGHTNESS_DEF,
102             },
103             .set = sd_setbrightness,
104             .get = sd_getbrightness,
105         },
106         {
107             {
108                 .id      = V4L2_CID_GAIN,
109                 .type    = V4L2_CTRL_TYPE_INTEGER,
110                 .name    = "Gain",
111                 .minimum = 0,
112                 .maximum = 255,
113                 .step    = 1,
114 #define GAIN_DEF 127
115 #define GAIN_KNEE 200
116                 .default_value = GAIN_DEF,
117             },
118             .set = sd_setgain,
119             .get = sd_getgain,
120         },
121         {
122                 {
123                         .id = V4L2_CID_EXPOSURE,
124                         .type = V4L2_CTRL_TYPE_INTEGER,
125                         .name = "Exposure",
126 #define EXPOSURE_DEF  16 /*  32 ms / 30 fps */
127 #define EXPOSURE_KNEE 50 /* 100 ms / 10 fps */
128                         .minimum = 0,
129                         .maximum = 255,
130                         .step = 1,
131                         .default_value = EXPOSURE_DEF,
132                         .flags = 0,
133                 },
134                 .set = sd_setexposure,
135                 .get = sd_getexposure,
136         },
137         {
138                 {
139                         .id = V4L2_CID_AUTOGAIN,
140                         .type = V4L2_CTRL_TYPE_BOOLEAN,
141                         .name = "Automatic Gain (and Exposure)",
142                         .minimum = 0,
143                         .maximum = 1,
144                         .step = 1,
145 #define AUTOGAIN_DEF 1
146                         .default_value = AUTOGAIN_DEF,
147                         .flags = 0,
148                 },
149                 .set = sd_setautogain,
150                 .get = sd_getautogain,
151         },
152         {
153                 {
154                         .id      = V4L2_CID_POWER_LINE_FREQUENCY,
155                         .type    = V4L2_CTRL_TYPE_MENU,
156                         .name    = "Light frequency filter",
157                         .minimum = 0,
158                         .maximum = 2,   /* 0: 0, 1: 50Hz, 2:60Hz */
159                         .step    = 1,
160 #define FREQ_DEF 1
161                         .default_value = FREQ_DEF,
162                 },
163                 .set = sd_setfreq,
164                 .get = sd_getfreq,
165         },
166 };
167
168 static struct v4l2_pix_format vga_mode[] = {
169         {160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
170                 .bytesperline = 160,
171                 .sizeimage = 160 * 120,
172                 .colorspace = V4L2_COLORSPACE_SRGB,
173                 .priv = 2},
174         {320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
175                 .bytesperline = 320,
176                 .sizeimage = 320 * 240,
177                 .colorspace = V4L2_COLORSPACE_SRGB,
178                 .priv = 1},
179         {640, 480, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
180                 .bytesperline = 640,
181                 .sizeimage = 640 * 480,
182                 .colorspace = V4L2_COLORSPACE_SRGB,
183                 .priv = 0},
184 };
185 static struct v4l2_pix_format sif_mode[] = {
186         {176, 144, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
187                 .bytesperline = 176,
188                 .sizeimage = 176 * 144,
189                 .colorspace = V4L2_COLORSPACE_SRGB,
190                 .priv = 1},
191         {352, 288, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
192                 .bytesperline = 352,
193                 .sizeimage = 352 * 288,
194                 .colorspace = V4L2_COLORSPACE_SRGB,
195                 .priv = 0},
196 };
197
198 static const __u8 probe_ov7630[] = {0x08, 0x44};
199
200 static const __u8 initHv7131[] = {
201         0x46, 0x77, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,
202         0x00, 0x00,
203         0x00, 0x00, 0x00, 0x03, 0x01, 0x00,     /* shift from 0x02 0x01 0x00 */
204         0x28, 0x1e, 0x60, 0x8a, 0x20,
205         0x1d, 0x10, 0x02, 0x03, 0x0f, 0x0c
206 };
207 static const __u8 hv7131_sensor_init[][8] = {
208         {0xc0, 0x11, 0x31, 0x38, 0x2a, 0x2e, 0x00, 0x10},
209         {0xa0, 0x11, 0x01, 0x08, 0x2a, 0x2e, 0x00, 0x10},
210         {0xb0, 0x11, 0x20, 0x00, 0xd0, 0x2e, 0x00, 0x10},
211         {0xc0, 0x11, 0x25, 0x03, 0x0e, 0x28, 0x00, 0x16},
212         {0xa0, 0x11, 0x30, 0x10, 0x0e, 0x28, 0x00, 0x15},
213 };
214 static const __u8 initOv6650[] = {
215         0x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
216         0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
217         0x00, 0x02, 0x01, 0x0a, 0x16, 0x12, 0x68, 0x0b,
218         0x10, 0x1d, 0x10, 0x00, 0x06, 0x1f, 0x00
219 };
220 static const __u8 ov6650_sensor_init[][8] =
221 {
222         /* Bright, contrast, etc are set througth SCBB interface.
223          * AVCAP on win2 do not send any data on this   controls. */
224         /* Anyway, some registers appears to alter bright and constrat */
225
226         /* Reset sensor */
227         {0xa0, 0x60, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
228         /* Set clock register 0x11 low nibble is clock divider */
229         {0xd0, 0x60, 0x11, 0xc0, 0x1b, 0x18, 0xc1, 0x10},
230         /* Next some unknown stuff */
231         {0xb0, 0x60, 0x15, 0x00, 0x02, 0x18, 0xc1, 0x10},
232 /*      {0xa0, 0x60, 0x1b, 0x01, 0x02, 0x18, 0xc1, 0x10},
233                  * THIS SET GREEN SCREEN
234                  * (pixels could be innverted in decode kind of "brg",
235                  * but blue wont be there. Avoid this data ... */
236         {0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10}, /* format out? */
237         {0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10},
238         {0xa0, 0x60, 0x30, 0x3d, 0x0A, 0xd8, 0xa4, 0x10},
239         /* Enable rgb brightness control */
240         {0xa0, 0x60, 0x61, 0x08, 0x00, 0x00, 0x00, 0x10},
241         /* HDG: Note windows uses the line below, which sets both register 0x60
242            and 0x61 I believe these registers of the ov6650 are identical as
243            those of the ov7630, because if this is true the windows settings
244            add a bit additional red gain and a lot additional blue gain, which
245            matches my findings that the windows settings make blue much too
246            blue and red a little too red.
247         {0xb0, 0x60, 0x60, 0x66, 0x68, 0xd8, 0xa4, 0x10}, */
248         /* Some more unknown stuff */
249         {0xa0, 0x60, 0x68, 0x04, 0x68, 0xd8, 0xa4, 0x10},
250         {0xd0, 0x60, 0x17, 0x24, 0xd6, 0x04, 0x94, 0x10}, /* Clipreg */
251 };
252
253 static const __u8 initOv7630[] = {
254         0x04, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, /* r01 .. r08 */
255         0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* r09 .. r10 */
256         0x00, 0x02, 0x01, 0x0a,                         /* r11 .. r14 */
257         0x28, 0x1e,                     /* H & V sizes     r15 .. r16 */
258         0x68, COMP1, MCK_INIT1,                         /* r17 .. r19 */
259         0x1d, 0x10, 0x02, 0x03, 0x0f, 0x0c              /* r1a .. r1f */
260 };
261 static const __u8 initOv7630_3[] = {
262         0x44, 0x44, 0x00, 0x1a, 0x20, 0x20, 0x20, 0x80, /* r01 .. r08 */
263         0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, /* r09 .. r10 */
264         0x00, 0x01, 0x01, 0x0a,                         /* r11 .. r14 */
265         0x28, 0x1e,                     /* H & V sizes     r15 .. r16 */
266         0x68, 0x8f, MCK_INIT1,                          /* r17 .. r19 */
267         0x1d, 0x10, 0x02, 0x03, 0x0f, 0x0c, 0x00,       /* r1a .. r20 */
268         0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, /* r21 .. r28 */
269         0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0xff  /* r29 .. r30 */
270 };
271 static const __u8 ov7630_sensor_init[][8] = {
272         {0xa0, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
273         {0xb0, 0x21, 0x01, 0x77, 0x3a, 0x00, 0x00, 0x10},
274 /*      {0xd0, 0x21, 0x12, 0x7c, 0x01, 0x80, 0x34, 0x10},          jfm */
275         {0xd0, 0x21, 0x12, 0x1c, 0x00, 0x80, 0x34, 0x10},       /* jfm */
276         {0xa0, 0x21, 0x1b, 0x04, 0x00, 0x80, 0x34, 0x10},
277         {0xa0, 0x21, 0x20, 0x44, 0x00, 0x80, 0x34, 0x10},
278         {0xa0, 0x21, 0x23, 0xee, 0x00, 0x80, 0x34, 0x10},
279         {0xd0, 0x21, 0x26, 0xa0, 0x9a, 0xa0, 0x30, 0x10},
280         {0xb0, 0x21, 0x2a, 0x80, 0x00, 0xa0, 0x30, 0x10},
281         {0xb0, 0x21, 0x2f, 0x3d, 0x24, 0xa0, 0x30, 0x10},
282         {0xa0, 0x21, 0x32, 0x86, 0x24, 0xa0, 0x30, 0x10},
283         {0xb0, 0x21, 0x60, 0xa9, 0x4a, 0xa0, 0x30, 0x10},
284 /*      {0xb0, 0x21, 0x60, 0xa9, 0x42, 0xa0, 0x30, 0x10},        * jfm */
285         {0xa0, 0x21, 0x65, 0x00, 0x42, 0xa0, 0x30, 0x10},
286         {0xa0, 0x21, 0x69, 0x38, 0x42, 0xa0, 0x30, 0x10},
287         {0xc0, 0x21, 0x6f, 0x88, 0x0b, 0x00, 0x30, 0x10},
288         {0xc0, 0x21, 0x74, 0x21, 0x8e, 0x00, 0x30, 0x10},
289         {0xa0, 0x21, 0x7d, 0xf7, 0x8e, 0x00, 0x30, 0x10},
290         {0xd0, 0x21, 0x17, 0x1c, 0xbd, 0x06, 0xf6, 0x10},
291 };
292
293 static const __u8 initPas106[] = {
294         0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x40, 0x00, 0x00, 0x00,
295         0x00, 0x00,
296         0x00, 0x00, 0x00, 0x05, 0x01, 0x00,
297         0x16, 0x12, 0x28, COMP1, MCK_INIT1,
298         0x18, 0x10, 0x04, 0x03, 0x11, 0x0c
299 };
300 /* compression 0x86 mckinit1 0x2b */
301 static const __u8 pas106_data[][2] = {
302         {0x02, 0x04},           /* Pixel Clock Divider 6 */
303         {0x03, 0x13},           /* Frame Time MSB */
304 /*      {0x03, 0x12},            * Frame Time MSB */
305         {0x04, 0x06},           /* Frame Time LSB */
306 /*      {0x04, 0x05},            * Frame Time LSB */
307         {0x05, 0x65},           /* Shutter Time Line Offset */
308 /*      {0x05, 0x6d},            * Shutter Time Line Offset */
309 /*      {0x06, 0xb1},            * Shutter Time Pixel Offset */
310         {0x06, 0xcd},           /* Shutter Time Pixel Offset */
311         {0x07, 0xc1},           /* Black Level Subtract Sign */
312 /*      {0x07, 0x00},            * Black Level Subtract Sign */
313         {0x08, 0x06},           /* Black Level Subtract Level */
314         {0x08, 0x06},           /* Black Level Subtract Level */
315 /*      {0x08, 0x01},            * Black Level Subtract Level */
316         {0x09, 0x05},           /* Color Gain B Pixel 5 a */
317         {0x0a, 0x04},           /* Color Gain G1 Pixel 1 5 */
318         {0x0b, 0x04},           /* Color Gain G2 Pixel 1 0 5 */
319         {0x0c, 0x05},           /* Color Gain R Pixel 3 1 */
320         {0x0d, 0x00},           /* Color GainH  Pixel */
321         {0x0e, 0x0e},           /* Global Gain */
322         {0x0f, 0x00},           /* Contrast */
323         {0x10, 0x06},           /* H&V synchro polarity */
324         {0x11, 0x06},           /* ?default */
325         {0x12, 0x06},           /* DAC scale */
326         {0x14, 0x02},           /* ?default */
327         {0x13, 0x01},           /* Validate Settings */
328 };
329 static const __u8 initPas202[] = {
330         0x44, 0x44, 0x21, 0x30, 0x00, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0x00,
331         0x00, 0x00,
332         0x00, 0x00, 0x00, 0x07, 0x03, 0x0a,     /* 6 */
333         0x28, 0x1e, 0x28, 0x89, 0x30,
334         0x00, 0x00, 0x02, 0x03, 0x0f, 0x0c
335 };
336 static const __u8 pas202_sensor_init[][8] = {
337         {0xa0, 0x40, 0x02, 0x03, 0x00, 0x00, 0x00, 0x10},
338         {0xd0, 0x40, 0x04, 0x07, 0x34, 0x00, 0x09, 0x10},
339         {0xd0, 0x40, 0x08, 0x01, 0x00, 0x00, 0x01, 0x10},
340         {0xd0, 0x40, 0x0C, 0x00, 0x0C, 0x00, 0x32, 0x10},
341         {0xd0, 0x40, 0x10, 0x00, 0x01, 0x00, 0x63, 0x10},
342         {0xa0, 0x40, 0x15, 0x70, 0x01, 0x00, 0x63, 0x10},
343         {0xa0, 0x40, 0x18, 0x00, 0x01, 0x00, 0x63, 0x10},
344         {0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
345         {0xa0, 0x40, 0x03, 0x56, 0x01, 0x00, 0x63, 0x10},
346         {0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
347         {0xb0, 0x40, 0x04, 0x07, 0x2a, 0x00, 0x63, 0x10},
348         {0xb0, 0x40, 0x0e, 0x00, 0x3d, 0x00, 0x63, 0x10},
349
350         {0xa0, 0x40, 0x11, 0x01, 0x3d, 0x00, 0x63, 0x16},
351         {0xa0, 0x40, 0x10, 0x08, 0x3d, 0x00, 0x63, 0x15},
352         {0xa0, 0x40, 0x02, 0x04, 0x3d, 0x00, 0x63, 0x16},
353         {0xa0, 0x40, 0x11, 0x01, 0x3d, 0x00, 0x63, 0x16},
354         {0xb0, 0x40, 0x0e, 0x00, 0x31, 0x00, 0x63, 0x16},
355         {0xa0, 0x40, 0x11, 0x01, 0x31, 0x00, 0x63, 0x16},
356         {0xa0, 0x40, 0x10, 0x0e, 0x31, 0x00, 0x63, 0x15},
357         {0xa0, 0x40, 0x11, 0x01, 0x31, 0x00, 0x63, 0x16},
358 };
359
360 static const __u8 initTas5110[] = {
361         0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
362         0x00, 0x00,
363         0x00, 0x01, 0x00, 0x46, 0x09, 0x0a,     /* shift from 0x45 0x09 0x0a */
364         0x16, 0x12, 0x60, 0x86, 0x2b,
365         0x14, 0x0a, 0x02, 0x02, 0x09, 0x07
366 };
367 static const __u8 tas5110_sensor_init[][8] = {
368         {0x30, 0x11, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x10},
369         {0x30, 0x11, 0x02, 0x20, 0xa9, 0x00, 0x00, 0x10},
370         {0xa0, 0x61, 0x9a, 0xca, 0x00, 0x00, 0x00, 0x17},
371 };
372
373 static const __u8 initTas5130[] = {
374         0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
375         0x00, 0x00,
376         0x00, 0x01, 0x00, 0x69, 0x0c, 0x0a,
377         0x28, 0x1e, 0x60, COMP, MCK_INIT,
378         0x18, 0x10, 0x04, 0x03, 0x11, 0x0c
379 };
380 static const __u8 tas5130_sensor_init[][8] = {
381 /*      {0x30, 0x11, 0x00, 0x40, 0x47, 0x00, 0x00, 0x10},
382                                         * shutter 0x47 short exposure? */
383         {0x30, 0x11, 0x00, 0x40, 0x01, 0x00, 0x00, 0x10},
384                                         /* shutter 0x01 long exposure */
385         {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10},
386 };
387
388 /* get one byte in gspca_dev->usb_buf */
389 static void reg_r(struct gspca_dev *gspca_dev,
390                   __u16 value)
391 {
392         usb_control_msg(gspca_dev->dev,
393                         usb_rcvctrlpipe(gspca_dev->dev, 0),
394                         0,                      /* request */
395                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
396                         value,
397                         0,                      /* index */
398                         gspca_dev->usb_buf, 1,
399                         500);
400 }
401
402 static void reg_w(struct gspca_dev *gspca_dev,
403                   __u16 value,
404                   const __u8 *buffer,
405                   int len)
406 {
407 #ifdef CONFIG_VIDEO_ADV_DEBUG
408         if (len > sizeof gspca_dev->usb_buf) {
409                 PDEBUG(D_ERR|D_PACK, "reg_w: buffer overflow");
410                 return;
411         }
412 #endif
413         memcpy(gspca_dev->usb_buf, buffer, len);
414         usb_control_msg(gspca_dev->dev,
415                         usb_sndctrlpipe(gspca_dev->dev, 0),
416                         0x08,                   /* request */
417                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
418                         value,
419                         0,                      /* index */
420                         gspca_dev->usb_buf, len,
421                         500);
422 }
423
424 static void reg_w_big(struct gspca_dev *gspca_dev,
425                   __u16 value,
426                   const __u8 *buffer,
427                   int len)
428 {
429         __u8 *tmpbuf;
430
431         tmpbuf = kmalloc(len, GFP_KERNEL);
432         memcpy(tmpbuf, buffer, len);
433         usb_control_msg(gspca_dev->dev,
434                         usb_sndctrlpipe(gspca_dev->dev, 0),
435                         0x08,                   /* request */
436                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
437                         value,
438                         0,                      /* index */
439                         tmpbuf, len,
440                         500);
441         kfree(tmpbuf);
442 }
443
444 static int i2c_w(struct gspca_dev *gspca_dev, const __u8 *buffer)
445 {
446         int retry = 60;
447
448         /* is i2c ready */
449         reg_w(gspca_dev, 0x08, buffer, 8);
450         while (retry--) {
451                 msleep(10);
452                 reg_r(gspca_dev, 0x08);
453                 if (gspca_dev->usb_buf[0] & 0x04) {
454                         if (gspca_dev->usb_buf[0] & 0x08)
455                                 return -1;
456                         return 0;
457                 }
458         }
459         return -1;
460 }
461
462 static void i2c_w_vector(struct gspca_dev *gspca_dev,
463                         const __u8 buffer[][8], int len)
464 {
465         for (;;) {
466                 reg_w(gspca_dev, 0x08, *buffer, 8);
467                 len -= 8;
468                 if (len <= 0)
469                         break;
470                 buffer++;
471         }
472 }
473
474 static void setbrightness(struct gspca_dev *gspca_dev)
475 {
476         struct sd *sd = (struct sd *) gspca_dev;
477         __u8 value;
478
479         switch (sd->sensor) {
480         case  SENSOR_OV6650:
481         case  SENSOR_OV7630: {
482                 __u8 i2cOV[] =
483                         {0xa0, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10};
484
485                 /* change reg 0x06 */
486                 i2cOV[1] = sd->sensor_addr;
487                 i2cOV[3] = sd->brightness;
488                 if (i2c_w(gspca_dev, i2cOV) < 0)
489                         goto err;
490                 break;
491             }
492         case SENSOR_PAS106: {
493                 __u8 i2c1[] =
494                         {0xa1, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14};
495
496                 i2c1[3] = sd->brightness >> 3;
497                 i2c1[2] = 0x0e;
498                 if (i2c_w(gspca_dev, i2c1) < 0)
499                         goto err;
500                 i2c1[3] = 0x01;
501                 i2c1[2] = 0x13;
502                 if (i2c_w(gspca_dev, i2c1) < 0)
503                         goto err;
504                 break;
505             }
506         case SENSOR_PAS202: {
507                 /* __u8 i2cpexpo1[] =
508                         {0xb0, 0x40, 0x04, 0x07, 0x2a, 0x00, 0x63, 0x16}; */
509                 __u8 i2cpexpo[] =
510                         {0xb0, 0x40, 0x0e, 0x01, 0xab, 0x00, 0x63, 0x16};
511                 __u8 i2cp202[] =
512                         {0xa0, 0x40, 0x10, 0x0e, 0x31, 0x00, 0x63, 0x15};
513                 static __u8 i2cpdoit[] =
514                         {0xa0, 0x40, 0x11, 0x01, 0x31, 0x00, 0x63, 0x16};
515
516                 /* change reg 0x10 */
517                 i2cpexpo[4] = 0xff - sd->brightness;
518 /*              if(i2c_w(gspca_dev,i2cpexpo1) < 0)
519                         goto err; */
520 /*              if(i2c_w(gspca_dev,i2cpdoit) < 0)
521                         goto err; */
522                 if (i2c_w(gspca_dev, i2cpexpo) < 0)
523                         goto err;
524                 if (i2c_w(gspca_dev, i2cpdoit) < 0)
525                         goto err;
526                 i2cp202[3] = sd->brightness >> 3;
527                 if (i2c_w(gspca_dev, i2cp202) < 0)
528                         goto err;
529                 if (i2c_w(gspca_dev, i2cpdoit) < 0)
530                         goto err;
531                 break;
532             }
533         case SENSOR_TAS5130CXX: {
534                 __u8 i2c[] =
535                         {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};
536
537                 value = 0xff - sd->brightness;
538                 i2c[4] = value;
539                 PDEBUG(D_CONF, "brightness %d : %d", value, i2c[4]);
540                 if (i2c_w(gspca_dev, i2c) < 0)
541                         goto err;
542                 break;
543             }
544         case SENSOR_TAS5110:
545                 /* FIXME figure out howto control brightness on TAS5110 */
546                 break;
547         }
548         return;
549 err:
550         PDEBUG(D_ERR, "i2c error brightness");
551 }
552
553 static void setsensorgain(struct gspca_dev *gspca_dev)
554 {
555         struct sd *sd = (struct sd *) gspca_dev;
556         unsigned char gain = sd->gain;
557
558         switch (sd->sensor) {
559
560         case SENSOR_TAS5110: {
561                 __u8 i2c[] =
562                         {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};
563
564                 i2c[4] = 255 - gain;
565                 if (i2c_w(gspca_dev, i2c) < 0)
566                         goto err;
567                 break;
568             }
569
570         case SENSOR_OV6650:
571                 gain >>= 1;
572                 /* fall thru */
573         case SENSOR_OV7630: {
574                 __u8 i2c[] = {0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
575
576                 i2c[1] = sd->sensor_addr;
577                 i2c[3] = gain >> 2;
578                 if (i2c_w(gspca_dev, i2c) < 0)
579                         goto err;
580                 break;
581             }
582         }
583         return;
584 err:
585         PDEBUG(D_ERR, "i2c error gain");
586 }
587
588 static void setgain(struct gspca_dev *gspca_dev)
589 {
590         struct sd *sd = (struct sd *) gspca_dev;
591         __u8 gain;
592         __u8 rgb_value;
593
594         gain = sd->gain >> 4;
595
596         /* red and blue gain */
597         rgb_value = gain << 4 | gain;
598         reg_w(gspca_dev, 0x10, &rgb_value, 1);
599         /* green gain */
600         rgb_value = gain;
601         reg_w(gspca_dev, 0x11, &rgb_value, 1);
602
603         if (sd->sensor_has_gain)
604                 setsensorgain(gspca_dev);
605 }
606
607 static void setexposure(struct gspca_dev *gspca_dev)
608 {
609         struct sd *sd = (struct sd *) gspca_dev;
610
611         switch (sd->sensor) {
612         case SENSOR_TAS5110: {
613                 __u8 reg;
614
615                 /* register 19's high nibble contains the sn9c10x clock divider
616                    The high nibble configures the no fps according to the
617                    formula: 60 / high_nibble. With a maximum of 30 fps */
618                 reg = 120 * sd->exposure / 1000;
619                 if (reg < 2)
620                         reg = 2;
621                 else if (reg > 15)
622                         reg = 15;
623                 reg = (reg << 4) | 0x0b;
624                 reg_w(gspca_dev, 0x19, &reg, 1);
625                 break;
626             }
627         case SENSOR_OV6650:
628         case SENSOR_OV7630: {
629                 /* The ov6650 / ov7630 have 2 registers which both influence
630                    exposure, register 11, whose low nibble sets the nr off fps
631                    according to: fps = 30 / (low_nibble + 1)
632
633                    The fps configures the maximum exposure setting, but it is
634                    possible to use less exposure then what the fps maximum
635                    allows by setting register 10. register 10 configures the
636                    actual exposure as quotient of the full exposure, with 0
637                    being no exposure at all (not very usefull) and reg10_max
638                    being max exposure possible at that framerate.
639
640                    The code maps our 0 - 510 ms exposure ctrl to these 2
641                    registers, trying to keep fps as high as possible.
642                 */
643                 __u8 i2c[] = {0xb0, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10};
644                 int reg10, reg11, reg10_max;
645
646                 /* ov6645 datasheet says reg10_max is 9a, but that uses
647                    tline * 2 * reg10 as formula for calculating texpo, the
648                    ov6650 probably uses the same formula as the 7730 which uses
649                    tline * 4 * reg10, which explains why the reg10max we've
650                    found experimentally for the ov6650 is exactly half that of
651                    the ov6645. The ov7630 datasheet says the max is 0x41. */
652                 if (sd->sensor == SENSOR_OV6650) {
653                         reg10_max = 0x4d;
654                         i2c[4] = 0xc0; /* OV6650 needs non default vsync pol */
655                 } else
656                         reg10_max = 0x41;
657
658                 reg11 = (60 * sd->exposure + 999) / 1000;
659                 if (reg11 < 1)
660                         reg11 = 1;
661                 else if (reg11 > 16)
662                         reg11 = 16;
663
664                 /* frame exposure time in ms = 1000 * reg11 / 30    ->
665                 reg10 = sd->exposure * 2 * reg10_max / (1000 * reg11 / 30) */
666                 reg10 = (sd->exposure * 60 * reg10_max) / (1000 * reg11);
667
668                 /* Don't allow this to get below 10 when using autogain, the
669                    steps become very large (relatively) when below 10 causing
670                    the image to oscilate from much too dark, to much too bright
671                    and back again. */
672                 if (sd->autogain && reg10 < 10)
673                         reg10 = 10;
674                 else if (reg10 > reg10_max)
675                         reg10 = reg10_max;
676
677                 /* In 640x480, if the reg11 has less than 3, the image is
678                    unstable (not enough bandwidth). */
679                 if (gspca_dev->width == 640 && reg11 < 3)
680                         reg11 = 3;
681
682                 /* Write reg 10 and reg11 low nibble */
683                 i2c[1] = sd->sensor_addr;
684                 i2c[3] = reg10;
685                 i2c[4] |= reg11 - 1;
686
687                 /* If register 11 didn't change, don't change it */
688                 if (sd->reg11 == reg11 )
689                         i2c[0] = 0xa0;
690
691                 if (i2c_w(gspca_dev, i2c) == 0)
692                         sd->reg11 = reg11;
693                 else
694                         PDEBUG(D_ERR, "i2c error exposure");
695                 break;
696             }
697         }
698 }
699
700 static void setfreq(struct gspca_dev *gspca_dev)
701 {
702         struct sd *sd = (struct sd *) gspca_dev;
703
704         switch (sd->sensor) {
705         case SENSOR_OV6650:
706         case SENSOR_OV7630: {
707                 /* Framerate adjust register for artificial light 50 hz flicker
708                    compensation, for the ov6650 this is identical to ov6630
709                    0x2b register, see ov6630 datasheet.
710                    0x4f / 0x8a -> (30 fps -> 25 fps), 0x00 -> no adjustment */
711                 __u8 i2c[] = {0xa0, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10};
712                 switch (sd->freq) {
713                 default:
714 /*              case 0:                  * no filter*/
715 /*              case 2:                  * 60 hz */
716                         i2c[3] = 0;
717                         break;
718                 case 1:                 /* 50 hz */
719                         i2c[3] = (sd->sensor == SENSOR_OV6650)
720                                         ? 0x4f : 0x8a;
721                         break;
722                 }
723                 i2c[1] = sd->sensor_addr;
724                 if (i2c_w(gspca_dev, i2c) < 0)
725                         PDEBUG(D_ERR, "i2c error setfreq");
726                 break;
727             }
728         }
729 }
730
731 static void do_autogain(struct gspca_dev *gspca_dev)
732 {
733         struct sd *sd = (struct sd *) gspca_dev;
734         int avg_lum = atomic_read(&sd->avg_lum);
735
736         if (avg_lum == -1)
737                 return;
738
739         if (sd->autogain_ignore_frames > 0)
740                 sd->autogain_ignore_frames--;
741         else if (gspca_auto_gain_n_exposure(gspca_dev, avg_lum,
742                         sd->brightness * DESIRED_AVG_LUM / 127,
743                         AUTOGAIN_DEADZONE, GAIN_KNEE, EXPOSURE_KNEE)) {
744                 PDEBUG(D_FRAM, "autogain: gain changed: gain: %d expo: %d\n",
745                         (int)sd->gain, (int)sd->exposure);
746                 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
747         }
748 }
749
750 /* this function is called at probe time */
751 static int sd_config(struct gspca_dev *gspca_dev,
752                         const struct usb_device_id *id)
753 {
754         struct sd *sd = (struct sd *) gspca_dev;
755         struct cam *cam;
756         __u16 product;
757         int sif = 0;
758
759         /* nctrls depends upon the sensor, so we use a per cam copy */
760         memcpy(&sd->sd_desc, gspca_dev->sd_desc, sizeof(struct sd_desc));
761         gspca_dev->sd_desc = &sd->sd_desc;
762
763         sd->fr_h_sz = 12;               /* default size of the frame header */
764         sd->sd_desc.nctrls = 2;         /* default nb of ctrls */
765         product = id->idProduct;
766 /*      switch (id->idVendor) { */
767 /*      case 0x0c45:                             * Sonix */
768                 switch (product) {
769                 case 0x6001:                    /* SN9C102 */
770                 case 0x6005:                    /* SN9C101 */
771                 case 0x6007:                    /* SN9C101 */
772                         sd->sensor = SENSOR_TAS5110;
773                         sd->sensor_has_gain = 1;
774                         sd->sd_desc.nctrls = 4;
775                         sd->sd_desc.dq_callback = do_autogain;
776                         sif = 1;
777                         break;
778                 case 0x6009:                    /* SN9C101 */
779                 case 0x600d:                    /* SN9C101 */
780                 case 0x6029:                    /* SN9C101 */
781                         sd->sensor = SENSOR_PAS106;
782                         sif = 1;
783                         break;
784                 case 0x6011:                    /* SN9C101 - SN9C101G */
785                         sd->sensor = SENSOR_OV6650;
786                         sd->sensor_has_gain = 1;
787                         sd->sensor_addr = 0x60;
788                         sd->sd_desc.nctrls = 5;
789                         sd->sd_desc.dq_callback = do_autogain;
790                         sif = 1;
791                         break;
792                 case 0x6019:                    /* SN9C101 */
793                 case 0x602c:                    /* SN9C102 */
794                 case 0x602e:                    /* SN9C102 */
795                 case 0x60b0:                    /* SN9C103 */
796                         sd->sensor = SENSOR_OV7630;
797                         sd->sensor_addr = 0x21;
798                         sd->sensor_has_gain = 1;
799                         sd->sd_desc.nctrls = 5;
800                         sd->sd_desc.dq_callback = do_autogain;
801                         if (product == 0x60b0)
802                                 sd->fr_h_sz = 18; /* size of frame header */
803                         break;
804                 case 0x6024:                    /* SN9C102 */
805                 case 0x6025:                    /* SN9C102 */
806                         sd->sensor = SENSOR_TAS5130CXX;
807                         break;
808                 case 0x6028:                    /* SN9C102 */
809                         sd->sensor = SENSOR_PAS202;
810                         break;
811                 case 0x602d:                    /* SN9C102 */
812                         sd->sensor = SENSOR_HV7131R;
813                         break;
814                 case 0x60af:                    /* SN9C103 */
815                         sd->sensor = SENSOR_PAS202;
816                         sd->fr_h_sz = 18;       /* size of frame header (?) */
817                         break;
818                 }
819 /*              break; */
820 /*      } */
821
822         cam = &gspca_dev->cam;
823         cam->dev_name = (char *) id->driver_info;
824         cam->epaddr = 0x01;
825         if (!sif) {
826                 cam->cam_mode = vga_mode;
827                 cam->nmodes = ARRAY_SIZE(vga_mode);
828                 if (product == 0x60b0) { /* SN9C103 with OV7630 */
829                         /* We only have 320x240 & 640x480 */
830                         cam->cam_mode++;
831                         cam->nmodes--;
832                 }
833         } else {
834                 cam->cam_mode = sif_mode;
835                 cam->nmodes = ARRAY_SIZE(sif_mode);
836         }
837         sd->brightness = BRIGHTNESS_DEF;
838         sd->gain = GAIN_DEF;
839         sd->exposure = EXPOSURE_DEF;
840         sd->autogain = AUTOGAIN_DEF;
841         sd->freq = FREQ_DEF;
842
843         if (product == 0x60b0) /* SN9C103 with OV7630 */
844                 reg_w(gspca_dev, 0x01, probe_ov7630, sizeof probe_ov7630);
845
846         return 0;
847 }
848
849 /* this function is called at open time */
850 static int sd_open(struct gspca_dev *gspca_dev)
851 {
852         reg_r(gspca_dev, 0x00);
853         if (gspca_dev->usb_buf[0] != 0x10)
854                 return -ENODEV;
855         return 0;
856 }
857
858 static void pas106_i2cinit(struct gspca_dev *gspca_dev)
859 {
860         int i;
861         const __u8 *data;
862         __u8 i2c1[] = { 0xa1, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14 };
863
864         i = ARRAY_SIZE(pas106_data);
865         data = pas106_data[0];
866         while (--i >= 0) {
867                 memcpy(&i2c1[2], data, 2);
868                                         /* copy 2 bytes from the template */
869                 if (i2c_w(gspca_dev, i2c1) < 0)
870                         PDEBUG(D_ERR, "i2c error pas106");
871                 data += 2;
872         }
873 }
874
875 /* -- start the camera -- */
876 static void sd_start(struct gspca_dev *gspca_dev)
877 {
878         struct sd *sd = (struct sd *) gspca_dev;
879         int mode, l = 0x1f;
880         const __u8 *sn9c10x;
881         __u8 reg01, reg17;
882         __u8 reg17_19[3];
883
884         mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
885         switch (sd->sensor) {
886         case SENSOR_HV7131R:
887                 sn9c10x = initHv7131;
888                 reg17_19[0] = 0x60;
889                 reg17_19[1] = (mode << 4) | 0x8a;
890                 reg17_19[2] = 0x20;
891                 break;
892         case SENSOR_OV6650:
893                 sn9c10x = initOv6650;
894                 reg17_19[0] = 0x68;
895                 reg17_19[1] = (mode << 4) | 0x8b;
896                 reg17_19[2] = 0x20;
897                 break;
898         case SENSOR_OV7630:
899                 if (sd->fr_h_sz == 18) { /* SN9C103 */
900                         sn9c10x = initOv7630_3;
901                         l = sizeof initOv7630_3;
902                 } else
903                         sn9c10x = initOv7630;
904                 reg17_19[0] = 0x68;
905                 reg17_19[1] = (mode << 4) | COMP2;
906                 reg17_19[2] = MCK_INIT1;
907                 break;
908         case SENSOR_PAS106:
909                 sn9c10x = initPas106;
910                 reg17_19[0] = 0x24;             /* 0x28 */
911                 reg17_19[1] = (mode << 4) | COMP1;
912                 reg17_19[2] = MCK_INIT1;
913                 break;
914         case SENSOR_PAS202:
915                 sn9c10x = initPas202;
916                 reg17_19[0] = mode ? 0x24 : 0x20;
917                 reg17_19[1] = (mode << 4) | 0x89;
918                 reg17_19[2] = 0x20;
919                 break;
920         case SENSOR_TAS5110:
921                 sn9c10x = initTas5110;
922                 reg17_19[0] = 0x60;
923                 reg17_19[1] = (mode << 4) | 0x86;
924                 reg17_19[2] = 0x2b;             /* 0xf3; */
925                 break;
926         default:
927 /*      case SENSOR_TAS5130CXX: */
928                 sn9c10x = initTas5130;
929                 reg17_19[0] = 0x60;
930                 reg17_19[1] = (mode << 4) | COMP;
931                 reg17_19[2] = mode ? 0x23 : 0x43;
932                 break;
933         }
934
935         /* Special case for SN9C101/2 with OV 7630 */
936         /* HDG: is this really necessary we overwrite the values immediately
937            afterwards with the ones from the template ?? */
938         if (sd->sensor == SENSOR_OV7630 && sd->fr_h_sz == 12) {
939                 reg01 = 0x06;
940                 reg17 = 0x29;
941         } else {
942                 reg01 = sn9c10x[0];
943                 reg17 = sn9c10x[0x17 - 1];
944         }
945
946         /* reg 0x01 bit 2 video transfert on */
947         reg_w(gspca_dev, 0x01, &reg01, 1);
948         /* reg 0x17 SensorClk enable inv Clk 0x60 */
949         reg_w(gspca_dev, 0x17, &reg17, 1);
950         /* Set the registers from the template */
951         reg_w_big(gspca_dev, 0x01, sn9c10x, l);
952         switch (sd->sensor) {
953         case SENSOR_HV7131R:
954                 i2c_w_vector(gspca_dev, hv7131_sensor_init,
955                                 sizeof hv7131_sensor_init);
956                 break;
957         case SENSOR_OV6650:
958                 i2c_w_vector(gspca_dev, ov6650_sensor_init,
959                                 sizeof ov6650_sensor_init);
960                 break;
961         case SENSOR_OV7630:
962                 i2c_w_vector(gspca_dev, ov7630_sensor_init,
963                                 sizeof ov7630_sensor_init);
964                 if (sd->fr_h_sz == 18) { /* SN9C103 */
965                         const __u8 i2c[] = { 0xa0, 0x21, 0x13, 0x80, 0x00,
966                                                 0x00, 0x00, 0x10 };
967                         i2c_w(gspca_dev, i2c);
968                 }
969                 break;
970         case SENSOR_PAS106:
971                 pas106_i2cinit(gspca_dev);
972                 break;
973         case SENSOR_PAS202:
974                 i2c_w_vector(gspca_dev, pas202_sensor_init,
975                                 sizeof pas202_sensor_init);
976                 break;
977         case SENSOR_TAS5110:
978                 i2c_w_vector(gspca_dev, tas5110_sensor_init,
979                                 sizeof tas5110_sensor_init);
980                 break;
981         default:
982 /*      case SENSOR_TAS5130CXX: */
983                 i2c_w_vector(gspca_dev, tas5130_sensor_init,
984                                 sizeof tas5130_sensor_init);
985                 break;
986         }
987         /* H_size V_size 0x28, 0x1e -> 640x480. 0x16, 0x12 -> 352x288 */
988         reg_w(gspca_dev, 0x15, &sn9c10x[0x15 - 1], 2);
989         /* compression register */
990         reg_w(gspca_dev, 0x18, &reg17_19[1], 1);
991         /* H_start */
992         reg_w(gspca_dev, 0x12, &sn9c10x[0x12 - 1], 1);
993         /* V_START */
994         reg_w(gspca_dev, 0x13, &sn9c10x[0x13 - 1], 1);
995         /* reset 0x17 SensorClk enable inv Clk 0x60 */
996                                 /*fixme: ov7630 [17]=68 8f (+20 if 102)*/
997         reg_w(gspca_dev, 0x17, &reg17_19[0], 1);
998         /*MCKSIZE ->3 */        /*fixme: not ov7630*/
999         reg_w(gspca_dev, 0x19, &reg17_19[2], 1);
1000         /* AE_STRX AE_STRY AE_ENDX AE_ENDY */
1001         reg_w(gspca_dev, 0x1c, &sn9c10x[0x1c - 1], 4);
1002         /* Enable video transfert */
1003         reg_w(gspca_dev, 0x01, &sn9c10x[0], 1);
1004         /* Compression */
1005         reg_w(gspca_dev, 0x18, &reg17_19[1], 2);
1006         msleep(20);
1007
1008         sd->reg11 = -1;
1009
1010         setgain(gspca_dev);
1011         setbrightness(gspca_dev);
1012         setexposure(gspca_dev);
1013         setfreq(gspca_dev);
1014
1015         sd->frames_to_drop = 0;
1016         sd->autogain_ignore_frames = 0;
1017         atomic_set(&sd->avg_lum, -1);
1018 }
1019
1020 static void sd_stopN(struct gspca_dev *gspca_dev)
1021 {
1022         __u8 ByteSend;
1023
1024         ByteSend = 0x09;        /* 0X00 */
1025         reg_w(gspca_dev, 0x01, &ByteSend, 1);
1026 }
1027
1028 static void sd_stop0(struct gspca_dev *gspca_dev)
1029 {
1030 }
1031
1032 static void sd_close(struct gspca_dev *gspca_dev)
1033 {
1034 }
1035
1036 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1037                         struct gspca_frame *frame,      /* target */
1038                         unsigned char *data,            /* isoc packet */
1039                         int len)                        /* iso packet length */
1040 {
1041         int i;
1042         struct sd *sd = (struct sd *) gspca_dev;
1043
1044         /* frames start with:
1045          *      ff ff 00 c4 c4 96       synchro
1046          *      00              (unknown)
1047          *      xx              (frame sequence / size / compression)
1048          *      (xx)            (idem - extra byte for sn9c103)
1049          *      ll mm           brightness sum inside auto exposure
1050          *      ll mm           brightness sum outside auto exposure
1051          *      (xx xx xx xx xx)        audio values for snc103
1052          */
1053         if (len > 6 && len < 24) {
1054                 for (i = 0; i < len - 6; i++) {
1055                         if (data[0 + i] == 0xff
1056                             && data[1 + i] == 0xff
1057                             && data[2 + i] == 0x00
1058                             && data[3 + i] == 0xc4
1059                             && data[4 + i] == 0xc4
1060                             && data[5 + i] == 0x96) {   /* start of frame */
1061                                 int lum = -1;
1062                                 int pkt_type = LAST_PACKET;
1063
1064                                 if (len - i < sd->fr_h_sz) {
1065                                         PDEBUG(D_STREAM, "packet too short to"
1066                                                 " get avg brightness");
1067                                 } else if (sd->fr_h_sz == 12) {
1068                                         lum = data[i + 8] + (data[i + 9] << 8);
1069                                 } else {
1070                                         lum = data[i + 9] +
1071                                                 (data[i + 10] << 8);
1072                                 }
1073                                 if (lum == 0) {
1074                                         lum = -1;
1075                                         sd->frames_to_drop = 2;
1076                                 }
1077                                 atomic_set(&sd->avg_lum, lum);
1078
1079                                 if (sd->frames_to_drop) {
1080                                         sd->frames_to_drop--;
1081                                         pkt_type = DISCARD_PACKET;
1082                                 }
1083
1084                                 frame = gspca_frame_add(gspca_dev, pkt_type,
1085                                                         frame, data, 0);
1086                                 data += i + sd->fr_h_sz;
1087                                 len -= i + sd->fr_h_sz;
1088                                 gspca_frame_add(gspca_dev, FIRST_PACKET,
1089                                                 frame, data, len);
1090                                 return;
1091                         }
1092                 }
1093         }
1094         gspca_frame_add(gspca_dev, INTER_PACKET,
1095                         frame, data, len);
1096 }
1097
1098 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
1099 {
1100         struct sd *sd = (struct sd *) gspca_dev;
1101
1102         sd->brightness = val;
1103         if (gspca_dev->streaming)
1104                 setbrightness(gspca_dev);
1105         return 0;
1106 }
1107
1108 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
1109 {
1110         struct sd *sd = (struct sd *) gspca_dev;
1111
1112         *val = sd->brightness;
1113         return 0;
1114 }
1115
1116 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1117 {
1118         struct sd *sd = (struct sd *) gspca_dev;
1119
1120         sd->gain = val;
1121         if (gspca_dev->streaming)
1122                 setgain(gspca_dev);
1123         return 0;
1124 }
1125
1126 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1127 {
1128         struct sd *sd = (struct sd *) gspca_dev;
1129
1130         *val = sd->gain;
1131         return 0;
1132 }
1133
1134 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
1135 {
1136         struct sd *sd = (struct sd *) gspca_dev;
1137
1138         sd->exposure = val;
1139         if (gspca_dev->streaming)
1140                 setexposure(gspca_dev);
1141         return 0;
1142 }
1143
1144 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
1145 {
1146         struct sd *sd = (struct sd *) gspca_dev;
1147
1148         *val = sd->exposure;
1149         return 0;
1150 }
1151
1152 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1153 {
1154         struct sd *sd = (struct sd *) gspca_dev;
1155
1156         sd->autogain = val;
1157         /* when switching to autogain set defaults to make sure
1158            we are on a valid point of the autogain gain /
1159            exposure knee graph, and give this change time to
1160            take effect before doing autogain. */
1161         if (sd->autogain) {
1162                 sd->exposure = EXPOSURE_DEF;
1163                 sd->gain = GAIN_DEF;
1164                 if (gspca_dev->streaming) {
1165                         sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
1166                         setexposure(gspca_dev);
1167                         setgain(gspca_dev);
1168                 }
1169         }
1170
1171         return 0;
1172 }
1173
1174 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1175 {
1176         struct sd *sd = (struct sd *) gspca_dev;
1177
1178         *val = sd->autogain;
1179         return 0;
1180 }
1181
1182 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
1183 {
1184         struct sd *sd = (struct sd *) gspca_dev;
1185
1186         sd->freq = val;
1187         if (gspca_dev->streaming)
1188                 setfreq(gspca_dev);
1189         return 0;
1190 }
1191
1192 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
1193 {
1194         struct sd *sd = (struct sd *) gspca_dev;
1195
1196         *val = sd->freq;
1197         return 0;
1198 }
1199
1200 static int sd_querymenu(struct gspca_dev *gspca_dev,
1201                         struct v4l2_querymenu *menu)
1202 {
1203         switch (menu->id) {
1204         case V4L2_CID_POWER_LINE_FREQUENCY:
1205                 switch (menu->index) {
1206                 case 0:         /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1207                         strcpy((char *) menu->name, "NoFliker");
1208                         return 0;
1209                 case 1:         /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1210                         strcpy((char *) menu->name, "50 Hz");
1211                         return 0;
1212                 case 2:         /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
1213                         strcpy((char *) menu->name, "60 Hz");
1214                         return 0;
1215                 }
1216                 break;
1217         }
1218         return -EINVAL;
1219 }
1220
1221 /* sub-driver description */
1222 static const struct sd_desc sd_desc = {
1223         .name = MODULE_NAME,
1224         .ctrls = sd_ctrls,
1225         .nctrls = ARRAY_SIZE(sd_ctrls),
1226         .config = sd_config,
1227         .open = sd_open,
1228         .start = sd_start,
1229         .stopN = sd_stopN,
1230         .stop0 = sd_stop0,
1231         .close = sd_close,
1232         .pkt_scan = sd_pkt_scan,
1233         .querymenu = sd_querymenu,
1234 };
1235
1236 /* -- module initialisation -- */
1237 #define DVNM(name) .driver_info = (kernel_ulong_t) name
1238 static __devinitdata struct usb_device_id device_table[] = {
1239 #ifndef CONFIG_USB_SN9C102
1240         {USB_DEVICE(0x0c45, 0x6001), DVNM("Genius VideoCAM NB")},
1241         {USB_DEVICE(0x0c45, 0x6005), DVNM("Sweex Tas5110")},
1242         {USB_DEVICE(0x0c45, 0x6007), DVNM("Sonix sn9c101 + Tas5110D")},
1243         {USB_DEVICE(0x0c45, 0x6009), DVNM("spcaCam@120")},
1244         {USB_DEVICE(0x0c45, 0x600d), DVNM("spcaCam@120")},
1245 #endif
1246         {USB_DEVICE(0x0c45, 0x6011), DVNM("MAX Webcam Microdia")},
1247 #ifndef CONFIG_USB_SN9C102
1248         {USB_DEVICE(0x0c45, 0x6019), DVNM("Generic Sonix OV7630")},
1249         {USB_DEVICE(0x0c45, 0x6024), DVNM("Generic Sonix Tas5130c")},
1250         {USB_DEVICE(0x0c45, 0x6025), DVNM("Xcam Shanga")},
1251         {USB_DEVICE(0x0c45, 0x6028), DVNM("Sonix Btc Pc380")},
1252         {USB_DEVICE(0x0c45, 0x6029), DVNM("spcaCam@150")},
1253         {USB_DEVICE(0x0c45, 0x602c), DVNM("Generic Sonix OV7630")},
1254         {USB_DEVICE(0x0c45, 0x602d), DVNM("LIC-200 LG")},
1255         {USB_DEVICE(0x0c45, 0x602e), DVNM("Genius VideoCam Messenger")},
1256         {USB_DEVICE(0x0c45, 0x60af), DVNM("Trust WB3100P")},
1257         {USB_DEVICE(0x0c45, 0x60b0), DVNM("Genius VideoCam Look")},
1258 #endif
1259         {}
1260 };
1261 MODULE_DEVICE_TABLE(usb, device_table);
1262
1263 /* -- device connect -- */
1264 static int sd_probe(struct usb_interface *intf,
1265                         const struct usb_device_id *id)
1266 {
1267         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1268                                 THIS_MODULE);
1269 }
1270
1271 static struct usb_driver sd_driver = {
1272         .name = MODULE_NAME,
1273         .id_table = device_table,
1274         .probe = sd_probe,
1275         .disconnect = gspca_disconnect,
1276 };
1277
1278 /* -- module insert / remove -- */
1279 static int __init sd_mod_init(void)
1280 {
1281         if (usb_register(&sd_driver) < 0)
1282                 return -1;
1283         PDEBUG(D_PROBE, "registered");
1284         return 0;
1285 }
1286 static void __exit sd_mod_exit(void)
1287 {
1288         usb_deregister(&sd_driver);
1289         PDEBUG(D_PROBE, "deregistered");
1290 }
1291
1292 module_init(sd_mod_init);
1293 module_exit(sd_mod_exit);