Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
spca561.c
Go to the documentation of this file.
1 /*
2  * Sunplus spca561 subdriver
3  *
4  * Copyright (C) 2004 Michel Xhaard [email protected]
5  *
6  * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */
22 
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 
25 #define MODULE_NAME "spca561"
26 
27 #include <linux/input.h>
28 #include "gspca.h"
29 
30 MODULE_AUTHOR("Michel Xhaard <[email protected]>");
31 MODULE_DESCRIPTION("GSPCA/SPCA561 USB Camera Driver");
32 MODULE_LICENSE("GPL");
33 
34 #define EXPOSURE_MAX (2047 + 325)
35 
36 /* specific webcam descriptor */
37 struct sd {
38  struct gspca_dev gspca_dev; /* !! must be the first item */
39 
40  struct { /* hue/contrast control cluster */
41  struct v4l2_ctrl *contrast;
42  struct v4l2_ctrl *hue;
43  };
44  struct v4l2_ctrl *autogain;
45 
46 #define EXPO12A_DEF 3
47  __u8 expo12a; /* expo/gain? for rev 12a */
48 
50 #define Rev012A 0
51 #define Rev072A 1
52 
53  signed char ag_cnt;
54 #define AG_CNT_START 13
55 };
56 
57 static const struct v4l2_pix_format sif_012a_mode[] = {
59  .bytesperline = 160,
60  .sizeimage = 160 * 120,
61  .colorspace = V4L2_COLORSPACE_SRGB,
62  .priv = 3},
64  .bytesperline = 176,
65  .sizeimage = 176 * 144,
66  .colorspace = V4L2_COLORSPACE_SRGB,
67  .priv = 2},
69  .bytesperline = 320,
70  .sizeimage = 320 * 240 * 4 / 8,
71  .colorspace = V4L2_COLORSPACE_SRGB,
72  .priv = 1},
74  .bytesperline = 352,
75  .sizeimage = 352 * 288 * 4 / 8,
76  .colorspace = V4L2_COLORSPACE_SRGB,
77  .priv = 0},
78 };
79 
80 static const struct v4l2_pix_format sif_072a_mode[] = {
82  .bytesperline = 160,
83  .sizeimage = 160 * 120,
84  .colorspace = V4L2_COLORSPACE_SRGB,
85  .priv = 3},
87  .bytesperline = 176,
88  .sizeimage = 176 * 144,
89  .colorspace = V4L2_COLORSPACE_SRGB,
90  .priv = 2},
92  .bytesperline = 320,
93  .sizeimage = 320 * 240,
94  .colorspace = V4L2_COLORSPACE_SRGB,
95  .priv = 1},
97  .bytesperline = 352,
98  .sizeimage = 352 * 288,
99  .colorspace = V4L2_COLORSPACE_SRGB,
100  .priv = 0},
101 };
102 
103 /*
104  * Initialization data
105  * I'm not very sure how to split initialization from open data
106  * chunks. For now, we'll consider everything as initialization
107  */
108 /* Frame packet header offsets for the spca561 */
109 #define SPCA561_OFFSET_SNAP 1
110 #define SPCA561_OFFSET_TYPE 2
111 #define SPCA561_OFFSET_COMPRESS 3
112 #define SPCA561_OFFSET_FRAMSEQ 4
113 #define SPCA561_OFFSET_GPIO 5
114 #define SPCA561_OFFSET_USBBUFF 6
115 #define SPCA561_OFFSET_WIN2GRAVE 7
116 #define SPCA561_OFFSET_WIN2RAVE 8
117 #define SPCA561_OFFSET_WIN2BAVE 9
118 #define SPCA561_OFFSET_WIN2GBAVE 10
119 #define SPCA561_OFFSET_WIN1GRAVE 11
120 #define SPCA561_OFFSET_WIN1RAVE 12
121 #define SPCA561_OFFSET_WIN1BAVE 13
122 #define SPCA561_OFFSET_WIN1GBAVE 14
123 #define SPCA561_OFFSET_FREQ 15
124 #define SPCA561_OFFSET_VSYNC 16
125 #define SPCA561_INDEX_I2C_BASE 0x8800
126 #define SPCA561_SNAPBIT 0x20
127 #define SPCA561_SNAPCTRL 0x40
128 
129 static const u16 rev72a_reset[][2] = {
130  {0x0000, 0x8114}, /* Software GPIO output data */
131  {0x0001, 0x8114}, /* Software GPIO output data */
132  {0x0000, 0x8112}, /* Some kind of reset */
133  {}
134 };
135 static const __u16 rev72a_init_data1[][2] = {
136  {0x0003, 0x8701}, /* PCLK clock delay adjustment */
137  {0x0001, 0x8703}, /* HSYNC from cmos inverted */
138  {0x0011, 0x8118}, /* Enable and conf sensor */
139  {0x0001, 0x8118}, /* Conf sensor */
140  {0x0092, 0x8804}, /* I know nothing about these */
141  {0x0010, 0x8802}, /* 0x88xx registers, so I won't */
142  {}
143 };
144 static const u16 rev72a_init_sensor1[][2] = {
145  {0x0001, 0x000d},
146  {0x0002, 0x0018},
147  {0x0004, 0x0165},
148  {0x0005, 0x0021},
149  {0x0007, 0x00aa},
150  {0x0020, 0x1504},
151  {0x0039, 0x0002},
152  {0x0035, 0x0010},
153  {0x0009, 0x1049},
154  {0x0028, 0x000b},
155  {0x003b, 0x000f},
156  {0x003c, 0x0000},
157  {}
158 };
159 static const __u16 rev72a_init_data2[][2] = {
160  {0x0018, 0x8601}, /* Pixel/line selection for color separation */
161  {0x0000, 0x8602}, /* Optical black level for user setting */
162  {0x0060, 0x8604}, /* Optical black horizontal offset */
163  {0x0002, 0x8605}, /* Optical black vertical offset */
164  {0x0000, 0x8603}, /* Non-automatic optical black level */
165  {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
166  {0x0000, 0x865f}, /* Vertical valid pixels window (x2) */
167  {0x00b0, 0x865d}, /* Horizontal valid pixels window (x2) */
168  {0x0090, 0x865e}, /* Vertical valid lines window (x2) */
169  {0x00e0, 0x8406}, /* Memory buffer threshold */
170  {0x0000, 0x8660}, /* Compensation memory stuff */
171  {0x0002, 0x8201}, /* Output address for r/w serial EEPROM */
172  {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
173  {0x0001, 0x8200}, /* OprMode to be executed by hardware */
174 /* from ms-win */
175  {0x0000, 0x8611}, /* R offset for white balance */
176  {0x00fd, 0x8612}, /* Gr offset for white balance */
177  {0x0003, 0x8613}, /* B offset for white balance */
178  {0x0000, 0x8614}, /* Gb offset for white balance */
179 /* from ms-win */
180  {0x0035, 0x8651}, /* R gain for white balance */
181  {0x0040, 0x8652}, /* Gr gain for white balance */
182  {0x005f, 0x8653}, /* B gain for white balance */
183  {0x0040, 0x8654}, /* Gb gain for white balance */
184  {0x0002, 0x8502}, /* Maximum average bit rate stuff */
185  {0x0011, 0x8802},
186 
187  {0x0087, 0x8700}, /* Set master clock (96Mhz????) */
188  {0x0081, 0x8702}, /* Master clock output enable */
189 
190  {0x0000, 0x8500}, /* Set image type (352x288 no compression) */
191  /* Originally was 0x0010 (352x288 compression) */
192 
193  {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
194  {0x0003, 0x865c}, /* Vertical offset for valid lines */
195  {}
196 };
197 static const u16 rev72a_init_sensor2[][2] = {
198  {0x0003, 0x0121},
199  {0x0004, 0x0165},
200  {0x0005, 0x002f}, /* blanking control column */
201  {0x0006, 0x0000}, /* blanking mode row*/
202  {0x000a, 0x0002},
203  {0x0009, 0x1061}, /* setexposure times && pixel clock
204  * 0001 0 | 000 0110 0001 */
205  {0x0035, 0x0014},
206  {}
207 };
208 
209 /******************** QC Express etch2 stuff ********************/
210 static const __u16 Pb100_1map8300[][2] = {
211  /* reg, value */
212  {0x8320, 0x3304},
213 
214  {0x8303, 0x0125}, /* image area */
215  {0x8304, 0x0169},
216  {0x8328, 0x000b},
217  {0x833c, 0x0001}, /*fixme: win:07*/
218 
219  {0x832f, 0x1904}, /*fixme: was 0419*/
220  {0x8307, 0x00aa},
221  {0x8301, 0x0003},
222  {0x8302, 0x000e},
223  {}
224 };
225 static const __u16 Pb100_2map8300[][2] = {
226  /* reg, value */
227  {0x8339, 0x0000},
228  {0x8307, 0x00aa},
229  {}
230 };
231 
232 static const __u16 spca561_161rev12A_data1[][2] = {
233  {0x29, 0x8118}, /* Control register (various enable bits) */
234  {0x08, 0x8114}, /* GPIO: Led off */
235  {0x0e, 0x8112}, /* 0x0e stream off 0x3e stream on */
236  {0x00, 0x8102}, /* white balance - new */
237  {0x92, 0x8804},
238  {0x04, 0x8802}, /* windows uses 08 */
239  {}
240 };
241 static const __u16 spca561_161rev12A_data2[][2] = {
242  {0x21, 0x8118},
243  {0x10, 0x8500},
244  {0x07, 0x8601},
245  {0x07, 0x8602},
246  {0x04, 0x8501},
247 
248  {0x07, 0x8201}, /* windows uses 02 */
249  {0x08, 0x8200},
250  {0x01, 0x8200},
251 
252  {0x90, 0x8604},
253  {0x00, 0x8605},
254  {0xb0, 0x8603},
255 
256  /* sensor gains */
257  {0x07, 0x8601}, /* white balance - new */
258  {0x07, 0x8602}, /* white balance - new */
259  {0x00, 0x8610}, /* *red */
260  {0x00, 0x8611}, /* 3f *green */
261  {0x00, 0x8612}, /* green *blue */
262  {0x00, 0x8613}, /* blue *green */
263  {0x43, 0x8614}, /* green *red - white balance - was 0x35 */
264  {0x40, 0x8615}, /* 40 *green - white balance - was 0x35 */
265  {0x71, 0x8616}, /* 7a *blue - white balance - was 0x35 */
266  {0x40, 0x8617}, /* 40 *green - white balance - was 0x35 */
267 
268  {0x0c, 0x8620}, /* 0c */
269  {0xc8, 0x8631}, /* c8 */
270  {0xc8, 0x8634}, /* c8 */
271  {0x23, 0x8635}, /* 23 */
272  {0x1f, 0x8636}, /* 1f */
273  {0xdd, 0x8637}, /* dd */
274  {0xe1, 0x8638}, /* e1 */
275  {0x1d, 0x8639}, /* 1d */
276  {0x21, 0x863a}, /* 21 */
277  {0xe3, 0x863b}, /* e3 */
278  {0xdf, 0x863c}, /* df */
279  {0xf0, 0x8505},
280  {0x32, 0x850a},
281 /* {0x99, 0x8700}, * - white balance - new (removed) */
282  /* HDG we used to do this in stop0, making the init state and the state
283  after a start / stop different, so do this here instead. */
284  {0x29, 0x8118},
285  {}
286 };
287 
288 static void reg_w_val(struct usb_device *dev, __u16 index, __u8 value)
289 {
290  int ret;
291 
292  ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
293  0, /* request */
295  value, index, NULL, 0, 500);
296  PDEBUG(D_USBO, "reg write: 0x%02x:0x%02x", index, value);
297  if (ret < 0)
298  pr_err("reg write: error %d\n", ret);
299 }
300 
301 static void write_vector(struct gspca_dev *gspca_dev,
302  const __u16 data[][2])
303 {
304  struct usb_device *dev = gspca_dev->dev;
305  int i;
306 
307  i = 0;
308  while (data[i][1] != 0) {
309  reg_w_val(dev, data[i][1], data[i][0]);
310  i++;
311  }
312 }
313 
314 /* read 'len' bytes to gspca_dev->usb_buf */
315 static void reg_r(struct gspca_dev *gspca_dev,
316  __u16 index, __u16 length)
317 {
318  usb_control_msg(gspca_dev->dev,
319  usb_rcvctrlpipe(gspca_dev->dev, 0),
320  0, /* request */
322  0, /* value */
323  index, gspca_dev->usb_buf, length, 500);
324 }
325 
326 /* write 'len' bytes from gspca_dev->usb_buf */
327 static void reg_w_buf(struct gspca_dev *gspca_dev,
328  __u16 index, __u16 len)
329 {
330  usb_control_msg(gspca_dev->dev,
331  usb_sndctrlpipe(gspca_dev->dev, 0),
332  0, /* request */
334  0, /* value */
335  index, gspca_dev->usb_buf, len, 500);
336 }
337 
338 static void i2c_write(struct gspca_dev *gspca_dev, __u16 value, __u16 reg)
339 {
340  int retry = 60;
341 
342  reg_w_val(gspca_dev->dev, 0x8801, reg);
343  reg_w_val(gspca_dev->dev, 0x8805, value);
344  reg_w_val(gspca_dev->dev, 0x8800, value >> 8);
345  do {
346  reg_r(gspca_dev, 0x8803, 1);
347  if (!gspca_dev->usb_buf[0])
348  return;
349  msleep(10);
350  } while (--retry);
351 }
352 
353 static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
354 {
355  int retry = 60;
356  __u8 value;
357 
358  reg_w_val(gspca_dev->dev, 0x8804, 0x92);
359  reg_w_val(gspca_dev->dev, 0x8801, reg);
360  reg_w_val(gspca_dev->dev, 0x8802, mode | 0x01);
361  do {
362  reg_r(gspca_dev, 0x8803, 1);
363  if (!gspca_dev->usb_buf[0]) {
364  reg_r(gspca_dev, 0x8800, 1);
365  value = gspca_dev->usb_buf[0];
366  reg_r(gspca_dev, 0x8805, 1);
367  return ((int) value << 8) | gspca_dev->usb_buf[0];
368  }
369  msleep(10);
370  } while (--retry);
371  return -1;
372 }
373 
374 static void sensor_mapwrite(struct gspca_dev *gspca_dev,
375  const __u16 (*sensormap)[2])
376 {
377  while ((*sensormap)[0]) {
378  gspca_dev->usb_buf[0] = (*sensormap)[1];
379  gspca_dev->usb_buf[1] = (*sensormap)[1] >> 8;
380  reg_w_buf(gspca_dev, (*sensormap)[0], 2);
381  sensormap++;
382  }
383 }
384 
385 static void write_sensor_72a(struct gspca_dev *gspca_dev,
386  const __u16 (*sensor)[2])
387 {
388  while ((*sensor)[0]) {
389  i2c_write(gspca_dev, (*sensor)[1], (*sensor)[0]);
390  sensor++;
391  }
392 }
393 
394 static void init_161rev12A(struct gspca_dev *gspca_dev)
395 {
396  write_vector(gspca_dev, spca561_161rev12A_data1);
397  sensor_mapwrite(gspca_dev, Pb100_1map8300);
398 /*fixme: should be in sd_start*/
399  write_vector(gspca_dev, spca561_161rev12A_data2);
400  sensor_mapwrite(gspca_dev, Pb100_2map8300);
401 }
402 
403 /* this function is called at probe time */
404 static int sd_config(struct gspca_dev *gspca_dev,
405  const struct usb_device_id *id)
406 {
407  struct sd *sd = (struct sd *) gspca_dev;
408  struct cam *cam;
410  __u8 data1, data2;
411 
412  /* Read frm global register the USB product and vendor IDs, just to
413  * prove that we can communicate with the device. This works, which
414  * confirms at we are communicating properly and that the device
415  * is a 561. */
416  reg_r(gspca_dev, 0x8104, 1);
417  data1 = gspca_dev->usb_buf[0];
418  reg_r(gspca_dev, 0x8105, 1);
419  data2 = gspca_dev->usb_buf[0];
420  vendor = (data2 << 8) | data1;
421  reg_r(gspca_dev, 0x8106, 1);
422  data1 = gspca_dev->usb_buf[0];
423  reg_r(gspca_dev, 0x8107, 1);
424  data2 = gspca_dev->usb_buf[0];
425  product = (data2 << 8) | data1;
426  if (vendor != id->idVendor || product != id->idProduct) {
427  PDEBUG(D_PROBE, "Bad vendor / product from device");
428  return -EINVAL;
429  }
430 
431  cam = &gspca_dev->cam;
432  cam->needs_full_bandwidth = 1;
433 
434  sd->chip_revision = id->driver_info;
435  if (sd->chip_revision == Rev012A) {
436  cam->cam_mode = sif_012a_mode;
437  cam->nmodes = ARRAY_SIZE(sif_012a_mode);
438  } else {
439  cam->cam_mode = sif_072a_mode;
440  cam->nmodes = ARRAY_SIZE(sif_072a_mode);
441  }
442  sd->expo12a = EXPO12A_DEF;
443  return 0;
444 }
445 
446 /* this function is called at probe and resume time */
447 static int sd_init_12a(struct gspca_dev *gspca_dev)
448 {
449  PDEBUG(D_STREAM, "Chip revision: 012a");
450  init_161rev12A(gspca_dev);
451  return 0;
452 }
453 static int sd_init_72a(struct gspca_dev *gspca_dev)
454 {
455  PDEBUG(D_STREAM, "Chip revision: 072a");
456  write_vector(gspca_dev, rev72a_reset);
457  msleep(200);
458  write_vector(gspca_dev, rev72a_init_data1);
459  write_sensor_72a(gspca_dev, rev72a_init_sensor1);
460  write_vector(gspca_dev, rev72a_init_data2);
461  write_sensor_72a(gspca_dev, rev72a_init_sensor2);
462  reg_w_val(gspca_dev->dev, 0x8112, 0x30);
463  return 0;
464 }
465 
466 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
467 {
468  struct sd *sd = (struct sd *) gspca_dev;
469  struct usb_device *dev = gspca_dev->dev;
470  __u16 reg;
471 
472  if (sd->chip_revision == Rev012A)
473  reg = 0x8610;
474  else
475  reg = 0x8611;
476 
477  reg_w_val(dev, reg + 0, val); /* R */
478  reg_w_val(dev, reg + 1, val); /* Gr */
479  reg_w_val(dev, reg + 2, val); /* B */
480  reg_w_val(dev, reg + 3, val); /* Gb */
481 }
482 
483 static void setwhite(struct gspca_dev *gspca_dev, s32 white, s32 contrast)
484 {
485  struct sd *sd = (struct sd *) gspca_dev;
486  struct usb_device *dev = gspca_dev->dev;
487  __u8 blue, red;
488  __u16 reg;
489 
490  /* try to emulate MS-win as possible */
491  red = 0x20 + white * 3 / 8;
492  blue = 0x90 - white * 5 / 8;
493  if (sd->chip_revision == Rev012A) {
494  reg = 0x8614;
495  } else {
496  reg = 0x8651;
497  red += contrast - 0x20;
498  blue += contrast - 0x20;
499  reg_w_val(dev, 0x8652, contrast + 0x20); /* Gr */
500  reg_w_val(dev, 0x8654, contrast + 0x20); /* Gb */
501  }
502  reg_w_val(dev, reg, red);
503  reg_w_val(dev, reg + 2, blue);
504 }
505 
506 /* rev 12a only */
507 static void setexposure(struct gspca_dev *gspca_dev, s32 val)
508 {
509  int i, expo = 0;
510 
511  /* Register 0x8309 controls exposure for the spca561,
512  the basic exposure setting goes from 1-2047, where 1 is completely
513  dark and 2047 is very bright. It not only influences exposure but
514  also the framerate (to allow for longer exposure) from 1 - 300 it
515  only raises the exposure time then from 300 - 600 it halves the
516  framerate to be able to further raise the exposure time and for every
517  300 more it halves the framerate again. This allows for a maximum
518  exposure time of circa 0.2 - 0.25 seconds (30 / (2000/3000) fps).
519  Sometimes this is not enough, the 1-2047 uses bits 0-10, bits 11-12
520  configure a divider for the base framerate which us used at the
521  exposure setting of 1-300. These bits configure the base framerate
522  according to the following formula: fps = 60 / (value + 2) */
523 
524  /* We choose to use the high bits setting the fixed framerate divisor
525  asap, as setting high basic exposure setting without the fixed
526  divider in combination with high gains makes the cam stop */
527  int table[] = { 0, 450, 550, 625, EXPOSURE_MAX };
528 
529  for (i = 0; i < ARRAY_SIZE(table) - 1; i++) {
530  if (val <= table[i + 1]) {
531  expo = val - table[i];
532  if (i)
533  expo += 300;
534  expo |= i << 11;
535  break;
536  }
537  }
538 
539  gspca_dev->usb_buf[0] = expo;
540  gspca_dev->usb_buf[1] = expo >> 8;
541  reg_w_buf(gspca_dev, 0x8309, 2);
542 }
543 
544 /* rev 12a only */
545 static void setgain(struct gspca_dev *gspca_dev, s32 val)
546 {
547  /* gain reg low 6 bits 0-63 gain, bit 6 and 7, both double the
548  sensitivity when set, so 31 + one of them set == 63, and 15
549  with both of them set == 63 */
550  if (val < 64)
551  gspca_dev->usb_buf[0] = val;
552  else if (val < 128)
553  gspca_dev->usb_buf[0] = (val / 2) | 0x40;
554  else
555  gspca_dev->usb_buf[0] = (val / 4) | 0xc0;
556 
557  gspca_dev->usb_buf[1] = 0;
558  reg_w_buf(gspca_dev, 0x8335, 2);
559 }
560 
561 static void setautogain(struct gspca_dev *gspca_dev, s32 val)
562 {
563  struct sd *sd = (struct sd *) gspca_dev;
564 
565  if (val)
566  sd->ag_cnt = AG_CNT_START;
567  else
568  sd->ag_cnt = -1;
569 }
570 
571 static int sd_start_12a(struct gspca_dev *gspca_dev)
572 {
573  struct usb_device *dev = gspca_dev->dev;
574  int mode;
575  static const __u8 Reg8391[8] =
576  {0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00};
577 
578  mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
579  if (mode <= 1) {
580  /* Use compression on 320x240 and above */
581  reg_w_val(dev, 0x8500, 0x10 | mode);
582  } else {
583  /* I couldn't get the compression to work below 320x240
584  * Fortunately at these resolutions the bandwidth
585  * is sufficient to push raw frames at ~20fps */
586  reg_w_val(dev, 0x8500, mode);
587  } /* -- [email protected] */
588 
589  gspca_dev->usb_buf[0] = 0xaa;
590  gspca_dev->usb_buf[1] = 0x00;
591  reg_w_buf(gspca_dev, 0x8307, 2);
592  /* clock - lower 0x8X values lead to fps > 30 */
593  reg_w_val(gspca_dev->dev, 0x8700, 0x8a);
594  /* 0x8f 0x85 0x27 clock */
595  reg_w_val(gspca_dev->dev, 0x8112, 0x1e | 0x20);
596  reg_w_val(gspca_dev->dev, 0x850b, 0x03);
597  memcpy(gspca_dev->usb_buf, Reg8391, 8);
598  reg_w_buf(gspca_dev, 0x8391, 8);
599  reg_w_buf(gspca_dev, 0x8390, 8);
600 
601  /* Led ON (bit 3 -> 0 */
602  reg_w_val(gspca_dev->dev, 0x8114, 0x00);
603  return 0;
604 }
605 static int sd_start_72a(struct gspca_dev *gspca_dev)
606 {
607  struct sd *sd = (struct sd *) gspca_dev;
608  struct usb_device *dev = gspca_dev->dev;
609  int Clck;
610  int mode;
611 
612  write_vector(gspca_dev, rev72a_reset);
613  msleep(200);
614  write_vector(gspca_dev, rev72a_init_data1);
615  write_sensor_72a(gspca_dev, rev72a_init_sensor1);
616 
617  mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
618  switch (mode) {
619  default:
620  case 0:
621  Clck = 0x27; /* ms-win 0x87 */
622  break;
623  case 1:
624  Clck = 0x25;
625  break;
626  case 2:
627  Clck = 0x22;
628  break;
629  case 3:
630  Clck = 0x21;
631  break;
632  }
633  reg_w_val(dev, 0x8700, Clck); /* 0x27 clock */
634  reg_w_val(dev, 0x8702, 0x81);
635  reg_w_val(dev, 0x8500, mode); /* mode */
636  write_sensor_72a(gspca_dev, rev72a_init_sensor2);
637  setwhite(gspca_dev, v4l2_ctrl_g_ctrl(sd->hue),
639 /* setbrightness(gspca_dev); * fixme: bad values */
640  setautogain(gspca_dev, v4l2_ctrl_g_ctrl(sd->autogain));
641  reg_w_val(dev, 0x8112, 0x10 | 0x20);
642  return 0;
643 }
644 
645 static void sd_stopN(struct gspca_dev *gspca_dev)
646 {
647  struct sd *sd = (struct sd *) gspca_dev;
648 
649  if (sd->chip_revision == Rev012A) {
650  reg_w_val(gspca_dev->dev, 0x8112, 0x0e);
651  /* Led Off (bit 3 -> 1 */
652  reg_w_val(gspca_dev->dev, 0x8114, 0x08);
653  } else {
654  reg_w_val(gspca_dev->dev, 0x8112, 0x20);
655 /* reg_w_val(gspca_dev->dev, 0x8102, 0x00); ?? */
656  }
657 }
658 
659 static void do_autogain(struct gspca_dev *gspca_dev)
660 {
661  struct sd *sd = (struct sd *) gspca_dev;
662  int expotimes;
663  int pixelclk;
664  int gainG;
665  __u8 R, Gr, Gb, B;
666  int y;
667  __u8 luma_mean = 110;
668  __u8 luma_delta = 20;
669  __u8 spring = 4;
670 
671  if (sd->ag_cnt < 0)
672  return;
673  if (--sd->ag_cnt >= 0)
674  return;
675  sd->ag_cnt = AG_CNT_START;
676 
677  switch (sd->chip_revision) {
678  case Rev072A:
679  reg_r(gspca_dev, 0x8621, 1);
680  Gr = gspca_dev->usb_buf[0];
681  reg_r(gspca_dev, 0x8622, 1);
682  R = gspca_dev->usb_buf[0];
683  reg_r(gspca_dev, 0x8623, 1);
684  B = gspca_dev->usb_buf[0];
685  reg_r(gspca_dev, 0x8624, 1);
686  Gb = gspca_dev->usb_buf[0];
687  y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
688  /* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
689  /* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
690  /* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */
691 
692  if (y < luma_mean - luma_delta ||
693  y > luma_mean + luma_delta) {
694  expotimes = i2c_read(gspca_dev, 0x09, 0x10);
695  pixelclk = 0x0800;
696  expotimes = expotimes & 0x07ff;
697  /* PDEBUG(D_PACK,
698  "Exposition Times 0x%03X Clock 0x%04X ",
699  expotimes,pixelclk); */
700  gainG = i2c_read(gspca_dev, 0x35, 0x10);
701  /* PDEBUG(D_PACK,
702  "reading Gain register %d", gainG); */
703 
704  expotimes += (luma_mean - y) >> spring;
705  gainG += (luma_mean - y) / 50;
706  /* PDEBUG(D_PACK,
707  "compute expotimes %d gain %d",
708  expotimes,gainG); */
709 
710  if (gainG > 0x3f)
711  gainG = 0x3f;
712  else if (gainG < 3)
713  gainG = 3;
714  i2c_write(gspca_dev, gainG, 0x35);
715 
716  if (expotimes > 0x0256)
717  expotimes = 0x0256;
718  else if (expotimes < 3)
719  expotimes = 3;
720  i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
721  }
722  break;
723  }
724 }
725 
726 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
727  u8 *data, /* isoc packet */
728  int len) /* iso packet length */
729 {
730  struct sd *sd = (struct sd *) gspca_dev;
731 
732  len--;
733  switch (*data++) { /* sequence number */
734  case 0: /* start of frame */
735  gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
736 
737  /* This should never happen */
738  if (len < 2) {
739  PDEBUG(D_ERR, "Short SOF packet, ignoring");
740  gspca_dev->last_packet_type = DISCARD_PACKET;
741  return;
742  }
743 
744 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
745  if (data[0] & 0x20) {
746  input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
747  input_sync(gspca_dev->input_dev);
748  input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
749  input_sync(gspca_dev->input_dev);
750  }
751 #endif
752 
753  if (data[1] & 0x10) {
754  /* compressed bayer */
755  gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
756  } else {
757  /* raw bayer (with a header, which we skip) */
758  if (sd->chip_revision == Rev012A) {
759  data += 20;
760  len -= 20;
761  } else {
762  data += 16;
763  len -= 16;
764  }
765  gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
766  }
767  return;
768  case 0xff: /* drop (empty mpackets) */
769  return;
770  }
771  gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
772 }
773 
774 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
775 {
776  struct gspca_dev *gspca_dev =
777  container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
778  struct sd *sd = (struct sd *)gspca_dev;
779 
780  gspca_dev->usb_err = 0;
781 
782  if (!gspca_dev->streaming)
783  return 0;
784 
785  switch (ctrl->id) {
786  case V4L2_CID_BRIGHTNESS:
787  setbrightness(gspca_dev, ctrl->val);
788  break;
789  case V4L2_CID_CONTRAST:
790  /* hue/contrast control cluster for 72a */
791  setwhite(gspca_dev, sd->hue->val, ctrl->val);
792  break;
793  case V4L2_CID_HUE:
794  /* just plain hue control for 12a */
795  setwhite(gspca_dev, ctrl->val, 0);
796  break;
797  case V4L2_CID_EXPOSURE:
798  setexposure(gspca_dev, ctrl->val);
799  break;
800  case V4L2_CID_GAIN:
801  setgain(gspca_dev, ctrl->val);
802  break;
803  case V4L2_CID_AUTOGAIN:
804  setautogain(gspca_dev, ctrl->val);
805  break;
806  }
807  return gspca_dev->usb_err;
808 }
809 
810 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
811  .s_ctrl = sd_s_ctrl,
812 };
813 
814 static int sd_init_controls_12a(struct gspca_dev *gspca_dev)
815 {
816  struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
817 
818  gspca_dev->vdev.ctrl_handler = hdl;
819  v4l2_ctrl_handler_init(hdl, 3);
820  v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
821  V4L2_CID_HUE, 1, 0x7f, 1, 0x40);
822  v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
823  V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
824  v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
825  V4L2_CID_EXPOSURE, 1, EXPOSURE_MAX, 1, 700);
826  v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
827  V4L2_CID_GAIN, 0, 255, 1, 63);
828 
829  if (hdl->error) {
830  pr_err("Could not initialize controls\n");
831  return hdl->error;
832  }
833  return 0;
834 }
835 
836 static int sd_init_controls_72a(struct gspca_dev *gspca_dev)
837 {
838  struct sd *sd = (struct sd *)gspca_dev;
839  struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
840 
841  gspca_dev->vdev.ctrl_handler = hdl;
842  v4l2_ctrl_handler_init(hdl, 4);
843  sd->contrast = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
844  V4L2_CID_CONTRAST, 0, 0x3f, 1, 0x20);
845  sd->hue = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
846  V4L2_CID_HUE, 1, 0x7f, 1, 0x40);
847  v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
848  V4L2_CID_BRIGHTNESS, 0, 0x3f, 1, 0x20);
849  sd->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
850  V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
851 
852  if (hdl->error) {
853  pr_err("Could not initialize controls\n");
854  return hdl->error;
855  }
856  v4l2_ctrl_cluster(2, &sd->contrast);
857  return 0;
858 }
859 
860 /* sub-driver description */
861 static const struct sd_desc sd_desc_12a = {
862  .name = MODULE_NAME,
863  .init_controls = sd_init_controls_12a,
864  .config = sd_config,
865  .init = sd_init_12a,
866  .start = sd_start_12a,
867  .stopN = sd_stopN,
868  .pkt_scan = sd_pkt_scan,
869 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
870  .other_input = 1,
871 #endif
872 };
873 static const struct sd_desc sd_desc_72a = {
874  .name = MODULE_NAME,
875  .init_controls = sd_init_controls_72a,
876  .config = sd_config,
877  .init = sd_init_72a,
878  .start = sd_start_72a,
879  .stopN = sd_stopN,
880  .pkt_scan = sd_pkt_scan,
881  .dq_callback = do_autogain,
882 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
883  .other_input = 1,
884 #endif
885 };
886 static const struct sd_desc *sd_desc[2] = {
887  &sd_desc_12a,
888  &sd_desc_72a
889 };
890 
891 /* -- module initialisation -- */
892 static const struct usb_device_id device_table[] = {
893  {USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
894  {USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
895  {USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
896  {USB_DEVICE(0x0461, 0x0815), .driver_info = Rev072A},
897  {USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
898  {USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
899  {USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
900  {USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
901  {USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
902  {USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
903  {USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
904  {USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
905  {USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
906  {USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
907  {USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
908  {USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
909  {}
910 };
911 
912 MODULE_DEVICE_TABLE(usb, device_table);
913 
914 /* -- device connect -- */
915 static int sd_probe(struct usb_interface *intf,
916  const struct usb_device_id *id)
917 {
918  return gspca_dev_probe(intf, id,
919  sd_desc[id->driver_info],
920  sizeof(struct sd),
921  THIS_MODULE);
922 }
923 
924 static struct usb_driver sd_driver = {
925  .name = MODULE_NAME,
926  .id_table = device_table,
927  .probe = sd_probe,
928  .disconnect = gspca_disconnect,
929 #ifdef CONFIG_PM
930  .suspend = gspca_suspend,
931  .resume = gspca_resume,
932  .reset_resume = gspca_resume,
933 #endif
934 };
935 
936 module_usb_driver(sd_driver);