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