V4L/DVB: gspca_sonixb: pas202: fixup brightness ctrl and add gain and exposure ctrls
[linux-2.6/btrfs-unstable.git] / drivers / media / video / gspca / sonixb.c
blob66fffa345e7b2ba8f677a3fbf0b875ecaf937a5e
1 /*
2 * sonix sn9c102 (bayer) library
3 * Copyright (C) 2003 2004 Michel Xhaard mxhaard@magic.fr
4 * Add Pas106 Stefano Mozzi (C) 2004
6 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
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.
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.
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
23 /* Some documentation on known sonixb registers:
25 Reg Use
26 0x10 high nibble red gain low nibble blue gain
27 0x11 low nibble green gain
28 0x12 hstart
29 0x13 vstart
30 0x15 hsize (hsize = register-value * 16)
31 0x16 vsize (vsize = register-value * 16)
32 0x17 bit 0 toggle compression quality (according to sn9c102 driver)
33 0x18 bit 7 enables compression, bit 4-5 set image down scaling:
34 00 scale 1, 01 scale 1/2, 10, scale 1/4
35 0x19 high-nibble is sensor clock divider, changes exposure on sensors which
36 use a clock generated by the bridge. Some sensors have their own clock.
37 0x1c auto_exposure area (for avg_lum) startx (startx = register-value * 32)
38 0x1d auto_exposure area (for avg_lum) starty (starty = register-value * 32)
39 0x1e auto_exposure area (for avg_lum) stopx (hsize = (0x1e - 0x1c) * 32)
40 0x1f auto_exposure area (for avg_lum) stopy (vsize = (0x1f - 0x1d) * 32)
43 #define MODULE_NAME "sonixb"
45 #include <linux/input.h>
46 #include "gspca.h"
48 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
49 MODULE_DESCRIPTION("GSPCA/SN9C102 USB Camera Driver");
50 MODULE_LICENSE("GPL");
52 /* specific webcam descriptor */
53 struct sd {
54 struct gspca_dev gspca_dev; /* !! must be the first item */
55 atomic_t avg_lum;
56 int prev_avg_lum;
57 int exp_too_low_cnt;
58 int exp_too_high_cnt;
60 unsigned short exposure;
61 unsigned char gain;
62 unsigned char brightness;
63 unsigned char autogain;
64 unsigned char autogain_ignore_frames;
65 unsigned char frames_to_drop;
66 unsigned char freq; /* light freq filter setting */
68 __u8 bridge; /* Type of bridge */
69 #define BRIDGE_101 0
70 #define BRIDGE_102 0 /* We make no difference between 101 and 102 */
71 #define BRIDGE_103 1
73 __u8 sensor; /* Type of image sensor chip */
74 #define SENSOR_HV7131R 0
75 #define SENSOR_OV6650 1
76 #define SENSOR_OV7630 2
77 #define SENSOR_PAS106 3
78 #define SENSOR_PAS202 4
79 #define SENSOR_TAS5110C 5
80 #define SENSOR_TAS5110D 6
81 #define SENSOR_TAS5130CXX 7
82 __u8 reg11;
85 typedef const __u8 sensor_init_t[8];
87 struct sensor_data {
88 const __u8 *bridge_init[2];
89 int bridge_init_size[2];
90 sensor_init_t *sensor_init;
91 int sensor_init_size;
92 sensor_init_t *sensor_bridge_init[2];
93 int sensor_bridge_init_size[2];
94 int flags;
95 unsigned ctrl_dis;
96 __u8 sensor_addr;
99 /* sensor_data flags */
100 #define F_GAIN 0x01 /* has gain */
101 #define F_SIF 0x02 /* sif or vga */
102 #define F_COARSE_EXPO 0x04 /* exposure control is coarse */
104 /* priv field of struct v4l2_pix_format flags (do not use low nibble!) */
105 #define MODE_RAW 0x10 /* raw bayer mode */
106 #define MODE_REDUCED_SIF 0x20 /* vga mode (320x240 / 160x120) on sif cam */
108 /* ctrl_dis helper macros */
109 #define NO_EXPO ((1 << EXPOSURE_IDX) | (1 << COARSE_EXPOSURE_IDX) | \
110 (1 << AUTOGAIN_IDX))
111 #define NO_FREQ (1 << FREQ_IDX)
112 #define NO_BRIGHTNESS (1 << BRIGHTNESS_IDX)
114 #define COMP2 0x8f
115 #define COMP 0xc7 /* 0x87 //0x07 */
116 #define COMP1 0xc9 /* 0x89 //0x09 */
118 #define MCK_INIT 0x63
119 #define MCK_INIT1 0x20 /*fixme: Bayer - 0x50 for JPEG ??*/
121 #define SYS_CLK 0x04
123 #define SENS(bridge_1, bridge_3, sensor, sensor_1, \
124 sensor_3, _flags, _ctrl_dis, _sensor_addr) \
126 .bridge_init = { bridge_1, bridge_3 }, \
127 .bridge_init_size = { sizeof(bridge_1), sizeof(bridge_3) }, \
128 .sensor_init = sensor, \
129 .sensor_init_size = sizeof(sensor), \
130 .sensor_bridge_init = { sensor_1, sensor_3,}, \
131 .sensor_bridge_init_size = { sizeof(sensor_1), sizeof(sensor_3)}, \
132 .flags = _flags, .ctrl_dis = _ctrl_dis, .sensor_addr = _sensor_addr \
135 /* We calculate the autogain at the end of the transfer of a frame, at this
136 moment a frame with the old settings is being captured and transmitted. So
137 if we adjust the gain or exposure we must ignore atleast the next frame for
138 the new settings to come into effect before doing any other adjustments. */
139 #define AUTOGAIN_IGNORE_FRAMES 1
141 /* V4L2 controls supported by the driver */
142 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
143 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
144 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
145 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
146 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
147 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
148 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
149 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
150 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
151 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
153 static const struct ctrl sd_ctrls[] = {
154 #define BRIGHTNESS_IDX 0
157 .id = V4L2_CID_BRIGHTNESS,
158 .type = V4L2_CTRL_TYPE_INTEGER,
159 .name = "Brightness",
160 .minimum = 0,
161 .maximum = 255,
162 .step = 1,
163 #define BRIGHTNESS_DEF 127
164 .default_value = BRIGHTNESS_DEF,
166 .set = sd_setbrightness,
167 .get = sd_getbrightness,
169 #define GAIN_IDX 1
172 .id = V4L2_CID_GAIN,
173 .type = V4L2_CTRL_TYPE_INTEGER,
174 .name = "Gain",
175 .minimum = 0,
176 .maximum = 255,
177 .step = 1,
178 #define GAIN_DEF 127
179 #define GAIN_KNEE 230
180 .default_value = GAIN_DEF,
182 .set = sd_setgain,
183 .get = sd_getgain,
185 #define EXPOSURE_IDX 2
188 .id = V4L2_CID_EXPOSURE,
189 .type = V4L2_CTRL_TYPE_INTEGER,
190 .name = "Exposure",
191 #define EXPOSURE_DEF 66 /* 33 ms / 30 fps (except on PAS202) */
192 #define EXPOSURE_KNEE 200 /* 100 ms / 10 fps (except on PAS202) */
193 .minimum = 0,
194 .maximum = 1023,
195 .step = 1,
196 .default_value = EXPOSURE_DEF,
197 .flags = 0,
199 .set = sd_setexposure,
200 .get = sd_getexposure,
202 #define COARSE_EXPOSURE_IDX 3
205 .id = V4L2_CID_EXPOSURE,
206 .type = V4L2_CTRL_TYPE_INTEGER,
207 .name = "Exposure",
208 #define COARSE_EXPOSURE_DEF 2 /* 30 fps */
209 .minimum = 2,
210 .maximum = 15,
211 .step = 1,
212 .default_value = COARSE_EXPOSURE_DEF,
213 .flags = 0,
215 .set = sd_setexposure,
216 .get = sd_getexposure,
218 #define AUTOGAIN_IDX 4
221 .id = V4L2_CID_AUTOGAIN,
222 .type = V4L2_CTRL_TYPE_BOOLEAN,
223 .name = "Automatic Gain (and Exposure)",
224 .minimum = 0,
225 .maximum = 1,
226 .step = 1,
227 #define AUTOGAIN_DEF 1
228 .default_value = AUTOGAIN_DEF,
229 .flags = 0,
231 .set = sd_setautogain,
232 .get = sd_getautogain,
234 #define FREQ_IDX 5
237 .id = V4L2_CID_POWER_LINE_FREQUENCY,
238 .type = V4L2_CTRL_TYPE_MENU,
239 .name = "Light frequency filter",
240 .minimum = 0,
241 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
242 .step = 1,
243 #define FREQ_DEF 1
244 .default_value = FREQ_DEF,
246 .set = sd_setfreq,
247 .get = sd_getfreq,
251 static const struct v4l2_pix_format vga_mode[] = {
252 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
253 .bytesperline = 160,
254 .sizeimage = 160 * 120,
255 .colorspace = V4L2_COLORSPACE_SRGB,
256 .priv = 2 | MODE_RAW},
257 {160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
258 .bytesperline = 160,
259 .sizeimage = 160 * 120 * 5 / 4,
260 .colorspace = V4L2_COLORSPACE_SRGB,
261 .priv = 2},
262 {320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
263 .bytesperline = 320,
264 .sizeimage = 320 * 240 * 5 / 4,
265 .colorspace = V4L2_COLORSPACE_SRGB,
266 .priv = 1},
267 {640, 480, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
268 .bytesperline = 640,
269 .sizeimage = 640 * 480 * 5 / 4,
270 .colorspace = V4L2_COLORSPACE_SRGB,
271 .priv = 0},
273 static const struct v4l2_pix_format sif_mode[] = {
274 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
275 .bytesperline = 160,
276 .sizeimage = 160 * 120,
277 .colorspace = V4L2_COLORSPACE_SRGB,
278 .priv = 1 | MODE_RAW | MODE_REDUCED_SIF},
279 {160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
280 .bytesperline = 160,
281 .sizeimage = 160 * 120 * 5 / 4,
282 .colorspace = V4L2_COLORSPACE_SRGB,
283 .priv = 1 | MODE_REDUCED_SIF},
284 {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
285 .bytesperline = 176,
286 .sizeimage = 176 * 144,
287 .colorspace = V4L2_COLORSPACE_SRGB,
288 .priv = 1 | MODE_RAW},
289 {176, 144, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
290 .bytesperline = 176,
291 .sizeimage = 176 * 144 * 5 / 4,
292 .colorspace = V4L2_COLORSPACE_SRGB,
293 .priv = 1},
294 {320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
295 .bytesperline = 320,
296 .sizeimage = 320 * 240 * 5 / 4,
297 .colorspace = V4L2_COLORSPACE_SRGB,
298 .priv = 0 | MODE_REDUCED_SIF},
299 {352, 288, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
300 .bytesperline = 352,
301 .sizeimage = 352 * 288 * 5 / 4,
302 .colorspace = V4L2_COLORSPACE_SRGB,
303 .priv = 0},
306 static const __u8 initHv7131[] = {
307 0x46, 0x77, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,
308 0x00, 0x00,
309 0x00, 0x00, 0x00, 0x02, 0x01, 0x00,
310 0x28, 0x1e, 0x60, 0x8a, 0x20,
311 0x1d, 0x10, 0x02, 0x03, 0x0f, 0x0c
313 static const __u8 hv7131_sensor_init[][8] = {
314 {0xc0, 0x11, 0x31, 0x38, 0x2a, 0x2e, 0x00, 0x10},
315 {0xa0, 0x11, 0x01, 0x08, 0x2a, 0x2e, 0x00, 0x10},
316 {0xb0, 0x11, 0x20, 0x00, 0xd0, 0x2e, 0x00, 0x10},
317 {0xc0, 0x11, 0x25, 0x03, 0x0e, 0x28, 0x00, 0x16},
318 {0xa0, 0x11, 0x30, 0x10, 0x0e, 0x28, 0x00, 0x15},
320 static const __u8 initOv6650[] = {
321 0x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
322 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
323 0x00, 0x01, 0x01, 0x0a, 0x16, 0x12, 0x68, 0x8b,
324 0x10, 0x1d, 0x10, 0x02, 0x02, 0x09, 0x07
326 static const __u8 ov6650_sensor_init[][8] =
328 /* Bright, contrast, etc are set through SCBB interface.
329 * AVCAP on win2 do not send any data on this controls. */
330 /* Anyway, some registers appears to alter bright and constrat */
332 /* Reset sensor */
333 {0xa0, 0x60, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
334 /* Set clock register 0x11 low nibble is clock divider */
335 {0xd0, 0x60, 0x11, 0xc0, 0x1b, 0x18, 0xc1, 0x10},
336 /* Next some unknown stuff */
337 {0xb0, 0x60, 0x15, 0x00, 0x02, 0x18, 0xc1, 0x10},
338 /* {0xa0, 0x60, 0x1b, 0x01, 0x02, 0x18, 0xc1, 0x10},
339 * THIS SET GREEN SCREEN
340 * (pixels could be innverted in decode kind of "brg",
341 * but blue wont be there. Avoid this data ... */
342 {0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10}, /* format out? */
343 {0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10},
344 {0xa0, 0x60, 0x30, 0x3d, 0x0A, 0xd8, 0xa4, 0x10},
345 /* Enable rgb brightness control */
346 {0xa0, 0x60, 0x61, 0x08, 0x00, 0x00, 0x00, 0x10},
347 /* HDG: Note windows uses the line below, which sets both register 0x60
348 and 0x61 I believe these registers of the ov6650 are identical as
349 those of the ov7630, because if this is true the windows settings
350 add a bit additional red gain and a lot additional blue gain, which
351 matches my findings that the windows settings make blue much too
352 blue and red a little too red.
353 {0xb0, 0x60, 0x60, 0x66, 0x68, 0xd8, 0xa4, 0x10}, */
354 /* Some more unknown stuff */
355 {0xa0, 0x60, 0x68, 0x04, 0x68, 0xd8, 0xa4, 0x10},
356 {0xd0, 0x60, 0x17, 0x24, 0xd6, 0x04, 0x94, 0x10}, /* Clipreg */
359 static const __u8 initOv7630[] = {
360 0x04, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, /* r01 .. r08 */
361 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* r09 .. r10 */
362 0x00, 0x01, 0x01, 0x0a, /* r11 .. r14 */
363 0x28, 0x1e, /* H & V sizes r15 .. r16 */
364 0x68, COMP2, MCK_INIT1, /* r17 .. r19 */
365 0x1d, 0x10, 0x02, 0x03, 0x0f, 0x0c /* r1a .. r1f */
367 static const __u8 initOv7630_3[] = {
368 0x44, 0x44, 0x00, 0x1a, 0x20, 0x20, 0x20, 0x80, /* r01 .. r08 */
369 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* r09 .. r10 */
370 0x00, 0x02, 0x01, 0x0a, /* r11 .. r14 */
371 0x28, 0x1e, /* H & V sizes r15 .. r16 */
372 0x68, 0x8f, MCK_INIT1, /* r17 .. r19 */
373 0x1d, 0x10, 0x02, 0x03, 0x0f, 0x0c, 0x00, /* r1a .. r20 */
374 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, /* r21 .. r28 */
375 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0xff /* r29 .. r30 */
377 static const __u8 ov7630_sensor_init[][8] = {
378 {0xa0, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
379 {0xb0, 0x21, 0x01, 0x77, 0x3a, 0x00, 0x00, 0x10},
380 /* {0xd0, 0x21, 0x12, 0x7c, 0x01, 0x80, 0x34, 0x10}, jfm */
381 {0xd0, 0x21, 0x12, 0x1c, 0x00, 0x80, 0x34, 0x10}, /* jfm */
382 {0xa0, 0x21, 0x1b, 0x04, 0x00, 0x80, 0x34, 0x10},
383 {0xa0, 0x21, 0x20, 0x44, 0x00, 0x80, 0x34, 0x10},
384 {0xa0, 0x21, 0x23, 0xee, 0x00, 0x80, 0x34, 0x10},
385 {0xd0, 0x21, 0x26, 0xa0, 0x9a, 0xa0, 0x30, 0x10},
386 {0xb0, 0x21, 0x2a, 0x80, 0x00, 0xa0, 0x30, 0x10},
387 {0xb0, 0x21, 0x2f, 0x3d, 0x24, 0xa0, 0x30, 0x10},
388 {0xa0, 0x21, 0x32, 0x86, 0x24, 0xa0, 0x30, 0x10},
389 {0xb0, 0x21, 0x60, 0xa9, 0x4a, 0xa0, 0x30, 0x10},
390 /* {0xb0, 0x21, 0x60, 0xa9, 0x42, 0xa0, 0x30, 0x10}, * jfm */
391 {0xa0, 0x21, 0x65, 0x00, 0x42, 0xa0, 0x30, 0x10},
392 {0xa0, 0x21, 0x69, 0x38, 0x42, 0xa0, 0x30, 0x10},
393 {0xc0, 0x21, 0x6f, 0x88, 0x0b, 0x00, 0x30, 0x10},
394 {0xc0, 0x21, 0x74, 0x21, 0x8e, 0x00, 0x30, 0x10},
395 {0xa0, 0x21, 0x7d, 0xf7, 0x8e, 0x00, 0x30, 0x10},
396 {0xd0, 0x21, 0x17, 0x1c, 0xbd, 0x06, 0xf6, 0x10},
399 static const __u8 ov7630_sensor_init_3[][8] = {
400 {0xa0, 0x21, 0x13, 0x80, 0x00, 0x00, 0x00, 0x10},
403 static const __u8 initPas106[] = {
404 0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x40, 0x00, 0x00, 0x00,
405 0x00, 0x00,
406 0x00, 0x00, 0x00, 0x04, 0x01, 0x00,
407 0x16, 0x12, 0x24, COMP1, MCK_INIT1,
408 0x18, 0x10, 0x02, 0x02, 0x09, 0x07
410 /* compression 0x86 mckinit1 0x2b */
411 static 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 },
453 static const __u8 initPas202[] = {
454 0x44, 0x44, 0x21, 0x30, 0x00, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0x00,
455 0x00, 0x00,
456 0x00, 0x00, 0x00, 0x06, 0x03, 0x0a,
457 0x28, 0x1e, 0x20, 0x89, 0x20,
458 0x00, 0x00, 0x02, 0x03, 0x0f, 0x0c
461 /* "Known" PAS202BCB registers:
462 0x02 clock divider
463 0x04 Variable framerate bits 6-11 (*)
464 0x05 Var framerate bits 0-5, one must leave the 2 msb's at 0 !!
465 0x07 Blue Gain
466 0x08 Green Gain
467 0x09 Red Gain
468 0x0b offset sign bit (bit0 1 > negative offset)
469 0x0c offset
470 0x0e Unknown image is slightly brighter when bit 0 is 0, if reg0f is 0 too,
471 leave at 1 otherwise we get a jump in our exposure control
472 0x0f Exposure 0-255, 0 = use full frame time, 255 = no exposure at all
473 0x10 Master gain 0 - 31
474 0x11 write 1 to apply changes
475 (*) The variable framerate control must never be set lower then 500
476 which sets the framerate at 30 / reg02, otherwise vsync is lost.
478 static const __u8 pas202_sensor_init[][8] = {
479 /* Set the clock divider to 4 -> 30 / 4 = 7.5 fps, we would like
480 to set it lower, but for some reason the bridge starts missing
481 vsync's then */
482 {0xa0, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x10},
483 {0xd0, 0x40, 0x04, 0x07, 0x34, 0x00, 0x09, 0x10},
484 {0xd0, 0x40, 0x08, 0x01, 0x00, 0x00, 0x01, 0x10},
485 {0xd0, 0x40, 0x0C, 0x00, 0x0C, 0x01, 0x32, 0x10},
486 {0xd0, 0x40, 0x10, 0x00, 0x01, 0x00, 0x63, 0x10},
487 {0xa0, 0x40, 0x15, 0x70, 0x01, 0x00, 0x63, 0x10},
488 {0xa0, 0x40, 0x18, 0x00, 0x01, 0x00, 0x63, 0x10},
489 {0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
490 {0xa0, 0x40, 0x03, 0x56, 0x01, 0x00, 0x63, 0x10},
491 {0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
494 static const __u8 initTas5110c[] = {
495 0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
496 0x00, 0x00,
497 0x00, 0x00, 0x00, 0x45, 0x09, 0x0a,
498 0x16, 0x12, 0x60, 0x86, 0x2b,
499 0x14, 0x0a, 0x02, 0x02, 0x09, 0x07
501 /* Same as above, except a different hstart */
502 static const __u8 initTas5110d[] = {
503 0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
504 0x00, 0x00,
505 0x00, 0x00, 0x00, 0x41, 0x09, 0x0a,
506 0x16, 0x12, 0x60, 0x86, 0x2b,
507 0x14, 0x0a, 0x02, 0x02, 0x09, 0x07
509 static const __u8 tas5110_sensor_init[][8] = {
510 {0x30, 0x11, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x10},
511 {0x30, 0x11, 0x02, 0x20, 0xa9, 0x00, 0x00, 0x10},
512 {0xa0, 0x61, 0x9a, 0xca, 0x00, 0x00, 0x00, 0x17},
515 static const __u8 initTas5130[] = {
516 0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
517 0x00, 0x00,
518 0x00, 0x00, 0x00, 0x68, 0x0c, 0x0a,
519 0x28, 0x1e, 0x60, COMP, MCK_INIT,
520 0x18, 0x10, 0x04, 0x03, 0x11, 0x0c
522 static const __u8 tas5130_sensor_init[][8] = {
523 /* {0x30, 0x11, 0x00, 0x40, 0x47, 0x00, 0x00, 0x10},
524 * shutter 0x47 short exposure? */
525 {0x30, 0x11, 0x00, 0x40, 0x01, 0x00, 0x00, 0x10},
526 /* shutter 0x01 long exposure */
527 {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10},
530 static struct sensor_data sensor_data[] = {
531 SENS(initHv7131, NULL, hv7131_sensor_init, NULL, NULL, 0, NO_EXPO|NO_FREQ, 0),
532 SENS(initOv6650, NULL, ov6650_sensor_init, NULL, NULL, F_GAIN|F_SIF, 0, 0x60),
533 SENS(initOv7630, initOv7630_3, ov7630_sensor_init, NULL, ov7630_sensor_init_3,
534 F_GAIN, 0, 0x21),
535 SENS(initPas106, NULL, pas106_sensor_init, NULL, NULL, F_SIF, NO_EXPO|NO_FREQ,
537 SENS(initPas202, initPas202, pas202_sensor_init, NULL, NULL, F_GAIN,
538 NO_FREQ, 0),
539 SENS(initTas5110c, NULL, tas5110_sensor_init, NULL, NULL,
540 F_GAIN|F_SIF|F_COARSE_EXPO, NO_BRIGHTNESS|NO_FREQ, 0),
541 SENS(initTas5110d, NULL, tas5110_sensor_init, NULL, NULL,
542 F_GAIN|F_SIF|F_COARSE_EXPO, NO_BRIGHTNESS|NO_FREQ, 0),
543 SENS(initTas5130, NULL, tas5130_sensor_init, NULL, NULL, 0, NO_EXPO|NO_FREQ,
547 /* get one byte in gspca_dev->usb_buf */
548 static void reg_r(struct gspca_dev *gspca_dev,
549 __u16 value)
551 usb_control_msg(gspca_dev->dev,
552 usb_rcvctrlpipe(gspca_dev->dev, 0),
553 0, /* request */
554 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
555 value,
556 0, /* index */
557 gspca_dev->usb_buf, 1,
558 500);
561 static void reg_w(struct gspca_dev *gspca_dev,
562 __u16 value,
563 const __u8 *buffer,
564 int len)
566 #ifdef GSPCA_DEBUG
567 if (len > USB_BUF_SZ) {
568 PDEBUG(D_ERR|D_PACK, "reg_w: buffer overflow");
569 return;
571 #endif
572 memcpy(gspca_dev->usb_buf, buffer, len);
573 usb_control_msg(gspca_dev->dev,
574 usb_sndctrlpipe(gspca_dev->dev, 0),
575 0x08, /* request */
576 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
577 value,
578 0, /* index */
579 gspca_dev->usb_buf, len,
580 500);
583 static int i2c_w(struct gspca_dev *gspca_dev, const __u8 *buffer)
585 int retry = 60;
587 /* is i2c ready */
588 reg_w(gspca_dev, 0x08, buffer, 8);
589 while (retry--) {
590 msleep(10);
591 reg_r(gspca_dev, 0x08);
592 if (gspca_dev->usb_buf[0] & 0x04) {
593 if (gspca_dev->usb_buf[0] & 0x08)
594 return -1;
595 return 0;
598 return -1;
601 static void i2c_w_vector(struct gspca_dev *gspca_dev,
602 const __u8 buffer[][8], int len)
604 for (;;) {
605 reg_w(gspca_dev, 0x08, *buffer, 8);
606 len -= 8;
607 if (len <= 0)
608 break;
609 buffer++;
613 static void setbrightness(struct gspca_dev *gspca_dev)
615 struct sd *sd = (struct sd *) gspca_dev;
616 __u8 value;
618 switch (sd->sensor) {
619 case SENSOR_OV6650:
620 case SENSOR_OV7630: {
621 __u8 i2cOV[] =
622 {0xa0, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10};
624 /* change reg 0x06 */
625 i2cOV[1] = sensor_data[sd->sensor].sensor_addr;
626 i2cOV[3] = sd->brightness;
627 if (i2c_w(gspca_dev, i2cOV) < 0)
628 goto err;
629 break;
631 case SENSOR_PAS106: {
632 __u8 i2c1[] =
633 {0xa1, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14};
635 i2c1[3] = sd->brightness >> 3;
636 i2c1[2] = 0x0e;
637 if (i2c_w(gspca_dev, i2c1) < 0)
638 goto err;
639 i2c1[3] = 0x01;
640 i2c1[2] = 0x13;
641 if (i2c_w(gspca_dev, i2c1) < 0)
642 goto err;
643 break;
645 case SENSOR_PAS202: {
646 __u8 i2cpbright[] =
647 {0xb0, 0x40, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x16};
648 const __u8 i2cpdoit[] =
649 {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
651 if (sd->brightness < 127) {
652 /* change reg 0x0b, signreg */
653 i2cpbright[3] = 0x01;
654 /* set reg 0x0c, offset */
655 i2cpbright[4] = 127 - sd->brightness;
656 } else
657 i2cpbright[4] = sd->brightness - 127;
659 if (i2c_w(gspca_dev, i2cpbright) < 0)
660 goto err;
661 if (i2c_w(gspca_dev, i2cpdoit) < 0)
662 goto err;
663 break;
665 case SENSOR_TAS5130CXX: {
666 __u8 i2c[] =
667 {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};
669 value = 0xff - sd->brightness;
670 i2c[4] = value;
671 PDEBUG(D_CONF, "brightness %d : %d", value, i2c[4]);
672 if (i2c_w(gspca_dev, i2c) < 0)
673 goto err;
674 break;
677 return;
678 err:
679 PDEBUG(D_ERR, "i2c error brightness");
682 static void setsensorgain(struct gspca_dev *gspca_dev)
684 struct sd *sd = (struct sd *) gspca_dev;
685 unsigned char gain = sd->gain;
687 switch (sd->sensor) {
689 case SENSOR_TAS5110C:
690 case SENSOR_TAS5110D: {
691 __u8 i2c[] =
692 {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};
694 i2c[4] = 255 - gain;
695 if (i2c_w(gspca_dev, i2c) < 0)
696 goto err;
697 break;
700 case SENSOR_OV6650:
701 gain >>= 1;
702 /* fall thru */
703 case SENSOR_OV7630: {
704 __u8 i2c[] = {0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
706 i2c[1] = sensor_data[sd->sensor].sensor_addr;
707 i2c[3] = gain >> 2;
708 if (i2c_w(gspca_dev, i2c) < 0)
709 goto err;
710 break;
712 case SENSOR_PAS202: {
713 __u8 i2cpgain[] =
714 {0xa0, 0x40, 0x10, 0x00, 0x00, 0x00, 0x63, 0x15};
715 __u8 i2cpcolorgain[] =
716 {0xc0, 0x40, 0x07, 0x00, 0x00, 0x00, 0x00, 0x15};
717 const __u8 i2cpdoit[] =
718 {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x63, 0x16};
720 i2cpgain[3] = sd->gain >> 3;
721 i2cpcolorgain[3] = sd->gain >> 4;
722 i2cpcolorgain[4] = sd->gain >> 4;
723 i2cpcolorgain[5] = sd->gain >> 4;
725 if (i2c_w(gspca_dev, i2cpgain) < 0)
726 goto err;
727 if (i2c_w(gspca_dev, i2cpcolorgain) < 0)
728 goto err;
729 if (i2c_w(gspca_dev, i2cpdoit) < 0)
730 goto err;
731 break;
734 return;
735 err:
736 PDEBUG(D_ERR, "i2c error gain");
739 static void setgain(struct gspca_dev *gspca_dev)
741 struct sd *sd = (struct sd *) gspca_dev;
742 __u8 gain;
743 __u8 buf[2] = { 0, 0 };
745 if (sensor_data[sd->sensor].flags & F_GAIN) {
746 /* Use the sensor gain to do the actual gain */
747 setsensorgain(gspca_dev);
748 return;
751 gain = sd->gain >> 4;
753 /* red and blue gain */
754 buf[0] = gain << 4 | gain;
755 /* green gain */
756 buf[1] = gain;
757 reg_w(gspca_dev, 0x10, buf, 2);
760 static void setexposure(struct gspca_dev *gspca_dev)
762 struct sd *sd = (struct sd *) gspca_dev;
764 switch (sd->sensor) {
765 case SENSOR_TAS5110C:
766 case SENSOR_TAS5110D: {
767 /* register 19's high nibble contains the sn9c10x clock divider
768 The high nibble configures the no fps according to the
769 formula: 60 / high_nibble. With a maximum of 30 fps */
770 __u8 reg = sd->exposure;
771 reg = (reg << 4) | 0x0b;
772 reg_w(gspca_dev, 0x19, &reg, 1);
773 break;
775 case SENSOR_OV6650:
776 case SENSOR_OV7630: {
777 /* The ov6650 / ov7630 have 2 registers which both influence
778 exposure, register 11, whose low nibble sets the nr off fps
779 according to: fps = 30 / (low_nibble + 1)
781 The fps configures the maximum exposure setting, but it is
782 possible to use less exposure then what the fps maximum
783 allows by setting register 10. register 10 configures the
784 actual exposure as quotient of the full exposure, with 0
785 being no exposure at all (not very usefull) and reg10_max
786 being max exposure possible at that framerate.
788 The code maps our 0 - 510 ms exposure ctrl to these 2
789 registers, trying to keep fps as high as possible.
791 __u8 i2c[] = {0xb0, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10};
792 int reg10, reg11, reg10_max;
794 /* ov6645 datasheet says reg10_max is 9a, but that uses
795 tline * 2 * reg10 as formula for calculating texpo, the
796 ov6650 probably uses the same formula as the 7730 which uses
797 tline * 4 * reg10, which explains why the reg10max we've
798 found experimentally for the ov6650 is exactly half that of
799 the ov6645. The ov7630 datasheet says the max is 0x41. */
800 if (sd->sensor == SENSOR_OV6650) {
801 reg10_max = 0x4d;
802 i2c[4] = 0xc0; /* OV6650 needs non default vsync pol */
803 } else
804 reg10_max = 0x41;
806 reg11 = (15 * sd->exposure + 999) / 1000;
807 if (reg11 < 1)
808 reg11 = 1;
809 else if (reg11 > 16)
810 reg11 = 16;
812 /* In 640x480, if the reg11 has less than 3, the image is
813 unstable (not enough bandwidth). */
814 if (gspca_dev->width == 640 && reg11 < 3)
815 reg11 = 3;
817 /* frame exposure time in ms = 1000 * reg11 / 30 ->
818 reg10 = (sd->exposure / 2) * reg10_max / (1000 * reg11 / 30) */
819 reg10 = (sd->exposure * 15 * reg10_max) / (1000 * reg11);
821 /* Don't allow this to get below 10 when using autogain, the
822 steps become very large (relatively) when below 10 causing
823 the image to oscilate from much too dark, to much too bright
824 and back again. */
825 if (sd->autogain && reg10 < 10)
826 reg10 = 10;
827 else if (reg10 > reg10_max)
828 reg10 = reg10_max;
830 /* Write reg 10 and reg11 low nibble */
831 i2c[1] = sensor_data[sd->sensor].sensor_addr;
832 i2c[3] = reg10;
833 i2c[4] |= reg11 - 1;
835 /* If register 11 didn't change, don't change it */
836 if (sd->reg11 == reg11 )
837 i2c[0] = 0xa0;
839 if (i2c_w(gspca_dev, i2c) == 0)
840 sd->reg11 = reg11;
841 else
842 goto err;
843 break;
845 case SENSOR_PAS202: {
846 __u8 i2cpframerate[] =
847 {0xb0, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x16};
848 __u8 i2cpexpo[] =
849 {0xa0, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x16};
850 const __u8 i2cpdoit[] =
851 {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
852 int framerate_ctrl;
854 /* The exposure knee for the autogain algorithm is 200
855 (100 ms / 10 fps on other sensors), for values below this
856 use the control for setting the partial frame expose time,
857 above that use variable framerate. This way we run at max
858 framerate (640x480@7.5 fps, 320x240@10fps) until the knee
859 is reached. Using the variable framerate control above 200
860 is better then playing around with both clockdiv + partial
861 frame exposure times (like we are doing with the ov chips),
862 as that sometimes leads to jumps in the exposure control,
863 which are bad for auto exposure. */
864 if (sd->exposure < 200) {
865 i2cpexpo[3] = 255 - (sd->exposure * 255) / 200;
866 framerate_ctrl = 500;
867 } else {
868 /* The PAS202's exposure control goes from 0 - 4095,
869 but anything below 500 causes vsync issues, so scale
870 our 200-1023 to 500-4095 */
871 framerate_ctrl = (sd->exposure - 200) * 1000 / 229 +
872 500;
875 i2cpframerate[3] = framerate_ctrl >> 6;
876 i2cpframerate[4] = framerate_ctrl & 0x3f;
877 if (i2c_w(gspca_dev, i2cpframerate) < 0)
878 goto err;
879 if (i2c_w(gspca_dev, i2cpexpo) < 0)
880 goto err;
881 if (i2c_w(gspca_dev, i2cpdoit) < 0)
882 goto err;
883 break;
886 return;
887 err:
888 PDEBUG(D_ERR, "i2c error exposure");
891 static void setfreq(struct gspca_dev *gspca_dev)
893 struct sd *sd = (struct sd *) gspca_dev;
895 switch (sd->sensor) {
896 case SENSOR_OV6650:
897 case SENSOR_OV7630: {
898 /* Framerate adjust register for artificial light 50 hz flicker
899 compensation, for the ov6650 this is identical to ov6630
900 0x2b register, see ov6630 datasheet.
901 0x4f / 0x8a -> (30 fps -> 25 fps), 0x00 -> no adjustment */
902 __u8 i2c[] = {0xa0, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10};
903 switch (sd->freq) {
904 default:
905 /* case 0: * no filter*/
906 /* case 2: * 60 hz */
907 i2c[3] = 0;
908 break;
909 case 1: /* 50 hz */
910 i2c[3] = (sd->sensor == SENSOR_OV6650)
911 ? 0x4f : 0x8a;
912 break;
914 i2c[1] = sensor_data[sd->sensor].sensor_addr;
915 if (i2c_w(gspca_dev, i2c) < 0)
916 PDEBUG(D_ERR, "i2c error setfreq");
917 break;
922 #include "coarse_expo_autogain.h"
924 static void do_autogain(struct gspca_dev *gspca_dev)
926 int deadzone, desired_avg_lum, result;
927 struct sd *sd = (struct sd *) gspca_dev;
928 int avg_lum = atomic_read(&sd->avg_lum);
930 if (avg_lum == -1 || !sd->autogain)
931 return;
933 if (sd->autogain_ignore_frames > 0) {
934 sd->autogain_ignore_frames--;
935 return;
938 /* SIF / VGA sensors have a different autoexposure area and thus
939 different avg_lum values for the same picture brightness */
940 if (sensor_data[sd->sensor].flags & F_SIF) {
941 deadzone = 500;
942 /* SIF sensors tend to overexpose, so keep this small */
943 desired_avg_lum = 5000;
944 } else {
945 deadzone = 1500;
946 desired_avg_lum = 23000;
949 if (sensor_data[sd->sensor].flags & F_COARSE_EXPO)
950 result = gspca_coarse_grained_expo_autogain(gspca_dev, avg_lum,
951 sd->brightness * desired_avg_lum / 127,
952 deadzone);
953 else
954 result = gspca_auto_gain_n_exposure(gspca_dev, avg_lum,
955 sd->brightness * desired_avg_lum / 127,
956 deadzone, GAIN_KNEE, EXPOSURE_KNEE);
958 if (result) {
959 PDEBUG(D_FRAM, "autogain: gain changed: gain: %d expo: %d",
960 (int)sd->gain, (int)sd->exposure);
961 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
965 /* this function is called at probe time */
966 static int sd_config(struct gspca_dev *gspca_dev,
967 const struct usb_device_id *id)
969 struct sd *sd = (struct sd *) gspca_dev;
970 struct cam *cam;
972 reg_r(gspca_dev, 0x00);
973 if (gspca_dev->usb_buf[0] != 0x10)
974 return -ENODEV;
976 /* copy the webcam info from the device id */
977 sd->sensor = id->driver_info >> 8;
978 sd->bridge = id->driver_info & 0xff;
979 gspca_dev->ctrl_dis = sensor_data[sd->sensor].ctrl_dis;
981 cam = &gspca_dev->cam;
982 if (!(sensor_data[sd->sensor].flags & F_SIF)) {
983 cam->cam_mode = vga_mode;
984 cam->nmodes = ARRAY_SIZE(vga_mode);
985 } else {
986 cam->cam_mode = sif_mode;
987 cam->nmodes = ARRAY_SIZE(sif_mode);
989 cam->npkt = 36; /* 36 packets per ISOC message */
991 sd->brightness = BRIGHTNESS_DEF;
992 sd->gain = GAIN_DEF;
993 if (sensor_data[sd->sensor].flags & F_COARSE_EXPO) {
994 sd->exposure = COARSE_EXPOSURE_DEF;
995 gspca_dev->ctrl_dis |= (1 << EXPOSURE_IDX);
996 } else {
997 sd->exposure = EXPOSURE_DEF;
998 gspca_dev->ctrl_dis |= (1 << COARSE_EXPOSURE_IDX);
1000 if (gspca_dev->ctrl_dis & (1 << AUTOGAIN_IDX))
1001 sd->autogain = 0; /* Disable do_autogain callback */
1002 else
1003 sd->autogain = AUTOGAIN_DEF;
1004 sd->freq = FREQ_DEF;
1006 return 0;
1009 /* this function is called at probe and resume time */
1010 static int sd_init(struct gspca_dev *gspca_dev)
1012 const __u8 stop = 0x09; /* Disable stream turn of LED */
1014 reg_w(gspca_dev, 0x01, &stop, 1);
1016 return 0;
1019 /* -- start the camera -- */
1020 static int sd_start(struct gspca_dev *gspca_dev)
1022 struct sd *sd = (struct sd *) gspca_dev;
1023 struct cam *cam = &gspca_dev->cam;
1024 int mode, l;
1025 const __u8 *sn9c10x;
1026 __u8 reg12_19[8];
1028 mode = cam->cam_mode[gspca_dev->curr_mode].priv & 0x07;
1029 sn9c10x = sensor_data[sd->sensor].bridge_init[sd->bridge];
1030 l = sensor_data[sd->sensor].bridge_init_size[sd->bridge];
1031 memcpy(reg12_19, &sn9c10x[0x12 - 1], 8);
1032 reg12_19[6] = sn9c10x[0x18 - 1] | (mode << 4);
1033 /* Special cases where reg 17 and or 19 value depends on mode */
1034 switch (sd->sensor) {
1035 case SENSOR_TAS5130CXX:
1036 /* probably not mode specific at all most likely the upper
1037 nibble of 0x19 is exposure (clock divider) just as with
1038 the tas5110, we need someone to test this. */
1039 reg12_19[7] = mode ? 0x23 : 0x43;
1040 break;
1042 /* Disable compression when the raw bayer format has been selected */
1043 if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW)
1044 reg12_19[6] &= ~0x80;
1046 /* Vga mode emulation on SIF sensor? */
1047 if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_REDUCED_SIF) {
1048 reg12_19[0] += 16; /* 0x12: hstart adjust */
1049 reg12_19[1] += 24; /* 0x13: vstart adjust */
1050 reg12_19[3] = 320 / 16; /* 0x15: hsize */
1051 reg12_19[4] = 240 / 16; /* 0x16: vsize */
1054 /* reg 0x01 bit 2 video transfert on */
1055 reg_w(gspca_dev, 0x01, &sn9c10x[0x01 - 1], 1);
1056 /* reg 0x17 SensorClk enable inv Clk 0x60 */
1057 reg_w(gspca_dev, 0x17, &sn9c10x[0x17 - 1], 1);
1058 /* Set the registers from the template */
1059 reg_w(gspca_dev, 0x01, sn9c10x, l);
1061 /* Init the sensor */
1062 i2c_w_vector(gspca_dev, sensor_data[sd->sensor].sensor_init,
1063 sensor_data[sd->sensor].sensor_init_size);
1064 if (sensor_data[sd->sensor].sensor_bridge_init[sd->bridge])
1065 i2c_w_vector(gspca_dev,
1066 sensor_data[sd->sensor].sensor_bridge_init[sd->bridge],
1067 sensor_data[sd->sensor].sensor_bridge_init_size[
1068 sd->bridge]);
1070 /* Mode specific sensor setup */
1071 switch (sd->sensor) {
1072 case SENSOR_PAS202: {
1073 const __u8 i2cpclockdiv[] =
1074 {0xa0, 0x40, 0x02, 0x03, 0x00, 0x00, 0x00, 0x10};
1075 /* clockdiv from 4 to 3 (7.5 -> 10 fps) when in low res mode */
1076 if (mode)
1077 i2c_w(gspca_dev, i2cpclockdiv);
1080 /* H_size V_size 0x28, 0x1e -> 640x480. 0x16, 0x12 -> 352x288 */
1081 reg_w(gspca_dev, 0x15, &reg12_19[3], 2);
1082 /* compression register */
1083 reg_w(gspca_dev, 0x18, &reg12_19[6], 1);
1084 /* H_start */
1085 reg_w(gspca_dev, 0x12, &reg12_19[0], 1);
1086 /* V_START */
1087 reg_w(gspca_dev, 0x13, &reg12_19[1], 1);
1088 /* reset 0x17 SensorClk enable inv Clk 0x60 */
1089 /*fixme: ov7630 [17]=68 8f (+20 if 102)*/
1090 reg_w(gspca_dev, 0x17, &reg12_19[5], 1);
1091 /*MCKSIZE ->3 */ /*fixme: not ov7630*/
1092 reg_w(gspca_dev, 0x19, &reg12_19[7], 1);
1093 /* AE_STRX AE_STRY AE_ENDX AE_ENDY */
1094 reg_w(gspca_dev, 0x1c, &sn9c10x[0x1c - 1], 4);
1095 /* Enable video transfert */
1096 reg_w(gspca_dev, 0x01, &sn9c10x[0], 1);
1097 /* Compression */
1098 reg_w(gspca_dev, 0x18, &reg12_19[6], 2);
1099 msleep(20);
1101 sd->reg11 = -1;
1103 setgain(gspca_dev);
1104 setbrightness(gspca_dev);
1105 setexposure(gspca_dev);
1106 setfreq(gspca_dev);
1108 sd->frames_to_drop = 0;
1109 sd->autogain_ignore_frames = 0;
1110 sd->exp_too_high_cnt = 0;
1111 sd->exp_too_low_cnt = 0;
1112 atomic_set(&sd->avg_lum, -1);
1113 return 0;
1116 static void sd_stopN(struct gspca_dev *gspca_dev)
1118 sd_init(gspca_dev);
1121 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1122 u8 *data, /* isoc packet */
1123 int len) /* iso packet length */
1125 int i;
1126 struct sd *sd = (struct sd *) gspca_dev;
1127 struct cam *cam = &gspca_dev->cam;
1129 /* frames start with:
1130 * ff ff 00 c4 c4 96 synchro
1131 * 00 (unknown)
1132 * xx (frame sequence / size / compression)
1133 * (xx) (idem - extra byte for sn9c103)
1134 * ll mm brightness sum inside auto exposure
1135 * ll mm brightness sum outside auto exposure
1136 * (xx xx xx xx xx) audio values for snc103
1138 if (len > 6 && len < 24) {
1139 for (i = 0; i < len - 6; i++) {
1140 if (data[0 + i] == 0xff
1141 && data[1 + i] == 0xff
1142 && data[2 + i] == 0x00
1143 && data[3 + i] == 0xc4
1144 && data[4 + i] == 0xc4
1145 && data[5 + i] == 0x96) { /* start of frame */
1146 int lum = -1;
1147 int pkt_type = LAST_PACKET;
1148 int fr_h_sz = (sd->bridge == BRIDGE_103) ?
1149 18 : 12;
1151 if (len - i < fr_h_sz) {
1152 PDEBUG(D_STREAM, "packet too short to"
1153 " get avg brightness");
1154 } else if (sd->bridge == BRIDGE_103) {
1155 lum = data[i + 9] +
1156 (data[i + 10] << 8);
1157 } else {
1158 lum = data[i + 8] + (data[i + 9] << 8);
1160 /* When exposure changes midway a frame we
1161 get a lum of 0 in this case drop 2 frames
1162 as the frames directly after an exposure
1163 change have an unstable image. Sometimes lum
1164 *really* is 0 (cam used in low light with
1165 low exposure setting), so do not drop frames
1166 if the previous lum was 0 too. */
1167 if (lum == 0 && sd->prev_avg_lum != 0) {
1168 lum = -1;
1169 sd->frames_to_drop = 2;
1170 sd->prev_avg_lum = 0;
1171 } else
1172 sd->prev_avg_lum = lum;
1173 atomic_set(&sd->avg_lum, lum);
1175 if (sd->frames_to_drop) {
1176 sd->frames_to_drop--;
1177 pkt_type = DISCARD_PACKET;
1180 gspca_frame_add(gspca_dev, pkt_type,
1181 NULL, 0);
1182 data += i + fr_h_sz;
1183 len -= i + fr_h_sz;
1184 gspca_frame_add(gspca_dev, FIRST_PACKET,
1185 data, len);
1186 return;
1191 if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW) {
1192 /* In raw mode we sometimes get some garbage after the frame
1193 ignore this */
1194 struct gspca_frame *frame;
1195 int used;
1196 int size = cam->cam_mode[gspca_dev->curr_mode].sizeimage;
1198 frame = gspca_get_i_frame(gspca_dev);
1199 if (frame == NULL) {
1200 gspca_dev->last_packet_type = DISCARD_PACKET;
1201 return;
1203 used = frame->data_end - frame->data;
1204 if (used + len > size)
1205 len = size - used;
1208 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1211 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
1213 struct sd *sd = (struct sd *) gspca_dev;
1215 sd->brightness = val;
1216 if (gspca_dev->streaming)
1217 setbrightness(gspca_dev);
1218 return 0;
1221 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
1223 struct sd *sd = (struct sd *) gspca_dev;
1225 *val = sd->brightness;
1226 return 0;
1229 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1231 struct sd *sd = (struct sd *) gspca_dev;
1233 sd->gain = val;
1234 if (gspca_dev->streaming)
1235 setgain(gspca_dev);
1236 return 0;
1239 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1241 struct sd *sd = (struct sd *) gspca_dev;
1243 *val = sd->gain;
1244 return 0;
1247 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
1249 struct sd *sd = (struct sd *) gspca_dev;
1251 sd->exposure = val;
1252 if (gspca_dev->streaming)
1253 setexposure(gspca_dev);
1254 return 0;
1257 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
1259 struct sd *sd = (struct sd *) gspca_dev;
1261 *val = sd->exposure;
1262 return 0;
1265 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1267 struct sd *sd = (struct sd *) gspca_dev;
1269 sd->autogain = val;
1270 sd->exp_too_high_cnt = 0;
1271 sd->exp_too_low_cnt = 0;
1273 /* when switching to autogain set defaults to make sure
1274 we are on a valid point of the autogain gain /
1275 exposure knee graph, and give this change time to
1276 take effect before doing autogain. */
1277 if (sd->autogain && !(sensor_data[sd->sensor].flags & F_COARSE_EXPO)) {
1278 sd->exposure = EXPOSURE_DEF;
1279 sd->gain = GAIN_DEF;
1280 if (gspca_dev->streaming) {
1281 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
1282 setexposure(gspca_dev);
1283 setgain(gspca_dev);
1287 return 0;
1290 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1292 struct sd *sd = (struct sd *) gspca_dev;
1294 *val = sd->autogain;
1295 return 0;
1298 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
1300 struct sd *sd = (struct sd *) gspca_dev;
1302 sd->freq = val;
1303 if (gspca_dev->streaming)
1304 setfreq(gspca_dev);
1305 return 0;
1308 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
1310 struct sd *sd = (struct sd *) gspca_dev;
1312 *val = sd->freq;
1313 return 0;
1316 static int sd_querymenu(struct gspca_dev *gspca_dev,
1317 struct v4l2_querymenu *menu)
1319 switch (menu->id) {
1320 case V4L2_CID_POWER_LINE_FREQUENCY:
1321 switch (menu->index) {
1322 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1323 strcpy((char *) menu->name, "NoFliker");
1324 return 0;
1325 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1326 strcpy((char *) menu->name, "50 Hz");
1327 return 0;
1328 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
1329 strcpy((char *) menu->name, "60 Hz");
1330 return 0;
1332 break;
1334 return -EINVAL;
1337 #ifdef CONFIG_INPUT
1338 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
1339 u8 *data, /* interrupt packet data */
1340 int len) /* interrupt packet length */
1342 int ret = -EINVAL;
1344 if (len == 1 && data[0] == 1) {
1345 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
1346 input_sync(gspca_dev->input_dev);
1347 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
1348 input_sync(gspca_dev->input_dev);
1349 ret = 0;
1352 return ret;
1354 #endif
1356 /* sub-driver description */
1357 static const struct sd_desc sd_desc = {
1358 .name = MODULE_NAME,
1359 .ctrls = sd_ctrls,
1360 .nctrls = ARRAY_SIZE(sd_ctrls),
1361 .config = sd_config,
1362 .init = sd_init,
1363 .start = sd_start,
1364 .stopN = sd_stopN,
1365 .pkt_scan = sd_pkt_scan,
1366 .querymenu = sd_querymenu,
1367 .dq_callback = do_autogain,
1368 #ifdef CONFIG_INPUT
1369 .int_pkt_scan = sd_int_pkt_scan,
1370 #endif
1373 /* -- module initialisation -- */
1374 #define SB(sensor, bridge) \
1375 .driver_info = (SENSOR_ ## sensor << 8) | BRIDGE_ ## bridge
1378 static const struct usb_device_id device_table[] __devinitconst = {
1379 {USB_DEVICE(0x0c45, 0x6001), SB(TAS5110C, 102)}, /* TAS5110C1B */
1380 {USB_DEVICE(0x0c45, 0x6005), SB(TAS5110C, 101)}, /* TAS5110C1B */
1381 #if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
1382 {USB_DEVICE(0x0c45, 0x6007), SB(TAS5110D, 101)}, /* TAS5110D */
1383 {USB_DEVICE(0x0c45, 0x6009), SB(PAS106, 101)},
1384 {USB_DEVICE(0x0c45, 0x600d), SB(PAS106, 101)},
1385 #endif
1386 {USB_DEVICE(0x0c45, 0x6011), SB(OV6650, 101)},
1387 #if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
1388 {USB_DEVICE(0x0c45, 0x6019), SB(OV7630, 101)},
1389 {USB_DEVICE(0x0c45, 0x6024), SB(TAS5130CXX, 102)},
1390 {USB_DEVICE(0x0c45, 0x6025), SB(TAS5130CXX, 102)},
1391 {USB_DEVICE(0x0c45, 0x6028), SB(PAS202, 102)},
1392 {USB_DEVICE(0x0c45, 0x6029), SB(PAS106, 102)},
1393 #endif
1394 {USB_DEVICE(0x0c45, 0x602c), SB(OV7630, 102)},
1395 {USB_DEVICE(0x0c45, 0x602d), SB(HV7131R, 102)},
1396 #if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
1397 {USB_DEVICE(0x0c45, 0x602e), SB(OV7630, 102)},
1398 #endif
1399 {USB_DEVICE(0x0c45, 0x608f), SB(OV7630, 103)},
1400 #if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
1401 {USB_DEVICE(0x0c45, 0x60af), SB(PAS202, 103)},
1402 #endif
1403 {USB_DEVICE(0x0c45, 0x60b0), SB(OV7630, 103)},
1406 MODULE_DEVICE_TABLE(usb, device_table);
1408 /* -- device connect -- */
1409 static int __devinit sd_probe(struct usb_interface *intf,
1410 const struct usb_device_id *id)
1412 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1413 THIS_MODULE);
1416 static struct usb_driver sd_driver = {
1417 .name = MODULE_NAME,
1418 .id_table = device_table,
1419 .probe = sd_probe,
1420 .disconnect = gspca_disconnect,
1421 #ifdef CONFIG_PM
1422 .suspend = gspca_suspend,
1423 .resume = gspca_resume,
1424 #endif
1427 /* -- module insert / remove -- */
1428 static int __init sd_mod_init(void)
1430 int ret;
1431 ret = usb_register(&sd_driver);
1432 if (ret < 0)
1433 return ret;
1434 PDEBUG(D_PROBE, "registered");
1435 return 0;
1437 static void __exit sd_mod_exit(void)
1439 usb_deregister(&sd_driver);
1440 PDEBUG(D_PROBE, "deregistered");
1443 module_init(sd_mod_init);
1444 module_exit(sd_mod_exit);