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