V4L/DVB (8195): gspca: Input buffer overwritten in spca561 + cleanup code.
[pandora-kernel.git] / drivers / media / video / gspca / etoms.c
1 /*
2  * Etoms Et61x151 GPL Linux driver by Michel Xhaard (09/09/2004)
3  *
4  * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #define MODULE_NAME "etoms"
22
23 #include "gspca.h"
24
25 #define DRIVER_VERSION_NUMBER   KERNEL_VERSION(2, 1, 4)
26 static const char version[] = "2.1.4";
27
28 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
29 MODULE_DESCRIPTION("Etoms USB Camera Driver");
30 MODULE_LICENSE("GPL");
31
32 /* specific webcam descriptor */
33 struct sd {
34         struct gspca_dev gspca_dev;     /* !! must be the first item */
35
36         unsigned char brightness;
37         unsigned char contrast;
38         unsigned char colors;
39         unsigned char autogain;
40
41         char sensor;
42 #define SENSOR_PAS106 0
43 #define SENSOR_TAS5130CXX 1
44         signed char ag_cnt;
45 #define AG_CNT_START 13
46 };
47
48 /* V4L2 controls supported by the driver */
49 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
50 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
51 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
52 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
53 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
54 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
55 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
56 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
57
58 static struct ctrl sd_ctrls[] = {
59         {
60          {
61           .id = V4L2_CID_BRIGHTNESS,
62           .type = V4L2_CTRL_TYPE_INTEGER,
63           .name = "Brightness",
64           .minimum = 1,
65           .maximum = 127,
66           .step = 1,
67 #define BRIGHTNESS_DEF 63
68           .default_value = BRIGHTNESS_DEF,
69           },
70          .set = sd_setbrightness,
71          .get = sd_getbrightness,
72          },
73         {
74          {
75           .id = V4L2_CID_CONTRAST,
76           .type = V4L2_CTRL_TYPE_INTEGER,
77           .name = "Contrast",
78           .minimum = 0,
79           .maximum = 255,
80           .step = 1,
81 #define CONTRAST_DEF 127
82           .default_value = CONTRAST_DEF,
83           },
84          .set = sd_setcontrast,
85          .get = sd_getcontrast,
86          },
87         {
88          {
89           .id = V4L2_CID_SATURATION,
90           .type = V4L2_CTRL_TYPE_INTEGER,
91           .name = "Color",
92           .minimum = 0,
93           .maximum = 15,
94           .step = 1,
95 #define COLOR_DEF 7
96           .default_value = COLOR_DEF,
97           },
98          .set = sd_setcolors,
99          .get = sd_getcolors,
100          },
101         {
102          {
103           .id = V4L2_CID_AUTOGAIN,
104           .type = V4L2_CTRL_TYPE_BOOLEAN,
105           .name = "Auto Gain",
106           .minimum = 0,
107           .maximum = 1,
108           .step = 1,
109 #define AUTOGAIN_DEF 1
110           .default_value = AUTOGAIN_DEF,
111           },
112          .set = sd_setautogain,
113          .get = sd_getautogain,
114          },
115 };
116
117 static struct cam_mode vga_mode[] = {
118         {V4L2_PIX_FMT_SBGGR8, 320, 240, 1},
119 /*      {V4L2_PIX_FMT_SBGGR8, 640, 480, 0}, */
120 };
121
122 static struct cam_mode sif_mode[] = {
123         {V4L2_PIX_FMT_SBGGR8, 176, 144, 1},
124         {V4L2_PIX_FMT_SBGGR8, 352, 288, 0},
125 };
126
127 #define ETOMS_ALT_SIZE_1000   12
128
129 #define ET_GPIO_DIR_CTRL 0x04   /* Control IO bit[0..5] (0 in  1 out) */
130 #define ET_GPIO_OUT 0x05        /* Only IO data */
131 #define ET_GPIO_IN 0x06         /* Read Only IO data */
132 #define ET_RESET_ALL 0x03
133 #define ET_ClCK 0x01
134 #define ET_CTRL 0x02            /* enable i2c OutClck Powerdown mode */
135
136 #define ET_COMP 0x12            /* Compression register */
137 #define ET_MAXQt 0x13
138 #define ET_MINQt 0x14
139 #define ET_COMP_VAL0 0x02
140 #define ET_COMP_VAL1 0x03
141
142 #define ET_REG1d 0x1d
143 #define ET_REG1e 0x1e
144 #define ET_REG1f 0x1f
145 #define ET_REG20 0x20
146 #define ET_REG21 0x21
147 #define ET_REG22 0x22
148 #define ET_REG23 0x23
149 #define ET_REG24 0x24
150 #define ET_REG25 0x25
151 /* base registers for luma calculation */
152 #define ET_LUMA_CENTER 0x39
153
154 #define ET_G_RED 0x4d
155 #define ET_G_GREEN1 0x4e
156 #define ET_G_BLUE 0x4f
157 #define ET_G_GREEN2 0x50
158 #define ET_G_GR_H 0x51
159 #define ET_G_GB_H 0x52
160
161 #define ET_O_RED 0x34
162 #define ET_O_GREEN1 0x35
163 #define ET_O_BLUE 0x36
164 #define ET_O_GREEN2 0x37
165
166 #define ET_SYNCHRO 0x68
167 #define ET_STARTX 0x69
168 #define ET_STARTY 0x6a
169 #define ET_WIDTH_LOW 0x6b
170 #define ET_HEIGTH_LOW 0x6c
171 #define ET_W_H_HEIGTH 0x6d
172
173 #define ET_REG6e 0x6e           /* OBW */
174 #define ET_REG6f 0x6f           /* OBW */
175 #define ET_REG70 0x70           /* OBW_AWB */
176 #define ET_REG71 0x71           /* OBW_AWB */
177 #define ET_REG72 0x72           /* OBW_AWB */
178 #define ET_REG73 0x73           /* Clkdelay ns */
179 #define ET_REG74 0x74           /* test pattern */
180 #define ET_REG75 0x75           /* test pattern */
181
182 #define ET_I2C_CLK 0x8c
183 #define ET_PXL_CLK 0x60
184
185 #define ET_I2C_BASE 0x89
186 #define ET_I2C_COUNT 0x8a
187 #define ET_I2C_PREFETCH 0x8b
188 #define ET_I2C_REG 0x88
189 #define ET_I2C_DATA7 0x87
190 #define ET_I2C_DATA6 0x86
191 #define ET_I2C_DATA5 0x85
192 #define ET_I2C_DATA4 0x84
193 #define ET_I2C_DATA3 0x83
194 #define ET_I2C_DATA2 0x82
195 #define ET_I2C_DATA1 0x81
196 #define ET_I2C_DATA0 0x80
197
198 #define PAS106_REG2 0x02        /* pxlClk = systemClk/(reg2) */
199 #define PAS106_REG3 0x03        /* line/frame H [11..4] */
200 #define PAS106_REG4 0x04        /* line/frame L [3..0] */
201 #define PAS106_REG5 0x05        /* exposure time line offset(default 5) */
202 #define PAS106_REG6 0x06        /* exposure time pixel offset(default 6) */
203 #define PAS106_REG7 0x07        /* signbit Dac (default 0) */
204 #define PAS106_REG9 0x09
205 #define PAS106_REG0e 0x0e       /* global gain [4..0](default 0x0e) */
206 #define PAS106_REG13 0x13       /* end i2c write */
207
208 static const __u8 GainRGBG[] = { 0x80, 0x80, 0x80, 0x80, 0x00, 0x00 };
209
210 static const __u8 I2c2[] = { 0x08, 0x08, 0x08, 0x08, 0x0d };
211
212 static const __u8 I2c3[] = { 0x12, 0x05 };
213
214 static const __u8 I2c4[] = { 0x41, 0x08 };
215
216 static void reg_r(struct usb_device *dev,
217                        __u16 index, __u8 *buffer, int len)
218 {
219         usb_control_msg(dev,
220                         usb_rcvctrlpipe(dev, 0),
221                         0,
222                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
223                         0, index, buffer, len, 500);
224 }
225
226 static void reg_w_val(struct usb_device *dev,
227                         __u16 index, __u8 val)
228 {
229         __u8 data;
230
231         data = val;
232         usb_control_msg(dev,
233                         usb_sndctrlpipe(dev, 0),
234                         0,
235                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
236                         0, index, &data, 1, 500);
237 }
238
239 static void reg_w(struct usb_device *dev,
240                   __u16 index, const __u8 *buffer, __u16 len)
241 {
242         __u8 tmpbuf[8];
243
244         memcpy(tmpbuf, buffer, len);
245         usb_control_msg(dev,
246                         usb_sndctrlpipe(dev, 0),
247                         0,
248                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
249                         0, index, tmpbuf, len, 500);
250 }
251
252 static int Et_i2cwrite(struct usb_device *dev, __u8 reg,
253                         const __u8 *buffer,
254                         __u16 len, __u8 mode)
255 {
256         /* buffer should be [D0..D7] */
257         __u8 ptchcount;
258
259         /* set the base address */
260         reg_w_val(dev, ET_I2C_BASE, 0x40); /* sensor base for the pas106 */
261         /* set count and prefetch */
262         ptchcount = ((len & 0x07) << 4) | (mode & 0x03);
263         reg_w_val(dev, ET_I2C_COUNT, ptchcount);
264         /* set the register base */
265         reg_w_val(dev, ET_I2C_REG, reg);
266         while (--len >= 0)
267                 reg_w_val(dev, ET_I2C_DATA0 + len, buffer[len]);
268         return 0;
269 }
270
271 static int Et_i2cread(struct usb_device *dev, __u8 reg,
272                         __u8 *buffer,
273                         __u16 length, __u8 mode)
274 {
275         /* buffer should be [D0..D7] */
276         int i, j;
277         __u8 ptchcount;
278
279         /* set the base address */
280         reg_w_val(dev, ET_I2C_BASE, 0x40); /* sensor base for the pas106 */
281         /* set count and prefetch */
282         ptchcount = ((length & 0x07) << 4) | (mode & 0x03);
283         reg_w_val(dev, ET_I2C_COUNT, ptchcount);
284         /* set the register base */
285         reg_w_val(dev, ET_I2C_REG, reg);
286         reg_w_val(dev, ET_I2C_PREFETCH, 0x02);  /* prefetch */
287         reg_w_val(dev, ET_I2C_PREFETCH, 0);
288         j = length - 1;
289         for (i = 0; i < length; i++) {
290                 reg_r(dev, (ET_I2C_DATA0 + j), &buffer[j], 1);
291                 j--;
292         }
293         return 0;
294 }
295
296 static int Et_WaitStatus(struct usb_device *dev)
297 {
298         __u8 bytereceived;
299         int retry = 10;
300
301         while (retry--) {
302                 reg_r(dev, ET_ClCK, &bytereceived, 1);
303                 if (bytereceived != 0)
304                         return 1;
305         }
306         return 0;
307 }
308
309 static int et_video(struct usb_device *dev, int on)
310 {
311         int err;
312
313         reg_w_val(dev, ET_GPIO_OUT, on
314                                 ? 0x10          /* startvideo - set Bit5 */
315                                 : 0);           /* stopvideo */
316         err = Et_WaitStatus(dev);
317         if (!err)
318                 PDEBUG(D_ERR, "timeout video on/off");
319         return err;
320 }
321
322 static void Et_init2(struct gspca_dev *gspca_dev)
323 {
324         struct usb_device *dev = gspca_dev->dev;
325         __u8 value;
326         __u8 received;
327         static const __u8 FormLine[] = { 0x84, 0x03, 0x14, 0xf4, 0x01, 0x05 };
328
329         PDEBUG(D_STREAM, "Open Init2 ET");
330         reg_w_val(dev, ET_GPIO_DIR_CTRL, 0x2f);
331         reg_w_val(dev, ET_GPIO_OUT, 0x10);
332         reg_r(dev, ET_GPIO_IN, &received, 1);
333         reg_w_val(dev, ET_ClCK, 0x14);  /* 0x14 // 0x16 enabled pattern */
334         reg_w_val(dev, ET_CTRL, 0x1b);
335
336         /*  compression et subsampling */
337         if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].mode)
338                 value = ET_COMP_VAL1;   /* 320 */
339         else
340                 value = ET_COMP_VAL0;   /* 640 */
341         reg_w_val(dev, ET_COMP, value);
342         reg_w_val(dev, ET_MAXQt, 0x1f);
343         reg_w_val(dev, ET_MINQt, 0x04);
344         /* undocumented registers */
345         reg_w_val(dev, ET_REG1d, 0xff);
346         reg_w_val(dev, ET_REG1e, 0xff);
347         reg_w_val(dev, ET_REG1f, 0xff);
348         reg_w_val(dev, ET_REG20, 0x35);
349         reg_w_val(dev, ET_REG21, 0x01);
350         reg_w_val(dev, ET_REG22, 0x00);
351         reg_w_val(dev, ET_REG23, 0xff);
352         reg_w_val(dev, ET_REG24, 0xff);
353         reg_w_val(dev, ET_REG25, 0x0f);
354         /* colors setting */
355         reg_w_val(dev, 0x30, 0x11);             /* 0x30 */
356         reg_w_val(dev, 0x31, 0x40);
357         reg_w_val(dev, 0x32, 0x00);
358         reg_w_val(dev, ET_O_RED, 0x00);         /* 0x34 */
359         reg_w_val(dev, ET_O_GREEN1, 0x00);
360         reg_w_val(dev, ET_O_BLUE, 0x00);
361         reg_w_val(dev, ET_O_GREEN2, 0x00);
362         /*************/
363         reg_w_val(dev, ET_G_RED, 0x80);         /* 0x4d */
364         reg_w_val(dev, ET_G_GREEN1, 0x80);
365         reg_w_val(dev, ET_G_BLUE, 0x80);
366         reg_w_val(dev, ET_G_GREEN2, 0x80);
367         reg_w_val(dev, ET_G_GR_H, 0x00);
368         reg_w_val(dev, ET_G_GB_H, 0x00);                /* 0x52 */
369         /* Window control registers */
370         reg_w_val(dev, 0x61, 0x80);             /* use cmc_out */
371         reg_w_val(dev, 0x62, 0x02);
372         reg_w_val(dev, 0x63, 0x03);
373         reg_w_val(dev, 0x64, 0x14);
374         reg_w_val(dev, 0x65, 0x0e);
375         reg_w_val(dev, 0x66, 0x02);
376         reg_w_val(dev, 0x67, 0x02);
377
378         /**************************************/
379         reg_w_val(dev, ET_SYNCHRO, 0x8f);       /* 0x68 */
380         reg_w_val(dev, ET_STARTX, 0x69);                /* 0x6a //0x69 */
381         reg_w_val(dev, ET_STARTY, 0x0d);                /* 0x0d //0x0c */
382         reg_w_val(dev, ET_WIDTH_LOW, 0x80);
383         reg_w_val(dev, ET_HEIGTH_LOW, 0xe0);
384         reg_w_val(dev, ET_W_H_HEIGTH, 0x60);    /* 6d */
385         reg_w_val(dev, ET_REG6e, 0x86);
386         reg_w_val(dev, ET_REG6f, 0x01);
387         reg_w_val(dev, ET_REG70, 0x26);
388         reg_w_val(dev, ET_REG71, 0x7a);
389         reg_w_val(dev, ET_REG72, 0x01);
390         /* Clock Pattern registers ***************** */
391         reg_w_val(dev, ET_REG73, 0x00);
392         reg_w_val(dev, ET_REG74, 0x18);         /* 0x28 */
393         reg_w_val(dev, ET_REG75, 0x0f);         /* 0x01 */
394         /**********************************************/
395         reg_w_val(dev, 0x8a, 0x20);
396         reg_w_val(dev, 0x8d, 0x0f);
397         reg_w_val(dev, 0x8e, 0x08);
398         /**************************************/
399         reg_w_val(dev, 0x03, 0x08);
400         reg_w_val(dev, ET_PXL_CLK, 0x03);
401         reg_w_val(dev, 0x81, 0xff);
402         reg_w_val(dev, 0x80, 0x00);
403         reg_w_val(dev, 0x81, 0xff);
404         reg_w_val(dev, 0x80, 0x20);
405         reg_w_val(dev, 0x03, 0x01);
406         reg_w_val(dev, 0x03, 0x00);
407         reg_w_val(dev, 0x03, 0x08);
408         /********************************************/
409
410 /*      reg_r(dev, ET_I2C_BASE, &received, 1);
411                                          always 0x40 as the pas106 ??? */
412         /* set the sensor */
413         if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].mode)
414                 value = 0x04;           /* 320 */
415         else                            /* 640 */
416                 value = 0x1e;   /* 0x17  * setting PixelClock
417                                          * 0x03 mean 24/(3+1) = 6 Mhz
418                                          * 0x05 -> 24/(5+1) = 4 Mhz
419                                          * 0x0b -> 24/(11+1) = 2 Mhz
420                                          * 0x17 -> 24/(23+1) = 1 Mhz
421                                          */
422         reg_w_val(dev, ET_PXL_CLK, value);
423         /* now set by fifo the FormatLine setting */
424         reg_w(dev, 0x62, FormLine, 6);
425
426         /* set exposure times [ 0..0x78] 0->longvalue 0x78->shortvalue */
427         reg_w_val(dev, 0x81, 0x47);             /* 0x47; */
428         reg_w_val(dev, 0x80, 0x40);             /* 0x40; */
429         /* Pedro change */
430         /* Brightness change Brith+ decrease value */
431         /* Brigth- increase value */
432         /* original value = 0x70; */
433         reg_w_val(dev, 0x81, 0x30);             /* 0x20; - set brightness */
434         reg_w_val(dev, 0x80, 0x20);             /* 0x20; */
435 }
436
437 static void setcolors(struct gspca_dev *gspca_dev)
438 {
439         struct sd *sd = (struct sd *) gspca_dev;
440         struct usb_device *dev = gspca_dev->dev;
441         __u8 I2cc[] = { 0x05, 0x02, 0x02, 0x05, 0x0d };
442         __u8 i2cflags = 0x01;
443         /* __u8 green = 0; */
444         __u8 colors = sd->colors;
445
446         I2cc[3] = colors;       /* red */
447         I2cc[0] = 15 - colors;  /* blue */
448         /* green = 15 - ((((7*I2cc[0]) >> 2 ) + I2cc[3]) >> 1); */
449         /* I2cc[1] = I2cc[2] = green; */
450         if (sd->sensor == SENSOR_PAS106) {
451                 Et_i2cwrite(dev, PAS106_REG13, &i2cflags, 1, 3);
452                 Et_i2cwrite(dev, PAS106_REG9, I2cc, sizeof I2cc, 1);
453         }
454 /*      PDEBUG(D_CONF , "Etoms red %d blue %d green %d",
455                 I2cc[3], I2cc[0], green); */
456 }
457
458 static void getcolors(struct gspca_dev *gspca_dev)
459 {
460         struct sd *sd = (struct sd *) gspca_dev;
461 /*      __u8 valblue; */
462         __u8 valred;
463
464         if (sd->sensor == SENSOR_PAS106) {
465 /*              Et_i2cread(gspca_dev->dev, PAS106_REG9, &valblue, 1, 1); */
466                 Et_i2cread(gspca_dev->dev, PAS106_REG9 + 3, &valred, 1, 1);
467                 sd->colors = valred & 0x0f;
468         }
469 }
470
471 static void Et_init1(struct gspca_dev *gspca_dev)
472 {
473         struct usb_device *dev = gspca_dev->dev;
474         __u8 value;
475         __u8 received;
476 /*      __u8 I2c0 [] = {0x0a, 0x12, 0x05, 0x22, 0xac, 0x00, 0x01, 0x00}; */
477         __u8 I2c0[] = { 0x0a, 0x12, 0x05, 0x6d, 0xcd, 0x00, 0x01, 0x00 };
478                                                 /* try 1/120 0x6d 0xcd 0x40 */
479 /*      __u8 I2c0 [] = {0x0a, 0x12, 0x05, 0xfe, 0xfe, 0xc0, 0x01, 0x00};
480                                                  * 1/60000 hmm ?? */
481
482         PDEBUG(D_STREAM, "Open Init1 ET");
483         reg_w_val(dev, ET_GPIO_DIR_CTRL, 7);
484         reg_r(dev, ET_GPIO_IN, &received, 1);
485         reg_w_val(dev, ET_RESET_ALL, 1);
486         reg_w_val(dev, ET_RESET_ALL, 0);
487         reg_w_val(dev, ET_ClCK, 0x10);
488         reg_w_val(dev, ET_CTRL, 0x19);
489         /*   compression et subsampling */
490         if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].mode)
491                 value = ET_COMP_VAL1;
492         else
493                 value = ET_COMP_VAL0;
494         PDEBUG(D_STREAM, "Open mode %d Compression %d",
495                gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].mode,
496                value);
497         reg_w_val(dev, ET_COMP, value);
498         reg_w_val(dev, ET_MAXQt, 0x1d);
499         reg_w_val(dev, ET_MINQt, 0x02);
500         /* undocumented registers */
501         reg_w_val(dev, ET_REG1d, 0xff);
502         reg_w_val(dev, ET_REG1e, 0xff);
503         reg_w_val(dev, ET_REG1f, 0xff);
504         reg_w_val(dev, ET_REG20, 0x35);
505         reg_w_val(dev, ET_REG21, 0x01);
506         reg_w_val(dev, ET_REG22, 0x00);
507         reg_w_val(dev, ET_REG23, 0xf7);
508         reg_w_val(dev, ET_REG24, 0xff);
509         reg_w_val(dev, ET_REG25, 0x07);
510         /* colors setting */
511         reg_w_val(dev, ET_G_RED, 0x80);
512         reg_w_val(dev, ET_G_GREEN1, 0x80);
513         reg_w_val(dev, ET_G_BLUE, 0x80);
514         reg_w_val(dev, ET_G_GREEN2, 0x80);
515         reg_w_val(dev, ET_G_GR_H, 0x00);
516         reg_w_val(dev, ET_G_GB_H, 0x00);
517         /* Window control registers */
518         reg_w_val(dev, ET_SYNCHRO, 0xf0);
519         reg_w_val(dev, ET_STARTX, 0x56);                /* 0x56 */
520         reg_w_val(dev, ET_STARTY, 0x05);                /* 0x04 */
521         reg_w_val(dev, ET_WIDTH_LOW, 0x60);
522         reg_w_val(dev, ET_HEIGTH_LOW, 0x20);
523         reg_w_val(dev, ET_W_H_HEIGTH, 0x50);
524         reg_w_val(dev, ET_REG6e, 0x86);
525         reg_w_val(dev, ET_REG6f, 0x01);
526         reg_w_val(dev, ET_REG70, 0x86);
527         reg_w_val(dev, ET_REG71, 0x14);
528         reg_w_val(dev, ET_REG72, 0x00);
529         /* Clock Pattern registers */
530         reg_w_val(dev, ET_REG73, 0x00);
531         reg_w_val(dev, ET_REG74, 0x00);
532         reg_w_val(dev, ET_REG75, 0x0a);
533         reg_w_val(dev, ET_I2C_CLK, 0x04);
534         reg_w_val(dev, ET_PXL_CLK, 0x01);
535         /* set the sensor */
536         if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].mode) {
537                 I2c0[0] = 0x06;
538                 Et_i2cwrite(dev, PAS106_REG2, I2c0, sizeof I2c0, 1);
539                 Et_i2cwrite(dev, PAS106_REG9, I2c2, sizeof I2c2, 1);
540                 value = 0x06;
541                 Et_i2cwrite(dev, PAS106_REG2, &value, 1, 1);
542                 Et_i2cwrite(dev, PAS106_REG3, I2c3, sizeof I2c3, 1);
543                 /* value = 0x1f; */
544                 value = 0x04;
545                 Et_i2cwrite(dev, PAS106_REG0e, &value, 1, 1);
546         } else {
547                 I2c0[0] = 0x0a;
548
549                 Et_i2cwrite(dev, PAS106_REG2, I2c0, sizeof I2c0, 1);
550                 Et_i2cwrite(dev, PAS106_REG9, I2c2, sizeof I2c2, 1);
551                 value = 0x0a;
552
553                 Et_i2cwrite(dev, PAS106_REG2, &value, 1, 1);
554                 Et_i2cwrite(dev, PAS106_REG3, I2c3, sizeof I2c3, 1);
555                 value = 0x04;
556                 /* value = 0x10; */
557                 Et_i2cwrite(dev, PAS106_REG0e, &value, 1, 1);
558                 /* bit 2 enable bit 1:2 select 0 1 2 3
559                    value = 0x07;                                * curve 0 *
560                    Et_i2cwrite(dev,PAS106_REG0f,&value,1,1);
561                  */
562         }
563
564 /*      value = 0x01; */
565 /*      value = 0x22; */
566 /*      Et_i2cwrite(dev, PAS106_REG5, &value, 1, 1); */
567         /* magnetude and sign bit for DAC */
568         Et_i2cwrite(dev, PAS106_REG7, I2c4, sizeof I2c4, 1);
569         /* now set by fifo the whole colors setting */
570         reg_w(dev, ET_G_RED, GainRGBG, 6);
571         getcolors(gspca_dev);
572         setcolors(gspca_dev);
573 }
574
575 /* this function is called at probe time */
576 static int sd_config(struct gspca_dev *gspca_dev,
577                      const struct usb_device_id *id)
578 {
579         struct sd *sd = (struct sd *) gspca_dev;
580         struct cam *cam;
581         __u16 vendor;
582         __u16 product;
583
584         vendor = id->idVendor;
585         product = id->idProduct;
586 /*      switch (vendor) { */
587 /*      case 0x102c:            * Etoms */
588                 switch (product) {
589                 case 0x6151:
590                         sd->sensor = SENSOR_PAS106;     /* Etoms61x151 */
591                         break;
592                 case 0x6251:
593                         sd->sensor = SENSOR_TAS5130CXX; /* Etoms61x251 */
594                         break;
595 /*              } */
596 /*              break; */
597         }
598         cam = &gspca_dev->cam;
599         cam->dev_name = (char *) id->driver_info;
600         cam->epaddr = 1;
601         if (sd->sensor == SENSOR_PAS106) {
602                 cam->cam_mode = sif_mode;
603                 cam->nmodes = sizeof sif_mode / sizeof sif_mode[0];
604         } else {
605                 cam->cam_mode = vga_mode;
606                 cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
607         }
608         sd->brightness = BRIGHTNESS_DEF;
609         sd->contrast = CONTRAST_DEF;
610         sd->colors = COLOR_DEF;
611         sd->autogain = AUTOGAIN_DEF;
612         return 0;
613 }
614
615 /* this function is called at open time */
616 static int sd_open(struct gspca_dev *gspca_dev)
617 {
618         struct sd *sd = (struct sd *) gspca_dev;
619         struct usb_device *dev = gspca_dev->dev;
620
621         if (sd->sensor == SENSOR_PAS106)
622                 Et_init1(gspca_dev);
623         else
624                 Et_init2(gspca_dev);
625         reg_w_val(dev, ET_RESET_ALL, 0x08);
626         et_video(dev, 0);               /* video off */
627         return 0;
628 }
629
630 /* -- start the camera -- */
631 static void sd_start(struct gspca_dev *gspca_dev)
632 {
633         struct sd *sd = (struct sd *) gspca_dev;
634         struct usb_device *dev = gspca_dev->dev;
635
636         if (sd->sensor == SENSOR_PAS106)
637                 Et_init1(gspca_dev);
638         else
639                 Et_init2(gspca_dev);
640
641         reg_w_val(dev, ET_RESET_ALL, 0x08);
642         et_video(dev, 1);               /* video on */
643 }
644
645 static void sd_stopN(struct gspca_dev *gspca_dev)
646 {
647         et_video(gspca_dev->dev, 0);    /* video off */
648 }
649
650 static void sd_stop0(struct gspca_dev *gspca_dev)
651 {
652 }
653
654 static void sd_close(struct gspca_dev *gspca_dev)
655 {
656 }
657
658 static void setbrightness(struct gspca_dev *gspca_dev)
659 {
660         struct sd *sd = (struct sd *) gspca_dev;
661         int i;
662         __u8 brightness = sd->brightness;
663
664         for (i = 0; i < 4; i++)
665                 reg_w_val(gspca_dev->dev, (ET_O_RED + i), brightness);
666 }
667
668 static void getbrightness(struct gspca_dev *gspca_dev)
669 {
670         struct sd *sd = (struct sd *) gspca_dev;
671         int i;
672         int brightness = 0;
673         __u8 value;
674
675         for (i = 0; i < 4; i++) {
676                 reg_r(gspca_dev->dev, (ET_O_RED + i), &value, 1);
677                 brightness += value;
678         }
679         sd->brightness = brightness >> 3;
680 }
681
682 static void setcontrast(struct gspca_dev *gspca_dev)
683 {
684         struct sd *sd = (struct sd *) gspca_dev;
685         __u8 RGBG[] = { 0x80, 0x80, 0x80, 0x80, 0x00, 0x00 };
686         __u8 contrast = sd->contrast;
687
688         memset(RGBG, contrast, sizeof RGBG - 2);
689         reg_w(gspca_dev->dev, ET_G_RED, RGBG, 6);
690 }
691
692 static void getcontrast(struct gspca_dev *gspca_dev)
693 {
694         struct sd *sd = (struct sd *) gspca_dev;
695         int i;
696         int contrast = 0;
697         __u8 value = 0;
698
699         for (i = 0; i < 4; i++) {
700                 reg_r(gspca_dev->dev, (ET_G_RED + i), &value, 1);
701                 contrast += value;
702         }
703         sd->contrast = contrast >> 2;
704 }
705
706 static __u8 Et_getgainG(struct gspca_dev *gspca_dev)
707 {
708         struct sd *sd = (struct sd *) gspca_dev;
709         __u8 value = 0;
710
711         if (sd->sensor == SENSOR_PAS106) {
712                 Et_i2cread(gspca_dev->dev, PAS106_REG0e, &value, 1, 1);
713                 PDEBUG(D_CONF, "Etoms gain G %d", value);
714                 return value;
715         }
716         return 0x1f;
717 }
718
719 static void Et_setgainG(struct gspca_dev *gspca_dev, __u8 gain)
720 {
721         struct sd *sd = (struct sd *) gspca_dev;
722         struct usb_device *dev = gspca_dev->dev;
723         __u8 i2cflags = 0x01;
724
725         if (sd->sensor == SENSOR_PAS106) {
726                 Et_i2cwrite(dev, PAS106_REG13, &i2cflags, 1, 3);
727                 Et_i2cwrite(dev, PAS106_REG0e, &gain, 1, 1);
728         }
729 }
730
731 #define BLIMIT(bright) \
732         (__u8)((bright > 0x1f)?0x1f:((bright < 4)?3:bright))
733 #define LIMIT(color) \
734         (unsigned char)((color > 0xff)?0xff:((color < 0)?0:color))
735
736 static void setautogain(struct gspca_dev *gspca_dev)
737 {
738         struct usb_device *dev = gspca_dev->dev;
739         __u8 GRBG[] = { 0, 0, 0, 0 };
740         __u8 luma = 0;
741         __u8 luma_mean = 128;
742         __u8 luma_delta = 20;
743         __u8 spring = 4;
744         int Gbright = 0;
745         __u8 r, g, b;
746
747         Gbright = Et_getgainG(gspca_dev);
748         reg_r(dev, ET_LUMA_CENTER, GRBG, 4);
749         g = (GRBG[0] + GRBG[3]) >> 1;
750         r = GRBG[1];
751         b = GRBG[2];
752         r = ((r << 8) - (r << 4) - (r << 3)) >> 10;
753         b = ((b << 7) >> 10);
754         g = ((g << 9) + (g << 7) + (g << 5)) >> 10;
755         luma = LIMIT(r + g + b);
756         PDEBUG(D_FRAM, "Etoms luma G %d", luma);
757         if (luma < luma_mean - luma_delta || luma > luma_mean + luma_delta) {
758                 Gbright += (luma_mean - luma) >> spring;
759                 Gbright = BLIMIT(Gbright);
760                 PDEBUG(D_FRAM, "Etoms Gbright %d", Gbright);
761                 Et_setgainG(gspca_dev, (__u8) Gbright);
762         }
763 }
764
765 #undef BLIMIT
766 #undef LIMIT
767
768 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
769                         struct gspca_frame *frame,      /* target */
770                         __u8 *data,                     /* isoc packet */
771                         int len)                        /* iso packet length */
772 {
773         struct sd *sd;
774         int seqframe;
775
776         seqframe = data[0] & 0x3f;
777         len = (int) (((data[0] & 0xc0) << 2) | data[1]);
778         if (seqframe == 0x3f) {
779                 PDEBUG(D_FRAM,
780                        "header packet found datalength %d !!", len);
781                 PDEBUG(D_FRAM, "G %d R %d G %d B %d",
782                        data[2], data[3], data[4], data[5]);
783                 data += 30;
784                 /* don't change datalength as the chips provided it */
785                 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
786                                         data, 0);
787                 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, data, len);
788                 sd = (struct sd *) gspca_dev;
789                 if (sd->ag_cnt >= 0) {
790                         if (--sd->ag_cnt < 0) {
791                                 sd->ag_cnt = AG_CNT_START;
792                                 setautogain(gspca_dev);
793                         }
794                 }
795                 return;
796         }
797         if (len) {
798                 data += 8;
799                 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
800         } else {                        /* Drop Packet */
801                 gspca_dev->last_packet_type = DISCARD_PACKET;
802         }
803 }
804
805 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
806 {
807         struct sd *sd = (struct sd *) gspca_dev;
808
809         sd->brightness = val;
810         if (gspca_dev->streaming)
811                 setbrightness(gspca_dev);
812         return 0;
813 }
814
815 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
816 {
817         struct sd *sd = (struct sd *) gspca_dev;
818
819         getbrightness(gspca_dev);
820         *val = sd->brightness;
821         return 0;
822 }
823
824 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
825 {
826         struct sd *sd = (struct sd *) gspca_dev;
827
828         sd->contrast = val;
829         if (gspca_dev->streaming)
830                 setcontrast(gspca_dev);
831         return 0;
832 }
833
834 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
835 {
836         struct sd *sd = (struct sd *) gspca_dev;
837
838         getcontrast(gspca_dev);
839         *val = sd->contrast;
840         return 0;
841 }
842
843 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
844 {
845         struct sd *sd = (struct sd *) gspca_dev;
846
847         sd->colors = val;
848         if (gspca_dev->streaming)
849                 setcolors(gspca_dev);
850         return 0;
851 }
852
853 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
854 {
855         struct sd *sd = (struct sd *) gspca_dev;
856
857         getcolors(gspca_dev);
858         *val = sd->colors;
859         return 0;
860 }
861
862 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
863 {
864         struct sd *sd = (struct sd *) gspca_dev;
865
866         sd->autogain = val;
867         if (val)
868                 sd->ag_cnt = AG_CNT_START;
869         else
870                 sd->ag_cnt = -1;
871         return 0;
872 }
873
874 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
875 {
876         struct sd *sd = (struct sd *) gspca_dev;
877
878         *val = sd->autogain;
879         return 0;
880 }
881
882 /* sub-driver description */
883 static struct sd_desc sd_desc = {
884         .name = MODULE_NAME,
885         .ctrls = sd_ctrls,
886         .nctrls = ARRAY_SIZE(sd_ctrls),
887         .config = sd_config,
888         .open = sd_open,
889         .start = sd_start,
890         .stopN = sd_stopN,
891         .stop0 = sd_stop0,
892         .close = sd_close,
893         .pkt_scan = sd_pkt_scan,
894 };
895
896 /* -- module initialisation -- */
897 #define DVNM(name) .driver_info = (kernel_ulong_t) name
898 static __devinitdata struct usb_device_id device_table[] = {
899         {USB_DEVICE(0x102c, 0x6151), DVNM("Qcam Sangha CIF")},
900         {USB_DEVICE(0x102c, 0x6251), DVNM("Qcam xxxxxx VGA")},
901         {}
902 };
903
904 MODULE_DEVICE_TABLE(usb, device_table);
905
906 /* -- device connect -- */
907 static int sd_probe(struct usb_interface *intf,
908                     const struct usb_device_id *id)
909 {
910         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
911                                THIS_MODULE);
912 }
913
914 static struct usb_driver sd_driver = {
915         .name = MODULE_NAME,
916         .id_table = device_table,
917         .probe = sd_probe,
918         .disconnect = gspca_disconnect,
919 };
920
921 /* -- module insert / remove -- */
922 static int __init sd_mod_init(void)
923 {
924         if (usb_register(&sd_driver) < 0)
925                 return -1;
926         PDEBUG(D_PROBE, "v%s registered", version);
927         return 0;
928 }
929
930 static void __exit sd_mod_exit(void)
931 {
932         usb_deregister(&sd_driver);
933         PDEBUG(D_PROBE, "deregistered");
934 }
935
936 module_init(sd_mod_init);
937 module_exit(sd_mod_exit);