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