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
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:
26 0x10 high nibble red gain low nibble blue gain
27 0x11 low nibble green gain
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>
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 */
54 struct gspca_dev gspca_dev; /* !! must be the first item */
60 unsigned short exposure;
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 */
70 #define BRIDGE_102 0 /* We make no difference between 101 and 102 */
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
85 typedef const __u8 sensor_init_t[8];
88 const __u8 *bridge_init[2];
89 int bridge_init_size[2];
90 sensor_init_t *sensor_init;
92 sensor_init_t *sensor_bridge_init[2];
93 int sensor_bridge_init_size[2];
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) | \
111 #define NO_FREQ (1 << FREQ_IDX)
112 #define NO_BRIGHTNESS (1 << BRIGHTNESS_IDX)
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 ??*/
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",
163 #define BRIGHTNESS_DEF 127
164 .default_value = BRIGHTNESS_DEF,
166 .set = sd_setbrightness,
167 .get = sd_getbrightness,
173 .type = V4L2_CTRL_TYPE_INTEGER,
179 #define GAIN_KNEE 230
180 .default_value = GAIN_DEF,
185 #define EXPOSURE_IDX 2
188 .id = V4L2_CID_EXPOSURE,
189 .type = V4L2_CTRL_TYPE_INTEGER,
191 #define EXPOSURE_DEF 66 /* 33 ms / 30 fps (except on PASXXX) */
192 #define EXPOSURE_KNEE 200 /* 100 ms / 10 fps (except on PASXXX) */
196 .default_value = EXPOSURE_DEF,
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,
208 #define COARSE_EXPOSURE_DEF 2 /* 30 fps */
212 .default_value = COARSE_EXPOSURE_DEF,
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)",
227 #define AUTOGAIN_DEF 1
228 .default_value = AUTOGAIN_DEF,
231 .set = sd_setautogain,
232 .get = sd_getautogain,
237 .id = V4L2_CID_POWER_LINE_FREQUENCY,
238 .type = V4L2_CTRL_TYPE_MENU,
239 .name = "Light frequency filter",
241 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
244 .default_value = FREQ_DEF,
251 static const struct v4l2_pix_format vga_mode[] = {
252 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
254 .sizeimage = 160 * 120,
255 .colorspace = V4L2_COLORSPACE_SRGB,
256 .priv = 2 | MODE_RAW},
257 {160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
259 .sizeimage = 160 * 120 * 5 / 4,
260 .colorspace = V4L2_COLORSPACE_SRGB,
262 {320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
264 .sizeimage = 320 * 240 * 5 / 4,
265 .colorspace = V4L2_COLORSPACE_SRGB,
267 {640, 480, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
269 .sizeimage = 640 * 480 * 5 / 4,
270 .colorspace = V4L2_COLORSPACE_SRGB,
273 static const struct v4l2_pix_format sif_mode[] = {
274 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
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,
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,
286 .sizeimage = 176 * 144,
287 .colorspace = V4L2_COLORSPACE_SRGB,
288 .priv = 1 | MODE_RAW},
289 {176, 144, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
291 .sizeimage = 176 * 144 * 5 / 4,
292 .colorspace = V4L2_COLORSPACE_SRGB,
294 {320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
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,
301 .sizeimage = 352 * 288 * 5 / 4,
302 .colorspace = V4L2_COLORSPACE_SRGB,
306 static const __u8 initHv7131[] = {
307 0x46, 0x77, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 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] = {
327 /* Bright, contrast, etc are set through SCBB interface.
328 * AVCAP on win2 do not send any data on this controls. */
329 /* Anyway, some registers appears to alter bright and constrat */
332 {0xa0, 0x60, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
333 /* Set clock register 0x11 low nibble is clock divider */
334 {0xd0, 0x60, 0x11, 0xc0, 0x1b, 0x18, 0xc1, 0x10},
335 /* Next some unknown stuff */
336 {0xb0, 0x60, 0x15, 0x00, 0x02, 0x18, 0xc1, 0x10},
337 /* {0xa0, 0x60, 0x1b, 0x01, 0x02, 0x18, 0xc1, 0x10},
338 * THIS SET GREEN SCREEN
339 * (pixels could be innverted in decode kind of "brg",
340 * but blue wont be there. Avoid this data ... */
341 {0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10}, /* format out? */
342 {0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10},
343 {0xa0, 0x60, 0x30, 0x3d, 0x0a, 0xd8, 0xa4, 0x10},
344 /* Enable rgb brightness control */
345 {0xa0, 0x60, 0x61, 0x08, 0x00, 0x00, 0x00, 0x10},
346 /* HDG: Note windows uses the line below, which sets both register 0x60
347 and 0x61 I believe these registers of the ov6650 are identical as
348 those of the ov7630, because if this is true the windows settings
349 add a bit additional red gain and a lot additional blue gain, which
350 matches my findings that the windows settings make blue much too
351 blue and red a little too red.
352 {0xb0, 0x60, 0x60, 0x66, 0x68, 0xd8, 0xa4, 0x10}, */
353 /* Some more unknown stuff */
354 {0xa0, 0x60, 0x68, 0x04, 0x68, 0xd8, 0xa4, 0x10},
355 {0xd0, 0x60, 0x17, 0x24, 0xd6, 0x04, 0x94, 0x10}, /* Clipreg */
358 static const __u8 initOv7630[] = {
359 0x04, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, /* r01 .. r08 */
360 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* r09 .. r10 */
361 0x00, 0x01, 0x01, 0x0a, /* r11 .. r14 */
362 0x28, 0x1e, /* H & V sizes r15 .. r16 */
363 0x68, COMP2, MCK_INIT1, /* r17 .. r19 */
364 0x1d, 0x10, 0x02, 0x03, 0x0f, 0x0c /* r1a .. r1f */
366 static const __u8 initOv7630_3[] = {
367 0x44, 0x44, 0x00, 0x1a, 0x20, 0x20, 0x20, 0x80, /* r01 .. r08 */
368 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* r09 .. r10 */
369 0x00, 0x02, 0x01, 0x0a, /* r11 .. r14 */
370 0x28, 0x1e, /* H & V sizes r15 .. r16 */
371 0x68, 0x8f, MCK_INIT1, /* r17 .. r19 */
372 0x1d, 0x10, 0x02, 0x03, 0x0f, 0x0c, 0x00, /* r1a .. r20 */
373 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, /* r21 .. r28 */
374 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0xff /* r29 .. r30 */
376 static const __u8 ov7630_sensor_init[][8] = {
377 {0xa0, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
378 {0xb0, 0x21, 0x01, 0x77, 0x3a, 0x00, 0x00, 0x10},
379 /* {0xd0, 0x21, 0x12, 0x7c, 0x01, 0x80, 0x34, 0x10}, jfm */
380 {0xd0, 0x21, 0x12, 0x1c, 0x00, 0x80, 0x34, 0x10}, /* jfm */
381 {0xa0, 0x21, 0x1b, 0x04, 0x00, 0x80, 0x34, 0x10},
382 {0xa0, 0x21, 0x20, 0x44, 0x00, 0x80, 0x34, 0x10},
383 {0xa0, 0x21, 0x23, 0xee, 0x00, 0x80, 0x34, 0x10},
384 {0xd0, 0x21, 0x26, 0xa0, 0x9a, 0xa0, 0x30, 0x10},
385 {0xb0, 0x21, 0x2a, 0x80, 0x00, 0xa0, 0x30, 0x10},
386 {0xb0, 0x21, 0x2f, 0x3d, 0x24, 0xa0, 0x30, 0x10},
387 {0xa0, 0x21, 0x32, 0x86, 0x24, 0xa0, 0x30, 0x10},
388 {0xb0, 0x21, 0x60, 0xa9, 0x4a, 0xa0, 0x30, 0x10},
389 /* {0xb0, 0x21, 0x60, 0xa9, 0x42, 0xa0, 0x30, 0x10}, * jfm */
390 {0xa0, 0x21, 0x65, 0x00, 0x42, 0xa0, 0x30, 0x10},
391 {0xa0, 0x21, 0x69, 0x38, 0x42, 0xa0, 0x30, 0x10},
392 {0xc0, 0x21, 0x6f, 0x88, 0x0b, 0x00, 0x30, 0x10},
393 {0xc0, 0x21, 0x74, 0x21, 0x8e, 0x00, 0x30, 0x10},
394 {0xa0, 0x21, 0x7d, 0xf7, 0x8e, 0x00, 0x30, 0x10},
395 {0xd0, 0x21, 0x17, 0x1c, 0xbd, 0x06, 0xf6, 0x10},
398 static const __u8 ov7630_sensor_init_3[][8] = {
399 {0xa0, 0x21, 0x13, 0x80, 0x00, 0x00, 0x00, 0x10},
402 static const __u8 initPas106[] = {
403 0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x40, 0x00, 0x00, 0x00,
405 0x00, 0x00, 0x00, 0x04, 0x01, 0x00,
406 0x16, 0x12, 0x24, COMP1, MCK_INIT1,
407 0x18, 0x10, 0x02, 0x02, 0x09, 0x07
409 /* compression 0x86 mckinit1 0x2b */
411 /* "Known" PAS106B registers:
413 0x03 Variable framerate bits 4-11
414 0x04 Var framerate bits 0-3, one must leave the 4 msb's at 0 !!
415 The variable framerate control must never be set lower then 300,
416 which sets the framerate at 90 / reg02, otherwise vsync is lost.
417 0x05 Shutter Time Line Offset, this can be used as an exposure control:
418 0 = use full frame time, 255 = no exposure at all
419 Note this may never be larger then "var-framerate control" / 2 - 2.
420 When var-framerate control is < 514, no exposure is reached at the max
421 allowed value for the framerate control value, rather then at 255.
422 0x06 Shutter Time Pixel Offset, like reg05 this influences exposure, but
423 only a very little bit, leave at 0xcd
424 0x07 offset sign bit (bit0 1 > negative offset)
431 0x13 Write 1 to commit settings to sensor
434 static const __u8 pas106_sensor_init[][8] = {
435 /* Pixel Clock Divider 6 */
436 { 0xa1, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x14 },
437 /* Frame Time MSB (also seen as 0x12) */
438 { 0xa1, 0x40, 0x03, 0x13, 0x00, 0x00, 0x00, 0x14 },
439 /* Frame Time LSB (also seen as 0x05) */
440 { 0xa1, 0x40, 0x04, 0x06, 0x00, 0x00, 0x00, 0x14 },
441 /* Shutter Time Line Offset (also seen as 0x6d) */
442 { 0xa1, 0x40, 0x05, 0x65, 0x00, 0x00, 0x00, 0x14 },
443 /* Shutter Time Pixel Offset (also seen as 0xb1) */
444 { 0xa1, 0x40, 0x06, 0xcd, 0x00, 0x00, 0x00, 0x14 },
445 /* Black Level Subtract Sign (also seen 0x00) */
446 { 0xa1, 0x40, 0x07, 0xc1, 0x00, 0x00, 0x00, 0x14 },
447 /* Black Level Subtract Level (also seen 0x01) */
448 { 0xa1, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x14 },
449 { 0xa1, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x14 },
450 /* Color Gain B Pixel 5 a */
451 { 0xa1, 0x40, 0x09, 0x05, 0x00, 0x00, 0x00, 0x14 },
452 /* Color Gain G1 Pixel 1 5 */
453 { 0xa1, 0x40, 0x0a, 0x04, 0x00, 0x00, 0x00, 0x14 },
454 /* Color Gain G2 Pixel 1 0 5 */
455 { 0xa1, 0x40, 0x0b, 0x04, 0x00, 0x00, 0x00, 0x14 },
456 /* Color Gain R Pixel 3 1 */
457 { 0xa1, 0x40, 0x0c, 0x05, 0x00, 0x00, 0x00, 0x14 },
458 /* Color GainH Pixel */
459 { 0xa1, 0x40, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x14 },
461 { 0xa1, 0x40, 0x0e, 0x0e, 0x00, 0x00, 0x00, 0x14 },
463 { 0xa1, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x14 },
464 /* H&V synchro polarity */
465 { 0xa1, 0x40, 0x10, 0x06, 0x00, 0x00, 0x00, 0x14 },
467 { 0xa1, 0x40, 0x11, 0x06, 0x00, 0x00, 0x00, 0x14 },
469 { 0xa1, 0x40, 0x12, 0x06, 0x00, 0x00, 0x00, 0x14 },
471 { 0xa1, 0x40, 0x14, 0x02, 0x00, 0x00, 0x00, 0x14 },
472 /* Validate Settings */
473 { 0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14 },
476 static const __u8 initPas202[] = {
477 0x44, 0x44, 0x21, 0x30, 0x00, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0x00,
479 0x00, 0x00, 0x00, 0x06, 0x03, 0x0a,
480 0x28, 0x1e, 0x20, 0x89, 0x20,
481 0x00, 0x00, 0x02, 0x03, 0x0f, 0x0c
484 /* "Known" PAS202BCB registers:
486 0x04 Variable framerate bits 6-11 (*)
487 0x05 Var framerate bits 0-5, one must leave the 2 msb's at 0 !!
491 0x0b offset sign bit (bit0 1 > negative offset)
493 0x0e Unknown image is slightly brighter when bit 0 is 0, if reg0f is 0 too,
494 leave at 1 otherwise we get a jump in our exposure control
495 0x0f Exposure 0-255, 0 = use full frame time, 255 = no exposure at all
496 0x10 Master gain 0 - 31
497 0x11 write 1 to apply changes
498 (*) The variable framerate control must never be set lower then 500
499 which sets the framerate at 30 / reg02, otherwise vsync is lost.
501 static const __u8 pas202_sensor_init[][8] = {
502 /* Set the clock divider to 4 -> 30 / 4 = 7.5 fps, we would like
503 to set it lower, but for some reason the bridge starts missing
505 {0xa0, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x10},
506 {0xd0, 0x40, 0x04, 0x07, 0x34, 0x00, 0x09, 0x10},
507 {0xd0, 0x40, 0x08, 0x01, 0x00, 0x00, 0x01, 0x10},
508 {0xd0, 0x40, 0x0c, 0x00, 0x0c, 0x01, 0x32, 0x10},
509 {0xd0, 0x40, 0x10, 0x00, 0x01, 0x00, 0x63, 0x10},
510 {0xa0, 0x40, 0x15, 0x70, 0x01, 0x00, 0x63, 0x10},
511 {0xa0, 0x40, 0x18, 0x00, 0x01, 0x00, 0x63, 0x10},
512 {0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
513 {0xa0, 0x40, 0x03, 0x56, 0x01, 0x00, 0x63, 0x10},
514 {0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
517 static const __u8 initTas5110c[] = {
518 0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
520 0x00, 0x00, 0x00, 0x45, 0x09, 0x0a,
521 0x16, 0x12, 0x60, 0x86, 0x2b,
522 0x14, 0x0a, 0x02, 0x02, 0x09, 0x07
524 /* Same as above, except a different hstart */
525 static const __u8 initTas5110d[] = {
526 0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
528 0x00, 0x00, 0x00, 0x41, 0x09, 0x0a,
529 0x16, 0x12, 0x60, 0x86, 0x2b,
530 0x14, 0x0a, 0x02, 0x02, 0x09, 0x07
532 static const __u8 tas5110_sensor_init[][8] = {
533 {0x30, 0x11, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x10},
534 {0x30, 0x11, 0x02, 0x20, 0xa9, 0x00, 0x00, 0x10},
535 {0xa0, 0x61, 0x9a, 0xca, 0x00, 0x00, 0x00, 0x17},
538 static const __u8 initTas5130[] = {
539 0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
541 0x00, 0x00, 0x00, 0x68, 0x0c, 0x0a,
542 0x28, 0x1e, 0x60, COMP, MCK_INIT,
543 0x18, 0x10, 0x04, 0x03, 0x11, 0x0c
545 static const __u8 tas5130_sensor_init[][8] = {
546 /* {0x30, 0x11, 0x00, 0x40, 0x47, 0x00, 0x00, 0x10},
547 * shutter 0x47 short exposure? */
548 {0x30, 0x11, 0x00, 0x40, 0x01, 0x00, 0x00, 0x10},
549 /* shutter 0x01 long exposure */
550 {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10},
553 static struct sensor_data sensor_data[] = {
554 SENS(initHv7131, NULL, hv7131_sensor_init, NULL, NULL, 0, NO_EXPO|NO_FREQ, 0),
555 SENS(initOv6650, NULL, ov6650_sensor_init, NULL, NULL, F_GAIN|F_SIF, 0, 0x60),
556 SENS(initOv7630, initOv7630_3, ov7630_sensor_init, NULL, ov7630_sensor_init_3,
558 SENS(initPas106, NULL, pas106_sensor_init, NULL, NULL, F_GAIN|F_SIF, NO_FREQ,
560 SENS(initPas202, initPas202, pas202_sensor_init, NULL, NULL, F_GAIN,
562 SENS(initTas5110c, NULL, tas5110_sensor_init, NULL, NULL,
563 F_GAIN|F_SIF|F_COARSE_EXPO, NO_BRIGHTNESS|NO_FREQ, 0),
564 SENS(initTas5110d, NULL, tas5110_sensor_init, NULL, NULL,
565 F_GAIN|F_SIF|F_COARSE_EXPO, NO_BRIGHTNESS|NO_FREQ, 0),
566 SENS(initTas5130, NULL, tas5130_sensor_init, NULL, NULL, 0, NO_EXPO|NO_FREQ,
570 /* get one byte in gspca_dev->usb_buf */
571 static void reg_r(struct gspca_dev *gspca_dev,
574 usb_control_msg(gspca_dev->dev,
575 usb_rcvctrlpipe(gspca_dev->dev, 0),
577 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
580 gspca_dev->usb_buf, 1,
584 static void reg_w(struct gspca_dev *gspca_dev,
590 if (len > USB_BUF_SZ) {
591 PDEBUG(D_ERR|D_PACK, "reg_w: buffer overflow");
595 memcpy(gspca_dev->usb_buf, buffer, len);
596 usb_control_msg(gspca_dev->dev,
597 usb_sndctrlpipe(gspca_dev->dev, 0),
599 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
602 gspca_dev->usb_buf, len,
606 static int i2c_w(struct gspca_dev *gspca_dev, const __u8 *buffer)
611 reg_w(gspca_dev, 0x08, buffer, 8);
614 reg_r(gspca_dev, 0x08);
615 if (gspca_dev->usb_buf[0] & 0x04) {
616 if (gspca_dev->usb_buf[0] & 0x08)
624 static void i2c_w_vector(struct gspca_dev *gspca_dev,
625 const __u8 buffer[][8], int len)
628 reg_w(gspca_dev, 0x08, *buffer, 8);
636 static void setbrightness(struct gspca_dev *gspca_dev)
638 struct sd *sd = (struct sd *) gspca_dev;
641 switch (sd->sensor) {
643 case SENSOR_OV7630: {
645 {0xa0, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10};
647 /* change reg 0x06 */
648 i2cOV[1] = sensor_data[sd->sensor].sensor_addr;
649 i2cOV[3] = sd->brightness;
650 if (i2c_w(gspca_dev, i2cOV) < 0)
655 case SENSOR_PAS202: {
657 {0xb0, 0x40, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x16};
659 {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
661 /* PAS106 uses reg 7 and 8 instead of b and c */
662 if (sd->sensor == SENSOR_PAS106) {
667 if (sd->brightness < 127) {
668 /* change reg 0x0b, signreg */
669 i2cpbright[3] = 0x01;
670 /* set reg 0x0c, offset */
671 i2cpbright[4] = 127 - sd->brightness;
673 i2cpbright[4] = sd->brightness - 127;
675 if (i2c_w(gspca_dev, i2cpbright) < 0)
677 if (i2c_w(gspca_dev, i2cpdoit) < 0)
681 case SENSOR_TAS5130CXX: {
683 {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};
685 value = 0xff - sd->brightness;
687 PDEBUG(D_CONF, "brightness %d : %d", value, i2c[4]);
688 if (i2c_w(gspca_dev, i2c) < 0)
695 PDEBUG(D_ERR, "i2c error brightness");
698 static void setsensorgain(struct gspca_dev *gspca_dev)
700 struct sd *sd = (struct sd *) gspca_dev;
701 unsigned char gain = sd->gain;
703 switch (sd->sensor) {
705 case SENSOR_TAS5110C:
706 case SENSOR_TAS5110D: {
708 {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};
711 if (i2c_w(gspca_dev, i2c) < 0)
719 case SENSOR_OV7630: {
720 __u8 i2c[] = {0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
722 i2c[1] = sensor_data[sd->sensor].sensor_addr;
724 if (i2c_w(gspca_dev, i2c) < 0)
729 case SENSOR_PAS202: {
731 {0xa0, 0x40, 0x10, 0x00, 0x00, 0x00, 0x00, 0x15};
732 __u8 i2cpcolorgain[] =
733 {0xc0, 0x40, 0x07, 0x00, 0x00, 0x00, 0x00, 0x15};
735 {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
737 /* PAS106 uses different regs (and has split green gains) */
738 if (sd->sensor == SENSOR_PAS106) {
740 i2cpcolorgain[0] = 0xd0;
741 i2cpcolorgain[2] = 0x09;
745 i2cpgain[3] = sd->gain >> 3;
746 i2cpcolorgain[3] = sd->gain >> 4;
747 i2cpcolorgain[4] = sd->gain >> 4;
748 i2cpcolorgain[5] = sd->gain >> 4;
749 i2cpcolorgain[6] = sd->gain >> 4;
751 if (i2c_w(gspca_dev, i2cpgain) < 0)
753 if (i2c_w(gspca_dev, i2cpcolorgain) < 0)
755 if (i2c_w(gspca_dev, i2cpdoit) < 0)
762 PDEBUG(D_ERR, "i2c error gain");
765 static void setgain(struct gspca_dev *gspca_dev)
767 struct sd *sd = (struct sd *) gspca_dev;
769 __u8 buf[2] = { 0, 0 };
771 if (sensor_data[sd->sensor].flags & F_GAIN) {
772 /* Use the sensor gain to do the actual gain */
773 setsensorgain(gspca_dev);
777 gain = sd->gain >> 4;
779 /* red and blue gain */
780 buf[0] = gain << 4 | gain;
783 reg_w(gspca_dev, 0x10, buf, 2);
786 static void setexposure(struct gspca_dev *gspca_dev)
788 struct sd *sd = (struct sd *) gspca_dev;
790 switch (sd->sensor) {
791 case SENSOR_TAS5110C:
792 case SENSOR_TAS5110D: {
793 /* register 19's high nibble contains the sn9c10x clock divider
794 The high nibble configures the no fps according to the
795 formula: 60 / high_nibble. With a maximum of 30 fps */
796 __u8 reg = sd->exposure;
797 reg = (reg << 4) | 0x0b;
798 reg_w(gspca_dev, 0x19, ®, 1);
802 case SENSOR_OV7630: {
803 /* The ov6650 / ov7630 have 2 registers which both influence
804 exposure, register 11, whose low nibble sets the nr off fps
805 according to: fps = 30 / (low_nibble + 1)
807 The fps configures the maximum exposure setting, but it is
808 possible to use less exposure then what the fps maximum
809 allows by setting register 10. register 10 configures the
810 actual exposure as quotient of the full exposure, with 0
811 being no exposure at all (not very usefull) and reg10_max
812 being max exposure possible at that framerate.
814 The code maps our 0 - 510 ms exposure ctrl to these 2
815 registers, trying to keep fps as high as possible.
817 __u8 i2c[] = {0xb0, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10};
818 int reg10, reg11, reg10_max;
820 /* ov6645 datasheet says reg10_max is 9a, but that uses
821 tline * 2 * reg10 as formula for calculating texpo, the
822 ov6650 probably uses the same formula as the 7730 which uses
823 tline * 4 * reg10, which explains why the reg10max we've
824 found experimentally for the ov6650 is exactly half that of
825 the ov6645. The ov7630 datasheet says the max is 0x41. */
826 if (sd->sensor == SENSOR_OV6650) {
828 i2c[4] = 0xc0; /* OV6650 needs non default vsync pol */
832 reg11 = (15 * sd->exposure + 999) / 1000;
838 /* In 640x480, if the reg11 has less than 4, the image is
839 unstable (the bridge goes into a higher compression mode
840 which we have not reverse engineered yet). */
841 if (gspca_dev->width == 640 && reg11 < 4)
844 /* frame exposure time in ms = 1000 * reg11 / 30 ->
845 reg10 = (sd->exposure / 2) * reg10_max / (1000 * reg11 / 30) */
846 reg10 = (sd->exposure * 15 * reg10_max) / (1000 * reg11);
848 /* Don't allow this to get below 10 when using autogain, the
849 steps become very large (relatively) when below 10 causing
850 the image to oscilate from much too dark, to much too bright
852 if (sd->autogain && reg10 < 10)
854 else if (reg10 > reg10_max)
857 /* Write reg 10 and reg11 low nibble */
858 i2c[1] = sensor_data[sd->sensor].sensor_addr;
862 /* If register 11 didn't change, don't change it */
863 if (sd->reg11 == reg11)
866 if (i2c_w(gspca_dev, i2c) == 0)
872 case SENSOR_PAS202: {
873 __u8 i2cpframerate[] =
874 {0xb0, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x16};
876 {0xa0, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x16};
877 const __u8 i2cpdoit[] =
878 {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
881 /* The exposure knee for the autogain algorithm is 200
882 (100 ms / 10 fps on other sensors), for values below this
883 use the control for setting the partial frame expose time,
884 above that use variable framerate. This way we run at max
885 framerate (640x480@7.5 fps, 320x240@10fps) until the knee
886 is reached. Using the variable framerate control above 200
887 is better then playing around with both clockdiv + partial
888 frame exposure times (like we are doing with the ov chips),
889 as that sometimes leads to jumps in the exposure control,
890 which are bad for auto exposure. */
891 if (sd->exposure < 200) {
892 i2cpexpo[3] = 255 - (sd->exposure * 255) / 200;
893 framerate_ctrl = 500;
895 /* The PAS202's exposure control goes from 0 - 4095,
896 but anything below 500 causes vsync issues, so scale
897 our 200-1023 to 500-4095 */
898 framerate_ctrl = (sd->exposure - 200) * 1000 / 229 +
902 i2cpframerate[3] = framerate_ctrl >> 6;
903 i2cpframerate[4] = framerate_ctrl & 0x3f;
904 if (i2c_w(gspca_dev, i2cpframerate) < 0)
906 if (i2c_w(gspca_dev, i2cpexpo) < 0)
908 if (i2c_w(gspca_dev, i2cpdoit) < 0)
912 case SENSOR_PAS106: {
913 __u8 i2cpframerate[] =
914 {0xb1, 0x40, 0x03, 0x00, 0x00, 0x00, 0x00, 0x14};
916 {0xa1, 0x40, 0x05, 0x00, 0x00, 0x00, 0x00, 0x14};
917 const __u8 i2cpdoit[] =
918 {0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14};
921 /* For values below 150 use partial frame exposure, above
922 that use framerate ctrl */
923 if (sd->exposure < 150) {
924 i2cpexpo[3] = 150 - sd->exposure;
925 framerate_ctrl = 300;
927 /* The PAS106's exposure control goes from 0 - 4095,
928 but anything below 300 causes vsync issues, so scale
929 our 150-1023 to 300-4095 */
930 framerate_ctrl = (sd->exposure - 150) * 1000 / 230 +
934 i2cpframerate[3] = framerate_ctrl >> 4;
935 i2cpframerate[4] = framerate_ctrl & 0x0f;
936 if (i2c_w(gspca_dev, i2cpframerate) < 0)
938 if (i2c_w(gspca_dev, i2cpexpo) < 0)
940 if (i2c_w(gspca_dev, i2cpdoit) < 0)
947 PDEBUG(D_ERR, "i2c error exposure");
950 static void setfreq(struct gspca_dev *gspca_dev)
952 struct sd *sd = (struct sd *) gspca_dev;
954 switch (sd->sensor) {
956 case SENSOR_OV7630: {
957 /* Framerate adjust register for artificial light 50 hz flicker
958 compensation, for the ov6650 this is identical to ov6630
959 0x2b register, see ov6630 datasheet.
960 0x4f / 0x8a -> (30 fps -> 25 fps), 0x00 -> no adjustment */
961 __u8 i2c[] = {0xa0, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10};
964 /* case 0: * no filter*/
965 /* case 2: * 60 hz */
969 i2c[3] = (sd->sensor == SENSOR_OV6650)
973 i2c[1] = sensor_data[sd->sensor].sensor_addr;
974 if (i2c_w(gspca_dev, i2c) < 0)
975 PDEBUG(D_ERR, "i2c error setfreq");
981 #include "coarse_expo_autogain.h"
983 static void do_autogain(struct gspca_dev *gspca_dev)
985 int deadzone, desired_avg_lum, result;
986 struct sd *sd = (struct sd *) gspca_dev;
987 int avg_lum = atomic_read(&sd->avg_lum);
989 if (avg_lum == -1 || !sd->autogain)
992 if (sd->autogain_ignore_frames > 0) {
993 sd->autogain_ignore_frames--;
997 /* SIF / VGA sensors have a different autoexposure area and thus
998 different avg_lum values for the same picture brightness */
999 if (sensor_data[sd->sensor].flags & F_SIF) {
1001 /* SIF sensors tend to overexpose, so keep this small */
1002 desired_avg_lum = 5000;
1005 desired_avg_lum = 18000;
1008 if (sensor_data[sd->sensor].flags & F_COARSE_EXPO)
1009 result = gspca_coarse_grained_expo_autogain(gspca_dev, avg_lum,
1010 sd->brightness * desired_avg_lum / 127,
1013 result = gspca_auto_gain_n_exposure(gspca_dev, avg_lum,
1014 sd->brightness * desired_avg_lum / 127,
1015 deadzone, GAIN_KNEE, EXPOSURE_KNEE);
1018 PDEBUG(D_FRAM, "autogain: gain changed: gain: %d expo: %d",
1019 (int)sd->gain, (int)sd->exposure);
1020 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
1024 /* this function is called at probe time */
1025 static int sd_config(struct gspca_dev *gspca_dev,
1026 const struct usb_device_id *id)
1028 struct sd *sd = (struct sd *) gspca_dev;
1031 reg_r(gspca_dev, 0x00);
1032 if (gspca_dev->usb_buf[0] != 0x10)
1035 /* copy the webcam info from the device id */
1036 sd->sensor = id->driver_info >> 8;
1037 sd->bridge = id->driver_info & 0xff;
1038 gspca_dev->ctrl_dis = sensor_data[sd->sensor].ctrl_dis;
1040 cam = &gspca_dev->cam;
1041 if (!(sensor_data[sd->sensor].flags & F_SIF)) {
1042 cam->cam_mode = vga_mode;
1043 cam->nmodes = ARRAY_SIZE(vga_mode);
1045 cam->cam_mode = sif_mode;
1046 cam->nmodes = ARRAY_SIZE(sif_mode);
1048 cam->npkt = 36; /* 36 packets per ISOC message */
1050 sd->brightness = BRIGHTNESS_DEF;
1051 sd->gain = GAIN_DEF;
1052 if (sensor_data[sd->sensor].flags & F_COARSE_EXPO) {
1053 sd->exposure = COARSE_EXPOSURE_DEF;
1054 gspca_dev->ctrl_dis |= (1 << EXPOSURE_IDX);
1056 sd->exposure = EXPOSURE_DEF;
1057 gspca_dev->ctrl_dis |= (1 << COARSE_EXPOSURE_IDX);
1059 if (gspca_dev->ctrl_dis & (1 << AUTOGAIN_IDX))
1060 sd->autogain = 0; /* Disable do_autogain callback */
1062 sd->autogain = AUTOGAIN_DEF;
1063 sd->freq = FREQ_DEF;
1068 /* this function is called at probe and resume time */
1069 static int sd_init(struct gspca_dev *gspca_dev)
1071 const __u8 stop = 0x09; /* Disable stream turn of LED */
1073 reg_w(gspca_dev, 0x01, &stop, 1);
1078 /* -- start the camera -- */
1079 static int sd_start(struct gspca_dev *gspca_dev)
1081 struct sd *sd = (struct sd *) gspca_dev;
1082 struct cam *cam = &gspca_dev->cam;
1084 const __u8 *sn9c10x;
1087 mode = cam->cam_mode[gspca_dev->curr_mode].priv & 0x07;
1088 sn9c10x = sensor_data[sd->sensor].bridge_init[sd->bridge];
1089 l = sensor_data[sd->sensor].bridge_init_size[sd->bridge];
1090 memcpy(reg12_19, &sn9c10x[0x12 - 1], 8);
1091 reg12_19[6] = sn9c10x[0x18 - 1] | (mode << 4);
1092 /* Special cases where reg 17 and or 19 value depends on mode */
1093 switch (sd->sensor) {
1094 case SENSOR_TAS5130CXX:
1095 /* probably not mode specific at all most likely the upper
1096 nibble of 0x19 is exposure (clock divider) just as with
1097 the tas5110, we need someone to test this. */
1098 reg12_19[7] = mode ? 0x23 : 0x43;
1101 /* Disable compression when the raw bayer format has been selected */
1102 if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW)
1103 reg12_19[6] &= ~0x80;
1105 /* Vga mode emulation on SIF sensor? */
1106 if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_REDUCED_SIF) {
1107 reg12_19[0] += 16; /* 0x12: hstart adjust */
1108 reg12_19[1] += 24; /* 0x13: vstart adjust */
1109 reg12_19[3] = 320 / 16; /* 0x15: hsize */
1110 reg12_19[4] = 240 / 16; /* 0x16: vsize */
1113 /* reg 0x01 bit 2 video transfert on */
1114 reg_w(gspca_dev, 0x01, &sn9c10x[0x01 - 1], 1);
1115 /* reg 0x17 SensorClk enable inv Clk 0x60 */
1116 reg_w(gspca_dev, 0x17, &sn9c10x[0x17 - 1], 1);
1117 /* Set the registers from the template */
1118 reg_w(gspca_dev, 0x01, sn9c10x, l);
1120 /* Init the sensor */
1121 i2c_w_vector(gspca_dev, sensor_data[sd->sensor].sensor_init,
1122 sensor_data[sd->sensor].sensor_init_size);
1123 if (sensor_data[sd->sensor].sensor_bridge_init[sd->bridge])
1124 i2c_w_vector(gspca_dev,
1125 sensor_data[sd->sensor].sensor_bridge_init[sd->bridge],
1126 sensor_data[sd->sensor].sensor_bridge_init_size[
1129 /* Mode specific sensor setup */
1130 switch (sd->sensor) {
1131 case SENSOR_PAS202: {
1132 const __u8 i2cpclockdiv[] =
1133 {0xa0, 0x40, 0x02, 0x03, 0x00, 0x00, 0x00, 0x10};
1134 /* clockdiv from 4 to 3 (7.5 -> 10 fps) when in low res mode */
1136 i2c_w(gspca_dev, i2cpclockdiv);
1139 /* H_size V_size 0x28, 0x1e -> 640x480. 0x16, 0x12 -> 352x288 */
1140 reg_w(gspca_dev, 0x15, ®12_19[3], 2);
1141 /* compression register */
1142 reg_w(gspca_dev, 0x18, ®12_19[6], 1);
1144 reg_w(gspca_dev, 0x12, ®12_19[0], 1);
1146 reg_w(gspca_dev, 0x13, ®12_19[1], 1);
1147 /* reset 0x17 SensorClk enable inv Clk 0x60 */
1148 /*fixme: ov7630 [17]=68 8f (+20 if 102)*/
1149 reg_w(gspca_dev, 0x17, ®12_19[5], 1);
1150 /*MCKSIZE ->3 */ /*fixme: not ov7630*/
1151 reg_w(gspca_dev, 0x19, ®12_19[7], 1);
1152 /* AE_STRX AE_STRY AE_ENDX AE_ENDY */
1153 reg_w(gspca_dev, 0x1c, &sn9c10x[0x1c - 1], 4);
1154 /* Enable video transfert */
1155 reg_w(gspca_dev, 0x01, &sn9c10x[0], 1);
1157 reg_w(gspca_dev, 0x18, ®12_19[6], 2);
1163 setbrightness(gspca_dev);
1164 setexposure(gspca_dev);
1167 sd->frames_to_drop = 0;
1168 sd->autogain_ignore_frames = 0;
1169 sd->exp_too_high_cnt = 0;
1170 sd->exp_too_low_cnt = 0;
1171 atomic_set(&sd->avg_lum, -1);
1175 static void sd_stopN(struct gspca_dev *gspca_dev)
1180 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1181 u8 *data, /* isoc packet */
1182 int len) /* iso packet length */
1185 struct sd *sd = (struct sd *) gspca_dev;
1186 struct cam *cam = &gspca_dev->cam;
1188 /* frames start with:
1189 * ff ff 00 c4 c4 96 synchro
1191 * xx (frame sequence / size / compression)
1192 * (xx) (idem - extra byte for sn9c103)
1193 * ll mm brightness sum inside auto exposure
1194 * ll mm brightness sum outside auto exposure
1195 * (xx xx xx xx xx) audio values for snc103
1197 if (len > 6 && len < 24) {
1198 for (i = 0; i < len - 6; i++) {
1199 if (data[0 + i] == 0xff
1200 && data[1 + i] == 0xff
1201 && data[2 + i] == 0x00
1202 && data[3 + i] == 0xc4
1203 && data[4 + i] == 0xc4
1204 && data[5 + i] == 0x96) { /* start of frame */
1206 int pkt_type = LAST_PACKET;
1207 int fr_h_sz = (sd->bridge == BRIDGE_103) ?
1210 if (len - i < fr_h_sz) {
1211 PDEBUG(D_STREAM, "packet too short to"
1212 " get avg brightness");
1213 } else if (sd->bridge == BRIDGE_103) {
1215 (data[i + 10] << 8);
1217 lum = data[i + 8] + (data[i + 9] << 8);
1219 /* When exposure changes midway a frame we
1220 get a lum of 0 in this case drop 2 frames
1221 as the frames directly after an exposure
1222 change have an unstable image. Sometimes lum
1223 *really* is 0 (cam used in low light with
1224 low exposure setting), so do not drop frames
1225 if the previous lum was 0 too. */
1226 if (lum == 0 && sd->prev_avg_lum != 0) {
1228 sd->frames_to_drop = 2;
1229 sd->prev_avg_lum = 0;
1231 sd->prev_avg_lum = lum;
1232 atomic_set(&sd->avg_lum, lum);
1234 if (sd->frames_to_drop) {
1235 sd->frames_to_drop--;
1236 pkt_type = DISCARD_PACKET;
1239 gspca_frame_add(gspca_dev, pkt_type,
1241 data += i + fr_h_sz;
1243 gspca_frame_add(gspca_dev, FIRST_PACKET,
1250 if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW) {
1251 /* In raw mode we sometimes get some garbage after the frame
1254 int size = cam->cam_mode[gspca_dev->curr_mode].sizeimage;
1256 used = gspca_dev->image_len;
1257 if (used + len > size)
1261 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1264 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
1266 struct sd *sd = (struct sd *) gspca_dev;
1268 sd->brightness = val;
1269 if (gspca_dev->streaming)
1270 setbrightness(gspca_dev);
1274 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
1276 struct sd *sd = (struct sd *) gspca_dev;
1278 *val = sd->brightness;
1282 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1284 struct sd *sd = (struct sd *) gspca_dev;
1287 if (gspca_dev->streaming)
1292 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1294 struct sd *sd = (struct sd *) gspca_dev;
1300 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
1302 struct sd *sd = (struct sd *) gspca_dev;
1305 if (gspca_dev->streaming)
1306 setexposure(gspca_dev);
1310 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
1312 struct sd *sd = (struct sd *) gspca_dev;
1314 *val = sd->exposure;
1318 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1320 struct sd *sd = (struct sd *) gspca_dev;
1323 sd->exp_too_high_cnt = 0;
1324 sd->exp_too_low_cnt = 0;
1326 /* when switching to autogain set defaults to make sure
1327 we are on a valid point of the autogain gain /
1328 exposure knee graph, and give this change time to
1329 take effect before doing autogain. */
1330 if (sd->autogain && !(sensor_data[sd->sensor].flags & F_COARSE_EXPO)) {
1331 sd->exposure = EXPOSURE_DEF;
1332 sd->gain = GAIN_DEF;
1333 if (gspca_dev->streaming) {
1334 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
1335 setexposure(gspca_dev);
1343 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1345 struct sd *sd = (struct sd *) gspca_dev;
1347 *val = sd->autogain;
1351 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
1353 struct sd *sd = (struct sd *) gspca_dev;
1356 if (gspca_dev->streaming)
1361 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
1363 struct sd *sd = (struct sd *) gspca_dev;
1369 static int sd_querymenu(struct gspca_dev *gspca_dev,
1370 struct v4l2_querymenu *menu)
1373 case V4L2_CID_POWER_LINE_FREQUENCY:
1374 switch (menu->index) {
1375 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1376 strcpy((char *) menu->name, "NoFliker");
1378 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1379 strcpy((char *) menu->name, "50 Hz");
1381 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
1382 strcpy((char *) menu->name, "60 Hz");
1390 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
1391 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
1392 u8 *data, /* interrupt packet data */
1393 int len) /* interrupt packet length */
1397 if (len == 1 && data[0] == 1) {
1398 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
1399 input_sync(gspca_dev->input_dev);
1400 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
1401 input_sync(gspca_dev->input_dev);
1409 /* sub-driver description */
1410 static const struct sd_desc sd_desc = {
1411 .name = MODULE_NAME,
1413 .nctrls = ARRAY_SIZE(sd_ctrls),
1414 .config = sd_config,
1418 .pkt_scan = sd_pkt_scan,
1419 .querymenu = sd_querymenu,
1420 .dq_callback = do_autogain,
1421 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
1422 .int_pkt_scan = sd_int_pkt_scan,
1426 /* -- module initialisation -- */
1427 #define SB(sensor, bridge) \
1428 .driver_info = (SENSOR_ ## sensor << 8) | BRIDGE_ ## bridge
1431 static const struct usb_device_id device_table[] __devinitconst = {
1432 {USB_DEVICE(0x0c45, 0x6001), SB(TAS5110C, 102)}, /* TAS5110C1B */
1433 {USB_DEVICE(0x0c45, 0x6005), SB(TAS5110C, 101)}, /* TAS5110C1B */
1434 {USB_DEVICE(0x0c45, 0x6007), SB(TAS5110D, 101)}, /* TAS5110D */
1435 {USB_DEVICE(0x0c45, 0x6009), SB(PAS106, 101)},
1436 {USB_DEVICE(0x0c45, 0x600d), SB(PAS106, 101)},
1437 {USB_DEVICE(0x0c45, 0x6011), SB(OV6650, 101)},
1438 #if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
1439 {USB_DEVICE(0x0c45, 0x6019), SB(OV7630, 101)},
1440 {USB_DEVICE(0x0c45, 0x6024), SB(TAS5130CXX, 102)},
1441 {USB_DEVICE(0x0c45, 0x6025), SB(TAS5130CXX, 102)},
1443 {USB_DEVICE(0x0c45, 0x6028), SB(PAS202, 102)},
1444 {USB_DEVICE(0x0c45, 0x6029), SB(PAS106, 102)},
1445 {USB_DEVICE(0x0c45, 0x602c), SB(OV7630, 102)},
1446 {USB_DEVICE(0x0c45, 0x602d), SB(HV7131R, 102)},
1447 {USB_DEVICE(0x0c45, 0x602e), SB(OV7630, 102)},
1448 {USB_DEVICE(0x0c45, 0x608f), SB(OV7630, 103)},
1449 #if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
1450 {USB_DEVICE(0x0c45, 0x60af), SB(PAS202, 103)},
1452 {USB_DEVICE(0x0c45, 0x60b0), SB(OV7630, 103)},
1455 MODULE_DEVICE_TABLE(usb, device_table);
1457 /* -- device connect -- */
1458 static int __devinit sd_probe(struct usb_interface *intf,
1459 const struct usb_device_id *id)
1461 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1465 static struct usb_driver sd_driver = {
1466 .name = MODULE_NAME,
1467 .id_table = device_table,
1469 .disconnect = gspca_disconnect,
1471 .suspend = gspca_suspend,
1472 .resume = gspca_resume,
1476 /* -- module insert / remove -- */
1477 static int __init sd_mod_init(void)
1479 return usb_register(&sd_driver);
1481 static void __exit sd_mod_exit(void)
1483 usb_deregister(&sd_driver);
1486 module_init(sd_mod_init);
1487 module_exit(sd_mod_exit);