GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / media / usb / gspca / sonixb.c
1 /*
2  *              sonix sn9c102 (bayer) library
3  *
4  * Copyright (C) 2009-2011 Jean-François Moine <http://moinejf.free.fr>
5  * Copyright (C) 2003 2004 Michel Xhaard mxhaard@magic.fr
6  * Add Pas106 Stefano Mozzi (C) 2004
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
19 /* Some documentation on known sonixb registers:
20
21 Reg     Use
22 sn9c101 / sn9c102:
23 0x10    high nibble red gain low nibble blue gain
24 0x11    low nibble green gain
25 sn9c103:
26 0x05    red gain 0-127
27 0x06    blue gain 0-127
28 0x07    green gain 0-127
29 all:
30 0x08-0x0f i2c / 3wire registers
31 0x12    hstart
32 0x13    vstart
33 0x15    hsize (hsize = register-value * 16)
34 0x16    vsize (vsize = register-value * 16)
35 0x17    bit 0 toggle compression quality (according to sn9c102 driver)
36 0x18    bit 7 enables compression, bit 4-5 set image down scaling:
37         00 scale 1, 01 scale 1/2, 10, scale 1/4
38 0x19    high-nibble is sensor clock divider, changes exposure on sensors which
39         use a clock generated by the bridge. Some sensors have their own clock.
40 0x1c    auto_exposure area (for avg_lum) startx (startx = register-value * 32)
41 0x1d    auto_exposure area (for avg_lum) starty (starty = register-value * 32)
42 0x1e    auto_exposure area (for avg_lum) stopx (hsize = (0x1e - 0x1c) * 32)
43 0x1f    auto_exposure area (for avg_lum) stopy (vsize = (0x1f - 0x1d) * 32)
44 */
45
46 #define MODULE_NAME "sonixb"
47
48 #include <linux/input.h>
49 #include "gspca.h"
50
51 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
52 MODULE_DESCRIPTION("GSPCA/SN9C102 USB Camera Driver");
53 MODULE_LICENSE("GPL");
54
55 /* specific webcam descriptor */
56 struct sd {
57         struct gspca_dev gspca_dev;     /* !! must be the first item */
58
59         struct v4l2_ctrl *brightness;
60         struct v4l2_ctrl *plfreq;
61
62         atomic_t avg_lum;
63         int prev_avg_lum;
64         int exposure_knee;
65         int header_read;
66         u8 header[12]; /* Header without sof marker */
67
68         unsigned char autogain_ignore_frames;
69         unsigned char frames_to_drop;
70
71         __u8 bridge;                    /* Type of bridge */
72 #define BRIDGE_101 0
73 #define BRIDGE_102 0 /* We make no difference between 101 and 102 */
74 #define BRIDGE_103 1
75
76         __u8 sensor;                    /* Type of image sensor chip */
77 #define SENSOR_HV7131D 0
78 #define SENSOR_HV7131R 1
79 #define SENSOR_OV6650 2
80 #define SENSOR_OV7630 3
81 #define SENSOR_PAS106 4
82 #define SENSOR_PAS202 5
83 #define SENSOR_TAS5110C 6
84 #define SENSOR_TAS5110D 7
85 #define SENSOR_TAS5130CXX 8
86         __u8 reg11;
87 };
88
89 typedef const __u8 sensor_init_t[8];
90
91 struct sensor_data {
92         const __u8 *bridge_init;
93         sensor_init_t *sensor_init;
94         int sensor_init_size;
95         int flags;
96         __u8 sensor_addr;
97 };
98
99 /* sensor_data flags */
100 #define F_SIF           0x01    /* sif or vga */
101
102 /* priv field of struct v4l2_pix_format flags (do not use low nibble!) */
103 #define MODE_RAW 0x10           /* raw bayer mode */
104 #define MODE_REDUCED_SIF 0x20   /* vga mode (320x240 / 160x120) on sif cam */
105
106 #define COMP 0xc7               /* 0x87 //0x07 */
107 #define COMP1 0xc9              /* 0x89 //0x09 */
108
109 #define MCK_INIT 0x63
110 #define MCK_INIT1 0x20          /*fixme: Bayer - 0x50 for JPEG ??*/
111
112 #define SYS_CLK 0x04
113
114 #define SENS(bridge, sensor, _flags, _sensor_addr) \
115 { \
116         .bridge_init = bridge, \
117         .sensor_init = sensor, \
118         .sensor_init_size = sizeof(sensor), \
119         .flags = _flags, .sensor_addr = _sensor_addr \
120 }
121
122 /* We calculate the autogain at the end of the transfer of a frame, at this
123    moment a frame with the old settings is being captured and transmitted. So
124    if we adjust the gain or exposure we must ignore atleast the next frame for
125    the new settings to come into effect before doing any other adjustments. */
126 #define AUTOGAIN_IGNORE_FRAMES 1
127
128 static const struct v4l2_pix_format vga_mode[] = {
129         {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
130                 .bytesperline = 160,
131                 .sizeimage = 160 * 120,
132                 .colorspace = V4L2_COLORSPACE_SRGB,
133                 .priv = 2 | MODE_RAW},
134         {160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
135                 .bytesperline = 160,
136                 .sizeimage = 160 * 120 * 5 / 4,
137                 .colorspace = V4L2_COLORSPACE_SRGB,
138                 .priv = 2},
139         {320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
140                 .bytesperline = 320,
141                 .sizeimage = 320 * 240 * 5 / 4,
142                 .colorspace = V4L2_COLORSPACE_SRGB,
143                 .priv = 1},
144         {640, 480, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
145                 .bytesperline = 640,
146                 .sizeimage = 640 * 480 * 5 / 4,
147                 .colorspace = V4L2_COLORSPACE_SRGB,
148                 .priv = 0},
149 };
150 static const struct v4l2_pix_format sif_mode[] = {
151         {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
152                 .bytesperline = 160,
153                 .sizeimage = 160 * 120,
154                 .colorspace = V4L2_COLORSPACE_SRGB,
155                 .priv = 1 | MODE_RAW | MODE_REDUCED_SIF},
156         {160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
157                 .bytesperline = 160,
158                 .sizeimage = 160 * 120 * 5 / 4,
159                 .colorspace = V4L2_COLORSPACE_SRGB,
160                 .priv = 1 | MODE_REDUCED_SIF},
161         {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
162                 .bytesperline = 176,
163                 .sizeimage = 176 * 144,
164                 .colorspace = V4L2_COLORSPACE_SRGB,
165                 .priv = 1 | MODE_RAW},
166         {176, 144, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
167                 .bytesperline = 176,
168                 .sizeimage = 176 * 144 * 5 / 4,
169                 .colorspace = V4L2_COLORSPACE_SRGB,
170                 .priv = 1},
171         {320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
172                 .bytesperline = 320,
173                 .sizeimage = 320 * 240 * 5 / 4,
174                 .colorspace = V4L2_COLORSPACE_SRGB,
175                 .priv = 0 | MODE_REDUCED_SIF},
176         {352, 288, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
177                 .bytesperline = 352,
178                 .sizeimage = 352 * 288 * 5 / 4,
179                 .colorspace = V4L2_COLORSPACE_SRGB,
180                 .priv = 0},
181 };
182
183 static const __u8 initHv7131d[] = {
184         0x04, 0x03, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,
185         0x00, 0x00,
186         0x00, 0x00, 0x00, 0x02, 0x02, 0x00,
187         0x28, 0x1e, 0x60, 0x8e, 0x42,
188 };
189 static const __u8 hv7131d_sensor_init[][8] = {
190         {0xa0, 0x11, 0x01, 0x04, 0x00, 0x00, 0x00, 0x17},
191         {0xa0, 0x11, 0x02, 0x00, 0x00, 0x00, 0x00, 0x17},
192         {0xa0, 0x11, 0x28, 0x00, 0x00, 0x00, 0x00, 0x17},
193         {0xa0, 0x11, 0x30, 0x30, 0x00, 0x00, 0x00, 0x17}, /* reset level */
194         {0xa0, 0x11, 0x34, 0x02, 0x00, 0x00, 0x00, 0x17}, /* pixel bias volt */
195 };
196
197 static const __u8 initHv7131r[] = {
198         0x46, 0x77, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,
199         0x00, 0x00,
200         0x00, 0x00, 0x00, 0x02, 0x01, 0x00,
201         0x28, 0x1e, 0x60, 0x8a, 0x20,
202 };
203 static const __u8 hv7131r_sensor_init[][8] = {
204         {0xc0, 0x11, 0x31, 0x38, 0x2a, 0x2e, 0x00, 0x10},
205         {0xa0, 0x11, 0x01, 0x08, 0x2a, 0x2e, 0x00, 0x10},
206         {0xb0, 0x11, 0x20, 0x00, 0xd0, 0x2e, 0x00, 0x10},
207         {0xc0, 0x11, 0x25, 0x03, 0x0e, 0x28, 0x00, 0x16},
208         {0xa0, 0x11, 0x30, 0x10, 0x0e, 0x28, 0x00, 0x15},
209 };
210 static const __u8 initOv6650[] = {
211         0x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
212         0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
213         0x00, 0x01, 0x01, 0x0a, 0x16, 0x12, 0x68, 0x8b,
214         0x10,
215 };
216 static const __u8 ov6650_sensor_init[][8] = {
217         /* Bright, contrast, etc are set through SCBB interface.
218          * AVCAP on win2 do not send any data on this controls. */
219         /* Anyway, some registers appears to alter bright and constrat */
220
221         /* Reset sensor */
222         {0xa0, 0x60, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
223         /* Set clock register 0x11 low nibble is clock divider */
224         {0xd0, 0x60, 0x11, 0xc0, 0x1b, 0x18, 0xc1, 0x10},
225         /* Next some unknown stuff */
226         {0xb0, 0x60, 0x15, 0x00, 0x02, 0x18, 0xc1, 0x10},
227 /*      {0xa0, 0x60, 0x1b, 0x01, 0x02, 0x18, 0xc1, 0x10},
228                  * THIS SET GREEN SCREEN
229                  * (pixels could be innverted in decode kind of "brg",
230                  * but blue wont be there. Avoid this data ... */
231         {0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10}, /* format out? */
232         {0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10},
233         {0xa0, 0x60, 0x30, 0x3d, 0x0a, 0xd8, 0xa4, 0x10},
234         /* Enable rgb brightness control */
235         {0xa0, 0x60, 0x61, 0x08, 0x00, 0x00, 0x00, 0x10},
236         /* HDG: Note windows uses the line below, which sets both register 0x60
237            and 0x61 I believe these registers of the ov6650 are identical as
238            those of the ov7630, because if this is true the windows settings
239            add a bit additional red gain and a lot additional blue gain, which
240            matches my findings that the windows settings make blue much too
241            blue and red a little too red.
242         {0xb0, 0x60, 0x60, 0x66, 0x68, 0xd8, 0xa4, 0x10}, */
243         /* Some more unknown stuff */
244         {0xa0, 0x60, 0x68, 0x04, 0x68, 0xd8, 0xa4, 0x10},
245         {0xd0, 0x60, 0x17, 0x24, 0xd6, 0x04, 0x94, 0x10}, /* Clipreg */
246 };
247
248 static const __u8 initOv7630[] = {
249         0x04, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, /* r01 .. r08 */
250         0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* r09 .. r10 */
251         0x00, 0x01, 0x01, 0x0a,                         /* r11 .. r14 */
252         0x28, 0x1e,                     /* H & V sizes     r15 .. r16 */
253         0x68, 0x8f, MCK_INIT1,                          /* r17 .. r19 */
254 };
255 static const __u8 ov7630_sensor_init[][8] = {
256         {0xa0, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
257         {0xb0, 0x21, 0x01, 0x77, 0x3a, 0x00, 0x00, 0x10},
258 /*      {0xd0, 0x21, 0x12, 0x7c, 0x01, 0x80, 0x34, 0x10},          jfm */
259         {0xd0, 0x21, 0x12, 0x5c, 0x00, 0x80, 0x34, 0x10},       /* jfm */
260         {0xa0, 0x21, 0x1b, 0x04, 0x00, 0x80, 0x34, 0x10},
261         {0xa0, 0x21, 0x20, 0x44, 0x00, 0x80, 0x34, 0x10},
262         {0xa0, 0x21, 0x23, 0xee, 0x00, 0x80, 0x34, 0x10},
263         {0xd0, 0x21, 0x26, 0xa0, 0x9a, 0xa0, 0x30, 0x10},
264         {0xb0, 0x21, 0x2a, 0x80, 0x00, 0xa0, 0x30, 0x10},
265         {0xb0, 0x21, 0x2f, 0x3d, 0x24, 0xa0, 0x30, 0x10},
266         {0xa0, 0x21, 0x32, 0x86, 0x24, 0xa0, 0x30, 0x10},
267         {0xb0, 0x21, 0x60, 0xa9, 0x4a, 0xa0, 0x30, 0x10},
268 /*      {0xb0, 0x21, 0x60, 0xa9, 0x42, 0xa0, 0x30, 0x10},        * jfm */
269         {0xa0, 0x21, 0x65, 0x00, 0x42, 0xa0, 0x30, 0x10},
270         {0xa0, 0x21, 0x69, 0x38, 0x42, 0xa0, 0x30, 0x10},
271         {0xc0, 0x21, 0x6f, 0x88, 0x0b, 0x00, 0x30, 0x10},
272         {0xc0, 0x21, 0x74, 0x21, 0x8e, 0x00, 0x30, 0x10},
273         {0xa0, 0x21, 0x7d, 0xf7, 0x8e, 0x00, 0x30, 0x10},
274         {0xd0, 0x21, 0x17, 0x1c, 0xbd, 0x06, 0xf6, 0x10},
275 };
276
277 static const __u8 initPas106[] = {
278         0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x40, 0x00, 0x00, 0x00,
279         0x00, 0x00,
280         0x00, 0x00, 0x00, 0x04, 0x01, 0x00,
281         0x16, 0x12, 0x24, COMP1, MCK_INIT1,
282 };
283 /* compression 0x86 mckinit1 0x2b */
284
285 /* "Known" PAS106B registers:
286   0x02 clock divider
287   0x03 Variable framerate bits 4-11
288   0x04 Var framerate bits 0-3, one must leave the 4 msb's at 0 !!
289        The variable framerate control must never be set lower then 300,
290        which sets the framerate at 90 / reg02, otherwise vsync is lost.
291   0x05 Shutter Time Line Offset, this can be used as an exposure control:
292        0 = use full frame time, 255 = no exposure at all
293        Note this may never be larger then "var-framerate control" / 2 - 2.
294        When var-framerate control is < 514, no exposure is reached at the max
295        allowed value for the framerate control value, rather then at 255.
296   0x06 Shutter Time Pixel Offset, like reg05 this influences exposure, but
297        only a very little bit, leave at 0xcd
298   0x07 offset sign bit (bit0 1 > negative offset)
299   0x08 offset
300   0x09 Blue Gain
301   0x0a Green1 Gain
302   0x0b Green2 Gain
303   0x0c Red Gain
304   0x0e Global gain
305   0x13 Write 1 to commit settings to sensor
306 */
307
308 static const __u8 pas106_sensor_init[][8] = {
309         /* Pixel Clock Divider 6 */
310         { 0xa1, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x14 },
311         /* Frame Time MSB (also seen as 0x12) */
312         { 0xa1, 0x40, 0x03, 0x13, 0x00, 0x00, 0x00, 0x14 },
313         /* Frame Time LSB (also seen as 0x05) */
314         { 0xa1, 0x40, 0x04, 0x06, 0x00, 0x00, 0x00, 0x14 },
315         /* Shutter Time Line Offset (also seen as 0x6d) */
316         { 0xa1, 0x40, 0x05, 0x65, 0x00, 0x00, 0x00, 0x14 },
317         /* Shutter Time Pixel Offset (also seen as 0xb1) */
318         { 0xa1, 0x40, 0x06, 0xcd, 0x00, 0x00, 0x00, 0x14 },
319         /* Black Level Subtract Sign (also seen 0x00) */
320         { 0xa1, 0x40, 0x07, 0xc1, 0x00, 0x00, 0x00, 0x14 },
321         /* Black Level Subtract Level (also seen 0x01) */
322         { 0xa1, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x14 },
323         { 0xa1, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x14 },
324         /* Color Gain B Pixel 5 a */
325         { 0xa1, 0x40, 0x09, 0x05, 0x00, 0x00, 0x00, 0x14 },
326         /* Color Gain G1 Pixel 1 5 */
327         { 0xa1, 0x40, 0x0a, 0x04, 0x00, 0x00, 0x00, 0x14 },
328         /* Color Gain G2 Pixel 1 0 5 */
329         { 0xa1, 0x40, 0x0b, 0x04, 0x00, 0x00, 0x00, 0x14 },
330         /* Color Gain R Pixel 3 1 */
331         { 0xa1, 0x40, 0x0c, 0x05, 0x00, 0x00, 0x00, 0x14 },
332         /* Color GainH  Pixel */
333         { 0xa1, 0x40, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x14 },
334         /* Global Gain */
335         { 0xa1, 0x40, 0x0e, 0x0e, 0x00, 0x00, 0x00, 0x14 },
336         /* Contrast */
337         { 0xa1, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x14 },
338         /* H&V synchro polarity */
339         { 0xa1, 0x40, 0x10, 0x06, 0x00, 0x00, 0x00, 0x14 },
340         /* ?default */
341         { 0xa1, 0x40, 0x11, 0x06, 0x00, 0x00, 0x00, 0x14 },
342         /* DAC scale */
343         { 0xa1, 0x40, 0x12, 0x06, 0x00, 0x00, 0x00, 0x14 },
344         /* ?default */
345         { 0xa1, 0x40, 0x14, 0x02, 0x00, 0x00, 0x00, 0x14 },
346         /* Validate Settings */
347         { 0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14 },
348 };
349
350 static const __u8 initPas202[] = {
351         0x44, 0x44, 0x21, 0x30, 0x00, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0x00,
352         0x00, 0x00,
353         0x00, 0x00, 0x00, 0x06, 0x03, 0x0a,
354         0x28, 0x1e, 0x20, 0x89, 0x20,
355 };
356
357 /* "Known" PAS202BCB registers:
358   0x02 clock divider
359   0x04 Variable framerate bits 6-11 (*)
360   0x05 Var framerate  bits 0-5, one must leave the 2 msb's at 0 !!
361   0x07 Blue Gain
362   0x08 Green Gain
363   0x09 Red Gain
364   0x0b offset sign bit (bit0 1 > negative offset)
365   0x0c offset
366   0x0e Unknown image is slightly brighter when bit 0 is 0, if reg0f is 0 too,
367        leave at 1 otherwise we get a jump in our exposure control
368   0x0f Exposure 0-255, 0 = use full frame time, 255 = no exposure at all
369   0x10 Master gain 0 - 31
370   0x11 write 1 to apply changes
371   (*) The variable framerate control must never be set lower then 500
372       which sets the framerate at 30 / reg02, otherwise vsync is lost.
373 */
374 static const __u8 pas202_sensor_init[][8] = {
375         /* Set the clock divider to 4 -> 30 / 4 = 7.5 fps, we would like
376            to set it lower, but for some reason the bridge starts missing
377            vsync's then */
378         {0xa0, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x10},
379         {0xd0, 0x40, 0x04, 0x07, 0x34, 0x00, 0x09, 0x10},
380         {0xd0, 0x40, 0x08, 0x01, 0x00, 0x00, 0x01, 0x10},
381         {0xd0, 0x40, 0x0c, 0x00, 0x0c, 0x01, 0x32, 0x10},
382         {0xd0, 0x40, 0x10, 0x00, 0x01, 0x00, 0x63, 0x10},
383         {0xa0, 0x40, 0x15, 0x70, 0x01, 0x00, 0x63, 0x10},
384         {0xa0, 0x40, 0x18, 0x00, 0x01, 0x00, 0x63, 0x10},
385         {0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
386         {0xa0, 0x40, 0x03, 0x56, 0x01, 0x00, 0x63, 0x10},
387         {0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
388 };
389
390 static const __u8 initTas5110c[] = {
391         0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
392         0x00, 0x00,
393         0x00, 0x00, 0x00, 0x45, 0x09, 0x0a,
394         0x16, 0x12, 0x60, 0x86, 0x2b,
395 };
396 /* Same as above, except a different hstart */
397 static const __u8 initTas5110d[] = {
398         0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
399         0x00, 0x00,
400         0x00, 0x00, 0x00, 0x41, 0x09, 0x0a,
401         0x16, 0x12, 0x60, 0x86, 0x2b,
402 };
403 /* tas5110c is 3 wire, tas5110d is 2 wire (regular i2c) */
404 static const __u8 tas5110c_sensor_init[][8] = {
405         {0x30, 0x11, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x10},
406         {0x30, 0x11, 0x02, 0x20, 0xa9, 0x00, 0x00, 0x10},
407 };
408 /* Known TAS5110D registers
409  * reg02: gain, bit order reversed!! 0 == max gain, 255 == min gain
410  * reg03: bit3: vflip, bit4: ~hflip, bit7: ~gainboost (~ == inverted)
411  *        Note: writing reg03 seems to only work when written together with 02
412  */
413 static const __u8 tas5110d_sensor_init[][8] = {
414         {0xa0, 0x61, 0x9a, 0xca, 0x00, 0x00, 0x00, 0x17}, /* reset */
415 };
416
417 static const __u8 initTas5130[] = {
418         0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
419         0x00, 0x00,
420         0x00, 0x00, 0x00, 0x68, 0x0c, 0x0a,
421         0x28, 0x1e, 0x60, COMP, MCK_INIT,
422 };
423 static const __u8 tas5130_sensor_init[][8] = {
424 /*      {0x30, 0x11, 0x00, 0x40, 0x47, 0x00, 0x00, 0x10},
425                                         * shutter 0x47 short exposure? */
426         {0x30, 0x11, 0x00, 0x40, 0x01, 0x00, 0x00, 0x10},
427                                         /* shutter 0x01 long exposure */
428         {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10},
429 };
430
431 static const struct sensor_data sensor_data[] = {
432         SENS(initHv7131d, hv7131d_sensor_init, 0, 0),
433         SENS(initHv7131r, hv7131r_sensor_init, 0, 0),
434         SENS(initOv6650, ov6650_sensor_init, F_SIF, 0x60),
435         SENS(initOv7630, ov7630_sensor_init, 0, 0x21),
436         SENS(initPas106, pas106_sensor_init, F_SIF, 0),
437         SENS(initPas202, pas202_sensor_init, 0, 0),
438         SENS(initTas5110c, tas5110c_sensor_init, F_SIF, 0),
439         SENS(initTas5110d, tas5110d_sensor_init, F_SIF, 0),
440         SENS(initTas5130, tas5130_sensor_init, 0, 0),
441 };
442
443 /* get one byte in gspca_dev->usb_buf */
444 static void reg_r(struct gspca_dev *gspca_dev,
445                   __u16 value)
446 {
447         int res;
448
449         if (gspca_dev->usb_err < 0)
450                 return;
451
452         res = usb_control_msg(gspca_dev->dev,
453                         usb_rcvctrlpipe(gspca_dev->dev, 0),
454                         0,                      /* request */
455                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
456                         value,
457                         0,                      /* index */
458                         gspca_dev->usb_buf, 1,
459                         500);
460
461         if (res < 0) {
462                 dev_err(gspca_dev->v4l2_dev.dev,
463                         "Error reading register %02x: %d\n", value, res);
464                 gspca_dev->usb_err = res;
465                 /*
466                  * Make sure the result is zeroed to avoid uninitialized
467                  * values.
468                  */
469                 gspca_dev->usb_buf[0] = 0;
470         }
471 }
472
473 static void reg_w(struct gspca_dev *gspca_dev,
474                   __u16 value,
475                   const __u8 *buffer,
476                   int len)
477 {
478         int res;
479
480         if (gspca_dev->usb_err < 0)
481                 return;
482
483         memcpy(gspca_dev->usb_buf, buffer, len);
484         res = usb_control_msg(gspca_dev->dev,
485                         usb_sndctrlpipe(gspca_dev->dev, 0),
486                         0x08,                   /* request */
487                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
488                         value,
489                         0,                      /* index */
490                         gspca_dev->usb_buf, len,
491                         500);
492
493         if (res < 0) {
494                 dev_err(gspca_dev->v4l2_dev.dev,
495                         "Error writing register %02x: %d\n", value, res);
496                 gspca_dev->usb_err = res;
497         }
498 }
499
500 static void i2c_w(struct gspca_dev *gspca_dev, const u8 *buf)
501 {
502         int retry = 60;
503
504         if (gspca_dev->usb_err < 0)
505                 return;
506
507         /* is i2c ready */
508         reg_w(gspca_dev, 0x08, buf, 8);
509         while (retry--) {
510                 if (gspca_dev->usb_err < 0)
511                         return;
512                 msleep(1);
513                 reg_r(gspca_dev, 0x08);
514                 if (gspca_dev->usb_buf[0] & 0x04) {
515                         if (gspca_dev->usb_buf[0] & 0x08) {
516                                 dev_err(gspca_dev->v4l2_dev.dev,
517                                         "i2c error writing %8ph\n", buf);
518                                 gspca_dev->usb_err = -EIO;
519                         }
520                         return;
521                 }
522         }
523
524         dev_err(gspca_dev->v4l2_dev.dev, "i2c write timeout\n");
525         gspca_dev->usb_err = -EIO;
526 }
527
528 static void i2c_w_vector(struct gspca_dev *gspca_dev,
529                         const __u8 buffer[][8], int len)
530 {
531         for (;;) {
532                 if (gspca_dev->usb_err < 0)
533                         return;
534                 i2c_w(gspca_dev, *buffer);
535                 len -= 8;
536                 if (len <= 0)
537                         break;
538                 buffer++;
539         }
540 }
541
542 static void setbrightness(struct gspca_dev *gspca_dev)
543 {
544         struct sd *sd = (struct sd *) gspca_dev;
545
546         switch (sd->sensor) {
547         case  SENSOR_OV6650:
548         case  SENSOR_OV7630: {
549                 __u8 i2cOV[] =
550                         {0xa0, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10};
551
552                 /* change reg 0x06 */
553                 i2cOV[1] = sensor_data[sd->sensor].sensor_addr;
554                 i2cOV[3] = sd->brightness->val;
555                 i2c_w(gspca_dev, i2cOV);
556                 break;
557         }
558         case SENSOR_PAS106:
559         case SENSOR_PAS202: {
560                 __u8 i2cpbright[] =
561                         {0xb0, 0x40, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x16};
562                 __u8 i2cpdoit[] =
563                         {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
564
565                 /* PAS106 uses reg 7 and 8 instead of b and c */
566                 if (sd->sensor == SENSOR_PAS106) {
567                         i2cpbright[2] = 7;
568                         i2cpdoit[2] = 0x13;
569                 }
570
571                 if (sd->brightness->val < 127) {
572                         /* change reg 0x0b, signreg */
573                         i2cpbright[3] = 0x01;
574                         /* set reg 0x0c, offset */
575                         i2cpbright[4] = 127 - sd->brightness->val;
576                 } else
577                         i2cpbright[4] = sd->brightness->val - 127;
578
579                 i2c_w(gspca_dev, i2cpbright);
580                 i2c_w(gspca_dev, i2cpdoit);
581                 break;
582         }
583         default:
584                 break;
585         }
586 }
587
588 static void setgain(struct gspca_dev *gspca_dev)
589 {
590         struct sd *sd = (struct sd *) gspca_dev;
591         u8 gain = gspca_dev->gain->val;
592
593         switch (sd->sensor) {
594         case SENSOR_HV7131D: {
595                 __u8 i2c[] =
596                         {0xc0, 0x11, 0x31, 0x00, 0x00, 0x00, 0x00, 0x17};
597
598                 i2c[3] = 0x3f - gain;
599                 i2c[4] = 0x3f - gain;
600                 i2c[5] = 0x3f - gain;
601
602                 i2c_w(gspca_dev, i2c);
603                 break;
604         }
605         case SENSOR_TAS5110C:
606         case SENSOR_TAS5130CXX: {
607                 __u8 i2c[] =
608                         {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};
609
610                 i2c[4] = 255 - gain;
611                 i2c_w(gspca_dev, i2c);
612                 break;
613         }
614         case SENSOR_TAS5110D: {
615                 __u8 i2c[] = {
616                         0xb0, 0x61, 0x02, 0x00, 0x10, 0x00, 0x00, 0x17 };
617                 gain = 255 - gain;
618                 /* The bits in the register are the wrong way around!! */
619                 i2c[3] |= (gain & 0x80) >> 7;
620                 i2c[3] |= (gain & 0x40) >> 5;
621                 i2c[3] |= (gain & 0x20) >> 3;
622                 i2c[3] |= (gain & 0x10) >> 1;
623                 i2c[3] |= (gain & 0x08) << 1;
624                 i2c[3] |= (gain & 0x04) << 3;
625                 i2c[3] |= (gain & 0x02) << 5;
626                 i2c[3] |= (gain & 0x01) << 7;
627                 i2c_w(gspca_dev, i2c);
628                 break;
629         }
630         case SENSOR_OV6650:
631         case SENSOR_OV7630: {
632                 __u8 i2c[] = {0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
633
634                 /*
635                  * The ov7630's gain is weird, at 32 the gain drops to the
636                  * same level as at 16, so skip 32-47 (of the 0-63 scale).
637                  */
638                 if (sd->sensor == SENSOR_OV7630 && gain >= 32)
639                         gain += 16;
640
641                 i2c[1] = sensor_data[sd->sensor].sensor_addr;
642                 i2c[3] = gain;
643                 i2c_w(gspca_dev, i2c);
644                 break;
645         }
646         case SENSOR_PAS106:
647         case SENSOR_PAS202: {
648                 __u8 i2cpgain[] =
649                         {0xa0, 0x40, 0x10, 0x00, 0x00, 0x00, 0x00, 0x15};
650                 __u8 i2cpcolorgain[] =
651                         {0xc0, 0x40, 0x07, 0x00, 0x00, 0x00, 0x00, 0x15};
652                 __u8 i2cpdoit[] =
653                         {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
654
655                 /* PAS106 uses different regs (and has split green gains) */
656                 if (sd->sensor == SENSOR_PAS106) {
657                         i2cpgain[2] = 0x0e;
658                         i2cpcolorgain[0] = 0xd0;
659                         i2cpcolorgain[2] = 0x09;
660                         i2cpdoit[2] = 0x13;
661                 }
662
663                 i2cpgain[3] = gain;
664                 i2cpcolorgain[3] = gain >> 1;
665                 i2cpcolorgain[4] = gain >> 1;
666                 i2cpcolorgain[5] = gain >> 1;
667                 i2cpcolorgain[6] = gain >> 1;
668
669                 i2c_w(gspca_dev, i2cpgain);
670                 i2c_w(gspca_dev, i2cpcolorgain);
671                 i2c_w(gspca_dev, i2cpdoit);
672                 break;
673         }
674         default:
675                 if (sd->bridge == BRIDGE_103) {
676                         u8 buf[3] = { gain, gain, gain }; /* R, G, B */
677                         reg_w(gspca_dev, 0x05, buf, 3);
678                 } else {
679                         u8 buf[2];
680                         buf[0] = gain << 4 | gain; /* Red and blue */
681                         buf[1] = gain; /* Green */
682                         reg_w(gspca_dev, 0x10, buf, 2);
683                 }
684         }
685 }
686
687 static void setexposure(struct gspca_dev *gspca_dev)
688 {
689         struct sd *sd = (struct sd *) gspca_dev;
690
691         switch (sd->sensor) {
692         case SENSOR_HV7131D: {
693                 /* Note the datasheet wrongly says line mode exposure uses reg
694                    0x26 and 0x27, testing has shown 0x25 + 0x26 */
695                 __u8 i2c[] = {0xc0, 0x11, 0x25, 0x00, 0x00, 0x00, 0x00, 0x17};
696                 u16 reg = gspca_dev->exposure->val;
697
698                 i2c[3] = reg >> 8;
699                 i2c[4] = reg & 0xff;
700                 i2c_w(gspca_dev, i2c);
701                 break;
702         }
703         case SENSOR_TAS5110C:
704         case SENSOR_TAS5110D: {
705                 /* register 19's high nibble contains the sn9c10x clock divider
706                    The high nibble configures the no fps according to the
707                    formula: 60 / high_nibble. With a maximum of 30 fps */
708                 u8 reg = gspca_dev->exposure->val;
709
710                 reg = (reg << 4) | 0x0b;
711                 reg_w(gspca_dev, 0x19, &reg, 1);
712                 break;
713         }
714         case SENSOR_OV6650:
715         case SENSOR_OV7630: {
716                 /* The ov6650 / ov7630 have 2 registers which both influence
717                    exposure, register 11, whose low nibble sets the nr off fps
718                    according to: fps = 30 / (low_nibble + 1)
719
720                    The fps configures the maximum exposure setting, but it is
721                    possible to use less exposure then what the fps maximum
722                    allows by setting register 10. register 10 configures the
723                    actual exposure as quotient of the full exposure, with 0
724                    being no exposure at all (not very useful) and reg10_max
725                    being max exposure possible at that framerate.
726
727                    The code maps our 0 - 510 ms exposure ctrl to these 2
728                    registers, trying to keep fps as high as possible.
729                 */
730                 __u8 i2c[] = {0xb0, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10};
731                 int reg10, reg11, reg10_max;
732
733                 /* ov6645 datasheet says reg10_max is 9a, but that uses
734                    tline * 2 * reg10 as formula for calculating texpo, the
735                    ov6650 probably uses the same formula as the 7730 which uses
736                    tline * 4 * reg10, which explains why the reg10max we've
737                    found experimentally for the ov6650 is exactly half that of
738                    the ov6645. The ov7630 datasheet says the max is 0x41. */
739                 if (sd->sensor == SENSOR_OV6650) {
740                         reg10_max = 0x4d;
741                         i2c[4] = 0xc0; /* OV6650 needs non default vsync pol */
742                 } else
743                         reg10_max = 0x41;
744
745                 reg11 = (15 * gspca_dev->exposure->val + 999) / 1000;
746                 if (reg11 < 1)
747                         reg11 = 1;
748                 else if (reg11 > 16)
749                         reg11 = 16;
750
751                 /* In 640x480, if the reg11 has less than 4, the image is
752                    unstable (the bridge goes into a higher compression mode
753                    which we have not reverse engineered yet). */
754                 if (gspca_dev->pixfmt.width == 640 && reg11 < 4)
755                         reg11 = 4;
756
757                 /* frame exposure time in ms = 1000 * reg11 / 30    ->
758                 reg10 = (gspca_dev->exposure->val / 2) * reg10_max
759                                 / (1000 * reg11 / 30) */
760                 reg10 = (gspca_dev->exposure->val * 15 * reg10_max)
761                                 / (1000 * reg11);
762
763                 /* Don't allow this to get below 10 when using autogain, the
764                    steps become very large (relatively) when below 10 causing
765                    the image to oscilate from much too dark, to much too bright
766                    and back again. */
767                 if (gspca_dev->autogain->val && reg10 < 10)
768                         reg10 = 10;
769                 else if (reg10 > reg10_max)
770                         reg10 = reg10_max;
771
772                 /* Write reg 10 and reg11 low nibble */
773                 i2c[1] = sensor_data[sd->sensor].sensor_addr;
774                 i2c[3] = reg10;
775                 i2c[4] |= reg11 - 1;
776
777                 /* If register 11 didn't change, don't change it */
778                 if (sd->reg11 == reg11)
779                         i2c[0] = 0xa0;
780
781                 i2c_w(gspca_dev, i2c);
782                 if (gspca_dev->usb_err == 0)
783                         sd->reg11 = reg11;
784                 break;
785         }
786         case SENSOR_PAS202: {
787                 __u8 i2cpframerate[] =
788                         {0xb0, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x16};
789                 __u8 i2cpexpo[] =
790                         {0xa0, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x16};
791                 const __u8 i2cpdoit[] =
792                         {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
793                 int framerate_ctrl;
794
795                 /* The exposure knee for the autogain algorithm is 200
796                    (100 ms / 10 fps on other sensors), for values below this
797                    use the control for setting the partial frame expose time,
798                    above that use variable framerate. This way we run at max
799                    framerate (640x480@7.5 fps, 320x240@10fps) until the knee
800                    is reached. Using the variable framerate control above 200
801                    is better then playing around with both clockdiv + partial
802                    frame exposure times (like we are doing with the ov chips),
803                    as that sometimes leads to jumps in the exposure control,
804                    which are bad for auto exposure. */
805                 if (gspca_dev->exposure->val < 200) {
806                         i2cpexpo[3] = 255 - (gspca_dev->exposure->val * 255)
807                                                 / 200;
808                         framerate_ctrl = 500;
809                 } else {
810                         /* The PAS202's exposure control goes from 0 - 4095,
811                            but anything below 500 causes vsync issues, so scale
812                            our 200-1023 to 500-4095 */
813                         framerate_ctrl = (gspca_dev->exposure->val - 200)
814                                                         * 1000 / 229 +  500;
815                 }
816
817                 i2cpframerate[3] = framerate_ctrl >> 6;
818                 i2cpframerate[4] = framerate_ctrl & 0x3f;
819                 i2c_w(gspca_dev, i2cpframerate);
820                 i2c_w(gspca_dev, i2cpexpo);
821                 i2c_w(gspca_dev, i2cpdoit);
822                 break;
823         }
824         case SENSOR_PAS106: {
825                 __u8 i2cpframerate[] =
826                         {0xb1, 0x40, 0x03, 0x00, 0x00, 0x00, 0x00, 0x14};
827                 __u8 i2cpexpo[] =
828                         {0xa1, 0x40, 0x05, 0x00, 0x00, 0x00, 0x00, 0x14};
829                 const __u8 i2cpdoit[] =
830                         {0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14};
831                 int framerate_ctrl;
832
833                 /* For values below 150 use partial frame exposure, above
834                    that use framerate ctrl */
835                 if (gspca_dev->exposure->val < 150) {
836                         i2cpexpo[3] = 150 - gspca_dev->exposure->val;
837                         framerate_ctrl = 300;
838                 } else {
839                         /* The PAS106's exposure control goes from 0 - 4095,
840                            but anything below 300 causes vsync issues, so scale
841                            our 150-1023 to 300-4095 */
842                         framerate_ctrl = (gspca_dev->exposure->val - 150)
843                                                 * 1000 / 230 + 300;
844                 }
845
846                 i2cpframerate[3] = framerate_ctrl >> 4;
847                 i2cpframerate[4] = framerate_ctrl & 0x0f;
848                 i2c_w(gspca_dev, i2cpframerate);
849                 i2c_w(gspca_dev, i2cpexpo);
850                 i2c_w(gspca_dev, i2cpdoit);
851                 break;
852         }
853         default:
854                 break;
855         }
856 }
857
858 static void setfreq(struct gspca_dev *gspca_dev)
859 {
860         struct sd *sd = (struct sd *) gspca_dev;
861
862         if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630) {
863                 /* Framerate adjust register for artificial light 50 hz flicker
864                    compensation, for the ov6650 this is identical to ov6630
865                    0x2b register, see ov6630 datasheet.
866                    0x4f / 0x8a -> (30 fps -> 25 fps), 0x00 -> no adjustment */
867                 __u8 i2c[] = {0xa0, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10};
868                 switch (sd->plfreq->val) {
869                 default:
870 /*              case 0:                  * no filter*/
871 /*              case 2:                  * 60 hz */
872                         i2c[3] = 0;
873                         break;
874                 case 1:                 /* 50 hz */
875                         i2c[3] = (sd->sensor == SENSOR_OV6650)
876                                         ? 0x4f : 0x8a;
877                         break;
878                 }
879                 i2c[1] = sensor_data[sd->sensor].sensor_addr;
880                 i2c_w(gspca_dev, i2c);
881         }
882 }
883
884 static void do_autogain(struct gspca_dev *gspca_dev)
885 {
886         struct sd *sd = (struct sd *) gspca_dev;
887         int deadzone, desired_avg_lum, avg_lum;
888
889         avg_lum = atomic_read(&sd->avg_lum);
890         if (avg_lum == -1)
891                 return;
892
893         if (sd->autogain_ignore_frames > 0) {
894                 sd->autogain_ignore_frames--;
895                 return;
896         }
897
898         /* SIF / VGA sensors have a different autoexposure area and thus
899            different avg_lum values for the same picture brightness */
900         if (sensor_data[sd->sensor].flags & F_SIF) {
901                 deadzone = 500;
902                 /* SIF sensors tend to overexpose, so keep this small */
903                 desired_avg_lum = 5000;
904         } else {
905                 deadzone = 1500;
906                 desired_avg_lum = 13000;
907         }
908
909         if (sd->brightness)
910                 desired_avg_lum = sd->brightness->val * desired_avg_lum / 127;
911
912         if (gspca_dev->exposure->maximum < 500) {
913                 if (gspca_coarse_grained_expo_autogain(gspca_dev, avg_lum,
914                                 desired_avg_lum, deadzone))
915                         sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
916         } else {
917                 int gain_knee = (s32)gspca_dev->gain->maximum * 9 / 10;
918                 if (gspca_expo_autogain(gspca_dev, avg_lum, desired_avg_lum,
919                                 deadzone, gain_knee, sd->exposure_knee))
920                         sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
921         }
922 }
923
924 /* this function is called at probe time */
925 static int sd_config(struct gspca_dev *gspca_dev,
926                         const struct usb_device_id *id)
927 {
928         struct sd *sd = (struct sd *) gspca_dev;
929         struct cam *cam;
930
931         reg_r(gspca_dev, 0x00);
932         if (gspca_dev->usb_buf[0] != 0x10)
933                 return -ENODEV;
934
935         /* copy the webcam info from the device id */
936         sd->sensor = id->driver_info >> 8;
937         sd->bridge = id->driver_info & 0xff;
938
939         cam = &gspca_dev->cam;
940         if (!(sensor_data[sd->sensor].flags & F_SIF)) {
941                 cam->cam_mode = vga_mode;
942                 cam->nmodes = ARRAY_SIZE(vga_mode);
943         } else {
944                 cam->cam_mode = sif_mode;
945                 cam->nmodes = ARRAY_SIZE(sif_mode);
946         }
947         cam->npkt = 36;                 /* 36 packets per ISOC message */
948
949         return 0;
950 }
951
952 /* this function is called at probe and resume time */
953 static int sd_init(struct gspca_dev *gspca_dev)
954 {
955         const __u8 stop = 0x09; /* Disable stream turn of LED */
956
957         reg_w(gspca_dev, 0x01, &stop, 1);
958
959         return gspca_dev->usb_err;
960 }
961
962 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
963 {
964         struct gspca_dev *gspca_dev =
965                 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
966         struct sd *sd = (struct sd *)gspca_dev;
967
968         gspca_dev->usb_err = 0;
969
970         if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
971                 /* when switching to autogain set defaults to make sure
972                    we are on a valid point of the autogain gain /
973                    exposure knee graph, and give this change time to
974                    take effect before doing autogain. */
975                 gspca_dev->gain->val = gspca_dev->gain->default_value;
976                 gspca_dev->exposure->val = gspca_dev->exposure->default_value;
977                 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
978         }
979
980         if (!gspca_dev->streaming)
981                 return 0;
982
983         switch (ctrl->id) {
984         case V4L2_CID_BRIGHTNESS:
985                 setbrightness(gspca_dev);
986                 break;
987         case V4L2_CID_AUTOGAIN:
988                 if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
989                         setexposure(gspca_dev);
990                 if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
991                         setgain(gspca_dev);
992                 break;
993         case V4L2_CID_POWER_LINE_FREQUENCY:
994                 setfreq(gspca_dev);
995                 break;
996         default:
997                 return -EINVAL;
998         }
999         return gspca_dev->usb_err;
1000 }
1001
1002 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1003         .s_ctrl = sd_s_ctrl,
1004 };
1005
1006 /* this function is called at probe time */
1007 static int sd_init_controls(struct gspca_dev *gspca_dev)
1008 {
1009         struct sd *sd = (struct sd *) gspca_dev;
1010         struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1011
1012         gspca_dev->vdev.ctrl_handler = hdl;
1013         v4l2_ctrl_handler_init(hdl, 5);
1014
1015         if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630 ||
1016             sd->sensor == SENSOR_PAS106 || sd->sensor == SENSOR_PAS202)
1017                 sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1018                                         V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
1019
1020         /* Gain range is sensor dependent */
1021         switch (sd->sensor) {
1022         case SENSOR_OV6650:
1023         case SENSOR_PAS106:
1024         case SENSOR_PAS202:
1025                 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1026                                         V4L2_CID_GAIN, 0, 31, 1, 15);
1027                 break;
1028         case SENSOR_OV7630:
1029                 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1030                                         V4L2_CID_GAIN, 0, 47, 1, 31);
1031                 break;
1032         case SENSOR_HV7131D:
1033                 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1034                                         V4L2_CID_GAIN, 0, 63, 1, 31);
1035                 break;
1036         case SENSOR_TAS5110C:
1037         case SENSOR_TAS5110D:
1038         case SENSOR_TAS5130CXX:
1039                 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1040                                         V4L2_CID_GAIN, 0, 255, 1, 127);
1041                 break;
1042         default:
1043                 if (sd->bridge == BRIDGE_103) {
1044                         gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1045                                                 V4L2_CID_GAIN, 0, 127, 1, 63);
1046                 } else {
1047                         gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1048                                                 V4L2_CID_GAIN, 0, 15, 1, 7);
1049                 }
1050         }
1051
1052         /* Exposure range is sensor dependent, and not all have exposure */
1053         switch (sd->sensor) {
1054         case SENSOR_HV7131D:
1055                 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1056                                         V4L2_CID_EXPOSURE, 0, 8191, 1, 482);
1057                 sd->exposure_knee = 964;
1058                 break;
1059         case SENSOR_OV6650:
1060         case SENSOR_OV7630:
1061         case SENSOR_PAS106:
1062         case SENSOR_PAS202:
1063                 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1064                                         V4L2_CID_EXPOSURE, 0, 1023, 1, 66);
1065                 sd->exposure_knee = 200;
1066                 break;
1067         case SENSOR_TAS5110C:
1068         case SENSOR_TAS5110D:
1069                 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1070                                         V4L2_CID_EXPOSURE, 2, 15, 1, 2);
1071                 break;
1072         }
1073
1074         if (gspca_dev->exposure) {
1075                 gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1076                                                 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1077         }
1078
1079         if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630)
1080                 sd->plfreq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
1081                         V4L2_CID_POWER_LINE_FREQUENCY,
1082                         V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0,
1083                         V4L2_CID_POWER_LINE_FREQUENCY_DISABLED);
1084
1085         if (hdl->error) {
1086                 pr_err("Could not initialize controls\n");
1087                 return hdl->error;
1088         }
1089
1090         if (gspca_dev->autogain)
1091                 v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
1092
1093         return 0;
1094 }
1095
1096 /* -- start the camera -- */
1097 static int sd_start(struct gspca_dev *gspca_dev)
1098 {
1099         struct sd *sd = (struct sd *) gspca_dev;
1100         struct cam *cam = &gspca_dev->cam;
1101         int i, mode;
1102         __u8 regs[0x31];
1103
1104         mode = cam->cam_mode[gspca_dev->curr_mode].priv & 0x07;
1105         /* Copy registers 0x01 - 0x19 from the template */
1106         memcpy(&regs[0x01], sensor_data[sd->sensor].bridge_init, 0x19);
1107         /* Set the mode */
1108         regs[0x18] |= mode << 4;
1109
1110         /* Set bridge gain to 1.0 */
1111         if (sd->bridge == BRIDGE_103) {
1112                 regs[0x05] = 0x20; /* Red */
1113                 regs[0x06] = 0x20; /* Green */
1114                 regs[0x07] = 0x20; /* Blue */
1115         } else {
1116                 regs[0x10] = 0x00; /* Red and blue */
1117                 regs[0x11] = 0x00; /* Green */
1118         }
1119
1120         /* Setup pixel numbers and auto exposure window */
1121         if (sensor_data[sd->sensor].flags & F_SIF) {
1122                 regs[0x1a] = 0x14; /* HO_SIZE 640, makes no sense */
1123                 regs[0x1b] = 0x0a; /* VO_SIZE 320, makes no sense */
1124                 regs[0x1c] = 0x02; /* AE H-start 64 */
1125                 regs[0x1d] = 0x02; /* AE V-start 64 */
1126                 regs[0x1e] = 0x09; /* AE H-end 288 */
1127                 regs[0x1f] = 0x07; /* AE V-end 224 */
1128         } else {
1129                 regs[0x1a] = 0x1d; /* HO_SIZE 960, makes no sense */
1130                 regs[0x1b] = 0x10; /* VO_SIZE 512, makes no sense */
1131                 regs[0x1c] = 0x05; /* AE H-start 160 */
1132                 regs[0x1d] = 0x03; /* AE V-start 96 */
1133                 regs[0x1e] = 0x0f; /* AE H-end 480 */
1134                 regs[0x1f] = 0x0c; /* AE V-end 384 */
1135         }
1136
1137         /* Setup the gamma table (only used with the sn9c103 bridge) */
1138         for (i = 0; i < 16; i++)
1139                 regs[0x20 + i] = i * 16;
1140         regs[0x20 + i] = 255;
1141
1142         /* Special cases where some regs depend on mode or bridge */
1143         switch (sd->sensor) {
1144         case SENSOR_TAS5130CXX:
1145                 /* FIXME / TESTME
1146                    probably not mode specific at all most likely the upper
1147                    nibble of 0x19 is exposure (clock divider) just as with
1148                    the tas5110, we need someone to test this. */
1149                 regs[0x19] = mode ? 0x23 : 0x43;
1150                 break;
1151         case SENSOR_OV7630:
1152                 /* FIXME / TESTME for some reason with the 101/102 bridge the
1153                    clock is set to 12 Mhz (reg1 == 0x04), rather then 24.
1154                    Also the hstart needs to go from 1 to 2 when using a 103,
1155                    which is likely related. This does not seem right. */
1156                 if (sd->bridge == BRIDGE_103) {
1157                         regs[0x01] = 0x44; /* Select 24 Mhz clock */
1158                         regs[0x12] = 0x02; /* Set hstart to 2 */
1159                 }
1160                 break;
1161         case SENSOR_PAS202:
1162                 /* For some unknown reason we need to increase hstart by 1 on
1163                    the sn9c103, otherwise we get wrong colors (bayer shift). */
1164                 if (sd->bridge == BRIDGE_103)
1165                         regs[0x12] += 1;
1166                 break;
1167         }
1168         /* Disable compression when the raw bayer format has been selected */
1169         if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW)
1170                 regs[0x18] &= ~0x80;
1171
1172         /* Vga mode emulation on SIF sensor? */
1173         if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_REDUCED_SIF) {
1174                 regs[0x12] += 16;       /* hstart adjust */
1175                 regs[0x13] += 24;       /* vstart adjust */
1176                 regs[0x15]  = 320 / 16; /* hsize */
1177                 regs[0x16]  = 240 / 16; /* vsize */
1178         }
1179
1180         /* reg 0x01 bit 2 video transfert on */
1181         reg_w(gspca_dev, 0x01, &regs[0x01], 1);
1182         /* reg 0x17 SensorClk enable inv Clk 0x60 */
1183         reg_w(gspca_dev, 0x17, &regs[0x17], 1);
1184         /* Set the registers from the template */
1185         reg_w(gspca_dev, 0x01, &regs[0x01],
1186               (sd->bridge == BRIDGE_103) ? 0x30 : 0x1f);
1187
1188         /* Init the sensor */
1189         i2c_w_vector(gspca_dev, sensor_data[sd->sensor].sensor_init,
1190                         sensor_data[sd->sensor].sensor_init_size);
1191
1192         /* Mode / bridge specific sensor setup */
1193         switch (sd->sensor) {
1194         case SENSOR_PAS202: {
1195                 const __u8 i2cpclockdiv[] =
1196                         {0xa0, 0x40, 0x02, 0x03, 0x00, 0x00, 0x00, 0x10};
1197                 /* clockdiv from 4 to 3 (7.5 -> 10 fps) when in low res mode */
1198                 if (mode)
1199                         i2c_w(gspca_dev, i2cpclockdiv);
1200                 break;
1201             }
1202         case SENSOR_OV7630:
1203                 /* FIXME / TESTME We should be able to handle this identical
1204                    for the 101/102 and the 103 case */
1205                 if (sd->bridge == BRIDGE_103) {
1206                         const __u8 i2c[] = { 0xa0, 0x21, 0x13,
1207                                              0x80, 0x00, 0x00, 0x00, 0x10 };
1208                         i2c_w(gspca_dev, i2c);
1209                 }
1210                 break;
1211         }
1212         /* H_size V_size 0x28, 0x1e -> 640x480. 0x16, 0x12 -> 352x288 */
1213         reg_w(gspca_dev, 0x15, &regs[0x15], 2);
1214         /* compression register */
1215         reg_w(gspca_dev, 0x18, &regs[0x18], 1);
1216         /* H_start */
1217         reg_w(gspca_dev, 0x12, &regs[0x12], 1);
1218         /* V_START */
1219         reg_w(gspca_dev, 0x13, &regs[0x13], 1);
1220         /* reset 0x17 SensorClk enable inv Clk 0x60 */
1221                                 /*fixme: ov7630 [17]=68 8f (+20 if 102)*/
1222         reg_w(gspca_dev, 0x17, &regs[0x17], 1);
1223         /*MCKSIZE ->3 */        /*fixme: not ov7630*/
1224         reg_w(gspca_dev, 0x19, &regs[0x19], 1);
1225         /* AE_STRX AE_STRY AE_ENDX AE_ENDY */
1226         reg_w(gspca_dev, 0x1c, &regs[0x1c], 4);
1227         /* Enable video transfert */
1228         reg_w(gspca_dev, 0x01, &regs[0x01], 1);
1229         /* Compression */
1230         reg_w(gspca_dev, 0x18, &regs[0x18], 2);
1231         msleep(20);
1232
1233         sd->reg11 = -1;
1234
1235         setgain(gspca_dev);
1236         setbrightness(gspca_dev);
1237         setexposure(gspca_dev);
1238         setfreq(gspca_dev);
1239
1240         sd->frames_to_drop = 0;
1241         sd->autogain_ignore_frames = 0;
1242         gspca_dev->exp_too_high_cnt = 0;
1243         gspca_dev->exp_too_low_cnt = 0;
1244         atomic_set(&sd->avg_lum, -1);
1245         return gspca_dev->usb_err;
1246 }
1247
1248 static void sd_stopN(struct gspca_dev *gspca_dev)
1249 {
1250         sd_init(gspca_dev);
1251 }
1252
1253 static u8* find_sof(struct gspca_dev *gspca_dev, u8 *data, int len)
1254 {
1255         struct sd *sd = (struct sd *) gspca_dev;
1256         int i, header_size = (sd->bridge == BRIDGE_103) ? 18 : 12;
1257
1258         /* frames start with:
1259          *      ff ff 00 c4 c4 96       synchro
1260          *      00              (unknown)
1261          *      xx              (frame sequence / size / compression)
1262          *      (xx)            (idem - extra byte for sn9c103)
1263          *      ll mm           brightness sum inside auto exposure
1264          *      ll mm           brightness sum outside auto exposure
1265          *      (xx xx xx xx xx)        audio values for snc103
1266          */
1267         for (i = 0; i < len; i++) {
1268                 switch (sd->header_read) {
1269                 case 0:
1270                         if (data[i] == 0xff)
1271                                 sd->header_read++;
1272                         break;
1273                 case 1:
1274                         if (data[i] == 0xff)
1275                                 sd->header_read++;
1276                         else
1277                                 sd->header_read = 0;
1278                         break;
1279                 case 2:
1280                         if (data[i] == 0x00)
1281                                 sd->header_read++;
1282                         else if (data[i] != 0xff)
1283                                 sd->header_read = 0;
1284                         break;
1285                 case 3:
1286                         if (data[i] == 0xc4)
1287                                 sd->header_read++;
1288                         else if (data[i] == 0xff)
1289                                 sd->header_read = 1;
1290                         else
1291                                 sd->header_read = 0;
1292                         break;
1293                 case 4:
1294                         if (data[i] == 0xc4)
1295                                 sd->header_read++;
1296                         else if (data[i] == 0xff)
1297                                 sd->header_read = 1;
1298                         else
1299                                 sd->header_read = 0;
1300                         break;
1301                 case 5:
1302                         if (data[i] == 0x96)
1303                                 sd->header_read++;
1304                         else if (data[i] == 0xff)
1305                                 sd->header_read = 1;
1306                         else
1307                                 sd->header_read = 0;
1308                         break;
1309                 default:
1310                         sd->header[sd->header_read - 6] = data[i];
1311                         sd->header_read++;
1312                         if (sd->header_read == header_size) {
1313                                 sd->header_read = 0;
1314                                 return data + i + 1;
1315                         }
1316                 }
1317         }
1318         return NULL;
1319 }
1320
1321 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1322                         u8 *data,                       /* isoc packet */
1323                         int len)                        /* iso packet length */
1324 {
1325         int fr_h_sz = 0, lum_offset = 0, len_after_sof = 0;
1326         struct sd *sd = (struct sd *) gspca_dev;
1327         struct cam *cam = &gspca_dev->cam;
1328         u8 *sof;
1329
1330         sof = find_sof(gspca_dev, data, len);
1331         if (sof) {
1332                 if (sd->bridge == BRIDGE_103) {
1333                         fr_h_sz = 18;
1334                         lum_offset = 3;
1335                 } else {
1336                         fr_h_sz = 12;
1337                         lum_offset = 2;
1338                 }
1339
1340                 len_after_sof = len - (sof - data);
1341                 len = (sof - data) - fr_h_sz;
1342                 if (len < 0)
1343                         len = 0;
1344         }
1345
1346         if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW) {
1347                 /* In raw mode we sometimes get some garbage after the frame
1348                    ignore this */
1349                 int used;
1350                 int size = cam->cam_mode[gspca_dev->curr_mode].sizeimage;
1351
1352                 used = gspca_dev->image_len;
1353                 if (used + len > size)
1354                         len = size - used;
1355         }
1356
1357         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1358
1359         if (sof) {
1360                 int  lum = sd->header[lum_offset] +
1361                           (sd->header[lum_offset + 1] << 8);
1362
1363                 /* When exposure changes midway a frame we
1364                    get a lum of 0 in this case drop 2 frames
1365                    as the frames directly after an exposure
1366                    change have an unstable image. Sometimes lum
1367                    *really* is 0 (cam used in low light with
1368                    low exposure setting), so do not drop frames
1369                    if the previous lum was 0 too. */
1370                 if (lum == 0 && sd->prev_avg_lum != 0) {
1371                         lum = -1;
1372                         sd->frames_to_drop = 2;
1373                         sd->prev_avg_lum = 0;
1374                 } else
1375                         sd->prev_avg_lum = lum;
1376                 atomic_set(&sd->avg_lum, lum);
1377
1378                 if (sd->frames_to_drop)
1379                         sd->frames_to_drop--;
1380                 else
1381                         gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
1382
1383                 gspca_frame_add(gspca_dev, FIRST_PACKET, sof, len_after_sof);
1384         }
1385 }
1386
1387 #if IS_ENABLED(CONFIG_INPUT)
1388 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
1389                         u8 *data,               /* interrupt packet data */
1390                         int len)                /* interrupt packet length */
1391 {
1392         int ret = -EINVAL;
1393
1394         if (len == 1 && data[0] == 1) {
1395                 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
1396                 input_sync(gspca_dev->input_dev);
1397                 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
1398                 input_sync(gspca_dev->input_dev);
1399                 ret = 0;
1400         }
1401
1402         return ret;
1403 }
1404 #endif
1405
1406 /* sub-driver description */
1407 static const struct sd_desc sd_desc = {
1408         .name = MODULE_NAME,
1409         .config = sd_config,
1410         .init = sd_init,
1411         .init_controls = sd_init_controls,
1412         .start = sd_start,
1413         .stopN = sd_stopN,
1414         .pkt_scan = sd_pkt_scan,
1415         .dq_callback = do_autogain,
1416 #if IS_ENABLED(CONFIG_INPUT)
1417         .int_pkt_scan = sd_int_pkt_scan,
1418 #endif
1419 };
1420
1421 /* -- module initialisation -- */
1422 #define SB(sensor, bridge) \
1423         .driver_info = (SENSOR_ ## sensor << 8) | BRIDGE_ ## bridge
1424
1425
1426 static const struct usb_device_id device_table[] = {
1427         {USB_DEVICE(0x0c45, 0x6001), SB(TAS5110C, 102)}, /* TAS5110C1B */
1428         {USB_DEVICE(0x0c45, 0x6005), SB(TAS5110C, 101)}, /* TAS5110C1B */
1429         {USB_DEVICE(0x0c45, 0x6007), SB(TAS5110D, 101)}, /* TAS5110D */
1430         {USB_DEVICE(0x0c45, 0x6009), SB(PAS106, 101)},
1431         {USB_DEVICE(0x0c45, 0x600d), SB(PAS106, 101)},
1432         {USB_DEVICE(0x0c45, 0x6011), SB(OV6650, 101)},
1433         {USB_DEVICE(0x0c45, 0x6019), SB(OV7630, 101)},
1434         {USB_DEVICE(0x0c45, 0x6024), SB(TAS5130CXX, 102)},
1435         {USB_DEVICE(0x0c45, 0x6025), SB(TAS5130CXX, 102)},
1436         {USB_DEVICE(0x0c45, 0x6027), SB(OV7630, 101)}, /* Genius Eye 310 */
1437         {USB_DEVICE(0x0c45, 0x6028), SB(PAS202, 102)},
1438         {USB_DEVICE(0x0c45, 0x6029), SB(PAS106, 102)},
1439         {USB_DEVICE(0x0c45, 0x602a), SB(HV7131D, 102)},
1440         /* {USB_DEVICE(0x0c45, 0x602b), SB(MI0343, 102)}, */
1441         {USB_DEVICE(0x0c45, 0x602c), SB(OV7630, 102)},
1442         {USB_DEVICE(0x0c45, 0x602d), SB(HV7131R, 102)},
1443         {USB_DEVICE(0x0c45, 0x602e), SB(OV7630, 102)},
1444         /* {USB_DEVICE(0x0c45, 0x6030), SB(MI03XX, 102)}, */ /* MI0343 MI0360 MI0330 */
1445         /* {USB_DEVICE(0x0c45, 0x6082), SB(MI03XX, 103)}, */ /* MI0343 MI0360 */
1446         {USB_DEVICE(0x0c45, 0x6083), SB(HV7131D, 103)},
1447         {USB_DEVICE(0x0c45, 0x608c), SB(HV7131R, 103)},
1448         /* {USB_DEVICE(0x0c45, 0x608e), SB(CISVF10, 103)}, */
1449         {USB_DEVICE(0x0c45, 0x608f), SB(OV7630, 103)},
1450         {USB_DEVICE(0x0c45, 0x60a8), SB(PAS106, 103)},
1451         {USB_DEVICE(0x0c45, 0x60aa), SB(TAS5130CXX, 103)},
1452         {USB_DEVICE(0x0c45, 0x60af), SB(PAS202, 103)},
1453         {USB_DEVICE(0x0c45, 0x60b0), SB(OV7630, 103)},
1454         {}
1455 };
1456 MODULE_DEVICE_TABLE(usb, device_table);
1457
1458 /* -- device connect -- */
1459 static int sd_probe(struct usb_interface *intf,
1460                         const struct usb_device_id *id)
1461 {
1462         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1463                                 THIS_MODULE);
1464 }
1465
1466 static struct usb_driver sd_driver = {
1467         .name = MODULE_NAME,
1468         .id_table = device_table,
1469         .probe = sd_probe,
1470         .disconnect = gspca_disconnect,
1471 #ifdef CONFIG_PM
1472         .suspend = gspca_suspend,
1473         .resume = gspca_resume,
1474         .reset_resume = gspca_resume,
1475 #endif
1476 };
1477
1478 module_usb_driver(sd_driver);