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