blob: b5acb1e4b4e7ce26dc0b644411872847ca8e5f77 [file] [log] [blame]
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001/*
Jean-François Moine458efe22011-02-10 10:11:04 -03002 * ov534-ov7xxx gspca driver
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03003 *
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03004 * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
Jim Paris0f7a50b2008-12-10 05:45:14 -03005 * Copyright (C) 2008 Jim Paris <jim@jtan.com>
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -03006 * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03007 *
8 * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
9 * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
10 * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
11 *
Jean-Francois Moine189d92a2009-11-11 07:46:28 -030012 * PS3 Eye camera enhanced by Richard Kaswy http://kaswy.free.fr
Max Thrun6721b512010-02-27 17:20:28 -030013 * PS3 Eye camera - brightness, contrast, awb, agc, aec controls
14 * added by Max Thrun <bear24rw@gmail.com>
Jean-Francois Moine189d92a2009-11-11 07:46:28 -030015 *
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030016 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 */
30
Joe Perches133a9fe2011-08-21 19:56:57 -030031#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030033#define MODULE_NAME "ov534"
34
35#include "gspca.h"
36
Antonio Ospitee89fca92012-05-14 08:07:45 -030037#include <linux/fixp-arith.h>
38
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -030039#define OV534_REG_ADDRESS 0xf1 /* sensor address */
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030040#define OV534_REG_SUBADDR 0xf2
41#define OV534_REG_WRITE 0xf3
42#define OV534_REG_READ 0xf4
43#define OV534_REG_OPERATION 0xf5
44#define OV534_REG_STATUS 0xf6
45
46#define OV534_OP_WRITE_3 0x37
47#define OV534_OP_WRITE_2 0x33
48#define OV534_OP_READ_2 0xf9
49
50#define CTRL_TIMEOUT 500
51
52MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
53MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
54MODULE_LICENSE("GPL");
55
Jean-François Moine228dd262011-02-10 09:34:57 -030056/* controls */
57enum e_ctrl {
Antonio Ospitee89fca92012-05-14 08:07:45 -030058 HUE,
Antonio Ospitee0fde592012-05-14 08:07:43 -030059 SATURATION,
Jean-François Moine228dd262011-02-10 09:34:57 -030060 BRIGHTNESS,
61 CONTRAST,
62 GAIN,
63 EXPOSURE,
64 AGC,
65 AWB,
66 AEC,
67 SHARPNESS,
68 HFLIP,
69 VFLIP,
70 LIGHTFREQ,
71 NCTRLS /* number of controls */
72};
73
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030074/* specific webcam descriptor */
75struct sd {
76 struct gspca_dev gspca_dev; /* !! must be the first item */
Jean-François Moine228dd262011-02-10 09:34:57 -030077
78 struct gspca_ctrl ctrls[NCTRLS];
79
Jean-Francois Moine8c252052008-12-04 05:06:08 -030080 __u32 last_pts;
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -030081 u16 last_fid;
82 u8 frame_rate;
Jean-Francois Moineb014f942009-11-11 14:28:53 -030083
Jean-François Moine458efe22011-02-10 10:11:04 -030084 u8 sensor;
85};
86enum sensors {
87 SENSOR_OV767x,
88 SENSOR_OV772x,
89 NSENSORS
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030090};
91
92/* V4L2 controls supported by the driver */
Antonio Ospitee89fca92012-05-14 08:07:45 -030093static void sethue(struct gspca_dev *gspca_dev);
Antonio Ospitee0fde592012-05-14 08:07:43 -030094static void setsaturation(struct gspca_dev *gspca_dev);
Jean-François Moine228dd262011-02-10 09:34:57 -030095static void setbrightness(struct gspca_dev *gspca_dev);
96static void setcontrast(struct gspca_dev *gspca_dev);
97static void setgain(struct gspca_dev *gspca_dev);
98static void setexposure(struct gspca_dev *gspca_dev);
Max Thrun8b7fbda2010-02-27 17:20:20 -030099static int sd_setagc(struct gspca_dev *gspca_dev, __s32 val);
Jean-François Moine228dd262011-02-10 09:34:57 -0300100static void setawb(struct gspca_dev *gspca_dev);
101static void setaec(struct gspca_dev *gspca_dev);
102static void setsharpness(struct gspca_dev *gspca_dev);
103static void sethvflip(struct gspca_dev *gspca_dev);
104static void setlightfreq(struct gspca_dev *gspca_dev);
105
106static int sd_start(struct gspca_dev *gspca_dev);
107static void sd_stopN(struct gspca_dev *gspca_dev);
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300108
Jean-Francois Moinec52af792010-01-07 05:18:16 -0300109static const struct ctrl sd_ctrls[] = {
Antonio Ospitee89fca92012-05-14 08:07:45 -0300110[HUE] = {
111 {
112 .id = V4L2_CID_HUE,
113 .type = V4L2_CTRL_TYPE_INTEGER,
114 .name = "Hue",
115 .minimum = -90,
116 .maximum = 90,
117 .step = 1,
118 .default_value = 0,
119 },
120 .set_control = sethue
121 },
Antonio Ospitee0fde592012-05-14 08:07:43 -0300122[SATURATION] = {
123 {
124 .id = V4L2_CID_SATURATION,
125 .type = V4L2_CTRL_TYPE_INTEGER,
126 .name = "Saturation",
127 .minimum = 0,
128 .maximum = 255,
129 .step = 1,
130 .default_value = 64,
131 },
132 .set_control = setsaturation
133 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300134[BRIGHTNESS] = {
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300135 {
136 .id = V4L2_CID_BRIGHTNESS,
137 .type = V4L2_CTRL_TYPE_INTEGER,
138 .name = "Brightness",
139 .minimum = 0,
140 .maximum = 255,
141 .step = 1,
Jean-François Moine228dd262011-02-10 09:34:57 -0300142 .default_value = 0,
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300143 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300144 .set_control = setbrightness
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300145 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300146[CONTRAST] = {
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300147 {
148 .id = V4L2_CID_CONTRAST,
149 .type = V4L2_CTRL_TYPE_INTEGER,
150 .name = "Contrast",
151 .minimum = 0,
152 .maximum = 255,
153 .step = 1,
Jean-François Moine228dd262011-02-10 09:34:57 -0300154 .default_value = 32,
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300155 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300156 .set_control = setcontrast
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300157 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300158[GAIN] = {
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300159 {
160 .id = V4L2_CID_GAIN,
161 .type = V4L2_CTRL_TYPE_INTEGER,
162 .name = "Main Gain",
163 .minimum = 0,
164 .maximum = 63,
165 .step = 1,
Jean-François Moine228dd262011-02-10 09:34:57 -0300166 .default_value = 20,
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300167 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300168 .set_control = setgain
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300169 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300170[EXPOSURE] = {
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300171 {
172 .id = V4L2_CID_EXPOSURE,
173 .type = V4L2_CTRL_TYPE_INTEGER,
174 .name = "Exposure",
175 .minimum = 0,
176 .maximum = 255,
177 .step = 1,
Jean-François Moine228dd262011-02-10 09:34:57 -0300178 .default_value = 120,
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300179 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300180 .set_control = setexposure
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300181 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300182[AGC] = {
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300183 {
184 .id = V4L2_CID_AUTOGAIN,
185 .type = V4L2_CTRL_TYPE_BOOLEAN,
186 .name = "Auto Gain",
187 .minimum = 0,
188 .maximum = 1,
189 .step = 1,
Jean-François Moine228dd262011-02-10 09:34:57 -0300190 .default_value = 1,
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300191 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300192 .set = sd_setagc
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300193 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300194[AWB] = {
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300195 {
196 .id = V4L2_CID_AUTO_WHITE_BALANCE,
197 .type = V4L2_CTRL_TYPE_BOOLEAN,
198 .name = "Auto White Balance",
199 .minimum = 0,
200 .maximum = 1,
201 .step = 1,
Jean-François Moine228dd262011-02-10 09:34:57 -0300202 .default_value = 1,
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300203 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300204 .set_control = setawb
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300205 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300206[AEC] = {
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300207 {
208 .id = V4L2_CID_EXPOSURE_AUTO,
209 .type = V4L2_CTRL_TYPE_BOOLEAN,
210 .name = "Auto Exposure",
211 .minimum = 0,
212 .maximum = 1,
213 .step = 1,
Jean-François Moine228dd262011-02-10 09:34:57 -0300214 .default_value = 1,
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300215 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300216 .set_control = setaec
Max Thrunf2938822010-02-27 17:20:21 -0300217 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300218[SHARPNESS] = {
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300219 {
220 .id = V4L2_CID_SHARPNESS,
221 .type = V4L2_CTRL_TYPE_INTEGER,
222 .name = "Sharpness",
223 .minimum = 0,
224 .maximum = 63,
225 .step = 1,
Jean-François Moine228dd262011-02-10 09:34:57 -0300226 .default_value = 0,
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300227 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300228 .set_control = setsharpness
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300229 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300230[HFLIP] = {
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300231 {
232 .id = V4L2_CID_HFLIP,
233 .type = V4L2_CTRL_TYPE_BOOLEAN,
234 .name = "HFlip",
235 .minimum = 0,
236 .maximum = 1,
237 .step = 1,
Jean-François Moine228dd262011-02-10 09:34:57 -0300238 .default_value = 0,
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300239 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300240 .set_control = sethvflip
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300241 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300242[VFLIP] = {
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300243 {
244 .id = V4L2_CID_VFLIP,
245 .type = V4L2_CTRL_TYPE_BOOLEAN,
246 .name = "VFlip",
247 .minimum = 0,
248 .maximum = 1,
249 .step = 1,
Jean-François Moine228dd262011-02-10 09:34:57 -0300250 .default_value = 0,
Antonio Ospite7f9e9e82010-03-01 08:54:33 -0300251 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300252 .set_control = sethvflip
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300253 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300254[LIGHTFREQ] = {
Mosalam Ebrahimi4b787542010-03-08 13:52:17 -0300255 {
256 .id = V4L2_CID_POWER_LINE_FREQUENCY,
257 .type = V4L2_CTRL_TYPE_MENU,
258 .name = "Light Frequency Filter",
259 .minimum = 0,
260 .maximum = 1,
261 .step = 1,
Jean-François Moine228dd262011-02-10 09:34:57 -0300262 .default_value = 0,
Mosalam Ebrahimi4b787542010-03-08 13:52:17 -0300263 },
Jean-François Moine228dd262011-02-10 09:34:57 -0300264 .set_control = setlightfreq
Mosalam Ebrahimi4b787542010-03-08 13:52:17 -0300265 },
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300266};
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300267
Jean-Francois Moine569691a2009-11-14 09:45:38 -0300268static const struct v4l2_pix_format ov772x_mode[] = {
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300269 {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
270 .bytesperline = 320 * 2,
271 .sizeimage = 320 * 240 * 2,
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300272 .colorspace = V4L2_COLORSPACE_SRGB,
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300273 .priv = 1},
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300274 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
275 .bytesperline = 640 * 2,
276 .sizeimage = 640 * 480 * 2,
Jean-Francois Moine191d0e72009-05-22 04:16:42 -0300277 .colorspace = V4L2_COLORSPACE_SRGB,
278 .priv = 0},
279};
Jean-François Moine458efe22011-02-10 10:11:04 -0300280static const struct v4l2_pix_format ov767x_mode[] = {
281 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
282 .bytesperline = 320,
283 .sizeimage = 320 * 240 * 3 / 8 + 590,
284 .colorspace = V4L2_COLORSPACE_JPEG},
285 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
286 .bytesperline = 640,
287 .sizeimage = 640 * 480 * 3 / 8 + 590,
288 .colorspace = V4L2_COLORSPACE_JPEG},
289};
Jean-Francois Moine191d0e72009-05-22 04:16:42 -0300290
Antonio Ospite29b87f02010-01-17 03:27:04 -0300291static const u8 qvga_rates[] = {125, 100, 75, 60, 50, 40, 30};
292static const u8 vga_rates[] = {60, 50, 40, 30, 15};
293
294static const struct framerates ov772x_framerates[] = {
295 { /* 320x240 */
296 .rates = qvga_rates,
297 .nrates = ARRAY_SIZE(qvga_rates),
298 },
299 { /* 640x480 */
300 .rates = vga_rates,
301 .nrates = ARRAY_SIZE(vga_rates),
302 },
303};
304
Jean-François Moine458efe22011-02-10 10:11:04 -0300305struct reg_array {
306 const u8 (*val)[2];
307 int len;
308};
309
310static const u8 bridge_init_767x[][2] = {
311/* comments from the ms-win file apollo7670.set */
312/* str1 */
313 {0xf1, 0x42},
314 {0x88, 0xf8},
315 {0x89, 0xff},
316 {0x76, 0x03},
317 {0x92, 0x03},
318 {0x95, 0x10},
319 {0xe2, 0x00},
320 {0xe7, 0x3e},
321 {0x8d, 0x1c},
322 {0x8e, 0x00},
323 {0x8f, 0x00},
324 {0x1f, 0x00},
325 {0xc3, 0xf9},
326 {0x89, 0xff},
327 {0x88, 0xf8},
328 {0x76, 0x03},
329 {0x92, 0x01},
330 {0x93, 0x18},
331 {0x1c, 0x00},
332 {0x1d, 0x48},
333 {0x1d, 0x00},
334 {0x1d, 0xff},
335 {0x1d, 0x02},
336 {0x1d, 0x58},
337 {0x1d, 0x00},
338 {0x1c, 0x0a},
339 {0x1d, 0x0a},
340 {0x1d, 0x0e},
341 {0xc0, 0x50}, /* HSize 640 */
342 {0xc1, 0x3c}, /* VSize 480 */
343 {0x34, 0x05}, /* enable Audio Suspend mode */
344 {0xc2, 0x0c}, /* Input YUV */
345 {0xc3, 0xf9}, /* enable PRE */
346 {0x34, 0x05}, /* enable Audio Suspend mode */
347 {0xe7, 0x2e}, /* this solves failure of "SuspendResumeTest" */
348 {0x31, 0xf9}, /* enable 1.8V Suspend */
349 {0x35, 0x02}, /* turn on JPEG */
350 {0xd9, 0x10},
351 {0x25, 0x42}, /* GPIO[8]:Input */
352 {0x94, 0x11}, /* If the default setting is loaded when
353 * system boots up, this flag is closed here */
354};
355static const u8 sensor_init_767x[][2] = {
356 {0x12, 0x80},
357 {0x11, 0x03},
358 {0x3a, 0x04},
359 {0x12, 0x00},
360 {0x17, 0x13},
361 {0x18, 0x01},
362 {0x32, 0xb6},
363 {0x19, 0x02},
364 {0x1a, 0x7a},
365 {0x03, 0x0a},
366 {0x0c, 0x00},
367 {0x3e, 0x00},
368 {0x70, 0x3a},
369 {0x71, 0x35},
370 {0x72, 0x11},
371 {0x73, 0xf0},
372 {0xa2, 0x02},
373 {0x7a, 0x2a}, /* set Gamma=1.6 below */
374 {0x7b, 0x12},
375 {0x7c, 0x1d},
376 {0x7d, 0x2d},
377 {0x7e, 0x45},
378 {0x7f, 0x50},
379 {0x80, 0x59},
380 {0x81, 0x62},
381 {0x82, 0x6b},
382 {0x83, 0x73},
383 {0x84, 0x7b},
384 {0x85, 0x8a},
385 {0x86, 0x98},
386 {0x87, 0xb2},
387 {0x88, 0xca},
388 {0x89, 0xe0},
389 {0x13, 0xe0},
390 {0x00, 0x00},
391 {0x10, 0x00},
392 {0x0d, 0x40},
393 {0x14, 0x38}, /* gain max 16x */
394 {0xa5, 0x05},
395 {0xab, 0x07},
396 {0x24, 0x95},
397 {0x25, 0x33},
398 {0x26, 0xe3},
399 {0x9f, 0x78},
400 {0xa0, 0x68},
401 {0xa1, 0x03},
402 {0xa6, 0xd8},
403 {0xa7, 0xd8},
404 {0xa8, 0xf0},
405 {0xa9, 0x90},
406 {0xaa, 0x94},
407 {0x13, 0xe5},
408 {0x0e, 0x61},
409 {0x0f, 0x4b},
410 {0x16, 0x02},
411 {0x21, 0x02},
412 {0x22, 0x91},
413 {0x29, 0x07},
414 {0x33, 0x0b},
415 {0x35, 0x0b},
416 {0x37, 0x1d},
417 {0x38, 0x71},
418 {0x39, 0x2a},
419 {0x3c, 0x78},
420 {0x4d, 0x40},
421 {0x4e, 0x20},
422 {0x69, 0x00},
423 {0x6b, 0x4a},
424 {0x74, 0x10},
425 {0x8d, 0x4f},
426 {0x8e, 0x00},
427 {0x8f, 0x00},
428 {0x90, 0x00},
429 {0x91, 0x00},
430 {0x96, 0x00},
431 {0x9a, 0x80},
432 {0xb0, 0x84},
433 {0xb1, 0x0c},
434 {0xb2, 0x0e},
435 {0xb3, 0x82},
436 {0xb8, 0x0a},
437 {0x43, 0x0a},
438 {0x44, 0xf0},
439 {0x45, 0x34},
440 {0x46, 0x58},
441 {0x47, 0x28},
442 {0x48, 0x3a},
443 {0x59, 0x88},
444 {0x5a, 0x88},
445 {0x5b, 0x44},
446 {0x5c, 0x67},
447 {0x5d, 0x49},
448 {0x5e, 0x0e},
449 {0x6c, 0x0a},
450 {0x6d, 0x55},
451 {0x6e, 0x11},
452 {0x6f, 0x9f},
453 {0x6a, 0x40},
454 {0x01, 0x40},
455 {0x02, 0x40},
456 {0x13, 0xe7},
457 {0x4f, 0x80},
458 {0x50, 0x80},
459 {0x51, 0x00},
460 {0x52, 0x22},
461 {0x53, 0x5e},
462 {0x54, 0x80},
463 {0x58, 0x9e},
464 {0x41, 0x08},
465 {0x3f, 0x00},
466 {0x75, 0x04},
467 {0x76, 0xe1},
468 {0x4c, 0x00},
469 {0x77, 0x01},
470 {0x3d, 0xc2},
471 {0x4b, 0x09},
472 {0xc9, 0x60},
473 {0x41, 0x38}, /* jfm: auto sharpness + auto de-noise */
474 {0x56, 0x40},
475 {0x34, 0x11},
476 {0x3b, 0xc2},
477 {0xa4, 0x8a}, /* Night mode trigger point */
478 {0x96, 0x00},
479 {0x97, 0x30},
480 {0x98, 0x20},
481 {0x99, 0x20},
482 {0x9a, 0x84},
483 {0x9b, 0x29},
484 {0x9c, 0x03},
485 {0x9d, 0x4c},
486 {0x9e, 0x3f},
487 {0x78, 0x04},
488 {0x79, 0x01},
489 {0xc8, 0xf0},
490 {0x79, 0x0f},
491 {0xc8, 0x00},
492 {0x79, 0x10},
493 {0xc8, 0x7e},
494 {0x79, 0x0a},
495 {0xc8, 0x80},
496 {0x79, 0x0b},
497 {0xc8, 0x01},
498 {0x79, 0x0c},
499 {0xc8, 0x0f},
500 {0x79, 0x0d},
501 {0xc8, 0x20},
502 {0x79, 0x09},
503 {0xc8, 0x80},
504 {0x79, 0x02},
505 {0xc8, 0xc0},
506 {0x79, 0x03},
507 {0xc8, 0x20},
508 {0x79, 0x26},
509};
510static const u8 bridge_start_vga_767x[][2] = {
511/* str59 JPG */
512 {0x94, 0xaa},
513 {0xf1, 0x42},
514 {0xe5, 0x04},
515 {0xc0, 0x50},
516 {0xc1, 0x3c},
517 {0xc2, 0x0c},
518 {0x35, 0x02}, /* turn on JPEG */
519 {0xd9, 0x10},
520 {0xda, 0x00}, /* for higher clock rate(30fps) */
521 {0x34, 0x05}, /* enable Audio Suspend mode */
522 {0xc3, 0xf9}, /* enable PRE */
523 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
524 {0x8d, 0x1c}, /* output YUV */
525/* {0x34, 0x05}, * enable Audio Suspend mode (?) */
526 {0x50, 0x00}, /* H/V divider=0 */
527 {0x51, 0xa0}, /* input H=640/4 */
528 {0x52, 0x3c}, /* input V=480/4 */
529 {0x53, 0x00}, /* offset X=0 */
530 {0x54, 0x00}, /* offset Y=0 */
531 {0x55, 0x00}, /* H/V size[8]=0 */
532 {0x57, 0x00}, /* H-size[9]=0 */
533 {0x5c, 0x00}, /* output size[9:8]=0 */
534 {0x5a, 0xa0}, /* output H=640/4 */
535 {0x5b, 0x78}, /* output V=480/4 */
536 {0x1c, 0x0a},
537 {0x1d, 0x0a},
538 {0x94, 0x11},
539};
540static const u8 sensor_start_vga_767x[][2] = {
541 {0x11, 0x01},
542 {0x1e, 0x04},
543 {0x19, 0x02},
544 {0x1a, 0x7a},
545};
546static const u8 bridge_start_qvga_767x[][2] = {
547/* str86 JPG */
548 {0x94, 0xaa},
549 {0xf1, 0x42},
550 {0xe5, 0x04},
551 {0xc0, 0x80},
552 {0xc1, 0x60},
553 {0xc2, 0x0c},
554 {0x35, 0x02}, /* turn on JPEG */
555 {0xd9, 0x10},
556 {0xc0, 0x50}, /* CIF HSize 640 */
557 {0xc1, 0x3c}, /* CIF VSize 480 */
558 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
559 {0x8d, 0x1c}, /* output YUV */
560 {0x34, 0x05}, /* enable Audio Suspend mode */
561 {0xc2, 0x4c}, /* output YUV and Enable DCW */
562 {0xc3, 0xf9}, /* enable PRE */
563 {0x1c, 0x00}, /* indirect addressing */
564 {0x1d, 0x48}, /* output YUV422 */
565 {0x50, 0x89}, /* H/V divider=/2; plus DCW AVG */
566 {0x51, 0xa0}, /* DCW input H=640/4 */
567 {0x52, 0x78}, /* DCW input V=480/4 */
568 {0x53, 0x00}, /* offset X=0 */
569 {0x54, 0x00}, /* offset Y=0 */
570 {0x55, 0x00}, /* H/V size[8]=0 */
571 {0x57, 0x00}, /* H-size[9]=0 */
572 {0x5c, 0x00}, /* DCW output size[9:8]=0 */
573 {0x5a, 0x50}, /* DCW output H=320/4 */
574 {0x5b, 0x3c}, /* DCW output V=240/4 */
575 {0x1c, 0x0a},
576 {0x1d, 0x0a},
577 {0x94, 0x11},
578};
579static const u8 sensor_start_qvga_767x[][2] = {
580 {0x11, 0x01},
581 {0x1e, 0x04},
582 {0x19, 0x02},
583 {0x1a, 0x7a},
584};
585
586static const u8 bridge_init_772x[][2] = {
Jim Paris47dfd212008-12-04 04:28:27 -0300587 { 0xc2, 0x0c },
588 { 0x88, 0xf8 },
589 { 0xc3, 0x69 },
590 { 0x89, 0xff },
591 { 0x76, 0x03 },
592 { 0x92, 0x01 },
593 { 0x93, 0x18 },
594 { 0x94, 0x10 },
595 { 0x95, 0x10 },
596 { 0xe2, 0x00 },
597 { 0xe7, 0x3e },
598
Jim Paris47dfd212008-12-04 04:28:27 -0300599 { 0x96, 0x00 },
600
601 { 0x97, 0x20 },
602 { 0x97, 0x20 },
603 { 0x97, 0x20 },
604 { 0x97, 0x0a },
605 { 0x97, 0x3f },
606 { 0x97, 0x4a },
607 { 0x97, 0x20 },
608 { 0x97, 0x15 },
609 { 0x97, 0x0b },
610
611 { 0x8e, 0x40 },
612 { 0x1f, 0x81 },
613 { 0x34, 0x05 },
614 { 0xe3, 0x04 },
615 { 0x88, 0x00 },
616 { 0x89, 0x00 },
617 { 0x76, 0x00 },
618 { 0xe7, 0x2e },
619 { 0x31, 0xf9 },
620 { 0x25, 0x42 },
621 { 0x21, 0xf0 },
622
623 { 0x1c, 0x00 },
624 { 0x1d, 0x40 },
Jim Paris0f7a50b2008-12-10 05:45:14 -0300625 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
626 { 0x1d, 0x00 }, /* payload size */
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300627
Jim Paris5ea9c4d2008-12-04 04:36:14 -0300628 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
629 { 0x1d, 0x58 }, /* frame size */
630 { 0x1d, 0x00 }, /* frame size */
Jim Paris47dfd212008-12-04 04:28:27 -0300631
Jim Parisc06eb612008-12-10 05:47:44 -0300632 { 0x1c, 0x0a },
633 { 0x1d, 0x08 }, /* turn on UVC header */
634 { 0x1d, 0x0e }, /* .. */
635
Jim Paris47dfd212008-12-04 04:28:27 -0300636 { 0x8d, 0x1c },
637 { 0x8e, 0x80 },
638 { 0xe5, 0x04 },
639
640 { 0xc0, 0x50 },
641 { 0xc1, 0x3c },
642 { 0xc2, 0x0c },
643};
Jean-François Moine458efe22011-02-10 10:11:04 -0300644static const u8 sensor_init_772x[][2] = {
Jim Paris47dfd212008-12-04 04:28:27 -0300645 { 0x12, 0x80 },
646 { 0x11, 0x01 },
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300647/*fixme: better have a delay?*/
648 { 0x11, 0x01 },
649 { 0x11, 0x01 },
650 { 0x11, 0x01 },
651 { 0x11, 0x01 },
652 { 0x11, 0x01 },
653 { 0x11, 0x01 },
654 { 0x11, 0x01 },
655 { 0x11, 0x01 },
656 { 0x11, 0x01 },
657 { 0x11, 0x01 },
Jim Paris47dfd212008-12-04 04:28:27 -0300658
659 { 0x3d, 0x03 },
660 { 0x17, 0x26 },
661 { 0x18, 0xa0 },
662 { 0x19, 0x07 },
663 { 0x1a, 0xf0 },
664 { 0x32, 0x00 },
665 { 0x29, 0xa0 },
666 { 0x2c, 0xf0 },
667 { 0x65, 0x20 },
668 { 0x11, 0x01 },
669 { 0x42, 0x7f },
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300670 { 0x63, 0xaa }, /* AWB - was e0 */
Jim Paris47dfd212008-12-04 04:28:27 -0300671 { 0x64, 0xff },
672 { 0x66, 0x00 },
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300673 { 0x13, 0xf0 }, /* com8 */
Jim Paris47dfd212008-12-04 04:28:27 -0300674 { 0x0d, 0x41 },
675 { 0x0f, 0xc5 },
676 { 0x14, 0x11 },
677
678 { 0x22, 0x7f },
679 { 0x23, 0x03 },
680 { 0x24, 0x40 },
681 { 0x25, 0x30 },
682 { 0x26, 0xa1 },
683 { 0x2a, 0x00 },
684 { 0x2b, 0x00 },
685 { 0x6b, 0xaa },
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300686 { 0x13, 0xff }, /* AWB */
Jim Paris47dfd212008-12-04 04:28:27 -0300687
688 { 0x90, 0x05 },
689 { 0x91, 0x01 },
690 { 0x92, 0x03 },
691 { 0x93, 0x00 },
692 { 0x94, 0x60 },
693 { 0x95, 0x3c },
694 { 0x96, 0x24 },
695 { 0x97, 0x1e },
696 { 0x98, 0x62 },
697 { 0x99, 0x80 },
698 { 0x9a, 0x1e },
699 { 0x9b, 0x08 },
700 { 0x9c, 0x20 },
701 { 0x9e, 0x81 },
702
Antonio Ospitee89fca92012-05-14 08:07:45 -0300703 { 0xa6, 0x07 },
Jim Paris47dfd212008-12-04 04:28:27 -0300704 { 0x7e, 0x0c },
705 { 0x7f, 0x16 },
706 { 0x80, 0x2a },
707 { 0x81, 0x4e },
708 { 0x82, 0x61 },
709 { 0x83, 0x6f },
710 { 0x84, 0x7b },
711 { 0x85, 0x86 },
712 { 0x86, 0x8e },
713 { 0x87, 0x97 },
714 { 0x88, 0xa4 },
715 { 0x89, 0xaf },
716 { 0x8a, 0xc5 },
717 { 0x8b, 0xd7 },
718 { 0x8c, 0xe8 },
719 { 0x8d, 0x20 },
720
721 { 0x0c, 0x90 },
722
723 { 0x2b, 0x00 },
724 { 0x22, 0x7f },
725 { 0x23, 0x03 },
726 { 0x11, 0x01 },
727 { 0x0c, 0xd0 },
728 { 0x64, 0xff },
729 { 0x0d, 0x41 },
730
731 { 0x14, 0x41 },
732 { 0x0e, 0xcd },
733 { 0xac, 0xbf },
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300734 { 0x8e, 0x00 }, /* De-noise threshold */
Jim Paris47dfd212008-12-04 04:28:27 -0300735 { 0x0c, 0xd0 }
736};
Jean-François Moine458efe22011-02-10 10:11:04 -0300737static const u8 bridge_start_vga_772x[][2] = {
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300738 {0x1c, 0x00},
739 {0x1d, 0x40},
740 {0x1d, 0x02},
741 {0x1d, 0x00},
742 {0x1d, 0x02},
743 {0x1d, 0x58},
744 {0x1d, 0x00},
745 {0xc0, 0x50},
746 {0xc1, 0x3c},
747};
Jean-François Moine458efe22011-02-10 10:11:04 -0300748static const u8 sensor_start_vga_772x[][2] = {
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300749 {0x12, 0x00},
750 {0x17, 0x26},
751 {0x18, 0xa0},
752 {0x19, 0x07},
753 {0x1a, 0xf0},
754 {0x29, 0xa0},
755 {0x2c, 0xf0},
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300756 {0x65, 0x20},
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300757};
Jean-François Moine458efe22011-02-10 10:11:04 -0300758static const u8 bridge_start_qvga_772x[][2] = {
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300759 {0x1c, 0x00},
760 {0x1d, 0x40},
761 {0x1d, 0x02},
762 {0x1d, 0x00},
763 {0x1d, 0x01},
764 {0x1d, 0x4b},
765 {0x1d, 0x00},
766 {0xc0, 0x28},
767 {0xc1, 0x1e},
768};
Jean-François Moine458efe22011-02-10 10:11:04 -0300769static const u8 sensor_start_qvga_772x[][2] = {
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300770 {0x12, 0x40},
771 {0x17, 0x3f},
772 {0x18, 0x50},
773 {0x19, 0x03},
774 {0x1a, 0x78},
775 {0x29, 0x50},
776 {0x2c, 0x78},
Jean-Francois Moineb014f942009-11-11 14:28:53 -0300777 {0x65, 0x2f},
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300778};
Jim Paris47dfd212008-12-04 04:28:27 -0300779
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300780static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
781{
782 struct usb_device *udev = gspca_dev->dev;
783 int ret;
784
Jean-François Moine14b67c22011-01-13 05:58:04 -0300785 if (gspca_dev->usb_err < 0)
786 return;
787
Jean-François Moineddffa492011-01-13 05:49:47 -0300788 PDEBUG(D_USBO, "SET 01 0000 %04x %02x", reg, val);
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300789 gspca_dev->usb_buf[0] = val;
790 ret = usb_control_msg(udev,
791 usb_sndctrlpipe(udev, 0),
792 0x01,
793 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
794 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
Jean-François Moine14b67c22011-01-13 05:58:04 -0300795 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300796 pr_err("write failed %d\n", ret);
Jean-François Moine14b67c22011-01-13 05:58:04 -0300797 gspca_dev->usb_err = ret;
798 }
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300799}
800
801static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
802{
803 struct usb_device *udev = gspca_dev->dev;
804 int ret;
805
Jean-François Moine14b67c22011-01-13 05:58:04 -0300806 if (gspca_dev->usb_err < 0)
807 return 0;
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300808 ret = usb_control_msg(udev,
809 usb_rcvctrlpipe(udev, 0),
810 0x01,
811 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
812 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
Jean-François Moineddffa492011-01-13 05:49:47 -0300813 PDEBUG(D_USBI, "GET 01 0000 %04x %02x", reg, gspca_dev->usb_buf[0]);
Jean-François Moine14b67c22011-01-13 05:58:04 -0300814 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300815 pr_err("read failed %d\n", ret);
Jean-François Moine14b67c22011-01-13 05:58:04 -0300816 gspca_dev->usb_err = ret;
817 }
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300818 return gspca_dev->usb_buf[0];
819}
820
821/* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
822 * (direction and output)? */
823static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
824{
825 u8 data;
826
827 PDEBUG(D_CONF, "led status: %d", status);
828
829 data = ov534_reg_read(gspca_dev, 0x21);
830 data |= 0x80;
831 ov534_reg_write(gspca_dev, 0x21, data);
832
833 data = ov534_reg_read(gspca_dev, 0x23);
834 if (status)
835 data |= 0x80;
836 else
837 data &= ~0x80;
838
839 ov534_reg_write(gspca_dev, 0x23, data);
840
841 if (!status) {
842 data = ov534_reg_read(gspca_dev, 0x21);
843 data &= ~0x80;
844 ov534_reg_write(gspca_dev, 0x21, data);
845 }
846}
847
848static int sccb_check_status(struct gspca_dev *gspca_dev)
849{
850 u8 data;
851 int i;
852
853 for (i = 0; i < 5; i++) {
854 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
855
856 switch (data) {
857 case 0x00:
858 return 1;
859 case 0x04:
860 return 0;
861 case 0x03:
862 break;
863 default:
864 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
865 data, i + 1);
866 }
867 }
868 return 0;
869}
870
871static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
872{
Jean-François Moineddffa492011-01-13 05:49:47 -0300873 PDEBUG(D_USBO, "sccb write: %02x %02x", reg, val);
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300874 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
875 ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
876 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
877
Jean-François Moine14b67c22011-01-13 05:58:04 -0300878 if (!sccb_check_status(gspca_dev)) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300879 pr_err("sccb_reg_write failed\n");
Jean-François Moine14b67c22011-01-13 05:58:04 -0300880 gspca_dev->usb_err = -EIO;
881 }
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300882}
883
884static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
885{
886 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
887 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
888 if (!sccb_check_status(gspca_dev))
Joe Perches133a9fe2011-08-21 19:56:57 -0300889 pr_err("sccb_reg_read failed 1\n");
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300890
891 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
892 if (!sccb_check_status(gspca_dev))
Joe Perches133a9fe2011-08-21 19:56:57 -0300893 pr_err("sccb_reg_read failed 2\n");
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300894
895 return ov534_reg_read(gspca_dev, OV534_REG_READ);
896}
897
898/* output a bridge sequence (reg - val) */
899static void reg_w_array(struct gspca_dev *gspca_dev,
900 const u8 (*data)[2], int len)
901{
902 while (--len >= 0) {
903 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
904 data++;
905 }
906}
907
908/* output a sensor sequence (reg - val) */
909static void sccb_w_array(struct gspca_dev *gspca_dev,
910 const u8 (*data)[2], int len)
911{
912 while (--len >= 0) {
913 if ((*data)[0] != 0xff) {
914 sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
915 } else {
916 sccb_reg_read(gspca_dev, (*data)[1]);
917 sccb_reg_write(gspca_dev, 0xff, 0x00);
918 }
919 data++;
920 }
921}
922
Jean-Francois Moine69f1fe22009-11-12 06:10:36 -0300923/* ov772x specific controls */
924static void set_frame_rate(struct gspca_dev *gspca_dev)
Jim Paris11d9f252008-12-10 06:06:20 -0300925{
926 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300927 int i;
928 struct rate_s {
929 u8 fps;
930 u8 r11;
931 u8 r0d;
932 u8 re5;
933 };
934 const struct rate_s *r;
935 static const struct rate_s rate_0[] = { /* 640x480 */
936 {60, 0x01, 0xc1, 0x04},
937 {50, 0x01, 0x41, 0x02},
938 {40, 0x02, 0xc1, 0x04},
939 {30, 0x04, 0x81, 0x02},
940 {15, 0x03, 0x41, 0x04},
941 };
942 static const struct rate_s rate_1[] = { /* 320x240 */
943 {125, 0x02, 0x81, 0x02},
944 {100, 0x02, 0xc1, 0x04},
945 {75, 0x03, 0xc1, 0x04},
946 {60, 0x04, 0xc1, 0x04},
947 {50, 0x02, 0x41, 0x04},
948 {40, 0x03, 0x41, 0x04},
949 {30, 0x04, 0x41, 0x04},
950 };
Jim Paris11d9f252008-12-10 06:06:20 -0300951
Jean-François Moine458efe22011-02-10 10:11:04 -0300952 if (sd->sensor != SENSOR_OV772x)
953 return;
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300954 if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
955 r = rate_0;
956 i = ARRAY_SIZE(rate_0);
957 } else {
958 r = rate_1;
959 i = ARRAY_SIZE(rate_1);
960 }
961 while (--i > 0) {
962 if (sd->frame_rate >= r->fps)
963 break;
964 r++;
Jim Paris11d9f252008-12-10 06:06:20 -0300965 }
966
Jean-Francois Moine189d92a2009-11-11 07:46:28 -0300967 sccb_reg_write(gspca_dev, 0x11, r->r11);
968 sccb_reg_write(gspca_dev, 0x0d, r->r0d);
969 ov534_reg_write(gspca_dev, 0xe5, r->re5);
970
971 PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
972}
973
Antonio Ospitee89fca92012-05-14 08:07:45 -0300974static void sethue(struct gspca_dev *gspca_dev)
975{
976 struct sd *sd = (struct sd *) gspca_dev;
977 int val;
978
979 val = sd->ctrls[HUE].val;
980 if (sd->sensor == SENSOR_OV767x) {
981 /* TBD */
982 } else {
983 s16 huesin;
984 s16 huecos;
985
986 /* fixp_sin and fixp_cos accept only positive values, while
987 * our val is between -90 and 90
988 */
989 val += 360;
990
991 /* According to the datasheet the registers expect HUESIN and
992 * HUECOS to be the result of the trigonometric functions,
993 * scaled by 0x80.
994 *
995 * The 0x100 here represents the maximun absolute value
996 * returned byt fixp_sin and fixp_cos, so the scaling will
997 * consider the result like in the interval [-1.0, 1.0].
998 */
999 huesin = fixp_sin(val) * 0x80 / 0x100;
1000 huecos = fixp_cos(val) * 0x80 / 0x100;
1001
1002 if (huesin < 0) {
1003 sccb_reg_write(gspca_dev, 0xab,
1004 sccb_reg_read(gspca_dev, 0xab) | 0x2);
1005 huesin = -huesin;
1006 } else {
1007 sccb_reg_write(gspca_dev, 0xab,
1008 sccb_reg_read(gspca_dev, 0xab) & ~0x2);
1009
1010 }
1011 sccb_reg_write(gspca_dev, 0xa9, (u8)huecos);
1012 sccb_reg_write(gspca_dev, 0xaa, (u8)huesin);
1013 }
1014}
1015
Antonio Ospitee0fde592012-05-14 08:07:43 -03001016static void setsaturation(struct gspca_dev *gspca_dev)
1017{
1018 struct sd *sd = (struct sd *) gspca_dev;
1019 int val;
1020
1021 val = sd->ctrls[SATURATION].val;
1022 if (sd->sensor == SENSOR_OV767x) {
1023 int i;
1024 static u8 color_tb[][6] = {
1025 {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
1026 {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
1027 {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
1028 {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
1029 {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
1030 {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
1031 {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
1032 };
1033
1034 for (i = 0; i < ARRAY_SIZE(color_tb[0]); i++)
1035 sccb_reg_write(gspca_dev, 0x4f + i, color_tb[val][i]);
1036 } else {
1037 sccb_reg_write(gspca_dev, 0xa7, val); /* U saturation */
1038 sccb_reg_write(gspca_dev, 0xa8, val); /* V saturation */
1039 }
1040}
1041
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001042static void setbrightness(struct gspca_dev *gspca_dev)
Jean-Francois Moineb014f942009-11-11 14:28:53 -03001043{
1044 struct sd *sd = (struct sd *) gspca_dev;
Jean-François Moine458efe22011-02-10 10:11:04 -03001045 int val;
Jean-Francois Moineb014f942009-11-11 14:28:53 -03001046
Jean-François Moine458efe22011-02-10 10:11:04 -03001047 val = sd->ctrls[BRIGHTNESS].val;
1048 if (sd->sensor == SENSOR_OV767x) {
1049 if (val < 0)
1050 val = 0x80 - val;
1051 sccb_reg_write(gspca_dev, 0x55, val); /* bright */
1052 } else {
1053 sccb_reg_write(gspca_dev, 0x9b, val);
1054 }
Jean-Francois Moineb014f942009-11-11 14:28:53 -03001055}
1056
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001057static void setcontrast(struct gspca_dev *gspca_dev)
Jean-Francois Moineb014f942009-11-11 14:28:53 -03001058{
1059 struct sd *sd = (struct sd *) gspca_dev;
Jean-François Moine458efe22011-02-10 10:11:04 -03001060 u8 val;
Jean-Francois Moineb014f942009-11-11 14:28:53 -03001061
Jean-François Moine458efe22011-02-10 10:11:04 -03001062 val = sd->ctrls[CONTRAST].val;
1063 if (sd->sensor == SENSOR_OV767x)
1064 sccb_reg_write(gspca_dev, 0x56, val); /* contras */
1065 else
1066 sccb_reg_write(gspca_dev, 0x9c, val);
Jean-Francois Moineb014f942009-11-11 14:28:53 -03001067}
1068
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001069static void setgain(struct gspca_dev *gspca_dev)
1070{
1071 struct sd *sd = (struct sd *) gspca_dev;
1072 u8 val;
1073
Jean-François Moine228dd262011-02-10 09:34:57 -03001074 if (sd->ctrls[AGC].val)
Max Thrun8b7fbda2010-02-27 17:20:20 -03001075 return;
1076
Jean-François Moine228dd262011-02-10 09:34:57 -03001077 val = sd->ctrls[GAIN].val;
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001078 switch (val & 0x30) {
1079 case 0x00:
1080 val &= 0x0f;
1081 break;
1082 case 0x10:
1083 val &= 0x0f;
1084 val |= 0x30;
1085 break;
1086 case 0x20:
1087 val &= 0x0f;
1088 val |= 0x70;
1089 break;
1090 default:
1091/* case 0x30: */
1092 val &= 0x0f;
1093 val |= 0xf0;
1094 break;
1095 }
1096 sccb_reg_write(gspca_dev, 0x00, val);
1097}
1098
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001099static void setexposure(struct gspca_dev *gspca_dev)
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001100{
1101 struct sd *sd = (struct sd *) gspca_dev;
1102 u8 val;
1103
Jean-François Moine228dd262011-02-10 09:34:57 -03001104 if (sd->ctrls[AEC].val)
Antonio Ospite18cf8c92010-03-01 08:53:34 -03001105 return;
1106
Jean-François Moine228dd262011-02-10 09:34:57 -03001107 val = sd->ctrls[EXPOSURE].val;
Jean-François Moine458efe22011-02-10 10:11:04 -03001108 if (sd->sensor == SENSOR_OV767x) {
1109
1110 /* set only aec[9:2] */
1111 sccb_reg_write(gspca_dev, 0x10, val); /* aech */
1112 } else {
1113
1114 /* 'val' is one byte and represents half of the exposure value
1115 * we are going to set into registers, a two bytes value:
1116 *
1117 * MSB: ((u16) val << 1) >> 8 == val >> 7
1118 * LSB: ((u16) val << 1) & 0xff == val << 1
1119 */
1120 sccb_reg_write(gspca_dev, 0x08, val >> 7);
1121 sccb_reg_write(gspca_dev, 0x10, val << 1);
1122 }
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001123}
1124
Max Thrun8b7fbda2010-02-27 17:20:20 -03001125static void setagc(struct gspca_dev *gspca_dev)
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001126{
1127 struct sd *sd = (struct sd *) gspca_dev;
1128
Jean-François Moine228dd262011-02-10 09:34:57 -03001129 if (sd->ctrls[AGC].val) {
Max Thrun8b7fbda2010-02-27 17:20:20 -03001130 sccb_reg_write(gspca_dev, 0x13,
1131 sccb_reg_read(gspca_dev, 0x13) | 0x04);
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001132 sccb_reg_write(gspca_dev, 0x64,
1133 sccb_reg_read(gspca_dev, 0x64) | 0x03);
1134 } else {
Max Thrun8b7fbda2010-02-27 17:20:20 -03001135 sccb_reg_write(gspca_dev, 0x13,
1136 sccb_reg_read(gspca_dev, 0x13) & ~0x04);
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001137 sccb_reg_write(gspca_dev, 0x64,
Max Thrun8b7fbda2010-02-27 17:20:20 -03001138 sccb_reg_read(gspca_dev, 0x64) & ~0x03);
1139
1140 setgain(gspca_dev);
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001141 }
1142}
1143
Jean-Francois Moineb014f942009-11-11 14:28:53 -03001144static void setawb(struct gspca_dev *gspca_dev)
1145{
1146 struct sd *sd = (struct sd *) gspca_dev;
1147
Jean-François Moine228dd262011-02-10 09:34:57 -03001148 if (sd->ctrls[AWB].val) {
Max Thrun8fef9d92010-02-27 17:20:23 -03001149 sccb_reg_write(gspca_dev, 0x13,
1150 sccb_reg_read(gspca_dev, 0x13) | 0x02);
Jean-François Moine458efe22011-02-10 10:11:04 -03001151 if (sd->sensor == SENSOR_OV772x)
1152 sccb_reg_write(gspca_dev, 0x63,
Max Thrun8fef9d92010-02-27 17:20:23 -03001153 sccb_reg_read(gspca_dev, 0x63) | 0xc0);
1154 } else {
1155 sccb_reg_write(gspca_dev, 0x13,
1156 sccb_reg_read(gspca_dev, 0x13) & ~0x02);
Jean-François Moine458efe22011-02-10 10:11:04 -03001157 if (sd->sensor == SENSOR_OV772x)
1158 sccb_reg_write(gspca_dev, 0x63,
Max Thrun8fef9d92010-02-27 17:20:23 -03001159 sccb_reg_read(gspca_dev, 0x63) & ~0xc0);
1160 }
Jean-Francois Moineb014f942009-11-11 14:28:53 -03001161}
1162
Max Thrunf2938822010-02-27 17:20:21 -03001163static void setaec(struct gspca_dev *gspca_dev)
1164{
1165 struct sd *sd = (struct sd *) gspca_dev;
Jean-François Moine458efe22011-02-10 10:11:04 -03001166 u8 data;
Max Thrunf2938822010-02-27 17:20:21 -03001167
Jean-François Moine458efe22011-02-10 10:11:04 -03001168 data = sd->sensor == SENSOR_OV767x ?
1169 0x05 : /* agc + aec */
1170 0x01; /* agc */
Jean-François Moine228dd262011-02-10 09:34:57 -03001171 if (sd->ctrls[AEC].val)
Max Thrunf2938822010-02-27 17:20:21 -03001172 sccb_reg_write(gspca_dev, 0x13,
Jean-François Moine458efe22011-02-10 10:11:04 -03001173 sccb_reg_read(gspca_dev, 0x13) | data);
Max Thrunf2938822010-02-27 17:20:21 -03001174 else {
1175 sccb_reg_write(gspca_dev, 0x13,
Jean-François Moine458efe22011-02-10 10:11:04 -03001176 sccb_reg_read(gspca_dev, 0x13) & ~data);
1177 if (sd->sensor == SENSOR_OV767x)
1178 sd->ctrls[EXPOSURE].val =
1179 sccb_reg_read(gspca_dev, 10); /* aech */
1180 else
1181 setexposure(gspca_dev);
Max Thrunf2938822010-02-27 17:20:21 -03001182 }
1183}
1184
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001185static void setsharpness(struct gspca_dev *gspca_dev)
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001186{
1187 struct sd *sd = (struct sd *) gspca_dev;
1188 u8 val;
1189
Jean-François Moine228dd262011-02-10 09:34:57 -03001190 val = sd->ctrls[SHARPNESS].val;
Max Thrun3149cfb2010-02-27 17:20:24 -03001191 sccb_reg_write(gspca_dev, 0x91, val); /* Auto de-noise threshold */
1192 sccb_reg_write(gspca_dev, 0x8e, val); /* De-noise threshold */
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001193}
1194
Jean-François Moine228dd262011-02-10 09:34:57 -03001195static void sethvflip(struct gspca_dev *gspca_dev)
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001196{
1197 struct sd *sd = (struct sd *) gspca_dev;
Jean-François Moine228dd262011-02-10 09:34:57 -03001198 u8 val;
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001199
Jean-François Moine458efe22011-02-10 10:11:04 -03001200 if (sd->sensor == SENSOR_OV767x) {
1201 val = sccb_reg_read(gspca_dev, 0x1e); /* mvfp */
1202 val &= ~0x30;
1203 if (sd->ctrls[HFLIP].val)
1204 val |= 0x20;
1205 if (sd->ctrls[VFLIP].val)
1206 val |= 0x10;
1207 sccb_reg_write(gspca_dev, 0x1e, val);
1208 } else {
1209 val = sccb_reg_read(gspca_dev, 0x0c);
1210 val &= ~0xc0;
1211 if (sd->ctrls[HFLIP].val == 0)
1212 val |= 0x40;
1213 if (sd->ctrls[VFLIP].val == 0)
1214 val |= 0x80;
1215 sccb_reg_write(gspca_dev, 0x0c, val);
1216 }
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001217}
1218
Jean-François Moine228dd262011-02-10 09:34:57 -03001219static void setlightfreq(struct gspca_dev *gspca_dev)
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001220{
1221 struct sd *sd = (struct sd *) gspca_dev;
Jean-François Moine458efe22011-02-10 10:11:04 -03001222 u8 val;
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001223
Jean-François Moine458efe22011-02-10 10:11:04 -03001224 val = sd->ctrls[LIGHTFREQ].val ? 0x9e : 0x00;
1225 if (sd->sensor == SENSOR_OV767x) {
1226 sccb_reg_write(gspca_dev, 0x2a, 0x00);
1227 if (val)
1228 val = 0x9d; /* insert dummy to 25fps for 50Hz */
1229 }
1230 sccb_reg_write(gspca_dev, 0x2b, val);
Mosalam Ebrahimi4b787542010-03-08 13:52:17 -03001231}
1232
1233
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001234/* this function is called at probe time */
1235static int sd_config(struct gspca_dev *gspca_dev,
1236 const struct usb_device_id *id)
1237{
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -03001238 struct sd *sd = (struct sd *) gspca_dev;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001239 struct cam *cam;
1240
1241 cam = &gspca_dev->cam;
1242
Jean-François Moine228dd262011-02-10 09:34:57 -03001243 cam->ctrls = sd->ctrls;
1244
1245 /* the auto white balance control works only when auto gain is set */
1246 if (sd_ctrls[AGC].qctrl.default_value == 0)
1247 gspca_dev->ctrl_inac |= (1 << AWB);
1248
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001249 cam->cam_mode = ov772x_mode;
1250 cam->nmodes = ARRAY_SIZE(ov772x_mode);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001251
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001252 sd->frame_rate = 30;
Jean-Francois Moineb014f942009-11-11 14:28:53 -03001253
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001254 return 0;
1255}
1256
1257/* this function is called at probe and resume time */
1258static int sd_init(struct gspca_dev *gspca_dev)
1259{
Jean-François Moine458efe22011-02-10 10:11:04 -03001260 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -03001261 u16 sensor_id;
Jean-François Moine458efe22011-02-10 10:11:04 -03001262 static const struct reg_array bridge_init[NSENSORS] = {
1263 [SENSOR_OV767x] = {bridge_init_767x, ARRAY_SIZE(bridge_init_767x)},
1264 [SENSOR_OV772x] = {bridge_init_772x, ARRAY_SIZE(bridge_init_772x)},
1265 };
1266 static const struct reg_array sensor_init[NSENSORS] = {
1267 [SENSOR_OV767x] = {sensor_init_767x, ARRAY_SIZE(sensor_init_767x)},
1268 [SENSOR_OV772x] = {sensor_init_772x, ARRAY_SIZE(sensor_init_772x)},
1269 };
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -03001270
1271 /* reset bridge */
1272 ov534_reg_write(gspca_dev, 0xe7, 0x3a);
1273 ov534_reg_write(gspca_dev, 0xe0, 0x08);
1274 msleep(100);
1275
1276 /* initialize the sensor address */
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001277 ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -03001278
1279 /* reset sensor */
1280 sccb_reg_write(gspca_dev, 0x12, 0x80);
1281 msleep(10);
1282
1283 /* probe the sensor */
1284 sccb_reg_read(gspca_dev, 0x0a);
1285 sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
1286 sccb_reg_read(gspca_dev, 0x0b);
1287 sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
1288 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
1289
Jean-François Moine458efe22011-02-10 10:11:04 -03001290 if ((sensor_id & 0xfff0) == 0x7670) {
1291 sd->sensor = SENSOR_OV767x;
Antonio Ospitee89fca92012-05-14 08:07:45 -03001292 gspca_dev->ctrl_dis = (1 << HUE) |
1293 (1 << GAIN) |
Jean-François Moine458efe22011-02-10 10:11:04 -03001294 (1 << AGC) |
1295 (1 << SHARPNESS); /* auto */
Antonio Ospitee0fde592012-05-14 08:07:43 -03001296 sd->ctrls[SATURATION].min = 0,
1297 sd->ctrls[SATURATION].max = 6,
1298 sd->ctrls[SATURATION].def = 3,
Jean-François Moine458efe22011-02-10 10:11:04 -03001299 sd->ctrls[BRIGHTNESS].min = -127;
1300 sd->ctrls[BRIGHTNESS].max = 127;
1301 sd->ctrls[BRIGHTNESS].def = 0;
1302 sd->ctrls[CONTRAST].max = 0x80;
1303 sd->ctrls[CONTRAST].def = 0x40;
1304 sd->ctrls[EXPOSURE].min = 0x08;
1305 sd->ctrls[EXPOSURE].max = 0x60;
1306 sd->ctrls[EXPOSURE].def = 0x13;
1307 sd->ctrls[SHARPNESS].max = 9;
1308 sd->ctrls[SHARPNESS].def = 4;
1309 sd->ctrls[HFLIP].def = 1;
1310 gspca_dev->cam.cam_mode = ov767x_mode;
1311 gspca_dev->cam.nmodes = ARRAY_SIZE(ov767x_mode);
1312 } else {
1313 sd->sensor = SENSOR_OV772x;
1314 gspca_dev->cam.bulk = 1;
1315 gspca_dev->cam.bulk_size = 16384;
1316 gspca_dev->cam.bulk_nurbs = 2;
1317 gspca_dev->cam.mode_framerates = ov772x_framerates;
1318 }
1319
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -03001320 /* initialize */
Jean-François Moine458efe22011-02-10 10:11:04 -03001321 reg_w_array(gspca_dev, bridge_init[sd->sensor].val,
1322 bridge_init[sd->sensor].len);
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001323 ov534_set_led(gspca_dev, 1);
Jean-François Moine458efe22011-02-10 10:11:04 -03001324 sccb_w_array(gspca_dev, sensor_init[sd->sensor].val,
1325 sensor_init[sd->sensor].len);
1326 if (sd->sensor == SENSOR_OV767x)
1327 sd_start(gspca_dev);
1328 sd_stopN(gspca_dev);
1329/* set_frame_rate(gspca_dev); */
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001330
Jean-François Moine14b67c22011-01-13 05:58:04 -03001331 return gspca_dev->usb_err;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001332}
1333
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001334static int sd_start(struct gspca_dev *gspca_dev)
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001335{
Jean-François Moine458efe22011-02-10 10:11:04 -03001336 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine191d0e72009-05-22 04:16:42 -03001337 int mode;
Jean-François Moine458efe22011-02-10 10:11:04 -03001338 static const struct reg_array bridge_start[NSENSORS][2] = {
1339 [SENSOR_OV767x] = {{bridge_start_qvga_767x,
1340 ARRAY_SIZE(bridge_start_qvga_767x)},
1341 {bridge_start_vga_767x,
1342 ARRAY_SIZE(bridge_start_vga_767x)}},
1343 [SENSOR_OV772x] = {{bridge_start_qvga_772x,
1344 ARRAY_SIZE(bridge_start_qvga_772x)},
1345 {bridge_start_vga_772x,
1346 ARRAY_SIZE(bridge_start_vga_772x)}},
1347 };
1348 static const struct reg_array sensor_start[NSENSORS][2] = {
1349 [SENSOR_OV767x] = {{sensor_start_qvga_767x,
1350 ARRAY_SIZE(sensor_start_qvga_767x)},
1351 {sensor_start_vga_767x,
1352 ARRAY_SIZE(sensor_start_vga_767x)}},
1353 [SENSOR_OV772x] = {{sensor_start_qvga_772x,
1354 ARRAY_SIZE(sensor_start_qvga_772x)},
1355 {sensor_start_vga_772x,
1356 ARRAY_SIZE(sensor_start_vga_772x)}},
1357 };
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001358
Jean-François Moine458efe22011-02-10 10:11:04 -03001359 /* (from ms-win trace) */
1360 if (sd->sensor == SENSOR_OV767x)
1361 sccb_reg_write(gspca_dev, 0x1e, 0x04);
1362 /* black sun enable ? */
1363
1364 mode = gspca_dev->curr_mode; /* 0: 320x240, 1: 640x480 */
1365 reg_w_array(gspca_dev, bridge_start[sd->sensor][mode].val,
1366 bridge_start[sd->sensor][mode].len);
1367 sccb_w_array(gspca_dev, sensor_start[sd->sensor][mode].val,
1368 sensor_start[sd->sensor][mode].len);
1369
Jean-Francois Moine69f1fe22009-11-12 06:10:36 -03001370 set_frame_rate(gspca_dev);
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001371
Antonio Ospitee89fca92012-05-14 08:07:45 -03001372 if (!(gspca_dev->ctrl_dis & (1 << HUE)))
1373 sethue(gspca_dev);
Antonio Ospitee0fde592012-05-14 08:07:43 -03001374 setsaturation(gspca_dev);
Jean-François Moine228dd262011-02-10 09:34:57 -03001375 if (!(gspca_dev->ctrl_dis & (1 << AGC)))
1376 setagc(gspca_dev);
Jean-Francois Moineb014f942009-11-11 14:28:53 -03001377 setawb(gspca_dev);
Max Thrunf2938822010-02-27 17:20:21 -03001378 setaec(gspca_dev);
Jean-François Moine228dd262011-02-10 09:34:57 -03001379 if (!(gspca_dev->ctrl_dis & (1 << GAIN)))
1380 setgain(gspca_dev);
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001381 setexposure(gspca_dev);
1382 setbrightness(gspca_dev);
1383 setcontrast(gspca_dev);
Jean-François Moine228dd262011-02-10 09:34:57 -03001384 if (!(gspca_dev->ctrl_dis & (1 << SHARPNESS)))
1385 setsharpness(gspca_dev);
1386 sethvflip(gspca_dev);
1387 setlightfreq(gspca_dev);
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001388
1389 ov534_set_led(gspca_dev, 1);
1390 ov534_reg_write(gspca_dev, 0xe0, 0x00);
Jean-François Moine14b67c22011-01-13 05:58:04 -03001391 return gspca_dev->usb_err;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001392}
1393
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001394static void sd_stopN(struct gspca_dev *gspca_dev)
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001395{
1396 ov534_reg_write(gspca_dev, 0xe0, 0x09);
1397 ov534_set_led(gspca_dev, 0);
1398}
1399
Jim Parisfb139222008-12-04 04:52:40 -03001400/* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1401#define UVC_STREAM_EOH (1 << 7)
1402#define UVC_STREAM_ERR (1 << 6)
1403#define UVC_STREAM_STI (1 << 5)
1404#define UVC_STREAM_RES (1 << 4)
1405#define UVC_STREAM_SCR (1 << 3)
1406#define UVC_STREAM_PTS (1 << 2)
1407#define UVC_STREAM_EOF (1 << 1)
1408#define UVC_STREAM_FID (1 << 0)
1409
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001410static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1411 u8 *data, int len)
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001412{
Jean-Francois Moine8c252052008-12-04 05:06:08 -03001413 struct sd *sd = (struct sd *) gspca_dev;
Jim Parisfb139222008-12-04 04:52:40 -03001414 __u32 this_pts;
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001415 u16 this_fid;
Jim Paris0f7a50b2008-12-10 05:45:14 -03001416 int remaining_len = len;
Jean-François Moine458efe22011-02-10 10:11:04 -03001417 int payload_len;
Jim Paris0f7a50b2008-12-10 05:45:14 -03001418
Jean-François Moine458efe22011-02-10 10:11:04 -03001419 payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001420 do {
Jean-François Moine458efe22011-02-10 10:11:04 -03001421 len = min(remaining_len, payload_len);
Jim Paris0f7a50b2008-12-10 05:45:14 -03001422
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001423 /* Payloads are prefixed with a UVC-style header. We
1424 consider a frame to start when the FID toggles, or the PTS
1425 changes. A frame ends when EOF is set, and we've received
1426 the correct number of bytes. */
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001427
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001428 /* Verify UVC header. Header length is always 12 */
1429 if (data[0] != 12 || len < 12) {
1430 PDEBUG(D_PACK, "bad header");
Jim Parisfb139222008-12-04 04:52:40 -03001431 goto discard;
1432 }
1433
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001434 /* Check errors */
1435 if (data[1] & UVC_STREAM_ERR) {
1436 PDEBUG(D_PACK, "payload error");
1437 goto discard;
Jean-Francois Moine3481c192009-03-19 06:05:06 -03001438 }
Jim Parisfb139222008-12-04 04:52:40 -03001439
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001440 /* Extract PTS and FID */
1441 if (!(data[1] & UVC_STREAM_PTS)) {
1442 PDEBUG(D_PACK, "PTS not present");
1443 goto discard;
1444 }
1445 this_pts = (data[5] << 24) | (data[4] << 16)
1446 | (data[3] << 8) | data[2];
1447 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
1448
1449 /* If PTS or FID has changed, start a new frame. */
1450 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
Jean-Francois Moineed471192009-04-23 13:52:27 -03001451 if (gspca_dev->last_packet_type == INTER_PACKET)
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001452 gspca_frame_add(gspca_dev, LAST_PACKET,
1453 NULL, 0);
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001454 sd->last_pts = this_pts;
1455 sd->last_fid = this_fid;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001456 gspca_frame_add(gspca_dev, FIRST_PACKET,
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001457 data + 12, len - 12);
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001458 /* If this packet is marked as EOF, end the frame */
Jean-Francois Moineed471192009-04-23 13:52:27 -03001459 } else if (data[1] & UVC_STREAM_EOF) {
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001460 sd->last_pts = 0;
Jean-François Moine458efe22011-02-10 10:11:04 -03001461 if (gspca_dev->pixfmt == V4L2_PIX_FMT_YUYV
1462 && gspca_dev->image_len + len - 12 !=
1463 gspca_dev->width * gspca_dev->height * 2) {
Antonio Ospite11edebc2010-01-17 03:42:14 -03001464 PDEBUG(D_PACK, "wrong sized frame");
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001465 goto discard;
1466 }
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001467 gspca_frame_add(gspca_dev, LAST_PACKET,
1468 data + 12, len - 12);
Jean-Francois Moineed471192009-04-23 13:52:27 -03001469 } else {
1470
1471 /* Add the data from this payload */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001472 gspca_frame_add(gspca_dev, INTER_PACKET,
1473 data + 12, len - 12);
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001474 }
1475
1476 /* Done this payload */
1477 goto scan_next;
Jim Parisfb139222008-12-04 04:52:40 -03001478
1479discard:
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001480 /* Discard data until a new frame starts. */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001481 gspca_dev->last_packet_type = DISCARD_PACKET;
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -03001482
1483scan_next:
1484 remaining_len -= len;
1485 data += len;
1486 } while (remaining_len > 0);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001487}
1488
Max Thrun8b7fbda2010-02-27 17:20:20 -03001489static int sd_setagc(struct gspca_dev *gspca_dev, __s32 val)
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001490{
1491 struct sd *sd = (struct sd *) gspca_dev;
1492
Jean-François Moine228dd262011-02-10 09:34:57 -03001493 sd->ctrls[AGC].val = val;
Jean-Francois Moine2d19a2c2009-11-12 16:15:44 -03001494
Jean-François Moine228dd262011-02-10 09:34:57 -03001495 /* the auto white balance control works only
1496 * when auto gain is set */
1497 if (val) {
1498 gspca_dev->ctrl_inac &= ~(1 << AWB);
1499 } else {
1500 gspca_dev->ctrl_inac |= (1 << AWB);
1501 if (sd->ctrls[AWB].val) {
1502 sd->ctrls[AWB].val = 0;
1503 if (gspca_dev->streaming)
1504 setawb(gspca_dev);
1505 }
Jean-Francois Moinec22c4a22009-11-24 05:22:05 -03001506 }
Jean-Francois Moineb014f942009-11-11 14:28:53 -03001507 if (gspca_dev->streaming)
Jean-François Moine228dd262011-02-10 09:34:57 -03001508 setagc(gspca_dev);
1509 return gspca_dev->usb_err;
Mosalam Ebrahimi4b787542010-03-08 13:52:17 -03001510}
1511
1512static int sd_querymenu(struct gspca_dev *gspca_dev,
1513 struct v4l2_querymenu *menu)
1514{
1515 switch (menu->id) {
1516 case V4L2_CID_POWER_LINE_FREQUENCY:
1517 switch (menu->index) {
1518 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1519 strcpy((char *) menu->name, "Disabled");
1520 return 0;
1521 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1522 strcpy((char *) menu->name, "50 Hz");
1523 return 0;
1524 }
1525 break;
1526 }
1527
1528 return -EINVAL;
1529}
1530
Jim Paris11d9f252008-12-10 06:06:20 -03001531/* get stream parameters (framerate) */
Jean-François Moine668f44a2010-12-25 13:46:14 -03001532static void sd_get_streamparm(struct gspca_dev *gspca_dev,
Jean-Francois Moine5c1a15a2008-12-21 15:02:54 -03001533 struct v4l2_streamparm *parm)
Jim Paris11d9f252008-12-10 06:06:20 -03001534{
1535 struct v4l2_captureparm *cp = &parm->parm.capture;
1536 struct v4l2_fract *tpf = &cp->timeperframe;
1537 struct sd *sd = (struct sd *) gspca_dev;
1538
Jim Paris11d9f252008-12-10 06:06:20 -03001539 cp->capability |= V4L2_CAP_TIMEPERFRAME;
1540 tpf->numerator = 1;
1541 tpf->denominator = sd->frame_rate;
Jim Paris11d9f252008-12-10 06:06:20 -03001542}
1543
1544/* set stream parameters (framerate) */
Jean-François Moine668f44a2010-12-25 13:46:14 -03001545static void sd_set_streamparm(struct gspca_dev *gspca_dev,
Jean-Francois Moine5c1a15a2008-12-21 15:02:54 -03001546 struct v4l2_streamparm *parm)
Jim Paris11d9f252008-12-10 06:06:20 -03001547{
1548 struct v4l2_captureparm *cp = &parm->parm.capture;
1549 struct v4l2_fract *tpf = &cp->timeperframe;
1550 struct sd *sd = (struct sd *) gspca_dev;
1551
Jim Paris11d9f252008-12-10 06:06:20 -03001552 /* Set requested framerate */
1553 sd->frame_rate = tpf->denominator / tpf->numerator;
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001554 if (gspca_dev->streaming)
Jean-Francois Moine69f1fe22009-11-12 06:10:36 -03001555 set_frame_rate(gspca_dev);
Jim Paris11d9f252008-12-10 06:06:20 -03001556
1557 /* Return the actual framerate */
1558 tpf->numerator = 1;
1559 tpf->denominator = sd->frame_rate;
Jim Paris11d9f252008-12-10 06:06:20 -03001560}
1561
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001562/* sub-driver description */
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001563static const struct sd_desc sd_desc = {
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001564 .name = MODULE_NAME,
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001565 .ctrls = sd_ctrls,
1566 .nctrls = ARRAY_SIZE(sd_ctrls),
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001567 .config = sd_config,
1568 .init = sd_init,
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001569 .start = sd_start,
1570 .stopN = sd_stopN,
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001571 .pkt_scan = sd_pkt_scan,
Mosalam Ebrahimi4b787542010-03-08 13:52:17 -03001572 .querymenu = sd_querymenu,
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001573 .get_streamparm = sd_get_streamparm,
1574 .set_streamparm = sd_set_streamparm,
1575};
1576
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001577/* -- module initialisation -- */
Jean-François Moine95c967c2011-01-13 05:20:29 -03001578static const struct usb_device_id device_table[] = {
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001579 {USB_DEVICE(0x1415, 0x2000)},
Jean-François Moine458efe22011-02-10 10:11:04 -03001580 {USB_DEVICE(0x06f8, 0x3002)},
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001581 {}
1582};
1583
1584MODULE_DEVICE_TABLE(usb, device_table);
1585
1586/* -- device connect -- */
1587static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1588{
Jean-Francois Moinec52af792010-01-07 05:18:16 -03001589 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
Jean-Francois Moine189d92a2009-11-11 07:46:28 -03001590 THIS_MODULE);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001591}
1592
1593static struct usb_driver sd_driver = {
1594 .name = MODULE_NAME,
1595 .id_table = device_table,
1596 .probe = sd_probe,
1597 .disconnect = gspca_disconnect,
1598#ifdef CONFIG_PM
1599 .suspend = gspca_suspend,
1600 .resume = gspca_resume,
1601#endif
1602};
1603
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08001604module_usb_driver(sd_driver);