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