2 * sonix sn9c102 (bayer) library
4 * Copyright (C) 2009-2011 Jean-François Moine <http://moinejf.free.fr>
5 * Copyright (C) 2003 2004 Michel Xhaard mxhaard@magic.fr
6 * Add Pas106 Stefano Mozzi (C) 2004
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
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.
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
23 /* Some documentation on known sonixb registers:
27 0x10 high nibble red gain low nibble blue gain
28 0x11 low nibble green gain
34 0x08-0x0f i2c / 3wire registers
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)
50 #define MODULE_NAME "sonixb"
52 #include <linux/input.h>
55 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
56 MODULE_DESCRIPTION("GSPCA/SN9C102 USB Camera Driver");
57 MODULE_LICENSE("GPL");
59 /* specific webcam descriptor */
61 struct gspca_dev gspca_dev; /* !! must be the first item */
63 struct v4l2_ctrl *brightness;
64 struct v4l2_ctrl *plfreq;
70 u8 header[12]; /* Header without sof marker */
72 unsigned char autogain_ignore_frames;
73 unsigned char frames_to_drop;
75 __u8 bridge; /* Type of bridge */
77 #define BRIDGE_102 0 /* We make no difference between 101 and 102 */
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
93 typedef const __u8 sensor_init_t[8];
96 const __u8 *bridge_init;
97 sensor_init_t *sensor_init;
103 /* sensor_data flags */
104 #define F_SIF 0x01 /* sif or vga */
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 */
110 #define COMP 0xc7 /* 0x87 //0x07 */
111 #define COMP1 0xc9 /* 0x89 //0x09 */
113 #define MCK_INIT 0x63
114 #define MCK_INIT1 0x20 /*fixme: Bayer - 0x50 for JPEG ??*/
118 #define SENS(bridge, sensor, _flags, _sensor_addr) \
120 .bridge_init = bridge, \
121 .sensor_init = sensor, \
122 .sensor_init_size = sizeof(sensor), \
123 .flags = _flags, .sensor_addr = _sensor_addr \
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
132 static const struct v4l2_pix_format vga_mode[] = {
133 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
135 .sizeimage = 160 * 120,
136 .colorspace = V4L2_COLORSPACE_SRGB,
137 .priv = 2 | MODE_RAW},
138 {160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
140 .sizeimage = 160 * 120 * 5 / 4,
141 .colorspace = V4L2_COLORSPACE_SRGB,
143 {320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
145 .sizeimage = 320 * 240 * 5 / 4,
146 .colorspace = V4L2_COLORSPACE_SRGB,
148 {640, 480, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
150 .sizeimage = 640 * 480 * 5 / 4,
151 .colorspace = V4L2_COLORSPACE_SRGB,
154 static const struct v4l2_pix_format sif_mode[] = {
155 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
157 .sizeimage = 160 * 120,
158 .colorspace = V4L2_COLORSPACE_SRGB,
159 .priv = 1 | MODE_RAW | MODE_REDUCED_SIF},
160 {160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
162 .sizeimage = 160 * 120 * 5 / 4,
163 .colorspace = V4L2_COLORSPACE_SRGB,
164 .priv = 1 | MODE_REDUCED_SIF},
165 {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
167 .sizeimage = 176 * 144,
168 .colorspace = V4L2_COLORSPACE_SRGB,
169 .priv = 1 | MODE_RAW},
170 {176, 144, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
172 .sizeimage = 176 * 144 * 5 / 4,
173 .colorspace = V4L2_COLORSPACE_SRGB,
175 {320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
177 .sizeimage = 320 * 240 * 5 / 4,
178 .colorspace = V4L2_COLORSPACE_SRGB,
179 .priv = 0 | MODE_REDUCED_SIF},
180 {352, 288, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
182 .sizeimage = 352 * 288 * 5 / 4,
183 .colorspace = V4L2_COLORSPACE_SRGB,
187 static const __u8 initHv7131d[] = {
188 0x04, 0x03, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,
190 0x00, 0x00, 0x00, 0x02, 0x02, 0x00,
191 0x28, 0x1e, 0x60, 0x8e, 0x42,
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 */
201 static const __u8 initHv7131r[] = {
202 0x46, 0x77, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,
204 0x00, 0x00, 0x00, 0x02, 0x01, 0x00,
205 0x28, 0x1e, 0x60, 0x8a, 0x20,
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},
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,
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 */
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 */
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 */
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},
281 static const __u8 initPas106[] = {
282 0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x40, 0x00, 0x00, 0x00,
284 0x00, 0x00, 0x00, 0x04, 0x01, 0x00,
285 0x16, 0x12, 0x24, COMP1, MCK_INIT1,
287 /* compression 0x86 mckinit1 0x2b */
289 /* "Known" PAS106B registers:
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)
309 0x13 Write 1 to commit settings to sensor
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 },
339 { 0xa1, 0x40, 0x0e, 0x0e, 0x00, 0x00, 0x00, 0x14 },
341 { 0xa1, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x14 },
342 /* H&V synchro polarity */
343 { 0xa1, 0x40, 0x10, 0x06, 0x00, 0x00, 0x00, 0x14 },
345 { 0xa1, 0x40, 0x11, 0x06, 0x00, 0x00, 0x00, 0x14 },
347 { 0xa1, 0x40, 0x12, 0x06, 0x00, 0x00, 0x00, 0x14 },
349 { 0xa1, 0x40, 0x14, 0x02, 0x00, 0x00, 0x00, 0x14 },
350 /* Validate Settings */
351 { 0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14 },
354 static const __u8 initPas202[] = {
355 0x44, 0x44, 0x21, 0x30, 0x00, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0x00,
357 0x00, 0x00, 0x00, 0x06, 0x03, 0x0a,
358 0x28, 0x1e, 0x20, 0x89, 0x20,
361 /* "Known" PAS202BCB registers:
363 0x04 Variable framerate bits 6-11 (*)
364 0x05 Var framerate bits 0-5, one must leave the 2 msb's at 0 !!
368 0x0b offset sign bit (bit0 1 > negative 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.
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
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},
394 static const __u8 initTas5110c[] = {
395 0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
397 0x00, 0x00, 0x00, 0x45, 0x09, 0x0a,
398 0x16, 0x12, 0x60, 0x86, 0x2b,
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,
404 0x00, 0x00, 0x00, 0x41, 0x09, 0x0a,
405 0x16, 0x12, 0x60, 0x86, 0x2b,
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},
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
417 static const __u8 tas5110d_sensor_init[][8] = {
418 {0xa0, 0x61, 0x9a, 0xca, 0x00, 0x00, 0x00, 0x17}, /* reset */
421 static const __u8 initTas5130[] = {
422 0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
424 0x00, 0x00, 0x00, 0x68, 0x0c, 0x0a,
425 0x28, 0x1e, 0x60, COMP, MCK_INIT,
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},
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),
447 /* get one byte in gspca_dev->usb_buf */
448 static void reg_r(struct gspca_dev *gspca_dev,
453 if (gspca_dev->usb_err < 0)
456 res = usb_control_msg(gspca_dev->dev,
457 usb_rcvctrlpipe(gspca_dev->dev, 0),
459 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
462 gspca_dev->usb_buf, 1,
466 dev_err(gspca_dev->v4l2_dev.dev,
467 "Error reading register %02x: %d\n", value, res);
468 gspca_dev->usb_err = res;
470 * Make sure the result is zeroed to avoid uninitialized
473 gspca_dev->usb_buf[0] = 0;
477 static void reg_w(struct gspca_dev *gspca_dev,
484 if (gspca_dev->usb_err < 0)
487 memcpy(gspca_dev->usb_buf, buffer, len);
488 res = usb_control_msg(gspca_dev->dev,
489 usb_sndctrlpipe(gspca_dev->dev, 0),
491 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
494 gspca_dev->usb_buf, len,
498 dev_err(gspca_dev->v4l2_dev.dev,
499 "Error writing register %02x: %d\n", value, res);
500 gspca_dev->usb_err = res;
504 static void i2c_w(struct gspca_dev *gspca_dev, const u8 *buf)
508 if (gspca_dev->usb_err < 0)
512 reg_w(gspca_dev, 0x08, buf, 8);
514 if (gspca_dev->usb_err < 0)
517 reg_r(gspca_dev, 0x08);
518 if (gspca_dev->usb_buf[0] & 0x04) {
519 if (gspca_dev->usb_buf[0] & 0x08) {
520 dev_err(gspca_dev->v4l2_dev.dev,
521 "i2c error writing %8ph\n", buf);
522 gspca_dev->usb_err = -EIO;
528 dev_err(gspca_dev->v4l2_dev.dev, "i2c write timeout\n");
529 gspca_dev->usb_err = -EIO;
532 static void i2c_w_vector(struct gspca_dev *gspca_dev,
533 const __u8 buffer[][8], int len)
536 if (gspca_dev->usb_err < 0)
538 i2c_w(gspca_dev, *buffer);
546 static void setbrightness(struct gspca_dev *gspca_dev)
548 struct sd *sd = (struct sd *) gspca_dev;
550 switch (sd->sensor) {
552 case SENSOR_OV7630: {
554 {0xa0, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10};
556 /* change reg 0x06 */
557 i2cOV[1] = sensor_data[sd->sensor].sensor_addr;
558 i2cOV[3] = sd->brightness->val;
559 i2c_w(gspca_dev, i2cOV);
563 case SENSOR_PAS202: {
565 {0xb0, 0x40, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x16};
567 {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
569 /* PAS106 uses reg 7 and 8 instead of b and c */
570 if (sd->sensor == SENSOR_PAS106) {
575 if (sd->brightness->val < 127) {
576 /* change reg 0x0b, signreg */
577 i2cpbright[3] = 0x01;
578 /* set reg 0x0c, offset */
579 i2cpbright[4] = 127 - sd->brightness->val;
581 i2cpbright[4] = sd->brightness->val - 127;
583 i2c_w(gspca_dev, i2cpbright);
584 i2c_w(gspca_dev, i2cpdoit);
592 static void setgain(struct gspca_dev *gspca_dev)
594 struct sd *sd = (struct sd *) gspca_dev;
595 u8 gain = gspca_dev->gain->val;
597 switch (sd->sensor) {
598 case SENSOR_HV7131D: {
600 {0xc0, 0x11, 0x31, 0x00, 0x00, 0x00, 0x00, 0x17};
602 i2c[3] = 0x3f - gain;
603 i2c[4] = 0x3f - gain;
604 i2c[5] = 0x3f - gain;
606 i2c_w(gspca_dev, i2c);
609 case SENSOR_TAS5110C:
610 case SENSOR_TAS5130CXX: {
612 {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};
615 i2c_w(gspca_dev, i2c);
618 case SENSOR_TAS5110D: {
620 0xb0, 0x61, 0x02, 0x00, 0x10, 0x00, 0x00, 0x17 };
622 /* The bits in the register are the wrong way around!! */
623 i2c[3] |= (gain & 0x80) >> 7;
624 i2c[3] |= (gain & 0x40) >> 5;
625 i2c[3] |= (gain & 0x20) >> 3;
626 i2c[3] |= (gain & 0x10) >> 1;
627 i2c[3] |= (gain & 0x08) << 1;
628 i2c[3] |= (gain & 0x04) << 3;
629 i2c[3] |= (gain & 0x02) << 5;
630 i2c[3] |= (gain & 0x01) << 7;
631 i2c_w(gspca_dev, i2c);
635 case SENSOR_OV7630: {
636 __u8 i2c[] = {0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
639 * The ov7630's gain is weird, at 32 the gain drops to the
640 * same level as at 16, so skip 32-47 (of the 0-63 scale).
642 if (sd->sensor == SENSOR_OV7630 && gain >= 32)
645 i2c[1] = sensor_data[sd->sensor].sensor_addr;
647 i2c_w(gspca_dev, i2c);
651 case SENSOR_PAS202: {
653 {0xa0, 0x40, 0x10, 0x00, 0x00, 0x00, 0x00, 0x15};
654 __u8 i2cpcolorgain[] =
655 {0xc0, 0x40, 0x07, 0x00, 0x00, 0x00, 0x00, 0x15};
657 {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
659 /* PAS106 uses different regs (and has split green gains) */
660 if (sd->sensor == SENSOR_PAS106) {
662 i2cpcolorgain[0] = 0xd0;
663 i2cpcolorgain[2] = 0x09;
668 i2cpcolorgain[3] = gain >> 1;
669 i2cpcolorgain[4] = gain >> 1;
670 i2cpcolorgain[5] = gain >> 1;
671 i2cpcolorgain[6] = gain >> 1;
673 i2c_w(gspca_dev, i2cpgain);
674 i2c_w(gspca_dev, i2cpcolorgain);
675 i2c_w(gspca_dev, i2cpdoit);
679 if (sd->bridge == BRIDGE_103) {
680 u8 buf[3] = { gain, gain, gain }; /* R, G, B */
681 reg_w(gspca_dev, 0x05, buf, 3);
684 buf[0] = gain << 4 | gain; /* Red and blue */
685 buf[1] = gain; /* Green */
686 reg_w(gspca_dev, 0x10, buf, 2);
691 static void setexposure(struct gspca_dev *gspca_dev)
693 struct sd *sd = (struct sd *) gspca_dev;
695 switch (sd->sensor) {
696 case SENSOR_HV7131D: {
697 /* Note the datasheet wrongly says line mode exposure uses reg
698 0x26 and 0x27, testing has shown 0x25 + 0x26 */
699 __u8 i2c[] = {0xc0, 0x11, 0x25, 0x00, 0x00, 0x00, 0x00, 0x17};
700 u16 reg = gspca_dev->exposure->val;
704 i2c_w(gspca_dev, i2c);
707 case SENSOR_TAS5110C:
708 case SENSOR_TAS5110D: {
709 /* register 19's high nibble contains the sn9c10x clock divider
710 The high nibble configures the no fps according to the
711 formula: 60 / high_nibble. With a maximum of 30 fps */
712 u8 reg = gspca_dev->exposure->val;
714 reg = (reg << 4) | 0x0b;
715 reg_w(gspca_dev, 0x19, ®, 1);
719 case SENSOR_OV7630: {
720 /* The ov6650 / ov7630 have 2 registers which both influence
721 exposure, register 11, whose low nibble sets the nr off fps
722 according to: fps = 30 / (low_nibble + 1)
724 The fps configures the maximum exposure setting, but it is
725 possible to use less exposure then what the fps maximum
726 allows by setting register 10. register 10 configures the
727 actual exposure as quotient of the full exposure, with 0
728 being no exposure at all (not very useful) and reg10_max
729 being max exposure possible at that framerate.
731 The code maps our 0 - 510 ms exposure ctrl to these 2
732 registers, trying to keep fps as high as possible.
734 __u8 i2c[] = {0xb0, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10};
735 int reg10, reg11, reg10_max;
737 /* ov6645 datasheet says reg10_max is 9a, but that uses
738 tline * 2 * reg10 as formula for calculating texpo, the
739 ov6650 probably uses the same formula as the 7730 which uses
740 tline * 4 * reg10, which explains why the reg10max we've
741 found experimentally for the ov6650 is exactly half that of
742 the ov6645. The ov7630 datasheet says the max is 0x41. */
743 if (sd->sensor == SENSOR_OV6650) {
745 i2c[4] = 0xc0; /* OV6650 needs non default vsync pol */
749 reg11 = (15 * gspca_dev->exposure->val + 999) / 1000;
755 /* In 640x480, if the reg11 has less than 4, the image is
756 unstable (the bridge goes into a higher compression mode
757 which we have not reverse engineered yet). */
758 if (gspca_dev->pixfmt.width == 640 && reg11 < 4)
761 /* frame exposure time in ms = 1000 * reg11 / 30 ->
762 reg10 = (gspca_dev->exposure->val / 2) * reg10_max
763 / (1000 * reg11 / 30) */
764 reg10 = (gspca_dev->exposure->val * 15 * reg10_max)
767 /* Don't allow this to get below 10 when using autogain, the
768 steps become very large (relatively) when below 10 causing
769 the image to oscilate from much too dark, to much too bright
771 if (gspca_dev->autogain->val && reg10 < 10)
773 else if (reg10 > reg10_max)
776 /* Write reg 10 and reg11 low nibble */
777 i2c[1] = sensor_data[sd->sensor].sensor_addr;
781 /* If register 11 didn't change, don't change it */
782 if (sd->reg11 == reg11)
785 i2c_w(gspca_dev, i2c);
786 if (gspca_dev->usb_err == 0)
790 case SENSOR_PAS202: {
791 __u8 i2cpframerate[] =
792 {0xb0, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x16};
794 {0xa0, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x16};
795 const __u8 i2cpdoit[] =
796 {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
799 /* The exposure knee for the autogain algorithm is 200
800 (100 ms / 10 fps on other sensors), for values below this
801 use the control for setting the partial frame expose time,
802 above that use variable framerate. This way we run at max
803 framerate (640x480@7.5 fps, 320x240@10fps) until the knee
804 is reached. Using the variable framerate control above 200
805 is better then playing around with both clockdiv + partial
806 frame exposure times (like we are doing with the ov chips),
807 as that sometimes leads to jumps in the exposure control,
808 which are bad for auto exposure. */
809 if (gspca_dev->exposure->val < 200) {
810 i2cpexpo[3] = 255 - (gspca_dev->exposure->val * 255)
812 framerate_ctrl = 500;
814 /* The PAS202's exposure control goes from 0 - 4095,
815 but anything below 500 causes vsync issues, so scale
816 our 200-1023 to 500-4095 */
817 framerate_ctrl = (gspca_dev->exposure->val - 200)
821 i2cpframerate[3] = framerate_ctrl >> 6;
822 i2cpframerate[4] = framerate_ctrl & 0x3f;
823 i2c_w(gspca_dev, i2cpframerate);
824 i2c_w(gspca_dev, i2cpexpo);
825 i2c_w(gspca_dev, i2cpdoit);
828 case SENSOR_PAS106: {
829 __u8 i2cpframerate[] =
830 {0xb1, 0x40, 0x03, 0x00, 0x00, 0x00, 0x00, 0x14};
832 {0xa1, 0x40, 0x05, 0x00, 0x00, 0x00, 0x00, 0x14};
833 const __u8 i2cpdoit[] =
834 {0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14};
837 /* For values below 150 use partial frame exposure, above
838 that use framerate ctrl */
839 if (gspca_dev->exposure->val < 150) {
840 i2cpexpo[3] = 150 - gspca_dev->exposure->val;
841 framerate_ctrl = 300;
843 /* The PAS106's exposure control goes from 0 - 4095,
844 but anything below 300 causes vsync issues, so scale
845 our 150-1023 to 300-4095 */
846 framerate_ctrl = (gspca_dev->exposure->val - 150)
850 i2cpframerate[3] = framerate_ctrl >> 4;
851 i2cpframerate[4] = framerate_ctrl & 0x0f;
852 i2c_w(gspca_dev, i2cpframerate);
853 i2c_w(gspca_dev, i2cpexpo);
854 i2c_w(gspca_dev, i2cpdoit);
862 static void setfreq(struct gspca_dev *gspca_dev)
864 struct sd *sd = (struct sd *) gspca_dev;
866 if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630) {
867 /* Framerate adjust register for artificial light 50 hz flicker
868 compensation, for the ov6650 this is identical to ov6630
869 0x2b register, see ov6630 datasheet.
870 0x4f / 0x8a -> (30 fps -> 25 fps), 0x00 -> no adjustment */
871 __u8 i2c[] = {0xa0, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10};
872 switch (sd->plfreq->val) {
874 /* case 0: * no filter*/
875 /* case 2: * 60 hz */
879 i2c[3] = (sd->sensor == SENSOR_OV6650)
883 i2c[1] = sensor_data[sd->sensor].sensor_addr;
884 i2c_w(gspca_dev, i2c);
888 static void do_autogain(struct gspca_dev *gspca_dev)
890 struct sd *sd = (struct sd *) gspca_dev;
891 int deadzone, desired_avg_lum, avg_lum;
893 avg_lum = atomic_read(&sd->avg_lum);
897 if (sd->autogain_ignore_frames > 0) {
898 sd->autogain_ignore_frames--;
902 /* SIF / VGA sensors have a different autoexposure area and thus
903 different avg_lum values for the same picture brightness */
904 if (sensor_data[sd->sensor].flags & F_SIF) {
906 /* SIF sensors tend to overexpose, so keep this small */
907 desired_avg_lum = 5000;
910 desired_avg_lum = 13000;
914 desired_avg_lum = sd->brightness->val * desired_avg_lum / 127;
916 if (gspca_dev->exposure->maximum < 500) {
917 if (gspca_coarse_grained_expo_autogain(gspca_dev, avg_lum,
918 desired_avg_lum, deadzone))
919 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
921 int gain_knee = (s32)gspca_dev->gain->maximum * 9 / 10;
922 if (gspca_expo_autogain(gspca_dev, avg_lum, desired_avg_lum,
923 deadzone, gain_knee, sd->exposure_knee))
924 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
928 /* this function is called at probe time */
929 static int sd_config(struct gspca_dev *gspca_dev,
930 const struct usb_device_id *id)
932 struct sd *sd = (struct sd *) gspca_dev;
935 reg_r(gspca_dev, 0x00);
936 if (gspca_dev->usb_buf[0] != 0x10)
939 /* copy the webcam info from the device id */
940 sd->sensor = id->driver_info >> 8;
941 sd->bridge = id->driver_info & 0xff;
943 cam = &gspca_dev->cam;
944 if (!(sensor_data[sd->sensor].flags & F_SIF)) {
945 cam->cam_mode = vga_mode;
946 cam->nmodes = ARRAY_SIZE(vga_mode);
948 cam->cam_mode = sif_mode;
949 cam->nmodes = ARRAY_SIZE(sif_mode);
951 cam->npkt = 36; /* 36 packets per ISOC message */
956 /* this function is called at probe and resume time */
957 static int sd_init(struct gspca_dev *gspca_dev)
959 const __u8 stop = 0x09; /* Disable stream turn of LED */
961 reg_w(gspca_dev, 0x01, &stop, 1);
963 return gspca_dev->usb_err;
966 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
968 struct gspca_dev *gspca_dev =
969 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
970 struct sd *sd = (struct sd *)gspca_dev;
972 gspca_dev->usb_err = 0;
974 if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
975 /* when switching to autogain set defaults to make sure
976 we are on a valid point of the autogain gain /
977 exposure knee graph, and give this change time to
978 take effect before doing autogain. */
979 gspca_dev->gain->val = gspca_dev->gain->default_value;
980 gspca_dev->exposure->val = gspca_dev->exposure->default_value;
981 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
984 if (!gspca_dev->streaming)
988 case V4L2_CID_BRIGHTNESS:
989 setbrightness(gspca_dev);
991 case V4L2_CID_AUTOGAIN:
992 if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
993 setexposure(gspca_dev);
994 if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
997 case V4L2_CID_POWER_LINE_FREQUENCY:
1003 return gspca_dev->usb_err;
1006 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1007 .s_ctrl = sd_s_ctrl,
1010 /* this function is called at probe time */
1011 static int sd_init_controls(struct gspca_dev *gspca_dev)
1013 struct sd *sd = (struct sd *) gspca_dev;
1014 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1016 gspca_dev->vdev.ctrl_handler = hdl;
1017 v4l2_ctrl_handler_init(hdl, 5);
1019 if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630 ||
1020 sd->sensor == SENSOR_PAS106 || sd->sensor == SENSOR_PAS202)
1021 sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1022 V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
1024 /* Gain range is sensor dependent */
1025 switch (sd->sensor) {
1029 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1030 V4L2_CID_GAIN, 0, 31, 1, 15);
1033 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1034 V4L2_CID_GAIN, 0, 47, 1, 31);
1036 case SENSOR_HV7131D:
1037 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1038 V4L2_CID_GAIN, 0, 63, 1, 31);
1040 case SENSOR_TAS5110C:
1041 case SENSOR_TAS5110D:
1042 case SENSOR_TAS5130CXX:
1043 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1044 V4L2_CID_GAIN, 0, 255, 1, 127);
1047 if (sd->bridge == BRIDGE_103) {
1048 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1049 V4L2_CID_GAIN, 0, 127, 1, 63);
1051 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1052 V4L2_CID_GAIN, 0, 15, 1, 7);
1056 /* Exposure range is sensor dependent, and not all have exposure */
1057 switch (sd->sensor) {
1058 case SENSOR_HV7131D:
1059 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1060 V4L2_CID_EXPOSURE, 0, 8191, 1, 482);
1061 sd->exposure_knee = 964;
1067 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1068 V4L2_CID_EXPOSURE, 0, 1023, 1, 66);
1069 sd->exposure_knee = 200;
1071 case SENSOR_TAS5110C:
1072 case SENSOR_TAS5110D:
1073 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1074 V4L2_CID_EXPOSURE, 2, 15, 1, 2);
1078 if (gspca_dev->exposure) {
1079 gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1080 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1083 if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630)
1084 sd->plfreq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
1085 V4L2_CID_POWER_LINE_FREQUENCY,
1086 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0,
1087 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED);
1090 pr_err("Could not initialize controls\n");
1094 if (gspca_dev->autogain)
1095 v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
1100 /* -- start the camera -- */
1101 static int sd_start(struct gspca_dev *gspca_dev)
1103 struct sd *sd = (struct sd *) gspca_dev;
1104 struct cam *cam = &gspca_dev->cam;
1108 mode = cam->cam_mode[gspca_dev->curr_mode].priv & 0x07;
1109 /* Copy registers 0x01 - 0x19 from the template */
1110 memcpy(®s[0x01], sensor_data[sd->sensor].bridge_init, 0x19);
1112 regs[0x18] |= mode << 4;
1114 /* Set bridge gain to 1.0 */
1115 if (sd->bridge == BRIDGE_103) {
1116 regs[0x05] = 0x20; /* Red */
1117 regs[0x06] = 0x20; /* Green */
1118 regs[0x07] = 0x20; /* Blue */
1120 regs[0x10] = 0x00; /* Red and blue */
1121 regs[0x11] = 0x00; /* Green */
1124 /* Setup pixel numbers and auto exposure window */
1125 if (sensor_data[sd->sensor].flags & F_SIF) {
1126 regs[0x1a] = 0x14; /* HO_SIZE 640, makes no sense */
1127 regs[0x1b] = 0x0a; /* VO_SIZE 320, makes no sense */
1128 regs[0x1c] = 0x02; /* AE H-start 64 */
1129 regs[0x1d] = 0x02; /* AE V-start 64 */
1130 regs[0x1e] = 0x09; /* AE H-end 288 */
1131 regs[0x1f] = 0x07; /* AE V-end 224 */
1133 regs[0x1a] = 0x1d; /* HO_SIZE 960, makes no sense */
1134 regs[0x1b] = 0x10; /* VO_SIZE 512, makes no sense */
1135 regs[0x1c] = 0x05; /* AE H-start 160 */
1136 regs[0x1d] = 0x03; /* AE V-start 96 */
1137 regs[0x1e] = 0x0f; /* AE H-end 480 */
1138 regs[0x1f] = 0x0c; /* AE V-end 384 */
1141 /* Setup the gamma table (only used with the sn9c103 bridge) */
1142 for (i = 0; i < 16; i++)
1143 regs[0x20 + i] = i * 16;
1144 regs[0x20 + i] = 255;
1146 /* Special cases where some regs depend on mode or bridge */
1147 switch (sd->sensor) {
1148 case SENSOR_TAS5130CXX:
1150 probably not mode specific at all most likely the upper
1151 nibble of 0x19 is exposure (clock divider) just as with
1152 the tas5110, we need someone to test this. */
1153 regs[0x19] = mode ? 0x23 : 0x43;
1156 /* FIXME / TESTME for some reason with the 101/102 bridge the
1157 clock is set to 12 Mhz (reg1 == 0x04), rather then 24.
1158 Also the hstart needs to go from 1 to 2 when using a 103,
1159 which is likely related. This does not seem right. */
1160 if (sd->bridge == BRIDGE_103) {
1161 regs[0x01] = 0x44; /* Select 24 Mhz clock */
1162 regs[0x12] = 0x02; /* Set hstart to 2 */
1166 /* For some unknown reason we need to increase hstart by 1 on
1167 the sn9c103, otherwise we get wrong colors (bayer shift). */
1168 if (sd->bridge == BRIDGE_103)
1172 /* Disable compression when the raw bayer format has been selected */
1173 if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW)
1174 regs[0x18] &= ~0x80;
1176 /* Vga mode emulation on SIF sensor? */
1177 if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_REDUCED_SIF) {
1178 regs[0x12] += 16; /* hstart adjust */
1179 regs[0x13] += 24; /* vstart adjust */
1180 regs[0x15] = 320 / 16; /* hsize */
1181 regs[0x16] = 240 / 16; /* vsize */
1184 /* reg 0x01 bit 2 video transfert on */
1185 reg_w(gspca_dev, 0x01, ®s[0x01], 1);
1186 /* reg 0x17 SensorClk enable inv Clk 0x60 */
1187 reg_w(gspca_dev, 0x17, ®s[0x17], 1);
1188 /* Set the registers from the template */
1189 reg_w(gspca_dev, 0x01, ®s[0x01],
1190 (sd->bridge == BRIDGE_103) ? 0x30 : 0x1f);
1192 /* Init the sensor */
1193 i2c_w_vector(gspca_dev, sensor_data[sd->sensor].sensor_init,
1194 sensor_data[sd->sensor].sensor_init_size);
1196 /* Mode / bridge specific sensor setup */
1197 switch (sd->sensor) {
1198 case SENSOR_PAS202: {
1199 const __u8 i2cpclockdiv[] =
1200 {0xa0, 0x40, 0x02, 0x03, 0x00, 0x00, 0x00, 0x10};
1201 /* clockdiv from 4 to 3 (7.5 -> 10 fps) when in low res mode */
1203 i2c_w(gspca_dev, i2cpclockdiv);
1207 /* FIXME / TESTME We should be able to handle this identical
1208 for the 101/102 and the 103 case */
1209 if (sd->bridge == BRIDGE_103) {
1210 const __u8 i2c[] = { 0xa0, 0x21, 0x13,
1211 0x80, 0x00, 0x00, 0x00, 0x10 };
1212 i2c_w(gspca_dev, i2c);
1216 /* H_size V_size 0x28, 0x1e -> 640x480. 0x16, 0x12 -> 352x288 */
1217 reg_w(gspca_dev, 0x15, ®s[0x15], 2);
1218 /* compression register */
1219 reg_w(gspca_dev, 0x18, ®s[0x18], 1);
1221 reg_w(gspca_dev, 0x12, ®s[0x12], 1);
1223 reg_w(gspca_dev, 0x13, ®s[0x13], 1);
1224 /* reset 0x17 SensorClk enable inv Clk 0x60 */
1225 /*fixme: ov7630 [17]=68 8f (+20 if 102)*/
1226 reg_w(gspca_dev, 0x17, ®s[0x17], 1);
1227 /*MCKSIZE ->3 */ /*fixme: not ov7630*/
1228 reg_w(gspca_dev, 0x19, ®s[0x19], 1);
1229 /* AE_STRX AE_STRY AE_ENDX AE_ENDY */
1230 reg_w(gspca_dev, 0x1c, ®s[0x1c], 4);
1231 /* Enable video transfert */
1232 reg_w(gspca_dev, 0x01, ®s[0x01], 1);
1234 reg_w(gspca_dev, 0x18, ®s[0x18], 2);
1240 setbrightness(gspca_dev);
1241 setexposure(gspca_dev);
1244 sd->frames_to_drop = 0;
1245 sd->autogain_ignore_frames = 0;
1246 gspca_dev->exp_too_high_cnt = 0;
1247 gspca_dev->exp_too_low_cnt = 0;
1248 atomic_set(&sd->avg_lum, -1);
1249 return gspca_dev->usb_err;
1252 static void sd_stopN(struct gspca_dev *gspca_dev)
1257 static u8* find_sof(struct gspca_dev *gspca_dev, u8 *data, int len)
1259 struct sd *sd = (struct sd *) gspca_dev;
1260 int i, header_size = (sd->bridge == BRIDGE_103) ? 18 : 12;
1262 /* frames start with:
1263 * ff ff 00 c4 c4 96 synchro
1265 * xx (frame sequence / size / compression)
1266 * (xx) (idem - extra byte for sn9c103)
1267 * ll mm brightness sum inside auto exposure
1268 * ll mm brightness sum outside auto exposure
1269 * (xx xx xx xx xx) audio values for snc103
1271 for (i = 0; i < len; i++) {
1272 switch (sd->header_read) {
1274 if (data[i] == 0xff)
1278 if (data[i] == 0xff)
1281 sd->header_read = 0;
1284 if (data[i] == 0x00)
1286 else if (data[i] != 0xff)
1287 sd->header_read = 0;
1290 if (data[i] == 0xc4)
1292 else if (data[i] == 0xff)
1293 sd->header_read = 1;
1295 sd->header_read = 0;
1298 if (data[i] == 0xc4)
1300 else if (data[i] == 0xff)
1301 sd->header_read = 1;
1303 sd->header_read = 0;
1306 if (data[i] == 0x96)
1308 else if (data[i] == 0xff)
1309 sd->header_read = 1;
1311 sd->header_read = 0;
1314 sd->header[sd->header_read - 6] = data[i];
1316 if (sd->header_read == header_size) {
1317 sd->header_read = 0;
1318 return data + i + 1;
1325 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1326 u8 *data, /* isoc packet */
1327 int len) /* iso packet length */
1329 int fr_h_sz = 0, lum_offset = 0, len_after_sof = 0;
1330 struct sd *sd = (struct sd *) gspca_dev;
1331 struct cam *cam = &gspca_dev->cam;
1334 sof = find_sof(gspca_dev, data, len);
1336 if (sd->bridge == BRIDGE_103) {
1344 len_after_sof = len - (sof - data);
1345 len = (sof - data) - fr_h_sz;
1350 if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW) {
1351 /* In raw mode we sometimes get some garbage after the frame
1354 int size = cam->cam_mode[gspca_dev->curr_mode].sizeimage;
1356 used = gspca_dev->image_len;
1357 if (used + len > size)
1361 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1364 int lum = sd->header[lum_offset] +
1365 (sd->header[lum_offset + 1] << 8);
1367 /* When exposure changes midway a frame we
1368 get a lum of 0 in this case drop 2 frames
1369 as the frames directly after an exposure
1370 change have an unstable image. Sometimes lum
1371 *really* is 0 (cam used in low light with
1372 low exposure setting), so do not drop frames
1373 if the previous lum was 0 too. */
1374 if (lum == 0 && sd->prev_avg_lum != 0) {
1376 sd->frames_to_drop = 2;
1377 sd->prev_avg_lum = 0;
1379 sd->prev_avg_lum = lum;
1380 atomic_set(&sd->avg_lum, lum);
1382 if (sd->frames_to_drop)
1383 sd->frames_to_drop--;
1385 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
1387 gspca_frame_add(gspca_dev, FIRST_PACKET, sof, len_after_sof);
1391 #if IS_ENABLED(CONFIG_INPUT)
1392 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
1393 u8 *data, /* interrupt packet data */
1394 int len) /* interrupt packet length */
1398 if (len == 1 && data[0] == 1) {
1399 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
1400 input_sync(gspca_dev->input_dev);
1401 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
1402 input_sync(gspca_dev->input_dev);
1410 /* sub-driver description */
1411 static const struct sd_desc sd_desc = {
1412 .name = MODULE_NAME,
1413 .config = sd_config,
1415 .init_controls = sd_init_controls,
1418 .pkt_scan = sd_pkt_scan,
1419 .dq_callback = do_autogain,
1420 #if IS_ENABLED(CONFIG_INPUT)
1421 .int_pkt_scan = sd_int_pkt_scan,
1425 /* -- module initialisation -- */
1426 #define SB(sensor, bridge) \
1427 .driver_info = (SENSOR_ ## sensor << 8) | BRIDGE_ ## bridge
1430 static const struct usb_device_id device_table[] = {
1431 {USB_DEVICE(0x0c45, 0x6001), SB(TAS5110C, 102)}, /* TAS5110C1B */
1432 {USB_DEVICE(0x0c45, 0x6005), SB(TAS5110C, 101)}, /* TAS5110C1B */
1433 {USB_DEVICE(0x0c45, 0x6007), SB(TAS5110D, 101)}, /* TAS5110D */
1434 {USB_DEVICE(0x0c45, 0x6009), SB(PAS106, 101)},
1435 {USB_DEVICE(0x0c45, 0x600d), SB(PAS106, 101)},
1436 {USB_DEVICE(0x0c45, 0x6011), SB(OV6650, 101)},
1437 {USB_DEVICE(0x0c45, 0x6019), SB(OV7630, 101)},
1438 {USB_DEVICE(0x0c45, 0x6024), SB(TAS5130CXX, 102)},
1439 {USB_DEVICE(0x0c45, 0x6025), SB(TAS5130CXX, 102)},
1440 {USB_DEVICE(0x0c45, 0x6027), SB(OV7630, 101)}, /* Genius Eye 310 */
1441 {USB_DEVICE(0x0c45, 0x6028), SB(PAS202, 102)},
1442 {USB_DEVICE(0x0c45, 0x6029), SB(PAS106, 102)},
1443 {USB_DEVICE(0x0c45, 0x602a), SB(HV7131D, 102)},
1444 /* {USB_DEVICE(0x0c45, 0x602b), SB(MI0343, 102)}, */
1445 {USB_DEVICE(0x0c45, 0x602c), SB(OV7630, 102)},
1446 {USB_DEVICE(0x0c45, 0x602d), SB(HV7131R, 102)},
1447 {USB_DEVICE(0x0c45, 0x602e), SB(OV7630, 102)},
1448 /* {USB_DEVICE(0x0c45, 0x6030), SB(MI03XX, 102)}, */ /* MI0343 MI0360 MI0330 */
1449 /* {USB_DEVICE(0x0c45, 0x6082), SB(MI03XX, 103)}, */ /* MI0343 MI0360 */
1450 {USB_DEVICE(0x0c45, 0x6083), SB(HV7131D, 103)},
1451 {USB_DEVICE(0x0c45, 0x608c), SB(HV7131R, 103)},
1452 /* {USB_DEVICE(0x0c45, 0x608e), SB(CISVF10, 103)}, */
1453 {USB_DEVICE(0x0c45, 0x608f), SB(OV7630, 103)},
1454 {USB_DEVICE(0x0c45, 0x60a8), SB(PAS106, 103)},
1455 {USB_DEVICE(0x0c45, 0x60aa), SB(TAS5130CXX, 103)},
1456 {USB_DEVICE(0x0c45, 0x60af), SB(PAS202, 103)},
1457 {USB_DEVICE(0x0c45, 0x60b0), SB(OV7630, 103)},
1460 MODULE_DEVICE_TABLE(usb, device_table);
1462 /* -- device connect -- */
1463 static int sd_probe(struct usb_interface *intf,
1464 const struct usb_device_id *id)
1466 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1470 static struct usb_driver sd_driver = {
1471 .name = MODULE_NAME,
1472 .id_table = device_table,
1474 .disconnect = gspca_disconnect,
1476 .suspend = gspca_suspend,
1477 .resume = gspca_resume,
1478 .reset_resume = gspca_resume,
1482 module_usb_driver(sd_driver);