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