Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sonixb.c
Go to the documentation of this file.
1 /*
2  * sonix sn9c102 (bayer) library
3  *
4  * Copyright (C) 2009-2011 Jean-Fran├žois Moine <http://moinejf.free.fr>
5  * Copyright (C) 2003 2004 Michel Xhaard [email protected]
6  * Add Pas106 Stefano Mozzi (C) 2004
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 /* Some documentation on known sonixb registers:
24 
25 Reg Use
26 sn9c101 / sn9c102:
27 0x10 high nibble red gain low nibble blue gain
28 0x11 low nibble green gain
29 sn9c103:
30 0x05 red gain 0-127
31 0x06 blue gain 0-127
32 0x07 green gain 0-127
33 all:
34 0x08-0x0f i2c / 3wire registers
35 0x12 hstart
36 0x13 vstart
37 0x15 hsize (hsize = register-value * 16)
38 0x16 vsize (vsize = register-value * 16)
39 0x17 bit 0 toggle compression quality (according to sn9c102 driver)
40 0x18 bit 7 enables compression, bit 4-5 set image down scaling:
41  00 scale 1, 01 scale 1/2, 10, scale 1/4
42 0x19 high-nibble is sensor clock divider, changes exposure on sensors which
43  use a clock generated by the bridge. Some sensors have their own clock.
44 0x1c auto_exposure area (for avg_lum) startx (startx = register-value * 32)
45 0x1d auto_exposure area (for avg_lum) starty (starty = register-value * 32)
46 0x1e auto_exposure area (for avg_lum) stopx (hsize = (0x1e - 0x1c) * 32)
47 0x1f auto_exposure area (for avg_lum) stopy (vsize = (0x1f - 0x1d) * 32)
48 */
49 
50 #define MODULE_NAME "sonixb"
51 
52 #include <linux/input.h>
53 #include "gspca.h"
54 
55 MODULE_AUTHOR("Jean-Fran├žois Moine <http://moinejf.free.fr>");
56 MODULE_DESCRIPTION("GSPCA/SN9C102 USB Camera Driver");
57 MODULE_LICENSE("GPL");
58 
59 /* specific webcam descriptor */
60 struct sd {
61  struct gspca_dev gspca_dev; /* !! must be the first item */
62 
63  struct v4l2_ctrl *brightness;
64  struct v4l2_ctrl *plfreq;
65 
70  u8 header[12]; /* Header without sof marker */
71 
72  unsigned char autogain_ignore_frames;
73  unsigned char frames_to_drop;
74 
75  __u8 bridge; /* Type of bridge */
76 #define BRIDGE_101 0
77 #define BRIDGE_102 0 /* We make no difference between 101 and 102 */
78 #define BRIDGE_103 1
79 
80  __u8 sensor; /* Type of image sensor chip */
81 #define SENSOR_HV7131D 0
82 #define SENSOR_HV7131R 1
83 #define SENSOR_OV6650 2
84 #define SENSOR_OV7630 3
85 #define SENSOR_PAS106 4
86 #define SENSOR_PAS202 5
87 #define SENSOR_TAS5110C 6
88 #define SENSOR_TAS5110D 7
89 #define SENSOR_TAS5130CXX 8
91 };
92 
93 typedef const __u8 sensor_init_t[8];
94 
95 struct sensor_data {
96  const __u8 *bridge_init;
99  int flags;
101 };
102 
103 /* sensor_data flags */
104 #define F_SIF 0x01 /* sif or vga */
105 
106 /* priv field of struct v4l2_pix_format flags (do not use low nibble!) */
107 #define MODE_RAW 0x10 /* raw bayer mode */
108 #define MODE_REDUCED_SIF 0x20 /* vga mode (320x240 / 160x120) on sif cam */
109 
110 #define COMP 0xc7 /* 0x87 //0x07 */
111 #define COMP1 0xc9 /* 0x89 //0x09 */
112 
113 #define MCK_INIT 0x63
114 #define MCK_INIT1 0x20 /*fixme: Bayer - 0x50 for JPEG ??*/
115 
116 #define SYS_CLK 0x04
117 
118 #define SENS(bridge, sensor, _flags, _sensor_addr) \
119 { \
120  .bridge_init = bridge, \
121  .sensor_init = sensor, \
122  .sensor_init_size = sizeof(sensor), \
123  .flags = _flags, .sensor_addr = _sensor_addr \
124 }
125 
126 /* We calculate the autogain at the end of the transfer of a frame, at this
127  moment a frame with the old settings is being captured and transmitted. So
128  if we adjust the gain or exposure we must ignore atleast the next frame for
129  the new settings to come into effect before doing any other adjustments. */
130 #define AUTOGAIN_IGNORE_FRAMES 1
131 
132 static const struct v4l2_pix_format vga_mode[] = {
134  .bytesperline = 160,
135  .sizeimage = 160 * 120,
136  .colorspace = V4L2_COLORSPACE_SRGB,
137  .priv = 2 | MODE_RAW},
139  .bytesperline = 160,
140  .sizeimage = 160 * 120 * 5 / 4,
141  .colorspace = V4L2_COLORSPACE_SRGB,
142  .priv = 2},
144  .bytesperline = 320,
145  .sizeimage = 320 * 240 * 5 / 4,
146  .colorspace = V4L2_COLORSPACE_SRGB,
147  .priv = 1},
149  .bytesperline = 640,
150  .sizeimage = 640 * 480 * 5 / 4,
151  .colorspace = V4L2_COLORSPACE_SRGB,
152  .priv = 0},
153 };
154 static const struct v4l2_pix_format sif_mode[] = {
156  .bytesperline = 160,
157  .sizeimage = 160 * 120,
158  .colorspace = V4L2_COLORSPACE_SRGB,
159  .priv = 1 | MODE_RAW | MODE_REDUCED_SIF},
161  .bytesperline = 160,
162  .sizeimage = 160 * 120 * 5 / 4,
163  .colorspace = V4L2_COLORSPACE_SRGB,
164  .priv = 1 | MODE_REDUCED_SIF},
166  .bytesperline = 176,
167  .sizeimage = 176 * 144,
168  .colorspace = V4L2_COLORSPACE_SRGB,
169  .priv = 1 | MODE_RAW},
171  .bytesperline = 176,
172  .sizeimage = 176 * 144 * 5 / 4,
173  .colorspace = V4L2_COLORSPACE_SRGB,
174  .priv = 1},
176  .bytesperline = 320,
177  .sizeimage = 320 * 240 * 5 / 4,
178  .colorspace = V4L2_COLORSPACE_SRGB,
179  .priv = 0 | MODE_REDUCED_SIF},
181  .bytesperline = 352,
182  .sizeimage = 352 * 288 * 5 / 4,
183  .colorspace = V4L2_COLORSPACE_SRGB,
184  .priv = 0},
185 };
186 
187 static const __u8 initHv7131d[] = {
188  0x04, 0x03, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,
189  0x00, 0x00,
190  0x00, 0x00, 0x00, 0x02, 0x02, 0x00,
191  0x28, 0x1e, 0x60, 0x8e, 0x42,
192 };
193 static const __u8 hv7131d_sensor_init[][8] = {
194  {0xa0, 0x11, 0x01, 0x04, 0x00, 0x00, 0x00, 0x17},
195  {0xa0, 0x11, 0x02, 0x00, 0x00, 0x00, 0x00, 0x17},
196  {0xa0, 0x11, 0x28, 0x00, 0x00, 0x00, 0x00, 0x17},
197  {0xa0, 0x11, 0x30, 0x30, 0x00, 0x00, 0x00, 0x17}, /* reset level */
198  {0xa0, 0x11, 0x34, 0x02, 0x00, 0x00, 0x00, 0x17}, /* pixel bias volt */
199 };
200 
201 static const __u8 initHv7131r[] = {
202  0x46, 0x77, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,
203  0x00, 0x00,
204  0x00, 0x00, 0x00, 0x02, 0x01, 0x00,
205  0x28, 0x1e, 0x60, 0x8a, 0x20,
206 };
207 static const __u8 hv7131r_sensor_init[][8] = {
208  {0xc0, 0x11, 0x31, 0x38, 0x2a, 0x2e, 0x00, 0x10},
209  {0xa0, 0x11, 0x01, 0x08, 0x2a, 0x2e, 0x00, 0x10},
210  {0xb0, 0x11, 0x20, 0x00, 0xd0, 0x2e, 0x00, 0x10},
211  {0xc0, 0x11, 0x25, 0x03, 0x0e, 0x28, 0x00, 0x16},
212  {0xa0, 0x11, 0x30, 0x10, 0x0e, 0x28, 0x00, 0x15},
213 };
214 static const __u8 initOv6650[] = {
215  0x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
216  0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
217  0x00, 0x01, 0x01, 0x0a, 0x16, 0x12, 0x68, 0x8b,
218  0x10,
219 };
220 static const __u8 ov6650_sensor_init[][8] = {
221  /* Bright, contrast, etc are set through SCBB interface.
222  * AVCAP on win2 do not send any data on this controls. */
223  /* Anyway, some registers appears to alter bright and constrat */
224 
225  /* Reset sensor */
226  {0xa0, 0x60, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
227  /* Set clock register 0x11 low nibble is clock divider */
228  {0xd0, 0x60, 0x11, 0xc0, 0x1b, 0x18, 0xc1, 0x10},
229  /* Next some unknown stuff */
230  {0xb0, 0x60, 0x15, 0x00, 0x02, 0x18, 0xc1, 0x10},
231 /* {0xa0, 0x60, 0x1b, 0x01, 0x02, 0x18, 0xc1, 0x10},
232  * THIS SET GREEN SCREEN
233  * (pixels could be innverted in decode kind of "brg",
234  * but blue wont be there. Avoid this data ... */
235  {0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10}, /* format out? */
236  {0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10},
237  {0xa0, 0x60, 0x30, 0x3d, 0x0a, 0xd8, 0xa4, 0x10},
238  /* Enable rgb brightness control */
239  {0xa0, 0x60, 0x61, 0x08, 0x00, 0x00, 0x00, 0x10},
240  /* HDG: Note windows uses the line below, which sets both register 0x60
241  and 0x61 I believe these registers of the ov6650 are identical as
242  those of the ov7630, because if this is true the windows settings
243  add a bit additional red gain and a lot additional blue gain, which
244  matches my findings that the windows settings make blue much too
245  blue and red a little too red.
246  {0xb0, 0x60, 0x60, 0x66, 0x68, 0xd8, 0xa4, 0x10}, */
247  /* Some more unknown stuff */
248  {0xa0, 0x60, 0x68, 0x04, 0x68, 0xd8, 0xa4, 0x10},
249  {0xd0, 0x60, 0x17, 0x24, 0xd6, 0x04, 0x94, 0x10}, /* Clipreg */
250 };
251 
252 static const __u8 initOv7630[] = {
253  0x04, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, /* r01 .. r08 */
254  0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* r09 .. r10 */
255  0x00, 0x01, 0x01, 0x0a, /* r11 .. r14 */
256  0x28, 0x1e, /* H & V sizes r15 .. r16 */
257  0x68, 0x8f, MCK_INIT1, /* r17 .. r19 */
258 };
259 static const __u8 ov7630_sensor_init[][8] = {
260  {0xa0, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
261  {0xb0, 0x21, 0x01, 0x77, 0x3a, 0x00, 0x00, 0x10},
262 /* {0xd0, 0x21, 0x12, 0x7c, 0x01, 0x80, 0x34, 0x10}, jfm */
263  {0xd0, 0x21, 0x12, 0x5c, 0x00, 0x80, 0x34, 0x10}, /* jfm */
264  {0xa0, 0x21, 0x1b, 0x04, 0x00, 0x80, 0x34, 0x10},
265  {0xa0, 0x21, 0x20, 0x44, 0x00, 0x80, 0x34, 0x10},
266  {0xa0, 0x21, 0x23, 0xee, 0x00, 0x80, 0x34, 0x10},
267  {0xd0, 0x21, 0x26, 0xa0, 0x9a, 0xa0, 0x30, 0x10},
268  {0xb0, 0x21, 0x2a, 0x80, 0x00, 0xa0, 0x30, 0x10},
269  {0xb0, 0x21, 0x2f, 0x3d, 0x24, 0xa0, 0x30, 0x10},
270  {0xa0, 0x21, 0x32, 0x86, 0x24, 0xa0, 0x30, 0x10},
271  {0xb0, 0x21, 0x60, 0xa9, 0x4a, 0xa0, 0x30, 0x10},
272 /* {0xb0, 0x21, 0x60, 0xa9, 0x42, 0xa0, 0x30, 0x10}, * jfm */
273  {0xa0, 0x21, 0x65, 0x00, 0x42, 0xa0, 0x30, 0x10},
274  {0xa0, 0x21, 0x69, 0x38, 0x42, 0xa0, 0x30, 0x10},
275  {0xc0, 0x21, 0x6f, 0x88, 0x0b, 0x00, 0x30, 0x10},
276  {0xc0, 0x21, 0x74, 0x21, 0x8e, 0x00, 0x30, 0x10},
277  {0xa0, 0x21, 0x7d, 0xf7, 0x8e, 0x00, 0x30, 0x10},
278  {0xd0, 0x21, 0x17, 0x1c, 0xbd, 0x06, 0xf6, 0x10},
279 };
280 
281 static const __u8 initPas106[] = {
282  0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x40, 0x00, 0x00, 0x00,
283  0x00, 0x00,
284  0x00, 0x00, 0x00, 0x04, 0x01, 0x00,
285  0x16, 0x12, 0x24, COMP1, MCK_INIT1,
286 };
287 /* compression 0x86 mckinit1 0x2b */
288 
289 /* "Known" PAS106B registers:
290  0x02 clock divider
291  0x03 Variable framerate bits 4-11
292  0x04 Var framerate bits 0-3, one must leave the 4 msb's at 0 !!
293  The variable framerate control must never be set lower then 300,
294  which sets the framerate at 90 / reg02, otherwise vsync is lost.
295  0x05 Shutter Time Line Offset, this can be used as an exposure control:
296  0 = use full frame time, 255 = no exposure at all
297  Note this may never be larger then "var-framerate control" / 2 - 2.
298  When var-framerate control is < 514, no exposure is reached at the max
299  allowed value for the framerate control value, rather then at 255.
300  0x06 Shutter Time Pixel Offset, like reg05 this influences exposure, but
301  only a very little bit, leave at 0xcd
302  0x07 offset sign bit (bit0 1 > negative offset)
303  0x08 offset
304  0x09 Blue Gain
305  0x0a Green1 Gain
306  0x0b Green2 Gain
307  0x0c Red Gain
308  0x0e Global gain
309  0x13 Write 1 to commit settings to sensor
310 */
311 
312 static const __u8 pas106_sensor_init[][8] = {
313  /* Pixel Clock Divider 6 */
314  { 0xa1, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x14 },
315  /* Frame Time MSB (also seen as 0x12) */
316  { 0xa1, 0x40, 0x03, 0x13, 0x00, 0x00, 0x00, 0x14 },
317  /* Frame Time LSB (also seen as 0x05) */
318  { 0xa1, 0x40, 0x04, 0x06, 0x00, 0x00, 0x00, 0x14 },
319  /* Shutter Time Line Offset (also seen as 0x6d) */
320  { 0xa1, 0x40, 0x05, 0x65, 0x00, 0x00, 0x00, 0x14 },
321  /* Shutter Time Pixel Offset (also seen as 0xb1) */
322  { 0xa1, 0x40, 0x06, 0xcd, 0x00, 0x00, 0x00, 0x14 },
323  /* Black Level Subtract Sign (also seen 0x00) */
324  { 0xa1, 0x40, 0x07, 0xc1, 0x00, 0x00, 0x00, 0x14 },
325  /* Black Level Subtract Level (also seen 0x01) */
326  { 0xa1, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x14 },
327  { 0xa1, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x14 },
328  /* Color Gain B Pixel 5 a */
329  { 0xa1, 0x40, 0x09, 0x05, 0x00, 0x00, 0x00, 0x14 },
330  /* Color Gain G1 Pixel 1 5 */
331  { 0xa1, 0x40, 0x0a, 0x04, 0x00, 0x00, 0x00, 0x14 },
332  /* Color Gain G2 Pixel 1 0 5 */
333  { 0xa1, 0x40, 0x0b, 0x04, 0x00, 0x00, 0x00, 0x14 },
334  /* Color Gain R Pixel 3 1 */
335  { 0xa1, 0x40, 0x0c, 0x05, 0x00, 0x00, 0x00, 0x14 },
336  /* Color GainH Pixel */
337  { 0xa1, 0x40, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x14 },
338  /* Global Gain */
339  { 0xa1, 0x40, 0x0e, 0x0e, 0x00, 0x00, 0x00, 0x14 },
340  /* Contrast */
341  { 0xa1, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x14 },
342  /* H&V synchro polarity */
343  { 0xa1, 0x40, 0x10, 0x06, 0x00, 0x00, 0x00, 0x14 },
344  /* ?default */
345  { 0xa1, 0x40, 0x11, 0x06, 0x00, 0x00, 0x00, 0x14 },
346  /* DAC scale */
347  { 0xa1, 0x40, 0x12, 0x06, 0x00, 0x00, 0x00, 0x14 },
348  /* ?default */
349  { 0xa1, 0x40, 0x14, 0x02, 0x00, 0x00, 0x00, 0x14 },
350  /* Validate Settings */
351  { 0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14 },
352 };
353 
354 static const __u8 initPas202[] = {
355  0x44, 0x44, 0x21, 0x30, 0x00, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0x00,
356  0x00, 0x00,
357  0x00, 0x00, 0x00, 0x06, 0x03, 0x0a,
358  0x28, 0x1e, 0x20, 0x89, 0x20,
359 };
360 
361 /* "Known" PAS202BCB registers:
362  0x02 clock divider
363  0x04 Variable framerate bits 6-11 (*)
364  0x05 Var framerate bits 0-5, one must leave the 2 msb's at 0 !!
365  0x07 Blue Gain
366  0x08 Green Gain
367  0x09 Red Gain
368  0x0b offset sign bit (bit0 1 > negative offset)
369  0x0c offset
370  0x0e Unknown image is slightly brighter when bit 0 is 0, if reg0f is 0 too,
371  leave at 1 otherwise we get a jump in our exposure control
372  0x0f Exposure 0-255, 0 = use full frame time, 255 = no exposure at all
373  0x10 Master gain 0 - 31
374  0x11 write 1 to apply changes
375  (*) The variable framerate control must never be set lower then 500
376  which sets the framerate at 30 / reg02, otherwise vsync is lost.
377 */
378 static const __u8 pas202_sensor_init[][8] = {
379  /* Set the clock divider to 4 -> 30 / 4 = 7.5 fps, we would like
380  to set it lower, but for some reason the bridge starts missing
381  vsync's then */
382  {0xa0, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x10},
383  {0xd0, 0x40, 0x04, 0x07, 0x34, 0x00, 0x09, 0x10},
384  {0xd0, 0x40, 0x08, 0x01, 0x00, 0x00, 0x01, 0x10},
385  {0xd0, 0x40, 0x0c, 0x00, 0x0c, 0x01, 0x32, 0x10},
386  {0xd0, 0x40, 0x10, 0x00, 0x01, 0x00, 0x63, 0x10},
387  {0xa0, 0x40, 0x15, 0x70, 0x01, 0x00, 0x63, 0x10},
388  {0xa0, 0x40, 0x18, 0x00, 0x01, 0x00, 0x63, 0x10},
389  {0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
390  {0xa0, 0x40, 0x03, 0x56, 0x01, 0x00, 0x63, 0x10},
391  {0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
392 };
393 
394 static const __u8 initTas5110c[] = {
395  0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
396  0x00, 0x00,
397  0x00, 0x00, 0x00, 0x45, 0x09, 0x0a,
398  0x16, 0x12, 0x60, 0x86, 0x2b,
399 };
400 /* Same as above, except a different hstart */
401 static const __u8 initTas5110d[] = {
402  0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
403  0x00, 0x00,
404  0x00, 0x00, 0x00, 0x41, 0x09, 0x0a,
405  0x16, 0x12, 0x60, 0x86, 0x2b,
406 };
407 /* tas5110c is 3 wire, tas5110d is 2 wire (regular i2c) */
408 static const __u8 tas5110c_sensor_init[][8] = {
409  {0x30, 0x11, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x10},
410  {0x30, 0x11, 0x02, 0x20, 0xa9, 0x00, 0x00, 0x10},
411 };
412 /* Known TAS5110D registers
413  * reg02: gain, bit order reversed!! 0 == max gain, 255 == min gain
414  * reg03: bit3: vflip, bit4: ~hflip, bit7: ~gainboost (~ == inverted)
415  * Note: writing reg03 seems to only work when written together with 02
416  */
417 static const __u8 tas5110d_sensor_init[][8] = {
418  {0xa0, 0x61, 0x9a, 0xca, 0x00, 0x00, 0x00, 0x17}, /* reset */
419 };
420 
421 static const __u8 initTas5130[] = {
422  0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
423  0x00, 0x00,
424  0x00, 0x00, 0x00, 0x68, 0x0c, 0x0a,
425  0x28, 0x1e, 0x60, COMP, MCK_INIT,
426 };
427 static const __u8 tas5130_sensor_init[][8] = {
428 /* {0x30, 0x11, 0x00, 0x40, 0x47, 0x00, 0x00, 0x10},
429  * shutter 0x47 short exposure? */
430  {0x30, 0x11, 0x00, 0x40, 0x01, 0x00, 0x00, 0x10},
431  /* shutter 0x01 long exposure */
432  {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10},
433 };
434 
435 static const struct sensor_data sensor_data[] = {
436  SENS(initHv7131d, hv7131d_sensor_init, 0, 0),
437  SENS(initHv7131r, hv7131r_sensor_init, 0, 0),
438  SENS(initOv6650, ov6650_sensor_init, F_SIF, 0x60),
439  SENS(initOv7630, ov7630_sensor_init, 0, 0x21),
440  SENS(initPas106, pas106_sensor_init, F_SIF, 0),
441  SENS(initPas202, pas202_sensor_init, 0, 0),
442  SENS(initTas5110c, tas5110c_sensor_init, F_SIF, 0),
443  SENS(initTas5110d, tas5110d_sensor_init, F_SIF, 0),
444  SENS(initTas5130, tas5130_sensor_init, 0, 0),
445 };
446 
447 /* get one byte in gspca_dev->usb_buf */
448 static void reg_r(struct gspca_dev *gspca_dev,
449  __u16 value)
450 {
451  int res;
452 
453  if (gspca_dev->usb_err < 0)
454  return;
455 
456  res = usb_control_msg(gspca_dev->dev,
457  usb_rcvctrlpipe(gspca_dev->dev, 0),
458  0, /* request */
460  value,
461  0, /* index */
462  gspca_dev->usb_buf, 1,
463  500);
464 
465  if (res < 0) {
466  dev_err(gspca_dev->v4l2_dev.dev,
467  "Error reading register %02x: %d\n", value, res);
468  gspca_dev->usb_err = res;
469  }
470 }
471 
472 static void reg_w(struct gspca_dev *gspca_dev,
473  __u16 value,
474  const __u8 *buffer,
475  int len)
476 {
477  int res;
478 
479  if (gspca_dev->usb_err < 0)
480  return;
481 
482  memcpy(gspca_dev->usb_buf, buffer, len);
483  res = usb_control_msg(gspca_dev->dev,
484  usb_sndctrlpipe(gspca_dev->dev, 0),
485  0x08, /* request */
487  value,
488  0, /* index */
489  gspca_dev->usb_buf, len,
490  500);
491 
492  if (res < 0) {
493  dev_err(gspca_dev->v4l2_dev.dev,
494  "Error writing register %02x: %d\n", value, res);
495  gspca_dev->usb_err = res;
496  }
497 }
498 
499 static void i2c_w(struct gspca_dev *gspca_dev, const __u8 *buffer)
500 {
501  int retry = 60;
502 
503  if (gspca_dev->usb_err < 0)
504  return;
505 
506  /* is i2c ready */
507  reg_w(gspca_dev, 0x08, buffer, 8);
508  while (retry--) {
509  if (gspca_dev->usb_err < 0)
510  return;
511  msleep(10);
512  reg_r(gspca_dev, 0x08);
513  if (gspca_dev->usb_buf[0] & 0x04) {
514  if (gspca_dev->usb_buf[0] & 0x08) {
515  dev_err(gspca_dev->v4l2_dev.dev,
516  "i2c write error\n");
517  gspca_dev->usb_err = -EIO;
518  }
519  return;
520  }
521  }
522 
523  dev_err(gspca_dev->v4l2_dev.dev, "i2c write timeout\n");
524  gspca_dev->usb_err = -EIO;
525 }
526 
527 static void i2c_w_vector(struct gspca_dev *gspca_dev,
528  const __u8 buffer[][8], int len)
529 {
530  for (;;) {
531  if (gspca_dev->usb_err < 0)
532  return;
533  reg_w(gspca_dev, 0x08, *buffer, 8);
534  len -= 8;
535  if (len <= 0)
536  break;
537  buffer++;
538  }
539 }
540 
541 static void setbrightness(struct gspca_dev *gspca_dev)
542 {
543  struct sd *sd = (struct sd *) gspca_dev;
544 
545  switch (sd->sensor) {
546  case SENSOR_OV6650:
547  case SENSOR_OV7630: {
548  __u8 i2cOV[] =
549  {0xa0, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10};
550 
551  /* change reg 0x06 */
552  i2cOV[1] = sensor_data[sd->sensor].sensor_addr;
553  i2cOV[3] = sd->brightness->val;
554  i2c_w(gspca_dev, i2cOV);
555  break;
556  }
557  case SENSOR_PAS106:
558  case SENSOR_PAS202: {
559  __u8 i2cpbright[] =
560  {0xb0, 0x40, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x16};
561  __u8 i2cpdoit[] =
562  {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
563 
564  /* PAS106 uses reg 7 and 8 instead of b and c */
565  if (sd->sensor == SENSOR_PAS106) {
566  i2cpbright[2] = 7;
567  i2cpdoit[2] = 0x13;
568  }
569 
570  if (sd->brightness->val < 127) {
571  /* change reg 0x0b, signreg */
572  i2cpbright[3] = 0x01;
573  /* set reg 0x0c, offset */
574  i2cpbright[4] = 127 - sd->brightness->val;
575  } else
576  i2cpbright[4] = sd->brightness->val - 127;
577 
578  i2c_w(gspca_dev, i2cpbright);
579  i2c_w(gspca_dev, i2cpdoit);
580  break;
581  }
582  default:
583  break;
584  }
585 }
586 
587 static void setgain(struct gspca_dev *gspca_dev)
588 {
589  struct sd *sd = (struct sd *) gspca_dev;
590  u8 gain = gspca_dev->gain->val;
591 
592  switch (sd->sensor) {
593  case SENSOR_HV7131D: {
594  __u8 i2c[] =
595  {0xc0, 0x11, 0x31, 0x00, 0x00, 0x00, 0x00, 0x17};
596 
597  i2c[3] = 0x3f - gain;
598  i2c[4] = 0x3f - gain;
599  i2c[5] = 0x3f - gain;
600 
601  i2c_w(gspca_dev, i2c);
602  break;
603  }
604  case SENSOR_TAS5110C:
605  case SENSOR_TAS5130CXX: {
606  __u8 i2c[] =
607  {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};
608 
609  i2c[4] = 255 - gain;
610  i2c_w(gspca_dev, i2c);
611  break;
612  }
613  case SENSOR_TAS5110D: {
614  __u8 i2c[] = {
615  0xb0, 0x61, 0x02, 0x00, 0x10, 0x00, 0x00, 0x17 };
616  gain = 255 - gain;
617  /* The bits in the register are the wrong way around!! */
618  i2c[3] |= (gain & 0x80) >> 7;
619  i2c[3] |= (gain & 0x40) >> 5;
620  i2c[3] |= (gain & 0x20) >> 3;
621  i2c[3] |= (gain & 0x10) >> 1;
622  i2c[3] |= (gain & 0x08) << 1;
623  i2c[3] |= (gain & 0x04) << 3;
624  i2c[3] |= (gain & 0x02) << 5;
625  i2c[3] |= (gain & 0x01) << 7;
626  i2c_w(gspca_dev, i2c);
627  break;
628  }
629  case SENSOR_OV6650:
630  case SENSOR_OV7630: {
631  __u8 i2c[] = {0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
632 
633  /*
634  * The ov7630's gain is weird, at 32 the gain drops to the
635  * same level as at 16, so skip 32-47 (of the 0-63 scale).
636  */
637  if (sd->sensor == SENSOR_OV7630 && gain >= 32)
638  gain += 16;
639 
640  i2c[1] = sensor_data[sd->sensor].sensor_addr;
641  i2c[3] = gain;
642  i2c_w(gspca_dev, i2c);
643  break;
644  }
645  case SENSOR_PAS106:
646  case SENSOR_PAS202: {
647  __u8 i2cpgain[] =
648  {0xa0, 0x40, 0x10, 0x00, 0x00, 0x00, 0x00, 0x15};
649  __u8 i2cpcolorgain[] =
650  {0xc0, 0x40, 0x07, 0x00, 0x00, 0x00, 0x00, 0x15};
651  __u8 i2cpdoit[] =
652  {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
653 
654  /* PAS106 uses different regs (and has split green gains) */
655  if (sd->sensor == SENSOR_PAS106) {
656  i2cpgain[2] = 0x0e;
657  i2cpcolorgain[0] = 0xd0;
658  i2cpcolorgain[2] = 0x09;
659  i2cpdoit[2] = 0x13;
660  }
661 
662  i2cpgain[3] = gain;
663  i2cpcolorgain[3] = gain >> 1;
664  i2cpcolorgain[4] = gain >> 1;
665  i2cpcolorgain[5] = gain >> 1;
666  i2cpcolorgain[6] = gain >> 1;
667 
668  i2c_w(gspca_dev, i2cpgain);
669  i2c_w(gspca_dev, i2cpcolorgain);
670  i2c_w(gspca_dev, i2cpdoit);
671  break;
672  }
673  default:
674  if (sd->bridge == BRIDGE_103) {
675  u8 buf[3] = { gain, gain, gain }; /* R, G, B */
676  reg_w(gspca_dev, 0x05, buf, 3);
677  } else {
678  u8 buf[2];
679  buf[0] = gain << 4 | gain; /* Red and blue */
680  buf[1] = gain; /* Green */
681  reg_w(gspca_dev, 0x10, buf, 2);
682  }
683  }
684 }
685 
686 static void setexposure(struct gspca_dev *gspca_dev)
687 {
688  struct sd *sd = (struct sd *) gspca_dev;
689 
690  switch (sd->sensor) {
691  case SENSOR_HV7131D: {
692  /* Note the datasheet wrongly says line mode exposure uses reg
693  0x26 and 0x27, testing has shown 0x25 + 0x26 */
694  __u8 i2c[] = {0xc0, 0x11, 0x25, 0x00, 0x00, 0x00, 0x00, 0x17};
695  u16 reg = gspca_dev->exposure->val;
696 
697  i2c[3] = reg >> 8;
698  i2c[4] = reg & 0xff;
699  i2c_w(gspca_dev, i2c);
700  break;
701  }
702  case SENSOR_TAS5110C:
703  case SENSOR_TAS5110D: {
704  /* register 19's high nibble contains the sn9c10x clock divider
705  The high nibble configures the no fps according to the
706  formula: 60 / high_nibble. With a maximum of 30 fps */
707  u8 reg = gspca_dev->exposure->val;
708 
709  reg = (reg << 4) | 0x0b;
710  reg_w(gspca_dev, 0x19, &reg, 1);
711  break;
712  }
713  case SENSOR_OV6650:
714  case SENSOR_OV7630: {
715  /* The ov6650 / ov7630 have 2 registers which both influence
716  exposure, register 11, whose low nibble sets the nr off fps
717  according to: fps = 30 / (low_nibble + 1)
718 
719  The fps configures the maximum exposure setting, but it is
720  possible to use less exposure then what the fps maximum
721  allows by setting register 10. register 10 configures the
722  actual exposure as quotient of the full exposure, with 0
723  being no exposure at all (not very useful) and reg10_max
724  being max exposure possible at that framerate.
725 
726  The code maps our 0 - 510 ms exposure ctrl to these 2
727  registers, trying to keep fps as high as possible.
728  */
729  __u8 i2c[] = {0xb0, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10};
730  int reg10, reg11, reg10_max;
731 
732  /* ov6645 datasheet says reg10_max is 9a, but that uses
733  tline * 2 * reg10 as formula for calculating texpo, the
734  ov6650 probably uses the same formula as the 7730 which uses
735  tline * 4 * reg10, which explains why the reg10max we've
736  found experimentally for the ov6650 is exactly half that of
737  the ov6645. The ov7630 datasheet says the max is 0x41. */
738  if (sd->sensor == SENSOR_OV6650) {
739  reg10_max = 0x4d;
740  i2c[4] = 0xc0; /* OV6650 needs non default vsync pol */
741  } else
742  reg10_max = 0x41;
743 
744  reg11 = (15 * gspca_dev->exposure->val + 999) / 1000;
745  if (reg11 < 1)
746  reg11 = 1;
747  else if (reg11 > 16)
748  reg11 = 16;
749 
750  /* In 640x480, if the reg11 has less than 4, the image is
751  unstable (the bridge goes into a higher compression mode
752  which we have not reverse engineered yet). */
753  if (gspca_dev->width == 640 && reg11 < 4)
754  reg11 = 4;
755 
756  /* frame exposure time in ms = 1000 * reg11 / 30 ->
757  reg10 = (gspca_dev->exposure->val / 2) * reg10_max
758  / (1000 * reg11 / 30) */
759  reg10 = (gspca_dev->exposure->val * 15 * reg10_max)
760  / (1000 * reg11);
761 
762  /* Don't allow this to get below 10 when using autogain, the
763  steps become very large (relatively) when below 10 causing
764  the image to oscilate from much too dark, to much too bright
765  and back again. */
766  if (gspca_dev->autogain->val && reg10 < 10)
767  reg10 = 10;
768  else if (reg10 > reg10_max)
769  reg10 = reg10_max;
770 
771  /* Write reg 10 and reg11 low nibble */
772  i2c[1] = sensor_data[sd->sensor].sensor_addr;
773  i2c[3] = reg10;
774  i2c[4] |= reg11 - 1;
775 
776  /* If register 11 didn't change, don't change it */
777  if (sd->reg11 == reg11)
778  i2c[0] = 0xa0;
779 
780  i2c_w(gspca_dev, i2c);
781  if (gspca_dev->usb_err == 0)
782  sd->reg11 = reg11;
783  break;
784  }
785  case SENSOR_PAS202: {
786  __u8 i2cpframerate[] =
787  {0xb0, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x16};
788  __u8 i2cpexpo[] =
789  {0xa0, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x16};
790  const __u8 i2cpdoit[] =
791  {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
792  int framerate_ctrl;
793 
794  /* The exposure knee for the autogain algorithm is 200
795  (100 ms / 10 fps on other sensors), for values below this
796  use the control for setting the partial frame expose time,
797  above that use variable framerate. This way we run at max
798  framerate ([email protected] fps, 320x240@10fps) until the knee
799  is reached. Using the variable framerate control above 200
800  is better then playing around with both clockdiv + partial
801  frame exposure times (like we are doing with the ov chips),
802  as that sometimes leads to jumps in the exposure control,
803  which are bad for auto exposure. */
804  if (gspca_dev->exposure->val < 200) {
805  i2cpexpo[3] = 255 - (gspca_dev->exposure->val * 255)
806  / 200;
807  framerate_ctrl = 500;
808  } else {
809  /* The PAS202's exposure control goes from 0 - 4095,
810  but anything below 500 causes vsync issues, so scale
811  our 200-1023 to 500-4095 */
812  framerate_ctrl = (gspca_dev->exposure->val - 200)
813  * 1000 / 229 + 500;
814  }
815 
816  i2cpframerate[3] = framerate_ctrl >> 6;
817  i2cpframerate[4] = framerate_ctrl & 0x3f;
818  i2c_w(gspca_dev, i2cpframerate);
819  i2c_w(gspca_dev, i2cpexpo);
820  i2c_w(gspca_dev, i2cpdoit);
821  break;
822  }
823  case SENSOR_PAS106: {
824  __u8 i2cpframerate[] =
825  {0xb1, 0x40, 0x03, 0x00, 0x00, 0x00, 0x00, 0x14};
826  __u8 i2cpexpo[] =
827  {0xa1, 0x40, 0x05, 0x00, 0x00, 0x00, 0x00, 0x14};
828  const __u8 i2cpdoit[] =
829  {0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14};
830  int framerate_ctrl;
831 
832  /* For values below 150 use partial frame exposure, above
833  that use framerate ctrl */
834  if (gspca_dev->exposure->val < 150) {
835  i2cpexpo[3] = 150 - gspca_dev->exposure->val;
836  framerate_ctrl = 300;
837  } else {
838  /* The PAS106's exposure control goes from 0 - 4095,
839  but anything below 300 causes vsync issues, so scale
840  our 150-1023 to 300-4095 */
841  framerate_ctrl = (gspca_dev->exposure->val - 150)
842  * 1000 / 230 + 300;
843  }
844 
845  i2cpframerate[3] = framerate_ctrl >> 4;
846  i2cpframerate[4] = framerate_ctrl & 0x0f;
847  i2c_w(gspca_dev, i2cpframerate);
848  i2c_w(gspca_dev, i2cpexpo);
849  i2c_w(gspca_dev, i2cpdoit);
850  break;
851  }
852  default:
853  break;
854  }
855 }
856 
857 static void setfreq(struct gspca_dev *gspca_dev)
858 {
859  struct sd *sd = (struct sd *) gspca_dev;
860 
861  if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630) {
862  /* Framerate adjust register for artificial light 50 hz flicker
863  compensation, for the ov6650 this is identical to ov6630
864  0x2b register, see ov6630 datasheet.
865  0x4f / 0x8a -> (30 fps -> 25 fps), 0x00 -> no adjustment */
866  __u8 i2c[] = {0xa0, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10};
867  switch (sd->plfreq->val) {
868  default:
869 /* case 0: * no filter*/
870 /* case 2: * 60 hz */
871  i2c[3] = 0;
872  break;
873  case 1: /* 50 hz */
874  i2c[3] = (sd->sensor == SENSOR_OV6650)
875  ? 0x4f : 0x8a;
876  break;
877  }
878  i2c[1] = sensor_data[sd->sensor].sensor_addr;
879  i2c_w(gspca_dev, i2c);
880  }
881 }
882 
883 static void do_autogain(struct gspca_dev *gspca_dev)
884 {
885  struct sd *sd = (struct sd *) gspca_dev;
886  int deadzone, desired_avg_lum, avg_lum;
887 
888  avg_lum = atomic_read(&sd->avg_lum);
889  if (avg_lum == -1)
890  return;
891 
892  if (sd->autogain_ignore_frames > 0) {
894  return;
895  }
896 
897  /* SIF / VGA sensors have a different autoexposure area and thus
898  different avg_lum values for the same picture brightness */
899  if (sensor_data[sd->sensor].flags & F_SIF) {
900  deadzone = 500;
901  /* SIF sensors tend to overexpose, so keep this small */
902  desired_avg_lum = 5000;
903  } else {
904  deadzone = 1500;
905  desired_avg_lum = 13000;
906  }
907 
908  if (sd->brightness)
909  desired_avg_lum = sd->brightness->val * desired_avg_lum / 127;
910 
911  if (gspca_dev->exposure->maximum < 500) {
912  if (gspca_coarse_grained_expo_autogain(gspca_dev, avg_lum,
913  desired_avg_lum, deadzone))
915  } else {
916  int gain_knee = gspca_dev->gain->maximum * 9 / 10;
917  if (gspca_expo_autogain(gspca_dev, avg_lum, desired_avg_lum,
918  deadzone, gain_knee, sd->exposure_knee))
920  }
921 }
922 
923 /* this function is called at probe time */
924 static int sd_config(struct gspca_dev *gspca_dev,
925  const struct usb_device_id *id)
926 {
927  struct sd *sd = (struct sd *) gspca_dev;
928  struct cam *cam;
929 
930  reg_r(gspca_dev, 0x00);
931  if (gspca_dev->usb_buf[0] != 0x10)
932  return -ENODEV;
933 
934  /* copy the webcam info from the device id */
935  sd->sensor = id->driver_info >> 8;
936  sd->bridge = id->driver_info & 0xff;
937 
938  cam = &gspca_dev->cam;
939  if (!(sensor_data[sd->sensor].flags & F_SIF)) {
940  cam->cam_mode = vga_mode;
941  cam->nmodes = ARRAY_SIZE(vga_mode);
942  } else {
943  cam->cam_mode = sif_mode;
944  cam->nmodes = ARRAY_SIZE(sif_mode);
945  }
946  cam->npkt = 36; /* 36 packets per ISOC message */
947 
948  return 0;
949 }
950 
951 /* this function is called at probe and resume time */
952 static int sd_init(struct gspca_dev *gspca_dev)
953 {
954  const __u8 stop = 0x09; /* Disable stream turn of LED */
955 
956  reg_w(gspca_dev, 0x01, &stop, 1);
957 
958  return gspca_dev->usb_err;
959 }
960 
961 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
962 {
963  struct gspca_dev *gspca_dev =
964  container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
965  struct sd *sd = (struct sd *)gspca_dev;
966 
967  gspca_dev->usb_err = 0;
968 
969  if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
970  /* when switching to autogain set defaults to make sure
971  we are on a valid point of the autogain gain /
972  exposure knee graph, and give this change time to
973  take effect before doing autogain. */
974  gspca_dev->gain->val = gspca_dev->gain->default_value;
975  gspca_dev->exposure->val = gspca_dev->exposure->default_value;
977  }
978 
979  if (!gspca_dev->streaming)
980  return 0;
981 
982  switch (ctrl->id) {
983  case V4L2_CID_BRIGHTNESS:
984  setbrightness(gspca_dev);
985  break;
986  case V4L2_CID_AUTOGAIN:
987  if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
988  setexposure(gspca_dev);
989  if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
990  setgain(gspca_dev);
991  break;
993  setfreq(gspca_dev);
994  break;
995  default:
996  return -EINVAL;
997  }
998  return gspca_dev->usb_err;
999 }
1000 
1001 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1002  .s_ctrl = sd_s_ctrl,
1003 };
1004 
1005 /* this function is called at probe time */
1006 static int sd_init_controls(struct gspca_dev *gspca_dev)
1007 {
1008  struct sd *sd = (struct sd *) gspca_dev;
1009  struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1010 
1011  gspca_dev->vdev.ctrl_handler = hdl;
1012  v4l2_ctrl_handler_init(hdl, 5);
1013 
1014  if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630 ||
1015  sd->sensor == SENSOR_PAS106 || sd->sensor == SENSOR_PAS202)
1016  sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1017  V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
1018 
1019  /* Gain range is sensor dependent */
1020  switch (sd->sensor) {
1021  case SENSOR_OV6650:
1022  case SENSOR_PAS106:
1023  case SENSOR_PAS202:
1024  gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1025  V4L2_CID_GAIN, 0, 31, 1, 15);
1026  break;
1027  case SENSOR_OV7630:
1028  gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1029  V4L2_CID_GAIN, 0, 47, 1, 31);
1030  break;
1031  case SENSOR_HV7131D:
1032  gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1033  V4L2_CID_GAIN, 0, 63, 1, 31);
1034  break;
1035  case SENSOR_TAS5110C:
1036  case SENSOR_TAS5110D:
1037  case SENSOR_TAS5130CXX:
1038  gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1039  V4L2_CID_GAIN, 0, 255, 1, 127);
1040  break;
1041  default:
1042  if (sd->bridge == BRIDGE_103) {
1043  gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1044  V4L2_CID_GAIN, 0, 127, 1, 63);
1045  } else {
1046  gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1047  V4L2_CID_GAIN, 0, 15, 1, 7);
1048  }
1049  }
1050 
1051  /* Exposure range is sensor dependent, and not all have exposure */
1052  switch (sd->sensor) {
1053  case SENSOR_HV7131D:
1054  gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1055  V4L2_CID_EXPOSURE, 0, 8191, 1, 482);
1056  sd->exposure_knee = 964;
1057  break;
1058  case SENSOR_OV6650:
1059  case SENSOR_OV7630:
1060  case SENSOR_PAS106:
1061  case SENSOR_PAS202:
1062  gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1063  V4L2_CID_EXPOSURE, 0, 1023, 1, 66);
1064  sd->exposure_knee = 200;
1065  break;
1066  case SENSOR_TAS5110C:
1067  case SENSOR_TAS5110D:
1068  gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1069  V4L2_CID_EXPOSURE, 2, 15, 1, 2);
1070  break;
1071  }
1072 
1073  if (gspca_dev->exposure) {
1074  gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1075  V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1076  }
1077 
1078  if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630)
1079  sd->plfreq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
1083 
1084  if (hdl->error) {
1085  pr_err("Could not initialize controls\n");
1086  return hdl->error;
1087  }
1088 
1089  if (gspca_dev->autogain)
1090  v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
1091 
1092  return 0;
1093 }
1094 
1095 /* -- start the camera -- */
1096 static int sd_start(struct gspca_dev *gspca_dev)
1097 {
1098  struct sd *sd = (struct sd *) gspca_dev;
1099  struct cam *cam = &gspca_dev->cam;
1100  int i, mode;
1101  __u8 regs[0x31];
1102 
1103  mode = cam->cam_mode[gspca_dev->curr_mode].priv & 0x07;
1104  /* Copy registers 0x01 - 0x19 from the template */
1105  memcpy(&regs[0x01], sensor_data[sd->sensor].bridge_init, 0x19);
1106  /* Set the mode */
1107  regs[0x18] |= mode << 4;
1108 
1109  /* Set bridge gain to 1.0 */
1110  if (sd->bridge == BRIDGE_103) {
1111  regs[0x05] = 0x20; /* Red */
1112  regs[0x06] = 0x20; /* Green */
1113  regs[0x07] = 0x20; /* Blue */
1114  } else {
1115  regs[0x10] = 0x00; /* Red and blue */
1116  regs[0x11] = 0x00; /* Green */
1117  }
1118 
1119  /* Setup pixel numbers and auto exposure window */
1120  if (sensor_data[sd->sensor].flags & F_SIF) {
1121  regs[0x1a] = 0x14; /* HO_SIZE 640, makes no sense */
1122  regs[0x1b] = 0x0a; /* VO_SIZE 320, makes no sense */
1123  regs[0x1c] = 0x02; /* AE H-start 64 */
1124  regs[0x1d] = 0x02; /* AE V-start 64 */
1125  regs[0x1e] = 0x09; /* AE H-end 288 */
1126  regs[0x1f] = 0x07; /* AE V-end 224 */
1127  } else {
1128  regs[0x1a] = 0x1d; /* HO_SIZE 960, makes no sense */
1129  regs[0x1b] = 0x10; /* VO_SIZE 512, makes no sense */
1130  regs[0x1c] = 0x05; /* AE H-start 160 */
1131  regs[0x1d] = 0x03; /* AE V-start 96 */
1132  regs[0x1e] = 0x0f; /* AE H-end 480 */
1133  regs[0x1f] = 0x0c; /* AE V-end 384 */
1134  }
1135 
1136  /* Setup the gamma table (only used with the sn9c103 bridge) */
1137  for (i = 0; i < 16; i++)
1138  regs[0x20 + i] = i * 16;
1139  regs[0x20 + i] = 255;
1140 
1141  /* Special cases where some regs depend on mode or bridge */
1142  switch (sd->sensor) {
1143  case SENSOR_TAS5130CXX:
1144  /* FIXME / TESTME
1145  probably not mode specific at all most likely the upper
1146  nibble of 0x19 is exposure (clock divider) just as with
1147  the tas5110, we need someone to test this. */
1148  regs[0x19] = mode ? 0x23 : 0x43;
1149  break;
1150  case SENSOR_OV7630:
1151  /* FIXME / TESTME for some reason with the 101/102 bridge the
1152  clock is set to 12 Mhz (reg1 == 0x04), rather then 24.
1153  Also the hstart needs to go from 1 to 2 when using a 103,
1154  which is likely related. This does not seem right. */
1155  if (sd->bridge == BRIDGE_103) {
1156  regs[0x01] = 0x44; /* Select 24 Mhz clock */
1157  regs[0x12] = 0x02; /* Set hstart to 2 */
1158  }
1159  }
1160  /* Disable compression when the raw bayer format has been selected */
1161  if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW)
1162  regs[0x18] &= ~0x80;
1163 
1164  /* Vga mode emulation on SIF sensor? */
1165  if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_REDUCED_SIF) {
1166  regs[0x12] += 16; /* hstart adjust */
1167  regs[0x13] += 24; /* vstart adjust */
1168  regs[0x15] = 320 / 16; /* hsize */
1169  regs[0x16] = 240 / 16; /* vsize */
1170  }
1171 
1172  /* reg 0x01 bit 2 video transfert on */
1173  reg_w(gspca_dev, 0x01, &regs[0x01], 1);
1174  /* reg 0x17 SensorClk enable inv Clk 0x60 */
1175  reg_w(gspca_dev, 0x17, &regs[0x17], 1);
1176  /* Set the registers from the template */
1177  reg_w(gspca_dev, 0x01, &regs[0x01],
1178  (sd->bridge == BRIDGE_103) ? 0x30 : 0x1f);
1179 
1180  /* Init the sensor */
1181  i2c_w_vector(gspca_dev, sensor_data[sd->sensor].sensor_init,
1182  sensor_data[sd->sensor].sensor_init_size);
1183 
1184  /* Mode / bridge specific sensor setup */
1185  switch (sd->sensor) {
1186  case SENSOR_PAS202: {
1187  const __u8 i2cpclockdiv[] =
1188  {0xa0, 0x40, 0x02, 0x03, 0x00, 0x00, 0x00, 0x10};
1189  /* clockdiv from 4 to 3 (7.5 -> 10 fps) when in low res mode */
1190  if (mode)
1191  i2c_w(gspca_dev, i2cpclockdiv);
1192  break;
1193  }
1194  case SENSOR_OV7630:
1195  /* FIXME / TESTME We should be able to handle this identical
1196  for the 101/102 and the 103 case */
1197  if (sd->bridge == BRIDGE_103) {
1198  const __u8 i2c[] = { 0xa0, 0x21, 0x13,
1199  0x80, 0x00, 0x00, 0x00, 0x10 };
1200  i2c_w(gspca_dev, i2c);
1201  }
1202  break;
1203  }
1204  /* H_size V_size 0x28, 0x1e -> 640x480. 0x16, 0x12 -> 352x288 */
1205  reg_w(gspca_dev, 0x15, &regs[0x15], 2);
1206  /* compression register */
1207  reg_w(gspca_dev, 0x18, &regs[0x18], 1);
1208  /* H_start */
1209  reg_w(gspca_dev, 0x12, &regs[0x12], 1);
1210  /* V_START */
1211  reg_w(gspca_dev, 0x13, &regs[0x13], 1);
1212  /* reset 0x17 SensorClk enable inv Clk 0x60 */
1213  /*fixme: ov7630 [17]=68 8f (+20 if 102)*/
1214  reg_w(gspca_dev, 0x17, &regs[0x17], 1);
1215  /*MCKSIZE ->3 */ /*fixme: not ov7630*/
1216  reg_w(gspca_dev, 0x19, &regs[0x19], 1);
1217  /* AE_STRX AE_STRY AE_ENDX AE_ENDY */
1218  reg_w(gspca_dev, 0x1c, &regs[0x1c], 4);
1219  /* Enable video transfert */
1220  reg_w(gspca_dev, 0x01, &regs[0x01], 1);
1221  /* Compression */
1222  reg_w(gspca_dev, 0x18, &regs[0x18], 2);
1223  msleep(20);
1224 
1225  sd->reg11 = -1;
1226 
1227  setgain(gspca_dev);
1228  setbrightness(gspca_dev);
1229  setexposure(gspca_dev);
1230  setfreq(gspca_dev);
1231 
1232  sd->frames_to_drop = 0;
1233  sd->autogain_ignore_frames = 0;
1234  gspca_dev->exp_too_high_cnt = 0;
1235  gspca_dev->exp_too_low_cnt = 0;
1236  atomic_set(&sd->avg_lum, -1);
1237  return gspca_dev->usb_err;
1238 }
1239 
1240 static void sd_stopN(struct gspca_dev *gspca_dev)
1241 {
1242  sd_init(gspca_dev);
1243 }
1244 
1245 static u8* find_sof(struct gspca_dev *gspca_dev, u8 *data, int len)
1246 {
1247  struct sd *sd = (struct sd *) gspca_dev;
1248  int i, header_size = (sd->bridge == BRIDGE_103) ? 18 : 12;
1249 
1250  /* frames start with:
1251  * ff ff 00 c4 c4 96 synchro
1252  * 00 (unknown)
1253  * xx (frame sequence / size / compression)
1254  * (xx) (idem - extra byte for sn9c103)
1255  * ll mm brightness sum inside auto exposure
1256  * ll mm brightness sum outside auto exposure
1257  * (xx xx xx xx xx) audio values for snc103
1258  */
1259  for (i = 0; i < len; i++) {
1260  switch (sd->header_read) {
1261  case 0:
1262  if (data[i] == 0xff)
1263  sd->header_read++;
1264  break;
1265  case 1:
1266  if (data[i] == 0xff)
1267  sd->header_read++;
1268  else
1269  sd->header_read = 0;
1270  break;
1271  case 2:
1272  if (data[i] == 0x00)
1273  sd->header_read++;
1274  else if (data[i] != 0xff)
1275  sd->header_read = 0;
1276  break;
1277  case 3:
1278  if (data[i] == 0xc4)
1279  sd->header_read++;
1280  else if (data[i] == 0xff)
1281  sd->header_read = 1;
1282  else
1283  sd->header_read = 0;
1284  break;
1285  case 4:
1286  if (data[i] == 0xc4)
1287  sd->header_read++;
1288  else if (data[i] == 0xff)
1289  sd->header_read = 1;
1290  else
1291  sd->header_read = 0;
1292  break;
1293  case 5:
1294  if (data[i] == 0x96)
1295  sd->header_read++;
1296  else if (data[i] == 0xff)
1297  sd->header_read = 1;
1298  else
1299  sd->header_read = 0;
1300  break;
1301  default:
1302  sd->header[sd->header_read - 6] = data[i];
1303  sd->header_read++;
1304  if (sd->header_read == header_size) {
1305  sd->header_read = 0;
1306  return data + i + 1;
1307  }
1308  }
1309  }
1310  return NULL;
1311 }
1312 
1313 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1314  u8 *data, /* isoc packet */
1315  int len) /* iso packet length */
1316 {
1317  int fr_h_sz = 0, lum_offset = 0, len_after_sof = 0;
1318  struct sd *sd = (struct sd *) gspca_dev;
1319  struct cam *cam = &gspca_dev->cam;
1320  u8 *sof;
1321 
1322  sof = find_sof(gspca_dev, data, len);
1323  if (sof) {
1324  if (sd->bridge == BRIDGE_103) {
1325  fr_h_sz = 18;
1326  lum_offset = 3;
1327  } else {
1328  fr_h_sz = 12;
1329  lum_offset = 2;
1330  }
1331 
1332  len_after_sof = len - (sof - data);
1333  len = (sof - data) - fr_h_sz;
1334  if (len < 0)
1335  len = 0;
1336  }
1337 
1338  if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW) {
1339  /* In raw mode we sometimes get some garbage after the frame
1340  ignore this */
1341  int used;
1342  int size = cam->cam_mode[gspca_dev->curr_mode].sizeimage;
1343 
1344  used = gspca_dev->image_len;
1345  if (used + len > size)
1346  len = size - used;
1347  }
1348 
1349  gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1350 
1351  if (sof) {
1352  int lum = sd->header[lum_offset] +
1353  (sd->header[lum_offset + 1] << 8);
1354 
1355  /* When exposure changes midway a frame we
1356  get a lum of 0 in this case drop 2 frames
1357  as the frames directly after an exposure
1358  change have an unstable image. Sometimes lum
1359  *really* is 0 (cam used in low light with
1360  low exposure setting), so do not drop frames
1361  if the previous lum was 0 too. */
1362  if (lum == 0 && sd->prev_avg_lum != 0) {
1363  lum = -1;
1364  sd->frames_to_drop = 2;
1365  sd->prev_avg_lum = 0;
1366  } else
1367  sd->prev_avg_lum = lum;
1368  atomic_set(&sd->avg_lum, lum);
1369 
1370  if (sd->frames_to_drop)
1371  sd->frames_to_drop--;
1372  else
1373  gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
1374 
1375  gspca_frame_add(gspca_dev, FIRST_PACKET, sof, len_after_sof);
1376  }
1377 }
1378 
1379 static int sd_querymenu(struct gspca_dev *gspca_dev,
1380  struct v4l2_querymenu *menu)
1381 {
1382  switch (menu->id) {
1384  switch (menu->index) {
1385  case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1386  strcpy((char *) menu->name, "NoFliker");
1387  return 0;
1388  case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1389  strcpy((char *) menu->name, "50 Hz");
1390  return 0;
1391  case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
1392  strcpy((char *) menu->name, "60 Hz");
1393  return 0;
1394  }
1395  break;
1396  }
1397  return -EINVAL;
1398 }
1399 
1400 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
1401 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
1402  u8 *data, /* interrupt packet data */
1403  int len) /* interrupt packet length */
1404 {
1405  int ret = -EINVAL;
1406 
1407  if (len == 1 && data[0] == 1) {
1408  input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
1409  input_sync(gspca_dev->input_dev);
1410  input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
1411  input_sync(gspca_dev->input_dev);
1412  ret = 0;
1413  }
1414 
1415  return ret;
1416 }
1417 #endif
1418 
1419 /* sub-driver description */
1420 static const struct sd_desc sd_desc = {
1421  .name = MODULE_NAME,
1422  .config = sd_config,
1423  .init = sd_init,
1424  .init_controls = sd_init_controls,
1425  .start = sd_start,
1426  .stopN = sd_stopN,
1427  .pkt_scan = sd_pkt_scan,
1428  .querymenu = sd_querymenu,
1429  .dq_callback = do_autogain,
1430 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
1431  .int_pkt_scan = sd_int_pkt_scan,
1432 #endif
1433 };
1434 
1435 /* -- module initialisation -- */
1436 #define SB(sensor, bridge) \
1437  .driver_info = (SENSOR_ ## sensor << 8) | BRIDGE_ ## bridge
1438 
1439 
1440 static const struct usb_device_id device_table[] = {
1441  {USB_DEVICE(0x0c45, 0x6001), SB(TAS5110C, 102)}, /* TAS5110C1B */
1442  {USB_DEVICE(0x0c45, 0x6005), SB(TAS5110C, 101)}, /* TAS5110C1B */
1443  {USB_DEVICE(0x0c45, 0x6007), SB(TAS5110D, 101)}, /* TAS5110D */
1444  {USB_DEVICE(0x0c45, 0x6009), SB(PAS106, 101)},
1445  {USB_DEVICE(0x0c45, 0x600d), SB(PAS106, 101)},
1446  {USB_DEVICE(0x0c45, 0x6011), SB(OV6650, 101)},
1447  {USB_DEVICE(0x0c45, 0x6019), SB(OV7630, 101)},
1448 #if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
1449  {USB_DEVICE(0x0c45, 0x6024), SB(TAS5130CXX, 102)},
1450  {USB_DEVICE(0x0c45, 0x6025), SB(TAS5130CXX, 102)},
1451 #endif
1452  {USB_DEVICE(0x0c45, 0x6028), SB(PAS202, 102)},
1453  {USB_DEVICE(0x0c45, 0x6029), SB(PAS106, 102)},
1454  {USB_DEVICE(0x0c45, 0x602a), SB(HV7131D, 102)},
1455  /* {USB_DEVICE(0x0c45, 0x602b), SB(MI0343, 102)}, */
1456  {USB_DEVICE(0x0c45, 0x602c), SB(OV7630, 102)},
1457  {USB_DEVICE(0x0c45, 0x602d), SB(HV7131R, 102)},
1458  {USB_DEVICE(0x0c45, 0x602e), SB(OV7630, 102)},
1459  /* {USB_DEVICE(0x0c45, 0x6030), SB(MI03XX, 102)}, */ /* MI0343 MI0360 MI0330 */
1460  /* {USB_DEVICE(0x0c45, 0x6082), SB(MI03XX, 103)}, */ /* MI0343 MI0360 */
1461  {USB_DEVICE(0x0c45, 0x6083), SB(HV7131D, 103)},
1462  {USB_DEVICE(0x0c45, 0x608c), SB(HV7131R, 103)},
1463  /* {USB_DEVICE(0x0c45, 0x608e), SB(CISVF10, 103)}, */
1464  {USB_DEVICE(0x0c45, 0x608f), SB(OV7630, 103)},
1465  {USB_DEVICE(0x0c45, 0x60a8), SB(PAS106, 103)},
1466  {USB_DEVICE(0x0c45, 0x60aa), SB(TAS5130CXX, 103)},
1467  {USB_DEVICE(0x0c45, 0x60af), SB(PAS202, 103)},
1468  {USB_DEVICE(0x0c45, 0x60b0), SB(OV7630, 103)},
1469  {}
1470 };
1471 MODULE_DEVICE_TABLE(usb, device_table);
1472 
1473 /* -- device connect -- */
1474 static int sd_probe(struct usb_interface *intf,
1475  const struct usb_device_id *id)
1476 {
1477  return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1478  THIS_MODULE);
1479 }
1480 
1481 static struct usb_driver sd_driver = {
1482  .name = MODULE_NAME,
1483  .id_table = device_table,
1484  .probe = sd_probe,
1485  .disconnect = gspca_disconnect,
1486 #ifdef CONFIG_PM
1487  .suspend = gspca_suspend,
1488  .resume = gspca_resume,
1489  .reset_resume = gspca_resume,
1490 #endif
1491 };
1492 
1493 module_usb_driver(sd_driver);