ed7a8f9c0d7a3d8a4d7679ecd58899c5859ff409
[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, 3)
26 static const char version[] = "2.1.3";
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 #define SD_BRIGHTNESS 0
60         {
61          {
62           .id = V4L2_CID_BRIGHTNESS,
63           .type = V4L2_CTRL_TYPE_INTEGER,
64           .name = "Brightness",
65           .minimum = 1,
66           .maximum = 127,
67           .step = 1,
68           .default_value = 63,
69           },
70          .set = sd_setbrightness,
71          .get = sd_getbrightness,
72          },
73 #define SD_CONTRAST 1
74         {
75          {
76           .id = V4L2_CID_CONTRAST,
77           .type = V4L2_CTRL_TYPE_INTEGER,
78           .name = "Contrast",
79           .minimum = 0,
80           .maximum = 255,
81           .step = 1,
82           .default_value = 127,
83           },
84          .set = sd_setcontrast,
85          .get = sd_getcontrast,
86          },
87 #define SD_COLOR 2
88         {
89          {
90           .id = V4L2_CID_SATURATION,
91           .type = V4L2_CTRL_TYPE_INTEGER,
92           .name = "Color",
93           .minimum = 0,
94           .maximum = 15,
95           .step = 1,
96           .default_value = 7,
97           },
98          .set = sd_setcolors,
99          .get = sd_getcolors,
100          },
101 #define SD_AUTOGAIN 3
102         {
103          {
104           .id = V4L2_CID_AUTOGAIN,
105           .type = V4L2_CTRL_TYPE_BOOLEAN,
106           .name = "Auto Gain",
107           .minimum = 0,
108           .maximum = 1,
109           .step = 1,
110           .default_value = 1,
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 __u8 GainRGBG[] = { 0x80, 0x80, 0x80, 0x80, 0x00, 0x00 };
209
210 static __u8 I2c2[] = { 0x08, 0x08, 0x08, 0x08, 0x0d };
211
212 static __u8 I2c3[] = { 0x12, 0x05 };
213
214 static __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(struct usb_device *dev,
227                         __u16 index, __u8 *buffer, __u16 len)
228 {
229         __u8 tmpbuf[8];
230
231         memcpy(tmpbuf, buffer, len);
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, tmpbuf, len, 500);
237 }
238
239 static int Et_i2cwrite(struct usb_device *dev, __u8 reg, __u8 *buffer,
240                        __u16 length, __u8 mode)
241 {
242 /* buffer should be [D0..D7] */
243         int i, j;
244         __u8 base = 0x40;       /* sensor base for the pas106 */
245         __u8 ptchcount = 0;
246
247         ptchcount = (((length & 0x07) << 4) | (mode & 0x03));
248 /* set the base address */
249         reg_w(dev, ET_I2C_BASE, &base, 1);
250 /* set count and prefetch */
251         reg_w(dev, ET_I2C_COUNT, &ptchcount, 1);
252 /* set the register base */
253         reg_w(dev, ET_I2C_REG, &reg, 1);
254         j = length - 1;
255         for (i = 0; i < length; i++) {
256                 reg_w(dev, (ET_I2C_DATA0 + j), &buffer[j], 1);
257                 j--;
258         }
259         return 0;
260 }
261
262 static int Et_i2cread(struct usb_device *dev, __u8 reg, __u8 *buffer,
263                       __u16 length, __u8 mode)
264 {
265 /* buffer should be [D0..D7] */
266         int i, j;
267         __u8 base = 0x40;       /* sensor base for the pas106 */
268         __u8 ptchcount;
269         __u8 prefetch = 0x02;
270
271         ptchcount = (((length & 0x07) << 4) | (mode & 0x03));
272 /* set the base address */
273         reg_w(dev, ET_I2C_BASE, &base, 1);
274 /* set count and prefetch */
275         reg_w(dev, ET_I2C_COUNT, &ptchcount, 1);
276 /* set the register base */
277         reg_w(dev, ET_I2C_REG, &reg, 1);
278         reg_w(dev, ET_I2C_PREFETCH, &prefetch, 1);
279         prefetch = 0x00;
280         reg_w(dev, ET_I2C_PREFETCH, &prefetch, 1);
281         j = length - 1;
282         for (i = 0; i < length; i++) {
283                 reg_r(dev, (ET_I2C_DATA0 + j), &buffer[j], 1);
284                 j--;
285         }
286         return 0;
287 }
288
289 static int Et_WaitStatus(struct usb_device *dev)
290 {
291         __u8 bytereceived;
292         int retry = 10;
293
294         while (retry--) {
295                 reg_r(dev, ET_ClCK, &bytereceived, 1);
296                 if (bytereceived != 0)
297                         return 1;
298         }
299         return 0;
300 }
301
302 static int Et_videoOff(struct usb_device *dev)
303 {
304         int err;
305         __u8 stopvideo = 0;
306
307         reg_w(dev, ET_GPIO_OUT, &stopvideo, 1);
308         err = Et_WaitStatus(dev);
309         if (!err)
310                 PDEBUG(D_ERR, "timeout Et_waitStatus VideoON");
311         return err;
312 }
313
314 static int Et_videoOn(struct usb_device *dev)
315 {
316         int err;
317         __u8 startvideo = 0x10; /* set Bit5 */
318
319         reg_w(dev, ET_GPIO_OUT, &startvideo, 1);
320         err = Et_WaitStatus(dev);
321         if (!err)
322                 PDEBUG(D_ERR, "timeout Et_waitStatus VideoOFF");
323         return err;
324 }
325
326 static void Et_init2(struct gspca_dev *gspca_dev)
327 {
328         struct usb_device *dev = gspca_dev->dev;
329         __u8 value = 0x00;
330         __u8 received = 0x00;
331         __u8 FormLine[] = { 0x84, 0x03, 0x14, 0xf4, 0x01, 0x05 };
332
333         PDEBUG(D_STREAM, "Open Init2 ET");
334         value = 0x2f;
335         reg_w(dev, ET_GPIO_DIR_CTRL, &value, 1);
336         value = 0x10;
337         reg_w(dev, ET_GPIO_OUT, &value, 1);
338         reg_r(dev, ET_GPIO_IN, &received, 1);
339         value = 0x14;           /* 0x14 // 0x16 enabled pattern */
340         reg_w(dev, ET_ClCK, &value, 1);
341         value = 0x1b;
342         reg_w(dev, ET_CTRL, &value, 1);
343
344         /*  compression et subsampling */
345         if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].mode)
346                 value = ET_COMP_VAL1;   /* 320 */
347         else
348                 value = ET_COMP_VAL0;   /* 640 */
349         reg_w(dev, ET_COMP, &value, 1);
350         value = 0x1f;
351         reg_w(dev, ET_MAXQt, &value, 1);
352         value = 0x04;
353         reg_w(dev, ET_MINQt, &value, 1);
354         /* undocumented registers */
355         value = 0xff;
356         reg_w(dev, ET_REG1d, &value, 1);
357         value = 0xff;
358         reg_w(dev, ET_REG1e, &value, 1);
359         value = 0xff;
360         reg_w(dev, ET_REG1f, &value, 1);
361         value = 0x35;
362         reg_w(dev, ET_REG20, &value, 1);
363         value = 0x01;
364         reg_w(dev, ET_REG21, &value, 1);
365         value = 0x00;
366         reg_w(dev, ET_REG22, &value, 1);
367         value = 0xff;
368         reg_w(dev, ET_REG23, &value, 1);
369         value = 0xff;
370         reg_w(dev, ET_REG24, &value, 1);
371         value = 0x0f;
372         reg_w(dev, ET_REG25, &value, 1);
373         /* colors setting */
374         value = 0x11;
375         reg_w(dev, 0x30, &value, 1);    /* 0x30 */
376         value = 0x40;
377         reg_w(dev, 0x31, &value, 1);
378         value = 0x00;
379         reg_w(dev, 0x32, &value, 1);
380         value = 0x00;
381         reg_w(dev, ET_O_RED, &value, 1);        /* 0x34 */
382         value = 0x00;
383         reg_w(dev, ET_O_GREEN1, &value, 1);
384         value = 0x00;
385         reg_w(dev, ET_O_BLUE, &value, 1);
386         value = 0x00;
387         reg_w(dev, ET_O_GREEN2, &value, 1);
388         /*************/
389         value = 0x80;
390         reg_w(dev, ET_G_RED, &value, 1);        /* 0x4d */
391         value = 0x80;
392         reg_w(dev, ET_G_GREEN1, &value, 1);
393         value = 0x80;
394         reg_w(dev, ET_G_BLUE, &value, 1);
395         value = 0x80;
396         reg_w(dev, ET_G_GREEN2, &value, 1);
397         value = 0x00;
398         reg_w(dev, ET_G_GR_H, &value, 1);
399         value = 0x00;
400         reg_w(dev, ET_G_GB_H, &value, 1);       /* 0x52 */
401         /* Window control registers */
402
403         value = 0x80;           /* use cmc_out */
404         reg_w(dev, 0x61, &value, 1);
405
406         value = 0x02;
407         reg_w(dev, 0x62, &value, 1);
408         value = 0x03;
409         reg_w(dev, 0x63, &value, 1);
410         value = 0x14;
411         reg_w(dev, 0x64, &value, 1);
412         value = 0x0e;
413         reg_w(dev, 0x65, &value, 1);
414         value = 0x02;
415         reg_w(dev, 0x66, &value, 1);
416         value = 0x02;
417         reg_w(dev, 0x67, &value, 1);
418
419         /**************************************/
420         value = 0x8f;
421         reg_w(dev, ET_SYNCHRO, &value, 1);      /* 0x68 */
422         value = 0x69;           /* 0x6a //0x69 */
423         reg_w(dev, ET_STARTX, &value, 1);
424         value = 0x0d;           /* 0x0d //0x0c */
425         reg_w(dev, ET_STARTY, &value, 1);
426         value = 0x80;
427         reg_w(dev, ET_WIDTH_LOW, &value, 1);
428         value = 0xe0;
429         reg_w(dev, ET_HEIGTH_LOW, &value, 1);
430         value = 0x60;
431         reg_w(dev, ET_W_H_HEIGTH, &value, 1);   /* 6d */
432         value = 0x86;
433         reg_w(dev, ET_REG6e, &value, 1);
434         value = 0x01;
435         reg_w(dev, ET_REG6f, &value, 1);
436         value = 0x26;
437         reg_w(dev, ET_REG70, &value, 1);
438         value = 0x7a;
439         reg_w(dev, ET_REG71, &value, 1);
440         value = 0x01;
441         reg_w(dev, ET_REG72, &value, 1);
442         /* Clock Pattern registers ***************** */
443         value = 0x00;
444         reg_w(dev, ET_REG73, &value, 1);
445         value = 0x18;           /* 0x28 */
446         reg_w(dev, ET_REG74, &value, 1);
447         value = 0x0f;           /* 0x01 */
448         reg_w(dev, ET_REG75, &value, 1);
449         /**********************************************/
450         value = 0x20;
451         reg_w(dev, 0x8a, &value, 1);
452         value = 0x0f;
453         reg_w(dev, 0x8d, &value, 1);
454         value = 0x08;
455         reg_w(dev, 0x8e, &value, 1);
456         /**************************************/
457         value = 0x08;
458         reg_w(dev, 0x03, &value, 1);
459         value = 0x03;
460         reg_w(dev, ET_PXL_CLK, &value, 1);
461         value = 0xff;
462         reg_w(dev, 0x81, &value, 1);
463         value = 0x00;
464         reg_w(dev, 0x80, &value, 1);
465         value = 0xff;
466         reg_w(dev, 0x81, &value, 1);
467         value = 0x20;
468         reg_w(dev, 0x80, &value, 1);
469         value = 0x01;
470         reg_w(dev, 0x03, &value, 1);
471         value = 0x00;
472         reg_w(dev, 0x03, &value, 1);
473         value = 0x08;
474         reg_w(dev, 0x03, &value, 1);
475         /********************************************/
476
477         /* reg_r(dev,0x0,ET_I2C_BASE,&received,1);
478                                          always 0x40 as the pas106 ??? */
479         /* set the sensor */
480         if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].mode) {
481                 value = 0x04;   /* 320 */
482                 reg_w(dev, ET_PXL_CLK, &value, 1);
483                 /* now set by fifo the FormatLine setting */
484                 reg_w(dev, 0x62, FormLine, 6);
485         } else {                /* 640 */
486                 /* setting PixelClock
487                    0x03 mean 24/(3+1) = 6 Mhz
488                    0x05 -> 24/(5+1) = 4 Mhz
489                    0x0b -> 24/(11+1) = 2 Mhz
490                    0x17 -> 24/(23+1) = 1 Mhz
491                  */
492                 value = 0x1e;   /* 0x17 */
493                 reg_w(dev, ET_PXL_CLK, &value, 1);
494                 /* now set by fifo the FormatLine setting */
495                 reg_w(dev, 0x62, FormLine, 6);
496         }
497
498         /* set exposure times [ 0..0x78] 0->longvalue 0x78->shortvalue */
499         value = 0x47;           /* 0x47; */
500         reg_w(dev, 0x81, &value, 1);
501         value = 0x40;           /* 0x40; */
502         reg_w(dev, 0x80, &value, 1);
503         /* Pedro change */
504         /* Brightness change Brith+ decrease value */
505         /* Brigth- increase value */
506         /* original value = 0x70; */
507         value = 0x30;           /* 0x20; */
508         reg_w(dev, 0x81, &value, 1);    /* set brightness */
509         value = 0x20;           /* 0x20; */
510         reg_w(dev, 0x80, &value, 1);
511 }
512
513 static void setcolors(struct gspca_dev *gspca_dev)
514 {
515         struct sd *sd = (struct sd *) gspca_dev;
516         struct usb_device *dev = gspca_dev->dev;
517         static __u8 I2cc[] = { 0x05, 0x02, 0x02, 0x05, 0x0d };
518         __u8 i2cflags = 0x01;
519         /* __u8 green = 0; */
520         __u8 colors = sd->colors;
521
522         I2cc[3] = colors;       /* red */
523         I2cc[0] = 15 - colors;  /* blue */
524         /* green = 15 - ((((7*I2cc[0]) >> 2 ) + I2cc[3]) >> 1); */
525         /* I2cc[1] = I2cc[2] = green; */
526         if (sd->sensor == SENSOR_PAS106) {
527                 Et_i2cwrite(dev, PAS106_REG13, &i2cflags, 1, 3);
528                 Et_i2cwrite(dev, PAS106_REG9, I2cc, sizeof(I2cc), 1);
529         }
530 /*      PDEBUG(D_CONF , "Etoms red %d blue %d green %d",
531                 I2cc[3], I2cc[0], green); */
532 }
533
534 static void getcolors(struct gspca_dev *gspca_dev)
535 {
536         struct sd *sd = (struct sd *) gspca_dev;
537         /* __u8 valblue = 0; */
538         __u8 valred;
539
540         if (sd->sensor == SENSOR_PAS106) {
541                 /* Et_i2cread(gspca_dev->dev,PAS106_REG9,&valblue,1,1); */
542                 Et_i2cread(gspca_dev->dev, PAS106_REG9 + 3, &valred, 1, 1);
543                 sd->colors = valred & 0x0f;
544         }
545 }
546
547 static void Et_init1(struct gspca_dev *gspca_dev)
548 {
549         struct usb_device *dev = gspca_dev->dev;
550         __u8 value = 0x00;
551         __u8 received = 0x00;
552 /*      __u8 I2c0 [] ={0x0a,0x12,0x05,0x22,0xac,0x00,0x01,0x00}; */
553         __u8 I2c0[] = { 0x0a, 0x12, 0x05, 0x6d, 0xcd, 0x00, 0x01, 0x00 };
554                                                 /* try 1/120 0x6d 0xcd 0x40 */
555 /*      __u8 I2c0 [] ={0x0a,0x12,0x05,0xfe,0xfe,0xc0,0x01,0x00};
556                                                  * 1/60000 hmm ?? */
557
558         PDEBUG(D_STREAM, "Open Init1 ET");
559         value = 7;
560         reg_w(dev, ET_GPIO_DIR_CTRL, &value, 1);
561         reg_r(dev, ET_GPIO_IN, &received, 1);
562         value = 1;
563         reg_w(dev, ET_RESET_ALL, &value, 1);
564         value = 0;
565         reg_w(dev, ET_RESET_ALL, &value, 1);
566         value = 0x10;
567         reg_w(dev, ET_ClCK, &value, 1);
568         value = 0x19;
569         reg_w(dev, ET_CTRL, &value, 1);
570         /*   compression et subsampling */
571         if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].mode)
572                 value = ET_COMP_VAL1;
573         else
574                 value = ET_COMP_VAL0;
575
576         PDEBUG(D_STREAM, "Open mode %d Compression %d",
577                gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].mode,
578                value);
579         reg_w(dev, ET_COMP, &value, 1);
580         value = 0x1d;
581         reg_w(dev, ET_MAXQt, &value, 1);
582         value = 0x02;
583         reg_w(dev, ET_MINQt, &value, 1);
584         /* undocumented registers */
585         value = 0xff;
586         reg_w(dev, ET_REG1d, &value, 1);
587         value = 0xff;
588         reg_w(dev, ET_REG1e, &value, 1);
589         value = 0xff;
590         reg_w(dev, ET_REG1f, &value, 1);
591         value = 0x35;
592         reg_w(dev, ET_REG20, &value, 1);
593         value = 0x01;
594         reg_w(dev, ET_REG21, &value, 1);
595         value = 0x00;
596         reg_w(dev, ET_REG22, &value, 1);
597         value = 0xf7;
598         reg_w(dev, ET_REG23, &value, 1);
599         value = 0xff;
600         reg_w(dev, ET_REG24, &value, 1);
601         value = 0x07;
602         reg_w(dev, ET_REG25, &value, 1);
603         /* colors setting */
604         value = 0x80;
605         reg_w(dev, ET_G_RED, &value, 1);
606         value = 0x80;
607         reg_w(dev, ET_G_GREEN1, &value, 1);
608         value = 0x80;
609         reg_w(dev, ET_G_BLUE, &value, 1);
610         value = 0x80;
611         reg_w(dev, ET_G_GREEN2, &value, 1);
612         value = 0x00;
613         reg_w(dev, ET_G_GR_H, &value, 1);
614         value = 0x00;
615         reg_w(dev, ET_G_GB_H, &value, 1);
616         /* Window control registers */
617         value = 0xf0;
618         reg_w(dev, ET_SYNCHRO, &value, 1);
619         value = 0x56;           /* 0x56 */
620         reg_w(dev, ET_STARTX, &value, 1);
621         value = 0x05;           /* 0x04 */
622         reg_w(dev, ET_STARTY, &value, 1);
623         value = 0x60;
624         reg_w(dev, ET_WIDTH_LOW, &value, 1);
625         value = 0x20;
626         reg_w(dev, ET_HEIGTH_LOW, &value, 1);
627         value = 0x50;
628         reg_w(dev, ET_W_H_HEIGTH, &value, 1);
629         value = 0x86;
630         reg_w(dev, ET_REG6e, &value, 1);
631         value = 0x01;
632         reg_w(dev, ET_REG6f, &value, 1);
633         value = 0x86;
634         reg_w(dev, ET_REG70, &value, 1);
635         value = 0x14;
636         reg_w(dev, ET_REG71, &value, 1);
637         value = 0x00;
638         reg_w(dev, ET_REG72, &value, 1);
639         /* Clock Pattern registers */
640         value = 0x00;
641         reg_w(dev, ET_REG73, &value, 1);
642         value = 0x00;
643         reg_w(dev, ET_REG74, &value, 1);
644         value = 0x0a;
645         reg_w(dev, ET_REG75, &value, 1);
646         value = 0x04;
647         reg_w(dev, ET_I2C_CLK, &value, 1);
648         value = 0x01;
649         reg_w(dev, ET_PXL_CLK, &value, 1);
650         /* set the sensor */
651         if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].mode) {
652                 I2c0[0] = 0x06;
653                 Et_i2cwrite(dev, PAS106_REG2, I2c0, sizeof(I2c0), 1);
654                 Et_i2cwrite(dev, PAS106_REG9, I2c2, sizeof(I2c2), 1);
655                 value = 0x06;
656                 Et_i2cwrite(dev, PAS106_REG2, &value, 1, 1);
657                 Et_i2cwrite(dev, PAS106_REG3, I2c3, sizeof(I2c3), 1);
658                 /* value = 0x1f; */
659                 value = 0x04;
660                 Et_i2cwrite(dev, PAS106_REG0e, &value, 1, 1);
661         } else {
662                 I2c0[0] = 0x0a;
663
664                 Et_i2cwrite(dev, PAS106_REG2, I2c0, sizeof(I2c0), 1);
665                 Et_i2cwrite(dev, PAS106_REG9, I2c2, sizeof(I2c2), 1);
666                 value = 0x0a;
667
668                 Et_i2cwrite(dev, PAS106_REG2, &value, 1, 1);
669                 Et_i2cwrite(dev, PAS106_REG3, I2c3, sizeof(I2c3), 1);
670                 value = 0x04;
671                 /* value = 0x10; */
672                 Et_i2cwrite(dev, PAS106_REG0e, &value, 1, 1);
673                 /* bit 2 enable bit 1:2 select 0 1 2 3
674                    value = 0x07;                                * curve 0 *
675                    Et_i2cwrite(dev,PAS106_REG0f,&value,1,1);
676                  */
677         }
678
679 /*      value = 0x01; */
680 /*      value = 0x22; */
681 /*      Et_i2cwrite(dev, PAS106_REG5, &value, 1, 1); */
682         /* magnetude and sign bit for DAC */
683         Et_i2cwrite(dev, PAS106_REG7, I2c4, sizeof I2c4, 1);
684         /* now set by fifo the whole colors setting */
685         reg_w(dev, ET_G_RED, GainRGBG, 6);
686         getcolors(gspca_dev);
687         setcolors(gspca_dev);
688 }
689
690 /* this function is called at probe time */
691 static int sd_config(struct gspca_dev *gspca_dev,
692                      const struct usb_device_id *id)
693 {
694         struct sd *sd = (struct sd *) gspca_dev;
695         struct cam *cam;
696         __u16 vendor;
697         __u16 product;
698
699         vendor = id->idVendor;
700         product = id->idProduct;
701 /*      switch (vendor) { */
702 /*      case 0x102c:            * Etoms */
703                 switch (product) {
704                 case 0x6151:
705                         sd->sensor = SENSOR_PAS106;     /* Etoms61x151 */
706                         break;
707                 case 0x6251:
708                         sd->sensor = SENSOR_TAS5130CXX; /* Etoms61x251 */
709                         break;
710 /*              } */
711 /*              break; */
712         }
713         cam = &gspca_dev->cam;
714         cam->dev_name = (char *) id->driver_info;
715         cam->epaddr = 1;
716         if (sd->sensor == SENSOR_PAS106) {
717                 cam->cam_mode = sif_mode;
718                 cam->nmodes = sizeof sif_mode / sizeof sif_mode[0];
719         } else {
720                 cam->cam_mode = vga_mode;
721                 cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
722         }
723         sd->brightness = sd_ctrls[SD_BRIGHTNESS].qctrl.default_value;
724         sd->contrast = sd_ctrls[SD_CONTRAST].qctrl.default_value;
725         sd->colors = sd_ctrls[SD_COLOR].qctrl.default_value;
726         sd->autogain = sd_ctrls[SD_AUTOGAIN].qctrl.default_value;
727         return 0;
728 }
729
730 /* this function is called at open time */
731 static int sd_open(struct gspca_dev *gspca_dev)
732 {
733         struct sd *sd = (struct sd *) gspca_dev;
734         struct usb_device *dev = gspca_dev->dev;
735         int err;
736         __u8 value;
737
738         PDEBUG(D_STREAM, "Initialize ET1");
739         if (sd->sensor == SENSOR_PAS106)
740                 Et_init1(gspca_dev);
741         else
742                 Et_init2(gspca_dev);
743         value = 0x08;
744         reg_w(dev, ET_RESET_ALL, &value, 1);
745         err = Et_videoOff(dev);
746         PDEBUG(D_STREAM, "Et_Init_VideoOff %d", err);
747         return 0;
748 }
749
750 /* -- start the camera -- */
751 static void sd_start(struct gspca_dev *gspca_dev)
752 {
753         struct sd *sd = (struct sd *) gspca_dev;
754         struct usb_device *dev = gspca_dev->dev;
755         int err;
756         __u8 value;
757
758         if (sd->sensor == SENSOR_PAS106)
759                 Et_init1(gspca_dev);
760         else
761                 Et_init2(gspca_dev);
762
763         value = 0x08;
764         reg_w(dev, ET_RESET_ALL, &value, 1);
765         err = Et_videoOn(dev);
766         PDEBUG(D_STREAM, "Et_VideoOn %d", err);
767 }
768
769 static void sd_stopN(struct gspca_dev *gspca_dev)
770 {
771         int err;
772
773         err = Et_videoOff(gspca_dev->dev);
774         PDEBUG(D_STREAM, "Et_VideoOff %d", err);
775
776 }
777
778 static void sd_stop0(struct gspca_dev *gspca_dev)
779 {
780 }
781
782 static void sd_close(struct gspca_dev *gspca_dev)
783 {
784 }
785
786 static void setbrightness(struct gspca_dev *gspca_dev)
787 {
788         struct sd *sd = (struct sd *) gspca_dev;
789         int i;
790         __u8 brightness = sd->brightness;
791
792         for (i = 0; i < 4; i++)
793                 reg_w(gspca_dev->dev, (ET_O_RED + i), &brightness, 1);
794 }
795
796 static void getbrightness(struct gspca_dev *gspca_dev)
797 {
798         struct sd *sd = (struct sd *) gspca_dev;
799         int i;
800         int brightness = 0;
801         __u8 value = 0;
802
803         for (i = 0; i < 4; i++) {
804                 reg_r(gspca_dev->dev, (ET_O_RED + i), &value, 1);
805                 brightness += value;
806         }
807         sd->brightness = brightness >> 3;
808 }
809
810 static void setcontrast(struct gspca_dev *gspca_dev)
811 {
812         struct sd *sd = (struct sd *) gspca_dev;
813         __u8 RGBG[] = { 0x80, 0x80, 0x80, 0x80, 0x00, 0x00 };
814         __u8 contrast = sd->contrast;
815
816         memset(RGBG, contrast, sizeof RGBG - 2);
817         reg_w(gspca_dev->dev, ET_G_RED, RGBG, 6);
818 }
819
820 static void getcontrast(struct gspca_dev *gspca_dev)
821 {
822         struct sd *sd = (struct sd *) gspca_dev;
823         int i;
824         int contrast = 0;
825         __u8 value = 0;
826
827         for (i = 0; i < 4; i++) {
828                 reg_r(gspca_dev->dev, (ET_G_RED + i), &value, 1);
829                 contrast += value;
830         }
831         sd->contrast = contrast >> 2;
832 }
833
834 static __u8 Et_getgainG(struct gspca_dev *gspca_dev)
835 {
836         struct sd *sd = (struct sd *) gspca_dev;
837         __u8 value = 0;
838
839         if (sd->sensor == SENSOR_PAS106) {
840                 Et_i2cread(gspca_dev->dev, PAS106_REG0e, &value, 1, 1);
841                 PDEBUG(D_CONF, "Etoms gain G %d", value);
842                 return value;
843         }
844         return 0x1f;
845 }
846
847 static void Et_setgainG(struct gspca_dev *gspca_dev, __u8 gain)
848 {
849         struct sd *sd = (struct sd *) gspca_dev;
850         struct usb_device *dev = gspca_dev->dev;
851         __u8 i2cflags = 0x01;
852
853         if (sd->sensor == SENSOR_PAS106) {
854                 Et_i2cwrite(dev, PAS106_REG13, &i2cflags, 1, 3);
855                 Et_i2cwrite(dev, PAS106_REG0e, &gain, 1, 1);
856         }
857 }
858
859 #define BLIMIT(bright) \
860         (__u8)((bright > 0x1f)?0x1f:((bright < 4)?3:bright))
861 #define LIMIT(color) \
862         (unsigned char)((color > 0xff)?0xff:((color < 0)?0:color))
863
864 static void setautogain(struct gspca_dev *gspca_dev)
865 {
866         struct usb_device *dev = gspca_dev->dev;
867         __u8 GRBG[] = { 0, 0, 0, 0 };
868         __u8 luma = 0;
869         __u8 luma_mean = 128;
870         __u8 luma_delta = 20;
871         __u8 spring = 4;
872         int Gbright = 0;
873         __u8 r, g, b;
874
875         Gbright = Et_getgainG(gspca_dev);
876         reg_r(dev, ET_LUMA_CENTER, GRBG, 4);
877         g = (GRBG[0] + GRBG[3]) >> 1;
878         r = GRBG[1];
879         b = GRBG[2];
880         r = ((r << 8) - (r << 4) - (r << 3)) >> 10;
881         b = ((b << 7) >> 10);
882         g = ((g << 9) + (g << 7) + (g << 5)) >> 10;
883         luma = LIMIT(r + g + b);
884         PDEBUG(D_FRAM, "Etoms luma G %d", luma);
885         if (luma < luma_mean - luma_delta || luma > luma_mean + luma_delta) {
886                 Gbright += (luma_mean - luma) >> spring;
887                 Gbright = BLIMIT(Gbright);
888                 PDEBUG(D_FRAM, "Etoms Gbright %d", Gbright);
889                 Et_setgainG(gspca_dev, (__u8) Gbright);
890         }
891 }
892
893 #undef BLIMIT
894 #undef LIMIT
895
896 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
897                         struct gspca_frame *frame,      /* target */
898                         unsigned char *data,            /* isoc packet */
899                         int len)                        /* iso packet length */
900 {
901         struct sd *sd;
902         int seqframe;
903
904         seqframe = data[0] & 0x3f;
905         len = (int) (((data[0] & 0xc0) << 2) | data[1]);
906         if (seqframe == 0x3f) {
907                 PDEBUG(D_FRAM,
908                        "header packet found datalength %d !!", len);
909                 PDEBUG(D_FRAM, "G %d R %d G %d B %d",
910                        data[2], data[3], data[4], data[5]);
911                 data += 30;
912                 /* don't change datalength as the chips provided it */
913                 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
914                                         data, 0);
915                 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, data, len);
916                 sd = (struct sd *) gspca_dev;
917                 if (sd->ag_cnt >= 0) {
918                         if (--sd->ag_cnt < 0) {
919                                 sd->ag_cnt = AG_CNT_START;
920                                 setautogain(gspca_dev);
921                         }
922                 }
923                 return;
924         }
925         if (len) {
926                 data += 8;
927                 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
928         } else {                        /* Drop Packet */
929                 gspca_dev->last_packet_type = DISCARD_PACKET;
930         }
931 }
932
933 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
934 {
935         struct sd *sd = (struct sd *) gspca_dev;
936
937         sd->brightness = val;
938         if (gspca_dev->streaming)
939                 setbrightness(gspca_dev);
940         return 0;
941 }
942
943 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
944 {
945         struct sd *sd = (struct sd *) gspca_dev;
946
947         getbrightness(gspca_dev);
948         *val = sd->brightness;
949         return 0;
950 }
951
952 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
953 {
954         struct sd *sd = (struct sd *) gspca_dev;
955
956         sd->contrast = val;
957         if (gspca_dev->streaming)
958                 setcontrast(gspca_dev);
959         return 0;
960 }
961
962 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
963 {
964         struct sd *sd = (struct sd *) gspca_dev;
965
966         getcontrast(gspca_dev);
967         *val = sd->contrast;
968         return 0;
969 }
970
971 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
972 {
973         struct sd *sd = (struct sd *) gspca_dev;
974
975         sd->colors = val;
976         if (gspca_dev->streaming)
977                 setcolors(gspca_dev);
978         return 0;
979 }
980
981 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
982 {
983         struct sd *sd = (struct sd *) gspca_dev;
984
985         getcolors(gspca_dev);
986         *val = sd->colors;
987         return 0;
988 }
989
990 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
991 {
992         struct sd *sd = (struct sd *) gspca_dev;
993
994         sd->autogain = val;
995         if (val)
996                 sd->ag_cnt = AG_CNT_START;
997         else
998                 sd->ag_cnt = -1;
999         return 0;
1000 }
1001
1002 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1003 {
1004         struct sd *sd = (struct sd *) gspca_dev;
1005
1006         *val = sd->autogain;
1007         return 0;
1008 }
1009
1010 /* sub-driver description */
1011 static struct sd_desc sd_desc = {
1012         .name = MODULE_NAME,
1013         .ctrls = sd_ctrls,
1014         .nctrls = ARRAY_SIZE(sd_ctrls),
1015         .config = sd_config,
1016         .open = sd_open,
1017         .start = sd_start,
1018         .stopN = sd_stopN,
1019         .stop0 = sd_stop0,
1020         .close = sd_close,
1021         .pkt_scan = sd_pkt_scan,
1022 };
1023
1024 /* -- module initialisation -- */
1025 #define DVNM(name) .driver_info = (kernel_ulong_t) name
1026 static __devinitdata struct usb_device_id device_table[] = {
1027         {USB_DEVICE(0x102c, 0x6151), DVNM("Qcam Sangha CIF")},
1028         {USB_DEVICE(0x102c, 0x6251), DVNM("Qcam xxxxxx VGA")},
1029         {}
1030 };
1031
1032 MODULE_DEVICE_TABLE(usb, device_table);
1033
1034 /* -- device connect -- */
1035 static int sd_probe(struct usb_interface *intf,
1036                     const struct usb_device_id *id)
1037 {
1038         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1039                                THIS_MODULE);
1040 }
1041
1042 static struct usb_driver sd_driver = {
1043         .name = MODULE_NAME,
1044         .id_table = device_table,
1045         .probe = sd_probe,
1046         .disconnect = gspca_disconnect,
1047 };
1048
1049 /* -- module insert / remove -- */
1050 static int __init sd_mod_init(void)
1051 {
1052         if (usb_register(&sd_driver) < 0)
1053                 return -1;
1054         PDEBUG(D_PROBE, "v%s registered", version);
1055         return 0;
1056 }
1057
1058 static void __exit sd_mod_exit(void)
1059 {
1060         usb_deregister(&sd_driver);
1061         PDEBUG(D_PROBE, "deregistered");
1062 }
1063
1064 module_init(sd_mod_init);
1065 module_exit(sd_mod_exit);